wDockAttachIcon paint argument
[wmaker-crm.git] / src / dock.c
blobe6a575bf727b04053de421e82a52166f717be325
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;
580 Bool update_icon = False;
582 assert(entry->clientdata != NULL);
583 clip = clickedIcon->dock;
585 aicon = clip->screen_ptr->app_icon_list;
587 while (aicon) {
588 if (!aicon->docked && wDockFindFreeSlot(clip, &x, &y)) {
589 x_pos = clip->x_pos + x * ICON_SIZE;
590 y_pos = clip->y_pos + y * ICON_SIZE;
591 if (aicon->x_pos != x_pos || aicon->y_pos != y_pos)
592 move_window(aicon->icon->core->window, aicon->x_pos, aicon->y_pos, x_pos, y_pos);
594 aicon->attracted = 1;
595 if (!aicon->icon->shadowed) {
596 aicon->icon->shadowed = 1;
597 update_icon = True;
599 wDockAttachIcon(clip, aicon, x, y, update_icon);
600 if (clip->collapsed || !clip->mapped)
601 XUnmapWindow(dpy, aicon->icon->core->window);
603 aicon = aicon->next;
607 static void selectIconsCallback(WMenu *menu, WMenuEntry *entry)
609 WAppIcon *clickedIcon = (WAppIcon *) entry->clientdata;
610 WDock *dock;
611 WMArray *selectedIcons;
612 WMArrayIterator iter;
613 WAppIcon *btn;
614 int i;
616 assert(clickedIcon != NULL);
617 dock = clickedIcon->dock;
619 selectedIcons = getSelected(dock);
621 if (!WMGetArrayItemCount(selectedIcons)) {
622 for (i = 1; i < dock->max_icons; i++) {
623 btn = dock->icon_array[i];
624 if (btn && !btn->icon->selected)
625 wIconSelect(btn->icon);
627 } else {
628 WM_ITERATE_ARRAY(selectedIcons, btn, iter) {
629 wIconSelect(btn->icon);
632 WMFreeArray(selectedIcons);
634 wMenuPaint(menu);
637 static void toggleCollapsedCallback(WMenu *menu, WMenuEntry *entry)
639 assert(entry->clientdata != NULL);
641 toggleCollapsed(entry->clientdata);
643 entry->flags.indicator_on = ((WDock *) entry->clientdata)->collapsed;
645 wMenuPaint(menu);
648 static void toggleAutoCollapseCallback(WMenu *menu, WMenuEntry *entry)
650 WDock *dock;
651 assert(entry->clientdata != NULL);
653 dock = (WDock *) entry->clientdata;
655 dock->auto_collapse = !dock->auto_collapse;
657 entry->flags.indicator_on = ((WDock *) entry->clientdata)->auto_collapse;
659 wMenuPaint(menu);
662 static void toggleAutoRaiseLowerCallback(WMenu *menu, WMenuEntry *entry)
664 WDock *dock;
665 assert(entry->clientdata != NULL);
667 dock = (WDock *) entry->clientdata;
669 dock->auto_raise_lower = !dock->auto_raise_lower;
671 entry->flags.indicator_on = ((WDock *) entry->clientdata)->auto_raise_lower;
673 wMenuPaint(menu);
676 static void launchCallback(WMenu *menu, WMenuEntry *entry)
678 WAppIcon *btn = (WAppIcon *) entry->clientdata;
680 launchDockedApplication(btn, False);
683 static void settingsCallback(WMenu *menu, WMenuEntry *entry)
685 WAppIcon *btn = (WAppIcon *) entry->clientdata;
687 if (btn->editing)
688 return;
689 ShowDockAppSettingsPanel(btn);
692 static void hideCallback(WMenu *menu, WMenuEntry *entry)
694 WApplication *wapp;
695 WAppIcon *btn = (WAppIcon *) entry->clientdata;
697 wapp = wApplicationOf(btn->icon->owner->main_window);
699 if (wapp->flags.hidden) {
700 wWorkspaceChange(btn->icon->core->screen_ptr, wapp->last_workspace);
701 wUnhideApplication(wapp, False, False);
702 } else {
703 wHideApplication(wapp);
707 static void unhideHereCallback(WMenu *menu, WMenuEntry *entry)
709 WApplication *wapp;
710 WAppIcon *btn = (WAppIcon *) entry->clientdata;
712 wapp = wApplicationOf(btn->icon->owner->main_window);
714 wUnhideApplication(wapp, False, True);
717 static WAppIcon *mainIconCreate(WScreen *scr, int type)
719 WAppIcon *btn;
720 int x_pos;
722 if (type == WM_CLIP) {
723 if (scr->clip_icon)
724 return scr->clip_icon;
725 btn = wAppIconCreateForDock(scr, NULL, "Logo", "WMClip", TILE_CLIP);
726 btn->icon->core->descriptor.handle_expose = clipIconExpose;
727 btn->icon->core->descriptor.handle_enternotify = clipEnterNotify;
728 btn->icon->core->descriptor.handle_leavenotify = clipLeaveNotify;
729 x_pos = 0;
730 } else {
731 btn = wAppIconCreateForDock(scr, NULL, "Logo", "WMDock", TILE_NORMAL);
732 x_pos = scr->scr_width - ICON_SIZE - DOCK_EXTRA_SPACE;
735 btn->xindex = 0;
736 btn->yindex = 0;
738 btn->icon->core->descriptor.handle_mousedown = iconMouseDown;
739 btn->icon->core->descriptor.parent_type = WCLASS_DOCK_ICON;
740 btn->icon->core->descriptor.parent = btn;
741 XMapWindow(dpy, btn->icon->core->window);
742 btn->x_pos = x_pos;
743 btn->y_pos = 0;
744 btn->docked = 1;
745 if (type == WM_CLIP)
746 scr->clip_icon = btn;
748 return btn;
751 static void switchWSCommand(WMenu *menu, WMenuEntry *entry)
753 WAppIcon *btn, *icon = (WAppIcon *) entry->clientdata;
754 WScreen *scr = icon->icon->core->screen_ptr;
755 WDock *src, *dest;
756 WMArray *selectedIcons;
757 int x, y;
759 if (entry->order == scr->current_workspace)
760 return;
761 src = icon->dock;
762 dest = scr->workspaces[entry->order]->clip;
764 selectedIcons = getSelected(src);
766 if (WMGetArrayItemCount(selectedIcons)) {
767 WMArrayIterator iter;
769 WM_ITERATE_ARRAY(selectedIcons, btn, iter) {
770 if (wDockFindFreeSlot(dest, &x, &y)) {
771 moveIconBetweenDocks(src, dest, btn, x, y);
772 XUnmapWindow(dpy, btn->icon->core->window);
775 } else if (icon != scr->clip_icon) {
776 if (wDockFindFreeSlot(dest, &x, &y)) {
777 moveIconBetweenDocks(src, dest, icon, x, y);
778 XUnmapWindow(dpy, icon->icon->core->window);
781 WMFreeArray(selectedIcons);
784 static void launchDockedApplication(WAppIcon *btn, Bool withSelection)
786 WScreen *scr = btn->icon->core->screen_ptr;
788 if (!btn->launching &&
789 ((!withSelection && btn->command != NULL) || (withSelection && btn->paste_command != NULL))) {
790 if (!btn->forced_dock) {
791 btn->relaunching = btn->running;
792 btn->running = 1;
794 if (btn->wm_instance || btn->wm_class) {
795 WWindowAttributes attr;
796 memset(&attr, 0, sizeof(WWindowAttributes));
797 wDefaultFillAttributes(btn->wm_instance, btn->wm_class, &attr, NULL, True);
799 if (!attr.no_appicon && !btn->buggy_app)
800 btn->launching = 1;
801 else
802 btn->running = 0;
804 btn->drop_launch = 0;
805 btn->paste_launch = withSelection;
806 scr->last_dock = btn->dock;
807 btn->pid = execCommand(btn, (withSelection ? btn->paste_command : btn->command), NULL);
808 if (btn->pid > 0) {
809 if (btn->buggy_app) {
810 /* give feedback that the app was launched */
811 btn->launching = 1;
812 dockIconPaint(btn);
813 btn->launching = 0;
814 WMAddTimerHandler(200, (WMCallback *) dockIconPaint, btn);
815 } else {
816 dockIconPaint(btn);
818 } else {
819 wwarning(_("could not launch application %s"), btn->command);
820 btn->launching = 0;
821 if (!btn->relaunching)
822 btn->running = 0;
827 static void updateWorkspaceMenu(WMenu *menu, WAppIcon *icon)
829 WScreen *scr = menu->frame->screen_ptr;
830 char title[MAX_WORKSPACENAME_WIDTH + 1];
831 int i;
833 if (!menu || !icon)
834 return;
836 for (i = 0; i < scr->workspace_count; i++) {
837 if (i < menu->entry_no) {
838 if (strcmp(menu->entries[i]->text, scr->workspaces[i]->name) != 0) {
839 wfree(menu->entries[i]->text);
840 strcpy(title, scr->workspaces[i]->name);
841 menu->entries[i]->text = wstrdup(title);
842 menu->flags.realized = 0;
844 menu->entries[i]->clientdata = (void *)icon;
845 } else {
846 strcpy(title, scr->workspaces[i]->name);
848 wMenuAddCallback(menu, title, switchWSCommand, (void *)icon);
850 menu->flags.realized = 0;
853 if (i == scr->current_workspace)
854 wMenuSetEnabled(menu, i, False);
855 else
856 wMenuSetEnabled(menu, i, True);
859 if (!menu->flags.realized)
860 wMenuRealize(menu);
863 static WMenu *makeWorkspaceMenu(WScreen *scr)
865 WMenu *menu;
867 menu = wMenuCreate(scr, NULL, False);
868 if (!menu)
869 wwarning(_("could not create workspace submenu for Clip menu"));
871 wMenuAddCallback(menu, "", switchWSCommand, (void *)scr->clip_icon);
873 menu->flags.realized = 0;
874 wMenuRealize(menu);
876 return menu;
879 static void updateClipOptionsMenu(WMenu *menu, WDock *dock)
881 WMenuEntry *entry;
882 int index = 0;
884 if (!menu || !dock)
885 return;
887 /* keep on top */
888 entry = menu->entries[index];
889 entry->flags.indicator_on = !dock->lowered;
890 entry->clientdata = dock;
892 /* collapsed */
893 entry = menu->entries[++index];
894 entry->flags.indicator_on = dock->collapsed;
895 entry->clientdata = dock;
897 /* auto-collapse */
898 entry = menu->entries[++index];
899 entry->flags.indicator_on = dock->auto_collapse;
900 entry->clientdata = dock;
902 /* auto-raise/lower */
903 entry = menu->entries[++index];
904 entry->flags.indicator_on = dock->auto_raise_lower;
905 entry->clientdata = dock;
906 wMenuSetEnabled(menu, index, dock->lowered);
908 /* attract icons */
909 entry = menu->entries[++index];
910 entry->flags.indicator_on = dock->attract_icons;
911 entry->clientdata = dock;
913 menu->flags.realized = 0;
914 wMenuRealize(menu);
917 static WMenu *makeClipOptionsMenu(WScreen *scr)
919 WMenu *menu;
920 WMenuEntry *entry;
922 menu = wMenuCreate(scr, NULL, False);
923 if (!menu) {
924 wwarning(_("could not create options submenu for Clip menu"));
925 return NULL;
928 entry = wMenuAddCallback(menu, _("Keep on Top"), toggleLoweredCallback, NULL);
929 entry->flags.indicator = 1;
930 entry->flags.indicator_on = 1;
931 entry->flags.indicator_type = MI_CHECK;
933 entry = wMenuAddCallback(menu, _("Collapsed"), toggleCollapsedCallback, NULL);
934 entry->flags.indicator = 1;
935 entry->flags.indicator_on = 1;
936 entry->flags.indicator_type = MI_CHECK;
938 entry = wMenuAddCallback(menu, _("Autocollapse"), toggleAutoCollapseCallback, NULL);
939 entry->flags.indicator = 1;
940 entry->flags.indicator_on = 1;
941 entry->flags.indicator_type = MI_CHECK;
943 entry = wMenuAddCallback(menu, _("Autoraise"), toggleAutoRaiseLowerCallback, NULL);
944 entry->flags.indicator = 1;
945 entry->flags.indicator_on = 1;
946 entry->flags.indicator_type = MI_CHECK;
948 entry = wMenuAddCallback(menu, _("Autoattract Icons"), toggleAutoAttractCallback, NULL);
949 entry->flags.indicator = 1;
950 entry->flags.indicator_on = 1;
951 entry->flags.indicator_type = MI_CHECK;
953 menu->flags.realized = 0;
954 wMenuRealize(menu);
956 return menu;
959 static WMenu *dockMenuCreate(WScreen *scr, int type)
961 WMenu *menu;
962 WMenuEntry *entry;
964 if (type == WM_CLIP && scr->clip_menu)
965 return scr->clip_menu;
967 menu = wMenuCreate(scr, NULL, False);
968 if (type != WM_CLIP) {
969 entry = wMenuAddCallback(menu, _("Keep on Top"), toggleLoweredCallback, NULL);
970 entry->flags.indicator = 1;
971 entry->flags.indicator_on = 1;
972 entry->flags.indicator_type = MI_CHECK;
973 } else {
974 entry = wMenuAddCallback(menu, _("Clip Options"), NULL, NULL);
975 scr->clip_options = makeClipOptionsMenu(scr);
976 if (scr->clip_options)
977 wMenuEntrySetCascade(menu, entry, scr->clip_options);
979 entry = wMenuAddCallback(menu, _("Rename Workspace"), renameCallback, NULL);
980 wfree(entry->text);
981 entry->text = _("Rename Workspace");
983 entry = wMenuAddCallback(menu, _("Selected"), selectCallback, NULL);
984 entry->flags.indicator = 1;
985 entry->flags.indicator_on = 1;
986 entry->flags.indicator_type = MI_CHECK;
988 entry = wMenuAddCallback(menu, _("Select All Icons"), selectIconsCallback, NULL);
989 wfree(entry->text);
990 entry->text = _("Select All Icons");
992 entry = wMenuAddCallback(menu, _("Keep Icon"), keepIconsCallback, NULL);
993 wfree(entry->text);
994 entry->text = _("Keep Icon");
996 entry = wMenuAddCallback(menu, _("Move Icon To"), NULL, NULL);
997 wfree(entry->text);
998 entry->text = _("Move Icon To");
999 scr->clip_submenu = makeWorkspaceMenu(scr);
1000 if (scr->clip_submenu)
1001 wMenuEntrySetCascade(menu, entry, scr->clip_submenu);
1003 entry = wMenuAddCallback(menu, _("Remove Icon"), removeIconsCallback, NULL);
1004 wfree(entry->text);
1005 entry->text = _("Remove Icon");
1007 wMenuAddCallback(menu, _("Attract Icons"), colectIconsCallback, NULL);
1010 wMenuAddCallback(menu, _("Launch"), launchCallback, NULL);
1012 wMenuAddCallback(menu, _("Unhide Here"), unhideHereCallback, NULL);
1014 entry = wMenuAddCallback(menu, _("Hide"), hideCallback, NULL);
1015 wfree(entry->text);
1016 entry->text = _("Hide");
1018 wMenuAddCallback(menu, _("Settings..."), settingsCallback, NULL);
1020 wMenuAddCallback(menu, _("Kill"), killCallback, NULL);
1022 if (type == WM_CLIP)
1023 scr->clip_menu = menu;
1025 return menu;
1028 WDock *wDockCreate(WScreen *scr, int type)
1030 WDock *dock;
1031 WAppIcon *btn;
1033 make_keys();
1035 dock = wmalloc(sizeof(WDock));
1037 dock->max_icons = DOCK_MAX_ICONS;
1039 dock->icon_array = wmalloc(sizeof(WAppIcon *) * dock->max_icons);
1041 btn = mainIconCreate(scr, type);
1043 btn->dock = dock;
1045 dock->x_pos = btn->x_pos;
1046 dock->y_pos = btn->y_pos;
1047 dock->screen_ptr = scr;
1048 dock->type = type;
1049 dock->icon_count = 1;
1050 dock->on_right_side = 1;
1051 dock->collapsed = 0;
1052 dock->auto_collapse = 0;
1053 dock->auto_collapse_magic = NULL;
1054 dock->auto_raise_lower = 0;
1055 dock->auto_lower_magic = NULL;
1056 dock->auto_raise_magic = NULL;
1057 dock->attract_icons = 0;
1058 dock->lowered = 1;
1059 dock->icon_array[0] = btn;
1060 wRaiseFrame(btn->icon->core);
1061 XMoveWindow(dpy, btn->icon->core->window, btn->x_pos, btn->y_pos);
1063 /* create dock menu */
1064 dock->menu = dockMenuCreate(scr, type);
1066 return dock;
1069 void wDockDestroy(WDock *dock)
1071 int i;
1072 WAppIcon *aicon;
1074 for (i = (dock->type == WM_CLIP) ? 1 : 0; i < dock->max_icons; i++) {
1075 aicon = dock->icon_array[i];
1076 if (aicon) {
1077 int keepit = aicon->running && wApplicationOf(aicon->main_window);
1078 wDockDetach(dock, aicon);
1079 if (keepit) {
1080 /* XXX: can: aicon->icon == NULL ? */
1081 PlaceIcon(dock->screen_ptr, &aicon->x_pos, &aicon->y_pos,
1082 wGetHeadForWindow(aicon->icon->owner));
1083 XMoveWindow(dpy, aicon->icon->core->window, aicon->x_pos, aicon->y_pos);
1084 if (!dock->mapped || dock->collapsed)
1085 XMapWindow(dpy, aicon->icon->core->window);
1089 if (wPreferences.auto_arrange_icons)
1090 wArrangeIcons(dock->screen_ptr, True);
1091 wfree(dock->icon_array);
1092 if (dock->menu && dock->type != WM_CLIP)
1093 wMenuDestroy(dock->menu, True);
1094 if (dock->screen_ptr->last_dock == dock)
1095 dock->screen_ptr->last_dock = NULL;
1096 wfree(dock);
1099 void wClipIconPaint(WAppIcon *aicon)
1101 WScreen *scr = aicon->icon->core->screen_ptr;
1102 WWorkspace *workspace = scr->workspaces[scr->current_workspace];
1103 WMColor *color;
1104 Window win = aicon->icon->core->window;
1105 int length, nlength;
1106 char *ws_name, ws_number[10];
1107 int ty, tx;
1109 wIconPaint(aicon->icon);
1111 length = strlen(workspace->name);
1112 ws_name = wmalloc(length + 1);
1113 snprintf(ws_name, length + 1, "%s", workspace->name);
1114 snprintf(ws_number, sizeof(ws_number), "%i", scr->current_workspace + 1);
1115 nlength = strlen(ws_number);
1117 if (!workspace->clip->collapsed)
1118 color = scr->clip_title_color[CLIP_NORMAL];
1119 else
1120 color = scr->clip_title_color[CLIP_COLLAPSED];
1122 ty = ICON_SIZE - WMFontHeight(scr->clip_title_font) - 3;
1124 tx = CLIP_BUTTON_SIZE * ICON_SIZE / 64;
1126 if(wPreferences.show_clip_title)
1127 WMDrawString(scr->wmscreen, win, color, scr->clip_title_font, tx, ty, ws_name, length);
1129 tx = (ICON_SIZE / 2 - WMWidthOfString(scr->clip_title_font, ws_number, nlength)) / 2;
1131 WMDrawString(scr->wmscreen, win, color, scr->clip_title_font, tx, 2, ws_number, nlength);
1133 wfree(ws_name);
1135 if (aicon->launching)
1136 XFillRectangle(dpy, aicon->icon->core->window, scr->stipple_gc,
1137 0, 0, wPreferences.icon_size, wPreferences.icon_size);
1139 paintClipButtons(aicon, aicon->dock->lclip_button_pushed, aicon->dock->rclip_button_pushed);
1142 static void clipIconExpose(WObjDescriptor *desc, XEvent *event)
1144 wClipIconPaint(desc->parent);
1147 static void dockIconPaint(WAppIcon *btn)
1149 if (btn == btn->icon->core->screen_ptr->clip_icon) {
1150 wClipIconPaint(btn);
1151 } else {
1152 wAppIconPaint(btn);
1153 save_appicon(btn, True);
1157 static WMPropList *make_icon_state(WAppIcon *btn)
1159 WMPropList *node = NULL;
1160 WMPropList *command, *autolaunch, *lock, *name, *forced;
1161 WMPropList *position, *buggy, *omnipresent;
1162 char *tmp;
1163 char buffer[64];
1165 if (btn) {
1166 if (!btn->command)
1167 command = WMCreatePLString("-");
1168 else
1169 command = WMCreatePLString(btn->command);
1171 autolaunch = btn->auto_launch ? dYes : dNo;
1173 lock = btn->lock ? dYes : dNo;
1175 tmp = EscapeWM_CLASS(btn->wm_instance, btn->wm_class);
1177 name = WMCreatePLString(tmp);
1179 wfree(tmp);
1181 forced = btn->forced_dock ? dYes : dNo;
1183 buggy = btn->buggy_app ? dYes : dNo;
1185 if (btn == btn->icon->core->screen_ptr->clip_icon)
1186 snprintf(buffer, sizeof(buffer), "%i,%i", btn->x_pos, btn->y_pos);
1187 else
1188 snprintf(buffer, sizeof(buffer), "%hi,%hi", btn->xindex, btn->yindex);
1189 position = WMCreatePLString(buffer);
1191 node = WMCreatePLDictionary(dCommand, command,
1192 dName, name,
1193 dAutoLaunch, autolaunch,
1194 dLock, lock,
1195 dForced, forced, dBuggyApplication, buggy, dPosition, position, NULL);
1196 WMReleasePropList(command);
1197 WMReleasePropList(name);
1198 WMReleasePropList(position);
1200 omnipresent = btn->omnipresent ? dYes : dNo;
1201 if (btn->dock != btn->icon->core->screen_ptr->dock && (btn->xindex != 0 || btn->yindex != 0))
1202 WMPutInPLDictionary(node, dOmnipresent, omnipresent);
1204 #ifdef XDND /* was OFFIX */
1205 if (btn->dnd_command) {
1206 command = WMCreatePLString(btn->dnd_command);
1207 WMPutInPLDictionary(node, dDropCommand, command);
1208 WMReleasePropList(command);
1210 #endif /* XDND */
1212 if (btn->paste_command) {
1213 command = WMCreatePLString(btn->paste_command);
1214 WMPutInPLDictionary(node, dPasteCommand, command);
1215 WMReleasePropList(command);
1219 return node;
1222 static WMPropList *dockSaveState(WDock *dock)
1224 int i;
1225 WMPropList *icon_info;
1226 WMPropList *list = NULL, *dock_state = NULL;
1227 WMPropList *value, *key;
1228 char buffer[256];
1230 list = WMCreatePLArray(NULL);
1232 for (i = (dock->type == WM_DOCK ? 0 : 1); i < dock->max_icons; i++) {
1233 WAppIcon *btn = dock->icon_array[i];
1235 if (!btn || btn->attracted)
1236 continue;
1238 if ((icon_info = make_icon_state(dock->icon_array[i]))) {
1239 WMAddToPLArray(list, icon_info);
1240 WMReleasePropList(icon_info);
1244 dock_state = WMCreatePLDictionary(dApplications, list, NULL);
1246 if (dock->type == WM_DOCK) {
1247 snprintf(buffer, sizeof(buffer), "Applications%i", dock->screen_ptr->scr_height);
1248 key = WMCreatePLString(buffer);
1249 WMPutInPLDictionary(dock_state, key, list);
1250 WMReleasePropList(key);
1252 snprintf(buffer, sizeof(buffer), "%i,%i", (dock->on_right_side ? -ICON_SIZE : 0), dock->y_pos);
1253 value = WMCreatePLString(buffer);
1254 WMPutInPLDictionary(dock_state, dPosition, value);
1255 WMReleasePropList(value);
1257 WMReleasePropList(list);
1259 value = (dock->lowered ? dYes : dNo);
1260 WMPutInPLDictionary(dock_state, dLowered, value);
1262 if (dock->type == WM_CLIP) {
1263 value = (dock->collapsed ? dYes : dNo);
1264 WMPutInPLDictionary(dock_state, dCollapsed, value);
1266 value = (dock->auto_collapse ? dYes : dNo);
1267 WMPutInPLDictionary(dock_state, dAutoCollapse, value);
1269 value = (dock->auto_raise_lower ? dYes : dNo);
1270 WMPutInPLDictionary(dock_state, dAutoRaiseLower, value);
1272 value = (dock->attract_icons ? dYes : dNo);
1273 WMPutInPLDictionary(dock_state, dAutoAttractIcons, value);
1276 return dock_state;
1279 void wDockSaveState(WScreen *scr, WMPropList *old_state)
1281 WMPropList *dock_state;
1282 WMPropList *keys;
1284 dock_state = dockSaveState(scr->dock);
1287 * Copy saved states of docks with different sizes.
1289 if (old_state) {
1290 int i;
1291 WMPropList *tmp;
1293 keys = WMGetPLDictionaryKeys(old_state);
1294 for (i = 0; i < WMGetPropListItemCount(keys); i++) {
1295 tmp = WMGetFromPLArray(keys, i);
1297 if (strncasecmp(WMGetFromPLString(tmp), "applications", 12) == 0
1298 && !WMGetFromPLDictionary(dock_state, tmp)) {
1300 WMPutInPLDictionary(dock_state, tmp, WMGetFromPLDictionary(old_state, tmp));
1303 WMReleasePropList(keys);
1306 WMPutInPLDictionary(scr->session_state, dDock, dock_state);
1308 WMReleasePropList(dock_state);
1311 void wClipSaveState(WScreen *scr)
1313 WMPropList *clip_state;
1315 clip_state = make_icon_state(scr->clip_icon);
1317 WMPutInPLDictionary(scr->session_state, dClip, clip_state);
1319 WMReleasePropList(clip_state);
1322 WMPropList *wClipSaveWorkspaceState(WScreen *scr, int workspace)
1324 return dockSaveState(scr->workspaces[workspace]->clip);
1327 static Bool getBooleanDockValue(WMPropList *value, WMPropList *key)
1329 if (value) {
1330 if (WMIsPLString(value)) {
1331 if (strcasecmp(WMGetFromPLString(value), "YES") == 0)
1332 return True;
1333 } else {
1334 wwarning(_("bad value in docked icon state info %s"), WMGetFromPLString(key));
1337 return False;
1340 static WAppIcon *restore_icon_state(WScreen *scr, WMPropList *info, int type, int index)
1342 WAppIcon *aicon;
1343 WMPropList *cmd, *value;
1344 char *wclass, *winstance, *command;
1346 cmd = WMGetFromPLDictionary(info, dCommand);
1347 if (!cmd || !WMIsPLString(cmd))
1348 return NULL;
1350 /* parse window name */
1351 value = WMGetFromPLDictionary(info, dName);
1352 if (!value)
1353 return NULL;
1355 ParseWindowName(value, &winstance, &wclass, "dock");
1357 if (!winstance && !wclass)
1358 return NULL;
1360 /* get commands */
1361 if (cmd)
1362 command = wstrdup(WMGetFromPLString(cmd));
1363 else
1364 command = NULL;
1366 if (!command || strcmp(command, "-") == 0) {
1367 if (command)
1368 wfree(command);
1369 if (wclass)
1370 wfree(wclass);
1371 if (winstance)
1372 wfree(winstance);
1374 return NULL;
1377 aicon = wAppIconCreateForDock(scr, command, winstance, wclass, TILE_NORMAL);
1378 if (wclass)
1379 wfree(wclass);
1380 if (winstance)
1381 wfree(winstance);
1382 if (command)
1383 wfree(command);
1385 aicon->icon->core->descriptor.handle_mousedown = iconMouseDown;
1386 if (type == WM_CLIP) {
1387 aicon->icon->core->descriptor.handle_enternotify = clipEnterNotify;
1388 aicon->icon->core->descriptor.handle_leavenotify = clipLeaveNotify;
1390 aicon->icon->core->descriptor.parent_type = WCLASS_DOCK_ICON;
1391 aicon->icon->core->descriptor.parent = aicon;
1393 #ifdef XDND /* was OFFIX */
1394 cmd = WMGetFromPLDictionary(info, dDropCommand);
1395 if (cmd)
1396 aicon->dnd_command = wstrdup(WMGetFromPLString(cmd));
1397 #endif
1399 cmd = WMGetFromPLDictionary(info, dPasteCommand);
1400 if (cmd)
1401 aicon->paste_command = wstrdup(WMGetFromPLString(cmd));
1403 /* check auto launch */
1404 value = WMGetFromPLDictionary(info, dAutoLaunch);
1406 aicon->auto_launch = getBooleanDockValue(value, dAutoLaunch);
1408 /* check lock */
1409 value = WMGetFromPLDictionary(info, dLock);
1411 aicon->lock = getBooleanDockValue(value, dLock);
1413 /* check if it wasn't normally docked */
1414 value = WMGetFromPLDictionary(info, dForced);
1416 aicon->forced_dock = getBooleanDockValue(value, dForced);
1418 /* check if we can rely on the stuff in the app */
1419 value = WMGetFromPLDictionary(info, dBuggyApplication);
1421 aicon->buggy_app = getBooleanDockValue(value, dBuggyApplication);
1423 /* get position in the dock */
1424 value = WMGetFromPLDictionary(info, dPosition);
1425 if (value && WMIsPLString(value)) {
1426 if (sscanf(WMGetFromPLString(value), "%hi,%hi", &aicon->xindex, &aicon->yindex) != 2)
1427 wwarning(_("bad value in docked icon state info %s"), WMGetFromPLString(dPosition));
1429 /* check position sanity */
1430 /* incomplete section! */
1431 if (type == WM_DOCK) {
1432 aicon->xindex = 0;
1433 if (aicon->yindex < 0)
1434 wwarning(_("bad value in docked icon position %i,%i"),
1435 aicon->xindex, aicon->yindex);
1437 } else {
1438 aicon->yindex = index;
1439 aicon->xindex = 0;
1442 /* check if icon is omnipresent */
1443 value = WMGetFromPLDictionary(info, dOmnipresent);
1445 aicon->omnipresent = getBooleanDockValue(value, dOmnipresent);
1447 aicon->running = 0;
1448 aicon->docked = 1;
1450 return aicon;
1453 #define COMPLAIN(key) wwarning(_("bad value in dock state info:%s"), key)
1455 WAppIcon *wClipRestoreState(WScreen *scr, WMPropList *clip_state)
1457 WAppIcon *icon;
1458 WMPropList *value;
1460 icon = mainIconCreate(scr, WM_CLIP);
1462 if (!clip_state)
1463 return icon;
1465 WMRetainPropList(clip_state);
1467 /* restore position */
1469 value = WMGetFromPLDictionary(clip_state, dPosition);
1471 if (value) {
1472 if (!WMIsPLString(value)) {
1473 COMPLAIN("Position");
1474 } else {
1475 WMRect rect;
1476 int flags;
1478 if (sscanf(WMGetFromPLString(value), "%i,%i", &icon->x_pos, &icon->y_pos) != 2)
1479 COMPLAIN("Position");
1481 /* check position sanity */
1482 rect.pos.x = icon->x_pos;
1483 rect.pos.y = icon->y_pos;
1484 rect.size.width = rect.size.height = ICON_SIZE;
1486 wGetRectPlacementInfo(scr, rect, &flags);
1487 if (flags & (XFLAG_DEAD | XFLAG_PARTIAL))
1488 wScreenKeepInside(scr, &icon->x_pos, &icon->y_pos, ICON_SIZE, ICON_SIZE);
1491 #ifdef XDND /* was OFFIX */
1492 value = WMGetFromPLDictionary(clip_state, dDropCommand);
1493 if (value && WMIsPLString(value))
1494 icon->dnd_command = wstrdup(WMGetFromPLString(value));
1495 #endif
1497 value = WMGetFromPLDictionary(clip_state, dPasteCommand);
1498 if (value && WMIsPLString(value))
1499 icon->paste_command = wstrdup(WMGetFromPLString(value));
1501 WMReleasePropList(clip_state);
1503 return icon;
1506 WDock *wDockRestoreState(WScreen *scr, WMPropList *dock_state, int type)
1508 WDock *dock;
1509 WMPropList *apps;
1510 WMPropList *value;
1511 WAppIcon *aicon, *old_top;
1512 int count, i;
1514 dock = wDockCreate(scr, type);
1516 if (!dock_state)
1517 return dock;
1519 WMRetainPropList(dock_state);
1521 /* restore position */
1522 value = WMGetFromPLDictionary(dock_state, dPosition);
1523 if (value) {
1524 if (!WMIsPLString(value)) {
1525 COMPLAIN("Position");
1526 } else {
1527 WMRect rect;
1528 int flags;
1530 if (sscanf(WMGetFromPLString(value), "%i,%i", &dock->x_pos, &dock->y_pos) != 2)
1531 COMPLAIN("Position");
1533 /* check position sanity */
1534 rect.pos.x = dock->x_pos;
1535 rect.pos.y = dock->y_pos;
1536 rect.size.width = rect.size.height = ICON_SIZE;
1538 wGetRectPlacementInfo(scr, rect, &flags);
1539 if (flags & (XFLAG_DEAD | XFLAG_PARTIAL)) {
1540 int x = dock->x_pos;
1541 wScreenKeepInside(scr, &x, &dock->y_pos, ICON_SIZE, ICON_SIZE);
1544 /* Is this needed any more? */
1545 if (type == WM_CLIP) {
1546 if (dock->x_pos < 0) {
1547 dock->x_pos = 0;
1548 } else if (dock->x_pos > scr->scr_width - ICON_SIZE) {
1549 dock->x_pos = scr->scr_width - ICON_SIZE;
1551 } else {
1552 if (dock->x_pos >= 0) {
1553 dock->x_pos = DOCK_EXTRA_SPACE;
1554 dock->on_right_side = 0;
1555 } else {
1556 dock->x_pos = scr->scr_width - DOCK_EXTRA_SPACE - ICON_SIZE;
1557 dock->on_right_side = 1;
1563 /* restore lowered/raised state */
1564 dock->lowered = 0;
1566 value = WMGetFromPLDictionary(dock_state, dLowered);
1567 if (value) {
1568 if (!WMIsPLString(value)) {
1569 COMPLAIN("Lowered");
1570 } else {
1571 if (strcasecmp(WMGetFromPLString(value), "YES") == 0)
1572 dock->lowered = 1;
1576 /* restore collapsed state */
1577 dock->collapsed = 0;
1579 value = WMGetFromPLDictionary(dock_state, dCollapsed);
1580 if (value) {
1581 if (!WMIsPLString(value)) {
1582 COMPLAIN("Collapsed");
1583 } else {
1584 if (strcasecmp(WMGetFromPLString(value), "YES") == 0)
1585 dock->collapsed = 1;
1589 /* restore auto-collapsed state */
1590 value = WMGetFromPLDictionary(dock_state, dAutoCollapse);
1591 if (value) {
1592 if (!WMIsPLString(value)) {
1593 COMPLAIN("AutoCollapse");
1594 } else {
1595 if (strcasecmp(WMGetFromPLString(value), "YES") == 0) {
1596 dock->auto_collapse = 1;
1597 dock->collapsed = 1;
1602 /* restore auto-raise/lower state */
1603 value = WMGetFromPLDictionary(dock_state, dAutoRaiseLower);
1604 if (value) {
1605 if (!WMIsPLString(value)) {
1606 COMPLAIN("AutoRaiseLower");
1607 } else {
1608 if (strcasecmp(WMGetFromPLString(value), "YES") == 0)
1609 dock->auto_raise_lower = 1;
1613 /* restore attract icons state */
1614 dock->attract_icons = 0;
1616 value = WMGetFromPLDictionary(dock_state, dAutoAttractIcons);
1617 if (value) {
1618 if (!WMIsPLString(value)) {
1619 COMPLAIN("AutoAttractIcons");
1620 } else {
1621 if (strcasecmp(WMGetFromPLString(value), "YES") == 0)
1622 dock->attract_icons = 1;
1626 /* application list */
1629 WMPropList *tmp;
1630 char buffer[64];
1633 * When saving, it saves the dock state in
1634 * Applications and Applicationsnnn
1636 * When loading, it will first try Applicationsnnn.
1637 * If it does not exist, use Applications as default.
1640 snprintf(buffer, sizeof(buffer), "Applications%i", scr->scr_height);
1642 tmp = WMCreatePLString(buffer);
1643 apps = WMGetFromPLDictionary(dock_state, tmp);
1644 WMReleasePropList(tmp);
1646 if (!apps)
1647 apps = WMGetFromPLDictionary(dock_state, dApplications);
1650 if (!apps)
1651 goto finish;
1653 count = WMGetPropListItemCount(apps);
1654 if (count == 0)
1655 goto finish;
1657 old_top = dock->icon_array[0];
1659 /* dock->icon_count is set to 1 when dock is created.
1660 * Since Clip is already restored, we want to keep it so for clip,
1661 * but for dock we may change the default top tile, so we set it to 0.
1663 if (type == WM_DOCK)
1664 dock->icon_count = 0;
1666 for (i = 0; i < count; i++) {
1667 if (dock->icon_count >= dock->max_icons) {
1668 wwarning(_("there are too many icons stored in dock. Ignoring what doesn't fit"));
1669 break;
1672 value = WMGetFromPLArray(apps, i);
1673 aicon = restore_icon_state(scr, value, type, dock->icon_count);
1675 dock->icon_array[dock->icon_count] = aicon;
1677 if (aicon) {
1678 aicon->dock = dock;
1679 aicon->x_pos = dock->x_pos + (aicon->xindex * ICON_SIZE);
1680 aicon->y_pos = dock->y_pos + (aicon->yindex * ICON_SIZE);
1682 if (dock->lowered)
1683 ChangeStackingLevel(aicon->icon->core, WMNormalLevel);
1684 else
1685 ChangeStackingLevel(aicon->icon->core, WMDockLevel);
1687 wCoreConfigure(aicon->icon->core, aicon->x_pos, aicon->y_pos, 0, 0);
1688 if (!dock->collapsed)
1689 XMapWindow(dpy, aicon->icon->core->window);
1691 wRaiseFrame(aicon->icon->core);
1693 dock->icon_count++;
1694 } else if (dock->icon_count == 0 && type == WM_DOCK) {
1695 dock->icon_count++;
1699 /* if the first icon is not defined, use the default */
1700 if (dock->icon_array[0] == NULL) {
1701 /* update default icon */
1702 old_top->x_pos = dock->x_pos;
1703 old_top->y_pos = dock->y_pos;
1704 if (dock->lowered)
1705 ChangeStackingLevel(old_top->icon->core, WMNormalLevel);
1706 else
1707 ChangeStackingLevel(old_top->icon->core, WMDockLevel);
1709 dock->icon_array[0] = old_top;
1710 XMoveWindow(dpy, old_top->icon->core->window, dock->x_pos, dock->y_pos);
1711 /* we don't need to increment dock->icon_count here because it was
1712 * incremented in the loop above.
1714 } else if (old_top != dock->icon_array[0]) {
1715 if (old_top == scr->clip_icon)
1716 scr->clip_icon = dock->icon_array[0];
1718 wAppIconDestroy(old_top);
1721 finish:
1722 WMReleasePropList(dock_state);
1724 return dock;
1727 void wDockLaunchWithState(WDock *dock, WAppIcon *btn, WSavedState *state)
1729 if (btn && btn->command && !btn->running && !btn->launching) {
1730 btn->drop_launch = 0;
1731 btn->paste_launch = 0;
1733 btn->pid = execCommand(btn, btn->command, state);
1735 if (btn->pid > 0) {
1736 if (!btn->forced_dock && !btn->buggy_app) {
1737 btn->launching = 1;
1738 dockIconPaint(btn);
1741 } else {
1742 wfree(state);
1746 void wDockDoAutoLaunch(WDock *dock, int workspace)
1748 WAppIcon *btn;
1749 WSavedState *state;
1750 int i;
1752 for (i = 0; i < dock->max_icons; i++) {
1753 btn = dock->icon_array[i];
1754 if (!btn || !btn->auto_launch)
1755 continue;
1757 state = wmalloc(sizeof(WSavedState));
1758 state->workspace = workspace;
1759 /* TODO: this is klugy and is very difficult to understand
1760 * what's going on. Try to clean up */
1761 wDockLaunchWithState(dock, btn, state);
1765 #ifdef XDND /* was OFFIX */
1766 static WDock *findDock(WScreen *scr, XEvent *event, int *icon_pos)
1768 WDock *dock;
1769 int i;
1771 *icon_pos = -1;
1772 if ((dock = scr->dock) != NULL) {
1773 for (i = 0; i < dock->max_icons; i++) {
1774 if (dock->icon_array[i]
1775 && dock->icon_array[i]->icon->core->window == event->xclient.window) {
1776 *icon_pos = i;
1777 break;
1781 if (*icon_pos < 0 && (dock = scr->workspaces[scr->current_workspace]->clip) != NULL) {
1782 for (i = 0; i < dock->max_icons; i++) {
1783 if (dock->icon_array[i]
1784 && dock->icon_array[i]->icon->core->window == event->xclient.window) {
1785 *icon_pos = i;
1786 break;
1790 if (*icon_pos >= 0)
1791 return dock;
1792 return NULL;
1795 int wDockReceiveDNDDrop(WScreen *scr, XEvent *event)
1797 WDock *dock;
1798 WAppIcon *btn;
1799 int icon_pos;
1801 dock = findDock(scr, event, &icon_pos);
1802 if (!dock)
1803 return False;
1806 * Return True if the drop was on an application icon window.
1807 * In this case, let the ClientMessage handler redirect the
1808 * message to the app.
1810 if (dock->icon_array[icon_pos]->icon->icon_win != None)
1811 return True;
1813 if (dock->icon_array[icon_pos]->dnd_command != NULL) {
1814 scr->flags.dnd_data_convertion_status = 0;
1816 btn = dock->icon_array[icon_pos];
1818 if (!btn->forced_dock) {
1819 btn->relaunching = btn->running;
1820 btn->running = 1;
1822 if (btn->wm_instance || btn->wm_class) {
1823 WWindowAttributes attr;
1824 memset(&attr, 0, sizeof(WWindowAttributes));
1825 wDefaultFillAttributes(btn->wm_instance, btn->wm_class, &attr, NULL, True);
1827 if (!attr.no_appicon)
1828 btn->launching = 1;
1829 else
1830 btn->running = 0;
1833 btn->paste_launch = 0;
1834 btn->drop_launch = 1;
1835 scr->last_dock = dock;
1836 btn->pid = execCommand(btn, btn->dnd_command, NULL);
1837 if (btn->pid > 0) {
1838 dockIconPaint(btn);
1839 } else {
1840 btn->launching = 0;
1841 if (!btn->relaunching)
1842 btn->running = 0;
1845 return False;
1847 #endif /* XDND */
1849 Bool wDockAttachIcon(WDock *dock, WAppIcon *icon, int x, int y, Bool update_icon)
1851 WWindow *wwin;
1852 int index;
1854 wwin = icon->icon->owner;
1855 icon->editing = 0;
1857 if (update_icon)
1858 icon->icon->force_paint = 1;
1860 if (icon->command == NULL) {
1861 char *command;
1863 command = GetCommandForWindow(wwin->client_win);
1864 if (command) {
1865 icon->command = command;
1866 } else {
1867 /* icon->forced_dock = 1; */
1868 if (dock->type != WM_CLIP || !icon->attracted) {
1869 icon->editing = 1;
1870 if (wInputDialog(dock->screen_ptr, _("Dock Icon"),
1871 _("Type the command used to launch the application"), &command)) {
1872 if (command && (command[0] == 0 || (command[0] == '-' && command[1] == 0))) {
1873 wfree(command);
1874 command = NULL;
1876 icon->command = command;
1877 icon->editing = 0;
1878 } else {
1879 icon->editing = 0;
1880 if (command)
1881 wfree(command);
1882 /* If the target is the dock, reject the icon. If
1883 * the target is the clip, make it an attracted icon
1885 if (dock->type == WM_CLIP) {
1886 icon->attracted = 1;
1887 if (!icon->icon->shadowed) {
1888 icon->icon->shadowed = 1;
1889 icon->icon->force_paint = 1;
1891 } else {
1892 return False;
1899 for (index = 1; index < dock->max_icons; index++)
1900 if (dock->icon_array[index] == NULL)
1901 break;
1902 /* if (index == dock->max_icons)
1903 return; */
1905 assert(index < dock->max_icons);
1907 dock->icon_array[index] = icon;
1908 icon->yindex = y;
1909 icon->xindex = x;
1911 icon->omnipresent = 0;
1913 icon->x_pos = dock->x_pos + x * ICON_SIZE;
1914 icon->y_pos = dock->y_pos + y * ICON_SIZE;
1916 dock->icon_count++;
1918 icon->running = 1;
1919 icon->launching = 0;
1920 icon->docked = 1;
1921 icon->dock = dock;
1922 icon->icon->core->descriptor.handle_mousedown = iconMouseDown;
1923 if (dock->type == WM_CLIP) {
1924 icon->icon->core->descriptor.handle_enternotify = clipEnterNotify;
1925 icon->icon->core->descriptor.handle_leavenotify = clipLeaveNotify;
1927 icon->icon->core->descriptor.parent_type = WCLASS_DOCK_ICON;
1928 icon->icon->core->descriptor.parent = icon;
1930 MoveInStackListUnder(dock->icon_array[index - 1]->icon->core, icon->icon->core);
1931 wAppIconMove(icon, icon->x_pos, icon->y_pos);
1932 wAppIconPaint(icon);
1933 save_appicon(icon, True);
1935 if (wPreferences.auto_arrange_icons)
1936 wArrangeIcons(dock->screen_ptr, True);
1938 #ifdef XDND /* was OFFIX */
1939 if (icon->command && !icon->dnd_command) {
1940 int len = strlen(icon->command) + 8;
1941 icon->dnd_command = wmalloc(len);
1942 snprintf(icon->dnd_command, len, "%s %%d", icon->command);
1944 #endif
1946 if (icon->command && !icon->paste_command) {
1947 int len = strlen(icon->command) + 8;
1948 icon->paste_command = wmalloc(len);
1949 snprintf(icon->paste_command, len, "%s %%s", icon->command);
1952 return True;
1955 static void reattachIcon(WDock *dock, WAppIcon *icon, int x, int y)
1957 int index;
1959 for (index = 1; index < dock->max_icons; index++) {
1960 if (dock->icon_array[index] == icon)
1961 break;
1963 assert(index < dock->max_icons);
1965 icon->yindex = y;
1966 icon->xindex = x;
1968 icon->x_pos = dock->x_pos + x * ICON_SIZE;
1969 icon->y_pos = dock->y_pos + y * ICON_SIZE;
1972 static Bool moveIconBetweenDocks(WDock *src, WDock *dest, WAppIcon *icon, int x, int y)
1974 WWindow *wwin;
1975 char *command;
1976 int index;
1978 if (src == dest)
1979 return True; /* No move needed, we're already there */
1981 if (dest == NULL)
1982 return False;
1984 wwin = icon->icon->owner;
1987 * For the moment we can't do this if we move icons in Clip from one
1988 * workspace to other, because if we move two or more icons without
1989 * command, the dialog box will not be able to tell us to which of the
1990 * moved icons it applies. -Dan
1992 if ((dest->type == WM_DOCK /*|| dest->keep_attracted */ ) && icon->command == NULL) {
1993 command = GetCommandForWindow(wwin->client_win);
1994 if (command) {
1995 icon->command = command;
1996 } else {
1997 icon->editing = 1;
1998 /* icon->forced_dock = 1; */
1999 if (wInputDialog(src->screen_ptr, _("Dock Icon"),
2000 _("Type the command used to launch the application"), &command)) {
2001 if (command && (command[0] == 0 || (command[0] == '-' && command[1] == 0))) {
2002 wfree(command);
2003 command = NULL;
2005 icon->command = command;
2006 } else {
2007 icon->editing = 0;
2008 if (command)
2009 wfree(command);
2010 return False;
2012 icon->editing = 0;
2016 if (dest->type == WM_DOCK)
2017 wClipMakeIconOmnipresent(icon, False);
2019 for (index = 1; index < src->max_icons; index++) {
2020 if (src->icon_array[index] == icon)
2021 break;
2023 assert(index < src->max_icons);
2025 src->icon_array[index] = NULL;
2026 src->icon_count--;
2028 for (index = 1; index < dest->max_icons; index++) {
2029 if (dest->icon_array[index] == NULL)
2030 break;
2033 assert(index < dest->max_icons);
2035 dest->icon_array[index] = icon;
2036 icon->dock = dest;
2038 /* deselect the icon */
2039 if (icon->icon->selected)
2040 wIconSelect(icon->icon);
2042 if (dest->type == WM_DOCK) {
2043 icon->icon->core->descriptor.handle_enternotify = NULL;
2044 icon->icon->core->descriptor.handle_leavenotify = NULL;
2045 } else {
2046 icon->icon->core->descriptor.handle_enternotify = clipEnterNotify;
2047 icon->icon->core->descriptor.handle_leavenotify = clipLeaveNotify;
2050 /* set it to be kept when moving to dock.
2051 * Unless the icon does not have a command set
2053 if (icon->command && dest->type == WM_DOCK) {
2054 icon->attracted = 0;
2055 if (icon->icon->shadowed) {
2056 icon->icon->shadowed = 0;
2057 icon->icon->force_paint = 1;
2059 save_appicon(icon, True);
2062 if (src->auto_collapse || src->auto_raise_lower)
2063 clipLeave(src);
2065 icon->yindex = y;
2066 icon->xindex = x;
2068 icon->x_pos = dest->x_pos + x * ICON_SIZE;
2069 icon->y_pos = dest->y_pos + y * ICON_SIZE;
2071 dest->icon_count++;
2073 MoveInStackListUnder(dest->icon_array[index - 1]->icon->core, icon->icon->core);
2074 wAppIconPaint(icon);
2076 return True;
2079 void wDockDetach(WDock *dock, WAppIcon *icon)
2081 int index;
2083 /* make the settings panel be closed */
2084 if (icon->panel)
2085 DestroyDockAppSettingsPanel(icon->panel);
2087 /* This must be called before icon->dock is set to NULL.
2088 * Don't move it. -Dan
2090 wClipMakeIconOmnipresent(icon, False);
2092 icon->docked = 0;
2093 icon->dock = NULL;
2094 icon->attracted = 0;
2095 icon->auto_launch = 0;
2096 if (icon->icon->shadowed) {
2097 icon->icon->shadowed = 0;
2098 icon->icon->force_paint = 1;
2101 /* deselect the icon */
2102 if (icon->icon->selected)
2103 wIconSelect(icon->icon);
2105 if (icon->command) {
2106 wfree(icon->command);
2107 icon->command = NULL;
2109 #ifdef XDND /* was OFFIX */
2110 if (icon->dnd_command) {
2111 wfree(icon->dnd_command);
2112 icon->dnd_command = NULL;
2114 #endif
2115 if (icon->paste_command) {
2116 wfree(icon->paste_command);
2117 icon->paste_command = NULL;
2120 for (index = 1; index < dock->max_icons; index++)
2121 if (dock->icon_array[index] == icon)
2122 break;
2124 assert(index < dock->max_icons);
2125 dock->icon_array[index] = NULL;
2126 icon->yindex = -1;
2127 icon->xindex = -1;
2129 dock->icon_count--;
2131 /* if the dock is not attached to an application or
2132 * the application did not set the appropriate hints yet,
2133 * destroy the icon */
2134 if (!icon->running || !wApplicationOf(icon->main_window)) {
2135 wAppIconDestroy(icon);
2136 } else {
2137 icon->icon->core->descriptor.handle_mousedown = appIconMouseDown;
2138 icon->icon->core->descriptor.handle_enternotify = NULL;
2139 icon->icon->core->descriptor.handle_leavenotify = NULL;
2140 icon->icon->core->descriptor.parent_type = WCLASS_APPICON;
2141 icon->icon->core->descriptor.parent = icon;
2143 ChangeStackingLevel(icon->icon->core, NORMAL_ICON_LEVEL);
2145 wAppIconPaint(icon);
2146 if (wPreferences.auto_arrange_icons)
2147 wArrangeIcons(dock->screen_ptr, True);
2149 if (dock->auto_collapse || dock->auto_raise_lower)
2150 clipLeave(dock);
2154 * returns the closest Dock slot index for the passed
2155 * coordinates.
2157 * Returns False if icon can't be docked.
2159 * Note: this function should NEVER alter ret_x or ret_y, unless it will
2160 * return True. -Dan
2162 Bool wDockSnapIcon(WDock *dock, WAppIcon *icon, int req_x, int req_y, int *ret_x, int *ret_y, int redocking)
2164 WScreen *scr = dock->screen_ptr;
2165 int dx, dy;
2166 int ex_x, ex_y;
2167 int i, offset = ICON_SIZE / 2;
2168 WAppIcon *aicon = NULL;
2169 WAppIcon *nicon = NULL;
2170 int max_y_icons;
2172 /* TODO: XINERAMA, for these */
2173 max_y_icons = scr->scr_height / ICON_SIZE - 1;
2175 if (wPreferences.flags.noupdates)
2176 return False;
2178 dx = dock->x_pos;
2179 dy = dock->y_pos;
2181 /* if the dock is full */
2182 if (!redocking && (dock->icon_count >= dock->max_icons))
2183 return False;
2185 /* exact position */
2186 if (req_y < dy)
2187 ex_y = (req_y - offset - dy) / ICON_SIZE;
2188 else
2189 ex_y = (req_y + offset - dy) / ICON_SIZE;
2191 if (req_x < dx)
2192 ex_x = (req_x - offset - dx) / ICON_SIZE;
2193 else
2194 ex_x = (req_x + offset - dx) / ICON_SIZE;
2196 /* check if the icon is outside the screen boundaries */
2198 WMRect rect;
2199 int flags;
2201 rect.pos.x = dx + ex_x * ICON_SIZE;
2202 rect.pos.y = dy + ex_y * ICON_SIZE;
2203 rect.size.width = rect.size.height = ICON_SIZE;
2205 wGetRectPlacementInfo(scr, rect, &flags);
2206 if (flags & (XFLAG_DEAD | XFLAG_PARTIAL))
2207 return False;
2210 if (dock->type == WM_DOCK) {
2211 if (icon->dock != dock && ex_x != 0)
2212 return False;
2214 aicon = NULL;
2215 for (i = 0; i < dock->max_icons; i++) {
2216 nicon = dock->icon_array[i];
2217 if (nicon && nicon->yindex == ex_y) {
2218 aicon = nicon;
2219 break;
2223 if (redocking) {
2224 int sig, done, closest;
2226 /* Possible cases when redocking:
2228 * icon dragged out of range of any slot -> false
2229 * icon dragged to range of free slot
2230 * icon dragged to range of same slot
2231 * icon dragged to range of different icon
2233 if (abs(ex_x) > DOCK_DETTACH_THRESHOLD)
2234 return False;
2236 if (ex_y >= 0 && ex_y <= max_y_icons && (aicon == icon || !aicon)) {
2237 *ret_x = 0;
2238 *ret_y = ex_y;
2239 return True;
2242 /* start looking at the upper slot or lower? */
2243 if (ex_y * ICON_SIZE < (req_y + offset - dy))
2244 sig = 1;
2245 else
2246 sig = -1;
2248 closest = -1;
2249 done = 0;
2250 /* look for closest free slot */
2251 for (i = 0; i < (DOCK_DETTACH_THRESHOLD + 1) * 2 && !done; i++) {
2252 int j;
2254 done = 1;
2255 closest = sig * (i / 2) + ex_y;
2256 /* check if this slot is used */
2257 if (closest >= 0) {
2258 for (j = 0; j < dock->max_icons; j++) {
2259 if (dock->icon_array[j]
2260 && dock->icon_array[j]->yindex == closest) {
2261 /* slot is used by someone else */
2262 if (dock->icon_array[j] != icon)
2263 done = 0;
2264 break;
2268 sig = -sig;
2270 if (done && closest >= 0 && closest <= max_y_icons &&
2271 ((ex_y >= closest && ex_y - closest < DOCK_DETTACH_THRESHOLD + 1)
2272 || (ex_y < closest && closest - ex_y <= DOCK_DETTACH_THRESHOLD + 1))) {
2273 *ret_x = 0;
2274 *ret_y = closest;
2275 return True;
2277 } else { /* !redocking */
2279 /* if slot is free and the icon is close enough, return it */
2280 if (!aicon && ex_x == 0 && ex_y >= 0 && ex_y <= max_y_icons) {
2281 *ret_x = 0;
2282 *ret_y = ex_y;
2283 return True;
2286 } else { /* CLIP */
2287 int neighbours = 0;
2288 int start, stop, k;
2290 start = icon->omnipresent ? 0 : scr->current_workspace;
2291 stop = icon->omnipresent ? scr->workspace_count : start + 1;
2293 aicon = NULL;
2294 for (k = start; k < stop; k++) {
2295 WDock *tmp = scr->workspaces[k]->clip;
2296 if (!tmp)
2297 continue;
2298 for (i = 0; i < tmp->max_icons; i++) {
2299 nicon = tmp->icon_array[i];
2300 if (nicon && nicon->xindex == ex_x && nicon->yindex == ex_y) {
2301 aicon = nicon;
2302 break;
2305 if (aicon)
2306 break;
2308 for (k = start; k < stop; k++) {
2309 WDock *tmp = scr->workspaces[k]->clip;
2310 if (!tmp)
2311 continue;
2312 for (i = 0; i < tmp->max_icons; i++) {
2313 nicon = tmp->icon_array[i];
2314 if (nicon && nicon != icon && /* Icon can't be it's own neighbour */
2315 (abs(nicon->xindex - ex_x) <= CLIP_ATTACH_VICINITY &&
2316 abs(nicon->yindex - ex_y) <= CLIP_ATTACH_VICINITY)) {
2317 neighbours = 1;
2318 break;
2321 if (neighbours)
2322 break;
2325 if (neighbours && (aicon == NULL || (redocking && aicon == icon))) {
2326 *ret_x = ex_x;
2327 *ret_y = ex_y;
2328 return True;
2331 return False;
2334 static int onScreen(WScreen *scr, int x, int y, int sx, int ex, int sy, int ey)
2336 WMRect rect = wmkrect(x, y, ICON_SIZE, ICON_SIZE);
2337 int flags;
2339 wGetRectPlacementInfo(scr, rect, &flags);
2341 return !(flags & (XFLAG_DEAD | XFLAG_PARTIAL));
2345 * returns true if it can find a free slot in the dock,
2346 * in which case it changes x_pos and y_pos accordingly.
2347 * Else returns false.
2349 Bool wDockFindFreeSlot(WDock *dock, int *x_pos, int *y_pos)
2351 WScreen *scr = dock->screen_ptr;
2352 WAppIcon *btn;
2353 WAppIconChain *chain;
2354 unsigned char *slot_map;
2355 int mwidth;
2356 int r;
2357 int x, y;
2358 int i, done = False;
2359 int corner;
2360 int sx = 0, sy = 0, ex = scr->scr_width, ey = scr->scr_height;
2361 int extra_count = 0;
2363 if (dock->type == WM_CLIP && dock != scr->workspaces[scr->current_workspace]->clip)
2364 extra_count = scr->global_icon_count;
2366 /* if the dock is full */
2367 if (dock->icon_count + extra_count >= dock->max_icons)
2368 return False;
2370 if (!wPreferences.flags.nodock && scr->dock) {
2371 if (scr->dock->on_right_side)
2372 ex -= ICON_SIZE + DOCK_EXTRA_SPACE;
2373 else
2374 sx += ICON_SIZE + DOCK_EXTRA_SPACE;
2377 if (ex < dock->x_pos)
2378 ex = dock->x_pos;
2379 if (sx > dock->x_pos + ICON_SIZE)
2380 sx = dock->x_pos + ICON_SIZE;
2381 #define C_NONE 0
2382 #define C_NW 1
2383 #define C_NE 2
2384 #define C_SW 3
2385 #define C_SE 4
2387 /* check if clip is in a corner */
2388 if (dock->type == WM_CLIP) {
2389 if (dock->x_pos < 1 && dock->y_pos < 1)
2390 corner = C_NE;
2391 else if (dock->x_pos < 1 && dock->y_pos >= (ey - ICON_SIZE))
2392 corner = C_SE;
2393 else if (dock->x_pos >= (ex - ICON_SIZE) && dock->y_pos >= (ey - ICON_SIZE))
2394 corner = C_SW;
2395 else if (dock->x_pos >= (ex - ICON_SIZE) && dock->y_pos < 1)
2396 corner = C_NW;
2397 else
2398 corner = C_NONE;
2399 } else {
2400 corner = C_NONE;
2403 /* If the clip is in the corner, use only slots that are in the border
2404 * of the screen */
2405 if (corner != C_NONE) {
2406 char *hmap, *vmap;
2407 int hcount, vcount;
2409 hcount = WMIN(dock->max_icons, scr->scr_width / ICON_SIZE);
2410 vcount = WMIN(dock->max_icons, scr->scr_height / ICON_SIZE);
2411 hmap = wmalloc(hcount + 1);
2412 vmap = wmalloc(vcount + 1);
2414 /* mark used positions */
2415 switch (corner) {
2416 case C_NE:
2417 for (i = 0; i < dock->max_icons; i++) {
2418 btn = dock->icon_array[i];
2419 if (!btn)
2420 continue;
2422 if (btn->xindex == 0 && btn->yindex > 0 && btn->yindex < vcount)
2423 vmap[btn->yindex] = 1;
2424 else if (btn->yindex == 0 && btn->xindex > 0 && btn->xindex < hcount)
2425 hmap[btn->xindex] = 1;
2427 for (chain = scr->global_icons; chain != NULL; chain = chain->next) {
2428 btn = chain->aicon;
2429 if (btn->xindex == 0 && btn->yindex > 0 && btn->yindex < vcount)
2430 vmap[btn->yindex] = 1;
2431 else if (btn->yindex == 0 && btn->xindex > 0 && btn->xindex < hcount)
2432 hmap[btn->xindex] = 1;
2434 break;
2435 case C_NW:
2436 for (i = 0; i < dock->max_icons; i++) {
2437 btn = dock->icon_array[i];
2438 if (!btn)
2439 continue;
2441 if (btn->xindex == 0 && btn->yindex > 0 && btn->yindex < vcount)
2442 vmap[btn->yindex] = 1;
2443 else if (btn->yindex == 0 && btn->xindex < 0 && btn->xindex > -hcount)
2444 hmap[-btn->xindex] = 1;
2446 for (chain = scr->global_icons; chain != NULL; chain = chain->next) {
2447 btn = chain->aicon;
2448 if (btn->xindex == 0 && btn->yindex > 0 && btn->yindex < vcount)
2449 vmap[btn->yindex] = 1;
2450 else if (btn->yindex == 0 && btn->xindex < 0 && btn->xindex > -hcount)
2451 hmap[-btn->xindex] = 1;
2453 break;
2454 case C_SE:
2455 for (i = 0; i < dock->max_icons; i++) {
2456 btn = dock->icon_array[i];
2457 if (!btn)
2458 continue;
2460 if (btn->xindex == 0 && btn->yindex < 0 && btn->yindex > -vcount)
2461 vmap[-btn->yindex] = 1;
2462 else if (btn->yindex == 0 && btn->xindex > 0 && btn->xindex < hcount)
2463 hmap[btn->xindex] = 1;
2465 for (chain = scr->global_icons; chain != NULL; chain = chain->next) {
2466 btn = chain->aicon;
2467 if (btn->xindex == 0 && btn->yindex < 0 && btn->yindex > -vcount)
2468 vmap[-btn->yindex] = 1;
2469 else if (btn->yindex == 0 && btn->xindex > 0 && btn->xindex < hcount)
2470 hmap[btn->xindex] = 1;
2472 break;
2473 case C_SW:
2474 default:
2475 for (i = 0; i < dock->max_icons; i++) {
2476 btn = dock->icon_array[i];
2477 if (!btn)
2478 continue;
2480 if (btn->xindex == 0 && btn->yindex < 0 && btn->yindex > -vcount)
2481 vmap[-btn->yindex] = 1;
2482 else if (btn->yindex == 0 && btn->xindex < 0 && btn->xindex > -hcount)
2483 hmap[-btn->xindex] = 1;
2485 for (chain = scr->global_icons; chain != NULL; chain = chain->next) {
2486 btn = chain->aicon;
2487 if (btn->xindex == 0 && btn->yindex < 0 && btn->yindex > -vcount)
2488 vmap[-btn->yindex] = 1;
2489 else if (btn->yindex == 0 && btn->xindex < 0 && btn->xindex > -hcount)
2490 hmap[-btn->xindex] = 1;
2493 x = 0;
2494 y = 0;
2495 done = 0;
2496 /* search a vacant slot */
2497 for (i = 1; i < WMAX(vcount, hcount); i++) {
2498 if (i < vcount && vmap[i] == 0) {
2499 /* found a slot */
2500 x = 0;
2501 y = i;
2502 done = 1;
2503 break;
2504 } else if (i < hcount && hmap[i] == 0) {
2505 /* found a slot */
2506 x = i;
2507 y = 0;
2508 done = 1;
2509 break;
2512 wfree(vmap);
2513 wfree(hmap);
2514 /* If found a slot, translate and return */
2515 if (done) {
2516 if (corner == C_NW || corner == C_NE)
2517 *y_pos = y;
2518 else
2519 *y_pos = -y;
2521 if (corner == C_NE || corner == C_SE)
2522 *x_pos = x;
2523 else
2524 *x_pos = -x;
2526 return True;
2528 /* else, try to find a slot somewhere else */
2531 /* a map of mwidth x mwidth would be enough if we allowed icons to be
2532 * placed outside of screen */
2533 mwidth = (int)ceil(sqrt(dock->max_icons));
2535 /* In the worst case (the clip is in the corner of the screen),
2536 * the amount of icons that fit in the clip is smaller.
2537 * Double the map to get a safe value.
2539 mwidth += mwidth;
2541 r = (mwidth - 1) / 2;
2543 slot_map = wmalloc(mwidth * mwidth);
2545 #define XY2OFS(x,y) (WMAX(abs(x),abs(y)) > r) ? 0 : (((y)+r)*(mwidth)+(x)+r)
2547 /* mark used slots in the map. If the slot falls outside the map
2548 * (for example, when all icons are placed in line), ignore them. */
2549 for (i = 0; i < dock->max_icons; i++) {
2550 btn = dock->icon_array[i];
2551 if (btn)
2552 slot_map[XY2OFS(btn->xindex, btn->yindex)] = 1;
2555 for (chain = scr->global_icons; chain != NULL; chain = chain->next)
2556 slot_map[XY2OFS(chain->aicon->xindex, chain->aicon->yindex)] = 1;
2558 /* Find closest slot from the center that is free by scanning the
2559 * map from the center to outward in circular passes.
2560 * This will not result in a neat layout, but will be optimal
2561 * in the sense that there will not be holes left.
2563 done = 0;
2564 for (i = 1; i <= r && !done; i++) {
2565 int tx, ty;
2567 /* top and bottom parts of the ring */
2568 for (x = -i; x <= i && !done; x++) {
2569 tx = dock->x_pos + x * ICON_SIZE;
2570 y = -i;
2571 ty = dock->y_pos + y * ICON_SIZE;
2572 if (slot_map[XY2OFS(x, y)] == 0 && onScreen(scr, tx, ty, sx, ex, sy, ey)) {
2573 *x_pos = x;
2574 *y_pos = y;
2575 done = 1;
2576 break;
2578 y = i;
2579 ty = dock->y_pos + y * ICON_SIZE;
2580 if (slot_map[XY2OFS(x, y)] == 0 && onScreen(scr, tx, ty, sx, ex, sy, ey)) {
2581 *x_pos = x;
2582 *y_pos = y;
2583 done = 1;
2584 break;
2587 /* left and right parts of the ring */
2588 for (y = -i + 1; y <= i - 1; y++) {
2589 ty = dock->y_pos + y * ICON_SIZE;
2590 x = -i;
2591 tx = dock->x_pos + x * ICON_SIZE;
2592 if (slot_map[XY2OFS(x, y)] == 0 && onScreen(scr, tx, ty, sx, ex, sy, ey)) {
2593 *x_pos = x;
2594 *y_pos = y;
2595 done = 1;
2596 break;
2598 x = i;
2599 tx = dock->x_pos + x * ICON_SIZE;
2600 if (slot_map[XY2OFS(x, y)] == 0 && onScreen(scr, tx, ty, sx, ex, sy, ey)) {
2601 *x_pos = x;
2602 *y_pos = y;
2603 done = 1;
2604 break;
2608 wfree(slot_map);
2609 #undef XY2OFS
2610 return done;
2613 static void moveDock(WDock *dock, int new_x, int new_y)
2615 WAppIcon *btn;
2616 int i;
2618 dock->x_pos = new_x;
2619 dock->y_pos = new_y;
2620 for (i = 0; i < dock->max_icons; i++) {
2621 btn = dock->icon_array[i];
2622 if (btn) {
2623 btn->x_pos = new_x + btn->xindex * ICON_SIZE;
2624 btn->y_pos = new_y + btn->yindex * ICON_SIZE;
2625 XMoveWindow(dpy, btn->icon->core->window, btn->x_pos, btn->y_pos);
2630 static void swapDock(WDock *dock)
2632 WScreen *scr = dock->screen_ptr;
2633 WAppIcon *btn;
2634 int x, i;
2636 if (dock->on_right_side)
2637 x = dock->x_pos = scr->scr_width - ICON_SIZE - DOCK_EXTRA_SPACE;
2638 else
2639 x = dock->x_pos = DOCK_EXTRA_SPACE;
2641 for (i = 0; i < dock->max_icons; i++) {
2642 btn = dock->icon_array[i];
2643 if (btn) {
2644 btn->x_pos = x;
2645 XMoveWindow(dpy, btn->icon->core->window, btn->x_pos, btn->y_pos);
2649 wScreenUpdateUsableArea(scr);
2652 static pid_t execCommand(WAppIcon *btn, char *command, WSavedState *state)
2654 WScreen *scr = btn->icon->core->screen_ptr;
2655 pid_t pid;
2656 char **argv;
2657 int argc;
2658 char *cmdline;
2660 cmdline = ExpandOptions(scr, command);
2662 if (scr->flags.dnd_data_convertion_status || !cmdline) {
2663 if (cmdline)
2664 wfree(cmdline);
2665 if (state)
2666 wfree(state);
2667 return 0;
2670 wtokensplit(cmdline, &argv, &argc);
2672 if (!argc) {
2673 if (cmdline)
2674 wfree(cmdline);
2675 if (state)
2676 wfree(state);
2677 return 0;
2680 if ((pid = fork()) == 0) {
2681 char **args;
2682 int i;
2684 SetupEnvironment(scr);
2686 #ifdef HAVE_SETSID
2687 setsid();
2688 #endif
2690 args = malloc(sizeof(char *) * (argc + 1));
2691 if (!args)
2692 exit(111);
2694 for (i = 0; i < argc; i++)
2695 args[i] = argv[i];
2697 args[argc] = NULL;
2698 execvp(argv[0], args);
2699 exit(111);
2701 wtokenfree(argv, argc);
2703 if (pid > 0) {
2704 if (!state) {
2705 state = wmalloc(sizeof(WSavedState));
2706 state->hidden = -1;
2707 state->miniaturized = -1;
2708 state->shaded = -1;
2709 if (btn->dock == scr->dock || btn->omnipresent)
2710 state->workspace = -1;
2711 else
2712 state->workspace = scr->current_workspace;
2714 wWindowAddSavedState(btn->wm_instance, btn->wm_class, cmdline, pid, state);
2715 wAddDeathHandler(pid, (WDeathHandler *) trackDeadProcess, btn->dock);
2716 } else if (state) {
2717 wfree(state);
2719 wfree(cmdline);
2720 return pid;
2723 void wDockHideIcons(WDock *dock)
2725 int i;
2727 if (dock == NULL)
2728 return;
2730 for (i = 1; i < dock->max_icons; i++) {
2731 if (dock->icon_array[i])
2732 XUnmapWindow(dpy, dock->icon_array[i]->icon->core->window);
2734 dock->mapped = 0;
2736 dockIconPaint(dock->icon_array[0]);
2739 void wDockShowIcons(WDock *dock)
2741 int i, newlevel;
2742 WAppIcon *btn;
2744 if (dock == NULL)
2745 return;
2747 btn = dock->icon_array[0];
2748 moveDock(dock, btn->x_pos, btn->y_pos);
2750 newlevel = dock->lowered ? WMNormalLevel : WMDockLevel;
2751 ChangeStackingLevel(btn->icon->core, newlevel);
2753 for (i = 1; i < dock->max_icons; i++) {
2754 if (dock->icon_array[i]) {
2755 MoveInStackListAbove(dock->icon_array[i]->icon->core, btn->icon->core);
2756 break;
2760 if (!dock->collapsed) {
2761 for (i = 1; i < dock->max_icons; i++) {
2762 if (dock->icon_array[i])
2763 XMapWindow(dpy, dock->icon_array[i]->icon->core->window);
2766 dock->mapped = 1;
2768 dockIconPaint(btn);
2771 void wDockLower(WDock *dock)
2773 int i;
2775 for (i = 0; i < dock->max_icons; i++) {
2776 if (dock->icon_array[i])
2777 wLowerFrame(dock->icon_array[i]->icon->core);
2781 void wDockRaise(WDock *dock)
2783 int i;
2785 for (i = dock->max_icons - 1; i >= 0; i--) {
2786 if (dock->icon_array[i])
2787 wRaiseFrame(dock->icon_array[i]->icon->core);
2791 void wDockRaiseLower(WDock *dock)
2793 if (!dock->icon_array[0]->icon->core->stacking->above
2794 || (dock->icon_array[0]->icon->core->stacking->window_level
2795 != dock->icon_array[0]->icon->core->stacking->above->stacking->window_level))
2796 wDockLower(dock);
2797 else
2798 wDockRaise(dock);
2801 void wDockFinishLaunch(WDock *dock, WAppIcon *icon)
2803 icon->launching = 0;
2804 icon->relaunching = 0;
2805 dockIconPaint(icon);
2808 WAppIcon *wDockFindIconForWindow(WDock *dock, Window window)
2810 WAppIcon *icon;
2811 int i;
2813 for (i = 0; i < dock->max_icons; i++) {
2814 icon = dock->icon_array[i];
2815 if (icon && icon->main_window == window)
2816 return icon;
2818 return NULL;
2821 void wDockTrackWindowLaunch(WDock *dock, Window window)
2823 WAppIcon *icon;
2824 char *wm_class, *wm_instance;
2825 int i;
2826 Bool firstPass = True;
2827 Bool found = False;
2828 char *command = NULL;
2830 if (!PropGetWMClass(window, &wm_class, &wm_instance) || (!wm_class && !wm_instance))
2831 return;
2833 command = GetCommandForWindow(window);
2834 retry:
2835 for (i = 0; i < dock->max_icons; i++) {
2836 icon = dock->icon_array[i];
2837 if (!icon)
2838 continue;
2840 /* app is already attached to icon */
2841 if (icon->main_window == window) {
2842 found = True;
2843 break;
2846 if ((icon->wm_instance || icon->wm_class)
2847 && (icon->launching || !icon->running)) {
2849 if (icon->wm_instance && wm_instance && strcmp(icon->wm_instance, wm_instance) != 0)
2850 continue;
2852 if (icon->wm_class && wm_class && strcmp(icon->wm_class, wm_class) != 0)
2853 continue;
2855 if (firstPass && command && strcmp(icon->command, command) != 0)
2856 continue;
2858 if (!icon->relaunching) {
2859 WApplication *wapp;
2861 /* Possibly an application that was docked with dockit,
2862 * but the user did not update WMState to indicate that
2863 * it was docked by force */
2864 wapp = wApplicationOf(window);
2865 if (!wapp) {
2866 icon->forced_dock = 1;
2867 icon->running = 0;
2869 if (!icon->forced_dock)
2870 icon->main_window = window;
2872 found = True;
2873 if (!wPreferences.no_animations && !icon->launching &&
2874 !dock->screen_ptr->flags.startup && !dock->collapsed) {
2875 WAppIcon *aicon;
2876 int x0, y0;
2878 icon->launching = 1;
2879 dockIconPaint(icon);
2881 aicon = wAppIconCreateForDock(dock->screen_ptr, NULL,
2882 wm_instance, wm_class, TILE_NORMAL);
2883 /* XXX: can: aicon->icon == NULL ? */
2884 PlaceIcon(dock->screen_ptr, &x0, &y0, wGetHeadForWindow(aicon->icon->owner));
2885 wAppIconMove(aicon, x0, y0);
2886 /* Should this always be lowered? -Dan */
2887 if (dock->lowered)
2888 wLowerFrame(aicon->icon->core);
2889 XMapWindow(dpy, aicon->icon->core->window);
2890 aicon->launching = 1;
2891 wAppIconPaint(aicon);
2892 SlideWindow(aicon->icon->core->window, x0, y0, icon->x_pos, icon->y_pos);
2893 XUnmapWindow(dpy, aicon->icon->core->window);
2894 wAppIconDestroy(aicon);
2896 wDockFinishLaunch(dock, icon);
2897 break;
2901 if (firstPass && !found) {
2902 firstPass = False;
2903 goto retry;
2906 if (command)
2907 wfree(command);
2909 if (wm_class)
2910 free(wm_class);
2911 if (wm_instance)
2912 free(wm_instance);
2915 void wClipUpdateForWorkspaceChange(WScreen *scr, int workspace)
2917 if (!wPreferences.flags.noclip) {
2918 scr->clip_icon->dock = scr->workspaces[workspace]->clip;
2919 if (scr->current_workspace != workspace) {
2920 WDock *old_clip = scr->workspaces[scr->current_workspace]->clip;
2921 WAppIconChain *chain = scr->global_icons;
2923 while (chain) {
2924 moveIconBetweenDocks(chain->aicon->dock,
2925 scr->workspaces[workspace]->clip,
2926 chain->aicon, chain->aicon->xindex, chain->aicon->yindex);
2927 if (scr->workspaces[workspace]->clip->collapsed)
2928 XUnmapWindow(dpy, chain->aicon->icon->core->window);
2929 chain = chain->next;
2932 wDockHideIcons(old_clip);
2933 if (old_clip->auto_raise_lower) {
2934 if (old_clip->auto_raise_magic) {
2935 WMDeleteTimerHandler(old_clip->auto_raise_magic);
2936 old_clip->auto_raise_magic = NULL;
2938 wDockLower(old_clip);
2940 if (old_clip->auto_collapse) {
2941 if (old_clip->auto_expand_magic) {
2942 WMDeleteTimerHandler(old_clip->auto_expand_magic);
2943 old_clip->auto_expand_magic = NULL;
2945 old_clip->collapsed = 1;
2947 wDockShowIcons(scr->workspaces[workspace]->clip);
2952 static void trackDeadProcess(pid_t pid, unsigned char status, WDock *dock)
2954 WAppIcon *icon;
2955 int i;
2957 for (i = 0; i < dock->max_icons; i++) {
2958 icon = dock->icon_array[i];
2959 if (!icon)
2960 continue;
2962 if (icon->launching && icon->pid == pid) {
2963 if (!icon->relaunching) {
2964 icon->running = 0;
2965 icon->main_window = None;
2967 wDockFinishLaunch(dock, icon);
2968 icon->pid = 0;
2969 if (status == 111) {
2970 char msg[PATH_MAX];
2971 char *cmd;
2973 #ifdef XDND
2974 if (icon->drop_launch)
2975 cmd = icon->dnd_command;
2976 else
2977 #endif
2978 if (icon->paste_launch)
2979 cmd = icon->paste_command;
2980 else
2981 cmd = icon->command;
2983 snprintf(msg, sizeof(msg), _("Could not execute command \"%s\""), cmd);
2985 wMessageDialog(dock->screen_ptr, _("Error"), msg, _("OK"), NULL, NULL);
2987 break;
2992 static void toggleLowered(WDock *dock)
2994 WAppIcon *tmp;
2995 int newlevel, i;
2997 /* lower/raise Dock */
2998 if (!dock->lowered) {
2999 newlevel = WMNormalLevel;
3000 dock->lowered = 1;
3001 } else {
3002 newlevel = WMDockLevel;
3003 dock->lowered = 0;
3006 for (i = 0; i < dock->max_icons; i++) {
3007 tmp = dock->icon_array[i];
3008 if (!tmp)
3009 continue;
3011 ChangeStackingLevel(tmp->icon->core, newlevel);
3012 if (dock->lowered)
3013 wLowerFrame(tmp->icon->core);
3016 if (dock->type == WM_DOCK)
3017 wScreenUpdateUsableArea(dock->screen_ptr);
3020 static void toggleCollapsed(WDock *dock)
3022 if (dock->collapsed) {
3023 dock->collapsed = 0;
3024 wDockShowIcons(dock);
3025 } else {
3026 dock->collapsed = 1;
3027 wDockHideIcons(dock);
3031 static void openDockMenu(WDock *dock, WAppIcon *aicon, XEvent *event)
3033 WScreen *scr = dock->screen_ptr;
3034 WObjDescriptor *desc;
3035 WMenuEntry *entry;
3036 WApplication *wapp = NULL;
3037 int index = 0;
3038 int x_pos;
3039 int n_selected;
3040 int appIsRunning = aicon->running && aicon->icon && aicon->icon->owner;
3042 if (dock->type == WM_DOCK) {
3043 /* keep on top */
3044 entry = dock->menu->entries[index];
3045 entry->flags.indicator_on = !dock->lowered;
3046 entry->clientdata = dock;
3047 dock->menu->flags.realized = 0;
3048 } else {
3049 /* clip options */
3050 if (scr->clip_options)
3051 updateClipOptionsMenu(scr->clip_options, dock);
3053 n_selected = numberOfSelectedIcons(dock);
3055 /* Rename Workspace */
3056 entry = dock->menu->entries[++index];
3057 if (aicon == scr->clip_icon) {
3058 entry->callback = renameCallback;
3059 entry->clientdata = dock;
3060 entry->flags.indicator = 0;
3061 entry->text = _("Rename Workspace");
3062 } else {
3063 entry->callback = omnipresentCallback;
3064 entry->clientdata = aicon;
3065 if (n_selected > 0) {
3066 entry->flags.indicator = 0;
3067 entry->text = _("Toggle Omnipresent");
3068 } else {
3069 entry->flags.indicator = 1;
3070 entry->flags.indicator_on = aicon->omnipresent;
3071 entry->flags.indicator_type = MI_CHECK;
3072 entry->text = _("Omnipresent");
3076 /* select/unselect icon */
3077 entry = dock->menu->entries[++index];
3078 entry->clientdata = aicon;
3079 entry->flags.indicator_on = aicon->icon->selected;
3080 wMenuSetEnabled(dock->menu, index, aicon != scr->clip_icon);
3082 /* select/unselect all icons */
3083 entry = dock->menu->entries[++index];
3084 entry->clientdata = aicon;
3085 if (n_selected > 0)
3086 entry->text = _("Unselect All Icons");
3087 else
3088 entry->text = _("Select All Icons");
3090 wMenuSetEnabled(dock->menu, index, dock->icon_count > 1);
3092 /* keep icon(s) */
3093 entry = dock->menu->entries[++index];
3094 entry->clientdata = aicon;
3095 if (n_selected > 1)
3096 entry->text = _("Keep Icons");
3097 else
3098 entry->text = _("Keep Icon");
3100 wMenuSetEnabled(dock->menu, index, dock->icon_count > 1);
3102 /* this is the workspace submenu part */
3103 entry = dock->menu->entries[++index];
3104 if (n_selected > 1)
3105 entry->text = _("Move Icons To");
3106 else
3107 entry->text = _("Move Icon To");
3109 if (scr->clip_submenu)
3110 updateWorkspaceMenu(scr->clip_submenu, aicon);
3112 wMenuSetEnabled(dock->menu, index, !aicon->omnipresent);
3114 /* remove icon(s) */
3115 entry = dock->menu->entries[++index];
3116 entry->clientdata = aicon;
3117 if (n_selected > 1)
3118 entry->text = _("Remove Icons");
3119 else
3120 entry->text = _("Remove Icon");
3122 wMenuSetEnabled(dock->menu, index, dock->icon_count > 1);
3124 /* attract icon(s) */
3125 entry = dock->menu->entries[++index];
3126 entry->clientdata = aicon;
3128 dock->menu->flags.realized = 0;
3129 wMenuRealize(dock->menu);
3132 if (aicon->icon->owner)
3133 wapp = wApplicationOf(aicon->icon->owner->main_window);
3134 else
3135 wapp = NULL;
3137 /* launch */
3138 entry = dock->menu->entries[++index];
3139 entry->clientdata = aicon;
3140 wMenuSetEnabled(dock->menu, index, aicon->command != NULL);
3142 /* unhide here */
3143 entry = dock->menu->entries[++index];
3144 entry->clientdata = aicon;
3145 if (wapp && wapp->flags.hidden)
3146 entry->text = _("Unhide Here");
3147 else
3148 entry->text = _("Bring Here");
3150 wMenuSetEnabled(dock->menu, index, appIsRunning);
3152 /* hide */
3153 entry = dock->menu->entries[++index];
3154 entry->clientdata = aicon;
3155 if (wapp && wapp->flags.hidden)
3156 entry->text = _("Unhide");
3157 else
3158 entry->text = _("Hide");
3160 wMenuSetEnabled(dock->menu, index, appIsRunning);
3162 /* settings */
3163 entry = dock->menu->entries[++index];
3164 entry->clientdata = aicon;
3165 wMenuSetEnabled(dock->menu, index, !aicon->editing && !wPreferences.flags.noupdates);
3167 /* kill */
3168 entry = dock->menu->entries[++index];
3169 entry->clientdata = aicon;
3170 wMenuSetEnabled(dock->menu, index, appIsRunning);
3172 if (!dock->menu->flags.realized)
3173 wMenuRealize(dock->menu);
3175 if (dock->type == WM_CLIP) {
3176 /*x_pos = event->xbutton.x_root+2; */
3177 x_pos = event->xbutton.x_root - dock->menu->frame->core->width / 2 - 1;
3178 if (x_pos < 0) {
3179 x_pos = 0;
3180 } else if (x_pos + dock->menu->frame->core->width > scr->scr_width - 2) {
3181 x_pos = scr->scr_width - dock->menu->frame->core->width - 4;
3183 } else {
3184 x_pos = dock->on_right_side ? scr->scr_width - dock->menu->frame->core->width - 3 : 0;
3187 wMenuMapAt(dock->menu, x_pos, event->xbutton.y_root + 2, False);
3189 /* allow drag select */
3190 event->xany.send_event = True;
3191 desc = &dock->menu->menu->descriptor;
3192 (*desc->handle_mousedown) (desc, event);
3195 /******************************************************************/
3196 static void iconDblClick(WObjDescriptor *desc, XEvent *event)
3198 WAppIcon *btn = desc->parent;
3199 WDock *dock = btn->dock;
3200 WApplication *wapp = NULL;
3201 int unhideHere = 0;
3203 if (btn->icon->owner && !(event->xbutton.state & ControlMask)) {
3204 wapp = wApplicationOf(btn->icon->owner->main_window);
3206 assert(wapp != NULL);
3208 unhideHere = (event->xbutton.state & ShiftMask);
3210 /* go to the last workspace that the user worked on the app */
3211 if (wapp->last_workspace != dock->screen_ptr->current_workspace && !unhideHere)
3212 wWorkspaceChange(dock->screen_ptr, wapp->last_workspace);
3214 wUnhideApplication(wapp, event->xbutton.button == Button2, unhideHere);
3216 if (event->xbutton.state & MOD_MASK)
3217 wHideOtherApplications(btn->icon->owner);
3218 } else {
3219 if (event->xbutton.button == Button1) {
3220 if (event->xbutton.state & MOD_MASK) {
3221 /* raise/lower dock */
3222 toggleLowered(dock);
3223 } else if (btn == dock->screen_ptr->clip_icon) {
3224 if (getClipButton(event->xbutton.x, event->xbutton.y) == CLIP_IDLE)
3225 toggleCollapsed(dock);
3226 else
3227 handleClipChangeWorkspace(dock->screen_ptr, event);
3228 } else if (btn->command) {
3229 if (!btn->launching && (!btn->running || (event->xbutton.state & ControlMask)))
3230 launchDockedApplication(btn, False);
3236 static void handleDockMove(WDock *dock, WAppIcon *aicon, XEvent *event)
3238 WScreen *scr = dock->screen_ptr;
3239 int ofs_x = event->xbutton.x, ofs_y = event->xbutton.y;
3240 int x, y;
3241 XEvent ev;
3242 int grabbed = 0, swapped = 0, done;
3243 Pixmap ghost = None;
3244 int superfluous = wPreferences.superfluous; /* we catch it to avoid problems */
3246 if (XGrabPointer(dpy, aicon->icon->core->window, True, ButtonMotionMask
3247 | ButtonReleaseMask | ButtonPressMask, GrabModeAsync,
3248 GrabModeAsync, None, None, CurrentTime) != GrabSuccess)
3249 wwarning("pointer grab failed for dock move");
3251 y = 0;
3252 for (x = 0; x < dock->max_icons; x++) {
3253 if (dock->icon_array[x] != NULL && dock->icon_array[x]->yindex > y)
3254 y = dock->icon_array[x]->yindex;
3256 y++;
3257 XResizeWindow(dpy, scr->dock_shadow, ICON_SIZE, ICON_SIZE * y);
3259 done = 0;
3260 while (!done) {
3261 WMMaskEvent(dpy, PointerMotionMask | ButtonReleaseMask | ButtonPressMask
3262 | ButtonMotionMask | ExposureMask | EnterWindowMask, &ev);
3263 switch (ev.type) {
3264 case Expose:
3265 WMHandleEvent(&ev);
3266 break;
3268 case EnterNotify:
3269 /* It means the cursor moved so fast that it entered
3270 * something else (if moving slowly, it would have
3271 * stayed in the dock that is being moved. Ignore such
3272 * "spurious" EnterNotifiy's */
3273 break;
3275 case MotionNotify:
3276 if (!grabbed) {
3277 if (abs(ofs_x - ev.xmotion.x) >= MOVE_THRESHOLD
3278 || abs(ofs_y - ev.xmotion.y) >= MOVE_THRESHOLD) {
3279 XChangeActivePointerGrab(dpy, ButtonMotionMask
3280 | ButtonReleaseMask | ButtonPressMask,
3281 wCursor[WCUR_MOVE], CurrentTime);
3282 grabbed = 1;
3284 break;
3286 if (dock->type == WM_CLIP) {
3287 x = ev.xmotion.x_root - ofs_x;
3288 y = ev.xmotion.y_root - ofs_y;
3289 wScreenKeepInside(scr, &x, &y, ICON_SIZE, ICON_SIZE);
3291 moveDock(dock, x, y);
3292 } else {
3293 /* move vertically if pointer is inside the dock */
3294 if ((dock->on_right_side && ev.xmotion.x_root >= dock->x_pos - ICON_SIZE)
3295 || (!dock->on_right_side && ev.xmotion.x_root <= dock->x_pos + ICON_SIZE * 2)) {
3297 x = ev.xmotion.x_root - ofs_x;
3298 y = ev.xmotion.y_root - ofs_y;
3299 wScreenKeepInside(scr, &x, &y, ICON_SIZE, ICON_SIZE);
3300 moveDock(dock, dock->x_pos, y);
3302 /* move horizontally to change sides */
3303 x = ev.xmotion.x_root - ofs_x;
3304 if (!dock->on_right_side) {
3306 /* is on left */
3307 if (ev.xmotion.x_root > dock->x_pos + ICON_SIZE * 2) {
3308 XMoveWindow(dpy, scr->dock_shadow, scr->scr_width - ICON_SIZE
3309 - DOCK_EXTRA_SPACE - 1, dock->y_pos);
3310 if (superfluous && ghost == None) {
3311 ghost = MakeGhostDock(dock, dock->x_pos,
3312 scr->scr_width - ICON_SIZE
3313 - DOCK_EXTRA_SPACE - 1, dock->y_pos);
3314 XSetWindowBackgroundPixmap(dpy, scr->dock_shadow, ghost);
3315 XClearWindow(dpy, scr->dock_shadow);
3317 XMapRaised(dpy, scr->dock_shadow);
3318 swapped = 1;
3319 } else {
3320 if (superfluous && ghost != None) {
3321 XFreePixmap(dpy, ghost);
3322 ghost = None;
3324 XUnmapWindow(dpy, scr->dock_shadow);
3325 swapped = 0;
3327 } else {
3328 /* is on right */
3329 if (ev.xmotion.x_root < dock->x_pos - ICON_SIZE) {
3330 XMoveWindow(dpy, scr->dock_shadow, DOCK_EXTRA_SPACE, dock->y_pos);
3331 if (superfluous && ghost == None) {
3332 ghost = MakeGhostDock(dock, dock->x_pos,
3333 DOCK_EXTRA_SPACE, dock->y_pos);
3334 XSetWindowBackgroundPixmap(dpy, scr->dock_shadow, ghost);
3335 XClearWindow(dpy, scr->dock_shadow);
3337 XMapRaised(dpy, scr->dock_shadow);
3338 swapped = -1;
3339 } else {
3340 XUnmapWindow(dpy, scr->dock_shadow);
3341 swapped = 0;
3342 if (superfluous && ghost != None) {
3343 XFreePixmap(dpy, ghost);
3344 ghost = None;
3349 break;
3351 case ButtonPress:
3352 break;
3354 case ButtonRelease:
3355 if (ev.xbutton.button != event->xbutton.button)
3356 break;
3357 XUngrabPointer(dpy, CurrentTime);
3358 XUnmapWindow(dpy, scr->dock_shadow);
3359 XResizeWindow(dpy, scr->dock_shadow, ICON_SIZE, ICON_SIZE);
3360 if (dock->type == WM_DOCK) {
3361 if (swapped != 0) {
3362 if (swapped > 0)
3363 dock->on_right_side = 1;
3364 else
3365 dock->on_right_side = 0;
3366 swapDock(dock);
3367 wArrangeIcons(scr, False);
3370 done = 1;
3371 break;
3374 if (superfluous) {
3375 if (ghost != None)
3376 XFreePixmap(dpy, ghost);
3377 XSetWindowBackground(dpy, scr->dock_shadow, scr->white_pixel);
3381 static Bool handleIconMove(WDock *dock, WAppIcon *aicon, XEvent *event)
3383 WScreen *scr = dock->screen_ptr;
3384 Window wins[2];
3385 WIcon *icon = aicon->icon;
3386 WDock *dock2 = NULL, *last_dock = dock, *clip = NULL;
3387 int ondock, grabbed = 0, change_dock = 0, collapsed = 0;
3388 XEvent ev;
3389 int x = aicon->x_pos, y = aicon->y_pos;
3390 int ofs_x = event->xbutton.x, ofs_y = event->xbutton.y;
3391 int shad_x = x, shad_y = y;
3392 int ix = aicon->xindex, iy = aicon->yindex;
3393 int tmp;
3394 Pixmap ghost = None;
3395 Bool docked;
3396 int superfluous = wPreferences.superfluous; /* we catch it to avoid problems */
3397 int omnipresent = aicon->omnipresent; /* this must be cached!!! */
3398 Bool hasMoved = False;
3400 if (wPreferences.flags.noupdates)
3401 return hasMoved;
3403 if (XGrabPointer(dpy, icon->core->window, True, ButtonMotionMask
3404 | ButtonReleaseMask | ButtonPressMask, GrabModeAsync,
3405 GrabModeAsync, None, None, CurrentTime) != GrabSuccess) {
3408 if (!(event->xbutton.state & MOD_MASK))
3409 wRaiseFrame(icon->core);
3411 if (!wPreferences.flags.noclip)
3412 clip = scr->workspaces[scr->current_workspace]->clip;
3414 if (dock == scr->dock && !wPreferences.flags.noclip)
3415 dock2 = clip;
3416 else if (dock != scr->dock && !wPreferences.flags.nodock)
3417 dock2 = scr->dock;
3419 wins[0] = icon->core->window;
3420 wins[1] = scr->dock_shadow;
3421 XRestackWindows(dpy, wins, 2);
3422 XMoveResizeWindow(dpy, scr->dock_shadow, aicon->x_pos, aicon->y_pos, ICON_SIZE, ICON_SIZE);
3423 if (superfluous) {
3424 if (icon->pixmap != None)
3425 ghost = MakeGhostIcon(scr, icon->pixmap);
3426 else
3427 ghost = MakeGhostIcon(scr, icon->core->window);
3429 XSetWindowBackgroundPixmap(dpy, scr->dock_shadow, ghost);
3430 XClearWindow(dpy, scr->dock_shadow);
3432 XMapWindow(dpy, scr->dock_shadow);
3434 ondock = 1;
3436 while (1) {
3437 XMaskEvent(dpy, PointerMotionMask | ButtonReleaseMask | ButtonPressMask
3438 | ButtonMotionMask | ExposureMask, &ev);
3439 switch (ev.type) {
3440 case Expose:
3441 WMHandleEvent(&ev);
3442 break;
3444 case MotionNotify:
3445 hasMoved = True;
3446 if (!grabbed) {
3447 if (abs(ofs_x - ev.xmotion.x) >= MOVE_THRESHOLD
3448 || abs(ofs_y - ev.xmotion.y) >= MOVE_THRESHOLD) {
3449 XChangeActivePointerGrab(dpy, ButtonMotionMask
3450 | ButtonReleaseMask | ButtonPressMask,
3451 wCursor[WCUR_MOVE], CurrentTime);
3452 grabbed = 1;
3453 } else {
3454 break;
3458 if (omnipresent) {
3459 int i;
3460 for (i = 0; i < scr->workspace_count; i++) {
3461 if (i == scr->current_workspace)
3462 continue;
3463 wDockShowIcons(scr->workspaces[i]->clip);
3467 x = ev.xmotion.x_root - ofs_x;
3468 y = ev.xmotion.y_root - ofs_y;
3469 tmp = wDockSnapIcon(dock, aicon, x, y, &ix, &iy, True);
3470 if (tmp && dock2) {
3471 change_dock = 0;
3472 if (last_dock != dock && collapsed) {
3473 last_dock->collapsed = 1;
3474 wDockHideIcons(last_dock);
3475 collapsed = 0;
3477 if (!collapsed && (collapsed = dock->collapsed)) {
3478 dock->collapsed = 0;
3479 wDockShowIcons(dock);
3481 if (dock->auto_raise_lower)
3482 wDockRaise(dock);
3483 last_dock = dock;
3484 } else if (dock2) {
3485 tmp = wDockSnapIcon(dock2, aicon, x, y, &ix, &iy, False);
3486 if (tmp) {
3487 change_dock = 1;
3488 if (last_dock != dock2 && collapsed) {
3489 last_dock->collapsed = 1;
3490 wDockHideIcons(last_dock);
3491 collapsed = 0;
3493 if (!collapsed && (collapsed = dock2->collapsed)) {
3494 dock2->collapsed = 0;
3495 wDockShowIcons(dock2);
3497 if (dock2->auto_raise_lower)
3498 wDockRaise(dock2);
3499 last_dock = dock2;
3502 if (aicon->launching || aicon->lock || (aicon->running && !(ev.xmotion.state & MOD_MASK))
3503 || (!aicon->running && tmp)) {
3504 shad_x = last_dock->x_pos + ix * wPreferences.icon_size;
3505 shad_y = last_dock->y_pos + iy * wPreferences.icon_size;
3507 XMoveWindow(dpy, scr->dock_shadow, shad_x, shad_y);
3509 if (!ondock)
3510 XMapWindow(dpy, scr->dock_shadow);
3512 ondock = 1;
3513 } else {
3514 if (ondock)
3515 XUnmapWindow(dpy, scr->dock_shadow);
3517 ondock = 0;
3519 XMoveWindow(dpy, icon->core->window, x, y);
3520 break;
3522 case ButtonPress:
3523 break;
3525 case ButtonRelease:
3526 if (ev.xbutton.button != event->xbutton.button)
3527 break;
3528 XUngrabPointer(dpy, CurrentTime);
3529 if (ondock) {
3530 SlideWindow(icon->core->window, x, y, shad_x, shad_y);
3531 XUnmapWindow(dpy, scr->dock_shadow);
3532 if (!change_dock) {
3533 reattachIcon(dock, aicon, ix, iy);
3534 if (clip && dock != clip && clip->auto_raise_lower)
3535 wDockLower(clip);
3536 } else {
3537 docked = moveIconBetweenDocks(dock, dock2, aicon, ix, iy);
3538 if (!docked) {
3539 /* Slide it back if dock rejected it */
3540 SlideWindow(icon->core->window, x, y, aicon->x_pos, aicon->y_pos);
3541 reattachIcon(dock, aicon, aicon->xindex, aicon->yindex);
3543 if (last_dock->type == WM_CLIP && last_dock->auto_collapse)
3544 collapsed = 0;
3546 } else {
3547 aicon->x_pos = x;
3548 aicon->y_pos = y;
3549 if (superfluous) {
3550 if (!aicon->running && !wPreferences.no_animations) {
3551 /* We need to deselect it, even if is deselected in
3552 * wDockDetach(), because else DoKaboom() will fail.
3554 if (aicon->icon->selected)
3555 wIconSelect(aicon->icon);
3557 DoKaboom(scr, aicon->icon->core->window, x, y);
3560 if (clip && clip->auto_raise_lower)
3561 wDockLower(clip);
3562 wDockDetach(dock, aicon);
3564 if (collapsed) {
3565 last_dock->collapsed = 1;
3566 wDockHideIcons(last_dock);
3567 collapsed = 0;
3569 if (superfluous) {
3570 if (ghost != None)
3571 XFreePixmap(dpy, ghost);
3572 XSetWindowBackground(dpy, scr->dock_shadow, scr->white_pixel);
3574 if (omnipresent) {
3575 int i;
3576 for (i = 0; i < scr->workspace_count; i++) {
3577 if (i == scr->current_workspace)
3578 continue;
3579 wDockHideIcons(scr->workspaces[i]->clip);
3582 return hasMoved;;
3585 return False; /* never reached */
3588 static int getClipButton(int px, int py)
3590 int pt = (CLIP_BUTTON_SIZE + 2) * ICON_SIZE / 64;
3592 if (px < 0 || py < 0 || px >= ICON_SIZE || py >= ICON_SIZE)
3593 return CLIP_IDLE;
3595 if (py <= pt - ((int)ICON_SIZE - 1 - px))
3596 return CLIP_FORWARD;
3597 else if (px <= pt - ((int)ICON_SIZE - 1 - py))
3598 return CLIP_REWIND;
3600 return CLIP_IDLE;
3603 static void handleClipChangeWorkspace(WScreen *scr, XEvent *event)
3605 XEvent ev;
3606 int done, direction, new_ws;
3607 int new_dir;
3608 WDock *clip = scr->clip_icon->dock;
3610 direction = getClipButton(event->xbutton.x, event->xbutton.y);
3612 clip->lclip_button_pushed = direction == CLIP_REWIND;
3613 clip->rclip_button_pushed = direction == CLIP_FORWARD;
3615 wClipIconPaint(scr->clip_icon);
3616 done = 0;
3617 while (!done) {
3618 WMMaskEvent(dpy, ExposureMask | ButtonMotionMask | ButtonReleaseMask | ButtonPressMask, &ev);
3619 switch (ev.type) {
3620 case Expose:
3621 WMHandleEvent(&ev);
3622 break;
3624 case MotionNotify:
3625 new_dir = getClipButton(ev.xmotion.x, ev.xmotion.y);
3626 if (new_dir != direction) {
3627 direction = new_dir;
3628 clip->lclip_button_pushed = direction == CLIP_REWIND;
3629 clip->rclip_button_pushed = direction == CLIP_FORWARD;
3630 wClipIconPaint(scr->clip_icon);
3632 break;
3634 case ButtonPress:
3635 break;
3637 case ButtonRelease:
3638 if (ev.xbutton.button == event->xbutton.button)
3639 done = 1;
3643 clip->lclip_button_pushed = 0;
3644 clip->rclip_button_pushed = 0;
3646 new_ws = wPreferences.ws_advance || (event->xbutton.state & ControlMask);
3648 if (direction == CLIP_FORWARD) {
3649 if (scr->current_workspace < scr->workspace_count - 1)
3650 wWorkspaceChange(scr, scr->current_workspace + 1);
3651 else if (new_ws && scr->current_workspace < MAX_WORKSPACES - 1)
3652 wWorkspaceChange(scr, scr->current_workspace + 1);
3653 else if (wPreferences.ws_cycle)
3654 wWorkspaceChange(scr, 0);
3655 } else if (direction == CLIP_REWIND) {
3656 if (scr->current_workspace > 0)
3657 wWorkspaceChange(scr, scr->current_workspace - 1);
3658 else if (scr->current_workspace == 0 && wPreferences.ws_cycle)
3659 wWorkspaceChange(scr, scr->workspace_count - 1);
3662 wClipIconPaint(scr->clip_icon);
3665 static void iconMouseDown(WObjDescriptor *desc, XEvent *event)
3667 WAppIcon *aicon = desc->parent;
3668 WDock *dock = aicon->dock;
3669 WScreen *scr = aicon->icon->core->screen_ptr;
3671 if (aicon->editing || WCHECK_STATE(WSTATE_MODAL))
3672 return;
3674 scr->last_dock = dock;
3676 if (dock->menu->flags.mapped)
3677 wMenuUnmap(dock->menu);
3679 if (IsDoubleClick(scr, event)) {
3680 /* double-click was not in the main clip icon */
3681 if (dock->type != WM_CLIP || aicon->xindex != 0 || aicon->yindex != 0
3682 || getClipButton(event->xbutton.x, event->xbutton.y) == CLIP_IDLE) {
3683 iconDblClick(desc, event);
3684 return;
3688 if (event->xbutton.button == Button1) {
3689 if (event->xbutton.state & MOD_MASK)
3690 wDockLower(dock);
3691 else
3692 wDockRaise(dock);
3694 if ((event->xbutton.state & ShiftMask) && aicon != scr->clip_icon && dock->type != WM_DOCK) {
3695 wIconSelect(aicon->icon);
3696 return;
3699 if (aicon->yindex == 0 && aicon->xindex == 0) {
3700 if (getClipButton(event->xbutton.x, event->xbutton.y) != CLIP_IDLE
3701 && dock->type == WM_CLIP)
3702 handleClipChangeWorkspace(scr, event);
3703 else
3704 handleDockMove(dock, aicon, event);
3705 } else {
3706 Bool hasMoved = handleIconMove(dock, aicon, event);
3707 if (wPreferences.single_click && !hasMoved)
3708 iconDblClick(desc, event);
3710 } else if (event->xbutton.button == Button2 && dock->type == WM_CLIP && aicon == scr->clip_icon) {
3711 if (!scr->clip_ws_menu) {
3712 scr->clip_ws_menu = wWorkspaceMenuMake(scr, False);
3714 if (scr->clip_ws_menu) {
3715 WMenu *wsMenu = scr->clip_ws_menu;
3716 int xpos;
3718 wWorkspaceMenuUpdate(scr, wsMenu);
3720 xpos = event->xbutton.x_root - wsMenu->frame->core->width / 2 - 1;
3721 if (xpos < 0) {
3722 xpos = 0;
3723 } else if (xpos + wsMenu->frame->core->width > scr->scr_width - 2) {
3724 xpos = scr->scr_width - wsMenu->frame->core->width - 4;
3726 wMenuMapAt(wsMenu, xpos, event->xbutton.y_root + 2, False);
3728 desc = &wsMenu->menu->descriptor;
3729 event->xany.send_event = True;
3730 (*desc->handle_mousedown) (desc, event);
3732 } else if (event->xbutton.button == Button2 && dock->type == WM_CLIP &&
3733 (event->xbutton.state & ShiftMask) && aicon != scr->clip_icon) {
3734 wClipMakeIconOmnipresent(aicon, !aicon->omnipresent);
3735 } else if (event->xbutton.button == Button3) {
3736 if (event->xbutton.send_event &&
3737 XGrabPointer(dpy, aicon->icon->core->window, True, ButtonMotionMask
3738 | ButtonReleaseMask | ButtonPressMask, GrabModeAsync,
3739 GrabModeAsync, None, None, CurrentTime) != GrabSuccess) {
3740 wwarning("pointer grab failed for dockicon menu");
3741 return;
3744 openDockMenu(dock, aicon, event);
3745 } else if (event->xbutton.button == Button2) {
3746 WAppIcon *btn = desc->parent;
3748 if (!btn->launching && (!btn->running || (event->xbutton.state & ControlMask)))
3749 launchDockedApplication(btn, True);
3753 static void clipEnterNotify(WObjDescriptor *desc, XEvent *event)
3755 WAppIcon *btn = (WAppIcon *) desc->parent;
3756 WDock *dock;
3757 WScreen *scr;
3759 assert(event->type == EnterNotify);
3761 if (desc->parent_type != WCLASS_DOCK_ICON)
3762 return;
3764 scr = btn->icon->core->screen_ptr;
3765 if (!btn->omnipresent)
3766 dock = btn->dock;
3767 else
3768 dock = scr->workspaces[scr->current_workspace]->clip;
3770 if (!dock || dock->type != WM_CLIP)
3771 return;
3773 /* The auto raise/lower code */
3774 if (dock->auto_lower_magic) {
3775 WMDeleteTimerHandler(dock->auto_lower_magic);
3776 dock->auto_lower_magic = NULL;
3778 if (dock->auto_raise_lower && !dock->auto_raise_magic)
3779 dock->auto_raise_magic = WMAddTimerHandler(AUTO_RAISE_DELAY, clipAutoRaise, (void *)dock);
3781 /* The auto expand/collapse code */
3782 if (dock->auto_collapse_magic) {
3783 WMDeleteTimerHandler(dock->auto_collapse_magic);
3784 dock->auto_collapse_magic = NULL;
3786 if (dock->auto_collapse && !dock->auto_expand_magic)
3787 dock->auto_expand_magic = WMAddTimerHandler(AUTO_EXPAND_DELAY, clipAutoExpand, (void *)dock);
3790 static void clipLeave(WDock *dock)
3792 XEvent event;
3793 WObjDescriptor *desc = NULL;
3795 if (!dock || dock->type != WM_CLIP)
3796 return;
3798 if (XCheckTypedEvent(dpy, EnterNotify, &event) != False) {
3799 if (XFindContext(dpy, event.xcrossing.window, wWinContext,
3800 (XPointer *) & desc) != XCNOENT
3801 && desc && desc->parent_type == WCLASS_DOCK_ICON
3802 && ((WAppIcon *) desc->parent)->dock && ((WAppIcon *) desc->parent)->dock->type == WM_CLIP) {
3803 /* We didn't left the Clip yet */
3804 XPutBackEvent(dpy, &event);
3805 return;
3808 XPutBackEvent(dpy, &event);
3809 } else {
3810 /* We entered a withdrawn window, so we're still in Clip */
3811 return;
3814 if (dock->auto_raise_magic) {
3815 WMDeleteTimerHandler(dock->auto_raise_magic);
3816 dock->auto_raise_magic = NULL;
3818 if (dock->auto_raise_lower && !dock->auto_lower_magic)
3819 dock->auto_lower_magic = WMAddTimerHandler(AUTO_LOWER_DELAY, clipAutoLower, (void *)dock);
3821 if (dock->auto_expand_magic) {
3822 WMDeleteTimerHandler(dock->auto_expand_magic);
3823 dock->auto_expand_magic = NULL;
3825 if (dock->auto_collapse && !dock->auto_collapse_magic)
3826 dock->auto_collapse_magic = WMAddTimerHandler(AUTO_COLLAPSE_DELAY, clipAutoCollapse, (void *)dock);
3829 static void clipLeaveNotify(WObjDescriptor *desc, XEvent *event)
3831 WAppIcon *btn = (WAppIcon *) desc->parent;
3833 assert(event->type == LeaveNotify);
3835 if (desc->parent_type != WCLASS_DOCK_ICON)
3836 return;
3838 clipLeave(btn->dock);
3841 static void clipAutoCollapse(void *cdata)
3843 WDock *dock = (WDock *) cdata;
3845 if (dock->type != WM_CLIP)
3846 return;
3848 if (dock->auto_collapse) {
3849 dock->collapsed = 1;
3850 wDockHideIcons(dock);
3852 dock->auto_collapse_magic = NULL;
3855 static void clipAutoExpand(void *cdata)
3857 WDock *dock = (WDock *) cdata;
3859 if (dock->type != WM_CLIP)
3860 return;
3862 if (dock->auto_collapse) {
3863 dock->collapsed = 0;
3864 wDockShowIcons(dock);
3866 dock->auto_expand_magic = NULL;
3869 static void clipAutoLower(void *cdata)
3871 WDock *dock = (WDock *) cdata;
3873 if (dock->type != WM_CLIP)
3874 return;
3876 if (dock->auto_raise_lower)
3877 wDockLower(dock);
3879 dock->auto_lower_magic = NULL;
3882 static void clipAutoRaise(void *cdata)
3884 WDock *dock = (WDock *) cdata;
3886 if (dock->type != WM_CLIP)
3887 return;
3889 if (dock->auto_raise_lower)
3890 wDockRaise(dock);
3892 dock->auto_raise_magic = NULL;
3895 static Bool iconCanBeOmnipresent(WAppIcon *aicon)
3897 WScreen *scr = aicon->icon->core->screen_ptr;
3898 WDock *clip;
3899 WAppIcon *btn;
3900 int i, j;
3902 for (i = 0; i < scr->workspace_count; i++) {
3903 clip = scr->workspaces[i]->clip;
3905 if (clip == aicon->dock)
3906 continue;
3908 if (clip->icon_count + scr->global_icon_count >= clip->max_icons)
3909 return False; /* Clip is full in some workspace */
3911 for (j = 0; j < clip->max_icons; j++) {
3912 btn = clip->icon_array[j];
3913 if (btn && btn->xindex == aicon->xindex && btn->yindex == aicon->yindex)
3914 return False;
3918 return True;
3921 int wClipMakeIconOmnipresent(WAppIcon *aicon, int omnipresent)
3923 WScreen *scr = aicon->icon->core->screen_ptr;
3924 WAppIconChain *new_entry, *tmp, *tmp1;
3925 int status = WO_SUCCESS;
3927 if ((scr->dock && aicon->dock == scr->dock) || aicon == scr->clip_icon)
3928 return WO_NOT_APPLICABLE;
3930 if (aicon->omnipresent == omnipresent)
3931 return WO_SUCCESS;
3933 if (omnipresent) {
3934 if (iconCanBeOmnipresent(aicon)) {
3935 aicon->omnipresent = 1;
3936 new_entry = wmalloc(sizeof(WAppIconChain));
3937 new_entry->aicon = aicon;
3938 new_entry->next = scr->global_icons;
3939 scr->global_icons = new_entry;
3940 scr->global_icon_count++;
3941 } else {
3942 aicon->omnipresent = 0;
3943 status = WO_FAILED;
3945 } else {
3946 aicon->omnipresent = 0;
3947 if (aicon == scr->global_icons->aicon) {
3948 tmp = scr->global_icons->next;
3949 wfree(scr->global_icons);
3950 scr->global_icons = tmp;
3951 scr->global_icon_count--;
3952 } else {
3953 tmp = scr->global_icons;
3954 while (tmp->next) {
3955 if (tmp->next->aicon == aicon) {
3956 tmp1 = tmp->next->next;
3957 wfree(tmp->next);
3958 tmp->next = tmp1;
3959 scr->global_icon_count--;
3960 break;
3962 tmp = tmp->next;
3967 wAppIconPaint(aicon);
3969 return status;