Removed block in restore_icon_state
[wmaker-crm.git] / src / dock.c
blobc90ae939118721c84e473144129bebae88aba925
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 "main.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);
171 wfree(name);
174 static void toggleLoweredCallback(WMenu *menu, WMenuEntry *entry)
176 assert(entry->clientdata != NULL);
178 toggleLowered(entry->clientdata);
180 entry->flags.indicator_on = !(((WDock *) entry->clientdata)->lowered);
182 wMenuPaint(menu);
185 static int matchWindow(const void *item, const void *cdata)
187 return (((WFakeGroupLeader *) item)->leader == (Window) cdata);
190 static void killCallback(WMenu *menu, WMenuEntry *entry)
192 WScreen *scr = menu->menu->screen_ptr;
193 WAppIcon *icon;
194 WFakeGroupLeader *fPtr;
195 char *buffer, *shortname, **argv;
196 char *basename(const char *shortname);
197 int argc;
199 if (!WCHECK_STATE(WSTATE_NORMAL))
200 return;
202 assert(entry->clientdata != NULL);
204 icon = (WAppIcon *) entry->clientdata;
206 icon->editing = 1;
208 WCHANGE_STATE(WSTATE_MODAL);
210 /* strip away dir names */
211 shortname = basename(icon->command);
212 /* separate out command options */
213 wtokensplit(shortname, &argv, &argc);
215 buffer = wstrconcat(argv[0],
216 _(" will be forcibly closed.\n"
217 "Any unsaved changes will be lost.\n" "Please confirm."));
219 if (icon->icon && icon->icon->owner) {
220 fPtr = icon->icon->owner->fake_group;
221 } else {
222 /* is this really necessary? can we kill a non-running dock icon? */
223 Window win = icon->main_window;
224 int index;
226 index = WMFindInArray(scr->fakeGroupLeaders, matchWindow, (void *)win);
227 if (index != WANotFound)
228 fPtr = WMGetFromArray(scr->fakeGroupLeaders, index);
229 else
230 fPtr = NULL;
233 if (wPreferences.dont_confirm_kill
234 || wMessageDialog(menu->frame->screen_ptr, _("Kill Application"),
235 buffer, _("Yes"), _("No"), NULL) == WAPRDefault) {
236 if (fPtr != NULL) {
237 WWindow *wwin, *twin;
239 wwin = scr->focused_window;
240 while (wwin) {
241 twin = wwin->prev;
242 if (wwin->fake_group == fPtr)
243 wClientKill(wwin);
245 wwin = twin;
247 } else if (icon->icon && icon->icon->owner) {
248 wClientKill(icon->icon->owner);
252 wfree(buffer);
253 wtokenfree(argv, argc);
255 icon->editing = 0;
257 WCHANGE_STATE(WSTATE_NORMAL);
260 /* TODO: replace this function with a member of the dock struct */
261 static int numberOfSelectedIcons(WDock *dock)
263 WAppIcon *aicon;
264 int i, n;
266 n = 0;
267 for (i = 1; i < dock->max_icons; i++) {
268 aicon = dock->icon_array[i];
269 if (aicon && aicon->icon->selected)
270 n++;
273 return n;
276 static WMArray *getSelected(WDock *dock)
278 WMArray *ret = WMCreateArray(8);
279 WAppIcon *btn;
280 int i;
282 for (i = 1; i < dock->max_icons; i++) {
283 btn = dock->icon_array[i];
284 if (btn && btn->icon->selected)
285 WMAddToArray(ret, btn);
288 return ret;
291 static void paintClipButtons(WAppIcon *clipIcon, Bool lpushed, Bool rpushed)
293 Window win = clipIcon->icon->core->window;
294 WScreen *scr = clipIcon->icon->core->screen_ptr;
295 XPoint p[4];
296 int pt = CLIP_BUTTON_SIZE * ICON_SIZE / 64;
297 int tp = ICON_SIZE - pt;
298 int as = pt - 15; /* 15 = 5+5+5 */
299 GC gc = scr->draw_gc; /* maybe use WMColorGC() instead here? */
300 WMColor *color;
302 color = scr->clip_title_color[CLIP_NORMAL];
304 XSetForeground(dpy, gc, WMColorPixel(color));
306 if (rpushed) {
307 p[0].x = tp + 1;
308 p[0].y = 1;
309 p[1].x = ICON_SIZE - 2;
310 p[1].y = 1;
311 p[2].x = ICON_SIZE - 2;
312 p[2].y = pt - 1;
313 } else if (lpushed) {
314 p[0].x = 1;
315 p[0].y = tp;
316 p[1].x = pt;
317 p[1].y = ICON_SIZE - 2;
318 p[2].x = 1;
319 p[2].y = ICON_SIZE - 2;
321 if (lpushed || rpushed) {
322 XSetForeground(dpy, scr->draw_gc, scr->white_pixel);
323 XFillPolygon(dpy, win, scr->draw_gc, p, 3, Convex, CoordModeOrigin);
324 XSetForeground(dpy, scr->draw_gc, scr->black_pixel);
327 /* top right arrow */
328 p[0].x = p[3].x = ICON_SIZE - 5 - as;
329 p[0].y = p[3].y = 5;
330 p[1].x = ICON_SIZE - 6;
331 p[1].y = 5;
332 p[2].x = ICON_SIZE - 6;
333 p[2].y = 4 + as;
334 if (rpushed) {
335 XFillPolygon(dpy, win, scr->draw_gc, p, 3, Convex, CoordModeOrigin);
336 XDrawLines(dpy, win, scr->draw_gc, p, 4, CoordModeOrigin);
337 } else {
338 XFillPolygon(dpy, win, gc, p, 3, Convex, CoordModeOrigin);
339 XDrawLines(dpy, win, gc, p, 4, CoordModeOrigin);
342 /* bottom left arrow */
343 p[0].x = p[3].x = 5;
344 p[0].y = p[3].y = ICON_SIZE - 5 - as;
345 p[1].x = 5;
346 p[1].y = ICON_SIZE - 6;
347 p[2].x = 4 + as;
348 p[2].y = ICON_SIZE - 6;
349 if (lpushed) {
350 XFillPolygon(dpy, win, scr->draw_gc, p, 3, Convex, CoordModeOrigin);
351 XDrawLines(dpy, win, scr->draw_gc, p, 4, CoordModeOrigin);
352 } else {
353 XFillPolygon(dpy, win, gc, p, 3, Convex, CoordModeOrigin);
354 XDrawLines(dpy, win, gc, p, 4, CoordModeOrigin);
358 RImage *wClipMakeTile(WScreen *scr, RImage *normalTile)
360 RImage *tile = RCloneImage(normalTile);
361 RColor black;
362 RColor dark;
363 RColor light;
364 int pt, tp;
365 int as;
367 pt = CLIP_BUTTON_SIZE * wPreferences.icon_size / 64;
368 tp = wPreferences.icon_size - 1 - pt;
369 as = pt - 15;
371 black.alpha = 255;
372 black.red = black.green = black.blue = 0;
374 dark.alpha = 0;
375 dark.red = dark.green = dark.blue = 60;
377 light.alpha = 0;
378 light.red = light.green = light.blue = 80;
380 /* top right */
381 ROperateLine(tile, RSubtractOperation, tp, 0, wPreferences.icon_size - 2, pt - 1, &dark);
382 RDrawLine(tile, tp - 1, 0, wPreferences.icon_size - 1, pt + 1, &black);
383 ROperateLine(tile, RAddOperation, tp, 2, wPreferences.icon_size - 3, pt, &light);
385 /* arrow bevel */
386 ROperateLine(tile, RSubtractOperation, ICON_SIZE - 7 - as, 4, ICON_SIZE - 5, 4, &dark);
387 ROperateLine(tile, RSubtractOperation, ICON_SIZE - 6 - as, 5, ICON_SIZE - 5, 6 + as, &dark);
388 ROperateLine(tile, RAddOperation, ICON_SIZE - 5, 4, ICON_SIZE - 5, 6 + as, &light);
390 /* bottom left */
391 ROperateLine(tile, RAddOperation, 2, tp + 2, pt - 2, wPreferences.icon_size - 3, &dark);
392 RDrawLine(tile, 0, tp - 1, pt + 1, wPreferences.icon_size - 1, &black);
393 ROperateLine(tile, RSubtractOperation, 0, tp - 2, pt + 1, wPreferences.icon_size - 2, &light);
395 /* arrow bevel */
396 ROperateLine(tile, RSubtractOperation, 4, ICON_SIZE - 7 - as, 4, ICON_SIZE - 5, &dark);
397 ROperateLine(tile, RSubtractOperation, 5, ICON_SIZE - 6 - as, 6 + as, ICON_SIZE - 5, &dark);
398 ROperateLine(tile, RAddOperation, 4, ICON_SIZE - 5, 6 + as, ICON_SIZE - 5, &light);
400 return tile;
403 static void omnipresentCallback(WMenu *menu, WMenuEntry *entry)
405 WAppIcon *clickedIcon = entry->clientdata;
406 WAppIcon *aicon;
407 WDock *dock;
408 WMArray *selectedIcons;
409 WMArrayIterator iter;
410 int failed;
412 assert(entry->clientdata != NULL);
414 dock = clickedIcon->dock;
416 selectedIcons = getSelected(dock);
418 if (!WMGetArrayItemCount(selectedIcons))
419 WMAddToArray(selectedIcons, clickedIcon);
421 failed = 0;
422 WM_ITERATE_ARRAY(selectedIcons, aicon, iter) {
423 if (wClipMakeIconOmnipresent(aicon, !aicon->omnipresent) == WO_FAILED)
424 failed++;
425 else if (aicon->icon->selected)
426 wIconSelect(aicon->icon);
428 WMFreeArray(selectedIcons);
430 if (failed > 1) {
431 wMessageDialog(dock->screen_ptr, _("Warning"),
432 _("Some icons cannot be made omnipresent. "
433 "Please make sure that no other icon is "
434 "docked in the same positions on the other "
435 "workspaces and the Clip is not full in "
436 "some workspace."), _("OK"), NULL, NULL);
437 } else if (failed == 1) {
438 wMessageDialog(dock->screen_ptr, _("Warning"),
439 _("Icon cannot be made omnipresent. "
440 "Please make sure that no other icon is "
441 "docked in the same position on the other "
442 "workspaces and the Clip is not full in "
443 "some workspace."), _("OK"), NULL, NULL);
447 static void removeIconsCallback(WMenu *menu, WMenuEntry *entry)
449 WAppIcon *clickedIcon = (WAppIcon *) entry->clientdata;
450 WDock *dock;
451 WAppIcon *aicon;
452 WMArray *selectedIcons;
453 int keepit;
454 WMArrayIterator it;
456 assert(clickedIcon != NULL);
458 dock = clickedIcon->dock;
460 selectedIcons = getSelected(dock);
462 if (WMGetArrayItemCount(selectedIcons)) {
463 if (wMessageDialog(dock->screen_ptr, _("Workspace Clip"),
464 _("All selected icons will be removed!"),
465 _("OK"), _("Cancel"), NULL) != WAPRDefault) {
466 WMFreeArray(selectedIcons);
467 return;
469 } else {
470 if (clickedIcon->xindex == 0 && clickedIcon->yindex == 0) {
471 WMFreeArray(selectedIcons);
472 return;
474 WMAddToArray(selectedIcons, clickedIcon);
477 WM_ITERATE_ARRAY(selectedIcons, aicon, it) {
478 keepit = aicon->running && wApplicationOf(aicon->main_window);
479 wDockDetach(dock, aicon);
480 if (keepit) {
481 /* XXX: can: aicon->icon == NULL ? */
482 PlaceIcon(dock->screen_ptr, &aicon->x_pos, &aicon->y_pos,
483 wGetHeadForWindow(aicon->icon->owner));
484 XMoveWindow(dpy, aicon->icon->core->window, aicon->x_pos, aicon->y_pos);
485 if (!dock->mapped || dock->collapsed)
486 XMapWindow(dpy, aicon->icon->core->window);
489 WMFreeArray(selectedIcons);
491 if (wPreferences.auto_arrange_icons)
492 wArrangeIcons(dock->screen_ptr, True);
495 static void keepIconsCallback(WMenu *menu, WMenuEntry *entry)
497 WAppIcon *clickedIcon = (WAppIcon *) entry->clientdata;
498 WDock *dock;
499 WAppIcon *aicon;
500 WMArray *selectedIcons;
501 WMArrayIterator it;
503 assert(clickedIcon != NULL);
504 dock = clickedIcon->dock;
506 selectedIcons = getSelected(dock);
508 if (!WMGetArrayItemCount(selectedIcons)
509 && clickedIcon != dock->screen_ptr->clip_icon) {
510 char *command = NULL;
512 if (!clickedIcon->command && !clickedIcon->editing) {
513 clickedIcon->editing = 1;
514 if (wInputDialog(dock->screen_ptr, _("Keep Icon"),
515 _("Type the command used to launch the application"), &command)) {
516 if (command && (command[0] == 0 || (command[0] == '-' && command[1] == 0))) {
517 wfree(command);
518 command = NULL;
520 clickedIcon->command = command;
521 clickedIcon->editing = 0;
522 } else {
523 clickedIcon->editing = 0;
524 if (command)
525 wfree(command);
526 WMFreeArray(selectedIcons);
527 return;
531 WMAddToArray(selectedIcons, clickedIcon);
534 WM_ITERATE_ARRAY(selectedIcons, aicon, it) {
535 if (aicon->icon->selected)
536 wIconSelect(aicon->icon);
537 if (aicon && aicon->attracted && aicon->command) {
538 aicon->attracted = 0;
539 if (aicon->icon->shadowed) {
540 aicon->icon->shadowed = 0;
541 aicon->icon->force_paint = 1;
542 wAppIconPaint(aicon);
545 save_appicon(aicon, True);
547 WMFreeArray(selectedIcons);
550 static void toggleAutoAttractCallback(WMenu *menu, WMenuEntry *entry)
552 WDock *dock = (WDock *) entry->clientdata;
554 assert(entry->clientdata != NULL);
556 dock->attract_icons = !dock->attract_icons;
558 entry->flags.indicator_on = dock->attract_icons;
560 wMenuPaint(menu);
563 static void selectCallback(WMenu *menu, WMenuEntry *entry)
565 WAppIcon *icon = (WAppIcon *) entry->clientdata;
567 assert(icon != NULL);
569 wIconSelect(icon->icon);
571 wMenuPaint(menu);
574 static void colectIconsCallback(WMenu *menu, WMenuEntry *entry)
576 WAppIcon *clickedIcon = (WAppIcon *) entry->clientdata;
577 WDock *clip;
578 WAppIcon *aicon;
579 int x, y, x_pos, y_pos;
581 assert(entry->clientdata != NULL);
582 clip = clickedIcon->dock;
584 aicon = clip->screen_ptr->app_icon_list;
586 while (aicon) {
587 if (!aicon->docked && wDockFindFreeSlot(clip, &x, &y)) {
588 x_pos = clip->x_pos + x * ICON_SIZE;
589 y_pos = clip->y_pos + y * ICON_SIZE;
590 if (aicon->x_pos != x_pos || aicon->y_pos != y_pos)
591 move_window(aicon->icon->core->window, aicon->x_pos, aicon->y_pos, x_pos, y_pos);
593 aicon->attracted = 1;
594 if (!aicon->icon->shadowed) {
595 aicon->icon->shadowed = 1;
596 aicon->icon->force_paint = 1;
597 /* We don't do an wAppIconPaint() here because it's in
598 * wDockAttachIcon(). -Dan
601 wDockAttachIcon(clip, aicon, x, y);
602 if (clip->collapsed || !clip->mapped)
603 XUnmapWindow(dpy, aicon->icon->core->window);
605 aicon = aicon->next;
609 static void selectIconsCallback(WMenu *menu, WMenuEntry *entry)
611 WAppIcon *clickedIcon = (WAppIcon *) entry->clientdata;
612 WDock *dock;
613 WMArray *selectedIcons;
614 WMArrayIterator iter;
615 WAppIcon *btn;
616 int i;
618 assert(clickedIcon != NULL);
619 dock = clickedIcon->dock;
621 selectedIcons = getSelected(dock);
623 if (!WMGetArrayItemCount(selectedIcons)) {
624 for (i = 1; i < dock->max_icons; i++) {
625 btn = dock->icon_array[i];
626 if (btn && !btn->icon->selected)
627 wIconSelect(btn->icon);
629 } else {
630 WM_ITERATE_ARRAY(selectedIcons, btn, iter) {
631 wIconSelect(btn->icon);
634 WMFreeArray(selectedIcons);
636 wMenuPaint(menu);
639 static void toggleCollapsedCallback(WMenu *menu, WMenuEntry *entry)
641 assert(entry->clientdata != NULL);
643 toggleCollapsed(entry->clientdata);
645 entry->flags.indicator_on = ((WDock *) entry->clientdata)->collapsed;
647 wMenuPaint(menu);
650 static void toggleAutoCollapseCallback(WMenu *menu, WMenuEntry *entry)
652 WDock *dock;
653 assert(entry->clientdata != NULL);
655 dock = (WDock *) entry->clientdata;
657 dock->auto_collapse = !dock->auto_collapse;
659 entry->flags.indicator_on = ((WDock *) entry->clientdata)->auto_collapse;
661 wMenuPaint(menu);
664 static void toggleAutoRaiseLowerCallback(WMenu *menu, WMenuEntry *entry)
666 WDock *dock;
667 assert(entry->clientdata != NULL);
669 dock = (WDock *) entry->clientdata;
671 dock->auto_raise_lower = !dock->auto_raise_lower;
673 entry->flags.indicator_on = ((WDock *) entry->clientdata)->auto_raise_lower;
675 wMenuPaint(menu);
678 static void launchCallback(WMenu *menu, WMenuEntry *entry)
680 WAppIcon *btn = (WAppIcon *) entry->clientdata;
682 launchDockedApplication(btn, False);
685 static void settingsCallback(WMenu *menu, WMenuEntry *entry)
687 WAppIcon *btn = (WAppIcon *) entry->clientdata;
689 if (btn->editing)
690 return;
691 ShowDockAppSettingsPanel(btn);
694 static void hideCallback(WMenu *menu, WMenuEntry *entry)
696 WApplication *wapp;
697 WAppIcon *btn = (WAppIcon *) entry->clientdata;
699 wapp = wApplicationOf(btn->icon->owner->main_window);
701 if (wapp->flags.hidden) {
702 wWorkspaceChange(btn->icon->core->screen_ptr, wapp->last_workspace);
703 wUnhideApplication(wapp, False, False);
704 } else {
705 wHideApplication(wapp);
709 static void unhideHereCallback(WMenu *menu, WMenuEntry *entry)
711 WApplication *wapp;
712 WAppIcon *btn = (WAppIcon *) entry->clientdata;
714 wapp = wApplicationOf(btn->icon->owner->main_window);
716 wUnhideApplication(wapp, False, True);
719 static WAppIcon *mainIconCreate(WScreen *scr, int type)
721 WAppIcon *btn;
722 int x_pos;
724 if (type == WM_CLIP) {
725 if (scr->clip_icon)
726 return scr->clip_icon;
727 btn = wAppIconCreateForDock(scr, NULL, "Logo", "WMClip", TILE_CLIP);
728 btn->icon->core->descriptor.handle_expose = clipIconExpose;
729 btn->icon->core->descriptor.handle_enternotify = clipEnterNotify;
730 btn->icon->core->descriptor.handle_leavenotify = clipLeaveNotify;
731 x_pos = 0;
732 } else {
733 btn = wAppIconCreateForDock(scr, NULL, "Logo", "WMDock", TILE_NORMAL);
734 x_pos = scr->scr_width - ICON_SIZE - DOCK_EXTRA_SPACE;
737 btn->xindex = 0;
738 btn->yindex = 0;
740 btn->icon->core->descriptor.handle_mousedown = iconMouseDown;
741 btn->icon->core->descriptor.parent_type = WCLASS_DOCK_ICON;
742 btn->icon->core->descriptor.parent = btn;
743 XMapWindow(dpy, btn->icon->core->window);
744 btn->x_pos = x_pos;
745 btn->y_pos = 0;
746 btn->docked = 1;
747 if (type == WM_CLIP)
748 scr->clip_icon = btn;
750 return btn;
753 static void switchWSCommand(WMenu *menu, WMenuEntry *entry)
755 WAppIcon *btn, *icon = (WAppIcon *) entry->clientdata;
756 WScreen *scr = icon->icon->core->screen_ptr;
757 WDock *src, *dest;
758 WMArray *selectedIcons;
759 int x, y;
761 if (entry->order == scr->current_workspace)
762 return;
763 src = icon->dock;
764 dest = scr->workspaces[entry->order]->clip;
766 selectedIcons = getSelected(src);
768 if (WMGetArrayItemCount(selectedIcons)) {
769 WMArrayIterator iter;
771 WM_ITERATE_ARRAY(selectedIcons, btn, iter) {
772 if (wDockFindFreeSlot(dest, &x, &y)) {
773 moveIconBetweenDocks(src, dest, btn, x, y);
774 XUnmapWindow(dpy, btn->icon->core->window);
777 } else if (icon != scr->clip_icon) {
778 if (wDockFindFreeSlot(dest, &x, &y)) {
779 moveIconBetweenDocks(src, dest, icon, x, y);
780 XUnmapWindow(dpy, icon->icon->core->window);
783 WMFreeArray(selectedIcons);
786 static void launchDockedApplication(WAppIcon *btn, Bool withSelection)
788 WScreen *scr = btn->icon->core->screen_ptr;
790 if (!btn->launching &&
791 ((!withSelection && btn->command != NULL) || (withSelection && btn->paste_command != NULL))) {
792 if (!btn->forced_dock) {
793 btn->relaunching = btn->running;
794 btn->running = 1;
796 if (btn->wm_instance || btn->wm_class) {
797 WWindowAttributes attr;
798 memset(&attr, 0, sizeof(WWindowAttributes));
799 wDefaultFillAttributes(btn->wm_instance, btn->wm_class, &attr, NULL, True);
801 if (!attr.no_appicon && !btn->buggy_app)
802 btn->launching = 1;
803 else
804 btn->running = 0;
806 btn->drop_launch = 0;
807 btn->paste_launch = withSelection;
808 scr->last_dock = btn->dock;
809 btn->pid = execCommand(btn, (withSelection ? btn->paste_command : btn->command), NULL);
810 if (btn->pid > 0) {
811 if (btn->buggy_app) {
812 /* give feedback that the app was launched */
813 btn->launching = 1;
814 dockIconPaint(btn);
815 btn->launching = 0;
816 WMAddTimerHandler(200, (WMCallback *) dockIconPaint, btn);
817 } else {
818 dockIconPaint(btn);
820 } else {
821 wwarning(_("could not launch application %s"), btn->command);
822 btn->launching = 0;
823 if (!btn->relaunching)
824 btn->running = 0;
829 static void updateWorkspaceMenu(WMenu *menu, WAppIcon *icon)
831 WScreen *scr = menu->frame->screen_ptr;
832 char title[MAX_WORKSPACENAME_WIDTH + 1];
833 int i;
835 if (!menu || !icon)
836 return;
838 for (i = 0; i < scr->workspace_count; i++) {
839 if (i < menu->entry_no) {
840 if (strcmp(menu->entries[i]->text, scr->workspaces[i]->name) != 0) {
841 wfree(menu->entries[i]->text);
842 strcpy(title, scr->workspaces[i]->name);
843 menu->entries[i]->text = wstrdup(title);
844 menu->flags.realized = 0;
846 menu->entries[i]->clientdata = (void *)icon;
847 } else {
848 strcpy(title, scr->workspaces[i]->name);
850 wMenuAddCallback(menu, title, switchWSCommand, (void *)icon);
852 menu->flags.realized = 0;
855 if (i == scr->current_workspace)
856 wMenuSetEnabled(menu, i, False);
857 else
858 wMenuSetEnabled(menu, i, True);
861 if (!menu->flags.realized)
862 wMenuRealize(menu);
865 static WMenu *makeWorkspaceMenu(WScreen *scr)
867 WMenu *menu;
869 menu = wMenuCreate(scr, NULL, False);
870 if (!menu)
871 wwarning(_("could not create workspace submenu for Clip menu"));
873 wMenuAddCallback(menu, "", switchWSCommand, (void *)scr->clip_icon);
875 menu->flags.realized = 0;
876 wMenuRealize(menu);
878 return menu;
881 static void updateClipOptionsMenu(WMenu *menu, WDock *dock)
883 WMenuEntry *entry;
884 int index = 0;
886 if (!menu || !dock)
887 return;
889 /* keep on top */
890 entry = menu->entries[index];
891 entry->flags.indicator_on = !dock->lowered;
892 entry->clientdata = dock;
894 /* collapsed */
895 entry = menu->entries[++index];
896 entry->flags.indicator_on = dock->collapsed;
897 entry->clientdata = dock;
899 /* auto-collapse */
900 entry = menu->entries[++index];
901 entry->flags.indicator_on = dock->auto_collapse;
902 entry->clientdata = dock;
904 /* auto-raise/lower */
905 entry = menu->entries[++index];
906 entry->flags.indicator_on = dock->auto_raise_lower;
907 entry->clientdata = dock;
908 wMenuSetEnabled(menu, index, dock->lowered);
910 /* attract icons */
911 entry = menu->entries[++index];
912 entry->flags.indicator_on = dock->attract_icons;
913 entry->clientdata = dock;
915 menu->flags.realized = 0;
916 wMenuRealize(menu);
919 static WMenu *makeClipOptionsMenu(WScreen *scr)
921 WMenu *menu;
922 WMenuEntry *entry;
924 menu = wMenuCreate(scr, NULL, False);
925 if (!menu) {
926 wwarning(_("could not create options submenu for Clip menu"));
927 return NULL;
930 entry = wMenuAddCallback(menu, _("Keep on Top"), toggleLoweredCallback, NULL);
931 entry->flags.indicator = 1;
932 entry->flags.indicator_on = 1;
933 entry->flags.indicator_type = MI_CHECK;
935 entry = wMenuAddCallback(menu, _("Collapsed"), toggleCollapsedCallback, NULL);
936 entry->flags.indicator = 1;
937 entry->flags.indicator_on = 1;
938 entry->flags.indicator_type = MI_CHECK;
940 entry = wMenuAddCallback(menu, _("Autocollapse"), toggleAutoCollapseCallback, NULL);
941 entry->flags.indicator = 1;
942 entry->flags.indicator_on = 1;
943 entry->flags.indicator_type = MI_CHECK;
945 entry = wMenuAddCallback(menu, _("Autoraise"), toggleAutoRaiseLowerCallback, NULL);
946 entry->flags.indicator = 1;
947 entry->flags.indicator_on = 1;
948 entry->flags.indicator_type = MI_CHECK;
950 entry = wMenuAddCallback(menu, _("Autoattract Icons"), toggleAutoAttractCallback, NULL);
951 entry->flags.indicator = 1;
952 entry->flags.indicator_on = 1;
953 entry->flags.indicator_type = MI_CHECK;
955 menu->flags.realized = 0;
956 wMenuRealize(menu);
958 return menu;
961 static WMenu *dockMenuCreate(WScreen *scr, int type)
963 WMenu *menu;
964 WMenuEntry *entry;
966 if (type == WM_CLIP && scr->clip_menu)
967 return scr->clip_menu;
969 menu = wMenuCreate(scr, NULL, False);
970 if (type != WM_CLIP) {
971 entry = wMenuAddCallback(menu, _("Keep on Top"), toggleLoweredCallback, NULL);
972 entry->flags.indicator = 1;
973 entry->flags.indicator_on = 1;
974 entry->flags.indicator_type = MI_CHECK;
975 } else {
976 entry = wMenuAddCallback(menu, _("Clip Options"), NULL, NULL);
977 scr->clip_options = makeClipOptionsMenu(scr);
978 if (scr->clip_options)
979 wMenuEntrySetCascade(menu, entry, scr->clip_options);
981 entry = wMenuAddCallback(menu, _("Rename Workspace"), renameCallback, NULL);
982 wfree(entry->text);
983 entry->text = _("Rename Workspace");
985 entry = wMenuAddCallback(menu, _("Selected"), selectCallback, NULL);
986 entry->flags.indicator = 1;
987 entry->flags.indicator_on = 1;
988 entry->flags.indicator_type = MI_CHECK;
990 entry = wMenuAddCallback(menu, _("Select All Icons"), selectIconsCallback, NULL);
991 wfree(entry->text);
992 entry->text = _("Select All Icons");
994 entry = wMenuAddCallback(menu, _("Keep Icon"), keepIconsCallback, NULL);
995 wfree(entry->text);
996 entry->text = _("Keep Icon");
998 entry = wMenuAddCallback(menu, _("Move Icon To"), NULL, NULL);
999 wfree(entry->text);
1000 entry->text = _("Move Icon To");
1001 scr->clip_submenu = makeWorkspaceMenu(scr);
1002 if (scr->clip_submenu)
1003 wMenuEntrySetCascade(menu, entry, scr->clip_submenu);
1005 entry = wMenuAddCallback(menu, _("Remove Icon"), removeIconsCallback, NULL);
1006 wfree(entry->text);
1007 entry->text = _("Remove Icon");
1009 wMenuAddCallback(menu, _("Attract Icons"), colectIconsCallback, NULL);
1012 wMenuAddCallback(menu, _("Launch"), launchCallback, NULL);
1014 wMenuAddCallback(menu, _("Unhide Here"), unhideHereCallback, NULL);
1016 entry = wMenuAddCallback(menu, _("Hide"), hideCallback, NULL);
1017 wfree(entry->text);
1018 entry->text = _("Hide");
1020 wMenuAddCallback(menu, _("Settings..."), settingsCallback, NULL);
1022 wMenuAddCallback(menu, _("Kill"), killCallback, NULL);
1024 if (type == WM_CLIP)
1025 scr->clip_menu = menu;
1027 return menu;
1030 WDock *wDockCreate(WScreen *scr, int type)
1032 WDock *dock;
1033 WAppIcon *btn;
1035 make_keys();
1037 dock = wmalloc(sizeof(WDock));
1039 dock->max_icons = DOCK_MAX_ICONS;
1041 dock->icon_array = wmalloc(sizeof(WAppIcon *) * dock->max_icons);
1043 btn = mainIconCreate(scr, type);
1045 btn->dock = dock;
1047 dock->x_pos = btn->x_pos;
1048 dock->y_pos = btn->y_pos;
1049 dock->screen_ptr = scr;
1050 dock->type = type;
1051 dock->icon_count = 1;
1052 dock->on_right_side = 1;
1053 dock->collapsed = 0;
1054 dock->auto_collapse = 0;
1055 dock->auto_collapse_magic = NULL;
1056 dock->auto_raise_lower = 0;
1057 dock->auto_lower_magic = NULL;
1058 dock->auto_raise_magic = NULL;
1059 dock->attract_icons = 0;
1060 dock->lowered = 1;
1061 dock->icon_array[0] = btn;
1062 wRaiseFrame(btn->icon->core);
1063 XMoveWindow(dpy, btn->icon->core->window, btn->x_pos, btn->y_pos);
1065 /* create dock menu */
1066 dock->menu = dockMenuCreate(scr, type);
1068 return dock;
1071 void wDockDestroy(WDock *dock)
1073 int i;
1074 WAppIcon *aicon;
1076 for (i = (dock->type == WM_CLIP) ? 1 : 0; i < dock->max_icons; i++) {
1077 aicon = dock->icon_array[i];
1078 if (aicon) {
1079 int keepit = aicon->running && wApplicationOf(aicon->main_window);
1080 wDockDetach(dock, aicon);
1081 if (keepit) {
1082 /* XXX: can: aicon->icon == NULL ? */
1083 PlaceIcon(dock->screen_ptr, &aicon->x_pos, &aicon->y_pos,
1084 wGetHeadForWindow(aicon->icon->owner));
1085 XMoveWindow(dpy, aicon->icon->core->window, aicon->x_pos, aicon->y_pos);
1086 if (!dock->mapped || dock->collapsed)
1087 XMapWindow(dpy, aicon->icon->core->window);
1091 if (wPreferences.auto_arrange_icons)
1092 wArrangeIcons(dock->screen_ptr, True);
1093 wfree(dock->icon_array);
1094 if (dock->menu && dock->type != WM_CLIP)
1095 wMenuDestroy(dock->menu, True);
1096 if (dock->screen_ptr->last_dock == dock)
1097 dock->screen_ptr->last_dock = NULL;
1098 wfree(dock);
1101 void wClipIconPaint(WAppIcon *aicon)
1103 WScreen *scr = aicon->icon->core->screen_ptr;
1104 WWorkspace *workspace = scr->workspaces[scr->current_workspace];
1105 WMColor *color;
1106 Window win = aicon->icon->core->window;
1107 int length, nlength;
1108 char *ws_name, ws_number[10];
1109 int ty, tx;
1111 wIconPaint(aicon->icon);
1113 length = strlen(workspace->name);
1114 ws_name = wmalloc(length + 1);
1115 snprintf(ws_name, length + 1, "%s", workspace->name);
1116 snprintf(ws_number, sizeof(ws_number), "%i", scr->current_workspace + 1);
1117 nlength = strlen(ws_number);
1119 if (!workspace->clip->collapsed)
1120 color = scr->clip_title_color[CLIP_NORMAL];
1121 else
1122 color = scr->clip_title_color[CLIP_COLLAPSED];
1124 ty = ICON_SIZE - WMFontHeight(scr->clip_title_font) - 3;
1126 tx = CLIP_BUTTON_SIZE * ICON_SIZE / 64;
1128 if(wPreferences.show_clip_title)
1129 WMDrawString(scr->wmscreen, win, color, scr->clip_title_font, tx, ty, ws_name, length);
1131 tx = (ICON_SIZE / 2 - WMWidthOfString(scr->clip_title_font, ws_number, nlength)) / 2;
1133 WMDrawString(scr->wmscreen, win, color, scr->clip_title_font, tx, 2, ws_number, nlength);
1135 wfree(ws_name);
1137 if (aicon->launching)
1138 XFillRectangle(dpy, aicon->icon->core->window, scr->stipple_gc,
1139 0, 0, wPreferences.icon_size, wPreferences.icon_size);
1141 paintClipButtons(aicon, aicon->dock->lclip_button_pushed, aicon->dock->rclip_button_pushed);
1144 static void clipIconExpose(WObjDescriptor *desc, XEvent *event)
1146 wClipIconPaint(desc->parent);
1149 static void dockIconPaint(WAppIcon *btn)
1151 if (btn == btn->icon->core->screen_ptr->clip_icon) {
1152 wClipIconPaint(btn);
1153 } else {
1154 wAppIconPaint(btn);
1155 save_appicon(btn, True);
1159 static WMPropList *make_icon_state(WAppIcon *btn)
1161 WMPropList *node = NULL;
1162 WMPropList *command, *autolaunch, *lock, *name, *forced;
1163 WMPropList *position, *buggy, *omnipresent;
1164 char *tmp;
1165 char buffer[64];
1167 if (btn) {
1168 if (!btn->command)
1169 command = WMCreatePLString("-");
1170 else
1171 command = WMCreatePLString(btn->command);
1173 autolaunch = btn->auto_launch ? dYes : dNo;
1175 lock = btn->lock ? dYes : dNo;
1177 tmp = EscapeWM_CLASS(btn->wm_instance, btn->wm_class);
1179 name = WMCreatePLString(tmp);
1181 wfree(tmp);
1183 forced = btn->forced_dock ? dYes : dNo;
1185 buggy = btn->buggy_app ? dYes : dNo;
1187 if (btn == btn->icon->core->screen_ptr->clip_icon)
1188 snprintf(buffer, sizeof(buffer), "%i,%i", btn->x_pos, btn->y_pos);
1189 else
1190 snprintf(buffer, sizeof(buffer), "%hi,%hi", btn->xindex, btn->yindex);
1191 position = WMCreatePLString(buffer);
1193 node = WMCreatePLDictionary(dCommand, command,
1194 dName, name,
1195 dAutoLaunch, autolaunch,
1196 dLock, lock,
1197 dForced, forced, dBuggyApplication, buggy, dPosition, position, NULL);
1198 WMReleasePropList(command);
1199 WMReleasePropList(name);
1200 WMReleasePropList(position);
1202 omnipresent = btn->omnipresent ? dYes : dNo;
1203 if (btn->dock != btn->icon->core->screen_ptr->dock && (btn->xindex != 0 || btn->yindex != 0))
1204 WMPutInPLDictionary(node, dOmnipresent, omnipresent);
1206 #ifdef XDND /* was OFFIX */
1207 if (btn->dnd_command) {
1208 command = WMCreatePLString(btn->dnd_command);
1209 WMPutInPLDictionary(node, dDropCommand, command);
1210 WMReleasePropList(command);
1212 #endif /* XDND */
1214 if (btn->paste_command) {
1215 command = WMCreatePLString(btn->paste_command);
1216 WMPutInPLDictionary(node, dPasteCommand, command);
1217 WMReleasePropList(command);
1221 return node;
1224 static WMPropList *dockSaveState(WDock *dock)
1226 int i;
1227 WMPropList *icon_info;
1228 WMPropList *list = NULL, *dock_state = NULL;
1229 WMPropList *value, *key;
1230 char buffer[256];
1232 list = WMCreatePLArray(NULL);
1234 for (i = (dock->type == WM_DOCK ? 0 : 1); i < dock->max_icons; i++) {
1235 WAppIcon *btn = dock->icon_array[i];
1237 if (!btn || btn->attracted)
1238 continue;
1240 if ((icon_info = make_icon_state(dock->icon_array[i]))) {
1241 WMAddToPLArray(list, icon_info);
1242 WMReleasePropList(icon_info);
1246 dock_state = WMCreatePLDictionary(dApplications, list, NULL);
1248 if (dock->type == WM_DOCK) {
1249 snprintf(buffer, sizeof(buffer), "Applications%i", dock->screen_ptr->scr_height);
1250 key = WMCreatePLString(buffer);
1251 WMPutInPLDictionary(dock_state, key, list);
1252 WMReleasePropList(key);
1254 snprintf(buffer, sizeof(buffer), "%i,%i", (dock->on_right_side ? -ICON_SIZE : 0), dock->y_pos);
1255 value = WMCreatePLString(buffer);
1256 WMPutInPLDictionary(dock_state, dPosition, value);
1257 WMReleasePropList(value);
1259 WMReleasePropList(list);
1261 value = (dock->lowered ? dYes : dNo);
1262 WMPutInPLDictionary(dock_state, dLowered, value);
1264 if (dock->type == WM_CLIP) {
1265 value = (dock->collapsed ? dYes : dNo);
1266 WMPutInPLDictionary(dock_state, dCollapsed, value);
1268 value = (dock->auto_collapse ? dYes : dNo);
1269 WMPutInPLDictionary(dock_state, dAutoCollapse, value);
1271 value = (dock->auto_raise_lower ? dYes : dNo);
1272 WMPutInPLDictionary(dock_state, dAutoRaiseLower, value);
1274 value = (dock->attract_icons ? dYes : dNo);
1275 WMPutInPLDictionary(dock_state, dAutoAttractIcons, value);
1278 return dock_state;
1281 void wDockSaveState(WScreen *scr, WMPropList *old_state)
1283 WMPropList *dock_state;
1284 WMPropList *keys;
1286 dock_state = dockSaveState(scr->dock);
1289 * Copy saved states of docks with different sizes.
1291 if (old_state) {
1292 int i;
1293 WMPropList *tmp;
1295 keys = WMGetPLDictionaryKeys(old_state);
1296 for (i = 0; i < WMGetPropListItemCount(keys); i++) {
1297 tmp = WMGetFromPLArray(keys, i);
1299 if (strncasecmp(WMGetFromPLString(tmp), "applications", 12) == 0
1300 && !WMGetFromPLDictionary(dock_state, tmp)) {
1302 WMPutInPLDictionary(dock_state, tmp, WMGetFromPLDictionary(old_state, tmp));
1305 WMReleasePropList(keys);
1308 WMPutInPLDictionary(scr->session_state, dDock, dock_state);
1310 WMReleasePropList(dock_state);
1313 void wClipSaveState(WScreen *scr)
1315 WMPropList *clip_state;
1317 clip_state = make_icon_state(scr->clip_icon);
1319 WMPutInPLDictionary(scr->session_state, dClip, clip_state);
1321 WMReleasePropList(clip_state);
1324 WMPropList *wClipSaveWorkspaceState(WScreen *scr, int workspace)
1326 return dockSaveState(scr->workspaces[workspace]->clip);
1329 static Bool getBooleanDockValue(WMPropList *value, WMPropList *key)
1331 if (value) {
1332 if (WMIsPLString(value)) {
1333 if (strcasecmp(WMGetFromPLString(value), "YES") == 0)
1334 return True;
1335 } else {
1336 wwarning(_("bad value in docked icon state info %s"), WMGetFromPLString(key));
1339 return False;
1342 static WAppIcon *restore_icon_state(WScreen *scr, WMPropList *info, int type, int index)
1344 WAppIcon *aicon;
1345 WMPropList *cmd, *value;
1346 char *wclass, *winstance, *command;
1348 cmd = WMGetFromPLDictionary(info, dCommand);
1349 if (!cmd || !WMIsPLString(cmd))
1350 return NULL;
1352 /* parse window name */
1353 value = WMGetFromPLDictionary(info, dName);
1354 if (!value)
1355 return NULL;
1357 ParseWindowName(value, &winstance, &wclass, "dock");
1359 if (!winstance && !wclass)
1360 return NULL;
1362 /* get commands */
1363 if (cmd)
1364 command = wstrdup(WMGetFromPLString(cmd));
1365 else
1366 command = NULL;
1368 if (!command || strcmp(command, "-") == 0) {
1369 if (command)
1370 wfree(command);
1371 if (wclass)
1372 wfree(wclass);
1373 if (winstance)
1374 wfree(winstance);
1376 return NULL;
1379 aicon = wAppIconCreateForDock(scr, command, winstance, wclass, TILE_NORMAL);
1380 if (wclass)
1381 wfree(wclass);
1382 if (winstance)
1383 wfree(winstance);
1384 if (command)
1385 wfree(command);
1387 aicon->icon->core->descriptor.handle_mousedown = iconMouseDown;
1388 if (type == WM_CLIP) {
1389 aicon->icon->core->descriptor.handle_enternotify = clipEnterNotify;
1390 aicon->icon->core->descriptor.handle_leavenotify = clipLeaveNotify;
1392 aicon->icon->core->descriptor.parent_type = WCLASS_DOCK_ICON;
1393 aicon->icon->core->descriptor.parent = aicon;
1395 #ifdef XDND /* was OFFIX */
1396 cmd = WMGetFromPLDictionary(info, dDropCommand);
1397 if (cmd)
1398 aicon->dnd_command = wstrdup(WMGetFromPLString(cmd));
1399 #endif
1401 cmd = WMGetFromPLDictionary(info, dPasteCommand);
1402 if (cmd)
1403 aicon->paste_command = wstrdup(WMGetFromPLString(cmd));
1405 /* check auto launch */
1406 value = WMGetFromPLDictionary(info, dAutoLaunch);
1408 aicon->auto_launch = getBooleanDockValue(value, dAutoLaunch);
1410 /* check lock */
1411 value = WMGetFromPLDictionary(info, dLock);
1413 aicon->lock = getBooleanDockValue(value, dLock);
1415 /* check if it wasn't normally docked */
1416 value = WMGetFromPLDictionary(info, dForced);
1418 aicon->forced_dock = getBooleanDockValue(value, dForced);
1420 /* check if we can rely on the stuff in the app */
1421 value = WMGetFromPLDictionary(info, dBuggyApplication);
1423 aicon->buggy_app = getBooleanDockValue(value, dBuggyApplication);
1425 /* get position in the dock */
1426 value = WMGetFromPLDictionary(info, dPosition);
1427 if (value && WMIsPLString(value)) {
1428 if (sscanf(WMGetFromPLString(value), "%hi,%hi", &aicon->xindex, &aicon->yindex) != 2)
1429 wwarning(_("bad value in docked icon state info %s"), WMGetFromPLString(dPosition));
1431 /* check position sanity */
1432 /* incomplete section! */
1433 if (type == WM_DOCK) {
1434 aicon->xindex = 0;
1435 if (aicon->yindex < 0)
1436 wwarning(_("bad value in docked icon position %i,%i"),
1437 aicon->xindex, aicon->yindex);
1439 } else {
1440 aicon->yindex = index;
1441 aicon->xindex = 0;
1444 /* check if icon is omnipresent */
1445 value = WMGetFromPLDictionary(info, dOmnipresent);
1447 aicon->omnipresent = getBooleanDockValue(value, dOmnipresent);
1449 aicon->running = 0;
1450 aicon->docked = 1;
1452 return aicon;
1455 #define COMPLAIN(key) wwarning(_("bad value in dock state info:%s"), key)
1457 WAppIcon *wClipRestoreState(WScreen *scr, WMPropList *clip_state)
1459 WAppIcon *icon;
1460 WMPropList *value;
1462 icon = mainIconCreate(scr, WM_CLIP);
1464 if (!clip_state)
1465 return icon;
1467 WMRetainPropList(clip_state);
1469 /* restore position */
1471 value = WMGetFromPLDictionary(clip_state, dPosition);
1473 if (value) {
1474 if (!WMIsPLString(value)) {
1475 COMPLAIN("Position");
1476 } else {
1477 WMRect rect;
1478 int flags;
1480 if (sscanf(WMGetFromPLString(value), "%i,%i", &icon->x_pos, &icon->y_pos) != 2)
1481 COMPLAIN("Position");
1483 /* check position sanity */
1484 rect.pos.x = icon->x_pos;
1485 rect.pos.y = icon->y_pos;
1486 rect.size.width = rect.size.height = ICON_SIZE;
1488 wGetRectPlacementInfo(scr, rect, &flags);
1489 if (flags & (XFLAG_DEAD | XFLAG_PARTIAL))
1490 wScreenKeepInside(scr, &icon->x_pos, &icon->y_pos, ICON_SIZE, ICON_SIZE);
1493 #ifdef XDND /* was OFFIX */
1494 value = WMGetFromPLDictionary(clip_state, dDropCommand);
1495 if (value && WMIsPLString(value))
1496 icon->dnd_command = wstrdup(WMGetFromPLString(value));
1497 #endif
1499 value = WMGetFromPLDictionary(clip_state, dPasteCommand);
1500 if (value && WMIsPLString(value))
1501 icon->paste_command = wstrdup(WMGetFromPLString(value));
1503 WMReleasePropList(clip_state);
1505 return icon;
1508 WDock *wDockRestoreState(WScreen *scr, WMPropList *dock_state, int type)
1510 WDock *dock;
1511 WMPropList *apps;
1512 WMPropList *value;
1513 WAppIcon *aicon, *old_top;
1514 int count, i;
1516 dock = wDockCreate(scr, type);
1518 if (!dock_state)
1519 return dock;
1521 WMRetainPropList(dock_state);
1523 /* restore position */
1524 value = WMGetFromPLDictionary(dock_state, dPosition);
1525 if (value) {
1526 if (!WMIsPLString(value)) {
1527 COMPLAIN("Position");
1528 } else {
1529 WMRect rect;
1530 int flags;
1532 if (sscanf(WMGetFromPLString(value), "%i,%i", &dock->x_pos, &dock->y_pos) != 2)
1533 COMPLAIN("Position");
1535 /* check position sanity */
1536 rect.pos.x = dock->x_pos;
1537 rect.pos.y = dock->y_pos;
1538 rect.size.width = rect.size.height = ICON_SIZE;
1540 wGetRectPlacementInfo(scr, rect, &flags);
1541 if (flags & (XFLAG_DEAD | XFLAG_PARTIAL)) {
1542 int x = dock->x_pos;
1543 wScreenKeepInside(scr, &x, &dock->y_pos, ICON_SIZE, ICON_SIZE);
1546 /* Is this needed any more? */
1547 if (type == WM_CLIP) {
1548 if (dock->x_pos < 0) {
1549 dock->x_pos = 0;
1550 } else if (dock->x_pos > scr->scr_width - ICON_SIZE) {
1551 dock->x_pos = scr->scr_width - ICON_SIZE;
1553 } else {
1554 if (dock->x_pos >= 0) {
1555 dock->x_pos = DOCK_EXTRA_SPACE;
1556 dock->on_right_side = 0;
1557 } else {
1558 dock->x_pos = scr->scr_width - DOCK_EXTRA_SPACE - ICON_SIZE;
1559 dock->on_right_side = 1;
1565 /* restore lowered/raised state */
1566 dock->lowered = 0;
1568 value = WMGetFromPLDictionary(dock_state, dLowered);
1569 if (value) {
1570 if (!WMIsPLString(value)) {
1571 COMPLAIN("Lowered");
1572 } else {
1573 if (strcasecmp(WMGetFromPLString(value), "YES") == 0)
1574 dock->lowered = 1;
1578 /* restore collapsed state */
1579 dock->collapsed = 0;
1581 value = WMGetFromPLDictionary(dock_state, dCollapsed);
1582 if (value) {
1583 if (!WMIsPLString(value)) {
1584 COMPLAIN("Collapsed");
1585 } else {
1586 if (strcasecmp(WMGetFromPLString(value), "YES") == 0)
1587 dock->collapsed = 1;
1591 /* restore auto-collapsed state */
1592 value = WMGetFromPLDictionary(dock_state, dAutoCollapse);
1593 if (value) {
1594 if (!WMIsPLString(value)) {
1595 COMPLAIN("AutoCollapse");
1596 } else {
1597 if (strcasecmp(WMGetFromPLString(value), "YES") == 0) {
1598 dock->auto_collapse = 1;
1599 dock->collapsed = 1;
1604 /* restore auto-raise/lower state */
1605 value = WMGetFromPLDictionary(dock_state, dAutoRaiseLower);
1606 if (value) {
1607 if (!WMIsPLString(value)) {
1608 COMPLAIN("AutoRaiseLower");
1609 } else {
1610 if (strcasecmp(WMGetFromPLString(value), "YES") == 0)
1611 dock->auto_raise_lower = 1;
1615 /* restore attract icons state */
1616 dock->attract_icons = 0;
1618 value = WMGetFromPLDictionary(dock_state, dAutoAttractIcons);
1619 if (value) {
1620 if (!WMIsPLString(value)) {
1621 COMPLAIN("AutoAttractIcons");
1622 } else {
1623 if (strcasecmp(WMGetFromPLString(value), "YES") == 0)
1624 dock->attract_icons = 1;
1628 /* application list */
1631 WMPropList *tmp;
1632 char buffer[64];
1635 * When saving, it saves the dock state in
1636 * Applications and Applicationsnnn
1638 * When loading, it will first try Applicationsnnn.
1639 * If it does not exist, use Applications as default.
1642 snprintf(buffer, sizeof(buffer), "Applications%i", scr->scr_height);
1644 tmp = WMCreatePLString(buffer);
1645 apps = WMGetFromPLDictionary(dock_state, tmp);
1646 WMReleasePropList(tmp);
1648 if (!apps)
1649 apps = WMGetFromPLDictionary(dock_state, dApplications);
1652 if (!apps)
1653 goto finish;
1655 count = WMGetPropListItemCount(apps);
1656 if (count == 0)
1657 goto finish;
1659 old_top = dock->icon_array[0];
1661 /* dock->icon_count is set to 1 when dock is created.
1662 * Since Clip is already restored, we want to keep it so for clip,
1663 * but for dock we may change the default top tile, so we set it to 0.
1665 if (type == WM_DOCK)
1666 dock->icon_count = 0;
1668 for (i = 0; i < count; i++) {
1669 if (dock->icon_count >= dock->max_icons) {
1670 wwarning(_("there are too many icons stored in dock. Ignoring what doesn't fit"));
1671 break;
1674 value = WMGetFromPLArray(apps, i);
1675 aicon = restore_icon_state(scr, value, type, dock->icon_count);
1677 dock->icon_array[dock->icon_count] = aicon;
1679 if (aicon) {
1680 aicon->dock = dock;
1681 aicon->x_pos = dock->x_pos + (aicon->xindex * ICON_SIZE);
1682 aicon->y_pos = dock->y_pos + (aicon->yindex * ICON_SIZE);
1684 if (dock->lowered)
1685 ChangeStackingLevel(aicon->icon->core, WMNormalLevel);
1686 else
1687 ChangeStackingLevel(aicon->icon->core, WMDockLevel);
1689 wCoreConfigure(aicon->icon->core, aicon->x_pos, aicon->y_pos, 0, 0);
1690 if (!dock->collapsed)
1691 XMapWindow(dpy, aicon->icon->core->window);
1693 wRaiseFrame(aicon->icon->core);
1695 dock->icon_count++;
1696 } else if (dock->icon_count == 0 && type == WM_DOCK) {
1697 dock->icon_count++;
1701 /* if the first icon is not defined, use the default */
1702 if (dock->icon_array[0] == NULL) {
1703 /* update default icon */
1704 old_top->x_pos = dock->x_pos;
1705 old_top->y_pos = dock->y_pos;
1706 if (dock->lowered)
1707 ChangeStackingLevel(old_top->icon->core, WMNormalLevel);
1708 else
1709 ChangeStackingLevel(old_top->icon->core, WMDockLevel);
1711 dock->icon_array[0] = old_top;
1712 XMoveWindow(dpy, old_top->icon->core->window, dock->x_pos, dock->y_pos);
1713 /* we don't need to increment dock->icon_count here because it was
1714 * incremented in the loop above.
1716 } else if (old_top != dock->icon_array[0]) {
1717 if (old_top == scr->clip_icon)
1718 scr->clip_icon = dock->icon_array[0];
1720 wAppIconDestroy(old_top);
1723 finish:
1724 WMReleasePropList(dock_state);
1726 return dock;
1729 void wDockLaunchWithState(WDock *dock, WAppIcon *btn, WSavedState *state)
1731 if (btn && btn->command && !btn->running && !btn->launching) {
1732 btn->drop_launch = 0;
1733 btn->paste_launch = 0;
1735 btn->pid = execCommand(btn, btn->command, state);
1737 if (btn->pid > 0) {
1738 if (!btn->forced_dock && !btn->buggy_app) {
1739 btn->launching = 1;
1740 dockIconPaint(btn);
1743 } else {
1744 wfree(state);
1748 void wDockDoAutoLaunch(WDock *dock, int workspace)
1750 WAppIcon *btn;
1751 WSavedState *state;
1752 int i;
1754 for (i = 0; i < dock->max_icons; i++) {
1755 btn = dock->icon_array[i];
1756 if (!btn || !btn->auto_launch)
1757 continue;
1759 state = wmalloc(sizeof(WSavedState));
1760 state->workspace = workspace;
1761 /* TODO: this is klugy and is very difficult to understand
1762 * what's going on. Try to clean up */
1763 wDockLaunchWithState(dock, btn, state);
1767 #ifdef XDND /* was OFFIX */
1768 static WDock *findDock(WScreen *scr, XEvent *event, int *icon_pos)
1770 WDock *dock;
1771 int i;
1773 *icon_pos = -1;
1774 if ((dock = scr->dock) != NULL) {
1775 for (i = 0; i < dock->max_icons; i++) {
1776 if (dock->icon_array[i]
1777 && dock->icon_array[i]->icon->core->window == event->xclient.window) {
1778 *icon_pos = i;
1779 break;
1783 if (*icon_pos < 0 && (dock = scr->workspaces[scr->current_workspace]->clip) != NULL) {
1784 for (i = 0; i < dock->max_icons; i++) {
1785 if (dock->icon_array[i]
1786 && dock->icon_array[i]->icon->core->window == event->xclient.window) {
1787 *icon_pos = i;
1788 break;
1792 if (*icon_pos >= 0)
1793 return dock;
1794 return NULL;
1797 int wDockReceiveDNDDrop(WScreen *scr, XEvent *event)
1799 WDock *dock;
1800 WAppIcon *btn;
1801 int icon_pos;
1803 dock = findDock(scr, event, &icon_pos);
1804 if (!dock)
1805 return False;
1808 * Return True if the drop was on an application icon window.
1809 * In this case, let the ClientMessage handler redirect the
1810 * message to the app.
1812 if (dock->icon_array[icon_pos]->icon->icon_win != None)
1813 return True;
1815 if (dock->icon_array[icon_pos]->dnd_command != NULL) {
1816 scr->flags.dnd_data_convertion_status = 0;
1818 btn = dock->icon_array[icon_pos];
1820 if (!btn->forced_dock) {
1821 btn->relaunching = btn->running;
1822 btn->running = 1;
1824 if (btn->wm_instance || btn->wm_class) {
1825 WWindowAttributes attr;
1826 memset(&attr, 0, sizeof(WWindowAttributes));
1827 wDefaultFillAttributes(btn->wm_instance, btn->wm_class, &attr, NULL, True);
1829 if (!attr.no_appicon)
1830 btn->launching = 1;
1831 else
1832 btn->running = 0;
1835 btn->paste_launch = 0;
1836 btn->drop_launch = 1;
1837 scr->last_dock = dock;
1838 btn->pid = execCommand(btn, btn->dnd_command, NULL);
1839 if (btn->pid > 0) {
1840 dockIconPaint(btn);
1841 } else {
1842 btn->launching = 0;
1843 if (!btn->relaunching)
1844 btn->running = 0;
1847 return False;
1849 #endif /* XDND */
1851 Bool wDockAttachIcon(WDock *dock, WAppIcon *icon, int x, int y)
1853 WWindow *wwin;
1854 int index;
1856 wwin = icon->icon->owner;
1857 if (icon->command == NULL) {
1858 char *command;
1860 icon->editing = 0;
1862 command = GetCommandForWindow(wwin->client_win);
1863 if (command) {
1864 icon->command = command;
1865 } else {
1866 /* icon->forced_dock = 1; */
1867 if (dock->type != WM_CLIP || !icon->attracted) {
1868 icon->editing = 1;
1869 if (wInputDialog(dock->screen_ptr, _("Dock Icon"),
1870 _("Type the command used to launch the application"), &command)) {
1871 if (command && (command[0] == 0 || (command[0] == '-' && command[1] == 0))) {
1872 wfree(command);
1873 command = NULL;
1875 icon->command = command;
1876 icon->editing = 0;
1877 } else {
1878 icon->editing = 0;
1879 if (command)
1880 wfree(command);
1881 /* If the target is the dock, reject the icon. If
1882 * the target is the clip, make it an attracted icon
1884 if (dock->type == WM_CLIP) {
1885 icon->attracted = 1;
1886 if (!icon->icon->shadowed) {
1887 icon->icon->shadowed = 1;
1888 icon->icon->force_paint = 1;
1890 } else {
1891 return False;
1896 } else {
1897 icon->editing = 0;
1900 for (index = 1; index < dock->max_icons; index++)
1901 if (dock->icon_array[index] == NULL)
1902 break;
1903 /* if (index == dock->max_icons)
1904 return; */
1906 assert(index < dock->max_icons);
1908 dock->icon_array[index] = icon;
1909 icon->yindex = y;
1910 icon->xindex = x;
1912 icon->omnipresent = 0;
1914 icon->x_pos = dock->x_pos + x * ICON_SIZE;
1915 icon->y_pos = dock->y_pos + y * ICON_SIZE;
1917 dock->icon_count++;
1919 icon->running = 1;
1920 icon->launching = 0;
1921 icon->docked = 1;
1922 icon->dock = dock;
1923 icon->icon->core->descriptor.handle_mousedown = iconMouseDown;
1924 if (dock->type == WM_CLIP) {
1925 icon->icon->core->descriptor.handle_enternotify = clipEnterNotify;
1926 icon->icon->core->descriptor.handle_leavenotify = clipLeaveNotify;
1928 icon->icon->core->descriptor.parent_type = WCLASS_DOCK_ICON;
1929 icon->icon->core->descriptor.parent = icon;
1931 MoveInStackListUnder(dock->icon_array[index - 1]->icon->core, icon->icon->core);
1932 wAppIconMove(icon, icon->x_pos, icon->y_pos);
1933 wAppIconPaint(icon);
1934 save_appicon(icon, True);
1936 if (wPreferences.auto_arrange_icons)
1937 wArrangeIcons(dock->screen_ptr, True);
1939 #ifdef XDND /* was OFFIX */
1940 if (icon->command && !icon->dnd_command) {
1941 int len = strlen(icon->command) + 8;
1942 icon->dnd_command = wmalloc(len);
1943 snprintf(icon->dnd_command, len, "%s %%d", icon->command);
1945 #endif
1947 if (icon->command && !icon->paste_command) {
1948 int len = strlen(icon->command) + 8;
1949 icon->paste_command = wmalloc(len);
1950 snprintf(icon->paste_command, len, "%s %%s", icon->command);
1953 return True;
1956 static void reattachIcon(WDock *dock, WAppIcon *icon, int x, int y)
1958 int index;
1960 for (index = 1; index < dock->max_icons; index++) {
1961 if (dock->icon_array[index] == icon)
1962 break;
1964 assert(index < dock->max_icons);
1966 icon->yindex = y;
1967 icon->xindex = x;
1969 icon->x_pos = dock->x_pos + x * ICON_SIZE;
1970 icon->y_pos = dock->y_pos + y * ICON_SIZE;
1973 static Bool moveIconBetweenDocks(WDock *src, WDock *dest, WAppIcon *icon, int x, int y)
1975 WWindow *wwin;
1976 char *command;
1977 int index;
1979 if (src == dest)
1980 return True; /* No move needed, we're already there */
1982 if (dest == NULL)
1983 return False;
1985 wwin = icon->icon->owner;
1988 * For the moment we can't do this if we move icons in Clip from one
1989 * workspace to other, because if we move two or more icons without
1990 * command, the dialog box will not be able to tell us to which of the
1991 * moved icons it applies. -Dan
1993 if ((dest->type == WM_DOCK /*|| dest->keep_attracted */ ) && icon->command == NULL) {
1994 command = GetCommandForWindow(wwin->client_win);
1995 if (command) {
1996 icon->command = command;
1997 } else {
1998 icon->editing = 1;
1999 /* icon->forced_dock = 1; */
2000 if (wInputDialog(src->screen_ptr, _("Dock Icon"),
2001 _("Type the command used to launch the application"), &command)) {
2002 if (command && (command[0] == 0 || (command[0] == '-' && command[1] == 0))) {
2003 wfree(command);
2004 command = NULL;
2006 icon->command = command;
2007 } else {
2008 icon->editing = 0;
2009 if (command)
2010 wfree(command);
2011 return False;
2013 icon->editing = 0;
2017 if (dest->type == WM_DOCK)
2018 wClipMakeIconOmnipresent(icon, False);
2020 for (index = 1; index < src->max_icons; index++) {
2021 if (src->icon_array[index] == icon)
2022 break;
2024 assert(index < src->max_icons);
2026 src->icon_array[index] = NULL;
2027 src->icon_count--;
2029 for (index = 1; index < dest->max_icons; index++) {
2030 if (dest->icon_array[index] == NULL)
2031 break;
2034 assert(index < dest->max_icons);
2036 dest->icon_array[index] = icon;
2037 icon->dock = dest;
2039 /* deselect the icon */
2040 if (icon->icon->selected)
2041 wIconSelect(icon->icon);
2043 if (dest->type == WM_DOCK) {
2044 icon->icon->core->descriptor.handle_enternotify = NULL;
2045 icon->icon->core->descriptor.handle_leavenotify = NULL;
2046 } else {
2047 icon->icon->core->descriptor.handle_enternotify = clipEnterNotify;
2048 icon->icon->core->descriptor.handle_leavenotify = clipLeaveNotify;
2051 /* set it to be kept when moving to dock.
2052 * Unless the icon does not have a command set
2054 if (icon->command && dest->type == WM_DOCK) {
2055 icon->attracted = 0;
2056 if (icon->icon->shadowed) {
2057 icon->icon->shadowed = 0;
2058 icon->icon->force_paint = 1;
2060 save_appicon(icon, True);
2063 if (src->auto_collapse || src->auto_raise_lower)
2064 clipLeave(src);
2066 icon->yindex = y;
2067 icon->xindex = x;
2069 icon->x_pos = dest->x_pos + x * ICON_SIZE;
2070 icon->y_pos = dest->y_pos + y * ICON_SIZE;
2072 dest->icon_count++;
2074 MoveInStackListUnder(dest->icon_array[index - 1]->icon->core, icon->icon->core);
2075 wAppIconPaint(icon);
2077 return True;
2080 void wDockDetach(WDock *dock, WAppIcon *icon)
2082 int index;
2084 /* make the settings panel be closed */
2085 if (icon->panel)
2086 DestroyDockAppSettingsPanel(icon->panel);
2088 /* This must be called before icon->dock is set to NULL.
2089 * Don't move it. -Dan
2091 wClipMakeIconOmnipresent(icon, False);
2093 icon->docked = 0;
2094 icon->dock = NULL;
2095 icon->attracted = 0;
2096 icon->auto_launch = 0;
2097 if (icon->icon->shadowed) {
2098 icon->icon->shadowed = 0;
2099 icon->icon->force_paint = 1;
2102 /* deselect the icon */
2103 if (icon->icon->selected)
2104 wIconSelect(icon->icon);
2106 if (icon->command) {
2107 wfree(icon->command);
2108 icon->command = NULL;
2110 #ifdef XDND /* was OFFIX */
2111 if (icon->dnd_command) {
2112 wfree(icon->dnd_command);
2113 icon->dnd_command = NULL;
2115 #endif
2116 if (icon->paste_command) {
2117 wfree(icon->paste_command);
2118 icon->paste_command = NULL;
2121 for (index = 1; index < dock->max_icons; index++)
2122 if (dock->icon_array[index] == icon)
2123 break;
2125 assert(index < dock->max_icons);
2126 dock->icon_array[index] = NULL;
2127 icon->yindex = -1;
2128 icon->xindex = -1;
2130 dock->icon_count--;
2132 /* if the dock is not attached to an application or
2133 * the application did not set the appropriate hints yet,
2134 * destroy the icon */
2135 if (!icon->running || !wApplicationOf(icon->main_window)) {
2136 wAppIconDestroy(icon);
2137 } else {
2138 icon->icon->core->descriptor.handle_mousedown = appIconMouseDown;
2139 icon->icon->core->descriptor.handle_enternotify = NULL;
2140 icon->icon->core->descriptor.handle_leavenotify = NULL;
2141 icon->icon->core->descriptor.parent_type = WCLASS_APPICON;
2142 icon->icon->core->descriptor.parent = icon;
2144 ChangeStackingLevel(icon->icon->core, NORMAL_ICON_LEVEL);
2146 wAppIconPaint(icon);
2147 if (wPreferences.auto_arrange_icons)
2148 wArrangeIcons(dock->screen_ptr, True);
2150 if (dock->auto_collapse || dock->auto_raise_lower)
2151 clipLeave(dock);
2155 * returns the closest Dock slot index for the passed
2156 * coordinates.
2158 * Returns False if icon can't be docked.
2160 * Note: this function should NEVER alter ret_x or ret_y, unless it will
2161 * return True. -Dan
2163 Bool wDockSnapIcon(WDock *dock, WAppIcon *icon, int req_x, int req_y, int *ret_x, int *ret_y, int redocking)
2165 WScreen *scr = dock->screen_ptr;
2166 int dx, dy;
2167 int ex_x, ex_y;
2168 int i, offset = ICON_SIZE / 2;
2169 WAppIcon *aicon = NULL;
2170 WAppIcon *nicon = NULL;
2171 int max_y_icons;
2173 /* TODO: XINERAMA, for these */
2174 max_y_icons = scr->scr_height / ICON_SIZE - 1;
2176 if (wPreferences.flags.noupdates)
2177 return False;
2179 dx = dock->x_pos;
2180 dy = dock->y_pos;
2182 /* if the dock is full */
2183 if (!redocking && (dock->icon_count >= dock->max_icons))
2184 return False;
2186 /* exact position */
2187 if (req_y < dy)
2188 ex_y = (req_y - offset - dy) / ICON_SIZE;
2189 else
2190 ex_y = (req_y + offset - dy) / ICON_SIZE;
2192 if (req_x < dx)
2193 ex_x = (req_x - offset - dx) / ICON_SIZE;
2194 else
2195 ex_x = (req_x + offset - dx) / ICON_SIZE;
2197 /* check if the icon is outside the screen boundaries */
2199 WMRect rect;
2200 int flags;
2202 rect.pos.x = dx + ex_x * ICON_SIZE;
2203 rect.pos.y = dy + ex_y * ICON_SIZE;
2204 rect.size.width = rect.size.height = ICON_SIZE;
2206 wGetRectPlacementInfo(scr, rect, &flags);
2207 if (flags & (XFLAG_DEAD | XFLAG_PARTIAL))
2208 return False;
2211 if (dock->type == WM_DOCK) {
2212 if (icon->dock != dock && ex_x != 0)
2213 return False;
2215 aicon = NULL;
2216 for (i = 0; i < dock->max_icons; i++) {
2217 nicon = dock->icon_array[i];
2218 if (nicon && nicon->yindex == ex_y) {
2219 aicon = nicon;
2220 break;
2224 if (redocking) {
2225 int sig, done, closest;
2227 /* Possible cases when redocking:
2229 * icon dragged out of range of any slot -> false
2230 * icon dragged to range of free slot
2231 * icon dragged to range of same slot
2232 * icon dragged to range of different icon
2234 if (abs(ex_x) > DOCK_DETTACH_THRESHOLD)
2235 return False;
2237 if (ex_y >= 0 && ex_y <= max_y_icons && (aicon == icon || !aicon)) {
2238 *ret_x = 0;
2239 *ret_y = ex_y;
2240 return True;
2243 /* start looking at the upper slot or lower? */
2244 if (ex_y * ICON_SIZE < (req_y + offset - dy))
2245 sig = 1;
2246 else
2247 sig = -1;
2249 closest = -1;
2250 done = 0;
2251 /* look for closest free slot */
2252 for (i = 0; i < (DOCK_DETTACH_THRESHOLD + 1) * 2 && !done; i++) {
2253 int j;
2255 done = 1;
2256 closest = sig * (i / 2) + ex_y;
2257 /* check if this slot is used */
2258 if (closest >= 0) {
2259 for (j = 0; j < dock->max_icons; j++) {
2260 if (dock->icon_array[j]
2261 && dock->icon_array[j]->yindex == closest) {
2262 /* slot is used by someone else */
2263 if (dock->icon_array[j] != icon)
2264 done = 0;
2265 break;
2269 sig = -sig;
2271 if (done && closest >= 0 && closest <= max_y_icons &&
2272 ((ex_y >= closest && ex_y - closest < DOCK_DETTACH_THRESHOLD + 1)
2273 || (ex_y < closest && closest - ex_y <= DOCK_DETTACH_THRESHOLD + 1))) {
2274 *ret_x = 0;
2275 *ret_y = closest;
2276 return True;
2278 } else { /* !redocking */
2280 /* if slot is free and the icon is close enough, return it */
2281 if (!aicon && ex_x == 0 && ex_y >= 0 && ex_y <= max_y_icons) {
2282 *ret_x = 0;
2283 *ret_y = ex_y;
2284 return True;
2287 } else { /* CLIP */
2288 int neighbours = 0;
2289 int start, stop, k;
2291 start = icon->omnipresent ? 0 : scr->current_workspace;
2292 stop = icon->omnipresent ? scr->workspace_count : start + 1;
2294 aicon = NULL;
2295 for (k = start; k < stop; k++) {
2296 WDock *tmp = scr->workspaces[k]->clip;
2297 if (!tmp)
2298 continue;
2299 for (i = 0; i < tmp->max_icons; i++) {
2300 nicon = tmp->icon_array[i];
2301 if (nicon && nicon->xindex == ex_x && nicon->yindex == ex_y) {
2302 aicon = nicon;
2303 break;
2306 if (aicon)
2307 break;
2309 for (k = start; k < stop; k++) {
2310 WDock *tmp = scr->workspaces[k]->clip;
2311 if (!tmp)
2312 continue;
2313 for (i = 0; i < tmp->max_icons; i++) {
2314 nicon = tmp->icon_array[i];
2315 if (nicon && nicon != icon && /* Icon can't be it's own neighbour */
2316 (abs(nicon->xindex - ex_x) <= CLIP_ATTACH_VICINITY &&
2317 abs(nicon->yindex - ex_y) <= CLIP_ATTACH_VICINITY)) {
2318 neighbours = 1;
2319 break;
2322 if (neighbours)
2323 break;
2326 if (neighbours && (aicon == NULL || (redocking && aicon == icon))) {
2327 *ret_x = ex_x;
2328 *ret_y = ex_y;
2329 return True;
2332 return False;
2335 static int onScreen(WScreen *scr, int x, int y, int sx, int ex, int sy, int ey)
2337 WMRect rect = wmkrect(x, y, ICON_SIZE, ICON_SIZE);
2338 int flags;
2340 wGetRectPlacementInfo(scr, rect, &flags);
2342 return !(flags & (XFLAG_DEAD | XFLAG_PARTIAL));
2346 * returns true if it can find a free slot in the dock,
2347 * in which case it changes x_pos and y_pos accordingly.
2348 * Else returns false.
2350 Bool wDockFindFreeSlot(WDock *dock, int *x_pos, int *y_pos)
2352 WScreen *scr = dock->screen_ptr;
2353 WAppIcon *btn;
2354 WAppIconChain *chain;
2355 unsigned char *slot_map;
2356 int mwidth;
2357 int r;
2358 int x, y;
2359 int i, done = False;
2360 int corner;
2361 int sx = 0, sy = 0, ex = scr->scr_width, ey = scr->scr_height;
2362 int extra_count = 0;
2364 if (dock->type == WM_CLIP && dock != scr->workspaces[scr->current_workspace]->clip)
2365 extra_count = scr->global_icon_count;
2367 /* if the dock is full */
2368 if (dock->icon_count + extra_count >= dock->max_icons)
2369 return False;
2371 if (!wPreferences.flags.nodock && scr->dock) {
2372 if (scr->dock->on_right_side)
2373 ex -= ICON_SIZE + DOCK_EXTRA_SPACE;
2374 else
2375 sx += ICON_SIZE + DOCK_EXTRA_SPACE;
2378 if (ex < dock->x_pos)
2379 ex = dock->x_pos;
2380 if (sx > dock->x_pos + ICON_SIZE)
2381 sx = dock->x_pos + ICON_SIZE;
2382 #define C_NONE 0
2383 #define C_NW 1
2384 #define C_NE 2
2385 #define C_SW 3
2386 #define C_SE 4
2388 /* check if clip is in a corner */
2389 if (dock->type == WM_CLIP) {
2390 if (dock->x_pos < 1 && dock->y_pos < 1)
2391 corner = C_NE;
2392 else if (dock->x_pos < 1 && dock->y_pos >= (ey - ICON_SIZE))
2393 corner = C_SE;
2394 else if (dock->x_pos >= (ex - ICON_SIZE) && dock->y_pos >= (ey - ICON_SIZE))
2395 corner = C_SW;
2396 else if (dock->x_pos >= (ex - ICON_SIZE) && dock->y_pos < 1)
2397 corner = C_NW;
2398 else
2399 corner = C_NONE;
2400 } else {
2401 corner = C_NONE;
2404 /* If the clip is in the corner, use only slots that are in the border
2405 * of the screen */
2406 if (corner != C_NONE) {
2407 char *hmap, *vmap;
2408 int hcount, vcount;
2410 hcount = WMIN(dock->max_icons, scr->scr_width / ICON_SIZE);
2411 vcount = WMIN(dock->max_icons, scr->scr_height / ICON_SIZE);
2412 hmap = wmalloc(hcount + 1);
2413 vmap = wmalloc(vcount + 1);
2415 /* mark used positions */
2416 switch (corner) {
2417 case C_NE:
2418 for (i = 0; i < dock->max_icons; i++) {
2419 btn = dock->icon_array[i];
2420 if (!btn)
2421 continue;
2423 if (btn->xindex == 0 && btn->yindex > 0 && btn->yindex < vcount)
2424 vmap[btn->yindex] = 1;
2425 else if (btn->yindex == 0 && btn->xindex > 0 && btn->xindex < hcount)
2426 hmap[btn->xindex] = 1;
2428 for (chain = scr->global_icons; chain != NULL; chain = chain->next) {
2429 btn = chain->aicon;
2430 if (btn->xindex == 0 && btn->yindex > 0 && btn->yindex < vcount)
2431 vmap[btn->yindex] = 1;
2432 else if (btn->yindex == 0 && btn->xindex > 0 && btn->xindex < hcount)
2433 hmap[btn->xindex] = 1;
2435 break;
2436 case C_NW:
2437 for (i = 0; i < dock->max_icons; i++) {
2438 btn = dock->icon_array[i];
2439 if (!btn)
2440 continue;
2442 if (btn->xindex == 0 && btn->yindex > 0 && btn->yindex < vcount)
2443 vmap[btn->yindex] = 1;
2444 else if (btn->yindex == 0 && btn->xindex < 0 && btn->xindex > -hcount)
2445 hmap[-btn->xindex] = 1;
2447 for (chain = scr->global_icons; chain != NULL; chain = chain->next) {
2448 btn = chain->aicon;
2449 if (btn->xindex == 0 && btn->yindex > 0 && btn->yindex < vcount)
2450 vmap[btn->yindex] = 1;
2451 else if (btn->yindex == 0 && btn->xindex < 0 && btn->xindex > -hcount)
2452 hmap[-btn->xindex] = 1;
2454 break;
2455 case C_SE:
2456 for (i = 0; i < dock->max_icons; i++) {
2457 btn = dock->icon_array[i];
2458 if (!btn)
2459 continue;
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 for (chain = scr->global_icons; chain != NULL; chain = chain->next) {
2467 btn = chain->aicon;
2468 if (btn->xindex == 0 && btn->yindex < 0 && btn->yindex > -vcount)
2469 vmap[-btn->yindex] = 1;
2470 else if (btn->yindex == 0 && btn->xindex > 0 && btn->xindex < hcount)
2471 hmap[btn->xindex] = 1;
2473 break;
2474 case C_SW:
2475 default:
2476 for (i = 0; i < dock->max_icons; i++) {
2477 btn = dock->icon_array[i];
2478 if (!btn)
2479 continue;
2481 if (btn->xindex == 0 && btn->yindex < 0 && btn->yindex > -vcount)
2482 vmap[-btn->yindex] = 1;
2483 else if (btn->yindex == 0 && btn->xindex < 0 && btn->xindex > -hcount)
2484 hmap[-btn->xindex] = 1;
2486 for (chain = scr->global_icons; chain != NULL; chain = chain->next) {
2487 btn = chain->aicon;
2488 if (btn->xindex == 0 && btn->yindex < 0 && btn->yindex > -vcount)
2489 vmap[-btn->yindex] = 1;
2490 else if (btn->yindex == 0 && btn->xindex < 0 && btn->xindex > -hcount)
2491 hmap[-btn->xindex] = 1;
2494 x = 0;
2495 y = 0;
2496 done = 0;
2497 /* search a vacant slot */
2498 for (i = 1; i < WMAX(vcount, hcount); i++) {
2499 if (i < vcount && vmap[i] == 0) {
2500 /* found a slot */
2501 x = 0;
2502 y = i;
2503 done = 1;
2504 break;
2505 } else if (i < hcount && hmap[i] == 0) {
2506 /* found a slot */
2507 x = i;
2508 y = 0;
2509 done = 1;
2510 break;
2513 wfree(vmap);
2514 wfree(hmap);
2515 /* If found a slot, translate and return */
2516 if (done) {
2517 if (corner == C_NW || corner == C_NE)
2518 *y_pos = y;
2519 else
2520 *y_pos = -y;
2522 if (corner == C_NE || corner == C_SE)
2523 *x_pos = x;
2524 else
2525 *x_pos = -x;
2527 return True;
2529 /* else, try to find a slot somewhere else */
2532 /* a map of mwidth x mwidth would be enough if we allowed icons to be
2533 * placed outside of screen */
2534 mwidth = (int)ceil(sqrt(dock->max_icons));
2536 /* In the worst case (the clip is in the corner of the screen),
2537 * the amount of icons that fit in the clip is smaller.
2538 * Double the map to get a safe value.
2540 mwidth += mwidth;
2542 r = (mwidth - 1) / 2;
2544 slot_map = wmalloc(mwidth * mwidth);
2546 #define XY2OFS(x,y) (WMAX(abs(x),abs(y)) > r) ? 0 : (((y)+r)*(mwidth)+(x)+r)
2548 /* mark used slots in the map. If the slot falls outside the map
2549 * (for example, when all icons are placed in line), ignore them. */
2550 for (i = 0; i < dock->max_icons; i++) {
2551 btn = dock->icon_array[i];
2552 if (btn)
2553 slot_map[XY2OFS(btn->xindex, btn->yindex)] = 1;
2556 for (chain = scr->global_icons; chain != NULL; chain = chain->next)
2557 slot_map[XY2OFS(chain->aicon->xindex, chain->aicon->yindex)] = 1;
2559 /* Find closest slot from the center that is free by scanning the
2560 * map from the center to outward in circular passes.
2561 * This will not result in a neat layout, but will be optimal
2562 * in the sense that there will not be holes left.
2564 done = 0;
2565 for (i = 1; i <= r && !done; i++) {
2566 int tx, ty;
2568 /* top and bottom parts of the ring */
2569 for (x = -i; x <= i && !done; x++) {
2570 tx = dock->x_pos + x * ICON_SIZE;
2571 y = -i;
2572 ty = dock->y_pos + y * ICON_SIZE;
2573 if (slot_map[XY2OFS(x, y)] == 0 && onScreen(scr, tx, ty, sx, ex, sy, ey)) {
2574 *x_pos = x;
2575 *y_pos = y;
2576 done = 1;
2577 break;
2579 y = i;
2580 ty = dock->y_pos + y * ICON_SIZE;
2581 if (slot_map[XY2OFS(x, y)] == 0 && onScreen(scr, tx, ty, sx, ex, sy, ey)) {
2582 *x_pos = x;
2583 *y_pos = y;
2584 done = 1;
2585 break;
2588 /* left and right parts of the ring */
2589 for (y = -i + 1; y <= i - 1; y++) {
2590 ty = dock->y_pos + y * ICON_SIZE;
2591 x = -i;
2592 tx = dock->x_pos + x * ICON_SIZE;
2593 if (slot_map[XY2OFS(x, y)] == 0 && onScreen(scr, tx, ty, sx, ex, sy, ey)) {
2594 *x_pos = x;
2595 *y_pos = y;
2596 done = 1;
2597 break;
2599 x = i;
2600 tx = dock->x_pos + x * ICON_SIZE;
2601 if (slot_map[XY2OFS(x, y)] == 0 && onScreen(scr, tx, ty, sx, ex, sy, ey)) {
2602 *x_pos = x;
2603 *y_pos = y;
2604 done = 1;
2605 break;
2609 wfree(slot_map);
2610 #undef XY2OFS
2611 return done;
2614 static void moveDock(WDock *dock, int new_x, int new_y)
2616 WAppIcon *btn;
2617 int i;
2619 dock->x_pos = new_x;
2620 dock->y_pos = new_y;
2621 for (i = 0; i < dock->max_icons; i++) {
2622 btn = dock->icon_array[i];
2623 if (btn) {
2624 btn->x_pos = new_x + btn->xindex * ICON_SIZE;
2625 btn->y_pos = new_y + btn->yindex * ICON_SIZE;
2626 XMoveWindow(dpy, btn->icon->core->window, btn->x_pos, btn->y_pos);
2631 static void swapDock(WDock *dock)
2633 WScreen *scr = dock->screen_ptr;
2634 WAppIcon *btn;
2635 int x, i;
2637 if (dock->on_right_side)
2638 x = dock->x_pos = scr->scr_width - ICON_SIZE - DOCK_EXTRA_SPACE;
2639 else
2640 x = dock->x_pos = DOCK_EXTRA_SPACE;
2642 for (i = 0; i < dock->max_icons; i++) {
2643 btn = dock->icon_array[i];
2644 if (btn) {
2645 btn->x_pos = x;
2646 XMoveWindow(dpy, btn->icon->core->window, btn->x_pos, btn->y_pos);
2650 wScreenUpdateUsableArea(scr);
2653 static pid_t execCommand(WAppIcon *btn, char *command, WSavedState *state)
2655 WScreen *scr = btn->icon->core->screen_ptr;
2656 pid_t pid;
2657 char **argv;
2658 int argc;
2659 char *cmdline;
2661 cmdline = ExpandOptions(scr, command);
2663 if (scr->flags.dnd_data_convertion_status || !cmdline) {
2664 if (cmdline)
2665 wfree(cmdline);
2666 if (state)
2667 wfree(state);
2668 return 0;
2671 wtokensplit(cmdline, &argv, &argc);
2673 if (!argc) {
2674 if (cmdline)
2675 wfree(cmdline);
2676 if (state)
2677 wfree(state);
2678 return 0;
2681 if ((pid = fork()) == 0) {
2682 char **args;
2683 int i;
2685 SetupEnvironment(scr);
2687 #ifdef HAVE_SETSID
2688 setsid();
2689 #endif
2691 args = malloc(sizeof(char *) * (argc + 1));
2692 if (!args)
2693 exit(111);
2695 for (i = 0; i < argc; i++)
2696 args[i] = argv[i];
2698 args[argc] = NULL;
2699 execvp(argv[0], args);
2700 exit(111);
2702 wtokenfree(argv, argc);
2704 if (pid > 0) {
2705 if (!state) {
2706 state = wmalloc(sizeof(WSavedState));
2707 state->hidden = -1;
2708 state->miniaturized = -1;
2709 state->shaded = -1;
2710 if (btn->dock == scr->dock || btn->omnipresent)
2711 state->workspace = -1;
2712 else
2713 state->workspace = scr->current_workspace;
2715 wWindowAddSavedState(btn->wm_instance, btn->wm_class, cmdline, pid, state);
2716 wAddDeathHandler(pid, (WDeathHandler *) trackDeadProcess, btn->dock);
2717 } else if (state) {
2718 wfree(state);
2720 wfree(cmdline);
2721 return pid;
2724 void wDockHideIcons(WDock *dock)
2726 int i;
2728 if (dock == NULL)
2729 return;
2731 for (i = 1; i < dock->max_icons; i++) {
2732 if (dock->icon_array[i])
2733 XUnmapWindow(dpy, dock->icon_array[i]->icon->core->window);
2735 dock->mapped = 0;
2737 dockIconPaint(dock->icon_array[0]);
2740 void wDockShowIcons(WDock *dock)
2742 int i, newlevel;
2743 WAppIcon *btn;
2745 if (dock == NULL)
2746 return;
2748 btn = dock->icon_array[0];
2749 moveDock(dock, btn->x_pos, btn->y_pos);
2751 newlevel = dock->lowered ? WMNormalLevel : WMDockLevel;
2752 ChangeStackingLevel(btn->icon->core, newlevel);
2754 for (i = 1; i < dock->max_icons; i++) {
2755 if (dock->icon_array[i]) {
2756 MoveInStackListAbove(dock->icon_array[i]->icon->core, btn->icon->core);
2757 break;
2761 if (!dock->collapsed) {
2762 for (i = 1; i < dock->max_icons; i++) {
2763 if (dock->icon_array[i])
2764 XMapWindow(dpy, dock->icon_array[i]->icon->core->window);
2767 dock->mapped = 1;
2769 dockIconPaint(btn);
2772 void wDockLower(WDock *dock)
2774 int i;
2776 for (i = 0; i < dock->max_icons; i++) {
2777 if (dock->icon_array[i])
2778 wLowerFrame(dock->icon_array[i]->icon->core);
2782 void wDockRaise(WDock *dock)
2784 int i;
2786 for (i = dock->max_icons - 1; i >= 0; i--) {
2787 if (dock->icon_array[i])
2788 wRaiseFrame(dock->icon_array[i]->icon->core);
2792 void wDockRaiseLower(WDock *dock)
2794 if (!dock->icon_array[0]->icon->core->stacking->above
2795 || (dock->icon_array[0]->icon->core->stacking->window_level
2796 != dock->icon_array[0]->icon->core->stacking->above->stacking->window_level))
2797 wDockLower(dock);
2798 else
2799 wDockRaise(dock);
2802 void wDockFinishLaunch(WDock *dock, WAppIcon *icon)
2804 icon->launching = 0;
2805 icon->relaunching = 0;
2806 dockIconPaint(icon);
2809 WAppIcon *wDockFindIconForWindow(WDock *dock, Window window)
2811 WAppIcon *icon;
2812 int i;
2814 for (i = 0; i < dock->max_icons; i++) {
2815 icon = dock->icon_array[i];
2816 if (icon && icon->main_window == window)
2817 return icon;
2819 return NULL;
2822 void wDockTrackWindowLaunch(WDock *dock, Window window)
2824 WAppIcon *icon;
2825 char *wm_class, *wm_instance;
2826 int i;
2827 Bool firstPass = True;
2828 Bool found = False;
2829 char *command = NULL;
2831 if (!PropGetWMClass(window, &wm_class, &wm_instance) || (!wm_class && !wm_instance))
2832 return;
2834 command = GetCommandForWindow(window);
2835 retry:
2836 for (i = 0; i < dock->max_icons; i++) {
2837 icon = dock->icon_array[i];
2838 if (!icon)
2839 continue;
2841 /* app is already attached to icon */
2842 if (icon->main_window == window) {
2843 found = True;
2844 break;
2847 if ((icon->wm_instance || icon->wm_class)
2848 && (icon->launching || !icon->running)) {
2850 if (icon->wm_instance && wm_instance && strcmp(icon->wm_instance, wm_instance) != 0)
2851 continue;
2853 if (icon->wm_class && wm_class && strcmp(icon->wm_class, wm_class) != 0)
2854 continue;
2856 if (firstPass && command && strcmp(icon->command, command) != 0)
2857 continue;
2859 if (!icon->relaunching) {
2860 WApplication *wapp;
2862 /* Possibly an application that was docked with dockit,
2863 * but the user did not update WMState to indicate that
2864 * it was docked by force */
2865 wapp = wApplicationOf(window);
2866 if (!wapp) {
2867 icon->forced_dock = 1;
2868 icon->running = 0;
2870 if (!icon->forced_dock)
2871 icon->main_window = window;
2873 found = True;
2874 if (!wPreferences.no_animations && !icon->launching &&
2875 !dock->screen_ptr->flags.startup && !dock->collapsed) {
2876 WAppIcon *aicon;
2877 int x0, y0;
2879 icon->launching = 1;
2880 dockIconPaint(icon);
2882 aicon = wAppIconCreateForDock(dock->screen_ptr, NULL,
2883 wm_instance, wm_class, TILE_NORMAL);
2884 /* XXX: can: aicon->icon == NULL ? */
2885 PlaceIcon(dock->screen_ptr, &x0, &y0, wGetHeadForWindow(aicon->icon->owner));
2886 wAppIconMove(aicon, x0, y0);
2887 /* Should this always be lowered? -Dan */
2888 if (dock->lowered)
2889 wLowerFrame(aicon->icon->core);
2890 XMapWindow(dpy, aicon->icon->core->window);
2891 aicon->launching = 1;
2892 wAppIconPaint(aicon);
2893 SlideWindow(aicon->icon->core->window, x0, y0, icon->x_pos, icon->y_pos);
2894 XUnmapWindow(dpy, aicon->icon->core->window);
2895 wAppIconDestroy(aicon);
2897 wDockFinishLaunch(dock, icon);
2898 break;
2902 if (firstPass && !found) {
2903 firstPass = False;
2904 goto retry;
2907 if (command)
2908 wfree(command);
2910 if (wm_class)
2911 free(wm_class);
2912 if (wm_instance)
2913 free(wm_instance);
2916 void wClipUpdateForWorkspaceChange(WScreen *scr, int workspace)
2918 if (!wPreferences.flags.noclip) {
2919 scr->clip_icon->dock = scr->workspaces[workspace]->clip;
2920 if (scr->current_workspace != workspace) {
2921 WDock *old_clip = scr->workspaces[scr->current_workspace]->clip;
2922 WAppIconChain *chain = scr->global_icons;
2924 while (chain) {
2925 moveIconBetweenDocks(chain->aicon->dock,
2926 scr->workspaces[workspace]->clip,
2927 chain->aicon, chain->aicon->xindex, chain->aicon->yindex);
2928 if (scr->workspaces[workspace]->clip->collapsed)
2929 XUnmapWindow(dpy, chain->aicon->icon->core->window);
2930 chain = chain->next;
2933 wDockHideIcons(old_clip);
2934 if (old_clip->auto_raise_lower) {
2935 if (old_clip->auto_raise_magic) {
2936 WMDeleteTimerHandler(old_clip->auto_raise_magic);
2937 old_clip->auto_raise_magic = NULL;
2939 wDockLower(old_clip);
2941 if (old_clip->auto_collapse) {
2942 if (old_clip->auto_expand_magic) {
2943 WMDeleteTimerHandler(old_clip->auto_expand_magic);
2944 old_clip->auto_expand_magic = NULL;
2946 old_clip->collapsed = 1;
2948 wDockShowIcons(scr->workspaces[workspace]->clip);
2953 static void trackDeadProcess(pid_t pid, unsigned char status, WDock *dock)
2955 WAppIcon *icon;
2956 int i;
2958 for (i = 0; i < dock->max_icons; i++) {
2959 icon = dock->icon_array[i];
2960 if (!icon)
2961 continue;
2963 if (icon->launching && icon->pid == pid) {
2964 if (!icon->relaunching) {
2965 icon->running = 0;
2966 icon->main_window = None;
2968 wDockFinishLaunch(dock, icon);
2969 icon->pid = 0;
2970 if (status == 111) {
2971 char msg[PATH_MAX];
2972 char *cmd;
2974 #ifdef XDND
2975 if (icon->drop_launch)
2976 cmd = icon->dnd_command;
2977 else
2978 #endif
2979 if (icon->paste_launch)
2980 cmd = icon->paste_command;
2981 else
2982 cmd = icon->command;
2984 snprintf(msg, sizeof(msg), _("Could not execute command \"%s\""), cmd);
2986 wMessageDialog(dock->screen_ptr, _("Error"), msg, _("OK"), NULL, NULL);
2988 break;
2993 static void toggleLowered(WDock *dock)
2995 WAppIcon *tmp;
2996 int newlevel, i;
2998 /* lower/raise Dock */
2999 if (!dock->lowered) {
3000 newlevel = WMNormalLevel;
3001 dock->lowered = 1;
3002 } else {
3003 newlevel = WMDockLevel;
3004 dock->lowered = 0;
3007 for (i = 0; i < dock->max_icons; i++) {
3008 tmp = dock->icon_array[i];
3009 if (!tmp)
3010 continue;
3012 ChangeStackingLevel(tmp->icon->core, newlevel);
3013 if (dock->lowered)
3014 wLowerFrame(tmp->icon->core);
3017 if (dock->type == WM_DOCK)
3018 wScreenUpdateUsableArea(dock->screen_ptr);
3021 static void toggleCollapsed(WDock *dock)
3023 if (dock->collapsed) {
3024 dock->collapsed = 0;
3025 wDockShowIcons(dock);
3026 } else {
3027 dock->collapsed = 1;
3028 wDockHideIcons(dock);
3032 static void openDockMenu(WDock *dock, WAppIcon *aicon, XEvent *event)
3034 WScreen *scr = dock->screen_ptr;
3035 WObjDescriptor *desc;
3036 WMenuEntry *entry;
3037 WApplication *wapp = NULL;
3038 int index = 0;
3039 int x_pos;
3040 int n_selected;
3041 int appIsRunning = aicon->running && aicon->icon && aicon->icon->owner;
3043 if (dock->type == WM_DOCK) {
3044 /* keep on top */
3045 entry = dock->menu->entries[index];
3046 entry->flags.indicator_on = !dock->lowered;
3047 entry->clientdata = dock;
3048 dock->menu->flags.realized = 0;
3049 } else {
3050 /* clip options */
3051 if (scr->clip_options)
3052 updateClipOptionsMenu(scr->clip_options, dock);
3054 n_selected = numberOfSelectedIcons(dock);
3056 /* Rename Workspace */
3057 entry = dock->menu->entries[++index];
3058 if (aicon == scr->clip_icon) {
3059 entry->callback = renameCallback;
3060 entry->clientdata = dock;
3061 entry->flags.indicator = 0;
3062 entry->text = _("Rename Workspace");
3063 } else {
3064 entry->callback = omnipresentCallback;
3065 entry->clientdata = aicon;
3066 if (n_selected > 0) {
3067 entry->flags.indicator = 0;
3068 entry->text = _("Toggle Omnipresent");
3069 } else {
3070 entry->flags.indicator = 1;
3071 entry->flags.indicator_on = aicon->omnipresent;
3072 entry->flags.indicator_type = MI_CHECK;
3073 entry->text = _("Omnipresent");
3077 /* select/unselect icon */
3078 entry = dock->menu->entries[++index];
3079 entry->clientdata = aicon;
3080 entry->flags.indicator_on = aicon->icon->selected;
3081 wMenuSetEnabled(dock->menu, index, aicon != scr->clip_icon);
3083 /* select/unselect all icons */
3084 entry = dock->menu->entries[++index];
3085 entry->clientdata = aicon;
3086 if (n_selected > 0)
3087 entry->text = _("Unselect All Icons");
3088 else
3089 entry->text = _("Select All Icons");
3091 wMenuSetEnabled(dock->menu, index, dock->icon_count > 1);
3093 /* keep icon(s) */
3094 entry = dock->menu->entries[++index];
3095 entry->clientdata = aicon;
3096 if (n_selected > 1)
3097 entry->text = _("Keep Icons");
3098 else
3099 entry->text = _("Keep Icon");
3101 wMenuSetEnabled(dock->menu, index, dock->icon_count > 1);
3103 /* this is the workspace submenu part */
3104 entry = dock->menu->entries[++index];
3105 if (n_selected > 1)
3106 entry->text = _("Move Icons To");
3107 else
3108 entry->text = _("Move Icon To");
3110 if (scr->clip_submenu)
3111 updateWorkspaceMenu(scr->clip_submenu, aicon);
3113 wMenuSetEnabled(dock->menu, index, !aicon->omnipresent);
3115 /* remove icon(s) */
3116 entry = dock->menu->entries[++index];
3117 entry->clientdata = aicon;
3118 if (n_selected > 1)
3119 entry->text = _("Remove Icons");
3120 else
3121 entry->text = _("Remove Icon");
3123 wMenuSetEnabled(dock->menu, index, dock->icon_count > 1);
3125 /* attract icon(s) */
3126 entry = dock->menu->entries[++index];
3127 entry->clientdata = aicon;
3129 dock->menu->flags.realized = 0;
3130 wMenuRealize(dock->menu);
3133 if (aicon->icon->owner)
3134 wapp = wApplicationOf(aicon->icon->owner->main_window);
3135 else
3136 wapp = NULL;
3138 /* launch */
3139 entry = dock->menu->entries[++index];
3140 entry->clientdata = aicon;
3141 wMenuSetEnabled(dock->menu, index, aicon->command != NULL);
3143 /* unhide here */
3144 entry = dock->menu->entries[++index];
3145 entry->clientdata = aicon;
3146 if (wapp && wapp->flags.hidden)
3147 entry->text = _("Unhide Here");
3148 else
3149 entry->text = _("Bring Here");
3151 wMenuSetEnabled(dock->menu, index, appIsRunning);
3153 /* hide */
3154 entry = dock->menu->entries[++index];
3155 entry->clientdata = aicon;
3156 if (wapp && wapp->flags.hidden)
3157 entry->text = _("Unhide");
3158 else
3159 entry->text = _("Hide");
3161 wMenuSetEnabled(dock->menu, index, appIsRunning);
3163 /* settings */
3164 entry = dock->menu->entries[++index];
3165 entry->clientdata = aicon;
3166 wMenuSetEnabled(dock->menu, index, !aicon->editing && !wPreferences.flags.noupdates);
3168 /* kill */
3169 entry = dock->menu->entries[++index];
3170 entry->clientdata = aicon;
3171 wMenuSetEnabled(dock->menu, index, appIsRunning);
3173 if (!dock->menu->flags.realized)
3174 wMenuRealize(dock->menu);
3176 if (dock->type == WM_CLIP) {
3177 /*x_pos = event->xbutton.x_root+2; */
3178 x_pos = event->xbutton.x_root - dock->menu->frame->core->width / 2 - 1;
3179 if (x_pos < 0) {
3180 x_pos = 0;
3181 } else if (x_pos + dock->menu->frame->core->width > scr->scr_width - 2) {
3182 x_pos = scr->scr_width - dock->menu->frame->core->width - 4;
3184 } else {
3185 x_pos = dock->on_right_side ? scr->scr_width - dock->menu->frame->core->width - 3 : 0;
3188 wMenuMapAt(dock->menu, x_pos, event->xbutton.y_root + 2, False);
3190 /* allow drag select */
3191 event->xany.send_event = True;
3192 desc = &dock->menu->menu->descriptor;
3193 (*desc->handle_mousedown) (desc, event);
3196 /******************************************************************/
3197 static void iconDblClick(WObjDescriptor *desc, XEvent *event)
3199 WAppIcon *btn = desc->parent;
3200 WDock *dock = btn->dock;
3201 WApplication *wapp = NULL;
3202 int unhideHere = 0;
3204 if (btn->icon->owner && !(event->xbutton.state & ControlMask)) {
3205 wapp = wApplicationOf(btn->icon->owner->main_window);
3207 assert(wapp != NULL);
3209 unhideHere = (event->xbutton.state & ShiftMask);
3211 /* go to the last workspace that the user worked on the app */
3212 if (wapp->last_workspace != dock->screen_ptr->current_workspace && !unhideHere)
3213 wWorkspaceChange(dock->screen_ptr, wapp->last_workspace);
3215 wUnhideApplication(wapp, event->xbutton.button == Button2, unhideHere);
3217 if (event->xbutton.state & MOD_MASK)
3218 wHideOtherApplications(btn->icon->owner);
3219 } else {
3220 if (event->xbutton.button == Button1) {
3221 if (event->xbutton.state & MOD_MASK) {
3222 /* raise/lower dock */
3223 toggleLowered(dock);
3224 } else if (btn == dock->screen_ptr->clip_icon) {
3225 if (getClipButton(event->xbutton.x, event->xbutton.y) == CLIP_IDLE)
3226 toggleCollapsed(dock);
3227 else
3228 handleClipChangeWorkspace(dock->screen_ptr, event);
3229 } else if (btn->command) {
3230 if (!btn->launching && (!btn->running || (event->xbutton.state & ControlMask)))
3231 launchDockedApplication(btn, False);
3237 static void handleDockMove(WDock *dock, WAppIcon *aicon, XEvent *event)
3239 WScreen *scr = dock->screen_ptr;
3240 int ofs_x = event->xbutton.x, ofs_y = event->xbutton.y;
3241 int x, y;
3242 XEvent ev;
3243 int grabbed = 0, swapped = 0, done;
3244 Pixmap ghost = None;
3245 int superfluous = wPreferences.superfluous; /* we catch it to avoid problems */
3247 if (XGrabPointer(dpy, aicon->icon->core->window, True, ButtonMotionMask
3248 | ButtonReleaseMask | ButtonPressMask, GrabModeAsync,
3249 GrabModeAsync, None, None, CurrentTime) != GrabSuccess)
3250 wwarning("pointer grab failed for dock move");
3252 y = 0;
3253 for (x = 0; x < dock->max_icons; x++) {
3254 if (dock->icon_array[x] != NULL && dock->icon_array[x]->yindex > y)
3255 y = dock->icon_array[x]->yindex;
3257 y++;
3258 XResizeWindow(dpy, scr->dock_shadow, ICON_SIZE, ICON_SIZE * y);
3260 done = 0;
3261 while (!done) {
3262 WMMaskEvent(dpy, PointerMotionMask | ButtonReleaseMask | ButtonPressMask
3263 | ButtonMotionMask | ExposureMask | EnterWindowMask, &ev);
3264 switch (ev.type) {
3265 case Expose:
3266 WMHandleEvent(&ev);
3267 break;
3269 case EnterNotify:
3270 /* It means the cursor moved so fast that it entered
3271 * something else (if moving slowly, it would have
3272 * stayed in the dock that is being moved. Ignore such
3273 * "spurious" EnterNotifiy's */
3274 break;
3276 case MotionNotify:
3277 if (!grabbed) {
3278 if (abs(ofs_x - ev.xmotion.x) >= MOVE_THRESHOLD
3279 || abs(ofs_y - ev.xmotion.y) >= MOVE_THRESHOLD) {
3280 XChangeActivePointerGrab(dpy, ButtonMotionMask
3281 | ButtonReleaseMask | ButtonPressMask,
3282 wCursor[WCUR_MOVE], CurrentTime);
3283 grabbed = 1;
3285 break;
3287 if (dock->type == WM_CLIP) {
3288 x = ev.xmotion.x_root - ofs_x;
3289 y = ev.xmotion.y_root - ofs_y;
3290 wScreenKeepInside(scr, &x, &y, ICON_SIZE, ICON_SIZE);
3292 moveDock(dock, x, y);
3293 } else {
3294 /* move vertically if pointer is inside the dock */
3295 if ((dock->on_right_side && ev.xmotion.x_root >= dock->x_pos - ICON_SIZE)
3296 || (!dock->on_right_side && ev.xmotion.x_root <= dock->x_pos + ICON_SIZE * 2)) {
3298 x = ev.xmotion.x_root - ofs_x;
3299 y = ev.xmotion.y_root - ofs_y;
3300 wScreenKeepInside(scr, &x, &y, ICON_SIZE, ICON_SIZE);
3301 moveDock(dock, dock->x_pos, y);
3303 /* move horizontally to change sides */
3304 x = ev.xmotion.x_root - ofs_x;
3305 if (!dock->on_right_side) {
3307 /* is on left */
3308 if (ev.xmotion.x_root > dock->x_pos + ICON_SIZE * 2) {
3309 XMoveWindow(dpy, scr->dock_shadow, scr->scr_width - ICON_SIZE
3310 - DOCK_EXTRA_SPACE - 1, dock->y_pos);
3311 if (superfluous && ghost == None) {
3312 ghost = MakeGhostDock(dock, dock->x_pos,
3313 scr->scr_width - ICON_SIZE
3314 - DOCK_EXTRA_SPACE - 1, dock->y_pos);
3315 XSetWindowBackgroundPixmap(dpy, scr->dock_shadow, ghost);
3316 XClearWindow(dpy, scr->dock_shadow);
3318 XMapRaised(dpy, scr->dock_shadow);
3319 swapped = 1;
3320 } else {
3321 if (superfluous && ghost != None) {
3322 XFreePixmap(dpy, ghost);
3323 ghost = None;
3325 XUnmapWindow(dpy, scr->dock_shadow);
3326 swapped = 0;
3328 } else {
3329 /* is on right */
3330 if (ev.xmotion.x_root < dock->x_pos - ICON_SIZE) {
3331 XMoveWindow(dpy, scr->dock_shadow, DOCK_EXTRA_SPACE, dock->y_pos);
3332 if (superfluous && ghost == None) {
3333 ghost = MakeGhostDock(dock, dock->x_pos,
3334 DOCK_EXTRA_SPACE, dock->y_pos);
3335 XSetWindowBackgroundPixmap(dpy, scr->dock_shadow, ghost);
3336 XClearWindow(dpy, scr->dock_shadow);
3338 XMapRaised(dpy, scr->dock_shadow);
3339 swapped = -1;
3340 } else {
3341 XUnmapWindow(dpy, scr->dock_shadow);
3342 swapped = 0;
3343 if (superfluous && ghost != None) {
3344 XFreePixmap(dpy, ghost);
3345 ghost = None;
3350 break;
3352 case ButtonPress:
3353 break;
3355 case ButtonRelease:
3356 if (ev.xbutton.button != event->xbutton.button)
3357 break;
3358 XUngrabPointer(dpy, CurrentTime);
3359 XUnmapWindow(dpy, scr->dock_shadow);
3360 XResizeWindow(dpy, scr->dock_shadow, ICON_SIZE, ICON_SIZE);
3361 if (dock->type == WM_DOCK) {
3362 if (swapped != 0) {
3363 if (swapped > 0)
3364 dock->on_right_side = 1;
3365 else
3366 dock->on_right_side = 0;
3367 swapDock(dock);
3368 wArrangeIcons(scr, False);
3371 done = 1;
3372 break;
3375 if (superfluous) {
3376 if (ghost != None)
3377 XFreePixmap(dpy, ghost);
3378 XSetWindowBackground(dpy, scr->dock_shadow, scr->white_pixel);
3382 static Bool handleIconMove(WDock *dock, WAppIcon *aicon, XEvent *event)
3384 WScreen *scr = dock->screen_ptr;
3385 Window wins[2];
3386 WIcon *icon = aicon->icon;
3387 WDock *dock2 = NULL, *last_dock = dock, *clip = NULL;
3388 int ondock, grabbed = 0, change_dock = 0, collapsed = 0;
3389 XEvent ev;
3390 int x = aicon->x_pos, y = aicon->y_pos;
3391 int ofs_x = event->xbutton.x, ofs_y = event->xbutton.y;
3392 int shad_x = x, shad_y = y;
3393 int ix = aicon->xindex, iy = aicon->yindex;
3394 int tmp;
3395 Pixmap ghost = None;
3396 Bool docked;
3397 int superfluous = wPreferences.superfluous; /* we catch it to avoid problems */
3398 int omnipresent = aicon->omnipresent; /* this must be cached!!! */
3399 Bool hasMoved = False;
3401 if (wPreferences.flags.noupdates)
3402 return hasMoved;
3404 if (XGrabPointer(dpy, icon->core->window, True, ButtonMotionMask
3405 | ButtonReleaseMask | ButtonPressMask, GrabModeAsync,
3406 GrabModeAsync, None, None, CurrentTime) != GrabSuccess) {
3409 if (!(event->xbutton.state & MOD_MASK))
3410 wRaiseFrame(icon->core);
3412 if (!wPreferences.flags.noclip)
3413 clip = scr->workspaces[scr->current_workspace]->clip;
3415 if (dock == scr->dock && !wPreferences.flags.noclip)
3416 dock2 = clip;
3417 else if (dock != scr->dock && !wPreferences.flags.nodock)
3418 dock2 = scr->dock;
3420 wins[0] = icon->core->window;
3421 wins[1] = scr->dock_shadow;
3422 XRestackWindows(dpy, wins, 2);
3423 XMoveResizeWindow(dpy, scr->dock_shadow, aicon->x_pos, aicon->y_pos, ICON_SIZE, ICON_SIZE);
3424 if (superfluous) {
3425 if (icon->pixmap != None)
3426 ghost = MakeGhostIcon(scr, icon->pixmap);
3427 else
3428 ghost = MakeGhostIcon(scr, icon->core->window);
3430 XSetWindowBackgroundPixmap(dpy, scr->dock_shadow, ghost);
3431 XClearWindow(dpy, scr->dock_shadow);
3433 XMapWindow(dpy, scr->dock_shadow);
3435 ondock = 1;
3437 while (1) {
3438 XMaskEvent(dpy, PointerMotionMask | ButtonReleaseMask | ButtonPressMask
3439 | ButtonMotionMask | ExposureMask, &ev);
3440 switch (ev.type) {
3441 case Expose:
3442 WMHandleEvent(&ev);
3443 break;
3445 case MotionNotify:
3446 hasMoved = True;
3447 if (!grabbed) {
3448 if (abs(ofs_x - ev.xmotion.x) >= MOVE_THRESHOLD
3449 || abs(ofs_y - ev.xmotion.y) >= MOVE_THRESHOLD) {
3450 XChangeActivePointerGrab(dpy, ButtonMotionMask
3451 | ButtonReleaseMask | ButtonPressMask,
3452 wCursor[WCUR_MOVE], CurrentTime);
3453 grabbed = 1;
3454 } else {
3455 break;
3459 if (omnipresent) {
3460 int i;
3461 for (i = 0; i < scr->workspace_count; i++) {
3462 if (i == scr->current_workspace)
3463 continue;
3464 wDockShowIcons(scr->workspaces[i]->clip);
3468 x = ev.xmotion.x_root - ofs_x;
3469 y = ev.xmotion.y_root - ofs_y;
3470 tmp = wDockSnapIcon(dock, aicon, x, y, &ix, &iy, True);
3471 if (tmp && dock2) {
3472 change_dock = 0;
3473 if (last_dock != dock && collapsed) {
3474 last_dock->collapsed = 1;
3475 wDockHideIcons(last_dock);
3476 collapsed = 0;
3478 if (!collapsed && (collapsed = dock->collapsed)) {
3479 dock->collapsed = 0;
3480 wDockShowIcons(dock);
3482 if (dock->auto_raise_lower)
3483 wDockRaise(dock);
3484 last_dock = dock;
3485 } else if (dock2) {
3486 tmp = wDockSnapIcon(dock2, aicon, x, y, &ix, &iy, False);
3487 if (tmp) {
3488 change_dock = 1;
3489 if (last_dock != dock2 && collapsed) {
3490 last_dock->collapsed = 1;
3491 wDockHideIcons(last_dock);
3492 collapsed = 0;
3494 if (!collapsed && (collapsed = dock2->collapsed)) {
3495 dock2->collapsed = 0;
3496 wDockShowIcons(dock2);
3498 if (dock2->auto_raise_lower)
3499 wDockRaise(dock2);
3500 last_dock = dock2;
3503 if (aicon->launching || aicon->lock || (aicon->running && !(ev.xmotion.state & MOD_MASK))
3504 || (!aicon->running && tmp)) {
3505 shad_x = last_dock->x_pos + ix * wPreferences.icon_size;
3506 shad_y = last_dock->y_pos + iy * wPreferences.icon_size;
3508 XMoveWindow(dpy, scr->dock_shadow, shad_x, shad_y);
3510 if (!ondock)
3511 XMapWindow(dpy, scr->dock_shadow);
3513 ondock = 1;
3514 } else {
3515 if (ondock)
3516 XUnmapWindow(dpy, scr->dock_shadow);
3518 ondock = 0;
3520 XMoveWindow(dpy, icon->core->window, x, y);
3521 break;
3523 case ButtonPress:
3524 break;
3526 case ButtonRelease:
3527 if (ev.xbutton.button != event->xbutton.button)
3528 break;
3529 XUngrabPointer(dpy, CurrentTime);
3530 if (ondock) {
3531 SlideWindow(icon->core->window, x, y, shad_x, shad_y);
3532 XUnmapWindow(dpy, scr->dock_shadow);
3533 if (!change_dock) {
3534 reattachIcon(dock, aicon, ix, iy);
3535 if (clip && dock != clip && clip->auto_raise_lower)
3536 wDockLower(clip);
3537 } else {
3538 docked = moveIconBetweenDocks(dock, dock2, aicon, ix, iy);
3539 if (!docked) {
3540 /* Slide it back if dock rejected it */
3541 SlideWindow(icon->core->window, x, y, aicon->x_pos, aicon->y_pos);
3542 reattachIcon(dock, aicon, aicon->xindex, aicon->yindex);
3544 if (last_dock->type == WM_CLIP && last_dock->auto_collapse)
3545 collapsed = 0;
3547 } else {
3548 aicon->x_pos = x;
3549 aicon->y_pos = y;
3550 if (superfluous) {
3551 if (!aicon->running && !wPreferences.no_animations) {
3552 /* We need to deselect it, even if is deselected in
3553 * wDockDetach(), because else DoKaboom() will fail.
3555 if (aicon->icon->selected)
3556 wIconSelect(aicon->icon);
3558 DoKaboom(scr, aicon->icon->core->window, x, y);
3561 if (clip && clip->auto_raise_lower)
3562 wDockLower(clip);
3563 wDockDetach(dock, aicon);
3565 if (collapsed) {
3566 last_dock->collapsed = 1;
3567 wDockHideIcons(last_dock);
3568 collapsed = 0;
3570 if (superfluous) {
3571 if (ghost != None)
3572 XFreePixmap(dpy, ghost);
3573 XSetWindowBackground(dpy, scr->dock_shadow, scr->white_pixel);
3575 if (omnipresent) {
3576 int i;
3577 for (i = 0; i < scr->workspace_count; i++) {
3578 if (i == scr->current_workspace)
3579 continue;
3580 wDockHideIcons(scr->workspaces[i]->clip);
3583 return hasMoved;;
3586 return False; /* never reached */
3589 static int getClipButton(int px, int py)
3591 int pt = (CLIP_BUTTON_SIZE + 2) * ICON_SIZE / 64;
3593 if (px < 0 || py < 0 || px >= ICON_SIZE || py >= ICON_SIZE)
3594 return CLIP_IDLE;
3596 if (py <= pt - ((int)ICON_SIZE - 1 - px))
3597 return CLIP_FORWARD;
3598 else if (px <= pt - ((int)ICON_SIZE - 1 - py))
3599 return CLIP_REWIND;
3601 return CLIP_IDLE;
3604 static void handleClipChangeWorkspace(WScreen *scr, XEvent *event)
3606 XEvent ev;
3607 int done, direction, new_ws;
3608 int new_dir;
3609 WDock *clip = scr->clip_icon->dock;
3611 direction = getClipButton(event->xbutton.x, event->xbutton.y);
3613 clip->lclip_button_pushed = direction == CLIP_REWIND;
3614 clip->rclip_button_pushed = direction == CLIP_FORWARD;
3616 wClipIconPaint(scr->clip_icon);
3617 done = 0;
3618 while (!done) {
3619 WMMaskEvent(dpy, ExposureMask | ButtonMotionMask | ButtonReleaseMask | ButtonPressMask, &ev);
3620 switch (ev.type) {
3621 case Expose:
3622 WMHandleEvent(&ev);
3623 break;
3625 case MotionNotify:
3626 new_dir = getClipButton(ev.xmotion.x, ev.xmotion.y);
3627 if (new_dir != direction) {
3628 direction = new_dir;
3629 clip->lclip_button_pushed = direction == CLIP_REWIND;
3630 clip->rclip_button_pushed = direction == CLIP_FORWARD;
3631 wClipIconPaint(scr->clip_icon);
3633 break;
3635 case ButtonPress:
3636 break;
3638 case ButtonRelease:
3639 if (ev.xbutton.button == event->xbutton.button)
3640 done = 1;
3644 clip->lclip_button_pushed = 0;
3645 clip->rclip_button_pushed = 0;
3647 new_ws = wPreferences.ws_advance || (event->xbutton.state & ControlMask);
3649 if (direction == CLIP_FORWARD) {
3650 if (scr->current_workspace < scr->workspace_count - 1)
3651 wWorkspaceChange(scr, scr->current_workspace + 1);
3652 else if (new_ws && scr->current_workspace < MAX_WORKSPACES - 1)
3653 wWorkspaceChange(scr, scr->current_workspace + 1);
3654 else if (wPreferences.ws_cycle)
3655 wWorkspaceChange(scr, 0);
3656 } else if (direction == CLIP_REWIND) {
3657 if (scr->current_workspace > 0)
3658 wWorkspaceChange(scr, scr->current_workspace - 1);
3659 else if (scr->current_workspace == 0 && wPreferences.ws_cycle)
3660 wWorkspaceChange(scr, scr->workspace_count - 1);
3663 wClipIconPaint(scr->clip_icon);
3666 static void iconMouseDown(WObjDescriptor *desc, XEvent *event)
3668 WAppIcon *aicon = desc->parent;
3669 WDock *dock = aicon->dock;
3670 WScreen *scr = aicon->icon->core->screen_ptr;
3672 if (aicon->editing || WCHECK_STATE(WSTATE_MODAL))
3673 return;
3675 scr->last_dock = dock;
3677 if (dock->menu->flags.mapped)
3678 wMenuUnmap(dock->menu);
3680 if (IsDoubleClick(scr, event)) {
3681 /* double-click was not in the main clip icon */
3682 if (dock->type != WM_CLIP || aicon->xindex != 0 || aicon->yindex != 0
3683 || getClipButton(event->xbutton.x, event->xbutton.y) == CLIP_IDLE) {
3684 iconDblClick(desc, event);
3685 return;
3689 if (event->xbutton.button == Button1) {
3690 if (event->xbutton.state & MOD_MASK)
3691 wDockLower(dock);
3692 else
3693 wDockRaise(dock);
3695 if ((event->xbutton.state & ShiftMask) && aicon != scr->clip_icon && dock->type != WM_DOCK) {
3696 wIconSelect(aicon->icon);
3697 return;
3700 if (aicon->yindex == 0 && aicon->xindex == 0) {
3701 if (getClipButton(event->xbutton.x, event->xbutton.y) != CLIP_IDLE
3702 && dock->type == WM_CLIP)
3703 handleClipChangeWorkspace(scr, event);
3704 else
3705 handleDockMove(dock, aicon, event);
3706 } else {
3707 Bool hasMoved = handleIconMove(dock, aicon, event);
3708 if (wPreferences.single_click && !hasMoved)
3709 iconDblClick(desc, event);
3711 } else if (event->xbutton.button == Button2 && dock->type == WM_CLIP && aicon == scr->clip_icon) {
3712 if (!scr->clip_ws_menu) {
3713 scr->clip_ws_menu = wWorkspaceMenuMake(scr, False);
3715 if (scr->clip_ws_menu) {
3716 WMenu *wsMenu = scr->clip_ws_menu;
3717 int xpos;
3719 wWorkspaceMenuUpdate(scr, wsMenu);
3721 xpos = event->xbutton.x_root - wsMenu->frame->core->width / 2 - 1;
3722 if (xpos < 0) {
3723 xpos = 0;
3724 } else if (xpos + wsMenu->frame->core->width > scr->scr_width - 2) {
3725 xpos = scr->scr_width - wsMenu->frame->core->width - 4;
3727 wMenuMapAt(wsMenu, xpos, event->xbutton.y_root + 2, False);
3729 desc = &wsMenu->menu->descriptor;
3730 event->xany.send_event = True;
3731 (*desc->handle_mousedown) (desc, event);
3733 } else if (event->xbutton.button == Button2 && dock->type == WM_CLIP &&
3734 (event->xbutton.state & ShiftMask) && aicon != scr->clip_icon) {
3735 wClipMakeIconOmnipresent(aicon, !aicon->omnipresent);
3736 } else if (event->xbutton.button == Button3) {
3737 if (event->xbutton.send_event &&
3738 XGrabPointer(dpy, aicon->icon->core->window, True, ButtonMotionMask
3739 | ButtonReleaseMask | ButtonPressMask, GrabModeAsync,
3740 GrabModeAsync, None, None, CurrentTime) != GrabSuccess) {
3741 wwarning("pointer grab failed for dockicon menu");
3742 return;
3745 openDockMenu(dock, aicon, event);
3746 } else if (event->xbutton.button == Button2) {
3747 WAppIcon *btn = desc->parent;
3749 if (!btn->launching && (!btn->running || (event->xbutton.state & ControlMask)))
3750 launchDockedApplication(btn, True);
3754 static void clipEnterNotify(WObjDescriptor *desc, XEvent *event)
3756 WAppIcon *btn = (WAppIcon *) desc->parent;
3757 WDock *dock;
3758 WScreen *scr;
3760 assert(event->type == EnterNotify);
3762 if (desc->parent_type != WCLASS_DOCK_ICON)
3763 return;
3765 scr = btn->icon->core->screen_ptr;
3766 if (!btn->omnipresent)
3767 dock = btn->dock;
3768 else
3769 dock = scr->workspaces[scr->current_workspace]->clip;
3771 if (!dock || dock->type != WM_CLIP)
3772 return;
3774 /* The auto raise/lower code */
3775 if (dock->auto_lower_magic) {
3776 WMDeleteTimerHandler(dock->auto_lower_magic);
3777 dock->auto_lower_magic = NULL;
3779 if (dock->auto_raise_lower && !dock->auto_raise_magic)
3780 dock->auto_raise_magic = WMAddTimerHandler(AUTO_RAISE_DELAY, clipAutoRaise, (void *)dock);
3782 /* The auto expand/collapse code */
3783 if (dock->auto_collapse_magic) {
3784 WMDeleteTimerHandler(dock->auto_collapse_magic);
3785 dock->auto_collapse_magic = NULL;
3787 if (dock->auto_collapse && !dock->auto_expand_magic)
3788 dock->auto_expand_magic = WMAddTimerHandler(AUTO_EXPAND_DELAY, clipAutoExpand, (void *)dock);
3791 static void clipLeave(WDock *dock)
3793 XEvent event;
3794 WObjDescriptor *desc = NULL;
3796 if (!dock || dock->type != WM_CLIP)
3797 return;
3799 if (XCheckTypedEvent(dpy, EnterNotify, &event) != False) {
3800 if (XFindContext(dpy, event.xcrossing.window, wWinContext,
3801 (XPointer *) & desc) != XCNOENT
3802 && desc && desc->parent_type == WCLASS_DOCK_ICON
3803 && ((WAppIcon *) desc->parent)->dock && ((WAppIcon *) desc->parent)->dock->type == WM_CLIP) {
3804 /* We didn't left the Clip yet */
3805 XPutBackEvent(dpy, &event);
3806 return;
3809 XPutBackEvent(dpy, &event);
3810 } else {
3811 /* We entered a withdrawn window, so we're still in Clip */
3812 return;
3815 if (dock->auto_raise_magic) {
3816 WMDeleteTimerHandler(dock->auto_raise_magic);
3817 dock->auto_raise_magic = NULL;
3819 if (dock->auto_raise_lower && !dock->auto_lower_magic)
3820 dock->auto_lower_magic = WMAddTimerHandler(AUTO_LOWER_DELAY, clipAutoLower, (void *)dock);
3822 if (dock->auto_expand_magic) {
3823 WMDeleteTimerHandler(dock->auto_expand_magic);
3824 dock->auto_expand_magic = NULL;
3826 if (dock->auto_collapse && !dock->auto_collapse_magic)
3827 dock->auto_collapse_magic = WMAddTimerHandler(AUTO_COLLAPSE_DELAY, clipAutoCollapse, (void *)dock);
3830 static void clipLeaveNotify(WObjDescriptor *desc, XEvent *event)
3832 WAppIcon *btn = (WAppIcon *) desc->parent;
3834 assert(event->type == LeaveNotify);
3836 if (desc->parent_type != WCLASS_DOCK_ICON)
3837 return;
3839 clipLeave(btn->dock);
3842 static void clipAutoCollapse(void *cdata)
3844 WDock *dock = (WDock *) cdata;
3846 if (dock->type != WM_CLIP)
3847 return;
3849 if (dock->auto_collapse) {
3850 dock->collapsed = 1;
3851 wDockHideIcons(dock);
3853 dock->auto_collapse_magic = NULL;
3856 static void clipAutoExpand(void *cdata)
3858 WDock *dock = (WDock *) cdata;
3860 if (dock->type != WM_CLIP)
3861 return;
3863 if (dock->auto_collapse) {
3864 dock->collapsed = 0;
3865 wDockShowIcons(dock);
3867 dock->auto_expand_magic = NULL;
3870 static void clipAutoLower(void *cdata)
3872 WDock *dock = (WDock *) cdata;
3874 if (dock->type != WM_CLIP)
3875 return;
3877 if (dock->auto_raise_lower)
3878 wDockLower(dock);
3880 dock->auto_lower_magic = NULL;
3883 static void clipAutoRaise(void *cdata)
3885 WDock *dock = (WDock *) cdata;
3887 if (dock->type != WM_CLIP)
3888 return;
3890 if (dock->auto_raise_lower)
3891 wDockRaise(dock);
3893 dock->auto_raise_magic = NULL;
3896 static Bool iconCanBeOmnipresent(WAppIcon *aicon)
3898 WScreen *scr = aicon->icon->core->screen_ptr;
3899 WDock *clip;
3900 WAppIcon *btn;
3901 int i, j;
3903 for (i = 0; i < scr->workspace_count; i++) {
3904 clip = scr->workspaces[i]->clip;
3906 if (clip == aicon->dock)
3907 continue;
3909 if (clip->icon_count + scr->global_icon_count >= clip->max_icons)
3910 return False; /* Clip is full in some workspace */
3912 for (j = 0; j < clip->max_icons; j++) {
3913 btn = clip->icon_array[j];
3914 if (btn && btn->xindex == aicon->xindex && btn->yindex == aicon->yindex)
3915 return False;
3919 return True;
3922 int wClipMakeIconOmnipresent(WAppIcon *aicon, int omnipresent)
3924 WScreen *scr = aicon->icon->core->screen_ptr;
3925 WAppIconChain *new_entry, *tmp, *tmp1;
3926 int status = WO_SUCCESS;
3928 if ((scr->dock && aicon->dock == scr->dock) || aicon == scr->clip_icon)
3929 return WO_NOT_APPLICABLE;
3931 if (aicon->omnipresent == omnipresent)
3932 return WO_SUCCESS;
3934 if (omnipresent) {
3935 if (iconCanBeOmnipresent(aicon)) {
3936 aicon->omnipresent = 1;
3937 new_entry = wmalloc(sizeof(WAppIconChain));
3938 new_entry->aicon = aicon;
3939 new_entry->next = scr->global_icons;
3940 scr->global_icons = new_entry;
3941 scr->global_icon_count++;
3942 } else {
3943 aicon->omnipresent = 0;
3944 status = WO_FAILED;
3946 } else {
3947 aicon->omnipresent = 0;
3948 if (aicon == scr->global_icons->aicon) {
3949 tmp = scr->global_icons->next;
3950 wfree(scr->global_icons);
3951 scr->global_icons = tmp;
3952 scr->global_icon_count--;
3953 } else {
3954 tmp = scr->global_icons;
3955 while (tmp->next) {
3956 if (tmp->next->aicon == aicon) {
3957 tmp1 = tmp->next->next;
3958 wfree(tmp->next);
3959 tmp->next = tmp1;
3960 scr->global_icon_count--;
3961 break;
3963 tmp = tmp->next;
3968 wAppIconPaint(aicon);
3970 return status;