new appicon grouping stuff
[wmaker-crm.git] / src / dock.c
blobdff6e81ab017ca680bd280a5e80041ad8f00c6dd
1 /* dock.c- built-in Dock module for WindowMaker
3 * Window Maker window manager
5 * Copyright (c) 1997, 1998 Alfredo K. Kojima
6 * Copyright (c) 1998, 1999 Dan Pascu
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
21 * USA.
25 #include "wconfig.h"
27 #include <X11/Xlib.h>
28 #include <X11/Xutil.h>
29 #include <stdlib.h>
30 #include <string.h>
31 #include <unistd.h>
32 #include <math.h>
33 #include <limits.h>
35 #ifndef PATH_MAX
36 #define PATH_MAX DEFAULT_PATH_MAX
37 #endif
39 #include "WindowMaker.h"
40 #include "wcore.h"
41 #include "window.h"
42 #include "icon.h"
43 #include "appicon.h"
44 #include "actions.h"
45 #include "stacking.h"
46 #include "dock.h"
47 #include "dialog.h"
48 #include "funcs.h"
49 #include "properties.h"
50 #include "menu.h"
51 #include "client.h"
52 #include "defaults.h"
53 #include "workspace.h"
54 #include "framewin.h"
55 #include "superfluous.h"
56 #include "wsound.h"
59 #include <proplist.h>
64 /**** Local variables ****/
65 #define CLIP_REWIND 1
66 #define CLIP_IDLE 0
67 #define CLIP_FORWARD 2
70 /**** Global variables ****/
72 /* in dockedapp.c */
73 extern void DestroyDockAppSettingsPanel();
75 extern void ShowDockAppSettingsPanel(WAppIcon *aicon);
78 extern XContext wWinContext;
80 extern Cursor wCursor[WCUR_LAST];
82 extern WPreferences wPreferences;
84 extern XContext wWinContext;
86 #ifdef OFFIX_DND
87 extern Atom _XA_DND_PROTOCOL;
88 #endif
91 #define MOD_MASK wPreferences.modifier_mask
93 extern void appIconMouseDown(WObjDescriptor *desc, XEvent *event);
95 #define ICON_SIZE wPreferences.icon_size
98 /***** Local variables ****/
100 static proplist_t dCommand=NULL;
101 #ifdef OFFIX_DND
102 static proplist_t dDropCommand=NULL;
103 #endif
104 static proplist_t dAutoLaunch, dLock;
105 static proplist_t dName, dForced, dBuggyApplication, dYes, dNo;
106 static proplist_t dHost, dDock, dClip;
107 static proplist_t dAutoAttractIcons;
109 static proplist_t dPosition, dApplications, dLowered, dCollapsed, dAutoCollapse;
111 static proplist_t dAutoRaiseLower, dOmnipresent;
113 static void dockIconPaint(WAppIcon *btn);
115 static void iconMouseDown(WObjDescriptor *desc, XEvent *event);
117 static pid_t execCommand(WAppIcon *btn, char *command, WSavedState *state);
119 static void trackDeadProcess(pid_t pid, unsigned char status, WDock *dock);
121 static int getClipButton(int px, int py);
123 static void toggleLowered(WDock *dock);
125 static void toggleCollapsed(WDock *dock);
127 static void clipIconExpose(WObjDescriptor *desc, XEvent *event);
129 static void clipLeave(WDock *dock);
131 static void handleClipChangeWorkspace(WScreen *scr, XEvent *event);
133 Bool moveIconBetweenDocks(WDock *src, WDock *dest, WAppIcon *icon, int x, int y);
135 static void clipEnterNotify(WObjDescriptor *desc, XEvent *event);
136 static void clipLeaveNotify(WObjDescriptor *desc, XEvent *event);
137 static void clipAutoCollapse(void *cdata);
138 static void clipAutoExpand(void *cdata);
139 static void launchDockedApplication(WAppIcon *btn);
141 static void clipAutoLower(void *cdata);
142 static void clipAutoRaise(void *cdata);
144 static void showClipBalloon(WDock *dock, int workspace);
146 #ifdef OFFIX_DND
148 #define DndNotDnd -1
149 #define DndUnknown 0
150 #define DndRawData 1
151 #define DndFile 2
152 #define DndFiles 3
153 #define DndText 4
154 #define DndDir 5
155 #define DndLink 6
156 #define DndExe 7
158 #define DndEND 8
160 #endif /* OFFIX_DND */
164 static void
165 make_keys()
167 if (dCommand!=NULL)
168 return;
170 dCommand = PLRetain(PLMakeString("Command"));
171 #ifdef OFFIX_DND
172 dDropCommand = PLRetain(PLMakeString("DropCommand"));
173 #endif
174 dLock = PLRetain(PLMakeString("Lock"));
175 dAutoLaunch = PLRetain(PLMakeString("AutoLaunch"));
176 dName = PLRetain(PLMakeString("Name"));
177 dForced = PLRetain(PLMakeString("Forced"));
178 dBuggyApplication = PLRetain(PLMakeString("BuggyApplication"));
179 dYes = PLRetain(PLMakeString("Yes"));
180 dNo = PLRetain(PLMakeString("No"));
181 dHost = PLRetain(PLMakeString("Host"));
183 dPosition = PLMakeString("Position");
184 dApplications = PLMakeString("Applications");
185 dLowered = PLMakeString("Lowered");
186 dCollapsed = PLMakeString("Collapsed");
187 dAutoCollapse = PLMakeString("AutoCollapse");
188 dAutoRaiseLower = PLMakeString("AutoRaiseLower");
189 dAutoAttractIcons = PLMakeString("AutoAttractIcons");
191 dOmnipresent = PLMakeString("Omnipresent");
193 dDock = PLMakeString("Dock");
194 dClip = PLMakeString("Clip");
199 static void
200 renameCallback(WMenu *menu, WMenuEntry *entry)
202 WDock *dock = entry->clientdata;
203 char buffer[128];
204 int wspace;
205 char *name;
207 assert(entry->clientdata!=NULL);
209 wspace = dock->screen_ptr->current_workspace;
211 name = wstrdup(dock->screen_ptr->workspaces[wspace]->name);
213 sprintf(buffer, _("Type the name for workspace %i:"), wspace+1);
214 if (wInputDialog(dock->screen_ptr, _("Rename Workspace"), buffer,
215 &name)) {
216 wWorkspaceRename(dock->screen_ptr, wspace, name);
218 if (name) {
219 wfree(name);
224 static void
225 toggleLoweredCallback(WMenu *menu, WMenuEntry *entry)
227 assert(entry->clientdata!=NULL);
229 toggleLowered(entry->clientdata);
231 wMenuPaint(menu);
236 static void
237 killCallback(WMenu *menu, WMenuEntry *entry)
239 WAppIcon *icon;
240 char *buffer;
242 if (!WCHECK_STATE(WSTATE_NORMAL))
243 return;
245 assert(entry->clientdata!=NULL);
247 icon = (WAppIcon*)entry->clientdata;
249 icon->editing = 1;
251 WCHANGE_STATE(WSTATE_MODAL);
253 buffer = wstrconcat(icon->wm_class,
254 _(" will be forcibly closed.\n"
255 "Any unsaved changes will be lost.\n"
256 "Please confirm."));
258 if (wPreferences.dont_confirm_kill
259 || wMessageDialog(menu->frame->screen_ptr, _("Kill Application"),
260 buffer, _("Yes"), _("No"), NULL)==WAPRDefault) {
261 if (icon->icon && icon->icon->owner) {
262 wClientKill(icon->icon->owner);
266 wfree(buffer);
268 icon->editing = 0;
270 WCHANGE_STATE(WSTATE_NORMAL);
274 /* TODO: replace this function with a member of the dock struct */
275 static int
276 numberOfSelectedIcons(WDock *dock)
278 WAppIcon *aicon;
279 int i, n;
281 n = 0;
282 for (i=1; i<dock->max_icons; i++) {
283 aicon = dock->icon_array[i];
284 if (aicon && aicon->icon->selected) {
285 n++;
289 return n;
293 static WMBag*
294 getSelected(WDock *dock)
296 WMBag *ret = WMCreateBag(8);
297 WAppIcon *btn;
298 int i;
300 for (i=1; i<dock->max_icons; i++) {
301 btn = dock->icon_array[i];
302 if (btn && btn->icon->selected) {
303 WMPutInBag(ret, btn);
307 return ret;
311 static void
312 paintClipButtons(WAppIcon *clipIcon, Bool lpushed, Bool rpushed)
314 Window win = clipIcon->icon->core->window;
315 WScreen *scr = clipIcon->icon->core->screen_ptr;
316 XPoint p[4];
317 int pt = CLIP_BUTTON_SIZE*ICON_SIZE/64;
318 int tp = ICON_SIZE - pt;
319 int as = pt - 15; /* 15 = 5+5+5 */
320 GC gc = scr->clip_title_gc;
321 #ifdef GRADIENT_CLIP_ARROW
322 Bool collapsed = clipIcon->dock->collapsed;
323 #endif
325 if (rpushed) {
326 p[0].x = tp+1;
327 p[0].y = 1;
328 p[1].x = ICON_SIZE-2;
329 p[1].y = 1;
330 p[2].x = ICON_SIZE-2;
331 p[2].y = pt-1;
332 } else if (lpushed) {
333 p[0].x = 1;
334 p[0].y = tp;
335 p[1].x = pt;
336 p[1].y = ICON_SIZE-2;
337 p[2].x = 1;
338 p[2].y = ICON_SIZE-2;
340 if (lpushed || rpushed) {
341 XSetForeground(dpy, scr->draw_gc, scr->white_pixel);
342 XFillPolygon(dpy, win, scr->draw_gc, p, 3, Convex, CoordModeOrigin);
343 XSetForeground(dpy, scr->draw_gc, scr->black_pixel);
345 #ifdef GRADIENT_CLIP_ARROW
346 if (!collapsed) {
347 XSetFillStyle(dpy, scr->copy_gc, FillTiled);
348 XSetTile(dpy, scr->copy_gc, scr->clip_arrow_gradient);
349 XSetClipMask(dpy, scr->copy_gc, None);
350 gc = scr->copy_gc;
352 #endif /* GRADIENT_CLIP_ARROW */
354 /* top right arrow */
355 p[0].x = p[3].x = ICON_SIZE-5-as;
356 p[0].y = p[3].y = 5;
357 p[1].x = ICON_SIZE-6;
358 p[1].y = 5;
359 p[2].x = ICON_SIZE-6;
360 p[2].y = 4+as;
361 if (rpushed) {
362 XFillPolygon(dpy, win, scr->draw_gc, p, 3, Convex, CoordModeOrigin);
363 XDrawLines(dpy, win, scr->draw_gc, p, 4, CoordModeOrigin);
364 } else {
365 #ifdef GRADIENT_CLIP_ARROW
366 if (!collapsed)
367 XSetTSOrigin(dpy, gc, ICON_SIZE-6-as, 5);
368 #endif
369 XFillPolygon(dpy, win, gc, p,3,Convex,CoordModeOrigin);
370 XDrawLines(dpy, win, gc, p,4,CoordModeOrigin);
373 /* bottom left arrow */
374 p[0].x = p[3].x = 5;
375 p[0].y = p[3].y = ICON_SIZE-5-as;
376 p[1].x = 5;
377 p[1].y = ICON_SIZE-6;
378 p[2].x = 4+as;
379 p[2].y = ICON_SIZE-6;
380 if (lpushed) {
381 XFillPolygon(dpy, win, scr->draw_gc, p, 3, Convex, CoordModeOrigin);
382 XDrawLines(dpy, win, scr->draw_gc, p, 4, CoordModeOrigin);
383 } else {
384 #ifdef GRADIENT_CLIP_ARROW
385 if (!collapsed)
386 XSetTSOrigin(dpy, gc, 5, ICON_SIZE-6-as);
387 #endif
388 XFillPolygon(dpy, win, gc, p,3,Convex,CoordModeOrigin);
389 XDrawLines(dpy, win, gc, p,4,CoordModeOrigin);
391 #ifdef GRADIENT_CLIP_ARROW
392 if (!collapsed)
393 XSetFillStyle(dpy, scr->copy_gc, FillSolid);
394 #endif
398 RImage*
399 wClipMakeTile(WScreen *scr, RImage *normalTile)
401 RImage *tile = RCloneImage(normalTile);
402 RColor black;
403 RColor dark;
404 RColor light;
405 int pt, tp;
406 int as;
408 pt = CLIP_BUTTON_SIZE*wPreferences.icon_size/64;
409 tp = wPreferences.icon_size-1 - pt;
410 as = pt - 15;
412 black.alpha = 255;
413 black.red = black.green = black.blue = 0;
415 dark.alpha = 0;
416 dark.red = dark.green = dark.blue = 60;
418 light.alpha = 0;
419 light.red = light.green = light.blue = 80;
422 /* top right */
423 ROperateLine(tile, RSubtractOperation, tp, 0, wPreferences.icon_size-2,
424 pt-1, &dark);
425 RDrawLine(tile, tp-1, 0, wPreferences.icon_size-1, pt+1, &black);
426 ROperateLine(tile, RAddOperation, tp, 2, wPreferences.icon_size-3,
427 pt, &light);
429 /* arrow bevel */
430 ROperateLine(tile, RSubtractOperation, ICON_SIZE - 7 - as, 4,
431 ICON_SIZE - 5, 4, &dark);
432 ROperateLine(tile, RSubtractOperation, ICON_SIZE - 6 - as, 5,
433 ICON_SIZE - 5, 6 + as, &dark);
434 ROperateLine(tile, RAddOperation, ICON_SIZE - 5, 4, ICON_SIZE - 5, 6 + as,
435 &light);
437 /* bottom left */
438 ROperateLine(tile, RAddOperation, 2, tp+2, pt-2,
439 wPreferences.icon_size-3, &dark);
440 RDrawLine(tile, 0, tp-1, pt+1, wPreferences.icon_size-1, &black);
441 ROperateLine(tile, RSubtractOperation, 0, tp-2, pt+1,
442 wPreferences.icon_size-2, &light);
444 /* arrow bevel */
445 ROperateLine(tile, RSubtractOperation, 4, ICON_SIZE - 7 - as, 4,
446 ICON_SIZE - 5, &dark);
447 ROperateLine(tile, RSubtractOperation, 5, ICON_SIZE - 6 - as,
448 6 + as, ICON_SIZE - 5, &dark);
449 ROperateLine(tile, RAddOperation, 4, ICON_SIZE - 5, 6 + as, ICON_SIZE - 5,
450 &light);
452 return tile;
456 static void
457 omnipresentCallback(WMenu *menu, WMenuEntry *entry)
459 WAppIcon *clickedIcon = entry->clientdata;
460 WAppIcon *aicon;
461 WDock *dock;
462 WMBag *selectedIcons;
463 int failed;
464 int i;
466 assert(entry->clientdata!=NULL);
468 dock = clickedIcon->dock;
470 selectedIcons = getSelected(dock);
472 if (!WMGetBagItemCount(selectedIcons))
473 WMPutInBag(selectedIcons, clickedIcon);
475 failed = 0;
476 for (i = 0; i < WMGetBagItemCount(selectedIcons); i++) {
477 aicon = WMGetFromBag(selectedIcons, i);
479 if (wClipMakeIconOmnipresent(aicon, !aicon->omnipresent) == WO_FAILED)
480 failed++;
481 else if (aicon->icon->selected)
482 wIconSelect(aicon->icon);
484 WMFreeBag(selectedIcons);
486 if (failed > 1) {
487 wMessageDialog(dock->screen_ptr, _("Warning"),
488 _("Some icons cannot be made omnipresent. "
489 "Please make sure that no other icon is "
490 "docked in the same positions on the other "
491 "workspaces and the Clip is not full in "
492 "some workspace."),
493 _("OK"), NULL, NULL);
494 } else if (failed == 1) {
495 wMessageDialog(dock->screen_ptr, _("Warning"),
496 _("Icon cannot be made omnipresent. "
497 "Please make sure that no other icon is "
498 "docked in the same position on the other "
499 "workspaces and the Clip is not full in "
500 "some workspace."),
501 _("OK"), NULL, NULL);
506 static void
507 removeIconsCallback(WMenu *menu, WMenuEntry *entry)
509 WAppIcon *clickedIcon = (WAppIcon*)entry->clientdata;
510 WDock *dock;
511 WAppIcon *aicon;
512 WMBag *selectedIcons;
513 int keepit;
514 WMBagIterator it;
516 assert(clickedIcon!=NULL);
518 dock = clickedIcon->dock;
520 selectedIcons = getSelected(dock);
522 if (WMGetBagItemCount(selectedIcons)) {
523 if (wMessageDialog(dock->screen_ptr, _("Workspace Clip"),
524 _("All selected icons will be removed!"),
525 _("OK"), _("Cancel"), NULL)!=WAPRDefault) {
526 WMFreeBag(selectedIcons);
527 return;
529 } else {
530 if (clickedIcon->xindex==0 && clickedIcon->yindex==0) {
531 WMFreeBag(selectedIcons);
532 return;
534 WMPutInBag(selectedIcons, clickedIcon);
537 WM_ITERATE_BAG(selectedIcons, aicon, it) {
538 keepit = aicon->running && wApplicationOf(aicon->main_window);
539 wDockDetach(dock, aicon);
540 if (keepit) {
541 PlaceIcon(dock->screen_ptr, &aicon->x_pos, &aicon->y_pos);
542 XMoveWindow(dpy, aicon->icon->core->window,
543 aicon->x_pos, aicon->y_pos);
544 if (!dock->mapped || dock->collapsed)
545 XMapWindow(dpy, aicon->icon->core->window);
548 WMFreeBag(selectedIcons);
550 if (wPreferences.auto_arrange_icons)
551 wArrangeIcons(dock->screen_ptr, True);
555 static void
556 keepIconsCallback(WMenu *menu, WMenuEntry *entry)
558 WAppIcon *clickedIcon = (WAppIcon*)entry->clientdata;
559 WDock *dock;
560 WAppIcon *aicon;
561 WMBag *selectedIcons;
562 WMBagIterator it;
564 assert(clickedIcon!=NULL);
565 dock = clickedIcon->dock;
567 selectedIcons = getSelected(dock);
569 if (!WMGetBagItemCount(selectedIcons)
570 && clickedIcon!=dock->screen_ptr->clip_icon) {
571 char *command = NULL;
573 if (!clickedIcon->command && !clickedIcon->editing) {
574 clickedIcon->editing = 1;
575 if (wInputDialog(dock->screen_ptr, _("Keep Icon"),
576 _("Type the command used to launch the application"),
577 &command)) {
578 if (command && (command[0]==0 ||
579 (command[0]=='-' && command[1]==0))) {
580 wfree(command);
581 command = NULL;
583 clickedIcon->command = command;
584 clickedIcon->editing = 0;
585 } else {
586 clickedIcon->editing = 0;
587 if (command)
588 wfree(command);
589 WMFreeBag(selectedIcons);
590 return;
594 WMPutInBag(selectedIcons, clickedIcon);
597 WM_ITERATE_BAG(selectedIcons, aicon, it) {
598 if (aicon->icon->selected)
599 wIconSelect(aicon->icon);
600 if (aicon && aicon->attracted && aicon->command) {
601 aicon->attracted = 0;
602 if (aicon->icon->shadowed) {
603 aicon->icon->shadowed = 0;
604 aicon->icon->force_paint = 1;
605 wAppIconPaint(aicon);
609 WMFreeBag(selectedIcons);
615 static void
616 toggleAutoAttractCallback(WMenu *menu, WMenuEntry *entry)
618 WDock *dock = (WDock*)entry->clientdata;
620 assert(entry->clientdata!=NULL);
622 dock->attract_icons = !dock->attract_icons;
623 /*if (!dock->attract_icons)
624 dock->keep_attracted = 0;*/
626 entry->flags.indicator_on = dock->attract_icons;
628 wMenuPaint(menu);
632 static void
633 selectCallback(WMenu *menu, WMenuEntry *entry)
635 WAppIcon *icon = (WAppIcon*)entry->clientdata;
637 assert(icon!=NULL);
639 wIconSelect(icon->icon);
641 wMenuPaint(menu);
645 static void
646 colectIconsCallback(WMenu *menu, WMenuEntry *entry)
648 WAppIcon *clickedIcon = (WAppIcon*)entry->clientdata;
649 WDock *clip;
650 WAppIcon *aicon;
651 int x, y, x_pos, y_pos;
653 assert(entry->clientdata!=NULL);
654 clip = clickedIcon->dock;
656 aicon = clip->screen_ptr->app_icon_list;
658 while (aicon) {
659 if (!aicon->docked && wDockFindFreeSlot(clip, &x, &y)) {
660 x_pos = clip->x_pos + x*ICON_SIZE;
661 y_pos = clip->y_pos + y*ICON_SIZE;
662 if (aicon->x_pos != x_pos || aicon->y_pos != y_pos) {
663 #ifdef ANIMATIONS
664 if (wPreferences.no_animations) {
665 XMoveWindow(dpy, aicon->icon->core->window, x_pos, y_pos);
666 } else {
667 SlideWindow(aicon->icon->core->window,
668 aicon->x_pos, aicon->y_pos, x_pos, y_pos);
670 #else
671 XMoveWindow(dpy, aicon->icon->core->window, x_pos, y_pos);
672 #endif /* ANIMATIONS */
674 aicon->attracted = 1;
675 if (!aicon->icon->shadowed) {
676 aicon->icon->shadowed = 1;
677 aicon->icon->force_paint = 1;
678 /* We don't do an wAppIconPaint() here because it's in
679 * wDockAttachIcon(). -Dan
682 wDockAttachIcon(clip, aicon, x, y);
683 if (clip->collapsed || !clip->mapped)
684 XUnmapWindow(dpy, aicon->icon->core->window);
686 aicon = aicon->next;
691 static void
692 selectIconsCallback(WMenu *menu, WMenuEntry *entry)
694 WAppIcon *clickedIcon = (WAppIcon*)entry->clientdata;
695 WDock *dock;
696 WMBag *selectedIcons;
697 WAppIcon *btn;
698 int i;
700 assert(clickedIcon!=NULL);
701 dock = clickedIcon->dock;
703 selectedIcons = getSelected(dock);
705 if (!WMGetBagItemCount(selectedIcons)) {
706 for (i=1; i<dock->max_icons; i++) {
707 btn = dock->icon_array[i];
708 if (btn && !btn->icon->selected) {
709 wIconSelect(btn->icon);
712 } else {
713 for (i = 0; i < WMGetBagItemCount(selectedIcons); i++) {
714 btn = WMGetFromBag(selectedIcons, i);
715 wIconSelect(btn->icon);
718 WMFreeBag(selectedIcons);
720 wMenuPaint(menu);
724 static void
725 toggleCollapsedCallback(WMenu *menu, WMenuEntry *entry)
727 assert(entry->clientdata!=NULL);
729 toggleCollapsed(entry->clientdata);
731 entry->flags.indicator_on = ((WDock*)entry->clientdata)->collapsed;
733 wMenuPaint(menu);
737 static void
738 toggleAutoCollapseCallback(WMenu *menu, WMenuEntry *entry)
740 WDock *dock;
741 assert(entry->clientdata!=NULL);
743 dock = (WDock*) entry->clientdata;
745 dock->auto_collapse = !dock->auto_collapse;
747 entry->flags.indicator_on = ((WDock*)entry->clientdata)->auto_collapse;
749 wMenuPaint(menu);
753 static void
754 toggleAutoRaiseLowerCallback(WMenu *menu, WMenuEntry *entry)
756 WDock *dock;
757 assert(entry->clientdata!=NULL);
759 dock = (WDock*) entry->clientdata;
761 dock->auto_raise_lower = !dock->auto_raise_lower;
763 entry->flags.indicator_on = ((WDock*)entry->clientdata)->auto_raise_lower;
765 wMenuPaint(menu);
769 static void
770 launchCallback(WMenu *menu, WMenuEntry *entry)
772 WAppIcon *btn = (WAppIcon*)entry->clientdata;
774 launchDockedApplication(btn);
778 static void
779 settingsCallback(WMenu *menu, WMenuEntry *entry)
781 WAppIcon *btn = (WAppIcon*)entry->clientdata;
783 if (btn->editing)
784 return;
785 ShowDockAppSettingsPanel(btn);
789 static void
790 hideCallback(WMenu *menu, WMenuEntry *entry)
792 WApplication *wapp;
793 WAppIcon *btn = (WAppIcon*)entry->clientdata;
795 wapp = wApplicationOf(btn->icon->owner->main_window);
797 if (wapp->flags.hidden) {
798 wWorkspaceChange(btn->icon->core->screen_ptr,wapp->last_workspace);
799 wUnhideApplication(wapp, False, False);
800 } else {
801 wHideApplication(wapp);
806 static void
807 unhideHereCallback(WMenu *menu, WMenuEntry *entry)
809 WApplication *wapp;
810 WAppIcon *btn = (WAppIcon*)entry->clientdata;
812 wapp = wApplicationOf(btn->icon->owner->main_window);
814 wUnhideApplication(wapp, False, True);
818 WAppIcon*
819 mainIconCreate(WScreen *scr, int type)
821 WAppIcon *btn;
822 int x_pos;
824 if (type == WM_CLIP) {
825 if (scr->clip_icon)
826 return scr->clip_icon;
827 btn = wAppIconCreateForDock(scr, NULL, "Logo", "WMClip", TILE_CLIP);
828 btn->icon->core->descriptor.handle_expose = clipIconExpose;
829 btn->icon->core->descriptor.handle_enternotify = clipEnterNotify;
830 btn->icon->core->descriptor.handle_leavenotify = clipLeaveNotify;
831 /*x_pos = scr->scr_width - ICON_SIZE*2 - DOCK_EXTRA_SPACE;*/
832 x_pos = 0;
833 } else {
834 btn = wAppIconCreateForDock(scr, NULL, "Logo", "WMDock", TILE_NORMAL);
835 x_pos = scr->scr_width - ICON_SIZE - DOCK_EXTRA_SPACE;
838 btn->xindex = 0;
839 btn->yindex = 0;
841 btn->icon->core->descriptor.handle_mousedown = iconMouseDown;
842 btn->icon->core->descriptor.parent_type = WCLASS_DOCK_ICON;
843 btn->icon->core->descriptor.parent = btn;
844 /*ChangeStackingLevel(btn->icon->core, WMDockLevel);*/
845 XMapWindow(dpy, btn->icon->core->window);
846 btn->x_pos = x_pos;
847 btn->y_pos = 0;
848 btn->docked = 1;
849 if (type == WM_CLIP)
850 scr->clip_icon = btn;
852 return btn;
856 static void
857 switchWSCommand(WMenu *menu, WMenuEntry *entry)
859 WAppIcon *btn, *icon = (WAppIcon*) entry->clientdata;
860 WScreen *scr = icon->icon->core->screen_ptr;
861 WDock *src, *dest;
862 WMBag *selectedIcons;
863 int x, y;
865 if (entry->order == scr->current_workspace)
866 return;
867 src = icon->dock;
868 dest = scr->workspaces[entry->order]->clip;
870 selectedIcons = getSelected(src);
872 if (WMGetBagItemCount(selectedIcons)) {
873 int i;
874 for (i = 0; i < WMGetBagItemCount(selectedIcons); i++) {
875 btn = WMGetFromBag(selectedIcons, i);
876 if (wDockFindFreeSlot(dest, &x, &y)) {
877 moveIconBetweenDocks(src, dest, btn, x, y);
878 XUnmapWindow(dpy, btn->icon->core->window);
881 } else if (icon != scr->clip_icon) {
882 if (wDockFindFreeSlot(dest, &x, &y)) {
883 moveIconBetweenDocks(src, dest, icon, x, y);
884 XUnmapWindow(dpy, icon->icon->core->window);
887 WMFreeBag(selectedIcons);
892 static void
893 launchDockedApplication(WAppIcon *btn)
895 WScreen *scr = btn->icon->core->screen_ptr;
897 if (!btn->launching && btn->command!=NULL) {
898 if (!btn->forced_dock) {
899 btn->relaunching = btn->running;
900 btn->running = 1;
902 if (btn->wm_instance || btn->wm_class) {
903 WWindowAttributes attr;
904 memset(&attr, 0, sizeof(WWindowAttributes));
905 wDefaultFillAttributes(scr, btn->wm_instance, btn->wm_class,
906 &attr, NULL, True);
908 if (!attr.no_appicon && !btn->buggy_app)
909 btn->launching = 1;
910 else
911 btn->running = 0;
913 btn->drop_launch = 0;
914 scr->last_dock = btn->dock;
915 btn->pid = execCommand(btn, btn->command, NULL);
916 if (btn->pid>0) {
917 if (btn->buggy_app) {
918 /* give feedback that the app was launched */
919 btn->launching = 1;
920 dockIconPaint(btn);
921 btn->launching = 0;
922 WMAddTimerHandler(200, (WMCallback*)dockIconPaint, btn);
923 } else {
924 dockIconPaint(btn);
926 } else {
927 wwarning(_("could not launch application %s\n"), btn->command);
928 btn->launching = 0;
929 if (!btn->relaunching)
930 btn->running = 0;
937 static void
938 updateWorkspaceMenu(WMenu *menu, WAppIcon *icon)
940 WScreen *scr = menu->frame->screen_ptr;
941 char title[MAX_WORKSPACENAME_WIDTH+1];
942 int i;
944 if (!menu || !icon)
945 return;
947 for (i=0; i<scr->workspace_count; i++) {
948 if (i < menu->entry_no) {
949 if (strcmp(menu->entries[i]->text,scr->workspaces[i]->name)!=0) {
950 wfree(menu->entries[i]->text);
951 strcpy(title, scr->workspaces[i]->name);
952 menu->entries[i]->text = wstrdup(title);
953 menu->flags.realized = 0;
955 menu->entries[i]->clientdata = (void*)icon;
956 } else {
957 strcpy(title, scr->workspaces[i]->name);
959 wMenuAddCallback(menu, title, switchWSCommand, (void*)icon);
961 menu->flags.realized = 0;
963 if (i == scr->current_workspace) {
964 wMenuSetEnabled(menu, i, False);
965 } else {
966 wMenuSetEnabled(menu, i, True);
970 if (!menu->flags.realized)
971 wMenuRealize(menu);
975 static WMenu*
976 makeWorkspaceMenu(WScreen *scr)
978 WMenu *menu;
980 menu = wMenuCreate(scr, NULL, False);
981 if (!menu)
982 wwarning(_("could not create workspace submenu for Clip menu"));
984 wMenuAddCallback(menu, "", switchWSCommand, (void*)scr->clip_icon);
986 menu->flags.realized = 0;
987 wMenuRealize(menu);
989 return menu;
993 static void
994 updateClipOptionsMenu(WMenu *menu, WDock *dock)
996 WMenuEntry *entry;
997 int index = 0;
999 if (!menu || !dock)
1000 return;
1002 /* keep on top */
1003 entry = menu->entries[index];
1004 if (dock->lowered) {
1005 entry->text = _("Keep on Top");
1006 } else {
1007 entry->text = _("Allow Lowering");
1009 entry->clientdata = dock;
1011 /* collapsed */
1012 entry = menu->entries[++index];
1013 entry->flags.indicator_on = dock->collapsed;
1014 entry->clientdata = dock;
1016 /* auto-collapse */
1017 entry = menu->entries[++index];
1018 entry->flags.indicator_on = dock->auto_collapse;
1019 entry->clientdata = dock;
1021 /* auto-raise/lower */
1022 entry = menu->entries[++index];
1023 entry->flags.indicator_on = dock->auto_raise_lower;
1024 entry->clientdata = dock;
1026 /* attract icons */
1027 entry = menu->entries[++index];
1028 entry->flags.indicator_on = dock->attract_icons;
1029 entry->clientdata = dock;
1031 menu->flags.realized = 0;
1032 wMenuRealize(menu);
1036 static WMenu*
1037 makeClipOptionsMenu(WScreen *scr)
1039 WMenu *menu;
1040 WMenuEntry *entry;
1042 menu = wMenuCreate(scr, NULL, False);
1043 if (!menu) {
1044 wwarning(_("could not create options submenu for Clip menu"));
1045 return NULL;
1048 entry = wMenuAddCallback(menu, _("Keep on Top"),
1049 toggleLoweredCallback, NULL);
1051 entry = wMenuAddCallback(menu, _("Collapsed"),
1052 toggleCollapsedCallback, NULL);
1053 entry->flags.indicator = 1;
1054 entry->flags.indicator_on = 1;
1055 entry->flags.indicator_type = MI_CHECK;
1057 entry = wMenuAddCallback(menu, _("AutoCollapse"),
1058 toggleAutoCollapseCallback, NULL);
1059 entry->flags.indicator = 1;
1060 entry->flags.indicator_on = 1;
1061 entry->flags.indicator_type = MI_CHECK;
1063 entry = wMenuAddCallback(menu, _("AutoRaiseLower"),
1064 toggleAutoRaiseLowerCallback, NULL);
1065 entry->flags.indicator = 1;
1066 entry->flags.indicator_on = 1;
1067 entry->flags.indicator_type = MI_CHECK;
1069 entry = wMenuAddCallback(menu, _("AutoAttract Icons"),
1070 toggleAutoAttractCallback, NULL);
1071 entry->flags.indicator = 1;
1072 entry->flags.indicator_on = 1;
1073 entry->flags.indicator_type = MI_CHECK;
1075 menu->flags.realized = 0;
1076 wMenuRealize(menu);
1078 return menu;
1082 static WMenu*
1083 dockMenuCreate(WScreen *scr, int type)
1085 WMenu *menu;
1086 WMenuEntry *entry;
1088 if (type == WM_CLIP && scr->clip_menu)
1089 return scr->clip_menu;
1091 menu = wMenuCreate(scr, NULL, False);
1092 if (type != WM_CLIP) {
1093 entry = wMenuAddCallback(menu, _("Keep on Top"),
1094 toggleLoweredCallback, NULL);
1095 } else {
1096 entry = wMenuAddCallback(menu, _("Clip Options"), NULL, NULL);
1097 scr->clip_options = makeClipOptionsMenu(scr);
1098 if (scr->clip_options)
1099 wMenuEntrySetCascade(menu, entry, scr->clip_options);
1101 entry = wMenuAddCallback(menu, _("Rename Workspace"), renameCallback,
1102 NULL);
1103 wfree(entry->text);
1104 entry->text = _("Rename Workspace");
1106 entry = wMenuAddCallback(menu, _("Selected"), selectCallback, NULL);
1107 entry->flags.indicator = 1;
1108 entry->flags.indicator_on = 1;
1109 entry->flags.indicator_type = MI_CHECK;
1111 entry = wMenuAddCallback(menu, _("Select All Icons"),
1112 selectIconsCallback, NULL);
1113 wfree(entry->text);
1114 entry->text = _("Select All Icons");
1116 entry = wMenuAddCallback(menu, _("Keep Icon"), keepIconsCallback, NULL);
1117 wfree(entry->text);
1118 entry->text = _("Keep Icon");
1120 entry = wMenuAddCallback(menu, _("Move Icon To"), NULL, NULL);
1121 wfree(entry->text);
1122 entry->text = _("Move Icon To");
1123 scr->clip_submenu = makeWorkspaceMenu(scr);
1124 if (scr->clip_submenu)
1125 wMenuEntrySetCascade(menu, entry, scr->clip_submenu);
1127 entry = wMenuAddCallback(menu, _("Remove Icon"), removeIconsCallback,
1128 NULL);
1129 wfree(entry->text);
1130 entry->text = _("Remove Icon");
1132 wMenuAddCallback(menu, _("Attract Icons"), colectIconsCallback, NULL);
1135 wMenuAddCallback(menu, _("Launch"), launchCallback, NULL);
1137 wMenuAddCallback(menu, _("Unhide Here"), unhideHereCallback, NULL);
1139 entry = wMenuAddCallback(menu, _("Hide"), hideCallback, NULL);
1140 wfree(entry->text);
1141 entry->text = _("Hide");
1143 wMenuAddCallback(menu, _("Settings..."), settingsCallback, NULL);
1145 wMenuAddCallback(menu, _("Kill"), killCallback, NULL);
1147 if (type == WM_CLIP)
1148 scr->clip_menu = menu;
1150 return menu;
1154 WDock*
1155 wDockCreate(WScreen *scr, int type)
1157 WDock *dock;
1158 WAppIcon *btn;
1159 int icon_count;
1161 make_keys();
1163 dock = wmalloc(sizeof(WDock));
1164 memset(dock, 0, sizeof(WDock));
1166 if (type == WM_CLIP)
1167 icon_count = CLIP_MAX_ICONS;
1168 else
1169 icon_count = scr->scr_height/wPreferences.icon_size;
1171 dock->icon_array = wmalloc(sizeof(WAppIcon*)*icon_count);
1172 memset(dock->icon_array, 0, sizeof(WAppIcon*)*icon_count);
1174 dock->max_icons = icon_count;
1176 btn = mainIconCreate(scr, type);
1178 btn->dock = dock;
1180 dock->x_pos = btn->x_pos;
1181 dock->y_pos = btn->y_pos;
1182 dock->screen_ptr = scr;
1183 dock->type = type;
1184 dock->icon_count = 1;
1185 dock->on_right_side = 1;
1186 dock->collapsed = 0;
1187 dock->auto_collapse = 0;
1188 dock->auto_collapse_magic = NULL;
1189 dock->auto_raise_lower = 0;
1190 dock->auto_lower_magic = NULL;
1191 dock->auto_raise_magic = NULL;
1192 dock->attract_icons = 0;
1193 dock->lowered = 1;
1194 dock->icon_array[0] = btn;
1195 wRaiseFrame(btn->icon->core);
1196 XMoveWindow(dpy, btn->icon->core->window, btn->x_pos, btn->y_pos);
1198 /* create dock menu */
1199 dock->menu = dockMenuCreate(scr, type);
1201 return dock;
1205 void
1206 wDockDestroy(WDock *dock)
1208 int i;
1209 WAppIcon *aicon;
1211 for (i=(dock->type == WM_CLIP) ? 1 : 0; i<dock->max_icons; i++) {
1212 aicon = dock->icon_array[i];
1213 if (aicon) {
1214 int keepit = aicon->running && wApplicationOf(aicon->main_window);
1215 wDockDetach(dock, aicon);
1216 if (keepit) {
1217 PlaceIcon(dock->screen_ptr, &aicon->x_pos, &aicon->y_pos);
1218 XMoveWindow(dpy, aicon->icon->core->window,
1219 aicon->x_pos, aicon->y_pos);
1220 if (!dock->mapped || dock->collapsed)
1221 XMapWindow(dpy, aicon->icon->core->window);
1225 if (wPreferences.auto_arrange_icons)
1226 wArrangeIcons(dock->screen_ptr, True);
1227 wfree(dock->icon_array);
1228 if (dock->menu && dock->type!=WM_CLIP)
1229 wMenuDestroy(dock->menu, True);
1230 if (dock->screen_ptr->last_dock == dock)
1231 dock->screen_ptr->last_dock = NULL;
1232 wfree(dock);
1236 void
1237 wClipIconPaint(WAppIcon *aicon)
1239 WScreen *scr = aicon->icon->core->screen_ptr;
1240 WWorkspace *workspace = scr->workspaces[scr->current_workspace];
1241 GC gc;
1242 Window win = aicon->icon->core->window;
1243 int length, nlength;
1244 char *ws_name, ws_number[10];
1245 int ty, tx;
1247 wIconPaint(aicon->icon);
1249 length = strlen(workspace->name);
1250 ws_name = malloc(length + 1);
1251 sprintf(ws_name, "%s", workspace->name);
1252 sprintf(ws_number, "%i", scr->current_workspace + 1);
1253 nlength = strlen(ws_number);
1255 gc = scr->clip_title_gc;
1257 if (!workspace->clip->collapsed)
1258 XSetForeground(dpy, gc, scr->clip_title_pixel[CLIP_NORMAL]);
1259 else
1260 XSetForeground(dpy, gc, scr->clip_title_pixel[CLIP_COLLAPSED]);
1262 ty = ICON_SIZE - WMFontHeight(scr->clip_title_font) - 3;
1264 tx = CLIP_BUTTON_SIZE*ICON_SIZE/64;
1266 WMDrawString(scr->wmscreen, win, gc, scr->clip_title_font, tx,
1267 ty, ws_name, length);
1269 tx = (ICON_SIZE/2 - WMWidthOfString(scr->clip_title_font, ws_number,
1270 nlength))/2;
1272 WMDrawString(scr->wmscreen, win, gc, scr->clip_title_font, tx,
1273 2, ws_number, nlength);
1275 wfree(ws_name);
1277 if (aicon->launching) {
1278 XFillRectangle(dpy, aicon->icon->core->window, scr->stipple_gc,
1279 0, 0, wPreferences.icon_size, wPreferences.icon_size);
1281 paintClipButtons(aicon, aicon->dock->lclip_button_pushed,
1282 aicon->dock->rclip_button_pushed);
1286 static void
1287 clipIconExpose(WObjDescriptor *desc, XEvent *event)
1289 wClipIconPaint(desc->parent);
1293 static void
1294 dockIconPaint(WAppIcon *btn)
1296 if (btn == btn->icon->core->screen_ptr->clip_icon)
1297 wClipIconPaint(btn);
1298 else
1299 wAppIconPaint(btn);
1303 static proplist_t
1304 make_icon_state(WAppIcon *btn)
1306 proplist_t node = NULL;
1307 proplist_t command, autolaunch, lock, name, forced, host, position, buggy;
1308 proplist_t omnipresent;
1309 char *tmp;
1310 char buffer[64];
1312 if (btn) {
1313 if (!btn->command)
1314 command = PLMakeString("-");
1315 else
1316 command = PLMakeString(btn->command);
1318 autolaunch = btn->auto_launch ? dYes : dNo;
1320 lock = btn->lock ? dYes : dNo;
1322 tmp = EscapeWM_CLASS(btn->wm_instance, btn->wm_class);
1324 name = PLMakeString(tmp);
1326 wfree(tmp);
1328 forced = btn->forced_dock ? dYes : dNo;
1330 buggy = btn->buggy_app ? dYes : dNo;
1332 if (btn == btn->icon->core->screen_ptr->clip_icon)
1333 sprintf(buffer, "%i,%i", btn->x_pos, btn->y_pos);
1334 else
1335 sprintf(buffer, "%hi,%hi", btn->xindex, btn->yindex);
1336 position = PLMakeString(buffer);
1338 node = PLMakeDictionaryFromEntries(dCommand, command,
1339 dName, name,
1340 dAutoLaunch, autolaunch,
1341 dLock, lock,
1342 dForced, forced,
1343 dBuggyApplication, buggy,
1344 dPosition, position,
1345 NULL);
1346 PLRelease(command);
1347 PLRelease(name);
1348 PLRelease(position);
1350 omnipresent = btn->omnipresent ? dYes : dNo;
1351 if (btn->dock != btn->icon->core->screen_ptr->dock &&
1352 (btn->xindex != 0 || btn->yindex != 0))
1353 PLInsertDictionaryEntry(node, dOmnipresent, omnipresent);
1355 #ifdef OFFIX_DND
1356 if (btn->dnd_command) {
1357 command = PLMakeString(btn->dnd_command);
1358 PLInsertDictionaryEntry(node, dDropCommand, command);
1359 PLRelease(command);
1361 #endif /* OFFIX_DND */
1363 if (btn->client_machine && btn->remote_start) {
1364 host = PLMakeString(btn->client_machine);
1365 PLInsertDictionaryEntry(node, dHost, host);
1366 PLRelease(host);
1370 return node;
1374 static proplist_t
1375 dockSaveState(WDock *dock)
1377 int i;
1378 proplist_t icon_info;
1379 proplist_t list=NULL, dock_state=NULL;
1380 proplist_t value, key;
1381 char buffer[256];
1383 list = PLMakeArrayFromElements(NULL);
1385 for (i=(dock->type==WM_DOCK ? 0 : 1); i<dock->max_icons; i++) {
1386 WAppIcon *btn = dock->icon_array[i];
1388 if (!btn || btn->attracted)
1389 continue;
1391 if ((icon_info = make_icon_state(dock->icon_array[i]))) {
1392 list = PLAppendArrayElement(list, icon_info);
1393 PLRelease(icon_info);
1397 dock_state = PLMakeDictionaryFromEntries(dApplications, list,
1398 NULL);
1400 if (dock->type == WM_DOCK) {
1401 sprintf(buffer, "Applications%i", dock->screen_ptr->scr_height);
1402 key = PLMakeString(buffer);
1403 PLInsertDictionaryEntry(dock_state, key, list);
1404 PLRelease(key);
1407 sprintf(buffer, "%i,%i", (dock->on_right_side ? -ICON_SIZE : 0),
1408 dock->y_pos);
1409 value = PLMakeString(buffer);
1410 PLInsertDictionaryEntry(dock_state, dPosition, value);
1411 PLRelease(value);
1413 PLRelease(list);
1416 value = (dock->lowered ? dYes : dNo);
1417 PLInsertDictionaryEntry(dock_state, dLowered, value);
1419 if (dock->type == WM_CLIP) {
1420 value = (dock->collapsed ? dYes : dNo);
1421 PLInsertDictionaryEntry(dock_state, dCollapsed, value);
1423 value = (dock->auto_collapse ? dYes : dNo);
1424 PLInsertDictionaryEntry(dock_state, dAutoCollapse, value);
1426 value = (dock->auto_raise_lower ? dYes : dNo);
1427 PLInsertDictionaryEntry(dock_state, dAutoRaiseLower, value);
1429 value = (dock->attract_icons ? dYes : dNo);
1430 PLInsertDictionaryEntry(dock_state, dAutoAttractIcons, value);
1433 return dock_state;
1437 void
1438 wDockSaveState(WScreen *scr, proplist_t old_state)
1440 proplist_t dock_state;
1441 proplist_t keys;
1443 dock_state = dockSaveState(scr->dock);
1446 * Copy saved states of docks with different sizes.
1448 if (old_state) {
1449 int i;
1450 proplist_t tmp;
1452 keys = PLGetAllDictionaryKeys(old_state);
1453 for (i = 0; i < PLGetNumberOfElements(keys); i++) {
1454 tmp = PLGetArrayElement(keys, i);
1456 if (strncasecmp(PLGetString(tmp), "applications", 12) == 0
1457 && !PLGetDictionaryEntry(dock_state, tmp)) {
1459 PLInsertDictionaryEntry(dock_state,
1460 tmp,
1461 PLGetDictionaryEntry(old_state, tmp));
1464 PLRelease(keys);
1468 PLInsertDictionaryEntry(scr->session_state, dDock, dock_state);
1470 PLRelease(dock_state);
1474 void
1475 wClipSaveState(WScreen *scr)
1477 proplist_t clip_state;
1479 clip_state = make_icon_state(scr->clip_icon);
1481 PLInsertDictionaryEntry(scr->session_state, dClip, clip_state);
1483 PLRelease(clip_state);
1487 proplist_t
1488 wClipSaveWorkspaceState(WScreen *scr, int workspace)
1490 return dockSaveState(scr->workspaces[workspace]->clip);
1494 static Bool
1495 getBooleanDockValue(proplist_t value, proplist_t key)
1497 if (value) {
1498 if (PLIsString(value)) {
1499 if (strcasecmp(PLGetString(value), "YES")==0)
1500 return True;
1501 } else {
1502 wwarning(_("bad value in docked icon state info %s"),
1503 PLGetString(key));
1506 return False;
1510 static WAppIcon*
1511 restore_icon_state(WScreen *scr, proplist_t info, int type, int index)
1513 WAppIcon *aicon;
1514 proplist_t cmd, value;
1517 cmd = PLGetDictionaryEntry(info, dCommand);
1518 if (!cmd || !PLIsString(cmd)) {
1519 return NULL;
1522 /* parse window name */
1523 value = PLGetDictionaryEntry(info, dName);
1524 if (!value)
1525 return NULL;
1528 char *wclass, *winstance;
1529 char *command;
1531 ParseWindowName(value, &winstance, &wclass, "dock");
1533 if (!winstance && !wclass) {
1534 return NULL;
1537 /* get commands */
1539 if (cmd)
1540 command = wstrdup(PLGetString(cmd));
1541 else
1542 command = NULL;
1544 if (!command || strcmp(command, "-")==0) {
1545 if (command)
1546 wfree(command);
1547 if (wclass)
1548 wfree(wclass);
1549 if (winstance)
1550 wfree(winstance);
1552 return NULL;
1555 aicon = wAppIconCreateForDock(scr, command, winstance, wclass,
1556 TILE_NORMAL);
1557 if (wclass)
1558 wfree(wclass);
1559 if (winstance)
1560 wfree(winstance);
1561 if (command)
1562 wfree(command);
1565 aicon->icon->core->descriptor.handle_mousedown = iconMouseDown;
1566 if (type == WM_CLIP) {
1567 aicon->icon->core->descriptor.handle_enternotify = clipEnterNotify;
1568 aicon->icon->core->descriptor.handle_leavenotify = clipLeaveNotify;
1570 aicon->icon->core->descriptor.parent_type = WCLASS_DOCK_ICON;
1571 aicon->icon->core->descriptor.parent = aicon;
1574 #ifdef OFFIX_DND
1575 cmd = PLGetDictionaryEntry(info, dDropCommand);
1576 if (cmd)
1577 aicon->dnd_command = wstrdup(PLGetString(cmd));
1578 #endif
1580 /* check auto launch */
1581 value = PLGetDictionaryEntry(info, dAutoLaunch);
1583 aicon->auto_launch = getBooleanDockValue(value, dAutoLaunch);
1585 /* check lock */
1586 value = PLGetDictionaryEntry(info, dLock);
1588 aicon->lock = getBooleanDockValue(value, dLock);
1590 /* check if it wasn't normally docked */
1591 value = PLGetDictionaryEntry(info, dForced);
1593 aicon->forced_dock = getBooleanDockValue(value, dForced);
1595 /* check if we can rely on the stuff in the app */
1596 value = PLGetDictionaryEntry(info, dBuggyApplication);
1598 aicon->buggy_app = getBooleanDockValue(value, dBuggyApplication);
1600 /* get position in the dock */
1601 value = PLGetDictionaryEntry(info, dPosition);
1602 if (value && PLIsString(value)) {
1603 if (sscanf(PLGetString(value), "%hi,%hi", &aicon->xindex,
1604 &aicon->yindex)!=2)
1605 wwarning(_("bad value in docked icon state info %s"),
1606 PLGetString(dPosition));
1608 /* check position sanity */
1609 /* incomplete section! */
1610 if (type == WM_DOCK) {
1611 aicon->xindex = 0;
1612 if (aicon->yindex < 0)
1613 wwarning(_("bad value in docked icon position %i,%i"),
1614 aicon->xindex, aicon->yindex);
1616 } else {
1617 aicon->yindex = index;
1618 aicon->xindex = 0;
1621 /* check if icon is omnipresent */
1622 value = PLGetDictionaryEntry(info, dOmnipresent);
1624 aicon->omnipresent = getBooleanDockValue(value, dOmnipresent);
1626 aicon->running = 0;
1627 aicon->docked = 1;
1629 return aicon;
1633 #define COMPLAIN(key) wwarning(_("bad value in dock state info:%s"), key)
1636 WAppIcon*
1637 wClipRestoreState(WScreen *scr, proplist_t clip_state)
1639 WAppIcon *icon;
1640 proplist_t value;
1643 icon = mainIconCreate(scr, WM_CLIP);
1645 if (!clip_state)
1646 return icon;
1647 else
1648 PLRetain(clip_state);
1650 /* restore position */
1652 value = PLGetDictionaryEntry(clip_state, dPosition);
1654 if (value) {
1655 if (!PLIsString(value))
1656 COMPLAIN("Position");
1657 else {
1658 if (sscanf(PLGetString(value), "%i,%i", &icon->x_pos,
1659 &icon->y_pos)!=2)
1660 COMPLAIN("Position");
1662 /* check position sanity */
1663 if (icon->y_pos < 0)
1664 icon->y_pos = 0;
1665 else if (icon->y_pos > scr->scr_height-ICON_SIZE)
1666 icon->y_pos = scr->scr_height-ICON_SIZE;
1668 if (icon->x_pos < 0)
1669 icon->x_pos = 0;
1670 else if (icon->x_pos > scr->scr_width-ICON_SIZE)
1671 icon->x_pos = scr->scr_width-ICON_SIZE;
1675 #ifdef OFFIX_DND
1676 value = PLGetDictionaryEntry(clip_state, dDropCommand);
1677 if (value && PLIsString(value))
1678 icon->dnd_command = wstrdup(PLGetString(value));
1679 #endif
1681 PLRelease(clip_state);
1683 return icon;
1687 WDock*
1688 wDockRestoreState(WScreen *scr, proplist_t dock_state, int type)
1690 WDock *dock;
1691 proplist_t apps;
1692 proplist_t value;
1693 WAppIcon *aicon, *old_top;
1694 int count, i;
1697 dock = wDockCreate(scr, type);
1699 if (!dock_state)
1700 return dock;
1702 if (dock_state)
1703 PLRetain(dock_state);
1706 /* restore position */
1708 value = PLGetDictionaryEntry(dock_state, dPosition);
1710 if (value) {
1711 if (!PLIsString(value))
1712 COMPLAIN("Position");
1713 else {
1714 if (sscanf(PLGetString(value), "%i,%i", &dock->x_pos,
1715 &dock->y_pos)!=2)
1716 COMPLAIN("Position");
1718 /* check position sanity */
1719 if (dock->y_pos < 0)
1720 dock->y_pos = 0;
1721 else if (dock->y_pos > scr->scr_height-ICON_SIZE)
1722 dock->y_pos = scr->scr_height - ICON_SIZE;
1724 /* This is no more needed. ??? */
1725 if (type == WM_CLIP) {
1726 if (dock->x_pos < 0)
1727 dock->x_pos = 0;
1728 else if (dock->x_pos > scr->scr_width-ICON_SIZE)
1729 dock->x_pos = scr->scr_width-ICON_SIZE;
1730 } else {
1731 if (dock->x_pos >= 0) {
1732 dock->x_pos = DOCK_EXTRA_SPACE;
1733 dock->on_right_side = 0;
1734 } else {
1735 dock->x_pos = scr->scr_width - DOCK_EXTRA_SPACE - ICON_SIZE;
1736 dock->on_right_side = 1;
1742 /* restore lowered/raised state */
1744 dock->lowered = 0;
1746 value = PLGetDictionaryEntry(dock_state, dLowered);
1748 if (value) {
1749 if (!PLIsString(value))
1750 COMPLAIN("Lowered");
1751 else {
1752 if (strcasecmp(PLGetString(value), "YES")==0)
1753 dock->lowered = 1;
1758 /* restore collapsed state */
1760 dock->collapsed = 0;
1762 value = PLGetDictionaryEntry(dock_state, dCollapsed);
1764 if (value) {
1765 if (!PLIsString(value))
1766 COMPLAIN("Collapsed");
1767 else {
1768 if (strcasecmp(PLGetString(value), "YES")==0)
1769 dock->collapsed = 1;
1774 /* restore auto-collapsed state */
1776 value = PLGetDictionaryEntry(dock_state, dAutoCollapse);
1778 if (value) {
1779 if (!PLIsString(value))
1780 COMPLAIN("AutoCollapse");
1781 else {
1782 if (strcasecmp(PLGetString(value), "YES")==0) {
1783 dock->auto_collapse = 1;
1784 dock->collapsed = 1;
1790 /* restore auto-raise/lower state */
1792 value = PLGetDictionaryEntry(dock_state, dAutoRaiseLower);
1794 if (value) {
1795 if (!PLIsString(value))
1796 COMPLAIN("AutoRaiseLower");
1797 else {
1798 if (strcasecmp(PLGetString(value), "YES")==0) {
1799 dock->auto_raise_lower = 1;
1804 /* restore attract icons state */
1806 dock->attract_icons = 0;
1808 value = PLGetDictionaryEntry(dock_state, dAutoAttractIcons);
1810 if (value) {
1811 if (!PLIsString(value))
1812 COMPLAIN("AutoAttractIcons");
1813 else {
1814 if (strcasecmp(PLGetString(value), "YES")==0)
1815 dock->attract_icons = 1;
1820 /* application list */
1823 proplist_t tmp;
1824 char buffer[64];
1827 * When saving, it saves the dock state in
1828 * Applications and Applicationsnnn
1830 * When loading, it will first try Applicationsnnn.
1831 * If it does not exist, use Applications as default.
1834 sprintf(buffer, "Applications%i", scr->scr_height);
1836 tmp = PLMakeString(buffer);
1837 apps = PLGetDictionaryEntry(dock_state, tmp);
1838 PLRelease(tmp);
1840 if (!apps) {
1841 apps = PLGetDictionaryEntry(dock_state, dApplications);
1845 if (!apps) {
1846 goto finish;
1849 count = PLGetNumberOfElements(apps);
1851 if (count==0)
1852 goto finish;
1854 old_top = dock->icon_array[0];
1856 /* dock->icon_count is set to 1 when dock is created.
1857 * Since Clip is already restored, we want to keep it so for clip,
1858 * but for dock we may change the default top tile, so we set it to 0.
1860 if (type == WM_DOCK)
1861 dock->icon_count = 0;
1863 for (i=0; i<count; i++) {
1864 if (dock->icon_count >= dock->max_icons) {
1865 wwarning(_("there are too many icons stored in dock. Ignoring what doesn't fit"));
1866 break;
1869 value = PLGetArrayElement(apps, i);
1870 aicon = restore_icon_state(scr, value, type, dock->icon_count);
1872 dock->icon_array[dock->icon_count] = aicon;
1874 if (aicon) {
1875 aicon->dock = dock;
1876 aicon->x_pos = dock->x_pos + (aicon->xindex*ICON_SIZE);
1877 aicon->y_pos = dock->y_pos + (aicon->yindex*ICON_SIZE);
1879 if (dock->lowered)
1880 ChangeStackingLevel(aicon->icon->core, WMNormalLevel);
1881 else
1882 ChangeStackingLevel(aicon->icon->core, WMDockLevel);
1884 wCoreConfigure(aicon->icon->core, aicon->x_pos, aicon->y_pos,
1885 0, 0);
1887 if (!dock->collapsed)
1888 XMapWindow(dpy, aicon->icon->core->window);
1889 wRaiseFrame(aicon->icon->core);
1891 dock->icon_count++;
1892 } else if (dock->icon_count==0 && type==WM_DOCK)
1893 dock->icon_count++;
1896 /* if the first icon is not defined, use the default */
1897 if (dock->icon_array[0]==NULL) {
1898 /* update default icon */
1899 old_top->x_pos = dock->x_pos;
1900 old_top->y_pos = dock->y_pos;
1901 if (dock->lowered)
1902 ChangeStackingLevel(old_top->icon->core, WMNormalLevel);
1903 else
1904 ChangeStackingLevel(old_top->icon->core, WMDockLevel);
1905 dock->icon_array[0] = old_top;
1906 XMoveWindow(dpy, old_top->icon->core->window, dock->x_pos, dock->y_pos);
1907 /* we don't need to increment dock->icon_count here because it was
1908 * incremented in the loop above.
1910 } else if (old_top!=dock->icon_array[0]) {
1911 if (old_top == scr->clip_icon)
1912 scr->clip_icon = dock->icon_array[0];
1913 wAppIconDestroy(old_top);
1916 finish:
1917 if (dock_state)
1918 PLRelease(dock_state);
1920 return dock;
1925 void
1926 wDockLaunchWithState(WDock *dock, WAppIcon *btn, WSavedState *state)
1928 if (btn && btn->command && !btn->running && !btn->launching) {
1930 btn->drop_launch = 0;
1932 btn->pid = execCommand(btn, btn->command, state);
1934 if (btn->pid>0) {
1935 if (!btn->forced_dock && !btn->buggy_app) {
1936 btn->launching = 1;
1937 dockIconPaint(btn);
1940 } else {
1941 wfree(state);
1946 void
1947 wDockDoAutoLaunch(WDock *dock, int workspace)
1949 WAppIcon *btn;
1950 WSavedState *state;
1951 int i;
1953 for (i = 0; i < dock->max_icons; i++) {
1954 btn = dock->icon_array[i];
1955 if (!btn || !btn->auto_launch)
1956 continue;
1958 state = wmalloc(sizeof(WSavedState));
1959 memset(state, 0, sizeof(WSavedState));
1960 state->workspace = workspace;
1961 /* TODO: this is klugy and is very difficult to understand
1962 * what's going on. Try to clean up */
1963 wDockLaunchWithState(dock, btn, state);
1968 #ifdef OFFIX_DND
1969 static WDock*
1970 findDock(WScreen *scr, XEvent *event, int *icon_pos)
1972 WDock *dock;
1973 int i;
1975 *icon_pos = -1;
1976 if ((dock = scr->dock)!=NULL) {
1977 for (i=0; i<dock->max_icons; i++) {
1978 if (dock->icon_array[i]
1979 && dock->icon_array[i]->icon->core->window==event->xclient.window) {
1980 *icon_pos = i;
1981 break;
1985 if (*icon_pos<0 && (dock = scr->workspaces[scr->current_workspace]->clip)!=NULL) {
1986 for (i=0; i<dock->max_icons; i++) {
1987 if (dock->icon_array[i]
1988 && dock->icon_array[i]->icon->core->window==event->xclient.window) {
1989 *icon_pos = i;
1990 break;
1994 if(*icon_pos>=0)
1995 return dock;
1996 return NULL;
2001 wDockReceiveDNDDrop(WScreen *scr, XEvent *event)
2003 WDock *dock;
2004 WAppIcon *btn;
2005 int icon_pos;
2007 dock = findDock(scr, event, &icon_pos);
2008 if (!dock)
2009 return False;
2012 * Return True if the drop was on an application icon window.
2013 * In this case, let the ClientMessage handler redirect the
2014 * message to the app.
2016 if (dock->icon_array[icon_pos]->icon->icon_win!=None)
2017 return True;
2019 if (dock->icon_array[icon_pos]->dnd_command!=NULL) {
2020 scr->flags.dnd_data_convertion_status = 0;
2022 btn = dock->icon_array[icon_pos];
2024 if (!btn->forced_dock) {
2025 btn->relaunching = btn->running;
2026 btn->running = 1;
2028 if (btn->wm_instance || btn->wm_class) {
2029 WWindowAttributes attr;
2030 memset(&attr, 0, sizeof(WWindowAttributes));
2031 wDefaultFillAttributes(btn->icon->core->screen_ptr,
2032 btn->wm_instance,
2033 btn->wm_class, &attr, NULL, True);
2035 if (!attr.no_appicon)
2036 btn->launching = 1;
2037 else
2038 btn->running = 0;
2041 btn->drop_launch = 1;
2042 scr->last_dock = dock;
2043 btn->pid = execCommand(btn, btn->dnd_command, NULL);
2044 if (btn->pid>0) {
2045 dockIconPaint(btn);
2046 } else {
2047 btn->launching = 0;
2048 if (!btn->relaunching) {
2049 btn->running = 0;
2053 return False;
2055 #endif /* OFFIX_DND */
2059 Bool
2060 wDockAttachIcon(WDock *dock, WAppIcon *icon, int x, int y)
2062 WWindow *wwin;
2063 char **argv;
2064 int argc;
2065 int index;
2067 wwin = icon->icon->owner;
2068 if (icon->command==NULL) {
2069 icon->editing = 0;
2070 if (XGetCommand(dpy, wwin->client_win, &argv, &argc) && argc>0) {
2072 icon->command = wtokenjoin(argv, argc);
2073 XFreeStringList(argv);
2074 } else {
2075 char *command=NULL;
2077 /* icon->forced_dock = 1;*/
2078 if (dock->type!=WM_CLIP || !icon->attracted) {
2079 icon->editing = 1;
2080 if (wInputDialog(dock->screen_ptr, _("Dock Icon"),
2081 _("Type the command used to launch the application"),
2082 &command)) {
2083 if (command && (command[0]==0 ||
2084 (command[0]=='-' && command[1]==0))) {
2085 wfree(command);
2086 command = NULL;
2088 icon->command = command;
2089 icon->editing = 0;
2090 } else {
2091 icon->editing = 0;
2092 if (command)
2093 wfree(command);
2094 /* If the target is the dock, reject the icon. If
2095 * the target is the clip, make it an attracted icon
2097 if (dock->type==WM_CLIP) {
2098 icon->attracted = 1;
2099 if (!icon->icon->shadowed) {
2100 icon->icon->shadowed = 1;
2101 icon->icon->force_paint = 1;
2103 } else {
2104 return False;
2109 } else {
2110 icon->editing = 0;
2113 for (index=1; index<dock->max_icons; index++)
2114 if (dock->icon_array[index] == NULL)
2115 break;
2116 /* if (index == dock->max_icons)
2117 return; */
2119 assert(index < dock->max_icons);
2121 dock->icon_array[index] = icon;
2122 icon->yindex = y;
2123 icon->xindex = x;
2125 icon->omnipresent = 0;
2127 icon->x_pos = dock->x_pos + x*ICON_SIZE;
2128 icon->y_pos = dock->y_pos + y*ICON_SIZE;
2130 dock->icon_count++;
2132 icon->running = 1;
2133 icon->launching = 0;
2134 icon->docked = 1;
2135 icon->dock = dock;
2136 icon->icon->core->descriptor.handle_mousedown = iconMouseDown;
2137 if (dock->type == WM_CLIP) {
2138 icon->icon->core->descriptor.handle_enternotify = clipEnterNotify;
2139 icon->icon->core->descriptor.handle_leavenotify = clipLeaveNotify;
2141 icon->icon->core->descriptor.parent_type = WCLASS_DOCK_ICON;
2142 icon->icon->core->descriptor.parent = icon;
2144 MoveInStackListUnder(dock->icon_array[index-1]->icon->core,
2145 icon->icon->core);
2146 wAppIconMove(icon, icon->x_pos, icon->y_pos);
2147 wAppIconPaint(icon);
2149 if (wPreferences.auto_arrange_icons)
2150 wArrangeIcons(dock->screen_ptr, True);
2152 #ifdef OFFIX_DND
2153 if (icon->command && !icon->dnd_command) {
2154 icon->dnd_command = wmalloc(strlen(icon->command)+8);
2155 sprintf(icon->dnd_command, "%s %%d", icon->command);
2157 #endif
2159 return True;
2163 void
2164 reattachIcon(WDock *dock, WAppIcon *icon, int x, int y)
2166 int index;
2168 for(index=1; index<dock->max_icons; index++) {
2169 if(dock->icon_array[index] == icon)
2170 break;
2172 assert(index < dock->max_icons);
2174 icon->yindex = y;
2175 icon->xindex = x;
2177 icon->x_pos = dock->x_pos + x*ICON_SIZE;
2178 icon->y_pos = dock->y_pos + y*ICON_SIZE;
2182 Bool
2183 moveIconBetweenDocks(WDock *src, WDock *dest, WAppIcon *icon, int x, int y)
2185 WWindow *wwin;
2186 char **argv;
2187 int argc;
2188 int index;
2190 if (src == dest)
2191 return True; /* No move needed, we're already there */
2193 if (dest == NULL)
2194 return False;
2196 wwin = icon->icon->owner;
2199 * For the moment we can't do this if we move icons in Clip from one
2200 * workspace to other, because if we move two or more icons without
2201 * command, the dialog box will not be able to tell us to which of the
2202 * moved icons it applies. -Dan
2204 if ((dest->type==WM_DOCK /*|| dest->keep_attracted*/) && icon->command==NULL) {
2205 if (XGetCommand(dpy, wwin->client_win, &argv, &argc) && argc>0) {
2207 icon->command = wtokenjoin(argv, argc);
2208 XFreeStringList(argv);
2209 } else {
2210 char *command=NULL;
2212 icon->editing = 1;
2213 /* icon->forced_dock = 1;*/
2214 if (wInputDialog(src->screen_ptr, _("Dock Icon"),
2215 _("Type the command used to launch the application"),
2216 &command)) {
2217 if (command && (command[0]==0 ||
2218 (command[0]=='-' && command[1]==0))) {
2219 wfree(command);
2220 command = NULL;
2222 icon->command = command;
2223 } else {
2224 icon->editing = 0;
2225 if (command)
2226 wfree(command);
2227 return False;
2229 icon->editing = 0;
2233 if (dest->type == WM_DOCK)
2234 wClipMakeIconOmnipresent(icon, False);
2236 for(index=1; index<src->max_icons; index++) {
2237 if(src->icon_array[index] == icon)
2238 break;
2240 assert(index < src->max_icons);
2242 src->icon_array[index] = NULL;
2243 src->icon_count--;
2245 for(index=1; index<dest->max_icons; index++) {
2246 if(dest->icon_array[index] == NULL)
2247 break;
2249 /* if (index == dest->max_icons)
2250 return; */
2252 assert(index < dest->max_icons);
2254 dest->icon_array[index] = icon;
2255 icon->dock = dest;
2257 /* deselect the icon */
2258 if (icon->icon->selected)
2259 wIconSelect(icon->icon);
2261 if (dest->type == WM_DOCK) {
2262 icon->icon->core->descriptor.handle_enternotify = NULL;
2263 icon->icon->core->descriptor.handle_leavenotify = NULL;
2264 } else {
2265 icon->icon->core->descriptor.handle_enternotify = clipEnterNotify;
2266 icon->icon->core->descriptor.handle_leavenotify = clipLeaveNotify;
2269 /* set it to be kept when moving to dock.
2270 * Unless the icon does not have a command set
2272 if (icon->command && dest->type==WM_DOCK) {
2273 icon->attracted = 0;
2274 if (icon->icon->shadowed) {
2275 icon->icon->shadowed = 0;
2276 icon->icon->force_paint = 1;
2280 if (src->auto_collapse || src->auto_raise_lower)
2281 clipLeave(src);
2283 icon->yindex = y;
2284 icon->xindex = x;
2286 icon->x_pos = dest->x_pos + x*ICON_SIZE;
2287 icon->y_pos = dest->y_pos + y*ICON_SIZE;
2289 dest->icon_count++;
2291 MoveInStackListUnder(dest->icon_array[index-1]->icon->core,
2292 icon->icon->core);
2293 wAppIconPaint(icon);
2295 return True;
2299 void
2300 wDockDetach(WDock *dock, WAppIcon *icon)
2302 int index;
2304 /* make the settings panel be closed */
2305 if (icon->panel) {
2306 DestroyDockAppSettingsPanel(icon->panel);
2309 /* This must be called before icon->dock is set to NULL.
2310 * Don't move it. -Dan
2312 wClipMakeIconOmnipresent(icon, False);
2314 icon->docked = 0;
2315 icon->dock = NULL;
2316 icon->attracted = 0;
2317 icon->auto_launch = 0;
2318 if (icon->icon->shadowed) {
2319 icon->icon->shadowed = 0;
2320 icon->icon->force_paint = 1;
2323 /* deselect the icon */
2324 if (icon->icon->selected)
2325 wIconSelect(icon->icon);
2327 if (icon->command) {
2328 wfree(icon->command);
2329 icon->command = NULL;
2331 #ifdef OFFIX_DND
2332 if (icon->dnd_command) {
2333 wfree(icon->dnd_command);
2334 icon->dnd_command = NULL;
2336 #endif
2338 for (index=1; index<dock->max_icons; index++)
2339 if (dock->icon_array[index] == icon)
2340 break;
2341 assert(index < dock->max_icons);
2342 dock->icon_array[index] = NULL;
2343 icon->yindex = -1;
2344 icon->xindex = -1;
2346 dock->icon_count--;
2348 /* if the dock is not attached to an application or
2349 * the the application did not set the approriate hints yet,
2350 * destroy the icon */
2351 if (!icon->running || !wApplicationOf(icon->main_window))
2352 wAppIconDestroy(icon);
2353 else {
2354 icon->icon->core->descriptor.handle_mousedown = appIconMouseDown;
2355 icon->icon->core->descriptor.handle_enternotify = NULL;
2356 icon->icon->core->descriptor.handle_leavenotify = NULL;
2357 icon->icon->core->descriptor.parent_type = WCLASS_APPICON;
2358 icon->icon->core->descriptor.parent = icon;
2360 ChangeStackingLevel(icon->icon->core, NORMAL_ICON_LEVEL);
2362 wAppIconPaint(icon);
2363 if (wPreferences.auto_arrange_icons) {
2364 wArrangeIcons(dock->screen_ptr, True);
2365 } else {
2366 WAppIcon *bla = wAppIconNextSibling(icon);
2368 if (bla) {
2369 SlideWindow(icon->icon->core->window, icon->x_pos, icon->y_pos,
2370 bla->x_pos, bla->y_pos);
2371 wAppIconMove(icon, bla->x_pos, bla->y_pos);
2375 if (dock->auto_collapse || dock->auto_raise_lower)
2376 clipLeave(dock);
2381 * returns the closest Dock slot index for the passed
2382 * coordinates.
2384 * Returns False if icon can't be docked.
2386 * Note: this function should NEVER alter ret_x or ret_y, unless it will
2387 * return True. -Dan
2389 Bool
2390 wDockSnapIcon(WDock *dock, WAppIcon *icon, int req_x, int req_y,
2391 int *ret_x, int *ret_y, int redocking)
2393 WScreen *scr = dock->screen_ptr;
2394 int dx, dy;
2395 int ex_x, ex_y;
2396 int i, offset = ICON_SIZE/2;
2397 WAppIcon *aicon = NULL;
2398 WAppIcon *nicon = NULL;
2399 int max_y_icons, max_x_icons;
2401 max_x_icons = scr->scr_width/ICON_SIZE;
2402 max_y_icons = scr->scr_height/ICON_SIZE-1;
2404 if (wPreferences.flags.noupdates)
2405 return False;
2407 dx = dock->x_pos;
2408 dy = dock->y_pos;
2410 /* if the dock is full */
2411 if (!redocking &&
2412 (dock->icon_count >= dock->max_icons)) {
2413 return False;
2416 /* exact position */
2417 if (req_y < dy)
2418 ex_y = (req_y - offset - dy)/ICON_SIZE;
2419 else
2420 ex_y = (req_y + offset - dy)/ICON_SIZE;
2422 if (req_x < dx)
2423 ex_x = (req_x - offset - dx)/ICON_SIZE;
2424 else
2425 ex_x = (req_x + offset - dx)/ICON_SIZE;
2427 /* check if the icon is outside the screen boundaries */
2428 if (dx + ex_x*ICON_SIZE < -ICON_SIZE+2 ||
2429 dx + ex_x*ICON_SIZE >= scr->scr_width-1 ||
2430 dy + ex_y*ICON_SIZE < -ICON_SIZE+2 ||
2431 dy + ex_y*ICON_SIZE >= scr->scr_height-1)
2432 return False;
2434 if (dock->type == WM_DOCK) {
2435 if (icon->dock != dock && ex_x != 0)
2436 return False;
2438 aicon = NULL;
2439 for (i=0; i<dock->max_icons; i++) {
2440 nicon = dock->icon_array[i];
2441 if (nicon && nicon->yindex == ex_y) {
2442 aicon = nicon;
2443 break;
2447 if (redocking) {
2448 int sig, done, closest;
2450 /* Possible cases when redocking:
2452 * icon dragged out of range of any slot -> false
2453 * icon dragged to range of free slot
2454 * icon dragged to range of same slot
2455 * icon dragged to range of different icon
2457 if (abs(ex_x) > DOCK_DETTACH_THRESHOLD)
2458 return False;
2460 if (ex_y>=0 && ex_y<=max_y_icons && (aicon==icon || !aicon)) {
2461 *ret_x = 0;
2462 *ret_y = ex_y;
2463 return True;
2466 /* start looking at the upper slot or lower? */
2467 if (ex_y*ICON_SIZE < (req_y + offset - dy))
2468 sig = 1;
2469 else
2470 sig = -1;
2472 closest = -1;
2473 done = 0;
2474 /* look for closest free slot */
2475 for (i=0; i<(DOCK_DETTACH_THRESHOLD+1)*2 && !done; i++) {
2476 int j;
2478 done = 1;
2479 closest = sig*(i/2) + ex_y;
2480 /* check if this slot is used */
2481 if (closest >= 0) {
2482 for (j = 0; j<dock->max_icons; j++) {
2483 if (dock->icon_array[j]
2484 && dock->icon_array[j]->yindex==closest) {
2485 /* slot is used by someone else */
2486 if (dock->icon_array[j]!=icon)
2487 done = 0;
2488 break;
2492 sig = -sig;
2494 if (done && closest >= 0 && closest <= max_y_icons &&
2495 ((ex_y >= closest && ex_y - closest < DOCK_DETTACH_THRESHOLD+1)
2497 (ex_y < closest && closest - ex_y <= DOCK_DETTACH_THRESHOLD+1))) {
2498 *ret_x = 0;
2499 *ret_y = closest;
2500 return True;
2502 } else { /* !redocking */
2504 /* if slot is free and the icon is close enough, return it */
2505 if (!aicon && ex_x == 0 && ex_y >= 0 && ex_y <= max_y_icons) {
2506 *ret_x = 0;
2507 *ret_y = ex_y;
2508 return True;
2511 } else { /* CLIP */
2512 int neighbours = 0;
2513 int start, stop, k;
2515 start = icon->omnipresent ? 0 : scr->current_workspace;
2516 stop = icon->omnipresent ? scr->workspace_count : start+1;
2518 aicon = NULL;
2519 for (k=start; k<stop; k++) {
2520 WDock *tmp = scr->workspaces[k]->clip;
2521 if (!tmp)
2522 continue;
2523 for (i=0; i<tmp->max_icons; i++) {
2524 nicon = tmp->icon_array[i];
2525 if (nicon && nicon->xindex == ex_x && nicon->yindex == ex_y) {
2526 aicon = nicon;
2527 break;
2530 if (aicon)
2531 break;
2533 for (k=start; k<stop; k++) {
2534 WDock *tmp = scr->workspaces[k]->clip;
2535 if (!tmp)
2536 continue;
2537 for (i=0; i<tmp->max_icons; i++) {
2538 nicon = tmp->icon_array[i];
2539 if (nicon && nicon != icon && /* Icon can't be it's own neighbour */
2540 (abs(nicon->xindex - ex_x) <= CLIP_ATTACH_VICINITY &&
2541 abs(nicon->yindex - ex_y) <= CLIP_ATTACH_VICINITY)) {
2542 neighbours = 1;
2543 break;
2546 if (neighbours)
2547 break;
2550 if (neighbours && (aicon==NULL || (redocking && aicon == icon))) {
2551 *ret_x = ex_x;
2552 *ret_y = ex_y;
2553 return True;
2556 return False;
2559 #define MIN(x, y) ((x) > (y) ? (y) : (x))
2560 #define MAX(x, y) ((x) < (y) ? (y) : (x))
2562 #define ON_SCREEN(x, y, sx, ex, sy, ey) \
2563 ((((x)+ICON_SIZE/2) >= (sx)) && (((y)+ICON_SIZE/2) >= (sy)) && \
2564 (((x) + (ICON_SIZE/2)) <= (ex)) && (((y) + (ICON_SIZE/2)) <= ey))
2568 * returns true if it can find a free slot in the dock,
2569 * in which case it changes x_pos and y_pos accordingly.
2570 * Else returns false.
2572 Bool
2573 wDockFindFreeSlot(WDock *dock, int *x_pos, int *y_pos)
2575 WScreen *scr = dock->screen_ptr;
2576 WAppIcon *btn;
2577 WAppIconChain *chain;
2578 unsigned char *slot_map;
2579 int mwidth;
2580 int r;
2581 int x, y;
2582 int i, done = False;
2583 int corner;
2584 int sx=0, sy=0, ex=scr->scr_width, ey=scr->scr_height;
2585 int extra_count=0;
2587 if (dock->type == WM_CLIP &&
2588 dock != scr->workspaces[scr->current_workspace]->clip)
2589 extra_count = scr->global_icon_count;
2591 /* if the dock is full */
2592 if (dock->icon_count+extra_count >= dock->max_icons) {
2593 return False;
2596 if (!wPreferences.flags.nodock && scr->dock) {
2597 if (scr->dock->on_right_side)
2598 ex -= ICON_SIZE + DOCK_EXTRA_SPACE;
2599 else
2600 sx += ICON_SIZE + DOCK_EXTRA_SPACE;
2603 if (ex < dock->x_pos)
2604 ex = dock->x_pos;
2605 if (sx > dock->x_pos+ICON_SIZE)
2606 sx = dock->x_pos+ICON_SIZE;
2607 #define C_NONE 0
2608 #define C_NW 1
2609 #define C_NE 2
2610 #define C_SW 3
2611 #define C_SE 4
2613 /* check if clip is in a corner */
2614 if (dock->type==WM_CLIP) {
2615 if (dock->x_pos < 1 && dock->y_pos < 1)
2616 corner = C_NE;
2617 else if (dock->x_pos < 1 && dock->y_pos >= (ey-ICON_SIZE))
2618 corner = C_SE;
2619 else if (dock->x_pos >= (ex-ICON_SIZE)&& dock->y_pos >= (ey-ICON_SIZE))
2620 corner = C_SW;
2621 else if (dock->x_pos >= (ex-ICON_SIZE) && dock->y_pos < 1)
2622 corner = C_NW;
2623 else
2624 corner = C_NONE;
2625 } else
2626 corner = C_NONE;
2628 /* If the clip is in the corner, use only slots that are in the border
2629 * of the screen */
2630 if (corner!=C_NONE) {
2631 char *hmap, *vmap;
2632 int hcount, vcount;
2634 hcount = MIN(dock->max_icons, scr->scr_width/ICON_SIZE);
2635 vcount = MIN(dock->max_icons, scr->scr_height/ICON_SIZE);
2636 hmap = wmalloc(hcount+1);
2637 memset(hmap, 0, hcount+1);
2638 vmap = wmalloc(vcount+1);
2639 memset(vmap, 0, vcount+1);
2641 /* mark used positions */
2642 switch (corner) {
2643 case C_NE:
2644 for (i=0; i<dock->max_icons; i++) {
2645 btn = dock->icon_array[i];
2646 if (!btn)
2647 continue;
2649 if (btn->xindex==0 && btn->yindex > 0 && btn->yindex < vcount)
2650 vmap[btn->yindex] = 1;
2651 else if (btn->yindex==0 && btn->xindex>0 && btn->xindex<hcount)
2652 hmap[btn->xindex] = 1;
2654 for (chain=scr->global_icons; chain!=NULL; chain=chain->next) {
2655 btn = chain->aicon;
2656 if (btn->xindex==0 && btn->yindex > 0 && btn->yindex < vcount)
2657 vmap[btn->yindex] = 1;
2658 else if (btn->yindex==0 && btn->xindex>0 && btn->xindex<hcount)
2659 hmap[btn->xindex] = 1;
2661 break;
2662 case C_NW:
2663 for (i=0; i<dock->max_icons; i++) {
2664 btn = dock->icon_array[i];
2665 if (!btn)
2666 continue;
2668 if (btn->xindex==0 && btn->yindex > 0 && btn->yindex < vcount)
2669 vmap[btn->yindex] = 1;
2670 else if (btn->yindex==0 && btn->xindex<0 &&btn->xindex>-hcount)
2671 hmap[-btn->xindex] = 1;
2673 for (chain=scr->global_icons; chain!=NULL; chain=chain->next) {
2674 btn = chain->aicon;
2675 if (btn->xindex==0 && btn->yindex > 0 && btn->yindex < vcount)
2676 vmap[btn->yindex] = 1;
2677 else if (btn->yindex==0 && btn->xindex<0 &&btn->xindex>-hcount)
2678 hmap[-btn->xindex] = 1;
2680 break;
2681 case C_SE:
2682 for (i=0; i<dock->max_icons; i++) {
2683 btn = dock->icon_array[i];
2684 if (!btn)
2685 continue;
2687 if (btn->xindex==0 && btn->yindex < 0 && btn->yindex > -vcount)
2688 vmap[-btn->yindex] = 1;
2689 else if (btn->yindex==0 && btn->xindex>0 && btn->xindex<hcount)
2690 hmap[btn->xindex] = 1;
2692 for (chain=scr->global_icons; chain!=NULL; chain=chain->next) {
2693 btn = chain->aicon;
2694 if (btn->xindex==0 && btn->yindex < 0 && btn->yindex > -vcount)
2695 vmap[-btn->yindex] = 1;
2696 else if (btn->yindex==0 && btn->xindex>0 && btn->xindex<hcount)
2697 hmap[btn->xindex] = 1;
2699 break;
2700 case C_SW:
2701 default:
2702 for (i=0; i<dock->max_icons; i++) {
2703 btn = dock->icon_array[i];
2704 if (!btn)
2705 continue;
2707 if (btn->xindex==0 && btn->yindex < 0 && btn->yindex > -vcount)
2708 vmap[-btn->yindex] = 1;
2709 else if (btn->yindex==0 && btn->xindex<0 &&btn->xindex>-hcount)
2710 hmap[-btn->xindex] = 1;
2712 for (chain=scr->global_icons; chain!=NULL; chain=chain->next) {
2713 btn = chain->aicon;
2714 if (btn->xindex==0 && btn->yindex < 0 && btn->yindex > -vcount)
2715 vmap[-btn->yindex] = 1;
2716 else if (btn->yindex==0 && btn->xindex<0 &&btn->xindex>-hcount)
2717 hmap[-btn->xindex] = 1;
2720 x=0; y=0;
2721 done = 0;
2722 /* search a vacant slot */
2723 for (i=1; i<MAX(vcount, hcount); i++) {
2724 if (i < vcount && vmap[i]==0) {
2725 /* found a slot */
2726 x = 0;
2727 y = i;
2728 done = 1;
2729 break;
2730 } else if (i < hcount && hmap[i]==0) {
2731 /* found a slot */
2732 x = i;
2733 y = 0;
2734 done = 1;
2735 break;
2738 wfree(vmap);
2739 wfree(hmap);
2740 /* If found a slot, translate and return */
2741 if (done) {
2742 if (corner==C_NW || corner==C_NE) {
2743 *y_pos = y;
2744 } else {
2745 *y_pos = -y;
2747 if (corner==C_NE || corner==C_SE) {
2748 *x_pos = x;
2749 } else {
2750 *x_pos = -x;
2752 return True;
2754 /* else, try to find a slot somewhere else */
2757 /* a map of mwidth x mwidth would be enough if we allowed icons to be
2758 * placed outside of screen */
2759 mwidth = (int)ceil(sqrt(dock->max_icons));
2761 /* In the worst case (the clip is in the corner of the screen),
2762 * the amount of icons that fit in the clip is smaller.
2763 * Double the map to get a safe value.
2765 mwidth += mwidth;
2767 r = (mwidth-1)/2;
2769 slot_map = wmalloc(mwidth*mwidth);
2770 memset(slot_map, 0, mwidth*mwidth);
2772 #define XY2OFS(x,y) (MAX(abs(x),abs(y)) > r) ? 0 : (((y)+r)*(mwidth)+(x)+r)
2774 /* mark used slots in the map. If the slot falls outside the map
2775 * (for example, when all icons are placed in line), ignore them. */
2776 for (i=0; i<dock->max_icons; i++) {
2777 btn = dock->icon_array[i];
2778 if (btn)
2779 slot_map[XY2OFS(btn->xindex, btn->yindex)] = 1;
2781 for (chain=scr->global_icons; chain!=NULL; chain=chain->next) {
2782 slot_map[XY2OFS(chain->aicon->xindex, chain->aicon->yindex)] = 1;
2784 /* Find closest slot from the center that is free by scanning the
2785 * map from the center to outward in circular passes.
2786 * This will not result in a neat layout, but will be optimal
2787 * in the sense that there will not be holes left.
2789 done = 0;
2790 for (i = 1; i <= r && !done; i++) {
2791 int tx, ty;
2793 /* top and bottom parts of the ring */
2794 for (x = -i; x <= i && !done; x++) {
2795 tx = dock->x_pos + x*ICON_SIZE;
2796 y = -i;
2797 ty = dock->y_pos + y*ICON_SIZE;
2798 if (slot_map[XY2OFS(x,y)]==0
2799 && ON_SCREEN(tx, ty, sx, ex, sy, ey)) {
2800 *x_pos = x;
2801 *y_pos = y;
2802 done = 1;
2803 break;
2805 y = i;
2806 ty = dock->y_pos + y*ICON_SIZE;
2807 if (slot_map[XY2OFS(x,y)]==0
2808 && ON_SCREEN(tx, ty, sx, ex, sy, ey)) {
2809 *x_pos = x;
2810 *y_pos = y;
2811 done = 1;
2812 break;
2815 /* left and right parts of the ring */
2816 for (y = -i+1; y <= i-1; y++) {
2817 ty = dock->y_pos + y*ICON_SIZE;
2818 x = -i;
2819 tx = dock->x_pos + x*ICON_SIZE;
2820 if (slot_map[XY2OFS(x,y)]==0
2821 && ON_SCREEN(tx, ty, sx, ex, sy, ey)) {
2822 *x_pos = x;
2823 *y_pos = y;
2824 done = 1;
2825 break;
2827 x = i;
2828 tx = dock->x_pos + x*ICON_SIZE;
2829 if (slot_map[XY2OFS(x,y)]==0
2830 && ON_SCREEN(tx, ty, sx, ex, sy, ey)) {
2831 *x_pos = x;
2832 *y_pos = y;
2833 done = 1;
2834 break;
2838 wfree(slot_map);
2839 #undef XY2OFS
2840 return done;
2844 static void
2845 moveDock(WDock *dock, int new_x, int new_y)
2847 WAppIcon *btn;
2848 int i;
2850 dock->x_pos = new_x;
2851 dock->y_pos = new_y;
2852 for (i=0; i<dock->max_icons; i++) {
2853 btn = dock->icon_array[i];
2854 if (btn) {
2855 btn->x_pos = new_x + btn->xindex*ICON_SIZE;
2856 btn->y_pos = new_y + btn->yindex*ICON_SIZE;
2857 XMoveWindow(dpy, btn->icon->core->window, btn->x_pos, btn->y_pos);
2863 static void
2864 swapDock(WDock *dock)
2866 WScreen *scr = dock->screen_ptr;
2867 WAppIcon *btn;
2868 int x, i;
2871 if (dock->on_right_side) {
2872 x = dock->x_pos = scr->scr_width - ICON_SIZE - DOCK_EXTRA_SPACE;
2873 } else {
2874 x = dock->x_pos = DOCK_EXTRA_SPACE;
2877 for (i=0; i<dock->max_icons; i++) {
2878 btn = dock->icon_array[i];
2879 if (btn) {
2880 btn->x_pos = x;
2881 XMoveWindow(dpy, btn->icon->core->window, btn->x_pos, btn->y_pos);
2885 wScreenUpdateUsableArea(scr);
2889 static pid_t
2890 execCommand(WAppIcon *btn, char *command, WSavedState *state)
2892 WScreen *scr = btn->icon->core->screen_ptr;
2893 pid_t pid;
2894 char **argv;
2895 int argc;
2896 char *cmdline;
2898 cmdline = ExpandOptions(scr, command);
2900 if (scr->flags.dnd_data_convertion_status || !cmdline) {
2901 if (cmdline)
2902 wfree(cmdline);
2903 if (state)
2904 wfree(state);
2905 return 0;
2908 wtokensplit(cmdline, &argv, &argc);
2910 if (argv==NULL) {
2911 if (cmdline)
2912 wfree(cmdline);
2913 if (state)
2914 wfree(state);
2915 return 0;
2918 if ((pid=fork())==0) {
2919 char **args;
2920 int i;
2922 SetupEnvironment(scr);
2924 #ifdef HAVE_SETPGID
2925 setpgid(0, 0);
2926 #endif
2928 args = malloc(sizeof(char*)*(argc+1));
2929 if (!args)
2930 exit(111);
2931 for (i=0; i<argc; i++) {
2932 args[i] = argv[i];
2934 args[argc] = NULL;
2935 execvp(argv[0], args);
2936 exit(111);
2938 while (argc > 0)
2939 wfree(argv[--argc]);
2940 wfree(argv);
2942 if (pid > 0) {
2943 if (!state) {
2944 state = wmalloc(sizeof(WSavedState));
2945 memset(state, 0, sizeof(WSavedState));
2946 state->hidden = -1;
2947 state->miniaturized = -1;
2948 state->shaded = -1;
2949 if (btn->dock==scr->dock || btn->omnipresent)
2950 state->workspace = -1;
2951 else
2952 state->workspace = scr->current_workspace;
2954 wWindowAddSavedState(btn->wm_instance, btn->wm_class, cmdline, pid,
2955 state);
2956 wAddDeathHandler(pid, (WDeathHandler*)trackDeadProcess,
2957 btn->dock);
2958 } else if (state) {
2959 wfree(state);
2961 wfree(cmdline);
2962 return pid;
2966 void
2967 wDockHideIcons(WDock *dock)
2969 int i;
2971 if (dock==NULL)
2972 return;
2974 for (i=1; i<dock->max_icons; i++) {
2975 if (dock->icon_array[i])
2976 XUnmapWindow(dpy, dock->icon_array[i]->icon->core->window);
2978 dock->mapped = 0;
2980 dockIconPaint(dock->icon_array[0]);
2984 void
2985 wDockShowIcons(WDock *dock)
2987 int i, newlevel;
2988 WAppIcon *btn;
2990 if (dock==NULL)
2991 return;
2993 btn = dock->icon_array[0];
2994 moveDock(dock, btn->x_pos, btn->y_pos);
2996 newlevel = dock->lowered ? WMNormalLevel : WMDockLevel;
2997 ChangeStackingLevel(btn->icon->core, newlevel);
2999 for (i=1; i<dock->max_icons; i++) {
3000 if (dock->icon_array[i]) {
3001 MoveInStackListAbove(dock->icon_array[i]->icon->core,
3002 btn->icon->core);
3003 break;
3007 if (!dock->collapsed) {
3008 for (i=1; i<dock->max_icons; i++) {
3009 if (dock->icon_array[i]) {
3010 XMapWindow(dpy, dock->icon_array[i]->icon->core->window);
3014 dock->mapped = 1;
3016 dockIconPaint(btn);
3020 void
3021 wDockLower(WDock *dock)
3023 int i;
3025 for (i=0; i<dock->max_icons; i++) {
3026 if (dock->icon_array[i])
3027 wLowerFrame(dock->icon_array[i]->icon->core);
3032 void
3033 wDockRaise(WDock *dock)
3035 int i;
3037 for (i=dock->max_icons-1; i>=0; i--) {
3038 if (dock->icon_array[i])
3039 wRaiseFrame(dock->icon_array[i]->icon->core);
3044 void
3045 wDockRaiseLower(WDock *dock)
3047 if (!dock->icon_array[0]->icon->core->stacking->above
3048 ||(dock->icon_array[0]->icon->core->stacking->window_level
3049 !=dock->icon_array[0]->icon->core->stacking->above->stacking->window_level))
3050 wDockLower(dock);
3051 else
3052 wDockRaise(dock);
3056 void
3057 wDockFinishLaunch(WDock *dock, WAppIcon *icon)
3059 icon->launching = 0;
3060 icon->relaunching = 0;
3061 dockIconPaint(icon);
3065 WAppIcon*
3066 wDockFindIconForWindow(WDock *dock, Window window)
3068 WAppIcon *icon;
3069 int i;
3071 for (i=0; i<dock->max_icons; i++) {
3072 icon = dock->icon_array[i];
3073 if (icon && icon->main_window == window)
3074 return icon;
3076 return NULL;
3080 void
3081 wDockTrackWindowLaunch(WDock *dock, Window window)
3083 WAppIcon *icon;
3084 char *wm_class, *wm_instance;
3085 int i;
3086 Bool firstPass = True;
3087 Bool found = False;
3088 char *command = NULL;
3091 int argc;
3092 char **argv;
3094 if (XGetCommand(dpy, window, &argv, &argc)) {
3095 if (argc > 0 && argv != NULL)
3096 command = wtokenjoin(argv,argc);
3097 if (argv) {
3098 XFreeStringList(argv);
3103 if (!PropGetWMClass(window, &wm_class, &wm_instance) ||
3104 (!wm_class && !wm_instance))
3105 return;
3107 retry:
3108 for (i=0; i<dock->max_icons; i++) {
3109 icon = dock->icon_array[i];
3110 if (!icon)
3111 continue;
3113 /* app is already attached to icon */
3114 if (icon->main_window == window) {
3115 found = True;
3116 break;
3119 if ((icon->wm_instance || icon->wm_class)
3120 && (icon->launching
3121 || (dock->screen_ptr->flags.startup && !icon->running))) {
3123 if (icon->wm_instance && wm_instance &&
3124 strcmp(icon->wm_instance, wm_instance)!=0) {
3125 continue;
3127 if (icon->wm_class && wm_class &&
3128 strcmp(icon->wm_class, wm_class)!=0) {
3129 continue;
3131 if (firstPass && command && strcmp(icon->command, command)!=0) {
3132 continue;
3135 if (!icon->relaunching) {
3136 WApplication *wapp;
3138 /* Possibly an application that was docked with dockit,
3139 * but the user did not update WMState to indicate that
3140 * it was docked by force */
3141 wapp = wApplicationOf(window);
3142 if (!wapp) {
3143 icon->forced_dock = 1;
3144 icon->running = 0;
3146 if (!icon->forced_dock)
3147 icon->main_window = window;
3150 found = True;
3151 wDockFinishLaunch(dock, icon);
3152 break;
3156 if (firstPass && !found) {
3157 firstPass = False;
3158 goto retry;
3161 if (command)
3162 wfree(command);
3164 if (wm_class)
3165 XFree(wm_class);
3166 if (wm_instance)
3167 XFree(wm_instance);
3172 void
3173 wClipUpdateForWorkspaceChange(WScreen *scr, int workspace)
3175 if (!wPreferences.flags.noclip) {
3176 scr->clip_icon->dock = scr->workspaces[workspace]->clip;
3177 if (scr->current_workspace != workspace) {
3178 WDock *old_clip = scr->workspaces[scr->current_workspace]->clip;
3179 WAppIconChain *chain = scr->global_icons;
3181 while (chain) {
3182 moveIconBetweenDocks(chain->aicon->dock,
3183 scr->workspaces[workspace]->clip,
3184 chain->aicon, chain->aicon->xindex,
3185 chain->aicon->yindex);
3186 if (scr->workspaces[workspace]->clip->collapsed)
3187 XUnmapWindow(dpy, chain->aicon->icon->core->window);
3188 chain = chain->next;
3191 wDockHideIcons(old_clip);
3192 if (old_clip->auto_raise_lower) {
3193 if (old_clip->auto_raise_magic) {
3194 WMDeleteTimerHandler(old_clip->auto_raise_magic);
3195 old_clip->auto_raise_magic = NULL;
3197 wDockLower(old_clip);
3199 if (old_clip->auto_collapse) {
3200 if (old_clip->auto_expand_magic) {
3201 WMDeleteTimerHandler(old_clip->auto_expand_magic);
3202 old_clip->auto_expand_magic = NULL;
3204 old_clip->collapsed = 1;
3206 wDockShowIcons(scr->workspaces[workspace]->clip);
3208 if (scr->flags.clip_balloon_mapped)
3209 showClipBalloon(scr->clip_icon->dock, workspace);
3215 static void
3216 trackDeadProcess(pid_t pid, unsigned char status, WDock *dock)
3218 WAppIcon *icon;
3219 int i;
3221 for (i=0; i<dock->max_icons; i++) {
3222 icon = dock->icon_array[i];
3223 if (!icon)
3224 continue;
3226 if (icon->launching && icon->pid == pid) {
3227 if (!icon->relaunching) {
3228 icon->running = 0;
3229 icon->main_window = None;
3231 wDockFinishLaunch(dock, icon);
3232 icon->pid = 0;
3233 if (status==111) {
3234 char msg[PATH_MAX];
3235 #ifdef OFFIX_DND
3236 sprintf(msg, _("Could not execute command \"%s\""),
3237 icon->drop_launch && icon->dnd_command
3238 ? icon->dnd_command : icon->command);
3239 #else
3240 sprintf(msg, _("Could not execute command \"%s\""),
3241 icon->command);
3242 #endif
3243 wMessageDialog(dock->screen_ptr, _("Error"), msg,
3244 _("OK"), NULL, NULL);
3246 break;
3252 static void
3253 toggleLowered(WDock *dock)
3255 WAppIcon *tmp;
3256 int newlevel, i;
3258 /* lower/raise Dock */
3259 if (!dock->lowered) {
3260 newlevel = WMNormalLevel;
3261 dock->lowered = 1;
3262 } else {
3263 newlevel = WMDockLevel;
3264 dock->lowered = 0;
3267 for (i=0; i<dock->max_icons; i++) {
3268 tmp = dock->icon_array[i];
3269 if (!tmp)
3270 continue;
3272 ChangeStackingLevel(tmp->icon->core, newlevel);
3273 if (dock->lowered)
3274 wLowerFrame(tmp->icon->core);
3277 if (dock->type == WM_DOCK)
3278 wScreenUpdateUsableArea(dock->screen_ptr);
3282 static void
3283 toggleCollapsed(WDock *dock)
3285 if (dock->collapsed) {
3286 dock->collapsed = 0;
3287 wDockShowIcons(dock);
3289 else {
3290 dock->collapsed = 1;
3291 wDockHideIcons(dock);
3296 static void
3297 openDockMenu(WDock *dock, WAppIcon *aicon, XEvent *event)
3299 WScreen *scr = dock->screen_ptr;
3300 WObjDescriptor *desc;
3301 WMenuEntry *entry;
3302 WApplication *wapp = NULL;
3303 int index = 0;
3304 int x_pos;
3305 int n_selected;
3306 int appIsRunning = aicon->running && aicon->icon && aicon->icon->owner;
3308 if (dock->type == WM_DOCK) {
3309 /* keep on top */
3310 entry = dock->menu->entries[index];
3312 if (dock->lowered) {
3313 entry->text = _("Keep on Top");
3314 } else {
3315 entry->text = _("Allow Lowering");
3317 entry->clientdata = dock;
3319 dock->menu->flags.realized = 0;
3320 } else {
3321 /* clip options */
3322 if (scr->clip_options)
3323 updateClipOptionsMenu(scr->clip_options, dock);
3325 n_selected = numberOfSelectedIcons(dock);
3327 /* Rename Workspace */
3328 entry = dock->menu->entries[++index];
3329 if (aicon == scr->clip_icon) {
3330 entry->callback = renameCallback;
3331 entry->clientdata = dock;
3332 entry->flags.indicator = 0;
3333 entry->text = _("Rename Workspace");
3334 } else {
3335 entry->callback = omnipresentCallback;
3336 entry->clientdata = aicon;
3337 if (n_selected > 0) {
3338 entry->flags.indicator = 0;
3339 entry->text = _("Toggle Omnipresent");
3340 } else {
3341 entry->flags.indicator = 1;
3342 entry->flags.indicator_on = aicon->omnipresent;
3343 entry->flags.indicator_type = MI_CHECK;
3344 entry->text = _("Omnipresent");
3348 /* select/unselect icon */
3349 entry = dock->menu->entries[++index];
3350 entry->clientdata = aicon;
3351 entry->flags.indicator_on = aicon->icon->selected;
3352 wMenuSetEnabled(dock->menu, index, aicon!=scr->clip_icon);
3354 /* select/unselect all icons */
3355 entry = dock->menu->entries[++index];
3356 entry->clientdata = aicon;
3357 if (n_selected > 0)
3358 entry->text = _("Unselect All Icons");
3359 else
3360 entry->text = _("Select All Icons");
3361 wMenuSetEnabled(dock->menu, index, dock->icon_count > 1);
3363 /* keep icon(s) */
3364 entry = dock->menu->entries[++index];
3365 entry->clientdata = aicon;
3366 if (n_selected > 1)
3367 entry->text = _("Keep Icons");
3368 else
3369 entry->text = _("Keep Icon");
3370 wMenuSetEnabled(dock->menu, index, dock->icon_count > 1);
3372 /* this is the workspace submenu part */
3373 entry = dock->menu->entries[++index];
3374 if (n_selected > 1)
3375 entry->text = _("Move Icons To");
3376 else
3377 entry->text = _("Move Icon To");
3378 if (scr->clip_submenu)
3379 updateWorkspaceMenu(scr->clip_submenu, aicon);
3380 wMenuSetEnabled(dock->menu, index, !aicon->omnipresent);
3382 /* remove icon(s) */
3383 entry = dock->menu->entries[++index];
3384 entry->clientdata = aicon;
3385 if (n_selected > 1)
3386 entry->text = _("Remove Icons");
3387 else
3388 entry->text = _("Remove Icon");
3389 wMenuSetEnabled(dock->menu, index, dock->icon_count > 1);
3391 /* attract icon(s) */
3392 entry = dock->menu->entries[++index];
3393 entry->clientdata = aicon;
3395 dock->menu->flags.realized = 0;
3396 wMenuRealize(dock->menu);
3400 if (aicon->icon->owner) {
3401 wapp = wApplicationOf(aicon->icon->owner->main_window);
3402 } else {
3403 wapp = NULL;
3406 /* launch */
3407 entry = dock->menu->entries[++index];
3408 entry->clientdata = aicon;
3409 wMenuSetEnabled(dock->menu, index, aicon->command!=NULL);
3411 /* unhide here */
3412 entry = dock->menu->entries[++index];
3413 entry->clientdata = aicon;
3414 if (wapp && wapp->flags.hidden) {
3415 entry->text = _("Unhide Here");
3416 } else {
3417 entry->text = _("Bring Here");
3419 wMenuSetEnabled(dock->menu, index, appIsRunning);
3421 /* hide */
3422 entry = dock->menu->entries[++index];
3423 entry->clientdata = aicon;
3424 if (wapp && wapp->flags.hidden) {
3425 entry->text = _("Unhide");
3426 } else {
3427 entry->text = _("Hide");
3429 wMenuSetEnabled(dock->menu, index, appIsRunning);
3431 /* settings */
3432 entry = dock->menu->entries[++index];
3433 entry->clientdata = aicon;
3434 wMenuSetEnabled(dock->menu, index, !aicon->editing
3435 && !wPreferences.flags.noupdates);
3437 /* kill */
3438 entry = dock->menu->entries[++index];
3439 entry->clientdata = aicon;
3440 wMenuSetEnabled(dock->menu, index, appIsRunning);
3442 if (!dock->menu->flags.realized)
3443 wMenuRealize(dock->menu);
3445 if (dock->type == WM_CLIP) {
3446 x_pos = event->xbutton.x_root+2;
3447 } else {
3448 x_pos = dock->on_right_side ?
3449 scr->scr_width - dock->menu->frame->core->width - 2 : 0;
3452 wMenuMapAt(dock->menu, x_pos, event->xbutton.y_root+2, False);
3454 /* allow drag select */
3455 event->xany.send_event = True;
3456 desc = &dock->menu->menu->descriptor;
3457 (*desc->handle_mousedown)(desc, event);
3461 static void
3462 openClipWorkspaceMenu(WScreen *scr, int x, int y)
3464 if (!scr->clip_ws_menu) {
3465 scr->clip_ws_menu = wWorkspaceMenuMake(scr, False);
3467 wWorkspaceMenuUpdate(scr, scr->clip_ws_menu);
3468 wMenuMapAt(scr->clip_ws_menu, x, y, False);
3472 /******************************************************************/
3473 static void
3474 iconDblClick(WObjDescriptor *desc, XEvent *event)
3476 WAppIcon *btn = desc->parent;
3477 WDock *dock = btn->dock;
3478 WApplication *wapp = NULL;
3479 int unhideHere = 0;
3481 if (btn->icon->owner && !(event->xbutton.state & ControlMask)) {
3482 wapp = wApplicationOf(btn->icon->owner->main_window);
3484 assert(wapp!=NULL);
3486 unhideHere = (event->xbutton.state & ShiftMask);
3488 /* go to the last workspace that the user worked on the app */
3489 if (wapp->last_workspace != dock->screen_ptr->current_workspace
3490 && !unhideHere) {
3491 wWorkspaceChange(dock->screen_ptr, wapp->last_workspace);
3494 wUnhideApplication(wapp, event->xbutton.button==Button2,
3495 unhideHere);
3497 if (event->xbutton.state & MOD_MASK) {
3498 wHideOtherApplications(btn->icon->owner);
3500 } else {
3501 if (event->xbutton.button==Button1) {
3503 if (event->xbutton.state & MOD_MASK) {
3504 /* raise/lower dock */
3505 toggleLowered(dock);
3506 } else if (btn == dock->screen_ptr->clip_icon) {
3507 if (getClipButton(event->xbutton.x, event->xbutton.y)==CLIP_IDLE)
3508 toggleCollapsed(dock);
3509 else
3510 handleClipChangeWorkspace(dock->screen_ptr, event);
3511 } else if (btn->command) {
3512 if (!btn->launching &&
3513 (!btn->running || (event->xbutton.state & ControlMask))) {
3514 launchDockedApplication(btn);
3516 } else if (btn->xindex == 0 && btn->yindex == 0
3517 && btn->dock->type == WM_DOCK) {
3519 wShowGNUstepPanel(dock->screen_ptr);
3526 static void
3527 handleDockMove(WDock *dock, WAppIcon *aicon, XEvent *event)
3529 WScreen *scr = dock->screen_ptr;
3530 int ofs_x=event->xbutton.x, ofs_y=event->xbutton.y;
3531 int x, y;
3532 XEvent ev;
3533 int grabbed = 0, swapped = 0, done;
3534 Pixmap ghost = None;
3535 int superfluous = wPreferences.superfluous; /* we catch it to avoid problems */
3537 #ifdef DEBUG
3538 puts("moving dock");
3539 #endif
3540 if (XGrabPointer(dpy, aicon->icon->core->window, True, ButtonMotionMask
3541 |ButtonReleaseMask|ButtonPressMask, GrabModeAsync,
3542 GrabModeAsync, None, None, CurrentTime) !=GrabSuccess) {
3543 wwarning("pointer grab failed for dock move");
3545 y = 0;
3546 for (x=0; x<dock->max_icons; x++) {
3547 if (dock->icon_array[x]!=NULL &&
3548 dock->icon_array[x]->yindex > y)
3549 y = dock->icon_array[x]->yindex;
3551 y++;
3552 XResizeWindow(dpy, scr->dock_shadow, ICON_SIZE, ICON_SIZE*y);
3554 done = 0;
3555 while (!done) {
3556 WMMaskEvent(dpy, PointerMotionMask|ButtonReleaseMask|ButtonPressMask
3557 |ButtonMotionMask|ExposureMask, &ev);
3558 switch (ev.type) {
3559 case Expose:
3560 WMHandleEvent(&ev);
3561 break;
3563 case MotionNotify:
3564 if (!grabbed) {
3565 if (abs(ofs_x-ev.xmotion.x)>=MOVE_THRESHOLD
3566 || abs(ofs_y-ev.xmotion.y)>=MOVE_THRESHOLD) {
3567 XChangeActivePointerGrab(dpy, ButtonMotionMask
3568 |ButtonReleaseMask|ButtonPressMask,
3569 wCursor[WCUR_MOVE], CurrentTime);
3570 grabbed=1;
3572 break;
3574 if (dock->type == WM_CLIP) {
3575 if (ev.xmotion.x_root - ofs_x < 0) {
3576 x = 0;
3577 } else if (ev.xmotion.x_root - ofs_x + ICON_SIZE >
3578 scr->scr_width) {
3579 x = scr->scr_width - ICON_SIZE;
3580 } else {
3581 x = ev.xmotion.x_root - ofs_x;
3583 if (ev.xmotion.y_root - ofs_y < 0) {
3584 y = 0;
3585 } else if (ev.xmotion.y_root - ofs_y + ICON_SIZE >
3586 scr->scr_height) {
3587 y = scr->scr_height - ICON_SIZE;
3588 } else {
3589 y = ev.xmotion.y_root - ofs_y;
3591 moveDock(dock, x, y);
3592 } else {
3593 /* move vertically if pointer is inside the dock*/
3594 if ((dock->on_right_side &&
3595 ev.xmotion.x_root >= dock->x_pos - ICON_SIZE)
3596 || (!dock->on_right_side &&
3597 ev.xmotion.x_root <= dock->x_pos + ICON_SIZE*2)) {
3599 if (ev.xmotion.y_root - ofs_y < 0) {
3600 y = 0;
3601 } else if (ev.xmotion.y_root - ofs_y + ICON_SIZE >
3602 scr->scr_height) {
3603 y = scr->scr_height - ICON_SIZE;
3604 } else {
3605 y = ev.xmotion.y_root - ofs_y;
3607 moveDock(dock, dock->x_pos, y);
3609 /* move horizontally to change sides */
3610 x = ev.xmotion.x_root - ofs_x;
3611 if (!dock->on_right_side) {
3613 /* is on left */
3615 if (ev.xmotion.x_root > dock->x_pos + ICON_SIZE*2) {
3616 XMoveWindow(dpy, scr->dock_shadow, scr->scr_width-ICON_SIZE
3617 -DOCK_EXTRA_SPACE-1, dock->y_pos);
3618 if (superfluous && ghost==None) {
3619 ghost = MakeGhostDock(dock, dock->x_pos,
3620 scr->scr_width-ICON_SIZE
3621 -DOCK_EXTRA_SPACE-1,
3622 dock->y_pos);
3623 XSetWindowBackgroundPixmap(dpy, scr->dock_shadow,
3624 ghost);
3625 XClearWindow(dpy, scr->dock_shadow);
3627 XMapRaised(dpy, scr->dock_shadow);
3628 swapped = 1;
3629 } else {
3630 if (superfluous && ghost!=None) {
3631 XFreePixmap(dpy, ghost);
3632 ghost = None;
3634 XUnmapWindow(dpy, scr->dock_shadow);
3635 swapped = 0;
3637 } else {
3638 /* is on right */
3639 if (ev.xmotion.x_root < dock->x_pos - ICON_SIZE) {
3640 XMoveWindow(dpy, scr->dock_shadow,
3641 DOCK_EXTRA_SPACE, dock->y_pos);
3642 if (superfluous && ghost==None) {
3643 ghost = MakeGhostDock(dock, dock->x_pos,
3644 DOCK_EXTRA_SPACE, dock->y_pos);
3645 XSetWindowBackgroundPixmap(dpy, scr->dock_shadow,
3646 ghost);
3647 XClearWindow(dpy, scr->dock_shadow);
3649 XMapRaised(dpy, scr->dock_shadow);
3650 swapped = -1;
3651 } else {
3652 XUnmapWindow(dpy, scr->dock_shadow);
3653 swapped = 0;
3654 if (superfluous && ghost!=None) {
3655 XFreePixmap(dpy, ghost);
3656 ghost = None;
3661 break;
3663 case ButtonPress:
3664 break;
3666 case ButtonRelease:
3667 if (ev.xbutton.button != event->xbutton.button)
3668 break;
3669 XUngrabPointer(dpy, CurrentTime);
3670 XUnmapWindow(dpy, scr->dock_shadow);
3671 XResizeWindow(dpy, scr->dock_shadow, ICON_SIZE, ICON_SIZE);
3672 if (dock->type == WM_DOCK) {
3673 if (swapped!=0) {
3674 if (swapped>0)
3675 dock->on_right_side = 1;
3676 else
3677 dock->on_right_side = 0;
3678 swapDock(dock);
3679 wArrangeIcons(scr, False);
3682 done = 1;
3683 break;
3686 if (superfluous) {
3687 if (ghost!=None)
3688 XFreePixmap(dpy, ghost);
3689 XSetWindowBackground(dpy, scr->dock_shadow, scr->white_pixel);
3691 #ifdef DEBUG
3692 puts("End dock move");
3693 #endif
3698 static void
3699 handleIconMove(WDock *dock, WAppIcon *aicon, XEvent *event)
3701 WScreen *scr = dock->screen_ptr;
3702 Window wins[2];
3703 WIcon *icon = aicon->icon;
3704 WDock *dock2 = NULL, *last_dock = dock, *clip = NULL;
3705 int ondock, grabbed = 0, change_dock = 0, collapsed = 0;
3706 XEvent ev;
3707 int x = aicon->x_pos, y = aicon->y_pos;
3708 int ofs_x = event->xbutton.x, ofs_y = event->xbutton.y;
3709 int shad_x = x, shad_y = y;
3710 int ix = aicon->xindex, iy = aicon->yindex;
3711 int tmp;
3712 Pixmap ghost = None;
3713 Bool docked;
3714 int superfluous = wPreferences.superfluous; /* we catch it to avoid problems */
3715 int omnipresent = aicon->omnipresent; /* this must be cached!!! */
3718 if (wPreferences.flags.noupdates)
3719 return;
3721 if (XGrabPointer(dpy, icon->core->window, True, ButtonMotionMask
3722 |ButtonReleaseMask|ButtonPressMask, GrabModeAsync,
3723 GrabModeAsync, None, None, CurrentTime) !=GrabSuccess) {
3724 #ifdef DEBUG0
3725 wwarning("pointer grab failed for icon move");
3726 #endif
3729 if (!(event->xbutton.state & MOD_MASK))
3730 wRaiseFrame(icon->core);
3732 if (!wPreferences.flags.noclip)
3733 clip = scr->workspaces[scr->current_workspace]->clip;
3735 if (dock == scr->dock && !wPreferences.flags.noclip)
3736 dock2 = clip;
3737 else if (dock != scr->dock && !wPreferences.flags.nodock)
3738 dock2 = scr->dock;
3740 wins[0] = icon->core->window;
3741 wins[1] = scr->dock_shadow;
3742 XRestackWindows(dpy, wins, 2);
3743 XMoveResizeWindow(dpy, scr->dock_shadow, aicon->x_pos, aicon->y_pos,
3744 ICON_SIZE, ICON_SIZE);
3745 if (superfluous) {
3746 if (icon->pixmap!=None)
3747 ghost = MakeGhostIcon(scr, icon->pixmap);
3748 else
3749 ghost = MakeGhostIcon(scr, icon->core->window);
3751 XSetWindowBackgroundPixmap(dpy, scr->dock_shadow, ghost);
3752 XClearWindow(dpy, scr->dock_shadow);
3754 XMapWindow(dpy, scr->dock_shadow);
3756 ondock = 1;
3759 while(1) {
3760 XMaskEvent(dpy, PointerMotionMask|ButtonReleaseMask|ButtonPressMask
3761 |ButtonMotionMask|ExposureMask, &ev);
3762 switch (ev.type) {
3763 case Expose:
3764 WMHandleEvent(&ev);
3765 break;
3767 case MotionNotify:
3768 if (!grabbed) {
3769 if (abs(ofs_x-ev.xmotion.x)>=MOVE_THRESHOLD
3770 || abs(ofs_y-ev.xmotion.y)>=MOVE_THRESHOLD) {
3771 XChangeActivePointerGrab(dpy, ButtonMotionMask
3772 |ButtonReleaseMask|ButtonPressMask,
3773 wCursor[WCUR_MOVE], CurrentTime);
3774 grabbed=1;
3775 } else {
3776 break;
3780 if (omnipresent) {
3781 int i;
3782 for (i=0; i<scr->workspace_count; i++) {
3783 if (i == scr->current_workspace)
3784 continue;
3785 wDockShowIcons(scr->workspaces[i]->clip);
3789 x = ev.xmotion.x_root - ofs_x;
3790 y = ev.xmotion.y_root - ofs_y;
3791 tmp = wDockSnapIcon(dock, aicon, x, y, &ix, &iy, True);
3792 if (tmp && dock2) {
3793 change_dock = 0;
3794 if (last_dock != dock && collapsed) {
3795 last_dock->collapsed = 1;
3796 wDockHideIcons(last_dock);
3797 collapsed = 0;
3799 if (!collapsed && (collapsed = dock->collapsed)) {
3800 dock->collapsed = 0;
3801 wDockShowIcons(dock);
3803 if (dock->auto_raise_lower)
3804 wDockRaise(dock);
3805 last_dock = dock;
3806 } else if (dock2) {
3807 tmp = wDockSnapIcon(dock2, aicon, x, y, &ix, &iy, False);
3808 if (tmp) {
3809 change_dock = 1;
3810 if (last_dock != dock2 && collapsed) {
3811 last_dock->collapsed = 1;
3812 wDockHideIcons(last_dock);
3813 collapsed = 0;
3815 if (!collapsed && (collapsed = dock2->collapsed)) {
3816 dock2->collapsed = 0;
3817 wDockShowIcons(dock2);
3819 if (dock2->auto_raise_lower)
3820 wDockRaise(dock2);
3821 last_dock = dock2;
3824 if (aicon->launching
3825 || aicon->lock
3826 || (aicon->running && !(ev.xmotion.state & MOD_MASK))
3827 || (!aicon->running && tmp)) {
3828 shad_x = last_dock->x_pos + ix*wPreferences.icon_size;
3829 shad_y = last_dock->y_pos + iy*wPreferences.icon_size;
3831 XMoveWindow(dpy, scr->dock_shadow, shad_x, shad_y);
3833 if (!ondock) {
3834 XMapWindow(dpy, scr->dock_shadow);
3836 ondock = 1;
3837 } else {
3838 if (ondock) {
3839 XUnmapWindow(dpy, scr->dock_shadow);
3841 ondock = 0;
3843 XMoveWindow(dpy, icon->core->window, x, y);
3844 break;
3846 case ButtonPress:
3847 break;
3849 case ButtonRelease:
3850 if (ev.xbutton.button != event->xbutton.button)
3851 break;
3852 XUngrabPointer(dpy, CurrentTime);
3853 if (ondock) {
3854 SlideWindow(icon->core->window, x, y, shad_x, shad_y);
3855 XUnmapWindow(dpy, scr->dock_shadow);
3856 if (!change_dock) {
3857 reattachIcon(dock, aicon, ix, iy);
3858 if (clip && dock!=clip && clip->auto_raise_lower)
3859 wDockLower(clip);
3860 } else {
3861 docked = moveIconBetweenDocks(dock, dock2, aicon, ix, iy);
3862 if (!docked) {
3863 /* Slide it back if dock rejected it */
3864 SlideWindow(icon->core->window, x, y, aicon->x_pos,
3865 aicon->y_pos);
3866 reattachIcon(dock, aicon, aicon->xindex,aicon->yindex);
3868 if (last_dock->type==WM_CLIP && last_dock->auto_collapse) {
3869 collapsed = 0;
3872 } else {
3873 aicon->x_pos = x;
3874 aicon->y_pos = y;
3875 if (superfluous) {
3876 if (!aicon->running && !wPreferences.no_animations) {
3877 /* We need to deselect it, even if is deselected in
3878 * wDockDetach(), because else DoKaboom() will fail.
3880 if (aicon->icon->selected)
3881 wIconSelect(aicon->icon);
3883 #ifdef WSOUND
3884 wSoundPlay(WSOUND_KABOOM);
3885 #endif
3886 DoKaboom(scr,aicon->icon->core->window, x, y);
3887 #ifdef WSOUND
3888 } else {
3889 wSoundPlay(WSOUND_UNDOCK);
3890 #endif
3892 #ifdef WSOUND
3893 } else {
3894 wSoundPlay(WSOUND_UNDOCK);
3895 #endif
3897 if (clip && clip->auto_raise_lower)
3898 wDockLower(clip);
3899 wDockDetach(dock, aicon);
3901 if (collapsed) {
3902 last_dock->collapsed = 1;
3903 wDockHideIcons(last_dock);
3904 collapsed = 0;
3906 if (superfluous) {
3907 if (ghost!=None)
3908 XFreePixmap(dpy, ghost);
3909 XSetWindowBackground(dpy, scr->dock_shadow, scr->white_pixel);
3911 if (omnipresent) {
3912 int i;
3913 for (i=0; i<scr->workspace_count; i++) {
3914 if (i == scr->current_workspace)
3915 continue;
3916 wDockHideIcons(scr->workspaces[i]->clip);
3920 #ifdef DEBUG
3921 puts("End icon move");
3922 #endif
3923 return;
3929 static int
3930 getClipButton(int px, int py)
3932 int pt = (CLIP_BUTTON_SIZE+2)*ICON_SIZE/64;
3934 if (px < 0 || py < 0 || px >= ICON_SIZE || py >= ICON_SIZE)
3935 return CLIP_IDLE;
3937 if (py <= pt-((int)ICON_SIZE-1-px))
3938 return CLIP_FORWARD;
3939 else if (px <= pt-((int)ICON_SIZE-1-py))
3940 return CLIP_REWIND;
3942 return CLIP_IDLE;
3946 static void
3947 handleClipChangeWorkspace(WScreen *scr, XEvent *event)
3949 XEvent ev;
3950 int done, direction, new_ws;
3951 int new_dir;
3952 WDock *clip = scr->clip_icon->dock;
3954 direction = getClipButton(event->xbutton.x, event->xbutton.y);
3956 clip->lclip_button_pushed = direction==CLIP_REWIND;
3957 clip->rclip_button_pushed = direction==CLIP_FORWARD;
3959 wClipIconPaint(scr->clip_icon);
3960 done = 0;
3961 while(!done) {
3962 WMMaskEvent(dpy, ExposureMask|ButtonMotionMask|ButtonReleaseMask
3963 |ButtonPressMask, &ev);
3964 switch (ev.type) {
3965 case Expose:
3966 WMHandleEvent(&ev);
3967 break;
3969 case MotionNotify:
3970 new_dir = getClipButton(ev.xmotion.x, ev.xmotion.y);
3971 if (new_dir != direction) {
3972 direction = new_dir;
3973 clip->lclip_button_pushed = direction==CLIP_REWIND;
3974 clip->rclip_button_pushed = direction==CLIP_FORWARD;
3975 wClipIconPaint(scr->clip_icon);
3977 break;
3979 case ButtonPress:
3980 break;
3982 case ButtonRelease:
3983 if (ev.xbutton.button == event->xbutton.button)
3984 done = 1;
3988 clip->lclip_button_pushed = 0;
3989 clip->rclip_button_pushed = 0;
3991 new_ws = wPreferences.ws_advance || (event->xbutton.state & ControlMask);
3993 if (direction == CLIP_FORWARD) {
3994 if (scr->current_workspace < scr->workspace_count-1)
3995 wWorkspaceChange(scr, scr->current_workspace+1);
3996 else if (new_ws && scr->current_workspace < MAX_WORKSPACES-1)
3997 wWorkspaceChange(scr, scr->current_workspace+1);
3998 else if (wPreferences.ws_cycle)
3999 wWorkspaceChange(scr, 0);
4001 else if (direction == CLIP_REWIND) {
4002 if (scr->current_workspace > 0)
4003 wWorkspaceChange(scr, scr->current_workspace-1);
4004 else if (scr->current_workspace==0 && wPreferences.ws_cycle)
4005 wWorkspaceChange(scr, scr->workspace_count-1);
4008 wClipIconPaint(scr->clip_icon);
4012 static void
4013 iconMouseDown(WObjDescriptor *desc, XEvent *event)
4015 WAppIcon *aicon = desc->parent;
4016 WDock *dock = aicon->dock;
4017 WScreen *scr = aicon->icon->core->screen_ptr;
4019 if (aicon->editing || WCHECK_STATE(WSTATE_MODAL))
4020 return;
4022 scr->last_dock = dock;
4024 if (dock->menu->flags.mapped)
4025 wMenuUnmap(dock->menu);
4027 if (IsDoubleClick(scr, event)) {
4028 /* double-click was not in the main clip icon */
4029 if (dock->type != WM_CLIP || aicon->xindex!=0 || aicon->yindex!=0
4030 || getClipButton(event->xbutton.x, event->xbutton.y)==CLIP_IDLE) {
4031 iconDblClick(desc, event);
4032 return;
4036 if (dock->type == WM_CLIP && scr->flags.clip_balloon_mapped) {
4037 XUnmapWindow(dpy, scr->clip_balloon);
4038 scr->flags.clip_balloon_mapped = 0;
4041 #ifdef DEBUG
4042 puts("handling dock");
4043 #endif
4044 if (event->xbutton.button == Button1) {
4045 if (event->xbutton.state & MOD_MASK)
4046 wDockLower(dock);
4047 else
4048 wDockRaise(dock);
4050 if ((event->xbutton.state & ShiftMask) && aicon!=scr->clip_icon &&
4051 dock->type!=WM_DOCK) {
4052 wIconSelect(aicon->icon);
4053 return;
4056 if (aicon->yindex==0 && aicon->xindex==0) {
4057 if (getClipButton(event->xbutton.x, event->xbutton.y)!=CLIP_IDLE
4058 && dock->type==WM_CLIP)
4059 handleClipChangeWorkspace(scr, event);
4060 else
4061 handleDockMove(dock, aicon, event);
4062 } else
4063 handleIconMove(dock, aicon, event);
4065 } else if (event->xbutton.button==Button2 && dock->type==WM_CLIP &&
4066 aicon==scr->clip_icon) {
4067 openClipWorkspaceMenu(scr, event->xbutton.x_root+2,
4068 event->xbutton.y_root+2);
4069 if (scr->clip_ws_menu) {
4070 WMenu *menu;
4071 menu = scr->clip_ws_menu;
4072 desc = &menu->menu->descriptor;
4074 event->xany.send_event = True;
4075 (*desc->handle_mousedown)(desc, event);
4077 } else if (event->xbutton.button==Button2 && dock->type==WM_CLIP &&
4078 (event->xbutton.state & ShiftMask) && aicon!=scr->clip_icon) {
4079 wClipMakeIconOmnipresent(aicon, !aicon->omnipresent);
4080 } else if (event->xbutton.button == Button3) {
4081 if (event->xbutton.send_event &&
4082 XGrabPointer(dpy, aicon->icon->core->window, True, ButtonMotionMask
4083 |ButtonReleaseMask|ButtonPressMask, GrabModeAsync,
4084 GrabModeAsync, None, None, CurrentTime) !=GrabSuccess) {
4085 wwarning("pointer grab failed for dockicon menu");
4086 return;
4089 openDockMenu(dock, aicon, event);
4094 static void
4095 showClipBalloon(WDock *dock, int workspace)
4097 int w, h;
4098 int x, y;
4099 WScreen *scr = dock->screen_ptr;
4100 char *text;
4101 Window stack[2];
4103 scr->flags.clip_balloon_mapped = 1;
4104 XMapWindow(dpy, scr->clip_balloon);
4106 text = scr->workspaces[workspace]->name;
4108 w = WMWidthOfString(scr->clip_title_font, text, strlen(text));
4110 h = WMFontHeight(scr->clip_title_font);
4111 XResizeWindow(dpy, scr->clip_balloon, w, h);
4113 x = dock->x_pos + CLIP_BUTTON_SIZE*ICON_SIZE/64;
4114 y = dock->y_pos + ICON_SIZE - WMFontHeight(scr->clip_title_font) - 3;
4116 if (x+w > scr->scr_width) {
4117 x = scr->scr_width - w;
4118 if (dock->y_pos + ICON_SIZE + h > scr->scr_height)
4119 y = dock->y_pos - h - 1;
4120 else
4121 y = dock->y_pos + ICON_SIZE;
4122 XRaiseWindow(dpy, scr->clip_balloon);
4123 } else {
4124 stack[0] = scr->clip_icon->icon->core->window;
4125 stack[1] = scr->clip_balloon;
4126 XRestackWindows(dpy, stack, 2);
4128 XMoveWindow(dpy, scr->clip_balloon, x, y);
4129 XSetForeground(dpy, scr->clip_title_gc,
4130 scr->clip_title_pixel[CLIP_NORMAL]);
4131 XClearWindow(dpy, scr->clip_balloon);
4132 WMDrawString(scr->wmscreen, scr->clip_balloon, scr->clip_title_gc,
4133 scr->clip_title_font, 0, 0, text, strlen(text));
4137 static void
4138 clipEnterNotify(WObjDescriptor *desc, XEvent *event)
4140 WAppIcon *btn = (WAppIcon*)desc->parent;
4141 WDock *dock;
4142 WScreen *scr;
4144 assert(event->type==EnterNotify);
4146 if(desc->parent_type!=WCLASS_DOCK_ICON)
4147 return;
4149 scr = btn->icon->core->screen_ptr;
4150 if (!btn->omnipresent)
4151 dock = btn->dock;
4152 else
4153 dock = scr->workspaces[scr->current_workspace]->clip;
4155 if (!dock || dock->type!=WM_CLIP)
4156 return;
4158 /* The auto raise/lower code */
4159 if (dock->auto_lower_magic) {
4160 WMDeleteTimerHandler(dock->auto_lower_magic);
4161 dock->auto_lower_magic = NULL;
4163 if (dock->auto_raise_lower && !dock->auto_raise_magic) {
4164 dock->auto_raise_magic = WMAddTimerHandler(AUTO_RAISE_DELAY,
4165 clipAutoRaise,
4166 (void *)dock);
4169 /* The auto expand/collapse code */
4170 if (dock->auto_collapse_magic) {
4171 WMDeleteTimerHandler(dock->auto_collapse_magic);
4172 dock->auto_collapse_magic = NULL;
4174 if (dock->auto_collapse && !dock->auto_expand_magic) {
4175 dock->auto_expand_magic = WMAddTimerHandler(AUTO_EXPAND_DELAY,
4176 clipAutoExpand,
4177 (void *)dock);
4180 if (btn->xindex == 0 && btn->yindex == 0)
4181 showClipBalloon(dock, dock->screen_ptr->current_workspace);
4182 else {
4183 if (dock->screen_ptr->flags.clip_balloon_mapped) {
4184 XUnmapWindow(dpy, dock->screen_ptr->clip_balloon);
4185 dock->screen_ptr->flags.clip_balloon_mapped = 0;
4191 static void
4192 clipLeave(WDock *dock)
4194 XEvent event;
4195 WObjDescriptor *desc = NULL;
4197 if (!dock || dock->type!=WM_CLIP)
4198 return;
4200 if (XCheckTypedEvent(dpy, EnterNotify, &event)!=False) {
4201 if (XFindContext(dpy, event.xcrossing.window, wWinContext,
4202 (XPointer *)&desc)!=XCNOENT
4203 && desc && desc->parent_type==WCLASS_DOCK_ICON
4204 && ((WAppIcon*)desc->parent)->dock
4205 && ((WAppIcon*)desc->parent)->dock->type==WM_CLIP) {
4206 /* We didn't left the Clip yet */
4207 XPutBackEvent(dpy, &event);
4208 return;
4211 XPutBackEvent(dpy, &event);
4212 } else {
4213 /* We entered a withdrawn window, so we're still in Clip */
4214 return;
4217 if (dock->auto_raise_magic) {
4218 WMDeleteTimerHandler(dock->auto_raise_magic);
4219 dock->auto_raise_magic = NULL;
4221 if (dock->auto_raise_lower && !dock->auto_lower_magic) {
4222 dock->auto_lower_magic = WMAddTimerHandler(AUTO_LOWER_DELAY,
4223 clipAutoLower,
4224 (void *)dock);
4227 if (dock->auto_expand_magic) {
4228 WMDeleteTimerHandler(dock->auto_expand_magic);
4229 dock->auto_expand_magic = NULL;
4231 if (dock->auto_collapse && !dock->auto_collapse_magic) {
4232 dock->auto_collapse_magic = WMAddTimerHandler(AUTO_COLLAPSE_DELAY,
4233 clipAutoCollapse,
4234 (void *)dock);
4239 static void
4240 clipLeaveNotify(WObjDescriptor *desc, XEvent *event)
4242 WAppIcon *btn = (WAppIcon*)desc->parent;
4244 assert(event->type==LeaveNotify);
4246 if(desc->parent_type!=WCLASS_DOCK_ICON)
4247 return;
4249 clipLeave(btn->dock);
4253 static void
4254 clipAutoCollapse(void *cdata)
4256 WDock *dock = (WDock *)cdata;
4258 if (dock->type!=WM_CLIP)
4259 return;
4261 if (dock->auto_collapse) {
4262 dock->collapsed = 1;
4263 wDockHideIcons(dock);
4265 dock->auto_collapse_magic = NULL;
4269 static void
4270 clipAutoExpand(void *cdata)
4272 WDock *dock = (WDock *)cdata;
4274 if (dock->type!=WM_CLIP)
4275 return;
4277 if (dock->auto_collapse) {
4278 dock->collapsed = 0;
4279 wDockShowIcons(dock);
4281 dock->auto_expand_magic = NULL;
4285 static void
4286 clipAutoLower(void *cdata)
4288 WDock *dock = (WDock *)cdata;
4290 if (dock->type!=WM_CLIP)
4291 return;
4293 if (dock->auto_raise_lower)
4294 wDockLower(dock);
4296 dock->auto_lower_magic = NULL;
4300 static void
4301 clipAutoRaise(void *cdata)
4303 WDock *dock = (WDock *)cdata;
4305 if (dock->type!=WM_CLIP)
4306 return;
4308 if (dock->auto_raise_lower)
4309 wDockRaise(dock);
4311 if (dock->screen_ptr->flags.clip_balloon_mapped) {
4312 showClipBalloon(dock, dock->screen_ptr->current_workspace);
4315 dock->auto_raise_magic = NULL;
4319 static Bool
4320 iconCanBeOmnipresent(WAppIcon *aicon)
4322 WScreen *scr = aicon->icon->core->screen_ptr;
4323 WDock *clip;
4324 WAppIcon *btn;
4325 int i, j;
4327 for (i=0; i<scr->workspace_count; i++) {
4328 clip = scr->workspaces[i]->clip;
4330 if (clip == aicon->dock)
4331 continue;
4333 if (clip->icon_count + scr->global_icon_count >= clip->max_icons)
4334 return False; /* Clip is full in some workspace */
4336 for (j=0; j<clip->max_icons; j++) {
4337 btn = clip->icon_array[j];
4338 if(btn && btn->xindex==aicon->xindex && btn->yindex==aicon->yindex)
4339 return False;
4343 return True;
4348 wClipMakeIconOmnipresent(WAppIcon *aicon, int omnipresent)
4350 WScreen *scr = aicon->icon->core->screen_ptr;
4351 WAppIconChain *new_entry, *tmp, *tmp1;
4352 int status = WO_SUCCESS;
4354 if ((scr->dock && aicon->dock==scr->dock) || aicon==scr->clip_icon) {
4355 return WO_NOT_APPLICABLE;
4358 if (aicon->omnipresent == omnipresent)
4359 return WO_SUCCESS;
4361 if (omnipresent) {
4362 if (iconCanBeOmnipresent(aicon)) {
4363 aicon->omnipresent = 1;
4364 new_entry = wmalloc(sizeof(WAppIconChain));
4365 new_entry->aicon = aicon;
4366 new_entry->next = scr->global_icons;
4367 scr->global_icons = new_entry;
4368 scr->global_icon_count++;
4369 } else {
4370 aicon->omnipresent = 0;
4371 status = WO_FAILED;
4373 } else {
4374 aicon->omnipresent = 0;
4375 if (aicon == scr->global_icons->aicon) {
4376 tmp = scr->global_icons->next;
4377 wfree(scr->global_icons);
4378 scr->global_icons = tmp;
4379 scr->global_icon_count--;
4380 } else {
4381 tmp = scr->global_icons;
4382 while (tmp->next) {
4383 if (tmp->next->aicon == aicon) {
4384 tmp1 = tmp->next->next;
4385 wfree(tmp->next);
4386 tmp->next = tmp1;
4387 scr->global_icon_count--;
4388 break;
4390 tmp = tmp->next;
4395 wAppIconPaint(aicon);
4397 return status;