WMaker: fix memory leak in wDockTrackWindowLaunch (Coverity #50120 and #50121)
[wmaker-crm.git] / src / dock.c
blob3fd3dbe1b6780d19b7425a5057c3e37ffeb3bc94
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 <stdio.h>
28 #include <stdlib.h>
29 #include <libgen.h>
30 #include <string.h>
31 #include <strings.h>
32 #include <unistd.h>
33 #include <math.h>
34 #include <limits.h>
36 #ifndef PATH_MAX
37 #define PATH_MAX DEFAULT_PATH_MAX
38 #endif
40 #include "WindowMaker.h"
41 #include "wcore.h"
42 #include "window.h"
43 #include "icon.h"
44 #include "appicon.h"
45 #include "actions.h"
46 #include "stacking.h"
47 #include "dock.h"
48 #include "dockedapp.h"
49 #include "dialog.h"
50 #include "main.h"
51 #include "properties.h"
52 #include "menu.h"
53 #include "client.h"
54 #include "defaults.h"
55 #include "workspace.h"
56 #include "framewin.h"
57 #include "superfluous.h"
58 #include "xinerama.h"
59 #include "placement.h"
60 #include "misc.h"
61 #include "event.h"
63 /**** Local variables ****/
64 #define CLIP_REWIND 1
65 #define CLIP_IDLE 0
66 #define CLIP_FORWARD 2
68 #define MOD_MASK wPreferences.modifier_mask
69 #define ICON_SIZE wPreferences.icon_size
71 /***** Local variables ****/
73 static WMPropList *dCommand = NULL;
74 static WMPropList *dPasteCommand = NULL;
75 #ifdef XDND /* XXX was OFFIX */
76 static WMPropList *dDropCommand = NULL;
77 #endif
78 static WMPropList *dAutoLaunch, *dLock;
79 static WMPropList *dName, *dForced, *dBuggyApplication, *dYes, *dNo;
80 static WMPropList *dHost, *dDock, *dClip;
81 static WMPropList *dAutoAttractIcons;
83 static WMPropList *dPosition, *dApplications, *dLowered, *dCollapsed;
85 static WMPropList *dAutoCollapse, *dAutoRaiseLower, *dOmnipresent;
87 static WMPropList *dDrawers = NULL;
89 static void dockIconPaint(WAppIcon *btn);
91 static void iconMouseDown(WObjDescriptor *desc, XEvent *event);
93 static pid_t execCommand(WAppIcon *btn, const char *command, WSavedState *state);
95 static void trackDeadProcess(pid_t pid, unsigned char status, WDock *dock);
97 static int getClipButton(int px, int py);
99 static void toggleLowered(WDock *dock);
101 static void toggleCollapsed(WDock *dock);
103 static void clipIconExpose(WObjDescriptor *desc, XEvent *event);
105 static void clipLeave(WDock *dock);
107 static void handleClipChangeWorkspace(WScreen *scr, XEvent *event);
109 static void clipEnterNotify(WObjDescriptor *desc, XEvent *event);
110 static void clipLeaveNotify(WObjDescriptor *desc, XEvent *event);
111 static void clipAutoCollapse(void *cdata);
112 static void clipAutoExpand(void *cdata);
113 static void launchDockedApplication(WAppIcon *btn, Bool withSelection);
115 static void clipAutoLower(void *cdata);
116 static void clipAutoRaise(void *cdata);
117 static WAppIcon *mainIconCreate(WScreen *scr, int type, const char *name);
119 static void drawerIconExpose(WObjDescriptor *desc, XEvent *event);
120 static void removeDrawerCallback(WMenu *menu, WMenuEntry *entry);
121 static void drawerAppendToChain(WScreen *scr, WDock *drawer);
122 static char *findUniqueName(WScreen *scr, const char *instance_basename);
123 static void addADrawerCallback(WMenu *menu, WMenuEntry *entry);
124 static void swapDrawers(WScreen *scr, int new_x);
125 static WDock* getDrawer(WScreen *scr, int y_index);
126 static int indexOfHole(WDock *drawer, WAppIcon *moving_aicon, int redocking);
127 static void drawerConsolidateIcons(WDock *drawer);
129 static int onScreen(WScreen *scr, int x, int y);
131 static void make_keys(void)
133 if (dCommand != NULL)
134 return;
136 dCommand = WMRetainPropList(WMCreatePLString("Command"));
137 dPasteCommand = WMRetainPropList(WMCreatePLString("PasteCommand"));
138 #ifdef XDND
139 dDropCommand = WMRetainPropList(WMCreatePLString("DropCommand"));
140 #endif
141 dLock = WMRetainPropList(WMCreatePLString("Lock"));
142 dAutoLaunch = WMRetainPropList(WMCreatePLString("AutoLaunch"));
143 dName = WMRetainPropList(WMCreatePLString("Name"));
144 dForced = WMRetainPropList(WMCreatePLString("Forced"));
145 dBuggyApplication = WMRetainPropList(WMCreatePLString("BuggyApplication"));
146 dYes = WMRetainPropList(WMCreatePLString("Yes"));
147 dNo = WMRetainPropList(WMCreatePLString("No"));
148 dHost = WMRetainPropList(WMCreatePLString("Host"));
150 dPosition = WMCreatePLString("Position");
151 dApplications = WMCreatePLString("Applications");
152 dLowered = WMCreatePLString("Lowered");
153 dCollapsed = WMCreatePLString("Collapsed");
154 dAutoCollapse = WMCreatePLString("AutoCollapse");
155 dAutoRaiseLower = WMCreatePLString("AutoRaiseLower");
156 dAutoAttractIcons = WMCreatePLString("AutoAttractIcons");
158 dOmnipresent = WMCreatePLString("Omnipresent");
160 dDock = WMCreatePLString("Dock");
161 dClip = WMCreatePLString("Clip");
162 dDrawers = WMCreatePLString("Drawers");
165 static void renameCallback(WMenu *menu, WMenuEntry *entry)
167 WDock *dock = entry->clientdata;
168 char buffer[128];
169 int wspace;
170 char *name;
172 /* Parameter not used, but tell the compiler that it is ok */
173 (void) menu;
175 assert(entry->clientdata != NULL);
177 wspace = w_global.workspace.current;
179 name = wstrdup(w_global.workspace.array[wspace]->name);
181 snprintf(buffer, sizeof(buffer), _("Type the name for workspace %i:"), wspace + 1);
182 if (wInputDialog(dock->screen_ptr, _("Rename Workspace"), buffer, &name))
183 wWorkspaceRename(dock->screen_ptr, wspace, name);
185 wfree(name);
188 static void toggleLoweredCallback(WMenu *menu, WMenuEntry *entry)
190 assert(entry->clientdata != NULL);
192 toggleLowered(entry->clientdata);
194 entry->flags.indicator_on = !(((WDock *) entry->clientdata)->lowered);
196 wMenuPaint(menu);
199 static int matchWindow(const void *item, const void *cdata)
201 return (((WFakeGroupLeader *) item)->leader == (Window) cdata);
204 static void killCallback(WMenu *menu, WMenuEntry *entry)
206 WScreen *scr = menu->menu->screen_ptr;
207 WAppIcon *icon;
208 WFakeGroupLeader *fPtr;
209 char *buffer, *shortname, **argv;
210 int argc;
212 if (!WCHECK_STATE(WSTATE_NORMAL))
213 return;
215 assert(entry->clientdata != NULL);
217 icon = (WAppIcon *) entry->clientdata;
219 icon->editing = 1;
221 WCHANGE_STATE(WSTATE_MODAL);
223 /* strip away dir names */
224 shortname = basename(icon->command);
225 /* separate out command options */
226 wtokensplit(shortname, &argv, &argc);
228 buffer = wstrconcat(argv[0],
229 _(" will be forcibly closed.\n"
230 "Any unsaved changes will be lost.\n" "Please confirm."));
232 if (icon->icon && icon->icon->owner) {
233 fPtr = icon->icon->owner->fake_group;
234 } else {
235 /* is this really necessary? can we kill a non-running dock icon? */
236 Window win = icon->main_window;
237 int index;
239 index = WMFindInArray(scr->fakeGroupLeaders, matchWindow, (void *)win);
240 if (index != WANotFound)
241 fPtr = WMGetFromArray(scr->fakeGroupLeaders, index);
242 else
243 fPtr = NULL;
246 if (wPreferences.dont_confirm_kill
247 || wMessageDialog(menu->frame->screen_ptr, _("Kill Application"),
248 buffer, _("Yes"), _("No"), NULL) == WAPRDefault) {
249 if (fPtr != NULL) {
250 WWindow *wwin, *twin;
252 wwin = scr->focused_window;
253 while (wwin) {
254 twin = wwin->prev;
255 if (wwin->fake_group == fPtr)
256 wClientKill(wwin);
258 wwin = twin;
260 } else if (icon->icon && icon->icon->owner) {
261 wClientKill(icon->icon->owner);
265 wfree(buffer);
266 wtokenfree(argv, argc);
268 icon->editing = 0;
270 WCHANGE_STATE(WSTATE_NORMAL);
273 /* TODO: replace this function with a member of the dock struct */
274 static int numberOfSelectedIcons(WDock *dock)
276 WAppIcon *aicon;
277 int i, n;
279 n = 0;
280 for (i = 1; i < dock->max_icons; i++) {
281 aicon = dock->icon_array[i];
282 if (aicon && aicon->icon->selected)
283 n++;
286 return n;
289 static WMArray *getSelected(WDock *dock)
291 WMArray *ret = WMCreateArray(8);
292 WAppIcon *btn;
293 int i;
295 for (i = 1; i < dock->max_icons; i++) {
296 btn = dock->icon_array[i];
297 if (btn && btn->icon->selected)
298 WMAddToArray(ret, btn);
301 return ret;
304 static void paintClipButtons(WAppIcon *clipIcon, Bool lpushed, Bool rpushed)
306 Window win = clipIcon->icon->core->window;
307 WScreen *scr = clipIcon->icon->core->screen_ptr;
308 XPoint p[4];
309 int pt = CLIP_BUTTON_SIZE * ICON_SIZE / 64;
310 int tp = ICON_SIZE - pt;
311 int as = pt - 15; /* 15 = 5+5+5 */
312 GC gc = scr->draw_gc; /* maybe use WMColorGC() instead here? */
313 WMColor *color;
315 color = scr->clip_title_color[CLIP_NORMAL];
317 XSetForeground(dpy, gc, WMColorPixel(color));
319 if (rpushed) {
320 p[0].x = tp + 1;
321 p[0].y = 1;
322 p[1].x = ICON_SIZE - 2;
323 p[1].y = 1;
324 p[2].x = ICON_SIZE - 2;
325 p[2].y = pt - 1;
326 } else if (lpushed) {
327 p[0].x = 1;
328 p[0].y = tp;
329 p[1].x = pt;
330 p[1].y = ICON_SIZE - 2;
331 p[2].x = 1;
332 p[2].y = ICON_SIZE - 2;
334 if (lpushed || rpushed) {
335 XSetForeground(dpy, scr->draw_gc, scr->white_pixel);
336 XFillPolygon(dpy, win, scr->draw_gc, p, 3, Convex, CoordModeOrigin);
337 XSetForeground(dpy, scr->draw_gc, scr->black_pixel);
340 /* top right arrow */
341 p[0].x = p[3].x = ICON_SIZE - 5 - as;
342 p[0].y = p[3].y = 5;
343 p[1].x = ICON_SIZE - 6;
344 p[1].y = 5;
345 p[2].x = ICON_SIZE - 6;
346 p[2].y = 4 + as;
347 if (rpushed) {
348 XFillPolygon(dpy, win, scr->draw_gc, p, 3, Convex, CoordModeOrigin);
349 XDrawLines(dpy, win, scr->draw_gc, p, 4, CoordModeOrigin);
350 } else {
351 XFillPolygon(dpy, win, gc, p, 3, Convex, CoordModeOrigin);
352 XDrawLines(dpy, win, gc, p, 4, CoordModeOrigin);
355 /* bottom left arrow */
356 p[0].x = p[3].x = 5;
357 p[0].y = p[3].y = ICON_SIZE - 5 - as;
358 p[1].x = 5;
359 p[1].y = ICON_SIZE - 6;
360 p[2].x = 4 + as;
361 p[2].y = ICON_SIZE - 6;
362 if (lpushed) {
363 XFillPolygon(dpy, win, scr->draw_gc, p, 3, Convex, CoordModeOrigin);
364 XDrawLines(dpy, win, scr->draw_gc, p, 4, CoordModeOrigin);
365 } else {
366 XFillPolygon(dpy, win, gc, p, 3, Convex, CoordModeOrigin);
367 XDrawLines(dpy, win, gc, p, 4, CoordModeOrigin);
371 RImage *wClipMakeTile(RImage *normalTile)
373 RImage *tile = RCloneImage(normalTile);
374 RColor black;
375 RColor dark;
376 RColor light;
377 int pt, tp;
378 int as;
380 pt = CLIP_BUTTON_SIZE * wPreferences.icon_size / 64;
381 tp = wPreferences.icon_size - 1 - pt;
382 as = pt - 15;
384 black.alpha = 255;
385 black.red = black.green = black.blue = 0;
387 dark.alpha = 0;
388 dark.red = dark.green = dark.blue = 60;
390 light.alpha = 0;
391 light.red = light.green = light.blue = 80;
393 /* top right */
394 ROperateLine(tile, RSubtractOperation, tp, 0, wPreferences.icon_size - 2, pt - 1, &dark);
395 RDrawLine(tile, tp - 1, 0, wPreferences.icon_size - 1, pt + 1, &black);
396 ROperateLine(tile, RAddOperation, tp, 2, wPreferences.icon_size - 3, pt, &light);
398 /* arrow bevel */
399 ROperateLine(tile, RSubtractOperation, ICON_SIZE - 7 - as, 4, ICON_SIZE - 5, 4, &dark);
400 ROperateLine(tile, RSubtractOperation, ICON_SIZE - 6 - as, 5, ICON_SIZE - 5, 6 + as, &dark);
401 ROperateLine(tile, RAddOperation, ICON_SIZE - 5, 4, ICON_SIZE - 5, 6 + as, &light);
403 /* bottom left */
404 ROperateLine(tile, RAddOperation, 2, tp + 2, pt - 2, wPreferences.icon_size - 3, &dark);
405 RDrawLine(tile, 0, tp - 1, pt + 1, wPreferences.icon_size - 1, &black);
406 ROperateLine(tile, RSubtractOperation, 0, tp - 2, pt + 1, wPreferences.icon_size - 2, &light);
408 /* arrow bevel */
409 ROperateLine(tile, RSubtractOperation, 4, ICON_SIZE - 7 - as, 4, ICON_SIZE - 5, &dark);
410 ROperateLine(tile, RSubtractOperation, 5, ICON_SIZE - 6 - as, 6 + as, ICON_SIZE - 5, &dark);
411 ROperateLine(tile, RAddOperation, 4, ICON_SIZE - 5, 6 + as, ICON_SIZE - 5, &light);
413 return tile;
416 static void omnipresentCallback(WMenu *menu, WMenuEntry *entry)
418 WAppIcon *clickedIcon = entry->clientdata;
419 WAppIcon *aicon;
420 WDock *dock;
421 WMArray *selectedIcons;
422 WMArrayIterator iter;
423 int failed;
425 /* Parameter not used, but tell the compiler that it is ok */
426 (void) menu;
428 assert(entry->clientdata != NULL);
430 dock = clickedIcon->dock;
432 selectedIcons = getSelected(dock);
434 if (!WMGetArrayItemCount(selectedIcons))
435 WMAddToArray(selectedIcons, clickedIcon);
437 failed = 0;
438 WM_ITERATE_ARRAY(selectedIcons, aicon, iter) {
439 if (wClipMakeIconOmnipresent(aicon, !aicon->omnipresent) == WO_FAILED)
440 failed++;
441 else if (aicon->icon->selected)
442 wIconSelect(aicon->icon);
444 WMFreeArray(selectedIcons);
446 if (failed > 1) {
447 wMessageDialog(dock->screen_ptr, _("Warning"),
448 _("Some icons cannot be made omnipresent. "
449 "Please make sure that no other icon is "
450 "docked in the same positions on the other "
451 "workspaces and the Clip is not full in "
452 "some workspace."), _("OK"), NULL, NULL);
453 } else if (failed == 1) {
454 wMessageDialog(dock->screen_ptr, _("Warning"),
455 _("Icon cannot be made omnipresent. "
456 "Please make sure that no other icon is "
457 "docked in the same position on the other "
458 "workspaces and the Clip is not full in "
459 "some workspace."), _("OK"), NULL, NULL);
463 static void removeIcons(WMArray *icons, WDock *dock)
465 WAppIcon *aicon;
466 int keepit;
467 WMArrayIterator it;
469 WM_ITERATE_ARRAY(icons, aicon, it) {
470 keepit = aicon->running && wApplicationOf(aicon->main_window);
471 wDockDetach(dock, aicon);
472 if (keepit) {
473 /* XXX: can: aicon->icon == NULL ? */
474 PlaceIcon(dock->screen_ptr, &aicon->x_pos, &aicon->y_pos,
475 wGetHeadForWindow(aicon->icon->owner));
476 XMoveWindow(dpy, aicon->icon->core->window, aicon->x_pos, aicon->y_pos);
477 if (!dock->mapped || dock->collapsed)
478 XMapWindow(dpy, aicon->icon->core->window);
481 WMFreeArray(icons);
483 if (wPreferences.auto_arrange_icons)
484 wArrangeIcons(dock->screen_ptr, True);
487 static void removeIconsCallback(WMenu *menu, WMenuEntry *entry)
489 WAppIcon *clickedIcon = (WAppIcon *) entry->clientdata;
490 WDock *dock;
491 WMArray *selectedIcons;
493 /* Parameter not used, but tell the compiler that it is ok */
494 (void) menu;
496 assert(clickedIcon != NULL);
498 dock = clickedIcon->dock;
500 selectedIcons = getSelected(dock);
502 if (WMGetArrayItemCount(selectedIcons)) {
503 if (wMessageDialog(dock->screen_ptr,
504 dock->type == WM_CLIP ? _("Workspace Clip") : _("Drawer"),
505 _("All selected icons will be removed!"),
506 _("OK"), _("Cancel"), NULL) != WAPRDefault) {
507 WMFreeArray(selectedIcons);
508 return;
510 } else {
511 if (clickedIcon->xindex == 0 && clickedIcon->yindex == 0) {
512 WMFreeArray(selectedIcons);
513 return;
515 WMAddToArray(selectedIcons, clickedIcon);
518 removeIcons(selectedIcons, dock);
520 if (dock->type == WM_DRAWER) {
521 drawerConsolidateIcons(dock);
525 static void keepIconsCallback(WMenu *menu, WMenuEntry *entry)
527 WAppIcon *clickedIcon = (WAppIcon *) entry->clientdata;
528 WDock *dock;
529 WAppIcon *aicon;
530 WMArray *selectedIcons;
531 WMArrayIterator it;
533 /* Parameter not used, but tell the compiler that it is ok */
534 (void) menu;
536 assert(clickedIcon != NULL);
537 dock = clickedIcon->dock;
539 selectedIcons = getSelected(dock);
541 if (!WMGetArrayItemCount(selectedIcons)
542 && clickedIcon != w_global.clip.icon) {
543 char *command = NULL;
545 if (!clickedIcon->command && !clickedIcon->editing) {
546 clickedIcon->editing = 1;
547 if (wInputDialog(dock->screen_ptr, _("Keep Icon"),
548 _("Type the command used to launch the application"), &command)) {
549 if (command && (command[0] == 0 || (command[0] == '-' && command[1] == 0))) {
550 wfree(command);
551 command = NULL;
553 clickedIcon->command = command;
554 clickedIcon->editing = 0;
555 } else {
556 clickedIcon->editing = 0;
557 if (command)
558 wfree(command);
559 WMFreeArray(selectedIcons);
560 return;
564 WMAddToArray(selectedIcons, clickedIcon);
567 WM_ITERATE_ARRAY(selectedIcons, aicon, it) {
568 if (aicon->icon->selected)
569 wIconSelect(aicon->icon);
571 if (aicon && aicon->attracted && aicon->command) {
572 aicon->attracted = 0;
573 if (aicon->icon->shadowed) {
574 aicon->icon->shadowed = 0;
577 * Update icon pixmap, RImage doesn't change,
578 * so call wIconUpdate is not needed
580 update_icon_pixmap(aicon->icon);
582 /* Paint it */
583 wAppIconPaint(aicon);
586 save_appicon(aicon, True);
588 WMFreeArray(selectedIcons);
591 static void toggleAutoAttractCallback(WMenu *menu, WMenuEntry *entry)
593 WDock *dock = (WDock *) entry->clientdata;
594 WScreen *scr = dock->screen_ptr;
596 assert(entry->clientdata != NULL);
598 dock->attract_icons = !dock->attract_icons;
600 entry->flags.indicator_on = dock->attract_icons;
602 wMenuPaint(menu);
604 if (dock->attract_icons) {
605 if (dock->type == WM_DRAWER) {
606 /* The newly auto-attracting dock is a drawer: disable any clip and
607 * previously attracting drawer */
609 if (!wPreferences.flags.noclip) {
610 int i;
611 for (i = 0; i < w_global.workspace.count; i++)
612 w_global.workspace.array[i]->clip->attract_icons = False;
613 /* dock menu will be updated later, when opened */
616 if (scr->attracting_drawer != NULL)
617 scr->attracting_drawer->attract_icons = False;
618 scr->attracting_drawer = dock;
619 } else {
620 /* The newly auto-attracting dock is a clip: disable
621 * previously attracting drawer, if applicable */
622 if (scr->attracting_drawer != NULL) {
623 scr->attracting_drawer->attract_icons = False;
624 /* again, its menu will be updated, later. */
625 scr->attracting_drawer = NULL;
631 static void selectCallback(WMenu *menu, WMenuEntry *entry)
633 WAppIcon *icon = (WAppIcon *) entry->clientdata;
635 assert(icon != NULL);
637 wIconSelect(icon->icon);
639 wMenuPaint(menu);
642 static void colectIconsCallback(WMenu *menu, WMenuEntry *entry)
644 WAppIcon *clickedIcon = (WAppIcon *) entry->clientdata;
645 WDock *clip; /* clip... is a WM_CLIP or a WM_DRAWER */
646 WAppIcon *aicon;
647 int x, y, x_pos, y_pos;
648 Bool update_icon = False;
650 /* Parameter not used, but tell the compiler that it is ok */
651 (void) menu;
653 assert(entry->clientdata != NULL);
654 clip = clickedIcon->dock;
656 aicon = w_global.app_icon_list;
658 while (aicon) {
659 if (!aicon->docked && wDockFindFreeSlot(clip, &x, &y)) {
660 x_pos = clip->x_pos + x * ICON_SIZE;
661 y_pos = clip->y_pos + y * ICON_SIZE;
662 if (aicon->x_pos != x_pos || aicon->y_pos != y_pos)
663 move_window(aicon->icon->core->window, aicon->x_pos, aicon->y_pos, x_pos, y_pos);
665 aicon->attracted = 1;
666 if (!aicon->icon->shadowed) {
667 aicon->icon->shadowed = 1;
668 update_icon = True;
670 wDockAttachIcon(clip, aicon, x, y, update_icon);
671 if (clip->collapsed || !clip->mapped)
672 XUnmapWindow(dpy, aicon->icon->core->window);
674 aicon = aicon->next;
678 static void selectIconsCallback(WMenu *menu, WMenuEntry *entry)
680 WAppIcon *clickedIcon = (WAppIcon *) entry->clientdata;
681 WDock *dock;
682 WMArray *selectedIcons;
683 WMArrayIterator iter;
684 WAppIcon *btn;
685 int i;
687 assert(clickedIcon != NULL);
688 dock = clickedIcon->dock;
690 selectedIcons = getSelected(dock);
692 if (!WMGetArrayItemCount(selectedIcons)) {
693 for (i = 1; i < dock->max_icons; i++) {
694 btn = dock->icon_array[i];
695 if (btn && !btn->icon->selected)
696 wIconSelect(btn->icon);
698 } else {
699 WM_ITERATE_ARRAY(selectedIcons, btn, iter) {
700 wIconSelect(btn->icon);
703 WMFreeArray(selectedIcons);
705 wMenuPaint(menu);
708 static void toggleCollapsedCallback(WMenu *menu, WMenuEntry *entry)
710 assert(entry->clientdata != NULL);
712 toggleCollapsed(entry->clientdata);
714 entry->flags.indicator_on = ((WDock *) entry->clientdata)->collapsed;
716 wMenuPaint(menu);
719 static void toggleAutoCollapseCallback(WMenu *menu, WMenuEntry *entry)
721 WDock *dock;
722 assert(entry->clientdata != NULL);
724 dock = (WDock *) entry->clientdata;
726 dock->auto_collapse = !dock->auto_collapse;
728 entry->flags.indicator_on = ((WDock *) entry->clientdata)->auto_collapse;
730 wMenuPaint(menu);
733 static void toggleAutoRaiseLower(WDock *dock)
735 WDrawerChain *dc;
737 dock->auto_raise_lower = !dock->auto_raise_lower;
738 if (dock->type == WM_DOCK)
740 for (dc = dock->screen_ptr->drawers; dc != NULL; dc = dc->next) {
741 toggleAutoRaiseLower(dc->adrawer);
746 static void toggleAutoRaiseLowerCallback(WMenu *menu, WMenuEntry *entry)
748 WDock *dock;
750 /* Parameter not used, but tell the compiler that it is ok */
751 (void) menu;
753 assert(entry->clientdata != NULL);
755 dock = (WDock *) entry->clientdata;
757 toggleAutoRaiseLower(dock);
759 entry->flags.indicator_on = ((WDock *) entry->clientdata)->auto_raise_lower;
761 wMenuPaint(menu);
764 static void launchCallback(WMenu *menu, WMenuEntry *entry)
766 WAppIcon *btn = (WAppIcon *) entry->clientdata;
768 /* Parameter not used, but tell the compiler that it is ok */
769 (void) menu;
771 launchDockedApplication(btn, False);
774 static void settingsCallback(WMenu *menu, WMenuEntry *entry)
776 WAppIcon *btn = (WAppIcon *) entry->clientdata;
778 /* Parameter not used, but tell the compiler that it is ok */
779 (void) menu;
781 if (btn->editing)
782 return;
783 ShowDockAppSettingsPanel(btn);
786 static void hideCallback(WMenu *menu, WMenuEntry *entry)
788 WApplication *wapp;
789 WAppIcon *btn = (WAppIcon *) entry->clientdata;
791 /* Parameter not used, but tell the compiler that it is ok */
792 (void) menu;
794 wapp = wApplicationOf(btn->icon->owner->main_window);
796 if (wapp->flags.hidden) {
797 wWorkspaceChange(btn->icon->core->screen_ptr, wapp->last_workspace);
798 wUnhideApplication(wapp, False, False);
799 } else {
800 wHideApplication(wapp);
804 static void unhideHereCallback(WMenu *menu, WMenuEntry *entry)
806 WApplication *wapp;
807 WAppIcon *btn = (WAppIcon *) entry->clientdata;
809 /* Parameter not used, but tell the compiler that it is ok */
810 (void) menu;
812 wapp = wApplicationOf(btn->icon->owner->main_window);
814 wUnhideApplication(wapp, False, True);
817 /* Name is only used when type == WM_DRAWER and when restoring a specific
818 * drawer, with a specific name. When creating a drawer, leave name to NULL
819 * and mainIconCreate will find the first unused unique name */
820 static WAppIcon *mainIconCreate(WScreen *scr, int type, const char *name)
822 WAppIcon *btn;
823 int x_pos;
825 switch(type) {
826 case WM_CLIP:
827 if (w_global.clip.icon)
828 return w_global.clip.icon;
830 btn = wAppIconCreateForDock(scr, NULL, "Logo", "WMClip", TILE_CLIP);
831 btn->icon->core->descriptor.handle_expose = clipIconExpose;
832 x_pos = 0;
833 break;
834 case WM_DOCK:
835 default: /* to avoid a warning about btn and x_pos, basically */
836 btn = wAppIconCreateForDock(scr, NULL, "Logo", "WMDock", TILE_NORMAL);
837 if (wPreferences.flags.clip_merged_in_dock)
838 btn->icon->core->descriptor.handle_expose = clipIconExpose;
839 x_pos = scr->scr_width - ICON_SIZE - DOCK_EXTRA_SPACE;
840 break;
841 case WM_DRAWER:
842 if (name == NULL)
843 name = findUniqueName(scr, "Drawer");
844 btn = wAppIconCreateForDock(scr, NULL, name, "WMDrawer", TILE_DRAWER);
845 btn->icon->core->descriptor.handle_expose = drawerIconExpose;
846 x_pos = 0;
849 btn->xindex = 0;
850 btn->yindex = 0;
852 btn->icon->core->descriptor.handle_mousedown = iconMouseDown;
853 btn->icon->core->descriptor.handle_enternotify = clipEnterNotify;
854 btn->icon->core->descriptor.handle_leavenotify = clipLeaveNotify;
855 btn->icon->core->descriptor.parent_type = WCLASS_DOCK_ICON;
856 btn->icon->core->descriptor.parent = btn;
857 XMapWindow(dpy, btn->icon->core->window);
858 btn->x_pos = x_pos;
859 btn->y_pos = 0;
860 btn->docked = 1;
861 if (type == WM_CLIP ||
862 (type == WM_DOCK && wPreferences.flags.clip_merged_in_dock))
863 w_global.clip.icon = btn;
865 return btn;
868 static void switchWSCommand(WMenu *menu, WMenuEntry *entry)
870 WAppIcon *btn, *icon = (WAppIcon *) entry->clientdata;
871 WDock *src, *dest;
872 WMArray *selectedIcons;
873 int x, y;
875 /* Parameter not used, but tell the compiler that it is ok */
876 (void) menu;
878 if (entry->order == w_global.workspace.current)
879 return;
880 src = icon->dock;
881 dest = w_global.workspace.array[entry->order]->clip;
883 selectedIcons = getSelected(src);
885 if (WMGetArrayItemCount(selectedIcons)) {
886 WMArrayIterator iter;
888 WM_ITERATE_ARRAY(selectedIcons, btn, iter) {
889 if (wDockFindFreeSlot(dest, &x, &y)) {
890 wDockMoveIconBetweenDocks(src, dest, btn, x, y);
891 XUnmapWindow(dpy, btn->icon->core->window);
894 } else if (icon != w_global.clip.icon) {
895 if (wDockFindFreeSlot(dest, &x, &y)) {
896 wDockMoveIconBetweenDocks(src, dest, icon, x, y);
897 XUnmapWindow(dpy, icon->icon->core->window);
900 WMFreeArray(selectedIcons);
903 static void launchDockedApplication(WAppIcon *btn, Bool withSelection)
905 WScreen *scr = btn->icon->core->screen_ptr;
907 if (!btn->launching &&
908 ((!withSelection && btn->command != NULL) || (withSelection && btn->paste_command != NULL))) {
909 if (!btn->forced_dock) {
910 btn->relaunching = btn->running;
911 btn->running = 1;
913 if (btn->wm_instance || btn->wm_class) {
914 WWindowAttributes attr;
915 memset(&attr, 0, sizeof(WWindowAttributes));
916 wDefaultFillAttributes(btn->wm_instance, btn->wm_class, &attr, NULL, True);
918 if (!attr.no_appicon && !btn->buggy_app)
919 btn->launching = 1;
920 else
921 btn->running = 0;
923 btn->drop_launch = 0;
924 btn->paste_launch = withSelection;
925 scr->last_dock = btn->dock;
926 btn->pid = execCommand(btn, (withSelection ? btn->paste_command : btn->command), NULL);
927 if (btn->pid > 0) {
928 if (btn->buggy_app) {
929 /* give feedback that the app was launched */
930 btn->launching = 1;
931 dockIconPaint(btn);
932 btn->launching = 0;
933 WMAddTimerHandler(200, (WMCallback *) dockIconPaint, btn);
934 } else {
935 dockIconPaint(btn);
937 } else {
938 wwarning(_("could not launch application %s"), btn->command);
939 btn->launching = 0;
940 if (!btn->relaunching)
941 btn->running = 0;
946 static void updateWorkspaceMenu(WMenu *menu, WAppIcon *icon)
948 char title[MAX_WORKSPACENAME_WIDTH + 1];
949 int i;
951 if (!menu || !icon)
952 return;
954 for (i = 0; i < w_global.workspace.count; i++) {
955 if (i < menu->entry_no) {
956 if (strcmp(menu->entries[i]->text, w_global.workspace.array[i]->name) != 0) {
957 wfree(menu->entries[i]->text);
958 strcpy(title, w_global.workspace.array[i]->name);
959 menu->entries[i]->text = wstrdup(title);
960 menu->flags.realized = 0;
962 menu->entries[i]->clientdata = (void *)icon;
963 } else {
964 strcpy(title, w_global.workspace.array[i]->name);
966 wMenuAddCallback(menu, title, switchWSCommand, (void *)icon);
968 menu->flags.realized = 0;
971 if (i == w_global.workspace.current)
972 wMenuSetEnabled(menu, i, False);
973 else
974 wMenuSetEnabled(menu, i, True);
977 if (!menu->flags.realized)
978 wMenuRealize(menu);
981 static WMenu *makeWorkspaceMenu(WScreen *scr)
983 WMenu *menu;
985 menu = wMenuCreate(scr, NULL, False);
986 if (!menu)
987 wwarning(_("could not create workspace submenu for Clip menu"));
989 wMenuAddCallback(menu, "", switchWSCommand, (void *)w_global.clip.icon);
991 menu->flags.realized = 0;
992 wMenuRealize(menu);
994 return menu;
997 static void updateClipOptionsMenu(WMenu *menu, WDock *dock)
999 WMenuEntry *entry;
1000 int index = 0;
1002 if (!menu || !dock)
1003 return;
1005 /* keep on top */
1006 entry = menu->entries[index];
1007 entry->flags.indicator_on = !dock->lowered;
1008 entry->clientdata = dock;
1009 wMenuSetEnabled(menu, index, dock->type == WM_CLIP);
1011 /* collapsed */
1012 entry = menu->entries[++index];
1013 entry->flags.indicator_on = dock->collapsed;
1014 entry->clientdata = dock;
1016 /* auto-collapse */
1017 entry = menu->entries[++index];
1018 entry->flags.indicator_on = dock->auto_collapse;
1019 entry->clientdata = dock;
1021 /* auto-raise/lower */
1022 entry = menu->entries[++index];
1023 entry->flags.indicator_on = dock->auto_raise_lower;
1024 entry->clientdata = dock;
1025 wMenuSetEnabled(menu, index, dock->lowered && (dock->type == WM_CLIP));
1027 /* attract icons */
1028 entry = menu->entries[++index];
1029 entry->flags.indicator_on = dock->attract_icons;
1030 entry->clientdata = dock;
1032 menu->flags.realized = 0;
1033 wMenuRealize(menu);
1037 static WMenu *makeClipOptionsMenu(WScreen *scr)
1039 WMenu *menu;
1040 WMenuEntry *entry;
1042 menu = wMenuCreate(scr, NULL, False);
1043 if (!menu) {
1044 wwarning(_("could not create options submenu for Clip menu"));
1045 return NULL;
1048 entry = wMenuAddCallback(menu, _("Keep on Top"), toggleLoweredCallback, NULL);
1049 entry->flags.indicator = 1;
1050 entry->flags.indicator_on = 1;
1051 entry->flags.indicator_type = MI_CHECK;
1053 entry = wMenuAddCallback(menu, _("Collapsed"), toggleCollapsedCallback, NULL);
1054 entry->flags.indicator = 1;
1055 entry->flags.indicator_on = 1;
1056 entry->flags.indicator_type = MI_CHECK;
1058 entry = wMenuAddCallback(menu, _("Autocollapse"), toggleAutoCollapseCallback, NULL);
1059 entry->flags.indicator = 1;
1060 entry->flags.indicator_on = 1;
1061 entry->flags.indicator_type = MI_CHECK;
1063 entry = wMenuAddCallback(menu, _("Autoraise"), toggleAutoRaiseLowerCallback, NULL);
1064 entry->flags.indicator = 1;
1065 entry->flags.indicator_on = 1;
1066 entry->flags.indicator_type = MI_CHECK;
1068 entry = wMenuAddCallback(menu, _("Autoattract Icons"), toggleAutoAttractCallback, NULL);
1069 entry->flags.indicator = 1;
1070 entry->flags.indicator_on = 1;
1071 entry->flags.indicator_type = MI_CHECK;
1073 menu->flags.realized = 0;
1074 wMenuRealize(menu);
1076 return menu;
1080 static void setDockPositionNormalCallback(WMenu *menu, WMenuEntry *entry)
1082 WDock *dock = (WDock *) entry->clientdata;
1083 WDrawerChain *dc;
1085 /* Parameter not used, but tell the compiler that it is ok */
1086 (void) menu;
1088 if (entry->flags.indicator_on) // already set, nothing to do
1089 return;
1090 // Do we come from auto raise lower or keep on top?
1091 if (dock->auto_raise_lower)
1093 dock->auto_raise_lower = 0;
1094 // Only for aesthetic purposes, can be removed when Autoraise status is no longer exposed in drawer option menu
1095 for (dc = dock->screen_ptr->drawers; dc != NULL; dc = dc->next) {
1096 dc->adrawer->auto_raise_lower = 0;
1099 else
1101 // Will take care of setting lowered = 0 in drawers
1102 toggleLowered(dock);
1104 entry->flags.indicator_on = 1;
1107 static void setDockPositionAutoRaiseLowerCallback(WMenu *menu, WMenuEntry *entry)
1109 WDock *dock = (WDock *) entry->clientdata;
1110 WDrawerChain *dc;
1112 /* Parameter not used, but tell the compiler that it is ok */
1113 (void) menu;
1115 if (entry->flags.indicator_on) // already set, nothing to do
1116 return;
1117 // Do we come from normal or keep on top?
1118 if (!dock->lowered)
1120 toggleLowered(dock);
1122 dock->auto_raise_lower = 1;
1123 // Only for aesthetic purposes, can be removed when Autoraise status is no longer exposed in drawer option menu
1124 for (dc = dock->screen_ptr->drawers; dc != NULL; dc = dc->next) {
1125 dc->adrawer->auto_raise_lower = 1;
1127 entry->flags.indicator_on = 1;
1130 static void setDockPositionKeepOnTopCallback(WMenu *menu, WMenuEntry *entry)
1132 WDock *dock = (WDock *) entry->clientdata;
1133 WDrawerChain *dc;
1135 /* Parameter not used, but tell the compiler that it is ok */
1136 (void) menu;
1138 if (entry->flags.indicator_on) // already set, nothing to do
1139 return;
1140 dock->auto_raise_lower = 0;
1141 // Only for aesthetic purposes, can be removed when Autoraise status is no longer exposed in drawer option menu
1142 for (dc = dock->screen_ptr->drawers; dc != NULL; dc = dc->next) {
1143 dc->adrawer->auto_raise_lower = 0;
1145 toggleLowered(dock);
1146 entry->flags.indicator_on = 1;
1149 static void updateDockPositionMenu(WMenu *menu, WDock *dock)
1151 WMenuEntry *entry;
1152 int index = 0;
1154 assert(menu);
1155 assert(dock);
1157 /* Normal level */
1158 entry = menu->entries[index++];
1159 entry->flags.indicator_on = (dock->lowered && !dock->auto_raise_lower);
1160 entry->clientdata = dock;
1162 /* Auto-raise/lower */
1163 entry = menu->entries[index++];
1164 entry->flags.indicator_on = dock->auto_raise_lower;
1165 entry->clientdata = dock;
1167 /* Keep on top */
1168 entry = menu->entries[index++];
1169 entry->flags.indicator_on = !dock->lowered;
1170 entry->clientdata = dock;
1173 static WMenu *makeDockPositionMenu(WScreen *scr)
1175 /* When calling this, the dock is being created, so scr->dock is still not set
1176 * Therefore the callbacks' clientdata and the indicators can't be set,
1177 * they will be updated when the dock menu is opened. */
1178 WMenu *menu;
1179 WMenuEntry *entry;
1181 menu = wMenuCreate(scr, NULL, False);
1182 if (!menu) {
1183 wwarning(_("could not create options submenu for dock position menu"));
1184 return NULL;
1187 entry = wMenuAddCallback(menu, _("Normal"), setDockPositionNormalCallback, NULL);
1188 entry->flags.indicator = 1;
1189 entry->flags.indicator_type = MI_DIAMOND;
1191 entry = wMenuAddCallback(menu, _("Auto raise & lower"), setDockPositionAutoRaiseLowerCallback, NULL);
1192 entry->flags.indicator = 1;
1193 entry->flags.indicator_type = MI_DIAMOND;
1195 entry = wMenuAddCallback(menu, _("Keep on Top"), setDockPositionKeepOnTopCallback, NULL);
1196 entry->flags.indicator = 1;
1197 entry->flags.indicator_type = MI_DIAMOND;
1199 menu->flags.realized = 0;
1200 wMenuRealize(menu);
1202 return menu;
1206 static WMenu *dockMenuCreate(WScreen *scr, int type)
1208 WMenu *menu;
1209 WMenuEntry *entry;
1211 if (type == WM_CLIP && w_global.clip.menu)
1212 return w_global.clip.menu;
1214 if (type == WM_DRAWER && w_global.dock.drawer_menu)
1215 return w_global.dock.drawer_menu;
1217 menu = wMenuCreate(scr, NULL, False);
1218 if (type == WM_DOCK) {
1219 entry = wMenuAddCallback(menu, _("Dock position"), NULL, NULL);
1220 if (w_global.dock.pos_menu == NULL)
1221 w_global.dock.pos_menu = makeDockPositionMenu(scr);
1222 wMenuEntrySetCascade(menu, entry, w_global.dock.pos_menu);
1224 if (!wPreferences.flags.nodrawer) {
1225 entry = wMenuAddCallback(menu, _("Add a drawer"), addADrawerCallback, NULL);
1227 } else {
1228 if (type == WM_CLIP)
1229 entry = wMenuAddCallback(menu, _("Clip Options"), NULL, NULL);
1230 else /* if (type == WM_DRAWER) */
1231 entry = wMenuAddCallback(menu, _("Drawer options"), NULL, NULL);
1233 if (w_global.clip.opt_menu == NULL)
1234 w_global.clip.opt_menu = makeClipOptionsMenu(scr);
1236 wMenuEntrySetCascade(menu, entry, w_global.clip.opt_menu);
1238 /* The same menu is used for the dock and its appicons. If the menu
1239 * entry text is different between the two contexts, or if it can
1240 * change depending on some state, free the duplicated string (from
1241 * wMenuInsertCallback) and use gettext's string */
1242 if (type == WM_CLIP) {
1243 entry = wMenuAddCallback(menu, _("Rename Workspace"), renameCallback, NULL);
1244 wfree(entry->text);
1245 entry->text = _("Rename Workspace"); /* can be: (Toggle) Omnipresent */
1248 entry = wMenuAddCallback(menu, _("Selected"), selectCallback, NULL);
1249 entry->flags.indicator = 1;
1250 entry->flags.indicator_on = 1;
1251 entry->flags.indicator_type = MI_CHECK;
1253 entry = wMenuAddCallback(menu, _("Select All Icons"), selectIconsCallback, NULL);
1254 wfree(entry->text);
1255 entry->text = _("Select All Icons"); /* can be: Unselect all icons */
1257 entry = wMenuAddCallback(menu, _("Keep Icon"), keepIconsCallback, NULL);
1258 wfree(entry->text);
1259 entry->text = _("Keep Icon"); /* can be: Keep Icons */
1261 if (type == WM_CLIP) {
1262 entry = wMenuAddCallback(menu, _("Move Icon To"), NULL, NULL);
1263 wfree(entry->text);
1264 entry->text = _("Move Icon To"); /* can be: Move Icons to */
1265 w_global.clip.submenu = makeWorkspaceMenu(scr);
1266 if (w_global.clip.submenu)
1267 wMenuEntrySetCascade(menu, entry, w_global.clip.submenu);
1270 entry = wMenuAddCallback(menu, _("Remove Icon"), removeIconsCallback, NULL);
1271 wfree(entry->text);
1272 entry->text = _("Remove Icon"); /* can be: Remove Icons */
1274 wMenuAddCallback(menu, _("Attract Icons"), colectIconsCallback, NULL);
1277 wMenuAddCallback(menu, _("Launch"), launchCallback, NULL);
1279 wMenuAddCallback(menu, _("Unhide Here"), unhideHereCallback, NULL);
1281 entry = wMenuAddCallback(menu, _("Hide"), hideCallback, NULL);
1282 wfree(entry->text);
1283 entry->text = _("Hide"); /* can be: Unhide */
1285 wMenuAddCallback(menu, _("Settings..."), settingsCallback, NULL);
1287 entry = wMenuAddCallback(menu, _("Kill"), killCallback, NULL);
1288 wfree(entry->text);
1289 entry->text = _("Kill"); /* can be: Remove drawer */
1291 if (type == WM_CLIP)
1292 w_global.clip.menu = menu;
1294 if (type == WM_DRAWER)
1295 w_global.dock.drawer_menu = menu;
1297 return menu;
1300 WDock *wDockCreate(WScreen *scr, int type, const char *name)
1302 WDock *dock;
1303 WAppIcon *btn;
1305 make_keys();
1307 dock = wmalloc(sizeof(WDock));
1309 switch (type) {
1310 case WM_CLIP:
1311 dock->max_icons = DOCK_MAX_ICONS;
1312 break;
1313 case WM_DRAWER:
1314 dock->max_icons = scr->scr_width / wPreferences.icon_size;
1315 break;
1316 case WM_DOCK:
1317 default:
1318 dock->max_icons = scr->scr_height / wPreferences.icon_size;
1321 dock->icon_array = wmalloc(sizeof(WAppIcon *) * dock->max_icons);
1323 btn = mainIconCreate(scr, type, name);
1325 btn->dock = dock;
1327 dock->x_pos = btn->x_pos;
1328 dock->y_pos = btn->y_pos;
1329 dock->screen_ptr = scr;
1330 dock->type = type;
1331 dock->icon_count = 1;
1332 if (type == WM_DRAWER)
1333 dock->on_right_side = scr->dock->on_right_side;
1334 else
1335 dock->on_right_side = 1;
1336 dock->collapsed = 0;
1337 dock->auto_collapse = 0;
1338 dock->auto_collapse_magic = NULL;
1339 dock->auto_raise_lower = 0;
1340 dock->auto_lower_magic = NULL;
1341 dock->auto_raise_magic = NULL;
1342 dock->attract_icons = 0;
1343 dock->lowered = 1;
1344 dock->icon_array[0] = btn;
1345 wRaiseFrame(btn->icon->core);
1346 XMoveWindow(dpy, btn->icon->core->window, btn->x_pos, btn->y_pos);
1348 /* create dock menu */
1349 dock->menu = dockMenuCreate(scr, type);
1351 if (type == WM_DRAWER) {
1352 drawerAppendToChain(scr, dock);
1353 dock->auto_collapse = 1;
1356 return dock;
1359 void wDockDestroy(WDock *dock)
1361 int i;
1362 WAppIcon *aicon;
1364 for (i = (dock->type == WM_CLIP) ? 1 : 0; i < dock->max_icons; i++) {
1365 aicon = dock->icon_array[i];
1366 if (aicon) {
1367 int keepit = aicon->running && wApplicationOf(aicon->main_window);
1368 wDockDetach(dock, aicon);
1369 if (keepit) {
1370 /* XXX: can: aicon->icon == NULL ? */
1371 PlaceIcon(dock->screen_ptr, &aicon->x_pos, &aicon->y_pos,
1372 wGetHeadForWindow(aicon->icon->owner));
1373 XMoveWindow(dpy, aicon->icon->core->window, aicon->x_pos, aicon->y_pos);
1374 if (!dock->mapped || dock->collapsed)
1375 XMapWindow(dpy, aicon->icon->core->window);
1379 if (wPreferences.auto_arrange_icons)
1380 wArrangeIcons(dock->screen_ptr, True);
1381 wfree(dock->icon_array);
1382 if (dock->menu && dock->type != WM_CLIP)
1383 wMenuDestroy(dock->menu, True);
1384 if (dock->screen_ptr->last_dock == dock)
1385 dock->screen_ptr->last_dock = NULL;
1386 wfree(dock);
1389 void wClipIconPaint(void)
1391 WAppIcon *aicon = w_global.clip.icon;
1392 WScreen *scr = aicon->icon->core->screen_ptr;
1393 WWorkspace *workspace = w_global.workspace.array[w_global.workspace.current];
1394 WMColor *color;
1395 Window win = aicon->icon->core->window;
1396 int length, nlength;
1397 char *ws_name, ws_number[10];
1398 int ty, tx;
1400 wIconPaint(aicon->icon);
1402 length = strlen(workspace->name);
1403 ws_name = wmalloc(length + 1);
1404 snprintf(ws_name, length + 1, "%s", workspace->name);
1405 snprintf(ws_number, sizeof(ws_number), "%i", w_global.workspace.current + 1);
1406 nlength = strlen(ws_number);
1408 if (wPreferences.flags.noclip || !workspace->clip->collapsed)
1409 color = scr->clip_title_color[CLIP_NORMAL];
1410 else
1411 color = scr->clip_title_color[CLIP_COLLAPSED];
1413 ty = ICON_SIZE - WMFontHeight(scr->clip_title_font) - 3;
1415 tx = CLIP_BUTTON_SIZE * ICON_SIZE / 64;
1417 if(wPreferences.show_clip_title)
1418 WMDrawString(scr->wmscreen, win, color, scr->clip_title_font, tx, ty, ws_name, length);
1420 tx = (ICON_SIZE / 2 - WMWidthOfString(scr->clip_title_font, ws_number, nlength)) / 2;
1422 WMDrawString(scr->wmscreen, win, color, scr->clip_title_font, tx, 2, ws_number, nlength);
1424 wfree(ws_name);
1426 if (aicon->launching)
1427 XFillRectangle(dpy, aicon->icon->core->window, scr->stipple_gc,
1428 0, 0, wPreferences.icon_size, wPreferences.icon_size);
1430 paintClipButtons(aicon, aicon->dock->lclip_button_pushed, aicon->dock->rclip_button_pushed);
1433 static void clipIconExpose(WObjDescriptor *desc, XEvent *event)
1435 /* Parameter not used, but tell the compiler that it is ok */
1436 (void) desc;
1437 (void) event;
1439 wClipIconPaint();
1442 static void dockIconPaint(WAppIcon *btn)
1444 if (btn == w_global.clip.icon) {
1445 wClipIconPaint();
1446 } else if (wIsADrawer(btn)) {
1447 wDrawerIconPaint(btn);
1448 } else {
1449 wAppIconPaint(btn);
1450 save_appicon(btn, True);
1454 static WMPropList *make_icon_state(WAppIcon *btn)
1456 WMPropList *node = NULL;
1457 WMPropList *command, *autolaunch, *lock, *name, *forced;
1458 WMPropList *position, *buggy, *omnipresent;
1459 char *tmp;
1460 char buffer[64];
1462 if (btn) {
1463 if (!btn->command)
1464 command = WMCreatePLString("-");
1465 else
1466 command = WMCreatePLString(btn->command);
1468 autolaunch = btn->auto_launch ? dYes : dNo;
1470 lock = btn->lock ? dYes : dNo;
1472 tmp = EscapeWM_CLASS(btn->wm_instance, btn->wm_class);
1474 name = WMCreatePLString(tmp);
1476 wfree(tmp);
1478 forced = btn->forced_dock ? dYes : dNo;
1480 buggy = btn->buggy_app ? dYes : dNo;
1482 if (!wPreferences.flags.clip_merged_in_dock && btn == w_global.clip.icon)
1483 snprintf(buffer, sizeof(buffer), "%i,%i", btn->x_pos, btn->y_pos);
1484 else
1485 snprintf(buffer, sizeof(buffer), "%hi,%hi", btn->xindex, btn->yindex);
1486 position = WMCreatePLString(buffer);
1488 node = WMCreatePLDictionary(dCommand, command,
1489 dName, name,
1490 dAutoLaunch, autolaunch,
1491 dLock, lock,
1492 dForced, forced, dBuggyApplication, buggy, dPosition, position, NULL);
1493 WMReleasePropList(command);
1494 WMReleasePropList(name);
1495 WMReleasePropList(position);
1497 omnipresent = btn->omnipresent ? dYes : dNo;
1498 if (btn->dock != btn->icon->core->screen_ptr->dock && (btn->xindex != 0 || btn->yindex != 0))
1499 WMPutInPLDictionary(node, dOmnipresent, omnipresent);
1501 #ifdef XDND /* was OFFIX */
1502 if (btn->dnd_command) {
1503 command = WMCreatePLString(btn->dnd_command);
1504 WMPutInPLDictionary(node, dDropCommand, command);
1505 WMReleasePropList(command);
1507 #endif /* XDND */
1509 if (btn->paste_command) {
1510 command = WMCreatePLString(btn->paste_command);
1511 WMPutInPLDictionary(node, dPasteCommand, command);
1512 WMReleasePropList(command);
1516 return node;
1519 static WMPropList *dockSaveState(WDock *dock)
1521 int i;
1522 WMPropList *icon_info;
1523 WMPropList *list = NULL, *dock_state = NULL;
1524 WMPropList *value, *key;
1525 char buffer[256];
1527 list = WMCreatePLArray(NULL);
1529 for (i = (dock->type == WM_DOCK ? 0 : 1); i < dock->max_icons; i++) {
1530 WAppIcon *btn = dock->icon_array[i];
1532 if (!btn || btn->attracted)
1533 continue;
1535 if ((icon_info = make_icon_state(dock->icon_array[i]))) {
1536 WMAddToPLArray(list, icon_info);
1537 WMReleasePropList(icon_info);
1541 dock_state = WMCreatePLDictionary(dApplications, list, NULL);
1543 if (dock->type == WM_DOCK) {
1544 snprintf(buffer, sizeof(buffer), "Applications%i", dock->screen_ptr->scr_height);
1545 key = WMCreatePLString(buffer);
1546 WMPutInPLDictionary(dock_state, key, list);
1547 WMReleasePropList(key);
1549 snprintf(buffer, sizeof(buffer), "%i,%i", (dock->on_right_side ? -ICON_SIZE : 0), dock->y_pos);
1550 value = WMCreatePLString(buffer);
1551 WMPutInPLDictionary(dock_state, dPosition, value);
1552 WMReleasePropList(value);
1554 WMReleasePropList(list);
1556 if (dock->type == WM_CLIP || dock->type == WM_DRAWER) {
1557 value = (dock->collapsed ? dYes : dNo);
1558 WMPutInPLDictionary(dock_state, dCollapsed, value);
1560 value = (dock->auto_collapse ? dYes : dNo);
1561 WMPutInPLDictionary(dock_state, dAutoCollapse, value);
1563 value = (dock->attract_icons ? dYes : dNo);
1564 WMPutInPLDictionary(dock_state, dAutoAttractIcons, value);
1567 if (dock->type == WM_DOCK || dock->type == WM_CLIP) {
1568 value = (dock->lowered ? dYes : dNo);
1569 WMPutInPLDictionary(dock_state, dLowered, value);
1571 value = (dock->auto_raise_lower ? dYes : dNo);
1572 WMPutInPLDictionary(dock_state, dAutoRaiseLower, value);
1575 return dock_state;
1578 void wDockSaveState(WScreen *scr, WMPropList *old_state)
1580 WMPropList *dock_state;
1581 WMPropList *keys;
1583 dock_state = dockSaveState(scr->dock);
1586 * Copy saved states of docks with different sizes.
1588 if (old_state) {
1589 int i;
1590 WMPropList *tmp;
1592 keys = WMGetPLDictionaryKeys(old_state);
1593 for (i = 0; i < WMGetPropListItemCount(keys); i++) {
1594 tmp = WMGetFromPLArray(keys, i);
1596 if (strncasecmp(WMGetFromPLString(tmp), "applications", 12) == 0
1597 && !WMGetFromPLDictionary(dock_state, tmp)) {
1599 WMPutInPLDictionary(dock_state, tmp, WMGetFromPLDictionary(old_state, tmp));
1602 WMReleasePropList(keys);
1605 WMPutInPLDictionary(w_global.session_state, dDock, dock_state);
1606 WMReleasePropList(dock_state);
1609 void wClipSaveState(void)
1611 WMPropList *clip_state;
1613 clip_state = make_icon_state(w_global.clip.icon);
1615 WMPutInPLDictionary(w_global.session_state, dClip, clip_state);
1616 WMReleasePropList(clip_state);
1619 WMPropList *wClipSaveWorkspaceState(int workspace)
1621 return dockSaveState(w_global.workspace.array[workspace]->clip);
1624 static Bool getBooleanDockValue(WMPropList *value, WMPropList *key)
1626 if (value) {
1627 if (WMIsPLString(value)) {
1628 if (strcasecmp(WMGetFromPLString(value), "YES") == 0)
1629 return True;
1630 } else {
1631 wwarning(_("bad value in docked icon state info %s"), WMGetFromPLString(key));
1634 return False;
1637 static WAppIcon *restore_icon_state(WScreen *scr, WMPropList *info, int type, int index)
1639 WAppIcon *aicon;
1640 WMPropList *cmd, *value;
1641 char *wclass, *winstance, *command;
1643 cmd = WMGetFromPLDictionary(info, dCommand);
1644 if (!cmd || !WMIsPLString(cmd))
1645 return NULL;
1647 /* parse window name */
1648 value = WMGetFromPLDictionary(info, dName);
1649 if (!value)
1650 return NULL;
1652 ParseWindowName(value, &winstance, &wclass, "dock");
1654 if (!winstance && !wclass)
1655 return NULL;
1657 /* get commands */
1658 if (cmd)
1659 command = wstrdup(WMGetFromPLString(cmd));
1660 else
1661 command = NULL;
1663 if (!command || strcmp(command, "-") == 0) {
1664 if (command)
1665 wfree(command);
1666 if (wclass)
1667 wfree(wclass);
1668 if (winstance)
1669 wfree(winstance);
1671 return NULL;
1674 aicon = wAppIconCreateForDock(scr, command, winstance, wclass, TILE_NORMAL);
1675 if (wclass)
1676 wfree(wclass);
1677 if (winstance)
1678 wfree(winstance);
1679 if (command)
1680 wfree(command);
1682 aicon->icon->core->descriptor.handle_mousedown = iconMouseDown;
1683 aicon->icon->core->descriptor.handle_enternotify = clipEnterNotify;
1684 aicon->icon->core->descriptor.handle_leavenotify = clipLeaveNotify;
1685 aicon->icon->core->descriptor.parent_type = WCLASS_DOCK_ICON;
1686 aicon->icon->core->descriptor.parent = aicon;
1688 #ifdef XDND /* was OFFIX */
1689 cmd = WMGetFromPLDictionary(info, dDropCommand);
1690 if (cmd)
1691 aicon->dnd_command = wstrdup(WMGetFromPLString(cmd));
1692 #endif
1694 cmd = WMGetFromPLDictionary(info, dPasteCommand);
1695 if (cmd)
1696 aicon->paste_command = wstrdup(WMGetFromPLString(cmd));
1698 /* check auto launch */
1699 value = WMGetFromPLDictionary(info, dAutoLaunch);
1701 aicon->auto_launch = getBooleanDockValue(value, dAutoLaunch);
1703 /* check lock */
1704 value = WMGetFromPLDictionary(info, dLock);
1706 aicon->lock = getBooleanDockValue(value, dLock);
1708 /* check if it wasn't normally docked */
1709 value = WMGetFromPLDictionary(info, dForced);
1711 aicon->forced_dock = getBooleanDockValue(value, dForced);
1713 /* check if we can rely on the stuff in the app */
1714 value = WMGetFromPLDictionary(info, dBuggyApplication);
1716 aicon->buggy_app = getBooleanDockValue(value, dBuggyApplication);
1718 /* get position in the dock */
1719 value = WMGetFromPLDictionary(info, dPosition);
1720 if (value && WMIsPLString(value)) {
1721 if (sscanf(WMGetFromPLString(value), "%hi,%hi", &aicon->xindex, &aicon->yindex) != 2)
1722 wwarning(_("bad value in docked icon state info %s"), WMGetFromPLString(dPosition));
1724 /* check position sanity */
1725 /* *Very* incomplete section! */
1726 if (type == WM_DOCK) {
1727 aicon->xindex = 0;
1729 } else {
1730 aicon->yindex = index;
1731 aicon->xindex = 0;
1734 /* check if icon is omnipresent */
1735 value = WMGetFromPLDictionary(info, dOmnipresent);
1737 aicon->omnipresent = getBooleanDockValue(value, dOmnipresent);
1739 aicon->running = 0;
1740 aicon->docked = 1;
1742 return aicon;
1745 #define COMPLAIN(key) wwarning(_("bad value in dock state info:%s"), key)
1747 WAppIcon *wClipRestoreState(WScreen *scr, WMPropList *clip_state)
1749 WAppIcon *icon;
1750 WMPropList *value;
1752 icon = mainIconCreate(scr, WM_CLIP, NULL);
1754 if (!clip_state)
1755 return icon;
1757 WMRetainPropList(clip_state);
1759 /* restore position */
1761 value = WMGetFromPLDictionary(clip_state, dPosition);
1763 if (value) {
1764 if (!WMIsPLString(value)) {
1765 COMPLAIN("Position");
1766 } else {
1767 if (sscanf(WMGetFromPLString(value), "%i,%i", &icon->x_pos, &icon->y_pos) != 2)
1768 COMPLAIN("Position");
1770 /* check position sanity */
1771 if (!onScreen(scr, icon->x_pos, icon->y_pos))
1772 wScreenKeepInside(scr, &icon->x_pos, &icon->y_pos, ICON_SIZE, ICON_SIZE);
1775 #ifdef XDND /* was OFFIX */
1776 value = WMGetFromPLDictionary(clip_state, dDropCommand);
1777 if (value && WMIsPLString(value))
1778 icon->dnd_command = wstrdup(WMGetFromPLString(value));
1779 #endif
1781 value = WMGetFromPLDictionary(clip_state, dPasteCommand);
1782 if (value && WMIsPLString(value))
1783 icon->paste_command = wstrdup(WMGetFromPLString(value));
1785 WMReleasePropList(clip_state);
1787 return icon;
1790 WDock *wDockRestoreState(WScreen *scr, WMPropList *dock_state, int type)
1792 WDock *dock;
1793 WMPropList *apps;
1794 WMPropList *value;
1795 WAppIcon *aicon, *old_top;
1796 int count, i;
1798 dock = wDockCreate(scr, type, NULL);
1800 if (!dock_state)
1801 return dock;
1803 WMRetainPropList(dock_state);
1805 /* restore position */
1806 value = WMGetFromPLDictionary(dock_state, dPosition);
1807 if (value) {
1808 if (!WMIsPLString(value)) {
1809 COMPLAIN("Position");
1810 } else {
1811 if (sscanf(WMGetFromPLString(value), "%i,%i", &dock->x_pos, &dock->y_pos) != 2)
1812 COMPLAIN("Position");
1814 /* check position sanity */
1815 if (!onScreen(scr, dock->x_pos, dock->y_pos)) {
1816 int x = dock->x_pos;
1817 wScreenKeepInside(scr, &x, &dock->y_pos, ICON_SIZE, ICON_SIZE);
1820 /* Is this needed any more? */
1821 if (type == WM_CLIP) {
1822 if (dock->x_pos < 0) {
1823 dock->x_pos = 0;
1824 } else if (dock->x_pos > scr->scr_width - ICON_SIZE) {
1825 dock->x_pos = scr->scr_width - ICON_SIZE;
1827 } else {
1828 if (dock->x_pos >= 0) {
1829 dock->x_pos = DOCK_EXTRA_SPACE;
1830 dock->on_right_side = 0;
1831 } else {
1832 dock->x_pos = scr->scr_width - DOCK_EXTRA_SPACE - ICON_SIZE;
1833 dock->on_right_side = 1;
1839 /* restore lowered/raised state */
1840 dock->lowered = 0;
1842 value = WMGetFromPLDictionary(dock_state, dLowered);
1843 if (value) {
1844 if (!WMIsPLString(value)) {
1845 COMPLAIN("Lowered");
1846 } else {
1847 if (strcasecmp(WMGetFromPLString(value), "YES") == 0)
1848 dock->lowered = 1;
1852 /* restore collapsed state */
1853 dock->collapsed = 0;
1855 value = WMGetFromPLDictionary(dock_state, dCollapsed);
1856 if (value) {
1857 if (!WMIsPLString(value)) {
1858 COMPLAIN("Collapsed");
1859 } else {
1860 if (strcasecmp(WMGetFromPLString(value), "YES") == 0)
1861 dock->collapsed = 1;
1865 /* restore auto-collapsed state */
1866 value = WMGetFromPLDictionary(dock_state, dAutoCollapse);
1867 if (value) {
1868 if (!WMIsPLString(value)) {
1869 COMPLAIN("AutoCollapse");
1870 } else {
1871 if (strcasecmp(WMGetFromPLString(value), "YES") == 0) {
1872 dock->auto_collapse = 1;
1873 dock->collapsed = 1;
1878 /* restore auto-raise/lower state */
1879 value = WMGetFromPLDictionary(dock_state, dAutoRaiseLower);
1880 if (value) {
1881 if (!WMIsPLString(value)) {
1882 COMPLAIN("AutoRaiseLower");
1883 } else {
1884 if (strcasecmp(WMGetFromPLString(value), "YES") == 0)
1885 dock->auto_raise_lower = 1;
1889 /* restore attract icons state */
1890 dock->attract_icons = 0;
1892 value = WMGetFromPLDictionary(dock_state, dAutoAttractIcons);
1893 if (value) {
1894 if (!WMIsPLString(value)) {
1895 COMPLAIN("AutoAttractIcons");
1896 } else {
1897 if (strcasecmp(WMGetFromPLString(value), "YES") == 0)
1898 dock->attract_icons = 1;
1902 /* application list */
1905 WMPropList *tmp;
1906 char buffer[64];
1909 * When saving, it saves the dock state in
1910 * Applications and Applicationsnnn
1912 * When loading, it will first try Applicationsnnn.
1913 * If it does not exist, use Applications as default.
1916 snprintf(buffer, sizeof(buffer), "Applications%i", scr->scr_height);
1918 tmp = WMCreatePLString(buffer);
1919 apps = WMGetFromPLDictionary(dock_state, tmp);
1920 WMReleasePropList(tmp);
1922 if (!apps)
1923 apps = WMGetFromPLDictionary(dock_state, dApplications);
1926 if (!apps)
1927 goto finish;
1929 count = WMGetPropListItemCount(apps);
1930 if (count == 0)
1931 goto finish;
1933 old_top = dock->icon_array[0];
1935 /* dock->icon_count is set to 1 when dock is created.
1936 * Since Clip is already restored, we want to keep it so for clip,
1937 * but for dock we may change the default top tile, so we set it to 0.
1939 if (type == WM_DOCK)
1940 dock->icon_count = 0;
1942 for (i = 0; i < count; i++) {
1943 if (dock->icon_count >= dock->max_icons) {
1944 wwarning(_("there are too many icons stored in dock. Ignoring what doesn't fit"));
1945 break;
1948 value = WMGetFromPLArray(apps, i);
1949 aicon = restore_icon_state(scr, value, type, dock->icon_count);
1951 dock->icon_array[dock->icon_count] = aicon;
1953 if (aicon) {
1954 aicon->dock = dock;
1955 aicon->x_pos = dock->x_pos + (aicon->xindex * ICON_SIZE);
1956 aicon->y_pos = dock->y_pos + (aicon->yindex * ICON_SIZE);
1958 if (dock->lowered)
1959 ChangeStackingLevel(aicon->icon->core, WMNormalLevel);
1960 else
1961 ChangeStackingLevel(aicon->icon->core, WMDockLevel);
1963 wCoreConfigure(aicon->icon->core, aicon->x_pos, aicon->y_pos, 0, 0);
1964 if (!dock->collapsed)
1965 XMapWindow(dpy, aicon->icon->core->window);
1967 wRaiseFrame(aicon->icon->core);
1969 dock->icon_count++;
1970 } else if (dock->icon_count == 0 && type == WM_DOCK) {
1971 dock->icon_count++;
1975 /* if the first icon is not defined, use the default */
1976 if (dock->icon_array[0] == NULL) {
1977 /* update default icon */
1978 old_top->x_pos = dock->x_pos;
1979 old_top->y_pos = dock->y_pos;
1980 if (dock->lowered)
1981 ChangeStackingLevel(old_top->icon->core, WMNormalLevel);
1982 else
1983 ChangeStackingLevel(old_top->icon->core, WMDockLevel);
1985 dock->icon_array[0] = old_top;
1986 XMoveWindow(dpy, old_top->icon->core->window, dock->x_pos, dock->y_pos);
1987 /* we don't need to increment dock->icon_count here because it was
1988 * incremented in the loop above.
1990 } else if (old_top != dock->icon_array[0]) {
1991 if (old_top == w_global.clip.icon) /* TODO dande: understand the logic */
1992 w_global.clip.icon = dock->icon_array[0];
1994 wAppIconDestroy(old_top);
1997 finish:
1998 WMReleasePropList(dock_state);
2000 return dock;
2003 void wDockLaunchWithState(WAppIcon *btn, WSavedState *state)
2005 if (btn && btn->command && !btn->running && !btn->launching) {
2006 btn->drop_launch = 0;
2007 btn->paste_launch = 0;
2009 btn->pid = execCommand(btn, btn->command, state);
2011 if (btn->pid > 0) {
2012 if (!btn->forced_dock && !btn->buggy_app) {
2013 btn->launching = 1;
2014 dockIconPaint(btn);
2017 } else {
2018 wfree(state);
2022 void wDockDoAutoLaunch(WDock *dock, int workspace)
2024 WAppIcon *btn;
2025 WSavedState *state;
2026 int i;
2028 for (i = 0; i < dock->max_icons; i++) {
2029 btn = dock->icon_array[i];
2030 if (!btn || !btn->auto_launch)
2031 continue;
2033 state = wmalloc(sizeof(WSavedState));
2034 state->workspace = workspace;
2035 /* TODO: this is klugy and is very difficult to understand
2036 * what's going on. Try to clean up */
2037 wDockLaunchWithState(btn, state);
2041 #ifdef XDND /* was OFFIX */
2042 static WDock *findDock(WScreen *scr, XEvent *event, int *icon_pos)
2044 WDock *dock;
2045 int i;
2047 *icon_pos = -1;
2048 if ((dock = scr->dock) != NULL) {
2049 for (i = 0; i < dock->max_icons; i++) {
2050 if (dock->icon_array[i]
2051 && dock->icon_array[i]->icon->core->window == event->xclient.window) {
2052 *icon_pos = i;
2053 break;
2057 if (*icon_pos < 0 && (dock = w_global.workspace.array[w_global.workspace.current]->clip) != NULL) {
2058 for (i = 0; i < dock->max_icons; i++) {
2059 if (dock->icon_array[i]
2060 && dock->icon_array[i]->icon->core->window == event->xclient.window) {
2061 *icon_pos = i;
2062 break;
2066 if (*icon_pos >= 0)
2067 return dock;
2068 return NULL;
2071 int wDockReceiveDNDDrop(WScreen *scr, XEvent *event)
2073 WDock *dock;
2074 WAppIcon *btn;
2075 int icon_pos;
2077 dock = findDock(scr, event, &icon_pos);
2078 if (!dock)
2079 return False;
2082 * Return True if the drop was on an application icon window.
2083 * In this case, let the ClientMessage handler redirect the
2084 * message to the app.
2086 if (dock->icon_array[icon_pos]->icon->icon_win != None)
2087 return True;
2089 if (dock->icon_array[icon_pos]->dnd_command != NULL) {
2090 scr->flags.dnd_data_convertion_status = 0;
2092 btn = dock->icon_array[icon_pos];
2094 if (!btn->forced_dock) {
2095 btn->relaunching = btn->running;
2096 btn->running = 1;
2098 if (btn->wm_instance || btn->wm_class) {
2099 WWindowAttributes attr;
2100 memset(&attr, 0, sizeof(WWindowAttributes));
2101 wDefaultFillAttributes(btn->wm_instance, btn->wm_class, &attr, NULL, True);
2103 if (!attr.no_appicon)
2104 btn->launching = 1;
2105 else
2106 btn->running = 0;
2109 btn->paste_launch = 0;
2110 btn->drop_launch = 1;
2111 scr->last_dock = dock;
2112 btn->pid = execCommand(btn, btn->dnd_command, NULL);
2113 if (btn->pid > 0) {
2114 dockIconPaint(btn);
2115 } else {
2116 btn->launching = 0;
2117 if (!btn->relaunching)
2118 btn->running = 0;
2121 return False;
2123 #endif /* XDND */
2125 Bool wDockAttachIcon(WDock *dock, WAppIcon *icon, int x, int y, Bool update_icon)
2127 WWindow *wwin;
2128 Bool lupdate_icon = False;
2129 char *command = NULL;
2130 int index;
2132 icon->editing = 0;
2134 if (update_icon)
2135 lupdate_icon = True;
2137 if (icon->command == NULL) {
2138 /* If icon->owner exists, it means the application is running */
2139 if (icon->icon->owner) {
2140 wwin = icon->icon->owner;
2141 command = GetCommandForWindow(wwin->client_win);
2144 if (command) {
2145 icon->command = command;
2146 } else {
2147 /* icon->forced_dock = 1; */
2148 if (dock->type != WM_CLIP || !icon->attracted) {
2149 icon->editing = 1;
2150 if (wInputDialog(dock->screen_ptr, _("Dock Icon"),
2151 _("Type the command used to launch the application"), &command)) {
2152 if (command && (command[0] == 0 || (command[0] == '-' && command[1] == 0))) {
2153 wfree(command);
2154 command = NULL;
2156 icon->command = command;
2157 icon->editing = 0;
2158 } else {
2159 icon->editing = 0;
2160 if (command)
2161 wfree(command);
2162 /* If the target is the dock, reject the icon. If
2163 * the target is the clip, make it an attracted icon
2165 if (dock->type == WM_CLIP) {
2166 icon->attracted = 1;
2167 if (!icon->icon->shadowed) {
2168 icon->icon->shadowed = 1;
2169 lupdate_icon = True;
2171 } else {
2172 return False;
2179 for (index = 1; index < dock->max_icons; index++)
2180 if (dock->icon_array[index] == NULL)
2181 break;
2182 /* if (index == dock->max_icons)
2183 return; */
2185 assert(index < dock->max_icons);
2187 dock->icon_array[index] = icon;
2188 icon->yindex = y;
2189 icon->xindex = x;
2191 icon->omnipresent = 0;
2193 icon->x_pos = dock->x_pos + x * ICON_SIZE;
2194 icon->y_pos = dock->y_pos + y * ICON_SIZE;
2196 dock->icon_count++;
2198 icon->running = 1;
2199 icon->launching = 0;
2200 icon->docked = 1;
2201 icon->dock = dock;
2202 icon->icon->core->descriptor.handle_mousedown = iconMouseDown;
2203 icon->icon->core->descriptor.handle_enternotify = clipEnterNotify;
2204 icon->icon->core->descriptor.handle_leavenotify = clipLeaveNotify;
2205 icon->icon->core->descriptor.parent_type = WCLASS_DOCK_ICON;
2206 icon->icon->core->descriptor.parent = icon;
2208 MoveInStackListUnder(dock->icon_array[index - 1]->icon->core, icon->icon->core);
2209 wAppIconMove(icon, icon->x_pos, icon->y_pos);
2212 * Update icon pixmap, RImage doesn't change,
2213 * so call wIconUpdate is not needed
2215 if (lupdate_icon)
2216 update_icon_pixmap(icon->icon);
2218 /* Paint it */
2219 wAppIconPaint(icon);
2221 /* Save it */
2222 save_appicon(icon, True);
2224 if (wPreferences.auto_arrange_icons)
2225 wArrangeIcons(dock->screen_ptr, True);
2227 #ifdef XDND /* was OFFIX */
2228 if (icon->command && !icon->dnd_command) {
2229 int len = strlen(icon->command) + 8;
2230 icon->dnd_command = wmalloc(len);
2231 snprintf(icon->dnd_command, len, "%s %%d", icon->command);
2233 #endif
2235 if (icon->command && !icon->paste_command) {
2236 int len = strlen(icon->command) + 8;
2237 icon->paste_command = wmalloc(len);
2238 snprintf(icon->paste_command, len, "%s %%s", icon->command);
2241 return True;
2244 void wDockReattachIcon(WDock *dock, WAppIcon *icon, int x, int y)
2246 int index;
2248 for (index = 1; index < dock->max_icons; index++) {
2249 if (dock->icon_array[index] == icon)
2250 break;
2252 assert(index < dock->max_icons);
2254 icon->yindex = y;
2255 icon->xindex = x;
2257 icon->x_pos = dock->x_pos + x * ICON_SIZE;
2258 icon->y_pos = dock->y_pos + y * ICON_SIZE;
2261 Bool wDockMoveIconBetweenDocks(WDock *src, WDock *dest, WAppIcon *icon, int x, int y)
2263 WWindow *wwin;
2264 char *command = NULL;
2265 int index;
2266 Bool update_icon = False;
2268 if (src == dest)
2269 return True; /* No move needed, we're already there */
2271 if (dest == NULL)
2272 return False;
2275 * For the moment we can't do this if we move icons in Clip from one
2276 * workspace to other, because if we move two or more icons without
2277 * command, the dialog box will not be able to tell us to which of the
2278 * moved icons it applies. -Dan
2280 if ((dest->type == WM_DOCK /*|| dest->keep_attracted */ ) && icon->command == NULL) {
2281 /* If icon->owner exists, it means the application is running */
2282 if (icon->icon->owner) {
2283 wwin = icon->icon->owner;
2284 command = GetCommandForWindow(wwin->client_win);
2287 if (command) {
2288 icon->command = command;
2289 } else {
2290 icon->editing = 1;
2291 /* icon->forced_dock = 1; */
2292 if (wInputDialog(src->screen_ptr, _("Dock Icon"),
2293 _("Type the command used to launch the application"), &command)) {
2294 if (command && (command[0] == 0 || (command[0] == '-' && command[1] == 0))) {
2295 wfree(command);
2296 command = NULL;
2298 icon->command = command;
2299 } else {
2300 icon->editing = 0;
2301 if (command)
2302 wfree(command);
2303 return False;
2305 icon->editing = 0;
2309 if (dest->type == WM_DOCK || dest->type == WM_DRAWER)
2310 wClipMakeIconOmnipresent(icon, False);
2312 for (index = 1; index < src->max_icons; index++) {
2313 if (src->icon_array[index] == icon)
2314 break;
2316 assert(index < src->max_icons);
2318 src->icon_array[index] = NULL;
2319 src->icon_count--;
2321 for (index = 1; index < dest->max_icons; index++) {
2322 if (dest->icon_array[index] == NULL)
2323 break;
2326 assert(index < dest->max_icons);
2328 dest->icon_array[index] = icon;
2329 icon->dock = dest;
2331 /* deselect the icon */
2332 if (icon->icon->selected)
2333 wIconSelect(icon->icon);
2335 icon->icon->core->descriptor.handle_enternotify = clipEnterNotify;
2336 icon->icon->core->descriptor.handle_leavenotify = clipLeaveNotify;
2338 /* set it to be kept when moving to dock.
2339 * Unless the icon does not have a command set
2341 if (icon->command && (dest->type == WM_DOCK || dest->type == WM_DRAWER)) {
2342 icon->attracted = 0;
2343 if (icon->icon->shadowed) {
2344 icon->icon->shadowed = 0;
2345 update_icon = True;
2347 save_appicon(icon, True);
2350 if (src->auto_collapse || src->auto_raise_lower)
2351 clipLeave(src);
2353 icon->yindex = y;
2354 icon->xindex = x;
2356 icon->x_pos = dest->x_pos + x * ICON_SIZE;
2357 icon->y_pos = dest->y_pos + y * ICON_SIZE;
2359 dest->icon_count++;
2361 MoveInStackListUnder(dest->icon_array[index - 1]->icon->core, icon->icon->core);
2364 * Update icon pixmap, RImage doesn't change,
2365 * so call wIconUpdate is not needed
2367 if (update_icon)
2368 update_icon_pixmap(icon->icon);
2370 /* Paint it */
2371 wAppIconPaint(icon);
2373 return True;
2376 void wDockDetach(WDock *dock, WAppIcon *icon)
2378 int index;
2379 Bool update_icon = False;
2381 /* make the settings panel be closed */
2382 if (icon->panel)
2383 DestroyDockAppSettingsPanel(icon->panel);
2385 /* This must be called before icon->dock is set to NULL.
2386 * Don't move it. -Dan
2388 wClipMakeIconOmnipresent(icon, False);
2390 icon->docked = 0;
2391 icon->dock = NULL;
2392 icon->attracted = 0;
2393 icon->auto_launch = 0;
2394 if (icon->icon->shadowed) {
2395 icon->icon->shadowed = 0;
2396 update_icon = True;
2399 /* deselect the icon */
2400 if (icon->icon->selected)
2401 wIconSelect(icon->icon);
2403 if (icon->command) {
2404 wfree(icon->command);
2405 icon->command = NULL;
2407 #ifdef XDND /* was OFFIX */
2408 if (icon->dnd_command) {
2409 wfree(icon->dnd_command);
2410 icon->dnd_command = NULL;
2412 #endif
2413 if (icon->paste_command) {
2414 wfree(icon->paste_command);
2415 icon->paste_command = NULL;
2418 for (index = 1; index < dock->max_icons; index++)
2419 if (dock->icon_array[index] == icon)
2420 break;
2422 assert(index < dock->max_icons);
2423 dock->icon_array[index] = NULL;
2424 icon->yindex = -1;
2425 icon->xindex = -1;
2427 dock->icon_count--;
2429 /* if the dock is not attached to an application or
2430 * the application did not set the appropriate hints yet,
2431 * destroy the icon */
2432 if (!icon->running || !wApplicationOf(icon->main_window)) {
2433 wAppIconDestroy(icon);
2434 } else {
2435 icon->icon->core->descriptor.handle_mousedown = appIconMouseDown;
2436 icon->icon->core->descriptor.handle_enternotify = NULL;
2437 icon->icon->core->descriptor.handle_leavenotify = NULL;
2438 icon->icon->core->descriptor.parent_type = WCLASS_APPICON;
2439 icon->icon->core->descriptor.parent = icon;
2441 ChangeStackingLevel(icon->icon->core, NORMAL_ICON_LEVEL);
2444 * Update icon pixmap, RImage doesn't change,
2445 * so call wIconUpdate is not needed
2447 if (update_icon)
2448 update_icon_pixmap(icon->icon);
2450 /* Paint it */
2451 wAppIconPaint(icon);
2453 if (wPreferences.auto_arrange_icons)
2454 wArrangeIcons(dock->screen_ptr, True);
2456 if (dock->auto_collapse || dock->auto_raise_lower)
2457 clipLeave(dock);
2461 * returns the closest Dock slot index for the passed
2462 * coordinates.
2464 * Returns False if icon can't be docked.
2466 * Note: this function should NEVER alter ret_x or ret_y, unless it will
2467 * return True. -Dan
2469 /* Redocking == true means either icon->dock == dock (normal case)
2470 * or we are called from handleDockMove for a drawer */
2471 Bool wDockSnapIcon(WDock *dock, WAppIcon *icon, int req_x, int req_y, int *ret_x, int *ret_y, int redocking)
2473 WScreen *scr = dock->screen_ptr;
2474 int dx, dy;
2475 int ex_x, ex_y;
2476 int i, offset = ICON_SIZE / 2;
2477 WAppIcon *aicon = NULL;
2478 WAppIcon *nicon = NULL;
2480 if (wPreferences.flags.noupdates)
2481 return False;
2483 dx = dock->x_pos;
2484 dy = dock->y_pos;
2486 /* if the dock is full */
2487 if (!redocking && (dock->icon_count >= dock->max_icons))
2488 return False;
2490 /* exact position */
2491 if (req_y < dy)
2492 ex_y = (req_y - offset - dy) / ICON_SIZE;
2493 else
2494 ex_y = (req_y + offset - dy) / ICON_SIZE;
2496 if (req_x < dx)
2497 ex_x = (req_x - offset - dx) / ICON_SIZE;
2498 else
2499 ex_x = (req_x + offset - dx) / ICON_SIZE;
2501 /* check if the icon is outside the screen boundaries */
2502 if (!onScreen(scr, dx + ex_x * ICON_SIZE, dy + ex_y * ICON_SIZE))
2503 return False;
2505 switch (dock->type) {
2506 case WM_DOCK:
2507 /* We can return False right away if
2508 * - we do not come from this dock (which is a WM_DOCK),
2509 * - we are not right over it, and
2510 * - we are not the main tile of a drawer.
2511 * In the latter case, we are called from handleDockMove. */
2512 if (icon->dock != dock && ex_x != 0 &&
2513 !(icon->dock && icon->dock->type == WM_DRAWER && icon == icon->dock->icon_array[0]))
2514 return False;
2516 if (!redocking && ex_x != 0)
2517 return False;
2519 if (getDrawer(scr, ex_y)) /* Return false so that the drawer gets it. */
2520 return False;
2522 aicon = NULL;
2523 for (i = 0; i < dock->max_icons; i++) {
2524 nicon = dock->icon_array[i];
2525 if (nicon && nicon->yindex == ex_y) {
2526 aicon = nicon;
2527 break;
2531 if (redocking) {
2532 int sig, done, closest;
2534 /* Possible cases when redocking:
2536 * icon dragged out of range of any slot -> false
2537 * icon dragged on a drawer -> false (to open the drawer)
2538 * icon dragged to range of free slot
2539 * icon dragged to range of same slot
2540 * icon dragged to range of different icon
2542 if (abs(ex_x) > DOCK_DETTACH_THRESHOLD)
2543 return False;
2545 if (aicon == icon || !aicon) {
2546 *ret_x = 0;
2547 *ret_y = ex_y;
2548 return True;
2551 /* start looking at the upper slot or lower? */
2552 if (ex_y * ICON_SIZE < (req_y + offset - dy))
2553 sig = 1;
2554 else
2555 sig = -1;
2557 done = 0;
2558 /* look for closest free slot */
2559 for (i = 0; i < (DOCK_DETTACH_THRESHOLD + 1) * 2 && !done; i++) {
2560 int j;
2562 done = 1;
2563 closest = sig * (i / 2) + ex_y;
2564 /* check if this slot is fully on the screen and not used */
2565 if (onScreen(scr, dx, dy + closest * ICON_SIZE)) {
2566 for (j = 0; j < dock->max_icons; j++) {
2567 if (dock->icon_array[j]
2568 && dock->icon_array[j]->yindex == closest) {
2569 /* slot is used by someone else */
2570 if (dock->icon_array[j] != icon)
2571 done = 0;
2572 break;
2575 /* slot is used by a drawer */
2576 done = done && !getDrawer(scr, closest);
2578 else // !onScreen
2579 done = 0;
2580 sig = -sig;
2582 if (done &&
2583 ((ex_y >= closest && ex_y - closest < DOCK_DETTACH_THRESHOLD + 1)
2584 || (ex_y < closest && closest - ex_y <= DOCK_DETTACH_THRESHOLD + 1))) {
2585 *ret_x = 0;
2586 *ret_y = closest;
2587 return True;
2589 } else { /* !redocking */
2591 /* if slot is free and the icon is close enough, return it */
2592 if (!aicon && ex_x == 0) {
2593 *ret_x = 0;
2594 *ret_y = ex_y;
2595 return True;
2598 break;
2599 case WM_CLIP:
2601 int neighbours = 0;
2602 int start, stop, k;
2604 start = icon->omnipresent ? 0 : w_global.workspace.current;
2605 stop = icon->omnipresent ? w_global.workspace.count : start + 1;
2607 aicon = NULL;
2608 for (k = start; k < stop; k++) {
2609 WDock *tmp = w_global.workspace.array[k]->clip;
2610 if (!tmp)
2611 continue;
2612 for (i = 0; i < tmp->max_icons; i++) {
2613 nicon = tmp->icon_array[i];
2614 if (nicon && nicon->xindex == ex_x && nicon->yindex == ex_y) {
2615 aicon = nicon;
2616 break;
2619 if (aicon)
2620 break;
2622 for (k = start; k < stop; k++) {
2623 WDock *tmp = w_global.workspace.array[k]->clip;
2624 if (!tmp)
2625 continue;
2626 for (i = 0; i < tmp->max_icons; i++) {
2627 nicon = tmp->icon_array[i];
2628 if (nicon && nicon != icon && /* Icon can't be it's own neighbour */
2629 (abs(nicon->xindex - ex_x) <= CLIP_ATTACH_VICINITY &&
2630 abs(nicon->yindex - ex_y) <= CLIP_ATTACH_VICINITY)) {
2631 neighbours = 1;
2632 break;
2635 if (neighbours)
2636 break;
2639 if (neighbours && (aicon == NULL || (redocking && aicon == icon))) {
2640 *ret_x = ex_x;
2641 *ret_y = ex_y;
2642 return True;
2644 break;
2646 case WM_DRAWER:
2648 WAppIcon *aicons_to_shift[ dock->icon_count ];
2649 int index_of_hole, j;
2651 if (ex_y != 0 ||
2652 abs(ex_x) - dock->icon_count > DOCK_DETTACH_THRESHOLD ||
2653 (ex_x < 0 && !dock->on_right_side) ||
2654 (ex_x > 0 && dock->on_right_side)) {
2655 return False;
2658 if (ex_x == 0)
2659 ex_x = (dock->on_right_side ? -1 : 1);
2661 /* "Reduce" ex_x but keep its sign */
2662 if (redocking) {
2663 if (abs(ex_x) > dock->icon_count - 1) /* minus 1: do not take icon_array[0] into account */
2664 ex_x = ex_x * (dock->icon_count - 1) / abs(ex_x); /* don't use *= ! */
2665 } else {
2666 if (abs(ex_x) > dock->icon_count)
2667 ex_x = ex_x * dock->icon_count / abs(ex_x);
2669 index_of_hole = indexOfHole(dock, icon, redocking);
2671 /* Find the appicons between where icon was (index_of_hole) and where
2672 * it wants to be (ex_x) and slide them. */
2673 j = 0;
2674 for (i = 1; i < dock->max_icons; i++) {
2675 if ((aicon = dock->icon_array[ i ]) && aicon != icon &&
2676 ((ex_x <= aicon->xindex && aicon->xindex < index_of_hole) ||
2677 (index_of_hole < aicon->xindex && aicon->xindex <= ex_x)))
2678 aicons_to_shift[ j++ ] = aicon;
2680 assert(j == abs(ex_x - index_of_hole));
2682 wSlideAppicons(aicons_to_shift, j, (index_of_hole < ex_x));
2684 *ret_x = ex_x;
2685 *ret_y = ex_y;
2686 return True;
2689 return False;
2692 static int onScreen(WScreen *scr, int x, int y)
2694 WMRect rect;
2695 int flags;
2697 rect.pos.x = x;
2698 rect.pos.y = y;
2699 rect.size.width = rect.size.height = ICON_SIZE;
2701 wGetRectPlacementInfo(scr, rect, &flags);
2703 return !(flags & (XFLAG_DEAD | XFLAG_PARTIAL));
2707 * returns true if it can find a free slot in the dock,
2708 * in which case it changes x_pos and y_pos accordingly.
2709 * Else returns false.
2711 Bool wDockFindFreeSlot(WDock *dock, int *x_pos, int *y_pos)
2713 WScreen *scr = dock->screen_ptr;
2714 WAppIcon *btn;
2715 WAppIconChain *chain;
2716 unsigned char *slot_map;
2717 int mwidth;
2718 int r;
2719 int x, y;
2720 int i, done = False;
2721 int corner;
2722 int ex = scr->scr_width, ey = scr->scr_height;
2723 int extra_count = 0;
2725 if (dock->type == WM_DRAWER) {
2726 if (dock->icon_count >= dock->max_icons) { /* drawer is full */
2727 return False;
2729 *x_pos = dock->icon_count * (dock->on_right_side ? -1 : 1);
2730 *y_pos = 0;
2731 return True;
2734 if (dock->type == WM_CLIP && dock != w_global.workspace.array[w_global.workspace.current]->clip)
2735 extra_count = scr->global_icon_count;
2737 /* if the dock is full */
2738 if (dock->icon_count + extra_count >= dock->max_icons)
2739 return False;
2741 if (!wPreferences.flags.nodock && scr->dock && scr->dock->on_right_side) {
2742 ex -= ICON_SIZE + DOCK_EXTRA_SPACE;
2745 if (ex < dock->x_pos)
2746 ex = dock->x_pos;
2747 #define C_NONE 0
2748 #define C_NW 1
2749 #define C_NE 2
2750 #define C_SW 3
2751 #define C_SE 4
2753 /* check if clip is in a corner */
2754 if (dock->type == WM_CLIP) {
2755 if (dock->x_pos < 1 && dock->y_pos < 1)
2756 corner = C_NE;
2757 else if (dock->x_pos < 1 && dock->y_pos >= (ey - ICON_SIZE))
2758 corner = C_SE;
2759 else if (dock->x_pos >= (ex - ICON_SIZE) && dock->y_pos >= (ey - ICON_SIZE))
2760 corner = C_SW;
2761 else if (dock->x_pos >= (ex - ICON_SIZE) && dock->y_pos < 1)
2762 corner = C_NW;
2763 else
2764 corner = C_NONE;
2765 } else {
2766 corner = C_NONE;
2769 /* If the clip is in the corner, use only slots that are in the border
2770 * of the screen */
2771 if (corner != C_NONE) {
2772 char *hmap, *vmap;
2773 int hcount, vcount;
2775 hcount = WMIN(dock->max_icons, scr->scr_width / ICON_SIZE);
2776 vcount = WMIN(dock->max_icons, scr->scr_height / ICON_SIZE);
2777 hmap = wmalloc(hcount + 1);
2778 vmap = wmalloc(vcount + 1);
2780 /* mark used positions */
2781 switch (corner) {
2782 case C_NE:
2783 for (i = 0; i < dock->max_icons; i++) {
2784 btn = dock->icon_array[i];
2785 if (!btn)
2786 continue;
2788 if (btn->xindex == 0 && btn->yindex > 0 && btn->yindex < vcount)
2789 vmap[btn->yindex] = 1;
2790 else if (btn->yindex == 0 && btn->xindex > 0 && btn->xindex < hcount)
2791 hmap[btn->xindex] = 1;
2793 for (chain = scr->global_icons; chain != NULL; chain = chain->next) {
2794 btn = chain->aicon;
2795 if (btn->xindex == 0 && btn->yindex > 0 && btn->yindex < vcount)
2796 vmap[btn->yindex] = 1;
2797 else if (btn->yindex == 0 && btn->xindex > 0 && btn->xindex < hcount)
2798 hmap[btn->xindex] = 1;
2800 break;
2801 case C_NW:
2802 for (i = 0; i < dock->max_icons; i++) {
2803 btn = dock->icon_array[i];
2804 if (!btn)
2805 continue;
2807 if (btn->xindex == 0 && btn->yindex > 0 && btn->yindex < vcount)
2808 vmap[btn->yindex] = 1;
2809 else if (btn->yindex == 0 && btn->xindex < 0 && btn->xindex > -hcount)
2810 hmap[-btn->xindex] = 1;
2812 for (chain = scr->global_icons; chain != NULL; chain = chain->next) {
2813 btn = chain->aicon;
2814 if (btn->xindex == 0 && btn->yindex > 0 && btn->yindex < vcount)
2815 vmap[btn->yindex] = 1;
2816 else if (btn->yindex == 0 && btn->xindex < 0 && btn->xindex > -hcount)
2817 hmap[-btn->xindex] = 1;
2819 break;
2820 case C_SE:
2821 for (i = 0; i < dock->max_icons; i++) {
2822 btn = dock->icon_array[i];
2823 if (!btn)
2824 continue;
2826 if (btn->xindex == 0 && btn->yindex < 0 && btn->yindex > -vcount)
2827 vmap[-btn->yindex] = 1;
2828 else if (btn->yindex == 0 && btn->xindex > 0 && btn->xindex < hcount)
2829 hmap[btn->xindex] = 1;
2831 for (chain = scr->global_icons; chain != NULL; chain = chain->next) {
2832 btn = chain->aicon;
2833 if (btn->xindex == 0 && btn->yindex < 0 && btn->yindex > -vcount)
2834 vmap[-btn->yindex] = 1;
2835 else if (btn->yindex == 0 && btn->xindex > 0 && btn->xindex < hcount)
2836 hmap[btn->xindex] = 1;
2838 break;
2839 case C_SW:
2840 default:
2841 for (i = 0; i < dock->max_icons; i++) {
2842 btn = dock->icon_array[i];
2843 if (!btn)
2844 continue;
2846 if (btn->xindex == 0 && btn->yindex < 0 && btn->yindex > -vcount)
2847 vmap[-btn->yindex] = 1;
2848 else if (btn->yindex == 0 && btn->xindex < 0 && btn->xindex > -hcount)
2849 hmap[-btn->xindex] = 1;
2851 for (chain = scr->global_icons; chain != NULL; chain = chain->next) {
2852 btn = chain->aicon;
2853 if (btn->xindex == 0 && btn->yindex < 0 && btn->yindex > -vcount)
2854 vmap[-btn->yindex] = 1;
2855 else if (btn->yindex == 0 && btn->xindex < 0 && btn->xindex > -hcount)
2856 hmap[-btn->xindex] = 1;
2859 x = 0;
2860 y = 0;
2861 done = 0;
2862 /* search a vacant slot */
2863 for (i = 1; i < WMAX(vcount, hcount); i++) {
2864 if (i < vcount && vmap[i] == 0) {
2865 /* found a slot */
2866 x = 0;
2867 y = i;
2868 done = 1;
2869 break;
2870 } else if (i < hcount && hmap[i] == 0) {
2871 /* found a slot */
2872 x = i;
2873 y = 0;
2874 done = 1;
2875 break;
2878 wfree(vmap);
2879 wfree(hmap);
2880 /* If found a slot, translate and return */
2881 if (done) {
2882 if (corner == C_NW || corner == C_NE)
2883 *y_pos = y;
2884 else
2885 *y_pos = -y;
2887 if (corner == C_NE || corner == C_SE)
2888 *x_pos = x;
2889 else
2890 *x_pos = -x;
2892 return True;
2894 /* else, try to find a slot somewhere else */
2897 /* a map of mwidth x mwidth would be enough if we allowed icons to be
2898 * placed outside of screen */
2899 mwidth = (int)ceil(sqrt(dock->max_icons));
2901 /* In the worst case (the clip is in the corner of the screen),
2902 * the amount of icons that fit in the clip is smaller.
2903 * Double the map to get a safe value.
2905 mwidth += mwidth;
2907 r = (mwidth - 1) / 2;
2909 slot_map = wmalloc(mwidth * mwidth);
2911 #define XY2OFS(x,y) (WMAX(abs(x),abs(y)) > r) ? 0 : (((y)+r)*(mwidth)+(x)+r)
2913 /* mark used slots in the map. If the slot falls outside the map
2914 * (for example, when all icons are placed in line), ignore them. */
2915 for (i = 0; i < dock->max_icons; i++) {
2916 btn = dock->icon_array[i];
2917 if (btn)
2918 slot_map[XY2OFS(btn->xindex, btn->yindex)] = 1;
2921 for (chain = scr->global_icons; chain != NULL; chain = chain->next)
2922 slot_map[XY2OFS(chain->aicon->xindex, chain->aicon->yindex)] = 1;
2924 /* Find closest slot from the center that is free by scanning the
2925 * map from the center to outward in circular passes.
2926 * This will not result in a neat layout, but will be optimal
2927 * in the sense that there will not be holes left.
2929 done = 0;
2930 for (i = 1; i <= r && !done; i++) {
2931 int tx, ty;
2933 /* top and bottom parts of the ring */
2934 for (x = -i; x <= i && !done; x++) {
2935 tx = dock->x_pos + x * ICON_SIZE;
2936 y = -i;
2937 ty = dock->y_pos + y * ICON_SIZE;
2938 if (slot_map[XY2OFS(x, y)] == 0 && onScreen(scr, tx, ty)) {
2939 *x_pos = x;
2940 *y_pos = y;
2941 done = 1;
2942 break;
2944 y = i;
2945 ty = dock->y_pos + y * ICON_SIZE;
2946 if (slot_map[XY2OFS(x, y)] == 0 && onScreen(scr, tx, ty)) {
2947 *x_pos = x;
2948 *y_pos = y;
2949 done = 1;
2950 break;
2953 /* left and right parts of the ring */
2954 for (y = -i + 1; y <= i - 1; y++) {
2955 ty = dock->y_pos + y * ICON_SIZE;
2956 x = -i;
2957 tx = dock->x_pos + x * ICON_SIZE;
2958 if (slot_map[XY2OFS(x, y)] == 0 && onScreen(scr, tx, ty)) {
2959 *x_pos = x;
2960 *y_pos = y;
2961 done = 1;
2962 break;
2964 x = i;
2965 tx = dock->x_pos + x * ICON_SIZE;
2966 if (slot_map[XY2OFS(x, y)] == 0 && onScreen(scr, tx, ty)) {
2967 *x_pos = x;
2968 *y_pos = y;
2969 done = 1;
2970 break;
2974 wfree(slot_map);
2975 #undef XY2OFS
2976 return done;
2979 static void moveDock(WDock *dock, int new_x, int new_y)
2981 WAppIcon *btn;
2982 WDrawerChain *dc;
2983 int i;
2985 if (dock->type == WM_DOCK) {
2986 for (dc = dock->screen_ptr->drawers; dc != NULL; dc = dc->next)
2987 moveDock(dc->adrawer, new_x, dc->adrawer->y_pos - dock->y_pos + new_y);
2990 dock->x_pos = new_x;
2991 dock->y_pos = new_y;
2992 for (i = 0; i < dock->max_icons; i++) {
2993 btn = dock->icon_array[i];
2994 if (btn) {
2995 btn->x_pos = new_x + btn->xindex * ICON_SIZE;
2996 btn->y_pos = new_y + btn->yindex * ICON_SIZE;
2997 XMoveWindow(dpy, btn->icon->core->window, btn->x_pos, btn->y_pos);
3002 static void swapDock(WDock *dock)
3004 WScreen *scr = dock->screen_ptr;
3005 WAppIcon *btn;
3006 int x, i;
3008 if (dock->on_right_side)
3009 x = dock->x_pos = scr->scr_width - ICON_SIZE - DOCK_EXTRA_SPACE;
3010 else
3011 x = dock->x_pos = DOCK_EXTRA_SPACE;
3013 swapDrawers(scr, x);
3015 for (i = 0; i < dock->max_icons; i++) {
3016 btn = dock->icon_array[i];
3017 if (btn) {
3018 btn->x_pos = x;
3019 XMoveWindow(dpy, btn->icon->core->window, btn->x_pos, btn->y_pos);
3023 wScreenUpdateUsableArea(scr);
3026 static pid_t execCommand(WAppIcon *btn, const char *command, WSavedState *state)
3028 WScreen *scr = btn->icon->core->screen_ptr;
3029 pid_t pid;
3030 char **argv;
3031 int argc;
3032 char *cmdline;
3034 cmdline = ExpandOptions(scr, command);
3036 if (scr->flags.dnd_data_convertion_status || !cmdline) {
3037 if (cmdline)
3038 wfree(cmdline);
3039 if (state)
3040 wfree(state);
3041 return 0;
3044 wtokensplit(cmdline, &argv, &argc);
3046 if (!argc) {
3047 if (cmdline)
3048 wfree(cmdline);
3049 if (state)
3050 wfree(state);
3051 return 0;
3054 if ((pid = fork()) == 0) {
3055 char **args;
3056 int i;
3058 SetupEnvironment(scr);
3060 #ifdef HAVE_SETSID
3061 setsid();
3062 #endif
3064 args = malloc(sizeof(char *) * (argc + 1));
3065 if (!args)
3066 exit(111);
3068 for (i = 0; i < argc; i++)
3069 args[i] = argv[i];
3071 args[argc] = NULL;
3072 execvp(argv[0], args);
3073 exit(111);
3075 wtokenfree(argv, argc);
3077 if (pid > 0) {
3078 if (!state) {
3079 state = wmalloc(sizeof(WSavedState));
3080 state->hidden = -1;
3081 state->miniaturized = -1;
3082 state->shaded = -1;
3083 if (btn->dock == scr->dock || btn->dock->type == WM_DRAWER || btn->omnipresent)
3084 state->workspace = -1;
3085 else
3086 state->workspace = w_global.workspace.current;
3088 wWindowAddSavedState(btn->wm_instance, btn->wm_class, cmdline, pid, state);
3089 wAddDeathHandler(pid, (WDeathHandler *) trackDeadProcess, btn->dock);
3090 } else if (state) {
3091 wfree(state);
3093 wfree(cmdline);
3094 return pid;
3097 void wDockHideIcons(WDock *dock)
3099 int i;
3101 if (dock == NULL)
3102 return;
3104 for (i = 1; i < dock->max_icons; i++) {
3105 if (dock->icon_array[i])
3106 XUnmapWindow(dpy, dock->icon_array[i]->icon->core->window);
3108 dock->mapped = 0;
3110 dockIconPaint(dock->icon_array[0]);
3113 void wDockShowIcons(WDock *dock)
3115 int i;
3116 WAppIcon *btn;
3118 if (dock == NULL)
3119 return;
3121 btn = dock->icon_array[0];
3122 moveDock(dock, btn->x_pos, btn->y_pos);
3124 /* Deleting any change in stacking level, this function is now only about
3125 mapping icons */
3127 if (!dock->collapsed) {
3128 for (i = 1; i < dock->max_icons; i++) {
3129 if (dock->icon_array[i])
3130 XMapWindow(dpy, dock->icon_array[i]->icon->core->window);
3133 dock->mapped = 1;
3135 dockIconPaint(btn);
3138 void wDockLower(WDock *dock)
3140 int i;
3141 WDrawerChain *dc;
3143 if (dock->type == WM_DOCK) {
3144 for (dc = dock->screen_ptr->drawers; dc != NULL; dc = dc->next)
3145 wDockLower(dc->adrawer);
3147 for (i = 0; i < dock->max_icons; i++) {
3148 if (dock->icon_array[i])
3149 wLowerFrame(dock->icon_array[i]->icon->core);
3153 void wDockRaise(WDock *dock)
3155 int i;
3156 WDrawerChain *dc;
3158 for (i = dock->max_icons - 1; i >= 0; i--) {
3159 if (dock->icon_array[i])
3160 wRaiseFrame(dock->icon_array[i]->icon->core);
3162 if (dock->type == WM_DOCK) {
3163 for (dc = dock->screen_ptr->drawers; dc != NULL; dc = dc->next)
3164 wDockRaise(dc->adrawer);
3168 void wDockRaiseLower(WDock *dock)
3170 if (!dock->icon_array[0]->icon->core->stacking->above
3171 || (dock->icon_array[0]->icon->core->stacking->window_level
3172 != dock->icon_array[0]->icon->core->stacking->above->stacking->window_level))
3173 wDockLower(dock);
3174 else
3175 wDockRaise(dock);
3178 void wDockFinishLaunch(WAppIcon *icon)
3180 icon->launching = 0;
3181 icon->relaunching = 0;
3182 dockIconPaint(icon);
3185 WAppIcon *wDockFindIconForWindow(WDock *dock, Window window)
3187 WAppIcon *icon;
3188 int i;
3190 for (i = 0; i < dock->max_icons; i++) {
3191 icon = dock->icon_array[i];
3192 if (icon && icon->main_window == window)
3193 return icon;
3195 return NULL;
3198 void wDockTrackWindowLaunch(WDock *dock, Window window)
3200 WAppIcon *icon;
3201 char *wm_class, *wm_instance;
3202 int i;
3203 Bool firstPass = True;
3204 Bool found = False;
3205 char *command = NULL;
3207 if (!PropGetWMClass(window, &wm_class, &wm_instance)) {
3208 free(wm_class);
3209 free(wm_instance);
3210 return;
3213 command = GetCommandForWindow(window);
3214 retry:
3215 for (i = 0; i < dock->max_icons; i++) {
3216 icon = dock->icon_array[i];
3217 if (!icon)
3218 continue;
3220 /* app is already attached to icon */
3221 if (icon->main_window == window) {
3222 found = True;
3223 break;
3226 if ((icon->wm_instance || icon->wm_class)
3227 && (icon->launching || !icon->running)) {
3229 if (icon->wm_instance && wm_instance && strcmp(icon->wm_instance, wm_instance) != 0)
3230 continue;
3232 if (icon->wm_class && wm_class && strcmp(icon->wm_class, wm_class) != 0)
3233 continue;
3235 if (firstPass && command && strcmp(icon->command, command) != 0)
3236 continue;
3238 if (!icon->relaunching) {
3239 WApplication *wapp;
3241 /* Possibly an application that was docked with dockit,
3242 * but the user did not update WMState to indicate that
3243 * it was docked by force */
3244 wapp = wApplicationOf(window);
3245 if (!wapp) {
3246 icon->forced_dock = 1;
3247 icon->running = 0;
3249 if (!icon->forced_dock)
3250 icon->main_window = window;
3252 found = True;
3253 if (!wPreferences.no_animations && !icon->launching &&
3254 !dock->screen_ptr->flags.startup && !dock->collapsed) {
3255 WAppIcon *aicon;
3256 int x0, y0;
3258 icon->launching = 1;
3259 dockIconPaint(icon);
3261 aicon = wAppIconCreateForDock(dock->screen_ptr, NULL,
3262 wm_instance, wm_class, TILE_NORMAL);
3263 /* XXX: can: aicon->icon == NULL ? */
3264 PlaceIcon(dock->screen_ptr, &x0, &y0, wGetHeadForWindow(aicon->icon->owner));
3265 wAppIconMove(aicon, x0, y0);
3266 /* Should this always be lowered? -Dan */
3267 if (dock->lowered)
3268 wLowerFrame(aicon->icon->core);
3269 XMapWindow(dpy, aicon->icon->core->window);
3270 aicon->launching = 1;
3271 wAppIconPaint(aicon);
3272 SlideWindow(aicon->icon->core->window, x0, y0, icon->x_pos, icon->y_pos);
3273 XUnmapWindow(dpy, aicon->icon->core->window);
3274 wAppIconDestroy(aicon);
3276 wDockFinishLaunch(icon);
3277 break;
3281 if (firstPass && !found) {
3282 firstPass = False;
3283 goto retry;
3286 if (command)
3287 wfree(command);
3289 if (wm_class)
3290 free(wm_class);
3291 if (wm_instance)
3292 free(wm_instance);
3295 void wClipUpdateForWorkspaceChange(WScreen *scr, int workspace)
3297 if (!wPreferences.flags.noclip) {
3298 w_global.clip.icon->dock = w_global.workspace.array[workspace]->clip;
3299 if (w_global.workspace.current != workspace) {
3300 WDock *old_clip = w_global.workspace.array[w_global.workspace.current]->clip;
3301 WAppIconChain *chain = scr->global_icons;
3303 while (chain) {
3304 wDockMoveIconBetweenDocks(chain->aicon->dock,
3305 w_global.workspace.array[workspace]->clip,
3306 chain->aicon, chain->aicon->xindex, chain->aicon->yindex);
3307 if (w_global.workspace.array[workspace]->clip->collapsed)
3308 XUnmapWindow(dpy, chain->aicon->icon->core->window);
3309 chain = chain->next;
3312 wDockHideIcons(old_clip);
3313 if (old_clip->auto_raise_lower) {
3314 if (old_clip->auto_raise_magic) {
3315 WMDeleteTimerHandler(old_clip->auto_raise_magic);
3316 old_clip->auto_raise_magic = NULL;
3318 wDockLower(old_clip);
3320 if (old_clip->auto_collapse) {
3321 if (old_clip->auto_expand_magic) {
3322 WMDeleteTimerHandler(old_clip->auto_expand_magic);
3323 old_clip->auto_expand_magic = NULL;
3325 old_clip->collapsed = 1;
3327 wDockShowIcons(w_global.workspace.array[workspace]->clip);
3332 static void trackDeadProcess(pid_t pid, unsigned char status, WDock *dock)
3334 WAppIcon *icon;
3335 int i;
3337 for (i = 0; i < dock->max_icons; i++) {
3338 icon = dock->icon_array[i];
3339 if (!icon)
3340 continue;
3342 if (icon->launching && icon->pid == pid) {
3343 if (!icon->relaunching) {
3344 icon->running = 0;
3345 icon->main_window = None;
3347 wDockFinishLaunch(icon);
3348 icon->pid = 0;
3349 if (status == 111) {
3350 char msg[PATH_MAX];
3351 char *cmd;
3353 #ifdef XDND
3354 if (icon->drop_launch)
3355 cmd = icon->dnd_command;
3356 else
3357 #endif
3358 if (icon->paste_launch)
3359 cmd = icon->paste_command;
3360 else
3361 cmd = icon->command;
3363 snprintf(msg, sizeof(msg), _("Could not execute command \"%s\""), cmd);
3365 wMessageDialog(dock->screen_ptr, _("Error"), msg, _("OK"), NULL, NULL);
3367 break;
3372 /* This function is called when the dock switches state between
3373 * "normal" (including auto-raise/lower) and "keep on top". It is
3374 * therefore clearly distinct from wDockLower/Raise, which are called
3375 * each time a not-kept-on-top dock is lowered/raised. */
3376 static void toggleLowered(WDock *dock)
3378 WAppIcon *tmp;
3379 WDrawerChain *dc;
3380 int newlevel, i;
3382 if (!dock->lowered) {
3383 newlevel = WMNormalLevel;
3384 dock->lowered = 1;
3385 } else {
3386 newlevel = WMDockLevel;
3387 dock->lowered = 0;
3390 for (i = 0; i < dock->max_icons; i++) {
3391 tmp = dock->icon_array[i];
3392 if (!tmp)
3393 continue;
3395 ChangeStackingLevel(tmp->icon->core, newlevel);
3397 /* When the dock is no longer "on top", explicitly lower it as well.
3398 * It saves some CPU cycles (probably) to do it ourselves here
3399 * rather than calling wDockLower at the end of toggleLowered */
3400 if (dock->lowered)
3401 wLowerFrame(tmp->icon->core);
3404 if (dock->type == WM_DOCK) {
3405 for (dc = dock->screen_ptr->drawers; dc != NULL; dc = dc->next) {
3406 toggleLowered(dc->adrawer);
3408 wScreenUpdateUsableArea(dock->screen_ptr);
3412 static void toggleCollapsed(WDock *dock)
3414 if (dock->collapsed) {
3415 dock->collapsed = 0;
3416 wDockShowIcons(dock);
3417 } else {
3418 dock->collapsed = 1;
3419 wDockHideIcons(dock);
3423 static void openDockMenu(WDock *dock, WAppIcon *aicon, XEvent *event)
3425 WScreen *scr = dock->screen_ptr;
3426 WObjDescriptor *desc;
3427 WMenuEntry *entry;
3428 WApplication *wapp = NULL;
3429 int index = 0;
3430 int x_pos;
3431 int n_selected;
3432 int appIsRunning = aicon->running && aicon->icon && aicon->icon->owner;
3434 if (dock->type == WM_DOCK) {
3435 /* Dock position menu */
3436 updateDockPositionMenu(w_global.dock.pos_menu, dock);
3437 dock->menu->flags.realized = 0;
3438 if (!wPreferences.flags.nodrawer) {
3439 /* add a drawer */
3440 entry = dock->menu->entries[++index];
3441 entry->clientdata = aicon;
3442 wMenuSetEnabled(dock->menu, index, True);
3444 } else {
3445 /* clip/drawer options */
3446 if (w_global.clip.opt_menu)
3447 updateClipOptionsMenu(w_global.clip.opt_menu, dock);
3449 n_selected = numberOfSelectedIcons(dock);
3451 if (dock->type == WM_CLIP) {
3452 /* Rename Workspace */
3453 entry = dock->menu->entries[++index];
3454 if (aicon == w_global.clip.icon) {
3455 entry->callback = renameCallback;
3456 entry->clientdata = dock;
3457 entry->flags.indicator = 0;
3458 entry->text = _("Rename Workspace");
3459 } else {
3460 entry->callback = omnipresentCallback;
3461 entry->clientdata = aicon;
3462 if (n_selected > 0) {
3463 entry->flags.indicator = 0;
3464 entry->text = _("Toggle Omnipresent");
3465 } else {
3466 entry->flags.indicator = 1;
3467 entry->flags.indicator_on = aicon->omnipresent;
3468 entry->flags.indicator_type = MI_CHECK;
3469 entry->text = _("Omnipresent");
3474 /* select/unselect icon */
3475 entry = dock->menu->entries[++index];
3476 entry->clientdata = aicon;
3477 entry->flags.indicator_on = aicon->icon->selected;
3478 wMenuSetEnabled(dock->menu, index, aicon != w_global.clip.icon && !wIsADrawer(aicon));
3480 /* select/unselect all icons */
3481 entry = dock->menu->entries[++index];
3482 entry->clientdata = aicon;
3483 if (n_selected > 0)
3484 entry->text = _("Unselect All Icons");
3485 else
3486 entry->text = _("Select All Icons");
3488 wMenuSetEnabled(dock->menu, index, dock->icon_count > 1);
3490 /* keep icon(s) */
3491 entry = dock->menu->entries[++index];
3492 entry->clientdata = aicon;
3493 if (n_selected > 1)
3494 entry->text = _("Keep Icons");
3495 else
3496 entry->text = _("Keep Icon");
3498 wMenuSetEnabled(dock->menu, index, dock->icon_count > 1);
3500 if (dock->type == WM_CLIP) {
3501 /* this is the workspace submenu part */
3502 entry = dock->menu->entries[++index];
3503 if (n_selected > 1)
3504 entry->text = _("Move Icons To");
3505 else
3506 entry->text = _("Move Icon To");
3508 if (w_global.clip.submenu)
3509 updateWorkspaceMenu(w_global.clip.submenu, aicon);
3511 wMenuSetEnabled(dock->menu, index, !aicon->omnipresent);
3514 /* remove icon(s) */
3515 entry = dock->menu->entries[++index];
3516 entry->clientdata = aicon;
3517 if (n_selected > 1)
3518 entry->text = _("Remove Icons");
3519 else
3520 entry->text = _("Remove Icon");
3522 wMenuSetEnabled(dock->menu, index, dock->icon_count > 1);
3524 /* attract icon(s) */
3525 entry = dock->menu->entries[++index];
3526 entry->clientdata = aicon;
3528 dock->menu->flags.realized = 0;
3529 wMenuRealize(dock->menu);
3532 if (aicon->icon->owner)
3533 wapp = wApplicationOf(aicon->icon->owner->main_window);
3534 else
3535 wapp = NULL;
3537 /* launch */
3538 entry = dock->menu->entries[++index];
3539 entry->clientdata = aicon;
3540 wMenuSetEnabled(dock->menu, index, aicon->command != NULL);
3542 /* unhide here */
3543 entry = dock->menu->entries[++index];
3544 entry->clientdata = aicon;
3545 if (wapp && wapp->flags.hidden)
3546 entry->text = _("Unhide Here");
3547 else
3548 entry->text = _("Bring Here");
3550 wMenuSetEnabled(dock->menu, index, appIsRunning);
3552 /* hide */
3553 entry = dock->menu->entries[++index];
3554 entry->clientdata = aicon;
3555 if (wapp && wapp->flags.hidden)
3556 entry->text = _("Unhide");
3557 else
3558 entry->text = _("Hide");
3560 wMenuSetEnabled(dock->menu, index, appIsRunning);
3562 /* settings */
3563 entry = dock->menu->entries[++index];
3564 entry->clientdata = aicon;
3565 wMenuSetEnabled(dock->menu, index, !aicon->editing && !wPreferences.flags.noupdates);
3567 /* kill or remove drawer */
3568 entry = dock->menu->entries[++index];
3569 entry->clientdata = aicon;
3570 if (wIsADrawer(aicon)) {
3571 entry->callback = removeDrawerCallback;
3572 entry->text = _("Remove drawer");
3573 wMenuSetEnabled(dock->menu, index, True);
3574 } else {
3575 entry->callback = killCallback;
3576 entry->text = _("Kill");
3577 wMenuSetEnabled(dock->menu, index, appIsRunning);
3580 if (!dock->menu->flags.realized)
3581 wMenuRealize(dock->menu);
3583 if (dock->type == WM_CLIP || dock->type == WM_DRAWER) {
3584 /*x_pos = event->xbutton.x_root+2; */
3585 x_pos = event->xbutton.x_root - dock->menu->frame->core->width / 2 - 1;
3586 if (x_pos < 0) {
3587 x_pos = 0;
3588 } else if (x_pos + dock->menu->frame->core->width > scr->scr_width - 2) {
3589 x_pos = scr->scr_width - dock->menu->frame->core->width - 4;
3591 } else {
3592 x_pos = dock->on_right_side ? scr->scr_width - dock->menu->frame->core->width - 3 : 0;
3595 wMenuMapAt(dock->menu, x_pos, event->xbutton.y_root + 2, False);
3597 /* allow drag select */
3598 event->xany.send_event = True;
3599 desc = &dock->menu->menu->descriptor;
3600 (*desc->handle_mousedown) (desc, event);
3603 /******************************************************************/
3604 static void iconDblClick(WObjDescriptor *desc, XEvent *event)
3606 WAppIcon *btn = desc->parent;
3607 WDock *dock = btn->dock;
3608 WApplication *wapp = NULL;
3609 int unhideHere = 0;
3611 if (btn->icon->owner && !(event->xbutton.state & ControlMask)) {
3612 wapp = wApplicationOf(btn->icon->owner->main_window);
3614 assert(wapp != NULL);
3616 unhideHere = (event->xbutton.state & ShiftMask);
3618 /* go to the last workspace that the user worked on the app */
3619 if (wapp->last_workspace != w_global.workspace.current && !unhideHere)
3620 wWorkspaceChange(dock->screen_ptr, wapp->last_workspace);
3622 wUnhideApplication(wapp, event->xbutton.button == Button2, unhideHere);
3624 if (event->xbutton.state & MOD_MASK)
3625 wHideOtherApplications(btn->icon->owner);
3626 } else {
3627 if (event->xbutton.button == Button1) {
3628 if (event->xbutton.state & MOD_MASK) {
3629 /* raise/lower dock */
3630 toggleLowered(dock);
3631 } else if (btn == w_global.clip.icon) {
3632 if (getClipButton(event->xbutton.x, event->xbutton.y) != CLIP_IDLE)
3633 handleClipChangeWorkspace(dock->screen_ptr, event);
3634 else if (wPreferences.flags.clip_merged_in_dock) {
3635 // Is actually the dock
3636 if (btn->command)
3638 if (!btn->launching && (!btn->running || (event->xbutton.state & ControlMask)))
3639 launchDockedApplication(btn, False);
3641 else
3643 wShowInfoPanel(dock->screen_ptr);
3646 else
3647 toggleCollapsed(dock);
3648 } else if (wIsADrawer(btn)) {
3649 toggleCollapsed(dock);
3650 } else if (btn->command) {
3651 if (!btn->launching && (!btn->running || (event->xbutton.state & ControlMask)))
3652 launchDockedApplication(btn, False);
3653 } else if (btn->xindex == 0 && btn->yindex == 0 && btn->dock->type == WM_DOCK) {
3654 wShowInfoPanel(dock->screen_ptr);
3660 static void handleDockMove(WDock *dock, WAppIcon *aicon, XEvent *event)
3662 WScreen *scr = dock->screen_ptr;
3663 int ofs_x = event->xbutton.x, ofs_y = event->xbutton.y;
3664 WIcon *icon = aicon->icon;
3665 WAppIcon *tmpaicon;
3666 WDrawerChain *dc;
3667 int x = aicon->x_pos, y = aicon->y_pos;;
3668 int shad_x = x, shad_y = y;
3669 XEvent ev;
3670 int grabbed = 0, done, previously_on_right, now_on_right, previous_x_pos, i;
3671 Pixmap ghost = None;
3672 int superfluous = wPreferences.superfluous; /* we catch it to avoid problems */
3674 if (XGrabPointer(dpy, aicon->icon->core->window, True, ButtonMotionMask
3675 | ButtonReleaseMask | ButtonPressMask, GrabModeAsync,
3676 GrabModeAsync, None, None, CurrentTime) != GrabSuccess)
3677 wwarning("pointer grab failed for dock move");
3679 if (dock->type == WM_DRAWER) {
3680 Window wins[2];
3681 wins[0] = icon->core->window;
3682 wins[1] = scr->dock_shadow;
3683 XRestackWindows(dpy, wins, 2);
3684 XMoveResizeWindow(dpy, scr->dock_shadow, aicon->x_pos, aicon->y_pos,
3685 ICON_SIZE, ICON_SIZE);
3686 if (superfluous) {
3687 if (icon->pixmap!=None)
3688 ghost = MakeGhostIcon(scr, icon->pixmap);
3689 else
3690 ghost = MakeGhostIcon(scr, icon->core->window);
3692 XSetWindowBackgroundPixmap(dpy, scr->dock_shadow, ghost);
3693 XClearWindow(dpy, scr->dock_shadow);
3695 XMapWindow(dpy, scr->dock_shadow);
3698 previously_on_right = now_on_right = dock->on_right_side;
3699 previous_x_pos = dock->x_pos;
3700 done = 0;
3701 while (!done) {
3702 WMMaskEvent(dpy, PointerMotionMask | ButtonReleaseMask | ButtonPressMask
3703 | ButtonMotionMask | ExposureMask | EnterWindowMask, &ev);
3704 switch (ev.type) {
3705 case Expose:
3706 WMHandleEvent(&ev);
3707 break;
3709 case EnterNotify:
3710 /* It means the cursor moved so fast that it entered
3711 * something else (if moving slowly, it would have
3712 * stayed in the dock that is being moved. Ignore such
3713 * "spurious" EnterNotifiy's */
3714 break;
3716 case MotionNotify:
3717 if (!grabbed) {
3718 if (abs(ofs_x - ev.xmotion.x) >= MOVE_THRESHOLD
3719 || abs(ofs_y - ev.xmotion.y) >= MOVE_THRESHOLD) {
3720 XChangeActivePointerGrab(dpy, ButtonMotionMask
3721 | ButtonReleaseMask | ButtonPressMask,
3722 wPreferences.cursor[WCUR_MOVE], CurrentTime);
3723 grabbed = 1;
3725 break;
3727 switch (dock->type) {
3728 case WM_CLIP:
3729 x = ev.xmotion.x_root - ofs_x;
3730 y = ev.xmotion.y_root - ofs_y;
3731 wScreenKeepInside(scr, &x, &y, ICON_SIZE, ICON_SIZE);
3732 moveDock(dock, x, y);
3733 break;
3734 case WM_DOCK:
3735 x = ev.xmotion.x_root - ofs_x;
3736 y = ev.xmotion.y_root - ofs_y;
3737 if (previously_on_right)
3739 now_on_right = (ev.xmotion.x_root >= previous_x_pos - ICON_SIZE);
3741 else
3743 now_on_right = (ev.xmotion.x_root > previous_x_pos + ICON_SIZE * 2);
3745 if (now_on_right != dock->on_right_side)
3747 dock->on_right_side = now_on_right;
3748 swapDock(dock);
3749 wArrangeIcons(scr, False);
3751 // Also perform the vertical move
3752 wScreenKeepInside(scr, &x, &y, ICON_SIZE, ICON_SIZE);
3753 moveDock(dock, dock->x_pos, y);
3754 if (wPreferences.flags.wrap_appicons_in_dock)
3756 for (i = 0; i < dock->max_icons; i++) {
3757 int new_y, new_index, j, ok;
3758 if ((tmpaicon = dock->icon_array[i]) == NULL)
3759 continue;
3760 if (onScreen(scr, tmpaicon->x_pos, tmpaicon->y_pos))
3761 continue;
3762 new_y = (tmpaicon->y_pos + ICON_SIZE * dock->max_icons) % (ICON_SIZE * dock->max_icons);
3763 new_index = (new_y - dock->y_pos) / ICON_SIZE;
3764 if (!onScreen(scr, tmpaicon->x_pos, new_y))
3765 continue;
3766 ok = 1;
3767 for (j = 0; j < dock->max_icons; j++)
3769 if (dock->icon_array[j] != NULL &&
3770 dock->icon_array[j]->yindex == new_index)
3772 ok = 0;
3773 break;
3776 if (!ok || getDrawer(scr, new_index) != NULL)
3777 continue;
3778 wDockReattachIcon(dock, tmpaicon, tmpaicon->xindex, new_index);
3780 for (dc = scr->drawers; dc != NULL; dc = dc->next)
3782 int new_y, new_index, j, ok;
3783 tmpaicon = dc->adrawer->icon_array[0];
3784 if (onScreen(scr, tmpaicon->x_pos, tmpaicon->y_pos))
3785 continue;
3786 new_y = (tmpaicon->y_pos + ICON_SIZE * dock->max_icons) % (ICON_SIZE * dock->max_icons);
3787 new_index = (new_y - dock->y_pos) / ICON_SIZE;
3788 if (!onScreen(scr, tmpaicon->x_pos, new_y))
3789 continue;
3790 ok = 1;
3791 for (j = 0; j < dock->max_icons; j++)
3793 if (dock->icon_array[j] != NULL &&
3794 dock->icon_array[j]->yindex == new_index)
3796 ok = 0;
3797 break;
3800 if (!ok || getDrawer(scr, new_index) != NULL)
3801 continue;
3802 moveDock(dc->adrawer, tmpaicon->x_pos, new_y);
3805 break;
3806 case WM_DRAWER:
3808 WDock *real_dock = dock->screen_ptr->dock;
3809 Bool snapped;
3810 int ix, iy;
3811 x = ev.xmotion.x_root - ofs_x;
3812 y = ev.xmotion.y_root - ofs_y;
3813 snapped = wDockSnapIcon(real_dock, aicon, x, y, &ix, &iy, True);
3814 if (snapped) {
3815 shad_x = real_dock->x_pos + ix * wPreferences.icon_size;
3816 shad_y = real_dock->y_pos + iy * wPreferences.icon_size;
3817 XMoveWindow(dpy, scr->dock_shadow, shad_x, shad_y);
3819 moveDock(dock, x, y);
3820 break;
3823 break;
3825 case ButtonPress:
3826 break;
3828 case ButtonRelease:
3829 if (ev.xbutton.button != event->xbutton.button)
3830 break;
3831 XUngrabPointer(dpy, CurrentTime);
3832 if (dock->type == WM_DRAWER) {
3833 Window *wins[dock->icon_count];
3835 for (i = 0; i < dock->max_icons; i++) {
3836 if ((tmpaicon = dock->icon_array[i]) == NULL)
3837 continue;
3838 wins[ tmpaicon->xindex + (dock->on_right_side ? dock->icon_count - 1 : 0) ] = &tmpaicon->icon->core->window;
3840 SlideWindows(wins, dock->icon_count,
3841 (dock->on_right_side ? x - (dock->icon_count - 1) * ICON_SIZE : x),
3843 (dock->on_right_side ? shad_x - (dock->icon_count - 1) * ICON_SIZE : shad_x),
3844 shad_y);
3845 XUnmapWindow(dpy, scr->dock_shadow);
3846 moveDock(dock, shad_x, shad_y);
3847 XResizeWindow(dpy, scr->dock_shadow, ICON_SIZE, ICON_SIZE);
3849 done = 1;
3850 break;
3853 if (superfluous) {
3854 if (ghost != None)
3855 XFreePixmap(dpy, ghost);
3856 XSetWindowBackground(dpy, scr->dock_shadow, scr->white_pixel);
3861 static int getClipButton(int px, int py)
3863 int pt = (CLIP_BUTTON_SIZE + 2) * ICON_SIZE / 64;
3865 if (px < 0 || py < 0 || px >= ICON_SIZE || py >= ICON_SIZE)
3866 return CLIP_IDLE;
3868 if (py <= pt - ((int)ICON_SIZE - 1 - px))
3869 return CLIP_FORWARD;
3870 else if (px <= pt - ((int)ICON_SIZE - 1 - py))
3871 return CLIP_REWIND;
3873 return CLIP_IDLE;
3876 static void handleClipChangeWorkspace(WScreen *scr, XEvent *event)
3878 XEvent ev;
3879 int done, direction, new_ws;
3880 int new_dir;
3881 WDock *clip = w_global.clip.icon->dock;
3883 direction = getClipButton(event->xbutton.x, event->xbutton.y);
3885 clip->lclip_button_pushed = direction == CLIP_REWIND;
3886 clip->rclip_button_pushed = direction == CLIP_FORWARD;
3888 wClipIconPaint();
3889 done = 0;
3890 while (!done) {
3891 WMMaskEvent(dpy, ExposureMask | ButtonMotionMask | ButtonReleaseMask | ButtonPressMask, &ev);
3892 switch (ev.type) {
3893 case Expose:
3894 WMHandleEvent(&ev);
3895 break;
3897 case MotionNotify:
3898 new_dir = getClipButton(ev.xmotion.x, ev.xmotion.y);
3899 if (new_dir != direction) {
3900 direction = new_dir;
3901 clip->lclip_button_pushed = direction == CLIP_REWIND;
3902 clip->rclip_button_pushed = direction == CLIP_FORWARD;
3903 wClipIconPaint();
3905 break;
3907 case ButtonPress:
3908 break;
3910 case ButtonRelease:
3911 if (ev.xbutton.button == event->xbutton.button)
3912 done = 1;
3916 clip->lclip_button_pushed = 0;
3917 clip->rclip_button_pushed = 0;
3919 new_ws = wPreferences.ws_advance || (event->xbutton.state & ControlMask);
3921 if (direction == CLIP_FORWARD) {
3922 if (w_global.workspace.current < w_global.workspace.count - 1)
3923 wWorkspaceChange(scr, w_global.workspace.current + 1);
3924 else if (new_ws && w_global.workspace.current < MAX_WORKSPACES - 1)
3925 wWorkspaceChange(scr, w_global.workspace.current + 1);
3926 else if (wPreferences.ws_cycle)
3927 wWorkspaceChange(scr, 0);
3928 } else if (direction == CLIP_REWIND) {
3929 if (w_global.workspace.current > 0)
3930 wWorkspaceChange(scr, w_global.workspace.current - 1);
3931 else if (w_global.workspace.current == 0 && wPreferences.ws_cycle)
3932 wWorkspaceChange(scr, w_global.workspace.count - 1);
3935 wClipIconPaint();
3938 static void iconMouseDown(WObjDescriptor *desc, XEvent *event)
3940 WAppIcon *aicon = desc->parent;
3941 WDock *dock = aicon->dock;
3942 WScreen *scr = aicon->icon->core->screen_ptr;
3944 if (aicon->editing || WCHECK_STATE(WSTATE_MODAL))
3945 return;
3947 scr->last_dock = dock;
3949 if (dock->menu->flags.mapped)
3950 wMenuUnmap(dock->menu);
3952 if (IsDoubleClick(scr, event)) {
3953 /* double-click was not in the main clip icon */
3954 if (dock->type != WM_CLIP || aicon->xindex != 0 || aicon->yindex != 0
3955 || getClipButton(event->xbutton.x, event->xbutton.y) == CLIP_IDLE) {
3956 iconDblClick(desc, event);
3957 return;
3961 if (event->xbutton.button == Button1) {
3962 if (event->xbutton.state & MOD_MASK)
3963 wDockLower(dock);
3964 else
3965 wDockRaise(dock);
3967 if ((event->xbutton.state & ShiftMask) && aicon != w_global.clip.icon && dock->type != WM_DOCK) {
3968 wIconSelect(aicon->icon);
3969 return;
3972 if (aicon->yindex == 0 && aicon->xindex == 0) {
3973 if (getClipButton(event->xbutton.x, event->xbutton.y) != CLIP_IDLE &&
3974 (dock->type == WM_CLIP || (dock->type == WM_DOCK && wPreferences.flags.clip_merged_in_dock)))
3975 handleClipChangeWorkspace(scr, event);
3976 else
3977 handleDockMove(dock, aicon, event);
3978 } else {
3979 Bool hasMoved = wHandleAppIconMove(aicon, event);
3980 if (wPreferences.single_click && !hasMoved)
3981 iconDblClick(desc, event);
3983 } else if (event->xbutton.button == Button2 && aicon == w_global.clip.icon) {
3984 if (!w_global.clip.ws_menu)
3985 w_global.clip.ws_menu = wWorkspaceMenuMake(scr, False);
3987 if (w_global.clip.ws_menu) {
3988 WMenu *wsMenu = w_global.clip.ws_menu;
3989 int xpos;
3991 wWorkspaceMenuUpdate(wsMenu);
3993 xpos = event->xbutton.x_root - wsMenu->frame->core->width / 2 - 1;
3994 if (xpos < 0) {
3995 xpos = 0;
3996 } else if (xpos + wsMenu->frame->core->width > scr->scr_width - 2) {
3997 xpos = scr->scr_width - wsMenu->frame->core->width - 4;
3999 wMenuMapAt(wsMenu, xpos, event->xbutton.y_root + 2, False);
4001 desc = &wsMenu->menu->descriptor;
4002 event->xany.send_event = True;
4003 (*desc->handle_mousedown) (desc, event);
4005 } else if (event->xbutton.button == Button2 && dock->type == WM_CLIP &&
4006 (event->xbutton.state & ShiftMask) && aicon != w_global.clip.icon) {
4007 wClipMakeIconOmnipresent(aicon, !aicon->omnipresent);
4008 } else if (event->xbutton.button == Button3) {
4009 if (event->xbutton.send_event &&
4010 XGrabPointer(dpy, aicon->icon->core->window, True, ButtonMotionMask
4011 | ButtonReleaseMask | ButtonPressMask, GrabModeAsync,
4012 GrabModeAsync, None, None, CurrentTime) != GrabSuccess) {
4013 wwarning("pointer grab failed for dockicon menu");
4014 return;
4017 openDockMenu(dock, aicon, event);
4018 } else if (event->xbutton.button == Button2) {
4019 WAppIcon *btn = desc->parent;
4021 if (!btn->launching && (!btn->running || (event->xbutton.state & ControlMask)))
4022 launchDockedApplication(btn, True);
4026 static void clipEnterNotify(WObjDescriptor *desc, XEvent *event)
4028 WAppIcon *btn = (WAppIcon *) desc->parent;
4029 WDock *dock, *tmp;
4030 WScreen *scr;
4032 /* Parameter not used, but tell the compiler that it is ok */
4033 (void) event;
4035 assert(event->type == EnterNotify);
4037 if (desc->parent_type != WCLASS_DOCK_ICON)
4038 return;
4040 scr = btn->icon->core->screen_ptr;
4041 dock = btn->dock;
4043 if (dock == NULL)
4044 return;
4046 /* The auto raise/lower code */
4047 tmp = (dock->type == WM_DRAWER ? scr->dock : dock);
4048 if (tmp->auto_lower_magic) {
4049 WMDeleteTimerHandler(tmp->auto_lower_magic);
4050 tmp->auto_lower_magic = NULL;
4052 if (tmp->auto_raise_lower && !tmp->auto_raise_magic)
4053 tmp->auto_raise_magic = WMAddTimerHandler(wPreferences.clip_auto_raise_delay, clipAutoRaise, (void *) tmp);
4055 if (dock->type != WM_CLIP && dock->type != WM_DRAWER)
4056 return;
4058 /* The auto expand/collapse code */
4059 if (dock->auto_collapse_magic) {
4060 WMDeleteTimerHandler(dock->auto_collapse_magic);
4061 dock->auto_collapse_magic = NULL;
4063 if (dock->auto_collapse && !dock->auto_expand_magic)
4064 dock->auto_expand_magic = WMAddTimerHandler(wPreferences.clip_auto_expand_delay, clipAutoExpand, (void *)dock);
4067 static void clipLeave(WDock *dock)
4069 XEvent event;
4070 WObjDescriptor *desc = NULL;
4071 WDock *tmp;
4073 if (dock == NULL)
4074 return;
4076 if (XCheckTypedEvent(dpy, EnterNotify, &event) != False) {
4077 if (XFindContext(dpy, event.xcrossing.window, w_global.context.client_win,
4078 (XPointer *) & desc) != XCNOENT
4079 && desc && desc->parent_type == WCLASS_DOCK_ICON
4080 && ((WAppIcon *) desc->parent)->dock == dock) {
4081 /* We haven't left the dock/clip/drawer yet */
4082 XPutBackEvent(dpy, &event);
4083 return;
4086 XPutBackEvent(dpy, &event);
4087 } else {
4088 /* We entered a withdrawn window, so we're still in Clip */
4089 return;
4092 tmp = (dock->type == WM_DRAWER ? dock->screen_ptr->dock : dock);
4093 if (tmp->auto_raise_magic) {
4094 WMDeleteTimerHandler(tmp->auto_raise_magic);
4095 tmp->auto_raise_magic = NULL;
4097 if (tmp->auto_raise_lower && !tmp->auto_lower_magic)
4098 tmp->auto_lower_magic = WMAddTimerHandler(wPreferences.clip_auto_lower_delay, clipAutoLower, (void *)tmp);
4100 if (dock->type != WM_CLIP && dock->type != WM_DRAWER)
4101 return;
4103 if (dock->auto_expand_magic) {
4104 WMDeleteTimerHandler(dock->auto_expand_magic);
4105 dock->auto_expand_magic = NULL;
4107 if (dock->auto_collapse && !dock->auto_collapse_magic)
4108 dock->auto_collapse_magic = WMAddTimerHandler(wPreferences.clip_auto_collapse_delay, clipAutoCollapse, (void *)dock);
4111 static void clipLeaveNotify(WObjDescriptor *desc, XEvent *event)
4113 WAppIcon *btn = (WAppIcon *) desc->parent;
4115 /* Parameter not used, but tell the compiler that it is ok */
4116 (void) event;
4118 assert(event->type == LeaveNotify);
4120 if (desc->parent_type != WCLASS_DOCK_ICON)
4121 return;
4123 clipLeave(btn->dock);
4126 static void clipAutoCollapse(void *cdata)
4128 WDock *dock = (WDock *) cdata;
4130 if (dock->type != WM_CLIP && dock->type != WM_DRAWER)
4131 return;
4133 if (dock->auto_collapse) {
4134 dock->collapsed = 1;
4135 wDockHideIcons(dock);
4137 dock->auto_collapse_magic = NULL;
4140 static void clipAutoExpand(void *cdata)
4142 WDock *dock = (WDock *) cdata;
4144 if (dock->type != WM_CLIP && dock->type != WM_DRAWER)
4145 return;
4147 if (dock->auto_collapse) {
4148 dock->collapsed = 0;
4149 wDockShowIcons(dock);
4151 dock->auto_expand_magic = NULL;
4154 static void clipAutoLower(void *cdata)
4156 WDock *dock = (WDock *) cdata;
4158 if (dock->auto_raise_lower)
4159 wDockLower(dock);
4161 dock->auto_lower_magic = NULL;
4164 static void clipAutoRaise(void *cdata)
4166 WDock *dock = (WDock *) cdata;
4168 if (dock->auto_raise_lower)
4169 wDockRaise(dock);
4171 dock->auto_raise_magic = NULL;
4174 static Bool iconCanBeOmnipresent(WAppIcon *aicon)
4176 WScreen *scr = aicon->icon->core->screen_ptr;
4177 WDock *clip;
4178 WAppIcon *btn;
4179 int i, j;
4181 for (i = 0; i < w_global.workspace.count; i++) {
4182 clip = w_global.workspace.array[i]->clip;
4184 if (clip == aicon->dock)
4185 continue;
4187 if (clip->icon_count + scr->global_icon_count >= clip->max_icons)
4188 return False; /* Clip is full in some workspace */
4190 for (j = 0; j < clip->max_icons; j++) {
4191 btn = clip->icon_array[j];
4192 if (btn && btn->xindex == aicon->xindex && btn->yindex == aicon->yindex)
4193 return False;
4197 return True;
4200 int wClipMakeIconOmnipresent(WAppIcon *aicon, int omnipresent)
4202 WScreen *scr = aicon->icon->core->screen_ptr;
4203 WAppIconChain *new_entry, *tmp, *tmp1;
4204 int status = WO_SUCCESS;
4206 if ((scr->dock && aicon->dock == scr->dock) || aicon == w_global.clip.icon)
4207 return WO_NOT_APPLICABLE;
4209 if (aicon->omnipresent == omnipresent)
4210 return WO_SUCCESS;
4212 if (omnipresent) {
4213 if (iconCanBeOmnipresent(aicon)) {
4214 aicon->omnipresent = 1;
4215 new_entry = wmalloc(sizeof(WAppIconChain));
4216 new_entry->aicon = aicon;
4217 new_entry->next = scr->global_icons;
4218 scr->global_icons = new_entry;
4219 scr->global_icon_count++;
4220 } else {
4221 aicon->omnipresent = 0;
4222 status = WO_FAILED;
4224 } else {
4225 aicon->omnipresent = 0;
4226 if (aicon == scr->global_icons->aicon) {
4227 tmp = scr->global_icons->next;
4228 wfree(scr->global_icons);
4229 scr->global_icons = tmp;
4230 scr->global_icon_count--;
4231 } else {
4232 tmp = scr->global_icons;
4233 while (tmp->next) {
4234 if (tmp->next->aicon == aicon) {
4235 tmp1 = tmp->next->next;
4236 wfree(tmp->next);
4237 tmp->next = tmp1;
4238 scr->global_icon_count--;
4239 break;
4241 tmp = tmp->next;
4246 wAppIconPaint(aicon);
4248 return status;
4251 static void drawerAppendToChain(WScreen *scr, WDock *drawer)
4253 WDrawerChain **where_to_add;
4255 where_to_add = &scr->drawers;
4256 while ((*where_to_add) != NULL) {
4257 where_to_add = &(*where_to_add)->next;
4260 *where_to_add = wmalloc(sizeof(WDrawerChain));
4261 (*where_to_add)->adrawer = drawer;
4262 (*where_to_add)->next = NULL;
4263 scr->drawer_count++;
4267 static void drawerRemoveFromChain(WScreen *scr, WDock *drawer)
4269 WDrawerChain *next, **to_remove;
4271 to_remove = &scr->drawers;
4272 while (True) {
4273 if (*to_remove == NULL) {
4274 wwarning("The drawer to be removed can not be found.");
4275 return;
4277 if ((*to_remove)->adrawer == drawer)
4278 break;
4280 to_remove = &(*to_remove)->next;
4282 next = (*to_remove)->next;
4283 wfree(*to_remove);
4284 *to_remove = next;
4285 scr->drawer_count--;
4289 /* Don't free the returned string. Duplicate it. */
4290 static char * findUniqueName(WScreen *scr, const char *instance_basename)
4292 static char buffer[128];
4293 WDrawerChain *dc;
4294 int i;
4295 Bool already_in_use = True;
4297 #define UNIQUE_NAME_WATCHDOG 128
4298 for (i = 0; already_in_use && i < UNIQUE_NAME_WATCHDOG; i++) {
4299 snprintf(buffer, sizeof buffer, "%s%d", instance_basename, i);
4301 already_in_use = False;
4303 for (dc = scr->drawers; dc != NULL; dc = dc->next) {
4304 if (!strncmp(dc->adrawer->icon_array[0]->wm_instance, buffer,
4305 sizeof buffer)) {
4306 already_in_use = True;
4307 break;
4312 if (i == UNIQUE_NAME_WATCHDOG)
4313 wwarning("Couldn't find a unique name for drawer in %d attempts.", i);
4314 #undef UNIQUE_NAME_WATCHDOG
4316 return buffer;
4320 static void drawerIconExpose(WObjDescriptor *desc, XEvent *event)
4322 /* Parameter not used, but tell the compiler that it is ok */
4323 (void) event;
4325 wDrawerIconPaint((WAppIcon *) desc->parent);
4329 static int addADrawer(WScreen *scr)
4331 int i, y, sig, found_y;
4332 WDock *drawer, *dock = scr->dock;
4333 WDrawerChain *dc;
4334 char can_be_here[2 * dock->max_icons - 1];
4336 if (dock->icon_count + scr->drawer_count >= dock->max_icons)
4337 return -1;
4339 for (y = -dock->max_icons + 1; y < dock->max_icons; y++) {
4340 can_be_here[y + dock->max_icons - 1] = True;
4342 for (i = 0; i < dock->max_icons; i++) {
4343 if (dock->icon_array[i] != NULL)
4344 can_be_here[dock->icon_array[i]->yindex + dock->max_icons - 1] = False;
4346 for (dc = scr->drawers; dc != NULL; dc = dc->next) {
4347 y = (int) ((dc->adrawer->y_pos - dock->y_pos) / ICON_SIZE);
4348 can_be_here[y + dock->max_icons - 1] = False;
4351 found_y = False;
4352 for (sig = 1; !found_y && sig > -2; sig -= 2) // 1, then -1
4354 for (y = sig; sig * y < dock->max_icons; y += sig)
4356 if (can_be_here[y + dock->max_icons - 1] &&
4357 onScreen(scr, dock->x_pos, dock->y_pos + y * ICON_SIZE))
4359 found_y = True;
4360 break;
4365 if (!found_y)
4366 /* This can happen even when dock->icon_count + scr->drawer_count
4367 * < dock->max_icons when the dock is not aligned on an
4368 * ICON_SIZE multiple, as some space is lost above and under it */
4369 return -1;
4371 drawer = wDockCreate(scr, WM_DRAWER, NULL);
4372 drawer->lowered = scr->dock->lowered;
4373 if (!drawer->lowered)
4374 ChangeStackingLevel(drawer->icon_array[0]->icon->core, WMDockLevel);
4375 else
4376 ChangeStackingLevel(drawer->icon_array[0]->icon->core, WMNormalLevel);
4377 drawer->auto_raise_lower = scr->dock->auto_raise_lower;
4378 drawer->x_pos = dock->x_pos;
4379 drawer->y_pos = dock->y_pos + ICON_SIZE * y;
4380 drawer->icon_array[0]->xindex = 0;
4381 drawer->icon_array[0]->yindex = 0;
4382 drawer->icon_array[0]->x_pos = drawer->x_pos;
4383 drawer->icon_array[0]->y_pos = drawer->y_pos;
4384 XMoveWindow(dpy, drawer->icon_array[0]->icon->core->window,
4385 drawer->icon_array[0]->x_pos, drawer->icon_array[0]->y_pos);
4387 return 0;
4391 static void addADrawerCallback(WMenu *menu, WMenuEntry *entry)
4393 /* Parameter not used, but tell the compiler that it is ok */
4394 (void) menu;
4396 assert(entry->clientdata!=NULL);
4397 addADrawer(((WAppIcon *) entry->clientdata)->dock->screen_ptr);
4401 static void drawerDestroy(WDock *drawer)
4403 WScreen *scr;
4404 int i;
4405 WAppIcon *aicon = NULL;
4406 WMArray *icons;
4408 if (drawer == NULL)
4409 return;
4411 scr = drawer->screen_ptr;
4413 /* Note regarding menus: we can't delete any dock/clip/drawer menu, because
4414 * that would (attempt to) wfree some memory in gettext library (see menu
4415 * entries that have several "versions", such like "Hide" and "Unhide"). */
4416 wDefaultPurgeInfo(drawer->icon_array[0]->wm_instance,
4417 drawer->icon_array[0]->wm_class);
4419 if (drawer->icon_count == 2) {
4420 /* Drawer contains a single appicon: dock it where the drawer was */
4421 for (i = 1; i < drawer->max_icons; i++) {
4422 if ((aicon = drawer->icon_array[i]))
4423 break;
4426 wDockMoveIconBetweenDocks(drawer, scr->dock, aicon,
4427 0, (drawer->y_pos - scr->dock->y_pos) / ICON_SIZE);
4428 XMoveWindow(dpy, aicon->icon->core->window, drawer->x_pos, drawer->y_pos);
4429 XMapWindow(dpy, aicon->icon->core->window);
4430 } else if (drawer->icon_count > 2) {
4431 icons = WMCreateArray(drawer->icon_count - 1);
4432 for (i = 1; i < drawer->max_icons; i++) {
4433 if (!(aicon = drawer->icon_array[i]))
4434 continue;
4435 WMAddToArray(icons, aicon);
4437 removeIcons(icons, drawer);
4440 if (drawer->auto_collapse_magic) {
4441 WMDeleteTimerHandler(drawer->auto_collapse_magic);
4442 drawer->auto_collapse_magic = NULL;
4444 if (drawer->auto_lower_magic) {
4445 WMDeleteTimerHandler(drawer->auto_lower_magic);
4446 drawer->auto_lower_magic = NULL;
4449 wAppIconDestroy(drawer->icon_array[0]);
4450 wfree(drawer->icon_array);
4451 wfree(drawer);
4453 drawerRemoveFromChain(scr, drawer);
4454 if (scr->last_dock == drawer)
4455 scr->last_dock = NULL;
4456 if (scr->attracting_drawer == drawer)
4457 scr->attracting_drawer = NULL;
4461 static void removeDrawerCallback(WMenu *menu, WMenuEntry *entry)
4463 WDock *dock = ((WAppIcon*)entry->clientdata)->dock;
4465 /* Parameter not used, but tell the compiler that it is ok */
4466 (void) menu;
4468 assert(dock != NULL);
4470 if (dock->icon_count > 2) {
4471 if (wMessageDialog(dock->screen_ptr, _("Drawer"),
4472 _("All icons in this drawer will be detached!"),
4473 _("OK"), _("Cancel"), NULL) != WAPRDefault)
4474 return;
4476 drawerDestroy(dock);
4480 void wDrawerIconPaint(WAppIcon *dicon)
4482 Window win = dicon->icon->core->window;
4483 WScreen *scr = dicon->icon->core->screen_ptr;
4484 XPoint p[4];
4485 GC gc = scr->draw_gc;
4486 WMColor *color;
4488 wIconPaint(dicon->icon);
4490 if (!dicon->dock->collapsed)
4491 color = scr->clip_title_color[CLIP_NORMAL];
4492 else
4493 color = scr->clip_title_color[CLIP_COLLAPSED];
4494 XSetForeground(dpy, gc, WMColorPixel(color));
4496 if (dicon->dock->on_right_side) {
4497 p[0].x = p[3].x = 10;
4498 p[0].y = p[3].y = ICON_SIZE / 2 - 5;
4499 p[1].x = 10;
4500 p[1].y = ICON_SIZE / 2 + 5;
4501 p[2].x = 5;
4502 p[2].y = ICON_SIZE / 2;
4504 else {
4505 p[0].x = p[3].x = ICON_SIZE-1 - 10;
4506 p[0].y = p[3].y = ICON_SIZE / 2 - 5;
4507 p[1].x = ICON_SIZE-1 - 10;
4508 p[1].y = ICON_SIZE / 2 + 5;
4509 p[2].x = ICON_SIZE-1 - 5;
4510 p[2].y = ICON_SIZE / 2;
4512 XFillPolygon(dpy, win, gc, p,3,Convex,CoordModeOrigin);
4513 XDrawLines(dpy, win, gc, p,4,CoordModeOrigin);
4517 RImage* wDrawerMakeTile(WScreen *scr, RImage *normalTile)
4519 RImage *tile = RCloneImage(normalTile);
4520 RColor dark;
4521 RColor light;
4523 dark.alpha = 0;
4524 dark.red = dark.green = dark.blue = 60;
4526 light.alpha = 0;
4527 light.red = light.green = light.blue = 80;
4529 /* arrow bevel */
4530 if (!scr->dock || scr->dock->on_right_side) {
4531 ROperateLine(tile, RSubtractOperation, 11, ICON_SIZE / 2 - 7,
4532 4, ICON_SIZE / 2, &dark); /* / */
4533 ROperateLine(tile, RSubtractOperation, 11, ICON_SIZE / 2 + 7,
4534 4, ICON_SIZE / 2, &dark); /* \ */
4535 ROperateLine(tile, RAddOperation, 11, ICON_SIZE / 2 - 7,
4536 11, ICON_SIZE / 2 + 7, &light); /* | */
4538 else {
4539 ROperateLine(tile, RSubtractOperation, ICON_SIZE-1 - 11, ICON_SIZE / 2 - 7,
4540 ICON_SIZE-1 - 4, ICON_SIZE / 2, &dark); /* \ */
4541 ROperateLine(tile, RAddOperation, ICON_SIZE-1 - 11, ICON_SIZE / 2 + 7,
4542 ICON_SIZE-1 - 4, ICON_SIZE / 2, &light); /* / */
4543 ROperateLine(tile, RSubtractOperation, ICON_SIZE-1 - 11, ICON_SIZE / 2 - 7,
4544 ICON_SIZE-1 - 11, ICON_SIZE / 2 + 7, &dark); /* | */
4546 return tile;
4550 static void swapDrawer(WDock *drawer, int new_x)
4552 int i;
4554 drawer->on_right_side = !drawer->on_right_side;
4555 drawer->x_pos = new_x;
4557 for (i = 0; i < drawer->max_icons; i++) {
4558 WAppIcon *ai;
4559 if ((ai = drawer->icon_array[i]) == NULL)
4560 continue;
4561 ai->xindex *= -1; /* so A B C becomes C B A */
4562 ai->x_pos = new_x + ai->xindex * ICON_SIZE;
4564 /* Update drawer's tile */
4565 if (i == 0) {
4566 wIconUpdate(ai->icon);
4567 wDrawerIconPaint(ai);
4569 XMoveWindow(dpy, ai->icon->core->window, ai->x_pos, ai->y_pos);
4574 static void swapDrawers(WScreen *scr, int new_x)
4576 WDrawerChain *dc;
4578 if (scr->drawer_tile)
4579 RReleaseImage(scr->drawer_tile);
4581 scr->drawer_tile = wDrawerMakeTile(scr, scr->icon_tile);
4583 for (dc = scr->drawers; dc != NULL; dc = dc->next)
4584 swapDrawer(dc->adrawer, new_x);
4588 int wIsADrawer(WAppIcon *aicon)
4590 return aicon && aicon->dock &&
4591 aicon->dock->type == WM_DRAWER && aicon->dock->icon_array[0] == aicon;
4595 static WDock* getDrawer(WScreen *scr, int y_index)
4597 WDrawerChain *dc;
4599 for (dc = scr->drawers; dc != NULL; dc = dc->next) {
4600 if (dc->adrawer->y_pos - scr->dock->y_pos == y_index * ICON_SIZE)
4601 return dc->adrawer;
4603 return NULL;
4608 /* Find the "hole" a moving appicon created when snapped into the
4609 * drawer. redocking is a boolean. If the moving appicon comes from the
4610 * drawer, drawer->icon_count is correct. If not, redocking is then false and
4611 * there are now drawer->icon_count plus one appicons in the drawer. */
4612 static int indexOfHole(WDock *drawer, WAppIcon *moving_aicon, int redocking)
4614 int index_of_hole, i;
4616 /* Classic interview question...
4618 * We have n-1 (n = drawer->icon_count-1 or drawer->icon_count, see
4619 * redocking) appicons, whose xindex are unique in [1..n]. One is missing:
4620 * that's where the ghost of the moving appicon is, that's what the
4621 * function should return.
4623 * We compute 1+2+...+n (this sum is equal to n*(n+1)/2), we substract to
4624 * this sum the xindex of each of the n-1 appicons, and we get the correct
4625 * index! */
4627 if (redocking) {
4628 index_of_hole = (drawer->icon_count - 1) * drawer->icon_count / 2;
4629 } else {
4630 index_of_hole = drawer->icon_count * (drawer->icon_count + 1) / 2;
4632 index_of_hole *= (drawer->on_right_side ? -1 : 1);
4634 for (i = 1; i < drawer->max_icons; i++) {
4635 if (drawer->icon_array[i] && drawer->icon_array[i] != moving_aicon)
4636 index_of_hole -= drawer->icon_array[i]->xindex;
4638 /* wmessage(" Index of the moving appicon is %d (%sredocking)", index_of_hole, (redocking ? "" : "not ")); */
4639 if (abs(index_of_hole) > abs(drawer->icon_count) - (redocking ? 1 : 0))
4640 wwarning(" index_of_hole is too large ! (%d greater than %d)",
4641 index_of_hole, abs(drawer->icon_count) - (redocking ? 1 : 0));
4642 if (index_of_hole == 0)
4643 wwarning(" index_of_hole == 0 (%sredocking, icon_count == %d)", (redocking ? "" : "not "), drawer->icon_count);
4645 return index_of_hole;
4649 void wSlideAppicons(WAppIcon **appicons, int n, int to_the_left)
4651 int i;
4652 int leftmost = -1, min_index = 9999, from_x = -1; // leftmost and from_x initialized to avoid warning
4653 Window *wins[n];
4654 WAppIcon *aicon;
4656 if (n < 1)
4657 return;
4659 for (i = 0; i < n; i++) {
4660 aicon = appicons[i];
4661 aicon->xindex += (to_the_left ? -1 : +1);
4662 if (aicon->xindex < min_index) {
4663 min_index = aicon->xindex;
4664 leftmost = i;
4665 from_x = aicon->x_pos;
4667 aicon->x_pos += (to_the_left ? -ICON_SIZE : +ICON_SIZE);
4670 for (i = 0; i < n; i++) {
4671 aicon = appicons[i];
4672 wins[aicon->xindex - min_index] = &aicon->icon->core->window;
4674 aicon = appicons[leftmost];
4675 SlideWindows(wins, n, from_x, aicon->y_pos, aicon->x_pos, aicon->y_pos);
4679 void wDrawerFillTheGap(WDock *drawer, WAppIcon *aicon, Bool redocking)
4681 int i, j;
4682 int index_of_hole = indexOfHole(drawer, aicon, redocking);
4683 WAppIcon *aicons_to_shift[drawer->icon_count];
4685 j = 0;
4686 for (i = 0; i < drawer->max_icons; i++) {
4687 WAppIcon *ai = drawer->icon_array[i];
4688 if (ai && ai != aicon &&
4689 abs(ai->xindex) > abs(index_of_hole))
4690 aicons_to_shift[j++] = ai;
4692 if (j != drawer->icon_count - abs(index_of_hole) - (redocking ? 1 : 0))
4693 wwarning("Removing aicon at index %d from %s: j=%d but should be %d",
4694 index_of_hole, drawer->icon_array[0]->wm_instance,
4695 j, drawer->icon_count - abs(index_of_hole) - (redocking ? 1 : 0));
4696 wSlideAppicons(aicons_to_shift, j, !drawer->on_right_side);
4700 static void drawerConsolidateIcons(WDock *drawer)
4702 WAppIcon *aicons_to_shift[drawer->icon_count];
4703 int maxRemaining = 0;
4704 int sum = 0;
4705 int i;
4706 for (i = 0; i < drawer->max_icons; i++) {
4707 WAppIcon *ai = drawer->icon_array[i];
4708 if (ai == NULL)
4709 continue;
4710 sum += abs(ai->xindex);
4711 if (abs(ai->xindex) > maxRemaining)
4712 maxRemaining = abs(ai->xindex);
4714 while (sum != maxRemaining * (maxRemaining + 1) / 2) { // while there is a hole
4715 WAppIcon *ai;
4716 int n;
4717 // Look up for the hole at max position
4718 int maxDeleted;
4719 for (maxDeleted = maxRemaining - 1; maxDeleted > 0; maxDeleted--) {
4720 Bool foundAppIconThere = False;
4721 for (i = 0; i < drawer->max_icons; i++) {
4722 WAppIcon *ai = drawer->icon_array[i];
4723 if (ai == NULL)
4724 continue;
4725 if (abs(ai->xindex) == maxDeleted) {
4726 foundAppIconThere = True;
4727 break;
4730 if (!foundAppIconThere)
4731 break;
4733 assert(maxDeleted > 0); // would mean while test is wrong
4734 n = 0;
4735 for (i = 0; i < drawer->max_icons; i++) {
4736 ai = drawer->icon_array[i];
4737 if (ai != NULL && abs(ai->xindex) > maxDeleted)
4738 aicons_to_shift[n++] = ai;
4740 assert(n == maxRemaining - maxDeleted); // for the code review ;-)
4741 wSlideAppicons(aicons_to_shift, n, !drawer->on_right_side);
4742 // Efficient beancounting
4743 maxRemaining -= 1;
4744 sum -= n;
4750 /* similar to wDockRestoreState, but a lot a specific stuff too... */
4751 static WDock * drawerRestoreState(WScreen *scr, WMPropList *drawer_state)
4753 WDock *drawer;
4754 WMPropList *apps, *value, *dock_state;
4755 WAppIcon *aicon;
4756 int count, i;
4758 if (!drawer_state)
4759 return NULL;
4761 make_keys();
4763 WMRetainPropList(drawer_state);
4765 /* Get the instance name, and create a drawer */
4766 value = WMGetFromPLDictionary(drawer_state, dName);
4767 drawer = wDockCreate(scr, WM_DRAWER, WMGetFromPLString(value));
4769 /* restore DnD command and paste command */
4770 #ifdef XDND
4771 value = WMGetFromPLDictionary(drawer_state, dDropCommand);
4772 if (value && WMIsPLString(value))
4773 drawer->icon_array[0]->dnd_command = wstrdup(WMGetFromPLString(value));
4774 #endif /* XDND */
4776 value = WMGetFromPLDictionary(drawer_state, dPasteCommand);
4777 if (value && WMIsPLString(value))
4778 drawer->icon_array[0]->paste_command = wstrdup(WMGetFromPLString(value));
4780 /* restore position */
4781 value = WMGetFromPLDictionary(drawer_state, dPosition);
4782 if (!value || !WMIsPLString(value))
4783 COMPLAIN("Position");
4784 else {
4785 int x, y, y_index;
4786 if (sscanf(WMGetFromPLString(value), "%i,%i", &x, &y) != 2)
4787 COMPLAIN("Position");
4789 /* check position sanity */
4790 if (x != scr->dock->x_pos) {
4791 x = scr->dock->x_pos;
4793 y_index = (y - scr->dock->y_pos) / ICON_SIZE;
4794 if (y_index >= scr->dock->max_icons) {
4795 /* Here we should do something more intelligent, since it
4796 * can happen even if the user hasn't hand-edited his
4797 * G/D/State file (but uses a lower resolution). */
4798 y_index = scr->dock->max_icons - 1;
4800 y = scr->dock->y_pos + y_index * ICON_SIZE;
4801 moveDock(drawer, x, y);
4804 /* restore dock properties (applist and others) */
4805 dock_state = WMGetFromPLDictionary(drawer_state, dDock);
4807 /* restore lowered/raised state: same as scr->dock, no matter what */
4808 drawer->lowered = scr->dock->lowered;
4809 if (!drawer->lowered)
4810 ChangeStackingLevel(drawer->icon_array[0]->icon->core, WMDockLevel);
4811 else
4812 ChangeStackingLevel(drawer->icon_array[0]->icon->core, WMNormalLevel);
4813 wRaiseFrame(drawer->icon_array[0]->icon->core);
4815 /* restore collapsed state */
4816 drawer->collapsed = 0;
4817 value = WMGetFromPLDictionary(dock_state, dCollapsed);
4818 if (value && strcasecmp(WMGetFromPLString(value), "YES") == 0) {
4819 drawer->collapsed = 1;
4821 /* restore auto-collapsed state */
4822 value = WMGetFromPLDictionary(dock_state, dAutoCollapse);
4823 if (value && strcasecmp(WMGetFromPLString(value), "YES") == 0) {
4824 drawer->auto_collapse = 1;
4825 drawer->collapsed = 1;
4826 } else {
4827 drawer->auto_collapse = 0; // because wDockCreate sets it (drawers only)
4830 /* restore auto-raise/lower state: same as scr->dock, no matter what */
4831 drawer->auto_raise_lower = scr->dock->auto_raise_lower;
4833 /* restore attract icons state */
4834 drawer->attract_icons = 0;
4835 value = WMGetFromPLDictionary(dock_state, dAutoAttractIcons);
4836 if (value && strcasecmp(WMGetFromPLString(value), "YES") == 0) {
4837 drawer->attract_icons = 1;
4838 scr->attracting_drawer = drawer;
4841 /* application list */
4842 apps = WMGetFromPLDictionary(dock_state, dApplications);
4844 if (!apps) {
4845 goto finish;
4848 count = WMGetPropListItemCount(apps);
4850 if (count == 0)
4851 goto finish;
4853 for (i=0; i<count; i++) {
4854 if (drawer->icon_count >= drawer->max_icons) {
4855 wwarning(_("there are too many icons stored in drawer. Ignoring what doesn't fit"));
4856 break;
4859 value = WMGetFromPLArray(apps, i);
4860 aicon = restore_icon_state(scr, value, WM_DRAWER, drawer->icon_count);
4862 drawer->icon_array[drawer->icon_count] = aicon;
4864 if (aicon) {
4865 aicon->dock = drawer;
4866 aicon->x_pos = drawer->x_pos + (aicon->xindex * ICON_SIZE);
4867 aicon->y_pos = drawer->y_pos + (aicon->yindex * ICON_SIZE);
4869 if (!drawer->lowered)
4870 ChangeStackingLevel(aicon->icon->core, WMDockLevel);
4871 else
4872 ChangeStackingLevel(aicon->icon->core, WMNormalLevel);
4874 wCoreConfigure(aicon->icon->core, aicon->x_pos, aicon->y_pos, 0, 0);
4876 if (!drawer->collapsed)
4877 XMapWindow(dpy, aicon->icon->core->window);
4878 wRaiseFrame(aicon->icon->core);
4880 drawer->icon_count++;
4884 finish:
4885 WMReleasePropList(drawer_state);
4887 return drawer;
4891 /* Same kind of comment than for previous function: this function is
4892 * very similar to make_icon_state, but has substential differences as
4893 * well. */
4894 static WMPropList *drawerSaveState(WDock *drawer)
4896 WMPropList *pstr, *drawer_state;
4897 WAppIcon *ai;
4898 char buffer[64];
4900 ai = drawer->icon_array[0];
4901 /* Store its name */
4902 pstr = WMCreatePLString(ai->wm_instance);
4903 drawer_state = WMCreatePLDictionary(dName, pstr, NULL); /* we need this final NULL */
4904 WMReleasePropList(pstr);
4906 /* Store its position */
4907 snprintf(buffer, sizeof(buffer), "%i,%i", ai->x_pos, ai->y_pos);
4908 pstr = WMCreatePLString(buffer);
4909 WMPutInPLDictionary(drawer_state, dPosition, pstr);
4910 WMReleasePropList(pstr);
4912 #ifdef XDND
4913 /* Store its DnD command */
4914 if (ai->dnd_command) {
4915 pstr = WMCreatePLString(ai->dnd_command);
4916 WMPutInPLDictionary(drawer_state, dDropCommand, pstr);
4917 WMReleasePropList(pstr);
4919 #endif /* XDND */
4921 /* Store its paste command */
4922 if (ai->paste_command) {
4923 pstr = WMCreatePLString(ai->paste_command);
4924 WMPutInPLDictionary(drawer_state, dPasteCommand, pstr);
4925 WMReleasePropList(pstr);
4928 /* Store applications list and other properties */
4929 pstr = dockSaveState(drawer);
4930 WMPutInPLDictionary(drawer_state, dDock, pstr);
4931 WMReleasePropList(pstr);
4933 return drawer_state;
4937 void wDrawersSaveState(WScreen *scr)
4939 WMPropList *all_drawers, *drawer_state;
4940 int i;
4941 WDrawerChain *dc;
4943 make_keys();
4945 all_drawers = WMCreatePLArray(NULL);
4946 for (i=0, dc = scr->drawers;
4947 i < scr->drawer_count;
4948 i++, dc = dc->next) {
4949 drawer_state = drawerSaveState(dc->adrawer);
4950 WMAddToPLArray(all_drawers, drawer_state);
4951 WMReleasePropList(drawer_state);
4953 WMPutInPLDictionary(w_global.session_state, dDrawers, all_drawers);
4954 WMReleasePropList(all_drawers);
4958 void wDrawersRestoreState(WScreen *scr)
4960 WMPropList *all_drawers, *drawer_state;
4961 int i;
4963 make_keys();
4965 if (w_global.session_state == NULL)
4966 return;
4968 all_drawers = WMGetFromPLDictionary(w_global.session_state, dDrawers);
4969 if (!all_drawers)
4970 return;
4972 for (i = 0; i < WMGetPropListItemCount(all_drawers); i++) {
4973 drawer_state = WMGetFromPLArray(all_drawers, i);
4974 drawerRestoreState(scr, drawer_state);
4975 // Note: scr->drawers was updated when the the drawer was created