bug fix in wbutton.c, made mouse pointer go invisible when typing in
[wmaker-crm.git] / src / dock.c
blob7d18b9fb7aa8df59fc48fd46d067e476f466b4c0
1 /* dock.c- built-in Dock module for WindowMaker
2 *
3 * Window Maker window manager
4 *
5 * Copyright (c) 1997, 1998 Alfredo K. Kojima
6 * Copyright (c) 1998, 1999 Dan Pascu
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
21 * USA.
25 #include "wconfig.h"
27 #include <X11/Xlib.h>
28 #include <X11/Xutil.h>
29 #include <stdlib.h>
30 #include <string.h>
31 #include <unistd.h>
32 #include <math.h>
33 #include <limits.h>
35 #ifndef PATH_MAX
36 #define PATH_MAX DEFAULT_PATH_MAX
37 #endif
39 #include "WindowMaker.h"
40 #include "wcore.h"
41 #include "window.h"
42 #include "icon.h"
43 #include "appicon.h"
44 #include "actions.h"
45 #include "stacking.h"
46 #include "dock.h"
47 #include "dialog.h"
48 #include "funcs.h"
49 #include "properties.h"
50 #include "menu.h"
51 #include "client.h"
52 #include "defaults.h"
53 #include "workspace.h"
54 #include "framewin.h"
55 #include "superfluous.h"
57 #include "list.h"
59 #include <proplist.h>
64 /**** Local variables ****/
65 #define CLIP_REWIND 1
66 #define CLIP_IDLE 0
67 #define CLIP_FORWARD 2
70 /**** Global variables ****/
72 /* in dockedapp.c */
73 extern void DestroyDockAppSettingsPanel();
75 extern void ShowDockAppSettingsPanel(WAppIcon *aicon);
78 extern XContext wWinContext;
80 extern Cursor wCursor[WCUR_LAST];
82 extern WPreferences wPreferences;
84 extern XContext wWinContext;
86 #ifdef OFFIX_DND
87 extern Atom _XA_DND_PROTOCOL;
88 #endif
91 #define MOD_MASK wPreferences.modifier_mask
93 extern void appIconMouseDown(WObjDescriptor *desc, XEvent *event);
95 #define ICON_SIZE wPreferences.icon_size
98 /***** Local variables ****/
100 static proplist_t dCommand=NULL;
101 #ifdef OFFIX_DND
102 static proplist_t dDropCommand=NULL;
103 #endif
104 static proplist_t dAutoLaunch, dName, dForced, dBuggyApplication, dYes, dNo;
105 static proplist_t dHost, dDock, dClip;
106 static proplist_t dAutoAttractIcons, dKeepAttracted;
108 static proplist_t dPosition, dApplications, dLowered, dCollapsed, dAutoCollapse;
110 static proplist_t dAutoRaiseLower;
112 static void dockIconPaint(WAppIcon *btn);
114 static void iconMouseDown(WObjDescriptor *desc, XEvent *event);
116 static pid_t execCommand(WAppIcon *btn, char *command, WSavedState *state);
118 static void trackDeadProcess(pid_t pid, unsigned char status, WDock *dock);
120 static int getClipButton(int px, int py);
122 static void toggleLowered(WDock *dock);
124 static void toggleCollapsed(WDock *dock);
126 static void clipIconExpose(WObjDescriptor *desc, XEvent *event);
128 static void clipLeave(WDock *dock);
130 static void handleClipChangeWorkspace(WScreen *scr, XEvent *event);
132 Bool moveIconBetweenDocks(WDock *src, WDock *dest, WAppIcon *icon, int x, int y);
134 static void clipEnterNotify(WObjDescriptor *desc, XEvent *event);
135 static void clipLeaveNotify(WObjDescriptor *desc, XEvent *event);
136 static void clipAutoCollapse(void *cdata);
137 static void clipAutoExpand(void *cdata);
138 static void launchDockedApplication(WAppIcon *btn);
140 static void clipAutoLower(void *cdata);
141 static void clipAutoRaise(void *cdata);
143 static void showClipBalloon(WDock *dock, int workspace);
145 #ifdef OFFIX_DND
147 #define DndNotDnd -1
148 #define DndUnknown 0
149 #define DndRawData 1
150 #define DndFile 2
151 #define DndFiles 3
152 #define DndText 4
153 #define DndDir 5
154 #define DndLink 6
155 #define DndExe 7
157 #define DndEND 8
159 #endif /* OFFIX_DND */
163 static void
164 make_keys()
166 if (dCommand!=NULL)
167 return;
169 dCommand = PLRetain(PLMakeString("Command"));
170 #ifdef OFFIX_DND
171 dDropCommand = PLRetain(PLMakeString("DropCommand"));
172 #endif
173 dAutoLaunch = PLRetain(PLMakeString("AutoLaunch"));
174 dName = PLRetain(PLMakeString("Name"));
175 dForced = PLRetain(PLMakeString("Forced"));
176 dBuggyApplication = PLRetain(PLMakeString("BuggyApplication"));
177 dYes = PLRetain(PLMakeString("Yes"));
178 dNo = PLRetain(PLMakeString("No"));
179 dHost = PLRetain(PLMakeString("Host"));
181 dPosition = PLMakeString("Position");
182 dApplications = PLMakeString("Applications");
183 dLowered = PLMakeString("Lowered");
184 dCollapsed = PLMakeString("Collapsed");
185 dAutoCollapse = PLMakeString("AutoCollapse");
186 dAutoRaiseLower = PLMakeString("AutoRaiseLower");
187 dAutoAttractIcons = PLMakeString("AutoAttractIcons");
188 dKeepAttracted = PLMakeString("KeepAttracted");
190 dDock = PLMakeString("Dock");
191 dClip = PLMakeString("Clip");
196 static void
197 renameCallback(WMenu *menu, WMenuEntry *entry)
199 WDock *dock = entry->clientdata;
200 char buffer[128];
201 int wspace;
202 char *name;
204 assert(entry->clientdata!=NULL);
206 wspace = dock->screen_ptr->current_workspace;
208 name = wstrdup(dock->screen_ptr->workspaces[wspace]->name);
210 sprintf(buffer, _("Type the name for workspace %i:"), wspace+1);
211 if (wInputDialog(dock->screen_ptr, _("Rename Workspace"), buffer,
212 &name)) {
213 wWorkspaceRename(dock->screen_ptr, wspace, name);
215 if (name) {
216 free(name);
221 static void
222 toggleLoweredCallback(WMenu *menu, WMenuEntry *entry)
224 assert(entry->clientdata!=NULL);
226 toggleLowered(entry->clientdata);
228 entry->flags.indicator_on = !((WDock*)entry->clientdata)->lowered;
230 wMenuPaint(menu);
235 static void
236 killCallback(WMenu *menu, WMenuEntry *entry)
238 WAppIcon *icon;
239 #ifdef REDUCE_APPICONS
240 WAppIconAppList *tapplist;
242 extern Atom _XA_WM_DELETE_WINDOW;
243 #else
244 char *buffer;
245 #endif
247 if (!WCHECK_STATE(WSTATE_NORMAL))
248 return;
250 assert(entry->clientdata!=NULL);
252 icon = (WAppIcon*)entry->clientdata;
254 icon->editing = 1;
256 WCHANGE_STATE(WSTATE_MODAL);
258 #ifdef REDUCE_APPICONS
259 /* Send a delete message to the main window of each application
260 * bound to this docked appicon. - cls
262 tapplist = icon->applist;
263 while (tapplist != NULL) {
264 if (tapplist->wapp->main_window_desc != NULL) {
265 if (tapplist->wapp->main_window_desc->protocols.DELETE_WINDOW) {
266 wClientSendProtocol(tapplist->wapp->main_window_desc,
267 _XA_WM_DELETE_WINDOW, CurrentTime);
268 } else {
269 wClientKill(tapplist->wapp->main_window_desc);
272 tapplist = tapplist->next;
274 #else
275 buffer = wstrappend(icon->wm_class,
276 _(" will be forcibly closed.\n"
277 "Any unsaved changes will be lost.\n"
278 "Please confirm."));
280 if (wPreferences.dont_confirm_kill
281 || wMessageDialog(menu->frame->screen_ptr, _("Kill Application"),
282 buffer, _("Yes"), _("No"), NULL)==WAPRDefault) {
283 if (icon->icon && icon->icon->owner) {
284 wClientKill(icon->icon->owner);
287 #endif /* !REDUCE_APPICONS */
289 icon->editing = 0;
291 WCHANGE_STATE(WSTATE_NORMAL);}
294 static LinkedList*
295 getSelected(WDock *dock)
297 LinkedList *ret=NULL;
298 WAppIcon *btn;
299 int i;
301 for (i=1; i<dock->max_icons; i++) {
302 btn = dock->icon_array[i];
303 if (btn && btn->icon->selected) {
304 ret = list_cons(btn, ret);
308 return ret;
312 static void
313 paintClipButtons(WAppIcon *clipIcon, Bool lpushed, Bool rpushed)
315 Window win = clipIcon->icon->core->window;
316 WScreen *scr = clipIcon->icon->core->screen_ptr;
317 XPoint p[4];
318 int pt = CLIP_BUTTON_SIZE*ICON_SIZE/64;
319 int tp = ICON_SIZE - pt;
320 int as = pt - 15; /* 15 = 5+5+5 */
321 GC gc = scr->clip_title_gc;
322 #ifdef GRADIENT_CLIP_ARROW
323 Bool collapsed = clipIcon->dock->collapsed;
324 #endif
326 if (rpushed) {
327 p[0].x = tp+1;
328 p[0].y = 1;
329 p[1].x = ICON_SIZE-2;
330 p[1].y = 1;
331 p[2].x = ICON_SIZE-2;
332 p[2].y = pt-1;
333 } else if (lpushed) {
334 p[0].x = 1;
335 p[0].y = tp;
336 p[1].x = pt;
337 p[1].y = ICON_SIZE-2;
338 p[2].x = 1;
339 p[2].y = ICON_SIZE-2;
341 if (lpushed || rpushed) {
342 XSetForeground(dpy, scr->draw_gc, scr->white_pixel);
343 XFillPolygon(dpy, win, scr->draw_gc, p, 3, Convex, CoordModeOrigin);
344 XSetForeground(dpy, scr->draw_gc, scr->black_pixel);
346 #ifdef GRADIENT_CLIP_ARROW
347 if (!collapsed) {
348 XSetFillStyle(dpy, scr->copy_gc, FillTiled);
349 XSetTile(dpy, scr->copy_gc, scr->clip_arrow_gradient);
350 XSetClipMask(dpy, scr->copy_gc, None);
351 gc = scr->copy_gc;
353 #endif /* GRADIENT_CLIP_ARROW */
355 /* top right arrow */
356 p[0].x = p[3].x = ICON_SIZE-5-as;
357 p[0].y = p[3].y = 5;
358 p[1].x = ICON_SIZE-6;
359 p[1].y = 5;
360 p[2].x = ICON_SIZE-6;
361 p[2].y = 4+as;
362 if (rpushed) {
363 XFillPolygon(dpy, win, scr->draw_gc, p, 3, Convex, CoordModeOrigin);
364 XDrawLines(dpy, win, scr->draw_gc, p, 4, CoordModeOrigin);
365 } else {
366 #ifdef GRADIENT_CLIP_ARROW
367 if (!collapsed)
368 XSetTSOrigin(dpy, gc, ICON_SIZE-6-as, 5);
369 #endif
370 XFillPolygon(dpy, win, gc, p,3,Convex,CoordModeOrigin);
371 XDrawLines(dpy, win, gc, p,4,CoordModeOrigin);
374 /* bottom left arrow */
375 p[0].x = p[3].x = 5;
376 p[0].y = p[3].y = ICON_SIZE-5-as;
377 p[1].x = 5;
378 p[1].y = ICON_SIZE-6;
379 p[2].x = 4+as;
380 p[2].y = ICON_SIZE-6;
381 if (lpushed) {
382 XFillPolygon(dpy, win, scr->draw_gc, p, 3, Convex, CoordModeOrigin);
383 XDrawLines(dpy, win, scr->draw_gc, p, 4, CoordModeOrigin);
384 } else {
385 #ifdef GRADIENT_CLIP_ARROW
386 if (!collapsed)
387 XSetTSOrigin(dpy, gc, 5, ICON_SIZE-6-as);
388 #endif
389 XFillPolygon(dpy, win, gc, p,3,Convex,CoordModeOrigin);
390 XDrawLines(dpy, win, gc, p,4,CoordModeOrigin);
392 #ifdef GRADIENT_CLIP_ARROW
393 if (!collapsed)
394 XSetFillStyle(dpy, scr->copy_gc, FillSolid);
395 #endif
399 RImage*
400 wClipMakeTile(WScreen *scr, RImage *normalTile)
402 RImage *tile = RCloneImage(normalTile);
403 RColor black;
404 RColor dark;
405 RColor light;
406 int pt, tp;
407 int as;
409 pt = CLIP_BUTTON_SIZE*wPreferences.icon_size/64;
410 tp = wPreferences.icon_size-1 - pt;
411 as = pt - 15;
413 black.alpha = 255;
414 black.red = black.green = black.blue = 0;
416 dark.alpha = 0;
417 dark.red = dark.green = dark.blue = 60;
419 light.alpha = 0;
420 light.red = light.green = light.blue = 80;
423 /* top right */
424 ROperateLine(tile, RSubtractOperation, tp, 0, wPreferences.icon_size-2,
425 pt-1, &dark);
426 RDrawLine(tile, tp-1, 0, wPreferences.icon_size-1, pt+1, &black);
427 ROperateLine(tile, RAddOperation, tp, 2, wPreferences.icon_size-3,
428 pt, &light);
430 /* arrow bevel */
431 ROperateLine(tile, RSubtractOperation, ICON_SIZE - 7 - as, 4,
432 ICON_SIZE - 5, 4, &dark);
433 ROperateLine(tile, RSubtractOperation, ICON_SIZE - 6 - as, 5,
434 ICON_SIZE - 5, 6 + as, &dark);
435 ROperateLine(tile, RAddOperation, ICON_SIZE - 5, 4, ICON_SIZE - 5, 6 + as,
436 &light);
438 /* bottom left */
439 ROperateLine(tile, RAddOperation, 2, tp+2, pt-2,
440 wPreferences.icon_size-3, &dark);
441 RDrawLine(tile, 0, tp-1, pt+1, wPreferences.icon_size-1, &black);
442 ROperateLine(tile, RSubtractOperation, 0, tp-2, pt+1,
443 wPreferences.icon_size-2, &light);
445 /* arrow bevel */
446 ROperateLine(tile, RSubtractOperation, 4, ICON_SIZE - 7 - as, 4,
447 ICON_SIZE - 5, &dark);
448 ROperateLine(tile, RSubtractOperation, 5, ICON_SIZE - 6 - as,
449 6 + as, ICON_SIZE - 5, &dark);
450 ROperateLine(tile, RAddOperation, 4, ICON_SIZE - 5, 6 + as, ICON_SIZE - 5,
451 &light);
453 return tile;
457 static void
458 removeIconsCallback(WMenu *menu, WMenuEntry *entry)
460 WAppIcon *clickedIcon = (WAppIcon*)entry->clientdata;
461 WDock *dock;
462 WAppIcon *aicon;
463 LinkedList *selectedIcons;
464 int keepit;
466 assert(clickedIcon!=NULL);
468 dock = clickedIcon->dock;
470 selectedIcons = getSelected(dock);
472 if (selectedIcons) {
473 if (wMessageDialog(dock->screen_ptr, _("Workspace Clip"),
474 _("All selected icons will be removed!"),
475 _("OK"), _("Cancel"), NULL)!=WAPRDefault) {
476 return;
478 } else {
479 if (clickedIcon->xindex==0 && clickedIcon->yindex==0)
480 return;
481 selectedIcons = list_cons(clickedIcon, NULL);
484 while (selectedIcons) {
485 aicon = selectedIcons->head;
486 keepit = aicon->running && wApplicationOf(aicon->main_window);
487 wDockDetach(dock, aicon);
488 if (keepit) {
489 PlaceIcon(dock->screen_ptr, &aicon->x_pos, &aicon->y_pos);
490 XMoveWindow(dpy, aicon->icon->core->window,
491 aicon->x_pos, aicon->y_pos);
492 if (!dock->mapped || dock->collapsed)
493 XMapWindow(dpy, aicon->icon->core->window);
495 list_remove_head(&selectedIcons);
498 if (wPreferences.auto_arrange_icons)
499 wArrangeIcons(dock->screen_ptr, True);
503 static void
504 keepIconsCallback(WMenu *menu, WMenuEntry *entry)
506 WAppIcon *clickedIcon = (WAppIcon*)entry->clientdata;
507 WDock *dock;
508 WAppIcon *aicon;
509 LinkedList *selectedIcons;
511 assert(clickedIcon!=NULL);
512 dock = clickedIcon->dock;
514 selectedIcons = getSelected(dock);
516 if (!selectedIcons && clickedIcon!=dock->screen_ptr->clip_icon) {
517 char *command = NULL;
519 if (!clickedIcon->command && !clickedIcon->editing) {
520 clickedIcon->editing = 1;
521 if (wInputDialog(dock->screen_ptr, _("Keep Icon"),
522 _("Type the command used to launch the application"),
523 &command)) {
524 if (command && (command[0]==0 ||
525 (command[0]=='-' && command[1]==0))) {
526 free(command);
527 command = NULL;
529 clickedIcon->command = command;
530 clickedIcon->editing = 0;
531 } else {
532 clickedIcon->editing = 0;
533 if (command)
534 free(command);
535 return;
539 selectedIcons = list_cons(clickedIcon, NULL);
542 while (selectedIcons) {
543 aicon = selectedIcons->head;
544 if (aicon->icon->selected)
545 wIconSelect(aicon->icon);
546 if (aicon && aicon->attracted && aicon->command) {
547 aicon->attracted = 0;
548 if (aicon->icon->shadowed) {
549 aicon->icon->shadowed = 0;
550 aicon->icon->force_paint = 1;
551 wAppIconPaint(aicon);
554 list_remove_head(&selectedIcons);
561 static void
562 toggleAutoAttractCallback(WMenu *menu, WMenuEntry *entry)
564 WDock *dock = (WDock*)entry->clientdata;
566 assert(entry->clientdata!=NULL);
568 dock->attract_icons = !dock->attract_icons;
569 /*if (!dock->attract_icons)
570 dock->keep_attracted = 0;*/
572 entry->flags.indicator_on = dock->attract_icons;
574 wMenuPaint(menu);
578 static void
579 toggleKeepCallback(WMenu *menu, WMenuEntry *entry)
581 WDock *dock = (WDock*)entry->clientdata;
582 WAppIcon *btn;
583 int i;
585 assert(entry->clientdata!=NULL);
587 dock->keep_attracted = !dock->keep_attracted;
589 if (dock->keep_attracted) {
590 for (i=0; i< dock->max_icons; i++) {
591 btn = dock->icon_array[i];
592 if (btn && btn->attracted && btn->command) {
593 btn->attracted = 0;
594 if (btn->icon->shadowed) {
595 btn->icon->shadowed = 0;
596 btn->icon->force_paint = 1;
597 wAppIconPaint(btn);
603 entry->flags.indicator_on = dock->keep_attracted;
605 wMenuPaint(menu);
609 static void
610 selectCallback(WMenu *menu, WMenuEntry *entry)
612 WAppIcon *icon = (WAppIcon*)entry->clientdata;
614 assert(icon!=NULL);
616 wIconSelect(icon->icon);
618 wMenuPaint(menu);
622 static void
623 colectIconsCallback(WMenu *menu, WMenuEntry *entry)
625 WAppIcon *clickedIcon = (WAppIcon*)entry->clientdata;
626 WDock *clip;
627 WAppIcon *aicon;
628 int x, y, x_pos, y_pos;
630 assert(entry->clientdata!=NULL);
631 clip = clickedIcon->dock;
633 aicon = clip->screen_ptr->app_icon_list;
635 while (aicon) {
636 if (!aicon->docked && wDockFindFreeSlot(clip, &x, &y)) {
637 x_pos = clip->x_pos + x*ICON_SIZE;
638 y_pos = clip->y_pos + y*ICON_SIZE;
639 if (aicon->x_pos != x_pos || aicon->y_pos != y_pos) {
640 #ifdef ANIMATIONS
641 if (wPreferences.no_animations) {
642 XMoveWindow(dpy, aicon->icon->core->window, x_pos, y_pos);
643 } else {
644 SlideWindow(aicon->icon->core->window,
645 aicon->x_pos, aicon->y_pos, x_pos, y_pos);
647 #else
648 XMoveWindow(dpy, aicon->icon->core->window, x_pos, y_pos);
649 #endif /* ANIMATIONS */
651 aicon->attracted = 1;
652 if (!clip->keep_attracted && !aicon->icon->shadowed) {
653 aicon->icon->shadowed = 1;
654 aicon->icon->force_paint = 1;
655 /* We don't do an wAppIconPaint() here because it's in
656 * wDockAttachIcon(). -Dan
659 wDockAttachIcon(clip, aicon, x, y);
660 if (clip->collapsed || !clip->mapped)
661 XUnmapWindow(dpy, aicon->icon->core->window);
663 aicon = aicon->next;
668 static void
669 selectIconsCallback(WMenu *menu, WMenuEntry *entry)
671 WAppIcon *clickedIcon = (WAppIcon*)entry->clientdata;
672 WDock *dock;
673 LinkedList *selectedIcons;
674 WAppIcon *btn;
675 int i;
677 assert(clickedIcon!=NULL);
678 dock = clickedIcon->dock;
680 selectedIcons = getSelected(dock);
682 if (!selectedIcons) {
683 for (i=1; i<dock->max_icons; i++) {
684 btn = dock->icon_array[i];
685 if (btn && !btn->icon->selected) {
686 wIconSelect(btn->icon);
689 } else {
690 while(selectedIcons) {
691 btn = selectedIcons->head;
692 wIconSelect(btn->icon);
693 list_remove_head(&selectedIcons);
697 wMenuPaint(menu);
701 static void
702 toggleCollapsedCallback(WMenu *menu, WMenuEntry *entry)
704 assert(entry->clientdata!=NULL);
706 toggleCollapsed(entry->clientdata);
708 entry->flags.indicator_on = ((WDock*)entry->clientdata)->collapsed;
710 wMenuPaint(menu);
714 static void
715 toggleAutoCollapseCallback(WMenu *menu, WMenuEntry *entry)
717 WDock *dock;
718 assert(entry->clientdata!=NULL);
720 dock = (WDock*) entry->clientdata;
722 dock->auto_collapse = !dock->auto_collapse;
724 entry->flags.indicator_on = ((WDock*)entry->clientdata)->auto_collapse;
726 wMenuPaint(menu);
730 static void
731 toggleAutoRaiseLowerCallback(WMenu *menu, WMenuEntry *entry)
733 WDock *dock;
734 assert(entry->clientdata!=NULL);
736 dock = (WDock*) entry->clientdata;
738 dock->auto_raise_lower = !dock->auto_raise_lower;
740 entry->flags.indicator_on = ((WDock*)entry->clientdata)->auto_raise_lower;
742 wMenuPaint(menu);
746 static void
747 launchCallback(WMenu *menu, WMenuEntry *entry)
749 WAppIcon *btn = (WAppIcon*)entry->clientdata;
751 launchDockedApplication(btn);
755 static void
756 settingsCallback(WMenu *menu, WMenuEntry *entry)
758 WAppIcon *btn = (WAppIcon*)entry->clientdata;
760 if (btn->editing)
761 return;
762 ShowDockAppSettingsPanel(btn);
766 static void
767 hideCallback(WMenu *menu, WMenuEntry *entry)
769 WApplication *wapp;
770 WAppIcon *btn = (WAppIcon*)entry->clientdata;
772 wapp = wApplicationOf(btn->icon->owner->main_window);
774 if (wapp->flags.hidden) {
775 wWorkspaceChange(btn->icon->core->screen_ptr,wapp->last_workspace);
776 wUnhideApplication(wapp, False, False);
777 } else {
778 wHideApplication(wapp);
783 static void
784 unhideHereCallback(WMenu *menu, WMenuEntry *entry)
786 WApplication *wapp;
787 WAppIcon *btn = (WAppIcon*)entry->clientdata;
789 wapp = wApplicationOf(btn->icon->owner->main_window);
791 wUnhideApplication(wapp, False, True);
795 WAppIcon*
796 mainIconCreate(WScreen *scr, int type)
798 WAppIcon *btn;
799 int x_pos;
801 if (type == WM_CLIP) {
802 if (scr->clip_icon)
803 return scr->clip_icon;
804 btn = wAppIconCreateForDock(scr, NULL, "Logo", "WMClip", TILE_CLIP);
805 btn->icon->core->descriptor.handle_expose = clipIconExpose;
806 btn->icon->core->descriptor.handle_enternotify = clipEnterNotify;
807 btn->icon->core->descriptor.handle_leavenotify = clipLeaveNotify;
808 /*x_pos = scr->scr_width - ICON_SIZE*2 - DOCK_EXTRA_SPACE;*/
809 x_pos = 0;
810 } else {
811 btn = wAppIconCreateForDock(scr, NULL, "Logo", "WMDock", TILE_NORMAL);
812 x_pos = scr->scr_width - ICON_SIZE - DOCK_EXTRA_SPACE;
815 btn->xindex = 0;
816 btn->yindex = 0;
818 btn->icon->core->descriptor.handle_mousedown = iconMouseDown;
819 btn->icon->core->descriptor.parent_type = WCLASS_DOCK_ICON;
820 btn->icon->core->descriptor.parent = btn;
821 /*ChangeStackingLevel(btn->icon->core, WMDockLevel);*/
822 XMapWindow(dpy, btn->icon->core->window);
823 btn->x_pos = x_pos;
824 btn->y_pos = 0;
825 btn->docked = 1;
826 if (type == WM_CLIP)
827 scr->clip_icon = btn;
829 return btn;
833 static void
834 switchWSCommand(WMenu *menu, WMenuEntry *entry)
836 WAppIcon *btn, *icon = (WAppIcon*) entry->clientdata;
837 WScreen *scr = icon->icon->core->screen_ptr;
838 WDock *src, *dest;
839 LinkedList *selectedIcons;
840 int x, y;
842 if (entry->order == scr->current_workspace)
843 return;
844 src = icon->dock;
845 dest = scr->workspaces[entry->order]->clip;
847 selectedIcons = getSelected(src);
849 if (selectedIcons) {
850 while(selectedIcons) {
851 btn = selectedIcons->head;
852 if (wDockFindFreeSlot(dest, &x, &y)) {
853 moveIconBetweenDocks(src, dest, btn, x, y);
854 XUnmapWindow(dpy, btn->icon->core->window);
856 list_remove_head(&selectedIcons);
858 } else if (icon != scr->clip_icon) {
859 if (wDockFindFreeSlot(dest, &x, &y)) {
860 moveIconBetweenDocks(src, dest, icon, x, y);
861 XUnmapWindow(dpy, icon->icon->core->window);
868 static void
869 launchDockedApplication(WAppIcon *btn)
871 WScreen *scr = btn->icon->core->screen_ptr;
873 if (!btn->launching && btn->command!=NULL) {
874 if (!btn->forced_dock) {
875 btn->relaunching = btn->running;
876 btn->running = 1;
878 if (btn->wm_instance || btn->wm_class) {
879 WWindowAttributes attr;
880 memset(&attr, 0, sizeof(WWindowAttributes));
881 wDefaultFillAttributes(scr, btn->wm_instance, btn->wm_class,
882 &attr, NULL, True);
884 if (!attr.no_appicon && !btn->buggy_app)
885 btn->launching = 1;
886 else
887 btn->running = 0;
889 btn->drop_launch = 0;
890 scr->last_dock = btn->dock;
891 btn->pid = execCommand(btn, btn->command, NULL);
892 if (btn->pid>0) {
893 if (btn->buggy_app) {
894 /* give feedback that the app was launched */
895 btn->launching = 1;
896 dockIconPaint(btn);
897 btn->launching = 0;
898 WMAddTimerHandler(200, (WMCallback*)dockIconPaint, btn);
899 } else {
900 dockIconPaint(btn);
902 } else {
903 wwarning(_("could not launch application %s\n"), btn->command);
904 btn->launching = 0;
905 if (!btn->relaunching)
906 btn->running = 0;
913 static void
914 updateWorkspaceMenu(WMenu *menu, WAppIcon *icon)
916 WScreen *scr = menu->frame->screen_ptr;
917 char title[MAX_WORKSPACENAME_WIDTH+1];
918 int i;
920 if (!menu || !icon)
921 return;
923 for (i=0; i<scr->workspace_count; i++) {
924 if (i < menu->entry_no) {
925 if (strcmp(menu->entries[i]->text,scr->workspaces[i]->name)!=0) {
926 free(menu->entries[i]->text);
927 strcpy(title, scr->workspaces[i]->name);
928 menu->entries[i]->text = wstrdup(title);
929 menu->flags.realized = 0;
931 menu->entries[i]->clientdata = (void*)icon;
932 } else {
933 strcpy(title, scr->workspaces[i]->name);
935 wMenuAddCallback(menu, title, switchWSCommand, (void*)icon);
937 menu->flags.realized = 0;
939 if (i == scr->current_workspace) {
940 wMenuSetEnabled(menu, i, False);
941 } else {
942 wMenuSetEnabled(menu, i, True);
946 if (!menu->flags.realized)
947 wMenuRealize(menu);
951 static WMenu*
952 makeWorkspaceMenu(WScreen *scr)
954 WMenu *menu;
956 menu = wMenuCreate(scr, NULL, False);
957 if (!menu)
958 wwarning(_("could not create workspace submenu for Clip menu"));
960 wMenuAddCallback(menu, "", switchWSCommand, (void*)scr->clip_icon);
962 menu->flags.realized = 0;
963 wMenuRealize(menu);
965 return menu;
969 static void
970 updateClipOptionsMenu(WMenu *menu, WDock *dock)
972 WMenuEntry *entry;
973 int index = 0;
975 if (!menu || !dock)
976 return;
978 /* keep on top */
979 entry = menu->entries[index];
980 entry->flags.indicator_on = !dock->lowered;
981 entry->clientdata = dock;
983 /* collapsed */
984 entry = menu->entries[++index];
985 entry->flags.indicator_on = dock->collapsed;
986 entry->clientdata = dock;
988 /* auto-collapse */
989 entry = menu->entries[++index];
990 entry->flags.indicator_on = dock->auto_collapse;
991 entry->clientdata = dock;
993 /* auto-raise/lower */
994 entry = menu->entries[++index];
995 entry->flags.indicator_on = dock->auto_raise_lower;
996 entry->clientdata = dock;
998 /* attract icons */
999 entry = menu->entries[++index];
1000 entry->flags.indicator_on = dock->attract_icons;
1001 entry->clientdata = dock;
1003 /* keep attracted icons */
1004 entry = menu->entries[++index];
1005 entry->flags.indicator_on = dock->keep_attracted;
1006 entry->clientdata = dock;
1008 menu->flags.realized = 0;
1009 wMenuRealize(menu);
1013 static WMenu*
1014 makeClipOptionsMenu(WScreen *scr)
1016 WMenu *menu;
1017 WMenuEntry *entry;
1019 menu = wMenuCreate(scr, NULL, False);
1020 if (!menu) {
1021 wwarning(_("could not create options submenu for Clip menu"));
1022 return NULL;
1025 entry = wMenuAddCallback(menu, _("Keep on Top"),
1026 toggleLoweredCallback, NULL);
1027 entry->flags.indicator = 1;
1028 entry->flags.indicator_on = 1;
1029 entry->flags.indicator_type = MI_CHECK;
1031 entry = wMenuAddCallback(menu, _("Collapsed"),
1032 toggleCollapsedCallback, NULL);
1033 entry->flags.indicator = 1;
1034 entry->flags.indicator_on = 1;
1035 entry->flags.indicator_type = MI_CHECK;
1037 entry = wMenuAddCallback(menu, _("AutoCollapse"),
1038 toggleAutoCollapseCallback, NULL);
1039 entry->flags.indicator = 1;
1040 entry->flags.indicator_on = 1;
1041 entry->flags.indicator_type = MI_CHECK;
1043 entry = wMenuAddCallback(menu, _("AutoRaiseLower"),
1044 toggleAutoRaiseLowerCallback, NULL);
1045 entry->flags.indicator = 1;
1046 entry->flags.indicator_on = 1;
1047 entry->flags.indicator_type = MI_CHECK;
1049 entry = wMenuAddCallback(menu, _("AutoAttract Icons"),
1050 toggleAutoAttractCallback, NULL);
1051 entry->flags.indicator = 1;
1052 entry->flags.indicator_on = 1;
1053 entry->flags.indicator_type = MI_CHECK;
1055 entry = wMenuAddCallback(menu, _("Keep Attracted Icons"),
1056 toggleKeepCallback, NULL);
1057 entry->flags.indicator = 1;
1058 entry->flags.indicator_on = 1;
1059 entry->flags.indicator_type = MI_CHECK;
1061 menu->flags.realized = 0;
1062 wMenuRealize(menu);
1064 return menu;
1068 static WMenu*
1069 dockMenuCreate(WScreen *scr, int type)
1071 WMenu *menu;
1072 WMenuEntry *entry;
1074 if (type == WM_CLIP && scr->clip_menu)
1075 return scr->clip_menu;
1077 menu = wMenuCreate(scr, NULL, False);
1078 if (type != WM_CLIP) {
1079 entry = wMenuAddCallback(menu, _("Keep on top"),
1080 toggleLoweredCallback, NULL);
1081 entry->flags.indicator = 1;
1082 entry->flags.indicator_on = 1;
1083 entry->flags.indicator_type = MI_CHECK;
1084 } else {
1085 entry = wMenuAddCallback(menu, _("Clip Options"), NULL, NULL);
1086 scr->clip_options = makeClipOptionsMenu(scr);
1087 if (scr->clip_options)
1088 wMenuEntrySetCascade(menu, entry, scr->clip_options);
1090 wMenuAddCallback(menu, _("Rename Workspace"), renameCallback, NULL);
1092 wMenuAddCallback(menu, _("(Un)Select Icon"), selectCallback, NULL);
1094 wMenuAddCallback(menu, _("(Un)Select All Icons"), selectIconsCallback,
1095 NULL);
1097 wMenuAddCallback(menu, _("Keep Icon(s)"), keepIconsCallback, NULL);
1099 entry = wMenuAddCallback(menu, _("Move Icon(s) To"), NULL, NULL);
1100 scr->clip_submenu = makeWorkspaceMenu(scr);
1101 if (scr->clip_submenu)
1102 wMenuEntrySetCascade(menu, entry, scr->clip_submenu);
1104 wMenuAddCallback(menu, _("Remove Icon(s)"), removeIconsCallback, NULL);
1106 wMenuAddCallback(menu, _("Attract Icons"), colectIconsCallback, NULL);
1109 wMenuAddCallback(menu, _("Launch"), launchCallback, NULL);
1111 wMenuAddCallback(menu, _("Unhide Here"), unhideHereCallback, NULL);
1113 entry = wMenuAddCallback(menu, _("Hide"), hideCallback, NULL);
1114 free(entry->text);
1115 entry->text = _("Hide");
1117 wMenuAddCallback(menu, _("Settings..."), settingsCallback, NULL);
1119 wMenuAddCallback(menu, _("Kill"), killCallback, NULL);
1121 if (type == WM_CLIP)
1122 scr->clip_menu = menu;
1124 return menu;
1128 WDock*
1129 wDockCreate(WScreen *scr, int type)
1131 WDock *dock;
1132 WAppIcon *btn;
1133 int icon_count;
1135 make_keys();
1137 dock = wmalloc(sizeof(WDock));
1138 memset(dock, 0, sizeof(WDock));
1140 if (type == WM_CLIP)
1141 icon_count = CLIP_MAX_ICONS;
1142 else
1143 icon_count = scr->scr_height/wPreferences.icon_size;
1145 dock->icon_array = wmalloc(sizeof(WAppIcon*)*icon_count);
1146 memset(dock->icon_array, 0, sizeof(WAppIcon*)*icon_count);
1148 dock->max_icons = icon_count;
1150 btn = mainIconCreate(scr, type);
1152 btn->dock = dock;
1154 dock->x_pos = btn->x_pos;
1155 dock->y_pos = btn->y_pos;
1156 dock->screen_ptr = scr;
1157 dock->type = type;
1158 dock->icon_count = 1;
1159 dock->on_right_side = 1;
1160 dock->collapsed = 0;
1161 dock->auto_collapse = 0;
1162 dock->auto_collapse_magic = NULL;
1163 dock->auto_raise_lower = 0;
1164 dock->auto_lower_magic = NULL;
1165 dock->auto_raise_magic = NULL;
1166 dock->attract_icons = 0;
1167 dock->keep_attracted = 0;
1168 dock->lowered = 1;
1169 dock->icon_array[0] = btn;
1170 wRaiseFrame(btn->icon->core);
1171 XMoveWindow(dpy, btn->icon->core->window, btn->x_pos, btn->y_pos);
1173 /* create dock menu */
1174 dock->menu = dockMenuCreate(scr, type);
1176 return dock;
1180 void
1181 wDockDestroy(WDock *dock)
1183 int i;
1184 WAppIcon *aicon;
1186 for (i=(dock->type == WM_CLIP) ? 1 : 0; i<dock->max_icons; i++) {
1187 aicon = dock->icon_array[i];
1188 if (aicon) {
1189 int keepit = aicon->running && wApplicationOf(aicon->main_window);
1190 wDockDetach(dock, aicon);
1191 if (keepit) {
1192 PlaceIcon(dock->screen_ptr, &aicon->x_pos, &aicon->y_pos);
1193 XMoveWindow(dpy, aicon->icon->core->window,
1194 aicon->x_pos, aicon->y_pos);
1195 if (!dock->mapped || dock->collapsed)
1196 XMapWindow(dpy, aicon->icon->core->window);
1200 if (wPreferences.auto_arrange_icons)
1201 wArrangeIcons(dock->screen_ptr, True);
1202 free(dock->icon_array);
1203 if (dock->menu && dock->type!=WM_CLIP)
1204 wMenuDestroy(dock->menu, True);
1205 free(dock);
1209 void
1210 wClipIconPaint(WAppIcon *aicon)
1212 WScreen *scr = aicon->icon->core->screen_ptr;
1213 WWorkspace *workspace = scr->workspaces[scr->current_workspace];
1214 GC gc;
1215 Window win = aicon->icon->core->window;
1216 int length, nlength;
1217 char *ws_name, ws_number[10];
1218 int ty, tx;
1220 wIconPaint(aicon->icon);
1222 length = strlen(workspace->name);
1223 ws_name = malloc(length + 1);
1224 sprintf(ws_name, "%s", workspace->name);
1225 sprintf(ws_number, "%i", scr->current_workspace + 1);
1226 nlength = strlen(ws_number);
1228 gc = scr->clip_title_gc;
1230 if (!workspace->clip->collapsed)
1231 XSetForeground(dpy, gc, scr->clip_title_pixel[CLIP_NORMAL]);
1232 else
1233 XSetForeground(dpy, gc, scr->clip_title_pixel[CLIP_COLLAPSED]);
1235 ty = ICON_SIZE - scr->clip_title_font->height - 3;
1237 tx = CLIP_BUTTON_SIZE*ICON_SIZE/64;
1239 wDrawString(win, scr->clip_title_font, gc, tx,
1240 ty + scr->clip_title_font->y, ws_name, length);
1242 tx = (ICON_SIZE/2 - wTextWidth(scr->clip_title_font->font, ws_number, nlength))/2;
1244 wDrawString(win, scr->clip_title_font, gc, tx,
1245 scr->clip_title_font->y + 2, ws_number, nlength);
1247 free(ws_name);
1249 if (aicon->launching) {
1250 XFillRectangle(dpy, aicon->icon->core->window, scr->stipple_gc,
1251 0, 0, wPreferences.icon_size, wPreferences.icon_size);
1253 paintClipButtons(aicon, aicon->dock->lclip_button_pushed,
1254 aicon->dock->rclip_button_pushed);
1258 static void
1259 clipIconExpose(WObjDescriptor *desc, XEvent *event)
1261 wClipIconPaint(desc->parent);
1265 static void
1266 dockIconPaint(WAppIcon *btn)
1268 if (btn == btn->icon->core->screen_ptr->clip_icon)
1269 wClipIconPaint(btn);
1270 else
1271 wAppIconPaint(btn);
1275 static proplist_t
1276 make_icon_state(WAppIcon *btn)
1278 proplist_t node = NULL;
1279 proplist_t command, autolaunch, name, forced, host, position, buggy;
1280 char *tmp;
1281 char buffer[64];
1283 if (btn) {
1284 if (!btn->command)
1285 command = PLMakeString("-");
1286 else
1287 command = PLMakeString(btn->command);
1289 autolaunch = btn->auto_launch ? dYes : dNo;
1291 tmp = EscapeWM_CLASS(btn->wm_instance, btn->wm_class);
1293 name = PLMakeString(tmp);
1295 free(tmp);
1297 forced = btn->forced_dock ? dYes : dNo;
1299 buggy = btn->buggy_app ? dYes: dNo;
1301 if (btn == btn->icon->core->screen_ptr->clip_icon)
1302 sprintf(buffer, "%i,%i", btn->x_pos, btn->y_pos);
1303 else
1304 sprintf(buffer, "%hi,%hi", btn->xindex, btn->yindex);
1305 position = PLMakeString(buffer);
1307 node = PLMakeDictionaryFromEntries(dCommand, command,
1308 dName, name,
1309 dAutoLaunch, autolaunch,
1310 dForced, forced,
1311 dBuggyApplication, buggy,
1312 dPosition, position,
1313 NULL);
1314 PLRelease(command);
1315 PLRelease(name);
1316 PLRelease(position);
1317 #ifdef OFFIX_DND
1318 if (btn->dnd_command) {
1319 command = PLMakeString(btn->dnd_command);
1320 PLInsertDictionaryEntry(node, dDropCommand, command);
1321 PLRelease(command);
1323 #endif /* OFFIX_DND */
1325 if (btn->client_machine && btn->remote_start) {
1326 host = PLMakeString(btn->client_machine);
1327 PLInsertDictionaryEntry(node, dHost, host);
1328 PLRelease(host);
1332 return node;
1336 static proplist_t
1337 dockSaveState(WDock *dock)
1339 int i;
1340 proplist_t icon_info;
1341 proplist_t list=NULL, dock_state=NULL;
1342 proplist_t value;
1343 char buffer[256];
1345 list = PLMakeArrayFromElements(NULL);
1347 for (i=(dock->type==WM_DOCK ? 0 : 1); i<dock->max_icons; i++) {
1348 WAppIcon *btn = dock->icon_array[i];
1350 if (!btn || (btn->attracted && !dock->keep_attracted))
1351 continue;
1353 if ((icon_info = make_icon_state(dock->icon_array[i]))) {
1354 list = PLAppendArrayElement(list, icon_info);
1355 PLRelease(icon_info);
1359 dock_state = PLMakeDictionaryFromEntries(dApplications, list, NULL);
1361 PLRelease(list);
1363 if (dock->type == WM_DOCK) {
1364 sprintf(buffer, "%i,%i", (dock->on_right_side ? -ICON_SIZE : 0),
1365 dock->y_pos);
1366 value = PLMakeString(buffer);
1367 PLInsertDictionaryEntry(dock_state, dPosition, value);
1368 PLRelease(value);
1371 value = (dock->lowered ? dYes : dNo);
1372 PLInsertDictionaryEntry(dock_state, dLowered, value);
1374 if (dock->type == WM_CLIP) {
1375 value = (dock->collapsed ? dYes : dNo);
1376 PLInsertDictionaryEntry(dock_state, dCollapsed, value);
1378 value = (dock->auto_collapse ? dYes : dNo);
1379 PLInsertDictionaryEntry(dock_state, dAutoCollapse, value);
1381 value = (dock->auto_raise_lower ? dYes : dNo);
1382 PLInsertDictionaryEntry(dock_state, dAutoRaiseLower, value);
1384 value = (dock->attract_icons ? dYes : dNo);
1385 PLInsertDictionaryEntry(dock_state, dAutoAttractIcons, value);
1387 value = (dock->keep_attracted ? dYes : dNo);
1388 PLInsertDictionaryEntry(dock_state, dKeepAttracted, value);
1391 return dock_state;
1395 void
1396 wDockSaveState(WScreen *scr)
1398 proplist_t dock_state;
1400 dock_state = dockSaveState(scr->dock);
1402 PLInsertDictionaryEntry(scr->session_state, dDock, dock_state);
1404 PLRelease(dock_state);
1408 void
1409 wClipSaveState(WScreen *scr)
1411 proplist_t clip_state;
1413 clip_state = make_icon_state(scr->clip_icon);
1415 PLInsertDictionaryEntry(scr->session_state, dClip, clip_state);
1417 PLRelease(clip_state);
1421 proplist_t
1422 wClipSaveWorkspaceState(WScreen *scr, int workspace)
1424 return dockSaveState(scr->workspaces[workspace]->clip);
1428 static WAppIcon*
1429 restore_icon_state(WScreen *scr, proplist_t info, int type, int index)
1431 WAppIcon *aicon;
1432 char *wclass, *winstance;
1433 proplist_t cmd, value;
1434 char *command;
1437 cmd = PLGetDictionaryEntry(info, dCommand);
1438 if (!cmd || !PLIsString(cmd)) {
1439 return NULL;
1442 /* parse window name */
1443 value = PLGetDictionaryEntry(info, dName);
1444 if (!value)
1445 return NULL;
1447 ParseWindowName(value, &winstance, &wclass, "dock");
1449 if (!winstance && !wclass) {
1450 return NULL;
1453 /* get commands */
1455 if (cmd)
1456 command = wstrdup(PLGetString(cmd));
1457 else
1458 command = NULL;
1460 if (!command || strcmp(command, "-")==0) {
1461 if (command)
1462 free(command);
1463 if (wclass)
1464 free(wclass);
1465 if (winstance)
1466 free(winstance);
1468 return NULL;
1471 aicon = wAppIconCreateForDock(scr, command, winstance, wclass,
1472 TILE_NORMAL);
1473 if (wclass)
1474 free(wclass);
1475 if (winstance)
1476 free(winstance);
1478 aicon->icon->core->descriptor.handle_mousedown = iconMouseDown;
1479 if (type == WM_CLIP) {
1480 aicon->icon->core->descriptor.handle_enternotify = clipEnterNotify;
1481 aicon->icon->core->descriptor.handle_leavenotify = clipLeaveNotify;
1483 aicon->icon->core->descriptor.parent_type = WCLASS_DOCK_ICON;
1484 aicon->icon->core->descriptor.parent = aicon;
1487 #ifdef OFFIX_DND
1488 cmd = PLGetDictionaryEntry(info, dDropCommand);
1489 if (cmd)
1490 aicon->dnd_command = wstrdup(PLGetString(cmd));
1491 #endif
1493 /* check auto launch */
1494 value = PLGetDictionaryEntry(info, dAutoLaunch);
1496 aicon->auto_launch = 0;
1497 if (value) {
1498 if (PLIsString(value)) {
1499 if (strcasecmp(PLGetString(value), "YES")==0)
1500 aicon->auto_launch = 1;
1501 } else {
1502 wwarning(_("bad value in docked icon state info %s"),
1503 PLGetString(dAutoLaunch));
1507 /* check if it wasn't normally docked */
1508 value = PLGetDictionaryEntry(info, dForced);
1510 aicon->forced_dock = 0;
1511 if (value) {
1512 if (PLIsString(value)) {
1513 if (strcasecmp(PLGetString(value), "YES")==0)
1514 aicon->forced_dock = 1;
1515 } else {
1516 wwarning(_("bad value in docked icon state info %s"),
1517 PLGetString(dForced));
1521 /* check if we can rely on the stuff in the app */
1522 value = PLGetDictionaryEntry(info, dBuggyApplication);
1524 aicon->buggy_app = 0;
1525 if (value) {
1526 if (PLIsString(value)) {
1527 if (strcasecmp(PLGetString(value), "YES")==0)
1528 aicon->buggy_app = 1;
1529 } else {
1530 wwarning(_("bad value in docked icon state info %s"),
1531 PLGetString(dBuggyApplication));
1535 /* get position in the dock */
1536 value = PLGetDictionaryEntry(info, dPosition);
1537 if (value && PLIsString(value)) {
1538 if (sscanf(PLGetString(value), "%hi,%hi", &aicon->xindex,
1539 &aicon->yindex)!=2)
1540 wwarning(_("bad value in docked icon state info %s"),
1541 PLGetString(dPosition));
1543 /* check position sanity */
1544 /* incomplete section! */
1545 if (type == WM_DOCK) {
1546 aicon->xindex = 0;
1547 if (aicon->yindex < 0)
1548 wwarning(_("bad value in docked icon position %i,%i"),
1549 aicon->xindex, aicon->yindex);
1551 } else {
1552 aicon->yindex = index;
1553 aicon->xindex = 0;
1556 aicon->running = 0;
1557 aicon->docked = 1;
1559 return aicon;
1563 #define COMPLAIN(key) wwarning(_("bad value in dock state info:%s"), key)
1566 WAppIcon*
1567 wClipRestoreState(WScreen *scr, proplist_t clip_state)
1569 WAppIcon *icon;
1570 proplist_t value;
1573 icon = mainIconCreate(scr, WM_CLIP);
1575 if (!clip_state)
1576 return icon;
1577 else
1578 PLRetain(clip_state);
1580 /* restore position */
1582 value = PLGetDictionaryEntry(clip_state, dPosition);
1584 if (value) {
1585 if (!PLIsString(value))
1586 COMPLAIN("Position");
1587 else {
1588 if (sscanf(PLGetString(value), "%i,%i", &icon->x_pos,
1589 &icon->y_pos)!=2)
1590 COMPLAIN("Position");
1592 /* check position sanity */
1593 if (icon->y_pos < 0)
1594 icon->y_pos = 0;
1595 else if (icon->y_pos > scr->scr_height-ICON_SIZE)
1596 icon->y_pos = scr->scr_height-ICON_SIZE;
1598 if (icon->x_pos < 0)
1599 icon->x_pos = 0;
1600 else if (icon->x_pos > scr->scr_width-ICON_SIZE)
1601 icon->x_pos = scr->scr_width-ICON_SIZE;
1605 #ifdef OFFIX_DND
1606 value = PLGetDictionaryEntry(clip_state, dDropCommand);
1607 if (value && PLIsString(value))
1608 icon->dnd_command = wstrdup(PLGetString(value));
1609 #endif
1611 PLRelease(clip_state);
1613 return icon;
1617 WDock*
1618 wDockRestoreState(WScreen *scr, proplist_t dock_state, int type)
1620 WDock *dock;
1621 proplist_t apps;
1622 proplist_t value;
1623 WAppIcon *aicon, *old_top;
1624 int count, i;
1627 dock = wDockCreate(scr, type);
1629 if (!dock_state)
1630 return dock;
1632 if (dock_state)
1633 PLRetain(dock_state);
1636 /* restore position */
1638 value = PLGetDictionaryEntry(dock_state, dPosition);
1640 if (value) {
1641 if (!PLIsString(value))
1642 COMPLAIN("Position");
1643 else {
1644 if (sscanf(PLGetString(value), "%i,%i", &dock->x_pos,
1645 &dock->y_pos)!=2)
1646 COMPLAIN("Position");
1648 /* check position sanity */
1649 if (dock->y_pos < 0)
1650 dock->y_pos = 0;
1651 else if (dock->y_pos > scr->scr_height-ICON_SIZE)
1652 dock->y_pos = scr->scr_height - ICON_SIZE;
1654 /* This is no more needed. ??? */
1655 if (type == WM_CLIP) {
1656 if (dock->x_pos < 0)
1657 dock->x_pos = 0;
1658 else if (dock->x_pos > scr->scr_width-ICON_SIZE)
1659 dock->x_pos = scr->scr_width-ICON_SIZE;
1660 } else {
1661 if (dock->x_pos >= 0) {
1662 dock->x_pos = DOCK_EXTRA_SPACE;
1663 dock->on_right_side = 0;
1664 } else {
1665 dock->x_pos = scr->scr_width - DOCK_EXTRA_SPACE - ICON_SIZE;
1666 dock->on_right_side = 1;
1672 /* restore lowered/raised state */
1674 dock->lowered = 0;
1676 value = PLGetDictionaryEntry(dock_state, dLowered);
1678 if (value) {
1679 if (!PLIsString(value))
1680 COMPLAIN("Lowered");
1681 else {
1682 if (strcasecmp(PLGetString(value), "YES")==0)
1683 dock->lowered = 1;
1688 /* restore collapsed state */
1690 dock->collapsed = 0;
1692 value = PLGetDictionaryEntry(dock_state, dCollapsed);
1694 if (value) {
1695 if (!PLIsString(value))
1696 COMPLAIN("Collapsed");
1697 else {
1698 if (strcasecmp(PLGetString(value), "YES")==0)
1699 dock->collapsed = 1;
1704 /* restore auto-collapsed state */
1706 value = PLGetDictionaryEntry(dock_state, dAutoCollapse);
1708 if (value) {
1709 if (!PLIsString(value))
1710 COMPLAIN("AutoCollapse");
1711 else {
1712 if (strcasecmp(PLGetString(value), "YES")==0) {
1713 dock->auto_collapse = 1;
1714 dock->collapsed = 1;
1720 /* restore auto-raise/lower state */
1722 value = PLGetDictionaryEntry(dock_state, dAutoRaiseLower);
1724 if (value) {
1725 if (!PLIsString(value))
1726 COMPLAIN("AutoRaiseLower");
1727 else {
1728 if (strcasecmp(PLGetString(value), "YES")==0) {
1729 dock->auto_raise_lower = 1;
1735 /* restore attract icons state */
1737 dock->attract_icons = 0;
1739 value = PLGetDictionaryEntry(dock_state, dAutoAttractIcons);
1741 if (value) {
1742 if (!PLIsString(value))
1743 COMPLAIN("AutoAttractIcons");
1744 else {
1745 if (strcasecmp(PLGetString(value), "YES")==0)
1746 dock->attract_icons = 1;
1751 /* restore keep attracted icons state */
1753 dock->keep_attracted = 0;
1755 value = PLGetDictionaryEntry(dock_state, dKeepAttracted);
1757 if (value) {
1758 if (!PLIsString(value))
1759 COMPLAIN("KeepAttracted");
1760 else {
1761 if (strcasecmp(PLGetString(value), "YES")==0)
1762 dock->keep_attracted = 1;
1767 /* application list */
1769 apps = PLGetDictionaryEntry(dock_state, dApplications);
1771 if (!apps) {
1772 goto finish;
1775 count = PLGetNumberOfElements(apps);
1777 if (count==0)
1778 goto finish;
1780 old_top = dock->icon_array[0];
1782 /* dock->icon_count is set to 1 when dock is created.
1783 * Since Clip is already restored, we want to keep it so for clip,
1784 * but for dock we may change the default top tile, so we set it to 0.
1786 if (type == WM_DOCK)
1787 dock->icon_count = 0;
1789 for (i=0; i<count; i++) {
1790 if (dock->icon_count >= dock->max_icons) {
1791 wwarning(_("there are too many icons stored in dock. Ignoring what doesn't fit"));
1792 break;
1795 value = PLGetArrayElement(apps, i);
1796 aicon = restore_icon_state(scr, value, type, dock->icon_count);
1798 dock->icon_array[dock->icon_count] = aicon;
1800 if (aicon) {
1801 aicon->dock = dock;
1802 aicon->x_pos = dock->x_pos + (aicon->xindex*ICON_SIZE);
1803 aicon->y_pos = dock->y_pos + (aicon->yindex*ICON_SIZE);
1805 if (dock->lowered)
1806 ChangeStackingLevel(aicon->icon->core, WMNormalLevel);
1807 else
1808 ChangeStackingLevel(aicon->icon->core, WMDockLevel);
1810 wCoreConfigure(aicon->icon->core, aicon->x_pos, aicon->y_pos,
1811 0, 0);
1813 if (!dock->collapsed)
1814 XMapWindow(dpy, aicon->icon->core->window);
1815 wRaiseFrame(aicon->icon->core);
1817 dock->icon_count++;
1818 } else if (dock->icon_count==0 && type==WM_DOCK)
1819 dock->icon_count++;
1822 /* if the first icon is not defined, use the default */
1823 if (dock->icon_array[0]==NULL) {
1824 /* update default icon */
1825 old_top->x_pos = dock->x_pos;
1826 old_top->y_pos = dock->y_pos;
1827 if (dock->lowered)
1828 ChangeStackingLevel(old_top->icon->core, WMNormalLevel);
1829 else
1830 ChangeStackingLevel(old_top->icon->core, WMDockLevel);
1831 dock->icon_array[0] = old_top;
1832 XMoveWindow(dpy, old_top->icon->core->window, dock->x_pos, dock->y_pos);
1833 /* we don't need to increment dock->icon_count here because it was
1834 * incremented in the loop above.
1836 } else if (old_top!=dock->icon_array[0]) {
1837 if (old_top == scr->clip_icon)
1838 scr->clip_icon = dock->icon_array[0];
1839 wAppIconDestroy(old_top);
1842 finish:
1843 if (dock_state)
1844 PLRelease(dock_state);
1846 return dock;
1851 void
1852 wDockLaunchWithState(WDock *dock, WAppIcon *btn, WSavedState *state)
1854 if (btn && btn->command && !btn->running && !btn->launching) {
1856 btn->drop_launch = 0;
1858 btn->pid = execCommand(btn, btn->command, state);
1860 if (btn->pid>0) {
1861 if (!btn->forced_dock && !btn->buggy_app) {
1862 btn->launching = 1;
1863 dockIconPaint(btn);
1866 } else {
1867 free(state);
1872 void
1873 wDockDoAutoLaunch(WDock *dock, int workspace)
1875 WAppIcon *btn;
1876 WSavedState *state;
1877 int i;
1879 for (i = 0; i < dock->max_icons; i++) {
1880 btn = dock->icon_array[i];
1881 if (!btn || !btn->auto_launch)
1882 continue;
1884 state = wmalloc(sizeof(WSavedState));
1885 memset(state, 0, sizeof(WSavedState));
1886 state->workspace = workspace;
1887 /* TODO: this is klugy and is very difficult to understand
1888 * what's going on. Try to clean up */
1889 wDockLaunchWithState(dock, btn, state);
1893 #ifdef REDUCE_APPICONS
1894 void
1895 wDockSimulateLaunch(WDock *dock, WAppIcon *btn)
1897 if ((btn == NULL) || (dock == NULL))
1898 return;
1900 if (!btn->running) {
1901 if ((btn->icon->owner == NULL) && (btn->applist))
1902 btn->icon->owner = btn->applist->wapp->main_window_desc;
1903 if (!btn->forced_dock)
1904 btn->launching = 1;
1905 dockIconPaint(btn);
1906 wusleep(5000);
1909 #endif
1911 #ifdef OFFIX_DND
1912 static WDock*
1913 findDock(WScreen *scr, XEvent *event, int *icon_pos)
1915 WDock *dock;
1916 int i;
1918 *icon_pos = -1;
1919 if ((dock = scr->dock)!=NULL) {
1920 for (i=0; i<dock->max_icons; i++) {
1921 if (dock->icon_array[i]
1922 && dock->icon_array[i]->icon->core->window==event->xclient.window) {
1923 *icon_pos = i;
1924 break;
1928 if (*icon_pos<0 && (dock = scr->workspaces[scr->current_workspace]->clip)!=NULL) {
1929 for (i=0; i<dock->max_icons; i++) {
1930 if (dock->icon_array[i]
1931 && dock->icon_array[i]->icon->core->window==event->xclient.window) {
1932 *icon_pos = i;
1933 break;
1937 if(*icon_pos>=0)
1938 return dock;
1939 return NULL;
1944 wDockReceiveDNDDrop(WScreen *scr, XEvent *event)
1946 WDock *dock;
1947 WAppIcon *btn;
1948 int icon_pos;
1950 dock = findDock(scr, event, &icon_pos);
1951 if (!dock)
1952 return False;
1955 * Return True if the drop was on an application icon window.
1956 * In this case, let the ClientMessage handler redirect the
1957 * message to the app.
1959 if (dock->icon_array[icon_pos]->icon->icon_win!=None)
1960 return True;
1962 if (dock->icon_array[icon_pos]->dnd_command!=NULL) {
1963 scr->flags.dnd_data_convertion_status = 0;
1965 btn = dock->icon_array[icon_pos];
1967 if (!btn->forced_dock) {
1968 btn->relaunching = btn->running;
1969 btn->running = 1;
1971 if (btn->wm_instance || btn->wm_class) {
1972 WWindowAttributes attr;
1973 memset(&attr, 0, sizeof(WWindowAttributes));
1974 wDefaultFillAttributes(btn->icon->core->screen_ptr,
1975 btn->wm_instance,
1976 btn->wm_class, &attr, NULL, True);
1978 if (!attr.no_appicon)
1979 btn->launching = 1;
1980 else
1981 btn->running = 0;
1984 btn->drop_launch = 1;
1985 scr->last_dock = dock;
1986 btn->pid = execCommand(btn, btn->dnd_command, NULL);
1987 if (btn->pid>0) {
1988 dockIconPaint(btn);
1989 } else {
1990 btn->launching = 0;
1991 if (!btn->relaunching) {
1992 btn->running = 0;
1996 return False;
1998 #endif /* OFFIX_DND */
2002 Bool
2003 wDockAttachIcon(WDock *dock, WAppIcon *icon, int x, int y)
2005 WWindow *wwin;
2006 char **argv;
2007 int argc;
2008 int index;
2010 wwin = icon->icon->owner;
2011 if (icon->command==NULL) {
2012 icon->editing = 0;
2013 if (XGetCommand(dpy, wwin->client_win, &argv, &argc) && argc>0) {
2015 icon->command = FlattenStringList(argv, argc);
2016 XFreeStringList(argv);
2017 } else {
2018 char *command=NULL;
2020 /* icon->forced_dock = 1;*/
2021 if (!icon->attracted || dock->type!=WM_CLIP || dock->keep_attracted) {
2022 icon->editing = 1;
2023 if (wInputDialog(dock->screen_ptr, _("Dock Icon"),
2024 _("Type the command used to launch the application"),
2025 &command)) {
2026 if (command && (command[0]==0 ||
2027 (command[0]=='-' && command[1]==0))) {
2028 free(command);
2029 command = NULL;
2031 icon->command = command;
2032 icon->editing = 0;
2033 } else {
2034 icon->editing = 0;
2035 if (command)
2036 free(command);
2037 /* If the target is the dock, reject the icon. If
2038 * the target is the clip, make it an attracted icon
2040 if (dock->type==WM_CLIP) {
2041 icon->attracted = 1;
2042 if (!icon->icon->shadowed) {
2043 icon->icon->shadowed = 1;
2044 icon->icon->force_paint = 1;
2046 } else {
2047 return False;
2052 } else {
2053 icon->editing = 0;
2056 for (index=1; index<dock->max_icons; index++)
2057 if (dock->icon_array[index] == NULL)
2058 break;
2059 /* if (index == dock->max_icons)
2060 return; */
2062 assert(index < dock->max_icons);
2064 dock->icon_array[index] = icon;
2065 icon->yindex = y;
2066 icon->xindex = x;
2068 icon->x_pos = dock->x_pos + x*ICON_SIZE;
2069 icon->y_pos = dock->y_pos + y*ICON_SIZE;
2071 dock->icon_count++;
2073 icon->running = 1;
2074 icon->launching = 0;
2075 icon->docked = 1;
2076 icon->dock = dock;
2077 icon->icon->core->descriptor.handle_mousedown = iconMouseDown;
2078 if (dock->type == WM_CLIP) {
2079 icon->icon->core->descriptor.handle_enternotify = clipEnterNotify;
2080 icon->icon->core->descriptor.handle_leavenotify = clipLeaveNotify;
2082 icon->icon->core->descriptor.parent_type = WCLASS_DOCK_ICON;
2083 icon->icon->core->descriptor.parent = icon;
2085 MoveInStackListUnder(dock->icon_array[index-1]->icon->core,
2086 icon->icon->core);
2087 wAppIconMove(icon, icon->x_pos, icon->y_pos);
2088 wAppIconPaint(icon);
2090 if (wPreferences.auto_arrange_icons)
2091 wArrangeIcons(dock->screen_ptr, True);
2093 #ifdef OFFIX_DND
2094 if (icon->command && !icon->dnd_command) {
2095 icon->dnd_command = wmalloc(strlen(icon->command)+8);
2096 sprintf(icon->dnd_command, "%s %%d", icon->command);
2098 #endif
2100 return True;
2104 void
2105 reattachIcon(WDock *dock, WAppIcon *icon, int x, int y)
2107 int index;
2109 for(index=1; index<dock->max_icons; index++) {
2110 if(dock->icon_array[index] == icon)
2111 break;
2113 assert(index < dock->max_icons);
2115 icon->yindex = y;
2116 icon->xindex = x;
2118 icon->x_pos = dock->x_pos + x*ICON_SIZE;
2119 icon->y_pos = dock->y_pos + y*ICON_SIZE;
2123 Bool
2124 moveIconBetweenDocks(WDock *src, WDock *dest, WAppIcon *icon, int x, int y)
2126 WWindow *wwin;
2127 char **argv;
2128 int argc;
2129 int index;
2131 if (dest == NULL)
2132 return False;
2134 wwin = icon->icon->owner;
2137 * For the moment we can't do this if we move icons in Clip from one
2138 * workspace to other, because if we move two or more icons without
2139 * command, the dialog box will not be able to tell us to which of the
2140 * moved icons it applies. -Dan
2142 if ((dest->type==WM_DOCK /*|| dest->keep_attracted*/) && icon->command==NULL) {
2143 if (XGetCommand(dpy, wwin->client_win, &argv, &argc) && argc>0) {
2145 icon->command = FlattenStringList(argv, argc);
2146 XFreeStringList(argv);
2147 } else {
2148 char *command=NULL;
2150 icon->editing = 1;
2151 /* icon->forced_dock = 1;*/
2152 if (wInputDialog(src->screen_ptr, _("Dock Icon"),
2153 _("Type the command used to launch the application"),
2154 &command)) {
2155 if (command && (command[0]==0 ||
2156 (command[0]=='-' && command[1]==0))) {
2157 free(command);
2158 command = NULL;
2160 icon->command = command;
2161 } else {
2162 icon->editing = 0;
2163 if (command)
2164 free(command);
2165 return False;
2167 icon->editing = 0;
2171 for(index=1; index<src->max_icons; index++) {
2172 if(src->icon_array[index] == icon)
2173 break;
2175 assert(index < src->max_icons);
2177 src->icon_array[index] = NULL;
2178 src->icon_count--;
2180 for(index=1; index<dest->max_icons; index++) {
2181 if(dest->icon_array[index] == NULL)
2182 break;
2184 /* if (index == dest->max_icons)
2185 return; */
2187 assert(index < dest->max_icons);
2189 dest->icon_array[index] = icon;
2190 icon->dock = dest;
2192 /* deselect the icon */
2193 if (icon->icon->selected)
2194 wIconSelect(icon->icon);
2196 if (dest->type == WM_DOCK) {
2197 icon->icon->core->descriptor.handle_enternotify = NULL;
2198 icon->icon->core->descriptor.handle_leavenotify = NULL;
2199 } else {
2200 icon->icon->core->descriptor.handle_enternotify = clipEnterNotify;
2201 icon->icon->core->descriptor.handle_leavenotify = clipLeaveNotify;
2204 /* set it to be kept when moving to dock, or to a clip that keep the
2205 * attracted icons.
2206 * Unless the icon does not have a command set
2208 if (icon->command && (dest->type==WM_DOCK || dest->keep_attracted)) {
2209 icon->attracted = 0;
2210 if (icon->icon->shadowed) {
2211 icon->icon->shadowed = 0;
2212 icon->icon->force_paint = 1;
2216 if (src->auto_collapse || src->auto_raise_lower)
2217 clipLeave(src);
2219 icon->yindex = y;
2220 icon->xindex = x;
2222 icon->x_pos = dest->x_pos + x*ICON_SIZE;
2223 icon->y_pos = dest->y_pos + y*ICON_SIZE;
2225 dest->icon_count++;
2227 MoveInStackListUnder(dest->icon_array[index-1]->icon->core,
2228 icon->icon->core);
2229 wAppIconPaint(icon);
2231 return True;
2235 void
2236 wDockDetach(WDock *dock, WAppIcon *icon)
2238 int index;
2240 /* make the settings panel be closed */
2241 if (icon->panel) {
2242 DestroyDockAppSettingsPanel(icon->panel);
2245 icon->docked = 0;
2246 icon->dock = NULL;
2247 icon->attracted = 0;
2248 if (icon->icon->shadowed) {
2249 icon->icon->shadowed = 0;
2250 icon->icon->force_paint = 1;
2253 /* deselect the icon */
2254 if (icon->icon->selected)
2255 wIconSelect(icon->icon);
2257 if (icon->command) {
2258 free(icon->command);
2259 icon->command = NULL;
2261 #ifdef OFFIX_DND
2262 if (icon->dnd_command) {
2263 free(icon->dnd_command);
2264 icon->dnd_command = NULL;
2266 #endif
2268 for (index=1; index<dock->max_icons; index++)
2269 if (dock->icon_array[index] == icon)
2270 break;
2271 assert(index < dock->max_icons);
2272 dock->icon_array[index] = NULL;
2273 icon->yindex = -1;
2274 icon->xindex = -1;
2275 dock->icon_count--;
2277 /* if the dock is not attached to an application or
2278 * the the application did not set the approriate hints yet,
2279 * destroy the icon */
2280 #ifdef REDUCE_APPICONS
2281 if ((icon->num_apps == 0) && (!icon->running || !wApplicationOf(icon->main_window)) )
2282 #else
2283 if (!icon->running || !wApplicationOf(icon->main_window))
2284 #endif
2285 wAppIconDestroy(icon);
2286 else {
2287 icon->icon->core->descriptor.handle_mousedown = appIconMouseDown;
2288 icon->icon->core->descriptor.handle_enternotify = NULL;
2289 icon->icon->core->descriptor.handle_leavenotify = NULL;
2290 icon->icon->core->descriptor.parent_type = WCLASS_APPICON;
2291 icon->icon->core->descriptor.parent = icon;
2293 ChangeStackingLevel(icon->icon->core, NORMAL_ICON_LEVEL);
2295 wAppIconPaint(icon);
2296 if (wPreferences.auto_arrange_icons) {
2297 wArrangeIcons(dock->screen_ptr, True);
2300 if (dock->auto_collapse || dock->auto_raise_lower)
2301 clipLeave(dock);
2306 * returns the closest Dock slot index for the passed
2307 * coordinates.
2309 * Returns False if icon can't be docked.
2311 * Note: this function should NEVER alter ret_x or ret_y, unless it will
2312 * return True. -Dan
2314 Bool
2315 wDockSnapIcon(WDock *dock, WAppIcon *icon, int req_x, int req_y,
2316 int *ret_x, int *ret_y, int redocking)
2318 WScreen *scr = dock->screen_ptr;
2319 int dx, dy;
2320 int ex_x, ex_y;
2321 int i, offset = ICON_SIZE/2;
2322 WAppIcon *aicon = NULL;
2323 WAppIcon *nicon = NULL;
2324 int max_y_icons, max_x_icons;
2326 max_x_icons = scr->scr_width/ICON_SIZE;
2327 max_y_icons = scr->scr_height/ICON_SIZE-1;
2329 if (wPreferences.flags.noupdates)
2330 return False;
2332 dx = dock->x_pos;
2333 dy = dock->y_pos;
2335 /* if the dock is full */
2336 if (!redocking &&
2337 (dock->icon_count >= dock->max_icons)) {
2338 return False;
2341 /* exact position */
2342 if (req_y < dy)
2343 ex_y = (req_y - offset - dy)/ICON_SIZE;
2344 else
2345 ex_y = (req_y + offset - dy)/ICON_SIZE;
2347 if (req_x < dx)
2348 ex_x = (req_x - offset - dx)/ICON_SIZE;
2349 else
2350 ex_x = (req_x + offset - dx)/ICON_SIZE;
2352 /* check if the icon is outside the screen boundaries */
2353 if (dx + ex_x*ICON_SIZE < -ICON_SIZE+2 ||
2354 dx + ex_x*ICON_SIZE >= scr->scr_width-1 ||
2355 dy + ex_y*ICON_SIZE < -ICON_SIZE+2 ||
2356 dy + ex_y*ICON_SIZE >= scr->scr_height-1)
2357 return False;
2359 if (dock->type == WM_DOCK) {
2360 if (icon->dock != dock && ex_x != 0)
2361 return False;
2363 for (i=0; i<dock->max_icons; i++) {
2364 nicon = dock->icon_array[i];
2365 if (nicon && nicon->yindex == ex_y) {
2366 aicon = nicon;
2367 break;
2371 if (redocking) {
2372 int sig, done, closest;
2374 /* Possible cases when redocking:
2376 * icon dragged out of range of any slot -> false
2377 * icon dragged to range of free slot
2378 * icon dragged to range of same slot
2379 * icon dragged to range of different icon
2381 if (abs(ex_x) > DOCK_DETTACH_THRESHOLD)
2382 return False;
2384 if (ex_y>=0 && ex_y<=max_y_icons && (aicon==icon || !aicon)) {
2385 *ret_x = 0;
2386 *ret_y = ex_y;
2387 return True;
2390 /* start looking at the upper slot or lower? */
2391 if (ex_y*ICON_SIZE < (req_y + offset - dy))
2392 sig = 1;
2393 else
2394 sig = -1;
2396 closest = -1;
2397 done = 0;
2398 /* look for closest free slot */
2399 for (i=0; i<(DOCK_DETTACH_THRESHOLD+1)*2 && !done; i++) {
2400 int j;
2402 done = 1;
2403 closest = sig*(i/2) + ex_y;
2404 /* check if this slot is used */
2405 if (closest >= 0) {
2406 for (j = 0; j<dock->max_icons; j++) {
2407 if (dock->icon_array[j]
2408 && dock->icon_array[j]->yindex==closest) {
2409 /* slot is used by someone else */
2410 if (dock->icon_array[j]!=icon)
2411 done = 0;
2412 break;
2416 sig = -sig;
2418 if (done && closest >= 0 && closest <= max_y_icons &&
2419 ((ex_y >= closest && ex_y - closest < DOCK_DETTACH_THRESHOLD+1)
2421 (ex_y < closest && closest - ex_y <= DOCK_DETTACH_THRESHOLD+1))) {
2422 *ret_x = 0;
2423 *ret_y = closest;
2424 return True;
2426 } else { /* !redocking */
2428 /* if slot is free and the icon is close enough, return it */
2429 if (!aicon && ex_x == 0 && ex_y >= 0 && ex_y <= max_y_icons) {
2430 *ret_x = 0;
2431 *ret_y = ex_y;
2432 return True;
2435 } else { /* CLIP */
2436 int neighbours = 0;
2438 for (i=0; i<dock->max_icons; i++) {
2439 nicon = dock->icon_array[i];
2440 if (nicon && nicon->xindex == ex_x && nicon->yindex == ex_y) {
2441 aicon = nicon;
2442 break;
2446 for (i=0; i<dock->max_icons; i++) {
2447 nicon = dock->icon_array[i];
2448 if (nicon && nicon != icon && /* Icon can't be it's own neighbour */
2449 (abs(nicon->xindex - ex_x) <= CLIP_ATTACH_VICINITY &&
2450 abs(nicon->yindex - ex_y) <= CLIP_ATTACH_VICINITY)) {
2451 neighbours = 1;
2452 break;
2456 if (neighbours && (aicon==NULL || (redocking && aicon == icon))) {
2457 *ret_x = ex_x;
2458 *ret_y = ex_y;
2459 return True;
2462 return False;
2465 #define MIN(x, y) ((x) > (y) ? (y) : (x))
2466 #define MAX(x, y) ((x) < (y) ? (y) : (x))
2468 #define ON_SCREEN(x, y, sx, ex, sy, ey) \
2469 ((((x)+ICON_SIZE/2) >= (sx)) && (((y)+ICON_SIZE/2) >= (sy)) && \
2470 (((x) + (ICON_SIZE/2)) <= (ex)) && (((y) + (ICON_SIZE/2)) <= ey))
2474 * returns true if it can find a free slot in the dock,
2475 * in which case it changes x_pos and y_pos accordingly.
2476 * Else returns false.
2478 Bool
2479 wDockFindFreeSlot(WDock *dock, int *x_pos, int *y_pos)
2481 WScreen *scr = dock->screen_ptr;
2482 WAppIcon *btn;
2483 unsigned char *slot_map;
2484 int mwidth;
2485 int r;
2486 int x, y;
2487 int i, done = False;
2488 int corner;
2489 int sx=0, sy=0, ex=scr->scr_width, ey=scr->scr_height;
2492 /* if the dock is full */
2493 if (dock->icon_count >= dock->max_icons) {
2494 return False;
2497 if (!wPreferences.flags.nodock && scr->dock) {
2498 if (scr->dock->on_right_side)
2499 ex -= ICON_SIZE + DOCK_EXTRA_SPACE;
2500 else
2501 sx += ICON_SIZE + DOCK_EXTRA_SPACE;
2504 if (ex < dock->x_pos)
2505 ex = dock->x_pos;
2506 if (sx > dock->x_pos+ICON_SIZE)
2507 sx = dock->x_pos+ICON_SIZE;
2508 #define C_NONE 0
2509 #define C_NW 1
2510 #define C_NE 2
2511 #define C_SW 3
2512 #define C_SE 4
2514 /* check if clip is in a corner */
2515 if (dock->type==WM_CLIP) {
2516 if (dock->x_pos < 1 && dock->y_pos < 1)
2517 corner = C_NE;
2518 else if (dock->x_pos < 1 && dock->y_pos >= (ey-ICON_SIZE))
2519 corner = C_SE;
2520 else if (dock->x_pos >= (ex-ICON_SIZE)&& dock->y_pos >= (ey-ICON_SIZE))
2521 corner = C_SW;
2522 else if (dock->x_pos >= (ex-ICON_SIZE) && dock->y_pos < 1)
2523 corner = C_NW;
2524 else
2525 corner = C_NONE;
2526 } else
2527 corner = C_NONE;
2529 /* If the clip is in the corner, use only slots that are in the border
2530 * of the screen */
2531 if (corner!=C_NONE) {
2532 char *hmap, *vmap;
2533 int hcount, vcount;
2535 hcount = MIN(dock->max_icons, scr->scr_width/ICON_SIZE);
2536 vcount = MIN(dock->max_icons, scr->scr_height/ICON_SIZE);
2537 hmap = wmalloc(hcount+1);
2538 memset(hmap, 0, hcount+1);
2539 vmap = wmalloc(vcount+1);
2540 memset(vmap, 0, vcount+1);
2542 /* mark used positions */
2543 switch (corner) {
2544 case C_NE:
2545 for (i=0; i<dock->max_icons; i++) {
2546 btn = dock->icon_array[i];
2547 if (!btn)
2548 continue;
2550 if (btn->xindex==0 && btn->yindex > 0 && btn->yindex < vcount)
2551 vmap[btn->yindex] = 1;
2552 else if (btn->yindex==0 && btn->xindex>0 && btn->xindex<hcount)
2553 hmap[btn->xindex] = 1;
2555 case C_NW:
2556 for (i=0; i<dock->max_icons; i++) {
2557 btn = dock->icon_array[i];
2558 if (!btn)
2559 continue;
2561 if (btn->xindex==0 && btn->yindex > 0 && btn->yindex < vcount)
2562 vmap[btn->yindex] = 1;
2563 else if (btn->yindex==0 && btn->xindex<0 &&btn->xindex>-hcount)
2564 hmap[-btn->xindex] = 1;
2566 case C_SE:
2567 for (i=0; i<dock->max_icons; i++) {
2568 btn = dock->icon_array[i];
2569 if (!btn)
2570 continue;
2572 if (btn->xindex==0 && btn->yindex < 0 && btn->yindex > -vcount)
2573 vmap[-btn->yindex] = 1;
2574 else if (btn->yindex==0 && btn->xindex>0 && btn->xindex<hcount)
2575 hmap[btn->xindex] = 1;
2577 case C_SW:
2578 default:
2579 for (i=0; i<dock->max_icons; i++) {
2580 btn = dock->icon_array[i];
2581 if (!btn)
2582 continue;
2584 if (btn->xindex==0 && btn->yindex < 0 && btn->yindex > -vcount)
2585 vmap[-btn->yindex] = 1;
2586 else if (btn->yindex==0 && btn->xindex<0 &&btn->xindex>-hcount)
2587 hmap[-btn->xindex] = 1;
2590 x=0; y=0;
2591 done = 0;
2592 /* search a vacant slot */
2593 for (i=1; i<MAX(vcount, hcount); i++) {
2594 if (i < vcount && vmap[i]==0) {
2595 /* found a slot */
2596 x = 0;
2597 y = i;
2598 done = 1;
2599 break;
2600 } else if (i < hcount && hmap[i]==0) {
2601 /* found a slot */
2602 x = i;
2603 y = 0;
2604 done = 1;
2605 break;
2608 free(vmap);
2609 free(hmap);
2610 /* If found a slot, translate and return */
2611 if (done) {
2612 if (corner==C_NW || corner==C_NE) {
2613 *y_pos = y;
2614 } else {
2615 *y_pos = -y;
2617 if (corner==C_NE || corner==C_SE) {
2618 *x_pos = x;
2619 } else {
2620 *x_pos = -x;
2622 return True;
2624 /* else, try to find a slot somewhere else */
2627 /* a map of mwidth x mwidth would be enough if we allowed icons to be
2628 * placed outside of screen */
2629 mwidth = (int)ceil(sqrt(dock->max_icons));
2631 /* In the worst case (the clip is in the corner of the screen),
2632 * the amount of icons that fit in the clip is smaller.
2633 * Double the map to get a safe value.
2635 mwidth += mwidth;
2637 r = (mwidth-1)/2;
2639 slot_map = wmalloc(mwidth*mwidth);
2640 memset(slot_map, 0, mwidth*mwidth);
2642 #define XY2OFS(x,y) (MAX(abs(x),abs(y)) > r) ? 0 : (((y)+r)*(mwidth)+(x)+r)
2644 /* mark used slots in the map. If the slot falls outside the map
2645 * (for example, when all icons are placed in line), ignore them. */
2646 for (i=0; i<dock->max_icons; i++) {
2647 btn = dock->icon_array[i];
2648 if (btn)
2649 slot_map[XY2OFS(btn->xindex, btn->yindex)] = 1;
2651 /* Find closest slot from the center that is free by scanning the
2652 * map from the center to outward in circular passes.
2653 * This will not result in a neat layout, but will be optimal
2654 * in the sense that there will not be holes left.
2656 done = 0;
2657 for (i = 1; i <= r && !done; i++) {
2658 int tx, ty;
2660 /* top and bottom parts of the ring */
2661 for (x = -i; x <= i && !done; x++) {
2662 tx = dock->x_pos + x*ICON_SIZE;
2663 y = -i;
2664 ty = dock->y_pos + y*ICON_SIZE;
2665 if (slot_map[XY2OFS(x,y)]==0
2666 && ON_SCREEN(tx, ty, sx, ex, sy, ey)) {
2667 *x_pos = x;
2668 *y_pos = y;
2669 done = 1;
2670 break;
2672 y = i;
2673 ty = dock->y_pos + y*ICON_SIZE;
2674 if (slot_map[XY2OFS(x,y)]==0
2675 && ON_SCREEN(tx, ty, sx, ex, sy, ey)) {
2676 *x_pos = x;
2677 *y_pos = y;
2678 done = 1;
2679 break;
2682 /* left and right parts of the ring */
2683 for (y = -i+1; y <= i-1; y++) {
2684 ty = dock->y_pos + y*ICON_SIZE;
2685 x = -i;
2686 tx = dock->x_pos + x*ICON_SIZE;
2687 if (slot_map[XY2OFS(x,y)]==0
2688 && ON_SCREEN(tx, ty, sx, ex, sy, ey)) {
2689 *x_pos = x;
2690 *y_pos = y;
2691 done = 1;
2692 break;
2694 x = i;
2695 tx = dock->x_pos + x*ICON_SIZE;
2696 if (slot_map[XY2OFS(x,y)]==0
2697 && ON_SCREEN(tx, ty, sx, ex, sy, ey)) {
2698 *x_pos = x;
2699 *y_pos = y;
2700 done = 1;
2701 break;
2705 free(slot_map);
2706 #undef XY2OFS
2707 return done;
2711 static void
2712 moveDock(WDock *dock, int new_x, int new_y)
2714 WAppIcon *btn;
2715 int i;
2717 dock->x_pos = new_x;
2718 dock->y_pos = new_y;
2719 for (i=0; i<dock->max_icons; i++) {
2720 btn = dock->icon_array[i];
2721 if (btn) {
2722 btn->x_pos = new_x + btn->xindex*ICON_SIZE;
2723 btn->y_pos = new_y + btn->yindex*ICON_SIZE;
2724 XMoveWindow(dpy, btn->icon->core->window, btn->x_pos, btn->y_pos);
2730 static void
2731 swapDock(WDock *dock)
2733 WScreen *scr = dock->screen_ptr;
2734 WAppIcon *btn;
2735 int x, i;
2738 if (dock->on_right_side) {
2739 x = dock->x_pos = scr->scr_width - ICON_SIZE - DOCK_EXTRA_SPACE;
2740 } else {
2741 x = dock->x_pos = DOCK_EXTRA_SPACE;
2744 for (i=0; i<dock->max_icons; i++) {
2745 btn = dock->icon_array[i];
2746 if (btn) {
2747 btn->x_pos = x;
2748 XMoveWindow(dpy, btn->icon->core->window, btn->x_pos, btn->y_pos);
2752 wScreenUpdateUsableArea(scr);
2756 static pid_t
2757 execCommand(WAppIcon *btn, char *command, WSavedState *state)
2759 WScreen *scr = btn->icon->core->screen_ptr;
2760 pid_t pid;
2761 char **argv;
2762 int argc;
2763 char *cmdline;
2765 cmdline = ExpandOptions(scr, command);
2767 if (scr->flags.dnd_data_convertion_status || !cmdline) {
2768 if (cmdline)
2769 free(cmdline);
2770 if (state)
2771 free(state);
2772 return 0;
2775 ParseCommand(cmdline, &argv, &argc);
2777 if (argv==NULL) {
2778 if (cmdline)
2779 free(cmdline);
2780 if (state)
2781 free(state);
2782 return 0;
2785 if ((pid=fork())==0) {
2786 char **args;
2787 int i;
2789 SetupEnvironment(scr);
2791 #ifdef HAVE_SETPGID
2792 setpgid(0, 0);
2793 #endif
2795 args = malloc(sizeof(char*)*(argc+1));
2796 if (!args)
2797 exit(111);
2798 for (i=0; i<argc; i++) {
2799 args[i] = argv[i];
2801 args[argc] = NULL;
2802 execvp(argv[0], args);
2803 exit(111);
2805 while (argc > 0)
2806 free(argv[--argc]);
2807 free(argv);
2809 if (pid > 0) {
2810 if (!state) {
2811 state = wmalloc(sizeof(WSavedState));
2812 memset(state, 0, sizeof(WSavedState));
2813 state->hidden = -1;
2814 state->miniaturized = -1;
2815 state->shaded = -1;
2816 if (btn->dock == scr->dock)
2817 state->workspace = -1;
2818 else
2819 state->workspace = scr->current_workspace;
2821 wWindowAddSavedState(btn->wm_instance, btn->wm_class, cmdline, pid,
2822 state);
2823 wAddDeathHandler(pid, (WDeathHandler*)trackDeadProcess,
2824 btn->dock);
2825 } else if (state) {
2826 free(state);
2828 free(cmdline);
2829 return pid;
2833 void
2834 wDockHideIcons(WDock *dock)
2836 int i;
2837 WAppIcon *btn;
2839 if (dock==NULL)
2840 return;
2842 btn = dock->icon_array[0];
2844 for (i=1; i<dock->max_icons; i++) {
2845 if (dock->icon_array[i])
2846 XUnmapWindow(dpy, dock->icon_array[i]->icon->core->window);
2848 dock->mapped = 0;
2850 dockIconPaint(btn);
2854 void
2855 wDockShowIcons(WDock *dock)
2857 int i, newlevel;
2858 WAppIcon *btn;
2860 if (dock==NULL)
2861 return;
2863 btn = dock->icon_array[0];
2864 moveDock(dock, btn->x_pos, btn->y_pos);
2866 newlevel = dock->lowered ? WMNormalLevel : WMDockLevel;
2867 ChangeStackingLevel(btn->icon->core, newlevel);
2869 for (i=1; i<dock->max_icons; i++) {
2870 if (dock->icon_array[i]) {
2871 MoveInStackListAbove(dock->icon_array[i]->icon->core,
2872 btn->icon->core);
2873 break;
2877 if (!dock->collapsed) {
2878 for (i=1; i<dock->max_icons; i++) {
2879 if (dock->icon_array[i]) {
2880 XMapWindow(dpy, dock->icon_array[i]->icon->core->window);
2884 dock->mapped = 1;
2886 dockIconPaint(btn);
2890 void
2891 wDockLower(WDock *dock)
2893 int i;
2895 for (i=0; i<dock->max_icons; i++) {
2896 if (dock->icon_array[i])
2897 wLowerFrame(dock->icon_array[i]->icon->core);
2902 void
2903 wDockRaise(WDock *dock)
2905 int i;
2907 for (i=dock->max_icons-1; i>=0; i--) {
2908 if (dock->icon_array[i])
2909 wRaiseFrame(dock->icon_array[i]->icon->core);
2914 void
2915 wDockRaiseLower(WDock *dock)
2917 if (!dock->icon_array[0]->icon->core->stacking->above
2918 ||(dock->icon_array[0]->icon->core->stacking->window_level
2919 !=dock->icon_array[0]->icon->core->stacking->above->stacking->window_level))
2920 wDockLower(dock);
2921 else
2922 wDockRaise(dock);
2926 void
2927 wDockFinishLaunch(WDock *dock, WAppIcon *icon)
2929 icon->launching = 0;
2930 icon->relaunching = 0;
2931 dockIconPaint(icon);
2935 WAppIcon*
2936 wDockFindIconFor(WDock *dock, Window window)
2938 WAppIcon *icon;
2939 int i;
2941 for (i=0; i<dock->max_icons; i++) {
2942 icon = dock->icon_array[i];
2943 if (icon && icon->main_window == window)
2944 return icon;
2946 return NULL;
2950 void
2951 wDockTrackWindowLaunch(WDock *dock, Window window)
2953 WAppIcon *icon;
2954 #ifdef REDUCE_APPICONS
2955 WAppIconAppList *tapplist;
2956 #endif
2957 char *wm_class, *wm_instance;
2958 int i;
2959 Bool firstPass = True;
2960 Bool found = False;
2961 char *command = NULL;
2964 int argc;
2965 char **argv;
2967 if (XGetCommand(dpy, window, &argv, &argc)) {
2968 if (argc > 0 && argv != NULL)
2969 command = FlattenStringList(argv,argc);
2970 if (argv) {
2971 XFreeStringList(argv);
2976 if (!PropGetWMClass(window, &wm_class, &wm_instance) ||
2977 (!wm_class && !wm_instance))
2978 return;
2980 retry:
2981 for (i=0; i<dock->max_icons; i++) {
2982 icon = dock->icon_array[i];
2983 if (!icon)
2984 continue;
2986 /* app is already attached to icon */
2987 if (icon->main_window == window) {
2988 found = True;
2989 break;
2992 if ((icon->wm_instance || icon->wm_class)
2993 && (icon->launching
2994 || (dock->screen_ptr->flags.startup && !icon->running))) {
2996 if (icon->wm_instance && wm_instance &&
2997 strcmp(icon->wm_instance, wm_instance)!=0) {
2998 continue;
3000 if (icon->wm_class && wm_class &&
3001 strcmp(icon->wm_class, wm_class)!=0) {
3002 continue;
3004 if (firstPass && command && strcmp(icon->command, command)!=0) {
3005 continue;
3008 if (!icon->relaunching) {
3009 WApplication *wapp;
3011 /* Possibly an application that was docked with dockit,
3012 * but the user did not update WMState to indicate that
3013 * it was docked by force */
3014 wapp = wApplicationOf(window);
3015 if (!wapp) {
3016 icon->forced_dock = 1;
3017 icon->running = 0;
3019 if (!icon->forced_dock)
3020 icon->main_window = window;
3022 #ifdef REDUCE_APPICONS
3023 tapplist = wmalloc(sizeof(WAppIconAppList));
3024 memset(tapplist, 0, sizeof(WAppIconAppList));
3025 tapplist->next = icon->applist;
3026 if (icon->applist)
3027 icon->applist->prev = tapplist;
3028 icon->applist = tapplist;
3029 tapplist->wapp = wApplicationOf(window);
3030 icon->num_apps++;
3031 #endif
3033 found = True;
3034 wDockFinishLaunch(dock, icon);
3035 break;
3039 if (firstPass && !found) {
3040 firstPass = False;
3041 goto retry;
3044 if (command)
3045 free(command);
3047 if (wm_class)
3048 XFree(wm_class);
3049 if (wm_instance)
3050 XFree(wm_instance);
3055 void
3056 wClipUpdateForWorkspaceChange(WScreen *scr, int workspace)
3058 if (!wPreferences.flags.noclip) {
3059 scr->clip_icon->dock = scr->workspaces[workspace]->clip;
3060 if (scr->current_workspace != workspace) {
3061 WDock *old_clip = scr->workspaces[scr->current_workspace]->clip;
3063 wDockHideIcons(old_clip);
3064 if (old_clip->auto_raise_lower) {
3065 if (old_clip->auto_raise_magic) {
3066 WMDeleteTimerHandler(old_clip->auto_raise_magic);
3067 old_clip->auto_raise_magic = NULL;
3069 wDockLower(old_clip);
3071 if (old_clip->auto_collapse) {
3072 if (old_clip->auto_expand_magic) {
3073 WMDeleteTimerHandler(old_clip->auto_expand_magic);
3074 old_clip->auto_expand_magic = NULL;
3076 old_clip->collapsed = 1;
3078 wDockShowIcons(scr->workspaces[workspace]->clip);
3080 if (scr->flags.clip_balloon_mapped)
3081 showClipBalloon(scr->clip_icon->dock, workspace);
3087 static void
3088 trackDeadProcess(pid_t pid, unsigned char status, WDock *dock)
3090 WAppIcon *icon;
3091 int i;
3093 for (i=0; i<dock->max_icons; i++) {
3094 icon = dock->icon_array[i];
3095 if (!icon)
3096 continue;
3098 if (icon->launching && icon->pid == pid) {
3099 if (!icon->relaunching) {
3100 icon->running = 0;
3101 icon->main_window = None;
3103 wDockFinishLaunch(dock, icon);
3104 icon->pid = 0;
3105 if (status==111) {
3106 char msg[PATH_MAX];
3107 #ifdef OFFIX_DND
3108 sprintf(msg, _("Could not execute command \"%s\""),
3109 icon->drop_launch && icon->dnd_command
3110 ? icon->dnd_command : icon->command);
3111 #else
3112 sprintf(msg, _("Could not execute command \"%s\""),
3113 icon->command);
3114 #endif
3115 wMessageDialog(dock->screen_ptr, _("Error"), msg,
3116 _("OK"), NULL, NULL);
3118 break;
3124 static void
3125 toggleLowered(WDock *dock)
3127 WAppIcon *tmp;
3128 int newlevel, i;
3130 /* lower/raise Dock */
3131 if (!dock->lowered) {
3132 newlevel = WMNormalLevel;
3133 dock->lowered = 1;
3134 } else {
3135 newlevel = WMDockLevel;
3136 dock->lowered = 0;
3139 for (i=0; i<dock->max_icons; i++) {
3140 tmp = dock->icon_array[i];
3141 if (!tmp)
3142 continue;
3144 ChangeStackingLevel(tmp->icon->core, newlevel);
3145 if (dock->lowered)
3146 wLowerFrame(tmp->icon->core);
3149 if (dock->type == WM_DOCK)
3150 wScreenUpdateUsableArea(dock->screen_ptr);
3154 static void
3155 toggleCollapsed(WDock *dock)
3157 if (dock->collapsed) {
3158 dock->collapsed = 0;
3159 wDockShowIcons(dock);
3161 else {
3162 dock->collapsed = 1;
3163 wDockHideIcons(dock);
3168 static void
3169 openDockMenu(WDock *dock, WAppIcon *aicon, XEvent *event)
3171 WScreen *scr = dock->screen_ptr;
3172 WObjDescriptor *desc;
3173 WMenuEntry *entry;
3174 WApplication *wapp = NULL;
3175 int index = 0;
3176 int x_pos;
3177 int appIsRunning = aicon->running && aicon->icon && aicon->icon->owner;
3179 if (dock->type == WM_DOCK) {
3180 /* keep on top */
3181 entry = dock->menu->entries[index];
3182 entry->flags.indicator_on = !dock->lowered;
3183 entry->clientdata = dock;
3184 } else {
3185 /* clip options */
3186 if (scr->clip_options)
3187 updateClipOptionsMenu(scr->clip_options, dock);
3189 /* Rename Workspace */
3190 entry = dock->menu->entries[++index];
3191 entry->clientdata = dock;
3193 /* select icon */
3194 entry = dock->menu->entries[++index];
3195 entry->clientdata = aicon;
3196 wMenuSetEnabled(dock->menu, index, aicon!=scr->clip_icon);
3198 /* (un)select all icons */
3199 entry = dock->menu->entries[++index];
3200 entry->clientdata = aicon;
3201 wMenuSetEnabled(dock->menu, index, dock->icon_count > 1);
3203 /* keep icon(s) */
3204 entry = dock->menu->entries[++index];
3205 entry->clientdata = aicon;
3206 wMenuSetEnabled(dock->menu, index, dock->icon_count > 1);
3208 /* this is the workspace submenu part */
3209 if (scr->clip_submenu)
3210 updateWorkspaceMenu(scr->clip_submenu, aicon);
3211 index++;
3213 /* remove icon(s) */
3214 entry = dock->menu->entries[++index];
3215 entry->clientdata = aicon;
3216 wMenuSetEnabled(dock->menu, index, dock->icon_count > 1);
3218 /* attract icon(s) */
3219 entry = dock->menu->entries[++index];
3220 entry->clientdata = aicon;
3223 /* launch */
3224 entry = dock->menu->entries[++index];
3225 entry->clientdata = aicon;
3226 wMenuSetEnabled(dock->menu, index, aicon->command!=NULL);
3228 /* unhide here */
3229 entry = dock->menu->entries[++index];
3230 entry->clientdata = aicon;
3231 wMenuSetEnabled(dock->menu, index, appIsRunning);
3233 /* hide */
3234 entry = dock->menu->entries[++index];
3235 entry->clientdata = aicon;
3236 if (aicon->icon->owner) {
3237 wapp = wApplicationOf(aicon->icon->owner->main_window);
3238 if (wapp && wapp->flags.hidden)
3239 entry->text = _("Unhide");
3240 else
3241 entry->text = _("Hide");
3242 } else {
3243 entry->text = _("Hide");
3245 wMenuSetEnabled(dock->menu, index, appIsRunning);
3247 /* settings */
3248 entry = dock->menu->entries[++index];
3249 entry->clientdata = aicon;
3250 wMenuSetEnabled(dock->menu, index, !aicon->editing
3251 && !wPreferences.flags.noupdates);
3253 /* kill */
3254 entry = dock->menu->entries[++index];
3255 entry->clientdata = aicon;
3256 wMenuSetEnabled(dock->menu, index, appIsRunning);
3258 if (!dock->menu->flags.realized)
3259 wMenuRealize(dock->menu);
3261 if (dock->type == WM_CLIP) {
3262 x_pos = event->xbutton.x_root+2;
3263 } else {
3264 x_pos = dock->on_right_side ?
3265 scr->scr_width - dock->menu->frame->core->width - 2 : 0;
3268 wMenuMapAt(dock->menu, x_pos, event->xbutton.y_root+2, False);
3270 /* allow drag select */
3271 event->xany.send_event = True;
3272 desc = &dock->menu->menu->descriptor;
3273 (*desc->handle_mousedown)(desc, event);
3277 static void
3278 openClipWorkspaceMenu(WScreen *scr, int x, int y)
3280 if (!scr->clip_ws_menu) {
3281 scr->clip_ws_menu = wWorkspaceMenuMake(scr, False);
3283 wWorkspaceMenuUpdate(scr, scr->clip_ws_menu);
3284 wMenuMapAt(scr->clip_ws_menu, x, y, False);
3288 /******************************************************************/
3289 static void
3290 iconDblClick(WObjDescriptor *desc, XEvent *event)
3292 WAppIcon *btn = desc->parent;
3293 WDock *dock = btn->dock;
3294 WApplication *wapp = NULL;
3295 int unhideHere = 0;
3297 #ifdef REDUCE_APPICONS
3298 if ((btn->icon->owner && !(event->xbutton.state & ControlMask)) ||
3299 ((btn->icon->owner == NULL) && (btn->applist != NULL))) {
3300 if (btn->icon->owner == NULL)
3301 btn->icon->owner = btn->applist->wapp->main_window_desc;
3302 #ifdef I_HATE_THIS
3304 #endif
3305 #else
3306 if (btn->icon->owner && !(event->xbutton.state & ControlMask)) {
3307 #endif
3308 wapp = wApplicationOf(btn->icon->owner->main_window);
3310 assert(wapp!=NULL);
3312 unhideHere = (event->xbutton.state & ShiftMask);
3314 /* go to the last workspace that the user worked on the app */
3315 if (wapp->last_workspace != dock->screen_ptr->current_workspace
3316 && !unhideHere) {
3317 wWorkspaceChange(dock->screen_ptr, wapp->last_workspace);
3320 wUnhideApplication(wapp, event->xbutton.button==Button2,
3321 unhideHere);
3323 if (event->xbutton.state & MOD_MASK) {
3324 wHideOtherApplications(btn->icon->owner);
3326 } else {
3327 if (event->xbutton.button==Button1) {
3329 if (event->xbutton.state & MOD_MASK) {
3330 /* raise/lower dock */
3331 toggleLowered(dock);
3332 } else if (btn == dock->screen_ptr->clip_icon) {
3333 if (getClipButton(event->xbutton.x, event->xbutton.y)==CLIP_IDLE)
3334 toggleCollapsed(dock);
3335 else
3336 handleClipChangeWorkspace(dock->screen_ptr, event);
3337 } else if (btn->command) {
3338 if (!btn->launching &&
3339 (!btn->running || (event->xbutton.state & ControlMask))) {
3340 launchDockedApplication(btn);
3348 static void
3349 handleDockMove(WDock *dock, WAppIcon *aicon, XEvent *event)
3351 WScreen *scr = dock->screen_ptr;
3352 int ofs_x=event->xbutton.x, ofs_y=event->xbutton.y;
3353 int x, y;
3354 XEvent ev;
3355 int grabbed = 0, swapped = 0, done;
3356 Pixmap ghost = None;
3357 int superfluous = wPreferences.superfluous; /* we catch it to avoid problems */
3359 #ifdef DEBUG
3360 puts("moving dock");
3361 #endif
3362 if (XGrabPointer(dpy, aicon->icon->core->window, True, ButtonMotionMask
3363 |ButtonReleaseMask|ButtonPressMask, GrabModeAsync,
3364 GrabModeAsync, None, None, CurrentTime) !=GrabSuccess) {
3365 wwarning("pointer grab failed for dock move");
3367 y = 0;
3368 for (x=0; x<dock->max_icons; x++) {
3369 if (dock->icon_array[x]!=NULL &&
3370 dock->icon_array[x]->yindex > y)
3371 y = dock->icon_array[x]->yindex;
3373 y++;
3374 XResizeWindow(dpy, scr->dock_shadow, ICON_SIZE, ICON_SIZE*y);
3376 done = 0;
3377 while (!done) {
3378 WMMaskEvent(dpy, PointerMotionMask|ButtonReleaseMask|ButtonPressMask
3379 |ButtonMotionMask|ExposureMask, &ev);
3380 switch (ev.type) {
3381 case Expose:
3382 WMHandleEvent(&ev);
3383 break;
3385 case MotionNotify:
3386 if (!grabbed) {
3387 if (abs(ofs_x-ev.xmotion.x)>=MOVE_THRESHOLD
3388 || abs(ofs_y-ev.xmotion.y)>=MOVE_THRESHOLD) {
3389 XChangeActivePointerGrab(dpy, ButtonMotionMask
3390 |ButtonReleaseMask|ButtonPressMask,
3391 wCursor[WCUR_MOVE], CurrentTime);
3392 grabbed=1;
3394 break;
3396 if (dock->type == WM_CLIP) {
3397 if (ev.xmotion.x_root - ofs_x < 0) {
3398 x = 0;
3399 } else if (ev.xmotion.x_root - ofs_x + ICON_SIZE >
3400 scr->scr_width) {
3401 x = scr->scr_width - ICON_SIZE;
3402 } else {
3403 x = ev.xmotion.x_root - ofs_x;
3405 if (ev.xmotion.y_root - ofs_y < 0) {
3406 y = 0;
3407 } else if (ev.xmotion.y_root - ofs_y + ICON_SIZE >
3408 scr->scr_height) {
3409 y = scr->scr_height - ICON_SIZE;
3410 } else {
3411 y = ev.xmotion.y_root - ofs_y;
3413 moveDock(dock, x, y);
3414 } else {
3415 /* move vertically if pointer is inside the dock*/
3416 if ((dock->on_right_side &&
3417 ev.xmotion.x_root >= dock->x_pos - ICON_SIZE)
3418 || (!dock->on_right_side &&
3419 ev.xmotion.x_root <= dock->x_pos + ICON_SIZE*2)) {
3421 if (ev.xmotion.y_root - ofs_y < 0) {
3422 y = 0;
3423 } else if (ev.xmotion.y_root - ofs_y + ICON_SIZE >
3424 scr->scr_height) {
3425 y = scr->scr_height - ICON_SIZE;
3426 } else {
3427 y = ev.xmotion.y_root - ofs_y;
3429 moveDock(dock, dock->x_pos, y);
3431 /* move horizontally to change sides */
3432 x = ev.xmotion.x_root - ofs_x;
3433 if (!dock->on_right_side) {
3435 /* is on left */
3437 if (ev.xmotion.x_root > dock->x_pos + ICON_SIZE*2) {
3438 XMoveWindow(dpy, scr->dock_shadow, scr->scr_width-ICON_SIZE
3439 -DOCK_EXTRA_SPACE-1, dock->y_pos);
3440 if (superfluous) {
3441 if (ghost==None) {
3442 ghost = MakeGhostDock(dock, dock->x_pos,
3443 scr->scr_width-ICON_SIZE
3444 -DOCK_EXTRA_SPACE-1,
3445 dock->y_pos);
3446 XSetWindowBackgroundPixmap(dpy, scr->dock_shadow,
3447 ghost);
3448 XClearWindow(dpy, scr->dock_shadow);
3451 XMapRaised(dpy, scr->dock_shadow);
3452 swapped = 1;
3453 } else {
3454 if (superfluous && ghost!=None) {
3455 XFreePixmap(dpy, ghost);
3456 ghost = None;
3458 XUnmapWindow(dpy, scr->dock_shadow);
3459 swapped = 0;
3461 } else {
3462 /* is on right */
3463 if (ev.xmotion.x_root < dock->x_pos - ICON_SIZE) {
3464 XMoveWindow(dpy, scr->dock_shadow,
3465 DOCK_EXTRA_SPACE, dock->y_pos);
3466 if (superfluous) {
3467 if (ghost==None) {
3468 ghost = MakeGhostDock(dock, dock->x_pos,
3469 DOCK_EXTRA_SPACE, dock->y_pos);
3470 XSetWindowBackgroundPixmap(dpy, scr->dock_shadow,
3471 ghost);
3472 XClearWindow(dpy, scr->dock_shadow);
3475 XMapRaised(dpy, scr->dock_shadow);
3476 swapped = -1;
3477 } else {
3478 XUnmapWindow(dpy, scr->dock_shadow);
3479 swapped = 0;
3480 if (superfluous && ghost!=None) {
3481 XFreePixmap(dpy, ghost);
3482 ghost = None;
3487 break;
3489 case ButtonPress:
3490 break;
3492 case ButtonRelease:
3493 if (ev.xbutton.button != event->xbutton.button)
3494 break;
3495 XUngrabPointer(dpy, CurrentTime);
3496 XUnmapWindow(dpy, scr->dock_shadow);
3497 XResizeWindow(dpy, scr->dock_shadow, ICON_SIZE, ICON_SIZE);
3498 if (dock->type == WM_DOCK) {
3499 if (swapped!=0) {
3500 if (swapped>0)
3501 dock->on_right_side = 1;
3502 else
3503 dock->on_right_side = 0;
3504 swapDock(dock);
3505 wArrangeIcons(scr, False);
3508 done = 1;
3509 break;
3512 if (superfluous) {
3513 if (ghost!=None)
3514 XFreePixmap(dpy, ghost);
3515 XSetWindowBackground(dpy, scr->dock_shadow, scr->white_pixel);
3517 #ifdef DEBUG
3518 puts("End dock move");
3519 #endif
3524 static void
3525 handleIconMove(WDock *dock, WAppIcon *aicon, XEvent *event)
3527 WScreen *scr = dock->screen_ptr;
3528 Window wins[2];
3529 WIcon *icon = aicon->icon;
3530 WDock *dock2 = NULL, *last_dock = dock, *clip = NULL;
3531 int ondock, grabbed = 0, change_dock = 0, collapsed = 0;
3532 XEvent ev;
3533 int x = aicon->x_pos, y = aicon->y_pos;
3534 int ofs_x = event->xbutton.x, ofs_y = event->xbutton.y;
3535 int shad_x = x, shad_y = y;
3536 int ix = aicon->xindex, iy = aicon->yindex;
3537 int tmp;
3538 Pixmap ghost = None;
3539 Bool docked;
3540 int superfluous = wPreferences.superfluous; /* we catch it to avoid problems */
3542 if (wPreferences.flags.noupdates)
3543 return;
3545 if (XGrabPointer(dpy, icon->core->window, True, ButtonMotionMask
3546 |ButtonReleaseMask|ButtonPressMask, GrabModeAsync,
3547 GrabModeAsync, None, None, CurrentTime) !=GrabSuccess) {
3548 #ifdef DEBUG0
3549 wwarning("pointer grab failed for icon move");
3550 #endif
3553 wRaiseFrame(icon->core);
3555 if (!wPreferences.flags.noclip)
3556 clip = scr->workspaces[scr->current_workspace]->clip;
3558 if (dock == scr->dock && !wPreferences.flags.noclip)
3559 dock2 = clip;
3560 else if (dock != scr->dock && !wPreferences.flags.nodock)
3561 dock2 = scr->dock;
3563 wins[0] = icon->core->window;
3564 wins[1] = scr->dock_shadow;
3565 XRestackWindows(dpy, wins, 2);
3566 XMoveResizeWindow(dpy, scr->dock_shadow, aicon->x_pos, aicon->y_pos,
3567 ICON_SIZE, ICON_SIZE);
3568 if (superfluous) {
3569 if (icon->pixmap!=None)
3570 ghost = MakeGhostIcon(scr, icon->pixmap);
3571 else
3572 ghost = MakeGhostIcon(scr, icon->core->window);
3574 XSetWindowBackgroundPixmap(dpy, scr->dock_shadow, ghost);
3575 XClearWindow(dpy, scr->dock_shadow);
3577 XMapWindow(dpy, scr->dock_shadow);
3579 ondock = 1;
3582 while(1) {
3583 XMaskEvent(dpy, PointerMotionMask|ButtonReleaseMask|ButtonPressMask
3584 |ButtonMotionMask|ExposureMask, &ev);
3585 switch (ev.type) {
3586 case Expose:
3587 WMHandleEvent(&ev);
3588 break;
3590 case MotionNotify:
3591 if (!grabbed) {
3592 if (abs(ofs_x-ev.xmotion.x)>=MOVE_THRESHOLD
3593 || abs(ofs_y-ev.xmotion.y)>=MOVE_THRESHOLD) {
3594 XChangeActivePointerGrab(dpy, ButtonMotionMask
3595 |ButtonReleaseMask|ButtonPressMask,
3596 wCursor[WCUR_MOVE], CurrentTime);
3597 grabbed=1;
3598 } else {
3599 break;
3603 x = ev.xmotion.x_root - ofs_x;
3604 y = ev.xmotion.y_root - ofs_y;
3605 tmp = wDockSnapIcon(dock, aicon, x, y, &ix, &iy, True);
3606 if (tmp && dock2) {
3607 change_dock = 0;
3608 if (last_dock != dock && collapsed) {
3609 last_dock->collapsed = 1;
3610 wDockHideIcons(last_dock);
3611 collapsed = 0;
3613 if (!collapsed && (collapsed = dock->collapsed)) {
3614 dock->collapsed = 0;
3615 wDockShowIcons(dock);
3617 if (dock->auto_raise_lower)
3618 wDockRaise(dock);
3619 last_dock = dock;
3620 } else if (dock2) {
3621 tmp = wDockSnapIcon(dock2, aicon, x, y, &ix, &iy, False);
3622 if (tmp) {
3623 change_dock = 1;
3624 if (last_dock != dock2 && collapsed) {
3625 last_dock->collapsed = 1;
3626 wDockHideIcons(last_dock);
3627 collapsed = 0;
3629 if (!collapsed && (collapsed = dock2->collapsed)) {
3630 dock2->collapsed = 0;
3631 wDockShowIcons(dock2);
3633 if (dock2->auto_raise_lower)
3634 wDockRaise(dock2);
3635 last_dock = dock2;
3638 if (aicon->launching
3639 || (aicon->running && !(ev.xmotion.state & MOD_MASK))
3640 || (!aicon->running && tmp)) {
3641 shad_x = last_dock->x_pos + ix*wPreferences.icon_size;
3642 shad_y = last_dock->y_pos + iy*wPreferences.icon_size;
3644 XMoveWindow(dpy, scr->dock_shadow, shad_x, shad_y);
3646 if (!ondock) {
3647 XMapWindow(dpy, scr->dock_shadow);
3649 ondock = 1;
3650 } else {
3651 if (ondock) {
3652 XUnmapWindow(dpy, scr->dock_shadow);
3654 ondock = 0;
3656 XMoveWindow(dpy, icon->core->window, x, y);
3657 break;
3659 case ButtonPress:
3660 break;
3662 case ButtonRelease:
3663 if (ev.xbutton.button != event->xbutton.button)
3664 break;
3665 XUngrabPointer(dpy, CurrentTime);
3666 if (ondock) {
3667 SlideWindow(icon->core->window, x, y, shad_x, shad_y);
3668 XUnmapWindow(dpy, scr->dock_shadow);
3669 if (!change_dock) {
3670 reattachIcon(dock, aicon, ix, iy);
3671 if (clip && dock!=clip && clip->auto_raise_lower)
3672 wDockLower(clip);
3673 } else {
3674 docked = moveIconBetweenDocks(dock, dock2, aicon, ix, iy);
3675 if (!docked) {
3676 /* Slide it back if dock rejected it */
3677 SlideWindow(icon->core->window, x, y, aicon->x_pos,
3678 aicon->y_pos);
3679 reattachIcon(dock, aicon, aicon->xindex,aicon->yindex);
3681 if (last_dock->type==WM_CLIP && last_dock->auto_collapse) {
3682 collapsed = 0;
3685 } else {
3686 aicon->x_pos = x;
3687 aicon->y_pos = y;
3688 if (superfluous) {
3689 if (!aicon->running && !wPreferences.no_animations) {
3690 /* We need to deselect it, even if is deselected in
3691 * wDockDetach(), because else DoKaboom() will fail.
3693 if (aicon->icon->selected)
3694 wIconSelect(aicon->icon);
3695 DoKaboom(scr,aicon->icon->core->window, x, y);
3698 if (clip && clip->auto_raise_lower)
3699 wDockLower(clip);
3700 wDockDetach(dock, aicon);
3702 if (collapsed) {
3703 last_dock->collapsed = 1;
3704 wDockHideIcons(last_dock);
3705 collapsed = 0;
3707 if (superfluous) {
3708 if (ghost!=None)
3709 XFreePixmap(dpy, ghost);
3710 XSetWindowBackground(dpy, scr->dock_shadow, scr->white_pixel);
3712 #ifdef DEBUG
3713 puts("End icon move");
3714 #endif
3715 return;
3721 static int
3722 getClipButton(int px, int py)
3724 int pt = (CLIP_BUTTON_SIZE+2)*ICON_SIZE/64;
3726 if (px < 0 || py < 0 || px >= ICON_SIZE || py >= ICON_SIZE)
3727 return CLIP_IDLE;
3729 if (py <= pt-((int)ICON_SIZE-1-px))
3730 return CLIP_FORWARD;
3731 else if (px <= pt-((int)ICON_SIZE-1-py))
3732 return CLIP_REWIND;
3734 return CLIP_IDLE;
3738 static void
3739 handleClipChangeWorkspace(WScreen *scr, XEvent *event)
3741 XEvent ev;
3742 int done, direction, new_ws;
3743 int new_dir;
3744 WDock *clip = scr->clip_icon->dock;
3746 direction = getClipButton(event->xbutton.x, event->xbutton.y);
3748 clip->lclip_button_pushed = direction==CLIP_REWIND;
3749 clip->rclip_button_pushed = direction==CLIP_FORWARD;
3751 wClipIconPaint(scr->clip_icon);
3752 done = 0;
3753 while(!done) {
3754 WMMaskEvent(dpy, ExposureMask|ButtonMotionMask|ButtonReleaseMask
3755 |ButtonPressMask, &ev);
3756 switch (ev.type) {
3757 case Expose:
3758 WMHandleEvent(&ev);
3759 break;
3761 case MotionNotify:
3762 new_dir = getClipButton(ev.xmotion.x, ev.xmotion.y);
3763 if (new_dir != direction) {
3764 direction = new_dir;
3765 clip->lclip_button_pushed = direction==CLIP_REWIND;
3766 clip->rclip_button_pushed = direction==CLIP_FORWARD;
3767 wClipIconPaint(scr->clip_icon);
3769 break;
3771 case ButtonPress:
3772 break;
3774 case ButtonRelease:
3775 if (ev.xbutton.button == event->xbutton.button)
3776 done = 1;
3780 clip->lclip_button_pushed = 0;
3781 clip->rclip_button_pushed = 0;
3783 new_ws = wPreferences.ws_advance || (event->xbutton.state & ControlMask);
3785 if (direction == CLIP_FORWARD) {
3786 if (scr->current_workspace < scr->workspace_count-1)
3787 wWorkspaceChange(scr, scr->current_workspace+1);
3788 else if (new_ws && scr->current_workspace < MAX_WORKSPACES-1)
3789 wWorkspaceChange(scr, scr->current_workspace+1);
3790 else if (wPreferences.ws_cycle)
3791 wWorkspaceChange(scr, 0);
3793 else if (direction == CLIP_REWIND) {
3794 if (scr->current_workspace > 0)
3795 wWorkspaceChange(scr, scr->current_workspace-1);
3796 else if (scr->current_workspace==0 && wPreferences.ws_cycle)
3797 wWorkspaceChange(scr, scr->workspace_count-1);
3800 wClipIconPaint(scr->clip_icon);
3804 static void
3805 iconMouseDown(WObjDescriptor *desc, XEvent *event)
3807 WAppIcon *aicon = desc->parent;
3808 WDock *dock = aicon->dock;
3809 WScreen *scr = aicon->icon->core->screen_ptr;
3811 if (aicon->editing || WCHECK_STATE(WSTATE_MODAL))
3812 return;
3814 scr->last_dock = dock;
3816 if (dock->menu->flags.mapped)
3817 wMenuUnmap(dock->menu);
3819 if (IsDoubleClick(scr, event)) {
3820 /* double-click was not in the main clip icon */
3821 if (dock->type != WM_CLIP || aicon->xindex!=0 || aicon->yindex!=0
3822 || getClipButton(event->xbutton.x, event->xbutton.y)==CLIP_IDLE) {
3823 iconDblClick(desc, event);
3824 return;
3828 if (dock->type == WM_CLIP && scr->flags.clip_balloon_mapped) {
3829 XUnmapWindow(dpy, scr->clip_balloon);
3830 scr->flags.clip_balloon_mapped = 0;
3833 #ifdef DEBUG
3834 puts("handling dock");
3835 #endif
3836 if (event->xbutton.button == Button1) {
3837 if (event->xbutton.state & MOD_MASK)
3838 wDockLower(dock);
3839 else
3840 wDockRaise(dock);
3842 if ((event->xbutton.state & ShiftMask) && aicon!=scr->clip_icon &&
3843 dock->type!=WM_DOCK) {
3844 wIconSelect(aicon->icon);
3845 return;
3848 if (aicon->yindex==0 && aicon->xindex==0) {
3849 if (getClipButton(event->xbutton.x, event->xbutton.y)!=CLIP_IDLE
3850 && dock->type==WM_CLIP)
3851 handleClipChangeWorkspace(scr, event);
3852 else
3853 handleDockMove(dock, aicon, event);
3854 } else
3855 handleIconMove(dock, aicon, event);
3857 } else if (event->xbutton.button==Button2 && dock->type==WM_CLIP &&
3858 aicon->xindex==0 && aicon->yindex==0) {
3859 openClipWorkspaceMenu(scr, event->xbutton.x_root+2,
3860 event->xbutton.y_root+2);
3861 if (scr->clip_ws_menu) {
3862 WMenu *menu;
3863 menu = scr->clip_ws_menu;
3864 desc = &menu->menu->descriptor;
3866 event->xany.send_event = True;
3867 (*desc->handle_mousedown)(desc, event);
3869 } else if (event->xbutton.button == Button3) {
3870 openDockMenu(dock, aicon, event);
3875 static void
3876 showClipBalloon(WDock *dock, int workspace)
3878 int w, h;
3879 int x, y;
3880 WScreen *scr = dock->screen_ptr;
3881 char *text;
3882 Window stack[2];
3884 scr->flags.clip_balloon_mapped = 1;
3885 XMapWindow(dpy, scr->clip_balloon);
3887 text = scr->workspaces[workspace]->name;
3889 w = wTextWidth(scr->clip_title_font->font, text, strlen(text));
3891 h = scr->clip_title_font->height;
3892 XResizeWindow(dpy, scr->clip_balloon, w, h);
3894 x = dock->x_pos + CLIP_BUTTON_SIZE*ICON_SIZE/64;
3895 y = dock->y_pos + ICON_SIZE-scr->clip_title_font->height - 3;
3897 if (x+w > scr->scr_width) {
3898 x = scr->scr_width - w;
3899 if (dock->y_pos + ICON_SIZE + h > scr->scr_height)
3900 y = dock->y_pos - h - 1;
3901 else
3902 y = dock->y_pos + ICON_SIZE;
3903 XRaiseWindow(dpy, scr->clip_balloon);
3904 } else {
3905 stack[0] = scr->clip_icon->icon->core->window;
3906 stack[1] = scr->clip_balloon;
3907 XRestackWindows(dpy, stack, 2);
3909 XMoveWindow(dpy, scr->clip_balloon, x, y);
3910 XSetForeground(dpy, scr->clip_title_gc,
3911 scr->clip_title_pixel[CLIP_NORMAL]);
3912 XClearWindow(dpy, scr->clip_balloon);
3913 wDrawString(scr->clip_balloon, scr->clip_title_font, scr->clip_title_gc,
3914 0, scr->clip_title_font->y, text, strlen(text));
3918 static void
3919 clipEnterNotify(WObjDescriptor *desc, XEvent *event)
3921 WAppIcon *btn = (WAppIcon*)desc->parent;
3922 WDock *dock;
3924 assert(event->type==EnterNotify);
3926 if(desc->parent_type!=WCLASS_DOCK_ICON)
3927 return;
3929 dock = btn->dock;
3930 if (!dock || dock->type!=WM_CLIP)
3931 return;
3933 /* The auto raise/lower code */
3934 if (dock->auto_lower_magic) {
3935 WMDeleteTimerHandler(dock->auto_lower_magic);
3936 dock->auto_lower_magic = NULL;
3938 if (dock->auto_raise_lower && !dock->auto_raise_magic) {
3939 dock->auto_raise_magic = WMAddTimerHandler(AUTO_RAISE_DELAY,
3940 clipAutoRaise,
3941 (void *)dock);
3944 /* The auto expand/collapse code */
3945 if (dock->auto_collapse_magic) {
3946 WMDeleteTimerHandler(dock->auto_collapse_magic);
3947 dock->auto_collapse_magic = NULL;
3949 if (dock->auto_collapse && !dock->auto_expand_magic) {
3950 dock->auto_expand_magic = WMAddTimerHandler(AUTO_EXPAND_DELAY,
3951 clipAutoExpand,
3952 (void *)dock);
3955 if (btn->xindex == 0 && btn->yindex == 0)
3956 showClipBalloon(dock, dock->screen_ptr->current_workspace);
3957 else {
3958 if (dock->screen_ptr->flags.clip_balloon_mapped) {
3959 XUnmapWindow(dpy, dock->screen_ptr->clip_balloon);
3960 dock->screen_ptr->flags.clip_balloon_mapped = 0;
3966 static void
3967 clipLeave(WDock *dock)
3969 XEvent event;
3970 WObjDescriptor *desc = NULL;
3972 if (!dock || dock->type!=WM_CLIP)
3973 return;
3975 if (XCheckTypedEvent(dpy, EnterNotify, &event)!=False) {
3976 if (XFindContext(dpy, event.xcrossing.window, wWinContext,
3977 (XPointer *)&desc)!=XCNOENT
3978 && desc && desc->parent_type==WCLASS_DOCK_ICON
3979 && ((WAppIcon*)desc->parent)->dock
3980 && ((WAppIcon*)desc->parent)->dock->type==WM_CLIP) {
3981 /* We didn't left the Clip yet */
3982 XPutBackEvent(dpy, &event);
3983 return;
3986 XPutBackEvent(dpy, &event);
3987 } else {
3988 /* We entered a withdrawn window, so we're still in Clip */
3989 return;
3992 if (dock->auto_raise_magic) {
3993 WMDeleteTimerHandler(dock->auto_raise_magic);
3994 dock->auto_raise_magic = NULL;
3996 if (dock->auto_raise_lower && !dock->auto_lower_magic) {
3997 dock->auto_lower_magic = WMAddTimerHandler(AUTO_LOWER_DELAY,
3998 clipAutoLower,
3999 (void *)dock);
4002 if (dock->auto_expand_magic) {
4003 WMDeleteTimerHandler(dock->auto_expand_magic);
4004 dock->auto_expand_magic = NULL;
4006 if (dock->auto_collapse && !dock->auto_collapse_magic) {
4007 dock->auto_collapse_magic = WMAddTimerHandler(AUTO_COLLAPSE_DELAY,
4008 clipAutoCollapse,
4009 (void *)dock);
4014 static void
4015 clipLeaveNotify(WObjDescriptor *desc, XEvent *event)
4017 WAppIcon *btn = (WAppIcon*)desc->parent;
4019 assert(event->type==LeaveNotify);
4021 if(desc->parent_type!=WCLASS_DOCK_ICON)
4022 return;
4024 clipLeave(btn->dock);
4028 static void
4029 clipAutoCollapse(void *cdata)
4031 WDock *dock = (WDock *)cdata;
4033 if (dock->type!=WM_CLIP)
4034 return;
4036 if (dock->auto_collapse) {
4037 dock->collapsed = 1;
4038 wDockHideIcons(dock);
4040 dock->auto_collapse_magic = NULL;
4044 static void
4045 clipAutoExpand(void *cdata)
4047 WDock *dock = (WDock *)cdata;
4049 if (dock->type!=WM_CLIP)
4050 return;
4052 if (dock->auto_collapse) {
4053 dock->collapsed = 0;
4054 wDockShowIcons(dock);
4056 dock->auto_expand_magic = NULL;
4060 static void
4061 clipAutoLower(void *cdata)
4063 WDock *dock = (WDock *)cdata;
4065 if (dock->type!=WM_CLIP)
4066 return;
4068 if (dock->auto_raise_lower)
4069 wDockLower(dock);
4071 dock->auto_lower_magic = NULL;
4075 static void
4076 clipAutoRaise(void *cdata)
4078 WDock *dock = (WDock *)cdata;
4080 if (dock->type!=WM_CLIP)
4081 return;
4083 if (dock->auto_raise_lower)
4084 wDockRaise(dock);
4086 if (dock->screen_ptr->flags.clip_balloon_mapped) {
4087 showClipBalloon(dock, dock->screen_ptr->current_workspace);
4090 dock->auto_raise_magic = NULL;