Fix compiler warning from menu.c
[wmaker-crm.git] / src / dock.c
bloba0c4f2ddc5e5bc74901d289b1815579cea2e3b01
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 USE_DOCK_XDND
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 int status, void *cdata);
97 static int getClipButton(int px, int py);
99 static void toggleLowered(WDock *dock);
101 static void toggleCollapsed(WDock *dock);
103 static void clipIconExpose(WObjDescriptor *desc, XEvent *event);
105 static void clipLeave(WDock *dock);
107 static void handleClipChangeWorkspace(WScreen *scr, XEvent *event);
109 static void clipEnterNotify(WObjDescriptor *desc, XEvent *event);
110 static void clipLeaveNotify(WObjDescriptor *desc, XEvent *event);
111 static void clipAutoCollapse(void *cdata);
112 static void clipAutoExpand(void *cdata);
113 static void launchDockedApplication(WAppIcon *btn, Bool withSelection);
115 static void clipAutoLower(void *cdata);
116 static void clipAutoRaise(void *cdata);
117 static WAppIcon *mainIconCreate(WScreen *scr, int type, const char *name);
119 static void drawerIconExpose(WObjDescriptor *desc, XEvent *event);
120 static void removeDrawerCallback(WMenu *menu, WMenuEntry *entry);
121 static void drawerAppendToChain(WScreen *scr, WDock *drawer);
122 static char *findUniqueName(WScreen *scr, const char *instance_basename);
123 static void addADrawerCallback(WMenu *menu, WMenuEntry *entry);
124 static void swapDrawers(WScreen *scr, int new_x);
125 static WDock* getDrawer(WScreen *scr, int y_index);
126 static int indexOfHole(WDock *drawer, WAppIcon *moving_aicon, int redocking);
127 static void drawerConsolidateIcons(WDock *drawer);
129 static int onScreen(WScreen *scr, int x, int y);
131 static void make_keys(void)
133 if (dCommand != NULL)
134 return;
136 dCommand = WMRetainPropList(WMCreatePLString("Command"));
137 dPasteCommand = WMRetainPropList(WMCreatePLString("PasteCommand"));
138 #ifdef USE_DOCK_XDND
139 dDropCommand = WMRetainPropList(WMCreatePLString("DropCommand"));
140 #endif
141 dLock = WMRetainPropList(WMCreatePLString("Lock"));
142 dAutoLaunch = WMRetainPropList(WMCreatePLString("AutoLaunch"));
143 dName = WMRetainPropList(WMCreatePLString("Name"));
144 dForced = WMRetainPropList(WMCreatePLString("Forced"));
145 dBuggyApplication = WMRetainPropList(WMCreatePLString("BuggyApplication"));
146 dYes = WMRetainPropList(WMCreatePLString("Yes"));
147 dNo = WMRetainPropList(WMCreatePLString("No"));
148 dHost = WMRetainPropList(WMCreatePLString("Host"));
150 dPosition = WMCreatePLString("Position");
151 dApplications = WMCreatePLString("Applications");
152 dLowered = WMCreatePLString("Lowered");
153 dCollapsed = WMCreatePLString("Collapsed");
154 dAutoCollapse = WMCreatePLString("AutoCollapse");
155 dAutoRaiseLower = WMCreatePLString("AutoRaiseLower");
156 dAutoAttractIcons = WMCreatePLString("AutoAttractIcons");
158 dOmnipresent = WMCreatePLString("Omnipresent");
160 dDock = WMCreatePLString("Dock");
161 dClip = WMCreatePLString("Clip");
162 dDrawers = WMCreatePLString("Drawers");
165 static void renameCallback(WMenu *menu, WMenuEntry *entry)
167 WDock *dock = entry->clientdata;
168 char buffer[128];
169 int wspace;
170 char *name;
172 /* Parameter not used, but tell the compiler that it is ok */
173 (void) menu;
175 assert(entry->clientdata != NULL);
177 wspace = dock->screen_ptr->current_workspace;
179 name = wstrdup(dock->screen_ptr->workspaces[wspace]->name);
181 snprintf(buffer, sizeof(buffer), _("Type the name for workspace %i:"), wspace + 1);
182 if (wInputDialog(dock->screen_ptr, _("Rename Workspace"), buffer, &name))
183 wWorkspaceRename(dock->screen_ptr, wspace, name);
185 wfree(name);
188 static void toggleLoweredCallback(WMenu *menu, WMenuEntry *entry)
190 assert(entry->clientdata != NULL);
192 toggleLowered(entry->clientdata);
194 entry->flags.indicator_on = !(((WDock *) entry->clientdata)->lowered);
196 wMenuPaint(menu);
199 static int matchWindow(const void *item, const void *cdata)
201 return (((WFakeGroupLeader *) item)->leader == (Window) cdata);
204 static void killCallback(WMenu *menu, WMenuEntry *entry)
206 WScreen *scr = menu->menu->screen_ptr;
207 WAppIcon *icon;
208 WFakeGroupLeader *fPtr;
209 char *buffer, *shortname, **argv;
210 int argc;
212 if (!WCHECK_STATE(WSTATE_NORMAL))
213 return;
215 assert(entry->clientdata != NULL);
217 icon = (WAppIcon *) entry->clientdata;
219 icon->editing = 1;
221 WCHANGE_STATE(WSTATE_MODAL);
223 /* strip away dir names */
224 shortname = basename(icon->command);
225 /* separate out command options */
226 wtokensplit(shortname, &argv, &argc);
228 buffer = wstrconcat(argv[0],
229 _(" will be forcibly closed.\n"
230 "Any unsaved changes will be lost.\n" "Please confirm."));
232 if (icon->icon && icon->icon->owner) {
233 fPtr = icon->icon->owner->fake_group;
234 } else {
235 /* is this really necessary? can we kill a non-running dock icon? */
236 Window win = icon->main_window;
237 int index;
239 index = WMFindInArray(scr->fakeGroupLeaders, matchWindow, (void *)win);
240 if (index != WANotFound)
241 fPtr = WMGetFromArray(scr->fakeGroupLeaders, index);
242 else
243 fPtr = NULL;
246 if (wPreferences.dont_confirm_kill
247 || wMessageDialog(menu->frame->screen_ptr, _("Kill Application"),
248 buffer, _("Yes"), _("No"), NULL) == WAPRDefault) {
249 if (fPtr != NULL) {
250 WWindow *wwin, *twin;
252 wwin = scr->focused_window;
253 while (wwin) {
254 twin = wwin->prev;
255 if (wwin->fake_group == fPtr)
256 wClientKill(wwin);
258 wwin = twin;
260 } else if (icon->icon && icon->icon->owner) {
261 wClientKill(icon->icon->owner);
265 wfree(buffer);
266 wtokenfree(argv, argc);
268 icon->editing = 0;
270 WCHANGE_STATE(WSTATE_NORMAL);
273 /* TODO: replace this function with a member of the dock struct */
274 static int numberOfSelectedIcons(WDock *dock)
276 WAppIcon *aicon;
277 int i, n;
279 n = 0;
280 for (i = 1; i < dock->max_icons; i++) {
281 aicon = dock->icon_array[i];
282 if (aicon && aicon->icon->selected)
283 n++;
286 return n;
289 static WMArray *getSelected(WDock *dock)
291 WMArray *ret = WMCreateArray(8);
292 WAppIcon *btn;
293 int i;
295 for (i = 1; i < dock->max_icons; i++) {
296 btn = dock->icon_array[i];
297 if (btn && btn->icon->selected)
298 WMAddToArray(ret, btn);
301 return ret;
304 static void paintClipButtons(WAppIcon *clipIcon, Bool lpushed, Bool rpushed)
306 Window win = clipIcon->icon->core->window;
307 WScreen *scr = clipIcon->icon->core->screen_ptr;
308 XPoint p[4];
309 int pt = CLIP_BUTTON_SIZE * ICON_SIZE / 64;
310 int tp = ICON_SIZE - pt;
311 int as = pt - 15; /* 15 = 5+5+5 */
312 GC gc = scr->draw_gc; /* maybe use WMColorGC() instead here? */
313 WMColor *color;
315 color = scr->clip_title_color[CLIP_NORMAL];
317 XSetForeground(dpy, gc, WMColorPixel(color));
319 if (rpushed) {
320 p[0].x = tp + 1;
321 p[0].y = 1;
322 p[1].x = ICON_SIZE - 2;
323 p[1].y = 1;
324 p[2].x = ICON_SIZE - 2;
325 p[2].y = pt - 1;
326 } else if (lpushed) {
327 p[0].x = 1;
328 p[0].y = tp;
329 p[1].x = pt;
330 p[1].y = ICON_SIZE - 2;
331 p[2].x = 1;
332 p[2].y = ICON_SIZE - 2;
334 if (lpushed || rpushed) {
335 XSetForeground(dpy, scr->draw_gc, scr->white_pixel);
336 XFillPolygon(dpy, win, scr->draw_gc, p, 3, Convex, CoordModeOrigin);
337 XSetForeground(dpy, scr->draw_gc, scr->black_pixel);
340 /* top right arrow */
341 p[0].x = p[3].x = ICON_SIZE - 5 - as;
342 p[0].y = p[3].y = 5;
343 p[1].x = ICON_SIZE - 6;
344 p[1].y = 5;
345 p[2].x = ICON_SIZE - 6;
346 p[2].y = 4 + as;
347 if (rpushed) {
348 XFillPolygon(dpy, win, scr->draw_gc, p, 3, Convex, CoordModeOrigin);
349 XDrawLines(dpy, win, scr->draw_gc, p, 4, CoordModeOrigin);
350 } else {
351 XFillPolygon(dpy, win, gc, p, 3, Convex, CoordModeOrigin);
352 XDrawLines(dpy, win, gc, p, 4, CoordModeOrigin);
355 /* bottom left arrow */
356 p[0].x = p[3].x = 5;
357 p[0].y = p[3].y = ICON_SIZE - 5 - as;
358 p[1].x = 5;
359 p[1].y = ICON_SIZE - 6;
360 p[2].x = 4 + as;
361 p[2].y = ICON_SIZE - 6;
362 if (lpushed) {
363 XFillPolygon(dpy, win, scr->draw_gc, p, 3, Convex, CoordModeOrigin);
364 XDrawLines(dpy, win, scr->draw_gc, p, 4, CoordModeOrigin);
365 } else {
366 XFillPolygon(dpy, win, gc, p, 3, Convex, CoordModeOrigin);
367 XDrawLines(dpy, win, gc, p, 4, CoordModeOrigin);
371 RImage *wClipMakeTile(RImage *normalTile)
373 RImage *tile = RCloneImage(normalTile);
374 RColor black;
375 RColor dark;
376 RColor light;
377 int pt, tp;
378 int as;
380 pt = CLIP_BUTTON_SIZE * wPreferences.icon_size / 64;
381 tp = wPreferences.icon_size - 1 - pt;
382 as = pt - 15;
384 black.alpha = 255;
385 black.red = black.green = black.blue = 0;
387 dark.alpha = 0;
388 dark.red = dark.green = dark.blue = 60;
390 light.alpha = 0;
391 light.red = light.green = light.blue = 80;
393 /* top right */
394 ROperateLine(tile, RSubtractOperation, tp, 0, wPreferences.icon_size - 2, pt - 1, &dark);
395 RDrawLine(tile, tp - 1, 0, wPreferences.icon_size - 1, pt + 1, &black);
396 ROperateLine(tile, RAddOperation, tp, 2, wPreferences.icon_size - 3, pt, &light);
398 /* arrow bevel */
399 ROperateLine(tile, RSubtractOperation, ICON_SIZE - 7 - as, 4, ICON_SIZE - 5, 4, &dark);
400 ROperateLine(tile, RSubtractOperation, ICON_SIZE - 6 - as, 5, ICON_SIZE - 5, 6 + as, &dark);
401 ROperateLine(tile, RAddOperation, ICON_SIZE - 5, 4, ICON_SIZE - 5, 6 + as, &light);
403 /* bottom left */
404 ROperateLine(tile, RAddOperation, 2, tp + 2, pt - 2, wPreferences.icon_size - 3, &dark);
405 RDrawLine(tile, 0, tp - 1, pt + 1, wPreferences.icon_size - 1, &black);
406 ROperateLine(tile, RSubtractOperation, 0, tp - 2, pt + 1, wPreferences.icon_size - 2, &light);
408 /* arrow bevel */
409 ROperateLine(tile, RSubtractOperation, 4, ICON_SIZE - 7 - as, 4, ICON_SIZE - 5, &dark);
410 ROperateLine(tile, RSubtractOperation, 5, ICON_SIZE - 6 - as, 6 + as, ICON_SIZE - 5, &dark);
411 ROperateLine(tile, RAddOperation, 4, ICON_SIZE - 5, 6 + as, ICON_SIZE - 5, &light);
413 return tile;
416 static void omnipresentCallback(WMenu *menu, WMenuEntry *entry)
418 WAppIcon *clickedIcon = entry->clientdata;
419 WAppIcon *aicon;
420 WDock *dock;
421 WMArray *selectedIcons;
422 WMArrayIterator iter;
423 int failed;
425 /* Parameter not used, but tell the compiler that it is ok */
426 (void) menu;
428 assert(entry->clientdata != NULL);
430 dock = clickedIcon->dock;
432 selectedIcons = getSelected(dock);
434 if (!WMGetArrayItemCount(selectedIcons))
435 WMAddToArray(selectedIcons, clickedIcon);
437 failed = 0;
438 WM_ITERATE_ARRAY(selectedIcons, aicon, iter) {
439 if (wClipMakeIconOmnipresent(aicon, !aicon->omnipresent) == WO_FAILED)
440 failed++;
441 else if (aicon->icon->selected)
442 wIconSelect(aicon->icon);
444 WMFreeArray(selectedIcons);
446 if (failed > 1) {
447 wMessageDialog(dock->screen_ptr, _("Warning"),
448 _("Some icons cannot be made omnipresent. "
449 "Please make sure that no other icon is "
450 "docked in the same positions on the other "
451 "workspaces and the Clip is not full in "
452 "some workspace."), _("OK"), NULL, NULL);
453 } else if (failed == 1) {
454 wMessageDialog(dock->screen_ptr, _("Warning"),
455 _("Icon cannot be made omnipresent. "
456 "Please make sure that no other icon is "
457 "docked in the same position on the other "
458 "workspaces and the Clip is not full in "
459 "some workspace."), _("OK"), NULL, NULL);
463 static void removeIcons(WMArray *icons, WDock *dock)
465 WAppIcon *aicon;
466 int keepit;
467 WMArrayIterator it;
469 WM_ITERATE_ARRAY(icons, aicon, it) {
470 keepit = aicon->running && wApplicationOf(aicon->main_window);
471 wDockDetach(dock, aicon);
472 if (keepit) {
473 /* XXX: can: aicon->icon == NULL ? */
474 PlaceIcon(dock->screen_ptr, &aicon->x_pos, &aicon->y_pos,
475 wGetHeadForWindow(aicon->icon->owner));
476 XMoveWindow(dpy, aicon->icon->core->window, aicon->x_pos, aicon->y_pos);
477 if (!dock->mapped || dock->collapsed)
478 XMapWindow(dpy, aicon->icon->core->window);
481 WMFreeArray(icons);
483 if (wPreferences.auto_arrange_icons)
484 wArrangeIcons(dock->screen_ptr, True);
487 static void removeIconsCallback(WMenu *menu, WMenuEntry *entry)
489 WAppIcon *clickedIcon = (WAppIcon *) entry->clientdata;
490 WDock *dock;
491 WMArray *selectedIcons;
493 /* Parameter not used, but tell the compiler that it is ok */
494 (void) menu;
496 assert(clickedIcon != NULL);
498 dock = clickedIcon->dock;
500 selectedIcons = getSelected(dock);
502 if (WMGetArrayItemCount(selectedIcons)) {
503 if (wMessageDialog(dock->screen_ptr,
504 dock->type == WM_CLIP ? _("Workspace Clip") : _("Drawer"),
505 _("All selected icons will be removed!"),
506 _("OK"), _("Cancel"), NULL) != WAPRDefault) {
507 WMFreeArray(selectedIcons);
508 return;
510 } else {
511 if (clickedIcon->xindex == 0 && clickedIcon->yindex == 0) {
512 WMFreeArray(selectedIcons);
513 return;
515 WMAddToArray(selectedIcons, clickedIcon);
518 removeIcons(selectedIcons, dock);
520 if (dock->type == WM_DRAWER) {
521 drawerConsolidateIcons(dock);
525 static void keepIconsCallback(WMenu *menu, WMenuEntry *entry)
527 WAppIcon *clickedIcon = (WAppIcon *) entry->clientdata;
528 WDock *dock;
529 WAppIcon *aicon;
530 WMArray *selectedIcons;
531 WMArrayIterator it;
533 /* Parameter not used, but tell the compiler that it is ok */
534 (void) menu;
536 assert(clickedIcon != NULL);
537 dock = clickedIcon->dock;
539 selectedIcons = getSelected(dock);
541 if (!WMGetArrayItemCount(selectedIcons)
542 && clickedIcon != dock->screen_ptr->clip_icon) {
543 char *command = NULL;
545 if (!clickedIcon->command && !clickedIcon->editing) {
546 clickedIcon->editing = 1;
547 if (wInputDialog(dock->screen_ptr, _("Keep Icon"),
548 _("Type the command used to launch the application"), &command)) {
549 if (command && (command[0] == 0 || (command[0] == '-' && command[1] == 0))) {
550 wfree(command);
551 command = NULL;
553 clickedIcon->command = command;
554 clickedIcon->editing = 0;
555 } else {
556 clickedIcon->editing = 0;
557 if (command)
558 wfree(command);
559 WMFreeArray(selectedIcons);
560 return;
564 WMAddToArray(selectedIcons, clickedIcon);
567 WM_ITERATE_ARRAY(selectedIcons, aicon, it) {
568 if (aicon->icon->selected)
569 wIconSelect(aicon->icon);
571 if (aicon->attracted && aicon->command) {
572 aicon->attracted = 0;
573 if (aicon->icon->shadowed) {
574 aicon->icon->shadowed = 0;
577 * Update icon pixmap, RImage doesn't change,
578 * so call wIconUpdate is not needed
580 update_icon_pixmap(aicon->icon);
582 /* Paint it */
583 wAppIconPaint(aicon);
586 save_appicon(aicon);
588 WMFreeArray(selectedIcons);
591 static void toggleAutoAttractCallback(WMenu *menu, WMenuEntry *entry)
593 WDock *dock = (WDock *) entry->clientdata;
594 WScreen *scr = dock->screen_ptr;
596 assert(entry->clientdata != NULL);
598 dock->attract_icons = !dock->attract_icons;
600 entry->flags.indicator_on = dock->attract_icons;
602 wMenuPaint(menu);
604 if (dock->attract_icons) {
605 if (dock->type == WM_DRAWER) {
606 /* The newly auto-attracting dock is a drawer: disable any clip and
607 * previously attracting drawer */
609 if (!wPreferences.flags.noclip) {
610 int i;
611 for (i = 0; i < scr->workspace_count; i++)
612 scr->workspaces[ i ]->clip->attract_icons = False;
613 /* dock menu will be updated later, when opened */
616 if (scr->attracting_drawer != NULL)
617 scr->attracting_drawer->attract_icons = False;
618 scr->attracting_drawer = dock;
619 } else {
620 /* The newly auto-attracting dock is a clip: disable
621 * previously attracting drawer, if applicable */
622 if (scr->attracting_drawer != NULL) {
623 scr->attracting_drawer->attract_icons = False;
624 /* again, its menu will be updated, later. */
625 scr->attracting_drawer = NULL;
631 static void selectCallback(WMenu *menu, WMenuEntry *entry)
633 WAppIcon *icon = (WAppIcon *) entry->clientdata;
635 assert(icon != NULL);
637 wIconSelect(icon->icon);
639 wMenuPaint(menu);
642 static void attractIconsCallback(WMenu *menu, WMenuEntry *entry)
644 WAppIcon *clickedIcon = (WAppIcon *) entry->clientdata;
645 WDock *clip; /* clip... is a WM_CLIP or a WM_DRAWER */
646 WAppIcon *aicon;
647 int x, y, x_pos, y_pos;
648 Bool update_icon = False;
650 /* Parameter not used, but tell the compiler that it is ok */
651 (void) menu;
653 assert(entry->clientdata != NULL);
654 clip = clickedIcon->dock;
656 aicon = clip->screen_ptr->app_icon_list;
658 while (aicon) {
659 if (!aicon->docked && wDockFindFreeSlot(clip, &x, &y)) {
660 x_pos = clip->x_pos + x * ICON_SIZE;
661 y_pos = clip->y_pos + y * ICON_SIZE;
662 if (aicon->x_pos != x_pos || aicon->y_pos != y_pos)
663 move_window(aicon->icon->core->window, aicon->x_pos, aicon->y_pos, x_pos, y_pos);
665 aicon->attracted = 1;
666 if (!aicon->icon->shadowed) {
667 aicon->icon->shadowed = 1;
668 update_icon = True;
670 wDockAttachIcon(clip, aicon, x, y, update_icon);
671 if (clip->collapsed || !clip->mapped)
672 XUnmapWindow(dpy, aicon->icon->core->window);
674 aicon = aicon->next;
678 static void selectIconsCallback(WMenu *menu, WMenuEntry *entry)
680 WAppIcon *clickedIcon = (WAppIcon *) entry->clientdata;
681 WDock *dock;
682 WMArray *selectedIcons;
683 WMArrayIterator iter;
684 WAppIcon *btn;
685 int i;
687 assert(clickedIcon != NULL);
688 dock = clickedIcon->dock;
690 selectedIcons = getSelected(dock);
692 if (!WMGetArrayItemCount(selectedIcons)) {
693 for (i = 1; i < dock->max_icons; i++) {
694 btn = dock->icon_array[i];
695 if (btn && !btn->icon->selected)
696 wIconSelect(btn->icon);
698 } else {
699 WM_ITERATE_ARRAY(selectedIcons, btn, iter) {
700 wIconSelect(btn->icon);
703 WMFreeArray(selectedIcons);
705 wMenuPaint(menu);
708 static void toggleCollapsedCallback(WMenu *menu, WMenuEntry *entry)
710 assert(entry->clientdata != NULL);
712 toggleCollapsed(entry->clientdata);
714 entry->flags.indicator_on = ((WDock *) entry->clientdata)->collapsed;
716 wMenuPaint(menu);
719 static void toggleAutoCollapseCallback(WMenu *menu, WMenuEntry *entry)
721 WDock *dock;
722 assert(entry->clientdata != NULL);
724 dock = (WDock *) entry->clientdata;
726 dock->auto_collapse = !dock->auto_collapse;
728 entry->flags.indicator_on = ((WDock *) entry->clientdata)->auto_collapse;
730 wMenuPaint(menu);
733 static void toggleAutoRaiseLower(WDock *dock)
735 WDrawerChain *dc;
737 dock->auto_raise_lower = !dock->auto_raise_lower;
738 if (dock->type == WM_DOCK)
740 for (dc = dock->screen_ptr->drawers; dc != NULL; dc = dc->next) {
741 toggleAutoRaiseLower(dc->adrawer);
746 static void toggleAutoRaiseLowerCallback(WMenu *menu, WMenuEntry *entry)
748 WDock *dock;
750 /* Parameter not used, but tell the compiler that it is ok */
751 (void) menu;
753 assert(entry->clientdata != NULL);
755 dock = (WDock *) entry->clientdata;
757 toggleAutoRaiseLower(dock);
759 entry->flags.indicator_on = ((WDock *) entry->clientdata)->auto_raise_lower;
761 wMenuPaint(menu);
764 static void launchCallback(WMenu *menu, WMenuEntry *entry)
766 WAppIcon *btn = (WAppIcon *) entry->clientdata;
768 /* Parameter not used, but tell the compiler that it is ok */
769 (void) menu;
771 launchDockedApplication(btn, False);
774 static void settingsCallback(WMenu *menu, WMenuEntry *entry)
776 WAppIcon *btn = (WAppIcon *) entry->clientdata;
778 /* Parameter not used, but tell the compiler that it is ok */
779 (void) menu;
781 if (btn->editing)
782 return;
783 ShowDockAppSettingsPanel(btn);
786 static void hideCallback(WMenu *menu, WMenuEntry *entry)
788 WApplication *wapp;
789 WAppIcon *btn = (WAppIcon *) entry->clientdata;
791 /* Parameter not used, but tell the compiler that it is ok */
792 (void) menu;
794 wapp = wApplicationOf(btn->icon->owner->main_window);
796 if (wapp->flags.hidden) {
797 wWorkspaceChange(btn->icon->core->screen_ptr, wapp->last_workspace);
798 wUnhideApplication(wapp, False, False);
799 } else {
800 wHideApplication(wapp);
804 static void unhideHereCallback(WMenu *menu, WMenuEntry *entry)
806 WApplication *wapp;
807 WAppIcon *btn = (WAppIcon *) entry->clientdata;
809 /* Parameter not used, but tell the compiler that it is ok */
810 (void) menu;
812 wapp = wApplicationOf(btn->icon->owner->main_window);
814 wUnhideApplication(wapp, False, True);
817 static int getDockXPosition(WScreen *scr, Bool on_right_side)
819 int x;
821 if (wPreferences.keep_dock_on_primary_head) {
822 WMRect rect;
824 rect = wGetRectForHead(scr, scr->xine_info.primary_head);
825 x = rect.pos.x;
826 if (on_right_side)
827 x += rect.size.width - ICON_SIZE - DOCK_EXTRA_SPACE;
828 else
829 x += DOCK_EXTRA_SPACE;
830 } else {
831 if (on_right_side)
832 x = scr->scr_width - ICON_SIZE - DOCK_EXTRA_SPACE;
833 else
834 x = DOCK_EXTRA_SPACE;
837 return x;
840 /* Name is only used when type == WM_DRAWER and when restoring a specific
841 * drawer, with a specific name. When creating a drawer, leave name to NULL
842 * and mainIconCreate will find the first unused unique name */
843 static WAppIcon *mainIconCreate(WScreen *scr, int type, const char *name)
845 WAppIcon *btn;
846 int x_pos;
848 switch(type) {
849 case WM_CLIP:
850 if (scr->clip_icon)
851 return scr->clip_icon;
853 btn = wAppIconCreateForDock(scr, NULL, "Logo", "WMClip", TILE_CLIP);
854 btn->icon->core->descriptor.handle_expose = clipIconExpose;
855 x_pos = 0;
856 break;
857 case WM_DOCK:
858 default: /* to avoid a warning about btn and x_pos, basically */
859 btn = wAppIconCreateForDock(scr, NULL, "Logo", "WMDock", TILE_NORMAL);
860 if (wPreferences.flags.clip_merged_in_dock)
861 btn->icon->core->descriptor.handle_expose = clipIconExpose;
862 x_pos = getDockXPosition(scr, True);
863 break;
864 case WM_DRAWER:
865 if (name == NULL)
866 name = findUniqueName(scr, "Drawer");
867 btn = wAppIconCreateForDock(scr, NULL, name, "WMDrawer", TILE_DRAWER);
868 btn->icon->core->descriptor.handle_expose = drawerIconExpose;
869 x_pos = 0;
872 btn->xindex = 0;
873 btn->yindex = 0;
875 btn->icon->core->descriptor.handle_mousedown = iconMouseDown;
876 btn->icon->core->descriptor.handle_enternotify = clipEnterNotify;
877 btn->icon->core->descriptor.handle_leavenotify = clipLeaveNotify;
878 btn->icon->core->descriptor.parent_type = WCLASS_DOCK_ICON;
879 btn->icon->core->descriptor.parent = btn;
880 XMapWindow(dpy, btn->icon->core->window);
881 btn->x_pos = x_pos;
882 btn->y_pos = 0;
883 btn->docked = 1;
884 if (type == WM_CLIP ||
885 (type == WM_DOCK && wPreferences.flags.clip_merged_in_dock))
886 scr->clip_icon = btn;
888 return btn;
891 static void switchWSCommand(WMenu *menu, WMenuEntry *entry)
893 WAppIcon *btn, *icon = (WAppIcon *) entry->clientdata;
894 WScreen *scr = icon->icon->core->screen_ptr;
895 WDock *src, *dest;
896 WMArray *selectedIcons;
897 int x, y;
899 /* Parameter not used, but tell the compiler that it is ok */
900 (void) menu;
902 if (entry->order == scr->current_workspace)
903 return;
905 src = icon->dock;
906 dest = scr->workspaces[entry->order]->clip;
908 selectedIcons = getSelected(src);
910 if (WMGetArrayItemCount(selectedIcons)) {
911 WMArrayIterator iter;
913 WM_ITERATE_ARRAY(selectedIcons, btn, iter) {
914 if (wDockFindFreeSlot(dest, &x, &y)) {
915 wDockMoveIconBetweenDocks(src, dest, btn, x, y);
916 XUnmapWindow(dpy, btn->icon->core->window);
919 } else if (icon != scr->clip_icon) {
920 if (wDockFindFreeSlot(dest, &x, &y)) {
921 wDockMoveIconBetweenDocks(src, dest, icon, x, y);
922 XUnmapWindow(dpy, icon->icon->core->window);
925 WMFreeArray(selectedIcons);
928 static void launchDockedApplication(WAppIcon *btn, Bool withSelection)
930 WScreen *scr = btn->icon->core->screen_ptr;
932 if (!btn->launching &&
933 ((!withSelection && btn->command != NULL) || (withSelection && btn->paste_command != NULL))) {
934 if (!btn->forced_dock) {
935 btn->relaunching = btn->running;
936 btn->running = 1;
938 if (btn->wm_instance || btn->wm_class) {
939 WWindowAttributes attr;
940 memset(&attr, 0, sizeof(WWindowAttributes));
941 wDefaultFillAttributes(btn->wm_instance, btn->wm_class, &attr, NULL, True);
943 if (!attr.no_appicon && !btn->buggy_app)
944 btn->launching = 1;
945 else
946 btn->running = 0;
948 btn->drop_launch = 0;
949 btn->paste_launch = withSelection;
950 scr->last_dock = btn->dock;
951 btn->pid = execCommand(btn, (withSelection ? btn->paste_command : btn->command), NULL);
952 if (btn->pid > 0) {
953 if (btn->buggy_app) {
954 /* give feedback that the app was launched */
955 btn->launching = 1;
956 dockIconPaint(btn);
957 btn->launching = 0;
958 WMAddTimerHandler(200, (WMCallback *) dockIconPaint, btn);
959 } else {
960 dockIconPaint(btn);
962 } else {
963 wwarning(_("could not launch application %s"), btn->command);
964 btn->launching = 0;
965 if (!btn->relaunching)
966 btn->running = 0;
971 static void updateWorkspaceMenu(WMenu *menu, WAppIcon *icon)
973 WScreen *scr = menu->frame->screen_ptr;
974 int i;
976 if (!menu || !icon)
977 return;
979 for (i = 0; i < scr->workspace_count; i++) {
980 if (i < menu->entry_no) {
981 if (strcmp(menu->entries[i]->text, scr->workspaces[i]->name) != 0) {
982 wfree(menu->entries[i]->text);
983 menu->entries[i]->text = wstrdup(scr->workspaces[i]->name);
984 menu->flags.realized = 0;
986 menu->entries[i]->clientdata = (void *)icon;
987 } else {
988 wMenuAddCallback(menu, scr->workspaces[i]->name, switchWSCommand, (void *)icon);
990 menu->flags.realized = 0;
993 if (i == scr->current_workspace)
994 wMenuSetEnabled(menu, i, False);
995 else
996 wMenuSetEnabled(menu, i, True);
999 if (!menu->flags.realized)
1000 wMenuRealize(menu);
1003 static WMenu *makeWorkspaceMenu(WScreen *scr)
1005 WMenu *menu;
1007 menu = wMenuCreate(scr, NULL, False);
1008 if (!menu)
1009 wwarning(_("could not create workspace submenu for Clip menu"));
1011 wMenuAddCallback(menu, "", switchWSCommand, (void *)scr->clip_icon);
1013 menu->flags.realized = 0;
1014 wMenuRealize(menu);
1016 return menu;
1019 static void updateClipOptionsMenu(WMenu *menu, WDock *dock)
1021 WMenuEntry *entry;
1022 int index = 0;
1024 if (!menu || !dock)
1025 return;
1027 /* keep on top */
1028 entry = menu->entries[index];
1029 entry->flags.indicator_on = !dock->lowered;
1030 entry->clientdata = dock;
1031 wMenuSetEnabled(menu, index, dock->type == WM_CLIP);
1033 /* collapsed */
1034 entry = menu->entries[++index];
1035 entry->flags.indicator_on = dock->collapsed;
1036 entry->clientdata = dock;
1038 /* auto-collapse */
1039 entry = menu->entries[++index];
1040 entry->flags.indicator_on = dock->auto_collapse;
1041 entry->clientdata = dock;
1043 /* auto-raise/lower */
1044 entry = menu->entries[++index];
1045 entry->flags.indicator_on = dock->auto_raise_lower;
1046 entry->clientdata = dock;
1047 wMenuSetEnabled(menu, index, dock->lowered && (dock->type == WM_CLIP));
1049 /* attract icons */
1050 entry = menu->entries[++index];
1051 entry->flags.indicator_on = dock->attract_icons;
1052 entry->clientdata = dock;
1054 menu->flags.realized = 0;
1055 wMenuRealize(menu);
1059 static WMenu *makeClipOptionsMenu(WScreen *scr)
1061 WMenu *menu;
1062 WMenuEntry *entry;
1064 menu = wMenuCreate(scr, NULL, False);
1065 if (!menu) {
1066 wwarning(_("could not create options submenu for Clip menu"));
1067 return NULL;
1070 entry = wMenuAddCallback(menu, _("Keep on Top"), toggleLoweredCallback, NULL);
1071 entry->flags.indicator = 1;
1072 entry->flags.indicator_on = 1;
1073 entry->flags.indicator_type = MI_CHECK;
1075 entry = wMenuAddCallback(menu, _("Collapsed"), toggleCollapsedCallback, NULL);
1076 entry->flags.indicator = 1;
1077 entry->flags.indicator_on = 1;
1078 entry->flags.indicator_type = MI_CHECK;
1080 entry = wMenuAddCallback(menu, _("Autocollapse"), toggleAutoCollapseCallback, NULL);
1081 entry->flags.indicator = 1;
1082 entry->flags.indicator_on = 1;
1083 entry->flags.indicator_type = MI_CHECK;
1085 entry = wMenuAddCallback(menu, _("Autoraise"), toggleAutoRaiseLowerCallback, NULL);
1086 entry->flags.indicator = 1;
1087 entry->flags.indicator_on = 1;
1088 entry->flags.indicator_type = MI_CHECK;
1090 entry = wMenuAddCallback(menu, _("Autoattract Icons"), toggleAutoAttractCallback, NULL);
1091 entry->flags.indicator = 1;
1092 entry->flags.indicator_on = 1;
1093 entry->flags.indicator_type = MI_CHECK;
1095 menu->flags.realized = 0;
1096 wMenuRealize(menu);
1098 return menu;
1102 static void setDockPositionNormalCallback(WMenu *menu, WMenuEntry *entry)
1104 WDock *dock = (WDock *) entry->clientdata;
1105 WDrawerChain *dc;
1107 /* Parameter not used, but tell the compiler that it is ok */
1108 (void) menu;
1110 if (entry->flags.indicator_on) // already set, nothing to do
1111 return;
1112 // Do we come from auto raise lower or keep on top?
1113 if (dock->auto_raise_lower)
1115 dock->auto_raise_lower = 0;
1116 // Only for aesthetic purposes, can be removed when Autoraise status is no longer exposed in drawer option menu
1117 for (dc = dock->screen_ptr->drawers; dc != NULL; dc = dc->next) {
1118 dc->adrawer->auto_raise_lower = 0;
1121 else
1123 // Will take care of setting lowered = 0 in drawers
1124 toggleLowered(dock);
1126 entry->flags.indicator_on = 1;
1129 static void setDockPositionAutoRaiseLowerCallback(WMenu *menu, WMenuEntry *entry)
1131 WDock *dock = (WDock *) entry->clientdata;
1132 WDrawerChain *dc;
1134 /* Parameter not used, but tell the compiler that it is ok */
1135 (void) menu;
1137 if (entry->flags.indicator_on) // already set, nothing to do
1138 return;
1139 // Do we come from normal or keep on top?
1140 if (!dock->lowered)
1142 toggleLowered(dock);
1144 dock->auto_raise_lower = 1;
1145 // Only for aesthetic purposes, can be removed when Autoraise status is no longer exposed in drawer option menu
1146 for (dc = dock->screen_ptr->drawers; dc != NULL; dc = dc->next) {
1147 dc->adrawer->auto_raise_lower = 1;
1149 entry->flags.indicator_on = 1;
1152 static void setDockPositionKeepOnTopCallback(WMenu *menu, WMenuEntry *entry)
1154 WDock *dock = (WDock *) entry->clientdata;
1155 WDrawerChain *dc;
1157 /* Parameter not used, but tell the compiler that it is ok */
1158 (void) menu;
1160 if (entry->flags.indicator_on) // already set, nothing to do
1161 return;
1162 dock->auto_raise_lower = 0;
1163 // Only for aesthetic purposes, can be removed when Autoraise status is no longer exposed in drawer option menu
1164 for (dc = dock->screen_ptr->drawers; dc != NULL; dc = dc->next) {
1165 dc->adrawer->auto_raise_lower = 0;
1167 toggleLowered(dock);
1168 entry->flags.indicator_on = 1;
1171 static void updateDockPositionMenu(WMenu *menu, WDock *dock)
1173 WMenuEntry *entry;
1174 int index = 0;
1176 assert(menu);
1177 assert(dock);
1179 /* Normal level */
1180 entry = menu->entries[index++];
1181 entry->flags.indicator_on = (dock->lowered && !dock->auto_raise_lower);
1182 entry->clientdata = dock;
1184 /* Auto-raise/lower */
1185 entry = menu->entries[index++];
1186 entry->flags.indicator_on = dock->auto_raise_lower;
1187 entry->clientdata = dock;
1189 /* Keep on top */
1190 entry = menu->entries[index++];
1191 entry->flags.indicator_on = !dock->lowered;
1192 entry->clientdata = dock;
1195 static WMenu *makeDockPositionMenu(WScreen *scr)
1197 /* When calling this, the dock is being created, so scr->dock is still not set
1198 * Therefore the callbacks' clientdata and the indicators can't be set,
1199 * they will be updated when the dock menu is opened. */
1200 WMenu *menu;
1201 WMenuEntry *entry;
1203 menu = wMenuCreate(scr, NULL, False);
1204 if (!menu) {
1205 wwarning(_("could not create options submenu for dock position menu"));
1206 return NULL;
1209 entry = wMenuAddCallback(menu, _("Normal"), setDockPositionNormalCallback, NULL);
1210 entry->flags.indicator = 1;
1211 entry->flags.indicator_type = MI_DIAMOND;
1213 entry = wMenuAddCallback(menu, _("Auto raise & lower"), setDockPositionAutoRaiseLowerCallback, NULL);
1214 entry->flags.indicator = 1;
1215 entry->flags.indicator_type = MI_DIAMOND;
1217 entry = wMenuAddCallback(menu, _("Keep on Top"), setDockPositionKeepOnTopCallback, NULL);
1218 entry->flags.indicator = 1;
1219 entry->flags.indicator_type = MI_DIAMOND;
1221 menu->flags.realized = 0;
1222 wMenuRealize(menu);
1224 return menu;
1228 static WMenu *dockMenuCreate(WScreen *scr, int type)
1230 WMenu *menu;
1231 WMenuEntry *entry;
1233 if (type == WM_CLIP && scr->clip_menu)
1234 return scr->clip_menu;
1236 if (type == WM_DRAWER && scr->drawer_menu)
1237 return scr->drawer_menu;
1239 menu = wMenuCreate(scr, NULL, False);
1240 if (type == WM_DOCK) {
1241 entry = wMenuAddCallback(menu, _("Dock position"), NULL, NULL);
1242 if (scr->dock_pos_menu == NULL)
1243 scr->dock_pos_menu = makeDockPositionMenu(scr);
1244 wMenuEntrySetCascade(menu, entry, scr->dock_pos_menu);
1246 if (!wPreferences.flags.nodrawer)
1247 wMenuAddCallback(menu, _("Add a drawer"), addADrawerCallback, NULL);
1249 } else {
1250 if (type == WM_CLIP)
1251 entry = wMenuAddCallback(menu, _("Clip Options"), NULL, NULL);
1252 else /* if (type == WM_DRAWER) */
1253 entry = wMenuAddCallback(menu, _("Drawer options"), NULL, NULL);
1255 if (scr->clip_options == NULL)
1256 scr->clip_options = makeClipOptionsMenu(scr);
1258 wMenuEntrySetCascade(menu, entry, scr->clip_options);
1260 /* The same menu is used for the dock and its appicons. If the menu
1261 * entry text is different between the two contexts, or if it can
1262 * change depending on some state, free the duplicated string (from
1263 * wMenuInsertCallback) and use gettext's string */
1264 if (type == WM_CLIP) {
1265 entry = wMenuAddCallback(menu, _("Rename Workspace"), renameCallback, NULL);
1266 wfree(entry->text);
1267 entry->text = _("Rename Workspace"); /* can be: (Toggle) Omnipresent */
1270 entry = wMenuAddCallback(menu, _("Selected"), selectCallback, NULL);
1271 entry->flags.indicator = 1;
1272 entry->flags.indicator_on = 1;
1273 entry->flags.indicator_type = MI_CHECK;
1275 entry = wMenuAddCallback(menu, _("Select All Icons"), selectIconsCallback, NULL);
1276 wfree(entry->text);
1277 entry->text = _("Select All Icons"); /* can be: Unselect all icons */
1279 entry = wMenuAddCallback(menu, _("Keep Icon"), keepIconsCallback, NULL);
1280 wfree(entry->text);
1281 entry->text = _("Keep Icon"); /* can be: Keep Icons */
1283 if (type == WM_CLIP) {
1284 entry = wMenuAddCallback(menu, _("Move Icon To"), NULL, NULL);
1285 wfree(entry->text);
1286 entry->text = _("Move Icon To"); /* can be: Move Icons to */
1287 scr->clip_submenu = makeWorkspaceMenu(scr);
1288 if (scr->clip_submenu)
1289 wMenuEntrySetCascade(menu, entry, scr->clip_submenu);
1292 entry = wMenuAddCallback(menu, _("Remove Icon"), removeIconsCallback, NULL);
1293 wfree(entry->text);
1294 entry->text = _("Remove Icon"); /* can be: Remove Icons */
1296 wMenuAddCallback(menu, _("Attract Icons"), attractIconsCallback, NULL);
1299 wMenuAddCallback(menu, _("Launch"), launchCallback, NULL);
1301 wMenuAddCallback(menu, _("Unhide Here"), unhideHereCallback, NULL);
1303 entry = wMenuAddCallback(menu, _("Hide"), hideCallback, NULL);
1304 wfree(entry->text);
1305 entry->text = _("Hide"); /* can be: Unhide */
1307 wMenuAddCallback(menu, _("Settings..."), settingsCallback, NULL);
1309 entry = wMenuAddCallback(menu, _("Kill"), killCallback, NULL);
1310 wfree(entry->text);
1311 entry->text = _("Kill"); /* can be: Remove drawer */
1313 if (type == WM_CLIP)
1314 scr->clip_menu = menu;
1316 if (type == WM_DRAWER)
1317 scr->drawer_menu = menu;
1319 return menu;
1322 WDock *wDockCreate(WScreen *scr, int type, const char *name)
1324 WDock *dock;
1325 WAppIcon *btn;
1327 make_keys();
1329 dock = wmalloc(sizeof(WDock));
1331 switch (type) {
1332 case WM_CLIP:
1333 dock->max_icons = DOCK_MAX_ICONS;
1334 break;
1335 case WM_DRAWER:
1336 dock->max_icons = scr->scr_width / wPreferences.icon_size;
1337 break;
1338 case WM_DOCK:
1339 default:
1340 dock->max_icons = scr->scr_height / wPreferences.icon_size;
1343 dock->icon_array = wmalloc(sizeof(WAppIcon *) * dock->max_icons);
1345 btn = mainIconCreate(scr, type, name);
1347 btn->dock = dock;
1349 dock->x_pos = btn->x_pos;
1350 dock->y_pos = btn->y_pos;
1351 dock->screen_ptr = scr;
1352 dock->type = type;
1353 dock->icon_count = 1;
1354 if (type == WM_DRAWER)
1355 dock->on_right_side = scr->dock->on_right_side;
1356 else
1357 dock->on_right_side = 1;
1358 dock->collapsed = 0;
1359 dock->auto_collapse = 0;
1360 dock->auto_collapse_magic = NULL;
1361 dock->auto_raise_lower = 0;
1362 dock->auto_lower_magic = NULL;
1363 dock->auto_raise_magic = NULL;
1364 dock->attract_icons = 0;
1365 dock->lowered = 1;
1366 dock->icon_array[0] = btn;
1367 wRaiseFrame(btn->icon->core);
1368 XMoveWindow(dpy, btn->icon->core->window, btn->x_pos, btn->y_pos);
1370 /* create dock menu */
1371 dock->menu = dockMenuCreate(scr, type);
1373 if (type == WM_DRAWER) {
1374 drawerAppendToChain(scr, dock);
1375 dock->auto_collapse = 1;
1378 return dock;
1381 void wDockDestroy(WDock *dock)
1383 int i;
1384 WAppIcon *aicon;
1386 for (i = (dock->type == WM_CLIP) ? 1 : 0; i < dock->max_icons; i++) {
1387 aicon = dock->icon_array[i];
1388 if (aicon) {
1389 int keepit = aicon->running && wApplicationOf(aicon->main_window);
1390 wDockDetach(dock, aicon);
1391 if (keepit) {
1392 /* XXX: can: aicon->icon == NULL ? */
1393 PlaceIcon(dock->screen_ptr, &aicon->x_pos, &aicon->y_pos,
1394 wGetHeadForWindow(aicon->icon->owner));
1395 XMoveWindow(dpy, aicon->icon->core->window, aicon->x_pos, aicon->y_pos);
1396 if (!dock->mapped || dock->collapsed)
1397 XMapWindow(dpy, aicon->icon->core->window);
1401 if (wPreferences.auto_arrange_icons)
1402 wArrangeIcons(dock->screen_ptr, True);
1403 wfree(dock->icon_array);
1404 if (dock->menu && dock->type != WM_CLIP)
1405 wMenuDestroy(dock->menu, True);
1406 if (dock->screen_ptr->last_dock == dock)
1407 dock->screen_ptr->last_dock = NULL;
1408 wfree(dock);
1411 void wClipIconPaint(WAppIcon *aicon)
1413 WScreen *scr = aicon->icon->core->screen_ptr;
1414 WWorkspace *workspace = scr->workspaces[scr->current_workspace];
1415 WMColor *color;
1416 Window win = aicon->icon->core->window;
1417 int length, nlength;
1418 char *ws_name, ws_number[sizeof scr->current_workspace * CHAR_BIT / 3 + 1];
1419 int ty, tx;
1421 wIconPaint(aicon->icon);
1423 length = strlen(workspace->name);
1424 ws_name = wmalloc(length + 1);
1425 snprintf(ws_name, length + 1, "%s", workspace->name);
1426 snprintf(ws_number, sizeof ws_number, "%u", scr->current_workspace + 1);
1427 nlength = strlen(ws_number);
1429 if (wPreferences.flags.noclip || !workspace->clip->collapsed)
1430 color = scr->clip_title_color[CLIP_NORMAL];
1431 else
1432 color = scr->clip_title_color[CLIP_COLLAPSED];
1434 ty = ICON_SIZE - WMFontHeight(scr->clip_title_font) - 3;
1436 tx = CLIP_BUTTON_SIZE * ICON_SIZE / 64;
1438 if(wPreferences.show_clip_title)
1439 WMDrawString(scr->wmscreen, win, color, scr->clip_title_font, tx, ty, ws_name, length);
1441 tx = (ICON_SIZE / 2 - WMWidthOfString(scr->clip_title_font, ws_number, nlength)) / 2;
1443 WMDrawString(scr->wmscreen, win, color, scr->clip_title_font, tx, 2, ws_number, nlength);
1445 wfree(ws_name);
1447 if (aicon->launching)
1448 XFillRectangle(dpy, aicon->icon->core->window, scr->stipple_gc,
1449 0, 0, wPreferences.icon_size, wPreferences.icon_size);
1451 paintClipButtons(aicon, aicon->dock->lclip_button_pushed, aicon->dock->rclip_button_pushed);
1454 static void clipIconExpose(WObjDescriptor *desc, XEvent *event)
1456 /* Parameter not used, but tell the compiler that it is ok */
1457 (void) desc;
1458 (void) event;
1460 wClipIconPaint(desc->parent);
1463 static void dockIconPaint(WAppIcon *btn)
1465 if (btn == btn->icon->core->screen_ptr->clip_icon) {
1466 wClipIconPaint(btn);
1467 } else if (wIsADrawer(btn)) {
1468 wDrawerIconPaint(btn);
1469 } else {
1470 wAppIconPaint(btn);
1471 save_appicon(btn);
1475 static WMPropList *make_icon_state(WAppIcon *btn)
1477 WMPropList *node = NULL;
1478 WMPropList *command, *autolaunch, *lock, *name, *forced;
1479 WMPropList *position, *buggy, *omnipresent;
1480 char *tmp;
1481 char buffer[64];
1483 if (btn) {
1484 if (!btn->command)
1485 command = WMCreatePLString("-");
1486 else
1487 command = WMCreatePLString(btn->command);
1489 autolaunch = btn->auto_launch ? dYes : dNo;
1491 lock = btn->lock ? dYes : dNo;
1493 tmp = EscapeWM_CLASS(btn->wm_instance, btn->wm_class);
1495 name = WMCreatePLString(tmp);
1497 wfree(tmp);
1499 forced = btn->forced_dock ? dYes : dNo;
1501 buggy = btn->buggy_app ? dYes : dNo;
1503 if (!wPreferences.flags.clip_merged_in_dock && btn == btn->icon->core->screen_ptr->clip_icon)
1504 snprintf(buffer, sizeof(buffer), "%i,%i", btn->x_pos, btn->y_pos);
1505 else
1506 snprintf(buffer, sizeof(buffer), "%hi,%hi", btn->xindex, btn->yindex);
1507 position = WMCreatePLString(buffer);
1509 node = WMCreatePLDictionary(dCommand, command,
1510 dName, name,
1511 dAutoLaunch, autolaunch,
1512 dLock, lock,
1513 dForced, forced, dBuggyApplication, buggy, dPosition, position, NULL);
1514 WMReleasePropList(command);
1515 WMReleasePropList(name);
1516 WMReleasePropList(position);
1518 omnipresent = btn->omnipresent ? dYes : dNo;
1519 if (btn->dock != btn->icon->core->screen_ptr->dock && (btn->xindex != 0 || btn->yindex != 0))
1520 WMPutInPLDictionary(node, dOmnipresent, omnipresent);
1522 #ifdef USE_DOCK_XDND
1523 if (btn->dnd_command) {
1524 command = WMCreatePLString(btn->dnd_command);
1525 WMPutInPLDictionary(node, dDropCommand, command);
1526 WMReleasePropList(command);
1528 #endif /* USE_DOCK_XDND */
1530 if (btn->paste_command) {
1531 command = WMCreatePLString(btn->paste_command);
1532 WMPutInPLDictionary(node, dPasteCommand, command);
1533 WMReleasePropList(command);
1537 return node;
1540 static WMPropList *dockSaveState(WDock *dock)
1542 int i;
1543 WMPropList *icon_info;
1544 WMPropList *list = NULL, *dock_state = NULL;
1545 WMPropList *value, *key;
1546 char buffer[256];
1548 list = WMCreatePLArray(NULL);
1550 for (i = (dock->type == WM_DOCK ? 0 : 1); i < dock->max_icons; i++) {
1551 WAppIcon *btn = dock->icon_array[i];
1553 if (!btn || btn->attracted)
1554 continue;
1556 icon_info = make_icon_state(dock->icon_array[i]);
1557 if (icon_info != NULL) {
1558 WMAddToPLArray(list, icon_info);
1559 WMReleasePropList(icon_info);
1563 dock_state = WMCreatePLDictionary(dApplications, list, NULL);
1565 if (dock->type == WM_DOCK) {
1566 snprintf(buffer, sizeof(buffer), "Applications%i", dock->screen_ptr->scr_height);
1567 key = WMCreatePLString(buffer);
1568 WMPutInPLDictionary(dock_state, key, list);
1569 WMReleasePropList(key);
1571 snprintf(buffer, sizeof(buffer), "%i,%i", dock->x_pos, dock->y_pos);
1572 value = WMCreatePLString(buffer);
1573 WMPutInPLDictionary(dock_state, dPosition, value);
1574 WMReleasePropList(value);
1576 WMReleasePropList(list);
1578 if (dock->type == WM_CLIP || dock->type == WM_DRAWER) {
1579 value = (dock->collapsed ? dYes : dNo);
1580 WMPutInPLDictionary(dock_state, dCollapsed, value);
1582 value = (dock->auto_collapse ? dYes : dNo);
1583 WMPutInPLDictionary(dock_state, dAutoCollapse, value);
1585 value = (dock->attract_icons ? dYes : dNo);
1586 WMPutInPLDictionary(dock_state, dAutoAttractIcons, value);
1589 if (dock->type == WM_DOCK || dock->type == WM_CLIP) {
1590 value = (dock->lowered ? dYes : dNo);
1591 WMPutInPLDictionary(dock_state, dLowered, value);
1593 value = (dock->auto_raise_lower ? dYes : dNo);
1594 WMPutInPLDictionary(dock_state, dAutoRaiseLower, value);
1597 return dock_state;
1600 void wDockSaveState(WScreen *scr, WMPropList *old_state)
1602 WMPropList *dock_state;
1603 WMPropList *keys;
1605 dock_state = dockSaveState(scr->dock);
1608 * Copy saved states of docks with different sizes.
1610 if (old_state) {
1611 int i;
1612 WMPropList *tmp;
1614 keys = WMGetPLDictionaryKeys(old_state);
1615 for (i = 0; i < WMGetPropListItemCount(keys); i++) {
1616 tmp = WMGetFromPLArray(keys, i);
1618 if (strncasecmp(WMGetFromPLString(tmp), "applications", 12) == 0
1619 && !WMGetFromPLDictionary(dock_state, tmp)) {
1621 WMPutInPLDictionary(dock_state, tmp, WMGetFromPLDictionary(old_state, tmp));
1624 WMReleasePropList(keys);
1627 WMPutInPLDictionary(scr->session_state, dDock, dock_state);
1629 WMReleasePropList(dock_state);
1632 void wClipSaveState(WScreen *scr)
1634 WMPropList *clip_state;
1636 clip_state = make_icon_state(scr->clip_icon);
1638 WMPutInPLDictionary(scr->session_state, dClip, clip_state);
1640 WMReleasePropList(clip_state);
1643 WMPropList *wClipSaveWorkspaceState(WScreen *scr, int workspace)
1645 return dockSaveState(scr->workspaces[workspace]->clip);
1648 static Bool getBooleanDockValue(WMPropList *value, WMPropList *key)
1650 if (value) {
1651 if (WMIsPLString(value)) {
1652 if (strcasecmp(WMGetFromPLString(value), "YES") == 0)
1653 return True;
1654 } else {
1655 wwarning(_("bad value in docked icon state info %s"), WMGetFromPLString(key));
1658 return False;
1661 static WAppIcon *restore_icon_state(WScreen *scr, WMPropList *info, int type, int index)
1663 WAppIcon *aicon;
1664 WMPropList *cmd, *value;
1665 char *wclass, *winstance, *command;
1667 cmd = WMGetFromPLDictionary(info, dCommand);
1668 if (!cmd || !WMIsPLString(cmd))
1669 return NULL;
1671 /* parse window name */
1672 value = WMGetFromPLDictionary(info, dName);
1673 if (!value)
1674 return NULL;
1676 ParseWindowName(value, &winstance, &wclass, "dock");
1678 if (!winstance && !wclass)
1679 return NULL;
1681 /* get commands */
1682 command = wstrdup(WMGetFromPLString(cmd));
1684 if (strcmp(command, "-") == 0) {
1685 wfree(command);
1687 if (wclass)
1688 wfree(wclass);
1689 if (winstance)
1690 wfree(winstance);
1692 return NULL;
1695 aicon = wAppIconCreateForDock(scr, command, winstance, wclass, TILE_NORMAL);
1696 if (wclass)
1697 wfree(wclass);
1698 if (winstance)
1699 wfree(winstance);
1701 wfree(command);
1703 aicon->icon->core->descriptor.handle_mousedown = iconMouseDown;
1704 aicon->icon->core->descriptor.handle_enternotify = clipEnterNotify;
1705 aicon->icon->core->descriptor.handle_leavenotify = clipLeaveNotify;
1706 aicon->icon->core->descriptor.parent_type = WCLASS_DOCK_ICON;
1707 aicon->icon->core->descriptor.parent = aicon;
1709 #ifdef USE_DOCK_XDND
1710 cmd = WMGetFromPLDictionary(info, dDropCommand);
1711 if (cmd)
1712 aicon->dnd_command = wstrdup(WMGetFromPLString(cmd));
1713 #endif
1715 cmd = WMGetFromPLDictionary(info, dPasteCommand);
1716 if (cmd)
1717 aicon->paste_command = wstrdup(WMGetFromPLString(cmd));
1719 /* check auto launch */
1720 value = WMGetFromPLDictionary(info, dAutoLaunch);
1722 aicon->auto_launch = getBooleanDockValue(value, dAutoLaunch);
1724 /* check lock */
1725 value = WMGetFromPLDictionary(info, dLock);
1727 aicon->lock = getBooleanDockValue(value, dLock);
1729 /* check if it wasn't normally docked */
1730 value = WMGetFromPLDictionary(info, dForced);
1732 aicon->forced_dock = getBooleanDockValue(value, dForced);
1734 /* check if we can rely on the stuff in the app */
1735 value = WMGetFromPLDictionary(info, dBuggyApplication);
1737 aicon->buggy_app = getBooleanDockValue(value, dBuggyApplication);
1739 /* get position in the dock */
1740 value = WMGetFromPLDictionary(info, dPosition);
1741 if (value && WMIsPLString(value)) {
1742 if (sscanf(WMGetFromPLString(value), "%hi,%hi", &aicon->xindex, &aicon->yindex) != 2)
1743 wwarning(_("bad value in docked icon state info %s"), WMGetFromPLString(dPosition));
1745 /* check position sanity */
1746 /* *Very* incomplete section! */
1747 if (type == WM_DOCK) {
1748 aicon->xindex = 0;
1750 } else {
1751 aicon->yindex = index;
1752 aicon->xindex = 0;
1755 /* check if icon is omnipresent */
1756 value = WMGetFromPLDictionary(info, dOmnipresent);
1758 aicon->omnipresent = getBooleanDockValue(value, dOmnipresent);
1760 aicon->running = 0;
1761 aicon->docked = 1;
1763 return aicon;
1766 #define COMPLAIN(key) wwarning(_("bad value in dock state info:%s"), key)
1768 WAppIcon *wClipRestoreState(WScreen *scr, WMPropList *clip_state)
1770 WAppIcon *icon;
1771 WMPropList *value;
1773 icon = mainIconCreate(scr, WM_CLIP, NULL);
1775 if (!clip_state)
1776 return icon;
1778 WMRetainPropList(clip_state);
1780 /* restore position */
1782 value = WMGetFromPLDictionary(clip_state, dPosition);
1784 if (value) {
1785 if (!WMIsPLString(value)) {
1786 COMPLAIN("Position");
1787 } else {
1788 if (sscanf(WMGetFromPLString(value), "%i,%i", &icon->x_pos, &icon->y_pos) != 2)
1789 COMPLAIN("Position");
1791 /* check position sanity */
1792 if (!onScreen(scr, icon->x_pos, icon->y_pos))
1793 wScreenKeepInside(scr, &icon->x_pos, &icon->y_pos, ICON_SIZE, ICON_SIZE);
1796 #ifdef USE_DOCK_XDND
1797 value = WMGetFromPLDictionary(clip_state, dDropCommand);
1798 if (value && WMIsPLString(value))
1799 icon->dnd_command = wstrdup(WMGetFromPLString(value));
1800 #endif
1802 value = WMGetFromPLDictionary(clip_state, dPasteCommand);
1803 if (value && WMIsPLString(value))
1804 icon->paste_command = wstrdup(WMGetFromPLString(value));
1806 WMReleasePropList(clip_state);
1808 return icon;
1811 WDock *wDockRestoreState(WScreen *scr, WMPropList *dock_state, int type)
1813 WDock *dock;
1814 WMPropList *apps;
1815 WMPropList *value;
1816 WAppIcon *aicon, *old_top;
1817 int count, i;
1819 dock = wDockCreate(scr, type, NULL);
1821 if (!dock_state)
1822 return dock;
1824 WMRetainPropList(dock_state);
1826 /* restore position */
1827 value = WMGetFromPLDictionary(dock_state, dPosition);
1828 if (value) {
1829 if (!WMIsPLString(value)) {
1830 COMPLAIN("Position");
1831 } else {
1832 if (sscanf(WMGetFromPLString(value), "%i,%i", &dock->x_pos, &dock->y_pos) != 2)
1833 COMPLAIN("Position");
1835 /* check position sanity */
1836 if (!onScreen(scr, dock->x_pos, dock->y_pos)) {
1837 int x = dock->x_pos;
1838 wScreenKeepInside(scr, &x, &dock->y_pos, ICON_SIZE, ICON_SIZE);
1841 /* Is this needed any more? */
1842 if (type == WM_CLIP) {
1843 if (dock->x_pos < 0) {
1844 dock->x_pos = 0;
1845 } else if (dock->x_pos > scr->scr_width - ICON_SIZE) {
1846 dock->x_pos = scr->scr_width - ICON_SIZE;
1848 } else {
1849 int left, right, midpoint;
1851 left = getDockXPosition(scr, False);
1852 right = getDockXPosition(scr, True);
1853 midpoint = (left + right) / 2;
1855 if (dock->x_pos < midpoint) {
1856 dock->x_pos = left;
1857 dock->on_right_side = 0;
1858 } else {
1859 dock->x_pos = right;
1860 dock->on_right_side = 1;
1866 /* restore lowered/raised state */
1867 dock->lowered = 0;
1869 value = WMGetFromPLDictionary(dock_state, dLowered);
1870 if (value) {
1871 if (!WMIsPLString(value)) {
1872 COMPLAIN("Lowered");
1873 } else {
1874 if (strcasecmp(WMGetFromPLString(value), "YES") == 0)
1875 dock->lowered = 1;
1879 /* restore collapsed state */
1880 dock->collapsed = 0;
1882 value = WMGetFromPLDictionary(dock_state, dCollapsed);
1883 if (value) {
1884 if (!WMIsPLString(value)) {
1885 COMPLAIN("Collapsed");
1886 } else {
1887 if (strcasecmp(WMGetFromPLString(value), "YES") == 0)
1888 dock->collapsed = 1;
1892 /* restore auto-collapsed state */
1893 value = WMGetFromPLDictionary(dock_state, dAutoCollapse);
1894 if (value) {
1895 if (!WMIsPLString(value)) {
1896 COMPLAIN("AutoCollapse");
1897 } else {
1898 if (strcasecmp(WMGetFromPLString(value), "YES") == 0) {
1899 dock->auto_collapse = 1;
1900 dock->collapsed = 1;
1905 /* restore auto-raise/lower state */
1906 value = WMGetFromPLDictionary(dock_state, dAutoRaiseLower);
1907 if (value) {
1908 if (!WMIsPLString(value)) {
1909 COMPLAIN("AutoRaiseLower");
1910 } else {
1911 if (strcasecmp(WMGetFromPLString(value), "YES") == 0)
1912 dock->auto_raise_lower = 1;
1916 /* restore attract icons state */
1917 dock->attract_icons = 0;
1919 value = WMGetFromPLDictionary(dock_state, dAutoAttractIcons);
1920 if (value) {
1921 if (!WMIsPLString(value)) {
1922 COMPLAIN("AutoAttractIcons");
1923 } else {
1924 if (strcasecmp(WMGetFromPLString(value), "YES") == 0)
1925 dock->attract_icons = 1;
1929 /* application list */
1932 WMPropList *tmp;
1933 char buffer[64];
1936 * When saving, it saves the dock state in
1937 * Applications and Applicationsnnn
1939 * When loading, it will first try Applicationsnnn.
1940 * If it does not exist, use Applications as default.
1943 snprintf(buffer, sizeof(buffer), "Applications%i", scr->scr_height);
1945 tmp = WMCreatePLString(buffer);
1946 apps = WMGetFromPLDictionary(dock_state, tmp);
1947 WMReleasePropList(tmp);
1949 if (!apps)
1950 apps = WMGetFromPLDictionary(dock_state, dApplications);
1953 if (!apps)
1954 goto finish;
1956 count = WMGetPropListItemCount(apps);
1957 if (count == 0)
1958 goto finish;
1960 old_top = dock->icon_array[0];
1962 /* dock->icon_count is set to 1 when dock is created.
1963 * Since Clip is already restored, we want to keep it so for clip,
1964 * but for dock we may change the default top tile, so we set it to 0.
1966 if (type == WM_DOCK)
1967 dock->icon_count = 0;
1969 for (i = 0; i < count; i++) {
1970 if (dock->icon_count >= dock->max_icons) {
1971 wwarning(_("there are too many icons stored in dock. Ignoring what doesn't fit"));
1972 break;
1975 value = WMGetFromPLArray(apps, i);
1976 aicon = restore_icon_state(scr, value, type, dock->icon_count);
1978 dock->icon_array[dock->icon_count] = aicon;
1980 if (aicon) {
1981 aicon->dock = dock;
1982 aicon->x_pos = dock->x_pos + (aicon->xindex * ICON_SIZE);
1983 aicon->y_pos = dock->y_pos + (aicon->yindex * ICON_SIZE);
1985 if (dock->lowered)
1986 ChangeStackingLevel(aicon->icon->core, WMNormalLevel);
1987 else
1988 ChangeStackingLevel(aicon->icon->core, WMDockLevel);
1990 wCoreConfigure(aicon->icon->core, aicon->x_pos, aicon->y_pos, 0, 0);
1991 if (!dock->collapsed)
1992 XMapWindow(dpy, aicon->icon->core->window);
1994 wRaiseFrame(aicon->icon->core);
1996 dock->icon_count++;
1997 } else if (dock->icon_count == 0 && type == WM_DOCK) {
1998 dock->icon_count++;
2002 /* if the first icon is not defined, use the default */
2003 if (dock->icon_array[0] == NULL) {
2004 /* update default icon */
2005 old_top->x_pos = dock->x_pos;
2006 old_top->y_pos = dock->y_pos;
2007 if (dock->lowered)
2008 ChangeStackingLevel(old_top->icon->core, WMNormalLevel);
2009 else
2010 ChangeStackingLevel(old_top->icon->core, WMDockLevel);
2012 dock->icon_array[0] = old_top;
2013 XMoveWindow(dpy, old_top->icon->core->window, dock->x_pos, dock->y_pos);
2014 /* we don't need to increment dock->icon_count here because it was
2015 * incremented in the loop above.
2017 } else if (old_top != dock->icon_array[0]) {
2018 if (old_top == scr->clip_icon) // TODO dande: understand the logic
2019 scr->clip_icon = dock->icon_array[0];
2021 wAppIconDestroy(old_top);
2024 finish:
2025 WMReleasePropList(dock_state);
2027 return dock;
2030 void wDockLaunchWithState(WAppIcon *btn, WSavedState *state)
2032 if (btn && btn->command && !btn->running && !btn->launching) {
2033 btn->drop_launch = 0;
2034 btn->paste_launch = 0;
2036 btn->pid = execCommand(btn, btn->command, state);
2038 if (btn->pid > 0) {
2039 if (!btn->forced_dock && !btn->buggy_app) {
2040 btn->launching = 1;
2041 dockIconPaint(btn);
2044 } else {
2045 wfree(state);
2049 void wDockDoAutoLaunch(WDock *dock, int workspace)
2051 WAppIcon *btn;
2052 WSavedState *state;
2053 int i;
2055 for (i = 0; i < dock->max_icons; i++) {
2056 btn = dock->icon_array[i];
2057 if (!btn || !btn->auto_launch)
2058 continue;
2060 state = wmalloc(sizeof(WSavedState));
2061 state->workspace = workspace;
2062 /* TODO: this is klugy and is very difficult to understand
2063 * what's going on. Try to clean up */
2064 wDockLaunchWithState(btn, state);
2068 #ifdef USE_DOCK_XDND
2069 static WDock *findDock(WScreen *scr, XEvent *event, int *icon_pos)
2071 WDock *dock;
2072 int i;
2074 dock = scr->dock;
2075 if (dock != NULL) {
2076 for (i = 0; i < dock->max_icons; i++) {
2077 if (dock->icon_array[i] &&
2078 dock->icon_array[i]->icon->core->window == event->xclient.window) {
2079 *icon_pos = i;
2080 return dock;
2085 dock = scr->workspaces[scr->current_workspace]->clip;
2086 if (dock != NULL) {
2087 for (i = 0; i < dock->max_icons; i++) {
2088 if (dock->icon_array[i] &&
2089 dock->icon_array[i]->icon->core->window == event->xclient.window) {
2090 *icon_pos = i;
2091 return dock;
2096 *icon_pos = -1;
2097 return NULL;
2100 int wDockReceiveDNDDrop(WScreen *scr, XEvent *event)
2102 WDock *dock;
2103 WAppIcon *btn;
2104 int icon_pos;
2106 dock = findDock(scr, event, &icon_pos);
2107 if (!dock)
2108 return False;
2111 * Return True if the drop was on an application icon window.
2112 * In this case, let the ClientMessage handler redirect the
2113 * message to the app.
2115 if (dock->icon_array[icon_pos]->icon->icon_win != None)
2116 return True;
2118 if (dock->icon_array[icon_pos]->dnd_command != NULL) {
2119 scr->flags.dnd_data_convertion_status = 0;
2121 btn = dock->icon_array[icon_pos];
2123 if (!btn->forced_dock) {
2124 btn->relaunching = btn->running;
2125 btn->running = 1;
2127 if (btn->wm_instance || btn->wm_class) {
2128 WWindowAttributes attr;
2129 memset(&attr, 0, sizeof(WWindowAttributes));
2130 wDefaultFillAttributes(btn->wm_instance, btn->wm_class, &attr, NULL, True);
2132 if (!attr.no_appicon)
2133 btn->launching = 1;
2134 else
2135 btn->running = 0;
2138 btn->paste_launch = 0;
2139 btn->drop_launch = 1;
2140 scr->last_dock = dock;
2141 btn->pid = execCommand(btn, btn->dnd_command, NULL);
2142 if (btn->pid > 0) {
2143 dockIconPaint(btn);
2144 } else {
2145 btn->launching = 0;
2146 if (!btn->relaunching)
2147 btn->running = 0;
2150 return False;
2152 #endif /* USE_DOCK_XDND */
2154 Bool wDockAttachIcon(WDock *dock, WAppIcon *icon, int x, int y, Bool update_icon)
2156 WWindow *wwin;
2157 Bool lupdate_icon = False;
2158 char *command = NULL;
2159 int index;
2161 icon->editing = 0;
2163 if (update_icon)
2164 lupdate_icon = True;
2166 if (icon->command == NULL) {
2167 /* If icon->owner exists, it means the application is running */
2168 if (icon->icon->owner) {
2169 wwin = icon->icon->owner;
2170 command = GetCommandForWindow(wwin->client_win);
2173 if (command) {
2174 icon->command = command;
2175 } else {
2176 /* icon->forced_dock = 1; */
2177 if (dock->type != WM_CLIP || !icon->attracted) {
2178 icon->editing = 1;
2179 if (wInputDialog(dock->screen_ptr, _("Dock Icon"),
2180 _("Type the command used to launch the application"), &command)) {
2181 if (command && (command[0] == 0 || (command[0] == '-' && command[1] == 0))) {
2182 wfree(command);
2183 command = NULL;
2185 icon->command = command;
2186 icon->editing = 0;
2187 } else {
2188 icon->editing = 0;
2189 if (command)
2190 wfree(command);
2191 /* If the target is the dock, reject the icon. If
2192 * the target is the clip, make it an attracted icon
2194 if (dock->type == WM_CLIP) {
2195 icon->attracted = 1;
2196 if (!icon->icon->shadowed) {
2197 icon->icon->shadowed = 1;
2198 lupdate_icon = True;
2200 } else {
2201 return False;
2208 for (index = 1; index < dock->max_icons; index++)
2209 if (dock->icon_array[index] == NULL)
2210 break;
2211 /* if (index == dock->max_icons)
2212 return; */
2214 assert(index < dock->max_icons);
2216 dock->icon_array[index] = icon;
2217 icon->yindex = y;
2218 icon->xindex = x;
2220 icon->omnipresent = 0;
2222 icon->x_pos = dock->x_pos + x * ICON_SIZE;
2223 icon->y_pos = dock->y_pos + y * ICON_SIZE;
2225 dock->icon_count++;
2227 icon->running = 1;
2228 icon->launching = 0;
2229 icon->docked = 1;
2230 icon->dock = dock;
2231 icon->icon->core->descriptor.handle_mousedown = iconMouseDown;
2232 icon->icon->core->descriptor.handle_enternotify = clipEnterNotify;
2233 icon->icon->core->descriptor.handle_leavenotify = clipLeaveNotify;
2234 icon->icon->core->descriptor.parent_type = WCLASS_DOCK_ICON;
2235 icon->icon->core->descriptor.parent = icon;
2237 MoveInStackListUnder(dock->icon_array[index - 1]->icon->core, icon->icon->core);
2238 wAppIconMove(icon, icon->x_pos, icon->y_pos);
2241 * Update icon pixmap, RImage doesn't change,
2242 * so call wIconUpdate is not needed
2244 if (lupdate_icon)
2245 update_icon_pixmap(icon->icon);
2247 /* Paint it */
2248 wAppIconPaint(icon);
2250 /* Save it */
2251 save_appicon(icon);
2253 if (wPreferences.auto_arrange_icons)
2254 wArrangeIcons(dock->screen_ptr, True);
2256 #ifdef USE_DOCK_XDND
2257 if (icon->command && !icon->dnd_command) {
2258 int len = strlen(icon->command) + 8;
2259 icon->dnd_command = wmalloc(len);
2260 snprintf(icon->dnd_command, len, "%s %%d", icon->command);
2262 #endif
2264 if (icon->command && !icon->paste_command) {
2265 int len = strlen(icon->command) + 8;
2266 icon->paste_command = wmalloc(len);
2267 snprintf(icon->paste_command, len, "%s %%s", icon->command);
2270 return True;
2273 void wDockReattachIcon(WDock *dock, WAppIcon *icon, int x, int y)
2275 int index;
2277 for (index = 1; index < dock->max_icons; index++) {
2278 if (dock->icon_array[index] == icon)
2279 break;
2281 assert(index < dock->max_icons);
2283 icon->yindex = y;
2284 icon->xindex = x;
2286 icon->x_pos = dock->x_pos + x * ICON_SIZE;
2287 icon->y_pos = dock->y_pos + y * ICON_SIZE;
2290 Bool wDockMoveIconBetweenDocks(WDock *src, WDock *dest, WAppIcon *icon, int x, int y)
2292 WWindow *wwin;
2293 char *command = NULL;
2294 int index;
2295 Bool update_icon = False;
2297 if (src == dest)
2298 return True; /* No move needed, we're already there */
2300 if (dest == NULL)
2301 return False;
2304 * For the moment we can't do this if we move icons in Clip from one
2305 * workspace to other, because if we move two or more icons without
2306 * command, the dialog box will not be able to tell us to which of the
2307 * moved icons it applies. -Dan
2309 if ((dest->type == WM_DOCK /*|| dest->keep_attracted */ ) && icon->command == NULL) {
2310 /* If icon->owner exists, it means the application is running */
2311 if (icon->icon->owner) {
2312 wwin = icon->icon->owner;
2313 command = GetCommandForWindow(wwin->client_win);
2316 if (command) {
2317 icon->command = command;
2318 } else {
2319 icon->editing = 1;
2320 /* icon->forced_dock = 1; */
2321 if (wInputDialog(src->screen_ptr, _("Dock Icon"),
2322 _("Type the command used to launch the application"), &command)) {
2323 if (command && (command[0] == 0 || (command[0] == '-' && command[1] == 0))) {
2324 wfree(command);
2325 command = NULL;
2327 icon->command = command;
2328 } else {
2329 icon->editing = 0;
2330 if (command)
2331 wfree(command);
2332 return False;
2334 icon->editing = 0;
2338 if (dest->type == WM_DOCK || dest->type == WM_DRAWER)
2339 wClipMakeIconOmnipresent(icon, False);
2341 for (index = 1; index < src->max_icons; index++) {
2342 if (src->icon_array[index] == icon)
2343 break;
2345 assert(index < src->max_icons);
2347 src->icon_array[index] = NULL;
2348 src->icon_count--;
2350 for (index = 1; index < dest->max_icons; index++) {
2351 if (dest->icon_array[index] == NULL)
2352 break;
2355 assert(index < dest->max_icons);
2357 dest->icon_array[index] = icon;
2358 icon->dock = dest;
2360 /* deselect the icon */
2361 if (icon->icon->selected)
2362 wIconSelect(icon->icon);
2364 icon->icon->core->descriptor.handle_enternotify = clipEnterNotify;
2365 icon->icon->core->descriptor.handle_leavenotify = clipLeaveNotify;
2367 /* set it to be kept when moving to dock.
2368 * Unless the icon does not have a command set
2370 if (icon->command && (dest->type == WM_DOCK || dest->type == WM_DRAWER)) {
2371 icon->attracted = 0;
2372 if (icon->icon->shadowed) {
2373 icon->icon->shadowed = 0;
2374 update_icon = True;
2378 if (src->auto_collapse || src->auto_raise_lower)
2379 clipLeave(src);
2381 icon->yindex = y;
2382 icon->xindex = x;
2384 icon->x_pos = dest->x_pos + x * ICON_SIZE;
2385 icon->y_pos = dest->y_pos + y * ICON_SIZE;
2387 dest->icon_count++;
2389 MoveInStackListUnder(dest->icon_array[index - 1]->icon->core, icon->icon->core);
2392 * Update icon pixmap, RImage doesn't change,
2393 * so call wIconUpdate is not needed
2395 if (update_icon)
2396 update_icon_pixmap(icon->icon);
2398 /* Paint it */
2399 wAppIconPaint(icon);
2401 return True;
2404 void wDockDetach(WDock *dock, WAppIcon *icon)
2406 int index;
2407 Bool update_icon = False;
2409 /* make the settings panel be closed */
2410 if (icon->panel)
2411 DestroyDockAppSettingsPanel(icon->panel);
2413 /* This must be called before icon->dock is set to NULL.
2414 * Don't move it. -Dan
2416 wClipMakeIconOmnipresent(icon, False);
2418 icon->docked = 0;
2419 icon->dock = NULL;
2420 icon->attracted = 0;
2421 icon->auto_launch = 0;
2422 if (icon->icon->shadowed) {
2423 icon->icon->shadowed = 0;
2424 update_icon = True;
2427 /* deselect the icon */
2428 if (icon->icon->selected)
2429 wIconSelect(icon->icon);
2431 if (icon->command) {
2432 wfree(icon->command);
2433 icon->command = NULL;
2435 #ifdef USE_DOCK_XDND
2436 if (icon->dnd_command) {
2437 wfree(icon->dnd_command);
2438 icon->dnd_command = NULL;
2440 #endif
2441 if (icon->paste_command) {
2442 wfree(icon->paste_command);
2443 icon->paste_command = NULL;
2446 for (index = 1; index < dock->max_icons; index++)
2447 if (dock->icon_array[index] == icon)
2448 break;
2450 assert(index < dock->max_icons);
2451 dock->icon_array[index] = NULL;
2452 icon->yindex = -1;
2453 icon->xindex = -1;
2455 dock->icon_count--;
2457 /* Remove the Cached Icon */
2458 remove_cache_icon(icon->icon->file);
2460 /* if the dock is not attached to an application or
2461 * the application did not set the appropriate hints yet,
2462 * destroy the icon */
2463 if (!icon->running || !wApplicationOf(icon->main_window)) {
2464 wAppIconDestroy(icon);
2465 } else {
2466 icon->icon->core->descriptor.handle_mousedown = appIconMouseDown;
2467 icon->icon->core->descriptor.handle_enternotify = NULL;
2468 icon->icon->core->descriptor.handle_leavenotify = NULL;
2469 icon->icon->core->descriptor.parent_type = WCLASS_APPICON;
2470 icon->icon->core->descriptor.parent = icon;
2472 ChangeStackingLevel(icon->icon->core, NORMAL_ICON_LEVEL);
2475 * Update icon pixmap, RImage doesn't change,
2476 * so call wIconUpdate is not needed
2478 if (update_icon)
2479 update_icon_pixmap(icon->icon);
2481 /* Paint it */
2482 wAppIconPaint(icon);
2484 if (wPreferences.auto_arrange_icons)
2485 wArrangeIcons(dock->screen_ptr, True);
2488 if (dock->auto_collapse || dock->auto_raise_lower)
2489 clipLeave(dock);
2493 * returns the closest Dock slot index for the passed
2494 * coordinates.
2496 * Returns False if icon can't be docked.
2498 * Note: this function should NEVER alter ret_x or ret_y, unless it will
2499 * return True. -Dan
2501 /* Redocking == true means either icon->dock == dock (normal case)
2502 * or we are called from handleDockMove for a drawer */
2503 Bool wDockSnapIcon(WDock *dock, WAppIcon *icon, int req_x, int req_y, int *ret_x, int *ret_y, int redocking)
2505 WScreen *scr = dock->screen_ptr;
2506 int dx, dy;
2507 int ex_x, ex_y;
2508 int i, offset = ICON_SIZE / 2;
2509 WAppIcon *aicon = NULL;
2510 WAppIcon *nicon = NULL;
2512 if (wPreferences.flags.noupdates)
2513 return False;
2515 dx = dock->x_pos;
2516 dy = dock->y_pos;
2518 /* if the dock is full */
2519 if (!redocking && (dock->icon_count >= dock->max_icons))
2520 return False;
2522 /* exact position */
2523 if (req_y < dy)
2524 ex_y = (req_y - offset - dy) / ICON_SIZE;
2525 else
2526 ex_y = (req_y + offset - dy) / ICON_SIZE;
2528 if (req_x < dx)
2529 ex_x = (req_x - offset - dx) / ICON_SIZE;
2530 else
2531 ex_x = (req_x + offset - dx) / ICON_SIZE;
2533 /* check if the icon is outside the screen boundaries */
2534 if (!onScreen(scr, dx + ex_x * ICON_SIZE, dy + ex_y * ICON_SIZE))
2535 return False;
2537 switch (dock->type) {
2538 case WM_DOCK:
2539 /* We can return False right away if
2540 * - we do not come from this dock (which is a WM_DOCK),
2541 * - we are not right over it, and
2542 * - we are not the main tile of a drawer.
2543 * In the latter case, we are called from handleDockMove. */
2544 if (icon->dock != dock && ex_x != 0 &&
2545 !(icon->dock && icon->dock->type == WM_DRAWER && icon == icon->dock->icon_array[0]))
2546 return False;
2548 if (!redocking && ex_x != 0)
2549 return False;
2551 if (getDrawer(scr, ex_y)) /* Return false so that the drawer gets it. */
2552 return False;
2554 aicon = NULL;
2555 for (i = 0; i < dock->max_icons; i++) {
2556 nicon = dock->icon_array[i];
2557 if (nicon && nicon->yindex == ex_y) {
2558 aicon = nicon;
2559 break;
2563 if (redocking) {
2564 int sig, done, closest;
2566 /* Possible cases when redocking:
2568 * icon dragged out of range of any slot -> false
2569 * icon dragged on a drawer -> false (to open the drawer)
2570 * icon dragged to range of free slot
2571 * icon dragged to range of same slot
2572 * icon dragged to range of different icon
2574 if (abs(ex_x) > DOCK_DETTACH_THRESHOLD)
2575 return False;
2577 if (aicon == icon || !aicon) {
2578 *ret_x = 0;
2579 *ret_y = ex_y;
2580 return True;
2583 /* start looking at the upper slot or lower? */
2584 if (ex_y * ICON_SIZE < (req_y + offset - dy))
2585 sig = 1;
2586 else
2587 sig = -1;
2589 done = 0;
2590 /* look for closest free slot */
2591 for (i = 0; i < (DOCK_DETTACH_THRESHOLD + 1) * 2 && !done; i++) {
2592 int j;
2594 done = 1;
2595 closest = sig * (i / 2) + ex_y;
2596 /* check if this slot is fully on the screen and not used */
2597 if (onScreen(scr, dx, dy + closest * ICON_SIZE)) {
2598 for (j = 0; j < dock->max_icons; j++) {
2599 if (dock->icon_array[j]
2600 && dock->icon_array[j]->yindex == closest) {
2601 /* slot is used by someone else */
2602 if (dock->icon_array[j] != icon)
2603 done = 0;
2604 break;
2607 /* slot is used by a drawer */
2608 done = done && !getDrawer(scr, closest);
2610 else // !onScreen
2611 done = 0;
2612 sig = -sig;
2614 if (done &&
2615 ((ex_y >= closest && ex_y - closest < DOCK_DETTACH_THRESHOLD + 1)
2616 || (ex_y < closest && closest - ex_y <= DOCK_DETTACH_THRESHOLD + 1))) {
2617 *ret_x = 0;
2618 *ret_y = closest;
2619 return True;
2621 } else { /* !redocking */
2623 /* if slot is free and the icon is close enough, return it */
2624 if (!aicon && ex_x == 0) {
2625 *ret_x = 0;
2626 *ret_y = ex_y;
2627 return True;
2630 break;
2631 case WM_CLIP:
2633 int neighbours = 0;
2634 int start, stop, k;
2636 start = icon->omnipresent ? 0 : scr->current_workspace;
2637 stop = icon->omnipresent ? scr->workspace_count : start + 1;
2639 aicon = NULL;
2640 for (k = start; k < stop; k++) {
2641 WDock *tmp = scr->workspaces[k]->clip;
2642 if (!tmp)
2643 continue;
2644 for (i = 0; i < tmp->max_icons; i++) {
2645 nicon = tmp->icon_array[i];
2646 if (nicon && nicon->xindex == ex_x && nicon->yindex == ex_y) {
2647 aicon = nicon;
2648 break;
2651 if (aicon)
2652 break;
2654 for (k = start; k < stop; k++) {
2655 WDock *tmp = scr->workspaces[k]->clip;
2656 if (!tmp)
2657 continue;
2658 for (i = 0; i < tmp->max_icons; i++) {
2659 nicon = tmp->icon_array[i];
2660 if (nicon && nicon != icon && /* Icon can't be its own neighbour */
2661 (abs(nicon->xindex - ex_x) <= CLIP_ATTACH_VICINITY &&
2662 abs(nicon->yindex - ex_y) <= CLIP_ATTACH_VICINITY)) {
2663 neighbours = 1;
2664 break;
2667 if (neighbours)
2668 break;
2671 if (neighbours && (aicon == NULL || (redocking && aicon == icon))) {
2672 *ret_x = ex_x;
2673 *ret_y = ex_y;
2674 return True;
2676 break;
2678 case WM_DRAWER:
2680 WAppIcon *aicons_to_shift[ dock->icon_count ];
2681 int index_of_hole, j;
2683 if (ex_y != 0 ||
2684 abs(ex_x) - dock->icon_count > DOCK_DETTACH_THRESHOLD ||
2685 (ex_x < 0 && !dock->on_right_side) ||
2686 (ex_x > 0 && dock->on_right_side)) {
2687 return False;
2690 if (ex_x == 0)
2691 ex_x = (dock->on_right_side ? -1 : 1);
2693 /* "Reduce" ex_x but keep its sign */
2694 if (redocking) {
2695 if (abs(ex_x) > dock->icon_count - 1) /* minus 1: do not take icon_array[0] into account */
2696 ex_x = ex_x * (dock->icon_count - 1) / abs(ex_x); /* don't use *= ! */
2697 } else {
2698 if (abs(ex_x) > dock->icon_count)
2699 ex_x = ex_x * dock->icon_count / abs(ex_x);
2701 index_of_hole = indexOfHole(dock, icon, redocking);
2703 /* Find the appicons between where icon was (index_of_hole) and where
2704 * it wants to be (ex_x) and slide them. */
2705 j = 0;
2706 for (i = 1; i < dock->max_icons; i++) {
2707 aicon = dock->icon_array[i];
2708 if ((aicon != NULL) && (aicon != icon) &&
2709 ((ex_x <= aicon->xindex && aicon->xindex < index_of_hole) ||
2710 (index_of_hole < aicon->xindex && aicon->xindex <= ex_x)))
2711 aicons_to_shift[ j++ ] = aicon;
2713 assert(j == abs(ex_x - index_of_hole));
2715 wSlideAppicons(aicons_to_shift, j, (index_of_hole < ex_x));
2717 *ret_x = ex_x;
2718 *ret_y = ex_y;
2719 return True;
2722 return False;
2725 static int onScreen(WScreen *scr, int x, int y)
2727 WMRect rect;
2728 int flags;
2730 rect.pos.x = x;
2731 rect.pos.y = y;
2732 rect.size.width = rect.size.height = ICON_SIZE;
2734 wGetRectPlacementInfo(scr, rect, &flags);
2736 return !(flags & (XFLAG_DEAD | XFLAG_PARTIAL));
2740 * returns true if it can find a free slot in the dock,
2741 * in which case it changes x_pos and y_pos accordingly.
2742 * Else returns false.
2744 Bool wDockFindFreeSlot(WDock *dock, int *x_pos, int *y_pos)
2746 WScreen *scr = dock->screen_ptr;
2747 WAppIcon *btn;
2748 WAppIconChain *chain;
2749 unsigned char *slot_map;
2750 int mwidth;
2751 int r;
2752 int x, y;
2753 int i, done = False;
2754 int corner;
2755 int ex = scr->scr_width, ey = scr->scr_height;
2756 int extra_count = 0;
2758 if (dock->type == WM_DRAWER) {
2759 if (dock->icon_count >= dock->max_icons) { /* drawer is full */
2760 return False;
2762 *x_pos = dock->icon_count * (dock->on_right_side ? -1 : 1);
2763 *y_pos = 0;
2764 return True;
2767 if (dock->type == WM_CLIP && dock != scr->workspaces[scr->current_workspace]->clip)
2768 extra_count = scr->global_icon_count;
2770 /* if the dock is full */
2771 if (dock->icon_count + extra_count >= dock->max_icons)
2772 return False;
2774 if (!wPreferences.flags.nodock && scr->dock && scr->dock->on_right_side) {
2775 ex -= ICON_SIZE + DOCK_EXTRA_SPACE;
2778 if (ex < dock->x_pos)
2779 ex = dock->x_pos;
2780 #define C_NONE 0
2781 #define C_NW 1
2782 #define C_NE 2
2783 #define C_SW 3
2784 #define C_SE 4
2786 /* check if clip is in a corner */
2787 if (dock->type == WM_CLIP) {
2788 if (dock->x_pos < 1 && dock->y_pos < 1)
2789 corner = C_NE;
2790 else if (dock->x_pos < 1 && dock->y_pos >= (ey - ICON_SIZE))
2791 corner = C_SE;
2792 else if (dock->x_pos >= (ex - ICON_SIZE) && dock->y_pos >= (ey - ICON_SIZE))
2793 corner = C_SW;
2794 else if (dock->x_pos >= (ex - ICON_SIZE) && dock->y_pos < 1)
2795 corner = C_NW;
2796 else
2797 corner = C_NONE;
2798 } else {
2799 corner = C_NONE;
2802 /* If the clip is in the corner, use only slots that are in the border
2803 * of the screen */
2804 if (corner != C_NONE) {
2805 char *hmap, *vmap;
2806 int hcount, vcount;
2808 hcount = WMIN(dock->max_icons, scr->scr_width / ICON_SIZE);
2809 vcount = WMIN(dock->max_icons, scr->scr_height / ICON_SIZE);
2810 hmap = wmalloc(hcount + 1);
2811 vmap = wmalloc(vcount + 1);
2813 /* mark used positions */
2814 switch (corner) {
2815 case C_NE:
2816 for (i = 0; i < dock->max_icons; i++) {
2817 btn = dock->icon_array[i];
2818 if (!btn)
2819 continue;
2821 if (btn->xindex == 0 && btn->yindex > 0 && btn->yindex < vcount)
2822 vmap[btn->yindex] = 1;
2823 else if (btn->yindex == 0 && btn->xindex > 0 && btn->xindex < hcount)
2824 hmap[btn->xindex] = 1;
2826 for (chain = scr->global_icons; chain != NULL; chain = chain->next) {
2827 btn = chain->aicon;
2828 if (btn->xindex == 0 && btn->yindex > 0 && btn->yindex < vcount)
2829 vmap[btn->yindex] = 1;
2830 else if (btn->yindex == 0 && btn->xindex > 0 && btn->xindex < hcount)
2831 hmap[btn->xindex] = 1;
2833 break;
2834 case C_NW:
2835 for (i = 0; i < dock->max_icons; i++) {
2836 btn = dock->icon_array[i];
2837 if (!btn)
2838 continue;
2840 if (btn->xindex == 0 && btn->yindex > 0 && btn->yindex < vcount)
2841 vmap[btn->yindex] = 1;
2842 else if (btn->yindex == 0 && btn->xindex < 0 && btn->xindex > -hcount)
2843 hmap[-btn->xindex] = 1;
2845 for (chain = scr->global_icons; chain != NULL; chain = chain->next) {
2846 btn = chain->aicon;
2847 if (btn->xindex == 0 && btn->yindex > 0 && btn->yindex < vcount)
2848 vmap[btn->yindex] = 1;
2849 else if (btn->yindex == 0 && btn->xindex < 0 && btn->xindex > -hcount)
2850 hmap[-btn->xindex] = 1;
2852 break;
2853 case C_SE:
2854 for (i = 0; i < dock->max_icons; i++) {
2855 btn = dock->icon_array[i];
2856 if (!btn)
2857 continue;
2859 if (btn->xindex == 0 && btn->yindex < 0 && btn->yindex > -vcount)
2860 vmap[-btn->yindex] = 1;
2861 else if (btn->yindex == 0 && btn->xindex > 0 && btn->xindex < hcount)
2862 hmap[btn->xindex] = 1;
2864 for (chain = scr->global_icons; chain != NULL; chain = chain->next) {
2865 btn = chain->aicon;
2866 if (btn->xindex == 0 && btn->yindex < 0 && btn->yindex > -vcount)
2867 vmap[-btn->yindex] = 1;
2868 else if (btn->yindex == 0 && btn->xindex > 0 && btn->xindex < hcount)
2869 hmap[btn->xindex] = 1;
2871 break;
2872 case C_SW:
2873 default:
2874 for (i = 0; i < dock->max_icons; i++) {
2875 btn = dock->icon_array[i];
2876 if (!btn)
2877 continue;
2879 if (btn->xindex == 0 && btn->yindex < 0 && btn->yindex > -vcount)
2880 vmap[-btn->yindex] = 1;
2881 else if (btn->yindex == 0 && btn->xindex < 0 && btn->xindex > -hcount)
2882 hmap[-btn->xindex] = 1;
2884 for (chain = scr->global_icons; chain != NULL; chain = chain->next) {
2885 btn = chain->aicon;
2886 if (btn->xindex == 0 && btn->yindex < 0 && btn->yindex > -vcount)
2887 vmap[-btn->yindex] = 1;
2888 else if (btn->yindex == 0 && btn->xindex < 0 && btn->xindex > -hcount)
2889 hmap[-btn->xindex] = 1;
2892 x = 0;
2893 y = 0;
2894 done = 0;
2895 /* search a vacant slot */
2896 for (i = 1; i < WMAX(vcount, hcount); i++) {
2897 if (i < vcount && vmap[i] == 0) {
2898 /* found a slot */
2899 x = 0;
2900 y = i;
2901 done = 1;
2902 break;
2903 } else if (i < hcount && hmap[i] == 0) {
2904 /* found a slot */
2905 x = i;
2906 y = 0;
2907 done = 1;
2908 break;
2911 wfree(vmap);
2912 wfree(hmap);
2913 /* If found a slot, translate and return */
2914 if (done) {
2915 if (corner == C_NW || corner == C_NE)
2916 *y_pos = y;
2917 else
2918 *y_pos = -y;
2920 if (corner == C_NE || corner == C_SE)
2921 *x_pos = x;
2922 else
2923 *x_pos = -x;
2925 return True;
2927 /* else, try to find a slot somewhere else */
2930 /* a map of mwidth x mwidth would be enough if we allowed icons to be
2931 * placed outside of screen */
2932 mwidth = (int)ceil(sqrt(dock->max_icons));
2934 /* In the worst case (the clip is in the corner of the screen),
2935 * the amount of icons that fit in the clip is smaller.
2936 * Double the map to get a safe value.
2938 mwidth += mwidth;
2940 r = (mwidth - 1) / 2;
2942 slot_map = wmalloc(mwidth * mwidth);
2944 #define XY2OFS(x,y) (WMAX(abs(x),abs(y)) > r) ? 0 : (((y)+r)*(mwidth)+(x)+r)
2946 /* mark used slots in the map. If the slot falls outside the map
2947 * (for example, when all icons are placed in line), ignore them. */
2948 for (i = 0; i < dock->max_icons; i++) {
2949 btn = dock->icon_array[i];
2950 if (btn)
2951 slot_map[XY2OFS(btn->xindex, btn->yindex)] = 1;
2954 for (chain = scr->global_icons; chain != NULL; chain = chain->next)
2955 slot_map[XY2OFS(chain->aicon->xindex, chain->aicon->yindex)] = 1;
2957 /* Find closest slot from the center that is free by scanning the
2958 * map from the center to outward in circular passes.
2959 * This will not result in a neat layout, but will be optimal
2960 * in the sense that there will not be holes left.
2962 done = 0;
2963 for (i = 1; i <= r && !done; i++) {
2964 int tx, ty;
2966 /* top and bottom parts of the ring */
2967 for (x = -i; x <= i && !done; x++) {
2968 tx = dock->x_pos + x * ICON_SIZE;
2969 y = -i;
2970 ty = dock->y_pos + y * ICON_SIZE;
2971 if (slot_map[XY2OFS(x, y)] == 0 && onScreen(scr, tx, ty)) {
2972 *x_pos = x;
2973 *y_pos = y;
2974 done = 1;
2975 break;
2977 y = i;
2978 ty = dock->y_pos + y * ICON_SIZE;
2979 if (slot_map[XY2OFS(x, y)] == 0 && onScreen(scr, tx, ty)) {
2980 *x_pos = x;
2981 *y_pos = y;
2982 done = 1;
2983 break;
2986 /* left and right parts of the ring */
2987 for (y = -i + 1; y <= i - 1; y++) {
2988 ty = dock->y_pos + y * ICON_SIZE;
2989 x = -i;
2990 tx = dock->x_pos + x * ICON_SIZE;
2991 if (slot_map[XY2OFS(x, y)] == 0 && onScreen(scr, tx, ty)) {
2992 *x_pos = x;
2993 *y_pos = y;
2994 done = 1;
2995 break;
2997 x = i;
2998 tx = dock->x_pos + x * ICON_SIZE;
2999 if (slot_map[XY2OFS(x, y)] == 0 && onScreen(scr, tx, ty)) {
3000 *x_pos = x;
3001 *y_pos = y;
3002 done = 1;
3003 break;
3007 wfree(slot_map);
3008 #undef XY2OFS
3009 return done;
3012 static void moveDock(WDock *dock, int new_x, int new_y)
3014 WAppIcon *btn;
3015 WDrawerChain *dc;
3016 int i;
3018 if (dock->type == WM_DOCK) {
3019 for (dc = dock->screen_ptr->drawers; dc != NULL; dc = dc->next)
3020 moveDock(dc->adrawer, new_x, dc->adrawer->y_pos - dock->y_pos + new_y);
3023 dock->x_pos = new_x;
3024 dock->y_pos = new_y;
3025 for (i = 0; i < dock->max_icons; i++) {
3026 btn = dock->icon_array[i];
3027 if (btn) {
3028 btn->x_pos = new_x + btn->xindex * ICON_SIZE;
3029 btn->y_pos = new_y + btn->yindex * ICON_SIZE;
3030 XMoveWindow(dpy, btn->icon->core->window, btn->x_pos, btn->y_pos);
3035 void wDockSwap(WDock *dock)
3037 WScreen *scr = dock->screen_ptr;
3038 WAppIcon *btn;
3039 int x, i;
3042 x = getDockXPosition(scr, dock->on_right_side);
3043 swapDrawers(scr, x);
3044 dock->x_pos = x;
3046 for (i = 0; i < dock->max_icons; i++) {
3047 btn = dock->icon_array[i];
3048 if (btn) {
3049 btn->x_pos = x;
3050 XMoveWindow(dpy, btn->icon->core->window, btn->x_pos, btn->y_pos);
3054 wScreenUpdateUsableArea(scr);
3057 static pid_t execCommand(WAppIcon *btn, const char *command, WSavedState *state)
3059 WScreen *scr = btn->icon->core->screen_ptr;
3060 pid_t pid;
3061 char **argv;
3062 int argc;
3063 char *cmdline;
3065 cmdline = ExpandOptions(scr, command);
3067 if (scr->flags.dnd_data_convertion_status || !cmdline) {
3068 if (cmdline)
3069 wfree(cmdline);
3070 if (state)
3071 wfree(state);
3072 return 0;
3075 wtokensplit(cmdline, &argv, &argc);
3077 if (!argc) {
3078 if (cmdline)
3079 wfree(cmdline);
3080 if (state)
3081 wfree(state);
3082 return 0;
3085 pid = fork();
3086 if (pid == 0) {
3087 char **args;
3088 int i;
3090 SetupEnvironment(scr);
3092 #ifdef HAVE_SETSID
3093 setsid();
3094 #endif
3096 args = malloc(sizeof(char *) * (argc + 1));
3097 if (!args)
3098 exit(111);
3100 for (i = 0; i < argc; i++)
3101 args[i] = argv[i];
3103 args[argc] = NULL;
3104 execvp(argv[0], args);
3105 exit(111);
3107 wtokenfree(argv, argc);
3109 if (pid > 0) {
3110 if (!state) {
3111 state = wmalloc(sizeof(WSavedState));
3112 state->hidden = -1;
3113 state->miniaturized = -1;
3114 state->shaded = -1;
3115 if (btn->dock == scr->dock || btn->dock->type == WM_DRAWER || btn->omnipresent)
3116 state->workspace = -1;
3117 else
3118 state->workspace = scr->current_workspace;
3120 wWindowAddSavedState(btn->wm_instance, btn->wm_class, cmdline, pid, state);
3121 wAddDeathHandler(pid, trackDeadProcess, btn->dock);
3122 } else if (state) {
3123 wfree(state);
3125 wfree(cmdline);
3126 return pid;
3129 void wDockHideIcons(WDock *dock)
3131 int i;
3133 if (dock == NULL)
3134 return;
3136 for (i = 1; i < dock->max_icons; i++) {
3137 if (dock->icon_array[i])
3138 XUnmapWindow(dpy, dock->icon_array[i]->icon->core->window);
3140 dock->mapped = 0;
3142 dockIconPaint(dock->icon_array[0]);
3145 void wDockShowIcons(WDock *dock)
3147 int i;
3148 WAppIcon *btn;
3150 if (dock == NULL)
3151 return;
3153 btn = dock->icon_array[0];
3154 moveDock(dock, btn->x_pos, btn->y_pos);
3156 /* Deleting any change in stacking level, this function is now only about
3157 mapping icons */
3159 if (!dock->collapsed) {
3160 for (i = 1; i < dock->max_icons; i++) {
3161 if (dock->icon_array[i])
3162 XMapWindow(dpy, dock->icon_array[i]->icon->core->window);
3165 dock->mapped = 1;
3167 dockIconPaint(btn);
3170 void wDockLower(WDock *dock)
3172 int i;
3173 WDrawerChain *dc;
3175 if (dock->type == WM_DOCK) {
3176 for (dc = dock->screen_ptr->drawers; dc != NULL; dc = dc->next)
3177 wDockLower(dc->adrawer);
3179 for (i = 0; i < dock->max_icons; i++) {
3180 if (dock->icon_array[i])
3181 wLowerFrame(dock->icon_array[i]->icon->core);
3185 void wDockRaise(WDock *dock)
3187 int i;
3188 WDrawerChain *dc;
3190 for (i = dock->max_icons - 1; i >= 0; i--) {
3191 if (dock->icon_array[i])
3192 wRaiseFrame(dock->icon_array[i]->icon->core);
3194 if (dock->type == WM_DOCK) {
3195 for (dc = dock->screen_ptr->drawers; dc != NULL; dc = dc->next)
3196 wDockRaise(dc->adrawer);
3200 void wDockRaiseLower(WDock *dock)
3202 if (!dock->icon_array[0]->icon->core->stacking->above
3203 || (dock->icon_array[0]->icon->core->stacking->window_level
3204 != dock->icon_array[0]->icon->core->stacking->above->stacking->window_level))
3205 wDockLower(dock);
3206 else
3207 wDockRaise(dock);
3210 void wDockFinishLaunch(WAppIcon *icon)
3212 icon->launching = 0;
3213 icon->relaunching = 0;
3214 dockIconPaint(icon);
3217 WAppIcon *wDockFindIconForWindow(WDock *dock, Window window)
3219 WAppIcon *icon;
3220 int i;
3222 for (i = 0; i < dock->max_icons; i++) {
3223 icon = dock->icon_array[i];
3224 if (icon && icon->main_window == window)
3225 return icon;
3227 return NULL;
3230 void wDockTrackWindowLaunch(WDock *dock, Window window)
3232 WAppIcon *icon;
3233 char *wm_class, *wm_instance;
3234 int i;
3235 Bool firstPass = True;
3236 Bool found = False;
3237 char *command = NULL;
3239 if (!PropGetWMClass(window, &wm_class, &wm_instance)) {
3240 free(wm_class);
3241 free(wm_instance);
3242 return;
3245 command = GetCommandForWindow(window);
3246 retry:
3247 for (i = 0; i < dock->max_icons; i++) {
3248 icon = dock->icon_array[i];
3249 if (!icon)
3250 continue;
3252 /* app is already attached to icon */
3253 if (icon->main_window == window) {
3254 found = True;
3255 break;
3258 if ((icon->wm_instance || icon->wm_class)
3259 && (icon->launching || !icon->running)) {
3261 if (icon->wm_instance && wm_instance && strcmp(icon->wm_instance, wm_instance) != 0)
3262 continue;
3264 if (icon->wm_class && wm_class && strcmp(icon->wm_class, wm_class) != 0)
3265 continue;
3267 if (firstPass && command && strcmp(icon->command, command) != 0)
3268 continue;
3270 if (!icon->relaunching) {
3271 WApplication *wapp;
3273 /* Possibly an application that was docked with dockit,
3274 * but the user did not update WMState to indicate that
3275 * it was docked by force */
3276 wapp = wApplicationOf(window);
3277 if (!wapp) {
3278 icon->forced_dock = 1;
3279 icon->running = 0;
3281 if (!icon->forced_dock)
3282 icon->main_window = window;
3284 found = True;
3285 if (!wPreferences.no_animations && !icon->launching &&
3286 !dock->screen_ptr->flags.startup && !dock->collapsed) {
3287 WAppIcon *aicon;
3288 int x0, y0;
3290 icon->launching = 1;
3291 dockIconPaint(icon);
3293 aicon = wAppIconCreateForDock(dock->screen_ptr, NULL,
3294 wm_instance, wm_class, TILE_NORMAL);
3295 /* XXX: can: aicon->icon == NULL ? */
3296 PlaceIcon(dock->screen_ptr, &x0, &y0, wGetHeadForWindow(aicon->icon->owner));
3297 wAppIconMove(aicon, x0, y0);
3298 /* Should this always be lowered? -Dan */
3299 if (dock->lowered)
3300 wLowerFrame(aicon->icon->core);
3301 XMapWindow(dpy, aicon->icon->core->window);
3302 aicon->launching = 1;
3303 wAppIconPaint(aicon);
3304 slide_window(aicon->icon->core->window, x0, y0, icon->x_pos, icon->y_pos);
3305 XUnmapWindow(dpy, aicon->icon->core->window);
3306 wAppIconDestroy(aicon);
3308 wDockFinishLaunch(icon);
3309 break;
3313 if (firstPass && !found) {
3314 firstPass = False;
3315 goto retry;
3318 if (command)
3319 wfree(command);
3321 if (wm_class)
3322 free(wm_class);
3323 if (wm_instance)
3324 free(wm_instance);
3327 void wClipUpdateForWorkspaceChange(WScreen *scr, int workspace)
3329 if (!wPreferences.flags.noclip) {
3330 scr->clip_icon->dock = scr->workspaces[workspace]->clip;
3331 if (scr->current_workspace != workspace) {
3332 WDock *old_clip = scr->workspaces[scr->current_workspace]->clip;
3333 WAppIconChain *chain = scr->global_icons;
3335 while (chain) {
3336 wDockMoveIconBetweenDocks(chain->aicon->dock,
3337 scr->workspaces[workspace]->clip,
3338 chain->aicon, chain->aicon->xindex, chain->aicon->yindex);
3339 if (scr->workspaces[workspace]->clip->collapsed)
3340 XUnmapWindow(dpy, chain->aicon->icon->core->window);
3341 chain = chain->next;
3344 wDockHideIcons(old_clip);
3345 if (old_clip->auto_raise_lower) {
3346 if (old_clip->auto_raise_magic) {
3347 WMDeleteTimerHandler(old_clip->auto_raise_magic);
3348 old_clip->auto_raise_magic = NULL;
3350 wDockLower(old_clip);
3352 if (old_clip->auto_collapse) {
3353 if (old_clip->auto_expand_magic) {
3354 WMDeleteTimerHandler(old_clip->auto_expand_magic);
3355 old_clip->auto_expand_magic = NULL;
3357 old_clip->collapsed = 1;
3359 wDockShowIcons(scr->workspaces[workspace]->clip);
3364 static void trackDeadProcess(pid_t pid, unsigned int status, void *cdata)
3366 WDock *dock = cdata;
3367 WAppIcon *icon;
3368 int i;
3370 for (i = 0; i < dock->max_icons; i++) {
3371 icon = dock->icon_array[i];
3372 if (!icon)
3373 continue;
3375 if (icon->launching && icon->pid == pid) {
3376 if (!icon->relaunching) {
3377 icon->running = 0;
3378 icon->main_window = None;
3380 wDockFinishLaunch(icon);
3381 icon->pid = 0;
3382 if (status == 111) {
3383 char msg[PATH_MAX];
3384 char *cmd;
3386 #ifdef USE_DOCK_XDND
3387 if (icon->drop_launch)
3388 cmd = icon->dnd_command;
3389 else
3390 #endif
3391 if (icon->paste_launch)
3392 cmd = icon->paste_command;
3393 else
3394 cmd = icon->command;
3396 snprintf(msg, sizeof(msg), _("Could not execute command \"%s\""), cmd);
3398 wMessageDialog(dock->screen_ptr, _("Error"), msg, _("OK"), NULL, NULL);
3400 break;
3405 /* This function is called when the dock switches state between
3406 * "normal" (including auto-raise/lower) and "keep on top". It is
3407 * therefore clearly distinct from wDockLower/Raise, which are called
3408 * each time a not-kept-on-top dock is lowered/raised. */
3409 static void toggleLowered(WDock *dock)
3411 WAppIcon *tmp;
3412 WDrawerChain *dc;
3413 int newlevel, i;
3415 if (!dock->lowered) {
3416 newlevel = WMNormalLevel;
3417 dock->lowered = 1;
3418 } else {
3419 newlevel = WMDockLevel;
3420 dock->lowered = 0;
3423 for (i = 0; i < dock->max_icons; i++) {
3424 tmp = dock->icon_array[i];
3425 if (!tmp)
3426 continue;
3428 ChangeStackingLevel(tmp->icon->core, newlevel);
3430 /* When the dock is no longer "on top", explicitly lower it as well.
3431 * It saves some CPU cycles (probably) to do it ourselves here
3432 * rather than calling wDockLower at the end of toggleLowered */
3433 if (dock->lowered)
3434 wLowerFrame(tmp->icon->core);
3437 if (dock->type == WM_DOCK) {
3438 for (dc = dock->screen_ptr->drawers; dc != NULL; dc = dc->next) {
3439 toggleLowered(dc->adrawer);
3441 wScreenUpdateUsableArea(dock->screen_ptr);
3445 static void toggleCollapsed(WDock *dock)
3447 if (dock->collapsed) {
3448 dock->collapsed = 0;
3449 wDockShowIcons(dock);
3450 } else {
3451 dock->collapsed = 1;
3452 wDockHideIcons(dock);
3456 static void openDockMenu(WDock *dock, WAppIcon *aicon, XEvent *event)
3458 WScreen *scr = dock->screen_ptr;
3459 WObjDescriptor *desc;
3460 WMenuEntry *entry;
3461 WApplication *wapp = NULL;
3462 int index = 0;
3463 int x_pos;
3464 int n_selected;
3465 int appIsRunning = aicon->running && aicon->icon && aicon->icon->owner;
3467 if (dock->type == WM_DOCK) {
3468 /* Dock position menu */
3469 updateDockPositionMenu(scr->dock_pos_menu, dock);
3470 dock->menu->flags.realized = 0;
3471 if (!wPreferences.flags.nodrawer) {
3472 /* add a drawer */
3473 entry = dock->menu->entries[++index];
3474 entry->clientdata = aicon;
3475 wMenuSetEnabled(dock->menu, index, True);
3477 } else {
3478 /* clip/drawer options */
3479 if (scr->clip_options)
3480 updateClipOptionsMenu(scr->clip_options, dock);
3482 n_selected = numberOfSelectedIcons(dock);
3484 if (dock->type == WM_CLIP) {
3485 /* Rename Workspace */
3486 entry = dock->menu->entries[++index];
3487 if (aicon == scr->clip_icon) {
3488 entry->callback = renameCallback;
3489 entry->clientdata = dock;
3490 entry->flags.indicator = 0;
3491 entry->text = _("Rename Workspace");
3492 } else {
3493 entry->callback = omnipresentCallback;
3494 entry->clientdata = aicon;
3495 if (n_selected > 0) {
3496 entry->flags.indicator = 0;
3497 entry->text = _("Toggle Omnipresent");
3498 } else {
3499 entry->flags.indicator = 1;
3500 entry->flags.indicator_on = aicon->omnipresent;
3501 entry->flags.indicator_type = MI_CHECK;
3502 entry->text = _("Omnipresent");
3507 /* select/unselect icon */
3508 entry = dock->menu->entries[++index];
3509 entry->clientdata = aicon;
3510 entry->flags.indicator_on = aicon->icon->selected;
3511 wMenuSetEnabled(dock->menu, index, aicon != scr->clip_icon && !wIsADrawer(aicon));
3513 /* select/unselect all icons */
3514 entry = dock->menu->entries[++index];
3515 entry->clientdata = aicon;
3516 if (n_selected > 0)
3517 entry->text = _("Unselect All Icons");
3518 else
3519 entry->text = _("Select All Icons");
3521 wMenuSetEnabled(dock->menu, index, dock->icon_count > 1);
3523 /* keep icon(s) */
3524 entry = dock->menu->entries[++index];
3525 entry->clientdata = aicon;
3526 if (n_selected > 1)
3527 entry->text = _("Keep Icons");
3528 else
3529 entry->text = _("Keep Icon");
3531 wMenuSetEnabled(dock->menu, index, dock->icon_count > 1);
3533 if (dock->type == WM_CLIP) {
3534 /* this is the workspace submenu part */
3535 entry = dock->menu->entries[++index];
3536 if (n_selected > 1)
3537 entry->text = _("Move Icons To");
3538 else
3539 entry->text = _("Move Icon To");
3541 if (scr->clip_submenu)
3542 updateWorkspaceMenu(scr->clip_submenu, aicon);
3544 wMenuSetEnabled(dock->menu, index, !aicon->omnipresent);
3547 /* remove icon(s) */
3548 entry = dock->menu->entries[++index];
3549 entry->clientdata = aicon;
3550 if (n_selected > 1)
3551 entry->text = _("Remove Icons");
3552 else
3553 entry->text = _("Remove Icon");
3555 wMenuSetEnabled(dock->menu, index, dock->icon_count > 1);
3557 /* attract icon(s) */
3558 entry = dock->menu->entries[++index];
3559 entry->clientdata = aicon;
3561 dock->menu->flags.realized = 0;
3562 wMenuRealize(dock->menu);
3565 if (aicon->icon->owner)
3566 wapp = wApplicationOf(aicon->icon->owner->main_window);
3567 else
3568 wapp = NULL;
3570 /* launch */
3571 entry = dock->menu->entries[++index];
3572 entry->clientdata = aicon;
3573 wMenuSetEnabled(dock->menu, index, aicon->command != NULL);
3575 /* unhide here */
3576 entry = dock->menu->entries[++index];
3577 entry->clientdata = aicon;
3578 if (wapp && wapp->flags.hidden)
3579 entry->text = _("Unhide Here");
3580 else
3581 entry->text = _("Bring Here");
3583 wMenuSetEnabled(dock->menu, index, appIsRunning);
3585 /* hide */
3586 entry = dock->menu->entries[++index];
3587 entry->clientdata = aicon;
3588 if (wapp && wapp->flags.hidden)
3589 entry->text = _("Unhide");
3590 else
3591 entry->text = _("Hide");
3593 wMenuSetEnabled(dock->menu, index, appIsRunning);
3595 /* settings */
3596 entry = dock->menu->entries[++index];
3597 entry->clientdata = aicon;
3598 wMenuSetEnabled(dock->menu, index, !aicon->editing && !wPreferences.flags.noupdates);
3600 /* kill or remove drawer */
3601 entry = dock->menu->entries[++index];
3602 entry->clientdata = aicon;
3603 if (wIsADrawer(aicon)) {
3604 entry->callback = removeDrawerCallback;
3605 entry->text = _("Remove drawer");
3606 wMenuSetEnabled(dock->menu, index, True);
3607 } else {
3608 entry->callback = killCallback;
3609 entry->text = _("Kill");
3610 wMenuSetEnabled(dock->menu, index, appIsRunning);
3613 if (!dock->menu->flags.realized)
3614 wMenuRealize(dock->menu);
3616 if (dock->type == WM_CLIP || dock->type == WM_DRAWER) {
3617 /*x_pos = event->xbutton.x_root+2; */
3618 x_pos = event->xbutton.x_root - dock->menu->frame->core->width / 2 - 1;
3619 if (x_pos < 0) {
3620 x_pos = 0;
3621 } else if (x_pos + dock->menu->frame->core->width > scr->scr_width - 2) {
3622 x_pos = scr->scr_width - dock->menu->frame->core->width - 4;
3624 } else {
3625 x_pos = dock->x_pos;
3626 if (dock->on_right_side)
3627 x_pos += ICON_SIZE + DOCK_EXTRA_SPACE
3628 - dock->menu->frame->core->width - 3;
3631 wMenuMapAt(dock->menu, x_pos, event->xbutton.y_root + 2, False);
3633 /* allow drag select */
3634 event->xany.send_event = True;
3635 desc = &dock->menu->menu->descriptor;
3636 (*desc->handle_mousedown) (desc, event);
3639 /******************************************************************/
3640 static void iconDblClick(WObjDescriptor *desc, XEvent *event)
3642 WAppIcon *btn = desc->parent;
3643 WDock *dock = btn->dock;
3644 WApplication *wapp = NULL;
3645 int unhideHere = 0;
3647 if (btn->icon->owner && !(event->xbutton.state & ControlMask)) {
3648 wapp = wApplicationOf(btn->icon->owner->main_window);
3650 assert(wapp != NULL);
3652 unhideHere = (event->xbutton.state & ShiftMask);
3654 /* go to the last workspace that the user worked on the app */
3655 if (wapp->last_workspace != dock->screen_ptr->current_workspace && !unhideHere)
3656 wWorkspaceChange(dock->screen_ptr, wapp->last_workspace);
3658 wUnhideApplication(wapp, event->xbutton.button == Button2, unhideHere);
3660 if (event->xbutton.state & MOD_MASK)
3661 wHideOtherApplications(btn->icon->owner);
3662 } else {
3663 if (event->xbutton.button == Button1) {
3664 if (event->xbutton.state & MOD_MASK) {
3665 /* raise/lower dock */
3666 toggleLowered(dock);
3667 } else if (btn == dock->screen_ptr->clip_icon) {
3668 if (getClipButton(event->xbutton.x, event->xbutton.y) != CLIP_IDLE)
3669 handleClipChangeWorkspace(dock->screen_ptr, event);
3670 else if (wPreferences.flags.clip_merged_in_dock) {
3671 // Is actually the dock
3672 if (btn->command)
3674 if (!btn->launching && (!btn->running || (event->xbutton.state & ControlMask)))
3675 launchDockedApplication(btn, False);
3677 else
3679 wShowInfoPanel(dock->screen_ptr);
3682 else
3683 toggleCollapsed(dock);
3684 } else if (wIsADrawer(btn)) {
3685 toggleCollapsed(dock);
3686 } else if (btn->command) {
3687 if (!btn->launching && (!btn->running || (event->xbutton.state & ControlMask)))
3688 launchDockedApplication(btn, False);
3689 } else if (btn->xindex == 0 && btn->yindex == 0 && btn->dock->type == WM_DOCK) {
3690 wShowInfoPanel(dock->screen_ptr);
3696 static void handleDockMove(WDock *dock, WAppIcon *aicon, XEvent *event)
3698 WScreen *scr = dock->screen_ptr;
3699 int ofs_x = event->xbutton.x, ofs_y = event->xbutton.y;
3700 WIcon *icon = aicon->icon;
3701 WAppIcon *tmpaicon;
3702 WDrawerChain *dc;
3703 int x = aicon->x_pos, y = aicon->y_pos;;
3704 int shad_x = x, shad_y = y;
3705 XEvent ev;
3706 int grabbed = 0, done, previously_on_right, now_on_right, previous_x_pos, i;
3707 Pixmap ghost = None;
3708 int superfluous = wPreferences.superfluous; /* we catch it to avoid problems */
3710 if (XGrabPointer(dpy, aicon->icon->core->window, True, ButtonMotionMask
3711 | ButtonReleaseMask | ButtonPressMask, GrabModeAsync,
3712 GrabModeAsync, None, None, CurrentTime) != GrabSuccess)
3713 wwarning("pointer grab failed for dock move");
3715 if (dock->type == WM_DRAWER) {
3716 Window wins[2];
3717 wins[0] = icon->core->window;
3718 wins[1] = scr->dock_shadow;
3719 XRestackWindows(dpy, wins, 2);
3720 XMoveResizeWindow(dpy, scr->dock_shadow, aicon->x_pos, aicon->y_pos,
3721 ICON_SIZE, ICON_SIZE);
3722 if (superfluous) {
3723 if (icon->pixmap!=None)
3724 ghost = MakeGhostIcon(scr, icon->pixmap);
3725 else
3726 ghost = MakeGhostIcon(scr, icon->core->window);
3728 XSetWindowBackgroundPixmap(dpy, scr->dock_shadow, ghost);
3729 XClearWindow(dpy, scr->dock_shadow);
3731 XMapWindow(dpy, scr->dock_shadow);
3734 previously_on_right = now_on_right = dock->on_right_side;
3735 previous_x_pos = dock->x_pos;
3736 done = 0;
3737 while (!done) {
3738 WMMaskEvent(dpy, PointerMotionMask | ButtonReleaseMask | ButtonPressMask
3739 | ButtonMotionMask | ExposureMask | EnterWindowMask, &ev);
3740 switch (ev.type) {
3741 case Expose:
3742 WMHandleEvent(&ev);
3743 break;
3745 case EnterNotify:
3746 /* It means the cursor moved so fast that it entered
3747 * something else (if moving slowly, it would have
3748 * stayed in the dock that is being moved. Ignore such
3749 * "spurious" EnterNotifiy's */
3750 break;
3752 case MotionNotify:
3753 if (!grabbed) {
3754 if (abs(ofs_x - ev.xmotion.x) >= MOVE_THRESHOLD
3755 || abs(ofs_y - ev.xmotion.y) >= MOVE_THRESHOLD) {
3756 XChangeActivePointerGrab(dpy, ButtonMotionMask
3757 | ButtonReleaseMask | ButtonPressMask,
3758 wPreferences.cursor[WCUR_MOVE], CurrentTime);
3759 grabbed = 1;
3761 break;
3763 switch (dock->type) {
3764 case WM_CLIP:
3765 x = ev.xmotion.x_root - ofs_x;
3766 y = ev.xmotion.y_root - ofs_y;
3767 wScreenKeepInside(scr, &x, &y, ICON_SIZE, ICON_SIZE);
3768 moveDock(dock, x, y);
3769 break;
3770 case WM_DOCK:
3771 x = ev.xmotion.x_root - ofs_x;
3772 y = ev.xmotion.y_root - ofs_y;
3773 if (previously_on_right)
3775 now_on_right = (ev.xmotion.x_root >= previous_x_pos - ICON_SIZE);
3777 else
3779 now_on_right = (ev.xmotion.x_root > previous_x_pos + ICON_SIZE * 2);
3781 if (now_on_right != dock->on_right_side)
3783 dock->on_right_side = now_on_right;
3784 wDockSwap(dock);
3785 wArrangeIcons(scr, False);
3787 // Also perform the vertical move
3788 wScreenKeepInside(scr, &x, &y, ICON_SIZE, ICON_SIZE);
3789 moveDock(dock, dock->x_pos, y);
3790 if (wPreferences.flags.wrap_appicons_in_dock)
3792 for (i = 0; i < dock->max_icons; i++) {
3793 int new_y, new_index, j, ok;
3795 tmpaicon = dock->icon_array[i];
3796 if (tmpaicon == NULL)
3797 continue;
3798 if (onScreen(scr, tmpaicon->x_pos, tmpaicon->y_pos))
3799 continue;
3800 new_y = (tmpaicon->y_pos + ICON_SIZE * dock->max_icons) % (ICON_SIZE * dock->max_icons);
3801 new_index = (new_y - dock->y_pos) / ICON_SIZE;
3802 if (!onScreen(scr, tmpaicon->x_pos, new_y))
3803 continue;
3804 ok = 1;
3805 for (j = 0; j < dock->max_icons; j++)
3807 if (dock->icon_array[j] != NULL &&
3808 dock->icon_array[j]->yindex == new_index)
3810 ok = 0;
3811 break;
3814 if (!ok || getDrawer(scr, new_index) != NULL)
3815 continue;
3816 wDockReattachIcon(dock, tmpaicon, tmpaicon->xindex, new_index);
3818 for (dc = scr->drawers; dc != NULL; dc = dc->next)
3820 int new_y, new_index, j, ok;
3821 tmpaicon = dc->adrawer->icon_array[0];
3822 if (onScreen(scr, tmpaicon->x_pos, tmpaicon->y_pos))
3823 continue;
3824 new_y = (tmpaicon->y_pos + ICON_SIZE * dock->max_icons) % (ICON_SIZE * dock->max_icons);
3825 new_index = (new_y - dock->y_pos) / ICON_SIZE;
3826 if (!onScreen(scr, tmpaicon->x_pos, new_y))
3827 continue;
3828 ok = 1;
3829 for (j = 0; j < dock->max_icons; j++)
3831 if (dock->icon_array[j] != NULL &&
3832 dock->icon_array[j]->yindex == new_index)
3834 ok = 0;
3835 break;
3838 if (!ok || getDrawer(scr, new_index) != NULL)
3839 continue;
3840 moveDock(dc->adrawer, tmpaicon->x_pos, new_y);
3843 break;
3844 case WM_DRAWER:
3846 WDock *real_dock = dock->screen_ptr->dock;
3847 Bool snapped;
3848 int ix, iy;
3849 x = ev.xmotion.x_root - ofs_x;
3850 y = ev.xmotion.y_root - ofs_y;
3851 snapped = wDockSnapIcon(real_dock, aicon, x, y, &ix, &iy, True);
3852 if (snapped) {
3853 shad_x = real_dock->x_pos + ix * wPreferences.icon_size;
3854 shad_y = real_dock->y_pos + iy * wPreferences.icon_size;
3855 XMoveWindow(dpy, scr->dock_shadow, shad_x, shad_y);
3857 moveDock(dock, x, y);
3858 break;
3861 break;
3863 case ButtonPress:
3864 break;
3866 case ButtonRelease:
3867 if (ev.xbutton.button != event->xbutton.button)
3868 break;
3869 XUngrabPointer(dpy, CurrentTime);
3870 if (dock->type == WM_DRAWER) {
3871 Window wins[dock->icon_count];
3872 int offset_index;
3875 * When the dock is on the Right side, the index of the icons are negative to
3876 * reflect the fact that they are placed on the other side of the dock; we use
3877 * an offset here so we can have an always positive index for the storage in
3878 * the 'wins' array.
3880 if (dock->on_right_side)
3881 offset_index = dock->icon_count - 1;
3882 else
3883 offset_index = 0;
3885 for (i = 0; i < dock->max_icons; i++) {
3886 tmpaicon = dock->icon_array[i];
3887 if (tmpaicon == NULL)
3888 continue;
3889 wins[tmpaicon->xindex + offset_index] = tmpaicon->icon->core->window;
3891 slide_windows(wins, dock->icon_count,
3892 (dock->on_right_side ? x - (dock->icon_count - 1) * ICON_SIZE : x),
3894 (dock->on_right_side ? shad_x - (dock->icon_count - 1) * ICON_SIZE : shad_x),
3895 shad_y);
3896 XUnmapWindow(dpy, scr->dock_shadow);
3897 moveDock(dock, shad_x, shad_y);
3898 XResizeWindow(dpy, scr->dock_shadow, ICON_SIZE, ICON_SIZE);
3900 done = 1;
3901 break;
3904 if (superfluous) {
3905 if (ghost != None)
3906 XFreePixmap(dpy, ghost);
3907 XSetWindowBackground(dpy, scr->dock_shadow, scr->white_pixel);
3912 static int getClipButton(int px, int py)
3914 int pt = (CLIP_BUTTON_SIZE + 2) * ICON_SIZE / 64;
3916 if (px < 0 || py < 0 || px >= ICON_SIZE || py >= ICON_SIZE)
3917 return CLIP_IDLE;
3919 if (py <= pt - ((int)ICON_SIZE - 1 - px))
3920 return CLIP_FORWARD;
3921 else if (px <= pt - ((int)ICON_SIZE - 1 - py))
3922 return CLIP_REWIND;
3924 return CLIP_IDLE;
3927 static void handleClipChangeWorkspace(WScreen *scr, XEvent *event)
3929 XEvent ev;
3930 int done, direction, new_ws;
3931 int new_dir;
3932 WDock *clip = scr->clip_icon->dock;
3934 direction = getClipButton(event->xbutton.x, event->xbutton.y);
3936 clip->lclip_button_pushed = direction == CLIP_REWIND;
3937 clip->rclip_button_pushed = direction == CLIP_FORWARD;
3939 wClipIconPaint(scr->clip_icon);
3940 done = 0;
3941 while (!done) {
3942 WMMaskEvent(dpy, ExposureMask | ButtonMotionMask | ButtonReleaseMask | ButtonPressMask, &ev);
3943 switch (ev.type) {
3944 case Expose:
3945 WMHandleEvent(&ev);
3946 break;
3948 case MotionNotify:
3949 new_dir = getClipButton(ev.xmotion.x, ev.xmotion.y);
3950 if (new_dir != direction) {
3951 direction = new_dir;
3952 clip->lclip_button_pushed = direction == CLIP_REWIND;
3953 clip->rclip_button_pushed = direction == CLIP_FORWARD;
3954 wClipIconPaint(scr->clip_icon);
3956 break;
3958 case ButtonPress:
3959 break;
3961 case ButtonRelease:
3962 if (ev.xbutton.button == event->xbutton.button)
3963 done = 1;
3967 clip->lclip_button_pushed = 0;
3968 clip->rclip_button_pushed = 0;
3970 new_ws = wPreferences.ws_advance || (event->xbutton.state & ControlMask);
3972 if (direction == CLIP_FORWARD) {
3973 if (scr->current_workspace < scr->workspace_count - 1)
3974 wWorkspaceChange(scr, scr->current_workspace + 1);
3975 else if (new_ws && scr->current_workspace < MAX_WORKSPACES - 1)
3976 wWorkspaceChange(scr, scr->current_workspace + 1);
3977 else if (wPreferences.ws_cycle)
3978 wWorkspaceChange(scr, 0);
3979 } else if (direction == CLIP_REWIND) {
3980 if (scr->current_workspace > 0)
3981 wWorkspaceChange(scr, scr->current_workspace - 1);
3982 else if (scr->current_workspace == 0 && wPreferences.ws_cycle)
3983 wWorkspaceChange(scr, scr->workspace_count - 1);
3986 wClipIconPaint(scr->clip_icon);
3989 static void iconMouseDown(WObjDescriptor *desc, XEvent *event)
3991 WAppIcon *aicon = desc->parent;
3992 WDock *dock = aicon->dock;
3993 WScreen *scr = aicon->icon->core->screen_ptr;
3995 if (aicon->editing || WCHECK_STATE(WSTATE_MODAL))
3996 return;
3998 scr->last_dock = dock;
4000 if (dock->menu->flags.mapped)
4001 wMenuUnmap(dock->menu);
4003 if (IsDoubleClick(scr, event)) {
4004 /* double-click was not in the main clip icon */
4005 if (dock->type != WM_CLIP || aicon->xindex != 0 || aicon->yindex != 0
4006 || getClipButton(event->xbutton.x, event->xbutton.y) == CLIP_IDLE) {
4007 iconDblClick(desc, event);
4008 return;
4012 if (event->xbutton.button == Button1) {
4013 if (event->xbutton.state & MOD_MASK)
4014 wDockLower(dock);
4015 else
4016 wDockRaise(dock);
4018 if ((event->xbutton.state & ShiftMask) && aicon != scr->clip_icon && dock->type != WM_DOCK) {
4019 wIconSelect(aicon->icon);
4020 return;
4023 if (aicon->yindex == 0 && aicon->xindex == 0) {
4024 if (getClipButton(event->xbutton.x, event->xbutton.y) != CLIP_IDLE &&
4025 (dock->type == WM_CLIP || (dock->type == WM_DOCK && wPreferences.flags.clip_merged_in_dock)))
4026 handleClipChangeWorkspace(scr, event);
4027 else
4028 handleDockMove(dock, aicon, event);
4029 } else {
4030 Bool hasMoved = wHandleAppIconMove(aicon, event);
4031 if (wPreferences.single_click && !hasMoved)
4032 iconDblClick(desc, event);
4034 } else if (event->xbutton.button == Button2 && aicon == scr->clip_icon) {
4035 if (!scr->clip_ws_menu)
4036 scr->clip_ws_menu = wWorkspaceMenuMake(scr, False);
4038 if (scr->clip_ws_menu) {
4039 WMenu *wsMenu = scr->clip_ws_menu;
4040 int xpos;
4042 wWorkspaceMenuUpdate(scr, wsMenu);
4044 xpos = event->xbutton.x_root - wsMenu->frame->core->width / 2 - 1;
4045 if (xpos < 0) {
4046 xpos = 0;
4047 } else if (xpos + wsMenu->frame->core->width > scr->scr_width - 2) {
4048 xpos = scr->scr_width - wsMenu->frame->core->width - 4;
4050 wMenuMapAt(wsMenu, xpos, event->xbutton.y_root + 2, False);
4052 desc = &wsMenu->menu->descriptor;
4053 event->xany.send_event = True;
4054 (*desc->handle_mousedown) (desc, event);
4056 } else if (event->xbutton.button == Button2 && dock->type == WM_CLIP &&
4057 (event->xbutton.state & ShiftMask) && aicon != scr->clip_icon) {
4058 wClipMakeIconOmnipresent(aicon, !aicon->omnipresent);
4059 } else if (event->xbutton.button == Button3) {
4060 if (event->xbutton.send_event &&
4061 XGrabPointer(dpy, aicon->icon->core->window, True, ButtonMotionMask
4062 | ButtonReleaseMask | ButtonPressMask, GrabModeAsync,
4063 GrabModeAsync, None, None, CurrentTime) != GrabSuccess) {
4064 wwarning("pointer grab failed for dockicon menu");
4065 return;
4068 openDockMenu(dock, aicon, event);
4069 } else if (event->xbutton.button == Button2) {
4070 WAppIcon *btn = desc->parent;
4072 if (!btn->launching && (!btn->running || (event->xbutton.state & ControlMask)))
4073 launchDockedApplication(btn, True);
4074 } else if (event->xbutton.button == Button4 && dock->type == WM_CLIP) {
4075 wWorkspaceRelativeChange(scr, 1);
4076 } else if (event->xbutton.button == Button5 && dock->type == WM_CLIP) {
4077 wWorkspaceRelativeChange(scr, -1);
4081 static void clipEnterNotify(WObjDescriptor *desc, XEvent *event)
4083 WAppIcon *btn = (WAppIcon *) desc->parent;
4084 WDock *dock, *tmp;
4085 WScreen *scr;
4087 /* Parameter not used, but tell the compiler that it is ok */
4088 (void) event;
4090 assert(event->type == EnterNotify);
4092 if (desc->parent_type != WCLASS_DOCK_ICON)
4093 return;
4095 scr = btn->icon->core->screen_ptr;
4096 dock = btn->dock;
4098 if (dock == NULL)
4099 return;
4101 /* The auto raise/lower code */
4102 tmp = (dock->type == WM_DRAWER ? scr->dock : dock);
4103 if (tmp->auto_lower_magic) {
4104 WMDeleteTimerHandler(tmp->auto_lower_magic);
4105 tmp->auto_lower_magic = NULL;
4107 if (tmp->auto_raise_lower && !tmp->auto_raise_magic)
4108 tmp->auto_raise_magic = WMAddTimerHandler(wPreferences.clip_auto_raise_delay, clipAutoRaise, (void *) tmp);
4110 if (dock->type != WM_CLIP && dock->type != WM_DRAWER)
4111 return;
4113 /* The auto expand/collapse code */
4114 if (dock->auto_collapse_magic) {
4115 WMDeleteTimerHandler(dock->auto_collapse_magic);
4116 dock->auto_collapse_magic = NULL;
4118 if (dock->auto_collapse && !dock->auto_expand_magic)
4119 dock->auto_expand_magic = WMAddTimerHandler(wPreferences.clip_auto_expand_delay, clipAutoExpand, (void *)dock);
4122 static void clipLeave(WDock *dock)
4124 XEvent event;
4125 WObjDescriptor *desc = NULL;
4126 WDock *tmp;
4128 if (dock == NULL)
4129 return;
4131 if (XCheckTypedEvent(dpy, EnterNotify, &event) != False) {
4132 if (XFindContext(dpy, event.xcrossing.window, w_global.context.client_win,
4133 (XPointer *) & desc) != XCNOENT
4134 && desc && desc->parent_type == WCLASS_DOCK_ICON
4135 && ((WAppIcon *) desc->parent)->dock == dock) {
4136 /* We haven't left the dock/clip/drawer yet */
4137 XPutBackEvent(dpy, &event);
4138 return;
4141 XPutBackEvent(dpy, &event);
4142 } else {
4143 /* We entered a withdrawn window, so we're still in Clip */
4144 return;
4147 tmp = (dock->type == WM_DRAWER ? dock->screen_ptr->dock : dock);
4148 if (tmp->auto_raise_magic) {
4149 WMDeleteTimerHandler(tmp->auto_raise_magic);
4150 tmp->auto_raise_magic = NULL;
4152 if (tmp->auto_raise_lower && !tmp->auto_lower_magic)
4153 tmp->auto_lower_magic = WMAddTimerHandler(wPreferences.clip_auto_lower_delay, clipAutoLower, (void *)tmp);
4155 if (dock->type != WM_CLIP && dock->type != WM_DRAWER)
4156 return;
4158 if (dock->auto_expand_magic) {
4159 WMDeleteTimerHandler(dock->auto_expand_magic);
4160 dock->auto_expand_magic = NULL;
4162 if (dock->auto_collapse && !dock->auto_collapse_magic)
4163 dock->auto_collapse_magic = WMAddTimerHandler(wPreferences.clip_auto_collapse_delay, clipAutoCollapse, (void *)dock);
4166 static void clipLeaveNotify(WObjDescriptor *desc, XEvent *event)
4168 WAppIcon *btn = (WAppIcon *) desc->parent;
4170 /* Parameter not used, but tell the compiler that it is ok */
4171 (void) event;
4173 assert(event->type == LeaveNotify);
4175 if (desc->parent_type != WCLASS_DOCK_ICON)
4176 return;
4178 clipLeave(btn->dock);
4181 static void clipAutoCollapse(void *cdata)
4183 WDock *dock = (WDock *) cdata;
4185 if (dock->type != WM_CLIP && dock->type != WM_DRAWER)
4186 return;
4188 if (dock->auto_collapse) {
4189 dock->collapsed = 1;
4190 wDockHideIcons(dock);
4192 dock->auto_collapse_magic = NULL;
4195 static void clipAutoExpand(void *cdata)
4197 WDock *dock = (WDock *) cdata;
4199 if (dock->type != WM_CLIP && dock->type != WM_DRAWER)
4200 return;
4202 if (dock->auto_collapse) {
4203 dock->collapsed = 0;
4204 wDockShowIcons(dock);
4206 dock->auto_expand_magic = NULL;
4209 static void clipAutoLower(void *cdata)
4211 WDock *dock = (WDock *) cdata;
4213 if (dock->auto_raise_lower)
4214 wDockLower(dock);
4216 dock->auto_lower_magic = NULL;
4219 static void clipAutoRaise(void *cdata)
4221 WDock *dock = (WDock *) cdata;
4223 if (dock->auto_raise_lower)
4224 wDockRaise(dock);
4226 dock->auto_raise_magic = NULL;
4229 static Bool iconCanBeOmnipresent(WAppIcon *aicon)
4231 WScreen *scr = aicon->icon->core->screen_ptr;
4232 WDock *clip;
4233 WAppIcon *btn;
4234 int i, j;
4236 for (i = 0; i < scr->workspace_count; i++) {
4237 clip = scr->workspaces[i]->clip;
4239 if (clip == aicon->dock)
4240 continue;
4242 if (clip->icon_count + scr->global_icon_count >= clip->max_icons)
4243 return False; /* Clip is full in some workspace */
4245 for (j = 0; j < clip->max_icons; j++) {
4246 btn = clip->icon_array[j];
4247 if (btn && btn->xindex == aicon->xindex && btn->yindex == aicon->yindex)
4248 return False;
4252 return True;
4255 int wClipMakeIconOmnipresent(WAppIcon *aicon, int omnipresent)
4257 WScreen *scr = aicon->icon->core->screen_ptr;
4258 WAppIconChain *new_entry, *tmp, *tmp1;
4259 int status = WO_SUCCESS;
4261 if ((scr->dock && aicon->dock == scr->dock) || aicon == scr->clip_icon)
4262 return WO_NOT_APPLICABLE;
4264 if (aicon->omnipresent == omnipresent)
4265 return WO_SUCCESS;
4267 if (omnipresent) {
4268 if (iconCanBeOmnipresent(aicon)) {
4269 aicon->omnipresent = 1;
4270 new_entry = wmalloc(sizeof(WAppIconChain));
4271 new_entry->aicon = aicon;
4272 new_entry->next = scr->global_icons;
4273 scr->global_icons = new_entry;
4274 scr->global_icon_count++;
4275 } else {
4276 aicon->omnipresent = 0;
4277 status = WO_FAILED;
4279 } else {
4280 aicon->omnipresent = 0;
4281 if (aicon == scr->global_icons->aicon) {
4282 tmp = scr->global_icons->next;
4283 wfree(scr->global_icons);
4284 scr->global_icons = tmp;
4285 scr->global_icon_count--;
4286 } else {
4287 tmp = scr->global_icons;
4288 while (tmp->next) {
4289 if (tmp->next->aicon == aicon) {
4290 tmp1 = tmp->next->next;
4291 wfree(tmp->next);
4292 tmp->next = tmp1;
4293 scr->global_icon_count--;
4294 break;
4296 tmp = tmp->next;
4301 wAppIconPaint(aicon);
4303 return status;
4306 static void drawerAppendToChain(WScreen *scr, WDock *drawer)
4308 WDrawerChain **where_to_add;
4310 where_to_add = &scr->drawers;
4311 while ((*where_to_add) != NULL) {
4312 where_to_add = &(*where_to_add)->next;
4315 *where_to_add = wmalloc(sizeof(WDrawerChain));
4316 (*where_to_add)->adrawer = drawer;
4317 (*where_to_add)->next = NULL;
4318 scr->drawer_count++;
4322 static void drawerRemoveFromChain(WScreen *scr, WDock *drawer)
4324 WDrawerChain *next, **to_remove;
4326 to_remove = &scr->drawers;
4327 while (True) {
4328 if (*to_remove == NULL) {
4329 wwarning("The drawer to be removed can not be found.");
4330 return;
4332 if ((*to_remove)->adrawer == drawer)
4333 break;
4335 to_remove = &(*to_remove)->next;
4337 next = (*to_remove)->next;
4338 wfree(*to_remove);
4339 *to_remove = next;
4340 scr->drawer_count--;
4344 /* Don't free the returned string. Duplicate it. */
4345 static char * findUniqueName(WScreen *scr, const char *instance_basename)
4347 static char buffer[128];
4348 WDrawerChain *dc;
4349 int i;
4350 Bool already_in_use = True;
4352 #define UNIQUE_NAME_WATCHDOG 128
4353 for (i = 0; already_in_use && i < UNIQUE_NAME_WATCHDOG; i++) {
4354 snprintf(buffer, sizeof buffer, "%s%d", instance_basename, i);
4356 already_in_use = False;
4358 for (dc = scr->drawers; dc != NULL; dc = dc->next) {
4359 if (!strncmp(dc->adrawer->icon_array[0]->wm_instance, buffer,
4360 sizeof buffer)) {
4361 already_in_use = True;
4362 break;
4367 if (i == UNIQUE_NAME_WATCHDOG)
4368 wwarning("Couldn't find a unique name for drawer in %d attempts.", i);
4369 #undef UNIQUE_NAME_WATCHDOG
4371 return buffer;
4375 static void drawerIconExpose(WObjDescriptor *desc, XEvent *event)
4377 /* Parameter not used, but tell the compiler that it is ok */
4378 (void) event;
4380 wDrawerIconPaint((WAppIcon *) desc->parent);
4384 static int addADrawer(WScreen *scr)
4386 int i, y, sig, found_y;
4387 WDock *drawer, *dock = scr->dock;
4388 WDrawerChain *dc;
4389 char can_be_here[2 * dock->max_icons - 1];
4391 if (dock->icon_count + scr->drawer_count >= dock->max_icons)
4392 return -1;
4394 for (y = -dock->max_icons + 1; y < dock->max_icons; y++) {
4395 can_be_here[y + dock->max_icons - 1] = True;
4397 for (i = 0; i < dock->max_icons; i++) {
4398 if (dock->icon_array[i] != NULL)
4399 can_be_here[dock->icon_array[i]->yindex + dock->max_icons - 1] = False;
4401 for (dc = scr->drawers; dc != NULL; dc = dc->next) {
4402 y = (int) ((dc->adrawer->y_pos - dock->y_pos) / ICON_SIZE);
4403 can_be_here[y + dock->max_icons - 1] = False;
4406 found_y = False;
4407 for (sig = 1; !found_y && sig > -2; sig -= 2) // 1, then -1
4409 for (y = sig; sig * y < dock->max_icons; y += sig)
4411 if (can_be_here[y + dock->max_icons - 1] &&
4412 onScreen(scr, dock->x_pos, dock->y_pos + y * ICON_SIZE))
4414 found_y = True;
4415 break;
4420 if (!found_y)
4421 /* This can happen even when dock->icon_count + scr->drawer_count
4422 * < dock->max_icons when the dock is not aligned on an
4423 * ICON_SIZE multiple, as some space is lost above and under it */
4424 return -1;
4426 drawer = wDockCreate(scr, WM_DRAWER, NULL);
4427 drawer->lowered = scr->dock->lowered;
4428 if (!drawer->lowered)
4429 ChangeStackingLevel(drawer->icon_array[0]->icon->core, WMDockLevel);
4430 else
4431 ChangeStackingLevel(drawer->icon_array[0]->icon->core, WMNormalLevel);
4432 drawer->auto_raise_lower = scr->dock->auto_raise_lower;
4433 drawer->x_pos = dock->x_pos;
4434 drawer->y_pos = dock->y_pos + ICON_SIZE * y;
4435 drawer->icon_array[0]->xindex = 0;
4436 drawer->icon_array[0]->yindex = 0;
4437 drawer->icon_array[0]->x_pos = drawer->x_pos;
4438 drawer->icon_array[0]->y_pos = drawer->y_pos;
4439 XMoveWindow(dpy, drawer->icon_array[0]->icon->core->window,
4440 drawer->icon_array[0]->x_pos, drawer->icon_array[0]->y_pos);
4442 return 0;
4446 static void addADrawerCallback(WMenu *menu, WMenuEntry *entry)
4448 /* Parameter not used, but tell the compiler that it is ok */
4449 (void) menu;
4451 assert(entry->clientdata!=NULL);
4452 addADrawer(((WAppIcon *) entry->clientdata)->dock->screen_ptr);
4456 static void drawerDestroy(WDock *drawer)
4458 WScreen *scr;
4459 int i;
4460 WAppIcon *aicon = NULL;
4461 WMArray *icons;
4463 if (drawer == NULL)
4464 return;
4466 scr = drawer->screen_ptr;
4468 /* Note regarding menus: we can't delete any dock/clip/drawer menu, because
4469 * that would (attempt to) wfree some memory in gettext library (see menu
4470 * entries that have several "versions", such like "Hide" and "Unhide"). */
4471 wDefaultPurgeInfo(drawer->icon_array[0]->wm_instance,
4472 drawer->icon_array[0]->wm_class);
4474 if (drawer->icon_count == 2) {
4475 /* Drawer contains a single appicon: dock it where the drawer was */
4476 for (i = 1; i < drawer->max_icons; i++) {
4477 aicon = drawer->icon_array[i];
4478 if (aicon != NULL)
4479 break;
4482 wDockMoveIconBetweenDocks(drawer, scr->dock, aicon,
4483 0, (drawer->y_pos - scr->dock->y_pos) / ICON_SIZE);
4484 XMoveWindow(dpy, aicon->icon->core->window, drawer->x_pos, drawer->y_pos);
4485 XMapWindow(dpy, aicon->icon->core->window);
4486 } else if (drawer->icon_count > 2) {
4487 icons = WMCreateArray(drawer->icon_count - 1);
4488 for (i = 1; i < drawer->max_icons; i++) {
4489 aicon = drawer->icon_array[i];
4490 if (aicon == NULL)
4491 continue;
4492 WMAddToArray(icons, aicon);
4494 removeIcons(icons, drawer);
4497 if (drawer->auto_collapse_magic) {
4498 WMDeleteTimerHandler(drawer->auto_collapse_magic);
4499 drawer->auto_collapse_magic = NULL;
4501 if (drawer->auto_lower_magic) {
4502 WMDeleteTimerHandler(drawer->auto_lower_magic);
4503 drawer->auto_lower_magic = NULL;
4506 wAppIconDestroy(drawer->icon_array[0]);
4507 wfree(drawer->icon_array);
4508 drawer->icon_array = NULL;
4510 drawerRemoveFromChain(scr, drawer);
4511 if (scr->last_dock == drawer)
4512 scr->last_dock = NULL;
4513 if (scr->attracting_drawer == drawer)
4514 scr->attracting_drawer = NULL;
4516 wfree(drawer);
4520 static void removeDrawerCallback(WMenu *menu, WMenuEntry *entry)
4522 WDock *dock = ((WAppIcon*)entry->clientdata)->dock;
4524 /* Parameter not used, but tell the compiler that it is ok */
4525 (void) menu;
4527 assert(dock != NULL);
4529 if (dock->icon_count > 2) {
4530 if (wMessageDialog(dock->screen_ptr, _("Drawer"),
4531 _("All icons in this drawer will be detached!"),
4532 _("OK"), _("Cancel"), NULL) != WAPRDefault)
4533 return;
4535 drawerDestroy(dock);
4539 void wDrawerIconPaint(WAppIcon *dicon)
4541 Window win = dicon->icon->core->window;
4542 WScreen *scr = dicon->icon->core->screen_ptr;
4543 XPoint p[4];
4544 GC gc = scr->draw_gc;
4545 WMColor *color;
4547 wIconPaint(dicon->icon);
4549 if (!dicon->dock->collapsed)
4550 color = scr->clip_title_color[CLIP_NORMAL];
4551 else
4552 color = scr->clip_title_color[CLIP_COLLAPSED];
4553 XSetForeground(dpy, gc, WMColorPixel(color));
4555 if (dicon->dock->on_right_side) {
4556 p[0].x = p[3].x = 10;
4557 p[0].y = p[3].y = ICON_SIZE / 2 - 5;
4558 p[1].x = 10;
4559 p[1].y = ICON_SIZE / 2 + 5;
4560 p[2].x = 5;
4561 p[2].y = ICON_SIZE / 2;
4563 else {
4564 p[0].x = p[3].x = ICON_SIZE-1 - 10;
4565 p[0].y = p[3].y = ICON_SIZE / 2 - 5;
4566 p[1].x = ICON_SIZE-1 - 10;
4567 p[1].y = ICON_SIZE / 2 + 5;
4568 p[2].x = ICON_SIZE-1 - 5;
4569 p[2].y = ICON_SIZE / 2;
4571 XFillPolygon(dpy, win, gc, p,3,Convex,CoordModeOrigin);
4572 XDrawLines(dpy, win, gc, p,4,CoordModeOrigin);
4576 RImage* wDrawerMakeTile(WScreen *scr, RImage *normalTile)
4578 RImage *tile = RCloneImage(normalTile);
4579 RColor dark;
4580 RColor light;
4582 dark.alpha = 0;
4583 dark.red = dark.green = dark.blue = 60;
4585 light.alpha = 0;
4586 light.red = light.green = light.blue = 80;
4588 /* arrow bevel */
4589 if (!scr->dock || scr->dock->on_right_side) {
4590 ROperateLine(tile, RSubtractOperation, 11, ICON_SIZE / 2 - 7,
4591 4, ICON_SIZE / 2, &dark); /* / */
4592 ROperateLine(tile, RSubtractOperation, 11, ICON_SIZE / 2 + 7,
4593 4, ICON_SIZE / 2, &dark); /* \ */
4594 ROperateLine(tile, RAddOperation, 11, ICON_SIZE / 2 - 7,
4595 11, ICON_SIZE / 2 + 7, &light); /* | */
4597 else {
4598 ROperateLine(tile, RSubtractOperation, ICON_SIZE-1 - 11, ICON_SIZE / 2 - 7,
4599 ICON_SIZE-1 - 4, ICON_SIZE / 2, &dark); /* \ */
4600 ROperateLine(tile, RAddOperation, ICON_SIZE-1 - 11, ICON_SIZE / 2 + 7,
4601 ICON_SIZE-1 - 4, ICON_SIZE / 2, &light); /* / */
4602 ROperateLine(tile, RSubtractOperation, ICON_SIZE-1 - 11, ICON_SIZE / 2 - 7,
4603 ICON_SIZE-1 - 11, ICON_SIZE / 2 + 7, &dark); /* | */
4605 return tile;
4609 static void swapDrawer(WDock *drawer, int new_x)
4611 int i;
4613 drawer->on_right_side = drawer->screen_ptr->dock->on_right_side;
4614 drawer->x_pos = new_x;
4616 for (i = 0; i < drawer->max_icons; i++) {
4617 WAppIcon *ai;
4619 ai = drawer->icon_array[i];
4620 if (ai == NULL)
4621 continue;
4622 ai->xindex *= -1; /* so A B C becomes C B A */
4623 ai->x_pos = new_x + ai->xindex * ICON_SIZE;
4625 /* Update drawer's tile */
4626 if (i == 0) {
4627 wIconUpdate(ai->icon);
4628 wDrawerIconPaint(ai);
4630 XMoveWindow(dpy, ai->icon->core->window, ai->x_pos, ai->y_pos);
4635 static void swapDrawers(WScreen *scr, int new_x)
4637 WDrawerChain *dc;
4639 if (scr->drawer_tile)
4640 RReleaseImage(scr->drawer_tile);
4642 scr->drawer_tile = wDrawerMakeTile(scr, scr->icon_tile);
4644 for (dc = scr->drawers; dc != NULL; dc = dc->next)
4645 swapDrawer(dc->adrawer, new_x);
4649 int wIsADrawer(WAppIcon *aicon)
4651 return aicon && aicon->dock &&
4652 aicon->dock->type == WM_DRAWER && aicon->dock->icon_array[0] == aicon;
4656 static WDock* getDrawer(WScreen *scr, int y_index)
4658 WDrawerChain *dc;
4660 for (dc = scr->drawers; dc != NULL; dc = dc->next) {
4661 if (dc->adrawer->y_pos - scr->dock->y_pos == y_index * ICON_SIZE)
4662 return dc->adrawer;
4664 return NULL;
4669 /* Find the "hole" a moving appicon created when snapped into the
4670 * drawer. redocking is a boolean. If the moving appicon comes from the
4671 * drawer, drawer->icon_count is correct. If not, redocking is then false and
4672 * there are now drawer->icon_count plus one appicons in the drawer. */
4673 static int indexOfHole(WDock *drawer, WAppIcon *moving_aicon, int redocking)
4675 int index_of_hole, i;
4677 /* Classic interview question...
4679 * We have n-1 (n = drawer->icon_count-1 or drawer->icon_count, see
4680 * redocking) appicons, whose xindex are unique in [1..n]. One is missing:
4681 * that's where the ghost of the moving appicon is, that's what the
4682 * function should return.
4684 * We compute 1+2+...+n (this sum is equal to n*(n+1)/2), we subtract to
4685 * this sum the xindex of each of the n-1 appicons, and we get the correct
4686 * index! */
4688 if (redocking) {
4689 index_of_hole = (drawer->icon_count - 1) * drawer->icon_count / 2;
4690 } else {
4691 index_of_hole = drawer->icon_count * (drawer->icon_count + 1) / 2;
4693 index_of_hole *= (drawer->on_right_side ? -1 : 1);
4695 for (i = 1; i < drawer->max_icons; i++) {
4696 if (drawer->icon_array[i] && drawer->icon_array[i] != moving_aicon)
4697 index_of_hole -= drawer->icon_array[i]->xindex;
4699 /* wmessage(" Index of the moving appicon is %d (%sredocking)", index_of_hole, (redocking ? "" : "not ")); */
4700 if (abs(index_of_hole) > abs(drawer->icon_count) - (redocking ? 1 : 0))
4701 wwarning(" index_of_hole is too large ! (%d greater than %d)",
4702 index_of_hole, abs(drawer->icon_count) - (redocking ? 1 : 0));
4703 if (index_of_hole == 0)
4704 wwarning(" index_of_hole == 0 (%sredocking, icon_count == %d)", (redocking ? "" : "not "), drawer->icon_count);
4706 return index_of_hole;
4710 void wSlideAppicons(WAppIcon **appicons, int n, int to_the_left)
4712 int i;
4713 int leftmost = -1, min_index = 9999, from_x = -1; // leftmost and from_x initialized to avoid warning
4714 Window wins[n];
4715 WAppIcon *aicon;
4717 if (n < 1)
4718 return;
4720 for (i = 0; i < n; i++) {
4721 aicon = appicons[i];
4722 aicon->xindex += (to_the_left ? -1 : +1);
4723 if (aicon->xindex < min_index) {
4724 min_index = aicon->xindex;
4725 leftmost = i;
4726 from_x = aicon->x_pos;
4728 aicon->x_pos += (to_the_left ? -ICON_SIZE : +ICON_SIZE);
4731 for (i = 0; i < n; i++) {
4732 aicon = appicons[i];
4733 wins[aicon->xindex - min_index] = aicon->icon->core->window;
4735 aicon = appicons[leftmost];
4736 slide_windows(wins, n, from_x, aicon->y_pos, aicon->x_pos, aicon->y_pos);
4740 void wDrawerFillTheGap(WDock *drawer, WAppIcon *aicon, Bool redocking)
4742 int i, j;
4743 int index_of_hole = indexOfHole(drawer, aicon, redocking);
4744 WAppIcon *aicons_to_shift[drawer->icon_count];
4746 j = 0;
4747 for (i = 0; i < drawer->max_icons; i++) {
4748 WAppIcon *ai = drawer->icon_array[i];
4749 if (ai && ai != aicon &&
4750 abs(ai->xindex) > abs(index_of_hole))
4751 aicons_to_shift[j++] = ai;
4753 if (j != drawer->icon_count - abs(index_of_hole) - (redocking ? 1 : 0))
4754 wwarning("Removing aicon at index %d from %s: j=%d but should be %d",
4755 index_of_hole, drawer->icon_array[0]->wm_instance,
4756 j, drawer->icon_count - abs(index_of_hole) - (redocking ? 1 : 0));
4757 wSlideAppicons(aicons_to_shift, j, !drawer->on_right_side);
4761 static void drawerConsolidateIcons(WDock *drawer)
4763 WAppIcon *aicons_to_shift[drawer->icon_count];
4764 int maxRemaining = 0;
4765 int sum = 0;
4766 int i;
4767 for (i = 0; i < drawer->max_icons; i++) {
4768 WAppIcon *ai = drawer->icon_array[i];
4769 if (ai == NULL)
4770 continue;
4771 sum += abs(ai->xindex);
4772 if (abs(ai->xindex) > maxRemaining)
4773 maxRemaining = abs(ai->xindex);
4775 while (sum != maxRemaining * (maxRemaining + 1) / 2) { // while there is a hole
4776 WAppIcon *ai;
4777 int n;
4778 // Look up for the hole at max position
4779 int maxDeleted;
4780 for (maxDeleted = maxRemaining - 1; maxDeleted > 0; maxDeleted--) {
4781 Bool foundAppIconThere = False;
4782 for (i = 0; i < drawer->max_icons; i++) {
4783 WAppIcon *ai = drawer->icon_array[i];
4784 if (ai == NULL)
4785 continue;
4786 if (abs(ai->xindex) == maxDeleted) {
4787 foundAppIconThere = True;
4788 break;
4791 if (!foundAppIconThere)
4792 break;
4794 assert(maxDeleted > 0); // would mean while test is wrong
4795 n = 0;
4796 for (i = 0; i < drawer->max_icons; i++) {
4797 ai = drawer->icon_array[i];
4798 if (ai != NULL && abs(ai->xindex) > maxDeleted)
4799 aicons_to_shift[n++] = ai;
4801 assert(n == maxRemaining - maxDeleted); // for the code review ;-)
4802 wSlideAppicons(aicons_to_shift, n, !drawer->on_right_side);
4803 // Efficient beancounting
4804 maxRemaining -= 1;
4805 sum -= n;
4811 /* similar to wDockRestoreState, but a lot a specific stuff too... */
4812 static WDock * drawerRestoreState(WScreen *scr, WMPropList *drawer_state)
4814 WDock *drawer;
4815 WMPropList *apps, *value, *dock_state;
4816 WAppIcon *aicon;
4817 int count, i;
4819 if (!drawer_state)
4820 return NULL;
4822 make_keys();
4824 WMRetainPropList(drawer_state);
4826 /* Get the instance name, and create a drawer */
4827 value = WMGetFromPLDictionary(drawer_state, dName);
4828 drawer = wDockCreate(scr, WM_DRAWER, WMGetFromPLString(value));
4830 /* restore DnD command and paste command */
4831 #ifdef USE_DOCK_XDND
4832 value = WMGetFromPLDictionary(drawer_state, dDropCommand);
4833 if (value && WMIsPLString(value))
4834 drawer->icon_array[0]->dnd_command = wstrdup(WMGetFromPLString(value));
4835 #endif /* USE_DOCK_XDND */
4837 value = WMGetFromPLDictionary(drawer_state, dPasteCommand);
4838 if (value && WMIsPLString(value))
4839 drawer->icon_array[0]->paste_command = wstrdup(WMGetFromPLString(value));
4841 /* restore position */
4842 value = WMGetFromPLDictionary(drawer_state, dPosition);
4843 if (!value || !WMIsPLString(value))
4844 COMPLAIN("Position");
4845 else {
4846 int x, y, y_index;
4847 if (sscanf(WMGetFromPLString(value), "%i,%i", &x, &y) != 2)
4848 COMPLAIN("Position");
4850 /* check position sanity */
4851 if (x != scr->dock->x_pos) {
4852 x = scr->dock->x_pos;
4854 y_index = (y - scr->dock->y_pos) / ICON_SIZE;
4855 if (y_index >= scr->dock->max_icons) {
4856 /* Here we should do something more intelligent, since it
4857 * can happen even if the user hasn't hand-edited his
4858 * G/D/State file (but uses a lower resolution). */
4859 y_index = scr->dock->max_icons - 1;
4861 y = scr->dock->y_pos + y_index * ICON_SIZE;
4862 moveDock(drawer, x, y);
4865 /* restore dock properties (applist and others) */
4866 dock_state = WMGetFromPLDictionary(drawer_state, dDock);
4868 /* restore lowered/raised state: same as scr->dock, no matter what */
4869 drawer->lowered = scr->dock->lowered;
4870 if (!drawer->lowered)
4871 ChangeStackingLevel(drawer->icon_array[0]->icon->core, WMDockLevel);
4872 else
4873 ChangeStackingLevel(drawer->icon_array[0]->icon->core, WMNormalLevel);
4874 wRaiseFrame(drawer->icon_array[0]->icon->core);
4876 /* restore collapsed state */
4877 drawer->collapsed = 0;
4878 value = WMGetFromPLDictionary(dock_state, dCollapsed);
4879 if (value && strcasecmp(WMGetFromPLString(value), "YES") == 0) {
4880 drawer->collapsed = 1;
4882 /* restore auto-collapsed state */
4883 value = WMGetFromPLDictionary(dock_state, dAutoCollapse);
4884 if (value && strcasecmp(WMGetFromPLString(value), "YES") == 0) {
4885 drawer->auto_collapse = 1;
4886 drawer->collapsed = 1;
4887 } else {
4888 drawer->auto_collapse = 0; // because wDockCreate sets it (drawers only)
4891 /* restore auto-raise/lower state: same as scr->dock, no matter what */
4892 drawer->auto_raise_lower = scr->dock->auto_raise_lower;
4894 /* restore attract icons state */
4895 drawer->attract_icons = 0;
4896 value = WMGetFromPLDictionary(dock_state, dAutoAttractIcons);
4897 if (value && strcasecmp(WMGetFromPLString(value), "YES") == 0) {
4898 drawer->attract_icons = 1;
4899 scr->attracting_drawer = drawer;
4902 /* application list */
4903 apps = WMGetFromPLDictionary(dock_state, dApplications);
4905 if (!apps) {
4906 goto finish;
4909 count = WMGetPropListItemCount(apps);
4911 if (count == 0)
4912 goto finish;
4914 for (i=0; i<count; i++) {
4915 if (drawer->icon_count >= drawer->max_icons) {
4916 wwarning(_("there are too many icons stored in drawer. Ignoring what doesn't fit"));
4917 break;
4920 value = WMGetFromPLArray(apps, i);
4921 aicon = restore_icon_state(scr, value, WM_DRAWER, drawer->icon_count);
4923 drawer->icon_array[drawer->icon_count] = aicon;
4925 if (aicon) {
4926 aicon->dock = drawer;
4927 aicon->x_pos = drawer->x_pos + (aicon->xindex * ICON_SIZE);
4928 aicon->y_pos = drawer->y_pos + (aicon->yindex * ICON_SIZE);
4930 if (!drawer->lowered)
4931 ChangeStackingLevel(aicon->icon->core, WMDockLevel);
4932 else
4933 ChangeStackingLevel(aicon->icon->core, WMNormalLevel);
4935 wCoreConfigure(aicon->icon->core, aicon->x_pos, aicon->y_pos, 0, 0);
4937 if (!drawer->collapsed)
4938 XMapWindow(dpy, aicon->icon->core->window);
4939 wRaiseFrame(aicon->icon->core);
4941 drawer->icon_count++;
4945 finish:
4946 WMReleasePropList(drawer_state);
4948 return drawer;
4952 /* Same kind of comment than for previous function: this function is
4953 * very similar to make_icon_state, but has substential differences as
4954 * well. */
4955 static WMPropList *drawerSaveState(WDock *drawer)
4957 WMPropList *pstr, *drawer_state;
4958 WAppIcon *ai;
4959 char buffer[64];
4961 ai = drawer->icon_array[0];
4962 /* Store its name */
4963 pstr = WMCreatePLString(ai->wm_instance);
4964 drawer_state = WMCreatePLDictionary(dName, pstr, NULL); /* we need this final NULL */
4965 WMReleasePropList(pstr);
4967 /* Store its position */
4968 snprintf(buffer, sizeof(buffer), "%i,%i", ai->x_pos, ai->y_pos);
4969 pstr = WMCreatePLString(buffer);
4970 WMPutInPLDictionary(drawer_state, dPosition, pstr);
4971 WMReleasePropList(pstr);
4973 #ifdef USE_DOCK_XDND
4974 /* Store its DnD command */
4975 if (ai->dnd_command) {
4976 pstr = WMCreatePLString(ai->dnd_command);
4977 WMPutInPLDictionary(drawer_state, dDropCommand, pstr);
4978 WMReleasePropList(pstr);
4980 #endif /* USE_DOCK_XDND */
4982 /* Store its paste command */
4983 if (ai->paste_command) {
4984 pstr = WMCreatePLString(ai->paste_command);
4985 WMPutInPLDictionary(drawer_state, dPasteCommand, pstr);
4986 WMReleasePropList(pstr);
4989 /* Store applications list and other properties */
4990 pstr = dockSaveState(drawer);
4991 WMPutInPLDictionary(drawer_state, dDock, pstr);
4992 WMReleasePropList(pstr);
4994 return drawer_state;
4998 void wDrawersSaveState(WScreen *scr)
5000 WMPropList *all_drawers, *drawer_state;
5001 int i;
5002 WDrawerChain *dc;
5004 make_keys();
5006 all_drawers = WMCreatePLArray(NULL);
5007 for (i=0, dc = scr->drawers;
5008 i < scr->drawer_count;
5009 i++, dc = dc->next) {
5010 drawer_state = drawerSaveState(dc->adrawer);
5011 WMAddToPLArray(all_drawers, drawer_state);
5012 WMReleasePropList(drawer_state);
5014 WMPutInPLDictionary(scr->session_state, dDrawers, all_drawers);
5015 WMReleasePropList(all_drawers);
5019 void wDrawersRestoreState(WScreen *scr)
5021 WMPropList *all_drawers, *drawer_state;
5022 int i;
5024 make_keys();
5026 if (scr->session_state == NULL)
5027 return;
5029 all_drawers = WMGetFromPLDictionary(scr->session_state, dDrawers);
5030 if (!all_drawers)
5031 return;
5033 for (i = 0; i < WMGetPropListItemCount(all_drawers); i++) {
5034 drawer_state = WMGetFromPLArray(all_drawers, i);
5035 drawerRestoreState(scr, drawer_state);
5036 // Note: scr->drawers was updated when the the drawer was created