Add OPEN_PLMENU option to parse command generated proplist style menus
[wmaker-crm.git] / src / dock.c
blob21d6aeec334bbb1beaff07481c0e313267480ae9
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);
538 if (aicon && aicon->attracted && aicon->command) {
539 aicon->attracted = 0;
540 if (aicon->icon->shadowed) {
541 aicon->icon->shadowed = 0;
543 /* Update the icon images */
544 wIconUpdate(aicon->icon, NULL);
546 /* Paint it */
547 wAppIconPaint(aicon);
550 save_appicon(aicon, True);
552 WMFreeArray(selectedIcons);
555 static void toggleAutoAttractCallback(WMenu *menu, WMenuEntry *entry)
557 WDock *dock = (WDock *) entry->clientdata;
559 assert(entry->clientdata != NULL);
561 dock->attract_icons = !dock->attract_icons;
563 entry->flags.indicator_on = dock->attract_icons;
565 wMenuPaint(menu);
568 static void selectCallback(WMenu *menu, WMenuEntry *entry)
570 WAppIcon *icon = (WAppIcon *) entry->clientdata;
572 assert(icon != NULL);
574 wIconSelect(icon->icon);
576 wMenuPaint(menu);
579 static void colectIconsCallback(WMenu *menu, WMenuEntry *entry)
581 WAppIcon *clickedIcon = (WAppIcon *) entry->clientdata;
582 WDock *clip;
583 WAppIcon *aicon;
584 int x, y, x_pos, y_pos;
585 Bool update_icon = False;
587 assert(entry->clientdata != NULL);
588 clip = clickedIcon->dock;
590 aicon = clip->screen_ptr->app_icon_list;
592 while (aicon) {
593 if (!aicon->docked && wDockFindFreeSlot(clip, &x, &y)) {
594 x_pos = clip->x_pos + x * ICON_SIZE;
595 y_pos = clip->y_pos + y * ICON_SIZE;
596 if (aicon->x_pos != x_pos || aicon->y_pos != y_pos)
597 move_window(aicon->icon->core->window, aicon->x_pos, aicon->y_pos, x_pos, y_pos);
599 aicon->attracted = 1;
600 if (!aicon->icon->shadowed) {
601 aicon->icon->shadowed = 1;
602 update_icon = True;
604 wDockAttachIcon(clip, aicon, x, y, update_icon);
605 if (clip->collapsed || !clip->mapped)
606 XUnmapWindow(dpy, aicon->icon->core->window);
608 aicon = aicon->next;
612 static void selectIconsCallback(WMenu *menu, WMenuEntry *entry)
614 WAppIcon *clickedIcon = (WAppIcon *) entry->clientdata;
615 WDock *dock;
616 WMArray *selectedIcons;
617 WMArrayIterator iter;
618 WAppIcon *btn;
619 int i;
621 assert(clickedIcon != NULL);
622 dock = clickedIcon->dock;
624 selectedIcons = getSelected(dock);
626 if (!WMGetArrayItemCount(selectedIcons)) {
627 for (i = 1; i < dock->max_icons; i++) {
628 btn = dock->icon_array[i];
629 if (btn && !btn->icon->selected)
630 wIconSelect(btn->icon);
632 } else {
633 WM_ITERATE_ARRAY(selectedIcons, btn, iter) {
634 wIconSelect(btn->icon);
637 WMFreeArray(selectedIcons);
639 wMenuPaint(menu);
642 static void toggleCollapsedCallback(WMenu *menu, WMenuEntry *entry)
644 assert(entry->clientdata != NULL);
646 toggleCollapsed(entry->clientdata);
648 entry->flags.indicator_on = ((WDock *) entry->clientdata)->collapsed;
650 wMenuPaint(menu);
653 static void toggleAutoCollapseCallback(WMenu *menu, WMenuEntry *entry)
655 WDock *dock;
656 assert(entry->clientdata != NULL);
658 dock = (WDock *) entry->clientdata;
660 dock->auto_collapse = !dock->auto_collapse;
662 entry->flags.indicator_on = ((WDock *) entry->clientdata)->auto_collapse;
664 wMenuPaint(menu);
667 static void toggleAutoRaiseLowerCallback(WMenu *menu, WMenuEntry *entry)
669 WDock *dock;
670 assert(entry->clientdata != NULL);
672 dock = (WDock *) entry->clientdata;
674 dock->auto_raise_lower = !dock->auto_raise_lower;
676 entry->flags.indicator_on = ((WDock *) entry->clientdata)->auto_raise_lower;
678 wMenuPaint(menu);
681 static void launchCallback(WMenu *menu, WMenuEntry *entry)
683 WAppIcon *btn = (WAppIcon *) entry->clientdata;
685 launchDockedApplication(btn, False);
688 static void settingsCallback(WMenu *menu, WMenuEntry *entry)
690 WAppIcon *btn = (WAppIcon *) entry->clientdata;
692 if (btn->editing)
693 return;
694 ShowDockAppSettingsPanel(btn);
697 static void hideCallback(WMenu *menu, WMenuEntry *entry)
699 WApplication *wapp;
700 WAppIcon *btn = (WAppIcon *) entry->clientdata;
702 wapp = wApplicationOf(btn->icon->owner->main_window);
704 if (wapp->flags.hidden) {
705 wWorkspaceChange(btn->icon->core->screen_ptr, wapp->last_workspace);
706 wUnhideApplication(wapp, False, False);
707 } else {
708 wHideApplication(wapp);
712 static void unhideHereCallback(WMenu *menu, WMenuEntry *entry)
714 WApplication *wapp;
715 WAppIcon *btn = (WAppIcon *) entry->clientdata;
717 wapp = wApplicationOf(btn->icon->owner->main_window);
719 wUnhideApplication(wapp, False, True);
722 static WAppIcon *mainIconCreate(WScreen *scr, int type)
724 WAppIcon *btn;
725 int x_pos;
727 if (type == WM_CLIP) {
728 if (scr->clip_icon)
729 return scr->clip_icon;
730 btn = wAppIconCreateForDock(scr, NULL, "Logo", "WMClip", TILE_CLIP);
731 btn->icon->core->descriptor.handle_expose = clipIconExpose;
732 btn->icon->core->descriptor.handle_enternotify = clipEnterNotify;
733 btn->icon->core->descriptor.handle_leavenotify = clipLeaveNotify;
734 x_pos = 0;
735 } else {
736 btn = wAppIconCreateForDock(scr, NULL, "Logo", "WMDock", TILE_NORMAL);
737 x_pos = scr->scr_width - ICON_SIZE - DOCK_EXTRA_SPACE;
740 btn->xindex = 0;
741 btn->yindex = 0;
743 btn->icon->core->descriptor.handle_mousedown = iconMouseDown;
744 btn->icon->core->descriptor.parent_type = WCLASS_DOCK_ICON;
745 btn->icon->core->descriptor.parent = btn;
746 XMapWindow(dpy, btn->icon->core->window);
747 btn->x_pos = x_pos;
748 btn->y_pos = 0;
749 btn->docked = 1;
750 if (type == WM_CLIP)
751 scr->clip_icon = btn;
753 return btn;
756 static void switchWSCommand(WMenu *menu, WMenuEntry *entry)
758 WAppIcon *btn, *icon = (WAppIcon *) entry->clientdata;
759 WScreen *scr = icon->icon->core->screen_ptr;
760 WDock *src, *dest;
761 WMArray *selectedIcons;
762 int x, y;
764 if (entry->order == scr->current_workspace)
765 return;
766 src = icon->dock;
767 dest = scr->workspaces[entry->order]->clip;
769 selectedIcons = getSelected(src);
771 if (WMGetArrayItemCount(selectedIcons)) {
772 WMArrayIterator iter;
774 WM_ITERATE_ARRAY(selectedIcons, btn, iter) {
775 if (wDockFindFreeSlot(dest, &x, &y)) {
776 moveIconBetweenDocks(src, dest, btn, x, y);
777 XUnmapWindow(dpy, btn->icon->core->window);
780 } else if (icon != scr->clip_icon) {
781 if (wDockFindFreeSlot(dest, &x, &y)) {
782 moveIconBetweenDocks(src, dest, icon, x, y);
783 XUnmapWindow(dpy, icon->icon->core->window);
786 WMFreeArray(selectedIcons);
789 static void launchDockedApplication(WAppIcon *btn, Bool withSelection)
791 WScreen *scr = btn->icon->core->screen_ptr;
793 if (!btn->launching &&
794 ((!withSelection && btn->command != NULL) || (withSelection && btn->paste_command != NULL))) {
795 if (!btn->forced_dock) {
796 btn->relaunching = btn->running;
797 btn->running = 1;
799 if (btn->wm_instance || btn->wm_class) {
800 WWindowAttributes attr;
801 memset(&attr, 0, sizeof(WWindowAttributes));
802 wDefaultFillAttributes(btn->wm_instance, btn->wm_class, &attr, NULL, True);
804 if (!attr.no_appicon && !btn->buggy_app)
805 btn->launching = 1;
806 else
807 btn->running = 0;
809 btn->drop_launch = 0;
810 btn->paste_launch = withSelection;
811 scr->last_dock = btn->dock;
812 btn->pid = execCommand(btn, (withSelection ? btn->paste_command : btn->command), NULL);
813 if (btn->pid > 0) {
814 if (btn->buggy_app) {
815 /* give feedback that the app was launched */
816 btn->launching = 1;
817 dockIconPaint(btn);
818 btn->launching = 0;
819 WMAddTimerHandler(200, (WMCallback *) dockIconPaint, btn);
820 } else {
821 dockIconPaint(btn);
823 } else {
824 wwarning(_("could not launch application %s"), btn->command);
825 btn->launching = 0;
826 if (!btn->relaunching)
827 btn->running = 0;
832 static void updateWorkspaceMenu(WMenu *menu, WAppIcon *icon)
834 WScreen *scr = menu->frame->screen_ptr;
835 char title[MAX_WORKSPACENAME_WIDTH + 1];
836 int i;
838 if (!menu || !icon)
839 return;
841 for (i = 0; i < scr->workspace_count; i++) {
842 if (i < menu->entry_no) {
843 if (strcmp(menu->entries[i]->text, scr->workspaces[i]->name) != 0) {
844 wfree(menu->entries[i]->text);
845 strcpy(title, scr->workspaces[i]->name);
846 menu->entries[i]->text = wstrdup(title);
847 menu->flags.realized = 0;
849 menu->entries[i]->clientdata = (void *)icon;
850 } else {
851 strcpy(title, scr->workspaces[i]->name);
853 wMenuAddCallback(menu, title, switchWSCommand, (void *)icon);
855 menu->flags.realized = 0;
858 if (i == scr->current_workspace)
859 wMenuSetEnabled(menu, i, False);
860 else
861 wMenuSetEnabled(menu, i, True);
864 if (!menu->flags.realized)
865 wMenuRealize(menu);
868 static WMenu *makeWorkspaceMenu(WScreen *scr)
870 WMenu *menu;
872 menu = wMenuCreate(scr, NULL, False);
873 if (!menu)
874 wwarning(_("could not create workspace submenu for Clip menu"));
876 wMenuAddCallback(menu, "", switchWSCommand, (void *)scr->clip_icon);
878 menu->flags.realized = 0;
879 wMenuRealize(menu);
881 return menu;
884 static void updateClipOptionsMenu(WMenu *menu, WDock *dock)
886 WMenuEntry *entry;
887 int index = 0;
889 if (!menu || !dock)
890 return;
892 /* keep on top */
893 entry = menu->entries[index];
894 entry->flags.indicator_on = !dock->lowered;
895 entry->clientdata = dock;
897 /* collapsed */
898 entry = menu->entries[++index];
899 entry->flags.indicator_on = dock->collapsed;
900 entry->clientdata = dock;
902 /* auto-collapse */
903 entry = menu->entries[++index];
904 entry->flags.indicator_on = dock->auto_collapse;
905 entry->clientdata = dock;
907 /* auto-raise/lower */
908 entry = menu->entries[++index];
909 entry->flags.indicator_on = dock->auto_raise_lower;
910 entry->clientdata = dock;
911 wMenuSetEnabled(menu, index, dock->lowered);
913 /* attract icons */
914 entry = menu->entries[++index];
915 entry->flags.indicator_on = dock->attract_icons;
916 entry->clientdata = dock;
918 menu->flags.realized = 0;
919 wMenuRealize(menu);
922 static WMenu *makeClipOptionsMenu(WScreen *scr)
924 WMenu *menu;
925 WMenuEntry *entry;
927 menu = wMenuCreate(scr, NULL, False);
928 if (!menu) {
929 wwarning(_("could not create options submenu for Clip menu"));
930 return NULL;
933 entry = wMenuAddCallback(menu, _("Keep on Top"), toggleLoweredCallback, NULL);
934 entry->flags.indicator = 1;
935 entry->flags.indicator_on = 1;
936 entry->flags.indicator_type = MI_CHECK;
938 entry = wMenuAddCallback(menu, _("Collapsed"), toggleCollapsedCallback, NULL);
939 entry->flags.indicator = 1;
940 entry->flags.indicator_on = 1;
941 entry->flags.indicator_type = MI_CHECK;
943 entry = wMenuAddCallback(menu, _("Autocollapse"), toggleAutoCollapseCallback, NULL);
944 entry->flags.indicator = 1;
945 entry->flags.indicator_on = 1;
946 entry->flags.indicator_type = MI_CHECK;
948 entry = wMenuAddCallback(menu, _("Autoraise"), toggleAutoRaiseLowerCallback, NULL);
949 entry->flags.indicator = 1;
950 entry->flags.indicator_on = 1;
951 entry->flags.indicator_type = MI_CHECK;
953 entry = wMenuAddCallback(menu, _("Autoattract Icons"), toggleAutoAttractCallback, NULL);
954 entry->flags.indicator = 1;
955 entry->flags.indicator_on = 1;
956 entry->flags.indicator_type = MI_CHECK;
958 menu->flags.realized = 0;
959 wMenuRealize(menu);
961 return menu;
964 static WMenu *dockMenuCreate(WScreen *scr, int type)
966 WMenu *menu;
967 WMenuEntry *entry;
969 if (type == WM_CLIP && scr->clip_menu)
970 return scr->clip_menu;
972 menu = wMenuCreate(scr, NULL, False);
973 if (type != WM_CLIP) {
974 entry = wMenuAddCallback(menu, _("Keep on Top"), toggleLoweredCallback, NULL);
975 entry->flags.indicator = 1;
976 entry->flags.indicator_on = 1;
977 entry->flags.indicator_type = MI_CHECK;
978 } else {
979 entry = wMenuAddCallback(menu, _("Clip Options"), NULL, NULL);
980 scr->clip_options = makeClipOptionsMenu(scr);
981 if (scr->clip_options)
982 wMenuEntrySetCascade(menu, entry, scr->clip_options);
984 entry = wMenuAddCallback(menu, _("Rename Workspace"), renameCallback, NULL);
985 wfree(entry->text);
986 entry->text = _("Rename Workspace");
988 entry = wMenuAddCallback(menu, _("Selected"), selectCallback, NULL);
989 entry->flags.indicator = 1;
990 entry->flags.indicator_on = 1;
991 entry->flags.indicator_type = MI_CHECK;
993 entry = wMenuAddCallback(menu, _("Select All Icons"), selectIconsCallback, NULL);
994 wfree(entry->text);
995 entry->text = _("Select All Icons");
997 entry = wMenuAddCallback(menu, _("Keep Icon"), keepIconsCallback, NULL);
998 wfree(entry->text);
999 entry->text = _("Keep Icon");
1001 entry = wMenuAddCallback(menu, _("Move Icon To"), NULL, NULL);
1002 wfree(entry->text);
1003 entry->text = _("Move Icon To");
1004 scr->clip_submenu = makeWorkspaceMenu(scr);
1005 if (scr->clip_submenu)
1006 wMenuEntrySetCascade(menu, entry, scr->clip_submenu);
1008 entry = wMenuAddCallback(menu, _("Remove Icon"), removeIconsCallback, NULL);
1009 wfree(entry->text);
1010 entry->text = _("Remove Icon");
1012 wMenuAddCallback(menu, _("Attract Icons"), colectIconsCallback, NULL);
1015 wMenuAddCallback(menu, _("Launch"), launchCallback, NULL);
1017 wMenuAddCallback(menu, _("Unhide Here"), unhideHereCallback, NULL);
1019 entry = wMenuAddCallback(menu, _("Hide"), hideCallback, NULL);
1020 wfree(entry->text);
1021 entry->text = _("Hide");
1023 wMenuAddCallback(menu, _("Settings..."), settingsCallback, NULL);
1025 wMenuAddCallback(menu, _("Kill"), killCallback, NULL);
1027 if (type == WM_CLIP)
1028 scr->clip_menu = menu;
1030 return menu;
1033 WDock *wDockCreate(WScreen *scr, int type)
1035 WDock *dock;
1036 WAppIcon *btn;
1038 make_keys();
1040 dock = wmalloc(sizeof(WDock));
1042 dock->max_icons = DOCK_MAX_ICONS;
1044 dock->icon_array = wmalloc(sizeof(WAppIcon *) * dock->max_icons);
1046 btn = mainIconCreate(scr, type);
1048 btn->dock = dock;
1050 dock->x_pos = btn->x_pos;
1051 dock->y_pos = btn->y_pos;
1052 dock->screen_ptr = scr;
1053 dock->type = type;
1054 dock->icon_count = 1;
1055 dock->on_right_side = 1;
1056 dock->collapsed = 0;
1057 dock->auto_collapse = 0;
1058 dock->auto_collapse_magic = NULL;
1059 dock->auto_raise_lower = 0;
1060 dock->auto_lower_magic = NULL;
1061 dock->auto_raise_magic = NULL;
1062 dock->attract_icons = 0;
1063 dock->lowered = 1;
1064 dock->icon_array[0] = btn;
1065 wRaiseFrame(btn->icon->core);
1066 XMoveWindow(dpy, btn->icon->core->window, btn->x_pos, btn->y_pos);
1068 /* create dock menu */
1069 dock->menu = dockMenuCreate(scr, type);
1071 return dock;
1074 void wDockDestroy(WDock *dock)
1076 int i;
1077 WAppIcon *aicon;
1079 for (i = (dock->type == WM_CLIP) ? 1 : 0; i < dock->max_icons; i++) {
1080 aicon = dock->icon_array[i];
1081 if (aicon) {
1082 int keepit = aicon->running && wApplicationOf(aicon->main_window);
1083 wDockDetach(dock, aicon);
1084 if (keepit) {
1085 /* XXX: can: aicon->icon == NULL ? */
1086 PlaceIcon(dock->screen_ptr, &aicon->x_pos, &aicon->y_pos,
1087 wGetHeadForWindow(aicon->icon->owner));
1088 XMoveWindow(dpy, aicon->icon->core->window, aicon->x_pos, aicon->y_pos);
1089 if (!dock->mapped || dock->collapsed)
1090 XMapWindow(dpy, aicon->icon->core->window);
1094 if (wPreferences.auto_arrange_icons)
1095 wArrangeIcons(dock->screen_ptr, True);
1096 wfree(dock->icon_array);
1097 if (dock->menu && dock->type != WM_CLIP)
1098 wMenuDestroy(dock->menu, True);
1099 if (dock->screen_ptr->last_dock == dock)
1100 dock->screen_ptr->last_dock = NULL;
1101 wfree(dock);
1104 void wClipIconPaint(WAppIcon *aicon)
1106 WScreen *scr = aicon->icon->core->screen_ptr;
1107 WWorkspace *workspace = scr->workspaces[scr->current_workspace];
1108 WMColor *color;
1109 Window win = aicon->icon->core->window;
1110 int length, nlength;
1111 char *ws_name, ws_number[10];
1112 int ty, tx;
1114 wIconPaint(aicon->icon);
1116 length = strlen(workspace->name);
1117 ws_name = wmalloc(length + 1);
1118 snprintf(ws_name, length + 1, "%s", workspace->name);
1119 snprintf(ws_number, sizeof(ws_number), "%i", scr->current_workspace + 1);
1120 nlength = strlen(ws_number);
1122 if (!workspace->clip->collapsed)
1123 color = scr->clip_title_color[CLIP_NORMAL];
1124 else
1125 color = scr->clip_title_color[CLIP_COLLAPSED];
1127 ty = ICON_SIZE - WMFontHeight(scr->clip_title_font) - 3;
1129 tx = CLIP_BUTTON_SIZE * ICON_SIZE / 64;
1131 if(wPreferences.show_clip_title)
1132 WMDrawString(scr->wmscreen, win, color, scr->clip_title_font, tx, ty, ws_name, length);
1134 tx = (ICON_SIZE / 2 - WMWidthOfString(scr->clip_title_font, ws_number, nlength)) / 2;
1136 WMDrawString(scr->wmscreen, win, color, scr->clip_title_font, tx, 2, ws_number, nlength);
1138 wfree(ws_name);
1140 if (aicon->launching)
1141 XFillRectangle(dpy, aicon->icon->core->window, scr->stipple_gc,
1142 0, 0, wPreferences.icon_size, wPreferences.icon_size);
1144 paintClipButtons(aicon, aicon->dock->lclip_button_pushed, aicon->dock->rclip_button_pushed);
1147 static void clipIconExpose(WObjDescriptor *desc, XEvent *event)
1149 wClipIconPaint(desc->parent);
1152 static void dockIconPaint(WAppIcon *btn)
1154 if (btn == btn->icon->core->screen_ptr->clip_icon) {
1155 wClipIconPaint(btn);
1156 } else {
1157 wAppIconPaint(btn);
1158 save_appicon(btn, True);
1162 static WMPropList *make_icon_state(WAppIcon *btn)
1164 WMPropList *node = NULL;
1165 WMPropList *command, *autolaunch, *lock, *name, *forced;
1166 WMPropList *position, *buggy, *omnipresent;
1167 char *tmp;
1168 char buffer[64];
1170 if (btn) {
1171 if (!btn->command)
1172 command = WMCreatePLString("-");
1173 else
1174 command = WMCreatePLString(btn->command);
1176 autolaunch = btn->auto_launch ? dYes : dNo;
1178 lock = btn->lock ? dYes : dNo;
1180 tmp = EscapeWM_CLASS(btn->wm_instance, btn->wm_class);
1182 name = WMCreatePLString(tmp);
1184 wfree(tmp);
1186 forced = btn->forced_dock ? dYes : dNo;
1188 buggy = btn->buggy_app ? dYes : dNo;
1190 if (btn == btn->icon->core->screen_ptr->clip_icon)
1191 snprintf(buffer, sizeof(buffer), "%i,%i", btn->x_pos, btn->y_pos);
1192 else
1193 snprintf(buffer, sizeof(buffer), "%hi,%hi", btn->xindex, btn->yindex);
1194 position = WMCreatePLString(buffer);
1196 node = WMCreatePLDictionary(dCommand, command,
1197 dName, name,
1198 dAutoLaunch, autolaunch,
1199 dLock, lock,
1200 dForced, forced, dBuggyApplication, buggy, dPosition, position, NULL);
1201 WMReleasePropList(command);
1202 WMReleasePropList(name);
1203 WMReleasePropList(position);
1205 omnipresent = btn->omnipresent ? dYes : dNo;
1206 if (btn->dock != btn->icon->core->screen_ptr->dock && (btn->xindex != 0 || btn->yindex != 0))
1207 WMPutInPLDictionary(node, dOmnipresent, omnipresent);
1209 #ifdef XDND /* was OFFIX */
1210 if (btn->dnd_command) {
1211 command = WMCreatePLString(btn->dnd_command);
1212 WMPutInPLDictionary(node, dDropCommand, command);
1213 WMReleasePropList(command);
1215 #endif /* XDND */
1217 if (btn->paste_command) {
1218 command = WMCreatePLString(btn->paste_command);
1219 WMPutInPLDictionary(node, dPasteCommand, command);
1220 WMReleasePropList(command);
1224 return node;
1227 static WMPropList *dockSaveState(WDock *dock)
1229 int i;
1230 WMPropList *icon_info;
1231 WMPropList *list = NULL, *dock_state = NULL;
1232 WMPropList *value, *key;
1233 char buffer[256];
1235 list = WMCreatePLArray(NULL);
1237 for (i = (dock->type == WM_DOCK ? 0 : 1); i < dock->max_icons; i++) {
1238 WAppIcon *btn = dock->icon_array[i];
1240 if (!btn || btn->attracted)
1241 continue;
1243 if ((icon_info = make_icon_state(dock->icon_array[i]))) {
1244 WMAddToPLArray(list, icon_info);
1245 WMReleasePropList(icon_info);
1249 dock_state = WMCreatePLDictionary(dApplications, list, NULL);
1251 if (dock->type == WM_DOCK) {
1252 snprintf(buffer, sizeof(buffer), "Applications%i", dock->screen_ptr->scr_height);
1253 key = WMCreatePLString(buffer);
1254 WMPutInPLDictionary(dock_state, key, list);
1255 WMReleasePropList(key);
1257 snprintf(buffer, sizeof(buffer), "%i,%i", (dock->on_right_side ? -ICON_SIZE : 0), dock->y_pos);
1258 value = WMCreatePLString(buffer);
1259 WMPutInPLDictionary(dock_state, dPosition, value);
1260 WMReleasePropList(value);
1262 WMReleasePropList(list);
1264 value = (dock->lowered ? dYes : dNo);
1265 WMPutInPLDictionary(dock_state, dLowered, value);
1267 if (dock->type == WM_CLIP) {
1268 value = (dock->collapsed ? dYes : dNo);
1269 WMPutInPLDictionary(dock_state, dCollapsed, value);
1271 value = (dock->auto_collapse ? dYes : dNo);
1272 WMPutInPLDictionary(dock_state, dAutoCollapse, value);
1274 value = (dock->auto_raise_lower ? dYes : dNo);
1275 WMPutInPLDictionary(dock_state, dAutoRaiseLower, value);
1277 value = (dock->attract_icons ? dYes : dNo);
1278 WMPutInPLDictionary(dock_state, dAutoAttractIcons, value);
1281 return dock_state;
1284 void wDockSaveState(WScreen *scr, WMPropList *old_state)
1286 WMPropList *dock_state;
1287 WMPropList *keys;
1289 dock_state = dockSaveState(scr->dock);
1292 * Copy saved states of docks with different sizes.
1294 if (old_state) {
1295 int i;
1296 WMPropList *tmp;
1298 keys = WMGetPLDictionaryKeys(old_state);
1299 for (i = 0; i < WMGetPropListItemCount(keys); i++) {
1300 tmp = WMGetFromPLArray(keys, i);
1302 if (strncasecmp(WMGetFromPLString(tmp), "applications", 12) == 0
1303 && !WMGetFromPLDictionary(dock_state, tmp)) {
1305 WMPutInPLDictionary(dock_state, tmp, WMGetFromPLDictionary(old_state, tmp));
1308 WMReleasePropList(keys);
1311 WMPutInPLDictionary(scr->session_state, dDock, dock_state);
1313 WMReleasePropList(dock_state);
1316 void wClipSaveState(WScreen *scr)
1318 WMPropList *clip_state;
1320 clip_state = make_icon_state(scr->clip_icon);
1322 WMPutInPLDictionary(scr->session_state, dClip, clip_state);
1324 WMReleasePropList(clip_state);
1327 WMPropList *wClipSaveWorkspaceState(WScreen *scr, int workspace)
1329 return dockSaveState(scr->workspaces[workspace]->clip);
1332 static Bool getBooleanDockValue(WMPropList *value, WMPropList *key)
1334 if (value) {
1335 if (WMIsPLString(value)) {
1336 if (strcasecmp(WMGetFromPLString(value), "YES") == 0)
1337 return True;
1338 } else {
1339 wwarning(_("bad value in docked icon state info %s"), WMGetFromPLString(key));
1342 return False;
1345 static WAppIcon *restore_icon_state(WScreen *scr, WMPropList *info, int type, int index)
1347 WAppIcon *aicon;
1348 WMPropList *cmd, *value;
1349 char *wclass, *winstance, *command;
1351 cmd = WMGetFromPLDictionary(info, dCommand);
1352 if (!cmd || !WMIsPLString(cmd))
1353 return NULL;
1355 /* parse window name */
1356 value = WMGetFromPLDictionary(info, dName);
1357 if (!value)
1358 return NULL;
1360 ParseWindowName(value, &winstance, &wclass, "dock");
1362 if (!winstance && !wclass)
1363 return NULL;
1365 /* get commands */
1366 if (cmd)
1367 command = wstrdup(WMGetFromPLString(cmd));
1368 else
1369 command = NULL;
1371 if (!command || strcmp(command, "-") == 0) {
1372 if (command)
1373 wfree(command);
1374 if (wclass)
1375 wfree(wclass);
1376 if (winstance)
1377 wfree(winstance);
1379 return NULL;
1382 aicon = wAppIconCreateForDock(scr, command, winstance, wclass, TILE_NORMAL);
1383 if (wclass)
1384 wfree(wclass);
1385 if (winstance)
1386 wfree(winstance);
1387 if (command)
1388 wfree(command);
1390 aicon->icon->core->descriptor.handle_mousedown = iconMouseDown;
1391 if (type == WM_CLIP) {
1392 aicon->icon->core->descriptor.handle_enternotify = clipEnterNotify;
1393 aicon->icon->core->descriptor.handle_leavenotify = clipLeaveNotify;
1395 aicon->icon->core->descriptor.parent_type = WCLASS_DOCK_ICON;
1396 aicon->icon->core->descriptor.parent = aicon;
1398 #ifdef XDND /* was OFFIX */
1399 cmd = WMGetFromPLDictionary(info, dDropCommand);
1400 if (cmd)
1401 aicon->dnd_command = wstrdup(WMGetFromPLString(cmd));
1402 #endif
1404 cmd = WMGetFromPLDictionary(info, dPasteCommand);
1405 if (cmd)
1406 aicon->paste_command = wstrdup(WMGetFromPLString(cmd));
1408 /* check auto launch */
1409 value = WMGetFromPLDictionary(info, dAutoLaunch);
1411 aicon->auto_launch = getBooleanDockValue(value, dAutoLaunch);
1413 /* check lock */
1414 value = WMGetFromPLDictionary(info, dLock);
1416 aicon->lock = getBooleanDockValue(value, dLock);
1418 /* check if it wasn't normally docked */
1419 value = WMGetFromPLDictionary(info, dForced);
1421 aicon->forced_dock = getBooleanDockValue(value, dForced);
1423 /* check if we can rely on the stuff in the app */
1424 value = WMGetFromPLDictionary(info, dBuggyApplication);
1426 aicon->buggy_app = getBooleanDockValue(value, dBuggyApplication);
1428 /* get position in the dock */
1429 value = WMGetFromPLDictionary(info, dPosition);
1430 if (value && WMIsPLString(value)) {
1431 if (sscanf(WMGetFromPLString(value), "%hi,%hi", &aicon->xindex, &aicon->yindex) != 2)
1432 wwarning(_("bad value in docked icon state info %s"), WMGetFromPLString(dPosition));
1434 /* check position sanity */
1435 /* incomplete section! */
1436 if (type == WM_DOCK) {
1437 aicon->xindex = 0;
1438 if (aicon->yindex < 0)
1439 wwarning(_("bad value in docked icon position %i,%i"),
1440 aicon->xindex, aicon->yindex);
1442 } else {
1443 aicon->yindex = index;
1444 aicon->xindex = 0;
1447 /* check if icon is omnipresent */
1448 value = WMGetFromPLDictionary(info, dOmnipresent);
1450 aicon->omnipresent = getBooleanDockValue(value, dOmnipresent);
1452 aicon->running = 0;
1453 aicon->docked = 1;
1455 return aicon;
1458 #define COMPLAIN(key) wwarning(_("bad value in dock state info:%s"), key)
1460 WAppIcon *wClipRestoreState(WScreen *scr, WMPropList *clip_state)
1462 WAppIcon *icon;
1463 WMPropList *value;
1465 icon = mainIconCreate(scr, WM_CLIP);
1467 if (!clip_state)
1468 return icon;
1470 WMRetainPropList(clip_state);
1472 /* restore position */
1474 value = WMGetFromPLDictionary(clip_state, dPosition);
1476 if (value) {
1477 if (!WMIsPLString(value)) {
1478 COMPLAIN("Position");
1479 } else {
1480 WMRect rect;
1481 int flags;
1483 if (sscanf(WMGetFromPLString(value), "%i,%i", &icon->x_pos, &icon->y_pos) != 2)
1484 COMPLAIN("Position");
1486 /* check position sanity */
1487 rect.pos.x = icon->x_pos;
1488 rect.pos.y = icon->y_pos;
1489 rect.size.width = rect.size.height = ICON_SIZE;
1491 wGetRectPlacementInfo(scr, rect, &flags);
1492 if (flags & (XFLAG_DEAD | XFLAG_PARTIAL))
1493 wScreenKeepInside(scr, &icon->x_pos, &icon->y_pos, ICON_SIZE, ICON_SIZE);
1496 #ifdef XDND /* was OFFIX */
1497 value = WMGetFromPLDictionary(clip_state, dDropCommand);
1498 if (value && WMIsPLString(value))
1499 icon->dnd_command = wstrdup(WMGetFromPLString(value));
1500 #endif
1502 value = WMGetFromPLDictionary(clip_state, dPasteCommand);
1503 if (value && WMIsPLString(value))
1504 icon->paste_command = wstrdup(WMGetFromPLString(value));
1506 WMReleasePropList(clip_state);
1508 return icon;
1511 WDock *wDockRestoreState(WScreen *scr, WMPropList *dock_state, int type)
1513 WDock *dock;
1514 WMPropList *apps;
1515 WMPropList *value;
1516 WAppIcon *aicon, *old_top;
1517 int count, i;
1519 dock = wDockCreate(scr, type);
1521 if (!dock_state)
1522 return dock;
1524 WMRetainPropList(dock_state);
1526 /* restore position */
1527 value = WMGetFromPLDictionary(dock_state, dPosition);
1528 if (value) {
1529 if (!WMIsPLString(value)) {
1530 COMPLAIN("Position");
1531 } else {
1532 WMRect rect;
1533 int flags;
1535 if (sscanf(WMGetFromPLString(value), "%i,%i", &dock->x_pos, &dock->y_pos) != 2)
1536 COMPLAIN("Position");
1538 /* check position sanity */
1539 rect.pos.x = dock->x_pos;
1540 rect.pos.y = dock->y_pos;
1541 rect.size.width = rect.size.height = ICON_SIZE;
1543 wGetRectPlacementInfo(scr, rect, &flags);
1544 if (flags & (XFLAG_DEAD | XFLAG_PARTIAL)) {
1545 int x = dock->x_pos;
1546 wScreenKeepInside(scr, &x, &dock->y_pos, ICON_SIZE, ICON_SIZE);
1549 /* Is this needed any more? */
1550 if (type == WM_CLIP) {
1551 if (dock->x_pos < 0) {
1552 dock->x_pos = 0;
1553 } else if (dock->x_pos > scr->scr_width - ICON_SIZE) {
1554 dock->x_pos = scr->scr_width - ICON_SIZE;
1556 } else {
1557 if (dock->x_pos >= 0) {
1558 dock->x_pos = DOCK_EXTRA_SPACE;
1559 dock->on_right_side = 0;
1560 } else {
1561 dock->x_pos = scr->scr_width - DOCK_EXTRA_SPACE - ICON_SIZE;
1562 dock->on_right_side = 1;
1568 /* restore lowered/raised state */
1569 dock->lowered = 0;
1571 value = WMGetFromPLDictionary(dock_state, dLowered);
1572 if (value) {
1573 if (!WMIsPLString(value)) {
1574 COMPLAIN("Lowered");
1575 } else {
1576 if (strcasecmp(WMGetFromPLString(value), "YES") == 0)
1577 dock->lowered = 1;
1581 /* restore collapsed state */
1582 dock->collapsed = 0;
1584 value = WMGetFromPLDictionary(dock_state, dCollapsed);
1585 if (value) {
1586 if (!WMIsPLString(value)) {
1587 COMPLAIN("Collapsed");
1588 } else {
1589 if (strcasecmp(WMGetFromPLString(value), "YES") == 0)
1590 dock->collapsed = 1;
1594 /* restore auto-collapsed state */
1595 value = WMGetFromPLDictionary(dock_state, dAutoCollapse);
1596 if (value) {
1597 if (!WMIsPLString(value)) {
1598 COMPLAIN("AutoCollapse");
1599 } else {
1600 if (strcasecmp(WMGetFromPLString(value), "YES") == 0) {
1601 dock->auto_collapse = 1;
1602 dock->collapsed = 1;
1607 /* restore auto-raise/lower state */
1608 value = WMGetFromPLDictionary(dock_state, dAutoRaiseLower);
1609 if (value) {
1610 if (!WMIsPLString(value)) {
1611 COMPLAIN("AutoRaiseLower");
1612 } else {
1613 if (strcasecmp(WMGetFromPLString(value), "YES") == 0)
1614 dock->auto_raise_lower = 1;
1618 /* restore attract icons state */
1619 dock->attract_icons = 0;
1621 value = WMGetFromPLDictionary(dock_state, dAutoAttractIcons);
1622 if (value) {
1623 if (!WMIsPLString(value)) {
1624 COMPLAIN("AutoAttractIcons");
1625 } else {
1626 if (strcasecmp(WMGetFromPLString(value), "YES") == 0)
1627 dock->attract_icons = 1;
1631 /* application list */
1634 WMPropList *tmp;
1635 char buffer[64];
1638 * When saving, it saves the dock state in
1639 * Applications and Applicationsnnn
1641 * When loading, it will first try Applicationsnnn.
1642 * If it does not exist, use Applications as default.
1645 snprintf(buffer, sizeof(buffer), "Applications%i", scr->scr_height);
1647 tmp = WMCreatePLString(buffer);
1648 apps = WMGetFromPLDictionary(dock_state, tmp);
1649 WMReleasePropList(tmp);
1651 if (!apps)
1652 apps = WMGetFromPLDictionary(dock_state, dApplications);
1655 if (!apps)
1656 goto finish;
1658 count = WMGetPropListItemCount(apps);
1659 if (count == 0)
1660 goto finish;
1662 old_top = dock->icon_array[0];
1664 /* dock->icon_count is set to 1 when dock is created.
1665 * Since Clip is already restored, we want to keep it so for clip,
1666 * but for dock we may change the default top tile, so we set it to 0.
1668 if (type == WM_DOCK)
1669 dock->icon_count = 0;
1671 for (i = 0; i < count; i++) {
1672 if (dock->icon_count >= dock->max_icons) {
1673 wwarning(_("there are too many icons stored in dock. Ignoring what doesn't fit"));
1674 break;
1677 value = WMGetFromPLArray(apps, i);
1678 aicon = restore_icon_state(scr, value, type, dock->icon_count);
1680 dock->icon_array[dock->icon_count] = aicon;
1682 if (aicon) {
1683 aicon->dock = dock;
1684 aicon->x_pos = dock->x_pos + (aicon->xindex * ICON_SIZE);
1685 aicon->y_pos = dock->y_pos + (aicon->yindex * ICON_SIZE);
1687 if (dock->lowered)
1688 ChangeStackingLevel(aicon->icon->core, WMNormalLevel);
1689 else
1690 ChangeStackingLevel(aicon->icon->core, WMDockLevel);
1692 wCoreConfigure(aicon->icon->core, aicon->x_pos, aicon->y_pos, 0, 0);
1693 if (!dock->collapsed)
1694 XMapWindow(dpy, aicon->icon->core->window);
1696 wRaiseFrame(aicon->icon->core);
1698 dock->icon_count++;
1699 } else if (dock->icon_count == 0 && type == WM_DOCK) {
1700 dock->icon_count++;
1704 /* if the first icon is not defined, use the default */
1705 if (dock->icon_array[0] == NULL) {
1706 /* update default icon */
1707 old_top->x_pos = dock->x_pos;
1708 old_top->y_pos = dock->y_pos;
1709 if (dock->lowered)
1710 ChangeStackingLevel(old_top->icon->core, WMNormalLevel);
1711 else
1712 ChangeStackingLevel(old_top->icon->core, WMDockLevel);
1714 dock->icon_array[0] = old_top;
1715 XMoveWindow(dpy, old_top->icon->core->window, dock->x_pos, dock->y_pos);
1716 /* we don't need to increment dock->icon_count here because it was
1717 * incremented in the loop above.
1719 } else if (old_top != dock->icon_array[0]) {
1720 if (old_top == scr->clip_icon)
1721 scr->clip_icon = dock->icon_array[0];
1723 wAppIconDestroy(old_top);
1726 finish:
1727 WMReleasePropList(dock_state);
1729 return dock;
1732 void wDockLaunchWithState(WDock *dock, WAppIcon *btn, WSavedState *state)
1734 if (btn && btn->command && !btn->running && !btn->launching) {
1735 btn->drop_launch = 0;
1736 btn->paste_launch = 0;
1738 btn->pid = execCommand(btn, btn->command, state);
1740 if (btn->pid > 0) {
1741 if (!btn->forced_dock && !btn->buggy_app) {
1742 btn->launching = 1;
1743 dockIconPaint(btn);
1746 } else {
1747 wfree(state);
1751 void wDockDoAutoLaunch(WDock *dock, int workspace)
1753 WAppIcon *btn;
1754 WSavedState *state;
1755 int i;
1757 for (i = 0; i < dock->max_icons; i++) {
1758 btn = dock->icon_array[i];
1759 if (!btn || !btn->auto_launch)
1760 continue;
1762 state = wmalloc(sizeof(WSavedState));
1763 state->workspace = workspace;
1764 /* TODO: this is klugy and is very difficult to understand
1765 * what's going on. Try to clean up */
1766 wDockLaunchWithState(dock, btn, state);
1770 #ifdef XDND /* was OFFIX */
1771 static WDock *findDock(WScreen *scr, XEvent *event, int *icon_pos)
1773 WDock *dock;
1774 int i;
1776 *icon_pos = -1;
1777 if ((dock = scr->dock) != NULL) {
1778 for (i = 0; i < dock->max_icons; i++) {
1779 if (dock->icon_array[i]
1780 && dock->icon_array[i]->icon->core->window == event->xclient.window) {
1781 *icon_pos = i;
1782 break;
1786 if (*icon_pos < 0 && (dock = scr->workspaces[scr->current_workspace]->clip) != NULL) {
1787 for (i = 0; i < dock->max_icons; i++) {
1788 if (dock->icon_array[i]
1789 && dock->icon_array[i]->icon->core->window == event->xclient.window) {
1790 *icon_pos = i;
1791 break;
1795 if (*icon_pos >= 0)
1796 return dock;
1797 return NULL;
1800 int wDockReceiveDNDDrop(WScreen *scr, XEvent *event)
1802 WDock *dock;
1803 WAppIcon *btn;
1804 int icon_pos;
1806 dock = findDock(scr, event, &icon_pos);
1807 if (!dock)
1808 return False;
1811 * Return True if the drop was on an application icon window.
1812 * In this case, let the ClientMessage handler redirect the
1813 * message to the app.
1815 if (dock->icon_array[icon_pos]->icon->icon_win != None)
1816 return True;
1818 if (dock->icon_array[icon_pos]->dnd_command != NULL) {
1819 scr->flags.dnd_data_convertion_status = 0;
1821 btn = dock->icon_array[icon_pos];
1823 if (!btn->forced_dock) {
1824 btn->relaunching = btn->running;
1825 btn->running = 1;
1827 if (btn->wm_instance || btn->wm_class) {
1828 WWindowAttributes attr;
1829 memset(&attr, 0, sizeof(WWindowAttributes));
1830 wDefaultFillAttributes(btn->wm_instance, btn->wm_class, &attr, NULL, True);
1832 if (!attr.no_appicon)
1833 btn->launching = 1;
1834 else
1835 btn->running = 0;
1838 btn->paste_launch = 0;
1839 btn->drop_launch = 1;
1840 scr->last_dock = dock;
1841 btn->pid = execCommand(btn, btn->dnd_command, NULL);
1842 if (btn->pid > 0) {
1843 dockIconPaint(btn);
1844 } else {
1845 btn->launching = 0;
1846 if (!btn->relaunching)
1847 btn->running = 0;
1850 return False;
1852 #endif /* XDND */
1854 Bool wDockAttachIcon(WDock *dock, WAppIcon *icon, int x, int y, Bool update_icon)
1856 WWindow *wwin;
1857 Bool lupdate_icon = False;
1858 char *command = NULL;
1859 int index;
1861 icon->editing = 0;
1863 if (update_icon)
1864 lupdate_icon = True;
1866 if (icon->command == NULL) {
1867 /* If icon->owner exists, it means the application is running */
1868 if (icon->icon->owner) {
1869 wwin = icon->icon->owner;
1870 command = GetCommandForWindow(wwin->client_win);
1873 if (command) {
1874 icon->command = command;
1875 } else {
1876 /* icon->forced_dock = 1; */
1877 if (dock->type != WM_CLIP || !icon->attracted) {
1878 icon->editing = 1;
1879 if (wInputDialog(dock->screen_ptr, _("Dock Icon"),
1880 _("Type the command used to launch the application"), &command)) {
1881 if (command && (command[0] == 0 || (command[0] == '-' && command[1] == 0))) {
1882 wfree(command);
1883 command = NULL;
1885 icon->command = command;
1886 icon->editing = 0;
1887 } else {
1888 icon->editing = 0;
1889 if (command)
1890 wfree(command);
1891 /* If the target is the dock, reject the icon. If
1892 * the target is the clip, make it an attracted icon
1894 if (dock->type == WM_CLIP) {
1895 icon->attracted = 1;
1896 if (!icon->icon->shadowed) {
1897 icon->icon->shadowed = 1;
1898 lupdate_icon = True;
1900 } else {
1901 return False;
1908 for (index = 1; index < dock->max_icons; index++)
1909 if (dock->icon_array[index] == NULL)
1910 break;
1911 /* if (index == dock->max_icons)
1912 return; */
1914 assert(index < dock->max_icons);
1916 dock->icon_array[index] = icon;
1917 icon->yindex = y;
1918 icon->xindex = x;
1920 icon->omnipresent = 0;
1922 icon->x_pos = dock->x_pos + x * ICON_SIZE;
1923 icon->y_pos = dock->y_pos + y * ICON_SIZE;
1925 dock->icon_count++;
1927 icon->running = 1;
1928 icon->launching = 0;
1929 icon->docked = 1;
1930 icon->dock = dock;
1931 icon->icon->core->descriptor.handle_mousedown = iconMouseDown;
1932 if (dock->type == WM_CLIP) {
1933 icon->icon->core->descriptor.handle_enternotify = clipEnterNotify;
1934 icon->icon->core->descriptor.handle_leavenotify = clipLeaveNotify;
1936 icon->icon->core->descriptor.parent_type = WCLASS_DOCK_ICON;
1937 icon->icon->core->descriptor.parent = icon;
1939 MoveInStackListUnder(dock->icon_array[index - 1]->icon->core, icon->icon->core);
1940 wAppIconMove(icon, icon->x_pos, icon->y_pos);
1942 /* Update the icon images */
1943 if (lupdate_icon)
1944 wIconUpdate(icon->icon, NULL);
1946 /* Paint it */
1947 wAppIconPaint(icon);
1949 /* Save it */
1950 save_appicon(icon, True);
1952 if (wPreferences.auto_arrange_icons)
1953 wArrangeIcons(dock->screen_ptr, True);
1955 #ifdef XDND /* was OFFIX */
1956 if (icon->command && !icon->dnd_command) {
1957 int len = strlen(icon->command) + 8;
1958 icon->dnd_command = wmalloc(len);
1959 snprintf(icon->dnd_command, len, "%s %%d", icon->command);
1961 #endif
1963 if (icon->command && !icon->paste_command) {
1964 int len = strlen(icon->command) + 8;
1965 icon->paste_command = wmalloc(len);
1966 snprintf(icon->paste_command, len, "%s %%s", icon->command);
1969 return True;
1972 static void reattachIcon(WDock *dock, WAppIcon *icon, int x, int y)
1974 int index;
1976 for (index = 1; index < dock->max_icons; index++) {
1977 if (dock->icon_array[index] == icon)
1978 break;
1980 assert(index < dock->max_icons);
1982 icon->yindex = y;
1983 icon->xindex = x;
1985 icon->x_pos = dock->x_pos + x * ICON_SIZE;
1986 icon->y_pos = dock->y_pos + y * ICON_SIZE;
1989 static Bool moveIconBetweenDocks(WDock *src, WDock *dest, WAppIcon *icon, int x, int y)
1991 WWindow *wwin;
1992 char *command = NULL;
1993 int index;
1994 Bool update_icon = False;
1996 if (src == dest)
1997 return True; /* No move needed, we're already there */
1999 if (dest == NULL)
2000 return False;
2003 * For the moment we can't do this if we move icons in Clip from one
2004 * workspace to other, because if we move two or more icons without
2005 * command, the dialog box will not be able to tell us to which of the
2006 * moved icons it applies. -Dan
2008 if ((dest->type == WM_DOCK /*|| dest->keep_attracted */ ) && icon->command == NULL) {
2009 /* If icon->owner exists, it means the application is running */
2010 if (icon->icon->owner) {
2011 wwin = icon->icon->owner;
2012 command = GetCommandForWindow(wwin->client_win);
2015 if (command) {
2016 icon->command = command;
2017 } else {
2018 icon->editing = 1;
2019 /* icon->forced_dock = 1; */
2020 if (wInputDialog(src->screen_ptr, _("Dock Icon"),
2021 _("Type the command used to launch the application"), &command)) {
2022 if (command && (command[0] == 0 || (command[0] == '-' && command[1] == 0))) {
2023 wfree(command);
2024 command = NULL;
2026 icon->command = command;
2027 } else {
2028 icon->editing = 0;
2029 if (command)
2030 wfree(command);
2031 return False;
2033 icon->editing = 0;
2037 if (dest->type == WM_DOCK)
2038 wClipMakeIconOmnipresent(icon, False);
2040 for (index = 1; index < src->max_icons; index++) {
2041 if (src->icon_array[index] == icon)
2042 break;
2044 assert(index < src->max_icons);
2046 src->icon_array[index] = NULL;
2047 src->icon_count--;
2049 for (index = 1; index < dest->max_icons; index++) {
2050 if (dest->icon_array[index] == NULL)
2051 break;
2054 assert(index < dest->max_icons);
2056 dest->icon_array[index] = icon;
2057 icon->dock = dest;
2059 /* deselect the icon */
2060 if (icon->icon->selected)
2061 wIconSelect(icon->icon);
2063 if (dest->type == WM_DOCK) {
2064 icon->icon->core->descriptor.handle_enternotify = NULL;
2065 icon->icon->core->descriptor.handle_leavenotify = NULL;
2066 } else {
2067 icon->icon->core->descriptor.handle_enternotify = clipEnterNotify;
2068 icon->icon->core->descriptor.handle_leavenotify = clipLeaveNotify;
2071 /* set it to be kept when moving to dock.
2072 * Unless the icon does not have a command set
2074 if (icon->command && dest->type == WM_DOCK) {
2075 icon->attracted = 0;
2076 if (icon->icon->shadowed) {
2077 icon->icon->shadowed = 0;
2078 update_icon = True;
2080 save_appicon(icon, True);
2083 if (src->auto_collapse || src->auto_raise_lower)
2084 clipLeave(src);
2086 icon->yindex = y;
2087 icon->xindex = x;
2089 icon->x_pos = dest->x_pos + x * ICON_SIZE;
2090 icon->y_pos = dest->y_pos + y * ICON_SIZE;
2092 dest->icon_count++;
2094 MoveInStackListUnder(dest->icon_array[index - 1]->icon->core, icon->icon->core);
2096 /* Update the icon images */
2097 if (update_icon)
2098 wIconUpdate(icon->icon, NULL);
2100 /* Paint it */
2101 wAppIconPaint(icon);
2103 return True;
2106 void wDockDetach(WDock *dock, WAppIcon *icon)
2108 int index;
2109 Bool update_icon = False;
2111 /* make the settings panel be closed */
2112 if (icon->panel)
2113 DestroyDockAppSettingsPanel(icon->panel);
2115 /* This must be called before icon->dock is set to NULL.
2116 * Don't move it. -Dan
2118 wClipMakeIconOmnipresent(icon, False);
2120 icon->docked = 0;
2121 icon->dock = NULL;
2122 icon->attracted = 0;
2123 icon->auto_launch = 0;
2124 if (icon->icon->shadowed) {
2125 icon->icon->shadowed = 0;
2126 update_icon = True;
2129 /* deselect the icon */
2130 if (icon->icon->selected)
2131 wIconSelect(icon->icon);
2133 if (icon->command) {
2134 wfree(icon->command);
2135 icon->command = NULL;
2137 #ifdef XDND /* was OFFIX */
2138 if (icon->dnd_command) {
2139 wfree(icon->dnd_command);
2140 icon->dnd_command = NULL;
2142 #endif
2143 if (icon->paste_command) {
2144 wfree(icon->paste_command);
2145 icon->paste_command = NULL;
2148 for (index = 1; index < dock->max_icons; index++)
2149 if (dock->icon_array[index] == icon)
2150 break;
2152 assert(index < dock->max_icons);
2153 dock->icon_array[index] = NULL;
2154 icon->yindex = -1;
2155 icon->xindex = -1;
2157 dock->icon_count--;
2159 /* if the dock is not attached to an application or
2160 * the application did not set the appropriate hints yet,
2161 * destroy the icon */
2162 if (!icon->running || !wApplicationOf(icon->main_window)) {
2163 wAppIconDestroy(icon);
2164 } else {
2165 icon->icon->core->descriptor.handle_mousedown = appIconMouseDown;
2166 icon->icon->core->descriptor.handle_enternotify = NULL;
2167 icon->icon->core->descriptor.handle_leavenotify = NULL;
2168 icon->icon->core->descriptor.parent_type = WCLASS_APPICON;
2169 icon->icon->core->descriptor.parent = icon;
2171 ChangeStackingLevel(icon->icon->core, NORMAL_ICON_LEVEL);
2173 /* Update the icon images */
2174 if (update_icon)
2175 wIconUpdate(icon->icon, NULL);
2177 /* Paint it */
2178 wAppIconPaint(icon);
2180 if (wPreferences.auto_arrange_icons)
2181 wArrangeIcons(dock->screen_ptr, True);
2183 if (dock->auto_collapse || dock->auto_raise_lower)
2184 clipLeave(dock);
2188 * returns the closest Dock slot index for the passed
2189 * coordinates.
2191 * Returns False if icon can't be docked.
2193 * Note: this function should NEVER alter ret_x or ret_y, unless it will
2194 * return True. -Dan
2196 Bool wDockSnapIcon(WDock *dock, WAppIcon *icon, int req_x, int req_y, int *ret_x, int *ret_y, int redocking)
2198 WScreen *scr = dock->screen_ptr;
2199 int dx, dy;
2200 int ex_x, ex_y;
2201 int i, offset = ICON_SIZE / 2;
2202 WAppIcon *aicon = NULL;
2203 WAppIcon *nicon = NULL;
2204 int max_y_icons;
2206 /* TODO: XINERAMA, for these */
2207 max_y_icons = scr->scr_height / ICON_SIZE - 1;
2209 if (wPreferences.flags.noupdates)
2210 return False;
2212 dx = dock->x_pos;
2213 dy = dock->y_pos;
2215 /* if the dock is full */
2216 if (!redocking && (dock->icon_count >= dock->max_icons))
2217 return False;
2219 /* exact position */
2220 if (req_y < dy)
2221 ex_y = (req_y - offset - dy) / ICON_SIZE;
2222 else
2223 ex_y = (req_y + offset - dy) / ICON_SIZE;
2225 if (req_x < dx)
2226 ex_x = (req_x - offset - dx) / ICON_SIZE;
2227 else
2228 ex_x = (req_x + offset - dx) / ICON_SIZE;
2230 /* check if the icon is outside the screen boundaries */
2232 WMRect rect;
2233 int flags;
2235 rect.pos.x = dx + ex_x * ICON_SIZE;
2236 rect.pos.y = dy + ex_y * ICON_SIZE;
2237 rect.size.width = rect.size.height = ICON_SIZE;
2239 wGetRectPlacementInfo(scr, rect, &flags);
2240 if (flags & (XFLAG_DEAD | XFLAG_PARTIAL))
2241 return False;
2244 if (dock->type == WM_DOCK) {
2245 if (icon->dock != dock && ex_x != 0)
2246 return False;
2248 aicon = NULL;
2249 for (i = 0; i < dock->max_icons; i++) {
2250 nicon = dock->icon_array[i];
2251 if (nicon && nicon->yindex == ex_y) {
2252 aicon = nicon;
2253 break;
2257 if (redocking) {
2258 int sig, done, closest;
2260 /* Possible cases when redocking:
2262 * icon dragged out of range of any slot -> false
2263 * icon dragged to range of free slot
2264 * icon dragged to range of same slot
2265 * icon dragged to range of different icon
2267 if (abs(ex_x) > DOCK_DETTACH_THRESHOLD)
2268 return False;
2270 if (ex_y >= 0 && ex_y <= max_y_icons && (aicon == icon || !aicon)) {
2271 *ret_x = 0;
2272 *ret_y = ex_y;
2273 return True;
2276 /* start looking at the upper slot or lower? */
2277 if (ex_y * ICON_SIZE < (req_y + offset - dy))
2278 sig = 1;
2279 else
2280 sig = -1;
2282 closest = -1;
2283 done = 0;
2284 /* look for closest free slot */
2285 for (i = 0; i < (DOCK_DETTACH_THRESHOLD + 1) * 2 && !done; i++) {
2286 int j;
2288 done = 1;
2289 closest = sig * (i / 2) + ex_y;
2290 /* check if this slot is used */
2291 if (closest >= 0) {
2292 for (j = 0; j < dock->max_icons; j++) {
2293 if (dock->icon_array[j]
2294 && dock->icon_array[j]->yindex == closest) {
2295 /* slot is used by someone else */
2296 if (dock->icon_array[j] != icon)
2297 done = 0;
2298 break;
2302 sig = -sig;
2304 if (done && closest >= 0 && closest <= max_y_icons &&
2305 ((ex_y >= closest && ex_y - closest < DOCK_DETTACH_THRESHOLD + 1)
2306 || (ex_y < closest && closest - ex_y <= DOCK_DETTACH_THRESHOLD + 1))) {
2307 *ret_x = 0;
2308 *ret_y = closest;
2309 return True;
2311 } else { /* !redocking */
2313 /* if slot is free and the icon is close enough, return it */
2314 if (!aicon && ex_x == 0 && ex_y >= 0 && ex_y <= max_y_icons) {
2315 *ret_x = 0;
2316 *ret_y = ex_y;
2317 return True;
2320 } else { /* CLIP */
2321 int neighbours = 0;
2322 int start, stop, k;
2324 start = icon->omnipresent ? 0 : scr->current_workspace;
2325 stop = icon->omnipresent ? scr->workspace_count : start + 1;
2327 aicon = NULL;
2328 for (k = start; k < stop; k++) {
2329 WDock *tmp = scr->workspaces[k]->clip;
2330 if (!tmp)
2331 continue;
2332 for (i = 0; i < tmp->max_icons; i++) {
2333 nicon = tmp->icon_array[i];
2334 if (nicon && nicon->xindex == ex_x && nicon->yindex == ex_y) {
2335 aicon = nicon;
2336 break;
2339 if (aicon)
2340 break;
2342 for (k = start; k < stop; k++) {
2343 WDock *tmp = scr->workspaces[k]->clip;
2344 if (!tmp)
2345 continue;
2346 for (i = 0; i < tmp->max_icons; i++) {
2347 nicon = tmp->icon_array[i];
2348 if (nicon && nicon != icon && /* Icon can't be it's own neighbour */
2349 (abs(nicon->xindex - ex_x) <= CLIP_ATTACH_VICINITY &&
2350 abs(nicon->yindex - ex_y) <= CLIP_ATTACH_VICINITY)) {
2351 neighbours = 1;
2352 break;
2355 if (neighbours)
2356 break;
2359 if (neighbours && (aicon == NULL || (redocking && aicon == icon))) {
2360 *ret_x = ex_x;
2361 *ret_y = ex_y;
2362 return True;
2365 return False;
2368 static int onScreen(WScreen *scr, int x, int y, int sx, int ex, int sy, int ey)
2370 WMRect rect = wmkrect(x, y, ICON_SIZE, ICON_SIZE);
2371 int flags;
2373 wGetRectPlacementInfo(scr, rect, &flags);
2375 return !(flags & (XFLAG_DEAD | XFLAG_PARTIAL));
2379 * returns true if it can find a free slot in the dock,
2380 * in which case it changes x_pos and y_pos accordingly.
2381 * Else returns false.
2383 Bool wDockFindFreeSlot(WDock *dock, int *x_pos, int *y_pos)
2385 WScreen *scr = dock->screen_ptr;
2386 WAppIcon *btn;
2387 WAppIconChain *chain;
2388 unsigned char *slot_map;
2389 int mwidth;
2390 int r;
2391 int x, y;
2392 int i, done = False;
2393 int corner;
2394 int sx = 0, sy = 0, ex = scr->scr_width, ey = scr->scr_height;
2395 int extra_count = 0;
2397 if (dock->type == WM_CLIP && dock != scr->workspaces[scr->current_workspace]->clip)
2398 extra_count = scr->global_icon_count;
2400 /* if the dock is full */
2401 if (dock->icon_count + extra_count >= dock->max_icons)
2402 return False;
2404 if (!wPreferences.flags.nodock && scr->dock) {
2405 if (scr->dock->on_right_side)
2406 ex -= ICON_SIZE + DOCK_EXTRA_SPACE;
2407 else
2408 sx += ICON_SIZE + DOCK_EXTRA_SPACE;
2411 if (ex < dock->x_pos)
2412 ex = dock->x_pos;
2413 if (sx > dock->x_pos + ICON_SIZE)
2414 sx = dock->x_pos + ICON_SIZE;
2415 #define C_NONE 0
2416 #define C_NW 1
2417 #define C_NE 2
2418 #define C_SW 3
2419 #define C_SE 4
2421 /* check if clip is in a corner */
2422 if (dock->type == WM_CLIP) {
2423 if (dock->x_pos < 1 && dock->y_pos < 1)
2424 corner = C_NE;
2425 else if (dock->x_pos < 1 && dock->y_pos >= (ey - ICON_SIZE))
2426 corner = C_SE;
2427 else if (dock->x_pos >= (ex - ICON_SIZE) && dock->y_pos >= (ey - ICON_SIZE))
2428 corner = C_SW;
2429 else if (dock->x_pos >= (ex - ICON_SIZE) && dock->y_pos < 1)
2430 corner = C_NW;
2431 else
2432 corner = C_NONE;
2433 } else {
2434 corner = C_NONE;
2437 /* If the clip is in the corner, use only slots that are in the border
2438 * of the screen */
2439 if (corner != C_NONE) {
2440 char *hmap, *vmap;
2441 int hcount, vcount;
2443 hcount = WMIN(dock->max_icons, scr->scr_width / ICON_SIZE);
2444 vcount = WMIN(dock->max_icons, scr->scr_height / ICON_SIZE);
2445 hmap = wmalloc(hcount + 1);
2446 vmap = wmalloc(vcount + 1);
2448 /* mark used positions */
2449 switch (corner) {
2450 case C_NE:
2451 for (i = 0; i < dock->max_icons; i++) {
2452 btn = dock->icon_array[i];
2453 if (!btn)
2454 continue;
2456 if (btn->xindex == 0 && btn->yindex > 0 && btn->yindex < vcount)
2457 vmap[btn->yindex] = 1;
2458 else if (btn->yindex == 0 && btn->xindex > 0 && btn->xindex < hcount)
2459 hmap[btn->xindex] = 1;
2461 for (chain = scr->global_icons; chain != NULL; chain = chain->next) {
2462 btn = chain->aicon;
2463 if (btn->xindex == 0 && btn->yindex > 0 && btn->yindex < vcount)
2464 vmap[btn->yindex] = 1;
2465 else if (btn->yindex == 0 && btn->xindex > 0 && btn->xindex < hcount)
2466 hmap[btn->xindex] = 1;
2468 break;
2469 case C_NW:
2470 for (i = 0; i < dock->max_icons; i++) {
2471 btn = dock->icon_array[i];
2472 if (!btn)
2473 continue;
2475 if (btn->xindex == 0 && btn->yindex > 0 && btn->yindex < vcount)
2476 vmap[btn->yindex] = 1;
2477 else if (btn->yindex == 0 && btn->xindex < 0 && btn->xindex > -hcount)
2478 hmap[-btn->xindex] = 1;
2480 for (chain = scr->global_icons; chain != NULL; chain = chain->next) {
2481 btn = chain->aicon;
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 break;
2488 case C_SE:
2489 for (i = 0; i < dock->max_icons; i++) {
2490 btn = dock->icon_array[i];
2491 if (!btn)
2492 continue;
2494 if (btn->xindex == 0 && btn->yindex < 0 && btn->yindex > -vcount)
2495 vmap[-btn->yindex] = 1;
2496 else if (btn->yindex == 0 && btn->xindex > 0 && btn->xindex < hcount)
2497 hmap[btn->xindex] = 1;
2499 for (chain = scr->global_icons; chain != NULL; chain = chain->next) {
2500 btn = chain->aicon;
2501 if (btn->xindex == 0 && btn->yindex < 0 && btn->yindex > -vcount)
2502 vmap[-btn->yindex] = 1;
2503 else if (btn->yindex == 0 && btn->xindex > 0 && btn->xindex < hcount)
2504 hmap[btn->xindex] = 1;
2506 break;
2507 case C_SW:
2508 default:
2509 for (i = 0; i < dock->max_icons; i++) {
2510 btn = dock->icon_array[i];
2511 if (!btn)
2512 continue;
2514 if (btn->xindex == 0 && btn->yindex < 0 && btn->yindex > -vcount)
2515 vmap[-btn->yindex] = 1;
2516 else if (btn->yindex == 0 && btn->xindex < 0 && btn->xindex > -hcount)
2517 hmap[-btn->xindex] = 1;
2519 for (chain = scr->global_icons; chain != NULL; chain = chain->next) {
2520 btn = chain->aicon;
2521 if (btn->xindex == 0 && btn->yindex < 0 && btn->yindex > -vcount)
2522 vmap[-btn->yindex] = 1;
2523 else if (btn->yindex == 0 && btn->xindex < 0 && btn->xindex > -hcount)
2524 hmap[-btn->xindex] = 1;
2527 x = 0;
2528 y = 0;
2529 done = 0;
2530 /* search a vacant slot */
2531 for (i = 1; i < WMAX(vcount, hcount); i++) {
2532 if (i < vcount && vmap[i] == 0) {
2533 /* found a slot */
2534 x = 0;
2535 y = i;
2536 done = 1;
2537 break;
2538 } else if (i < hcount && hmap[i] == 0) {
2539 /* found a slot */
2540 x = i;
2541 y = 0;
2542 done = 1;
2543 break;
2546 wfree(vmap);
2547 wfree(hmap);
2548 /* If found a slot, translate and return */
2549 if (done) {
2550 if (corner == C_NW || corner == C_NE)
2551 *y_pos = y;
2552 else
2553 *y_pos = -y;
2555 if (corner == C_NE || corner == C_SE)
2556 *x_pos = x;
2557 else
2558 *x_pos = -x;
2560 return True;
2562 /* else, try to find a slot somewhere else */
2565 /* a map of mwidth x mwidth would be enough if we allowed icons to be
2566 * placed outside of screen */
2567 mwidth = (int)ceil(sqrt(dock->max_icons));
2569 /* In the worst case (the clip is in the corner of the screen),
2570 * the amount of icons that fit in the clip is smaller.
2571 * Double the map to get a safe value.
2573 mwidth += mwidth;
2575 r = (mwidth - 1) / 2;
2577 slot_map = wmalloc(mwidth * mwidth);
2579 #define XY2OFS(x,y) (WMAX(abs(x),abs(y)) > r) ? 0 : (((y)+r)*(mwidth)+(x)+r)
2581 /* mark used slots in the map. If the slot falls outside the map
2582 * (for example, when all icons are placed in line), ignore them. */
2583 for (i = 0; i < dock->max_icons; i++) {
2584 btn = dock->icon_array[i];
2585 if (btn)
2586 slot_map[XY2OFS(btn->xindex, btn->yindex)] = 1;
2589 for (chain = scr->global_icons; chain != NULL; chain = chain->next)
2590 slot_map[XY2OFS(chain->aicon->xindex, chain->aicon->yindex)] = 1;
2592 /* Find closest slot from the center that is free by scanning the
2593 * map from the center to outward in circular passes.
2594 * This will not result in a neat layout, but will be optimal
2595 * in the sense that there will not be holes left.
2597 done = 0;
2598 for (i = 1; i <= r && !done; i++) {
2599 int tx, ty;
2601 /* top and bottom parts of the ring */
2602 for (x = -i; x <= i && !done; x++) {
2603 tx = dock->x_pos + x * ICON_SIZE;
2604 y = -i;
2605 ty = dock->y_pos + y * ICON_SIZE;
2606 if (slot_map[XY2OFS(x, y)] == 0 && onScreen(scr, tx, ty, sx, ex, sy, ey)) {
2607 *x_pos = x;
2608 *y_pos = y;
2609 done = 1;
2610 break;
2612 y = i;
2613 ty = dock->y_pos + y * ICON_SIZE;
2614 if (slot_map[XY2OFS(x, y)] == 0 && onScreen(scr, tx, ty, sx, ex, sy, ey)) {
2615 *x_pos = x;
2616 *y_pos = y;
2617 done = 1;
2618 break;
2621 /* left and right parts of the ring */
2622 for (y = -i + 1; y <= i - 1; y++) {
2623 ty = dock->y_pos + y * ICON_SIZE;
2624 x = -i;
2625 tx = dock->x_pos + x * ICON_SIZE;
2626 if (slot_map[XY2OFS(x, y)] == 0 && onScreen(scr, tx, ty, sx, ex, sy, ey)) {
2627 *x_pos = x;
2628 *y_pos = y;
2629 done = 1;
2630 break;
2632 x = i;
2633 tx = dock->x_pos + x * ICON_SIZE;
2634 if (slot_map[XY2OFS(x, y)] == 0 && onScreen(scr, tx, ty, sx, ex, sy, ey)) {
2635 *x_pos = x;
2636 *y_pos = y;
2637 done = 1;
2638 break;
2642 wfree(slot_map);
2643 #undef XY2OFS
2644 return done;
2647 static void moveDock(WDock *dock, int new_x, int new_y)
2649 WAppIcon *btn;
2650 int i;
2652 dock->x_pos = new_x;
2653 dock->y_pos = new_y;
2654 for (i = 0; i < dock->max_icons; i++) {
2655 btn = dock->icon_array[i];
2656 if (btn) {
2657 btn->x_pos = new_x + btn->xindex * ICON_SIZE;
2658 btn->y_pos = new_y + btn->yindex * ICON_SIZE;
2659 XMoveWindow(dpy, btn->icon->core->window, btn->x_pos, btn->y_pos);
2664 static void swapDock(WDock *dock)
2666 WScreen *scr = dock->screen_ptr;
2667 WAppIcon *btn;
2668 int x, i;
2670 if (dock->on_right_side)
2671 x = dock->x_pos = scr->scr_width - ICON_SIZE - DOCK_EXTRA_SPACE;
2672 else
2673 x = dock->x_pos = DOCK_EXTRA_SPACE;
2675 for (i = 0; i < dock->max_icons; i++) {
2676 btn = dock->icon_array[i];
2677 if (btn) {
2678 btn->x_pos = x;
2679 XMoveWindow(dpy, btn->icon->core->window, btn->x_pos, btn->y_pos);
2683 wScreenUpdateUsableArea(scr);
2686 static pid_t execCommand(WAppIcon *btn, char *command, WSavedState *state)
2688 WScreen *scr = btn->icon->core->screen_ptr;
2689 pid_t pid;
2690 char **argv;
2691 int argc;
2692 char *cmdline;
2694 cmdline = ExpandOptions(scr, command);
2696 if (scr->flags.dnd_data_convertion_status || !cmdline) {
2697 if (cmdline)
2698 wfree(cmdline);
2699 if (state)
2700 wfree(state);
2701 return 0;
2704 wtokensplit(cmdline, &argv, &argc);
2706 if (!argc) {
2707 if (cmdline)
2708 wfree(cmdline);
2709 if (state)
2710 wfree(state);
2711 return 0;
2714 if ((pid = fork()) == 0) {
2715 char **args;
2716 int i;
2718 SetupEnvironment(scr);
2720 #ifdef HAVE_SETSID
2721 setsid();
2722 #endif
2724 args = malloc(sizeof(char *) * (argc + 1));
2725 if (!args)
2726 exit(111);
2728 for (i = 0; i < argc; i++)
2729 args[i] = argv[i];
2731 args[argc] = NULL;
2732 execvp(argv[0], args);
2733 exit(111);
2735 wtokenfree(argv, argc);
2737 if (pid > 0) {
2738 if (!state) {
2739 state = wmalloc(sizeof(WSavedState));
2740 state->hidden = -1;
2741 state->miniaturized = -1;
2742 state->shaded = -1;
2743 if (btn->dock == scr->dock || btn->omnipresent)
2744 state->workspace = -1;
2745 else
2746 state->workspace = scr->current_workspace;
2748 wWindowAddSavedState(btn->wm_instance, btn->wm_class, cmdline, pid, state);
2749 wAddDeathHandler(pid, (WDeathHandler *) trackDeadProcess, btn->dock);
2750 } else if (state) {
2751 wfree(state);
2753 wfree(cmdline);
2754 return pid;
2757 void wDockHideIcons(WDock *dock)
2759 int i;
2761 if (dock == NULL)
2762 return;
2764 for (i = 1; i < dock->max_icons; i++) {
2765 if (dock->icon_array[i])
2766 XUnmapWindow(dpy, dock->icon_array[i]->icon->core->window);
2768 dock->mapped = 0;
2770 dockIconPaint(dock->icon_array[0]);
2773 void wDockShowIcons(WDock *dock)
2775 int i, newlevel;
2776 WAppIcon *btn;
2778 if (dock == NULL)
2779 return;
2781 btn = dock->icon_array[0];
2782 moveDock(dock, btn->x_pos, btn->y_pos);
2784 newlevel = dock->lowered ? WMNormalLevel : WMDockLevel;
2785 ChangeStackingLevel(btn->icon->core, newlevel);
2787 for (i = 1; i < dock->max_icons; i++) {
2788 if (dock->icon_array[i]) {
2789 MoveInStackListAbove(dock->icon_array[i]->icon->core, btn->icon->core);
2790 break;
2794 if (!dock->collapsed) {
2795 for (i = 1; i < dock->max_icons; i++) {
2796 if (dock->icon_array[i])
2797 XMapWindow(dpy, dock->icon_array[i]->icon->core->window);
2800 dock->mapped = 1;
2802 dockIconPaint(btn);
2805 void wDockLower(WDock *dock)
2807 int i;
2809 for (i = 0; i < dock->max_icons; i++) {
2810 if (dock->icon_array[i])
2811 wLowerFrame(dock->icon_array[i]->icon->core);
2815 void wDockRaise(WDock *dock)
2817 int i;
2819 for (i = dock->max_icons - 1; i >= 0; i--) {
2820 if (dock->icon_array[i])
2821 wRaiseFrame(dock->icon_array[i]->icon->core);
2825 void wDockRaiseLower(WDock *dock)
2827 if (!dock->icon_array[0]->icon->core->stacking->above
2828 || (dock->icon_array[0]->icon->core->stacking->window_level
2829 != dock->icon_array[0]->icon->core->stacking->above->stacking->window_level))
2830 wDockLower(dock);
2831 else
2832 wDockRaise(dock);
2835 void wDockFinishLaunch(WDock *dock, WAppIcon *icon)
2837 icon->launching = 0;
2838 icon->relaunching = 0;
2839 dockIconPaint(icon);
2842 WAppIcon *wDockFindIconForWindow(WDock *dock, Window window)
2844 WAppIcon *icon;
2845 int i;
2847 for (i = 0; i < dock->max_icons; i++) {
2848 icon = dock->icon_array[i];
2849 if (icon && icon->main_window == window)
2850 return icon;
2852 return NULL;
2855 void wDockTrackWindowLaunch(WDock *dock, Window window)
2857 WAppIcon *icon;
2858 char *wm_class, *wm_instance;
2859 int i;
2860 Bool firstPass = True;
2861 Bool found = False;
2862 char *command = NULL;
2864 if (!PropGetWMClass(window, &wm_class, &wm_instance) || (!wm_class && !wm_instance))
2865 return;
2867 command = GetCommandForWindow(window);
2868 retry:
2869 for (i = 0; i < dock->max_icons; i++) {
2870 icon = dock->icon_array[i];
2871 if (!icon)
2872 continue;
2874 /* app is already attached to icon */
2875 if (icon->main_window == window) {
2876 found = True;
2877 break;
2880 if ((icon->wm_instance || icon->wm_class)
2881 && (icon->launching || !icon->running)) {
2883 if (icon->wm_instance && wm_instance && strcmp(icon->wm_instance, wm_instance) != 0)
2884 continue;
2886 if (icon->wm_class && wm_class && strcmp(icon->wm_class, wm_class) != 0)
2887 continue;
2889 if (firstPass && command && strcmp(icon->command, command) != 0)
2890 continue;
2892 if (!icon->relaunching) {
2893 WApplication *wapp;
2895 /* Possibly an application that was docked with dockit,
2896 * but the user did not update WMState to indicate that
2897 * it was docked by force */
2898 wapp = wApplicationOf(window);
2899 if (!wapp) {
2900 icon->forced_dock = 1;
2901 icon->running = 0;
2903 if (!icon->forced_dock)
2904 icon->main_window = window;
2906 found = True;
2907 if (!wPreferences.no_animations && !icon->launching &&
2908 !dock->screen_ptr->flags.startup && !dock->collapsed) {
2909 WAppIcon *aicon;
2910 int x0, y0;
2912 icon->launching = 1;
2913 dockIconPaint(icon);
2915 aicon = wAppIconCreateForDock(dock->screen_ptr, NULL,
2916 wm_instance, wm_class, TILE_NORMAL);
2917 /* XXX: can: aicon->icon == NULL ? */
2918 PlaceIcon(dock->screen_ptr, &x0, &y0, wGetHeadForWindow(aicon->icon->owner));
2919 wAppIconMove(aicon, x0, y0);
2920 /* Should this always be lowered? -Dan */
2921 if (dock->lowered)
2922 wLowerFrame(aicon->icon->core);
2923 XMapWindow(dpy, aicon->icon->core->window);
2924 aicon->launching = 1;
2925 wAppIconPaint(aicon);
2926 SlideWindow(aicon->icon->core->window, x0, y0, icon->x_pos, icon->y_pos);
2927 XUnmapWindow(dpy, aicon->icon->core->window);
2928 wAppIconDestroy(aicon);
2930 wDockFinishLaunch(dock, icon);
2931 break;
2935 if (firstPass && !found) {
2936 firstPass = False;
2937 goto retry;
2940 if (command)
2941 wfree(command);
2943 if (wm_class)
2944 free(wm_class);
2945 if (wm_instance)
2946 free(wm_instance);
2949 void wClipUpdateForWorkspaceChange(WScreen *scr, int workspace)
2951 if (!wPreferences.flags.noclip) {
2952 scr->clip_icon->dock = scr->workspaces[workspace]->clip;
2953 if (scr->current_workspace != workspace) {
2954 WDock *old_clip = scr->workspaces[scr->current_workspace]->clip;
2955 WAppIconChain *chain = scr->global_icons;
2957 while (chain) {
2958 moveIconBetweenDocks(chain->aicon->dock,
2959 scr->workspaces[workspace]->clip,
2960 chain->aicon, chain->aicon->xindex, chain->aicon->yindex);
2961 if (scr->workspaces[workspace]->clip->collapsed)
2962 XUnmapWindow(dpy, chain->aicon->icon->core->window);
2963 chain = chain->next;
2966 wDockHideIcons(old_clip);
2967 if (old_clip->auto_raise_lower) {
2968 if (old_clip->auto_raise_magic) {
2969 WMDeleteTimerHandler(old_clip->auto_raise_magic);
2970 old_clip->auto_raise_magic = NULL;
2972 wDockLower(old_clip);
2974 if (old_clip->auto_collapse) {
2975 if (old_clip->auto_expand_magic) {
2976 WMDeleteTimerHandler(old_clip->auto_expand_magic);
2977 old_clip->auto_expand_magic = NULL;
2979 old_clip->collapsed = 1;
2981 wDockShowIcons(scr->workspaces[workspace]->clip);
2986 static void trackDeadProcess(pid_t pid, unsigned char status, WDock *dock)
2988 WAppIcon *icon;
2989 int i;
2991 for (i = 0; i < dock->max_icons; i++) {
2992 icon = dock->icon_array[i];
2993 if (!icon)
2994 continue;
2996 if (icon->launching && icon->pid == pid) {
2997 if (!icon->relaunching) {
2998 icon->running = 0;
2999 icon->main_window = None;
3001 wDockFinishLaunch(dock, icon);
3002 icon->pid = 0;
3003 if (status == 111) {
3004 char msg[PATH_MAX];
3005 char *cmd;
3007 #ifdef XDND
3008 if (icon->drop_launch)
3009 cmd = icon->dnd_command;
3010 else
3011 #endif
3012 if (icon->paste_launch)
3013 cmd = icon->paste_command;
3014 else
3015 cmd = icon->command;
3017 snprintf(msg, sizeof(msg), _("Could not execute command \"%s\""), cmd);
3019 wMessageDialog(dock->screen_ptr, _("Error"), msg, _("OK"), NULL, NULL);
3021 break;
3026 static void toggleLowered(WDock *dock)
3028 WAppIcon *tmp;
3029 int newlevel, i;
3031 /* lower/raise Dock */
3032 if (!dock->lowered) {
3033 newlevel = WMNormalLevel;
3034 dock->lowered = 1;
3035 } else {
3036 newlevel = WMDockLevel;
3037 dock->lowered = 0;
3040 for (i = 0; i < dock->max_icons; i++) {
3041 tmp = dock->icon_array[i];
3042 if (!tmp)
3043 continue;
3045 ChangeStackingLevel(tmp->icon->core, newlevel);
3046 if (dock->lowered)
3047 wLowerFrame(tmp->icon->core);
3050 if (dock->type == WM_DOCK)
3051 wScreenUpdateUsableArea(dock->screen_ptr);
3054 static void toggleCollapsed(WDock *dock)
3056 if (dock->collapsed) {
3057 dock->collapsed = 0;
3058 wDockShowIcons(dock);
3059 } else {
3060 dock->collapsed = 1;
3061 wDockHideIcons(dock);
3065 static void openDockMenu(WDock *dock, WAppIcon *aicon, XEvent *event)
3067 WScreen *scr = dock->screen_ptr;
3068 WObjDescriptor *desc;
3069 WMenuEntry *entry;
3070 WApplication *wapp = NULL;
3071 int index = 0;
3072 int x_pos;
3073 int n_selected;
3074 int appIsRunning = aicon->running && aicon->icon && aicon->icon->owner;
3076 if (dock->type == WM_DOCK) {
3077 /* keep on top */
3078 entry = dock->menu->entries[index];
3079 entry->flags.indicator_on = !dock->lowered;
3080 entry->clientdata = dock;
3081 dock->menu->flags.realized = 0;
3082 } else {
3083 /* clip options */
3084 if (scr->clip_options)
3085 updateClipOptionsMenu(scr->clip_options, dock);
3087 n_selected = numberOfSelectedIcons(dock);
3089 /* Rename Workspace */
3090 entry = dock->menu->entries[++index];
3091 if (aicon == scr->clip_icon) {
3092 entry->callback = renameCallback;
3093 entry->clientdata = dock;
3094 entry->flags.indicator = 0;
3095 entry->text = _("Rename Workspace");
3096 } else {
3097 entry->callback = omnipresentCallback;
3098 entry->clientdata = aicon;
3099 if (n_selected > 0) {
3100 entry->flags.indicator = 0;
3101 entry->text = _("Toggle Omnipresent");
3102 } else {
3103 entry->flags.indicator = 1;
3104 entry->flags.indicator_on = aicon->omnipresent;
3105 entry->flags.indicator_type = MI_CHECK;
3106 entry->text = _("Omnipresent");
3110 /* select/unselect icon */
3111 entry = dock->menu->entries[++index];
3112 entry->clientdata = aicon;
3113 entry->flags.indicator_on = aicon->icon->selected;
3114 wMenuSetEnabled(dock->menu, index, aicon != scr->clip_icon);
3116 /* select/unselect all icons */
3117 entry = dock->menu->entries[++index];
3118 entry->clientdata = aicon;
3119 if (n_selected > 0)
3120 entry->text = _("Unselect All Icons");
3121 else
3122 entry->text = _("Select All Icons");
3124 wMenuSetEnabled(dock->menu, index, dock->icon_count > 1);
3126 /* keep icon(s) */
3127 entry = dock->menu->entries[++index];
3128 entry->clientdata = aicon;
3129 if (n_selected > 1)
3130 entry->text = _("Keep Icons");
3131 else
3132 entry->text = _("Keep Icon");
3134 wMenuSetEnabled(dock->menu, index, dock->icon_count > 1);
3136 /* this is the workspace submenu part */
3137 entry = dock->menu->entries[++index];
3138 if (n_selected > 1)
3139 entry->text = _("Move Icons To");
3140 else
3141 entry->text = _("Move Icon To");
3143 if (scr->clip_submenu)
3144 updateWorkspaceMenu(scr->clip_submenu, aicon);
3146 wMenuSetEnabled(dock->menu, index, !aicon->omnipresent);
3148 /* remove icon(s) */
3149 entry = dock->menu->entries[++index];
3150 entry->clientdata = aicon;
3151 if (n_selected > 1)
3152 entry->text = _("Remove Icons");
3153 else
3154 entry->text = _("Remove Icon");
3156 wMenuSetEnabled(dock->menu, index, dock->icon_count > 1);
3158 /* attract icon(s) */
3159 entry = dock->menu->entries[++index];
3160 entry->clientdata = aicon;
3162 dock->menu->flags.realized = 0;
3163 wMenuRealize(dock->menu);
3166 if (aicon->icon->owner)
3167 wapp = wApplicationOf(aicon->icon->owner->main_window);
3168 else
3169 wapp = NULL;
3171 /* launch */
3172 entry = dock->menu->entries[++index];
3173 entry->clientdata = aicon;
3174 wMenuSetEnabled(dock->menu, index, aicon->command != NULL);
3176 /* unhide here */
3177 entry = dock->menu->entries[++index];
3178 entry->clientdata = aicon;
3179 if (wapp && wapp->flags.hidden)
3180 entry->text = _("Unhide Here");
3181 else
3182 entry->text = _("Bring Here");
3184 wMenuSetEnabled(dock->menu, index, appIsRunning);
3186 /* hide */
3187 entry = dock->menu->entries[++index];
3188 entry->clientdata = aicon;
3189 if (wapp && wapp->flags.hidden)
3190 entry->text = _("Unhide");
3191 else
3192 entry->text = _("Hide");
3194 wMenuSetEnabled(dock->menu, index, appIsRunning);
3196 /* settings */
3197 entry = dock->menu->entries[++index];
3198 entry->clientdata = aicon;
3199 wMenuSetEnabled(dock->menu, index, !aicon->editing && !wPreferences.flags.noupdates);
3201 /* kill */
3202 entry = dock->menu->entries[++index];
3203 entry->clientdata = aicon;
3204 wMenuSetEnabled(dock->menu, index, appIsRunning);
3206 if (!dock->menu->flags.realized)
3207 wMenuRealize(dock->menu);
3209 if (dock->type == WM_CLIP) {
3210 /*x_pos = event->xbutton.x_root+2; */
3211 x_pos = event->xbutton.x_root - dock->menu->frame->core->width / 2 - 1;
3212 if (x_pos < 0) {
3213 x_pos = 0;
3214 } else if (x_pos + dock->menu->frame->core->width > scr->scr_width - 2) {
3215 x_pos = scr->scr_width - dock->menu->frame->core->width - 4;
3217 } else {
3218 x_pos = dock->on_right_side ? scr->scr_width - dock->menu->frame->core->width - 3 : 0;
3221 wMenuMapAt(dock->menu, x_pos, event->xbutton.y_root + 2, False);
3223 /* allow drag select */
3224 event->xany.send_event = True;
3225 desc = &dock->menu->menu->descriptor;
3226 (*desc->handle_mousedown) (desc, event);
3229 /******************************************************************/
3230 static void iconDblClick(WObjDescriptor *desc, XEvent *event)
3232 WAppIcon *btn = desc->parent;
3233 WDock *dock = btn->dock;
3234 WApplication *wapp = NULL;
3235 int unhideHere = 0;
3237 if (btn->icon->owner && !(event->xbutton.state & ControlMask)) {
3238 wapp = wApplicationOf(btn->icon->owner->main_window);
3240 assert(wapp != NULL);
3242 unhideHere = (event->xbutton.state & ShiftMask);
3244 /* go to the last workspace that the user worked on the app */
3245 if (wapp->last_workspace != dock->screen_ptr->current_workspace && !unhideHere)
3246 wWorkspaceChange(dock->screen_ptr, wapp->last_workspace);
3248 wUnhideApplication(wapp, event->xbutton.button == Button2, unhideHere);
3250 if (event->xbutton.state & MOD_MASK)
3251 wHideOtherApplications(btn->icon->owner);
3252 } else {
3253 if (event->xbutton.button == Button1) {
3254 if (event->xbutton.state & MOD_MASK) {
3255 /* raise/lower dock */
3256 toggleLowered(dock);
3257 } else if (btn == dock->screen_ptr->clip_icon) {
3258 if (getClipButton(event->xbutton.x, event->xbutton.y) == CLIP_IDLE)
3259 toggleCollapsed(dock);
3260 else
3261 handleClipChangeWorkspace(dock->screen_ptr, event);
3262 } else if (btn->command) {
3263 if (!btn->launching && (!btn->running || (event->xbutton.state & ControlMask)))
3264 launchDockedApplication(btn, False);
3270 static void handleDockMove(WDock *dock, WAppIcon *aicon, XEvent *event)
3272 WScreen *scr = dock->screen_ptr;
3273 int ofs_x = event->xbutton.x, ofs_y = event->xbutton.y;
3274 int x, y;
3275 XEvent ev;
3276 int grabbed = 0, swapped = 0, done;
3277 Pixmap ghost = None;
3278 int superfluous = wPreferences.superfluous; /* we catch it to avoid problems */
3280 if (XGrabPointer(dpy, aicon->icon->core->window, True, ButtonMotionMask
3281 | ButtonReleaseMask | ButtonPressMask, GrabModeAsync,
3282 GrabModeAsync, None, None, CurrentTime) != GrabSuccess)
3283 wwarning("pointer grab failed for dock move");
3285 y = 0;
3286 for (x = 0; x < dock->max_icons; x++) {
3287 if (dock->icon_array[x] != NULL && dock->icon_array[x]->yindex > y)
3288 y = dock->icon_array[x]->yindex;
3290 y++;
3291 XResizeWindow(dpy, scr->dock_shadow, ICON_SIZE, ICON_SIZE * y);
3293 done = 0;
3294 while (!done) {
3295 WMMaskEvent(dpy, PointerMotionMask | ButtonReleaseMask | ButtonPressMask
3296 | ButtonMotionMask | ExposureMask | EnterWindowMask, &ev);
3297 switch (ev.type) {
3298 case Expose:
3299 WMHandleEvent(&ev);
3300 break;
3302 case EnterNotify:
3303 /* It means the cursor moved so fast that it entered
3304 * something else (if moving slowly, it would have
3305 * stayed in the dock that is being moved. Ignore such
3306 * "spurious" EnterNotifiy's */
3307 break;
3309 case MotionNotify:
3310 if (!grabbed) {
3311 if (abs(ofs_x - ev.xmotion.x) >= MOVE_THRESHOLD
3312 || abs(ofs_y - ev.xmotion.y) >= MOVE_THRESHOLD) {
3313 XChangeActivePointerGrab(dpy, ButtonMotionMask
3314 | ButtonReleaseMask | ButtonPressMask,
3315 wCursor[WCUR_MOVE], CurrentTime);
3316 grabbed = 1;
3318 break;
3320 if (dock->type == WM_CLIP) {
3321 x = ev.xmotion.x_root - ofs_x;
3322 y = ev.xmotion.y_root - ofs_y;
3323 wScreenKeepInside(scr, &x, &y, ICON_SIZE, ICON_SIZE);
3325 moveDock(dock, x, y);
3326 } else {
3327 /* move vertically if pointer is inside the dock */
3328 if ((dock->on_right_side && ev.xmotion.x_root >= dock->x_pos - ICON_SIZE)
3329 || (!dock->on_right_side && ev.xmotion.x_root <= dock->x_pos + ICON_SIZE * 2)) {
3331 x = ev.xmotion.x_root - ofs_x;
3332 y = ev.xmotion.y_root - ofs_y;
3333 wScreenKeepInside(scr, &x, &y, ICON_SIZE, ICON_SIZE);
3334 moveDock(dock, dock->x_pos, y);
3336 /* move horizontally to change sides */
3337 x = ev.xmotion.x_root - ofs_x;
3338 if (!dock->on_right_side) {
3340 /* is on left */
3341 if (ev.xmotion.x_root > dock->x_pos + ICON_SIZE * 2) {
3342 XMoveWindow(dpy, scr->dock_shadow, scr->scr_width - ICON_SIZE
3343 - DOCK_EXTRA_SPACE - 1, dock->y_pos);
3344 if (superfluous && ghost == None) {
3345 ghost = MakeGhostDock(dock, dock->x_pos,
3346 scr->scr_width - ICON_SIZE
3347 - DOCK_EXTRA_SPACE - 1, dock->y_pos);
3348 XSetWindowBackgroundPixmap(dpy, scr->dock_shadow, ghost);
3349 XClearWindow(dpy, scr->dock_shadow);
3351 XMapRaised(dpy, scr->dock_shadow);
3352 swapped = 1;
3353 } else {
3354 if (superfluous && ghost != None) {
3355 XFreePixmap(dpy, ghost);
3356 ghost = None;
3358 XUnmapWindow(dpy, scr->dock_shadow);
3359 swapped = 0;
3361 } else {
3362 /* is on right */
3363 if (ev.xmotion.x_root < dock->x_pos - ICON_SIZE) {
3364 XMoveWindow(dpy, scr->dock_shadow, DOCK_EXTRA_SPACE, dock->y_pos);
3365 if (superfluous && ghost == None) {
3366 ghost = MakeGhostDock(dock, dock->x_pos,
3367 DOCK_EXTRA_SPACE, dock->y_pos);
3368 XSetWindowBackgroundPixmap(dpy, scr->dock_shadow, ghost);
3369 XClearWindow(dpy, scr->dock_shadow);
3371 XMapRaised(dpy, scr->dock_shadow);
3372 swapped = -1;
3373 } else {
3374 XUnmapWindow(dpy, scr->dock_shadow);
3375 swapped = 0;
3376 if (superfluous && ghost != None) {
3377 XFreePixmap(dpy, ghost);
3378 ghost = None;
3383 break;
3385 case ButtonPress:
3386 break;
3388 case ButtonRelease:
3389 if (ev.xbutton.button != event->xbutton.button)
3390 break;
3391 XUngrabPointer(dpy, CurrentTime);
3392 XUnmapWindow(dpy, scr->dock_shadow);
3393 XResizeWindow(dpy, scr->dock_shadow, ICON_SIZE, ICON_SIZE);
3394 if (dock->type == WM_DOCK) {
3395 if (swapped != 0) {
3396 if (swapped > 0)
3397 dock->on_right_side = 1;
3398 else
3399 dock->on_right_side = 0;
3400 swapDock(dock);
3401 wArrangeIcons(scr, False);
3404 done = 1;
3405 break;
3408 if (superfluous) {
3409 if (ghost != None)
3410 XFreePixmap(dpy, ghost);
3411 XSetWindowBackground(dpy, scr->dock_shadow, scr->white_pixel);
3415 static Bool handleIconMove(WDock *dock, WAppIcon *aicon, XEvent *event)
3417 WScreen *scr = dock->screen_ptr;
3418 Window wins[2];
3419 WIcon *icon = aicon->icon;
3420 WDock *dock2 = NULL, *last_dock = dock, *clip = NULL;
3421 int ondock, grabbed = 0, change_dock = 0, collapsed = 0;
3422 XEvent ev;
3423 int x = aicon->x_pos, y = aicon->y_pos;
3424 int ofs_x = event->xbutton.x, ofs_y = event->xbutton.y;
3425 int shad_x = x, shad_y = y;
3426 int ix = aicon->xindex, iy = aicon->yindex;
3427 int tmp;
3428 Pixmap ghost = None;
3429 Bool docked;
3430 int superfluous = wPreferences.superfluous; /* we catch it to avoid problems */
3431 int omnipresent = aicon->omnipresent; /* this must be cached!!! */
3432 Bool hasMoved = False;
3434 if (wPreferences.flags.noupdates)
3435 return hasMoved;
3437 if (XGrabPointer(dpy, icon->core->window, True, ButtonMotionMask
3438 | ButtonReleaseMask | ButtonPressMask, GrabModeAsync,
3439 GrabModeAsync, None, None, CurrentTime) != GrabSuccess) {
3442 if (!(event->xbutton.state & MOD_MASK))
3443 wRaiseFrame(icon->core);
3445 if (!wPreferences.flags.noclip)
3446 clip = scr->workspaces[scr->current_workspace]->clip;
3448 if (dock == scr->dock && !wPreferences.flags.noclip)
3449 dock2 = clip;
3450 else if (dock != scr->dock && !wPreferences.flags.nodock)
3451 dock2 = scr->dock;
3453 wins[0] = icon->core->window;
3454 wins[1] = scr->dock_shadow;
3455 XRestackWindows(dpy, wins, 2);
3456 XMoveResizeWindow(dpy, scr->dock_shadow, aicon->x_pos, aicon->y_pos, ICON_SIZE, ICON_SIZE);
3457 if (superfluous) {
3458 if (icon->pixmap != None)
3459 ghost = MakeGhostIcon(scr, icon->pixmap);
3460 else
3461 ghost = MakeGhostIcon(scr, icon->core->window);
3463 XSetWindowBackgroundPixmap(dpy, scr->dock_shadow, ghost);
3464 XClearWindow(dpy, scr->dock_shadow);
3466 XMapWindow(dpy, scr->dock_shadow);
3468 ondock = 1;
3470 while (1) {
3471 XMaskEvent(dpy, PointerMotionMask | ButtonReleaseMask | ButtonPressMask
3472 | ButtonMotionMask | ExposureMask, &ev);
3473 switch (ev.type) {
3474 case Expose:
3475 WMHandleEvent(&ev);
3476 break;
3478 case MotionNotify:
3479 hasMoved = True;
3480 if (!grabbed) {
3481 if (abs(ofs_x - ev.xmotion.x) >= MOVE_THRESHOLD
3482 || abs(ofs_y - ev.xmotion.y) >= MOVE_THRESHOLD) {
3483 XChangeActivePointerGrab(dpy, ButtonMotionMask
3484 | ButtonReleaseMask | ButtonPressMask,
3485 wCursor[WCUR_MOVE], CurrentTime);
3486 grabbed = 1;
3487 } else {
3488 break;
3492 if (omnipresent) {
3493 int i;
3494 for (i = 0; i < scr->workspace_count; i++) {
3495 if (i == scr->current_workspace)
3496 continue;
3497 wDockShowIcons(scr->workspaces[i]->clip);
3501 x = ev.xmotion.x_root - ofs_x;
3502 y = ev.xmotion.y_root - ofs_y;
3503 tmp = wDockSnapIcon(dock, aicon, x, y, &ix, &iy, True);
3504 if (tmp && dock2) {
3505 change_dock = 0;
3506 if (last_dock != dock && collapsed) {
3507 last_dock->collapsed = 1;
3508 wDockHideIcons(last_dock);
3509 collapsed = 0;
3511 if (!collapsed && (collapsed = dock->collapsed)) {
3512 dock->collapsed = 0;
3513 wDockShowIcons(dock);
3515 if (dock->auto_raise_lower)
3516 wDockRaise(dock);
3517 last_dock = dock;
3518 } else if (dock2) {
3519 tmp = wDockSnapIcon(dock2, aicon, x, y, &ix, &iy, False);
3520 if (tmp) {
3521 change_dock = 1;
3522 if (last_dock != dock2 && collapsed) {
3523 last_dock->collapsed = 1;
3524 wDockHideIcons(last_dock);
3525 collapsed = 0;
3527 if (!collapsed && (collapsed = dock2->collapsed)) {
3528 dock2->collapsed = 0;
3529 wDockShowIcons(dock2);
3531 if (dock2->auto_raise_lower)
3532 wDockRaise(dock2);
3533 last_dock = dock2;
3536 if (aicon->launching || aicon->lock || (aicon->running && !(ev.xmotion.state & MOD_MASK))
3537 || (!aicon->running && tmp)) {
3538 shad_x = last_dock->x_pos + ix * wPreferences.icon_size;
3539 shad_y = last_dock->y_pos + iy * wPreferences.icon_size;
3541 XMoveWindow(dpy, scr->dock_shadow, shad_x, shad_y);
3543 if (!ondock)
3544 XMapWindow(dpy, scr->dock_shadow);
3546 ondock = 1;
3547 } else {
3548 if (ondock)
3549 XUnmapWindow(dpy, scr->dock_shadow);
3551 ondock = 0;
3553 XMoveWindow(dpy, icon->core->window, x, y);
3554 break;
3556 case ButtonPress:
3557 break;
3559 case ButtonRelease:
3560 if (ev.xbutton.button != event->xbutton.button)
3561 break;
3562 XUngrabPointer(dpy, CurrentTime);
3563 if (ondock) {
3564 SlideWindow(icon->core->window, x, y, shad_x, shad_y);
3565 XUnmapWindow(dpy, scr->dock_shadow);
3566 if (!change_dock) {
3567 reattachIcon(dock, aicon, ix, iy);
3568 if (clip && dock != clip && clip->auto_raise_lower)
3569 wDockLower(clip);
3570 } else {
3571 docked = moveIconBetweenDocks(dock, dock2, aicon, ix, iy);
3572 if (!docked) {
3573 /* Slide it back if dock rejected it */
3574 SlideWindow(icon->core->window, x, y, aicon->x_pos, aicon->y_pos);
3575 reattachIcon(dock, aicon, aicon->xindex, aicon->yindex);
3577 if (last_dock->type == WM_CLIP && last_dock->auto_collapse)
3578 collapsed = 0;
3580 } else {
3581 aicon->x_pos = x;
3582 aicon->y_pos = y;
3583 if (superfluous) {
3584 if (!aicon->running && !wPreferences.no_animations) {
3585 /* We need to deselect it, even if is deselected in
3586 * wDockDetach(), because else DoKaboom() will fail.
3588 if (aicon->icon->selected)
3589 wIconSelect(aicon->icon);
3591 DoKaboom(scr, aicon->icon->core->window, x, y);
3594 if (clip && clip->auto_raise_lower)
3595 wDockLower(clip);
3596 wDockDetach(dock, aicon);
3598 if (collapsed) {
3599 last_dock->collapsed = 1;
3600 wDockHideIcons(last_dock);
3601 collapsed = 0;
3603 if (superfluous) {
3604 if (ghost != None)
3605 XFreePixmap(dpy, ghost);
3606 XSetWindowBackground(dpy, scr->dock_shadow, scr->white_pixel);
3608 if (omnipresent) {
3609 int i;
3610 for (i = 0; i < scr->workspace_count; i++) {
3611 if (i == scr->current_workspace)
3612 continue;
3613 wDockHideIcons(scr->workspaces[i]->clip);
3616 return hasMoved;;
3619 return False; /* never reached */
3622 static int getClipButton(int px, int py)
3624 int pt = (CLIP_BUTTON_SIZE + 2) * ICON_SIZE / 64;
3626 if (px < 0 || py < 0 || px >= ICON_SIZE || py >= ICON_SIZE)
3627 return CLIP_IDLE;
3629 if (py <= pt - ((int)ICON_SIZE - 1 - px))
3630 return CLIP_FORWARD;
3631 else if (px <= pt - ((int)ICON_SIZE - 1 - py))
3632 return CLIP_REWIND;
3634 return CLIP_IDLE;
3637 static void handleClipChangeWorkspace(WScreen *scr, XEvent *event)
3639 XEvent ev;
3640 int done, direction, new_ws;
3641 int new_dir;
3642 WDock *clip = scr->clip_icon->dock;
3644 direction = getClipButton(event->xbutton.x, event->xbutton.y);
3646 clip->lclip_button_pushed = direction == CLIP_REWIND;
3647 clip->rclip_button_pushed = direction == CLIP_FORWARD;
3649 wClipIconPaint(scr->clip_icon);
3650 done = 0;
3651 while (!done) {
3652 WMMaskEvent(dpy, ExposureMask | ButtonMotionMask | ButtonReleaseMask | ButtonPressMask, &ev);
3653 switch (ev.type) {
3654 case Expose:
3655 WMHandleEvent(&ev);
3656 break;
3658 case MotionNotify:
3659 new_dir = getClipButton(ev.xmotion.x, ev.xmotion.y);
3660 if (new_dir != direction) {
3661 direction = new_dir;
3662 clip->lclip_button_pushed = direction == CLIP_REWIND;
3663 clip->rclip_button_pushed = direction == CLIP_FORWARD;
3664 wClipIconPaint(scr->clip_icon);
3666 break;
3668 case ButtonPress:
3669 break;
3671 case ButtonRelease:
3672 if (ev.xbutton.button == event->xbutton.button)
3673 done = 1;
3677 clip->lclip_button_pushed = 0;
3678 clip->rclip_button_pushed = 0;
3680 new_ws = wPreferences.ws_advance || (event->xbutton.state & ControlMask);
3682 if (direction == CLIP_FORWARD) {
3683 if (scr->current_workspace < scr->workspace_count - 1)
3684 wWorkspaceChange(scr, scr->current_workspace + 1);
3685 else if (new_ws && scr->current_workspace < MAX_WORKSPACES - 1)
3686 wWorkspaceChange(scr, scr->current_workspace + 1);
3687 else if (wPreferences.ws_cycle)
3688 wWorkspaceChange(scr, 0);
3689 } else if (direction == CLIP_REWIND) {
3690 if (scr->current_workspace > 0)
3691 wWorkspaceChange(scr, scr->current_workspace - 1);
3692 else if (scr->current_workspace == 0 && wPreferences.ws_cycle)
3693 wWorkspaceChange(scr, scr->workspace_count - 1);
3696 wClipIconPaint(scr->clip_icon);
3699 static void iconMouseDown(WObjDescriptor *desc, XEvent *event)
3701 WAppIcon *aicon = desc->parent;
3702 WDock *dock = aicon->dock;
3703 WScreen *scr = aicon->icon->core->screen_ptr;
3705 if (aicon->editing || WCHECK_STATE(WSTATE_MODAL))
3706 return;
3708 scr->last_dock = dock;
3710 if (dock->menu->flags.mapped)
3711 wMenuUnmap(dock->menu);
3713 if (IsDoubleClick(scr, event)) {
3714 /* double-click was not in the main clip icon */
3715 if (dock->type != WM_CLIP || aicon->xindex != 0 || aicon->yindex != 0
3716 || getClipButton(event->xbutton.x, event->xbutton.y) == CLIP_IDLE) {
3717 iconDblClick(desc, event);
3718 return;
3722 if (event->xbutton.button == Button1) {
3723 if (event->xbutton.state & MOD_MASK)
3724 wDockLower(dock);
3725 else
3726 wDockRaise(dock);
3728 if ((event->xbutton.state & ShiftMask) && aicon != scr->clip_icon && dock->type != WM_DOCK) {
3729 wIconSelect(aicon->icon);
3730 return;
3733 if (aicon->yindex == 0 && aicon->xindex == 0) {
3734 if (getClipButton(event->xbutton.x, event->xbutton.y) != CLIP_IDLE
3735 && dock->type == WM_CLIP)
3736 handleClipChangeWorkspace(scr, event);
3737 else
3738 handleDockMove(dock, aicon, event);
3739 } else {
3740 Bool hasMoved = handleIconMove(dock, aicon, event);
3741 if (wPreferences.single_click && !hasMoved)
3742 iconDblClick(desc, event);
3744 } else if (event->xbutton.button == Button2 && dock->type == WM_CLIP && aicon == scr->clip_icon) {
3745 if (!scr->clip_ws_menu) {
3746 scr->clip_ws_menu = wWorkspaceMenuMake(scr, False);
3748 if (scr->clip_ws_menu) {
3749 WMenu *wsMenu = scr->clip_ws_menu;
3750 int xpos;
3752 wWorkspaceMenuUpdate(scr, wsMenu);
3754 xpos = event->xbutton.x_root - wsMenu->frame->core->width / 2 - 1;
3755 if (xpos < 0) {
3756 xpos = 0;
3757 } else if (xpos + wsMenu->frame->core->width > scr->scr_width - 2) {
3758 xpos = scr->scr_width - wsMenu->frame->core->width - 4;
3760 wMenuMapAt(wsMenu, xpos, event->xbutton.y_root + 2, False);
3762 desc = &wsMenu->menu->descriptor;
3763 event->xany.send_event = True;
3764 (*desc->handle_mousedown) (desc, event);
3766 } else if (event->xbutton.button == Button2 && dock->type == WM_CLIP &&
3767 (event->xbutton.state & ShiftMask) && aicon != scr->clip_icon) {
3768 wClipMakeIconOmnipresent(aicon, !aicon->omnipresent);
3769 } else if (event->xbutton.button == Button3) {
3770 if (event->xbutton.send_event &&
3771 XGrabPointer(dpy, aicon->icon->core->window, True, ButtonMotionMask
3772 | ButtonReleaseMask | ButtonPressMask, GrabModeAsync,
3773 GrabModeAsync, None, None, CurrentTime) != GrabSuccess) {
3774 wwarning("pointer grab failed for dockicon menu");
3775 return;
3778 openDockMenu(dock, aicon, event);
3779 } else if (event->xbutton.button == Button2) {
3780 WAppIcon *btn = desc->parent;
3782 if (!btn->launching && (!btn->running || (event->xbutton.state & ControlMask)))
3783 launchDockedApplication(btn, True);
3787 static void clipEnterNotify(WObjDescriptor *desc, XEvent *event)
3789 WAppIcon *btn = (WAppIcon *) desc->parent;
3790 WDock *dock;
3791 WScreen *scr;
3793 assert(event->type == EnterNotify);
3795 if (desc->parent_type != WCLASS_DOCK_ICON)
3796 return;
3798 scr = btn->icon->core->screen_ptr;
3799 if (!btn->omnipresent)
3800 dock = btn->dock;
3801 else
3802 dock = scr->workspaces[scr->current_workspace]->clip;
3804 if (!dock || dock->type != WM_CLIP)
3805 return;
3807 /* The auto raise/lower code */
3808 if (dock->auto_lower_magic) {
3809 WMDeleteTimerHandler(dock->auto_lower_magic);
3810 dock->auto_lower_magic = NULL;
3812 if (dock->auto_raise_lower && !dock->auto_raise_magic)
3813 dock->auto_raise_magic = WMAddTimerHandler(AUTO_RAISE_DELAY, clipAutoRaise, (void *)dock);
3815 /* The auto expand/collapse code */
3816 if (dock->auto_collapse_magic) {
3817 WMDeleteTimerHandler(dock->auto_collapse_magic);
3818 dock->auto_collapse_magic = NULL;
3820 if (dock->auto_collapse && !dock->auto_expand_magic)
3821 dock->auto_expand_magic = WMAddTimerHandler(AUTO_EXPAND_DELAY, clipAutoExpand, (void *)dock);
3824 static void clipLeave(WDock *dock)
3826 XEvent event;
3827 WObjDescriptor *desc = NULL;
3829 if (!dock || dock->type != WM_CLIP)
3830 return;
3832 if (XCheckTypedEvent(dpy, EnterNotify, &event) != False) {
3833 if (XFindContext(dpy, event.xcrossing.window, wWinContext,
3834 (XPointer *) & desc) != XCNOENT
3835 && desc && desc->parent_type == WCLASS_DOCK_ICON
3836 && ((WAppIcon *) desc->parent)->dock && ((WAppIcon *) desc->parent)->dock->type == WM_CLIP) {
3837 /* We didn't left the Clip yet */
3838 XPutBackEvent(dpy, &event);
3839 return;
3842 XPutBackEvent(dpy, &event);
3843 } else {
3844 /* We entered a withdrawn window, so we're still in Clip */
3845 return;
3848 if (dock->auto_raise_magic) {
3849 WMDeleteTimerHandler(dock->auto_raise_magic);
3850 dock->auto_raise_magic = NULL;
3852 if (dock->auto_raise_lower && !dock->auto_lower_magic)
3853 dock->auto_lower_magic = WMAddTimerHandler(AUTO_LOWER_DELAY, clipAutoLower, (void *)dock);
3855 if (dock->auto_expand_magic) {
3856 WMDeleteTimerHandler(dock->auto_expand_magic);
3857 dock->auto_expand_magic = NULL;
3859 if (dock->auto_collapse && !dock->auto_collapse_magic)
3860 dock->auto_collapse_magic = WMAddTimerHandler(AUTO_COLLAPSE_DELAY, clipAutoCollapse, (void *)dock);
3863 static void clipLeaveNotify(WObjDescriptor *desc, XEvent *event)
3865 WAppIcon *btn = (WAppIcon *) desc->parent;
3867 assert(event->type == LeaveNotify);
3869 if (desc->parent_type != WCLASS_DOCK_ICON)
3870 return;
3872 clipLeave(btn->dock);
3875 static void clipAutoCollapse(void *cdata)
3877 WDock *dock = (WDock *) cdata;
3879 if (dock->type != WM_CLIP)
3880 return;
3882 if (dock->auto_collapse) {
3883 dock->collapsed = 1;
3884 wDockHideIcons(dock);
3886 dock->auto_collapse_magic = NULL;
3889 static void clipAutoExpand(void *cdata)
3891 WDock *dock = (WDock *) cdata;
3893 if (dock->type != WM_CLIP)
3894 return;
3896 if (dock->auto_collapse) {
3897 dock->collapsed = 0;
3898 wDockShowIcons(dock);
3900 dock->auto_expand_magic = NULL;
3903 static void clipAutoLower(void *cdata)
3905 WDock *dock = (WDock *) cdata;
3907 if (dock->type != WM_CLIP)
3908 return;
3910 if (dock->auto_raise_lower)
3911 wDockLower(dock);
3913 dock->auto_lower_magic = NULL;
3916 static void clipAutoRaise(void *cdata)
3918 WDock *dock = (WDock *) cdata;
3920 if (dock->type != WM_CLIP)
3921 return;
3923 if (dock->auto_raise_lower)
3924 wDockRaise(dock);
3926 dock->auto_raise_magic = NULL;
3929 static Bool iconCanBeOmnipresent(WAppIcon *aicon)
3931 WScreen *scr = aicon->icon->core->screen_ptr;
3932 WDock *clip;
3933 WAppIcon *btn;
3934 int i, j;
3936 for (i = 0; i < scr->workspace_count; i++) {
3937 clip = scr->workspaces[i]->clip;
3939 if (clip == aicon->dock)
3940 continue;
3942 if (clip->icon_count + scr->global_icon_count >= clip->max_icons)
3943 return False; /* Clip is full in some workspace */
3945 for (j = 0; j < clip->max_icons; j++) {
3946 btn = clip->icon_array[j];
3947 if (btn && btn->xindex == aicon->xindex && btn->yindex == aicon->yindex)
3948 return False;
3952 return True;
3955 int wClipMakeIconOmnipresent(WAppIcon *aicon, int omnipresent)
3957 WScreen *scr = aicon->icon->core->screen_ptr;
3958 WAppIconChain *new_entry, *tmp, *tmp1;
3959 int status = WO_SUCCESS;
3961 if ((scr->dock && aicon->dock == scr->dock) || aicon == scr->clip_icon)
3962 return WO_NOT_APPLICABLE;
3964 if (aicon->omnipresent == omnipresent)
3965 return WO_SUCCESS;
3967 if (omnipresent) {
3968 if (iconCanBeOmnipresent(aicon)) {
3969 aicon->omnipresent = 1;
3970 new_entry = wmalloc(sizeof(WAppIconChain));
3971 new_entry->aicon = aicon;
3972 new_entry->next = scr->global_icons;
3973 scr->global_icons = new_entry;
3974 scr->global_icon_count++;
3975 } else {
3976 aicon->omnipresent = 0;
3977 status = WO_FAILED;
3979 } else {
3980 aicon->omnipresent = 0;
3981 if (aicon == scr->global_icons->aicon) {
3982 tmp = scr->global_icons->next;
3983 wfree(scr->global_icons);
3984 scr->global_icons = tmp;
3985 scr->global_icon_count--;
3986 } else {
3987 tmp = scr->global_icons;
3988 while (tmp->next) {
3989 if (tmp->next->aicon == aicon) {
3990 tmp1 = tmp->next->next;
3991 wfree(tmp->next);
3992 tmp->next = tmp1;
3993 scr->global_icon_count--;
3994 break;
3996 tmp = tmp->next;
4001 wAppIconPaint(aicon);
4003 return status;