- added WMRemoveFromArrayMatching(array, match, cdata), which will remove the
[wmaker-crm.git] / src / dock.c
blob112224e4744153c890adb7b90cea034f2e96232e
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 entry->flags.indicator_on = !(((WDock*)entry->clientdata)->lowered);
233 wMenuPaint(menu);
238 static void
239 killCallback(WMenu *menu, WMenuEntry *entry)
241 WAppIcon *icon;
242 char *buffer;
244 if (!WCHECK_STATE(WSTATE_NORMAL))
245 return;
247 assert(entry->clientdata!=NULL);
249 icon = (WAppIcon*)entry->clientdata;
251 icon->editing = 1;
253 WCHANGE_STATE(WSTATE_MODAL);
255 buffer = wstrconcat(icon->wm_class,
256 _(" will be forcibly closed.\n"
257 "Any unsaved changes will be lost.\n"
258 "Please confirm."));
260 if (wPreferences.dont_confirm_kill
261 || wMessageDialog(menu->frame->screen_ptr, _("Kill Application"),
262 buffer, _("Yes"), _("No"), NULL)==WAPRDefault) {
263 if (icon->icon && icon->icon->owner) {
264 wClientKill(icon->icon->owner);
268 wfree(buffer);
270 icon->editing = 0;
272 WCHANGE_STATE(WSTATE_NORMAL);
276 /* TODO: replace this function with a member of the dock struct */
277 static int
278 numberOfSelectedIcons(WDock *dock)
280 WAppIcon *aicon;
281 int i, n;
283 n = 0;
284 for (i=1; i<dock->max_icons; i++) {
285 aicon = dock->icon_array[i];
286 if (aicon && aicon->icon->selected) {
287 n++;
291 return n;
295 static WMArray*
296 getSelected(WDock *dock)
298 WMArray *ret = WMCreateArray(8);
299 WAppIcon *btn;
300 int i;
302 for (i=1; i<dock->max_icons; i++) {
303 btn = dock->icon_array[i];
304 if (btn && btn->icon->selected) {
305 WMAddToArray(ret, btn);
309 return ret;
313 static void
314 paintClipButtons(WAppIcon *clipIcon, Bool lpushed, Bool rpushed)
316 Window win = clipIcon->icon->core->window;
317 WScreen *scr = clipIcon->icon->core->screen_ptr;
318 XPoint p[4];
319 int pt = CLIP_BUTTON_SIZE*ICON_SIZE/64;
320 int tp = ICON_SIZE - pt;
321 int as = pt - 15; /* 15 = 5+5+5 */
322 GC gc = scr->clip_title_gc;
323 #ifdef GRADIENT_CLIP_ARROW
324 Bool collapsed = clipIcon->dock->collapsed;
325 #endif
327 if (rpushed) {
328 p[0].x = tp+1;
329 p[0].y = 1;
330 p[1].x = ICON_SIZE-2;
331 p[1].y = 1;
332 p[2].x = ICON_SIZE-2;
333 p[2].y = pt-1;
334 } else if (lpushed) {
335 p[0].x = 1;
336 p[0].y = tp;
337 p[1].x = pt;
338 p[1].y = ICON_SIZE-2;
339 p[2].x = 1;
340 p[2].y = ICON_SIZE-2;
342 if (lpushed || rpushed) {
343 XSetForeground(dpy, scr->draw_gc, scr->white_pixel);
344 XFillPolygon(dpy, win, scr->draw_gc, p, 3, Convex, CoordModeOrigin);
345 XSetForeground(dpy, scr->draw_gc, scr->black_pixel);
347 #ifdef GRADIENT_CLIP_ARROW
348 if (!collapsed) {
349 XSetFillStyle(dpy, scr->copy_gc, FillTiled);
350 XSetTile(dpy, scr->copy_gc, scr->clip_arrow_gradient);
351 XSetClipMask(dpy, scr->copy_gc, None);
352 gc = scr->copy_gc;
354 #endif /* GRADIENT_CLIP_ARROW */
356 /* top right arrow */
357 p[0].x = p[3].x = ICON_SIZE-5-as;
358 p[0].y = p[3].y = 5;
359 p[1].x = ICON_SIZE-6;
360 p[1].y = 5;
361 p[2].x = ICON_SIZE-6;
362 p[2].y = 4+as;
363 if (rpushed) {
364 XFillPolygon(dpy, win, scr->draw_gc, p, 3, Convex, CoordModeOrigin);
365 XDrawLines(dpy, win, scr->draw_gc, p, 4, CoordModeOrigin);
366 } else {
367 #ifdef GRADIENT_CLIP_ARROW
368 if (!collapsed)
369 XSetTSOrigin(dpy, gc, ICON_SIZE-6-as, 5);
370 #endif
371 XFillPolygon(dpy, win, gc, p,3,Convex,CoordModeOrigin);
372 XDrawLines(dpy, win, gc, p,4,CoordModeOrigin);
375 /* bottom left arrow */
376 p[0].x = p[3].x = 5;
377 p[0].y = p[3].y = ICON_SIZE-5-as;
378 p[1].x = 5;
379 p[1].y = ICON_SIZE-6;
380 p[2].x = 4+as;
381 p[2].y = ICON_SIZE-6;
382 if (lpushed) {
383 XFillPolygon(dpy, win, scr->draw_gc, p, 3, Convex, CoordModeOrigin);
384 XDrawLines(dpy, win, scr->draw_gc, p, 4, CoordModeOrigin);
385 } else {
386 #ifdef GRADIENT_CLIP_ARROW
387 if (!collapsed)
388 XSetTSOrigin(dpy, gc, 5, ICON_SIZE-6-as);
389 #endif
390 XFillPolygon(dpy, win, gc, p,3,Convex,CoordModeOrigin);
391 XDrawLines(dpy, win, gc, p,4,CoordModeOrigin);
393 #ifdef GRADIENT_CLIP_ARROW
394 if (!collapsed)
395 XSetFillStyle(dpy, scr->copy_gc, FillSolid);
396 #endif
400 RImage*
401 wClipMakeTile(WScreen *scr, RImage *normalTile)
403 RImage *tile = RCloneImage(normalTile);
404 RColor black;
405 RColor dark;
406 RColor light;
407 int pt, tp;
408 int as;
410 pt = CLIP_BUTTON_SIZE*wPreferences.icon_size/64;
411 tp = wPreferences.icon_size-1 - pt;
412 as = pt - 15;
414 black.alpha = 255;
415 black.red = black.green = black.blue = 0;
417 dark.alpha = 0;
418 dark.red = dark.green = dark.blue = 60;
420 light.alpha = 0;
421 light.red = light.green = light.blue = 80;
424 /* top right */
425 ROperateLine(tile, RSubtractOperation, tp, 0, wPreferences.icon_size-2,
426 pt-1, &dark);
427 RDrawLine(tile, tp-1, 0, wPreferences.icon_size-1, pt+1, &black);
428 ROperateLine(tile, RAddOperation, tp, 2, wPreferences.icon_size-3,
429 pt, &light);
431 /* arrow bevel */
432 ROperateLine(tile, RSubtractOperation, ICON_SIZE - 7 - as, 4,
433 ICON_SIZE - 5, 4, &dark);
434 ROperateLine(tile, RSubtractOperation, ICON_SIZE - 6 - as, 5,
435 ICON_SIZE - 5, 6 + as, &dark);
436 ROperateLine(tile, RAddOperation, ICON_SIZE - 5, 4, ICON_SIZE - 5, 6 + as,
437 &light);
439 /* bottom left */
440 ROperateLine(tile, RAddOperation, 2, tp+2, pt-2,
441 wPreferences.icon_size-3, &dark);
442 RDrawLine(tile, 0, tp-1, pt+1, wPreferences.icon_size-1, &black);
443 ROperateLine(tile, RSubtractOperation, 0, tp-2, pt+1,
444 wPreferences.icon_size-2, &light);
446 /* arrow bevel */
447 ROperateLine(tile, RSubtractOperation, 4, ICON_SIZE - 7 - as, 4,
448 ICON_SIZE - 5, &dark);
449 ROperateLine(tile, RSubtractOperation, 5, ICON_SIZE - 6 - as,
450 6 + as, ICON_SIZE - 5, &dark);
451 ROperateLine(tile, RAddOperation, 4, ICON_SIZE - 5, 6 + as, ICON_SIZE - 5,
452 &light);
454 return tile;
458 static void
459 omnipresentCallback(WMenu *menu, WMenuEntry *entry)
461 WAppIcon *clickedIcon = entry->clientdata;
462 WAppIcon *aicon;
463 WDock *dock;
464 WMArray *selectedIcons;
465 WMArrayIterator iter;
466 int failed;
468 assert(entry->clientdata!=NULL);
470 dock = clickedIcon->dock;
472 selectedIcons = getSelected(dock);
474 if (!WMGetArrayItemCount(selectedIcons))
475 WMAddToArray(selectedIcons, clickedIcon);
477 failed = 0;
478 WM_ITERATE_ARRAY(selectedIcons, aicon, iter) {
479 if (wClipMakeIconOmnipresent(aicon, !aicon->omnipresent) == WO_FAILED)
480 failed++;
481 else if (aicon->icon->selected)
482 wIconSelect(aicon->icon);
484 WMFreeArray(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 WMArray *selectedIcons;
513 int keepit;
514 WMArrayIterator it;
516 assert(clickedIcon!=NULL);
518 dock = clickedIcon->dock;
520 selectedIcons = getSelected(dock);
522 if (WMGetArrayItemCount(selectedIcons)) {
523 if (wMessageDialog(dock->screen_ptr, _("Workspace Clip"),
524 _("All selected icons will be removed!"),
525 _("OK"), _("Cancel"), NULL)!=WAPRDefault) {
526 WMFreeArray(selectedIcons);
527 return;
529 } else {
530 if (clickedIcon->xindex==0 && clickedIcon->yindex==0) {
531 WMFreeArray(selectedIcons);
532 return;
534 WMAddToArray(selectedIcons, clickedIcon);
537 WM_ITERATE_ARRAY(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 WMFreeArray(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 WMArray *selectedIcons;
562 WMArrayIterator it;
564 assert(clickedIcon!=NULL);
565 dock = clickedIcon->dock;
567 selectedIcons = getSelected(dock);
569 if (!WMGetArrayItemCount(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 WMFreeArray(selectedIcons);
590 return;
594 WMAddToArray(selectedIcons, clickedIcon);
597 WM_ITERATE_ARRAY(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 WMFreeArray(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 WMArray *selectedIcons;
697 WMArrayIterator iter;
698 WAppIcon *btn;
699 int i;
701 assert(clickedIcon!=NULL);
702 dock = clickedIcon->dock;
704 selectedIcons = getSelected(dock);
706 if (!WMGetArrayItemCount(selectedIcons)) {
707 for (i=1; i<dock->max_icons; i++) {
708 btn = dock->icon_array[i];
709 if (btn && !btn->icon->selected) {
710 wIconSelect(btn->icon);
713 } else {
714 WM_ITERATE_ARRAY(selectedIcons, btn, iter) {
715 wIconSelect(btn->icon);
718 WMFreeArray(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 WMArray *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 (WMGetArrayItemCount(selectedIcons)) {
873 WMArrayIterator iter;
875 WM_ITERATE_ARRAY(selectedIcons, btn, iter) {
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 WMFreeArray(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 entry->flags.indicator_on = !dock->lowered;
1005 entry->clientdata = dock;
1007 /* collapsed */
1008 entry = menu->entries[++index];
1009 entry->flags.indicator_on = dock->collapsed;
1010 entry->clientdata = dock;
1012 /* auto-collapse */
1013 entry = menu->entries[++index];
1014 entry->flags.indicator_on = dock->auto_collapse;
1015 entry->clientdata = dock;
1017 /* auto-raise/lower */
1018 entry = menu->entries[++index];
1019 entry->flags.indicator_on = dock->auto_raise_lower;
1020 entry->clientdata = dock;
1022 /* attract icons */
1023 entry = menu->entries[++index];
1024 entry->flags.indicator_on = dock->attract_icons;
1025 entry->clientdata = dock;
1027 menu->flags.realized = 0;
1028 wMenuRealize(menu);
1032 static WMenu*
1033 makeClipOptionsMenu(WScreen *scr)
1035 WMenu *menu;
1036 WMenuEntry *entry;
1038 menu = wMenuCreate(scr, NULL, False);
1039 if (!menu) {
1040 wwarning(_("could not create options submenu for Clip menu"));
1041 return NULL;
1044 entry = wMenuAddCallback(menu, _("Keep on Top"),
1045 toggleLoweredCallback, NULL);
1046 entry->flags.indicator = 1;
1047 entry->flags.indicator_on = 1;
1048 entry->flags.indicator_type = MI_CHECK;
1050 entry = wMenuAddCallback(menu, _("Collapsed"),
1051 toggleCollapsedCallback, NULL);
1052 entry->flags.indicator = 1;
1053 entry->flags.indicator_on = 1;
1054 entry->flags.indicator_type = MI_CHECK;
1056 entry = wMenuAddCallback(menu, _("Autocollapse"),
1057 toggleAutoCollapseCallback, NULL);
1058 entry->flags.indicator = 1;
1059 entry->flags.indicator_on = 1;
1060 entry->flags.indicator_type = MI_CHECK;
1062 entry = wMenuAddCallback(menu, _("Autoraise"),
1063 toggleAutoRaiseLowerCallback, NULL);
1064 entry->flags.indicator = 1;
1065 entry->flags.indicator_on = 1;
1066 entry->flags.indicator_type = MI_CHECK;
1068 entry = wMenuAddCallback(menu, _("Autoattract Icons"),
1069 toggleAutoAttractCallback, NULL);
1070 entry->flags.indicator = 1;
1071 entry->flags.indicator_on = 1;
1072 entry->flags.indicator_type = MI_CHECK;
1074 menu->flags.realized = 0;
1075 wMenuRealize(menu);
1077 return menu;
1081 static WMenu*
1082 dockMenuCreate(WScreen *scr, int type)
1084 WMenu *menu;
1085 WMenuEntry *entry;
1087 if (type == WM_CLIP && scr->clip_menu)
1088 return scr->clip_menu;
1090 menu = wMenuCreate(scr, NULL, False);
1091 if (type != WM_CLIP) {
1092 entry = wMenuAddCallback(menu, _("Keep on Top"),
1093 toggleLoweredCallback, NULL);
1094 entry->flags.indicator = 1;
1095 entry->flags.indicator_on = 1;
1096 entry->flags.indicator_type = MI_CHECK;
1097 } else {
1098 entry = wMenuAddCallback(menu, _("Clip Options"), NULL, NULL);
1099 scr->clip_options = makeClipOptionsMenu(scr);
1100 if (scr->clip_options)
1101 wMenuEntrySetCascade(menu, entry, scr->clip_options);
1103 entry = wMenuAddCallback(menu, _("Rename Workspace"), renameCallback,
1104 NULL);
1105 wfree(entry->text);
1106 entry->text = _("Rename Workspace");
1108 entry = wMenuAddCallback(menu, _("Selected"), selectCallback, NULL);
1109 entry->flags.indicator = 1;
1110 entry->flags.indicator_on = 1;
1111 entry->flags.indicator_type = MI_CHECK;
1113 entry = wMenuAddCallback(menu, _("Select All Icons"),
1114 selectIconsCallback, NULL);
1115 wfree(entry->text);
1116 entry->text = _("Select All Icons");
1118 entry = wMenuAddCallback(menu, _("Keep Icon"), keepIconsCallback, NULL);
1119 wfree(entry->text);
1120 entry->text = _("Keep Icon");
1122 entry = wMenuAddCallback(menu, _("Move Icon To"), NULL, NULL);
1123 wfree(entry->text);
1124 entry->text = _("Move Icon To");
1125 scr->clip_submenu = makeWorkspaceMenu(scr);
1126 if (scr->clip_submenu)
1127 wMenuEntrySetCascade(menu, entry, scr->clip_submenu);
1129 entry = wMenuAddCallback(menu, _("Remove Icon"), removeIconsCallback,
1130 NULL);
1131 wfree(entry->text);
1132 entry->text = _("Remove Icon");
1134 wMenuAddCallback(menu, _("Attract Icons"), colectIconsCallback, NULL);
1137 wMenuAddCallback(menu, _("Launch"), launchCallback, NULL);
1139 wMenuAddCallback(menu, _("Unhide Here"), unhideHereCallback, NULL);
1141 entry = wMenuAddCallback(menu, _("Hide"), hideCallback, NULL);
1142 wfree(entry->text);
1143 entry->text = _("Hide");
1145 wMenuAddCallback(menu, _("Settings..."), settingsCallback, NULL);
1147 wMenuAddCallback(menu, _("Kill"), killCallback, NULL);
1149 if (type == WM_CLIP)
1150 scr->clip_menu = menu;
1152 return menu;
1156 WDock*
1157 wDockCreate(WScreen *scr, int type)
1159 WDock *dock;
1160 WAppIcon *btn;
1161 int icon_count;
1163 make_keys();
1165 dock = wmalloc(sizeof(WDock));
1166 memset(dock, 0, sizeof(WDock));
1168 if (type == WM_CLIP)
1169 icon_count = CLIP_MAX_ICONS;
1170 else
1171 icon_count = scr->scr_height/wPreferences.icon_size;
1173 dock->icon_array = wmalloc(sizeof(WAppIcon*)*icon_count);
1174 memset(dock->icon_array, 0, sizeof(WAppIcon*)*icon_count);
1176 dock->max_icons = icon_count;
1178 btn = mainIconCreate(scr, type);
1180 btn->dock = dock;
1182 dock->x_pos = btn->x_pos;
1183 dock->y_pos = btn->y_pos;
1184 dock->screen_ptr = scr;
1185 dock->type = type;
1186 dock->icon_count = 1;
1187 dock->on_right_side = 1;
1188 dock->collapsed = 0;
1189 dock->auto_collapse = 0;
1190 dock->auto_collapse_magic = NULL;
1191 dock->auto_raise_lower = 0;
1192 dock->auto_lower_magic = NULL;
1193 dock->auto_raise_magic = NULL;
1194 dock->attract_icons = 0;
1195 dock->lowered = 1;
1196 dock->icon_array[0] = btn;
1197 wRaiseFrame(btn->icon->core);
1198 XMoveWindow(dpy, btn->icon->core->window, btn->x_pos, btn->y_pos);
1200 /* create dock menu */
1201 dock->menu = dockMenuCreate(scr, type);
1203 return dock;
1207 void
1208 wDockDestroy(WDock *dock)
1210 int i;
1211 WAppIcon *aicon;
1213 for (i=(dock->type == WM_CLIP) ? 1 : 0; i<dock->max_icons; i++) {
1214 aicon = dock->icon_array[i];
1215 if (aicon) {
1216 int keepit = aicon->running && wApplicationOf(aicon->main_window);
1217 wDockDetach(dock, aicon);
1218 if (keepit) {
1219 PlaceIcon(dock->screen_ptr, &aicon->x_pos, &aicon->y_pos);
1220 XMoveWindow(dpy, aicon->icon->core->window,
1221 aicon->x_pos, aicon->y_pos);
1222 if (!dock->mapped || dock->collapsed)
1223 XMapWindow(dpy, aicon->icon->core->window);
1227 if (wPreferences.auto_arrange_icons)
1228 wArrangeIcons(dock->screen_ptr, True);
1229 wfree(dock->icon_array);
1230 if (dock->menu && dock->type!=WM_CLIP)
1231 wMenuDestroy(dock->menu, True);
1232 if (dock->screen_ptr->last_dock == dock)
1233 dock->screen_ptr->last_dock = NULL;
1234 wfree(dock);
1238 void
1239 wClipIconPaint(WAppIcon *aicon)
1241 WScreen *scr = aicon->icon->core->screen_ptr;
1242 WWorkspace *workspace = scr->workspaces[scr->current_workspace];
1243 GC gc;
1244 Window win = aicon->icon->core->window;
1245 int length, nlength;
1246 char *ws_name, ws_number[10];
1247 int ty, tx;
1249 wIconPaint(aicon->icon);
1251 length = strlen(workspace->name);
1252 ws_name = malloc(length + 1);
1253 sprintf(ws_name, "%s", workspace->name);
1254 sprintf(ws_number, "%i", scr->current_workspace + 1);
1255 nlength = strlen(ws_number);
1257 gc = scr->clip_title_gc;
1259 if (!workspace->clip->collapsed)
1260 XSetForeground(dpy, gc, scr->clip_title_pixel[CLIP_NORMAL]);
1261 else
1262 XSetForeground(dpy, gc, scr->clip_title_pixel[CLIP_COLLAPSED]);
1264 ty = ICON_SIZE - WMFontHeight(scr->clip_title_font) - 3;
1266 tx = CLIP_BUTTON_SIZE*ICON_SIZE/64;
1268 WMDrawString(scr->wmscreen, win, gc, scr->clip_title_font, tx,
1269 ty, ws_name, length);
1271 tx = (ICON_SIZE/2 - WMWidthOfString(scr->clip_title_font, ws_number,
1272 nlength))/2;
1274 WMDrawString(scr->wmscreen, win, gc, scr->clip_title_font, tx,
1275 2, ws_number, nlength);
1277 wfree(ws_name);
1279 if (aicon->launching) {
1280 XFillRectangle(dpy, aicon->icon->core->window, scr->stipple_gc,
1281 0, 0, wPreferences.icon_size, wPreferences.icon_size);
1283 paintClipButtons(aicon, aicon->dock->lclip_button_pushed,
1284 aicon->dock->rclip_button_pushed);
1288 static void
1289 clipIconExpose(WObjDescriptor *desc, XEvent *event)
1291 wClipIconPaint(desc->parent);
1295 static void
1296 dockIconPaint(WAppIcon *btn)
1298 if (btn == btn->icon->core->screen_ptr->clip_icon)
1299 wClipIconPaint(btn);
1300 else
1301 wAppIconPaint(btn);
1305 static proplist_t
1306 make_icon_state(WAppIcon *btn)
1308 proplist_t node = NULL;
1309 proplist_t command, autolaunch, lock, name, forced, host, position, buggy;
1310 proplist_t omnipresent;
1311 char *tmp;
1312 char buffer[64];
1314 if (btn) {
1315 if (!btn->command)
1316 command = PLMakeString("-");
1317 else
1318 command = PLMakeString(btn->command);
1320 autolaunch = btn->auto_launch ? dYes : dNo;
1322 lock = btn->lock ? dYes : dNo;
1324 tmp = EscapeWM_CLASS(btn->wm_instance, btn->wm_class);
1326 name = PLMakeString(tmp);
1328 wfree(tmp);
1330 forced = btn->forced_dock ? dYes : dNo;
1332 buggy = btn->buggy_app ? dYes : dNo;
1334 if (btn == btn->icon->core->screen_ptr->clip_icon)
1335 sprintf(buffer, "%i,%i", btn->x_pos, btn->y_pos);
1336 else
1337 sprintf(buffer, "%hi,%hi", btn->xindex, btn->yindex);
1338 position = PLMakeString(buffer);
1340 node = PLMakeDictionaryFromEntries(dCommand, command,
1341 dName, name,
1342 dAutoLaunch, autolaunch,
1343 dLock, lock,
1344 dForced, forced,
1345 dBuggyApplication, buggy,
1346 dPosition, position,
1347 NULL);
1348 PLRelease(command);
1349 PLRelease(name);
1350 PLRelease(position);
1352 omnipresent = btn->omnipresent ? dYes : dNo;
1353 if (btn->dock != btn->icon->core->screen_ptr->dock &&
1354 (btn->xindex != 0 || btn->yindex != 0))
1355 PLInsertDictionaryEntry(node, dOmnipresent, omnipresent);
1357 #ifdef OFFIX_DND
1358 if (btn->dnd_command) {
1359 command = PLMakeString(btn->dnd_command);
1360 PLInsertDictionaryEntry(node, dDropCommand, command);
1361 PLRelease(command);
1363 #endif /* OFFIX_DND */
1365 if (btn->client_machine && btn->remote_start) {
1366 host = PLMakeString(btn->client_machine);
1367 PLInsertDictionaryEntry(node, dHost, host);
1368 PLRelease(host);
1372 return node;
1376 static proplist_t
1377 dockSaveState(WDock *dock)
1379 int i;
1380 proplist_t icon_info;
1381 proplist_t list=NULL, dock_state=NULL;
1382 proplist_t value, key;
1383 char buffer[256];
1385 list = PLMakeArrayFromElements(NULL);
1387 for (i=(dock->type==WM_DOCK ? 0 : 1); i<dock->max_icons; i++) {
1388 WAppIcon *btn = dock->icon_array[i];
1390 if (!btn || btn->attracted)
1391 continue;
1393 if ((icon_info = make_icon_state(dock->icon_array[i]))) {
1394 list = PLAppendArrayElement(list, icon_info);
1395 PLRelease(icon_info);
1399 dock_state = PLMakeDictionaryFromEntries(dApplications, list,
1400 NULL);
1402 if (dock->type == WM_DOCK) {
1403 sprintf(buffer, "Applications%i", dock->screen_ptr->scr_height);
1404 key = PLMakeString(buffer);
1405 PLInsertDictionaryEntry(dock_state, key, list);
1406 PLRelease(key);
1409 sprintf(buffer, "%i,%i", (dock->on_right_side ? -ICON_SIZE : 0),
1410 dock->y_pos);
1411 value = PLMakeString(buffer);
1412 PLInsertDictionaryEntry(dock_state, dPosition, value);
1413 PLRelease(value);
1415 PLRelease(list);
1418 value = (dock->lowered ? dYes : dNo);
1419 PLInsertDictionaryEntry(dock_state, dLowered, value);
1421 if (dock->type == WM_CLIP) {
1422 value = (dock->collapsed ? dYes : dNo);
1423 PLInsertDictionaryEntry(dock_state, dCollapsed, value);
1425 value = (dock->auto_collapse ? dYes : dNo);
1426 PLInsertDictionaryEntry(dock_state, dAutoCollapse, value);
1428 value = (dock->auto_raise_lower ? dYes : dNo);
1429 PLInsertDictionaryEntry(dock_state, dAutoRaiseLower, value);
1431 value = (dock->attract_icons ? dYes : dNo);
1432 PLInsertDictionaryEntry(dock_state, dAutoAttractIcons, value);
1435 return dock_state;
1439 void
1440 wDockSaveState(WScreen *scr, proplist_t old_state)
1442 proplist_t dock_state;
1443 proplist_t keys;
1445 dock_state = dockSaveState(scr->dock);
1448 * Copy saved states of docks with different sizes.
1450 if (old_state) {
1451 int i;
1452 proplist_t tmp;
1454 keys = PLGetAllDictionaryKeys(old_state);
1455 for (i = 0; i < PLGetNumberOfElements(keys); i++) {
1456 tmp = PLGetArrayElement(keys, i);
1458 if (strncasecmp(PLGetString(tmp), "applications", 12) == 0
1459 && !PLGetDictionaryEntry(dock_state, tmp)) {
1461 PLInsertDictionaryEntry(dock_state,
1462 tmp,
1463 PLGetDictionaryEntry(old_state, tmp));
1466 PLRelease(keys);
1470 PLInsertDictionaryEntry(scr->session_state, dDock, dock_state);
1472 PLRelease(dock_state);
1476 void
1477 wClipSaveState(WScreen *scr)
1479 proplist_t clip_state;
1481 clip_state = make_icon_state(scr->clip_icon);
1483 PLInsertDictionaryEntry(scr->session_state, dClip, clip_state);
1485 PLRelease(clip_state);
1489 proplist_t
1490 wClipSaveWorkspaceState(WScreen *scr, int workspace)
1492 return dockSaveState(scr->workspaces[workspace]->clip);
1496 static Bool
1497 getBooleanDockValue(proplist_t value, proplist_t key)
1499 if (value) {
1500 if (PLIsString(value)) {
1501 if (strcasecmp(PLGetString(value), "YES")==0)
1502 return True;
1503 } else {
1504 wwarning(_("bad value in docked icon state info %s"),
1505 PLGetString(key));
1508 return False;
1512 static WAppIcon*
1513 restore_icon_state(WScreen *scr, proplist_t info, int type, int index)
1515 WAppIcon *aicon;
1516 proplist_t cmd, value;
1519 cmd = PLGetDictionaryEntry(info, dCommand);
1520 if (!cmd || !PLIsString(cmd)) {
1521 return NULL;
1524 /* parse window name */
1525 value = PLGetDictionaryEntry(info, dName);
1526 if (!value)
1527 return NULL;
1530 char *wclass, *winstance;
1531 char *command;
1533 ParseWindowName(value, &winstance, &wclass, "dock");
1535 if (!winstance && !wclass) {
1536 return NULL;
1539 /* get commands */
1541 if (cmd)
1542 command = wstrdup(PLGetString(cmd));
1543 else
1544 command = NULL;
1546 if (!command || strcmp(command, "-")==0) {
1547 if (command)
1548 wfree(command);
1549 if (wclass)
1550 wfree(wclass);
1551 if (winstance)
1552 wfree(winstance);
1554 return NULL;
1557 aicon = wAppIconCreateForDock(scr, command, winstance, wclass,
1558 TILE_NORMAL);
1559 if (wclass)
1560 wfree(wclass);
1561 if (winstance)
1562 wfree(winstance);
1563 if (command)
1564 wfree(command);
1567 aicon->icon->core->descriptor.handle_mousedown = iconMouseDown;
1568 if (type == WM_CLIP) {
1569 aicon->icon->core->descriptor.handle_enternotify = clipEnterNotify;
1570 aicon->icon->core->descriptor.handle_leavenotify = clipLeaveNotify;
1572 aicon->icon->core->descriptor.parent_type = WCLASS_DOCK_ICON;
1573 aicon->icon->core->descriptor.parent = aicon;
1576 #ifdef OFFIX_DND
1577 cmd = PLGetDictionaryEntry(info, dDropCommand);
1578 if (cmd)
1579 aicon->dnd_command = wstrdup(PLGetString(cmd));
1580 #endif
1582 /* check auto launch */
1583 value = PLGetDictionaryEntry(info, dAutoLaunch);
1585 aicon->auto_launch = getBooleanDockValue(value, dAutoLaunch);
1587 /* check lock */
1588 value = PLGetDictionaryEntry(info, dLock);
1590 aicon->lock = getBooleanDockValue(value, dLock);
1592 /* check if it wasn't normally docked */
1593 value = PLGetDictionaryEntry(info, dForced);
1595 aicon->forced_dock = getBooleanDockValue(value, dForced);
1597 /* check if we can rely on the stuff in the app */
1598 value = PLGetDictionaryEntry(info, dBuggyApplication);
1600 aicon->buggy_app = getBooleanDockValue(value, dBuggyApplication);
1602 /* get position in the dock */
1603 value = PLGetDictionaryEntry(info, dPosition);
1604 if (value && PLIsString(value)) {
1605 if (sscanf(PLGetString(value), "%hi,%hi", &aicon->xindex,
1606 &aicon->yindex)!=2)
1607 wwarning(_("bad value in docked icon state info %s"),
1608 PLGetString(dPosition));
1610 /* check position sanity */
1611 /* incomplete section! */
1612 if (type == WM_DOCK) {
1613 aicon->xindex = 0;
1614 if (aicon->yindex < 0)
1615 wwarning(_("bad value in docked icon position %i,%i"),
1616 aicon->xindex, aicon->yindex);
1618 } else {
1619 aicon->yindex = index;
1620 aicon->xindex = 0;
1623 /* check if icon is omnipresent */
1624 value = PLGetDictionaryEntry(info, dOmnipresent);
1626 aicon->omnipresent = getBooleanDockValue(value, dOmnipresent);
1628 aicon->running = 0;
1629 aicon->docked = 1;
1631 return aicon;
1635 #define COMPLAIN(key) wwarning(_("bad value in dock state info:%s"), key)
1638 WAppIcon*
1639 wClipRestoreState(WScreen *scr, proplist_t clip_state)
1641 WAppIcon *icon;
1642 proplist_t value;
1645 icon = mainIconCreate(scr, WM_CLIP);
1647 if (!clip_state)
1648 return icon;
1649 else
1650 PLRetain(clip_state);
1652 /* restore position */
1654 value = PLGetDictionaryEntry(clip_state, dPosition);
1656 if (value) {
1657 if (!PLIsString(value))
1658 COMPLAIN("Position");
1659 else {
1660 if (sscanf(PLGetString(value), "%i,%i", &icon->x_pos,
1661 &icon->y_pos)!=2)
1662 COMPLAIN("Position");
1664 /* check position sanity */
1665 if (icon->y_pos < 0)
1666 icon->y_pos = 0;
1667 else if (icon->y_pos > scr->scr_height-ICON_SIZE)
1668 icon->y_pos = scr->scr_height-ICON_SIZE;
1670 if (icon->x_pos < 0)
1671 icon->x_pos = 0;
1672 else if (icon->x_pos > scr->scr_width-ICON_SIZE)
1673 icon->x_pos = scr->scr_width-ICON_SIZE;
1677 #ifdef OFFIX_DND
1678 value = PLGetDictionaryEntry(clip_state, dDropCommand);
1679 if (value && PLIsString(value))
1680 icon->dnd_command = wstrdup(PLGetString(value));
1681 #endif
1683 PLRelease(clip_state);
1685 return icon;
1689 WDock*
1690 wDockRestoreState(WScreen *scr, proplist_t dock_state, int type)
1692 WDock *dock;
1693 proplist_t apps;
1694 proplist_t value;
1695 WAppIcon *aicon, *old_top;
1696 int count, i;
1699 dock = wDockCreate(scr, type);
1701 if (!dock_state)
1702 return dock;
1704 if (dock_state)
1705 PLRetain(dock_state);
1708 /* restore position */
1710 value = PLGetDictionaryEntry(dock_state, dPosition);
1712 if (value) {
1713 if (!PLIsString(value))
1714 COMPLAIN("Position");
1715 else {
1716 if (sscanf(PLGetString(value), "%i,%i", &dock->x_pos,
1717 &dock->y_pos)!=2)
1718 COMPLAIN("Position");
1720 /* check position sanity */
1721 if (dock->y_pos < 0)
1722 dock->y_pos = 0;
1723 else if (dock->y_pos > scr->scr_height-ICON_SIZE)
1724 dock->y_pos = scr->scr_height - ICON_SIZE;
1726 /* This is no more needed. ??? */
1727 if (type == WM_CLIP) {
1728 if (dock->x_pos < 0)
1729 dock->x_pos = 0;
1730 else if (dock->x_pos > scr->scr_width-ICON_SIZE)
1731 dock->x_pos = scr->scr_width-ICON_SIZE;
1732 } else {
1733 if (dock->x_pos >= 0) {
1734 dock->x_pos = DOCK_EXTRA_SPACE;
1735 dock->on_right_side = 0;
1736 } else {
1737 dock->x_pos = scr->scr_width - DOCK_EXTRA_SPACE - ICON_SIZE;
1738 dock->on_right_side = 1;
1744 /* restore lowered/raised state */
1746 dock->lowered = 0;
1748 value = PLGetDictionaryEntry(dock_state, dLowered);
1750 if (value) {
1751 if (!PLIsString(value))
1752 COMPLAIN("Lowered");
1753 else {
1754 if (strcasecmp(PLGetString(value), "YES")==0)
1755 dock->lowered = 1;
1760 /* restore collapsed state */
1762 dock->collapsed = 0;
1764 value = PLGetDictionaryEntry(dock_state, dCollapsed);
1766 if (value) {
1767 if (!PLIsString(value))
1768 COMPLAIN("Collapsed");
1769 else {
1770 if (strcasecmp(PLGetString(value), "YES")==0)
1771 dock->collapsed = 1;
1776 /* restore auto-collapsed state */
1778 value = PLGetDictionaryEntry(dock_state, dAutoCollapse);
1780 if (value) {
1781 if (!PLIsString(value))
1782 COMPLAIN("AutoCollapse");
1783 else {
1784 if (strcasecmp(PLGetString(value), "YES")==0) {
1785 dock->auto_collapse = 1;
1786 dock->collapsed = 1;
1792 /* restore auto-raise/lower state */
1794 value = PLGetDictionaryEntry(dock_state, dAutoRaiseLower);
1796 if (value) {
1797 if (!PLIsString(value))
1798 COMPLAIN("AutoRaiseLower");
1799 else {
1800 if (strcasecmp(PLGetString(value), "YES")==0) {
1801 dock->auto_raise_lower = 1;
1806 /* restore attract icons state */
1808 dock->attract_icons = 0;
1810 value = PLGetDictionaryEntry(dock_state, dAutoAttractIcons);
1812 if (value) {
1813 if (!PLIsString(value))
1814 COMPLAIN("AutoAttractIcons");
1815 else {
1816 if (strcasecmp(PLGetString(value), "YES")==0)
1817 dock->attract_icons = 1;
1822 /* application list */
1825 proplist_t tmp;
1826 char buffer[64];
1829 * When saving, it saves the dock state in
1830 * Applications and Applicationsnnn
1832 * When loading, it will first try Applicationsnnn.
1833 * If it does not exist, use Applications as default.
1836 sprintf(buffer, "Applications%i", scr->scr_height);
1838 tmp = PLMakeString(buffer);
1839 apps = PLGetDictionaryEntry(dock_state, tmp);
1840 PLRelease(tmp);
1842 if (!apps) {
1843 apps = PLGetDictionaryEntry(dock_state, dApplications);
1847 if (!apps) {
1848 goto finish;
1851 count = PLGetNumberOfElements(apps);
1853 if (count==0)
1854 goto finish;
1856 old_top = dock->icon_array[0];
1858 /* dock->icon_count is set to 1 when dock is created.
1859 * Since Clip is already restored, we want to keep it so for clip,
1860 * but for dock we may change the default top tile, so we set it to 0.
1862 if (type == WM_DOCK)
1863 dock->icon_count = 0;
1865 for (i=0; i<count; i++) {
1866 if (dock->icon_count >= dock->max_icons) {
1867 wwarning(_("there are too many icons stored in dock. Ignoring what doesn't fit"));
1868 break;
1871 value = PLGetArrayElement(apps, i);
1872 aicon = restore_icon_state(scr, value, type, dock->icon_count);
1874 dock->icon_array[dock->icon_count] = aicon;
1876 if (aicon) {
1877 aicon->dock = dock;
1878 aicon->x_pos = dock->x_pos + (aicon->xindex*ICON_SIZE);
1879 aicon->y_pos = dock->y_pos + (aicon->yindex*ICON_SIZE);
1881 if (dock->lowered)
1882 ChangeStackingLevel(aicon->icon->core, WMNormalLevel);
1883 else
1884 ChangeStackingLevel(aicon->icon->core, WMDockLevel);
1886 wCoreConfigure(aicon->icon->core, aicon->x_pos, aicon->y_pos,
1887 0, 0);
1889 if (!dock->collapsed)
1890 XMapWindow(dpy, aicon->icon->core->window);
1891 wRaiseFrame(aicon->icon->core);
1893 dock->icon_count++;
1894 } else if (dock->icon_count==0 && type==WM_DOCK)
1895 dock->icon_count++;
1898 /* if the first icon is not defined, use the default */
1899 if (dock->icon_array[0]==NULL) {
1900 /* update default icon */
1901 old_top->x_pos = dock->x_pos;
1902 old_top->y_pos = dock->y_pos;
1903 if (dock->lowered)
1904 ChangeStackingLevel(old_top->icon->core, WMNormalLevel);
1905 else
1906 ChangeStackingLevel(old_top->icon->core, WMDockLevel);
1907 dock->icon_array[0] = old_top;
1908 XMoveWindow(dpy, old_top->icon->core->window, dock->x_pos, dock->y_pos);
1909 /* we don't need to increment dock->icon_count here because it was
1910 * incremented in the loop above.
1912 } else if (old_top!=dock->icon_array[0]) {
1913 if (old_top == scr->clip_icon)
1914 scr->clip_icon = dock->icon_array[0];
1915 wAppIconDestroy(old_top);
1918 finish:
1919 if (dock_state)
1920 PLRelease(dock_state);
1922 return dock;
1927 void
1928 wDockLaunchWithState(WDock *dock, WAppIcon *btn, WSavedState *state)
1930 if (btn && btn->command && !btn->running && !btn->launching) {
1932 btn->drop_launch = 0;
1934 btn->pid = execCommand(btn, btn->command, state);
1936 if (btn->pid>0) {
1937 if (!btn->forced_dock && !btn->buggy_app) {
1938 btn->launching = 1;
1939 dockIconPaint(btn);
1942 } else {
1943 wfree(state);
1948 void
1949 wDockDoAutoLaunch(WDock *dock, int workspace)
1951 WAppIcon *btn;
1952 WSavedState *state;
1953 int i;
1955 for (i = 0; i < dock->max_icons; i++) {
1956 btn = dock->icon_array[i];
1957 if (!btn || !btn->auto_launch)
1958 continue;
1960 state = wmalloc(sizeof(WSavedState));
1961 memset(state, 0, sizeof(WSavedState));
1962 state->workspace = workspace;
1963 /* TODO: this is klugy and is very difficult to understand
1964 * what's going on. Try to clean up */
1965 wDockLaunchWithState(dock, btn, state);
1970 #ifdef OFFIX_DND
1971 static WDock*
1972 findDock(WScreen *scr, XEvent *event, int *icon_pos)
1974 WDock *dock;
1975 int i;
1977 *icon_pos = -1;
1978 if ((dock = scr->dock)!=NULL) {
1979 for (i=0; i<dock->max_icons; i++) {
1980 if (dock->icon_array[i]
1981 && dock->icon_array[i]->icon->core->window==event->xclient.window) {
1982 *icon_pos = i;
1983 break;
1987 if (*icon_pos<0 && (dock = scr->workspaces[scr->current_workspace]->clip)!=NULL) {
1988 for (i=0; i<dock->max_icons; i++) {
1989 if (dock->icon_array[i]
1990 && dock->icon_array[i]->icon->core->window==event->xclient.window) {
1991 *icon_pos = i;
1992 break;
1996 if(*icon_pos>=0)
1997 return dock;
1998 return NULL;
2003 wDockReceiveDNDDrop(WScreen *scr, XEvent *event)
2005 WDock *dock;
2006 WAppIcon *btn;
2007 int icon_pos;
2009 dock = findDock(scr, event, &icon_pos);
2010 if (!dock)
2011 return False;
2014 * Return True if the drop was on an application icon window.
2015 * In this case, let the ClientMessage handler redirect the
2016 * message to the app.
2018 if (dock->icon_array[icon_pos]->icon->icon_win!=None)
2019 return True;
2021 if (dock->icon_array[icon_pos]->dnd_command!=NULL) {
2022 scr->flags.dnd_data_convertion_status = 0;
2024 btn = dock->icon_array[icon_pos];
2026 if (!btn->forced_dock) {
2027 btn->relaunching = btn->running;
2028 btn->running = 1;
2030 if (btn->wm_instance || btn->wm_class) {
2031 WWindowAttributes attr;
2032 memset(&attr, 0, sizeof(WWindowAttributes));
2033 wDefaultFillAttributes(btn->icon->core->screen_ptr,
2034 btn->wm_instance,
2035 btn->wm_class, &attr, NULL, True);
2037 if (!attr.no_appicon)
2038 btn->launching = 1;
2039 else
2040 btn->running = 0;
2043 btn->drop_launch = 1;
2044 scr->last_dock = dock;
2045 btn->pid = execCommand(btn, btn->dnd_command, NULL);
2046 if (btn->pid>0) {
2047 dockIconPaint(btn);
2048 } else {
2049 btn->launching = 0;
2050 if (!btn->relaunching) {
2051 btn->running = 0;
2055 return False;
2057 #endif /* OFFIX_DND */
2061 Bool
2062 wDockAttachIcon(WDock *dock, WAppIcon *icon, int x, int y)
2064 WWindow *wwin;
2065 char **argv;
2066 int argc;
2067 int index;
2069 wwin = icon->icon->owner;
2070 if (icon->command==NULL) {
2071 icon->editing = 0;
2072 if (XGetCommand(dpy, wwin->client_win, &argv, &argc) && argc>0) {
2074 icon->command = wtokenjoin(argv, argc);
2075 XFreeStringList(argv);
2076 } else {
2077 char *command=NULL;
2079 /* icon->forced_dock = 1;*/
2080 if (dock->type!=WM_CLIP || !icon->attracted) {
2081 icon->editing = 1;
2082 if (wInputDialog(dock->screen_ptr, _("Dock Icon"),
2083 _("Type the command used to launch the application"),
2084 &command)) {
2085 if (command && (command[0]==0 ||
2086 (command[0]=='-' && command[1]==0))) {
2087 wfree(command);
2088 command = NULL;
2090 icon->command = command;
2091 icon->editing = 0;
2092 } else {
2093 icon->editing = 0;
2094 if (command)
2095 wfree(command);
2096 /* If the target is the dock, reject the icon. If
2097 * the target is the clip, make it an attracted icon
2099 if (dock->type==WM_CLIP) {
2100 icon->attracted = 1;
2101 if (!icon->icon->shadowed) {
2102 icon->icon->shadowed = 1;
2103 icon->icon->force_paint = 1;
2105 } else {
2106 return False;
2111 } else {
2112 icon->editing = 0;
2115 for (index=1; index<dock->max_icons; index++)
2116 if (dock->icon_array[index] == NULL)
2117 break;
2118 /* if (index == dock->max_icons)
2119 return; */
2121 assert(index < dock->max_icons);
2123 dock->icon_array[index] = icon;
2124 icon->yindex = y;
2125 icon->xindex = x;
2127 icon->omnipresent = 0;
2129 icon->x_pos = dock->x_pos + x*ICON_SIZE;
2130 icon->y_pos = dock->y_pos + y*ICON_SIZE;
2132 dock->icon_count++;
2134 icon->running = 1;
2135 icon->launching = 0;
2136 icon->docked = 1;
2137 icon->dock = dock;
2138 icon->icon->core->descriptor.handle_mousedown = iconMouseDown;
2139 if (dock->type == WM_CLIP) {
2140 icon->icon->core->descriptor.handle_enternotify = clipEnterNotify;
2141 icon->icon->core->descriptor.handle_leavenotify = clipLeaveNotify;
2143 icon->icon->core->descriptor.parent_type = WCLASS_DOCK_ICON;
2144 icon->icon->core->descriptor.parent = icon;
2146 MoveInStackListUnder(dock->icon_array[index-1]->icon->core,
2147 icon->icon->core);
2148 wAppIconMove(icon, icon->x_pos, icon->y_pos);
2149 wAppIconPaint(icon);
2151 if (wPreferences.auto_arrange_icons)
2152 wArrangeIcons(dock->screen_ptr, True);
2154 #ifdef OFFIX_DND
2155 if (icon->command && !icon->dnd_command) {
2156 icon->dnd_command = wmalloc(strlen(icon->command)+8);
2157 sprintf(icon->dnd_command, "%s %%d", icon->command);
2159 #endif
2161 return True;
2165 void
2166 reattachIcon(WDock *dock, WAppIcon *icon, int x, int y)
2168 int index;
2170 for(index=1; index<dock->max_icons; index++) {
2171 if(dock->icon_array[index] == icon)
2172 break;
2174 assert(index < dock->max_icons);
2176 icon->yindex = y;
2177 icon->xindex = x;
2179 icon->x_pos = dock->x_pos + x*ICON_SIZE;
2180 icon->y_pos = dock->y_pos + y*ICON_SIZE;
2184 Bool
2185 moveIconBetweenDocks(WDock *src, WDock *dest, WAppIcon *icon, int x, int y)
2187 WWindow *wwin;
2188 char **argv;
2189 int argc;
2190 int index;
2192 if (src == dest)
2193 return True; /* No move needed, we're already there */
2195 if (dest == NULL)
2196 return False;
2198 wwin = icon->icon->owner;
2201 * For the moment we can't do this if we move icons in Clip from one
2202 * workspace to other, because if we move two or more icons without
2203 * command, the dialog box will not be able to tell us to which of the
2204 * moved icons it applies. -Dan
2206 if ((dest->type==WM_DOCK /*|| dest->keep_attracted*/) && icon->command==NULL) {
2207 if (XGetCommand(dpy, wwin->client_win, &argv, &argc) && argc>0) {
2209 icon->command = wtokenjoin(argv, argc);
2210 XFreeStringList(argv);
2211 } else {
2212 char *command=NULL;
2214 icon->editing = 1;
2215 /* icon->forced_dock = 1;*/
2216 if (wInputDialog(src->screen_ptr, _("Dock Icon"),
2217 _("Type the command used to launch the application"),
2218 &command)) {
2219 if (command && (command[0]==0 ||
2220 (command[0]=='-' && command[1]==0))) {
2221 wfree(command);
2222 command = NULL;
2224 icon->command = command;
2225 } else {
2226 icon->editing = 0;
2227 if (command)
2228 wfree(command);
2229 return False;
2231 icon->editing = 0;
2235 if (dest->type == WM_DOCK)
2236 wClipMakeIconOmnipresent(icon, False);
2238 for(index=1; index<src->max_icons; index++) {
2239 if(src->icon_array[index] == icon)
2240 break;
2242 assert(index < src->max_icons);
2244 src->icon_array[index] = NULL;
2245 src->icon_count--;
2247 for(index=1; index<dest->max_icons; index++) {
2248 if(dest->icon_array[index] == NULL)
2249 break;
2251 /* if (index == dest->max_icons)
2252 return; */
2254 assert(index < dest->max_icons);
2256 dest->icon_array[index] = icon;
2257 icon->dock = dest;
2259 /* deselect the icon */
2260 if (icon->icon->selected)
2261 wIconSelect(icon->icon);
2263 if (dest->type == WM_DOCK) {
2264 icon->icon->core->descriptor.handle_enternotify = NULL;
2265 icon->icon->core->descriptor.handle_leavenotify = NULL;
2266 } else {
2267 icon->icon->core->descriptor.handle_enternotify = clipEnterNotify;
2268 icon->icon->core->descriptor.handle_leavenotify = clipLeaveNotify;
2271 /* set it to be kept when moving to dock.
2272 * Unless the icon does not have a command set
2274 if (icon->command && dest->type==WM_DOCK) {
2275 icon->attracted = 0;
2276 if (icon->icon->shadowed) {
2277 icon->icon->shadowed = 0;
2278 icon->icon->force_paint = 1;
2282 if (src->auto_collapse || src->auto_raise_lower)
2283 clipLeave(src);
2285 icon->yindex = y;
2286 icon->xindex = x;
2288 icon->x_pos = dest->x_pos + x*ICON_SIZE;
2289 icon->y_pos = dest->y_pos + y*ICON_SIZE;
2291 dest->icon_count++;
2293 MoveInStackListUnder(dest->icon_array[index-1]->icon->core,
2294 icon->icon->core);
2295 wAppIconPaint(icon);
2297 return True;
2301 void
2302 wDockDetach(WDock *dock, WAppIcon *icon)
2304 int index;
2306 /* make the settings panel be closed */
2307 if (icon->panel) {
2308 DestroyDockAppSettingsPanel(icon->panel);
2311 /* This must be called before icon->dock is set to NULL.
2312 * Don't move it. -Dan
2314 wClipMakeIconOmnipresent(icon, False);
2316 icon->docked = 0;
2317 icon->dock = NULL;
2318 icon->attracted = 0;
2319 icon->auto_launch = 0;
2320 if (icon->icon->shadowed) {
2321 icon->icon->shadowed = 0;
2322 icon->icon->force_paint = 1;
2325 /* deselect the icon */
2326 if (icon->icon->selected)
2327 wIconSelect(icon->icon);
2329 if (icon->command) {
2330 wfree(icon->command);
2331 icon->command = NULL;
2333 #ifdef OFFIX_DND
2334 if (icon->dnd_command) {
2335 wfree(icon->dnd_command);
2336 icon->dnd_command = NULL;
2338 #endif
2340 for (index=1; index<dock->max_icons; index++)
2341 if (dock->icon_array[index] == icon)
2342 break;
2343 assert(index < dock->max_icons);
2344 dock->icon_array[index] = NULL;
2345 icon->yindex = -1;
2346 icon->xindex = -1;
2348 dock->icon_count--;
2350 /* if the dock is not attached to an application or
2351 * the the application did not set the approriate hints yet,
2352 * destroy the icon */
2353 if (!icon->running || !wApplicationOf(icon->main_window))
2354 wAppIconDestroy(icon);
2355 else {
2356 icon->icon->core->descriptor.handle_mousedown = appIconMouseDown;
2357 icon->icon->core->descriptor.handle_enternotify = NULL;
2358 icon->icon->core->descriptor.handle_leavenotify = NULL;
2359 icon->icon->core->descriptor.parent_type = WCLASS_APPICON;
2360 icon->icon->core->descriptor.parent = icon;
2362 ChangeStackingLevel(icon->icon->core, NORMAL_ICON_LEVEL);
2364 wAppIconPaint(icon);
2365 if (wPreferences.auto_arrange_icons) {
2366 wArrangeIcons(dock->screen_ptr, True);
2367 } else {
2368 WAppIcon *bla = wAppIconNextSibling(icon);
2370 if (bla) {
2371 SlideWindow(icon->icon->core->window, icon->x_pos, icon->y_pos,
2372 bla->x_pos, bla->y_pos);
2373 wAppIconMove(icon, bla->x_pos, bla->y_pos);
2377 if (dock->auto_collapse || dock->auto_raise_lower)
2378 clipLeave(dock);
2383 * returns the closest Dock slot index for the passed
2384 * coordinates.
2386 * Returns False if icon can't be docked.
2388 * Note: this function should NEVER alter ret_x or ret_y, unless it will
2389 * return True. -Dan
2391 Bool
2392 wDockSnapIcon(WDock *dock, WAppIcon *icon, int req_x, int req_y,
2393 int *ret_x, int *ret_y, int redocking)
2395 WScreen *scr = dock->screen_ptr;
2396 int dx, dy;
2397 int ex_x, ex_y;
2398 int i, offset = ICON_SIZE/2;
2399 WAppIcon *aicon = NULL;
2400 WAppIcon *nicon = NULL;
2401 int max_y_icons, max_x_icons;
2403 max_x_icons = scr->scr_width/ICON_SIZE;
2404 max_y_icons = scr->scr_height/ICON_SIZE-1;
2406 if (wPreferences.flags.noupdates)
2407 return False;
2409 dx = dock->x_pos;
2410 dy = dock->y_pos;
2412 /* if the dock is full */
2413 if (!redocking &&
2414 (dock->icon_count >= dock->max_icons)) {
2415 return False;
2418 /* exact position */
2419 if (req_y < dy)
2420 ex_y = (req_y - offset - dy)/ICON_SIZE;
2421 else
2422 ex_y = (req_y + offset - dy)/ICON_SIZE;
2424 if (req_x < dx)
2425 ex_x = (req_x - offset - dx)/ICON_SIZE;
2426 else
2427 ex_x = (req_x + offset - dx)/ICON_SIZE;
2429 /* check if the icon is outside the screen boundaries */
2430 if (dx + ex_x*ICON_SIZE < -ICON_SIZE+2 ||
2431 dx + ex_x*ICON_SIZE >= scr->scr_width-1 ||
2432 dy + ex_y*ICON_SIZE < -ICON_SIZE+2 ||
2433 dy + ex_y*ICON_SIZE >= scr->scr_height-1)
2434 return False;
2436 if (dock->type == WM_DOCK) {
2437 if (icon->dock != dock && ex_x != 0)
2438 return False;
2440 aicon = NULL;
2441 for (i=0; i<dock->max_icons; i++) {
2442 nicon = dock->icon_array[i];
2443 if (nicon && nicon->yindex == ex_y) {
2444 aicon = nicon;
2445 break;
2449 if (redocking) {
2450 int sig, done, closest;
2452 /* Possible cases when redocking:
2454 * icon dragged out of range of any slot -> false
2455 * icon dragged to range of free slot
2456 * icon dragged to range of same slot
2457 * icon dragged to range of different icon
2459 if (abs(ex_x) > DOCK_DETTACH_THRESHOLD)
2460 return False;
2462 if (ex_y>=0 && ex_y<=max_y_icons && (aicon==icon || !aicon)) {
2463 *ret_x = 0;
2464 *ret_y = ex_y;
2465 return True;
2468 /* start looking at the upper slot or lower? */
2469 if (ex_y*ICON_SIZE < (req_y + offset - dy))
2470 sig = 1;
2471 else
2472 sig = -1;
2474 closest = -1;
2475 done = 0;
2476 /* look for closest free slot */
2477 for (i=0; i<(DOCK_DETTACH_THRESHOLD+1)*2 && !done; i++) {
2478 int j;
2480 done = 1;
2481 closest = sig*(i/2) + ex_y;
2482 /* check if this slot is used */
2483 if (closest >= 0) {
2484 for (j = 0; j<dock->max_icons; j++) {
2485 if (dock->icon_array[j]
2486 && dock->icon_array[j]->yindex==closest) {
2487 /* slot is used by someone else */
2488 if (dock->icon_array[j]!=icon)
2489 done = 0;
2490 break;
2494 sig = -sig;
2496 if (done && closest >= 0 && closest <= max_y_icons &&
2497 ((ex_y >= closest && ex_y - closest < DOCK_DETTACH_THRESHOLD+1)
2499 (ex_y < closest && closest - ex_y <= DOCK_DETTACH_THRESHOLD+1))) {
2500 *ret_x = 0;
2501 *ret_y = closest;
2502 return True;
2504 } else { /* !redocking */
2506 /* if slot is free and the icon is close enough, return it */
2507 if (!aicon && ex_x == 0 && ex_y >= 0 && ex_y <= max_y_icons) {
2508 *ret_x = 0;
2509 *ret_y = ex_y;
2510 return True;
2513 } else { /* CLIP */
2514 int neighbours = 0;
2515 int start, stop, k;
2517 start = icon->omnipresent ? 0 : scr->current_workspace;
2518 stop = icon->omnipresent ? scr->workspace_count : start+1;
2520 aicon = NULL;
2521 for (k=start; k<stop; k++) {
2522 WDock *tmp = scr->workspaces[k]->clip;
2523 if (!tmp)
2524 continue;
2525 for (i=0; i<tmp->max_icons; i++) {
2526 nicon = tmp->icon_array[i];
2527 if (nicon && nicon->xindex == ex_x && nicon->yindex == ex_y) {
2528 aicon = nicon;
2529 break;
2532 if (aicon)
2533 break;
2535 for (k=start; k<stop; k++) {
2536 WDock *tmp = scr->workspaces[k]->clip;
2537 if (!tmp)
2538 continue;
2539 for (i=0; i<tmp->max_icons; i++) {
2540 nicon = tmp->icon_array[i];
2541 if (nicon && nicon != icon && /* Icon can't be it's own neighbour */
2542 (abs(nicon->xindex - ex_x) <= CLIP_ATTACH_VICINITY &&
2543 abs(nicon->yindex - ex_y) <= CLIP_ATTACH_VICINITY)) {
2544 neighbours = 1;
2545 break;
2548 if (neighbours)
2549 break;
2552 if (neighbours && (aicon==NULL || (redocking && aicon == icon))) {
2553 *ret_x = ex_x;
2554 *ret_y = ex_y;
2555 return True;
2558 return False;
2561 #define MIN(x, y) ((x) > (y) ? (y) : (x))
2562 #define MAX(x, y) ((x) < (y) ? (y) : (x))
2564 #define ON_SCREEN(x, y, sx, ex, sy, ey) \
2565 ((((x)+ICON_SIZE/2) >= (sx)) && (((y)+ICON_SIZE/2) >= (sy)) && \
2566 (((x) + (ICON_SIZE/2)) <= (ex)) && (((y) + (ICON_SIZE/2)) <= ey))
2570 * returns true if it can find a free slot in the dock,
2571 * in which case it changes x_pos and y_pos accordingly.
2572 * Else returns false.
2574 Bool
2575 wDockFindFreeSlot(WDock *dock, int *x_pos, int *y_pos)
2577 WScreen *scr = dock->screen_ptr;
2578 WAppIcon *btn;
2579 WAppIconChain *chain;
2580 unsigned char *slot_map;
2581 int mwidth;
2582 int r;
2583 int x, y;
2584 int i, done = False;
2585 int corner;
2586 int sx=0, sy=0, ex=scr->scr_width, ey=scr->scr_height;
2587 int extra_count=0;
2589 if (dock->type == WM_CLIP &&
2590 dock != scr->workspaces[scr->current_workspace]->clip)
2591 extra_count = scr->global_icon_count;
2593 /* if the dock is full */
2594 if (dock->icon_count+extra_count >= dock->max_icons) {
2595 return False;
2598 if (!wPreferences.flags.nodock && scr->dock) {
2599 if (scr->dock->on_right_side)
2600 ex -= ICON_SIZE + DOCK_EXTRA_SPACE;
2601 else
2602 sx += ICON_SIZE + DOCK_EXTRA_SPACE;
2605 if (ex < dock->x_pos)
2606 ex = dock->x_pos;
2607 if (sx > dock->x_pos+ICON_SIZE)
2608 sx = dock->x_pos+ICON_SIZE;
2609 #define C_NONE 0
2610 #define C_NW 1
2611 #define C_NE 2
2612 #define C_SW 3
2613 #define C_SE 4
2615 /* check if clip is in a corner */
2616 if (dock->type==WM_CLIP) {
2617 if (dock->x_pos < 1 && dock->y_pos < 1)
2618 corner = C_NE;
2619 else if (dock->x_pos < 1 && dock->y_pos >= (ey-ICON_SIZE))
2620 corner = C_SE;
2621 else if (dock->x_pos >= (ex-ICON_SIZE)&& dock->y_pos >= (ey-ICON_SIZE))
2622 corner = C_SW;
2623 else if (dock->x_pos >= (ex-ICON_SIZE) && dock->y_pos < 1)
2624 corner = C_NW;
2625 else
2626 corner = C_NONE;
2627 } else
2628 corner = C_NONE;
2630 /* If the clip is in the corner, use only slots that are in the border
2631 * of the screen */
2632 if (corner!=C_NONE) {
2633 char *hmap, *vmap;
2634 int hcount, vcount;
2636 hcount = MIN(dock->max_icons, scr->scr_width/ICON_SIZE);
2637 vcount = MIN(dock->max_icons, scr->scr_height/ICON_SIZE);
2638 hmap = wmalloc(hcount+1);
2639 memset(hmap, 0, hcount+1);
2640 vmap = wmalloc(vcount+1);
2641 memset(vmap, 0, vcount+1);
2643 /* mark used positions */
2644 switch (corner) {
2645 case C_NE:
2646 for (i=0; i<dock->max_icons; i++) {
2647 btn = dock->icon_array[i];
2648 if (!btn)
2649 continue;
2651 if (btn->xindex==0 && btn->yindex > 0 && btn->yindex < vcount)
2652 vmap[btn->yindex] = 1;
2653 else if (btn->yindex==0 && btn->xindex>0 && btn->xindex<hcount)
2654 hmap[btn->xindex] = 1;
2656 for (chain=scr->global_icons; chain!=NULL; chain=chain->next) {
2657 btn = chain->aicon;
2658 if (btn->xindex==0 && btn->yindex > 0 && btn->yindex < vcount)
2659 vmap[btn->yindex] = 1;
2660 else if (btn->yindex==0 && btn->xindex>0 && btn->xindex<hcount)
2661 hmap[btn->xindex] = 1;
2663 break;
2664 case C_NW:
2665 for (i=0; i<dock->max_icons; i++) {
2666 btn = dock->icon_array[i];
2667 if (!btn)
2668 continue;
2670 if (btn->xindex==0 && btn->yindex > 0 && btn->yindex < vcount)
2671 vmap[btn->yindex] = 1;
2672 else if (btn->yindex==0 && btn->xindex<0 &&btn->xindex>-hcount)
2673 hmap[-btn->xindex] = 1;
2675 for (chain=scr->global_icons; chain!=NULL; chain=chain->next) {
2676 btn = chain->aicon;
2677 if (btn->xindex==0 && btn->yindex > 0 && btn->yindex < vcount)
2678 vmap[btn->yindex] = 1;
2679 else if (btn->yindex==0 && btn->xindex<0 &&btn->xindex>-hcount)
2680 hmap[-btn->xindex] = 1;
2682 break;
2683 case C_SE:
2684 for (i=0; i<dock->max_icons; i++) {
2685 btn = dock->icon_array[i];
2686 if (!btn)
2687 continue;
2689 if (btn->xindex==0 && btn->yindex < 0 && btn->yindex > -vcount)
2690 vmap[-btn->yindex] = 1;
2691 else if (btn->yindex==0 && btn->xindex>0 && btn->xindex<hcount)
2692 hmap[btn->xindex] = 1;
2694 for (chain=scr->global_icons; chain!=NULL; chain=chain->next) {
2695 btn = chain->aicon;
2696 if (btn->xindex==0 && btn->yindex < 0 && btn->yindex > -vcount)
2697 vmap[-btn->yindex] = 1;
2698 else if (btn->yindex==0 && btn->xindex>0 && btn->xindex<hcount)
2699 hmap[btn->xindex] = 1;
2701 break;
2702 case C_SW:
2703 default:
2704 for (i=0; i<dock->max_icons; i++) {
2705 btn = dock->icon_array[i];
2706 if (!btn)
2707 continue;
2709 if (btn->xindex==0 && btn->yindex < 0 && btn->yindex > -vcount)
2710 vmap[-btn->yindex] = 1;
2711 else if (btn->yindex==0 && btn->xindex<0 &&btn->xindex>-hcount)
2712 hmap[-btn->xindex] = 1;
2714 for (chain=scr->global_icons; chain!=NULL; chain=chain->next) {
2715 btn = chain->aicon;
2716 if (btn->xindex==0 && btn->yindex < 0 && btn->yindex > -vcount)
2717 vmap[-btn->yindex] = 1;
2718 else if (btn->yindex==0 && btn->xindex<0 &&btn->xindex>-hcount)
2719 hmap[-btn->xindex] = 1;
2722 x=0; y=0;
2723 done = 0;
2724 /* search a vacant slot */
2725 for (i=1; i<MAX(vcount, hcount); i++) {
2726 if (i < vcount && vmap[i]==0) {
2727 /* found a slot */
2728 x = 0;
2729 y = i;
2730 done = 1;
2731 break;
2732 } else if (i < hcount && hmap[i]==0) {
2733 /* found a slot */
2734 x = i;
2735 y = 0;
2736 done = 1;
2737 break;
2740 wfree(vmap);
2741 wfree(hmap);
2742 /* If found a slot, translate and return */
2743 if (done) {
2744 if (corner==C_NW || corner==C_NE) {
2745 *y_pos = y;
2746 } else {
2747 *y_pos = -y;
2749 if (corner==C_NE || corner==C_SE) {
2750 *x_pos = x;
2751 } else {
2752 *x_pos = -x;
2754 return True;
2756 /* else, try to find a slot somewhere else */
2759 /* a map of mwidth x mwidth would be enough if we allowed icons to be
2760 * placed outside of screen */
2761 mwidth = (int)ceil(sqrt(dock->max_icons));
2763 /* In the worst case (the clip is in the corner of the screen),
2764 * the amount of icons that fit in the clip is smaller.
2765 * Double the map to get a safe value.
2767 mwidth += mwidth;
2769 r = (mwidth-1)/2;
2771 slot_map = wmalloc(mwidth*mwidth);
2772 memset(slot_map, 0, mwidth*mwidth);
2774 #define XY2OFS(x,y) (MAX(abs(x),abs(y)) > r) ? 0 : (((y)+r)*(mwidth)+(x)+r)
2776 /* mark used slots in the map. If the slot falls outside the map
2777 * (for example, when all icons are placed in line), ignore them. */
2778 for (i=0; i<dock->max_icons; i++) {
2779 btn = dock->icon_array[i];
2780 if (btn)
2781 slot_map[XY2OFS(btn->xindex, btn->yindex)] = 1;
2783 for (chain=scr->global_icons; chain!=NULL; chain=chain->next) {
2784 slot_map[XY2OFS(chain->aicon->xindex, chain->aicon->yindex)] = 1;
2786 /* Find closest slot from the center that is free by scanning the
2787 * map from the center to outward in circular passes.
2788 * This will not result in a neat layout, but will be optimal
2789 * in the sense that there will not be holes left.
2791 done = 0;
2792 for (i = 1; i <= r && !done; i++) {
2793 int tx, ty;
2795 /* top and bottom parts of the ring */
2796 for (x = -i; x <= i && !done; x++) {
2797 tx = dock->x_pos + x*ICON_SIZE;
2798 y = -i;
2799 ty = dock->y_pos + y*ICON_SIZE;
2800 if (slot_map[XY2OFS(x,y)]==0
2801 && ON_SCREEN(tx, ty, sx, ex, sy, ey)) {
2802 *x_pos = x;
2803 *y_pos = y;
2804 done = 1;
2805 break;
2807 y = i;
2808 ty = dock->y_pos + y*ICON_SIZE;
2809 if (slot_map[XY2OFS(x,y)]==0
2810 && ON_SCREEN(tx, ty, sx, ex, sy, ey)) {
2811 *x_pos = x;
2812 *y_pos = y;
2813 done = 1;
2814 break;
2817 /* left and right parts of the ring */
2818 for (y = -i+1; y <= i-1; y++) {
2819 ty = dock->y_pos + y*ICON_SIZE;
2820 x = -i;
2821 tx = dock->x_pos + x*ICON_SIZE;
2822 if (slot_map[XY2OFS(x,y)]==0
2823 && ON_SCREEN(tx, ty, sx, ex, sy, ey)) {
2824 *x_pos = x;
2825 *y_pos = y;
2826 done = 1;
2827 break;
2829 x = i;
2830 tx = dock->x_pos + x*ICON_SIZE;
2831 if (slot_map[XY2OFS(x,y)]==0
2832 && ON_SCREEN(tx, ty, sx, ex, sy, ey)) {
2833 *x_pos = x;
2834 *y_pos = y;
2835 done = 1;
2836 break;
2840 wfree(slot_map);
2841 #undef XY2OFS
2842 return done;
2846 static void
2847 moveDock(WDock *dock, int new_x, int new_y)
2849 WAppIcon *btn;
2850 int i;
2852 dock->x_pos = new_x;
2853 dock->y_pos = new_y;
2854 for (i=0; i<dock->max_icons; i++) {
2855 btn = dock->icon_array[i];
2856 if (btn) {
2857 btn->x_pos = new_x + btn->xindex*ICON_SIZE;
2858 btn->y_pos = new_y + btn->yindex*ICON_SIZE;
2859 XMoveWindow(dpy, btn->icon->core->window, btn->x_pos, btn->y_pos);
2865 static void
2866 swapDock(WDock *dock)
2868 WScreen *scr = dock->screen_ptr;
2869 WAppIcon *btn;
2870 int x, i;
2873 if (dock->on_right_side) {
2874 x = dock->x_pos = scr->scr_width - ICON_SIZE - DOCK_EXTRA_SPACE;
2875 } else {
2876 x = dock->x_pos = DOCK_EXTRA_SPACE;
2879 for (i=0; i<dock->max_icons; i++) {
2880 btn = dock->icon_array[i];
2881 if (btn) {
2882 btn->x_pos = x;
2883 XMoveWindow(dpy, btn->icon->core->window, btn->x_pos, btn->y_pos);
2887 wScreenUpdateUsableArea(scr);
2891 static pid_t
2892 execCommand(WAppIcon *btn, char *command, WSavedState *state)
2894 WScreen *scr = btn->icon->core->screen_ptr;
2895 pid_t pid;
2896 char **argv;
2897 int argc;
2898 char *cmdline;
2900 cmdline = ExpandOptions(scr, command);
2902 if (scr->flags.dnd_data_convertion_status || !cmdline) {
2903 if (cmdline)
2904 wfree(cmdline);
2905 if (state)
2906 wfree(state);
2907 return 0;
2910 wtokensplit(cmdline, &argv, &argc);
2912 if (argv==NULL) {
2913 if (cmdline)
2914 wfree(cmdline);
2915 if (state)
2916 wfree(state);
2917 return 0;
2920 if ((pid=fork())==0) {
2921 char **args;
2922 int i;
2924 SetupEnvironment(scr);
2926 #ifdef HAVE_SETPGID
2927 setpgid(0, 0);
2928 #endif
2930 args = malloc(sizeof(char*)*(argc+1));
2931 if (!args)
2932 exit(111);
2933 for (i=0; i<argc; i++) {
2934 args[i] = argv[i];
2936 args[argc] = NULL;
2937 execvp(argv[0], args);
2938 exit(111);
2940 while (argc > 0)
2941 wfree(argv[--argc]);
2942 wfree(argv);
2944 if (pid > 0) {
2945 if (!state) {
2946 state = wmalloc(sizeof(WSavedState));
2947 memset(state, 0, sizeof(WSavedState));
2948 state->hidden = -1;
2949 state->miniaturized = -1;
2950 state->shaded = -1;
2951 if (btn->dock==scr->dock || btn->omnipresent)
2952 state->workspace = -1;
2953 else
2954 state->workspace = scr->current_workspace;
2956 wWindowAddSavedState(btn->wm_instance, btn->wm_class, cmdline, pid,
2957 state);
2958 wAddDeathHandler(pid, (WDeathHandler*)trackDeadProcess,
2959 btn->dock);
2960 } else if (state) {
2961 wfree(state);
2963 wfree(cmdline);
2964 return pid;
2968 void
2969 wDockHideIcons(WDock *dock)
2971 int i;
2973 if (dock==NULL)
2974 return;
2976 for (i=1; i<dock->max_icons; i++) {
2977 if (dock->icon_array[i])
2978 XUnmapWindow(dpy, dock->icon_array[i]->icon->core->window);
2980 dock->mapped = 0;
2982 dockIconPaint(dock->icon_array[0]);
2986 void
2987 wDockShowIcons(WDock *dock)
2989 int i, newlevel;
2990 WAppIcon *btn;
2992 if (dock==NULL)
2993 return;
2995 btn = dock->icon_array[0];
2996 moveDock(dock, btn->x_pos, btn->y_pos);
2998 newlevel = dock->lowered ? WMNormalLevel : WMDockLevel;
2999 ChangeStackingLevel(btn->icon->core, newlevel);
3001 for (i=1; i<dock->max_icons; i++) {
3002 if (dock->icon_array[i]) {
3003 MoveInStackListAbove(dock->icon_array[i]->icon->core,
3004 btn->icon->core);
3005 break;
3009 if (!dock->collapsed) {
3010 for (i=1; i<dock->max_icons; i++) {
3011 if (dock->icon_array[i]) {
3012 XMapWindow(dpy, dock->icon_array[i]->icon->core->window);
3016 dock->mapped = 1;
3018 dockIconPaint(btn);
3022 void
3023 wDockLower(WDock *dock)
3025 int i;
3027 for (i=0; i<dock->max_icons; i++) {
3028 if (dock->icon_array[i])
3029 wLowerFrame(dock->icon_array[i]->icon->core);
3034 void
3035 wDockRaise(WDock *dock)
3037 int i;
3039 for (i=dock->max_icons-1; i>=0; i--) {
3040 if (dock->icon_array[i])
3041 wRaiseFrame(dock->icon_array[i]->icon->core);
3046 void
3047 wDockRaiseLower(WDock *dock)
3049 if (!dock->icon_array[0]->icon->core->stacking->above
3050 ||(dock->icon_array[0]->icon->core->stacking->window_level
3051 !=dock->icon_array[0]->icon->core->stacking->above->stacking->window_level))
3052 wDockLower(dock);
3053 else
3054 wDockRaise(dock);
3058 void
3059 wDockFinishLaunch(WDock *dock, WAppIcon *icon)
3061 icon->launching = 0;
3062 icon->relaunching = 0;
3063 dockIconPaint(icon);
3067 WAppIcon*
3068 wDockFindIconForWindow(WDock *dock, Window window)
3070 WAppIcon *icon;
3071 int i;
3073 for (i=0; i<dock->max_icons; i++) {
3074 icon = dock->icon_array[i];
3075 if (icon && icon->main_window == window)
3076 return icon;
3078 return NULL;
3082 void
3083 wDockTrackWindowLaunch(WDock *dock, Window window)
3085 WAppIcon *icon;
3086 char *wm_class, *wm_instance;
3087 int i;
3088 Bool firstPass = True;
3089 Bool found = False;
3090 char *command = NULL;
3093 int argc;
3094 char **argv;
3096 if (XGetCommand(dpy, window, &argv, &argc)) {
3097 if (argc > 0 && argv != NULL)
3098 command = wtokenjoin(argv,argc);
3099 if (argv) {
3100 XFreeStringList(argv);
3105 if (!PropGetWMClass(window, &wm_class, &wm_instance) ||
3106 (!wm_class && !wm_instance))
3107 return;
3109 retry:
3110 for (i=0; i<dock->max_icons; i++) {
3111 icon = dock->icon_array[i];
3112 if (!icon)
3113 continue;
3115 /* app is already attached to icon */
3116 if (icon->main_window == window) {
3117 found = True;
3118 break;
3121 if ((icon->wm_instance || icon->wm_class)
3122 && (icon->launching
3123 || (dock->screen_ptr->flags.startup && !icon->running))) {
3125 if (icon->wm_instance && wm_instance &&
3126 strcmp(icon->wm_instance, wm_instance)!=0) {
3127 continue;
3129 if (icon->wm_class && wm_class &&
3130 strcmp(icon->wm_class, wm_class)!=0) {
3131 continue;
3133 if (firstPass && command && strcmp(icon->command, command)!=0) {
3134 continue;
3137 if (!icon->relaunching) {
3138 WApplication *wapp;
3140 /* Possibly an application that was docked with dockit,
3141 * but the user did not update WMState to indicate that
3142 * it was docked by force */
3143 wapp = wApplicationOf(window);
3144 if (!wapp) {
3145 icon->forced_dock = 1;
3146 icon->running = 0;
3148 if (!icon->forced_dock)
3149 icon->main_window = window;
3152 found = True;
3153 wDockFinishLaunch(dock, icon);
3154 break;
3158 if (firstPass && !found) {
3159 firstPass = False;
3160 goto retry;
3163 if (command)
3164 wfree(command);
3166 if (wm_class)
3167 XFree(wm_class);
3168 if (wm_instance)
3169 XFree(wm_instance);
3174 void
3175 wClipUpdateForWorkspaceChange(WScreen *scr, int workspace)
3177 if (!wPreferences.flags.noclip) {
3178 scr->clip_icon->dock = scr->workspaces[workspace]->clip;
3179 if (scr->current_workspace != workspace) {
3180 WDock *old_clip = scr->workspaces[scr->current_workspace]->clip;
3181 WAppIconChain *chain = scr->global_icons;
3183 while (chain) {
3184 moveIconBetweenDocks(chain->aicon->dock,
3185 scr->workspaces[workspace]->clip,
3186 chain->aicon, chain->aicon->xindex,
3187 chain->aicon->yindex);
3188 if (scr->workspaces[workspace]->clip->collapsed)
3189 XUnmapWindow(dpy, chain->aicon->icon->core->window);
3190 chain = chain->next;
3193 wDockHideIcons(old_clip);
3194 if (old_clip->auto_raise_lower) {
3195 if (old_clip->auto_raise_magic) {
3196 WMDeleteTimerHandler(old_clip->auto_raise_magic);
3197 old_clip->auto_raise_magic = NULL;
3199 wDockLower(old_clip);
3201 if (old_clip->auto_collapse) {
3202 if (old_clip->auto_expand_magic) {
3203 WMDeleteTimerHandler(old_clip->auto_expand_magic);
3204 old_clip->auto_expand_magic = NULL;
3206 old_clip->collapsed = 1;
3208 wDockShowIcons(scr->workspaces[workspace]->clip);
3210 if (scr->flags.clip_balloon_mapped)
3211 showClipBalloon(scr->clip_icon->dock, workspace);
3217 static void
3218 trackDeadProcess(pid_t pid, unsigned char status, WDock *dock)
3220 WAppIcon *icon;
3221 int i;
3223 for (i=0; i<dock->max_icons; i++) {
3224 icon = dock->icon_array[i];
3225 if (!icon)
3226 continue;
3228 if (icon->launching && icon->pid == pid) {
3229 if (!icon->relaunching) {
3230 icon->running = 0;
3231 icon->main_window = None;
3233 wDockFinishLaunch(dock, icon);
3234 icon->pid = 0;
3235 if (status==111) {
3236 char msg[PATH_MAX];
3237 #ifdef OFFIX_DND
3238 sprintf(msg, _("Could not execute command \"%s\""),
3239 icon->drop_launch && icon->dnd_command
3240 ? icon->dnd_command : icon->command);
3241 #else
3242 sprintf(msg, _("Could not execute command \"%s\""),
3243 icon->command);
3244 #endif
3245 wMessageDialog(dock->screen_ptr, _("Error"), msg,
3246 _("OK"), NULL, NULL);
3248 break;
3254 static void
3255 toggleLowered(WDock *dock)
3257 WAppIcon *tmp;
3258 int newlevel, i;
3260 /* lower/raise Dock */
3261 if (!dock->lowered) {
3262 newlevel = WMNormalLevel;
3263 dock->lowered = 1;
3264 } else {
3265 newlevel = WMDockLevel;
3266 dock->lowered = 0;
3269 for (i=0; i<dock->max_icons; i++) {
3270 tmp = dock->icon_array[i];
3271 if (!tmp)
3272 continue;
3274 ChangeStackingLevel(tmp->icon->core, newlevel);
3275 if (dock->lowered)
3276 wLowerFrame(tmp->icon->core);
3279 if (dock->type == WM_DOCK)
3280 wScreenUpdateUsableArea(dock->screen_ptr);
3284 static void
3285 toggleCollapsed(WDock *dock)
3287 if (dock->collapsed) {
3288 dock->collapsed = 0;
3289 wDockShowIcons(dock);
3291 else {
3292 dock->collapsed = 1;
3293 wDockHideIcons(dock);
3298 static void
3299 openDockMenu(WDock *dock, WAppIcon *aicon, XEvent *event)
3301 WScreen *scr = dock->screen_ptr;
3302 WObjDescriptor *desc;
3303 WMenuEntry *entry;
3304 WApplication *wapp = NULL;
3305 int index = 0;
3306 int x_pos;
3307 int n_selected;
3308 int appIsRunning = aicon->running && aicon->icon && aicon->icon->owner;
3310 if (dock->type == WM_DOCK) {
3311 /* keep on top */
3312 entry = dock->menu->entries[index];
3313 entry->flags.indicator_on = !dock->lowered;
3314 entry->clientdata = dock;
3315 dock->menu->flags.realized = 0;
3316 } else {
3317 /* clip options */
3318 if (scr->clip_options)
3319 updateClipOptionsMenu(scr->clip_options, dock);
3321 n_selected = numberOfSelectedIcons(dock);
3323 /* Rename Workspace */
3324 entry = dock->menu->entries[++index];
3325 if (aicon == scr->clip_icon) {
3326 entry->callback = renameCallback;
3327 entry->clientdata = dock;
3328 entry->flags.indicator = 0;
3329 entry->text = _("Rename Workspace");
3330 } else {
3331 entry->callback = omnipresentCallback;
3332 entry->clientdata = aicon;
3333 if (n_selected > 0) {
3334 entry->flags.indicator = 0;
3335 entry->text = _("Toggle Omnipresent");
3336 } else {
3337 entry->flags.indicator = 1;
3338 entry->flags.indicator_on = aicon->omnipresent;
3339 entry->flags.indicator_type = MI_CHECK;
3340 entry->text = _("Omnipresent");
3344 /* select/unselect icon */
3345 entry = dock->menu->entries[++index];
3346 entry->clientdata = aicon;
3347 entry->flags.indicator_on = aicon->icon->selected;
3348 wMenuSetEnabled(dock->menu, index, aicon!=scr->clip_icon);
3350 /* select/unselect all icons */
3351 entry = dock->menu->entries[++index];
3352 entry->clientdata = aicon;
3353 if (n_selected > 0)
3354 entry->text = _("Unselect All Icons");
3355 else
3356 entry->text = _("Select All Icons");
3357 wMenuSetEnabled(dock->menu, index, dock->icon_count > 1);
3359 /* keep icon(s) */
3360 entry = dock->menu->entries[++index];
3361 entry->clientdata = aicon;
3362 if (n_selected > 1)
3363 entry->text = _("Keep Icons");
3364 else
3365 entry->text = _("Keep Icon");
3366 wMenuSetEnabled(dock->menu, index, dock->icon_count > 1);
3368 /* this is the workspace submenu part */
3369 entry = dock->menu->entries[++index];
3370 if (n_selected > 1)
3371 entry->text = _("Move Icons To");
3372 else
3373 entry->text = _("Move Icon To");
3374 if (scr->clip_submenu)
3375 updateWorkspaceMenu(scr->clip_submenu, aicon);
3376 wMenuSetEnabled(dock->menu, index, !aicon->omnipresent);
3378 /* remove icon(s) */
3379 entry = dock->menu->entries[++index];
3380 entry->clientdata = aicon;
3381 if (n_selected > 1)
3382 entry->text = _("Remove Icons");
3383 else
3384 entry->text = _("Remove Icon");
3385 wMenuSetEnabled(dock->menu, index, dock->icon_count > 1);
3387 /* attract icon(s) */
3388 entry = dock->menu->entries[++index];
3389 entry->clientdata = aicon;
3391 dock->menu->flags.realized = 0;
3392 wMenuRealize(dock->menu);
3396 if (aicon->icon->owner) {
3397 wapp = wApplicationOf(aicon->icon->owner->main_window);
3398 } else {
3399 wapp = NULL;
3402 /* launch */
3403 entry = dock->menu->entries[++index];
3404 entry->clientdata = aicon;
3405 wMenuSetEnabled(dock->menu, index, aicon->command!=NULL);
3407 /* unhide here */
3408 entry = dock->menu->entries[++index];
3409 entry->clientdata = aicon;
3410 if (wapp && wapp->flags.hidden) {
3411 entry->text = _("Unhide Here");
3412 } else {
3413 entry->text = _("Bring Here");
3415 wMenuSetEnabled(dock->menu, index, appIsRunning);
3417 /* hide */
3418 entry = dock->menu->entries[++index];
3419 entry->clientdata = aicon;
3420 if (wapp && wapp->flags.hidden) {
3421 entry->text = _("Unhide");
3422 } else {
3423 entry->text = _("Hide");
3425 wMenuSetEnabled(dock->menu, index, appIsRunning);
3427 /* settings */
3428 entry = dock->menu->entries[++index];
3429 entry->clientdata = aicon;
3430 wMenuSetEnabled(dock->menu, index, !aicon->editing
3431 && !wPreferences.flags.noupdates);
3433 /* kill */
3434 entry = dock->menu->entries[++index];
3435 entry->clientdata = aicon;
3436 wMenuSetEnabled(dock->menu, index, appIsRunning);
3438 if (!dock->menu->flags.realized)
3439 wMenuRealize(dock->menu);
3441 if (dock->type == WM_CLIP) {
3442 x_pos = event->xbutton.x_root+2;
3443 } else {
3444 x_pos = dock->on_right_side ?
3445 scr->scr_width - dock->menu->frame->core->width - 2 : 0;
3448 wMenuMapAt(dock->menu, x_pos, event->xbutton.y_root+2, False);
3450 /* allow drag select */
3451 event->xany.send_event = True;
3452 desc = &dock->menu->menu->descriptor;
3453 (*desc->handle_mousedown)(desc, event);
3457 static void
3458 openClipWorkspaceMenu(WScreen *scr, int x, int y)
3460 if (!scr->clip_ws_menu) {
3461 scr->clip_ws_menu = wWorkspaceMenuMake(scr, False);
3463 wWorkspaceMenuUpdate(scr, scr->clip_ws_menu);
3464 wMenuMapAt(scr->clip_ws_menu, x, y, False);
3468 /******************************************************************/
3469 static void
3470 iconDblClick(WObjDescriptor *desc, XEvent *event)
3472 WAppIcon *btn = desc->parent;
3473 WDock *dock = btn->dock;
3474 WApplication *wapp = NULL;
3475 int unhideHere = 0;
3477 if (btn->icon->owner && !(event->xbutton.state & ControlMask)) {
3478 wapp = wApplicationOf(btn->icon->owner->main_window);
3480 assert(wapp!=NULL);
3482 unhideHere = (event->xbutton.state & ShiftMask);
3484 /* go to the last workspace that the user worked on the app */
3485 if (wapp->last_workspace != dock->screen_ptr->current_workspace
3486 && !unhideHere) {
3487 wWorkspaceChange(dock->screen_ptr, wapp->last_workspace);
3490 wUnhideApplication(wapp, event->xbutton.button==Button2,
3491 unhideHere);
3493 if (event->xbutton.state & MOD_MASK) {
3494 wHideOtherApplications(btn->icon->owner);
3496 } else {
3497 if (event->xbutton.button==Button1) {
3499 if (event->xbutton.state & MOD_MASK) {
3500 /* raise/lower dock */
3501 toggleLowered(dock);
3502 } else if (btn == dock->screen_ptr->clip_icon) {
3503 if (getClipButton(event->xbutton.x, event->xbutton.y)==CLIP_IDLE)
3504 toggleCollapsed(dock);
3505 else
3506 handleClipChangeWorkspace(dock->screen_ptr, event);
3507 } else if (btn->command) {
3508 if (!btn->launching &&
3509 (!btn->running || (event->xbutton.state & ControlMask))) {
3510 launchDockedApplication(btn);
3512 } else if (btn->xindex == 0 && btn->yindex == 0
3513 && btn->dock->type == WM_DOCK) {
3515 wShowGNUstepPanel(dock->screen_ptr);
3522 static void
3523 handleDockMove(WDock *dock, WAppIcon *aicon, XEvent *event)
3525 WScreen *scr = dock->screen_ptr;
3526 int ofs_x=event->xbutton.x, ofs_y=event->xbutton.y;
3527 int x, y;
3528 XEvent ev;
3529 int grabbed = 0, swapped = 0, done;
3530 Pixmap ghost = None;
3531 int superfluous = wPreferences.superfluous; /* we catch it to avoid problems */
3533 #ifdef DEBUG
3534 puts("moving dock");
3535 #endif
3536 if (XGrabPointer(dpy, aicon->icon->core->window, True, ButtonMotionMask
3537 |ButtonReleaseMask|ButtonPressMask, GrabModeAsync,
3538 GrabModeAsync, None, None, CurrentTime) !=GrabSuccess) {
3539 wwarning("pointer grab failed for dock move");
3541 y = 0;
3542 for (x=0; x<dock->max_icons; x++) {
3543 if (dock->icon_array[x]!=NULL &&
3544 dock->icon_array[x]->yindex > y)
3545 y = dock->icon_array[x]->yindex;
3547 y++;
3548 XResizeWindow(dpy, scr->dock_shadow, ICON_SIZE, ICON_SIZE*y);
3550 done = 0;
3551 while (!done) {
3552 WMMaskEvent(dpy, PointerMotionMask|ButtonReleaseMask|ButtonPressMask
3553 |ButtonMotionMask|ExposureMask, &ev);
3554 switch (ev.type) {
3555 case Expose:
3556 WMHandleEvent(&ev);
3557 break;
3559 case MotionNotify:
3560 if (!grabbed) {
3561 if (abs(ofs_x-ev.xmotion.x)>=MOVE_THRESHOLD
3562 || abs(ofs_y-ev.xmotion.y)>=MOVE_THRESHOLD) {
3563 XChangeActivePointerGrab(dpy, ButtonMotionMask
3564 |ButtonReleaseMask|ButtonPressMask,
3565 wCursor[WCUR_MOVE], CurrentTime);
3566 grabbed=1;
3568 break;
3570 if (dock->type == WM_CLIP) {
3571 if (ev.xmotion.x_root - ofs_x < 0) {
3572 x = 0;
3573 } else if (ev.xmotion.x_root - ofs_x + ICON_SIZE >
3574 scr->scr_width) {
3575 x = scr->scr_width - ICON_SIZE;
3576 } else {
3577 x = ev.xmotion.x_root - ofs_x;
3579 if (ev.xmotion.y_root - ofs_y < 0) {
3580 y = 0;
3581 } else if (ev.xmotion.y_root - ofs_y + ICON_SIZE >
3582 scr->scr_height) {
3583 y = scr->scr_height - ICON_SIZE;
3584 } else {
3585 y = ev.xmotion.y_root - ofs_y;
3587 moveDock(dock, x, y);
3588 } else {
3589 /* move vertically if pointer is inside the dock*/
3590 if ((dock->on_right_side &&
3591 ev.xmotion.x_root >= dock->x_pos - ICON_SIZE)
3592 || (!dock->on_right_side &&
3593 ev.xmotion.x_root <= dock->x_pos + ICON_SIZE*2)) {
3595 if (ev.xmotion.y_root - ofs_y < 0) {
3596 y = 0;
3597 } else if (ev.xmotion.y_root - ofs_y + ICON_SIZE >
3598 scr->scr_height) {
3599 y = scr->scr_height - ICON_SIZE;
3600 } else {
3601 y = ev.xmotion.y_root - ofs_y;
3603 moveDock(dock, dock->x_pos, y);
3605 /* move horizontally to change sides */
3606 x = ev.xmotion.x_root - ofs_x;
3607 if (!dock->on_right_side) {
3609 /* is on left */
3611 if (ev.xmotion.x_root > dock->x_pos + ICON_SIZE*2) {
3612 XMoveWindow(dpy, scr->dock_shadow, scr->scr_width-ICON_SIZE
3613 -DOCK_EXTRA_SPACE-1, dock->y_pos);
3614 if (superfluous && ghost==None) {
3615 ghost = MakeGhostDock(dock, dock->x_pos,
3616 scr->scr_width-ICON_SIZE
3617 -DOCK_EXTRA_SPACE-1,
3618 dock->y_pos);
3619 XSetWindowBackgroundPixmap(dpy, scr->dock_shadow,
3620 ghost);
3621 XClearWindow(dpy, scr->dock_shadow);
3623 XMapRaised(dpy, scr->dock_shadow);
3624 swapped = 1;
3625 } else {
3626 if (superfluous && ghost!=None) {
3627 XFreePixmap(dpy, ghost);
3628 ghost = None;
3630 XUnmapWindow(dpy, scr->dock_shadow);
3631 swapped = 0;
3633 } else {
3634 /* is on right */
3635 if (ev.xmotion.x_root < dock->x_pos - ICON_SIZE) {
3636 XMoveWindow(dpy, scr->dock_shadow,
3637 DOCK_EXTRA_SPACE, dock->y_pos);
3638 if (superfluous && ghost==None) {
3639 ghost = MakeGhostDock(dock, dock->x_pos,
3640 DOCK_EXTRA_SPACE, dock->y_pos);
3641 XSetWindowBackgroundPixmap(dpy, scr->dock_shadow,
3642 ghost);
3643 XClearWindow(dpy, scr->dock_shadow);
3645 XMapRaised(dpy, scr->dock_shadow);
3646 swapped = -1;
3647 } else {
3648 XUnmapWindow(dpy, scr->dock_shadow);
3649 swapped = 0;
3650 if (superfluous && ghost!=None) {
3651 XFreePixmap(dpy, ghost);
3652 ghost = None;
3657 break;
3659 case ButtonPress:
3660 break;
3662 case ButtonRelease:
3663 if (ev.xbutton.button != event->xbutton.button)
3664 break;
3665 XUngrabPointer(dpy, CurrentTime);
3666 XUnmapWindow(dpy, scr->dock_shadow);
3667 XResizeWindow(dpy, scr->dock_shadow, ICON_SIZE, ICON_SIZE);
3668 if (dock->type == WM_DOCK) {
3669 if (swapped!=0) {
3670 if (swapped>0)
3671 dock->on_right_side = 1;
3672 else
3673 dock->on_right_side = 0;
3674 swapDock(dock);
3675 wArrangeIcons(scr, False);
3678 done = 1;
3679 break;
3682 if (superfluous) {
3683 if (ghost!=None)
3684 XFreePixmap(dpy, ghost);
3685 XSetWindowBackground(dpy, scr->dock_shadow, scr->white_pixel);
3687 #ifdef DEBUG
3688 puts("End dock move");
3689 #endif
3694 static void
3695 handleIconMove(WDock *dock, WAppIcon *aicon, XEvent *event)
3697 WScreen *scr = dock->screen_ptr;
3698 Window wins[2];
3699 WIcon *icon = aicon->icon;
3700 WDock *dock2 = NULL, *last_dock = dock, *clip = NULL;
3701 int ondock, grabbed = 0, change_dock = 0, collapsed = 0;
3702 XEvent ev;
3703 int x = aicon->x_pos, y = aicon->y_pos;
3704 int ofs_x = event->xbutton.x, ofs_y = event->xbutton.y;
3705 int shad_x = x, shad_y = y;
3706 int ix = aicon->xindex, iy = aicon->yindex;
3707 int tmp;
3708 Pixmap ghost = None;
3709 Bool docked;
3710 int superfluous = wPreferences.superfluous; /* we catch it to avoid problems */
3711 int omnipresent = aicon->omnipresent; /* this must be cached!!! */
3714 if (wPreferences.flags.noupdates)
3715 return;
3717 if (XGrabPointer(dpy, icon->core->window, True, ButtonMotionMask
3718 |ButtonReleaseMask|ButtonPressMask, GrabModeAsync,
3719 GrabModeAsync, None, None, CurrentTime) !=GrabSuccess) {
3720 #ifdef DEBUG0
3721 wwarning("pointer grab failed for icon move");
3722 #endif
3725 if (!(event->xbutton.state & MOD_MASK))
3726 wRaiseFrame(icon->core);
3728 if (!wPreferences.flags.noclip)
3729 clip = scr->workspaces[scr->current_workspace]->clip;
3731 if (dock == scr->dock && !wPreferences.flags.noclip)
3732 dock2 = clip;
3733 else if (dock != scr->dock && !wPreferences.flags.nodock)
3734 dock2 = scr->dock;
3736 wins[0] = icon->core->window;
3737 wins[1] = scr->dock_shadow;
3738 XRestackWindows(dpy, wins, 2);
3739 XMoveResizeWindow(dpy, scr->dock_shadow, aicon->x_pos, aicon->y_pos,
3740 ICON_SIZE, ICON_SIZE);
3741 if (superfluous) {
3742 if (icon->pixmap!=None)
3743 ghost = MakeGhostIcon(scr, icon->pixmap);
3744 else
3745 ghost = MakeGhostIcon(scr, icon->core->window);
3747 XSetWindowBackgroundPixmap(dpy, scr->dock_shadow, ghost);
3748 XClearWindow(dpy, scr->dock_shadow);
3750 XMapWindow(dpy, scr->dock_shadow);
3752 ondock = 1;
3755 while(1) {
3756 XMaskEvent(dpy, PointerMotionMask|ButtonReleaseMask|ButtonPressMask
3757 |ButtonMotionMask|ExposureMask, &ev);
3758 switch (ev.type) {
3759 case Expose:
3760 WMHandleEvent(&ev);
3761 break;
3763 case MotionNotify:
3764 if (!grabbed) {
3765 if (abs(ofs_x-ev.xmotion.x)>=MOVE_THRESHOLD
3766 || abs(ofs_y-ev.xmotion.y)>=MOVE_THRESHOLD) {
3767 XChangeActivePointerGrab(dpy, ButtonMotionMask
3768 |ButtonReleaseMask|ButtonPressMask,
3769 wCursor[WCUR_MOVE], CurrentTime);
3770 grabbed=1;
3771 } else {
3772 break;
3776 if (omnipresent) {
3777 int i;
3778 for (i=0; i<scr->workspace_count; i++) {
3779 if (i == scr->current_workspace)
3780 continue;
3781 wDockShowIcons(scr->workspaces[i]->clip);
3785 x = ev.xmotion.x_root - ofs_x;
3786 y = ev.xmotion.y_root - ofs_y;
3787 tmp = wDockSnapIcon(dock, aicon, x, y, &ix, &iy, True);
3788 if (tmp && dock2) {
3789 change_dock = 0;
3790 if (last_dock != dock && collapsed) {
3791 last_dock->collapsed = 1;
3792 wDockHideIcons(last_dock);
3793 collapsed = 0;
3795 if (!collapsed && (collapsed = dock->collapsed)) {
3796 dock->collapsed = 0;
3797 wDockShowIcons(dock);
3799 if (dock->auto_raise_lower)
3800 wDockRaise(dock);
3801 last_dock = dock;
3802 } else if (dock2) {
3803 tmp = wDockSnapIcon(dock2, aicon, x, y, &ix, &iy, False);
3804 if (tmp) {
3805 change_dock = 1;
3806 if (last_dock != dock2 && collapsed) {
3807 last_dock->collapsed = 1;
3808 wDockHideIcons(last_dock);
3809 collapsed = 0;
3811 if (!collapsed && (collapsed = dock2->collapsed)) {
3812 dock2->collapsed = 0;
3813 wDockShowIcons(dock2);
3815 if (dock2->auto_raise_lower)
3816 wDockRaise(dock2);
3817 last_dock = dock2;
3820 if (aicon->launching
3821 || aicon->lock
3822 || (aicon->running && !(ev.xmotion.state & MOD_MASK))
3823 || (!aicon->running && tmp)) {
3824 shad_x = last_dock->x_pos + ix*wPreferences.icon_size;
3825 shad_y = last_dock->y_pos + iy*wPreferences.icon_size;
3827 XMoveWindow(dpy, scr->dock_shadow, shad_x, shad_y);
3829 if (!ondock) {
3830 XMapWindow(dpy, scr->dock_shadow);
3832 ondock = 1;
3833 } else {
3834 if (ondock) {
3835 XUnmapWindow(dpy, scr->dock_shadow);
3837 ondock = 0;
3839 XMoveWindow(dpy, icon->core->window, x, y);
3840 break;
3842 case ButtonPress:
3843 break;
3845 case ButtonRelease:
3846 if (ev.xbutton.button != event->xbutton.button)
3847 break;
3848 XUngrabPointer(dpy, CurrentTime);
3849 if (ondock) {
3850 SlideWindow(icon->core->window, x, y, shad_x, shad_y);
3851 XUnmapWindow(dpy, scr->dock_shadow);
3852 if (!change_dock) {
3853 reattachIcon(dock, aicon, ix, iy);
3854 if (clip && dock!=clip && clip->auto_raise_lower)
3855 wDockLower(clip);
3856 } else {
3857 docked = moveIconBetweenDocks(dock, dock2, aicon, ix, iy);
3858 if (!docked) {
3859 /* Slide it back if dock rejected it */
3860 SlideWindow(icon->core->window, x, y, aicon->x_pos,
3861 aicon->y_pos);
3862 reattachIcon(dock, aicon, aicon->xindex,aicon->yindex);
3864 if (last_dock->type==WM_CLIP && last_dock->auto_collapse) {
3865 collapsed = 0;
3868 } else {
3869 aicon->x_pos = x;
3870 aicon->y_pos = y;
3871 if (superfluous) {
3872 if (!aicon->running && !wPreferences.no_animations) {
3873 /* We need to deselect it, even if is deselected in
3874 * wDockDetach(), because else DoKaboom() will fail.
3876 if (aicon->icon->selected)
3877 wIconSelect(aicon->icon);
3879 #ifdef WSOUND
3880 wSoundPlay(WSOUND_KABOOM);
3881 #endif
3882 DoKaboom(scr,aicon->icon->core->window, x, y);
3883 #ifdef WSOUND
3884 } else {
3885 wSoundPlay(WSOUND_UNDOCK);
3886 #endif
3888 #ifdef WSOUND
3889 } else {
3890 wSoundPlay(WSOUND_UNDOCK);
3891 #endif
3893 if (clip && clip->auto_raise_lower)
3894 wDockLower(clip);
3895 wDockDetach(dock, aicon);
3897 if (collapsed) {
3898 last_dock->collapsed = 1;
3899 wDockHideIcons(last_dock);
3900 collapsed = 0;
3902 if (superfluous) {
3903 if (ghost!=None)
3904 XFreePixmap(dpy, ghost);
3905 XSetWindowBackground(dpy, scr->dock_shadow, scr->white_pixel);
3907 if (omnipresent) {
3908 int i;
3909 for (i=0; i<scr->workspace_count; i++) {
3910 if (i == scr->current_workspace)
3911 continue;
3912 wDockHideIcons(scr->workspaces[i]->clip);
3916 #ifdef DEBUG
3917 puts("End icon move");
3918 #endif
3919 return;
3925 static int
3926 getClipButton(int px, int py)
3928 int pt = (CLIP_BUTTON_SIZE+2)*ICON_SIZE/64;
3930 if (px < 0 || py < 0 || px >= ICON_SIZE || py >= ICON_SIZE)
3931 return CLIP_IDLE;
3933 if (py <= pt-((int)ICON_SIZE-1-px))
3934 return CLIP_FORWARD;
3935 else if (px <= pt-((int)ICON_SIZE-1-py))
3936 return CLIP_REWIND;
3938 return CLIP_IDLE;
3942 static void
3943 handleClipChangeWorkspace(WScreen *scr, XEvent *event)
3945 XEvent ev;
3946 int done, direction, new_ws;
3947 int new_dir;
3948 WDock *clip = scr->clip_icon->dock;
3950 direction = getClipButton(event->xbutton.x, event->xbutton.y);
3952 clip->lclip_button_pushed = direction==CLIP_REWIND;
3953 clip->rclip_button_pushed = direction==CLIP_FORWARD;
3955 wClipIconPaint(scr->clip_icon);
3956 done = 0;
3957 while(!done) {
3958 WMMaskEvent(dpy, ExposureMask|ButtonMotionMask|ButtonReleaseMask
3959 |ButtonPressMask, &ev);
3960 switch (ev.type) {
3961 case Expose:
3962 WMHandleEvent(&ev);
3963 break;
3965 case MotionNotify:
3966 new_dir = getClipButton(ev.xmotion.x, ev.xmotion.y);
3967 if (new_dir != direction) {
3968 direction = new_dir;
3969 clip->lclip_button_pushed = direction==CLIP_REWIND;
3970 clip->rclip_button_pushed = direction==CLIP_FORWARD;
3971 wClipIconPaint(scr->clip_icon);
3973 break;
3975 case ButtonPress:
3976 break;
3978 case ButtonRelease:
3979 if (ev.xbutton.button == event->xbutton.button)
3980 done = 1;
3984 clip->lclip_button_pushed = 0;
3985 clip->rclip_button_pushed = 0;
3987 new_ws = wPreferences.ws_advance || (event->xbutton.state & ControlMask);
3989 if (direction == CLIP_FORWARD) {
3990 if (scr->current_workspace < scr->workspace_count-1)
3991 wWorkspaceChange(scr, scr->current_workspace+1);
3992 else if (new_ws && scr->current_workspace < MAX_WORKSPACES-1)
3993 wWorkspaceChange(scr, scr->current_workspace+1);
3994 else if (wPreferences.ws_cycle)
3995 wWorkspaceChange(scr, 0);
3997 else if (direction == CLIP_REWIND) {
3998 if (scr->current_workspace > 0)
3999 wWorkspaceChange(scr, scr->current_workspace-1);
4000 else if (scr->current_workspace==0 && wPreferences.ws_cycle)
4001 wWorkspaceChange(scr, scr->workspace_count-1);
4004 wClipIconPaint(scr->clip_icon);
4008 static void
4009 iconMouseDown(WObjDescriptor *desc, XEvent *event)
4011 WAppIcon *aicon = desc->parent;
4012 WDock *dock = aicon->dock;
4013 WScreen *scr = aicon->icon->core->screen_ptr;
4015 if (aicon->editing || WCHECK_STATE(WSTATE_MODAL))
4016 return;
4018 scr->last_dock = dock;
4020 if (dock->menu->flags.mapped)
4021 wMenuUnmap(dock->menu);
4023 if (IsDoubleClick(scr, event)) {
4024 /* double-click was not in the main clip icon */
4025 if (dock->type != WM_CLIP || aicon->xindex!=0 || aicon->yindex!=0
4026 || getClipButton(event->xbutton.x, event->xbutton.y)==CLIP_IDLE) {
4027 iconDblClick(desc, event);
4028 return;
4032 if (dock->type == WM_CLIP && scr->flags.clip_balloon_mapped) {
4033 XUnmapWindow(dpy, scr->clip_balloon);
4034 scr->flags.clip_balloon_mapped = 0;
4037 #ifdef DEBUG
4038 puts("handling dock");
4039 #endif
4040 if (event->xbutton.button == Button1) {
4041 if (event->xbutton.state & MOD_MASK)
4042 wDockLower(dock);
4043 else
4044 wDockRaise(dock);
4046 if ((event->xbutton.state & ShiftMask) && aicon!=scr->clip_icon &&
4047 dock->type!=WM_DOCK) {
4048 wIconSelect(aicon->icon);
4049 return;
4052 if (aicon->yindex==0 && aicon->xindex==0) {
4053 if (getClipButton(event->xbutton.x, event->xbutton.y)!=CLIP_IDLE
4054 && dock->type==WM_CLIP)
4055 handleClipChangeWorkspace(scr, event);
4056 else
4057 handleDockMove(dock, aicon, event);
4058 } else
4059 handleIconMove(dock, aicon, event);
4061 } else if (event->xbutton.button==Button2 && dock->type==WM_CLIP &&
4062 aicon==scr->clip_icon) {
4063 openClipWorkspaceMenu(scr, event->xbutton.x_root+2,
4064 event->xbutton.y_root+2);
4065 if (scr->clip_ws_menu) {
4066 WMenu *menu;
4067 menu = scr->clip_ws_menu;
4068 desc = &menu->menu->descriptor;
4070 event->xany.send_event = True;
4071 (*desc->handle_mousedown)(desc, event);
4073 } else if (event->xbutton.button==Button2 && dock->type==WM_CLIP &&
4074 (event->xbutton.state & ShiftMask) && aicon!=scr->clip_icon) {
4075 wClipMakeIconOmnipresent(aicon, !aicon->omnipresent);
4076 } else if (event->xbutton.button == Button3) {
4077 if (event->xbutton.send_event &&
4078 XGrabPointer(dpy, aicon->icon->core->window, True, ButtonMotionMask
4079 |ButtonReleaseMask|ButtonPressMask, GrabModeAsync,
4080 GrabModeAsync, None, None, CurrentTime) !=GrabSuccess) {
4081 wwarning("pointer grab failed for dockicon menu");
4082 return;
4085 openDockMenu(dock, aicon, event);
4090 static void
4091 showClipBalloon(WDock *dock, int workspace)
4093 int w, h;
4094 int x, y;
4095 WScreen *scr = dock->screen_ptr;
4096 char *text;
4097 Window stack[2];
4099 scr->flags.clip_balloon_mapped = 1;
4100 XMapWindow(dpy, scr->clip_balloon);
4102 text = scr->workspaces[workspace]->name;
4104 w = WMWidthOfString(scr->clip_title_font, text, strlen(text));
4106 h = WMFontHeight(scr->clip_title_font);
4107 XResizeWindow(dpy, scr->clip_balloon, w, h);
4109 x = dock->x_pos + CLIP_BUTTON_SIZE*ICON_SIZE/64;
4110 y = dock->y_pos + ICON_SIZE - WMFontHeight(scr->clip_title_font) - 3;
4112 if (x+w > scr->scr_width) {
4113 x = scr->scr_width - w;
4114 if (dock->y_pos + ICON_SIZE + h > scr->scr_height)
4115 y = dock->y_pos - h - 1;
4116 else
4117 y = dock->y_pos + ICON_SIZE;
4118 XRaiseWindow(dpy, scr->clip_balloon);
4119 } else {
4120 stack[0] = scr->clip_icon->icon->core->window;
4121 stack[1] = scr->clip_balloon;
4122 XRestackWindows(dpy, stack, 2);
4124 XMoveWindow(dpy, scr->clip_balloon, x, y);
4125 XSetForeground(dpy, scr->clip_title_gc,
4126 scr->clip_title_pixel[CLIP_NORMAL]);
4127 XClearWindow(dpy, scr->clip_balloon);
4128 WMDrawString(scr->wmscreen, scr->clip_balloon, scr->clip_title_gc,
4129 scr->clip_title_font, 0, 0, text, strlen(text));
4133 static void
4134 clipEnterNotify(WObjDescriptor *desc, XEvent *event)
4136 WAppIcon *btn = (WAppIcon*)desc->parent;
4137 WDock *dock;
4138 WScreen *scr;
4140 assert(event->type==EnterNotify);
4142 if(desc->parent_type!=WCLASS_DOCK_ICON)
4143 return;
4145 scr = btn->icon->core->screen_ptr;
4146 if (!btn->omnipresent)
4147 dock = btn->dock;
4148 else
4149 dock = scr->workspaces[scr->current_workspace]->clip;
4151 if (!dock || dock->type!=WM_CLIP)
4152 return;
4154 /* The auto raise/lower code */
4155 if (dock->auto_lower_magic) {
4156 WMDeleteTimerHandler(dock->auto_lower_magic);
4157 dock->auto_lower_magic = NULL;
4159 if (dock->auto_raise_lower && !dock->auto_raise_magic) {
4160 dock->auto_raise_magic = WMAddTimerHandler(AUTO_RAISE_DELAY,
4161 clipAutoRaise,
4162 (void *)dock);
4165 /* The auto expand/collapse code */
4166 if (dock->auto_collapse_magic) {
4167 WMDeleteTimerHandler(dock->auto_collapse_magic);
4168 dock->auto_collapse_magic = NULL;
4170 if (dock->auto_collapse && !dock->auto_expand_magic) {
4171 dock->auto_expand_magic = WMAddTimerHandler(AUTO_EXPAND_DELAY,
4172 clipAutoExpand,
4173 (void *)dock);
4176 if (btn->xindex == 0 && btn->yindex == 0)
4177 showClipBalloon(dock, dock->screen_ptr->current_workspace);
4178 else {
4179 if (dock->screen_ptr->flags.clip_balloon_mapped) {
4180 XUnmapWindow(dpy, dock->screen_ptr->clip_balloon);
4181 dock->screen_ptr->flags.clip_balloon_mapped = 0;
4187 static void
4188 clipLeave(WDock *dock)
4190 XEvent event;
4191 WObjDescriptor *desc = NULL;
4193 if (!dock || dock->type!=WM_CLIP)
4194 return;
4196 if (XCheckTypedEvent(dpy, EnterNotify, &event)!=False) {
4197 if (XFindContext(dpy, event.xcrossing.window, wWinContext,
4198 (XPointer *)&desc)!=XCNOENT
4199 && desc && desc->parent_type==WCLASS_DOCK_ICON
4200 && ((WAppIcon*)desc->parent)->dock
4201 && ((WAppIcon*)desc->parent)->dock->type==WM_CLIP) {
4202 /* We didn't left the Clip yet */
4203 XPutBackEvent(dpy, &event);
4204 return;
4207 XPutBackEvent(dpy, &event);
4208 } else {
4209 /* We entered a withdrawn window, so we're still in Clip */
4210 return;
4213 if (dock->auto_raise_magic) {
4214 WMDeleteTimerHandler(dock->auto_raise_magic);
4215 dock->auto_raise_magic = NULL;
4217 if (dock->auto_raise_lower && !dock->auto_lower_magic) {
4218 dock->auto_lower_magic = WMAddTimerHandler(AUTO_LOWER_DELAY,
4219 clipAutoLower,
4220 (void *)dock);
4223 if (dock->auto_expand_magic) {
4224 WMDeleteTimerHandler(dock->auto_expand_magic);
4225 dock->auto_expand_magic = NULL;
4227 if (dock->auto_collapse && !dock->auto_collapse_magic) {
4228 dock->auto_collapse_magic = WMAddTimerHandler(AUTO_COLLAPSE_DELAY,
4229 clipAutoCollapse,
4230 (void *)dock);
4235 static void
4236 clipLeaveNotify(WObjDescriptor *desc, XEvent *event)
4238 WAppIcon *btn = (WAppIcon*)desc->parent;
4240 assert(event->type==LeaveNotify);
4242 if(desc->parent_type!=WCLASS_DOCK_ICON)
4243 return;
4245 clipLeave(btn->dock);
4249 static void
4250 clipAutoCollapse(void *cdata)
4252 WDock *dock = (WDock *)cdata;
4254 if (dock->type!=WM_CLIP)
4255 return;
4257 if (dock->auto_collapse) {
4258 dock->collapsed = 1;
4259 wDockHideIcons(dock);
4261 dock->auto_collapse_magic = NULL;
4265 static void
4266 clipAutoExpand(void *cdata)
4268 WDock *dock = (WDock *)cdata;
4270 if (dock->type!=WM_CLIP)
4271 return;
4273 if (dock->auto_collapse) {
4274 dock->collapsed = 0;
4275 wDockShowIcons(dock);
4277 dock->auto_expand_magic = NULL;
4281 static void
4282 clipAutoLower(void *cdata)
4284 WDock *dock = (WDock *)cdata;
4286 if (dock->type!=WM_CLIP)
4287 return;
4289 if (dock->auto_raise_lower)
4290 wDockLower(dock);
4292 dock->auto_lower_magic = NULL;
4296 static void
4297 clipAutoRaise(void *cdata)
4299 WDock *dock = (WDock *)cdata;
4301 if (dock->type!=WM_CLIP)
4302 return;
4304 if (dock->auto_raise_lower)
4305 wDockRaise(dock);
4307 if (dock->screen_ptr->flags.clip_balloon_mapped) {
4308 showClipBalloon(dock, dock->screen_ptr->current_workspace);
4311 dock->auto_raise_magic = NULL;
4315 static Bool
4316 iconCanBeOmnipresent(WAppIcon *aicon)
4318 WScreen *scr = aicon->icon->core->screen_ptr;
4319 WDock *clip;
4320 WAppIcon *btn;
4321 int i, j;
4323 for (i=0; i<scr->workspace_count; i++) {
4324 clip = scr->workspaces[i]->clip;
4326 if (clip == aicon->dock)
4327 continue;
4329 if (clip->icon_count + scr->global_icon_count >= clip->max_icons)
4330 return False; /* Clip is full in some workspace */
4332 for (j=0; j<clip->max_icons; j++) {
4333 btn = clip->icon_array[j];
4334 if(btn && btn->xindex==aicon->xindex && btn->yindex==aicon->yindex)
4335 return False;
4339 return True;
4344 wClipMakeIconOmnipresent(WAppIcon *aicon, int omnipresent)
4346 WScreen *scr = aicon->icon->core->screen_ptr;
4347 WAppIconChain *new_entry, *tmp, *tmp1;
4348 int status = WO_SUCCESS;
4350 if ((scr->dock && aicon->dock==scr->dock) || aicon==scr->clip_icon) {
4351 return WO_NOT_APPLICABLE;
4354 if (aicon->omnipresent == omnipresent)
4355 return WO_SUCCESS;
4357 if (omnipresent) {
4358 if (iconCanBeOmnipresent(aicon)) {
4359 aicon->omnipresent = 1;
4360 new_entry = wmalloc(sizeof(WAppIconChain));
4361 new_entry->aicon = aicon;
4362 new_entry->next = scr->global_icons;
4363 scr->global_icons = new_entry;
4364 scr->global_icon_count++;
4365 } else {
4366 aicon->omnipresent = 0;
4367 status = WO_FAILED;
4369 } else {
4370 aicon->omnipresent = 0;
4371 if (aicon == scr->global_icons->aicon) {
4372 tmp = scr->global_icons->next;
4373 wfree(scr->global_icons);
4374 scr->global_icons = tmp;
4375 scr->global_icon_count--;
4376 } else {
4377 tmp = scr->global_icons;
4378 while (tmp->next) {
4379 if (tmp->next->aicon == aicon) {
4380 tmp1 = tmp->next->next;
4381 wfree(tmp->next);
4382 tmp->next = tmp1;
4383 scr->global_icon_count--;
4384 break;
4386 tmp = tmp->next;
4391 wAppIconPaint(aicon);
4393 return status;