Fixed Clip regarding auto raise/lower and autocollapse behavior.
[wmaker-crm.git] / src / dock.c
blob3df19350edf7300197fee807060e895feafcd631
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 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
20 * USA.
24 #include "wconfig.h"
26 #include <X11/Xlib.h>
27 #include <X11/Xutil.h>
28 #include <stdlib.h>
29 #include <string.h>
30 #include <unistd.h>
31 #include <math.h>
32 #include <limits.h>
34 #ifndef PATH_MAX
35 #define PATH_MAX DEFAULT_PATH_MAX
36 #endif
38 #include "WindowMaker.h"
39 #include "wcore.h"
40 #include "window.h"
41 #include "icon.h"
42 #include "appicon.h"
43 #include "actions.h"
44 #include "stacking.h"
45 #include "dock.h"
46 #include "dialog.h"
47 #include "funcs.h"
48 #include "properties.h"
49 #include "menu.h"
50 #include "client.h"
51 #include "defaults.h"
52 #include "workspace.h"
53 #include "framewin.h"
54 #include "superfluous.h"
56 #include "list.h"
58 #ifdef WMSOUND
59 #include "wmsound.h"
60 #endif
62 #include <proplist.h>
67 /**** Local variables ****/
68 #define CLIP_REWIND 1
69 #define CLIP_IDLE 0
70 #define CLIP_FORWARD 2
72 #define CLIP_BUTTON_SIZE 23
75 /**** Global variables ****/
77 /* in dockedapp.c */
78 extern void DestroyDockAppSettingsPanel();
80 extern void ShowDockAppSettingsPanel(WAppIcon *aicon);
83 extern XContext wWinContext;
85 extern Cursor wCursor[WCUR_LAST];
87 extern WPreferences wPreferences;
89 extern XContext wWinContext;
91 #ifdef OFFIX_DND
92 extern Atom _XA_DND_PROTOCOL;
93 #endif
96 #define MOD_MASK wPreferences.modifier_mask
98 extern void appIconMouseDown(WObjDescriptor *desc, XEvent *event);
100 #define ICON_SIZE wPreferences.icon_size
103 /***** Local variables ****/
105 static proplist_t dCommand=NULL;
106 #ifdef OFFIX_DND
107 static proplist_t dDropCommand=NULL;
108 #endif
109 static proplist_t dAutoLaunch, dName, dForced, dBuggyApplication, dYes, dNo;
110 static proplist_t dHost, dDock, dClip;
111 static proplist_t dAutoAttractIcons, dKeepAttracted;
113 static proplist_t dPosition, dApplications, dLowered, dCollapsed, dAutoCollapse;
115 static proplist_t dAutoRaiseLower;
117 static void dockIconPaint(WAppIcon *btn);
119 static void iconMouseDown(WObjDescriptor *desc, XEvent *event);
121 static pid_t execCommand(WAppIcon *btn, char *command, WSavedState *state);
123 static void trackDeadProcess(pid_t pid, unsigned char status, WDock *dock);
125 static int getClipButton(int px, int py);
127 static void toggleLowered(WDock *dock);
129 static void toggleCollapsed(WDock *dock);
131 static void clipIconExpose(WObjDescriptor *desc, XEvent *event);
133 static void clipLeave(WDock *dock);
135 static void handleClipChangeWorkspace(WScreen *scr, XEvent *event);
137 Bool moveIconBetweenDocks(WDock *src, WDock *dest, WAppIcon *icon, int x, int y);
139 static void clipEnterNotify(WObjDescriptor *desc, XEvent *event);
140 static void clipLeaveNotify(WObjDescriptor *desc, XEvent *event);
141 static void clipAutoCollapse(void *cdata);
142 static void clipAutoExpand(void *cdata);
143 static void launchDockedApplication(WAppIcon *btn);
145 static void clipAutoLower(void *cdata);
146 static void clipAutoRaise(void *cdata);
148 static void showClipBalloon(WDock *dock, int workspace);
150 #ifdef OFFIX_DND
152 #define DndNotDnd -1
153 #define DndUnknown 0
154 #define DndRawData 1
155 #define DndFile 2
156 #define DndFiles 3
157 #define DndText 4
158 #define DndDir 5
159 #define DndLink 6
160 #define DndExe 7
162 #define DndEND 8
164 #endif /* OFFIX_DND */
168 static void
169 make_keys()
171 if (dCommand!=NULL)
172 return;
174 dCommand = PLRetain(PLMakeString("Command"));
175 #ifdef OFFIX_DND
176 dDropCommand = PLRetain(PLMakeString("DropCommand"));
177 #endif
178 dAutoLaunch = PLRetain(PLMakeString("AutoLaunch"));
179 dName = PLRetain(PLMakeString("Name"));
180 dForced = PLRetain(PLMakeString("Forced"));
181 dBuggyApplication = PLRetain(PLMakeString("BuggyApplication"));
182 dYes = PLRetain(PLMakeString("Yes"));
183 dNo = PLRetain(PLMakeString("No"));
184 dHost = PLRetain(PLMakeString("Host"));
186 dPosition = PLMakeString("Position");
187 dApplications = PLMakeString("Applications");
188 dLowered = PLMakeString("Lowered");
189 dCollapsed = PLMakeString("Collapsed");
190 dAutoCollapse = PLMakeString("AutoCollapse");
191 dAutoRaiseLower = PLMakeString("AutoRaiseLower");
192 dAutoAttractIcons = PLMakeString("AutoAttractIcons");
193 dKeepAttracted = PLMakeString("KeepAttracted");
195 dDock = PLMakeString("Dock");
196 dClip = PLMakeString("Clip");
201 static void
202 renameCallback(WMenu *menu, WMenuEntry *entry)
204 WDock *dock = entry->clientdata;
205 char buffer[128];
206 int wspace;
207 char *name;
209 assert(entry->clientdata!=NULL);
211 wspace = dock->screen_ptr->current_workspace;
213 name = wstrdup(dock->screen_ptr->workspaces[wspace]->name);
215 sprintf(buffer, _("Type the name for workspace %i:"), wspace+1);
216 if (wInputDialog(dock->screen_ptr, _("Rename Workspace"), buffer,
217 &name)) {
218 wWorkspaceRename(dock->screen_ptr, wspace, name);
220 if (name) {
221 free(name);
226 static void
227 toggleLoweredCallback(WMenu *menu, WMenuEntry *entry)
229 assert(entry->clientdata!=NULL);
231 toggleLowered(entry->clientdata);
233 entry->flags.indicator_on = !((WDock*)entry->clientdata)->lowered;
235 wMenuPaint(menu);
240 static void
241 killCallback(WMenu *menu, WMenuEntry *entry)
243 WAppIcon *icon;
244 #ifdef REDUCE_APPICONS
245 WAppIconAppList *tapplist;
247 extern Atom _XA_WM_DELETE_WINDOW;
248 #else
249 char *buffer;
250 #endif
252 if (!WCHECK_STATE(WSTATE_NORMAL))
253 return;
255 assert(entry->clientdata!=NULL);
257 icon = (WAppIcon*)entry->clientdata;
259 icon->editing = 1;
261 WCHANGE_STATE(WSTATE_MODAL);
263 #ifdef REDUCE_APPICONS
264 /* Send a delete message to the main window of each application
265 * bound to this docked appicon. - cls
267 tapplist = icon->applist;
268 while (tapplist != NULL) {
269 if (tapplist->wapp->main_window_desc != NULL) {
270 if (tapplist->wapp->main_window_desc->protocols.DELETE_WINDOW) {
271 wClientSendProtocol(tapplist->wapp->main_window_desc,
272 _XA_WM_DELETE_WINDOW, CurrentTime);
273 } else {
274 wClientKill(tapplist->wapp->main_window_desc);
277 tapplist = tapplist->next;
279 #else
280 buffer = wstrappend(icon->wm_class,
281 _(" will be forcibly closed.\n"
282 "Any unsaved changes will be lost.\n"
283 "Please confirm."));
285 if (wPreferences.dont_confirm_kill
286 || wMessageDialog(menu->frame->screen_ptr, _("Kill Application"),
287 buffer, _("Yes"), _("No"), NULL)==WAPRDefault) {
288 if (icon->icon && icon->icon->owner) {
289 wClientKill(icon->icon->owner);
292 #endif /* !REDUCE_APPICONS */
294 icon->editing = 0;
296 WCHANGE_STATE(WSTATE_NORMAL);}
299 static LinkedList*
300 getSelected(WDock *dock)
302 LinkedList *ret=NULL;
303 WAppIcon *btn;
304 int i;
306 for (i=1; i<dock->max_icons; i++) {
307 btn = dock->icon_array[i];
308 if (btn && btn->icon->selected) {
309 ret = list_cons(btn, ret);
313 return ret;
317 static void
318 paintClipButtons(WAppIcon *clipIcon, Bool lpushed, Bool rpushed)
320 Window win = clipIcon->icon->core->window;
321 WScreen *scr = clipIcon->icon->core->screen_ptr;
322 XPoint p[4];
323 int pt = CLIP_BUTTON_SIZE*ICON_SIZE/64;
324 int tp = ICON_SIZE - pt;
325 int as = pt - 15; /* 15 = 5+5+5 */
328 if (rpushed) {
329 p[0].x = tp+1;
330 p[0].y = 1;
331 p[1].x = ICON_SIZE-2;
332 p[1].y = 1;
333 p[2].x = ICON_SIZE-2;
334 p[2].y = pt-1;
335 } else if (lpushed) {
336 p[0].x = 1;
337 p[0].y = tp;
338 p[1].x = pt;
339 p[1].y = ICON_SIZE-2;
340 p[2].x = 1;
341 p[2].y = ICON_SIZE-2;
343 if (lpushed || rpushed) {
344 XSetForeground(dpy, scr->draw_gc, scr->white_pixel);
345 XFillPolygon(dpy, win, scr->draw_gc, p, 3, Convex, CoordModeOrigin);
346 XSetForeground(dpy, scr->draw_gc, scr->black_pixel);
349 p[0].x = p[3].x = ICON_SIZE-6-as;
350 p[0].y = p[3].y = 5;
351 p[1].x = ICON_SIZE-6;
352 p[1].y = 5;
353 p[2].x = ICON_SIZE-6;
354 p[2].y = 5+as;
355 if (rpushed) {
356 XFillPolygon(dpy, win, scr->draw_gc, p, 3, Convex, CoordModeOrigin);
357 XDrawLines(dpy, win, scr->draw_gc, p, 4, CoordModeOrigin);
358 } else {
359 XFillPolygon(dpy, win, scr->clip_title_gc, p,3,Convex,CoordModeOrigin);
360 XDrawLines(dpy, win, scr->clip_title_gc, p,4,CoordModeOrigin);
363 p[0].x = p[3].x = 5;
364 p[0].y = p[3].y = ICON_SIZE-6-as;
365 p[1].x = 5;
366 p[1].y = ICON_SIZE-6;
367 p[2].x = 5+as;
368 p[2].y = ICON_SIZE-6;
369 if (lpushed) {
370 XFillPolygon(dpy, win, scr->draw_gc, p, 3, Convex, CoordModeOrigin);
371 XDrawLines(dpy, win, scr->draw_gc, p, 4, CoordModeOrigin);
372 } else {
373 XFillPolygon(dpy, win, scr->clip_title_gc, p,3,Convex,CoordModeOrigin);
374 XDrawLines(dpy, win, scr->clip_title_gc, p,4,CoordModeOrigin);
379 RImage*
380 wClipMakeTile(WScreen *scr, RImage *normalTile)
382 RImage *tile = RCloneImage(normalTile);
383 RColor black;
384 RColor dark;
385 RColor light;
386 int pt, tp;
388 pt = CLIP_BUTTON_SIZE*wPreferences.icon_size/64;
389 tp = wPreferences.icon_size-1 - pt;
391 black.alpha = 255;
392 black.red = black.green = black.blue = 0;
394 dark.alpha = 0;
395 dark.red = dark.green = dark.blue = 80;
397 light.alpha = 0;
398 light.red = light.green = light.blue = 80;
401 /* top right */
402 ROperateLine(tile, RSubtractOperation, tp, 0, wPreferences.icon_size-2,
403 pt-1, &dark);
404 RDrawLine(tile, tp-1, 0, wPreferences.icon_size-1, pt+1, &black);
405 ROperateLine(tile, RAddOperation, tp, 2, wPreferences.icon_size-3,
406 pt, &light);
409 /* bottom left */
410 ROperateLine(tile, RAddOperation, 2, tp+2, pt-2,
411 wPreferences.icon_size-3, &dark);
412 RDrawLine(tile, 0, tp-1, pt+1, wPreferences.icon_size-1, &black);
413 ROperateLine(tile, RSubtractOperation, 0, tp-2, pt+1,
414 wPreferences.icon_size-2, &light);
416 return tile;
420 static void
421 removeIconsCallback(WMenu *menu, WMenuEntry *entry)
423 WAppIcon *clickedIcon = (WAppIcon*)entry->clientdata;
424 WDock *dock;
425 WAppIcon *aicon;
426 LinkedList *selectedIcons;
427 int keepit;
429 assert(clickedIcon!=NULL);
431 dock = clickedIcon->dock;
433 selectedIcons = getSelected(dock);
435 if (selectedIcons) {
436 if (wMessageDialog(dock->screen_ptr, _("Workspace Clip"),
437 _("All selected icons will be removed!"),
438 _("OK"), _("Cancel"), NULL)!=WAPRDefault) {
439 return;
441 } else {
442 if (clickedIcon->xindex==0 && clickedIcon->yindex==0)
443 return;
444 selectedIcons = list_cons(clickedIcon, NULL);
447 while (selectedIcons) {
448 aicon = selectedIcons->head;
449 keepit = aicon->running && wApplicationOf(aicon->main_window);
450 wDockDetach(dock, aicon);
451 if (keepit) {
452 PlaceIcon(dock->screen_ptr, &aicon->x_pos, &aicon->y_pos);
453 XMoveWindow(dpy, aicon->icon->core->window,
454 aicon->x_pos, aicon->y_pos);
455 if (!dock->mapped || dock->collapsed)
456 XMapWindow(dpy, aicon->icon->core->window);
458 list_remove_head(&selectedIcons);
461 if (wPreferences.auto_arrange_icons)
462 wArrangeIcons(dock->screen_ptr, True);
466 static void
467 keepIconsCallback(WMenu *menu, WMenuEntry *entry)
469 WAppIcon *clickedIcon = (WAppIcon*)entry->clientdata;
470 WDock *dock;
471 WAppIcon *aicon;
472 LinkedList *selectedIcons;
474 assert(clickedIcon!=NULL);
475 dock = clickedIcon->dock;
477 selectedIcons = getSelected(dock);
479 if (!selectedIcons && clickedIcon!=dock->screen_ptr->clip_icon) {
480 char *command = NULL;
482 if (!clickedIcon->command && !clickedIcon->editing) {
483 clickedIcon->editing = 1;
484 if (wInputDialog(dock->screen_ptr, _("Keep Icon"),
485 _("Type the command used to launch the application"),
486 &command)) {
487 if (command && (command[0]==0 ||
488 (command[0]=='-' && command[1]==0))) {
489 free(command);
490 command = NULL;
492 clickedIcon->command = command;
493 clickedIcon->editing = 0;
494 } else {
495 clickedIcon->editing = 0;
496 if (command)
497 free(command);
498 return;
502 selectedIcons = list_cons(clickedIcon, NULL);
505 while (selectedIcons) {
506 aicon = selectedIcons->head;
507 if (aicon->icon->selected)
508 wIconSelect(aicon->icon);
509 if (aicon && aicon->attracted && aicon->command) {
510 aicon->attracted = 0;
511 if (aicon->icon->shadowed) {
512 aicon->icon->shadowed = 0;
513 aicon->icon->force_paint = 1;
514 wAppIconPaint(aicon);
517 list_remove_head(&selectedIcons);
524 static void
525 toggleAutoAttractCallback(WMenu *menu, WMenuEntry *entry)
527 WDock *dock = (WDock*)entry->clientdata;
529 assert(entry->clientdata!=NULL);
531 dock->attract_icons = !dock->attract_icons;
532 /*if (!dock->attract_icons)
533 dock->keep_attracted = 0;*/
535 entry->flags.indicator_on = dock->attract_icons;
537 wMenuPaint(menu);
541 static void
542 toggleKeepCallback(WMenu *menu, WMenuEntry *entry)
544 WDock *dock = (WDock*)entry->clientdata;
545 WAppIcon *btn;
546 int i;
548 assert(entry->clientdata!=NULL);
550 dock->keep_attracted = !dock->keep_attracted;
552 if (dock->keep_attracted) {
553 for (i=0; i< dock->max_icons; i++) {
554 btn = dock->icon_array[i];
555 if (btn && btn->attracted && btn->command) {
556 btn->attracted = 0;
557 if (btn->icon->shadowed) {
558 btn->icon->shadowed = 0;
559 btn->icon->force_paint = 1;
560 wAppIconPaint(btn);
566 entry->flags.indicator_on = dock->keep_attracted;
568 wMenuPaint(menu);
572 static void
573 selectCallback(WMenu *menu, WMenuEntry *entry)
575 WAppIcon *icon = (WAppIcon*)entry->clientdata;
577 assert(icon!=NULL);
579 wIconSelect(icon->icon);
581 wMenuPaint(menu);
585 static void
586 colectIconsCallback(WMenu *menu, WMenuEntry *entry)
588 WAppIcon *clickedIcon = (WAppIcon*)entry->clientdata;
589 WDock *clip;
590 WAppIcon *aicon;
591 int x, y, x_pos, y_pos;
593 assert(entry->clientdata!=NULL);
594 clip = clickedIcon->dock;
596 aicon = clip->screen_ptr->app_icon_list;
598 while (aicon) {
599 if (!aicon->docked && wDockFindFreeSlot(clip, &x, &y)) {
600 x_pos = clip->x_pos + x*ICON_SIZE;
601 y_pos = clip->y_pos + y*ICON_SIZE;
602 if (aicon->x_pos != x_pos || aicon->y_pos != y_pos) {
603 #ifdef ANIMATIONS
604 if (wPreferences.no_animations) {
605 XMoveWindow(dpy, aicon->icon->core->window, x_pos, y_pos);
606 } else {
607 SlideWindow(aicon->icon->core->window,
608 aicon->x_pos, aicon->y_pos, x_pos, y_pos);
610 #else
611 XMoveWindow(dpy, aicon->icon->core->window, x_pos, y_pos);
612 #endif /* ANIMATIONS */
614 aicon->attracted = 1;
615 if (!clip->keep_attracted && !aicon->icon->shadowed) {
616 aicon->icon->shadowed = 1;
617 aicon->icon->force_paint = 1;
618 /* We don't do an wAppIconPaint() here because it's in
619 * wDockAttachIcon(). -Dan
622 wDockAttachIcon(clip, aicon, x, y);
623 if (clip->collapsed || !clip->mapped)
624 XUnmapWindow(dpy, aicon->icon->core->window);
626 aicon = aicon->next;
631 static void
632 selectIconsCallback(WMenu *menu, WMenuEntry *entry)
634 WAppIcon *clickedIcon = (WAppIcon*)entry->clientdata;
635 WDock *dock;
636 LinkedList *selectedIcons;
637 WAppIcon *btn;
638 int i;
640 assert(clickedIcon!=NULL);
641 dock = clickedIcon->dock;
643 selectedIcons = getSelected(dock);
645 if (!selectedIcons) {
646 for (i=1; i<dock->max_icons; i++) {
647 btn = dock->icon_array[i];
648 if (btn && !btn->icon->selected) {
649 wIconSelect(btn->icon);
652 } else {
653 while(selectedIcons) {
654 btn = selectedIcons->head;
655 wIconSelect(btn->icon);
656 list_remove_head(&selectedIcons);
660 wMenuPaint(menu);
664 static void
665 toggleCollapsedCallback(WMenu *menu, WMenuEntry *entry)
667 assert(entry->clientdata!=NULL);
669 toggleCollapsed(entry->clientdata);
671 entry->flags.indicator_on = ((WDock*)entry->clientdata)->collapsed;
673 wMenuPaint(menu);
677 static void
678 toggleAutoCollapseCallback(WMenu *menu, WMenuEntry *entry)
680 WDock *dock;
681 assert(entry->clientdata!=NULL);
683 dock = (WDock*) entry->clientdata;
685 dock->auto_collapse = !dock->auto_collapse;
686 //if (dock->auto_collapse_magic) {
687 // WMDeleteTimerHandler(dock->auto_collapse_magic);
688 // dock->auto_collapse_magic = NULL;
691 entry->flags.indicator_on = ((WDock*)entry->clientdata)->auto_collapse;
693 wMenuPaint(menu);
697 static void
698 toggleAutoRaiseLowerCallback(WMenu *menu, WMenuEntry *entry)
700 WDock *dock;
701 assert(entry->clientdata!=NULL);
703 dock = (WDock*) entry->clientdata;
705 dock->auto_raise_lower = !dock->auto_raise_lower;
707 entry->flags.indicator_on = ((WDock*)entry->clientdata)->auto_raise_lower;
709 wMenuPaint(menu);
713 static void
714 launchCallback(WMenu *menu, WMenuEntry *entry)
716 WAppIcon *btn = (WAppIcon*)entry->clientdata;
718 launchDockedApplication(btn);
722 static void
723 settingsCallback(WMenu *menu, WMenuEntry *entry)
725 WAppIcon *btn = (WAppIcon*)entry->clientdata;
727 if (btn->editing)
728 return;
729 ShowDockAppSettingsPanel(btn);
733 static void
734 hideCallback(WMenu *menu, WMenuEntry *entry)
736 WApplication *wapp;
737 WAppIcon *btn = (WAppIcon*)entry->clientdata;
739 wapp = wApplicationOf(btn->icon->owner->main_window);
741 if (wapp->flags.hidden) {
742 wWorkspaceChange(btn->icon->core->screen_ptr,wapp->last_workspace);
743 wUnhideApplication(wapp, False, False);
744 } else {
745 wHideApplication(wapp);
750 static void
751 unhideHereCallback(WMenu *menu, WMenuEntry *entry)
753 WApplication *wapp;
754 WAppIcon *btn = (WAppIcon*)entry->clientdata;
756 wapp = wApplicationOf(btn->icon->owner->main_window);
758 wUnhideApplication(wapp, False, True);
762 WAppIcon*
763 mainIconCreate(WScreen *scr, int type)
765 WAppIcon *btn;
766 int x_pos;
768 if (type == WM_CLIP) {
769 if (scr->clip_icon)
770 return scr->clip_icon;
771 btn = wAppIconCreateForDock(scr, NULL, "Logo", "WMClip", TILE_CLIP);
772 btn->icon->core->descriptor.handle_expose = clipIconExpose;
773 btn->icon->core->descriptor.handle_enternotify = clipEnterNotify;
774 btn->icon->core->descriptor.handle_leavenotify = clipLeaveNotify;
775 /*x_pos = scr->scr_width - ICON_SIZE*2 - DOCK_EXTRA_SPACE;*/
776 x_pos = 0;
777 } else {
778 btn = wAppIconCreateForDock(scr, NULL, "Logo", "WMDock", TILE_NORMAL);
779 x_pos = scr->scr_width - ICON_SIZE - DOCK_EXTRA_SPACE;
782 btn->xindex = 0;
783 btn->yindex = 0;
785 btn->icon->core->descriptor.handle_mousedown = iconMouseDown;
786 btn->icon->core->descriptor.parent_type = WCLASS_DOCK_ICON;
787 btn->icon->core->descriptor.parent = btn;
788 /*ChangeStackingLevel(btn->icon->core, WMDockLevel);*/
789 XMapWindow(dpy, btn->icon->core->window);
790 btn->x_pos = x_pos;
791 btn->y_pos = 0;
792 btn->docked = 1;
793 if (type == WM_CLIP)
794 scr->clip_icon = btn;
796 return btn;
800 static void
801 switchWSCommand(WMenu *menu, WMenuEntry *entry)
803 WAppIcon *btn, *icon = (WAppIcon*) entry->clientdata;
804 WScreen *scr = icon->icon->core->screen_ptr;
805 WDock *src, *dest;
806 LinkedList *selectedIcons;
807 int x, y;
809 if (entry->order == scr->current_workspace)
810 return;
811 src = icon->dock;
812 dest = scr->workspaces[entry->order]->clip;
814 selectedIcons = getSelected(src);
816 if (selectedIcons) {
817 while(selectedIcons) {
818 btn = selectedIcons->head;
819 if (wDockFindFreeSlot(dest, &x, &y)) {
820 moveIconBetweenDocks(src, dest, btn, x, y);
821 XUnmapWindow(dpy, btn->icon->core->window);
823 list_remove_head(&selectedIcons);
825 } else if (icon != scr->clip_icon) {
826 if (wDockFindFreeSlot(dest, &x, &y)) {
827 moveIconBetweenDocks(src, dest, icon, x, y);
828 XUnmapWindow(dpy, icon->icon->core->window);
835 static void
836 launchDockedApplication(WAppIcon *btn)
838 WScreen *scr = btn->icon->core->screen_ptr;
840 if (!btn->launching && btn->command!=NULL) {
841 if (!btn->forced_dock) {
842 btn->relaunching = btn->running;
843 btn->running = 1;
845 if (btn->wm_instance || btn->wm_class) {
846 WWindowAttributes attr;
847 memset(&attr, 0, sizeof(WWindowAttributes));
848 wDefaultFillAttributes(scr, btn->wm_instance, btn->wm_class,
849 &attr, NULL, True);
851 if (!attr.no_appicon && !btn->buggy_app)
852 btn->launching = 1;
853 else
854 btn->running = 0;
856 btn->drop_launch = 0;
857 scr->last_dock = btn->dock;
858 btn->pid = execCommand(btn, btn->command, NULL);
859 if (btn->pid>0) {
860 if (btn->buggy_app) {
861 /* give feedback that the app was launched */
862 btn->launching = 1;
863 dockIconPaint(btn);
864 btn->launching = 0;
865 WMAddTimerHandler(200, (WMCallback*)dockIconPaint, btn);
866 } else {
867 dockIconPaint(btn);
869 } else {
870 wwarning(_("could not launch application %s\n"), btn->command);
871 btn->launching = 0;
872 if (!btn->relaunching)
873 btn->running = 0;
880 static void
881 updateWorkspaceMenu(WMenu *menu, WAppIcon *icon)
883 WScreen *scr = menu->frame->screen_ptr;
884 char title[MAX_WORKSPACENAME_WIDTH+1];
885 int i;
887 if (!menu || !icon)
888 return;
890 for (i=0; i<scr->workspace_count; i++) {
891 if (i < menu->entry_no) {
892 if (strcmp(menu->entries[i]->text,scr->workspaces[i]->name)!=0) {
893 free(menu->entries[i]->text);
894 strcpy(title, scr->workspaces[i]->name);
895 menu->entries[i]->text = wstrdup(title);
896 menu->flags.realized = 0;
898 menu->entries[i]->clientdata = (void*)icon;
899 } else {
900 strcpy(title, scr->workspaces[i]->name);
902 wMenuAddCallback(menu, title, switchWSCommand, (void*)icon);
904 menu->flags.realized = 0;
906 if (i == scr->current_workspace) {
907 wMenuSetEnabled(menu, i, False);
908 } else {
909 wMenuSetEnabled(menu, i, True);
913 if (!menu->flags.realized)
914 wMenuRealize(menu);
918 static WMenu*
919 makeWorkspaceMenu(WScreen *scr)
921 WMenu *menu;
923 menu = wMenuCreate(scr, NULL, False);
924 if (!menu)
925 wwarning(_("could not create workspace submenu for Clip menu"));
927 wMenuAddCallback(menu, "", switchWSCommand, (void*)scr->clip_icon);
929 menu->flags.realized = 0;
930 wMenuRealize(menu);
932 return menu;
936 static void
937 updateClipOptionsMenu(WMenu *menu, WDock *dock)
939 WMenuEntry *entry;
940 int index = 0;
942 if (!menu || !dock)
943 return;
945 /* keep on top */
946 entry = menu->entries[index];
947 entry->flags.indicator_on = !dock->lowered;
948 entry->clientdata = dock;
950 /* collapsed */
951 entry = menu->entries[++index];
952 entry->flags.indicator_on = dock->collapsed;
953 entry->clientdata = dock;
955 /* auto-collapse */
956 entry = menu->entries[++index];
957 entry->flags.indicator_on = dock->auto_collapse;
958 entry->clientdata = dock;
960 /* auto-raise/lower */
961 entry = menu->entries[++index];
962 entry->flags.indicator_on = dock->auto_raise_lower;
963 entry->clientdata = dock;
965 /* attract icons */
966 entry = menu->entries[++index];
967 entry->flags.indicator_on = dock->attract_icons;
968 entry->clientdata = dock;
970 /* keep attracted icons */
971 entry = menu->entries[++index];
972 entry->flags.indicator_on = dock->keep_attracted;
973 entry->clientdata = dock;
975 menu->flags.realized = 0;
976 wMenuRealize(menu);
980 static WMenu*
981 makeClipOptionsMenu(WScreen *scr)
983 WMenu *menu;
984 WMenuEntry *entry;
986 menu = wMenuCreate(scr, NULL, False);
987 if (!menu) {
988 wwarning(_("could not create options submenu for Clip menu"));
989 return NULL;
992 entry = wMenuAddCallback(menu, _("Keep on top"),
993 toggleLoweredCallback, NULL);
994 entry->flags.indicator = 1;
995 entry->flags.indicator_on = 1;
996 entry->flags.indicator_type = MI_CHECK;
998 entry = wMenuAddCallback(menu, _("Collapsed"),
999 toggleCollapsedCallback, NULL);
1000 entry->flags.indicator = 1;
1001 entry->flags.indicator_on = 1;
1002 entry->flags.indicator_type = MI_CHECK;
1004 entry = wMenuAddCallback(menu, _("AutoCollapse"),
1005 toggleAutoCollapseCallback, NULL);
1006 entry->flags.indicator = 1;
1007 entry->flags.indicator_on = 1;
1008 entry->flags.indicator_type = MI_CHECK;
1010 entry = wMenuAddCallback(menu, _("AutoRaiseLower"),
1011 toggleAutoRaiseLowerCallback, NULL);
1012 entry->flags.indicator = 1;
1013 entry->flags.indicator_on = 1;
1014 entry->flags.indicator_type = MI_CHECK;
1016 entry = wMenuAddCallback(menu, _("AutoAttract Icons"),
1017 toggleAutoAttractCallback, NULL);
1018 entry->flags.indicator = 1;
1019 entry->flags.indicator_on = 1;
1020 entry->flags.indicator_type = MI_CHECK;
1022 entry = wMenuAddCallback(menu, _("Keep Attracted Icons"),
1023 toggleKeepCallback, NULL);
1024 entry->flags.indicator = 1;
1025 entry->flags.indicator_on = 1;
1026 entry->flags.indicator_type = MI_CHECK;
1028 menu->flags.realized = 0;
1029 wMenuRealize(menu);
1031 return menu;
1035 static WMenu*
1036 dockMenuCreate(WScreen *scr, int type)
1038 WMenu *menu;
1039 WMenuEntry *entry;
1041 if (type == WM_CLIP && scr->clip_menu)
1042 return scr->clip_menu;
1044 menu = wMenuCreate(scr, NULL, False);
1045 if (type != WM_CLIP) {
1046 entry = wMenuAddCallback(menu, _("Keep on top"),
1047 toggleLoweredCallback, NULL);
1048 entry->flags.indicator = 1;
1049 entry->flags.indicator_on = 1;
1050 entry->flags.indicator_type = MI_CHECK;
1051 } else {
1052 entry = wMenuAddCallback(menu, _("Clip Options"), NULL, NULL);
1053 scr->clip_options = makeClipOptionsMenu(scr);
1054 if (scr->clip_options)
1055 wMenuEntrySetCascade(menu, entry, scr->clip_options);
1057 wMenuAddCallback(menu, _("Rename Workspace"), renameCallback, NULL);
1059 wMenuAddCallback(menu, _("(Un)Select Icon"), selectCallback, NULL);
1061 wMenuAddCallback(menu, _("(Un)Select All Icons"), selectIconsCallback,
1062 NULL);
1064 wMenuAddCallback(menu, _("Keep Icon(s)"), keepIconsCallback, NULL);
1066 entry = wMenuAddCallback(menu, _("Move Icon(s) To"), NULL, NULL);
1067 scr->clip_submenu = makeWorkspaceMenu(scr);
1068 if (scr->clip_submenu)
1069 wMenuEntrySetCascade(menu, entry, scr->clip_submenu);
1071 wMenuAddCallback(menu, _("Remove Icon(s)"), removeIconsCallback, NULL);
1073 wMenuAddCallback(menu, _("Attract Icons"), colectIconsCallback, NULL);
1076 wMenuAddCallback(menu, _("Launch"), launchCallback, NULL);
1078 wMenuAddCallback(menu, _("Unhide Here"), unhideHereCallback, NULL);
1080 entry = wMenuAddCallback(menu, _("Hide"), hideCallback, NULL);
1081 free(entry->text);
1082 entry->text = _("Hide");
1084 wMenuAddCallback(menu, _("Settings..."), settingsCallback, NULL);
1086 wMenuAddCallback(menu, _("Kill"), killCallback, NULL);
1088 if (type == WM_CLIP)
1089 scr->clip_menu = menu;
1091 return menu;
1095 WDock*
1096 wDockCreate(WScreen *scr, int type)
1098 WDock *dock;
1099 WAppIcon *btn;
1100 int icon_count;
1102 make_keys();
1104 dock = wmalloc(sizeof(WDock));
1105 memset(dock, 0, sizeof(WDock));
1107 if (type == WM_CLIP)
1108 icon_count = CLIP_MAX_ICONS;
1109 else
1110 icon_count = scr->scr_height/wPreferences.icon_size;
1112 dock->icon_array = wmalloc(sizeof(WAppIcon*)*icon_count);
1113 memset(dock->icon_array, 0, sizeof(WAppIcon*)*icon_count);
1115 dock->max_icons = icon_count;
1117 btn = mainIconCreate(scr, type);
1119 btn->dock = dock;
1121 dock->x_pos = btn->x_pos;
1122 dock->y_pos = btn->y_pos;
1123 dock->screen_ptr = scr;
1124 dock->type = type;
1125 dock->icon_count = 1;
1126 dock->on_right_side = 1;
1127 dock->collapsed = 0;
1128 dock->auto_collapse = 0;
1129 dock->auto_collapse_magic = NULL;
1130 dock->auto_raise_lower = 0;
1131 dock->auto_lower_magic = NULL;
1132 dock->auto_raise_magic = NULL;
1133 dock->attract_icons = 0;
1134 dock->keep_attracted = 0;
1135 dock->lowered = 1;
1136 dock->icon_array[0] = btn;
1137 wRaiseFrame(btn->icon->core);
1138 XMoveWindow(dpy, btn->icon->core->window, btn->x_pos, btn->y_pos);
1140 /* create dock menu */
1141 dock->menu = dockMenuCreate(scr, type);
1143 return dock;
1147 void
1148 wDockDestroy(WDock *dock)
1150 int i;
1151 WAppIcon *aicon;
1153 for (i=(dock->type == WM_CLIP) ? 1 : 0; i<dock->max_icons; i++) {
1154 aicon = dock->icon_array[i];
1155 if (aicon) {
1156 int keepit = aicon->running && wApplicationOf(aicon->main_window);
1157 wDockDetach(dock, aicon);
1158 if (keepit) {
1159 PlaceIcon(dock->screen_ptr, &aicon->x_pos, &aicon->y_pos);
1160 XMoveWindow(dpy, aicon->icon->core->window,
1161 aicon->x_pos, aicon->y_pos);
1162 if (!dock->mapped || dock->collapsed)
1163 XMapWindow(dpy, aicon->icon->core->window);
1167 if (wPreferences.auto_arrange_icons)
1168 wArrangeIcons(dock->screen_ptr, True);
1169 free(dock->icon_array);
1170 if (dock->menu && dock->type!=WM_CLIP)
1171 wMenuDestroy(dock->menu, True);
1172 free(dock);
1176 void
1177 wClipIconPaint(WAppIcon *aicon)
1179 WScreen *scr = aicon->icon->core->screen_ptr;
1180 WWorkspace *workspace = scr->workspaces[scr->current_workspace];
1181 GC gc;
1182 Window win = aicon->icon->core->window;
1183 int length, nlength;
1184 char *ws_name, ws_number[10];
1185 int ty, tx;
1187 wIconPaint(aicon->icon);
1189 length = strlen(workspace->name);
1190 ws_name = malloc(length + 1);
1191 sprintf(ws_name, "%s", workspace->name);
1192 sprintf(ws_number, "%i", scr->current_workspace + 1);
1193 nlength = strlen(ws_number);
1195 gc = scr->clip_title_gc;
1197 if (!workspace->clip->collapsed)
1198 XSetForeground(dpy, gc, scr->clip_title_pixel[CLIP_NORMAL]);
1199 else
1200 XSetForeground(dpy, gc, scr->clip_title_pixel[CLIP_COLLAPSED]);
1202 ty = ICON_SIZE - scr->clip_title_font->height - 3;
1204 tx = CLIP_BUTTON_SIZE*ICON_SIZE/64;
1206 wDrawString(win, scr->clip_title_font, gc, tx,
1207 ty + scr->clip_title_font->y, ws_name, length);
1209 tx = (ICON_SIZE/2 - wTextWidth(scr->clip_title_font->font, ws_number, nlength))/2;
1211 wDrawString(win, scr->clip_title_font, gc, tx,
1212 scr->clip_title_font->y + 2, ws_number, nlength);
1214 free(ws_name);
1216 if (aicon->launching) {
1217 XFillRectangle(dpy, aicon->icon->core->window, scr->stipple_gc,
1218 0, 0, wPreferences.icon_size, wPreferences.icon_size);
1220 paintClipButtons(aicon, aicon->dock->lclip_button_pushed,
1221 aicon->dock->rclip_button_pushed);
1225 static void
1226 clipIconExpose(WObjDescriptor *desc, XEvent *event)
1228 wClipIconPaint(desc->parent);
1232 static void
1233 dockIconPaint(WAppIcon *btn)
1235 if (btn == btn->icon->core->screen_ptr->clip_icon)
1236 wClipIconPaint(btn);
1237 else
1238 wAppIconPaint(btn);
1242 static proplist_t
1243 make_icon_state(WAppIcon *btn)
1245 proplist_t node = NULL;
1246 proplist_t command, autolaunch, name, forced, host, position, buggy;
1247 char *tmp;
1248 char buffer[64];
1250 if (btn) {
1251 if (!btn->command)
1252 command = PLMakeString("-");
1253 else
1254 command = PLMakeString(btn->command);
1256 autolaunch = btn->auto_launch ? dYes : dNo;
1258 tmp = EscapeWM_CLASS(btn->wm_instance, btn->wm_class);
1260 name = PLMakeString(tmp);
1262 free(tmp);
1264 forced = btn->forced_dock ? dYes : dNo;
1266 buggy = btn->buggy_app ? dYes: dNo;
1268 if (btn == btn->icon->core->screen_ptr->clip_icon)
1269 sprintf(buffer, "%i,%i", btn->x_pos, btn->y_pos);
1270 else
1271 sprintf(buffer, "%hi,%hi", btn->xindex, btn->yindex);
1272 position = PLMakeString(buffer);
1274 node = PLMakeDictionaryFromEntries(dCommand, command,
1275 dName, name,
1276 dAutoLaunch, autolaunch,
1277 dForced, forced,
1278 dBuggyApplication, buggy,
1279 dPosition, position,
1280 NULL);
1281 PLRelease(command);
1282 PLRelease(name);
1283 PLRelease(position);
1284 #ifdef OFFIX_DND
1285 if (btn->dnd_command) {
1286 command = PLMakeString(btn->dnd_command);
1287 PLInsertDictionaryEntry(node, dDropCommand, command);
1288 PLRelease(command);
1290 #endif /* OFFIX_DND */
1292 if (btn->client_machine && btn->remote_start) {
1293 host = PLMakeString(btn->client_machine);
1294 PLInsertDictionaryEntry(node, dHost, host);
1295 PLRelease(host);
1299 return node;
1303 static proplist_t
1304 dockSaveState(WDock *dock)
1306 int i;
1307 proplist_t icon_info;
1308 proplist_t list=NULL, dock_state=NULL;
1309 proplist_t value;
1310 char buffer[256];
1312 list = PLMakeArrayFromElements(NULL);
1314 for (i=(dock->type==WM_DOCK ? 0 : 1); i<dock->max_icons; i++) {
1315 WAppIcon *btn = dock->icon_array[i];
1317 if (!btn || (btn->attracted && !dock->keep_attracted))
1318 continue;
1320 if ((icon_info = make_icon_state(dock->icon_array[i]))) {
1321 list = PLAppendArrayElement(list, icon_info);
1322 PLRelease(icon_info);
1326 dock_state = PLMakeDictionaryFromEntries(dApplications, list, NULL);
1328 PLRelease(list);
1330 if (dock->type == WM_DOCK) {
1331 sprintf(buffer, "%i,%i", (dock->on_right_side ? -ICON_SIZE : 0),
1332 dock->y_pos);
1333 value = PLMakeString(buffer);
1334 PLInsertDictionaryEntry(dock_state, dPosition, value);
1335 PLRelease(value);
1338 value = (dock->lowered ? dYes : dNo);
1339 PLInsertDictionaryEntry(dock_state, dLowered, value);
1341 if (dock->type == WM_CLIP) {
1342 value = (dock->collapsed ? dYes : dNo);
1343 PLInsertDictionaryEntry(dock_state, dCollapsed, value);
1345 value = (dock->auto_collapse ? dYes : dNo);
1346 PLInsertDictionaryEntry(dock_state, dAutoCollapse, value);
1348 value = (dock->auto_raise_lower ? dYes : dNo);
1349 PLInsertDictionaryEntry(dock_state, dAutoRaiseLower, value);
1351 value = (dock->attract_icons ? dYes : dNo);
1352 PLInsertDictionaryEntry(dock_state, dAutoAttractIcons, value);
1354 value = (dock->keep_attracted ? dYes : dNo);
1355 PLInsertDictionaryEntry(dock_state, dKeepAttracted, value);
1358 return dock_state;
1362 void
1363 wDockSaveState(WScreen *scr)
1365 proplist_t dock_state;
1367 dock_state = dockSaveState(scr->dock);
1369 PLInsertDictionaryEntry(scr->session_state, dDock, dock_state);
1371 PLRelease(dock_state);
1375 void
1376 wClipSaveState(WScreen *scr)
1378 proplist_t clip_state;
1380 clip_state = make_icon_state(scr->clip_icon);
1382 PLInsertDictionaryEntry(scr->session_state, dClip, clip_state);
1384 PLRelease(clip_state);
1388 proplist_t
1389 wClipSaveWorkspaceState(WScreen *scr, int workspace)
1391 return dockSaveState(scr->workspaces[workspace]->clip);
1395 static WAppIcon*
1396 restore_icon_state(WScreen *scr, proplist_t info, int type, int index)
1398 WAppIcon *aicon;
1399 char *wclass, *winstance;
1400 proplist_t cmd, value;
1401 char *command;
1404 cmd = PLGetDictionaryEntry(info, dCommand);
1405 if (!cmd || !PLIsString(cmd)) {
1406 return NULL;
1409 /* parse window name */
1410 value = PLGetDictionaryEntry(info, dName);
1411 if (!value)
1412 return NULL;
1414 ParseWindowName(value, &winstance, &wclass, "dock");
1416 if (!winstance && !wclass) {
1417 return NULL;
1420 /* get commands */
1422 if (cmd)
1423 command = wstrdup(PLGetString(cmd));
1424 else
1425 command = NULL;
1427 if (!command || strcmp(command, "-")==0) {
1428 if (command)
1429 free(command);
1430 if (wclass)
1431 free(wclass);
1432 if (winstance)
1433 free(winstance);
1435 return NULL;
1438 aicon = wAppIconCreateForDock(scr, command, winstance, wclass,
1439 TILE_NORMAL);
1440 if (wclass)
1441 free(wclass);
1442 if (winstance)
1443 free(winstance);
1445 aicon->icon->core->descriptor.handle_mousedown = iconMouseDown;
1446 if (type == WM_CLIP) {
1447 aicon->icon->core->descriptor.handle_enternotify = clipEnterNotify;
1448 aicon->icon->core->descriptor.handle_leavenotify = clipLeaveNotify;
1450 aicon->icon->core->descriptor.parent_type = WCLASS_DOCK_ICON;
1451 aicon->icon->core->descriptor.parent = aicon;
1454 #ifdef OFFIX_DND
1455 cmd = PLGetDictionaryEntry(info, dDropCommand);
1456 if (cmd)
1457 aicon->dnd_command = wstrdup(PLGetString(cmd));
1458 #endif
1460 /* check auto launch */
1461 value = PLGetDictionaryEntry(info, dAutoLaunch);
1463 aicon->auto_launch = 0;
1464 if (value) {
1465 if (PLIsString(value)) {
1466 if (strcasecmp(PLGetString(value), "YES")==0)
1467 aicon->auto_launch = 1;
1468 } else {
1469 wwarning(_("bad value in docked icon state info %s"),
1470 PLGetString(dAutoLaunch));
1474 /* check if it wasn't normally docked */
1475 value = PLGetDictionaryEntry(info, dForced);
1477 aicon->forced_dock = 0;
1478 if (value) {
1479 if (PLIsString(value)) {
1480 if (strcasecmp(PLGetString(value), "YES")==0)
1481 aicon->forced_dock = 1;
1482 } else {
1483 wwarning(_("bad value in docked icon state info %s"),
1484 PLGetString(dForced));
1488 /* check if we can rely on the stuff in the app */
1489 value = PLGetDictionaryEntry(info, dBuggyApplication);
1491 aicon->buggy_app = 0;
1492 if (value) {
1493 if (PLIsString(value)) {
1494 if (strcasecmp(PLGetString(value), "YES")==0)
1495 aicon->buggy_app = 1;
1496 } else {
1497 wwarning(_("bad value in docked icon state info %s"),
1498 PLGetString(dBuggyApplication));
1502 /* get position in the dock */
1503 value = PLGetDictionaryEntry(info, dPosition);
1504 if (value && PLIsString(value)) {
1505 if (sscanf(PLGetString(value), "%hi,%hi", &aicon->xindex,
1506 &aicon->yindex)!=2)
1507 wwarning(_("bad value in docked icon state info %s"),
1508 PLGetString(dPosition));
1510 /* check position sanity */
1511 /* incomplete section! */
1512 if (type == WM_DOCK) {
1513 aicon->xindex = 0;
1514 if (aicon->yindex < 0)
1515 wwarning(_("bad value in docked icon position %i,%i"),
1516 aicon->xindex, aicon->yindex);
1518 } else {
1519 aicon->yindex = index;
1520 aicon->xindex = 0;
1523 aicon->running = 0;
1524 aicon->docked = 1;
1526 return aicon;
1530 #define COMPLAIN(key) wwarning(_("bad value in dock state info:%s"), key)
1533 WAppIcon*
1534 wClipRestoreState(WScreen *scr, proplist_t clip_state)
1536 WAppIcon *icon;
1537 proplist_t value;
1540 icon = mainIconCreate(scr, WM_CLIP);
1542 if (!clip_state)
1543 return icon;
1544 else
1545 PLRetain(clip_state);
1547 /* restore position */
1549 value = PLGetDictionaryEntry(clip_state, dPosition);
1551 if (value) {
1552 if (!PLIsString(value))
1553 COMPLAIN("Position");
1554 else {
1555 if (sscanf(PLGetString(value), "%i,%i", &icon->x_pos,
1556 &icon->y_pos)!=2)
1557 COMPLAIN("Position");
1559 /* check position sanity */
1560 if (icon->y_pos < 0)
1561 icon->y_pos = 0;
1562 else if (icon->y_pos > scr->scr_height-ICON_SIZE)
1563 icon->y_pos = scr->scr_height-ICON_SIZE;
1565 if (icon->x_pos < 0)
1566 icon->x_pos = 0;
1567 else if (icon->x_pos > scr->scr_width-ICON_SIZE)
1568 icon->x_pos = scr->scr_width-ICON_SIZE;
1572 #ifdef OFFIX_DND
1573 value = PLGetDictionaryEntry(clip_state, dDropCommand);
1574 if (value && PLIsString(value))
1575 icon->dnd_command = wstrdup(PLGetString(value));
1576 #endif
1578 PLRelease(clip_state);
1580 return icon;
1584 WDock*
1585 wDockRestoreState(WScreen *scr, proplist_t dock_state, int type)
1587 WDock *dock;
1588 proplist_t apps;
1589 proplist_t value;
1590 WAppIcon *aicon, *old_top;
1591 int count, i;
1594 dock = wDockCreate(scr, type);
1596 if (!dock_state)
1597 return dock;
1599 if (dock_state)
1600 PLRetain(dock_state);
1603 /* restore position */
1605 value = PLGetDictionaryEntry(dock_state, dPosition);
1607 if (value) {
1608 if (!PLIsString(value))
1609 COMPLAIN("Position");
1610 else {
1611 if (sscanf(PLGetString(value), "%i,%i", &dock->x_pos,
1612 &dock->y_pos)!=2)
1613 COMPLAIN("Position");
1615 /* check position sanity */
1616 if (dock->y_pos < 0)
1617 dock->y_pos = 0;
1618 else if (dock->y_pos > scr->scr_height-ICON_SIZE)
1619 dock->y_pos = scr->scr_height - ICON_SIZE;
1621 /* This is no more needed. ??? */
1622 if (type == WM_CLIP) {
1623 if (dock->x_pos < 0)
1624 dock->x_pos = 0;
1625 else if (dock->x_pos > scr->scr_width-ICON_SIZE)
1626 dock->x_pos = scr->scr_width-ICON_SIZE;
1627 } else {
1628 if (dock->x_pos >= 0) {
1629 dock->x_pos = DOCK_EXTRA_SPACE;
1630 dock->on_right_side = 0;
1631 } else {
1632 dock->x_pos = scr->scr_width - DOCK_EXTRA_SPACE - ICON_SIZE;
1633 dock->on_right_side = 1;
1639 /* restore lowered/raised state */
1641 dock->lowered = 0;
1643 value = PLGetDictionaryEntry(dock_state, dLowered);
1645 if (value) {
1646 if (!PLIsString(value))
1647 COMPLAIN("Lowered");
1648 else {
1649 if (strcasecmp(PLGetString(value), "YES")==0)
1650 dock->lowered = 1;
1655 /* restore collapsed state */
1657 dock->collapsed = 0;
1659 value = PLGetDictionaryEntry(dock_state, dCollapsed);
1661 if (value) {
1662 if (!PLIsString(value))
1663 COMPLAIN("Collapsed");
1664 else {
1665 if (strcasecmp(PLGetString(value), "YES")==0)
1666 dock->collapsed = 1;
1671 /* restore auto-collapsed state */
1673 value = PLGetDictionaryEntry(dock_state, dAutoCollapse);
1675 if (value) {
1676 if (!PLIsString(value))
1677 COMPLAIN("AutoCollapse");
1678 else {
1679 if (strcasecmp(PLGetString(value), "YES")==0) {
1680 dock->auto_collapse = 1;
1681 dock->collapsed = 1;
1687 /* restore auto-raise/lower state */
1689 value = PLGetDictionaryEntry(dock_state, dAutoRaiseLower);
1691 if (value) {
1692 if (!PLIsString(value))
1693 COMPLAIN("AutoRaiseLower");
1694 else {
1695 if (strcasecmp(PLGetString(value), "YES")==0) {
1696 dock->auto_raise_lower = 1;
1702 /* restore attract icons state */
1704 dock->attract_icons = 0;
1706 value = PLGetDictionaryEntry(dock_state, dAutoAttractIcons);
1708 if (value) {
1709 if (!PLIsString(value))
1710 COMPLAIN("AutoAttractIcons");
1711 else {
1712 if (strcasecmp(PLGetString(value), "YES")==0)
1713 dock->attract_icons = 1;
1718 /* restore keep attracted icons state */
1720 dock->keep_attracted = 0;
1722 value = PLGetDictionaryEntry(dock_state, dKeepAttracted);
1724 if (value) {
1725 if (!PLIsString(value))
1726 COMPLAIN("KeepAttracted");
1727 else {
1728 if (strcasecmp(PLGetString(value), "YES")==0)
1729 dock->keep_attracted = 1;
1734 /* application list */
1736 apps = PLGetDictionaryEntry(dock_state, dApplications);
1738 if (!apps) {
1739 goto finish;
1742 count = PLGetNumberOfElements(apps);
1744 if (count==0)
1745 goto finish;
1747 old_top = dock->icon_array[0];
1749 /* dock->icon_count is set to 1 when dock is created.
1750 * Since Clip is already restored, we want to keep it so for clip,
1751 * but for dock we may change the default top tile, so we set it to 0.
1753 if (type == WM_DOCK)
1754 dock->icon_count = 0;
1756 for (i=0; i<count; i++) {
1757 if (dock->icon_count >= dock->max_icons) {
1758 wwarning(_("there are too many icons stored in dock. Ignoring what doesn't fit"));
1759 break;
1762 value = PLGetArrayElement(apps, i);
1763 aicon = restore_icon_state(scr, value, type, dock->icon_count);
1765 dock->icon_array[dock->icon_count] = aicon;
1767 if (aicon) {
1768 aicon->dock = dock;
1769 aicon->x_pos = dock->x_pos + (aicon->xindex*ICON_SIZE);
1770 aicon->y_pos = dock->y_pos + (aicon->yindex*ICON_SIZE);
1772 if (dock->lowered)
1773 ChangeStackingLevel(aicon->icon->core, WMNormalLevel);
1774 else
1775 ChangeStackingLevel(aicon->icon->core, WMDockLevel);
1777 wCoreConfigure(aicon->icon->core, aicon->x_pos, aicon->y_pos,
1778 0, 0);
1780 if (!dock->collapsed)
1781 XMapWindow(dpy, aicon->icon->core->window);
1782 wRaiseFrame(aicon->icon->core);
1784 dock->icon_count++;
1785 } else if (dock->icon_count==0 && type==WM_DOCK)
1786 dock->icon_count++;
1789 /* if the first icon is not defined, use the default */
1790 if (dock->icon_array[0]==NULL) {
1791 /* update default icon */
1792 old_top->x_pos = dock->x_pos;
1793 old_top->y_pos = dock->y_pos;
1794 if (dock->lowered)
1795 ChangeStackingLevel(old_top->icon->core, WMNormalLevel);
1796 else
1797 ChangeStackingLevel(old_top->icon->core, WMDockLevel);
1798 dock->icon_array[0] = old_top;
1799 XMoveWindow(dpy, old_top->icon->core->window, dock->x_pos, dock->y_pos);
1800 /* we don't need to increment dock->icon_count here because it was
1801 * incremented in the loop above.
1803 } else if (old_top!=dock->icon_array[0]) {
1804 if (old_top == scr->clip_icon)
1805 scr->clip_icon = dock->icon_array[0];
1806 wAppIconDestroy(old_top);
1809 finish:
1810 if (dock_state)
1811 PLRelease(dock_state);
1813 return dock;
1818 void
1819 wDockLaunchWithState(WDock *dock, WAppIcon *btn, WSavedState *state)
1821 if (btn && btn->command && !btn->running && !btn->launching) {
1823 btn->drop_launch = 0;
1825 btn->pid = execCommand(btn, btn->command, state);
1827 if (btn->pid>0) {
1828 if (!btn->forced_dock && !btn->buggy_app) {
1829 btn->launching = 1;
1830 dockIconPaint(btn);
1833 } else {
1834 free(state);
1839 void
1840 wDockDoAutoLaunch(WDock *dock, int workspace)
1842 WAppIcon *btn;
1843 WSavedState *state;
1844 int i;
1846 for (i = 0; i < dock->max_icons; i++) {
1847 btn = dock->icon_array[i];
1848 if (!btn || !btn->auto_launch)
1849 continue;
1851 state = wmalloc(sizeof(WSavedState));
1852 memset(state, 0, sizeof(WSavedState));
1853 state->workspace = workspace;
1854 /* TODO: this is klugy and is very difficult to understand
1855 * what's going on. Try to clean up */
1856 wDockLaunchWithState(dock, btn, state);
1860 #ifdef REDUCE_APPICONS
1861 void
1862 wDockSimulateLaunch(WDock *dock, WAppIcon *btn)
1864 if ((btn == NULL) || (dock == NULL))
1865 return;
1867 if (!btn->running) {
1868 if ((btn->icon->owner == NULL) && (btn->applist))
1869 btn->icon->owner = btn->applist->wapp->main_window_desc;
1870 if (!btn->forced_dock)
1871 btn->launching = 1;
1872 dockIconPaint(btn);
1873 wusleep(5000);
1876 #endif
1878 #ifdef OFFIX_DND
1879 static WDock*
1880 findDock(WScreen *scr, XEvent *event, int *icon_pos)
1882 WDock *dock;
1883 int i;
1885 *icon_pos = -1;
1886 if ((dock = scr->dock)!=NULL) {
1887 for (i=0; i<dock->max_icons; i++) {
1888 if (dock->icon_array[i]
1889 && dock->icon_array[i]->icon->core->window==event->xclient.window) {
1890 *icon_pos = i;
1891 break;
1895 if (*icon_pos<0 && (dock = scr->workspaces[scr->current_workspace]->clip)!=NULL) {
1896 for (i=0; i<dock->max_icons; i++) {
1897 if (dock->icon_array[i]
1898 && dock->icon_array[i]->icon->core->window==event->xclient.window) {
1899 *icon_pos = i;
1900 break;
1904 if(*icon_pos>=0)
1905 return dock;
1906 return NULL;
1911 wDockReceiveDNDDrop(WScreen *scr, XEvent *event)
1913 WDock *dock;
1914 WAppIcon *btn;
1915 int icon_pos;
1917 dock = findDock(scr, event, &icon_pos);
1918 if (!dock)
1919 return False;
1922 * Return True if the drop was on an application icon window.
1923 * In this case, let the ClientMessage handler redirect the
1924 * message to the app.
1926 if (dock->icon_array[icon_pos]->icon->icon_win!=None)
1927 return True;
1929 if (dock->icon_array[icon_pos]->dnd_command!=NULL) {
1930 scr->flags.dnd_data_convertion_status = 0;
1932 btn = dock->icon_array[icon_pos];
1934 if (!btn->forced_dock) {
1935 btn->relaunching = btn->running;
1936 btn->running = 1;
1938 if (btn->wm_instance || btn->wm_class) {
1939 WWindowAttributes attr;
1940 memset(&attr, 0, sizeof(WWindowAttributes));
1941 wDefaultFillAttributes(btn->icon->core->screen_ptr,
1942 btn->wm_instance,
1943 btn->wm_class, &attr, NULL, True);
1945 if (!attr.no_appicon)
1946 btn->launching = 1;
1947 else
1948 btn->running = 0;
1951 btn->drop_launch = 1;
1952 scr->last_dock = dock;
1953 btn->pid = execCommand(btn, btn->dnd_command, NULL);
1954 if (btn->pid>0) {
1955 dockIconPaint(btn);
1956 } else {
1957 btn->launching = 0;
1958 if (!btn->relaunching) {
1959 btn->running = 0;
1963 return False;
1965 #endif /* OFFIX_DND */
1969 Bool
1970 wDockAttachIcon(WDock *dock, WAppIcon *icon, int x, int y)
1972 WWindow *wwin;
1973 char **argv;
1974 int argc;
1975 int index;
1977 wwin = icon->icon->owner;
1978 if (icon->command==NULL) {
1979 icon->editing = 0;
1980 if (XGetCommand(dpy, wwin->client_win, &argv, &argc) && argc>0) {
1982 icon->command = FlattenStringList(argv, argc);
1983 XFreeStringList(argv);
1984 } else {
1985 char *command=NULL;
1987 /* icon->forced_dock = 1;*/
1988 if (!icon->attracted || dock->type!=WM_CLIP || dock->keep_attracted) {
1989 icon->editing = 1;
1990 if (wInputDialog(dock->screen_ptr, _("Dock Icon"),
1991 _("Type the command used to launch the application"),
1992 &command)) {
1993 if (command && (command[0]==0 ||
1994 (command[0]=='-' && command[1]==0))) {
1995 free(command);
1996 command = NULL;
1998 icon->command = command;
1999 icon->editing = 0;
2000 } else {
2001 icon->editing = 0;
2002 if (command)
2003 free(command);
2004 /* If the target is the dock, reject the icon. If
2005 * the target is the clip, make it an attracted icon
2007 if (dock->type==WM_CLIP) {
2008 icon->attracted = 1;
2009 if (!icon->icon->shadowed) {
2010 icon->icon->shadowed = 1;
2011 icon->icon->force_paint = 1;
2013 } else {
2014 return False;
2019 } else {
2020 icon->editing = 0;
2023 for (index=1; index<dock->max_icons; index++)
2024 if (dock->icon_array[index] == NULL)
2025 break;
2026 /* if (index == dock->max_icons)
2027 return; */
2029 assert(index < dock->max_icons);
2031 dock->icon_array[index] = icon;
2032 icon->yindex = y;
2033 icon->xindex = x;
2035 icon->x_pos = dock->x_pos + x*ICON_SIZE;
2036 icon->y_pos = dock->y_pos + y*ICON_SIZE;
2038 dock->icon_count++;
2040 icon->running = 1;
2041 icon->launching = 0;
2042 icon->docked = 1;
2043 icon->dock = dock;
2044 icon->icon->core->descriptor.handle_mousedown = iconMouseDown;
2045 if (dock->type == WM_CLIP) {
2046 icon->icon->core->descriptor.handle_enternotify = clipEnterNotify;
2047 icon->icon->core->descriptor.handle_leavenotify = clipLeaveNotify;
2049 icon->icon->core->descriptor.parent_type = WCLASS_DOCK_ICON;
2050 icon->icon->core->descriptor.parent = icon;
2052 MoveInStackListUnder(dock->icon_array[index-1]->icon->core,
2053 icon->icon->core);
2054 wAppIconMove(icon, icon->x_pos, icon->y_pos);
2055 wAppIconPaint(icon);
2057 if (wPreferences.auto_arrange_icons)
2058 wArrangeIcons(dock->screen_ptr, True);
2060 #ifdef OFFIX_DND
2061 if (icon->command && !icon->dnd_command) {
2062 icon->dnd_command = wmalloc(strlen(icon->command)+8);
2063 sprintf(icon->dnd_command, "%s %%d", icon->command);
2065 #endif
2067 return True;
2071 void
2072 reattachIcon(WDock *dock, WAppIcon *icon, int x, int y)
2074 int index;
2076 for(index=1; index<dock->max_icons; index++) {
2077 if(dock->icon_array[index] == icon)
2078 break;
2080 assert(index < dock->max_icons);
2082 icon->yindex = y;
2083 icon->xindex = x;
2085 icon->x_pos = dock->x_pos + x*ICON_SIZE;
2086 icon->y_pos = dock->y_pos + y*ICON_SIZE;
2090 Bool
2091 moveIconBetweenDocks(WDock *src, WDock *dest, WAppIcon *icon, int x, int y)
2093 WWindow *wwin;
2094 char **argv;
2095 int argc;
2096 int index;
2098 if (dest == NULL)
2099 return False;
2101 wwin = icon->icon->owner;
2104 * For the moment we can't do this if we move icons in Clip from one
2105 * workspace to other, because if we move two or more icons without
2106 * command, the dialog box will not be able to tell us to which of the
2107 * moved icons it applies. -Dan
2109 if ((dest->type==WM_DOCK /*|| dest->keep_attracted*/) && icon->command==NULL) {
2110 if (XGetCommand(dpy, wwin->client_win, &argv, &argc) && argc>0) {
2112 icon->command = FlattenStringList(argv, argc);
2113 XFreeStringList(argv);
2114 } else {
2115 char *command=NULL;
2117 icon->editing = 1;
2118 /* icon->forced_dock = 1;*/
2119 if (wInputDialog(src->screen_ptr, _("Dock Icon"),
2120 _("Type the command used to launch the application"),
2121 &command)) {
2122 if (command && (command[0]==0 ||
2123 (command[0]=='-' && command[1]==0))) {
2124 free(command);
2125 command = NULL;
2127 icon->command = command;
2128 } else {
2129 icon->editing = 0;
2130 if (command)
2131 free(command);
2132 return False;
2134 icon->editing = 0;
2138 for(index=1; index<src->max_icons; index++) {
2139 if(src->icon_array[index] == icon)
2140 break;
2142 assert(index < src->max_icons);
2144 src->icon_array[index] = NULL;
2145 src->icon_count--;
2147 for(index=1; index<dest->max_icons; index++) {
2148 if(dest->icon_array[index] == NULL)
2149 break;
2151 /* if (index == dest->max_icons)
2152 return; */
2154 assert(index < dest->max_icons);
2156 dest->icon_array[index] = icon;
2157 icon->dock = dest;
2159 /* deselect the icon */
2160 if (icon->icon->selected)
2161 wIconSelect(icon->icon);
2163 if (dest->type == WM_DOCK) {
2164 icon->icon->core->descriptor.handle_enternotify = NULL;
2165 icon->icon->core->descriptor.handle_leavenotify = NULL;
2166 } else {
2167 icon->icon->core->descriptor.handle_enternotify = clipEnterNotify;
2168 icon->icon->core->descriptor.handle_leavenotify = clipLeaveNotify;
2171 /* set it to be kept when moving to dock, or to a clip that keep the
2172 * attracted icons.
2173 * Unless the icon does not have a command set
2175 if (icon->command && (dest->type==WM_DOCK || dest->keep_attracted)) {
2176 icon->attracted = 0;
2177 if (icon->icon->shadowed) {
2178 icon->icon->shadowed = 0;
2179 icon->icon->force_paint = 1;
2183 if (src->auto_collapse || src->auto_raise_lower)
2184 clipLeave(src);
2186 icon->yindex = y;
2187 icon->xindex = x;
2189 icon->x_pos = dest->x_pos + x*ICON_SIZE;
2190 icon->y_pos = dest->y_pos + y*ICON_SIZE;
2192 dest->icon_count++;
2194 MoveInStackListUnder(dest->icon_array[index-1]->icon->core,
2195 icon->icon->core);
2196 wAppIconPaint(icon);
2198 return True;
2202 void
2203 wDockDetach(WDock *dock, WAppIcon *icon)
2205 int index;
2207 /* make the settings panel be closed */
2208 if (icon->panel) {
2209 DestroyDockAppSettingsPanel(icon->panel);
2212 icon->docked = 0;
2213 icon->dock = NULL;
2214 icon->attracted = 0;
2215 if (icon->icon->shadowed) {
2216 icon->icon->shadowed = 0;
2217 icon->icon->force_paint = 1;
2220 /* deselect the icon */
2221 if (icon->icon->selected)
2222 wIconSelect(icon->icon);
2224 if (icon->command) {
2225 free(icon->command);
2226 icon->command = NULL;
2228 #ifdef OFFIX_DND
2229 if (icon->dnd_command) {
2230 free(icon->dnd_command);
2231 icon->dnd_command = NULL;
2233 #endif
2235 for (index=1; index<dock->max_icons; index++)
2236 if (dock->icon_array[index] == icon)
2237 break;
2238 assert(index < dock->max_icons);
2239 dock->icon_array[index] = NULL;
2240 icon->yindex = -1;
2241 icon->xindex = -1;
2242 dock->icon_count--;
2244 /* if the dock is not attached to an application or
2245 * the the application did not set the approriate hints yet,
2246 * destroy the icon */
2247 #ifdef REDUCE_APPICONS
2248 if ((icon->num_apps == 0) && (!icon->running || !wApplicationOf(icon->main_window)) )
2249 #else
2250 if (!icon->running || !wApplicationOf(icon->main_window))
2251 #endif
2252 wAppIconDestroy(icon);
2253 else {
2254 icon->icon->core->descriptor.handle_mousedown = appIconMouseDown;
2255 icon->icon->core->descriptor.handle_enternotify = NULL;
2256 icon->icon->core->descriptor.handle_leavenotify = NULL;
2257 icon->icon->core->descriptor.parent_type = WCLASS_APPICON;
2258 icon->icon->core->descriptor.parent = icon;
2260 ChangeStackingLevel(icon->icon->core, NORMAL_ICON_LEVEL);
2262 wAppIconPaint(icon);
2263 if (wPreferences.auto_arrange_icons) {
2264 wArrangeIcons(dock->screen_ptr, True);
2267 if (dock->auto_collapse || dock->auto_raise_lower)
2268 clipLeave(dock);
2273 * returns the closest Dock slot index for the passed
2274 * coordinates.
2276 * Returns False if icon can't be docked.
2278 Bool
2279 wDockSnapIcon(WDock *dock, WAppIcon *icon, int req_x, int req_y,
2280 int *ret_x, int *ret_y, int redocking)
2282 WScreen *scr = dock->screen_ptr;
2283 int dx, dy;
2284 int ex_x, ex_y;
2285 int i, offset = ICON_SIZE/2;
2286 WAppIcon *aicon = NULL;
2287 WAppIcon *nicon = NULL;
2288 int max_y_icons, max_x_icons;
2290 max_x_icons = scr->scr_width/ICON_SIZE;
2291 max_y_icons = scr->scr_height/ICON_SIZE-1;
2293 if (wPreferences.flags.noupdates)
2294 return False;
2296 dx = dock->x_pos;
2297 dy = dock->y_pos;
2299 /* if the dock is full */
2300 if (!redocking &&
2301 (dock->icon_count >= dock->max_icons)) {
2302 return False;
2305 /* exact position */
2306 if (req_y < dy)
2307 ex_y = (req_y - offset - dy)/ICON_SIZE;
2308 else
2309 ex_y = (req_y + offset - dy)/ICON_SIZE;
2311 if (req_x < dx)
2312 ex_x = (req_x - offset - dx)/ICON_SIZE;
2313 else
2314 ex_x = (req_x + offset - dx)/ICON_SIZE;
2316 /* check if the icon is outside the screen boundaries */
2317 if (dx + ex_x*ICON_SIZE < -ICON_SIZE+2 ||
2318 dx + ex_x*ICON_SIZE >= scr->scr_width-1 ||
2319 dy + ex_y*ICON_SIZE < -ICON_SIZE+2 ||
2320 dy + ex_y*ICON_SIZE >= scr->scr_height-1)
2321 return False;
2323 if (dock->type == WM_DOCK) {
2324 if (icon->dock != dock && ex_x != 0)
2325 return False;
2327 for (i=0; i<dock->max_icons; i++) {
2328 nicon = dock->icon_array[i];
2329 if (nicon && nicon->yindex == ex_y) {
2330 aicon = nicon;
2331 break;
2335 *ret_x = 0;
2337 if (redocking) {
2338 int sig, done, closest;
2340 /* Possible cases when redocking:
2342 * icon dragged out of range of any slot -> false
2343 * icon dragged to range of free slot
2344 * icon dragged to range of same slot
2345 * icon dragged to range of different icon
2347 if (abs(ex_x) > DOCK_DETTACH_THRESHOLD)
2348 return False;
2350 if (ex_y >= 0 && ex_y <= max_y_icons && (aicon == icon || !aicon)) {
2352 *ret_y = ex_y;
2355 return True;
2358 /* start looking at the upper slot or lower? */
2359 if (ex_y*ICON_SIZE < (req_y + offset - dy))
2360 sig = 1;
2361 else
2362 sig = -1;
2364 closest = -1;
2365 done = 0;
2366 /* look for closest free slot */
2367 for (i=0; i<(DOCK_DETTACH_THRESHOLD+1)*2 && !done; i++) {
2368 int j;
2370 done = 1;
2371 closest = sig*(i/2) + ex_y;
2372 /* check if this slot is used */
2373 if (closest >= 0) {
2374 for (j = 0; j<dock->max_icons; j++) {
2375 if (dock->icon_array[j]
2376 && dock->icon_array[j]->yindex==closest) {
2377 /* slot is used by someone else */
2378 if (dock->icon_array[j]!=icon)
2379 done = 0;
2380 break;
2384 sig = -sig;
2386 if (done && closest >= 0 && closest <= max_y_icons &&
2387 ((ex_y >= closest && ex_y - closest < DOCK_DETTACH_THRESHOLD+1)
2389 (ex_y < closest && closest - ex_y <= DOCK_DETTACH_THRESHOLD+1))) {
2391 *ret_y = closest;
2393 return True;
2395 } else { /* !redocking */
2397 /* if slot is free and the icon is close enough, return it */
2398 if (!aicon && ex_x == 0 && ex_y >= 0 && ex_y <= max_y_icons) {
2399 *ret_y = ex_y;
2400 return True;
2403 } else { /* CLIP */
2404 int neighbours = 0;
2406 for (i=0; i<dock->max_icons; i++) {
2407 nicon = dock->icon_array[i];
2408 if (nicon && nicon->xindex == ex_x && nicon->yindex == ex_y) {
2409 aicon = nicon;
2410 break;
2414 for (i=0; i<dock->max_icons; i++) {
2415 nicon = dock->icon_array[i];
2416 if (nicon && nicon != icon && /* Icon can't be it's own neighbour */
2417 (abs(nicon->xindex - ex_x) <= CLIP_ATTACH_VICINITY &&
2418 abs(nicon->yindex - ex_y) <= CLIP_ATTACH_VICINITY)) {
2419 neighbours = 1;
2420 break;
2424 if (neighbours && (aicon==NULL || (redocking && aicon == icon))) {
2425 *ret_x = ex_x;
2426 *ret_y = ex_y;
2427 return True;
2430 return False;
2433 #define MIN(x, y) ((x) > (y) ? (y) : (x))
2434 #define MAX(x, y) ((x) < (y) ? (y) : (x))
2436 #define ON_SCREEN(x, y, sx, ex, sy, ey) \
2437 ((((x)+ICON_SIZE/2) >= (sx)) && (((y)+ICON_SIZE/2) >= (sy)) && \
2438 (((x) + (ICON_SIZE/2)) <= (ex)) && (((y) + (ICON_SIZE/2)) <= ey))
2442 * returns true if it can find a free slot in the dock,
2443 * in which case it changes x_pos and y_pos accordingly.
2444 * Else returns false.
2446 Bool
2447 wDockFindFreeSlot(WDock *dock, int *x_pos, int *y_pos)
2449 WScreen *scr = dock->screen_ptr;
2450 WAppIcon *btn;
2451 unsigned char *slot_map;
2452 int mwidth;
2453 int r;
2454 int x, y;
2455 int i, done = False;
2456 int corner;
2457 int sx=0, sy=0, ex=scr->scr_width, ey=scr->scr_height;
2460 /* if the dock is full */
2461 if (dock->icon_count >= dock->max_icons) {
2462 return False;
2465 if (!wPreferences.flags.nodock && scr->dock) {
2466 if (scr->dock->on_right_side)
2467 ex -= ICON_SIZE + DOCK_EXTRA_SPACE;
2468 else
2469 sx += ICON_SIZE + DOCK_EXTRA_SPACE;
2472 if (ex < dock->x_pos)
2473 ex = dock->x_pos;
2474 if (sx > dock->x_pos+ICON_SIZE)
2475 sx = dock->x_pos+ICON_SIZE;
2476 #define C_NONE 0
2477 #define C_NW 1
2478 #define C_NE 2
2479 #define C_SW 3
2480 #define C_SE 4
2482 /* check if clip is in a corner */
2483 if (dock->type==WM_CLIP) {
2484 if (dock->x_pos < 1 && dock->y_pos < 1)
2485 corner = C_NE;
2486 else if (dock->x_pos < 1 && dock->y_pos >= (ey-ICON_SIZE))
2487 corner = C_SE;
2488 else if (dock->x_pos >= (ex-ICON_SIZE)&& dock->y_pos >= (ey-ICON_SIZE))
2489 corner = C_SW;
2490 else if (dock->x_pos >= (ex-ICON_SIZE) && dock->y_pos < 1)
2491 corner = C_NW;
2492 else
2493 corner = C_NONE;
2494 } else
2495 corner = C_NONE;
2497 /* If the clip is in the corner, use only slots that are in the border
2498 * of the screen */
2499 if (corner!=C_NONE) {
2500 char *hmap, *vmap;
2501 int hcount, vcount;
2503 hcount = MIN(dock->max_icons, scr->scr_width/ICON_SIZE);
2504 vcount = MIN(dock->max_icons, scr->scr_height/ICON_SIZE);
2505 hmap = wmalloc(hcount+1);
2506 memset(hmap, 0, hcount+1);
2507 vmap = wmalloc(vcount+1);
2508 memset(vmap, 0, vcount+1);
2510 /* mark used positions */
2511 switch (corner) {
2512 case C_NE:
2513 for (i=0; i<dock->max_icons; i++) {
2514 btn = dock->icon_array[i];
2515 if (!btn)
2516 continue;
2518 if (btn->xindex==0 && btn->yindex > 0 && btn->yindex < vcount)
2519 vmap[btn->yindex] = 1;
2520 else if (btn->yindex==0 && btn->xindex>0 && btn->xindex<hcount)
2521 hmap[btn->xindex] = 1;
2523 case C_NW:
2524 for (i=0; i<dock->max_icons; i++) {
2525 btn = dock->icon_array[i];
2526 if (!btn)
2527 continue;
2529 if (btn->xindex==0 && btn->yindex > 0 && btn->yindex < vcount)
2530 vmap[btn->yindex] = 1;
2531 else if (btn->yindex==0 && btn->xindex<0 &&btn->xindex>-hcount)
2532 hmap[-btn->xindex] = 1;
2534 case C_SE:
2535 for (i=0; i<dock->max_icons; i++) {
2536 btn = dock->icon_array[i];
2537 if (!btn)
2538 continue;
2540 if (btn->xindex==0 && btn->yindex < 0 && btn->yindex > -vcount)
2541 vmap[-btn->yindex] = 1;
2542 else if (btn->yindex==0 && btn->xindex>0 && btn->xindex<hcount)
2543 hmap[btn->xindex] = 1;
2545 case C_SW:
2546 default:
2547 for (i=0; i<dock->max_icons; i++) {
2548 btn = dock->icon_array[i];
2549 if (!btn)
2550 continue;
2552 if (btn->xindex==0 && btn->yindex < 0 && btn->yindex > -vcount)
2553 vmap[-btn->yindex] = 1;
2554 else if (btn->yindex==0 && btn->xindex<0 &&btn->xindex>-hcount)
2555 hmap[-btn->xindex] = 1;
2558 x=0; y=0;
2559 done = 0;
2560 /* search a vacant slot */
2561 for (i=1; i<MAX(vcount, hcount); i++) {
2562 if (i < vcount && vmap[i]==0) {
2563 /* found a slot */
2564 x = 0;
2565 y = i;
2566 done = 1;
2567 break;
2568 } else if (i < hcount && hmap[i]==0) {
2569 /* found a slot */
2570 x = i;
2571 y = 0;
2572 done = 1;
2573 break;
2576 free(vmap);
2577 free(hmap);
2578 /* If found a slot, translate and return */
2579 if (done) {
2580 if (corner==C_NW || corner==C_NE) {
2581 *y_pos = y;
2582 } else {
2583 *y_pos = -y;
2585 if (corner==C_NE || corner==C_SE) {
2586 *x_pos = x;
2587 } else {
2588 *x_pos = -x;
2590 return True;
2592 /* else, try to find a slot somewhere else */
2595 /* a map of mwidth x mwidth would be enough if we allowed icons to be
2596 * placed outside of screen */
2597 mwidth = (int)ceil(sqrt(dock->max_icons));
2599 /* In the worst case (the clip is in the corner of the screen),
2600 * the amount of icons that fit in the clip is smaller.
2601 * Double the map to get a safe value.
2603 mwidth += mwidth;
2605 r = (mwidth-1)/2;
2607 slot_map = wmalloc(mwidth*mwidth);
2608 memset(slot_map, 0, mwidth*mwidth);
2610 #define XY2OFS(x,y) (MAX(abs(x),abs(y)) > r) ? 0 : (((y)+r)*(mwidth)+(x)+r)
2612 /* mark used slots in the map. If the slot falls outside the map
2613 * (for example, when all icons are placed in line), ignore them. */
2614 for (i=0; i<dock->max_icons; i++) {
2615 btn = dock->icon_array[i];
2616 if (btn)
2617 slot_map[XY2OFS(btn->xindex, btn->yindex)] = 1;
2619 /* Find closest slot from the center that is free by scanning the
2620 * map from the center to outward in circular passes.
2621 * This will not result in a neat layout, but will be optimal
2622 * in the sense that there will not be holes left.
2624 done = 0;
2625 for (i = 1; i <= r && !done; i++) {
2626 int tx, ty;
2628 /* top and bottom parts of the ring */
2629 for (x = -i; x <= i && !done; x++) {
2630 tx = dock->x_pos + x*ICON_SIZE;
2631 y = -i;
2632 ty = dock->y_pos + y*ICON_SIZE;
2633 if (slot_map[XY2OFS(x,y)]==0
2634 && ON_SCREEN(tx, ty, sx, ex, sy, ey)) {
2635 *x_pos = x;
2636 *y_pos = y;
2637 done = 1;
2638 break;
2640 y = i;
2641 ty = dock->y_pos + y*ICON_SIZE;
2642 if (slot_map[XY2OFS(x,y)]==0
2643 && ON_SCREEN(tx, ty, sx, ex, sy, ey)) {
2644 *x_pos = x;
2645 *y_pos = y;
2646 done = 1;
2647 break;
2650 /* left and right parts of the ring */
2651 for (y = -i+1; y <= i-1; y++) {
2652 ty = dock->y_pos + y*ICON_SIZE;
2653 x = -i;
2654 tx = dock->x_pos + x*ICON_SIZE;
2655 if (slot_map[XY2OFS(x,y)]==0
2656 && ON_SCREEN(tx, ty, sx, ex, sy, ey)) {
2657 *x_pos = x;
2658 *y_pos = y;
2659 done = 1;
2660 break;
2662 x = i;
2663 tx = dock->x_pos + x*ICON_SIZE;
2664 if (slot_map[XY2OFS(x,y)]==0
2665 && ON_SCREEN(tx, ty, sx, ex, sy, ey)) {
2666 *x_pos = x;
2667 *y_pos = y;
2668 done = 1;
2669 break;
2673 free(slot_map);
2674 #undef XY2OFS
2675 return done;
2679 static void
2680 moveDock(WDock *dock, int new_x, int new_y)
2682 WAppIcon *btn;
2683 int i;
2685 dock->x_pos = new_x;
2686 dock->y_pos = new_y;
2687 for (i=0; i<dock->max_icons; i++) {
2688 btn = dock->icon_array[i];
2689 if (btn) {
2690 btn->x_pos = new_x + btn->xindex*ICON_SIZE;
2691 btn->y_pos = new_y + btn->yindex*ICON_SIZE;
2692 XMoveWindow(dpy, btn->icon->core->window, btn->x_pos, btn->y_pos);
2698 static void
2699 swapDock(WDock *dock)
2701 WScreen *scr = dock->screen_ptr;
2702 WAppIcon *btn;
2703 int x, i;
2706 if (dock->on_right_side) {
2707 x = dock->x_pos = scr->scr_width - ICON_SIZE - DOCK_EXTRA_SPACE;
2708 } else {
2709 x = dock->x_pos = DOCK_EXTRA_SPACE;
2712 for (i=0; i<dock->max_icons; i++) {
2713 btn = dock->icon_array[i];
2714 if (btn) {
2715 btn->x_pos = x;
2716 XMoveWindow(dpy, btn->icon->core->window, btn->x_pos, btn->y_pos);
2720 wScreenUpdateUsableArea(scr);
2724 static pid_t
2725 execCommand(WAppIcon *btn, char *command, WSavedState *state)
2727 WScreen *scr = btn->icon->core->screen_ptr;
2728 pid_t pid;
2729 char **argv;
2730 int argc;
2731 char *cmdline;
2733 cmdline = ExpandOptions(scr, command);
2735 if (scr->flags.dnd_data_convertion_status || !cmdline) {
2736 if (cmdline)
2737 free(cmdline);
2738 if (state)
2739 free(state);
2740 return 0;
2743 ParseCommand(cmdline, &argv, &argc);
2745 if (argv==NULL) {
2746 if (cmdline)
2747 free(cmdline);
2748 if (state)
2749 free(state);
2750 return 0;
2753 if ((pid=fork())==0) {
2754 char **args;
2755 int i;
2757 SetupEnvironment(scr);
2759 #ifdef HAVE_SETPGID
2760 setpgid(0, 0);
2761 #endif
2763 args = malloc(sizeof(char*)*(argc+1));
2764 if (!args)
2765 exit(111);
2766 for (i=0; i<argc; i++) {
2767 args[i] = argv[i];
2769 args[argc] = NULL;
2770 execvp(argv[0], args);
2771 exit(111);
2773 while (argc > 0)
2774 free(argv[--argc]);
2775 free(argv);
2777 if (pid > 0) {
2778 if (!state) {
2779 state = wmalloc(sizeof(WSavedState));
2780 memset(state, 0, sizeof(WSavedState));
2781 state->hidden = -1;
2782 state->miniaturized = -1;
2783 state->shaded = -1;
2784 if (btn->dock == scr->dock)
2785 state->workspace = -1;
2786 else
2787 state->workspace = scr->current_workspace;
2789 wWindowAddSavedState(btn->wm_instance, btn->wm_class, cmdline, pid,
2790 state);
2791 wAddDeathHandler(pid, (WDeathHandler*)trackDeadProcess,
2792 btn->dock);
2793 } else if (state) {
2794 free(state);
2796 free(cmdline);
2797 return pid;
2801 void
2802 wDockHideIcons(WDock *dock)
2804 int i;
2805 WAppIcon *btn;
2807 if (dock==NULL)
2808 return;
2810 btn = dock->icon_array[0];
2812 for (i=1; i<dock->max_icons; i++) {
2813 if (dock->icon_array[i])
2814 XUnmapWindow(dpy, dock->icon_array[i]->icon->core->window);
2816 dock->mapped = 0;
2818 dockIconPaint(btn);
2822 void
2823 wDockShowIcons(WDock *dock)
2825 int i, newlevel;
2826 WAppIcon *btn;
2828 if (dock==NULL)
2829 return;
2831 btn = dock->icon_array[0];
2832 moveDock(dock, btn->x_pos, btn->y_pos);
2834 newlevel = dock->lowered ? WMNormalLevel : WMDockLevel;
2835 ChangeStackingLevel(btn->icon->core, newlevel);
2837 for (i=1; i<dock->max_icons; i++) {
2838 if (dock->icon_array[i]) {
2839 MoveInStackListAbove(dock->icon_array[i]->icon->core,
2840 btn->icon->core);
2841 break;
2845 if (!dock->collapsed) {
2846 for (i=1; i<dock->max_icons; i++) {
2847 if (dock->icon_array[i]) {
2848 XMapWindow(dpy, dock->icon_array[i]->icon->core->window);
2852 dock->mapped = 1;
2854 dockIconPaint(btn);
2858 void
2859 wDockLower(WDock *dock)
2861 int i;
2863 for (i=0; i<dock->max_icons; i++) {
2864 if (dock->icon_array[i])
2865 wLowerFrame(dock->icon_array[i]->icon->core);
2870 void
2871 wDockRaise(WDock *dock)
2873 int i;
2875 for (i=dock->max_icons-1; i>=0; i--) {
2876 if (dock->icon_array[i])
2877 wRaiseFrame(dock->icon_array[i]->icon->core);
2882 void
2883 wDockRaiseLower(WDock *dock)
2885 if (!dock->icon_array[0]->icon->core->stacking->above
2886 ||(dock->icon_array[0]->icon->core->stacking->window_level
2887 !=dock->icon_array[0]->icon->core->stacking->above->stacking->window_level))
2888 wDockLower(dock);
2889 else
2890 wDockRaise(dock);
2894 void
2895 wDockFinishLaunch(WDock *dock, WAppIcon *icon)
2897 icon->launching = 0;
2898 icon->relaunching = 0;
2899 dockIconPaint(icon);
2903 WAppIcon*
2904 wDockFindIconFor(WDock *dock, Window window)
2906 WAppIcon *icon;
2907 int i;
2909 for (i=0; i<dock->max_icons; i++) {
2910 icon = dock->icon_array[i];
2911 if (icon && icon->main_window == window)
2912 return icon;
2914 return NULL;
2918 void
2919 wDockTrackWindowLaunch(WDock *dock, Window window)
2921 WAppIcon *icon;
2922 #ifdef REDUCE_APPICONS
2923 WAppIconAppList *tapplist;
2924 #endif
2925 char *wm_class, *wm_instance;
2926 int i;
2927 Bool firstPass = True;
2928 Bool found = False;
2929 char *command = NULL;
2932 int argc;
2933 char **argv;
2935 if (XGetCommand(dpy, window, &argv, &argc)) {
2936 if (argc > 0 && argv != NULL)
2937 command = FlattenStringList(argv,argc);
2938 if (argv) {
2939 XFreeStringList(argv);
2944 if (!PropGetWMClass(window, &wm_class, &wm_instance) ||
2945 (!wm_class && !wm_instance))
2946 return;
2948 retry:
2949 for (i=0; i<dock->max_icons; i++) {
2950 icon = dock->icon_array[i];
2951 if (!icon)
2952 continue;
2954 /* app is already attached to icon */
2955 if (icon->main_window == window) {
2956 found = True;
2957 break;
2960 if ((icon->wm_instance || icon->wm_class)
2961 && (icon->launching
2962 || (dock->screen_ptr->flags.startup && !icon->running))) {
2964 if (icon->wm_instance && wm_instance &&
2965 strcmp(icon->wm_instance, wm_instance)!=0) {
2966 continue;
2968 if (icon->wm_class && wm_class &&
2969 strcmp(icon->wm_class, wm_class)!=0) {
2970 continue;
2972 if (firstPass && command && strcmp(icon->command, command)!=0) {
2973 continue;
2976 if (!icon->relaunching) {
2977 WApplication *wapp;
2979 /* Possibly an application that was docked with dockit,
2980 * but the user did not update WMState to indicate that
2981 * it was docked by force */
2982 wapp = wApplicationOf(window);
2983 if (!wapp) {
2984 icon->forced_dock = 1;
2985 icon->running = 0;
2987 if (!icon->forced_dock)
2988 icon->main_window = window;
2990 #ifdef REDUCE_APPICONS
2991 tapplist = wmalloc(sizeof(WAppIconAppList));
2992 memset(tapplist, 0, sizeof(WAppIconAppList));
2993 tapplist->next = icon->applist;
2994 if (icon->applist)
2995 icon->applist->prev = tapplist;
2996 icon->applist = tapplist;
2997 tapplist->wapp = wApplicationOf(window);
2998 icon->num_apps++;
2999 #endif
3001 found = True;
3002 wDockFinishLaunch(dock, icon);
3003 break;
3007 if (firstPass && !found) {
3008 firstPass = False;
3009 goto retry;
3012 if (command)
3013 free(command);
3015 if (wm_class)
3016 XFree(wm_class);
3017 if (wm_instance)
3018 XFree(wm_instance);
3023 void
3024 wClipUpdateForWorkspaceChange(WScreen *scr, int workspace)
3026 if (!wPreferences.flags.noclip) {
3027 scr->clip_icon->dock = scr->workspaces[workspace]->clip;
3028 if (scr->current_workspace != workspace) {
3029 WDock *old_clip = scr->workspaces[scr->current_workspace]->clip;
3031 wDockHideIcons(old_clip);
3032 if (old_clip->auto_raise_lower) {
3033 if (old_clip->auto_raise_magic) {
3034 WMDeleteTimerHandler(old_clip->auto_raise_magic);
3035 old_clip->auto_raise_magic = NULL;
3037 wDockLower(old_clip);
3039 if (old_clip->auto_collapse) {
3040 if (old_clip->auto_expand_magic) {
3041 WMDeleteTimerHandler(old_clip->auto_expand_magic);
3042 old_clip->auto_expand_magic = NULL;
3044 old_clip->collapsed = 1;
3046 wDockShowIcons(scr->workspaces[workspace]->clip);
3048 if (scr->flags.clip_balloon_mapped)
3049 showClipBalloon(scr->clip_icon->dock, workspace);
3055 static void
3056 trackDeadProcess(pid_t pid, unsigned char status, WDock *dock)
3058 WAppIcon *icon;
3059 int i;
3061 for (i=0; i<dock->max_icons; i++) {
3062 icon = dock->icon_array[i];
3063 if (!icon)
3064 continue;
3066 if (icon->launching && icon->pid == pid) {
3067 if (!icon->relaunching) {
3068 icon->running = 0;
3069 icon->main_window = None;
3071 wDockFinishLaunch(dock, icon);
3072 icon->pid = 0;
3073 if (status==111) {
3074 char msg[PATH_MAX];
3075 #ifdef OFFIX_DND
3076 sprintf(msg, _("Could not execute command \"%s\""),
3077 icon->drop_launch && icon->dnd_command
3078 ? icon->dnd_command : icon->command);
3079 #else
3080 sprintf(msg, _("Could not execute command \"%s\""),
3081 icon->command);
3082 #endif
3083 wMessageDialog(dock->screen_ptr, _("Error"), msg,
3084 _("OK"), NULL, NULL);
3086 break;
3092 static void
3093 toggleLowered(WDock *dock)
3095 WAppIcon *tmp;
3096 int newlevel, i;
3098 /* lower/raise Dock */
3099 if (!dock->lowered) {
3100 newlevel = WMNormalLevel;
3101 dock->lowered = 1;
3102 } else {
3103 newlevel = WMDockLevel;
3104 dock->lowered = 0;
3107 for (i=0; i<dock->max_icons; i++) {
3108 tmp = dock->icon_array[i];
3109 if (!tmp)
3110 continue;
3112 ChangeStackingLevel(tmp->icon->core, newlevel);
3113 if (dock->lowered)
3114 wLowerFrame(tmp->icon->core);
3117 if (dock->type == WM_DOCK)
3118 wScreenUpdateUsableArea(dock->screen_ptr);
3122 static void
3123 toggleCollapsed(WDock *dock)
3125 if (dock->collapsed) {
3126 dock->collapsed = 0;
3127 wDockShowIcons(dock);
3129 else {
3130 dock->collapsed = 1;
3131 wDockHideIcons(dock);
3136 static void
3137 openDockMenu(WDock *dock, WAppIcon *aicon, XEvent *event)
3139 WScreen *scr = dock->screen_ptr;
3140 WObjDescriptor *desc;
3141 WMenuEntry *entry;
3142 WApplication *wapp = NULL;
3143 int index = 0;
3144 int x_pos;
3145 int appIsRunning = aicon->running && aicon->icon && aicon->icon->owner;
3147 if (dock->type == WM_DOCK) {
3148 /* keep on top */
3149 entry = dock->menu->entries[index];
3150 entry->flags.indicator_on = !dock->lowered;
3151 entry->clientdata = dock;
3152 } else {
3153 /* clip options */
3154 if (scr->clip_options)
3155 updateClipOptionsMenu(scr->clip_options, dock);
3157 /* Rename Workspace */
3158 entry = dock->menu->entries[++index];
3159 entry->clientdata = dock;
3161 /* select icon */
3162 entry = dock->menu->entries[++index];
3163 entry->clientdata = aicon;
3164 wMenuSetEnabled(dock->menu, index, aicon!=scr->clip_icon);
3166 /* (un)select all icons */
3167 entry = dock->menu->entries[++index];
3168 entry->clientdata = aicon;
3169 wMenuSetEnabled(dock->menu, index, dock->icon_count > 1);
3171 /* keep icon(s) */
3172 entry = dock->menu->entries[++index];
3173 entry->clientdata = aicon;
3174 wMenuSetEnabled(dock->menu, index, dock->icon_count > 1);
3176 /* this is the workspace submenu part */
3177 if (scr->clip_submenu)
3178 updateWorkspaceMenu(scr->clip_submenu, aicon);
3179 index++;
3181 /* remove icon(s) */
3182 entry = dock->menu->entries[++index];
3183 entry->clientdata = aicon;
3184 wMenuSetEnabled(dock->menu, index, dock->icon_count > 1);
3186 /* attract icon(s) */
3187 entry = dock->menu->entries[++index];
3188 entry->clientdata = aicon;
3191 /* launch */
3192 entry = dock->menu->entries[++index];
3193 entry->clientdata = aicon;
3194 wMenuSetEnabled(dock->menu, index, aicon->command!=NULL);
3196 /* unhide here */
3197 entry = dock->menu->entries[++index];
3198 entry->clientdata = aicon;
3199 wMenuSetEnabled(dock->menu, index, appIsRunning);
3201 /* hide */
3202 entry = dock->menu->entries[++index];
3203 entry->clientdata = aicon;
3204 if (aicon->icon->owner) {
3205 wapp = wApplicationOf(aicon->icon->owner->main_window);
3206 if (wapp && wapp->flags.hidden)
3207 entry->text = _("Unhide");
3208 else
3209 entry->text = _("Hide");
3210 } else {
3211 entry->text = _("Hide");
3213 wMenuSetEnabled(dock->menu, index, appIsRunning);
3215 /* settings */
3216 entry = dock->menu->entries[++index];
3217 entry->clientdata = aicon;
3218 wMenuSetEnabled(dock->menu, index, !aicon->editing
3219 && !wPreferences.flags.noupdates);
3221 /* kill */
3222 entry = dock->menu->entries[++index];
3223 entry->clientdata = aicon;
3224 wMenuSetEnabled(dock->menu, index, appIsRunning);
3226 if (!dock->menu->flags.realized)
3227 wMenuRealize(dock->menu);
3229 if (dock->type == WM_CLIP) {
3230 x_pos = event->xbutton.x_root+2;
3231 } else {
3232 x_pos = dock->on_right_side ?
3233 scr->scr_width - dock->menu->frame->core->width - 2 : 0;
3236 wMenuMapAt(dock->menu, x_pos, event->xbutton.y_root+2, False);
3238 /* allow drag select */
3239 event->xany.send_event = True;
3240 desc = &dock->menu->menu->descriptor;
3241 (*desc->handle_mousedown)(desc, event);
3245 static void
3246 openClipWorkspaceMenu(WScreen *scr, int x, int y)
3248 if (!scr->clip_ws_menu) {
3249 scr->clip_ws_menu = wWorkspaceMenuMake(scr, False);
3251 wWorkspaceMenuUpdate(scr, scr->clip_ws_menu);
3252 wMenuMapAt(scr->clip_ws_menu, x, y, False);
3256 /******************************************************************/
3257 static void
3258 iconDblClick(WObjDescriptor *desc, XEvent *event)
3260 WAppIcon *btn = desc->parent;
3261 WDock *dock = btn->dock;
3262 WApplication *wapp = NULL;
3263 int unhideHere = 0;
3265 #ifdef REDUCE_APPICONS
3266 if ((btn->icon->owner && !(event->xbutton.state & ControlMask)) ||
3267 ((btn->icon->owner == NULL) && (btn->applist != NULL))) {
3268 if (btn->icon->owner == NULL)
3269 btn->icon->owner = btn->applist->wapp->main_window_desc;
3270 #ifdef I_HATE_THIS
3272 #endif
3273 #else
3274 if (btn->icon->owner && !(event->xbutton.state & ControlMask)) {
3275 #endif
3276 wapp = wApplicationOf(btn->icon->owner->main_window);
3278 assert(wapp!=NULL);
3280 unhideHere = (event->xbutton.state & ShiftMask);
3282 /* go to the last workspace that the user worked on the app */
3283 if (!unhideHere) {
3284 wWorkspaceChange(dock->screen_ptr, wapp->last_workspace);
3287 wUnhideApplication(wapp, event->xbutton.button==Button2,
3288 unhideHere);
3290 if (event->xbutton.state & MOD_MASK) {
3291 wHideOtherApplications(btn->icon->owner);
3293 } else {
3294 if (event->xbutton.button==Button1) {
3296 if (event->xbutton.state & MOD_MASK) {
3297 /* raise/lower dock */
3298 toggleLowered(dock);
3299 } else if (btn == dock->screen_ptr->clip_icon) {
3300 if (getClipButton(event->xbutton.x, event->xbutton.y)==CLIP_IDLE)
3301 toggleCollapsed(dock);
3302 else
3303 handleClipChangeWorkspace(dock->screen_ptr, event);
3304 } else if (btn->command) {
3305 if (!btn->launching &&
3306 (!btn->running || (event->xbutton.state & ControlMask))) {
3307 launchDockedApplication(btn);
3315 static void
3316 handleDockMove(WDock *dock, WAppIcon *aicon, XEvent *event)
3318 WScreen *scr = dock->screen_ptr;
3319 int ofs_x=event->xbutton.x, ofs_y=event->xbutton.y;
3320 int x, y;
3321 XEvent ev;
3322 int grabbed = 0, swapped = 0, done;
3323 Pixmap ghost = None;
3324 int superfluous = wPreferences.superfluous; /* we catch it to avoid problems */
3326 #ifdef DEBUG
3327 puts("moving dock");
3328 #endif
3329 if (XGrabPointer(dpy, aicon->icon->core->window, True, ButtonMotionMask
3330 |ButtonReleaseMask|ButtonPressMask, GrabModeAsync,
3331 GrabModeAsync, None, None, CurrentTime) !=GrabSuccess) {
3332 wwarning("pointer grab failed for dock move");
3334 y = 0;
3335 for (x=0; x<dock->max_icons; x++) {
3336 if (dock->icon_array[x]!=NULL &&
3337 dock->icon_array[x]->yindex > y)
3338 y = dock->icon_array[x]->yindex;
3340 y++;
3341 XResizeWindow(dpy, scr->dock_shadow, ICON_SIZE, ICON_SIZE*y);
3343 done = 0;
3344 while (!done) {
3345 WMMaskEvent(dpy, PointerMotionMask|ButtonReleaseMask|ButtonPressMask
3346 |ButtonMotionMask|ExposureMask, &ev);
3347 switch (ev.type) {
3348 case Expose:
3349 WMHandleEvent(&ev);
3350 break;
3352 case MotionNotify:
3353 if (!grabbed) {
3354 if (abs(ofs_x-ev.xmotion.x)>=MOVE_THRESHOLD
3355 || abs(ofs_y-ev.xmotion.y)>=MOVE_THRESHOLD) {
3356 XChangeActivePointerGrab(dpy, ButtonMotionMask
3357 |ButtonReleaseMask|ButtonPressMask,
3358 wCursor[WCUR_MOVE], CurrentTime);
3359 grabbed=1;
3361 break;
3363 if (dock->type == WM_CLIP) {
3364 if (ev.xmotion.x_root - ofs_x < 0) {
3365 x = 0;
3366 } else if (ev.xmotion.x_root - ofs_x + ICON_SIZE >
3367 scr->scr_width) {
3368 x = scr->scr_width - ICON_SIZE;
3369 } else {
3370 x = ev.xmotion.x_root - ofs_x;
3372 if (ev.xmotion.y_root - ofs_y < 0) {
3373 y = 0;
3374 } else if (ev.xmotion.y_root - ofs_y + ICON_SIZE >
3375 scr->scr_height) {
3376 y = scr->scr_height - ICON_SIZE;
3377 } else {
3378 y = ev.xmotion.y_root - ofs_y;
3380 moveDock(dock, x, y);
3381 } else {
3382 /* move vertically if pointer is inside the dock*/
3383 if ((dock->on_right_side &&
3384 ev.xmotion.x_root >= dock->x_pos - ICON_SIZE)
3385 || (!dock->on_right_side &&
3386 ev.xmotion.x_root <= dock->x_pos + ICON_SIZE*2)) {
3388 if (ev.xmotion.y_root - ofs_y < 0) {
3389 y = 0;
3390 } else if (ev.xmotion.y_root - ofs_y + ICON_SIZE >
3391 scr->scr_height) {
3392 y = scr->scr_height - ICON_SIZE;
3393 } else {
3394 y = ev.xmotion.y_root - ofs_y;
3396 moveDock(dock, dock->x_pos, y);
3398 /* move horizontally to change sides */
3399 x = ev.xmotion.x_root - ofs_x;
3400 if (!dock->on_right_side) {
3402 /* is on left */
3404 if (ev.xmotion.x_root > dock->x_pos + ICON_SIZE*2) {
3405 XMoveWindow(dpy, scr->dock_shadow, scr->scr_width-ICON_SIZE
3406 -DOCK_EXTRA_SPACE-1, dock->y_pos);
3407 if (superfluous) {
3408 if (ghost==None) {
3409 ghost = MakeGhostDock(dock, dock->x_pos,
3410 scr->scr_width-ICON_SIZE
3411 -DOCK_EXTRA_SPACE-1,
3412 dock->y_pos);
3413 XSetWindowBackgroundPixmap(dpy, scr->dock_shadow,
3414 ghost);
3415 XClearWindow(dpy, scr->dock_shadow);
3418 XMapRaised(dpy, scr->dock_shadow);
3419 swapped = 1;
3420 } else {
3421 if (superfluous && ghost!=None) {
3422 XFreePixmap(dpy, ghost);
3423 ghost = None;
3425 XUnmapWindow(dpy, scr->dock_shadow);
3426 swapped = 0;
3428 } else {
3429 /* is on right */
3430 if (ev.xmotion.x_root < dock->x_pos - ICON_SIZE) {
3431 XMoveWindow(dpy, scr->dock_shadow,
3432 DOCK_EXTRA_SPACE, dock->y_pos);
3433 if (superfluous) {
3434 if (ghost==None) {
3435 ghost = MakeGhostDock(dock, dock->x_pos,
3436 DOCK_EXTRA_SPACE, dock->y_pos);
3437 XSetWindowBackgroundPixmap(dpy, scr->dock_shadow,
3438 ghost);
3439 XClearWindow(dpy, scr->dock_shadow);
3442 XMapRaised(dpy, scr->dock_shadow);
3443 swapped = -1;
3444 } else {
3445 XUnmapWindow(dpy, scr->dock_shadow);
3446 swapped = 0;
3447 if (superfluous && ghost!=None) {
3448 XFreePixmap(dpy, ghost);
3449 ghost = None;
3454 break;
3456 case ButtonPress:
3457 break;
3459 case ButtonRelease:
3460 if (ev.xbutton.button != event->xbutton.button)
3461 break;
3462 XUngrabPointer(dpy, CurrentTime);
3463 XUnmapWindow(dpy, scr->dock_shadow);
3464 XResizeWindow(dpy, scr->dock_shadow, ICON_SIZE, ICON_SIZE);
3465 if (dock->type == WM_DOCK) {
3466 if (swapped!=0) {
3467 if (swapped>0)
3468 dock->on_right_side = 1;
3469 else
3470 dock->on_right_side = 0;
3471 swapDock(dock);
3472 wArrangeIcons(scr, False);
3475 done = 1;
3476 break;
3479 if (superfluous) {
3480 if (ghost!=None)
3481 XFreePixmap(dpy, ghost);
3482 XSetWindowBackground(dpy, scr->dock_shadow, scr->white_pixel);
3484 #ifdef DEBUG
3485 puts("End dock move");
3486 #endif
3491 static void
3492 handleIconMove(WDock *dock, WAppIcon *aicon, XEvent *event)
3494 WScreen *scr = dock->screen_ptr;
3495 Window wins[2];
3496 WIcon *icon = aicon->icon;
3497 WDock *dock2 = NULL, *last_dock = dock, *clip = NULL;
3498 int ondock, grabbed = 0, change_dock = 0, collapsed = 0;
3499 XEvent ev;
3500 int x = aicon->x_pos, y = aicon->y_pos;
3501 int ofs_x = event->xbutton.x, ofs_y = event->xbutton.y;
3502 int shad_x = x, shad_y = y;
3503 int ix = aicon->xindex, iy = aicon->yindex;
3504 int tmp;
3505 Pixmap ghost = None;
3506 Bool docked;
3507 int superfluous = wPreferences.superfluous; /* we catch it to avoid problems */
3509 if (wPreferences.flags.noupdates)
3510 return;
3512 if (XGrabPointer(dpy, icon->core->window, True, ButtonMotionMask
3513 |ButtonReleaseMask|ButtonPressMask, GrabModeAsync,
3514 GrabModeAsync, None, None, CurrentTime) !=GrabSuccess) {
3515 #ifdef DEBUG0
3516 wwarning("pointer grab failed for icon move");
3517 #endif
3520 wRaiseFrame(icon->core);
3522 if (!wPreferences.flags.noclip)
3523 clip = scr->workspaces[scr->current_workspace]->clip;
3525 if (dock == scr->dock && !wPreferences.flags.noclip)
3526 dock2 = clip;
3527 else if (dock != scr->dock && !wPreferences.flags.nodock)
3528 dock2 = scr->dock;
3530 wins[0] = icon->core->window;
3531 wins[1] = scr->dock_shadow;
3532 XRestackWindows(dpy, wins, 2);
3533 XMoveResizeWindow(dpy, scr->dock_shadow, aicon->x_pos, aicon->y_pos,
3534 ICON_SIZE, ICON_SIZE);
3535 if (superfluous) {
3536 if (icon->pixmap!=None)
3537 ghost = MakeGhostIcon(scr, icon->pixmap);
3538 else
3539 ghost = MakeGhostIcon(scr, icon->core->window);
3541 XSetWindowBackgroundPixmap(dpy, scr->dock_shadow, ghost);
3542 XClearWindow(dpy, scr->dock_shadow);
3544 XMapWindow(dpy, scr->dock_shadow);
3546 ondock = 1;
3549 while(1) {
3550 XMaskEvent(dpy, PointerMotionMask|ButtonReleaseMask|ButtonPressMask
3551 |ButtonMotionMask|ExposureMask, &ev);
3552 switch (ev.type) {
3553 case Expose:
3554 WMHandleEvent(&ev);
3555 break;
3557 case MotionNotify:
3558 if (!grabbed) {
3559 if (abs(ofs_x-ev.xmotion.x)>=MOVE_THRESHOLD
3560 || abs(ofs_y-ev.xmotion.y)>=MOVE_THRESHOLD) {
3561 XChangeActivePointerGrab(dpy, ButtonMotionMask
3562 |ButtonReleaseMask|ButtonPressMask,
3563 wCursor[WCUR_MOVE], CurrentTime);
3564 grabbed=1;
3565 } else {
3566 break;
3570 x = ev.xmotion.x_root - ofs_x;
3571 y = ev.xmotion.y_root - ofs_y;
3572 tmp = wDockSnapIcon(dock, aicon, x, y, &ix, &iy, True);
3573 if (tmp && dock2) {
3574 change_dock = 0;
3575 if (last_dock != dock && collapsed) {
3576 last_dock->collapsed = 1;
3577 wDockHideIcons(last_dock);
3578 collapsed = 0;
3580 if (!collapsed && (collapsed = dock->collapsed)) {
3581 dock->collapsed = 0;
3582 wDockShowIcons(dock);
3584 if (dock->auto_raise_lower)
3585 wDockRaise(dock);
3586 last_dock = dock;
3588 else if (dock2) {
3589 tmp = wDockSnapIcon(dock2, aicon, x, y, &ix, &iy, False);
3590 if (tmp) {
3591 change_dock = 1;
3592 if (last_dock != dock2 && collapsed) {
3593 last_dock->collapsed = 1;
3594 wDockHideIcons(last_dock);
3595 collapsed = 0;
3597 if (!collapsed && (collapsed = dock2->collapsed)) {
3598 dock2->collapsed = 0;
3599 wDockShowIcons(dock2);
3601 if (dock2->auto_raise_lower)
3602 wDockRaise(dock2);
3603 last_dock = dock2;
3606 if (aicon->launching
3607 || (aicon->running && !(ev.xmotion.state & MOD_MASK))
3608 || (!aicon->running && tmp)) {
3609 shad_x = last_dock->x_pos + ix*wPreferences.icon_size;
3610 shad_y = last_dock->y_pos + iy*wPreferences.icon_size;
3612 XMoveWindow(dpy, scr->dock_shadow, shad_x, shad_y);
3614 if (!ondock) {
3615 XMapWindow(dpy, scr->dock_shadow);
3616 #if 0
3617 if (!collapsed && (collapsed = last_dock->collapsed)) {
3618 last_dock->collapsed = 0;
3619 wDockShowIcons(last_dock);
3621 #endif
3623 ondock = 1;
3624 } else {
3625 if (ondock) {
3626 XUnmapWindow(dpy, scr->dock_shadow);
3627 #if 0
3628 if (last_dock && collapsed &&
3629 aicon->running && (ev.xmotion.state & MOD_MASK)) {
3630 last_dock->collapsed = 1;
3631 wDockHideIcons(last_dock);
3632 collapsed = 0;
3634 #endif
3636 ondock = 0;
3638 XMoveWindow(dpy, icon->core->window, x, y);
3639 break;
3641 case ButtonPress:
3642 break;
3644 case ButtonRelease:
3645 if (ev.xbutton.button != event->xbutton.button)
3646 break;
3647 XUngrabPointer(dpy, CurrentTime);
3648 if (ondock) {
3649 SlideWindow(icon->core->window, x, y, shad_x, shad_y);
3650 XUnmapWindow(dpy, scr->dock_shadow);
3651 if (!change_dock) {
3652 reattachIcon(dock, aicon, ix, iy);
3653 if (clip && dock!=clip && clip->auto_raise_lower)
3654 wDockLower(clip);
3655 } else {
3656 docked = moveIconBetweenDocks(dock, dock2, aicon, ix, iy);
3657 if (!docked) {
3658 /* Slide it back if dock rejected it */
3659 SlideWindow(icon->core->window, x, y, aicon->x_pos,
3660 aicon->y_pos);
3661 reattachIcon(dock, aicon, aicon->xindex,aicon->yindex);
3663 if (last_dock->type==WM_CLIP && last_dock->auto_collapse) {
3664 collapsed = 0;
3667 } else {
3668 aicon->x_pos = x;
3669 aicon->y_pos = y;
3670 if (superfluous) {
3671 if (!aicon->running && !wPreferences.no_animations) {
3672 /* We need to deselect it, even if is deselected in
3673 * wDockDetach(), because else DoKaboom() will fail.
3675 if (aicon->icon->selected)
3676 wIconSelect(aicon->icon);
3677 DoKaboom(scr,aicon->icon->core->window, x, y);
3680 if (clip && clip->auto_raise_lower)
3681 wDockLower(clip);
3682 wDockDetach(dock, aicon);
3684 if (collapsed) {
3685 last_dock->collapsed = 1;
3686 wDockHideIcons(last_dock);
3687 collapsed = 0;
3689 if (superfluous) {
3690 if (ghost!=None)
3691 XFreePixmap(dpy, ghost);
3692 XSetWindowBackground(dpy, scr->dock_shadow, scr->white_pixel);
3694 #ifdef DEBUG
3695 puts("End icon move");
3696 #endif
3697 return;
3703 static int
3704 getClipButton(int px, int py)
3706 int pt = (CLIP_BUTTON_SIZE+2)*ICON_SIZE/64;
3708 if (px < 0 || py < 0 || px >= ICON_SIZE || py >= ICON_SIZE)
3709 return CLIP_IDLE;
3711 if (py <= pt-((int)ICON_SIZE-1-px))
3712 return CLIP_FORWARD;
3713 else if (px <= pt-((int)ICON_SIZE-1-py))
3714 return CLIP_REWIND;
3716 return CLIP_IDLE;
3720 static void
3721 handleClipChangeWorkspace(WScreen *scr, XEvent *event)
3723 XEvent ev;
3724 int done, direction, new_ws;
3725 int new_dir;
3726 WDock *clip = scr->clip_icon->dock;
3728 direction = getClipButton(event->xbutton.x, event->xbutton.y);
3730 clip->lclip_button_pushed = direction==CLIP_REWIND;
3731 clip->rclip_button_pushed = direction==CLIP_FORWARD;
3733 wClipIconPaint(scr->clip_icon);
3734 done = 0;
3735 while(!done) {
3736 WMMaskEvent(dpy, ExposureMask|ButtonMotionMask|ButtonReleaseMask
3737 |ButtonPressMask, &ev);
3738 switch (ev.type) {
3739 case Expose:
3740 WMHandleEvent(&ev);
3741 break;
3743 case MotionNotify:
3744 new_dir = getClipButton(ev.xmotion.x, ev.xmotion.y);
3745 if (new_dir != direction) {
3746 direction = new_dir;
3747 clip->lclip_button_pushed = direction==CLIP_REWIND;
3748 clip->rclip_button_pushed = direction==CLIP_FORWARD;
3749 wClipIconPaint(scr->clip_icon);
3751 break;
3753 case ButtonPress:
3754 break;
3756 case ButtonRelease:
3757 if (ev.xbutton.button == event->xbutton.button)
3758 done = 1;
3762 clip->lclip_button_pushed = 0;
3763 clip->rclip_button_pushed = 0;
3765 new_ws = wPreferences.ws_advance || (event->xbutton.state & ControlMask);
3767 if (direction == CLIP_FORWARD) {
3768 if (scr->current_workspace < scr->workspace_count-1)
3769 wWorkspaceChange(scr, scr->current_workspace+1);
3770 else if (new_ws && scr->current_workspace < MAX_WORKSPACES-1)
3771 wWorkspaceChange(scr, scr->current_workspace+1);
3772 else if (wPreferences.ws_cycle)
3773 wWorkspaceChange(scr, 0);
3775 else if (direction == CLIP_REWIND) {
3776 if (scr->current_workspace > 0)
3777 wWorkspaceChange(scr, scr->current_workspace-1);
3778 else if (scr->current_workspace==0 && wPreferences.ws_cycle)
3779 wWorkspaceChange(scr, scr->workspace_count-1);
3782 wClipIconPaint(scr->clip_icon);
3786 static void
3787 iconMouseDown(WObjDescriptor *desc, XEvent *event)
3789 WAppIcon *aicon = desc->parent;
3790 WDock *dock = aicon->dock;
3791 WScreen *scr = aicon->icon->core->screen_ptr;
3793 if (aicon->editing || WCHECK_STATE(WSTATE_MODAL))
3794 return;
3796 scr->last_dock = dock;
3798 if (dock->menu->flags.mapped)
3799 wMenuUnmap(dock->menu);
3801 if (IsDoubleClick(scr, event)) {
3802 /* double-click was not in the main clip icon */
3803 if (dock->type != WM_CLIP || aicon->xindex!=0 || aicon->yindex!=0
3804 || getClipButton(event->xbutton.x, event->xbutton.y)==CLIP_IDLE) {
3805 iconDblClick(desc, event);
3806 return;
3810 if (dock->type == WM_CLIP && scr->flags.clip_balloon_mapped) {
3811 XUnmapWindow(dpy, scr->clip_balloon);
3812 scr->flags.clip_balloon_mapped = 0;
3815 #ifdef DEBUG
3816 puts("handling dock");
3817 #endif
3818 if (event->xbutton.button == Button1) {
3819 if (event->xbutton.state & MOD_MASK)
3820 wDockLower(dock);
3821 else
3822 wDockRaise(dock);
3824 if ((event->xbutton.state & ShiftMask) && aicon!=scr->clip_icon &&
3825 dock->type!=WM_DOCK) {
3826 wIconSelect(aicon->icon);
3827 return;
3830 if (aicon->yindex==0 && aicon->xindex==0) {
3831 if (getClipButton(event->xbutton.x, event->xbutton.y)!=CLIP_IDLE
3832 && dock->type==WM_CLIP)
3833 handleClipChangeWorkspace(scr, event);
3834 else
3835 handleDockMove(dock, aicon, event);
3836 } else
3837 handleIconMove(dock, aicon, event);
3839 } else if (event->xbutton.button==Button2 && dock->type==WM_CLIP &&
3840 aicon->xindex==0 && aicon->yindex==0) {
3841 openClipWorkspaceMenu(scr, event->xbutton.x_root+2,
3842 event->xbutton.y_root+2);
3843 if (scr->clip_ws_menu) {
3844 WMenu *menu;
3845 menu = scr->clip_ws_menu;
3846 desc = &menu->menu->descriptor;
3848 event->xany.send_event = True;
3849 (*desc->handle_mousedown)(desc, event);
3851 } else if (event->xbutton.button == Button3) {
3852 openDockMenu(dock, aicon, event);
3857 static void
3858 showClipBalloon(WDock *dock, int workspace)
3860 int w, h;
3861 int x, y;
3862 WScreen *scr = dock->screen_ptr;
3863 char *text;
3864 Window stack[2];
3866 scr->flags.clip_balloon_mapped = 1;
3867 XMapWindow(dpy, scr->clip_balloon);
3869 text = scr->workspaces[workspace]->name;
3871 w = wTextWidth(scr->clip_title_font->font, text, strlen(text));
3873 h = scr->clip_title_font->height;
3874 XResizeWindow(dpy, scr->clip_balloon, w, h);
3876 x = dock->x_pos + CLIP_BUTTON_SIZE*ICON_SIZE/64;
3877 y = dock->y_pos + ICON_SIZE-scr->clip_title_font->height - 3;
3879 if (x+w > scr->scr_width) {
3880 x = scr->scr_width - w;
3881 if (dock->y_pos + ICON_SIZE + h > scr->scr_height)
3882 y = dock->y_pos - h - 1;
3883 else
3884 y = dock->y_pos + ICON_SIZE;
3885 XRaiseWindow(dpy, scr->clip_balloon);
3886 } else {
3887 stack[0] = scr->clip_icon->icon->core->window;
3888 stack[1] = scr->clip_balloon;
3889 XRestackWindows(dpy, stack, 2);
3891 XMoveWindow(dpy, scr->clip_balloon, x, y);
3892 XSetForeground(dpy, scr->clip_title_gc,
3893 scr->clip_title_pixel[CLIP_NORMAL]);
3894 XClearWindow(dpy, scr->clip_balloon);
3895 wDrawString(scr->clip_balloon, scr->clip_title_font, scr->clip_title_gc,
3896 0, scr->clip_title_font->y, text, strlen(text));
3900 static void
3901 clipEnterNotify(WObjDescriptor *desc, XEvent *event)
3903 WAppIcon *btn = (WAppIcon*)desc->parent;
3904 WDock *dock;
3906 assert(event->type==EnterNotify);
3908 if(desc->parent_type!=WCLASS_DOCK_ICON)
3909 return;
3911 dock = btn->dock;
3912 if (!dock || dock->type!=WM_CLIP)
3913 return;
3915 /* The auto raise/lower code */
3916 if (dock->auto_lower_magic) {
3917 WMDeleteTimerHandler(dock->auto_lower_magic);
3918 dock->auto_lower_magic = NULL;
3920 if (dock->auto_raise_lower && !dock->auto_raise_magic) {
3921 dock->auto_raise_magic = WMAddTimerHandler(AUTO_RAISE_DELAY,
3922 clipAutoRaise,
3923 (void *)dock);
3926 /* The auto expand/collapse code */
3927 if (dock->auto_collapse_magic) {
3928 WMDeleteTimerHandler(dock->auto_collapse_magic);
3929 dock->auto_collapse_magic = NULL;
3931 if (dock->auto_collapse && !dock->auto_expand_magic) {
3932 dock->auto_expand_magic = WMAddTimerHandler(AUTO_EXPAND_DELAY,
3933 clipAutoExpand,
3934 (void *)dock);
3937 if (btn->xindex == 0 && btn->yindex == 0)
3938 showClipBalloon(dock, dock->screen_ptr->current_workspace);
3939 else {
3940 if (dock->screen_ptr->flags.clip_balloon_mapped) {
3941 XUnmapWindow(dpy, dock->screen_ptr->clip_balloon);
3942 dock->screen_ptr->flags.clip_balloon_mapped = 0;
3948 static void
3949 clipLeave(WDock *dock)
3951 XEvent event;
3952 WObjDescriptor *desc = NULL;
3954 if (!dock || dock->type!=WM_CLIP)
3955 return;
3957 if (XCheckTypedEvent(dpy, EnterNotify, &event)!=False) {
3958 if (XFindContext(dpy, event.xcrossing.window, wWinContext,
3959 (XPointer *)&desc)!=XCNOENT
3960 && desc && desc->parent_type==WCLASS_DOCK_ICON
3961 && ((WAppIcon*)desc->parent)->dock
3962 && ((WAppIcon*)desc->parent)->dock->type==WM_CLIP) {
3963 /* We didn't left the Clip yet */
3964 XPutBackEvent(dpy, &event);
3965 return;
3968 XPutBackEvent(dpy, &event);
3969 } else {
3970 /* We entered a withdrawn window, so we're still in Clip */
3971 return;
3974 if (dock->auto_raise_magic) {
3975 WMDeleteTimerHandler(dock->auto_raise_magic);
3976 dock->auto_raise_magic = NULL;
3978 if (dock->auto_raise_lower && !dock->auto_lower_magic) {
3979 dock->auto_lower_magic = WMAddTimerHandler(AUTO_LOWER_DELAY,
3980 clipAutoLower,
3981 (void *)dock);
3984 if (dock->auto_expand_magic) {
3985 WMDeleteTimerHandler(dock->auto_expand_magic);
3986 dock->auto_expand_magic = NULL;
3988 if (dock->auto_collapse && !dock->auto_collapse_magic) {
3989 dock->auto_collapse_magic = WMAddTimerHandler(AUTO_COLLAPSE_DELAY,
3990 clipAutoCollapse,
3991 (void *)dock);
3996 static void
3997 clipLeaveNotify(WObjDescriptor *desc, XEvent *event)
3999 WAppIcon *btn = (WAppIcon*)desc->parent;
4001 assert(event->type==LeaveNotify);
4003 if(desc->parent_type!=WCLASS_DOCK_ICON)
4004 return;
4006 clipLeave(btn->dock);
4010 static void
4011 clipAutoCollapse(void *cdata)
4013 WDock *dock = (WDock *)cdata;
4015 if (dock->type!=WM_CLIP || !dock->auto_collapse)
4016 return;
4018 if (dock->auto_collapse_magic) {
4019 dock->collapsed = 1;
4020 wDockHideIcons(dock);
4022 dock->auto_collapse_magic = NULL;
4026 static void
4027 clipAutoExpand(void *cdata)
4029 WDock *dock = (WDock *)cdata;
4031 if (dock->type!=WM_CLIP || !dock->auto_collapse)
4032 return;
4034 if (dock->auto_expand_magic) {
4035 dock->collapsed = 0;
4036 wDockShowIcons(dock);
4038 dock->auto_expand_magic = NULL;
4042 static void
4043 clipAutoLower(void *cdata)
4045 WDock *dock = (WDock *)cdata;
4047 if (dock->type!=WM_CLIP)
4048 return;
4050 if (dock->auto_raise_lower)
4051 wDockLower(dock);
4053 dock->auto_lower_magic = NULL;
4057 static void
4058 clipAutoRaise(void *cdata)
4060 WDock *dock = (WDock *)cdata;
4062 if (dock->type!=WM_CLIP)
4063 return;
4065 if (dock->auto_raise_lower)
4066 wDockRaise(dock);
4068 if (dock->screen_ptr->flags.clip_balloon_mapped) {
4069 showClipBalloon(dock, dock->screen_ptr->current_workspace);
4072 dock->auto_raise_magic = NULL;