wstrdup cannot fail, therefore no need to check for NULL.
[wmaker-crm.git] / src / dock.c
blob155e1a80dc051e931b60982a64663301091b98e5
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 <strings.h>
30 #include <unistd.h>
31 #include <math.h>
32 #include <limits.h>
34 #ifndef PATH_MAX
35 #define PATH_MAX DEFAULT_PATH_MAX
36 #endif
38 #include "WindowMaker.h"
39 #include "wcore.h"
40 #include "window.h"
41 #include "icon.h"
42 #include "appicon.h"
43 #include "actions.h"
44 #include "stacking.h"
45 #include "dock.h"
46 #include "dialog.h"
47 #include "funcs.h"
48 #include "properties.h"
49 #include "menu.h"
50 #include "client.h"
51 #include "defaults.h"
52 #include "workspace.h"
53 #include "framewin.h"
54 #include "superfluous.h"
55 #include "xinerama.h"
57 /**** Local variables ****/
58 #define CLIP_REWIND 1
59 #define CLIP_IDLE 0
60 #define CLIP_FORWARD 2
62 /**** Global variables ****/
64 /* in dockedapp.c */
65 extern void DestroyDockAppSettingsPanel();
66 extern void ShowDockAppSettingsPanel(WAppIcon * aicon);
67 extern Cursor wCursor[WCUR_LAST];
68 extern WPreferences wPreferences;
69 extern XContext wWinContext;
70 extern void appIconMouseDown(WObjDescriptor *desc, XEvent *event);
72 #define MOD_MASK wPreferences.modifier_mask
73 #define ICON_SIZE wPreferences.icon_size
75 /***** Local variables ****/
77 static WMPropList *dCommand = NULL;
78 static WMPropList *dPasteCommand = NULL;
79 #ifdef XDND /* XXX was OFFIX */
80 static WMPropList *dDropCommand = NULL;
81 #endif
82 static WMPropList *dAutoLaunch, *dLock;
83 static WMPropList *dName, *dForced, *dBuggyApplication, *dYes, *dNo;
84 static WMPropList *dHost, *dDock, *dClip;
85 static WMPropList *dAutoAttractIcons;
87 static WMPropList *dPosition, *dApplications, *dLowered, *dCollapsed;
89 static WMPropList *dAutoCollapse, *dAutoRaiseLower, *dOmnipresent;
91 static void dockIconPaint(WAppIcon *btn);
93 static void iconMouseDown(WObjDescriptor *desc, XEvent *event);
95 static pid_t execCommand(WAppIcon *btn, char *command, WSavedState *state);
97 static void trackDeadProcess(pid_t pid, unsigned char status, WDock *dock);
99 static int getClipButton(int px, int py);
101 static void toggleLowered(WDock *dock);
103 static void toggleCollapsed(WDock *dock);
105 static void clipIconExpose(WObjDescriptor *desc, XEvent *event);
107 static void clipLeave(WDock *dock);
109 static void handleClipChangeWorkspace(WScreen *scr, XEvent *event);
111 static Bool moveIconBetweenDocks(WDock *src, WDock *dest, WAppIcon *icon, int x, int y);
113 static void clipEnterNotify(WObjDescriptor *desc, XEvent *event);
114 static void clipLeaveNotify(WObjDescriptor *desc, XEvent *event);
115 static void clipAutoCollapse(void *cdata);
116 static void clipAutoExpand(void *cdata);
117 static void launchDockedApplication(WAppIcon *btn, Bool withSelection);
119 static void clipAutoLower(void *cdata);
120 static void clipAutoRaise(void *cdata);
121 static void reattachIcon(WDock *dock, WAppIcon *icon, int x, int y);
122 static WAppIcon *mainIconCreate(WScreen *scr, int type);
124 static void make_keys(void)
126 if (dCommand != NULL)
127 return;
129 dCommand = WMRetainPropList(WMCreatePLString("Command"));
130 dPasteCommand = WMRetainPropList(WMCreatePLString("PasteCommand"));
131 #ifdef XDND
132 dDropCommand = WMRetainPropList(WMCreatePLString("DropCommand"));
133 #endif
134 dLock = WMRetainPropList(WMCreatePLString("Lock"));
135 dAutoLaunch = WMRetainPropList(WMCreatePLString("AutoLaunch"));
136 dName = WMRetainPropList(WMCreatePLString("Name"));
137 dForced = WMRetainPropList(WMCreatePLString("Forced"));
138 dBuggyApplication = WMRetainPropList(WMCreatePLString("BuggyApplication"));
139 dYes = WMRetainPropList(WMCreatePLString("Yes"));
140 dNo = WMRetainPropList(WMCreatePLString("No"));
141 dHost = WMRetainPropList(WMCreatePLString("Host"));
143 dPosition = WMCreatePLString("Position");
144 dApplications = WMCreatePLString("Applications");
145 dLowered = WMCreatePLString("Lowered");
146 dCollapsed = WMCreatePLString("Collapsed");
147 dAutoCollapse = WMCreatePLString("AutoCollapse");
148 dAutoRaiseLower = WMCreatePLString("AutoRaiseLower");
149 dAutoAttractIcons = WMCreatePLString("AutoAttractIcons");
151 dOmnipresent = WMCreatePLString("Omnipresent");
153 dDock = WMCreatePLString("Dock");
154 dClip = WMCreatePLString("Clip");
157 static void renameCallback(WMenu *menu, WMenuEntry *entry)
159 WDock *dock = entry->clientdata;
160 char buffer[128];
161 int wspace;
162 char *name;
164 assert(entry->clientdata != NULL);
166 wspace = dock->screen_ptr->current_workspace;
168 name = wstrdup(dock->screen_ptr->workspaces[wspace]->name);
170 snprintf(buffer, sizeof(buffer), _("Type the name for workspace %i:"), wspace + 1);
171 if (wInputDialog(dock->screen_ptr, _("Rename Workspace"), buffer, &name)) {
172 wWorkspaceRename(dock->screen_ptr, wspace, name);
175 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;
308 color = scr->clip_title_color[CLIP_NORMAL];
310 XSetForeground(dpy, gc, WMColorPixel(color));
312 if (rpushed) {
313 p[0].x = tp + 1;
314 p[0].y = 1;
315 p[1].x = ICON_SIZE - 2;
316 p[1].y = 1;
317 p[2].x = ICON_SIZE - 2;
318 p[2].y = pt - 1;
319 } else if (lpushed) {
320 p[0].x = 1;
321 p[0].y = tp;
322 p[1].x = pt;
323 p[1].y = ICON_SIZE - 2;
324 p[2].x = 1;
325 p[2].y = ICON_SIZE - 2;
327 if (lpushed || rpushed) {
328 XSetForeground(dpy, scr->draw_gc, scr->white_pixel);
329 XFillPolygon(dpy, win, scr->draw_gc, p, 3, Convex, CoordModeOrigin);
330 XSetForeground(dpy, scr->draw_gc, scr->black_pixel);
333 /* top right arrow */
334 p[0].x = p[3].x = ICON_SIZE - 5 - as;
335 p[0].y = p[3].y = 5;
336 p[1].x = ICON_SIZE - 6;
337 p[1].y = 5;
338 p[2].x = ICON_SIZE - 6;
339 p[2].y = 4 + as;
340 if (rpushed) {
341 XFillPolygon(dpy, win, scr->draw_gc, p, 3, Convex, CoordModeOrigin);
342 XDrawLines(dpy, win, scr->draw_gc, p, 4, CoordModeOrigin);
343 } else {
344 XFillPolygon(dpy, win, gc, p, 3, Convex, CoordModeOrigin);
345 XDrawLines(dpy, win, gc, p, 4, CoordModeOrigin);
348 /* bottom left arrow */
349 p[0].x = p[3].x = 5;
350 p[0].y = p[3].y = ICON_SIZE - 5 - as;
351 p[1].x = 5;
352 p[1].y = ICON_SIZE - 6;
353 p[2].x = 4 + as;
354 p[2].y = ICON_SIZE - 6;
355 if (lpushed) {
356 XFillPolygon(dpy, win, scr->draw_gc, p, 3, Convex, CoordModeOrigin);
357 XDrawLines(dpy, win, scr->draw_gc, p, 4, CoordModeOrigin);
358 } else {
359 XFillPolygon(dpy, win, gc, p, 3, Convex, CoordModeOrigin);
360 XDrawLines(dpy, win, gc, p, 4, CoordModeOrigin);
364 RImage *wClipMakeTile(WScreen *scr, RImage *normalTile)
366 RImage *tile = RCloneImage(normalTile);
367 RColor black;
368 RColor dark;
369 RColor light;
370 int pt, tp;
371 int as;
373 pt = CLIP_BUTTON_SIZE * wPreferences.icon_size / 64;
374 tp = wPreferences.icon_size - 1 - pt;
375 as = pt - 15;
377 black.alpha = 255;
378 black.red = black.green = black.blue = 0;
380 dark.alpha = 0;
381 dark.red = dark.green = dark.blue = 60;
383 light.alpha = 0;
384 light.red = light.green = light.blue = 80;
386 /* top right */
387 ROperateLine(tile, RSubtractOperation, tp, 0, wPreferences.icon_size - 2, pt - 1, &dark);
388 RDrawLine(tile, tp - 1, 0, wPreferences.icon_size - 1, pt + 1, &black);
389 ROperateLine(tile, RAddOperation, tp, 2, wPreferences.icon_size - 3, pt, &light);
391 /* arrow bevel */
392 ROperateLine(tile, RSubtractOperation, ICON_SIZE - 7 - as, 4, ICON_SIZE - 5, 4, &dark);
393 ROperateLine(tile, RSubtractOperation, ICON_SIZE - 6 - as, 5, ICON_SIZE - 5, 6 + as, &dark);
394 ROperateLine(tile, RAddOperation, ICON_SIZE - 5, 4, ICON_SIZE - 5, 6 + as, &light);
396 /* bottom left */
397 ROperateLine(tile, RAddOperation, 2, tp + 2, pt - 2, wPreferences.icon_size - 3, &dark);
398 RDrawLine(tile, 0, tp - 1, pt + 1, wPreferences.icon_size - 1, &black);
399 ROperateLine(tile, RSubtractOperation, 0, tp - 2, pt + 1, wPreferences.icon_size - 2, &light);
401 /* arrow bevel */
402 ROperateLine(tile, RSubtractOperation, 4, ICON_SIZE - 7 - as, 4, ICON_SIZE - 5, &dark);
403 ROperateLine(tile, RSubtractOperation, 5, ICON_SIZE - 6 - as, 6 + as, ICON_SIZE - 5, &dark);
404 ROperateLine(tile, RAddOperation, 4, ICON_SIZE - 5, 6 + as, ICON_SIZE - 5, &light);
406 return tile;
409 static void omnipresentCallback(WMenu *menu, WMenuEntry *entry)
411 WAppIcon *clickedIcon = entry->clientdata;
412 WAppIcon *aicon;
413 WDock *dock;
414 WMArray *selectedIcons;
415 WMArrayIterator iter;
416 int failed;
418 assert(entry->clientdata != NULL);
420 dock = clickedIcon->dock;
422 selectedIcons = getSelected(dock);
424 if (!WMGetArrayItemCount(selectedIcons))
425 WMAddToArray(selectedIcons, clickedIcon);
427 failed = 0;
428 WM_ITERATE_ARRAY(selectedIcons, aicon, iter) {
429 if (wClipMakeIconOmnipresent(aicon, !aicon->omnipresent) == WO_FAILED)
430 failed++;
431 else if (aicon->icon->selected)
432 wIconSelect(aicon->icon);
434 WMFreeArray(selectedIcons);
436 if (failed > 1) {
437 wMessageDialog(dock->screen_ptr, _("Warning"),
438 _("Some icons cannot be made omnipresent. "
439 "Please make sure that no other icon is "
440 "docked in the same positions on the other "
441 "workspaces and the Clip is not full in "
442 "some workspace."), _("OK"), NULL, NULL);
443 } else if (failed == 1) {
444 wMessageDialog(dock->screen_ptr, _("Warning"),
445 _("Icon cannot be made omnipresent. "
446 "Please make sure that no other icon is "
447 "docked in the same position on the other "
448 "workspaces and the Clip is not full in "
449 "some workspace."), _("OK"), NULL, NULL);
453 static void removeIconsCallback(WMenu *menu, WMenuEntry *entry)
455 WAppIcon *clickedIcon = (WAppIcon *) entry->clientdata;
456 WDock *dock;
457 WAppIcon *aicon;
458 WMArray *selectedIcons;
459 int keepit;
460 WMArrayIterator it;
462 assert(clickedIcon != NULL);
464 dock = clickedIcon->dock;
466 selectedIcons = getSelected(dock);
468 if (WMGetArrayItemCount(selectedIcons)) {
469 if (wMessageDialog(dock->screen_ptr, _("Workspace Clip"),
470 _("All selected icons will be removed!"),
471 _("OK"), _("Cancel"), NULL) != WAPRDefault) {
472 WMFreeArray(selectedIcons);
473 return;
475 } else {
476 if (clickedIcon->xindex == 0 && clickedIcon->yindex == 0) {
477 WMFreeArray(selectedIcons);
478 return;
480 WMAddToArray(selectedIcons, clickedIcon);
483 WM_ITERATE_ARRAY(selectedIcons, aicon, it) {
484 keepit = aicon->running && wApplicationOf(aicon->main_window);
485 wDockDetach(dock, aicon);
486 if (keepit) {
487 /* XXX: can: aicon->icon == NULL ? */
488 PlaceIcon(dock->screen_ptr, &aicon->x_pos, &aicon->y_pos,
489 wGetHeadForWindow(aicon->icon->owner));
490 XMoveWindow(dpy, aicon->icon->core->window, aicon->x_pos, aicon->y_pos);
491 if (!dock->mapped || dock->collapsed)
492 XMapWindow(dpy, aicon->icon->core->window);
495 WMFreeArray(selectedIcons);
497 if (wPreferences.auto_arrange_icons)
498 wArrangeIcons(dock->screen_ptr, True);
501 static void keepIconsCallback(WMenu *menu, WMenuEntry *entry)
503 WAppIcon *clickedIcon = (WAppIcon *) entry->clientdata;
504 WDock *dock;
505 WAppIcon *aicon;
506 WMArray *selectedIcons;
507 WMArrayIterator it;
509 assert(clickedIcon != NULL);
510 dock = clickedIcon->dock;
512 selectedIcons = getSelected(dock);
514 if (!WMGetArrayItemCount(selectedIcons)
515 && clickedIcon != dock->screen_ptr->clip_icon) {
516 char *command = NULL;
518 if (!clickedIcon->command && !clickedIcon->editing) {
519 clickedIcon->editing = 1;
520 if (wInputDialog(dock->screen_ptr, _("Keep Icon"),
521 _("Type the command used to launch the application"), &command)) {
522 if (command && (command[0] == 0 || (command[0] == '-' && command[1] == 0))) {
523 wfree(command);
524 command = NULL;
526 clickedIcon->command = command;
527 clickedIcon->editing = 0;
528 } else {
529 clickedIcon->editing = 0;
530 if (command)
531 wfree(command);
532 WMFreeArray(selectedIcons);
533 return;
537 WMAddToArray(selectedIcons, clickedIcon);
540 WM_ITERATE_ARRAY(selectedIcons, aicon, it) {
541 if (aicon->icon->selected)
542 wIconSelect(aicon->icon);
543 if (aicon && aicon->attracted && aicon->command) {
544 aicon->attracted = 0;
545 if (aicon->icon->shadowed) {
546 aicon->icon->shadowed = 0;
547 aicon->icon->force_paint = 1;
548 wAppIconPaint(aicon);
551 wAppIconSave(aicon);
553 WMFreeArray(selectedIcons);
556 static void toggleAutoAttractCallback(WMenu *menu, WMenuEntry *entry)
558 WDock *dock = (WDock *) entry->clientdata;
560 assert(entry->clientdata != NULL);
562 dock->attract_icons = !dock->attract_icons;
563 /*if (!dock->attract_icons)
564 dock->keep_attracted = 0; */
566 entry->flags.indicator_on = dock->attract_icons;
568 wMenuPaint(menu);
571 static void selectCallback(WMenu *menu, WMenuEntry *entry)
573 WAppIcon *icon = (WAppIcon *) entry->clientdata;
575 assert(icon != NULL);
577 wIconSelect(icon->icon);
579 wMenuPaint(menu);
582 static void colectIconsCallback(WMenu *menu, WMenuEntry *entry)
584 WAppIcon *clickedIcon = (WAppIcon *) entry->clientdata;
585 WDock *clip;
586 WAppIcon *aicon;
587 int x, y, x_pos, y_pos;
589 assert(entry->clientdata != NULL);
590 clip = clickedIcon->dock;
592 aicon = clip->screen_ptr->app_icon_list;
594 while (aicon) {
595 if (!aicon->docked && wDockFindFreeSlot(clip, &x, &y)) {
596 x_pos = clip->x_pos + x * ICON_SIZE;
597 y_pos = clip->y_pos + y * ICON_SIZE;
598 if (aicon->x_pos != x_pos || aicon->y_pos != y_pos) {
599 #ifdef ANIMATIONS
600 if (wPreferences.no_animations) {
601 XMoveWindow(dpy, aicon->icon->core->window, x_pos, y_pos);
602 } else {
603 SlideWindow(aicon->icon->core->window,
604 aicon->x_pos, aicon->y_pos, x_pos, y_pos);
606 #else
607 XMoveWindow(dpy, aicon->icon->core->window, x_pos, y_pos);
608 #endif /* ANIMATIONS */
610 aicon->attracted = 1;
611 if (!aicon->icon->shadowed) {
612 aicon->icon->shadowed = 1;
613 aicon->icon->force_paint = 1;
614 /* We don't do an wAppIconPaint() here because it's in
615 * wDockAttachIcon(). -Dan
618 wDockAttachIcon(clip, aicon, x, y);
619 if (clip->collapsed || !clip->mapped)
620 XUnmapWindow(dpy, aicon->icon->core->window);
622 aicon = aicon->next;
626 static void selectIconsCallback(WMenu *menu, WMenuEntry *entry)
628 WAppIcon *clickedIcon = (WAppIcon *) entry->clientdata;
629 WDock *dock;
630 WMArray *selectedIcons;
631 WMArrayIterator iter;
632 WAppIcon *btn;
633 int i;
635 assert(clickedIcon != NULL);
636 dock = clickedIcon->dock;
638 selectedIcons = getSelected(dock);
640 if (!WMGetArrayItemCount(selectedIcons)) {
641 for (i = 1; i < dock->max_icons; i++) {
642 btn = dock->icon_array[i];
643 if (btn && !btn->icon->selected) {
644 wIconSelect(btn->icon);
647 } else {
648 WM_ITERATE_ARRAY(selectedIcons, btn, iter) {
649 wIconSelect(btn->icon);
652 WMFreeArray(selectedIcons);
654 wMenuPaint(menu);
657 static void toggleCollapsedCallback(WMenu *menu, WMenuEntry *entry)
659 assert(entry->clientdata != NULL);
661 toggleCollapsed(entry->clientdata);
663 entry->flags.indicator_on = ((WDock *) entry->clientdata)->collapsed;
665 wMenuPaint(menu);
668 static void toggleAutoCollapseCallback(WMenu *menu, WMenuEntry *entry)
670 WDock *dock;
671 assert(entry->clientdata != NULL);
673 dock = (WDock *) entry->clientdata;
675 dock->auto_collapse = !dock->auto_collapse;
677 entry->flags.indicator_on = ((WDock *) entry->clientdata)->auto_collapse;
679 wMenuPaint(menu);
682 static void toggleAutoRaiseLowerCallback(WMenu *menu, WMenuEntry *entry)
684 WDock *dock;
685 assert(entry->clientdata != NULL);
687 dock = (WDock *) entry->clientdata;
689 dock->auto_raise_lower = !dock->auto_raise_lower;
691 entry->flags.indicator_on = ((WDock *) entry->clientdata)->auto_raise_lower;
693 wMenuPaint(menu);
696 static void launchCallback(WMenu *menu, WMenuEntry *entry)
698 WAppIcon *btn = (WAppIcon *) entry->clientdata;
700 launchDockedApplication(btn, False);
703 static void settingsCallback(WMenu *menu, WMenuEntry *entry)
705 WAppIcon *btn = (WAppIcon *) entry->clientdata;
707 if (btn->editing)
708 return;
709 ShowDockAppSettingsPanel(btn);
712 static void hideCallback(WMenu *menu, WMenuEntry *entry)
714 WApplication *wapp;
715 WAppIcon *btn = (WAppIcon *) entry->clientdata;
717 wapp = wApplicationOf(btn->icon->owner->main_window);
719 if (wapp->flags.hidden) {
720 wWorkspaceChange(btn->icon->core->screen_ptr, wapp->last_workspace);
721 wUnhideApplication(wapp, False, False);
722 } else {
723 wHideApplication(wapp);
727 static void unhideHereCallback(WMenu *menu, WMenuEntry *entry)
729 WApplication *wapp;
730 WAppIcon *btn = (WAppIcon *) entry->clientdata;
732 wapp = wApplicationOf(btn->icon->owner->main_window);
734 wUnhideApplication(wapp, False, True);
737 static WAppIcon *mainIconCreate(WScreen *scr, int type)
739 WAppIcon *btn;
740 int x_pos;
742 if (type == WM_CLIP) {
743 if (scr->clip_icon)
744 return scr->clip_icon;
745 btn = wAppIconCreateForDock(scr, NULL, "Logo", "WMClip", TILE_CLIP);
746 btn->icon->core->descriptor.handle_expose = clipIconExpose;
747 btn->icon->core->descriptor.handle_enternotify = clipEnterNotify;
748 btn->icon->core->descriptor.handle_leavenotify = clipLeaveNotify;
749 /*x_pos = scr->scr_width - ICON_SIZE*2 - DOCK_EXTRA_SPACE; */
750 x_pos = 0;
751 } else {
752 btn = wAppIconCreateForDock(scr, NULL, "Logo", "WMDock", TILE_NORMAL);
753 x_pos = scr->scr_width - ICON_SIZE - DOCK_EXTRA_SPACE;
756 btn->xindex = 0;
757 btn->yindex = 0;
759 btn->icon->core->descriptor.handle_mousedown = iconMouseDown;
760 btn->icon->core->descriptor.parent_type = WCLASS_DOCK_ICON;
761 btn->icon->core->descriptor.parent = btn;
762 /*ChangeStackingLevel(btn->icon->core, WMDockLevel); */
763 XMapWindow(dpy, btn->icon->core->window);
764 btn->x_pos = x_pos;
765 btn->y_pos = 0;
766 btn->docked = 1;
767 if (type == WM_CLIP)
768 scr->clip_icon = btn;
770 return btn;
773 static void switchWSCommand(WMenu *menu, WMenuEntry *entry)
775 WAppIcon *btn, *icon = (WAppIcon *) entry->clientdata;
776 WScreen *scr = icon->icon->core->screen_ptr;
777 WDock *src, *dest;
778 WMArray *selectedIcons;
779 int x, y;
781 if (entry->order == scr->current_workspace)
782 return;
783 src = icon->dock;
784 dest = scr->workspaces[entry->order]->clip;
786 selectedIcons = getSelected(src);
788 if (WMGetArrayItemCount(selectedIcons)) {
789 WMArrayIterator iter;
791 WM_ITERATE_ARRAY(selectedIcons, btn, iter) {
792 if (wDockFindFreeSlot(dest, &x, &y)) {
793 moveIconBetweenDocks(src, dest, btn, x, y);
794 XUnmapWindow(dpy, btn->icon->core->window);
797 } else if (icon != scr->clip_icon) {
798 if (wDockFindFreeSlot(dest, &x, &y)) {
799 moveIconBetweenDocks(src, dest, icon, x, y);
800 XUnmapWindow(dpy, icon->icon->core->window);
803 WMFreeArray(selectedIcons);
806 static void launchDockedApplication(WAppIcon *btn, Bool withSelection)
808 WScreen *scr = btn->icon->core->screen_ptr;
810 if (!btn->launching &&
811 ((!withSelection && btn->command != NULL) || (withSelection && btn->paste_command != NULL))) {
812 if (!btn->forced_dock) {
813 btn->relaunching = btn->running;
814 btn->running = 1;
816 if (btn->wm_instance || btn->wm_class) {
817 WWindowAttributes attr;
818 memset(&attr, 0, sizeof(WWindowAttributes));
819 wDefaultFillAttributes(scr, btn->wm_instance, btn->wm_class, &attr, NULL, True);
821 if (!attr.no_appicon && !btn->buggy_app)
822 btn->launching = 1;
823 else
824 btn->running = 0;
826 btn->drop_launch = 0;
827 btn->paste_launch = withSelection;
828 scr->last_dock = btn->dock;
829 btn->pid = execCommand(btn, (withSelection ? btn->paste_command : btn->command), NULL);
830 if (btn->pid > 0) {
831 if (btn->buggy_app) {
832 /* give feedback that the app was launched */
833 btn->launching = 1;
834 dockIconPaint(btn);
835 btn->launching = 0;
836 WMAddTimerHandler(200, (WMCallback *) dockIconPaint, btn);
837 } else {
838 dockIconPaint(btn);
840 } else {
841 wwarning(_("could not launch application %s"), btn->command);
842 btn->launching = 0;
843 if (!btn->relaunching) {
844 btn->running = 0;
850 static void updateWorkspaceMenu(WMenu *menu, WAppIcon *icon)
852 WScreen *scr = menu->frame->screen_ptr;
853 char title[MAX_WORKSPACENAME_WIDTH + 1];
854 int i;
856 if (!menu || !icon)
857 return;
859 for (i = 0; i < scr->workspace_count; i++) {
860 if (i < menu->entry_no) {
861 if (strcmp(menu->entries[i]->text, scr->workspaces[i]->name) != 0) {
862 wfree(menu->entries[i]->text);
863 strcpy(title, scr->workspaces[i]->name);
864 menu->entries[i]->text = wstrdup(title);
865 menu->flags.realized = 0;
867 menu->entries[i]->clientdata = (void *)icon;
868 } else {
869 strcpy(title, scr->workspaces[i]->name);
871 wMenuAddCallback(menu, title, switchWSCommand, (void *)icon);
873 menu->flags.realized = 0;
875 if (i == scr->current_workspace) {
876 wMenuSetEnabled(menu, i, False);
877 } else {
878 wMenuSetEnabled(menu, i, True);
882 if (!menu->flags.realized)
883 wMenuRealize(menu);
886 static WMenu *makeWorkspaceMenu(WScreen *scr)
888 WMenu *menu;
890 menu = wMenuCreate(scr, NULL, False);
891 if (!menu)
892 wwarning(_("could not create workspace submenu for Clip menu"));
894 wMenuAddCallback(menu, "", switchWSCommand, (void *)scr->clip_icon);
896 menu->flags.realized = 0;
897 wMenuRealize(menu);
899 return menu;
902 static void updateClipOptionsMenu(WMenu *menu, WDock *dock)
904 WMenuEntry *entry;
905 int index = 0;
907 if (!menu || !dock)
908 return;
910 /* keep on top */
911 entry = menu->entries[index];
912 entry->flags.indicator_on = !dock->lowered;
913 entry->clientdata = dock;
915 /* collapsed */
916 entry = menu->entries[++index];
917 entry->flags.indicator_on = dock->collapsed;
918 entry->clientdata = dock;
920 /* auto-collapse */
921 entry = menu->entries[++index];
922 entry->flags.indicator_on = dock->auto_collapse;
923 entry->clientdata = dock;
925 /* auto-raise/lower */
926 entry = menu->entries[++index];
927 entry->flags.indicator_on = dock->auto_raise_lower;
928 entry->clientdata = dock;
929 wMenuSetEnabled(menu, index, dock->lowered);
931 /* attract icons */
932 entry = menu->entries[++index];
933 entry->flags.indicator_on = dock->attract_icons;
934 entry->clientdata = dock;
936 menu->flags.realized = 0;
937 wMenuRealize(menu);
940 static WMenu *makeClipOptionsMenu(WScreen *scr)
942 WMenu *menu;
943 WMenuEntry *entry;
945 menu = wMenuCreate(scr, NULL, False);
946 if (!menu) {
947 wwarning(_("could not create options submenu for Clip menu"));
948 return NULL;
951 entry = wMenuAddCallback(menu, _("Keep on Top"), toggleLoweredCallback, NULL);
952 entry->flags.indicator = 1;
953 entry->flags.indicator_on = 1;
954 entry->flags.indicator_type = MI_CHECK;
956 entry = wMenuAddCallback(menu, _("Collapsed"), toggleCollapsedCallback, NULL);
957 entry->flags.indicator = 1;
958 entry->flags.indicator_on = 1;
959 entry->flags.indicator_type = MI_CHECK;
961 entry = wMenuAddCallback(menu, _("Autocollapse"), toggleAutoCollapseCallback, NULL);
962 entry->flags.indicator = 1;
963 entry->flags.indicator_on = 1;
964 entry->flags.indicator_type = MI_CHECK;
966 entry = wMenuAddCallback(menu, _("Autoraise"), toggleAutoRaiseLowerCallback, NULL);
967 entry->flags.indicator = 1;
968 entry->flags.indicator_on = 1;
969 entry->flags.indicator_type = MI_CHECK;
971 entry = wMenuAddCallback(menu, _("Autoattract Icons"), toggleAutoAttractCallback, NULL);
972 entry->flags.indicator = 1;
973 entry->flags.indicator_on = 1;
974 entry->flags.indicator_type = MI_CHECK;
976 menu->flags.realized = 0;
977 wMenuRealize(menu);
979 return menu;
982 static WMenu *dockMenuCreate(WScreen *scr, int type)
984 WMenu *menu;
985 WMenuEntry *entry;
987 if (type == WM_CLIP && scr->clip_menu)
988 return scr->clip_menu;
990 menu = wMenuCreate(scr, NULL, False);
991 if (type != WM_CLIP) {
992 entry = wMenuAddCallback(menu, _("Keep on Top"), toggleLoweredCallback, NULL);
993 entry->flags.indicator = 1;
994 entry->flags.indicator_on = 1;
995 entry->flags.indicator_type = MI_CHECK;
996 } else {
997 entry = wMenuAddCallback(menu, _("Clip Options"), NULL, NULL);
998 scr->clip_options = makeClipOptionsMenu(scr);
999 if (scr->clip_options)
1000 wMenuEntrySetCascade(menu, entry, scr->clip_options);
1002 entry = wMenuAddCallback(menu, _("Rename Workspace"), renameCallback, NULL);
1003 wfree(entry->text);
1004 entry->text = _("Rename Workspace");
1006 entry = wMenuAddCallback(menu, _("Selected"), selectCallback, NULL);
1007 entry->flags.indicator = 1;
1008 entry->flags.indicator_on = 1;
1009 entry->flags.indicator_type = MI_CHECK;
1011 entry = wMenuAddCallback(menu, _("Select All Icons"), selectIconsCallback, NULL);
1012 wfree(entry->text);
1013 entry->text = _("Select All Icons");
1015 entry = wMenuAddCallback(menu, _("Keep Icon"), keepIconsCallback, NULL);
1016 wfree(entry->text);
1017 entry->text = _("Keep Icon");
1019 entry = wMenuAddCallback(menu, _("Move Icon To"), NULL, NULL);
1020 wfree(entry->text);
1021 entry->text = _("Move Icon To");
1022 scr->clip_submenu = makeWorkspaceMenu(scr);
1023 if (scr->clip_submenu)
1024 wMenuEntrySetCascade(menu, entry, scr->clip_submenu);
1026 entry = wMenuAddCallback(menu, _("Remove Icon"), removeIconsCallback, NULL);
1027 wfree(entry->text);
1028 entry->text = _("Remove Icon");
1030 wMenuAddCallback(menu, _("Attract Icons"), colectIconsCallback, NULL);
1033 wMenuAddCallback(menu, _("Launch"), launchCallback, NULL);
1035 wMenuAddCallback(menu, _("Unhide Here"), unhideHereCallback, NULL);
1037 entry = wMenuAddCallback(menu, _("Hide"), hideCallback, NULL);
1038 wfree(entry->text);
1039 entry->text = _("Hide");
1041 wMenuAddCallback(menu, _("Settings..."), settingsCallback, NULL);
1043 wMenuAddCallback(menu, _("Kill"), killCallback, NULL);
1045 if (type == WM_CLIP)
1046 scr->clip_menu = menu;
1048 return menu;
1051 WDock *wDockCreate(WScreen *scr, int type)
1053 WDock *dock;
1054 WAppIcon *btn;
1056 make_keys();
1058 dock = wmalloc(sizeof(WDock));
1060 dock->max_icons = DOCK_MAX_ICONS;
1062 dock->icon_array = wmalloc(sizeof(WAppIcon *) * dock->max_icons);
1064 btn = mainIconCreate(scr, type);
1066 btn->dock = dock;
1068 dock->x_pos = btn->x_pos;
1069 dock->y_pos = btn->y_pos;
1070 dock->screen_ptr = scr;
1071 dock->type = type;
1072 dock->icon_count = 1;
1073 dock->on_right_side = 1;
1074 dock->collapsed = 0;
1075 dock->auto_collapse = 0;
1076 dock->auto_collapse_magic = NULL;
1077 dock->auto_raise_lower = 0;
1078 dock->auto_lower_magic = NULL;
1079 dock->auto_raise_magic = NULL;
1080 dock->attract_icons = 0;
1081 dock->lowered = 1;
1082 dock->icon_array[0] = btn;
1083 wRaiseFrame(btn->icon->core);
1084 XMoveWindow(dpy, btn->icon->core->window, btn->x_pos, btn->y_pos);
1086 /* create dock menu */
1087 dock->menu = dockMenuCreate(scr, type);
1089 return dock;
1092 void wDockDestroy(WDock *dock)
1094 int i;
1095 WAppIcon *aicon;
1097 for (i = (dock->type == WM_CLIP) ? 1 : 0; i < dock->max_icons; i++) {
1098 aicon = dock->icon_array[i];
1099 if (aicon) {
1100 int keepit = aicon->running && wApplicationOf(aicon->main_window);
1101 wDockDetach(dock, aicon);
1102 if (keepit) {
1103 /* XXX: can: aicon->icon == NULL ? */
1104 PlaceIcon(dock->screen_ptr, &aicon->x_pos, &aicon->y_pos,
1105 wGetHeadForWindow(aicon->icon->owner));
1106 XMoveWindow(dpy, aicon->icon->core->window, aicon->x_pos, aicon->y_pos);
1107 if (!dock->mapped || dock->collapsed)
1108 XMapWindow(dpy, aicon->icon->core->window);
1112 if (wPreferences.auto_arrange_icons)
1113 wArrangeIcons(dock->screen_ptr, True);
1114 wfree(dock->icon_array);
1115 if (dock->menu && dock->type != WM_CLIP)
1116 wMenuDestroy(dock->menu, True);
1117 if (dock->screen_ptr->last_dock == dock)
1118 dock->screen_ptr->last_dock = NULL;
1119 wfree(dock);
1122 void wClipIconPaint(WAppIcon *aicon)
1124 WScreen *scr = aicon->icon->core->screen_ptr;
1125 WWorkspace *workspace = scr->workspaces[scr->current_workspace];
1126 WMColor *color;
1127 Window win = aicon->icon->core->window;
1128 int length, nlength;
1129 char *ws_name, ws_number[10];
1130 int ty, tx;
1132 wIconPaint(aicon->icon);
1134 length = strlen(workspace->name);
1135 ws_name = wmalloc(length + 1);
1136 snprintf(ws_name, length + 1, "%s", workspace->name);
1137 snprintf(ws_number, sizeof(ws_number), "%i", scr->current_workspace + 1);
1138 nlength = strlen(ws_number);
1140 if (!workspace->clip->collapsed)
1141 color = scr->clip_title_color[CLIP_NORMAL];
1142 else
1143 color = scr->clip_title_color[CLIP_COLLAPSED];
1145 ty = ICON_SIZE - WMFontHeight(scr->clip_title_font) - 3;
1147 tx = CLIP_BUTTON_SIZE * ICON_SIZE / 64;
1149 if(wPreferences.show_clip_title)
1150 WMDrawString(scr->wmscreen, win, color, scr->clip_title_font, tx, ty, ws_name, length);
1152 tx = (ICON_SIZE / 2 - WMWidthOfString(scr->clip_title_font, ws_number, nlength)) / 2;
1154 WMDrawString(scr->wmscreen, win, color, scr->clip_title_font, tx, 2, ws_number, nlength);
1156 wfree(ws_name);
1158 if (aicon->launching) {
1159 XFillRectangle(dpy, aicon->icon->core->window, scr->stipple_gc,
1160 0, 0, wPreferences.icon_size, wPreferences.icon_size);
1162 paintClipButtons(aicon, aicon->dock->lclip_button_pushed, aicon->dock->rclip_button_pushed);
1165 static void clipIconExpose(WObjDescriptor *desc, XEvent *event)
1167 wClipIconPaint(desc->parent);
1170 static void dockIconPaint(WAppIcon *btn)
1172 if (btn == btn->icon->core->screen_ptr->clip_icon)
1173 wClipIconPaint(btn);
1174 else {
1175 wAppIconPaint(btn);
1176 wAppIconSave(btn);
1180 static WMPropList *make_icon_state(WAppIcon *btn)
1182 WMPropList *node = NULL;
1183 WMPropList *command, *autolaunch, *lock, *name, *forced;
1184 WMPropList *position, *buggy, *omnipresent;
1185 char *tmp;
1186 char buffer[64];
1188 if (btn) {
1189 if (!btn->command)
1190 command = WMCreatePLString("-");
1191 else
1192 command = WMCreatePLString(btn->command);
1194 autolaunch = btn->auto_launch ? dYes : dNo;
1196 lock = btn->lock ? dYes : dNo;
1198 tmp = EscapeWM_CLASS(btn->wm_instance, btn->wm_class);
1200 name = WMCreatePLString(tmp);
1202 wfree(tmp);
1204 forced = btn->forced_dock ? dYes : dNo;
1206 buggy = btn->buggy_app ? dYes : dNo;
1208 if (btn == btn->icon->core->screen_ptr->clip_icon)
1209 snprintf(buffer, sizeof(buffer), "%i,%i", btn->x_pos, btn->y_pos);
1210 else
1211 snprintf(buffer, sizeof(buffer), "%hi,%hi", btn->xindex, btn->yindex);
1212 position = WMCreatePLString(buffer);
1214 node = WMCreatePLDictionary(dCommand, command,
1215 dName, name,
1216 dAutoLaunch, autolaunch,
1217 dLock, lock,
1218 dForced, forced, dBuggyApplication, buggy, dPosition, position, NULL);
1219 WMReleasePropList(command);
1220 WMReleasePropList(name);
1221 WMReleasePropList(position);
1223 omnipresent = btn->omnipresent ? dYes : dNo;
1224 if (btn->dock != btn->icon->core->screen_ptr->dock && (btn->xindex != 0 || btn->yindex != 0))
1225 WMPutInPLDictionary(node, dOmnipresent, omnipresent);
1227 #ifdef XDND /* was OFFIX */
1228 if (btn->dnd_command) {
1229 command = WMCreatePLString(btn->dnd_command);
1230 WMPutInPLDictionary(node, dDropCommand, command);
1231 WMReleasePropList(command);
1233 #endif /* XDND */
1235 if (btn->paste_command) {
1236 command = WMCreatePLString(btn->paste_command);
1237 WMPutInPLDictionary(node, dPasteCommand, command);
1238 WMReleasePropList(command);
1242 return node;
1245 static WMPropList *dockSaveState(WDock *dock)
1247 int i;
1248 WMPropList *icon_info;
1249 WMPropList *list = NULL, *dock_state = NULL;
1250 WMPropList *value, *key;
1251 char buffer[256];
1253 list = WMCreatePLArray(NULL);
1255 for (i = (dock->type == WM_DOCK ? 0 : 1); i < dock->max_icons; i++) {
1256 WAppIcon *btn = dock->icon_array[i];
1258 if (!btn || btn->attracted)
1259 continue;
1261 if ((icon_info = make_icon_state(dock->icon_array[i]))) {
1262 WMAddToPLArray(list, icon_info);
1263 WMReleasePropList(icon_info);
1267 dock_state = WMCreatePLDictionary(dApplications, list, NULL);
1269 if (dock->type == WM_DOCK) {
1270 snprintf(buffer, sizeof(buffer), "Applications%i", dock->screen_ptr->scr_height);
1271 key = WMCreatePLString(buffer);
1272 WMPutInPLDictionary(dock_state, key, list);
1273 WMReleasePropList(key);
1275 snprintf(buffer, sizeof(buffer), "%i,%i", (dock->on_right_side ? -ICON_SIZE : 0), dock->y_pos);
1276 value = WMCreatePLString(buffer);
1277 WMPutInPLDictionary(dock_state, dPosition, value);
1278 WMReleasePropList(value);
1280 WMReleasePropList(list);
1282 value = (dock->lowered ? dYes : dNo);
1283 WMPutInPLDictionary(dock_state, dLowered, value);
1285 if (dock->type == WM_CLIP) {
1286 value = (dock->collapsed ? dYes : dNo);
1287 WMPutInPLDictionary(dock_state, dCollapsed, value);
1289 value = (dock->auto_collapse ? dYes : dNo);
1290 WMPutInPLDictionary(dock_state, dAutoCollapse, value);
1292 value = (dock->auto_raise_lower ? dYes : dNo);
1293 WMPutInPLDictionary(dock_state, dAutoRaiseLower, value);
1295 value = (dock->attract_icons ? dYes : dNo);
1296 WMPutInPLDictionary(dock_state, dAutoAttractIcons, value);
1299 return dock_state;
1302 void wDockSaveState(WScreen *scr, WMPropList *old_state)
1304 WMPropList *dock_state;
1305 WMPropList *keys;
1307 dock_state = dockSaveState(scr->dock);
1310 * Copy saved states of docks with different sizes.
1312 if (old_state) {
1313 int i;
1314 WMPropList *tmp;
1316 keys = WMGetPLDictionaryKeys(old_state);
1317 for (i = 0; i < WMGetPropListItemCount(keys); i++) {
1318 tmp = WMGetFromPLArray(keys, i);
1320 if (strncasecmp(WMGetFromPLString(tmp), "applications", 12) == 0
1321 && !WMGetFromPLDictionary(dock_state, tmp)) {
1323 WMPutInPLDictionary(dock_state, tmp, WMGetFromPLDictionary(old_state, tmp));
1326 WMReleasePropList(keys);
1329 WMPutInPLDictionary(scr->session_state, dDock, dock_state);
1331 WMReleasePropList(dock_state);
1334 void wClipSaveState(WScreen *scr)
1336 WMPropList *clip_state;
1338 clip_state = make_icon_state(scr->clip_icon);
1340 WMPutInPLDictionary(scr->session_state, dClip, clip_state);
1342 WMReleasePropList(clip_state);
1345 WMPropList *wClipSaveWorkspaceState(WScreen *scr, int workspace)
1347 return dockSaveState(scr->workspaces[workspace]->clip);
1350 static Bool getBooleanDockValue(WMPropList *value, WMPropList *key)
1352 if (value) {
1353 if (WMIsPLString(value)) {
1354 if (strcasecmp(WMGetFromPLString(value), "YES") == 0)
1355 return True;
1356 } else {
1357 wwarning(_("bad value in docked icon state info %s"), WMGetFromPLString(key));
1360 return False;
1363 static WAppIcon *restore_icon_state(WScreen *scr, WMPropList *info, int type, int index)
1365 WAppIcon *aicon;
1366 WMPropList *cmd, *value;
1368 cmd = WMGetFromPLDictionary(info, dCommand);
1369 if (!cmd || !WMIsPLString(cmd)) {
1370 return NULL;
1373 /* parse window name */
1374 value = WMGetFromPLDictionary(info, dName);
1375 if (!value)
1376 return NULL;
1379 char *wclass, *winstance;
1380 char *command;
1382 ParseWindowName(value, &winstance, &wclass, "dock");
1384 if (!winstance && !wclass) {
1385 return NULL;
1388 /* get commands */
1390 if (cmd)
1391 command = wstrdup(WMGetFromPLString(cmd));
1392 else
1393 command = NULL;
1395 if (!command || strcmp(command, "-") == 0) {
1396 if (command)
1397 wfree(command);
1398 if (wclass)
1399 wfree(wclass);
1400 if (winstance)
1401 wfree(winstance);
1403 return NULL;
1406 aicon = wAppIconCreateForDock(scr, command, winstance, wclass, TILE_NORMAL);
1407 if (wclass)
1408 wfree(wclass);
1409 if (winstance)
1410 wfree(winstance);
1411 if (command)
1412 wfree(command);
1415 aicon->icon->core->descriptor.handle_mousedown = iconMouseDown;
1416 if (type == WM_CLIP) {
1417 aicon->icon->core->descriptor.handle_enternotify = clipEnterNotify;
1418 aicon->icon->core->descriptor.handle_leavenotify = clipLeaveNotify;
1420 aicon->icon->core->descriptor.parent_type = WCLASS_DOCK_ICON;
1421 aicon->icon->core->descriptor.parent = aicon;
1423 #ifdef XDND /* was OFFIX */
1424 cmd = WMGetFromPLDictionary(info, dDropCommand);
1425 if (cmd)
1426 aicon->dnd_command = wstrdup(WMGetFromPLString(cmd));
1427 #endif
1429 cmd = WMGetFromPLDictionary(info, dPasteCommand);
1430 if (cmd)
1431 aicon->paste_command = wstrdup(WMGetFromPLString(cmd));
1433 /* check auto launch */
1434 value = WMGetFromPLDictionary(info, dAutoLaunch);
1436 aicon->auto_launch = getBooleanDockValue(value, dAutoLaunch);
1438 /* check lock */
1439 value = WMGetFromPLDictionary(info, dLock);
1441 aicon->lock = getBooleanDockValue(value, dLock);
1443 /* check if it wasn't normally docked */
1444 value = WMGetFromPLDictionary(info, dForced);
1446 aicon->forced_dock = getBooleanDockValue(value, dForced);
1448 /* check if we can rely on the stuff in the app */
1449 value = WMGetFromPLDictionary(info, dBuggyApplication);
1451 aicon->buggy_app = getBooleanDockValue(value, dBuggyApplication);
1453 /* get position in the dock */
1454 value = WMGetFromPLDictionary(info, dPosition);
1455 if (value && WMIsPLString(value)) {
1456 if (sscanf(WMGetFromPLString(value), "%hi,%hi", &aicon->xindex, &aicon->yindex) != 2)
1457 wwarning(_("bad value in docked icon state info %s"), WMGetFromPLString(dPosition));
1459 /* check position sanity */
1460 /* incomplete section! */
1461 if (type == WM_DOCK) {
1462 aicon->xindex = 0;
1463 if (aicon->yindex < 0)
1464 wwarning(_("bad value in docked icon position %i,%i"),
1465 aicon->xindex, aicon->yindex);
1467 } else {
1468 aicon->yindex = index;
1469 aicon->xindex = 0;
1472 /* check if icon is omnipresent */
1473 value = WMGetFromPLDictionary(info, dOmnipresent);
1475 aicon->omnipresent = getBooleanDockValue(value, dOmnipresent);
1477 aicon->running = 0;
1478 aicon->docked = 1;
1480 return aicon;
1483 #define COMPLAIN(key) wwarning(_("bad value in dock state info:%s"), key)
1485 WAppIcon *wClipRestoreState(WScreen *scr, WMPropList *clip_state)
1487 WAppIcon *icon;
1488 WMPropList *value;
1490 icon = mainIconCreate(scr, WM_CLIP);
1492 if (!clip_state)
1493 return icon;
1495 WMRetainPropList(clip_state);
1497 /* restore position */
1499 value = WMGetFromPLDictionary(clip_state, dPosition);
1501 if (value) {
1502 if (!WMIsPLString(value))
1503 COMPLAIN("Position");
1504 else {
1505 WMRect rect;
1506 int flags;
1508 if (sscanf(WMGetFromPLString(value), "%i,%i", &icon->x_pos, &icon->y_pos) != 2)
1509 COMPLAIN("Position");
1511 /* check position sanity */
1512 rect.pos.x = icon->x_pos;
1513 rect.pos.y = icon->y_pos;
1514 rect.size.width = rect.size.height = ICON_SIZE;
1516 wGetRectPlacementInfo(scr, rect, &flags);
1517 if (flags & (XFLAG_DEAD | XFLAG_PARTIAL))
1518 wScreenKeepInside(scr, &icon->x_pos, &icon->y_pos, ICON_SIZE, ICON_SIZE);
1521 #ifdef XDND /* was OFFIX */
1522 value = WMGetFromPLDictionary(clip_state, dDropCommand);
1523 if (value && WMIsPLString(value))
1524 icon->dnd_command = wstrdup(WMGetFromPLString(value));
1525 #endif
1527 value = WMGetFromPLDictionary(clip_state, dPasteCommand);
1528 if (value && WMIsPLString(value))
1529 icon->paste_command = wstrdup(WMGetFromPLString(value));
1531 WMReleasePropList(clip_state);
1533 return icon;
1536 WDock *wDockRestoreState(WScreen *scr, WMPropList *dock_state, int type)
1538 WDock *dock;
1539 WMPropList *apps;
1540 WMPropList *value;
1541 WAppIcon *aicon, *old_top;
1542 int count, i;
1544 dock = wDockCreate(scr, type);
1546 if (!dock_state)
1547 return dock;
1549 WMRetainPropList(dock_state);
1551 /* restore position */
1552 value = WMGetFromPLDictionary(dock_state, dPosition);
1553 if (value) {
1554 if (!WMIsPLString(value)) {
1555 COMPLAIN("Position");
1556 } else {
1557 WMRect rect;
1558 int flags;
1560 if (sscanf(WMGetFromPLString(value), "%i,%i", &dock->x_pos, &dock->y_pos) != 2)
1561 COMPLAIN("Position");
1563 /* check position sanity */
1564 rect.pos.x = dock->x_pos;
1565 rect.pos.y = dock->y_pos;
1566 rect.size.width = rect.size.height = ICON_SIZE;
1568 wGetRectPlacementInfo(scr, rect, &flags);
1569 if (flags & (XFLAG_DEAD | XFLAG_PARTIAL)) {
1570 int x = dock->x_pos;
1571 wScreenKeepInside(scr, &x, &dock->y_pos, ICON_SIZE, ICON_SIZE);
1574 /* Is this needed any more? */
1575 if (type == WM_CLIP) {
1576 if (dock->x_pos < 0) {
1577 dock->x_pos = 0;
1578 } else if (dock->x_pos > scr->scr_width - ICON_SIZE) {
1579 dock->x_pos = scr->scr_width - ICON_SIZE;
1581 } else {
1582 if (dock->x_pos >= 0) {
1583 dock->x_pos = DOCK_EXTRA_SPACE;
1584 dock->on_right_side = 0;
1585 } else {
1586 dock->x_pos = scr->scr_width - DOCK_EXTRA_SPACE - ICON_SIZE;
1587 dock->on_right_side = 1;
1593 /* restore lowered/raised state */
1594 dock->lowered = 0;
1596 value = WMGetFromPLDictionary(dock_state, dLowered);
1597 if (value) {
1598 if (!WMIsPLString(value)) {
1599 COMPLAIN("Lowered");
1600 } else {
1601 if (strcasecmp(WMGetFromPLString(value), "YES") == 0) {
1602 dock->lowered = 1;
1607 /* restore collapsed state */
1608 dock->collapsed = 0;
1610 value = WMGetFromPLDictionary(dock_state, dCollapsed);
1611 if (value) {
1612 if (!WMIsPLString(value)) {
1613 COMPLAIN("Collapsed");
1614 } else {
1615 if (strcasecmp(WMGetFromPLString(value), "YES") == 0) {
1616 dock->collapsed = 1;
1621 /* restore auto-collapsed state */
1622 value = WMGetFromPLDictionary(dock_state, dAutoCollapse);
1623 if (value) {
1624 if (!WMIsPLString(value)) {
1625 COMPLAIN("AutoCollapse");
1626 } else {
1627 if (strcasecmp(WMGetFromPLString(value), "YES") == 0) {
1628 dock->auto_collapse = 1;
1629 dock->collapsed = 1;
1634 /* restore auto-raise/lower state */
1635 value = WMGetFromPLDictionary(dock_state, dAutoRaiseLower);
1636 if (value) {
1637 if (!WMIsPLString(value)) {
1638 COMPLAIN("AutoRaiseLower");
1639 } else {
1640 if (strcasecmp(WMGetFromPLString(value), "YES") == 0)
1641 dock->auto_raise_lower = 1;
1645 /* restore attract icons state */
1646 dock->attract_icons = 0;
1648 value = WMGetFromPLDictionary(dock_state, dAutoAttractIcons);
1649 if (value) {
1650 if (!WMIsPLString(value)) {
1651 COMPLAIN("AutoAttractIcons");
1652 } else {
1653 if (strcasecmp(WMGetFromPLString(value), "YES") == 0)
1654 dock->attract_icons = 1;
1658 /* application list */
1661 WMPropList *tmp;
1662 char buffer[64];
1665 * When saving, it saves the dock state in
1666 * Applications and Applicationsnnn
1668 * When loading, it will first try Applicationsnnn.
1669 * If it does not exist, use Applications as default.
1672 snprintf(buffer, sizeof(buffer), "Applications%i", scr->scr_height);
1674 tmp = WMCreatePLString(buffer);
1675 apps = WMGetFromPLDictionary(dock_state, tmp);
1676 WMReleasePropList(tmp);
1678 if (!apps)
1679 apps = WMGetFromPLDictionary(dock_state, dApplications);
1682 if (!apps)
1683 goto finish;
1685 count = WMGetPropListItemCount(apps);
1686 if (count == 0)
1687 goto finish;
1689 old_top = dock->icon_array[0];
1691 /* dock->icon_count is set to 1 when dock is created.
1692 * Since Clip is already restored, we want to keep it so for clip,
1693 * but for dock we may change the default top tile, so we set it to 0.
1695 if (type == WM_DOCK)
1696 dock->icon_count = 0;
1698 for (i = 0; i < count; i++) {
1699 if (dock->icon_count >= dock->max_icons) {
1700 wwarning(_("there are too many icons stored in dock. Ignoring what doesn't fit"));
1701 break;
1704 value = WMGetFromPLArray(apps, i);
1705 aicon = restore_icon_state(scr, value, type, dock->icon_count);
1707 dock->icon_array[dock->icon_count] = aicon;
1709 if (aicon) {
1710 aicon->dock = dock;
1711 aicon->x_pos = dock->x_pos + (aicon->xindex * ICON_SIZE);
1712 aicon->y_pos = dock->y_pos + (aicon->yindex * ICON_SIZE);
1714 if (dock->lowered)
1715 ChangeStackingLevel(aicon->icon->core, WMNormalLevel);
1716 else
1717 ChangeStackingLevel(aicon->icon->core, WMDockLevel);
1719 wCoreConfigure(aicon->icon->core, aicon->x_pos, aicon->y_pos, 0, 0);
1721 if (!dock->collapsed)
1722 XMapWindow(dpy, aicon->icon->core->window);
1723 wRaiseFrame(aicon->icon->core);
1725 dock->icon_count++;
1726 } else if (dock->icon_count == 0 && type == WM_DOCK)
1727 dock->icon_count++;
1730 /* if the first icon is not defined, use the default */
1731 if (dock->icon_array[0] == NULL) {
1732 /* update default icon */
1733 old_top->x_pos = dock->x_pos;
1734 old_top->y_pos = dock->y_pos;
1735 if (dock->lowered)
1736 ChangeStackingLevel(old_top->icon->core, WMNormalLevel);
1737 else
1738 ChangeStackingLevel(old_top->icon->core, WMDockLevel);
1739 dock->icon_array[0] = old_top;
1740 XMoveWindow(dpy, old_top->icon->core->window, dock->x_pos, dock->y_pos);
1741 /* we don't need to increment dock->icon_count here because it was
1742 * incremented in the loop above.
1744 } else if (old_top != dock->icon_array[0]) {
1745 if (old_top == scr->clip_icon)
1746 scr->clip_icon = dock->icon_array[0];
1747 wAppIconDestroy(old_top);
1750 finish:
1751 WMReleasePropList(dock_state);
1753 return dock;
1756 void wDockLaunchWithState(WDock *dock, WAppIcon *btn, WSavedState *state)
1758 if (btn && btn->command && !btn->running && !btn->launching) {
1760 btn->drop_launch = 0;
1761 btn->paste_launch = 0;
1763 btn->pid = execCommand(btn, btn->command, state);
1765 if (btn->pid > 0) {
1766 if (!btn->forced_dock && !btn->buggy_app) {
1767 btn->launching = 1;
1768 dockIconPaint(btn);
1771 } else {
1772 wfree(state);
1776 void wDockDoAutoLaunch(WDock *dock, int workspace)
1778 WAppIcon *btn;
1779 WSavedState *state;
1780 int i;
1782 for (i = 0; i < dock->max_icons; i++) {
1783 btn = dock->icon_array[i];
1784 if (!btn || !btn->auto_launch)
1785 continue;
1787 state = wmalloc(sizeof(WSavedState));
1788 state->workspace = workspace;
1789 /* TODO: this is klugy and is very difficult to understand
1790 * what's going on. Try to clean up */
1791 wDockLaunchWithState(dock, btn, state);
1795 #ifdef XDND /* was OFFIX */
1796 static WDock *findDock(WScreen *scr, XEvent *event, int *icon_pos)
1798 WDock *dock;
1799 int i;
1801 *icon_pos = -1;
1802 if ((dock = scr->dock) != NULL) {
1803 for (i = 0; i < dock->max_icons; i++) {
1804 if (dock->icon_array[i]
1805 && dock->icon_array[i]->icon->core->window == event->xclient.window) {
1806 *icon_pos = i;
1807 break;
1811 if (*icon_pos < 0 && (dock = scr->workspaces[scr->current_workspace]->clip) != NULL) {
1812 for (i = 0; i < dock->max_icons; i++) {
1813 if (dock->icon_array[i]
1814 && dock->icon_array[i]->icon->core->window == event->xclient.window) {
1815 *icon_pos = i;
1816 break;
1820 if (*icon_pos >= 0)
1821 return dock;
1822 return NULL;
1825 int wDockReceiveDNDDrop(WScreen *scr, XEvent *event)
1827 WDock *dock;
1828 WAppIcon *btn;
1829 int icon_pos;
1831 dock = findDock(scr, event, &icon_pos);
1832 if (!dock)
1833 return False;
1836 * Return True if the drop was on an application icon window.
1837 * In this case, let the ClientMessage handler redirect the
1838 * message to the app.
1840 if (dock->icon_array[icon_pos]->icon->icon_win != None)
1841 return True;
1843 if (dock->icon_array[icon_pos]->dnd_command != NULL) {
1844 scr->flags.dnd_data_convertion_status = 0;
1846 btn = dock->icon_array[icon_pos];
1848 if (!btn->forced_dock) {
1849 btn->relaunching = btn->running;
1850 btn->running = 1;
1852 if (btn->wm_instance || btn->wm_class) {
1853 WWindowAttributes attr;
1854 memset(&attr, 0, sizeof(WWindowAttributes));
1855 wDefaultFillAttributes(btn->icon->core->screen_ptr,
1856 btn->wm_instance, btn->wm_class, &attr, NULL, True);
1858 if (!attr.no_appicon)
1859 btn->launching = 1;
1860 else
1861 btn->running = 0;
1864 btn->paste_launch = 0;
1865 btn->drop_launch = 1;
1866 scr->last_dock = dock;
1867 btn->pid = execCommand(btn, btn->dnd_command, NULL);
1868 if (btn->pid > 0) {
1869 dockIconPaint(btn);
1870 } else {
1871 btn->launching = 0;
1872 if (!btn->relaunching) {
1873 btn->running = 0;
1877 return False;
1879 #endif /* XDND */
1881 Bool wDockAttachIcon(WDock *dock, WAppIcon *icon, int x, int y)
1883 WWindow *wwin;
1884 int index;
1886 wwin = icon->icon->owner;
1887 if (icon->command == NULL) {
1888 char *command;
1890 icon->editing = 0;
1892 command = GetCommandForWindow(wwin->client_win);
1893 if (command) {
1894 icon->command = command;
1895 } else {
1896 /* icon->forced_dock = 1; */
1897 if (dock->type != WM_CLIP || !icon->attracted) {
1898 icon->editing = 1;
1899 if (wInputDialog(dock->screen_ptr, _("Dock Icon"),
1900 _("Type the command used to launch the application"), &command)) {
1901 if (command && (command[0] == 0 || (command[0] == '-' && command[1] == 0))) {
1902 wfree(command);
1903 command = NULL;
1905 icon->command = command;
1906 icon->editing = 0;
1907 } else {
1908 icon->editing = 0;
1909 if (command)
1910 wfree(command);
1911 /* If the target is the dock, reject the icon. If
1912 * the target is the clip, make it an attracted icon
1914 if (dock->type == WM_CLIP) {
1915 icon->attracted = 1;
1916 if (!icon->icon->shadowed) {
1917 icon->icon->shadowed = 1;
1918 icon->icon->force_paint = 1;
1920 } else {
1921 return False;
1926 } else {
1927 icon->editing = 0;
1930 for (index = 1; index < dock->max_icons; index++)
1931 if (dock->icon_array[index] == NULL)
1932 break;
1933 /* if (index == dock->max_icons)
1934 return; */
1936 assert(index < dock->max_icons);
1938 dock->icon_array[index] = icon;
1939 icon->yindex = y;
1940 icon->xindex = x;
1942 icon->omnipresent = 0;
1944 icon->x_pos = dock->x_pos + x * ICON_SIZE;
1945 icon->y_pos = dock->y_pos + y * ICON_SIZE;
1947 dock->icon_count++;
1949 icon->running = 1;
1950 icon->launching = 0;
1951 icon->docked = 1;
1952 icon->dock = dock;
1953 icon->icon->core->descriptor.handle_mousedown = iconMouseDown;
1954 if (dock->type == WM_CLIP) {
1955 icon->icon->core->descriptor.handle_enternotify = clipEnterNotify;
1956 icon->icon->core->descriptor.handle_leavenotify = clipLeaveNotify;
1958 icon->icon->core->descriptor.parent_type = WCLASS_DOCK_ICON;
1959 icon->icon->core->descriptor.parent = icon;
1961 MoveInStackListUnder(dock->icon_array[index - 1]->icon->core, icon->icon->core);
1962 wAppIconMove(icon, icon->x_pos, icon->y_pos);
1963 wAppIconPaint(icon);
1964 wAppIconSave(icon);
1966 if (wPreferences.auto_arrange_icons)
1967 wArrangeIcons(dock->screen_ptr, True);
1969 #ifdef XDND /* was OFFIX */
1970 if (icon->command && !icon->dnd_command) {
1971 int len = strlen(icon->command) + 8;
1972 icon->dnd_command = wmalloc(len);
1973 snprintf(icon->dnd_command, len, "%s %%d", icon->command);
1975 #endif
1977 if (icon->command && !icon->paste_command) {
1978 int len = strlen(icon->command) + 8;
1979 icon->paste_command = wmalloc(len);
1980 snprintf(icon->paste_command, len, "%s %%s", icon->command);
1983 return True;
1986 static void reattachIcon(WDock *dock, WAppIcon *icon, int x, int y)
1988 int index;
1990 for (index = 1; index < dock->max_icons; index++) {
1991 if (dock->icon_array[index] == icon)
1992 break;
1994 assert(index < dock->max_icons);
1996 icon->yindex = y;
1997 icon->xindex = x;
1999 icon->x_pos = dock->x_pos + x * ICON_SIZE;
2000 icon->y_pos = dock->y_pos + y * ICON_SIZE;
2003 static Bool moveIconBetweenDocks(WDock *src, WDock *dest, WAppIcon *icon, int x, int y)
2005 WWindow *wwin;
2006 char *command;
2007 int index;
2009 if (src == dest)
2010 return True; /* No move needed, we're already there */
2012 if (dest == NULL)
2013 return False;
2015 wwin = icon->icon->owner;
2018 * For the moment we can't do this if we move icons in Clip from one
2019 * workspace to other, because if we move two or more icons without
2020 * command, the dialog box will not be able to tell us to which of the
2021 * moved icons it applies. -Dan
2023 if ((dest->type == WM_DOCK /*|| dest->keep_attracted */ ) && icon->command == NULL) {
2024 command = GetCommandForWindow(wwin->client_win);
2025 if (command) {
2026 icon->command = command;
2027 } else {
2028 icon->editing = 1;
2029 /* icon->forced_dock = 1; */
2030 if (wInputDialog(src->screen_ptr, _("Dock Icon"),
2031 _("Type the command used to launch the application"), &command)) {
2032 if (command && (command[0] == 0 || (command[0] == '-' && command[1] == 0))) {
2033 wfree(command);
2034 command = NULL;
2036 icon->command = command;
2037 } else {
2038 icon->editing = 0;
2039 if (command)
2040 wfree(command);
2041 return False;
2043 icon->editing = 0;
2047 if (dest->type == WM_DOCK)
2048 wClipMakeIconOmnipresent(icon, False);
2050 for (index = 1; index < src->max_icons; index++) {
2051 if (src->icon_array[index] == icon)
2052 break;
2054 assert(index < src->max_icons);
2056 src->icon_array[index] = NULL;
2057 src->icon_count--;
2059 for (index = 1; index < dest->max_icons; index++) {
2060 if (dest->icon_array[index] == NULL)
2061 break;
2063 /* if (index == dest->max_icons)
2064 return; */
2066 assert(index < dest->max_icons);
2068 dest->icon_array[index] = icon;
2069 icon->dock = dest;
2071 /* deselect the icon */
2072 if (icon->icon->selected)
2073 wIconSelect(icon->icon);
2075 if (dest->type == WM_DOCK) {
2076 icon->icon->core->descriptor.handle_enternotify = NULL;
2077 icon->icon->core->descriptor.handle_leavenotify = NULL;
2078 } else {
2079 icon->icon->core->descriptor.handle_enternotify = clipEnterNotify;
2080 icon->icon->core->descriptor.handle_leavenotify = clipLeaveNotify;
2083 /* set it to be kept when moving to dock.
2084 * Unless the icon does not have a command set
2086 if (icon->command && dest->type == WM_DOCK) {
2087 icon->attracted = 0;
2088 if (icon->icon->shadowed) {
2089 icon->icon->shadowed = 0;
2090 icon->icon->force_paint = 1;
2092 wAppIconSave(icon);
2095 if (src->auto_collapse || src->auto_raise_lower)
2096 clipLeave(src);
2098 icon->yindex = y;
2099 icon->xindex = x;
2101 icon->x_pos = dest->x_pos + x * ICON_SIZE;
2102 icon->y_pos = dest->y_pos + y * ICON_SIZE;
2104 dest->icon_count++;
2106 MoveInStackListUnder(dest->icon_array[index - 1]->icon->core, icon->icon->core);
2107 wAppIconPaint(icon);
2109 return True;
2112 void wDockDetach(WDock *dock, WAppIcon *icon)
2114 int index;
2116 /* make the settings panel be closed */
2117 if (icon->panel) {
2118 DestroyDockAppSettingsPanel(icon->panel);
2121 /* This must be called before icon->dock is set to NULL.
2122 * Don't move it. -Dan
2124 wClipMakeIconOmnipresent(icon, False);
2126 icon->docked = 0;
2127 icon->dock = NULL;
2128 icon->attracted = 0;
2129 icon->auto_launch = 0;
2130 if (icon->icon->shadowed) {
2131 icon->icon->shadowed = 0;
2132 icon->icon->force_paint = 1;
2135 /* deselect the icon */
2136 if (icon->icon->selected)
2137 wIconSelect(icon->icon);
2139 if (icon->command) {
2140 wfree(icon->command);
2141 icon->command = NULL;
2143 #ifdef XDND /* was OFFIX */
2144 if (icon->dnd_command) {
2145 wfree(icon->dnd_command);
2146 icon->dnd_command = NULL;
2148 #endif
2149 if (icon->paste_command) {
2150 wfree(icon->paste_command);
2151 icon->paste_command = NULL;
2154 for (index = 1; index < dock->max_icons; index++)
2155 if (dock->icon_array[index] == icon)
2156 break;
2157 assert(index < dock->max_icons);
2158 dock->icon_array[index] = NULL;
2159 icon->yindex = -1;
2160 icon->xindex = -1;
2162 dock->icon_count--;
2164 /* if the dock is not attached to an application or
2165 * the application did not set the appropriate hints yet,
2166 * destroy the icon */
2167 if (!icon->running || !wApplicationOf(icon->main_window))
2168 wAppIconDestroy(icon);
2169 else {
2170 icon->icon->core->descriptor.handle_mousedown = appIconMouseDown;
2171 icon->icon->core->descriptor.handle_enternotify = NULL;
2172 icon->icon->core->descriptor.handle_leavenotify = NULL;
2173 icon->icon->core->descriptor.parent_type = WCLASS_APPICON;
2174 icon->icon->core->descriptor.parent = icon;
2176 ChangeStackingLevel(icon->icon->core, NORMAL_ICON_LEVEL);
2178 wAppIconPaint(icon);
2179 if (wPreferences.auto_arrange_icons) {
2180 wArrangeIcons(dock->screen_ptr, True);
2183 if (dock->auto_collapse || dock->auto_raise_lower)
2184 clipLeave(dock);
2188 * returns the closest Dock slot index for the passed
2189 * coordinates.
2191 * Returns False if icon can't be docked.
2193 * Note: this function should NEVER alter ret_x or ret_y, unless it will
2194 * return True. -Dan
2196 Bool wDockSnapIcon(WDock *dock, WAppIcon *icon, int req_x, int req_y, int *ret_x, int *ret_y, int redocking)
2198 WScreen *scr = dock->screen_ptr;
2199 int dx, dy;
2200 int ex_x, ex_y;
2201 int i, offset = ICON_SIZE / 2;
2202 WAppIcon *aicon = NULL;
2203 WAppIcon *nicon = NULL;
2204 int max_y_icons;
2206 /* TODO: XINERAMA, for these */
2207 max_y_icons = scr->scr_height / ICON_SIZE - 1;
2209 if (wPreferences.flags.noupdates)
2210 return False;
2212 dx = dock->x_pos;
2213 dy = dock->y_pos;
2215 /* if the dock is full */
2216 if (!redocking && (dock->icon_count >= dock->max_icons)) {
2217 return False;
2220 /* exact position */
2221 if (req_y < dy)
2222 ex_y = (req_y - offset - dy) / ICON_SIZE;
2223 else
2224 ex_y = (req_y + offset - dy) / ICON_SIZE;
2226 if (req_x < dx)
2227 ex_x = (req_x - offset - dx) / ICON_SIZE;
2228 else
2229 ex_x = (req_x + offset - dx) / ICON_SIZE;
2231 /* check if the icon is outside the screen boundaries */
2233 WMRect rect;
2234 int flags;
2236 rect.pos.x = dx + ex_x * ICON_SIZE;
2237 rect.pos.y = dy + ex_y * ICON_SIZE;
2238 rect.size.width = rect.size.height = ICON_SIZE;
2240 wGetRectPlacementInfo(scr, rect, &flags);
2241 if (flags & (XFLAG_DEAD | XFLAG_PARTIAL))
2242 return False;
2245 if (dock->type == WM_DOCK) {
2246 if (icon->dock != dock && ex_x != 0)
2247 return False;
2249 aicon = NULL;
2250 for (i = 0; i < dock->max_icons; i++) {
2251 nicon = dock->icon_array[i];
2252 if (nicon && nicon->yindex == ex_y) {
2253 aicon = nicon;
2254 break;
2258 if (redocking) {
2259 int sig, done, closest;
2261 /* Possible cases when redocking:
2263 * icon dragged out of range of any slot -> false
2264 * icon dragged to range of free slot
2265 * icon dragged to range of same slot
2266 * icon dragged to range of different icon
2268 if (abs(ex_x) > DOCK_DETTACH_THRESHOLD)
2269 return False;
2271 if (ex_y >= 0 && ex_y <= max_y_icons && (aicon == icon || !aicon)) {
2272 *ret_x = 0;
2273 *ret_y = ex_y;
2274 return True;
2277 /* start looking at the upper slot or lower? */
2278 if (ex_y * ICON_SIZE < (req_y + offset - dy))
2279 sig = 1;
2280 else
2281 sig = -1;
2283 closest = -1;
2284 done = 0;
2285 /* look for closest free slot */
2286 for (i = 0; i < (DOCK_DETTACH_THRESHOLD + 1) * 2 && !done; i++) {
2287 int j;
2289 done = 1;
2290 closest = sig * (i / 2) + ex_y;
2291 /* check if this slot is used */
2292 if (closest >= 0) {
2293 for (j = 0; j < dock->max_icons; j++) {
2294 if (dock->icon_array[j]
2295 && dock->icon_array[j]->yindex == closest) {
2296 /* slot is used by someone else */
2297 if (dock->icon_array[j] != icon)
2298 done = 0;
2299 break;
2303 sig = -sig;
2305 if (done && closest >= 0 && closest <= max_y_icons &&
2306 ((ex_y >= closest && ex_y - closest < DOCK_DETTACH_THRESHOLD + 1)
2307 || (ex_y < closest && closest - ex_y <= DOCK_DETTACH_THRESHOLD + 1))) {
2308 *ret_x = 0;
2309 *ret_y = closest;
2310 return True;
2312 } else { /* !redocking */
2314 /* if slot is free and the icon is close enough, return it */
2315 if (!aicon && ex_x == 0 && ex_y >= 0 && ex_y <= max_y_icons) {
2316 *ret_x = 0;
2317 *ret_y = ex_y;
2318 return True;
2321 } else { /* CLIP */
2322 int neighbours = 0;
2323 int start, stop, k;
2325 start = icon->omnipresent ? 0 : scr->current_workspace;
2326 stop = icon->omnipresent ? scr->workspace_count : start + 1;
2328 aicon = NULL;
2329 for (k = start; k < stop; k++) {
2330 WDock *tmp = scr->workspaces[k]->clip;
2331 if (!tmp)
2332 continue;
2333 for (i = 0; i < tmp->max_icons; i++) {
2334 nicon = tmp->icon_array[i];
2335 if (nicon && nicon->xindex == ex_x && nicon->yindex == ex_y) {
2336 aicon = nicon;
2337 break;
2340 if (aicon)
2341 break;
2343 for (k = start; k < stop; k++) {
2344 WDock *tmp = scr->workspaces[k]->clip;
2345 if (!tmp)
2346 continue;
2347 for (i = 0; i < tmp->max_icons; i++) {
2348 nicon = tmp->icon_array[i];
2349 if (nicon && nicon != icon && /* Icon can't be it's own neighbour */
2350 (abs(nicon->xindex - ex_x) <= CLIP_ATTACH_VICINITY &&
2351 abs(nicon->yindex - ex_y) <= CLIP_ATTACH_VICINITY)) {
2352 neighbours = 1;
2353 break;
2356 if (neighbours)
2357 break;
2360 if (neighbours && (aicon == NULL || (redocking && aicon == icon))) {
2361 *ret_x = ex_x;
2362 *ret_y = ex_y;
2363 return True;
2366 return False;
2369 static int onScreen(WScreen *scr, int x, int y, int sx, int ex, int sy, int ey)
2371 WMRect rect = wmkrect(x, y, ICON_SIZE, ICON_SIZE);
2372 int flags;
2374 wGetRectPlacementInfo(scr, rect, &flags);
2376 return !(flags & (XFLAG_DEAD | XFLAG_PARTIAL));
2380 * returns true if it can find a free slot in the dock,
2381 * in which case it changes x_pos and y_pos accordingly.
2382 * Else returns false.
2384 Bool wDockFindFreeSlot(WDock *dock, int *x_pos, int *y_pos)
2386 WScreen *scr = dock->screen_ptr;
2387 WAppIcon *btn;
2388 WAppIconChain *chain;
2389 unsigned char *slot_map;
2390 int mwidth;
2391 int r;
2392 int x, y;
2393 int i, done = False;
2394 int corner;
2395 int sx = 0, sy = 0, ex = scr->scr_width, ey = scr->scr_height;
2396 int extra_count = 0;
2398 if (dock->type == WM_CLIP && dock != scr->workspaces[scr->current_workspace]->clip)
2399 extra_count = scr->global_icon_count;
2401 /* if the dock is full */
2402 if (dock->icon_count + extra_count >= dock->max_icons) {
2403 return False;
2406 if (!wPreferences.flags.nodock && scr->dock) {
2407 if (scr->dock->on_right_side)
2408 ex -= ICON_SIZE + DOCK_EXTRA_SPACE;
2409 else
2410 sx += ICON_SIZE + DOCK_EXTRA_SPACE;
2413 if (ex < dock->x_pos)
2414 ex = dock->x_pos;
2415 if (sx > dock->x_pos + ICON_SIZE)
2416 sx = dock->x_pos + ICON_SIZE;
2417 #define C_NONE 0
2418 #define C_NW 1
2419 #define C_NE 2
2420 #define C_SW 3
2421 #define C_SE 4
2423 /* check if clip is in a corner */
2424 if (dock->type == WM_CLIP) {
2425 if (dock->x_pos < 1 && dock->y_pos < 1)
2426 corner = C_NE;
2427 else if (dock->x_pos < 1 && dock->y_pos >= (ey - ICON_SIZE))
2428 corner = C_SE;
2429 else if (dock->x_pos >= (ex - ICON_SIZE) && dock->y_pos >= (ey - ICON_SIZE))
2430 corner = C_SW;
2431 else if (dock->x_pos >= (ex - ICON_SIZE) && dock->y_pos < 1)
2432 corner = C_NW;
2433 else
2434 corner = C_NONE;
2435 } else
2436 corner = C_NONE;
2438 /* If the clip is in the corner, use only slots that are in the border
2439 * of the screen */
2440 if (corner != C_NONE) {
2441 char *hmap, *vmap;
2442 int hcount, vcount;
2444 hcount = WMIN(dock->max_icons, scr->scr_width / ICON_SIZE);
2445 vcount = WMIN(dock->max_icons, scr->scr_height / ICON_SIZE);
2446 hmap = wmalloc(hcount + 1);
2447 vmap = wmalloc(vcount + 1);
2449 /* mark used positions */
2450 switch (corner) {
2451 case C_NE:
2452 for (i = 0; i < dock->max_icons; i++) {
2453 btn = dock->icon_array[i];
2454 if (!btn)
2455 continue;
2457 if (btn->xindex == 0 && btn->yindex > 0 && btn->yindex < vcount)
2458 vmap[btn->yindex] = 1;
2459 else if (btn->yindex == 0 && btn->xindex > 0 && btn->xindex < hcount)
2460 hmap[btn->xindex] = 1;
2462 for (chain = scr->global_icons; chain != NULL; chain = chain->next) {
2463 btn = chain->aicon;
2464 if (btn->xindex == 0 && btn->yindex > 0 && btn->yindex < vcount)
2465 vmap[btn->yindex] = 1;
2466 else if (btn->yindex == 0 && btn->xindex > 0 && btn->xindex < hcount)
2467 hmap[btn->xindex] = 1;
2469 break;
2470 case C_NW:
2471 for (i = 0; i < dock->max_icons; i++) {
2472 btn = dock->icon_array[i];
2473 if (!btn)
2474 continue;
2476 if (btn->xindex == 0 && btn->yindex > 0 && btn->yindex < vcount)
2477 vmap[btn->yindex] = 1;
2478 else if (btn->yindex == 0 && btn->xindex < 0 && btn->xindex > -hcount)
2479 hmap[-btn->xindex] = 1;
2481 for (chain = scr->global_icons; chain != NULL; chain = chain->next) {
2482 btn = chain->aicon;
2483 if (btn->xindex == 0 && btn->yindex > 0 && btn->yindex < vcount)
2484 vmap[btn->yindex] = 1;
2485 else if (btn->yindex == 0 && btn->xindex < 0 && btn->xindex > -hcount)
2486 hmap[-btn->xindex] = 1;
2488 break;
2489 case C_SE:
2490 for (i = 0; i < dock->max_icons; i++) {
2491 btn = dock->icon_array[i];
2492 if (!btn)
2493 continue;
2495 if (btn->xindex == 0 && btn->yindex < 0 && btn->yindex > -vcount)
2496 vmap[-btn->yindex] = 1;
2497 else if (btn->yindex == 0 && btn->xindex > 0 && btn->xindex < hcount)
2498 hmap[btn->xindex] = 1;
2500 for (chain = scr->global_icons; chain != NULL; chain = chain->next) {
2501 btn = chain->aicon;
2502 if (btn->xindex == 0 && btn->yindex < 0 && btn->yindex > -vcount)
2503 vmap[-btn->yindex] = 1;
2504 else if (btn->yindex == 0 && btn->xindex > 0 && btn->xindex < hcount)
2505 hmap[btn->xindex] = 1;
2507 break;
2508 case C_SW:
2509 default:
2510 for (i = 0; i < dock->max_icons; i++) {
2511 btn = dock->icon_array[i];
2512 if (!btn)
2513 continue;
2515 if (btn->xindex == 0 && btn->yindex < 0 && btn->yindex > -vcount)
2516 vmap[-btn->yindex] = 1;
2517 else if (btn->yindex == 0 && btn->xindex < 0 && btn->xindex > -hcount)
2518 hmap[-btn->xindex] = 1;
2520 for (chain = scr->global_icons; chain != NULL; chain = chain->next) {
2521 btn = chain->aicon;
2522 if (btn->xindex == 0 && btn->yindex < 0 && btn->yindex > -vcount)
2523 vmap[-btn->yindex] = 1;
2524 else if (btn->yindex == 0 && btn->xindex < 0 && btn->xindex > -hcount)
2525 hmap[-btn->xindex] = 1;
2528 x = 0;
2529 y = 0;
2530 done = 0;
2531 /* search a vacant slot */
2532 for (i = 1; i < WMAX(vcount, hcount); i++) {
2533 if (i < vcount && vmap[i] == 0) {
2534 /* found a slot */
2535 x = 0;
2536 y = i;
2537 done = 1;
2538 break;
2539 } else if (i < hcount && hmap[i] == 0) {
2540 /* found a slot */
2541 x = i;
2542 y = 0;
2543 done = 1;
2544 break;
2547 wfree(vmap);
2548 wfree(hmap);
2549 /* If found a slot, translate and return */
2550 if (done) {
2551 if (corner == C_NW || corner == C_NE) {
2552 *y_pos = y;
2553 } else {
2554 *y_pos = -y;
2556 if (corner == C_NE || corner == C_SE) {
2557 *x_pos = x;
2558 } else {
2559 *x_pos = -x;
2561 return True;
2563 /* else, try to find a slot somewhere else */
2566 /* a map of mwidth x mwidth would be enough if we allowed icons to be
2567 * placed outside of screen */
2568 mwidth = (int)ceil(sqrt(dock->max_icons));
2570 /* In the worst case (the clip is in the corner of the screen),
2571 * the amount of icons that fit in the clip is smaller.
2572 * Double the map to get a safe value.
2574 mwidth += mwidth;
2576 r = (mwidth - 1) / 2;
2578 slot_map = wmalloc(mwidth * mwidth);
2580 #define XY2OFS(x,y) (WMAX(abs(x),abs(y)) > r) ? 0 : (((y)+r)*(mwidth)+(x)+r)
2582 /* mark used slots in the map. If the slot falls outside the map
2583 * (for example, when all icons are placed in line), ignore them. */
2584 for (i = 0; i < dock->max_icons; i++) {
2585 btn = dock->icon_array[i];
2586 if (btn)
2587 slot_map[XY2OFS(btn->xindex, btn->yindex)] = 1;
2589 for (chain = scr->global_icons; chain != NULL; chain = chain->next) {
2590 slot_map[XY2OFS(chain->aicon->xindex, chain->aicon->yindex)] = 1;
2592 /* Find closest slot from the center that is free by scanning the
2593 * map from the center to outward in circular passes.
2594 * This will not result in a neat layout, but will be optimal
2595 * in the sense that there will not be holes left.
2597 done = 0;
2598 for (i = 1; i <= r && !done; i++) {
2599 int tx, ty;
2601 /* top and bottom parts of the ring */
2602 for (x = -i; x <= i && !done; x++) {
2603 tx = dock->x_pos + x * ICON_SIZE;
2604 y = -i;
2605 ty = dock->y_pos + y * ICON_SIZE;
2606 if (slot_map[XY2OFS(x, y)] == 0 && onScreen(scr, tx, ty, sx, ex, sy, ey)) {
2607 *x_pos = x;
2608 *y_pos = y;
2609 done = 1;
2610 break;
2612 y = i;
2613 ty = dock->y_pos + y * ICON_SIZE;
2614 if (slot_map[XY2OFS(x, y)] == 0 && onScreen(scr, tx, ty, sx, ex, sy, ey)) {
2615 *x_pos = x;
2616 *y_pos = y;
2617 done = 1;
2618 break;
2621 /* left and right parts of the ring */
2622 for (y = -i + 1; y <= i - 1; y++) {
2623 ty = dock->y_pos + y * ICON_SIZE;
2624 x = -i;
2625 tx = dock->x_pos + x * ICON_SIZE;
2626 if (slot_map[XY2OFS(x, y)] == 0 && onScreen(scr, tx, ty, sx, ex, sy, ey)) {
2627 *x_pos = x;
2628 *y_pos = y;
2629 done = 1;
2630 break;
2632 x = i;
2633 tx = dock->x_pos + x * ICON_SIZE;
2634 if (slot_map[XY2OFS(x, y)] == 0 && onScreen(scr, tx, ty, sx, ex, sy, ey)) {
2635 *x_pos = x;
2636 *y_pos = y;
2637 done = 1;
2638 break;
2642 wfree(slot_map);
2643 #undef XY2OFS
2644 return done;
2647 static void moveDock(WDock *dock, int new_x, int new_y)
2649 WAppIcon *btn;
2650 int i;
2652 dock->x_pos = new_x;
2653 dock->y_pos = new_y;
2654 for (i = 0; i < dock->max_icons; i++) {
2655 btn = dock->icon_array[i];
2656 if (btn) {
2657 btn->x_pos = new_x + btn->xindex * ICON_SIZE;
2658 btn->y_pos = new_y + btn->yindex * ICON_SIZE;
2659 XMoveWindow(dpy, btn->icon->core->window, btn->x_pos, btn->y_pos);
2664 static void swapDock(WDock *dock)
2666 WScreen *scr = dock->screen_ptr;
2667 WAppIcon *btn;
2668 int x, i;
2670 if (dock->on_right_side) {
2671 x = dock->x_pos = scr->scr_width - ICON_SIZE - DOCK_EXTRA_SPACE;
2672 } else {
2673 x = dock->x_pos = DOCK_EXTRA_SPACE;
2676 for (i = 0; i < dock->max_icons; i++) {
2677 btn = dock->icon_array[i];
2678 if (btn) {
2679 btn->x_pos = x;
2680 XMoveWindow(dpy, btn->icon->core->window, btn->x_pos, btn->y_pos);
2684 wScreenUpdateUsableArea(scr);
2687 static pid_t execCommand(WAppIcon *btn, char *command, WSavedState *state)
2689 WScreen *scr = btn->icon->core->screen_ptr;
2690 pid_t pid;
2691 char **argv;
2692 int argc;
2693 char *cmdline;
2695 cmdline = ExpandOptions(scr, command);
2697 if (scr->flags.dnd_data_convertion_status || !cmdline) {
2698 if (cmdline)
2699 wfree(cmdline);
2700 if (state)
2701 wfree(state);
2702 return 0;
2705 wtokensplit(cmdline, &argv, &argc);
2707 if (!argc) {
2708 if (cmdline)
2709 wfree(cmdline);
2710 if (state)
2711 wfree(state);
2712 return 0;
2715 if ((pid = fork()) == 0) {
2716 char **args;
2717 int i;
2719 SetupEnvironment(scr);
2721 #ifdef HAVE_SETSID
2722 setsid();
2723 #endif
2725 args = malloc(sizeof(char *) * (argc + 1));
2726 if (!args)
2727 exit(111);
2728 for (i = 0; i < argc; i++) {
2729 args[i] = argv[i];
2731 args[argc] = NULL;
2732 execvp(argv[0], args);
2733 exit(111);
2735 wtokenfree(argv, argc);
2737 if (pid > 0) {
2738 if (!state) {
2739 state = wmalloc(sizeof(WSavedState));
2740 state->hidden = -1;
2741 state->miniaturized = -1;
2742 state->shaded = -1;
2743 if (btn->dock == scr->dock || btn->omnipresent)
2744 state->workspace = -1;
2745 else
2746 state->workspace = scr->current_workspace;
2748 wWindowAddSavedState(btn->wm_instance, btn->wm_class, cmdline, pid, state);
2749 wAddDeathHandler(pid, (WDeathHandler *) trackDeadProcess, btn->dock);
2750 } else if (state) {
2751 wfree(state);
2753 wfree(cmdline);
2754 return pid;
2757 void wDockHideIcons(WDock *dock)
2759 int i;
2761 if (dock == NULL)
2762 return;
2764 for (i = 1; i < dock->max_icons; i++) {
2765 if (dock->icon_array[i])
2766 XUnmapWindow(dpy, dock->icon_array[i]->icon->core->window);
2768 dock->mapped = 0;
2770 dockIconPaint(dock->icon_array[0]);
2773 void wDockShowIcons(WDock *dock)
2775 int i, newlevel;
2776 WAppIcon *btn;
2778 if (dock == NULL)
2779 return;
2781 btn = dock->icon_array[0];
2782 moveDock(dock, btn->x_pos, btn->y_pos);
2784 newlevel = dock->lowered ? WMNormalLevel : WMDockLevel;
2785 ChangeStackingLevel(btn->icon->core, newlevel);
2787 for (i = 1; i < dock->max_icons; i++) {
2788 if (dock->icon_array[i]) {
2789 MoveInStackListAbove(dock->icon_array[i]->icon->core, btn->icon->core);
2790 break;
2794 if (!dock->collapsed) {
2795 for (i = 1; i < dock->max_icons; i++) {
2796 if (dock->icon_array[i]) {
2797 XMapWindow(dpy, dock->icon_array[i]->icon->core->window);
2801 dock->mapped = 1;
2803 dockIconPaint(btn);
2806 void wDockLower(WDock *dock)
2808 int i;
2810 for (i = 0; i < dock->max_icons; i++) {
2811 if (dock->icon_array[i])
2812 wLowerFrame(dock->icon_array[i]->icon->core);
2816 void wDockRaise(WDock *dock)
2818 int i;
2820 for (i = dock->max_icons - 1; i >= 0; i--) {
2821 if (dock->icon_array[i])
2822 wRaiseFrame(dock->icon_array[i]->icon->core);
2826 void wDockRaiseLower(WDock *dock)
2828 if (!dock->icon_array[0]->icon->core->stacking->above
2829 || (dock->icon_array[0]->icon->core->stacking->window_level
2830 != dock->icon_array[0]->icon->core->stacking->above->stacking->window_level))
2831 wDockLower(dock);
2832 else
2833 wDockRaise(dock);
2836 void wDockFinishLaunch(WDock *dock, WAppIcon *icon)
2838 icon->launching = 0;
2839 icon->relaunching = 0;
2840 dockIconPaint(icon);
2843 WAppIcon *wDockFindIconForWindow(WDock *dock, Window window)
2845 WAppIcon *icon;
2846 int i;
2848 for (i = 0; i < dock->max_icons; i++) {
2849 icon = dock->icon_array[i];
2850 if (icon && icon->main_window == window)
2851 return icon;
2853 return NULL;
2856 void wDockTrackWindowLaunch(WDock *dock, Window window)
2858 WAppIcon *icon;
2859 char *wm_class, *wm_instance;
2860 int i;
2861 Bool firstPass = True;
2862 Bool found = False;
2863 char *command = NULL;
2865 if (!PropGetWMClass(window, &wm_class, &wm_instance) || (!wm_class && !wm_instance)) {
2866 return;
2869 command = GetCommandForWindow(window);
2870 retry:
2871 for (i = 0; i < dock->max_icons; i++) {
2872 icon = dock->icon_array[i];
2873 if (!icon)
2874 continue;
2876 /* app is already attached to icon */
2877 if (icon->main_window == window) {
2878 found = True;
2879 break;
2882 if ((icon->wm_instance || icon->wm_class)
2883 && (icon->launching || !icon->running)) {
2885 if (icon->wm_instance && wm_instance && strcmp(icon->wm_instance, wm_instance) != 0) {
2886 continue;
2888 if (icon->wm_class && wm_class && strcmp(icon->wm_class, wm_class) != 0) {
2889 continue;
2891 if (firstPass && command && strcmp(icon->command, command) != 0) {
2892 continue;
2895 if (!icon->relaunching) {
2896 WApplication *wapp;
2898 /* Possibly an application that was docked with dockit,
2899 * but the user did not update WMState to indicate that
2900 * it was docked by force */
2901 wapp = wApplicationOf(window);
2902 if (!wapp) {
2903 icon->forced_dock = 1;
2904 icon->running = 0;
2906 if (!icon->forced_dock) {
2907 icon->main_window = window;
2910 found = True;
2911 if (!wPreferences.no_animations && !icon->launching &&
2912 !dock->screen_ptr->flags.startup && !dock->collapsed) {
2913 WAppIcon *aicon;
2914 int x0, y0;
2916 icon->launching = 1;
2917 dockIconPaint(icon);
2919 aicon = wAppIconCreateForDock(dock->screen_ptr, NULL,
2920 wm_instance, wm_class, TILE_NORMAL);
2921 /* XXX: can: aicon->icon == NULL ? */
2922 PlaceIcon(dock->screen_ptr, &x0, &y0, wGetHeadForWindow(aicon->icon->owner));
2923 wAppIconMove(aicon, x0, y0);
2924 /* Should this always be lowered? -Dan */
2925 if (dock->lowered)
2926 wLowerFrame(aicon->icon->core);
2927 XMapWindow(dpy, aicon->icon->core->window);
2928 aicon->launching = 1;
2929 wAppIconPaint(aicon);
2930 SlideWindow(aicon->icon->core->window, x0, y0, icon->x_pos, icon->y_pos);
2931 XUnmapWindow(dpy, aicon->icon->core->window);
2932 wAppIconDestroy(aicon);
2934 wDockFinishLaunch(dock, icon);
2935 break;
2939 if (firstPass && !found) {
2940 firstPass = False;
2941 goto retry;
2944 if (command)
2945 wfree(command);
2947 if (wm_class)
2948 free(wm_class);
2949 if (wm_instance)
2950 free(wm_instance);
2953 void wClipUpdateForWorkspaceChange(WScreen *scr, int workspace)
2955 if (!wPreferences.flags.noclip) {
2956 scr->clip_icon->dock = scr->workspaces[workspace]->clip;
2957 if (scr->current_workspace != workspace) {
2958 WDock *old_clip = scr->workspaces[scr->current_workspace]->clip;
2959 WAppIconChain *chain = scr->global_icons;
2961 while (chain) {
2962 moveIconBetweenDocks(chain->aicon->dock,
2963 scr->workspaces[workspace]->clip,
2964 chain->aicon, chain->aicon->xindex, chain->aicon->yindex);
2965 if (scr->workspaces[workspace]->clip->collapsed)
2966 XUnmapWindow(dpy, chain->aicon->icon->core->window);
2967 chain = chain->next;
2970 wDockHideIcons(old_clip);
2971 if (old_clip->auto_raise_lower) {
2972 if (old_clip->auto_raise_magic) {
2973 WMDeleteTimerHandler(old_clip->auto_raise_magic);
2974 old_clip->auto_raise_magic = NULL;
2976 wDockLower(old_clip);
2978 if (old_clip->auto_collapse) {
2979 if (old_clip->auto_expand_magic) {
2980 WMDeleteTimerHandler(old_clip->auto_expand_magic);
2981 old_clip->auto_expand_magic = NULL;
2983 old_clip->collapsed = 1;
2985 wDockShowIcons(scr->workspaces[workspace]->clip);
2990 static void trackDeadProcess(pid_t pid, unsigned char status, WDock *dock)
2992 WAppIcon *icon;
2993 int i;
2995 for (i = 0; i < dock->max_icons; i++) {
2996 icon = dock->icon_array[i];
2997 if (!icon)
2998 continue;
3000 if (icon->launching && icon->pid == pid) {
3001 if (!icon->relaunching) {
3002 icon->running = 0;
3003 icon->main_window = None;
3005 wDockFinishLaunch(dock, icon);
3006 icon->pid = 0;
3007 if (status == 111) {
3008 char msg[PATH_MAX];
3009 char *cmd;
3011 #ifdef XDND
3012 if (icon->drop_launch)
3013 cmd = icon->dnd_command;
3014 else
3015 #endif
3016 if (icon->paste_launch)
3017 cmd = icon->paste_command;
3018 else
3019 cmd = icon->command;
3021 snprintf(msg, sizeof(msg), _("Could not execute command \"%s\""), cmd);
3023 wMessageDialog(dock->screen_ptr, _("Error"), msg, _("OK"), NULL, NULL);
3025 break;
3030 static void toggleLowered(WDock *dock)
3032 WAppIcon *tmp;
3033 int newlevel, i;
3035 /* lower/raise Dock */
3036 if (!dock->lowered) {
3037 newlevel = WMNormalLevel;
3038 dock->lowered = 1;
3039 } else {
3040 newlevel = WMDockLevel;
3041 dock->lowered = 0;
3044 for (i = 0; i < dock->max_icons; i++) {
3045 tmp = dock->icon_array[i];
3046 if (!tmp)
3047 continue;
3049 ChangeStackingLevel(tmp->icon->core, newlevel);
3050 if (dock->lowered)
3051 wLowerFrame(tmp->icon->core);
3054 if (dock->type == WM_DOCK)
3055 wScreenUpdateUsableArea(dock->screen_ptr);
3058 static void toggleCollapsed(WDock *dock)
3060 if (dock->collapsed) {
3061 dock->collapsed = 0;
3062 wDockShowIcons(dock);
3063 } else {
3064 dock->collapsed = 1;
3065 wDockHideIcons(dock);
3069 static void openDockMenu(WDock *dock, WAppIcon *aicon, XEvent *event)
3071 WScreen *scr = dock->screen_ptr;
3072 WObjDescriptor *desc;
3073 WMenuEntry *entry;
3074 WApplication *wapp = NULL;
3075 int index = 0;
3076 int x_pos;
3077 int n_selected;
3078 int appIsRunning = aicon->running && aicon->icon && aicon->icon->owner;
3080 if (dock->type == WM_DOCK) {
3081 /* keep on top */
3082 entry = dock->menu->entries[index];
3083 entry->flags.indicator_on = !dock->lowered;
3084 entry->clientdata = dock;
3085 dock->menu->flags.realized = 0;
3086 } else {
3087 /* clip options */
3088 if (scr->clip_options)
3089 updateClipOptionsMenu(scr->clip_options, dock);
3091 n_selected = numberOfSelectedIcons(dock);
3093 /* Rename Workspace */
3094 entry = dock->menu->entries[++index];
3095 if (aicon == scr->clip_icon) {
3096 entry->callback = renameCallback;
3097 entry->clientdata = dock;
3098 entry->flags.indicator = 0;
3099 entry->text = _("Rename Workspace");
3100 } else {
3101 entry->callback = omnipresentCallback;
3102 entry->clientdata = aicon;
3103 if (n_selected > 0) {
3104 entry->flags.indicator = 0;
3105 entry->text = _("Toggle Omnipresent");
3106 } else {
3107 entry->flags.indicator = 1;
3108 entry->flags.indicator_on = aicon->omnipresent;
3109 entry->flags.indicator_type = MI_CHECK;
3110 entry->text = _("Omnipresent");
3114 /* select/unselect icon */
3115 entry = dock->menu->entries[++index];
3116 entry->clientdata = aicon;
3117 entry->flags.indicator_on = aicon->icon->selected;
3118 wMenuSetEnabled(dock->menu, index, aicon != scr->clip_icon);
3120 /* select/unselect all icons */
3121 entry = dock->menu->entries[++index];
3122 entry->clientdata = aicon;
3123 if (n_selected > 0)
3124 entry->text = _("Unselect All Icons");
3125 else
3126 entry->text = _("Select All Icons");
3127 wMenuSetEnabled(dock->menu, index, dock->icon_count > 1);
3129 /* keep icon(s) */
3130 entry = dock->menu->entries[++index];
3131 entry->clientdata = aicon;
3132 if (n_selected > 1)
3133 entry->text = _("Keep Icons");
3134 else
3135 entry->text = _("Keep Icon");
3136 wMenuSetEnabled(dock->menu, index, dock->icon_count > 1);
3138 /* this is the workspace submenu part */
3139 entry = dock->menu->entries[++index];
3140 if (n_selected > 1)
3141 entry->text = _("Move Icons To");
3142 else
3143 entry->text = _("Move Icon To");
3144 if (scr->clip_submenu)
3145 updateWorkspaceMenu(scr->clip_submenu, aicon);
3146 wMenuSetEnabled(dock->menu, index, !aicon->omnipresent);
3148 /* remove icon(s) */
3149 entry = dock->menu->entries[++index];
3150 entry->clientdata = aicon;
3151 if (n_selected > 1)
3152 entry->text = _("Remove Icons");
3153 else
3154 entry->text = _("Remove Icon");
3155 wMenuSetEnabled(dock->menu, index, dock->icon_count > 1);
3157 /* attract icon(s) */
3158 entry = dock->menu->entries[++index];
3159 entry->clientdata = aicon;
3161 dock->menu->flags.realized = 0;
3162 wMenuRealize(dock->menu);
3165 if (aicon->icon->owner) {
3166 wapp = wApplicationOf(aicon->icon->owner->main_window);
3167 } else {
3168 wapp = NULL;
3171 /* launch */
3172 entry = dock->menu->entries[++index];
3173 entry->clientdata = aicon;
3174 wMenuSetEnabled(dock->menu, index, aicon->command != NULL);
3176 /* unhide here */
3177 entry = dock->menu->entries[++index];
3178 entry->clientdata = aicon;
3179 if (wapp && wapp->flags.hidden) {
3180 entry->text = _("Unhide Here");
3181 } else {
3182 entry->text = _("Bring Here");
3184 wMenuSetEnabled(dock->menu, index, appIsRunning);
3186 /* hide */
3187 entry = dock->menu->entries[++index];
3188 entry->clientdata = aicon;
3189 if (wapp && wapp->flags.hidden) {
3190 entry->text = _("Unhide");
3191 } else {
3192 entry->text = _("Hide");
3194 wMenuSetEnabled(dock->menu, index, appIsRunning);
3196 /* settings */
3197 entry = dock->menu->entries[++index];
3198 entry->clientdata = aicon;
3199 wMenuSetEnabled(dock->menu, index, !aicon->editing && !wPreferences.flags.noupdates);
3201 /* kill */
3202 entry = dock->menu->entries[++index];
3203 entry->clientdata = aicon;
3204 wMenuSetEnabled(dock->menu, index, appIsRunning);
3206 if (!dock->menu->flags.realized)
3207 wMenuRealize(dock->menu);
3209 if (dock->type == WM_CLIP) {
3210 /*x_pos = event->xbutton.x_root+2; */
3211 x_pos = event->xbutton.x_root - dock->menu->frame->core->width / 2 - 1;
3212 if (x_pos < 0) {
3213 x_pos = 0;
3214 } else if (x_pos + dock->menu->frame->core->width > scr->scr_width - 2) {
3215 x_pos = scr->scr_width - dock->menu->frame->core->width - 4;
3217 } else {
3218 x_pos = dock->on_right_side ? scr->scr_width - dock->menu->frame->core->width - 3 : 0;
3221 wMenuMapAt(dock->menu, x_pos, event->xbutton.y_root + 2, False);
3223 /* allow drag select */
3224 event->xany.send_event = True;
3225 desc = &dock->menu->menu->descriptor;
3226 (*desc->handle_mousedown) (desc, event);
3229 /******************************************************************/
3230 static void iconDblClick(WObjDescriptor *desc, XEvent *event)
3232 WAppIcon *btn = desc->parent;
3233 WDock *dock = btn->dock;
3234 WApplication *wapp = NULL;
3235 int unhideHere = 0;
3237 if (btn->icon->owner && !(event->xbutton.state & ControlMask)) {
3238 wapp = wApplicationOf(btn->icon->owner->main_window);
3240 assert(wapp != NULL);
3242 unhideHere = (event->xbutton.state & ShiftMask);
3244 /* go to the last workspace that the user worked on the app */
3245 if (wapp->last_workspace != dock->screen_ptr->current_workspace && !unhideHere) {
3246 wWorkspaceChange(dock->screen_ptr, wapp->last_workspace);
3249 wUnhideApplication(wapp, event->xbutton.button == Button2, unhideHere);
3251 if (event->xbutton.state & MOD_MASK) {
3252 wHideOtherApplications(btn->icon->owner);
3254 } else {
3255 if (event->xbutton.button == Button1) {
3257 if (event->xbutton.state & MOD_MASK) {
3258 /* raise/lower dock */
3259 toggleLowered(dock);
3260 } else if (btn == dock->screen_ptr->clip_icon) {
3261 if (getClipButton(event->xbutton.x, event->xbutton.y) == CLIP_IDLE)
3262 toggleCollapsed(dock);
3263 else
3264 handleClipChangeWorkspace(dock->screen_ptr, event);
3265 } else if (btn->command) {
3266 if (!btn->launching && (!btn->running || (event->xbutton.state & ControlMask)))
3267 launchDockedApplication(btn, False);
3273 static void handleDockMove(WDock *dock, WAppIcon *aicon, XEvent *event)
3275 WScreen *scr = dock->screen_ptr;
3276 int ofs_x = event->xbutton.x, ofs_y = event->xbutton.y;
3277 int x, y;
3278 XEvent ev;
3279 int grabbed = 0, swapped = 0, done;
3280 Pixmap ghost = None;
3281 int superfluous = wPreferences.superfluous; /* we catch it to avoid problems */
3283 if (XGrabPointer(dpy, aicon->icon->core->window, True, ButtonMotionMask
3284 | ButtonReleaseMask | ButtonPressMask, GrabModeAsync,
3285 GrabModeAsync, None, None, CurrentTime) != GrabSuccess) {
3286 wwarning("pointer grab failed for dock move");
3288 y = 0;
3289 for (x = 0; x < dock->max_icons; x++) {
3290 if (dock->icon_array[x] != NULL && dock->icon_array[x]->yindex > y)
3291 y = dock->icon_array[x]->yindex;
3293 y++;
3294 XResizeWindow(dpy, scr->dock_shadow, ICON_SIZE, ICON_SIZE * y);
3296 done = 0;
3297 while (!done) {
3298 WMMaskEvent(dpy, PointerMotionMask | ButtonReleaseMask | ButtonPressMask
3299 | ButtonMotionMask | ExposureMask | EnterWindowMask, &ev);
3300 switch (ev.type) {
3301 case Expose:
3302 WMHandleEvent(&ev);
3303 break;
3305 case EnterNotify:
3306 /* It means the cursor moved so fast that it entered
3307 * something else (if moving slowly, it would have
3308 * stayed in the dock that is being moved. Ignore such
3309 * "spurious" EnterNotifiy's */
3310 break;
3312 case MotionNotify:
3313 if (!grabbed) {
3314 if (abs(ofs_x - ev.xmotion.x) >= MOVE_THRESHOLD
3315 || abs(ofs_y - ev.xmotion.y) >= MOVE_THRESHOLD) {
3316 XChangeActivePointerGrab(dpy, ButtonMotionMask
3317 | ButtonReleaseMask | ButtonPressMask,
3318 wCursor[WCUR_MOVE], CurrentTime);
3319 grabbed = 1;
3321 break;
3323 if (dock->type == WM_CLIP) {
3324 x = ev.xmotion.x_root - ofs_x;
3325 y = ev.xmotion.y_root - ofs_y;
3326 wScreenKeepInside(scr, &x, &y, ICON_SIZE, ICON_SIZE);
3328 moveDock(dock, x, y);
3329 } else {
3330 /* move vertically if pointer is inside the dock */
3331 if ((dock->on_right_side && ev.xmotion.x_root >= dock->x_pos - ICON_SIZE)
3332 || (!dock->on_right_side && ev.xmotion.x_root <= dock->x_pos + ICON_SIZE * 2)) {
3334 x = ev.xmotion.x_root - ofs_x;
3335 y = ev.xmotion.y_root - ofs_y;
3336 wScreenKeepInside(scr, &x, &y, ICON_SIZE, ICON_SIZE);
3337 moveDock(dock, dock->x_pos, y);
3339 /* move horizontally to change sides */
3340 x = ev.xmotion.x_root - ofs_x;
3341 if (!dock->on_right_side) {
3343 /* is on left */
3345 if (ev.xmotion.x_root > dock->x_pos + ICON_SIZE * 2) {
3346 XMoveWindow(dpy, scr->dock_shadow, scr->scr_width - ICON_SIZE
3347 - DOCK_EXTRA_SPACE - 1, dock->y_pos);
3348 if (superfluous && ghost == None) {
3349 ghost = MakeGhostDock(dock, dock->x_pos,
3350 scr->scr_width - ICON_SIZE
3351 - DOCK_EXTRA_SPACE - 1, dock->y_pos);
3352 XSetWindowBackgroundPixmap(dpy, scr->dock_shadow, ghost);
3353 XClearWindow(dpy, scr->dock_shadow);
3355 XMapRaised(dpy, scr->dock_shadow);
3356 swapped = 1;
3357 } else {
3358 if (superfluous && ghost != None) {
3359 XFreePixmap(dpy, ghost);
3360 ghost = None;
3362 XUnmapWindow(dpy, scr->dock_shadow);
3363 swapped = 0;
3365 } else {
3366 /* is on right */
3367 if (ev.xmotion.x_root < dock->x_pos - ICON_SIZE) {
3368 XMoveWindow(dpy, scr->dock_shadow, DOCK_EXTRA_SPACE, dock->y_pos);
3369 if (superfluous && ghost == None) {
3370 ghost = MakeGhostDock(dock, dock->x_pos,
3371 DOCK_EXTRA_SPACE, dock->y_pos);
3372 XSetWindowBackgroundPixmap(dpy, scr->dock_shadow, ghost);
3373 XClearWindow(dpy, scr->dock_shadow);
3375 XMapRaised(dpy, scr->dock_shadow);
3376 swapped = -1;
3377 } else {
3378 XUnmapWindow(dpy, scr->dock_shadow);
3379 swapped = 0;
3380 if (superfluous && ghost != None) {
3381 XFreePixmap(dpy, ghost);
3382 ghost = None;
3387 break;
3389 case ButtonPress:
3390 break;
3392 case ButtonRelease:
3393 if (ev.xbutton.button != event->xbutton.button)
3394 break;
3395 XUngrabPointer(dpy, CurrentTime);
3396 XUnmapWindow(dpy, scr->dock_shadow);
3397 XResizeWindow(dpy, scr->dock_shadow, ICON_SIZE, ICON_SIZE);
3398 if (dock->type == WM_DOCK) {
3399 if (swapped != 0) {
3400 if (swapped > 0)
3401 dock->on_right_side = 1;
3402 else
3403 dock->on_right_side = 0;
3404 swapDock(dock);
3405 wArrangeIcons(scr, False);
3408 done = 1;
3409 break;
3412 if (superfluous) {
3413 if (ghost != None)
3414 XFreePixmap(dpy, ghost);
3415 XSetWindowBackground(dpy, scr->dock_shadow, scr->white_pixel);
3419 static Bool handleIconMove(WDock *dock, WAppIcon *aicon, XEvent *event)
3421 WScreen *scr = dock->screen_ptr;
3422 Window wins[2];
3423 WIcon *icon = aicon->icon;
3424 WDock *dock2 = NULL, *last_dock = dock, *clip = NULL;
3425 int ondock, grabbed = 0, change_dock = 0, collapsed = 0;
3426 XEvent ev;
3427 int x = aicon->x_pos, y = aicon->y_pos;
3428 int ofs_x = event->xbutton.x, ofs_y = event->xbutton.y;
3429 int shad_x = x, shad_y = y;
3430 int ix = aicon->xindex, iy = aicon->yindex;
3431 int tmp;
3432 Pixmap ghost = None;
3433 Bool docked;
3434 int superfluous = wPreferences.superfluous; /* we catch it to avoid problems */
3435 int omnipresent = aicon->omnipresent; /* this must be cached!!! */
3436 Bool hasMoved = False;
3438 if (wPreferences.flags.noupdates)
3439 return hasMoved;
3441 if (XGrabPointer(dpy, icon->core->window, True, ButtonMotionMask
3442 | ButtonReleaseMask | ButtonPressMask, GrabModeAsync,
3443 GrabModeAsync, None, None, CurrentTime) != GrabSuccess) {
3446 if (!(event->xbutton.state & MOD_MASK))
3447 wRaiseFrame(icon->core);
3449 if (!wPreferences.flags.noclip)
3450 clip = scr->workspaces[scr->current_workspace]->clip;
3452 if (dock == scr->dock && !wPreferences.flags.noclip)
3453 dock2 = clip;
3454 else if (dock != scr->dock && !wPreferences.flags.nodock)
3455 dock2 = scr->dock;
3457 wins[0] = icon->core->window;
3458 wins[1] = scr->dock_shadow;
3459 XRestackWindows(dpy, wins, 2);
3460 XMoveResizeWindow(dpy, scr->dock_shadow, aicon->x_pos, aicon->y_pos, ICON_SIZE, ICON_SIZE);
3461 if (superfluous) {
3462 if (icon->pixmap != None)
3463 ghost = MakeGhostIcon(scr, icon->pixmap);
3464 else
3465 ghost = MakeGhostIcon(scr, icon->core->window);
3467 XSetWindowBackgroundPixmap(dpy, scr->dock_shadow, ghost);
3468 XClearWindow(dpy, scr->dock_shadow);
3470 XMapWindow(dpy, scr->dock_shadow);
3472 ondock = 1;
3474 while (1) {
3475 XMaskEvent(dpy, PointerMotionMask | ButtonReleaseMask | ButtonPressMask
3476 | ButtonMotionMask | ExposureMask, &ev);
3477 switch (ev.type) {
3478 case Expose:
3479 WMHandleEvent(&ev);
3480 break;
3482 case MotionNotify:
3483 hasMoved = True;
3484 if (!grabbed) {
3485 if (abs(ofs_x - ev.xmotion.x) >= MOVE_THRESHOLD
3486 || abs(ofs_y - ev.xmotion.y) >= MOVE_THRESHOLD) {
3487 XChangeActivePointerGrab(dpy, ButtonMotionMask
3488 | ButtonReleaseMask | ButtonPressMask,
3489 wCursor[WCUR_MOVE], CurrentTime);
3490 grabbed = 1;
3491 } else {
3492 break;
3496 if (omnipresent) {
3497 int i;
3498 for (i = 0; i < scr->workspace_count; i++) {
3499 if (i == scr->current_workspace)
3500 continue;
3501 wDockShowIcons(scr->workspaces[i]->clip);
3505 x = ev.xmotion.x_root - ofs_x;
3506 y = ev.xmotion.y_root - ofs_y;
3507 tmp = wDockSnapIcon(dock, aicon, x, y, &ix, &iy, True);
3508 if (tmp && dock2) {
3509 change_dock = 0;
3510 if (last_dock != dock && collapsed) {
3511 last_dock->collapsed = 1;
3512 wDockHideIcons(last_dock);
3513 collapsed = 0;
3515 if (!collapsed && (collapsed = dock->collapsed)) {
3516 dock->collapsed = 0;
3517 wDockShowIcons(dock);
3519 if (dock->auto_raise_lower)
3520 wDockRaise(dock);
3521 last_dock = dock;
3522 } else if (dock2) {
3523 tmp = wDockSnapIcon(dock2, aicon, x, y, &ix, &iy, False);
3524 if (tmp) {
3525 change_dock = 1;
3526 if (last_dock != dock2 && collapsed) {
3527 last_dock->collapsed = 1;
3528 wDockHideIcons(last_dock);
3529 collapsed = 0;
3531 if (!collapsed && (collapsed = dock2->collapsed)) {
3532 dock2->collapsed = 0;
3533 wDockShowIcons(dock2);
3535 if (dock2->auto_raise_lower)
3536 wDockRaise(dock2);
3537 last_dock = dock2;
3540 if (aicon->launching || aicon->lock || (aicon->running && !(ev.xmotion.state & MOD_MASK))
3541 || (!aicon->running && tmp)) {
3542 shad_x = last_dock->x_pos + ix * wPreferences.icon_size;
3543 shad_y = last_dock->y_pos + iy * wPreferences.icon_size;
3545 XMoveWindow(dpy, scr->dock_shadow, shad_x, shad_y);
3547 if (!ondock) {
3548 XMapWindow(dpy, scr->dock_shadow);
3550 ondock = 1;
3551 } else {
3552 if (ondock) {
3553 XUnmapWindow(dpy, scr->dock_shadow);
3555 ondock = 0;
3557 XMoveWindow(dpy, icon->core->window, x, y);
3558 break;
3560 case ButtonPress:
3561 break;
3563 case ButtonRelease:
3564 if (ev.xbutton.button != event->xbutton.button)
3565 break;
3566 XUngrabPointer(dpy, CurrentTime);
3567 if (ondock) {
3568 SlideWindow(icon->core->window, x, y, shad_x, shad_y);
3569 XUnmapWindow(dpy, scr->dock_shadow);
3570 if (!change_dock) {
3571 reattachIcon(dock, aicon, ix, iy);
3572 if (clip && dock != clip && clip->auto_raise_lower)
3573 wDockLower(clip);
3574 } else {
3575 docked = moveIconBetweenDocks(dock, dock2, aicon, ix, iy);
3576 if (!docked) {
3577 /* Slide it back if dock rejected it */
3578 SlideWindow(icon->core->window, x, y, aicon->x_pos, aicon->y_pos);
3579 reattachIcon(dock, aicon, aicon->xindex, aicon->yindex);
3581 if (last_dock->type == WM_CLIP && last_dock->auto_collapse) {
3582 collapsed = 0;
3585 } else {
3586 aicon->x_pos = x;
3587 aicon->y_pos = y;
3588 if (superfluous) {
3589 if (!aicon->running && !wPreferences.no_animations) {
3590 /* We need to deselect it, even if is deselected in
3591 * wDockDetach(), because else DoKaboom() will fail.
3593 if (aicon->icon->selected)
3594 wIconSelect(aicon->icon);
3596 DoKaboom(scr, aicon->icon->core->window, x, y);
3599 if (clip && clip->auto_raise_lower)
3600 wDockLower(clip);
3601 wDockDetach(dock, aicon);
3603 if (collapsed) {
3604 last_dock->collapsed = 1;
3605 wDockHideIcons(last_dock);
3606 collapsed = 0;
3608 if (superfluous) {
3609 if (ghost != None)
3610 XFreePixmap(dpy, ghost);
3611 XSetWindowBackground(dpy, scr->dock_shadow, scr->white_pixel);
3613 if (omnipresent) {
3614 int i;
3615 for (i = 0; i < scr->workspace_count; i++) {
3616 if (i == scr->current_workspace)
3617 continue;
3618 wDockHideIcons(scr->workspaces[i]->clip);
3621 return hasMoved;;
3624 return False; /* never reached */
3627 static int getClipButton(int px, int py)
3629 int pt = (CLIP_BUTTON_SIZE + 2) * ICON_SIZE / 64;
3631 if (px < 0 || py < 0 || px >= ICON_SIZE || py >= ICON_SIZE)
3632 return CLIP_IDLE;
3634 if (py <= pt - ((int)ICON_SIZE - 1 - px))
3635 return CLIP_FORWARD;
3636 else if (px <= pt - ((int)ICON_SIZE - 1 - py))
3637 return CLIP_REWIND;
3639 return CLIP_IDLE;
3642 static void handleClipChangeWorkspace(WScreen *scr, XEvent *event)
3644 XEvent ev;
3645 int done, direction, new_ws;
3646 int new_dir;
3647 WDock *clip = scr->clip_icon->dock;
3649 direction = getClipButton(event->xbutton.x, event->xbutton.y);
3651 clip->lclip_button_pushed = direction == CLIP_REWIND;
3652 clip->rclip_button_pushed = direction == CLIP_FORWARD;
3654 wClipIconPaint(scr->clip_icon);
3655 done = 0;
3656 while (!done) {
3657 WMMaskEvent(dpy, ExposureMask | ButtonMotionMask | ButtonReleaseMask | ButtonPressMask, &ev);
3658 switch (ev.type) {
3659 case Expose:
3660 WMHandleEvent(&ev);
3661 break;
3663 case MotionNotify:
3664 new_dir = getClipButton(ev.xmotion.x, ev.xmotion.y);
3665 if (new_dir != direction) {
3666 direction = new_dir;
3667 clip->lclip_button_pushed = direction == CLIP_REWIND;
3668 clip->rclip_button_pushed = direction == CLIP_FORWARD;
3669 wClipIconPaint(scr->clip_icon);
3671 break;
3673 case ButtonPress:
3674 break;
3676 case ButtonRelease:
3677 if (ev.xbutton.button == event->xbutton.button)
3678 done = 1;
3682 clip->lclip_button_pushed = 0;
3683 clip->rclip_button_pushed = 0;
3685 new_ws = wPreferences.ws_advance || (event->xbutton.state & ControlMask);
3687 if (direction == CLIP_FORWARD) {
3688 if (scr->current_workspace < scr->workspace_count - 1)
3689 wWorkspaceChange(scr, scr->current_workspace + 1);
3690 else if (new_ws && scr->current_workspace < MAX_WORKSPACES - 1)
3691 wWorkspaceChange(scr, scr->current_workspace + 1);
3692 else if (wPreferences.ws_cycle)
3693 wWorkspaceChange(scr, 0);
3694 } else if (direction == CLIP_REWIND) {
3695 if (scr->current_workspace > 0)
3696 wWorkspaceChange(scr, scr->current_workspace - 1);
3697 else if (scr->current_workspace == 0 && wPreferences.ws_cycle)
3698 wWorkspaceChange(scr, scr->workspace_count - 1);
3701 wClipIconPaint(scr->clip_icon);
3704 static void iconMouseDown(WObjDescriptor *desc, XEvent *event)
3706 WAppIcon *aicon = desc->parent;
3707 WDock *dock = aicon->dock;
3708 WScreen *scr = aicon->icon->core->screen_ptr;
3710 if (aicon->editing || WCHECK_STATE(WSTATE_MODAL))
3711 return;
3713 scr->last_dock = dock;
3715 if (dock->menu->flags.mapped)
3716 wMenuUnmap(dock->menu);
3718 if (IsDoubleClick(scr, event)) {
3719 /* double-click was not in the main clip icon */
3720 if (dock->type != WM_CLIP || aicon->xindex != 0 || aicon->yindex != 0
3721 || getClipButton(event->xbutton.x, event->xbutton.y) == CLIP_IDLE) {
3722 iconDblClick(desc, event);
3723 return;
3727 if (event->xbutton.button == Button1) {
3728 if (event->xbutton.state & MOD_MASK)
3729 wDockLower(dock);
3730 else
3731 wDockRaise(dock);
3733 if ((event->xbutton.state & ShiftMask) && aicon != scr->clip_icon && dock->type != WM_DOCK) {
3734 wIconSelect(aicon->icon);
3735 return;
3738 if (aicon->yindex == 0 && aicon->xindex == 0) {
3739 if (getClipButton(event->xbutton.x, event->xbutton.y) != CLIP_IDLE
3740 && dock->type == WM_CLIP)
3741 handleClipChangeWorkspace(scr, event);
3742 else
3743 handleDockMove(dock, aicon, event);
3744 } else {
3745 Bool hasMoved = handleIconMove(dock, aicon, event);
3746 if (wPreferences.single_click && !hasMoved)
3747 iconDblClick(desc, event);
3749 } else if (event->xbutton.button == Button2 && dock->type == WM_CLIP && aicon == scr->clip_icon) {
3750 if (!scr->clip_ws_menu) {
3751 scr->clip_ws_menu = wWorkspaceMenuMake(scr, False);
3753 if (scr->clip_ws_menu) {
3754 WMenu *wsMenu = scr->clip_ws_menu;
3755 int xpos;
3757 wWorkspaceMenuUpdate(scr, wsMenu);
3759 xpos = event->xbutton.x_root - wsMenu->frame->core->width / 2 - 1;
3760 if (xpos < 0) {
3761 xpos = 0;
3762 } else if (xpos + wsMenu->frame->core->width > scr->scr_width - 2) {
3763 xpos = scr->scr_width - wsMenu->frame->core->width - 4;
3765 wMenuMapAt(wsMenu, xpos, event->xbutton.y_root + 2, False);
3767 desc = &wsMenu->menu->descriptor;
3768 event->xany.send_event = True;
3769 (*desc->handle_mousedown) (desc, event);
3771 } else if (event->xbutton.button == Button2 && dock->type == WM_CLIP &&
3772 (event->xbutton.state & ShiftMask) && aicon != scr->clip_icon) {
3773 wClipMakeIconOmnipresent(aicon, !aicon->omnipresent);
3774 } else if (event->xbutton.button == Button3) {
3775 if (event->xbutton.send_event &&
3776 XGrabPointer(dpy, aicon->icon->core->window, True, ButtonMotionMask
3777 | ButtonReleaseMask | ButtonPressMask, GrabModeAsync,
3778 GrabModeAsync, None, None, CurrentTime) != GrabSuccess) {
3779 wwarning("pointer grab failed for dockicon menu");
3780 return;
3783 openDockMenu(dock, aicon, event);
3784 } else if (event->xbutton.button == Button2) {
3785 WAppIcon *btn = desc->parent;
3787 if (!btn->launching && (!btn->running || (event->xbutton.state & ControlMask))) {
3788 launchDockedApplication(btn, True);
3793 static void clipEnterNotify(WObjDescriptor *desc, XEvent *event)
3795 WAppIcon *btn = (WAppIcon *) desc->parent;
3796 WDock *dock;
3797 WScreen *scr;
3799 assert(event->type == EnterNotify);
3801 if (desc->parent_type != WCLASS_DOCK_ICON)
3802 return;
3804 scr = btn->icon->core->screen_ptr;
3805 if (!btn->omnipresent)
3806 dock = btn->dock;
3807 else
3808 dock = scr->workspaces[scr->current_workspace]->clip;
3810 if (!dock || dock->type != WM_CLIP)
3811 return;
3813 /* The auto raise/lower code */
3814 if (dock->auto_lower_magic) {
3815 WMDeleteTimerHandler(dock->auto_lower_magic);
3816 dock->auto_lower_magic = NULL;
3818 if (dock->auto_raise_lower && !dock->auto_raise_magic) {
3819 dock->auto_raise_magic = WMAddTimerHandler(AUTO_RAISE_DELAY, clipAutoRaise, (void *)dock);
3822 /* The auto expand/collapse code */
3823 if (dock->auto_collapse_magic) {
3824 WMDeleteTimerHandler(dock->auto_collapse_magic);
3825 dock->auto_collapse_magic = NULL;
3827 if (dock->auto_collapse && !dock->auto_expand_magic) {
3828 dock->auto_expand_magic = WMAddTimerHandler(AUTO_EXPAND_DELAY, clipAutoExpand, (void *)dock);
3832 static void clipLeave(WDock *dock)
3834 XEvent event;
3835 WObjDescriptor *desc = NULL;
3837 if (!dock || dock->type != WM_CLIP)
3838 return;
3840 if (XCheckTypedEvent(dpy, EnterNotify, &event) != False) {
3841 if (XFindContext(dpy, event.xcrossing.window, wWinContext,
3842 (XPointer *) & desc) != XCNOENT
3843 && desc && desc->parent_type == WCLASS_DOCK_ICON
3844 && ((WAppIcon *) desc->parent)->dock && ((WAppIcon *) desc->parent)->dock->type == WM_CLIP) {
3845 /* We didn't left the Clip yet */
3846 XPutBackEvent(dpy, &event);
3847 return;
3850 XPutBackEvent(dpy, &event);
3851 } else {
3852 /* We entered a withdrawn window, so we're still in Clip */
3853 return;
3856 if (dock->auto_raise_magic) {
3857 WMDeleteTimerHandler(dock->auto_raise_magic);
3858 dock->auto_raise_magic = NULL;
3860 if (dock->auto_raise_lower && !dock->auto_lower_magic) {
3861 dock->auto_lower_magic = WMAddTimerHandler(AUTO_LOWER_DELAY, clipAutoLower, (void *)dock);
3864 if (dock->auto_expand_magic) {
3865 WMDeleteTimerHandler(dock->auto_expand_magic);
3866 dock->auto_expand_magic = NULL;
3868 if (dock->auto_collapse && !dock->auto_collapse_magic) {
3869 dock->auto_collapse_magic = WMAddTimerHandler(AUTO_COLLAPSE_DELAY, clipAutoCollapse, (void *)dock);
3873 static void clipLeaveNotify(WObjDescriptor *desc, XEvent *event)
3875 WAppIcon *btn = (WAppIcon *) desc->parent;
3877 assert(event->type == LeaveNotify);
3879 if (desc->parent_type != WCLASS_DOCK_ICON)
3880 return;
3882 clipLeave(btn->dock);
3885 static void clipAutoCollapse(void *cdata)
3887 WDock *dock = (WDock *) cdata;
3889 if (dock->type != WM_CLIP)
3890 return;
3892 if (dock->auto_collapse) {
3893 dock->collapsed = 1;
3894 wDockHideIcons(dock);
3896 dock->auto_collapse_magic = NULL;
3899 static void clipAutoExpand(void *cdata)
3901 WDock *dock = (WDock *) cdata;
3903 if (dock->type != WM_CLIP)
3904 return;
3906 if (dock->auto_collapse) {
3907 dock->collapsed = 0;
3908 wDockShowIcons(dock);
3910 dock->auto_expand_magic = NULL;
3913 static void clipAutoLower(void *cdata)
3915 WDock *dock = (WDock *) cdata;
3917 if (dock->type != WM_CLIP)
3918 return;
3920 if (dock->auto_raise_lower)
3921 wDockLower(dock);
3923 dock->auto_lower_magic = NULL;
3926 static void clipAutoRaise(void *cdata)
3928 WDock *dock = (WDock *) cdata;
3930 if (dock->type != WM_CLIP)
3931 return;
3933 if (dock->auto_raise_lower)
3934 wDockRaise(dock);
3936 dock->auto_raise_magic = NULL;
3939 static Bool iconCanBeOmnipresent(WAppIcon *aicon)
3941 WScreen *scr = aicon->icon->core->screen_ptr;
3942 WDock *clip;
3943 WAppIcon *btn;
3944 int i, j;
3946 for (i = 0; i < scr->workspace_count; i++) {
3947 clip = scr->workspaces[i]->clip;
3949 if (clip == aicon->dock)
3950 continue;
3952 if (clip->icon_count + scr->global_icon_count >= clip->max_icons)
3953 return False; /* Clip is full in some workspace */
3955 for (j = 0; j < clip->max_icons; j++) {
3956 btn = clip->icon_array[j];
3957 if (btn && btn->xindex == aicon->xindex && btn->yindex == aicon->yindex)
3958 return False;
3962 return True;
3965 int wClipMakeIconOmnipresent(WAppIcon *aicon, int omnipresent)
3967 WScreen *scr = aicon->icon->core->screen_ptr;
3968 WAppIconChain *new_entry, *tmp, *tmp1;
3969 int status = WO_SUCCESS;
3971 if ((scr->dock && aicon->dock == scr->dock) || aicon == scr->clip_icon) {
3972 return WO_NOT_APPLICABLE;
3975 if (aicon->omnipresent == omnipresent)
3976 return WO_SUCCESS;
3978 if (omnipresent) {
3979 if (iconCanBeOmnipresent(aicon)) {
3980 aicon->omnipresent = 1;
3981 new_entry = wmalloc(sizeof(WAppIconChain));
3982 new_entry->aicon = aicon;
3983 new_entry->next = scr->global_icons;
3984 scr->global_icons = new_entry;
3985 scr->global_icon_count++;
3986 } else {
3987 aicon->omnipresent = 0;
3988 status = WO_FAILED;
3990 } else {
3991 aicon->omnipresent = 0;
3992 if (aicon == scr->global_icons->aicon) {
3993 tmp = scr->global_icons->next;
3994 wfree(scr->global_icons);
3995 scr->global_icons = tmp;
3996 scr->global_icon_count--;
3997 } else {
3998 tmp = scr->global_icons;
3999 while (tmp->next) {
4000 if (tmp->next->aicon == aicon) {
4001 tmp1 = tmp->next->next;
4002 wfree(tmp->next);
4003 tmp->next = tmp1;
4004 scr->global_icon_count--;
4005 break;
4007 tmp = tmp->next;
4012 wAppIconPaint(aicon);
4014 return status;