wmaker: Removed global variable 'WDelayedActionSet' and associated dead code
[wmaker-crm.git] / src / dock.c
bloba6b4d999aa7de2400bd46cf180e6d169551a2be0
1 /* dock.c- built-in Dock module for WindowMaker
3 * Window Maker window manager
5 * Copyright (c) 1997-2003 Alfredo K. Kojima
6 * Copyright (c) 1998-2003 Dan Pascu
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
23 #include "wconfig.h"
25 #include <X11/Xlib.h>
26 #include <X11/Xutil.h>
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <libgen.h>
30 #include <string.h>
31 #include <strings.h>
32 #include <unistd.h>
33 #include <math.h>
34 #include <limits.h>
36 #ifndef PATH_MAX
37 #define PATH_MAX DEFAULT_PATH_MAX
38 #endif
40 #include "WindowMaker.h"
41 #include "wcore.h"
42 #include "window.h"
43 #include "icon.h"
44 #include "appicon.h"
45 #include "actions.h"
46 #include "stacking.h"
47 #include "dock.h"
48 #include "dockedapp.h"
49 #include "dialog.h"
50 #include "main.h"
51 #include "properties.h"
52 #include "menu.h"
53 #include "client.h"
54 #include "defaults.h"
55 #include "workspace.h"
56 #include "framewin.h"
57 #include "superfluous.h"
58 #include "xinerama.h"
59 #include "placement.h"
60 #include "misc.h"
61 #include "event.h"
63 /**** Local variables ****/
64 #define CLIP_REWIND 1
65 #define CLIP_IDLE 0
66 #define CLIP_FORWARD 2
68 #define MOD_MASK wPreferences.modifier_mask
69 #define ICON_SIZE wPreferences.icon_size
71 /***** Local variables ****/
73 static WMPropList *dCommand = NULL;
74 static WMPropList *dPasteCommand = NULL;
75 #ifdef XDND /* XXX was OFFIX */
76 static WMPropList *dDropCommand = NULL;
77 #endif
78 static WMPropList *dAutoLaunch, *dLock;
79 static WMPropList *dName, *dForced, *dBuggyApplication, *dYes, *dNo;
80 static WMPropList *dHost, *dDock, *dClip;
81 static WMPropList *dAutoAttractIcons;
83 static WMPropList *dPosition, *dApplications, *dLowered, *dCollapsed;
85 static WMPropList *dAutoCollapse, *dAutoRaiseLower, *dOmnipresent;
87 static WMPropList *dDrawers = NULL;
89 static void dockIconPaint(WAppIcon *btn);
91 static void iconMouseDown(WObjDescriptor *desc, XEvent *event);
93 static pid_t execCommand(WAppIcon *btn, const char *command, WSavedState *state);
95 static void trackDeadProcess(pid_t pid, unsigned char status, WDock *dock);
97 static int getClipButton(int px, int py);
99 static void toggleLowered(WDock *dock);
101 static void toggleCollapsed(WDock *dock);
103 static void clipIconExpose(WObjDescriptor *desc, XEvent *event);
105 static void clipLeave(WDock *dock);
107 static void handleClipChangeWorkspace(WScreen *scr, XEvent *event);
109 static void clipEnterNotify(WObjDescriptor *desc, XEvent *event);
110 static void clipLeaveNotify(WObjDescriptor *desc, XEvent *event);
111 static void clipAutoCollapse(void *cdata);
112 static void clipAutoExpand(void *cdata);
113 static void launchDockedApplication(WAppIcon *btn, Bool withSelection);
115 static void clipAutoLower(void *cdata);
116 static void clipAutoRaise(void *cdata);
117 static WAppIcon *mainIconCreate(WScreen *scr, int type, const char *name);
119 static void drawerIconExpose(WObjDescriptor *desc, XEvent *event);
120 static void removeDrawerCallback(WMenu *menu, WMenuEntry *entry);
121 static void drawerAppendToChain(WScreen *scr, WDock *drawer);
122 static char *findUniqueName(WScreen *scr, const char *instance_basename);
123 static void addADrawerCallback(WMenu *menu, WMenuEntry *entry);
124 static void swapDrawers(WScreen *scr, int on_right_side, int new_x);
125 static WDock* getDrawer(WScreen *scr, int y_index);
126 static int indexOfHole(WDock *drawer, WAppIcon *moving_aicon, int redocking);
127 static void drawerConsolidateIcons(WDock *drawer);
129 static int onScreen(WScreen *scr, int x, int y);
131 static void make_keys(void)
133 if (dCommand != NULL)
134 return;
136 dCommand = WMRetainPropList(WMCreatePLString("Command"));
137 dPasteCommand = WMRetainPropList(WMCreatePLString("PasteCommand"));
138 #ifdef XDND
139 dDropCommand = WMRetainPropList(WMCreatePLString("DropCommand"));
140 #endif
141 dLock = WMRetainPropList(WMCreatePLString("Lock"));
142 dAutoLaunch = WMRetainPropList(WMCreatePLString("AutoLaunch"));
143 dName = WMRetainPropList(WMCreatePLString("Name"));
144 dForced = WMRetainPropList(WMCreatePLString("Forced"));
145 dBuggyApplication = WMRetainPropList(WMCreatePLString("BuggyApplication"));
146 dYes = WMRetainPropList(WMCreatePLString("Yes"));
147 dNo = WMRetainPropList(WMCreatePLString("No"));
148 dHost = WMRetainPropList(WMCreatePLString("Host"));
150 dPosition = WMCreatePLString("Position");
151 dApplications = WMCreatePLString("Applications");
152 dLowered = WMCreatePLString("Lowered");
153 dCollapsed = WMCreatePLString("Collapsed");
154 dAutoCollapse = WMCreatePLString("AutoCollapse");
155 dAutoRaiseLower = WMCreatePLString("AutoRaiseLower");
156 dAutoAttractIcons = WMCreatePLString("AutoAttractIcons");
158 dOmnipresent = WMCreatePLString("Omnipresent");
160 dDock = WMCreatePLString("Dock");
161 dClip = WMCreatePLString("Clip");
162 dDrawers = WMCreatePLString("Drawers");
165 static void renameCallback(WMenu *menu, WMenuEntry *entry)
167 WDock *dock = entry->clientdata;
168 char buffer[128];
169 int wspace;
170 char *name;
172 assert(entry->clientdata != NULL);
174 wspace = w_global.workspace.current;
176 name = wstrdup(w_global.workspace.array[wspace]->name);
178 snprintf(buffer, sizeof(buffer), _("Type the name for workspace %i:"), wspace + 1);
179 if (wInputDialog(dock->screen_ptr, _("Rename Workspace"), buffer, &name))
180 wWorkspaceRename(dock->screen_ptr, wspace, name);
182 wfree(name);
185 static void toggleLoweredCallback(WMenu *menu, WMenuEntry *entry)
187 assert(entry->clientdata != NULL);
189 toggleLowered(entry->clientdata);
191 entry->flags.indicator_on = !(((WDock *) entry->clientdata)->lowered);
193 wMenuPaint(menu);
196 static int matchWindow(const void *item, const void *cdata)
198 return (((WFakeGroupLeader *) item)->leader == (Window) cdata);
201 static void killCallback(WMenu *menu, WMenuEntry *entry)
203 WScreen *scr = menu->menu->screen_ptr;
204 WAppIcon *icon;
205 WFakeGroupLeader *fPtr;
206 char *buffer, *shortname, **argv;
207 int argc;
209 if (!WCHECK_STATE(WSTATE_NORMAL))
210 return;
212 assert(entry->clientdata != NULL);
214 icon = (WAppIcon *) entry->clientdata;
216 icon->editing = 1;
218 WCHANGE_STATE(WSTATE_MODAL);
220 /* strip away dir names */
221 shortname = basename(icon->command);
222 /* separate out command options */
223 wtokensplit(shortname, &argv, &argc);
225 buffer = wstrconcat(argv[0],
226 _(" will be forcibly closed.\n"
227 "Any unsaved changes will be lost.\n" "Please confirm."));
229 if (icon->icon && icon->icon->owner) {
230 fPtr = icon->icon->owner->fake_group;
231 } else {
232 /* is this really necessary? can we kill a non-running dock icon? */
233 Window win = icon->main_window;
234 int index;
236 index = WMFindInArray(scr->fakeGroupLeaders, matchWindow, (void *)win);
237 if (index != WANotFound)
238 fPtr = WMGetFromArray(scr->fakeGroupLeaders, index);
239 else
240 fPtr = NULL;
243 if (wPreferences.dont_confirm_kill
244 || wMessageDialog(menu->frame->screen_ptr, _("Kill Application"),
245 buffer, _("Yes"), _("No"), NULL) == WAPRDefault) {
246 if (fPtr != NULL) {
247 WWindow *wwin, *twin;
249 wwin = scr->focused_window;
250 while (wwin) {
251 twin = wwin->prev;
252 if (wwin->fake_group == fPtr)
253 wClientKill(wwin);
255 wwin = twin;
257 } else if (icon->icon && icon->icon->owner) {
258 wClientKill(icon->icon->owner);
262 wfree(buffer);
263 wtokenfree(argv, argc);
265 icon->editing = 0;
267 WCHANGE_STATE(WSTATE_NORMAL);
270 /* TODO: replace this function with a member of the dock struct */
271 static int numberOfSelectedIcons(WDock *dock)
273 WAppIcon *aicon;
274 int i, n;
276 n = 0;
277 for (i = 1; i < dock->max_icons; i++) {
278 aicon = dock->icon_array[i];
279 if (aicon && aicon->icon->selected)
280 n++;
283 return n;
286 static WMArray *getSelected(WDock *dock)
288 WMArray *ret = WMCreateArray(8);
289 WAppIcon *btn;
290 int i;
292 for (i = 1; i < dock->max_icons; i++) {
293 btn = dock->icon_array[i];
294 if (btn && btn->icon->selected)
295 WMAddToArray(ret, btn);
298 return ret;
301 static void paintClipButtons(WAppIcon *clipIcon, Bool lpushed, Bool rpushed)
303 Window win = clipIcon->icon->core->window;
304 WScreen *scr = clipIcon->icon->core->screen_ptr;
305 XPoint p[4];
306 int pt = CLIP_BUTTON_SIZE * ICON_SIZE / 64;
307 int tp = ICON_SIZE - pt;
308 int as = pt - 15; /* 15 = 5+5+5 */
309 GC gc = scr->draw_gc; /* maybe use WMColorGC() instead here? */
310 WMColor *color;
312 color = scr->clip_title_color[CLIP_NORMAL];
314 XSetForeground(dpy, gc, WMColorPixel(color));
316 if (rpushed) {
317 p[0].x = tp + 1;
318 p[0].y = 1;
319 p[1].x = ICON_SIZE - 2;
320 p[1].y = 1;
321 p[2].x = ICON_SIZE - 2;
322 p[2].y = pt - 1;
323 } else if (lpushed) {
324 p[0].x = 1;
325 p[0].y = tp;
326 p[1].x = pt;
327 p[1].y = ICON_SIZE - 2;
328 p[2].x = 1;
329 p[2].y = ICON_SIZE - 2;
331 if (lpushed || rpushed) {
332 XSetForeground(dpy, scr->draw_gc, scr->white_pixel);
333 XFillPolygon(dpy, win, scr->draw_gc, p, 3, Convex, CoordModeOrigin);
334 XSetForeground(dpy, scr->draw_gc, scr->black_pixel);
337 /* top right arrow */
338 p[0].x = p[3].x = ICON_SIZE - 5 - as;
339 p[0].y = p[3].y = 5;
340 p[1].x = ICON_SIZE - 6;
341 p[1].y = 5;
342 p[2].x = ICON_SIZE - 6;
343 p[2].y = 4 + as;
344 if (rpushed) {
345 XFillPolygon(dpy, win, scr->draw_gc, p, 3, Convex, CoordModeOrigin);
346 XDrawLines(dpy, win, scr->draw_gc, p, 4, CoordModeOrigin);
347 } else {
348 XFillPolygon(dpy, win, gc, p, 3, Convex, CoordModeOrigin);
349 XDrawLines(dpy, win, gc, p, 4, CoordModeOrigin);
352 /* bottom left arrow */
353 p[0].x = p[3].x = 5;
354 p[0].y = p[3].y = ICON_SIZE - 5 - as;
355 p[1].x = 5;
356 p[1].y = ICON_SIZE - 6;
357 p[2].x = 4 + as;
358 p[2].y = ICON_SIZE - 6;
359 if (lpushed) {
360 XFillPolygon(dpy, win, scr->draw_gc, p, 3, Convex, CoordModeOrigin);
361 XDrawLines(dpy, win, scr->draw_gc, p, 4, CoordModeOrigin);
362 } else {
363 XFillPolygon(dpy, win, gc, p, 3, Convex, CoordModeOrigin);
364 XDrawLines(dpy, win, gc, p, 4, CoordModeOrigin);
368 RImage *wClipMakeTile(WScreen *scr, RImage *normalTile)
370 RImage *tile = RCloneImage(normalTile);
371 RColor black;
372 RColor dark;
373 RColor light;
374 int pt, tp;
375 int as;
377 pt = CLIP_BUTTON_SIZE * wPreferences.icon_size / 64;
378 tp = wPreferences.icon_size - 1 - pt;
379 as = pt - 15;
381 black.alpha = 255;
382 black.red = black.green = black.blue = 0;
384 dark.alpha = 0;
385 dark.red = dark.green = dark.blue = 60;
387 light.alpha = 0;
388 light.red = light.green = light.blue = 80;
390 /* top right */
391 ROperateLine(tile, RSubtractOperation, tp, 0, wPreferences.icon_size - 2, pt - 1, &dark);
392 RDrawLine(tile, tp - 1, 0, wPreferences.icon_size - 1, pt + 1, &black);
393 ROperateLine(tile, RAddOperation, tp, 2, wPreferences.icon_size - 3, pt, &light);
395 /* arrow bevel */
396 ROperateLine(tile, RSubtractOperation, ICON_SIZE - 7 - as, 4, ICON_SIZE - 5, 4, &dark);
397 ROperateLine(tile, RSubtractOperation, ICON_SIZE - 6 - as, 5, ICON_SIZE - 5, 6 + as, &dark);
398 ROperateLine(tile, RAddOperation, ICON_SIZE - 5, 4, ICON_SIZE - 5, 6 + as, &light);
400 /* bottom left */
401 ROperateLine(tile, RAddOperation, 2, tp + 2, pt - 2, wPreferences.icon_size - 3, &dark);
402 RDrawLine(tile, 0, tp - 1, pt + 1, wPreferences.icon_size - 1, &black);
403 ROperateLine(tile, RSubtractOperation, 0, tp - 2, pt + 1, wPreferences.icon_size - 2, &light);
405 /* arrow bevel */
406 ROperateLine(tile, RSubtractOperation, 4, ICON_SIZE - 7 - as, 4, ICON_SIZE - 5, &dark);
407 ROperateLine(tile, RSubtractOperation, 5, ICON_SIZE - 6 - as, 6 + as, ICON_SIZE - 5, &dark);
408 ROperateLine(tile, RAddOperation, 4, ICON_SIZE - 5, 6 + as, ICON_SIZE - 5, &light);
410 return tile;
413 static void omnipresentCallback(WMenu *menu, WMenuEntry *entry)
415 WAppIcon *clickedIcon = entry->clientdata;
416 WAppIcon *aicon;
417 WDock *dock;
418 WMArray *selectedIcons;
419 WMArrayIterator iter;
420 int failed;
422 assert(entry->clientdata != NULL);
424 dock = clickedIcon->dock;
426 selectedIcons = getSelected(dock);
428 if (!WMGetArrayItemCount(selectedIcons))
429 WMAddToArray(selectedIcons, clickedIcon);
431 failed = 0;
432 WM_ITERATE_ARRAY(selectedIcons, aicon, iter) {
433 if (wClipMakeIconOmnipresent(aicon, !aicon->omnipresent) == WO_FAILED)
434 failed++;
435 else if (aicon->icon->selected)
436 wIconSelect(aicon->icon);
438 WMFreeArray(selectedIcons);
440 if (failed > 1) {
441 wMessageDialog(dock->screen_ptr, _("Warning"),
442 _("Some icons cannot be made omnipresent. "
443 "Please make sure that no other icon is "
444 "docked in the same positions on the other "
445 "workspaces and the Clip is not full in "
446 "some workspace."), _("OK"), NULL, NULL);
447 } else if (failed == 1) {
448 wMessageDialog(dock->screen_ptr, _("Warning"),
449 _("Icon cannot be made omnipresent. "
450 "Please make sure that no other icon is "
451 "docked in the same position on the other "
452 "workspaces and the Clip is not full in "
453 "some workspace."), _("OK"), NULL, NULL);
457 static void removeIcons(WMArray *icons, WDock *dock)
459 WAppIcon *aicon;
460 int keepit;
461 WMArrayIterator it;
463 WM_ITERATE_ARRAY(icons, aicon, it) {
464 keepit = aicon->running && wApplicationOf(aicon->main_window);
465 wDockDetach(dock, aicon);
466 if (keepit) {
467 /* XXX: can: aicon->icon == NULL ? */
468 PlaceIcon(dock->screen_ptr, &aicon->x_pos, &aicon->y_pos,
469 wGetHeadForWindow(aicon->icon->owner));
470 XMoveWindow(dpy, aicon->icon->core->window, aicon->x_pos, aicon->y_pos);
471 if (!dock->mapped || dock->collapsed)
472 XMapWindow(dpy, aicon->icon->core->window);
475 WMFreeArray(icons);
477 if (wPreferences.auto_arrange_icons)
478 wArrangeIcons(dock->screen_ptr, True);
481 static void removeIconsCallback(WMenu *menu, WMenuEntry *entry)
483 WAppIcon *clickedIcon = (WAppIcon *) entry->clientdata;
484 WDock *dock;
485 WMArray *selectedIcons;
487 assert(clickedIcon != NULL);
489 dock = clickedIcon->dock;
491 selectedIcons = getSelected(dock);
493 if (WMGetArrayItemCount(selectedIcons)) {
494 if (wMessageDialog(dock->screen_ptr,
495 dock->type == WM_CLIP ? _("Workspace Clip") : _("Drawer"),
496 _("All selected icons will be removed!"),
497 _("OK"), _("Cancel"), NULL) != WAPRDefault) {
498 WMFreeArray(selectedIcons);
499 return;
501 } else {
502 if (clickedIcon->xindex == 0 && clickedIcon->yindex == 0) {
503 WMFreeArray(selectedIcons);
504 return;
506 WMAddToArray(selectedIcons, clickedIcon);
509 removeIcons(selectedIcons, dock);
511 if (dock->type == WM_DRAWER) {
512 drawerConsolidateIcons(dock);
516 static void keepIconsCallback(WMenu *menu, WMenuEntry *entry)
518 WAppIcon *clickedIcon = (WAppIcon *) entry->clientdata;
519 WDock *dock;
520 WAppIcon *aicon;
521 WMArray *selectedIcons;
522 WMArrayIterator it;
524 assert(clickedIcon != NULL);
525 dock = clickedIcon->dock;
527 selectedIcons = getSelected(dock);
529 if (!WMGetArrayItemCount(selectedIcons)
530 && clickedIcon != w_global.clip.icon) {
531 char *command = NULL;
533 if (!clickedIcon->command && !clickedIcon->editing) {
534 clickedIcon->editing = 1;
535 if (wInputDialog(dock->screen_ptr, _("Keep Icon"),
536 _("Type the command used to launch the application"), &command)) {
537 if (command && (command[0] == 0 || (command[0] == '-' && command[1] == 0))) {
538 wfree(command);
539 command = NULL;
541 clickedIcon->command = command;
542 clickedIcon->editing = 0;
543 } else {
544 clickedIcon->editing = 0;
545 if (command)
546 wfree(command);
547 WMFreeArray(selectedIcons);
548 return;
552 WMAddToArray(selectedIcons, clickedIcon);
555 WM_ITERATE_ARRAY(selectedIcons, aicon, it) {
556 if (aicon->icon->selected)
557 wIconSelect(aicon->icon);
559 if (aicon && aicon->attracted && aicon->command) {
560 aicon->attracted = 0;
561 if (aicon->icon->shadowed) {
562 aicon->icon->shadowed = 0;
565 * Update icon pixmap, RImage doesn't change,
566 * so call wIconUpdate is not needed
568 update_icon_pixmap(aicon->icon);
570 /* Paint it */
571 wAppIconPaint(aicon);
574 save_appicon(aicon, True);
576 WMFreeArray(selectedIcons);
579 static void toggleAutoAttractCallback(WMenu *menu, WMenuEntry *entry)
581 WDock *dock = (WDock *) entry->clientdata;
582 WScreen *scr = dock->screen_ptr;
584 assert(entry->clientdata != NULL);
586 dock->attract_icons = !dock->attract_icons;
588 entry->flags.indicator_on = dock->attract_icons;
590 wMenuPaint(menu);
592 if (dock->attract_icons) {
593 if (dock->type == WM_DRAWER) {
594 /* The newly auto-attracting dock is a drawer: disable any clip and
595 * previously attracting drawer */
597 if (!wPreferences.flags.noclip) {
598 int i;
599 for (i = 0; i < w_global.workspace.count; i++)
600 w_global.workspace.array[i]->clip->attract_icons = False;
601 /* dock menu will be updated later, when opened */
604 if (scr->attracting_drawer != NULL)
605 scr->attracting_drawer->attract_icons = False;
606 scr->attracting_drawer = dock;
607 } else {
608 /* The newly auto-attracting dock is a clip: disable
609 * previously attracting drawer, if applicable */
610 if (scr->attracting_drawer != NULL) {
611 scr->attracting_drawer->attract_icons = False;
612 /* again, its menu will be updated, later. */
613 scr->attracting_drawer = NULL;
619 static void selectCallback(WMenu *menu, WMenuEntry *entry)
621 WAppIcon *icon = (WAppIcon *) entry->clientdata;
623 assert(icon != NULL);
625 wIconSelect(icon->icon);
627 wMenuPaint(menu);
630 static void colectIconsCallback(WMenu *menu, WMenuEntry *entry)
632 WAppIcon *clickedIcon = (WAppIcon *) entry->clientdata;
633 WDock *clip; /* clip... is a WM_CLIP or a WM_DRAWER */
634 WAppIcon *aicon;
635 int x, y, x_pos, y_pos;
636 Bool update_icon = False;
638 assert(entry->clientdata != NULL);
639 clip = clickedIcon->dock;
641 aicon = w_global.app_icon_list;
643 while (aicon) {
644 if (!aicon->docked && wDockFindFreeSlot(clip, &x, &y)) {
645 x_pos = clip->x_pos + x * ICON_SIZE;
646 y_pos = clip->y_pos + y * ICON_SIZE;
647 if (aicon->x_pos != x_pos || aicon->y_pos != y_pos)
648 move_window(aicon->icon->core->window, aicon->x_pos, aicon->y_pos, x_pos, y_pos);
650 aicon->attracted = 1;
651 if (!aicon->icon->shadowed) {
652 aicon->icon->shadowed = 1;
653 update_icon = True;
655 wDockAttachIcon(clip, aicon, x, y, update_icon);
656 if (clip->collapsed || !clip->mapped)
657 XUnmapWindow(dpy, aicon->icon->core->window);
659 aicon = aicon->next;
663 static void selectIconsCallback(WMenu *menu, WMenuEntry *entry)
665 WAppIcon *clickedIcon = (WAppIcon *) entry->clientdata;
666 WDock *dock;
667 WMArray *selectedIcons;
668 WMArrayIterator iter;
669 WAppIcon *btn;
670 int i;
672 assert(clickedIcon != NULL);
673 dock = clickedIcon->dock;
675 selectedIcons = getSelected(dock);
677 if (!WMGetArrayItemCount(selectedIcons)) {
678 for (i = 1; i < dock->max_icons; i++) {
679 btn = dock->icon_array[i];
680 if (btn && !btn->icon->selected)
681 wIconSelect(btn->icon);
683 } else {
684 WM_ITERATE_ARRAY(selectedIcons, btn, iter) {
685 wIconSelect(btn->icon);
688 WMFreeArray(selectedIcons);
690 wMenuPaint(menu);
693 static void toggleCollapsedCallback(WMenu *menu, WMenuEntry *entry)
695 assert(entry->clientdata != NULL);
697 toggleCollapsed(entry->clientdata);
699 entry->flags.indicator_on = ((WDock *) entry->clientdata)->collapsed;
701 wMenuPaint(menu);
704 static void toggleAutoCollapseCallback(WMenu *menu, WMenuEntry *entry)
706 WDock *dock;
707 assert(entry->clientdata != NULL);
709 dock = (WDock *) entry->clientdata;
711 dock->auto_collapse = !dock->auto_collapse;
713 entry->flags.indicator_on = ((WDock *) entry->clientdata)->auto_collapse;
715 wMenuPaint(menu);
718 static void toggleAutoRaiseLower(WDock *dock)
720 WDrawerChain *dc;
722 dock->auto_raise_lower = !dock->auto_raise_lower;
723 if (dock->type == WM_DOCK)
725 for (dc = dock->screen_ptr->drawers; dc != NULL; dc = dc->next) {
726 toggleAutoRaiseLower(dc->adrawer);
731 static void toggleAutoRaiseLowerCallback(WMenu *menu, WMenuEntry *entry)
733 WDock *dock;
734 assert(entry->clientdata != NULL);
736 dock = (WDock *) entry->clientdata;
738 toggleAutoRaiseLower(dock);
740 entry->flags.indicator_on = ((WDock *) entry->clientdata)->auto_raise_lower;
742 wMenuPaint(menu);
745 static void launchCallback(WMenu *menu, WMenuEntry *entry)
747 WAppIcon *btn = (WAppIcon *) entry->clientdata;
749 launchDockedApplication(btn, False);
752 static void settingsCallback(WMenu *menu, WMenuEntry *entry)
754 WAppIcon *btn = (WAppIcon *) entry->clientdata;
756 if (btn->editing)
757 return;
758 ShowDockAppSettingsPanel(btn);
761 static void hideCallback(WMenu *menu, WMenuEntry *entry)
763 WApplication *wapp;
764 WAppIcon *btn = (WAppIcon *) entry->clientdata;
766 wapp = wApplicationOf(btn->icon->owner->main_window);
768 if (wapp->flags.hidden) {
769 wWorkspaceChange(btn->icon->core->screen_ptr, wapp->last_workspace);
770 wUnhideApplication(wapp, False, False);
771 } else {
772 wHideApplication(wapp);
776 static void unhideHereCallback(WMenu *menu, WMenuEntry *entry)
778 WApplication *wapp;
779 WAppIcon *btn = (WAppIcon *) entry->clientdata;
781 wapp = wApplicationOf(btn->icon->owner->main_window);
783 wUnhideApplication(wapp, False, True);
786 /* Name is only used when type == WM_DRAWER and when restoring a specific
787 * drawer, with a specific name. When creating a drawer, leave name to NULL
788 * and mainIconCreate will find the first unused unique name */
789 static WAppIcon *mainIconCreate(WScreen *scr, int type, const char *name)
791 WAppIcon *btn;
792 int x_pos;
794 switch(type) {
795 case WM_CLIP:
796 if (w_global.clip.icon)
797 return w_global.clip.icon;
799 btn = wAppIconCreateForDock(scr, NULL, "Logo", "WMClip", TILE_CLIP);
800 btn->icon->core->descriptor.handle_expose = clipIconExpose;
801 x_pos = 0;
802 break;
803 case WM_DOCK:
804 default: /* to avoid a warning about btn and x_pos, basically */
805 btn = wAppIconCreateForDock(scr, NULL, "Logo", "WMDock", TILE_NORMAL);
806 if (wPreferences.flags.clip_merged_in_dock)
807 btn->icon->core->descriptor.handle_expose = clipIconExpose;
808 x_pos = scr->scr_width - ICON_SIZE - DOCK_EXTRA_SPACE;
809 break;
810 case WM_DRAWER:
811 if (name == NULL)
812 name = findUniqueName(scr, "Drawer");
813 btn = wAppIconCreateForDock(scr, NULL, name, "WMDrawer", TILE_DRAWER);
814 btn->icon->core->descriptor.handle_expose = drawerIconExpose;
815 x_pos = 0;
818 btn->xindex = 0;
819 btn->yindex = 0;
821 btn->icon->core->descriptor.handle_mousedown = iconMouseDown;
822 btn->icon->core->descriptor.handle_enternotify = clipEnterNotify;
823 btn->icon->core->descriptor.handle_leavenotify = clipLeaveNotify;
824 btn->icon->core->descriptor.parent_type = WCLASS_DOCK_ICON;
825 btn->icon->core->descriptor.parent = btn;
826 XMapWindow(dpy, btn->icon->core->window);
827 btn->x_pos = x_pos;
828 btn->y_pos = 0;
829 btn->docked = 1;
830 if (type == WM_CLIP ||
831 (type == WM_DOCK && wPreferences.flags.clip_merged_in_dock))
832 w_global.clip.icon = btn;
834 return btn;
837 static void switchWSCommand(WMenu *menu, WMenuEntry *entry)
839 WAppIcon *btn, *icon = (WAppIcon *) entry->clientdata;
840 WDock *src, *dest;
841 WMArray *selectedIcons;
842 int x, y;
844 if (entry->order == w_global.workspace.current)
845 return;
846 src = icon->dock;
847 dest = w_global.workspace.array[entry->order]->clip;
849 selectedIcons = getSelected(src);
851 if (WMGetArrayItemCount(selectedIcons)) {
852 WMArrayIterator iter;
854 WM_ITERATE_ARRAY(selectedIcons, btn, iter) {
855 if (wDockFindFreeSlot(dest, &x, &y)) {
856 wDockMoveIconBetweenDocks(src, dest, btn, x, y);
857 XUnmapWindow(dpy, btn->icon->core->window);
860 } else if (icon != w_global.clip.icon) {
861 if (wDockFindFreeSlot(dest, &x, &y)) {
862 wDockMoveIconBetweenDocks(src, dest, icon, x, y);
863 XUnmapWindow(dpy, icon->icon->core->window);
866 WMFreeArray(selectedIcons);
869 static void launchDockedApplication(WAppIcon *btn, Bool withSelection)
871 WScreen *scr = btn->icon->core->screen_ptr;
873 if (!btn->launching &&
874 ((!withSelection && btn->command != NULL) || (withSelection && btn->paste_command != NULL))) {
875 if (!btn->forced_dock) {
876 btn->relaunching = btn->running;
877 btn->running = 1;
879 if (btn->wm_instance || btn->wm_class) {
880 WWindowAttributes attr;
881 memset(&attr, 0, sizeof(WWindowAttributes));
882 wDefaultFillAttributes(btn->wm_instance, btn->wm_class, &attr, NULL, True);
884 if (!attr.no_appicon && !btn->buggy_app)
885 btn->launching = 1;
886 else
887 btn->running = 0;
889 btn->drop_launch = 0;
890 btn->paste_launch = withSelection;
891 scr->last_dock = btn->dock;
892 btn->pid = execCommand(btn, (withSelection ? btn->paste_command : btn->command), NULL);
893 if (btn->pid > 0) {
894 if (btn->buggy_app) {
895 /* give feedback that the app was launched */
896 btn->launching = 1;
897 dockIconPaint(btn);
898 btn->launching = 0;
899 WMAddTimerHandler(200, (WMCallback *) dockIconPaint, btn);
900 } else {
901 dockIconPaint(btn);
903 } else {
904 wwarning(_("could not launch application %s"), btn->command);
905 btn->launching = 0;
906 if (!btn->relaunching)
907 btn->running = 0;
912 static void updateWorkspaceMenu(WMenu *menu, WAppIcon *icon)
914 char title[MAX_WORKSPACENAME_WIDTH + 1];
915 int i;
917 if (!menu || !icon)
918 return;
920 for (i = 0; i < w_global.workspace.count; i++) {
921 if (i < menu->entry_no) {
922 if (strcmp(menu->entries[i]->text, w_global.workspace.array[i]->name) != 0) {
923 wfree(menu->entries[i]->text);
924 strcpy(title, w_global.workspace.array[i]->name);
925 menu->entries[i]->text = wstrdup(title);
926 menu->flags.realized = 0;
928 menu->entries[i]->clientdata = (void *)icon;
929 } else {
930 strcpy(title, w_global.workspace.array[i]->name);
932 wMenuAddCallback(menu, title, switchWSCommand, (void *)icon);
934 menu->flags.realized = 0;
937 if (i == w_global.workspace.current)
938 wMenuSetEnabled(menu, i, False);
939 else
940 wMenuSetEnabled(menu, i, True);
943 if (!menu->flags.realized)
944 wMenuRealize(menu);
947 static WMenu *makeWorkspaceMenu(WScreen *scr)
949 WMenu *menu;
951 menu = wMenuCreate(scr, NULL, False);
952 if (!menu)
953 wwarning(_("could not create workspace submenu for Clip menu"));
955 wMenuAddCallback(menu, "", switchWSCommand, (void *)w_global.clip.icon);
957 menu->flags.realized = 0;
958 wMenuRealize(menu);
960 return menu;
963 static void updateClipOptionsMenu(WMenu *menu, WDock *dock)
965 WMenuEntry *entry;
966 int index = 0;
968 if (!menu || !dock)
969 return;
971 /* keep on top */
972 entry = menu->entries[index];
973 entry->flags.indicator_on = !dock->lowered;
974 entry->clientdata = dock;
975 wMenuSetEnabled(menu, index, dock->type == WM_CLIP);
977 /* collapsed */
978 entry = menu->entries[++index];
979 entry->flags.indicator_on = dock->collapsed;
980 entry->clientdata = dock;
982 /* auto-collapse */
983 entry = menu->entries[++index];
984 entry->flags.indicator_on = dock->auto_collapse;
985 entry->clientdata = dock;
987 /* auto-raise/lower */
988 entry = menu->entries[++index];
989 entry->flags.indicator_on = dock->auto_raise_lower;
990 entry->clientdata = dock;
991 wMenuSetEnabled(menu, index, dock->lowered && (dock->type == WM_CLIP));
993 /* attract icons */
994 entry = menu->entries[++index];
995 entry->flags.indicator_on = dock->attract_icons;
996 entry->clientdata = dock;
998 menu->flags.realized = 0;
999 wMenuRealize(menu);
1003 static WMenu *makeClipOptionsMenu(WScreen *scr)
1005 WMenu *menu;
1006 WMenuEntry *entry;
1008 menu = wMenuCreate(scr, NULL, False);
1009 if (!menu) {
1010 wwarning(_("could not create options submenu for Clip menu"));
1011 return NULL;
1014 entry = wMenuAddCallback(menu, _("Keep on Top"), toggleLoweredCallback, NULL);
1015 entry->flags.indicator = 1;
1016 entry->flags.indicator_on = 1;
1017 entry->flags.indicator_type = MI_CHECK;
1019 entry = wMenuAddCallback(menu, _("Collapsed"), toggleCollapsedCallback, NULL);
1020 entry->flags.indicator = 1;
1021 entry->flags.indicator_on = 1;
1022 entry->flags.indicator_type = MI_CHECK;
1024 entry = wMenuAddCallback(menu, _("Autocollapse"), toggleAutoCollapseCallback, NULL);
1025 entry->flags.indicator = 1;
1026 entry->flags.indicator_on = 1;
1027 entry->flags.indicator_type = MI_CHECK;
1029 entry = wMenuAddCallback(menu, _("Autoraise"), toggleAutoRaiseLowerCallback, NULL);
1030 entry->flags.indicator = 1;
1031 entry->flags.indicator_on = 1;
1032 entry->flags.indicator_type = MI_CHECK;
1034 entry = wMenuAddCallback(menu, _("Autoattract Icons"), toggleAutoAttractCallback, NULL);
1035 entry->flags.indicator = 1;
1036 entry->flags.indicator_on = 1;
1037 entry->flags.indicator_type = MI_CHECK;
1039 menu->flags.realized = 0;
1040 wMenuRealize(menu);
1042 return menu;
1046 static void setDockPositionNormalCallback(WMenu *menu, WMenuEntry *entry)
1048 WDock *dock = (WDock *) entry->clientdata;
1049 WDrawerChain *dc;
1050 if (entry->flags.indicator_on) // already set, nothing to do
1051 return;
1052 // Do we come from auto raise lower or keep on top?
1053 if (dock->auto_raise_lower)
1055 dock->auto_raise_lower = 0;
1056 // Only for aesthetic purposes, can be removed when Autoraise status is no longer exposed in drawer option menu
1057 for (dc = dock->screen_ptr->drawers; dc != NULL; dc = dc->next) {
1058 dc->adrawer->auto_raise_lower = 0;
1061 else
1063 // Will take care of setting lowered = 0 in drawers
1064 toggleLowered(dock);
1066 entry->flags.indicator_on = 1;
1069 static void setDockPositionAutoRaiseLowerCallback(WMenu *menu, WMenuEntry *entry)
1071 WDock *dock = (WDock *) entry->clientdata;
1072 WDrawerChain *dc;
1073 if (entry->flags.indicator_on) // already set, nothing to do
1074 return;
1075 // Do we come from normal or keep on top?
1076 if (!dock->lowered)
1078 toggleLowered(dock);
1080 dock->auto_raise_lower = 1;
1081 // Only for aesthetic purposes, can be removed when Autoraise status is no longer exposed in drawer option menu
1082 for (dc = dock->screen_ptr->drawers; dc != NULL; dc = dc->next) {
1083 dc->adrawer->auto_raise_lower = 1;
1085 entry->flags.indicator_on = 1;
1088 static void setDockPositionKeepOnTopCallback(WMenu *menu, WMenuEntry *entry)
1090 WDock *dock = (WDock *) entry->clientdata;
1091 WDrawerChain *dc;
1092 if (entry->flags.indicator_on) // already set, nothing to do
1093 return;
1094 dock->auto_raise_lower = 0;
1095 // Only for aesthetic purposes, can be removed when Autoraise status is no longer exposed in drawer option menu
1096 for (dc = dock->screen_ptr->drawers; dc != NULL; dc = dc->next) {
1097 dc->adrawer->auto_raise_lower = 0;
1099 toggleLowered(dock);
1100 entry->flags.indicator_on = 1;
1103 static void updateDockPositionMenu(WMenu *menu, WDock *dock)
1105 WMenuEntry *entry;
1106 int index = 0;
1108 assert(menu);
1109 assert(dock);
1111 /* Normal level */
1112 entry = menu->entries[index++];
1113 entry->flags.indicator_on = (dock->lowered && !dock->auto_raise_lower);
1114 entry->clientdata = dock;
1116 /* Auto-raise/lower */
1117 entry = menu->entries[index++];
1118 entry->flags.indicator_on = dock->auto_raise_lower;
1119 entry->clientdata = dock;
1121 /* Keep on top */
1122 entry = menu->entries[index++];
1123 entry->flags.indicator_on = !dock->lowered;
1124 entry->clientdata = dock;
1127 static WMenu *makeDockPositionMenu(WScreen *scr)
1129 /* When calling this, the dock is being created, so scr->dock is still not set
1130 * Therefore the callbacks' clientdata and the indicators can't be set,
1131 * they will be updated when the dock menu is opened. */
1132 WMenu *menu;
1133 WMenuEntry *entry;
1135 menu = wMenuCreate(scr, NULL, False);
1136 if (!menu) {
1137 wwarning(_("could not create options submenu for dock position menu"));
1138 return NULL;
1141 entry = wMenuAddCallback(menu, _("Normal"), setDockPositionNormalCallback, NULL);
1142 entry->flags.indicator = 1;
1143 entry->flags.indicator_type = MI_DIAMOND;
1145 entry = wMenuAddCallback(menu, _("Auto raise & lower"), setDockPositionAutoRaiseLowerCallback, NULL);
1146 entry->flags.indicator = 1;
1147 entry->flags.indicator_type = MI_DIAMOND;
1149 entry = wMenuAddCallback(menu, _("Keep on Top"), setDockPositionKeepOnTopCallback, NULL);
1150 entry->flags.indicator = 1;
1151 entry->flags.indicator_type = MI_DIAMOND;
1153 menu->flags.realized = 0;
1154 wMenuRealize(menu);
1156 return menu;
1160 static WMenu *dockMenuCreate(WScreen *scr, int type)
1162 WMenu *menu;
1163 WMenuEntry *entry;
1165 if (type == WM_CLIP && w_global.clip.menu)
1166 return w_global.clip.menu;
1168 if (type == WM_DRAWER && w_global.dock.drawer_menu)
1169 return w_global.dock.drawer_menu;
1171 menu = wMenuCreate(scr, NULL, False);
1172 if (type == WM_DOCK) {
1173 entry = wMenuAddCallback(menu, _("Dock position"), NULL, NULL);
1174 if (w_global.dock.pos_menu == NULL)
1175 w_global.dock.pos_menu = makeDockPositionMenu(scr);
1176 wMenuEntrySetCascade(menu, entry, w_global.dock.pos_menu);
1178 if (!wPreferences.flags.nodrawer) {
1179 entry = wMenuAddCallback(menu, _("Add a drawer"), addADrawerCallback, NULL);
1181 } else {
1182 if (type == WM_CLIP)
1183 entry = wMenuAddCallback(menu, _("Clip Options"), NULL, NULL);
1184 else /* if (type == WM_DRAWER) */
1185 entry = wMenuAddCallback(menu, _("Drawer options"), NULL, NULL);
1187 if (w_global.clip.opt_menu == NULL)
1188 w_global.clip.opt_menu = makeClipOptionsMenu(scr);
1190 wMenuEntrySetCascade(menu, entry, w_global.clip.opt_menu);
1192 /* The same menu is used for the dock and its appicons. If the menu
1193 * entry text is different between the two contexts, or if it can
1194 * change depending on some state, free the duplicated string (from
1195 * wMenuInsertCallback) and use gettext's string */
1196 if (type == WM_CLIP) {
1197 entry = wMenuAddCallback(menu, _("Rename Workspace"), renameCallback, NULL);
1198 wfree(entry->text);
1199 entry->text = _("Rename Workspace"); /* can be: (Toggle) Omnipresent */
1202 entry = wMenuAddCallback(menu, _("Selected"), selectCallback, NULL);
1203 entry->flags.indicator = 1;
1204 entry->flags.indicator_on = 1;
1205 entry->flags.indicator_type = MI_CHECK;
1207 entry = wMenuAddCallback(menu, _("Select All Icons"), selectIconsCallback, NULL);
1208 wfree(entry->text);
1209 entry->text = _("Select All Icons"); /* can be: Unselect all icons */
1211 entry = wMenuAddCallback(menu, _("Keep Icon"), keepIconsCallback, NULL);
1212 wfree(entry->text);
1213 entry->text = _("Keep Icon"); /* can be: Keep Icons */
1215 if (type == WM_CLIP) {
1216 entry = wMenuAddCallback(menu, _("Move Icon To"), NULL, NULL);
1217 wfree(entry->text);
1218 entry->text = _("Move Icon To"); /* can be: Move Icons to */
1219 w_global.clip.submenu = makeWorkspaceMenu(scr);
1220 if (w_global.clip.submenu)
1221 wMenuEntrySetCascade(menu, entry, w_global.clip.submenu);
1224 entry = wMenuAddCallback(menu, _("Remove Icon"), removeIconsCallback, NULL);
1225 wfree(entry->text);
1226 entry->text = _("Remove Icon"); /* can be: Remove Icons */
1228 wMenuAddCallback(menu, _("Attract Icons"), colectIconsCallback, NULL);
1231 wMenuAddCallback(menu, _("Launch"), launchCallback, NULL);
1233 wMenuAddCallback(menu, _("Unhide Here"), unhideHereCallback, NULL);
1235 entry = wMenuAddCallback(menu, _("Hide"), hideCallback, NULL);
1236 wfree(entry->text);
1237 entry->text = _("Hide"); /* can be: Unhide */
1239 wMenuAddCallback(menu, _("Settings..."), settingsCallback, NULL);
1241 entry = wMenuAddCallback(menu, _("Kill"), killCallback, NULL);
1242 wfree(entry->text);
1243 entry->text = _("Kill"); /* can be: Remove drawer */
1245 if (type == WM_CLIP)
1246 w_global.clip.menu = menu;
1248 if (type == WM_DRAWER)
1249 w_global.dock.drawer_menu = menu;
1251 return menu;
1254 WDock *wDockCreate(WScreen *scr, int type, const char *name)
1256 WDock *dock;
1257 WAppIcon *btn;
1259 make_keys();
1261 dock = wmalloc(sizeof(WDock));
1263 switch (type) {
1264 case WM_CLIP:
1265 dock->max_icons = DOCK_MAX_ICONS;
1266 break;
1267 case WM_DRAWER:
1268 dock->max_icons = scr->scr_width / wPreferences.icon_size;
1269 break;
1270 case WM_DOCK:
1271 default:
1272 dock->max_icons = scr->scr_height / wPreferences.icon_size;
1275 dock->icon_array = wmalloc(sizeof(WAppIcon *) * dock->max_icons);
1277 btn = mainIconCreate(scr, type, name);
1279 btn->dock = dock;
1281 dock->x_pos = btn->x_pos;
1282 dock->y_pos = btn->y_pos;
1283 dock->screen_ptr = scr;
1284 dock->type = type;
1285 dock->icon_count = 1;
1286 if (type == WM_DRAWER)
1287 dock->on_right_side = scr->dock->on_right_side;
1288 else
1289 dock->on_right_side = 1;
1290 dock->collapsed = 0;
1291 dock->auto_collapse = 0;
1292 dock->auto_collapse_magic = NULL;
1293 dock->auto_raise_lower = 0;
1294 dock->auto_lower_magic = NULL;
1295 dock->auto_raise_magic = NULL;
1296 dock->attract_icons = 0;
1297 dock->lowered = 1;
1298 dock->icon_array[0] = btn;
1299 wRaiseFrame(btn->icon->core);
1300 XMoveWindow(dpy, btn->icon->core->window, btn->x_pos, btn->y_pos);
1302 /* create dock menu */
1303 dock->menu = dockMenuCreate(scr, type);
1305 if (type == WM_DRAWER) {
1306 drawerAppendToChain(scr, dock);
1307 dock->auto_collapse = 1;
1310 return dock;
1313 void wDockDestroy(WDock *dock)
1315 int i;
1316 WAppIcon *aicon;
1318 for (i = (dock->type == WM_CLIP) ? 1 : 0; i < dock->max_icons; i++) {
1319 aicon = dock->icon_array[i];
1320 if (aicon) {
1321 int keepit = aicon->running && wApplicationOf(aicon->main_window);
1322 wDockDetach(dock, aicon);
1323 if (keepit) {
1324 /* XXX: can: aicon->icon == NULL ? */
1325 PlaceIcon(dock->screen_ptr, &aicon->x_pos, &aicon->y_pos,
1326 wGetHeadForWindow(aicon->icon->owner));
1327 XMoveWindow(dpy, aicon->icon->core->window, aicon->x_pos, aicon->y_pos);
1328 if (!dock->mapped || dock->collapsed)
1329 XMapWindow(dpy, aicon->icon->core->window);
1333 if (wPreferences.auto_arrange_icons)
1334 wArrangeIcons(dock->screen_ptr, True);
1335 wfree(dock->icon_array);
1336 if (dock->menu && dock->type != WM_CLIP)
1337 wMenuDestroy(dock->menu, True);
1338 if (dock->screen_ptr->last_dock == dock)
1339 dock->screen_ptr->last_dock = NULL;
1340 wfree(dock);
1343 void wClipIconPaint(void)
1345 WAppIcon *aicon = w_global.clip.icon;
1346 WScreen *scr = aicon->icon->core->screen_ptr;
1347 WWorkspace *workspace = w_global.workspace.array[w_global.workspace.current];
1348 WMColor *color;
1349 Window win = aicon->icon->core->window;
1350 int length, nlength;
1351 char *ws_name, ws_number[10];
1352 int ty, tx;
1354 wIconPaint(aicon->icon);
1356 length = strlen(workspace->name);
1357 ws_name = wmalloc(length + 1);
1358 snprintf(ws_name, length + 1, "%s", workspace->name);
1359 snprintf(ws_number, sizeof(ws_number), "%i", w_global.workspace.current + 1);
1360 nlength = strlen(ws_number);
1362 if (wPreferences.flags.noclip || !workspace->clip->collapsed)
1363 color = scr->clip_title_color[CLIP_NORMAL];
1364 else
1365 color = scr->clip_title_color[CLIP_COLLAPSED];
1367 ty = ICON_SIZE - WMFontHeight(scr->clip_title_font) - 3;
1369 tx = CLIP_BUTTON_SIZE * ICON_SIZE / 64;
1371 if(wPreferences.show_clip_title)
1372 WMDrawString(scr->wmscreen, win, color, scr->clip_title_font, tx, ty, ws_name, length);
1374 tx = (ICON_SIZE / 2 - WMWidthOfString(scr->clip_title_font, ws_number, nlength)) / 2;
1376 WMDrawString(scr->wmscreen, win, color, scr->clip_title_font, tx, 2, ws_number, nlength);
1378 wfree(ws_name);
1380 if (aicon->launching)
1381 XFillRectangle(dpy, aicon->icon->core->window, scr->stipple_gc,
1382 0, 0, wPreferences.icon_size, wPreferences.icon_size);
1384 paintClipButtons(aicon, aicon->dock->lclip_button_pushed, aicon->dock->rclip_button_pushed);
1387 static void clipIconExpose(WObjDescriptor *desc, XEvent *event)
1389 wClipIconPaint();
1392 static void dockIconPaint(WAppIcon *btn)
1394 if (btn == w_global.clip.icon) {
1395 wClipIconPaint();
1396 } else if (wIsADrawer(btn)) {
1397 wDrawerIconPaint(btn);
1398 } else {
1399 wAppIconPaint(btn);
1400 save_appicon(btn, True);
1404 static WMPropList *make_icon_state(WAppIcon *btn)
1406 WMPropList *node = NULL;
1407 WMPropList *command, *autolaunch, *lock, *name, *forced;
1408 WMPropList *position, *buggy, *omnipresent;
1409 char *tmp;
1410 char buffer[64];
1412 if (btn) {
1413 if (!btn->command)
1414 command = WMCreatePLString("-");
1415 else
1416 command = WMCreatePLString(btn->command);
1418 autolaunch = btn->auto_launch ? dYes : dNo;
1420 lock = btn->lock ? dYes : dNo;
1422 tmp = EscapeWM_CLASS(btn->wm_instance, btn->wm_class);
1424 name = WMCreatePLString(tmp);
1426 wfree(tmp);
1428 forced = btn->forced_dock ? dYes : dNo;
1430 buggy = btn->buggy_app ? dYes : dNo;
1432 if (!wPreferences.flags.clip_merged_in_dock && btn == w_global.clip.icon)
1433 snprintf(buffer, sizeof(buffer), "%i,%i", btn->x_pos, btn->y_pos);
1434 else
1435 snprintf(buffer, sizeof(buffer), "%hi,%hi", btn->xindex, btn->yindex);
1436 position = WMCreatePLString(buffer);
1438 node = WMCreatePLDictionary(dCommand, command,
1439 dName, name,
1440 dAutoLaunch, autolaunch,
1441 dLock, lock,
1442 dForced, forced, dBuggyApplication, buggy, dPosition, position, NULL);
1443 WMReleasePropList(command);
1444 WMReleasePropList(name);
1445 WMReleasePropList(position);
1447 omnipresent = btn->omnipresent ? dYes : dNo;
1448 if (btn->dock != btn->icon->core->screen_ptr->dock && (btn->xindex != 0 || btn->yindex != 0))
1449 WMPutInPLDictionary(node, dOmnipresent, omnipresent);
1451 #ifdef XDND /* was OFFIX */
1452 if (btn->dnd_command) {
1453 command = WMCreatePLString(btn->dnd_command);
1454 WMPutInPLDictionary(node, dDropCommand, command);
1455 WMReleasePropList(command);
1457 #endif /* XDND */
1459 if (btn->paste_command) {
1460 command = WMCreatePLString(btn->paste_command);
1461 WMPutInPLDictionary(node, dPasteCommand, command);
1462 WMReleasePropList(command);
1466 return node;
1469 static WMPropList *dockSaveState(WDock *dock)
1471 int i;
1472 WMPropList *icon_info;
1473 WMPropList *list = NULL, *dock_state = NULL;
1474 WMPropList *value, *key;
1475 char buffer[256];
1477 list = WMCreatePLArray(NULL);
1479 for (i = (dock->type == WM_DOCK ? 0 : 1); i < dock->max_icons; i++) {
1480 WAppIcon *btn = dock->icon_array[i];
1482 if (!btn || btn->attracted)
1483 continue;
1485 if ((icon_info = make_icon_state(dock->icon_array[i]))) {
1486 WMAddToPLArray(list, icon_info);
1487 WMReleasePropList(icon_info);
1491 dock_state = WMCreatePLDictionary(dApplications, list, NULL);
1493 if (dock->type == WM_DOCK) {
1494 snprintf(buffer, sizeof(buffer), "Applications%i", dock->screen_ptr->scr_height);
1495 key = WMCreatePLString(buffer);
1496 WMPutInPLDictionary(dock_state, key, list);
1497 WMReleasePropList(key);
1499 snprintf(buffer, sizeof(buffer), "%i,%i", (dock->on_right_side ? -ICON_SIZE : 0), dock->y_pos);
1500 value = WMCreatePLString(buffer);
1501 WMPutInPLDictionary(dock_state, dPosition, value);
1502 WMReleasePropList(value);
1504 WMReleasePropList(list);
1506 if (dock->type == WM_CLIP || dock->type == WM_DRAWER) {
1507 value = (dock->collapsed ? dYes : dNo);
1508 WMPutInPLDictionary(dock_state, dCollapsed, value);
1510 value = (dock->auto_collapse ? dYes : dNo);
1511 WMPutInPLDictionary(dock_state, dAutoCollapse, value);
1513 value = (dock->attract_icons ? dYes : dNo);
1514 WMPutInPLDictionary(dock_state, dAutoAttractIcons, value);
1517 if (dock->type == WM_DOCK || dock->type == WM_CLIP) {
1518 value = (dock->lowered ? dYes : dNo);
1519 WMPutInPLDictionary(dock_state, dLowered, value);
1521 value = (dock->auto_raise_lower ? dYes : dNo);
1522 WMPutInPLDictionary(dock_state, dAutoRaiseLower, value);
1525 return dock_state;
1528 void wDockSaveState(WScreen *scr, WMPropList *old_state)
1530 WMPropList *dock_state;
1531 WMPropList *keys;
1533 dock_state = dockSaveState(scr->dock);
1536 * Copy saved states of docks with different sizes.
1538 if (old_state) {
1539 int i;
1540 WMPropList *tmp;
1542 keys = WMGetPLDictionaryKeys(old_state);
1543 for (i = 0; i < WMGetPropListItemCount(keys); i++) {
1544 tmp = WMGetFromPLArray(keys, i);
1546 if (strncasecmp(WMGetFromPLString(tmp), "applications", 12) == 0
1547 && !WMGetFromPLDictionary(dock_state, tmp)) {
1549 WMPutInPLDictionary(dock_state, tmp, WMGetFromPLDictionary(old_state, tmp));
1552 WMReleasePropList(keys);
1555 WMPutInPLDictionary(w_global.session_state, dDock, dock_state);
1556 WMReleasePropList(dock_state);
1559 void wClipSaveState(WScreen *scr)
1561 WMPropList *clip_state;
1563 clip_state = make_icon_state(w_global.clip.icon);
1565 WMPutInPLDictionary(w_global.session_state, dClip, clip_state);
1566 WMReleasePropList(clip_state);
1569 WMPropList *wClipSaveWorkspaceState(WScreen *scr, int workspace)
1571 return dockSaveState(w_global.workspace.array[workspace]->clip);
1574 static Bool getBooleanDockValue(WMPropList *value, WMPropList *key)
1576 if (value) {
1577 if (WMIsPLString(value)) {
1578 if (strcasecmp(WMGetFromPLString(value), "YES") == 0)
1579 return True;
1580 } else {
1581 wwarning(_("bad value in docked icon state info %s"), WMGetFromPLString(key));
1584 return False;
1587 static WAppIcon *restore_icon_state(WScreen *scr, WMPropList *info, int type, int index)
1589 WAppIcon *aicon;
1590 WMPropList *cmd, *value;
1591 char *wclass, *winstance, *command;
1593 cmd = WMGetFromPLDictionary(info, dCommand);
1594 if (!cmd || !WMIsPLString(cmd))
1595 return NULL;
1597 /* parse window name */
1598 value = WMGetFromPLDictionary(info, dName);
1599 if (!value)
1600 return NULL;
1602 ParseWindowName(value, &winstance, &wclass, "dock");
1604 if (!winstance && !wclass)
1605 return NULL;
1607 /* get commands */
1608 if (cmd)
1609 command = wstrdup(WMGetFromPLString(cmd));
1610 else
1611 command = NULL;
1613 if (!command || strcmp(command, "-") == 0) {
1614 if (command)
1615 wfree(command);
1616 if (wclass)
1617 wfree(wclass);
1618 if (winstance)
1619 wfree(winstance);
1621 return NULL;
1624 aicon = wAppIconCreateForDock(scr, command, winstance, wclass, TILE_NORMAL);
1625 if (wclass)
1626 wfree(wclass);
1627 if (winstance)
1628 wfree(winstance);
1629 if (command)
1630 wfree(command);
1632 aicon->icon->core->descriptor.handle_mousedown = iconMouseDown;
1633 aicon->icon->core->descriptor.handle_enternotify = clipEnterNotify;
1634 aicon->icon->core->descriptor.handle_leavenotify = clipLeaveNotify;
1635 aicon->icon->core->descriptor.parent_type = WCLASS_DOCK_ICON;
1636 aicon->icon->core->descriptor.parent = aicon;
1638 #ifdef XDND /* was OFFIX */
1639 cmd = WMGetFromPLDictionary(info, dDropCommand);
1640 if (cmd)
1641 aicon->dnd_command = wstrdup(WMGetFromPLString(cmd));
1642 #endif
1644 cmd = WMGetFromPLDictionary(info, dPasteCommand);
1645 if (cmd)
1646 aicon->paste_command = wstrdup(WMGetFromPLString(cmd));
1648 /* check auto launch */
1649 value = WMGetFromPLDictionary(info, dAutoLaunch);
1651 aicon->auto_launch = getBooleanDockValue(value, dAutoLaunch);
1653 /* check lock */
1654 value = WMGetFromPLDictionary(info, dLock);
1656 aicon->lock = getBooleanDockValue(value, dLock);
1658 /* check if it wasn't normally docked */
1659 value = WMGetFromPLDictionary(info, dForced);
1661 aicon->forced_dock = getBooleanDockValue(value, dForced);
1663 /* check if we can rely on the stuff in the app */
1664 value = WMGetFromPLDictionary(info, dBuggyApplication);
1666 aicon->buggy_app = getBooleanDockValue(value, dBuggyApplication);
1668 /* get position in the dock */
1669 value = WMGetFromPLDictionary(info, dPosition);
1670 if (value && WMIsPLString(value)) {
1671 if (sscanf(WMGetFromPLString(value), "%hi,%hi", &aicon->xindex, &aicon->yindex) != 2)
1672 wwarning(_("bad value in docked icon state info %s"), WMGetFromPLString(dPosition));
1674 /* check position sanity */
1675 /* *Very* incomplete section! */
1676 if (type == WM_DOCK) {
1677 aicon->xindex = 0;
1679 } else {
1680 aicon->yindex = index;
1681 aicon->xindex = 0;
1684 /* check if icon is omnipresent */
1685 value = WMGetFromPLDictionary(info, dOmnipresent);
1687 aicon->omnipresent = getBooleanDockValue(value, dOmnipresent);
1689 aicon->running = 0;
1690 aicon->docked = 1;
1692 return aicon;
1695 #define COMPLAIN(key) wwarning(_("bad value in dock state info:%s"), key)
1697 WAppIcon *wClipRestoreState(WScreen *scr, WMPropList *clip_state)
1699 WAppIcon *icon;
1700 WMPropList *value;
1702 icon = mainIconCreate(scr, WM_CLIP, NULL);
1704 if (!clip_state)
1705 return icon;
1707 WMRetainPropList(clip_state);
1709 /* restore position */
1711 value = WMGetFromPLDictionary(clip_state, dPosition);
1713 if (value) {
1714 if (!WMIsPLString(value)) {
1715 COMPLAIN("Position");
1716 } else {
1717 if (sscanf(WMGetFromPLString(value), "%i,%i", &icon->x_pos, &icon->y_pos) != 2)
1718 COMPLAIN("Position");
1720 /* check position sanity */
1721 if (!onScreen(scr, icon->x_pos, icon->y_pos))
1722 wScreenKeepInside(scr, &icon->x_pos, &icon->y_pos, ICON_SIZE, ICON_SIZE);
1725 #ifdef XDND /* was OFFIX */
1726 value = WMGetFromPLDictionary(clip_state, dDropCommand);
1727 if (value && WMIsPLString(value))
1728 icon->dnd_command = wstrdup(WMGetFromPLString(value));
1729 #endif
1731 value = WMGetFromPLDictionary(clip_state, dPasteCommand);
1732 if (value && WMIsPLString(value))
1733 icon->paste_command = wstrdup(WMGetFromPLString(value));
1735 WMReleasePropList(clip_state);
1737 return icon;
1740 WDock *wDockRestoreState(WScreen *scr, WMPropList *dock_state, int type)
1742 WDock *dock;
1743 WMPropList *apps;
1744 WMPropList *value;
1745 WAppIcon *aicon, *old_top;
1746 int count, i;
1748 dock = wDockCreate(scr, type, NULL);
1750 if (!dock_state)
1751 return dock;
1753 WMRetainPropList(dock_state);
1755 /* restore position */
1756 value = WMGetFromPLDictionary(dock_state, dPosition);
1757 if (value) {
1758 if (!WMIsPLString(value)) {
1759 COMPLAIN("Position");
1760 } else {
1761 if (sscanf(WMGetFromPLString(value), "%i,%i", &dock->x_pos, &dock->y_pos) != 2)
1762 COMPLAIN("Position");
1764 /* check position sanity */
1765 if (!onScreen(scr, dock->x_pos, dock->y_pos)) {
1766 int x = dock->x_pos;
1767 wScreenKeepInside(scr, &x, &dock->y_pos, ICON_SIZE, ICON_SIZE);
1770 /* Is this needed any more? */
1771 if (type == WM_CLIP) {
1772 if (dock->x_pos < 0) {
1773 dock->x_pos = 0;
1774 } else if (dock->x_pos > scr->scr_width - ICON_SIZE) {
1775 dock->x_pos = scr->scr_width - ICON_SIZE;
1777 } else {
1778 if (dock->x_pos >= 0) {
1779 dock->x_pos = DOCK_EXTRA_SPACE;
1780 dock->on_right_side = 0;
1781 } else {
1782 dock->x_pos = scr->scr_width - DOCK_EXTRA_SPACE - ICON_SIZE;
1783 dock->on_right_side = 1;
1789 /* restore lowered/raised state */
1790 dock->lowered = 0;
1792 value = WMGetFromPLDictionary(dock_state, dLowered);
1793 if (value) {
1794 if (!WMIsPLString(value)) {
1795 COMPLAIN("Lowered");
1796 } else {
1797 if (strcasecmp(WMGetFromPLString(value), "YES") == 0)
1798 dock->lowered = 1;
1802 /* restore collapsed state */
1803 dock->collapsed = 0;
1805 value = WMGetFromPLDictionary(dock_state, dCollapsed);
1806 if (value) {
1807 if (!WMIsPLString(value)) {
1808 COMPLAIN("Collapsed");
1809 } else {
1810 if (strcasecmp(WMGetFromPLString(value), "YES") == 0)
1811 dock->collapsed = 1;
1815 /* restore auto-collapsed state */
1816 value = WMGetFromPLDictionary(dock_state, dAutoCollapse);
1817 if (value) {
1818 if (!WMIsPLString(value)) {
1819 COMPLAIN("AutoCollapse");
1820 } else {
1821 if (strcasecmp(WMGetFromPLString(value), "YES") == 0) {
1822 dock->auto_collapse = 1;
1823 dock->collapsed = 1;
1828 /* restore auto-raise/lower state */
1829 value = WMGetFromPLDictionary(dock_state, dAutoRaiseLower);
1830 if (value) {
1831 if (!WMIsPLString(value)) {
1832 COMPLAIN("AutoRaiseLower");
1833 } else {
1834 if (strcasecmp(WMGetFromPLString(value), "YES") == 0)
1835 dock->auto_raise_lower = 1;
1839 /* restore attract icons state */
1840 dock->attract_icons = 0;
1842 value = WMGetFromPLDictionary(dock_state, dAutoAttractIcons);
1843 if (value) {
1844 if (!WMIsPLString(value)) {
1845 COMPLAIN("AutoAttractIcons");
1846 } else {
1847 if (strcasecmp(WMGetFromPLString(value), "YES") == 0)
1848 dock->attract_icons = 1;
1852 /* application list */
1855 WMPropList *tmp;
1856 char buffer[64];
1859 * When saving, it saves the dock state in
1860 * Applications and Applicationsnnn
1862 * When loading, it will first try Applicationsnnn.
1863 * If it does not exist, use Applications as default.
1866 snprintf(buffer, sizeof(buffer), "Applications%i", scr->scr_height);
1868 tmp = WMCreatePLString(buffer);
1869 apps = WMGetFromPLDictionary(dock_state, tmp);
1870 WMReleasePropList(tmp);
1872 if (!apps)
1873 apps = WMGetFromPLDictionary(dock_state, dApplications);
1876 if (!apps)
1877 goto finish;
1879 count = WMGetPropListItemCount(apps);
1880 if (count == 0)
1881 goto finish;
1883 old_top = dock->icon_array[0];
1885 /* dock->icon_count is set to 1 when dock is created.
1886 * Since Clip is already restored, we want to keep it so for clip,
1887 * but for dock we may change the default top tile, so we set it to 0.
1889 if (type == WM_DOCK)
1890 dock->icon_count = 0;
1892 for (i = 0; i < count; i++) {
1893 if (dock->icon_count >= dock->max_icons) {
1894 wwarning(_("there are too many icons stored in dock. Ignoring what doesn't fit"));
1895 break;
1898 value = WMGetFromPLArray(apps, i);
1899 aicon = restore_icon_state(scr, value, type, dock->icon_count);
1901 dock->icon_array[dock->icon_count] = aicon;
1903 if (aicon) {
1904 aicon->dock = dock;
1905 aicon->x_pos = dock->x_pos + (aicon->xindex * ICON_SIZE);
1906 aicon->y_pos = dock->y_pos + (aicon->yindex * ICON_SIZE);
1908 if (dock->lowered)
1909 ChangeStackingLevel(aicon->icon->core, WMNormalLevel);
1910 else
1911 ChangeStackingLevel(aicon->icon->core, WMDockLevel);
1913 wCoreConfigure(aicon->icon->core, aicon->x_pos, aicon->y_pos, 0, 0);
1914 if (!dock->collapsed)
1915 XMapWindow(dpy, aicon->icon->core->window);
1917 wRaiseFrame(aicon->icon->core);
1919 dock->icon_count++;
1920 } else if (dock->icon_count == 0 && type == WM_DOCK) {
1921 dock->icon_count++;
1925 /* if the first icon is not defined, use the default */
1926 if (dock->icon_array[0] == NULL) {
1927 /* update default icon */
1928 old_top->x_pos = dock->x_pos;
1929 old_top->y_pos = dock->y_pos;
1930 if (dock->lowered)
1931 ChangeStackingLevel(old_top->icon->core, WMNormalLevel);
1932 else
1933 ChangeStackingLevel(old_top->icon->core, WMDockLevel);
1935 dock->icon_array[0] = old_top;
1936 XMoveWindow(dpy, old_top->icon->core->window, dock->x_pos, dock->y_pos);
1937 /* we don't need to increment dock->icon_count here because it was
1938 * incremented in the loop above.
1940 } else if (old_top != dock->icon_array[0]) {
1941 if (old_top == w_global.clip.icon) /* TODO dande: understand the logic */
1942 w_global.clip.icon = dock->icon_array[0];
1944 wAppIconDestroy(old_top);
1947 finish:
1948 WMReleasePropList(dock_state);
1950 return dock;
1953 void wDockLaunchWithState(WDock *dock, WAppIcon *btn, WSavedState *state)
1955 if (btn && btn->command && !btn->running && !btn->launching) {
1956 btn->drop_launch = 0;
1957 btn->paste_launch = 0;
1959 btn->pid = execCommand(btn, btn->command, state);
1961 if (btn->pid > 0) {
1962 if (!btn->forced_dock && !btn->buggy_app) {
1963 btn->launching = 1;
1964 dockIconPaint(btn);
1967 } else {
1968 wfree(state);
1972 void wDockDoAutoLaunch(WDock *dock, int workspace)
1974 WAppIcon *btn;
1975 WSavedState *state;
1976 int i;
1978 for (i = 0; i < dock->max_icons; i++) {
1979 btn = dock->icon_array[i];
1980 if (!btn || !btn->auto_launch)
1981 continue;
1983 state = wmalloc(sizeof(WSavedState));
1984 state->workspace = workspace;
1985 /* TODO: this is klugy and is very difficult to understand
1986 * what's going on. Try to clean up */
1987 wDockLaunchWithState(dock, btn, state);
1991 #ifdef XDND /* was OFFIX */
1992 static WDock *findDock(WScreen *scr, XEvent *event, int *icon_pos)
1994 WDock *dock;
1995 int i;
1997 *icon_pos = -1;
1998 if ((dock = scr->dock) != NULL) {
1999 for (i = 0; i < dock->max_icons; i++) {
2000 if (dock->icon_array[i]
2001 && dock->icon_array[i]->icon->core->window == event->xclient.window) {
2002 *icon_pos = i;
2003 break;
2007 if (*icon_pos < 0 && (dock = w_global.workspace.array[w_global.workspace.current]->clip) != NULL) {
2008 for (i = 0; i < dock->max_icons; i++) {
2009 if (dock->icon_array[i]
2010 && dock->icon_array[i]->icon->core->window == event->xclient.window) {
2011 *icon_pos = i;
2012 break;
2016 if (*icon_pos >= 0)
2017 return dock;
2018 return NULL;
2021 int wDockReceiveDNDDrop(WScreen *scr, XEvent *event)
2023 WDock *dock;
2024 WAppIcon *btn;
2025 int icon_pos;
2027 dock = findDock(scr, event, &icon_pos);
2028 if (!dock)
2029 return False;
2032 * Return True if the drop was on an application icon window.
2033 * In this case, let the ClientMessage handler redirect the
2034 * message to the app.
2036 if (dock->icon_array[icon_pos]->icon->icon_win != None)
2037 return True;
2039 if (dock->icon_array[icon_pos]->dnd_command != NULL) {
2040 scr->flags.dnd_data_convertion_status = 0;
2042 btn = dock->icon_array[icon_pos];
2044 if (!btn->forced_dock) {
2045 btn->relaunching = btn->running;
2046 btn->running = 1;
2048 if (btn->wm_instance || btn->wm_class) {
2049 WWindowAttributes attr;
2050 memset(&attr, 0, sizeof(WWindowAttributes));
2051 wDefaultFillAttributes(btn->wm_instance, btn->wm_class, &attr, NULL, True);
2053 if (!attr.no_appicon)
2054 btn->launching = 1;
2055 else
2056 btn->running = 0;
2059 btn->paste_launch = 0;
2060 btn->drop_launch = 1;
2061 scr->last_dock = dock;
2062 btn->pid = execCommand(btn, btn->dnd_command, NULL);
2063 if (btn->pid > 0) {
2064 dockIconPaint(btn);
2065 } else {
2066 btn->launching = 0;
2067 if (!btn->relaunching)
2068 btn->running = 0;
2071 return False;
2073 #endif /* XDND */
2075 Bool wDockAttachIcon(WDock *dock, WAppIcon *icon, int x, int y, Bool update_icon)
2077 WWindow *wwin;
2078 Bool lupdate_icon = False;
2079 char *command = NULL;
2080 int index;
2082 icon->editing = 0;
2084 if (update_icon)
2085 lupdate_icon = True;
2087 if (icon->command == NULL) {
2088 /* If icon->owner exists, it means the application is running */
2089 if (icon->icon->owner) {
2090 wwin = icon->icon->owner;
2091 command = GetCommandForWindow(wwin->client_win);
2094 if (command) {
2095 icon->command = command;
2096 } else {
2097 /* icon->forced_dock = 1; */
2098 if (dock->type != WM_CLIP || !icon->attracted) {
2099 icon->editing = 1;
2100 if (wInputDialog(dock->screen_ptr, _("Dock Icon"),
2101 _("Type the command used to launch the application"), &command)) {
2102 if (command && (command[0] == 0 || (command[0] == '-' && command[1] == 0))) {
2103 wfree(command);
2104 command = NULL;
2106 icon->command = command;
2107 icon->editing = 0;
2108 } else {
2109 icon->editing = 0;
2110 if (command)
2111 wfree(command);
2112 /* If the target is the dock, reject the icon. If
2113 * the target is the clip, make it an attracted icon
2115 if (dock->type == WM_CLIP) {
2116 icon->attracted = 1;
2117 if (!icon->icon->shadowed) {
2118 icon->icon->shadowed = 1;
2119 lupdate_icon = True;
2121 } else {
2122 return False;
2129 for (index = 1; index < dock->max_icons; index++)
2130 if (dock->icon_array[index] == NULL)
2131 break;
2132 /* if (index == dock->max_icons)
2133 return; */
2135 assert(index < dock->max_icons);
2137 dock->icon_array[index] = icon;
2138 icon->yindex = y;
2139 icon->xindex = x;
2141 icon->omnipresent = 0;
2143 icon->x_pos = dock->x_pos + x * ICON_SIZE;
2144 icon->y_pos = dock->y_pos + y * ICON_SIZE;
2146 dock->icon_count++;
2148 icon->running = 1;
2149 icon->launching = 0;
2150 icon->docked = 1;
2151 icon->dock = dock;
2152 icon->icon->core->descriptor.handle_mousedown = iconMouseDown;
2153 icon->icon->core->descriptor.handle_enternotify = clipEnterNotify;
2154 icon->icon->core->descriptor.handle_leavenotify = clipLeaveNotify;
2155 icon->icon->core->descriptor.parent_type = WCLASS_DOCK_ICON;
2156 icon->icon->core->descriptor.parent = icon;
2158 MoveInStackListUnder(dock->icon_array[index - 1]->icon->core, icon->icon->core);
2159 wAppIconMove(icon, icon->x_pos, icon->y_pos);
2162 * Update icon pixmap, RImage doesn't change,
2163 * so call wIconUpdate is not needed
2165 if (lupdate_icon)
2166 update_icon_pixmap(icon->icon);
2168 /* Paint it */
2169 wAppIconPaint(icon);
2171 /* Save it */
2172 save_appicon(icon, True);
2174 if (wPreferences.auto_arrange_icons)
2175 wArrangeIcons(dock->screen_ptr, True);
2177 #ifdef XDND /* was OFFIX */
2178 if (icon->command && !icon->dnd_command) {
2179 int len = strlen(icon->command) + 8;
2180 icon->dnd_command = wmalloc(len);
2181 snprintf(icon->dnd_command, len, "%s %%d", icon->command);
2183 #endif
2185 if (icon->command && !icon->paste_command) {
2186 int len = strlen(icon->command) + 8;
2187 icon->paste_command = wmalloc(len);
2188 snprintf(icon->paste_command, len, "%s %%s", icon->command);
2191 return True;
2194 void wDockReattachIcon(WDock *dock, WAppIcon *icon, int x, int y)
2196 int index;
2198 for (index = 1; index < dock->max_icons; index++) {
2199 if (dock->icon_array[index] == icon)
2200 break;
2202 assert(index < dock->max_icons);
2204 icon->yindex = y;
2205 icon->xindex = x;
2207 icon->x_pos = dock->x_pos + x * ICON_SIZE;
2208 icon->y_pos = dock->y_pos + y * ICON_SIZE;
2211 Bool wDockMoveIconBetweenDocks(WDock *src, WDock *dest, WAppIcon *icon, int x, int y)
2213 WWindow *wwin;
2214 char *command = NULL;
2215 int index;
2216 Bool update_icon = False;
2218 if (src == dest)
2219 return True; /* No move needed, we're already there */
2221 if (dest == NULL)
2222 return False;
2225 * For the moment we can't do this if we move icons in Clip from one
2226 * workspace to other, because if we move two or more icons without
2227 * command, the dialog box will not be able to tell us to which of the
2228 * moved icons it applies. -Dan
2230 if ((dest->type == WM_DOCK /*|| dest->keep_attracted */ ) && icon->command == NULL) {
2231 /* If icon->owner exists, it means the application is running */
2232 if (icon->icon->owner) {
2233 wwin = icon->icon->owner;
2234 command = GetCommandForWindow(wwin->client_win);
2237 if (command) {
2238 icon->command = command;
2239 } else {
2240 icon->editing = 1;
2241 /* icon->forced_dock = 1; */
2242 if (wInputDialog(src->screen_ptr, _("Dock Icon"),
2243 _("Type the command used to launch the application"), &command)) {
2244 if (command && (command[0] == 0 || (command[0] == '-' && command[1] == 0))) {
2245 wfree(command);
2246 command = NULL;
2248 icon->command = command;
2249 } else {
2250 icon->editing = 0;
2251 if (command)
2252 wfree(command);
2253 return False;
2255 icon->editing = 0;
2259 if (dest->type == WM_DOCK || dest->type == WM_DRAWER)
2260 wClipMakeIconOmnipresent(icon, False);
2262 for (index = 1; index < src->max_icons; index++) {
2263 if (src->icon_array[index] == icon)
2264 break;
2266 assert(index < src->max_icons);
2268 src->icon_array[index] = NULL;
2269 src->icon_count--;
2271 for (index = 1; index < dest->max_icons; index++) {
2272 if (dest->icon_array[index] == NULL)
2273 break;
2276 assert(index < dest->max_icons);
2278 dest->icon_array[index] = icon;
2279 icon->dock = dest;
2281 /* deselect the icon */
2282 if (icon->icon->selected)
2283 wIconSelect(icon->icon);
2285 icon->icon->core->descriptor.handle_enternotify = clipEnterNotify;
2286 icon->icon->core->descriptor.handle_leavenotify = clipLeaveNotify;
2288 /* set it to be kept when moving to dock.
2289 * Unless the icon does not have a command set
2291 if (icon->command && (dest->type == WM_DOCK || dest->type == WM_DRAWER)) {
2292 icon->attracted = 0;
2293 if (icon->icon->shadowed) {
2294 icon->icon->shadowed = 0;
2295 update_icon = True;
2297 save_appicon(icon, True);
2300 if (src->auto_collapse || src->auto_raise_lower)
2301 clipLeave(src);
2303 icon->yindex = y;
2304 icon->xindex = x;
2306 icon->x_pos = dest->x_pos + x * ICON_SIZE;
2307 icon->y_pos = dest->y_pos + y * ICON_SIZE;
2309 dest->icon_count++;
2311 MoveInStackListUnder(dest->icon_array[index - 1]->icon->core, icon->icon->core);
2314 * Update icon pixmap, RImage doesn't change,
2315 * so call wIconUpdate is not needed
2317 if (update_icon)
2318 update_icon_pixmap(icon->icon);
2320 /* Paint it */
2321 wAppIconPaint(icon);
2323 return True;
2326 void wDockDetach(WDock *dock, WAppIcon *icon)
2328 int index;
2329 Bool update_icon = False;
2331 /* make the settings panel be closed */
2332 if (icon->panel)
2333 DestroyDockAppSettingsPanel(icon->panel);
2335 /* This must be called before icon->dock is set to NULL.
2336 * Don't move it. -Dan
2338 wClipMakeIconOmnipresent(icon, False);
2340 icon->docked = 0;
2341 icon->dock = NULL;
2342 icon->attracted = 0;
2343 icon->auto_launch = 0;
2344 if (icon->icon->shadowed) {
2345 icon->icon->shadowed = 0;
2346 update_icon = True;
2349 /* deselect the icon */
2350 if (icon->icon->selected)
2351 wIconSelect(icon->icon);
2353 if (icon->command) {
2354 wfree(icon->command);
2355 icon->command = NULL;
2357 #ifdef XDND /* was OFFIX */
2358 if (icon->dnd_command) {
2359 wfree(icon->dnd_command);
2360 icon->dnd_command = NULL;
2362 #endif
2363 if (icon->paste_command) {
2364 wfree(icon->paste_command);
2365 icon->paste_command = NULL;
2368 for (index = 1; index < dock->max_icons; index++)
2369 if (dock->icon_array[index] == icon)
2370 break;
2372 assert(index < dock->max_icons);
2373 dock->icon_array[index] = NULL;
2374 icon->yindex = -1;
2375 icon->xindex = -1;
2377 dock->icon_count--;
2379 /* if the dock is not attached to an application or
2380 * the application did not set the appropriate hints yet,
2381 * destroy the icon */
2382 if (!icon->running || !wApplicationOf(icon->main_window)) {
2383 wAppIconDestroy(icon);
2384 } else {
2385 icon->icon->core->descriptor.handle_mousedown = appIconMouseDown;
2386 icon->icon->core->descriptor.handle_enternotify = NULL;
2387 icon->icon->core->descriptor.handle_leavenotify = NULL;
2388 icon->icon->core->descriptor.parent_type = WCLASS_APPICON;
2389 icon->icon->core->descriptor.parent = icon;
2391 ChangeStackingLevel(icon->icon->core, NORMAL_ICON_LEVEL);
2394 * Update icon pixmap, RImage doesn't change,
2395 * so call wIconUpdate is not needed
2397 if (update_icon)
2398 update_icon_pixmap(icon->icon);
2400 /* Paint it */
2401 wAppIconPaint(icon);
2403 if (wPreferences.auto_arrange_icons)
2404 wArrangeIcons(dock->screen_ptr, True);
2406 if (dock->auto_collapse || dock->auto_raise_lower)
2407 clipLeave(dock);
2411 * returns the closest Dock slot index for the passed
2412 * coordinates.
2414 * Returns False if icon can't be docked.
2416 * Note: this function should NEVER alter ret_x or ret_y, unless it will
2417 * return True. -Dan
2419 /* Redocking == true means either icon->dock == dock (normal case)
2420 * or we are called from handleDockMove for a drawer */
2421 Bool wDockSnapIcon(WDock *dock, WAppIcon *icon, int req_x, int req_y, int *ret_x, int *ret_y, int redocking)
2423 WScreen *scr = dock->screen_ptr;
2424 int dx, dy;
2425 int ex_x, ex_y;
2426 int i, offset = ICON_SIZE / 2;
2427 WAppIcon *aicon = NULL;
2428 WAppIcon *nicon = NULL;
2430 if (wPreferences.flags.noupdates)
2431 return False;
2433 dx = dock->x_pos;
2434 dy = dock->y_pos;
2436 /* if the dock is full */
2437 if (!redocking && (dock->icon_count >= dock->max_icons))
2438 return False;
2440 /* exact position */
2441 if (req_y < dy)
2442 ex_y = (req_y - offset - dy) / ICON_SIZE;
2443 else
2444 ex_y = (req_y + offset - dy) / ICON_SIZE;
2446 if (req_x < dx)
2447 ex_x = (req_x - offset - dx) / ICON_SIZE;
2448 else
2449 ex_x = (req_x + offset - dx) / ICON_SIZE;
2451 /* check if the icon is outside the screen boundaries */
2452 if (!onScreen(scr, dx + ex_x * ICON_SIZE, dy + ex_y * ICON_SIZE))
2453 return False;
2455 switch (dock->type) {
2456 case WM_DOCK:
2457 /* We can return False right away if
2458 * - we do not come from this dock (which is a WM_DOCK),
2459 * - we are not right over it, and
2460 * - we are not the main tile of a drawer.
2461 * In the latter case, we are called from handleDockMove. */
2462 if (icon->dock != dock && ex_x != 0 &&
2463 !(icon->dock && icon->dock->type == WM_DRAWER && icon == icon->dock->icon_array[0]))
2464 return False;
2466 if (!redocking && ex_x != 0)
2467 return False;
2469 if (getDrawer(scr, ex_y)) /* Return false so that the drawer gets it. */
2470 return False;
2472 aicon = NULL;
2473 for (i = 0; i < dock->max_icons; i++) {
2474 nicon = dock->icon_array[i];
2475 if (nicon && nicon->yindex == ex_y) {
2476 aicon = nicon;
2477 break;
2481 if (redocking) {
2482 int sig, done, closest;
2484 /* Possible cases when redocking:
2486 * icon dragged out of range of any slot -> false
2487 * icon dragged on a drawer -> false (to open the drawer)
2488 * icon dragged to range of free slot
2489 * icon dragged to range of same slot
2490 * icon dragged to range of different icon
2492 if (abs(ex_x) > DOCK_DETTACH_THRESHOLD)
2493 return False;
2495 if (aicon == icon || !aicon) {
2496 *ret_x = 0;
2497 *ret_y = ex_y;
2498 return True;
2501 /* start looking at the upper slot or lower? */
2502 if (ex_y * ICON_SIZE < (req_y + offset - dy))
2503 sig = 1;
2504 else
2505 sig = -1;
2507 done = 0;
2508 /* look for closest free slot */
2509 for (i = 0; i < (DOCK_DETTACH_THRESHOLD + 1) * 2 && !done; i++) {
2510 int j;
2512 done = 1;
2513 closest = sig * (i / 2) + ex_y;
2514 /* check if this slot is fully on the screen and not used */
2515 if (onScreen(scr, dx, dy + closest * ICON_SIZE)) {
2516 for (j = 0; j < dock->max_icons; j++) {
2517 if (dock->icon_array[j]
2518 && dock->icon_array[j]->yindex == closest) {
2519 /* slot is used by someone else */
2520 if (dock->icon_array[j] != icon)
2521 done = 0;
2522 break;
2525 /* slot is used by a drawer */
2526 done = done && !getDrawer(scr, closest);
2528 else // !onScreen
2529 done = 0;
2530 sig = -sig;
2532 if (done &&
2533 ((ex_y >= closest && ex_y - closest < DOCK_DETTACH_THRESHOLD + 1)
2534 || (ex_y < closest && closest - ex_y <= DOCK_DETTACH_THRESHOLD + 1))) {
2535 *ret_x = 0;
2536 *ret_y = closest;
2537 return True;
2539 } else { /* !redocking */
2541 /* if slot is free and the icon is close enough, return it */
2542 if (!aicon && ex_x == 0) {
2543 *ret_x = 0;
2544 *ret_y = ex_y;
2545 return True;
2548 break;
2549 case WM_CLIP:
2551 int neighbours = 0;
2552 int start, stop, k;
2554 start = icon->omnipresent ? 0 : w_global.workspace.current;
2555 stop = icon->omnipresent ? w_global.workspace.count : start + 1;
2557 aicon = NULL;
2558 for (k = start; k < stop; k++) {
2559 WDock *tmp = w_global.workspace.array[k]->clip;
2560 if (!tmp)
2561 continue;
2562 for (i = 0; i < tmp->max_icons; i++) {
2563 nicon = tmp->icon_array[i];
2564 if (nicon && nicon->xindex == ex_x && nicon->yindex == ex_y) {
2565 aicon = nicon;
2566 break;
2569 if (aicon)
2570 break;
2572 for (k = start; k < stop; k++) {
2573 WDock *tmp = w_global.workspace.array[k]->clip;
2574 if (!tmp)
2575 continue;
2576 for (i = 0; i < tmp->max_icons; i++) {
2577 nicon = tmp->icon_array[i];
2578 if (nicon && nicon != icon && /* Icon can't be it's own neighbour */
2579 (abs(nicon->xindex - ex_x) <= CLIP_ATTACH_VICINITY &&
2580 abs(nicon->yindex - ex_y) <= CLIP_ATTACH_VICINITY)) {
2581 neighbours = 1;
2582 break;
2585 if (neighbours)
2586 break;
2589 if (neighbours && (aicon == NULL || (redocking && aicon == icon))) {
2590 *ret_x = ex_x;
2591 *ret_y = ex_y;
2592 return True;
2594 break;
2596 case WM_DRAWER:
2598 WAppIcon *aicons_to_shift[ dock->icon_count ];
2599 int index_of_hole, j;
2601 if (ex_y != 0 ||
2602 abs(ex_x) - dock->icon_count > DOCK_DETTACH_THRESHOLD ||
2603 (ex_x < 0 && !dock->on_right_side) ||
2604 (ex_x > 0 && dock->on_right_side)) {
2605 return False;
2608 if (ex_x == 0)
2609 ex_x = (dock->on_right_side ? -1 : 1);
2611 /* "Reduce" ex_x but keep its sign */
2612 if (redocking) {
2613 if (abs(ex_x) > dock->icon_count - 1) /* minus 1: do not take icon_array[0] into account */
2614 ex_x = ex_x * (dock->icon_count - 1) / abs(ex_x); /* don't use *= ! */
2615 } else {
2616 if (abs(ex_x) > dock->icon_count)
2617 ex_x = ex_x * dock->icon_count / abs(ex_x);
2619 index_of_hole = indexOfHole(dock, icon, redocking);
2621 /* Find the appicons between where icon was (index_of_hole) and where
2622 * it wants to be (ex_x) and slide them. */
2623 j = 0;
2624 for (i = 1; i < dock->max_icons; i++) {
2625 if ((aicon = dock->icon_array[ i ]) && aicon != icon &&
2626 ((ex_x <= aicon->xindex && aicon->xindex < index_of_hole) ||
2627 (index_of_hole < aicon->xindex && aicon->xindex <= ex_x)))
2628 aicons_to_shift[ j++ ] = aicon;
2630 assert(j == abs(ex_x - index_of_hole));
2632 wSlideAppicons(aicons_to_shift, j, (index_of_hole < ex_x));
2634 *ret_x = ex_x;
2635 *ret_y = ex_y;
2636 return True;
2639 return False;
2642 static int onScreen(WScreen *scr, int x, int y)
2644 WMRect rect;
2645 int flags;
2647 rect.pos.x = x;
2648 rect.pos.y = y;
2649 rect.size.width = rect.size.height = ICON_SIZE;
2651 wGetRectPlacementInfo(scr, rect, &flags);
2653 return !(flags & (XFLAG_DEAD | XFLAG_PARTIAL));
2657 * returns true if it can find a free slot in the dock,
2658 * in which case it changes x_pos and y_pos accordingly.
2659 * Else returns false.
2661 Bool wDockFindFreeSlot(WDock *dock, int *x_pos, int *y_pos)
2663 WScreen *scr = dock->screen_ptr;
2664 WAppIcon *btn;
2665 WAppIconChain *chain;
2666 unsigned char *slot_map;
2667 int mwidth;
2668 int r;
2669 int x, y;
2670 int i, done = False;
2671 int corner;
2672 int sx = 0, ex = scr->scr_width, ey = scr->scr_height;
2673 int extra_count = 0;
2675 if (dock->type == WM_DRAWER) {
2676 if (dock->icon_count >= dock->max_icons) { /* drawer is full */
2677 return False;
2679 *x_pos = dock->icon_count * (dock->on_right_side ? -1 : 1);
2680 *y_pos = 0;
2681 return True;
2684 if (dock->type == WM_CLIP && dock != w_global.workspace.array[w_global.workspace.current]->clip)
2685 extra_count = scr->global_icon_count;
2687 /* if the dock is full */
2688 if (dock->icon_count + extra_count >= dock->max_icons)
2689 return False;
2691 if (!wPreferences.flags.nodock && scr->dock) {
2692 if (scr->dock->on_right_side)
2693 ex -= ICON_SIZE + DOCK_EXTRA_SPACE;
2694 else
2695 sx += ICON_SIZE + DOCK_EXTRA_SPACE;
2698 if (ex < dock->x_pos)
2699 ex = dock->x_pos;
2700 if (sx > dock->x_pos + ICON_SIZE)
2701 sx = dock->x_pos + ICON_SIZE;
2702 #define C_NONE 0
2703 #define C_NW 1
2704 #define C_NE 2
2705 #define C_SW 3
2706 #define C_SE 4
2708 /* check if clip is in a corner */
2709 if (dock->type == WM_CLIP) {
2710 if (dock->x_pos < 1 && dock->y_pos < 1)
2711 corner = C_NE;
2712 else if (dock->x_pos < 1 && dock->y_pos >= (ey - ICON_SIZE))
2713 corner = C_SE;
2714 else if (dock->x_pos >= (ex - ICON_SIZE) && dock->y_pos >= (ey - ICON_SIZE))
2715 corner = C_SW;
2716 else if (dock->x_pos >= (ex - ICON_SIZE) && dock->y_pos < 1)
2717 corner = C_NW;
2718 else
2719 corner = C_NONE;
2720 } else {
2721 corner = C_NONE;
2724 /* If the clip is in the corner, use only slots that are in the border
2725 * of the screen */
2726 if (corner != C_NONE) {
2727 char *hmap, *vmap;
2728 int hcount, vcount;
2730 hcount = WMIN(dock->max_icons, scr->scr_width / ICON_SIZE);
2731 vcount = WMIN(dock->max_icons, scr->scr_height / ICON_SIZE);
2732 hmap = wmalloc(hcount + 1);
2733 vmap = wmalloc(vcount + 1);
2735 /* mark used positions */
2736 switch (corner) {
2737 case C_NE:
2738 for (i = 0; i < dock->max_icons; i++) {
2739 btn = dock->icon_array[i];
2740 if (!btn)
2741 continue;
2743 if (btn->xindex == 0 && btn->yindex > 0 && btn->yindex < vcount)
2744 vmap[btn->yindex] = 1;
2745 else if (btn->yindex == 0 && btn->xindex > 0 && btn->xindex < hcount)
2746 hmap[btn->xindex] = 1;
2748 for (chain = scr->global_icons; chain != NULL; chain = chain->next) {
2749 btn = chain->aicon;
2750 if (btn->xindex == 0 && btn->yindex > 0 && btn->yindex < vcount)
2751 vmap[btn->yindex] = 1;
2752 else if (btn->yindex == 0 && btn->xindex > 0 && btn->xindex < hcount)
2753 hmap[btn->xindex] = 1;
2755 break;
2756 case C_NW:
2757 for (i = 0; i < dock->max_icons; i++) {
2758 btn = dock->icon_array[i];
2759 if (!btn)
2760 continue;
2762 if (btn->xindex == 0 && btn->yindex > 0 && btn->yindex < vcount)
2763 vmap[btn->yindex] = 1;
2764 else if (btn->yindex == 0 && btn->xindex < 0 && btn->xindex > -hcount)
2765 hmap[-btn->xindex] = 1;
2767 for (chain = scr->global_icons; chain != NULL; chain = chain->next) {
2768 btn = chain->aicon;
2769 if (btn->xindex == 0 && btn->yindex > 0 && btn->yindex < vcount)
2770 vmap[btn->yindex] = 1;
2771 else if (btn->yindex == 0 && btn->xindex < 0 && btn->xindex > -hcount)
2772 hmap[-btn->xindex] = 1;
2774 break;
2775 case C_SE:
2776 for (i = 0; i < dock->max_icons; i++) {
2777 btn = dock->icon_array[i];
2778 if (!btn)
2779 continue;
2781 if (btn->xindex == 0 && btn->yindex < 0 && btn->yindex > -vcount)
2782 vmap[-btn->yindex] = 1;
2783 else if (btn->yindex == 0 && btn->xindex > 0 && btn->xindex < hcount)
2784 hmap[btn->xindex] = 1;
2786 for (chain = scr->global_icons; chain != NULL; chain = chain->next) {
2787 btn = chain->aicon;
2788 if (btn->xindex == 0 && btn->yindex < 0 && btn->yindex > -vcount)
2789 vmap[-btn->yindex] = 1;
2790 else if (btn->yindex == 0 && btn->xindex > 0 && btn->xindex < hcount)
2791 hmap[btn->xindex] = 1;
2793 break;
2794 case C_SW:
2795 default:
2796 for (i = 0; i < dock->max_icons; i++) {
2797 btn = dock->icon_array[i];
2798 if (!btn)
2799 continue;
2801 if (btn->xindex == 0 && btn->yindex < 0 && btn->yindex > -vcount)
2802 vmap[-btn->yindex] = 1;
2803 else if (btn->yindex == 0 && btn->xindex < 0 && btn->xindex > -hcount)
2804 hmap[-btn->xindex] = 1;
2806 for (chain = scr->global_icons; chain != NULL; chain = chain->next) {
2807 btn = chain->aicon;
2808 if (btn->xindex == 0 && btn->yindex < 0 && btn->yindex > -vcount)
2809 vmap[-btn->yindex] = 1;
2810 else if (btn->yindex == 0 && btn->xindex < 0 && btn->xindex > -hcount)
2811 hmap[-btn->xindex] = 1;
2814 x = 0;
2815 y = 0;
2816 done = 0;
2817 /* search a vacant slot */
2818 for (i = 1; i < WMAX(vcount, hcount); i++) {
2819 if (i < vcount && vmap[i] == 0) {
2820 /* found a slot */
2821 x = 0;
2822 y = i;
2823 done = 1;
2824 break;
2825 } else if (i < hcount && hmap[i] == 0) {
2826 /* found a slot */
2827 x = i;
2828 y = 0;
2829 done = 1;
2830 break;
2833 wfree(vmap);
2834 wfree(hmap);
2835 /* If found a slot, translate and return */
2836 if (done) {
2837 if (corner == C_NW || corner == C_NE)
2838 *y_pos = y;
2839 else
2840 *y_pos = -y;
2842 if (corner == C_NE || corner == C_SE)
2843 *x_pos = x;
2844 else
2845 *x_pos = -x;
2847 return True;
2849 /* else, try to find a slot somewhere else */
2852 /* a map of mwidth x mwidth would be enough if we allowed icons to be
2853 * placed outside of screen */
2854 mwidth = (int)ceil(sqrt(dock->max_icons));
2856 /* In the worst case (the clip is in the corner of the screen),
2857 * the amount of icons that fit in the clip is smaller.
2858 * Double the map to get a safe value.
2860 mwidth += mwidth;
2862 r = (mwidth - 1) / 2;
2864 slot_map = wmalloc(mwidth * mwidth);
2866 #define XY2OFS(x,y) (WMAX(abs(x),abs(y)) > r) ? 0 : (((y)+r)*(mwidth)+(x)+r)
2868 /* mark used slots in the map. If the slot falls outside the map
2869 * (for example, when all icons are placed in line), ignore them. */
2870 for (i = 0; i < dock->max_icons; i++) {
2871 btn = dock->icon_array[i];
2872 if (btn)
2873 slot_map[XY2OFS(btn->xindex, btn->yindex)] = 1;
2876 for (chain = scr->global_icons; chain != NULL; chain = chain->next)
2877 slot_map[XY2OFS(chain->aicon->xindex, chain->aicon->yindex)] = 1;
2879 /* Find closest slot from the center that is free by scanning the
2880 * map from the center to outward in circular passes.
2881 * This will not result in a neat layout, but will be optimal
2882 * in the sense that there will not be holes left.
2884 done = 0;
2885 for (i = 1; i <= r && !done; i++) {
2886 int tx, ty;
2888 /* top and bottom parts of the ring */
2889 for (x = -i; x <= i && !done; x++) {
2890 tx = dock->x_pos + x * ICON_SIZE;
2891 y = -i;
2892 ty = dock->y_pos + y * ICON_SIZE;
2893 if (slot_map[XY2OFS(x, y)] == 0 && onScreen(scr, tx, ty)) {
2894 *x_pos = x;
2895 *y_pos = y;
2896 done = 1;
2897 break;
2899 y = i;
2900 ty = dock->y_pos + y * ICON_SIZE;
2901 if (slot_map[XY2OFS(x, y)] == 0 && onScreen(scr, tx, ty)) {
2902 *x_pos = x;
2903 *y_pos = y;
2904 done = 1;
2905 break;
2908 /* left and right parts of the ring */
2909 for (y = -i + 1; y <= i - 1; y++) {
2910 ty = dock->y_pos + y * ICON_SIZE;
2911 x = -i;
2912 tx = dock->x_pos + x * ICON_SIZE;
2913 if (slot_map[XY2OFS(x, y)] == 0 && onScreen(scr, tx, ty)) {
2914 *x_pos = x;
2915 *y_pos = y;
2916 done = 1;
2917 break;
2919 x = i;
2920 tx = dock->x_pos + x * ICON_SIZE;
2921 if (slot_map[XY2OFS(x, y)] == 0 && onScreen(scr, tx, ty)) {
2922 *x_pos = x;
2923 *y_pos = y;
2924 done = 1;
2925 break;
2929 wfree(slot_map);
2930 #undef XY2OFS
2931 return done;
2934 static void moveDock(WDock *dock, int new_x, int new_y)
2936 WAppIcon *btn;
2937 WDrawerChain *dc;
2938 int i;
2940 if (dock->type == WM_DOCK) {
2941 for (dc = dock->screen_ptr->drawers; dc != NULL; dc = dc->next)
2942 moveDock(dc->adrawer, new_x, dc->adrawer->y_pos - dock->y_pos + new_y);
2945 dock->x_pos = new_x;
2946 dock->y_pos = new_y;
2947 for (i = 0; i < dock->max_icons; i++) {
2948 btn = dock->icon_array[i];
2949 if (btn) {
2950 btn->x_pos = new_x + btn->xindex * ICON_SIZE;
2951 btn->y_pos = new_y + btn->yindex * ICON_SIZE;
2952 XMoveWindow(dpy, btn->icon->core->window, btn->x_pos, btn->y_pos);
2957 static void swapDock(WDock *dock)
2959 WScreen *scr = dock->screen_ptr;
2960 WAppIcon *btn;
2961 int x, i;
2963 if (dock->on_right_side)
2964 x = dock->x_pos = scr->scr_width - ICON_SIZE - DOCK_EXTRA_SPACE;
2965 else
2966 x = dock->x_pos = DOCK_EXTRA_SPACE;
2968 swapDrawers(scr, dock->on_right_side, x);
2970 for (i = 0; i < dock->max_icons; i++) {
2971 btn = dock->icon_array[i];
2972 if (btn) {
2973 btn->x_pos = x;
2974 XMoveWindow(dpy, btn->icon->core->window, btn->x_pos, btn->y_pos);
2978 wScreenUpdateUsableArea(scr);
2981 static pid_t execCommand(WAppIcon *btn, const char *command, WSavedState *state)
2983 WScreen *scr = btn->icon->core->screen_ptr;
2984 pid_t pid;
2985 char **argv;
2986 int argc;
2987 char *cmdline;
2989 cmdline = ExpandOptions(scr, command);
2991 if (scr->flags.dnd_data_convertion_status || !cmdline) {
2992 if (cmdline)
2993 wfree(cmdline);
2994 if (state)
2995 wfree(state);
2996 return 0;
2999 wtokensplit(cmdline, &argv, &argc);
3001 if (!argc) {
3002 if (cmdline)
3003 wfree(cmdline);
3004 if (state)
3005 wfree(state);
3006 return 0;
3009 if ((pid = fork()) == 0) {
3010 char **args;
3011 int i;
3013 SetupEnvironment(scr);
3015 #ifdef HAVE_SETSID
3016 setsid();
3017 #endif
3019 args = malloc(sizeof(char *) * (argc + 1));
3020 if (!args)
3021 exit(111);
3023 for (i = 0; i < argc; i++)
3024 args[i] = argv[i];
3026 args[argc] = NULL;
3027 execvp(argv[0], args);
3028 exit(111);
3030 wtokenfree(argv, argc);
3032 if (pid > 0) {
3033 if (!state) {
3034 state = wmalloc(sizeof(WSavedState));
3035 state->hidden = -1;
3036 state->miniaturized = -1;
3037 state->shaded = -1;
3038 if (btn->dock == scr->dock || btn->dock->type == WM_DRAWER || btn->omnipresent)
3039 state->workspace = -1;
3040 else
3041 state->workspace = w_global.workspace.current;
3043 wWindowAddSavedState(btn->wm_instance, btn->wm_class, cmdline, pid, state);
3044 wAddDeathHandler(pid, (WDeathHandler *) trackDeadProcess, btn->dock);
3045 } else if (state) {
3046 wfree(state);
3048 wfree(cmdline);
3049 return pid;
3052 void wDockHideIcons(WDock *dock)
3054 int i;
3056 if (dock == NULL)
3057 return;
3059 for (i = 1; i < dock->max_icons; i++) {
3060 if (dock->icon_array[i])
3061 XUnmapWindow(dpy, dock->icon_array[i]->icon->core->window);
3063 dock->mapped = 0;
3065 dockIconPaint(dock->icon_array[0]);
3068 void wDockShowIcons(WDock *dock)
3070 int i;
3071 WAppIcon *btn;
3073 if (dock == NULL)
3074 return;
3076 btn = dock->icon_array[0];
3077 moveDock(dock, btn->x_pos, btn->y_pos);
3079 /* Deleting any change in stacking level, this function is now only about
3080 mapping icons */
3082 if (!dock->collapsed) {
3083 for (i = 1; i < dock->max_icons; i++) {
3084 if (dock->icon_array[i])
3085 XMapWindow(dpy, dock->icon_array[i]->icon->core->window);
3088 dock->mapped = 1;
3090 dockIconPaint(btn);
3093 void wDockLower(WDock *dock)
3095 int i;
3096 WDrawerChain *dc;
3098 if (dock->type == WM_DOCK) {
3099 for (dc = dock->screen_ptr->drawers; dc != NULL; dc = dc->next)
3100 wDockLower(dc->adrawer);
3102 for (i = 0; i < dock->max_icons; i++) {
3103 if (dock->icon_array[i])
3104 wLowerFrame(dock->icon_array[i]->icon->core);
3108 void wDockRaise(WDock *dock)
3110 int i;
3111 WDrawerChain *dc;
3113 for (i = dock->max_icons - 1; i >= 0; i--) {
3114 if (dock->icon_array[i])
3115 wRaiseFrame(dock->icon_array[i]->icon->core);
3117 if (dock->type == WM_DOCK) {
3118 for (dc = dock->screen_ptr->drawers; dc != NULL; dc = dc->next)
3119 wDockRaise(dc->adrawer);
3123 void wDockRaiseLower(WDock *dock)
3125 if (!dock->icon_array[0]->icon->core->stacking->above
3126 || (dock->icon_array[0]->icon->core->stacking->window_level
3127 != dock->icon_array[0]->icon->core->stacking->above->stacking->window_level))
3128 wDockLower(dock);
3129 else
3130 wDockRaise(dock);
3133 void wDockFinishLaunch(WDock *dock, WAppIcon *icon)
3135 icon->launching = 0;
3136 icon->relaunching = 0;
3137 dockIconPaint(icon);
3140 WAppIcon *wDockFindIconForWindow(WDock *dock, Window window)
3142 WAppIcon *icon;
3143 int i;
3145 for (i = 0; i < dock->max_icons; i++) {
3146 icon = dock->icon_array[i];
3147 if (icon && icon->main_window == window)
3148 return icon;
3150 return NULL;
3153 void wDockTrackWindowLaunch(WDock *dock, Window window)
3155 WAppIcon *icon;
3156 char *wm_class, *wm_instance;
3157 int i;
3158 Bool firstPass = True;
3159 Bool found = False;
3160 char *command = NULL;
3162 if (!PropGetWMClass(window, &wm_class, &wm_instance) || (!wm_class && !wm_instance))
3163 return;
3165 command = GetCommandForWindow(window);
3166 retry:
3167 for (i = 0; i < dock->max_icons; i++) {
3168 icon = dock->icon_array[i];
3169 if (!icon)
3170 continue;
3172 /* app is already attached to icon */
3173 if (icon->main_window == window) {
3174 found = True;
3175 break;
3178 if ((icon->wm_instance || icon->wm_class)
3179 && (icon->launching || !icon->running)) {
3181 if (icon->wm_instance && wm_instance && strcmp(icon->wm_instance, wm_instance) != 0)
3182 continue;
3184 if (icon->wm_class && wm_class && strcmp(icon->wm_class, wm_class) != 0)
3185 continue;
3187 if (firstPass && command && strcmp(icon->command, command) != 0)
3188 continue;
3190 if (!icon->relaunching) {
3191 WApplication *wapp;
3193 /* Possibly an application that was docked with dockit,
3194 * but the user did not update WMState to indicate that
3195 * it was docked by force */
3196 wapp = wApplicationOf(window);
3197 if (!wapp) {
3198 icon->forced_dock = 1;
3199 icon->running = 0;
3201 if (!icon->forced_dock)
3202 icon->main_window = window;
3204 found = True;
3205 if (!wPreferences.no_animations && !icon->launching &&
3206 !dock->screen_ptr->flags.startup && !dock->collapsed) {
3207 WAppIcon *aicon;
3208 int x0, y0;
3210 icon->launching = 1;
3211 dockIconPaint(icon);
3213 aicon = wAppIconCreateForDock(dock->screen_ptr, NULL,
3214 wm_instance, wm_class, TILE_NORMAL);
3215 /* XXX: can: aicon->icon == NULL ? */
3216 PlaceIcon(dock->screen_ptr, &x0, &y0, wGetHeadForWindow(aicon->icon->owner));
3217 wAppIconMove(aicon, x0, y0);
3218 /* Should this always be lowered? -Dan */
3219 if (dock->lowered)
3220 wLowerFrame(aicon->icon->core);
3221 XMapWindow(dpy, aicon->icon->core->window);
3222 aicon->launching = 1;
3223 wAppIconPaint(aicon);
3224 SlideWindow(aicon->icon->core->window, x0, y0, icon->x_pos, icon->y_pos);
3225 XUnmapWindow(dpy, aicon->icon->core->window);
3226 wAppIconDestroy(aicon);
3228 wDockFinishLaunch(dock, icon);
3229 break;
3233 if (firstPass && !found) {
3234 firstPass = False;
3235 goto retry;
3238 if (command)
3239 wfree(command);
3241 if (wm_class)
3242 free(wm_class);
3243 if (wm_instance)
3244 free(wm_instance);
3247 void wClipUpdateForWorkspaceChange(WScreen *scr, int workspace)
3249 if (!wPreferences.flags.noclip) {
3250 w_global.clip.icon->dock = w_global.workspace.array[workspace]->clip;
3251 if (w_global.workspace.current != workspace) {
3252 WDock *old_clip = w_global.workspace.array[w_global.workspace.current]->clip;
3253 WAppIconChain *chain = scr->global_icons;
3255 while (chain) {
3256 wDockMoveIconBetweenDocks(chain->aicon->dock,
3257 w_global.workspace.array[workspace]->clip,
3258 chain->aicon, chain->aicon->xindex, chain->aicon->yindex);
3259 if (w_global.workspace.array[workspace]->clip->collapsed)
3260 XUnmapWindow(dpy, chain->aicon->icon->core->window);
3261 chain = chain->next;
3264 wDockHideIcons(old_clip);
3265 if (old_clip->auto_raise_lower) {
3266 if (old_clip->auto_raise_magic) {
3267 WMDeleteTimerHandler(old_clip->auto_raise_magic);
3268 old_clip->auto_raise_magic = NULL;
3270 wDockLower(old_clip);
3272 if (old_clip->auto_collapse) {
3273 if (old_clip->auto_expand_magic) {
3274 WMDeleteTimerHandler(old_clip->auto_expand_magic);
3275 old_clip->auto_expand_magic = NULL;
3277 old_clip->collapsed = 1;
3279 wDockShowIcons(w_global.workspace.array[workspace]->clip);
3284 static void trackDeadProcess(pid_t pid, unsigned char status, WDock *dock)
3286 WAppIcon *icon;
3287 int i;
3289 for (i = 0; i < dock->max_icons; i++) {
3290 icon = dock->icon_array[i];
3291 if (!icon)
3292 continue;
3294 if (icon->launching && icon->pid == pid) {
3295 if (!icon->relaunching) {
3296 icon->running = 0;
3297 icon->main_window = None;
3299 wDockFinishLaunch(dock, icon);
3300 icon->pid = 0;
3301 if (status == 111) {
3302 char msg[PATH_MAX];
3303 char *cmd;
3305 #ifdef XDND
3306 if (icon->drop_launch)
3307 cmd = icon->dnd_command;
3308 else
3309 #endif
3310 if (icon->paste_launch)
3311 cmd = icon->paste_command;
3312 else
3313 cmd = icon->command;
3315 snprintf(msg, sizeof(msg), _("Could not execute command \"%s\""), cmd);
3317 wMessageDialog(dock->screen_ptr, _("Error"), msg, _("OK"), NULL, NULL);
3319 break;
3324 /* This function is called when the dock switches state between
3325 * "normal" (including auto-raise/lower) and "keep on top". It is
3326 * therefore clearly distinct from wDockLower/Raise, which are called
3327 * each time a not-kept-on-top dock is lowered/raised. */
3328 static void toggleLowered(WDock *dock)
3330 WAppIcon *tmp;
3331 WDrawerChain *dc;
3332 int newlevel, i;
3334 if (!dock->lowered) {
3335 newlevel = WMNormalLevel;
3336 dock->lowered = 1;
3337 } else {
3338 newlevel = WMDockLevel;
3339 dock->lowered = 0;
3342 for (i = 0; i < dock->max_icons; i++) {
3343 tmp = dock->icon_array[i];
3344 if (!tmp)
3345 continue;
3347 ChangeStackingLevel(tmp->icon->core, newlevel);
3349 /* When the dock is no longer "on top", explicitly lower it as well.
3350 * It saves some CPU cycles (probably) to do it ourselves here
3351 * rather than calling wDockLower at the end of toggleLowered */
3352 if (dock->lowered)
3353 wLowerFrame(tmp->icon->core);
3356 if (dock->type == WM_DOCK) {
3357 for (dc = dock->screen_ptr->drawers; dc != NULL; dc = dc->next) {
3358 toggleLowered(dc->adrawer);
3360 wScreenUpdateUsableArea(dock->screen_ptr);
3364 static void toggleCollapsed(WDock *dock)
3366 if (dock->collapsed) {
3367 dock->collapsed = 0;
3368 wDockShowIcons(dock);
3369 } else {
3370 dock->collapsed = 1;
3371 wDockHideIcons(dock);
3375 static void openDockMenu(WDock *dock, WAppIcon *aicon, XEvent *event)
3377 WScreen *scr = dock->screen_ptr;
3378 WObjDescriptor *desc;
3379 WMenuEntry *entry;
3380 WApplication *wapp = NULL;
3381 int index = 0;
3382 int x_pos;
3383 int n_selected;
3384 int appIsRunning = aicon->running && aicon->icon && aicon->icon->owner;
3386 if (dock->type == WM_DOCK) {
3387 /* Dock position menu */
3388 updateDockPositionMenu(w_global.dock.pos_menu, dock);
3389 dock->menu->flags.realized = 0;
3390 if (!wPreferences.flags.nodrawer) {
3391 /* add a drawer */
3392 entry = dock->menu->entries[++index];
3393 entry->clientdata = aicon;
3394 wMenuSetEnabled(dock->menu, index, True);
3396 } else {
3397 /* clip/drawer options */
3398 if (w_global.clip.opt_menu)
3399 updateClipOptionsMenu(w_global.clip.opt_menu, dock);
3401 n_selected = numberOfSelectedIcons(dock);
3403 if (dock->type == WM_CLIP) {
3404 /* Rename Workspace */
3405 entry = dock->menu->entries[++index];
3406 if (aicon == w_global.clip.icon) {
3407 entry->callback = renameCallback;
3408 entry->clientdata = dock;
3409 entry->flags.indicator = 0;
3410 entry->text = _("Rename Workspace");
3411 } else {
3412 entry->callback = omnipresentCallback;
3413 entry->clientdata = aicon;
3414 if (n_selected > 0) {
3415 entry->flags.indicator = 0;
3416 entry->text = _("Toggle Omnipresent");
3417 } else {
3418 entry->flags.indicator = 1;
3419 entry->flags.indicator_on = aicon->omnipresent;
3420 entry->flags.indicator_type = MI_CHECK;
3421 entry->text = _("Omnipresent");
3426 /* select/unselect icon */
3427 entry = dock->menu->entries[++index];
3428 entry->clientdata = aicon;
3429 entry->flags.indicator_on = aicon->icon->selected;
3430 wMenuSetEnabled(dock->menu, index, aicon != w_global.clip.icon && !wIsADrawer(aicon));
3432 /* select/unselect all icons */
3433 entry = dock->menu->entries[++index];
3434 entry->clientdata = aicon;
3435 if (n_selected > 0)
3436 entry->text = _("Unselect All Icons");
3437 else
3438 entry->text = _("Select All Icons");
3440 wMenuSetEnabled(dock->menu, index, dock->icon_count > 1);
3442 /* keep icon(s) */
3443 entry = dock->menu->entries[++index];
3444 entry->clientdata = aicon;
3445 if (n_selected > 1)
3446 entry->text = _("Keep Icons");
3447 else
3448 entry->text = _("Keep Icon");
3450 wMenuSetEnabled(dock->menu, index, dock->icon_count > 1);
3452 if (dock->type == WM_CLIP) {
3453 /* this is the workspace submenu part */
3454 entry = dock->menu->entries[++index];
3455 if (n_selected > 1)
3456 entry->text = _("Move Icons To");
3457 else
3458 entry->text = _("Move Icon To");
3460 if (w_global.clip.submenu)
3461 updateWorkspaceMenu(w_global.clip.submenu, aicon);
3463 wMenuSetEnabled(dock->menu, index, !aicon->omnipresent);
3466 /* remove icon(s) */
3467 entry = dock->menu->entries[++index];
3468 entry->clientdata = aicon;
3469 if (n_selected > 1)
3470 entry->text = _("Remove Icons");
3471 else
3472 entry->text = _("Remove Icon");
3474 wMenuSetEnabled(dock->menu, index, dock->icon_count > 1);
3476 /* attract icon(s) */
3477 entry = dock->menu->entries[++index];
3478 entry->clientdata = aicon;
3480 dock->menu->flags.realized = 0;
3481 wMenuRealize(dock->menu);
3484 if (aicon->icon->owner)
3485 wapp = wApplicationOf(aicon->icon->owner->main_window);
3486 else
3487 wapp = NULL;
3489 /* launch */
3490 entry = dock->menu->entries[++index];
3491 entry->clientdata = aicon;
3492 wMenuSetEnabled(dock->menu, index, aicon->command != NULL);
3494 /* unhide here */
3495 entry = dock->menu->entries[++index];
3496 entry->clientdata = aicon;
3497 if (wapp && wapp->flags.hidden)
3498 entry->text = _("Unhide Here");
3499 else
3500 entry->text = _("Bring Here");
3502 wMenuSetEnabled(dock->menu, index, appIsRunning);
3504 /* hide */
3505 entry = dock->menu->entries[++index];
3506 entry->clientdata = aicon;
3507 if (wapp && wapp->flags.hidden)
3508 entry->text = _("Unhide");
3509 else
3510 entry->text = _("Hide");
3512 wMenuSetEnabled(dock->menu, index, appIsRunning);
3514 /* settings */
3515 entry = dock->menu->entries[++index];
3516 entry->clientdata = aicon;
3517 wMenuSetEnabled(dock->menu, index, !aicon->editing && !wPreferences.flags.noupdates);
3519 /* kill or remove drawer */
3520 entry = dock->menu->entries[++index];
3521 entry->clientdata = aicon;
3522 if (wIsADrawer(aicon)) {
3523 entry->callback = removeDrawerCallback;
3524 entry->text = _("Remove drawer");
3525 wMenuSetEnabled(dock->menu, index, True);
3526 } else {
3527 entry->callback = killCallback;
3528 entry->text = _("Kill");
3529 wMenuSetEnabled(dock->menu, index, appIsRunning);
3532 if (!dock->menu->flags.realized)
3533 wMenuRealize(dock->menu);
3535 if (dock->type == WM_CLIP || dock->type == WM_DRAWER) {
3536 /*x_pos = event->xbutton.x_root+2; */
3537 x_pos = event->xbutton.x_root - dock->menu->frame->core->width / 2 - 1;
3538 if (x_pos < 0) {
3539 x_pos = 0;
3540 } else if (x_pos + dock->menu->frame->core->width > scr->scr_width - 2) {
3541 x_pos = scr->scr_width - dock->menu->frame->core->width - 4;
3543 } else {
3544 x_pos = dock->on_right_side ? scr->scr_width - dock->menu->frame->core->width - 3 : 0;
3547 wMenuMapAt(dock->menu, x_pos, event->xbutton.y_root + 2, False);
3549 /* allow drag select */
3550 event->xany.send_event = True;
3551 desc = &dock->menu->menu->descriptor;
3552 (*desc->handle_mousedown) (desc, event);
3555 /******************************************************************/
3556 static void iconDblClick(WObjDescriptor *desc, XEvent *event)
3558 WAppIcon *btn = desc->parent;
3559 WDock *dock = btn->dock;
3560 WApplication *wapp = NULL;
3561 int unhideHere = 0;
3563 if (btn->icon->owner && !(event->xbutton.state & ControlMask)) {
3564 wapp = wApplicationOf(btn->icon->owner->main_window);
3566 assert(wapp != NULL);
3568 unhideHere = (event->xbutton.state & ShiftMask);
3570 /* go to the last workspace that the user worked on the app */
3571 if (wapp->last_workspace != w_global.workspace.current && !unhideHere)
3572 wWorkspaceChange(dock->screen_ptr, wapp->last_workspace);
3574 wUnhideApplication(wapp, event->xbutton.button == Button2, unhideHere);
3576 if (event->xbutton.state & MOD_MASK)
3577 wHideOtherApplications(btn->icon->owner);
3578 } else {
3579 if (event->xbutton.button == Button1) {
3580 if (event->xbutton.state & MOD_MASK) {
3581 /* raise/lower dock */
3582 toggleLowered(dock);
3583 } else if (btn == w_global.clip.icon) {
3584 if (getClipButton(event->xbutton.x, event->xbutton.y) != CLIP_IDLE)
3585 handleClipChangeWorkspace(dock->screen_ptr, event);
3586 else if (wPreferences.flags.clip_merged_in_dock) {
3587 // Is actually the dock
3588 if (btn->command)
3590 if (!btn->launching && (!btn->running || (event->xbutton.state & ControlMask)))
3591 launchDockedApplication(btn, False);
3593 else
3595 wShowInfoPanel(dock->screen_ptr);
3598 else
3599 toggleCollapsed(dock);
3600 } else if (wIsADrawer(btn)) {
3601 toggleCollapsed(dock);
3602 } else if (btn->command) {
3603 if (!btn->launching && (!btn->running || (event->xbutton.state & ControlMask)))
3604 launchDockedApplication(btn, False);
3605 } else if (btn->xindex == 0 && btn->yindex == 0 && btn->dock->type == WM_DOCK) {
3606 wShowInfoPanel(dock->screen_ptr);
3612 static void handleDockMove(WDock *dock, WAppIcon *aicon, XEvent *event)
3614 WScreen *scr = dock->screen_ptr;
3615 int ofs_x = event->xbutton.x, ofs_y = event->xbutton.y;
3616 WIcon *icon = aicon->icon;
3617 WAppIcon *tmpaicon;
3618 WDrawerChain *dc;
3619 int x = aicon->x_pos, y = aicon->y_pos;;
3620 int shad_x = x, shad_y = y;
3621 XEvent ev;
3622 int grabbed = 0, done, previously_on_right, now_on_right, previous_x_pos, i;
3623 Pixmap ghost = None;
3624 int superfluous = wPreferences.superfluous; /* we catch it to avoid problems */
3626 if (XGrabPointer(dpy, aicon->icon->core->window, True, ButtonMotionMask
3627 | ButtonReleaseMask | ButtonPressMask, GrabModeAsync,
3628 GrabModeAsync, None, None, CurrentTime) != GrabSuccess)
3629 wwarning("pointer grab failed for dock move");
3631 if (dock->type == WM_DRAWER) {
3632 Window wins[2];
3633 wins[0] = icon->core->window;
3634 wins[1] = scr->dock_shadow;
3635 XRestackWindows(dpy, wins, 2);
3636 XMoveResizeWindow(dpy, scr->dock_shadow, aicon->x_pos, aicon->y_pos,
3637 ICON_SIZE, ICON_SIZE);
3638 if (superfluous) {
3639 if (icon->pixmap!=None)
3640 ghost = MakeGhostIcon(scr, icon->pixmap);
3641 else
3642 ghost = MakeGhostIcon(scr, icon->core->window);
3644 XSetWindowBackgroundPixmap(dpy, scr->dock_shadow, ghost);
3645 XClearWindow(dpy, scr->dock_shadow);
3647 XMapWindow(dpy, scr->dock_shadow);
3650 previously_on_right = now_on_right = dock->on_right_side;
3651 previous_x_pos = dock->x_pos;
3652 done = 0;
3653 while (!done) {
3654 WMMaskEvent(dpy, PointerMotionMask | ButtonReleaseMask | ButtonPressMask
3655 | ButtonMotionMask | ExposureMask | EnterWindowMask, &ev);
3656 switch (ev.type) {
3657 case Expose:
3658 WMHandleEvent(&ev);
3659 break;
3661 case EnterNotify:
3662 /* It means the cursor moved so fast that it entered
3663 * something else (if moving slowly, it would have
3664 * stayed in the dock that is being moved. Ignore such
3665 * "spurious" EnterNotifiy's */
3666 break;
3668 case MotionNotify:
3669 if (!grabbed) {
3670 if (abs(ofs_x - ev.xmotion.x) >= MOVE_THRESHOLD
3671 || abs(ofs_y - ev.xmotion.y) >= MOVE_THRESHOLD) {
3672 XChangeActivePointerGrab(dpy, ButtonMotionMask
3673 | ButtonReleaseMask | ButtonPressMask,
3674 wPreferences.cursor[WCUR_MOVE], CurrentTime);
3675 grabbed = 1;
3677 break;
3679 switch (dock->type) {
3680 case WM_CLIP:
3681 x = ev.xmotion.x_root - ofs_x;
3682 y = ev.xmotion.y_root - ofs_y;
3683 wScreenKeepInside(scr, &x, &y, ICON_SIZE, ICON_SIZE);
3684 moveDock(dock, x, y);
3685 break;
3686 case WM_DOCK:
3687 x = ev.xmotion.x_root - ofs_x;
3688 y = ev.xmotion.y_root - ofs_y;
3689 if (previously_on_right)
3691 now_on_right = (ev.xmotion.x_root >= previous_x_pos - ICON_SIZE);
3693 else
3695 now_on_right = (ev.xmotion.x_root > previous_x_pos + ICON_SIZE * 2);
3697 if (now_on_right != dock->on_right_side)
3699 dock->on_right_side = now_on_right;
3700 swapDock(dock);
3701 wArrangeIcons(scr, False);
3703 // Also perform the vertical move
3704 wScreenKeepInside(scr, &x, &y, ICON_SIZE, ICON_SIZE);
3705 moveDock(dock, dock->x_pos, y);
3706 if (wPreferences.flags.wrap_appicons_in_dock)
3708 for (i = 0; i < dock->max_icons; i++) {
3709 int new_y, new_index, j, ok;
3710 if ((tmpaicon = dock->icon_array[i]) == NULL)
3711 continue;
3712 if (onScreen(scr, tmpaicon->x_pos, tmpaicon->y_pos))
3713 continue;
3714 new_y = (tmpaicon->y_pos + ICON_SIZE * dock->max_icons) % (ICON_SIZE * dock->max_icons);
3715 new_index = (new_y - dock->y_pos) / ICON_SIZE;
3716 if (!onScreen(scr, tmpaicon->x_pos, new_y))
3717 continue;
3718 ok = 1;
3719 for (j = 0; j < dock->max_icons; j++)
3721 if (dock->icon_array[j] != NULL &&
3722 dock->icon_array[j]->yindex == new_index)
3724 ok = 0;
3725 break;
3728 if (!ok || getDrawer(scr, new_index) != NULL)
3729 continue;
3730 wDockReattachIcon(dock, tmpaicon, tmpaicon->xindex, new_index);
3732 for (dc = scr->drawers; dc != NULL; dc = dc->next)
3734 int new_y, new_index, j, ok;
3735 tmpaicon = dc->adrawer->icon_array[0];
3736 if (onScreen(scr, tmpaicon->x_pos, tmpaicon->y_pos))
3737 continue;
3738 new_y = (tmpaicon->y_pos + ICON_SIZE * dock->max_icons) % (ICON_SIZE * dock->max_icons);
3739 new_index = (new_y - dock->y_pos) / ICON_SIZE;
3740 if (!onScreen(scr, tmpaicon->x_pos, new_y))
3741 continue;
3742 ok = 1;
3743 for (j = 0; j < dock->max_icons; j++)
3745 if (dock->icon_array[j] != NULL &&
3746 dock->icon_array[j]->yindex == new_index)
3748 ok = 0;
3749 break;
3752 if (!ok || getDrawer(scr, new_index) != NULL)
3753 continue;
3754 moveDock(dc->adrawer, tmpaicon->x_pos, new_y);
3757 break;
3758 case WM_DRAWER:
3760 WDock *real_dock = dock->screen_ptr->dock;
3761 Bool snapped;
3762 int ix, iy;
3763 x = ev.xmotion.x_root - ofs_x;
3764 y = ev.xmotion.y_root - ofs_y;
3765 snapped = wDockSnapIcon(real_dock, aicon, x, y, &ix, &iy, True);
3766 if (snapped) {
3767 shad_x = real_dock->x_pos + ix * wPreferences.icon_size;
3768 shad_y = real_dock->y_pos + iy * wPreferences.icon_size;
3769 XMoveWindow(dpy, scr->dock_shadow, shad_x, shad_y);
3771 moveDock(dock, x, y);
3772 break;
3775 break;
3777 case ButtonPress:
3778 break;
3780 case ButtonRelease:
3781 if (ev.xbutton.button != event->xbutton.button)
3782 break;
3783 XUngrabPointer(dpy, CurrentTime);
3784 if (dock->type == WM_DRAWER) {
3785 Window *wins[dock->icon_count];
3787 for (i = 0; i < dock->max_icons; i++) {
3788 if ((tmpaicon = dock->icon_array[i]) == NULL)
3789 continue;
3790 wins[ tmpaicon->xindex + (dock->on_right_side ? dock->icon_count - 1 : 0) ] = &tmpaicon->icon->core->window;
3792 SlideWindows(wins, dock->icon_count,
3793 (dock->on_right_side ? x - (dock->icon_count - 1) * ICON_SIZE : x),
3795 (dock->on_right_side ? shad_x - (dock->icon_count - 1) * ICON_SIZE : shad_x),
3796 shad_y);
3797 XUnmapWindow(dpy, scr->dock_shadow);
3798 moveDock(dock, shad_x, shad_y);
3799 XResizeWindow(dpy, scr->dock_shadow, ICON_SIZE, ICON_SIZE);
3801 done = 1;
3802 break;
3805 if (superfluous) {
3806 if (ghost != None)
3807 XFreePixmap(dpy, ghost);
3808 XSetWindowBackground(dpy, scr->dock_shadow, scr->white_pixel);
3813 static int getClipButton(int px, int py)
3815 int pt = (CLIP_BUTTON_SIZE + 2) * ICON_SIZE / 64;
3817 if (px < 0 || py < 0 || px >= ICON_SIZE || py >= ICON_SIZE)
3818 return CLIP_IDLE;
3820 if (py <= pt - ((int)ICON_SIZE - 1 - px))
3821 return CLIP_FORWARD;
3822 else if (px <= pt - ((int)ICON_SIZE - 1 - py))
3823 return CLIP_REWIND;
3825 return CLIP_IDLE;
3828 static void handleClipChangeWorkspace(WScreen *scr, XEvent *event)
3830 XEvent ev;
3831 int done, direction, new_ws;
3832 int new_dir;
3833 WDock *clip = w_global.clip.icon->dock;
3835 direction = getClipButton(event->xbutton.x, event->xbutton.y);
3837 clip->lclip_button_pushed = direction == CLIP_REWIND;
3838 clip->rclip_button_pushed = direction == CLIP_FORWARD;
3840 wClipIconPaint();
3841 done = 0;
3842 while (!done) {
3843 WMMaskEvent(dpy, ExposureMask | ButtonMotionMask | ButtonReleaseMask | ButtonPressMask, &ev);
3844 switch (ev.type) {
3845 case Expose:
3846 WMHandleEvent(&ev);
3847 break;
3849 case MotionNotify:
3850 new_dir = getClipButton(ev.xmotion.x, ev.xmotion.y);
3851 if (new_dir != direction) {
3852 direction = new_dir;
3853 clip->lclip_button_pushed = direction == CLIP_REWIND;
3854 clip->rclip_button_pushed = direction == CLIP_FORWARD;
3855 wClipIconPaint();
3857 break;
3859 case ButtonPress:
3860 break;
3862 case ButtonRelease:
3863 if (ev.xbutton.button == event->xbutton.button)
3864 done = 1;
3868 clip->lclip_button_pushed = 0;
3869 clip->rclip_button_pushed = 0;
3871 new_ws = wPreferences.ws_advance || (event->xbutton.state & ControlMask);
3873 if (direction == CLIP_FORWARD) {
3874 if (w_global.workspace.current < w_global.workspace.count - 1)
3875 wWorkspaceChange(scr, w_global.workspace.current + 1);
3876 else if (new_ws && w_global.workspace.current < MAX_WORKSPACES - 1)
3877 wWorkspaceChange(scr, w_global.workspace.current + 1);
3878 else if (wPreferences.ws_cycle)
3879 wWorkspaceChange(scr, 0);
3880 } else if (direction == CLIP_REWIND) {
3881 if (w_global.workspace.current > 0)
3882 wWorkspaceChange(scr, w_global.workspace.current - 1);
3883 else if (w_global.workspace.current == 0 && wPreferences.ws_cycle)
3884 wWorkspaceChange(scr, w_global.workspace.count - 1);
3887 wClipIconPaint();
3890 static void iconMouseDown(WObjDescriptor *desc, XEvent *event)
3892 WAppIcon *aicon = desc->parent;
3893 WDock *dock = aicon->dock;
3894 WScreen *scr = aicon->icon->core->screen_ptr;
3896 if (aicon->editing || WCHECK_STATE(WSTATE_MODAL))
3897 return;
3899 scr->last_dock = dock;
3901 if (dock->menu->flags.mapped)
3902 wMenuUnmap(dock->menu);
3904 if (IsDoubleClick(scr, event)) {
3905 /* double-click was not in the main clip icon */
3906 if (dock->type != WM_CLIP || aicon->xindex != 0 || aicon->yindex != 0
3907 || getClipButton(event->xbutton.x, event->xbutton.y) == CLIP_IDLE) {
3908 iconDblClick(desc, event);
3909 return;
3913 if (event->xbutton.button == Button1) {
3914 if (event->xbutton.state & MOD_MASK)
3915 wDockLower(dock);
3916 else
3917 wDockRaise(dock);
3919 if ((event->xbutton.state & ShiftMask) && aicon != w_global.clip.icon && dock->type != WM_DOCK) {
3920 wIconSelect(aicon->icon);
3921 return;
3924 if (aicon->yindex == 0 && aicon->xindex == 0) {
3925 if (getClipButton(event->xbutton.x, event->xbutton.y) != CLIP_IDLE &&
3926 (dock->type == WM_CLIP || (dock->type == WM_DOCK && wPreferences.flags.clip_merged_in_dock)))
3927 handleClipChangeWorkspace(scr, event);
3928 else
3929 handleDockMove(dock, aicon, event);
3930 } else {
3931 Bool hasMoved = wHandleAppIconMove(aicon, event);
3932 if (wPreferences.single_click && !hasMoved)
3933 iconDblClick(desc, event);
3935 } else if (event->xbutton.button == Button2 && aicon == w_global.clip.icon) {
3936 if (!w_global.clip.ws_menu)
3937 w_global.clip.ws_menu = wWorkspaceMenuMake(scr, False);
3939 if (w_global.clip.ws_menu) {
3940 WMenu *wsMenu = w_global.clip.ws_menu;
3941 int xpos;
3943 wWorkspaceMenuUpdate(wsMenu);
3945 xpos = event->xbutton.x_root - wsMenu->frame->core->width / 2 - 1;
3946 if (xpos < 0) {
3947 xpos = 0;
3948 } else if (xpos + wsMenu->frame->core->width > scr->scr_width - 2) {
3949 xpos = scr->scr_width - wsMenu->frame->core->width - 4;
3951 wMenuMapAt(wsMenu, xpos, event->xbutton.y_root + 2, False);
3953 desc = &wsMenu->menu->descriptor;
3954 event->xany.send_event = True;
3955 (*desc->handle_mousedown) (desc, event);
3957 } else if (event->xbutton.button == Button2 && dock->type == WM_CLIP &&
3958 (event->xbutton.state & ShiftMask) && aicon != w_global.clip.icon) {
3959 wClipMakeIconOmnipresent(aicon, !aicon->omnipresent);
3960 } else if (event->xbutton.button == Button3) {
3961 if (event->xbutton.send_event &&
3962 XGrabPointer(dpy, aicon->icon->core->window, True, ButtonMotionMask
3963 | ButtonReleaseMask | ButtonPressMask, GrabModeAsync,
3964 GrabModeAsync, None, None, CurrentTime) != GrabSuccess) {
3965 wwarning("pointer grab failed for dockicon menu");
3966 return;
3969 openDockMenu(dock, aicon, event);
3970 } else if (event->xbutton.button == Button2) {
3971 WAppIcon *btn = desc->parent;
3973 if (!btn->launching && (!btn->running || (event->xbutton.state & ControlMask)))
3974 launchDockedApplication(btn, True);
3978 static void clipEnterNotify(WObjDescriptor *desc, XEvent *event)
3980 WAppIcon *btn = (WAppIcon *) desc->parent;
3981 WDock *dock, *tmp;
3982 WScreen *scr;
3984 assert(event->type == EnterNotify);
3986 if (desc->parent_type != WCLASS_DOCK_ICON)
3987 return;
3989 scr = btn->icon->core->screen_ptr;
3990 dock = btn->dock;
3992 if (dock == NULL)
3993 return;
3995 /* The auto raise/lower code */
3996 tmp = (dock->type == WM_DRAWER ? scr->dock : dock);
3997 if (tmp->auto_lower_magic) {
3998 WMDeleteTimerHandler(tmp->auto_lower_magic);
3999 tmp->auto_lower_magic = NULL;
4001 if (tmp->auto_raise_lower && !tmp->auto_raise_magic)
4002 tmp->auto_raise_magic = WMAddTimerHandler(wPreferences.clip_auto_raise_delay, clipAutoRaise, (void *) tmp);
4004 if (dock->type != WM_CLIP && dock->type != WM_DRAWER)
4005 return;
4007 /* The auto expand/collapse code */
4008 if (dock->auto_collapse_magic) {
4009 WMDeleteTimerHandler(dock->auto_collapse_magic);
4010 dock->auto_collapse_magic = NULL;
4012 if (dock->auto_collapse && !dock->auto_expand_magic)
4013 dock->auto_expand_magic = WMAddTimerHandler(wPreferences.clip_auto_expand_delay, clipAutoExpand, (void *)dock);
4016 static void clipLeave(WDock *dock)
4018 XEvent event;
4019 WObjDescriptor *desc = NULL;
4020 WDock *tmp;
4022 if (dock == NULL)
4023 return;
4025 if (XCheckTypedEvent(dpy, EnterNotify, &event) != False) {
4026 if (XFindContext(dpy, event.xcrossing.window, w_global.context.client_win,
4027 (XPointer *) & desc) != XCNOENT
4028 && desc && desc->parent_type == WCLASS_DOCK_ICON
4029 && ((WAppIcon *) desc->parent)->dock == dock) {
4030 /* We haven't left the dock/clip/drawer yet */
4031 XPutBackEvent(dpy, &event);
4032 return;
4035 XPutBackEvent(dpy, &event);
4036 } else {
4037 /* We entered a withdrawn window, so we're still in Clip */
4038 return;
4041 tmp = (dock->type == WM_DRAWER ? dock->screen_ptr->dock : dock);
4042 if (tmp->auto_raise_magic) {
4043 WMDeleteTimerHandler(tmp->auto_raise_magic);
4044 tmp->auto_raise_magic = NULL;
4046 if (tmp->auto_raise_lower && !tmp->auto_lower_magic)
4047 tmp->auto_lower_magic = WMAddTimerHandler(wPreferences.clip_auto_lower_delay, clipAutoLower, (void *)tmp);
4049 if (dock->type != WM_CLIP && dock->type != WM_DRAWER)
4050 return;
4052 if (dock->auto_expand_magic) {
4053 WMDeleteTimerHandler(dock->auto_expand_magic);
4054 dock->auto_expand_magic = NULL;
4056 if (dock->auto_collapse && !dock->auto_collapse_magic)
4057 dock->auto_collapse_magic = WMAddTimerHandler(wPreferences.clip_auto_collapse_delay, clipAutoCollapse, (void *)dock);
4060 static void clipLeaveNotify(WObjDescriptor *desc, XEvent *event)
4062 WAppIcon *btn = (WAppIcon *) desc->parent;
4064 assert(event->type == LeaveNotify);
4066 if (desc->parent_type != WCLASS_DOCK_ICON)
4067 return;
4069 clipLeave(btn->dock);
4072 static void clipAutoCollapse(void *cdata)
4074 WDock *dock = (WDock *) cdata;
4076 if (dock->type != WM_CLIP && dock->type != WM_DRAWER)
4077 return;
4079 if (dock->auto_collapse) {
4080 dock->collapsed = 1;
4081 wDockHideIcons(dock);
4083 dock->auto_collapse_magic = NULL;
4086 static void clipAutoExpand(void *cdata)
4088 WDock *dock = (WDock *) cdata;
4090 if (dock->type != WM_CLIP && dock->type != WM_DRAWER)
4091 return;
4093 if (dock->auto_collapse) {
4094 dock->collapsed = 0;
4095 wDockShowIcons(dock);
4097 dock->auto_expand_magic = NULL;
4100 static void clipAutoLower(void *cdata)
4102 WDock *dock = (WDock *) cdata;
4104 if (dock->auto_raise_lower)
4105 wDockLower(dock);
4107 dock->auto_lower_magic = NULL;
4110 static void clipAutoRaise(void *cdata)
4112 WDock *dock = (WDock *) cdata;
4114 if (dock->auto_raise_lower)
4115 wDockRaise(dock);
4117 dock->auto_raise_magic = NULL;
4120 static Bool iconCanBeOmnipresent(WAppIcon *aicon)
4122 WScreen *scr = aicon->icon->core->screen_ptr;
4123 WDock *clip;
4124 WAppIcon *btn;
4125 int i, j;
4127 for (i = 0; i < w_global.workspace.count; i++) {
4128 clip = w_global.workspace.array[i]->clip;
4130 if (clip == aicon->dock)
4131 continue;
4133 if (clip->icon_count + scr->global_icon_count >= clip->max_icons)
4134 return False; /* Clip is full in some workspace */
4136 for (j = 0; j < clip->max_icons; j++) {
4137 btn = clip->icon_array[j];
4138 if (btn && btn->xindex == aicon->xindex && btn->yindex == aicon->yindex)
4139 return False;
4143 return True;
4146 int wClipMakeIconOmnipresent(WAppIcon *aicon, int omnipresent)
4148 WScreen *scr = aicon->icon->core->screen_ptr;
4149 WAppIconChain *new_entry, *tmp, *tmp1;
4150 int status = WO_SUCCESS;
4152 if ((scr->dock && aicon->dock == scr->dock) || aicon == w_global.clip.icon)
4153 return WO_NOT_APPLICABLE;
4155 if (aicon->omnipresent == omnipresent)
4156 return WO_SUCCESS;
4158 if (omnipresent) {
4159 if (iconCanBeOmnipresent(aicon)) {
4160 aicon->omnipresent = 1;
4161 new_entry = wmalloc(sizeof(WAppIconChain));
4162 new_entry->aicon = aicon;
4163 new_entry->next = scr->global_icons;
4164 scr->global_icons = new_entry;
4165 scr->global_icon_count++;
4166 } else {
4167 aicon->omnipresent = 0;
4168 status = WO_FAILED;
4170 } else {
4171 aicon->omnipresent = 0;
4172 if (aicon == scr->global_icons->aicon) {
4173 tmp = scr->global_icons->next;
4174 wfree(scr->global_icons);
4175 scr->global_icons = tmp;
4176 scr->global_icon_count--;
4177 } else {
4178 tmp = scr->global_icons;
4179 while (tmp->next) {
4180 if (tmp->next->aicon == aicon) {
4181 tmp1 = tmp->next->next;
4182 wfree(tmp->next);
4183 tmp->next = tmp1;
4184 scr->global_icon_count--;
4185 break;
4187 tmp = tmp->next;
4192 wAppIconPaint(aicon);
4194 return status;
4197 static void drawerAppendToChain(WScreen *scr, WDock *drawer)
4199 WDrawerChain **where_to_add;
4201 where_to_add = &scr->drawers;
4202 while ((*where_to_add) != NULL) {
4203 where_to_add = &(*where_to_add)->next;
4206 *where_to_add = wmalloc(sizeof(WDrawerChain));
4207 (*where_to_add)->adrawer = drawer;
4208 (*where_to_add)->next = NULL;
4209 scr->drawer_count++;
4213 static void drawerRemoveFromChain(WScreen *scr, WDock *drawer)
4215 WDrawerChain *next, **to_remove;
4217 to_remove = &scr->drawers;
4218 while (True) {
4219 if (*to_remove == NULL) {
4220 wwarning("The drawer to be removed can not be found.");
4221 return;
4223 if ((*to_remove)->adrawer == drawer)
4224 break;
4226 to_remove = &(*to_remove)->next;
4228 next = (*to_remove)->next;
4229 wfree(*to_remove);
4230 *to_remove = next;
4231 scr->drawer_count--;
4235 /* Don't free the returned string. Duplicate it. */
4236 static char * findUniqueName(WScreen *scr, const char *instance_basename)
4238 static char buffer[128];
4239 WDrawerChain *dc;
4240 int i;
4241 Bool already_in_use = True;
4243 #define UNIQUE_NAME_WATCHDOG 128
4244 for (i = 0; already_in_use && i < UNIQUE_NAME_WATCHDOG; i++) {
4245 snprintf(buffer, sizeof buffer, "%s%d", instance_basename, i);
4247 already_in_use = False;
4249 for (dc = scr->drawers; dc != NULL; dc = dc->next) {
4250 if (!strncmp(dc->adrawer->icon_array[0]->wm_instance, buffer,
4251 sizeof buffer)) {
4252 already_in_use = True;
4253 break;
4258 if (i == UNIQUE_NAME_WATCHDOG)
4259 wwarning("Couldn't find a unique name for drawer in %d attempts.", i);
4260 #undef UNIQUE_NAME_WATCHDOG
4262 return buffer;
4266 static void drawerIconExpose(WObjDescriptor *desc, XEvent *event)
4268 wDrawerIconPaint((WAppIcon *) desc->parent);
4272 static int addADrawer(WScreen *scr)
4274 int i, y, sig, found_y;
4275 WDock *drawer, *dock = scr->dock;
4276 WDrawerChain *dc;
4277 char can_be_here[2 * dock->max_icons - 1];
4279 if (dock->icon_count + scr->drawer_count >= dock->max_icons)
4280 return -1;
4282 for (y = -dock->max_icons + 1; y < dock->max_icons; y++) {
4283 can_be_here[y + dock->max_icons - 1] = True;
4285 for (i = 0; i < dock->max_icons; i++) {
4286 if (dock->icon_array[i] != NULL)
4287 can_be_here[dock->icon_array[i]->yindex + dock->max_icons - 1] = False;
4289 for (dc = scr->drawers; dc != NULL; dc = dc->next) {
4290 y = (int) ((dc->adrawer->y_pos - dock->y_pos) / ICON_SIZE);
4291 can_be_here[y + dock->max_icons - 1] = False;
4294 found_y = False;
4295 for (sig = 1; !found_y && sig > -2; sig -= 2) // 1, then -1
4297 for (y = sig; sig * y < dock->max_icons; y += sig)
4299 if (can_be_here[y + dock->max_icons - 1] &&
4300 onScreen(scr, dock->x_pos, dock->y_pos + y * ICON_SIZE))
4302 found_y = True;
4303 break;
4308 if (!found_y)
4309 /* This can happen even when dock->icon_count + scr->drawer_count
4310 * < dock->max_icons when the dock is not aligned on an
4311 * ICON_SIZE multiple, as some space is lost above and under it */
4312 return -1;
4314 drawer = wDockCreate(scr, WM_DRAWER, NULL);
4315 drawer->lowered = scr->dock->lowered;
4316 if (!drawer->lowered)
4317 ChangeStackingLevel(drawer->icon_array[0]->icon->core, WMDockLevel);
4318 else
4319 ChangeStackingLevel(drawer->icon_array[0]->icon->core, WMNormalLevel);
4320 drawer->auto_raise_lower = scr->dock->auto_raise_lower;
4321 drawer->x_pos = dock->x_pos;
4322 drawer->y_pos = dock->y_pos + ICON_SIZE * y;
4323 drawer->icon_array[0]->xindex = 0;
4324 drawer->icon_array[0]->yindex = 0;
4325 drawer->icon_array[0]->x_pos = drawer->x_pos;
4326 drawer->icon_array[0]->y_pos = drawer->y_pos;
4327 XMoveWindow(dpy, drawer->icon_array[0]->icon->core->window,
4328 drawer->icon_array[0]->x_pos, drawer->icon_array[0]->y_pos);
4330 return 0;
4334 static void addADrawerCallback(WMenu *menu, WMenuEntry *entry)
4336 assert(entry->clientdata!=NULL);
4337 addADrawer(((WAppIcon *) entry->clientdata)->dock->screen_ptr);
4341 static void drawerDestroy(WDock *drawer)
4343 WScreen *scr;
4344 int i;
4345 WAppIcon *aicon = NULL;
4346 WMArray *icons;
4348 if (drawer == NULL)
4349 return;
4351 scr = drawer->screen_ptr;
4353 /* Note regarding menus: we can't delete any dock/clip/drawer menu, because
4354 * that would (attempt to) wfree some memory in gettext library (see menu
4355 * entries that have several "versions", such like "Hide" and "Unhide"). */
4356 wDefaultPurgeInfo(scr, drawer->icon_array[0]->wm_instance,
4357 drawer->icon_array[0]->wm_class);
4359 if (drawer->icon_count == 2) {
4360 /* Drawer contains a single appicon: dock it where the drawer was */
4361 for (i = 1; i < drawer->max_icons; i++) {
4362 if ((aicon = drawer->icon_array[i]))
4363 break;
4366 wDockMoveIconBetweenDocks(drawer, scr->dock, aicon,
4367 0, (drawer->y_pos - scr->dock->y_pos) / ICON_SIZE);
4368 XMoveWindow(dpy, aicon->icon->core->window, drawer->x_pos, drawer->y_pos);
4369 XMapWindow(dpy, aicon->icon->core->window);
4370 } else if (drawer->icon_count > 2) {
4371 icons = WMCreateArray(drawer->icon_count - 1);
4372 for (i = 1; i < drawer->max_icons; i++) {
4373 if (!(aicon = drawer->icon_array[i]))
4374 continue;
4375 WMAddToArray(icons, aicon);
4377 removeIcons(icons, drawer);
4380 if (drawer->auto_collapse_magic) {
4381 WMDeleteTimerHandler(drawer->auto_collapse_magic);
4382 drawer->auto_collapse_magic = NULL;
4384 if (drawer->auto_lower_magic) {
4385 WMDeleteTimerHandler(drawer->auto_lower_magic);
4386 drawer->auto_lower_magic = NULL;
4389 wAppIconDestroy(drawer->icon_array[0]);
4390 wfree(drawer->icon_array);
4391 wfree(drawer);
4393 drawerRemoveFromChain(scr, drawer);
4394 if (scr->last_dock == drawer)
4395 scr->last_dock = NULL;
4396 if (scr->attracting_drawer == drawer)
4397 scr->attracting_drawer = NULL;
4401 static void removeDrawerCallback(WMenu *menu, WMenuEntry *entry)
4403 WDock *dock = ((WAppIcon*)entry->clientdata)->dock;
4404 assert(dock != NULL);
4406 if (dock->icon_count > 2) {
4407 if (wMessageDialog(dock->screen_ptr, _("Drawer"),
4408 _("All icons in this drawer will be detached!"),
4409 _("OK"), _("Cancel"), NULL) != WAPRDefault)
4410 return;
4412 drawerDestroy(dock);
4416 void wDrawerIconPaint(WAppIcon *dicon)
4418 Window win = dicon->icon->core->window;
4419 WScreen *scr = dicon->icon->core->screen_ptr;
4420 XPoint p[4];
4421 GC gc = scr->draw_gc;
4422 WMColor *color;
4424 wIconPaint(dicon->icon);
4426 if (!dicon->dock->collapsed)
4427 color = scr->clip_title_color[CLIP_NORMAL];
4428 else
4429 color = scr->clip_title_color[CLIP_COLLAPSED];
4430 XSetForeground(dpy, gc, WMColorPixel(color));
4432 if (dicon->dock->on_right_side) {
4433 p[0].x = p[3].x = 10;
4434 p[0].y = p[3].y = ICON_SIZE / 2 - 5;
4435 p[1].x = 10;
4436 p[1].y = ICON_SIZE / 2 + 5;
4437 p[2].x = 5;
4438 p[2].y = ICON_SIZE / 2;
4440 else {
4441 p[0].x = p[3].x = ICON_SIZE-1 - 10;
4442 p[0].y = p[3].y = ICON_SIZE / 2 - 5;
4443 p[1].x = ICON_SIZE-1 - 10;
4444 p[1].y = ICON_SIZE / 2 + 5;
4445 p[2].x = ICON_SIZE-1 - 5;
4446 p[2].y = ICON_SIZE / 2;
4448 XFillPolygon(dpy, win, gc, p,3,Convex,CoordModeOrigin);
4449 XDrawLines(dpy, win, gc, p,4,CoordModeOrigin);
4453 RImage* wDrawerMakeTile(WScreen *scr, RImage *normalTile)
4455 RImage *tile = RCloneImage(normalTile);
4456 RColor dark;
4457 RColor light;
4459 dark.alpha = 0;
4460 dark.red = dark.green = dark.blue = 60;
4462 light.alpha = 0;
4463 light.red = light.green = light.blue = 80;
4465 /* arrow bevel */
4466 if (!scr->dock || scr->dock->on_right_side) {
4467 ROperateLine(tile, RSubtractOperation, 11, ICON_SIZE / 2 - 7,
4468 4, ICON_SIZE / 2, &dark); /* / */
4469 ROperateLine(tile, RSubtractOperation, 11, ICON_SIZE / 2 + 7,
4470 4, ICON_SIZE / 2, &dark); /* \ */
4471 ROperateLine(tile, RAddOperation, 11, ICON_SIZE / 2 - 7,
4472 11, ICON_SIZE / 2 + 7, &light); /* | */
4474 else {
4475 ROperateLine(tile, RSubtractOperation, ICON_SIZE-1 - 11, ICON_SIZE / 2 - 7,
4476 ICON_SIZE-1 - 4, ICON_SIZE / 2, &dark); /* \ */
4477 ROperateLine(tile, RAddOperation, ICON_SIZE-1 - 11, ICON_SIZE / 2 + 7,
4478 ICON_SIZE-1 - 4, ICON_SIZE / 2, &light); /* / */
4479 ROperateLine(tile, RSubtractOperation, ICON_SIZE-1 - 11, ICON_SIZE / 2 - 7,
4480 ICON_SIZE-1 - 11, ICON_SIZE / 2 + 7, &dark); /* | */
4482 return tile;
4486 static void swapDrawer(WDock *drawer, int new_x)
4488 int i;
4490 drawer->on_right_side = !drawer->on_right_side;
4491 drawer->x_pos = new_x;
4493 for (i = 0; i < drawer->max_icons; i++) {
4494 WAppIcon *ai;
4495 if ((ai = drawer->icon_array[i]) == NULL)
4496 continue;
4497 ai->xindex *= -1; /* so A B C becomes C B A */
4498 ai->x_pos = new_x + ai->xindex * ICON_SIZE;
4500 /* Update drawer's tile */
4501 if (i == 0) {
4502 wIconUpdate(ai->icon);
4503 wDrawerIconPaint(ai);
4505 XMoveWindow(dpy, ai->icon->core->window, ai->x_pos, ai->y_pos);
4510 static void swapDrawers(WScreen *scr, int on_right_side, int new_x)
4512 WDrawerChain *dc;
4514 if (scr->drawer_tile) {
4515 RReleaseImage(scr->drawer_tile);
4517 scr->drawer_tile = wDrawerMakeTile(scr, scr->icon_tile);
4519 for (dc = scr->drawers; dc != NULL; dc = dc->next) {
4520 swapDrawer(dc->adrawer, new_x);
4521 assert(dc->adrawer->on_right_side == on_right_side);
4526 int wIsADrawer(WAppIcon *aicon)
4528 return aicon && aicon->dock &&
4529 aicon->dock->type == WM_DRAWER && aicon->dock->icon_array[0] == aicon;
4533 static WDock* getDrawer(WScreen *scr, int y_index)
4535 WDrawerChain *dc;
4537 for (dc = scr->drawers; dc != NULL; dc = dc->next) {
4538 if (dc->adrawer->y_pos - scr->dock->y_pos == y_index * ICON_SIZE)
4539 return dc->adrawer;
4541 return NULL;
4546 /* Find the "hole" a moving appicon created when snapped into the
4547 * drawer. redocking is a boolean. If the moving appicon comes from the
4548 * drawer, drawer->icon_count is correct. If not, redocking is then false and
4549 * there are now drawer->icon_count plus one appicons in the drawer. */
4550 static int indexOfHole(WDock *drawer, WAppIcon *moving_aicon, int redocking)
4552 int index_of_hole, i;
4554 /* Classic interview question...
4556 * We have n-1 (n = drawer->icon_count-1 or drawer->icon_count, see
4557 * redocking) appicons, whose xindex are unique in [1..n]. One is missing:
4558 * that's where the ghost of the moving appicon is, that's what the
4559 * function should return.
4561 * We compute 1+2+...+n (this sum is equal to n*(n+1)/2), we substract to
4562 * this sum the xindex of each of the n-1 appicons, and we get the correct
4563 * index! */
4565 if (redocking) {
4566 index_of_hole = (drawer->icon_count - 1) * drawer->icon_count / 2;
4567 } else {
4568 index_of_hole = drawer->icon_count * (drawer->icon_count + 1) / 2;
4570 index_of_hole *= (drawer->on_right_side ? -1 : 1);
4572 for (i = 1; i < drawer->max_icons; i++) {
4573 if (drawer->icon_array[i] && drawer->icon_array[i] != moving_aicon)
4574 index_of_hole -= drawer->icon_array[i]->xindex;
4576 /* wmessage(" Index of the moving appicon is %d (%sredocking)", index_of_hole, (redocking ? "" : "not ")); */
4577 if (abs(index_of_hole) > abs(drawer->icon_count) - (redocking ? 1 : 0))
4578 wwarning(" index_of_hole is too large ! (%d greater than %d)",
4579 index_of_hole, abs(drawer->icon_count) - (redocking ? 1 : 0));
4580 if (index_of_hole == 0)
4581 wwarning(" index_of_hole == 0 (%sredocking, icon_count == %d)", (redocking ? "" : "not "), drawer->icon_count);
4583 return index_of_hole;
4587 void wSlideAppicons(WAppIcon **appicons, int n, int to_the_left)
4589 int i;
4590 int leftmost = -1, min_index = 9999, from_x = -1; // leftmost and from_x initialized to avoid warning
4591 Window *wins[n];
4592 WAppIcon *aicon;
4594 if (n < 1)
4595 return;
4597 for (i = 0; i < n; i++) {
4598 aicon = appicons[i];
4599 aicon->xindex += (to_the_left ? -1 : +1);
4600 if (aicon->xindex < min_index) {
4601 min_index = aicon->xindex;
4602 leftmost = i;
4603 from_x = aicon->x_pos;
4605 aicon->x_pos += (to_the_left ? -ICON_SIZE : +ICON_SIZE);
4608 for (i = 0; i < n; i++) {
4609 aicon = appicons[i];
4610 wins[aicon->xindex - min_index] = &aicon->icon->core->window;
4612 aicon = appicons[leftmost];
4613 SlideWindows(wins, n, from_x, aicon->y_pos, aicon->x_pos, aicon->y_pos);
4617 void wDrawerFillTheGap(WDock *drawer, WAppIcon *aicon, Bool redocking)
4619 int i, j;
4620 int index_of_hole = indexOfHole(drawer, aicon, redocking);
4621 WAppIcon *aicons_to_shift[drawer->icon_count];
4623 j = 0;
4624 for (i = 0; i < drawer->max_icons; i++) {
4625 WAppIcon *ai = drawer->icon_array[i];
4626 if (ai && ai != aicon &&
4627 abs(ai->xindex) > abs(index_of_hole))
4628 aicons_to_shift[j++] = ai;
4630 if (j != drawer->icon_count - abs(index_of_hole) - (redocking ? 1 : 0))
4631 wwarning("Removing aicon at index %d from %s: j=%d but should be %d",
4632 index_of_hole, drawer->icon_array[0]->wm_instance,
4633 j, drawer->icon_count - abs(index_of_hole) - (redocking ? 1 : 0));
4634 wSlideAppicons(aicons_to_shift, j, !drawer->on_right_side);
4638 static void drawerConsolidateIcons(WDock *drawer)
4640 WAppIcon *aicons_to_shift[drawer->icon_count];
4641 int maxRemaining = 0;
4642 int sum = 0;
4643 int i;
4644 for (i = 0; i < drawer->max_icons; i++) {
4645 WAppIcon *ai = drawer->icon_array[i];
4646 if (ai == NULL)
4647 continue;
4648 sum += abs(ai->xindex);
4649 if (abs(ai->xindex) > maxRemaining)
4650 maxRemaining = abs(ai->xindex);
4652 while (sum != maxRemaining * (maxRemaining + 1) / 2) { // while there is a hole
4653 WAppIcon *ai;
4654 int n;
4655 // Look up for the hole at max position
4656 int maxDeleted;
4657 for (maxDeleted = maxRemaining - 1; maxDeleted > 0; maxDeleted--) {
4658 Bool foundAppIconThere = False;
4659 for (i = 0; i < drawer->max_icons; i++) {
4660 WAppIcon *ai = drawer->icon_array[i];
4661 if (ai == NULL)
4662 continue;
4663 if (abs(ai->xindex) == maxDeleted) {
4664 foundAppIconThere = True;
4665 break;
4668 if (!foundAppIconThere)
4669 break;
4671 assert(maxDeleted > 0); // would mean while test is wrong
4672 n = 0;
4673 for (i = 0; i < drawer->max_icons; i++) {
4674 ai = drawer->icon_array[i];
4675 if (ai != NULL && abs(ai->xindex) > maxDeleted)
4676 aicons_to_shift[n++] = ai;
4678 assert(n == maxRemaining - maxDeleted); // for the code review ;-)
4679 wSlideAppicons(aicons_to_shift, n, !drawer->on_right_side);
4680 // Efficient beancounting
4681 maxRemaining -= 1;
4682 sum -= n;
4688 /* similar to wDockRestoreState, but a lot a specific stuff too... */
4689 static WDock * drawerRestoreState(WScreen *scr, WMPropList *drawer_state)
4691 WDock *drawer;
4692 WMPropList *apps, *value, *dock_state;
4693 WAppIcon *aicon;
4694 int count, i;
4696 if (!drawer_state)
4697 return NULL;
4699 make_keys();
4701 WMRetainPropList(drawer_state);
4703 /* Get the instance name, and create a drawer */
4704 value = WMGetFromPLDictionary(drawer_state, dName);
4705 drawer = wDockCreate(scr, WM_DRAWER, WMGetFromPLString(value));
4707 /* restore DnD command and paste command */
4708 #ifdef XDND
4709 value = WMGetFromPLDictionary(drawer_state, dDropCommand);
4710 if (value && WMIsPLString(value))
4711 drawer->icon_array[0]->dnd_command = wstrdup(WMGetFromPLString(value));
4712 #endif /* XDND */
4714 value = WMGetFromPLDictionary(drawer_state, dPasteCommand);
4715 if (value && WMIsPLString(value))
4716 drawer->icon_array[0]->paste_command = wstrdup(WMGetFromPLString(value));
4718 /* restore position */
4719 value = WMGetFromPLDictionary(drawer_state, dPosition);
4720 if (!value || !WMIsPLString(value))
4721 COMPLAIN("Position");
4722 else {
4723 int x, y, y_index;
4724 if (sscanf(WMGetFromPLString(value), "%i,%i", &x, &y) != 2)
4725 COMPLAIN("Position");
4727 /* check position sanity */
4728 if (x != scr->dock->x_pos) {
4729 x = scr->dock->x_pos;
4731 y_index = (y - scr->dock->y_pos) / ICON_SIZE;
4732 if (y_index >= scr->dock->max_icons) {
4733 /* Here we should do something more intelligent, since it
4734 * can happen even if the user hasn't hand-edited his
4735 * G/D/State file (but uses a lower resolution). */
4736 y_index = scr->dock->max_icons - 1;
4738 y = scr->dock->y_pos + y_index * ICON_SIZE;
4739 moveDock(drawer, x, y);
4742 /* restore dock properties (applist and others) */
4743 dock_state = WMGetFromPLDictionary(drawer_state, dDock);
4745 /* restore lowered/raised state: same as scr->dock, no matter what */
4746 drawer->lowered = scr->dock->lowered;
4747 if (!drawer->lowered)
4748 ChangeStackingLevel(drawer->icon_array[0]->icon->core, WMDockLevel);
4749 else
4750 ChangeStackingLevel(drawer->icon_array[0]->icon->core, WMNormalLevel);
4751 wRaiseFrame(drawer->icon_array[0]->icon->core);
4753 /* restore collapsed state */
4754 drawer->collapsed = 0;
4755 value = WMGetFromPLDictionary(dock_state, dCollapsed);
4756 if (value && strcasecmp(WMGetFromPLString(value), "YES") == 0) {
4757 drawer->collapsed = 1;
4759 /* restore auto-collapsed state */
4760 value = WMGetFromPLDictionary(dock_state, dAutoCollapse);
4761 if (value && strcasecmp(WMGetFromPLString(value), "YES") == 0) {
4762 drawer->auto_collapse = 1;
4763 drawer->collapsed = 1;
4764 } else {
4765 drawer->auto_collapse = 0; // because wDockCreate sets it (drawers only)
4768 /* restore auto-raise/lower state: same as scr->dock, no matter what */
4769 drawer->auto_raise_lower = scr->dock->auto_raise_lower;
4771 /* restore attract icons state */
4772 drawer->attract_icons = 0;
4773 value = WMGetFromPLDictionary(dock_state, dAutoAttractIcons);
4774 if (value && strcasecmp(WMGetFromPLString(value), "YES") == 0) {
4775 drawer->attract_icons = 1;
4776 scr->attracting_drawer = drawer;
4779 /* application list */
4780 apps = WMGetFromPLDictionary(dock_state, dApplications);
4782 if (!apps) {
4783 goto finish;
4786 count = WMGetPropListItemCount(apps);
4788 if (count == 0)
4789 goto finish;
4791 for (i=0; i<count; i++) {
4792 if (drawer->icon_count >= drawer->max_icons) {
4793 wwarning(_("there are too many icons stored in drawer. Ignoring what doesn't fit"));
4794 break;
4797 value = WMGetFromPLArray(apps, i);
4798 aicon = restore_icon_state(scr, value, WM_DRAWER, drawer->icon_count);
4800 drawer->icon_array[drawer->icon_count] = aicon;
4802 if (aicon) {
4803 aicon->dock = drawer;
4804 aicon->x_pos = drawer->x_pos + (aicon->xindex * ICON_SIZE);
4805 aicon->y_pos = drawer->y_pos + (aicon->yindex * ICON_SIZE);
4807 if (!drawer->lowered)
4808 ChangeStackingLevel(aicon->icon->core, WMDockLevel);
4809 else
4810 ChangeStackingLevel(aicon->icon->core, WMNormalLevel);
4812 wCoreConfigure(aicon->icon->core, aicon->x_pos, aicon->y_pos, 0, 0);
4814 if (!drawer->collapsed)
4815 XMapWindow(dpy, aicon->icon->core->window);
4816 wRaiseFrame(aicon->icon->core);
4818 drawer->icon_count++;
4822 finish:
4823 WMReleasePropList(drawer_state);
4825 return drawer;
4829 /* Same kind of comment than for previous function: this function is
4830 * very similar to make_icon_state, but has substential differences as
4831 * well. */
4832 static WMPropList *drawerSaveState(WScreen *scr, WDock *drawer)
4834 WMPropList *pstr, *drawer_state;
4835 WAppIcon *ai;
4836 char buffer[64];
4838 ai = drawer->icon_array[0];
4839 /* Store its name */
4840 pstr = WMCreatePLString(ai->wm_instance);
4841 drawer_state = WMCreatePLDictionary(dName, pstr, NULL); /* we need this final NULL */
4842 WMReleasePropList(pstr);
4844 /* Store its position */
4845 snprintf(buffer, sizeof(buffer), "%i,%i", ai->x_pos, ai->y_pos);
4846 pstr = WMCreatePLString(buffer);
4847 WMPutInPLDictionary(drawer_state, dPosition, pstr);
4848 WMReleasePropList(pstr);
4850 #ifdef XDND
4851 /* Store its DnD command */
4852 if (ai->dnd_command) {
4853 pstr = WMCreatePLString(ai->dnd_command);
4854 WMPutInPLDictionary(drawer_state, dDropCommand, pstr);
4855 WMReleasePropList(pstr);
4857 #endif /* XDND */
4859 /* Store its paste command */
4860 if (ai->paste_command) {
4861 pstr = WMCreatePLString(ai->paste_command);
4862 WMPutInPLDictionary(drawer_state, dPasteCommand, pstr);
4863 WMReleasePropList(pstr);
4866 /* Store applications list and other properties */
4867 pstr = dockSaveState(drawer);
4868 WMPutInPLDictionary(drawer_state, dDock, pstr);
4869 WMReleasePropList(pstr);
4871 return drawer_state;
4875 void wDrawersSaveState(WScreen *scr)
4877 WMPropList *all_drawers, *drawer_state;
4878 int i;
4879 WDrawerChain *dc;
4881 make_keys();
4883 all_drawers = WMCreatePLArray(NULL);
4884 for (i=0, dc = scr->drawers;
4885 i < scr->drawer_count;
4886 i++, dc = dc->next) {
4887 drawer_state = drawerSaveState(scr, dc->adrawer);
4888 WMAddToPLArray(all_drawers, drawer_state);
4889 WMReleasePropList(drawer_state);
4891 WMPutInPLDictionary(w_global.session_state, dDrawers, all_drawers);
4892 WMReleasePropList(all_drawers);
4896 void wDrawersRestoreState(WScreen *scr)
4898 WMPropList *all_drawers, *drawer_state;
4899 int i;
4901 make_keys();
4903 if (w_global.session_state == NULL)
4904 return;
4906 all_drawers = WMGetFromPLDictionary(w_global.session_state, dDrawers);
4907 if (!all_drawers)
4908 return;
4910 for (i = 0; i < WMGetPropListItemCount(all_drawers); i++) {
4911 drawer_state = WMGetFromPLArray(all_drawers, i);
4912 drawerRestoreState(scr, drawer_state);
4913 // Note: scr->drawers was updated when the the drawer was created