- Finished moving to the new proplist handling code in WINGs.
[wmaker-crm.git] / src / dock.c
blobf007a09dd0a396e15d8d2fa59afcb5febb150c35
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 <WINGs/WUtil.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 WMPropList *dCommand=NULL;
101 static WMPropList *dPasteCommand=NULL;
102 #ifdef OFFIX_DND
103 static WMPropList *dDropCommand=NULL;
104 #endif
105 static WMPropList *dAutoLaunch, *dLock;
106 static WMPropList *dName, *dForced, *dBuggyApplication, *dYes, *dNo;
107 static WMPropList *dHost, *dDock, *dClip;
108 static WMPropList *dAutoAttractIcons;
110 static WMPropList *dPosition, *dApplications, *dLowered, *dCollapsed;
112 static WMPropList *dAutoCollapse, *dAutoRaiseLower, *dOmnipresent;
114 static void dockIconPaint(WAppIcon *btn);
116 static void iconMouseDown(WObjDescriptor *desc, XEvent *event);
118 static pid_t execCommand(WAppIcon *btn, char *command, WSavedState *state);
120 static void trackDeadProcess(pid_t pid, unsigned char status, WDock *dock);
122 static int getClipButton(int px, int py);
124 static void toggleLowered(WDock *dock);
126 static void toggleCollapsed(WDock *dock);
128 static void clipIconExpose(WObjDescriptor *desc, XEvent *event);
130 static void clipLeave(WDock *dock);
132 static void handleClipChangeWorkspace(WScreen *scr, XEvent *event);
134 Bool moveIconBetweenDocks(WDock *src, WDock *dest, WAppIcon *icon, int x, int y);
136 static void clipEnterNotify(WObjDescriptor *desc, XEvent *event);
137 static void clipLeaveNotify(WObjDescriptor *desc, XEvent *event);
138 static void clipAutoCollapse(void *cdata);
139 static void clipAutoExpand(void *cdata);
140 static void launchDockedApplication(WAppIcon *btn, Bool withSelection);
142 static void clipAutoLower(void *cdata);
143 static void clipAutoRaise(void *cdata);
145 static void showClipBalloon(WDock *dock, int workspace);
147 #ifdef OFFIX_DND
149 #define DndNotDnd -1
150 #define DndUnknown 0
151 #define DndRawData 1
152 #define DndFile 2
153 #define DndFiles 3
154 #define DndText 4
155 #define DndDir 5
156 #define DndLink 6
157 #define DndExe 7
159 #define DndEND 8
161 #endif /* OFFIX_DND */
165 static void
166 make_keys()
168 if (dCommand!=NULL)
169 return;
171 dCommand = WMRetainPropList(WMCreatePLString("Command"));
172 dPasteCommand = WMRetainPropList(WMCreatePLString("PasteCommand"));
173 dDropCommand = WMRetainPropList(WMCreatePLString("DropCommand"));
174 dLock = WMRetainPropList(WMCreatePLString("Lock"));
175 dAutoLaunch = WMRetainPropList(WMCreatePLString("AutoLaunch"));
176 dName = WMRetainPropList(WMCreatePLString("Name"));
177 dForced = WMRetainPropList(WMCreatePLString("Forced"));
178 dBuggyApplication = WMRetainPropList(WMCreatePLString("BuggyApplication"));
179 dYes = WMRetainPropList(WMCreatePLString("Yes"));
180 dNo = WMRetainPropList(WMCreatePLString("No"));
181 dHost = WMRetainPropList(WMCreatePLString("Host"));
183 dPosition = WMCreatePLString("Position");
184 dApplications = WMCreatePLString("Applications");
185 dLowered = WMCreatePLString("Lowered");
186 dCollapsed = WMCreatePLString("Collapsed");
187 dAutoCollapse = WMCreatePLString("AutoCollapse");
188 dAutoRaiseLower = WMCreatePLString("AutoRaiseLower");
189 dAutoAttractIcons = WMCreatePLString("AutoAttractIcons");
191 dOmnipresent = WMCreatePLString("Omnipresent");
193 dDock = WMCreatePLString("Dock");
194 dClip = WMCreatePLString("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 snprintf(buffer, sizeof(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, False);
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, Bool withSelection)
895 WScreen *scr = btn->icon->core->screen_ptr;
897 if (!btn->launching &&
898 ((!withSelection && btn->command!=NULL) ||
899 (withSelection && btn->paste_command!=NULL))) {
900 if (!btn->forced_dock) {
901 btn->relaunching = btn->running;
902 btn->running = 1;
904 if (btn->wm_instance || btn->wm_class) {
905 WWindowAttributes attr;
906 memset(&attr, 0, sizeof(WWindowAttributes));
907 wDefaultFillAttributes(scr, btn->wm_instance, btn->wm_class,
908 &attr, NULL, True);
910 if (!attr.no_appicon && !btn->buggy_app)
911 btn->launching = 1;
912 else
913 btn->running = 0;
915 btn->drop_launch = 0;
916 btn->paste_launch = withSelection;
917 scr->last_dock = btn->dock;
918 btn->pid = execCommand(btn,
919 withSelection ? btn->paste_command : btn->command,
920 NULL);
921 if (btn->pid>0) {
922 if (btn->buggy_app) {
923 /* give feedback that the app was launched */
924 btn->launching = 1;
925 dockIconPaint(btn);
926 btn->launching = 0;
927 WMAddTimerHandler(200, (WMCallback*)dockIconPaint, btn);
928 } else {
929 dockIconPaint(btn);
931 } else {
932 wwarning(_("could not launch application %s\n"), btn->command);
933 btn->launching = 0;
934 if (!btn->relaunching)
935 btn->running = 0;
942 static void
943 updateWorkspaceMenu(WMenu *menu, WAppIcon *icon)
945 WScreen *scr = menu->frame->screen_ptr;
946 char title[MAX_WORKSPACENAME_WIDTH+1];
947 int i;
949 if (!menu || !icon)
950 return;
952 for (i=0; i<scr->workspace_count; i++) {
953 if (i < menu->entry_no) {
954 if (strcmp(menu->entries[i]->text,scr->workspaces[i]->name)!=0) {
955 wfree(menu->entries[i]->text);
956 strcpy(title, scr->workspaces[i]->name);
957 menu->entries[i]->text = wstrdup(title);
958 menu->flags.realized = 0;
960 menu->entries[i]->clientdata = (void*)icon;
961 } else {
962 strcpy(title, scr->workspaces[i]->name);
964 wMenuAddCallback(menu, title, switchWSCommand, (void*)icon);
966 menu->flags.realized = 0;
968 if (i == scr->current_workspace) {
969 wMenuSetEnabled(menu, i, False);
970 } else {
971 wMenuSetEnabled(menu, i, True);
975 if (!menu->flags.realized)
976 wMenuRealize(menu);
980 static WMenu*
981 makeWorkspaceMenu(WScreen *scr)
983 WMenu *menu;
985 menu = wMenuCreate(scr, NULL, False);
986 if (!menu)
987 wwarning(_("could not create workspace submenu for Clip menu"));
989 wMenuAddCallback(menu, "", switchWSCommand, (void*)scr->clip_icon);
991 menu->flags.realized = 0;
992 wMenuRealize(menu);
994 return menu;
998 static void
999 updateClipOptionsMenu(WMenu *menu, WDock *dock)
1001 WMenuEntry *entry;
1002 int index = 0;
1004 if (!menu || !dock)
1005 return;
1007 /* keep on top */
1008 entry = menu->entries[index];
1009 entry->flags.indicator_on = !dock->lowered;
1010 entry->clientdata = dock;
1012 /* collapsed */
1013 entry = menu->entries[++index];
1014 entry->flags.indicator_on = dock->collapsed;
1015 entry->clientdata = dock;
1017 /* auto-collapse */
1018 entry = menu->entries[++index];
1019 entry->flags.indicator_on = dock->auto_collapse;
1020 entry->clientdata = dock;
1022 /* auto-raise/lower */
1023 entry = menu->entries[++index];
1024 entry->flags.indicator_on = dock->auto_raise_lower;
1025 entry->clientdata = dock;
1027 /* attract icons */
1028 entry = menu->entries[++index];
1029 entry->flags.indicator_on = dock->attract_icons;
1030 entry->clientdata = dock;
1032 menu->flags.realized = 0;
1033 wMenuRealize(menu);
1037 static WMenu*
1038 makeClipOptionsMenu(WScreen *scr)
1040 WMenu *menu;
1041 WMenuEntry *entry;
1043 menu = wMenuCreate(scr, NULL, False);
1044 if (!menu) {
1045 wwarning(_("could not create options submenu for Clip menu"));
1046 return NULL;
1049 entry = wMenuAddCallback(menu, _("Keep on Top"),
1050 toggleLoweredCallback, NULL);
1051 entry->flags.indicator = 1;
1052 entry->flags.indicator_on = 1;
1053 entry->flags.indicator_type = MI_CHECK;
1055 entry = wMenuAddCallback(menu, _("Collapsed"),
1056 toggleCollapsedCallback, NULL);
1057 entry->flags.indicator = 1;
1058 entry->flags.indicator_on = 1;
1059 entry->flags.indicator_type = MI_CHECK;
1061 entry = wMenuAddCallback(menu, _("Autocollapse"),
1062 toggleAutoCollapseCallback, NULL);
1063 entry->flags.indicator = 1;
1064 entry->flags.indicator_on = 1;
1065 entry->flags.indicator_type = MI_CHECK;
1067 entry = wMenuAddCallback(menu, _("Autoraise"),
1068 toggleAutoRaiseLowerCallback, NULL);
1069 entry->flags.indicator = 1;
1070 entry->flags.indicator_on = 1;
1071 entry->flags.indicator_type = MI_CHECK;
1073 entry = wMenuAddCallback(menu, _("Autoattract Icons"),
1074 toggleAutoAttractCallback, NULL);
1075 entry->flags.indicator = 1;
1076 entry->flags.indicator_on = 1;
1077 entry->flags.indicator_type = MI_CHECK;
1079 menu->flags.realized = 0;
1080 wMenuRealize(menu);
1082 return menu;
1086 static WMenu*
1087 dockMenuCreate(WScreen *scr, int type)
1089 WMenu *menu;
1090 WMenuEntry *entry;
1092 if (type == WM_CLIP && scr->clip_menu)
1093 return scr->clip_menu;
1095 menu = wMenuCreate(scr, NULL, False);
1096 if (type != WM_CLIP) {
1097 entry = wMenuAddCallback(menu, _("Keep on Top"),
1098 toggleLoweredCallback, NULL);
1099 entry->flags.indicator = 1;
1100 entry->flags.indicator_on = 1;
1101 entry->flags.indicator_type = MI_CHECK;
1102 } else {
1103 entry = wMenuAddCallback(menu, _("Clip Options"), NULL, NULL);
1104 scr->clip_options = makeClipOptionsMenu(scr);
1105 if (scr->clip_options)
1106 wMenuEntrySetCascade(menu, entry, scr->clip_options);
1108 entry = wMenuAddCallback(menu, _("Rename Workspace"), renameCallback,
1109 NULL);
1110 wfree(entry->text);
1111 entry->text = _("Rename Workspace");
1113 entry = wMenuAddCallback(menu, _("Selected"), selectCallback, NULL);
1114 entry->flags.indicator = 1;
1115 entry->flags.indicator_on = 1;
1116 entry->flags.indicator_type = MI_CHECK;
1118 entry = wMenuAddCallback(menu, _("Select All Icons"),
1119 selectIconsCallback, NULL);
1120 wfree(entry->text);
1121 entry->text = _("Select All Icons");
1123 entry = wMenuAddCallback(menu, _("Keep Icon"), keepIconsCallback, NULL);
1124 wfree(entry->text);
1125 entry->text = _("Keep Icon");
1127 entry = wMenuAddCallback(menu, _("Move Icon To"), NULL, NULL);
1128 wfree(entry->text);
1129 entry->text = _("Move Icon To");
1130 scr->clip_submenu = makeWorkspaceMenu(scr);
1131 if (scr->clip_submenu)
1132 wMenuEntrySetCascade(menu, entry, scr->clip_submenu);
1134 entry = wMenuAddCallback(menu, _("Remove Icon"), removeIconsCallback,
1135 NULL);
1136 wfree(entry->text);
1137 entry->text = _("Remove Icon");
1139 wMenuAddCallback(menu, _("Attract Icons"), colectIconsCallback, NULL);
1142 wMenuAddCallback(menu, _("Launch"), launchCallback, NULL);
1144 wMenuAddCallback(menu, _("Unhide Here"), unhideHereCallback, NULL);
1146 entry = wMenuAddCallback(menu, _("Hide"), hideCallback, NULL);
1147 wfree(entry->text);
1148 entry->text = _("Hide");
1150 wMenuAddCallback(menu, _("Settings..."), settingsCallback, NULL);
1152 wMenuAddCallback(menu, _("Kill"), killCallback, NULL);
1154 if (type == WM_CLIP)
1155 scr->clip_menu = menu;
1157 return menu;
1161 WDock*
1162 wDockCreate(WScreen *scr, int type)
1164 WDock *dock;
1165 WAppIcon *btn;
1166 int icon_count;
1168 make_keys();
1170 dock = wmalloc(sizeof(WDock));
1171 memset(dock, 0, sizeof(WDock));
1173 if (type == WM_CLIP)
1174 icon_count = CLIP_MAX_ICONS;
1175 else
1176 icon_count = scr->scr_height/wPreferences.icon_size;
1178 dock->icon_array = wmalloc(sizeof(WAppIcon*)*icon_count);
1179 memset(dock->icon_array, 0, sizeof(WAppIcon*)*icon_count);
1181 dock->max_icons = icon_count;
1183 btn = mainIconCreate(scr, type);
1185 btn->dock = dock;
1187 dock->x_pos = btn->x_pos;
1188 dock->y_pos = btn->y_pos;
1189 dock->screen_ptr = scr;
1190 dock->type = type;
1191 dock->icon_count = 1;
1192 dock->on_right_side = 1;
1193 dock->collapsed = 0;
1194 dock->auto_collapse = 0;
1195 dock->auto_collapse_magic = NULL;
1196 dock->auto_raise_lower = 0;
1197 dock->auto_lower_magic = NULL;
1198 dock->auto_raise_magic = NULL;
1199 dock->attract_icons = 0;
1200 dock->lowered = 1;
1201 dock->icon_array[0] = btn;
1202 wRaiseFrame(btn->icon->core);
1203 XMoveWindow(dpy, btn->icon->core->window, btn->x_pos, btn->y_pos);
1205 /* create dock menu */
1206 dock->menu = dockMenuCreate(scr, type);
1208 return dock;
1212 void
1213 wDockDestroy(WDock *dock)
1215 int i;
1216 WAppIcon *aicon;
1218 for (i=(dock->type == WM_CLIP) ? 1 : 0; i<dock->max_icons; i++) {
1219 aicon = dock->icon_array[i];
1220 if (aicon) {
1221 int keepit = aicon->running && wApplicationOf(aicon->main_window);
1222 wDockDetach(dock, aicon);
1223 if (keepit) {
1224 PlaceIcon(dock->screen_ptr, &aicon->x_pos, &aicon->y_pos);
1225 XMoveWindow(dpy, aicon->icon->core->window,
1226 aicon->x_pos, aicon->y_pos);
1227 if (!dock->mapped || dock->collapsed)
1228 XMapWindow(dpy, aicon->icon->core->window);
1232 if (wPreferences.auto_arrange_icons)
1233 wArrangeIcons(dock->screen_ptr, True);
1234 wfree(dock->icon_array);
1235 if (dock->menu && dock->type!=WM_CLIP)
1236 wMenuDestroy(dock->menu, True);
1237 if (dock->screen_ptr->last_dock == dock)
1238 dock->screen_ptr->last_dock = NULL;
1239 wfree(dock);
1243 void
1244 wClipIconPaint(WAppIcon *aicon)
1246 WScreen *scr = aicon->icon->core->screen_ptr;
1247 WWorkspace *workspace = scr->workspaces[scr->current_workspace];
1248 GC gc;
1249 Window win = aicon->icon->core->window;
1250 int length, nlength;
1251 char *ws_name, ws_number[10];
1252 int ty, tx;
1254 wIconPaint(aicon->icon);
1256 length = strlen(workspace->name);
1257 ws_name = wmalloc(length + 1);
1258 snprintf(ws_name, length+1, "%s", workspace->name);
1259 snprintf(ws_number, sizeof(ws_number), "%i", scr->current_workspace + 1);
1260 nlength = strlen(ws_number);
1262 gc = scr->clip_title_gc;
1264 if (!workspace->clip->collapsed)
1265 XSetForeground(dpy, gc, scr->clip_title_pixel[CLIP_NORMAL]);
1266 else
1267 XSetForeground(dpy, gc, scr->clip_title_pixel[CLIP_COLLAPSED]);
1269 ty = ICON_SIZE - WMFontHeight(scr->clip_title_font) - 3;
1271 tx = CLIP_BUTTON_SIZE*ICON_SIZE/64;
1273 WMDrawString(scr->wmscreen, win, gc, scr->clip_title_font, tx,
1274 ty, ws_name, length);
1276 tx = (ICON_SIZE/2 - WMWidthOfString(scr->clip_title_font, ws_number,
1277 nlength))/2;
1279 WMDrawString(scr->wmscreen, win, gc, scr->clip_title_font, tx,
1280 2, ws_number, nlength);
1282 wfree(ws_name);
1284 if (aicon->launching) {
1285 XFillRectangle(dpy, aicon->icon->core->window, scr->stipple_gc,
1286 0, 0, wPreferences.icon_size, wPreferences.icon_size);
1288 paintClipButtons(aicon, aicon->dock->lclip_button_pushed,
1289 aicon->dock->rclip_button_pushed);
1293 static void
1294 clipIconExpose(WObjDescriptor *desc, XEvent *event)
1296 wClipIconPaint(desc->parent);
1300 static void
1301 dockIconPaint(WAppIcon *btn)
1303 if (btn == btn->icon->core->screen_ptr->clip_icon)
1304 wClipIconPaint(btn);
1305 else
1306 wAppIconPaint(btn);
1310 static WMPropList*
1311 make_icon_state(WAppIcon *btn)
1313 WMPropList *node = NULL;
1314 WMPropList *command, *autolaunch, *lock, *name, *forced, *host;
1315 WMPropList *position, *buggy, *omnipresent;
1316 char *tmp;
1317 char buffer[64];
1319 if (btn) {
1320 if (!btn->command)
1321 command = WMCreatePLString("-");
1322 else
1323 command = WMCreatePLString(btn->command);
1325 autolaunch = btn->auto_launch ? dYes : dNo;
1327 lock = btn->lock ? dYes : dNo;
1329 tmp = EscapeWM_CLASS(btn->wm_instance, btn->wm_class);
1331 name = WMCreatePLString(tmp);
1333 wfree(tmp);
1335 forced = btn->forced_dock ? dYes : dNo;
1337 buggy = btn->buggy_app ? dYes : dNo;
1339 if (btn == btn->icon->core->screen_ptr->clip_icon)
1340 snprintf(buffer, sizeof(buffer), "%i,%i", btn->x_pos, btn->y_pos);
1341 else
1342 snprintf(buffer, sizeof(buffer), "%hi,%hi", btn->xindex, btn->yindex);
1343 position = WMCreatePLString(buffer);
1345 node = WMCreatePLDictionary(dCommand, command,
1346 dName, name,
1347 dAutoLaunch, autolaunch,
1348 dLock, lock,
1349 dForced, forced,
1350 dBuggyApplication, buggy,
1351 dPosition, position,
1352 NULL);
1353 WMReleasePropList(command);
1354 WMReleasePropList(name);
1355 WMReleasePropList(position);
1357 omnipresent = btn->omnipresent ? dYes : dNo;
1358 if (btn->dock != btn->icon->core->screen_ptr->dock &&
1359 (btn->xindex != 0 || btn->yindex != 0))
1360 WMPutInPLDictionary(node, dOmnipresent, omnipresent);
1362 #ifdef OFFIX_DND
1363 if (btn->dnd_command) {
1364 command = WMCreatePLString(btn->dnd_command);
1365 WMPutInPLDictionary(node, dDropCommand, command);
1366 WMReleasePropList(command);
1368 #endif /* OFFIX_DND */
1370 if (btn->paste_command) {
1371 command = WMCreatePLString(btn->paste_command);
1372 WMPutInPLDictionary(node, dPasteCommand, command);
1373 WMReleasePropList(command);
1376 if (btn->client_machine && btn->remote_start) {
1377 host = WMCreatePLString(btn->client_machine);
1378 WMPutInPLDictionary(node, dHost, host);
1379 WMReleasePropList(host);
1383 return node;
1387 static WMPropList*
1388 dockSaveState(WDock *dock)
1390 int i;
1391 WMPropList *icon_info;
1392 WMPropList *list=NULL, *dock_state=NULL;
1393 WMPropList *value, *key;
1394 char buffer[256];
1396 list = WMCreatePLArray(NULL);
1398 for (i=(dock->type==WM_DOCK ? 0 : 1); i<dock->max_icons; i++) {
1399 WAppIcon *btn = dock->icon_array[i];
1401 if (!btn || btn->attracted)
1402 continue;
1404 if ((icon_info = make_icon_state(dock->icon_array[i]))) {
1405 WMAddToPLArray(list, icon_info);
1406 WMReleasePropList(icon_info);
1410 dock_state = WMCreatePLDictionary(dApplications, list,
1411 NULL);
1413 if (dock->type == WM_DOCK) {
1414 snprintf(buffer, sizeof(buffer), "Applications%i", dock->screen_ptr->scr_height);
1415 key = WMCreatePLString(buffer);
1416 WMPutInPLDictionary(dock_state, key, list);
1417 WMReleasePropList(key);
1420 snprintf(buffer, sizeof(buffer), "%i,%i", (dock->on_right_side ? -ICON_SIZE : 0),
1421 dock->y_pos);
1422 value = WMCreatePLString(buffer);
1423 WMPutInPLDictionary(dock_state, dPosition, value);
1424 WMReleasePropList(value);
1426 WMReleasePropList(list);
1429 value = (dock->lowered ? dYes : dNo);
1430 WMPutInPLDictionary(dock_state, dLowered, value);
1432 if (dock->type == WM_CLIP) {
1433 value = (dock->collapsed ? dYes : dNo);
1434 WMPutInPLDictionary(dock_state, dCollapsed, value);
1436 value = (dock->auto_collapse ? dYes : dNo);
1437 WMPutInPLDictionary(dock_state, dAutoCollapse, value);
1439 value = (dock->auto_raise_lower ? dYes : dNo);
1440 WMPutInPLDictionary(dock_state, dAutoRaiseLower, value);
1442 value = (dock->attract_icons ? dYes : dNo);
1443 WMPutInPLDictionary(dock_state, dAutoAttractIcons, value);
1446 return dock_state;
1450 void
1451 wDockSaveState(WScreen *scr, WMPropList *old_state)
1453 WMPropList *dock_state;
1454 WMPropList *keys;
1456 dock_state = dockSaveState(scr->dock);
1459 * Copy saved states of docks with different sizes.
1461 if (old_state) {
1462 int i;
1463 WMPropList *tmp;
1465 keys = WMGetPLDictionaryKeys(old_state);
1466 for (i = 0; i < WMGetPropListItemCount(keys); i++) {
1467 tmp = WMGetFromPLArray(keys, i);
1469 if (strncasecmp(WMGetFromPLString(tmp), "applications", 12) == 0
1470 && !WMGetFromPLDictionary(dock_state, tmp)) {
1472 WMPutInPLDictionary(dock_state,
1473 tmp,
1474 WMGetFromPLDictionary(old_state, tmp));
1477 WMReleasePropList(keys);
1481 WMPutInPLDictionary(scr->session_state, dDock, dock_state);
1483 WMReleasePropList(dock_state);
1487 void
1488 wClipSaveState(WScreen *scr)
1490 WMPropList *clip_state;
1492 clip_state = make_icon_state(scr->clip_icon);
1494 WMPutInPLDictionary(scr->session_state, dClip, clip_state);
1496 WMReleasePropList(clip_state);
1500 WMPropList*
1501 wClipSaveWorkspaceState(WScreen *scr, int workspace)
1503 return dockSaveState(scr->workspaces[workspace]->clip);
1507 static Bool
1508 getBooleanDockValue(WMPropList *value, WMPropList *key)
1510 if (value) {
1511 if (WMIsPLString(value)) {
1512 if (strcasecmp(WMGetFromPLString(value), "YES")==0)
1513 return True;
1514 } else {
1515 wwarning(_("bad value in docked icon state info %s"),
1516 WMGetFromPLString(key));
1519 return False;
1523 static WAppIcon*
1524 restore_icon_state(WScreen *scr, WMPropList *info, int type, int index)
1526 WAppIcon *aicon;
1527 WMPropList *cmd, *value;
1530 cmd = WMGetFromPLDictionary(info, dCommand);
1531 if (!cmd || !WMIsPLString(cmd)) {
1532 return NULL;
1535 /* parse window name */
1536 value = WMGetFromPLDictionary(info, dName);
1537 if (!value)
1538 return NULL;
1541 char *wclass, *winstance;
1542 char *command;
1544 ParseWindowName(value, &winstance, &wclass, "dock");
1546 if (!winstance && !wclass) {
1547 return NULL;
1550 /* get commands */
1552 if (cmd)
1553 command = wstrdup(WMGetFromPLString(cmd));
1554 else
1555 command = NULL;
1557 if (!command || strcmp(command, "-")==0) {
1558 if (command)
1559 wfree(command);
1560 if (wclass)
1561 wfree(wclass);
1562 if (winstance)
1563 wfree(winstance);
1565 return NULL;
1568 aicon = wAppIconCreateForDock(scr, command, winstance, wclass,
1569 TILE_NORMAL);
1570 if (wclass)
1571 wfree(wclass);
1572 if (winstance)
1573 wfree(winstance);
1574 if (command)
1575 wfree(command);
1578 aicon->icon->core->descriptor.handle_mousedown = iconMouseDown;
1579 if (type == WM_CLIP) {
1580 aicon->icon->core->descriptor.handle_enternotify = clipEnterNotify;
1581 aicon->icon->core->descriptor.handle_leavenotify = clipLeaveNotify;
1583 aicon->icon->core->descriptor.parent_type = WCLASS_DOCK_ICON;
1584 aicon->icon->core->descriptor.parent = aicon;
1587 #ifdef OFFIX_DND
1588 cmd = WMGetFromPLDictionary(info, dDropCommand);
1589 if (cmd)
1590 aicon->dnd_command = wstrdup(WMGetFromPLString(cmd));
1591 #endif
1593 cmd = WMGetFromPLDictionary(info, dPasteCommand);
1594 if (cmd)
1595 aicon->paste_command = wstrdup(WMGetFromPLString(cmd));
1597 /* check auto launch */
1598 value = WMGetFromPLDictionary(info, dAutoLaunch);
1600 aicon->auto_launch = getBooleanDockValue(value, dAutoLaunch);
1602 /* check lock */
1603 value = WMGetFromPLDictionary(info, dLock);
1605 aicon->lock = getBooleanDockValue(value, dLock);
1607 /* check if it wasn't normally docked */
1608 value = WMGetFromPLDictionary(info, dForced);
1610 aicon->forced_dock = getBooleanDockValue(value, dForced);
1612 /* check if we can rely on the stuff in the app */
1613 value = WMGetFromPLDictionary(info, dBuggyApplication);
1615 aicon->buggy_app = getBooleanDockValue(value, dBuggyApplication);
1617 /* get position in the dock */
1618 value = WMGetFromPLDictionary(info, dPosition);
1619 if (value && WMIsPLString(value)) {
1620 if (sscanf(WMGetFromPLString(value), "%hi,%hi", &aicon->xindex,
1621 &aicon->yindex)!=2)
1622 wwarning(_("bad value in docked icon state info %s"),
1623 WMGetFromPLString(dPosition));
1625 /* check position sanity */
1626 /* incomplete section! */
1627 if (type == WM_DOCK) {
1628 aicon->xindex = 0;
1629 if (aicon->yindex < 0)
1630 wwarning(_("bad value in docked icon position %i,%i"),
1631 aicon->xindex, aicon->yindex);
1633 } else {
1634 aicon->yindex = index;
1635 aicon->xindex = 0;
1638 /* check if icon is omnipresent */
1639 value = WMGetFromPLDictionary(info, dOmnipresent);
1641 aicon->omnipresent = getBooleanDockValue(value, dOmnipresent);
1643 aicon->running = 0;
1644 aicon->docked = 1;
1646 return aicon;
1650 #define COMPLAIN(key) wwarning(_("bad value in dock state info:%s"), key)
1653 WAppIcon*
1654 wClipRestoreState(WScreen *scr, WMPropList *clip_state)
1656 WAppIcon *icon;
1657 WMPropList *value;
1660 icon = mainIconCreate(scr, WM_CLIP);
1662 if (!clip_state)
1663 return icon;
1664 else
1665 WMRetainPropList(clip_state);
1667 /* restore position */
1669 value = WMGetFromPLDictionary(clip_state, dPosition);
1671 if (value) {
1672 if (!WMIsPLString(value))
1673 COMPLAIN("Position");
1674 else {
1675 if (sscanf(WMGetFromPLString(value), "%i,%i", &icon->x_pos,
1676 &icon->y_pos)!=2)
1677 COMPLAIN("Position");
1679 /* check position sanity */
1680 if (icon->y_pos < 0)
1681 icon->y_pos = 0;
1682 else if (icon->y_pos > scr->scr_height-ICON_SIZE)
1683 icon->y_pos = scr->scr_height-ICON_SIZE;
1685 if (icon->x_pos < 0)
1686 icon->x_pos = 0;
1687 else if (icon->x_pos > scr->scr_width-ICON_SIZE)
1688 icon->x_pos = scr->scr_width-ICON_SIZE;
1692 #ifdef OFFIX_DND
1693 value = WMGetFromPLDictionary(clip_state, dDropCommand);
1694 if (value && WMIsPLString(value))
1695 icon->dnd_command = wstrdup(WMGetFromPLString(value));
1696 #endif
1698 value = WMGetFromPLDictionary(clip_state, dPasteCommand);
1699 if (value && WMIsPLString(value))
1700 icon->paste_command = wstrdup(WMGetFromPLString(value));
1702 WMReleasePropList(clip_state);
1704 return icon;
1708 WDock*
1709 wDockRestoreState(WScreen *scr, WMPropList *dock_state, int type)
1711 WDock *dock;
1712 WMPropList *apps;
1713 WMPropList *value;
1714 WAppIcon *aicon, *old_top;
1715 int count, i;
1718 dock = wDockCreate(scr, type);
1720 if (!dock_state)
1721 return dock;
1723 if (dock_state)
1724 WMRetainPropList(dock_state);
1727 /* restore position */
1729 value = WMGetFromPLDictionary(dock_state, dPosition);
1731 if (value) {
1732 if (!WMIsPLString(value))
1733 COMPLAIN("Position");
1734 else {
1735 if (sscanf(WMGetFromPLString(value), "%i,%i", &dock->x_pos,
1736 &dock->y_pos)!=2)
1737 COMPLAIN("Position");
1739 /* check position sanity */
1740 if (dock->y_pos < 0)
1741 dock->y_pos = 0;
1742 else if (dock->y_pos > scr->scr_height-ICON_SIZE)
1743 dock->y_pos = scr->scr_height - ICON_SIZE;
1745 /* This is no more needed. ??? */
1746 if (type == WM_CLIP) {
1747 if (dock->x_pos < 0)
1748 dock->x_pos = 0;
1749 else if (dock->x_pos > scr->scr_width-ICON_SIZE)
1750 dock->x_pos = scr->scr_width-ICON_SIZE;
1751 } else {
1752 if (dock->x_pos >= 0) {
1753 dock->x_pos = DOCK_EXTRA_SPACE;
1754 dock->on_right_side = 0;
1755 } else {
1756 dock->x_pos = scr->scr_width - DOCK_EXTRA_SPACE - ICON_SIZE;
1757 dock->on_right_side = 1;
1763 /* restore lowered/raised state */
1765 dock->lowered = 0;
1767 value = WMGetFromPLDictionary(dock_state, dLowered);
1769 if (value) {
1770 if (!WMIsPLString(value))
1771 COMPLAIN("Lowered");
1772 else {
1773 if (strcasecmp(WMGetFromPLString(value), "YES")==0)
1774 dock->lowered = 1;
1779 /* restore collapsed state */
1781 dock->collapsed = 0;
1783 value = WMGetFromPLDictionary(dock_state, dCollapsed);
1785 if (value) {
1786 if (!WMIsPLString(value))
1787 COMPLAIN("Collapsed");
1788 else {
1789 if (strcasecmp(WMGetFromPLString(value), "YES")==0)
1790 dock->collapsed = 1;
1795 /* restore auto-collapsed state */
1797 value = WMGetFromPLDictionary(dock_state, dAutoCollapse);
1799 if (value) {
1800 if (!WMIsPLString(value))
1801 COMPLAIN("AutoCollapse");
1802 else {
1803 if (strcasecmp(WMGetFromPLString(value), "YES")==0) {
1804 dock->auto_collapse = 1;
1805 dock->collapsed = 1;
1811 /* restore auto-raise/lower state */
1813 value = WMGetFromPLDictionary(dock_state, dAutoRaiseLower);
1815 if (value) {
1816 if (!WMIsPLString(value))
1817 COMPLAIN("AutoRaiseLower");
1818 else {
1819 if (strcasecmp(WMGetFromPLString(value), "YES")==0) {
1820 dock->auto_raise_lower = 1;
1825 /* restore attract icons state */
1827 dock->attract_icons = 0;
1829 value = WMGetFromPLDictionary(dock_state, dAutoAttractIcons);
1831 if (value) {
1832 if (!WMIsPLString(value))
1833 COMPLAIN("AutoAttractIcons");
1834 else {
1835 if (strcasecmp(WMGetFromPLString(value), "YES")==0)
1836 dock->attract_icons = 1;
1841 /* application list */
1844 WMPropList *tmp;
1845 char buffer[64];
1848 * When saving, it saves the dock state in
1849 * Applications and Applicationsnnn
1851 * When loading, it will first try Applicationsnnn.
1852 * If it does not exist, use Applications as default.
1855 snprintf(buffer, sizeof(buffer), "Applications%i", scr->scr_height);
1857 tmp = WMCreatePLString(buffer);
1858 apps = WMGetFromPLDictionary(dock_state, tmp);
1859 WMReleasePropList(tmp);
1861 if (!apps) {
1862 apps = WMGetFromPLDictionary(dock_state, dApplications);
1866 if (!apps) {
1867 goto finish;
1870 count = WMGetPropListItemCount(apps);
1872 if (count==0)
1873 goto finish;
1875 old_top = dock->icon_array[0];
1877 /* dock->icon_count is set to 1 when dock is created.
1878 * Since Clip is already restored, we want to keep it so for clip,
1879 * but for dock we may change the default top tile, so we set it to 0.
1881 if (type == WM_DOCK)
1882 dock->icon_count = 0;
1884 for (i=0; i<count; i++) {
1885 if (dock->icon_count >= dock->max_icons) {
1886 wwarning(_("there are too many icons stored in dock. Ignoring what doesn't fit"));
1887 break;
1890 value = WMGetFromPLArray(apps, i);
1891 aicon = restore_icon_state(scr, value, type, dock->icon_count);
1893 dock->icon_array[dock->icon_count] = aicon;
1895 if (aicon) {
1896 aicon->dock = dock;
1897 aicon->x_pos = dock->x_pos + (aicon->xindex*ICON_SIZE);
1898 aicon->y_pos = dock->y_pos + (aicon->yindex*ICON_SIZE);
1900 if (dock->lowered)
1901 ChangeStackingLevel(aicon->icon->core, WMNormalLevel);
1902 else
1903 ChangeStackingLevel(aicon->icon->core, WMDockLevel);
1905 wCoreConfigure(aicon->icon->core, aicon->x_pos, aicon->y_pos,
1906 0, 0);
1908 if (!dock->collapsed)
1909 XMapWindow(dpy, aicon->icon->core->window);
1910 wRaiseFrame(aicon->icon->core);
1912 dock->icon_count++;
1913 } else if (dock->icon_count==0 && type==WM_DOCK)
1914 dock->icon_count++;
1917 /* if the first icon is not defined, use the default */
1918 if (dock->icon_array[0]==NULL) {
1919 /* update default icon */
1920 old_top->x_pos = dock->x_pos;
1921 old_top->y_pos = dock->y_pos;
1922 if (dock->lowered)
1923 ChangeStackingLevel(old_top->icon->core, WMNormalLevel);
1924 else
1925 ChangeStackingLevel(old_top->icon->core, WMDockLevel);
1926 dock->icon_array[0] = old_top;
1927 XMoveWindow(dpy, old_top->icon->core->window, dock->x_pos, dock->y_pos);
1928 /* we don't need to increment dock->icon_count here because it was
1929 * incremented in the loop above.
1931 } else if (old_top!=dock->icon_array[0]) {
1932 if (old_top == scr->clip_icon)
1933 scr->clip_icon = dock->icon_array[0];
1934 wAppIconDestroy(old_top);
1937 finish:
1938 if (dock_state)
1939 WMReleasePropList(dock_state);
1941 return dock;
1946 void
1947 wDockLaunchWithState(WDock *dock, WAppIcon *btn, WSavedState *state)
1949 if (btn && btn->command && !btn->running && !btn->launching) {
1951 btn->drop_launch = 0;
1952 btn->paste_launch = 0;
1954 btn->pid = execCommand(btn, btn->command, state);
1956 if (btn->pid>0) {
1957 if (!btn->forced_dock && !btn->buggy_app) {
1958 btn->launching = 1;
1959 dockIconPaint(btn);
1962 } else {
1963 wfree(state);
1968 void
1969 wDockDoAutoLaunch(WDock *dock, int workspace)
1971 WAppIcon *btn;
1972 WSavedState *state;
1973 int i;
1975 for (i = 0; i < dock->max_icons; i++) {
1976 btn = dock->icon_array[i];
1977 if (!btn || !btn->auto_launch)
1978 continue;
1980 state = wmalloc(sizeof(WSavedState));
1981 memset(state, 0, sizeof(WSavedState));
1982 state->workspace = workspace;
1983 /* TODO: this is klugy and is very difficult to understand
1984 * what's going on. Try to clean up */
1985 wDockLaunchWithState(dock, btn, state);
1990 #ifdef OFFIX_DND
1991 static WDock*
1992 findDock(WScreen *scr, XEvent *event, int *icon_pos)
1994 WDock *dock;
1995 int i;
1997 *icon_pos = -1;
1998 if ((dock = scr->dock)!=NULL) {
1999 for (i=0; i<dock->max_icons; i++) {
2000 if (dock->icon_array[i]
2001 && dock->icon_array[i]->icon->core->window==event->xclient.window) {
2002 *icon_pos = i;
2003 break;
2007 if (*icon_pos<0 && (dock = scr->workspaces[scr->current_workspace]->clip)!=NULL) {
2008 for (i=0; i<dock->max_icons; i++) {
2009 if (dock->icon_array[i]
2010 && dock->icon_array[i]->icon->core->window==event->xclient.window) {
2011 *icon_pos = i;
2012 break;
2016 if(*icon_pos>=0)
2017 return dock;
2018 return NULL;
2023 wDockReceiveDNDDrop(WScreen *scr, XEvent *event)
2025 WDock *dock;
2026 WAppIcon *btn;
2027 int icon_pos;
2029 dock = findDock(scr, event, &icon_pos);
2030 if (!dock)
2031 return False;
2034 * Return True if the drop was on an application icon window.
2035 * In this case, let the ClientMessage handler redirect the
2036 * message to the app.
2038 if (dock->icon_array[icon_pos]->icon->icon_win!=None)
2039 return True;
2041 if (dock->icon_array[icon_pos]->dnd_command!=NULL) {
2042 scr->flags.dnd_data_convertion_status = 0;
2044 btn = dock->icon_array[icon_pos];
2046 if (!btn->forced_dock) {
2047 btn->relaunching = btn->running;
2048 btn->running = 1;
2050 if (btn->wm_instance || btn->wm_class) {
2051 WWindowAttributes attr;
2052 memset(&attr, 0, sizeof(WWindowAttributes));
2053 wDefaultFillAttributes(btn->icon->core->screen_ptr,
2054 btn->wm_instance,
2055 btn->wm_class, &attr, NULL, True);
2057 if (!attr.no_appicon)
2058 btn->launching = 1;
2059 else
2060 btn->running = 0;
2063 btn->paste_launch = 0;
2064 btn->drop_launch = 1;
2065 scr->last_dock = dock;
2066 btn->pid = execCommand(btn, btn->dnd_command, NULL);
2067 if (btn->pid>0) {
2068 dockIconPaint(btn);
2069 } else {
2070 btn->launching = 0;
2071 if (!btn->relaunching) {
2072 btn->running = 0;
2076 return False;
2078 #endif /* OFFIX_DND */
2082 Bool
2083 wDockAttachIcon(WDock *dock, WAppIcon *icon, int x, int y)
2085 WWindow *wwin;
2086 char **argv;
2087 int argc;
2088 int index;
2090 wwin = icon->icon->owner;
2091 if (icon->command==NULL) {
2092 icon->editing = 0;
2093 if (XGetCommand(dpy, wwin->client_win, &argv, &argc) && argc>0) {
2095 icon->command = wtokenjoin(argv, argc);
2096 XFreeStringList(argv);
2097 } else {
2098 char *command=NULL;
2100 /* icon->forced_dock = 1;*/
2101 if (dock->type!=WM_CLIP || !icon->attracted) {
2102 icon->editing = 1;
2103 if (wInputDialog(dock->screen_ptr, _("Dock Icon"),
2104 _("Type the command used to launch the application"),
2105 &command)) {
2106 if (command && (command[0]==0 ||
2107 (command[0]=='-' && command[1]==0))) {
2108 wfree(command);
2109 command = NULL;
2111 icon->command = command;
2112 icon->editing = 0;
2113 } else {
2114 icon->editing = 0;
2115 if (command)
2116 wfree(command);
2117 /* If the target is the dock, reject the icon. If
2118 * the target is the clip, make it an attracted icon
2120 if (dock->type==WM_CLIP) {
2121 icon->attracted = 1;
2122 if (!icon->icon->shadowed) {
2123 icon->icon->shadowed = 1;
2124 icon->icon->force_paint = 1;
2126 } else {
2127 return False;
2132 } else {
2133 icon->editing = 0;
2136 for (index=1; index<dock->max_icons; index++)
2137 if (dock->icon_array[index] == NULL)
2138 break;
2139 /* if (index == dock->max_icons)
2140 return; */
2142 assert(index < dock->max_icons);
2144 dock->icon_array[index] = icon;
2145 icon->yindex = y;
2146 icon->xindex = x;
2148 icon->omnipresent = 0;
2150 icon->x_pos = dock->x_pos + x*ICON_SIZE;
2151 icon->y_pos = dock->y_pos + y*ICON_SIZE;
2153 dock->icon_count++;
2155 icon->running = 1;
2156 icon->launching = 0;
2157 icon->docked = 1;
2158 icon->dock = dock;
2159 icon->icon->core->descriptor.handle_mousedown = iconMouseDown;
2160 if (dock->type == WM_CLIP) {
2161 icon->icon->core->descriptor.handle_enternotify = clipEnterNotify;
2162 icon->icon->core->descriptor.handle_leavenotify = clipLeaveNotify;
2164 icon->icon->core->descriptor.parent_type = WCLASS_DOCK_ICON;
2165 icon->icon->core->descriptor.parent = icon;
2167 MoveInStackListUnder(dock->icon_array[index-1]->icon->core,
2168 icon->icon->core);
2169 wAppIconMove(icon, icon->x_pos, icon->y_pos);
2170 wAppIconPaint(icon);
2172 if (wPreferences.auto_arrange_icons)
2173 wArrangeIcons(dock->screen_ptr, True);
2175 #ifdef OFFIX_DND
2176 if (icon->command && !icon->dnd_command) {
2177 int len = strlen(icon->command)+8;
2178 icon->dnd_command = wmalloc(len);
2179 snprintf(icon->dnd_command, len, "%s %%d", icon->command);
2181 #endif
2183 if (icon->command && !icon->paste_command) {
2184 int len = strlen(icon->command)+8;
2185 icon->paste_command = wmalloc(len);
2186 snprintf(icon->paste_command, len, "%s %%s", icon->command);
2189 return True;
2193 void
2194 reattachIcon(WDock *dock, WAppIcon *icon, int x, int y)
2196 int index;
2198 for(index=1; index<dock->max_icons; index++) {
2199 if(dock->icon_array[index] == icon)
2200 break;
2202 assert(index < dock->max_icons);
2204 icon->yindex = y;
2205 icon->xindex = x;
2207 icon->x_pos = dock->x_pos + x*ICON_SIZE;
2208 icon->y_pos = dock->y_pos + y*ICON_SIZE;
2212 Bool
2213 moveIconBetweenDocks(WDock *src, WDock *dest, WAppIcon *icon, int x, int y)
2215 WWindow *wwin;
2216 char **argv;
2217 int argc;
2218 int index;
2220 if (src == dest)
2221 return True; /* No move needed, we're already there */
2223 if (dest == NULL)
2224 return False;
2226 wwin = icon->icon->owner;
2229 * For the moment we can't do this if we move icons in Clip from one
2230 * workspace to other, because if we move two or more icons without
2231 * command, the dialog box will not be able to tell us to which of the
2232 * moved icons it applies. -Dan
2234 if ((dest->type==WM_DOCK /*|| dest->keep_attracted*/) && icon->command==NULL) {
2235 if (XGetCommand(dpy, wwin->client_win, &argv, &argc) && argc>0) {
2237 icon->command = wtokenjoin(argv, argc);
2238 XFreeStringList(argv);
2239 } else {
2240 char *command=NULL;
2242 icon->editing = 1;
2243 /* icon->forced_dock = 1;*/
2244 if (wInputDialog(src->screen_ptr, _("Dock Icon"),
2245 _("Type the command used to launch the application"),
2246 &command)) {
2247 if (command && (command[0]==0 ||
2248 (command[0]=='-' && command[1]==0))) {
2249 wfree(command);
2250 command = NULL;
2252 icon->command = command;
2253 } else {
2254 icon->editing = 0;
2255 if (command)
2256 wfree(command);
2257 return False;
2259 icon->editing = 0;
2263 if (dest->type == WM_DOCK)
2264 wClipMakeIconOmnipresent(icon, False);
2266 for(index=1; index<src->max_icons; index++) {
2267 if(src->icon_array[index] == icon)
2268 break;
2270 assert(index < src->max_icons);
2272 src->icon_array[index] = NULL;
2273 src->icon_count--;
2275 for(index=1; index<dest->max_icons; index++) {
2276 if(dest->icon_array[index] == NULL)
2277 break;
2279 /* if (index == dest->max_icons)
2280 return; */
2282 assert(index < dest->max_icons);
2284 dest->icon_array[index] = icon;
2285 icon->dock = dest;
2287 /* deselect the icon */
2288 if (icon->icon->selected)
2289 wIconSelect(icon->icon);
2291 if (dest->type == WM_DOCK) {
2292 icon->icon->core->descriptor.handle_enternotify = NULL;
2293 icon->icon->core->descriptor.handle_leavenotify = NULL;
2294 } else {
2295 icon->icon->core->descriptor.handle_enternotify = clipEnterNotify;
2296 icon->icon->core->descriptor.handle_leavenotify = clipLeaveNotify;
2299 /* set it to be kept when moving to dock.
2300 * Unless the icon does not have a command set
2302 if (icon->command && dest->type==WM_DOCK) {
2303 icon->attracted = 0;
2304 if (icon->icon->shadowed) {
2305 icon->icon->shadowed = 0;
2306 icon->icon->force_paint = 1;
2310 if (src->auto_collapse || src->auto_raise_lower)
2311 clipLeave(src);
2313 icon->yindex = y;
2314 icon->xindex = x;
2316 icon->x_pos = dest->x_pos + x*ICON_SIZE;
2317 icon->y_pos = dest->y_pos + y*ICON_SIZE;
2319 dest->icon_count++;
2321 MoveInStackListUnder(dest->icon_array[index-1]->icon->core,
2322 icon->icon->core);
2323 wAppIconPaint(icon);
2325 return True;
2329 void
2330 wDockDetach(WDock *dock, WAppIcon *icon)
2332 int index;
2334 /* make the settings panel be closed */
2335 if (icon->panel) {
2336 DestroyDockAppSettingsPanel(icon->panel);
2339 /* This must be called before icon->dock is set to NULL.
2340 * Don't move it. -Dan
2342 wClipMakeIconOmnipresent(icon, False);
2344 icon->docked = 0;
2345 icon->dock = NULL;
2346 icon->attracted = 0;
2347 icon->auto_launch = 0;
2348 if (icon->icon->shadowed) {
2349 icon->icon->shadowed = 0;
2350 icon->icon->force_paint = 1;
2353 /* deselect the icon */
2354 if (icon->icon->selected)
2355 wIconSelect(icon->icon);
2357 if (icon->command) {
2358 wfree(icon->command);
2359 icon->command = NULL;
2361 #ifdef OFFIX_DND
2362 if (icon->dnd_command) {
2363 wfree(icon->dnd_command);
2364 icon->dnd_command = NULL;
2366 #endif
2367 if (icon->paste_command) {
2368 wfree(icon->paste_command);
2369 icon->paste_command = NULL;
2372 for (index=1; index<dock->max_icons; index++)
2373 if (dock->icon_array[index] == icon)
2374 break;
2375 assert(index < dock->max_icons);
2376 dock->icon_array[index] = NULL;
2377 icon->yindex = -1;
2378 icon->xindex = -1;
2380 dock->icon_count--;
2382 /* if the dock is not attached to an application or
2383 * the the application did not set the approriate hints yet,
2384 * destroy the icon */
2385 if (!icon->running || !wApplicationOf(icon->main_window))
2386 wAppIconDestroy(icon);
2387 else {
2388 icon->icon->core->descriptor.handle_mousedown = appIconMouseDown;
2389 icon->icon->core->descriptor.handle_enternotify = NULL;
2390 icon->icon->core->descriptor.handle_leavenotify = NULL;
2391 icon->icon->core->descriptor.parent_type = WCLASS_APPICON;
2392 icon->icon->core->descriptor.parent = icon;
2394 ChangeStackingLevel(icon->icon->core, NORMAL_ICON_LEVEL);
2396 wAppIconPaint(icon);
2397 if (wPreferences.auto_arrange_icons) {
2398 wArrangeIcons(dock->screen_ptr, True);
2399 } else {
2400 WAppIcon *bla = wAppIconNextSibling(icon);
2402 if (bla) {
2403 SlideWindow(icon->icon->core->window, icon->x_pos, icon->y_pos,
2404 bla->x_pos, bla->y_pos);
2405 wAppIconMove(icon, bla->x_pos, bla->y_pos);
2409 if (dock->auto_collapse || dock->auto_raise_lower)
2410 clipLeave(dock);
2415 * returns the closest Dock slot index for the passed
2416 * coordinates.
2418 * Returns False if icon can't be docked.
2420 * Note: this function should NEVER alter ret_x or ret_y, unless it will
2421 * return True. -Dan
2423 Bool
2424 wDockSnapIcon(WDock *dock, WAppIcon *icon, int req_x, int req_y,
2425 int *ret_x, int *ret_y, int redocking)
2427 WScreen *scr = dock->screen_ptr;
2428 int dx, dy;
2429 int ex_x, ex_y;
2430 int i, offset = ICON_SIZE/2;
2431 WAppIcon *aicon = NULL;
2432 WAppIcon *nicon = NULL;
2433 int max_y_icons, max_x_icons;
2435 max_x_icons = scr->scr_width/ICON_SIZE;
2436 max_y_icons = scr->scr_height/ICON_SIZE-1;
2438 if (wPreferences.flags.noupdates)
2439 return False;
2441 dx = dock->x_pos;
2442 dy = dock->y_pos;
2444 /* if the dock is full */
2445 if (!redocking &&
2446 (dock->icon_count >= dock->max_icons)) {
2447 return False;
2450 /* exact position */
2451 if (req_y < dy)
2452 ex_y = (req_y - offset - dy)/ICON_SIZE;
2453 else
2454 ex_y = (req_y + offset - dy)/ICON_SIZE;
2456 if (req_x < dx)
2457 ex_x = (req_x - offset - dx)/ICON_SIZE;
2458 else
2459 ex_x = (req_x + offset - dx)/ICON_SIZE;
2461 /* check if the icon is outside the screen boundaries */
2462 if (dx + ex_x*ICON_SIZE < -ICON_SIZE+2 ||
2463 dx + ex_x*ICON_SIZE >= scr->scr_width-1 ||
2464 dy + ex_y*ICON_SIZE < -ICON_SIZE+2 ||
2465 dy + ex_y*ICON_SIZE >= scr->scr_height-1)
2466 return False;
2468 if (dock->type == WM_DOCK) {
2469 if (icon->dock != dock && ex_x != 0)
2470 return False;
2472 aicon = NULL;
2473 for (i=0; i<dock->max_icons; i++) {
2474 nicon = dock->icon_array[i];
2475 if (nicon && nicon->yindex == ex_y) {
2476 aicon = nicon;
2477 break;
2481 if (redocking) {
2482 int sig, done, closest;
2484 /* Possible cases when redocking:
2486 * icon dragged out of range of any slot -> false
2487 * icon dragged to range of free slot
2488 * icon dragged to range of same slot
2489 * icon dragged to range of different icon
2491 if (abs(ex_x) > DOCK_DETTACH_THRESHOLD)
2492 return False;
2494 if (ex_y>=0 && ex_y<=max_y_icons && (aicon==icon || !aicon)) {
2495 *ret_x = 0;
2496 *ret_y = ex_y;
2497 return True;
2500 /* start looking at the upper slot or lower? */
2501 if (ex_y*ICON_SIZE < (req_y + offset - dy))
2502 sig = 1;
2503 else
2504 sig = -1;
2506 closest = -1;
2507 done = 0;
2508 /* look for closest free slot */
2509 for (i=0; i<(DOCK_DETTACH_THRESHOLD+1)*2 && !done; i++) {
2510 int j;
2512 done = 1;
2513 closest = sig*(i/2) + ex_y;
2514 /* check if this slot is used */
2515 if (closest >= 0) {
2516 for (j = 0; j<dock->max_icons; j++) {
2517 if (dock->icon_array[j]
2518 && dock->icon_array[j]->yindex==closest) {
2519 /* slot is used by someone else */
2520 if (dock->icon_array[j]!=icon)
2521 done = 0;
2522 break;
2526 sig = -sig;
2528 if (done && closest >= 0 && closest <= max_y_icons &&
2529 ((ex_y >= closest && ex_y - closest < DOCK_DETTACH_THRESHOLD+1)
2531 (ex_y < closest && closest - ex_y <= DOCK_DETTACH_THRESHOLD+1))) {
2532 *ret_x = 0;
2533 *ret_y = closest;
2534 return True;
2536 } else { /* !redocking */
2538 /* if slot is free and the icon is close enough, return it */
2539 if (!aicon && ex_x == 0 && ex_y >= 0 && ex_y <= max_y_icons) {
2540 *ret_x = 0;
2541 *ret_y = ex_y;
2542 return True;
2545 } else { /* CLIP */
2546 int neighbours = 0;
2547 int start, stop, k;
2549 start = icon->omnipresent ? 0 : scr->current_workspace;
2550 stop = icon->omnipresent ? scr->workspace_count : start+1;
2552 aicon = NULL;
2553 for (k=start; k<stop; k++) {
2554 WDock *tmp = scr->workspaces[k]->clip;
2555 if (!tmp)
2556 continue;
2557 for (i=0; i<tmp->max_icons; i++) {
2558 nicon = tmp->icon_array[i];
2559 if (nicon && nicon->xindex == ex_x && nicon->yindex == ex_y) {
2560 aicon = nicon;
2561 break;
2564 if (aicon)
2565 break;
2567 for (k=start; k<stop; k++) {
2568 WDock *tmp = scr->workspaces[k]->clip;
2569 if (!tmp)
2570 continue;
2571 for (i=0; i<tmp->max_icons; i++) {
2572 nicon = tmp->icon_array[i];
2573 if (nicon && nicon != icon && /* Icon can't be it's own neighbour */
2574 (abs(nicon->xindex - ex_x) <= CLIP_ATTACH_VICINITY &&
2575 abs(nicon->yindex - ex_y) <= CLIP_ATTACH_VICINITY)) {
2576 neighbours = 1;
2577 break;
2580 if (neighbours)
2581 break;
2584 if (neighbours && (aicon==NULL || (redocking && aicon == icon))) {
2585 *ret_x = ex_x;
2586 *ret_y = ex_y;
2587 return True;
2590 return False;
2594 #define ON_SCREEN(x, y, sx, ex, sy, ey) \
2595 ((((x)+ICON_SIZE/2) >= (sx)) && (((y)+ICON_SIZE/2) >= (sy)) && \
2596 (((x) + (ICON_SIZE/2)) <= (ex)) && (((y) + (ICON_SIZE/2)) <= ey))
2600 * returns true if it can find a free slot in the dock,
2601 * in which case it changes x_pos and y_pos accordingly.
2602 * Else returns false.
2604 Bool
2605 wDockFindFreeSlot(WDock *dock, int *x_pos, int *y_pos)
2607 WScreen *scr = dock->screen_ptr;
2608 WAppIcon *btn;
2609 WAppIconChain *chain;
2610 unsigned char *slot_map;
2611 int mwidth;
2612 int r;
2613 int x, y;
2614 int i, done = False;
2615 int corner;
2616 int sx=0, sy=0, ex=scr->scr_width, ey=scr->scr_height;
2617 int extra_count=0;
2619 if (dock->type == WM_CLIP &&
2620 dock != scr->workspaces[scr->current_workspace]->clip)
2621 extra_count = scr->global_icon_count;
2623 /* if the dock is full */
2624 if (dock->icon_count+extra_count >= dock->max_icons) {
2625 return False;
2628 if (!wPreferences.flags.nodock && scr->dock) {
2629 if (scr->dock->on_right_side)
2630 ex -= ICON_SIZE + DOCK_EXTRA_SPACE;
2631 else
2632 sx += ICON_SIZE + DOCK_EXTRA_SPACE;
2635 if (ex < dock->x_pos)
2636 ex = dock->x_pos;
2637 if (sx > dock->x_pos+ICON_SIZE)
2638 sx = dock->x_pos+ICON_SIZE;
2639 #define C_NONE 0
2640 #define C_NW 1
2641 #define C_NE 2
2642 #define C_SW 3
2643 #define C_SE 4
2645 /* check if clip is in a corner */
2646 if (dock->type==WM_CLIP) {
2647 if (dock->x_pos < 1 && dock->y_pos < 1)
2648 corner = C_NE;
2649 else if (dock->x_pos < 1 && dock->y_pos >= (ey-ICON_SIZE))
2650 corner = C_SE;
2651 else if (dock->x_pos >= (ex-ICON_SIZE)&& dock->y_pos >= (ey-ICON_SIZE))
2652 corner = C_SW;
2653 else if (dock->x_pos >= (ex-ICON_SIZE) && dock->y_pos < 1)
2654 corner = C_NW;
2655 else
2656 corner = C_NONE;
2657 } else
2658 corner = C_NONE;
2660 /* If the clip is in the corner, use only slots that are in the border
2661 * of the screen */
2662 if (corner!=C_NONE) {
2663 char *hmap, *vmap;
2664 int hcount, vcount;
2666 hcount = WMIN(dock->max_icons, scr->scr_width/ICON_SIZE);
2667 vcount = WMIN(dock->max_icons, scr->scr_height/ICON_SIZE);
2668 hmap = wmalloc(hcount+1);
2669 memset(hmap, 0, hcount+1);
2670 vmap = wmalloc(vcount+1);
2671 memset(vmap, 0, vcount+1);
2673 /* mark used positions */
2674 switch (corner) {
2675 case C_NE:
2676 for (i=0; i<dock->max_icons; i++) {
2677 btn = dock->icon_array[i];
2678 if (!btn)
2679 continue;
2681 if (btn->xindex==0 && btn->yindex > 0 && btn->yindex < vcount)
2682 vmap[btn->yindex] = 1;
2683 else if (btn->yindex==0 && btn->xindex>0 && btn->xindex<hcount)
2684 hmap[btn->xindex] = 1;
2686 for (chain=scr->global_icons; chain!=NULL; chain=chain->next) {
2687 btn = chain->aicon;
2688 if (btn->xindex==0 && btn->yindex > 0 && btn->yindex < vcount)
2689 vmap[btn->yindex] = 1;
2690 else if (btn->yindex==0 && btn->xindex>0 && btn->xindex<hcount)
2691 hmap[btn->xindex] = 1;
2693 break;
2694 case C_NW:
2695 for (i=0; i<dock->max_icons; i++) {
2696 btn = dock->icon_array[i];
2697 if (!btn)
2698 continue;
2700 if (btn->xindex==0 && btn->yindex > 0 && btn->yindex < vcount)
2701 vmap[btn->yindex] = 1;
2702 else if (btn->yindex==0 && btn->xindex<0 &&btn->xindex>-hcount)
2703 hmap[-btn->xindex] = 1;
2705 for (chain=scr->global_icons; chain!=NULL; chain=chain->next) {
2706 btn = chain->aicon;
2707 if (btn->xindex==0 && btn->yindex > 0 && btn->yindex < vcount)
2708 vmap[btn->yindex] = 1;
2709 else if (btn->yindex==0 && btn->xindex<0 &&btn->xindex>-hcount)
2710 hmap[-btn->xindex] = 1;
2712 break;
2713 case C_SE:
2714 for (i=0; i<dock->max_icons; i++) {
2715 btn = dock->icon_array[i];
2716 if (!btn)
2717 continue;
2719 if (btn->xindex==0 && btn->yindex < 0 && btn->yindex > -vcount)
2720 vmap[-btn->yindex] = 1;
2721 else if (btn->yindex==0 && btn->xindex>0 && btn->xindex<hcount)
2722 hmap[btn->xindex] = 1;
2724 for (chain=scr->global_icons; chain!=NULL; chain=chain->next) {
2725 btn = chain->aicon;
2726 if (btn->xindex==0 && btn->yindex < 0 && btn->yindex > -vcount)
2727 vmap[-btn->yindex] = 1;
2728 else if (btn->yindex==0 && btn->xindex>0 && btn->xindex<hcount)
2729 hmap[btn->xindex] = 1;
2731 break;
2732 case C_SW:
2733 default:
2734 for (i=0; i<dock->max_icons; i++) {
2735 btn = dock->icon_array[i];
2736 if (!btn)
2737 continue;
2739 if (btn->xindex==0 && btn->yindex < 0 && btn->yindex > -vcount)
2740 vmap[-btn->yindex] = 1;
2741 else if (btn->yindex==0 && btn->xindex<0 &&btn->xindex>-hcount)
2742 hmap[-btn->xindex] = 1;
2744 for (chain=scr->global_icons; chain!=NULL; chain=chain->next) {
2745 btn = chain->aicon;
2746 if (btn->xindex==0 && btn->yindex < 0 && btn->yindex > -vcount)
2747 vmap[-btn->yindex] = 1;
2748 else if (btn->yindex==0 && btn->xindex<0 &&btn->xindex>-hcount)
2749 hmap[-btn->xindex] = 1;
2752 x=0; y=0;
2753 done = 0;
2754 /* search a vacant slot */
2755 for (i=1; i<WMAX(vcount, hcount); i++) {
2756 if (i < vcount && vmap[i]==0) {
2757 /* found a slot */
2758 x = 0;
2759 y = i;
2760 done = 1;
2761 break;
2762 } else if (i < hcount && hmap[i]==0) {
2763 /* found a slot */
2764 x = i;
2765 y = 0;
2766 done = 1;
2767 break;
2770 wfree(vmap);
2771 wfree(hmap);
2772 /* If found a slot, translate and return */
2773 if (done) {
2774 if (corner==C_NW || corner==C_NE) {
2775 *y_pos = y;
2776 } else {
2777 *y_pos = -y;
2779 if (corner==C_NE || corner==C_SE) {
2780 *x_pos = x;
2781 } else {
2782 *x_pos = -x;
2784 return True;
2786 /* else, try to find a slot somewhere else */
2789 /* a map of mwidth x mwidth would be enough if we allowed icons to be
2790 * placed outside of screen */
2791 mwidth = (int)ceil(sqrt(dock->max_icons));
2793 /* In the worst case (the clip is in the corner of the screen),
2794 * the amount of icons that fit in the clip is smaller.
2795 * Double the map to get a safe value.
2797 mwidth += mwidth;
2799 r = (mwidth-1)/2;
2801 slot_map = wmalloc(mwidth*mwidth);
2802 memset(slot_map, 0, mwidth*mwidth);
2804 #define XY2OFS(x,y) (WMAX(abs(x),abs(y)) > r) ? 0 : (((y)+r)*(mwidth)+(x)+r)
2806 /* mark used slots in the map. If the slot falls outside the map
2807 * (for example, when all icons are placed in line), ignore them. */
2808 for (i=0; i<dock->max_icons; i++) {
2809 btn = dock->icon_array[i];
2810 if (btn)
2811 slot_map[XY2OFS(btn->xindex, btn->yindex)] = 1;
2813 for (chain=scr->global_icons; chain!=NULL; chain=chain->next) {
2814 slot_map[XY2OFS(chain->aicon->xindex, chain->aicon->yindex)] = 1;
2816 /* Find closest slot from the center that is free by scanning the
2817 * map from the center to outward in circular passes.
2818 * This will not result in a neat layout, but will be optimal
2819 * in the sense that there will not be holes left.
2821 done = 0;
2822 for (i = 1; i <= r && !done; i++) {
2823 int tx, ty;
2825 /* top and bottom parts of the ring */
2826 for (x = -i; x <= i && !done; x++) {
2827 tx = dock->x_pos + x*ICON_SIZE;
2828 y = -i;
2829 ty = dock->y_pos + y*ICON_SIZE;
2830 if (slot_map[XY2OFS(x,y)]==0
2831 && ON_SCREEN(tx, ty, sx, ex, sy, ey)) {
2832 *x_pos = x;
2833 *y_pos = y;
2834 done = 1;
2835 break;
2837 y = i;
2838 ty = dock->y_pos + y*ICON_SIZE;
2839 if (slot_map[XY2OFS(x,y)]==0
2840 && ON_SCREEN(tx, ty, sx, ex, sy, ey)) {
2841 *x_pos = x;
2842 *y_pos = y;
2843 done = 1;
2844 break;
2847 /* left and right parts of the ring */
2848 for (y = -i+1; y <= i-1; y++) {
2849 ty = dock->y_pos + y*ICON_SIZE;
2850 x = -i;
2851 tx = dock->x_pos + x*ICON_SIZE;
2852 if (slot_map[XY2OFS(x,y)]==0
2853 && ON_SCREEN(tx, ty, sx, ex, sy, ey)) {
2854 *x_pos = x;
2855 *y_pos = y;
2856 done = 1;
2857 break;
2859 x = i;
2860 tx = dock->x_pos + x*ICON_SIZE;
2861 if (slot_map[XY2OFS(x,y)]==0
2862 && ON_SCREEN(tx, ty, sx, ex, sy, ey)) {
2863 *x_pos = x;
2864 *y_pos = y;
2865 done = 1;
2866 break;
2870 wfree(slot_map);
2871 #undef XY2OFS
2872 return done;
2876 static void
2877 moveDock(WDock *dock, int new_x, int new_y)
2879 WAppIcon *btn;
2880 int i;
2882 dock->x_pos = new_x;
2883 dock->y_pos = new_y;
2884 for (i=0; i<dock->max_icons; i++) {
2885 btn = dock->icon_array[i];
2886 if (btn) {
2887 btn->x_pos = new_x + btn->xindex*ICON_SIZE;
2888 btn->y_pos = new_y + btn->yindex*ICON_SIZE;
2889 XMoveWindow(dpy, btn->icon->core->window, btn->x_pos, btn->y_pos);
2895 static void
2896 swapDock(WDock *dock)
2898 WScreen *scr = dock->screen_ptr;
2899 WAppIcon *btn;
2900 int x, i;
2903 if (dock->on_right_side) {
2904 x = dock->x_pos = scr->scr_width - ICON_SIZE - DOCK_EXTRA_SPACE;
2905 } else {
2906 x = dock->x_pos = DOCK_EXTRA_SPACE;
2909 for (i=0; i<dock->max_icons; i++) {
2910 btn = dock->icon_array[i];
2911 if (btn) {
2912 btn->x_pos = x;
2913 XMoveWindow(dpy, btn->icon->core->window, btn->x_pos, btn->y_pos);
2917 wScreenUpdateUsableArea(scr);
2921 static pid_t
2922 execCommand(WAppIcon *btn, char *command, WSavedState *state)
2924 WScreen *scr = btn->icon->core->screen_ptr;
2925 pid_t pid;
2926 char **argv;
2927 int argc;
2928 char *cmdline;
2930 cmdline = ExpandOptions(scr, command);
2932 if (scr->flags.dnd_data_convertion_status || !cmdline) {
2933 if (cmdline)
2934 wfree(cmdline);
2935 if (state)
2936 wfree(state);
2937 return 0;
2940 wtokensplit(cmdline, &argv, &argc);
2942 if (argv==NULL) {
2943 if (cmdline)
2944 wfree(cmdline);
2945 if (state)
2946 wfree(state);
2947 return 0;
2950 if ((pid=fork())==0) {
2951 char **args;
2952 int i;
2954 SetupEnvironment(scr);
2956 #ifdef HAVE_SETPGID
2957 setpgid(0, 0);
2958 #endif
2960 args = malloc(sizeof(char*)*(argc+1));
2961 if (!args)
2962 exit(111);
2963 for (i=0; i<argc; i++) {
2964 args[i] = argv[i];
2966 args[argc] = NULL;
2967 execvp(argv[0], args);
2968 exit(111);
2970 wtokenfree(argv, argc);
2972 if (pid > 0) {
2973 if (!state) {
2974 state = wmalloc(sizeof(WSavedState));
2975 memset(state, 0, sizeof(WSavedState));
2976 state->hidden = -1;
2977 state->miniaturized = -1;
2978 state->shaded = -1;
2979 if (btn->dock==scr->dock || btn->omnipresent)
2980 state->workspace = -1;
2981 else
2982 state->workspace = scr->current_workspace;
2984 wWindowAddSavedState(btn->wm_instance, btn->wm_class, cmdline, pid,
2985 state);
2986 wAddDeathHandler(pid, (WDeathHandler*)trackDeadProcess,
2987 btn->dock);
2988 } else if (state) {
2989 wfree(state);
2991 wfree(cmdline);
2992 return pid;
2996 void
2997 wDockHideIcons(WDock *dock)
2999 int i;
3001 if (dock==NULL)
3002 return;
3004 for (i=1; i<dock->max_icons; i++) {
3005 if (dock->icon_array[i])
3006 XUnmapWindow(dpy, dock->icon_array[i]->icon->core->window);
3008 dock->mapped = 0;
3010 dockIconPaint(dock->icon_array[0]);
3014 void
3015 wDockShowIcons(WDock *dock)
3017 int i, newlevel;
3018 WAppIcon *btn;
3020 if (dock==NULL)
3021 return;
3023 btn = dock->icon_array[0];
3024 moveDock(dock, btn->x_pos, btn->y_pos);
3026 newlevel = dock->lowered ? WMNormalLevel : WMDockLevel;
3027 ChangeStackingLevel(btn->icon->core, newlevel);
3029 for (i=1; i<dock->max_icons; i++) {
3030 if (dock->icon_array[i]) {
3031 MoveInStackListAbove(dock->icon_array[i]->icon->core,
3032 btn->icon->core);
3033 break;
3037 if (!dock->collapsed) {
3038 for (i=1; i<dock->max_icons; i++) {
3039 if (dock->icon_array[i]) {
3040 XMapWindow(dpy, dock->icon_array[i]->icon->core->window);
3044 dock->mapped = 1;
3046 dockIconPaint(btn);
3050 void
3051 wDockLower(WDock *dock)
3053 int i;
3055 for (i=0; i<dock->max_icons; i++) {
3056 if (dock->icon_array[i])
3057 wLowerFrame(dock->icon_array[i]->icon->core);
3062 void
3063 wDockRaise(WDock *dock)
3065 int i;
3067 for (i=dock->max_icons-1; i>=0; i--) {
3068 if (dock->icon_array[i])
3069 wRaiseFrame(dock->icon_array[i]->icon->core);
3074 void
3075 wDockRaiseLower(WDock *dock)
3077 if (!dock->icon_array[0]->icon->core->stacking->above
3078 ||(dock->icon_array[0]->icon->core->stacking->window_level
3079 !=dock->icon_array[0]->icon->core->stacking->above->stacking->window_level))
3080 wDockLower(dock);
3081 else
3082 wDockRaise(dock);
3086 void
3087 wDockFinishLaunch(WDock *dock, WAppIcon *icon)
3089 icon->launching = 0;
3090 icon->relaunching = 0;
3091 dockIconPaint(icon);
3095 WAppIcon*
3096 wDockFindIconForWindow(WDock *dock, Window window)
3098 WAppIcon *icon;
3099 int i;
3101 for (i=0; i<dock->max_icons; i++) {
3102 icon = dock->icon_array[i];
3103 if (icon && icon->main_window == window)
3104 return icon;
3106 return NULL;
3110 void
3111 wDockTrackWindowLaunch(WDock *dock, Window window)
3113 WAppIcon *icon;
3114 char *wm_class, *wm_instance;
3115 int i;
3116 Bool firstPass = True;
3117 Bool found = False;
3118 char *command = NULL;
3121 int argc;
3122 char **argv;
3124 if (XGetCommand(dpy, window, &argv, &argc)) {
3125 if (argc > 0 && argv != NULL)
3126 command = wtokenjoin(argv,argc);
3127 if (argv) {
3128 XFreeStringList(argv);
3133 if (!PropGetWMClass(window, &wm_class, &wm_instance) ||
3134 (!wm_class && !wm_instance))
3135 return;
3137 retry:
3138 for (i=0; i<dock->max_icons; i++) {
3139 icon = dock->icon_array[i];
3140 if (!icon)
3141 continue;
3143 /* app is already attached to icon */
3144 if (icon->main_window == window) {
3145 found = True;
3146 break;
3149 if ((icon->wm_instance || icon->wm_class)
3150 && (icon->launching
3151 || (dock->screen_ptr->flags.startup && !icon->running))) {
3153 if (icon->wm_instance && wm_instance &&
3154 strcmp(icon->wm_instance, wm_instance)!=0) {
3155 continue;
3157 if (icon->wm_class && wm_class &&
3158 strcmp(icon->wm_class, wm_class)!=0) {
3159 continue;
3161 if (firstPass && command && strcmp(icon->command, command)!=0) {
3162 continue;
3165 if (!icon->relaunching) {
3166 WApplication *wapp;
3168 /* Possibly an application that was docked with dockit,
3169 * but the user did not update WMState to indicate that
3170 * it was docked by force */
3171 wapp = wApplicationOf(window);
3172 if (!wapp) {
3173 icon->forced_dock = 1;
3174 icon->running = 0;
3176 if (!icon->forced_dock)
3177 icon->main_window = window;
3180 found = True;
3181 wDockFinishLaunch(dock, icon);
3182 break;
3186 if (firstPass && !found) {
3187 firstPass = False;
3188 goto retry;
3191 if (command)
3192 wfree(command);
3194 if (wm_class)
3195 XFree(wm_class);
3196 if (wm_instance)
3197 XFree(wm_instance);
3202 void
3203 wClipUpdateForWorkspaceChange(WScreen *scr, int workspace)
3205 if (!wPreferences.flags.noclip) {
3206 scr->clip_icon->dock = scr->workspaces[workspace]->clip;
3207 if (scr->current_workspace != workspace) {
3208 WDock *old_clip = scr->workspaces[scr->current_workspace]->clip;
3209 WAppIconChain *chain = scr->global_icons;
3211 while (chain) {
3212 moveIconBetweenDocks(chain->aicon->dock,
3213 scr->workspaces[workspace]->clip,
3214 chain->aicon, chain->aicon->xindex,
3215 chain->aicon->yindex);
3216 if (scr->workspaces[workspace]->clip->collapsed)
3217 XUnmapWindow(dpy, chain->aicon->icon->core->window);
3218 chain = chain->next;
3221 wDockHideIcons(old_clip);
3222 if (old_clip->auto_raise_lower) {
3223 if (old_clip->auto_raise_magic) {
3224 WMDeleteTimerHandler(old_clip->auto_raise_magic);
3225 old_clip->auto_raise_magic = NULL;
3227 wDockLower(old_clip);
3229 if (old_clip->auto_collapse) {
3230 if (old_clip->auto_expand_magic) {
3231 WMDeleteTimerHandler(old_clip->auto_expand_magic);
3232 old_clip->auto_expand_magic = NULL;
3234 old_clip->collapsed = 1;
3236 wDockShowIcons(scr->workspaces[workspace]->clip);
3238 if (scr->flags.clip_balloon_mapped)
3239 showClipBalloon(scr->clip_icon->dock, workspace);
3245 static void
3246 trackDeadProcess(pid_t pid, unsigned char status, WDock *dock)
3248 WAppIcon *icon;
3249 int i;
3251 for (i=0; i<dock->max_icons; i++) {
3252 icon = dock->icon_array[i];
3253 if (!icon)
3254 continue;
3256 if (icon->launching && icon->pid == pid) {
3257 if (!icon->relaunching) {
3258 icon->running = 0;
3259 icon->main_window = None;
3261 wDockFinishLaunch(dock, icon);
3262 icon->pid = 0;
3263 if (status==111) {
3264 char msg[PATH_MAX];
3266 if (icon->drop_launch)
3267 snprintf(msg, sizeof(msg), _("Could not execute command \"%s\""),
3268 icon->dnd_command);
3269 else if (icon->paste_launch)
3270 snprintf(msg, sizeof(msg), _("Could not execute command \"%s\""),
3271 icon->paste_command);
3272 else
3273 snprintf(msg, sizeof(msg), _("Could not execute command \"%s\""),
3274 icon->command);
3276 wMessageDialog(dock->screen_ptr, _("Error"), msg,
3277 _("OK"), NULL, NULL);
3279 break;
3285 static void
3286 toggleLowered(WDock *dock)
3288 WAppIcon *tmp;
3289 int newlevel, i;
3291 /* lower/raise Dock */
3292 if (!dock->lowered) {
3293 newlevel = WMNormalLevel;
3294 dock->lowered = 1;
3295 } else {
3296 newlevel = WMDockLevel;
3297 dock->lowered = 0;
3300 for (i=0; i<dock->max_icons; i++) {
3301 tmp = dock->icon_array[i];
3302 if (!tmp)
3303 continue;
3305 ChangeStackingLevel(tmp->icon->core, newlevel);
3306 if (dock->lowered)
3307 wLowerFrame(tmp->icon->core);
3310 if (dock->type == WM_DOCK)
3311 wScreenUpdateUsableArea(dock->screen_ptr);
3315 static void
3316 toggleCollapsed(WDock *dock)
3318 if (dock->collapsed) {
3319 dock->collapsed = 0;
3320 wDockShowIcons(dock);
3322 else {
3323 dock->collapsed = 1;
3324 wDockHideIcons(dock);
3329 static void
3330 openDockMenu(WDock *dock, WAppIcon *aicon, XEvent *event)
3332 WScreen *scr = dock->screen_ptr;
3333 WObjDescriptor *desc;
3334 WMenuEntry *entry;
3335 WApplication *wapp = NULL;
3336 int index = 0;
3337 int x_pos;
3338 int n_selected;
3339 int appIsRunning = aicon->running && aicon->icon && aicon->icon->owner;
3341 if (dock->type == WM_DOCK) {
3342 /* keep on top */
3343 entry = dock->menu->entries[index];
3344 entry->flags.indicator_on = !dock->lowered;
3345 entry->clientdata = dock;
3346 dock->menu->flags.realized = 0;
3347 } else {
3348 /* clip options */
3349 if (scr->clip_options)
3350 updateClipOptionsMenu(scr->clip_options, dock);
3352 n_selected = numberOfSelectedIcons(dock);
3354 /* Rename Workspace */
3355 entry = dock->menu->entries[++index];
3356 if (aicon == scr->clip_icon) {
3357 entry->callback = renameCallback;
3358 entry->clientdata = dock;
3359 entry->flags.indicator = 0;
3360 entry->text = _("Rename Workspace");
3361 } else {
3362 entry->callback = omnipresentCallback;
3363 entry->clientdata = aicon;
3364 if (n_selected > 0) {
3365 entry->flags.indicator = 0;
3366 entry->text = _("Toggle Omnipresent");
3367 } else {
3368 entry->flags.indicator = 1;
3369 entry->flags.indicator_on = aicon->omnipresent;
3370 entry->flags.indicator_type = MI_CHECK;
3371 entry->text = _("Omnipresent");
3375 /* select/unselect icon */
3376 entry = dock->menu->entries[++index];
3377 entry->clientdata = aicon;
3378 entry->flags.indicator_on = aicon->icon->selected;
3379 wMenuSetEnabled(dock->menu, index, aicon!=scr->clip_icon);
3381 /* select/unselect all icons */
3382 entry = dock->menu->entries[++index];
3383 entry->clientdata = aicon;
3384 if (n_selected > 0)
3385 entry->text = _("Unselect All Icons");
3386 else
3387 entry->text = _("Select All Icons");
3388 wMenuSetEnabled(dock->menu, index, dock->icon_count > 1);
3390 /* keep icon(s) */
3391 entry = dock->menu->entries[++index];
3392 entry->clientdata = aicon;
3393 if (n_selected > 1)
3394 entry->text = _("Keep Icons");
3395 else
3396 entry->text = _("Keep Icon");
3397 wMenuSetEnabled(dock->menu, index, dock->icon_count > 1);
3399 /* this is the workspace submenu part */
3400 entry = dock->menu->entries[++index];
3401 if (n_selected > 1)
3402 entry->text = _("Move Icons To");
3403 else
3404 entry->text = _("Move Icon To");
3405 if (scr->clip_submenu)
3406 updateWorkspaceMenu(scr->clip_submenu, aicon);
3407 wMenuSetEnabled(dock->menu, index, !aicon->omnipresent);
3409 /* remove icon(s) */
3410 entry = dock->menu->entries[++index];
3411 entry->clientdata = aicon;
3412 if (n_selected > 1)
3413 entry->text = _("Remove Icons");
3414 else
3415 entry->text = _("Remove Icon");
3416 wMenuSetEnabled(dock->menu, index, dock->icon_count > 1);
3418 /* attract icon(s) */
3419 entry = dock->menu->entries[++index];
3420 entry->clientdata = aicon;
3422 dock->menu->flags.realized = 0;
3423 wMenuRealize(dock->menu);
3427 if (aicon->icon->owner) {
3428 wapp = wApplicationOf(aicon->icon->owner->main_window);
3429 } else {
3430 wapp = NULL;
3433 /* launch */
3434 entry = dock->menu->entries[++index];
3435 entry->clientdata = aicon;
3436 wMenuSetEnabled(dock->menu, index, aicon->command!=NULL);
3438 /* unhide here */
3439 entry = dock->menu->entries[++index];
3440 entry->clientdata = aicon;
3441 if (wapp && wapp->flags.hidden) {
3442 entry->text = _("Unhide Here");
3443 } else {
3444 entry->text = _("Bring Here");
3446 wMenuSetEnabled(dock->menu, index, appIsRunning);
3448 /* hide */
3449 entry = dock->menu->entries[++index];
3450 entry->clientdata = aicon;
3451 if (wapp && wapp->flags.hidden) {
3452 entry->text = _("Unhide");
3453 } else {
3454 entry->text = _("Hide");
3456 wMenuSetEnabled(dock->menu, index, appIsRunning);
3458 /* settings */
3459 entry = dock->menu->entries[++index];
3460 entry->clientdata = aicon;
3461 wMenuSetEnabled(dock->menu, index, !aicon->editing
3462 && !wPreferences.flags.noupdates);
3464 /* kill */
3465 entry = dock->menu->entries[++index];
3466 entry->clientdata = aicon;
3467 wMenuSetEnabled(dock->menu, index, appIsRunning);
3469 if (!dock->menu->flags.realized)
3470 wMenuRealize(dock->menu);
3472 if (dock->type == WM_CLIP) {
3473 x_pos = event->xbutton.x_root+2;
3474 } else {
3475 x_pos = dock->on_right_side ?
3476 scr->scr_width - dock->menu->frame->core->width - 2 : 0;
3479 wMenuMapAt(dock->menu, x_pos, event->xbutton.y_root+2, False);
3481 /* allow drag select */
3482 event->xany.send_event = True;
3483 desc = &dock->menu->menu->descriptor;
3484 (*desc->handle_mousedown)(desc, event);
3488 static void
3489 openClipWorkspaceMenu(WScreen *scr, int x, int y)
3491 if (!scr->clip_ws_menu) {
3492 scr->clip_ws_menu = wWorkspaceMenuMake(scr, False);
3494 wWorkspaceMenuUpdate(scr, scr->clip_ws_menu);
3495 wMenuMapAt(scr->clip_ws_menu, x, y, False);
3499 /******************************************************************/
3500 static void
3501 iconDblClick(WObjDescriptor *desc, XEvent *event)
3503 WAppIcon *btn = desc->parent;
3504 WDock *dock = btn->dock;
3505 WApplication *wapp = NULL;
3506 int unhideHere = 0;
3508 if (btn->icon->owner && !(event->xbutton.state & ControlMask)) {
3509 wapp = wApplicationOf(btn->icon->owner->main_window);
3511 assert(wapp!=NULL);
3513 unhideHere = (event->xbutton.state & ShiftMask);
3515 /* go to the last workspace that the user worked on the app */
3516 if (wapp->last_workspace != dock->screen_ptr->current_workspace
3517 && !unhideHere) {
3518 wWorkspaceChange(dock->screen_ptr, wapp->last_workspace);
3521 wUnhideApplication(wapp, event->xbutton.button==Button2,
3522 unhideHere);
3524 if (event->xbutton.state & MOD_MASK) {
3525 wHideOtherApplications(btn->icon->owner);
3527 } else {
3528 if (event->xbutton.button==Button1) {
3530 if (event->xbutton.state & MOD_MASK) {
3531 /* raise/lower dock */
3532 toggleLowered(dock);
3533 } else if (btn == dock->screen_ptr->clip_icon) {
3534 if (getClipButton(event->xbutton.x, event->xbutton.y)==CLIP_IDLE)
3535 toggleCollapsed(dock);
3536 else
3537 handleClipChangeWorkspace(dock->screen_ptr, event);
3538 } else if (btn->command) {
3539 if (!btn->launching &&
3540 (!btn->running || (event->xbutton.state & ControlMask))) {
3541 launchDockedApplication(btn, False);
3543 } else if (btn->xindex == 0 && btn->yindex == 0
3544 && btn->dock->type == WM_DOCK) {
3546 wShowGNUstepPanel(dock->screen_ptr);
3554 static void
3555 handleDockMove(WDock *dock, WAppIcon *aicon, XEvent *event)
3557 WScreen *scr = dock->screen_ptr;
3558 int ofs_x=event->xbutton.x, ofs_y=event->xbutton.y;
3559 int x, y;
3560 XEvent ev;
3561 int grabbed = 0, swapped = 0, done;
3562 Pixmap ghost = None;
3563 int superfluous = wPreferences.superfluous; /* we catch it to avoid problems */
3565 #ifdef DEBUG
3566 puts("moving dock");
3567 #endif
3568 if (XGrabPointer(dpy, aicon->icon->core->window, True, ButtonMotionMask
3569 |ButtonReleaseMask|ButtonPressMask, GrabModeAsync,
3570 GrabModeAsync, None, None, CurrentTime) !=GrabSuccess) {
3571 wwarning("pointer grab failed for dock move");
3573 y = 0;
3574 for (x=0; x<dock->max_icons; x++) {
3575 if (dock->icon_array[x]!=NULL &&
3576 dock->icon_array[x]->yindex > y)
3577 y = dock->icon_array[x]->yindex;
3579 y++;
3580 XResizeWindow(dpy, scr->dock_shadow, ICON_SIZE, ICON_SIZE*y);
3582 done = 0;
3583 while (!done) {
3584 WMMaskEvent(dpy, PointerMotionMask|ButtonReleaseMask|ButtonPressMask
3585 |ButtonMotionMask|ExposureMask, &ev);
3586 switch (ev.type) {
3587 case Expose:
3588 WMHandleEvent(&ev);
3589 break;
3591 case MotionNotify:
3592 if (!grabbed) {
3593 if (abs(ofs_x-ev.xmotion.x)>=MOVE_THRESHOLD
3594 || abs(ofs_y-ev.xmotion.y)>=MOVE_THRESHOLD) {
3595 XChangeActivePointerGrab(dpy, ButtonMotionMask
3596 |ButtonReleaseMask|ButtonPressMask,
3597 wCursor[WCUR_MOVE], CurrentTime);
3598 grabbed=1;
3600 break;
3602 if (dock->type == WM_CLIP) {
3603 if (ev.xmotion.x_root - ofs_x < 0) {
3604 x = 0;
3605 } else if (ev.xmotion.x_root - ofs_x + ICON_SIZE >
3606 scr->scr_width) {
3607 x = scr->scr_width - ICON_SIZE;
3608 } else {
3609 x = ev.xmotion.x_root - ofs_x;
3611 if (ev.xmotion.y_root - ofs_y < 0) {
3612 y = 0;
3613 } else if (ev.xmotion.y_root - ofs_y + ICON_SIZE >
3614 scr->scr_height) {
3615 y = scr->scr_height - ICON_SIZE;
3616 } else {
3617 y = ev.xmotion.y_root - ofs_y;
3619 moveDock(dock, x, y);
3620 } else {
3621 /* move vertically if pointer is inside the dock*/
3622 if ((dock->on_right_side &&
3623 ev.xmotion.x_root >= dock->x_pos - ICON_SIZE)
3624 || (!dock->on_right_side &&
3625 ev.xmotion.x_root <= dock->x_pos + ICON_SIZE*2)) {
3627 if (ev.xmotion.y_root - ofs_y < 0) {
3628 y = 0;
3629 } else if (ev.xmotion.y_root - ofs_y + ICON_SIZE >
3630 scr->scr_height) {
3631 y = scr->scr_height - ICON_SIZE;
3632 } else {
3633 y = ev.xmotion.y_root - ofs_y;
3635 moveDock(dock, dock->x_pos, y);
3637 /* move horizontally to change sides */
3638 x = ev.xmotion.x_root - ofs_x;
3639 if (!dock->on_right_side) {
3641 /* is on left */
3643 if (ev.xmotion.x_root > dock->x_pos + ICON_SIZE*2) {
3644 XMoveWindow(dpy, scr->dock_shadow, scr->scr_width-ICON_SIZE
3645 -DOCK_EXTRA_SPACE-1, dock->y_pos);
3646 if (superfluous && ghost==None) {
3647 ghost = MakeGhostDock(dock, dock->x_pos,
3648 scr->scr_width-ICON_SIZE
3649 -DOCK_EXTRA_SPACE-1,
3650 dock->y_pos);
3651 XSetWindowBackgroundPixmap(dpy, scr->dock_shadow,
3652 ghost);
3653 XClearWindow(dpy, scr->dock_shadow);
3655 XMapRaised(dpy, scr->dock_shadow);
3656 swapped = 1;
3657 } else {
3658 if (superfluous && ghost!=None) {
3659 XFreePixmap(dpy, ghost);
3660 ghost = None;
3662 XUnmapWindow(dpy, scr->dock_shadow);
3663 swapped = 0;
3665 } else {
3666 /* is on right */
3667 if (ev.xmotion.x_root < dock->x_pos - ICON_SIZE) {
3668 XMoveWindow(dpy, scr->dock_shadow,
3669 DOCK_EXTRA_SPACE, dock->y_pos);
3670 if (superfluous && ghost==None) {
3671 ghost = MakeGhostDock(dock, dock->x_pos,
3672 DOCK_EXTRA_SPACE, dock->y_pos);
3673 XSetWindowBackgroundPixmap(dpy, scr->dock_shadow,
3674 ghost);
3675 XClearWindow(dpy, scr->dock_shadow);
3677 XMapRaised(dpy, scr->dock_shadow);
3678 swapped = -1;
3679 } else {
3680 XUnmapWindow(dpy, scr->dock_shadow);
3681 swapped = 0;
3682 if (superfluous && ghost!=None) {
3683 XFreePixmap(dpy, ghost);
3684 ghost = None;
3689 break;
3691 case ButtonPress:
3692 break;
3694 case ButtonRelease:
3695 if (ev.xbutton.button != event->xbutton.button)
3696 break;
3697 XUngrabPointer(dpy, CurrentTime);
3698 XUnmapWindow(dpy, scr->dock_shadow);
3699 XResizeWindow(dpy, scr->dock_shadow, ICON_SIZE, ICON_SIZE);
3700 if (dock->type == WM_DOCK) {
3701 if (swapped!=0) {
3702 if (swapped>0)
3703 dock->on_right_side = 1;
3704 else
3705 dock->on_right_side = 0;
3706 swapDock(dock);
3707 wArrangeIcons(scr, False);
3710 done = 1;
3711 break;
3714 if (superfluous) {
3715 if (ghost!=None)
3716 XFreePixmap(dpy, ghost);
3717 XSetWindowBackground(dpy, scr->dock_shadow, scr->white_pixel);
3719 #ifdef DEBUG
3720 puts("End dock move");
3721 #endif
3726 static void
3727 handleIconMove(WDock *dock, WAppIcon *aicon, XEvent *event)
3729 WScreen *scr = dock->screen_ptr;
3730 Window wins[2];
3731 WIcon *icon = aicon->icon;
3732 WDock *dock2 = NULL, *last_dock = dock, *clip = NULL;
3733 int ondock, grabbed = 0, change_dock = 0, collapsed = 0;
3734 XEvent ev;
3735 int x = aicon->x_pos, y = aicon->y_pos;
3736 int ofs_x = event->xbutton.x, ofs_y = event->xbutton.y;
3737 int shad_x = x, shad_y = y;
3738 int ix = aicon->xindex, iy = aicon->yindex;
3739 int tmp;
3740 Pixmap ghost = None;
3741 Bool docked;
3742 int superfluous = wPreferences.superfluous; /* we catch it to avoid problems */
3743 int omnipresent = aicon->omnipresent; /* this must be cached!!! */
3746 if (wPreferences.flags.noupdates)
3747 return;
3749 if (XGrabPointer(dpy, icon->core->window, True, ButtonMotionMask
3750 |ButtonReleaseMask|ButtonPressMask, GrabModeAsync,
3751 GrabModeAsync, None, None, CurrentTime) !=GrabSuccess) {
3752 #ifdef DEBUG0
3753 wwarning("pointer grab failed for icon move");
3754 #endif
3757 if (!(event->xbutton.state & MOD_MASK))
3758 wRaiseFrame(icon->core);
3760 if (!wPreferences.flags.noclip)
3761 clip = scr->workspaces[scr->current_workspace]->clip;
3763 if (dock == scr->dock && !wPreferences.flags.noclip)
3764 dock2 = clip;
3765 else if (dock != scr->dock && !wPreferences.flags.nodock)
3766 dock2 = scr->dock;
3768 wins[0] = icon->core->window;
3769 wins[1] = scr->dock_shadow;
3770 XRestackWindows(dpy, wins, 2);
3771 XMoveResizeWindow(dpy, scr->dock_shadow, aicon->x_pos, aicon->y_pos,
3772 ICON_SIZE, ICON_SIZE);
3773 if (superfluous) {
3774 if (icon->pixmap!=None)
3775 ghost = MakeGhostIcon(scr, icon->pixmap);
3776 else
3777 ghost = MakeGhostIcon(scr, icon->core->window);
3779 XSetWindowBackgroundPixmap(dpy, scr->dock_shadow, ghost);
3780 XClearWindow(dpy, scr->dock_shadow);
3782 XMapWindow(dpy, scr->dock_shadow);
3784 ondock = 1;
3787 while(1) {
3788 XMaskEvent(dpy, PointerMotionMask|ButtonReleaseMask|ButtonPressMask
3789 |ButtonMotionMask|ExposureMask, &ev);
3790 switch (ev.type) {
3791 case Expose:
3792 WMHandleEvent(&ev);
3793 break;
3795 case MotionNotify:
3796 if (!grabbed) {
3797 if (abs(ofs_x-ev.xmotion.x)>=MOVE_THRESHOLD
3798 || abs(ofs_y-ev.xmotion.y)>=MOVE_THRESHOLD) {
3799 XChangeActivePointerGrab(dpy, ButtonMotionMask
3800 |ButtonReleaseMask|ButtonPressMask,
3801 wCursor[WCUR_MOVE], CurrentTime);
3802 grabbed=1;
3803 } else {
3804 break;
3808 if (omnipresent) {
3809 int i;
3810 for (i=0; i<scr->workspace_count; i++) {
3811 if (i == scr->current_workspace)
3812 continue;
3813 wDockShowIcons(scr->workspaces[i]->clip);
3817 x = ev.xmotion.x_root - ofs_x;
3818 y = ev.xmotion.y_root - ofs_y;
3819 tmp = wDockSnapIcon(dock, aicon, x, y, &ix, &iy, True);
3820 if (tmp && dock2) {
3821 change_dock = 0;
3822 if (last_dock != dock && collapsed) {
3823 last_dock->collapsed = 1;
3824 wDockHideIcons(last_dock);
3825 collapsed = 0;
3827 if (!collapsed && (collapsed = dock->collapsed)) {
3828 dock->collapsed = 0;
3829 wDockShowIcons(dock);
3831 if (dock->auto_raise_lower)
3832 wDockRaise(dock);
3833 last_dock = dock;
3834 } else if (dock2) {
3835 tmp = wDockSnapIcon(dock2, aicon, x, y, &ix, &iy, False);
3836 if (tmp) {
3837 change_dock = 1;
3838 if (last_dock != dock2 && collapsed) {
3839 last_dock->collapsed = 1;
3840 wDockHideIcons(last_dock);
3841 collapsed = 0;
3843 if (!collapsed && (collapsed = dock2->collapsed)) {
3844 dock2->collapsed = 0;
3845 wDockShowIcons(dock2);
3847 if (dock2->auto_raise_lower)
3848 wDockRaise(dock2);
3849 last_dock = dock2;
3852 if (aicon->launching
3853 || aicon->lock
3854 || (aicon->running && !(ev.xmotion.state & MOD_MASK))
3855 || (!aicon->running && tmp)) {
3856 shad_x = last_dock->x_pos + ix*wPreferences.icon_size;
3857 shad_y = last_dock->y_pos + iy*wPreferences.icon_size;
3859 XMoveWindow(dpy, scr->dock_shadow, shad_x, shad_y);
3861 if (!ondock) {
3862 XMapWindow(dpy, scr->dock_shadow);
3864 ondock = 1;
3865 } else {
3866 if (ondock) {
3867 XUnmapWindow(dpy, scr->dock_shadow);
3869 ondock = 0;
3871 XMoveWindow(dpy, icon->core->window, x, y);
3872 break;
3874 case ButtonPress:
3875 break;
3877 case ButtonRelease:
3878 if (ev.xbutton.button != event->xbutton.button)
3879 break;
3880 XUngrabPointer(dpy, CurrentTime);
3881 if (ondock) {
3882 SlideWindow(icon->core->window, x, y, shad_x, shad_y);
3883 XUnmapWindow(dpy, scr->dock_shadow);
3884 if (!change_dock) {
3885 reattachIcon(dock, aicon, ix, iy);
3886 if (clip && dock!=clip && clip->auto_raise_lower)
3887 wDockLower(clip);
3888 } else {
3889 docked = moveIconBetweenDocks(dock, dock2, aicon, ix, iy);
3890 if (!docked) {
3891 /* Slide it back if dock rejected it */
3892 SlideWindow(icon->core->window, x, y, aicon->x_pos,
3893 aicon->y_pos);
3894 reattachIcon(dock, aicon, aicon->xindex,aicon->yindex);
3896 if (last_dock->type==WM_CLIP && last_dock->auto_collapse) {
3897 collapsed = 0;
3900 } else {
3901 aicon->x_pos = x;
3902 aicon->y_pos = y;
3903 if (superfluous) {
3904 if (!aicon->running && !wPreferences.no_animations) {
3905 /* We need to deselect it, even if is deselected in
3906 * wDockDetach(), because else DoKaboom() will fail.
3908 if (aicon->icon->selected)
3909 wIconSelect(aicon->icon);
3911 wSoundPlay(WSOUND_KABOOM);
3912 DoKaboom(scr,aicon->icon->core->window, x, y);
3913 } else {
3914 wSoundPlay(WSOUND_UNDOCK);
3916 } else {
3917 wSoundPlay(WSOUND_UNDOCK);
3919 if (clip && clip->auto_raise_lower)
3920 wDockLower(clip);
3921 wDockDetach(dock, aicon);
3923 if (collapsed) {
3924 last_dock->collapsed = 1;
3925 wDockHideIcons(last_dock);
3926 collapsed = 0;
3928 if (superfluous) {
3929 if (ghost!=None)
3930 XFreePixmap(dpy, ghost);
3931 XSetWindowBackground(dpy, scr->dock_shadow, scr->white_pixel);
3933 if (omnipresent) {
3934 int i;
3935 for (i=0; i<scr->workspace_count; i++) {
3936 if (i == scr->current_workspace)
3937 continue;
3938 wDockHideIcons(scr->workspaces[i]->clip);
3942 #ifdef DEBUG
3943 puts("End icon move");
3944 #endif
3945 return;
3951 static int
3952 getClipButton(int px, int py)
3954 int pt = (CLIP_BUTTON_SIZE+2)*ICON_SIZE/64;
3956 if (px < 0 || py < 0 || px >= ICON_SIZE || py >= ICON_SIZE)
3957 return CLIP_IDLE;
3959 if (py <= pt-((int)ICON_SIZE-1-px))
3960 return CLIP_FORWARD;
3961 else if (px <= pt-((int)ICON_SIZE-1-py))
3962 return CLIP_REWIND;
3964 return CLIP_IDLE;
3968 static void
3969 handleClipChangeWorkspace(WScreen *scr, XEvent *event)
3971 XEvent ev;
3972 int done, direction, new_ws;
3973 int new_dir;
3974 WDock *clip = scr->clip_icon->dock;
3976 direction = getClipButton(event->xbutton.x, event->xbutton.y);
3978 clip->lclip_button_pushed = direction==CLIP_REWIND;
3979 clip->rclip_button_pushed = direction==CLIP_FORWARD;
3981 wClipIconPaint(scr->clip_icon);
3982 done = 0;
3983 while(!done) {
3984 WMMaskEvent(dpy, ExposureMask|ButtonMotionMask|ButtonReleaseMask
3985 |ButtonPressMask, &ev);
3986 switch (ev.type) {
3987 case Expose:
3988 WMHandleEvent(&ev);
3989 break;
3991 case MotionNotify:
3992 new_dir = getClipButton(ev.xmotion.x, ev.xmotion.y);
3993 if (new_dir != direction) {
3994 direction = new_dir;
3995 clip->lclip_button_pushed = direction==CLIP_REWIND;
3996 clip->rclip_button_pushed = direction==CLIP_FORWARD;
3997 wClipIconPaint(scr->clip_icon);
3999 break;
4001 case ButtonPress:
4002 break;
4004 case ButtonRelease:
4005 if (ev.xbutton.button == event->xbutton.button)
4006 done = 1;
4010 clip->lclip_button_pushed = 0;
4011 clip->rclip_button_pushed = 0;
4013 new_ws = wPreferences.ws_advance || (event->xbutton.state & ControlMask);
4015 if (direction == CLIP_FORWARD) {
4016 if (scr->current_workspace < scr->workspace_count-1)
4017 wWorkspaceChange(scr, scr->current_workspace+1);
4018 else if (new_ws && scr->current_workspace < MAX_WORKSPACES-1)
4019 wWorkspaceChange(scr, scr->current_workspace+1);
4020 else if (wPreferences.ws_cycle)
4021 wWorkspaceChange(scr, 0);
4023 else if (direction == CLIP_REWIND) {
4024 if (scr->current_workspace > 0)
4025 wWorkspaceChange(scr, scr->current_workspace-1);
4026 else if (scr->current_workspace==0 && wPreferences.ws_cycle)
4027 wWorkspaceChange(scr, scr->workspace_count-1);
4030 wClipIconPaint(scr->clip_icon);
4034 static void
4035 iconMouseDown(WObjDescriptor *desc, XEvent *event)
4037 WAppIcon *aicon = desc->parent;
4038 WDock *dock = aicon->dock;
4039 WScreen *scr = aicon->icon->core->screen_ptr;
4041 if (aicon->editing || WCHECK_STATE(WSTATE_MODAL))
4042 return;
4044 scr->last_dock = dock;
4046 if (dock->menu->flags.mapped)
4047 wMenuUnmap(dock->menu);
4049 if (IsDoubleClick(scr, event)) {
4050 /* double-click was not in the main clip icon */
4051 if (dock->type != WM_CLIP || aicon->xindex!=0 || aicon->yindex!=0
4052 || getClipButton(event->xbutton.x, event->xbutton.y)==CLIP_IDLE) {
4053 iconDblClick(desc, event);
4054 return;
4058 if (dock->type == WM_CLIP && scr->flags.clip_balloon_mapped) {
4059 XUnmapWindow(dpy, scr->clip_balloon);
4060 scr->flags.clip_balloon_mapped = 0;
4063 #ifdef DEBUG
4064 puts("handling dock");
4065 #endif
4066 if (event->xbutton.button == Button1) {
4067 if (event->xbutton.state & MOD_MASK)
4068 wDockLower(dock);
4069 else
4070 wDockRaise(dock);
4072 if ((event->xbutton.state & ShiftMask) && aicon!=scr->clip_icon &&
4073 dock->type!=WM_DOCK) {
4074 wIconSelect(aicon->icon);
4075 return;
4078 if (aicon->yindex==0 && aicon->xindex==0) {
4079 if (getClipButton(event->xbutton.x, event->xbutton.y)!=CLIP_IDLE
4080 && dock->type==WM_CLIP)
4081 handleClipChangeWorkspace(scr, event);
4082 else
4083 handleDockMove(dock, aicon, event);
4084 } else
4085 handleIconMove(dock, aicon, event);
4087 } else if (event->xbutton.button==Button2 && dock->type==WM_CLIP &&
4088 aicon==scr->clip_icon) {
4089 openClipWorkspaceMenu(scr, event->xbutton.x_root+2,
4090 event->xbutton.y_root+2);
4091 if (scr->clip_ws_menu) {
4092 WMenu *menu;
4093 menu = scr->clip_ws_menu;
4094 desc = &menu->menu->descriptor;
4096 event->xany.send_event = True;
4097 (*desc->handle_mousedown)(desc, event);
4099 } else if (event->xbutton.button==Button2 && dock->type==WM_CLIP &&
4100 (event->xbutton.state & ShiftMask) && aicon!=scr->clip_icon) {
4101 wClipMakeIconOmnipresent(aicon, !aicon->omnipresent);
4102 } else if (event->xbutton.button == Button3) {
4103 if (event->xbutton.send_event &&
4104 XGrabPointer(dpy, aicon->icon->core->window, True, ButtonMotionMask
4105 |ButtonReleaseMask|ButtonPressMask, GrabModeAsync,
4106 GrabModeAsync, None, None, CurrentTime) !=GrabSuccess) {
4107 wwarning("pointer grab failed for dockicon menu");
4108 return;
4111 openDockMenu(dock, aicon, event);
4112 } else if (event->xbutton.button == Button2) {
4113 WAppIcon *btn = desc->parent;
4115 if (!btn->launching &&
4116 (!btn->running || (event->xbutton.state & ControlMask))) {
4117 launchDockedApplication(btn, True);
4123 static void
4124 showClipBalloon(WDock *dock, int workspace)
4126 int w, h;
4127 int x, y;
4128 WScreen *scr = dock->screen_ptr;
4129 char *text;
4130 Window stack[2];
4132 scr->flags.clip_balloon_mapped = 1;
4133 XMapWindow(dpy, scr->clip_balloon);
4135 text = scr->workspaces[workspace]->name;
4137 w = WMWidthOfString(scr->clip_title_font, text, strlen(text));
4139 h = WMFontHeight(scr->clip_title_font);
4140 XResizeWindow(dpy, scr->clip_balloon, w, h);
4142 x = dock->x_pos + CLIP_BUTTON_SIZE*ICON_SIZE/64;
4143 y = dock->y_pos + ICON_SIZE - WMFontHeight(scr->clip_title_font) - 3;
4145 if (x+w > scr->scr_width) {
4146 x = scr->scr_width - w;
4147 if (dock->y_pos + ICON_SIZE + h > scr->scr_height)
4148 y = dock->y_pos - h - 1;
4149 else
4150 y = dock->y_pos + ICON_SIZE;
4151 XRaiseWindow(dpy, scr->clip_balloon);
4152 } else {
4153 stack[0] = scr->clip_icon->icon->core->window;
4154 stack[1] = scr->clip_balloon;
4155 XRestackWindows(dpy, stack, 2);
4157 XMoveWindow(dpy, scr->clip_balloon, x, y);
4158 XSetForeground(dpy, scr->clip_title_gc,
4159 scr->clip_title_pixel[CLIP_NORMAL]);
4160 XClearWindow(dpy, scr->clip_balloon);
4161 WMDrawString(scr->wmscreen, scr->clip_balloon, scr->clip_title_gc,
4162 scr->clip_title_font, 0, 0, text, strlen(text));
4166 static void
4167 clipEnterNotify(WObjDescriptor *desc, XEvent *event)
4169 WAppIcon *btn = (WAppIcon*)desc->parent;
4170 WDock *dock;
4171 WScreen *scr;
4173 assert(event->type==EnterNotify);
4175 if(desc->parent_type!=WCLASS_DOCK_ICON)
4176 return;
4178 scr = btn->icon->core->screen_ptr;
4179 if (!btn->omnipresent)
4180 dock = btn->dock;
4181 else
4182 dock = scr->workspaces[scr->current_workspace]->clip;
4184 if (!dock || dock->type!=WM_CLIP)
4185 return;
4187 /* The auto raise/lower code */
4188 if (dock->auto_lower_magic) {
4189 WMDeleteTimerHandler(dock->auto_lower_magic);
4190 dock->auto_lower_magic = NULL;
4192 if (dock->auto_raise_lower && !dock->auto_raise_magic) {
4193 dock->auto_raise_magic = WMAddTimerHandler(AUTO_RAISE_DELAY,
4194 clipAutoRaise,
4195 (void *)dock);
4198 /* The auto expand/collapse code */
4199 if (dock->auto_collapse_magic) {
4200 WMDeleteTimerHandler(dock->auto_collapse_magic);
4201 dock->auto_collapse_magic = NULL;
4203 if (dock->auto_collapse && !dock->auto_expand_magic) {
4204 dock->auto_expand_magic = WMAddTimerHandler(AUTO_EXPAND_DELAY,
4205 clipAutoExpand,
4206 (void *)dock);
4209 if (btn->xindex == 0 && btn->yindex == 0)
4210 showClipBalloon(dock, dock->screen_ptr->current_workspace);
4211 else {
4212 if (dock->screen_ptr->flags.clip_balloon_mapped) {
4213 XUnmapWindow(dpy, dock->screen_ptr->clip_balloon);
4214 dock->screen_ptr->flags.clip_balloon_mapped = 0;
4220 static void
4221 clipLeave(WDock *dock)
4223 XEvent event;
4224 WObjDescriptor *desc = NULL;
4226 if (!dock || dock->type!=WM_CLIP)
4227 return;
4229 if (XCheckTypedEvent(dpy, EnterNotify, &event)!=False) {
4230 if (XFindContext(dpy, event.xcrossing.window, wWinContext,
4231 (XPointer *)&desc)!=XCNOENT
4232 && desc && desc->parent_type==WCLASS_DOCK_ICON
4233 && ((WAppIcon*)desc->parent)->dock
4234 && ((WAppIcon*)desc->parent)->dock->type==WM_CLIP) {
4235 /* We didn't left the Clip yet */
4236 XPutBackEvent(dpy, &event);
4237 return;
4240 XPutBackEvent(dpy, &event);
4241 } else {
4242 /* We entered a withdrawn window, so we're still in Clip */
4243 return;
4246 if (dock->auto_raise_magic) {
4247 WMDeleteTimerHandler(dock->auto_raise_magic);
4248 dock->auto_raise_magic = NULL;
4250 if (dock->auto_raise_lower && !dock->auto_lower_magic) {
4251 dock->auto_lower_magic = WMAddTimerHandler(AUTO_LOWER_DELAY,
4252 clipAutoLower,
4253 (void *)dock);
4256 if (dock->auto_expand_magic) {
4257 WMDeleteTimerHandler(dock->auto_expand_magic);
4258 dock->auto_expand_magic = NULL;
4260 if (dock->auto_collapse && !dock->auto_collapse_magic) {
4261 dock->auto_collapse_magic = WMAddTimerHandler(AUTO_COLLAPSE_DELAY,
4262 clipAutoCollapse,
4263 (void *)dock);
4268 static void
4269 clipLeaveNotify(WObjDescriptor *desc, XEvent *event)
4271 WAppIcon *btn = (WAppIcon*)desc->parent;
4273 assert(event->type==LeaveNotify);
4275 if(desc->parent_type!=WCLASS_DOCK_ICON)
4276 return;
4278 clipLeave(btn->dock);
4282 static void
4283 clipAutoCollapse(void *cdata)
4285 WDock *dock = (WDock *)cdata;
4287 if (dock->type!=WM_CLIP)
4288 return;
4290 if (dock->auto_collapse) {
4291 dock->collapsed = 1;
4292 wDockHideIcons(dock);
4294 dock->auto_collapse_magic = NULL;
4298 static void
4299 clipAutoExpand(void *cdata)
4301 WDock *dock = (WDock *)cdata;
4303 if (dock->type!=WM_CLIP)
4304 return;
4306 if (dock->auto_collapse) {
4307 dock->collapsed = 0;
4308 wDockShowIcons(dock);
4310 dock->auto_expand_magic = NULL;
4314 static void
4315 clipAutoLower(void *cdata)
4317 WDock *dock = (WDock *)cdata;
4319 if (dock->type!=WM_CLIP)
4320 return;
4322 if (dock->auto_raise_lower)
4323 wDockLower(dock);
4325 dock->auto_lower_magic = NULL;
4329 static void
4330 clipAutoRaise(void *cdata)
4332 WDock *dock = (WDock *)cdata;
4334 if (dock->type!=WM_CLIP)
4335 return;
4337 if (dock->auto_raise_lower)
4338 wDockRaise(dock);
4340 if (dock->screen_ptr->flags.clip_balloon_mapped) {
4341 showClipBalloon(dock, dock->screen_ptr->current_workspace);
4344 dock->auto_raise_magic = NULL;
4348 static Bool
4349 iconCanBeOmnipresent(WAppIcon *aicon)
4351 WScreen *scr = aicon->icon->core->screen_ptr;
4352 WDock *clip;
4353 WAppIcon *btn;
4354 int i, j;
4356 for (i=0; i<scr->workspace_count; i++) {
4357 clip = scr->workspaces[i]->clip;
4359 if (clip == aicon->dock)
4360 continue;
4362 if (clip->icon_count + scr->global_icon_count >= clip->max_icons)
4363 return False; /* Clip is full in some workspace */
4365 for (j=0; j<clip->max_icons; j++) {
4366 btn = clip->icon_array[j];
4367 if(btn && btn->xindex==aicon->xindex && btn->yindex==aicon->yindex)
4368 return False;
4372 return True;
4377 wClipMakeIconOmnipresent(WAppIcon *aicon, int omnipresent)
4379 WScreen *scr = aicon->icon->core->screen_ptr;
4380 WAppIconChain *new_entry, *tmp, *tmp1;
4381 int status = WO_SUCCESS;
4383 if ((scr->dock && aicon->dock==scr->dock) || aicon==scr->clip_icon) {
4384 return WO_NOT_APPLICABLE;
4387 if (aicon->omnipresent == omnipresent)
4388 return WO_SUCCESS;
4390 if (omnipresent) {
4391 if (iconCanBeOmnipresent(aicon)) {
4392 aicon->omnipresent = 1;
4393 new_entry = wmalloc(sizeof(WAppIconChain));
4394 new_entry->aicon = aicon;
4395 new_entry->next = scr->global_icons;
4396 scr->global_icons = new_entry;
4397 scr->global_icon_count++;
4398 } else {
4399 aicon->omnipresent = 0;
4400 status = WO_FAILED;
4402 } else {
4403 aicon->omnipresent = 0;
4404 if (aicon == scr->global_icons->aicon) {
4405 tmp = scr->global_icons->next;
4406 wfree(scr->global_icons);
4407 scr->global_icons = tmp;
4408 scr->global_icon_count--;
4409 } else {
4410 tmp = scr->global_icons;
4411 while (tmp->next) {
4412 if (tmp->next->aicon == aicon) {
4413 tmp1 = tmp->next->next;
4414 wfree(tmp->next);
4415 tmp->next = tmp1;
4416 scr->global_icon_count--;
4417 break;
4419 tmp = tmp->next;
4424 wAppIconPaint(aicon);
4426 return status;