Address unprototyped call in dock.c
[wmaker-crm.git] / src / dock.c
blob193bc0120ed97e09f0a6ee085c51a14be0d64f1a
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 "dockedapp.h"
47 #include "dialog.h"
48 #include "funcs.h"
49 #include "properties.h"
50 #include "menu.h"
51 #include "client.h"
52 #include "defaults.h"
53 #include "workspace.h"
54 #include "framewin.h"
55 #include "superfluous.h"
56 #include "xinerama.h"
58 /**** Local variables ****/
59 #define CLIP_REWIND 1
60 #define CLIP_IDLE 0
61 #define CLIP_FORWARD 2
63 /**** Global variables ****/
64 extern Cursor wCursor[WCUR_LAST];
65 extern WPreferences wPreferences;
66 extern XContext wWinContext;
67 extern void appIconMouseDown(WObjDescriptor *desc, XEvent *event);
69 #define MOD_MASK wPreferences.modifier_mask
70 #define ICON_SIZE wPreferences.icon_size
72 /***** Local variables ****/
74 static WMPropList *dCommand = NULL;
75 static WMPropList *dPasteCommand = NULL;
76 #ifdef XDND /* XXX was OFFIX */
77 static WMPropList *dDropCommand = NULL;
78 #endif
79 static WMPropList *dAutoLaunch, *dLock;
80 static WMPropList *dName, *dForced, *dBuggyApplication, *dYes, *dNo;
81 static WMPropList *dHost, *dDock, *dClip;
82 static WMPropList *dAutoAttractIcons;
84 static WMPropList *dPosition, *dApplications, *dLowered, *dCollapsed;
86 static WMPropList *dAutoCollapse, *dAutoRaiseLower, *dOmnipresent;
88 static void dockIconPaint(WAppIcon *btn);
90 static void iconMouseDown(WObjDescriptor *desc, XEvent *event);
92 static pid_t execCommand(WAppIcon *btn, char *command, WSavedState *state);
94 static void trackDeadProcess(pid_t pid, unsigned char status, WDock *dock);
96 static int getClipButton(int px, int py);
98 static void toggleLowered(WDock *dock);
100 static void toggleCollapsed(WDock *dock);
102 static void clipIconExpose(WObjDescriptor *desc, XEvent *event);
104 static void clipLeave(WDock *dock);
106 static void handleClipChangeWorkspace(WScreen *scr, XEvent *event);
108 static Bool moveIconBetweenDocks(WDock *src, WDock *dest, WAppIcon *icon, int x, int y);
110 static void clipEnterNotify(WObjDescriptor *desc, XEvent *event);
111 static void clipLeaveNotify(WObjDescriptor *desc, XEvent *event);
112 static void clipAutoCollapse(void *cdata);
113 static void clipAutoExpand(void *cdata);
114 static void launchDockedApplication(WAppIcon *btn, Bool withSelection);
116 static void clipAutoLower(void *cdata);
117 static void clipAutoRaise(void *cdata);
118 static void reattachIcon(WDock *dock, WAppIcon *icon, int x, int y);
119 static WAppIcon *mainIconCreate(WScreen *scr, int type);
121 static void make_keys(void)
123 if (dCommand != NULL)
124 return;
126 dCommand = WMRetainPropList(WMCreatePLString("Command"));
127 dPasteCommand = WMRetainPropList(WMCreatePLString("PasteCommand"));
128 #ifdef XDND
129 dDropCommand = WMRetainPropList(WMCreatePLString("DropCommand"));
130 #endif
131 dLock = WMRetainPropList(WMCreatePLString("Lock"));
132 dAutoLaunch = WMRetainPropList(WMCreatePLString("AutoLaunch"));
133 dName = WMRetainPropList(WMCreatePLString("Name"));
134 dForced = WMRetainPropList(WMCreatePLString("Forced"));
135 dBuggyApplication = WMRetainPropList(WMCreatePLString("BuggyApplication"));
136 dYes = WMRetainPropList(WMCreatePLString("Yes"));
137 dNo = WMRetainPropList(WMCreatePLString("No"));
138 dHost = WMRetainPropList(WMCreatePLString("Host"));
140 dPosition = WMCreatePLString("Position");
141 dApplications = WMCreatePLString("Applications");
142 dLowered = WMCreatePLString("Lowered");
143 dCollapsed = WMCreatePLString("Collapsed");
144 dAutoCollapse = WMCreatePLString("AutoCollapse");
145 dAutoRaiseLower = WMCreatePLString("AutoRaiseLower");
146 dAutoAttractIcons = WMCreatePLString("AutoAttractIcons");
148 dOmnipresent = WMCreatePLString("Omnipresent");
150 dDock = WMCreatePLString("Dock");
151 dClip = WMCreatePLString("Clip");
154 static void renameCallback(WMenu *menu, WMenuEntry *entry)
156 WDock *dock = entry->clientdata;
157 char buffer[128];
158 int wspace;
159 char *name;
161 assert(entry->clientdata != NULL);
163 wspace = dock->screen_ptr->current_workspace;
165 name = wstrdup(dock->screen_ptr->workspaces[wspace]->name);
167 snprintf(buffer, sizeof(buffer), _("Type the name for workspace %i:"), wspace + 1);
168 if (wInputDialog(dock->screen_ptr, _("Rename Workspace"), buffer, &name)) {
169 wWorkspaceRename(dock->screen_ptr, wspace, name);
172 wfree(name);
175 static void toggleLoweredCallback(WMenu *menu, WMenuEntry *entry)
177 assert(entry->clientdata != NULL);
179 toggleLowered(entry->clientdata);
181 entry->flags.indicator_on = !(((WDock *) entry->clientdata)->lowered);
183 wMenuPaint(menu);
186 static int matchWindow(const void *item, const void *cdata)
188 return (((WFakeGroupLeader *) item)->leader == (Window) cdata);
191 static void killCallback(WMenu *menu, WMenuEntry *entry)
193 WScreen *scr = menu->menu->screen_ptr;
194 WAppIcon *icon;
195 WFakeGroupLeader *fPtr;
196 char *buffer, *shortname, **argv;
197 char *basename(const char *shortname);
198 int argc;
200 if (!WCHECK_STATE(WSTATE_NORMAL))
201 return;
203 assert(entry->clientdata != NULL);
205 icon = (WAppIcon *) entry->clientdata;
207 icon->editing = 1;
209 WCHANGE_STATE(WSTATE_MODAL);
211 /* strip away dir names */
212 shortname = basename(icon->command);
213 /* separate out command options */
214 wtokensplit(shortname, &argv, &argc);
216 buffer = wstrconcat(argv[0],
217 _(" will be forcibly closed.\n"
218 "Any unsaved changes will be lost.\n" "Please confirm."));
220 if (icon->icon && icon->icon->owner) {
221 fPtr = icon->icon->owner->fake_group;
222 } else {
223 /* is this really necessary? can we kill a non-running dock icon? */
224 Window win = icon->main_window;
225 int index;
227 index = WMFindInArray(scr->fakeGroupLeaders, matchWindow, (void *)win);
228 if (index != WANotFound)
229 fPtr = WMGetFromArray(scr->fakeGroupLeaders, index);
230 else
231 fPtr = NULL;
234 if (wPreferences.dont_confirm_kill
235 || wMessageDialog(menu->frame->screen_ptr, _("Kill Application"),
236 buffer, _("Yes"), _("No"), NULL) == WAPRDefault) {
237 if (fPtr != NULL) {
238 WWindow *wwin, *twin;
240 wwin = scr->focused_window;
241 while (wwin) {
242 twin = wwin->prev;
243 if (wwin->fake_group == fPtr) {
244 wClientKill(wwin);
246 wwin = twin;
248 } else if (icon->icon && icon->icon->owner) {
249 wClientKill(icon->icon->owner);
253 wfree(buffer);
254 wtokenfree(argv, argc);
256 icon->editing = 0;
258 WCHANGE_STATE(WSTATE_NORMAL);
261 /* TODO: replace this function with a member of the dock struct */
262 static int numberOfSelectedIcons(WDock *dock)
264 WAppIcon *aicon;
265 int i, n;
267 n = 0;
268 for (i = 1; i < dock->max_icons; i++) {
269 aicon = dock->icon_array[i];
270 if (aicon && aicon->icon->selected) {
271 n++;
275 return n;
278 static WMArray *getSelected(WDock *dock)
280 WMArray *ret = WMCreateArray(8);
281 WAppIcon *btn;
282 int i;
284 for (i = 1; i < dock->max_icons; i++) {
285 btn = dock->icon_array[i];
286 if (btn && btn->icon->selected) {
287 WMAddToArray(ret, btn);
291 return ret;
294 static void paintClipButtons(WAppIcon *clipIcon, Bool lpushed, Bool rpushed)
296 Window win = clipIcon->icon->core->window;
297 WScreen *scr = clipIcon->icon->core->screen_ptr;
298 XPoint p[4];
299 int pt = CLIP_BUTTON_SIZE * ICON_SIZE / 64;
300 int tp = ICON_SIZE - pt;
301 int as = pt - 15; /* 15 = 5+5+5 */
302 GC gc = scr->draw_gc; /* maybe use WMColorGC() instead here? */
303 WMColor *color;
305 color = scr->clip_title_color[CLIP_NORMAL];
307 XSetForeground(dpy, gc, WMColorPixel(color));
309 if (rpushed) {
310 p[0].x = tp + 1;
311 p[0].y = 1;
312 p[1].x = ICON_SIZE - 2;
313 p[1].y = 1;
314 p[2].x = ICON_SIZE - 2;
315 p[2].y = pt - 1;
316 } else if (lpushed) {
317 p[0].x = 1;
318 p[0].y = tp;
319 p[1].x = pt;
320 p[1].y = ICON_SIZE - 2;
321 p[2].x = 1;
322 p[2].y = ICON_SIZE - 2;
324 if (lpushed || rpushed) {
325 XSetForeground(dpy, scr->draw_gc, scr->white_pixel);
326 XFillPolygon(dpy, win, scr->draw_gc, p, 3, Convex, CoordModeOrigin);
327 XSetForeground(dpy, scr->draw_gc, scr->black_pixel);
330 /* top right arrow */
331 p[0].x = p[3].x = ICON_SIZE - 5 - as;
332 p[0].y = p[3].y = 5;
333 p[1].x = ICON_SIZE - 6;
334 p[1].y = 5;
335 p[2].x = ICON_SIZE - 6;
336 p[2].y = 4 + as;
337 if (rpushed) {
338 XFillPolygon(dpy, win, scr->draw_gc, p, 3, Convex, CoordModeOrigin);
339 XDrawLines(dpy, win, scr->draw_gc, p, 4, CoordModeOrigin);
340 } else {
341 XFillPolygon(dpy, win, gc, p, 3, Convex, CoordModeOrigin);
342 XDrawLines(dpy, win, gc, p, 4, CoordModeOrigin);
345 /* bottom left arrow */
346 p[0].x = p[3].x = 5;
347 p[0].y = p[3].y = ICON_SIZE - 5 - as;
348 p[1].x = 5;
349 p[1].y = ICON_SIZE - 6;
350 p[2].x = 4 + as;
351 p[2].y = ICON_SIZE - 6;
352 if (lpushed) {
353 XFillPolygon(dpy, win, scr->draw_gc, p, 3, Convex, CoordModeOrigin);
354 XDrawLines(dpy, win, scr->draw_gc, p, 4, CoordModeOrigin);
355 } else {
356 XFillPolygon(dpy, win, gc, p, 3, Convex, CoordModeOrigin);
357 XDrawLines(dpy, win, gc, p, 4, CoordModeOrigin);
361 RImage *wClipMakeTile(WScreen *scr, RImage *normalTile)
363 RImage *tile = RCloneImage(normalTile);
364 RColor black;
365 RColor dark;
366 RColor light;
367 int pt, tp;
368 int as;
370 pt = CLIP_BUTTON_SIZE * wPreferences.icon_size / 64;
371 tp = wPreferences.icon_size - 1 - pt;
372 as = pt - 15;
374 black.alpha = 255;
375 black.red = black.green = black.blue = 0;
377 dark.alpha = 0;
378 dark.red = dark.green = dark.blue = 60;
380 light.alpha = 0;
381 light.red = light.green = light.blue = 80;
383 /* top right */
384 ROperateLine(tile, RSubtractOperation, tp, 0, wPreferences.icon_size - 2, pt - 1, &dark);
385 RDrawLine(tile, tp - 1, 0, wPreferences.icon_size - 1, pt + 1, &black);
386 ROperateLine(tile, RAddOperation, tp, 2, wPreferences.icon_size - 3, pt, &light);
388 /* arrow bevel */
389 ROperateLine(tile, RSubtractOperation, ICON_SIZE - 7 - as, 4, ICON_SIZE - 5, 4, &dark);
390 ROperateLine(tile, RSubtractOperation, ICON_SIZE - 6 - as, 5, ICON_SIZE - 5, 6 + as, &dark);
391 ROperateLine(tile, RAddOperation, ICON_SIZE - 5, 4, ICON_SIZE - 5, 6 + as, &light);
393 /* bottom left */
394 ROperateLine(tile, RAddOperation, 2, tp + 2, pt - 2, wPreferences.icon_size - 3, &dark);
395 RDrawLine(tile, 0, tp - 1, pt + 1, wPreferences.icon_size - 1, &black);
396 ROperateLine(tile, RSubtractOperation, 0, tp - 2, pt + 1, wPreferences.icon_size - 2, &light);
398 /* arrow bevel */
399 ROperateLine(tile, RSubtractOperation, 4, ICON_SIZE - 7 - as, 4, ICON_SIZE - 5, &dark);
400 ROperateLine(tile, RSubtractOperation, 5, ICON_SIZE - 6 - as, 6 + as, ICON_SIZE - 5, &dark);
401 ROperateLine(tile, RAddOperation, 4, ICON_SIZE - 5, 6 + as, ICON_SIZE - 5, &light);
403 return tile;
406 static void omnipresentCallback(WMenu *menu, WMenuEntry *entry)
408 WAppIcon *clickedIcon = entry->clientdata;
409 WAppIcon *aicon;
410 WDock *dock;
411 WMArray *selectedIcons;
412 WMArrayIterator iter;
413 int failed;
415 assert(entry->clientdata != NULL);
417 dock = clickedIcon->dock;
419 selectedIcons = getSelected(dock);
421 if (!WMGetArrayItemCount(selectedIcons))
422 WMAddToArray(selectedIcons, clickedIcon);
424 failed = 0;
425 WM_ITERATE_ARRAY(selectedIcons, aicon, iter) {
426 if (wClipMakeIconOmnipresent(aicon, !aicon->omnipresent) == WO_FAILED)
427 failed++;
428 else if (aicon->icon->selected)
429 wIconSelect(aicon->icon);
431 WMFreeArray(selectedIcons);
433 if (failed > 1) {
434 wMessageDialog(dock->screen_ptr, _("Warning"),
435 _("Some icons cannot be made omnipresent. "
436 "Please make sure that no other icon is "
437 "docked in the same positions on the other "
438 "workspaces and the Clip is not full in "
439 "some workspace."), _("OK"), NULL, NULL);
440 } else if (failed == 1) {
441 wMessageDialog(dock->screen_ptr, _("Warning"),
442 _("Icon cannot be made omnipresent. "
443 "Please make sure that no other icon is "
444 "docked in the same position on the other "
445 "workspaces and the Clip is not full in "
446 "some workspace."), _("OK"), NULL, NULL);
450 static void removeIconsCallback(WMenu *menu, WMenuEntry *entry)
452 WAppIcon *clickedIcon = (WAppIcon *) entry->clientdata;
453 WDock *dock;
454 WAppIcon *aicon;
455 WMArray *selectedIcons;
456 int keepit;
457 WMArrayIterator it;
459 assert(clickedIcon != NULL);
461 dock = clickedIcon->dock;
463 selectedIcons = getSelected(dock);
465 if (WMGetArrayItemCount(selectedIcons)) {
466 if (wMessageDialog(dock->screen_ptr, _("Workspace Clip"),
467 _("All selected icons will be removed!"),
468 _("OK"), _("Cancel"), NULL) != WAPRDefault) {
469 WMFreeArray(selectedIcons);
470 return;
472 } else {
473 if (clickedIcon->xindex == 0 && clickedIcon->yindex == 0) {
474 WMFreeArray(selectedIcons);
475 return;
477 WMAddToArray(selectedIcons, clickedIcon);
480 WM_ITERATE_ARRAY(selectedIcons, aicon, it) {
481 keepit = aicon->running && wApplicationOf(aicon->main_window);
482 wDockDetach(dock, aicon);
483 if (keepit) {
484 /* XXX: can: aicon->icon == NULL ? */
485 PlaceIcon(dock->screen_ptr, &aicon->x_pos, &aicon->y_pos,
486 wGetHeadForWindow(aicon->icon->owner));
487 XMoveWindow(dpy, aicon->icon->core->window, aicon->x_pos, aicon->y_pos);
488 if (!dock->mapped || dock->collapsed)
489 XMapWindow(dpy, aicon->icon->core->window);
492 WMFreeArray(selectedIcons);
494 if (wPreferences.auto_arrange_icons)
495 wArrangeIcons(dock->screen_ptr, True);
498 static void keepIconsCallback(WMenu *menu, WMenuEntry *entry)
500 WAppIcon *clickedIcon = (WAppIcon *) entry->clientdata;
501 WDock *dock;
502 WAppIcon *aicon;
503 WMArray *selectedIcons;
504 WMArrayIterator it;
506 assert(clickedIcon != NULL);
507 dock = clickedIcon->dock;
509 selectedIcons = getSelected(dock);
511 if (!WMGetArrayItemCount(selectedIcons)
512 && clickedIcon != dock->screen_ptr->clip_icon) {
513 char *command = NULL;
515 if (!clickedIcon->command && !clickedIcon->editing) {
516 clickedIcon->editing = 1;
517 if (wInputDialog(dock->screen_ptr, _("Keep Icon"),
518 _("Type the command used to launch the application"), &command)) {
519 if (command && (command[0] == 0 || (command[0] == '-' && command[1] == 0))) {
520 wfree(command);
521 command = NULL;
523 clickedIcon->command = command;
524 clickedIcon->editing = 0;
525 } else {
526 clickedIcon->editing = 0;
527 if (command)
528 wfree(command);
529 WMFreeArray(selectedIcons);
530 return;
534 WMAddToArray(selectedIcons, clickedIcon);
537 WM_ITERATE_ARRAY(selectedIcons, aicon, it) {
538 if (aicon->icon->selected)
539 wIconSelect(aicon->icon);
540 if (aicon && aicon->attracted && aicon->command) {
541 aicon->attracted = 0;
542 if (aicon->icon->shadowed) {
543 aicon->icon->shadowed = 0;
544 aicon->icon->force_paint = 1;
545 wAppIconPaint(aicon);
548 save_appicon(aicon, True);
550 WMFreeArray(selectedIcons);
553 static void toggleAutoAttractCallback(WMenu *menu, WMenuEntry *entry)
555 WDock *dock = (WDock *) entry->clientdata;
557 assert(entry->clientdata != NULL);
559 dock->attract_icons = !dock->attract_icons;
560 /*if (!dock->attract_icons)
561 dock->keep_attracted = 0; */
563 entry->flags.indicator_on = dock->attract_icons;
565 wMenuPaint(menu);
568 static void selectCallback(WMenu *menu, WMenuEntry *entry)
570 WAppIcon *icon = (WAppIcon *) entry->clientdata;
572 assert(icon != NULL);
574 wIconSelect(icon->icon);
576 wMenuPaint(menu);
579 static void colectIconsCallback(WMenu *menu, WMenuEntry *entry)
581 WAppIcon *clickedIcon = (WAppIcon *) entry->clientdata;
582 WDock *clip;
583 WAppIcon *aicon;
584 int x, y, x_pos, y_pos;
586 assert(entry->clientdata != NULL);
587 clip = clickedIcon->dock;
589 aicon = clip->screen_ptr->app_icon_list;
591 while (aicon) {
592 if (!aicon->docked && wDockFindFreeSlot(clip, &x, &y)) {
593 x_pos = clip->x_pos + x * ICON_SIZE;
594 y_pos = clip->y_pos + y * ICON_SIZE;
595 if (aicon->x_pos != x_pos || aicon->y_pos != y_pos) {
596 #ifdef ANIMATIONS
597 if (wPreferences.no_animations) {
598 XMoveWindow(dpy, aicon->icon->core->window, x_pos, y_pos);
599 } else {
600 SlideWindow(aicon->icon->core->window,
601 aicon->x_pos, aicon->y_pos, x_pos, y_pos);
603 #else
604 XMoveWindow(dpy, aicon->icon->core->window, x_pos, y_pos);
605 #endif /* ANIMATIONS */
607 aicon->attracted = 1;
608 if (!aicon->icon->shadowed) {
609 aicon->icon->shadowed = 1;
610 aicon->icon->force_paint = 1;
611 /* We don't do an wAppIconPaint() here because it's in
612 * wDockAttachIcon(). -Dan
615 wDockAttachIcon(clip, aicon, x, y);
616 if (clip->collapsed || !clip->mapped)
617 XUnmapWindow(dpy, aicon->icon->core->window);
619 aicon = aicon->next;
623 static void selectIconsCallback(WMenu *menu, WMenuEntry *entry)
625 WAppIcon *clickedIcon = (WAppIcon *) entry->clientdata;
626 WDock *dock;
627 WMArray *selectedIcons;
628 WMArrayIterator iter;
629 WAppIcon *btn;
630 int i;
632 assert(clickedIcon != NULL);
633 dock = clickedIcon->dock;
635 selectedIcons = getSelected(dock);
637 if (!WMGetArrayItemCount(selectedIcons)) {
638 for (i = 1; i < dock->max_icons; i++) {
639 btn = dock->icon_array[i];
640 if (btn && !btn->icon->selected) {
641 wIconSelect(btn->icon);
644 } else {
645 WM_ITERATE_ARRAY(selectedIcons, btn, iter) {
646 wIconSelect(btn->icon);
649 WMFreeArray(selectedIcons);
651 wMenuPaint(menu);
654 static void toggleCollapsedCallback(WMenu *menu, WMenuEntry *entry)
656 assert(entry->clientdata != NULL);
658 toggleCollapsed(entry->clientdata);
660 entry->flags.indicator_on = ((WDock *) entry->clientdata)->collapsed;
662 wMenuPaint(menu);
665 static void toggleAutoCollapseCallback(WMenu *menu, WMenuEntry *entry)
667 WDock *dock;
668 assert(entry->clientdata != NULL);
670 dock = (WDock *) entry->clientdata;
672 dock->auto_collapse = !dock->auto_collapse;
674 entry->flags.indicator_on = ((WDock *) entry->clientdata)->auto_collapse;
676 wMenuPaint(menu);
679 static void toggleAutoRaiseLowerCallback(WMenu *menu, WMenuEntry *entry)
681 WDock *dock;
682 assert(entry->clientdata != NULL);
684 dock = (WDock *) entry->clientdata;
686 dock->auto_raise_lower = !dock->auto_raise_lower;
688 entry->flags.indicator_on = ((WDock *) entry->clientdata)->auto_raise_lower;
690 wMenuPaint(menu);
693 static void launchCallback(WMenu *menu, WMenuEntry *entry)
695 WAppIcon *btn = (WAppIcon *) entry->clientdata;
697 launchDockedApplication(btn, False);
700 static void settingsCallback(WMenu *menu, WMenuEntry *entry)
702 WAppIcon *btn = (WAppIcon *) entry->clientdata;
704 if (btn->editing)
705 return;
706 ShowDockAppSettingsPanel(btn);
709 static void hideCallback(WMenu *menu, WMenuEntry *entry)
711 WApplication *wapp;
712 WAppIcon *btn = (WAppIcon *) entry->clientdata;
714 wapp = wApplicationOf(btn->icon->owner->main_window);
716 if (wapp->flags.hidden) {
717 wWorkspaceChange(btn->icon->core->screen_ptr, wapp->last_workspace);
718 wUnhideApplication(wapp, False, False);
719 } else {
720 wHideApplication(wapp);
724 static void unhideHereCallback(WMenu *menu, WMenuEntry *entry)
726 WApplication *wapp;
727 WAppIcon *btn = (WAppIcon *) entry->clientdata;
729 wapp = wApplicationOf(btn->icon->owner->main_window);
731 wUnhideApplication(wapp, False, True);
734 static WAppIcon *mainIconCreate(WScreen *scr, int type)
736 WAppIcon *btn;
737 int x_pos;
739 if (type == WM_CLIP) {
740 if (scr->clip_icon)
741 return scr->clip_icon;
742 btn = wAppIconCreateForDock(scr, NULL, "Logo", "WMClip", TILE_CLIP);
743 btn->icon->core->descriptor.handle_expose = clipIconExpose;
744 btn->icon->core->descriptor.handle_enternotify = clipEnterNotify;
745 btn->icon->core->descriptor.handle_leavenotify = clipLeaveNotify;
746 /*x_pos = scr->scr_width - ICON_SIZE*2 - DOCK_EXTRA_SPACE; */
747 x_pos = 0;
748 } else {
749 btn = wAppIconCreateForDock(scr, NULL, "Logo", "WMDock", TILE_NORMAL);
750 x_pos = scr->scr_width - ICON_SIZE - DOCK_EXTRA_SPACE;
753 btn->xindex = 0;
754 btn->yindex = 0;
756 btn->icon->core->descriptor.handle_mousedown = iconMouseDown;
757 btn->icon->core->descriptor.parent_type = WCLASS_DOCK_ICON;
758 btn->icon->core->descriptor.parent = btn;
759 /*ChangeStackingLevel(btn->icon->core, WMDockLevel); */
760 XMapWindow(dpy, btn->icon->core->window);
761 btn->x_pos = x_pos;
762 btn->y_pos = 0;
763 btn->docked = 1;
764 if (type == WM_CLIP)
765 scr->clip_icon = btn;
767 return btn;
770 static void switchWSCommand(WMenu *menu, WMenuEntry *entry)
772 WAppIcon *btn, *icon = (WAppIcon *) entry->clientdata;
773 WScreen *scr = icon->icon->core->screen_ptr;
774 WDock *src, *dest;
775 WMArray *selectedIcons;
776 int x, y;
778 if (entry->order == scr->current_workspace)
779 return;
780 src = icon->dock;
781 dest = scr->workspaces[entry->order]->clip;
783 selectedIcons = getSelected(src);
785 if (WMGetArrayItemCount(selectedIcons)) {
786 WMArrayIterator iter;
788 WM_ITERATE_ARRAY(selectedIcons, btn, iter) {
789 if (wDockFindFreeSlot(dest, &x, &y)) {
790 moveIconBetweenDocks(src, dest, btn, x, y);
791 XUnmapWindow(dpy, btn->icon->core->window);
794 } else if (icon != scr->clip_icon) {
795 if (wDockFindFreeSlot(dest, &x, &y)) {
796 moveIconBetweenDocks(src, dest, icon, x, y);
797 XUnmapWindow(dpy, icon->icon->core->window);
800 WMFreeArray(selectedIcons);
803 static void launchDockedApplication(WAppIcon *btn, Bool withSelection)
805 WScreen *scr = btn->icon->core->screen_ptr;
807 if (!btn->launching &&
808 ((!withSelection && btn->command != NULL) || (withSelection && btn->paste_command != NULL))) {
809 if (!btn->forced_dock) {
810 btn->relaunching = btn->running;
811 btn->running = 1;
813 if (btn->wm_instance || btn->wm_class) {
814 WWindowAttributes attr;
815 memset(&attr, 0, sizeof(WWindowAttributes));
816 wDefaultFillAttributes(scr, btn->wm_instance, btn->wm_class, &attr, NULL, True);
818 if (!attr.no_appicon && !btn->buggy_app)
819 btn->launching = 1;
820 else
821 btn->running = 0;
823 btn->drop_launch = 0;
824 btn->paste_launch = withSelection;
825 scr->last_dock = btn->dock;
826 btn->pid = execCommand(btn, (withSelection ? btn->paste_command : btn->command), NULL);
827 if (btn->pid > 0) {
828 if (btn->buggy_app) {
829 /* give feedback that the app was launched */
830 btn->launching = 1;
831 dockIconPaint(btn);
832 btn->launching = 0;
833 WMAddTimerHandler(200, (WMCallback *) dockIconPaint, btn);
834 } else {
835 dockIconPaint(btn);
837 } else {
838 wwarning(_("could not launch application %s"), btn->command);
839 btn->launching = 0;
840 if (!btn->relaunching) {
841 btn->running = 0;
847 static void updateWorkspaceMenu(WMenu *menu, WAppIcon *icon)
849 WScreen *scr = menu->frame->screen_ptr;
850 char title[MAX_WORKSPACENAME_WIDTH + 1];
851 int i;
853 if (!menu || !icon)
854 return;
856 for (i = 0; i < scr->workspace_count; i++) {
857 if (i < menu->entry_no) {
858 if (strcmp(menu->entries[i]->text, scr->workspaces[i]->name) != 0) {
859 wfree(menu->entries[i]->text);
860 strcpy(title, scr->workspaces[i]->name);
861 menu->entries[i]->text = wstrdup(title);
862 menu->flags.realized = 0;
864 menu->entries[i]->clientdata = (void *)icon;
865 } else {
866 strcpy(title, scr->workspaces[i]->name);
868 wMenuAddCallback(menu, title, switchWSCommand, (void *)icon);
870 menu->flags.realized = 0;
872 if (i == scr->current_workspace) {
873 wMenuSetEnabled(menu, i, False);
874 } else {
875 wMenuSetEnabled(menu, i, True);
879 if (!menu->flags.realized)
880 wMenuRealize(menu);
883 static WMenu *makeWorkspaceMenu(WScreen *scr)
885 WMenu *menu;
887 menu = wMenuCreate(scr, NULL, False);
888 if (!menu)
889 wwarning(_("could not create workspace submenu for Clip menu"));
891 wMenuAddCallback(menu, "", switchWSCommand, (void *)scr->clip_icon);
893 menu->flags.realized = 0;
894 wMenuRealize(menu);
896 return menu;
899 static void updateClipOptionsMenu(WMenu *menu, WDock *dock)
901 WMenuEntry *entry;
902 int index = 0;
904 if (!menu || !dock)
905 return;
907 /* keep on top */
908 entry = menu->entries[index];
909 entry->flags.indicator_on = !dock->lowered;
910 entry->clientdata = dock;
912 /* collapsed */
913 entry = menu->entries[++index];
914 entry->flags.indicator_on = dock->collapsed;
915 entry->clientdata = dock;
917 /* auto-collapse */
918 entry = menu->entries[++index];
919 entry->flags.indicator_on = dock->auto_collapse;
920 entry->clientdata = dock;
922 /* auto-raise/lower */
923 entry = menu->entries[++index];
924 entry->flags.indicator_on = dock->auto_raise_lower;
925 entry->clientdata = dock;
926 wMenuSetEnabled(menu, index, dock->lowered);
928 /* attract icons */
929 entry = menu->entries[++index];
930 entry->flags.indicator_on = dock->attract_icons;
931 entry->clientdata = dock;
933 menu->flags.realized = 0;
934 wMenuRealize(menu);
937 static WMenu *makeClipOptionsMenu(WScreen *scr)
939 WMenu *menu;
940 WMenuEntry *entry;
942 menu = wMenuCreate(scr, NULL, False);
943 if (!menu) {
944 wwarning(_("could not create options submenu for Clip menu"));
945 return NULL;
948 entry = wMenuAddCallback(menu, _("Keep on Top"), toggleLoweredCallback, NULL);
949 entry->flags.indicator = 1;
950 entry->flags.indicator_on = 1;
951 entry->flags.indicator_type = MI_CHECK;
953 entry = wMenuAddCallback(menu, _("Collapsed"), toggleCollapsedCallback, NULL);
954 entry->flags.indicator = 1;
955 entry->flags.indicator_on = 1;
956 entry->flags.indicator_type = MI_CHECK;
958 entry = wMenuAddCallback(menu, _("Autocollapse"), toggleAutoCollapseCallback, NULL);
959 entry->flags.indicator = 1;
960 entry->flags.indicator_on = 1;
961 entry->flags.indicator_type = MI_CHECK;
963 entry = wMenuAddCallback(menu, _("Autoraise"), toggleAutoRaiseLowerCallback, NULL);
964 entry->flags.indicator = 1;
965 entry->flags.indicator_on = 1;
966 entry->flags.indicator_type = MI_CHECK;
968 entry = wMenuAddCallback(menu, _("Autoattract Icons"), toggleAutoAttractCallback, NULL);
969 entry->flags.indicator = 1;
970 entry->flags.indicator_on = 1;
971 entry->flags.indicator_type = MI_CHECK;
973 menu->flags.realized = 0;
974 wMenuRealize(menu);
976 return menu;
979 static WMenu *dockMenuCreate(WScreen *scr, int type)
981 WMenu *menu;
982 WMenuEntry *entry;
984 if (type == WM_CLIP && scr->clip_menu)
985 return scr->clip_menu;
987 menu = wMenuCreate(scr, NULL, False);
988 if (type != WM_CLIP) {
989 entry = wMenuAddCallback(menu, _("Keep on Top"), toggleLoweredCallback, NULL);
990 entry->flags.indicator = 1;
991 entry->flags.indicator_on = 1;
992 entry->flags.indicator_type = MI_CHECK;
993 } else {
994 entry = wMenuAddCallback(menu, _("Clip Options"), NULL, NULL);
995 scr->clip_options = makeClipOptionsMenu(scr);
996 if (scr->clip_options)
997 wMenuEntrySetCascade(menu, entry, scr->clip_options);
999 entry = wMenuAddCallback(menu, _("Rename Workspace"), renameCallback, NULL);
1000 wfree(entry->text);
1001 entry->text = _("Rename Workspace");
1003 entry = wMenuAddCallback(menu, _("Selected"), selectCallback, NULL);
1004 entry->flags.indicator = 1;
1005 entry->flags.indicator_on = 1;
1006 entry->flags.indicator_type = MI_CHECK;
1008 entry = wMenuAddCallback(menu, _("Select All Icons"), selectIconsCallback, NULL);
1009 wfree(entry->text);
1010 entry->text = _("Select All Icons");
1012 entry = wMenuAddCallback(menu, _("Keep Icon"), keepIconsCallback, NULL);
1013 wfree(entry->text);
1014 entry->text = _("Keep Icon");
1016 entry = wMenuAddCallback(menu, _("Move Icon To"), NULL, NULL);
1017 wfree(entry->text);
1018 entry->text = _("Move Icon To");
1019 scr->clip_submenu = makeWorkspaceMenu(scr);
1020 if (scr->clip_submenu)
1021 wMenuEntrySetCascade(menu, entry, scr->clip_submenu);
1023 entry = wMenuAddCallback(menu, _("Remove Icon"), removeIconsCallback, NULL);
1024 wfree(entry->text);
1025 entry->text = _("Remove Icon");
1027 wMenuAddCallback(menu, _("Attract Icons"), colectIconsCallback, NULL);
1030 wMenuAddCallback(menu, _("Launch"), launchCallback, NULL);
1032 wMenuAddCallback(menu, _("Unhide Here"), unhideHereCallback, NULL);
1034 entry = wMenuAddCallback(menu, _("Hide"), hideCallback, NULL);
1035 wfree(entry->text);
1036 entry->text = _("Hide");
1038 wMenuAddCallback(menu, _("Settings..."), settingsCallback, NULL);
1040 wMenuAddCallback(menu, _("Kill"), killCallback, NULL);
1042 if (type == WM_CLIP)
1043 scr->clip_menu = menu;
1045 return menu;
1048 WDock *wDockCreate(WScreen *scr, int type)
1050 WDock *dock;
1051 WAppIcon *btn;
1053 make_keys();
1055 dock = wmalloc(sizeof(WDock));
1057 dock->max_icons = DOCK_MAX_ICONS;
1059 dock->icon_array = wmalloc(sizeof(WAppIcon *) * dock->max_icons);
1061 btn = mainIconCreate(scr, type);
1063 btn->dock = dock;
1065 dock->x_pos = btn->x_pos;
1066 dock->y_pos = btn->y_pos;
1067 dock->screen_ptr = scr;
1068 dock->type = type;
1069 dock->icon_count = 1;
1070 dock->on_right_side = 1;
1071 dock->collapsed = 0;
1072 dock->auto_collapse = 0;
1073 dock->auto_collapse_magic = NULL;
1074 dock->auto_raise_lower = 0;
1075 dock->auto_lower_magic = NULL;
1076 dock->auto_raise_magic = NULL;
1077 dock->attract_icons = 0;
1078 dock->lowered = 1;
1079 dock->icon_array[0] = btn;
1080 wRaiseFrame(btn->icon->core);
1081 XMoveWindow(dpy, btn->icon->core->window, btn->x_pos, btn->y_pos);
1083 /* create dock menu */
1084 dock->menu = dockMenuCreate(scr, type);
1086 return dock;
1089 void wDockDestroy(WDock *dock)
1091 int i;
1092 WAppIcon *aicon;
1094 for (i = (dock->type == WM_CLIP) ? 1 : 0; i < dock->max_icons; i++) {
1095 aicon = dock->icon_array[i];
1096 if (aicon) {
1097 int keepit = aicon->running && wApplicationOf(aicon->main_window);
1098 wDockDetach(dock, aicon);
1099 if (keepit) {
1100 /* XXX: can: aicon->icon == NULL ? */
1101 PlaceIcon(dock->screen_ptr, &aicon->x_pos, &aicon->y_pos,
1102 wGetHeadForWindow(aicon->icon->owner));
1103 XMoveWindow(dpy, aicon->icon->core->window, aicon->x_pos, aicon->y_pos);
1104 if (!dock->mapped || dock->collapsed)
1105 XMapWindow(dpy, aicon->icon->core->window);
1109 if (wPreferences.auto_arrange_icons)
1110 wArrangeIcons(dock->screen_ptr, True);
1111 wfree(dock->icon_array);
1112 if (dock->menu && dock->type != WM_CLIP)
1113 wMenuDestroy(dock->menu, True);
1114 if (dock->screen_ptr->last_dock == dock)
1115 dock->screen_ptr->last_dock = NULL;
1116 wfree(dock);
1119 void wClipIconPaint(WAppIcon *aicon)
1121 WScreen *scr = aicon->icon->core->screen_ptr;
1122 WWorkspace *workspace = scr->workspaces[scr->current_workspace];
1123 WMColor *color;
1124 Window win = aicon->icon->core->window;
1125 int length, nlength;
1126 char *ws_name, ws_number[10];
1127 int ty, tx;
1129 wIconPaint(aicon->icon);
1131 length = strlen(workspace->name);
1132 ws_name = wmalloc(length + 1);
1133 snprintf(ws_name, length + 1, "%s", workspace->name);
1134 snprintf(ws_number, sizeof(ws_number), "%i", scr->current_workspace + 1);
1135 nlength = strlen(ws_number);
1137 if (!workspace->clip->collapsed)
1138 color = scr->clip_title_color[CLIP_NORMAL];
1139 else
1140 color = scr->clip_title_color[CLIP_COLLAPSED];
1142 ty = ICON_SIZE - WMFontHeight(scr->clip_title_font) - 3;
1144 tx = CLIP_BUTTON_SIZE * ICON_SIZE / 64;
1146 if(wPreferences.show_clip_title)
1147 WMDrawString(scr->wmscreen, win, color, scr->clip_title_font, tx, ty, ws_name, length);
1149 tx = (ICON_SIZE / 2 - WMWidthOfString(scr->clip_title_font, ws_number, nlength)) / 2;
1151 WMDrawString(scr->wmscreen, win, color, scr->clip_title_font, tx, 2, ws_number, nlength);
1153 wfree(ws_name);
1155 if (aicon->launching) {
1156 XFillRectangle(dpy, aicon->icon->core->window, scr->stipple_gc,
1157 0, 0, wPreferences.icon_size, wPreferences.icon_size);
1159 paintClipButtons(aicon, aicon->dock->lclip_button_pushed, aicon->dock->rclip_button_pushed);
1162 static void clipIconExpose(WObjDescriptor *desc, XEvent *event)
1164 wClipIconPaint(desc->parent);
1167 static void dockIconPaint(WAppIcon *btn)
1169 if (btn == btn->icon->core->screen_ptr->clip_icon)
1170 wClipIconPaint(btn);
1171 else {
1172 wAppIconPaint(btn);
1173 save_appicon(btn, True);
1177 static WMPropList *make_icon_state(WAppIcon *btn)
1179 WMPropList *node = NULL;
1180 WMPropList *command, *autolaunch, *lock, *name, *forced;
1181 WMPropList *position, *buggy, *omnipresent;
1182 char *tmp;
1183 char buffer[64];
1185 if (btn) {
1186 if (!btn->command)
1187 command = WMCreatePLString("-");
1188 else
1189 command = WMCreatePLString(btn->command);
1191 autolaunch = btn->auto_launch ? dYes : dNo;
1193 lock = btn->lock ? dYes : dNo;
1195 tmp = EscapeWM_CLASS(btn->wm_instance, btn->wm_class);
1197 name = WMCreatePLString(tmp);
1199 wfree(tmp);
1201 forced = btn->forced_dock ? dYes : dNo;
1203 buggy = btn->buggy_app ? dYes : dNo;
1205 if (btn == btn->icon->core->screen_ptr->clip_icon)
1206 snprintf(buffer, sizeof(buffer), "%i,%i", btn->x_pos, btn->y_pos);
1207 else
1208 snprintf(buffer, sizeof(buffer), "%hi,%hi", btn->xindex, btn->yindex);
1209 position = WMCreatePLString(buffer);
1211 node = WMCreatePLDictionary(dCommand, command,
1212 dName, name,
1213 dAutoLaunch, autolaunch,
1214 dLock, lock,
1215 dForced, forced, dBuggyApplication, buggy, dPosition, position, NULL);
1216 WMReleasePropList(command);
1217 WMReleasePropList(name);
1218 WMReleasePropList(position);
1220 omnipresent = btn->omnipresent ? dYes : dNo;
1221 if (btn->dock != btn->icon->core->screen_ptr->dock && (btn->xindex != 0 || btn->yindex != 0))
1222 WMPutInPLDictionary(node, dOmnipresent, omnipresent);
1224 #ifdef XDND /* was OFFIX */
1225 if (btn->dnd_command) {
1226 command = WMCreatePLString(btn->dnd_command);
1227 WMPutInPLDictionary(node, dDropCommand, command);
1228 WMReleasePropList(command);
1230 #endif /* XDND */
1232 if (btn->paste_command) {
1233 command = WMCreatePLString(btn->paste_command);
1234 WMPutInPLDictionary(node, dPasteCommand, command);
1235 WMReleasePropList(command);
1239 return node;
1242 static WMPropList *dockSaveState(WDock *dock)
1244 int i;
1245 WMPropList *icon_info;
1246 WMPropList *list = NULL, *dock_state = NULL;
1247 WMPropList *value, *key;
1248 char buffer[256];
1250 list = WMCreatePLArray(NULL);
1252 for (i = (dock->type == WM_DOCK ? 0 : 1); i < dock->max_icons; i++) {
1253 WAppIcon *btn = dock->icon_array[i];
1255 if (!btn || btn->attracted)
1256 continue;
1258 if ((icon_info = make_icon_state(dock->icon_array[i]))) {
1259 WMAddToPLArray(list, icon_info);
1260 WMReleasePropList(icon_info);
1264 dock_state = WMCreatePLDictionary(dApplications, list, NULL);
1266 if (dock->type == WM_DOCK) {
1267 snprintf(buffer, sizeof(buffer), "Applications%i", dock->screen_ptr->scr_height);
1268 key = WMCreatePLString(buffer);
1269 WMPutInPLDictionary(dock_state, key, list);
1270 WMReleasePropList(key);
1272 snprintf(buffer, sizeof(buffer), "%i,%i", (dock->on_right_side ? -ICON_SIZE : 0), dock->y_pos);
1273 value = WMCreatePLString(buffer);
1274 WMPutInPLDictionary(dock_state, dPosition, value);
1275 WMReleasePropList(value);
1277 WMReleasePropList(list);
1279 value = (dock->lowered ? dYes : dNo);
1280 WMPutInPLDictionary(dock_state, dLowered, value);
1282 if (dock->type == WM_CLIP) {
1283 value = (dock->collapsed ? dYes : dNo);
1284 WMPutInPLDictionary(dock_state, dCollapsed, value);
1286 value = (dock->auto_collapse ? dYes : dNo);
1287 WMPutInPLDictionary(dock_state, dAutoCollapse, value);
1289 value = (dock->auto_raise_lower ? dYes : dNo);
1290 WMPutInPLDictionary(dock_state, dAutoRaiseLower, value);
1292 value = (dock->attract_icons ? dYes : dNo);
1293 WMPutInPLDictionary(dock_state, dAutoAttractIcons, value);
1296 return dock_state;
1299 void wDockSaveState(WScreen *scr, WMPropList *old_state)
1301 WMPropList *dock_state;
1302 WMPropList *keys;
1304 dock_state = dockSaveState(scr->dock);
1307 * Copy saved states of docks with different sizes.
1309 if (old_state) {
1310 int i;
1311 WMPropList *tmp;
1313 keys = WMGetPLDictionaryKeys(old_state);
1314 for (i = 0; i < WMGetPropListItemCount(keys); i++) {
1315 tmp = WMGetFromPLArray(keys, i);
1317 if (strncasecmp(WMGetFromPLString(tmp), "applications", 12) == 0
1318 && !WMGetFromPLDictionary(dock_state, tmp)) {
1320 WMPutInPLDictionary(dock_state, tmp, WMGetFromPLDictionary(old_state, tmp));
1323 WMReleasePropList(keys);
1326 WMPutInPLDictionary(scr->session_state, dDock, dock_state);
1328 WMReleasePropList(dock_state);
1331 void wClipSaveState(WScreen *scr)
1333 WMPropList *clip_state;
1335 clip_state = make_icon_state(scr->clip_icon);
1337 WMPutInPLDictionary(scr->session_state, dClip, clip_state);
1339 WMReleasePropList(clip_state);
1342 WMPropList *wClipSaveWorkspaceState(WScreen *scr, int workspace)
1344 return dockSaveState(scr->workspaces[workspace]->clip);
1347 static Bool getBooleanDockValue(WMPropList *value, WMPropList *key)
1349 if (value) {
1350 if (WMIsPLString(value)) {
1351 if (strcasecmp(WMGetFromPLString(value), "YES") == 0)
1352 return True;
1353 } else {
1354 wwarning(_("bad value in docked icon state info %s"), WMGetFromPLString(key));
1357 return False;
1360 static WAppIcon *restore_icon_state(WScreen *scr, WMPropList *info, int type, int index)
1362 WAppIcon *aicon;
1363 WMPropList *cmd, *value;
1365 cmd = WMGetFromPLDictionary(info, dCommand);
1366 if (!cmd || !WMIsPLString(cmd)) {
1367 return NULL;
1370 /* parse window name */
1371 value = WMGetFromPLDictionary(info, dName);
1372 if (!value)
1373 return NULL;
1376 char *wclass, *winstance;
1377 char *command;
1379 ParseWindowName(value, &winstance, &wclass, "dock");
1381 if (!winstance && !wclass) {
1382 return NULL;
1385 /* get commands */
1387 if (cmd)
1388 command = wstrdup(WMGetFromPLString(cmd));
1389 else
1390 command = NULL;
1392 if (!command || strcmp(command, "-") == 0) {
1393 if (command)
1394 wfree(command);
1395 if (wclass)
1396 wfree(wclass);
1397 if (winstance)
1398 wfree(winstance);
1400 return NULL;
1403 aicon = wAppIconCreateForDock(scr, command, winstance, wclass, TILE_NORMAL);
1404 if (wclass)
1405 wfree(wclass);
1406 if (winstance)
1407 wfree(winstance);
1408 if (command)
1409 wfree(command);
1412 aicon->icon->core->descriptor.handle_mousedown = iconMouseDown;
1413 if (type == WM_CLIP) {
1414 aicon->icon->core->descriptor.handle_enternotify = clipEnterNotify;
1415 aicon->icon->core->descriptor.handle_leavenotify = clipLeaveNotify;
1417 aicon->icon->core->descriptor.parent_type = WCLASS_DOCK_ICON;
1418 aicon->icon->core->descriptor.parent = aicon;
1420 #ifdef XDND /* was OFFIX */
1421 cmd = WMGetFromPLDictionary(info, dDropCommand);
1422 if (cmd)
1423 aicon->dnd_command = wstrdup(WMGetFromPLString(cmd));
1424 #endif
1426 cmd = WMGetFromPLDictionary(info, dPasteCommand);
1427 if (cmd)
1428 aicon->paste_command = wstrdup(WMGetFromPLString(cmd));
1430 /* check auto launch */
1431 value = WMGetFromPLDictionary(info, dAutoLaunch);
1433 aicon->auto_launch = getBooleanDockValue(value, dAutoLaunch);
1435 /* check lock */
1436 value = WMGetFromPLDictionary(info, dLock);
1438 aicon->lock = getBooleanDockValue(value, dLock);
1440 /* check if it wasn't normally docked */
1441 value = WMGetFromPLDictionary(info, dForced);
1443 aicon->forced_dock = getBooleanDockValue(value, dForced);
1445 /* check if we can rely on the stuff in the app */
1446 value = WMGetFromPLDictionary(info, dBuggyApplication);
1448 aicon->buggy_app = getBooleanDockValue(value, dBuggyApplication);
1450 /* get position in the dock */
1451 value = WMGetFromPLDictionary(info, dPosition);
1452 if (value && WMIsPLString(value)) {
1453 if (sscanf(WMGetFromPLString(value), "%hi,%hi", &aicon->xindex, &aicon->yindex) != 2)
1454 wwarning(_("bad value in docked icon state info %s"), WMGetFromPLString(dPosition));
1456 /* check position sanity */
1457 /* incomplete section! */
1458 if (type == WM_DOCK) {
1459 aicon->xindex = 0;
1460 if (aicon->yindex < 0)
1461 wwarning(_("bad value in docked icon position %i,%i"),
1462 aicon->xindex, aicon->yindex);
1464 } else {
1465 aicon->yindex = index;
1466 aicon->xindex = 0;
1469 /* check if icon is omnipresent */
1470 value = WMGetFromPLDictionary(info, dOmnipresent);
1472 aicon->omnipresent = getBooleanDockValue(value, dOmnipresent);
1474 aicon->running = 0;
1475 aicon->docked = 1;
1477 return aicon;
1480 #define COMPLAIN(key) wwarning(_("bad value in dock state info:%s"), key)
1482 WAppIcon *wClipRestoreState(WScreen *scr, WMPropList *clip_state)
1484 WAppIcon *icon;
1485 WMPropList *value;
1487 icon = mainIconCreate(scr, WM_CLIP);
1489 if (!clip_state)
1490 return icon;
1492 WMRetainPropList(clip_state);
1494 /* restore position */
1496 value = WMGetFromPLDictionary(clip_state, dPosition);
1498 if (value) {
1499 if (!WMIsPLString(value))
1500 COMPLAIN("Position");
1501 else {
1502 WMRect rect;
1503 int flags;
1505 if (sscanf(WMGetFromPLString(value), "%i,%i", &icon->x_pos, &icon->y_pos) != 2)
1506 COMPLAIN("Position");
1508 /* check position sanity */
1509 rect.pos.x = icon->x_pos;
1510 rect.pos.y = icon->y_pos;
1511 rect.size.width = rect.size.height = ICON_SIZE;
1513 wGetRectPlacementInfo(scr, rect, &flags);
1514 if (flags & (XFLAG_DEAD | XFLAG_PARTIAL))
1515 wScreenKeepInside(scr, &icon->x_pos, &icon->y_pos, ICON_SIZE, ICON_SIZE);
1518 #ifdef XDND /* was OFFIX */
1519 value = WMGetFromPLDictionary(clip_state, dDropCommand);
1520 if (value && WMIsPLString(value))
1521 icon->dnd_command = wstrdup(WMGetFromPLString(value));
1522 #endif
1524 value = WMGetFromPLDictionary(clip_state, dPasteCommand);
1525 if (value && WMIsPLString(value))
1526 icon->paste_command = wstrdup(WMGetFromPLString(value));
1528 WMReleasePropList(clip_state);
1530 return icon;
1533 WDock *wDockRestoreState(WScreen *scr, WMPropList *dock_state, int type)
1535 WDock *dock;
1536 WMPropList *apps;
1537 WMPropList *value;
1538 WAppIcon *aicon, *old_top;
1539 int count, i;
1541 dock = wDockCreate(scr, type);
1543 if (!dock_state)
1544 return dock;
1546 WMRetainPropList(dock_state);
1548 /* restore position */
1549 value = WMGetFromPLDictionary(dock_state, dPosition);
1550 if (value) {
1551 if (!WMIsPLString(value)) {
1552 COMPLAIN("Position");
1553 } else {
1554 WMRect rect;
1555 int flags;
1557 if (sscanf(WMGetFromPLString(value), "%i,%i", &dock->x_pos, &dock->y_pos) != 2)
1558 COMPLAIN("Position");
1560 /* check position sanity */
1561 rect.pos.x = dock->x_pos;
1562 rect.pos.y = dock->y_pos;
1563 rect.size.width = rect.size.height = ICON_SIZE;
1565 wGetRectPlacementInfo(scr, rect, &flags);
1566 if (flags & (XFLAG_DEAD | XFLAG_PARTIAL)) {
1567 int x = dock->x_pos;
1568 wScreenKeepInside(scr, &x, &dock->y_pos, ICON_SIZE, ICON_SIZE);
1571 /* Is this needed any more? */
1572 if (type == WM_CLIP) {
1573 if (dock->x_pos < 0) {
1574 dock->x_pos = 0;
1575 } else if (dock->x_pos > scr->scr_width - ICON_SIZE) {
1576 dock->x_pos = scr->scr_width - ICON_SIZE;
1578 } else {
1579 if (dock->x_pos >= 0) {
1580 dock->x_pos = DOCK_EXTRA_SPACE;
1581 dock->on_right_side = 0;
1582 } else {
1583 dock->x_pos = scr->scr_width - DOCK_EXTRA_SPACE - ICON_SIZE;
1584 dock->on_right_side = 1;
1590 /* restore lowered/raised state */
1591 dock->lowered = 0;
1593 value = WMGetFromPLDictionary(dock_state, dLowered);
1594 if (value) {
1595 if (!WMIsPLString(value)) {
1596 COMPLAIN("Lowered");
1597 } else {
1598 if (strcasecmp(WMGetFromPLString(value), "YES") == 0) {
1599 dock->lowered = 1;
1604 /* restore collapsed state */
1605 dock->collapsed = 0;
1607 value = WMGetFromPLDictionary(dock_state, dCollapsed);
1608 if (value) {
1609 if (!WMIsPLString(value)) {
1610 COMPLAIN("Collapsed");
1611 } else {
1612 if (strcasecmp(WMGetFromPLString(value), "YES") == 0) {
1613 dock->collapsed = 1;
1618 /* restore auto-collapsed state */
1619 value = WMGetFromPLDictionary(dock_state, dAutoCollapse);
1620 if (value) {
1621 if (!WMIsPLString(value)) {
1622 COMPLAIN("AutoCollapse");
1623 } else {
1624 if (strcasecmp(WMGetFromPLString(value), "YES") == 0) {
1625 dock->auto_collapse = 1;
1626 dock->collapsed = 1;
1631 /* restore auto-raise/lower state */
1632 value = WMGetFromPLDictionary(dock_state, dAutoRaiseLower);
1633 if (value) {
1634 if (!WMIsPLString(value)) {
1635 COMPLAIN("AutoRaiseLower");
1636 } else {
1637 if (strcasecmp(WMGetFromPLString(value), "YES") == 0)
1638 dock->auto_raise_lower = 1;
1642 /* restore attract icons state */
1643 dock->attract_icons = 0;
1645 value = WMGetFromPLDictionary(dock_state, dAutoAttractIcons);
1646 if (value) {
1647 if (!WMIsPLString(value)) {
1648 COMPLAIN("AutoAttractIcons");
1649 } else {
1650 if (strcasecmp(WMGetFromPLString(value), "YES") == 0)
1651 dock->attract_icons = 1;
1655 /* application list */
1658 WMPropList *tmp;
1659 char buffer[64];
1662 * When saving, it saves the dock state in
1663 * Applications and Applicationsnnn
1665 * When loading, it will first try Applicationsnnn.
1666 * If it does not exist, use Applications as default.
1669 snprintf(buffer, sizeof(buffer), "Applications%i", scr->scr_height);
1671 tmp = WMCreatePLString(buffer);
1672 apps = WMGetFromPLDictionary(dock_state, tmp);
1673 WMReleasePropList(tmp);
1675 if (!apps)
1676 apps = WMGetFromPLDictionary(dock_state, dApplications);
1679 if (!apps)
1680 goto finish;
1682 count = WMGetPropListItemCount(apps);
1683 if (count == 0)
1684 goto finish;
1686 old_top = dock->icon_array[0];
1688 /* dock->icon_count is set to 1 when dock is created.
1689 * Since Clip is already restored, we want to keep it so for clip,
1690 * but for dock we may change the default top tile, so we set it to 0.
1692 if (type == WM_DOCK)
1693 dock->icon_count = 0;
1695 for (i = 0; i < count; i++) {
1696 if (dock->icon_count >= dock->max_icons) {
1697 wwarning(_("there are too many icons stored in dock. Ignoring what doesn't fit"));
1698 break;
1701 value = WMGetFromPLArray(apps, i);
1702 aicon = restore_icon_state(scr, value, type, dock->icon_count);
1704 dock->icon_array[dock->icon_count] = aicon;
1706 if (aicon) {
1707 aicon->dock = dock;
1708 aicon->x_pos = dock->x_pos + (aicon->xindex * ICON_SIZE);
1709 aicon->y_pos = dock->y_pos + (aicon->yindex * ICON_SIZE);
1711 if (dock->lowered)
1712 ChangeStackingLevel(aicon->icon->core, WMNormalLevel);
1713 else
1714 ChangeStackingLevel(aicon->icon->core, WMDockLevel);
1716 wCoreConfigure(aicon->icon->core, aicon->x_pos, aicon->y_pos, 0, 0);
1718 if (!dock->collapsed)
1719 XMapWindow(dpy, aicon->icon->core->window);
1720 wRaiseFrame(aicon->icon->core);
1722 dock->icon_count++;
1723 } else if (dock->icon_count == 0 && type == WM_DOCK)
1724 dock->icon_count++;
1727 /* if the first icon is not defined, use the default */
1728 if (dock->icon_array[0] == NULL) {
1729 /* update default icon */
1730 old_top->x_pos = dock->x_pos;
1731 old_top->y_pos = dock->y_pos;
1732 if (dock->lowered)
1733 ChangeStackingLevel(old_top->icon->core, WMNormalLevel);
1734 else
1735 ChangeStackingLevel(old_top->icon->core, WMDockLevel);
1736 dock->icon_array[0] = old_top;
1737 XMoveWindow(dpy, old_top->icon->core->window, dock->x_pos, dock->y_pos);
1738 /* we don't need to increment dock->icon_count here because it was
1739 * incremented in the loop above.
1741 } else if (old_top != dock->icon_array[0]) {
1742 if (old_top == scr->clip_icon)
1743 scr->clip_icon = dock->icon_array[0];
1744 wAppIconDestroy(old_top);
1747 finish:
1748 WMReleasePropList(dock_state);
1750 return dock;
1753 void wDockLaunchWithState(WDock *dock, WAppIcon *btn, WSavedState *state)
1755 if (btn && btn->command && !btn->running && !btn->launching) {
1757 btn->drop_launch = 0;
1758 btn->paste_launch = 0;
1760 btn->pid = execCommand(btn, btn->command, state);
1762 if (btn->pid > 0) {
1763 if (!btn->forced_dock && !btn->buggy_app) {
1764 btn->launching = 1;
1765 dockIconPaint(btn);
1768 } else {
1769 wfree(state);
1773 void wDockDoAutoLaunch(WDock *dock, int workspace)
1775 WAppIcon *btn;
1776 WSavedState *state;
1777 int i;
1779 for (i = 0; i < dock->max_icons; i++) {
1780 btn = dock->icon_array[i];
1781 if (!btn || !btn->auto_launch)
1782 continue;
1784 state = wmalloc(sizeof(WSavedState));
1785 state->workspace = workspace;
1786 /* TODO: this is klugy and is very difficult to understand
1787 * what's going on. Try to clean up */
1788 wDockLaunchWithState(dock, btn, state);
1792 #ifdef XDND /* was OFFIX */
1793 static WDock *findDock(WScreen *scr, XEvent *event, int *icon_pos)
1795 WDock *dock;
1796 int i;
1798 *icon_pos = -1;
1799 if ((dock = scr->dock) != NULL) {
1800 for (i = 0; i < dock->max_icons; i++) {
1801 if (dock->icon_array[i]
1802 && dock->icon_array[i]->icon->core->window == event->xclient.window) {
1803 *icon_pos = i;
1804 break;
1808 if (*icon_pos < 0 && (dock = scr->workspaces[scr->current_workspace]->clip) != NULL) {
1809 for (i = 0; i < dock->max_icons; i++) {
1810 if (dock->icon_array[i]
1811 && dock->icon_array[i]->icon->core->window == event->xclient.window) {
1812 *icon_pos = i;
1813 break;
1817 if (*icon_pos >= 0)
1818 return dock;
1819 return NULL;
1822 int wDockReceiveDNDDrop(WScreen *scr, XEvent *event)
1824 WDock *dock;
1825 WAppIcon *btn;
1826 int icon_pos;
1828 dock = findDock(scr, event, &icon_pos);
1829 if (!dock)
1830 return False;
1833 * Return True if the drop was on an application icon window.
1834 * In this case, let the ClientMessage handler redirect the
1835 * message to the app.
1837 if (dock->icon_array[icon_pos]->icon->icon_win != None)
1838 return True;
1840 if (dock->icon_array[icon_pos]->dnd_command != NULL) {
1841 scr->flags.dnd_data_convertion_status = 0;
1843 btn = dock->icon_array[icon_pos];
1845 if (!btn->forced_dock) {
1846 btn->relaunching = btn->running;
1847 btn->running = 1;
1849 if (btn->wm_instance || btn->wm_class) {
1850 WWindowAttributes attr;
1851 memset(&attr, 0, sizeof(WWindowAttributes));
1852 wDefaultFillAttributes(btn->icon->core->screen_ptr,
1853 btn->wm_instance, btn->wm_class, &attr, NULL, True);
1855 if (!attr.no_appicon)
1856 btn->launching = 1;
1857 else
1858 btn->running = 0;
1861 btn->paste_launch = 0;
1862 btn->drop_launch = 1;
1863 scr->last_dock = dock;
1864 btn->pid = execCommand(btn, btn->dnd_command, NULL);
1865 if (btn->pid > 0) {
1866 dockIconPaint(btn);
1867 } else {
1868 btn->launching = 0;
1869 if (!btn->relaunching) {
1870 btn->running = 0;
1874 return False;
1876 #endif /* XDND */
1878 Bool wDockAttachIcon(WDock *dock, WAppIcon *icon, int x, int y)
1880 WWindow *wwin;
1881 int index;
1883 wwin = icon->icon->owner;
1884 if (icon->command == NULL) {
1885 char *command;
1887 icon->editing = 0;
1889 command = GetCommandForWindow(wwin->client_win);
1890 if (command) {
1891 icon->command = command;
1892 } else {
1893 /* icon->forced_dock = 1; */
1894 if (dock->type != WM_CLIP || !icon->attracted) {
1895 icon->editing = 1;
1896 if (wInputDialog(dock->screen_ptr, _("Dock Icon"),
1897 _("Type the command used to launch the application"), &command)) {
1898 if (command && (command[0] == 0 || (command[0] == '-' && command[1] == 0))) {
1899 wfree(command);
1900 command = NULL;
1902 icon->command = command;
1903 icon->editing = 0;
1904 } else {
1905 icon->editing = 0;
1906 if (command)
1907 wfree(command);
1908 /* If the target is the dock, reject the icon. If
1909 * the target is the clip, make it an attracted icon
1911 if (dock->type == WM_CLIP) {
1912 icon->attracted = 1;
1913 if (!icon->icon->shadowed) {
1914 icon->icon->shadowed = 1;
1915 icon->icon->force_paint = 1;
1917 } else {
1918 return False;
1923 } else {
1924 icon->editing = 0;
1927 for (index = 1; index < dock->max_icons; index++)
1928 if (dock->icon_array[index] == NULL)
1929 break;
1930 /* if (index == dock->max_icons)
1931 return; */
1933 assert(index < dock->max_icons);
1935 dock->icon_array[index] = icon;
1936 icon->yindex = y;
1937 icon->xindex = x;
1939 icon->omnipresent = 0;
1941 icon->x_pos = dock->x_pos + x * ICON_SIZE;
1942 icon->y_pos = dock->y_pos + y * ICON_SIZE;
1944 dock->icon_count++;
1946 icon->running = 1;
1947 icon->launching = 0;
1948 icon->docked = 1;
1949 icon->dock = dock;
1950 icon->icon->core->descriptor.handle_mousedown = iconMouseDown;
1951 if (dock->type == WM_CLIP) {
1952 icon->icon->core->descriptor.handle_enternotify = clipEnterNotify;
1953 icon->icon->core->descriptor.handle_leavenotify = clipLeaveNotify;
1955 icon->icon->core->descriptor.parent_type = WCLASS_DOCK_ICON;
1956 icon->icon->core->descriptor.parent = icon;
1958 MoveInStackListUnder(dock->icon_array[index - 1]->icon->core, icon->icon->core);
1959 wAppIconMove(icon, icon->x_pos, icon->y_pos);
1960 wAppIconPaint(icon);
1961 save_appicon(icon, True);
1963 if (wPreferences.auto_arrange_icons)
1964 wArrangeIcons(dock->screen_ptr, True);
1966 #ifdef XDND /* was OFFIX */
1967 if (icon->command && !icon->dnd_command) {
1968 int len = strlen(icon->command) + 8;
1969 icon->dnd_command = wmalloc(len);
1970 snprintf(icon->dnd_command, len, "%s %%d", icon->command);
1972 #endif
1974 if (icon->command && !icon->paste_command) {
1975 int len = strlen(icon->command) + 8;
1976 icon->paste_command = wmalloc(len);
1977 snprintf(icon->paste_command, len, "%s %%s", icon->command);
1980 return True;
1983 static void reattachIcon(WDock *dock, WAppIcon *icon, int x, int y)
1985 int index;
1987 for (index = 1; index < dock->max_icons; index++) {
1988 if (dock->icon_array[index] == icon)
1989 break;
1991 assert(index < dock->max_icons);
1993 icon->yindex = y;
1994 icon->xindex = x;
1996 icon->x_pos = dock->x_pos + x * ICON_SIZE;
1997 icon->y_pos = dock->y_pos + y * ICON_SIZE;
2000 static Bool moveIconBetweenDocks(WDock *src, WDock *dest, WAppIcon *icon, int x, int y)
2002 WWindow *wwin;
2003 char *command;
2004 int index;
2006 if (src == dest)
2007 return True; /* No move needed, we're already there */
2009 if (dest == NULL)
2010 return False;
2012 wwin = icon->icon->owner;
2015 * For the moment we can't do this if we move icons in Clip from one
2016 * workspace to other, because if we move two or more icons without
2017 * command, the dialog box will not be able to tell us to which of the
2018 * moved icons it applies. -Dan
2020 if ((dest->type == WM_DOCK /*|| dest->keep_attracted */ ) && icon->command == NULL) {
2021 command = GetCommandForWindow(wwin->client_win);
2022 if (command) {
2023 icon->command = command;
2024 } else {
2025 icon->editing = 1;
2026 /* icon->forced_dock = 1; */
2027 if (wInputDialog(src->screen_ptr, _("Dock Icon"),
2028 _("Type the command used to launch the application"), &command)) {
2029 if (command && (command[0] == 0 || (command[0] == '-' && command[1] == 0))) {
2030 wfree(command);
2031 command = NULL;
2033 icon->command = command;
2034 } else {
2035 icon->editing = 0;
2036 if (command)
2037 wfree(command);
2038 return False;
2040 icon->editing = 0;
2044 if (dest->type == WM_DOCK)
2045 wClipMakeIconOmnipresent(icon, False);
2047 for (index = 1; index < src->max_icons; index++) {
2048 if (src->icon_array[index] == icon)
2049 break;
2051 assert(index < src->max_icons);
2053 src->icon_array[index] = NULL;
2054 src->icon_count--;
2056 for (index = 1; index < dest->max_icons; index++) {
2057 if (dest->icon_array[index] == NULL)
2058 break;
2060 /* if (index == dest->max_icons)
2061 return; */
2063 assert(index < dest->max_icons);
2065 dest->icon_array[index] = icon;
2066 icon->dock = dest;
2068 /* deselect the icon */
2069 if (icon->icon->selected)
2070 wIconSelect(icon->icon);
2072 if (dest->type == WM_DOCK) {
2073 icon->icon->core->descriptor.handle_enternotify = NULL;
2074 icon->icon->core->descriptor.handle_leavenotify = NULL;
2075 } else {
2076 icon->icon->core->descriptor.handle_enternotify = clipEnterNotify;
2077 icon->icon->core->descriptor.handle_leavenotify = clipLeaveNotify;
2080 /* set it to be kept when moving to dock.
2081 * Unless the icon does not have a command set
2083 if (icon->command && dest->type == WM_DOCK) {
2084 icon->attracted = 0;
2085 if (icon->icon->shadowed) {
2086 icon->icon->shadowed = 0;
2087 icon->icon->force_paint = 1;
2089 save_appicon(icon, True);
2092 if (src->auto_collapse || src->auto_raise_lower)
2093 clipLeave(src);
2095 icon->yindex = y;
2096 icon->xindex = x;
2098 icon->x_pos = dest->x_pos + x * ICON_SIZE;
2099 icon->y_pos = dest->y_pos + y * ICON_SIZE;
2101 dest->icon_count++;
2103 MoveInStackListUnder(dest->icon_array[index - 1]->icon->core, icon->icon->core);
2104 wAppIconPaint(icon);
2106 return True;
2109 void wDockDetach(WDock *dock, WAppIcon *icon)
2111 int index;
2113 /* make the settings panel be closed */
2114 if (icon->panel) {
2115 DestroyDockAppSettingsPanel(icon->panel);
2118 /* This must be called before icon->dock is set to NULL.
2119 * Don't move it. -Dan
2121 wClipMakeIconOmnipresent(icon, False);
2123 icon->docked = 0;
2124 icon->dock = NULL;
2125 icon->attracted = 0;
2126 icon->auto_launch = 0;
2127 if (icon->icon->shadowed) {
2128 icon->icon->shadowed = 0;
2129 icon->icon->force_paint = 1;
2132 /* deselect the icon */
2133 if (icon->icon->selected)
2134 wIconSelect(icon->icon);
2136 if (icon->command) {
2137 wfree(icon->command);
2138 icon->command = NULL;
2140 #ifdef XDND /* was OFFIX */
2141 if (icon->dnd_command) {
2142 wfree(icon->dnd_command);
2143 icon->dnd_command = NULL;
2145 #endif
2146 if (icon->paste_command) {
2147 wfree(icon->paste_command);
2148 icon->paste_command = NULL;
2151 for (index = 1; index < dock->max_icons; index++)
2152 if (dock->icon_array[index] == icon)
2153 break;
2154 assert(index < dock->max_icons);
2155 dock->icon_array[index] = NULL;
2156 icon->yindex = -1;
2157 icon->xindex = -1;
2159 dock->icon_count--;
2161 /* if the dock is not attached to an application or
2162 * the application did not set the appropriate hints yet,
2163 * destroy the icon */
2164 if (!icon->running || !wApplicationOf(icon->main_window))
2165 wAppIconDestroy(icon);
2166 else {
2167 icon->icon->core->descriptor.handle_mousedown = appIconMouseDown;
2168 icon->icon->core->descriptor.handle_enternotify = NULL;
2169 icon->icon->core->descriptor.handle_leavenotify = NULL;
2170 icon->icon->core->descriptor.parent_type = WCLASS_APPICON;
2171 icon->icon->core->descriptor.parent = icon;
2173 ChangeStackingLevel(icon->icon->core, NORMAL_ICON_LEVEL);
2175 wAppIconPaint(icon);
2176 if (wPreferences.auto_arrange_icons) {
2177 wArrangeIcons(dock->screen_ptr, True);
2180 if (dock->auto_collapse || dock->auto_raise_lower)
2181 clipLeave(dock);
2185 * returns the closest Dock slot index for the passed
2186 * coordinates.
2188 * Returns False if icon can't be docked.
2190 * Note: this function should NEVER alter ret_x or ret_y, unless it will
2191 * return True. -Dan
2193 Bool wDockSnapIcon(WDock *dock, WAppIcon *icon, int req_x, int req_y, int *ret_x, int *ret_y, int redocking)
2195 WScreen *scr = dock->screen_ptr;
2196 int dx, dy;
2197 int ex_x, ex_y;
2198 int i, offset = ICON_SIZE / 2;
2199 WAppIcon *aicon = NULL;
2200 WAppIcon *nicon = NULL;
2201 int max_y_icons;
2203 /* TODO: XINERAMA, for these */
2204 max_y_icons = scr->scr_height / ICON_SIZE - 1;
2206 if (wPreferences.flags.noupdates)
2207 return False;
2209 dx = dock->x_pos;
2210 dy = dock->y_pos;
2212 /* if the dock is full */
2213 if (!redocking && (dock->icon_count >= dock->max_icons)) {
2214 return False;
2217 /* exact position */
2218 if (req_y < dy)
2219 ex_y = (req_y - offset - dy) / ICON_SIZE;
2220 else
2221 ex_y = (req_y + offset - dy) / ICON_SIZE;
2223 if (req_x < dx)
2224 ex_x = (req_x - offset - dx) / ICON_SIZE;
2225 else
2226 ex_x = (req_x + offset - dx) / ICON_SIZE;
2228 /* check if the icon is outside the screen boundaries */
2230 WMRect rect;
2231 int flags;
2233 rect.pos.x = dx + ex_x * ICON_SIZE;
2234 rect.pos.y = dy + ex_y * ICON_SIZE;
2235 rect.size.width = rect.size.height = ICON_SIZE;
2237 wGetRectPlacementInfo(scr, rect, &flags);
2238 if (flags & (XFLAG_DEAD | XFLAG_PARTIAL))
2239 return False;
2242 if (dock->type == WM_DOCK) {
2243 if (icon->dock != dock && ex_x != 0)
2244 return False;
2246 aicon = NULL;
2247 for (i = 0; i < dock->max_icons; i++) {
2248 nicon = dock->icon_array[i];
2249 if (nicon && nicon->yindex == ex_y) {
2250 aicon = nicon;
2251 break;
2255 if (redocking) {
2256 int sig, done, closest;
2258 /* Possible cases when redocking:
2260 * icon dragged out of range of any slot -> false
2261 * icon dragged to range of free slot
2262 * icon dragged to range of same slot
2263 * icon dragged to range of different icon
2265 if (abs(ex_x) > DOCK_DETTACH_THRESHOLD)
2266 return False;
2268 if (ex_y >= 0 && ex_y <= max_y_icons && (aicon == icon || !aicon)) {
2269 *ret_x = 0;
2270 *ret_y = ex_y;
2271 return True;
2274 /* start looking at the upper slot or lower? */
2275 if (ex_y * ICON_SIZE < (req_y + offset - dy))
2276 sig = 1;
2277 else
2278 sig = -1;
2280 closest = -1;
2281 done = 0;
2282 /* look for closest free slot */
2283 for (i = 0; i < (DOCK_DETTACH_THRESHOLD + 1) * 2 && !done; i++) {
2284 int j;
2286 done = 1;
2287 closest = sig * (i / 2) + ex_y;
2288 /* check if this slot is used */
2289 if (closest >= 0) {
2290 for (j = 0; j < dock->max_icons; j++) {
2291 if (dock->icon_array[j]
2292 && dock->icon_array[j]->yindex == closest) {
2293 /* slot is used by someone else */
2294 if (dock->icon_array[j] != icon)
2295 done = 0;
2296 break;
2300 sig = -sig;
2302 if (done && closest >= 0 && closest <= max_y_icons &&
2303 ((ex_y >= closest && ex_y - closest < DOCK_DETTACH_THRESHOLD + 1)
2304 || (ex_y < closest && closest - ex_y <= DOCK_DETTACH_THRESHOLD + 1))) {
2305 *ret_x = 0;
2306 *ret_y = closest;
2307 return True;
2309 } else { /* !redocking */
2311 /* if slot is free and the icon is close enough, return it */
2312 if (!aicon && ex_x == 0 && ex_y >= 0 && ex_y <= max_y_icons) {
2313 *ret_x = 0;
2314 *ret_y = ex_y;
2315 return True;
2318 } else { /* CLIP */
2319 int neighbours = 0;
2320 int start, stop, k;
2322 start = icon->omnipresent ? 0 : scr->current_workspace;
2323 stop = icon->omnipresent ? scr->workspace_count : start + 1;
2325 aicon = NULL;
2326 for (k = start; k < stop; k++) {
2327 WDock *tmp = scr->workspaces[k]->clip;
2328 if (!tmp)
2329 continue;
2330 for (i = 0; i < tmp->max_icons; i++) {
2331 nicon = tmp->icon_array[i];
2332 if (nicon && nicon->xindex == ex_x && nicon->yindex == ex_y) {
2333 aicon = nicon;
2334 break;
2337 if (aicon)
2338 break;
2340 for (k = start; k < stop; k++) {
2341 WDock *tmp = scr->workspaces[k]->clip;
2342 if (!tmp)
2343 continue;
2344 for (i = 0; i < tmp->max_icons; i++) {
2345 nicon = tmp->icon_array[i];
2346 if (nicon && nicon != icon && /* Icon can't be it's own neighbour */
2347 (abs(nicon->xindex - ex_x) <= CLIP_ATTACH_VICINITY &&
2348 abs(nicon->yindex - ex_y) <= CLIP_ATTACH_VICINITY)) {
2349 neighbours = 1;
2350 break;
2353 if (neighbours)
2354 break;
2357 if (neighbours && (aicon == NULL || (redocking && aicon == icon))) {
2358 *ret_x = ex_x;
2359 *ret_y = ex_y;
2360 return True;
2363 return False;
2366 static int onScreen(WScreen *scr, int x, int y, int sx, int ex, int sy, int ey)
2368 WMRect rect = wmkrect(x, y, ICON_SIZE, ICON_SIZE);
2369 int flags;
2371 wGetRectPlacementInfo(scr, rect, &flags);
2373 return !(flags & (XFLAG_DEAD | XFLAG_PARTIAL));
2377 * returns true if it can find a free slot in the dock,
2378 * in which case it changes x_pos and y_pos accordingly.
2379 * Else returns false.
2381 Bool wDockFindFreeSlot(WDock *dock, int *x_pos, int *y_pos)
2383 WScreen *scr = dock->screen_ptr;
2384 WAppIcon *btn;
2385 WAppIconChain *chain;
2386 unsigned char *slot_map;
2387 int mwidth;
2388 int r;
2389 int x, y;
2390 int i, done = False;
2391 int corner;
2392 int sx = 0, sy = 0, ex = scr->scr_width, ey = scr->scr_height;
2393 int extra_count = 0;
2395 if (dock->type == WM_CLIP && dock != scr->workspaces[scr->current_workspace]->clip)
2396 extra_count = scr->global_icon_count;
2398 /* if the dock is full */
2399 if (dock->icon_count + extra_count >= dock->max_icons) {
2400 return False;
2403 if (!wPreferences.flags.nodock && scr->dock) {
2404 if (scr->dock->on_right_side)
2405 ex -= ICON_SIZE + DOCK_EXTRA_SPACE;
2406 else
2407 sx += ICON_SIZE + DOCK_EXTRA_SPACE;
2410 if (ex < dock->x_pos)
2411 ex = dock->x_pos;
2412 if (sx > dock->x_pos + ICON_SIZE)
2413 sx = dock->x_pos + ICON_SIZE;
2414 #define C_NONE 0
2415 #define C_NW 1
2416 #define C_NE 2
2417 #define C_SW 3
2418 #define C_SE 4
2420 /* check if clip is in a corner */
2421 if (dock->type == WM_CLIP) {
2422 if (dock->x_pos < 1 && dock->y_pos < 1)
2423 corner = C_NE;
2424 else if (dock->x_pos < 1 && dock->y_pos >= (ey - ICON_SIZE))
2425 corner = C_SE;
2426 else if (dock->x_pos >= (ex - ICON_SIZE) && dock->y_pos >= (ey - ICON_SIZE))
2427 corner = C_SW;
2428 else if (dock->x_pos >= (ex - ICON_SIZE) && dock->y_pos < 1)
2429 corner = C_NW;
2430 else
2431 corner = C_NONE;
2432 } else
2433 corner = C_NONE;
2435 /* If the clip is in the corner, use only slots that are in the border
2436 * of the screen */
2437 if (corner != C_NONE) {
2438 char *hmap, *vmap;
2439 int hcount, vcount;
2441 hcount = WMIN(dock->max_icons, scr->scr_width / ICON_SIZE);
2442 vcount = WMIN(dock->max_icons, scr->scr_height / ICON_SIZE);
2443 hmap = wmalloc(hcount + 1);
2444 vmap = wmalloc(vcount + 1);
2446 /* mark used positions */
2447 switch (corner) {
2448 case C_NE:
2449 for (i = 0; i < dock->max_icons; i++) {
2450 btn = dock->icon_array[i];
2451 if (!btn)
2452 continue;
2454 if (btn->xindex == 0 && btn->yindex > 0 && btn->yindex < vcount)
2455 vmap[btn->yindex] = 1;
2456 else if (btn->yindex == 0 && btn->xindex > 0 && btn->xindex < hcount)
2457 hmap[btn->xindex] = 1;
2459 for (chain = scr->global_icons; chain != NULL; chain = chain->next) {
2460 btn = chain->aicon;
2461 if (btn->xindex == 0 && btn->yindex > 0 && btn->yindex < vcount)
2462 vmap[btn->yindex] = 1;
2463 else if (btn->yindex == 0 && btn->xindex > 0 && btn->xindex < hcount)
2464 hmap[btn->xindex] = 1;
2466 break;
2467 case C_NW:
2468 for (i = 0; i < dock->max_icons; i++) {
2469 btn = dock->icon_array[i];
2470 if (!btn)
2471 continue;
2473 if (btn->xindex == 0 && btn->yindex > 0 && btn->yindex < vcount)
2474 vmap[btn->yindex] = 1;
2475 else if (btn->yindex == 0 && btn->xindex < 0 && btn->xindex > -hcount)
2476 hmap[-btn->xindex] = 1;
2478 for (chain = scr->global_icons; chain != NULL; chain = chain->next) {
2479 btn = chain->aicon;
2480 if (btn->xindex == 0 && btn->yindex > 0 && btn->yindex < vcount)
2481 vmap[btn->yindex] = 1;
2482 else if (btn->yindex == 0 && btn->xindex < 0 && btn->xindex > -hcount)
2483 hmap[-btn->xindex] = 1;
2485 break;
2486 case C_SE:
2487 for (i = 0; i < dock->max_icons; i++) {
2488 btn = dock->icon_array[i];
2489 if (!btn)
2490 continue;
2492 if (btn->xindex == 0 && btn->yindex < 0 && btn->yindex > -vcount)
2493 vmap[-btn->yindex] = 1;
2494 else if (btn->yindex == 0 && btn->xindex > 0 && btn->xindex < hcount)
2495 hmap[btn->xindex] = 1;
2497 for (chain = scr->global_icons; chain != NULL; chain = chain->next) {
2498 btn = chain->aicon;
2499 if (btn->xindex == 0 && btn->yindex < 0 && btn->yindex > -vcount)
2500 vmap[-btn->yindex] = 1;
2501 else if (btn->yindex == 0 && btn->xindex > 0 && btn->xindex < hcount)
2502 hmap[btn->xindex] = 1;
2504 break;
2505 case C_SW:
2506 default:
2507 for (i = 0; i < dock->max_icons; i++) {
2508 btn = dock->icon_array[i];
2509 if (!btn)
2510 continue;
2512 if (btn->xindex == 0 && btn->yindex < 0 && btn->yindex > -vcount)
2513 vmap[-btn->yindex] = 1;
2514 else if (btn->yindex == 0 && btn->xindex < 0 && btn->xindex > -hcount)
2515 hmap[-btn->xindex] = 1;
2517 for (chain = scr->global_icons; chain != NULL; chain = chain->next) {
2518 btn = chain->aicon;
2519 if (btn->xindex == 0 && btn->yindex < 0 && btn->yindex > -vcount)
2520 vmap[-btn->yindex] = 1;
2521 else if (btn->yindex == 0 && btn->xindex < 0 && btn->xindex > -hcount)
2522 hmap[-btn->xindex] = 1;
2525 x = 0;
2526 y = 0;
2527 done = 0;
2528 /* search a vacant slot */
2529 for (i = 1; i < WMAX(vcount, hcount); i++) {
2530 if (i < vcount && vmap[i] == 0) {
2531 /* found a slot */
2532 x = 0;
2533 y = i;
2534 done = 1;
2535 break;
2536 } else if (i < hcount && hmap[i] == 0) {
2537 /* found a slot */
2538 x = i;
2539 y = 0;
2540 done = 1;
2541 break;
2544 wfree(vmap);
2545 wfree(hmap);
2546 /* If found a slot, translate and return */
2547 if (done) {
2548 if (corner == C_NW || corner == C_NE) {
2549 *y_pos = y;
2550 } else {
2551 *y_pos = -y;
2553 if (corner == C_NE || corner == C_SE) {
2554 *x_pos = x;
2555 } else {
2556 *x_pos = -x;
2558 return True;
2560 /* else, try to find a slot somewhere else */
2563 /* a map of mwidth x mwidth would be enough if we allowed icons to be
2564 * placed outside of screen */
2565 mwidth = (int)ceil(sqrt(dock->max_icons));
2567 /* In the worst case (the clip is in the corner of the screen),
2568 * the amount of icons that fit in the clip is smaller.
2569 * Double the map to get a safe value.
2571 mwidth += mwidth;
2573 r = (mwidth - 1) / 2;
2575 slot_map = wmalloc(mwidth * mwidth);
2577 #define XY2OFS(x,y) (WMAX(abs(x),abs(y)) > r) ? 0 : (((y)+r)*(mwidth)+(x)+r)
2579 /* mark used slots in the map. If the slot falls outside the map
2580 * (for example, when all icons are placed in line), ignore them. */
2581 for (i = 0; i < dock->max_icons; i++) {
2582 btn = dock->icon_array[i];
2583 if (btn)
2584 slot_map[XY2OFS(btn->xindex, btn->yindex)] = 1;
2586 for (chain = scr->global_icons; chain != NULL; chain = chain->next) {
2587 slot_map[XY2OFS(chain->aicon->xindex, chain->aicon->yindex)] = 1;
2589 /* Find closest slot from the center that is free by scanning the
2590 * map from the center to outward in circular passes.
2591 * This will not result in a neat layout, but will be optimal
2592 * in the sense that there will not be holes left.
2594 done = 0;
2595 for (i = 1; i <= r && !done; i++) {
2596 int tx, ty;
2598 /* top and bottom parts of the ring */
2599 for (x = -i; x <= i && !done; x++) {
2600 tx = dock->x_pos + x * ICON_SIZE;
2601 y = -i;
2602 ty = dock->y_pos + y * ICON_SIZE;
2603 if (slot_map[XY2OFS(x, y)] == 0 && onScreen(scr, tx, ty, sx, ex, sy, ey)) {
2604 *x_pos = x;
2605 *y_pos = y;
2606 done = 1;
2607 break;
2609 y = i;
2610 ty = dock->y_pos + y * ICON_SIZE;
2611 if (slot_map[XY2OFS(x, y)] == 0 && onScreen(scr, tx, ty, sx, ex, sy, ey)) {
2612 *x_pos = x;
2613 *y_pos = y;
2614 done = 1;
2615 break;
2618 /* left and right parts of the ring */
2619 for (y = -i + 1; y <= i - 1; y++) {
2620 ty = dock->y_pos + y * ICON_SIZE;
2621 x = -i;
2622 tx = dock->x_pos + x * ICON_SIZE;
2623 if (slot_map[XY2OFS(x, y)] == 0 && onScreen(scr, tx, ty, sx, ex, sy, ey)) {
2624 *x_pos = x;
2625 *y_pos = y;
2626 done = 1;
2627 break;
2629 x = i;
2630 tx = dock->x_pos + x * ICON_SIZE;
2631 if (slot_map[XY2OFS(x, y)] == 0 && onScreen(scr, tx, ty, sx, ex, sy, ey)) {
2632 *x_pos = x;
2633 *y_pos = y;
2634 done = 1;
2635 break;
2639 wfree(slot_map);
2640 #undef XY2OFS
2641 return done;
2644 static void moveDock(WDock *dock, int new_x, int new_y)
2646 WAppIcon *btn;
2647 int i;
2649 dock->x_pos = new_x;
2650 dock->y_pos = new_y;
2651 for (i = 0; i < dock->max_icons; i++) {
2652 btn = dock->icon_array[i];
2653 if (btn) {
2654 btn->x_pos = new_x + btn->xindex * ICON_SIZE;
2655 btn->y_pos = new_y + btn->yindex * ICON_SIZE;
2656 XMoveWindow(dpy, btn->icon->core->window, btn->x_pos, btn->y_pos);
2661 static void swapDock(WDock *dock)
2663 WScreen *scr = dock->screen_ptr;
2664 WAppIcon *btn;
2665 int x, i;
2667 if (dock->on_right_side) {
2668 x = dock->x_pos = scr->scr_width - ICON_SIZE - DOCK_EXTRA_SPACE;
2669 } else {
2670 x = dock->x_pos = DOCK_EXTRA_SPACE;
2673 for (i = 0; i < dock->max_icons; i++) {
2674 btn = dock->icon_array[i];
2675 if (btn) {
2676 btn->x_pos = x;
2677 XMoveWindow(dpy, btn->icon->core->window, btn->x_pos, btn->y_pos);
2681 wScreenUpdateUsableArea(scr);
2684 static pid_t execCommand(WAppIcon *btn, char *command, WSavedState *state)
2686 WScreen *scr = btn->icon->core->screen_ptr;
2687 pid_t pid;
2688 char **argv;
2689 int argc;
2690 char *cmdline;
2692 cmdline = ExpandOptions(scr, command);
2694 if (scr->flags.dnd_data_convertion_status || !cmdline) {
2695 if (cmdline)
2696 wfree(cmdline);
2697 if (state)
2698 wfree(state);
2699 return 0;
2702 wtokensplit(cmdline, &argv, &argc);
2704 if (!argc) {
2705 if (cmdline)
2706 wfree(cmdline);
2707 if (state)
2708 wfree(state);
2709 return 0;
2712 if ((pid = fork()) == 0) {
2713 char **args;
2714 int i;
2716 SetupEnvironment(scr);
2718 #ifdef HAVE_SETSID
2719 setsid();
2720 #endif
2722 args = malloc(sizeof(char *) * (argc + 1));
2723 if (!args)
2724 exit(111);
2725 for (i = 0; i < argc; i++) {
2726 args[i] = argv[i];
2728 args[argc] = NULL;
2729 execvp(argv[0], args);
2730 exit(111);
2732 wtokenfree(argv, argc);
2734 if (pid > 0) {
2735 if (!state) {
2736 state = wmalloc(sizeof(WSavedState));
2737 state->hidden = -1;
2738 state->miniaturized = -1;
2739 state->shaded = -1;
2740 if (btn->dock == scr->dock || btn->omnipresent)
2741 state->workspace = -1;
2742 else
2743 state->workspace = scr->current_workspace;
2745 wWindowAddSavedState(btn->wm_instance, btn->wm_class, cmdline, pid, state);
2746 wAddDeathHandler(pid, (WDeathHandler *) trackDeadProcess, btn->dock);
2747 } else if (state) {
2748 wfree(state);
2750 wfree(cmdline);
2751 return pid;
2754 void wDockHideIcons(WDock *dock)
2756 int i;
2758 if (dock == NULL)
2759 return;
2761 for (i = 1; i < dock->max_icons; i++) {
2762 if (dock->icon_array[i])
2763 XUnmapWindow(dpy, dock->icon_array[i]->icon->core->window);
2765 dock->mapped = 0;
2767 dockIconPaint(dock->icon_array[0]);
2770 void wDockShowIcons(WDock *dock)
2772 int i, newlevel;
2773 WAppIcon *btn;
2775 if (dock == NULL)
2776 return;
2778 btn = dock->icon_array[0];
2779 moveDock(dock, btn->x_pos, btn->y_pos);
2781 newlevel = dock->lowered ? WMNormalLevel : WMDockLevel;
2782 ChangeStackingLevel(btn->icon->core, newlevel);
2784 for (i = 1; i < dock->max_icons; i++) {
2785 if (dock->icon_array[i]) {
2786 MoveInStackListAbove(dock->icon_array[i]->icon->core, btn->icon->core);
2787 break;
2791 if (!dock->collapsed) {
2792 for (i = 1; i < dock->max_icons; i++) {
2793 if (dock->icon_array[i]) {
2794 XMapWindow(dpy, dock->icon_array[i]->icon->core->window);
2798 dock->mapped = 1;
2800 dockIconPaint(btn);
2803 void wDockLower(WDock *dock)
2805 int i;
2807 for (i = 0; i < dock->max_icons; i++) {
2808 if (dock->icon_array[i])
2809 wLowerFrame(dock->icon_array[i]->icon->core);
2813 void wDockRaise(WDock *dock)
2815 int i;
2817 for (i = dock->max_icons - 1; i >= 0; i--) {
2818 if (dock->icon_array[i])
2819 wRaiseFrame(dock->icon_array[i]->icon->core);
2823 void wDockRaiseLower(WDock *dock)
2825 if (!dock->icon_array[0]->icon->core->stacking->above
2826 || (dock->icon_array[0]->icon->core->stacking->window_level
2827 != dock->icon_array[0]->icon->core->stacking->above->stacking->window_level))
2828 wDockLower(dock);
2829 else
2830 wDockRaise(dock);
2833 void wDockFinishLaunch(WDock *dock, WAppIcon *icon)
2835 icon->launching = 0;
2836 icon->relaunching = 0;
2837 dockIconPaint(icon);
2840 WAppIcon *wDockFindIconForWindow(WDock *dock, Window window)
2842 WAppIcon *icon;
2843 int i;
2845 for (i = 0; i < dock->max_icons; i++) {
2846 icon = dock->icon_array[i];
2847 if (icon && icon->main_window == window)
2848 return icon;
2850 return NULL;
2853 void wDockTrackWindowLaunch(WDock *dock, Window window)
2855 WAppIcon *icon;
2856 char *wm_class, *wm_instance;
2857 int i;
2858 Bool firstPass = True;
2859 Bool found = False;
2860 char *command = NULL;
2862 if (!PropGetWMClass(window, &wm_class, &wm_instance) || (!wm_class && !wm_instance)) {
2863 return;
2866 command = GetCommandForWindow(window);
2867 retry:
2868 for (i = 0; i < dock->max_icons; i++) {
2869 icon = dock->icon_array[i];
2870 if (!icon)
2871 continue;
2873 /* app is already attached to icon */
2874 if (icon->main_window == window) {
2875 found = True;
2876 break;
2879 if ((icon->wm_instance || icon->wm_class)
2880 && (icon->launching || !icon->running)) {
2882 if (icon->wm_instance && wm_instance && strcmp(icon->wm_instance, wm_instance) != 0) {
2883 continue;
2885 if (icon->wm_class && wm_class && strcmp(icon->wm_class, wm_class) != 0) {
2886 continue;
2888 if (firstPass && command && strcmp(icon->command, command) != 0) {
2889 continue;
2892 if (!icon->relaunching) {
2893 WApplication *wapp;
2895 /* Possibly an application that was docked with dockit,
2896 * but the user did not update WMState to indicate that
2897 * it was docked by force */
2898 wapp = wApplicationOf(window);
2899 if (!wapp) {
2900 icon->forced_dock = 1;
2901 icon->running = 0;
2903 if (!icon->forced_dock) {
2904 icon->main_window = window;
2907 found = True;
2908 if (!wPreferences.no_animations && !icon->launching &&
2909 !dock->screen_ptr->flags.startup && !dock->collapsed) {
2910 WAppIcon *aicon;
2911 int x0, y0;
2913 icon->launching = 1;
2914 dockIconPaint(icon);
2916 aicon = wAppIconCreateForDock(dock->screen_ptr, NULL,
2917 wm_instance, wm_class, TILE_NORMAL);
2918 /* XXX: can: aicon->icon == NULL ? */
2919 PlaceIcon(dock->screen_ptr, &x0, &y0, wGetHeadForWindow(aicon->icon->owner));
2920 wAppIconMove(aicon, x0, y0);
2921 /* Should this always be lowered? -Dan */
2922 if (dock->lowered)
2923 wLowerFrame(aicon->icon->core);
2924 XMapWindow(dpy, aicon->icon->core->window);
2925 aicon->launching = 1;
2926 wAppIconPaint(aicon);
2927 SlideWindow(aicon->icon->core->window, x0, y0, icon->x_pos, icon->y_pos);
2928 XUnmapWindow(dpy, aicon->icon->core->window);
2929 wAppIconDestroy(aicon);
2931 wDockFinishLaunch(dock, icon);
2932 break;
2936 if (firstPass && !found) {
2937 firstPass = False;
2938 goto retry;
2941 if (command)
2942 wfree(command);
2944 if (wm_class)
2945 free(wm_class);
2946 if (wm_instance)
2947 free(wm_instance);
2950 void wClipUpdateForWorkspaceChange(WScreen *scr, int workspace)
2952 if (!wPreferences.flags.noclip) {
2953 scr->clip_icon->dock = scr->workspaces[workspace]->clip;
2954 if (scr->current_workspace != workspace) {
2955 WDock *old_clip = scr->workspaces[scr->current_workspace]->clip;
2956 WAppIconChain *chain = scr->global_icons;
2958 while (chain) {
2959 moveIconBetweenDocks(chain->aicon->dock,
2960 scr->workspaces[workspace]->clip,
2961 chain->aicon, chain->aicon->xindex, chain->aicon->yindex);
2962 if (scr->workspaces[workspace]->clip->collapsed)
2963 XUnmapWindow(dpy, chain->aicon->icon->core->window);
2964 chain = chain->next;
2967 wDockHideIcons(old_clip);
2968 if (old_clip->auto_raise_lower) {
2969 if (old_clip->auto_raise_magic) {
2970 WMDeleteTimerHandler(old_clip->auto_raise_magic);
2971 old_clip->auto_raise_magic = NULL;
2973 wDockLower(old_clip);
2975 if (old_clip->auto_collapse) {
2976 if (old_clip->auto_expand_magic) {
2977 WMDeleteTimerHandler(old_clip->auto_expand_magic);
2978 old_clip->auto_expand_magic = NULL;
2980 old_clip->collapsed = 1;
2982 wDockShowIcons(scr->workspaces[workspace]->clip);
2987 static void trackDeadProcess(pid_t pid, unsigned char status, WDock *dock)
2989 WAppIcon *icon;
2990 int i;
2992 for (i = 0; i < dock->max_icons; i++) {
2993 icon = dock->icon_array[i];
2994 if (!icon)
2995 continue;
2997 if (icon->launching && icon->pid == pid) {
2998 if (!icon->relaunching) {
2999 icon->running = 0;
3000 icon->main_window = None;
3002 wDockFinishLaunch(dock, icon);
3003 icon->pid = 0;
3004 if (status == 111) {
3005 char msg[PATH_MAX];
3006 char *cmd;
3008 #ifdef XDND
3009 if (icon->drop_launch)
3010 cmd = icon->dnd_command;
3011 else
3012 #endif
3013 if (icon->paste_launch)
3014 cmd = icon->paste_command;
3015 else
3016 cmd = icon->command;
3018 snprintf(msg, sizeof(msg), _("Could not execute command \"%s\""), cmd);
3020 wMessageDialog(dock->screen_ptr, _("Error"), msg, _("OK"), NULL, NULL);
3022 break;
3027 static void toggleLowered(WDock *dock)
3029 WAppIcon *tmp;
3030 int newlevel, i;
3032 /* lower/raise Dock */
3033 if (!dock->lowered) {
3034 newlevel = WMNormalLevel;
3035 dock->lowered = 1;
3036 } else {
3037 newlevel = WMDockLevel;
3038 dock->lowered = 0;
3041 for (i = 0; i < dock->max_icons; i++) {
3042 tmp = dock->icon_array[i];
3043 if (!tmp)
3044 continue;
3046 ChangeStackingLevel(tmp->icon->core, newlevel);
3047 if (dock->lowered)
3048 wLowerFrame(tmp->icon->core);
3051 if (dock->type == WM_DOCK)
3052 wScreenUpdateUsableArea(dock->screen_ptr);
3055 static void toggleCollapsed(WDock *dock)
3057 if (dock->collapsed) {
3058 dock->collapsed = 0;
3059 wDockShowIcons(dock);
3060 } else {
3061 dock->collapsed = 1;
3062 wDockHideIcons(dock);
3066 static void openDockMenu(WDock *dock, WAppIcon *aicon, XEvent *event)
3068 WScreen *scr = dock->screen_ptr;
3069 WObjDescriptor *desc;
3070 WMenuEntry *entry;
3071 WApplication *wapp = NULL;
3072 int index = 0;
3073 int x_pos;
3074 int n_selected;
3075 int appIsRunning = aicon->running && aicon->icon && aicon->icon->owner;
3077 if (dock->type == WM_DOCK) {
3078 /* keep on top */
3079 entry = dock->menu->entries[index];
3080 entry->flags.indicator_on = !dock->lowered;
3081 entry->clientdata = dock;
3082 dock->menu->flags.realized = 0;
3083 } else {
3084 /* clip options */
3085 if (scr->clip_options)
3086 updateClipOptionsMenu(scr->clip_options, dock);
3088 n_selected = numberOfSelectedIcons(dock);
3090 /* Rename Workspace */
3091 entry = dock->menu->entries[++index];
3092 if (aicon == scr->clip_icon) {
3093 entry->callback = renameCallback;
3094 entry->clientdata = dock;
3095 entry->flags.indicator = 0;
3096 entry->text = _("Rename Workspace");
3097 } else {
3098 entry->callback = omnipresentCallback;
3099 entry->clientdata = aicon;
3100 if (n_selected > 0) {
3101 entry->flags.indicator = 0;
3102 entry->text = _("Toggle Omnipresent");
3103 } else {
3104 entry->flags.indicator = 1;
3105 entry->flags.indicator_on = aicon->omnipresent;
3106 entry->flags.indicator_type = MI_CHECK;
3107 entry->text = _("Omnipresent");
3111 /* select/unselect icon */
3112 entry = dock->menu->entries[++index];
3113 entry->clientdata = aicon;
3114 entry->flags.indicator_on = aicon->icon->selected;
3115 wMenuSetEnabled(dock->menu, index, aicon != scr->clip_icon);
3117 /* select/unselect all icons */
3118 entry = dock->menu->entries[++index];
3119 entry->clientdata = aicon;
3120 if (n_selected > 0)
3121 entry->text = _("Unselect All Icons");
3122 else
3123 entry->text = _("Select All Icons");
3124 wMenuSetEnabled(dock->menu, index, dock->icon_count > 1);
3126 /* keep icon(s) */
3127 entry = dock->menu->entries[++index];
3128 entry->clientdata = aicon;
3129 if (n_selected > 1)
3130 entry->text = _("Keep Icons");
3131 else
3132 entry->text = _("Keep Icon");
3133 wMenuSetEnabled(dock->menu, index, dock->icon_count > 1);
3135 /* this is the workspace submenu part */
3136 entry = dock->menu->entries[++index];
3137 if (n_selected > 1)
3138 entry->text = _("Move Icons To");
3139 else
3140 entry->text = _("Move Icon To");
3141 if (scr->clip_submenu)
3142 updateWorkspaceMenu(scr->clip_submenu, aicon);
3143 wMenuSetEnabled(dock->menu, index, !aicon->omnipresent);
3145 /* remove icon(s) */
3146 entry = dock->menu->entries[++index];
3147 entry->clientdata = aicon;
3148 if (n_selected > 1)
3149 entry->text = _("Remove Icons");
3150 else
3151 entry->text = _("Remove Icon");
3152 wMenuSetEnabled(dock->menu, index, dock->icon_count > 1);
3154 /* attract icon(s) */
3155 entry = dock->menu->entries[++index];
3156 entry->clientdata = aicon;
3158 dock->menu->flags.realized = 0;
3159 wMenuRealize(dock->menu);
3162 if (aicon->icon->owner) {
3163 wapp = wApplicationOf(aicon->icon->owner->main_window);
3164 } else {
3165 wapp = NULL;
3168 /* launch */
3169 entry = dock->menu->entries[++index];
3170 entry->clientdata = aicon;
3171 wMenuSetEnabled(dock->menu, index, aicon->command != NULL);
3173 /* unhide here */
3174 entry = dock->menu->entries[++index];
3175 entry->clientdata = aicon;
3176 if (wapp && wapp->flags.hidden) {
3177 entry->text = _("Unhide Here");
3178 } else {
3179 entry->text = _("Bring Here");
3181 wMenuSetEnabled(dock->menu, index, appIsRunning);
3183 /* hide */
3184 entry = dock->menu->entries[++index];
3185 entry->clientdata = aicon;
3186 if (wapp && wapp->flags.hidden) {
3187 entry->text = _("Unhide");
3188 } else {
3189 entry->text = _("Hide");
3191 wMenuSetEnabled(dock->menu, index, appIsRunning);
3193 /* settings */
3194 entry = dock->menu->entries[++index];
3195 entry->clientdata = aicon;
3196 wMenuSetEnabled(dock->menu, index, !aicon->editing && !wPreferences.flags.noupdates);
3198 /* kill */
3199 entry = dock->menu->entries[++index];
3200 entry->clientdata = aicon;
3201 wMenuSetEnabled(dock->menu, index, appIsRunning);
3203 if (!dock->menu->flags.realized)
3204 wMenuRealize(dock->menu);
3206 if (dock->type == WM_CLIP) {
3207 /*x_pos = event->xbutton.x_root+2; */
3208 x_pos = event->xbutton.x_root - dock->menu->frame->core->width / 2 - 1;
3209 if (x_pos < 0) {
3210 x_pos = 0;
3211 } else if (x_pos + dock->menu->frame->core->width > scr->scr_width - 2) {
3212 x_pos = scr->scr_width - dock->menu->frame->core->width - 4;
3214 } else {
3215 x_pos = dock->on_right_side ? scr->scr_width - dock->menu->frame->core->width - 3 : 0;
3218 wMenuMapAt(dock->menu, x_pos, event->xbutton.y_root + 2, False);
3220 /* allow drag select */
3221 event->xany.send_event = True;
3222 desc = &dock->menu->menu->descriptor;
3223 (*desc->handle_mousedown) (desc, event);
3226 /******************************************************************/
3227 static void iconDblClick(WObjDescriptor *desc, XEvent *event)
3229 WAppIcon *btn = desc->parent;
3230 WDock *dock = btn->dock;
3231 WApplication *wapp = NULL;
3232 int unhideHere = 0;
3234 if (btn->icon->owner && !(event->xbutton.state & ControlMask)) {
3235 wapp = wApplicationOf(btn->icon->owner->main_window);
3237 assert(wapp != NULL);
3239 unhideHere = (event->xbutton.state & ShiftMask);
3241 /* go to the last workspace that the user worked on the app */
3242 if (wapp->last_workspace != dock->screen_ptr->current_workspace && !unhideHere) {
3243 wWorkspaceChange(dock->screen_ptr, wapp->last_workspace);
3246 wUnhideApplication(wapp, event->xbutton.button == Button2, unhideHere);
3248 if (event->xbutton.state & MOD_MASK) {
3249 wHideOtherApplications(btn->icon->owner);
3251 } else {
3252 if (event->xbutton.button == Button1) {
3254 if (event->xbutton.state & MOD_MASK) {
3255 /* raise/lower dock */
3256 toggleLowered(dock);
3257 } else if (btn == dock->screen_ptr->clip_icon) {
3258 if (getClipButton(event->xbutton.x, event->xbutton.y) == CLIP_IDLE)
3259 toggleCollapsed(dock);
3260 else
3261 handleClipChangeWorkspace(dock->screen_ptr, event);
3262 } else if (btn->command) {
3263 if (!btn->launching && (!btn->running || (event->xbutton.state & ControlMask)))
3264 launchDockedApplication(btn, False);
3270 static void handleDockMove(WDock *dock, WAppIcon *aicon, XEvent *event)
3272 WScreen *scr = dock->screen_ptr;
3273 int ofs_x = event->xbutton.x, ofs_y = event->xbutton.y;
3274 int x, y;
3275 XEvent ev;
3276 int grabbed = 0, swapped = 0, done;
3277 Pixmap ghost = None;
3278 int superfluous = wPreferences.superfluous; /* we catch it to avoid problems */
3280 if (XGrabPointer(dpy, aicon->icon->core->window, True, ButtonMotionMask
3281 | ButtonReleaseMask | ButtonPressMask, GrabModeAsync,
3282 GrabModeAsync, None, None, CurrentTime) != GrabSuccess) {
3283 wwarning("pointer grab failed for dock move");
3285 y = 0;
3286 for (x = 0; x < dock->max_icons; x++) {
3287 if (dock->icon_array[x] != NULL && dock->icon_array[x]->yindex > y)
3288 y = dock->icon_array[x]->yindex;
3290 y++;
3291 XResizeWindow(dpy, scr->dock_shadow, ICON_SIZE, ICON_SIZE * y);
3293 done = 0;
3294 while (!done) {
3295 WMMaskEvent(dpy, PointerMotionMask | ButtonReleaseMask | ButtonPressMask
3296 | ButtonMotionMask | ExposureMask | EnterWindowMask, &ev);
3297 switch (ev.type) {
3298 case Expose:
3299 WMHandleEvent(&ev);
3300 break;
3302 case EnterNotify:
3303 /* It means the cursor moved so fast that it entered
3304 * something else (if moving slowly, it would have
3305 * stayed in the dock that is being moved. Ignore such
3306 * "spurious" EnterNotifiy's */
3307 break;
3309 case MotionNotify:
3310 if (!grabbed) {
3311 if (abs(ofs_x - ev.xmotion.x) >= MOVE_THRESHOLD
3312 || abs(ofs_y - ev.xmotion.y) >= MOVE_THRESHOLD) {
3313 XChangeActivePointerGrab(dpy, ButtonMotionMask
3314 | ButtonReleaseMask | ButtonPressMask,
3315 wCursor[WCUR_MOVE], CurrentTime);
3316 grabbed = 1;
3318 break;
3320 if (dock->type == WM_CLIP) {
3321 x = ev.xmotion.x_root - ofs_x;
3322 y = ev.xmotion.y_root - ofs_y;
3323 wScreenKeepInside(scr, &x, &y, ICON_SIZE, ICON_SIZE);
3325 moveDock(dock, x, y);
3326 } else {
3327 /* move vertically if pointer is inside the dock */
3328 if ((dock->on_right_side && ev.xmotion.x_root >= dock->x_pos - ICON_SIZE)
3329 || (!dock->on_right_side && ev.xmotion.x_root <= dock->x_pos + ICON_SIZE * 2)) {
3331 x = ev.xmotion.x_root - ofs_x;
3332 y = ev.xmotion.y_root - ofs_y;
3333 wScreenKeepInside(scr, &x, &y, ICON_SIZE, ICON_SIZE);
3334 moveDock(dock, dock->x_pos, y);
3336 /* move horizontally to change sides */
3337 x = ev.xmotion.x_root - ofs_x;
3338 if (!dock->on_right_side) {
3340 /* is on left */
3342 if (ev.xmotion.x_root > dock->x_pos + ICON_SIZE * 2) {
3343 XMoveWindow(dpy, scr->dock_shadow, scr->scr_width - ICON_SIZE
3344 - DOCK_EXTRA_SPACE - 1, dock->y_pos);
3345 if (superfluous && ghost == None) {
3346 ghost = MakeGhostDock(dock, dock->x_pos,
3347 scr->scr_width - ICON_SIZE
3348 - DOCK_EXTRA_SPACE - 1, dock->y_pos);
3349 XSetWindowBackgroundPixmap(dpy, scr->dock_shadow, ghost);
3350 XClearWindow(dpy, scr->dock_shadow);
3352 XMapRaised(dpy, scr->dock_shadow);
3353 swapped = 1;
3354 } else {
3355 if (superfluous && ghost != None) {
3356 XFreePixmap(dpy, ghost);
3357 ghost = None;
3359 XUnmapWindow(dpy, scr->dock_shadow);
3360 swapped = 0;
3362 } else {
3363 /* is on right */
3364 if (ev.xmotion.x_root < dock->x_pos - ICON_SIZE) {
3365 XMoveWindow(dpy, scr->dock_shadow, DOCK_EXTRA_SPACE, dock->y_pos);
3366 if (superfluous && ghost == None) {
3367 ghost = MakeGhostDock(dock, dock->x_pos,
3368 DOCK_EXTRA_SPACE, dock->y_pos);
3369 XSetWindowBackgroundPixmap(dpy, scr->dock_shadow, ghost);
3370 XClearWindow(dpy, scr->dock_shadow);
3372 XMapRaised(dpy, scr->dock_shadow);
3373 swapped = -1;
3374 } else {
3375 XUnmapWindow(dpy, scr->dock_shadow);
3376 swapped = 0;
3377 if (superfluous && ghost != None) {
3378 XFreePixmap(dpy, ghost);
3379 ghost = None;
3384 break;
3386 case ButtonPress:
3387 break;
3389 case ButtonRelease:
3390 if (ev.xbutton.button != event->xbutton.button)
3391 break;
3392 XUngrabPointer(dpy, CurrentTime);
3393 XUnmapWindow(dpy, scr->dock_shadow);
3394 XResizeWindow(dpy, scr->dock_shadow, ICON_SIZE, ICON_SIZE);
3395 if (dock->type == WM_DOCK) {
3396 if (swapped != 0) {
3397 if (swapped > 0)
3398 dock->on_right_side = 1;
3399 else
3400 dock->on_right_side = 0;
3401 swapDock(dock);
3402 wArrangeIcons(scr, False);
3405 done = 1;
3406 break;
3409 if (superfluous) {
3410 if (ghost != None)
3411 XFreePixmap(dpy, ghost);
3412 XSetWindowBackground(dpy, scr->dock_shadow, scr->white_pixel);
3416 static Bool handleIconMove(WDock *dock, WAppIcon *aicon, XEvent *event)
3418 WScreen *scr = dock->screen_ptr;
3419 Window wins[2];
3420 WIcon *icon = aicon->icon;
3421 WDock *dock2 = NULL, *last_dock = dock, *clip = NULL;
3422 int ondock, grabbed = 0, change_dock = 0, collapsed = 0;
3423 XEvent ev;
3424 int x = aicon->x_pos, y = aicon->y_pos;
3425 int ofs_x = event->xbutton.x, ofs_y = event->xbutton.y;
3426 int shad_x = x, shad_y = y;
3427 int ix = aicon->xindex, iy = aicon->yindex;
3428 int tmp;
3429 Pixmap ghost = None;
3430 Bool docked;
3431 int superfluous = wPreferences.superfluous; /* we catch it to avoid problems */
3432 int omnipresent = aicon->omnipresent; /* this must be cached!!! */
3433 Bool hasMoved = False;
3435 if (wPreferences.flags.noupdates)
3436 return hasMoved;
3438 if (XGrabPointer(dpy, icon->core->window, True, ButtonMotionMask
3439 | ButtonReleaseMask | ButtonPressMask, GrabModeAsync,
3440 GrabModeAsync, None, None, CurrentTime) != GrabSuccess) {
3443 if (!(event->xbutton.state & MOD_MASK))
3444 wRaiseFrame(icon->core);
3446 if (!wPreferences.flags.noclip)
3447 clip = scr->workspaces[scr->current_workspace]->clip;
3449 if (dock == scr->dock && !wPreferences.flags.noclip)
3450 dock2 = clip;
3451 else if (dock != scr->dock && !wPreferences.flags.nodock)
3452 dock2 = scr->dock;
3454 wins[0] = icon->core->window;
3455 wins[1] = scr->dock_shadow;
3456 XRestackWindows(dpy, wins, 2);
3457 XMoveResizeWindow(dpy, scr->dock_shadow, aicon->x_pos, aicon->y_pos, ICON_SIZE, ICON_SIZE);
3458 if (superfluous) {
3459 if (icon->pixmap != None)
3460 ghost = MakeGhostIcon(scr, icon->pixmap);
3461 else
3462 ghost = MakeGhostIcon(scr, icon->core->window);
3464 XSetWindowBackgroundPixmap(dpy, scr->dock_shadow, ghost);
3465 XClearWindow(dpy, scr->dock_shadow);
3467 XMapWindow(dpy, scr->dock_shadow);
3469 ondock = 1;
3471 while (1) {
3472 XMaskEvent(dpy, PointerMotionMask | ButtonReleaseMask | ButtonPressMask
3473 | ButtonMotionMask | ExposureMask, &ev);
3474 switch (ev.type) {
3475 case Expose:
3476 WMHandleEvent(&ev);
3477 break;
3479 case MotionNotify:
3480 hasMoved = True;
3481 if (!grabbed) {
3482 if (abs(ofs_x - ev.xmotion.x) >= MOVE_THRESHOLD
3483 || abs(ofs_y - ev.xmotion.y) >= MOVE_THRESHOLD) {
3484 XChangeActivePointerGrab(dpy, ButtonMotionMask
3485 | ButtonReleaseMask | ButtonPressMask,
3486 wCursor[WCUR_MOVE], CurrentTime);
3487 grabbed = 1;
3488 } else {
3489 break;
3493 if (omnipresent) {
3494 int i;
3495 for (i = 0; i < scr->workspace_count; i++) {
3496 if (i == scr->current_workspace)
3497 continue;
3498 wDockShowIcons(scr->workspaces[i]->clip);
3502 x = ev.xmotion.x_root - ofs_x;
3503 y = ev.xmotion.y_root - ofs_y;
3504 tmp = wDockSnapIcon(dock, aicon, x, y, &ix, &iy, True);
3505 if (tmp && dock2) {
3506 change_dock = 0;
3507 if (last_dock != dock && collapsed) {
3508 last_dock->collapsed = 1;
3509 wDockHideIcons(last_dock);
3510 collapsed = 0;
3512 if (!collapsed && (collapsed = dock->collapsed)) {
3513 dock->collapsed = 0;
3514 wDockShowIcons(dock);
3516 if (dock->auto_raise_lower)
3517 wDockRaise(dock);
3518 last_dock = dock;
3519 } else if (dock2) {
3520 tmp = wDockSnapIcon(dock2, aicon, x, y, &ix, &iy, False);
3521 if (tmp) {
3522 change_dock = 1;
3523 if (last_dock != dock2 && collapsed) {
3524 last_dock->collapsed = 1;
3525 wDockHideIcons(last_dock);
3526 collapsed = 0;
3528 if (!collapsed && (collapsed = dock2->collapsed)) {
3529 dock2->collapsed = 0;
3530 wDockShowIcons(dock2);
3532 if (dock2->auto_raise_lower)
3533 wDockRaise(dock2);
3534 last_dock = dock2;
3537 if (aicon->launching || aicon->lock || (aicon->running && !(ev.xmotion.state & MOD_MASK))
3538 || (!aicon->running && tmp)) {
3539 shad_x = last_dock->x_pos + ix * wPreferences.icon_size;
3540 shad_y = last_dock->y_pos + iy * wPreferences.icon_size;
3542 XMoveWindow(dpy, scr->dock_shadow, shad_x, shad_y);
3544 if (!ondock) {
3545 XMapWindow(dpy, scr->dock_shadow);
3547 ondock = 1;
3548 } else {
3549 if (ondock) {
3550 XUnmapWindow(dpy, scr->dock_shadow);
3552 ondock = 0;
3554 XMoveWindow(dpy, icon->core->window, x, y);
3555 break;
3557 case ButtonPress:
3558 break;
3560 case ButtonRelease:
3561 if (ev.xbutton.button != event->xbutton.button)
3562 break;
3563 XUngrabPointer(dpy, CurrentTime);
3564 if (ondock) {
3565 SlideWindow(icon->core->window, x, y, shad_x, shad_y);
3566 XUnmapWindow(dpy, scr->dock_shadow);
3567 if (!change_dock) {
3568 reattachIcon(dock, aicon, ix, iy);
3569 if (clip && dock != clip && clip->auto_raise_lower)
3570 wDockLower(clip);
3571 } else {
3572 docked = moveIconBetweenDocks(dock, dock2, aicon, ix, iy);
3573 if (!docked) {
3574 /* Slide it back if dock rejected it */
3575 SlideWindow(icon->core->window, x, y, aicon->x_pos, aicon->y_pos);
3576 reattachIcon(dock, aicon, aicon->xindex, aicon->yindex);
3578 if (last_dock->type == WM_CLIP && last_dock->auto_collapse) {
3579 collapsed = 0;
3582 } else {
3583 aicon->x_pos = x;
3584 aicon->y_pos = y;
3585 if (superfluous) {
3586 if (!aicon->running && !wPreferences.no_animations) {
3587 /* We need to deselect it, even if is deselected in
3588 * wDockDetach(), because else DoKaboom() will fail.
3590 if (aicon->icon->selected)
3591 wIconSelect(aicon->icon);
3593 DoKaboom(scr, aicon->icon->core->window, x, y);
3596 if (clip && clip->auto_raise_lower)
3597 wDockLower(clip);
3598 wDockDetach(dock, aicon);
3600 if (collapsed) {
3601 last_dock->collapsed = 1;
3602 wDockHideIcons(last_dock);
3603 collapsed = 0;
3605 if (superfluous) {
3606 if (ghost != None)
3607 XFreePixmap(dpy, ghost);
3608 XSetWindowBackground(dpy, scr->dock_shadow, scr->white_pixel);
3610 if (omnipresent) {
3611 int i;
3612 for (i = 0; i < scr->workspace_count; i++) {
3613 if (i == scr->current_workspace)
3614 continue;
3615 wDockHideIcons(scr->workspaces[i]->clip);
3618 return hasMoved;;
3621 return False; /* never reached */
3624 static int getClipButton(int px, int py)
3626 int pt = (CLIP_BUTTON_SIZE + 2) * ICON_SIZE / 64;
3628 if (px < 0 || py < 0 || px >= ICON_SIZE || py >= ICON_SIZE)
3629 return CLIP_IDLE;
3631 if (py <= pt - ((int)ICON_SIZE - 1 - px))
3632 return CLIP_FORWARD;
3633 else if (px <= pt - ((int)ICON_SIZE - 1 - py))
3634 return CLIP_REWIND;
3636 return CLIP_IDLE;
3639 static void handleClipChangeWorkspace(WScreen *scr, XEvent *event)
3641 XEvent ev;
3642 int done, direction, new_ws;
3643 int new_dir;
3644 WDock *clip = scr->clip_icon->dock;
3646 direction = getClipButton(event->xbutton.x, event->xbutton.y);
3648 clip->lclip_button_pushed = direction == CLIP_REWIND;
3649 clip->rclip_button_pushed = direction == CLIP_FORWARD;
3651 wClipIconPaint(scr->clip_icon);
3652 done = 0;
3653 while (!done) {
3654 WMMaskEvent(dpy, ExposureMask | ButtonMotionMask | ButtonReleaseMask | ButtonPressMask, &ev);
3655 switch (ev.type) {
3656 case Expose:
3657 WMHandleEvent(&ev);
3658 break;
3660 case MotionNotify:
3661 new_dir = getClipButton(ev.xmotion.x, ev.xmotion.y);
3662 if (new_dir != direction) {
3663 direction = new_dir;
3664 clip->lclip_button_pushed = direction == CLIP_REWIND;
3665 clip->rclip_button_pushed = direction == CLIP_FORWARD;
3666 wClipIconPaint(scr->clip_icon);
3668 break;
3670 case ButtonPress:
3671 break;
3673 case ButtonRelease:
3674 if (ev.xbutton.button == event->xbutton.button)
3675 done = 1;
3679 clip->lclip_button_pushed = 0;
3680 clip->rclip_button_pushed = 0;
3682 new_ws = wPreferences.ws_advance || (event->xbutton.state & ControlMask);
3684 if (direction == CLIP_FORWARD) {
3685 if (scr->current_workspace < scr->workspace_count - 1)
3686 wWorkspaceChange(scr, scr->current_workspace + 1);
3687 else if (new_ws && scr->current_workspace < MAX_WORKSPACES - 1)
3688 wWorkspaceChange(scr, scr->current_workspace + 1);
3689 else if (wPreferences.ws_cycle)
3690 wWorkspaceChange(scr, 0);
3691 } else if (direction == CLIP_REWIND) {
3692 if (scr->current_workspace > 0)
3693 wWorkspaceChange(scr, scr->current_workspace - 1);
3694 else if (scr->current_workspace == 0 && wPreferences.ws_cycle)
3695 wWorkspaceChange(scr, scr->workspace_count - 1);
3698 wClipIconPaint(scr->clip_icon);
3701 static void iconMouseDown(WObjDescriptor *desc, XEvent *event)
3703 WAppIcon *aicon = desc->parent;
3704 WDock *dock = aicon->dock;
3705 WScreen *scr = aicon->icon->core->screen_ptr;
3707 if (aicon->editing || WCHECK_STATE(WSTATE_MODAL))
3708 return;
3710 scr->last_dock = dock;
3712 if (dock->menu->flags.mapped)
3713 wMenuUnmap(dock->menu);
3715 if (IsDoubleClick(scr, event)) {
3716 /* double-click was not in the main clip icon */
3717 if (dock->type != WM_CLIP || aicon->xindex != 0 || aicon->yindex != 0
3718 || getClipButton(event->xbutton.x, event->xbutton.y) == CLIP_IDLE) {
3719 iconDblClick(desc, event);
3720 return;
3724 if (event->xbutton.button == Button1) {
3725 if (event->xbutton.state & MOD_MASK)
3726 wDockLower(dock);
3727 else
3728 wDockRaise(dock);
3730 if ((event->xbutton.state & ShiftMask) && aicon != scr->clip_icon && dock->type != WM_DOCK) {
3731 wIconSelect(aicon->icon);
3732 return;
3735 if (aicon->yindex == 0 && aicon->xindex == 0) {
3736 if (getClipButton(event->xbutton.x, event->xbutton.y) != CLIP_IDLE
3737 && dock->type == WM_CLIP)
3738 handleClipChangeWorkspace(scr, event);
3739 else
3740 handleDockMove(dock, aicon, event);
3741 } else {
3742 Bool hasMoved = handleIconMove(dock, aicon, event);
3743 if (wPreferences.single_click && !hasMoved)
3744 iconDblClick(desc, event);
3746 } else if (event->xbutton.button == Button2 && dock->type == WM_CLIP && aicon == scr->clip_icon) {
3747 if (!scr->clip_ws_menu) {
3748 scr->clip_ws_menu = wWorkspaceMenuMake(scr, False);
3750 if (scr->clip_ws_menu) {
3751 WMenu *wsMenu = scr->clip_ws_menu;
3752 int xpos;
3754 wWorkspaceMenuUpdate(scr, wsMenu);
3756 xpos = event->xbutton.x_root - wsMenu->frame->core->width / 2 - 1;
3757 if (xpos < 0) {
3758 xpos = 0;
3759 } else if (xpos + wsMenu->frame->core->width > scr->scr_width - 2) {
3760 xpos = scr->scr_width - wsMenu->frame->core->width - 4;
3762 wMenuMapAt(wsMenu, xpos, event->xbutton.y_root + 2, False);
3764 desc = &wsMenu->menu->descriptor;
3765 event->xany.send_event = True;
3766 (*desc->handle_mousedown) (desc, event);
3768 } else if (event->xbutton.button == Button2 && dock->type == WM_CLIP &&
3769 (event->xbutton.state & ShiftMask) && aicon != scr->clip_icon) {
3770 wClipMakeIconOmnipresent(aicon, !aicon->omnipresent);
3771 } else if (event->xbutton.button == Button3) {
3772 if (event->xbutton.send_event &&
3773 XGrabPointer(dpy, aicon->icon->core->window, True, ButtonMotionMask
3774 | ButtonReleaseMask | ButtonPressMask, GrabModeAsync,
3775 GrabModeAsync, None, None, CurrentTime) != GrabSuccess) {
3776 wwarning("pointer grab failed for dockicon menu");
3777 return;
3780 openDockMenu(dock, aicon, event);
3781 } else if (event->xbutton.button == Button2) {
3782 WAppIcon *btn = desc->parent;
3784 if (!btn->launching && (!btn->running || (event->xbutton.state & ControlMask))) {
3785 launchDockedApplication(btn, True);
3790 static void clipEnterNotify(WObjDescriptor *desc, XEvent *event)
3792 WAppIcon *btn = (WAppIcon *) desc->parent;
3793 WDock *dock;
3794 WScreen *scr;
3796 assert(event->type == EnterNotify);
3798 if (desc->parent_type != WCLASS_DOCK_ICON)
3799 return;
3801 scr = btn->icon->core->screen_ptr;
3802 if (!btn->omnipresent)
3803 dock = btn->dock;
3804 else
3805 dock = scr->workspaces[scr->current_workspace]->clip;
3807 if (!dock || dock->type != WM_CLIP)
3808 return;
3810 /* The auto raise/lower code */
3811 if (dock->auto_lower_magic) {
3812 WMDeleteTimerHandler(dock->auto_lower_magic);
3813 dock->auto_lower_magic = NULL;
3815 if (dock->auto_raise_lower && !dock->auto_raise_magic) {
3816 dock->auto_raise_magic = WMAddTimerHandler(AUTO_RAISE_DELAY, clipAutoRaise, (void *)dock);
3819 /* The auto expand/collapse code */
3820 if (dock->auto_collapse_magic) {
3821 WMDeleteTimerHandler(dock->auto_collapse_magic);
3822 dock->auto_collapse_magic = NULL;
3824 if (dock->auto_collapse && !dock->auto_expand_magic) {
3825 dock->auto_expand_magic = WMAddTimerHandler(AUTO_EXPAND_DELAY, clipAutoExpand, (void *)dock);
3829 static void clipLeave(WDock *dock)
3831 XEvent event;
3832 WObjDescriptor *desc = NULL;
3834 if (!dock || dock->type != WM_CLIP)
3835 return;
3837 if (XCheckTypedEvent(dpy, EnterNotify, &event) != False) {
3838 if (XFindContext(dpy, event.xcrossing.window, wWinContext,
3839 (XPointer *) & desc) != XCNOENT
3840 && desc && desc->parent_type == WCLASS_DOCK_ICON
3841 && ((WAppIcon *) desc->parent)->dock && ((WAppIcon *) desc->parent)->dock->type == WM_CLIP) {
3842 /* We didn't left the Clip yet */
3843 XPutBackEvent(dpy, &event);
3844 return;
3847 XPutBackEvent(dpy, &event);
3848 } else {
3849 /* We entered a withdrawn window, so we're still in Clip */
3850 return;
3853 if (dock->auto_raise_magic) {
3854 WMDeleteTimerHandler(dock->auto_raise_magic);
3855 dock->auto_raise_magic = NULL;
3857 if (dock->auto_raise_lower && !dock->auto_lower_magic) {
3858 dock->auto_lower_magic = WMAddTimerHandler(AUTO_LOWER_DELAY, clipAutoLower, (void *)dock);
3861 if (dock->auto_expand_magic) {
3862 WMDeleteTimerHandler(dock->auto_expand_magic);
3863 dock->auto_expand_magic = NULL;
3865 if (dock->auto_collapse && !dock->auto_collapse_magic) {
3866 dock->auto_collapse_magic = WMAddTimerHandler(AUTO_COLLAPSE_DELAY, clipAutoCollapse, (void *)dock);
3870 static void clipLeaveNotify(WObjDescriptor *desc, XEvent *event)
3872 WAppIcon *btn = (WAppIcon *) desc->parent;
3874 assert(event->type == LeaveNotify);
3876 if (desc->parent_type != WCLASS_DOCK_ICON)
3877 return;
3879 clipLeave(btn->dock);
3882 static void clipAutoCollapse(void *cdata)
3884 WDock *dock = (WDock *) cdata;
3886 if (dock->type != WM_CLIP)
3887 return;
3889 if (dock->auto_collapse) {
3890 dock->collapsed = 1;
3891 wDockHideIcons(dock);
3893 dock->auto_collapse_magic = NULL;
3896 static void clipAutoExpand(void *cdata)
3898 WDock *dock = (WDock *) cdata;
3900 if (dock->type != WM_CLIP)
3901 return;
3903 if (dock->auto_collapse) {
3904 dock->collapsed = 0;
3905 wDockShowIcons(dock);
3907 dock->auto_expand_magic = NULL;
3910 static void clipAutoLower(void *cdata)
3912 WDock *dock = (WDock *) cdata;
3914 if (dock->type != WM_CLIP)
3915 return;
3917 if (dock->auto_raise_lower)
3918 wDockLower(dock);
3920 dock->auto_lower_magic = NULL;
3923 static void clipAutoRaise(void *cdata)
3925 WDock *dock = (WDock *) cdata;
3927 if (dock->type != WM_CLIP)
3928 return;
3930 if (dock->auto_raise_lower)
3931 wDockRaise(dock);
3933 dock->auto_raise_magic = NULL;
3936 static Bool iconCanBeOmnipresent(WAppIcon *aicon)
3938 WScreen *scr = aicon->icon->core->screen_ptr;
3939 WDock *clip;
3940 WAppIcon *btn;
3941 int i, j;
3943 for (i = 0; i < scr->workspace_count; i++) {
3944 clip = scr->workspaces[i]->clip;
3946 if (clip == aicon->dock)
3947 continue;
3949 if (clip->icon_count + scr->global_icon_count >= clip->max_icons)
3950 return False; /* Clip is full in some workspace */
3952 for (j = 0; j < clip->max_icons; j++) {
3953 btn = clip->icon_array[j];
3954 if (btn && btn->xindex == aicon->xindex && btn->yindex == aicon->yindex)
3955 return False;
3959 return True;
3962 int wClipMakeIconOmnipresent(WAppIcon *aicon, int omnipresent)
3964 WScreen *scr = aicon->icon->core->screen_ptr;
3965 WAppIconChain *new_entry, *tmp, *tmp1;
3966 int status = WO_SUCCESS;
3968 if ((scr->dock && aicon->dock == scr->dock) || aicon == scr->clip_icon) {
3969 return WO_NOT_APPLICABLE;
3972 if (aicon->omnipresent == omnipresent)
3973 return WO_SUCCESS;
3975 if (omnipresent) {
3976 if (iconCanBeOmnipresent(aicon)) {
3977 aicon->omnipresent = 1;
3978 new_entry = wmalloc(sizeof(WAppIconChain));
3979 new_entry->aicon = aicon;
3980 new_entry->next = scr->global_icons;
3981 scr->global_icons = new_entry;
3982 scr->global_icon_count++;
3983 } else {
3984 aicon->omnipresent = 0;
3985 status = WO_FAILED;
3987 } else {
3988 aicon->omnipresent = 0;
3989 if (aicon == scr->global_icons->aicon) {
3990 tmp = scr->global_icons->next;
3991 wfree(scr->global_icons);
3992 scr->global_icons = tmp;
3993 scr->global_icon_count--;
3994 } else {
3995 tmp = scr->global_icons;
3996 while (tmp->next) {
3997 if (tmp->next->aicon == aicon) {
3998 tmp1 = tmp->next->next;
3999 wfree(tmp->next);
4000 tmp->next = tmp1;
4001 scr->global_icon_count--;
4002 break;
4004 tmp = tmp->next;
4009 wAppIconPaint(aicon);
4011 return status;