Update Serbian translation from master branch
[wmaker-crm.git] / src / dock.c
blob45282057ca462a9c68f604e2cabb3a317f981e9e
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
7 * Copyright (c) 2009-2023 Window Maker Team
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, write to the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24 #include "wconfig.h"
26 #include <X11/Xlib.h>
27 #include <X11/Xutil.h>
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <libgen.h>
31 #include <string.h>
32 #include <strings.h>
33 #include <unistd.h>
34 #include <math.h>
35 #include <limits.h>
37 #ifndef PATH_MAX
38 #define PATH_MAX DEFAULT_PATH_MAX
39 #endif
41 #include "WindowMaker.h"
42 #include "wcore.h"
43 #include "window.h"
44 #include "icon.h"
45 #include "appicon.h"
46 #include "actions.h"
47 #include "stacking.h"
48 #include "dock.h"
49 #include "dockedapp.h"
50 #include "dialog.h"
51 #include "main.h"
52 #include "properties.h"
53 #include "menu.h"
54 #include "client.h"
55 #include "defaults.h"
56 #include "workspace.h"
57 #include "framewin.h"
58 #include "superfluous.h"
59 #include "xinerama.h"
60 #include "placement.h"
61 #include "misc.h"
62 #include "event.h"
64 /**** Local variables ****/
65 #define CLIP_REWIND 1
66 #define CLIP_IDLE 0
67 #define CLIP_FORWARD 2
69 #define MOD_MASK wPreferences.modifier_mask
70 #define ICON_SIZE wPreferences.icon_size
72 /***** Local variables ****/
74 static WMPropList *dCommand = NULL;
75 static WMPropList *dPasteCommand = NULL;
76 #ifdef USE_DOCK_XDND
77 static WMPropList *dDropCommand = NULL;
78 #endif
79 static WMPropList *dAutoLaunch, *dLock;
80 static WMPropList *dName, *dForced, *dBuggyApplication, *dYes, *dNo;
81 static WMPropList *dHost, *dDock, *dClip;
82 static WMPropList *dAutoAttractIcons;
84 static WMPropList *dPosition, *dApplications, *dLowered, *dCollapsed;
86 static WMPropList *dAutoCollapse, *dAutoRaiseLower, *dOmnipresent;
88 static WMPropList *dDrawers = NULL;
90 static void dockIconPaint(WAppIcon *btn);
92 static void iconMouseDown(WObjDescriptor *desc, XEvent *event);
94 static pid_t execCommand(WAppIcon *btn, const char *command, WSavedState *state);
96 static void trackDeadProcess(pid_t pid, unsigned int status, void *cdata);
98 static int getClipButton(int px, int py);
100 static void toggleLowered(WDock *dock);
102 static void toggleCollapsed(WDock *dock);
104 static void clipIconExpose(WObjDescriptor *desc, XEvent *event);
106 static void clipLeave(WDock *dock);
108 static void handleClipChangeWorkspace(WScreen *scr, XEvent *event);
110 static void clipEnterNotify(WObjDescriptor *desc, XEvent *event);
111 static void clipLeaveNotify(WObjDescriptor *desc, XEvent *event);
112 static void clipAutoCollapse(void *cdata);
113 static void clipAutoExpand(void *cdata);
114 static void launchDockedApplication(WAppIcon *btn, Bool withSelection);
116 static void clipAutoLower(void *cdata);
117 static void clipAutoRaise(void *cdata);
118 static WAppIcon *mainIconCreate(WScreen *scr, int type, const char *name);
120 static void drawerIconExpose(WObjDescriptor *desc, XEvent *event);
121 static void removeDrawerCallback(WMenu *menu, WMenuEntry *entry);
122 static void drawerAppendToChain(WScreen *scr, WDock *drawer);
123 static char *findUniqueName(WScreen *scr, const char *instance_basename);
124 static void addADrawerCallback(WMenu *menu, WMenuEntry *entry);
125 static void swapDrawers(WScreen *scr, int new_x);
126 static WDock* getDrawer(WScreen *scr, int y_index);
127 static int indexOfHole(WDock *drawer, WAppIcon *moving_aicon, int redocking);
128 static void drawerConsolidateIcons(WDock *drawer);
130 static int onScreen(WScreen *scr, int x, int y);
132 static void make_keys(void)
134 if (dCommand != NULL)
135 return;
137 dCommand = WMRetainPropList(WMCreatePLString("Command"));
138 dPasteCommand = WMRetainPropList(WMCreatePLString("PasteCommand"));
139 #ifdef USE_DOCK_XDND
140 dDropCommand = WMRetainPropList(WMCreatePLString("DropCommand"));
141 #endif
142 dLock = WMRetainPropList(WMCreatePLString("Lock"));
143 dAutoLaunch = WMRetainPropList(WMCreatePLString("AutoLaunch"));
144 dName = WMRetainPropList(WMCreatePLString("Name"));
145 dForced = WMRetainPropList(WMCreatePLString("Forced"));
146 dBuggyApplication = WMRetainPropList(WMCreatePLString("BuggyApplication"));
147 dYes = WMRetainPropList(WMCreatePLString("Yes"));
148 dNo = WMRetainPropList(WMCreatePLString("No"));
149 dHost = WMRetainPropList(WMCreatePLString("Host"));
151 dPosition = WMCreatePLString("Position");
152 dApplications = WMCreatePLString("Applications");
153 dLowered = WMCreatePLString("Lowered");
154 dCollapsed = WMCreatePLString("Collapsed");
155 dAutoCollapse = WMCreatePLString("AutoCollapse");
156 dAutoRaiseLower = WMCreatePLString("AutoRaiseLower");
157 dAutoAttractIcons = WMCreatePLString("AutoAttractIcons");
159 dOmnipresent = WMCreatePLString("Omnipresent");
161 dDock = WMCreatePLString("Dock");
162 dClip = WMCreatePLString("Clip");
163 dDrawers = WMCreatePLString("Drawers");
166 static void renameCallback(WMenu *menu, WMenuEntry *entry)
168 WDock *dock = entry->clientdata;
169 char buffer[128];
170 int wspace;
171 char *name;
173 /* Parameter not used, but tell the compiler that it is ok */
174 (void) menu;
176 assert(entry->clientdata != NULL);
178 wspace = dock->screen_ptr->current_workspace;
180 name = wstrdup(dock->screen_ptr->workspaces[wspace]->name);
182 snprintf(buffer, sizeof(buffer), _("Type the name for workspace %i:"), wspace + 1);
183 if (wInputDialog(dock->screen_ptr, _("Rename Workspace"), buffer, &name))
184 wWorkspaceRename(dock->screen_ptr, wspace, name);
186 wfree(name);
189 static void toggleLoweredCallback(WMenu *menu, WMenuEntry *entry)
191 assert(entry->clientdata != NULL);
193 toggleLowered(entry->clientdata);
195 entry->flags.indicator_on = !(((WDock *) entry->clientdata)->lowered);
197 wMenuPaint(menu);
200 static int matchWindow(const void *item, const void *cdata)
202 return (((WFakeGroupLeader *) item)->leader == (Window) cdata);
205 static void killCallback(WMenu *menu, WMenuEntry *entry)
207 WScreen *scr = menu->menu->screen_ptr;
208 WAppIcon *icon;
209 WFakeGroupLeader *fPtr;
210 char *buffer, *shortname, **argv;
211 int argc;
213 if (!WCHECK_STATE(WSTATE_NORMAL))
214 return;
216 assert(entry->clientdata != NULL);
218 icon = (WAppIcon *) entry->clientdata;
220 icon->editing = 1;
222 WCHANGE_STATE(WSTATE_MODAL);
224 /* strip away dir names */
225 shortname = basename(icon->command);
226 /* separate out command options */
227 wtokensplit(shortname, &argv, &argc);
229 buffer = wstrconcat(argv[0],
230 _(" will be forcibly closed.\n"
231 "Any unsaved changes will be lost.\n" "Please confirm."));
233 if (icon->icon && icon->icon->owner) {
234 fPtr = icon->icon->owner->fake_group;
235 } else {
236 /* is this really necessary? can we kill a non-running dock icon? */
237 Window win = icon->main_window;
238 int index;
240 index = WMFindInArray(scr->fakeGroupLeaders, matchWindow, (void *)win);
241 if (index != WANotFound)
242 fPtr = WMGetFromArray(scr->fakeGroupLeaders, index);
243 else
244 fPtr = NULL;
247 if (wPreferences.dont_confirm_kill
248 || wMessageDialog(menu->frame->screen_ptr, _("Kill Application"),
249 buffer, _("Yes"), _("No"), NULL) == WAPRDefault) {
250 if (fPtr != NULL) {
251 WWindow *wwin, *twin;
253 wwin = scr->focused_window;
254 while (wwin) {
255 twin = wwin->prev;
256 if (wwin->fake_group == fPtr)
257 wClientKill(wwin);
259 wwin = twin;
261 } else if (icon->icon && icon->icon->owner) {
262 wClientKill(icon->icon->owner);
266 wfree(buffer);
267 wtokenfree(argv, argc);
269 icon->editing = 0;
271 WCHANGE_STATE(WSTATE_NORMAL);
274 /* TODO: replace this function with a member of the dock struct */
275 static int numberOfSelectedIcons(WDock *dock)
277 WAppIcon *aicon;
278 int i, n;
280 n = 0;
281 for (i = 1; i < dock->max_icons; i++) {
282 aicon = dock->icon_array[i];
283 if (aicon && aicon->icon->selected)
284 n++;
287 return n;
290 static WMArray *getSelected(WDock *dock)
292 WMArray *ret = WMCreateArray(8);
293 WAppIcon *btn;
294 int i;
296 for (i = 1; i < dock->max_icons; i++) {
297 btn = dock->icon_array[i];
298 if (btn && btn->icon->selected)
299 WMAddToArray(ret, btn);
302 return ret;
305 static void paintClipButtons(WAppIcon *clipIcon, Bool lpushed, Bool rpushed)
307 Window win = clipIcon->icon->core->window;
308 WScreen *scr = clipIcon->icon->core->screen_ptr;
309 XPoint p[4];
310 int pt = CLIP_BUTTON_SIZE * ICON_SIZE / 64;
311 int tp = ICON_SIZE - pt;
312 int as = pt - 15; /* 15 = 5+5+5 */
313 GC gc = scr->draw_gc; /* maybe use WMColorGC() instead here? */
314 WMColor *color;
316 color = scr->clip_title_color[CLIP_NORMAL];
318 XSetForeground(dpy, gc, WMColorPixel(color));
320 if (rpushed) {
321 p[0].x = tp + 1;
322 p[0].y = 1;
323 p[1].x = ICON_SIZE - 2;
324 p[1].y = 1;
325 p[2].x = ICON_SIZE - 2;
326 p[2].y = pt - 1;
327 } else if (lpushed) {
328 p[0].x = 1;
329 p[0].y = tp;
330 p[1].x = pt;
331 p[1].y = ICON_SIZE - 2;
332 p[2].x = 1;
333 p[2].y = ICON_SIZE - 2;
335 if (lpushed || rpushed) {
336 XSetForeground(dpy, scr->draw_gc, scr->white_pixel);
337 XFillPolygon(dpy, win, scr->draw_gc, p, 3, Convex, CoordModeOrigin);
338 XSetForeground(dpy, scr->draw_gc, scr->black_pixel);
341 /* top right arrow */
342 p[0].x = p[3].x = ICON_SIZE - 5 - as;
343 p[0].y = p[3].y = 5;
344 p[1].x = ICON_SIZE - 6;
345 p[1].y = 5;
346 p[2].x = ICON_SIZE - 6;
347 p[2].y = 4 + as;
348 if (rpushed) {
349 XFillPolygon(dpy, win, scr->draw_gc, p, 3, Convex, CoordModeOrigin);
350 XDrawLines(dpy, win, scr->draw_gc, p, 4, CoordModeOrigin);
351 } else {
352 XFillPolygon(dpy, win, gc, p, 3, Convex, CoordModeOrigin);
353 XDrawLines(dpy, win, gc, p, 4, CoordModeOrigin);
356 /* bottom left arrow */
357 p[0].x = p[3].x = 5;
358 p[0].y = p[3].y = ICON_SIZE - 5 - as;
359 p[1].x = 5;
360 p[1].y = ICON_SIZE - 6;
361 p[2].x = 4 + as;
362 p[2].y = ICON_SIZE - 6;
363 if (lpushed) {
364 XFillPolygon(dpy, win, scr->draw_gc, p, 3, Convex, CoordModeOrigin);
365 XDrawLines(dpy, win, scr->draw_gc, p, 4, CoordModeOrigin);
366 } else {
367 XFillPolygon(dpy, win, gc, p, 3, Convex, CoordModeOrigin);
368 XDrawLines(dpy, win, gc, p, 4, CoordModeOrigin);
372 RImage *wClipMakeTile(RImage *normalTile)
374 RImage *tile = RCloneImage(normalTile);
375 RColor black;
376 RColor dark;
377 RColor light;
378 int pt, tp;
379 int as;
381 pt = CLIP_BUTTON_SIZE * wPreferences.icon_size / 64;
382 tp = wPreferences.icon_size - 1 - pt;
383 as = pt - 15;
385 black.alpha = 255;
386 black.red = black.green = black.blue = 0;
388 dark.alpha = 0;
389 dark.red = dark.green = dark.blue = 60;
391 light.alpha = 0;
392 light.red = light.green = light.blue = 80;
394 /* top right */
395 ROperateLine(tile, RSubtractOperation, tp, 0, wPreferences.icon_size - 2, pt - 1, &dark);
396 RDrawLine(tile, tp - 1, 0, wPreferences.icon_size - 1, pt + 1, &black);
397 ROperateLine(tile, RAddOperation, tp, 2, wPreferences.icon_size - 3, pt, &light);
399 /* arrow bevel */
400 ROperateLine(tile, RSubtractOperation, ICON_SIZE - 7 - as, 4, ICON_SIZE - 5, 4, &dark);
401 ROperateLine(tile, RSubtractOperation, ICON_SIZE - 6 - as, 5, ICON_SIZE - 5, 6 + as, &dark);
402 ROperateLine(tile, RAddOperation, ICON_SIZE - 5, 4, ICON_SIZE - 5, 6 + as, &light);
404 /* bottom left */
405 ROperateLine(tile, RAddOperation, 2, tp + 2, pt - 2, wPreferences.icon_size - 3, &dark);
406 RDrawLine(tile, 0, tp - 1, pt + 1, wPreferences.icon_size - 1, &black);
407 ROperateLine(tile, RSubtractOperation, 0, tp - 2, pt + 1, wPreferences.icon_size - 2, &light);
409 /* arrow bevel */
410 ROperateLine(tile, RSubtractOperation, 4, ICON_SIZE - 7 - as, 4, ICON_SIZE - 5, &dark);
411 ROperateLine(tile, RSubtractOperation, 5, ICON_SIZE - 6 - as, 6 + as, ICON_SIZE - 5, &dark);
412 ROperateLine(tile, RAddOperation, 4, ICON_SIZE - 5, 6 + as, ICON_SIZE - 5, &light);
414 return tile;
417 static void omnipresentCallback(WMenu *menu, WMenuEntry *entry)
419 WAppIcon *clickedIcon = entry->clientdata;
420 WAppIcon *aicon;
421 WDock *dock;
422 WMArray *selectedIcons;
423 WMArrayIterator iter;
424 int failed;
426 /* Parameter not used, but tell the compiler that it is ok */
427 (void) menu;
429 assert(entry->clientdata != NULL);
431 dock = clickedIcon->dock;
433 selectedIcons = getSelected(dock);
435 if (!WMGetArrayItemCount(selectedIcons))
436 WMAddToArray(selectedIcons, clickedIcon);
438 failed = 0;
439 WM_ITERATE_ARRAY(selectedIcons, aicon, iter) {
440 if (wClipMakeIconOmnipresent(aicon, !aicon->omnipresent) == WO_FAILED)
441 failed++;
442 else if (aicon->icon->selected)
443 wIconSelect(aicon->icon);
445 WMFreeArray(selectedIcons);
447 if (failed > 1) {
448 wMessageDialog(dock->screen_ptr, _("Warning"),
449 _("Some icons cannot be made omnipresent. "
450 "Please make sure that no other icon is "
451 "docked in the same positions on the other "
452 "workspaces and the Clip is not full in "
453 "some workspace."), _("OK"), NULL, NULL);
454 } else if (failed == 1) {
455 wMessageDialog(dock->screen_ptr, _("Warning"),
456 _("Icon cannot be made omnipresent. "
457 "Please make sure that no other icon is "
458 "docked in the same position on the other "
459 "workspaces and the Clip is not full in "
460 "some workspace."), _("OK"), NULL, NULL);
464 static void removeIcons(WMArray *icons, WDock *dock)
466 WAppIcon *aicon;
467 int keepit;
468 WMArrayIterator it;
470 WM_ITERATE_ARRAY(icons, aicon, it) {
471 keepit = aicon->running && wApplicationOf(aicon->main_window);
472 wDockDetach(dock, aicon);
473 if (keepit) {
474 /* XXX: can: aicon->icon == NULL ? */
475 PlaceIcon(dock->screen_ptr, &aicon->x_pos, &aicon->y_pos,
476 wGetHeadForWindow(aicon->icon->owner));
477 XMoveWindow(dpy, aicon->icon->core->window, aicon->x_pos, aicon->y_pos);
478 if (!dock->mapped || dock->collapsed)
479 XMapWindow(dpy, aicon->icon->core->window);
482 WMFreeArray(icons);
484 if (wPreferences.auto_arrange_icons)
485 wArrangeIcons(dock->screen_ptr, True);
488 static void removeIconsCallback(WMenu *menu, WMenuEntry *entry)
490 WAppIcon *clickedIcon = (WAppIcon *) entry->clientdata;
491 WDock *dock;
492 WMArray *selectedIcons;
494 /* Parameter not used, but tell the compiler that it is ok */
495 (void) menu;
497 assert(clickedIcon != NULL);
499 dock = clickedIcon->dock;
501 selectedIcons = getSelected(dock);
503 if (WMGetArrayItemCount(selectedIcons)) {
504 if (wMessageDialog(dock->screen_ptr,
505 dock->type == WM_CLIP ? _("Workspace Clip") : _("Drawer"),
506 _("All selected icons will be removed!"),
507 _("OK"), _("Cancel"), NULL) != WAPRDefault) {
508 WMFreeArray(selectedIcons);
509 return;
511 } else {
512 if (clickedIcon->xindex == 0 && clickedIcon->yindex == 0) {
513 WMFreeArray(selectedIcons);
514 return;
516 WMAddToArray(selectedIcons, clickedIcon);
519 removeIcons(selectedIcons, dock);
521 if (dock->type == WM_DRAWER) {
522 drawerConsolidateIcons(dock);
526 static void keepIconsCallback(WMenu *menu, WMenuEntry *entry)
528 WAppIcon *clickedIcon = (WAppIcon *) entry->clientdata;
529 WDock *dock;
530 WAppIcon *aicon;
531 WMArray *selectedIcons;
532 WMArrayIterator it;
534 /* Parameter not used, but tell the compiler that it is ok */
535 (void) menu;
537 assert(clickedIcon != NULL);
538 dock = clickedIcon->dock;
540 selectedIcons = getSelected(dock);
542 if (!WMGetArrayItemCount(selectedIcons)
543 && clickedIcon != dock->screen_ptr->clip_icon) {
544 char *command = NULL;
546 if (!clickedIcon->command && !clickedIcon->editing) {
547 clickedIcon->editing = 1;
548 if (wInputDialog(dock->screen_ptr, _("Keep Icon"),
549 _("Type the command used to launch the application"), &command)) {
550 if (command && (command[0] == 0 || (command[0] == '-' && command[1] == 0))) {
551 wfree(command);
552 command = NULL;
554 clickedIcon->command = command;
555 clickedIcon->editing = 0;
556 } else {
557 clickedIcon->editing = 0;
558 if (command)
559 wfree(command);
560 WMFreeArray(selectedIcons);
561 return;
565 WMAddToArray(selectedIcons, clickedIcon);
568 WM_ITERATE_ARRAY(selectedIcons, aicon, it) {
569 if (aicon->icon->selected)
570 wIconSelect(aicon->icon);
572 if (aicon->attracted && aicon->command) {
573 aicon->attracted = 0;
574 if (aicon->icon->shadowed) {
575 aicon->icon->shadowed = 0;
578 * Update icon pixmap, RImage doesn't change,
579 * so call wIconUpdate is not needed
581 update_icon_pixmap(aicon->icon);
583 /* Paint it */
584 wAppIconPaint(aicon);
587 save_appicon(aicon);
589 WMFreeArray(selectedIcons);
592 static void toggleAutoAttractCallback(WMenu *menu, WMenuEntry *entry)
594 WDock *dock = (WDock *) entry->clientdata;
595 WScreen *scr = dock->screen_ptr;
597 assert(entry->clientdata != NULL);
599 dock->attract_icons = !dock->attract_icons;
601 entry->flags.indicator_on = dock->attract_icons;
603 wMenuPaint(menu);
605 if (dock->attract_icons) {
606 if (dock->type == WM_DRAWER) {
607 /* The newly auto-attracting dock is a drawer: disable any clip and
608 * previously attracting drawer */
610 if (!wPreferences.flags.noclip) {
611 int i;
612 for (i = 0; i < scr->workspace_count; i++)
613 scr->workspaces[ i ]->clip->attract_icons = False;
614 /* dock menu will be updated later, when opened */
617 if (scr->attracting_drawer != NULL)
618 scr->attracting_drawer->attract_icons = False;
619 scr->attracting_drawer = dock;
620 } else {
621 /* The newly auto-attracting dock is a clip: disable
622 * previously attracting drawer, if applicable */
623 if (scr->attracting_drawer != NULL) {
624 scr->attracting_drawer->attract_icons = False;
625 /* again, its menu will be updated, later. */
626 scr->attracting_drawer = NULL;
632 static void selectCallback(WMenu *menu, WMenuEntry *entry)
634 WAppIcon *icon = (WAppIcon *) entry->clientdata;
636 assert(icon != NULL);
638 wIconSelect(icon->icon);
640 wMenuPaint(menu);
643 static void attractIconsCallback(WMenu *menu, WMenuEntry *entry)
645 WAppIcon *clickedIcon = (WAppIcon *) entry->clientdata;
646 WDock *clip; /* clip... is a WM_CLIP or a WM_DRAWER */
647 WAppIcon *aicon;
648 int x, y, x_pos, y_pos;
649 Bool update_icon = False;
651 /* Parameter not used, but tell the compiler that it is ok */
652 (void) menu;
654 assert(entry->clientdata != NULL);
655 clip = clickedIcon->dock;
657 aicon = clip->screen_ptr->app_icon_list;
659 while (aicon) {
660 if (!aicon->docked && wDockFindFreeSlot(clip, &x, &y)) {
661 x_pos = clip->x_pos + x * ICON_SIZE;
662 y_pos = clip->y_pos + y * ICON_SIZE;
663 if (aicon->x_pos != x_pos || aicon->y_pos != y_pos)
664 move_window(aicon->icon->core->window, aicon->x_pos, aicon->y_pos, x_pos, y_pos);
666 aicon->attracted = 1;
667 if (!aicon->icon->shadowed) {
668 aicon->icon->shadowed = 1;
669 update_icon = True;
671 wDockAttachIcon(clip, aicon, x, y, update_icon);
672 if (clip->collapsed || !clip->mapped)
673 XUnmapWindow(dpy, aicon->icon->core->window);
675 aicon = aicon->next;
679 static void selectIconsCallback(WMenu *menu, WMenuEntry *entry)
681 WAppIcon *clickedIcon = (WAppIcon *) entry->clientdata;
682 WDock *dock;
683 WMArray *selectedIcons;
684 WMArrayIterator iter;
685 WAppIcon *btn;
686 int i;
688 assert(clickedIcon != NULL);
689 dock = clickedIcon->dock;
691 selectedIcons = getSelected(dock);
693 if (!WMGetArrayItemCount(selectedIcons)) {
694 for (i = 1; i < dock->max_icons; i++) {
695 btn = dock->icon_array[i];
696 if (btn && !btn->icon->selected)
697 wIconSelect(btn->icon);
699 } else {
700 WM_ITERATE_ARRAY(selectedIcons, btn, iter) {
701 wIconSelect(btn->icon);
704 WMFreeArray(selectedIcons);
706 wMenuPaint(menu);
709 static void toggleCollapsedCallback(WMenu *menu, WMenuEntry *entry)
711 assert(entry->clientdata != NULL);
713 toggleCollapsed(entry->clientdata);
715 entry->flags.indicator_on = ((WDock *) entry->clientdata)->collapsed;
717 wMenuPaint(menu);
720 static void toggleAutoCollapseCallback(WMenu *menu, WMenuEntry *entry)
722 WDock *dock;
723 assert(entry->clientdata != NULL);
725 dock = (WDock *) entry->clientdata;
727 dock->auto_collapse = !dock->auto_collapse;
729 entry->flags.indicator_on = ((WDock *) entry->clientdata)->auto_collapse;
731 wMenuPaint(menu);
734 static void toggleAutoRaiseLower(WDock *dock)
736 WDrawerChain *dc;
738 dock->auto_raise_lower = !dock->auto_raise_lower;
739 if (dock->type == WM_DOCK)
741 for (dc = dock->screen_ptr->drawers; dc != NULL; dc = dc->next) {
742 toggleAutoRaiseLower(dc->adrawer);
747 static void toggleAutoRaiseLowerCallback(WMenu *menu, WMenuEntry *entry)
749 WDock *dock;
751 /* Parameter not used, but tell the compiler that it is ok */
752 (void) menu;
754 assert(entry->clientdata != NULL);
756 dock = (WDock *) entry->clientdata;
758 toggleAutoRaiseLower(dock);
760 entry->flags.indicator_on = ((WDock *) entry->clientdata)->auto_raise_lower;
762 wMenuPaint(menu);
765 static void launchCallback(WMenu *menu, WMenuEntry *entry)
767 WAppIcon *btn = (WAppIcon *) entry->clientdata;
769 /* Parameter not used, but tell the compiler that it is ok */
770 (void) menu;
772 launchDockedApplication(btn, False);
775 static void settingsCallback(WMenu *menu, WMenuEntry *entry)
777 WAppIcon *btn = (WAppIcon *) entry->clientdata;
779 /* Parameter not used, but tell the compiler that it is ok */
780 (void) menu;
782 if (btn->editing)
783 return;
784 ShowDockAppSettingsPanel(btn);
787 static void hideCallback(WMenu *menu, WMenuEntry *entry)
789 WApplication *wapp;
790 WAppIcon *btn = (WAppIcon *) entry->clientdata;
792 /* Parameter not used, but tell the compiler that it is ok */
793 (void) menu;
795 wapp = wApplicationOf(btn->icon->owner->main_window);
797 if (wapp->flags.hidden) {
798 wWorkspaceChange(btn->icon->core->screen_ptr, wapp->last_workspace);
799 wUnhideApplication(wapp, False, False);
800 } else {
801 wHideApplication(wapp);
805 static void unhideHereCallback(WMenu *menu, WMenuEntry *entry)
807 WApplication *wapp;
808 WAppIcon *btn = (WAppIcon *) entry->clientdata;
810 /* Parameter not used, but tell the compiler that it is ok */
811 (void) menu;
813 wapp = wApplicationOf(btn->icon->owner->main_window);
815 wUnhideApplication(wapp, False, True);
818 static int getDockXPosition(WScreen *scr, Bool on_right_side)
820 int x;
822 if (wPreferences.keep_dock_on_primary_head) {
823 WMRect rect;
825 rect = wGetRectForHead(scr, scr->xine_info.primary_head);
826 x = rect.pos.x;
827 if (on_right_side)
828 x += rect.size.width - ICON_SIZE - DOCK_EXTRA_SPACE;
829 else
830 x += DOCK_EXTRA_SPACE;
831 } else {
832 if (on_right_side)
833 x = scr->scr_width - ICON_SIZE - DOCK_EXTRA_SPACE;
834 else
835 x = DOCK_EXTRA_SPACE;
838 return x;
841 /* Name is only used when type == WM_DRAWER and when restoring a specific
842 * drawer, with a specific name. When creating a drawer, leave name to NULL
843 * and mainIconCreate will find the first unused unique name */
844 static WAppIcon *mainIconCreate(WScreen *scr, int type, const char *name)
846 WAppIcon *btn;
847 int x_pos;
849 switch(type) {
850 case WM_CLIP:
851 if (scr->clip_icon)
852 return scr->clip_icon;
854 btn = wAppIconCreateForDock(scr, NULL, "Logo", "WMClip", TILE_CLIP);
855 btn->icon->core->descriptor.handle_expose = clipIconExpose;
856 x_pos = 0;
857 break;
858 case WM_DOCK:
859 default: /* to avoid a warning about btn and x_pos, basically */
860 btn = wAppIconCreateForDock(scr, NULL, "Logo", "WMDock", TILE_NORMAL);
861 if (wPreferences.flags.clip_merged_in_dock)
862 btn->icon->core->descriptor.handle_expose = clipIconExpose;
863 x_pos = getDockXPosition(scr, True);
864 break;
865 case WM_DRAWER:
866 if (name == NULL)
867 name = findUniqueName(scr, "Drawer");
868 btn = wAppIconCreateForDock(scr, NULL, name, "WMDrawer", TILE_DRAWER);
869 btn->icon->core->descriptor.handle_expose = drawerIconExpose;
870 x_pos = 0;
873 btn->xindex = 0;
874 btn->yindex = 0;
876 btn->icon->core->descriptor.handle_mousedown = iconMouseDown;
877 btn->icon->core->descriptor.handle_enternotify = clipEnterNotify;
878 btn->icon->core->descriptor.handle_leavenotify = clipLeaveNotify;
879 btn->icon->core->descriptor.parent_type = WCLASS_DOCK_ICON;
880 btn->icon->core->descriptor.parent = btn;
881 XMapWindow(dpy, btn->icon->core->window);
882 btn->x_pos = x_pos;
883 btn->y_pos = 0;
884 btn->docked = 1;
885 if (type == WM_CLIP ||
886 (type == WM_DOCK && wPreferences.flags.clip_merged_in_dock))
887 scr->clip_icon = btn;
889 return btn;
892 static void switchWSCommand(WMenu *menu, WMenuEntry *entry)
894 WAppIcon *btn, *icon = (WAppIcon *) entry->clientdata;
895 WScreen *scr = icon->icon->core->screen_ptr;
896 WDock *src, *dest;
897 WMArray *selectedIcons;
898 int x, y;
900 /* Parameter not used, but tell the compiler that it is ok */
901 (void) menu;
903 if (entry->order == scr->current_workspace)
904 return;
906 src = icon->dock;
907 dest = scr->workspaces[entry->order]->clip;
909 selectedIcons = getSelected(src);
911 if (WMGetArrayItemCount(selectedIcons)) {
912 WMArrayIterator iter;
914 WM_ITERATE_ARRAY(selectedIcons, btn, iter) {
915 if (wDockFindFreeSlot(dest, &x, &y)) {
916 wDockMoveIconBetweenDocks(src, dest, btn, x, y);
917 XUnmapWindow(dpy, btn->icon->core->window);
920 } else if (icon != scr->clip_icon) {
921 if (wDockFindFreeSlot(dest, &x, &y)) {
922 wDockMoveIconBetweenDocks(src, dest, icon, x, y);
923 XUnmapWindow(dpy, icon->icon->core->window);
926 WMFreeArray(selectedIcons);
929 static void launchDockedApplication(WAppIcon *btn, Bool withSelection)
931 WScreen *scr = btn->icon->core->screen_ptr;
933 if (!btn->launching &&
934 ((!withSelection && btn->command != NULL) || (withSelection && btn->paste_command != NULL))) {
935 if (!btn->forced_dock) {
936 btn->relaunching = btn->running;
937 btn->running = 1;
939 if (btn->wm_instance || btn->wm_class) {
940 WWindowAttributes attr;
941 memset(&attr, 0, sizeof(WWindowAttributes));
942 wDefaultFillAttributes(btn->wm_instance, btn->wm_class, &attr, NULL, True);
944 if (!attr.no_appicon && !btn->buggy_app)
945 btn->launching = 1;
946 else
947 btn->running = 0;
949 btn->drop_launch = 0;
950 btn->paste_launch = withSelection;
951 scr->last_dock = btn->dock;
952 btn->pid = execCommand(btn, (withSelection ? btn->paste_command : btn->command), NULL);
953 if (btn->pid > 0) {
954 if (btn->buggy_app) {
955 /* give feedback that the app was launched */
956 btn->launching = 1;
957 dockIconPaint(btn);
958 btn->launching = 0;
959 WMAddTimerHandler(200, (WMCallback *) dockIconPaint, btn);
960 } else {
961 dockIconPaint(btn);
963 } else {
964 wwarning(_("could not launch application %s"), btn->command);
965 btn->launching = 0;
966 if (!btn->relaunching)
967 btn->running = 0;
972 static void updateWorkspaceMenu(WMenu *menu, WAppIcon *icon)
974 WScreen *scr = menu->frame->screen_ptr;
975 int i;
977 if (!menu || !icon)
978 return;
980 for (i = 0; i < scr->workspace_count; i++) {
981 if (i < menu->entry_no) {
982 if (strcmp(menu->entries[i]->text, scr->workspaces[i]->name) != 0) {
983 wfree(menu->entries[i]->text);
984 menu->entries[i]->text = wstrdup(scr->workspaces[i]->name);
985 menu->flags.realized = 0;
987 menu->entries[i]->clientdata = (void *)icon;
988 } else {
989 wMenuAddCallback(menu, scr->workspaces[i]->name, switchWSCommand, (void *)icon);
991 menu->flags.realized = 0;
994 if (i == scr->current_workspace)
995 wMenuSetEnabled(menu, i, False);
996 else
997 wMenuSetEnabled(menu, i, True);
1000 if (!menu->flags.realized)
1001 wMenuRealize(menu);
1004 static WMenu *makeWorkspaceMenu(WScreen *scr)
1006 WMenu *menu;
1008 menu = wMenuCreate(scr, NULL, False);
1009 if (!menu)
1010 wwarning(_("could not create workspace submenu for Clip menu"));
1012 wMenuAddCallback(menu, "", switchWSCommand, (void *)scr->clip_icon);
1014 menu->flags.realized = 0;
1015 wMenuRealize(menu);
1017 return menu;
1020 static void updateClipOptionsMenu(WMenu *menu, WDock *dock)
1022 WMenuEntry *entry;
1023 int index = 0;
1025 if (!menu || !dock)
1026 return;
1028 /* keep on top */
1029 entry = menu->entries[index];
1030 entry->flags.indicator_on = !dock->lowered;
1031 entry->clientdata = dock;
1032 wMenuSetEnabled(menu, index, dock->type == WM_CLIP);
1034 /* collapsed */
1035 entry = menu->entries[++index];
1036 entry->flags.indicator_on = dock->collapsed;
1037 entry->clientdata = dock;
1039 /* auto-collapse */
1040 entry = menu->entries[++index];
1041 entry->flags.indicator_on = dock->auto_collapse;
1042 entry->clientdata = dock;
1044 /* auto-raise/lower */
1045 entry = menu->entries[++index];
1046 entry->flags.indicator_on = dock->auto_raise_lower;
1047 entry->clientdata = dock;
1048 wMenuSetEnabled(menu, index, dock->lowered && (dock->type == WM_CLIP));
1050 /* attract icons */
1051 entry = menu->entries[++index];
1052 entry->flags.indicator_on = dock->attract_icons;
1053 entry->clientdata = dock;
1055 menu->flags.realized = 0;
1056 wMenuRealize(menu);
1060 static WMenu *makeClipOptionsMenu(WScreen *scr)
1062 WMenu *menu;
1063 WMenuEntry *entry;
1065 menu = wMenuCreate(scr, NULL, False);
1066 if (!menu) {
1067 wwarning(_("could not create options submenu for Clip menu"));
1068 return NULL;
1071 entry = wMenuAddCallback(menu, _("Keep on Top"), toggleLoweredCallback, NULL);
1072 entry->flags.indicator = 1;
1073 entry->flags.indicator_on = 1;
1074 entry->flags.indicator_type = MI_CHECK;
1076 entry = wMenuAddCallback(menu, _("Collapsed"), toggleCollapsedCallback, NULL);
1077 entry->flags.indicator = 1;
1078 entry->flags.indicator_on = 1;
1079 entry->flags.indicator_type = MI_CHECK;
1081 entry = wMenuAddCallback(menu, _("Autocollapse"), toggleAutoCollapseCallback, NULL);
1082 entry->flags.indicator = 1;
1083 entry->flags.indicator_on = 1;
1084 entry->flags.indicator_type = MI_CHECK;
1086 entry = wMenuAddCallback(menu, _("Autoraise"), toggleAutoRaiseLowerCallback, NULL);
1087 entry->flags.indicator = 1;
1088 entry->flags.indicator_on = 1;
1089 entry->flags.indicator_type = MI_CHECK;
1091 entry = wMenuAddCallback(menu, _("Autoattract Icons"), toggleAutoAttractCallback, NULL);
1092 entry->flags.indicator = 1;
1093 entry->flags.indicator_on = 1;
1094 entry->flags.indicator_type = MI_CHECK;
1096 menu->flags.realized = 0;
1097 wMenuRealize(menu);
1099 return menu;
1103 static void setDockPositionNormalCallback(WMenu *menu, WMenuEntry *entry)
1105 WDock *dock = (WDock *) entry->clientdata;
1106 WDrawerChain *dc;
1108 /* Parameter not used, but tell the compiler that it is ok */
1109 (void) menu;
1111 if (entry->flags.indicator_on) // already set, nothing to do
1112 return;
1113 // Do we come from auto raise lower or keep on top?
1114 if (dock->auto_raise_lower)
1116 dock->auto_raise_lower = 0;
1117 // Only for aesthetic purposes, can be removed when Autoraise status is no longer exposed in drawer option menu
1118 for (dc = dock->screen_ptr->drawers; dc != NULL; dc = dc->next) {
1119 dc->adrawer->auto_raise_lower = 0;
1122 else
1124 // Will take care of setting lowered = 0 in drawers
1125 toggleLowered(dock);
1127 entry->flags.indicator_on = 1;
1130 static void setDockPositionAutoRaiseLowerCallback(WMenu *menu, WMenuEntry *entry)
1132 WDock *dock = (WDock *) entry->clientdata;
1133 WDrawerChain *dc;
1135 /* Parameter not used, but tell the compiler that it is ok */
1136 (void) menu;
1138 if (entry->flags.indicator_on) // already set, nothing to do
1139 return;
1140 // Do we come from normal or keep on top?
1141 if (!dock->lowered)
1143 toggleLowered(dock);
1145 dock->auto_raise_lower = 1;
1146 // Only for aesthetic purposes, can be removed when Autoraise status is no longer exposed in drawer option menu
1147 for (dc = dock->screen_ptr->drawers; dc != NULL; dc = dc->next) {
1148 dc->adrawer->auto_raise_lower = 1;
1150 entry->flags.indicator_on = 1;
1153 static void setDockPositionKeepOnTopCallback(WMenu *menu, WMenuEntry *entry)
1155 WDock *dock = (WDock *) entry->clientdata;
1156 WDrawerChain *dc;
1158 /* Parameter not used, but tell the compiler that it is ok */
1159 (void) menu;
1161 if (entry->flags.indicator_on) // already set, nothing to do
1162 return;
1163 dock->auto_raise_lower = 0;
1164 // Only for aesthetic purposes, can be removed when Autoraise status is no longer exposed in drawer option menu
1165 for (dc = dock->screen_ptr->drawers; dc != NULL; dc = dc->next) {
1166 dc->adrawer->auto_raise_lower = 0;
1168 toggleLowered(dock);
1169 entry->flags.indicator_on = 1;
1172 static void updateDockPositionMenu(WMenu *menu, WDock *dock)
1174 WMenuEntry *entry;
1175 int index = 0;
1177 assert(menu);
1178 assert(dock);
1180 /* Normal level */
1181 entry = menu->entries[index++];
1182 entry->flags.indicator_on = (dock->lowered && !dock->auto_raise_lower);
1183 entry->clientdata = dock;
1185 /* Auto-raise/lower */
1186 entry = menu->entries[index++];
1187 entry->flags.indicator_on = dock->auto_raise_lower;
1188 entry->clientdata = dock;
1190 /* Keep on top */
1191 entry = menu->entries[index++];
1192 entry->flags.indicator_on = !dock->lowered;
1193 entry->clientdata = dock;
1196 static WMenu *makeDockPositionMenu(WScreen *scr)
1198 /* When calling this, the dock is being created, so scr->dock is still not set
1199 * Therefore the callbacks' clientdata and the indicators can't be set,
1200 * they will be updated when the dock menu is opened. */
1201 WMenu *menu;
1202 WMenuEntry *entry;
1204 menu = wMenuCreate(scr, NULL, False);
1205 if (!menu) {
1206 wwarning(_("could not create options submenu for dock position menu"));
1207 return NULL;
1210 entry = wMenuAddCallback(menu, _("Normal"), setDockPositionNormalCallback, NULL);
1211 entry->flags.indicator = 1;
1212 entry->flags.indicator_type = MI_DIAMOND;
1214 entry = wMenuAddCallback(menu, _("Auto raise & lower"), setDockPositionAutoRaiseLowerCallback, NULL);
1215 entry->flags.indicator = 1;
1216 entry->flags.indicator_type = MI_DIAMOND;
1218 entry = wMenuAddCallback(menu, _("Keep on Top"), setDockPositionKeepOnTopCallback, NULL);
1219 entry->flags.indicator = 1;
1220 entry->flags.indicator_type = MI_DIAMOND;
1222 menu->flags.realized = 0;
1223 wMenuRealize(menu);
1225 return menu;
1229 static WMenu *dockMenuCreate(WScreen *scr, int type)
1231 WMenu *menu;
1232 WMenuEntry *entry;
1234 if (type == WM_CLIP && scr->clip_menu)
1235 return scr->clip_menu;
1237 if (type == WM_DRAWER && scr->drawer_menu)
1238 return scr->drawer_menu;
1240 menu = wMenuCreate(scr, NULL, False);
1241 if (type == WM_DOCK) {
1242 entry = wMenuAddCallback(menu, _("Dock position"), NULL, NULL);
1243 if (scr->dock_pos_menu == NULL)
1244 scr->dock_pos_menu = makeDockPositionMenu(scr);
1245 wMenuEntrySetCascade(menu, entry, scr->dock_pos_menu);
1247 if (!wPreferences.flags.nodrawer)
1248 wMenuAddCallback(menu, _("Add a drawer"), addADrawerCallback, NULL);
1250 } else {
1251 if (type == WM_CLIP)
1252 entry = wMenuAddCallback(menu, _("Clip Options"), NULL, NULL);
1253 else /* if (type == WM_DRAWER) */
1254 entry = wMenuAddCallback(menu, _("Drawer options"), NULL, NULL);
1256 if (scr->clip_options == NULL)
1257 scr->clip_options = makeClipOptionsMenu(scr);
1259 wMenuEntrySetCascade(menu, entry, scr->clip_options);
1261 /* The same menu is used for the dock and its appicons. If the menu
1262 * entry text is different between the two contexts, or if it can
1263 * change depending on some state, free the duplicated string (from
1264 * wMenuInsertCallback) and use gettext's string */
1265 if (type == WM_CLIP) {
1266 entry = wMenuAddCallback(menu, _("Rename Workspace"), renameCallback, NULL);
1267 wfree(entry->text);
1268 entry->text = _("Rename Workspace"); /* can be: (Toggle) Omnipresent */
1271 entry = wMenuAddCallback(menu, _("Selected"), selectCallback, NULL);
1272 entry->flags.indicator = 1;
1273 entry->flags.indicator_on = 1;
1274 entry->flags.indicator_type = MI_CHECK;
1276 entry = wMenuAddCallback(menu, _("Select All Icons"), selectIconsCallback, NULL);
1277 wfree(entry->text);
1278 entry->text = _("Select All Icons"); /* can be: Unselect all icons */
1280 entry = wMenuAddCallback(menu, _("Keep Icon"), keepIconsCallback, NULL);
1281 wfree(entry->text);
1282 entry->text = _("Keep Icon"); /* can be: Keep Icons */
1284 if (type == WM_CLIP) {
1285 entry = wMenuAddCallback(menu, _("Move Icon To"), NULL, NULL);
1286 wfree(entry->text);
1287 entry->text = _("Move Icon To"); /* can be: Move Icons to */
1288 scr->clip_submenu = makeWorkspaceMenu(scr);
1289 if (scr->clip_submenu)
1290 wMenuEntrySetCascade(menu, entry, scr->clip_submenu);
1293 entry = wMenuAddCallback(menu, _("Remove Icon"), removeIconsCallback, NULL);
1294 wfree(entry->text);
1295 entry->text = _("Remove Icon"); /* can be: Remove Icons */
1297 wMenuAddCallback(menu, _("Attract Icons"), attractIconsCallback, NULL);
1300 wMenuAddCallback(menu, _("Launch"), launchCallback, NULL);
1302 wMenuAddCallback(menu, _("Unhide Here"), unhideHereCallback, NULL);
1304 entry = wMenuAddCallback(menu, _("Hide"), hideCallback, NULL);
1305 wfree(entry->text);
1306 entry->text = _("Hide"); /* can be: Unhide */
1308 wMenuAddCallback(menu, _("Settings..."), settingsCallback, NULL);
1310 entry = wMenuAddCallback(menu, _("Kill"), killCallback, NULL);
1311 wfree(entry->text);
1312 entry->text = _("Kill"); /* can be: Remove drawer */
1314 if (type == WM_CLIP)
1315 scr->clip_menu = menu;
1317 if (type == WM_DRAWER)
1318 scr->drawer_menu = menu;
1320 return menu;
1323 WDock *wDockCreate(WScreen *scr, int type, const char *name)
1325 WDock *dock;
1326 WAppIcon *btn;
1328 make_keys();
1330 dock = wmalloc(sizeof(WDock));
1332 switch (type) {
1333 case WM_CLIP:
1334 dock->max_icons = DOCK_MAX_ICONS;
1335 break;
1336 case WM_DRAWER:
1337 dock->max_icons = scr->scr_width / wPreferences.icon_size;
1338 break;
1339 case WM_DOCK:
1340 default:
1341 dock->max_icons = scr->scr_height / wPreferences.icon_size;
1344 dock->icon_array = wmalloc(sizeof(WAppIcon *) * dock->max_icons);
1346 btn = mainIconCreate(scr, type, name);
1348 btn->dock = dock;
1350 dock->x_pos = btn->x_pos;
1351 dock->y_pos = btn->y_pos;
1352 dock->screen_ptr = scr;
1353 dock->type = type;
1354 dock->icon_count = 1;
1355 if (type == WM_DRAWER)
1356 dock->on_right_side = scr->dock->on_right_side;
1357 else
1358 dock->on_right_side = 1;
1359 dock->collapsed = 0;
1360 dock->auto_collapse = 0;
1361 dock->auto_collapse_magic = NULL;
1362 dock->auto_raise_lower = 0;
1363 dock->auto_lower_magic = NULL;
1364 dock->auto_raise_magic = NULL;
1365 dock->attract_icons = 0;
1366 dock->lowered = 1;
1367 dock->icon_array[0] = btn;
1368 wRaiseFrame(btn->icon->core);
1369 XMoveWindow(dpy, btn->icon->core->window, btn->x_pos, btn->y_pos);
1371 /* create dock menu */
1372 dock->menu = dockMenuCreate(scr, type);
1374 if (type == WM_DRAWER) {
1375 drawerAppendToChain(scr, dock);
1376 dock->auto_collapse = 1;
1379 return dock;
1382 void wDockDestroy(WDock *dock)
1384 int i;
1385 WAppIcon *aicon;
1387 for (i = (dock->type == WM_CLIP) ? 1 : 0; i < dock->max_icons; i++) {
1388 aicon = dock->icon_array[i];
1389 if (aicon) {
1390 int keepit = aicon->running && wApplicationOf(aicon->main_window);
1391 wDockDetach(dock, aicon);
1392 if (keepit) {
1393 /* XXX: can: aicon->icon == NULL ? */
1394 PlaceIcon(dock->screen_ptr, &aicon->x_pos, &aicon->y_pos,
1395 wGetHeadForWindow(aicon->icon->owner));
1396 XMoveWindow(dpy, aicon->icon->core->window, aicon->x_pos, aicon->y_pos);
1397 if (!dock->mapped || dock->collapsed)
1398 XMapWindow(dpy, aicon->icon->core->window);
1402 if (wPreferences.auto_arrange_icons)
1403 wArrangeIcons(dock->screen_ptr, True);
1404 wfree(dock->icon_array);
1405 if (dock->menu && dock->type != WM_CLIP)
1406 wMenuDestroy(dock->menu, True);
1407 if (dock->screen_ptr->last_dock == dock)
1408 dock->screen_ptr->last_dock = NULL;
1409 wfree(dock);
1412 void wClipIconPaint(WAppIcon *aicon)
1414 WScreen *scr = aicon->icon->core->screen_ptr;
1415 WWorkspace *workspace = scr->workspaces[scr->current_workspace];
1416 WMColor *color;
1417 Window win = aicon->icon->core->window;
1418 int length, nlength;
1419 char *ws_name, ws_number[sizeof scr->current_workspace * CHAR_BIT / 3 + 1];
1420 int ty, tx;
1422 wIconPaint(aicon->icon);
1424 length = strlen(workspace->name);
1425 ws_name = wmalloc(length + 1);
1426 snprintf(ws_name, length + 1, "%s", workspace->name);
1427 snprintf(ws_number, sizeof ws_number, "%u", scr->current_workspace + 1);
1428 nlength = strlen(ws_number);
1430 if (wPreferences.flags.noclip || !workspace->clip->collapsed)
1431 color = scr->clip_title_color[CLIP_NORMAL];
1432 else
1433 color = scr->clip_title_color[CLIP_COLLAPSED];
1435 ty = ICON_SIZE - WMFontHeight(scr->clip_title_font) - 3;
1437 tx = CLIP_BUTTON_SIZE * ICON_SIZE / 64;
1439 if(wPreferences.show_clip_title)
1440 WMDrawString(scr->wmscreen, win, color, scr->clip_title_font, tx, ty, ws_name, length);
1442 tx = (ICON_SIZE / 2 - WMWidthOfString(scr->clip_title_font, ws_number, nlength)) / 2;
1444 WMDrawString(scr->wmscreen, win, color, scr->clip_title_font, tx, 2, ws_number, nlength);
1446 wfree(ws_name);
1448 if (aicon->launching)
1449 XFillRectangle(dpy, aicon->icon->core->window, scr->stipple_gc,
1450 0, 0, wPreferences.icon_size, wPreferences.icon_size);
1452 paintClipButtons(aicon, aicon->dock->lclip_button_pushed, aicon->dock->rclip_button_pushed);
1455 static void clipIconExpose(WObjDescriptor *desc, XEvent *event)
1457 /* Parameter not used, but tell the compiler that it is ok */
1458 (void) desc;
1459 (void) event;
1461 wClipIconPaint(desc->parent);
1464 static void dockIconPaint(WAppIcon *btn)
1466 if (btn == btn->icon->core->screen_ptr->clip_icon) {
1467 wClipIconPaint(btn);
1468 } else if (wIsADrawer(btn)) {
1469 wDrawerIconPaint(btn);
1470 } else {
1471 wAppIconPaint(btn);
1472 save_appicon(btn);
1476 static WMPropList *make_icon_state(WAppIcon *btn)
1478 WMPropList *node = NULL;
1479 WMPropList *command, *autolaunch, *lock, *name, *forced;
1480 WMPropList *position, *buggy, *omnipresent;
1481 char *tmp;
1482 char buffer[64];
1484 if (btn) {
1485 if (!btn->command)
1486 command = WMCreatePLString("-");
1487 else
1488 command = WMCreatePLString(btn->command);
1490 autolaunch = btn->auto_launch ? dYes : dNo;
1492 lock = btn->lock ? dYes : dNo;
1494 tmp = EscapeWM_CLASS(btn->wm_instance, btn->wm_class);
1496 name = WMCreatePLString(tmp);
1498 wfree(tmp);
1500 forced = btn->forced_dock ? dYes : dNo;
1502 buggy = btn->buggy_app ? dYes : dNo;
1504 if (!wPreferences.flags.clip_merged_in_dock && btn == btn->icon->core->screen_ptr->clip_icon)
1505 snprintf(buffer, sizeof(buffer), "%i,%i", btn->x_pos, btn->y_pos);
1506 else
1507 snprintf(buffer, sizeof(buffer), "%hi,%hi", btn->xindex, btn->yindex);
1508 position = WMCreatePLString(buffer);
1510 node = WMCreatePLDictionary(dCommand, command,
1511 dName, name,
1512 dAutoLaunch, autolaunch,
1513 dLock, lock,
1514 dForced, forced, dBuggyApplication, buggy, dPosition, position, NULL);
1515 WMReleasePropList(command);
1516 WMReleasePropList(name);
1517 WMReleasePropList(position);
1519 omnipresent = btn->omnipresent ? dYes : dNo;
1520 if (btn->dock != btn->icon->core->screen_ptr->dock && (btn->xindex != 0 || btn->yindex != 0))
1521 WMPutInPLDictionary(node, dOmnipresent, omnipresent);
1523 #ifdef USE_DOCK_XDND
1524 if (btn->dnd_command) {
1525 command = WMCreatePLString(btn->dnd_command);
1526 WMPutInPLDictionary(node, dDropCommand, command);
1527 WMReleasePropList(command);
1529 #endif /* USE_DOCK_XDND */
1531 if (btn->paste_command) {
1532 command = WMCreatePLString(btn->paste_command);
1533 WMPutInPLDictionary(node, dPasteCommand, command);
1534 WMReleasePropList(command);
1538 return node;
1541 static WMPropList *dockSaveState(WDock *dock)
1543 int i;
1544 WMPropList *icon_info;
1545 WMPropList *list = NULL, *dock_state = NULL;
1546 WMPropList *value, *key;
1547 char buffer[256];
1549 list = WMCreatePLArray(NULL);
1551 for (i = (dock->type == WM_DOCK ? 0 : 1); i < dock->max_icons; i++) {
1552 WAppIcon *btn = dock->icon_array[i];
1554 if (!btn || btn->attracted)
1555 continue;
1557 icon_info = make_icon_state(dock->icon_array[i]);
1558 if (icon_info != NULL) {
1559 WMAddToPLArray(list, icon_info);
1560 WMReleasePropList(icon_info);
1564 dock_state = WMCreatePLDictionary(dApplications, list, NULL);
1566 if (dock->type == WM_DOCK) {
1567 snprintf(buffer, sizeof(buffer), "Applications%i", dock->screen_ptr->scr_height);
1568 key = WMCreatePLString(buffer);
1569 WMPutInPLDictionary(dock_state, key, list);
1570 WMReleasePropList(key);
1572 snprintf(buffer, sizeof(buffer), "%i,%i", dock->x_pos, dock->y_pos);
1573 value = WMCreatePLString(buffer);
1574 WMPutInPLDictionary(dock_state, dPosition, value);
1575 WMReleasePropList(value);
1577 WMReleasePropList(list);
1579 if (dock->type == WM_CLIP || dock->type == WM_DRAWER) {
1580 value = (dock->collapsed ? dYes : dNo);
1581 WMPutInPLDictionary(dock_state, dCollapsed, value);
1583 value = (dock->auto_collapse ? dYes : dNo);
1584 WMPutInPLDictionary(dock_state, dAutoCollapse, value);
1586 value = (dock->attract_icons ? dYes : dNo);
1587 WMPutInPLDictionary(dock_state, dAutoAttractIcons, value);
1590 if (dock->type == WM_DOCK || dock->type == WM_CLIP) {
1591 value = (dock->lowered ? dYes : dNo);
1592 WMPutInPLDictionary(dock_state, dLowered, value);
1594 value = (dock->auto_raise_lower ? dYes : dNo);
1595 WMPutInPLDictionary(dock_state, dAutoRaiseLower, value);
1598 return dock_state;
1601 void wDockSaveState(WScreen *scr, WMPropList *old_state)
1603 WMPropList *dock_state;
1604 WMPropList *keys;
1606 dock_state = dockSaveState(scr->dock);
1609 * Copy saved states of docks with different sizes.
1611 if (old_state) {
1612 int i;
1613 WMPropList *tmp;
1615 keys = WMGetPLDictionaryKeys(old_state);
1616 for (i = 0; i < WMGetPropListItemCount(keys); i++) {
1617 tmp = WMGetFromPLArray(keys, i);
1619 if (strncasecmp(WMGetFromPLString(tmp), "applications", 12) == 0
1620 && !WMGetFromPLDictionary(dock_state, tmp)) {
1622 WMPutInPLDictionary(dock_state, tmp, WMGetFromPLDictionary(old_state, tmp));
1625 WMReleasePropList(keys);
1628 WMPutInPLDictionary(scr->session_state, dDock, dock_state);
1630 WMReleasePropList(dock_state);
1633 void wClipSaveState(WScreen *scr)
1635 WMPropList *clip_state;
1637 clip_state = make_icon_state(scr->clip_icon);
1639 WMPutInPLDictionary(scr->session_state, dClip, clip_state);
1641 WMReleasePropList(clip_state);
1644 WMPropList *wClipSaveWorkspaceState(WScreen *scr, int workspace)
1646 return dockSaveState(scr->workspaces[workspace]->clip);
1649 static Bool getBooleanDockValue(WMPropList *value, WMPropList *key)
1651 if (value) {
1652 if (WMIsPLString(value)) {
1653 if (strcasecmp(WMGetFromPLString(value), "YES") == 0)
1654 return True;
1655 } else {
1656 wwarning(_("bad value in docked icon state info %s"), WMGetFromPLString(key));
1659 return False;
1662 static WAppIcon *restore_icon_state(WScreen *scr, WMPropList *info, int type, int index)
1664 WAppIcon *aicon;
1665 WMPropList *cmd, *value;
1666 char *wclass, *winstance, *command;
1668 cmd = WMGetFromPLDictionary(info, dCommand);
1669 if (!cmd || !WMIsPLString(cmd))
1670 return NULL;
1672 /* parse window name */
1673 value = WMGetFromPLDictionary(info, dName);
1674 if (!value)
1675 return NULL;
1677 ParseWindowName(value, &winstance, &wclass, "dock");
1679 if (!winstance && !wclass)
1680 return NULL;
1682 /* get commands */
1683 command = wstrdup(WMGetFromPLString(cmd));
1685 if (strcmp(command, "-") == 0) {
1686 wfree(command);
1688 if (wclass)
1689 wfree(wclass);
1690 if (winstance)
1691 wfree(winstance);
1693 return NULL;
1696 aicon = wAppIconCreateForDock(scr, command, winstance, wclass, TILE_NORMAL);
1697 if (wclass)
1698 wfree(wclass);
1699 if (winstance)
1700 wfree(winstance);
1702 wfree(command);
1704 aicon->icon->core->descriptor.handle_mousedown = iconMouseDown;
1705 aicon->icon->core->descriptor.handle_enternotify = clipEnterNotify;
1706 aicon->icon->core->descriptor.handle_leavenotify = clipLeaveNotify;
1707 aicon->icon->core->descriptor.parent_type = WCLASS_DOCK_ICON;
1708 aicon->icon->core->descriptor.parent = aicon;
1710 #ifdef USE_DOCK_XDND
1711 cmd = WMGetFromPLDictionary(info, dDropCommand);
1712 if (cmd)
1713 aicon->dnd_command = wstrdup(WMGetFromPLString(cmd));
1714 #endif
1716 cmd = WMGetFromPLDictionary(info, dPasteCommand);
1717 if (cmd)
1718 aicon->paste_command = wstrdup(WMGetFromPLString(cmd));
1720 /* check auto launch */
1721 value = WMGetFromPLDictionary(info, dAutoLaunch);
1723 aicon->auto_launch = getBooleanDockValue(value, dAutoLaunch);
1725 /* check lock */
1726 value = WMGetFromPLDictionary(info, dLock);
1728 aicon->lock = getBooleanDockValue(value, dLock);
1730 /* check if it wasn't normally docked */
1731 value = WMGetFromPLDictionary(info, dForced);
1733 aicon->forced_dock = getBooleanDockValue(value, dForced);
1735 /* check if we can rely on the stuff in the app */
1736 value = WMGetFromPLDictionary(info, dBuggyApplication);
1738 aicon->buggy_app = getBooleanDockValue(value, dBuggyApplication);
1740 /* get position in the dock */
1741 value = WMGetFromPLDictionary(info, dPosition);
1742 if (value && WMIsPLString(value)) {
1743 if (sscanf(WMGetFromPLString(value), "%hi,%hi", &aicon->xindex, &aicon->yindex) != 2)
1744 wwarning(_("bad value in docked icon state info %s"), WMGetFromPLString(dPosition));
1746 /* check position sanity */
1747 /* *Very* incomplete section! */
1748 if (type == WM_DOCK) {
1749 aicon->xindex = 0;
1751 } else {
1752 aicon->yindex = index;
1753 aicon->xindex = 0;
1756 /* check if icon is omnipresent */
1757 value = WMGetFromPLDictionary(info, dOmnipresent);
1759 aicon->omnipresent = getBooleanDockValue(value, dOmnipresent);
1761 aicon->running = 0;
1762 aicon->docked = 1;
1764 return aicon;
1767 #define COMPLAIN(key) wwarning(_("bad value in dock state info:%s"), key)
1769 WAppIcon *wClipRestoreState(WScreen *scr, WMPropList *clip_state)
1771 WAppIcon *icon;
1772 WMPropList *value;
1774 icon = mainIconCreate(scr, WM_CLIP, NULL);
1776 if (!clip_state)
1777 return icon;
1779 WMRetainPropList(clip_state);
1781 /* restore position */
1783 value = WMGetFromPLDictionary(clip_state, dPosition);
1785 if (value) {
1786 if (!WMIsPLString(value)) {
1787 COMPLAIN("Position");
1788 } else {
1789 if (sscanf(WMGetFromPLString(value), "%i,%i", &icon->x_pos, &icon->y_pos) != 2)
1790 COMPLAIN("Position");
1792 /* check position sanity */
1793 if (!onScreen(scr, icon->x_pos, icon->y_pos))
1794 wScreenKeepInside(scr, &icon->x_pos, &icon->y_pos, ICON_SIZE, ICON_SIZE);
1797 #ifdef USE_DOCK_XDND
1798 value = WMGetFromPLDictionary(clip_state, dDropCommand);
1799 if (value && WMIsPLString(value))
1800 icon->dnd_command = wstrdup(WMGetFromPLString(value));
1801 #endif
1803 value = WMGetFromPLDictionary(clip_state, dPasteCommand);
1804 if (value && WMIsPLString(value))
1805 icon->paste_command = wstrdup(WMGetFromPLString(value));
1807 WMReleasePropList(clip_state);
1809 return icon;
1812 WDock *wDockRestoreState(WScreen *scr, WMPropList *dock_state, int type)
1814 WDock *dock;
1815 WMPropList *apps;
1816 WMPropList *value;
1817 WAppIcon *aicon, *old_top;
1818 int count, i;
1820 dock = wDockCreate(scr, type, NULL);
1822 if (!dock_state)
1823 return dock;
1825 WMRetainPropList(dock_state);
1827 /* restore position */
1828 value = WMGetFromPLDictionary(dock_state, dPosition);
1829 if (value) {
1830 if (!WMIsPLString(value)) {
1831 COMPLAIN("Position");
1832 } else {
1833 if (sscanf(WMGetFromPLString(value), "%i,%i", &dock->x_pos, &dock->y_pos) != 2)
1834 COMPLAIN("Position");
1836 /* check position sanity */
1837 if (!onScreen(scr, dock->x_pos, dock->y_pos)) {
1838 int x = dock->x_pos;
1839 wScreenKeepInside(scr, &x, &dock->y_pos, ICON_SIZE, ICON_SIZE);
1842 /* Is this needed any more? */
1843 if (type == WM_CLIP) {
1844 if (dock->x_pos < 0) {
1845 dock->x_pos = 0;
1846 } else if (dock->x_pos > scr->scr_width - ICON_SIZE) {
1847 dock->x_pos = scr->scr_width - ICON_SIZE;
1849 } else {
1850 int left, right, midpoint;
1852 left = getDockXPosition(scr, False);
1853 right = getDockXPosition(scr, True);
1854 midpoint = (left + right) / 2;
1856 if (dock->x_pos < midpoint) {
1857 dock->x_pos = left;
1858 dock->on_right_side = 0;
1859 } else {
1860 dock->x_pos = right;
1861 dock->on_right_side = 1;
1867 /* restore lowered/raised state */
1868 dock->lowered = 0;
1870 value = WMGetFromPLDictionary(dock_state, dLowered);
1871 if (value) {
1872 if (!WMIsPLString(value)) {
1873 COMPLAIN("Lowered");
1874 } else {
1875 if (strcasecmp(WMGetFromPLString(value), "YES") == 0)
1876 dock->lowered = 1;
1880 /* restore collapsed state */
1881 dock->collapsed = 0;
1883 value = WMGetFromPLDictionary(dock_state, dCollapsed);
1884 if (value) {
1885 if (!WMIsPLString(value)) {
1886 COMPLAIN("Collapsed");
1887 } else {
1888 if (strcasecmp(WMGetFromPLString(value), "YES") == 0)
1889 dock->collapsed = 1;
1893 /* restore auto-collapsed state */
1894 value = WMGetFromPLDictionary(dock_state, dAutoCollapse);
1895 if (value) {
1896 if (!WMIsPLString(value)) {
1897 COMPLAIN("AutoCollapse");
1898 } else {
1899 if (strcasecmp(WMGetFromPLString(value), "YES") == 0) {
1900 dock->auto_collapse = 1;
1901 dock->collapsed = 1;
1906 /* restore auto-raise/lower state */
1907 value = WMGetFromPLDictionary(dock_state, dAutoRaiseLower);
1908 if (value) {
1909 if (!WMIsPLString(value)) {
1910 COMPLAIN("AutoRaiseLower");
1911 } else {
1912 if (strcasecmp(WMGetFromPLString(value), "YES") == 0)
1913 dock->auto_raise_lower = 1;
1917 /* restore attract icons state */
1918 dock->attract_icons = 0;
1920 value = WMGetFromPLDictionary(dock_state, dAutoAttractIcons);
1921 if (value) {
1922 if (!WMIsPLString(value)) {
1923 COMPLAIN("AutoAttractIcons");
1924 } else {
1925 if (strcasecmp(WMGetFromPLString(value), "YES") == 0)
1926 dock->attract_icons = 1;
1930 /* application list */
1933 WMPropList *tmp;
1934 char buffer[64];
1937 * When saving, it saves the dock state in
1938 * Applications and Applicationsnnn
1940 * When loading, it will first try Applicationsnnn.
1941 * If it does not exist, use Applications as default.
1944 snprintf(buffer, sizeof(buffer), "Applications%i", scr->scr_height);
1946 tmp = WMCreatePLString(buffer);
1947 apps = WMGetFromPLDictionary(dock_state, tmp);
1948 WMReleasePropList(tmp);
1950 if (!apps)
1951 apps = WMGetFromPLDictionary(dock_state, dApplications);
1954 if (!apps)
1955 goto finish;
1957 count = WMGetPropListItemCount(apps);
1958 if (count == 0)
1959 goto finish;
1961 old_top = dock->icon_array[0];
1963 /* dock->icon_count is set to 1 when dock is created.
1964 * Since Clip is already restored, we want to keep it so for clip,
1965 * but for dock we may change the default top tile, so we set it to 0.
1967 if (type == WM_DOCK)
1968 dock->icon_count = 0;
1970 for (i = 0; i < count; i++) {
1971 if (dock->icon_count >= dock->max_icons) {
1972 wwarning(_("there are too many icons stored in dock. Ignoring what doesn't fit"));
1973 break;
1976 value = WMGetFromPLArray(apps, i);
1977 aicon = restore_icon_state(scr, value, type, dock->icon_count);
1979 dock->icon_array[dock->icon_count] = aicon;
1981 if (aicon) {
1982 aicon->dock = dock;
1983 aicon->x_pos = dock->x_pos + (aicon->xindex * ICON_SIZE);
1984 aicon->y_pos = dock->y_pos + (aicon->yindex * ICON_SIZE);
1986 if (dock->lowered)
1987 ChangeStackingLevel(aicon->icon->core, WMNormalLevel);
1988 else
1989 ChangeStackingLevel(aicon->icon->core, WMDockLevel);
1991 wCoreConfigure(aicon->icon->core, aicon->x_pos, aicon->y_pos, 0, 0);
1992 if (!dock->collapsed)
1993 XMapWindow(dpy, aicon->icon->core->window);
1995 wRaiseFrame(aicon->icon->core);
1997 dock->icon_count++;
1998 } else if (dock->icon_count == 0 && type == WM_DOCK) {
1999 dock->icon_count++;
2003 /* if the first icon is not defined, use the default */
2004 if (dock->icon_array[0] == NULL) {
2005 /* update default icon */
2006 old_top->x_pos = dock->x_pos;
2007 old_top->y_pos = dock->y_pos;
2008 if (dock->lowered)
2009 ChangeStackingLevel(old_top->icon->core, WMNormalLevel);
2010 else
2011 ChangeStackingLevel(old_top->icon->core, WMDockLevel);
2013 dock->icon_array[0] = old_top;
2014 XMoveWindow(dpy, old_top->icon->core->window, dock->x_pos, dock->y_pos);
2015 /* we don't need to increment dock->icon_count here because it was
2016 * incremented in the loop above.
2018 } else if (old_top != dock->icon_array[0]) {
2019 if (old_top == scr->clip_icon) // TODO dande: understand the logic
2020 scr->clip_icon = dock->icon_array[0];
2022 wAppIconDestroy(old_top);
2025 finish:
2026 WMReleasePropList(dock_state);
2028 return dock;
2031 void wDockLaunchWithState(WAppIcon *btn, WSavedState *state)
2033 if (btn && btn->command && !btn->running && !btn->launching) {
2034 btn->drop_launch = 0;
2035 btn->paste_launch = 0;
2037 btn->pid = execCommand(btn, btn->command, state);
2039 if (btn->pid > 0) {
2040 if (!btn->forced_dock && !btn->buggy_app) {
2041 btn->launching = 1;
2042 dockIconPaint(btn);
2045 } else {
2046 wfree(state);
2050 void wDockDoAutoLaunch(WDock *dock, int workspace)
2052 WAppIcon *btn;
2053 WSavedState *state;
2054 int i;
2056 for (i = 0; i < dock->max_icons; i++) {
2057 btn = dock->icon_array[i];
2058 if (!btn || !btn->auto_launch)
2059 continue;
2061 state = wmalloc(sizeof(WSavedState));
2062 state->workspace = workspace;
2063 /* TODO: this is klugy and is very difficult to understand
2064 * what's going on. Try to clean up */
2065 wDockLaunchWithState(btn, state);
2069 #ifdef USE_DOCK_XDND
2070 static WDock *findDock(WScreen *scr, XEvent *event, int *icon_pos)
2072 WDock *dock;
2073 int i;
2075 dock = scr->dock;
2076 if (dock != NULL) {
2077 for (i = 0; i < dock->max_icons; i++) {
2078 if (dock->icon_array[i] &&
2079 dock->icon_array[i]->icon->core->window == event->xclient.window) {
2080 *icon_pos = i;
2081 return dock;
2086 dock = scr->workspaces[scr->current_workspace]->clip;
2087 if (dock != NULL) {
2088 for (i = 0; i < dock->max_icons; i++) {
2089 if (dock->icon_array[i] &&
2090 dock->icon_array[i]->icon->core->window == event->xclient.window) {
2091 *icon_pos = i;
2092 return dock;
2097 *icon_pos = -1;
2098 return NULL;
2101 int wDockReceiveDNDDrop(WScreen *scr, XEvent *event)
2103 WDock *dock;
2104 WAppIcon *btn;
2105 int icon_pos;
2107 dock = findDock(scr, event, &icon_pos);
2108 if (!dock)
2109 return False;
2112 * Return True if the drop was on an application icon window.
2113 * In this case, let the ClientMessage handler redirect the
2114 * message to the app.
2116 if (dock->icon_array[icon_pos]->icon->icon_win != None)
2117 return True;
2119 if (dock->icon_array[icon_pos]->dnd_command != NULL) {
2120 scr->flags.dnd_data_convertion_status = 0;
2122 btn = dock->icon_array[icon_pos];
2124 if (!btn->forced_dock) {
2125 btn->relaunching = btn->running;
2126 btn->running = 1;
2128 if (btn->wm_instance || btn->wm_class) {
2129 WWindowAttributes attr;
2130 memset(&attr, 0, sizeof(WWindowAttributes));
2131 wDefaultFillAttributes(btn->wm_instance, btn->wm_class, &attr, NULL, True);
2133 if (!attr.no_appicon)
2134 btn->launching = 1;
2135 else
2136 btn->running = 0;
2139 btn->paste_launch = 0;
2140 btn->drop_launch = 1;
2141 scr->last_dock = dock;
2142 btn->pid = execCommand(btn, btn->dnd_command, NULL);
2143 if (btn->pid > 0) {
2144 dockIconPaint(btn);
2145 } else {
2146 btn->launching = 0;
2147 if (!btn->relaunching)
2148 btn->running = 0;
2151 return False;
2153 #endif /* USE_DOCK_XDND */
2155 Bool wDockAttachIcon(WDock *dock, WAppIcon *icon, int x, int y, Bool update_icon)
2157 WWindow *wwin;
2158 Bool lupdate_icon = False;
2159 char *command = NULL;
2160 int index;
2162 icon->editing = 0;
2164 if (update_icon)
2165 lupdate_icon = True;
2167 if (icon->command == NULL) {
2168 /* If icon->owner exists, it means the application is running */
2169 if (icon->icon->owner) {
2170 wwin = icon->icon->owner;
2171 command = GetCommandForWindow(wwin->client_win);
2174 if (command) {
2175 icon->command = command;
2176 } else {
2177 /* icon->forced_dock = 1; */
2178 if (dock->type != WM_CLIP || !icon->attracted) {
2179 icon->editing = 1;
2180 if (wInputDialog(dock->screen_ptr, _("Dock Icon"),
2181 _("Type the command used to launch the application"), &command)) {
2182 if (command && (command[0] == 0 || (command[0] == '-' && command[1] == 0))) {
2183 wfree(command);
2184 command = NULL;
2186 icon->command = command;
2187 icon->editing = 0;
2188 } else {
2189 icon->editing = 0;
2190 if (command)
2191 wfree(command);
2192 /* If the target is the dock, reject the icon. If
2193 * the target is the clip, make it an attracted icon
2195 if (dock->type == WM_CLIP) {
2196 icon->attracted = 1;
2197 if (!icon->icon->shadowed) {
2198 icon->icon->shadowed = 1;
2199 lupdate_icon = True;
2201 } else {
2202 return False;
2209 for (index = 1; index < dock->max_icons; index++)
2210 if (dock->icon_array[index] == NULL)
2211 break;
2212 /* if (index == dock->max_icons)
2213 return; */
2215 assert(index < dock->max_icons);
2217 dock->icon_array[index] = icon;
2218 icon->yindex = y;
2219 icon->xindex = x;
2221 icon->omnipresent = 0;
2223 icon->x_pos = dock->x_pos + x * ICON_SIZE;
2224 icon->y_pos = dock->y_pos + y * ICON_SIZE;
2226 dock->icon_count++;
2228 icon->running = 1;
2229 icon->launching = 0;
2230 icon->docked = 1;
2231 icon->dock = dock;
2232 icon->icon->core->descriptor.handle_mousedown = iconMouseDown;
2233 icon->icon->core->descriptor.handle_enternotify = clipEnterNotify;
2234 icon->icon->core->descriptor.handle_leavenotify = clipLeaveNotify;
2235 icon->icon->core->descriptor.parent_type = WCLASS_DOCK_ICON;
2236 icon->icon->core->descriptor.parent = icon;
2238 MoveInStackListUnder(dock->icon_array[index - 1]->icon->core, icon->icon->core);
2239 wAppIconMove(icon, icon->x_pos, icon->y_pos);
2242 * Update icon pixmap, RImage doesn't change,
2243 * so call wIconUpdate is not needed
2245 if (lupdate_icon)
2246 update_icon_pixmap(icon->icon);
2248 /* Paint it */
2249 wAppIconPaint(icon);
2251 /* Save it */
2252 save_appicon(icon);
2254 if (wPreferences.auto_arrange_icons)
2255 wArrangeIcons(dock->screen_ptr, True);
2257 #ifdef USE_DOCK_XDND
2258 if (icon->command && !icon->dnd_command) {
2259 int len = strlen(icon->command) + 8;
2260 icon->dnd_command = wmalloc(len);
2261 snprintf(icon->dnd_command, len, "%s %%d", icon->command);
2263 #endif
2265 if (icon->command && !icon->paste_command) {
2266 int len = strlen(icon->command) + 8;
2267 icon->paste_command = wmalloc(len);
2268 snprintf(icon->paste_command, len, "%s %%s", icon->command);
2271 return True;
2274 void wDockReattachIcon(WDock *dock, WAppIcon *icon, int x, int y)
2276 int index;
2278 for (index = 1; index < dock->max_icons; index++) {
2279 if (dock->icon_array[index] == icon)
2280 break;
2282 assert(index < dock->max_icons);
2284 icon->yindex = y;
2285 icon->xindex = x;
2287 icon->x_pos = dock->x_pos + x * ICON_SIZE;
2288 icon->y_pos = dock->y_pos + y * ICON_SIZE;
2291 Bool wDockMoveIconBetweenDocks(WDock *src, WDock *dest, WAppIcon *icon, int x, int y)
2293 WWindow *wwin;
2294 char *command = NULL;
2295 int index;
2296 Bool update_icon = False;
2298 if (src == dest)
2299 return True; /* No move needed, we're already there */
2301 if (dest == NULL)
2302 return False;
2305 * For the moment we can't do this if we move icons in Clip from one
2306 * workspace to other, because if we move two or more icons without
2307 * command, the dialog box will not be able to tell us to which of the
2308 * moved icons it applies. -Dan
2310 if ((dest->type == WM_DOCK /*|| dest->keep_attracted */ ) && icon->command == NULL) {
2311 /* If icon->owner exists, it means the application is running */
2312 if (icon->icon->owner) {
2313 wwin = icon->icon->owner;
2314 command = GetCommandForWindow(wwin->client_win);
2317 if (command) {
2318 icon->command = command;
2319 } else {
2320 icon->editing = 1;
2321 /* icon->forced_dock = 1; */
2322 if (wInputDialog(src->screen_ptr, _("Dock Icon"),
2323 _("Type the command used to launch the application"), &command)) {
2324 if (command && (command[0] == 0 || (command[0] == '-' && command[1] == 0))) {
2325 wfree(command);
2326 command = NULL;
2328 icon->command = command;
2329 } else {
2330 icon->editing = 0;
2331 if (command)
2332 wfree(command);
2333 return False;
2335 icon->editing = 0;
2339 if (dest->type == WM_DOCK || dest->type == WM_DRAWER)
2340 wClipMakeIconOmnipresent(icon, False);
2342 for (index = 1; index < src->max_icons; index++) {
2343 if (src->icon_array[index] == icon)
2344 break;
2346 assert(index < src->max_icons);
2348 src->icon_array[index] = NULL;
2349 src->icon_count--;
2351 for (index = 1; index < dest->max_icons; index++) {
2352 if (dest->icon_array[index] == NULL)
2353 break;
2356 assert(index < dest->max_icons);
2358 dest->icon_array[index] = icon;
2359 icon->dock = dest;
2361 /* deselect the icon */
2362 if (icon->icon->selected)
2363 wIconSelect(icon->icon);
2365 icon->icon->core->descriptor.handle_enternotify = clipEnterNotify;
2366 icon->icon->core->descriptor.handle_leavenotify = clipLeaveNotify;
2368 /* set it to be kept when moving to dock.
2369 * Unless the icon does not have a command set
2371 if (icon->command && (dest->type == WM_DOCK || dest->type == WM_DRAWER)) {
2372 icon->attracted = 0;
2373 if (icon->icon->shadowed) {
2374 icon->icon->shadowed = 0;
2375 update_icon = True;
2379 if (src->auto_collapse || src->auto_raise_lower)
2380 clipLeave(src);
2382 icon->yindex = y;
2383 icon->xindex = x;
2385 icon->x_pos = dest->x_pos + x * ICON_SIZE;
2386 icon->y_pos = dest->y_pos + y * ICON_SIZE;
2388 dest->icon_count++;
2390 MoveInStackListUnder(dest->icon_array[index - 1]->icon->core, icon->icon->core);
2393 * Update icon pixmap, RImage doesn't change,
2394 * so call wIconUpdate is not needed
2396 if (update_icon)
2397 update_icon_pixmap(icon->icon);
2399 /* Paint it */
2400 wAppIconPaint(icon);
2402 return True;
2405 void wDockDetach(WDock *dock, WAppIcon *icon)
2407 int index;
2408 Bool update_icon = False;
2410 /* make the settings panel be closed */
2411 if (icon->panel)
2412 DestroyDockAppSettingsPanel(icon->panel);
2414 /* This must be called before icon->dock is set to NULL.
2415 * Don't move it. -Dan
2417 wClipMakeIconOmnipresent(icon, False);
2419 icon->docked = 0;
2420 icon->dock = NULL;
2421 icon->attracted = 0;
2422 icon->auto_launch = 0;
2423 if (icon->icon->shadowed) {
2424 icon->icon->shadowed = 0;
2425 update_icon = True;
2428 /* deselect the icon */
2429 if (icon->icon->selected)
2430 wIconSelect(icon->icon);
2432 if (icon->command) {
2433 wfree(icon->command);
2434 icon->command = NULL;
2436 #ifdef USE_DOCK_XDND
2437 if (icon->dnd_command) {
2438 wfree(icon->dnd_command);
2439 icon->dnd_command = NULL;
2441 #endif
2442 if (icon->paste_command) {
2443 wfree(icon->paste_command);
2444 icon->paste_command = NULL;
2447 for (index = 1; index < dock->max_icons; index++)
2448 if (dock->icon_array[index] == icon)
2449 break;
2451 assert(index < dock->max_icons);
2452 dock->icon_array[index] = NULL;
2453 icon->yindex = -1;
2454 icon->xindex = -1;
2456 dock->icon_count--;
2458 /* Remove the Cached Icon */
2459 remove_cache_icon(icon->icon->file);
2461 /* if the dock is not attached to an application or
2462 * the application did not set the appropriate hints yet,
2463 * destroy the icon */
2464 if (!icon->running || !wApplicationOf(icon->main_window)) {
2465 wAppIconDestroy(icon);
2466 } else {
2467 icon->icon->core->descriptor.handle_mousedown = appIconMouseDown;
2468 icon->icon->core->descriptor.handle_enternotify = NULL;
2469 icon->icon->core->descriptor.handle_leavenotify = NULL;
2470 icon->icon->core->descriptor.parent_type = WCLASS_APPICON;
2471 icon->icon->core->descriptor.parent = icon;
2473 ChangeStackingLevel(icon->icon->core, NORMAL_ICON_LEVEL);
2476 * Update icon pixmap, RImage doesn't change,
2477 * so call wIconUpdate is not needed
2479 if (update_icon)
2480 update_icon_pixmap(icon->icon);
2482 /* Paint it */
2483 wAppIconPaint(icon);
2485 if (wPreferences.auto_arrange_icons)
2486 wArrangeIcons(dock->screen_ptr, True);
2489 if (dock->auto_collapse || dock->auto_raise_lower)
2490 clipLeave(dock);
2494 * returns the closest Dock slot index for the passed
2495 * coordinates.
2497 * Returns False if icon can't be docked.
2499 * Note: this function should NEVER alter ret_x or ret_y, unless it will
2500 * return True. -Dan
2502 /* Redocking == true means either icon->dock == dock (normal case)
2503 * or we are called from handleDockMove for a drawer */
2504 Bool wDockSnapIcon(WDock *dock, WAppIcon *icon, int req_x, int req_y, int *ret_x, int *ret_y, int redocking)
2506 WScreen *scr = dock->screen_ptr;
2507 int dx, dy;
2508 int ex_x, ex_y;
2509 int i, offset = ICON_SIZE / 2;
2510 WAppIcon *aicon = NULL;
2511 WAppIcon *nicon = NULL;
2513 if (wPreferences.flags.noupdates)
2514 return False;
2516 dx = dock->x_pos;
2517 dy = dock->y_pos;
2519 /* if the dock is full */
2520 if (!redocking && (dock->icon_count >= dock->max_icons))
2521 return False;
2523 /* exact position */
2524 if (req_y < dy)
2525 ex_y = (req_y - offset - dy) / ICON_SIZE;
2526 else
2527 ex_y = (req_y + offset - dy) / ICON_SIZE;
2529 if (req_x < dx)
2530 ex_x = (req_x - offset - dx) / ICON_SIZE;
2531 else
2532 ex_x = (req_x + offset - dx) / ICON_SIZE;
2534 /* check if the icon is outside the screen boundaries */
2535 if (!onScreen(scr, dx + ex_x * ICON_SIZE, dy + ex_y * ICON_SIZE))
2536 return False;
2538 switch (dock->type) {
2539 case WM_DOCK:
2540 /* We can return False right away if
2541 * - we do not come from this dock (which is a WM_DOCK),
2542 * - we are not right over it, and
2543 * - we are not the main tile of a drawer.
2544 * In the latter case, we are called from handleDockMove. */
2545 if (icon->dock != dock && ex_x != 0 &&
2546 !(icon->dock && icon->dock->type == WM_DRAWER && icon == icon->dock->icon_array[0]))
2547 return False;
2549 if (!redocking && ex_x != 0)
2550 return False;
2552 if (getDrawer(scr, ex_y)) /* Return false so that the drawer gets it. */
2553 return False;
2555 aicon = NULL;
2556 for (i = 0; i < dock->max_icons; i++) {
2557 nicon = dock->icon_array[i];
2558 if (nicon && nicon->yindex == ex_y) {
2559 aicon = nicon;
2560 break;
2564 if (redocking) {
2565 int sig, done, closest;
2567 /* Possible cases when redocking:
2569 * icon dragged out of range of any slot -> false
2570 * icon dragged on a drawer -> false (to open the drawer)
2571 * icon dragged to range of free slot
2572 * icon dragged to range of same slot
2573 * icon dragged to range of different icon
2575 if (abs(ex_x) > DOCK_DETTACH_THRESHOLD)
2576 return False;
2578 if (aicon == icon || !aicon) {
2579 *ret_x = 0;
2580 *ret_y = ex_y;
2581 return True;
2584 /* start looking at the upper slot or lower? */
2585 if (ex_y * ICON_SIZE < (req_y + offset - dy))
2586 sig = 1;
2587 else
2588 sig = -1;
2590 done = 0;
2591 /* look for closest free slot */
2592 for (i = 0; i < (DOCK_DETTACH_THRESHOLD + 1) * 2 && !done; i++) {
2593 int j;
2595 done = 1;
2596 closest = sig * (i / 2) + ex_y;
2597 /* check if this slot is fully on the screen and not used */
2598 if (onScreen(scr, dx, dy + closest * ICON_SIZE)) {
2599 for (j = 0; j < dock->max_icons; j++) {
2600 if (dock->icon_array[j]
2601 && dock->icon_array[j]->yindex == closest) {
2602 /* slot is used by someone else */
2603 if (dock->icon_array[j] != icon)
2604 done = 0;
2605 break;
2608 /* slot is used by a drawer */
2609 done = done && !getDrawer(scr, closest);
2611 else // !onScreen
2612 done = 0;
2613 sig = -sig;
2615 if (done &&
2616 ((ex_y >= closest && ex_y - closest < DOCK_DETTACH_THRESHOLD + 1)
2617 || (ex_y < closest && closest - ex_y <= DOCK_DETTACH_THRESHOLD + 1))) {
2618 *ret_x = 0;
2619 *ret_y = closest;
2620 return True;
2622 } else { /* !redocking */
2624 /* if slot is free and the icon is close enough, return it */
2625 if (!aicon && ex_x == 0) {
2626 *ret_x = 0;
2627 *ret_y = ex_y;
2628 return True;
2631 break;
2632 case WM_CLIP:
2634 int neighbours = 0;
2635 int start, stop, k;
2637 start = icon->omnipresent ? 0 : scr->current_workspace;
2638 stop = icon->omnipresent ? scr->workspace_count : start + 1;
2640 aicon = NULL;
2641 for (k = start; k < stop; k++) {
2642 WDock *tmp = scr->workspaces[k]->clip;
2643 if (!tmp)
2644 continue;
2645 for (i = 0; i < tmp->max_icons; i++) {
2646 nicon = tmp->icon_array[i];
2647 if (nicon && nicon->xindex == ex_x && nicon->yindex == ex_y) {
2648 aicon = nicon;
2649 break;
2652 if (aicon)
2653 break;
2655 for (k = start; k < stop; k++) {
2656 WDock *tmp = scr->workspaces[k]->clip;
2657 if (!tmp)
2658 continue;
2659 for (i = 0; i < tmp->max_icons; i++) {
2660 nicon = tmp->icon_array[i];
2661 if (nicon && nicon != icon && /* Icon can't be its own neighbour */
2662 (abs(nicon->xindex - ex_x) <= CLIP_ATTACH_VICINITY &&
2663 abs(nicon->yindex - ex_y) <= CLIP_ATTACH_VICINITY)) {
2664 neighbours = 1;
2665 break;
2668 if (neighbours)
2669 break;
2672 if (neighbours && (aicon == NULL || (redocking && aicon == icon))) {
2673 *ret_x = ex_x;
2674 *ret_y = ex_y;
2675 return True;
2677 break;
2679 case WM_DRAWER:
2681 WAppIcon *aicons_to_shift[ dock->icon_count ];
2682 int index_of_hole, j;
2684 if (ex_y != 0 ||
2685 abs(ex_x) - dock->icon_count > DOCK_DETTACH_THRESHOLD ||
2686 (ex_x < 0 && !dock->on_right_side) ||
2687 (ex_x > 0 && dock->on_right_side)) {
2688 return False;
2691 if (ex_x == 0)
2692 ex_x = (dock->on_right_side ? -1 : 1);
2694 /* "Reduce" ex_x but keep its sign */
2695 if (redocking) {
2696 if (abs(ex_x) > dock->icon_count - 1) /* minus 1: do not take icon_array[0] into account */
2697 ex_x = ex_x * (dock->icon_count - 1) / abs(ex_x); /* don't use *= ! */
2698 } else {
2699 if (abs(ex_x) > dock->icon_count)
2700 ex_x = ex_x * dock->icon_count / abs(ex_x);
2702 index_of_hole = indexOfHole(dock, icon, redocking);
2704 /* Find the appicons between where icon was (index_of_hole) and where
2705 * it wants to be (ex_x) and slide them. */
2706 j = 0;
2707 for (i = 1; i < dock->max_icons; i++) {
2708 aicon = dock->icon_array[i];
2709 if ((aicon != NULL) && (aicon != icon) &&
2710 ((ex_x <= aicon->xindex && aicon->xindex < index_of_hole) ||
2711 (index_of_hole < aicon->xindex && aicon->xindex <= ex_x)))
2712 aicons_to_shift[ j++ ] = aicon;
2714 assert(j == abs(ex_x - index_of_hole));
2716 wSlideAppicons(aicons_to_shift, j, (index_of_hole < ex_x));
2718 *ret_x = ex_x;
2719 *ret_y = ex_y;
2720 return True;
2723 return False;
2726 static int onScreen(WScreen *scr, int x, int y)
2728 WMRect rect;
2729 int flags;
2731 rect.pos.x = x;
2732 rect.pos.y = y;
2733 rect.size.width = rect.size.height = ICON_SIZE;
2735 wGetRectPlacementInfo(scr, rect, &flags);
2737 return !(flags & (XFLAG_DEAD | XFLAG_PARTIAL));
2741 * returns true if it can find a free slot in the dock,
2742 * in which case it changes x_pos and y_pos accordingly.
2743 * Else returns false.
2745 Bool wDockFindFreeSlot(WDock *dock, int *x_pos, int *y_pos)
2747 WScreen *scr = dock->screen_ptr;
2748 WAppIcon *btn;
2749 WAppIconChain *chain;
2750 unsigned char *slot_map;
2751 int mwidth;
2752 int r;
2753 int x, y;
2754 int i, done = False;
2755 int corner;
2756 int ex = scr->scr_width, ey = scr->scr_height;
2757 int extra_count = 0;
2759 if (dock->type == WM_DRAWER) {
2760 if (dock->icon_count >= dock->max_icons) { /* drawer is full */
2761 return False;
2763 *x_pos = dock->icon_count * (dock->on_right_side ? -1 : 1);
2764 *y_pos = 0;
2765 return True;
2768 if (dock->type == WM_CLIP && dock != scr->workspaces[scr->current_workspace]->clip)
2769 extra_count = scr->global_icon_count;
2771 /* if the dock is full */
2772 if (dock->icon_count + extra_count >= dock->max_icons)
2773 return False;
2775 if (!wPreferences.flags.nodock && scr->dock && scr->dock->on_right_side) {
2776 ex -= ICON_SIZE + DOCK_EXTRA_SPACE;
2779 if (ex < dock->x_pos)
2780 ex = dock->x_pos;
2781 #define C_NONE 0
2782 #define C_NW 1
2783 #define C_NE 2
2784 #define C_SW 3
2785 #define C_SE 4
2787 /* check if clip is in a corner */
2788 if (dock->type == WM_CLIP) {
2789 if (dock->x_pos < 1 && dock->y_pos < 1)
2790 corner = C_NE;
2791 else if (dock->x_pos < 1 && dock->y_pos >= (ey - ICON_SIZE))
2792 corner = C_SE;
2793 else if (dock->x_pos >= (ex - ICON_SIZE) && dock->y_pos >= (ey - ICON_SIZE))
2794 corner = C_SW;
2795 else if (dock->x_pos >= (ex - ICON_SIZE) && dock->y_pos < 1)
2796 corner = C_NW;
2797 else
2798 corner = C_NONE;
2799 } else {
2800 corner = C_NONE;
2803 /* If the clip is in the corner, use only slots that are in the border
2804 * of the screen */
2805 if (corner != C_NONE) {
2806 char *hmap, *vmap;
2807 int hcount, vcount;
2809 hcount = WMIN(dock->max_icons, scr->scr_width / ICON_SIZE);
2810 vcount = WMIN(dock->max_icons, scr->scr_height / ICON_SIZE);
2811 hmap = wmalloc(hcount + 1);
2812 vmap = wmalloc(vcount + 1);
2814 /* mark used positions */
2815 switch (corner) {
2816 case C_NE:
2817 for (i = 0; i < dock->max_icons; i++) {
2818 btn = dock->icon_array[i];
2819 if (!btn)
2820 continue;
2822 if (btn->xindex == 0 && btn->yindex > 0 && btn->yindex < vcount)
2823 vmap[btn->yindex] = 1;
2824 else if (btn->yindex == 0 && btn->xindex > 0 && btn->xindex < hcount)
2825 hmap[btn->xindex] = 1;
2827 for (chain = scr->global_icons; chain != NULL; chain = chain->next) {
2828 btn = chain->aicon;
2829 if (btn->xindex == 0 && btn->yindex > 0 && btn->yindex < vcount)
2830 vmap[btn->yindex] = 1;
2831 else if (btn->yindex == 0 && btn->xindex > 0 && btn->xindex < hcount)
2832 hmap[btn->xindex] = 1;
2834 break;
2835 case C_NW:
2836 for (i = 0; i < dock->max_icons; i++) {
2837 btn = dock->icon_array[i];
2838 if (!btn)
2839 continue;
2841 if (btn->xindex == 0 && btn->yindex > 0 && btn->yindex < vcount)
2842 vmap[btn->yindex] = 1;
2843 else if (btn->yindex == 0 && btn->xindex < 0 && btn->xindex > -hcount)
2844 hmap[-btn->xindex] = 1;
2846 for (chain = scr->global_icons; chain != NULL; chain = chain->next) {
2847 btn = chain->aicon;
2848 if (btn->xindex == 0 && btn->yindex > 0 && btn->yindex < vcount)
2849 vmap[btn->yindex] = 1;
2850 else if (btn->yindex == 0 && btn->xindex < 0 && btn->xindex > -hcount)
2851 hmap[-btn->xindex] = 1;
2853 break;
2854 case C_SE:
2855 for (i = 0; i < dock->max_icons; i++) {
2856 btn = dock->icon_array[i];
2857 if (!btn)
2858 continue;
2860 if (btn->xindex == 0 && btn->yindex < 0 && btn->yindex > -vcount)
2861 vmap[-btn->yindex] = 1;
2862 else if (btn->yindex == 0 && btn->xindex > 0 && btn->xindex < hcount)
2863 hmap[btn->xindex] = 1;
2865 for (chain = scr->global_icons; chain != NULL; chain = chain->next) {
2866 btn = chain->aicon;
2867 if (btn->xindex == 0 && btn->yindex < 0 && btn->yindex > -vcount)
2868 vmap[-btn->yindex] = 1;
2869 else if (btn->yindex == 0 && btn->xindex > 0 && btn->xindex < hcount)
2870 hmap[btn->xindex] = 1;
2872 break;
2873 case C_SW:
2874 default:
2875 for (i = 0; i < dock->max_icons; i++) {
2876 btn = dock->icon_array[i];
2877 if (!btn)
2878 continue;
2880 if (btn->xindex == 0 && btn->yindex < 0 && btn->yindex > -vcount)
2881 vmap[-btn->yindex] = 1;
2882 else if (btn->yindex == 0 && btn->xindex < 0 && btn->xindex > -hcount)
2883 hmap[-btn->xindex] = 1;
2885 for (chain = scr->global_icons; chain != NULL; chain = chain->next) {
2886 btn = chain->aicon;
2887 if (btn->xindex == 0 && btn->yindex < 0 && btn->yindex > -vcount)
2888 vmap[-btn->yindex] = 1;
2889 else if (btn->yindex == 0 && btn->xindex < 0 && btn->xindex > -hcount)
2890 hmap[-btn->xindex] = 1;
2893 x = 0;
2894 y = 0;
2895 done = 0;
2896 /* search a vacant slot */
2897 for (i = 1; i < WMAX(vcount, hcount); i++) {
2898 if (i < vcount && vmap[i] == 0) {
2899 /* found a slot */
2900 x = 0;
2901 y = i;
2902 done = 1;
2903 break;
2904 } else if (i < hcount && hmap[i] == 0) {
2905 /* found a slot */
2906 x = i;
2907 y = 0;
2908 done = 1;
2909 break;
2912 wfree(vmap);
2913 wfree(hmap);
2914 /* If found a slot, translate and return */
2915 if (done) {
2916 if (corner == C_NW || corner == C_NE)
2917 *y_pos = y;
2918 else
2919 *y_pos = -y;
2921 if (corner == C_NE || corner == C_SE)
2922 *x_pos = x;
2923 else
2924 *x_pos = -x;
2926 return True;
2928 /* else, try to find a slot somewhere else */
2931 /* a map of mwidth x mwidth would be enough if we allowed icons to be
2932 * placed outside of screen */
2933 mwidth = (int)ceil(sqrt(dock->max_icons));
2935 /* In the worst case (the clip is in the corner of the screen),
2936 * the amount of icons that fit in the clip is smaller.
2937 * Double the map to get a safe value.
2939 mwidth += mwidth;
2941 r = (mwidth - 1) / 2;
2943 slot_map = wmalloc(mwidth * mwidth);
2945 #define XY2OFS(x,y) (WMAX(abs(x),abs(y)) > r) ? 0 : (((y)+r)*(mwidth)+(x)+r)
2947 /* mark used slots in the map. If the slot falls outside the map
2948 * (for example, when all icons are placed in line), ignore them. */
2949 for (i = 0; i < dock->max_icons; i++) {
2950 btn = dock->icon_array[i];
2951 if (btn)
2952 slot_map[XY2OFS(btn->xindex, btn->yindex)] = 1;
2955 for (chain = scr->global_icons; chain != NULL; chain = chain->next)
2956 slot_map[XY2OFS(chain->aicon->xindex, chain->aicon->yindex)] = 1;
2958 /* Find closest slot from the center that is free by scanning the
2959 * map from the center to outward in circular passes.
2960 * This will not result in a neat layout, but will be optimal
2961 * in the sense that there will not be holes left.
2963 done = 0;
2964 for (i = 1; i <= r && !done; i++) {
2965 int tx, ty;
2967 /* top and bottom parts of the ring */
2968 for (x = -i; x <= i && !done; x++) {
2969 tx = dock->x_pos + x * ICON_SIZE;
2970 y = -i;
2971 ty = dock->y_pos + y * ICON_SIZE;
2972 if (slot_map[XY2OFS(x, y)] == 0 && onScreen(scr, tx, ty)) {
2973 *x_pos = x;
2974 *y_pos = y;
2975 done = 1;
2976 break;
2978 y = i;
2979 ty = dock->y_pos + y * ICON_SIZE;
2980 if (slot_map[XY2OFS(x, y)] == 0 && onScreen(scr, tx, ty)) {
2981 *x_pos = x;
2982 *y_pos = y;
2983 done = 1;
2984 break;
2987 /* left and right parts of the ring */
2988 for (y = -i + 1; y <= i - 1; y++) {
2989 ty = dock->y_pos + y * ICON_SIZE;
2990 x = -i;
2991 tx = dock->x_pos + x * ICON_SIZE;
2992 if (slot_map[XY2OFS(x, y)] == 0 && onScreen(scr, tx, ty)) {
2993 *x_pos = x;
2994 *y_pos = y;
2995 done = 1;
2996 break;
2998 x = i;
2999 tx = dock->x_pos + x * ICON_SIZE;
3000 if (slot_map[XY2OFS(x, y)] == 0 && onScreen(scr, tx, ty)) {
3001 *x_pos = x;
3002 *y_pos = y;
3003 done = 1;
3004 break;
3008 wfree(slot_map);
3009 #undef XY2OFS
3010 return done;
3013 static void moveDock(WDock *dock, int new_x, int new_y)
3015 WAppIcon *btn;
3016 WDrawerChain *dc;
3017 int i;
3019 if (dock->type == WM_DOCK) {
3020 for (dc = dock->screen_ptr->drawers; dc != NULL; dc = dc->next)
3021 moveDock(dc->adrawer, new_x, dc->adrawer->y_pos - dock->y_pos + new_y);
3024 dock->x_pos = new_x;
3025 dock->y_pos = new_y;
3026 for (i = 0; i < dock->max_icons; i++) {
3027 btn = dock->icon_array[i];
3028 if (btn) {
3029 btn->x_pos = new_x + btn->xindex * ICON_SIZE;
3030 btn->y_pos = new_y + btn->yindex * ICON_SIZE;
3031 XMoveWindow(dpy, btn->icon->core->window, btn->x_pos, btn->y_pos);
3036 void wDockSwap(WDock *dock)
3038 WScreen *scr = dock->screen_ptr;
3039 WAppIcon *btn;
3040 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 if (drawer->on_right_side)
4623 ai->xindex = -abs(ai->xindex);
4624 else
4625 ai->xindex = abs(ai->xindex);
4626 ai->x_pos = new_x + ai->xindex * ICON_SIZE;
4628 /* Update drawer's tile */
4629 if (i == 0) {
4630 wIconUpdate(ai->icon);
4631 wDrawerIconPaint(ai);
4633 XMoveWindow(dpy, ai->icon->core->window, ai->x_pos, ai->y_pos);
4638 static void swapDrawers(WScreen *scr, int new_x)
4640 WDrawerChain *dc;
4642 if (scr->drawer_tile)
4643 RReleaseImage(scr->drawer_tile);
4645 scr->drawer_tile = wDrawerMakeTile(scr, scr->icon_tile);
4647 for (dc = scr->drawers; dc != NULL; dc = dc->next)
4648 swapDrawer(dc->adrawer, new_x);
4652 int wIsADrawer(WAppIcon *aicon)
4654 return aicon && aicon->dock &&
4655 aicon->dock->type == WM_DRAWER && aicon->dock->icon_array[0] == aicon;
4659 static WDock* getDrawer(WScreen *scr, int y_index)
4661 WDrawerChain *dc;
4663 for (dc = scr->drawers; dc != NULL; dc = dc->next) {
4664 if (dc->adrawer->y_pos - scr->dock->y_pos == y_index * ICON_SIZE)
4665 return dc->adrawer;
4667 return NULL;
4672 /* Find the "hole" a moving appicon created when snapped into the
4673 * drawer. redocking is a boolean. If the moving appicon comes from the
4674 * drawer, drawer->icon_count is correct. If not, redocking is then false and
4675 * there are now drawer->icon_count plus one appicons in the drawer. */
4676 static int indexOfHole(WDock *drawer, WAppIcon *moving_aicon, int redocking)
4678 int index_of_hole, i;
4680 /* Classic interview question...
4682 * We have n-1 (n = drawer->icon_count-1 or drawer->icon_count, see
4683 * redocking) appicons, whose xindex are unique in [1..n]. One is missing:
4684 * that's where the ghost of the moving appicon is, that's what the
4685 * function should return.
4687 * We compute 1+2+...+n (this sum is equal to n*(n+1)/2), we subtract to
4688 * this sum the xindex of each of the n-1 appicons, and we get the correct
4689 * index! */
4691 if (redocking) {
4692 index_of_hole = (drawer->icon_count - 1) * drawer->icon_count / 2;
4693 } else {
4694 index_of_hole = drawer->icon_count * (drawer->icon_count + 1) / 2;
4696 index_of_hole *= (drawer->on_right_side ? -1 : 1);
4698 for (i = 1; i < drawer->max_icons; i++) {
4699 if (drawer->icon_array[i] && drawer->icon_array[i] != moving_aicon)
4700 index_of_hole -= drawer->icon_array[i]->xindex;
4702 /* wmessage(" Index of the moving appicon is %d (%sredocking)", index_of_hole, (redocking ? "" : "not ")); */
4703 if (abs(index_of_hole) > abs(drawer->icon_count) - (redocking ? 1 : 0))
4704 wwarning(" index_of_hole is too large ! (%d greater than %d)",
4705 index_of_hole, abs(drawer->icon_count) - (redocking ? 1 : 0));
4706 if (index_of_hole == 0)
4707 wwarning(" index_of_hole == 0 (%sredocking, icon_count == %d)", (redocking ? "" : "not "), drawer->icon_count);
4709 return index_of_hole;
4713 void wSlideAppicons(WAppIcon **appicons, int n, int to_the_left)
4715 int i;
4716 int leftmost = -1, min_index = 9999, from_x = -1; // leftmost and from_x initialized to avoid warning
4717 Window wins[n];
4718 WAppIcon *aicon;
4720 if (n < 1)
4721 return;
4723 for (i = 0; i < n; i++) {
4724 aicon = appicons[i];
4725 aicon->xindex += (to_the_left ? -1 : +1);
4726 if (aicon->xindex < min_index) {
4727 min_index = aicon->xindex;
4728 leftmost = i;
4729 from_x = aicon->x_pos;
4731 aicon->x_pos += (to_the_left ? -ICON_SIZE : +ICON_SIZE);
4734 for (i = 0; i < n; i++) {
4735 aicon = appicons[i];
4736 wins[aicon->xindex - min_index] = aicon->icon->core->window;
4738 aicon = appicons[leftmost];
4739 slide_windows(wins, n, from_x, aicon->y_pos, aicon->x_pos, aicon->y_pos);
4743 void wDrawerFillTheGap(WDock *drawer, WAppIcon *aicon, Bool redocking)
4745 int i, j;
4746 int index_of_hole = indexOfHole(drawer, aicon, redocking);
4747 WAppIcon *aicons_to_shift[drawer->icon_count];
4749 j = 0;
4750 for (i = 0; i < drawer->max_icons; i++) {
4751 WAppIcon *ai = drawer->icon_array[i];
4752 if (ai && ai != aicon &&
4753 abs(ai->xindex) > abs(index_of_hole))
4754 aicons_to_shift[j++] = ai;
4756 if (j != drawer->icon_count - abs(index_of_hole) - (redocking ? 1 : 0))
4757 wwarning("Removing aicon at index %d from %s: j=%d but should be %d",
4758 index_of_hole, drawer->icon_array[0]->wm_instance,
4759 j, drawer->icon_count - abs(index_of_hole) - (redocking ? 1 : 0));
4760 wSlideAppicons(aicons_to_shift, j, !drawer->on_right_side);
4764 static void drawerConsolidateIcons(WDock *drawer)
4766 WAppIcon *aicons_to_shift[drawer->icon_count];
4767 int maxRemaining = 0;
4768 int sum = 0;
4769 int i;
4770 for (i = 0; i < drawer->max_icons; i++) {
4771 WAppIcon *ai = drawer->icon_array[i];
4772 if (ai == NULL)
4773 continue;
4774 sum += abs(ai->xindex);
4775 if (abs(ai->xindex) > maxRemaining)
4776 maxRemaining = abs(ai->xindex);
4778 while (sum != maxRemaining * (maxRemaining + 1) / 2) { // while there is a hole
4779 WAppIcon *ai;
4780 int n;
4781 // Look up for the hole at max position
4782 int maxDeleted;
4783 for (maxDeleted = maxRemaining - 1; maxDeleted > 0; maxDeleted--) {
4784 Bool foundAppIconThere = False;
4785 for (i = 0; i < drawer->max_icons; i++) {
4786 WAppIcon *ai = drawer->icon_array[i];
4787 if (ai == NULL)
4788 continue;
4789 if (abs(ai->xindex) == maxDeleted) {
4790 foundAppIconThere = True;
4791 break;
4794 if (!foundAppIconThere)
4795 break;
4797 assert(maxDeleted > 0); // would mean while test is wrong
4798 n = 0;
4799 for (i = 0; i < drawer->max_icons; i++) {
4800 ai = drawer->icon_array[i];
4801 if (ai != NULL && abs(ai->xindex) > maxDeleted)
4802 aicons_to_shift[n++] = ai;
4804 assert(n == maxRemaining - maxDeleted); // for the code review ;-)
4805 wSlideAppicons(aicons_to_shift, n, !drawer->on_right_side);
4806 // Efficient beancounting
4807 maxRemaining -= 1;
4808 sum -= n;
4814 /* similar to wDockRestoreState, but a lot a specific stuff too... */
4815 static WDock * drawerRestoreState(WScreen *scr, WMPropList *drawer_state)
4817 WDock *drawer;
4818 WMPropList *apps, *value, *dock_state;
4819 WAppIcon *aicon;
4820 int count, i;
4822 if (!drawer_state)
4823 return NULL;
4825 make_keys();
4827 WMRetainPropList(drawer_state);
4829 /* Get the instance name, and create a drawer */
4830 value = WMGetFromPLDictionary(drawer_state, dName);
4831 drawer = wDockCreate(scr, WM_DRAWER, WMGetFromPLString(value));
4833 /* restore DnD command and paste command */
4834 #ifdef USE_DOCK_XDND
4835 value = WMGetFromPLDictionary(drawer_state, dDropCommand);
4836 if (value && WMIsPLString(value))
4837 drawer->icon_array[0]->dnd_command = wstrdup(WMGetFromPLString(value));
4838 #endif /* USE_DOCK_XDND */
4840 value = WMGetFromPLDictionary(drawer_state, dPasteCommand);
4841 if (value && WMIsPLString(value))
4842 drawer->icon_array[0]->paste_command = wstrdup(WMGetFromPLString(value));
4844 /* restore position */
4845 value = WMGetFromPLDictionary(drawer_state, dPosition);
4846 if (!value || !WMIsPLString(value))
4847 COMPLAIN("Position");
4848 else {
4849 int x, y, y_index;
4850 if (sscanf(WMGetFromPLString(value), "%i,%i", &x, &y) != 2)
4851 COMPLAIN("Position");
4853 /* check position sanity */
4854 if (x != scr->dock->x_pos) {
4855 x = scr->dock->x_pos;
4857 y_index = (y - scr->dock->y_pos) / ICON_SIZE;
4858 if (y_index >= scr->dock->max_icons) {
4859 /* Here we should do something more intelligent, since it
4860 * can happen even if the user hasn't hand-edited his
4861 * G/D/State file (but uses a lower resolution). */
4862 y_index = scr->dock->max_icons - 1;
4864 y = scr->dock->y_pos + y_index * ICON_SIZE;
4865 moveDock(drawer, x, y);
4868 /* restore dock properties (applist and others) */
4869 dock_state = WMGetFromPLDictionary(drawer_state, dDock);
4871 /* restore lowered/raised state: same as scr->dock, no matter what */
4872 drawer->lowered = scr->dock->lowered;
4873 if (!drawer->lowered)
4874 ChangeStackingLevel(drawer->icon_array[0]->icon->core, WMDockLevel);
4875 else
4876 ChangeStackingLevel(drawer->icon_array[0]->icon->core, WMNormalLevel);
4877 wRaiseFrame(drawer->icon_array[0]->icon->core);
4879 /* restore collapsed state */
4880 drawer->collapsed = 0;
4881 value = WMGetFromPLDictionary(dock_state, dCollapsed);
4882 if (value && strcasecmp(WMGetFromPLString(value), "YES") == 0) {
4883 drawer->collapsed = 1;
4885 /* restore auto-collapsed state */
4886 value = WMGetFromPLDictionary(dock_state, dAutoCollapse);
4887 if (value && strcasecmp(WMGetFromPLString(value), "YES") == 0) {
4888 drawer->auto_collapse = 1;
4889 drawer->collapsed = 1;
4890 } else {
4891 drawer->auto_collapse = 0; // because wDockCreate sets it (drawers only)
4894 /* restore auto-raise/lower state: same as scr->dock, no matter what */
4895 drawer->auto_raise_lower = scr->dock->auto_raise_lower;
4897 /* restore attract icons state */
4898 drawer->attract_icons = 0;
4899 value = WMGetFromPLDictionary(dock_state, dAutoAttractIcons);
4900 if (value && strcasecmp(WMGetFromPLString(value), "YES") == 0) {
4901 drawer->attract_icons = 1;
4902 scr->attracting_drawer = drawer;
4905 /* application list */
4906 apps = WMGetFromPLDictionary(dock_state, dApplications);
4908 if (!apps) {
4909 goto finish;
4912 count = WMGetPropListItemCount(apps);
4914 if (count == 0)
4915 goto finish;
4917 for (i=0; i<count; i++) {
4918 if (drawer->icon_count >= drawer->max_icons) {
4919 wwarning(_("there are too many icons stored in drawer. Ignoring what doesn't fit"));
4920 break;
4923 value = WMGetFromPLArray(apps, i);
4924 aicon = restore_icon_state(scr, value, WM_DRAWER, drawer->icon_count);
4926 drawer->icon_array[drawer->icon_count] = aicon;
4928 if (aicon) {
4929 /* don't trust the stored direction sign as the dock is
4930 * dynamically positioned depending on the screen size
4932 if (drawer->screen_ptr->dock->on_right_side)
4933 aicon->xindex = -abs(aicon->xindex);
4934 else
4935 aicon->xindex = abs(aicon->xindex);
4936 aicon->dock = drawer;
4937 aicon->x_pos = drawer->x_pos + (aicon->xindex * ICON_SIZE);
4938 aicon->y_pos = drawer->y_pos + (aicon->yindex * ICON_SIZE);
4940 if (!drawer->lowered)
4941 ChangeStackingLevel(aicon->icon->core, WMDockLevel);
4942 else
4943 ChangeStackingLevel(aicon->icon->core, WMNormalLevel);
4945 wCoreConfigure(aicon->icon->core, aicon->x_pos, aicon->y_pos, 0, 0);
4947 if (!drawer->collapsed)
4948 XMapWindow(dpy, aicon->icon->core->window);
4949 wRaiseFrame(aicon->icon->core);
4951 drawer->icon_count++;
4955 finish:
4956 WMReleasePropList(drawer_state);
4958 return drawer;
4962 /* Same kind of comment than for previous function: this function is
4963 * very similar to make_icon_state, but has substential differences as
4964 * well. */
4965 static WMPropList *drawerSaveState(WDock *drawer)
4967 WMPropList *pstr, *drawer_state;
4968 WAppIcon *ai;
4969 char buffer[64];
4971 ai = drawer->icon_array[0];
4972 /* Store its name */
4973 pstr = WMCreatePLString(ai->wm_instance);
4974 drawer_state = WMCreatePLDictionary(dName, pstr, NULL); /* we need this final NULL */
4975 WMReleasePropList(pstr);
4977 /* Store its position */
4978 snprintf(buffer, sizeof(buffer), "%i,%i", ai->x_pos, ai->y_pos);
4979 pstr = WMCreatePLString(buffer);
4980 WMPutInPLDictionary(drawer_state, dPosition, pstr);
4981 WMReleasePropList(pstr);
4983 #ifdef USE_DOCK_XDND
4984 /* Store its DnD command */
4985 if (ai->dnd_command) {
4986 pstr = WMCreatePLString(ai->dnd_command);
4987 WMPutInPLDictionary(drawer_state, dDropCommand, pstr);
4988 WMReleasePropList(pstr);
4990 #endif /* USE_DOCK_XDND */
4992 /* Store its paste command */
4993 if (ai->paste_command) {
4994 pstr = WMCreatePLString(ai->paste_command);
4995 WMPutInPLDictionary(drawer_state, dPasteCommand, pstr);
4996 WMReleasePropList(pstr);
4999 /* Store applications list and other properties */
5000 pstr = dockSaveState(drawer);
5001 WMPutInPLDictionary(drawer_state, dDock, pstr);
5002 WMReleasePropList(pstr);
5004 return drawer_state;
5008 void wDrawersSaveState(WScreen *scr)
5010 WMPropList *all_drawers, *drawer_state;
5011 int i;
5012 WDrawerChain *dc;
5014 make_keys();
5016 all_drawers = WMCreatePLArray(NULL);
5017 for (i=0, dc = scr->drawers;
5018 i < scr->drawer_count;
5019 i++, dc = dc->next) {
5020 drawer_state = drawerSaveState(dc->adrawer);
5021 WMAddToPLArray(all_drawers, drawer_state);
5022 WMReleasePropList(drawer_state);
5024 WMPutInPLDictionary(scr->session_state, dDrawers, all_drawers);
5025 WMReleasePropList(all_drawers);
5029 void wDrawersRestoreState(WScreen *scr)
5031 WMPropList *all_drawers, *drawer_state;
5032 int i;
5034 make_keys();
5036 if (scr->session_state == NULL)
5037 return;
5039 all_drawers = WMGetFromPLDictionary(scr->session_state, dDrawers);
5040 if (!all_drawers)
5041 return;
5043 for (i = 0; i < WMGetPropListItemCount(all_drawers); i++) {
5044 drawer_state = WMGetFromPLArray(all_drawers, i);
5045 drawerRestoreState(scr, drawer_state);
5046 // Note: scr->drawers was updated when the the drawer was created