Update local copy of GPLv2 and FSF address in copyrights
[wmaker-crm.git] / src / dock.c
blob596c1b9793bab4d62f9ea5b2da753931cbfa4024
1 /* dock.c- built-in Dock module for WindowMaker
3 * Window Maker window manager
5 * Copyright (c) 1997-2003 Alfredo K. Kojima
6 * Copyright (c) 1998-2003 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 along
19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
23 #include "wconfig.h"
25 #include <X11/Xlib.h>
26 #include <X11/Xutil.h>
27 #include <stdlib.h>
28 #include <string.h>
29 #include <unistd.h>
30 #include <math.h>
31 #include <limits.h>
33 #ifndef PATH_MAX
34 #define PATH_MAX DEFAULT_PATH_MAX
35 #endif
37 #include "WindowMaker.h"
38 #include "wcore.h"
39 #include "window.h"
40 #include "icon.h"
41 #include "appicon.h"
42 #include "actions.h"
43 #include "stacking.h"
44 #include "dock.h"
45 #include "dialog.h"
46 #include "funcs.h"
47 #include "properties.h"
48 #include "menu.h"
49 #include "client.h"
50 #include "defaults.h"
51 #include "workspace.h"
52 #include "framewin.h"
53 #include "superfluous.h"
54 #include "xinerama.h"
56 /**** Local variables ****/
57 #define CLIP_REWIND 1
58 #define CLIP_IDLE 0
59 #define CLIP_FORWARD 2
61 /**** Global variables ****/
63 /* in dockedapp.c */
64 extern void DestroyDockAppSettingsPanel();
65 extern void ShowDockAppSettingsPanel(WAppIcon * aicon);
66 extern Cursor wCursor[WCUR_LAST];
67 extern WPreferences wPreferences;
68 extern XContext wWinContext;
69 extern void appIconMouseDown(WObjDescriptor *desc, XEvent *event);
71 #define MOD_MASK wPreferences.modifier_mask
72 #define ICON_SIZE wPreferences.icon_size
74 /***** Local variables ****/
76 static WMPropList *dCommand = NULL;
77 static WMPropList *dPasteCommand = NULL;
78 #ifdef XDND /* XXX was OFFIX */
79 static WMPropList *dDropCommand = NULL;
80 #endif
81 static WMPropList *dAutoLaunch, *dLock;
82 static WMPropList *dName, *dForced, *dBuggyApplication, *dYes, *dNo;
83 static WMPropList *dHost, *dDock, *dClip;
84 static WMPropList *dAutoAttractIcons;
86 static WMPropList *dPosition, *dApplications, *dLowered, *dCollapsed;
88 static WMPropList *dAutoCollapse, *dAutoRaiseLower, *dOmnipresent;
90 static void dockIconPaint(WAppIcon * btn);
92 static void iconMouseDown(WObjDescriptor * desc, XEvent * event);
94 static pid_t execCommand(WAppIcon * btn, char *command, WSavedState * state);
96 static void trackDeadProcess(pid_t pid, unsigned char status, WDock * dock);
98 static int getClipButton(int px, int py);
100 static void toggleLowered(WDock * dock);
102 static void toggleCollapsed(WDock * dock);
104 static void clipIconExpose(WObjDescriptor * desc, XEvent * event);
106 static void clipLeave(WDock * dock);
108 static void handleClipChangeWorkspace(WScreen * scr, XEvent * event);
110 Bool moveIconBetweenDocks(WDock * src, WDock * dest, WAppIcon * icon, int x, int y);
112 static void clipEnterNotify(WObjDescriptor * desc, XEvent * event);
113 static void clipLeaveNotify(WObjDescriptor * desc, XEvent * event);
114 static void clipAutoCollapse(void *cdata);
115 static void clipAutoExpand(void *cdata);
116 static void launchDockedApplication(WAppIcon * btn, Bool withSelection);
118 static void clipAutoLower(void *cdata);
119 static void clipAutoRaise(void *cdata);
121 static void showClipBalloon(WDock * dock, int workspace);
123 static void make_keys(void)
125 if (dCommand != NULL)
126 return;
128 dCommand = WMRetainPropList(WMCreatePLString("Command"));
129 dPasteCommand = WMRetainPropList(WMCreatePLString("PasteCommand"));
130 #ifdef XDND
131 dDropCommand = WMRetainPropList(WMCreatePLString("DropCommand"));
132 #endif
133 dLock = WMRetainPropList(WMCreatePLString("Lock"));
134 dAutoLaunch = WMRetainPropList(WMCreatePLString("AutoLaunch"));
135 dName = WMRetainPropList(WMCreatePLString("Name"));
136 dForced = WMRetainPropList(WMCreatePLString("Forced"));
137 dBuggyApplication = WMRetainPropList(WMCreatePLString("BuggyApplication"));
138 dYes = WMRetainPropList(WMCreatePLString("Yes"));
139 dNo = WMRetainPropList(WMCreatePLString("No"));
140 dHost = WMRetainPropList(WMCreatePLString("Host"));
142 dPosition = WMCreatePLString("Position");
143 dApplications = WMCreatePLString("Applications");
144 dLowered = WMCreatePLString("Lowered");
145 dCollapsed = WMCreatePLString("Collapsed");
146 dAutoCollapse = WMCreatePLString("AutoCollapse");
147 dAutoRaiseLower = WMCreatePLString("AutoRaiseLower");
148 dAutoAttractIcons = WMCreatePLString("AutoAttractIcons");
150 dOmnipresent = WMCreatePLString("Omnipresent");
152 dDock = WMCreatePLString("Dock");
153 dClip = WMCreatePLString("Clip");
156 static void renameCallback(WMenu * menu, WMenuEntry * entry)
158 WDock *dock = entry->clientdata;
159 char buffer[128];
160 int wspace;
161 char *name;
163 assert(entry->clientdata != NULL);
165 wspace = dock->screen_ptr->current_workspace;
167 name = wstrdup(dock->screen_ptr->workspaces[wspace]->name);
169 snprintf(buffer, sizeof(buffer), _("Type the name for workspace %i:"), wspace + 1);
170 if (wInputDialog(dock->screen_ptr, _("Rename Workspace"), buffer, &name)) {
171 wWorkspaceRename(dock->screen_ptr, wspace, name);
173 if (name) {
174 wfree(name);
178 static void toggleLoweredCallback(WMenu * menu, WMenuEntry * entry)
180 assert(entry->clientdata != NULL);
182 toggleLowered(entry->clientdata);
184 entry->flags.indicator_on = !(((WDock *) entry->clientdata)->lowered);
186 wMenuPaint(menu);
189 static int matchWindow(const void *item, const void *cdata)
191 return (((WFakeGroupLeader *) item)->leader == (Window) cdata);
194 static void killCallback(WMenu * menu, WMenuEntry * entry)
196 WScreen *scr = menu->menu->screen_ptr;
197 WAppIcon *icon;
198 WFakeGroupLeader *fPtr;
199 char *buffer, *shortname, **argv;
200 char *basename(const char *shortname);
201 int argc;
203 if (!WCHECK_STATE(WSTATE_NORMAL))
204 return;
206 assert(entry->clientdata != NULL);
208 icon = (WAppIcon *) entry->clientdata;
210 icon->editing = 1;
212 WCHANGE_STATE(WSTATE_MODAL);
214 /* strip away dir names */
215 shortname = basename(icon->command);
216 /* separate out command options */
217 wtokensplit(shortname, &argv, &argc);
219 buffer = wstrconcat(argv[0],
220 _(" will be forcibly closed.\n"
221 "Any unsaved changes will be lost.\n" "Please confirm."));
223 if (icon->icon && icon->icon->owner) {
224 fPtr = icon->icon->owner->fake_group;
225 } else {
226 /* is this really necessary? can we kill a non-running dock icon? */
227 Window win = icon->main_window;
228 int index;
230 index = WMFindInArray(scr->fakeGroupLeaders, matchWindow, (void *)win);
231 if (index != WANotFound)
232 fPtr = WMGetFromArray(scr->fakeGroupLeaders, index);
233 else
234 fPtr = NULL;
237 if (wPreferences.dont_confirm_kill
238 || wMessageDialog(menu->frame->screen_ptr, _("Kill Application"),
239 buffer, _("Yes"), _("No"), NULL) == WAPRDefault) {
240 if (fPtr != NULL) {
241 WWindow *wwin, *twin;
243 wwin = scr->focused_window;
244 while (wwin) {
245 twin = wwin->prev;
246 if (wwin->fake_group == fPtr) {
247 wClientKill(wwin);
249 wwin = twin;
251 } else if (icon->icon && icon->icon->owner) {
252 wClientKill(icon->icon->owner);
256 wfree(buffer);
257 wtokenfree(argv, argc);
259 icon->editing = 0;
261 WCHANGE_STATE(WSTATE_NORMAL);
264 /* TODO: replace this function with a member of the dock struct */
265 static int numberOfSelectedIcons(WDock * dock)
267 WAppIcon *aicon;
268 int i, n;
270 n = 0;
271 for (i = 1; i < dock->max_icons; i++) {
272 aicon = dock->icon_array[i];
273 if (aicon && aicon->icon->selected) {
274 n++;
278 return n;
281 static WMArray *getSelected(WDock * dock)
283 WMArray *ret = WMCreateArray(8);
284 WAppIcon *btn;
285 int i;
287 for (i = 1; i < dock->max_icons; i++) {
288 btn = dock->icon_array[i];
289 if (btn && btn->icon->selected) {
290 WMAddToArray(ret, btn);
294 return ret;
297 static void paintClipButtons(WAppIcon * clipIcon, Bool lpushed, Bool rpushed)
299 Window win = clipIcon->icon->core->window;
300 WScreen *scr = clipIcon->icon->core->screen_ptr;
301 XPoint p[4];
302 int pt = CLIP_BUTTON_SIZE * ICON_SIZE / 64;
303 int tp = ICON_SIZE - pt;
304 int as = pt - 15; /* 15 = 5+5+5 */
305 GC gc = scr->draw_gc; /* maybe use WMColorGC() instead here? */
306 WMColor *color;
307 #ifdef GRADIENT_CLIP_ARROW
308 Bool collapsed = clipIcon->dock->collapsed;
309 #endif
311 /*if (!clipIcon->dock->collapsed)
312 color = scr->clip_title_color[CLIP_NORMAL];
313 else
314 color = scr->clip_title_color[CLIP_COLLAPSED]; */
315 color = scr->clip_title_color[CLIP_NORMAL];
317 XSetForeground(dpy, gc, WMColorPixel(color));
319 if (rpushed) {
320 p[0].x = tp + 1;
321 p[0].y = 1;
322 p[1].x = ICON_SIZE - 2;
323 p[1].y = 1;
324 p[2].x = ICON_SIZE - 2;
325 p[2].y = pt - 1;
326 } else if (lpushed) {
327 p[0].x = 1;
328 p[0].y = tp;
329 p[1].x = pt;
330 p[1].y = ICON_SIZE - 2;
331 p[2].x = 1;
332 p[2].y = ICON_SIZE - 2;
334 if (lpushed || rpushed) {
335 XSetForeground(dpy, scr->draw_gc, scr->white_pixel);
336 XFillPolygon(dpy, win, scr->draw_gc, p, 3, Convex, CoordModeOrigin);
337 XSetForeground(dpy, scr->draw_gc, scr->black_pixel);
339 #ifdef GRADIENT_CLIP_ARROW
340 if (!collapsed) {
341 XSetFillStyle(dpy, scr->copy_gc, FillTiled);
342 XSetTile(dpy, scr->copy_gc, scr->clip_arrow_gradient);
343 XSetClipMask(dpy, scr->copy_gc, None);
344 gc = scr->copy_gc;
346 #endif /* GRADIENT_CLIP_ARROW */
348 /* top right arrow */
349 p[0].x = p[3].x = ICON_SIZE - 5 - as;
350 p[0].y = p[3].y = 5;
351 p[1].x = ICON_SIZE - 6;
352 p[1].y = 5;
353 p[2].x = ICON_SIZE - 6;
354 p[2].y = 4 + as;
355 if (rpushed) {
356 XFillPolygon(dpy, win, scr->draw_gc, p, 3, Convex, CoordModeOrigin);
357 XDrawLines(dpy, win, scr->draw_gc, p, 4, CoordModeOrigin);
358 } else {
359 #ifdef GRADIENT_CLIP_ARROW
360 if (!collapsed)
361 XSetTSOrigin(dpy, gc, ICON_SIZE - 6 - as, 5);
362 #endif
363 XFillPolygon(dpy, win, gc, p, 3, Convex, CoordModeOrigin);
364 XDrawLines(dpy, win, gc, p, 4, CoordModeOrigin);
367 /* bottom left arrow */
368 p[0].x = p[3].x = 5;
369 p[0].y = p[3].y = ICON_SIZE - 5 - as;
370 p[1].x = 5;
371 p[1].y = ICON_SIZE - 6;
372 p[2].x = 4 + as;
373 p[2].y = ICON_SIZE - 6;
374 if (lpushed) {
375 XFillPolygon(dpy, win, scr->draw_gc, p, 3, Convex, CoordModeOrigin);
376 XDrawLines(dpy, win, scr->draw_gc, p, 4, CoordModeOrigin);
377 } else {
378 #ifdef GRADIENT_CLIP_ARROW
379 if (!collapsed)
380 XSetTSOrigin(dpy, gc, 5, ICON_SIZE - 6 - as);
381 #endif
382 XFillPolygon(dpy, win, gc, p, 3, Convex, CoordModeOrigin);
383 XDrawLines(dpy, win, gc, p, 4, CoordModeOrigin);
385 #ifdef GRADIENT_CLIP_ARROW
386 if (!collapsed)
387 XSetFillStyle(dpy, scr->copy_gc, FillSolid);
388 #endif
391 RImage *wClipMakeTile(WScreen * scr, RImage * normalTile)
393 RImage *tile = RCloneImage(normalTile);
394 RColor black;
395 RColor dark;
396 RColor light;
397 int pt, tp;
398 int as;
400 pt = CLIP_BUTTON_SIZE * wPreferences.icon_size / 64;
401 tp = wPreferences.icon_size - 1 - pt;
402 as = pt - 15;
404 black.alpha = 255;
405 black.red = black.green = black.blue = 0;
407 dark.alpha = 0;
408 dark.red = dark.green = dark.blue = 60;
410 light.alpha = 0;
411 light.red = light.green = light.blue = 80;
413 /* top right */
414 ROperateLine(tile, RSubtractOperation, tp, 0, wPreferences.icon_size - 2, pt - 1, &dark);
415 RDrawLine(tile, tp - 1, 0, wPreferences.icon_size - 1, pt + 1, &black);
416 ROperateLine(tile, RAddOperation, tp, 2, wPreferences.icon_size - 3, pt, &light);
418 /* arrow bevel */
419 ROperateLine(tile, RSubtractOperation, ICON_SIZE - 7 - as, 4, ICON_SIZE - 5, 4, &dark);
420 ROperateLine(tile, RSubtractOperation, ICON_SIZE - 6 - as, 5, ICON_SIZE - 5, 6 + as, &dark);
421 ROperateLine(tile, RAddOperation, ICON_SIZE - 5, 4, ICON_SIZE - 5, 6 + as, &light);
423 /* bottom left */
424 ROperateLine(tile, RAddOperation, 2, tp + 2, pt - 2, wPreferences.icon_size - 3, &dark);
425 RDrawLine(tile, 0, tp - 1, pt + 1, wPreferences.icon_size - 1, &black);
426 ROperateLine(tile, RSubtractOperation, 0, tp - 2, pt + 1, wPreferences.icon_size - 2, &light);
428 /* arrow bevel */
429 ROperateLine(tile, RSubtractOperation, 4, ICON_SIZE - 7 - as, 4, ICON_SIZE - 5, &dark);
430 ROperateLine(tile, RSubtractOperation, 5, ICON_SIZE - 6 - as, 6 + as, ICON_SIZE - 5, &dark);
431 ROperateLine(tile, RAddOperation, 4, ICON_SIZE - 5, 6 + as, ICON_SIZE - 5, &light);
433 return tile;
436 static void omnipresentCallback(WMenu * menu, WMenuEntry * entry)
438 WAppIcon *clickedIcon = entry->clientdata;
439 WAppIcon *aicon;
440 WDock *dock;
441 WMArray *selectedIcons;
442 WMArrayIterator iter;
443 int failed;
445 assert(entry->clientdata != NULL);
447 dock = clickedIcon->dock;
449 selectedIcons = getSelected(dock);
451 if (!WMGetArrayItemCount(selectedIcons))
452 WMAddToArray(selectedIcons, clickedIcon);
454 failed = 0;
455 WM_ITERATE_ARRAY(selectedIcons, aicon, iter) {
456 if (wClipMakeIconOmnipresent(aicon, !aicon->omnipresent) == WO_FAILED)
457 failed++;
458 else if (aicon->icon->selected)
459 wIconSelect(aicon->icon);
461 WMFreeArray(selectedIcons);
463 if (failed > 1) {
464 wMessageDialog(dock->screen_ptr, _("Warning"),
465 _("Some icons cannot be made omnipresent. "
466 "Please make sure that no other icon is "
467 "docked in the same positions on the other "
468 "workspaces and the Clip is not full in "
469 "some workspace."), _("OK"), NULL, NULL);
470 } else if (failed == 1) {
471 wMessageDialog(dock->screen_ptr, _("Warning"),
472 _("Icon cannot be made omnipresent. "
473 "Please make sure that no other icon is "
474 "docked in the same position on the other "
475 "workspaces and the Clip is not full in "
476 "some workspace."), _("OK"), NULL, NULL);
480 static void removeIconsCallback(WMenu * menu, WMenuEntry * entry)
482 WAppIcon *clickedIcon = (WAppIcon *) entry->clientdata;
483 WDock *dock;
484 WAppIcon *aicon;
485 WMArray *selectedIcons;
486 int keepit;
487 WMArrayIterator it;
489 assert(clickedIcon != NULL);
491 dock = clickedIcon->dock;
493 selectedIcons = getSelected(dock);
495 if (WMGetArrayItemCount(selectedIcons)) {
496 if (wMessageDialog(dock->screen_ptr, _("Workspace Clip"),
497 _("All selected icons will be removed!"),
498 _("OK"), _("Cancel"), NULL) != WAPRDefault) {
499 WMFreeArray(selectedIcons);
500 return;
502 } else {
503 if (clickedIcon->xindex == 0 && clickedIcon->yindex == 0) {
504 WMFreeArray(selectedIcons);
505 return;
507 WMAddToArray(selectedIcons, clickedIcon);
510 WM_ITERATE_ARRAY(selectedIcons, aicon, it) {
511 keepit = aicon->running && wApplicationOf(aicon->main_window);
512 wDockDetach(dock, aicon);
513 if (keepit) {
514 /* XXX: can: aicon->icon == NULL ? */
515 PlaceIcon(dock->screen_ptr, &aicon->x_pos, &aicon->y_pos,
516 wGetHeadForWindow(aicon->icon->owner));
517 XMoveWindow(dpy, aicon->icon->core->window, aicon->x_pos, aicon->y_pos);
518 if (!dock->mapped || dock->collapsed)
519 XMapWindow(dpy, aicon->icon->core->window);
522 WMFreeArray(selectedIcons);
524 if (wPreferences.auto_arrange_icons)
525 wArrangeIcons(dock->screen_ptr, True);
528 static void keepIconsCallback(WMenu * menu, WMenuEntry * entry)
530 WAppIcon *clickedIcon = (WAppIcon *) entry->clientdata;
531 WDock *dock;
532 WAppIcon *aicon;
533 WMArray *selectedIcons;
534 WMArrayIterator it;
536 assert(clickedIcon != NULL);
537 dock = clickedIcon->dock;
539 selectedIcons = getSelected(dock);
541 if (!WMGetArrayItemCount(selectedIcons)
542 && clickedIcon != dock->screen_ptr->clip_icon) {
543 char *command = NULL;
545 if (!clickedIcon->command && !clickedIcon->editing) {
546 clickedIcon->editing = 1;
547 if (wInputDialog(dock->screen_ptr, _("Keep Icon"),
548 _("Type the command used to launch the application"), &command)) {
549 if (command && (command[0] == 0 || (command[0] == '-' && command[1] == 0))) {
550 wfree(command);
551 command = NULL;
553 clickedIcon->command = command;
554 clickedIcon->editing = 0;
555 } else {
556 clickedIcon->editing = 0;
557 if (command)
558 wfree(command);
559 WMFreeArray(selectedIcons);
560 return;
564 WMAddToArray(selectedIcons, clickedIcon);
567 WM_ITERATE_ARRAY(selectedIcons, aicon, it) {
568 if (aicon->icon->selected)
569 wIconSelect(aicon->icon);
570 if (aicon && aicon->attracted && aicon->command) {
571 aicon->attracted = 0;
572 if (aicon->icon->shadowed) {
573 aicon->icon->shadowed = 0;
574 aicon->icon->force_paint = 1;
575 wAppIconPaint(aicon);
579 WMFreeArray(selectedIcons);
582 static void toggleAutoAttractCallback(WMenu * menu, WMenuEntry * entry)
584 WDock *dock = (WDock *) entry->clientdata;
586 assert(entry->clientdata != NULL);
588 dock->attract_icons = !dock->attract_icons;
589 /*if (!dock->attract_icons)
590 dock->keep_attracted = 0; */
592 entry->flags.indicator_on = dock->attract_icons;
594 wMenuPaint(menu);
597 static void selectCallback(WMenu * menu, WMenuEntry * entry)
599 WAppIcon *icon = (WAppIcon *) entry->clientdata;
601 assert(icon != NULL);
603 wIconSelect(icon->icon);
605 wMenuPaint(menu);
608 static void colectIconsCallback(WMenu * menu, WMenuEntry * entry)
610 WAppIcon *clickedIcon = (WAppIcon *) entry->clientdata;
611 WDock *clip;
612 WAppIcon *aicon;
613 int x, y, x_pos, y_pos;
615 assert(entry->clientdata != NULL);
616 clip = clickedIcon->dock;
618 aicon = clip->screen_ptr->app_icon_list;
620 while (aicon) {
621 if (!aicon->docked && wDockFindFreeSlot(clip, &x, &y)) {
622 x_pos = clip->x_pos + x * ICON_SIZE;
623 y_pos = clip->y_pos + y * ICON_SIZE;
624 if (aicon->x_pos != x_pos || aicon->y_pos != y_pos) {
625 #ifdef ANIMATIONS
626 if (wPreferences.no_animations) {
627 XMoveWindow(dpy, aicon->icon->core->window, x_pos, y_pos);
628 } else {
629 SlideWindow(aicon->icon->core->window,
630 aicon->x_pos, aicon->y_pos, x_pos, y_pos);
632 #else
633 XMoveWindow(dpy, aicon->icon->core->window, x_pos, y_pos);
634 #endif /* ANIMATIONS */
636 aicon->attracted = 1;
637 if (!aicon->icon->shadowed) {
638 aicon->icon->shadowed = 1;
639 aicon->icon->force_paint = 1;
640 /* We don't do an wAppIconPaint() here because it's in
641 * wDockAttachIcon(). -Dan
644 wDockAttachIcon(clip, aicon, x, y);
645 if (clip->collapsed || !clip->mapped)
646 XUnmapWindow(dpy, aicon->icon->core->window);
648 aicon = aicon->next;
652 static void selectIconsCallback(WMenu * menu, WMenuEntry * entry)
654 WAppIcon *clickedIcon = (WAppIcon *) entry->clientdata;
655 WDock *dock;
656 WMArray *selectedIcons;
657 WMArrayIterator iter;
658 WAppIcon *btn;
659 int i;
661 assert(clickedIcon != NULL);
662 dock = clickedIcon->dock;
664 selectedIcons = getSelected(dock);
666 if (!WMGetArrayItemCount(selectedIcons)) {
667 for (i = 1; i < dock->max_icons; i++) {
668 btn = dock->icon_array[i];
669 if (btn && !btn->icon->selected) {
670 wIconSelect(btn->icon);
673 } else {
674 WM_ITERATE_ARRAY(selectedIcons, btn, iter) {
675 wIconSelect(btn->icon);
678 WMFreeArray(selectedIcons);
680 wMenuPaint(menu);
683 static void toggleCollapsedCallback(WMenu * menu, WMenuEntry * entry)
685 assert(entry->clientdata != NULL);
687 toggleCollapsed(entry->clientdata);
689 entry->flags.indicator_on = ((WDock *) entry->clientdata)->collapsed;
691 wMenuPaint(menu);
694 static void toggleAutoCollapseCallback(WMenu * menu, WMenuEntry * entry)
696 WDock *dock;
697 assert(entry->clientdata != NULL);
699 dock = (WDock *) entry->clientdata;
701 dock->auto_collapse = !dock->auto_collapse;
703 entry->flags.indicator_on = ((WDock *) entry->clientdata)->auto_collapse;
705 wMenuPaint(menu);
708 static void toggleAutoRaiseLowerCallback(WMenu * menu, WMenuEntry * entry)
710 WDock *dock;
711 assert(entry->clientdata != NULL);
713 dock = (WDock *) entry->clientdata;
715 dock->auto_raise_lower = !dock->auto_raise_lower;
717 entry->flags.indicator_on = ((WDock *) entry->clientdata)->auto_raise_lower;
719 wMenuPaint(menu);
722 static void launchCallback(WMenu * menu, WMenuEntry * entry)
724 WAppIcon *btn = (WAppIcon *) entry->clientdata;
726 launchDockedApplication(btn, False);
729 static void settingsCallback(WMenu * menu, WMenuEntry * entry)
731 WAppIcon *btn = (WAppIcon *) entry->clientdata;
733 if (btn->editing)
734 return;
735 ShowDockAppSettingsPanel(btn);
738 static void hideCallback(WMenu * menu, WMenuEntry * entry)
740 WApplication *wapp;
741 WAppIcon *btn = (WAppIcon *) entry->clientdata;
743 wapp = wApplicationOf(btn->icon->owner->main_window);
745 if (wapp->flags.hidden) {
746 wWorkspaceChange(btn->icon->core->screen_ptr, wapp->last_workspace);
747 wUnhideApplication(wapp, False, False);
748 } else {
749 wHideApplication(wapp);
753 static void unhideHereCallback(WMenu * menu, WMenuEntry * entry)
755 WApplication *wapp;
756 WAppIcon *btn = (WAppIcon *) entry->clientdata;
758 wapp = wApplicationOf(btn->icon->owner->main_window);
760 wUnhideApplication(wapp, False, True);
763 WAppIcon *mainIconCreate(WScreen * scr, int type)
765 WAppIcon *btn;
766 int x_pos;
768 if (type == WM_CLIP) {
769 if (scr->clip_icon)
770 return scr->clip_icon;
771 btn = wAppIconCreateForDock(scr, NULL, "Logo", "WMClip", TILE_CLIP);
772 btn->icon->core->descriptor.handle_expose = clipIconExpose;
773 btn->icon->core->descriptor.handle_enternotify = clipEnterNotify;
774 btn->icon->core->descriptor.handle_leavenotify = clipLeaveNotify;
775 /*x_pos = scr->scr_width - ICON_SIZE*2 - DOCK_EXTRA_SPACE; */
776 x_pos = 0;
777 } else {
778 btn = wAppIconCreateForDock(scr, NULL, "Logo", "WMDock", TILE_NORMAL);
779 x_pos = scr->scr_width - ICON_SIZE - DOCK_EXTRA_SPACE;
782 btn->xindex = 0;
783 btn->yindex = 0;
785 btn->icon->core->descriptor.handle_mousedown = iconMouseDown;
786 btn->icon->core->descriptor.parent_type = WCLASS_DOCK_ICON;
787 btn->icon->core->descriptor.parent = btn;
788 /*ChangeStackingLevel(btn->icon->core, WMDockLevel); */
789 XMapWindow(dpy, btn->icon->core->window);
790 btn->x_pos = x_pos;
791 btn->y_pos = 0;
792 btn->docked = 1;
793 if (type == WM_CLIP)
794 scr->clip_icon = btn;
796 return btn;
799 static void switchWSCommand(WMenu * menu, WMenuEntry * entry)
801 WAppIcon *btn, *icon = (WAppIcon *) entry->clientdata;
802 WScreen *scr = icon->icon->core->screen_ptr;
803 WDock *src, *dest;
804 WMArray *selectedIcons;
805 int x, y;
807 if (entry->order == scr->current_workspace)
808 return;
809 src = icon->dock;
810 dest = scr->workspaces[entry->order]->clip;
812 selectedIcons = getSelected(src);
814 if (WMGetArrayItemCount(selectedIcons)) {
815 WMArrayIterator iter;
817 WM_ITERATE_ARRAY(selectedIcons, btn, iter) {
818 if (wDockFindFreeSlot(dest, &x, &y)) {
819 moveIconBetweenDocks(src, dest, btn, x, y);
820 XUnmapWindow(dpy, btn->icon->core->window);
823 } else if (icon != scr->clip_icon) {
824 if (wDockFindFreeSlot(dest, &x, &y)) {
825 moveIconBetweenDocks(src, dest, icon, x, y);
826 XUnmapWindow(dpy, icon->icon->core->window);
829 WMFreeArray(selectedIcons);
832 static void launchDockedApplication(WAppIcon * btn, Bool withSelection)
834 WScreen *scr = btn->icon->core->screen_ptr;
836 if (!btn->launching &&
837 ((!withSelection && btn->command != NULL) || (withSelection && btn->paste_command != NULL))) {
838 if (!btn->forced_dock) {
839 btn->relaunching = btn->running;
840 btn->running = 1;
842 if (btn->wm_instance || btn->wm_class) {
843 WWindowAttributes attr;
844 memset(&attr, 0, sizeof(WWindowAttributes));
845 wDefaultFillAttributes(scr, btn->wm_instance, btn->wm_class, &attr, NULL, True);
847 if (!attr.no_appicon && !btn->buggy_app)
848 btn->launching = 1;
849 else
850 btn->running = 0;
852 btn->drop_launch = 0;
853 btn->paste_launch = withSelection;
854 scr->last_dock = btn->dock;
855 btn->pid = execCommand(btn, (withSelection ? btn->paste_command : btn->command), NULL);
856 if (btn->pid > 0) {
857 if (btn->buggy_app) {
858 /* give feedback that the app was launched */
859 btn->launching = 1;
860 dockIconPaint(btn);
861 btn->launching = 0;
862 WMAddTimerHandler(200, (WMCallback *) dockIconPaint, btn);
863 } else {
864 dockIconPaint(btn);
866 } else {
867 wwarning(_("could not launch application %s"), btn->command);
868 btn->launching = 0;
869 if (!btn->relaunching) {
870 btn->running = 0;
876 static void updateWorkspaceMenu(WMenu * menu, WAppIcon * icon)
878 WScreen *scr = menu->frame->screen_ptr;
879 char title[MAX_WORKSPACENAME_WIDTH + 1];
880 int i;
882 if (!menu || !icon)
883 return;
885 for (i = 0; i < scr->workspace_count; i++) {
886 if (i < menu->entry_no) {
887 if (strcmp(menu->entries[i]->text, scr->workspaces[i]->name) != 0) {
888 wfree(menu->entries[i]->text);
889 strcpy(title, scr->workspaces[i]->name);
890 menu->entries[i]->text = wstrdup(title);
891 menu->flags.realized = 0;
893 menu->entries[i]->clientdata = (void *)icon;
894 } else {
895 strcpy(title, scr->workspaces[i]->name);
897 wMenuAddCallback(menu, title, switchWSCommand, (void *)icon);
899 menu->flags.realized = 0;
901 if (i == scr->current_workspace) {
902 wMenuSetEnabled(menu, i, False);
903 } else {
904 wMenuSetEnabled(menu, i, True);
908 if (!menu->flags.realized)
909 wMenuRealize(menu);
912 static WMenu *makeWorkspaceMenu(WScreen * scr)
914 WMenu *menu;
916 menu = wMenuCreate(scr, NULL, False);
917 if (!menu)
918 wwarning(_("could not create workspace submenu for Clip menu"));
920 wMenuAddCallback(menu, "", switchWSCommand, (void *)scr->clip_icon);
922 menu->flags.realized = 0;
923 wMenuRealize(menu);
925 return menu;
928 static void updateClipOptionsMenu(WMenu * menu, WDock * dock)
930 WMenuEntry *entry;
931 int index = 0;
933 if (!menu || !dock)
934 return;
936 /* keep on top */
937 entry = menu->entries[index];
938 entry->flags.indicator_on = !dock->lowered;
939 entry->clientdata = dock;
941 /* collapsed */
942 entry = menu->entries[++index];
943 entry->flags.indicator_on = dock->collapsed;
944 entry->clientdata = dock;
946 /* auto-collapse */
947 entry = menu->entries[++index];
948 entry->flags.indicator_on = dock->auto_collapse;
949 entry->clientdata = dock;
951 /* auto-raise/lower */
952 entry = menu->entries[++index];
953 entry->flags.indicator_on = dock->auto_raise_lower;
954 entry->clientdata = dock;
955 wMenuSetEnabled(menu, index, dock->lowered);
957 /* attract icons */
958 entry = menu->entries[++index];
959 entry->flags.indicator_on = dock->attract_icons;
960 entry->clientdata = dock;
962 menu->flags.realized = 0;
963 wMenuRealize(menu);
966 static WMenu *makeClipOptionsMenu(WScreen * scr)
968 WMenu *menu;
969 WMenuEntry *entry;
971 menu = wMenuCreate(scr, NULL, False);
972 if (!menu) {
973 wwarning(_("could not create options submenu for Clip menu"));
974 return NULL;
977 entry = wMenuAddCallback(menu, _("Keep on Top"), toggleLoweredCallback, NULL);
978 entry->flags.indicator = 1;
979 entry->flags.indicator_on = 1;
980 entry->flags.indicator_type = MI_CHECK;
982 entry = wMenuAddCallback(menu, _("Collapsed"), toggleCollapsedCallback, NULL);
983 entry->flags.indicator = 1;
984 entry->flags.indicator_on = 1;
985 entry->flags.indicator_type = MI_CHECK;
987 entry = wMenuAddCallback(menu, _("Autocollapse"), toggleAutoCollapseCallback, NULL);
988 entry->flags.indicator = 1;
989 entry->flags.indicator_on = 1;
990 entry->flags.indicator_type = MI_CHECK;
992 entry = wMenuAddCallback(menu, _("Autoraise"), toggleAutoRaiseLowerCallback, NULL);
993 entry->flags.indicator = 1;
994 entry->flags.indicator_on = 1;
995 entry->flags.indicator_type = MI_CHECK;
997 entry = wMenuAddCallback(menu, _("Autoattract Icons"), toggleAutoAttractCallback, NULL);
998 entry->flags.indicator = 1;
999 entry->flags.indicator_on = 1;
1000 entry->flags.indicator_type = MI_CHECK;
1002 menu->flags.realized = 0;
1003 wMenuRealize(menu);
1005 return menu;
1008 static WMenu *dockMenuCreate(WScreen * scr, int type)
1010 WMenu *menu;
1011 WMenuEntry *entry;
1013 if (type == WM_CLIP && scr->clip_menu)
1014 return scr->clip_menu;
1016 menu = wMenuCreate(scr, NULL, False);
1017 if (type != WM_CLIP) {
1018 entry = wMenuAddCallback(menu, _("Keep on Top"), toggleLoweredCallback, NULL);
1019 entry->flags.indicator = 1;
1020 entry->flags.indicator_on = 1;
1021 entry->flags.indicator_type = MI_CHECK;
1022 } else {
1023 entry = wMenuAddCallback(menu, _("Clip Options"), NULL, NULL);
1024 scr->clip_options = makeClipOptionsMenu(scr);
1025 if (scr->clip_options)
1026 wMenuEntrySetCascade(menu, entry, scr->clip_options);
1028 entry = wMenuAddCallback(menu, _("Rename Workspace"), renameCallback, NULL);
1029 wfree(entry->text);
1030 entry->text = _("Rename Workspace");
1032 entry = wMenuAddCallback(menu, _("Selected"), selectCallback, NULL);
1033 entry->flags.indicator = 1;
1034 entry->flags.indicator_on = 1;
1035 entry->flags.indicator_type = MI_CHECK;
1037 entry = wMenuAddCallback(menu, _("Select All Icons"), selectIconsCallback, NULL);
1038 wfree(entry->text);
1039 entry->text = _("Select All Icons");
1041 entry = wMenuAddCallback(menu, _("Keep Icon"), keepIconsCallback, NULL);
1042 wfree(entry->text);
1043 entry->text = _("Keep Icon");
1045 entry = wMenuAddCallback(menu, _("Move Icon To"), NULL, NULL);
1046 wfree(entry->text);
1047 entry->text = _("Move Icon To");
1048 scr->clip_submenu = makeWorkspaceMenu(scr);
1049 if (scr->clip_submenu)
1050 wMenuEntrySetCascade(menu, entry, scr->clip_submenu);
1052 entry = wMenuAddCallback(menu, _("Remove Icon"), removeIconsCallback, NULL);
1053 wfree(entry->text);
1054 entry->text = _("Remove Icon");
1056 wMenuAddCallback(menu, _("Attract Icons"), colectIconsCallback, NULL);
1059 wMenuAddCallback(menu, _("Launch"), launchCallback, NULL);
1061 wMenuAddCallback(menu, _("Unhide Here"), unhideHereCallback, NULL);
1063 entry = wMenuAddCallback(menu, _("Hide"), hideCallback, NULL);
1064 wfree(entry->text);
1065 entry->text = _("Hide");
1067 wMenuAddCallback(menu, _("Settings..."), settingsCallback, NULL);
1069 wMenuAddCallback(menu, _("Kill"), killCallback, NULL);
1071 if (type == WM_CLIP)
1072 scr->clip_menu = menu;
1074 return menu;
1077 WDock *wDockCreate(WScreen * scr, int type)
1079 WDock *dock;
1080 WAppIcon *btn;
1081 int icon_count;
1083 make_keys();
1085 dock = wmalloc(sizeof(WDock));
1086 memset(dock, 0, sizeof(WDock));
1088 if (type == WM_CLIP)
1089 icon_count = CLIP_MAX_ICONS;
1090 else
1091 icon_count = scr->scr_height / wPreferences.icon_size;
1093 dock->icon_array = wmalloc(sizeof(WAppIcon *) * icon_count);
1094 memset(dock->icon_array, 0, sizeof(WAppIcon *) * icon_count);
1096 dock->max_icons = icon_count;
1098 btn = mainIconCreate(scr, type);
1100 btn->dock = dock;
1102 dock->x_pos = btn->x_pos;
1103 dock->y_pos = btn->y_pos;
1104 dock->screen_ptr = scr;
1105 dock->type = type;
1106 dock->icon_count = 1;
1107 dock->on_right_side = 1;
1108 dock->collapsed = 0;
1109 dock->auto_collapse = 0;
1110 dock->auto_collapse_magic = NULL;
1111 dock->auto_raise_lower = 0;
1112 dock->auto_lower_magic = NULL;
1113 dock->auto_raise_magic = NULL;
1114 dock->attract_icons = 0;
1115 dock->lowered = 1;
1116 dock->icon_array[0] = btn;
1117 wRaiseFrame(btn->icon->core);
1118 XMoveWindow(dpy, btn->icon->core->window, btn->x_pos, btn->y_pos);
1120 /* create dock menu */
1121 dock->menu = dockMenuCreate(scr, type);
1123 return dock;
1126 void wDockDestroy(WDock * dock)
1128 int i;
1129 WAppIcon *aicon;
1131 for (i = (dock->type == WM_CLIP) ? 1 : 0; i < dock->max_icons; i++) {
1132 aicon = dock->icon_array[i];
1133 if (aicon) {
1134 int keepit = aicon->running && wApplicationOf(aicon->main_window);
1135 wDockDetach(dock, aicon);
1136 if (keepit) {
1137 /* XXX: can: aicon->icon == NULL ? */
1138 PlaceIcon(dock->screen_ptr, &aicon->x_pos, &aicon->y_pos,
1139 wGetHeadForWindow(aicon->icon->owner));
1140 XMoveWindow(dpy, aicon->icon->core->window, aicon->x_pos, aicon->y_pos);
1141 if (!dock->mapped || dock->collapsed)
1142 XMapWindow(dpy, aicon->icon->core->window);
1146 if (wPreferences.auto_arrange_icons)
1147 wArrangeIcons(dock->screen_ptr, True);
1148 wfree(dock->icon_array);
1149 if (dock->menu && dock->type != WM_CLIP)
1150 wMenuDestroy(dock->menu, True);
1151 if (dock->screen_ptr->last_dock == dock)
1152 dock->screen_ptr->last_dock = NULL;
1153 wfree(dock);
1156 void wClipIconPaint(WAppIcon * aicon)
1158 WScreen *scr = aicon->icon->core->screen_ptr;
1159 WWorkspace *workspace = scr->workspaces[scr->current_workspace];
1160 WMColor *color;
1161 Window win = aicon->icon->core->window;
1162 int length, nlength;
1163 char *ws_name, ws_number[10];
1164 int ty, tx;
1166 wIconPaint(aicon->icon);
1168 length = strlen(workspace->name);
1169 ws_name = wmalloc(length + 1);
1170 snprintf(ws_name, length + 1, "%s", workspace->name);
1171 snprintf(ws_number, sizeof(ws_number), "%i", scr->current_workspace + 1);
1172 nlength = strlen(ws_number);
1174 if (!workspace->clip->collapsed)
1175 color = scr->clip_title_color[CLIP_NORMAL];
1176 else
1177 color = scr->clip_title_color[CLIP_COLLAPSED];
1179 ty = ICON_SIZE - WMFontHeight(scr->clip_title_font) - 3;
1181 tx = CLIP_BUTTON_SIZE * ICON_SIZE / 64;
1183 if(wPreferences.show_clip_title)
1184 WMDrawString(scr->wmscreen, win, color, scr->clip_title_font, tx, ty, ws_name, length);
1185 /*WMDrawString(scr->wmscreen, win, color, scr->clip_title_font, 4,
1186 2, ws_name, length); */
1188 tx = (ICON_SIZE / 2 - WMWidthOfString(scr->clip_title_font, ws_number, nlength)) / 2;
1190 WMDrawString(scr->wmscreen, win, color, scr->clip_title_font, tx, 2, ws_number, nlength);
1192 wfree(ws_name);
1194 if (aicon->launching) {
1195 XFillRectangle(dpy, aicon->icon->core->window, scr->stipple_gc,
1196 0, 0, wPreferences.icon_size, wPreferences.icon_size);
1198 paintClipButtons(aicon, aicon->dock->lclip_button_pushed, aicon->dock->rclip_button_pushed);
1201 static void clipIconExpose(WObjDescriptor * desc, XEvent * event)
1203 wClipIconPaint(desc->parent);
1206 static void dockIconPaint(WAppIcon * btn)
1208 if (btn == btn->icon->core->screen_ptr->clip_icon)
1209 wClipIconPaint(btn);
1210 else
1211 wAppIconPaint(btn);
1214 static WMPropList *make_icon_state(WAppIcon * btn)
1216 WMPropList *node = NULL;
1217 WMPropList *command, *autolaunch, *lock, *name, *forced, *host;
1218 WMPropList *position, *buggy, *omnipresent;
1219 char *tmp;
1220 char buffer[64];
1222 if (btn) {
1223 if (!btn->command)
1224 command = WMCreatePLString("-");
1225 else
1226 command = WMCreatePLString(btn->command);
1228 autolaunch = btn->auto_launch ? dYes : dNo;
1230 lock = btn->lock ? dYes : dNo;
1232 tmp = EscapeWM_CLASS(btn->wm_instance, btn->wm_class);
1234 name = WMCreatePLString(tmp);
1236 wfree(tmp);
1238 forced = btn->forced_dock ? dYes : dNo;
1240 buggy = btn->buggy_app ? dYes : dNo;
1242 if (btn == btn->icon->core->screen_ptr->clip_icon)
1243 snprintf(buffer, sizeof(buffer), "%i,%i", btn->x_pos, btn->y_pos);
1244 else
1245 snprintf(buffer, sizeof(buffer), "%hi,%hi", btn->xindex, btn->yindex);
1246 position = WMCreatePLString(buffer);
1248 node = WMCreatePLDictionary(dCommand, command,
1249 dName, name,
1250 dAutoLaunch, autolaunch,
1251 dLock, lock,
1252 dForced, forced, dBuggyApplication, buggy, dPosition, position, NULL);
1253 WMReleasePropList(command);
1254 WMReleasePropList(name);
1255 WMReleasePropList(position);
1257 omnipresent = btn->omnipresent ? dYes : dNo;
1258 if (btn->dock != btn->icon->core->screen_ptr->dock && (btn->xindex != 0 || btn->yindex != 0))
1259 WMPutInPLDictionary(node, dOmnipresent, omnipresent);
1261 #ifdef XDND /* was OFFIX */
1262 if (btn->dnd_command) {
1263 command = WMCreatePLString(btn->dnd_command);
1264 WMPutInPLDictionary(node, dDropCommand, command);
1265 WMReleasePropList(command);
1267 #endif /* XDND */
1269 if (btn->paste_command) {
1270 command = WMCreatePLString(btn->paste_command);
1271 WMPutInPLDictionary(node, dPasteCommand, command);
1272 WMReleasePropList(command);
1275 if (btn->client_machine && btn->remote_start) {
1276 host = WMCreatePLString(btn->client_machine);
1277 WMPutInPLDictionary(node, dHost, host);
1278 WMReleasePropList(host);
1282 return node;
1285 static WMPropList *dockSaveState(WDock * dock)
1287 int i;
1288 WMPropList *icon_info;
1289 WMPropList *list = NULL, *dock_state = NULL;
1290 WMPropList *value, *key;
1291 char buffer[256];
1293 list = WMCreatePLArray(NULL);
1295 for (i = (dock->type == WM_DOCK ? 0 : 1); i < dock->max_icons; i++) {
1296 WAppIcon *btn = dock->icon_array[i];
1298 if (!btn || btn->attracted)
1299 continue;
1301 if ((icon_info = make_icon_state(dock->icon_array[i]))) {
1302 WMAddToPLArray(list, icon_info);
1303 WMReleasePropList(icon_info);
1307 dock_state = WMCreatePLDictionary(dApplications, list, NULL);
1309 if (dock->type == WM_DOCK) {
1310 snprintf(buffer, sizeof(buffer), "Applications%i", dock->screen_ptr->scr_height);
1311 key = WMCreatePLString(buffer);
1312 WMPutInPLDictionary(dock_state, key, list);
1313 WMReleasePropList(key);
1315 snprintf(buffer, sizeof(buffer), "%i,%i", (dock->on_right_side ? -ICON_SIZE : 0), dock->y_pos);
1316 value = WMCreatePLString(buffer);
1317 WMPutInPLDictionary(dock_state, dPosition, value);
1318 WMReleasePropList(value);
1320 WMReleasePropList(list);
1322 value = (dock->lowered ? dYes : dNo);
1323 WMPutInPLDictionary(dock_state, dLowered, value);
1325 if (dock->type == WM_CLIP) {
1326 value = (dock->collapsed ? dYes : dNo);
1327 WMPutInPLDictionary(dock_state, dCollapsed, value);
1329 value = (dock->auto_collapse ? dYes : dNo);
1330 WMPutInPLDictionary(dock_state, dAutoCollapse, value);
1332 value = (dock->auto_raise_lower ? dYes : dNo);
1333 WMPutInPLDictionary(dock_state, dAutoRaiseLower, value);
1335 value = (dock->attract_icons ? dYes : dNo);
1336 WMPutInPLDictionary(dock_state, dAutoAttractIcons, value);
1339 return dock_state;
1342 void wDockSaveState(WScreen * scr, WMPropList * old_state)
1344 WMPropList *dock_state;
1345 WMPropList *keys;
1347 dock_state = dockSaveState(scr->dock);
1350 * Copy saved states of docks with different sizes.
1352 if (old_state) {
1353 int i;
1354 WMPropList *tmp;
1356 keys = WMGetPLDictionaryKeys(old_state);
1357 for (i = 0; i < WMGetPropListItemCount(keys); i++) {
1358 tmp = WMGetFromPLArray(keys, i);
1360 if (strncasecmp(WMGetFromPLString(tmp), "applications", 12) == 0
1361 && !WMGetFromPLDictionary(dock_state, tmp)) {
1363 WMPutInPLDictionary(dock_state, tmp, WMGetFromPLDictionary(old_state, tmp));
1366 WMReleasePropList(keys);
1369 WMPutInPLDictionary(scr->session_state, dDock, dock_state);
1371 WMReleasePropList(dock_state);
1374 void wClipSaveState(WScreen * scr)
1376 WMPropList *clip_state;
1378 clip_state = make_icon_state(scr->clip_icon);
1380 WMPutInPLDictionary(scr->session_state, dClip, clip_state);
1382 WMReleasePropList(clip_state);
1385 WMPropList *wClipSaveWorkspaceState(WScreen * scr, int workspace)
1387 return dockSaveState(scr->workspaces[workspace]->clip);
1390 static Bool getBooleanDockValue(WMPropList * value, WMPropList * key)
1392 if (value) {
1393 if (WMIsPLString(value)) {
1394 if (strcasecmp(WMGetFromPLString(value), "YES") == 0)
1395 return True;
1396 } else {
1397 wwarning(_("bad value in docked icon state info %s"), WMGetFromPLString(key));
1400 return False;
1403 static WAppIcon *restore_icon_state(WScreen * scr, WMPropList * info, int type, int index)
1405 WAppIcon *aicon;
1406 WMPropList *cmd, *value;
1408 cmd = WMGetFromPLDictionary(info, dCommand);
1409 if (!cmd || !WMIsPLString(cmd)) {
1410 return NULL;
1413 /* parse window name */
1414 value = WMGetFromPLDictionary(info, dName);
1415 if (!value)
1416 return NULL;
1419 char *wclass, *winstance;
1420 char *command;
1422 ParseWindowName(value, &winstance, &wclass, "dock");
1424 if (!winstance && !wclass) {
1425 return NULL;
1428 /* get commands */
1430 if (cmd)
1431 command = wstrdup(WMGetFromPLString(cmd));
1432 else
1433 command = NULL;
1435 if (!command || strcmp(command, "-") == 0) {
1436 if (command)
1437 wfree(command);
1438 if (wclass)
1439 wfree(wclass);
1440 if (winstance)
1441 wfree(winstance);
1443 return NULL;
1446 aicon = wAppIconCreateForDock(scr, command, winstance, wclass, TILE_NORMAL);
1447 if (wclass)
1448 wfree(wclass);
1449 if (winstance)
1450 wfree(winstance);
1451 if (command)
1452 wfree(command);
1455 aicon->icon->core->descriptor.handle_mousedown = iconMouseDown;
1456 if (type == WM_CLIP) {
1457 aicon->icon->core->descriptor.handle_enternotify = clipEnterNotify;
1458 aicon->icon->core->descriptor.handle_leavenotify = clipLeaveNotify;
1460 aicon->icon->core->descriptor.parent_type = WCLASS_DOCK_ICON;
1461 aicon->icon->core->descriptor.parent = aicon;
1463 #ifdef XDND /* was OFFIX */
1464 cmd = WMGetFromPLDictionary(info, dDropCommand);
1465 if (cmd)
1466 aicon->dnd_command = wstrdup(WMGetFromPLString(cmd));
1467 #endif
1469 cmd = WMGetFromPLDictionary(info, dPasteCommand);
1470 if (cmd)
1471 aicon->paste_command = wstrdup(WMGetFromPLString(cmd));
1473 /* check auto launch */
1474 value = WMGetFromPLDictionary(info, dAutoLaunch);
1476 aicon->auto_launch = getBooleanDockValue(value, dAutoLaunch);
1478 /* check lock */
1479 value = WMGetFromPLDictionary(info, dLock);
1481 aicon->lock = getBooleanDockValue(value, dLock);
1483 /* check if it wasn't normally docked */
1484 value = WMGetFromPLDictionary(info, dForced);
1486 aicon->forced_dock = getBooleanDockValue(value, dForced);
1488 /* check if we can rely on the stuff in the app */
1489 value = WMGetFromPLDictionary(info, dBuggyApplication);
1491 aicon->buggy_app = getBooleanDockValue(value, dBuggyApplication);
1493 /* get position in the dock */
1494 value = WMGetFromPLDictionary(info, dPosition);
1495 if (value && WMIsPLString(value)) {
1496 if (sscanf(WMGetFromPLString(value), "%hi,%hi", &aicon->xindex, &aicon->yindex) != 2)
1497 wwarning(_("bad value in docked icon state info %s"), WMGetFromPLString(dPosition));
1499 /* check position sanity */
1500 /* incomplete section! */
1501 if (type == WM_DOCK) {
1502 aicon->xindex = 0;
1503 if (aicon->yindex < 0)
1504 wwarning(_("bad value in docked icon position %i,%i"),
1505 aicon->xindex, aicon->yindex);
1507 } else {
1508 aicon->yindex = index;
1509 aicon->xindex = 0;
1512 /* check if icon is omnipresent */
1513 value = WMGetFromPLDictionary(info, dOmnipresent);
1515 aicon->omnipresent = getBooleanDockValue(value, dOmnipresent);
1517 aicon->running = 0;
1518 aicon->docked = 1;
1520 return aicon;
1523 #define COMPLAIN(key) wwarning(_("bad value in dock state info:%s"), key)
1525 WAppIcon *wClipRestoreState(WScreen * scr, WMPropList * clip_state)
1527 WAppIcon *icon;
1528 WMPropList *value;
1530 icon = mainIconCreate(scr, WM_CLIP);
1532 if (!clip_state)
1533 return icon;
1535 WMRetainPropList(clip_state);
1537 /* restore position */
1539 value = WMGetFromPLDictionary(clip_state, dPosition);
1541 if (value) {
1542 if (!WMIsPLString(value))
1543 COMPLAIN("Position");
1544 else {
1545 WMRect rect;
1546 int flags;
1548 if (sscanf(WMGetFromPLString(value), "%i,%i", &icon->x_pos, &icon->y_pos) != 2)
1549 COMPLAIN("Position");
1551 /* check position sanity */
1552 rect.pos.x = icon->x_pos;
1553 rect.pos.y = icon->y_pos;
1554 rect.size.width = rect.size.height = ICON_SIZE;
1556 wGetRectPlacementInfo(scr, rect, &flags);
1557 if (flags & (XFLAG_DEAD | XFLAG_PARTIAL))
1558 wScreenKeepInside(scr, &icon->x_pos, &icon->y_pos, ICON_SIZE, ICON_SIZE);
1561 #ifdef XDND /* was OFFIX */
1562 value = WMGetFromPLDictionary(clip_state, dDropCommand);
1563 if (value && WMIsPLString(value))
1564 icon->dnd_command = wstrdup(WMGetFromPLString(value));
1565 #endif
1567 value = WMGetFromPLDictionary(clip_state, dPasteCommand);
1568 if (value && WMIsPLString(value))
1569 icon->paste_command = wstrdup(WMGetFromPLString(value));
1571 WMReleasePropList(clip_state);
1573 return icon;
1576 WDock *wDockRestoreState(WScreen * scr, WMPropList * dock_state, int type)
1578 WDock *dock;
1579 WMPropList *apps;
1580 WMPropList *value;
1581 WAppIcon *aicon, *old_top;
1582 int count, i;
1584 dock = wDockCreate(scr, type);
1586 if (!dock_state)
1587 return dock;
1589 WMRetainPropList(dock_state);
1591 /* restore position */
1593 value = WMGetFromPLDictionary(dock_state, dPosition);
1595 if (value) {
1596 if (!WMIsPLString(value)) {
1597 COMPLAIN("Position");
1598 } else {
1599 WMRect rect;
1600 int flags;
1602 if (sscanf(WMGetFromPLString(value), "%i,%i", &dock->x_pos, &dock->y_pos) != 2)
1603 COMPLAIN("Position");
1605 /* check position sanity */
1606 rect.pos.x = dock->x_pos;
1607 rect.pos.y = dock->y_pos;
1608 rect.size.width = rect.size.height = ICON_SIZE;
1610 wGetRectPlacementInfo(scr, rect, &flags);
1611 if (flags & (XFLAG_DEAD | XFLAG_PARTIAL)) {
1612 int x = dock->x_pos;
1613 wScreenKeepInside(scr, &x, &dock->y_pos, ICON_SIZE, ICON_SIZE);
1616 /* Is this needed any more? */
1617 if (type == WM_CLIP) {
1618 if (dock->x_pos < 0) {
1619 dock->x_pos = 0;
1620 } else if (dock->x_pos > scr->scr_width - ICON_SIZE) {
1621 dock->x_pos = scr->scr_width - ICON_SIZE;
1623 } else {
1624 if (dock->x_pos >= 0) {
1625 dock->x_pos = DOCK_EXTRA_SPACE;
1626 dock->on_right_side = 0;
1627 } else {
1628 dock->x_pos = scr->scr_width - DOCK_EXTRA_SPACE - ICON_SIZE;
1629 dock->on_right_side = 1;
1635 /* restore lowered/raised state */
1637 dock->lowered = 0;
1639 value = WMGetFromPLDictionary(dock_state, dLowered);
1641 if (value) {
1642 if (!WMIsPLString(value)) {
1643 COMPLAIN("Lowered");
1644 } else {
1645 if (strcasecmp(WMGetFromPLString(value), "YES") == 0) {
1646 dock->lowered = 1;
1651 /* restore collapsed state */
1653 dock->collapsed = 0;
1655 value = WMGetFromPLDictionary(dock_state, dCollapsed);
1657 if (value) {
1658 if (!WMIsPLString(value)) {
1659 COMPLAIN("Collapsed");
1660 } else {
1661 if (strcasecmp(WMGetFromPLString(value), "YES") == 0) {
1662 dock->collapsed = 1;
1667 /* restore auto-collapsed state */
1669 value = WMGetFromPLDictionary(dock_state, dAutoCollapse);
1671 if (value) {
1672 if (!WMIsPLString(value)) {
1673 COMPLAIN("AutoCollapse");
1674 } else {
1675 if (strcasecmp(WMGetFromPLString(value), "YES") == 0) {
1676 dock->auto_collapse = 1;
1677 dock->collapsed = 1;
1682 /* restore auto-raise/lower state */
1684 value = WMGetFromPLDictionary(dock_state, dAutoRaiseLower);
1686 if (value) {
1687 if (!WMIsPLString(value)) {
1688 COMPLAIN("AutoRaiseLower");
1689 } else {
1690 if (strcasecmp(WMGetFromPLString(value), "YES") == 0) {
1691 dock->auto_raise_lower = 1;
1696 /* restore attract icons state */
1698 dock->attract_icons = 0;
1700 value = WMGetFromPLDictionary(dock_state, dAutoAttractIcons);
1702 if (value) {
1703 if (!WMIsPLString(value)) {
1704 COMPLAIN("AutoAttractIcons");
1705 } else {
1706 if (strcasecmp(WMGetFromPLString(value), "YES") == 0) {
1707 dock->attract_icons = 1;
1712 /* application list */
1715 WMPropList *tmp;
1716 char buffer[64];
1719 * When saving, it saves the dock state in
1720 * Applications and Applicationsnnn
1722 * When loading, it will first try Applicationsnnn.
1723 * If it does not exist, use Applications as default.
1726 snprintf(buffer, sizeof(buffer), "Applications%i", scr->scr_height);
1728 tmp = WMCreatePLString(buffer);
1729 apps = WMGetFromPLDictionary(dock_state, tmp);
1730 WMReleasePropList(tmp);
1732 if (!apps) {
1733 apps = WMGetFromPLDictionary(dock_state, dApplications);
1737 if (!apps) {
1738 goto finish;
1741 count = WMGetPropListItemCount(apps);
1743 if (count == 0)
1744 goto finish;
1746 old_top = dock->icon_array[0];
1748 /* dock->icon_count is set to 1 when dock is created.
1749 * Since Clip is already restored, we want to keep it so for clip,
1750 * but for dock we may change the default top tile, so we set it to 0.
1752 if (type == WM_DOCK)
1753 dock->icon_count = 0;
1755 for (i = 0; i < count; i++) {
1756 if (dock->icon_count >= dock->max_icons) {
1757 wwarning(_("there are too many icons stored in dock. Ignoring what doesn't fit"));
1758 break;
1761 value = WMGetFromPLArray(apps, i);
1762 aicon = restore_icon_state(scr, value, type, dock->icon_count);
1764 dock->icon_array[dock->icon_count] = aicon;
1766 if (aicon) {
1767 aicon->dock = dock;
1768 aicon->x_pos = dock->x_pos + (aicon->xindex * ICON_SIZE);
1769 aicon->y_pos = dock->y_pos + (aicon->yindex * ICON_SIZE);
1771 if (dock->lowered)
1772 ChangeStackingLevel(aicon->icon->core, WMNormalLevel);
1773 else
1774 ChangeStackingLevel(aicon->icon->core, WMDockLevel);
1776 wCoreConfigure(aicon->icon->core, aicon->x_pos, aicon->y_pos, 0, 0);
1778 if (!dock->collapsed)
1779 XMapWindow(dpy, aicon->icon->core->window);
1780 wRaiseFrame(aicon->icon->core);
1782 dock->icon_count++;
1783 } else if (dock->icon_count == 0 && type == WM_DOCK)
1784 dock->icon_count++;
1787 /* if the first icon is not defined, use the default */
1788 if (dock->icon_array[0] == NULL) {
1789 /* update default icon */
1790 old_top->x_pos = dock->x_pos;
1791 old_top->y_pos = dock->y_pos;
1792 if (dock->lowered)
1793 ChangeStackingLevel(old_top->icon->core, WMNormalLevel);
1794 else
1795 ChangeStackingLevel(old_top->icon->core, WMDockLevel);
1796 dock->icon_array[0] = old_top;
1797 XMoveWindow(dpy, old_top->icon->core->window, dock->x_pos, dock->y_pos);
1798 /* we don't need to increment dock->icon_count here because it was
1799 * incremented in the loop above.
1801 } else if (old_top != dock->icon_array[0]) {
1802 if (old_top == scr->clip_icon)
1803 scr->clip_icon = dock->icon_array[0];
1804 wAppIconDestroy(old_top);
1807 finish:
1808 WMReleasePropList(dock_state);
1810 return dock;
1813 void wDockLaunchWithState(WDock * dock, WAppIcon * btn, WSavedState * state)
1815 if (btn && btn->command && !btn->running && !btn->launching) {
1817 btn->drop_launch = 0;
1818 btn->paste_launch = 0;
1820 btn->pid = execCommand(btn, btn->command, state);
1822 if (btn->pid > 0) {
1823 if (!btn->forced_dock && !btn->buggy_app) {
1824 btn->launching = 1;
1825 dockIconPaint(btn);
1828 } else {
1829 wfree(state);
1833 void wDockDoAutoLaunch(WDock * dock, int workspace)
1835 WAppIcon *btn;
1836 WSavedState *state;
1837 int i;
1839 for (i = 0; i < dock->max_icons; i++) {
1840 btn = dock->icon_array[i];
1841 if (!btn || !btn->auto_launch)
1842 continue;
1844 state = wmalloc(sizeof(WSavedState));
1845 memset(state, 0, sizeof(WSavedState));
1846 state->workspace = workspace;
1847 /* TODO: this is klugy and is very difficult to understand
1848 * what's going on. Try to clean up */
1849 wDockLaunchWithState(dock, btn, state);
1853 #ifdef XDND /* was OFFIX */
1854 static WDock *findDock(WScreen * scr, XEvent * event, int *icon_pos)
1856 WDock *dock;
1857 int i;
1859 *icon_pos = -1;
1860 if ((dock = scr->dock) != NULL) {
1861 for (i = 0; i < dock->max_icons; i++) {
1862 if (dock->icon_array[i]
1863 && dock->icon_array[i]->icon->core->window == event->xclient.window) {
1864 *icon_pos = i;
1865 break;
1869 if (*icon_pos < 0 && (dock = scr->workspaces[scr->current_workspace]->clip) != NULL) {
1870 for (i = 0; i < dock->max_icons; i++) {
1871 if (dock->icon_array[i]
1872 && dock->icon_array[i]->icon->core->window == event->xclient.window) {
1873 *icon_pos = i;
1874 break;
1878 if (*icon_pos >= 0)
1879 return dock;
1880 return NULL;
1883 int wDockReceiveDNDDrop(WScreen * scr, XEvent * event)
1885 WDock *dock;
1886 WAppIcon *btn;
1887 int icon_pos;
1889 dock = findDock(scr, event, &icon_pos);
1890 if (!dock)
1891 return False;
1894 * Return True if the drop was on an application icon window.
1895 * In this case, let the ClientMessage handler redirect the
1896 * message to the app.
1898 if (dock->icon_array[icon_pos]->icon->icon_win != None)
1899 return True;
1901 if (dock->icon_array[icon_pos]->dnd_command != NULL) {
1902 scr->flags.dnd_data_convertion_status = 0;
1904 btn = dock->icon_array[icon_pos];
1906 if (!btn->forced_dock) {
1907 btn->relaunching = btn->running;
1908 btn->running = 1;
1910 if (btn->wm_instance || btn->wm_class) {
1911 WWindowAttributes attr;
1912 memset(&attr, 0, sizeof(WWindowAttributes));
1913 wDefaultFillAttributes(btn->icon->core->screen_ptr,
1914 btn->wm_instance, btn->wm_class, &attr, NULL, True);
1916 if (!attr.no_appicon)
1917 btn->launching = 1;
1918 else
1919 btn->running = 0;
1922 btn->paste_launch = 0;
1923 btn->drop_launch = 1;
1924 scr->last_dock = dock;
1925 btn->pid = execCommand(btn, btn->dnd_command, NULL);
1926 if (btn->pid > 0) {
1927 dockIconPaint(btn);
1928 } else {
1929 btn->launching = 0;
1930 if (!btn->relaunching) {
1931 btn->running = 0;
1935 return False;
1937 #endif /* XDND */
1939 Bool wDockAttachIcon(WDock * dock, WAppIcon * icon, int x, int y)
1941 WWindow *wwin;
1942 int index;
1944 wwin = icon->icon->owner;
1945 if (icon->command == NULL) {
1946 char *command;
1948 icon->editing = 0;
1950 command = GetCommandForWindow(wwin->client_win);
1951 if (command) {
1952 icon->command = command;
1953 } else {
1954 /* icon->forced_dock = 1; */
1955 if (dock->type != WM_CLIP || !icon->attracted) {
1956 icon->editing = 1;
1957 if (wInputDialog(dock->screen_ptr, _("Dock Icon"),
1958 _("Type the command used to launch the application"), &command)) {
1959 if (command && (command[0] == 0 || (command[0] == '-' && command[1] == 0))) {
1960 wfree(command);
1961 command = NULL;
1963 icon->command = command;
1964 icon->editing = 0;
1965 } else {
1966 icon->editing = 0;
1967 if (command)
1968 wfree(command);
1969 /* If the target is the dock, reject the icon. If
1970 * the target is the clip, make it an attracted icon
1972 if (dock->type == WM_CLIP) {
1973 icon->attracted = 1;
1974 if (!icon->icon->shadowed) {
1975 icon->icon->shadowed = 1;
1976 icon->icon->force_paint = 1;
1978 } else {
1979 return False;
1984 } else {
1985 icon->editing = 0;
1988 for (index = 1; index < dock->max_icons; index++)
1989 if (dock->icon_array[index] == NULL)
1990 break;
1991 /* if (index == dock->max_icons)
1992 return; */
1994 assert(index < dock->max_icons);
1996 dock->icon_array[index] = icon;
1997 icon->yindex = y;
1998 icon->xindex = x;
2000 icon->omnipresent = 0;
2002 icon->x_pos = dock->x_pos + x * ICON_SIZE;
2003 icon->y_pos = dock->y_pos + y * ICON_SIZE;
2005 dock->icon_count++;
2007 icon->running = 1;
2008 icon->launching = 0;
2009 icon->docked = 1;
2010 icon->dock = dock;
2011 icon->icon->core->descriptor.handle_mousedown = iconMouseDown;
2012 if (dock->type == WM_CLIP) {
2013 icon->icon->core->descriptor.handle_enternotify = clipEnterNotify;
2014 icon->icon->core->descriptor.handle_leavenotify = clipLeaveNotify;
2016 icon->icon->core->descriptor.parent_type = WCLASS_DOCK_ICON;
2017 icon->icon->core->descriptor.parent = icon;
2019 MoveInStackListUnder(dock->icon_array[index - 1]->icon->core, icon->icon->core);
2020 wAppIconMove(icon, icon->x_pos, icon->y_pos);
2021 wAppIconPaint(icon);
2023 if (wPreferences.auto_arrange_icons)
2024 wArrangeIcons(dock->screen_ptr, True);
2026 #ifdef XDND /* was OFFIX */
2027 if (icon->command && !icon->dnd_command) {
2028 int len = strlen(icon->command) + 8;
2029 icon->dnd_command = wmalloc(len);
2030 snprintf(icon->dnd_command, len, "%s %%d", icon->command);
2032 #endif
2034 if (icon->command && !icon->paste_command) {
2035 int len = strlen(icon->command) + 8;
2036 icon->paste_command = wmalloc(len);
2037 snprintf(icon->paste_command, len, "%s %%s", icon->command);
2040 return True;
2043 void reattachIcon(WDock * dock, WAppIcon * icon, int x, int y)
2045 int index;
2047 for (index = 1; index < dock->max_icons; index++) {
2048 if (dock->icon_array[index] == icon)
2049 break;
2051 assert(index < dock->max_icons);
2053 icon->yindex = y;
2054 icon->xindex = x;
2056 icon->x_pos = dock->x_pos + x * ICON_SIZE;
2057 icon->y_pos = dock->y_pos + y * ICON_SIZE;
2060 Bool moveIconBetweenDocks(WDock * src, WDock * dest, WAppIcon * icon, int x, int y)
2062 WWindow *wwin;
2063 char *command;
2064 int index;
2066 if (src == dest)
2067 return True; /* No move needed, we're already there */
2069 if (dest == NULL)
2070 return False;
2072 wwin = icon->icon->owner;
2075 * For the moment we can't do this if we move icons in Clip from one
2076 * workspace to other, because if we move two or more icons without
2077 * command, the dialog box will not be able to tell us to which of the
2078 * moved icons it applies. -Dan
2080 if ((dest->type == WM_DOCK /*|| dest->keep_attracted */ ) && icon->command == NULL) {
2081 command = GetCommandForWindow(wwin->client_win);
2082 if (command) {
2083 icon->command = command;
2084 } else {
2085 icon->editing = 1;
2086 /* icon->forced_dock = 1; */
2087 if (wInputDialog(src->screen_ptr, _("Dock Icon"),
2088 _("Type the command used to launch the application"), &command)) {
2089 if (command && (command[0] == 0 || (command[0] == '-' && command[1] == 0))) {
2090 wfree(command);
2091 command = NULL;
2093 icon->command = command;
2094 } else {
2095 icon->editing = 0;
2096 if (command)
2097 wfree(command);
2098 return False;
2100 icon->editing = 0;
2104 if (dest->type == WM_DOCK)
2105 wClipMakeIconOmnipresent(icon, False);
2107 for (index = 1; index < src->max_icons; index++) {
2108 if (src->icon_array[index] == icon)
2109 break;
2111 assert(index < src->max_icons);
2113 src->icon_array[index] = NULL;
2114 src->icon_count--;
2116 for (index = 1; index < dest->max_icons; index++) {
2117 if (dest->icon_array[index] == NULL)
2118 break;
2120 /* if (index == dest->max_icons)
2121 return; */
2123 assert(index < dest->max_icons);
2125 dest->icon_array[index] = icon;
2126 icon->dock = dest;
2128 /* deselect the icon */
2129 if (icon->icon->selected)
2130 wIconSelect(icon->icon);
2132 if (dest->type == WM_DOCK) {
2133 icon->icon->core->descriptor.handle_enternotify = NULL;
2134 icon->icon->core->descriptor.handle_leavenotify = NULL;
2135 } else {
2136 icon->icon->core->descriptor.handle_enternotify = clipEnterNotify;
2137 icon->icon->core->descriptor.handle_leavenotify = clipLeaveNotify;
2140 /* set it to be kept when moving to dock.
2141 * Unless the icon does not have a command set
2143 if (icon->command && dest->type == WM_DOCK) {
2144 icon->attracted = 0;
2145 if (icon->icon->shadowed) {
2146 icon->icon->shadowed = 0;
2147 icon->icon->force_paint = 1;
2151 if (src->auto_collapse || src->auto_raise_lower)
2152 clipLeave(src);
2154 icon->yindex = y;
2155 icon->xindex = x;
2157 icon->x_pos = dest->x_pos + x * ICON_SIZE;
2158 icon->y_pos = dest->y_pos + y * ICON_SIZE;
2160 dest->icon_count++;
2162 MoveInStackListUnder(dest->icon_array[index - 1]->icon->core, icon->icon->core);
2163 wAppIconPaint(icon);
2165 return True;
2168 void wDockDetach(WDock * dock, WAppIcon * icon)
2170 int index;
2172 /* make the settings panel be closed */
2173 if (icon->panel) {
2174 DestroyDockAppSettingsPanel(icon->panel);
2177 /* This must be called before icon->dock is set to NULL.
2178 * Don't move it. -Dan
2180 wClipMakeIconOmnipresent(icon, False);
2182 icon->docked = 0;
2183 icon->dock = NULL;
2184 icon->attracted = 0;
2185 icon->auto_launch = 0;
2186 if (icon->icon->shadowed) {
2187 icon->icon->shadowed = 0;
2188 icon->icon->force_paint = 1;
2191 /* deselect the icon */
2192 if (icon->icon->selected)
2193 wIconSelect(icon->icon);
2195 if (icon->command) {
2196 wfree(icon->command);
2197 icon->command = NULL;
2199 #ifdef XDND /* was OFFIX */
2200 if (icon->dnd_command) {
2201 wfree(icon->dnd_command);
2202 icon->dnd_command = NULL;
2204 #endif
2205 if (icon->paste_command) {
2206 wfree(icon->paste_command);
2207 icon->paste_command = NULL;
2210 for (index = 1; index < dock->max_icons; index++)
2211 if (dock->icon_array[index] == icon)
2212 break;
2213 assert(index < dock->max_icons);
2214 dock->icon_array[index] = NULL;
2215 icon->yindex = -1;
2216 icon->xindex = -1;
2218 dock->icon_count--;
2220 /* if the dock is not attached to an application or
2221 * the the application did not set the approriate hints yet,
2222 * destroy the icon */
2223 if (!icon->running || !wApplicationOf(icon->main_window))
2224 wAppIconDestroy(icon);
2225 else {
2226 icon->icon->core->descriptor.handle_mousedown = appIconMouseDown;
2227 icon->icon->core->descriptor.handle_enternotify = NULL;
2228 icon->icon->core->descriptor.handle_leavenotify = NULL;
2229 icon->icon->core->descriptor.parent_type = WCLASS_APPICON;
2230 icon->icon->core->descriptor.parent = icon;
2232 ChangeStackingLevel(icon->icon->core, NORMAL_ICON_LEVEL);
2234 wAppIconPaint(icon);
2235 if (wPreferences.auto_arrange_icons) {
2236 wArrangeIcons(dock->screen_ptr, True);
2239 if (dock->auto_collapse || dock->auto_raise_lower)
2240 clipLeave(dock);
2244 * returns the closest Dock slot index for the passed
2245 * coordinates.
2247 * Returns False if icon can't be docked.
2249 * Note: this function should NEVER alter ret_x or ret_y, unless it will
2250 * return True. -Dan
2252 Bool wDockSnapIcon(WDock * dock, WAppIcon * icon, int req_x, int req_y, int *ret_x, int *ret_y, int redocking)
2254 WScreen *scr = dock->screen_ptr;
2255 int dx, dy;
2256 int ex_x, ex_y;
2257 int i, offset = ICON_SIZE / 2;
2258 WAppIcon *aicon = NULL;
2259 WAppIcon *nicon = NULL;
2260 int max_y_icons, max_x_icons;
2262 /* TODO: XINERAMA, for these */
2263 max_x_icons = scr->scr_width / ICON_SIZE;
2264 max_y_icons = scr->scr_height / ICON_SIZE - 1;
2266 if (wPreferences.flags.noupdates)
2267 return False;
2269 dx = dock->x_pos;
2270 dy = dock->y_pos;
2272 /* if the dock is full */
2273 if (!redocking && (dock->icon_count >= dock->max_icons)) {
2274 return False;
2277 /* exact position */
2278 if (req_y < dy)
2279 ex_y = (req_y - offset - dy) / ICON_SIZE;
2280 else
2281 ex_y = (req_y + offset - dy) / ICON_SIZE;
2283 if (req_x < dx)
2284 ex_x = (req_x - offset - dx) / ICON_SIZE;
2285 else
2286 ex_x = (req_x + offset - dx) / ICON_SIZE;
2288 /* check if the icon is outside the screen boundaries */
2290 WMRect rect;
2291 int flags;
2293 rect.pos.x = dx + ex_x * ICON_SIZE;
2294 rect.pos.y = dy + ex_y * ICON_SIZE;
2295 rect.size.width = rect.size.height = ICON_SIZE;
2297 wGetRectPlacementInfo(scr, rect, &flags);
2298 if (flags & (XFLAG_DEAD | XFLAG_PARTIAL))
2299 return False;
2302 if (dock->type == WM_DOCK) {
2303 if (icon->dock != dock && ex_x != 0)
2304 return False;
2306 aicon = NULL;
2307 for (i = 0; i < dock->max_icons; i++) {
2308 nicon = dock->icon_array[i];
2309 if (nicon && nicon->yindex == ex_y) {
2310 aicon = nicon;
2311 break;
2315 if (redocking) {
2316 int sig, done, closest;
2318 /* Possible cases when redocking:
2320 * icon dragged out of range of any slot -> false
2321 * icon dragged to range of free slot
2322 * icon dragged to range of same slot
2323 * icon dragged to range of different icon
2325 if (abs(ex_x) > DOCK_DETTACH_THRESHOLD)
2326 return False;
2328 if (ex_y >= 0 && ex_y <= max_y_icons && (aicon == icon || !aicon)) {
2329 *ret_x = 0;
2330 *ret_y = ex_y;
2331 return True;
2334 /* start looking at the upper slot or lower? */
2335 if (ex_y * ICON_SIZE < (req_y + offset - dy))
2336 sig = 1;
2337 else
2338 sig = -1;
2340 closest = -1;
2341 done = 0;
2342 /* look for closest free slot */
2343 for (i = 0; i < (DOCK_DETTACH_THRESHOLD + 1) * 2 && !done; i++) {
2344 int j;
2346 done = 1;
2347 closest = sig * (i / 2) + ex_y;
2348 /* check if this slot is used */
2349 if (closest >= 0) {
2350 for (j = 0; j < dock->max_icons; j++) {
2351 if (dock->icon_array[j]
2352 && dock->icon_array[j]->yindex == closest) {
2353 /* slot is used by someone else */
2354 if (dock->icon_array[j] != icon)
2355 done = 0;
2356 break;
2360 sig = -sig;
2362 if (done && closest >= 0 && closest <= max_y_icons &&
2363 ((ex_y >= closest && ex_y - closest < DOCK_DETTACH_THRESHOLD + 1)
2364 || (ex_y < closest && closest - ex_y <= DOCK_DETTACH_THRESHOLD + 1))) {
2365 *ret_x = 0;
2366 *ret_y = closest;
2367 return True;
2369 } else { /* !redocking */
2371 /* if slot is free and the icon is close enough, return it */
2372 if (!aicon && ex_x == 0 && ex_y >= 0 && ex_y <= max_y_icons) {
2373 *ret_x = 0;
2374 *ret_y = ex_y;
2375 return True;
2378 } else { /* CLIP */
2379 int neighbours = 0;
2380 int start, stop, k;
2382 start = icon->omnipresent ? 0 : scr->current_workspace;
2383 stop = icon->omnipresent ? scr->workspace_count : start + 1;
2385 aicon = NULL;
2386 for (k = start; k < stop; k++) {
2387 WDock *tmp = scr->workspaces[k]->clip;
2388 if (!tmp)
2389 continue;
2390 for (i = 0; i < tmp->max_icons; i++) {
2391 nicon = tmp->icon_array[i];
2392 if (nicon && nicon->xindex == ex_x && nicon->yindex == ex_y) {
2393 aicon = nicon;
2394 break;
2397 if (aicon)
2398 break;
2400 for (k = start; k < stop; k++) {
2401 WDock *tmp = scr->workspaces[k]->clip;
2402 if (!tmp)
2403 continue;
2404 for (i = 0; i < tmp->max_icons; i++) {
2405 nicon = tmp->icon_array[i];
2406 if (nicon && nicon != icon && /* Icon can't be it's own neighbour */
2407 (abs(nicon->xindex - ex_x) <= CLIP_ATTACH_VICINITY &&
2408 abs(nicon->yindex - ex_y) <= CLIP_ATTACH_VICINITY)) {
2409 neighbours = 1;
2410 break;
2413 if (neighbours)
2414 break;
2417 if (neighbours && (aicon == NULL || (redocking && aicon == icon))) {
2418 *ret_x = ex_x;
2419 *ret_y = ex_y;
2420 return True;
2423 return False;
2426 static int onScreen(WScreen * scr, int x, int y, int sx, int ex, int sy, int ey)
2428 WMRect rect = wmkrect(x, y, ICON_SIZE, ICON_SIZE);
2429 int flags;
2431 wGetRectPlacementInfo(scr, rect, &flags);
2433 return !(flags & (XFLAG_DEAD | XFLAG_PARTIAL));
2437 * returns true if it can find a free slot in the dock,
2438 * in which case it changes x_pos and y_pos accordingly.
2439 * Else returns false.
2441 Bool wDockFindFreeSlot(WDock * dock, int *x_pos, int *y_pos)
2443 WScreen *scr = dock->screen_ptr;
2444 WAppIcon *btn;
2445 WAppIconChain *chain;
2446 unsigned char *slot_map;
2447 int mwidth;
2448 int r;
2449 int x, y;
2450 int i, done = False;
2451 int corner;
2452 int sx = 0, sy = 0, ex = scr->scr_width, ey = scr->scr_height;
2453 int extra_count = 0;
2455 if (dock->type == WM_CLIP && dock != scr->workspaces[scr->current_workspace]->clip)
2456 extra_count = scr->global_icon_count;
2458 /* if the dock is full */
2459 if (dock->icon_count + extra_count >= dock->max_icons) {
2460 return False;
2463 if (!wPreferences.flags.nodock && scr->dock) {
2464 if (scr->dock->on_right_side)
2465 ex -= ICON_SIZE + DOCK_EXTRA_SPACE;
2466 else
2467 sx += ICON_SIZE + DOCK_EXTRA_SPACE;
2470 if (ex < dock->x_pos)
2471 ex = dock->x_pos;
2472 if (sx > dock->x_pos + ICON_SIZE)
2473 sx = dock->x_pos + ICON_SIZE;
2474 #define C_NONE 0
2475 #define C_NW 1
2476 #define C_NE 2
2477 #define C_SW 3
2478 #define C_SE 4
2480 /* check if clip is in a corner */
2481 if (dock->type == WM_CLIP) {
2482 if (dock->x_pos < 1 && dock->y_pos < 1)
2483 corner = C_NE;
2484 else if (dock->x_pos < 1 && dock->y_pos >= (ey - ICON_SIZE))
2485 corner = C_SE;
2486 else if (dock->x_pos >= (ex - ICON_SIZE) && dock->y_pos >= (ey - ICON_SIZE))
2487 corner = C_SW;
2488 else if (dock->x_pos >= (ex - ICON_SIZE) && dock->y_pos < 1)
2489 corner = C_NW;
2490 else
2491 corner = C_NONE;
2492 } else
2493 corner = C_NONE;
2495 /* If the clip is in the corner, use only slots that are in the border
2496 * of the screen */
2497 if (corner != C_NONE) {
2498 char *hmap, *vmap;
2499 int hcount, vcount;
2501 hcount = WMIN(dock->max_icons, scr->scr_width / ICON_SIZE);
2502 vcount = WMIN(dock->max_icons, scr->scr_height / ICON_SIZE);
2503 hmap = wmalloc(hcount + 1);
2504 memset(hmap, 0, hcount + 1);
2505 vmap = wmalloc(vcount + 1);
2506 memset(vmap, 0, vcount + 1);
2508 /* mark used positions */
2509 switch (corner) {
2510 case C_NE:
2511 for (i = 0; i < dock->max_icons; i++) {
2512 btn = dock->icon_array[i];
2513 if (!btn)
2514 continue;
2516 if (btn->xindex == 0 && btn->yindex > 0 && btn->yindex < vcount)
2517 vmap[btn->yindex] = 1;
2518 else if (btn->yindex == 0 && btn->xindex > 0 && btn->xindex < hcount)
2519 hmap[btn->xindex] = 1;
2521 for (chain = scr->global_icons; chain != NULL; chain = chain->next) {
2522 btn = chain->aicon;
2523 if (btn->xindex == 0 && btn->yindex > 0 && btn->yindex < vcount)
2524 vmap[btn->yindex] = 1;
2525 else if (btn->yindex == 0 && btn->xindex > 0 && btn->xindex < hcount)
2526 hmap[btn->xindex] = 1;
2528 break;
2529 case C_NW:
2530 for (i = 0; i < dock->max_icons; i++) {
2531 btn = dock->icon_array[i];
2532 if (!btn)
2533 continue;
2535 if (btn->xindex == 0 && btn->yindex > 0 && btn->yindex < vcount)
2536 vmap[btn->yindex] = 1;
2537 else if (btn->yindex == 0 && btn->xindex < 0 && btn->xindex > -hcount)
2538 hmap[-btn->xindex] = 1;
2540 for (chain = scr->global_icons; chain != NULL; chain = chain->next) {
2541 btn = chain->aicon;
2542 if (btn->xindex == 0 && btn->yindex > 0 && btn->yindex < vcount)
2543 vmap[btn->yindex] = 1;
2544 else if (btn->yindex == 0 && btn->xindex < 0 && btn->xindex > -hcount)
2545 hmap[-btn->xindex] = 1;
2547 break;
2548 case C_SE:
2549 for (i = 0; i < dock->max_icons; i++) {
2550 btn = dock->icon_array[i];
2551 if (!btn)
2552 continue;
2554 if (btn->xindex == 0 && btn->yindex < 0 && btn->yindex > -vcount)
2555 vmap[-btn->yindex] = 1;
2556 else if (btn->yindex == 0 && btn->xindex > 0 && btn->xindex < hcount)
2557 hmap[btn->xindex] = 1;
2559 for (chain = scr->global_icons; chain != NULL; chain = chain->next) {
2560 btn = chain->aicon;
2561 if (btn->xindex == 0 && btn->yindex < 0 && btn->yindex > -vcount)
2562 vmap[-btn->yindex] = 1;
2563 else if (btn->yindex == 0 && btn->xindex > 0 && btn->xindex < hcount)
2564 hmap[btn->xindex] = 1;
2566 break;
2567 case C_SW:
2568 default:
2569 for (i = 0; i < dock->max_icons; i++) {
2570 btn = dock->icon_array[i];
2571 if (!btn)
2572 continue;
2574 if (btn->xindex == 0 && btn->yindex < 0 && btn->yindex > -vcount)
2575 vmap[-btn->yindex] = 1;
2576 else if (btn->yindex == 0 && btn->xindex < 0 && btn->xindex > -hcount)
2577 hmap[-btn->xindex] = 1;
2579 for (chain = scr->global_icons; chain != NULL; chain = chain->next) {
2580 btn = chain->aicon;
2581 if (btn->xindex == 0 && btn->yindex < 0 && btn->yindex > -vcount)
2582 vmap[-btn->yindex] = 1;
2583 else if (btn->yindex == 0 && btn->xindex < 0 && btn->xindex > -hcount)
2584 hmap[-btn->xindex] = 1;
2587 x = 0;
2588 y = 0;
2589 done = 0;
2590 /* search a vacant slot */
2591 for (i = 1; i < WMAX(vcount, hcount); i++) {
2592 if (i < vcount && vmap[i] == 0) {
2593 /* found a slot */
2594 x = 0;
2595 y = i;
2596 done = 1;
2597 break;
2598 } else if (i < hcount && hmap[i] == 0) {
2599 /* found a slot */
2600 x = i;
2601 y = 0;
2602 done = 1;
2603 break;
2606 wfree(vmap);
2607 wfree(hmap);
2608 /* If found a slot, translate and return */
2609 if (done) {
2610 if (corner == C_NW || corner == C_NE) {
2611 *y_pos = y;
2612 } else {
2613 *y_pos = -y;
2615 if (corner == C_NE || corner == C_SE) {
2616 *x_pos = x;
2617 } else {
2618 *x_pos = -x;
2620 return True;
2622 /* else, try to find a slot somewhere else */
2625 /* a map of mwidth x mwidth would be enough if we allowed icons to be
2626 * placed outside of screen */
2627 mwidth = (int)ceil(sqrt(dock->max_icons));
2629 /* In the worst case (the clip is in the corner of the screen),
2630 * the amount of icons that fit in the clip is smaller.
2631 * Double the map to get a safe value.
2633 mwidth += mwidth;
2635 r = (mwidth - 1) / 2;
2637 slot_map = wmalloc(mwidth * mwidth);
2638 memset(slot_map, 0, mwidth * mwidth);
2640 #define XY2OFS(x,y) (WMAX(abs(x),abs(y)) > r) ? 0 : (((y)+r)*(mwidth)+(x)+r)
2642 /* mark used slots in the map. If the slot falls outside the map
2643 * (for example, when all icons are placed in line), ignore them. */
2644 for (i = 0; i < dock->max_icons; i++) {
2645 btn = dock->icon_array[i];
2646 if (btn)
2647 slot_map[XY2OFS(btn->xindex, btn->yindex)] = 1;
2649 for (chain = scr->global_icons; chain != NULL; chain = chain->next) {
2650 slot_map[XY2OFS(chain->aicon->xindex, chain->aicon->yindex)] = 1;
2652 /* Find closest slot from the center that is free by scanning the
2653 * map from the center to outward in circular passes.
2654 * This will not result in a neat layout, but will be optimal
2655 * in the sense that there will not be holes left.
2657 done = 0;
2658 for (i = 1; i <= r && !done; i++) {
2659 int tx, ty;
2661 /* top and bottom parts of the ring */
2662 for (x = -i; x <= i && !done; x++) {
2663 tx = dock->x_pos + x * ICON_SIZE;
2664 y = -i;
2665 ty = dock->y_pos + y * ICON_SIZE;
2666 if (slot_map[XY2OFS(x, y)] == 0 && onScreen(scr, tx, ty, sx, ex, sy, ey)) {
2667 *x_pos = x;
2668 *y_pos = y;
2669 done = 1;
2670 break;
2672 y = i;
2673 ty = dock->y_pos + y * ICON_SIZE;
2674 if (slot_map[XY2OFS(x, y)] == 0 && onScreen(scr, tx, ty, sx, ex, sy, ey)) {
2675 *x_pos = x;
2676 *y_pos = y;
2677 done = 1;
2678 break;
2681 /* left and right parts of the ring */
2682 for (y = -i + 1; y <= i - 1; y++) {
2683 ty = dock->y_pos + y * ICON_SIZE;
2684 x = -i;
2685 tx = dock->x_pos + x * ICON_SIZE;
2686 if (slot_map[XY2OFS(x, y)] == 0 && onScreen(scr, tx, ty, sx, ex, sy, ey)) {
2687 *x_pos = x;
2688 *y_pos = y;
2689 done = 1;
2690 break;
2692 x = i;
2693 tx = dock->x_pos + x * ICON_SIZE;
2694 if (slot_map[XY2OFS(x, y)] == 0 && onScreen(scr, tx, ty, sx, ex, sy, ey)) {
2695 *x_pos = x;
2696 *y_pos = y;
2697 done = 1;
2698 break;
2702 wfree(slot_map);
2703 #undef XY2OFS
2704 return done;
2707 static void moveDock(WDock * dock, int new_x, int new_y)
2709 WAppIcon *btn;
2710 int i;
2712 dock->x_pos = new_x;
2713 dock->y_pos = new_y;
2714 for (i = 0; i < dock->max_icons; i++) {
2715 btn = dock->icon_array[i];
2716 if (btn) {
2717 btn->x_pos = new_x + btn->xindex * ICON_SIZE;
2718 btn->y_pos = new_y + btn->yindex * ICON_SIZE;
2719 XMoveWindow(dpy, btn->icon->core->window, btn->x_pos, btn->y_pos);
2724 static void swapDock(WDock * dock)
2726 WScreen *scr = dock->screen_ptr;
2727 WAppIcon *btn;
2728 int x, i;
2730 if (dock->on_right_side) {
2731 x = dock->x_pos = scr->scr_width - ICON_SIZE - DOCK_EXTRA_SPACE;
2732 } else {
2733 x = dock->x_pos = DOCK_EXTRA_SPACE;
2736 for (i = 0; i < dock->max_icons; i++) {
2737 btn = dock->icon_array[i];
2738 if (btn) {
2739 btn->x_pos = x;
2740 XMoveWindow(dpy, btn->icon->core->window, btn->x_pos, btn->y_pos);
2744 wScreenUpdateUsableArea(scr);
2747 static pid_t execCommand(WAppIcon * btn, char *command, WSavedState * state)
2749 WScreen *scr = btn->icon->core->screen_ptr;
2750 pid_t pid;
2751 char **argv;
2752 int argc;
2753 char *cmdline;
2755 cmdline = ExpandOptions(scr, command);
2757 if (scr->flags.dnd_data_convertion_status || !cmdline) {
2758 if (cmdline)
2759 wfree(cmdline);
2760 if (state)
2761 wfree(state);
2762 return 0;
2765 wtokensplit(cmdline, &argv, &argc);
2767 if (!argc) {
2768 if (cmdline)
2769 wfree(cmdline);
2770 if (state)
2771 wfree(state);
2772 return 0;
2775 if ((pid = fork()) == 0) {
2776 char **args;
2777 int i;
2779 SetupEnvironment(scr);
2781 #ifdef HAVE_SETSID
2782 setsid();
2783 #endif
2785 args = malloc(sizeof(char *) * (argc + 1));
2786 if (!args)
2787 exit(111);
2788 for (i = 0; i < argc; i++) {
2789 args[i] = argv[i];
2791 args[argc] = NULL;
2792 execvp(argv[0], args);
2793 exit(111);
2795 wtokenfree(argv, argc);
2797 if (pid > 0) {
2798 if (!state) {
2799 state = wmalloc(sizeof(WSavedState));
2800 memset(state, 0, sizeof(WSavedState));
2801 state->hidden = -1;
2802 state->miniaturized = -1;
2803 state->shaded = -1;
2804 if (btn->dock == scr->dock || btn->omnipresent)
2805 state->workspace = -1;
2806 else
2807 state->workspace = scr->current_workspace;
2809 wWindowAddSavedState(btn->wm_instance, btn->wm_class, cmdline, pid, state);
2810 wAddDeathHandler(pid, (WDeathHandler *) trackDeadProcess, btn->dock);
2811 } else if (state) {
2812 wfree(state);
2814 wfree(cmdline);
2815 return pid;
2818 void wDockHideIcons(WDock * dock)
2820 int i;
2822 if (dock == NULL)
2823 return;
2825 for (i = 1; i < dock->max_icons; i++) {
2826 if (dock->icon_array[i])
2827 XUnmapWindow(dpy, dock->icon_array[i]->icon->core->window);
2829 dock->mapped = 0;
2831 dockIconPaint(dock->icon_array[0]);
2834 void wDockShowIcons(WDock * dock)
2836 int i, newlevel;
2837 WAppIcon *btn;
2839 if (dock == NULL)
2840 return;
2842 btn = dock->icon_array[0];
2843 moveDock(dock, btn->x_pos, btn->y_pos);
2845 newlevel = dock->lowered ? WMNormalLevel : WMDockLevel;
2846 ChangeStackingLevel(btn->icon->core, newlevel);
2848 for (i = 1; i < dock->max_icons; i++) {
2849 if (dock->icon_array[i]) {
2850 MoveInStackListAbove(dock->icon_array[i]->icon->core, btn->icon->core);
2851 break;
2855 if (!dock->collapsed) {
2856 for (i = 1; i < dock->max_icons; i++) {
2857 if (dock->icon_array[i]) {
2858 XMapWindow(dpy, dock->icon_array[i]->icon->core->window);
2862 dock->mapped = 1;
2864 dockIconPaint(btn);
2867 void wDockLower(WDock * dock)
2869 int i;
2871 for (i = 0; i < dock->max_icons; i++) {
2872 if (dock->icon_array[i])
2873 wLowerFrame(dock->icon_array[i]->icon->core);
2877 void wDockRaise(WDock * dock)
2879 int i;
2881 for (i = dock->max_icons - 1; i >= 0; i--) {
2882 if (dock->icon_array[i])
2883 wRaiseFrame(dock->icon_array[i]->icon->core);
2887 void wDockRaiseLower(WDock * dock)
2889 if (!dock->icon_array[0]->icon->core->stacking->above
2890 || (dock->icon_array[0]->icon->core->stacking->window_level
2891 != dock->icon_array[0]->icon->core->stacking->above->stacking->window_level))
2892 wDockLower(dock);
2893 else
2894 wDockRaise(dock);
2897 void wDockFinishLaunch(WDock * dock, WAppIcon * icon)
2899 icon->launching = 0;
2900 icon->relaunching = 0;
2901 dockIconPaint(icon);
2904 WAppIcon *wDockFindIconForWindow(WDock * dock, Window window)
2906 WAppIcon *icon;
2907 int i;
2909 for (i = 0; i < dock->max_icons; i++) {
2910 icon = dock->icon_array[i];
2911 if (icon && icon->main_window == window)
2912 return icon;
2914 return NULL;
2917 void wDockTrackWindowLaunch(WDock * dock, Window window)
2919 WAppIcon *icon;
2920 char *wm_class, *wm_instance;
2921 int i;
2922 Bool firstPass = True;
2923 Bool found = False;
2924 char *command = NULL;
2926 command = GetCommandForWindow(window);
2928 if (!PropGetWMClass(window, &wm_class, &wm_instance) || (!wm_class && !wm_instance)) {
2930 if (command)
2931 wfree(command);
2932 return;
2935 retry:
2936 for (i = 0; i < dock->max_icons; i++) {
2937 icon = dock->icon_array[i];
2938 if (!icon)
2939 continue;
2941 /* app is already attached to icon */
2942 if (icon->main_window == window) {
2943 found = True;
2944 break;
2947 if ((icon->wm_instance || icon->wm_class)
2948 && (icon->launching || !icon->running)) {
2950 if (icon->wm_instance && wm_instance && strcmp(icon->wm_instance, wm_instance) != 0) {
2951 continue;
2953 if (icon->wm_class && wm_class && strcmp(icon->wm_class, wm_class) != 0) {
2954 continue;
2956 if (firstPass && command && strcmp(icon->command, command) != 0) {
2957 continue;
2960 if (!icon->relaunching) {
2961 WApplication *wapp;
2963 /* Possibly an application that was docked with dockit,
2964 * but the user did not update WMState to indicate that
2965 * it was docked by force */
2966 wapp = wApplicationOf(window);
2967 if (!wapp) {
2968 icon->forced_dock = 1;
2969 icon->running = 0;
2971 if (!icon->forced_dock) {
2972 icon->main_window = window;
2975 found = True;
2976 if (!wPreferences.no_animations && !icon->launching &&
2977 !dock->screen_ptr->flags.startup && !dock->collapsed) {
2978 WAppIcon *aicon;
2979 int x0, y0;
2981 icon->launching = 1;
2982 dockIconPaint(icon);
2984 aicon = wAppIconCreateForDock(dock->screen_ptr, NULL,
2985 wm_instance, wm_class, TILE_NORMAL);
2986 /* XXX: can: aicon->icon == NULL ? */
2987 PlaceIcon(dock->screen_ptr, &x0, &y0, wGetHeadForWindow(aicon->icon->owner));
2988 wAppIconMove(aicon, x0, y0);
2989 /* Should this always be lowered? -Dan */
2990 if (dock->lowered)
2991 wLowerFrame(aicon->icon->core);
2992 XMapWindow(dpy, aicon->icon->core->window);
2993 aicon->launching = 1;
2994 wAppIconPaint(aicon);
2995 SlideWindow(aicon->icon->core->window, x0, y0, icon->x_pos, icon->y_pos);
2996 XUnmapWindow(dpy, aicon->icon->core->window);
2997 wAppIconDestroy(aicon);
2999 wDockFinishLaunch(dock, icon);
3000 break;
3004 if (firstPass && !found) {
3005 firstPass = False;
3006 goto retry;
3009 if (command)
3010 wfree(command);
3012 if (wm_class)
3013 XFree(wm_class);
3014 if (wm_instance)
3015 XFree(wm_instance);
3018 void wClipUpdateForWorkspaceChange(WScreen * scr, int workspace)
3020 if (!wPreferences.flags.noclip) {
3021 scr->clip_icon->dock = scr->workspaces[workspace]->clip;
3022 if (scr->current_workspace != workspace) {
3023 WDock *old_clip = scr->workspaces[scr->current_workspace]->clip;
3024 WAppIconChain *chain = scr->global_icons;
3026 while (chain) {
3027 moveIconBetweenDocks(chain->aicon->dock,
3028 scr->workspaces[workspace]->clip,
3029 chain->aicon, chain->aicon->xindex, chain->aicon->yindex);
3030 if (scr->workspaces[workspace]->clip->collapsed)
3031 XUnmapWindow(dpy, chain->aicon->icon->core->window);
3032 chain = chain->next;
3035 wDockHideIcons(old_clip);
3036 if (old_clip->auto_raise_lower) {
3037 if (old_clip->auto_raise_magic) {
3038 WMDeleteTimerHandler(old_clip->auto_raise_magic);
3039 old_clip->auto_raise_magic = NULL;
3041 wDockLower(old_clip);
3043 if (old_clip->auto_collapse) {
3044 if (old_clip->auto_expand_magic) {
3045 WMDeleteTimerHandler(old_clip->auto_expand_magic);
3046 old_clip->auto_expand_magic = NULL;
3048 old_clip->collapsed = 1;
3050 wDockShowIcons(scr->workspaces[workspace]->clip);
3052 if (scr->flags.clip_balloon_mapped)
3053 showClipBalloon(scr->clip_icon->dock, workspace);
3057 static void trackDeadProcess(pid_t pid, unsigned char status, WDock * dock)
3059 WAppIcon *icon;
3060 int i;
3062 for (i = 0; i < dock->max_icons; i++) {
3063 icon = dock->icon_array[i];
3064 if (!icon)
3065 continue;
3067 if (icon->launching && icon->pid == pid) {
3068 if (!icon->relaunching) {
3069 icon->running = 0;
3070 icon->main_window = None;
3072 wDockFinishLaunch(dock, icon);
3073 icon->pid = 0;
3074 if (status == 111) {
3075 char msg[PATH_MAX];
3076 char *cmd;
3078 #ifdef XDND
3079 if (icon->drop_launch)
3080 cmd = icon->dnd_command;
3081 else
3082 #endif
3083 if (icon->paste_launch)
3084 cmd = icon->paste_command;
3085 else
3086 cmd = icon->command;
3088 snprintf(msg, sizeof(msg), _("Could not execute command \"%s\""), cmd);
3090 wMessageDialog(dock->screen_ptr, _("Error"), msg, _("OK"), NULL, NULL);
3092 break;
3097 static void toggleLowered(WDock * dock)
3099 WAppIcon *tmp;
3100 int newlevel, i;
3102 /* lower/raise Dock */
3103 if (!dock->lowered) {
3104 newlevel = WMNormalLevel;
3105 dock->lowered = 1;
3106 } else {
3107 newlevel = WMDockLevel;
3108 dock->lowered = 0;
3111 for (i = 0; i < dock->max_icons; i++) {
3112 tmp = dock->icon_array[i];
3113 if (!tmp)
3114 continue;
3116 ChangeStackingLevel(tmp->icon->core, newlevel);
3117 if (dock->lowered)
3118 wLowerFrame(tmp->icon->core);
3121 if (dock->type == WM_DOCK)
3122 wScreenUpdateUsableArea(dock->screen_ptr);
3125 static void toggleCollapsed(WDock * dock)
3127 if (dock->collapsed) {
3128 dock->collapsed = 0;
3129 wDockShowIcons(dock);
3130 } else {
3131 dock->collapsed = 1;
3132 wDockHideIcons(dock);
3136 static void openDockMenu(WDock * dock, WAppIcon * aicon, XEvent * event)
3138 WScreen *scr = dock->screen_ptr;
3139 WObjDescriptor *desc;
3140 WMenuEntry *entry;
3141 WApplication *wapp = NULL;
3142 int index = 0;
3143 int x_pos;
3144 int n_selected;
3145 int appIsRunning = aicon->running && aicon->icon && aicon->icon->owner;
3147 if (dock->type == WM_DOCK) {
3148 /* keep on top */
3149 entry = dock->menu->entries[index];
3150 entry->flags.indicator_on = !dock->lowered;
3151 entry->clientdata = dock;
3152 dock->menu->flags.realized = 0;
3153 } else {
3154 /* clip options */
3155 if (scr->clip_options)
3156 updateClipOptionsMenu(scr->clip_options, dock);
3158 n_selected = numberOfSelectedIcons(dock);
3160 /* Rename Workspace */
3161 entry = dock->menu->entries[++index];
3162 if (aicon == scr->clip_icon) {
3163 entry->callback = renameCallback;
3164 entry->clientdata = dock;
3165 entry->flags.indicator = 0;
3166 entry->text = _("Rename Workspace");
3167 } else {
3168 entry->callback = omnipresentCallback;
3169 entry->clientdata = aicon;
3170 if (n_selected > 0) {
3171 entry->flags.indicator = 0;
3172 entry->text = _("Toggle Omnipresent");
3173 } else {
3174 entry->flags.indicator = 1;
3175 entry->flags.indicator_on = aicon->omnipresent;
3176 entry->flags.indicator_type = MI_CHECK;
3177 entry->text = _("Omnipresent");
3181 /* select/unselect icon */
3182 entry = dock->menu->entries[++index];
3183 entry->clientdata = aicon;
3184 entry->flags.indicator_on = aicon->icon->selected;
3185 wMenuSetEnabled(dock->menu, index, aicon != scr->clip_icon);
3187 /* select/unselect all icons */
3188 entry = dock->menu->entries[++index];
3189 entry->clientdata = aicon;
3190 if (n_selected > 0)
3191 entry->text = _("Unselect All Icons");
3192 else
3193 entry->text = _("Select All Icons");
3194 wMenuSetEnabled(dock->menu, index, dock->icon_count > 1);
3196 /* keep icon(s) */
3197 entry = dock->menu->entries[++index];
3198 entry->clientdata = aicon;
3199 if (n_selected > 1)
3200 entry->text = _("Keep Icons");
3201 else
3202 entry->text = _("Keep Icon");
3203 wMenuSetEnabled(dock->menu, index, dock->icon_count > 1);
3205 /* this is the workspace submenu part */
3206 entry = dock->menu->entries[++index];
3207 if (n_selected > 1)
3208 entry->text = _("Move Icons To");
3209 else
3210 entry->text = _("Move Icon To");
3211 if (scr->clip_submenu)
3212 updateWorkspaceMenu(scr->clip_submenu, aicon);
3213 wMenuSetEnabled(dock->menu, index, !aicon->omnipresent);
3215 /* remove icon(s) */
3216 entry = dock->menu->entries[++index];
3217 entry->clientdata = aicon;
3218 if (n_selected > 1)
3219 entry->text = _("Remove Icons");
3220 else
3221 entry->text = _("Remove Icon");
3222 wMenuSetEnabled(dock->menu, index, dock->icon_count > 1);
3224 /* attract icon(s) */
3225 entry = dock->menu->entries[++index];
3226 entry->clientdata = aicon;
3228 dock->menu->flags.realized = 0;
3229 wMenuRealize(dock->menu);
3232 if (aicon->icon->owner) {
3233 wapp = wApplicationOf(aicon->icon->owner->main_window);
3234 } else {
3235 wapp = NULL;
3238 /* launch */
3239 entry = dock->menu->entries[++index];
3240 entry->clientdata = aicon;
3241 wMenuSetEnabled(dock->menu, index, aicon->command != NULL);
3243 /* unhide here */
3244 entry = dock->menu->entries[++index];
3245 entry->clientdata = aicon;
3246 if (wapp && wapp->flags.hidden) {
3247 entry->text = _("Unhide Here");
3248 } else {
3249 entry->text = _("Bring Here");
3251 wMenuSetEnabled(dock->menu, index, appIsRunning);
3253 /* hide */
3254 entry = dock->menu->entries[++index];
3255 entry->clientdata = aicon;
3256 if (wapp && wapp->flags.hidden) {
3257 entry->text = _("Unhide");
3258 } else {
3259 entry->text = _("Hide");
3261 wMenuSetEnabled(dock->menu, index, appIsRunning);
3263 /* settings */
3264 entry = dock->menu->entries[++index];
3265 entry->clientdata = aicon;
3266 wMenuSetEnabled(dock->menu, index, !aicon->editing && !wPreferences.flags.noupdates);
3268 /* kill */
3269 entry = dock->menu->entries[++index];
3270 entry->clientdata = aicon;
3271 wMenuSetEnabled(dock->menu, index, appIsRunning);
3273 if (!dock->menu->flags.realized)
3274 wMenuRealize(dock->menu);
3276 if (dock->type == WM_CLIP) {
3277 /*x_pos = event->xbutton.x_root+2; */
3278 x_pos = event->xbutton.x_root - dock->menu->frame->core->width / 2 - 1;
3279 if (x_pos < 0) {
3280 x_pos = 0;
3281 } else if (x_pos + dock->menu->frame->core->width > scr->scr_width - 2) {
3282 x_pos = scr->scr_width - dock->menu->frame->core->width - 4;
3284 } else {
3285 x_pos = dock->on_right_side ? scr->scr_width - dock->menu->frame->core->width - 3 : 0;
3288 wMenuMapAt(dock->menu, x_pos, event->xbutton.y_root + 2, False);
3290 /* allow drag select */
3291 event->xany.send_event = True;
3292 desc = &dock->menu->menu->descriptor;
3293 (*desc->handle_mousedown) (desc, event);
3296 /******************************************************************/
3297 static void iconDblClick(WObjDescriptor * desc, XEvent * event)
3299 WAppIcon *btn = desc->parent;
3300 WDock *dock = btn->dock;
3301 WApplication *wapp = NULL;
3302 int unhideHere = 0;
3304 if (btn->icon->owner && !(event->xbutton.state & ControlMask)) {
3305 wapp = wApplicationOf(btn->icon->owner->main_window);
3307 assert(wapp != NULL);
3309 unhideHere = (event->xbutton.state & ShiftMask);
3311 /* go to the last workspace that the user worked on the app */
3312 if (wapp->last_workspace != dock->screen_ptr->current_workspace && !unhideHere) {
3313 wWorkspaceChange(dock->screen_ptr, wapp->last_workspace);
3316 wUnhideApplication(wapp, event->xbutton.button == Button2, unhideHere);
3318 if (event->xbutton.state & MOD_MASK) {
3319 wHideOtherApplications(btn->icon->owner);
3321 } else {
3322 if (event->xbutton.button == Button1) {
3324 if (event->xbutton.state & MOD_MASK) {
3325 /* raise/lower dock */
3326 toggleLowered(dock);
3327 } else if (btn == dock->screen_ptr->clip_icon) {
3328 if (getClipButton(event->xbutton.x, event->xbutton.y) == CLIP_IDLE)
3329 toggleCollapsed(dock);
3330 else
3331 handleClipChangeWorkspace(dock->screen_ptr, event);
3332 } else if (btn->command) {
3333 if (!btn->launching && (!btn->running || (event->xbutton.state & ControlMask))) {
3334 launchDockedApplication(btn, False);
3336 } else if (btn->xindex == 0 && btn->yindex == 0 && btn->dock->type == WM_DOCK) {
3337 wShowGNUstepPanel(dock->screen_ptr);
3343 static void handleDockMove(WDock * dock, WAppIcon * aicon, XEvent * event)
3345 WScreen *scr = dock->screen_ptr;
3346 int ofs_x = event->xbutton.x, ofs_y = event->xbutton.y;
3347 int x, y;
3348 XEvent ev;
3349 int grabbed = 0, swapped = 0, done;
3350 Pixmap ghost = None;
3351 int superfluous = wPreferences.superfluous; /* we catch it to avoid problems */
3353 if (XGrabPointer(dpy, aicon->icon->core->window, True, ButtonMotionMask
3354 | ButtonReleaseMask | ButtonPressMask, GrabModeAsync,
3355 GrabModeAsync, None, None, CurrentTime) != GrabSuccess) {
3356 wwarning("pointer grab failed for dock move");
3358 y = 0;
3359 for (x = 0; x < dock->max_icons; x++) {
3360 if (dock->icon_array[x] != NULL && dock->icon_array[x]->yindex > y)
3361 y = dock->icon_array[x]->yindex;
3363 y++;
3364 XResizeWindow(dpy, scr->dock_shadow, ICON_SIZE, ICON_SIZE * y);
3366 done = 0;
3367 while (!done) {
3368 WMMaskEvent(dpy, PointerMotionMask | ButtonReleaseMask | ButtonPressMask
3369 | ButtonMotionMask | ExposureMask | EnterWindowMask, &ev);
3370 switch (ev.type) {
3371 case Expose:
3372 WMHandleEvent(&ev);
3373 break;
3375 case EnterNotify:
3376 /* It means the cursor moved so fast that it entered
3377 * something else (if moving slowly, it would have
3378 * stayed in the dock that is being moved. Ignore such
3379 * "spurious" EnterNotifiy's */
3380 break;
3382 case MotionNotify:
3383 if (!grabbed) {
3384 if (abs(ofs_x - ev.xmotion.x) >= MOVE_THRESHOLD
3385 || abs(ofs_y - ev.xmotion.y) >= MOVE_THRESHOLD) {
3386 XChangeActivePointerGrab(dpy, ButtonMotionMask
3387 | ButtonReleaseMask | ButtonPressMask,
3388 wCursor[WCUR_MOVE], CurrentTime);
3389 grabbed = 1;
3391 break;
3393 if (dock->type == WM_CLIP) {
3394 x = ev.xmotion.x_root - ofs_x;
3395 y = ev.xmotion.y_root - ofs_y;
3396 wScreenKeepInside(scr, &x, &y, ICON_SIZE, ICON_SIZE);
3398 moveDock(dock, x, y);
3399 } else {
3400 /* move vertically if pointer is inside the dock */
3401 if ((dock->on_right_side && ev.xmotion.x_root >= dock->x_pos - ICON_SIZE)
3402 || (!dock->on_right_side && ev.xmotion.x_root <= dock->x_pos + ICON_SIZE * 2)) {
3404 x = ev.xmotion.x_root - ofs_x;
3405 y = ev.xmotion.y_root - ofs_y;
3406 wScreenKeepInside(scr, &x, &y, ICON_SIZE, ICON_SIZE);
3407 moveDock(dock, dock->x_pos, y);
3409 /* move horizontally to change sides */
3410 x = ev.xmotion.x_root - ofs_x;
3411 if (!dock->on_right_side) {
3413 /* is on left */
3415 if (ev.xmotion.x_root > dock->x_pos + ICON_SIZE * 2) {
3416 XMoveWindow(dpy, scr->dock_shadow, scr->scr_width - ICON_SIZE
3417 - DOCK_EXTRA_SPACE - 1, dock->y_pos);
3418 if (superfluous && ghost == None) {
3419 ghost = MakeGhostDock(dock, dock->x_pos,
3420 scr->scr_width - ICON_SIZE
3421 - DOCK_EXTRA_SPACE - 1, dock->y_pos);
3422 XSetWindowBackgroundPixmap(dpy, scr->dock_shadow, ghost);
3423 XClearWindow(dpy, scr->dock_shadow);
3425 XMapRaised(dpy, scr->dock_shadow);
3426 swapped = 1;
3427 } else {
3428 if (superfluous && ghost != None) {
3429 XFreePixmap(dpy, ghost);
3430 ghost = None;
3432 XUnmapWindow(dpy, scr->dock_shadow);
3433 swapped = 0;
3435 } else {
3436 /* is on right */
3437 if (ev.xmotion.x_root < dock->x_pos - ICON_SIZE) {
3438 XMoveWindow(dpy, scr->dock_shadow, DOCK_EXTRA_SPACE, dock->y_pos);
3439 if (superfluous && ghost == None) {
3440 ghost = MakeGhostDock(dock, dock->x_pos,
3441 DOCK_EXTRA_SPACE, dock->y_pos);
3442 XSetWindowBackgroundPixmap(dpy, scr->dock_shadow, ghost);
3443 XClearWindow(dpy, scr->dock_shadow);
3445 XMapRaised(dpy, scr->dock_shadow);
3446 swapped = -1;
3447 } else {
3448 XUnmapWindow(dpy, scr->dock_shadow);
3449 swapped = 0;
3450 if (superfluous && ghost != None) {
3451 XFreePixmap(dpy, ghost);
3452 ghost = None;
3457 break;
3459 case ButtonPress:
3460 break;
3462 case ButtonRelease:
3463 if (ev.xbutton.button != event->xbutton.button)
3464 break;
3465 XUngrabPointer(dpy, CurrentTime);
3466 XUnmapWindow(dpy, scr->dock_shadow);
3467 XResizeWindow(dpy, scr->dock_shadow, ICON_SIZE, ICON_SIZE);
3468 if (dock->type == WM_DOCK) {
3469 if (swapped != 0) {
3470 if (swapped > 0)
3471 dock->on_right_side = 1;
3472 else
3473 dock->on_right_side = 0;
3474 swapDock(dock);
3475 wArrangeIcons(scr, False);
3478 done = 1;
3479 break;
3482 if (superfluous) {
3483 if (ghost != None)
3484 XFreePixmap(dpy, ghost);
3485 XSetWindowBackground(dpy, scr->dock_shadow, scr->white_pixel);
3489 static Bool handleIconMove(WDock *dock, WAppIcon *aicon, XEvent *event)
3491 WScreen *scr = dock->screen_ptr;
3492 Window wins[2];
3493 WIcon *icon = aicon->icon;
3494 WDock *dock2 = NULL, *last_dock = dock, *clip = NULL;
3495 int ondock, grabbed = 0, change_dock = 0, collapsed = 0;
3496 XEvent ev;
3497 int x = aicon->x_pos, y = aicon->y_pos;
3498 int ofs_x = event->xbutton.x, ofs_y = event->xbutton.y;
3499 int shad_x = x, shad_y = y;
3500 int ix = aicon->xindex, iy = aicon->yindex;
3501 int tmp;
3502 Pixmap ghost = None;
3503 Bool docked;
3504 int superfluous = wPreferences.superfluous; /* we catch it to avoid problems */
3505 int omnipresent = aicon->omnipresent; /* this must be cached!!! */
3506 Bool hasMoved = False;
3508 if (wPreferences.flags.noupdates)
3509 return hasMoved;
3511 if (XGrabPointer(dpy, icon->core->window, True, ButtonMotionMask
3512 | ButtonReleaseMask | ButtonPressMask, GrabModeAsync,
3513 GrabModeAsync, None, None, CurrentTime) != GrabSuccess) {
3516 if (!(event->xbutton.state & MOD_MASK))
3517 wRaiseFrame(icon->core);
3519 if (!wPreferences.flags.noclip)
3520 clip = scr->workspaces[scr->current_workspace]->clip;
3522 if (dock == scr->dock && !wPreferences.flags.noclip)
3523 dock2 = clip;
3524 else if (dock != scr->dock && !wPreferences.flags.nodock)
3525 dock2 = scr->dock;
3527 wins[0] = icon->core->window;
3528 wins[1] = scr->dock_shadow;
3529 XRestackWindows(dpy, wins, 2);
3530 XMoveResizeWindow(dpy, scr->dock_shadow, aicon->x_pos, aicon->y_pos, ICON_SIZE, ICON_SIZE);
3531 if (superfluous) {
3532 if (icon->pixmap != None)
3533 ghost = MakeGhostIcon(scr, icon->pixmap);
3534 else
3535 ghost = MakeGhostIcon(scr, icon->core->window);
3537 XSetWindowBackgroundPixmap(dpy, scr->dock_shadow, ghost);
3538 XClearWindow(dpy, scr->dock_shadow);
3540 XMapWindow(dpy, scr->dock_shadow);
3542 ondock = 1;
3544 while (1) {
3545 XMaskEvent(dpy, PointerMotionMask | ButtonReleaseMask | ButtonPressMask
3546 | ButtonMotionMask | ExposureMask, &ev);
3547 switch (ev.type) {
3548 case Expose:
3549 WMHandleEvent(&ev);
3550 break;
3552 case MotionNotify:
3553 hasMoved = True;
3554 if (!grabbed) {
3555 if (abs(ofs_x - ev.xmotion.x) >= MOVE_THRESHOLD
3556 || abs(ofs_y - ev.xmotion.y) >= MOVE_THRESHOLD) {
3557 XChangeActivePointerGrab(dpy, ButtonMotionMask
3558 | ButtonReleaseMask | ButtonPressMask,
3559 wCursor[WCUR_MOVE], CurrentTime);
3560 grabbed = 1;
3561 } else {
3562 break;
3566 if (omnipresent) {
3567 int i;
3568 for (i = 0; i < scr->workspace_count; i++) {
3569 if (i == scr->current_workspace)
3570 continue;
3571 wDockShowIcons(scr->workspaces[i]->clip);
3575 x = ev.xmotion.x_root - ofs_x;
3576 y = ev.xmotion.y_root - ofs_y;
3577 tmp = wDockSnapIcon(dock, aicon, x, y, &ix, &iy, True);
3578 if (tmp && dock2) {
3579 change_dock = 0;
3580 if (last_dock != dock && collapsed) {
3581 last_dock->collapsed = 1;
3582 wDockHideIcons(last_dock);
3583 collapsed = 0;
3585 if (!collapsed && (collapsed = dock->collapsed)) {
3586 dock->collapsed = 0;
3587 wDockShowIcons(dock);
3589 if (dock->auto_raise_lower)
3590 wDockRaise(dock);
3591 last_dock = dock;
3592 } else if (dock2) {
3593 tmp = wDockSnapIcon(dock2, aicon, x, y, &ix, &iy, False);
3594 if (tmp) {
3595 change_dock = 1;
3596 if (last_dock != dock2 && collapsed) {
3597 last_dock->collapsed = 1;
3598 wDockHideIcons(last_dock);
3599 collapsed = 0;
3601 if (!collapsed && (collapsed = dock2->collapsed)) {
3602 dock2->collapsed = 0;
3603 wDockShowIcons(dock2);
3605 if (dock2->auto_raise_lower)
3606 wDockRaise(dock2);
3607 last_dock = dock2;
3610 if (aicon->launching || aicon->lock || (aicon->running && !(ev.xmotion.state & MOD_MASK))
3611 || (!aicon->running && tmp)) {
3612 shad_x = last_dock->x_pos + ix * wPreferences.icon_size;
3613 shad_y = last_dock->y_pos + iy * wPreferences.icon_size;
3615 XMoveWindow(dpy, scr->dock_shadow, shad_x, shad_y);
3617 if (!ondock) {
3618 XMapWindow(dpy, scr->dock_shadow);
3620 ondock = 1;
3621 } else {
3622 if (ondock) {
3623 XUnmapWindow(dpy, scr->dock_shadow);
3625 ondock = 0;
3627 XMoveWindow(dpy, icon->core->window, x, y);
3628 break;
3630 case ButtonPress:
3631 break;
3633 case ButtonRelease:
3634 if (ev.xbutton.button != event->xbutton.button)
3635 break;
3636 XUngrabPointer(dpy, CurrentTime);
3637 if (ondock) {
3638 SlideWindow(icon->core->window, x, y, shad_x, shad_y);
3639 XUnmapWindow(dpy, scr->dock_shadow);
3640 if (!change_dock) {
3641 reattachIcon(dock, aicon, ix, iy);
3642 if (clip && dock != clip && clip->auto_raise_lower)
3643 wDockLower(clip);
3644 } else {
3645 docked = moveIconBetweenDocks(dock, dock2, aicon, ix, iy);
3646 if (!docked) {
3647 /* Slide it back if dock rejected it */
3648 SlideWindow(icon->core->window, x, y, aicon->x_pos, aicon->y_pos);
3649 reattachIcon(dock, aicon, aicon->xindex, aicon->yindex);
3651 if (last_dock->type == WM_CLIP && last_dock->auto_collapse) {
3652 collapsed = 0;
3655 } else {
3656 aicon->x_pos = x;
3657 aicon->y_pos = y;
3658 if (superfluous) {
3659 if (!aicon->running && !wPreferences.no_animations) {
3660 /* We need to deselect it, even if is deselected in
3661 * wDockDetach(), because else DoKaboom() will fail.
3663 if (aicon->icon->selected)
3664 wIconSelect(aicon->icon);
3666 DoKaboom(scr, aicon->icon->core->window, x, y);
3669 if (clip && clip->auto_raise_lower)
3670 wDockLower(clip);
3671 wDockDetach(dock, aicon);
3673 if (collapsed) {
3674 last_dock->collapsed = 1;
3675 wDockHideIcons(last_dock);
3676 collapsed = 0;
3678 if (superfluous) {
3679 if (ghost != None)
3680 XFreePixmap(dpy, ghost);
3681 XSetWindowBackground(dpy, scr->dock_shadow, scr->white_pixel);
3683 if (omnipresent) {
3684 int i;
3685 for (i = 0; i < scr->workspace_count; i++) {
3686 if (i == scr->current_workspace)
3687 continue;
3688 wDockHideIcons(scr->workspaces[i]->clip);
3691 return hasMoved;;
3694 return False; /* never reached */
3697 static int getClipButton(int px, int py)
3699 int pt = (CLIP_BUTTON_SIZE + 2) * ICON_SIZE / 64;
3701 if (px < 0 || py < 0 || px >= ICON_SIZE || py >= ICON_SIZE)
3702 return CLIP_IDLE;
3704 if (py <= pt - ((int)ICON_SIZE - 1 - px))
3705 return CLIP_FORWARD;
3706 else if (px <= pt - ((int)ICON_SIZE - 1 - py))
3707 return CLIP_REWIND;
3709 return CLIP_IDLE;
3712 static void handleClipChangeWorkspace(WScreen * scr, XEvent * event)
3714 XEvent ev;
3715 int done, direction, new_ws;
3716 int new_dir;
3717 WDock *clip = scr->clip_icon->dock;
3719 direction = getClipButton(event->xbutton.x, event->xbutton.y);
3721 clip->lclip_button_pushed = direction == CLIP_REWIND;
3722 clip->rclip_button_pushed = direction == CLIP_FORWARD;
3724 wClipIconPaint(scr->clip_icon);
3725 done = 0;
3726 while (!done) {
3727 WMMaskEvent(dpy, ExposureMask | ButtonMotionMask | ButtonReleaseMask | ButtonPressMask, &ev);
3728 switch (ev.type) {
3729 case Expose:
3730 WMHandleEvent(&ev);
3731 break;
3733 case MotionNotify:
3734 new_dir = getClipButton(ev.xmotion.x, ev.xmotion.y);
3735 if (new_dir != direction) {
3736 direction = new_dir;
3737 clip->lclip_button_pushed = direction == CLIP_REWIND;
3738 clip->rclip_button_pushed = direction == CLIP_FORWARD;
3739 wClipIconPaint(scr->clip_icon);
3741 break;
3743 case ButtonPress:
3744 break;
3746 case ButtonRelease:
3747 if (ev.xbutton.button == event->xbutton.button)
3748 done = 1;
3752 clip->lclip_button_pushed = 0;
3753 clip->rclip_button_pushed = 0;
3755 new_ws = wPreferences.ws_advance || (event->xbutton.state & ControlMask);
3757 if (direction == CLIP_FORWARD) {
3758 if (scr->current_workspace < scr->workspace_count - 1)
3759 wWorkspaceChange(scr, scr->current_workspace + 1);
3760 else if (new_ws && scr->current_workspace < MAX_WORKSPACES - 1)
3761 wWorkspaceChange(scr, scr->current_workspace + 1);
3762 else if (wPreferences.ws_cycle)
3763 wWorkspaceChange(scr, 0);
3764 } else if (direction == CLIP_REWIND) {
3765 if (scr->current_workspace > 0)
3766 wWorkspaceChange(scr, scr->current_workspace - 1);
3767 else if (scr->current_workspace == 0 && wPreferences.ws_cycle)
3768 wWorkspaceChange(scr, scr->workspace_count - 1);
3771 wClipIconPaint(scr->clip_icon);
3774 static void iconMouseDown(WObjDescriptor * desc, XEvent * event)
3776 WAppIcon *aicon = desc->parent;
3777 WDock *dock = aicon->dock;
3778 WScreen *scr = aicon->icon->core->screen_ptr;
3780 if (aicon->editing || WCHECK_STATE(WSTATE_MODAL))
3781 return;
3783 scr->last_dock = dock;
3785 if (dock->menu->flags.mapped)
3786 wMenuUnmap(dock->menu);
3788 if (IsDoubleClick(scr, event)) {
3789 /* double-click was not in the main clip icon */
3790 if (dock->type != WM_CLIP || aicon->xindex != 0 || aicon->yindex != 0
3791 || getClipButton(event->xbutton.x, event->xbutton.y) == CLIP_IDLE) {
3792 iconDblClick(desc, event);
3793 return;
3797 if (dock->type == WM_CLIP && scr->flags.clip_balloon_mapped) {
3798 XUnmapWindow(dpy, scr->clip_balloon);
3799 scr->flags.clip_balloon_mapped = 0;
3801 if (event->xbutton.button == Button1) {
3802 if (event->xbutton.state & MOD_MASK)
3803 wDockLower(dock);
3804 else
3805 wDockRaise(dock);
3807 if ((event->xbutton.state & ShiftMask) && aicon != scr->clip_icon && dock->type != WM_DOCK) {
3808 wIconSelect(aicon->icon);
3809 return;
3812 if (aicon->yindex == 0 && aicon->xindex == 0) {
3813 if (getClipButton(event->xbutton.x, event->xbutton.y) != CLIP_IDLE
3814 && dock->type == WM_CLIP)
3815 handleClipChangeWorkspace(scr, event);
3816 else
3817 handleDockMove(dock, aicon, event);
3818 } else {
3819 Bool hasMoved = handleIconMove(dock, aicon, event);
3820 if (wPreferences.single_click && !hasMoved)
3821 iconDblClick(desc, event);
3823 } else if (event->xbutton.button == Button2 && dock->type == WM_CLIP && aicon == scr->clip_icon) {
3824 if (!scr->clip_ws_menu) {
3825 scr->clip_ws_menu = wWorkspaceMenuMake(scr, False);
3827 if (scr->clip_ws_menu) {
3828 WMenu *wsMenu = scr->clip_ws_menu;
3829 int xpos;
3831 wWorkspaceMenuUpdate(scr, wsMenu);
3833 xpos = event->xbutton.x_root - wsMenu->frame->core->width / 2 - 1;
3834 if (xpos < 0) {
3835 xpos = 0;
3836 } else if (xpos + wsMenu->frame->core->width > scr->scr_width - 2) {
3837 xpos = scr->scr_width - wsMenu->frame->core->width - 4;
3839 wMenuMapAt(wsMenu, xpos, event->xbutton.y_root + 2, False);
3841 desc = &wsMenu->menu->descriptor;
3842 event->xany.send_event = True;
3843 (*desc->handle_mousedown) (desc, event);
3845 } else if (event->xbutton.button == Button2 && dock->type == WM_CLIP &&
3846 (event->xbutton.state & ShiftMask) && aicon != scr->clip_icon) {
3847 wClipMakeIconOmnipresent(aicon, !aicon->omnipresent);
3848 } else if (event->xbutton.button == Button3) {
3849 if (event->xbutton.send_event &&
3850 XGrabPointer(dpy, aicon->icon->core->window, True, ButtonMotionMask
3851 | ButtonReleaseMask | ButtonPressMask, GrabModeAsync,
3852 GrabModeAsync, None, None, CurrentTime) != GrabSuccess) {
3853 wwarning("pointer grab failed for dockicon menu");
3854 return;
3857 openDockMenu(dock, aicon, event);
3858 } else if (event->xbutton.button == Button2) {
3859 WAppIcon *btn = desc->parent;
3861 if (!btn->launching && (!btn->running || (event->xbutton.state & ControlMask))) {
3862 launchDockedApplication(btn, True);
3867 static void showClipBalloon(WDock * dock, int workspace)
3869 int w, h;
3870 int x, y;
3871 WScreen *scr = dock->screen_ptr;
3872 char *text;
3873 Window stack[2];
3875 scr->flags.clip_balloon_mapped = 1;
3876 XMapWindow(dpy, scr->clip_balloon);
3878 text = scr->workspaces[workspace]->name;
3880 w = WMWidthOfString(scr->clip_title_font, text, strlen(text));
3882 h = WMFontHeight(scr->clip_title_font);
3883 XResizeWindow(dpy, scr->clip_balloon, w, h);
3885 x = dock->x_pos + CLIP_BUTTON_SIZE * ICON_SIZE / 64;
3886 y = dock->y_pos + ICON_SIZE - WMFontHeight(scr->clip_title_font) - 3;
3888 if (x + w > scr->scr_width) {
3889 x = scr->scr_width - w;
3890 if (dock->y_pos + ICON_SIZE + h > scr->scr_height)
3891 y = dock->y_pos - h - 1;
3892 else
3893 y = dock->y_pos + ICON_SIZE;
3894 XRaiseWindow(dpy, scr->clip_balloon);
3895 } else {
3896 stack[0] = scr->clip_icon->icon->core->window;
3897 stack[1] = scr->clip_balloon;
3898 XRestackWindows(dpy, stack, 2);
3900 XMoveWindow(dpy, scr->clip_balloon, x, y);
3901 XClearWindow(dpy, scr->clip_balloon);
3902 WMDrawString(scr->wmscreen, scr->clip_balloon,
3903 scr->clip_title_color[CLIP_NORMAL], scr->clip_title_font, 0, 0, text, strlen(text));
3906 static void clipEnterNotify(WObjDescriptor * desc, XEvent * event)
3908 WAppIcon *btn = (WAppIcon *) desc->parent;
3909 WDock *dock;
3910 WScreen *scr;
3912 assert(event->type == EnterNotify);
3914 if (desc->parent_type != WCLASS_DOCK_ICON)
3915 return;
3917 scr = btn->icon->core->screen_ptr;
3918 if (!btn->omnipresent)
3919 dock = btn->dock;
3920 else
3921 dock = scr->workspaces[scr->current_workspace]->clip;
3923 if (!dock || dock->type != WM_CLIP)
3924 return;
3926 /* The auto raise/lower code */
3927 if (dock->auto_lower_magic) {
3928 WMDeleteTimerHandler(dock->auto_lower_magic);
3929 dock->auto_lower_magic = NULL;
3931 if (dock->auto_raise_lower && !dock->auto_raise_magic) {
3932 dock->auto_raise_magic = WMAddTimerHandler(AUTO_RAISE_DELAY, clipAutoRaise, (void *)dock);
3935 /* The auto expand/collapse code */
3936 if (dock->auto_collapse_magic) {
3937 WMDeleteTimerHandler(dock->auto_collapse_magic);
3938 dock->auto_collapse_magic = NULL;
3940 if (dock->auto_collapse && !dock->auto_expand_magic) {
3941 dock->auto_expand_magic = WMAddTimerHandler(AUTO_EXPAND_DELAY, clipAutoExpand, (void *)dock);
3944 if (btn->xindex == 0 && btn->yindex == 0)
3945 showClipBalloon(dock, dock->screen_ptr->current_workspace);
3946 else {
3947 if (dock->screen_ptr->flags.clip_balloon_mapped) {
3948 XUnmapWindow(dpy, dock->screen_ptr->clip_balloon);
3949 dock->screen_ptr->flags.clip_balloon_mapped = 0;
3954 static void clipLeave(WDock * dock)
3956 XEvent event;
3957 WObjDescriptor *desc = NULL;
3959 if (!dock || dock->type != WM_CLIP)
3960 return;
3962 if (XCheckTypedEvent(dpy, EnterNotify, &event) != False) {
3963 if (XFindContext(dpy, event.xcrossing.window, wWinContext,
3964 (XPointer *) & desc) != XCNOENT
3965 && desc && desc->parent_type == WCLASS_DOCK_ICON
3966 && ((WAppIcon *) desc->parent)->dock && ((WAppIcon *) desc->parent)->dock->type == WM_CLIP) {
3967 /* We didn't left the Clip yet */
3968 XPutBackEvent(dpy, &event);
3969 return;
3972 XPutBackEvent(dpy, &event);
3973 } else {
3974 /* We entered a withdrawn window, so we're still in Clip */
3975 return;
3978 if (dock->auto_raise_magic) {
3979 WMDeleteTimerHandler(dock->auto_raise_magic);
3980 dock->auto_raise_magic = NULL;
3982 if (dock->auto_raise_lower && !dock->auto_lower_magic) {
3983 dock->auto_lower_magic = WMAddTimerHandler(AUTO_LOWER_DELAY, clipAutoLower, (void *)dock);
3986 if (dock->auto_expand_magic) {
3987 WMDeleteTimerHandler(dock->auto_expand_magic);
3988 dock->auto_expand_magic = NULL;
3990 if (dock->auto_collapse && !dock->auto_collapse_magic) {
3991 dock->auto_collapse_magic = WMAddTimerHandler(AUTO_COLLAPSE_DELAY, clipAutoCollapse, (void *)dock);
3995 static void clipLeaveNotify(WObjDescriptor * desc, XEvent * event)
3997 WAppIcon *btn = (WAppIcon *) desc->parent;
3999 assert(event->type == LeaveNotify);
4001 if (desc->parent_type != WCLASS_DOCK_ICON)
4002 return;
4004 clipLeave(btn->dock);
4007 static void clipAutoCollapse(void *cdata)
4009 WDock *dock = (WDock *) cdata;
4011 if (dock->type != WM_CLIP)
4012 return;
4014 if (dock->auto_collapse) {
4015 dock->collapsed = 1;
4016 wDockHideIcons(dock);
4018 dock->auto_collapse_magic = NULL;
4021 static void clipAutoExpand(void *cdata)
4023 WDock *dock = (WDock *) cdata;
4025 if (dock->type != WM_CLIP)
4026 return;
4028 if (dock->auto_collapse) {
4029 dock->collapsed = 0;
4030 wDockShowIcons(dock);
4032 dock->auto_expand_magic = NULL;
4035 static void clipAutoLower(void *cdata)
4037 WDock *dock = (WDock *) cdata;
4039 if (dock->type != WM_CLIP)
4040 return;
4042 if (dock->auto_raise_lower)
4043 wDockLower(dock);
4045 dock->auto_lower_magic = NULL;
4048 static void clipAutoRaise(void *cdata)
4050 WDock *dock = (WDock *) cdata;
4052 if (dock->type != WM_CLIP)
4053 return;
4055 if (dock->auto_raise_lower)
4056 wDockRaise(dock);
4058 if (dock->screen_ptr->flags.clip_balloon_mapped) {
4059 showClipBalloon(dock, dock->screen_ptr->current_workspace);
4062 dock->auto_raise_magic = NULL;
4065 static Bool iconCanBeOmnipresent(WAppIcon * aicon)
4067 WScreen *scr = aicon->icon->core->screen_ptr;
4068 WDock *clip;
4069 WAppIcon *btn;
4070 int i, j;
4072 for (i = 0; i < scr->workspace_count; i++) {
4073 clip = scr->workspaces[i]->clip;
4075 if (clip == aicon->dock)
4076 continue;
4078 if (clip->icon_count + scr->global_icon_count >= clip->max_icons)
4079 return False; /* Clip is full in some workspace */
4081 for (j = 0; j < clip->max_icons; j++) {
4082 btn = clip->icon_array[j];
4083 if (btn && btn->xindex == aicon->xindex && btn->yindex == aicon->yindex)
4084 return False;
4088 return True;
4091 int wClipMakeIconOmnipresent(WAppIcon * aicon, int omnipresent)
4093 WScreen *scr = aicon->icon->core->screen_ptr;
4094 WAppIconChain *new_entry, *tmp, *tmp1;
4095 int status = WO_SUCCESS;
4097 if ((scr->dock && aicon->dock == scr->dock) || aicon == scr->clip_icon) {
4098 return WO_NOT_APPLICABLE;
4101 if (aicon->omnipresent == omnipresent)
4102 return WO_SUCCESS;
4104 if (omnipresent) {
4105 if (iconCanBeOmnipresent(aicon)) {
4106 aicon->omnipresent = 1;
4107 new_entry = wmalloc(sizeof(WAppIconChain));
4108 new_entry->aicon = aicon;
4109 new_entry->next = scr->global_icons;
4110 scr->global_icons = new_entry;
4111 scr->global_icon_count++;
4112 } else {
4113 aicon->omnipresent = 0;
4114 status = WO_FAILED;
4116 } else {
4117 aicon->omnipresent = 0;
4118 if (aicon == scr->global_icons->aicon) {
4119 tmp = scr->global_icons->next;
4120 wfree(scr->global_icons);
4121 scr->global_icons = tmp;
4122 scr->global_icon_count--;
4123 } else {
4124 tmp = scr->global_icons;
4125 while (tmp->next) {
4126 if (tmp->next->aicon == aicon) {
4127 tmp1 = tmp->next->next;
4128 wfree(tmp->next);
4129 tmp->next = tmp1;
4130 scr->global_icon_count--;
4131 break;
4133 tmp = tmp->next;
4138 wAppIconPaint(aicon);
4140 return status;