Some stuff I forgot to clean after modifing the Clip's autocollapse code.
[wmaker-crm.git] / src / dock.c
blob829ade18ed618fd2ba6c67abfaa54326f6bdda76
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;
687 entry->flags.indicator_on = ((WDock*)entry->clientdata)->auto_collapse;
689 wMenuPaint(menu);
693 static void
694 toggleAutoRaiseLowerCallback(WMenu *menu, WMenuEntry *entry)
696 WDock *dock;
697 assert(entry->clientdata!=NULL);
699 dock = (WDock*) entry->clientdata;
701 dock->auto_raise_lower = !dock->auto_raise_lower;
703 entry->flags.indicator_on = ((WDock*)entry->clientdata)->auto_raise_lower;
705 wMenuPaint(menu);
709 static void
710 launchCallback(WMenu *menu, WMenuEntry *entry)
712 WAppIcon *btn = (WAppIcon*)entry->clientdata;
714 launchDockedApplication(btn);
718 static void
719 settingsCallback(WMenu *menu, WMenuEntry *entry)
721 WAppIcon *btn = (WAppIcon*)entry->clientdata;
723 if (btn->editing)
724 return;
725 ShowDockAppSettingsPanel(btn);
729 static void
730 hideCallback(WMenu *menu, WMenuEntry *entry)
732 WApplication *wapp;
733 WAppIcon *btn = (WAppIcon*)entry->clientdata;
735 wapp = wApplicationOf(btn->icon->owner->main_window);
737 if (wapp->flags.hidden) {
738 wWorkspaceChange(btn->icon->core->screen_ptr,wapp->last_workspace);
739 wUnhideApplication(wapp, False, False);
740 } else {
741 wHideApplication(wapp);
746 static void
747 unhideHereCallback(WMenu *menu, WMenuEntry *entry)
749 WApplication *wapp;
750 WAppIcon *btn = (WAppIcon*)entry->clientdata;
752 wapp = wApplicationOf(btn->icon->owner->main_window);
754 wUnhideApplication(wapp, False, True);
758 WAppIcon*
759 mainIconCreate(WScreen *scr, int type)
761 WAppIcon *btn;
762 int x_pos;
764 if (type == WM_CLIP) {
765 if (scr->clip_icon)
766 return scr->clip_icon;
767 btn = wAppIconCreateForDock(scr, NULL, "Logo", "WMClip", TILE_CLIP);
768 btn->icon->core->descriptor.handle_expose = clipIconExpose;
769 btn->icon->core->descriptor.handle_enternotify = clipEnterNotify;
770 btn->icon->core->descriptor.handle_leavenotify = clipLeaveNotify;
771 /*x_pos = scr->scr_width - ICON_SIZE*2 - DOCK_EXTRA_SPACE;*/
772 x_pos = 0;
773 } else {
774 btn = wAppIconCreateForDock(scr, NULL, "Logo", "WMDock", TILE_NORMAL);
775 x_pos = scr->scr_width - ICON_SIZE - DOCK_EXTRA_SPACE;
778 btn->xindex = 0;
779 btn->yindex = 0;
781 btn->icon->core->descriptor.handle_mousedown = iconMouseDown;
782 btn->icon->core->descriptor.parent_type = WCLASS_DOCK_ICON;
783 btn->icon->core->descriptor.parent = btn;
784 /*ChangeStackingLevel(btn->icon->core, WMDockLevel);*/
785 XMapWindow(dpy, btn->icon->core->window);
786 btn->x_pos = x_pos;
787 btn->y_pos = 0;
788 btn->docked = 1;
789 if (type == WM_CLIP)
790 scr->clip_icon = btn;
792 return btn;
796 static void
797 switchWSCommand(WMenu *menu, WMenuEntry *entry)
799 WAppIcon *btn, *icon = (WAppIcon*) entry->clientdata;
800 WScreen *scr = icon->icon->core->screen_ptr;
801 WDock *src, *dest;
802 LinkedList *selectedIcons;
803 int x, y;
805 if (entry->order == scr->current_workspace)
806 return;
807 src = icon->dock;
808 dest = scr->workspaces[entry->order]->clip;
810 selectedIcons = getSelected(src);
812 if (selectedIcons) {
813 while(selectedIcons) {
814 btn = selectedIcons->head;
815 if (wDockFindFreeSlot(dest, &x, &y)) {
816 moveIconBetweenDocks(src, dest, btn, x, y);
817 XUnmapWindow(dpy, btn->icon->core->window);
819 list_remove_head(&selectedIcons);
821 } else if (icon != scr->clip_icon) {
822 if (wDockFindFreeSlot(dest, &x, &y)) {
823 moveIconBetweenDocks(src, dest, icon, x, y);
824 XUnmapWindow(dpy, icon->icon->core->window);
831 static void
832 launchDockedApplication(WAppIcon *btn)
834 WScreen *scr = btn->icon->core->screen_ptr;
836 if (!btn->launching && btn->command!=NULL) {
837 if (!btn->forced_dock) {
838 btn->relaunching = btn->running;
839 btn->running = 1;
841 if (btn->wm_instance || btn->wm_class) {
842 WWindowAttributes attr;
843 memset(&attr, 0, sizeof(WWindowAttributes));
844 wDefaultFillAttributes(scr, btn->wm_instance, btn->wm_class,
845 &attr, NULL, True);
847 if (!attr.no_appicon && !btn->buggy_app)
848 btn->launching = 1;
849 else
850 btn->running = 0;
852 btn->drop_launch = 0;
853 scr->last_dock = btn->dock;
854 btn->pid = execCommand(btn, btn->command, NULL);
855 if (btn->pid>0) {
856 if (btn->buggy_app) {
857 /* give feedback that the app was launched */
858 btn->launching = 1;
859 dockIconPaint(btn);
860 btn->launching = 0;
861 WMAddTimerHandler(200, (WMCallback*)dockIconPaint, btn);
862 } else {
863 dockIconPaint(btn);
865 } else {
866 wwarning(_("could not launch application %s\n"), btn->command);
867 btn->launching = 0;
868 if (!btn->relaunching)
869 btn->running = 0;
876 static void
877 updateWorkspaceMenu(WMenu *menu, WAppIcon *icon)
879 WScreen *scr = menu->frame->screen_ptr;
880 char title[MAX_WORKSPACENAME_WIDTH+1];
881 int i;
883 if (!menu || !icon)
884 return;
886 for (i=0; i<scr->workspace_count; i++) {
887 if (i < menu->entry_no) {
888 if (strcmp(menu->entries[i]->text,scr->workspaces[i]->name)!=0) {
889 free(menu->entries[i]->text);
890 strcpy(title, scr->workspaces[i]->name);
891 menu->entries[i]->text = wstrdup(title);
892 menu->flags.realized = 0;
894 menu->entries[i]->clientdata = (void*)icon;
895 } else {
896 strcpy(title, scr->workspaces[i]->name);
898 wMenuAddCallback(menu, title, switchWSCommand, (void*)icon);
900 menu->flags.realized = 0;
902 if (i == scr->current_workspace) {
903 wMenuSetEnabled(menu, i, False);
904 } else {
905 wMenuSetEnabled(menu, i, True);
909 if (!menu->flags.realized)
910 wMenuRealize(menu);
914 static WMenu*
915 makeWorkspaceMenu(WScreen *scr)
917 WMenu *menu;
919 menu = wMenuCreate(scr, NULL, False);
920 if (!menu)
921 wwarning(_("could not create workspace submenu for Clip menu"));
923 wMenuAddCallback(menu, "", switchWSCommand, (void*)scr->clip_icon);
925 menu->flags.realized = 0;
926 wMenuRealize(menu);
928 return menu;
932 static void
933 updateClipOptionsMenu(WMenu *menu, WDock *dock)
935 WMenuEntry *entry;
936 int index = 0;
938 if (!menu || !dock)
939 return;
941 /* keep on top */
942 entry = menu->entries[index];
943 entry->flags.indicator_on = !dock->lowered;
944 entry->clientdata = dock;
946 /* collapsed */
947 entry = menu->entries[++index];
948 entry->flags.indicator_on = dock->collapsed;
949 entry->clientdata = dock;
951 /* auto-collapse */
952 entry = menu->entries[++index];
953 entry->flags.indicator_on = dock->auto_collapse;
954 entry->clientdata = dock;
956 /* auto-raise/lower */
957 entry = menu->entries[++index];
958 entry->flags.indicator_on = dock->auto_raise_lower;
959 entry->clientdata = dock;
961 /* attract icons */
962 entry = menu->entries[++index];
963 entry->flags.indicator_on = dock->attract_icons;
964 entry->clientdata = dock;
966 /* keep attracted icons */
967 entry = menu->entries[++index];
968 entry->flags.indicator_on = dock->keep_attracted;
969 entry->clientdata = dock;
971 menu->flags.realized = 0;
972 wMenuRealize(menu);
976 static WMenu*
977 makeClipOptionsMenu(WScreen *scr)
979 WMenu *menu;
980 WMenuEntry *entry;
982 menu = wMenuCreate(scr, NULL, False);
983 if (!menu) {
984 wwarning(_("could not create options submenu for Clip menu"));
985 return NULL;
988 entry = wMenuAddCallback(menu, _("Keep on top"),
989 toggleLoweredCallback, NULL);
990 entry->flags.indicator = 1;
991 entry->flags.indicator_on = 1;
992 entry->flags.indicator_type = MI_CHECK;
994 entry = wMenuAddCallback(menu, _("Collapsed"),
995 toggleCollapsedCallback, NULL);
996 entry->flags.indicator = 1;
997 entry->flags.indicator_on = 1;
998 entry->flags.indicator_type = MI_CHECK;
1000 entry = wMenuAddCallback(menu, _("AutoCollapse"),
1001 toggleAutoCollapseCallback, NULL);
1002 entry->flags.indicator = 1;
1003 entry->flags.indicator_on = 1;
1004 entry->flags.indicator_type = MI_CHECK;
1006 entry = wMenuAddCallback(menu, _("AutoRaiseLower"),
1007 toggleAutoRaiseLowerCallback, NULL);
1008 entry->flags.indicator = 1;
1009 entry->flags.indicator_on = 1;
1010 entry->flags.indicator_type = MI_CHECK;
1012 entry = wMenuAddCallback(menu, _("AutoAttract Icons"),
1013 toggleAutoAttractCallback, NULL);
1014 entry->flags.indicator = 1;
1015 entry->flags.indicator_on = 1;
1016 entry->flags.indicator_type = MI_CHECK;
1018 entry = wMenuAddCallback(menu, _("Keep Attracted Icons"),
1019 toggleKeepCallback, NULL);
1020 entry->flags.indicator = 1;
1021 entry->flags.indicator_on = 1;
1022 entry->flags.indicator_type = MI_CHECK;
1024 menu->flags.realized = 0;
1025 wMenuRealize(menu);
1027 return menu;
1031 static WMenu*
1032 dockMenuCreate(WScreen *scr, int type)
1034 WMenu *menu;
1035 WMenuEntry *entry;
1037 if (type == WM_CLIP && scr->clip_menu)
1038 return scr->clip_menu;
1040 menu = wMenuCreate(scr, NULL, False);
1041 if (type != WM_CLIP) {
1042 entry = wMenuAddCallback(menu, _("Keep on top"),
1043 toggleLoweredCallback, NULL);
1044 entry->flags.indicator = 1;
1045 entry->flags.indicator_on = 1;
1046 entry->flags.indicator_type = MI_CHECK;
1047 } else {
1048 entry = wMenuAddCallback(menu, _("Clip Options"), NULL, NULL);
1049 scr->clip_options = makeClipOptionsMenu(scr);
1050 if (scr->clip_options)
1051 wMenuEntrySetCascade(menu, entry, scr->clip_options);
1053 wMenuAddCallback(menu, _("Rename Workspace"), renameCallback, NULL);
1055 wMenuAddCallback(menu, _("(Un)Select Icon"), selectCallback, NULL);
1057 wMenuAddCallback(menu, _("(Un)Select All Icons"), selectIconsCallback,
1058 NULL);
1060 wMenuAddCallback(menu, _("Keep Icon(s)"), keepIconsCallback, NULL);
1062 entry = wMenuAddCallback(menu, _("Move Icon(s) To"), NULL, NULL);
1063 scr->clip_submenu = makeWorkspaceMenu(scr);
1064 if (scr->clip_submenu)
1065 wMenuEntrySetCascade(menu, entry, scr->clip_submenu);
1067 wMenuAddCallback(menu, _("Remove Icon(s)"), removeIconsCallback, NULL);
1069 wMenuAddCallback(menu, _("Attract Icons"), colectIconsCallback, NULL);
1072 wMenuAddCallback(menu, _("Launch"), launchCallback, NULL);
1074 wMenuAddCallback(menu, _("Unhide Here"), unhideHereCallback, NULL);
1076 entry = wMenuAddCallback(menu, _("Hide"), hideCallback, NULL);
1077 free(entry->text);
1078 entry->text = _("Hide");
1080 wMenuAddCallback(menu, _("Settings..."), settingsCallback, NULL);
1082 wMenuAddCallback(menu, _("Kill"), killCallback, NULL);
1084 if (type == WM_CLIP)
1085 scr->clip_menu = menu;
1087 return menu;
1091 WDock*
1092 wDockCreate(WScreen *scr, int type)
1094 WDock *dock;
1095 WAppIcon *btn;
1096 int icon_count;
1098 make_keys();
1100 dock = wmalloc(sizeof(WDock));
1101 memset(dock, 0, sizeof(WDock));
1103 if (type == WM_CLIP)
1104 icon_count = CLIP_MAX_ICONS;
1105 else
1106 icon_count = scr->scr_height/wPreferences.icon_size;
1108 dock->icon_array = wmalloc(sizeof(WAppIcon*)*icon_count);
1109 memset(dock->icon_array, 0, sizeof(WAppIcon*)*icon_count);
1111 dock->max_icons = icon_count;
1113 btn = mainIconCreate(scr, type);
1115 btn->dock = dock;
1117 dock->x_pos = btn->x_pos;
1118 dock->y_pos = btn->y_pos;
1119 dock->screen_ptr = scr;
1120 dock->type = type;
1121 dock->icon_count = 1;
1122 dock->on_right_side = 1;
1123 dock->collapsed = 0;
1124 dock->auto_collapse = 0;
1125 dock->auto_collapse_magic = NULL;
1126 dock->auto_raise_lower = 0;
1127 dock->auto_lower_magic = NULL;
1128 dock->auto_raise_magic = NULL;
1129 dock->attract_icons = 0;
1130 dock->keep_attracted = 0;
1131 dock->lowered = 1;
1132 dock->icon_array[0] = btn;
1133 wRaiseFrame(btn->icon->core);
1134 XMoveWindow(dpy, btn->icon->core->window, btn->x_pos, btn->y_pos);
1136 /* create dock menu */
1137 dock->menu = dockMenuCreate(scr, type);
1139 return dock;
1143 void
1144 wDockDestroy(WDock *dock)
1146 int i;
1147 WAppIcon *aicon;
1149 for (i=(dock->type == WM_CLIP) ? 1 : 0; i<dock->max_icons; i++) {
1150 aicon = dock->icon_array[i];
1151 if (aicon) {
1152 int keepit = aicon->running && wApplicationOf(aicon->main_window);
1153 wDockDetach(dock, aicon);
1154 if (keepit) {
1155 PlaceIcon(dock->screen_ptr, &aicon->x_pos, &aicon->y_pos);
1156 XMoveWindow(dpy, aicon->icon->core->window,
1157 aicon->x_pos, aicon->y_pos);
1158 if (!dock->mapped || dock->collapsed)
1159 XMapWindow(dpy, aicon->icon->core->window);
1163 if (wPreferences.auto_arrange_icons)
1164 wArrangeIcons(dock->screen_ptr, True);
1165 free(dock->icon_array);
1166 if (dock->menu && dock->type!=WM_CLIP)
1167 wMenuDestroy(dock->menu, True);
1168 free(dock);
1172 void
1173 wClipIconPaint(WAppIcon *aicon)
1175 WScreen *scr = aicon->icon->core->screen_ptr;
1176 WWorkspace *workspace = scr->workspaces[scr->current_workspace];
1177 GC gc;
1178 Window win = aicon->icon->core->window;
1179 int length, nlength;
1180 char *ws_name, ws_number[10];
1181 int ty, tx;
1183 wIconPaint(aicon->icon);
1185 length = strlen(workspace->name);
1186 ws_name = malloc(length + 1);
1187 sprintf(ws_name, "%s", workspace->name);
1188 sprintf(ws_number, "%i", scr->current_workspace + 1);
1189 nlength = strlen(ws_number);
1191 gc = scr->clip_title_gc;
1193 if (!workspace->clip->collapsed)
1194 XSetForeground(dpy, gc, scr->clip_title_pixel[CLIP_NORMAL]);
1195 else
1196 XSetForeground(dpy, gc, scr->clip_title_pixel[CLIP_COLLAPSED]);
1198 ty = ICON_SIZE - scr->clip_title_font->height - 3;
1200 tx = CLIP_BUTTON_SIZE*ICON_SIZE/64;
1202 wDrawString(win, scr->clip_title_font, gc, tx,
1203 ty + scr->clip_title_font->y, ws_name, length);
1205 tx = (ICON_SIZE/2 - wTextWidth(scr->clip_title_font->font, ws_number, nlength))/2;
1207 wDrawString(win, scr->clip_title_font, gc, tx,
1208 scr->clip_title_font->y + 2, ws_number, nlength);
1210 free(ws_name);
1212 if (aicon->launching) {
1213 XFillRectangle(dpy, aicon->icon->core->window, scr->stipple_gc,
1214 0, 0, wPreferences.icon_size, wPreferences.icon_size);
1216 paintClipButtons(aicon, aicon->dock->lclip_button_pushed,
1217 aicon->dock->rclip_button_pushed);
1221 static void
1222 clipIconExpose(WObjDescriptor *desc, XEvent *event)
1224 wClipIconPaint(desc->parent);
1228 static void
1229 dockIconPaint(WAppIcon *btn)
1231 if (btn == btn->icon->core->screen_ptr->clip_icon)
1232 wClipIconPaint(btn);
1233 else
1234 wAppIconPaint(btn);
1238 static proplist_t
1239 make_icon_state(WAppIcon *btn)
1241 proplist_t node = NULL;
1242 proplist_t command, autolaunch, name, forced, host, position, buggy;
1243 char *tmp;
1244 char buffer[64];
1246 if (btn) {
1247 if (!btn->command)
1248 command = PLMakeString("-");
1249 else
1250 command = PLMakeString(btn->command);
1252 autolaunch = btn->auto_launch ? dYes : dNo;
1254 tmp = EscapeWM_CLASS(btn->wm_instance, btn->wm_class);
1256 name = PLMakeString(tmp);
1258 free(tmp);
1260 forced = btn->forced_dock ? dYes : dNo;
1262 buggy = btn->buggy_app ? dYes: dNo;
1264 if (btn == btn->icon->core->screen_ptr->clip_icon)
1265 sprintf(buffer, "%i,%i", btn->x_pos, btn->y_pos);
1266 else
1267 sprintf(buffer, "%hi,%hi", btn->xindex, btn->yindex);
1268 position = PLMakeString(buffer);
1270 node = PLMakeDictionaryFromEntries(dCommand, command,
1271 dName, name,
1272 dAutoLaunch, autolaunch,
1273 dForced, forced,
1274 dBuggyApplication, buggy,
1275 dPosition, position,
1276 NULL);
1277 PLRelease(command);
1278 PLRelease(name);
1279 PLRelease(position);
1280 #ifdef OFFIX_DND
1281 if (btn->dnd_command) {
1282 command = PLMakeString(btn->dnd_command);
1283 PLInsertDictionaryEntry(node, dDropCommand, command);
1284 PLRelease(command);
1286 #endif /* OFFIX_DND */
1288 if (btn->client_machine && btn->remote_start) {
1289 host = PLMakeString(btn->client_machine);
1290 PLInsertDictionaryEntry(node, dHost, host);
1291 PLRelease(host);
1295 return node;
1299 static proplist_t
1300 dockSaveState(WDock *dock)
1302 int i;
1303 proplist_t icon_info;
1304 proplist_t list=NULL, dock_state=NULL;
1305 proplist_t value;
1306 char buffer[256];
1308 list = PLMakeArrayFromElements(NULL);
1310 for (i=(dock->type==WM_DOCK ? 0 : 1); i<dock->max_icons; i++) {
1311 WAppIcon *btn = dock->icon_array[i];
1313 if (!btn || (btn->attracted && !dock->keep_attracted))
1314 continue;
1316 if ((icon_info = make_icon_state(dock->icon_array[i]))) {
1317 list = PLAppendArrayElement(list, icon_info);
1318 PLRelease(icon_info);
1322 dock_state = PLMakeDictionaryFromEntries(dApplications, list, NULL);
1324 PLRelease(list);
1326 if (dock->type == WM_DOCK) {
1327 sprintf(buffer, "%i,%i", (dock->on_right_side ? -ICON_SIZE : 0),
1328 dock->y_pos);
1329 value = PLMakeString(buffer);
1330 PLInsertDictionaryEntry(dock_state, dPosition, value);
1331 PLRelease(value);
1334 value = (dock->lowered ? dYes : dNo);
1335 PLInsertDictionaryEntry(dock_state, dLowered, value);
1337 if (dock->type == WM_CLIP) {
1338 value = (dock->collapsed ? dYes : dNo);
1339 PLInsertDictionaryEntry(dock_state, dCollapsed, value);
1341 value = (dock->auto_collapse ? dYes : dNo);
1342 PLInsertDictionaryEntry(dock_state, dAutoCollapse, value);
1344 value = (dock->auto_raise_lower ? dYes : dNo);
1345 PLInsertDictionaryEntry(dock_state, dAutoRaiseLower, value);
1347 value = (dock->attract_icons ? dYes : dNo);
1348 PLInsertDictionaryEntry(dock_state, dAutoAttractIcons, value);
1350 value = (dock->keep_attracted ? dYes : dNo);
1351 PLInsertDictionaryEntry(dock_state, dKeepAttracted, value);
1354 return dock_state;
1358 void
1359 wDockSaveState(WScreen *scr)
1361 proplist_t dock_state;
1363 dock_state = dockSaveState(scr->dock);
1365 PLInsertDictionaryEntry(scr->session_state, dDock, dock_state);
1367 PLRelease(dock_state);
1371 void
1372 wClipSaveState(WScreen *scr)
1374 proplist_t clip_state;
1376 clip_state = make_icon_state(scr->clip_icon);
1378 PLInsertDictionaryEntry(scr->session_state, dClip, clip_state);
1380 PLRelease(clip_state);
1384 proplist_t
1385 wClipSaveWorkspaceState(WScreen *scr, int workspace)
1387 return dockSaveState(scr->workspaces[workspace]->clip);
1391 static WAppIcon*
1392 restore_icon_state(WScreen *scr, proplist_t info, int type, int index)
1394 WAppIcon *aicon;
1395 char *wclass, *winstance;
1396 proplist_t cmd, value;
1397 char *command;
1400 cmd = PLGetDictionaryEntry(info, dCommand);
1401 if (!cmd || !PLIsString(cmd)) {
1402 return NULL;
1405 /* parse window name */
1406 value = PLGetDictionaryEntry(info, dName);
1407 if (!value)
1408 return NULL;
1410 ParseWindowName(value, &winstance, &wclass, "dock");
1412 if (!winstance && !wclass) {
1413 return NULL;
1416 /* get commands */
1418 if (cmd)
1419 command = wstrdup(PLGetString(cmd));
1420 else
1421 command = NULL;
1423 if (!command || strcmp(command, "-")==0) {
1424 if (command)
1425 free(command);
1426 if (wclass)
1427 free(wclass);
1428 if (winstance)
1429 free(winstance);
1431 return NULL;
1434 aicon = wAppIconCreateForDock(scr, command, winstance, wclass,
1435 TILE_NORMAL);
1436 if (wclass)
1437 free(wclass);
1438 if (winstance)
1439 free(winstance);
1441 aicon->icon->core->descriptor.handle_mousedown = iconMouseDown;
1442 if (type == WM_CLIP) {
1443 aicon->icon->core->descriptor.handle_enternotify = clipEnterNotify;
1444 aicon->icon->core->descriptor.handle_leavenotify = clipLeaveNotify;
1446 aicon->icon->core->descriptor.parent_type = WCLASS_DOCK_ICON;
1447 aicon->icon->core->descriptor.parent = aicon;
1450 #ifdef OFFIX_DND
1451 cmd = PLGetDictionaryEntry(info, dDropCommand);
1452 if (cmd)
1453 aicon->dnd_command = wstrdup(PLGetString(cmd));
1454 #endif
1456 /* check auto launch */
1457 value = PLGetDictionaryEntry(info, dAutoLaunch);
1459 aicon->auto_launch = 0;
1460 if (value) {
1461 if (PLIsString(value)) {
1462 if (strcasecmp(PLGetString(value), "YES")==0)
1463 aicon->auto_launch = 1;
1464 } else {
1465 wwarning(_("bad value in docked icon state info %s"),
1466 PLGetString(dAutoLaunch));
1470 /* check if it wasn't normally docked */
1471 value = PLGetDictionaryEntry(info, dForced);
1473 aicon->forced_dock = 0;
1474 if (value) {
1475 if (PLIsString(value)) {
1476 if (strcasecmp(PLGetString(value), "YES")==0)
1477 aicon->forced_dock = 1;
1478 } else {
1479 wwarning(_("bad value in docked icon state info %s"),
1480 PLGetString(dForced));
1484 /* check if we can rely on the stuff in the app */
1485 value = PLGetDictionaryEntry(info, dBuggyApplication);
1487 aicon->buggy_app = 0;
1488 if (value) {
1489 if (PLIsString(value)) {
1490 if (strcasecmp(PLGetString(value), "YES")==0)
1491 aicon->buggy_app = 1;
1492 } else {
1493 wwarning(_("bad value in docked icon state info %s"),
1494 PLGetString(dBuggyApplication));
1498 /* get position in the dock */
1499 value = PLGetDictionaryEntry(info, dPosition);
1500 if (value && PLIsString(value)) {
1501 if (sscanf(PLGetString(value), "%hi,%hi", &aicon->xindex,
1502 &aicon->yindex)!=2)
1503 wwarning(_("bad value in docked icon state info %s"),
1504 PLGetString(dPosition));
1506 /* check position sanity */
1507 /* incomplete section! */
1508 if (type == WM_DOCK) {
1509 aicon->xindex = 0;
1510 if (aicon->yindex < 0)
1511 wwarning(_("bad value in docked icon position %i,%i"),
1512 aicon->xindex, aicon->yindex);
1514 } else {
1515 aicon->yindex = index;
1516 aicon->xindex = 0;
1519 aicon->running = 0;
1520 aicon->docked = 1;
1522 return aicon;
1526 #define COMPLAIN(key) wwarning(_("bad value in dock state info:%s"), key)
1529 WAppIcon*
1530 wClipRestoreState(WScreen *scr, proplist_t clip_state)
1532 WAppIcon *icon;
1533 proplist_t value;
1536 icon = mainIconCreate(scr, WM_CLIP);
1538 if (!clip_state)
1539 return icon;
1540 else
1541 PLRetain(clip_state);
1543 /* restore position */
1545 value = PLGetDictionaryEntry(clip_state, dPosition);
1547 if (value) {
1548 if (!PLIsString(value))
1549 COMPLAIN("Position");
1550 else {
1551 if (sscanf(PLGetString(value), "%i,%i", &icon->x_pos,
1552 &icon->y_pos)!=2)
1553 COMPLAIN("Position");
1555 /* check position sanity */
1556 if (icon->y_pos < 0)
1557 icon->y_pos = 0;
1558 else if (icon->y_pos > scr->scr_height-ICON_SIZE)
1559 icon->y_pos = scr->scr_height-ICON_SIZE;
1561 if (icon->x_pos < 0)
1562 icon->x_pos = 0;
1563 else if (icon->x_pos > scr->scr_width-ICON_SIZE)
1564 icon->x_pos = scr->scr_width-ICON_SIZE;
1568 #ifdef OFFIX_DND
1569 value = PLGetDictionaryEntry(clip_state, dDropCommand);
1570 if (value && PLIsString(value))
1571 icon->dnd_command = wstrdup(PLGetString(value));
1572 #endif
1574 PLRelease(clip_state);
1576 return icon;
1580 WDock*
1581 wDockRestoreState(WScreen *scr, proplist_t dock_state, int type)
1583 WDock *dock;
1584 proplist_t apps;
1585 proplist_t value;
1586 WAppIcon *aicon, *old_top;
1587 int count, i;
1590 dock = wDockCreate(scr, type);
1592 if (!dock_state)
1593 return dock;
1595 if (dock_state)
1596 PLRetain(dock_state);
1599 /* restore position */
1601 value = PLGetDictionaryEntry(dock_state, dPosition);
1603 if (value) {
1604 if (!PLIsString(value))
1605 COMPLAIN("Position");
1606 else {
1607 if (sscanf(PLGetString(value), "%i,%i", &dock->x_pos,
1608 &dock->y_pos)!=2)
1609 COMPLAIN("Position");
1611 /* check position sanity */
1612 if (dock->y_pos < 0)
1613 dock->y_pos = 0;
1614 else if (dock->y_pos > scr->scr_height-ICON_SIZE)
1615 dock->y_pos = scr->scr_height - ICON_SIZE;
1617 /* This is no more needed. ??? */
1618 if (type == WM_CLIP) {
1619 if (dock->x_pos < 0)
1620 dock->x_pos = 0;
1621 else if (dock->x_pos > scr->scr_width-ICON_SIZE)
1622 dock->x_pos = scr->scr_width-ICON_SIZE;
1623 } else {
1624 if (dock->x_pos >= 0) {
1625 dock->x_pos = DOCK_EXTRA_SPACE;
1626 dock->on_right_side = 0;
1627 } else {
1628 dock->x_pos = scr->scr_width - DOCK_EXTRA_SPACE - ICON_SIZE;
1629 dock->on_right_side = 1;
1635 /* restore lowered/raised state */
1637 dock->lowered = 0;
1639 value = PLGetDictionaryEntry(dock_state, dLowered);
1641 if (value) {
1642 if (!PLIsString(value))
1643 COMPLAIN("Lowered");
1644 else {
1645 if (strcasecmp(PLGetString(value), "YES")==0)
1646 dock->lowered = 1;
1651 /* restore collapsed state */
1653 dock->collapsed = 0;
1655 value = PLGetDictionaryEntry(dock_state, dCollapsed);
1657 if (value) {
1658 if (!PLIsString(value))
1659 COMPLAIN("Collapsed");
1660 else {
1661 if (strcasecmp(PLGetString(value), "YES")==0)
1662 dock->collapsed = 1;
1667 /* restore auto-collapsed state */
1669 value = PLGetDictionaryEntry(dock_state, dAutoCollapse);
1671 if (value) {
1672 if (!PLIsString(value))
1673 COMPLAIN("AutoCollapse");
1674 else {
1675 if (strcasecmp(PLGetString(value), "YES")==0) {
1676 dock->auto_collapse = 1;
1677 dock->collapsed = 1;
1683 /* restore auto-raise/lower state */
1685 value = PLGetDictionaryEntry(dock_state, dAutoRaiseLower);
1687 if (value) {
1688 if (!PLIsString(value))
1689 COMPLAIN("AutoRaiseLower");
1690 else {
1691 if (strcasecmp(PLGetString(value), "YES")==0) {
1692 dock->auto_raise_lower = 1;
1698 /* restore attract icons state */
1700 dock->attract_icons = 0;
1702 value = PLGetDictionaryEntry(dock_state, dAutoAttractIcons);
1704 if (value) {
1705 if (!PLIsString(value))
1706 COMPLAIN("AutoAttractIcons");
1707 else {
1708 if (strcasecmp(PLGetString(value), "YES")==0)
1709 dock->attract_icons = 1;
1714 /* restore keep attracted icons state */
1716 dock->keep_attracted = 0;
1718 value = PLGetDictionaryEntry(dock_state, dKeepAttracted);
1720 if (value) {
1721 if (!PLIsString(value))
1722 COMPLAIN("KeepAttracted");
1723 else {
1724 if (strcasecmp(PLGetString(value), "YES")==0)
1725 dock->keep_attracted = 1;
1730 /* application list */
1732 apps = PLGetDictionaryEntry(dock_state, dApplications);
1734 if (!apps) {
1735 goto finish;
1738 count = PLGetNumberOfElements(apps);
1740 if (count==0)
1741 goto finish;
1743 old_top = dock->icon_array[0];
1745 /* dock->icon_count is set to 1 when dock is created.
1746 * Since Clip is already restored, we want to keep it so for clip,
1747 * but for dock we may change the default top tile, so we set it to 0.
1749 if (type == WM_DOCK)
1750 dock->icon_count = 0;
1752 for (i=0; i<count; i++) {
1753 if (dock->icon_count >= dock->max_icons) {
1754 wwarning(_("there are too many icons stored in dock. Ignoring what doesn't fit"));
1755 break;
1758 value = PLGetArrayElement(apps, i);
1759 aicon = restore_icon_state(scr, value, type, dock->icon_count);
1761 dock->icon_array[dock->icon_count] = aicon;
1763 if (aicon) {
1764 aicon->dock = dock;
1765 aicon->x_pos = dock->x_pos + (aicon->xindex*ICON_SIZE);
1766 aicon->y_pos = dock->y_pos + (aicon->yindex*ICON_SIZE);
1768 if (dock->lowered)
1769 ChangeStackingLevel(aicon->icon->core, WMNormalLevel);
1770 else
1771 ChangeStackingLevel(aicon->icon->core, WMDockLevel);
1773 wCoreConfigure(aicon->icon->core, aicon->x_pos, aicon->y_pos,
1774 0, 0);
1776 if (!dock->collapsed)
1777 XMapWindow(dpy, aicon->icon->core->window);
1778 wRaiseFrame(aicon->icon->core);
1780 dock->icon_count++;
1781 } else if (dock->icon_count==0 && type==WM_DOCK)
1782 dock->icon_count++;
1785 /* if the first icon is not defined, use the default */
1786 if (dock->icon_array[0]==NULL) {
1787 /* update default icon */
1788 old_top->x_pos = dock->x_pos;
1789 old_top->y_pos = dock->y_pos;
1790 if (dock->lowered)
1791 ChangeStackingLevel(old_top->icon->core, WMNormalLevel);
1792 else
1793 ChangeStackingLevel(old_top->icon->core, WMDockLevel);
1794 dock->icon_array[0] = old_top;
1795 XMoveWindow(dpy, old_top->icon->core->window, dock->x_pos, dock->y_pos);
1796 /* we don't need to increment dock->icon_count here because it was
1797 * incremented in the loop above.
1799 } else if (old_top!=dock->icon_array[0]) {
1800 if (old_top == scr->clip_icon)
1801 scr->clip_icon = dock->icon_array[0];
1802 wAppIconDestroy(old_top);
1805 finish:
1806 if (dock_state)
1807 PLRelease(dock_state);
1809 return dock;
1814 void
1815 wDockLaunchWithState(WDock *dock, WAppIcon *btn, WSavedState *state)
1817 if (btn && btn->command && !btn->running && !btn->launching) {
1819 btn->drop_launch = 0;
1821 btn->pid = execCommand(btn, btn->command, state);
1823 if (btn->pid>0) {
1824 if (!btn->forced_dock && !btn->buggy_app) {
1825 btn->launching = 1;
1826 dockIconPaint(btn);
1829 } else {
1830 free(state);
1835 void
1836 wDockDoAutoLaunch(WDock *dock, int workspace)
1838 WAppIcon *btn;
1839 WSavedState *state;
1840 int i;
1842 for (i = 0; i < dock->max_icons; i++) {
1843 btn = dock->icon_array[i];
1844 if (!btn || !btn->auto_launch)
1845 continue;
1847 state = wmalloc(sizeof(WSavedState));
1848 memset(state, 0, sizeof(WSavedState));
1849 state->workspace = workspace;
1850 /* TODO: this is klugy and is very difficult to understand
1851 * what's going on. Try to clean up */
1852 wDockLaunchWithState(dock, btn, state);
1856 #ifdef REDUCE_APPICONS
1857 void
1858 wDockSimulateLaunch(WDock *dock, WAppIcon *btn)
1860 if ((btn == NULL) || (dock == NULL))
1861 return;
1863 if (!btn->running) {
1864 if ((btn->icon->owner == NULL) && (btn->applist))
1865 btn->icon->owner = btn->applist->wapp->main_window_desc;
1866 if (!btn->forced_dock)
1867 btn->launching = 1;
1868 dockIconPaint(btn);
1869 wusleep(5000);
1872 #endif
1874 #ifdef OFFIX_DND
1875 static WDock*
1876 findDock(WScreen *scr, XEvent *event, int *icon_pos)
1878 WDock *dock;
1879 int i;
1881 *icon_pos = -1;
1882 if ((dock = scr->dock)!=NULL) {
1883 for (i=0; i<dock->max_icons; i++) {
1884 if (dock->icon_array[i]
1885 && dock->icon_array[i]->icon->core->window==event->xclient.window) {
1886 *icon_pos = i;
1887 break;
1891 if (*icon_pos<0 && (dock = scr->workspaces[scr->current_workspace]->clip)!=NULL) {
1892 for (i=0; i<dock->max_icons; i++) {
1893 if (dock->icon_array[i]
1894 && dock->icon_array[i]->icon->core->window==event->xclient.window) {
1895 *icon_pos = i;
1896 break;
1900 if(*icon_pos>=0)
1901 return dock;
1902 return NULL;
1907 wDockReceiveDNDDrop(WScreen *scr, XEvent *event)
1909 WDock *dock;
1910 WAppIcon *btn;
1911 int icon_pos;
1913 dock = findDock(scr, event, &icon_pos);
1914 if (!dock)
1915 return False;
1918 * Return True if the drop was on an application icon window.
1919 * In this case, let the ClientMessage handler redirect the
1920 * message to the app.
1922 if (dock->icon_array[icon_pos]->icon->icon_win!=None)
1923 return True;
1925 if (dock->icon_array[icon_pos]->dnd_command!=NULL) {
1926 scr->flags.dnd_data_convertion_status = 0;
1928 btn = dock->icon_array[icon_pos];
1930 if (!btn->forced_dock) {
1931 btn->relaunching = btn->running;
1932 btn->running = 1;
1934 if (btn->wm_instance || btn->wm_class) {
1935 WWindowAttributes attr;
1936 memset(&attr, 0, sizeof(WWindowAttributes));
1937 wDefaultFillAttributes(btn->icon->core->screen_ptr,
1938 btn->wm_instance,
1939 btn->wm_class, &attr, NULL, True);
1941 if (!attr.no_appicon)
1942 btn->launching = 1;
1943 else
1944 btn->running = 0;
1947 btn->drop_launch = 1;
1948 scr->last_dock = dock;
1949 btn->pid = execCommand(btn, btn->dnd_command, NULL);
1950 if (btn->pid>0) {
1951 dockIconPaint(btn);
1952 } else {
1953 btn->launching = 0;
1954 if (!btn->relaunching) {
1955 btn->running = 0;
1959 return False;
1961 #endif /* OFFIX_DND */
1965 Bool
1966 wDockAttachIcon(WDock *dock, WAppIcon *icon, int x, int y)
1968 WWindow *wwin;
1969 char **argv;
1970 int argc;
1971 int index;
1973 wwin = icon->icon->owner;
1974 if (icon->command==NULL) {
1975 icon->editing = 0;
1976 if (XGetCommand(dpy, wwin->client_win, &argv, &argc) && argc>0) {
1978 icon->command = FlattenStringList(argv, argc);
1979 XFreeStringList(argv);
1980 } else {
1981 char *command=NULL;
1983 /* icon->forced_dock = 1;*/
1984 if (!icon->attracted || dock->type!=WM_CLIP || dock->keep_attracted) {
1985 icon->editing = 1;
1986 if (wInputDialog(dock->screen_ptr, _("Dock Icon"),
1987 _("Type the command used to launch the application"),
1988 &command)) {
1989 if (command && (command[0]==0 ||
1990 (command[0]=='-' && command[1]==0))) {
1991 free(command);
1992 command = NULL;
1994 icon->command = command;
1995 icon->editing = 0;
1996 } else {
1997 icon->editing = 0;
1998 if (command)
1999 free(command);
2000 /* If the target is the dock, reject the icon. If
2001 * the target is the clip, make it an attracted icon
2003 if (dock->type==WM_CLIP) {
2004 icon->attracted = 1;
2005 if (!icon->icon->shadowed) {
2006 icon->icon->shadowed = 1;
2007 icon->icon->force_paint = 1;
2009 } else {
2010 return False;
2015 } else {
2016 icon->editing = 0;
2019 for (index=1; index<dock->max_icons; index++)
2020 if (dock->icon_array[index] == NULL)
2021 break;
2022 /* if (index == dock->max_icons)
2023 return; */
2025 assert(index < dock->max_icons);
2027 dock->icon_array[index] = icon;
2028 icon->yindex = y;
2029 icon->xindex = x;
2031 icon->x_pos = dock->x_pos + x*ICON_SIZE;
2032 icon->y_pos = dock->y_pos + y*ICON_SIZE;
2034 dock->icon_count++;
2036 icon->running = 1;
2037 icon->launching = 0;
2038 icon->docked = 1;
2039 icon->dock = dock;
2040 icon->icon->core->descriptor.handle_mousedown = iconMouseDown;
2041 if (dock->type == WM_CLIP) {
2042 icon->icon->core->descriptor.handle_enternotify = clipEnterNotify;
2043 icon->icon->core->descriptor.handle_leavenotify = clipLeaveNotify;
2045 icon->icon->core->descriptor.parent_type = WCLASS_DOCK_ICON;
2046 icon->icon->core->descriptor.parent = icon;
2048 MoveInStackListUnder(dock->icon_array[index-1]->icon->core,
2049 icon->icon->core);
2050 wAppIconMove(icon, icon->x_pos, icon->y_pos);
2051 wAppIconPaint(icon);
2053 if (wPreferences.auto_arrange_icons)
2054 wArrangeIcons(dock->screen_ptr, True);
2056 #ifdef OFFIX_DND
2057 if (icon->command && !icon->dnd_command) {
2058 icon->dnd_command = wmalloc(strlen(icon->command)+8);
2059 sprintf(icon->dnd_command, "%s %%d", icon->command);
2061 #endif
2063 return True;
2067 void
2068 reattachIcon(WDock *dock, WAppIcon *icon, int x, int y)
2070 int index;
2072 for(index=1; index<dock->max_icons; index++) {
2073 if(dock->icon_array[index] == icon)
2074 break;
2076 assert(index < dock->max_icons);
2078 icon->yindex = y;
2079 icon->xindex = x;
2081 icon->x_pos = dock->x_pos + x*ICON_SIZE;
2082 icon->y_pos = dock->y_pos + y*ICON_SIZE;
2086 Bool
2087 moveIconBetweenDocks(WDock *src, WDock *dest, WAppIcon *icon, int x, int y)
2089 WWindow *wwin;
2090 char **argv;
2091 int argc;
2092 int index;
2094 if (dest == NULL)
2095 return False;
2097 wwin = icon->icon->owner;
2100 * For the moment we can't do this if we move icons in Clip from one
2101 * workspace to other, because if we move two or more icons without
2102 * command, the dialog box will not be able to tell us to which of the
2103 * moved icons it applies. -Dan
2105 if ((dest->type==WM_DOCK /*|| dest->keep_attracted*/) && icon->command==NULL) {
2106 if (XGetCommand(dpy, wwin->client_win, &argv, &argc) && argc>0) {
2108 icon->command = FlattenStringList(argv, argc);
2109 XFreeStringList(argv);
2110 } else {
2111 char *command=NULL;
2113 icon->editing = 1;
2114 /* icon->forced_dock = 1;*/
2115 if (wInputDialog(src->screen_ptr, _("Dock Icon"),
2116 _("Type the command used to launch the application"),
2117 &command)) {
2118 if (command && (command[0]==0 ||
2119 (command[0]=='-' && command[1]==0))) {
2120 free(command);
2121 command = NULL;
2123 icon->command = command;
2124 } else {
2125 icon->editing = 0;
2126 if (command)
2127 free(command);
2128 return False;
2130 icon->editing = 0;
2134 for(index=1; index<src->max_icons; index++) {
2135 if(src->icon_array[index] == icon)
2136 break;
2138 assert(index < src->max_icons);
2140 src->icon_array[index] = NULL;
2141 src->icon_count--;
2143 for(index=1; index<dest->max_icons; index++) {
2144 if(dest->icon_array[index] == NULL)
2145 break;
2147 /* if (index == dest->max_icons)
2148 return; */
2150 assert(index < dest->max_icons);
2152 dest->icon_array[index] = icon;
2153 icon->dock = dest;
2155 /* deselect the icon */
2156 if (icon->icon->selected)
2157 wIconSelect(icon->icon);
2159 if (dest->type == WM_DOCK) {
2160 icon->icon->core->descriptor.handle_enternotify = NULL;
2161 icon->icon->core->descriptor.handle_leavenotify = NULL;
2162 } else {
2163 icon->icon->core->descriptor.handle_enternotify = clipEnterNotify;
2164 icon->icon->core->descriptor.handle_leavenotify = clipLeaveNotify;
2167 /* set it to be kept when moving to dock, or to a clip that keep the
2168 * attracted icons.
2169 * Unless the icon does not have a command set
2171 if (icon->command && (dest->type==WM_DOCK || dest->keep_attracted)) {
2172 icon->attracted = 0;
2173 if (icon->icon->shadowed) {
2174 icon->icon->shadowed = 0;
2175 icon->icon->force_paint = 1;
2179 if (src->auto_collapse || src->auto_raise_lower)
2180 clipLeave(src);
2182 icon->yindex = y;
2183 icon->xindex = x;
2185 icon->x_pos = dest->x_pos + x*ICON_SIZE;
2186 icon->y_pos = dest->y_pos + y*ICON_SIZE;
2188 dest->icon_count++;
2190 MoveInStackListUnder(dest->icon_array[index-1]->icon->core,
2191 icon->icon->core);
2192 wAppIconPaint(icon);
2194 return True;
2198 void
2199 wDockDetach(WDock *dock, WAppIcon *icon)
2201 int index;
2203 /* make the settings panel be closed */
2204 if (icon->panel) {
2205 DestroyDockAppSettingsPanel(icon->panel);
2208 icon->docked = 0;
2209 icon->dock = NULL;
2210 icon->attracted = 0;
2211 if (icon->icon->shadowed) {
2212 icon->icon->shadowed = 0;
2213 icon->icon->force_paint = 1;
2216 /* deselect the icon */
2217 if (icon->icon->selected)
2218 wIconSelect(icon->icon);
2220 if (icon->command) {
2221 free(icon->command);
2222 icon->command = NULL;
2224 #ifdef OFFIX_DND
2225 if (icon->dnd_command) {
2226 free(icon->dnd_command);
2227 icon->dnd_command = NULL;
2229 #endif
2231 for (index=1; index<dock->max_icons; index++)
2232 if (dock->icon_array[index] == icon)
2233 break;
2234 assert(index < dock->max_icons);
2235 dock->icon_array[index] = NULL;
2236 icon->yindex = -1;
2237 icon->xindex = -1;
2238 dock->icon_count--;
2240 /* if the dock is not attached to an application or
2241 * the the application did not set the approriate hints yet,
2242 * destroy the icon */
2243 #ifdef REDUCE_APPICONS
2244 if ((icon->num_apps == 0) && (!icon->running || !wApplicationOf(icon->main_window)) )
2245 #else
2246 if (!icon->running || !wApplicationOf(icon->main_window))
2247 #endif
2248 wAppIconDestroy(icon);
2249 else {
2250 icon->icon->core->descriptor.handle_mousedown = appIconMouseDown;
2251 icon->icon->core->descriptor.handle_enternotify = NULL;
2252 icon->icon->core->descriptor.handle_leavenotify = NULL;
2253 icon->icon->core->descriptor.parent_type = WCLASS_APPICON;
2254 icon->icon->core->descriptor.parent = icon;
2256 ChangeStackingLevel(icon->icon->core, NORMAL_ICON_LEVEL);
2258 wAppIconPaint(icon);
2259 if (wPreferences.auto_arrange_icons) {
2260 wArrangeIcons(dock->screen_ptr, True);
2263 if (dock->auto_collapse || dock->auto_raise_lower)
2264 clipLeave(dock);
2269 * returns the closest Dock slot index for the passed
2270 * coordinates.
2272 * Returns False if icon can't be docked.
2274 Bool
2275 wDockSnapIcon(WDock *dock, WAppIcon *icon, int req_x, int req_y,
2276 int *ret_x, int *ret_y, int redocking)
2278 WScreen *scr = dock->screen_ptr;
2279 int dx, dy;
2280 int ex_x, ex_y;
2281 int i, offset = ICON_SIZE/2;
2282 WAppIcon *aicon = NULL;
2283 WAppIcon *nicon = NULL;
2284 int max_y_icons, max_x_icons;
2286 max_x_icons = scr->scr_width/ICON_SIZE;
2287 max_y_icons = scr->scr_height/ICON_SIZE-1;
2289 if (wPreferences.flags.noupdates)
2290 return False;
2292 dx = dock->x_pos;
2293 dy = dock->y_pos;
2295 /* if the dock is full */
2296 if (!redocking &&
2297 (dock->icon_count >= dock->max_icons)) {
2298 return False;
2301 /* exact position */
2302 if (req_y < dy)
2303 ex_y = (req_y - offset - dy)/ICON_SIZE;
2304 else
2305 ex_y = (req_y + offset - dy)/ICON_SIZE;
2307 if (req_x < dx)
2308 ex_x = (req_x - offset - dx)/ICON_SIZE;
2309 else
2310 ex_x = (req_x + offset - dx)/ICON_SIZE;
2312 /* check if the icon is outside the screen boundaries */
2313 if (dx + ex_x*ICON_SIZE < -ICON_SIZE+2 ||
2314 dx + ex_x*ICON_SIZE >= scr->scr_width-1 ||
2315 dy + ex_y*ICON_SIZE < -ICON_SIZE+2 ||
2316 dy + ex_y*ICON_SIZE >= scr->scr_height-1)
2317 return False;
2319 if (dock->type == WM_DOCK) {
2320 if (icon->dock != dock && ex_x != 0)
2321 return False;
2323 for (i=0; i<dock->max_icons; i++) {
2324 nicon = dock->icon_array[i];
2325 if (nicon && nicon->yindex == ex_y) {
2326 aicon = nicon;
2327 break;
2331 *ret_x = 0;
2333 if (redocking) {
2334 int sig, done, closest;
2336 /* Possible cases when redocking:
2338 * icon dragged out of range of any slot -> false
2339 * icon dragged to range of free slot
2340 * icon dragged to range of same slot
2341 * icon dragged to range of different icon
2343 if (abs(ex_x) > DOCK_DETTACH_THRESHOLD)
2344 return False;
2346 if (ex_y >= 0 && ex_y <= max_y_icons && (aicon == icon || !aicon)) {
2348 *ret_y = ex_y;
2351 return True;
2354 /* start looking at the upper slot or lower? */
2355 if (ex_y*ICON_SIZE < (req_y + offset - dy))
2356 sig = 1;
2357 else
2358 sig = -1;
2360 closest = -1;
2361 done = 0;
2362 /* look for closest free slot */
2363 for (i=0; i<(DOCK_DETTACH_THRESHOLD+1)*2 && !done; i++) {
2364 int j;
2366 done = 1;
2367 closest = sig*(i/2) + ex_y;
2368 /* check if this slot is used */
2369 if (closest >= 0) {
2370 for (j = 0; j<dock->max_icons; j++) {
2371 if (dock->icon_array[j]
2372 && dock->icon_array[j]->yindex==closest) {
2373 /* slot is used by someone else */
2374 if (dock->icon_array[j]!=icon)
2375 done = 0;
2376 break;
2380 sig = -sig;
2382 if (done && closest >= 0 && closest <= max_y_icons &&
2383 ((ex_y >= closest && ex_y - closest < DOCK_DETTACH_THRESHOLD+1)
2385 (ex_y < closest && closest - ex_y <= DOCK_DETTACH_THRESHOLD+1))) {
2387 *ret_y = closest;
2389 return True;
2391 } else { /* !redocking */
2393 /* if slot is free and the icon is close enough, return it */
2394 if (!aicon && ex_x == 0 && ex_y >= 0 && ex_y <= max_y_icons) {
2395 *ret_y = ex_y;
2396 return True;
2399 } else { /* CLIP */
2400 int neighbours = 0;
2402 for (i=0; i<dock->max_icons; i++) {
2403 nicon = dock->icon_array[i];
2404 if (nicon && nicon->xindex == ex_x && nicon->yindex == ex_y) {
2405 aicon = nicon;
2406 break;
2410 for (i=0; i<dock->max_icons; i++) {
2411 nicon = dock->icon_array[i];
2412 if (nicon && nicon != icon && /* Icon can't be it's own neighbour */
2413 (abs(nicon->xindex - ex_x) <= CLIP_ATTACH_VICINITY &&
2414 abs(nicon->yindex - ex_y) <= CLIP_ATTACH_VICINITY)) {
2415 neighbours = 1;
2416 break;
2420 if (neighbours && (aicon==NULL || (redocking && aicon == icon))) {
2421 *ret_x = ex_x;
2422 *ret_y = ex_y;
2423 return True;
2426 return False;
2429 #define MIN(x, y) ((x) > (y) ? (y) : (x))
2430 #define MAX(x, y) ((x) < (y) ? (y) : (x))
2432 #define ON_SCREEN(x, y, sx, ex, sy, ey) \
2433 ((((x)+ICON_SIZE/2) >= (sx)) && (((y)+ICON_SIZE/2) >= (sy)) && \
2434 (((x) + (ICON_SIZE/2)) <= (ex)) && (((y) + (ICON_SIZE/2)) <= ey))
2438 * returns true if it can find a free slot in the dock,
2439 * in which case it changes x_pos and y_pos accordingly.
2440 * Else returns false.
2442 Bool
2443 wDockFindFreeSlot(WDock *dock, int *x_pos, int *y_pos)
2445 WScreen *scr = dock->screen_ptr;
2446 WAppIcon *btn;
2447 unsigned char *slot_map;
2448 int mwidth;
2449 int r;
2450 int x, y;
2451 int i, done = False;
2452 int corner;
2453 int sx=0, sy=0, ex=scr->scr_width, ey=scr->scr_height;
2456 /* if the dock is full */
2457 if (dock->icon_count >= dock->max_icons) {
2458 return False;
2461 if (!wPreferences.flags.nodock && scr->dock) {
2462 if (scr->dock->on_right_side)
2463 ex -= ICON_SIZE + DOCK_EXTRA_SPACE;
2464 else
2465 sx += ICON_SIZE + DOCK_EXTRA_SPACE;
2468 if (ex < dock->x_pos)
2469 ex = dock->x_pos;
2470 if (sx > dock->x_pos+ICON_SIZE)
2471 sx = dock->x_pos+ICON_SIZE;
2472 #define C_NONE 0
2473 #define C_NW 1
2474 #define C_NE 2
2475 #define C_SW 3
2476 #define C_SE 4
2478 /* check if clip is in a corner */
2479 if (dock->type==WM_CLIP) {
2480 if (dock->x_pos < 1 && dock->y_pos < 1)
2481 corner = C_NE;
2482 else if (dock->x_pos < 1 && dock->y_pos >= (ey-ICON_SIZE))
2483 corner = C_SE;
2484 else if (dock->x_pos >= (ex-ICON_SIZE)&& dock->y_pos >= (ey-ICON_SIZE))
2485 corner = C_SW;
2486 else if (dock->x_pos >= (ex-ICON_SIZE) && dock->y_pos < 1)
2487 corner = C_NW;
2488 else
2489 corner = C_NONE;
2490 } else
2491 corner = C_NONE;
2493 /* If the clip is in the corner, use only slots that are in the border
2494 * of the screen */
2495 if (corner!=C_NONE) {
2496 char *hmap, *vmap;
2497 int hcount, vcount;
2499 hcount = MIN(dock->max_icons, scr->scr_width/ICON_SIZE);
2500 vcount = MIN(dock->max_icons, scr->scr_height/ICON_SIZE);
2501 hmap = wmalloc(hcount+1);
2502 memset(hmap, 0, hcount+1);
2503 vmap = wmalloc(vcount+1);
2504 memset(vmap, 0, vcount+1);
2506 /* mark used positions */
2507 switch (corner) {
2508 case C_NE:
2509 for (i=0; i<dock->max_icons; i++) {
2510 btn = dock->icon_array[i];
2511 if (!btn)
2512 continue;
2514 if (btn->xindex==0 && btn->yindex > 0 && btn->yindex < vcount)
2515 vmap[btn->yindex] = 1;
2516 else if (btn->yindex==0 && btn->xindex>0 && btn->xindex<hcount)
2517 hmap[btn->xindex] = 1;
2519 case C_NW:
2520 for (i=0; i<dock->max_icons; i++) {
2521 btn = dock->icon_array[i];
2522 if (!btn)
2523 continue;
2525 if (btn->xindex==0 && btn->yindex > 0 && btn->yindex < vcount)
2526 vmap[btn->yindex] = 1;
2527 else if (btn->yindex==0 && btn->xindex<0 &&btn->xindex>-hcount)
2528 hmap[-btn->xindex] = 1;
2530 case C_SE:
2531 for (i=0; i<dock->max_icons; i++) {
2532 btn = dock->icon_array[i];
2533 if (!btn)
2534 continue;
2536 if (btn->xindex==0 && btn->yindex < 0 && btn->yindex > -vcount)
2537 vmap[-btn->yindex] = 1;
2538 else if (btn->yindex==0 && btn->xindex>0 && btn->xindex<hcount)
2539 hmap[btn->xindex] = 1;
2541 case C_SW:
2542 default:
2543 for (i=0; i<dock->max_icons; i++) {
2544 btn = dock->icon_array[i];
2545 if (!btn)
2546 continue;
2548 if (btn->xindex==0 && btn->yindex < 0 && btn->yindex > -vcount)
2549 vmap[-btn->yindex] = 1;
2550 else if (btn->yindex==0 && btn->xindex<0 &&btn->xindex>-hcount)
2551 hmap[-btn->xindex] = 1;
2554 x=0; y=0;
2555 done = 0;
2556 /* search a vacant slot */
2557 for (i=1; i<MAX(vcount, hcount); i++) {
2558 if (i < vcount && vmap[i]==0) {
2559 /* found a slot */
2560 x = 0;
2561 y = i;
2562 done = 1;
2563 break;
2564 } else if (i < hcount && hmap[i]==0) {
2565 /* found a slot */
2566 x = i;
2567 y = 0;
2568 done = 1;
2569 break;
2572 free(vmap);
2573 free(hmap);
2574 /* If found a slot, translate and return */
2575 if (done) {
2576 if (corner==C_NW || corner==C_NE) {
2577 *y_pos = y;
2578 } else {
2579 *y_pos = -y;
2581 if (corner==C_NE || corner==C_SE) {
2582 *x_pos = x;
2583 } else {
2584 *x_pos = -x;
2586 return True;
2588 /* else, try to find a slot somewhere else */
2591 /* a map of mwidth x mwidth would be enough if we allowed icons to be
2592 * placed outside of screen */
2593 mwidth = (int)ceil(sqrt(dock->max_icons));
2595 /* In the worst case (the clip is in the corner of the screen),
2596 * the amount of icons that fit in the clip is smaller.
2597 * Double the map to get a safe value.
2599 mwidth += mwidth;
2601 r = (mwidth-1)/2;
2603 slot_map = wmalloc(mwidth*mwidth);
2604 memset(slot_map, 0, mwidth*mwidth);
2606 #define XY2OFS(x,y) (MAX(abs(x),abs(y)) > r) ? 0 : (((y)+r)*(mwidth)+(x)+r)
2608 /* mark used slots in the map. If the slot falls outside the map
2609 * (for example, when all icons are placed in line), ignore them. */
2610 for (i=0; i<dock->max_icons; i++) {
2611 btn = dock->icon_array[i];
2612 if (btn)
2613 slot_map[XY2OFS(btn->xindex, btn->yindex)] = 1;
2615 /* Find closest slot from the center that is free by scanning the
2616 * map from the center to outward in circular passes.
2617 * This will not result in a neat layout, but will be optimal
2618 * in the sense that there will not be holes left.
2620 done = 0;
2621 for (i = 1; i <= r && !done; i++) {
2622 int tx, ty;
2624 /* top and bottom parts of the ring */
2625 for (x = -i; x <= i && !done; x++) {
2626 tx = dock->x_pos + x*ICON_SIZE;
2627 y = -i;
2628 ty = dock->y_pos + y*ICON_SIZE;
2629 if (slot_map[XY2OFS(x,y)]==0
2630 && ON_SCREEN(tx, ty, sx, ex, sy, ey)) {
2631 *x_pos = x;
2632 *y_pos = y;
2633 done = 1;
2634 break;
2636 y = i;
2637 ty = dock->y_pos + y*ICON_SIZE;
2638 if (slot_map[XY2OFS(x,y)]==0
2639 && ON_SCREEN(tx, ty, sx, ex, sy, ey)) {
2640 *x_pos = x;
2641 *y_pos = y;
2642 done = 1;
2643 break;
2646 /* left and right parts of the ring */
2647 for (y = -i+1; y <= i-1; y++) {
2648 ty = dock->y_pos + y*ICON_SIZE;
2649 x = -i;
2650 tx = dock->x_pos + x*ICON_SIZE;
2651 if (slot_map[XY2OFS(x,y)]==0
2652 && ON_SCREEN(tx, ty, sx, ex, sy, ey)) {
2653 *x_pos = x;
2654 *y_pos = y;
2655 done = 1;
2656 break;
2658 x = i;
2659 tx = dock->x_pos + x*ICON_SIZE;
2660 if (slot_map[XY2OFS(x,y)]==0
2661 && ON_SCREEN(tx, ty, sx, ex, sy, ey)) {
2662 *x_pos = x;
2663 *y_pos = y;
2664 done = 1;
2665 break;
2669 free(slot_map);
2670 #undef XY2OFS
2671 return done;
2675 static void
2676 moveDock(WDock *dock, int new_x, int new_y)
2678 WAppIcon *btn;
2679 int i;
2681 dock->x_pos = new_x;
2682 dock->y_pos = new_y;
2683 for (i=0; i<dock->max_icons; i++) {
2684 btn = dock->icon_array[i];
2685 if (btn) {
2686 btn->x_pos = new_x + btn->xindex*ICON_SIZE;
2687 btn->y_pos = new_y + btn->yindex*ICON_SIZE;
2688 XMoveWindow(dpy, btn->icon->core->window, btn->x_pos, btn->y_pos);
2694 static void
2695 swapDock(WDock *dock)
2697 WScreen *scr = dock->screen_ptr;
2698 WAppIcon *btn;
2699 int x, i;
2702 if (dock->on_right_side) {
2703 x = dock->x_pos = scr->scr_width - ICON_SIZE - DOCK_EXTRA_SPACE;
2704 } else {
2705 x = dock->x_pos = DOCK_EXTRA_SPACE;
2708 for (i=0; i<dock->max_icons; i++) {
2709 btn = dock->icon_array[i];
2710 if (btn) {
2711 btn->x_pos = x;
2712 XMoveWindow(dpy, btn->icon->core->window, btn->x_pos, btn->y_pos);
2716 wScreenUpdateUsableArea(scr);
2720 static pid_t
2721 execCommand(WAppIcon *btn, char *command, WSavedState *state)
2723 WScreen *scr = btn->icon->core->screen_ptr;
2724 pid_t pid;
2725 char **argv;
2726 int argc;
2727 char *cmdline;
2729 cmdline = ExpandOptions(scr, command);
2731 if (scr->flags.dnd_data_convertion_status || !cmdline) {
2732 if (cmdline)
2733 free(cmdline);
2734 if (state)
2735 free(state);
2736 return 0;
2739 ParseCommand(cmdline, &argv, &argc);
2741 if (argv==NULL) {
2742 if (cmdline)
2743 free(cmdline);
2744 if (state)
2745 free(state);
2746 return 0;
2749 if ((pid=fork())==0) {
2750 char **args;
2751 int i;
2753 SetupEnvironment(scr);
2755 #ifdef HAVE_SETPGID
2756 setpgid(0, 0);
2757 #endif
2759 args = malloc(sizeof(char*)*(argc+1));
2760 if (!args)
2761 exit(111);
2762 for (i=0; i<argc; i++) {
2763 args[i] = argv[i];
2765 args[argc] = NULL;
2766 execvp(argv[0], args);
2767 exit(111);
2769 while (argc > 0)
2770 free(argv[--argc]);
2771 free(argv);
2773 if (pid > 0) {
2774 if (!state) {
2775 state = wmalloc(sizeof(WSavedState));
2776 memset(state, 0, sizeof(WSavedState));
2777 state->hidden = -1;
2778 state->miniaturized = -1;
2779 state->shaded = -1;
2780 if (btn->dock == scr->dock)
2781 state->workspace = -1;
2782 else
2783 state->workspace = scr->current_workspace;
2785 wWindowAddSavedState(btn->wm_instance, btn->wm_class, cmdline, pid,
2786 state);
2787 wAddDeathHandler(pid, (WDeathHandler*)trackDeadProcess,
2788 btn->dock);
2789 } else if (state) {
2790 free(state);
2792 free(cmdline);
2793 return pid;
2797 void
2798 wDockHideIcons(WDock *dock)
2800 int i;
2801 WAppIcon *btn;
2803 if (dock==NULL)
2804 return;
2806 btn = dock->icon_array[0];
2808 for (i=1; i<dock->max_icons; i++) {
2809 if (dock->icon_array[i])
2810 XUnmapWindow(dpy, dock->icon_array[i]->icon->core->window);
2812 dock->mapped = 0;
2814 dockIconPaint(btn);
2818 void
2819 wDockShowIcons(WDock *dock)
2821 int i, newlevel;
2822 WAppIcon *btn;
2824 if (dock==NULL)
2825 return;
2827 btn = dock->icon_array[0];
2828 moveDock(dock, btn->x_pos, btn->y_pos);
2830 newlevel = dock->lowered ? WMNormalLevel : WMDockLevel;
2831 ChangeStackingLevel(btn->icon->core, newlevel);
2833 for (i=1; i<dock->max_icons; i++) {
2834 if (dock->icon_array[i]) {
2835 MoveInStackListAbove(dock->icon_array[i]->icon->core,
2836 btn->icon->core);
2837 break;
2841 if (!dock->collapsed) {
2842 for (i=1; i<dock->max_icons; i++) {
2843 if (dock->icon_array[i]) {
2844 XMapWindow(dpy, dock->icon_array[i]->icon->core->window);
2848 dock->mapped = 1;
2850 dockIconPaint(btn);
2854 void
2855 wDockLower(WDock *dock)
2857 int i;
2859 for (i=0; i<dock->max_icons; i++) {
2860 if (dock->icon_array[i])
2861 wLowerFrame(dock->icon_array[i]->icon->core);
2866 void
2867 wDockRaise(WDock *dock)
2869 int i;
2871 for (i=dock->max_icons-1; i>=0; i--) {
2872 if (dock->icon_array[i])
2873 wRaiseFrame(dock->icon_array[i]->icon->core);
2878 void
2879 wDockRaiseLower(WDock *dock)
2881 if (!dock->icon_array[0]->icon->core->stacking->above
2882 ||(dock->icon_array[0]->icon->core->stacking->window_level
2883 !=dock->icon_array[0]->icon->core->stacking->above->stacking->window_level))
2884 wDockLower(dock);
2885 else
2886 wDockRaise(dock);
2890 void
2891 wDockFinishLaunch(WDock *dock, WAppIcon *icon)
2893 icon->launching = 0;
2894 icon->relaunching = 0;
2895 dockIconPaint(icon);
2899 WAppIcon*
2900 wDockFindIconFor(WDock *dock, Window window)
2902 WAppIcon *icon;
2903 int i;
2905 for (i=0; i<dock->max_icons; i++) {
2906 icon = dock->icon_array[i];
2907 if (icon && icon->main_window == window)
2908 return icon;
2910 return NULL;
2914 void
2915 wDockTrackWindowLaunch(WDock *dock, Window window)
2917 WAppIcon *icon;
2918 #ifdef REDUCE_APPICONS
2919 WAppIconAppList *tapplist;
2920 #endif
2921 char *wm_class, *wm_instance;
2922 int i;
2923 Bool firstPass = True;
2924 Bool found = False;
2925 char *command = NULL;
2928 int argc;
2929 char **argv;
2931 if (XGetCommand(dpy, window, &argv, &argc)) {
2932 if (argc > 0 && argv != NULL)
2933 command = FlattenStringList(argv,argc);
2934 if (argv) {
2935 XFreeStringList(argv);
2940 if (!PropGetWMClass(window, &wm_class, &wm_instance) ||
2941 (!wm_class && !wm_instance))
2942 return;
2944 retry:
2945 for (i=0; i<dock->max_icons; i++) {
2946 icon = dock->icon_array[i];
2947 if (!icon)
2948 continue;
2950 /* app is already attached to icon */
2951 if (icon->main_window == window) {
2952 found = True;
2953 break;
2956 if ((icon->wm_instance || icon->wm_class)
2957 && (icon->launching
2958 || (dock->screen_ptr->flags.startup && !icon->running))) {
2960 if (icon->wm_instance && wm_instance &&
2961 strcmp(icon->wm_instance, wm_instance)!=0) {
2962 continue;
2964 if (icon->wm_class && wm_class &&
2965 strcmp(icon->wm_class, wm_class)!=0) {
2966 continue;
2968 if (firstPass && command && strcmp(icon->command, command)!=0) {
2969 continue;
2972 if (!icon->relaunching) {
2973 WApplication *wapp;
2975 /* Possibly an application that was docked with dockit,
2976 * but the user did not update WMState to indicate that
2977 * it was docked by force */
2978 wapp = wApplicationOf(window);
2979 if (!wapp) {
2980 icon->forced_dock = 1;
2981 icon->running = 0;
2983 if (!icon->forced_dock)
2984 icon->main_window = window;
2986 #ifdef REDUCE_APPICONS
2987 tapplist = wmalloc(sizeof(WAppIconAppList));
2988 memset(tapplist, 0, sizeof(WAppIconAppList));
2989 tapplist->next = icon->applist;
2990 if (icon->applist)
2991 icon->applist->prev = tapplist;
2992 icon->applist = tapplist;
2993 tapplist->wapp = wApplicationOf(window);
2994 icon->num_apps++;
2995 #endif
2997 found = True;
2998 wDockFinishLaunch(dock, icon);
2999 break;
3003 if (firstPass && !found) {
3004 firstPass = False;
3005 goto retry;
3008 if (command)
3009 free(command);
3011 if (wm_class)
3012 XFree(wm_class);
3013 if (wm_instance)
3014 XFree(wm_instance);
3019 void
3020 wClipUpdateForWorkspaceChange(WScreen *scr, int workspace)
3022 if (!wPreferences.flags.noclip) {
3023 scr->clip_icon->dock = scr->workspaces[workspace]->clip;
3024 if (scr->current_workspace != workspace) {
3025 WDock *old_clip = scr->workspaces[scr->current_workspace]->clip;
3027 wDockHideIcons(old_clip);
3028 if (old_clip->auto_raise_lower) {
3029 if (old_clip->auto_raise_magic) {
3030 WMDeleteTimerHandler(old_clip->auto_raise_magic);
3031 old_clip->auto_raise_magic = NULL;
3033 wDockLower(old_clip);
3035 if (old_clip->auto_collapse) {
3036 if (old_clip->auto_expand_magic) {
3037 WMDeleteTimerHandler(old_clip->auto_expand_magic);
3038 old_clip->auto_expand_magic = NULL;
3040 old_clip->collapsed = 1;
3042 wDockShowIcons(scr->workspaces[workspace]->clip);
3044 if (scr->flags.clip_balloon_mapped)
3045 showClipBalloon(scr->clip_icon->dock, workspace);
3051 static void
3052 trackDeadProcess(pid_t pid, unsigned char status, WDock *dock)
3054 WAppIcon *icon;
3055 int i;
3057 for (i=0; i<dock->max_icons; i++) {
3058 icon = dock->icon_array[i];
3059 if (!icon)
3060 continue;
3062 if (icon->launching && icon->pid == pid) {
3063 if (!icon->relaunching) {
3064 icon->running = 0;
3065 icon->main_window = None;
3067 wDockFinishLaunch(dock, icon);
3068 icon->pid = 0;
3069 if (status==111) {
3070 char msg[PATH_MAX];
3071 #ifdef OFFIX_DND
3072 sprintf(msg, _("Could not execute command \"%s\""),
3073 icon->drop_launch && icon->dnd_command
3074 ? icon->dnd_command : icon->command);
3075 #else
3076 sprintf(msg, _("Could not execute command \"%s\""),
3077 icon->command);
3078 #endif
3079 wMessageDialog(dock->screen_ptr, _("Error"), msg,
3080 _("OK"), NULL, NULL);
3082 break;
3088 static void
3089 toggleLowered(WDock *dock)
3091 WAppIcon *tmp;
3092 int newlevel, i;
3094 /* lower/raise Dock */
3095 if (!dock->lowered) {
3096 newlevel = WMNormalLevel;
3097 dock->lowered = 1;
3098 } else {
3099 newlevel = WMDockLevel;
3100 dock->lowered = 0;
3103 for (i=0; i<dock->max_icons; i++) {
3104 tmp = dock->icon_array[i];
3105 if (!tmp)
3106 continue;
3108 ChangeStackingLevel(tmp->icon->core, newlevel);
3109 if (dock->lowered)
3110 wLowerFrame(tmp->icon->core);
3113 if (dock->type == WM_DOCK)
3114 wScreenUpdateUsableArea(dock->screen_ptr);
3118 static void
3119 toggleCollapsed(WDock *dock)
3121 if (dock->collapsed) {
3122 dock->collapsed = 0;
3123 wDockShowIcons(dock);
3125 else {
3126 dock->collapsed = 1;
3127 wDockHideIcons(dock);
3132 static void
3133 openDockMenu(WDock *dock, WAppIcon *aicon, XEvent *event)
3135 WScreen *scr = dock->screen_ptr;
3136 WObjDescriptor *desc;
3137 WMenuEntry *entry;
3138 WApplication *wapp = NULL;
3139 int index = 0;
3140 int x_pos;
3141 int appIsRunning = aicon->running && aicon->icon && aicon->icon->owner;
3143 if (dock->type == WM_DOCK) {
3144 /* keep on top */
3145 entry = dock->menu->entries[index];
3146 entry->flags.indicator_on = !dock->lowered;
3147 entry->clientdata = dock;
3148 } else {
3149 /* clip options */
3150 if (scr->clip_options)
3151 updateClipOptionsMenu(scr->clip_options, dock);
3153 /* Rename Workspace */
3154 entry = dock->menu->entries[++index];
3155 entry->clientdata = dock;
3157 /* select icon */
3158 entry = dock->menu->entries[++index];
3159 entry->clientdata = aicon;
3160 wMenuSetEnabled(dock->menu, index, aicon!=scr->clip_icon);
3162 /* (un)select all icons */
3163 entry = dock->menu->entries[++index];
3164 entry->clientdata = aicon;
3165 wMenuSetEnabled(dock->menu, index, dock->icon_count > 1);
3167 /* keep icon(s) */
3168 entry = dock->menu->entries[++index];
3169 entry->clientdata = aicon;
3170 wMenuSetEnabled(dock->menu, index, dock->icon_count > 1);
3172 /* this is the workspace submenu part */
3173 if (scr->clip_submenu)
3174 updateWorkspaceMenu(scr->clip_submenu, aicon);
3175 index++;
3177 /* remove icon(s) */
3178 entry = dock->menu->entries[++index];
3179 entry->clientdata = aicon;
3180 wMenuSetEnabled(dock->menu, index, dock->icon_count > 1);
3182 /* attract icon(s) */
3183 entry = dock->menu->entries[++index];
3184 entry->clientdata = aicon;
3187 /* launch */
3188 entry = dock->menu->entries[++index];
3189 entry->clientdata = aicon;
3190 wMenuSetEnabled(dock->menu, index, aicon->command!=NULL);
3192 /* unhide here */
3193 entry = dock->menu->entries[++index];
3194 entry->clientdata = aicon;
3195 wMenuSetEnabled(dock->menu, index, appIsRunning);
3197 /* hide */
3198 entry = dock->menu->entries[++index];
3199 entry->clientdata = aicon;
3200 if (aicon->icon->owner) {
3201 wapp = wApplicationOf(aicon->icon->owner->main_window);
3202 if (wapp && wapp->flags.hidden)
3203 entry->text = _("Unhide");
3204 else
3205 entry->text = _("Hide");
3206 } else {
3207 entry->text = _("Hide");
3209 wMenuSetEnabled(dock->menu, index, appIsRunning);
3211 /* settings */
3212 entry = dock->menu->entries[++index];
3213 entry->clientdata = aicon;
3214 wMenuSetEnabled(dock->menu, index, !aicon->editing
3215 && !wPreferences.flags.noupdates);
3217 /* kill */
3218 entry = dock->menu->entries[++index];
3219 entry->clientdata = aicon;
3220 wMenuSetEnabled(dock->menu, index, appIsRunning);
3222 if (!dock->menu->flags.realized)
3223 wMenuRealize(dock->menu);
3225 if (dock->type == WM_CLIP) {
3226 x_pos = event->xbutton.x_root+2;
3227 } else {
3228 x_pos = dock->on_right_side ?
3229 scr->scr_width - dock->menu->frame->core->width - 2 : 0;
3232 wMenuMapAt(dock->menu, x_pos, event->xbutton.y_root+2, False);
3234 /* allow drag select */
3235 event->xany.send_event = True;
3236 desc = &dock->menu->menu->descriptor;
3237 (*desc->handle_mousedown)(desc, event);
3241 static void
3242 openClipWorkspaceMenu(WScreen *scr, int x, int y)
3244 if (!scr->clip_ws_menu) {
3245 scr->clip_ws_menu = wWorkspaceMenuMake(scr, False);
3247 wWorkspaceMenuUpdate(scr, scr->clip_ws_menu);
3248 wMenuMapAt(scr->clip_ws_menu, x, y, False);
3252 /******************************************************************/
3253 static void
3254 iconDblClick(WObjDescriptor *desc, XEvent *event)
3256 WAppIcon *btn = desc->parent;
3257 WDock *dock = btn->dock;
3258 WApplication *wapp = NULL;
3259 int unhideHere = 0;
3261 #ifdef REDUCE_APPICONS
3262 if ((btn->icon->owner && !(event->xbutton.state & ControlMask)) ||
3263 ((btn->icon->owner == NULL) && (btn->applist != NULL))) {
3264 if (btn->icon->owner == NULL)
3265 btn->icon->owner = btn->applist->wapp->main_window_desc;
3266 #ifdef I_HATE_THIS
3268 #endif
3269 #else
3270 if (btn->icon->owner && !(event->xbutton.state & ControlMask)) {
3271 #endif
3272 wapp = wApplicationOf(btn->icon->owner->main_window);
3274 assert(wapp!=NULL);
3276 unhideHere = (event->xbutton.state & ShiftMask);
3278 /* go to the last workspace that the user worked on the app */
3279 if (!unhideHere) {
3280 wWorkspaceChange(dock->screen_ptr, wapp->last_workspace);
3283 wUnhideApplication(wapp, event->xbutton.button==Button2,
3284 unhideHere);
3286 if (event->xbutton.state & MOD_MASK) {
3287 wHideOtherApplications(btn->icon->owner);
3289 } else {
3290 if (event->xbutton.button==Button1) {
3292 if (event->xbutton.state & MOD_MASK) {
3293 /* raise/lower dock */
3294 toggleLowered(dock);
3295 } else if (btn == dock->screen_ptr->clip_icon) {
3296 if (getClipButton(event->xbutton.x, event->xbutton.y)==CLIP_IDLE)
3297 toggleCollapsed(dock);
3298 else
3299 handleClipChangeWorkspace(dock->screen_ptr, event);
3300 } else if (btn->command) {
3301 if (!btn->launching &&
3302 (!btn->running || (event->xbutton.state & ControlMask))) {
3303 launchDockedApplication(btn);
3311 static void
3312 handleDockMove(WDock *dock, WAppIcon *aicon, XEvent *event)
3314 WScreen *scr = dock->screen_ptr;
3315 int ofs_x=event->xbutton.x, ofs_y=event->xbutton.y;
3316 int x, y;
3317 XEvent ev;
3318 int grabbed = 0, swapped = 0, done;
3319 Pixmap ghost = None;
3320 int superfluous = wPreferences.superfluous; /* we catch it to avoid problems */
3322 #ifdef DEBUG
3323 puts("moving dock");
3324 #endif
3325 if (XGrabPointer(dpy, aicon->icon->core->window, True, ButtonMotionMask
3326 |ButtonReleaseMask|ButtonPressMask, GrabModeAsync,
3327 GrabModeAsync, None, None, CurrentTime) !=GrabSuccess) {
3328 wwarning("pointer grab failed for dock move");
3330 y = 0;
3331 for (x=0; x<dock->max_icons; x++) {
3332 if (dock->icon_array[x]!=NULL &&
3333 dock->icon_array[x]->yindex > y)
3334 y = dock->icon_array[x]->yindex;
3336 y++;
3337 XResizeWindow(dpy, scr->dock_shadow, ICON_SIZE, ICON_SIZE*y);
3339 done = 0;
3340 while (!done) {
3341 WMMaskEvent(dpy, PointerMotionMask|ButtonReleaseMask|ButtonPressMask
3342 |ButtonMotionMask|ExposureMask, &ev);
3343 switch (ev.type) {
3344 case Expose:
3345 WMHandleEvent(&ev);
3346 break;
3348 case MotionNotify:
3349 if (!grabbed) {
3350 if (abs(ofs_x-ev.xmotion.x)>=MOVE_THRESHOLD
3351 || abs(ofs_y-ev.xmotion.y)>=MOVE_THRESHOLD) {
3352 XChangeActivePointerGrab(dpy, ButtonMotionMask
3353 |ButtonReleaseMask|ButtonPressMask,
3354 wCursor[WCUR_MOVE], CurrentTime);
3355 grabbed=1;
3357 break;
3359 if (dock->type == WM_CLIP) {
3360 if (ev.xmotion.x_root - ofs_x < 0) {
3361 x = 0;
3362 } else if (ev.xmotion.x_root - ofs_x + ICON_SIZE >
3363 scr->scr_width) {
3364 x = scr->scr_width - ICON_SIZE;
3365 } else {
3366 x = ev.xmotion.x_root - ofs_x;
3368 if (ev.xmotion.y_root - ofs_y < 0) {
3369 y = 0;
3370 } else if (ev.xmotion.y_root - ofs_y + ICON_SIZE >
3371 scr->scr_height) {
3372 y = scr->scr_height - ICON_SIZE;
3373 } else {
3374 y = ev.xmotion.y_root - ofs_y;
3376 moveDock(dock, x, y);
3377 } else {
3378 /* move vertically if pointer is inside the dock*/
3379 if ((dock->on_right_side &&
3380 ev.xmotion.x_root >= dock->x_pos - ICON_SIZE)
3381 || (!dock->on_right_side &&
3382 ev.xmotion.x_root <= dock->x_pos + ICON_SIZE*2)) {
3384 if (ev.xmotion.y_root - ofs_y < 0) {
3385 y = 0;
3386 } else if (ev.xmotion.y_root - ofs_y + ICON_SIZE >
3387 scr->scr_height) {
3388 y = scr->scr_height - ICON_SIZE;
3389 } else {
3390 y = ev.xmotion.y_root - ofs_y;
3392 moveDock(dock, dock->x_pos, y);
3394 /* move horizontally to change sides */
3395 x = ev.xmotion.x_root - ofs_x;
3396 if (!dock->on_right_side) {
3398 /* is on left */
3400 if (ev.xmotion.x_root > dock->x_pos + ICON_SIZE*2) {
3401 XMoveWindow(dpy, scr->dock_shadow, scr->scr_width-ICON_SIZE
3402 -DOCK_EXTRA_SPACE-1, dock->y_pos);
3403 if (superfluous) {
3404 if (ghost==None) {
3405 ghost = MakeGhostDock(dock, dock->x_pos,
3406 scr->scr_width-ICON_SIZE
3407 -DOCK_EXTRA_SPACE-1,
3408 dock->y_pos);
3409 XSetWindowBackgroundPixmap(dpy, scr->dock_shadow,
3410 ghost);
3411 XClearWindow(dpy, scr->dock_shadow);
3414 XMapRaised(dpy, scr->dock_shadow);
3415 swapped = 1;
3416 } else {
3417 if (superfluous && ghost!=None) {
3418 XFreePixmap(dpy, ghost);
3419 ghost = None;
3421 XUnmapWindow(dpy, scr->dock_shadow);
3422 swapped = 0;
3424 } else {
3425 /* is on right */
3426 if (ev.xmotion.x_root < dock->x_pos - ICON_SIZE) {
3427 XMoveWindow(dpy, scr->dock_shadow,
3428 DOCK_EXTRA_SPACE, dock->y_pos);
3429 if (superfluous) {
3430 if (ghost==None) {
3431 ghost = MakeGhostDock(dock, dock->x_pos,
3432 DOCK_EXTRA_SPACE, dock->y_pos);
3433 XSetWindowBackgroundPixmap(dpy, scr->dock_shadow,
3434 ghost);
3435 XClearWindow(dpy, scr->dock_shadow);
3438 XMapRaised(dpy, scr->dock_shadow);
3439 swapped = -1;
3440 } else {
3441 XUnmapWindow(dpy, scr->dock_shadow);
3442 swapped = 0;
3443 if (superfluous && ghost!=None) {
3444 XFreePixmap(dpy, ghost);
3445 ghost = None;
3450 break;
3452 case ButtonPress:
3453 break;
3455 case ButtonRelease:
3456 if (ev.xbutton.button != event->xbutton.button)
3457 break;
3458 XUngrabPointer(dpy, CurrentTime);
3459 XUnmapWindow(dpy, scr->dock_shadow);
3460 XResizeWindow(dpy, scr->dock_shadow, ICON_SIZE, ICON_SIZE);
3461 if (dock->type == WM_DOCK) {
3462 if (swapped!=0) {
3463 if (swapped>0)
3464 dock->on_right_side = 1;
3465 else
3466 dock->on_right_side = 0;
3467 swapDock(dock);
3468 wArrangeIcons(scr, False);
3471 done = 1;
3472 break;
3475 if (superfluous) {
3476 if (ghost!=None)
3477 XFreePixmap(dpy, ghost);
3478 XSetWindowBackground(dpy, scr->dock_shadow, scr->white_pixel);
3480 #ifdef DEBUG
3481 puts("End dock move");
3482 #endif
3487 static void
3488 handleIconMove(WDock *dock, WAppIcon *aicon, XEvent *event)
3490 WScreen *scr = dock->screen_ptr;
3491 Window wins[2];
3492 WIcon *icon = aicon->icon;
3493 WDock *dock2 = NULL, *last_dock = dock, *clip = NULL;
3494 int ondock, grabbed = 0, change_dock = 0, collapsed = 0;
3495 XEvent ev;
3496 int x = aicon->x_pos, y = aicon->y_pos;
3497 int ofs_x = event->xbutton.x, ofs_y = event->xbutton.y;
3498 int shad_x = x, shad_y = y;
3499 int ix = aicon->xindex, iy = aicon->yindex;
3500 int tmp;
3501 Pixmap ghost = None;
3502 Bool docked;
3503 int superfluous = wPreferences.superfluous; /* we catch it to avoid problems */
3505 if (wPreferences.flags.noupdates)
3506 return;
3508 if (XGrabPointer(dpy, icon->core->window, True, ButtonMotionMask
3509 |ButtonReleaseMask|ButtonPressMask, GrabModeAsync,
3510 GrabModeAsync, None, None, CurrentTime) !=GrabSuccess) {
3511 #ifdef DEBUG0
3512 wwarning("pointer grab failed for icon move");
3513 #endif
3516 wRaiseFrame(icon->core);
3518 if (!wPreferences.flags.noclip)
3519 clip = scr->workspaces[scr->current_workspace]->clip;
3521 if (dock == scr->dock && !wPreferences.flags.noclip)
3522 dock2 = clip;
3523 else if (dock != scr->dock && !wPreferences.flags.nodock)
3524 dock2 = scr->dock;
3526 wins[0] = icon->core->window;
3527 wins[1] = scr->dock_shadow;
3528 XRestackWindows(dpy, wins, 2);
3529 XMoveResizeWindow(dpy, scr->dock_shadow, aicon->x_pos, aicon->y_pos,
3530 ICON_SIZE, ICON_SIZE);
3531 if (superfluous) {
3532 if (icon->pixmap!=None)
3533 ghost = MakeGhostIcon(scr, icon->pixmap);
3534 else
3535 ghost = MakeGhostIcon(scr, icon->core->window);
3537 XSetWindowBackgroundPixmap(dpy, scr->dock_shadow, ghost);
3538 XClearWindow(dpy, scr->dock_shadow);
3540 XMapWindow(dpy, scr->dock_shadow);
3542 ondock = 1;
3545 while(1) {
3546 XMaskEvent(dpy, PointerMotionMask|ButtonReleaseMask|ButtonPressMask
3547 |ButtonMotionMask|ExposureMask, &ev);
3548 switch (ev.type) {
3549 case Expose:
3550 WMHandleEvent(&ev);
3551 break;
3553 case MotionNotify:
3554 if (!grabbed) {
3555 if (abs(ofs_x-ev.xmotion.x)>=MOVE_THRESHOLD
3556 || abs(ofs_y-ev.xmotion.y)>=MOVE_THRESHOLD) {
3557 XChangeActivePointerGrab(dpy, ButtonMotionMask
3558 |ButtonReleaseMask|ButtonPressMask,
3559 wCursor[WCUR_MOVE], CurrentTime);
3560 grabbed=1;
3561 } else {
3562 break;
3566 x = ev.xmotion.x_root - ofs_x;
3567 y = ev.xmotion.y_root - ofs_y;
3568 tmp = wDockSnapIcon(dock, aicon, x, y, &ix, &iy, True);
3569 if (tmp && dock2) {
3570 change_dock = 0;
3571 if (last_dock != dock && collapsed) {
3572 last_dock->collapsed = 1;
3573 wDockHideIcons(last_dock);
3574 collapsed = 0;
3576 if (!collapsed && (collapsed = dock->collapsed)) {
3577 dock->collapsed = 0;
3578 wDockShowIcons(dock);
3580 if (dock->auto_raise_lower)
3581 wDockRaise(dock);
3582 last_dock = dock;
3584 else if (dock2) {
3585 tmp = wDockSnapIcon(dock2, aicon, x, y, &ix, &iy, False);
3586 if (tmp) {
3587 change_dock = 1;
3588 if (last_dock != dock2 && collapsed) {
3589 last_dock->collapsed = 1;
3590 wDockHideIcons(last_dock);
3591 collapsed = 0;
3593 if (!collapsed && (collapsed = dock2->collapsed)) {
3594 dock2->collapsed = 0;
3595 wDockShowIcons(dock2);
3597 if (dock2->auto_raise_lower)
3598 wDockRaise(dock2);
3599 last_dock = dock2;
3602 if (aicon->launching
3603 || (aicon->running && !(ev.xmotion.state & MOD_MASK))
3604 || (!aicon->running && tmp)) {
3605 shad_x = last_dock->x_pos + ix*wPreferences.icon_size;
3606 shad_y = last_dock->y_pos + iy*wPreferences.icon_size;
3608 XMoveWindow(dpy, scr->dock_shadow, shad_x, shad_y);
3610 if (!ondock) {
3611 XMapWindow(dpy, scr->dock_shadow);
3612 #if 0
3613 if (!collapsed && (collapsed = last_dock->collapsed)) {
3614 last_dock->collapsed = 0;
3615 wDockShowIcons(last_dock);
3617 #endif
3619 ondock = 1;
3620 } else {
3621 if (ondock) {
3622 XUnmapWindow(dpy, scr->dock_shadow);
3623 #if 0
3624 if (last_dock && collapsed &&
3625 aicon->running && (ev.xmotion.state & MOD_MASK)) {
3626 last_dock->collapsed = 1;
3627 wDockHideIcons(last_dock);
3628 collapsed = 0;
3630 #endif
3632 ondock = 0;
3634 XMoveWindow(dpy, icon->core->window, x, y);
3635 break;
3637 case ButtonPress:
3638 break;
3640 case ButtonRelease:
3641 if (ev.xbutton.button != event->xbutton.button)
3642 break;
3643 XUngrabPointer(dpy, CurrentTime);
3644 if (ondock) {
3645 SlideWindow(icon->core->window, x, y, shad_x, shad_y);
3646 XUnmapWindow(dpy, scr->dock_shadow);
3647 if (!change_dock) {
3648 reattachIcon(dock, aicon, ix, iy);
3649 if (clip && dock!=clip && clip->auto_raise_lower)
3650 wDockLower(clip);
3651 } else {
3652 docked = moveIconBetweenDocks(dock, dock2, aicon, ix, iy);
3653 if (!docked) {
3654 /* Slide it back if dock rejected it */
3655 SlideWindow(icon->core->window, x, y, aicon->x_pos,
3656 aicon->y_pos);
3657 reattachIcon(dock, aicon, aicon->xindex,aicon->yindex);
3659 if (last_dock->type==WM_CLIP && last_dock->auto_collapse) {
3660 collapsed = 0;
3663 } else {
3664 aicon->x_pos = x;
3665 aicon->y_pos = y;
3666 if (superfluous) {
3667 if (!aicon->running && !wPreferences.no_animations) {
3668 /* We need to deselect it, even if is deselected in
3669 * wDockDetach(), because else DoKaboom() will fail.
3671 if (aicon->icon->selected)
3672 wIconSelect(aicon->icon);
3673 DoKaboom(scr,aicon->icon->core->window, x, y);
3676 if (clip && clip->auto_raise_lower)
3677 wDockLower(clip);
3678 wDockDetach(dock, aicon);
3680 if (collapsed) {
3681 last_dock->collapsed = 1;
3682 wDockHideIcons(last_dock);
3683 collapsed = 0;
3685 if (superfluous) {
3686 if (ghost!=None)
3687 XFreePixmap(dpy, ghost);
3688 XSetWindowBackground(dpy, scr->dock_shadow, scr->white_pixel);
3690 #ifdef DEBUG
3691 puts("End icon move");
3692 #endif
3693 return;
3699 static int
3700 getClipButton(int px, int py)
3702 int pt = (CLIP_BUTTON_SIZE+2)*ICON_SIZE/64;
3704 if (px < 0 || py < 0 || px >= ICON_SIZE || py >= ICON_SIZE)
3705 return CLIP_IDLE;
3707 if (py <= pt-((int)ICON_SIZE-1-px))
3708 return CLIP_FORWARD;
3709 else if (px <= pt-((int)ICON_SIZE-1-py))
3710 return CLIP_REWIND;
3712 return CLIP_IDLE;
3716 static void
3717 handleClipChangeWorkspace(WScreen *scr, XEvent *event)
3719 XEvent ev;
3720 int done, direction, new_ws;
3721 int new_dir;
3722 WDock *clip = scr->clip_icon->dock;
3724 direction = getClipButton(event->xbutton.x, event->xbutton.y);
3726 clip->lclip_button_pushed = direction==CLIP_REWIND;
3727 clip->rclip_button_pushed = direction==CLIP_FORWARD;
3729 wClipIconPaint(scr->clip_icon);
3730 done = 0;
3731 while(!done) {
3732 WMMaskEvent(dpy, ExposureMask|ButtonMotionMask|ButtonReleaseMask
3733 |ButtonPressMask, &ev);
3734 switch (ev.type) {
3735 case Expose:
3736 WMHandleEvent(&ev);
3737 break;
3739 case MotionNotify:
3740 new_dir = getClipButton(ev.xmotion.x, ev.xmotion.y);
3741 if (new_dir != direction) {
3742 direction = new_dir;
3743 clip->lclip_button_pushed = direction==CLIP_REWIND;
3744 clip->rclip_button_pushed = direction==CLIP_FORWARD;
3745 wClipIconPaint(scr->clip_icon);
3747 break;
3749 case ButtonPress:
3750 break;
3752 case ButtonRelease:
3753 if (ev.xbutton.button == event->xbutton.button)
3754 done = 1;
3758 clip->lclip_button_pushed = 0;
3759 clip->rclip_button_pushed = 0;
3761 new_ws = wPreferences.ws_advance || (event->xbutton.state & ControlMask);
3763 if (direction == CLIP_FORWARD) {
3764 if (scr->current_workspace < scr->workspace_count-1)
3765 wWorkspaceChange(scr, scr->current_workspace+1);
3766 else if (new_ws && scr->current_workspace < MAX_WORKSPACES-1)
3767 wWorkspaceChange(scr, scr->current_workspace+1);
3768 else if (wPreferences.ws_cycle)
3769 wWorkspaceChange(scr, 0);
3771 else if (direction == CLIP_REWIND) {
3772 if (scr->current_workspace > 0)
3773 wWorkspaceChange(scr, scr->current_workspace-1);
3774 else if (scr->current_workspace==0 && wPreferences.ws_cycle)
3775 wWorkspaceChange(scr, scr->workspace_count-1);
3778 wClipIconPaint(scr->clip_icon);
3782 static void
3783 iconMouseDown(WObjDescriptor *desc, XEvent *event)
3785 WAppIcon *aicon = desc->parent;
3786 WDock *dock = aicon->dock;
3787 WScreen *scr = aicon->icon->core->screen_ptr;
3789 if (aicon->editing || WCHECK_STATE(WSTATE_MODAL))
3790 return;
3792 scr->last_dock = dock;
3794 if (dock->menu->flags.mapped)
3795 wMenuUnmap(dock->menu);
3797 if (IsDoubleClick(scr, event)) {
3798 /* double-click was not in the main clip icon */
3799 if (dock->type != WM_CLIP || aicon->xindex!=0 || aicon->yindex!=0
3800 || getClipButton(event->xbutton.x, event->xbutton.y)==CLIP_IDLE) {
3801 iconDblClick(desc, event);
3802 return;
3806 if (dock->type == WM_CLIP && scr->flags.clip_balloon_mapped) {
3807 XUnmapWindow(dpy, scr->clip_balloon);
3808 scr->flags.clip_balloon_mapped = 0;
3811 #ifdef DEBUG
3812 puts("handling dock");
3813 #endif
3814 if (event->xbutton.button == Button1) {
3815 if (event->xbutton.state & MOD_MASK)
3816 wDockLower(dock);
3817 else
3818 wDockRaise(dock);
3820 if ((event->xbutton.state & ShiftMask) && aicon!=scr->clip_icon &&
3821 dock->type!=WM_DOCK) {
3822 wIconSelect(aicon->icon);
3823 return;
3826 if (aicon->yindex==0 && aicon->xindex==0) {
3827 if (getClipButton(event->xbutton.x, event->xbutton.y)!=CLIP_IDLE
3828 && dock->type==WM_CLIP)
3829 handleClipChangeWorkspace(scr, event);
3830 else
3831 handleDockMove(dock, aicon, event);
3832 } else
3833 handleIconMove(dock, aicon, event);
3835 } else if (event->xbutton.button==Button2 && dock->type==WM_CLIP &&
3836 aicon->xindex==0 && aicon->yindex==0) {
3837 openClipWorkspaceMenu(scr, event->xbutton.x_root+2,
3838 event->xbutton.y_root+2);
3839 if (scr->clip_ws_menu) {
3840 WMenu *menu;
3841 menu = scr->clip_ws_menu;
3842 desc = &menu->menu->descriptor;
3844 event->xany.send_event = True;
3845 (*desc->handle_mousedown)(desc, event);
3847 } else if (event->xbutton.button == Button3) {
3848 openDockMenu(dock, aicon, event);
3853 static void
3854 showClipBalloon(WDock *dock, int workspace)
3856 int w, h;
3857 int x, y;
3858 WScreen *scr = dock->screen_ptr;
3859 char *text;
3860 Window stack[2];
3862 scr->flags.clip_balloon_mapped = 1;
3863 XMapWindow(dpy, scr->clip_balloon);
3865 text = scr->workspaces[workspace]->name;
3867 w = wTextWidth(scr->clip_title_font->font, text, strlen(text));
3869 h = scr->clip_title_font->height;
3870 XResizeWindow(dpy, scr->clip_balloon, w, h);
3872 x = dock->x_pos + CLIP_BUTTON_SIZE*ICON_SIZE/64;
3873 y = dock->y_pos + ICON_SIZE-scr->clip_title_font->height - 3;
3875 if (x+w > scr->scr_width) {
3876 x = scr->scr_width - w;
3877 if (dock->y_pos + ICON_SIZE + h > scr->scr_height)
3878 y = dock->y_pos - h - 1;
3879 else
3880 y = dock->y_pos + ICON_SIZE;
3881 XRaiseWindow(dpy, scr->clip_balloon);
3882 } else {
3883 stack[0] = scr->clip_icon->icon->core->window;
3884 stack[1] = scr->clip_balloon;
3885 XRestackWindows(dpy, stack, 2);
3887 XMoveWindow(dpy, scr->clip_balloon, x, y);
3888 XSetForeground(dpy, scr->clip_title_gc,
3889 scr->clip_title_pixel[CLIP_NORMAL]);
3890 XClearWindow(dpy, scr->clip_balloon);
3891 wDrawString(scr->clip_balloon, scr->clip_title_font, scr->clip_title_gc,
3892 0, scr->clip_title_font->y, text, strlen(text));
3896 static void
3897 clipEnterNotify(WObjDescriptor *desc, XEvent *event)
3899 WAppIcon *btn = (WAppIcon*)desc->parent;
3900 WDock *dock;
3902 assert(event->type==EnterNotify);
3904 if(desc->parent_type!=WCLASS_DOCK_ICON)
3905 return;
3907 dock = btn->dock;
3908 if (!dock || dock->type!=WM_CLIP)
3909 return;
3911 /* The auto raise/lower code */
3912 if (dock->auto_lower_magic) {
3913 WMDeleteTimerHandler(dock->auto_lower_magic);
3914 dock->auto_lower_magic = NULL;
3916 if (dock->auto_raise_lower && !dock->auto_raise_magic) {
3917 dock->auto_raise_magic = WMAddTimerHandler(AUTO_RAISE_DELAY,
3918 clipAutoRaise,
3919 (void *)dock);
3922 /* The auto expand/collapse code */
3923 if (dock->auto_collapse_magic) {
3924 WMDeleteTimerHandler(dock->auto_collapse_magic);
3925 dock->auto_collapse_magic = NULL;
3927 if (dock->auto_collapse && !dock->auto_expand_magic) {
3928 dock->auto_expand_magic = WMAddTimerHandler(AUTO_EXPAND_DELAY,
3929 clipAutoExpand,
3930 (void *)dock);
3933 if (btn->xindex == 0 && btn->yindex == 0)
3934 showClipBalloon(dock, dock->screen_ptr->current_workspace);
3935 else {
3936 if (dock->screen_ptr->flags.clip_balloon_mapped) {
3937 XUnmapWindow(dpy, dock->screen_ptr->clip_balloon);
3938 dock->screen_ptr->flags.clip_balloon_mapped = 0;
3944 static void
3945 clipLeave(WDock *dock)
3947 XEvent event;
3948 WObjDescriptor *desc = NULL;
3950 if (!dock || dock->type!=WM_CLIP)
3951 return;
3953 if (XCheckTypedEvent(dpy, EnterNotify, &event)!=False) {
3954 if (XFindContext(dpy, event.xcrossing.window, wWinContext,
3955 (XPointer *)&desc)!=XCNOENT
3956 && desc && desc->parent_type==WCLASS_DOCK_ICON
3957 && ((WAppIcon*)desc->parent)->dock
3958 && ((WAppIcon*)desc->parent)->dock->type==WM_CLIP) {
3959 /* We didn't left the Clip yet */
3960 XPutBackEvent(dpy, &event);
3961 return;
3964 XPutBackEvent(dpy, &event);
3965 } else {
3966 /* We entered a withdrawn window, so we're still in Clip */
3967 return;
3970 if (dock->auto_raise_magic) {
3971 WMDeleteTimerHandler(dock->auto_raise_magic);
3972 dock->auto_raise_magic = NULL;
3974 if (dock->auto_raise_lower && !dock->auto_lower_magic) {
3975 dock->auto_lower_magic = WMAddTimerHandler(AUTO_LOWER_DELAY,
3976 clipAutoLower,
3977 (void *)dock);
3980 if (dock->auto_expand_magic) {
3981 WMDeleteTimerHandler(dock->auto_expand_magic);
3982 dock->auto_expand_magic = NULL;
3984 if (dock->auto_collapse && !dock->auto_collapse_magic) {
3985 dock->auto_collapse_magic = WMAddTimerHandler(AUTO_COLLAPSE_DELAY,
3986 clipAutoCollapse,
3987 (void *)dock);
3992 static void
3993 clipLeaveNotify(WObjDescriptor *desc, XEvent *event)
3995 WAppIcon *btn = (WAppIcon*)desc->parent;
3997 assert(event->type==LeaveNotify);
3999 if(desc->parent_type!=WCLASS_DOCK_ICON)
4000 return;
4002 clipLeave(btn->dock);
4006 static void
4007 clipAutoCollapse(void *cdata)
4009 WDock *dock = (WDock *)cdata;
4011 if (dock->type!=WM_CLIP)
4012 return;
4014 if (dock->auto_collapse) {
4015 dock->collapsed = 1;
4016 wDockHideIcons(dock);
4018 dock->auto_collapse_magic = NULL;
4022 static void
4023 clipAutoExpand(void *cdata)
4025 WDock *dock = (WDock *)cdata;
4027 if (dock->type!=WM_CLIP)
4028 return;
4030 if (dock->auto_collapse) {
4031 dock->collapsed = 0;
4032 wDockShowIcons(dock);
4034 dock->auto_expand_magic = NULL;
4038 static void
4039 clipAutoLower(void *cdata)
4041 WDock *dock = (WDock *)cdata;
4043 if (dock->type!=WM_CLIP)
4044 return;
4046 if (dock->auto_raise_lower)
4047 wDockLower(dock);
4049 dock->auto_lower_magic = NULL;
4053 static void
4054 clipAutoRaise(void *cdata)
4056 WDock *dock = (WDock *)cdata;
4058 if (dock->type!=WM_CLIP)
4059 return;
4061 if (dock->auto_raise_lower)
4062 wDockRaise(dock);
4064 if (dock->screen_ptr->flags.clip_balloon_mapped) {
4065 showClipBalloon(dock, dock->screen_ptr->current_workspace);
4068 dock->auto_raise_magic = NULL;