Added the crash dialog panel
[wmaker-crm.git] / src / dock.c
blob9179773e3beeda57066bdf22fec848787f7ab994
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;
322 Bool collapsed = clipIcon->dock->collapsed;
324 if (rpushed) {
325 p[0].x = tp+1;
326 p[0].y = 1;
327 p[1].x = ICON_SIZE-2;
328 p[1].y = 1;
329 p[2].x = ICON_SIZE-2;
330 p[2].y = pt-1;
331 } else if (lpushed) {
332 p[0].x = 1;
333 p[0].y = tp;
334 p[1].x = pt;
335 p[1].y = ICON_SIZE-2;
336 p[2].x = 1;
337 p[2].y = ICON_SIZE-2;
339 if (lpushed || rpushed) {
340 XSetForeground(dpy, scr->draw_gc, scr->white_pixel);
341 XFillPolygon(dpy, win, scr->draw_gc, p, 3, Convex, CoordModeOrigin);
342 XSetForeground(dpy, scr->draw_gc, scr->black_pixel);
344 if (collapsed) {
345 gc = scr->clip_title_gc;
346 } else {
347 XSetFillStyle(dpy, scr->copy_gc, FillTiled);
348 XSetTile(dpy, scr->copy_gc, scr->clip_arrow_gradient);
349 XSetClipMask(dpy, scr->copy_gc, None);
350 gc = scr->copy_gc;
353 p[0].x = p[3].x = ICON_SIZE-6-as;
354 p[0].y = p[3].y = 5;
355 p[1].x = ICON_SIZE-6;
356 p[1].y = 5;
357 p[2].x = ICON_SIZE-6;
358 p[2].y = 5+as;
359 if (rpushed) {
360 XFillPolygon(dpy, win, scr->draw_gc, p, 3, Convex, CoordModeOrigin);
361 XDrawLines(dpy, win, scr->draw_gc, p, 4, CoordModeOrigin);
362 } else {
363 if (!collapsed)
364 XSetTSOrigin(dpy, gc, ICON_SIZE-6-as, 5);
366 XFillPolygon(dpy, win, gc, p,3,Convex,CoordModeOrigin);
367 XDrawLines(dpy, win, gc, p,4,CoordModeOrigin);
370 p[0].x = p[3].x = 5;
371 p[0].y = p[3].y = ICON_SIZE-6-as;
372 p[1].x = 5;
373 p[1].y = ICON_SIZE-6;
374 p[2].x = 5+as;
375 p[2].y = ICON_SIZE-6;
376 if (lpushed) {
377 XFillPolygon(dpy, win, scr->draw_gc, p, 3, Convex, CoordModeOrigin);
378 XDrawLines(dpy, win, scr->draw_gc, p, 4, CoordModeOrigin);
379 } else {
380 if (!collapsed)
381 XSetTSOrigin(dpy, gc, 5, ICON_SIZE-6-as);
383 XFillPolygon(dpy, win, gc, p,3,Convex,CoordModeOrigin);
384 XDrawLines(dpy, win, gc, p,4,CoordModeOrigin);
386 if (!collapsed)
387 XSetFillStyle(dpy, scr->copy_gc, FillSolid);
391 RImage*
392 wClipMakeTile(WScreen *scr, RImage *normalTile)
394 RImage *tile = RCloneImage(normalTile);
395 RColor black;
396 RColor dark;
397 RColor light;
398 int pt, tp;
400 pt = CLIP_BUTTON_SIZE*wPreferences.icon_size/64;
401 tp = wPreferences.icon_size-1 - pt;
403 black.alpha = 255;
404 black.red = black.green = black.blue = 0;
406 dark.alpha = 0;
407 dark.red = dark.green = dark.blue = 80;
409 light.alpha = 0;
410 light.red = light.green = light.blue = 80;
413 /* top right */
414 ROperateLine(tile, RSubtractOperation, tp, 0, wPreferences.icon_size-2,
415 pt-1, &dark);
416 RDrawLine(tile, tp-1, 0, wPreferences.icon_size-1, pt+1, &black);
417 ROperateLine(tile, RAddOperation, tp, 2, wPreferences.icon_size-3,
418 pt, &light);
421 /* bottom left */
422 ROperateLine(tile, RAddOperation, 2, tp+2, pt-2,
423 wPreferences.icon_size-3, &dark);
424 RDrawLine(tile, 0, tp-1, pt+1, wPreferences.icon_size-1, &black);
425 ROperateLine(tile, RSubtractOperation, 0, tp-2, pt+1,
426 wPreferences.icon_size-2, &light);
428 return tile;
432 static void
433 removeIconsCallback(WMenu *menu, WMenuEntry *entry)
435 WAppIcon *clickedIcon = (WAppIcon*)entry->clientdata;
436 WDock *dock;
437 WAppIcon *aicon;
438 LinkedList *selectedIcons;
439 int keepit;
441 assert(clickedIcon!=NULL);
443 dock = clickedIcon->dock;
445 selectedIcons = getSelected(dock);
447 if (selectedIcons) {
448 if (wMessageDialog(dock->screen_ptr, _("Workspace Clip"),
449 _("All selected icons will be removed!"),
450 _("OK"), _("Cancel"), NULL)!=WAPRDefault) {
451 return;
453 } else {
454 if (clickedIcon->xindex==0 && clickedIcon->yindex==0)
455 return;
456 selectedIcons = list_cons(clickedIcon, NULL);
459 while (selectedIcons) {
460 aicon = selectedIcons->head;
461 keepit = aicon->running && wApplicationOf(aicon->main_window);
462 wDockDetach(dock, aicon);
463 if (keepit) {
464 PlaceIcon(dock->screen_ptr, &aicon->x_pos, &aicon->y_pos);
465 XMoveWindow(dpy, aicon->icon->core->window,
466 aicon->x_pos, aicon->y_pos);
467 if (!dock->mapped || dock->collapsed)
468 XMapWindow(dpy, aicon->icon->core->window);
470 list_remove_head(&selectedIcons);
473 if (wPreferences.auto_arrange_icons)
474 wArrangeIcons(dock->screen_ptr, True);
478 static void
479 keepIconsCallback(WMenu *menu, WMenuEntry *entry)
481 WAppIcon *clickedIcon = (WAppIcon*)entry->clientdata;
482 WDock *dock;
483 WAppIcon *aicon;
484 LinkedList *selectedIcons;
486 assert(clickedIcon!=NULL);
487 dock = clickedIcon->dock;
489 selectedIcons = getSelected(dock);
491 if (!selectedIcons && clickedIcon!=dock->screen_ptr->clip_icon) {
492 char *command = NULL;
494 if (!clickedIcon->command && !clickedIcon->editing) {
495 clickedIcon->editing = 1;
496 if (wInputDialog(dock->screen_ptr, _("Keep Icon"),
497 _("Type the command used to launch the application"),
498 &command)) {
499 if (command && (command[0]==0 ||
500 (command[0]=='-' && command[1]==0))) {
501 free(command);
502 command = NULL;
504 clickedIcon->command = command;
505 clickedIcon->editing = 0;
506 } else {
507 clickedIcon->editing = 0;
508 if (command)
509 free(command);
510 return;
514 selectedIcons = list_cons(clickedIcon, NULL);
517 while (selectedIcons) {
518 aicon = selectedIcons->head;
519 if (aicon->icon->selected)
520 wIconSelect(aicon->icon);
521 if (aicon && aicon->attracted && aicon->command) {
522 aicon->attracted = 0;
523 if (aicon->icon->shadowed) {
524 aicon->icon->shadowed = 0;
525 aicon->icon->force_paint = 1;
526 wAppIconPaint(aicon);
529 list_remove_head(&selectedIcons);
536 static void
537 toggleAutoAttractCallback(WMenu *menu, WMenuEntry *entry)
539 WDock *dock = (WDock*)entry->clientdata;
541 assert(entry->clientdata!=NULL);
543 dock->attract_icons = !dock->attract_icons;
544 /*if (!dock->attract_icons)
545 dock->keep_attracted = 0;*/
547 entry->flags.indicator_on = dock->attract_icons;
549 wMenuPaint(menu);
553 static void
554 toggleKeepCallback(WMenu *menu, WMenuEntry *entry)
556 WDock *dock = (WDock*)entry->clientdata;
557 WAppIcon *btn;
558 int i;
560 assert(entry->clientdata!=NULL);
562 dock->keep_attracted = !dock->keep_attracted;
564 if (dock->keep_attracted) {
565 for (i=0; i< dock->max_icons; i++) {
566 btn = dock->icon_array[i];
567 if (btn && btn->attracted && btn->command) {
568 btn->attracted = 0;
569 if (btn->icon->shadowed) {
570 btn->icon->shadowed = 0;
571 btn->icon->force_paint = 1;
572 wAppIconPaint(btn);
578 entry->flags.indicator_on = dock->keep_attracted;
580 wMenuPaint(menu);
584 static void
585 selectCallback(WMenu *menu, WMenuEntry *entry)
587 WAppIcon *icon = (WAppIcon*)entry->clientdata;
589 assert(icon!=NULL);
591 wIconSelect(icon->icon);
593 wMenuPaint(menu);
597 static void
598 colectIconsCallback(WMenu *menu, WMenuEntry *entry)
600 WAppIcon *clickedIcon = (WAppIcon*)entry->clientdata;
601 WDock *clip;
602 WAppIcon *aicon;
603 int x, y, x_pos, y_pos;
605 assert(entry->clientdata!=NULL);
606 clip = clickedIcon->dock;
608 aicon = clip->screen_ptr->app_icon_list;
610 while (aicon) {
611 if (!aicon->docked && wDockFindFreeSlot(clip, &x, &y)) {
612 x_pos = clip->x_pos + x*ICON_SIZE;
613 y_pos = clip->y_pos + y*ICON_SIZE;
614 if (aicon->x_pos != x_pos || aicon->y_pos != y_pos) {
615 #ifdef ANIMATIONS
616 if (wPreferences.no_animations) {
617 XMoveWindow(dpy, aicon->icon->core->window, x_pos, y_pos);
618 } else {
619 SlideWindow(aicon->icon->core->window,
620 aicon->x_pos, aicon->y_pos, x_pos, y_pos);
622 #else
623 XMoveWindow(dpy, aicon->icon->core->window, x_pos, y_pos);
624 #endif /* ANIMATIONS */
626 aicon->attracted = 1;
627 if (!clip->keep_attracted && !aicon->icon->shadowed) {
628 aicon->icon->shadowed = 1;
629 aicon->icon->force_paint = 1;
630 /* We don't do an wAppIconPaint() here because it's in
631 * wDockAttachIcon(). -Dan
634 wDockAttachIcon(clip, aicon, x, y);
635 if (clip->collapsed || !clip->mapped)
636 XUnmapWindow(dpy, aicon->icon->core->window);
638 aicon = aicon->next;
643 static void
644 selectIconsCallback(WMenu *menu, WMenuEntry *entry)
646 WAppIcon *clickedIcon = (WAppIcon*)entry->clientdata;
647 WDock *dock;
648 LinkedList *selectedIcons;
649 WAppIcon *btn;
650 int i;
652 assert(clickedIcon!=NULL);
653 dock = clickedIcon->dock;
655 selectedIcons = getSelected(dock);
657 if (!selectedIcons) {
658 for (i=1; i<dock->max_icons; i++) {
659 btn = dock->icon_array[i];
660 if (btn && !btn->icon->selected) {
661 wIconSelect(btn->icon);
664 } else {
665 while(selectedIcons) {
666 btn = selectedIcons->head;
667 wIconSelect(btn->icon);
668 list_remove_head(&selectedIcons);
672 wMenuPaint(menu);
676 static void
677 toggleCollapsedCallback(WMenu *menu, WMenuEntry *entry)
679 assert(entry->clientdata!=NULL);
681 toggleCollapsed(entry->clientdata);
683 entry->flags.indicator_on = ((WDock*)entry->clientdata)->collapsed;
685 wMenuPaint(menu);
689 static void
690 toggleAutoCollapseCallback(WMenu *menu, WMenuEntry *entry)
692 WDock *dock;
693 assert(entry->clientdata!=NULL);
695 dock = (WDock*) entry->clientdata;
697 dock->auto_collapse = !dock->auto_collapse;
699 entry->flags.indicator_on = ((WDock*)entry->clientdata)->auto_collapse;
701 wMenuPaint(menu);
705 static void
706 toggleAutoRaiseLowerCallback(WMenu *menu, WMenuEntry *entry)
708 WDock *dock;
709 assert(entry->clientdata!=NULL);
711 dock = (WDock*) entry->clientdata;
713 dock->auto_raise_lower = !dock->auto_raise_lower;
715 entry->flags.indicator_on = ((WDock*)entry->clientdata)->auto_raise_lower;
717 wMenuPaint(menu);
721 static void
722 launchCallback(WMenu *menu, WMenuEntry *entry)
724 WAppIcon *btn = (WAppIcon*)entry->clientdata;
726 launchDockedApplication(btn);
730 static void
731 settingsCallback(WMenu *menu, WMenuEntry *entry)
733 WAppIcon *btn = (WAppIcon*)entry->clientdata;
735 if (btn->editing)
736 return;
737 ShowDockAppSettingsPanel(btn);
741 static void
742 hideCallback(WMenu *menu, WMenuEntry *entry)
744 WApplication *wapp;
745 WAppIcon *btn = (WAppIcon*)entry->clientdata;
747 wapp = wApplicationOf(btn->icon->owner->main_window);
749 if (wapp->flags.hidden) {
750 wWorkspaceChange(btn->icon->core->screen_ptr,wapp->last_workspace);
751 wUnhideApplication(wapp, False, False);
752 } else {
753 wHideApplication(wapp);
758 static void
759 unhideHereCallback(WMenu *menu, WMenuEntry *entry)
761 WApplication *wapp;
762 WAppIcon *btn = (WAppIcon*)entry->clientdata;
764 wapp = wApplicationOf(btn->icon->owner->main_window);
766 wUnhideApplication(wapp, False, True);
770 WAppIcon*
771 mainIconCreate(WScreen *scr, int type)
773 WAppIcon *btn;
774 int x_pos;
776 if (type == WM_CLIP) {
777 if (scr->clip_icon)
778 return scr->clip_icon;
779 btn = wAppIconCreateForDock(scr, NULL, "Logo", "WMClip", TILE_CLIP);
780 btn->icon->core->descriptor.handle_expose = clipIconExpose;
781 btn->icon->core->descriptor.handle_enternotify = clipEnterNotify;
782 btn->icon->core->descriptor.handle_leavenotify = clipLeaveNotify;
783 /*x_pos = scr->scr_width - ICON_SIZE*2 - DOCK_EXTRA_SPACE;*/
784 x_pos = 0;
785 } else {
786 btn = wAppIconCreateForDock(scr, NULL, "Logo", "WMDock", TILE_NORMAL);
787 x_pos = scr->scr_width - ICON_SIZE - DOCK_EXTRA_SPACE;
790 btn->xindex = 0;
791 btn->yindex = 0;
793 btn->icon->core->descriptor.handle_mousedown = iconMouseDown;
794 btn->icon->core->descriptor.parent_type = WCLASS_DOCK_ICON;
795 btn->icon->core->descriptor.parent = btn;
796 /*ChangeStackingLevel(btn->icon->core, WMDockLevel);*/
797 XMapWindow(dpy, btn->icon->core->window);
798 btn->x_pos = x_pos;
799 btn->y_pos = 0;
800 btn->docked = 1;
801 if (type == WM_CLIP)
802 scr->clip_icon = btn;
804 return btn;
808 static void
809 switchWSCommand(WMenu *menu, WMenuEntry *entry)
811 WAppIcon *btn, *icon = (WAppIcon*) entry->clientdata;
812 WScreen *scr = icon->icon->core->screen_ptr;
813 WDock *src, *dest;
814 LinkedList *selectedIcons;
815 int x, y;
817 if (entry->order == scr->current_workspace)
818 return;
819 src = icon->dock;
820 dest = scr->workspaces[entry->order]->clip;
822 selectedIcons = getSelected(src);
824 if (selectedIcons) {
825 while(selectedIcons) {
826 btn = selectedIcons->head;
827 if (wDockFindFreeSlot(dest, &x, &y)) {
828 moveIconBetweenDocks(src, dest, btn, x, y);
829 XUnmapWindow(dpy, btn->icon->core->window);
831 list_remove_head(&selectedIcons);
833 } else if (icon != scr->clip_icon) {
834 if (wDockFindFreeSlot(dest, &x, &y)) {
835 moveIconBetweenDocks(src, dest, icon, x, y);
836 XUnmapWindow(dpy, icon->icon->core->window);
843 static void
844 launchDockedApplication(WAppIcon *btn)
846 WScreen *scr = btn->icon->core->screen_ptr;
848 if (!btn->launching && btn->command!=NULL) {
849 if (!btn->forced_dock) {
850 btn->relaunching = btn->running;
851 btn->running = 1;
853 if (btn->wm_instance || btn->wm_class) {
854 WWindowAttributes attr;
855 memset(&attr, 0, sizeof(WWindowAttributes));
856 wDefaultFillAttributes(scr, btn->wm_instance, btn->wm_class,
857 &attr, NULL, True);
859 if (!attr.no_appicon && !btn->buggy_app)
860 btn->launching = 1;
861 else
862 btn->running = 0;
864 btn->drop_launch = 0;
865 scr->last_dock = btn->dock;
866 btn->pid = execCommand(btn, btn->command, NULL);
867 if (btn->pid>0) {
868 if (btn->buggy_app) {
869 /* give feedback that the app was launched */
870 btn->launching = 1;
871 dockIconPaint(btn);
872 btn->launching = 0;
873 WMAddTimerHandler(200, (WMCallback*)dockIconPaint, btn);
874 } else {
875 dockIconPaint(btn);
877 } else {
878 wwarning(_("could not launch application %s\n"), btn->command);
879 btn->launching = 0;
880 if (!btn->relaunching)
881 btn->running = 0;
888 static void
889 updateWorkspaceMenu(WMenu *menu, WAppIcon *icon)
891 WScreen *scr = menu->frame->screen_ptr;
892 char title[MAX_WORKSPACENAME_WIDTH+1];
893 int i;
895 if (!menu || !icon)
896 return;
898 for (i=0; i<scr->workspace_count; i++) {
899 if (i < menu->entry_no) {
900 if (strcmp(menu->entries[i]->text,scr->workspaces[i]->name)!=0) {
901 free(menu->entries[i]->text);
902 strcpy(title, scr->workspaces[i]->name);
903 menu->entries[i]->text = wstrdup(title);
904 menu->flags.realized = 0;
906 menu->entries[i]->clientdata = (void*)icon;
907 } else {
908 strcpy(title, scr->workspaces[i]->name);
910 wMenuAddCallback(menu, title, switchWSCommand, (void*)icon);
912 menu->flags.realized = 0;
914 if (i == scr->current_workspace) {
915 wMenuSetEnabled(menu, i, False);
916 } else {
917 wMenuSetEnabled(menu, i, True);
921 if (!menu->flags.realized)
922 wMenuRealize(menu);
926 static WMenu*
927 makeWorkspaceMenu(WScreen *scr)
929 WMenu *menu;
931 menu = wMenuCreate(scr, NULL, False);
932 if (!menu)
933 wwarning(_("could not create workspace submenu for Clip menu"));
935 wMenuAddCallback(menu, "", switchWSCommand, (void*)scr->clip_icon);
937 menu->flags.realized = 0;
938 wMenuRealize(menu);
940 return menu;
944 static void
945 updateClipOptionsMenu(WMenu *menu, WDock *dock)
947 WMenuEntry *entry;
948 int index = 0;
950 if (!menu || !dock)
951 return;
953 /* keep on top */
954 entry = menu->entries[index];
955 entry->flags.indicator_on = !dock->lowered;
956 entry->clientdata = dock;
958 /* collapsed */
959 entry = menu->entries[++index];
960 entry->flags.indicator_on = dock->collapsed;
961 entry->clientdata = dock;
963 /* auto-collapse */
964 entry = menu->entries[++index];
965 entry->flags.indicator_on = dock->auto_collapse;
966 entry->clientdata = dock;
968 /* auto-raise/lower */
969 entry = menu->entries[++index];
970 entry->flags.indicator_on = dock->auto_raise_lower;
971 entry->clientdata = dock;
973 /* attract icons */
974 entry = menu->entries[++index];
975 entry->flags.indicator_on = dock->attract_icons;
976 entry->clientdata = dock;
978 /* keep attracted icons */
979 entry = menu->entries[++index];
980 entry->flags.indicator_on = dock->keep_attracted;
981 entry->clientdata = dock;
983 menu->flags.realized = 0;
984 wMenuRealize(menu);
988 static WMenu*
989 makeClipOptionsMenu(WScreen *scr)
991 WMenu *menu;
992 WMenuEntry *entry;
994 menu = wMenuCreate(scr, NULL, False);
995 if (!menu) {
996 wwarning(_("could not create options submenu for Clip menu"));
997 return NULL;
1000 entry = wMenuAddCallback(menu, _("Keep on top"),
1001 toggleLoweredCallback, NULL);
1002 entry->flags.indicator = 1;
1003 entry->flags.indicator_on = 1;
1004 entry->flags.indicator_type = MI_CHECK;
1006 entry = wMenuAddCallback(menu, _("Collapsed"),
1007 toggleCollapsedCallback, NULL);
1008 entry->flags.indicator = 1;
1009 entry->flags.indicator_on = 1;
1010 entry->flags.indicator_type = MI_CHECK;
1012 entry = wMenuAddCallback(menu, _("AutoCollapse"),
1013 toggleAutoCollapseCallback, NULL);
1014 entry->flags.indicator = 1;
1015 entry->flags.indicator_on = 1;
1016 entry->flags.indicator_type = MI_CHECK;
1018 entry = wMenuAddCallback(menu, _("AutoRaiseLower"),
1019 toggleAutoRaiseLowerCallback, NULL);
1020 entry->flags.indicator = 1;
1021 entry->flags.indicator_on = 1;
1022 entry->flags.indicator_type = MI_CHECK;
1024 entry = wMenuAddCallback(menu, _("AutoAttract Icons"),
1025 toggleAutoAttractCallback, NULL);
1026 entry->flags.indicator = 1;
1027 entry->flags.indicator_on = 1;
1028 entry->flags.indicator_type = MI_CHECK;
1030 entry = wMenuAddCallback(menu, _("Keep Attracted Icons"),
1031 toggleKeepCallback, NULL);
1032 entry->flags.indicator = 1;
1033 entry->flags.indicator_on = 1;
1034 entry->flags.indicator_type = MI_CHECK;
1036 menu->flags.realized = 0;
1037 wMenuRealize(menu);
1039 return menu;
1043 static WMenu*
1044 dockMenuCreate(WScreen *scr, int type)
1046 WMenu *menu;
1047 WMenuEntry *entry;
1049 if (type == WM_CLIP && scr->clip_menu)
1050 return scr->clip_menu;
1052 menu = wMenuCreate(scr, NULL, False);
1053 if (type != WM_CLIP) {
1054 entry = wMenuAddCallback(menu, _("Keep on top"),
1055 toggleLoweredCallback, NULL);
1056 entry->flags.indicator = 1;
1057 entry->flags.indicator_on = 1;
1058 entry->flags.indicator_type = MI_CHECK;
1059 } else {
1060 entry = wMenuAddCallback(menu, _("Clip Options"), NULL, NULL);
1061 scr->clip_options = makeClipOptionsMenu(scr);
1062 if (scr->clip_options)
1063 wMenuEntrySetCascade(menu, entry, scr->clip_options);
1065 wMenuAddCallback(menu, _("Rename Workspace"), renameCallback, NULL);
1067 wMenuAddCallback(menu, _("(Un)Select Icon"), selectCallback, NULL);
1069 wMenuAddCallback(menu, _("(Un)Select All Icons"), selectIconsCallback,
1070 NULL);
1072 wMenuAddCallback(menu, _("Keep Icon(s)"), keepIconsCallback, NULL);
1074 entry = wMenuAddCallback(menu, _("Move Icon(s) To"), NULL, NULL);
1075 scr->clip_submenu = makeWorkspaceMenu(scr);
1076 if (scr->clip_submenu)
1077 wMenuEntrySetCascade(menu, entry, scr->clip_submenu);
1079 wMenuAddCallback(menu, _("Remove Icon(s)"), removeIconsCallback, NULL);
1081 wMenuAddCallback(menu, _("Attract Icons"), colectIconsCallback, NULL);
1084 wMenuAddCallback(menu, _("Launch"), launchCallback, NULL);
1086 wMenuAddCallback(menu, _("Unhide Here"), unhideHereCallback, NULL);
1088 entry = wMenuAddCallback(menu, _("Hide"), hideCallback, NULL);
1089 free(entry->text);
1090 entry->text = _("Hide");
1092 wMenuAddCallback(menu, _("Settings..."), settingsCallback, NULL);
1094 wMenuAddCallback(menu, _("Kill"), killCallback, NULL);
1096 if (type == WM_CLIP)
1097 scr->clip_menu = menu;
1099 return menu;
1103 WDock*
1104 wDockCreate(WScreen *scr, int type)
1106 WDock *dock;
1107 WAppIcon *btn;
1108 int icon_count;
1110 make_keys();
1112 dock = wmalloc(sizeof(WDock));
1113 memset(dock, 0, sizeof(WDock));
1115 if (type == WM_CLIP)
1116 icon_count = CLIP_MAX_ICONS;
1117 else
1118 icon_count = scr->scr_height/wPreferences.icon_size;
1120 dock->icon_array = wmalloc(sizeof(WAppIcon*)*icon_count);
1121 memset(dock->icon_array, 0, sizeof(WAppIcon*)*icon_count);
1123 dock->max_icons = icon_count;
1125 btn = mainIconCreate(scr, type);
1127 btn->dock = dock;
1129 dock->x_pos = btn->x_pos;
1130 dock->y_pos = btn->y_pos;
1131 dock->screen_ptr = scr;
1132 dock->type = type;
1133 dock->icon_count = 1;
1134 dock->on_right_side = 1;
1135 dock->collapsed = 0;
1136 dock->auto_collapse = 0;
1137 dock->auto_collapse_magic = NULL;
1138 dock->auto_raise_lower = 0;
1139 dock->auto_lower_magic = NULL;
1140 dock->auto_raise_magic = NULL;
1141 dock->attract_icons = 0;
1142 dock->keep_attracted = 0;
1143 dock->lowered = 1;
1144 dock->icon_array[0] = btn;
1145 wRaiseFrame(btn->icon->core);
1146 XMoveWindow(dpy, btn->icon->core->window, btn->x_pos, btn->y_pos);
1148 /* create dock menu */
1149 dock->menu = dockMenuCreate(scr, type);
1151 return dock;
1155 void
1156 wDockDestroy(WDock *dock)
1158 int i;
1159 WAppIcon *aicon;
1161 for (i=(dock->type == WM_CLIP) ? 1 : 0; i<dock->max_icons; i++) {
1162 aicon = dock->icon_array[i];
1163 if (aicon) {
1164 int keepit = aicon->running && wApplicationOf(aicon->main_window);
1165 wDockDetach(dock, aicon);
1166 if (keepit) {
1167 PlaceIcon(dock->screen_ptr, &aicon->x_pos, &aicon->y_pos);
1168 XMoveWindow(dpy, aicon->icon->core->window,
1169 aicon->x_pos, aicon->y_pos);
1170 if (!dock->mapped || dock->collapsed)
1171 XMapWindow(dpy, aicon->icon->core->window);
1175 if (wPreferences.auto_arrange_icons)
1176 wArrangeIcons(dock->screen_ptr, True);
1177 free(dock->icon_array);
1178 if (dock->menu && dock->type!=WM_CLIP)
1179 wMenuDestroy(dock->menu, True);
1180 free(dock);
1184 void
1185 wClipIconPaint(WAppIcon *aicon)
1187 WScreen *scr = aicon->icon->core->screen_ptr;
1188 WWorkspace *workspace = scr->workspaces[scr->current_workspace];
1189 GC gc;
1190 Window win = aicon->icon->core->window;
1191 int length, nlength;
1192 char *ws_name, ws_number[10];
1193 int ty, tx;
1195 wIconPaint(aicon->icon);
1197 length = strlen(workspace->name);
1198 ws_name = malloc(length + 1);
1199 sprintf(ws_name, "%s", workspace->name);
1200 sprintf(ws_number, "%i", scr->current_workspace + 1);
1201 nlength = strlen(ws_number);
1203 gc = scr->clip_title_gc;
1205 if (!workspace->clip->collapsed)
1206 XSetForeground(dpy, gc, scr->clip_title_pixel[CLIP_NORMAL]);
1207 else
1208 XSetForeground(dpy, gc, scr->clip_title_pixel[CLIP_COLLAPSED]);
1210 ty = ICON_SIZE - scr->clip_title_font->height - 3;
1212 tx = CLIP_BUTTON_SIZE*ICON_SIZE/64;
1214 wDrawString(win, scr->clip_title_font, gc, tx,
1215 ty + scr->clip_title_font->y, ws_name, length);
1217 tx = (ICON_SIZE/2 - wTextWidth(scr->clip_title_font->font, ws_number, nlength))/2;
1219 wDrawString(win, scr->clip_title_font, gc, tx,
1220 scr->clip_title_font->y + 2, ws_number, nlength);
1222 free(ws_name);
1224 if (aicon->launching) {
1225 XFillRectangle(dpy, aicon->icon->core->window, scr->stipple_gc,
1226 0, 0, wPreferences.icon_size, wPreferences.icon_size);
1228 paintClipButtons(aicon, aicon->dock->lclip_button_pushed,
1229 aicon->dock->rclip_button_pushed);
1233 static void
1234 clipIconExpose(WObjDescriptor *desc, XEvent *event)
1236 wClipIconPaint(desc->parent);
1240 static void
1241 dockIconPaint(WAppIcon *btn)
1243 if (btn == btn->icon->core->screen_ptr->clip_icon)
1244 wClipIconPaint(btn);
1245 else
1246 wAppIconPaint(btn);
1250 static proplist_t
1251 make_icon_state(WAppIcon *btn)
1253 proplist_t node = NULL;
1254 proplist_t command, autolaunch, name, forced, host, position, buggy;
1255 char *tmp;
1256 char buffer[64];
1258 if (btn) {
1259 if (!btn->command)
1260 command = PLMakeString("-");
1261 else
1262 command = PLMakeString(btn->command);
1264 autolaunch = btn->auto_launch ? dYes : dNo;
1266 tmp = EscapeWM_CLASS(btn->wm_instance, btn->wm_class);
1268 name = PLMakeString(tmp);
1270 free(tmp);
1272 forced = btn->forced_dock ? dYes : dNo;
1274 buggy = btn->buggy_app ? dYes: dNo;
1276 if (btn == btn->icon->core->screen_ptr->clip_icon)
1277 sprintf(buffer, "%i,%i", btn->x_pos, btn->y_pos);
1278 else
1279 sprintf(buffer, "%hi,%hi", btn->xindex, btn->yindex);
1280 position = PLMakeString(buffer);
1282 node = PLMakeDictionaryFromEntries(dCommand, command,
1283 dName, name,
1284 dAutoLaunch, autolaunch,
1285 dForced, forced,
1286 dBuggyApplication, buggy,
1287 dPosition, position,
1288 NULL);
1289 PLRelease(command);
1290 PLRelease(name);
1291 PLRelease(position);
1292 #ifdef OFFIX_DND
1293 if (btn->dnd_command) {
1294 command = PLMakeString(btn->dnd_command);
1295 PLInsertDictionaryEntry(node, dDropCommand, command);
1296 PLRelease(command);
1298 #endif /* OFFIX_DND */
1300 if (btn->client_machine && btn->remote_start) {
1301 host = PLMakeString(btn->client_machine);
1302 PLInsertDictionaryEntry(node, dHost, host);
1303 PLRelease(host);
1307 return node;
1311 static proplist_t
1312 dockSaveState(WDock *dock)
1314 int i;
1315 proplist_t icon_info;
1316 proplist_t list=NULL, dock_state=NULL;
1317 proplist_t value;
1318 char buffer[256];
1320 list = PLMakeArrayFromElements(NULL);
1322 for (i=(dock->type==WM_DOCK ? 0 : 1); i<dock->max_icons; i++) {
1323 WAppIcon *btn = dock->icon_array[i];
1325 if (!btn || (btn->attracted && !dock->keep_attracted))
1326 continue;
1328 if ((icon_info = make_icon_state(dock->icon_array[i]))) {
1329 list = PLAppendArrayElement(list, icon_info);
1330 PLRelease(icon_info);
1334 dock_state = PLMakeDictionaryFromEntries(dApplications, list, NULL);
1336 PLRelease(list);
1338 if (dock->type == WM_DOCK) {
1339 sprintf(buffer, "%i,%i", (dock->on_right_side ? -ICON_SIZE : 0),
1340 dock->y_pos);
1341 value = PLMakeString(buffer);
1342 PLInsertDictionaryEntry(dock_state, dPosition, value);
1343 PLRelease(value);
1346 value = (dock->lowered ? dYes : dNo);
1347 PLInsertDictionaryEntry(dock_state, dLowered, value);
1349 if (dock->type == WM_CLIP) {
1350 value = (dock->collapsed ? dYes : dNo);
1351 PLInsertDictionaryEntry(dock_state, dCollapsed, value);
1353 value = (dock->auto_collapse ? dYes : dNo);
1354 PLInsertDictionaryEntry(dock_state, dAutoCollapse, value);
1356 value = (dock->auto_raise_lower ? dYes : dNo);
1357 PLInsertDictionaryEntry(dock_state, dAutoRaiseLower, value);
1359 value = (dock->attract_icons ? dYes : dNo);
1360 PLInsertDictionaryEntry(dock_state, dAutoAttractIcons, value);
1362 value = (dock->keep_attracted ? dYes : dNo);
1363 PLInsertDictionaryEntry(dock_state, dKeepAttracted, value);
1366 return dock_state;
1370 void
1371 wDockSaveState(WScreen *scr)
1373 proplist_t dock_state;
1375 dock_state = dockSaveState(scr->dock);
1377 PLInsertDictionaryEntry(scr->session_state, dDock, dock_state);
1379 PLRelease(dock_state);
1383 void
1384 wClipSaveState(WScreen *scr)
1386 proplist_t clip_state;
1388 clip_state = make_icon_state(scr->clip_icon);
1390 PLInsertDictionaryEntry(scr->session_state, dClip, clip_state);
1392 PLRelease(clip_state);
1396 proplist_t
1397 wClipSaveWorkspaceState(WScreen *scr, int workspace)
1399 return dockSaveState(scr->workspaces[workspace]->clip);
1403 static WAppIcon*
1404 restore_icon_state(WScreen *scr, proplist_t info, int type, int index)
1406 WAppIcon *aicon;
1407 char *wclass, *winstance;
1408 proplist_t cmd, value;
1409 char *command;
1412 cmd = PLGetDictionaryEntry(info, dCommand);
1413 if (!cmd || !PLIsString(cmd)) {
1414 return NULL;
1417 /* parse window name */
1418 value = PLGetDictionaryEntry(info, dName);
1419 if (!value)
1420 return NULL;
1422 ParseWindowName(value, &winstance, &wclass, "dock");
1424 if (!winstance && !wclass) {
1425 return NULL;
1428 /* get commands */
1430 if (cmd)
1431 command = wstrdup(PLGetString(cmd));
1432 else
1433 command = NULL;
1435 if (!command || strcmp(command, "-")==0) {
1436 if (command)
1437 free(command);
1438 if (wclass)
1439 free(wclass);
1440 if (winstance)
1441 free(winstance);
1443 return NULL;
1446 aicon = wAppIconCreateForDock(scr, command, winstance, wclass,
1447 TILE_NORMAL);
1448 if (wclass)
1449 free(wclass);
1450 if (winstance)
1451 free(winstance);
1453 aicon->icon->core->descriptor.handle_mousedown = iconMouseDown;
1454 if (type == WM_CLIP) {
1455 aicon->icon->core->descriptor.handle_enternotify = clipEnterNotify;
1456 aicon->icon->core->descriptor.handle_leavenotify = clipLeaveNotify;
1458 aicon->icon->core->descriptor.parent_type = WCLASS_DOCK_ICON;
1459 aicon->icon->core->descriptor.parent = aicon;
1462 #ifdef OFFIX_DND
1463 cmd = PLGetDictionaryEntry(info, dDropCommand);
1464 if (cmd)
1465 aicon->dnd_command = wstrdup(PLGetString(cmd));
1466 #endif
1468 /* check auto launch */
1469 value = PLGetDictionaryEntry(info, dAutoLaunch);
1471 aicon->auto_launch = 0;
1472 if (value) {
1473 if (PLIsString(value)) {
1474 if (strcasecmp(PLGetString(value), "YES")==0)
1475 aicon->auto_launch = 1;
1476 } else {
1477 wwarning(_("bad value in docked icon state info %s"),
1478 PLGetString(dAutoLaunch));
1482 /* check if it wasn't normally docked */
1483 value = PLGetDictionaryEntry(info, dForced);
1485 aicon->forced_dock = 0;
1486 if (value) {
1487 if (PLIsString(value)) {
1488 if (strcasecmp(PLGetString(value), "YES")==0)
1489 aicon->forced_dock = 1;
1490 } else {
1491 wwarning(_("bad value in docked icon state info %s"),
1492 PLGetString(dForced));
1496 /* check if we can rely on the stuff in the app */
1497 value = PLGetDictionaryEntry(info, dBuggyApplication);
1499 aicon->buggy_app = 0;
1500 if (value) {
1501 if (PLIsString(value)) {
1502 if (strcasecmp(PLGetString(value), "YES")==0)
1503 aicon->buggy_app = 1;
1504 } else {
1505 wwarning(_("bad value in docked icon state info %s"),
1506 PLGetString(dBuggyApplication));
1510 /* get position in the dock */
1511 value = PLGetDictionaryEntry(info, dPosition);
1512 if (value && PLIsString(value)) {
1513 if (sscanf(PLGetString(value), "%hi,%hi", &aicon->xindex,
1514 &aicon->yindex)!=2)
1515 wwarning(_("bad value in docked icon state info %s"),
1516 PLGetString(dPosition));
1518 /* check position sanity */
1519 /* incomplete section! */
1520 if (type == WM_DOCK) {
1521 aicon->xindex = 0;
1522 if (aicon->yindex < 0)
1523 wwarning(_("bad value in docked icon position %i,%i"),
1524 aicon->xindex, aicon->yindex);
1526 } else {
1527 aicon->yindex = index;
1528 aicon->xindex = 0;
1531 aicon->running = 0;
1532 aicon->docked = 1;
1534 return aicon;
1538 #define COMPLAIN(key) wwarning(_("bad value in dock state info:%s"), key)
1541 WAppIcon*
1542 wClipRestoreState(WScreen *scr, proplist_t clip_state)
1544 WAppIcon *icon;
1545 proplist_t value;
1548 icon = mainIconCreate(scr, WM_CLIP);
1550 if (!clip_state)
1551 return icon;
1552 else
1553 PLRetain(clip_state);
1555 /* restore position */
1557 value = PLGetDictionaryEntry(clip_state, dPosition);
1559 if (value) {
1560 if (!PLIsString(value))
1561 COMPLAIN("Position");
1562 else {
1563 if (sscanf(PLGetString(value), "%i,%i", &icon->x_pos,
1564 &icon->y_pos)!=2)
1565 COMPLAIN("Position");
1567 /* check position sanity */
1568 if (icon->y_pos < 0)
1569 icon->y_pos = 0;
1570 else if (icon->y_pos > scr->scr_height-ICON_SIZE)
1571 icon->y_pos = scr->scr_height-ICON_SIZE;
1573 if (icon->x_pos < 0)
1574 icon->x_pos = 0;
1575 else if (icon->x_pos > scr->scr_width-ICON_SIZE)
1576 icon->x_pos = scr->scr_width-ICON_SIZE;
1580 #ifdef OFFIX_DND
1581 value = PLGetDictionaryEntry(clip_state, dDropCommand);
1582 if (value && PLIsString(value))
1583 icon->dnd_command = wstrdup(PLGetString(value));
1584 #endif
1586 PLRelease(clip_state);
1588 return icon;
1592 WDock*
1593 wDockRestoreState(WScreen *scr, proplist_t dock_state, int type)
1595 WDock *dock;
1596 proplist_t apps;
1597 proplist_t value;
1598 WAppIcon *aicon, *old_top;
1599 int count, i;
1602 dock = wDockCreate(scr, type);
1604 if (!dock_state)
1605 return dock;
1607 if (dock_state)
1608 PLRetain(dock_state);
1611 /* restore position */
1613 value = PLGetDictionaryEntry(dock_state, dPosition);
1615 if (value) {
1616 if (!PLIsString(value))
1617 COMPLAIN("Position");
1618 else {
1619 if (sscanf(PLGetString(value), "%i,%i", &dock->x_pos,
1620 &dock->y_pos)!=2)
1621 COMPLAIN("Position");
1623 /* check position sanity */
1624 if (dock->y_pos < 0)
1625 dock->y_pos = 0;
1626 else if (dock->y_pos > scr->scr_height-ICON_SIZE)
1627 dock->y_pos = scr->scr_height - ICON_SIZE;
1629 /* This is no more needed. ??? */
1630 if (type == WM_CLIP) {
1631 if (dock->x_pos < 0)
1632 dock->x_pos = 0;
1633 else if (dock->x_pos > scr->scr_width-ICON_SIZE)
1634 dock->x_pos = scr->scr_width-ICON_SIZE;
1635 } else {
1636 if (dock->x_pos >= 0) {
1637 dock->x_pos = DOCK_EXTRA_SPACE;
1638 dock->on_right_side = 0;
1639 } else {
1640 dock->x_pos = scr->scr_width - DOCK_EXTRA_SPACE - ICON_SIZE;
1641 dock->on_right_side = 1;
1647 /* restore lowered/raised state */
1649 dock->lowered = 0;
1651 value = PLGetDictionaryEntry(dock_state, dLowered);
1653 if (value) {
1654 if (!PLIsString(value))
1655 COMPLAIN("Lowered");
1656 else {
1657 if (strcasecmp(PLGetString(value), "YES")==0)
1658 dock->lowered = 1;
1663 /* restore collapsed state */
1665 dock->collapsed = 0;
1667 value = PLGetDictionaryEntry(dock_state, dCollapsed);
1669 if (value) {
1670 if (!PLIsString(value))
1671 COMPLAIN("Collapsed");
1672 else {
1673 if (strcasecmp(PLGetString(value), "YES")==0)
1674 dock->collapsed = 1;
1679 /* restore auto-collapsed state */
1681 value = PLGetDictionaryEntry(dock_state, dAutoCollapse);
1683 if (value) {
1684 if (!PLIsString(value))
1685 COMPLAIN("AutoCollapse");
1686 else {
1687 if (strcasecmp(PLGetString(value), "YES")==0) {
1688 dock->auto_collapse = 1;
1689 dock->collapsed = 1;
1695 /* restore auto-raise/lower state */
1697 value = PLGetDictionaryEntry(dock_state, dAutoRaiseLower);
1699 if (value) {
1700 if (!PLIsString(value))
1701 COMPLAIN("AutoRaiseLower");
1702 else {
1703 if (strcasecmp(PLGetString(value), "YES")==0) {
1704 dock->auto_raise_lower = 1;
1710 /* restore attract icons state */
1712 dock->attract_icons = 0;
1714 value = PLGetDictionaryEntry(dock_state, dAutoAttractIcons);
1716 if (value) {
1717 if (!PLIsString(value))
1718 COMPLAIN("AutoAttractIcons");
1719 else {
1720 if (strcasecmp(PLGetString(value), "YES")==0)
1721 dock->attract_icons = 1;
1726 /* restore keep attracted icons state */
1728 dock->keep_attracted = 0;
1730 value = PLGetDictionaryEntry(dock_state, dKeepAttracted);
1732 if (value) {
1733 if (!PLIsString(value))
1734 COMPLAIN("KeepAttracted");
1735 else {
1736 if (strcasecmp(PLGetString(value), "YES")==0)
1737 dock->keep_attracted = 1;
1742 /* application list */
1744 apps = PLGetDictionaryEntry(dock_state, dApplications);
1746 if (!apps) {
1747 goto finish;
1750 count = PLGetNumberOfElements(apps);
1752 if (count==0)
1753 goto finish;
1755 old_top = dock->icon_array[0];
1757 /* dock->icon_count is set to 1 when dock is created.
1758 * Since Clip is already restored, we want to keep it so for clip,
1759 * but for dock we may change the default top tile, so we set it to 0.
1761 if (type == WM_DOCK)
1762 dock->icon_count = 0;
1764 for (i=0; i<count; i++) {
1765 if (dock->icon_count >= dock->max_icons) {
1766 wwarning(_("there are too many icons stored in dock. Ignoring what doesn't fit"));
1767 break;
1770 value = PLGetArrayElement(apps, i);
1771 aicon = restore_icon_state(scr, value, type, dock->icon_count);
1773 dock->icon_array[dock->icon_count] = aicon;
1775 if (aicon) {
1776 aicon->dock = dock;
1777 aicon->x_pos = dock->x_pos + (aicon->xindex*ICON_SIZE);
1778 aicon->y_pos = dock->y_pos + (aicon->yindex*ICON_SIZE);
1780 if (dock->lowered)
1781 ChangeStackingLevel(aicon->icon->core, WMNormalLevel);
1782 else
1783 ChangeStackingLevel(aicon->icon->core, WMDockLevel);
1785 wCoreConfigure(aicon->icon->core, aicon->x_pos, aicon->y_pos,
1786 0, 0);
1788 if (!dock->collapsed)
1789 XMapWindow(dpy, aicon->icon->core->window);
1790 wRaiseFrame(aicon->icon->core);
1792 dock->icon_count++;
1793 } else if (dock->icon_count==0 && type==WM_DOCK)
1794 dock->icon_count++;
1797 /* if the first icon is not defined, use the default */
1798 if (dock->icon_array[0]==NULL) {
1799 /* update default icon */
1800 old_top->x_pos = dock->x_pos;
1801 old_top->y_pos = dock->y_pos;
1802 if (dock->lowered)
1803 ChangeStackingLevel(old_top->icon->core, WMNormalLevel);
1804 else
1805 ChangeStackingLevel(old_top->icon->core, WMDockLevel);
1806 dock->icon_array[0] = old_top;
1807 XMoveWindow(dpy, old_top->icon->core->window, dock->x_pos, dock->y_pos);
1808 /* we don't need to increment dock->icon_count here because it was
1809 * incremented in the loop above.
1811 } else if (old_top!=dock->icon_array[0]) {
1812 if (old_top == scr->clip_icon)
1813 scr->clip_icon = dock->icon_array[0];
1814 wAppIconDestroy(old_top);
1817 finish:
1818 if (dock_state)
1819 PLRelease(dock_state);
1821 return dock;
1826 void
1827 wDockLaunchWithState(WDock *dock, WAppIcon *btn, WSavedState *state)
1829 if (btn && btn->command && !btn->running && !btn->launching) {
1831 btn->drop_launch = 0;
1833 btn->pid = execCommand(btn, btn->command, state);
1835 if (btn->pid>0) {
1836 if (!btn->forced_dock && !btn->buggy_app) {
1837 btn->launching = 1;
1838 dockIconPaint(btn);
1841 } else {
1842 free(state);
1847 void
1848 wDockDoAutoLaunch(WDock *dock, int workspace)
1850 WAppIcon *btn;
1851 WSavedState *state;
1852 int i;
1854 for (i = 0; i < dock->max_icons; i++) {
1855 btn = dock->icon_array[i];
1856 if (!btn || !btn->auto_launch)
1857 continue;
1859 state = wmalloc(sizeof(WSavedState));
1860 memset(state, 0, sizeof(WSavedState));
1861 state->workspace = workspace;
1862 /* TODO: this is klugy and is very difficult to understand
1863 * what's going on. Try to clean up */
1864 wDockLaunchWithState(dock, btn, state);
1868 #ifdef REDUCE_APPICONS
1869 void
1870 wDockSimulateLaunch(WDock *dock, WAppIcon *btn)
1872 if ((btn == NULL) || (dock == NULL))
1873 return;
1875 if (!btn->running) {
1876 if ((btn->icon->owner == NULL) && (btn->applist))
1877 btn->icon->owner = btn->applist->wapp->main_window_desc;
1878 if (!btn->forced_dock)
1879 btn->launching = 1;
1880 dockIconPaint(btn);
1881 wusleep(5000);
1884 #endif
1886 #ifdef OFFIX_DND
1887 static WDock*
1888 findDock(WScreen *scr, XEvent *event, int *icon_pos)
1890 WDock *dock;
1891 int i;
1893 *icon_pos = -1;
1894 if ((dock = scr->dock)!=NULL) {
1895 for (i=0; i<dock->max_icons; i++) {
1896 if (dock->icon_array[i]
1897 && dock->icon_array[i]->icon->core->window==event->xclient.window) {
1898 *icon_pos = i;
1899 break;
1903 if (*icon_pos<0 && (dock = scr->workspaces[scr->current_workspace]->clip)!=NULL) {
1904 for (i=0; i<dock->max_icons; i++) {
1905 if (dock->icon_array[i]
1906 && dock->icon_array[i]->icon->core->window==event->xclient.window) {
1907 *icon_pos = i;
1908 break;
1912 if(*icon_pos>=0)
1913 return dock;
1914 return NULL;
1919 wDockReceiveDNDDrop(WScreen *scr, XEvent *event)
1921 WDock *dock;
1922 WAppIcon *btn;
1923 int icon_pos;
1925 dock = findDock(scr, event, &icon_pos);
1926 if (!dock)
1927 return False;
1930 * Return True if the drop was on an application icon window.
1931 * In this case, let the ClientMessage handler redirect the
1932 * message to the app.
1934 if (dock->icon_array[icon_pos]->icon->icon_win!=None)
1935 return True;
1937 if (dock->icon_array[icon_pos]->dnd_command!=NULL) {
1938 scr->flags.dnd_data_convertion_status = 0;
1940 btn = dock->icon_array[icon_pos];
1942 if (!btn->forced_dock) {
1943 btn->relaunching = btn->running;
1944 btn->running = 1;
1946 if (btn->wm_instance || btn->wm_class) {
1947 WWindowAttributes attr;
1948 memset(&attr, 0, sizeof(WWindowAttributes));
1949 wDefaultFillAttributes(btn->icon->core->screen_ptr,
1950 btn->wm_instance,
1951 btn->wm_class, &attr, NULL, True);
1953 if (!attr.no_appicon)
1954 btn->launching = 1;
1955 else
1956 btn->running = 0;
1959 btn->drop_launch = 1;
1960 scr->last_dock = dock;
1961 btn->pid = execCommand(btn, btn->dnd_command, NULL);
1962 if (btn->pid>0) {
1963 dockIconPaint(btn);
1964 } else {
1965 btn->launching = 0;
1966 if (!btn->relaunching) {
1967 btn->running = 0;
1971 return False;
1973 #endif /* OFFIX_DND */
1977 Bool
1978 wDockAttachIcon(WDock *dock, WAppIcon *icon, int x, int y)
1980 WWindow *wwin;
1981 char **argv;
1982 int argc;
1983 int index;
1985 wwin = icon->icon->owner;
1986 if (icon->command==NULL) {
1987 icon->editing = 0;
1988 if (XGetCommand(dpy, wwin->client_win, &argv, &argc) && argc>0) {
1990 icon->command = FlattenStringList(argv, argc);
1991 XFreeStringList(argv);
1992 } else {
1993 char *command=NULL;
1995 /* icon->forced_dock = 1;*/
1996 if (!icon->attracted || dock->type!=WM_CLIP || dock->keep_attracted) {
1997 icon->editing = 1;
1998 if (wInputDialog(dock->screen_ptr, _("Dock Icon"),
1999 _("Type the command used to launch the application"),
2000 &command)) {
2001 if (command && (command[0]==0 ||
2002 (command[0]=='-' && command[1]==0))) {
2003 free(command);
2004 command = NULL;
2006 icon->command = command;
2007 icon->editing = 0;
2008 } else {
2009 icon->editing = 0;
2010 if (command)
2011 free(command);
2012 /* If the target is the dock, reject the icon. If
2013 * the target is the clip, make it an attracted icon
2015 if (dock->type==WM_CLIP) {
2016 icon->attracted = 1;
2017 if (!icon->icon->shadowed) {
2018 icon->icon->shadowed = 1;
2019 icon->icon->force_paint = 1;
2021 } else {
2022 return False;
2027 } else {
2028 icon->editing = 0;
2031 for (index=1; index<dock->max_icons; index++)
2032 if (dock->icon_array[index] == NULL)
2033 break;
2034 /* if (index == dock->max_icons)
2035 return; */
2037 assert(index < dock->max_icons);
2039 dock->icon_array[index] = icon;
2040 icon->yindex = y;
2041 icon->xindex = x;
2043 icon->x_pos = dock->x_pos + x*ICON_SIZE;
2044 icon->y_pos = dock->y_pos + y*ICON_SIZE;
2046 dock->icon_count++;
2048 icon->running = 1;
2049 icon->launching = 0;
2050 icon->docked = 1;
2051 icon->dock = dock;
2052 icon->icon->core->descriptor.handle_mousedown = iconMouseDown;
2053 if (dock->type == WM_CLIP) {
2054 icon->icon->core->descriptor.handle_enternotify = clipEnterNotify;
2055 icon->icon->core->descriptor.handle_leavenotify = clipLeaveNotify;
2057 icon->icon->core->descriptor.parent_type = WCLASS_DOCK_ICON;
2058 icon->icon->core->descriptor.parent = icon;
2060 MoveInStackListUnder(dock->icon_array[index-1]->icon->core,
2061 icon->icon->core);
2062 wAppIconMove(icon, icon->x_pos, icon->y_pos);
2063 wAppIconPaint(icon);
2065 if (wPreferences.auto_arrange_icons)
2066 wArrangeIcons(dock->screen_ptr, True);
2068 #ifdef OFFIX_DND
2069 if (icon->command && !icon->dnd_command) {
2070 icon->dnd_command = wmalloc(strlen(icon->command)+8);
2071 sprintf(icon->dnd_command, "%s %%d", icon->command);
2073 #endif
2075 return True;
2079 void
2080 reattachIcon(WDock *dock, WAppIcon *icon, int x, int y)
2082 int index;
2084 for(index=1; index<dock->max_icons; index++) {
2085 if(dock->icon_array[index] == icon)
2086 break;
2088 assert(index < dock->max_icons);
2090 icon->yindex = y;
2091 icon->xindex = x;
2093 icon->x_pos = dock->x_pos + x*ICON_SIZE;
2094 icon->y_pos = dock->y_pos + y*ICON_SIZE;
2098 Bool
2099 moveIconBetweenDocks(WDock *src, WDock *dest, WAppIcon *icon, int x, int y)
2101 WWindow *wwin;
2102 char **argv;
2103 int argc;
2104 int index;
2106 if (dest == NULL)
2107 return False;
2109 wwin = icon->icon->owner;
2112 * For the moment we can't do this if we move icons in Clip from one
2113 * workspace to other, because if we move two or more icons without
2114 * command, the dialog box will not be able to tell us to which of the
2115 * moved icons it applies. -Dan
2117 if ((dest->type==WM_DOCK /*|| dest->keep_attracted*/) && icon->command==NULL) {
2118 if (XGetCommand(dpy, wwin->client_win, &argv, &argc) && argc>0) {
2120 icon->command = FlattenStringList(argv, argc);
2121 XFreeStringList(argv);
2122 } else {
2123 char *command=NULL;
2125 icon->editing = 1;
2126 /* icon->forced_dock = 1;*/
2127 if (wInputDialog(src->screen_ptr, _("Dock Icon"),
2128 _("Type the command used to launch the application"),
2129 &command)) {
2130 if (command && (command[0]==0 ||
2131 (command[0]=='-' && command[1]==0))) {
2132 free(command);
2133 command = NULL;
2135 icon->command = command;
2136 } else {
2137 icon->editing = 0;
2138 if (command)
2139 free(command);
2140 return False;
2142 icon->editing = 0;
2146 for(index=1; index<src->max_icons; index++) {
2147 if(src->icon_array[index] == icon)
2148 break;
2150 assert(index < src->max_icons);
2152 src->icon_array[index] = NULL;
2153 src->icon_count--;
2155 for(index=1; index<dest->max_icons; index++) {
2156 if(dest->icon_array[index] == NULL)
2157 break;
2159 /* if (index == dest->max_icons)
2160 return; */
2162 assert(index < dest->max_icons);
2164 dest->icon_array[index] = icon;
2165 icon->dock = dest;
2167 /* deselect the icon */
2168 if (icon->icon->selected)
2169 wIconSelect(icon->icon);
2171 if (dest->type == WM_DOCK) {
2172 icon->icon->core->descriptor.handle_enternotify = NULL;
2173 icon->icon->core->descriptor.handle_leavenotify = NULL;
2174 } else {
2175 icon->icon->core->descriptor.handle_enternotify = clipEnterNotify;
2176 icon->icon->core->descriptor.handle_leavenotify = clipLeaveNotify;
2179 /* set it to be kept when moving to dock, or to a clip that keep the
2180 * attracted icons.
2181 * Unless the icon does not have a command set
2183 if (icon->command && (dest->type==WM_DOCK || dest->keep_attracted)) {
2184 icon->attracted = 0;
2185 if (icon->icon->shadowed) {
2186 icon->icon->shadowed = 0;
2187 icon->icon->force_paint = 1;
2191 if (src->auto_collapse || src->auto_raise_lower)
2192 clipLeave(src);
2194 icon->yindex = y;
2195 icon->xindex = x;
2197 icon->x_pos = dest->x_pos + x*ICON_SIZE;
2198 icon->y_pos = dest->y_pos + y*ICON_SIZE;
2200 dest->icon_count++;
2202 MoveInStackListUnder(dest->icon_array[index-1]->icon->core,
2203 icon->icon->core);
2204 wAppIconPaint(icon);
2206 return True;
2210 void
2211 wDockDetach(WDock *dock, WAppIcon *icon)
2213 int index;
2215 /* make the settings panel be closed */
2216 if (icon->panel) {
2217 DestroyDockAppSettingsPanel(icon->panel);
2220 icon->docked = 0;
2221 icon->dock = NULL;
2222 icon->attracted = 0;
2223 if (icon->icon->shadowed) {
2224 icon->icon->shadowed = 0;
2225 icon->icon->force_paint = 1;
2228 /* deselect the icon */
2229 if (icon->icon->selected)
2230 wIconSelect(icon->icon);
2232 if (icon->command) {
2233 free(icon->command);
2234 icon->command = NULL;
2236 #ifdef OFFIX_DND
2237 if (icon->dnd_command) {
2238 free(icon->dnd_command);
2239 icon->dnd_command = NULL;
2241 #endif
2243 for (index=1; index<dock->max_icons; index++)
2244 if (dock->icon_array[index] == icon)
2245 break;
2246 assert(index < dock->max_icons);
2247 dock->icon_array[index] = NULL;
2248 icon->yindex = -1;
2249 icon->xindex = -1;
2250 dock->icon_count--;
2252 /* if the dock is not attached to an application or
2253 * the the application did not set the approriate hints yet,
2254 * destroy the icon */
2255 #ifdef REDUCE_APPICONS
2256 if ((icon->num_apps == 0) && (!icon->running || !wApplicationOf(icon->main_window)) )
2257 #else
2258 if (!icon->running || !wApplicationOf(icon->main_window))
2259 #endif
2260 wAppIconDestroy(icon);
2261 else {
2262 icon->icon->core->descriptor.handle_mousedown = appIconMouseDown;
2263 icon->icon->core->descriptor.handle_enternotify = NULL;
2264 icon->icon->core->descriptor.handle_leavenotify = NULL;
2265 icon->icon->core->descriptor.parent_type = WCLASS_APPICON;
2266 icon->icon->core->descriptor.parent = icon;
2268 ChangeStackingLevel(icon->icon->core, NORMAL_ICON_LEVEL);
2270 wAppIconPaint(icon);
2271 if (wPreferences.auto_arrange_icons) {
2272 wArrangeIcons(dock->screen_ptr, True);
2275 if (dock->auto_collapse || dock->auto_raise_lower)
2276 clipLeave(dock);
2281 * returns the closest Dock slot index for the passed
2282 * coordinates.
2284 * Returns False if icon can't be docked.
2286 Bool
2287 wDockSnapIcon(WDock *dock, WAppIcon *icon, int req_x, int req_y,
2288 int *ret_x, int *ret_y, int redocking)
2290 WScreen *scr = dock->screen_ptr;
2291 int dx, dy;
2292 int ex_x, ex_y;
2293 int i, offset = ICON_SIZE/2;
2294 WAppIcon *aicon = NULL;
2295 WAppIcon *nicon = NULL;
2296 int max_y_icons, max_x_icons;
2298 max_x_icons = scr->scr_width/ICON_SIZE;
2299 max_y_icons = scr->scr_height/ICON_SIZE-1;
2301 if (wPreferences.flags.noupdates)
2302 return False;
2304 dx = dock->x_pos;
2305 dy = dock->y_pos;
2307 /* if the dock is full */
2308 if (!redocking &&
2309 (dock->icon_count >= dock->max_icons)) {
2310 return False;
2313 /* exact position */
2314 if (req_y < dy)
2315 ex_y = (req_y - offset - dy)/ICON_SIZE;
2316 else
2317 ex_y = (req_y + offset - dy)/ICON_SIZE;
2319 if (req_x < dx)
2320 ex_x = (req_x - offset - dx)/ICON_SIZE;
2321 else
2322 ex_x = (req_x + offset - dx)/ICON_SIZE;
2324 /* check if the icon is outside the screen boundaries */
2325 if (dx + ex_x*ICON_SIZE < -ICON_SIZE+2 ||
2326 dx + ex_x*ICON_SIZE >= scr->scr_width-1 ||
2327 dy + ex_y*ICON_SIZE < -ICON_SIZE+2 ||
2328 dy + ex_y*ICON_SIZE >= scr->scr_height-1)
2329 return False;
2331 if (dock->type == WM_DOCK) {
2332 if (icon->dock != dock && ex_x != 0)
2333 return False;
2335 for (i=0; i<dock->max_icons; i++) {
2336 nicon = dock->icon_array[i];
2337 if (nicon && nicon->yindex == ex_y) {
2338 aicon = nicon;
2339 break;
2343 *ret_x = 0;
2345 if (redocking) {
2346 int sig, done, closest;
2348 /* Possible cases when redocking:
2350 * icon dragged out of range of any slot -> false
2351 * icon dragged to range of free slot
2352 * icon dragged to range of same slot
2353 * icon dragged to range of different icon
2355 if (abs(ex_x) > DOCK_DETTACH_THRESHOLD)
2356 return False;
2358 if (ex_y >= 0 && ex_y <= max_y_icons && (aicon == icon || !aicon)) {
2360 *ret_y = ex_y;
2363 return True;
2366 /* start looking at the upper slot or lower? */
2367 if (ex_y*ICON_SIZE < (req_y + offset - dy))
2368 sig = 1;
2369 else
2370 sig = -1;
2372 closest = -1;
2373 done = 0;
2374 /* look for closest free slot */
2375 for (i=0; i<(DOCK_DETTACH_THRESHOLD+1)*2 && !done; i++) {
2376 int j;
2378 done = 1;
2379 closest = sig*(i/2) + ex_y;
2380 /* check if this slot is used */
2381 if (closest >= 0) {
2382 for (j = 0; j<dock->max_icons; j++) {
2383 if (dock->icon_array[j]
2384 && dock->icon_array[j]->yindex==closest) {
2385 /* slot is used by someone else */
2386 if (dock->icon_array[j]!=icon)
2387 done = 0;
2388 break;
2392 sig = -sig;
2394 if (done && closest >= 0 && closest <= max_y_icons &&
2395 ((ex_y >= closest && ex_y - closest < DOCK_DETTACH_THRESHOLD+1)
2397 (ex_y < closest && closest - ex_y <= DOCK_DETTACH_THRESHOLD+1))) {
2399 *ret_y = closest;
2401 return True;
2403 } else { /* !redocking */
2405 /* if slot is free and the icon is close enough, return it */
2406 if (!aicon && ex_x == 0 && ex_y >= 0 && ex_y <= max_y_icons) {
2407 *ret_y = ex_y;
2408 return True;
2411 } else { /* CLIP */
2412 int neighbours = 0;
2414 for (i=0; i<dock->max_icons; i++) {
2415 nicon = dock->icon_array[i];
2416 if (nicon && nicon->xindex == ex_x && nicon->yindex == ex_y) {
2417 aicon = nicon;
2418 break;
2422 for (i=0; i<dock->max_icons; i++) {
2423 nicon = dock->icon_array[i];
2424 if (nicon && nicon != icon && /* Icon can't be it's own neighbour */
2425 (abs(nicon->xindex - ex_x) <= CLIP_ATTACH_VICINITY &&
2426 abs(nicon->yindex - ex_y) <= CLIP_ATTACH_VICINITY)) {
2427 neighbours = 1;
2428 break;
2432 if (neighbours && (aicon==NULL || (redocking && aicon == icon))) {
2433 *ret_x = ex_x;
2434 *ret_y = ex_y;
2435 return True;
2438 return False;
2441 #define MIN(x, y) ((x) > (y) ? (y) : (x))
2442 #define MAX(x, y) ((x) < (y) ? (y) : (x))
2444 #define ON_SCREEN(x, y, sx, ex, sy, ey) \
2445 ((((x)+ICON_SIZE/2) >= (sx)) && (((y)+ICON_SIZE/2) >= (sy)) && \
2446 (((x) + (ICON_SIZE/2)) <= (ex)) && (((y) + (ICON_SIZE/2)) <= ey))
2450 * returns true if it can find a free slot in the dock,
2451 * in which case it changes x_pos and y_pos accordingly.
2452 * Else returns false.
2454 Bool
2455 wDockFindFreeSlot(WDock *dock, int *x_pos, int *y_pos)
2457 WScreen *scr = dock->screen_ptr;
2458 WAppIcon *btn;
2459 unsigned char *slot_map;
2460 int mwidth;
2461 int r;
2462 int x, y;
2463 int i, done = False;
2464 int corner;
2465 int sx=0, sy=0, ex=scr->scr_width, ey=scr->scr_height;
2468 /* if the dock is full */
2469 if (dock->icon_count >= dock->max_icons) {
2470 return False;
2473 if (!wPreferences.flags.nodock && scr->dock) {
2474 if (scr->dock->on_right_side)
2475 ex -= ICON_SIZE + DOCK_EXTRA_SPACE;
2476 else
2477 sx += ICON_SIZE + DOCK_EXTRA_SPACE;
2480 if (ex < dock->x_pos)
2481 ex = dock->x_pos;
2482 if (sx > dock->x_pos+ICON_SIZE)
2483 sx = dock->x_pos+ICON_SIZE;
2484 #define C_NONE 0
2485 #define C_NW 1
2486 #define C_NE 2
2487 #define C_SW 3
2488 #define C_SE 4
2490 /* check if clip is in a corner */
2491 if (dock->type==WM_CLIP) {
2492 if (dock->x_pos < 1 && dock->y_pos < 1)
2493 corner = C_NE;
2494 else if (dock->x_pos < 1 && dock->y_pos >= (ey-ICON_SIZE))
2495 corner = C_SE;
2496 else if (dock->x_pos >= (ex-ICON_SIZE)&& dock->y_pos >= (ey-ICON_SIZE))
2497 corner = C_SW;
2498 else if (dock->x_pos >= (ex-ICON_SIZE) && dock->y_pos < 1)
2499 corner = C_NW;
2500 else
2501 corner = C_NONE;
2502 } else
2503 corner = C_NONE;
2505 /* If the clip is in the corner, use only slots that are in the border
2506 * of the screen */
2507 if (corner!=C_NONE) {
2508 char *hmap, *vmap;
2509 int hcount, vcount;
2511 hcount = MIN(dock->max_icons, scr->scr_width/ICON_SIZE);
2512 vcount = MIN(dock->max_icons, scr->scr_height/ICON_SIZE);
2513 hmap = wmalloc(hcount+1);
2514 memset(hmap, 0, hcount+1);
2515 vmap = wmalloc(vcount+1);
2516 memset(vmap, 0, vcount+1);
2518 /* mark used positions */
2519 switch (corner) {
2520 case C_NE:
2521 for (i=0; i<dock->max_icons; i++) {
2522 btn = dock->icon_array[i];
2523 if (!btn)
2524 continue;
2526 if (btn->xindex==0 && btn->yindex > 0 && btn->yindex < vcount)
2527 vmap[btn->yindex] = 1;
2528 else if (btn->yindex==0 && btn->xindex>0 && btn->xindex<hcount)
2529 hmap[btn->xindex] = 1;
2531 case C_NW:
2532 for (i=0; i<dock->max_icons; i++) {
2533 btn = dock->icon_array[i];
2534 if (!btn)
2535 continue;
2537 if (btn->xindex==0 && btn->yindex > 0 && btn->yindex < vcount)
2538 vmap[btn->yindex] = 1;
2539 else if (btn->yindex==0 && btn->xindex<0 &&btn->xindex>-hcount)
2540 hmap[-btn->xindex] = 1;
2542 case C_SE:
2543 for (i=0; i<dock->max_icons; i++) {
2544 btn = dock->icon_array[i];
2545 if (!btn)
2546 continue;
2548 if (btn->xindex==0 && btn->yindex < 0 && btn->yindex > -vcount)
2549 vmap[-btn->yindex] = 1;
2550 else if (btn->yindex==0 && btn->xindex>0 && btn->xindex<hcount)
2551 hmap[btn->xindex] = 1;
2553 case C_SW:
2554 default:
2555 for (i=0; i<dock->max_icons; i++) {
2556 btn = dock->icon_array[i];
2557 if (!btn)
2558 continue;
2560 if (btn->xindex==0 && btn->yindex < 0 && btn->yindex > -vcount)
2561 vmap[-btn->yindex] = 1;
2562 else if (btn->yindex==0 && btn->xindex<0 &&btn->xindex>-hcount)
2563 hmap[-btn->xindex] = 1;
2566 x=0; y=0;
2567 done = 0;
2568 /* search a vacant slot */
2569 for (i=1; i<MAX(vcount, hcount); i++) {
2570 if (i < vcount && vmap[i]==0) {
2571 /* found a slot */
2572 x = 0;
2573 y = i;
2574 done = 1;
2575 break;
2576 } else if (i < hcount && hmap[i]==0) {
2577 /* found a slot */
2578 x = i;
2579 y = 0;
2580 done = 1;
2581 break;
2584 free(vmap);
2585 free(hmap);
2586 /* If found a slot, translate and return */
2587 if (done) {
2588 if (corner==C_NW || corner==C_NE) {
2589 *y_pos = y;
2590 } else {
2591 *y_pos = -y;
2593 if (corner==C_NE || corner==C_SE) {
2594 *x_pos = x;
2595 } else {
2596 *x_pos = -x;
2598 return True;
2600 /* else, try to find a slot somewhere else */
2603 /* a map of mwidth x mwidth would be enough if we allowed icons to be
2604 * placed outside of screen */
2605 mwidth = (int)ceil(sqrt(dock->max_icons));
2607 /* In the worst case (the clip is in the corner of the screen),
2608 * the amount of icons that fit in the clip is smaller.
2609 * Double the map to get a safe value.
2611 mwidth += mwidth;
2613 r = (mwidth-1)/2;
2615 slot_map = wmalloc(mwidth*mwidth);
2616 memset(slot_map, 0, mwidth*mwidth);
2618 #define XY2OFS(x,y) (MAX(abs(x),abs(y)) > r) ? 0 : (((y)+r)*(mwidth)+(x)+r)
2620 /* mark used slots in the map. If the slot falls outside the map
2621 * (for example, when all icons are placed in line), ignore them. */
2622 for (i=0; i<dock->max_icons; i++) {
2623 btn = dock->icon_array[i];
2624 if (btn)
2625 slot_map[XY2OFS(btn->xindex, btn->yindex)] = 1;
2627 /* Find closest slot from the center that is free by scanning the
2628 * map from the center to outward in circular passes.
2629 * This will not result in a neat layout, but will be optimal
2630 * in the sense that there will not be holes left.
2632 done = 0;
2633 for (i = 1; i <= r && !done; i++) {
2634 int tx, ty;
2636 /* top and bottom parts of the ring */
2637 for (x = -i; x <= i && !done; x++) {
2638 tx = dock->x_pos + x*ICON_SIZE;
2639 y = -i;
2640 ty = dock->y_pos + y*ICON_SIZE;
2641 if (slot_map[XY2OFS(x,y)]==0
2642 && ON_SCREEN(tx, ty, sx, ex, sy, ey)) {
2643 *x_pos = x;
2644 *y_pos = y;
2645 done = 1;
2646 break;
2648 y = i;
2649 ty = dock->y_pos + y*ICON_SIZE;
2650 if (slot_map[XY2OFS(x,y)]==0
2651 && ON_SCREEN(tx, ty, sx, ex, sy, ey)) {
2652 *x_pos = x;
2653 *y_pos = y;
2654 done = 1;
2655 break;
2658 /* left and right parts of the ring */
2659 for (y = -i+1; y <= i-1; y++) {
2660 ty = dock->y_pos + y*ICON_SIZE;
2661 x = -i;
2662 tx = dock->x_pos + x*ICON_SIZE;
2663 if (slot_map[XY2OFS(x,y)]==0
2664 && ON_SCREEN(tx, ty, sx, ex, sy, ey)) {
2665 *x_pos = x;
2666 *y_pos = y;
2667 done = 1;
2668 break;
2670 x = i;
2671 tx = dock->x_pos + x*ICON_SIZE;
2672 if (slot_map[XY2OFS(x,y)]==0
2673 && ON_SCREEN(tx, ty, sx, ex, sy, ey)) {
2674 *x_pos = x;
2675 *y_pos = y;
2676 done = 1;
2677 break;
2681 free(slot_map);
2682 #undef XY2OFS
2683 return done;
2687 static void
2688 moveDock(WDock *dock, int new_x, int new_y)
2690 WAppIcon *btn;
2691 int i;
2693 dock->x_pos = new_x;
2694 dock->y_pos = new_y;
2695 for (i=0; i<dock->max_icons; i++) {
2696 btn = dock->icon_array[i];
2697 if (btn) {
2698 btn->x_pos = new_x + btn->xindex*ICON_SIZE;
2699 btn->y_pos = new_y + btn->yindex*ICON_SIZE;
2700 XMoveWindow(dpy, btn->icon->core->window, btn->x_pos, btn->y_pos);
2706 static void
2707 swapDock(WDock *dock)
2709 WScreen *scr = dock->screen_ptr;
2710 WAppIcon *btn;
2711 int x, i;
2714 if (dock->on_right_side) {
2715 x = dock->x_pos = scr->scr_width - ICON_SIZE - DOCK_EXTRA_SPACE;
2716 } else {
2717 x = dock->x_pos = DOCK_EXTRA_SPACE;
2720 for (i=0; i<dock->max_icons; i++) {
2721 btn = dock->icon_array[i];
2722 if (btn) {
2723 btn->x_pos = x;
2724 XMoveWindow(dpy, btn->icon->core->window, btn->x_pos, btn->y_pos);
2728 wScreenUpdateUsableArea(scr);
2732 static pid_t
2733 execCommand(WAppIcon *btn, char *command, WSavedState *state)
2735 WScreen *scr = btn->icon->core->screen_ptr;
2736 pid_t pid;
2737 char **argv;
2738 int argc;
2739 char *cmdline;
2741 cmdline = ExpandOptions(scr, command);
2743 if (scr->flags.dnd_data_convertion_status || !cmdline) {
2744 if (cmdline)
2745 free(cmdline);
2746 if (state)
2747 free(state);
2748 return 0;
2751 ParseCommand(cmdline, &argv, &argc);
2753 if (argv==NULL) {
2754 if (cmdline)
2755 free(cmdline);
2756 if (state)
2757 free(state);
2758 return 0;
2761 if ((pid=fork())==0) {
2762 char **args;
2763 int i;
2765 SetupEnvironment(scr);
2767 #ifdef HAVE_SETPGID
2768 setpgid(0, 0);
2769 #endif
2771 args = malloc(sizeof(char*)*(argc+1));
2772 if (!args)
2773 exit(111);
2774 for (i=0; i<argc; i++) {
2775 args[i] = argv[i];
2777 args[argc] = NULL;
2778 execvp(argv[0], args);
2779 exit(111);
2781 while (argc > 0)
2782 free(argv[--argc]);
2783 free(argv);
2785 if (pid > 0) {
2786 if (!state) {
2787 state = wmalloc(sizeof(WSavedState));
2788 memset(state, 0, sizeof(WSavedState));
2789 state->hidden = -1;
2790 state->miniaturized = -1;
2791 state->shaded = -1;
2792 if (btn->dock == scr->dock)
2793 state->workspace = -1;
2794 else
2795 state->workspace = scr->current_workspace;
2797 wWindowAddSavedState(btn->wm_instance, btn->wm_class, cmdline, pid,
2798 state);
2799 wAddDeathHandler(pid, (WDeathHandler*)trackDeadProcess,
2800 btn->dock);
2801 } else if (state) {
2802 free(state);
2804 free(cmdline);
2805 return pid;
2809 void
2810 wDockHideIcons(WDock *dock)
2812 int i;
2813 WAppIcon *btn;
2815 if (dock==NULL)
2816 return;
2818 btn = dock->icon_array[0];
2820 for (i=1; i<dock->max_icons; i++) {
2821 if (dock->icon_array[i])
2822 XUnmapWindow(dpy, dock->icon_array[i]->icon->core->window);
2824 dock->mapped = 0;
2826 dockIconPaint(btn);
2830 void
2831 wDockShowIcons(WDock *dock)
2833 int i, newlevel;
2834 WAppIcon *btn;
2836 if (dock==NULL)
2837 return;
2839 btn = dock->icon_array[0];
2840 moveDock(dock, btn->x_pos, btn->y_pos);
2842 newlevel = dock->lowered ? WMNormalLevel : WMDockLevel;
2843 ChangeStackingLevel(btn->icon->core, newlevel);
2845 for (i=1; i<dock->max_icons; i++) {
2846 if (dock->icon_array[i]) {
2847 MoveInStackListAbove(dock->icon_array[i]->icon->core,
2848 btn->icon->core);
2849 break;
2853 if (!dock->collapsed) {
2854 for (i=1; i<dock->max_icons; i++) {
2855 if (dock->icon_array[i]) {
2856 XMapWindow(dpy, dock->icon_array[i]->icon->core->window);
2860 dock->mapped = 1;
2862 dockIconPaint(btn);
2866 void
2867 wDockLower(WDock *dock)
2869 int i;
2871 for (i=0; i<dock->max_icons; i++) {
2872 if (dock->icon_array[i])
2873 wLowerFrame(dock->icon_array[i]->icon->core);
2878 void
2879 wDockRaise(WDock *dock)
2881 int i;
2883 for (i=dock->max_icons-1; i>=0; i--) {
2884 if (dock->icon_array[i])
2885 wRaiseFrame(dock->icon_array[i]->icon->core);
2890 void
2891 wDockRaiseLower(WDock *dock)
2893 if (!dock->icon_array[0]->icon->core->stacking->above
2894 ||(dock->icon_array[0]->icon->core->stacking->window_level
2895 !=dock->icon_array[0]->icon->core->stacking->above->stacking->window_level))
2896 wDockLower(dock);
2897 else
2898 wDockRaise(dock);
2902 void
2903 wDockFinishLaunch(WDock *dock, WAppIcon *icon)
2905 icon->launching = 0;
2906 icon->relaunching = 0;
2907 dockIconPaint(icon);
2911 WAppIcon*
2912 wDockFindIconFor(WDock *dock, Window window)
2914 WAppIcon *icon;
2915 int i;
2917 for (i=0; i<dock->max_icons; i++) {
2918 icon = dock->icon_array[i];
2919 if (icon && icon->main_window == window)
2920 return icon;
2922 return NULL;
2926 void
2927 wDockTrackWindowLaunch(WDock *dock, Window window)
2929 WAppIcon *icon;
2930 #ifdef REDUCE_APPICONS
2931 WAppIconAppList *tapplist;
2932 #endif
2933 char *wm_class, *wm_instance;
2934 int i;
2935 Bool firstPass = True;
2936 Bool found = False;
2937 char *command = NULL;
2940 int argc;
2941 char **argv;
2943 if (XGetCommand(dpy, window, &argv, &argc)) {
2944 if (argc > 0 && argv != NULL)
2945 command = FlattenStringList(argv,argc);
2946 if (argv) {
2947 XFreeStringList(argv);
2952 if (!PropGetWMClass(window, &wm_class, &wm_instance) ||
2953 (!wm_class && !wm_instance))
2954 return;
2956 retry:
2957 for (i=0; i<dock->max_icons; i++) {
2958 icon = dock->icon_array[i];
2959 if (!icon)
2960 continue;
2962 /* app is already attached to icon */
2963 if (icon->main_window == window) {
2964 found = True;
2965 break;
2968 if ((icon->wm_instance || icon->wm_class)
2969 && (icon->launching
2970 || (dock->screen_ptr->flags.startup && !icon->running))) {
2972 if (icon->wm_instance && wm_instance &&
2973 strcmp(icon->wm_instance, wm_instance)!=0) {
2974 continue;
2976 if (icon->wm_class && wm_class &&
2977 strcmp(icon->wm_class, wm_class)!=0) {
2978 continue;
2980 if (firstPass && command && strcmp(icon->command, command)!=0) {
2981 continue;
2984 if (!icon->relaunching) {
2985 WApplication *wapp;
2987 /* Possibly an application that was docked with dockit,
2988 * but the user did not update WMState to indicate that
2989 * it was docked by force */
2990 wapp = wApplicationOf(window);
2991 if (!wapp) {
2992 icon->forced_dock = 1;
2993 icon->running = 0;
2995 if (!icon->forced_dock)
2996 icon->main_window = window;
2998 #ifdef REDUCE_APPICONS
2999 tapplist = wmalloc(sizeof(WAppIconAppList));
3000 memset(tapplist, 0, sizeof(WAppIconAppList));
3001 tapplist->next = icon->applist;
3002 if (icon->applist)
3003 icon->applist->prev = tapplist;
3004 icon->applist = tapplist;
3005 tapplist->wapp = wApplicationOf(window);
3006 icon->num_apps++;
3007 #endif
3009 found = True;
3010 wDockFinishLaunch(dock, icon);
3011 break;
3015 if (firstPass && !found) {
3016 firstPass = False;
3017 goto retry;
3020 if (command)
3021 free(command);
3023 if (wm_class)
3024 XFree(wm_class);
3025 if (wm_instance)
3026 XFree(wm_instance);
3031 void
3032 wClipUpdateForWorkspaceChange(WScreen *scr, int workspace)
3034 if (!wPreferences.flags.noclip) {
3035 scr->clip_icon->dock = scr->workspaces[workspace]->clip;
3036 if (scr->current_workspace != workspace) {
3037 WDock *old_clip = scr->workspaces[scr->current_workspace]->clip;
3039 wDockHideIcons(old_clip);
3040 if (old_clip->auto_raise_lower) {
3041 if (old_clip->auto_raise_magic) {
3042 WMDeleteTimerHandler(old_clip->auto_raise_magic);
3043 old_clip->auto_raise_magic = NULL;
3045 wDockLower(old_clip);
3047 if (old_clip->auto_collapse) {
3048 if (old_clip->auto_expand_magic) {
3049 WMDeleteTimerHandler(old_clip->auto_expand_magic);
3050 old_clip->auto_expand_magic = NULL;
3052 old_clip->collapsed = 1;
3054 wDockShowIcons(scr->workspaces[workspace]->clip);
3056 if (scr->flags.clip_balloon_mapped)
3057 showClipBalloon(scr->clip_icon->dock, workspace);
3063 static void
3064 trackDeadProcess(pid_t pid, unsigned char status, WDock *dock)
3066 WAppIcon *icon;
3067 int i;
3069 for (i=0; i<dock->max_icons; i++) {
3070 icon = dock->icon_array[i];
3071 if (!icon)
3072 continue;
3074 if (icon->launching && icon->pid == pid) {
3075 if (!icon->relaunching) {
3076 icon->running = 0;
3077 icon->main_window = None;
3079 wDockFinishLaunch(dock, icon);
3080 icon->pid = 0;
3081 if (status==111) {
3082 char msg[PATH_MAX];
3083 #ifdef OFFIX_DND
3084 sprintf(msg, _("Could not execute command \"%s\""),
3085 icon->drop_launch && icon->dnd_command
3086 ? icon->dnd_command : icon->command);
3087 #else
3088 sprintf(msg, _("Could not execute command \"%s\""),
3089 icon->command);
3090 #endif
3091 wMessageDialog(dock->screen_ptr, _("Error"), msg,
3092 _("OK"), NULL, NULL);
3094 break;
3100 static void
3101 toggleLowered(WDock *dock)
3103 WAppIcon *tmp;
3104 int newlevel, i;
3106 /* lower/raise Dock */
3107 if (!dock->lowered) {
3108 newlevel = WMNormalLevel;
3109 dock->lowered = 1;
3110 } else {
3111 newlevel = WMDockLevel;
3112 dock->lowered = 0;
3115 for (i=0; i<dock->max_icons; i++) {
3116 tmp = dock->icon_array[i];
3117 if (!tmp)
3118 continue;
3120 ChangeStackingLevel(tmp->icon->core, newlevel);
3121 if (dock->lowered)
3122 wLowerFrame(tmp->icon->core);
3125 if (dock->type == WM_DOCK)
3126 wScreenUpdateUsableArea(dock->screen_ptr);
3130 static void
3131 toggleCollapsed(WDock *dock)
3133 if (dock->collapsed) {
3134 dock->collapsed = 0;
3135 wDockShowIcons(dock);
3137 else {
3138 dock->collapsed = 1;
3139 wDockHideIcons(dock);
3144 static void
3145 openDockMenu(WDock *dock, WAppIcon *aicon, XEvent *event)
3147 WScreen *scr = dock->screen_ptr;
3148 WObjDescriptor *desc;
3149 WMenuEntry *entry;
3150 WApplication *wapp = NULL;
3151 int index = 0;
3152 int x_pos;
3153 int appIsRunning = aicon->running && aicon->icon && aicon->icon->owner;
3155 if (dock->type == WM_DOCK) {
3156 /* keep on top */
3157 entry = dock->menu->entries[index];
3158 entry->flags.indicator_on = !dock->lowered;
3159 entry->clientdata = dock;
3160 } else {
3161 /* clip options */
3162 if (scr->clip_options)
3163 updateClipOptionsMenu(scr->clip_options, dock);
3165 /* Rename Workspace */
3166 entry = dock->menu->entries[++index];
3167 entry->clientdata = dock;
3169 /* select icon */
3170 entry = dock->menu->entries[++index];
3171 entry->clientdata = aicon;
3172 wMenuSetEnabled(dock->menu, index, aicon!=scr->clip_icon);
3174 /* (un)select all icons */
3175 entry = dock->menu->entries[++index];
3176 entry->clientdata = aicon;
3177 wMenuSetEnabled(dock->menu, index, dock->icon_count > 1);
3179 /* keep icon(s) */
3180 entry = dock->menu->entries[++index];
3181 entry->clientdata = aicon;
3182 wMenuSetEnabled(dock->menu, index, dock->icon_count > 1);
3184 /* this is the workspace submenu part */
3185 if (scr->clip_submenu)
3186 updateWorkspaceMenu(scr->clip_submenu, aicon);
3187 index++;
3189 /* remove icon(s) */
3190 entry = dock->menu->entries[++index];
3191 entry->clientdata = aicon;
3192 wMenuSetEnabled(dock->menu, index, dock->icon_count > 1);
3194 /* attract icon(s) */
3195 entry = dock->menu->entries[++index];
3196 entry->clientdata = aicon;
3199 /* launch */
3200 entry = dock->menu->entries[++index];
3201 entry->clientdata = aicon;
3202 wMenuSetEnabled(dock->menu, index, aicon->command!=NULL);
3204 /* unhide here */
3205 entry = dock->menu->entries[++index];
3206 entry->clientdata = aicon;
3207 wMenuSetEnabled(dock->menu, index, appIsRunning);
3209 /* hide */
3210 entry = dock->menu->entries[++index];
3211 entry->clientdata = aicon;
3212 if (aicon->icon->owner) {
3213 wapp = wApplicationOf(aicon->icon->owner->main_window);
3214 if (wapp && wapp->flags.hidden)
3215 entry->text = _("Unhide");
3216 else
3217 entry->text = _("Hide");
3218 } else {
3219 entry->text = _("Hide");
3221 wMenuSetEnabled(dock->menu, index, appIsRunning);
3223 /* settings */
3224 entry = dock->menu->entries[++index];
3225 entry->clientdata = aicon;
3226 wMenuSetEnabled(dock->menu, index, !aicon->editing
3227 && !wPreferences.flags.noupdates);
3229 /* kill */
3230 entry = dock->menu->entries[++index];
3231 entry->clientdata = aicon;
3232 wMenuSetEnabled(dock->menu, index, appIsRunning);
3234 if (!dock->menu->flags.realized)
3235 wMenuRealize(dock->menu);
3237 if (dock->type == WM_CLIP) {
3238 x_pos = event->xbutton.x_root+2;
3239 } else {
3240 x_pos = dock->on_right_side ?
3241 scr->scr_width - dock->menu->frame->core->width - 2 : 0;
3244 wMenuMapAt(dock->menu, x_pos, event->xbutton.y_root+2, False);
3246 /* allow drag select */
3247 event->xany.send_event = True;
3248 desc = &dock->menu->menu->descriptor;
3249 (*desc->handle_mousedown)(desc, event);
3253 static void
3254 openClipWorkspaceMenu(WScreen *scr, int x, int y)
3256 if (!scr->clip_ws_menu) {
3257 scr->clip_ws_menu = wWorkspaceMenuMake(scr, False);
3259 wWorkspaceMenuUpdate(scr, scr->clip_ws_menu);
3260 wMenuMapAt(scr->clip_ws_menu, x, y, False);
3264 /******************************************************************/
3265 static void
3266 iconDblClick(WObjDescriptor *desc, XEvent *event)
3268 WAppIcon *btn = desc->parent;
3269 WDock *dock = btn->dock;
3270 WApplication *wapp = NULL;
3271 int unhideHere = 0;
3273 #ifdef REDUCE_APPICONS
3274 if ((btn->icon->owner && !(event->xbutton.state & ControlMask)) ||
3275 ((btn->icon->owner == NULL) && (btn->applist != NULL))) {
3276 if (btn->icon->owner == NULL)
3277 btn->icon->owner = btn->applist->wapp->main_window_desc;
3278 #ifdef I_HATE_THIS
3280 #endif
3281 #else
3282 if (btn->icon->owner && !(event->xbutton.state & ControlMask)) {
3283 #endif
3284 wapp = wApplicationOf(btn->icon->owner->main_window);
3286 assert(wapp!=NULL);
3288 unhideHere = (event->xbutton.state & ShiftMask);
3290 /* go to the last workspace that the user worked on the app */
3291 if (!unhideHere) {
3292 wWorkspaceChange(dock->screen_ptr, wapp->last_workspace);
3295 wUnhideApplication(wapp, event->xbutton.button==Button2,
3296 unhideHere);
3298 if (event->xbutton.state & MOD_MASK) {
3299 wHideOtherApplications(btn->icon->owner);
3301 } else {
3302 if (event->xbutton.button==Button1) {
3304 if (event->xbutton.state & MOD_MASK) {
3305 /* raise/lower dock */
3306 toggleLowered(dock);
3307 } else if (btn == dock->screen_ptr->clip_icon) {
3308 if (getClipButton(event->xbutton.x, event->xbutton.y)==CLIP_IDLE)
3309 toggleCollapsed(dock);
3310 else
3311 handleClipChangeWorkspace(dock->screen_ptr, event);
3312 } else if (btn->command) {
3313 if (!btn->launching &&
3314 (!btn->running || (event->xbutton.state & ControlMask))) {
3315 launchDockedApplication(btn);
3323 static void
3324 handleDockMove(WDock *dock, WAppIcon *aicon, XEvent *event)
3326 WScreen *scr = dock->screen_ptr;
3327 int ofs_x=event->xbutton.x, ofs_y=event->xbutton.y;
3328 int x, y;
3329 XEvent ev;
3330 int grabbed = 0, swapped = 0, done;
3331 Pixmap ghost = None;
3332 int superfluous = wPreferences.superfluous; /* we catch it to avoid problems */
3334 #ifdef DEBUG
3335 puts("moving dock");
3336 #endif
3337 if (XGrabPointer(dpy, aicon->icon->core->window, True, ButtonMotionMask
3338 |ButtonReleaseMask|ButtonPressMask, GrabModeAsync,
3339 GrabModeAsync, None, None, CurrentTime) !=GrabSuccess) {
3340 wwarning("pointer grab failed for dock move");
3342 y = 0;
3343 for (x=0; x<dock->max_icons; x++) {
3344 if (dock->icon_array[x]!=NULL &&
3345 dock->icon_array[x]->yindex > y)
3346 y = dock->icon_array[x]->yindex;
3348 y++;
3349 XResizeWindow(dpy, scr->dock_shadow, ICON_SIZE, ICON_SIZE*y);
3351 done = 0;
3352 while (!done) {
3353 WMMaskEvent(dpy, PointerMotionMask|ButtonReleaseMask|ButtonPressMask
3354 |ButtonMotionMask|ExposureMask, &ev);
3355 switch (ev.type) {
3356 case Expose:
3357 WMHandleEvent(&ev);
3358 break;
3360 case MotionNotify:
3361 if (!grabbed) {
3362 if (abs(ofs_x-ev.xmotion.x)>=MOVE_THRESHOLD
3363 || abs(ofs_y-ev.xmotion.y)>=MOVE_THRESHOLD) {
3364 XChangeActivePointerGrab(dpy, ButtonMotionMask
3365 |ButtonReleaseMask|ButtonPressMask,
3366 wCursor[WCUR_MOVE], CurrentTime);
3367 grabbed=1;
3369 break;
3371 if (dock->type == WM_CLIP) {
3372 if (ev.xmotion.x_root - ofs_x < 0) {
3373 x = 0;
3374 } else if (ev.xmotion.x_root - ofs_x + ICON_SIZE >
3375 scr->scr_width) {
3376 x = scr->scr_width - ICON_SIZE;
3377 } else {
3378 x = ev.xmotion.x_root - ofs_x;
3380 if (ev.xmotion.y_root - ofs_y < 0) {
3381 y = 0;
3382 } else if (ev.xmotion.y_root - ofs_y + ICON_SIZE >
3383 scr->scr_height) {
3384 y = scr->scr_height - ICON_SIZE;
3385 } else {
3386 y = ev.xmotion.y_root - ofs_y;
3388 moveDock(dock, x, y);
3389 } else {
3390 /* move vertically if pointer is inside the dock*/
3391 if ((dock->on_right_side &&
3392 ev.xmotion.x_root >= dock->x_pos - ICON_SIZE)
3393 || (!dock->on_right_side &&
3394 ev.xmotion.x_root <= dock->x_pos + ICON_SIZE*2)) {
3396 if (ev.xmotion.y_root - ofs_y < 0) {
3397 y = 0;
3398 } else if (ev.xmotion.y_root - ofs_y + ICON_SIZE >
3399 scr->scr_height) {
3400 y = scr->scr_height - ICON_SIZE;
3401 } else {
3402 y = ev.xmotion.y_root - ofs_y;
3404 moveDock(dock, dock->x_pos, y);
3406 /* move horizontally to change sides */
3407 x = ev.xmotion.x_root - ofs_x;
3408 if (!dock->on_right_side) {
3410 /* is on left */
3412 if (ev.xmotion.x_root > dock->x_pos + ICON_SIZE*2) {
3413 XMoveWindow(dpy, scr->dock_shadow, scr->scr_width-ICON_SIZE
3414 -DOCK_EXTRA_SPACE-1, dock->y_pos);
3415 if (superfluous) {
3416 if (ghost==None) {
3417 ghost = MakeGhostDock(dock, dock->x_pos,
3418 scr->scr_width-ICON_SIZE
3419 -DOCK_EXTRA_SPACE-1,
3420 dock->y_pos);
3421 XSetWindowBackgroundPixmap(dpy, scr->dock_shadow,
3422 ghost);
3423 XClearWindow(dpy, scr->dock_shadow);
3426 XMapRaised(dpy, scr->dock_shadow);
3427 swapped = 1;
3428 } else {
3429 if (superfluous && ghost!=None) {
3430 XFreePixmap(dpy, ghost);
3431 ghost = None;
3433 XUnmapWindow(dpy, scr->dock_shadow);
3434 swapped = 0;
3436 } else {
3437 /* is on right */
3438 if (ev.xmotion.x_root < dock->x_pos - ICON_SIZE) {
3439 XMoveWindow(dpy, scr->dock_shadow,
3440 DOCK_EXTRA_SPACE, dock->y_pos);
3441 if (superfluous) {
3442 if (ghost==None) {
3443 ghost = MakeGhostDock(dock, dock->x_pos,
3444 DOCK_EXTRA_SPACE, dock->y_pos);
3445 XSetWindowBackgroundPixmap(dpy, scr->dock_shadow,
3446 ghost);
3447 XClearWindow(dpy, scr->dock_shadow);
3450 XMapRaised(dpy, scr->dock_shadow);
3451 swapped = -1;
3452 } else {
3453 XUnmapWindow(dpy, scr->dock_shadow);
3454 swapped = 0;
3455 if (superfluous && ghost!=None) {
3456 XFreePixmap(dpy, ghost);
3457 ghost = None;
3462 break;
3464 case ButtonPress:
3465 break;
3467 case ButtonRelease:
3468 if (ev.xbutton.button != event->xbutton.button)
3469 break;
3470 XUngrabPointer(dpy, CurrentTime);
3471 XUnmapWindow(dpy, scr->dock_shadow);
3472 XResizeWindow(dpy, scr->dock_shadow, ICON_SIZE, ICON_SIZE);
3473 if (dock->type == WM_DOCK) {
3474 if (swapped!=0) {
3475 if (swapped>0)
3476 dock->on_right_side = 1;
3477 else
3478 dock->on_right_side = 0;
3479 swapDock(dock);
3480 wArrangeIcons(scr, False);
3483 done = 1;
3484 break;
3487 if (superfluous) {
3488 if (ghost!=None)
3489 XFreePixmap(dpy, ghost);
3490 XSetWindowBackground(dpy, scr->dock_shadow, scr->white_pixel);
3492 #ifdef DEBUG
3493 puts("End dock move");
3494 #endif
3499 static void
3500 handleIconMove(WDock *dock, WAppIcon *aicon, XEvent *event)
3502 WScreen *scr = dock->screen_ptr;
3503 Window wins[2];
3504 WIcon *icon = aicon->icon;
3505 WDock *dock2 = NULL, *last_dock = dock, *clip = NULL;
3506 int ondock, grabbed = 0, change_dock = 0, collapsed = 0;
3507 XEvent ev;
3508 int x = aicon->x_pos, y = aicon->y_pos;
3509 int ofs_x = event->xbutton.x, ofs_y = event->xbutton.y;
3510 int shad_x = x, shad_y = y;
3511 int ix = aicon->xindex, iy = aicon->yindex;
3512 int tmp;
3513 Pixmap ghost = None;
3514 Bool docked;
3515 int superfluous = wPreferences.superfluous; /* we catch it to avoid problems */
3517 if (wPreferences.flags.noupdates)
3518 return;
3520 if (XGrabPointer(dpy, icon->core->window, True, ButtonMotionMask
3521 |ButtonReleaseMask|ButtonPressMask, GrabModeAsync,
3522 GrabModeAsync, None, None, CurrentTime) !=GrabSuccess) {
3523 #ifdef DEBUG0
3524 wwarning("pointer grab failed for icon move");
3525 #endif
3528 wRaiseFrame(icon->core);
3530 if (!wPreferences.flags.noclip)
3531 clip = scr->workspaces[scr->current_workspace]->clip;
3533 if (dock == scr->dock && !wPreferences.flags.noclip)
3534 dock2 = clip;
3535 else if (dock != scr->dock && !wPreferences.flags.nodock)
3536 dock2 = scr->dock;
3538 wins[0] = icon->core->window;
3539 wins[1] = scr->dock_shadow;
3540 XRestackWindows(dpy, wins, 2);
3541 XMoveResizeWindow(dpy, scr->dock_shadow, aicon->x_pos, aicon->y_pos,
3542 ICON_SIZE, ICON_SIZE);
3543 if (superfluous) {
3544 if (icon->pixmap!=None)
3545 ghost = MakeGhostIcon(scr, icon->pixmap);
3546 else
3547 ghost = MakeGhostIcon(scr, icon->core->window);
3549 XSetWindowBackgroundPixmap(dpy, scr->dock_shadow, ghost);
3550 XClearWindow(dpy, scr->dock_shadow);
3552 XMapWindow(dpy, scr->dock_shadow);
3554 ondock = 1;
3557 while(1) {
3558 XMaskEvent(dpy, PointerMotionMask|ButtonReleaseMask|ButtonPressMask
3559 |ButtonMotionMask|ExposureMask, &ev);
3560 switch (ev.type) {
3561 case Expose:
3562 WMHandleEvent(&ev);
3563 break;
3565 case MotionNotify:
3566 if (!grabbed) {
3567 if (abs(ofs_x-ev.xmotion.x)>=MOVE_THRESHOLD
3568 || abs(ofs_y-ev.xmotion.y)>=MOVE_THRESHOLD) {
3569 XChangeActivePointerGrab(dpy, ButtonMotionMask
3570 |ButtonReleaseMask|ButtonPressMask,
3571 wCursor[WCUR_MOVE], CurrentTime);
3572 grabbed=1;
3573 } else {
3574 break;
3578 x = ev.xmotion.x_root - ofs_x;
3579 y = ev.xmotion.y_root - ofs_y;
3580 tmp = wDockSnapIcon(dock, aicon, x, y, &ix, &iy, True);
3581 if (tmp && dock2) {
3582 change_dock = 0;
3583 if (last_dock != dock && collapsed) {
3584 last_dock->collapsed = 1;
3585 wDockHideIcons(last_dock);
3586 collapsed = 0;
3588 if (!collapsed && (collapsed = dock->collapsed)) {
3589 dock->collapsed = 0;
3590 wDockShowIcons(dock);
3592 if (dock->auto_raise_lower)
3593 wDockRaise(dock);
3594 last_dock = dock;
3596 else if (dock2) {
3597 tmp = wDockSnapIcon(dock2, aicon, x, y, &ix, &iy, False);
3598 if (tmp) {
3599 change_dock = 1;
3600 if (last_dock != dock2 && collapsed) {
3601 last_dock->collapsed = 1;
3602 wDockHideIcons(last_dock);
3603 collapsed = 0;
3605 if (!collapsed && (collapsed = dock2->collapsed)) {
3606 dock2->collapsed = 0;
3607 wDockShowIcons(dock2);
3609 if (dock2->auto_raise_lower)
3610 wDockRaise(dock2);
3611 last_dock = dock2;
3614 if (aicon->launching
3615 || (aicon->running && !(ev.xmotion.state & MOD_MASK))
3616 || (!aicon->running && tmp)) {
3617 shad_x = last_dock->x_pos + ix*wPreferences.icon_size;
3618 shad_y = last_dock->y_pos + iy*wPreferences.icon_size;
3620 XMoveWindow(dpy, scr->dock_shadow, shad_x, shad_y);
3622 if (!ondock) {
3623 XMapWindow(dpy, scr->dock_shadow);
3624 #if 0
3625 if (!collapsed && (collapsed = last_dock->collapsed)) {
3626 last_dock->collapsed = 0;
3627 wDockShowIcons(last_dock);
3629 #endif
3631 ondock = 1;
3632 } else {
3633 if (ondock) {
3634 XUnmapWindow(dpy, scr->dock_shadow);
3635 #if 0
3636 if (last_dock && collapsed &&
3637 aicon->running && (ev.xmotion.state & MOD_MASK)) {
3638 last_dock->collapsed = 1;
3639 wDockHideIcons(last_dock);
3640 collapsed = 0;
3642 #endif
3644 ondock = 0;
3646 XMoveWindow(dpy, icon->core->window, x, y);
3647 break;
3649 case ButtonPress:
3650 break;
3652 case ButtonRelease:
3653 if (ev.xbutton.button != event->xbutton.button)
3654 break;
3655 XUngrabPointer(dpy, CurrentTime);
3656 if (ondock) {
3657 SlideWindow(icon->core->window, x, y, shad_x, shad_y);
3658 XUnmapWindow(dpy, scr->dock_shadow);
3659 if (!change_dock) {
3660 reattachIcon(dock, aicon, ix, iy);
3661 if (clip && dock!=clip && clip->auto_raise_lower)
3662 wDockLower(clip);
3663 } else {
3664 docked = moveIconBetweenDocks(dock, dock2, aicon, ix, iy);
3665 if (!docked) {
3666 /* Slide it back if dock rejected it */
3667 SlideWindow(icon->core->window, x, y, aicon->x_pos,
3668 aicon->y_pos);
3669 reattachIcon(dock, aicon, aicon->xindex,aicon->yindex);
3671 if (last_dock->type==WM_CLIP && last_dock->auto_collapse) {
3672 collapsed = 0;
3675 } else {
3676 aicon->x_pos = x;
3677 aicon->y_pos = y;
3678 if (superfluous) {
3679 if (!aicon->running && !wPreferences.no_animations) {
3680 /* We need to deselect it, even if is deselected in
3681 * wDockDetach(), because else DoKaboom() will fail.
3683 if (aicon->icon->selected)
3684 wIconSelect(aicon->icon);
3685 DoKaboom(scr,aicon->icon->core->window, x, y);
3688 if (clip && clip->auto_raise_lower)
3689 wDockLower(clip);
3690 wDockDetach(dock, aicon);
3692 if (collapsed) {
3693 last_dock->collapsed = 1;
3694 wDockHideIcons(last_dock);
3695 collapsed = 0;
3697 if (superfluous) {
3698 if (ghost!=None)
3699 XFreePixmap(dpy, ghost);
3700 XSetWindowBackground(dpy, scr->dock_shadow, scr->white_pixel);
3702 #ifdef DEBUG
3703 puts("End icon move");
3704 #endif
3705 return;
3711 static int
3712 getClipButton(int px, int py)
3714 int pt = (CLIP_BUTTON_SIZE+2)*ICON_SIZE/64;
3716 if (px < 0 || py < 0 || px >= ICON_SIZE || py >= ICON_SIZE)
3717 return CLIP_IDLE;
3719 if (py <= pt-((int)ICON_SIZE-1-px))
3720 return CLIP_FORWARD;
3721 else if (px <= pt-((int)ICON_SIZE-1-py))
3722 return CLIP_REWIND;
3724 return CLIP_IDLE;
3728 static void
3729 handleClipChangeWorkspace(WScreen *scr, XEvent *event)
3731 XEvent ev;
3732 int done, direction, new_ws;
3733 int new_dir;
3734 WDock *clip = scr->clip_icon->dock;
3736 direction = getClipButton(event->xbutton.x, event->xbutton.y);
3738 clip->lclip_button_pushed = direction==CLIP_REWIND;
3739 clip->rclip_button_pushed = direction==CLIP_FORWARD;
3741 wClipIconPaint(scr->clip_icon);
3742 done = 0;
3743 while(!done) {
3744 WMMaskEvent(dpy, ExposureMask|ButtonMotionMask|ButtonReleaseMask
3745 |ButtonPressMask, &ev);
3746 switch (ev.type) {
3747 case Expose:
3748 WMHandleEvent(&ev);
3749 break;
3751 case MotionNotify:
3752 new_dir = getClipButton(ev.xmotion.x, ev.xmotion.y);
3753 if (new_dir != direction) {
3754 direction = new_dir;
3755 clip->lclip_button_pushed = direction==CLIP_REWIND;
3756 clip->rclip_button_pushed = direction==CLIP_FORWARD;
3757 wClipIconPaint(scr->clip_icon);
3759 break;
3761 case ButtonPress:
3762 break;
3764 case ButtonRelease:
3765 if (ev.xbutton.button == event->xbutton.button)
3766 done = 1;
3770 clip->lclip_button_pushed = 0;
3771 clip->rclip_button_pushed = 0;
3773 new_ws = wPreferences.ws_advance || (event->xbutton.state & ControlMask);
3775 if (direction == CLIP_FORWARD) {
3776 if (scr->current_workspace < scr->workspace_count-1)
3777 wWorkspaceChange(scr, scr->current_workspace+1);
3778 else if (new_ws && scr->current_workspace < MAX_WORKSPACES-1)
3779 wWorkspaceChange(scr, scr->current_workspace+1);
3780 else if (wPreferences.ws_cycle)
3781 wWorkspaceChange(scr, 0);
3783 else if (direction == CLIP_REWIND) {
3784 if (scr->current_workspace > 0)
3785 wWorkspaceChange(scr, scr->current_workspace-1);
3786 else if (scr->current_workspace==0 && wPreferences.ws_cycle)
3787 wWorkspaceChange(scr, scr->workspace_count-1);
3790 wClipIconPaint(scr->clip_icon);
3794 static void
3795 iconMouseDown(WObjDescriptor *desc, XEvent *event)
3797 WAppIcon *aicon = desc->parent;
3798 WDock *dock = aicon->dock;
3799 WScreen *scr = aicon->icon->core->screen_ptr;
3801 if (aicon->editing || WCHECK_STATE(WSTATE_MODAL))
3802 return;
3804 scr->last_dock = dock;
3806 if (dock->menu->flags.mapped)
3807 wMenuUnmap(dock->menu);
3809 if (IsDoubleClick(scr, event)) {
3810 /* double-click was not in the main clip icon */
3811 if (dock->type != WM_CLIP || aicon->xindex!=0 || aicon->yindex!=0
3812 || getClipButton(event->xbutton.x, event->xbutton.y)==CLIP_IDLE) {
3813 iconDblClick(desc, event);
3814 return;
3818 if (dock->type == WM_CLIP && scr->flags.clip_balloon_mapped) {
3819 XUnmapWindow(dpy, scr->clip_balloon);
3820 scr->flags.clip_balloon_mapped = 0;
3823 #ifdef DEBUG
3824 puts("handling dock");
3825 #endif
3826 if (event->xbutton.button == Button1) {
3827 if (event->xbutton.state & MOD_MASK)
3828 wDockLower(dock);
3829 else
3830 wDockRaise(dock);
3832 if ((event->xbutton.state & ShiftMask) && aicon!=scr->clip_icon &&
3833 dock->type!=WM_DOCK) {
3834 wIconSelect(aicon->icon);
3835 return;
3838 if (aicon->yindex==0 && aicon->xindex==0) {
3839 if (getClipButton(event->xbutton.x, event->xbutton.y)!=CLIP_IDLE
3840 && dock->type==WM_CLIP)
3841 handleClipChangeWorkspace(scr, event);
3842 else
3843 handleDockMove(dock, aicon, event);
3844 } else
3845 handleIconMove(dock, aicon, event);
3847 } else if (event->xbutton.button==Button2 && dock->type==WM_CLIP &&
3848 aicon->xindex==0 && aicon->yindex==0) {
3849 openClipWorkspaceMenu(scr, event->xbutton.x_root+2,
3850 event->xbutton.y_root+2);
3851 if (scr->clip_ws_menu) {
3852 WMenu *menu;
3853 menu = scr->clip_ws_menu;
3854 desc = &menu->menu->descriptor;
3856 event->xany.send_event = True;
3857 (*desc->handle_mousedown)(desc, event);
3859 } else if (event->xbutton.button == Button3) {
3860 openDockMenu(dock, aicon, event);
3865 static void
3866 showClipBalloon(WDock *dock, int workspace)
3868 int w, h;
3869 int x, y;
3870 WScreen *scr = dock->screen_ptr;
3871 char *text;
3872 Window stack[2];
3874 scr->flags.clip_balloon_mapped = 1;
3875 XMapWindow(dpy, scr->clip_balloon);
3877 text = scr->workspaces[workspace]->name;
3879 w = wTextWidth(scr->clip_title_font->font, text, strlen(text));
3881 h = scr->clip_title_font->height;
3882 XResizeWindow(dpy, scr->clip_balloon, w, h);
3884 x = dock->x_pos + CLIP_BUTTON_SIZE*ICON_SIZE/64;
3885 y = dock->y_pos + ICON_SIZE-scr->clip_title_font->height - 3;
3887 if (x+w > scr->scr_width) {
3888 x = scr->scr_width - w;
3889 if (dock->y_pos + ICON_SIZE + h > scr->scr_height)
3890 y = dock->y_pos - h - 1;
3891 else
3892 y = dock->y_pos + ICON_SIZE;
3893 XRaiseWindow(dpy, scr->clip_balloon);
3894 } else {
3895 stack[0] = scr->clip_icon->icon->core->window;
3896 stack[1] = scr->clip_balloon;
3897 XRestackWindows(dpy, stack, 2);
3899 XMoveWindow(dpy, scr->clip_balloon, x, y);
3900 XSetForeground(dpy, scr->clip_title_gc,
3901 scr->clip_title_pixel[CLIP_NORMAL]);
3902 XClearWindow(dpy, scr->clip_balloon);
3903 wDrawString(scr->clip_balloon, scr->clip_title_font, scr->clip_title_gc,
3904 0, scr->clip_title_font->y, text, strlen(text));
3908 static void
3909 clipEnterNotify(WObjDescriptor *desc, XEvent *event)
3911 WAppIcon *btn = (WAppIcon*)desc->parent;
3912 WDock *dock;
3914 assert(event->type==EnterNotify);
3916 if(desc->parent_type!=WCLASS_DOCK_ICON)
3917 return;
3919 dock = btn->dock;
3920 if (!dock || dock->type!=WM_CLIP)
3921 return;
3923 /* The auto raise/lower code */
3924 if (dock->auto_lower_magic) {
3925 WMDeleteTimerHandler(dock->auto_lower_magic);
3926 dock->auto_lower_magic = NULL;
3928 if (dock->auto_raise_lower && !dock->auto_raise_magic) {
3929 dock->auto_raise_magic = WMAddTimerHandler(AUTO_RAISE_DELAY,
3930 clipAutoRaise,
3931 (void *)dock);
3934 /* The auto expand/collapse code */
3935 if (dock->auto_collapse_magic) {
3936 WMDeleteTimerHandler(dock->auto_collapse_magic);
3937 dock->auto_collapse_magic = NULL;
3939 if (dock->auto_collapse && !dock->auto_expand_magic) {
3940 dock->auto_expand_magic = WMAddTimerHandler(AUTO_EXPAND_DELAY,
3941 clipAutoExpand,
3942 (void *)dock);
3945 if (btn->xindex == 0 && btn->yindex == 0)
3946 showClipBalloon(dock, dock->screen_ptr->current_workspace);
3947 else {
3948 if (dock->screen_ptr->flags.clip_balloon_mapped) {
3949 XUnmapWindow(dpy, dock->screen_ptr->clip_balloon);
3950 dock->screen_ptr->flags.clip_balloon_mapped = 0;
3956 static void
3957 clipLeave(WDock *dock)
3959 XEvent event;
3960 WObjDescriptor *desc = NULL;
3962 if (!dock || dock->type!=WM_CLIP)
3963 return;
3965 if (XCheckTypedEvent(dpy, EnterNotify, &event)!=False) {
3966 if (XFindContext(dpy, event.xcrossing.window, wWinContext,
3967 (XPointer *)&desc)!=XCNOENT
3968 && desc && desc->parent_type==WCLASS_DOCK_ICON
3969 && ((WAppIcon*)desc->parent)->dock
3970 && ((WAppIcon*)desc->parent)->dock->type==WM_CLIP) {
3971 /* We didn't left the Clip yet */
3972 XPutBackEvent(dpy, &event);
3973 return;
3976 XPutBackEvent(dpy, &event);
3977 } else {
3978 /* We entered a withdrawn window, so we're still in Clip */
3979 return;
3982 if (dock->auto_raise_magic) {
3983 WMDeleteTimerHandler(dock->auto_raise_magic);
3984 dock->auto_raise_magic = NULL;
3986 if (dock->auto_raise_lower && !dock->auto_lower_magic) {
3987 dock->auto_lower_magic = WMAddTimerHandler(AUTO_LOWER_DELAY,
3988 clipAutoLower,
3989 (void *)dock);
3992 if (dock->auto_expand_magic) {
3993 WMDeleteTimerHandler(dock->auto_expand_magic);
3994 dock->auto_expand_magic = NULL;
3996 if (dock->auto_collapse && !dock->auto_collapse_magic) {
3997 dock->auto_collapse_magic = WMAddTimerHandler(AUTO_COLLAPSE_DELAY,
3998 clipAutoCollapse,
3999 (void *)dock);
4004 static void
4005 clipLeaveNotify(WObjDescriptor *desc, XEvent *event)
4007 WAppIcon *btn = (WAppIcon*)desc->parent;
4009 assert(event->type==LeaveNotify);
4011 if(desc->parent_type!=WCLASS_DOCK_ICON)
4012 return;
4014 clipLeave(btn->dock);
4018 static void
4019 clipAutoCollapse(void *cdata)
4021 WDock *dock = (WDock *)cdata;
4023 if (dock->type!=WM_CLIP)
4024 return;
4026 if (dock->auto_collapse) {
4027 dock->collapsed = 1;
4028 wDockHideIcons(dock);
4030 dock->auto_collapse_magic = NULL;
4034 static void
4035 clipAutoExpand(void *cdata)
4037 WDock *dock = (WDock *)cdata;
4039 if (dock->type!=WM_CLIP)
4040 return;
4042 if (dock->auto_collapse) {
4043 dock->collapsed = 0;
4044 wDockShowIcons(dock);
4046 dock->auto_expand_magic = NULL;
4050 static void
4051 clipAutoLower(void *cdata)
4053 WDock *dock = (WDock *)cdata;
4055 if (dock->type!=WM_CLIP)
4056 return;
4058 if (dock->auto_raise_lower)
4059 wDockLower(dock);
4061 dock->auto_lower_magic = NULL;
4065 static void
4066 clipAutoRaise(void *cdata)
4068 WDock *dock = (WDock *)cdata;
4070 if (dock->type!=WM_CLIP)
4071 return;
4073 if (dock->auto_raise_lower)
4074 wDockRaise(dock);
4076 if (dock->screen_ptr->flags.clip_balloon_mapped) {
4077 showClipBalloon(dock, dock->screen_ptr->current_workspace);
4080 dock->auto_raise_magic = NULL;