Fix removal of drawer with a single appicon
[wmaker-crm.git] / src / dock.c
blob29091e3ea9a4e0302f12915b85efc448b17bd302
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 on_right_side, 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 assert(entry->clientdata != NULL);
174 wspace = w_global.workspace.current;
176 name = wstrdup(w_global.workspace.array[wspace]->name);
178 snprintf(buffer, sizeof(buffer), _("Type the name for workspace %i:"), wspace + 1);
179 if (wInputDialog(dock->screen_ptr, _("Rename Workspace"), buffer, &name))
180 wWorkspaceRename(dock->screen_ptr, wspace, name);
182 wfree(name);
185 static void toggleLoweredCallback(WMenu *menu, WMenuEntry *entry)
187 assert(entry->clientdata != NULL);
189 toggleLowered(entry->clientdata);
191 entry->flags.indicator_on = !(((WDock *) entry->clientdata)->lowered);
193 wMenuPaint(menu);
196 static int matchWindow(const void *item, const void *cdata)
198 return (((WFakeGroupLeader *) item)->leader == (Window) cdata);
201 static void killCallback(WMenu *menu, WMenuEntry *entry)
203 WScreen *scr = menu->menu->screen_ptr;
204 WAppIcon *icon;
205 WFakeGroupLeader *fPtr;
206 char *buffer, *shortname, **argv;
207 int argc;
209 if (!WCHECK_STATE(WSTATE_NORMAL))
210 return;
212 assert(entry->clientdata != NULL);
214 icon = (WAppIcon *) entry->clientdata;
216 icon->editing = 1;
218 WCHANGE_STATE(WSTATE_MODAL);
220 /* strip away dir names */
221 shortname = basename(icon->command);
222 /* separate out command options */
223 wtokensplit(shortname, &argv, &argc);
225 buffer = wstrconcat(argv[0],
226 _(" will be forcibly closed.\n"
227 "Any unsaved changes will be lost.\n" "Please confirm."));
229 if (icon->icon && icon->icon->owner) {
230 fPtr = icon->icon->owner->fake_group;
231 } else {
232 /* is this really necessary? can we kill a non-running dock icon? */
233 Window win = icon->main_window;
234 int index;
236 index = WMFindInArray(scr->fakeGroupLeaders, matchWindow, (void *)win);
237 if (index != WANotFound)
238 fPtr = WMGetFromArray(scr->fakeGroupLeaders, index);
239 else
240 fPtr = NULL;
243 if (wPreferences.dont_confirm_kill
244 || wMessageDialog(menu->frame->screen_ptr, _("Kill Application"),
245 buffer, _("Yes"), _("No"), NULL) == WAPRDefault) {
246 if (fPtr != NULL) {
247 WWindow *wwin, *twin;
249 wwin = scr->focused_window;
250 while (wwin) {
251 twin = wwin->prev;
252 if (wwin->fake_group == fPtr)
253 wClientKill(wwin);
255 wwin = twin;
257 } else if (icon->icon && icon->icon->owner) {
258 wClientKill(icon->icon->owner);
262 wfree(buffer);
263 wtokenfree(argv, argc);
265 icon->editing = 0;
267 WCHANGE_STATE(WSTATE_NORMAL);
270 /* TODO: replace this function with a member of the dock struct */
271 static int numberOfSelectedIcons(WDock *dock)
273 WAppIcon *aicon;
274 int i, n;
276 n = 0;
277 for (i = 1; i < dock->max_icons; i++) {
278 aicon = dock->icon_array[i];
279 if (aicon && aicon->icon->selected)
280 n++;
283 return n;
286 static WMArray *getSelected(WDock *dock)
288 WMArray *ret = WMCreateArray(8);
289 WAppIcon *btn;
290 int i;
292 for (i = 1; i < dock->max_icons; i++) {
293 btn = dock->icon_array[i];
294 if (btn && btn->icon->selected)
295 WMAddToArray(ret, btn);
298 return ret;
301 static void paintClipButtons(WAppIcon *clipIcon, Bool lpushed, Bool rpushed)
303 Window win = clipIcon->icon->core->window;
304 WScreen *scr = clipIcon->icon->core->screen_ptr;
305 XPoint p[4];
306 int pt = CLIP_BUTTON_SIZE * ICON_SIZE / 64;
307 int tp = ICON_SIZE - pt;
308 int as = pt - 15; /* 15 = 5+5+5 */
309 GC gc = scr->draw_gc; /* maybe use WMColorGC() instead here? */
310 WMColor *color;
312 color = scr->clip_title_color[CLIP_NORMAL];
314 XSetForeground(dpy, gc, WMColorPixel(color));
316 if (rpushed) {
317 p[0].x = tp + 1;
318 p[0].y = 1;
319 p[1].x = ICON_SIZE - 2;
320 p[1].y = 1;
321 p[2].x = ICON_SIZE - 2;
322 p[2].y = pt - 1;
323 } else if (lpushed) {
324 p[0].x = 1;
325 p[0].y = tp;
326 p[1].x = pt;
327 p[1].y = ICON_SIZE - 2;
328 p[2].x = 1;
329 p[2].y = ICON_SIZE - 2;
331 if (lpushed || rpushed) {
332 XSetForeground(dpy, scr->draw_gc, scr->white_pixel);
333 XFillPolygon(dpy, win, scr->draw_gc, p, 3, Convex, CoordModeOrigin);
334 XSetForeground(dpy, scr->draw_gc, scr->black_pixel);
337 /* top right arrow */
338 p[0].x = p[3].x = ICON_SIZE - 5 - as;
339 p[0].y = p[3].y = 5;
340 p[1].x = ICON_SIZE - 6;
341 p[1].y = 5;
342 p[2].x = ICON_SIZE - 6;
343 p[2].y = 4 + as;
344 if (rpushed) {
345 XFillPolygon(dpy, win, scr->draw_gc, p, 3, Convex, CoordModeOrigin);
346 XDrawLines(dpy, win, scr->draw_gc, p, 4, CoordModeOrigin);
347 } else {
348 XFillPolygon(dpy, win, gc, p, 3, Convex, CoordModeOrigin);
349 XDrawLines(dpy, win, gc, p, 4, CoordModeOrigin);
352 /* bottom left arrow */
353 p[0].x = p[3].x = 5;
354 p[0].y = p[3].y = ICON_SIZE - 5 - as;
355 p[1].x = 5;
356 p[1].y = ICON_SIZE - 6;
357 p[2].x = 4 + as;
358 p[2].y = ICON_SIZE - 6;
359 if (lpushed) {
360 XFillPolygon(dpy, win, scr->draw_gc, p, 3, Convex, CoordModeOrigin);
361 XDrawLines(dpy, win, scr->draw_gc, p, 4, CoordModeOrigin);
362 } else {
363 XFillPolygon(dpy, win, gc, p, 3, Convex, CoordModeOrigin);
364 XDrawLines(dpy, win, gc, p, 4, CoordModeOrigin);
368 RImage *wClipMakeTile(WScreen *scr, RImage *normalTile)
370 RImage *tile = RCloneImage(normalTile);
371 RColor black;
372 RColor dark;
373 RColor light;
374 int pt, tp;
375 int as;
377 pt = CLIP_BUTTON_SIZE * wPreferences.icon_size / 64;
378 tp = wPreferences.icon_size - 1 - pt;
379 as = pt - 15;
381 black.alpha = 255;
382 black.red = black.green = black.blue = 0;
384 dark.alpha = 0;
385 dark.red = dark.green = dark.blue = 60;
387 light.alpha = 0;
388 light.red = light.green = light.blue = 80;
390 /* top right */
391 ROperateLine(tile, RSubtractOperation, tp, 0, wPreferences.icon_size - 2, pt - 1, &dark);
392 RDrawLine(tile, tp - 1, 0, wPreferences.icon_size - 1, pt + 1, &black);
393 ROperateLine(tile, RAddOperation, tp, 2, wPreferences.icon_size - 3, pt, &light);
395 /* arrow bevel */
396 ROperateLine(tile, RSubtractOperation, ICON_SIZE - 7 - as, 4, ICON_SIZE - 5, 4, &dark);
397 ROperateLine(tile, RSubtractOperation, ICON_SIZE - 6 - as, 5, ICON_SIZE - 5, 6 + as, &dark);
398 ROperateLine(tile, RAddOperation, ICON_SIZE - 5, 4, ICON_SIZE - 5, 6 + as, &light);
400 /* bottom left */
401 ROperateLine(tile, RAddOperation, 2, tp + 2, pt - 2, wPreferences.icon_size - 3, &dark);
402 RDrawLine(tile, 0, tp - 1, pt + 1, wPreferences.icon_size - 1, &black);
403 ROperateLine(tile, RSubtractOperation, 0, tp - 2, pt + 1, wPreferences.icon_size - 2, &light);
405 /* arrow bevel */
406 ROperateLine(tile, RSubtractOperation, 4, ICON_SIZE - 7 - as, 4, ICON_SIZE - 5, &dark);
407 ROperateLine(tile, RSubtractOperation, 5, ICON_SIZE - 6 - as, 6 + as, ICON_SIZE - 5, &dark);
408 ROperateLine(tile, RAddOperation, 4, ICON_SIZE - 5, 6 + as, ICON_SIZE - 5, &light);
410 return tile;
413 static void omnipresentCallback(WMenu *menu, WMenuEntry *entry)
415 WAppIcon *clickedIcon = entry->clientdata;
416 WAppIcon *aicon;
417 WDock *dock;
418 WMArray *selectedIcons;
419 WMArrayIterator iter;
420 int failed;
422 assert(entry->clientdata != NULL);
424 dock = clickedIcon->dock;
426 selectedIcons = getSelected(dock);
428 if (!WMGetArrayItemCount(selectedIcons))
429 WMAddToArray(selectedIcons, clickedIcon);
431 failed = 0;
432 WM_ITERATE_ARRAY(selectedIcons, aicon, iter) {
433 if (wClipMakeIconOmnipresent(aicon, !aicon->omnipresent) == WO_FAILED)
434 failed++;
435 else if (aicon->icon->selected)
436 wIconSelect(aicon->icon);
438 WMFreeArray(selectedIcons);
440 if (failed > 1) {
441 wMessageDialog(dock->screen_ptr, _("Warning"),
442 _("Some icons cannot be made omnipresent. "
443 "Please make sure that no other icon is "
444 "docked in the same positions on the other "
445 "workspaces and the Clip is not full in "
446 "some workspace."), _("OK"), NULL, NULL);
447 } else if (failed == 1) {
448 wMessageDialog(dock->screen_ptr, _("Warning"),
449 _("Icon cannot be made omnipresent. "
450 "Please make sure that no other icon is "
451 "docked in the same position on the other "
452 "workspaces and the Clip is not full in "
453 "some workspace."), _("OK"), NULL, NULL);
457 static void removeIcons(WMArray *icons, WDock *dock)
459 WAppIcon *aicon;
460 int keepit;
461 WMArrayIterator it;
463 WM_ITERATE_ARRAY(icons, aicon, it) {
464 keepit = aicon->running && wApplicationOf(aicon->main_window);
465 wDockDetach(dock, aicon);
466 if (keepit) {
467 /* XXX: can: aicon->icon == NULL ? */
468 PlaceIcon(dock->screen_ptr, &aicon->x_pos, &aicon->y_pos,
469 wGetHeadForWindow(aicon->icon->owner));
470 XMoveWindow(dpy, aicon->icon->core->window, aicon->x_pos, aicon->y_pos);
471 if (!dock->mapped || dock->collapsed)
472 XMapWindow(dpy, aicon->icon->core->window);
475 WMFreeArray(icons);
477 if (wPreferences.auto_arrange_icons)
478 wArrangeIcons(dock->screen_ptr, True);
481 static void removeIconsCallback(WMenu *menu, WMenuEntry *entry)
483 WAppIcon *clickedIcon = (WAppIcon *) entry->clientdata;
484 WDock *dock;
485 WMArray *selectedIcons;
487 assert(clickedIcon != NULL);
489 dock = clickedIcon->dock;
491 selectedIcons = getSelected(dock);
493 if (WMGetArrayItemCount(selectedIcons)) {
494 if (wMessageDialog(dock->screen_ptr,
495 dock->type == WM_CLIP ? _("Workspace Clip") : _("Drawer"),
496 _("All selected icons will be removed!"),
497 _("OK"), _("Cancel"), NULL) != WAPRDefault) {
498 WMFreeArray(selectedIcons);
499 return;
501 } else {
502 if (clickedIcon->xindex == 0 && clickedIcon->yindex == 0) {
503 WMFreeArray(selectedIcons);
504 return;
506 WMAddToArray(selectedIcons, clickedIcon);
509 removeIcons(selectedIcons, dock);
511 if (dock->type == WM_DRAWER) {
512 drawerConsolidateIcons(dock);
516 static void keepIconsCallback(WMenu *menu, WMenuEntry *entry)
518 WAppIcon *clickedIcon = (WAppIcon *) entry->clientdata;
519 WDock *dock;
520 WAppIcon *aicon;
521 WMArray *selectedIcons;
522 WMArrayIterator it;
524 assert(clickedIcon != NULL);
525 dock = clickedIcon->dock;
527 selectedIcons = getSelected(dock);
529 if (!WMGetArrayItemCount(selectedIcons)
530 && clickedIcon != w_global.clip.icon) {
531 char *command = NULL;
533 if (!clickedIcon->command && !clickedIcon->editing) {
534 clickedIcon->editing = 1;
535 if (wInputDialog(dock->screen_ptr, _("Keep Icon"),
536 _("Type the command used to launch the application"), &command)) {
537 if (command && (command[0] == 0 || (command[0] == '-' && command[1] == 0))) {
538 wfree(command);
539 command = NULL;
541 clickedIcon->command = command;
542 clickedIcon->editing = 0;
543 } else {
544 clickedIcon->editing = 0;
545 if (command)
546 wfree(command);
547 WMFreeArray(selectedIcons);
548 return;
552 WMAddToArray(selectedIcons, clickedIcon);
555 WM_ITERATE_ARRAY(selectedIcons, aicon, it) {
556 if (aicon->icon->selected)
557 wIconSelect(aicon->icon);
559 if (aicon && aicon->attracted && aicon->command) {
560 aicon->attracted = 0;
561 if (aicon->icon->shadowed) {
562 aicon->icon->shadowed = 0;
565 * Update icon pixmap, RImage doesn't change,
566 * so call wIconUpdate is not needed
568 update_icon_pixmap(aicon->icon);
570 /* Paint it */
571 wAppIconPaint(aicon);
574 save_appicon(aicon, True);
576 WMFreeArray(selectedIcons);
579 static void toggleAutoAttractCallback(WMenu *menu, WMenuEntry *entry)
581 WDock *dock = (WDock *) entry->clientdata;
582 WScreen *scr = dock->screen_ptr;
584 assert(entry->clientdata != NULL);
586 /* Setting a clip as autoattracting will disable an autoattracting drawer,
587 * and reciprocally. Ask for confirmation? */
588 #ifdef CONFIRM_AUTOATTRACT_CHANGE
589 if (dock->type == WM_CLIP && scr->attracting_drawer) {
590 if (wMessageDialog(scr, _("Workspace Clip"),
591 _("The drawer that auto-attracted icons so far won't do so anymore!"),
592 _("OK"), _("Cancel"), NULL) != WAPRDefault)
593 return;
594 } else if (dock->type == WM_DRAWER) {
595 /* check if at least one clip already auto attracts icons */
596 int i, ask_for_confirmation = False;
597 for (i = 0; i < w_global.workspace.count; i++) {
598 if (w_global.workspace.array[i]->clip->attract_icons) {
599 ask_for_confirmation = True;
600 break;
603 if (ask_for_confirmation) {
604 if (wMessageDialog(scr, _("Drawer"),
605 _("Any clips that auto-attracted icons so far won't do so anymore!"),
606 _("OK"), _("Cancel"), NULL) != WAPRDefault)
607 return;
610 #endif
612 dock->attract_icons = !dock->attract_icons;
614 entry->flags.indicator_on = dock->attract_icons;
616 wMenuPaint(menu);
618 if (dock->attract_icons) {
619 if (dock->type == WM_DRAWER) {
620 /* The newly auto-attracting dock is a drawer: disable any clip and
621 * previously attracting drawer */
623 if (!wPreferences.flags.noclip) {
624 int i;
625 for (i = 0; i < w_global.workspace.count; i++)
626 w_global.workspace.array[i]->clip->attract_icons = False;
627 /* dock menu will be updated later, when opened */
630 if (scr->attracting_drawer != NULL)
631 scr->attracting_drawer->attract_icons = False;
632 scr->attracting_drawer = dock;
633 } else {
634 /* The newly auto-attracting dock is a clip: disable
635 * previously attracting drawer, if applicable */
636 if (scr->attracting_drawer != NULL) {
637 scr->attracting_drawer->attract_icons = False;
638 /* again, its menu will be updated, later. */
639 scr->attracting_drawer = NULL;
645 static void selectCallback(WMenu *menu, WMenuEntry *entry)
647 WAppIcon *icon = (WAppIcon *) entry->clientdata;
649 assert(icon != NULL);
651 wIconSelect(icon->icon);
653 wMenuPaint(menu);
656 static void colectIconsCallback(WMenu *menu, WMenuEntry *entry)
658 WAppIcon *clickedIcon = (WAppIcon *) entry->clientdata;
659 WDock *clip; /* clip... is a WM_CLIP or a WM_DRAWER */
660 WAppIcon *aicon;
661 int x, y, x_pos, y_pos;
662 Bool update_icon = False;
664 assert(entry->clientdata != NULL);
665 clip = clickedIcon->dock;
667 aicon = w_global.app_icon_list;
669 while (aicon) {
670 if (!aicon->docked && wDockFindFreeSlot(clip, &x, &y)) {
671 x_pos = clip->x_pos + x * ICON_SIZE;
672 y_pos = clip->y_pos + y * ICON_SIZE;
673 if (aicon->x_pos != x_pos || aicon->y_pos != y_pos)
674 move_window(aicon->icon->core->window, aicon->x_pos, aicon->y_pos, x_pos, y_pos);
676 aicon->attracted = 1;
677 if (!aicon->icon->shadowed) {
678 aicon->icon->shadowed = 1;
679 update_icon = True;
681 wDockAttachIcon(clip, aicon, x, y, update_icon);
682 if (clip->collapsed || !clip->mapped)
683 XUnmapWindow(dpy, aicon->icon->core->window);
685 aicon = aicon->next;
689 static void selectIconsCallback(WMenu *menu, WMenuEntry *entry)
691 WAppIcon *clickedIcon = (WAppIcon *) entry->clientdata;
692 WDock *dock;
693 WMArray *selectedIcons;
694 WMArrayIterator iter;
695 WAppIcon *btn;
696 int i;
698 assert(clickedIcon != NULL);
699 dock = clickedIcon->dock;
701 selectedIcons = getSelected(dock);
703 if (!WMGetArrayItemCount(selectedIcons)) {
704 for (i = 1; i < dock->max_icons; i++) {
705 btn = dock->icon_array[i];
706 if (btn && !btn->icon->selected)
707 wIconSelect(btn->icon);
709 } else {
710 WM_ITERATE_ARRAY(selectedIcons, btn, iter) {
711 wIconSelect(btn->icon);
714 WMFreeArray(selectedIcons);
716 wMenuPaint(menu);
719 static void toggleCollapsedCallback(WMenu *menu, WMenuEntry *entry)
721 assert(entry->clientdata != NULL);
723 toggleCollapsed(entry->clientdata);
725 entry->flags.indicator_on = ((WDock *) entry->clientdata)->collapsed;
727 wMenuPaint(menu);
730 static void toggleAutoCollapseCallback(WMenu *menu, WMenuEntry *entry)
732 WDock *dock;
733 assert(entry->clientdata != NULL);
735 dock = (WDock *) entry->clientdata;
737 dock->auto_collapse = !dock->auto_collapse;
739 entry->flags.indicator_on = ((WDock *) entry->clientdata)->auto_collapse;
741 wMenuPaint(menu);
744 static void toggleAutoRaiseLower(WDock *dock)
746 WDrawerChain *dc;
748 dock->auto_raise_lower = !dock->auto_raise_lower;
749 if (dock->type == WM_DOCK)
751 for (dc = dock->screen_ptr->drawers; dc != NULL; dc = dc->next) {
752 toggleAutoRaiseLower(dc->adrawer);
757 static void toggleAutoRaiseLowerCallback(WMenu *menu, WMenuEntry *entry)
759 WDock *dock;
760 assert(entry->clientdata != NULL);
762 dock = (WDock *) entry->clientdata;
764 toggleAutoRaiseLower(dock);
766 entry->flags.indicator_on = ((WDock *) entry->clientdata)->auto_raise_lower;
768 wMenuPaint(menu);
771 static void launchCallback(WMenu *menu, WMenuEntry *entry)
773 WAppIcon *btn = (WAppIcon *) entry->clientdata;
775 launchDockedApplication(btn, False);
778 static void settingsCallback(WMenu *menu, WMenuEntry *entry)
780 WAppIcon *btn = (WAppIcon *) entry->clientdata;
782 if (btn->editing)
783 return;
784 ShowDockAppSettingsPanel(btn);
787 static void hideCallback(WMenu *menu, WMenuEntry *entry)
789 WApplication *wapp;
790 WAppIcon *btn = (WAppIcon *) entry->clientdata;
792 wapp = wApplicationOf(btn->icon->owner->main_window);
794 if (wapp->flags.hidden) {
795 wWorkspaceChange(btn->icon->core->screen_ptr, wapp->last_workspace);
796 wUnhideApplication(wapp, False, False);
797 } else {
798 wHideApplication(wapp);
802 static void unhideHereCallback(WMenu *menu, WMenuEntry *entry)
804 WApplication *wapp;
805 WAppIcon *btn = (WAppIcon *) entry->clientdata;
807 wapp = wApplicationOf(btn->icon->owner->main_window);
809 wUnhideApplication(wapp, False, True);
812 /* Name is only used when type == WM_DRAWER and when restoring a specific
813 * drawer, with a specific name. When creating a drawer, leave name to NULL
814 * and mainIconCreate will find the first unused unique name */
815 static WAppIcon *mainIconCreate(WScreen *scr, int type, const char *name)
817 WAppIcon *btn;
818 int x_pos;
820 switch(type) {
821 case WM_CLIP:
822 if (w_global.clip.icon)
823 return w_global.clip.icon;
825 btn = wAppIconCreateForDock(scr, NULL, "Logo", "WMClip", TILE_CLIP);
826 btn->icon->core->descriptor.handle_expose = clipIconExpose;
827 x_pos = 0;
828 break;
829 case WM_DOCK:
830 default: /* to avoid a warning about btn and x_pos, basically */
831 btn = wAppIconCreateForDock(scr, NULL, "Logo", "WMDock", TILE_NORMAL);
832 if (wPreferences.flags.clip_merged_in_dock)
833 btn->icon->core->descriptor.handle_expose = clipIconExpose;
834 x_pos = scr->scr_width - ICON_SIZE - DOCK_EXTRA_SPACE;
835 break;
836 case WM_DRAWER:
837 if (name == NULL)
838 name = findUniqueName(scr, "Drawer");
839 btn = wAppIconCreateForDock(scr, NULL, name, "WMDrawer", TILE_DRAWER);
840 btn->icon->core->descriptor.handle_expose = drawerIconExpose;
841 x_pos = 0;
844 btn->xindex = 0;
845 btn->yindex = 0;
847 btn->icon->core->descriptor.handle_mousedown = iconMouseDown;
848 btn->icon->core->descriptor.handle_enternotify = clipEnterNotify;
849 btn->icon->core->descriptor.handle_leavenotify = clipLeaveNotify;
850 btn->icon->core->descriptor.parent_type = WCLASS_DOCK_ICON;
851 btn->icon->core->descriptor.parent = btn;
852 XMapWindow(dpy, btn->icon->core->window);
853 btn->x_pos = x_pos;
854 btn->y_pos = 0;
855 btn->docked = 1;
856 if (type == WM_CLIP ||
857 (type == WM_DOCK && wPreferences.flags.clip_merged_in_dock))
858 w_global.clip.icon = btn;
860 return btn;
863 static void switchWSCommand(WMenu *menu, WMenuEntry *entry)
865 WAppIcon *btn, *icon = (WAppIcon *) entry->clientdata;
866 WDock *src, *dest;
867 WMArray *selectedIcons;
868 int x, y;
870 if (entry->order == w_global.workspace.current)
871 return;
872 src = icon->dock;
873 dest = w_global.workspace.array[entry->order]->clip;
875 selectedIcons = getSelected(src);
877 if (WMGetArrayItemCount(selectedIcons)) {
878 WMArrayIterator iter;
880 WM_ITERATE_ARRAY(selectedIcons, btn, iter) {
881 if (wDockFindFreeSlot(dest, &x, &y)) {
882 wDockMoveIconBetweenDocks(src, dest, btn, x, y);
883 XUnmapWindow(dpy, btn->icon->core->window);
886 } else if (icon != w_global.clip.icon) {
887 if (wDockFindFreeSlot(dest, &x, &y)) {
888 wDockMoveIconBetweenDocks(src, dest, icon, x, y);
889 XUnmapWindow(dpy, icon->icon->core->window);
892 WMFreeArray(selectedIcons);
895 static void launchDockedApplication(WAppIcon *btn, Bool withSelection)
897 WScreen *scr = btn->icon->core->screen_ptr;
899 if (!btn->launching &&
900 ((!withSelection && btn->command != NULL) || (withSelection && btn->paste_command != NULL))) {
901 if (!btn->forced_dock) {
902 btn->relaunching = btn->running;
903 btn->running = 1;
905 if (btn->wm_instance || btn->wm_class) {
906 WWindowAttributes attr;
907 memset(&attr, 0, sizeof(WWindowAttributes));
908 wDefaultFillAttributes(btn->wm_instance, btn->wm_class, &attr, NULL, True);
910 if (!attr.no_appicon && !btn->buggy_app)
911 btn->launching = 1;
912 else
913 btn->running = 0;
915 btn->drop_launch = 0;
916 btn->paste_launch = withSelection;
917 scr->last_dock = btn->dock;
918 btn->pid = execCommand(btn, (withSelection ? btn->paste_command : btn->command), NULL);
919 if (btn->pid > 0) {
920 if (btn->buggy_app) {
921 /* give feedback that the app was launched */
922 btn->launching = 1;
923 dockIconPaint(btn);
924 btn->launching = 0;
925 WMAddTimerHandler(200, (WMCallback *) dockIconPaint, btn);
926 } else {
927 dockIconPaint(btn);
929 } else {
930 wwarning(_("could not launch application %s"), btn->command);
931 btn->launching = 0;
932 if (!btn->relaunching)
933 btn->running = 0;
938 static void updateWorkspaceMenu(WMenu *menu, WAppIcon *icon)
940 char title[MAX_WORKSPACENAME_WIDTH + 1];
941 int i;
943 if (!menu || !icon)
944 return;
946 for (i = 0; i < w_global.workspace.count; i++) {
947 if (i < menu->entry_no) {
948 if (strcmp(menu->entries[i]->text, w_global.workspace.array[i]->name) != 0) {
949 wfree(menu->entries[i]->text);
950 strcpy(title, w_global.workspace.array[i]->name);
951 menu->entries[i]->text = wstrdup(title);
952 menu->flags.realized = 0;
954 menu->entries[i]->clientdata = (void *)icon;
955 } else {
956 strcpy(title, w_global.workspace.array[i]->name);
958 wMenuAddCallback(menu, title, switchWSCommand, (void *)icon);
960 menu->flags.realized = 0;
963 if (i == w_global.workspace.current)
964 wMenuSetEnabled(menu, i, False);
965 else
966 wMenuSetEnabled(menu, i, True);
969 if (!menu->flags.realized)
970 wMenuRealize(menu);
973 static WMenu *makeWorkspaceMenu(WScreen *scr)
975 WMenu *menu;
977 menu = wMenuCreate(scr, NULL, False);
978 if (!menu)
979 wwarning(_("could not create workspace submenu for Clip menu"));
981 wMenuAddCallback(menu, "", switchWSCommand, (void *)w_global.clip.icon);
983 menu->flags.realized = 0;
984 wMenuRealize(menu);
986 return menu;
989 static void updateClipOptionsMenu(WMenu *menu, WDock *dock)
991 WMenuEntry *entry;
992 int index = 0;
994 if (!menu || !dock)
995 return;
997 /* keep on top */
998 entry = menu->entries[index];
999 entry->flags.indicator_on = !dock->lowered;
1000 entry->clientdata = dock;
1001 wMenuSetEnabled(menu, index, dock->type == WM_CLIP);
1003 /* collapsed */
1004 entry = menu->entries[++index];
1005 entry->flags.indicator_on = dock->collapsed;
1006 entry->clientdata = dock;
1008 /* auto-collapse */
1009 entry = menu->entries[++index];
1010 entry->flags.indicator_on = dock->auto_collapse;
1011 entry->clientdata = dock;
1013 /* auto-raise/lower */
1014 entry = menu->entries[++index];
1015 entry->flags.indicator_on = dock->auto_raise_lower;
1016 entry->clientdata = dock;
1017 wMenuSetEnabled(menu, index, dock->lowered && (dock->type == WM_CLIP));
1019 /* attract icons */
1020 entry = menu->entries[++index];
1021 entry->flags.indicator_on = dock->attract_icons;
1022 entry->clientdata = dock;
1024 menu->flags.realized = 0;
1025 wMenuRealize(menu);
1029 static WMenu *makeClipOptionsMenu(WScreen *scr)
1031 WMenu *menu;
1032 WMenuEntry *entry;
1034 menu = wMenuCreate(scr, NULL, False);
1035 if (!menu) {
1036 wwarning(_("could not create options submenu for Clip menu"));
1037 return NULL;
1040 entry = wMenuAddCallback(menu, _("Keep on Top"), toggleLoweredCallback, NULL);
1041 entry->flags.indicator = 1;
1042 entry->flags.indicator_on = 1;
1043 entry->flags.indicator_type = MI_CHECK;
1045 entry = wMenuAddCallback(menu, _("Collapsed"), toggleCollapsedCallback, NULL);
1046 entry->flags.indicator = 1;
1047 entry->flags.indicator_on = 1;
1048 entry->flags.indicator_type = MI_CHECK;
1050 entry = wMenuAddCallback(menu, _("Autocollapse"), toggleAutoCollapseCallback, NULL);
1051 entry->flags.indicator = 1;
1052 entry->flags.indicator_on = 1;
1053 entry->flags.indicator_type = MI_CHECK;
1055 entry = wMenuAddCallback(menu, _("Autoraise"), toggleAutoRaiseLowerCallback, NULL);
1056 entry->flags.indicator = 1;
1057 entry->flags.indicator_on = 1;
1058 entry->flags.indicator_type = MI_CHECK;
1060 entry = wMenuAddCallback(menu, _("Autoattract Icons"), toggleAutoAttractCallback, NULL);
1061 entry->flags.indicator = 1;
1062 entry->flags.indicator_on = 1;
1063 entry->flags.indicator_type = MI_CHECK;
1065 menu->flags.realized = 0;
1066 wMenuRealize(menu);
1068 return menu;
1072 static void setDockPositionNormalCallback(WMenu *menu, WMenuEntry *entry)
1074 WDock *dock = (WDock *) entry->clientdata;
1075 WDrawerChain *dc;
1076 if (entry->flags.indicator_on) // already set, nothing to do
1077 return;
1078 // Do we come from auto raise lower or keep on top?
1079 if (dock->auto_raise_lower)
1081 dock->auto_raise_lower = 0;
1082 // Only for aesthetic purposes, can be removed when Autoraise status is no longer exposed in drawer option menu
1083 for (dc = dock->screen_ptr->drawers; dc != NULL; dc = dc->next) {
1084 dc->adrawer->auto_raise_lower = 0;
1087 else
1089 // Will take care of setting lowered = 0 in drawers
1090 toggleLowered(dock);
1092 entry->flags.indicator_on = 1;
1095 static void setDockPositionAutoRaiseLowerCallback(WMenu *menu, WMenuEntry *entry)
1097 WDock *dock = (WDock *) entry->clientdata;
1098 WDrawerChain *dc;
1099 if (entry->flags.indicator_on) // already set, nothing to do
1100 return;
1101 // Do we come from normal or keep on top?
1102 if (!dock->lowered)
1104 toggleLowered(dock);
1106 dock->auto_raise_lower = 1;
1107 // Only for aesthetic purposes, can be removed when Autoraise status is no longer exposed in drawer option menu
1108 for (dc = dock->screen_ptr->drawers; dc != NULL; dc = dc->next) {
1109 dc->adrawer->auto_raise_lower = 1;
1111 entry->flags.indicator_on = 1;
1114 static void setDockPositionKeepOnTopCallback(WMenu *menu, WMenuEntry *entry)
1116 WDock *dock = (WDock *) entry->clientdata;
1117 WDrawerChain *dc;
1118 if (entry->flags.indicator_on) // already set, nothing to do
1119 return;
1120 dock->auto_raise_lower = 0;
1121 // Only for aesthetic purposes, can be removed when Autoraise status is no longer exposed in drawer option menu
1122 for (dc = dock->screen_ptr->drawers; dc != NULL; dc = dc->next) {
1123 dc->adrawer->auto_raise_lower = 0;
1125 toggleLowered(dock);
1126 entry->flags.indicator_on = 1;
1129 static void updateDockPositionMenu(WMenu *menu, WDock *dock)
1131 WMenuEntry *entry;
1132 int index = 0;
1134 assert(menu);
1135 assert(dock);
1137 /* Normal level */
1138 entry = menu->entries[index++];
1139 entry->flags.indicator_on = (dock->lowered && !dock->auto_raise_lower);
1140 entry->clientdata = dock;
1142 /* Auto-raise/lower */
1143 entry = menu->entries[index++];
1144 entry->flags.indicator_on = dock->auto_raise_lower;
1145 entry->clientdata = dock;
1147 /* Keep on top */
1148 entry = menu->entries[index++];
1149 entry->flags.indicator_on = !dock->lowered;
1150 entry->clientdata = dock;
1153 static WMenu *makeDockPositionMenu(WScreen *scr)
1155 /* When calling this, the dock is being created, so scr->dock is still not set
1156 * Therefore the callbacks' clientdata and the indicators can't be set,
1157 * they will be updated when the dock menu is opened. */
1158 WMenu *menu;
1159 WMenuEntry *entry;
1161 menu = wMenuCreate(scr, NULL, False);
1162 if (!menu) {
1163 wwarning(_("could not create options submenu for dock position menu"));
1164 return NULL;
1167 entry = wMenuAddCallback(menu, _("Normal"), setDockPositionNormalCallback, NULL);
1168 entry->flags.indicator = 1;
1169 entry->flags.indicator_type = MI_DIAMOND;
1171 entry = wMenuAddCallback(menu, _("Auto raise & lower"), setDockPositionAutoRaiseLowerCallback, NULL);
1172 entry->flags.indicator = 1;
1173 entry->flags.indicator_type = MI_DIAMOND;
1175 entry = wMenuAddCallback(menu, _("Keep on Top"), setDockPositionKeepOnTopCallback, NULL);
1176 entry->flags.indicator = 1;
1177 entry->flags.indicator_type = MI_DIAMOND;
1179 menu->flags.realized = 0;
1180 wMenuRealize(menu);
1182 return menu;
1186 static WMenu *dockMenuCreate(WScreen *scr, int type)
1188 WMenu *menu;
1189 WMenuEntry *entry;
1191 if (type == WM_CLIP && w_global.clip.menu)
1192 return w_global.clip.menu;
1194 if (type == WM_DRAWER && w_global.dock.drawer_menu)
1195 return w_global.dock.drawer_menu;
1197 menu = wMenuCreate(scr, NULL, False);
1198 if (type == WM_DOCK) {
1199 entry = wMenuAddCallback(menu, _("Dock position"), NULL, NULL);
1200 if (w_global.dock.pos_menu == NULL)
1201 w_global.dock.pos_menu = makeDockPositionMenu(scr);
1202 wMenuEntrySetCascade(menu, entry, w_global.dock.pos_menu);
1204 if (!wPreferences.flags.nodrawer) {
1205 entry = wMenuAddCallback(menu, _("Add a drawer"), addADrawerCallback, NULL);
1207 } else {
1208 if (type == WM_CLIP)
1209 entry = wMenuAddCallback(menu, _("Clip Options"), NULL, NULL);
1210 else /* if (type == WM_DRAWER) */
1211 entry = wMenuAddCallback(menu, _("Drawer options"), NULL, NULL);
1213 if (w_global.clip.opt_menu == NULL)
1214 w_global.clip.opt_menu = makeClipOptionsMenu(scr);
1216 wMenuEntrySetCascade(menu, entry, w_global.clip.opt_menu);
1218 /* The same menu is used for the dock and its appicons. If the menu
1219 * entry text is different between the two contexts, or if it can
1220 * change depending on some state, free the duplicated string (from
1221 * wMenuInsertCallback) and use gettext's string */
1222 if (type == WM_CLIP) {
1223 entry = wMenuAddCallback(menu, _("Rename Workspace"), renameCallback, NULL);
1224 wfree(entry->text);
1225 entry->text = _("Rename Workspace"); /* can be: (Toggle) Omnipresent */
1228 entry = wMenuAddCallback(menu, _("Selected"), selectCallback, NULL);
1229 entry->flags.indicator = 1;
1230 entry->flags.indicator_on = 1;
1231 entry->flags.indicator_type = MI_CHECK;
1233 entry = wMenuAddCallback(menu, _("Select All Icons"), selectIconsCallback, NULL);
1234 wfree(entry->text);
1235 entry->text = _("Select All Icons"); /* can be: Unselect all icons */
1237 entry = wMenuAddCallback(menu, _("Keep Icon"), keepIconsCallback, NULL);
1238 wfree(entry->text);
1239 entry->text = _("Keep Icon"); /* can be: Keep Icons */
1241 if (type == WM_CLIP) {
1242 entry = wMenuAddCallback(menu, _("Move Icon To"), NULL, NULL);
1243 wfree(entry->text);
1244 entry->text = _("Move Icon To"); /* can be: Move Icons to */
1245 w_global.clip.submenu = makeWorkspaceMenu(scr);
1246 if (w_global.clip.submenu)
1247 wMenuEntrySetCascade(menu, entry, w_global.clip.submenu);
1250 entry = wMenuAddCallback(menu, _("Remove Icon"), removeIconsCallback, NULL);
1251 wfree(entry->text);
1252 entry->text = _("Remove Icon"); /* can be: Remove Icons */
1254 wMenuAddCallback(menu, _("Attract Icons"), colectIconsCallback, NULL);
1257 wMenuAddCallback(menu, _("Launch"), launchCallback, NULL);
1259 wMenuAddCallback(menu, _("Unhide Here"), unhideHereCallback, NULL);
1261 entry = wMenuAddCallback(menu, _("Hide"), hideCallback, NULL);
1262 wfree(entry->text);
1263 entry->text = _("Hide"); /* can be: Unhide */
1265 wMenuAddCallback(menu, _("Settings..."), settingsCallback, NULL);
1267 entry = wMenuAddCallback(menu, _("Kill"), killCallback, NULL);
1268 wfree(entry->text);
1269 entry->text = _("Kill"); /* can be: Remove drawer */
1271 if (type == WM_CLIP)
1272 w_global.clip.menu = menu;
1274 if (type == WM_DRAWER)
1275 w_global.dock.drawer_menu = menu;
1277 return menu;
1280 WDock *wDockCreate(WScreen *scr, int type, const char *name)
1282 WDock *dock;
1283 WAppIcon *btn;
1285 make_keys();
1287 dock = wmalloc(sizeof(WDock));
1289 switch (type) {
1290 case WM_CLIP:
1291 dock->max_icons = DOCK_MAX_ICONS;
1292 break;
1293 case WM_DRAWER:
1294 dock->max_icons = scr->scr_width / wPreferences.icon_size;
1295 break;
1296 case WM_DOCK:
1297 default:
1298 dock->max_icons = scr->scr_height / wPreferences.icon_size;
1301 dock->icon_array = wmalloc(sizeof(WAppIcon *) * dock->max_icons);
1303 btn = mainIconCreate(scr, type, name);
1305 btn->dock = dock;
1307 dock->x_pos = btn->x_pos;
1308 dock->y_pos = btn->y_pos;
1309 dock->screen_ptr = scr;
1310 dock->type = type;
1311 dock->icon_count = 1;
1312 if (type == WM_DRAWER)
1313 dock->on_right_side = scr->dock->on_right_side;
1314 else
1315 dock->on_right_side = 1;
1316 dock->collapsed = 0;
1317 dock->auto_collapse = 0;
1318 dock->auto_collapse_magic = NULL;
1319 dock->auto_raise_lower = 0;
1320 dock->auto_lower_magic = NULL;
1321 dock->auto_raise_magic = NULL;
1322 dock->attract_icons = 0;
1323 dock->lowered = 1;
1324 dock->icon_array[0] = btn;
1325 wRaiseFrame(btn->icon->core);
1326 XMoveWindow(dpy, btn->icon->core->window, btn->x_pos, btn->y_pos);
1328 /* create dock menu */
1329 dock->menu = dockMenuCreate(scr, type);
1331 if (type == WM_DRAWER) {
1332 drawerAppendToChain(scr, dock);
1333 dock->auto_collapse = 1;
1336 return dock;
1339 void wDockDestroy(WDock *dock)
1341 int i;
1342 WAppIcon *aicon;
1344 for (i = (dock->type == WM_CLIP) ? 1 : 0; i < dock->max_icons; i++) {
1345 aicon = dock->icon_array[i];
1346 if (aicon) {
1347 int keepit = aicon->running && wApplicationOf(aicon->main_window);
1348 wDockDetach(dock, aicon);
1349 if (keepit) {
1350 /* XXX: can: aicon->icon == NULL ? */
1351 PlaceIcon(dock->screen_ptr, &aicon->x_pos, &aicon->y_pos,
1352 wGetHeadForWindow(aicon->icon->owner));
1353 XMoveWindow(dpy, aicon->icon->core->window, aicon->x_pos, aicon->y_pos);
1354 if (!dock->mapped || dock->collapsed)
1355 XMapWindow(dpy, aicon->icon->core->window);
1359 if (wPreferences.auto_arrange_icons)
1360 wArrangeIcons(dock->screen_ptr, True);
1361 wfree(dock->icon_array);
1362 if (dock->menu && dock->type != WM_CLIP)
1363 wMenuDestroy(dock->menu, True);
1364 if (dock->screen_ptr->last_dock == dock)
1365 dock->screen_ptr->last_dock = NULL;
1366 wfree(dock);
1369 void wClipIconPaint(void)
1371 WAppIcon *aicon = w_global.clip.icon;
1372 WScreen *scr = aicon->icon->core->screen_ptr;
1373 WWorkspace *workspace = w_global.workspace.array[w_global.workspace.current];
1374 WMColor *color;
1375 Window win = aicon->icon->core->window;
1376 int length, nlength;
1377 char *ws_name, ws_number[10];
1378 int ty, tx;
1380 wIconPaint(aicon->icon);
1382 length = strlen(workspace->name);
1383 ws_name = wmalloc(length + 1);
1384 snprintf(ws_name, length + 1, "%s", workspace->name);
1385 snprintf(ws_number, sizeof(ws_number), "%i", w_global.workspace.current + 1);
1386 nlength = strlen(ws_number);
1388 if (wPreferences.flags.noclip || !workspace->clip->collapsed)
1389 color = scr->clip_title_color[CLIP_NORMAL];
1390 else
1391 color = scr->clip_title_color[CLIP_COLLAPSED];
1393 ty = ICON_SIZE - WMFontHeight(scr->clip_title_font) - 3;
1395 tx = CLIP_BUTTON_SIZE * ICON_SIZE / 64;
1397 if(wPreferences.show_clip_title)
1398 WMDrawString(scr->wmscreen, win, color, scr->clip_title_font, tx, ty, ws_name, length);
1400 tx = (ICON_SIZE / 2 - WMWidthOfString(scr->clip_title_font, ws_number, nlength)) / 2;
1402 WMDrawString(scr->wmscreen, win, color, scr->clip_title_font, tx, 2, ws_number, nlength);
1404 wfree(ws_name);
1406 if (aicon->launching)
1407 XFillRectangle(dpy, aicon->icon->core->window, scr->stipple_gc,
1408 0, 0, wPreferences.icon_size, wPreferences.icon_size);
1410 paintClipButtons(aicon, aicon->dock->lclip_button_pushed, aicon->dock->rclip_button_pushed);
1413 static void clipIconExpose(WObjDescriptor *desc, XEvent *event)
1415 wClipIconPaint();
1418 static void dockIconPaint(WAppIcon *btn)
1420 if (btn == w_global.clip.icon) {
1421 wClipIconPaint();
1422 } else if (wIsADrawer(btn)) {
1423 wDrawerIconPaint(btn);
1424 } else {
1425 wAppIconPaint(btn);
1426 save_appicon(btn, True);
1430 static WMPropList *make_icon_state(WAppIcon *btn)
1432 WMPropList *node = NULL;
1433 WMPropList *command, *autolaunch, *lock, *name, *forced;
1434 WMPropList *position, *buggy, *omnipresent;
1435 char *tmp;
1436 char buffer[64];
1438 if (btn) {
1439 if (!btn->command)
1440 command = WMCreatePLString("-");
1441 else
1442 command = WMCreatePLString(btn->command);
1444 autolaunch = btn->auto_launch ? dYes : dNo;
1446 lock = btn->lock ? dYes : dNo;
1448 tmp = EscapeWM_CLASS(btn->wm_instance, btn->wm_class);
1450 name = WMCreatePLString(tmp);
1452 wfree(tmp);
1454 forced = btn->forced_dock ? dYes : dNo;
1456 buggy = btn->buggy_app ? dYes : dNo;
1458 if (!wPreferences.flags.clip_merged_in_dock && btn == w_global.clip.icon)
1459 snprintf(buffer, sizeof(buffer), "%i,%i", btn->x_pos, btn->y_pos);
1460 else
1461 snprintf(buffer, sizeof(buffer), "%hi,%hi", btn->xindex, btn->yindex);
1462 position = WMCreatePLString(buffer);
1464 node = WMCreatePLDictionary(dCommand, command,
1465 dName, name,
1466 dAutoLaunch, autolaunch,
1467 dLock, lock,
1468 dForced, forced, dBuggyApplication, buggy, dPosition, position, NULL);
1469 WMReleasePropList(command);
1470 WMReleasePropList(name);
1471 WMReleasePropList(position);
1473 omnipresent = btn->omnipresent ? dYes : dNo;
1474 if (btn->dock != btn->icon->core->screen_ptr->dock && (btn->xindex != 0 || btn->yindex != 0))
1475 WMPutInPLDictionary(node, dOmnipresent, omnipresent);
1477 #ifdef XDND /* was OFFIX */
1478 if (btn->dnd_command) {
1479 command = WMCreatePLString(btn->dnd_command);
1480 WMPutInPLDictionary(node, dDropCommand, command);
1481 WMReleasePropList(command);
1483 #endif /* XDND */
1485 if (btn->paste_command) {
1486 command = WMCreatePLString(btn->paste_command);
1487 WMPutInPLDictionary(node, dPasteCommand, command);
1488 WMReleasePropList(command);
1492 return node;
1495 static WMPropList *dockSaveState(WDock *dock)
1497 int i;
1498 WMPropList *icon_info;
1499 WMPropList *list = NULL, *dock_state = NULL;
1500 WMPropList *value, *key;
1501 char buffer[256];
1503 list = WMCreatePLArray(NULL);
1505 for (i = (dock->type == WM_DOCK ? 0 : 1); i < dock->max_icons; i++) {
1506 WAppIcon *btn = dock->icon_array[i];
1508 if (!btn || btn->attracted)
1509 continue;
1511 if ((icon_info = make_icon_state(dock->icon_array[i]))) {
1512 WMAddToPLArray(list, icon_info);
1513 WMReleasePropList(icon_info);
1517 dock_state = WMCreatePLDictionary(dApplications, list, NULL);
1519 if (dock->type == WM_DOCK) {
1520 snprintf(buffer, sizeof(buffer), "Applications%i", dock->screen_ptr->scr_height);
1521 key = WMCreatePLString(buffer);
1522 WMPutInPLDictionary(dock_state, key, list);
1523 WMReleasePropList(key);
1525 snprintf(buffer, sizeof(buffer), "%i,%i", (dock->on_right_side ? -ICON_SIZE : 0), dock->y_pos);
1526 value = WMCreatePLString(buffer);
1527 WMPutInPLDictionary(dock_state, dPosition, value);
1528 WMReleasePropList(value);
1530 WMReleasePropList(list);
1532 if (dock->type == WM_CLIP || dock->type == WM_DRAWER) {
1533 value = (dock->collapsed ? dYes : dNo);
1534 WMPutInPLDictionary(dock_state, dCollapsed, value);
1536 value = (dock->auto_collapse ? dYes : dNo);
1537 WMPutInPLDictionary(dock_state, dAutoCollapse, value);
1539 value = (dock->attract_icons ? dYes : dNo);
1540 WMPutInPLDictionary(dock_state, dAutoAttractIcons, value);
1543 if (dock->type == WM_DOCK || dock->type == WM_CLIP) {
1544 value = (dock->lowered ? dYes : dNo);
1545 WMPutInPLDictionary(dock_state, dLowered, value);
1547 value = (dock->auto_raise_lower ? dYes : dNo);
1548 WMPutInPLDictionary(dock_state, dAutoRaiseLower, value);
1551 return dock_state;
1554 void wDockSaveState(WScreen *scr, WMPropList *old_state)
1556 WMPropList *dock_state;
1557 WMPropList *keys;
1559 dock_state = dockSaveState(scr->dock);
1562 * Copy saved states of docks with different sizes.
1564 if (old_state) {
1565 int i;
1566 WMPropList *tmp;
1568 keys = WMGetPLDictionaryKeys(old_state);
1569 for (i = 0; i < WMGetPropListItemCount(keys); i++) {
1570 tmp = WMGetFromPLArray(keys, i);
1572 if (strncasecmp(WMGetFromPLString(tmp), "applications", 12) == 0
1573 && !WMGetFromPLDictionary(dock_state, tmp)) {
1575 WMPutInPLDictionary(dock_state, tmp, WMGetFromPLDictionary(old_state, tmp));
1578 WMReleasePropList(keys);
1581 WMPutInPLDictionary(w_global.session_state, dDock, dock_state);
1582 WMReleasePropList(dock_state);
1585 void wClipSaveState(WScreen *scr)
1587 WMPropList *clip_state;
1589 clip_state = make_icon_state(w_global.clip.icon);
1591 WMPutInPLDictionary(w_global.session_state, dClip, clip_state);
1592 WMReleasePropList(clip_state);
1595 WMPropList *wClipSaveWorkspaceState(WScreen *scr, int workspace)
1597 return dockSaveState(w_global.workspace.array[workspace]->clip);
1600 static Bool getBooleanDockValue(WMPropList *value, WMPropList *key)
1602 if (value) {
1603 if (WMIsPLString(value)) {
1604 if (strcasecmp(WMGetFromPLString(value), "YES") == 0)
1605 return True;
1606 } else {
1607 wwarning(_("bad value in docked icon state info %s"), WMGetFromPLString(key));
1610 return False;
1613 static WAppIcon *restore_icon_state(WScreen *scr, WMPropList *info, int type, int index)
1615 WAppIcon *aicon;
1616 WMPropList *cmd, *value;
1617 char *wclass, *winstance, *command;
1619 cmd = WMGetFromPLDictionary(info, dCommand);
1620 if (!cmd || !WMIsPLString(cmd))
1621 return NULL;
1623 /* parse window name */
1624 value = WMGetFromPLDictionary(info, dName);
1625 if (!value)
1626 return NULL;
1628 ParseWindowName(value, &winstance, &wclass, "dock");
1630 if (!winstance && !wclass)
1631 return NULL;
1633 /* get commands */
1634 if (cmd)
1635 command = wstrdup(WMGetFromPLString(cmd));
1636 else
1637 command = NULL;
1639 if (!command || strcmp(command, "-") == 0) {
1640 if (command)
1641 wfree(command);
1642 if (wclass)
1643 wfree(wclass);
1644 if (winstance)
1645 wfree(winstance);
1647 return NULL;
1650 aicon = wAppIconCreateForDock(scr, command, winstance, wclass, TILE_NORMAL);
1651 if (wclass)
1652 wfree(wclass);
1653 if (winstance)
1654 wfree(winstance);
1655 if (command)
1656 wfree(command);
1658 aicon->icon->core->descriptor.handle_mousedown = iconMouseDown;
1659 aicon->icon->core->descriptor.handle_enternotify = clipEnterNotify;
1660 aicon->icon->core->descriptor.handle_leavenotify = clipLeaveNotify;
1661 aicon->icon->core->descriptor.parent_type = WCLASS_DOCK_ICON;
1662 aicon->icon->core->descriptor.parent = aicon;
1664 #ifdef XDND /* was OFFIX */
1665 cmd = WMGetFromPLDictionary(info, dDropCommand);
1666 if (cmd)
1667 aicon->dnd_command = wstrdup(WMGetFromPLString(cmd));
1668 #endif
1670 cmd = WMGetFromPLDictionary(info, dPasteCommand);
1671 if (cmd)
1672 aicon->paste_command = wstrdup(WMGetFromPLString(cmd));
1674 /* check auto launch */
1675 value = WMGetFromPLDictionary(info, dAutoLaunch);
1677 aicon->auto_launch = getBooleanDockValue(value, dAutoLaunch);
1679 /* check lock */
1680 value = WMGetFromPLDictionary(info, dLock);
1682 aicon->lock = getBooleanDockValue(value, dLock);
1684 /* check if it wasn't normally docked */
1685 value = WMGetFromPLDictionary(info, dForced);
1687 aicon->forced_dock = getBooleanDockValue(value, dForced);
1689 /* check if we can rely on the stuff in the app */
1690 value = WMGetFromPLDictionary(info, dBuggyApplication);
1692 aicon->buggy_app = getBooleanDockValue(value, dBuggyApplication);
1694 /* get position in the dock */
1695 value = WMGetFromPLDictionary(info, dPosition);
1696 if (value && WMIsPLString(value)) {
1697 if (sscanf(WMGetFromPLString(value), "%hi,%hi", &aicon->xindex, &aicon->yindex) != 2)
1698 wwarning(_("bad value in docked icon state info %s"), WMGetFromPLString(dPosition));
1700 /* check position sanity */
1701 /* *Very* incomplete section! */
1702 if (type == WM_DOCK) {
1703 aicon->xindex = 0;
1705 } else {
1706 aicon->yindex = index;
1707 aicon->xindex = 0;
1710 /* check if icon is omnipresent */
1711 value = WMGetFromPLDictionary(info, dOmnipresent);
1713 aicon->omnipresent = getBooleanDockValue(value, dOmnipresent);
1715 aicon->running = 0;
1716 aicon->docked = 1;
1718 return aicon;
1721 #define COMPLAIN(key) wwarning(_("bad value in dock state info:%s"), key)
1723 WAppIcon *wClipRestoreState(WScreen *scr, WMPropList *clip_state)
1725 WAppIcon *icon;
1726 WMPropList *value;
1728 icon = mainIconCreate(scr, WM_CLIP, NULL);
1730 if (!clip_state)
1731 return icon;
1733 WMRetainPropList(clip_state);
1735 /* restore position */
1737 value = WMGetFromPLDictionary(clip_state, dPosition);
1739 if (value) {
1740 if (!WMIsPLString(value)) {
1741 COMPLAIN("Position");
1742 } else {
1743 if (sscanf(WMGetFromPLString(value), "%i,%i", &icon->x_pos, &icon->y_pos) != 2)
1744 COMPLAIN("Position");
1746 /* check position sanity */
1747 if (!onScreen(scr, icon->x_pos, icon->y_pos))
1748 wScreenKeepInside(scr, &icon->x_pos, &icon->y_pos, ICON_SIZE, ICON_SIZE);
1751 #ifdef XDND /* was OFFIX */
1752 value = WMGetFromPLDictionary(clip_state, dDropCommand);
1753 if (value && WMIsPLString(value))
1754 icon->dnd_command = wstrdup(WMGetFromPLString(value));
1755 #endif
1757 value = WMGetFromPLDictionary(clip_state, dPasteCommand);
1758 if (value && WMIsPLString(value))
1759 icon->paste_command = wstrdup(WMGetFromPLString(value));
1761 WMReleasePropList(clip_state);
1763 return icon;
1766 WDock *wDockRestoreState(WScreen *scr, WMPropList *dock_state, int type)
1768 WDock *dock;
1769 WMPropList *apps;
1770 WMPropList *value;
1771 WAppIcon *aicon, *old_top;
1772 int count, i;
1774 dock = wDockCreate(scr, type, NULL);
1776 if (!dock_state)
1777 return dock;
1779 WMRetainPropList(dock_state);
1781 /* restore position */
1782 value = WMGetFromPLDictionary(dock_state, dPosition);
1783 if (value) {
1784 if (!WMIsPLString(value)) {
1785 COMPLAIN("Position");
1786 } else {
1787 if (sscanf(WMGetFromPLString(value), "%i,%i", &dock->x_pos, &dock->y_pos) != 2)
1788 COMPLAIN("Position");
1790 /* check position sanity */
1791 if (!onScreen(scr, dock->x_pos, dock->y_pos)) {
1792 int x = dock->x_pos;
1793 wScreenKeepInside(scr, &x, &dock->y_pos, ICON_SIZE, ICON_SIZE);
1796 /* Is this needed any more? */
1797 if (type == WM_CLIP) {
1798 if (dock->x_pos < 0) {
1799 dock->x_pos = 0;
1800 } else if (dock->x_pos > scr->scr_width - ICON_SIZE) {
1801 dock->x_pos = scr->scr_width - ICON_SIZE;
1803 } else {
1804 if (dock->x_pos >= 0) {
1805 dock->x_pos = DOCK_EXTRA_SPACE;
1806 dock->on_right_side = 0;
1807 } else {
1808 dock->x_pos = scr->scr_width - DOCK_EXTRA_SPACE - ICON_SIZE;
1809 dock->on_right_side = 1;
1815 /* restore lowered/raised state */
1816 dock->lowered = 0;
1818 value = WMGetFromPLDictionary(dock_state, dLowered);
1819 if (value) {
1820 if (!WMIsPLString(value)) {
1821 COMPLAIN("Lowered");
1822 } else {
1823 if (strcasecmp(WMGetFromPLString(value), "YES") == 0)
1824 dock->lowered = 1;
1828 /* restore collapsed state */
1829 dock->collapsed = 0;
1831 value = WMGetFromPLDictionary(dock_state, dCollapsed);
1832 if (value) {
1833 if (!WMIsPLString(value)) {
1834 COMPLAIN("Collapsed");
1835 } else {
1836 if (strcasecmp(WMGetFromPLString(value), "YES") == 0)
1837 dock->collapsed = 1;
1841 /* restore auto-collapsed state */
1842 value = WMGetFromPLDictionary(dock_state, dAutoCollapse);
1843 if (value) {
1844 if (!WMIsPLString(value)) {
1845 COMPLAIN("AutoCollapse");
1846 } else {
1847 if (strcasecmp(WMGetFromPLString(value), "YES") == 0) {
1848 dock->auto_collapse = 1;
1849 dock->collapsed = 1;
1854 /* restore auto-raise/lower state */
1855 value = WMGetFromPLDictionary(dock_state, dAutoRaiseLower);
1856 if (value) {
1857 if (!WMIsPLString(value)) {
1858 COMPLAIN("AutoRaiseLower");
1859 } else {
1860 if (strcasecmp(WMGetFromPLString(value), "YES") == 0)
1861 dock->auto_raise_lower = 1;
1865 /* restore attract icons state */
1866 dock->attract_icons = 0;
1868 value = WMGetFromPLDictionary(dock_state, dAutoAttractIcons);
1869 if (value) {
1870 if (!WMIsPLString(value)) {
1871 COMPLAIN("AutoAttractIcons");
1872 } else {
1873 if (strcasecmp(WMGetFromPLString(value), "YES") == 0)
1874 dock->attract_icons = 1;
1878 /* application list */
1881 WMPropList *tmp;
1882 char buffer[64];
1885 * When saving, it saves the dock state in
1886 * Applications and Applicationsnnn
1888 * When loading, it will first try Applicationsnnn.
1889 * If it does not exist, use Applications as default.
1892 snprintf(buffer, sizeof(buffer), "Applications%i", scr->scr_height);
1894 tmp = WMCreatePLString(buffer);
1895 apps = WMGetFromPLDictionary(dock_state, tmp);
1896 WMReleasePropList(tmp);
1898 if (!apps)
1899 apps = WMGetFromPLDictionary(dock_state, dApplications);
1902 if (!apps)
1903 goto finish;
1905 count = WMGetPropListItemCount(apps);
1906 if (count == 0)
1907 goto finish;
1909 old_top = dock->icon_array[0];
1911 /* dock->icon_count is set to 1 when dock is created.
1912 * Since Clip is already restored, we want to keep it so for clip,
1913 * but for dock we may change the default top tile, so we set it to 0.
1915 if (type == WM_DOCK)
1916 dock->icon_count = 0;
1918 for (i = 0; i < count; i++) {
1919 if (dock->icon_count >= dock->max_icons) {
1920 wwarning(_("there are too many icons stored in dock. Ignoring what doesn't fit"));
1921 break;
1924 value = WMGetFromPLArray(apps, i);
1925 aicon = restore_icon_state(scr, value, type, dock->icon_count);
1927 dock->icon_array[dock->icon_count] = aicon;
1929 if (aicon) {
1930 aicon->dock = dock;
1931 aicon->x_pos = dock->x_pos + (aicon->xindex * ICON_SIZE);
1932 aicon->y_pos = dock->y_pos + (aicon->yindex * ICON_SIZE);
1934 if (dock->lowered)
1935 ChangeStackingLevel(aicon->icon->core, WMNormalLevel);
1936 else
1937 ChangeStackingLevel(aicon->icon->core, WMDockLevel);
1939 wCoreConfigure(aicon->icon->core, aicon->x_pos, aicon->y_pos, 0, 0);
1940 if (!dock->collapsed)
1941 XMapWindow(dpy, aicon->icon->core->window);
1943 wRaiseFrame(aicon->icon->core);
1945 dock->icon_count++;
1946 } else if (dock->icon_count == 0 && type == WM_DOCK) {
1947 dock->icon_count++;
1951 /* if the first icon is not defined, use the default */
1952 if (dock->icon_array[0] == NULL) {
1953 /* update default icon */
1954 old_top->x_pos = dock->x_pos;
1955 old_top->y_pos = dock->y_pos;
1956 if (dock->lowered)
1957 ChangeStackingLevel(old_top->icon->core, WMNormalLevel);
1958 else
1959 ChangeStackingLevel(old_top->icon->core, WMDockLevel);
1961 dock->icon_array[0] = old_top;
1962 XMoveWindow(dpy, old_top->icon->core->window, dock->x_pos, dock->y_pos);
1963 /* we don't need to increment dock->icon_count here because it was
1964 * incremented in the loop above.
1966 } else if (old_top != dock->icon_array[0]) {
1967 if (old_top == w_global.clip.icon) /* TODO dande: understand the logic */
1968 w_global.clip.icon = dock->icon_array[0];
1970 wAppIconDestroy(old_top);
1973 finish:
1974 WMReleasePropList(dock_state);
1976 return dock;
1979 void wDockLaunchWithState(WDock *dock, WAppIcon *btn, WSavedState *state)
1981 if (btn && btn->command && !btn->running && !btn->launching) {
1982 btn->drop_launch = 0;
1983 btn->paste_launch = 0;
1985 btn->pid = execCommand(btn, btn->command, state);
1987 if (btn->pid > 0) {
1988 if (!btn->forced_dock && !btn->buggy_app) {
1989 btn->launching = 1;
1990 dockIconPaint(btn);
1993 } else {
1994 wfree(state);
1998 void wDockDoAutoLaunch(WDock *dock, int workspace)
2000 WAppIcon *btn;
2001 WSavedState *state;
2002 int i;
2004 for (i = 0; i < dock->max_icons; i++) {
2005 btn = dock->icon_array[i];
2006 if (!btn || !btn->auto_launch)
2007 continue;
2009 state = wmalloc(sizeof(WSavedState));
2010 state->workspace = workspace;
2011 /* TODO: this is klugy and is very difficult to understand
2012 * what's going on. Try to clean up */
2013 wDockLaunchWithState(dock, btn, state);
2017 #ifdef XDND /* was OFFIX */
2018 static WDock *findDock(WScreen *scr, XEvent *event, int *icon_pos)
2020 WDock *dock;
2021 int i;
2023 *icon_pos = -1;
2024 if ((dock = scr->dock) != NULL) {
2025 for (i = 0; i < dock->max_icons; i++) {
2026 if (dock->icon_array[i]
2027 && dock->icon_array[i]->icon->core->window == event->xclient.window) {
2028 *icon_pos = i;
2029 break;
2033 if (*icon_pos < 0 && (dock = w_global.workspace.array[w_global.workspace.current]->clip) != NULL) {
2034 for (i = 0; i < dock->max_icons; i++) {
2035 if (dock->icon_array[i]
2036 && dock->icon_array[i]->icon->core->window == event->xclient.window) {
2037 *icon_pos = i;
2038 break;
2042 if (*icon_pos >= 0)
2043 return dock;
2044 return NULL;
2047 int wDockReceiveDNDDrop(WScreen *scr, XEvent *event)
2049 WDock *dock;
2050 WAppIcon *btn;
2051 int icon_pos;
2053 dock = findDock(scr, event, &icon_pos);
2054 if (!dock)
2055 return False;
2058 * Return True if the drop was on an application icon window.
2059 * In this case, let the ClientMessage handler redirect the
2060 * message to the app.
2062 if (dock->icon_array[icon_pos]->icon->icon_win != None)
2063 return True;
2065 if (dock->icon_array[icon_pos]->dnd_command != NULL) {
2066 scr->flags.dnd_data_convertion_status = 0;
2068 btn = dock->icon_array[icon_pos];
2070 if (!btn->forced_dock) {
2071 btn->relaunching = btn->running;
2072 btn->running = 1;
2074 if (btn->wm_instance || btn->wm_class) {
2075 WWindowAttributes attr;
2076 memset(&attr, 0, sizeof(WWindowAttributes));
2077 wDefaultFillAttributes(btn->wm_instance, btn->wm_class, &attr, NULL, True);
2079 if (!attr.no_appicon)
2080 btn->launching = 1;
2081 else
2082 btn->running = 0;
2085 btn->paste_launch = 0;
2086 btn->drop_launch = 1;
2087 scr->last_dock = dock;
2088 btn->pid = execCommand(btn, btn->dnd_command, NULL);
2089 if (btn->pid > 0) {
2090 dockIconPaint(btn);
2091 } else {
2092 btn->launching = 0;
2093 if (!btn->relaunching)
2094 btn->running = 0;
2097 return False;
2099 #endif /* XDND */
2101 Bool wDockAttachIcon(WDock *dock, WAppIcon *icon, int x, int y, Bool update_icon)
2103 WWindow *wwin;
2104 Bool lupdate_icon = False;
2105 char *command = NULL;
2106 int index;
2108 icon->editing = 0;
2110 if (update_icon)
2111 lupdate_icon = True;
2113 if (icon->command == NULL) {
2114 /* If icon->owner exists, it means the application is running */
2115 if (icon->icon->owner) {
2116 wwin = icon->icon->owner;
2117 command = GetCommandForWindow(wwin->client_win);
2120 if (command) {
2121 icon->command = command;
2122 } else {
2123 /* icon->forced_dock = 1; */
2124 if (dock->type != WM_CLIP || !icon->attracted) {
2125 icon->editing = 1;
2126 if (wInputDialog(dock->screen_ptr, _("Dock Icon"),
2127 _("Type the command used to launch the application"), &command)) {
2128 if (command && (command[0] == 0 || (command[0] == '-' && command[1] == 0))) {
2129 wfree(command);
2130 command = NULL;
2132 icon->command = command;
2133 icon->editing = 0;
2134 } else {
2135 icon->editing = 0;
2136 if (command)
2137 wfree(command);
2138 /* If the target is the dock, reject the icon. If
2139 * the target is the clip, make it an attracted icon
2141 if (dock->type == WM_CLIP) {
2142 icon->attracted = 1;
2143 if (!icon->icon->shadowed) {
2144 icon->icon->shadowed = 1;
2145 lupdate_icon = True;
2147 } else {
2148 return False;
2155 for (index = 1; index < dock->max_icons; index++)
2156 if (dock->icon_array[index] == NULL)
2157 break;
2158 /* if (index == dock->max_icons)
2159 return; */
2161 assert(index < dock->max_icons);
2163 dock->icon_array[index] = icon;
2164 icon->yindex = y;
2165 icon->xindex = x;
2167 icon->omnipresent = 0;
2169 icon->x_pos = dock->x_pos + x * ICON_SIZE;
2170 icon->y_pos = dock->y_pos + y * ICON_SIZE;
2172 dock->icon_count++;
2174 icon->running = 1;
2175 icon->launching = 0;
2176 icon->docked = 1;
2177 icon->dock = dock;
2178 icon->icon->core->descriptor.handle_mousedown = iconMouseDown;
2179 icon->icon->core->descriptor.handle_enternotify = clipEnterNotify;
2180 icon->icon->core->descriptor.handle_leavenotify = clipLeaveNotify;
2181 icon->icon->core->descriptor.parent_type = WCLASS_DOCK_ICON;
2182 icon->icon->core->descriptor.parent = icon;
2184 MoveInStackListUnder(dock->icon_array[index - 1]->icon->core, icon->icon->core);
2185 wAppIconMove(icon, icon->x_pos, icon->y_pos);
2188 * Update icon pixmap, RImage doesn't change,
2189 * so call wIconUpdate is not needed
2191 if (lupdate_icon)
2192 update_icon_pixmap(icon->icon);
2194 /* Paint it */
2195 wAppIconPaint(icon);
2197 /* Save it */
2198 save_appicon(icon, True);
2200 if (wPreferences.auto_arrange_icons)
2201 wArrangeIcons(dock->screen_ptr, True);
2203 #ifdef XDND /* was OFFIX */
2204 if (icon->command && !icon->dnd_command) {
2205 int len = strlen(icon->command) + 8;
2206 icon->dnd_command = wmalloc(len);
2207 snprintf(icon->dnd_command, len, "%s %%d", icon->command);
2209 #endif
2211 if (icon->command && !icon->paste_command) {
2212 int len = strlen(icon->command) + 8;
2213 icon->paste_command = wmalloc(len);
2214 snprintf(icon->paste_command, len, "%s %%s", icon->command);
2217 return True;
2220 void wDockReattachIcon(WDock *dock, WAppIcon *icon, int x, int y)
2222 int index;
2224 for (index = 1; index < dock->max_icons; index++) {
2225 if (dock->icon_array[index] == icon)
2226 break;
2228 assert(index < dock->max_icons);
2230 icon->yindex = y;
2231 icon->xindex = x;
2233 icon->x_pos = dock->x_pos + x * ICON_SIZE;
2234 icon->y_pos = dock->y_pos + y * ICON_SIZE;
2237 Bool wDockMoveIconBetweenDocks(WDock *src, WDock *dest, WAppIcon *icon, int x, int y)
2239 WWindow *wwin;
2240 char *command = NULL;
2241 int index;
2242 Bool update_icon = False;
2244 if (src == dest)
2245 return True; /* No move needed, we're already there */
2247 if (dest == NULL)
2248 return False;
2251 * For the moment we can't do this if we move icons in Clip from one
2252 * workspace to other, because if we move two or more icons without
2253 * command, the dialog box will not be able to tell us to which of the
2254 * moved icons it applies. -Dan
2256 if ((dest->type == WM_DOCK /*|| dest->keep_attracted */ ) && icon->command == NULL) {
2257 /* If icon->owner exists, it means the application is running */
2258 if (icon->icon->owner) {
2259 wwin = icon->icon->owner;
2260 command = GetCommandForWindow(wwin->client_win);
2263 if (command) {
2264 icon->command = command;
2265 } else {
2266 icon->editing = 1;
2267 /* icon->forced_dock = 1; */
2268 if (wInputDialog(src->screen_ptr, _("Dock Icon"),
2269 _("Type the command used to launch the application"), &command)) {
2270 if (command && (command[0] == 0 || (command[0] == '-' && command[1] == 0))) {
2271 wfree(command);
2272 command = NULL;
2274 icon->command = command;
2275 } else {
2276 icon->editing = 0;
2277 if (command)
2278 wfree(command);
2279 return False;
2281 icon->editing = 0;
2285 if (dest->type == WM_DOCK || dest->type == WM_DRAWER)
2286 wClipMakeIconOmnipresent(icon, False);
2288 for (index = 1; index < src->max_icons; index++) {
2289 if (src->icon_array[index] == icon)
2290 break;
2292 assert(index < src->max_icons);
2294 src->icon_array[index] = NULL;
2295 src->icon_count--;
2297 for (index = 1; index < dest->max_icons; index++) {
2298 if (dest->icon_array[index] == NULL)
2299 break;
2302 assert(index < dest->max_icons);
2304 dest->icon_array[index] = icon;
2305 icon->dock = dest;
2307 /* deselect the icon */
2308 if (icon->icon->selected)
2309 wIconSelect(icon->icon);
2311 icon->icon->core->descriptor.handle_enternotify = clipEnterNotify;
2312 icon->icon->core->descriptor.handle_leavenotify = clipLeaveNotify;
2314 /* set it to be kept when moving to dock.
2315 * Unless the icon does not have a command set
2317 if (icon->command && (dest->type == WM_DOCK || dest->type == WM_DRAWER)) {
2318 icon->attracted = 0;
2319 if (icon->icon->shadowed) {
2320 icon->icon->shadowed = 0;
2321 update_icon = True;
2323 save_appicon(icon, True);
2326 if (src->auto_collapse || src->auto_raise_lower)
2327 clipLeave(src);
2329 icon->yindex = y;
2330 icon->xindex = x;
2332 icon->x_pos = dest->x_pos + x * ICON_SIZE;
2333 icon->y_pos = dest->y_pos + y * ICON_SIZE;
2335 dest->icon_count++;
2337 MoveInStackListUnder(dest->icon_array[index - 1]->icon->core, icon->icon->core);
2340 * Update icon pixmap, RImage doesn't change,
2341 * so call wIconUpdate is not needed
2343 if (update_icon)
2344 update_icon_pixmap(icon->icon);
2346 /* Paint it */
2347 wAppIconPaint(icon);
2349 return True;
2352 void wDockDetach(WDock *dock, WAppIcon *icon)
2354 int index;
2355 Bool update_icon = False;
2357 /* make the settings panel be closed */
2358 if (icon->panel)
2359 DestroyDockAppSettingsPanel(icon->panel);
2361 /* This must be called before icon->dock is set to NULL.
2362 * Don't move it. -Dan
2364 wClipMakeIconOmnipresent(icon, False);
2366 icon->docked = 0;
2367 icon->dock = NULL;
2368 icon->attracted = 0;
2369 icon->auto_launch = 0;
2370 if (icon->icon->shadowed) {
2371 icon->icon->shadowed = 0;
2372 update_icon = True;
2375 /* deselect the icon */
2376 if (icon->icon->selected)
2377 wIconSelect(icon->icon);
2379 if (icon->command) {
2380 wfree(icon->command);
2381 icon->command = NULL;
2383 #ifdef XDND /* was OFFIX */
2384 if (icon->dnd_command) {
2385 wfree(icon->dnd_command);
2386 icon->dnd_command = NULL;
2388 #endif
2389 if (icon->paste_command) {
2390 wfree(icon->paste_command);
2391 icon->paste_command = NULL;
2394 for (index = 1; index < dock->max_icons; index++)
2395 if (dock->icon_array[index] == icon)
2396 break;
2398 assert(index < dock->max_icons);
2399 dock->icon_array[index] = NULL;
2400 icon->yindex = -1;
2401 icon->xindex = -1;
2403 dock->icon_count--;
2405 /* if the dock is not attached to an application or
2406 * the application did not set the appropriate hints yet,
2407 * destroy the icon */
2408 if (!icon->running || !wApplicationOf(icon->main_window)) {
2409 wAppIconDestroy(icon);
2410 } else {
2411 icon->icon->core->descriptor.handle_mousedown = appIconMouseDown;
2412 icon->icon->core->descriptor.handle_enternotify = NULL;
2413 icon->icon->core->descriptor.handle_leavenotify = NULL;
2414 icon->icon->core->descriptor.parent_type = WCLASS_APPICON;
2415 icon->icon->core->descriptor.parent = icon;
2417 ChangeStackingLevel(icon->icon->core, NORMAL_ICON_LEVEL);
2420 * Update icon pixmap, RImage doesn't change,
2421 * so call wIconUpdate is not needed
2423 if (update_icon)
2424 update_icon_pixmap(icon->icon);
2426 /* Paint it */
2427 wAppIconPaint(icon);
2429 if (wPreferences.auto_arrange_icons)
2430 wArrangeIcons(dock->screen_ptr, True);
2432 if (dock->auto_collapse || dock->auto_raise_lower)
2433 clipLeave(dock);
2437 * returns the closest Dock slot index for the passed
2438 * coordinates.
2440 * Returns False if icon can't be docked.
2442 * Note: this function should NEVER alter ret_x or ret_y, unless it will
2443 * return True. -Dan
2445 /* Redocking == true means either icon->dock == dock (normal case)
2446 * or we are called from handleDockMove for a drawer */
2447 Bool wDockSnapIcon(WDock *dock, WAppIcon *icon, int req_x, int req_y, int *ret_x, int *ret_y, int redocking)
2449 WScreen *scr = dock->screen_ptr;
2450 int dx, dy;
2451 int ex_x, ex_y;
2452 int i, offset = ICON_SIZE / 2;
2453 WAppIcon *aicon = NULL;
2454 WAppIcon *nicon = NULL;
2456 if (wPreferences.flags.noupdates)
2457 return False;
2459 dx = dock->x_pos;
2460 dy = dock->y_pos;
2462 /* if the dock is full */
2463 if (!redocking && (dock->icon_count >= dock->max_icons))
2464 return False;
2466 /* exact position */
2467 if (req_y < dy)
2468 ex_y = (req_y - offset - dy) / ICON_SIZE;
2469 else
2470 ex_y = (req_y + offset - dy) / ICON_SIZE;
2472 if (req_x < dx)
2473 ex_x = (req_x - offset - dx) / ICON_SIZE;
2474 else
2475 ex_x = (req_x + offset - dx) / ICON_SIZE;
2477 /* check if the icon is outside the screen boundaries */
2478 if (!onScreen(scr, dx + ex_x * ICON_SIZE, dy + ex_y * ICON_SIZE))
2479 return False;
2481 switch (dock->type) {
2482 case WM_DOCK:
2483 /* We can return False right away if
2484 * - we do not come from this dock (which is a WM_DOCK),
2485 * - we are not right over it, and
2486 * - we are not the main tile of a drawer.
2487 * In the latter case, we are called from handleDockMove. */
2488 if (icon->dock != dock && ex_x != 0 &&
2489 !(icon->dock && icon->dock->type == WM_DRAWER && icon == icon->dock->icon_array[0]))
2490 return False;
2492 if (!redocking && ex_x != 0)
2493 return False;
2495 if (getDrawer(scr, ex_y)) /* Return false so that the drawer gets it. */
2496 return False;
2498 aicon = NULL;
2499 for (i = 0; i < dock->max_icons; i++) {
2500 nicon = dock->icon_array[i];
2501 if (nicon && nicon->yindex == ex_y) {
2502 aicon = nicon;
2503 break;
2507 if (redocking) {
2508 int sig, done, closest;
2510 /* Possible cases when redocking:
2512 * icon dragged out of range of any slot -> false
2513 * icon dragged on a drawer -> false (to open the drawer)
2514 * icon dragged to range of free slot
2515 * icon dragged to range of same slot
2516 * icon dragged to range of different icon
2518 if (abs(ex_x) > DOCK_DETTACH_THRESHOLD)
2519 return False;
2521 if (aicon == icon || !aicon) {
2522 *ret_x = 0;
2523 *ret_y = ex_y;
2524 return True;
2527 /* start looking at the upper slot or lower? */
2528 if (ex_y * ICON_SIZE < (req_y + offset - dy))
2529 sig = 1;
2530 else
2531 sig = -1;
2533 done = 0;
2534 /* look for closest free slot */
2535 for (i = 0; i < (DOCK_DETTACH_THRESHOLD + 1) * 2 && !done; i++) {
2536 int j;
2538 done = 1;
2539 closest = sig * (i / 2) + ex_y;
2540 /* check if this slot is fully on the screen and not used */
2541 if (onScreen(scr, dx, dy + closest * ICON_SIZE)) {
2542 for (j = 0; j < dock->max_icons; j++) {
2543 if (dock->icon_array[j]
2544 && dock->icon_array[j]->yindex == closest) {
2545 /* slot is used by someone else */
2546 if (dock->icon_array[j] != icon)
2547 done = 0;
2548 break;
2551 /* slot is used by a drawer */
2552 done = done && !getDrawer(scr, closest);
2554 else // !onScreen
2555 done = 0;
2556 sig = -sig;
2558 if (done &&
2559 ((ex_y >= closest && ex_y - closest < DOCK_DETTACH_THRESHOLD + 1)
2560 || (ex_y < closest && closest - ex_y <= DOCK_DETTACH_THRESHOLD + 1))) {
2561 *ret_x = 0;
2562 *ret_y = closest;
2563 return True;
2565 } else { /* !redocking */
2567 /* if slot is free and the icon is close enough, return it */
2568 if (!aicon && ex_x == 0) {
2569 *ret_x = 0;
2570 *ret_y = ex_y;
2571 return True;
2574 break;
2575 case WM_CLIP:
2577 int neighbours = 0;
2578 int start, stop, k;
2580 start = icon->omnipresent ? 0 : w_global.workspace.current;
2581 stop = icon->omnipresent ? w_global.workspace.count : start + 1;
2583 aicon = NULL;
2584 for (k = start; k < stop; k++) {
2585 WDock *tmp = w_global.workspace.array[k]->clip;
2586 if (!tmp)
2587 continue;
2588 for (i = 0; i < tmp->max_icons; i++) {
2589 nicon = tmp->icon_array[i];
2590 if (nicon && nicon->xindex == ex_x && nicon->yindex == ex_y) {
2591 aicon = nicon;
2592 break;
2595 if (aicon)
2596 break;
2598 for (k = start; k < stop; k++) {
2599 WDock *tmp = w_global.workspace.array[k]->clip;
2600 if (!tmp)
2601 continue;
2602 for (i = 0; i < tmp->max_icons; i++) {
2603 nicon = tmp->icon_array[i];
2604 if (nicon && nicon != icon && /* Icon can't be it's own neighbour */
2605 (abs(nicon->xindex - ex_x) <= CLIP_ATTACH_VICINITY &&
2606 abs(nicon->yindex - ex_y) <= CLIP_ATTACH_VICINITY)) {
2607 neighbours = 1;
2608 break;
2611 if (neighbours)
2612 break;
2615 if (neighbours && (aicon == NULL || (redocking && aicon == icon))) {
2616 *ret_x = ex_x;
2617 *ret_y = ex_y;
2618 return True;
2620 break;
2622 case WM_DRAWER:
2624 WAppIcon *aicons_to_shift[ dock->icon_count ];
2625 int index_of_hole, j;
2627 if (ex_y != 0 ||
2628 abs(ex_x) - dock->icon_count > DOCK_DETTACH_THRESHOLD ||
2629 (ex_x < 0 && !dock->on_right_side) ||
2630 (ex_x > 0 && dock->on_right_side)) {
2631 return False;
2634 if (ex_x == 0)
2635 ex_x = (dock->on_right_side ? -1 : 1);
2637 /* "Reduce" ex_x but keep its sign */
2638 if (redocking) {
2639 if (abs(ex_x) > dock->icon_count - 1) /* minus 1: do not take icon_array[0] into account */
2640 ex_x = ex_x * (dock->icon_count - 1) / abs(ex_x); /* don't use *= ! */
2641 } else {
2642 if (abs(ex_x) > dock->icon_count)
2643 ex_x = ex_x * dock->icon_count / abs(ex_x);
2645 index_of_hole = indexOfHole(dock, icon, redocking);
2647 /* Find the appicons between where icon was (index_of_hole) and where
2648 * it wants to be (ex_x) and slide them. */
2649 j = 0;
2650 for (i = 1; i < dock->max_icons; i++) {
2651 if ((aicon = dock->icon_array[ i ]) && aicon != icon &&
2652 ((ex_x <= aicon->xindex && aicon->xindex < index_of_hole) ||
2653 (index_of_hole < aicon->xindex && aicon->xindex <= ex_x)))
2654 aicons_to_shift[ j++ ] = aicon;
2656 assert(j == abs(ex_x - index_of_hole));
2658 wSlideAppicons(aicons_to_shift, j, (index_of_hole < ex_x));
2660 *ret_x = ex_x;
2661 *ret_y = ex_y;
2662 return True;
2665 return False;
2668 static int onScreen(WScreen *scr, int x, int y)
2670 WMRect rect;
2671 int flags;
2673 rect.pos.x = x;
2674 rect.pos.y = y;
2675 rect.size.width = rect.size.height = ICON_SIZE;
2677 wGetRectPlacementInfo(scr, rect, &flags);
2679 return !(flags & (XFLAG_DEAD | XFLAG_PARTIAL));
2683 * returns true if it can find a free slot in the dock,
2684 * in which case it changes x_pos and y_pos accordingly.
2685 * Else returns false.
2687 Bool wDockFindFreeSlot(WDock *dock, int *x_pos, int *y_pos)
2689 WScreen *scr = dock->screen_ptr;
2690 WAppIcon *btn;
2691 WAppIconChain *chain;
2692 unsigned char *slot_map;
2693 int mwidth;
2694 int r;
2695 int x, y;
2696 int i, done = False;
2697 int corner;
2698 int sx = 0, ex = scr->scr_width, ey = scr->scr_height;
2699 int extra_count = 0;
2701 if (dock->type == WM_DRAWER) {
2702 if (dock->icon_count >= dock->max_icons) { /* drawer is full */
2703 return False;
2705 *x_pos = dock->icon_count * (dock->on_right_side ? -1 : 1);
2706 *y_pos = 0;
2707 return True;
2710 if (dock->type == WM_CLIP && dock != w_global.workspace.array[w_global.workspace.current]->clip)
2711 extra_count = scr->global_icon_count;
2713 /* if the dock is full */
2714 if (dock->icon_count + extra_count >= dock->max_icons)
2715 return False;
2717 if (!wPreferences.flags.nodock && scr->dock) {
2718 if (scr->dock->on_right_side)
2719 ex -= ICON_SIZE + DOCK_EXTRA_SPACE;
2720 else
2721 sx += ICON_SIZE + DOCK_EXTRA_SPACE;
2724 if (ex < dock->x_pos)
2725 ex = dock->x_pos;
2726 if (sx > dock->x_pos + ICON_SIZE)
2727 sx = dock->x_pos + ICON_SIZE;
2728 #define C_NONE 0
2729 #define C_NW 1
2730 #define C_NE 2
2731 #define C_SW 3
2732 #define C_SE 4
2734 /* check if clip is in a corner */
2735 if (dock->type == WM_CLIP) {
2736 if (dock->x_pos < 1 && dock->y_pos < 1)
2737 corner = C_NE;
2738 else if (dock->x_pos < 1 && dock->y_pos >= (ey - ICON_SIZE))
2739 corner = C_SE;
2740 else if (dock->x_pos >= (ex - ICON_SIZE) && dock->y_pos >= (ey - ICON_SIZE))
2741 corner = C_SW;
2742 else if (dock->x_pos >= (ex - ICON_SIZE) && dock->y_pos < 1)
2743 corner = C_NW;
2744 else
2745 corner = C_NONE;
2746 } else {
2747 corner = C_NONE;
2750 /* If the clip is in the corner, use only slots that are in the border
2751 * of the screen */
2752 if (corner != C_NONE) {
2753 char *hmap, *vmap;
2754 int hcount, vcount;
2756 hcount = WMIN(dock->max_icons, scr->scr_width / ICON_SIZE);
2757 vcount = WMIN(dock->max_icons, scr->scr_height / ICON_SIZE);
2758 hmap = wmalloc(hcount + 1);
2759 vmap = wmalloc(vcount + 1);
2761 /* mark used positions */
2762 switch (corner) {
2763 case C_NE:
2764 for (i = 0; i < dock->max_icons; i++) {
2765 btn = dock->icon_array[i];
2766 if (!btn)
2767 continue;
2769 if (btn->xindex == 0 && btn->yindex > 0 && btn->yindex < vcount)
2770 vmap[btn->yindex] = 1;
2771 else if (btn->yindex == 0 && btn->xindex > 0 && btn->xindex < hcount)
2772 hmap[btn->xindex] = 1;
2774 for (chain = scr->global_icons; chain != NULL; chain = chain->next) {
2775 btn = chain->aicon;
2776 if (btn->xindex == 0 && btn->yindex > 0 && btn->yindex < vcount)
2777 vmap[btn->yindex] = 1;
2778 else if (btn->yindex == 0 && btn->xindex > 0 && btn->xindex < hcount)
2779 hmap[btn->xindex] = 1;
2781 break;
2782 case C_NW:
2783 for (i = 0; i < dock->max_icons; i++) {
2784 btn = dock->icon_array[i];
2785 if (!btn)
2786 continue;
2788 if (btn->xindex == 0 && btn->yindex > 0 && btn->yindex < vcount)
2789 vmap[btn->yindex] = 1;
2790 else if (btn->yindex == 0 && btn->xindex < 0 && btn->xindex > -hcount)
2791 hmap[-btn->xindex] = 1;
2793 for (chain = scr->global_icons; chain != NULL; chain = chain->next) {
2794 btn = chain->aicon;
2795 if (btn->xindex == 0 && btn->yindex > 0 && btn->yindex < vcount)
2796 vmap[btn->yindex] = 1;
2797 else if (btn->yindex == 0 && btn->xindex < 0 && btn->xindex > -hcount)
2798 hmap[-btn->xindex] = 1;
2800 break;
2801 case C_SE:
2802 for (i = 0; i < dock->max_icons; i++) {
2803 btn = dock->icon_array[i];
2804 if (!btn)
2805 continue;
2807 if (btn->xindex == 0 && btn->yindex < 0 && btn->yindex > -vcount)
2808 vmap[-btn->yindex] = 1;
2809 else if (btn->yindex == 0 && btn->xindex > 0 && btn->xindex < hcount)
2810 hmap[btn->xindex] = 1;
2812 for (chain = scr->global_icons; chain != NULL; chain = chain->next) {
2813 btn = chain->aicon;
2814 if (btn->xindex == 0 && btn->yindex < 0 && btn->yindex > -vcount)
2815 vmap[-btn->yindex] = 1;
2816 else if (btn->yindex == 0 && btn->xindex > 0 && btn->xindex < hcount)
2817 hmap[btn->xindex] = 1;
2819 break;
2820 case C_SW:
2821 default:
2822 for (i = 0; i < dock->max_icons; i++) {
2823 btn = dock->icon_array[i];
2824 if (!btn)
2825 continue;
2827 if (btn->xindex == 0 && btn->yindex < 0 && btn->yindex > -vcount)
2828 vmap[-btn->yindex] = 1;
2829 else if (btn->yindex == 0 && btn->xindex < 0 && btn->xindex > -hcount)
2830 hmap[-btn->xindex] = 1;
2832 for (chain = scr->global_icons; chain != NULL; chain = chain->next) {
2833 btn = chain->aicon;
2834 if (btn->xindex == 0 && btn->yindex < 0 && btn->yindex > -vcount)
2835 vmap[-btn->yindex] = 1;
2836 else if (btn->yindex == 0 && btn->xindex < 0 && btn->xindex > -hcount)
2837 hmap[-btn->xindex] = 1;
2840 x = 0;
2841 y = 0;
2842 done = 0;
2843 /* search a vacant slot */
2844 for (i = 1; i < WMAX(vcount, hcount); i++) {
2845 if (i < vcount && vmap[i] == 0) {
2846 /* found a slot */
2847 x = 0;
2848 y = i;
2849 done = 1;
2850 break;
2851 } else if (i < hcount && hmap[i] == 0) {
2852 /* found a slot */
2853 x = i;
2854 y = 0;
2855 done = 1;
2856 break;
2859 wfree(vmap);
2860 wfree(hmap);
2861 /* If found a slot, translate and return */
2862 if (done) {
2863 if (corner == C_NW || corner == C_NE)
2864 *y_pos = y;
2865 else
2866 *y_pos = -y;
2868 if (corner == C_NE || corner == C_SE)
2869 *x_pos = x;
2870 else
2871 *x_pos = -x;
2873 return True;
2875 /* else, try to find a slot somewhere else */
2878 /* a map of mwidth x mwidth would be enough if we allowed icons to be
2879 * placed outside of screen */
2880 mwidth = (int)ceil(sqrt(dock->max_icons));
2882 /* In the worst case (the clip is in the corner of the screen),
2883 * the amount of icons that fit in the clip is smaller.
2884 * Double the map to get a safe value.
2886 mwidth += mwidth;
2888 r = (mwidth - 1) / 2;
2890 slot_map = wmalloc(mwidth * mwidth);
2892 #define XY2OFS(x,y) (WMAX(abs(x),abs(y)) > r) ? 0 : (((y)+r)*(mwidth)+(x)+r)
2894 /* mark used slots in the map. If the slot falls outside the map
2895 * (for example, when all icons are placed in line), ignore them. */
2896 for (i = 0; i < dock->max_icons; i++) {
2897 btn = dock->icon_array[i];
2898 if (btn)
2899 slot_map[XY2OFS(btn->xindex, btn->yindex)] = 1;
2902 for (chain = scr->global_icons; chain != NULL; chain = chain->next)
2903 slot_map[XY2OFS(chain->aicon->xindex, chain->aicon->yindex)] = 1;
2905 /* Find closest slot from the center that is free by scanning the
2906 * map from the center to outward in circular passes.
2907 * This will not result in a neat layout, but will be optimal
2908 * in the sense that there will not be holes left.
2910 done = 0;
2911 for (i = 1; i <= r && !done; i++) {
2912 int tx, ty;
2914 /* top and bottom parts of the ring */
2915 for (x = -i; x <= i && !done; x++) {
2916 tx = dock->x_pos + x * ICON_SIZE;
2917 y = -i;
2918 ty = dock->y_pos + y * ICON_SIZE;
2919 if (slot_map[XY2OFS(x, y)] == 0 && onScreen(scr, tx, ty)) {
2920 *x_pos = x;
2921 *y_pos = y;
2922 done = 1;
2923 break;
2925 y = i;
2926 ty = dock->y_pos + y * ICON_SIZE;
2927 if (slot_map[XY2OFS(x, y)] == 0 && onScreen(scr, tx, ty)) {
2928 *x_pos = x;
2929 *y_pos = y;
2930 done = 1;
2931 break;
2934 /* left and right parts of the ring */
2935 for (y = -i + 1; y <= i - 1; y++) {
2936 ty = dock->y_pos + y * ICON_SIZE;
2937 x = -i;
2938 tx = dock->x_pos + x * ICON_SIZE;
2939 if (slot_map[XY2OFS(x, y)] == 0 && onScreen(scr, tx, ty)) {
2940 *x_pos = x;
2941 *y_pos = y;
2942 done = 1;
2943 break;
2945 x = i;
2946 tx = dock->x_pos + x * ICON_SIZE;
2947 if (slot_map[XY2OFS(x, y)] == 0 && onScreen(scr, tx, ty)) {
2948 *x_pos = x;
2949 *y_pos = y;
2950 done = 1;
2951 break;
2955 wfree(slot_map);
2956 #undef XY2OFS
2957 return done;
2960 static void moveDock(WDock *dock, int new_x, int new_y)
2962 WAppIcon *btn;
2963 WDrawerChain *dc;
2964 int i;
2966 if (dock->type == WM_DOCK) {
2967 for (dc = dock->screen_ptr->drawers; dc != NULL; dc = dc->next)
2968 moveDock(dc->adrawer, new_x, dc->adrawer->y_pos - dock->y_pos + new_y);
2971 dock->x_pos = new_x;
2972 dock->y_pos = new_y;
2973 for (i = 0; i < dock->max_icons; i++) {
2974 btn = dock->icon_array[i];
2975 if (btn) {
2976 btn->x_pos = new_x + btn->xindex * ICON_SIZE;
2977 btn->y_pos = new_y + btn->yindex * ICON_SIZE;
2978 XMoveWindow(dpy, btn->icon->core->window, btn->x_pos, btn->y_pos);
2983 static void swapDock(WDock *dock)
2985 WScreen *scr = dock->screen_ptr;
2986 WAppIcon *btn;
2987 int x, i;
2989 if (dock->on_right_side)
2990 x = dock->x_pos = scr->scr_width - ICON_SIZE - DOCK_EXTRA_SPACE;
2991 else
2992 x = dock->x_pos = DOCK_EXTRA_SPACE;
2994 swapDrawers(scr, dock->on_right_side, x);
2996 for (i = 0; i < dock->max_icons; i++) {
2997 btn = dock->icon_array[i];
2998 if (btn) {
2999 btn->x_pos = x;
3000 XMoveWindow(dpy, btn->icon->core->window, btn->x_pos, btn->y_pos);
3004 wScreenUpdateUsableArea(scr);
3007 static pid_t execCommand(WAppIcon *btn, const char *command, WSavedState *state)
3009 WScreen *scr = btn->icon->core->screen_ptr;
3010 pid_t pid;
3011 char **argv;
3012 int argc;
3013 char *cmdline;
3015 cmdline = ExpandOptions(scr, command);
3017 if (scr->flags.dnd_data_convertion_status || !cmdline) {
3018 if (cmdline)
3019 wfree(cmdline);
3020 if (state)
3021 wfree(state);
3022 return 0;
3025 wtokensplit(cmdline, &argv, &argc);
3027 if (!argc) {
3028 if (cmdline)
3029 wfree(cmdline);
3030 if (state)
3031 wfree(state);
3032 return 0;
3035 if ((pid = fork()) == 0) {
3036 char **args;
3037 int i;
3039 SetupEnvironment(scr);
3041 #ifdef HAVE_SETSID
3042 setsid();
3043 #endif
3045 args = malloc(sizeof(char *) * (argc + 1));
3046 if (!args)
3047 exit(111);
3049 for (i = 0; i < argc; i++)
3050 args[i] = argv[i];
3052 args[argc] = NULL;
3053 execvp(argv[0], args);
3054 exit(111);
3056 wtokenfree(argv, argc);
3058 if (pid > 0) {
3059 if (!state) {
3060 state = wmalloc(sizeof(WSavedState));
3061 state->hidden = -1;
3062 state->miniaturized = -1;
3063 state->shaded = -1;
3064 if (btn->dock == scr->dock || btn->dock->type == WM_DRAWER || btn->omnipresent)
3065 state->workspace = -1;
3066 else
3067 state->workspace = w_global.workspace.current;
3069 wWindowAddSavedState(btn->wm_instance, btn->wm_class, cmdline, pid, state);
3070 wAddDeathHandler(pid, (WDeathHandler *) trackDeadProcess, btn->dock);
3071 } else if (state) {
3072 wfree(state);
3074 wfree(cmdline);
3075 return pid;
3078 void wDockHideIcons(WDock *dock)
3080 int i;
3082 if (dock == NULL)
3083 return;
3085 for (i = 1; i < dock->max_icons; i++) {
3086 if (dock->icon_array[i])
3087 XUnmapWindow(dpy, dock->icon_array[i]->icon->core->window);
3089 dock->mapped = 0;
3091 dockIconPaint(dock->icon_array[0]);
3094 void wDockShowIcons(WDock *dock)
3096 int i;
3097 WAppIcon *btn;
3099 if (dock == NULL)
3100 return;
3102 btn = dock->icon_array[0];
3103 moveDock(dock, btn->x_pos, btn->y_pos);
3105 /* Deleting any change in stacking level, this function is now only about
3106 mapping icons */
3108 if (!dock->collapsed) {
3109 for (i = 1; i < dock->max_icons; i++) {
3110 if (dock->icon_array[i])
3111 XMapWindow(dpy, dock->icon_array[i]->icon->core->window);
3114 dock->mapped = 1;
3116 dockIconPaint(btn);
3119 void wDockLower(WDock *dock)
3121 int i;
3122 WDrawerChain *dc;
3124 if (dock->type == WM_DOCK) {
3125 for (dc = dock->screen_ptr->drawers; dc != NULL; dc = dc->next)
3126 wDockLower(dc->adrawer);
3128 for (i = 0; i < dock->max_icons; i++) {
3129 if (dock->icon_array[i])
3130 wLowerFrame(dock->icon_array[i]->icon->core);
3134 void wDockRaise(WDock *dock)
3136 int i;
3137 WDrawerChain *dc;
3139 for (i = dock->max_icons - 1; i >= 0; i--) {
3140 if (dock->icon_array[i])
3141 wRaiseFrame(dock->icon_array[i]->icon->core);
3143 if (dock->type == WM_DOCK) {
3144 for (dc = dock->screen_ptr->drawers; dc != NULL; dc = dc->next)
3145 wDockRaise(dc->adrawer);
3149 void wDockRaiseLower(WDock *dock)
3151 if (!dock->icon_array[0]->icon->core->stacking->above
3152 || (dock->icon_array[0]->icon->core->stacking->window_level
3153 != dock->icon_array[0]->icon->core->stacking->above->stacking->window_level))
3154 wDockLower(dock);
3155 else
3156 wDockRaise(dock);
3159 void wDockFinishLaunch(WDock *dock, WAppIcon *icon)
3161 icon->launching = 0;
3162 icon->relaunching = 0;
3163 dockIconPaint(icon);
3166 WAppIcon *wDockFindIconForWindow(WDock *dock, Window window)
3168 WAppIcon *icon;
3169 int i;
3171 for (i = 0; i < dock->max_icons; i++) {
3172 icon = dock->icon_array[i];
3173 if (icon && icon->main_window == window)
3174 return icon;
3176 return NULL;
3179 void wDockTrackWindowLaunch(WDock *dock, Window window)
3181 WAppIcon *icon;
3182 char *wm_class, *wm_instance;
3183 int i;
3184 Bool firstPass = True;
3185 Bool found = False;
3186 char *command = NULL;
3188 if (!PropGetWMClass(window, &wm_class, &wm_instance) || (!wm_class && !wm_instance))
3189 return;
3191 command = GetCommandForWindow(window);
3192 retry:
3193 for (i = 0; i < dock->max_icons; i++) {
3194 icon = dock->icon_array[i];
3195 if (!icon)
3196 continue;
3198 /* app is already attached to icon */
3199 if (icon->main_window == window) {
3200 found = True;
3201 break;
3204 if ((icon->wm_instance || icon->wm_class)
3205 && (icon->launching || !icon->running)) {
3207 if (icon->wm_instance && wm_instance && strcmp(icon->wm_instance, wm_instance) != 0)
3208 continue;
3210 if (icon->wm_class && wm_class && strcmp(icon->wm_class, wm_class) != 0)
3211 continue;
3213 if (firstPass && command && strcmp(icon->command, command) != 0)
3214 continue;
3216 if (!icon->relaunching) {
3217 WApplication *wapp;
3219 /* Possibly an application that was docked with dockit,
3220 * but the user did not update WMState to indicate that
3221 * it was docked by force */
3222 wapp = wApplicationOf(window);
3223 if (!wapp) {
3224 icon->forced_dock = 1;
3225 icon->running = 0;
3227 if (!icon->forced_dock)
3228 icon->main_window = window;
3230 found = True;
3231 if (!wPreferences.no_animations && !icon->launching &&
3232 !dock->screen_ptr->flags.startup && !dock->collapsed) {
3233 WAppIcon *aicon;
3234 int x0, y0;
3236 icon->launching = 1;
3237 dockIconPaint(icon);
3239 aicon = wAppIconCreateForDock(dock->screen_ptr, NULL,
3240 wm_instance, wm_class, TILE_NORMAL);
3241 /* XXX: can: aicon->icon == NULL ? */
3242 PlaceIcon(dock->screen_ptr, &x0, &y0, wGetHeadForWindow(aicon->icon->owner));
3243 wAppIconMove(aicon, x0, y0);
3244 /* Should this always be lowered? -Dan */
3245 if (dock->lowered)
3246 wLowerFrame(aicon->icon->core);
3247 XMapWindow(dpy, aicon->icon->core->window);
3248 aicon->launching = 1;
3249 wAppIconPaint(aicon);
3250 SlideWindow(aicon->icon->core->window, x0, y0, icon->x_pos, icon->y_pos);
3251 XUnmapWindow(dpy, aicon->icon->core->window);
3252 wAppIconDestroy(aicon);
3254 wDockFinishLaunch(dock, icon);
3255 break;
3259 if (firstPass && !found) {
3260 firstPass = False;
3261 goto retry;
3264 if (command)
3265 wfree(command);
3267 if (wm_class)
3268 free(wm_class);
3269 if (wm_instance)
3270 free(wm_instance);
3273 void wClipUpdateForWorkspaceChange(WScreen *scr, int workspace)
3275 if (!wPreferences.flags.noclip) {
3276 w_global.clip.icon->dock = w_global.workspace.array[workspace]->clip;
3277 if (w_global.workspace.current != workspace) {
3278 WDock *old_clip = w_global.workspace.array[w_global.workspace.current]->clip;
3279 WAppIconChain *chain = scr->global_icons;
3281 while (chain) {
3282 wDockMoveIconBetweenDocks(chain->aicon->dock,
3283 w_global.workspace.array[workspace]->clip,
3284 chain->aicon, chain->aicon->xindex, chain->aicon->yindex);
3285 if (w_global.workspace.array[workspace]->clip->collapsed)
3286 XUnmapWindow(dpy, chain->aicon->icon->core->window);
3287 chain = chain->next;
3290 wDockHideIcons(old_clip);
3291 if (old_clip->auto_raise_lower) {
3292 if (old_clip->auto_raise_magic) {
3293 WMDeleteTimerHandler(old_clip->auto_raise_magic);
3294 old_clip->auto_raise_magic = NULL;
3296 wDockLower(old_clip);
3298 if (old_clip->auto_collapse) {
3299 if (old_clip->auto_expand_magic) {
3300 WMDeleteTimerHandler(old_clip->auto_expand_magic);
3301 old_clip->auto_expand_magic = NULL;
3303 old_clip->collapsed = 1;
3305 wDockShowIcons(w_global.workspace.array[workspace]->clip);
3310 static void trackDeadProcess(pid_t pid, unsigned char status, WDock *dock)
3312 WAppIcon *icon;
3313 int i;
3315 for (i = 0; i < dock->max_icons; i++) {
3316 icon = dock->icon_array[i];
3317 if (!icon)
3318 continue;
3320 if (icon->launching && icon->pid == pid) {
3321 if (!icon->relaunching) {
3322 icon->running = 0;
3323 icon->main_window = None;
3325 wDockFinishLaunch(dock, icon);
3326 icon->pid = 0;
3327 if (status == 111) {
3328 char msg[PATH_MAX];
3329 char *cmd;
3331 #ifdef XDND
3332 if (icon->drop_launch)
3333 cmd = icon->dnd_command;
3334 else
3335 #endif
3336 if (icon->paste_launch)
3337 cmd = icon->paste_command;
3338 else
3339 cmd = icon->command;
3341 snprintf(msg, sizeof(msg), _("Could not execute command \"%s\""), cmd);
3343 wMessageDialog(dock->screen_ptr, _("Error"), msg, _("OK"), NULL, NULL);
3345 break;
3350 /* This function is called when the dock switches state between
3351 * "normal" (including auto-raise/lower) and "keep on top". It is
3352 * therefore clearly distinct from wDockLower/Raise, which are called
3353 * each time a not-kept-on-top dock is lowered/raised. */
3354 static void toggleLowered(WDock *dock)
3356 WAppIcon *tmp;
3357 WDrawerChain *dc;
3358 int newlevel, i;
3360 if (!dock->lowered) {
3361 newlevel = WMNormalLevel;
3362 dock->lowered = 1;
3363 } else {
3364 newlevel = WMDockLevel;
3365 dock->lowered = 0;
3368 for (i = 0; i < dock->max_icons; i++) {
3369 tmp = dock->icon_array[i];
3370 if (!tmp)
3371 continue;
3373 ChangeStackingLevel(tmp->icon->core, newlevel);
3375 /* When the dock is no longer "on top", explicitly lower it as well.
3376 * It saves some CPU cycles (probably) to do it ourselves here
3377 * rather than calling wDockLower at the end of toggleLowered */
3378 if (dock->lowered)
3379 wLowerFrame(tmp->icon->core);
3382 if (dock->type == WM_DOCK) {
3383 for (dc = dock->screen_ptr->drawers; dc != NULL; dc = dc->next) {
3384 toggleLowered(dc->adrawer);
3386 wScreenUpdateUsableArea(dock->screen_ptr);
3390 static void toggleCollapsed(WDock *dock)
3392 if (dock->collapsed) {
3393 dock->collapsed = 0;
3394 wDockShowIcons(dock);
3395 } else {
3396 dock->collapsed = 1;
3397 wDockHideIcons(dock);
3401 static void openDockMenu(WDock *dock, WAppIcon *aicon, XEvent *event)
3403 WScreen *scr = dock->screen_ptr;
3404 WObjDescriptor *desc;
3405 WMenuEntry *entry;
3406 WApplication *wapp = NULL;
3407 int index = 0;
3408 int x_pos;
3409 int n_selected;
3410 int appIsRunning = aicon->running && aicon->icon && aicon->icon->owner;
3412 if (dock->type == WM_DOCK) {
3413 /* Dock position menu */
3414 updateDockPositionMenu(w_global.dock.pos_menu, dock);
3415 dock->menu->flags.realized = 0;
3416 if (!wPreferences.flags.nodrawer) {
3417 /* add a drawer */
3418 entry = dock->menu->entries[++index];
3419 entry->clientdata = aicon;
3420 wMenuSetEnabled(dock->menu, index, True);
3422 } else {
3423 /* clip/drawer options */
3424 if (w_global.clip.opt_menu)
3425 updateClipOptionsMenu(w_global.clip.opt_menu, dock);
3427 n_selected = numberOfSelectedIcons(dock);
3429 if (dock->type == WM_CLIP) {
3430 /* Rename Workspace */
3431 entry = dock->menu->entries[++index];
3432 if (aicon == w_global.clip.icon) {
3433 entry->callback = renameCallback;
3434 entry->clientdata = dock;
3435 entry->flags.indicator = 0;
3436 entry->text = _("Rename Workspace");
3437 } else {
3438 entry->callback = omnipresentCallback;
3439 entry->clientdata = aicon;
3440 if (n_selected > 0) {
3441 entry->flags.indicator = 0;
3442 entry->text = _("Toggle Omnipresent");
3443 } else {
3444 entry->flags.indicator = 1;
3445 entry->flags.indicator_on = aicon->omnipresent;
3446 entry->flags.indicator_type = MI_CHECK;
3447 entry->text = _("Omnipresent");
3452 /* select/unselect icon */
3453 entry = dock->menu->entries[++index];
3454 entry->clientdata = aicon;
3455 entry->flags.indicator_on = aicon->icon->selected;
3456 wMenuSetEnabled(dock->menu, index, aicon != w_global.clip.icon && !wIsADrawer(aicon));
3458 /* select/unselect all icons */
3459 entry = dock->menu->entries[++index];
3460 entry->clientdata = aicon;
3461 if (n_selected > 0)
3462 entry->text = _("Unselect All Icons");
3463 else
3464 entry->text = _("Select All Icons");
3466 wMenuSetEnabled(dock->menu, index, dock->icon_count > 1);
3468 /* keep icon(s) */
3469 entry = dock->menu->entries[++index];
3470 entry->clientdata = aicon;
3471 if (n_selected > 1)
3472 entry->text = _("Keep Icons");
3473 else
3474 entry->text = _("Keep Icon");
3476 wMenuSetEnabled(dock->menu, index, dock->icon_count > 1);
3478 if (dock->type == WM_CLIP) {
3479 /* this is the workspace submenu part */
3480 entry = dock->menu->entries[++index];
3481 if (n_selected > 1)
3482 entry->text = _("Move Icons To");
3483 else
3484 entry->text = _("Move Icon To");
3486 if (w_global.clip.submenu)
3487 updateWorkspaceMenu(w_global.clip.submenu, aicon);
3489 wMenuSetEnabled(dock->menu, index, !aicon->omnipresent);
3492 /* remove icon(s) */
3493 entry = dock->menu->entries[++index];
3494 entry->clientdata = aicon;
3495 if (n_selected > 1)
3496 entry->text = _("Remove Icons");
3497 else
3498 entry->text = _("Remove Icon");
3500 wMenuSetEnabled(dock->menu, index, dock->icon_count > 1);
3502 /* attract icon(s) */
3503 entry = dock->menu->entries[++index];
3504 entry->clientdata = aicon;
3506 dock->menu->flags.realized = 0;
3507 wMenuRealize(dock->menu);
3510 if (aicon->icon->owner)
3511 wapp = wApplicationOf(aicon->icon->owner->main_window);
3512 else
3513 wapp = NULL;
3515 /* launch */
3516 entry = dock->menu->entries[++index];
3517 entry->clientdata = aicon;
3518 wMenuSetEnabled(dock->menu, index, aicon->command != NULL);
3520 /* unhide here */
3521 entry = dock->menu->entries[++index];
3522 entry->clientdata = aicon;
3523 if (wapp && wapp->flags.hidden)
3524 entry->text = _("Unhide Here");
3525 else
3526 entry->text = _("Bring Here");
3528 wMenuSetEnabled(dock->menu, index, appIsRunning);
3530 /* hide */
3531 entry = dock->menu->entries[++index];
3532 entry->clientdata = aicon;
3533 if (wapp && wapp->flags.hidden)
3534 entry->text = _("Unhide");
3535 else
3536 entry->text = _("Hide");
3538 wMenuSetEnabled(dock->menu, index, appIsRunning);
3540 /* settings */
3541 entry = dock->menu->entries[++index];
3542 entry->clientdata = aicon;
3543 wMenuSetEnabled(dock->menu, index, !aicon->editing && !wPreferences.flags.noupdates);
3545 /* kill or remove drawer */
3546 entry = dock->menu->entries[++index];
3547 entry->clientdata = aicon;
3548 if (wIsADrawer(aicon)) {
3549 entry->callback = removeDrawerCallback;
3550 entry->text = _("Remove drawer");
3551 wMenuSetEnabled(dock->menu, index, True);
3552 } else {
3553 entry->callback = killCallback;
3554 entry->text = _("Kill");
3555 wMenuSetEnabled(dock->menu, index, appIsRunning);
3558 if (!dock->menu->flags.realized)
3559 wMenuRealize(dock->menu);
3561 if (dock->type == WM_CLIP || dock->type == WM_DRAWER) {
3562 /*x_pos = event->xbutton.x_root+2; */
3563 x_pos = event->xbutton.x_root - dock->menu->frame->core->width / 2 - 1;
3564 if (x_pos < 0) {
3565 x_pos = 0;
3566 } else if (x_pos + dock->menu->frame->core->width > scr->scr_width - 2) {
3567 x_pos = scr->scr_width - dock->menu->frame->core->width - 4;
3569 } else {
3570 x_pos = dock->on_right_side ? scr->scr_width - dock->menu->frame->core->width - 3 : 0;
3573 wMenuMapAt(dock->menu, x_pos, event->xbutton.y_root + 2, False);
3575 /* allow drag select */
3576 event->xany.send_event = True;
3577 desc = &dock->menu->menu->descriptor;
3578 (*desc->handle_mousedown) (desc, event);
3581 /******************************************************************/
3582 static void iconDblClick(WObjDescriptor *desc, XEvent *event)
3584 WAppIcon *btn = desc->parent;
3585 WDock *dock = btn->dock;
3586 WApplication *wapp = NULL;
3587 int unhideHere = 0;
3589 if (btn->icon->owner && !(event->xbutton.state & ControlMask)) {
3590 wapp = wApplicationOf(btn->icon->owner->main_window);
3592 assert(wapp != NULL);
3594 unhideHere = (event->xbutton.state & ShiftMask);
3596 /* go to the last workspace that the user worked on the app */
3597 if (wapp->last_workspace != w_global.workspace.current && !unhideHere)
3598 wWorkspaceChange(dock->screen_ptr, wapp->last_workspace);
3600 wUnhideApplication(wapp, event->xbutton.button == Button2, unhideHere);
3602 if (event->xbutton.state & MOD_MASK)
3603 wHideOtherApplications(btn->icon->owner);
3604 } else {
3605 if (event->xbutton.button == Button1) {
3606 if (event->xbutton.state & MOD_MASK) {
3607 /* raise/lower dock */
3608 toggleLowered(dock);
3609 } else if (btn == w_global.clip.icon) {
3610 if (getClipButton(event->xbutton.x, event->xbutton.y) != CLIP_IDLE)
3611 handleClipChangeWorkspace(dock->screen_ptr, event);
3612 else if (wPreferences.flags.clip_merged_in_dock) {
3613 // Is actually the dock
3614 if (btn->command)
3616 if (!btn->launching && (!btn->running || (event->xbutton.state & ControlMask)))
3617 launchDockedApplication(btn, False);
3619 else
3621 wShowInfoPanel(dock->screen_ptr);
3624 else
3625 toggleCollapsed(dock);
3626 } else if (wIsADrawer(btn)) {
3627 toggleCollapsed(dock);
3628 } else if (btn->command) {
3629 if (!btn->launching && (!btn->running || (event->xbutton.state & ControlMask)))
3630 launchDockedApplication(btn, False);
3631 } else if (btn->xindex == 0 && btn->yindex == 0 && btn->dock->type == WM_DOCK) {
3632 wShowInfoPanel(dock->screen_ptr);
3638 static void handleDockMove(WDock *dock, WAppIcon *aicon, XEvent *event)
3640 WScreen *scr = dock->screen_ptr;
3641 int ofs_x = event->xbutton.x, ofs_y = event->xbutton.y;
3642 WIcon *icon = aicon->icon;
3643 WAppIcon *tmpaicon;
3644 WDrawerChain *dc;
3645 int x = aicon->x_pos, y = aicon->y_pos;;
3646 int shad_x = x, shad_y = y;
3647 XEvent ev;
3648 int grabbed = 0, done, previously_on_right, now_on_right, previous_x_pos, i;
3649 Pixmap ghost = None;
3650 int superfluous = wPreferences.superfluous; /* we catch it to avoid problems */
3652 if (XGrabPointer(dpy, aicon->icon->core->window, True, ButtonMotionMask
3653 | ButtonReleaseMask | ButtonPressMask, GrabModeAsync,
3654 GrabModeAsync, None, None, CurrentTime) != GrabSuccess)
3655 wwarning("pointer grab failed for dock move");
3657 if (dock->type == WM_DRAWER) {
3658 Window wins[2];
3659 wins[0] = icon->core->window;
3660 wins[1] = scr->dock_shadow;
3661 XRestackWindows(dpy, wins, 2);
3662 XMoveResizeWindow(dpy, scr->dock_shadow, aicon->x_pos, aicon->y_pos,
3663 ICON_SIZE, ICON_SIZE);
3664 if (superfluous) {
3665 if (icon->pixmap!=None)
3666 ghost = MakeGhostIcon(scr, icon->pixmap);
3667 else
3668 ghost = MakeGhostIcon(scr, icon->core->window);
3670 XSetWindowBackgroundPixmap(dpy, scr->dock_shadow, ghost);
3671 XClearWindow(dpy, scr->dock_shadow);
3673 XMapWindow(dpy, scr->dock_shadow);
3676 previously_on_right = now_on_right = dock->on_right_side;
3677 previous_x_pos = dock->x_pos;
3678 done = 0;
3679 while (!done) {
3680 WMMaskEvent(dpy, PointerMotionMask | ButtonReleaseMask | ButtonPressMask
3681 | ButtonMotionMask | ExposureMask | EnterWindowMask, &ev);
3682 switch (ev.type) {
3683 case Expose:
3684 WMHandleEvent(&ev);
3685 break;
3687 case EnterNotify:
3688 /* It means the cursor moved so fast that it entered
3689 * something else (if moving slowly, it would have
3690 * stayed in the dock that is being moved. Ignore such
3691 * "spurious" EnterNotifiy's */
3692 break;
3694 case MotionNotify:
3695 if (!grabbed) {
3696 if (abs(ofs_x - ev.xmotion.x) >= MOVE_THRESHOLD
3697 || abs(ofs_y - ev.xmotion.y) >= MOVE_THRESHOLD) {
3698 XChangeActivePointerGrab(dpy, ButtonMotionMask
3699 | ButtonReleaseMask | ButtonPressMask,
3700 wPreferences.cursor[WCUR_MOVE], CurrentTime);
3701 grabbed = 1;
3703 break;
3705 switch (dock->type) {
3706 case WM_CLIP:
3707 x = ev.xmotion.x_root - ofs_x;
3708 y = ev.xmotion.y_root - ofs_y;
3709 wScreenKeepInside(scr, &x, &y, ICON_SIZE, ICON_SIZE);
3710 moveDock(dock, x, y);
3711 break;
3712 case WM_DOCK:
3713 x = ev.xmotion.x_root - ofs_x;
3714 y = ev.xmotion.y_root - ofs_y;
3715 if (previously_on_right)
3717 now_on_right = (ev.xmotion.x_root >= previous_x_pos - ICON_SIZE);
3719 else
3721 now_on_right = (ev.xmotion.x_root > previous_x_pos + ICON_SIZE * 2);
3723 if (now_on_right != dock->on_right_side)
3725 dock->on_right_side = now_on_right;
3726 swapDock(dock);
3727 wArrangeIcons(scr, False);
3729 // Also perform the vertical move
3730 wScreenKeepInside(scr, &x, &y, ICON_SIZE, ICON_SIZE);
3731 moveDock(dock, dock->x_pos, y);
3732 if (wPreferences.flags.wrap_appicons_in_dock)
3734 for (i = 0; i < dock->max_icons; i++) {
3735 int new_y, new_index, j, ok;
3736 if ((tmpaicon = dock->icon_array[i]) == NULL)
3737 continue;
3738 if (onScreen(scr, tmpaicon->x_pos, tmpaicon->y_pos))
3739 continue;
3740 new_y = (tmpaicon->y_pos + ICON_SIZE * dock->max_icons) % (ICON_SIZE * dock->max_icons);
3741 new_index = (new_y - dock->y_pos) / ICON_SIZE;
3742 if (!onScreen(scr, tmpaicon->x_pos, new_y))
3743 continue;
3744 ok = 1;
3745 for (j = 0; j < dock->max_icons; j++)
3747 if (dock->icon_array[j] != NULL &&
3748 dock->icon_array[j]->yindex == new_index)
3750 ok = 0;
3751 break;
3754 if (!ok || getDrawer(scr, new_index) != NULL)
3755 continue;
3756 wDockReattachIcon(dock, tmpaicon, tmpaicon->xindex, new_index);
3758 for (dc = scr->drawers; dc != NULL; dc = dc->next)
3760 int new_y, new_index, j, ok;
3761 tmpaicon = dc->adrawer->icon_array[0];
3762 if (onScreen(scr, tmpaicon->x_pos, tmpaicon->y_pos))
3763 continue;
3764 new_y = (tmpaicon->y_pos + ICON_SIZE * dock->max_icons) % (ICON_SIZE * dock->max_icons);
3765 new_index = (new_y - dock->y_pos) / ICON_SIZE;
3766 if (!onScreen(scr, tmpaicon->x_pos, new_y))
3767 continue;
3768 ok = 1;
3769 for (j = 0; j < dock->max_icons; j++)
3771 if (dock->icon_array[j] != NULL &&
3772 dock->icon_array[j]->yindex == new_index)
3774 ok = 0;
3775 break;
3778 if (!ok || getDrawer(scr, new_index) != NULL)
3779 continue;
3780 moveDock(dc->adrawer, tmpaicon->x_pos, new_y);
3783 break;
3784 case WM_DRAWER:
3786 WDock *real_dock = dock->screen_ptr->dock;
3787 Bool snapped;
3788 int ix, iy;
3789 x = ev.xmotion.x_root - ofs_x;
3790 y = ev.xmotion.y_root - ofs_y;
3791 snapped = wDockSnapIcon(real_dock, aicon, x, y, &ix, &iy, True);
3792 if (snapped) {
3793 shad_x = real_dock->x_pos + ix * wPreferences.icon_size;
3794 shad_y = real_dock->y_pos + iy * wPreferences.icon_size;
3795 XMoveWindow(dpy, scr->dock_shadow, shad_x, shad_y);
3797 moveDock(dock, x, y);
3798 break;
3801 break;
3803 case ButtonPress:
3804 break;
3806 case ButtonRelease:
3807 if (ev.xbutton.button != event->xbutton.button)
3808 break;
3809 XUngrabPointer(dpy, CurrentTime);
3810 if (dock->type == WM_DRAWER) {
3811 Window *wins[dock->icon_count];
3813 for (i = 0; i < dock->max_icons; i++) {
3814 if ((tmpaicon = dock->icon_array[i]) == NULL)
3815 continue;
3816 wins[ tmpaicon->xindex + (dock->on_right_side ? dock->icon_count - 1 : 0) ] = &tmpaicon->icon->core->window;
3818 SlideWindows(wins, dock->icon_count,
3819 (dock->on_right_side ? x - (dock->icon_count - 1) * ICON_SIZE : x),
3821 (dock->on_right_side ? shad_x - (dock->icon_count - 1) * ICON_SIZE : shad_x),
3822 shad_y);
3823 XUnmapWindow(dpy, scr->dock_shadow);
3824 moveDock(dock, shad_x, shad_y);
3825 XResizeWindow(dpy, scr->dock_shadow, ICON_SIZE, ICON_SIZE);
3827 done = 1;
3828 break;
3831 if (superfluous) {
3832 if (ghost != None)
3833 XFreePixmap(dpy, ghost);
3834 XSetWindowBackground(dpy, scr->dock_shadow, scr->white_pixel);
3839 static int getClipButton(int px, int py)
3841 int pt = (CLIP_BUTTON_SIZE + 2) * ICON_SIZE / 64;
3843 if (px < 0 || py < 0 || px >= ICON_SIZE || py >= ICON_SIZE)
3844 return CLIP_IDLE;
3846 if (py <= pt - ((int)ICON_SIZE - 1 - px))
3847 return CLIP_FORWARD;
3848 else if (px <= pt - ((int)ICON_SIZE - 1 - py))
3849 return CLIP_REWIND;
3851 return CLIP_IDLE;
3854 static void handleClipChangeWorkspace(WScreen *scr, XEvent *event)
3856 XEvent ev;
3857 int done, direction, new_ws;
3858 int new_dir;
3859 WDock *clip = w_global.clip.icon->dock;
3861 direction = getClipButton(event->xbutton.x, event->xbutton.y);
3863 clip->lclip_button_pushed = direction == CLIP_REWIND;
3864 clip->rclip_button_pushed = direction == CLIP_FORWARD;
3866 wClipIconPaint();
3867 done = 0;
3868 while (!done) {
3869 WMMaskEvent(dpy, ExposureMask | ButtonMotionMask | ButtonReleaseMask | ButtonPressMask, &ev);
3870 switch (ev.type) {
3871 case Expose:
3872 WMHandleEvent(&ev);
3873 break;
3875 case MotionNotify:
3876 new_dir = getClipButton(ev.xmotion.x, ev.xmotion.y);
3877 if (new_dir != direction) {
3878 direction = new_dir;
3879 clip->lclip_button_pushed = direction == CLIP_REWIND;
3880 clip->rclip_button_pushed = direction == CLIP_FORWARD;
3881 wClipIconPaint();
3883 break;
3885 case ButtonPress:
3886 break;
3888 case ButtonRelease:
3889 if (ev.xbutton.button == event->xbutton.button)
3890 done = 1;
3894 clip->lclip_button_pushed = 0;
3895 clip->rclip_button_pushed = 0;
3897 new_ws = wPreferences.ws_advance || (event->xbutton.state & ControlMask);
3899 if (direction == CLIP_FORWARD) {
3900 if (w_global.workspace.current < w_global.workspace.count - 1)
3901 wWorkspaceChange(scr, w_global.workspace.current + 1);
3902 else if (new_ws && w_global.workspace.current < MAX_WORKSPACES - 1)
3903 wWorkspaceChange(scr, w_global.workspace.current + 1);
3904 else if (wPreferences.ws_cycle)
3905 wWorkspaceChange(scr, 0);
3906 } else if (direction == CLIP_REWIND) {
3907 if (w_global.workspace.current > 0)
3908 wWorkspaceChange(scr, w_global.workspace.current - 1);
3909 else if (w_global.workspace.current == 0 && wPreferences.ws_cycle)
3910 wWorkspaceChange(scr, w_global.workspace.count - 1);
3913 wClipIconPaint();
3916 static void iconMouseDown(WObjDescriptor *desc, XEvent *event)
3918 WAppIcon *aicon = desc->parent;
3919 WDock *dock = aicon->dock;
3920 WScreen *scr = aicon->icon->core->screen_ptr;
3922 if (aicon->editing || WCHECK_STATE(WSTATE_MODAL))
3923 return;
3925 scr->last_dock = dock;
3927 if (dock->menu->flags.mapped)
3928 wMenuUnmap(dock->menu);
3930 if (IsDoubleClick(scr, event)) {
3931 /* double-click was not in the main clip icon */
3932 if (dock->type != WM_CLIP || aicon->xindex != 0 || aicon->yindex != 0
3933 || getClipButton(event->xbutton.x, event->xbutton.y) == CLIP_IDLE) {
3934 iconDblClick(desc, event);
3935 return;
3939 if (event->xbutton.button == Button1) {
3940 if (event->xbutton.state & MOD_MASK)
3941 wDockLower(dock);
3942 else
3943 wDockRaise(dock);
3945 if ((event->xbutton.state & ShiftMask) && aicon != w_global.clip.icon && dock->type != WM_DOCK) {
3946 wIconSelect(aicon->icon);
3947 return;
3950 if (aicon->yindex == 0 && aicon->xindex == 0) {
3951 if (getClipButton(event->xbutton.x, event->xbutton.y) != CLIP_IDLE &&
3952 (dock->type == WM_CLIP || (dock->type == WM_DOCK && wPreferences.flags.clip_merged_in_dock)))
3953 handleClipChangeWorkspace(scr, event);
3954 else
3955 handleDockMove(dock, aicon, event);
3956 } else {
3957 Bool hasMoved = wHandleAppIconMove(aicon, event);
3958 if (wPreferences.single_click && !hasMoved)
3959 iconDblClick(desc, event);
3961 } else if (event->xbutton.button == Button2 && aicon == w_global.clip.icon) {
3962 if (!w_global.clip.ws_menu)
3963 w_global.clip.ws_menu = wWorkspaceMenuMake(scr, False);
3965 if (w_global.clip.ws_menu) {
3966 WMenu *wsMenu = w_global.clip.ws_menu;
3967 int xpos;
3969 wWorkspaceMenuUpdate(wsMenu);
3971 xpos = event->xbutton.x_root - wsMenu->frame->core->width / 2 - 1;
3972 if (xpos < 0) {
3973 xpos = 0;
3974 } else if (xpos + wsMenu->frame->core->width > scr->scr_width - 2) {
3975 xpos = scr->scr_width - wsMenu->frame->core->width - 4;
3977 wMenuMapAt(wsMenu, xpos, event->xbutton.y_root + 2, False);
3979 desc = &wsMenu->menu->descriptor;
3980 event->xany.send_event = True;
3981 (*desc->handle_mousedown) (desc, event);
3983 } else if (event->xbutton.button == Button2 && dock->type == WM_CLIP &&
3984 (event->xbutton.state & ShiftMask) && aicon != w_global.clip.icon) {
3985 wClipMakeIconOmnipresent(aicon, !aicon->omnipresent);
3986 } else if (event->xbutton.button == Button3) {
3987 if (event->xbutton.send_event &&
3988 XGrabPointer(dpy, aicon->icon->core->window, True, ButtonMotionMask
3989 | ButtonReleaseMask | ButtonPressMask, GrabModeAsync,
3990 GrabModeAsync, None, None, CurrentTime) != GrabSuccess) {
3991 wwarning("pointer grab failed for dockicon menu");
3992 return;
3995 openDockMenu(dock, aicon, event);
3996 } else if (event->xbutton.button == Button2) {
3997 WAppIcon *btn = desc->parent;
3999 if (!btn->launching && (!btn->running || (event->xbutton.state & ControlMask)))
4000 launchDockedApplication(btn, True);
4004 static void clipEnterNotify(WObjDescriptor *desc, XEvent *event)
4006 WAppIcon *btn = (WAppIcon *) desc->parent;
4007 WDock *dock, *tmp;
4008 WScreen *scr;
4010 assert(event->type == EnterNotify);
4012 if (desc->parent_type != WCLASS_DOCK_ICON)
4013 return;
4015 scr = btn->icon->core->screen_ptr;
4016 dock = btn->dock;
4018 if (dock == NULL)
4019 return;
4021 /* The auto raise/lower code */
4022 tmp = (dock->type == WM_DRAWER ? scr->dock : dock);
4023 if (tmp->auto_lower_magic) {
4024 WMDeleteTimerHandler(tmp->auto_lower_magic);
4025 tmp->auto_lower_magic = NULL;
4027 if (tmp->auto_raise_lower && !tmp->auto_raise_magic)
4028 tmp->auto_raise_magic = WMAddTimerHandler(wPreferences.clip_auto_raise_delay, clipAutoRaise, (void *) tmp);
4030 if (dock->type != WM_CLIP && dock->type != WM_DRAWER)
4031 return;
4033 /* The auto expand/collapse code */
4034 if (dock->auto_collapse_magic) {
4035 WMDeleteTimerHandler(dock->auto_collapse_magic);
4036 dock->auto_collapse_magic = NULL;
4038 if (dock->auto_collapse && !dock->auto_expand_magic)
4039 dock->auto_expand_magic = WMAddTimerHandler(wPreferences.clip_auto_expand_delay, clipAutoExpand, (void *)dock);
4042 static void clipLeave(WDock *dock)
4044 XEvent event;
4045 WObjDescriptor *desc = NULL;
4046 WDock *tmp;
4048 if (dock == NULL)
4049 return;
4051 if (XCheckTypedEvent(dpy, EnterNotify, &event) != False) {
4052 if (XFindContext(dpy, event.xcrossing.window, w_global.context.client_win,
4053 (XPointer *) & desc) != XCNOENT
4054 && desc && desc->parent_type == WCLASS_DOCK_ICON
4055 && ((WAppIcon *) desc->parent)->dock == dock) {
4056 /* We haven't left the dock/clip/drawer yet */
4057 XPutBackEvent(dpy, &event);
4058 return;
4061 XPutBackEvent(dpy, &event);
4062 } else {
4063 /* We entered a withdrawn window, so we're still in Clip */
4064 return;
4067 tmp = (dock->type == WM_DRAWER ? dock->screen_ptr->dock : dock);
4068 if (tmp->auto_raise_magic) {
4069 WMDeleteTimerHandler(tmp->auto_raise_magic);
4070 tmp->auto_raise_magic = NULL;
4072 if (tmp->auto_raise_lower && !tmp->auto_lower_magic)
4073 tmp->auto_lower_magic = WMAddTimerHandler(wPreferences.clip_auto_lower_delay, clipAutoLower, (void *)tmp);
4075 if (dock->type != WM_CLIP && dock->type != WM_DRAWER)
4076 return;
4078 if (dock->auto_expand_magic) {
4079 WMDeleteTimerHandler(dock->auto_expand_magic);
4080 dock->auto_expand_magic = NULL;
4082 if (dock->auto_collapse && !dock->auto_collapse_magic)
4083 dock->auto_collapse_magic = WMAddTimerHandler(wPreferences.clip_auto_collapse_delay, clipAutoCollapse, (void *)dock);
4086 static void clipLeaveNotify(WObjDescriptor *desc, XEvent *event)
4088 WAppIcon *btn = (WAppIcon *) desc->parent;
4090 assert(event->type == LeaveNotify);
4092 if (desc->parent_type != WCLASS_DOCK_ICON)
4093 return;
4095 clipLeave(btn->dock);
4098 static void clipAutoCollapse(void *cdata)
4100 WDock *dock = (WDock *) cdata;
4102 if (dock->type != WM_CLIP && dock->type != WM_DRAWER)
4103 return;
4105 if (dock->auto_collapse) {
4106 dock->collapsed = 1;
4107 wDockHideIcons(dock);
4109 dock->auto_collapse_magic = NULL;
4112 static void clipAutoExpand(void *cdata)
4114 WDock *dock = (WDock *) cdata;
4116 if (dock->type != WM_CLIP && dock->type != WM_DRAWER)
4117 return;
4119 if (dock->auto_collapse) {
4120 dock->collapsed = 0;
4121 wDockShowIcons(dock);
4123 dock->auto_expand_magic = NULL;
4126 static void clipAutoLower(void *cdata)
4128 WDock *dock = (WDock *) cdata;
4130 if (dock->auto_raise_lower)
4131 wDockLower(dock);
4133 dock->auto_lower_magic = NULL;
4136 static void clipAutoRaise(void *cdata)
4138 WDock *dock = (WDock *) cdata;
4140 if (dock->auto_raise_lower)
4141 wDockRaise(dock);
4143 dock->auto_raise_magic = NULL;
4146 static Bool iconCanBeOmnipresent(WAppIcon *aicon)
4148 WScreen *scr = aicon->icon->core->screen_ptr;
4149 WDock *clip;
4150 WAppIcon *btn;
4151 int i, j;
4153 for (i = 0; i < w_global.workspace.count; i++) {
4154 clip = w_global.workspace.array[i]->clip;
4156 if (clip == aicon->dock)
4157 continue;
4159 if (clip->icon_count + scr->global_icon_count >= clip->max_icons)
4160 return False; /* Clip is full in some workspace */
4162 for (j = 0; j < clip->max_icons; j++) {
4163 btn = clip->icon_array[j];
4164 if (btn && btn->xindex == aicon->xindex && btn->yindex == aicon->yindex)
4165 return False;
4169 return True;
4172 int wClipMakeIconOmnipresent(WAppIcon *aicon, int omnipresent)
4174 WScreen *scr = aicon->icon->core->screen_ptr;
4175 WAppIconChain *new_entry, *tmp, *tmp1;
4176 int status = WO_SUCCESS;
4178 if ((scr->dock && aicon->dock == scr->dock) || aicon == w_global.clip.icon)
4179 return WO_NOT_APPLICABLE;
4181 if (aicon->omnipresent == omnipresent)
4182 return WO_SUCCESS;
4184 if (omnipresent) {
4185 if (iconCanBeOmnipresent(aicon)) {
4186 aicon->omnipresent = 1;
4187 new_entry = wmalloc(sizeof(WAppIconChain));
4188 new_entry->aicon = aicon;
4189 new_entry->next = scr->global_icons;
4190 scr->global_icons = new_entry;
4191 scr->global_icon_count++;
4192 } else {
4193 aicon->omnipresent = 0;
4194 status = WO_FAILED;
4196 } else {
4197 aicon->omnipresent = 0;
4198 if (aicon == scr->global_icons->aicon) {
4199 tmp = scr->global_icons->next;
4200 wfree(scr->global_icons);
4201 scr->global_icons = tmp;
4202 scr->global_icon_count--;
4203 } else {
4204 tmp = scr->global_icons;
4205 while (tmp->next) {
4206 if (tmp->next->aicon == aicon) {
4207 tmp1 = tmp->next->next;
4208 wfree(tmp->next);
4209 tmp->next = tmp1;
4210 scr->global_icon_count--;
4211 break;
4213 tmp = tmp->next;
4218 wAppIconPaint(aicon);
4220 return status;
4223 static void drawerAppendToChain(WScreen *scr, WDock *drawer)
4225 WDrawerChain **where_to_add;
4227 where_to_add = &scr->drawers;
4228 while ((*where_to_add) != NULL) {
4229 where_to_add = &(*where_to_add)->next;
4232 *where_to_add = wmalloc(sizeof(WDrawerChain));
4233 (*where_to_add)->adrawer = drawer;
4234 (*where_to_add)->next = NULL;
4235 scr->drawer_count++;
4239 static void drawerRemoveFromChain(WScreen *scr, WDock *drawer)
4241 WDrawerChain *next, **to_remove;
4243 to_remove = &scr->drawers;
4244 while (True) {
4245 if (*to_remove == NULL) {
4246 wwarning("The drawer to be removed can not be found.");
4247 return;
4249 if ((*to_remove)->adrawer == drawer)
4250 break;
4252 to_remove = &(*to_remove)->next;
4254 next = (*to_remove)->next;
4255 wfree(*to_remove);
4256 *to_remove = next;
4257 scr->drawer_count--;
4261 /* Don't free the returned string. Duplicate it. */
4262 static char * findUniqueName(WScreen *scr, const char *instance_basename)
4264 static char buffer[128];
4265 WDrawerChain *dc;
4266 int i;
4267 Bool already_in_use = True;
4269 #define UNIQUE_NAME_WATCHDOG 128
4270 for (i = 0; already_in_use && i < UNIQUE_NAME_WATCHDOG; i++) {
4271 snprintf(buffer, sizeof buffer, "%s%d", instance_basename, i);
4273 already_in_use = False;
4275 for (dc = scr->drawers; dc != NULL; dc = dc->next) {
4276 if (!strncmp(dc->adrawer->icon_array[0]->wm_instance, buffer,
4277 sizeof buffer)) {
4278 already_in_use = True;
4279 break;
4284 if (i == UNIQUE_NAME_WATCHDOG)
4285 wwarning("Couldn't find a unique name for drawer in %d attempts.", i);
4286 #undef UNIQUE_NAME_WATCHDOG
4288 return buffer;
4292 static void drawerIconExpose(WObjDescriptor *desc, XEvent *event)
4294 wDrawerIconPaint((WAppIcon *) desc->parent);
4298 static int addADrawer(WScreen *scr)
4300 int i, y, sig, found_y;
4301 WDock *drawer, *dock = scr->dock;
4302 WDrawerChain *dc;
4303 char can_be_here[2 * dock->max_icons - 1];
4305 if (dock->icon_count + scr->drawer_count >= dock->max_icons)
4306 return -1;
4308 for (y = -dock->max_icons + 1; y < dock->max_icons; y++) {
4309 can_be_here[y + dock->max_icons - 1] = True;
4311 for (i = 0; i < dock->max_icons; i++) {
4312 if (dock->icon_array[i] != NULL)
4313 can_be_here[dock->icon_array[i]->yindex + dock->max_icons - 1] = False;
4315 for (dc = scr->drawers; dc != NULL; dc = dc->next) {
4316 y = (int) ((dc->adrawer->y_pos - dock->y_pos) / ICON_SIZE);
4317 can_be_here[y + dock->max_icons - 1] = False;
4320 found_y = False;
4321 for (sig = 1; !found_y && sig > -2; sig -= 2) // 1, then -1
4323 for (y = sig; sig * y < dock->max_icons; y += sig)
4325 if (can_be_here[y + dock->max_icons - 1] &&
4326 onScreen(scr, dock->x_pos, dock->y_pos + y * ICON_SIZE))
4328 found_y = True;
4329 break;
4334 if (!found_y)
4335 /* This can happen even when dock->icon_count + scr->drawer_count
4336 * < dock->max_icons when the dock is not aligned on an
4337 * ICON_SIZE multiple, as some space is lost above and under it */
4338 return -1;
4340 drawer = wDockCreate(scr, WM_DRAWER, NULL);
4341 drawer->lowered = scr->dock->lowered;
4342 if (!drawer->lowered)
4343 ChangeStackingLevel(drawer->icon_array[0]->icon->core, WMDockLevel);
4344 else
4345 ChangeStackingLevel(drawer->icon_array[0]->icon->core, WMNormalLevel);
4346 drawer->auto_raise_lower = scr->dock->auto_raise_lower;
4347 drawer->x_pos = dock->x_pos;
4348 drawer->y_pos = dock->y_pos + ICON_SIZE * y;
4349 drawer->icon_array[0]->xindex = 0;
4350 drawer->icon_array[0]->yindex = 0;
4351 drawer->icon_array[0]->x_pos = drawer->x_pos;
4352 drawer->icon_array[0]->y_pos = drawer->y_pos;
4353 XMoveWindow(dpy, drawer->icon_array[0]->icon->core->window,
4354 drawer->icon_array[0]->x_pos, drawer->icon_array[0]->y_pos);
4356 return 0;
4360 static void addADrawerCallback(WMenu *menu, WMenuEntry *entry)
4362 assert(entry->clientdata!=NULL);
4363 addADrawer(((WAppIcon *) entry->clientdata)->dock->screen_ptr);
4367 static void drawerDestroy(WDock *drawer)
4369 WScreen *scr;
4370 int i;
4371 WAppIcon *aicon;
4372 WMArray *icons;
4374 if (drawer == NULL)
4375 return;
4377 scr = drawer->screen_ptr;
4379 /* Note regarding menus: we can't delete any dock/clip/drawer menu, because
4380 * that would (attempt to) wfree some memory in gettext library (see menu
4381 * entries that have several "versions", such like "Hide" and "Unhide"). */
4383 wDefaultPurgeInfo(scr, drawer->icon_array[0]->wm_instance,
4384 drawer->icon_array[0]->wm_class);
4385 if (drawer->icon_count == 2) {
4386 // Drawer contains a single appicon: dock it where the drawer was
4387 for (i = 1; i < drawer->max_icons; i++) {
4388 if ((aicon = drawer->icon_array[i]))
4389 break;
4391 wDockMoveIconBetweenDocks(drawer, scr->dock, aicon,
4392 0, (drawer->y_pos - scr->dock->y_pos) / ICON_SIZE);
4393 XMoveWindow(dpy, aicon->icon->core->window, drawer->x_pos, drawer->y_pos);
4394 XMapWindow(dpy, aicon->icon->core->window);
4396 else if (drawer->icon_count > 2) {
4397 icons = WMCreateArray(drawer->icon_count - 1);
4398 for (i = 1; i < drawer->max_icons; i++) {
4399 if (!(aicon = drawer->icon_array[i]))
4400 continue;
4401 WMAddToArray(icons, aicon);
4403 removeIcons(icons, drawer);
4406 if (drawer->auto_collapse_magic) {
4407 WMDeleteTimerHandler(drawer->auto_collapse_magic);
4408 drawer->auto_collapse_magic = NULL;
4410 if (drawer->auto_lower_magic) {
4411 WMDeleteTimerHandler(drawer->auto_lower_magic);
4412 drawer->auto_lower_magic = NULL;
4415 wAppIconDestroy(drawer->icon_array[0]);
4416 wfree(drawer->icon_array);
4417 wfree(drawer);
4419 drawerRemoveFromChain(scr, drawer);
4420 if (scr->last_dock == drawer)
4421 scr->last_dock = NULL;
4422 if (scr->attracting_drawer == drawer)
4423 scr->attracting_drawer = NULL;
4427 static void removeDrawerCallback(WMenu *menu, WMenuEntry *entry)
4429 WDock *dock = ((WAppIcon*)entry->clientdata)->dock;
4430 assert(dock != NULL);
4432 if (dock->icon_count > 2) {
4433 if (wMessageDialog(dock->screen_ptr, _("Drawer"),
4434 _("All icons in this drawer will be detached!"),
4435 _("OK"), _("Cancel"), NULL) != WAPRDefault)
4436 return;
4438 drawerDestroy(dock);
4442 void wDrawerIconPaint(WAppIcon *dicon)
4444 Window win = dicon->icon->core->window;
4445 WScreen *scr = dicon->icon->core->screen_ptr;
4446 XPoint p[4];
4447 GC gc = scr->draw_gc;
4448 WMColor *color;
4450 wIconPaint(dicon->icon);
4452 if (!dicon->dock->collapsed)
4453 color = scr->clip_title_color[CLIP_NORMAL];
4454 else
4455 color = scr->clip_title_color[CLIP_COLLAPSED];
4456 XSetForeground(dpy, gc, WMColorPixel(color));
4458 if (dicon->dock->on_right_side) {
4459 p[0].x = p[3].x = 10;
4460 p[0].y = p[3].y = ICON_SIZE / 2 - 5;
4461 p[1].x = 10;
4462 p[1].y = ICON_SIZE / 2 + 5;
4463 p[2].x = 5;
4464 p[2].y = ICON_SIZE / 2;
4466 else {
4467 p[0].x = p[3].x = ICON_SIZE-1 - 10;
4468 p[0].y = p[3].y = ICON_SIZE / 2 - 5;
4469 p[1].x = ICON_SIZE-1 - 10;
4470 p[1].y = ICON_SIZE / 2 + 5;
4471 p[2].x = ICON_SIZE-1 - 5;
4472 p[2].y = ICON_SIZE / 2;
4474 XFillPolygon(dpy, win, gc, p,3,Convex,CoordModeOrigin);
4475 XDrawLines(dpy, win, gc, p,4,CoordModeOrigin);
4479 RImage* wDrawerMakeTile(WScreen *scr, RImage *normalTile)
4481 RImage *tile = RCloneImage(normalTile);
4482 RColor dark;
4483 RColor light;
4485 dark.alpha = 0;
4486 dark.red = dark.green = dark.blue = 60;
4488 light.alpha = 0;
4489 light.red = light.green = light.blue = 80;
4491 /* arrow bevel */
4492 if (!scr->dock || scr->dock->on_right_side) {
4493 ROperateLine(tile, RSubtractOperation, 11, ICON_SIZE / 2 - 7,
4494 4, ICON_SIZE / 2, &dark); /* / */
4495 ROperateLine(tile, RSubtractOperation, 11, ICON_SIZE / 2 + 7,
4496 4, ICON_SIZE / 2, &dark); /* \ */
4497 ROperateLine(tile, RAddOperation, 11, ICON_SIZE / 2 - 7,
4498 11, ICON_SIZE / 2 + 7, &light); /* | */
4500 else {
4501 ROperateLine(tile, RSubtractOperation, ICON_SIZE-1 - 11, ICON_SIZE / 2 - 7,
4502 ICON_SIZE-1 - 4, ICON_SIZE / 2, &dark); /* \ */
4503 ROperateLine(tile, RAddOperation, ICON_SIZE-1 - 11, ICON_SIZE / 2 + 7,
4504 ICON_SIZE-1 - 4, ICON_SIZE / 2, &light); /* / */
4505 ROperateLine(tile, RSubtractOperation, ICON_SIZE-1 - 11, ICON_SIZE / 2 - 7,
4506 ICON_SIZE-1 - 11, ICON_SIZE / 2 + 7, &dark); /* | */
4508 return tile;
4512 static void swapDrawer(WDock *drawer, int new_x)
4514 int i;
4516 drawer->on_right_side = !drawer->on_right_side;
4517 drawer->x_pos = new_x;
4519 for (i = 0; i < drawer->max_icons; i++) {
4520 WAppIcon *ai;
4521 if ((ai = drawer->icon_array[i]) == NULL)
4522 continue;
4523 ai->xindex *= -1; /* so A B C becomes C B A */
4524 ai->x_pos = new_x + ai->xindex * ICON_SIZE;
4526 /* Update drawer's tile */
4527 if (i == 0) {
4528 wIconUpdate(ai->icon);
4529 wDrawerIconPaint(ai);
4531 XMoveWindow(dpy, ai->icon->core->window, ai->x_pos, ai->y_pos);
4536 static void swapDrawers(WScreen *scr, int on_right_side, int new_x)
4538 WDrawerChain *dc;
4540 if (scr->drawer_tile) {
4541 RReleaseImage(scr->drawer_tile);
4543 scr->drawer_tile = wDrawerMakeTile(scr, scr->icon_tile);
4545 for (dc = scr->drawers; dc != NULL; dc = dc->next) {
4546 swapDrawer(dc->adrawer, new_x);
4547 assert(dc->adrawer->on_right_side == on_right_side);
4552 int wIsADrawer(WAppIcon *aicon)
4554 return aicon && aicon->dock &&
4555 aicon->dock->type == WM_DRAWER && aicon->dock->icon_array[0] == aicon;
4559 static WDock* getDrawer(WScreen *scr, int y_index)
4561 WDrawerChain *dc;
4563 for (dc = scr->drawers; dc != NULL; dc = dc->next) {
4564 if (dc->adrawer->y_pos - scr->dock->y_pos == y_index * ICON_SIZE)
4565 return dc->adrawer;
4567 return NULL;
4572 /* Find the "hole" a moving appicon created when snapped into the
4573 * drawer. redocking is a boolean. If the moving appicon comes from the
4574 * drawer, drawer->icon_count is correct. If not, redocking is then false and
4575 * there are now drawer->icon_count plus one appicons in the drawer. */
4576 static int indexOfHole(WDock *drawer, WAppIcon *moving_aicon, int redocking)
4578 int index_of_hole, i;
4580 /* Classic interview question...
4582 * We have n-1 (n = drawer->icon_count-1 or drawer->icon_count, see
4583 * redocking) appicons, whose xindex are unique in [1..n]. One is missing:
4584 * that's where the ghost of the moving appicon is, that's what the
4585 * function should return.
4587 * We compute 1+2+...+n (this sum is equal to n*(n+1)/2), we substract to
4588 * this sum the xindex of each of the n-1 appicons, and we get the correct
4589 * index! */
4591 if (redocking) {
4592 index_of_hole = (drawer->icon_count - 1) * drawer->icon_count / 2;
4593 } else {
4594 index_of_hole = drawer->icon_count * (drawer->icon_count + 1) / 2;
4596 index_of_hole *= (drawer->on_right_side ? -1 : 1);
4598 for (i = 1; i < drawer->max_icons; i++) {
4599 if (drawer->icon_array[i] && drawer->icon_array[i] != moving_aicon)
4600 index_of_hole -= drawer->icon_array[i]->xindex;
4602 /* wmessage(" Index of the moving appicon is %d (%sredocking)", index_of_hole, (redocking ? "" : "not ")); */
4603 if (abs(index_of_hole) > abs(drawer->icon_count) - (redocking ? 1 : 0))
4604 wwarning(" index_of_hole is too large ! (%d greater than %d)",
4605 index_of_hole, abs(drawer->icon_count) - (redocking ? 1 : 0));
4606 if (index_of_hole == 0)
4607 wwarning(" index_of_hole == 0 (%sredocking, icon_count == %d)", (redocking ? "" : "not "), drawer->icon_count);
4609 return index_of_hole;
4613 void wSlideAppicons(WAppIcon **appicons, int n, int to_the_left)
4615 int i;
4616 int leftmost = -1, min_index = 9999, from_x = -1; // leftmost and from_x initialized to avoid warning
4617 Window *wins[n];
4618 WAppIcon *aicon;
4620 if (n < 1)
4621 return;
4623 for (i = 0; i < n; i++) {
4624 aicon = appicons[i];
4625 aicon->xindex += (to_the_left ? -1 : +1);
4626 if (aicon->xindex < min_index) {
4627 min_index = aicon->xindex;
4628 leftmost = i;
4629 from_x = aicon->x_pos;
4631 aicon->x_pos += (to_the_left ? -ICON_SIZE : +ICON_SIZE);
4634 for (i = 0; i < n; i++) {
4635 aicon = appicons[i];
4636 wins[aicon->xindex - min_index] = &aicon->icon->core->window;
4638 aicon = appicons[leftmost];
4639 SlideWindows(wins, n, from_x, aicon->y_pos, aicon->x_pos, aicon->y_pos);
4643 void wDrawerFillTheGap(WDock *drawer, WAppIcon *aicon, Bool redocking)
4645 int i, j;
4646 int index_of_hole = indexOfHole(drawer, aicon, redocking);
4647 WAppIcon *aicons_to_shift[drawer->icon_count];
4649 j = 0;
4650 for (i = 0; i < drawer->max_icons; i++) {
4651 WAppIcon *ai = drawer->icon_array[i];
4652 if (ai && ai != aicon &&
4653 abs(ai->xindex) > abs(index_of_hole))
4654 aicons_to_shift[j++] = ai;
4656 if (j != drawer->icon_count - abs(index_of_hole) - (redocking ? 1 : 0))
4657 wwarning("Removing aicon at index %d from %s: j=%d but should be %d",
4658 index_of_hole, drawer->icon_array[0]->wm_instance,
4659 j, drawer->icon_count - abs(index_of_hole) - (redocking ? 1 : 0));
4660 wSlideAppicons(aicons_to_shift, j, !drawer->on_right_side);
4664 static void drawerConsolidateIcons(WDock *drawer)
4666 WAppIcon *aicons_to_shift[drawer->icon_count];
4667 int maxRemaining = 0;
4668 int sum = 0;
4669 int i;
4670 for (i = 0; i < drawer->max_icons; i++) {
4671 WAppIcon *ai = drawer->icon_array[i];
4672 if (ai == NULL)
4673 continue;
4674 sum += abs(ai->xindex);
4675 if (abs(ai->xindex) > maxRemaining)
4676 maxRemaining = abs(ai->xindex);
4678 while (sum != maxRemaining * (maxRemaining + 1) / 2) { // while there is a hole
4679 WAppIcon *ai;
4680 int n;
4681 // Look up for the hole at max position
4682 int maxDeleted;
4683 for (maxDeleted = maxRemaining - 1; maxDeleted > 0; maxDeleted--) {
4684 Bool foundAppIconThere = False;
4685 for (i = 0; i < drawer->max_icons; i++) {
4686 WAppIcon *ai = drawer->icon_array[i];
4687 if (ai == NULL)
4688 continue;
4689 if (abs(ai->xindex) == maxDeleted) {
4690 foundAppIconThere = True;
4691 break;
4694 if (!foundAppIconThere)
4695 break;
4697 assert(maxDeleted > 0); // would mean while test is wrong
4698 n = 0;
4699 for (i = 0; i < drawer->max_icons; i++) {
4700 ai = drawer->icon_array[i];
4701 if (ai != NULL && abs(ai->xindex) > maxDeleted)
4702 aicons_to_shift[n++] = ai;
4704 assert(n == maxRemaining - maxDeleted); // for the code review ;-)
4705 wSlideAppicons(aicons_to_shift, n, !drawer->on_right_side);
4706 // Efficient beancounting
4707 maxRemaining -= 1;
4708 sum -= n;
4714 /* similar to wDockRestoreState, but a lot a specific stuff too... */
4715 static WDock * drawerRestoreState(WScreen *scr, WMPropList *drawer_state)
4717 WDock *drawer;
4718 WMPropList *apps, *value, *dock_state;
4719 WAppIcon *aicon;
4720 int count, i;
4722 if (!drawer_state)
4723 return NULL;
4725 make_keys();
4727 WMRetainPropList(drawer_state);
4729 /* Get the instance name, and create a drawer */
4730 value = WMGetFromPLDictionary(drawer_state, dName);
4731 drawer = wDockCreate(scr, WM_DRAWER, WMGetFromPLString(value));
4733 /* restore DnD command and paste command */
4734 #ifdef XDND
4735 value = WMGetFromPLDictionary(drawer_state, dDropCommand);
4736 if (value && WMIsPLString(value))
4737 drawer->icon_array[0]->dnd_command = wstrdup(WMGetFromPLString(value));
4738 #endif /* XDND */
4740 value = WMGetFromPLDictionary(drawer_state, dPasteCommand);
4741 if (value && WMIsPLString(value))
4742 drawer->icon_array[0]->paste_command = wstrdup(WMGetFromPLString(value));
4744 /* restore position */
4745 value = WMGetFromPLDictionary(drawer_state, dPosition);
4746 if (!value || !WMIsPLString(value))
4747 COMPLAIN("Position");
4748 else {
4749 int x, y, y_index;
4750 if (sscanf(WMGetFromPLString(value), "%i,%i", &x, &y) != 2)
4751 COMPLAIN("Position");
4753 /* check position sanity */
4754 if (x != scr->dock->x_pos) {
4755 x = scr->dock->x_pos;
4757 y_index = (y - scr->dock->y_pos) / ICON_SIZE;
4758 if (y_index >= scr->dock->max_icons) {
4759 /* Here we should do something more intelligent, since it
4760 * can happen even if the user hasn't hand-edited his
4761 * G/D/State file (but uses a lower resolution). */
4762 y_index = scr->dock->max_icons - 1;
4764 y = scr->dock->y_pos + y_index * ICON_SIZE;
4765 moveDock(drawer, x, y);
4768 /* restore dock properties (applist and others) */
4769 dock_state = WMGetFromPLDictionary(drawer_state, dDock);
4771 /* restore lowered/raised state: same as scr->dock, no matter what */
4772 drawer->lowered = scr->dock->lowered;
4773 if (!drawer->lowered)
4774 ChangeStackingLevel(drawer->icon_array[0]->icon->core, WMDockLevel);
4775 else
4776 ChangeStackingLevel(drawer->icon_array[0]->icon->core, WMNormalLevel);
4777 wRaiseFrame(drawer->icon_array[0]->icon->core);
4779 /* restore collapsed state */
4780 drawer->collapsed = 0;
4781 value = WMGetFromPLDictionary(dock_state, dCollapsed);
4782 if (value && strcasecmp(WMGetFromPLString(value), "YES") == 0) {
4783 drawer->collapsed = 1;
4785 /* restore auto-collapsed state */
4786 value = WMGetFromPLDictionary(dock_state, dAutoCollapse);
4787 if (value && strcasecmp(WMGetFromPLString(value), "YES") == 0) {
4788 drawer->auto_collapse = 1;
4789 drawer->collapsed = 1;
4790 } else {
4791 drawer->auto_collapse = 0; // because wDockCreate sets it (drawers only)
4794 /* restore auto-raise/lower state: same as scr->dock, no matter what */
4795 drawer->auto_raise_lower = scr->dock->auto_raise_lower;
4797 /* restore attract icons state */
4798 drawer->attract_icons = 0;
4799 value = WMGetFromPLDictionary(dock_state, dAutoAttractIcons);
4800 if (value && strcasecmp(WMGetFromPLString(value), "YES") == 0) {
4801 drawer->attract_icons = 1;
4802 scr->attracting_drawer = drawer;
4805 /* application list */
4806 apps = WMGetFromPLDictionary(dock_state, dApplications);
4808 if (!apps) {
4809 goto finish;
4812 count = WMGetPropListItemCount(apps);
4814 if (count == 0)
4815 goto finish;
4817 for (i=0; i<count; i++) {
4818 if (drawer->icon_count >= drawer->max_icons) {
4819 wwarning(_("there are too many icons stored in drawer. Ignoring what doesn't fit"));
4820 break;
4823 value = WMGetFromPLArray(apps, i);
4824 aicon = restore_icon_state(scr, value, WM_DRAWER, drawer->icon_count);
4826 drawer->icon_array[drawer->icon_count] = aicon;
4828 if (aicon) {
4829 aicon->dock = drawer;
4830 aicon->x_pos = drawer->x_pos + (aicon->xindex * ICON_SIZE);
4831 aicon->y_pos = drawer->y_pos + (aicon->yindex * ICON_SIZE);
4833 if (!drawer->lowered)
4834 ChangeStackingLevel(aicon->icon->core, WMDockLevel);
4835 else
4836 ChangeStackingLevel(aicon->icon->core, WMNormalLevel);
4838 wCoreConfigure(aicon->icon->core, aicon->x_pos, aicon->y_pos, 0, 0);
4840 if (!drawer->collapsed)
4841 XMapWindow(dpy, aicon->icon->core->window);
4842 wRaiseFrame(aicon->icon->core);
4844 drawer->icon_count++;
4848 finish:
4849 WMReleasePropList(drawer_state);
4851 return drawer;
4855 /* Same kind of comment than for previous function: this function is
4856 * very similar to make_icon_state, but has substential differences as
4857 * well. */
4858 static WMPropList *drawerSaveState(WScreen *scr, WDock *drawer)
4860 WMPropList *pstr, *drawer_state;
4861 WAppIcon *ai;
4862 char buffer[64];
4864 ai = drawer->icon_array[0];
4865 /* Store its name */
4866 pstr = WMCreatePLString(ai->wm_instance);
4867 drawer_state = WMCreatePLDictionary(dName, pstr, NULL); /* we need this final NULL */
4868 WMReleasePropList(pstr);
4870 /* Store its position */
4871 snprintf(buffer, sizeof(buffer), "%i,%i", ai->x_pos, ai->y_pos);
4872 pstr = WMCreatePLString(buffer);
4873 WMPutInPLDictionary(drawer_state, dPosition, pstr);
4874 WMReleasePropList(pstr);
4876 #ifdef XDND
4877 /* Store its DnD command */
4878 if (ai->dnd_command) {
4879 pstr = WMCreatePLString(ai->dnd_command);
4880 WMPutInPLDictionary(drawer_state, dDropCommand, pstr);
4881 WMReleasePropList(pstr);
4883 #endif /* XDND */
4885 /* Store its paste command */
4886 if (ai->paste_command) {
4887 pstr = WMCreatePLString(ai->paste_command);
4888 WMPutInPLDictionary(drawer_state, dPasteCommand, pstr);
4889 WMReleasePropList(pstr);
4892 /* Store applications list and other properties */
4893 pstr = dockSaveState(drawer);
4894 WMPutInPLDictionary(drawer_state, dDock, pstr);
4895 WMReleasePropList(pstr);
4897 return drawer_state;
4901 void wDrawersSaveState(WScreen *scr)
4903 WMPropList *all_drawers, *drawer_state;
4904 int i;
4905 WDrawerChain *dc;
4907 make_keys();
4909 all_drawers = WMCreatePLArray(NULL);
4910 for (i=0, dc = scr->drawers;
4911 i < scr->drawer_count;
4912 i++, dc = dc->next) {
4913 drawer_state = drawerSaveState(scr, dc->adrawer);
4914 WMAddToPLArray(all_drawers, drawer_state);
4915 WMReleasePropList(drawer_state);
4917 WMPutInPLDictionary(w_global.session_state, dDrawers, all_drawers);
4918 WMReleasePropList(all_drawers);
4922 void wDrawersRestoreState(WScreen *scr)
4924 WMPropList *all_drawers, *drawer_state;
4925 int i;
4927 make_keys();
4929 if (w_global.session_state == NULL)
4930 return;
4932 all_drawers = WMGetFromPLDictionary(w_global.session_state, dDrawers);
4933 if (!all_drawers)
4934 return;
4936 for (i = 0; i < WMGetPropListItemCount(all_drawers); i++) {
4937 drawer_state = WMGetFromPLArray(all_drawers, i);
4938 drawerRestoreState(scr, drawer_state);
4939 // Note: scr->drawers was updated when the the drawer was created