wmaker: replaced macro by an inline function, in X Modifier initialisation
[wmaker-crm.git] / src / dock.c
blob8853fa3f082d0900284dd46b8f27e11da6a66c82
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 = dock->screen_ptr->current_workspace;
179 name = wstrdup(dock->screen_ptr->workspaces[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 != dock->screen_ptr->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->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 < scr->workspace_count; i++)
612 scr->workspaces[ 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 attractIconsCallback(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 = clip->screen_ptr->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 (scr->clip_icon)
828 return scr->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 scr->clip_icon = btn;
865 return btn;
868 static void switchWSCommand(WMenu *menu, WMenuEntry *entry)
870 WAppIcon *btn, *icon = (WAppIcon *) entry->clientdata;
871 WScreen *scr = icon->icon->core->screen_ptr;
872 WDock *src, *dest;
873 WMArray *selectedIcons;
874 int x, y;
876 /* Parameter not used, but tell the compiler that it is ok */
877 (void) menu;
879 if (entry->order == scr->current_workspace)
880 return;
882 src = icon->dock;
883 dest = scr->workspaces[entry->order]->clip;
885 selectedIcons = getSelected(src);
887 if (WMGetArrayItemCount(selectedIcons)) {
888 WMArrayIterator iter;
890 WM_ITERATE_ARRAY(selectedIcons, btn, iter) {
891 if (wDockFindFreeSlot(dest, &x, &y)) {
892 wDockMoveIconBetweenDocks(src, dest, btn, x, y);
893 XUnmapWindow(dpy, btn->icon->core->window);
896 } else if (icon != scr->clip_icon) {
897 if (wDockFindFreeSlot(dest, &x, &y)) {
898 wDockMoveIconBetweenDocks(src, dest, icon, x, y);
899 XUnmapWindow(dpy, icon->icon->core->window);
902 WMFreeArray(selectedIcons);
905 static void launchDockedApplication(WAppIcon *btn, Bool withSelection)
907 WScreen *scr = btn->icon->core->screen_ptr;
909 if (!btn->launching &&
910 ((!withSelection && btn->command != NULL) || (withSelection && btn->paste_command != NULL))) {
911 if (!btn->forced_dock) {
912 btn->relaunching = btn->running;
913 btn->running = 1;
915 if (btn->wm_instance || btn->wm_class) {
916 WWindowAttributes attr;
917 memset(&attr, 0, sizeof(WWindowAttributes));
918 wDefaultFillAttributes(btn->wm_instance, btn->wm_class, &attr, NULL, True);
920 if (!attr.no_appicon && !btn->buggy_app)
921 btn->launching = 1;
922 else
923 btn->running = 0;
925 btn->drop_launch = 0;
926 btn->paste_launch = withSelection;
927 scr->last_dock = btn->dock;
928 btn->pid = execCommand(btn, (withSelection ? btn->paste_command : btn->command), NULL);
929 if (btn->pid > 0) {
930 if (btn->buggy_app) {
931 /* give feedback that the app was launched */
932 btn->launching = 1;
933 dockIconPaint(btn);
934 btn->launching = 0;
935 WMAddTimerHandler(200, (WMCallback *) dockIconPaint, btn);
936 } else {
937 dockIconPaint(btn);
939 } else {
940 wwarning(_("could not launch application %s"), btn->command);
941 btn->launching = 0;
942 if (!btn->relaunching)
943 btn->running = 0;
948 static void updateWorkspaceMenu(WMenu *menu, WAppIcon *icon)
950 WScreen *scr = menu->frame->screen_ptr;
951 int i;
953 if (!menu || !icon)
954 return;
956 for (i = 0; i < scr->workspace_count; i++) {
957 if (i < menu->entry_no) {
958 if (strcmp(menu->entries[i]->text, scr->workspaces[i]->name) != 0) {
959 wfree(menu->entries[i]->text);
960 menu->entries[i]->text = wstrdup(scr->workspaces[i]->name);
961 menu->flags.realized = 0;
963 menu->entries[i]->clientdata = (void *)icon;
964 } else {
965 wMenuAddCallback(menu, scr->workspaces[i]->name, switchWSCommand, (void *)icon);
967 menu->flags.realized = 0;
970 if (i == scr->current_workspace)
971 wMenuSetEnabled(menu, i, False);
972 else
973 wMenuSetEnabled(menu, i, True);
976 if (!menu->flags.realized)
977 wMenuRealize(menu);
980 static WMenu *makeWorkspaceMenu(WScreen *scr)
982 WMenu *menu;
984 menu = wMenuCreate(scr, NULL, False);
985 if (!menu)
986 wwarning(_("could not create workspace submenu for Clip menu"));
988 wMenuAddCallback(menu, "", switchWSCommand, (void *)scr->clip_icon);
990 menu->flags.realized = 0;
991 wMenuRealize(menu);
993 return menu;
996 static void updateClipOptionsMenu(WMenu *menu, WDock *dock)
998 WMenuEntry *entry;
999 int index = 0;
1001 if (!menu || !dock)
1002 return;
1004 /* keep on top */
1005 entry = menu->entries[index];
1006 entry->flags.indicator_on = !dock->lowered;
1007 entry->clientdata = dock;
1008 wMenuSetEnabled(menu, index, dock->type == WM_CLIP);
1010 /* collapsed */
1011 entry = menu->entries[++index];
1012 entry->flags.indicator_on = dock->collapsed;
1013 entry->clientdata = dock;
1015 /* auto-collapse */
1016 entry = menu->entries[++index];
1017 entry->flags.indicator_on = dock->auto_collapse;
1018 entry->clientdata = dock;
1020 /* auto-raise/lower */
1021 entry = menu->entries[++index];
1022 entry->flags.indicator_on = dock->auto_raise_lower;
1023 entry->clientdata = dock;
1024 wMenuSetEnabled(menu, index, dock->lowered && (dock->type == WM_CLIP));
1026 /* attract icons */
1027 entry = menu->entries[++index];
1028 entry->flags.indicator_on = dock->attract_icons;
1029 entry->clientdata = dock;
1031 menu->flags.realized = 0;
1032 wMenuRealize(menu);
1036 static WMenu *makeClipOptionsMenu(WScreen *scr)
1038 WMenu *menu;
1039 WMenuEntry *entry;
1041 menu = wMenuCreate(scr, NULL, False);
1042 if (!menu) {
1043 wwarning(_("could not create options submenu for Clip menu"));
1044 return NULL;
1047 entry = wMenuAddCallback(menu, _("Keep on Top"), toggleLoweredCallback, NULL);
1048 entry->flags.indicator = 1;
1049 entry->flags.indicator_on = 1;
1050 entry->flags.indicator_type = MI_CHECK;
1052 entry = wMenuAddCallback(menu, _("Collapsed"), toggleCollapsedCallback, NULL);
1053 entry->flags.indicator = 1;
1054 entry->flags.indicator_on = 1;
1055 entry->flags.indicator_type = MI_CHECK;
1057 entry = wMenuAddCallback(menu, _("Autocollapse"), toggleAutoCollapseCallback, NULL);
1058 entry->flags.indicator = 1;
1059 entry->flags.indicator_on = 1;
1060 entry->flags.indicator_type = MI_CHECK;
1062 entry = wMenuAddCallback(menu, _("Autoraise"), toggleAutoRaiseLowerCallback, NULL);
1063 entry->flags.indicator = 1;
1064 entry->flags.indicator_on = 1;
1065 entry->flags.indicator_type = MI_CHECK;
1067 entry = wMenuAddCallback(menu, _("Autoattract Icons"), toggleAutoAttractCallback, NULL);
1068 entry->flags.indicator = 1;
1069 entry->flags.indicator_on = 1;
1070 entry->flags.indicator_type = MI_CHECK;
1072 menu->flags.realized = 0;
1073 wMenuRealize(menu);
1075 return menu;
1079 static void setDockPositionNormalCallback(WMenu *menu, WMenuEntry *entry)
1081 WDock *dock = (WDock *) entry->clientdata;
1082 WDrawerChain *dc;
1084 /* Parameter not used, but tell the compiler that it is ok */
1085 (void) menu;
1087 if (entry->flags.indicator_on) // already set, nothing to do
1088 return;
1089 // Do we come from auto raise lower or keep on top?
1090 if (dock->auto_raise_lower)
1092 dock->auto_raise_lower = 0;
1093 // Only for aesthetic purposes, can be removed when Autoraise status is no longer exposed in drawer option menu
1094 for (dc = dock->screen_ptr->drawers; dc != NULL; dc = dc->next) {
1095 dc->adrawer->auto_raise_lower = 0;
1098 else
1100 // Will take care of setting lowered = 0 in drawers
1101 toggleLowered(dock);
1103 entry->flags.indicator_on = 1;
1106 static void setDockPositionAutoRaiseLowerCallback(WMenu *menu, WMenuEntry *entry)
1108 WDock *dock = (WDock *) entry->clientdata;
1109 WDrawerChain *dc;
1111 /* Parameter not used, but tell the compiler that it is ok */
1112 (void) menu;
1114 if (entry->flags.indicator_on) // already set, nothing to do
1115 return;
1116 // Do we come from normal or keep on top?
1117 if (!dock->lowered)
1119 toggleLowered(dock);
1121 dock->auto_raise_lower = 1;
1122 // Only for aesthetic purposes, can be removed when Autoraise status is no longer exposed in drawer option menu
1123 for (dc = dock->screen_ptr->drawers; dc != NULL; dc = dc->next) {
1124 dc->adrawer->auto_raise_lower = 1;
1126 entry->flags.indicator_on = 1;
1129 static void setDockPositionKeepOnTopCallback(WMenu *menu, WMenuEntry *entry)
1131 WDock *dock = (WDock *) entry->clientdata;
1132 WDrawerChain *dc;
1134 /* Parameter not used, but tell the compiler that it is ok */
1135 (void) menu;
1137 if (entry->flags.indicator_on) // already set, nothing to do
1138 return;
1139 dock->auto_raise_lower = 0;
1140 // Only for aesthetic purposes, can be removed when Autoraise status is no longer exposed in drawer option menu
1141 for (dc = dock->screen_ptr->drawers; dc != NULL; dc = dc->next) {
1142 dc->adrawer->auto_raise_lower = 0;
1144 toggleLowered(dock);
1145 entry->flags.indicator_on = 1;
1148 static void updateDockPositionMenu(WMenu *menu, WDock *dock)
1150 WMenuEntry *entry;
1151 int index = 0;
1153 assert(menu);
1154 assert(dock);
1156 /* Normal level */
1157 entry = menu->entries[index++];
1158 entry->flags.indicator_on = (dock->lowered && !dock->auto_raise_lower);
1159 entry->clientdata = dock;
1161 /* Auto-raise/lower */
1162 entry = menu->entries[index++];
1163 entry->flags.indicator_on = dock->auto_raise_lower;
1164 entry->clientdata = dock;
1166 /* Keep on top */
1167 entry = menu->entries[index++];
1168 entry->flags.indicator_on = !dock->lowered;
1169 entry->clientdata = dock;
1172 static WMenu *makeDockPositionMenu(WScreen *scr)
1174 /* When calling this, the dock is being created, so scr->dock is still not set
1175 * Therefore the callbacks' clientdata and the indicators can't be set,
1176 * they will be updated when the dock menu is opened. */
1177 WMenu *menu;
1178 WMenuEntry *entry;
1180 menu = wMenuCreate(scr, NULL, False);
1181 if (!menu) {
1182 wwarning(_("could not create options submenu for dock position menu"));
1183 return NULL;
1186 entry = wMenuAddCallback(menu, _("Normal"), setDockPositionNormalCallback, NULL);
1187 entry->flags.indicator = 1;
1188 entry->flags.indicator_type = MI_DIAMOND;
1190 entry = wMenuAddCallback(menu, _("Auto raise & lower"), setDockPositionAutoRaiseLowerCallback, NULL);
1191 entry->flags.indicator = 1;
1192 entry->flags.indicator_type = MI_DIAMOND;
1194 entry = wMenuAddCallback(menu, _("Keep on Top"), setDockPositionKeepOnTopCallback, NULL);
1195 entry->flags.indicator = 1;
1196 entry->flags.indicator_type = MI_DIAMOND;
1198 menu->flags.realized = 0;
1199 wMenuRealize(menu);
1201 return menu;
1205 static WMenu *dockMenuCreate(WScreen *scr, int type)
1207 WMenu *menu;
1208 WMenuEntry *entry;
1210 if (type == WM_CLIP && scr->clip_menu)
1211 return scr->clip_menu;
1213 if (type == WM_DRAWER && scr->drawer_menu)
1214 return scr->drawer_menu;
1216 menu = wMenuCreate(scr, NULL, False);
1217 if (type == WM_DOCK) {
1218 entry = wMenuAddCallback(menu, _("Dock position"), NULL, NULL);
1219 if (scr->dock_pos_menu == NULL)
1220 scr->dock_pos_menu = makeDockPositionMenu(scr);
1221 wMenuEntrySetCascade(menu, entry, scr->dock_pos_menu);
1223 if (!wPreferences.flags.nodrawer)
1224 wMenuAddCallback(menu, _("Add a drawer"), addADrawerCallback, NULL);
1226 } else {
1227 if (type == WM_CLIP)
1228 entry = wMenuAddCallback(menu, _("Clip Options"), NULL, NULL);
1229 else /* if (type == WM_DRAWER) */
1230 entry = wMenuAddCallback(menu, _("Drawer options"), NULL, NULL);
1232 if (scr->clip_options == NULL)
1233 scr->clip_options = makeClipOptionsMenu(scr);
1235 wMenuEntrySetCascade(menu, entry, scr->clip_options);
1237 /* The same menu is used for the dock and its appicons. If the menu
1238 * entry text is different between the two contexts, or if it can
1239 * change depending on some state, free the duplicated string (from
1240 * wMenuInsertCallback) and use gettext's string */
1241 if (type == WM_CLIP) {
1242 entry = wMenuAddCallback(menu, _("Rename Workspace"), renameCallback, NULL);
1243 wfree(entry->text);
1244 entry->text = _("Rename Workspace"); /* can be: (Toggle) Omnipresent */
1247 entry = wMenuAddCallback(menu, _("Selected"), selectCallback, NULL);
1248 entry->flags.indicator = 1;
1249 entry->flags.indicator_on = 1;
1250 entry->flags.indicator_type = MI_CHECK;
1252 entry = wMenuAddCallback(menu, _("Select All Icons"), selectIconsCallback, NULL);
1253 wfree(entry->text);
1254 entry->text = _("Select All Icons"); /* can be: Unselect all icons */
1256 entry = wMenuAddCallback(menu, _("Keep Icon"), keepIconsCallback, NULL);
1257 wfree(entry->text);
1258 entry->text = _("Keep Icon"); /* can be: Keep Icons */
1260 if (type == WM_CLIP) {
1261 entry = wMenuAddCallback(menu, _("Move Icon To"), NULL, NULL);
1262 wfree(entry->text);
1263 entry->text = _("Move Icon To"); /* can be: Move Icons to */
1264 scr->clip_submenu = makeWorkspaceMenu(scr);
1265 if (scr->clip_submenu)
1266 wMenuEntrySetCascade(menu, entry, scr->clip_submenu);
1269 entry = wMenuAddCallback(menu, _("Remove Icon"), removeIconsCallback, NULL);
1270 wfree(entry->text);
1271 entry->text = _("Remove Icon"); /* can be: Remove Icons */
1273 wMenuAddCallback(menu, _("Attract Icons"), attractIconsCallback, NULL);
1276 wMenuAddCallback(menu, _("Launch"), launchCallback, NULL);
1278 wMenuAddCallback(menu, _("Unhide Here"), unhideHereCallback, NULL);
1280 entry = wMenuAddCallback(menu, _("Hide"), hideCallback, NULL);
1281 wfree(entry->text);
1282 entry->text = _("Hide"); /* can be: Unhide */
1284 wMenuAddCallback(menu, _("Settings..."), settingsCallback, NULL);
1286 entry = wMenuAddCallback(menu, _("Kill"), killCallback, NULL);
1287 wfree(entry->text);
1288 entry->text = _("Kill"); /* can be: Remove drawer */
1290 if (type == WM_CLIP)
1291 scr->clip_menu = menu;
1293 if (type == WM_DRAWER)
1294 scr->drawer_menu = menu;
1296 return menu;
1299 WDock *wDockCreate(WScreen *scr, int type, const char *name)
1301 WDock *dock;
1302 WAppIcon *btn;
1304 make_keys();
1306 dock = wmalloc(sizeof(WDock));
1308 switch (type) {
1309 case WM_CLIP:
1310 dock->max_icons = DOCK_MAX_ICONS;
1311 break;
1312 case WM_DRAWER:
1313 dock->max_icons = scr->scr_width / wPreferences.icon_size;
1314 break;
1315 case WM_DOCK:
1316 default:
1317 dock->max_icons = scr->scr_height / wPreferences.icon_size;
1320 dock->icon_array = wmalloc(sizeof(WAppIcon *) * dock->max_icons);
1322 btn = mainIconCreate(scr, type, name);
1324 btn->dock = dock;
1326 dock->x_pos = btn->x_pos;
1327 dock->y_pos = btn->y_pos;
1328 dock->screen_ptr = scr;
1329 dock->type = type;
1330 dock->icon_count = 1;
1331 if (type == WM_DRAWER)
1332 dock->on_right_side = scr->dock->on_right_side;
1333 else
1334 dock->on_right_side = 1;
1335 dock->collapsed = 0;
1336 dock->auto_collapse = 0;
1337 dock->auto_collapse_magic = NULL;
1338 dock->auto_raise_lower = 0;
1339 dock->auto_lower_magic = NULL;
1340 dock->auto_raise_magic = NULL;
1341 dock->attract_icons = 0;
1342 dock->lowered = 1;
1343 dock->icon_array[0] = btn;
1344 wRaiseFrame(btn->icon->core);
1345 XMoveWindow(dpy, btn->icon->core->window, btn->x_pos, btn->y_pos);
1347 /* create dock menu */
1348 dock->menu = dockMenuCreate(scr, type);
1350 if (type == WM_DRAWER) {
1351 drawerAppendToChain(scr, dock);
1352 dock->auto_collapse = 1;
1355 return dock;
1358 void wDockDestroy(WDock *dock)
1360 int i;
1361 WAppIcon *aicon;
1363 for (i = (dock->type == WM_CLIP) ? 1 : 0; i < dock->max_icons; i++) {
1364 aicon = dock->icon_array[i];
1365 if (aicon) {
1366 int keepit = aicon->running && wApplicationOf(aicon->main_window);
1367 wDockDetach(dock, aicon);
1368 if (keepit) {
1369 /* XXX: can: aicon->icon == NULL ? */
1370 PlaceIcon(dock->screen_ptr, &aicon->x_pos, &aicon->y_pos,
1371 wGetHeadForWindow(aicon->icon->owner));
1372 XMoveWindow(dpy, aicon->icon->core->window, aicon->x_pos, aicon->y_pos);
1373 if (!dock->mapped || dock->collapsed)
1374 XMapWindow(dpy, aicon->icon->core->window);
1378 if (wPreferences.auto_arrange_icons)
1379 wArrangeIcons(dock->screen_ptr, True);
1380 wfree(dock->icon_array);
1381 if (dock->menu && dock->type != WM_CLIP)
1382 wMenuDestroy(dock->menu, True);
1383 if (dock->screen_ptr->last_dock == dock)
1384 dock->screen_ptr->last_dock = NULL;
1385 wfree(dock);
1388 void wClipIconPaint(WAppIcon *aicon)
1390 WScreen *scr = aicon->icon->core->screen_ptr;
1391 WWorkspace *workspace = scr->workspaces[scr->current_workspace];
1392 WMColor *color;
1393 Window win = aicon->icon->core->window;
1394 int length, nlength;
1395 char *ws_name, ws_number[10];
1396 int ty, tx;
1398 wIconPaint(aicon->icon);
1400 length = strlen(workspace->name);
1401 ws_name = wmalloc(length + 1);
1402 snprintf(ws_name, length + 1, "%s", workspace->name);
1403 snprintf(ws_number, sizeof(ws_number), "%i", scr->current_workspace + 1);
1404 nlength = strlen(ws_number);
1406 if (wPreferences.flags.noclip || !workspace->clip->collapsed)
1407 color = scr->clip_title_color[CLIP_NORMAL];
1408 else
1409 color = scr->clip_title_color[CLIP_COLLAPSED];
1411 ty = ICON_SIZE - WMFontHeight(scr->clip_title_font) - 3;
1413 tx = CLIP_BUTTON_SIZE * ICON_SIZE / 64;
1415 if(wPreferences.show_clip_title)
1416 WMDrawString(scr->wmscreen, win, color, scr->clip_title_font, tx, ty, ws_name, length);
1418 tx = (ICON_SIZE / 2 - WMWidthOfString(scr->clip_title_font, ws_number, nlength)) / 2;
1420 WMDrawString(scr->wmscreen, win, color, scr->clip_title_font, tx, 2, ws_number, nlength);
1422 wfree(ws_name);
1424 if (aicon->launching)
1425 XFillRectangle(dpy, aicon->icon->core->window, scr->stipple_gc,
1426 0, 0, wPreferences.icon_size, wPreferences.icon_size);
1428 paintClipButtons(aicon, aicon->dock->lclip_button_pushed, aicon->dock->rclip_button_pushed);
1431 static void clipIconExpose(WObjDescriptor *desc, XEvent *event)
1433 /* Parameter not used, but tell the compiler that it is ok */
1434 (void) desc;
1435 (void) event;
1437 wClipIconPaint(desc->parent);
1440 static void dockIconPaint(WAppIcon *btn)
1442 if (btn == btn->icon->core->screen_ptr->clip_icon) {
1443 wClipIconPaint(btn);
1444 } else if (wIsADrawer(btn)) {
1445 wDrawerIconPaint(btn);
1446 } else {
1447 wAppIconPaint(btn);
1448 save_appicon(btn, True);
1452 static WMPropList *make_icon_state(WAppIcon *btn)
1454 WMPropList *node = NULL;
1455 WMPropList *command, *autolaunch, *lock, *name, *forced;
1456 WMPropList *position, *buggy, *omnipresent;
1457 char *tmp;
1458 char buffer[64];
1460 if (btn) {
1461 if (!btn->command)
1462 command = WMCreatePLString("-");
1463 else
1464 command = WMCreatePLString(btn->command);
1466 autolaunch = btn->auto_launch ? dYes : dNo;
1468 lock = btn->lock ? dYes : dNo;
1470 tmp = EscapeWM_CLASS(btn->wm_instance, btn->wm_class);
1472 name = WMCreatePLString(tmp);
1474 wfree(tmp);
1476 forced = btn->forced_dock ? dYes : dNo;
1478 buggy = btn->buggy_app ? dYes : dNo;
1480 if (!wPreferences.flags.clip_merged_in_dock && btn == btn->icon->core->screen_ptr->clip_icon)
1481 snprintf(buffer, sizeof(buffer), "%i,%i", btn->x_pos, btn->y_pos);
1482 else
1483 snprintf(buffer, sizeof(buffer), "%hi,%hi", btn->xindex, btn->yindex);
1484 position = WMCreatePLString(buffer);
1486 node = WMCreatePLDictionary(dCommand, command,
1487 dName, name,
1488 dAutoLaunch, autolaunch,
1489 dLock, lock,
1490 dForced, forced, dBuggyApplication, buggy, dPosition, position, NULL);
1491 WMReleasePropList(command);
1492 WMReleasePropList(name);
1493 WMReleasePropList(position);
1495 omnipresent = btn->omnipresent ? dYes : dNo;
1496 if (btn->dock != btn->icon->core->screen_ptr->dock && (btn->xindex != 0 || btn->yindex != 0))
1497 WMPutInPLDictionary(node, dOmnipresent, omnipresent);
1499 #ifdef XDND /* was OFFIX */
1500 if (btn->dnd_command) {
1501 command = WMCreatePLString(btn->dnd_command);
1502 WMPutInPLDictionary(node, dDropCommand, command);
1503 WMReleasePropList(command);
1505 #endif /* XDND */
1507 if (btn->paste_command) {
1508 command = WMCreatePLString(btn->paste_command);
1509 WMPutInPLDictionary(node, dPasteCommand, command);
1510 WMReleasePropList(command);
1514 return node;
1517 static WMPropList *dockSaveState(WDock *dock)
1519 int i;
1520 WMPropList *icon_info;
1521 WMPropList *list = NULL, *dock_state = NULL;
1522 WMPropList *value, *key;
1523 char buffer[256];
1525 list = WMCreatePLArray(NULL);
1527 for (i = (dock->type == WM_DOCK ? 0 : 1); i < dock->max_icons; i++) {
1528 WAppIcon *btn = dock->icon_array[i];
1530 if (!btn || btn->attracted)
1531 continue;
1533 if ((icon_info = make_icon_state(dock->icon_array[i]))) {
1534 WMAddToPLArray(list, icon_info);
1535 WMReleasePropList(icon_info);
1539 dock_state = WMCreatePLDictionary(dApplications, list, NULL);
1541 if (dock->type == WM_DOCK) {
1542 snprintf(buffer, sizeof(buffer), "Applications%i", dock->screen_ptr->scr_height);
1543 key = WMCreatePLString(buffer);
1544 WMPutInPLDictionary(dock_state, key, list);
1545 WMReleasePropList(key);
1547 snprintf(buffer, sizeof(buffer), "%i,%i", (dock->on_right_side ? -ICON_SIZE : 0), dock->y_pos);
1548 value = WMCreatePLString(buffer);
1549 WMPutInPLDictionary(dock_state, dPosition, value);
1550 WMReleasePropList(value);
1552 WMReleasePropList(list);
1554 if (dock->type == WM_CLIP || dock->type == WM_DRAWER) {
1555 value = (dock->collapsed ? dYes : dNo);
1556 WMPutInPLDictionary(dock_state, dCollapsed, value);
1558 value = (dock->auto_collapse ? dYes : dNo);
1559 WMPutInPLDictionary(dock_state, dAutoCollapse, value);
1561 value = (dock->attract_icons ? dYes : dNo);
1562 WMPutInPLDictionary(dock_state, dAutoAttractIcons, value);
1565 if (dock->type == WM_DOCK || dock->type == WM_CLIP) {
1566 value = (dock->lowered ? dYes : dNo);
1567 WMPutInPLDictionary(dock_state, dLowered, value);
1569 value = (dock->auto_raise_lower ? dYes : dNo);
1570 WMPutInPLDictionary(dock_state, dAutoRaiseLower, value);
1573 return dock_state;
1576 void wDockSaveState(WScreen *scr, WMPropList *old_state)
1578 WMPropList *dock_state;
1579 WMPropList *keys;
1581 dock_state = dockSaveState(scr->dock);
1584 * Copy saved states of docks with different sizes.
1586 if (old_state) {
1587 int i;
1588 WMPropList *tmp;
1590 keys = WMGetPLDictionaryKeys(old_state);
1591 for (i = 0; i < WMGetPropListItemCount(keys); i++) {
1592 tmp = WMGetFromPLArray(keys, i);
1594 if (strncasecmp(WMGetFromPLString(tmp), "applications", 12) == 0
1595 && !WMGetFromPLDictionary(dock_state, tmp)) {
1597 WMPutInPLDictionary(dock_state, tmp, WMGetFromPLDictionary(old_state, tmp));
1600 WMReleasePropList(keys);
1603 WMPutInPLDictionary(scr->session_state, dDock, dock_state);
1605 WMReleasePropList(dock_state);
1608 void wClipSaveState(WScreen *scr)
1610 WMPropList *clip_state;
1612 clip_state = make_icon_state(scr->clip_icon);
1614 WMPutInPLDictionary(scr->session_state, dClip, clip_state);
1616 WMReleasePropList(clip_state);
1619 WMPropList *wClipSaveWorkspaceState(WScreen *scr, int workspace)
1621 return dockSaveState(scr->workspaces[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 command = wstrdup(WMGetFromPLString(cmd));
1660 if (strcmp(command, "-") == 0) {
1661 wfree(command);
1663 if (wclass)
1664 wfree(wclass);
1665 if (winstance)
1666 wfree(winstance);
1668 return NULL;
1671 aicon = wAppIconCreateForDock(scr, command, winstance, wclass, TILE_NORMAL);
1672 if (wclass)
1673 wfree(wclass);
1674 if (winstance)
1675 wfree(winstance);
1677 wfree(command);
1679 aicon->icon->core->descriptor.handle_mousedown = iconMouseDown;
1680 aicon->icon->core->descriptor.handle_enternotify = clipEnterNotify;
1681 aicon->icon->core->descriptor.handle_leavenotify = clipLeaveNotify;
1682 aicon->icon->core->descriptor.parent_type = WCLASS_DOCK_ICON;
1683 aicon->icon->core->descriptor.parent = aicon;
1685 #ifdef XDND /* was OFFIX */
1686 cmd = WMGetFromPLDictionary(info, dDropCommand);
1687 if (cmd)
1688 aicon->dnd_command = wstrdup(WMGetFromPLString(cmd));
1689 #endif
1691 cmd = WMGetFromPLDictionary(info, dPasteCommand);
1692 if (cmd)
1693 aicon->paste_command = wstrdup(WMGetFromPLString(cmd));
1695 /* check auto launch */
1696 value = WMGetFromPLDictionary(info, dAutoLaunch);
1698 aicon->auto_launch = getBooleanDockValue(value, dAutoLaunch);
1700 /* check lock */
1701 value = WMGetFromPLDictionary(info, dLock);
1703 aicon->lock = getBooleanDockValue(value, dLock);
1705 /* check if it wasn't normally docked */
1706 value = WMGetFromPLDictionary(info, dForced);
1708 aicon->forced_dock = getBooleanDockValue(value, dForced);
1710 /* check if we can rely on the stuff in the app */
1711 value = WMGetFromPLDictionary(info, dBuggyApplication);
1713 aicon->buggy_app = getBooleanDockValue(value, dBuggyApplication);
1715 /* get position in the dock */
1716 value = WMGetFromPLDictionary(info, dPosition);
1717 if (value && WMIsPLString(value)) {
1718 if (sscanf(WMGetFromPLString(value), "%hi,%hi", &aicon->xindex, &aicon->yindex) != 2)
1719 wwarning(_("bad value in docked icon state info %s"), WMGetFromPLString(dPosition));
1721 /* check position sanity */
1722 /* *Very* incomplete section! */
1723 if (type == WM_DOCK) {
1724 aicon->xindex = 0;
1726 } else {
1727 aicon->yindex = index;
1728 aicon->xindex = 0;
1731 /* check if icon is omnipresent */
1732 value = WMGetFromPLDictionary(info, dOmnipresent);
1734 aicon->omnipresent = getBooleanDockValue(value, dOmnipresent);
1736 aicon->running = 0;
1737 aicon->docked = 1;
1739 return aicon;
1742 #define COMPLAIN(key) wwarning(_("bad value in dock state info:%s"), key)
1744 WAppIcon *wClipRestoreState(WScreen *scr, WMPropList *clip_state)
1746 WAppIcon *icon;
1747 WMPropList *value;
1749 icon = mainIconCreate(scr, WM_CLIP, NULL);
1751 if (!clip_state)
1752 return icon;
1754 WMRetainPropList(clip_state);
1756 /* restore position */
1758 value = WMGetFromPLDictionary(clip_state, dPosition);
1760 if (value) {
1761 if (!WMIsPLString(value)) {
1762 COMPLAIN("Position");
1763 } else {
1764 if (sscanf(WMGetFromPLString(value), "%i,%i", &icon->x_pos, &icon->y_pos) != 2)
1765 COMPLAIN("Position");
1767 /* check position sanity */
1768 if (!onScreen(scr, icon->x_pos, icon->y_pos))
1769 wScreenKeepInside(scr, &icon->x_pos, &icon->y_pos, ICON_SIZE, ICON_SIZE);
1772 #ifdef XDND /* was OFFIX */
1773 value = WMGetFromPLDictionary(clip_state, dDropCommand);
1774 if (value && WMIsPLString(value))
1775 icon->dnd_command = wstrdup(WMGetFromPLString(value));
1776 #endif
1778 value = WMGetFromPLDictionary(clip_state, dPasteCommand);
1779 if (value && WMIsPLString(value))
1780 icon->paste_command = wstrdup(WMGetFromPLString(value));
1782 WMReleasePropList(clip_state);
1784 return icon;
1787 WDock *wDockRestoreState(WScreen *scr, WMPropList *dock_state, int type)
1789 WDock *dock;
1790 WMPropList *apps;
1791 WMPropList *value;
1792 WAppIcon *aicon, *old_top;
1793 int count, i;
1795 dock = wDockCreate(scr, type, NULL);
1797 if (!dock_state)
1798 return dock;
1800 WMRetainPropList(dock_state);
1802 /* restore position */
1803 value = WMGetFromPLDictionary(dock_state, dPosition);
1804 if (value) {
1805 if (!WMIsPLString(value)) {
1806 COMPLAIN("Position");
1807 } else {
1808 if (sscanf(WMGetFromPLString(value), "%i,%i", &dock->x_pos, &dock->y_pos) != 2)
1809 COMPLAIN("Position");
1811 /* check position sanity */
1812 if (!onScreen(scr, dock->x_pos, dock->y_pos)) {
1813 int x = dock->x_pos;
1814 wScreenKeepInside(scr, &x, &dock->y_pos, ICON_SIZE, ICON_SIZE);
1817 /* Is this needed any more? */
1818 if (type == WM_CLIP) {
1819 if (dock->x_pos < 0) {
1820 dock->x_pos = 0;
1821 } else if (dock->x_pos > scr->scr_width - ICON_SIZE) {
1822 dock->x_pos = scr->scr_width - ICON_SIZE;
1824 } else {
1825 if (dock->x_pos >= 0) {
1826 dock->x_pos = DOCK_EXTRA_SPACE;
1827 dock->on_right_side = 0;
1828 } else {
1829 dock->x_pos = scr->scr_width - DOCK_EXTRA_SPACE - ICON_SIZE;
1830 dock->on_right_side = 1;
1836 /* restore lowered/raised state */
1837 dock->lowered = 0;
1839 value = WMGetFromPLDictionary(dock_state, dLowered);
1840 if (value) {
1841 if (!WMIsPLString(value)) {
1842 COMPLAIN("Lowered");
1843 } else {
1844 if (strcasecmp(WMGetFromPLString(value), "YES") == 0)
1845 dock->lowered = 1;
1849 /* restore collapsed state */
1850 dock->collapsed = 0;
1852 value = WMGetFromPLDictionary(dock_state, dCollapsed);
1853 if (value) {
1854 if (!WMIsPLString(value)) {
1855 COMPLAIN("Collapsed");
1856 } else {
1857 if (strcasecmp(WMGetFromPLString(value), "YES") == 0)
1858 dock->collapsed = 1;
1862 /* restore auto-collapsed state */
1863 value = WMGetFromPLDictionary(dock_state, dAutoCollapse);
1864 if (value) {
1865 if (!WMIsPLString(value)) {
1866 COMPLAIN("AutoCollapse");
1867 } else {
1868 if (strcasecmp(WMGetFromPLString(value), "YES") == 0) {
1869 dock->auto_collapse = 1;
1870 dock->collapsed = 1;
1875 /* restore auto-raise/lower state */
1876 value = WMGetFromPLDictionary(dock_state, dAutoRaiseLower);
1877 if (value) {
1878 if (!WMIsPLString(value)) {
1879 COMPLAIN("AutoRaiseLower");
1880 } else {
1881 if (strcasecmp(WMGetFromPLString(value), "YES") == 0)
1882 dock->auto_raise_lower = 1;
1886 /* restore attract icons state */
1887 dock->attract_icons = 0;
1889 value = WMGetFromPLDictionary(dock_state, dAutoAttractIcons);
1890 if (value) {
1891 if (!WMIsPLString(value)) {
1892 COMPLAIN("AutoAttractIcons");
1893 } else {
1894 if (strcasecmp(WMGetFromPLString(value), "YES") == 0)
1895 dock->attract_icons = 1;
1899 /* application list */
1902 WMPropList *tmp;
1903 char buffer[64];
1906 * When saving, it saves the dock state in
1907 * Applications and Applicationsnnn
1909 * When loading, it will first try Applicationsnnn.
1910 * If it does not exist, use Applications as default.
1913 snprintf(buffer, sizeof(buffer), "Applications%i", scr->scr_height);
1915 tmp = WMCreatePLString(buffer);
1916 apps = WMGetFromPLDictionary(dock_state, tmp);
1917 WMReleasePropList(tmp);
1919 if (!apps)
1920 apps = WMGetFromPLDictionary(dock_state, dApplications);
1923 if (!apps)
1924 goto finish;
1926 count = WMGetPropListItemCount(apps);
1927 if (count == 0)
1928 goto finish;
1930 old_top = dock->icon_array[0];
1932 /* dock->icon_count is set to 1 when dock is created.
1933 * Since Clip is already restored, we want to keep it so for clip,
1934 * but for dock we may change the default top tile, so we set it to 0.
1936 if (type == WM_DOCK)
1937 dock->icon_count = 0;
1939 for (i = 0; i < count; i++) {
1940 if (dock->icon_count >= dock->max_icons) {
1941 wwarning(_("there are too many icons stored in dock. Ignoring what doesn't fit"));
1942 break;
1945 value = WMGetFromPLArray(apps, i);
1946 aicon = restore_icon_state(scr, value, type, dock->icon_count);
1948 dock->icon_array[dock->icon_count] = aicon;
1950 if (aicon) {
1951 aicon->dock = dock;
1952 aicon->x_pos = dock->x_pos + (aicon->xindex * ICON_SIZE);
1953 aicon->y_pos = dock->y_pos + (aicon->yindex * ICON_SIZE);
1955 if (dock->lowered)
1956 ChangeStackingLevel(aicon->icon->core, WMNormalLevel);
1957 else
1958 ChangeStackingLevel(aicon->icon->core, WMDockLevel);
1960 wCoreConfigure(aicon->icon->core, aicon->x_pos, aicon->y_pos, 0, 0);
1961 if (!dock->collapsed)
1962 XMapWindow(dpy, aicon->icon->core->window);
1964 wRaiseFrame(aicon->icon->core);
1966 dock->icon_count++;
1967 } else if (dock->icon_count == 0 && type == WM_DOCK) {
1968 dock->icon_count++;
1972 /* if the first icon is not defined, use the default */
1973 if (dock->icon_array[0] == NULL) {
1974 /* update default icon */
1975 old_top->x_pos = dock->x_pos;
1976 old_top->y_pos = dock->y_pos;
1977 if (dock->lowered)
1978 ChangeStackingLevel(old_top->icon->core, WMNormalLevel);
1979 else
1980 ChangeStackingLevel(old_top->icon->core, WMDockLevel);
1982 dock->icon_array[0] = old_top;
1983 XMoveWindow(dpy, old_top->icon->core->window, dock->x_pos, dock->y_pos);
1984 /* we don't need to increment dock->icon_count here because it was
1985 * incremented in the loop above.
1987 } else if (old_top != dock->icon_array[0]) {
1988 if (old_top == scr->clip_icon) // TODO dande: understand the logic
1989 scr->clip_icon = dock->icon_array[0];
1991 wAppIconDestroy(old_top);
1994 finish:
1995 WMReleasePropList(dock_state);
1997 return dock;
2000 void wDockLaunchWithState(WAppIcon *btn, WSavedState *state)
2002 if (btn && btn->command && !btn->running && !btn->launching) {
2003 btn->drop_launch = 0;
2004 btn->paste_launch = 0;
2006 btn->pid = execCommand(btn, btn->command, state);
2008 if (btn->pid > 0) {
2009 if (!btn->forced_dock && !btn->buggy_app) {
2010 btn->launching = 1;
2011 dockIconPaint(btn);
2014 } else {
2015 wfree(state);
2019 void wDockDoAutoLaunch(WDock *dock, int workspace)
2021 WAppIcon *btn;
2022 WSavedState *state;
2023 int i;
2025 for (i = 0; i < dock->max_icons; i++) {
2026 btn = dock->icon_array[i];
2027 if (!btn || !btn->auto_launch)
2028 continue;
2030 state = wmalloc(sizeof(WSavedState));
2031 state->workspace = workspace;
2032 /* TODO: this is klugy and is very difficult to understand
2033 * what's going on. Try to clean up */
2034 wDockLaunchWithState(btn, state);
2038 #ifdef XDND /* was OFFIX */
2039 static WDock *findDock(WScreen *scr, XEvent *event, int *icon_pos)
2041 WDock *dock;
2042 int i;
2044 *icon_pos = -1;
2045 if ((dock = scr->dock) != NULL) {
2046 for (i = 0; i < dock->max_icons; i++) {
2047 if (dock->icon_array[i]
2048 && dock->icon_array[i]->icon->core->window == event->xclient.window) {
2049 *icon_pos = i;
2050 break;
2054 if (*icon_pos < 0 && (dock = scr->workspaces[scr->current_workspace]->clip) != NULL) {
2055 for (i = 0; i < dock->max_icons; i++) {
2056 if (dock->icon_array[i]
2057 && dock->icon_array[i]->icon->core->window == event->xclient.window) {
2058 *icon_pos = i;
2059 break;
2063 if (*icon_pos >= 0)
2064 return dock;
2065 return NULL;
2068 int wDockReceiveDNDDrop(WScreen *scr, XEvent *event)
2070 WDock *dock;
2071 WAppIcon *btn;
2072 int icon_pos;
2074 dock = findDock(scr, event, &icon_pos);
2075 if (!dock)
2076 return False;
2079 * Return True if the drop was on an application icon window.
2080 * In this case, let the ClientMessage handler redirect the
2081 * message to the app.
2083 if (dock->icon_array[icon_pos]->icon->icon_win != None)
2084 return True;
2086 if (dock->icon_array[icon_pos]->dnd_command != NULL) {
2087 scr->flags.dnd_data_convertion_status = 0;
2089 btn = dock->icon_array[icon_pos];
2091 if (!btn->forced_dock) {
2092 btn->relaunching = btn->running;
2093 btn->running = 1;
2095 if (btn->wm_instance || btn->wm_class) {
2096 WWindowAttributes attr;
2097 memset(&attr, 0, sizeof(WWindowAttributes));
2098 wDefaultFillAttributes(btn->wm_instance, btn->wm_class, &attr, NULL, True);
2100 if (!attr.no_appicon)
2101 btn->launching = 1;
2102 else
2103 btn->running = 0;
2106 btn->paste_launch = 0;
2107 btn->drop_launch = 1;
2108 scr->last_dock = dock;
2109 btn->pid = execCommand(btn, btn->dnd_command, NULL);
2110 if (btn->pid > 0) {
2111 dockIconPaint(btn);
2112 } else {
2113 btn->launching = 0;
2114 if (!btn->relaunching)
2115 btn->running = 0;
2118 return False;
2120 #endif /* XDND */
2122 Bool wDockAttachIcon(WDock *dock, WAppIcon *icon, int x, int y, Bool update_icon)
2124 WWindow *wwin;
2125 Bool lupdate_icon = False;
2126 char *command = NULL;
2127 int index;
2129 icon->editing = 0;
2131 if (update_icon)
2132 lupdate_icon = True;
2134 if (icon->command == NULL) {
2135 /* If icon->owner exists, it means the application is running */
2136 if (icon->icon->owner) {
2137 wwin = icon->icon->owner;
2138 command = GetCommandForWindow(wwin->client_win);
2141 if (command) {
2142 icon->command = command;
2143 } else {
2144 /* icon->forced_dock = 1; */
2145 if (dock->type != WM_CLIP || !icon->attracted) {
2146 icon->editing = 1;
2147 if (wInputDialog(dock->screen_ptr, _("Dock Icon"),
2148 _("Type the command used to launch the application"), &command)) {
2149 if (command && (command[0] == 0 || (command[0] == '-' && command[1] == 0))) {
2150 wfree(command);
2151 command = NULL;
2153 icon->command = command;
2154 icon->editing = 0;
2155 } else {
2156 icon->editing = 0;
2157 if (command)
2158 wfree(command);
2159 /* If the target is the dock, reject the icon. If
2160 * the target is the clip, make it an attracted icon
2162 if (dock->type == WM_CLIP) {
2163 icon->attracted = 1;
2164 if (!icon->icon->shadowed) {
2165 icon->icon->shadowed = 1;
2166 lupdate_icon = True;
2168 } else {
2169 return False;
2176 for (index = 1; index < dock->max_icons; index++)
2177 if (dock->icon_array[index] == NULL)
2178 break;
2179 /* if (index == dock->max_icons)
2180 return; */
2182 assert(index < dock->max_icons);
2184 dock->icon_array[index] = icon;
2185 icon->yindex = y;
2186 icon->xindex = x;
2188 icon->omnipresent = 0;
2190 icon->x_pos = dock->x_pos + x * ICON_SIZE;
2191 icon->y_pos = dock->y_pos + y * ICON_SIZE;
2193 dock->icon_count++;
2195 icon->running = 1;
2196 icon->launching = 0;
2197 icon->docked = 1;
2198 icon->dock = dock;
2199 icon->icon->core->descriptor.handle_mousedown = iconMouseDown;
2200 icon->icon->core->descriptor.handle_enternotify = clipEnterNotify;
2201 icon->icon->core->descriptor.handle_leavenotify = clipLeaveNotify;
2202 icon->icon->core->descriptor.parent_type = WCLASS_DOCK_ICON;
2203 icon->icon->core->descriptor.parent = icon;
2205 MoveInStackListUnder(dock->icon_array[index - 1]->icon->core, icon->icon->core);
2206 wAppIconMove(icon, icon->x_pos, icon->y_pos);
2209 * Update icon pixmap, RImage doesn't change,
2210 * so call wIconUpdate is not needed
2212 if (lupdate_icon)
2213 update_icon_pixmap(icon->icon);
2215 /* Paint it */
2216 wAppIconPaint(icon);
2218 /* Save it */
2219 save_appicon(icon, True);
2221 if (wPreferences.auto_arrange_icons)
2222 wArrangeIcons(dock->screen_ptr, True);
2224 #ifdef XDND /* was OFFIX */
2225 if (icon->command && !icon->dnd_command) {
2226 int len = strlen(icon->command) + 8;
2227 icon->dnd_command = wmalloc(len);
2228 snprintf(icon->dnd_command, len, "%s %%d", icon->command);
2230 #endif
2232 if (icon->command && !icon->paste_command) {
2233 int len = strlen(icon->command) + 8;
2234 icon->paste_command = wmalloc(len);
2235 snprintf(icon->paste_command, len, "%s %%s", icon->command);
2238 return True;
2241 void wDockReattachIcon(WDock *dock, WAppIcon *icon, int x, int y)
2243 int index;
2245 for (index = 1; index < dock->max_icons; index++) {
2246 if (dock->icon_array[index] == icon)
2247 break;
2249 assert(index < dock->max_icons);
2251 icon->yindex = y;
2252 icon->xindex = x;
2254 icon->x_pos = dock->x_pos + x * ICON_SIZE;
2255 icon->y_pos = dock->y_pos + y * ICON_SIZE;
2258 Bool wDockMoveIconBetweenDocks(WDock *src, WDock *dest, WAppIcon *icon, int x, int y)
2260 WWindow *wwin;
2261 char *command = NULL;
2262 int index;
2263 Bool update_icon = False;
2265 if (src == dest)
2266 return True; /* No move needed, we're already there */
2268 if (dest == NULL)
2269 return False;
2272 * For the moment we can't do this if we move icons in Clip from one
2273 * workspace to other, because if we move two or more icons without
2274 * command, the dialog box will not be able to tell us to which of the
2275 * moved icons it applies. -Dan
2277 if ((dest->type == WM_DOCK /*|| dest->keep_attracted */ ) && icon->command == NULL) {
2278 /* If icon->owner exists, it means the application is running */
2279 if (icon->icon->owner) {
2280 wwin = icon->icon->owner;
2281 command = GetCommandForWindow(wwin->client_win);
2284 if (command) {
2285 icon->command = command;
2286 } else {
2287 icon->editing = 1;
2288 /* icon->forced_dock = 1; */
2289 if (wInputDialog(src->screen_ptr, _("Dock Icon"),
2290 _("Type the command used to launch the application"), &command)) {
2291 if (command && (command[0] == 0 || (command[0] == '-' && command[1] == 0))) {
2292 wfree(command);
2293 command = NULL;
2295 icon->command = command;
2296 } else {
2297 icon->editing = 0;
2298 if (command)
2299 wfree(command);
2300 return False;
2302 icon->editing = 0;
2306 if (dest->type == WM_DOCK || dest->type == WM_DRAWER)
2307 wClipMakeIconOmnipresent(icon, False);
2309 for (index = 1; index < src->max_icons; index++) {
2310 if (src->icon_array[index] == icon)
2311 break;
2313 assert(index < src->max_icons);
2315 src->icon_array[index] = NULL;
2316 src->icon_count--;
2318 for (index = 1; index < dest->max_icons; index++) {
2319 if (dest->icon_array[index] == NULL)
2320 break;
2323 assert(index < dest->max_icons);
2325 dest->icon_array[index] = icon;
2326 icon->dock = dest;
2328 /* deselect the icon */
2329 if (icon->icon->selected)
2330 wIconSelect(icon->icon);
2332 icon->icon->core->descriptor.handle_enternotify = clipEnterNotify;
2333 icon->icon->core->descriptor.handle_leavenotify = clipLeaveNotify;
2335 /* set it to be kept when moving to dock.
2336 * Unless the icon does not have a command set
2338 if (icon->command && (dest->type == WM_DOCK || dest->type == WM_DRAWER)) {
2339 icon->attracted = 0;
2340 if (icon->icon->shadowed) {
2341 icon->icon->shadowed = 0;
2342 update_icon = True;
2344 save_appicon(icon, True);
2347 if (src->auto_collapse || src->auto_raise_lower)
2348 clipLeave(src);
2350 icon->yindex = y;
2351 icon->xindex = x;
2353 icon->x_pos = dest->x_pos + x * ICON_SIZE;
2354 icon->y_pos = dest->y_pos + y * ICON_SIZE;
2356 dest->icon_count++;
2358 MoveInStackListUnder(dest->icon_array[index - 1]->icon->core, icon->icon->core);
2361 * Update icon pixmap, RImage doesn't change,
2362 * so call wIconUpdate is not needed
2364 if (update_icon)
2365 update_icon_pixmap(icon->icon);
2367 /* Paint it */
2368 wAppIconPaint(icon);
2370 return True;
2373 void wDockDetach(WDock *dock, WAppIcon *icon)
2375 int index;
2376 Bool update_icon = False;
2378 /* make the settings panel be closed */
2379 if (icon->panel)
2380 DestroyDockAppSettingsPanel(icon->panel);
2382 /* This must be called before icon->dock is set to NULL.
2383 * Don't move it. -Dan
2385 wClipMakeIconOmnipresent(icon, False);
2387 icon->docked = 0;
2388 icon->dock = NULL;
2389 icon->attracted = 0;
2390 icon->auto_launch = 0;
2391 if (icon->icon->shadowed) {
2392 icon->icon->shadowed = 0;
2393 update_icon = True;
2396 /* deselect the icon */
2397 if (icon->icon->selected)
2398 wIconSelect(icon->icon);
2400 if (icon->command) {
2401 wfree(icon->command);
2402 icon->command = NULL;
2404 #ifdef XDND /* was OFFIX */
2405 if (icon->dnd_command) {
2406 wfree(icon->dnd_command);
2407 icon->dnd_command = NULL;
2409 #endif
2410 if (icon->paste_command) {
2411 wfree(icon->paste_command);
2412 icon->paste_command = NULL;
2415 for (index = 1; index < dock->max_icons; index++)
2416 if (dock->icon_array[index] == icon)
2417 break;
2419 assert(index < dock->max_icons);
2420 dock->icon_array[index] = NULL;
2421 icon->yindex = -1;
2422 icon->xindex = -1;
2424 dock->icon_count--;
2426 /* if the dock is not attached to an application or
2427 * the application did not set the appropriate hints yet,
2428 * destroy the icon */
2429 if (!icon->running || !wApplicationOf(icon->main_window)) {
2430 wAppIconDestroy(icon);
2431 } else {
2432 icon->icon->core->descriptor.handle_mousedown = appIconMouseDown;
2433 icon->icon->core->descriptor.handle_enternotify = NULL;
2434 icon->icon->core->descriptor.handle_leavenotify = NULL;
2435 icon->icon->core->descriptor.parent_type = WCLASS_APPICON;
2436 icon->icon->core->descriptor.parent = icon;
2438 ChangeStackingLevel(icon->icon->core, NORMAL_ICON_LEVEL);
2441 * Update icon pixmap, RImage doesn't change,
2442 * so call wIconUpdate is not needed
2444 if (update_icon)
2445 update_icon_pixmap(icon->icon);
2447 /* Paint it */
2448 wAppIconPaint(icon);
2450 if (wPreferences.auto_arrange_icons)
2451 wArrangeIcons(dock->screen_ptr, True);
2453 if (dock->auto_collapse || dock->auto_raise_lower)
2454 clipLeave(dock);
2458 * returns the closest Dock slot index for the passed
2459 * coordinates.
2461 * Returns False if icon can't be docked.
2463 * Note: this function should NEVER alter ret_x or ret_y, unless it will
2464 * return True. -Dan
2466 /* Redocking == true means either icon->dock == dock (normal case)
2467 * or we are called from handleDockMove for a drawer */
2468 Bool wDockSnapIcon(WDock *dock, WAppIcon *icon, int req_x, int req_y, int *ret_x, int *ret_y, int redocking)
2470 WScreen *scr = dock->screen_ptr;
2471 int dx, dy;
2472 int ex_x, ex_y;
2473 int i, offset = ICON_SIZE / 2;
2474 WAppIcon *aicon = NULL;
2475 WAppIcon *nicon = NULL;
2477 if (wPreferences.flags.noupdates)
2478 return False;
2480 dx = dock->x_pos;
2481 dy = dock->y_pos;
2483 /* if the dock is full */
2484 if (!redocking && (dock->icon_count >= dock->max_icons))
2485 return False;
2487 /* exact position */
2488 if (req_y < dy)
2489 ex_y = (req_y - offset - dy) / ICON_SIZE;
2490 else
2491 ex_y = (req_y + offset - dy) / ICON_SIZE;
2493 if (req_x < dx)
2494 ex_x = (req_x - offset - dx) / ICON_SIZE;
2495 else
2496 ex_x = (req_x + offset - dx) / ICON_SIZE;
2498 /* check if the icon is outside the screen boundaries */
2499 if (!onScreen(scr, dx + ex_x * ICON_SIZE, dy + ex_y * ICON_SIZE))
2500 return False;
2502 switch (dock->type) {
2503 case WM_DOCK:
2504 /* We can return False right away if
2505 * - we do not come from this dock (which is a WM_DOCK),
2506 * - we are not right over it, and
2507 * - we are not the main tile of a drawer.
2508 * In the latter case, we are called from handleDockMove. */
2509 if (icon->dock != dock && ex_x != 0 &&
2510 !(icon->dock && icon->dock->type == WM_DRAWER && icon == icon->dock->icon_array[0]))
2511 return False;
2513 if (!redocking && ex_x != 0)
2514 return False;
2516 if (getDrawer(scr, ex_y)) /* Return false so that the drawer gets it. */
2517 return False;
2519 aicon = NULL;
2520 for (i = 0; i < dock->max_icons; i++) {
2521 nicon = dock->icon_array[i];
2522 if (nicon && nicon->yindex == ex_y) {
2523 aicon = nicon;
2524 break;
2528 if (redocking) {
2529 int sig, done, closest;
2531 /* Possible cases when redocking:
2533 * icon dragged out of range of any slot -> false
2534 * icon dragged on a drawer -> false (to open the drawer)
2535 * icon dragged to range of free slot
2536 * icon dragged to range of same slot
2537 * icon dragged to range of different icon
2539 if (abs(ex_x) > DOCK_DETTACH_THRESHOLD)
2540 return False;
2542 if (aicon == icon || !aicon) {
2543 *ret_x = 0;
2544 *ret_y = ex_y;
2545 return True;
2548 /* start looking at the upper slot or lower? */
2549 if (ex_y * ICON_SIZE < (req_y + offset - dy))
2550 sig = 1;
2551 else
2552 sig = -1;
2554 done = 0;
2555 /* look for closest free slot */
2556 for (i = 0; i < (DOCK_DETTACH_THRESHOLD + 1) * 2 && !done; i++) {
2557 int j;
2559 done = 1;
2560 closest = sig * (i / 2) + ex_y;
2561 /* check if this slot is fully on the screen and not used */
2562 if (onScreen(scr, dx, dy + closest * ICON_SIZE)) {
2563 for (j = 0; j < dock->max_icons; j++) {
2564 if (dock->icon_array[j]
2565 && dock->icon_array[j]->yindex == closest) {
2566 /* slot is used by someone else */
2567 if (dock->icon_array[j] != icon)
2568 done = 0;
2569 break;
2572 /* slot is used by a drawer */
2573 done = done && !getDrawer(scr, closest);
2575 else // !onScreen
2576 done = 0;
2577 sig = -sig;
2579 if (done &&
2580 ((ex_y >= closest && ex_y - closest < DOCK_DETTACH_THRESHOLD + 1)
2581 || (ex_y < closest && closest - ex_y <= DOCK_DETTACH_THRESHOLD + 1))) {
2582 *ret_x = 0;
2583 *ret_y = closest;
2584 return True;
2586 } else { /* !redocking */
2588 /* if slot is free and the icon is close enough, return it */
2589 if (!aicon && ex_x == 0) {
2590 *ret_x = 0;
2591 *ret_y = ex_y;
2592 return True;
2595 break;
2596 case WM_CLIP:
2598 int neighbours = 0;
2599 int start, stop, k;
2601 start = icon->omnipresent ? 0 : scr->current_workspace;
2602 stop = icon->omnipresent ? scr->workspace_count : start + 1;
2604 aicon = NULL;
2605 for (k = start; k < stop; k++) {
2606 WDock *tmp = scr->workspaces[k]->clip;
2607 if (!tmp)
2608 continue;
2609 for (i = 0; i < tmp->max_icons; i++) {
2610 nicon = tmp->icon_array[i];
2611 if (nicon && nicon->xindex == ex_x && nicon->yindex == ex_y) {
2612 aicon = nicon;
2613 break;
2616 if (aicon)
2617 break;
2619 for (k = start; k < stop; k++) {
2620 WDock *tmp = scr->workspaces[k]->clip;
2621 if (!tmp)
2622 continue;
2623 for (i = 0; i < tmp->max_icons; i++) {
2624 nicon = tmp->icon_array[i];
2625 if (nicon && nicon != icon && /* Icon can't be it's own neighbour */
2626 (abs(nicon->xindex - ex_x) <= CLIP_ATTACH_VICINITY &&
2627 abs(nicon->yindex - ex_y) <= CLIP_ATTACH_VICINITY)) {
2628 neighbours = 1;
2629 break;
2632 if (neighbours)
2633 break;
2636 if (neighbours && (aicon == NULL || (redocking && aicon == icon))) {
2637 *ret_x = ex_x;
2638 *ret_y = ex_y;
2639 return True;
2641 break;
2643 case WM_DRAWER:
2645 WAppIcon *aicons_to_shift[ dock->icon_count ];
2646 int index_of_hole, j;
2648 if (ex_y != 0 ||
2649 abs(ex_x) - dock->icon_count > DOCK_DETTACH_THRESHOLD ||
2650 (ex_x < 0 && !dock->on_right_side) ||
2651 (ex_x > 0 && dock->on_right_side)) {
2652 return False;
2655 if (ex_x == 0)
2656 ex_x = (dock->on_right_side ? -1 : 1);
2658 /* "Reduce" ex_x but keep its sign */
2659 if (redocking) {
2660 if (abs(ex_x) > dock->icon_count - 1) /* minus 1: do not take icon_array[0] into account */
2661 ex_x = ex_x * (dock->icon_count - 1) / abs(ex_x); /* don't use *= ! */
2662 } else {
2663 if (abs(ex_x) > dock->icon_count)
2664 ex_x = ex_x * dock->icon_count / abs(ex_x);
2666 index_of_hole = indexOfHole(dock, icon, redocking);
2668 /* Find the appicons between where icon was (index_of_hole) and where
2669 * it wants to be (ex_x) and slide them. */
2670 j = 0;
2671 for (i = 1; i < dock->max_icons; i++) {
2672 if ((aicon = dock->icon_array[ i ]) && aicon != icon &&
2673 ((ex_x <= aicon->xindex && aicon->xindex < index_of_hole) ||
2674 (index_of_hole < aicon->xindex && aicon->xindex <= ex_x)))
2675 aicons_to_shift[ j++ ] = aicon;
2677 assert(j == abs(ex_x - index_of_hole));
2679 wSlideAppicons(aicons_to_shift, j, (index_of_hole < ex_x));
2681 *ret_x = ex_x;
2682 *ret_y = ex_y;
2683 return True;
2686 return False;
2689 static int onScreen(WScreen *scr, int x, int y)
2691 WMRect rect;
2692 int flags;
2694 rect.pos.x = x;
2695 rect.pos.y = y;
2696 rect.size.width = rect.size.height = ICON_SIZE;
2698 wGetRectPlacementInfo(scr, rect, &flags);
2700 return !(flags & (XFLAG_DEAD | XFLAG_PARTIAL));
2704 * returns true if it can find a free slot in the dock,
2705 * in which case it changes x_pos and y_pos accordingly.
2706 * Else returns false.
2708 Bool wDockFindFreeSlot(WDock *dock, int *x_pos, int *y_pos)
2710 WScreen *scr = dock->screen_ptr;
2711 WAppIcon *btn;
2712 WAppIconChain *chain;
2713 unsigned char *slot_map;
2714 int mwidth;
2715 int r;
2716 int x, y;
2717 int i, done = False;
2718 int corner;
2719 int ex = scr->scr_width, ey = scr->scr_height;
2720 int extra_count = 0;
2722 if (dock->type == WM_DRAWER) {
2723 if (dock->icon_count >= dock->max_icons) { /* drawer is full */
2724 return False;
2726 *x_pos = dock->icon_count * (dock->on_right_side ? -1 : 1);
2727 *y_pos = 0;
2728 return True;
2731 if (dock->type == WM_CLIP && dock != scr->workspaces[scr->current_workspace]->clip)
2732 extra_count = scr->global_icon_count;
2734 /* if the dock is full */
2735 if (dock->icon_count + extra_count >= dock->max_icons)
2736 return False;
2738 if (!wPreferences.flags.nodock && scr->dock && scr->dock->on_right_side) {
2739 ex -= ICON_SIZE + DOCK_EXTRA_SPACE;
2742 if (ex < dock->x_pos)
2743 ex = dock->x_pos;
2744 #define C_NONE 0
2745 #define C_NW 1
2746 #define C_NE 2
2747 #define C_SW 3
2748 #define C_SE 4
2750 /* check if clip is in a corner */
2751 if (dock->type == WM_CLIP) {
2752 if (dock->x_pos < 1 && dock->y_pos < 1)
2753 corner = C_NE;
2754 else if (dock->x_pos < 1 && dock->y_pos >= (ey - ICON_SIZE))
2755 corner = C_SE;
2756 else if (dock->x_pos >= (ex - ICON_SIZE) && dock->y_pos >= (ey - ICON_SIZE))
2757 corner = C_SW;
2758 else if (dock->x_pos >= (ex - ICON_SIZE) && dock->y_pos < 1)
2759 corner = C_NW;
2760 else
2761 corner = C_NONE;
2762 } else {
2763 corner = C_NONE;
2766 /* If the clip is in the corner, use only slots that are in the border
2767 * of the screen */
2768 if (corner != C_NONE) {
2769 char *hmap, *vmap;
2770 int hcount, vcount;
2772 hcount = WMIN(dock->max_icons, scr->scr_width / ICON_SIZE);
2773 vcount = WMIN(dock->max_icons, scr->scr_height / ICON_SIZE);
2774 hmap = wmalloc(hcount + 1);
2775 vmap = wmalloc(vcount + 1);
2777 /* mark used positions */
2778 switch (corner) {
2779 case C_NE:
2780 for (i = 0; i < dock->max_icons; i++) {
2781 btn = dock->icon_array[i];
2782 if (!btn)
2783 continue;
2785 if (btn->xindex == 0 && btn->yindex > 0 && btn->yindex < vcount)
2786 vmap[btn->yindex] = 1;
2787 else if (btn->yindex == 0 && btn->xindex > 0 && btn->xindex < hcount)
2788 hmap[btn->xindex] = 1;
2790 for (chain = scr->global_icons; chain != NULL; chain = chain->next) {
2791 btn = chain->aicon;
2792 if (btn->xindex == 0 && btn->yindex > 0 && btn->yindex < vcount)
2793 vmap[btn->yindex] = 1;
2794 else if (btn->yindex == 0 && btn->xindex > 0 && btn->xindex < hcount)
2795 hmap[btn->xindex] = 1;
2797 break;
2798 case C_NW:
2799 for (i = 0; i < dock->max_icons; i++) {
2800 btn = dock->icon_array[i];
2801 if (!btn)
2802 continue;
2804 if (btn->xindex == 0 && btn->yindex > 0 && btn->yindex < vcount)
2805 vmap[btn->yindex] = 1;
2806 else if (btn->yindex == 0 && btn->xindex < 0 && btn->xindex > -hcount)
2807 hmap[-btn->xindex] = 1;
2809 for (chain = scr->global_icons; chain != NULL; chain = chain->next) {
2810 btn = chain->aicon;
2811 if (btn->xindex == 0 && btn->yindex > 0 && btn->yindex < vcount)
2812 vmap[btn->yindex] = 1;
2813 else if (btn->yindex == 0 && btn->xindex < 0 && btn->xindex > -hcount)
2814 hmap[-btn->xindex] = 1;
2816 break;
2817 case C_SE:
2818 for (i = 0; i < dock->max_icons; i++) {
2819 btn = dock->icon_array[i];
2820 if (!btn)
2821 continue;
2823 if (btn->xindex == 0 && btn->yindex < 0 && btn->yindex > -vcount)
2824 vmap[-btn->yindex] = 1;
2825 else if (btn->yindex == 0 && btn->xindex > 0 && btn->xindex < hcount)
2826 hmap[btn->xindex] = 1;
2828 for (chain = scr->global_icons; chain != NULL; chain = chain->next) {
2829 btn = chain->aicon;
2830 if (btn->xindex == 0 && btn->yindex < 0 && btn->yindex > -vcount)
2831 vmap[-btn->yindex] = 1;
2832 else if (btn->yindex == 0 && btn->xindex > 0 && btn->xindex < hcount)
2833 hmap[btn->xindex] = 1;
2835 break;
2836 case C_SW:
2837 default:
2838 for (i = 0; i < dock->max_icons; i++) {
2839 btn = dock->icon_array[i];
2840 if (!btn)
2841 continue;
2843 if (btn->xindex == 0 && btn->yindex < 0 && btn->yindex > -vcount)
2844 vmap[-btn->yindex] = 1;
2845 else if (btn->yindex == 0 && btn->xindex < 0 && btn->xindex > -hcount)
2846 hmap[-btn->xindex] = 1;
2848 for (chain = scr->global_icons; chain != NULL; chain = chain->next) {
2849 btn = chain->aicon;
2850 if (btn->xindex == 0 && btn->yindex < 0 && btn->yindex > -vcount)
2851 vmap[-btn->yindex] = 1;
2852 else if (btn->yindex == 0 && btn->xindex < 0 && btn->xindex > -hcount)
2853 hmap[-btn->xindex] = 1;
2856 x = 0;
2857 y = 0;
2858 done = 0;
2859 /* search a vacant slot */
2860 for (i = 1; i < WMAX(vcount, hcount); i++) {
2861 if (i < vcount && vmap[i] == 0) {
2862 /* found a slot */
2863 x = 0;
2864 y = i;
2865 done = 1;
2866 break;
2867 } else if (i < hcount && hmap[i] == 0) {
2868 /* found a slot */
2869 x = i;
2870 y = 0;
2871 done = 1;
2872 break;
2875 wfree(vmap);
2876 wfree(hmap);
2877 /* If found a slot, translate and return */
2878 if (done) {
2879 if (corner == C_NW || corner == C_NE)
2880 *y_pos = y;
2881 else
2882 *y_pos = -y;
2884 if (corner == C_NE || corner == C_SE)
2885 *x_pos = x;
2886 else
2887 *x_pos = -x;
2889 return True;
2891 /* else, try to find a slot somewhere else */
2894 /* a map of mwidth x mwidth would be enough if we allowed icons to be
2895 * placed outside of screen */
2896 mwidth = (int)ceil(sqrt(dock->max_icons));
2898 /* In the worst case (the clip is in the corner of the screen),
2899 * the amount of icons that fit in the clip is smaller.
2900 * Double the map to get a safe value.
2902 mwidth += mwidth;
2904 r = (mwidth - 1) / 2;
2906 slot_map = wmalloc(mwidth * mwidth);
2908 #define XY2OFS(x,y) (WMAX(abs(x),abs(y)) > r) ? 0 : (((y)+r)*(mwidth)+(x)+r)
2910 /* mark used slots in the map. If the slot falls outside the map
2911 * (for example, when all icons are placed in line), ignore them. */
2912 for (i = 0; i < dock->max_icons; i++) {
2913 btn = dock->icon_array[i];
2914 if (btn)
2915 slot_map[XY2OFS(btn->xindex, btn->yindex)] = 1;
2918 for (chain = scr->global_icons; chain != NULL; chain = chain->next)
2919 slot_map[XY2OFS(chain->aicon->xindex, chain->aicon->yindex)] = 1;
2921 /* Find closest slot from the center that is free by scanning the
2922 * map from the center to outward in circular passes.
2923 * This will not result in a neat layout, but will be optimal
2924 * in the sense that there will not be holes left.
2926 done = 0;
2927 for (i = 1; i <= r && !done; i++) {
2928 int tx, ty;
2930 /* top and bottom parts of the ring */
2931 for (x = -i; x <= i && !done; x++) {
2932 tx = dock->x_pos + x * ICON_SIZE;
2933 y = -i;
2934 ty = dock->y_pos + y * ICON_SIZE;
2935 if (slot_map[XY2OFS(x, y)] == 0 && onScreen(scr, tx, ty)) {
2936 *x_pos = x;
2937 *y_pos = y;
2938 done = 1;
2939 break;
2941 y = i;
2942 ty = dock->y_pos + y * ICON_SIZE;
2943 if (slot_map[XY2OFS(x, y)] == 0 && onScreen(scr, tx, ty)) {
2944 *x_pos = x;
2945 *y_pos = y;
2946 done = 1;
2947 break;
2950 /* left and right parts of the ring */
2951 for (y = -i + 1; y <= i - 1; y++) {
2952 ty = dock->y_pos + y * ICON_SIZE;
2953 x = -i;
2954 tx = dock->x_pos + x * ICON_SIZE;
2955 if (slot_map[XY2OFS(x, y)] == 0 && onScreen(scr, tx, ty)) {
2956 *x_pos = x;
2957 *y_pos = y;
2958 done = 1;
2959 break;
2961 x = i;
2962 tx = dock->x_pos + x * ICON_SIZE;
2963 if (slot_map[XY2OFS(x, y)] == 0 && onScreen(scr, tx, ty)) {
2964 *x_pos = x;
2965 *y_pos = y;
2966 done = 1;
2967 break;
2971 wfree(slot_map);
2972 #undef XY2OFS
2973 return done;
2976 static void moveDock(WDock *dock, int new_x, int new_y)
2978 WAppIcon *btn;
2979 WDrawerChain *dc;
2980 int i;
2982 if (dock->type == WM_DOCK) {
2983 for (dc = dock->screen_ptr->drawers; dc != NULL; dc = dc->next)
2984 moveDock(dc->adrawer, new_x, dc->adrawer->y_pos - dock->y_pos + new_y);
2987 dock->x_pos = new_x;
2988 dock->y_pos = new_y;
2989 for (i = 0; i < dock->max_icons; i++) {
2990 btn = dock->icon_array[i];
2991 if (btn) {
2992 btn->x_pos = new_x + btn->xindex * ICON_SIZE;
2993 btn->y_pos = new_y + btn->yindex * ICON_SIZE;
2994 XMoveWindow(dpy, btn->icon->core->window, btn->x_pos, btn->y_pos);
2999 static void swapDock(WDock *dock)
3001 WScreen *scr = dock->screen_ptr;
3002 WAppIcon *btn;
3003 int x, i;
3005 if (dock->on_right_side)
3006 x = dock->x_pos = scr->scr_width - ICON_SIZE - DOCK_EXTRA_SPACE;
3007 else
3008 x = dock->x_pos = DOCK_EXTRA_SPACE;
3010 swapDrawers(scr, x);
3012 for (i = 0; i < dock->max_icons; i++) {
3013 btn = dock->icon_array[i];
3014 if (btn) {
3015 btn->x_pos = x;
3016 XMoveWindow(dpy, btn->icon->core->window, btn->x_pos, btn->y_pos);
3020 wScreenUpdateUsableArea(scr);
3023 static pid_t execCommand(WAppIcon *btn, const char *command, WSavedState *state)
3025 WScreen *scr = btn->icon->core->screen_ptr;
3026 pid_t pid;
3027 char **argv;
3028 int argc;
3029 char *cmdline;
3031 cmdline = ExpandOptions(scr, command);
3033 if (scr->flags.dnd_data_convertion_status || !cmdline) {
3034 if (cmdline)
3035 wfree(cmdline);
3036 if (state)
3037 wfree(state);
3038 return 0;
3041 wtokensplit(cmdline, &argv, &argc);
3043 if (!argc) {
3044 if (cmdline)
3045 wfree(cmdline);
3046 if (state)
3047 wfree(state);
3048 return 0;
3051 if ((pid = fork()) == 0) {
3052 char **args;
3053 int i;
3055 SetupEnvironment(scr);
3057 #ifdef HAVE_SETSID
3058 setsid();
3059 #endif
3061 args = malloc(sizeof(char *) * (argc + 1));
3062 if (!args)
3063 exit(111);
3065 for (i = 0; i < argc; i++)
3066 args[i] = argv[i];
3068 args[argc] = NULL;
3069 execvp(argv[0], args);
3070 exit(111);
3072 wtokenfree(argv, argc);
3074 if (pid > 0) {
3075 if (!state) {
3076 state = wmalloc(sizeof(WSavedState));
3077 state->hidden = -1;
3078 state->miniaturized = -1;
3079 state->shaded = -1;
3080 if (btn->dock == scr->dock || btn->dock->type == WM_DRAWER || btn->omnipresent)
3081 state->workspace = -1;
3082 else
3083 state->workspace = scr->current_workspace;
3085 wWindowAddSavedState(btn->wm_instance, btn->wm_class, cmdline, pid, state);
3086 wAddDeathHandler(pid, (WDeathHandler *) trackDeadProcess, btn->dock);
3087 } else if (state) {
3088 wfree(state);
3090 wfree(cmdline);
3091 return pid;
3094 void wDockHideIcons(WDock *dock)
3096 int i;
3098 if (dock == NULL)
3099 return;
3101 for (i = 1; i < dock->max_icons; i++) {
3102 if (dock->icon_array[i])
3103 XUnmapWindow(dpy, dock->icon_array[i]->icon->core->window);
3105 dock->mapped = 0;
3107 dockIconPaint(dock->icon_array[0]);
3110 void wDockShowIcons(WDock *dock)
3112 int i;
3113 WAppIcon *btn;
3115 if (dock == NULL)
3116 return;
3118 btn = dock->icon_array[0];
3119 moveDock(dock, btn->x_pos, btn->y_pos);
3121 /* Deleting any change in stacking level, this function is now only about
3122 mapping icons */
3124 if (!dock->collapsed) {
3125 for (i = 1; i < dock->max_icons; i++) {
3126 if (dock->icon_array[i])
3127 XMapWindow(dpy, dock->icon_array[i]->icon->core->window);
3130 dock->mapped = 1;
3132 dockIconPaint(btn);
3135 void wDockLower(WDock *dock)
3137 int i;
3138 WDrawerChain *dc;
3140 if (dock->type == WM_DOCK) {
3141 for (dc = dock->screen_ptr->drawers; dc != NULL; dc = dc->next)
3142 wDockLower(dc->adrawer);
3144 for (i = 0; i < dock->max_icons; i++) {
3145 if (dock->icon_array[i])
3146 wLowerFrame(dock->icon_array[i]->icon->core);
3150 void wDockRaise(WDock *dock)
3152 int i;
3153 WDrawerChain *dc;
3155 for (i = dock->max_icons - 1; i >= 0; i--) {
3156 if (dock->icon_array[i])
3157 wRaiseFrame(dock->icon_array[i]->icon->core);
3159 if (dock->type == WM_DOCK) {
3160 for (dc = dock->screen_ptr->drawers; dc != NULL; dc = dc->next)
3161 wDockRaise(dc->adrawer);
3165 void wDockRaiseLower(WDock *dock)
3167 if (!dock->icon_array[0]->icon->core->stacking->above
3168 || (dock->icon_array[0]->icon->core->stacking->window_level
3169 != dock->icon_array[0]->icon->core->stacking->above->stacking->window_level))
3170 wDockLower(dock);
3171 else
3172 wDockRaise(dock);
3175 void wDockFinishLaunch(WAppIcon *icon)
3177 icon->launching = 0;
3178 icon->relaunching = 0;
3179 dockIconPaint(icon);
3182 WAppIcon *wDockFindIconForWindow(WDock *dock, Window window)
3184 WAppIcon *icon;
3185 int i;
3187 for (i = 0; i < dock->max_icons; i++) {
3188 icon = dock->icon_array[i];
3189 if (icon && icon->main_window == window)
3190 return icon;
3192 return NULL;
3195 void wDockTrackWindowLaunch(WDock *dock, Window window)
3197 WAppIcon *icon;
3198 char *wm_class, *wm_instance;
3199 int i;
3200 Bool firstPass = True;
3201 Bool found = False;
3202 char *command = NULL;
3204 if (!PropGetWMClass(window, &wm_class, &wm_instance)) {
3205 free(wm_class);
3206 free(wm_instance);
3207 return;
3210 command = GetCommandForWindow(window);
3211 retry:
3212 for (i = 0; i < dock->max_icons; i++) {
3213 icon = dock->icon_array[i];
3214 if (!icon)
3215 continue;
3217 /* app is already attached to icon */
3218 if (icon->main_window == window) {
3219 found = True;
3220 break;
3223 if ((icon->wm_instance || icon->wm_class)
3224 && (icon->launching || !icon->running)) {
3226 if (icon->wm_instance && wm_instance && strcmp(icon->wm_instance, wm_instance) != 0)
3227 continue;
3229 if (icon->wm_class && wm_class && strcmp(icon->wm_class, wm_class) != 0)
3230 continue;
3232 if (firstPass && command && strcmp(icon->command, command) != 0)
3233 continue;
3235 if (!icon->relaunching) {
3236 WApplication *wapp;
3238 /* Possibly an application that was docked with dockit,
3239 * but the user did not update WMState to indicate that
3240 * it was docked by force */
3241 wapp = wApplicationOf(window);
3242 if (!wapp) {
3243 icon->forced_dock = 1;
3244 icon->running = 0;
3246 if (!icon->forced_dock)
3247 icon->main_window = window;
3249 found = True;
3250 if (!wPreferences.no_animations && !icon->launching &&
3251 !dock->screen_ptr->flags.startup && !dock->collapsed) {
3252 WAppIcon *aicon;
3253 int x0, y0;
3255 icon->launching = 1;
3256 dockIconPaint(icon);
3258 aicon = wAppIconCreateForDock(dock->screen_ptr, NULL,
3259 wm_instance, wm_class, TILE_NORMAL);
3260 /* XXX: can: aicon->icon == NULL ? */
3261 PlaceIcon(dock->screen_ptr, &x0, &y0, wGetHeadForWindow(aicon->icon->owner));
3262 wAppIconMove(aicon, x0, y0);
3263 /* Should this always be lowered? -Dan */
3264 if (dock->lowered)
3265 wLowerFrame(aicon->icon->core);
3266 XMapWindow(dpy, aicon->icon->core->window);
3267 aicon->launching = 1;
3268 wAppIconPaint(aicon);
3269 SlideWindow(aicon->icon->core->window, x0, y0, icon->x_pos, icon->y_pos);
3270 XUnmapWindow(dpy, aicon->icon->core->window);
3271 wAppIconDestroy(aicon);
3273 wDockFinishLaunch(icon);
3274 break;
3278 if (firstPass && !found) {
3279 firstPass = False;
3280 goto retry;
3283 if (command)
3284 wfree(command);
3286 if (wm_class)
3287 free(wm_class);
3288 if (wm_instance)
3289 free(wm_instance);
3292 void wClipUpdateForWorkspaceChange(WScreen *scr, int workspace)
3294 if (!wPreferences.flags.noclip) {
3295 scr->clip_icon->dock = scr->workspaces[workspace]->clip;
3296 if (scr->current_workspace != workspace) {
3297 WDock *old_clip = scr->workspaces[scr->current_workspace]->clip;
3298 WAppIconChain *chain = scr->global_icons;
3300 while (chain) {
3301 wDockMoveIconBetweenDocks(chain->aicon->dock,
3302 scr->workspaces[workspace]->clip,
3303 chain->aicon, chain->aicon->xindex, chain->aicon->yindex);
3304 if (scr->workspaces[workspace]->clip->collapsed)
3305 XUnmapWindow(dpy, chain->aicon->icon->core->window);
3306 chain = chain->next;
3309 wDockHideIcons(old_clip);
3310 if (old_clip->auto_raise_lower) {
3311 if (old_clip->auto_raise_magic) {
3312 WMDeleteTimerHandler(old_clip->auto_raise_magic);
3313 old_clip->auto_raise_magic = NULL;
3315 wDockLower(old_clip);
3317 if (old_clip->auto_collapse) {
3318 if (old_clip->auto_expand_magic) {
3319 WMDeleteTimerHandler(old_clip->auto_expand_magic);
3320 old_clip->auto_expand_magic = NULL;
3322 old_clip->collapsed = 1;
3324 wDockShowIcons(scr->workspaces[workspace]->clip);
3329 static void trackDeadProcess(pid_t pid, unsigned char status, WDock *dock)
3331 WAppIcon *icon;
3332 int i;
3334 for (i = 0; i < dock->max_icons; i++) {
3335 icon = dock->icon_array[i];
3336 if (!icon)
3337 continue;
3339 if (icon->launching && icon->pid == pid) {
3340 if (!icon->relaunching) {
3341 icon->running = 0;
3342 icon->main_window = None;
3344 wDockFinishLaunch(icon);
3345 icon->pid = 0;
3346 if (status == 111) {
3347 char msg[PATH_MAX];
3348 char *cmd;
3350 #ifdef XDND
3351 if (icon->drop_launch)
3352 cmd = icon->dnd_command;
3353 else
3354 #endif
3355 if (icon->paste_launch)
3356 cmd = icon->paste_command;
3357 else
3358 cmd = icon->command;
3360 snprintf(msg, sizeof(msg), _("Could not execute command \"%s\""), cmd);
3362 wMessageDialog(dock->screen_ptr, _("Error"), msg, _("OK"), NULL, NULL);
3364 break;
3369 /* This function is called when the dock switches state between
3370 * "normal" (including auto-raise/lower) and "keep on top". It is
3371 * therefore clearly distinct from wDockLower/Raise, which are called
3372 * each time a not-kept-on-top dock is lowered/raised. */
3373 static void toggleLowered(WDock *dock)
3375 WAppIcon *tmp;
3376 WDrawerChain *dc;
3377 int newlevel, i;
3379 if (!dock->lowered) {
3380 newlevel = WMNormalLevel;
3381 dock->lowered = 1;
3382 } else {
3383 newlevel = WMDockLevel;
3384 dock->lowered = 0;
3387 for (i = 0; i < dock->max_icons; i++) {
3388 tmp = dock->icon_array[i];
3389 if (!tmp)
3390 continue;
3392 ChangeStackingLevel(tmp->icon->core, newlevel);
3394 /* When the dock is no longer "on top", explicitly lower it as well.
3395 * It saves some CPU cycles (probably) to do it ourselves here
3396 * rather than calling wDockLower at the end of toggleLowered */
3397 if (dock->lowered)
3398 wLowerFrame(tmp->icon->core);
3401 if (dock->type == WM_DOCK) {
3402 for (dc = dock->screen_ptr->drawers; dc != NULL; dc = dc->next) {
3403 toggleLowered(dc->adrawer);
3405 wScreenUpdateUsableArea(dock->screen_ptr);
3409 static void toggleCollapsed(WDock *dock)
3411 if (dock->collapsed) {
3412 dock->collapsed = 0;
3413 wDockShowIcons(dock);
3414 } else {
3415 dock->collapsed = 1;
3416 wDockHideIcons(dock);
3420 static void openDockMenu(WDock *dock, WAppIcon *aicon, XEvent *event)
3422 WScreen *scr = dock->screen_ptr;
3423 WObjDescriptor *desc;
3424 WMenuEntry *entry;
3425 WApplication *wapp = NULL;
3426 int index = 0;
3427 int x_pos;
3428 int n_selected;
3429 int appIsRunning = aicon->running && aicon->icon && aicon->icon->owner;
3431 if (dock->type == WM_DOCK) {
3432 /* Dock position menu */
3433 updateDockPositionMenu(scr->dock_pos_menu, dock);
3434 dock->menu->flags.realized = 0;
3435 if (!wPreferences.flags.nodrawer) {
3436 /* add a drawer */
3437 entry = dock->menu->entries[++index];
3438 entry->clientdata = aicon;
3439 wMenuSetEnabled(dock->menu, index, True);
3441 } else {
3442 /* clip/drawer options */
3443 if (scr->clip_options)
3444 updateClipOptionsMenu(scr->clip_options, dock);
3446 n_selected = numberOfSelectedIcons(dock);
3448 if (dock->type == WM_CLIP) {
3449 /* Rename Workspace */
3450 entry = dock->menu->entries[++index];
3451 if (aicon == scr->clip_icon) {
3452 entry->callback = renameCallback;
3453 entry->clientdata = dock;
3454 entry->flags.indicator = 0;
3455 entry->text = _("Rename Workspace");
3456 } else {
3457 entry->callback = omnipresentCallback;
3458 entry->clientdata = aicon;
3459 if (n_selected > 0) {
3460 entry->flags.indicator = 0;
3461 entry->text = _("Toggle Omnipresent");
3462 } else {
3463 entry->flags.indicator = 1;
3464 entry->flags.indicator_on = aicon->omnipresent;
3465 entry->flags.indicator_type = MI_CHECK;
3466 entry->text = _("Omnipresent");
3471 /* select/unselect icon */
3472 entry = dock->menu->entries[++index];
3473 entry->clientdata = aicon;
3474 entry->flags.indicator_on = aicon->icon->selected;
3475 wMenuSetEnabled(dock->menu, index, aicon != scr->clip_icon && !wIsADrawer(aicon));
3477 /* select/unselect all icons */
3478 entry = dock->menu->entries[++index];
3479 entry->clientdata = aicon;
3480 if (n_selected > 0)
3481 entry->text = _("Unselect All Icons");
3482 else
3483 entry->text = _("Select All Icons");
3485 wMenuSetEnabled(dock->menu, index, dock->icon_count > 1);
3487 /* keep icon(s) */
3488 entry = dock->menu->entries[++index];
3489 entry->clientdata = aicon;
3490 if (n_selected > 1)
3491 entry->text = _("Keep Icons");
3492 else
3493 entry->text = _("Keep Icon");
3495 wMenuSetEnabled(dock->menu, index, dock->icon_count > 1);
3497 if (dock->type == WM_CLIP) {
3498 /* this is the workspace submenu part */
3499 entry = dock->menu->entries[++index];
3500 if (n_selected > 1)
3501 entry->text = _("Move Icons To");
3502 else
3503 entry->text = _("Move Icon To");
3505 if (scr->clip_submenu)
3506 updateWorkspaceMenu(scr->clip_submenu, aicon);
3508 wMenuSetEnabled(dock->menu, index, !aicon->omnipresent);
3511 /* remove icon(s) */
3512 entry = dock->menu->entries[++index];
3513 entry->clientdata = aicon;
3514 if (n_selected > 1)
3515 entry->text = _("Remove Icons");
3516 else
3517 entry->text = _("Remove Icon");
3519 wMenuSetEnabled(dock->menu, index, dock->icon_count > 1);
3521 /* attract icon(s) */
3522 entry = dock->menu->entries[++index];
3523 entry->clientdata = aicon;
3525 dock->menu->flags.realized = 0;
3526 wMenuRealize(dock->menu);
3529 if (aicon->icon->owner)
3530 wapp = wApplicationOf(aicon->icon->owner->main_window);
3531 else
3532 wapp = NULL;
3534 /* launch */
3535 entry = dock->menu->entries[++index];
3536 entry->clientdata = aicon;
3537 wMenuSetEnabled(dock->menu, index, aicon->command != NULL);
3539 /* unhide here */
3540 entry = dock->menu->entries[++index];
3541 entry->clientdata = aicon;
3542 if (wapp && wapp->flags.hidden)
3543 entry->text = _("Unhide Here");
3544 else
3545 entry->text = _("Bring Here");
3547 wMenuSetEnabled(dock->menu, index, appIsRunning);
3549 /* hide */
3550 entry = dock->menu->entries[++index];
3551 entry->clientdata = aicon;
3552 if (wapp && wapp->flags.hidden)
3553 entry->text = _("Unhide");
3554 else
3555 entry->text = _("Hide");
3557 wMenuSetEnabled(dock->menu, index, appIsRunning);
3559 /* settings */
3560 entry = dock->menu->entries[++index];
3561 entry->clientdata = aicon;
3562 wMenuSetEnabled(dock->menu, index, !aicon->editing && !wPreferences.flags.noupdates);
3564 /* kill or remove drawer */
3565 entry = dock->menu->entries[++index];
3566 entry->clientdata = aicon;
3567 if (wIsADrawer(aicon)) {
3568 entry->callback = removeDrawerCallback;
3569 entry->text = _("Remove drawer");
3570 wMenuSetEnabled(dock->menu, index, True);
3571 } else {
3572 entry->callback = killCallback;
3573 entry->text = _("Kill");
3574 wMenuSetEnabled(dock->menu, index, appIsRunning);
3577 if (!dock->menu->flags.realized)
3578 wMenuRealize(dock->menu);
3580 if (dock->type == WM_CLIP || dock->type == WM_DRAWER) {
3581 /*x_pos = event->xbutton.x_root+2; */
3582 x_pos = event->xbutton.x_root - dock->menu->frame->core->width / 2 - 1;
3583 if (x_pos < 0) {
3584 x_pos = 0;
3585 } else if (x_pos + dock->menu->frame->core->width > scr->scr_width - 2) {
3586 x_pos = scr->scr_width - dock->menu->frame->core->width - 4;
3588 } else {
3589 x_pos = dock->on_right_side ? scr->scr_width - dock->menu->frame->core->width - 3 : 0;
3592 wMenuMapAt(dock->menu, x_pos, event->xbutton.y_root + 2, False);
3594 /* allow drag select */
3595 event->xany.send_event = True;
3596 desc = &dock->menu->menu->descriptor;
3597 (*desc->handle_mousedown) (desc, event);
3600 /******************************************************************/
3601 static void iconDblClick(WObjDescriptor *desc, XEvent *event)
3603 WAppIcon *btn = desc->parent;
3604 WDock *dock = btn->dock;
3605 WApplication *wapp = NULL;
3606 int unhideHere = 0;
3608 if (btn->icon->owner && !(event->xbutton.state & ControlMask)) {
3609 wapp = wApplicationOf(btn->icon->owner->main_window);
3611 assert(wapp != NULL);
3613 unhideHere = (event->xbutton.state & ShiftMask);
3615 /* go to the last workspace that the user worked on the app */
3616 if (wapp->last_workspace != dock->screen_ptr->current_workspace && !unhideHere)
3617 wWorkspaceChange(dock->screen_ptr, wapp->last_workspace);
3619 wUnhideApplication(wapp, event->xbutton.button == Button2, unhideHere);
3621 if (event->xbutton.state & MOD_MASK)
3622 wHideOtherApplications(btn->icon->owner);
3623 } else {
3624 if (event->xbutton.button == Button1) {
3625 if (event->xbutton.state & MOD_MASK) {
3626 /* raise/lower dock */
3627 toggleLowered(dock);
3628 } else if (btn == dock->screen_ptr->clip_icon) {
3629 if (getClipButton(event->xbutton.x, event->xbutton.y) != CLIP_IDLE)
3630 handleClipChangeWorkspace(dock->screen_ptr, event);
3631 else if (wPreferences.flags.clip_merged_in_dock) {
3632 // Is actually the dock
3633 if (btn->command)
3635 if (!btn->launching && (!btn->running || (event->xbutton.state & ControlMask)))
3636 launchDockedApplication(btn, False);
3638 else
3640 wShowInfoPanel(dock->screen_ptr);
3643 else
3644 toggleCollapsed(dock);
3645 } else if (wIsADrawer(btn)) {
3646 toggleCollapsed(dock);
3647 } else if (btn->command) {
3648 if (!btn->launching && (!btn->running || (event->xbutton.state & ControlMask)))
3649 launchDockedApplication(btn, False);
3650 } else if (btn->xindex == 0 && btn->yindex == 0 && btn->dock->type == WM_DOCK) {
3651 wShowInfoPanel(dock->screen_ptr);
3657 static void handleDockMove(WDock *dock, WAppIcon *aicon, XEvent *event)
3659 WScreen *scr = dock->screen_ptr;
3660 int ofs_x = event->xbutton.x, ofs_y = event->xbutton.y;
3661 WIcon *icon = aicon->icon;
3662 WAppIcon *tmpaicon;
3663 WDrawerChain *dc;
3664 int x = aicon->x_pos, y = aicon->y_pos;;
3665 int shad_x = x, shad_y = y;
3666 XEvent ev;
3667 int grabbed = 0, done, previously_on_right, now_on_right, previous_x_pos, i;
3668 Pixmap ghost = None;
3669 int superfluous = wPreferences.superfluous; /* we catch it to avoid problems */
3671 if (XGrabPointer(dpy, aicon->icon->core->window, True, ButtonMotionMask
3672 | ButtonReleaseMask | ButtonPressMask, GrabModeAsync,
3673 GrabModeAsync, None, None, CurrentTime) != GrabSuccess)
3674 wwarning("pointer grab failed for dock move");
3676 if (dock->type == WM_DRAWER) {
3677 Window wins[2];
3678 wins[0] = icon->core->window;
3679 wins[1] = scr->dock_shadow;
3680 XRestackWindows(dpy, wins, 2);
3681 XMoveResizeWindow(dpy, scr->dock_shadow, aicon->x_pos, aicon->y_pos,
3682 ICON_SIZE, ICON_SIZE);
3683 if (superfluous) {
3684 if (icon->pixmap!=None)
3685 ghost = MakeGhostIcon(scr, icon->pixmap);
3686 else
3687 ghost = MakeGhostIcon(scr, icon->core->window);
3689 XSetWindowBackgroundPixmap(dpy, scr->dock_shadow, ghost);
3690 XClearWindow(dpy, scr->dock_shadow);
3692 XMapWindow(dpy, scr->dock_shadow);
3695 previously_on_right = now_on_right = dock->on_right_side;
3696 previous_x_pos = dock->x_pos;
3697 done = 0;
3698 while (!done) {
3699 WMMaskEvent(dpy, PointerMotionMask | ButtonReleaseMask | ButtonPressMask
3700 | ButtonMotionMask | ExposureMask | EnterWindowMask, &ev);
3701 switch (ev.type) {
3702 case Expose:
3703 WMHandleEvent(&ev);
3704 break;
3706 case EnterNotify:
3707 /* It means the cursor moved so fast that it entered
3708 * something else (if moving slowly, it would have
3709 * stayed in the dock that is being moved. Ignore such
3710 * "spurious" EnterNotifiy's */
3711 break;
3713 case MotionNotify:
3714 if (!grabbed) {
3715 if (abs(ofs_x - ev.xmotion.x) >= MOVE_THRESHOLD
3716 || abs(ofs_y - ev.xmotion.y) >= MOVE_THRESHOLD) {
3717 XChangeActivePointerGrab(dpy, ButtonMotionMask
3718 | ButtonReleaseMask | ButtonPressMask,
3719 wPreferences.cursor[WCUR_MOVE], CurrentTime);
3720 grabbed = 1;
3722 break;
3724 switch (dock->type) {
3725 case WM_CLIP:
3726 x = ev.xmotion.x_root - ofs_x;
3727 y = ev.xmotion.y_root - ofs_y;
3728 wScreenKeepInside(scr, &x, &y, ICON_SIZE, ICON_SIZE);
3729 moveDock(dock, x, y);
3730 break;
3731 case WM_DOCK:
3732 x = ev.xmotion.x_root - ofs_x;
3733 y = ev.xmotion.y_root - ofs_y;
3734 if (previously_on_right)
3736 now_on_right = (ev.xmotion.x_root >= previous_x_pos - ICON_SIZE);
3738 else
3740 now_on_right = (ev.xmotion.x_root > previous_x_pos + ICON_SIZE * 2);
3742 if (now_on_right != dock->on_right_side)
3744 dock->on_right_side = now_on_right;
3745 swapDock(dock);
3746 wArrangeIcons(scr, False);
3748 // Also perform the vertical move
3749 wScreenKeepInside(scr, &x, &y, ICON_SIZE, ICON_SIZE);
3750 moveDock(dock, dock->x_pos, y);
3751 if (wPreferences.flags.wrap_appicons_in_dock)
3753 for (i = 0; i < dock->max_icons; i++) {
3754 int new_y, new_index, j, ok;
3755 if ((tmpaicon = dock->icon_array[i]) == NULL)
3756 continue;
3757 if (onScreen(scr, tmpaicon->x_pos, tmpaicon->y_pos))
3758 continue;
3759 new_y = (tmpaicon->y_pos + ICON_SIZE * dock->max_icons) % (ICON_SIZE * dock->max_icons);
3760 new_index = (new_y - dock->y_pos) / ICON_SIZE;
3761 if (!onScreen(scr, tmpaicon->x_pos, new_y))
3762 continue;
3763 ok = 1;
3764 for (j = 0; j < dock->max_icons; j++)
3766 if (dock->icon_array[j] != NULL &&
3767 dock->icon_array[j]->yindex == new_index)
3769 ok = 0;
3770 break;
3773 if (!ok || getDrawer(scr, new_index) != NULL)
3774 continue;
3775 wDockReattachIcon(dock, tmpaicon, tmpaicon->xindex, new_index);
3777 for (dc = scr->drawers; dc != NULL; dc = dc->next)
3779 int new_y, new_index, j, ok;
3780 tmpaicon = dc->adrawer->icon_array[0];
3781 if (onScreen(scr, tmpaicon->x_pos, tmpaicon->y_pos))
3782 continue;
3783 new_y = (tmpaicon->y_pos + ICON_SIZE * dock->max_icons) % (ICON_SIZE * dock->max_icons);
3784 new_index = (new_y - dock->y_pos) / ICON_SIZE;
3785 if (!onScreen(scr, tmpaicon->x_pos, new_y))
3786 continue;
3787 ok = 1;
3788 for (j = 0; j < dock->max_icons; j++)
3790 if (dock->icon_array[j] != NULL &&
3791 dock->icon_array[j]->yindex == new_index)
3793 ok = 0;
3794 break;
3797 if (!ok || getDrawer(scr, new_index) != NULL)
3798 continue;
3799 moveDock(dc->adrawer, tmpaicon->x_pos, new_y);
3802 break;
3803 case WM_DRAWER:
3805 WDock *real_dock = dock->screen_ptr->dock;
3806 Bool snapped;
3807 int ix, iy;
3808 x = ev.xmotion.x_root - ofs_x;
3809 y = ev.xmotion.y_root - ofs_y;
3810 snapped = wDockSnapIcon(real_dock, aicon, x, y, &ix, &iy, True);
3811 if (snapped) {
3812 shad_x = real_dock->x_pos + ix * wPreferences.icon_size;
3813 shad_y = real_dock->y_pos + iy * wPreferences.icon_size;
3814 XMoveWindow(dpy, scr->dock_shadow, shad_x, shad_y);
3816 moveDock(dock, x, y);
3817 break;
3820 break;
3822 case ButtonPress:
3823 break;
3825 case ButtonRelease:
3826 if (ev.xbutton.button != event->xbutton.button)
3827 break;
3828 XUngrabPointer(dpy, CurrentTime);
3829 if (dock->type == WM_DRAWER) {
3830 Window *wins[dock->icon_count];
3832 for (i = 0; i < dock->max_icons; i++) {
3833 if ((tmpaicon = dock->icon_array[i]) == NULL)
3834 continue;
3835 wins[ tmpaicon->xindex + (dock->on_right_side ? dock->icon_count - 1 : 0) ] = &tmpaicon->icon->core->window;
3837 SlideWindows(wins, dock->icon_count,
3838 (dock->on_right_side ? x - (dock->icon_count - 1) * ICON_SIZE : x),
3840 (dock->on_right_side ? shad_x - (dock->icon_count - 1) * ICON_SIZE : shad_x),
3841 shad_y);
3842 XUnmapWindow(dpy, scr->dock_shadow);
3843 moveDock(dock, shad_x, shad_y);
3844 XResizeWindow(dpy, scr->dock_shadow, ICON_SIZE, ICON_SIZE);
3846 done = 1;
3847 break;
3850 if (superfluous) {
3851 if (ghost != None)
3852 XFreePixmap(dpy, ghost);
3853 XSetWindowBackground(dpy, scr->dock_shadow, scr->white_pixel);
3858 static int getClipButton(int px, int py)
3860 int pt = (CLIP_BUTTON_SIZE + 2) * ICON_SIZE / 64;
3862 if (px < 0 || py < 0 || px >= ICON_SIZE || py >= ICON_SIZE)
3863 return CLIP_IDLE;
3865 if (py <= pt - ((int)ICON_SIZE - 1 - px))
3866 return CLIP_FORWARD;
3867 else if (px <= pt - ((int)ICON_SIZE - 1 - py))
3868 return CLIP_REWIND;
3870 return CLIP_IDLE;
3873 static void handleClipChangeWorkspace(WScreen *scr, XEvent *event)
3875 XEvent ev;
3876 int done, direction, new_ws;
3877 int new_dir;
3878 WDock *clip = scr->clip_icon->dock;
3880 direction = getClipButton(event->xbutton.x, event->xbutton.y);
3882 clip->lclip_button_pushed = direction == CLIP_REWIND;
3883 clip->rclip_button_pushed = direction == CLIP_FORWARD;
3885 wClipIconPaint(scr->clip_icon);
3886 done = 0;
3887 while (!done) {
3888 WMMaskEvent(dpy, ExposureMask | ButtonMotionMask | ButtonReleaseMask | ButtonPressMask, &ev);
3889 switch (ev.type) {
3890 case Expose:
3891 WMHandleEvent(&ev);
3892 break;
3894 case MotionNotify:
3895 new_dir = getClipButton(ev.xmotion.x, ev.xmotion.y);
3896 if (new_dir != direction) {
3897 direction = new_dir;
3898 clip->lclip_button_pushed = direction == CLIP_REWIND;
3899 clip->rclip_button_pushed = direction == CLIP_FORWARD;
3900 wClipIconPaint(scr->clip_icon);
3902 break;
3904 case ButtonPress:
3905 break;
3907 case ButtonRelease:
3908 if (ev.xbutton.button == event->xbutton.button)
3909 done = 1;
3913 clip->lclip_button_pushed = 0;
3914 clip->rclip_button_pushed = 0;
3916 new_ws = wPreferences.ws_advance || (event->xbutton.state & ControlMask);
3918 if (direction == CLIP_FORWARD) {
3919 if (scr->current_workspace < scr->workspace_count - 1)
3920 wWorkspaceChange(scr, scr->current_workspace + 1);
3921 else if (new_ws && scr->current_workspace < MAX_WORKSPACES - 1)
3922 wWorkspaceChange(scr, scr->current_workspace + 1);
3923 else if (wPreferences.ws_cycle)
3924 wWorkspaceChange(scr, 0);
3925 } else if (direction == CLIP_REWIND) {
3926 if (scr->current_workspace > 0)
3927 wWorkspaceChange(scr, scr->current_workspace - 1);
3928 else if (scr->current_workspace == 0 && wPreferences.ws_cycle)
3929 wWorkspaceChange(scr, scr->workspace_count - 1);
3932 wClipIconPaint(scr->clip_icon);
3935 static void iconMouseDown(WObjDescriptor *desc, XEvent *event)
3937 WAppIcon *aicon = desc->parent;
3938 WDock *dock = aicon->dock;
3939 WScreen *scr = aicon->icon->core->screen_ptr;
3941 if (aicon->editing || WCHECK_STATE(WSTATE_MODAL))
3942 return;
3944 scr->last_dock = dock;
3946 if (dock->menu->flags.mapped)
3947 wMenuUnmap(dock->menu);
3949 if (IsDoubleClick(scr, event)) {
3950 /* double-click was not in the main clip icon */
3951 if (dock->type != WM_CLIP || aicon->xindex != 0 || aicon->yindex != 0
3952 || getClipButton(event->xbutton.x, event->xbutton.y) == CLIP_IDLE) {
3953 iconDblClick(desc, event);
3954 return;
3958 if (event->xbutton.button == Button1) {
3959 if (event->xbutton.state & MOD_MASK)
3960 wDockLower(dock);
3961 else
3962 wDockRaise(dock);
3964 if ((event->xbutton.state & ShiftMask) && aicon != scr->clip_icon && dock->type != WM_DOCK) {
3965 wIconSelect(aicon->icon);
3966 return;
3969 if (aicon->yindex == 0 && aicon->xindex == 0) {
3970 if (getClipButton(event->xbutton.x, event->xbutton.y) != CLIP_IDLE &&
3971 (dock->type == WM_CLIP || (dock->type == WM_DOCK && wPreferences.flags.clip_merged_in_dock)))
3972 handleClipChangeWorkspace(scr, event);
3973 else
3974 handleDockMove(dock, aicon, event);
3975 } else {
3976 Bool hasMoved = wHandleAppIconMove(aicon, event);
3977 if (wPreferences.single_click && !hasMoved)
3978 iconDblClick(desc, event);
3980 } else if (event->xbutton.button == Button2 && aicon == scr->clip_icon) {
3981 if (!scr->clip_ws_menu)
3982 scr->clip_ws_menu = wWorkspaceMenuMake(scr, False);
3984 if (scr->clip_ws_menu) {
3985 WMenu *wsMenu = scr->clip_ws_menu;
3986 int xpos;
3988 wWorkspaceMenuUpdate(scr, wsMenu);
3990 xpos = event->xbutton.x_root - wsMenu->frame->core->width / 2 - 1;
3991 if (xpos < 0) {
3992 xpos = 0;
3993 } else if (xpos + wsMenu->frame->core->width > scr->scr_width - 2) {
3994 xpos = scr->scr_width - wsMenu->frame->core->width - 4;
3996 wMenuMapAt(wsMenu, xpos, event->xbutton.y_root + 2, False);
3998 desc = &wsMenu->menu->descriptor;
3999 event->xany.send_event = True;
4000 (*desc->handle_mousedown) (desc, event);
4002 } else if (event->xbutton.button == Button2 && dock->type == WM_CLIP &&
4003 (event->xbutton.state & ShiftMask) && aicon != scr->clip_icon) {
4004 wClipMakeIconOmnipresent(aicon, !aicon->omnipresent);
4005 } else if (event->xbutton.button == Button3) {
4006 if (event->xbutton.send_event &&
4007 XGrabPointer(dpy, aicon->icon->core->window, True, ButtonMotionMask
4008 | ButtonReleaseMask | ButtonPressMask, GrabModeAsync,
4009 GrabModeAsync, None, None, CurrentTime) != GrabSuccess) {
4010 wwarning("pointer grab failed for dockicon menu");
4011 return;
4014 openDockMenu(dock, aicon, event);
4015 } else if (event->xbutton.button == Button2) {
4016 WAppIcon *btn = desc->parent;
4018 if (!btn->launching && (!btn->running || (event->xbutton.state & ControlMask)))
4019 launchDockedApplication(btn, True);
4020 } else if (event->xbutton.button == Button4 && dock->type == WM_CLIP) {
4021 wWorkspaceRelativeChange(scr, 1);
4022 } else if (event->xbutton.button == Button5 && dock->type == WM_CLIP) {
4023 wWorkspaceRelativeChange(scr, -1);
4027 static void clipEnterNotify(WObjDescriptor *desc, XEvent *event)
4029 WAppIcon *btn = (WAppIcon *) desc->parent;
4030 WDock *dock, *tmp;
4031 WScreen *scr;
4033 /* Parameter not used, but tell the compiler that it is ok */
4034 (void) event;
4036 assert(event->type == EnterNotify);
4038 if (desc->parent_type != WCLASS_DOCK_ICON)
4039 return;
4041 scr = btn->icon->core->screen_ptr;
4042 dock = btn->dock;
4044 if (dock == NULL)
4045 return;
4047 /* The auto raise/lower code */
4048 tmp = (dock->type == WM_DRAWER ? scr->dock : dock);
4049 if (tmp->auto_lower_magic) {
4050 WMDeleteTimerHandler(tmp->auto_lower_magic);
4051 tmp->auto_lower_magic = NULL;
4053 if (tmp->auto_raise_lower && !tmp->auto_raise_magic)
4054 tmp->auto_raise_magic = WMAddTimerHandler(wPreferences.clip_auto_raise_delay, clipAutoRaise, (void *) tmp);
4056 if (dock->type != WM_CLIP && dock->type != WM_DRAWER)
4057 return;
4059 /* The auto expand/collapse code */
4060 if (dock->auto_collapse_magic) {
4061 WMDeleteTimerHandler(dock->auto_collapse_magic);
4062 dock->auto_collapse_magic = NULL;
4064 if (dock->auto_collapse && !dock->auto_expand_magic)
4065 dock->auto_expand_magic = WMAddTimerHandler(wPreferences.clip_auto_expand_delay, clipAutoExpand, (void *)dock);
4068 static void clipLeave(WDock *dock)
4070 XEvent event;
4071 WObjDescriptor *desc = NULL;
4072 WDock *tmp;
4074 if (dock == NULL)
4075 return;
4077 if (XCheckTypedEvent(dpy, EnterNotify, &event) != False) {
4078 if (XFindContext(dpy, event.xcrossing.window, w_global.context.client_win,
4079 (XPointer *) & desc) != XCNOENT
4080 && desc && desc->parent_type == WCLASS_DOCK_ICON
4081 && ((WAppIcon *) desc->parent)->dock == dock) {
4082 /* We haven't left the dock/clip/drawer yet */
4083 XPutBackEvent(dpy, &event);
4084 return;
4087 XPutBackEvent(dpy, &event);
4088 } else {
4089 /* We entered a withdrawn window, so we're still in Clip */
4090 return;
4093 tmp = (dock->type == WM_DRAWER ? dock->screen_ptr->dock : dock);
4094 if (tmp->auto_raise_magic) {
4095 WMDeleteTimerHandler(tmp->auto_raise_magic);
4096 tmp->auto_raise_magic = NULL;
4098 if (tmp->auto_raise_lower && !tmp->auto_lower_magic)
4099 tmp->auto_lower_magic = WMAddTimerHandler(wPreferences.clip_auto_lower_delay, clipAutoLower, (void *)tmp);
4101 if (dock->type != WM_CLIP && dock->type != WM_DRAWER)
4102 return;
4104 if (dock->auto_expand_magic) {
4105 WMDeleteTimerHandler(dock->auto_expand_magic);
4106 dock->auto_expand_magic = NULL;
4108 if (dock->auto_collapse && !dock->auto_collapse_magic)
4109 dock->auto_collapse_magic = WMAddTimerHandler(wPreferences.clip_auto_collapse_delay, clipAutoCollapse, (void *)dock);
4112 static void clipLeaveNotify(WObjDescriptor *desc, XEvent *event)
4114 WAppIcon *btn = (WAppIcon *) desc->parent;
4116 /* Parameter not used, but tell the compiler that it is ok */
4117 (void) event;
4119 assert(event->type == LeaveNotify);
4121 if (desc->parent_type != WCLASS_DOCK_ICON)
4122 return;
4124 clipLeave(btn->dock);
4127 static void clipAutoCollapse(void *cdata)
4129 WDock *dock = (WDock *) cdata;
4131 if (dock->type != WM_CLIP && dock->type != WM_DRAWER)
4132 return;
4134 if (dock->auto_collapse) {
4135 dock->collapsed = 1;
4136 wDockHideIcons(dock);
4138 dock->auto_collapse_magic = NULL;
4141 static void clipAutoExpand(void *cdata)
4143 WDock *dock = (WDock *) cdata;
4145 if (dock->type != WM_CLIP && dock->type != WM_DRAWER)
4146 return;
4148 if (dock->auto_collapse) {
4149 dock->collapsed = 0;
4150 wDockShowIcons(dock);
4152 dock->auto_expand_magic = NULL;
4155 static void clipAutoLower(void *cdata)
4157 WDock *dock = (WDock *) cdata;
4159 if (dock->auto_raise_lower)
4160 wDockLower(dock);
4162 dock->auto_lower_magic = NULL;
4165 static void clipAutoRaise(void *cdata)
4167 WDock *dock = (WDock *) cdata;
4169 if (dock->auto_raise_lower)
4170 wDockRaise(dock);
4172 dock->auto_raise_magic = NULL;
4175 static Bool iconCanBeOmnipresent(WAppIcon *aicon)
4177 WScreen *scr = aicon->icon->core->screen_ptr;
4178 WDock *clip;
4179 WAppIcon *btn;
4180 int i, j;
4182 for (i = 0; i < scr->workspace_count; i++) {
4183 clip = scr->workspaces[i]->clip;
4185 if (clip == aicon->dock)
4186 continue;
4188 if (clip->icon_count + scr->global_icon_count >= clip->max_icons)
4189 return False; /* Clip is full in some workspace */
4191 for (j = 0; j < clip->max_icons; j++) {
4192 btn = clip->icon_array[j];
4193 if (btn && btn->xindex == aicon->xindex && btn->yindex == aicon->yindex)
4194 return False;
4198 return True;
4201 int wClipMakeIconOmnipresent(WAppIcon *aicon, int omnipresent)
4203 WScreen *scr = aicon->icon->core->screen_ptr;
4204 WAppIconChain *new_entry, *tmp, *tmp1;
4205 int status = WO_SUCCESS;
4207 if ((scr->dock && aicon->dock == scr->dock) || aicon == scr->clip_icon)
4208 return WO_NOT_APPLICABLE;
4210 if (aicon->omnipresent == omnipresent)
4211 return WO_SUCCESS;
4213 if (omnipresent) {
4214 if (iconCanBeOmnipresent(aicon)) {
4215 aicon->omnipresent = 1;
4216 new_entry = wmalloc(sizeof(WAppIconChain));
4217 new_entry->aicon = aicon;
4218 new_entry->next = scr->global_icons;
4219 scr->global_icons = new_entry;
4220 scr->global_icon_count++;
4221 } else {
4222 aicon->omnipresent = 0;
4223 status = WO_FAILED;
4225 } else {
4226 aicon->omnipresent = 0;
4227 if (aicon == scr->global_icons->aicon) {
4228 tmp = scr->global_icons->next;
4229 wfree(scr->global_icons);
4230 scr->global_icons = tmp;
4231 scr->global_icon_count--;
4232 } else {
4233 tmp = scr->global_icons;
4234 while (tmp->next) {
4235 if (tmp->next->aicon == aicon) {
4236 tmp1 = tmp->next->next;
4237 wfree(tmp->next);
4238 tmp->next = tmp1;
4239 scr->global_icon_count--;
4240 break;
4242 tmp = tmp->next;
4247 wAppIconPaint(aicon);
4249 return status;
4252 static void drawerAppendToChain(WScreen *scr, WDock *drawer)
4254 WDrawerChain **where_to_add;
4256 where_to_add = &scr->drawers;
4257 while ((*where_to_add) != NULL) {
4258 where_to_add = &(*where_to_add)->next;
4261 *where_to_add = wmalloc(sizeof(WDrawerChain));
4262 (*where_to_add)->adrawer = drawer;
4263 (*where_to_add)->next = NULL;
4264 scr->drawer_count++;
4268 static void drawerRemoveFromChain(WScreen *scr, WDock *drawer)
4270 WDrawerChain *next, **to_remove;
4272 to_remove = &scr->drawers;
4273 while (True) {
4274 if (*to_remove == NULL) {
4275 wwarning("The drawer to be removed can not be found.");
4276 return;
4278 if ((*to_remove)->adrawer == drawer)
4279 break;
4281 to_remove = &(*to_remove)->next;
4283 next = (*to_remove)->next;
4284 wfree(*to_remove);
4285 *to_remove = next;
4286 scr->drawer_count--;
4290 /* Don't free the returned string. Duplicate it. */
4291 static char * findUniqueName(WScreen *scr, const char *instance_basename)
4293 static char buffer[128];
4294 WDrawerChain *dc;
4295 int i;
4296 Bool already_in_use = True;
4298 #define UNIQUE_NAME_WATCHDOG 128
4299 for (i = 0; already_in_use && i < UNIQUE_NAME_WATCHDOG; i++) {
4300 snprintf(buffer, sizeof buffer, "%s%d", instance_basename, i);
4302 already_in_use = False;
4304 for (dc = scr->drawers; dc != NULL; dc = dc->next) {
4305 if (!strncmp(dc->adrawer->icon_array[0]->wm_instance, buffer,
4306 sizeof buffer)) {
4307 already_in_use = True;
4308 break;
4313 if (i == UNIQUE_NAME_WATCHDOG)
4314 wwarning("Couldn't find a unique name for drawer in %d attempts.", i);
4315 #undef UNIQUE_NAME_WATCHDOG
4317 return buffer;
4321 static void drawerIconExpose(WObjDescriptor *desc, XEvent *event)
4323 /* Parameter not used, but tell the compiler that it is ok */
4324 (void) event;
4326 wDrawerIconPaint((WAppIcon *) desc->parent);
4330 static int addADrawer(WScreen *scr)
4332 int i, y, sig, found_y;
4333 WDock *drawer, *dock = scr->dock;
4334 WDrawerChain *dc;
4335 char can_be_here[2 * dock->max_icons - 1];
4337 if (dock->icon_count + scr->drawer_count >= dock->max_icons)
4338 return -1;
4340 for (y = -dock->max_icons + 1; y < dock->max_icons; y++) {
4341 can_be_here[y + dock->max_icons - 1] = True;
4343 for (i = 0; i < dock->max_icons; i++) {
4344 if (dock->icon_array[i] != NULL)
4345 can_be_here[dock->icon_array[i]->yindex + dock->max_icons - 1] = False;
4347 for (dc = scr->drawers; dc != NULL; dc = dc->next) {
4348 y = (int) ((dc->adrawer->y_pos - dock->y_pos) / ICON_SIZE);
4349 can_be_here[y + dock->max_icons - 1] = False;
4352 found_y = False;
4353 for (sig = 1; !found_y && sig > -2; sig -= 2) // 1, then -1
4355 for (y = sig; sig * y < dock->max_icons; y += sig)
4357 if (can_be_here[y + dock->max_icons - 1] &&
4358 onScreen(scr, dock->x_pos, dock->y_pos + y * ICON_SIZE))
4360 found_y = True;
4361 break;
4366 if (!found_y)
4367 /* This can happen even when dock->icon_count + scr->drawer_count
4368 * < dock->max_icons when the dock is not aligned on an
4369 * ICON_SIZE multiple, as some space is lost above and under it */
4370 return -1;
4372 drawer = wDockCreate(scr, WM_DRAWER, NULL);
4373 drawer->lowered = scr->dock->lowered;
4374 if (!drawer->lowered)
4375 ChangeStackingLevel(drawer->icon_array[0]->icon->core, WMDockLevel);
4376 else
4377 ChangeStackingLevel(drawer->icon_array[0]->icon->core, WMNormalLevel);
4378 drawer->auto_raise_lower = scr->dock->auto_raise_lower;
4379 drawer->x_pos = dock->x_pos;
4380 drawer->y_pos = dock->y_pos + ICON_SIZE * y;
4381 drawer->icon_array[0]->xindex = 0;
4382 drawer->icon_array[0]->yindex = 0;
4383 drawer->icon_array[0]->x_pos = drawer->x_pos;
4384 drawer->icon_array[0]->y_pos = drawer->y_pos;
4385 XMoveWindow(dpy, drawer->icon_array[0]->icon->core->window,
4386 drawer->icon_array[0]->x_pos, drawer->icon_array[0]->y_pos);
4388 return 0;
4392 static void addADrawerCallback(WMenu *menu, WMenuEntry *entry)
4394 /* Parameter not used, but tell the compiler that it is ok */
4395 (void) menu;
4397 assert(entry->clientdata!=NULL);
4398 addADrawer(((WAppIcon *) entry->clientdata)->dock->screen_ptr);
4402 static void drawerDestroy(WDock *drawer)
4404 WScreen *scr;
4405 int i;
4406 WAppIcon *aicon = NULL;
4407 WMArray *icons;
4409 if (drawer == NULL)
4410 return;
4412 scr = drawer->screen_ptr;
4414 /* Note regarding menus: we can't delete any dock/clip/drawer menu, because
4415 * that would (attempt to) wfree some memory in gettext library (see menu
4416 * entries that have several "versions", such like "Hide" and "Unhide"). */
4417 wDefaultPurgeInfo(drawer->icon_array[0]->wm_instance,
4418 drawer->icon_array[0]->wm_class);
4420 if (drawer->icon_count == 2) {
4421 /* Drawer contains a single appicon: dock it where the drawer was */
4422 for (i = 1; i < drawer->max_icons; i++) {
4423 if ((aicon = drawer->icon_array[i]))
4424 break;
4427 wDockMoveIconBetweenDocks(drawer, scr->dock, aicon,
4428 0, (drawer->y_pos - scr->dock->y_pos) / ICON_SIZE);
4429 XMoveWindow(dpy, aicon->icon->core->window, drawer->x_pos, drawer->y_pos);
4430 XMapWindow(dpy, aicon->icon->core->window);
4431 } else if (drawer->icon_count > 2) {
4432 icons = WMCreateArray(drawer->icon_count - 1);
4433 for (i = 1; i < drawer->max_icons; i++) {
4434 if (!(aicon = drawer->icon_array[i]))
4435 continue;
4436 WMAddToArray(icons, aicon);
4438 removeIcons(icons, drawer);
4441 if (drawer->auto_collapse_magic) {
4442 WMDeleteTimerHandler(drawer->auto_collapse_magic);
4443 drawer->auto_collapse_magic = NULL;
4445 if (drawer->auto_lower_magic) {
4446 WMDeleteTimerHandler(drawer->auto_lower_magic);
4447 drawer->auto_lower_magic = NULL;
4450 wAppIconDestroy(drawer->icon_array[0]);
4451 wfree(drawer->icon_array);
4452 drawer->icon_array = NULL;
4454 drawerRemoveFromChain(scr, drawer);
4455 if (scr->last_dock == drawer)
4456 scr->last_dock = NULL;
4457 if (scr->attracting_drawer == drawer)
4458 scr->attracting_drawer = NULL;
4460 wfree(drawer);
4464 static void removeDrawerCallback(WMenu *menu, WMenuEntry *entry)
4466 WDock *dock = ((WAppIcon*)entry->clientdata)->dock;
4468 /* Parameter not used, but tell the compiler that it is ok */
4469 (void) menu;
4471 assert(dock != NULL);
4473 if (dock->icon_count > 2) {
4474 if (wMessageDialog(dock->screen_ptr, _("Drawer"),
4475 _("All icons in this drawer will be detached!"),
4476 _("OK"), _("Cancel"), NULL) != WAPRDefault)
4477 return;
4479 drawerDestroy(dock);
4483 void wDrawerIconPaint(WAppIcon *dicon)
4485 Window win = dicon->icon->core->window;
4486 WScreen *scr = dicon->icon->core->screen_ptr;
4487 XPoint p[4];
4488 GC gc = scr->draw_gc;
4489 WMColor *color;
4491 wIconPaint(dicon->icon);
4493 if (!dicon->dock->collapsed)
4494 color = scr->clip_title_color[CLIP_NORMAL];
4495 else
4496 color = scr->clip_title_color[CLIP_COLLAPSED];
4497 XSetForeground(dpy, gc, WMColorPixel(color));
4499 if (dicon->dock->on_right_side) {
4500 p[0].x = p[3].x = 10;
4501 p[0].y = p[3].y = ICON_SIZE / 2 - 5;
4502 p[1].x = 10;
4503 p[1].y = ICON_SIZE / 2 + 5;
4504 p[2].x = 5;
4505 p[2].y = ICON_SIZE / 2;
4507 else {
4508 p[0].x = p[3].x = ICON_SIZE-1 - 10;
4509 p[0].y = p[3].y = ICON_SIZE / 2 - 5;
4510 p[1].x = ICON_SIZE-1 - 10;
4511 p[1].y = ICON_SIZE / 2 + 5;
4512 p[2].x = ICON_SIZE-1 - 5;
4513 p[2].y = ICON_SIZE / 2;
4515 XFillPolygon(dpy, win, gc, p,3,Convex,CoordModeOrigin);
4516 XDrawLines(dpy, win, gc, p,4,CoordModeOrigin);
4520 RImage* wDrawerMakeTile(WScreen *scr, RImage *normalTile)
4522 RImage *tile = RCloneImage(normalTile);
4523 RColor dark;
4524 RColor light;
4526 dark.alpha = 0;
4527 dark.red = dark.green = dark.blue = 60;
4529 light.alpha = 0;
4530 light.red = light.green = light.blue = 80;
4532 /* arrow bevel */
4533 if (!scr->dock || scr->dock->on_right_side) {
4534 ROperateLine(tile, RSubtractOperation, 11, ICON_SIZE / 2 - 7,
4535 4, ICON_SIZE / 2, &dark); /* / */
4536 ROperateLine(tile, RSubtractOperation, 11, ICON_SIZE / 2 + 7,
4537 4, ICON_SIZE / 2, &dark); /* \ */
4538 ROperateLine(tile, RAddOperation, 11, ICON_SIZE / 2 - 7,
4539 11, ICON_SIZE / 2 + 7, &light); /* | */
4541 else {
4542 ROperateLine(tile, RSubtractOperation, ICON_SIZE-1 - 11, ICON_SIZE / 2 - 7,
4543 ICON_SIZE-1 - 4, ICON_SIZE / 2, &dark); /* \ */
4544 ROperateLine(tile, RAddOperation, ICON_SIZE-1 - 11, ICON_SIZE / 2 + 7,
4545 ICON_SIZE-1 - 4, ICON_SIZE / 2, &light); /* / */
4546 ROperateLine(tile, RSubtractOperation, ICON_SIZE-1 - 11, ICON_SIZE / 2 - 7,
4547 ICON_SIZE-1 - 11, ICON_SIZE / 2 + 7, &dark); /* | */
4549 return tile;
4553 static void swapDrawer(WDock *drawer, int new_x)
4555 int i;
4557 drawer->on_right_side = !drawer->on_right_side;
4558 drawer->x_pos = new_x;
4560 for (i = 0; i < drawer->max_icons; i++) {
4561 WAppIcon *ai;
4562 if ((ai = drawer->icon_array[i]) == NULL)
4563 continue;
4564 ai->xindex *= -1; /* so A B C becomes C B A */
4565 ai->x_pos = new_x + ai->xindex * ICON_SIZE;
4567 /* Update drawer's tile */
4568 if (i == 0) {
4569 wIconUpdate(ai->icon);
4570 wDrawerIconPaint(ai);
4572 XMoveWindow(dpy, ai->icon->core->window, ai->x_pos, ai->y_pos);
4577 static void swapDrawers(WScreen *scr, int new_x)
4579 WDrawerChain *dc;
4581 if (scr->drawer_tile)
4582 RReleaseImage(scr->drawer_tile);
4584 scr->drawer_tile = wDrawerMakeTile(scr, scr->icon_tile);
4586 for (dc = scr->drawers; dc != NULL; dc = dc->next)
4587 swapDrawer(dc->adrawer, new_x);
4591 int wIsADrawer(WAppIcon *aicon)
4593 return aicon && aicon->dock &&
4594 aicon->dock->type == WM_DRAWER && aicon->dock->icon_array[0] == aicon;
4598 static WDock* getDrawer(WScreen *scr, int y_index)
4600 WDrawerChain *dc;
4602 for (dc = scr->drawers; dc != NULL; dc = dc->next) {
4603 if (dc->adrawer->y_pos - scr->dock->y_pos == y_index * ICON_SIZE)
4604 return dc->adrawer;
4606 return NULL;
4611 /* Find the "hole" a moving appicon created when snapped into the
4612 * drawer. redocking is a boolean. If the moving appicon comes from the
4613 * drawer, drawer->icon_count is correct. If not, redocking is then false and
4614 * there are now drawer->icon_count plus one appicons in the drawer. */
4615 static int indexOfHole(WDock *drawer, WAppIcon *moving_aicon, int redocking)
4617 int index_of_hole, i;
4619 /* Classic interview question...
4621 * We have n-1 (n = drawer->icon_count-1 or drawer->icon_count, see
4622 * redocking) appicons, whose xindex are unique in [1..n]. One is missing:
4623 * that's where the ghost of the moving appicon is, that's what the
4624 * function should return.
4626 * We compute 1+2+...+n (this sum is equal to n*(n+1)/2), we substract to
4627 * this sum the xindex of each of the n-1 appicons, and we get the correct
4628 * index! */
4630 if (redocking) {
4631 index_of_hole = (drawer->icon_count - 1) * drawer->icon_count / 2;
4632 } else {
4633 index_of_hole = drawer->icon_count * (drawer->icon_count + 1) / 2;
4635 index_of_hole *= (drawer->on_right_side ? -1 : 1);
4637 for (i = 1; i < drawer->max_icons; i++) {
4638 if (drawer->icon_array[i] && drawer->icon_array[i] != moving_aicon)
4639 index_of_hole -= drawer->icon_array[i]->xindex;
4641 /* wmessage(" Index of the moving appicon is %d (%sredocking)", index_of_hole, (redocking ? "" : "not ")); */
4642 if (abs(index_of_hole) > abs(drawer->icon_count) - (redocking ? 1 : 0))
4643 wwarning(" index_of_hole is too large ! (%d greater than %d)",
4644 index_of_hole, abs(drawer->icon_count) - (redocking ? 1 : 0));
4645 if (index_of_hole == 0)
4646 wwarning(" index_of_hole == 0 (%sredocking, icon_count == %d)", (redocking ? "" : "not "), drawer->icon_count);
4648 return index_of_hole;
4652 void wSlideAppicons(WAppIcon **appicons, int n, int to_the_left)
4654 int i;
4655 int leftmost = -1, min_index = 9999, from_x = -1; // leftmost and from_x initialized to avoid warning
4656 Window *wins[n];
4657 WAppIcon *aicon;
4659 if (n < 1)
4660 return;
4662 for (i = 0; i < n; i++) {
4663 aicon = appicons[i];
4664 aicon->xindex += (to_the_left ? -1 : +1);
4665 if (aicon->xindex < min_index) {
4666 min_index = aicon->xindex;
4667 leftmost = i;
4668 from_x = aicon->x_pos;
4670 aicon->x_pos += (to_the_left ? -ICON_SIZE : +ICON_SIZE);
4673 for (i = 0; i < n; i++) {
4674 aicon = appicons[i];
4675 wins[aicon->xindex - min_index] = &aicon->icon->core->window;
4677 aicon = appicons[leftmost];
4678 SlideWindows(wins, n, from_x, aicon->y_pos, aicon->x_pos, aicon->y_pos);
4682 void wDrawerFillTheGap(WDock *drawer, WAppIcon *aicon, Bool redocking)
4684 int i, j;
4685 int index_of_hole = indexOfHole(drawer, aicon, redocking);
4686 WAppIcon *aicons_to_shift[drawer->icon_count];
4688 j = 0;
4689 for (i = 0; i < drawer->max_icons; i++) {
4690 WAppIcon *ai = drawer->icon_array[i];
4691 if (ai && ai != aicon &&
4692 abs(ai->xindex) > abs(index_of_hole))
4693 aicons_to_shift[j++] = ai;
4695 if (j != drawer->icon_count - abs(index_of_hole) - (redocking ? 1 : 0))
4696 wwarning("Removing aicon at index %d from %s: j=%d but should be %d",
4697 index_of_hole, drawer->icon_array[0]->wm_instance,
4698 j, drawer->icon_count - abs(index_of_hole) - (redocking ? 1 : 0));
4699 wSlideAppicons(aicons_to_shift, j, !drawer->on_right_side);
4703 static void drawerConsolidateIcons(WDock *drawer)
4705 WAppIcon *aicons_to_shift[drawer->icon_count];
4706 int maxRemaining = 0;
4707 int sum = 0;
4708 int i;
4709 for (i = 0; i < drawer->max_icons; i++) {
4710 WAppIcon *ai = drawer->icon_array[i];
4711 if (ai == NULL)
4712 continue;
4713 sum += abs(ai->xindex);
4714 if (abs(ai->xindex) > maxRemaining)
4715 maxRemaining = abs(ai->xindex);
4717 while (sum != maxRemaining * (maxRemaining + 1) / 2) { // while there is a hole
4718 WAppIcon *ai;
4719 int n;
4720 // Look up for the hole at max position
4721 int maxDeleted;
4722 for (maxDeleted = maxRemaining - 1; maxDeleted > 0; maxDeleted--) {
4723 Bool foundAppIconThere = False;
4724 for (i = 0; i < drawer->max_icons; i++) {
4725 WAppIcon *ai = drawer->icon_array[i];
4726 if (ai == NULL)
4727 continue;
4728 if (abs(ai->xindex) == maxDeleted) {
4729 foundAppIconThere = True;
4730 break;
4733 if (!foundAppIconThere)
4734 break;
4736 assert(maxDeleted > 0); // would mean while test is wrong
4737 n = 0;
4738 for (i = 0; i < drawer->max_icons; i++) {
4739 ai = drawer->icon_array[i];
4740 if (ai != NULL && abs(ai->xindex) > maxDeleted)
4741 aicons_to_shift[n++] = ai;
4743 assert(n == maxRemaining - maxDeleted); // for the code review ;-)
4744 wSlideAppicons(aicons_to_shift, n, !drawer->on_right_side);
4745 // Efficient beancounting
4746 maxRemaining -= 1;
4747 sum -= n;
4753 /* similar to wDockRestoreState, but a lot a specific stuff too... */
4754 static WDock * drawerRestoreState(WScreen *scr, WMPropList *drawer_state)
4756 WDock *drawer;
4757 WMPropList *apps, *value, *dock_state;
4758 WAppIcon *aicon;
4759 int count, i;
4761 if (!drawer_state)
4762 return NULL;
4764 make_keys();
4766 WMRetainPropList(drawer_state);
4768 /* Get the instance name, and create a drawer */
4769 value = WMGetFromPLDictionary(drawer_state, dName);
4770 drawer = wDockCreate(scr, WM_DRAWER, WMGetFromPLString(value));
4772 /* restore DnD command and paste command */
4773 #ifdef XDND
4774 value = WMGetFromPLDictionary(drawer_state, dDropCommand);
4775 if (value && WMIsPLString(value))
4776 drawer->icon_array[0]->dnd_command = wstrdup(WMGetFromPLString(value));
4777 #endif /* XDND */
4779 value = WMGetFromPLDictionary(drawer_state, dPasteCommand);
4780 if (value && WMIsPLString(value))
4781 drawer->icon_array[0]->paste_command = wstrdup(WMGetFromPLString(value));
4783 /* restore position */
4784 value = WMGetFromPLDictionary(drawer_state, dPosition);
4785 if (!value || !WMIsPLString(value))
4786 COMPLAIN("Position");
4787 else {
4788 int x, y, y_index;
4789 if (sscanf(WMGetFromPLString(value), "%i,%i", &x, &y) != 2)
4790 COMPLAIN("Position");
4792 /* check position sanity */
4793 if (x != scr->dock->x_pos) {
4794 x = scr->dock->x_pos;
4796 y_index = (y - scr->dock->y_pos) / ICON_SIZE;
4797 if (y_index >= scr->dock->max_icons) {
4798 /* Here we should do something more intelligent, since it
4799 * can happen even if the user hasn't hand-edited his
4800 * G/D/State file (but uses a lower resolution). */
4801 y_index = scr->dock->max_icons - 1;
4803 y = scr->dock->y_pos + y_index * ICON_SIZE;
4804 moveDock(drawer, x, y);
4807 /* restore dock properties (applist and others) */
4808 dock_state = WMGetFromPLDictionary(drawer_state, dDock);
4810 /* restore lowered/raised state: same as scr->dock, no matter what */
4811 drawer->lowered = scr->dock->lowered;
4812 if (!drawer->lowered)
4813 ChangeStackingLevel(drawer->icon_array[0]->icon->core, WMDockLevel);
4814 else
4815 ChangeStackingLevel(drawer->icon_array[0]->icon->core, WMNormalLevel);
4816 wRaiseFrame(drawer->icon_array[0]->icon->core);
4818 /* restore collapsed state */
4819 drawer->collapsed = 0;
4820 value = WMGetFromPLDictionary(dock_state, dCollapsed);
4821 if (value && strcasecmp(WMGetFromPLString(value), "YES") == 0) {
4822 drawer->collapsed = 1;
4824 /* restore auto-collapsed state */
4825 value = WMGetFromPLDictionary(dock_state, dAutoCollapse);
4826 if (value && strcasecmp(WMGetFromPLString(value), "YES") == 0) {
4827 drawer->auto_collapse = 1;
4828 drawer->collapsed = 1;
4829 } else {
4830 drawer->auto_collapse = 0; // because wDockCreate sets it (drawers only)
4833 /* restore auto-raise/lower state: same as scr->dock, no matter what */
4834 drawer->auto_raise_lower = scr->dock->auto_raise_lower;
4836 /* restore attract icons state */
4837 drawer->attract_icons = 0;
4838 value = WMGetFromPLDictionary(dock_state, dAutoAttractIcons);
4839 if (value && strcasecmp(WMGetFromPLString(value), "YES") == 0) {
4840 drawer->attract_icons = 1;
4841 scr->attracting_drawer = drawer;
4844 /* application list */
4845 apps = WMGetFromPLDictionary(dock_state, dApplications);
4847 if (!apps) {
4848 goto finish;
4851 count = WMGetPropListItemCount(apps);
4853 if (count == 0)
4854 goto finish;
4856 for (i=0; i<count; i++) {
4857 if (drawer->icon_count >= drawer->max_icons) {
4858 wwarning(_("there are too many icons stored in drawer. Ignoring what doesn't fit"));
4859 break;
4862 value = WMGetFromPLArray(apps, i);
4863 aicon = restore_icon_state(scr, value, WM_DRAWER, drawer->icon_count);
4865 drawer->icon_array[drawer->icon_count] = aicon;
4867 if (aicon) {
4868 aicon->dock = drawer;
4869 aicon->x_pos = drawer->x_pos + (aicon->xindex * ICON_SIZE);
4870 aicon->y_pos = drawer->y_pos + (aicon->yindex * ICON_SIZE);
4872 if (!drawer->lowered)
4873 ChangeStackingLevel(aicon->icon->core, WMDockLevel);
4874 else
4875 ChangeStackingLevel(aicon->icon->core, WMNormalLevel);
4877 wCoreConfigure(aicon->icon->core, aicon->x_pos, aicon->y_pos, 0, 0);
4879 if (!drawer->collapsed)
4880 XMapWindow(dpy, aicon->icon->core->window);
4881 wRaiseFrame(aicon->icon->core);
4883 drawer->icon_count++;
4887 finish:
4888 WMReleasePropList(drawer_state);
4890 return drawer;
4894 /* Same kind of comment than for previous function: this function is
4895 * very similar to make_icon_state, but has substential differences as
4896 * well. */
4897 static WMPropList *drawerSaveState(WDock *drawer)
4899 WMPropList *pstr, *drawer_state;
4900 WAppIcon *ai;
4901 char buffer[64];
4903 ai = drawer->icon_array[0];
4904 /* Store its name */
4905 pstr = WMCreatePLString(ai->wm_instance);
4906 drawer_state = WMCreatePLDictionary(dName, pstr, NULL); /* we need this final NULL */
4907 WMReleasePropList(pstr);
4909 /* Store its position */
4910 snprintf(buffer, sizeof(buffer), "%i,%i", ai->x_pos, ai->y_pos);
4911 pstr = WMCreatePLString(buffer);
4912 WMPutInPLDictionary(drawer_state, dPosition, pstr);
4913 WMReleasePropList(pstr);
4915 #ifdef XDND
4916 /* Store its DnD command */
4917 if (ai->dnd_command) {
4918 pstr = WMCreatePLString(ai->dnd_command);
4919 WMPutInPLDictionary(drawer_state, dDropCommand, pstr);
4920 WMReleasePropList(pstr);
4922 #endif /* XDND */
4924 /* Store its paste command */
4925 if (ai->paste_command) {
4926 pstr = WMCreatePLString(ai->paste_command);
4927 WMPutInPLDictionary(drawer_state, dPasteCommand, pstr);
4928 WMReleasePropList(pstr);
4931 /* Store applications list and other properties */
4932 pstr = dockSaveState(drawer);
4933 WMPutInPLDictionary(drawer_state, dDock, pstr);
4934 WMReleasePropList(pstr);
4936 return drawer_state;
4940 void wDrawersSaveState(WScreen *scr)
4942 WMPropList *all_drawers, *drawer_state;
4943 int i;
4944 WDrawerChain *dc;
4946 make_keys();
4948 all_drawers = WMCreatePLArray(NULL);
4949 for (i=0, dc = scr->drawers;
4950 i < scr->drawer_count;
4951 i++, dc = dc->next) {
4952 drawer_state = drawerSaveState(dc->adrawer);
4953 WMAddToPLArray(all_drawers, drawer_state);
4954 WMReleasePropList(drawer_state);
4956 WMPutInPLDictionary(scr->session_state, dDrawers, all_drawers);
4957 WMReleasePropList(all_drawers);
4961 void wDrawersRestoreState(WScreen *scr)
4963 WMPropList *all_drawers, *drawer_state;
4964 int i;
4966 make_keys();
4968 if (scr->session_state == NULL)
4969 return;
4971 all_drawers = WMGetFromPLDictionary(scr->session_state, dDrawers);
4972 if (!all_drawers)
4973 return;
4975 for (i = 0; i < WMGetPropListItemCount(all_drawers); i++) {
4976 drawer_state = WMGetFromPLArray(all_drawers, i);
4977 drawerRestoreState(scr, drawer_state);
4978 // Note: scr->drawers was updated when the the drawer was created