wAppIconPaint paint argument
[wmaker-crm.git] / src / dock.c
blob295f7178de96315fb54d582a5c2d8f586c45485b
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 wAppIconPaint(aicon, True);
544 save_appicon(aicon, True);
546 WMFreeArray(selectedIcons);
549 static void toggleAutoAttractCallback(WMenu *menu, WMenuEntry *entry)
551 WDock *dock = (WDock *) entry->clientdata;
553 assert(entry->clientdata != NULL);
555 dock->attract_icons = !dock->attract_icons;
557 entry->flags.indicator_on = dock->attract_icons;
559 wMenuPaint(menu);
562 static void selectCallback(WMenu *menu, WMenuEntry *entry)
564 WAppIcon *icon = (WAppIcon *) entry->clientdata;
566 assert(icon != NULL);
568 wIconSelect(icon->icon);
570 wMenuPaint(menu);
573 static void colectIconsCallback(WMenu *menu, WMenuEntry *entry)
575 WAppIcon *clickedIcon = (WAppIcon *) entry->clientdata;
576 WDock *clip;
577 WAppIcon *aicon;
578 int x, y, x_pos, y_pos;
579 Bool update_icon = False;
581 assert(entry->clientdata != NULL);
582 clip = clickedIcon->dock;
584 aicon = clip->screen_ptr->app_icon_list;
586 while (aicon) {
587 if (!aicon->docked && wDockFindFreeSlot(clip, &x, &y)) {
588 x_pos = clip->x_pos + x * ICON_SIZE;
589 y_pos = clip->y_pos + y * ICON_SIZE;
590 if (aicon->x_pos != x_pos || aicon->y_pos != y_pos)
591 move_window(aicon->icon->core->window, aicon->x_pos, aicon->y_pos, x_pos, y_pos);
593 aicon->attracted = 1;
594 if (!aicon->icon->shadowed) {
595 aicon->icon->shadowed = 1;
596 update_icon = True;
598 wDockAttachIcon(clip, aicon, x, y, update_icon);
599 if (clip->collapsed || !clip->mapped)
600 XUnmapWindow(dpy, aicon->icon->core->window);
602 aicon = aicon->next;
606 static void selectIconsCallback(WMenu *menu, WMenuEntry *entry)
608 WAppIcon *clickedIcon = (WAppIcon *) entry->clientdata;
609 WDock *dock;
610 WMArray *selectedIcons;
611 WMArrayIterator iter;
612 WAppIcon *btn;
613 int i;
615 assert(clickedIcon != NULL);
616 dock = clickedIcon->dock;
618 selectedIcons = getSelected(dock);
620 if (!WMGetArrayItemCount(selectedIcons)) {
621 for (i = 1; i < dock->max_icons; i++) {
622 btn = dock->icon_array[i];
623 if (btn && !btn->icon->selected)
624 wIconSelect(btn->icon);
626 } else {
627 WM_ITERATE_ARRAY(selectedIcons, btn, iter) {
628 wIconSelect(btn->icon);
631 WMFreeArray(selectedIcons);
633 wMenuPaint(menu);
636 static void toggleCollapsedCallback(WMenu *menu, WMenuEntry *entry)
638 assert(entry->clientdata != NULL);
640 toggleCollapsed(entry->clientdata);
642 entry->flags.indicator_on = ((WDock *) entry->clientdata)->collapsed;
644 wMenuPaint(menu);
647 static void toggleAutoCollapseCallback(WMenu *menu, WMenuEntry *entry)
649 WDock *dock;
650 assert(entry->clientdata != NULL);
652 dock = (WDock *) entry->clientdata;
654 dock->auto_collapse = !dock->auto_collapse;
656 entry->flags.indicator_on = ((WDock *) entry->clientdata)->auto_collapse;
658 wMenuPaint(menu);
661 static void toggleAutoRaiseLowerCallback(WMenu *menu, WMenuEntry *entry)
663 WDock *dock;
664 assert(entry->clientdata != NULL);
666 dock = (WDock *) entry->clientdata;
668 dock->auto_raise_lower = !dock->auto_raise_lower;
670 entry->flags.indicator_on = ((WDock *) entry->clientdata)->auto_raise_lower;
672 wMenuPaint(menu);
675 static void launchCallback(WMenu *menu, WMenuEntry *entry)
677 WAppIcon *btn = (WAppIcon *) entry->clientdata;
679 launchDockedApplication(btn, False);
682 static void settingsCallback(WMenu *menu, WMenuEntry *entry)
684 WAppIcon *btn = (WAppIcon *) entry->clientdata;
686 if (btn->editing)
687 return;
688 ShowDockAppSettingsPanel(btn);
691 static void hideCallback(WMenu *menu, WMenuEntry *entry)
693 WApplication *wapp;
694 WAppIcon *btn = (WAppIcon *) entry->clientdata;
696 wapp = wApplicationOf(btn->icon->owner->main_window);
698 if (wapp->flags.hidden) {
699 wWorkspaceChange(btn->icon->core->screen_ptr, wapp->last_workspace);
700 wUnhideApplication(wapp, False, False);
701 } else {
702 wHideApplication(wapp);
706 static void unhideHereCallback(WMenu *menu, WMenuEntry *entry)
708 WApplication *wapp;
709 WAppIcon *btn = (WAppIcon *) entry->clientdata;
711 wapp = wApplicationOf(btn->icon->owner->main_window);
713 wUnhideApplication(wapp, False, True);
716 static WAppIcon *mainIconCreate(WScreen *scr, int type)
718 WAppIcon *btn;
719 int x_pos;
721 if (type == WM_CLIP) {
722 if (scr->clip_icon)
723 return scr->clip_icon;
724 btn = wAppIconCreateForDock(scr, NULL, "Logo", "WMClip", TILE_CLIP);
725 btn->icon->core->descriptor.handle_expose = clipIconExpose;
726 btn->icon->core->descriptor.handle_enternotify = clipEnterNotify;
727 btn->icon->core->descriptor.handle_leavenotify = clipLeaveNotify;
728 x_pos = 0;
729 } else {
730 btn = wAppIconCreateForDock(scr, NULL, "Logo", "WMDock", TILE_NORMAL);
731 x_pos = scr->scr_width - ICON_SIZE - DOCK_EXTRA_SPACE;
734 btn->xindex = 0;
735 btn->yindex = 0;
737 btn->icon->core->descriptor.handle_mousedown = iconMouseDown;
738 btn->icon->core->descriptor.parent_type = WCLASS_DOCK_ICON;
739 btn->icon->core->descriptor.parent = btn;
740 XMapWindow(dpy, btn->icon->core->window);
741 btn->x_pos = x_pos;
742 btn->y_pos = 0;
743 btn->docked = 1;
744 if (type == WM_CLIP)
745 scr->clip_icon = btn;
747 return btn;
750 static void switchWSCommand(WMenu *menu, WMenuEntry *entry)
752 WAppIcon *btn, *icon = (WAppIcon *) entry->clientdata;
753 WScreen *scr = icon->icon->core->screen_ptr;
754 WDock *src, *dest;
755 WMArray *selectedIcons;
756 int x, y;
758 if (entry->order == scr->current_workspace)
759 return;
760 src = icon->dock;
761 dest = scr->workspaces[entry->order]->clip;
763 selectedIcons = getSelected(src);
765 if (WMGetArrayItemCount(selectedIcons)) {
766 WMArrayIterator iter;
768 WM_ITERATE_ARRAY(selectedIcons, btn, iter) {
769 if (wDockFindFreeSlot(dest, &x, &y)) {
770 moveIconBetweenDocks(src, dest, btn, x, y);
771 XUnmapWindow(dpy, btn->icon->core->window);
774 } else if (icon != scr->clip_icon) {
775 if (wDockFindFreeSlot(dest, &x, &y)) {
776 moveIconBetweenDocks(src, dest, icon, x, y);
777 XUnmapWindow(dpy, icon->icon->core->window);
780 WMFreeArray(selectedIcons);
783 static void launchDockedApplication(WAppIcon *btn, Bool withSelection)
785 WScreen *scr = btn->icon->core->screen_ptr;
787 if (!btn->launching &&
788 ((!withSelection && btn->command != NULL) || (withSelection && btn->paste_command != NULL))) {
789 if (!btn->forced_dock) {
790 btn->relaunching = btn->running;
791 btn->running = 1;
793 if (btn->wm_instance || btn->wm_class) {
794 WWindowAttributes attr;
795 memset(&attr, 0, sizeof(WWindowAttributes));
796 wDefaultFillAttributes(btn->wm_instance, btn->wm_class, &attr, NULL, True);
798 if (!attr.no_appicon && !btn->buggy_app)
799 btn->launching = 1;
800 else
801 btn->running = 0;
803 btn->drop_launch = 0;
804 btn->paste_launch = withSelection;
805 scr->last_dock = btn->dock;
806 btn->pid = execCommand(btn, (withSelection ? btn->paste_command : btn->command), NULL);
807 if (btn->pid > 0) {
808 if (btn->buggy_app) {
809 /* give feedback that the app was launched */
810 btn->launching = 1;
811 dockIconPaint(btn);
812 btn->launching = 0;
813 WMAddTimerHandler(200, (WMCallback *) dockIconPaint, btn);
814 } else {
815 dockIconPaint(btn);
817 } else {
818 wwarning(_("could not launch application %s"), btn->command);
819 btn->launching = 0;
820 if (!btn->relaunching)
821 btn->running = 0;
826 static void updateWorkspaceMenu(WMenu *menu, WAppIcon *icon)
828 WScreen *scr = menu->frame->screen_ptr;
829 char title[MAX_WORKSPACENAME_WIDTH + 1];
830 int i;
832 if (!menu || !icon)
833 return;
835 for (i = 0; i < scr->workspace_count; i++) {
836 if (i < menu->entry_no) {
837 if (strcmp(menu->entries[i]->text, scr->workspaces[i]->name) != 0) {
838 wfree(menu->entries[i]->text);
839 strcpy(title, scr->workspaces[i]->name);
840 menu->entries[i]->text = wstrdup(title);
841 menu->flags.realized = 0;
843 menu->entries[i]->clientdata = (void *)icon;
844 } else {
845 strcpy(title, scr->workspaces[i]->name);
847 wMenuAddCallback(menu, title, switchWSCommand, (void *)icon);
849 menu->flags.realized = 0;
852 if (i == scr->current_workspace)
853 wMenuSetEnabled(menu, i, False);
854 else
855 wMenuSetEnabled(menu, i, True);
858 if (!menu->flags.realized)
859 wMenuRealize(menu);
862 static WMenu *makeWorkspaceMenu(WScreen *scr)
864 WMenu *menu;
866 menu = wMenuCreate(scr, NULL, False);
867 if (!menu)
868 wwarning(_("could not create workspace submenu for Clip menu"));
870 wMenuAddCallback(menu, "", switchWSCommand, (void *)scr->clip_icon);
872 menu->flags.realized = 0;
873 wMenuRealize(menu);
875 return menu;
878 static void updateClipOptionsMenu(WMenu *menu, WDock *dock)
880 WMenuEntry *entry;
881 int index = 0;
883 if (!menu || !dock)
884 return;
886 /* keep on top */
887 entry = menu->entries[index];
888 entry->flags.indicator_on = !dock->lowered;
889 entry->clientdata = dock;
891 /* collapsed */
892 entry = menu->entries[++index];
893 entry->flags.indicator_on = dock->collapsed;
894 entry->clientdata = dock;
896 /* auto-collapse */
897 entry = menu->entries[++index];
898 entry->flags.indicator_on = dock->auto_collapse;
899 entry->clientdata = dock;
901 /* auto-raise/lower */
902 entry = menu->entries[++index];
903 entry->flags.indicator_on = dock->auto_raise_lower;
904 entry->clientdata = dock;
905 wMenuSetEnabled(menu, index, dock->lowered);
907 /* attract icons */
908 entry = menu->entries[++index];
909 entry->flags.indicator_on = dock->attract_icons;
910 entry->clientdata = dock;
912 menu->flags.realized = 0;
913 wMenuRealize(menu);
916 static WMenu *makeClipOptionsMenu(WScreen *scr)
918 WMenu *menu;
919 WMenuEntry *entry;
921 menu = wMenuCreate(scr, NULL, False);
922 if (!menu) {
923 wwarning(_("could not create options submenu for Clip menu"));
924 return NULL;
927 entry = wMenuAddCallback(menu, _("Keep on Top"), toggleLoweredCallback, NULL);
928 entry->flags.indicator = 1;
929 entry->flags.indicator_on = 1;
930 entry->flags.indicator_type = MI_CHECK;
932 entry = wMenuAddCallback(menu, _("Collapsed"), toggleCollapsedCallback, NULL);
933 entry->flags.indicator = 1;
934 entry->flags.indicator_on = 1;
935 entry->flags.indicator_type = MI_CHECK;
937 entry = wMenuAddCallback(menu, _("Autocollapse"), toggleAutoCollapseCallback, NULL);
938 entry->flags.indicator = 1;
939 entry->flags.indicator_on = 1;
940 entry->flags.indicator_type = MI_CHECK;
942 entry = wMenuAddCallback(menu, _("Autoraise"), toggleAutoRaiseLowerCallback, NULL);
943 entry->flags.indicator = 1;
944 entry->flags.indicator_on = 1;
945 entry->flags.indicator_type = MI_CHECK;
947 entry = wMenuAddCallback(menu, _("Autoattract Icons"), toggleAutoAttractCallback, NULL);
948 entry->flags.indicator = 1;
949 entry->flags.indicator_on = 1;
950 entry->flags.indicator_type = MI_CHECK;
952 menu->flags.realized = 0;
953 wMenuRealize(menu);
955 return menu;
958 static WMenu *dockMenuCreate(WScreen *scr, int type)
960 WMenu *menu;
961 WMenuEntry *entry;
963 if (type == WM_CLIP && scr->clip_menu)
964 return scr->clip_menu;
966 menu = wMenuCreate(scr, NULL, False);
967 if (type != WM_CLIP) {
968 entry = wMenuAddCallback(menu, _("Keep on Top"), toggleLoweredCallback, NULL);
969 entry->flags.indicator = 1;
970 entry->flags.indicator_on = 1;
971 entry->flags.indicator_type = MI_CHECK;
972 } else {
973 entry = wMenuAddCallback(menu, _("Clip Options"), NULL, NULL);
974 scr->clip_options = makeClipOptionsMenu(scr);
975 if (scr->clip_options)
976 wMenuEntrySetCascade(menu, entry, scr->clip_options);
978 entry = wMenuAddCallback(menu, _("Rename Workspace"), renameCallback, NULL);
979 wfree(entry->text);
980 entry->text = _("Rename Workspace");
982 entry = wMenuAddCallback(menu, _("Selected"), selectCallback, NULL);
983 entry->flags.indicator = 1;
984 entry->flags.indicator_on = 1;
985 entry->flags.indicator_type = MI_CHECK;
987 entry = wMenuAddCallback(menu, _("Select All Icons"), selectIconsCallback, NULL);
988 wfree(entry->text);
989 entry->text = _("Select All Icons");
991 entry = wMenuAddCallback(menu, _("Keep Icon"), keepIconsCallback, NULL);
992 wfree(entry->text);
993 entry->text = _("Keep Icon");
995 entry = wMenuAddCallback(menu, _("Move Icon To"), NULL, NULL);
996 wfree(entry->text);
997 entry->text = _("Move Icon To");
998 scr->clip_submenu = makeWorkspaceMenu(scr);
999 if (scr->clip_submenu)
1000 wMenuEntrySetCascade(menu, entry, scr->clip_submenu);
1002 entry = wMenuAddCallback(menu, _("Remove Icon"), removeIconsCallback, NULL);
1003 wfree(entry->text);
1004 entry->text = _("Remove Icon");
1006 wMenuAddCallback(menu, _("Attract Icons"), colectIconsCallback, NULL);
1009 wMenuAddCallback(menu, _("Launch"), launchCallback, NULL);
1011 wMenuAddCallback(menu, _("Unhide Here"), unhideHereCallback, NULL);
1013 entry = wMenuAddCallback(menu, _("Hide"), hideCallback, NULL);
1014 wfree(entry->text);
1015 entry->text = _("Hide");
1017 wMenuAddCallback(menu, _("Settings..."), settingsCallback, NULL);
1019 wMenuAddCallback(menu, _("Kill"), killCallback, NULL);
1021 if (type == WM_CLIP)
1022 scr->clip_menu = menu;
1024 return menu;
1027 WDock *wDockCreate(WScreen *scr, int type)
1029 WDock *dock;
1030 WAppIcon *btn;
1032 make_keys();
1034 dock = wmalloc(sizeof(WDock));
1036 dock->max_icons = DOCK_MAX_ICONS;
1038 dock->icon_array = wmalloc(sizeof(WAppIcon *) * dock->max_icons);
1040 btn = mainIconCreate(scr, type);
1042 btn->dock = dock;
1044 dock->x_pos = btn->x_pos;
1045 dock->y_pos = btn->y_pos;
1046 dock->screen_ptr = scr;
1047 dock->type = type;
1048 dock->icon_count = 1;
1049 dock->on_right_side = 1;
1050 dock->collapsed = 0;
1051 dock->auto_collapse = 0;
1052 dock->auto_collapse_magic = NULL;
1053 dock->auto_raise_lower = 0;
1054 dock->auto_lower_magic = NULL;
1055 dock->auto_raise_magic = NULL;
1056 dock->attract_icons = 0;
1057 dock->lowered = 1;
1058 dock->icon_array[0] = btn;
1059 wRaiseFrame(btn->icon->core);
1060 XMoveWindow(dpy, btn->icon->core->window, btn->x_pos, btn->y_pos);
1062 /* create dock menu */
1063 dock->menu = dockMenuCreate(scr, type);
1065 return dock;
1068 void wDockDestroy(WDock *dock)
1070 int i;
1071 WAppIcon *aicon;
1073 for (i = (dock->type == WM_CLIP) ? 1 : 0; i < dock->max_icons; i++) {
1074 aicon = dock->icon_array[i];
1075 if (aicon) {
1076 int keepit = aicon->running && wApplicationOf(aicon->main_window);
1077 wDockDetach(dock, aicon);
1078 if (keepit) {
1079 /* XXX: can: aicon->icon == NULL ? */
1080 PlaceIcon(dock->screen_ptr, &aicon->x_pos, &aicon->y_pos,
1081 wGetHeadForWindow(aicon->icon->owner));
1082 XMoveWindow(dpy, aicon->icon->core->window, aicon->x_pos, aicon->y_pos);
1083 if (!dock->mapped || dock->collapsed)
1084 XMapWindow(dpy, aicon->icon->core->window);
1088 if (wPreferences.auto_arrange_icons)
1089 wArrangeIcons(dock->screen_ptr, True);
1090 wfree(dock->icon_array);
1091 if (dock->menu && dock->type != WM_CLIP)
1092 wMenuDestroy(dock->menu, True);
1093 if (dock->screen_ptr->last_dock == dock)
1094 dock->screen_ptr->last_dock = NULL;
1095 wfree(dock);
1098 void wClipIconPaint(WAppIcon *aicon)
1100 WScreen *scr = aicon->icon->core->screen_ptr;
1101 WWorkspace *workspace = scr->workspaces[scr->current_workspace];
1102 WMColor *color;
1103 Window win = aicon->icon->core->window;
1104 int length, nlength;
1105 char *ws_name, ws_number[10];
1106 int ty, tx;
1108 wIconPaint(aicon->icon);
1110 length = strlen(workspace->name);
1111 ws_name = wmalloc(length + 1);
1112 snprintf(ws_name, length + 1, "%s", workspace->name);
1113 snprintf(ws_number, sizeof(ws_number), "%i", scr->current_workspace + 1);
1114 nlength = strlen(ws_number);
1116 if (!workspace->clip->collapsed)
1117 color = scr->clip_title_color[CLIP_NORMAL];
1118 else
1119 color = scr->clip_title_color[CLIP_COLLAPSED];
1121 ty = ICON_SIZE - WMFontHeight(scr->clip_title_font) - 3;
1123 tx = CLIP_BUTTON_SIZE * ICON_SIZE / 64;
1125 if(wPreferences.show_clip_title)
1126 WMDrawString(scr->wmscreen, win, color, scr->clip_title_font, tx, ty, ws_name, length);
1128 tx = (ICON_SIZE / 2 - WMWidthOfString(scr->clip_title_font, ws_number, nlength)) / 2;
1130 WMDrawString(scr->wmscreen, win, color, scr->clip_title_font, tx, 2, ws_number, nlength);
1132 wfree(ws_name);
1134 if (aicon->launching)
1135 XFillRectangle(dpy, aicon->icon->core->window, scr->stipple_gc,
1136 0, 0, wPreferences.icon_size, wPreferences.icon_size);
1138 paintClipButtons(aicon, aicon->dock->lclip_button_pushed, aicon->dock->rclip_button_pushed);
1141 static void clipIconExpose(WObjDescriptor *desc, XEvent *event)
1143 wClipIconPaint(desc->parent);
1146 static void dockIconPaint(WAppIcon *btn)
1148 if (btn == btn->icon->core->screen_ptr->clip_icon) {
1149 wClipIconPaint(btn);
1150 } else {
1151 wAppIconPaint(btn, False);
1152 save_appicon(btn, True);
1156 static WMPropList *make_icon_state(WAppIcon *btn)
1158 WMPropList *node = NULL;
1159 WMPropList *command, *autolaunch, *lock, *name, *forced;
1160 WMPropList *position, *buggy, *omnipresent;
1161 char *tmp;
1162 char buffer[64];
1164 if (btn) {
1165 if (!btn->command)
1166 command = WMCreatePLString("-");
1167 else
1168 command = WMCreatePLString(btn->command);
1170 autolaunch = btn->auto_launch ? dYes : dNo;
1172 lock = btn->lock ? dYes : dNo;
1174 tmp = EscapeWM_CLASS(btn->wm_instance, btn->wm_class);
1176 name = WMCreatePLString(tmp);
1178 wfree(tmp);
1180 forced = btn->forced_dock ? dYes : dNo;
1182 buggy = btn->buggy_app ? dYes : dNo;
1184 if (btn == btn->icon->core->screen_ptr->clip_icon)
1185 snprintf(buffer, sizeof(buffer), "%i,%i", btn->x_pos, btn->y_pos);
1186 else
1187 snprintf(buffer, sizeof(buffer), "%hi,%hi", btn->xindex, btn->yindex);
1188 position = WMCreatePLString(buffer);
1190 node = WMCreatePLDictionary(dCommand, command,
1191 dName, name,
1192 dAutoLaunch, autolaunch,
1193 dLock, lock,
1194 dForced, forced, dBuggyApplication, buggy, dPosition, position, NULL);
1195 WMReleasePropList(command);
1196 WMReleasePropList(name);
1197 WMReleasePropList(position);
1199 omnipresent = btn->omnipresent ? dYes : dNo;
1200 if (btn->dock != btn->icon->core->screen_ptr->dock && (btn->xindex != 0 || btn->yindex != 0))
1201 WMPutInPLDictionary(node, dOmnipresent, omnipresent);
1203 #ifdef XDND /* was OFFIX */
1204 if (btn->dnd_command) {
1205 command = WMCreatePLString(btn->dnd_command);
1206 WMPutInPLDictionary(node, dDropCommand, command);
1207 WMReleasePropList(command);
1209 #endif /* XDND */
1211 if (btn->paste_command) {
1212 command = WMCreatePLString(btn->paste_command);
1213 WMPutInPLDictionary(node, dPasteCommand, command);
1214 WMReleasePropList(command);
1218 return node;
1221 static WMPropList *dockSaveState(WDock *dock)
1223 int i;
1224 WMPropList *icon_info;
1225 WMPropList *list = NULL, *dock_state = NULL;
1226 WMPropList *value, *key;
1227 char buffer[256];
1229 list = WMCreatePLArray(NULL);
1231 for (i = (dock->type == WM_DOCK ? 0 : 1); i < dock->max_icons; i++) {
1232 WAppIcon *btn = dock->icon_array[i];
1234 if (!btn || btn->attracted)
1235 continue;
1237 if ((icon_info = make_icon_state(dock->icon_array[i]))) {
1238 WMAddToPLArray(list, icon_info);
1239 WMReleasePropList(icon_info);
1243 dock_state = WMCreatePLDictionary(dApplications, list, NULL);
1245 if (dock->type == WM_DOCK) {
1246 snprintf(buffer, sizeof(buffer), "Applications%i", dock->screen_ptr->scr_height);
1247 key = WMCreatePLString(buffer);
1248 WMPutInPLDictionary(dock_state, key, list);
1249 WMReleasePropList(key);
1251 snprintf(buffer, sizeof(buffer), "%i,%i", (dock->on_right_side ? -ICON_SIZE : 0), dock->y_pos);
1252 value = WMCreatePLString(buffer);
1253 WMPutInPLDictionary(dock_state, dPosition, value);
1254 WMReleasePropList(value);
1256 WMReleasePropList(list);
1258 value = (dock->lowered ? dYes : dNo);
1259 WMPutInPLDictionary(dock_state, dLowered, value);
1261 if (dock->type == WM_CLIP) {
1262 value = (dock->collapsed ? dYes : dNo);
1263 WMPutInPLDictionary(dock_state, dCollapsed, value);
1265 value = (dock->auto_collapse ? dYes : dNo);
1266 WMPutInPLDictionary(dock_state, dAutoCollapse, value);
1268 value = (dock->auto_raise_lower ? dYes : dNo);
1269 WMPutInPLDictionary(dock_state, dAutoRaiseLower, value);
1271 value = (dock->attract_icons ? dYes : dNo);
1272 WMPutInPLDictionary(dock_state, dAutoAttractIcons, value);
1275 return dock_state;
1278 void wDockSaveState(WScreen *scr, WMPropList *old_state)
1280 WMPropList *dock_state;
1281 WMPropList *keys;
1283 dock_state = dockSaveState(scr->dock);
1286 * Copy saved states of docks with different sizes.
1288 if (old_state) {
1289 int i;
1290 WMPropList *tmp;
1292 keys = WMGetPLDictionaryKeys(old_state);
1293 for (i = 0; i < WMGetPropListItemCount(keys); i++) {
1294 tmp = WMGetFromPLArray(keys, i);
1296 if (strncasecmp(WMGetFromPLString(tmp), "applications", 12) == 0
1297 && !WMGetFromPLDictionary(dock_state, tmp)) {
1299 WMPutInPLDictionary(dock_state, tmp, WMGetFromPLDictionary(old_state, tmp));
1302 WMReleasePropList(keys);
1305 WMPutInPLDictionary(scr->session_state, dDock, dock_state);
1307 WMReleasePropList(dock_state);
1310 void wClipSaveState(WScreen *scr)
1312 WMPropList *clip_state;
1314 clip_state = make_icon_state(scr->clip_icon);
1316 WMPutInPLDictionary(scr->session_state, dClip, clip_state);
1318 WMReleasePropList(clip_state);
1321 WMPropList *wClipSaveWorkspaceState(WScreen *scr, int workspace)
1323 return dockSaveState(scr->workspaces[workspace]->clip);
1326 static Bool getBooleanDockValue(WMPropList *value, WMPropList *key)
1328 if (value) {
1329 if (WMIsPLString(value)) {
1330 if (strcasecmp(WMGetFromPLString(value), "YES") == 0)
1331 return True;
1332 } else {
1333 wwarning(_("bad value in docked icon state info %s"), WMGetFromPLString(key));
1336 return False;
1339 static WAppIcon *restore_icon_state(WScreen *scr, WMPropList *info, int type, int index)
1341 WAppIcon *aicon;
1342 WMPropList *cmd, *value;
1343 char *wclass, *winstance, *command;
1345 cmd = WMGetFromPLDictionary(info, dCommand);
1346 if (!cmd || !WMIsPLString(cmd))
1347 return NULL;
1349 /* parse window name */
1350 value = WMGetFromPLDictionary(info, dName);
1351 if (!value)
1352 return NULL;
1354 ParseWindowName(value, &winstance, &wclass, "dock");
1356 if (!winstance && !wclass)
1357 return NULL;
1359 /* get commands */
1360 if (cmd)
1361 command = wstrdup(WMGetFromPLString(cmd));
1362 else
1363 command = NULL;
1365 if (!command || strcmp(command, "-") == 0) {
1366 if (command)
1367 wfree(command);
1368 if (wclass)
1369 wfree(wclass);
1370 if (winstance)
1371 wfree(winstance);
1373 return NULL;
1376 aicon = wAppIconCreateForDock(scr, command, winstance, wclass, TILE_NORMAL);
1377 if (wclass)
1378 wfree(wclass);
1379 if (winstance)
1380 wfree(winstance);
1381 if (command)
1382 wfree(command);
1384 aicon->icon->core->descriptor.handle_mousedown = iconMouseDown;
1385 if (type == WM_CLIP) {
1386 aicon->icon->core->descriptor.handle_enternotify = clipEnterNotify;
1387 aicon->icon->core->descriptor.handle_leavenotify = clipLeaveNotify;
1389 aicon->icon->core->descriptor.parent_type = WCLASS_DOCK_ICON;
1390 aicon->icon->core->descriptor.parent = aicon;
1392 #ifdef XDND /* was OFFIX */
1393 cmd = WMGetFromPLDictionary(info, dDropCommand);
1394 if (cmd)
1395 aicon->dnd_command = wstrdup(WMGetFromPLString(cmd));
1396 #endif
1398 cmd = WMGetFromPLDictionary(info, dPasteCommand);
1399 if (cmd)
1400 aicon->paste_command = wstrdup(WMGetFromPLString(cmd));
1402 /* check auto launch */
1403 value = WMGetFromPLDictionary(info, dAutoLaunch);
1405 aicon->auto_launch = getBooleanDockValue(value, dAutoLaunch);
1407 /* check lock */
1408 value = WMGetFromPLDictionary(info, dLock);
1410 aicon->lock = getBooleanDockValue(value, dLock);
1412 /* check if it wasn't normally docked */
1413 value = WMGetFromPLDictionary(info, dForced);
1415 aicon->forced_dock = getBooleanDockValue(value, dForced);
1417 /* check if we can rely on the stuff in the app */
1418 value = WMGetFromPLDictionary(info, dBuggyApplication);
1420 aicon->buggy_app = getBooleanDockValue(value, dBuggyApplication);
1422 /* get position in the dock */
1423 value = WMGetFromPLDictionary(info, dPosition);
1424 if (value && WMIsPLString(value)) {
1425 if (sscanf(WMGetFromPLString(value), "%hi,%hi", &aicon->xindex, &aicon->yindex) != 2)
1426 wwarning(_("bad value in docked icon state info %s"), WMGetFromPLString(dPosition));
1428 /* check position sanity */
1429 /* incomplete section! */
1430 if (type == WM_DOCK) {
1431 aicon->xindex = 0;
1432 if (aicon->yindex < 0)
1433 wwarning(_("bad value in docked icon position %i,%i"),
1434 aicon->xindex, aicon->yindex);
1436 } else {
1437 aicon->yindex = index;
1438 aicon->xindex = 0;
1441 /* check if icon is omnipresent */
1442 value = WMGetFromPLDictionary(info, dOmnipresent);
1444 aicon->omnipresent = getBooleanDockValue(value, dOmnipresent);
1446 aicon->running = 0;
1447 aicon->docked = 1;
1449 return aicon;
1452 #define COMPLAIN(key) wwarning(_("bad value in dock state info:%s"), key)
1454 WAppIcon *wClipRestoreState(WScreen *scr, WMPropList *clip_state)
1456 WAppIcon *icon;
1457 WMPropList *value;
1459 icon = mainIconCreate(scr, WM_CLIP);
1461 if (!clip_state)
1462 return icon;
1464 WMRetainPropList(clip_state);
1466 /* restore position */
1468 value = WMGetFromPLDictionary(clip_state, dPosition);
1470 if (value) {
1471 if (!WMIsPLString(value)) {
1472 COMPLAIN("Position");
1473 } else {
1474 WMRect rect;
1475 int flags;
1477 if (sscanf(WMGetFromPLString(value), "%i,%i", &icon->x_pos, &icon->y_pos) != 2)
1478 COMPLAIN("Position");
1480 /* check position sanity */
1481 rect.pos.x = icon->x_pos;
1482 rect.pos.y = icon->y_pos;
1483 rect.size.width = rect.size.height = ICON_SIZE;
1485 wGetRectPlacementInfo(scr, rect, &flags);
1486 if (flags & (XFLAG_DEAD | XFLAG_PARTIAL))
1487 wScreenKeepInside(scr, &icon->x_pos, &icon->y_pos, ICON_SIZE, ICON_SIZE);
1490 #ifdef XDND /* was OFFIX */
1491 value = WMGetFromPLDictionary(clip_state, dDropCommand);
1492 if (value && WMIsPLString(value))
1493 icon->dnd_command = wstrdup(WMGetFromPLString(value));
1494 #endif
1496 value = WMGetFromPLDictionary(clip_state, dPasteCommand);
1497 if (value && WMIsPLString(value))
1498 icon->paste_command = wstrdup(WMGetFromPLString(value));
1500 WMReleasePropList(clip_state);
1502 return icon;
1505 WDock *wDockRestoreState(WScreen *scr, WMPropList *dock_state, int type)
1507 WDock *dock;
1508 WMPropList *apps;
1509 WMPropList *value;
1510 WAppIcon *aicon, *old_top;
1511 int count, i;
1513 dock = wDockCreate(scr, type);
1515 if (!dock_state)
1516 return dock;
1518 WMRetainPropList(dock_state);
1520 /* restore position */
1521 value = WMGetFromPLDictionary(dock_state, dPosition);
1522 if (value) {
1523 if (!WMIsPLString(value)) {
1524 COMPLAIN("Position");
1525 } else {
1526 WMRect rect;
1527 int flags;
1529 if (sscanf(WMGetFromPLString(value), "%i,%i", &dock->x_pos, &dock->y_pos) != 2)
1530 COMPLAIN("Position");
1532 /* check position sanity */
1533 rect.pos.x = dock->x_pos;
1534 rect.pos.y = dock->y_pos;
1535 rect.size.width = rect.size.height = ICON_SIZE;
1537 wGetRectPlacementInfo(scr, rect, &flags);
1538 if (flags & (XFLAG_DEAD | XFLAG_PARTIAL)) {
1539 int x = dock->x_pos;
1540 wScreenKeepInside(scr, &x, &dock->y_pos, ICON_SIZE, ICON_SIZE);
1543 /* Is this needed any more? */
1544 if (type == WM_CLIP) {
1545 if (dock->x_pos < 0) {
1546 dock->x_pos = 0;
1547 } else if (dock->x_pos > scr->scr_width - ICON_SIZE) {
1548 dock->x_pos = scr->scr_width - ICON_SIZE;
1550 } else {
1551 if (dock->x_pos >= 0) {
1552 dock->x_pos = DOCK_EXTRA_SPACE;
1553 dock->on_right_side = 0;
1554 } else {
1555 dock->x_pos = scr->scr_width - DOCK_EXTRA_SPACE - ICON_SIZE;
1556 dock->on_right_side = 1;
1562 /* restore lowered/raised state */
1563 dock->lowered = 0;
1565 value = WMGetFromPLDictionary(dock_state, dLowered);
1566 if (value) {
1567 if (!WMIsPLString(value)) {
1568 COMPLAIN("Lowered");
1569 } else {
1570 if (strcasecmp(WMGetFromPLString(value), "YES") == 0)
1571 dock->lowered = 1;
1575 /* restore collapsed state */
1576 dock->collapsed = 0;
1578 value = WMGetFromPLDictionary(dock_state, dCollapsed);
1579 if (value) {
1580 if (!WMIsPLString(value)) {
1581 COMPLAIN("Collapsed");
1582 } else {
1583 if (strcasecmp(WMGetFromPLString(value), "YES") == 0)
1584 dock->collapsed = 1;
1588 /* restore auto-collapsed state */
1589 value = WMGetFromPLDictionary(dock_state, dAutoCollapse);
1590 if (value) {
1591 if (!WMIsPLString(value)) {
1592 COMPLAIN("AutoCollapse");
1593 } else {
1594 if (strcasecmp(WMGetFromPLString(value), "YES") == 0) {
1595 dock->auto_collapse = 1;
1596 dock->collapsed = 1;
1601 /* restore auto-raise/lower state */
1602 value = WMGetFromPLDictionary(dock_state, dAutoRaiseLower);
1603 if (value) {
1604 if (!WMIsPLString(value)) {
1605 COMPLAIN("AutoRaiseLower");
1606 } else {
1607 if (strcasecmp(WMGetFromPLString(value), "YES") == 0)
1608 dock->auto_raise_lower = 1;
1612 /* restore attract icons state */
1613 dock->attract_icons = 0;
1615 value = WMGetFromPLDictionary(dock_state, dAutoAttractIcons);
1616 if (value) {
1617 if (!WMIsPLString(value)) {
1618 COMPLAIN("AutoAttractIcons");
1619 } else {
1620 if (strcasecmp(WMGetFromPLString(value), "YES") == 0)
1621 dock->attract_icons = 1;
1625 /* application list */
1628 WMPropList *tmp;
1629 char buffer[64];
1632 * When saving, it saves the dock state in
1633 * Applications and Applicationsnnn
1635 * When loading, it will first try Applicationsnnn.
1636 * If it does not exist, use Applications as default.
1639 snprintf(buffer, sizeof(buffer), "Applications%i", scr->scr_height);
1641 tmp = WMCreatePLString(buffer);
1642 apps = WMGetFromPLDictionary(dock_state, tmp);
1643 WMReleasePropList(tmp);
1645 if (!apps)
1646 apps = WMGetFromPLDictionary(dock_state, dApplications);
1649 if (!apps)
1650 goto finish;
1652 count = WMGetPropListItemCount(apps);
1653 if (count == 0)
1654 goto finish;
1656 old_top = dock->icon_array[0];
1658 /* dock->icon_count is set to 1 when dock is created.
1659 * Since Clip is already restored, we want to keep it so for clip,
1660 * but for dock we may change the default top tile, so we set it to 0.
1662 if (type == WM_DOCK)
1663 dock->icon_count = 0;
1665 for (i = 0; i < count; i++) {
1666 if (dock->icon_count >= dock->max_icons) {
1667 wwarning(_("there are too many icons stored in dock. Ignoring what doesn't fit"));
1668 break;
1671 value = WMGetFromPLArray(apps, i);
1672 aicon = restore_icon_state(scr, value, type, dock->icon_count);
1674 dock->icon_array[dock->icon_count] = aicon;
1676 if (aicon) {
1677 aicon->dock = dock;
1678 aicon->x_pos = dock->x_pos + (aicon->xindex * ICON_SIZE);
1679 aicon->y_pos = dock->y_pos + (aicon->yindex * ICON_SIZE);
1681 if (dock->lowered)
1682 ChangeStackingLevel(aicon->icon->core, WMNormalLevel);
1683 else
1684 ChangeStackingLevel(aicon->icon->core, WMDockLevel);
1686 wCoreConfigure(aicon->icon->core, aicon->x_pos, aicon->y_pos, 0, 0);
1687 if (!dock->collapsed)
1688 XMapWindow(dpy, aicon->icon->core->window);
1690 wRaiseFrame(aicon->icon->core);
1692 dock->icon_count++;
1693 } else if (dock->icon_count == 0 && type == WM_DOCK) {
1694 dock->icon_count++;
1698 /* if the first icon is not defined, use the default */
1699 if (dock->icon_array[0] == NULL) {
1700 /* update default icon */
1701 old_top->x_pos = dock->x_pos;
1702 old_top->y_pos = dock->y_pos;
1703 if (dock->lowered)
1704 ChangeStackingLevel(old_top->icon->core, WMNormalLevel);
1705 else
1706 ChangeStackingLevel(old_top->icon->core, WMDockLevel);
1708 dock->icon_array[0] = old_top;
1709 XMoveWindow(dpy, old_top->icon->core->window, dock->x_pos, dock->y_pos);
1710 /* we don't need to increment dock->icon_count here because it was
1711 * incremented in the loop above.
1713 } else if (old_top != dock->icon_array[0]) {
1714 if (old_top == scr->clip_icon)
1715 scr->clip_icon = dock->icon_array[0];
1717 wAppIconDestroy(old_top);
1720 finish:
1721 WMReleasePropList(dock_state);
1723 return dock;
1726 void wDockLaunchWithState(WDock *dock, WAppIcon *btn, WSavedState *state)
1728 if (btn && btn->command && !btn->running && !btn->launching) {
1729 btn->drop_launch = 0;
1730 btn->paste_launch = 0;
1732 btn->pid = execCommand(btn, btn->command, state);
1734 if (btn->pid > 0) {
1735 if (!btn->forced_dock && !btn->buggy_app) {
1736 btn->launching = 1;
1737 dockIconPaint(btn);
1740 } else {
1741 wfree(state);
1745 void wDockDoAutoLaunch(WDock *dock, int workspace)
1747 WAppIcon *btn;
1748 WSavedState *state;
1749 int i;
1751 for (i = 0; i < dock->max_icons; i++) {
1752 btn = dock->icon_array[i];
1753 if (!btn || !btn->auto_launch)
1754 continue;
1756 state = wmalloc(sizeof(WSavedState));
1757 state->workspace = workspace;
1758 /* TODO: this is klugy and is very difficult to understand
1759 * what's going on. Try to clean up */
1760 wDockLaunchWithState(dock, btn, state);
1764 #ifdef XDND /* was OFFIX */
1765 static WDock *findDock(WScreen *scr, XEvent *event, int *icon_pos)
1767 WDock *dock;
1768 int i;
1770 *icon_pos = -1;
1771 if ((dock = scr->dock) != NULL) {
1772 for (i = 0; i < dock->max_icons; i++) {
1773 if (dock->icon_array[i]
1774 && dock->icon_array[i]->icon->core->window == event->xclient.window) {
1775 *icon_pos = i;
1776 break;
1780 if (*icon_pos < 0 && (dock = scr->workspaces[scr->current_workspace]->clip) != NULL) {
1781 for (i = 0; i < dock->max_icons; i++) {
1782 if (dock->icon_array[i]
1783 && dock->icon_array[i]->icon->core->window == event->xclient.window) {
1784 *icon_pos = i;
1785 break;
1789 if (*icon_pos >= 0)
1790 return dock;
1791 return NULL;
1794 int wDockReceiveDNDDrop(WScreen *scr, XEvent *event)
1796 WDock *dock;
1797 WAppIcon *btn;
1798 int icon_pos;
1800 dock = findDock(scr, event, &icon_pos);
1801 if (!dock)
1802 return False;
1805 * Return True if the drop was on an application icon window.
1806 * In this case, let the ClientMessage handler redirect the
1807 * message to the app.
1809 if (dock->icon_array[icon_pos]->icon->icon_win != None)
1810 return True;
1812 if (dock->icon_array[icon_pos]->dnd_command != NULL) {
1813 scr->flags.dnd_data_convertion_status = 0;
1815 btn = dock->icon_array[icon_pos];
1817 if (!btn->forced_dock) {
1818 btn->relaunching = btn->running;
1819 btn->running = 1;
1821 if (btn->wm_instance || btn->wm_class) {
1822 WWindowAttributes attr;
1823 memset(&attr, 0, sizeof(WWindowAttributes));
1824 wDefaultFillAttributes(btn->wm_instance, btn->wm_class, &attr, NULL, True);
1826 if (!attr.no_appicon)
1827 btn->launching = 1;
1828 else
1829 btn->running = 0;
1832 btn->paste_launch = 0;
1833 btn->drop_launch = 1;
1834 scr->last_dock = dock;
1835 btn->pid = execCommand(btn, btn->dnd_command, NULL);
1836 if (btn->pid > 0) {
1837 dockIconPaint(btn);
1838 } else {
1839 btn->launching = 0;
1840 if (!btn->relaunching)
1841 btn->running = 0;
1844 return False;
1846 #endif /* XDND */
1848 Bool wDockAttachIcon(WDock *dock, WAppIcon *icon, int x, int y, Bool update_icon)
1850 WWindow *wwin;
1851 Bool lupdate_icon = False;
1852 int index;
1854 wwin = icon->icon->owner;
1855 icon->editing = 0;
1857 if (update_icon)
1858 lupdate_icon = True;
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 lupdate_icon = True;
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, lupdate_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;
1977 Bool update_icon = False;
1979 if (src == dest)
1980 return True; /* No move needed, we're already there */
1982 if (dest == NULL)
1983 return False;
1985 wwin = icon->icon->owner;
1988 * For the moment we can't do this if we move icons in Clip from one
1989 * workspace to other, because if we move two or more icons without
1990 * command, the dialog box will not be able to tell us to which of the
1991 * moved icons it applies. -Dan
1993 if ((dest->type == WM_DOCK /*|| dest->keep_attracted */ ) && icon->command == NULL) {
1994 command = GetCommandForWindow(wwin->client_win);
1995 if (command) {
1996 icon->command = command;
1997 } else {
1998 icon->editing = 1;
1999 /* icon->forced_dock = 1; */
2000 if (wInputDialog(src->screen_ptr, _("Dock Icon"),
2001 _("Type the command used to launch the application"), &command)) {
2002 if (command && (command[0] == 0 || (command[0] == '-' && command[1] == 0))) {
2003 wfree(command);
2004 command = NULL;
2006 icon->command = command;
2007 } else {
2008 icon->editing = 0;
2009 if (command)
2010 wfree(command);
2011 return False;
2013 icon->editing = 0;
2017 if (dest->type == WM_DOCK)
2018 wClipMakeIconOmnipresent(icon, False);
2020 for (index = 1; index < src->max_icons; index++) {
2021 if (src->icon_array[index] == icon)
2022 break;
2024 assert(index < src->max_icons);
2026 src->icon_array[index] = NULL;
2027 src->icon_count--;
2029 for (index = 1; index < dest->max_icons; index++) {
2030 if (dest->icon_array[index] == NULL)
2031 break;
2034 assert(index < dest->max_icons);
2036 dest->icon_array[index] = icon;
2037 icon->dock = dest;
2039 /* deselect the icon */
2040 if (icon->icon->selected)
2041 wIconSelect(icon->icon);
2043 if (dest->type == WM_DOCK) {
2044 icon->icon->core->descriptor.handle_enternotify = NULL;
2045 icon->icon->core->descriptor.handle_leavenotify = NULL;
2046 } else {
2047 icon->icon->core->descriptor.handle_enternotify = clipEnterNotify;
2048 icon->icon->core->descriptor.handle_leavenotify = clipLeaveNotify;
2051 /* set it to be kept when moving to dock.
2052 * Unless the icon does not have a command set
2054 if (icon->command && dest->type == WM_DOCK) {
2055 icon->attracted = 0;
2056 if (icon->icon->shadowed) {
2057 icon->icon->shadowed = 0;
2058 update_icon = True;
2060 save_appicon(icon, True);
2063 if (src->auto_collapse || src->auto_raise_lower)
2064 clipLeave(src);
2066 icon->yindex = y;
2067 icon->xindex = x;
2069 icon->x_pos = dest->x_pos + x * ICON_SIZE;
2070 icon->y_pos = dest->y_pos + y * ICON_SIZE;
2072 dest->icon_count++;
2074 MoveInStackListUnder(dest->icon_array[index - 1]->icon->core, icon->icon->core);
2075 wAppIconPaint(icon, update_icon);
2077 return True;
2080 void wDockDetach(WDock *dock, WAppIcon *icon)
2082 int index;
2083 Bool update_icon = False;
2085 /* make the settings panel be closed */
2086 if (icon->panel)
2087 DestroyDockAppSettingsPanel(icon->panel);
2089 /* This must be called before icon->dock is set to NULL.
2090 * Don't move it. -Dan
2092 wClipMakeIconOmnipresent(icon, False);
2094 icon->docked = 0;
2095 icon->dock = NULL;
2096 icon->attracted = 0;
2097 icon->auto_launch = 0;
2098 if (icon->icon->shadowed) {
2099 icon->icon->shadowed = 0;
2100 update_icon = True;
2103 /* deselect the icon */
2104 if (icon->icon->selected)
2105 wIconSelect(icon->icon);
2107 if (icon->command) {
2108 wfree(icon->command);
2109 icon->command = NULL;
2111 #ifdef XDND /* was OFFIX */
2112 if (icon->dnd_command) {
2113 wfree(icon->dnd_command);
2114 icon->dnd_command = NULL;
2116 #endif
2117 if (icon->paste_command) {
2118 wfree(icon->paste_command);
2119 icon->paste_command = NULL;
2122 for (index = 1; index < dock->max_icons; index++)
2123 if (dock->icon_array[index] == icon)
2124 break;
2126 assert(index < dock->max_icons);
2127 dock->icon_array[index] = NULL;
2128 icon->yindex = -1;
2129 icon->xindex = -1;
2131 dock->icon_count--;
2133 /* if the dock is not attached to an application or
2134 * the application did not set the appropriate hints yet,
2135 * destroy the icon */
2136 if (!icon->running || !wApplicationOf(icon->main_window)) {
2137 wAppIconDestroy(icon);
2138 } else {
2139 icon->icon->core->descriptor.handle_mousedown = appIconMouseDown;
2140 icon->icon->core->descriptor.handle_enternotify = NULL;
2141 icon->icon->core->descriptor.handle_leavenotify = NULL;
2142 icon->icon->core->descriptor.parent_type = WCLASS_APPICON;
2143 icon->icon->core->descriptor.parent = icon;
2145 ChangeStackingLevel(icon->icon->core, NORMAL_ICON_LEVEL);
2147 wAppIconPaint(icon, update_icon);
2148 if (wPreferences.auto_arrange_icons)
2149 wArrangeIcons(dock->screen_ptr, True);
2151 if (dock->auto_collapse || dock->auto_raise_lower)
2152 clipLeave(dock);
2156 * returns the closest Dock slot index for the passed
2157 * coordinates.
2159 * Returns False if icon can't be docked.
2161 * Note: this function should NEVER alter ret_x or ret_y, unless it will
2162 * return True. -Dan
2164 Bool wDockSnapIcon(WDock *dock, WAppIcon *icon, int req_x, int req_y, int *ret_x, int *ret_y, int redocking)
2166 WScreen *scr = dock->screen_ptr;
2167 int dx, dy;
2168 int ex_x, ex_y;
2169 int i, offset = ICON_SIZE / 2;
2170 WAppIcon *aicon = NULL;
2171 WAppIcon *nicon = NULL;
2172 int max_y_icons;
2174 /* TODO: XINERAMA, for these */
2175 max_y_icons = scr->scr_height / ICON_SIZE - 1;
2177 if (wPreferences.flags.noupdates)
2178 return False;
2180 dx = dock->x_pos;
2181 dy = dock->y_pos;
2183 /* if the dock is full */
2184 if (!redocking && (dock->icon_count >= dock->max_icons))
2185 return False;
2187 /* exact position */
2188 if (req_y < dy)
2189 ex_y = (req_y - offset - dy) / ICON_SIZE;
2190 else
2191 ex_y = (req_y + offset - dy) / ICON_SIZE;
2193 if (req_x < dx)
2194 ex_x = (req_x - offset - dx) / ICON_SIZE;
2195 else
2196 ex_x = (req_x + offset - dx) / ICON_SIZE;
2198 /* check if the icon is outside the screen boundaries */
2200 WMRect rect;
2201 int flags;
2203 rect.pos.x = dx + ex_x * ICON_SIZE;
2204 rect.pos.y = dy + ex_y * ICON_SIZE;
2205 rect.size.width = rect.size.height = ICON_SIZE;
2207 wGetRectPlacementInfo(scr, rect, &flags);
2208 if (flags & (XFLAG_DEAD | XFLAG_PARTIAL))
2209 return False;
2212 if (dock->type == WM_DOCK) {
2213 if (icon->dock != dock && ex_x != 0)
2214 return False;
2216 aicon = NULL;
2217 for (i = 0; i < dock->max_icons; i++) {
2218 nicon = dock->icon_array[i];
2219 if (nicon && nicon->yindex == ex_y) {
2220 aicon = nicon;
2221 break;
2225 if (redocking) {
2226 int sig, done, closest;
2228 /* Possible cases when redocking:
2230 * icon dragged out of range of any slot -> false
2231 * icon dragged to range of free slot
2232 * icon dragged to range of same slot
2233 * icon dragged to range of different icon
2235 if (abs(ex_x) > DOCK_DETTACH_THRESHOLD)
2236 return False;
2238 if (ex_y >= 0 && ex_y <= max_y_icons && (aicon == icon || !aicon)) {
2239 *ret_x = 0;
2240 *ret_y = ex_y;
2241 return True;
2244 /* start looking at the upper slot or lower? */
2245 if (ex_y * ICON_SIZE < (req_y + offset - dy))
2246 sig = 1;
2247 else
2248 sig = -1;
2250 closest = -1;
2251 done = 0;
2252 /* look for closest free slot */
2253 for (i = 0; i < (DOCK_DETTACH_THRESHOLD + 1) * 2 && !done; i++) {
2254 int j;
2256 done = 1;
2257 closest = sig * (i / 2) + ex_y;
2258 /* check if this slot is used */
2259 if (closest >= 0) {
2260 for (j = 0; j < dock->max_icons; j++) {
2261 if (dock->icon_array[j]
2262 && dock->icon_array[j]->yindex == closest) {
2263 /* slot is used by someone else */
2264 if (dock->icon_array[j] != icon)
2265 done = 0;
2266 break;
2270 sig = -sig;
2272 if (done && closest >= 0 && closest <= max_y_icons &&
2273 ((ex_y >= closest && ex_y - closest < DOCK_DETTACH_THRESHOLD + 1)
2274 || (ex_y < closest && closest - ex_y <= DOCK_DETTACH_THRESHOLD + 1))) {
2275 *ret_x = 0;
2276 *ret_y = closest;
2277 return True;
2279 } else { /* !redocking */
2281 /* if slot is free and the icon is close enough, return it */
2282 if (!aicon && ex_x == 0 && ex_y >= 0 && ex_y <= max_y_icons) {
2283 *ret_x = 0;
2284 *ret_y = ex_y;
2285 return True;
2288 } else { /* CLIP */
2289 int neighbours = 0;
2290 int start, stop, k;
2292 start = icon->omnipresent ? 0 : scr->current_workspace;
2293 stop = icon->omnipresent ? scr->workspace_count : start + 1;
2295 aicon = NULL;
2296 for (k = start; k < stop; k++) {
2297 WDock *tmp = scr->workspaces[k]->clip;
2298 if (!tmp)
2299 continue;
2300 for (i = 0; i < tmp->max_icons; i++) {
2301 nicon = tmp->icon_array[i];
2302 if (nicon && nicon->xindex == ex_x && nicon->yindex == ex_y) {
2303 aicon = nicon;
2304 break;
2307 if (aicon)
2308 break;
2310 for (k = start; k < stop; k++) {
2311 WDock *tmp = scr->workspaces[k]->clip;
2312 if (!tmp)
2313 continue;
2314 for (i = 0; i < tmp->max_icons; i++) {
2315 nicon = tmp->icon_array[i];
2316 if (nicon && nicon != icon && /* Icon can't be it's own neighbour */
2317 (abs(nicon->xindex - ex_x) <= CLIP_ATTACH_VICINITY &&
2318 abs(nicon->yindex - ex_y) <= CLIP_ATTACH_VICINITY)) {
2319 neighbours = 1;
2320 break;
2323 if (neighbours)
2324 break;
2327 if (neighbours && (aicon == NULL || (redocking && aicon == icon))) {
2328 *ret_x = ex_x;
2329 *ret_y = ex_y;
2330 return True;
2333 return False;
2336 static int onScreen(WScreen *scr, int x, int y, int sx, int ex, int sy, int ey)
2338 WMRect rect = wmkrect(x, y, ICON_SIZE, ICON_SIZE);
2339 int flags;
2341 wGetRectPlacementInfo(scr, rect, &flags);
2343 return !(flags & (XFLAG_DEAD | XFLAG_PARTIAL));
2347 * returns true if it can find a free slot in the dock,
2348 * in which case it changes x_pos and y_pos accordingly.
2349 * Else returns false.
2351 Bool wDockFindFreeSlot(WDock *dock, int *x_pos, int *y_pos)
2353 WScreen *scr = dock->screen_ptr;
2354 WAppIcon *btn;
2355 WAppIconChain *chain;
2356 unsigned char *slot_map;
2357 int mwidth;
2358 int r;
2359 int x, y;
2360 int i, done = False;
2361 int corner;
2362 int sx = 0, sy = 0, ex = scr->scr_width, ey = scr->scr_height;
2363 int extra_count = 0;
2365 if (dock->type == WM_CLIP && dock != scr->workspaces[scr->current_workspace]->clip)
2366 extra_count = scr->global_icon_count;
2368 /* if the dock is full */
2369 if (dock->icon_count + extra_count >= dock->max_icons)
2370 return False;
2372 if (!wPreferences.flags.nodock && scr->dock) {
2373 if (scr->dock->on_right_side)
2374 ex -= ICON_SIZE + DOCK_EXTRA_SPACE;
2375 else
2376 sx += ICON_SIZE + DOCK_EXTRA_SPACE;
2379 if (ex < dock->x_pos)
2380 ex = dock->x_pos;
2381 if (sx > dock->x_pos + ICON_SIZE)
2382 sx = dock->x_pos + ICON_SIZE;
2383 #define C_NONE 0
2384 #define C_NW 1
2385 #define C_NE 2
2386 #define C_SW 3
2387 #define C_SE 4
2389 /* check if clip is in a corner */
2390 if (dock->type == WM_CLIP) {
2391 if (dock->x_pos < 1 && dock->y_pos < 1)
2392 corner = C_NE;
2393 else if (dock->x_pos < 1 && dock->y_pos >= (ey - ICON_SIZE))
2394 corner = C_SE;
2395 else if (dock->x_pos >= (ex - ICON_SIZE) && dock->y_pos >= (ey - ICON_SIZE))
2396 corner = C_SW;
2397 else if (dock->x_pos >= (ex - ICON_SIZE) && dock->y_pos < 1)
2398 corner = C_NW;
2399 else
2400 corner = C_NONE;
2401 } else {
2402 corner = C_NONE;
2405 /* If the clip is in the corner, use only slots that are in the border
2406 * of the screen */
2407 if (corner != C_NONE) {
2408 char *hmap, *vmap;
2409 int hcount, vcount;
2411 hcount = WMIN(dock->max_icons, scr->scr_width / ICON_SIZE);
2412 vcount = WMIN(dock->max_icons, scr->scr_height / ICON_SIZE);
2413 hmap = wmalloc(hcount + 1);
2414 vmap = wmalloc(vcount + 1);
2416 /* mark used positions */
2417 switch (corner) {
2418 case C_NE:
2419 for (i = 0; i < dock->max_icons; i++) {
2420 btn = dock->icon_array[i];
2421 if (!btn)
2422 continue;
2424 if (btn->xindex == 0 && btn->yindex > 0 && btn->yindex < vcount)
2425 vmap[btn->yindex] = 1;
2426 else if (btn->yindex == 0 && btn->xindex > 0 && btn->xindex < hcount)
2427 hmap[btn->xindex] = 1;
2429 for (chain = scr->global_icons; chain != NULL; chain = chain->next) {
2430 btn = chain->aicon;
2431 if (btn->xindex == 0 && btn->yindex > 0 && btn->yindex < vcount)
2432 vmap[btn->yindex] = 1;
2433 else if (btn->yindex == 0 && btn->xindex > 0 && btn->xindex < hcount)
2434 hmap[btn->xindex] = 1;
2436 break;
2437 case C_NW:
2438 for (i = 0; i < dock->max_icons; i++) {
2439 btn = dock->icon_array[i];
2440 if (!btn)
2441 continue;
2443 if (btn->xindex == 0 && btn->yindex > 0 && btn->yindex < vcount)
2444 vmap[btn->yindex] = 1;
2445 else if (btn->yindex == 0 && btn->xindex < 0 && btn->xindex > -hcount)
2446 hmap[-btn->xindex] = 1;
2448 for (chain = scr->global_icons; chain != NULL; chain = chain->next) {
2449 btn = chain->aicon;
2450 if (btn->xindex == 0 && btn->yindex > 0 && btn->yindex < vcount)
2451 vmap[btn->yindex] = 1;
2452 else if (btn->yindex == 0 && btn->xindex < 0 && btn->xindex > -hcount)
2453 hmap[-btn->xindex] = 1;
2455 break;
2456 case C_SE:
2457 for (i = 0; i < dock->max_icons; i++) {
2458 btn = dock->icon_array[i];
2459 if (!btn)
2460 continue;
2462 if (btn->xindex == 0 && btn->yindex < 0 && btn->yindex > -vcount)
2463 vmap[-btn->yindex] = 1;
2464 else if (btn->yindex == 0 && btn->xindex > 0 && btn->xindex < hcount)
2465 hmap[btn->xindex] = 1;
2467 for (chain = scr->global_icons; chain != NULL; chain = chain->next) {
2468 btn = chain->aicon;
2469 if (btn->xindex == 0 && btn->yindex < 0 && btn->yindex > -vcount)
2470 vmap[-btn->yindex] = 1;
2471 else if (btn->yindex == 0 && btn->xindex > 0 && btn->xindex < hcount)
2472 hmap[btn->xindex] = 1;
2474 break;
2475 case C_SW:
2476 default:
2477 for (i = 0; i < dock->max_icons; i++) {
2478 btn = dock->icon_array[i];
2479 if (!btn)
2480 continue;
2482 if (btn->xindex == 0 && btn->yindex < 0 && btn->yindex > -vcount)
2483 vmap[-btn->yindex] = 1;
2484 else if (btn->yindex == 0 && btn->xindex < 0 && btn->xindex > -hcount)
2485 hmap[-btn->xindex] = 1;
2487 for (chain = scr->global_icons; chain != NULL; chain = chain->next) {
2488 btn = chain->aicon;
2489 if (btn->xindex == 0 && btn->yindex < 0 && btn->yindex > -vcount)
2490 vmap[-btn->yindex] = 1;
2491 else if (btn->yindex == 0 && btn->xindex < 0 && btn->xindex > -hcount)
2492 hmap[-btn->xindex] = 1;
2495 x = 0;
2496 y = 0;
2497 done = 0;
2498 /* search a vacant slot */
2499 for (i = 1; i < WMAX(vcount, hcount); i++) {
2500 if (i < vcount && vmap[i] == 0) {
2501 /* found a slot */
2502 x = 0;
2503 y = i;
2504 done = 1;
2505 break;
2506 } else if (i < hcount && hmap[i] == 0) {
2507 /* found a slot */
2508 x = i;
2509 y = 0;
2510 done = 1;
2511 break;
2514 wfree(vmap);
2515 wfree(hmap);
2516 /* If found a slot, translate and return */
2517 if (done) {
2518 if (corner == C_NW || corner == C_NE)
2519 *y_pos = y;
2520 else
2521 *y_pos = -y;
2523 if (corner == C_NE || corner == C_SE)
2524 *x_pos = x;
2525 else
2526 *x_pos = -x;
2528 return True;
2530 /* else, try to find a slot somewhere else */
2533 /* a map of mwidth x mwidth would be enough if we allowed icons to be
2534 * placed outside of screen */
2535 mwidth = (int)ceil(sqrt(dock->max_icons));
2537 /* In the worst case (the clip is in the corner of the screen),
2538 * the amount of icons that fit in the clip is smaller.
2539 * Double the map to get a safe value.
2541 mwidth += mwidth;
2543 r = (mwidth - 1) / 2;
2545 slot_map = wmalloc(mwidth * mwidth);
2547 #define XY2OFS(x,y) (WMAX(abs(x),abs(y)) > r) ? 0 : (((y)+r)*(mwidth)+(x)+r)
2549 /* mark used slots in the map. If the slot falls outside the map
2550 * (for example, when all icons are placed in line), ignore them. */
2551 for (i = 0; i < dock->max_icons; i++) {
2552 btn = dock->icon_array[i];
2553 if (btn)
2554 slot_map[XY2OFS(btn->xindex, btn->yindex)] = 1;
2557 for (chain = scr->global_icons; chain != NULL; chain = chain->next)
2558 slot_map[XY2OFS(chain->aicon->xindex, chain->aicon->yindex)] = 1;
2560 /* Find closest slot from the center that is free by scanning the
2561 * map from the center to outward in circular passes.
2562 * This will not result in a neat layout, but will be optimal
2563 * in the sense that there will not be holes left.
2565 done = 0;
2566 for (i = 1; i <= r && !done; i++) {
2567 int tx, ty;
2569 /* top and bottom parts of the ring */
2570 for (x = -i; x <= i && !done; x++) {
2571 tx = dock->x_pos + x * ICON_SIZE;
2572 y = -i;
2573 ty = dock->y_pos + y * ICON_SIZE;
2574 if (slot_map[XY2OFS(x, y)] == 0 && onScreen(scr, tx, ty, sx, ex, sy, ey)) {
2575 *x_pos = x;
2576 *y_pos = y;
2577 done = 1;
2578 break;
2580 y = i;
2581 ty = dock->y_pos + y * ICON_SIZE;
2582 if (slot_map[XY2OFS(x, y)] == 0 && onScreen(scr, tx, ty, sx, ex, sy, ey)) {
2583 *x_pos = x;
2584 *y_pos = y;
2585 done = 1;
2586 break;
2589 /* left and right parts of the ring */
2590 for (y = -i + 1; y <= i - 1; y++) {
2591 ty = dock->y_pos + y * ICON_SIZE;
2592 x = -i;
2593 tx = dock->x_pos + x * ICON_SIZE;
2594 if (slot_map[XY2OFS(x, y)] == 0 && onScreen(scr, tx, ty, sx, ex, sy, ey)) {
2595 *x_pos = x;
2596 *y_pos = y;
2597 done = 1;
2598 break;
2600 x = i;
2601 tx = dock->x_pos + x * ICON_SIZE;
2602 if (slot_map[XY2OFS(x, y)] == 0 && onScreen(scr, tx, ty, sx, ex, sy, ey)) {
2603 *x_pos = x;
2604 *y_pos = y;
2605 done = 1;
2606 break;
2610 wfree(slot_map);
2611 #undef XY2OFS
2612 return done;
2615 static void moveDock(WDock *dock, int new_x, int new_y)
2617 WAppIcon *btn;
2618 int i;
2620 dock->x_pos = new_x;
2621 dock->y_pos = new_y;
2622 for (i = 0; i < dock->max_icons; i++) {
2623 btn = dock->icon_array[i];
2624 if (btn) {
2625 btn->x_pos = new_x + btn->xindex * ICON_SIZE;
2626 btn->y_pos = new_y + btn->yindex * ICON_SIZE;
2627 XMoveWindow(dpy, btn->icon->core->window, btn->x_pos, btn->y_pos);
2632 static void swapDock(WDock *dock)
2634 WScreen *scr = dock->screen_ptr;
2635 WAppIcon *btn;
2636 int x, i;
2638 if (dock->on_right_side)
2639 x = dock->x_pos = scr->scr_width - ICON_SIZE - DOCK_EXTRA_SPACE;
2640 else
2641 x = dock->x_pos = DOCK_EXTRA_SPACE;
2643 for (i = 0; i < dock->max_icons; i++) {
2644 btn = dock->icon_array[i];
2645 if (btn) {
2646 btn->x_pos = x;
2647 XMoveWindow(dpy, btn->icon->core->window, btn->x_pos, btn->y_pos);
2651 wScreenUpdateUsableArea(scr);
2654 static pid_t execCommand(WAppIcon *btn, char *command, WSavedState *state)
2656 WScreen *scr = btn->icon->core->screen_ptr;
2657 pid_t pid;
2658 char **argv;
2659 int argc;
2660 char *cmdline;
2662 cmdline = ExpandOptions(scr, command);
2664 if (scr->flags.dnd_data_convertion_status || !cmdline) {
2665 if (cmdline)
2666 wfree(cmdline);
2667 if (state)
2668 wfree(state);
2669 return 0;
2672 wtokensplit(cmdline, &argv, &argc);
2674 if (!argc) {
2675 if (cmdline)
2676 wfree(cmdline);
2677 if (state)
2678 wfree(state);
2679 return 0;
2682 if ((pid = fork()) == 0) {
2683 char **args;
2684 int i;
2686 SetupEnvironment(scr);
2688 #ifdef HAVE_SETSID
2689 setsid();
2690 #endif
2692 args = malloc(sizeof(char *) * (argc + 1));
2693 if (!args)
2694 exit(111);
2696 for (i = 0; i < argc; i++)
2697 args[i] = argv[i];
2699 args[argc] = NULL;
2700 execvp(argv[0], args);
2701 exit(111);
2703 wtokenfree(argv, argc);
2705 if (pid > 0) {
2706 if (!state) {
2707 state = wmalloc(sizeof(WSavedState));
2708 state->hidden = -1;
2709 state->miniaturized = -1;
2710 state->shaded = -1;
2711 if (btn->dock == scr->dock || btn->omnipresent)
2712 state->workspace = -1;
2713 else
2714 state->workspace = scr->current_workspace;
2716 wWindowAddSavedState(btn->wm_instance, btn->wm_class, cmdline, pid, state);
2717 wAddDeathHandler(pid, (WDeathHandler *) trackDeadProcess, btn->dock);
2718 } else if (state) {
2719 wfree(state);
2721 wfree(cmdline);
2722 return pid;
2725 void wDockHideIcons(WDock *dock)
2727 int i;
2729 if (dock == NULL)
2730 return;
2732 for (i = 1; i < dock->max_icons; i++) {
2733 if (dock->icon_array[i])
2734 XUnmapWindow(dpy, dock->icon_array[i]->icon->core->window);
2736 dock->mapped = 0;
2738 dockIconPaint(dock->icon_array[0]);
2741 void wDockShowIcons(WDock *dock)
2743 int i, newlevel;
2744 WAppIcon *btn;
2746 if (dock == NULL)
2747 return;
2749 btn = dock->icon_array[0];
2750 moveDock(dock, btn->x_pos, btn->y_pos);
2752 newlevel = dock->lowered ? WMNormalLevel : WMDockLevel;
2753 ChangeStackingLevel(btn->icon->core, newlevel);
2755 for (i = 1; i < dock->max_icons; i++) {
2756 if (dock->icon_array[i]) {
2757 MoveInStackListAbove(dock->icon_array[i]->icon->core, btn->icon->core);
2758 break;
2762 if (!dock->collapsed) {
2763 for (i = 1; i < dock->max_icons; i++) {
2764 if (dock->icon_array[i])
2765 XMapWindow(dpy, dock->icon_array[i]->icon->core->window);
2768 dock->mapped = 1;
2770 dockIconPaint(btn);
2773 void wDockLower(WDock *dock)
2775 int i;
2777 for (i = 0; i < dock->max_icons; i++) {
2778 if (dock->icon_array[i])
2779 wLowerFrame(dock->icon_array[i]->icon->core);
2783 void wDockRaise(WDock *dock)
2785 int i;
2787 for (i = dock->max_icons - 1; i >= 0; i--) {
2788 if (dock->icon_array[i])
2789 wRaiseFrame(dock->icon_array[i]->icon->core);
2793 void wDockRaiseLower(WDock *dock)
2795 if (!dock->icon_array[0]->icon->core->stacking->above
2796 || (dock->icon_array[0]->icon->core->stacking->window_level
2797 != dock->icon_array[0]->icon->core->stacking->above->stacking->window_level))
2798 wDockLower(dock);
2799 else
2800 wDockRaise(dock);
2803 void wDockFinishLaunch(WDock *dock, WAppIcon *icon)
2805 icon->launching = 0;
2806 icon->relaunching = 0;
2807 dockIconPaint(icon);
2810 WAppIcon *wDockFindIconForWindow(WDock *dock, Window window)
2812 WAppIcon *icon;
2813 int i;
2815 for (i = 0; i < dock->max_icons; i++) {
2816 icon = dock->icon_array[i];
2817 if (icon && icon->main_window == window)
2818 return icon;
2820 return NULL;
2823 void wDockTrackWindowLaunch(WDock *dock, Window window)
2825 WAppIcon *icon;
2826 char *wm_class, *wm_instance;
2827 int i;
2828 Bool firstPass = True;
2829 Bool found = False;
2830 char *command = NULL;
2832 if (!PropGetWMClass(window, &wm_class, &wm_instance) || (!wm_class && !wm_instance))
2833 return;
2835 command = GetCommandForWindow(window);
2836 retry:
2837 for (i = 0; i < dock->max_icons; i++) {
2838 icon = dock->icon_array[i];
2839 if (!icon)
2840 continue;
2842 /* app is already attached to icon */
2843 if (icon->main_window == window) {
2844 found = True;
2845 break;
2848 if ((icon->wm_instance || icon->wm_class)
2849 && (icon->launching || !icon->running)) {
2851 if (icon->wm_instance && wm_instance && strcmp(icon->wm_instance, wm_instance) != 0)
2852 continue;
2854 if (icon->wm_class && wm_class && strcmp(icon->wm_class, wm_class) != 0)
2855 continue;
2857 if (firstPass && command && strcmp(icon->command, command) != 0)
2858 continue;
2860 if (!icon->relaunching) {
2861 WApplication *wapp;
2863 /* Possibly an application that was docked with dockit,
2864 * but the user did not update WMState to indicate that
2865 * it was docked by force */
2866 wapp = wApplicationOf(window);
2867 if (!wapp) {
2868 icon->forced_dock = 1;
2869 icon->running = 0;
2871 if (!icon->forced_dock)
2872 icon->main_window = window;
2874 found = True;
2875 if (!wPreferences.no_animations && !icon->launching &&
2876 !dock->screen_ptr->flags.startup && !dock->collapsed) {
2877 WAppIcon *aicon;
2878 int x0, y0;
2880 icon->launching = 1;
2881 dockIconPaint(icon);
2883 aicon = wAppIconCreateForDock(dock->screen_ptr, NULL,
2884 wm_instance, wm_class, TILE_NORMAL);
2885 /* XXX: can: aicon->icon == NULL ? */
2886 PlaceIcon(dock->screen_ptr, &x0, &y0, wGetHeadForWindow(aicon->icon->owner));
2887 wAppIconMove(aicon, x0, y0);
2888 /* Should this always be lowered? -Dan */
2889 if (dock->lowered)
2890 wLowerFrame(aicon->icon->core);
2891 XMapWindow(dpy, aicon->icon->core->window);
2892 aicon->launching = 1;
2893 wAppIconPaint(aicon, False);
2894 SlideWindow(aicon->icon->core->window, x0, y0, icon->x_pos, icon->y_pos);
2895 XUnmapWindow(dpy, aicon->icon->core->window);
2896 wAppIconDestroy(aicon);
2898 wDockFinishLaunch(dock, icon);
2899 break;
2903 if (firstPass && !found) {
2904 firstPass = False;
2905 goto retry;
2908 if (command)
2909 wfree(command);
2911 if (wm_class)
2912 free(wm_class);
2913 if (wm_instance)
2914 free(wm_instance);
2917 void wClipUpdateForWorkspaceChange(WScreen *scr, int workspace)
2919 if (!wPreferences.flags.noclip) {
2920 scr->clip_icon->dock = scr->workspaces[workspace]->clip;
2921 if (scr->current_workspace != workspace) {
2922 WDock *old_clip = scr->workspaces[scr->current_workspace]->clip;
2923 WAppIconChain *chain = scr->global_icons;
2925 while (chain) {
2926 moveIconBetweenDocks(chain->aicon->dock,
2927 scr->workspaces[workspace]->clip,
2928 chain->aicon, chain->aicon->xindex, chain->aicon->yindex);
2929 if (scr->workspaces[workspace]->clip->collapsed)
2930 XUnmapWindow(dpy, chain->aicon->icon->core->window);
2931 chain = chain->next;
2934 wDockHideIcons(old_clip);
2935 if (old_clip->auto_raise_lower) {
2936 if (old_clip->auto_raise_magic) {
2937 WMDeleteTimerHandler(old_clip->auto_raise_magic);
2938 old_clip->auto_raise_magic = NULL;
2940 wDockLower(old_clip);
2942 if (old_clip->auto_collapse) {
2943 if (old_clip->auto_expand_magic) {
2944 WMDeleteTimerHandler(old_clip->auto_expand_magic);
2945 old_clip->auto_expand_magic = NULL;
2947 old_clip->collapsed = 1;
2949 wDockShowIcons(scr->workspaces[workspace]->clip);
2954 static void trackDeadProcess(pid_t pid, unsigned char status, WDock *dock)
2956 WAppIcon *icon;
2957 int i;
2959 for (i = 0; i < dock->max_icons; i++) {
2960 icon = dock->icon_array[i];
2961 if (!icon)
2962 continue;
2964 if (icon->launching && icon->pid == pid) {
2965 if (!icon->relaunching) {
2966 icon->running = 0;
2967 icon->main_window = None;
2969 wDockFinishLaunch(dock, icon);
2970 icon->pid = 0;
2971 if (status == 111) {
2972 char msg[PATH_MAX];
2973 char *cmd;
2975 #ifdef XDND
2976 if (icon->drop_launch)
2977 cmd = icon->dnd_command;
2978 else
2979 #endif
2980 if (icon->paste_launch)
2981 cmd = icon->paste_command;
2982 else
2983 cmd = icon->command;
2985 snprintf(msg, sizeof(msg), _("Could not execute command \"%s\""), cmd);
2987 wMessageDialog(dock->screen_ptr, _("Error"), msg, _("OK"), NULL, NULL);
2989 break;
2994 static void toggleLowered(WDock *dock)
2996 WAppIcon *tmp;
2997 int newlevel, i;
2999 /* lower/raise Dock */
3000 if (!dock->lowered) {
3001 newlevel = WMNormalLevel;
3002 dock->lowered = 1;
3003 } else {
3004 newlevel = WMDockLevel;
3005 dock->lowered = 0;
3008 for (i = 0; i < dock->max_icons; i++) {
3009 tmp = dock->icon_array[i];
3010 if (!tmp)
3011 continue;
3013 ChangeStackingLevel(tmp->icon->core, newlevel);
3014 if (dock->lowered)
3015 wLowerFrame(tmp->icon->core);
3018 if (dock->type == WM_DOCK)
3019 wScreenUpdateUsableArea(dock->screen_ptr);
3022 static void toggleCollapsed(WDock *dock)
3024 if (dock->collapsed) {
3025 dock->collapsed = 0;
3026 wDockShowIcons(dock);
3027 } else {
3028 dock->collapsed = 1;
3029 wDockHideIcons(dock);
3033 static void openDockMenu(WDock *dock, WAppIcon *aicon, XEvent *event)
3035 WScreen *scr = dock->screen_ptr;
3036 WObjDescriptor *desc;
3037 WMenuEntry *entry;
3038 WApplication *wapp = NULL;
3039 int index = 0;
3040 int x_pos;
3041 int n_selected;
3042 int appIsRunning = aicon->running && aicon->icon && aicon->icon->owner;
3044 if (dock->type == WM_DOCK) {
3045 /* keep on top */
3046 entry = dock->menu->entries[index];
3047 entry->flags.indicator_on = !dock->lowered;
3048 entry->clientdata = dock;
3049 dock->menu->flags.realized = 0;
3050 } else {
3051 /* clip options */
3052 if (scr->clip_options)
3053 updateClipOptionsMenu(scr->clip_options, dock);
3055 n_selected = numberOfSelectedIcons(dock);
3057 /* Rename Workspace */
3058 entry = dock->menu->entries[++index];
3059 if (aicon == scr->clip_icon) {
3060 entry->callback = renameCallback;
3061 entry->clientdata = dock;
3062 entry->flags.indicator = 0;
3063 entry->text = _("Rename Workspace");
3064 } else {
3065 entry->callback = omnipresentCallback;
3066 entry->clientdata = aicon;
3067 if (n_selected > 0) {
3068 entry->flags.indicator = 0;
3069 entry->text = _("Toggle Omnipresent");
3070 } else {
3071 entry->flags.indicator = 1;
3072 entry->flags.indicator_on = aicon->omnipresent;
3073 entry->flags.indicator_type = MI_CHECK;
3074 entry->text = _("Omnipresent");
3078 /* select/unselect icon */
3079 entry = dock->menu->entries[++index];
3080 entry->clientdata = aicon;
3081 entry->flags.indicator_on = aicon->icon->selected;
3082 wMenuSetEnabled(dock->menu, index, aicon != scr->clip_icon);
3084 /* select/unselect all icons */
3085 entry = dock->menu->entries[++index];
3086 entry->clientdata = aicon;
3087 if (n_selected > 0)
3088 entry->text = _("Unselect All Icons");
3089 else
3090 entry->text = _("Select All Icons");
3092 wMenuSetEnabled(dock->menu, index, dock->icon_count > 1);
3094 /* keep icon(s) */
3095 entry = dock->menu->entries[++index];
3096 entry->clientdata = aicon;
3097 if (n_selected > 1)
3098 entry->text = _("Keep Icons");
3099 else
3100 entry->text = _("Keep Icon");
3102 wMenuSetEnabled(dock->menu, index, dock->icon_count > 1);
3104 /* this is the workspace submenu part */
3105 entry = dock->menu->entries[++index];
3106 if (n_selected > 1)
3107 entry->text = _("Move Icons To");
3108 else
3109 entry->text = _("Move Icon To");
3111 if (scr->clip_submenu)
3112 updateWorkspaceMenu(scr->clip_submenu, aicon);
3114 wMenuSetEnabled(dock->menu, index, !aicon->omnipresent);
3116 /* remove icon(s) */
3117 entry = dock->menu->entries[++index];
3118 entry->clientdata = aicon;
3119 if (n_selected > 1)
3120 entry->text = _("Remove Icons");
3121 else
3122 entry->text = _("Remove Icon");
3124 wMenuSetEnabled(dock->menu, index, dock->icon_count > 1);
3126 /* attract icon(s) */
3127 entry = dock->menu->entries[++index];
3128 entry->clientdata = aicon;
3130 dock->menu->flags.realized = 0;
3131 wMenuRealize(dock->menu);
3134 if (aicon->icon->owner)
3135 wapp = wApplicationOf(aicon->icon->owner->main_window);
3136 else
3137 wapp = NULL;
3139 /* launch */
3140 entry = dock->menu->entries[++index];
3141 entry->clientdata = aicon;
3142 wMenuSetEnabled(dock->menu, index, aicon->command != NULL);
3144 /* unhide here */
3145 entry = dock->menu->entries[++index];
3146 entry->clientdata = aicon;
3147 if (wapp && wapp->flags.hidden)
3148 entry->text = _("Unhide Here");
3149 else
3150 entry->text = _("Bring Here");
3152 wMenuSetEnabled(dock->menu, index, appIsRunning);
3154 /* hide */
3155 entry = dock->menu->entries[++index];
3156 entry->clientdata = aicon;
3157 if (wapp && wapp->flags.hidden)
3158 entry->text = _("Unhide");
3159 else
3160 entry->text = _("Hide");
3162 wMenuSetEnabled(dock->menu, index, appIsRunning);
3164 /* settings */
3165 entry = dock->menu->entries[++index];
3166 entry->clientdata = aicon;
3167 wMenuSetEnabled(dock->menu, index, !aicon->editing && !wPreferences.flags.noupdates);
3169 /* kill */
3170 entry = dock->menu->entries[++index];
3171 entry->clientdata = aicon;
3172 wMenuSetEnabled(dock->menu, index, appIsRunning);
3174 if (!dock->menu->flags.realized)
3175 wMenuRealize(dock->menu);
3177 if (dock->type == WM_CLIP) {
3178 /*x_pos = event->xbutton.x_root+2; */
3179 x_pos = event->xbutton.x_root - dock->menu->frame->core->width / 2 - 1;
3180 if (x_pos < 0) {
3181 x_pos = 0;
3182 } else if (x_pos + dock->menu->frame->core->width > scr->scr_width - 2) {
3183 x_pos = scr->scr_width - dock->menu->frame->core->width - 4;
3185 } else {
3186 x_pos = dock->on_right_side ? scr->scr_width - dock->menu->frame->core->width - 3 : 0;
3189 wMenuMapAt(dock->menu, x_pos, event->xbutton.y_root + 2, False);
3191 /* allow drag select */
3192 event->xany.send_event = True;
3193 desc = &dock->menu->menu->descriptor;
3194 (*desc->handle_mousedown) (desc, event);
3197 /******************************************************************/
3198 static void iconDblClick(WObjDescriptor *desc, XEvent *event)
3200 WAppIcon *btn = desc->parent;
3201 WDock *dock = btn->dock;
3202 WApplication *wapp = NULL;
3203 int unhideHere = 0;
3205 if (btn->icon->owner && !(event->xbutton.state & ControlMask)) {
3206 wapp = wApplicationOf(btn->icon->owner->main_window);
3208 assert(wapp != NULL);
3210 unhideHere = (event->xbutton.state & ShiftMask);
3212 /* go to the last workspace that the user worked on the app */
3213 if (wapp->last_workspace != dock->screen_ptr->current_workspace && !unhideHere)
3214 wWorkspaceChange(dock->screen_ptr, wapp->last_workspace);
3216 wUnhideApplication(wapp, event->xbutton.button == Button2, unhideHere);
3218 if (event->xbutton.state & MOD_MASK)
3219 wHideOtherApplications(btn->icon->owner);
3220 } else {
3221 if (event->xbutton.button == Button1) {
3222 if (event->xbutton.state & MOD_MASK) {
3223 /* raise/lower dock */
3224 toggleLowered(dock);
3225 } else if (btn == dock->screen_ptr->clip_icon) {
3226 if (getClipButton(event->xbutton.x, event->xbutton.y) == CLIP_IDLE)
3227 toggleCollapsed(dock);
3228 else
3229 handleClipChangeWorkspace(dock->screen_ptr, event);
3230 } else if (btn->command) {
3231 if (!btn->launching && (!btn->running || (event->xbutton.state & ControlMask)))
3232 launchDockedApplication(btn, False);
3238 static void handleDockMove(WDock *dock, WAppIcon *aicon, XEvent *event)
3240 WScreen *scr = dock->screen_ptr;
3241 int ofs_x = event->xbutton.x, ofs_y = event->xbutton.y;
3242 int x, y;
3243 XEvent ev;
3244 int grabbed = 0, swapped = 0, done;
3245 Pixmap ghost = None;
3246 int superfluous = wPreferences.superfluous; /* we catch it to avoid problems */
3248 if (XGrabPointer(dpy, aicon->icon->core->window, True, ButtonMotionMask
3249 | ButtonReleaseMask | ButtonPressMask, GrabModeAsync,
3250 GrabModeAsync, None, None, CurrentTime) != GrabSuccess)
3251 wwarning("pointer grab failed for dock move");
3253 y = 0;
3254 for (x = 0; x < dock->max_icons; x++) {
3255 if (dock->icon_array[x] != NULL && dock->icon_array[x]->yindex > y)
3256 y = dock->icon_array[x]->yindex;
3258 y++;
3259 XResizeWindow(dpy, scr->dock_shadow, ICON_SIZE, ICON_SIZE * y);
3261 done = 0;
3262 while (!done) {
3263 WMMaskEvent(dpy, PointerMotionMask | ButtonReleaseMask | ButtonPressMask
3264 | ButtonMotionMask | ExposureMask | EnterWindowMask, &ev);
3265 switch (ev.type) {
3266 case Expose:
3267 WMHandleEvent(&ev);
3268 break;
3270 case EnterNotify:
3271 /* It means the cursor moved so fast that it entered
3272 * something else (if moving slowly, it would have
3273 * stayed in the dock that is being moved. Ignore such
3274 * "spurious" EnterNotifiy's */
3275 break;
3277 case MotionNotify:
3278 if (!grabbed) {
3279 if (abs(ofs_x - ev.xmotion.x) >= MOVE_THRESHOLD
3280 || abs(ofs_y - ev.xmotion.y) >= MOVE_THRESHOLD) {
3281 XChangeActivePointerGrab(dpy, ButtonMotionMask
3282 | ButtonReleaseMask | ButtonPressMask,
3283 wCursor[WCUR_MOVE], CurrentTime);
3284 grabbed = 1;
3286 break;
3288 if (dock->type == WM_CLIP) {
3289 x = ev.xmotion.x_root - ofs_x;
3290 y = ev.xmotion.y_root - ofs_y;
3291 wScreenKeepInside(scr, &x, &y, ICON_SIZE, ICON_SIZE);
3293 moveDock(dock, x, y);
3294 } else {
3295 /* move vertically if pointer is inside the dock */
3296 if ((dock->on_right_side && ev.xmotion.x_root >= dock->x_pos - ICON_SIZE)
3297 || (!dock->on_right_side && ev.xmotion.x_root <= dock->x_pos + ICON_SIZE * 2)) {
3299 x = ev.xmotion.x_root - ofs_x;
3300 y = ev.xmotion.y_root - ofs_y;
3301 wScreenKeepInside(scr, &x, &y, ICON_SIZE, ICON_SIZE);
3302 moveDock(dock, dock->x_pos, y);
3304 /* move horizontally to change sides */
3305 x = ev.xmotion.x_root - ofs_x;
3306 if (!dock->on_right_side) {
3308 /* is on left */
3309 if (ev.xmotion.x_root > dock->x_pos + ICON_SIZE * 2) {
3310 XMoveWindow(dpy, scr->dock_shadow, scr->scr_width - ICON_SIZE
3311 - DOCK_EXTRA_SPACE - 1, dock->y_pos);
3312 if (superfluous && ghost == None) {
3313 ghost = MakeGhostDock(dock, dock->x_pos,
3314 scr->scr_width - ICON_SIZE
3315 - DOCK_EXTRA_SPACE - 1, dock->y_pos);
3316 XSetWindowBackgroundPixmap(dpy, scr->dock_shadow, ghost);
3317 XClearWindow(dpy, scr->dock_shadow);
3319 XMapRaised(dpy, scr->dock_shadow);
3320 swapped = 1;
3321 } else {
3322 if (superfluous && ghost != None) {
3323 XFreePixmap(dpy, ghost);
3324 ghost = None;
3326 XUnmapWindow(dpy, scr->dock_shadow);
3327 swapped = 0;
3329 } else {
3330 /* is on right */
3331 if (ev.xmotion.x_root < dock->x_pos - ICON_SIZE) {
3332 XMoveWindow(dpy, scr->dock_shadow, DOCK_EXTRA_SPACE, dock->y_pos);
3333 if (superfluous && ghost == None) {
3334 ghost = MakeGhostDock(dock, dock->x_pos,
3335 DOCK_EXTRA_SPACE, dock->y_pos);
3336 XSetWindowBackgroundPixmap(dpy, scr->dock_shadow, ghost);
3337 XClearWindow(dpy, scr->dock_shadow);
3339 XMapRaised(dpy, scr->dock_shadow);
3340 swapped = -1;
3341 } else {
3342 XUnmapWindow(dpy, scr->dock_shadow);
3343 swapped = 0;
3344 if (superfluous && ghost != None) {
3345 XFreePixmap(dpy, ghost);
3346 ghost = None;
3351 break;
3353 case ButtonPress:
3354 break;
3356 case ButtonRelease:
3357 if (ev.xbutton.button != event->xbutton.button)
3358 break;
3359 XUngrabPointer(dpy, CurrentTime);
3360 XUnmapWindow(dpy, scr->dock_shadow);
3361 XResizeWindow(dpy, scr->dock_shadow, ICON_SIZE, ICON_SIZE);
3362 if (dock->type == WM_DOCK) {
3363 if (swapped != 0) {
3364 if (swapped > 0)
3365 dock->on_right_side = 1;
3366 else
3367 dock->on_right_side = 0;
3368 swapDock(dock);
3369 wArrangeIcons(scr, False);
3372 done = 1;
3373 break;
3376 if (superfluous) {
3377 if (ghost != None)
3378 XFreePixmap(dpy, ghost);
3379 XSetWindowBackground(dpy, scr->dock_shadow, scr->white_pixel);
3383 static Bool handleIconMove(WDock *dock, WAppIcon *aicon, XEvent *event)
3385 WScreen *scr = dock->screen_ptr;
3386 Window wins[2];
3387 WIcon *icon = aicon->icon;
3388 WDock *dock2 = NULL, *last_dock = dock, *clip = NULL;
3389 int ondock, grabbed = 0, change_dock = 0, collapsed = 0;
3390 XEvent ev;
3391 int x = aicon->x_pos, y = aicon->y_pos;
3392 int ofs_x = event->xbutton.x, ofs_y = event->xbutton.y;
3393 int shad_x = x, shad_y = y;
3394 int ix = aicon->xindex, iy = aicon->yindex;
3395 int tmp;
3396 Pixmap ghost = None;
3397 Bool docked;
3398 int superfluous = wPreferences.superfluous; /* we catch it to avoid problems */
3399 int omnipresent = aicon->omnipresent; /* this must be cached!!! */
3400 Bool hasMoved = False;
3402 if (wPreferences.flags.noupdates)
3403 return hasMoved;
3405 if (XGrabPointer(dpy, icon->core->window, True, ButtonMotionMask
3406 | ButtonReleaseMask | ButtonPressMask, GrabModeAsync,
3407 GrabModeAsync, None, None, CurrentTime) != GrabSuccess) {
3410 if (!(event->xbutton.state & MOD_MASK))
3411 wRaiseFrame(icon->core);
3413 if (!wPreferences.flags.noclip)
3414 clip = scr->workspaces[scr->current_workspace]->clip;
3416 if (dock == scr->dock && !wPreferences.flags.noclip)
3417 dock2 = clip;
3418 else if (dock != scr->dock && !wPreferences.flags.nodock)
3419 dock2 = scr->dock;
3421 wins[0] = icon->core->window;
3422 wins[1] = scr->dock_shadow;
3423 XRestackWindows(dpy, wins, 2);
3424 XMoveResizeWindow(dpy, scr->dock_shadow, aicon->x_pos, aicon->y_pos, ICON_SIZE, ICON_SIZE);
3425 if (superfluous) {
3426 if (icon->pixmap != None)
3427 ghost = MakeGhostIcon(scr, icon->pixmap);
3428 else
3429 ghost = MakeGhostIcon(scr, icon->core->window);
3431 XSetWindowBackgroundPixmap(dpy, scr->dock_shadow, ghost);
3432 XClearWindow(dpy, scr->dock_shadow);
3434 XMapWindow(dpy, scr->dock_shadow);
3436 ondock = 1;
3438 while (1) {
3439 XMaskEvent(dpy, PointerMotionMask | ButtonReleaseMask | ButtonPressMask
3440 | ButtonMotionMask | ExposureMask, &ev);
3441 switch (ev.type) {
3442 case Expose:
3443 WMHandleEvent(&ev);
3444 break;
3446 case MotionNotify:
3447 hasMoved = True;
3448 if (!grabbed) {
3449 if (abs(ofs_x - ev.xmotion.x) >= MOVE_THRESHOLD
3450 || abs(ofs_y - ev.xmotion.y) >= MOVE_THRESHOLD) {
3451 XChangeActivePointerGrab(dpy, ButtonMotionMask
3452 | ButtonReleaseMask | ButtonPressMask,
3453 wCursor[WCUR_MOVE], CurrentTime);
3454 grabbed = 1;
3455 } else {
3456 break;
3460 if (omnipresent) {
3461 int i;
3462 for (i = 0; i < scr->workspace_count; i++) {
3463 if (i == scr->current_workspace)
3464 continue;
3465 wDockShowIcons(scr->workspaces[i]->clip);
3469 x = ev.xmotion.x_root - ofs_x;
3470 y = ev.xmotion.y_root - ofs_y;
3471 tmp = wDockSnapIcon(dock, aicon, x, y, &ix, &iy, True);
3472 if (tmp && dock2) {
3473 change_dock = 0;
3474 if (last_dock != dock && collapsed) {
3475 last_dock->collapsed = 1;
3476 wDockHideIcons(last_dock);
3477 collapsed = 0;
3479 if (!collapsed && (collapsed = dock->collapsed)) {
3480 dock->collapsed = 0;
3481 wDockShowIcons(dock);
3483 if (dock->auto_raise_lower)
3484 wDockRaise(dock);
3485 last_dock = dock;
3486 } else if (dock2) {
3487 tmp = wDockSnapIcon(dock2, aicon, x, y, &ix, &iy, False);
3488 if (tmp) {
3489 change_dock = 1;
3490 if (last_dock != dock2 && collapsed) {
3491 last_dock->collapsed = 1;
3492 wDockHideIcons(last_dock);
3493 collapsed = 0;
3495 if (!collapsed && (collapsed = dock2->collapsed)) {
3496 dock2->collapsed = 0;
3497 wDockShowIcons(dock2);
3499 if (dock2->auto_raise_lower)
3500 wDockRaise(dock2);
3501 last_dock = dock2;
3504 if (aicon->launching || aicon->lock || (aicon->running && !(ev.xmotion.state & MOD_MASK))
3505 || (!aicon->running && tmp)) {
3506 shad_x = last_dock->x_pos + ix * wPreferences.icon_size;
3507 shad_y = last_dock->y_pos + iy * wPreferences.icon_size;
3509 XMoveWindow(dpy, scr->dock_shadow, shad_x, shad_y);
3511 if (!ondock)
3512 XMapWindow(dpy, scr->dock_shadow);
3514 ondock = 1;
3515 } else {
3516 if (ondock)
3517 XUnmapWindow(dpy, scr->dock_shadow);
3519 ondock = 0;
3521 XMoveWindow(dpy, icon->core->window, x, y);
3522 break;
3524 case ButtonPress:
3525 break;
3527 case ButtonRelease:
3528 if (ev.xbutton.button != event->xbutton.button)
3529 break;
3530 XUngrabPointer(dpy, CurrentTime);
3531 if (ondock) {
3532 SlideWindow(icon->core->window, x, y, shad_x, shad_y);
3533 XUnmapWindow(dpy, scr->dock_shadow);
3534 if (!change_dock) {
3535 reattachIcon(dock, aicon, ix, iy);
3536 if (clip && dock != clip && clip->auto_raise_lower)
3537 wDockLower(clip);
3538 } else {
3539 docked = moveIconBetweenDocks(dock, dock2, aicon, ix, iy);
3540 if (!docked) {
3541 /* Slide it back if dock rejected it */
3542 SlideWindow(icon->core->window, x, y, aicon->x_pos, aicon->y_pos);
3543 reattachIcon(dock, aicon, aicon->xindex, aicon->yindex);
3545 if (last_dock->type == WM_CLIP && last_dock->auto_collapse)
3546 collapsed = 0;
3548 } else {
3549 aicon->x_pos = x;
3550 aicon->y_pos = y;
3551 if (superfluous) {
3552 if (!aicon->running && !wPreferences.no_animations) {
3553 /* We need to deselect it, even if is deselected in
3554 * wDockDetach(), because else DoKaboom() will fail.
3556 if (aicon->icon->selected)
3557 wIconSelect(aicon->icon);
3559 DoKaboom(scr, aicon->icon->core->window, x, y);
3562 if (clip && clip->auto_raise_lower)
3563 wDockLower(clip);
3564 wDockDetach(dock, aicon);
3566 if (collapsed) {
3567 last_dock->collapsed = 1;
3568 wDockHideIcons(last_dock);
3569 collapsed = 0;
3571 if (superfluous) {
3572 if (ghost != None)
3573 XFreePixmap(dpy, ghost);
3574 XSetWindowBackground(dpy, scr->dock_shadow, scr->white_pixel);
3576 if (omnipresent) {
3577 int i;
3578 for (i = 0; i < scr->workspace_count; i++) {
3579 if (i == scr->current_workspace)
3580 continue;
3581 wDockHideIcons(scr->workspaces[i]->clip);
3584 return hasMoved;;
3587 return False; /* never reached */
3590 static int getClipButton(int px, int py)
3592 int pt = (CLIP_BUTTON_SIZE + 2) * ICON_SIZE / 64;
3594 if (px < 0 || py < 0 || px >= ICON_SIZE || py >= ICON_SIZE)
3595 return CLIP_IDLE;
3597 if (py <= pt - ((int)ICON_SIZE - 1 - px))
3598 return CLIP_FORWARD;
3599 else if (px <= pt - ((int)ICON_SIZE - 1 - py))
3600 return CLIP_REWIND;
3602 return CLIP_IDLE;
3605 static void handleClipChangeWorkspace(WScreen *scr, XEvent *event)
3607 XEvent ev;
3608 int done, direction, new_ws;
3609 int new_dir;
3610 WDock *clip = scr->clip_icon->dock;
3612 direction = getClipButton(event->xbutton.x, event->xbutton.y);
3614 clip->lclip_button_pushed = direction == CLIP_REWIND;
3615 clip->rclip_button_pushed = direction == CLIP_FORWARD;
3617 wClipIconPaint(scr->clip_icon);
3618 done = 0;
3619 while (!done) {
3620 WMMaskEvent(dpy, ExposureMask | ButtonMotionMask | ButtonReleaseMask | ButtonPressMask, &ev);
3621 switch (ev.type) {
3622 case Expose:
3623 WMHandleEvent(&ev);
3624 break;
3626 case MotionNotify:
3627 new_dir = getClipButton(ev.xmotion.x, ev.xmotion.y);
3628 if (new_dir != direction) {
3629 direction = new_dir;
3630 clip->lclip_button_pushed = direction == CLIP_REWIND;
3631 clip->rclip_button_pushed = direction == CLIP_FORWARD;
3632 wClipIconPaint(scr->clip_icon);
3634 break;
3636 case ButtonPress:
3637 break;
3639 case ButtonRelease:
3640 if (ev.xbutton.button == event->xbutton.button)
3641 done = 1;
3645 clip->lclip_button_pushed = 0;
3646 clip->rclip_button_pushed = 0;
3648 new_ws = wPreferences.ws_advance || (event->xbutton.state & ControlMask);
3650 if (direction == CLIP_FORWARD) {
3651 if (scr->current_workspace < scr->workspace_count - 1)
3652 wWorkspaceChange(scr, scr->current_workspace + 1);
3653 else if (new_ws && scr->current_workspace < MAX_WORKSPACES - 1)
3654 wWorkspaceChange(scr, scr->current_workspace + 1);
3655 else if (wPreferences.ws_cycle)
3656 wWorkspaceChange(scr, 0);
3657 } else if (direction == CLIP_REWIND) {
3658 if (scr->current_workspace > 0)
3659 wWorkspaceChange(scr, scr->current_workspace - 1);
3660 else if (scr->current_workspace == 0 && wPreferences.ws_cycle)
3661 wWorkspaceChange(scr, scr->workspace_count - 1);
3664 wClipIconPaint(scr->clip_icon);
3667 static void iconMouseDown(WObjDescriptor *desc, XEvent *event)
3669 WAppIcon *aicon = desc->parent;
3670 WDock *dock = aicon->dock;
3671 WScreen *scr = aicon->icon->core->screen_ptr;
3673 if (aicon->editing || WCHECK_STATE(WSTATE_MODAL))
3674 return;
3676 scr->last_dock = dock;
3678 if (dock->menu->flags.mapped)
3679 wMenuUnmap(dock->menu);
3681 if (IsDoubleClick(scr, event)) {
3682 /* double-click was not in the main clip icon */
3683 if (dock->type != WM_CLIP || aicon->xindex != 0 || aicon->yindex != 0
3684 || getClipButton(event->xbutton.x, event->xbutton.y) == CLIP_IDLE) {
3685 iconDblClick(desc, event);
3686 return;
3690 if (event->xbutton.button == Button1) {
3691 if (event->xbutton.state & MOD_MASK)
3692 wDockLower(dock);
3693 else
3694 wDockRaise(dock);
3696 if ((event->xbutton.state & ShiftMask) && aicon != scr->clip_icon && dock->type != WM_DOCK) {
3697 wIconSelect(aicon->icon);
3698 return;
3701 if (aicon->yindex == 0 && aicon->xindex == 0) {
3702 if (getClipButton(event->xbutton.x, event->xbutton.y) != CLIP_IDLE
3703 && dock->type == WM_CLIP)
3704 handleClipChangeWorkspace(scr, event);
3705 else
3706 handleDockMove(dock, aicon, event);
3707 } else {
3708 Bool hasMoved = handleIconMove(dock, aicon, event);
3709 if (wPreferences.single_click && !hasMoved)
3710 iconDblClick(desc, event);
3712 } else if (event->xbutton.button == Button2 && dock->type == WM_CLIP && aicon == scr->clip_icon) {
3713 if (!scr->clip_ws_menu) {
3714 scr->clip_ws_menu = wWorkspaceMenuMake(scr, False);
3716 if (scr->clip_ws_menu) {
3717 WMenu *wsMenu = scr->clip_ws_menu;
3718 int xpos;
3720 wWorkspaceMenuUpdate(scr, wsMenu);
3722 xpos = event->xbutton.x_root - wsMenu->frame->core->width / 2 - 1;
3723 if (xpos < 0) {
3724 xpos = 0;
3725 } else if (xpos + wsMenu->frame->core->width > scr->scr_width - 2) {
3726 xpos = scr->scr_width - wsMenu->frame->core->width - 4;
3728 wMenuMapAt(wsMenu, xpos, event->xbutton.y_root + 2, False);
3730 desc = &wsMenu->menu->descriptor;
3731 event->xany.send_event = True;
3732 (*desc->handle_mousedown) (desc, event);
3734 } else if (event->xbutton.button == Button2 && dock->type == WM_CLIP &&
3735 (event->xbutton.state & ShiftMask) && aicon != scr->clip_icon) {
3736 wClipMakeIconOmnipresent(aicon, !aicon->omnipresent);
3737 } else if (event->xbutton.button == Button3) {
3738 if (event->xbutton.send_event &&
3739 XGrabPointer(dpy, aicon->icon->core->window, True, ButtonMotionMask
3740 | ButtonReleaseMask | ButtonPressMask, GrabModeAsync,
3741 GrabModeAsync, None, None, CurrentTime) != GrabSuccess) {
3742 wwarning("pointer grab failed for dockicon menu");
3743 return;
3746 openDockMenu(dock, aicon, event);
3747 } else if (event->xbutton.button == Button2) {
3748 WAppIcon *btn = desc->parent;
3750 if (!btn->launching && (!btn->running || (event->xbutton.state & ControlMask)))
3751 launchDockedApplication(btn, True);
3755 static void clipEnterNotify(WObjDescriptor *desc, XEvent *event)
3757 WAppIcon *btn = (WAppIcon *) desc->parent;
3758 WDock *dock;
3759 WScreen *scr;
3761 assert(event->type == EnterNotify);
3763 if (desc->parent_type != WCLASS_DOCK_ICON)
3764 return;
3766 scr = btn->icon->core->screen_ptr;
3767 if (!btn->omnipresent)
3768 dock = btn->dock;
3769 else
3770 dock = scr->workspaces[scr->current_workspace]->clip;
3772 if (!dock || dock->type != WM_CLIP)
3773 return;
3775 /* The auto raise/lower code */
3776 if (dock->auto_lower_magic) {
3777 WMDeleteTimerHandler(dock->auto_lower_magic);
3778 dock->auto_lower_magic = NULL;
3780 if (dock->auto_raise_lower && !dock->auto_raise_magic)
3781 dock->auto_raise_magic = WMAddTimerHandler(AUTO_RAISE_DELAY, clipAutoRaise, (void *)dock);
3783 /* The auto expand/collapse code */
3784 if (dock->auto_collapse_magic) {
3785 WMDeleteTimerHandler(dock->auto_collapse_magic);
3786 dock->auto_collapse_magic = NULL;
3788 if (dock->auto_collapse && !dock->auto_expand_magic)
3789 dock->auto_expand_magic = WMAddTimerHandler(AUTO_EXPAND_DELAY, clipAutoExpand, (void *)dock);
3792 static void clipLeave(WDock *dock)
3794 XEvent event;
3795 WObjDescriptor *desc = NULL;
3797 if (!dock || dock->type != WM_CLIP)
3798 return;
3800 if (XCheckTypedEvent(dpy, EnterNotify, &event) != False) {
3801 if (XFindContext(dpy, event.xcrossing.window, wWinContext,
3802 (XPointer *) & desc) != XCNOENT
3803 && desc && desc->parent_type == WCLASS_DOCK_ICON
3804 && ((WAppIcon *) desc->parent)->dock && ((WAppIcon *) desc->parent)->dock->type == WM_CLIP) {
3805 /* We didn't left the Clip yet */
3806 XPutBackEvent(dpy, &event);
3807 return;
3810 XPutBackEvent(dpy, &event);
3811 } else {
3812 /* We entered a withdrawn window, so we're still in Clip */
3813 return;
3816 if (dock->auto_raise_magic) {
3817 WMDeleteTimerHandler(dock->auto_raise_magic);
3818 dock->auto_raise_magic = NULL;
3820 if (dock->auto_raise_lower && !dock->auto_lower_magic)
3821 dock->auto_lower_magic = WMAddTimerHandler(AUTO_LOWER_DELAY, clipAutoLower, (void *)dock);
3823 if (dock->auto_expand_magic) {
3824 WMDeleteTimerHandler(dock->auto_expand_magic);
3825 dock->auto_expand_magic = NULL;
3827 if (dock->auto_collapse && !dock->auto_collapse_magic)
3828 dock->auto_collapse_magic = WMAddTimerHandler(AUTO_COLLAPSE_DELAY, clipAutoCollapse, (void *)dock);
3831 static void clipLeaveNotify(WObjDescriptor *desc, XEvent *event)
3833 WAppIcon *btn = (WAppIcon *) desc->parent;
3835 assert(event->type == LeaveNotify);
3837 if (desc->parent_type != WCLASS_DOCK_ICON)
3838 return;
3840 clipLeave(btn->dock);
3843 static void clipAutoCollapse(void *cdata)
3845 WDock *dock = (WDock *) cdata;
3847 if (dock->type != WM_CLIP)
3848 return;
3850 if (dock->auto_collapse) {
3851 dock->collapsed = 1;
3852 wDockHideIcons(dock);
3854 dock->auto_collapse_magic = NULL;
3857 static void clipAutoExpand(void *cdata)
3859 WDock *dock = (WDock *) cdata;
3861 if (dock->type != WM_CLIP)
3862 return;
3864 if (dock->auto_collapse) {
3865 dock->collapsed = 0;
3866 wDockShowIcons(dock);
3868 dock->auto_expand_magic = NULL;
3871 static void clipAutoLower(void *cdata)
3873 WDock *dock = (WDock *) cdata;
3875 if (dock->type != WM_CLIP)
3876 return;
3878 if (dock->auto_raise_lower)
3879 wDockLower(dock);
3881 dock->auto_lower_magic = NULL;
3884 static void clipAutoRaise(void *cdata)
3886 WDock *dock = (WDock *) cdata;
3888 if (dock->type != WM_CLIP)
3889 return;
3891 if (dock->auto_raise_lower)
3892 wDockRaise(dock);
3894 dock->auto_raise_magic = NULL;
3897 static Bool iconCanBeOmnipresent(WAppIcon *aicon)
3899 WScreen *scr = aicon->icon->core->screen_ptr;
3900 WDock *clip;
3901 WAppIcon *btn;
3902 int i, j;
3904 for (i = 0; i < scr->workspace_count; i++) {
3905 clip = scr->workspaces[i]->clip;
3907 if (clip == aicon->dock)
3908 continue;
3910 if (clip->icon_count + scr->global_icon_count >= clip->max_icons)
3911 return False; /* Clip is full in some workspace */
3913 for (j = 0; j < clip->max_icons; j++) {
3914 btn = clip->icon_array[j];
3915 if (btn && btn->xindex == aicon->xindex && btn->yindex == aicon->yindex)
3916 return False;
3920 return True;
3923 int wClipMakeIconOmnipresent(WAppIcon *aicon, int omnipresent)
3925 WScreen *scr = aicon->icon->core->screen_ptr;
3926 WAppIconChain *new_entry, *tmp, *tmp1;
3927 int status = WO_SUCCESS;
3929 if ((scr->dock && aicon->dock == scr->dock) || aicon == scr->clip_icon)
3930 return WO_NOT_APPLICABLE;
3932 if (aicon->omnipresent == omnipresent)
3933 return WO_SUCCESS;
3935 if (omnipresent) {
3936 if (iconCanBeOmnipresent(aicon)) {
3937 aicon->omnipresent = 1;
3938 new_entry = wmalloc(sizeof(WAppIconChain));
3939 new_entry->aicon = aicon;
3940 new_entry->next = scr->global_icons;
3941 scr->global_icons = new_entry;
3942 scr->global_icon_count++;
3943 } else {
3944 aicon->omnipresent = 0;
3945 status = WO_FAILED;
3947 } else {
3948 aicon->omnipresent = 0;
3949 if (aicon == scr->global_icons->aicon) {
3950 tmp = scr->global_icons->next;
3951 wfree(scr->global_icons);
3952 scr->global_icons = tmp;
3953 scr->global_icon_count--;
3954 } else {
3955 tmp = scr->global_icons;
3956 while (tmp->next) {
3957 if (tmp->next->aicon == aicon) {
3958 tmp1 = tmp->next->next;
3959 wfree(tmp->next);
3960 tmp->next = tmp1;
3961 scr->global_icon_count--;
3962 break;
3964 tmp = tmp->next;
3969 wAppIconPaint(aicon, False);
3971 return status;