shortcutWindows moved to w_global
[wmaker-crm.git] / src / dock.c
blob5ca6c1068d509bcf6e6e26f68b3575155f1f4f1d
1 /* dock.c- built-in Dock module for WindowMaker
3 * Window Maker window manager
5 * Copyright (c) 1997-2003 Alfredo K. Kojima
6 * Copyright (c) 1998-2003 Dan Pascu
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
23 #include "wconfig.h"
25 #include <X11/Xlib.h>
26 #include <X11/Xutil.h>
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <libgen.h>
30 #include <string.h>
31 #include <strings.h>
32 #include <unistd.h>
33 #include <math.h>
34 #include <limits.h>
36 #ifndef PATH_MAX
37 #define PATH_MAX DEFAULT_PATH_MAX
38 #endif
40 #include "WindowMaker.h"
41 #include "wcore.h"
42 #include "window.h"
43 #include "icon.h"
44 #include "appicon.h"
45 #include "actions.h"
46 #include "stacking.h"
47 #include "dock.h"
48 #include "dockedapp.h"
49 #include "dialog.h"
50 #include "main.h"
51 #include "properties.h"
52 #include "menu.h"
53 #include "client.h"
54 #include "defaults.h"
55 #include "workspace.h"
56 #include "framewin.h"
57 #include "superfluous.h"
58 #include "xinerama.h"
59 #include "placement.h"
60 #include "misc.h"
61 #include "event.h"
63 /**** Local variables ****/
64 #define CLIP_REWIND 1
65 #define CLIP_IDLE 0
66 #define CLIP_FORWARD 2
68 #define MOD_MASK wPreferences.modifier_mask
69 #define ICON_SIZE wPreferences.icon_size
71 /***** Local variables ****/
73 static WMPropList *dCommand = NULL;
74 static WMPropList *dPasteCommand = NULL;
75 #ifdef XDND /* XXX was OFFIX */
76 static WMPropList *dDropCommand = NULL;
77 #endif
78 static WMPropList *dAutoLaunch, *dLock;
79 static WMPropList *dName, *dForced, *dBuggyApplication, *dYes, *dNo;
80 static WMPropList *dHost, *dDock, *dClip;
81 static WMPropList *dAutoAttractIcons;
83 static WMPropList *dPosition, *dApplications, *dLowered, *dCollapsed;
85 static WMPropList *dAutoCollapse, *dAutoRaiseLower, *dOmnipresent;
87 static WMPropList *dDrawers = NULL;
89 static void dockIconPaint(WAppIcon *btn);
91 static void iconMouseDown(WObjDescriptor *desc, XEvent *event);
93 static pid_t execCommand(WAppIcon *btn, const char *command, WSavedState *state);
95 static void trackDeadProcess(pid_t pid, unsigned char status, WDock *dock);
97 static int getClipButton(int px, int py);
99 static void toggleLowered(WDock *dock);
101 static void toggleCollapsed(WDock *dock);
103 static void clipIconExpose(WObjDescriptor *desc, XEvent *event);
105 static void clipLeave(WDock *dock);
107 static void handleClipChangeWorkspace(WScreen *scr, XEvent *event);
109 static void clipEnterNotify(WObjDescriptor *desc, XEvent *event);
110 static void clipLeaveNotify(WObjDescriptor *desc, XEvent *event);
111 static void clipAutoCollapse(void *cdata);
112 static void clipAutoExpand(void *cdata);
113 static void launchDockedApplication(WAppIcon *btn, Bool withSelection);
115 static void clipAutoLower(void *cdata);
116 static void clipAutoRaise(void *cdata);
117 static WAppIcon *mainIconCreate(WScreen *scr, int type, const char *name);
119 static void drawerIconExpose(WObjDescriptor *desc, XEvent *event);
120 static void removeDrawerCallback(WMenu *menu, WMenuEntry *entry);
121 static void drawerAppendToChain(WScreen *scr, WDock *drawer);
122 static char *findUniqueName(WScreen *scr, const char *instance_basename);
123 static void addADrawerCallback(WMenu *menu, WMenuEntry *entry);
124 static void swapDrawers(WScreen *scr, int on_right_side, int new_x);
125 static WDock* getDrawer(WScreen *scr, int y_index);
126 static int indexOfHole(WDock *drawer, WAppIcon *moving_aicon, int redocking);
127 static void drawerConsolidateIcons(WDock *drawer);
129 static int onScreen(WScreen *scr, int x, int y);
131 static void make_keys(void)
133 if (dCommand != NULL)
134 return;
136 dCommand = WMRetainPropList(WMCreatePLString("Command"));
137 dPasteCommand = WMRetainPropList(WMCreatePLString("PasteCommand"));
138 #ifdef XDND
139 dDropCommand = WMRetainPropList(WMCreatePLString("DropCommand"));
140 #endif
141 dLock = WMRetainPropList(WMCreatePLString("Lock"));
142 dAutoLaunch = WMRetainPropList(WMCreatePLString("AutoLaunch"));
143 dName = WMRetainPropList(WMCreatePLString("Name"));
144 dForced = WMRetainPropList(WMCreatePLString("Forced"));
145 dBuggyApplication = WMRetainPropList(WMCreatePLString("BuggyApplication"));
146 dYes = WMRetainPropList(WMCreatePLString("Yes"));
147 dNo = WMRetainPropList(WMCreatePLString("No"));
148 dHost = WMRetainPropList(WMCreatePLString("Host"));
150 dPosition = WMCreatePLString("Position");
151 dApplications = WMCreatePLString("Applications");
152 dLowered = WMCreatePLString("Lowered");
153 dCollapsed = WMCreatePLString("Collapsed");
154 dAutoCollapse = WMCreatePLString("AutoCollapse");
155 dAutoRaiseLower = WMCreatePLString("AutoRaiseLower");
156 dAutoAttractIcons = WMCreatePLString("AutoAttractIcons");
158 dOmnipresent = WMCreatePLString("Omnipresent");
160 dDock = WMCreatePLString("Dock");
161 dClip = WMCreatePLString("Clip");
162 dDrawers = WMCreatePLString("Drawers");
165 static void renameCallback(WMenu *menu, WMenuEntry *entry)
167 WDock *dock = entry->clientdata;
168 char buffer[128];
169 int wspace;
170 char *name;
172 assert(entry->clientdata != NULL);
174 wspace = w_global.workspace.current;
176 name = wstrdup(w_global.workspace.array[wspace]->name);
178 snprintf(buffer, sizeof(buffer), _("Type the name for workspace %i:"), wspace + 1);
179 if (wInputDialog(dock->screen_ptr, _("Rename Workspace"), buffer, &name))
180 wWorkspaceRename(dock->screen_ptr, wspace, name);
182 wfree(name);
185 static void toggleLoweredCallback(WMenu *menu, WMenuEntry *entry)
187 assert(entry->clientdata != NULL);
189 toggleLowered(entry->clientdata);
191 entry->flags.indicator_on = !(((WDock *) entry->clientdata)->lowered);
193 wMenuPaint(menu);
196 static int matchWindow(const void *item, const void *cdata)
198 return (((WFakeGroupLeader *) item)->leader == (Window) cdata);
201 static void killCallback(WMenu *menu, WMenuEntry *entry)
203 WScreen *scr = menu->menu->screen_ptr;
204 WAppIcon *icon;
205 WFakeGroupLeader *fPtr;
206 char *buffer, *shortname, **argv;
207 int argc;
209 if (!WCHECK_STATE(WSTATE_NORMAL))
210 return;
212 assert(entry->clientdata != NULL);
214 icon = (WAppIcon *) entry->clientdata;
216 icon->editing = 1;
218 WCHANGE_STATE(WSTATE_MODAL);
220 /* strip away dir names */
221 shortname = basename(icon->command);
222 /* separate out command options */
223 wtokensplit(shortname, &argv, &argc);
225 buffer = wstrconcat(argv[0],
226 _(" will be forcibly closed.\n"
227 "Any unsaved changes will be lost.\n" "Please confirm."));
229 if (icon->icon && icon->icon->owner) {
230 fPtr = icon->icon->owner->fake_group;
231 } else {
232 /* is this really necessary? can we kill a non-running dock icon? */
233 Window win = icon->main_window;
234 int index;
236 index = WMFindInArray(scr->fakeGroupLeaders, matchWindow, (void *)win);
237 if (index != WANotFound)
238 fPtr = WMGetFromArray(scr->fakeGroupLeaders, index);
239 else
240 fPtr = NULL;
243 if (wPreferences.dont_confirm_kill
244 || wMessageDialog(menu->frame->screen_ptr, _("Kill Application"),
245 buffer, _("Yes"), _("No"), NULL) == WAPRDefault) {
246 if (fPtr != NULL) {
247 WWindow *wwin, *twin;
249 wwin = scr->focused_window;
250 while (wwin) {
251 twin = wwin->prev;
252 if (wwin->fake_group == fPtr)
253 wClientKill(wwin);
255 wwin = twin;
257 } else if (icon->icon && icon->icon->owner) {
258 wClientKill(icon->icon->owner);
262 wfree(buffer);
263 wtokenfree(argv, argc);
265 icon->editing = 0;
267 WCHANGE_STATE(WSTATE_NORMAL);
270 /* TODO: replace this function with a member of the dock struct */
271 static int numberOfSelectedIcons(WDock *dock)
273 WAppIcon *aicon;
274 int i, n;
276 n = 0;
277 for (i = 1; i < dock->max_icons; i++) {
278 aicon = dock->icon_array[i];
279 if (aicon && aicon->icon->selected)
280 n++;
283 return n;
286 static WMArray *getSelected(WDock *dock)
288 WMArray *ret = WMCreateArray(8);
289 WAppIcon *btn;
290 int i;
292 for (i = 1; i < dock->max_icons; i++) {
293 btn = dock->icon_array[i];
294 if (btn && btn->icon->selected)
295 WMAddToArray(ret, btn);
298 return ret;
301 static void paintClipButtons(WAppIcon *clipIcon, Bool lpushed, Bool rpushed)
303 Window win = clipIcon->icon->core->window;
304 WScreen *scr = clipIcon->icon->core->screen_ptr;
305 XPoint p[4];
306 int pt = CLIP_BUTTON_SIZE * ICON_SIZE / 64;
307 int tp = ICON_SIZE - pt;
308 int as = pt - 15; /* 15 = 5+5+5 */
309 GC gc = scr->draw_gc; /* maybe use WMColorGC() instead here? */
310 WMColor *color;
312 color = scr->clip_title_color[CLIP_NORMAL];
314 XSetForeground(dpy, gc, WMColorPixel(color));
316 if (rpushed) {
317 p[0].x = tp + 1;
318 p[0].y = 1;
319 p[1].x = ICON_SIZE - 2;
320 p[1].y = 1;
321 p[2].x = ICON_SIZE - 2;
322 p[2].y = pt - 1;
323 } else if (lpushed) {
324 p[0].x = 1;
325 p[0].y = tp;
326 p[1].x = pt;
327 p[1].y = ICON_SIZE - 2;
328 p[2].x = 1;
329 p[2].y = ICON_SIZE - 2;
331 if (lpushed || rpushed) {
332 XSetForeground(dpy, scr->draw_gc, scr->white_pixel);
333 XFillPolygon(dpy, win, scr->draw_gc, p, 3, Convex, CoordModeOrigin);
334 XSetForeground(dpy, scr->draw_gc, scr->black_pixel);
337 /* top right arrow */
338 p[0].x = p[3].x = ICON_SIZE - 5 - as;
339 p[0].y = p[3].y = 5;
340 p[1].x = ICON_SIZE - 6;
341 p[1].y = 5;
342 p[2].x = ICON_SIZE - 6;
343 p[2].y = 4 + as;
344 if (rpushed) {
345 XFillPolygon(dpy, win, scr->draw_gc, p, 3, Convex, CoordModeOrigin);
346 XDrawLines(dpy, win, scr->draw_gc, p, 4, CoordModeOrigin);
347 } else {
348 XFillPolygon(dpy, win, gc, p, 3, Convex, CoordModeOrigin);
349 XDrawLines(dpy, win, gc, p, 4, CoordModeOrigin);
352 /* bottom left arrow */
353 p[0].x = p[3].x = 5;
354 p[0].y = p[3].y = ICON_SIZE - 5 - as;
355 p[1].x = 5;
356 p[1].y = ICON_SIZE - 6;
357 p[2].x = 4 + as;
358 p[2].y = ICON_SIZE - 6;
359 if (lpushed) {
360 XFillPolygon(dpy, win, scr->draw_gc, p, 3, Convex, CoordModeOrigin);
361 XDrawLines(dpy, win, scr->draw_gc, p, 4, CoordModeOrigin);
362 } else {
363 XFillPolygon(dpy, win, gc, p, 3, Convex, CoordModeOrigin);
364 XDrawLines(dpy, win, gc, p, 4, CoordModeOrigin);
368 RImage *wClipMakeTile(WScreen *scr, RImage *normalTile)
370 RImage *tile = RCloneImage(normalTile);
371 RColor black;
372 RColor dark;
373 RColor light;
374 int pt, tp;
375 int as;
377 pt = CLIP_BUTTON_SIZE * wPreferences.icon_size / 64;
378 tp = wPreferences.icon_size - 1 - pt;
379 as = pt - 15;
381 black.alpha = 255;
382 black.red = black.green = black.blue = 0;
384 dark.alpha = 0;
385 dark.red = dark.green = dark.blue = 60;
387 light.alpha = 0;
388 light.red = light.green = light.blue = 80;
390 /* top right */
391 ROperateLine(tile, RSubtractOperation, tp, 0, wPreferences.icon_size - 2, pt - 1, &dark);
392 RDrawLine(tile, tp - 1, 0, wPreferences.icon_size - 1, pt + 1, &black);
393 ROperateLine(tile, RAddOperation, tp, 2, wPreferences.icon_size - 3, pt, &light);
395 /* arrow bevel */
396 ROperateLine(tile, RSubtractOperation, ICON_SIZE - 7 - as, 4, ICON_SIZE - 5, 4, &dark);
397 ROperateLine(tile, RSubtractOperation, ICON_SIZE - 6 - as, 5, ICON_SIZE - 5, 6 + as, &dark);
398 ROperateLine(tile, RAddOperation, ICON_SIZE - 5, 4, ICON_SIZE - 5, 6 + as, &light);
400 /* bottom left */
401 ROperateLine(tile, RAddOperation, 2, tp + 2, pt - 2, wPreferences.icon_size - 3, &dark);
402 RDrawLine(tile, 0, tp - 1, pt + 1, wPreferences.icon_size - 1, &black);
403 ROperateLine(tile, RSubtractOperation, 0, tp - 2, pt + 1, wPreferences.icon_size - 2, &light);
405 /* arrow bevel */
406 ROperateLine(tile, RSubtractOperation, 4, ICON_SIZE - 7 - as, 4, ICON_SIZE - 5, &dark);
407 ROperateLine(tile, RSubtractOperation, 5, ICON_SIZE - 6 - as, 6 + as, ICON_SIZE - 5, &dark);
408 ROperateLine(tile, RAddOperation, 4, ICON_SIZE - 5, 6 + as, ICON_SIZE - 5, &light);
410 return tile;
413 static void omnipresentCallback(WMenu *menu, WMenuEntry *entry)
415 WAppIcon *clickedIcon = entry->clientdata;
416 WAppIcon *aicon;
417 WDock *dock;
418 WMArray *selectedIcons;
419 WMArrayIterator iter;
420 int failed;
422 assert(entry->clientdata != NULL);
424 dock = clickedIcon->dock;
426 selectedIcons = getSelected(dock);
428 if (!WMGetArrayItemCount(selectedIcons))
429 WMAddToArray(selectedIcons, clickedIcon);
431 failed = 0;
432 WM_ITERATE_ARRAY(selectedIcons, aicon, iter) {
433 if (wClipMakeIconOmnipresent(aicon, !aicon->omnipresent) == WO_FAILED)
434 failed++;
435 else if (aicon->icon->selected)
436 wIconSelect(aicon->icon);
438 WMFreeArray(selectedIcons);
440 if (failed > 1) {
441 wMessageDialog(dock->screen_ptr, _("Warning"),
442 _("Some icons cannot be made omnipresent. "
443 "Please make sure that no other icon is "
444 "docked in the same positions on the other "
445 "workspaces and the Clip is not full in "
446 "some workspace."), _("OK"), NULL, NULL);
447 } else if (failed == 1) {
448 wMessageDialog(dock->screen_ptr, _("Warning"),
449 _("Icon cannot be made omnipresent. "
450 "Please make sure that no other icon is "
451 "docked in the same position on the other "
452 "workspaces and the Clip is not full in "
453 "some workspace."), _("OK"), NULL, NULL);
457 static void removeIcons(WMArray *icons, WDock *dock)
459 WAppIcon *aicon;
460 int keepit;
461 WMArrayIterator it;
463 WM_ITERATE_ARRAY(icons, aicon, it) {
464 keepit = aicon->running && wApplicationOf(aicon->main_window);
465 wDockDetach(dock, aicon);
466 if (keepit) {
467 /* XXX: can: aicon->icon == NULL ? */
468 PlaceIcon(dock->screen_ptr, &aicon->x_pos, &aicon->y_pos,
469 wGetHeadForWindow(aicon->icon->owner));
470 XMoveWindow(dpy, aicon->icon->core->window, aicon->x_pos, aicon->y_pos);
471 if (!dock->mapped || dock->collapsed)
472 XMapWindow(dpy, aicon->icon->core->window);
475 WMFreeArray(icons);
477 if (wPreferences.auto_arrange_icons)
478 wArrangeIcons(dock->screen_ptr, True);
481 static void removeIconsCallback(WMenu *menu, WMenuEntry *entry)
483 WAppIcon *clickedIcon = (WAppIcon *) entry->clientdata;
484 WDock *dock;
485 WMArray *selectedIcons;
487 assert(clickedIcon != NULL);
489 dock = clickedIcon->dock;
491 selectedIcons = getSelected(dock);
493 if (WMGetArrayItemCount(selectedIcons)) {
494 if (wMessageDialog(dock->screen_ptr,
495 dock->type == WM_CLIP ? _("Workspace Clip") : _("Drawer"),
496 _("All selected icons will be removed!"),
497 _("OK"), _("Cancel"), NULL) != WAPRDefault) {
498 WMFreeArray(selectedIcons);
499 return;
501 } else {
502 if (clickedIcon->xindex == 0 && clickedIcon->yindex == 0) {
503 WMFreeArray(selectedIcons);
504 return;
506 WMAddToArray(selectedIcons, clickedIcon);
509 removeIcons(selectedIcons, dock);
511 if (dock->type == WM_DRAWER) {
512 drawerConsolidateIcons(dock);
516 static void keepIconsCallback(WMenu *menu, WMenuEntry *entry)
518 WAppIcon *clickedIcon = (WAppIcon *) entry->clientdata;
519 WDock *dock;
520 WAppIcon *aicon;
521 WMArray *selectedIcons;
522 WMArrayIterator it;
524 assert(clickedIcon != NULL);
525 dock = clickedIcon->dock;
527 selectedIcons = getSelected(dock);
529 if (!WMGetArrayItemCount(selectedIcons)
530 && clickedIcon != w_global.clip.icon) {
531 char *command = NULL;
533 if (!clickedIcon->command && !clickedIcon->editing) {
534 clickedIcon->editing = 1;
535 if (wInputDialog(dock->screen_ptr, _("Keep Icon"),
536 _("Type the command used to launch the application"), &command)) {
537 if (command && (command[0] == 0 || (command[0] == '-' && command[1] == 0))) {
538 wfree(command);
539 command = NULL;
541 clickedIcon->command = command;
542 clickedIcon->editing = 0;
543 } else {
544 clickedIcon->editing = 0;
545 if (command)
546 wfree(command);
547 WMFreeArray(selectedIcons);
548 return;
552 WMAddToArray(selectedIcons, clickedIcon);
555 WM_ITERATE_ARRAY(selectedIcons, aicon, it) {
556 if (aicon->icon->selected)
557 wIconSelect(aicon->icon);
559 if (aicon && aicon->attracted && aicon->command) {
560 aicon->attracted = 0;
561 if (aicon->icon->shadowed) {
562 aicon->icon->shadowed = 0;
565 * Update icon pixmap, RImage doesn't change,
566 * so call wIconUpdate is not needed
568 update_icon_pixmap(aicon->icon);
570 /* Paint it */
571 wAppIconPaint(aicon);
574 save_appicon(aicon, True);
576 WMFreeArray(selectedIcons);
579 static void toggleAutoAttractCallback(WMenu *menu, WMenuEntry *entry)
581 WDock *dock = (WDock *) entry->clientdata;
582 WScreen *scr = dock->screen_ptr;
584 assert(entry->clientdata != NULL);
586 /* Setting a clip as autoattracting will disable an autoattracting drawer,
587 * and reciprocally. Ask for confirmation? */
588 #ifdef CONFIRM_AUTOATTRACT_CHANGE
589 if (dock->type == WM_CLIP && scr->attracting_drawer) {
590 if (wMessageDialog(scr, _("Workspace Clip"),
591 _("The drawer that auto-attracted icons so far won't do so anymore!"),
592 _("OK"), _("Cancel"), NULL) != WAPRDefault)
593 return;
594 } else if (dock->type == WM_DRAWER) {
595 /* check if at least one clip already auto attracts icons */
596 int i, ask_for_confirmation = False;
597 for (i = 0; i < w_global.workspace.count; i++) {
598 if (w_global.workspace.array[i]->clip->attract_icons) {
599 ask_for_confirmation = True;
600 break;
603 if (ask_for_confirmation) {
604 if (wMessageDialog(scr, _("Drawer"),
605 _("Any clips that auto-attracted icons so far won't do so anymore!"),
606 _("OK"), _("Cancel"), NULL) != WAPRDefault)
607 return;
610 #endif
612 dock->attract_icons = !dock->attract_icons;
614 entry->flags.indicator_on = dock->attract_icons;
616 wMenuPaint(menu);
618 if (dock->attract_icons) {
619 if (dock->type == WM_DRAWER) {
620 /* The newly auto-attracting dock is a drawer: disable any clip and
621 * previously attracting drawer */
622 int i;
623 for (i = 0; i < w_global.workspace.count; i++)
624 w_global.workspace.array[i]->clip->attract_icons = False;
625 /* dock menu will be updated later, when opened */
627 if (scr->attracting_drawer != NULL)
628 scr->attracting_drawer->attract_icons = False;
629 scr->attracting_drawer = dock;
630 } else {
631 /* The newly auto-attracting dock is a clip: disable
632 * previously attracting drawer, if applicable */
633 if (scr->attracting_drawer != NULL) {
634 scr->attracting_drawer->attract_icons = False;
635 /* again, its menu will be updated, later. */
636 scr->attracting_drawer = NULL;
642 static void selectCallback(WMenu *menu, WMenuEntry *entry)
644 WAppIcon *icon = (WAppIcon *) entry->clientdata;
646 assert(icon != NULL);
648 wIconSelect(icon->icon);
650 wMenuPaint(menu);
653 static void colectIconsCallback(WMenu *menu, WMenuEntry *entry)
655 WAppIcon *clickedIcon = (WAppIcon *) entry->clientdata;
656 WDock *clip; /* clip... is a WM_CLIP or a WM_DRAWER */
657 WAppIcon *aicon;
658 int x, y, x_pos, y_pos;
659 Bool update_icon = False;
661 assert(entry->clientdata != NULL);
662 clip = clickedIcon->dock;
664 aicon = w_global.app_icon_list;
666 while (aicon) {
667 if (!aicon->docked && wDockFindFreeSlot(clip, &x, &y)) {
668 x_pos = clip->x_pos + x * ICON_SIZE;
669 y_pos = clip->y_pos + y * ICON_SIZE;
670 if (aicon->x_pos != x_pos || aicon->y_pos != y_pos)
671 move_window(aicon->icon->core->window, aicon->x_pos, aicon->y_pos, x_pos, y_pos);
673 aicon->attracted = 1;
674 if (!aicon->icon->shadowed) {
675 aicon->icon->shadowed = 1;
676 update_icon = True;
678 wDockAttachIcon(clip, aicon, x, y, update_icon);
679 if (clip->collapsed || !clip->mapped)
680 XUnmapWindow(dpy, aicon->icon->core->window);
682 aicon = aicon->next;
686 static void selectIconsCallback(WMenu *menu, WMenuEntry *entry)
688 WAppIcon *clickedIcon = (WAppIcon *) entry->clientdata;
689 WDock *dock;
690 WMArray *selectedIcons;
691 WMArrayIterator iter;
692 WAppIcon *btn;
693 int i;
695 assert(clickedIcon != NULL);
696 dock = clickedIcon->dock;
698 selectedIcons = getSelected(dock);
700 if (!WMGetArrayItemCount(selectedIcons)) {
701 for (i = 1; i < dock->max_icons; i++) {
702 btn = dock->icon_array[i];
703 if (btn && !btn->icon->selected)
704 wIconSelect(btn->icon);
706 } else {
707 WM_ITERATE_ARRAY(selectedIcons, btn, iter) {
708 wIconSelect(btn->icon);
711 WMFreeArray(selectedIcons);
713 wMenuPaint(menu);
716 static void toggleCollapsedCallback(WMenu *menu, WMenuEntry *entry)
718 assert(entry->clientdata != NULL);
720 toggleCollapsed(entry->clientdata);
722 entry->flags.indicator_on = ((WDock *) entry->clientdata)->collapsed;
724 wMenuPaint(menu);
727 static void toggleAutoCollapseCallback(WMenu *menu, WMenuEntry *entry)
729 WDock *dock;
730 assert(entry->clientdata != NULL);
732 dock = (WDock *) entry->clientdata;
734 dock->auto_collapse = !dock->auto_collapse;
736 entry->flags.indicator_on = ((WDock *) entry->clientdata)->auto_collapse;
738 wMenuPaint(menu);
741 static void toggleAutoRaiseLower(WDock *dock)
743 WDrawerChain *dc;
745 dock->auto_raise_lower = !dock->auto_raise_lower;
746 if (dock->type == WM_DOCK)
748 for (dc = dock->screen_ptr->drawers; dc != NULL; dc = dc->next) {
749 toggleAutoRaiseLower(dc->adrawer);
754 static void toggleAutoRaiseLowerCallback(WMenu *menu, WMenuEntry *entry)
756 WDock *dock;
757 assert(entry->clientdata != NULL);
759 dock = (WDock *) entry->clientdata;
761 toggleAutoRaiseLower(dock);
763 entry->flags.indicator_on = ((WDock *) entry->clientdata)->auto_raise_lower;
765 wMenuPaint(menu);
768 static void launchCallback(WMenu *menu, WMenuEntry *entry)
770 WAppIcon *btn = (WAppIcon *) entry->clientdata;
772 launchDockedApplication(btn, False);
775 static void settingsCallback(WMenu *menu, WMenuEntry *entry)
777 WAppIcon *btn = (WAppIcon *) entry->clientdata;
779 if (btn->editing)
780 return;
781 ShowDockAppSettingsPanel(btn);
784 static void hideCallback(WMenu *menu, WMenuEntry *entry)
786 WApplication *wapp;
787 WAppIcon *btn = (WAppIcon *) entry->clientdata;
789 wapp = wApplicationOf(btn->icon->owner->main_window);
791 if (wapp->flags.hidden) {
792 wWorkspaceChange(btn->icon->core->screen_ptr, wapp->last_workspace);
793 wUnhideApplication(wapp, False, False);
794 } else {
795 wHideApplication(wapp);
799 static void unhideHereCallback(WMenu *menu, WMenuEntry *entry)
801 WApplication *wapp;
802 WAppIcon *btn = (WAppIcon *) entry->clientdata;
804 wapp = wApplicationOf(btn->icon->owner->main_window);
806 wUnhideApplication(wapp, False, True);
809 /* Name is only used when type == WM_DRAWER and when restoring a specific
810 * drawer, with a specific name. When creating a drawer, leave name to NULL
811 * and mainIconCreate will find the first unused unique name */
812 static WAppIcon *mainIconCreate(WScreen *scr, int type, const char *name)
814 WAppIcon *btn;
815 int x_pos;
817 switch(type) {
818 case WM_CLIP:
819 if (w_global.clip.icon)
820 return w_global.clip.icon;
822 btn = wAppIconCreateForDock(scr, NULL, "Logo", "WMClip", TILE_CLIP);
823 btn->icon->core->descriptor.handle_expose = clipIconExpose;
824 x_pos = 0;
825 break;
826 case WM_DOCK:
827 default: /* to avoid a warning about btn and x_pos, basically */
828 btn = wAppIconCreateForDock(scr, NULL, "Logo", "WMDock", TILE_NORMAL);
829 if (wPreferences.flags.clip_merged_in_dock)
830 btn->icon->core->descriptor.handle_expose = clipIconExpose;
831 x_pos = scr->scr_width - ICON_SIZE - DOCK_EXTRA_SPACE;
832 break;
833 case WM_DRAWER:
834 if (name == NULL)
835 name = findUniqueName(scr, "Drawer");
836 btn = wAppIconCreateForDock(scr, NULL, name, "WMDrawer", TILE_DRAWER);
837 btn->icon->core->descriptor.handle_expose = drawerIconExpose;
838 x_pos = 0;
841 btn->xindex = 0;
842 btn->yindex = 0;
844 btn->icon->core->descriptor.handle_mousedown = iconMouseDown;
845 btn->icon->core->descriptor.handle_enternotify = clipEnterNotify;
846 btn->icon->core->descriptor.handle_leavenotify = clipLeaveNotify;
847 btn->icon->core->descriptor.parent_type = WCLASS_DOCK_ICON;
848 btn->icon->core->descriptor.parent = btn;
849 XMapWindow(dpy, btn->icon->core->window);
850 btn->x_pos = x_pos;
851 btn->y_pos = 0;
852 btn->docked = 1;
853 if (type == WM_CLIP ||
854 (type == WM_DOCK && wPreferences.flags.clip_merged_in_dock))
855 w_global.clip.icon = btn;
857 return btn;
860 static void switchWSCommand(WMenu *menu, WMenuEntry *entry)
862 WAppIcon *btn, *icon = (WAppIcon *) entry->clientdata;
863 WDock *src, *dest;
864 WMArray *selectedIcons;
865 int x, y;
867 if (entry->order == w_global.workspace.current)
868 return;
869 src = icon->dock;
870 dest = w_global.workspace.array[entry->order]->clip;
872 selectedIcons = getSelected(src);
874 if (WMGetArrayItemCount(selectedIcons)) {
875 WMArrayIterator iter;
877 WM_ITERATE_ARRAY(selectedIcons, btn, iter) {
878 if (wDockFindFreeSlot(dest, &x, &y)) {
879 wDockMoveIconBetweenDocks(src, dest, btn, x, y);
880 XUnmapWindow(dpy, btn->icon->core->window);
883 } else if (icon != w_global.clip.icon) {
884 if (wDockFindFreeSlot(dest, &x, &y)) {
885 wDockMoveIconBetweenDocks(src, dest, icon, x, y);
886 XUnmapWindow(dpy, icon->icon->core->window);
889 WMFreeArray(selectedIcons);
892 static void launchDockedApplication(WAppIcon *btn, Bool withSelection)
894 WScreen *scr = btn->icon->core->screen_ptr;
896 if (!btn->launching &&
897 ((!withSelection && btn->command != NULL) || (withSelection && btn->paste_command != NULL))) {
898 if (!btn->forced_dock) {
899 btn->relaunching = btn->running;
900 btn->running = 1;
902 if (btn->wm_instance || btn->wm_class) {
903 WWindowAttributes attr;
904 memset(&attr, 0, sizeof(WWindowAttributes));
905 wDefaultFillAttributes(btn->wm_instance, btn->wm_class, &attr, NULL, True);
907 if (!attr.no_appicon && !btn->buggy_app)
908 btn->launching = 1;
909 else
910 btn->running = 0;
912 btn->drop_launch = 0;
913 btn->paste_launch = withSelection;
914 scr->last_dock = btn->dock;
915 btn->pid = execCommand(btn, (withSelection ? btn->paste_command : btn->command), NULL);
916 if (btn->pid > 0) {
917 if (btn->buggy_app) {
918 /* give feedback that the app was launched */
919 btn->launching = 1;
920 dockIconPaint(btn);
921 btn->launching = 0;
922 WMAddTimerHandler(200, (WMCallback *) dockIconPaint, btn);
923 } else {
924 dockIconPaint(btn);
926 } else {
927 wwarning(_("could not launch application %s"), btn->command);
928 btn->launching = 0;
929 if (!btn->relaunching)
930 btn->running = 0;
935 static void updateWorkspaceMenu(WMenu *menu, WAppIcon *icon)
937 char title[MAX_WORKSPACENAME_WIDTH + 1];
938 int i;
940 if (!menu || !icon)
941 return;
943 for (i = 0; i < w_global.workspace.count; i++) {
944 if (i < menu->entry_no) {
945 if (strcmp(menu->entries[i]->text, w_global.workspace.array[i]->name) != 0) {
946 wfree(menu->entries[i]->text);
947 strcpy(title, w_global.workspace.array[i]->name);
948 menu->entries[i]->text = wstrdup(title);
949 menu->flags.realized = 0;
951 menu->entries[i]->clientdata = (void *)icon;
952 } else {
953 strcpy(title, w_global.workspace.array[i]->name);
955 wMenuAddCallback(menu, title, switchWSCommand, (void *)icon);
957 menu->flags.realized = 0;
960 if (i == w_global.workspace.current)
961 wMenuSetEnabled(menu, i, False);
962 else
963 wMenuSetEnabled(menu, i, True);
966 if (!menu->flags.realized)
967 wMenuRealize(menu);
970 static WMenu *makeWorkspaceMenu(WScreen *scr)
972 WMenu *menu;
974 menu = wMenuCreate(scr, NULL, False);
975 if (!menu)
976 wwarning(_("could not create workspace submenu for Clip menu"));
978 wMenuAddCallback(menu, "", switchWSCommand, (void *)w_global.clip.icon);
980 menu->flags.realized = 0;
981 wMenuRealize(menu);
983 return menu;
986 static void updateClipOptionsMenu(WMenu *menu, WDock *dock)
988 WMenuEntry *entry;
989 int index = 0;
991 if (!menu || !dock)
992 return;
994 /* keep on top */
995 entry = menu->entries[index];
996 entry->flags.indicator_on = !dock->lowered;
997 entry->clientdata = dock;
998 wMenuSetEnabled(menu, index, dock->type == WM_CLIP);
1000 /* collapsed */
1001 entry = menu->entries[++index];
1002 entry->flags.indicator_on = dock->collapsed;
1003 entry->clientdata = dock;
1005 /* auto-collapse */
1006 entry = menu->entries[++index];
1007 entry->flags.indicator_on = dock->auto_collapse;
1008 entry->clientdata = dock;
1010 /* auto-raise/lower */
1011 entry = menu->entries[++index];
1012 entry->flags.indicator_on = dock->auto_raise_lower;
1013 entry->clientdata = dock;
1014 wMenuSetEnabled(menu, index, dock->lowered && (dock->type == WM_CLIP));
1016 /* attract icons */
1017 entry = menu->entries[++index];
1018 entry->flags.indicator_on = dock->attract_icons;
1019 entry->clientdata = dock;
1021 menu->flags.realized = 0;
1022 wMenuRealize(menu);
1026 static WMenu *makeClipOptionsMenu(WScreen *scr)
1028 WMenu *menu;
1029 WMenuEntry *entry;
1031 menu = wMenuCreate(scr, NULL, False);
1032 if (!menu) {
1033 wwarning(_("could not create options submenu for Clip menu"));
1034 return NULL;
1037 entry = wMenuAddCallback(menu, _("Keep on Top"), toggleLoweredCallback, NULL);
1038 entry->flags.indicator = 1;
1039 entry->flags.indicator_on = 1;
1040 entry->flags.indicator_type = MI_CHECK;
1042 entry = wMenuAddCallback(menu, _("Collapsed"), toggleCollapsedCallback, NULL);
1043 entry->flags.indicator = 1;
1044 entry->flags.indicator_on = 1;
1045 entry->flags.indicator_type = MI_CHECK;
1047 entry = wMenuAddCallback(menu, _("Autocollapse"), toggleAutoCollapseCallback, NULL);
1048 entry->flags.indicator = 1;
1049 entry->flags.indicator_on = 1;
1050 entry->flags.indicator_type = MI_CHECK;
1052 entry = wMenuAddCallback(menu, _("Autoraise"), toggleAutoRaiseLowerCallback, NULL);
1053 entry->flags.indicator = 1;
1054 entry->flags.indicator_on = 1;
1055 entry->flags.indicator_type = MI_CHECK;
1057 entry = wMenuAddCallback(menu, _("Autoattract Icons"), toggleAutoAttractCallback, NULL);
1058 entry->flags.indicator = 1;
1059 entry->flags.indicator_on = 1;
1060 entry->flags.indicator_type = MI_CHECK;
1062 menu->flags.realized = 0;
1063 wMenuRealize(menu);
1065 return menu;
1069 static void setDockPositionNormalCallback(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 auto raise lower or keep on top?
1076 if (dock->auto_raise_lower)
1078 dock->auto_raise_lower = 0;
1079 // Only for aesthetic purposes, can be removed when Autoraise status is no longer exposed in drawer option menu
1080 for (dc = dock->screen_ptr->drawers; dc != NULL; dc = dc->next) {
1081 dc->adrawer->auto_raise_lower = 0;
1084 else
1086 // Will take care of setting lowered = 0 in drawers
1087 toggleLowered(dock);
1089 entry->flags.indicator_on = 1;
1092 static void setDockPositionAutoRaiseLowerCallback(WMenu *menu, WMenuEntry *entry)
1094 WDock *dock = (WDock *) entry->clientdata;
1095 WDrawerChain *dc;
1096 if (entry->flags.indicator_on) // already set, nothing to do
1097 return;
1098 // Do we come from normal or keep on top?
1099 if (!dock->lowered)
1101 toggleLowered(dock);
1103 dock->auto_raise_lower = 1;
1104 // Only for aesthetic purposes, can be removed when Autoraise status is no longer exposed in drawer option menu
1105 for (dc = dock->screen_ptr->drawers; dc != NULL; dc = dc->next) {
1106 dc->adrawer->auto_raise_lower = 1;
1108 entry->flags.indicator_on = 1;
1111 static void setDockPositionKeepOnTopCallback(WMenu *menu, WMenuEntry *entry)
1113 WDock *dock = (WDock *) entry->clientdata;
1114 WDrawerChain *dc;
1115 if (entry->flags.indicator_on) // already set, nothing to do
1116 return;
1117 dock->auto_raise_lower = 0;
1118 // Only for aesthetic purposes, can be removed when Autoraise status is no longer exposed in drawer option menu
1119 for (dc = dock->screen_ptr->drawers; dc != NULL; dc = dc->next) {
1120 dc->adrawer->auto_raise_lower = 0;
1122 toggleLowered(dock);
1123 entry->flags.indicator_on = 1;
1126 static void updateDockPositionMenu(WMenu *menu, WDock *dock)
1128 WMenuEntry *entry;
1129 int index = 0;
1131 assert(menu);
1132 assert(dock);
1134 /* Normal level */
1135 entry = menu->entries[index++];
1136 entry->flags.indicator_on = (dock->lowered && !dock->auto_raise_lower);
1137 entry->clientdata = dock;
1139 /* Auto-raise/lower */
1140 entry = menu->entries[index++];
1141 entry->flags.indicator_on = dock->auto_raise_lower;
1142 entry->clientdata = dock;
1144 /* Keep on top */
1145 entry = menu->entries[index++];
1146 entry->flags.indicator_on = !dock->lowered;
1147 entry->clientdata = dock;
1150 static WMenu *makeDockPositionMenu(WScreen *scr)
1152 /* When calling this, the dock is being created, so scr->dock is still not set
1153 * Therefore the callbacks' clientdata and the indicators can't be set,
1154 * they will be updated when the dock menu is opened. */
1155 WMenu *menu;
1156 WMenuEntry *entry;
1158 menu = wMenuCreate(scr, NULL, False);
1159 if (!menu) {
1160 wwarning(_("could not create options submenu for dock position menu"));
1161 return NULL;
1164 entry = wMenuAddCallback(menu, _("Normal"), setDockPositionNormalCallback, NULL);
1165 entry->flags.indicator = 1;
1166 entry->flags.indicator_type = MI_DIAMOND;
1168 entry = wMenuAddCallback(menu, _("Auto raise & lower"), setDockPositionAutoRaiseLowerCallback, NULL);
1169 entry->flags.indicator = 1;
1170 entry->flags.indicator_type = MI_DIAMOND;
1172 entry = wMenuAddCallback(menu, _("Keep on Top"), setDockPositionKeepOnTopCallback, NULL);
1173 entry->flags.indicator = 1;
1174 entry->flags.indicator_type = MI_DIAMOND;
1176 menu->flags.realized = 0;
1177 wMenuRealize(menu);
1179 return menu;
1183 static WMenu *dockMenuCreate(WScreen *scr, int type)
1185 WMenu *menu;
1186 WMenuEntry *entry;
1188 if (type == WM_CLIP && w_global.clip.menu)
1189 return w_global.clip.menu;
1191 if (type == WM_DRAWER && w_global.dock.drawer_menu)
1192 return w_global.dock.drawer_menu;
1194 menu = wMenuCreate(scr, NULL, False);
1195 if (type == WM_DOCK) {
1196 entry = wMenuAddCallback(menu, _("Dock position"), NULL, NULL);
1197 if (w_global.dock.pos_menu == NULL)
1198 w_global.dock.pos_menu = makeDockPositionMenu(scr);
1199 wMenuEntrySetCascade(menu, entry, w_global.dock.pos_menu);
1201 if (!wPreferences.flags.nodrawer) {
1202 entry = wMenuAddCallback(menu, _("Add a drawer"), addADrawerCallback, NULL);
1204 } else {
1205 if (type == WM_CLIP)
1206 entry = wMenuAddCallback(menu, _("Clip Options"), NULL, NULL);
1207 else /* if (type == WM_DRAWER) */
1208 entry = wMenuAddCallback(menu, _("Drawer options"), NULL, NULL);
1210 if (w_global.clip.opt_menu == NULL)
1211 w_global.clip.opt_menu = makeClipOptionsMenu(scr);
1213 wMenuEntrySetCascade(menu, entry, w_global.clip.opt_menu);
1215 /* The same menu is used for the dock and its appicons. If the menu
1216 * entry text is different between the two contexts, or if it can
1217 * change depending on some state, free the duplicated string (from
1218 * wMenuInsertCallback) and use gettext's string */
1219 if (type == WM_CLIP) {
1220 entry = wMenuAddCallback(menu, _("Rename Workspace"), renameCallback, NULL);
1221 wfree(entry->text);
1222 entry->text = _("Rename Workspace"); /* can be: (Toggle) Omnipresent */
1225 entry = wMenuAddCallback(menu, _("Selected"), selectCallback, NULL);
1226 entry->flags.indicator = 1;
1227 entry->flags.indicator_on = 1;
1228 entry->flags.indicator_type = MI_CHECK;
1230 entry = wMenuAddCallback(menu, _("Select All Icons"), selectIconsCallback, NULL);
1231 wfree(entry->text);
1232 entry->text = _("Select All Icons"); /* can be: Unselect all icons */
1234 entry = wMenuAddCallback(menu, _("Keep Icon"), keepIconsCallback, NULL);
1235 wfree(entry->text);
1236 entry->text = _("Keep Icon"); /* can be: Keep Icons */
1238 if (type == WM_CLIP) {
1239 entry = wMenuAddCallback(menu, _("Move Icon To"), NULL, NULL);
1240 wfree(entry->text);
1241 entry->text = _("Move Icon To"); /* can be: Move Icons to */
1242 w_global.clip.submenu = makeWorkspaceMenu(scr);
1243 if (w_global.clip.submenu)
1244 wMenuEntrySetCascade(menu, entry, w_global.clip.submenu);
1247 entry = wMenuAddCallback(menu, _("Remove Icon"), removeIconsCallback, NULL);
1248 wfree(entry->text);
1249 entry->text = _("Remove Icon"); /* can be: Remove Icons */
1251 wMenuAddCallback(menu, _("Attract Icons"), colectIconsCallback, NULL);
1254 wMenuAddCallback(menu, _("Launch"), launchCallback, NULL);
1256 wMenuAddCallback(menu, _("Unhide Here"), unhideHereCallback, NULL);
1258 entry = wMenuAddCallback(menu, _("Hide"), hideCallback, NULL);
1259 wfree(entry->text);
1260 entry->text = _("Hide"); /* can be: Unhide */
1262 wMenuAddCallback(menu, _("Settings..."), settingsCallback, NULL);
1264 entry = wMenuAddCallback(menu, _("Kill"), killCallback, NULL);
1265 wfree(entry->text);
1266 entry->text = _("Kill"); /* can be: Remove drawer */
1268 if (type == WM_CLIP)
1269 w_global.clip.menu = menu;
1271 if (type == WM_DRAWER)
1272 w_global.dock.drawer_menu = menu;
1274 return menu;
1277 WDock *wDockCreate(WScreen *scr, int type, const char *name)
1279 WDock *dock;
1280 WAppIcon *btn;
1282 make_keys();
1284 dock = wmalloc(sizeof(WDock));
1286 switch (type) {
1287 case WM_CLIP:
1288 dock->max_icons = DOCK_MAX_ICONS;
1289 break;
1290 case WM_DRAWER:
1291 dock->max_icons = scr->scr_width / wPreferences.icon_size;
1292 break;
1293 case WM_DOCK:
1294 default:
1295 dock->max_icons = scr->scr_height / wPreferences.icon_size;
1298 dock->icon_array = wmalloc(sizeof(WAppIcon *) * dock->max_icons);
1300 btn = mainIconCreate(scr, type, name);
1302 btn->dock = dock;
1304 dock->x_pos = btn->x_pos;
1305 dock->y_pos = btn->y_pos;
1306 dock->screen_ptr = scr;
1307 dock->type = type;
1308 dock->icon_count = 1;
1309 if (type == WM_DRAWER)
1310 dock->on_right_side = scr->dock->on_right_side;
1311 else
1312 dock->on_right_side = 1;
1313 dock->collapsed = 0;
1314 dock->auto_collapse = 0;
1315 dock->auto_collapse_magic = NULL;
1316 dock->auto_raise_lower = 0;
1317 dock->auto_lower_magic = NULL;
1318 dock->auto_raise_magic = NULL;
1319 dock->attract_icons = 0;
1320 dock->lowered = 1;
1321 dock->icon_array[0] = btn;
1322 wRaiseFrame(btn->icon->core);
1323 XMoveWindow(dpy, btn->icon->core->window, btn->x_pos, btn->y_pos);
1325 /* create dock menu */
1326 dock->menu = dockMenuCreate(scr, type);
1328 if (type == WM_DRAWER) {
1329 drawerAppendToChain(scr, dock);
1330 dock->auto_collapse = 1;
1333 return dock;
1336 void wDockDestroy(WDock *dock)
1338 int i;
1339 WAppIcon *aicon;
1341 for (i = (dock->type == WM_CLIP) ? 1 : 0; i < dock->max_icons; i++) {
1342 aicon = dock->icon_array[i];
1343 if (aicon) {
1344 int keepit = aicon->running && wApplicationOf(aicon->main_window);
1345 wDockDetach(dock, aicon);
1346 if (keepit) {
1347 /* XXX: can: aicon->icon == NULL ? */
1348 PlaceIcon(dock->screen_ptr, &aicon->x_pos, &aicon->y_pos,
1349 wGetHeadForWindow(aicon->icon->owner));
1350 XMoveWindow(dpy, aicon->icon->core->window, aicon->x_pos, aicon->y_pos);
1351 if (!dock->mapped || dock->collapsed)
1352 XMapWindow(dpy, aicon->icon->core->window);
1356 if (wPreferences.auto_arrange_icons)
1357 wArrangeIcons(dock->screen_ptr, True);
1358 wfree(dock->icon_array);
1359 if (dock->menu && dock->type != WM_CLIP)
1360 wMenuDestroy(dock->menu, True);
1361 if (dock->screen_ptr->last_dock == dock)
1362 dock->screen_ptr->last_dock = NULL;
1363 wfree(dock);
1366 void wClipIconPaint(void)
1368 WAppIcon *aicon = w_global.clip.icon;
1369 WScreen *scr = aicon->icon->core->screen_ptr;
1370 WWorkspace *workspace = w_global.workspace.array[w_global.workspace.current];
1371 WMColor *color;
1372 Window win = aicon->icon->core->window;
1373 int length, nlength;
1374 char *ws_name, ws_number[10];
1375 int ty, tx;
1377 wIconPaint(aicon->icon);
1379 length = strlen(workspace->name);
1380 ws_name = wmalloc(length + 1);
1381 snprintf(ws_name, length + 1, "%s", workspace->name);
1382 snprintf(ws_number, sizeof(ws_number), "%i", w_global.workspace.current + 1);
1383 nlength = strlen(ws_number);
1385 if (wPreferences.flags.noclip || !workspace->clip->collapsed)
1386 color = scr->clip_title_color[CLIP_NORMAL];
1387 else
1388 color = scr->clip_title_color[CLIP_COLLAPSED];
1390 ty = ICON_SIZE - WMFontHeight(scr->clip_title_font) - 3;
1392 tx = CLIP_BUTTON_SIZE * ICON_SIZE / 64;
1394 if(wPreferences.show_clip_title)
1395 WMDrawString(scr->wmscreen, win, color, scr->clip_title_font, tx, ty, ws_name, length);
1397 tx = (ICON_SIZE / 2 - WMWidthOfString(scr->clip_title_font, ws_number, nlength)) / 2;
1399 WMDrawString(scr->wmscreen, win, color, scr->clip_title_font, tx, 2, ws_number, nlength);
1401 wfree(ws_name);
1403 if (aicon->launching)
1404 XFillRectangle(dpy, aicon->icon->core->window, scr->stipple_gc,
1405 0, 0, wPreferences.icon_size, wPreferences.icon_size);
1407 paintClipButtons(aicon, aicon->dock->lclip_button_pushed, aicon->dock->rclip_button_pushed);
1410 static void clipIconExpose(WObjDescriptor *desc, XEvent *event)
1412 wClipIconPaint();
1415 static void dockIconPaint(WAppIcon *btn)
1417 if (btn == w_global.clip.icon) {
1418 wClipIconPaint();
1419 } else if (wIsADrawer(btn)) {
1420 wDrawerIconPaint(btn);
1421 } else {
1422 wAppIconPaint(btn);
1423 save_appicon(btn, True);
1427 static WMPropList *make_icon_state(WAppIcon *btn)
1429 WMPropList *node = NULL;
1430 WMPropList *command, *autolaunch, *lock, *name, *forced;
1431 WMPropList *position, *buggy, *omnipresent;
1432 char *tmp;
1433 char buffer[64];
1435 if (btn) {
1436 if (!btn->command)
1437 command = WMCreatePLString("-");
1438 else
1439 command = WMCreatePLString(btn->command);
1441 autolaunch = btn->auto_launch ? dYes : dNo;
1443 lock = btn->lock ? dYes : dNo;
1445 tmp = EscapeWM_CLASS(btn->wm_instance, btn->wm_class);
1447 name = WMCreatePLString(tmp);
1449 wfree(tmp);
1451 forced = btn->forced_dock ? dYes : dNo;
1453 buggy = btn->buggy_app ? dYes : dNo;
1455 if (!wPreferences.flags.clip_merged_in_dock && btn == w_global.clip.icon)
1456 snprintf(buffer, sizeof(buffer), "%i,%i", btn->x_pos, btn->y_pos);
1457 else
1458 snprintf(buffer, sizeof(buffer), "%hi,%hi", btn->xindex, btn->yindex);
1459 position = WMCreatePLString(buffer);
1461 node = WMCreatePLDictionary(dCommand, command,
1462 dName, name,
1463 dAutoLaunch, autolaunch,
1464 dLock, lock,
1465 dForced, forced, dBuggyApplication, buggy, dPosition, position, NULL);
1466 WMReleasePropList(command);
1467 WMReleasePropList(name);
1468 WMReleasePropList(position);
1470 omnipresent = btn->omnipresent ? dYes : dNo;
1471 if (btn->dock != btn->icon->core->screen_ptr->dock && (btn->xindex != 0 || btn->yindex != 0))
1472 WMPutInPLDictionary(node, dOmnipresent, omnipresent);
1474 #ifdef XDND /* was OFFIX */
1475 if (btn->dnd_command) {
1476 command = WMCreatePLString(btn->dnd_command);
1477 WMPutInPLDictionary(node, dDropCommand, command);
1478 WMReleasePropList(command);
1480 #endif /* XDND */
1482 if (btn->paste_command) {
1483 command = WMCreatePLString(btn->paste_command);
1484 WMPutInPLDictionary(node, dPasteCommand, command);
1485 WMReleasePropList(command);
1489 return node;
1492 static WMPropList *dockSaveState(WDock *dock)
1494 int i;
1495 WMPropList *icon_info;
1496 WMPropList *list = NULL, *dock_state = NULL;
1497 WMPropList *value, *key;
1498 char buffer[256];
1500 list = WMCreatePLArray(NULL);
1502 for (i = (dock->type == WM_DOCK ? 0 : 1); i < dock->max_icons; i++) {
1503 WAppIcon *btn = dock->icon_array[i];
1505 if (!btn || btn->attracted)
1506 continue;
1508 if ((icon_info = make_icon_state(dock->icon_array[i]))) {
1509 WMAddToPLArray(list, icon_info);
1510 WMReleasePropList(icon_info);
1514 dock_state = WMCreatePLDictionary(dApplications, list, NULL);
1516 if (dock->type == WM_DOCK) {
1517 snprintf(buffer, sizeof(buffer), "Applications%i", dock->screen_ptr->scr_height);
1518 key = WMCreatePLString(buffer);
1519 WMPutInPLDictionary(dock_state, key, list);
1520 WMReleasePropList(key);
1522 snprintf(buffer, sizeof(buffer), "%i,%i", (dock->on_right_side ? -ICON_SIZE : 0), dock->y_pos);
1523 value = WMCreatePLString(buffer);
1524 WMPutInPLDictionary(dock_state, dPosition, value);
1525 WMReleasePropList(value);
1527 WMReleasePropList(list);
1529 if (dock->type == WM_CLIP || dock->type == WM_DRAWER) {
1530 value = (dock->collapsed ? dYes : dNo);
1531 WMPutInPLDictionary(dock_state, dCollapsed, value);
1533 value = (dock->auto_collapse ? dYes : dNo);
1534 WMPutInPLDictionary(dock_state, dAutoCollapse, value);
1536 value = (dock->attract_icons ? dYes : dNo);
1537 WMPutInPLDictionary(dock_state, dAutoAttractIcons, value);
1540 if (dock->type == WM_DOCK || dock->type == WM_CLIP) {
1541 value = (dock->lowered ? dYes : dNo);
1542 WMPutInPLDictionary(dock_state, dLowered, value);
1544 value = (dock->auto_raise_lower ? dYes : dNo);
1545 WMPutInPLDictionary(dock_state, dAutoRaiseLower, value);
1548 return dock_state;
1551 void wDockSaveState(WScreen *scr, WMPropList *old_state)
1553 WMPropList *dock_state;
1554 WMPropList *keys;
1556 dock_state = dockSaveState(scr->dock);
1559 * Copy saved states of docks with different sizes.
1561 if (old_state) {
1562 int i;
1563 WMPropList *tmp;
1565 keys = WMGetPLDictionaryKeys(old_state);
1566 for (i = 0; i < WMGetPropListItemCount(keys); i++) {
1567 tmp = WMGetFromPLArray(keys, i);
1569 if (strncasecmp(WMGetFromPLString(tmp), "applications", 12) == 0
1570 && !WMGetFromPLDictionary(dock_state, tmp)) {
1572 WMPutInPLDictionary(dock_state, tmp, WMGetFromPLDictionary(old_state, tmp));
1575 WMReleasePropList(keys);
1578 WMPutInPLDictionary(w_global.session_state, dDock, dock_state);
1579 WMReleasePropList(dock_state);
1582 void wClipSaveState(WScreen *scr)
1584 WMPropList *clip_state;
1586 clip_state = make_icon_state(w_global.clip.icon);
1588 WMPutInPLDictionary(w_global.session_state, dClip, clip_state);
1589 WMReleasePropList(clip_state);
1592 WMPropList *wClipSaveWorkspaceState(WScreen *scr, int workspace)
1594 return dockSaveState(w_global.workspace.array[workspace]->clip);
1597 static Bool getBooleanDockValue(WMPropList *value, WMPropList *key)
1599 if (value) {
1600 if (WMIsPLString(value)) {
1601 if (strcasecmp(WMGetFromPLString(value), "YES") == 0)
1602 return True;
1603 } else {
1604 wwarning(_("bad value in docked icon state info %s"), WMGetFromPLString(key));
1607 return False;
1610 static WAppIcon *restore_icon_state(WScreen *scr, WMPropList *info, int type, int index)
1612 WAppIcon *aicon;
1613 WMPropList *cmd, *value;
1614 char *wclass, *winstance, *command;
1616 cmd = WMGetFromPLDictionary(info, dCommand);
1617 if (!cmd || !WMIsPLString(cmd))
1618 return NULL;
1620 /* parse window name */
1621 value = WMGetFromPLDictionary(info, dName);
1622 if (!value)
1623 return NULL;
1625 ParseWindowName(value, &winstance, &wclass, "dock");
1627 if (!winstance && !wclass)
1628 return NULL;
1630 /* get commands */
1631 if (cmd)
1632 command = wstrdup(WMGetFromPLString(cmd));
1633 else
1634 command = NULL;
1636 if (!command || strcmp(command, "-") == 0) {
1637 if (command)
1638 wfree(command);
1639 if (wclass)
1640 wfree(wclass);
1641 if (winstance)
1642 wfree(winstance);
1644 return NULL;
1647 aicon = wAppIconCreateForDock(scr, command, winstance, wclass, TILE_NORMAL);
1648 if (wclass)
1649 wfree(wclass);
1650 if (winstance)
1651 wfree(winstance);
1652 if (command)
1653 wfree(command);
1655 aicon->icon->core->descriptor.handle_mousedown = iconMouseDown;
1656 aicon->icon->core->descriptor.handle_enternotify = clipEnterNotify;
1657 aicon->icon->core->descriptor.handle_leavenotify = clipLeaveNotify;
1658 aicon->icon->core->descriptor.parent_type = WCLASS_DOCK_ICON;
1659 aicon->icon->core->descriptor.parent = aicon;
1661 #ifdef XDND /* was OFFIX */
1662 cmd = WMGetFromPLDictionary(info, dDropCommand);
1663 if (cmd)
1664 aicon->dnd_command = wstrdup(WMGetFromPLString(cmd));
1665 #endif
1667 cmd = WMGetFromPLDictionary(info, dPasteCommand);
1668 if (cmd)
1669 aicon->paste_command = wstrdup(WMGetFromPLString(cmd));
1671 /* check auto launch */
1672 value = WMGetFromPLDictionary(info, dAutoLaunch);
1674 aicon->auto_launch = getBooleanDockValue(value, dAutoLaunch);
1676 /* check lock */
1677 value = WMGetFromPLDictionary(info, dLock);
1679 aicon->lock = getBooleanDockValue(value, dLock);
1681 /* check if it wasn't normally docked */
1682 value = WMGetFromPLDictionary(info, dForced);
1684 aicon->forced_dock = getBooleanDockValue(value, dForced);
1686 /* check if we can rely on the stuff in the app */
1687 value = WMGetFromPLDictionary(info, dBuggyApplication);
1689 aicon->buggy_app = getBooleanDockValue(value, dBuggyApplication);
1691 /* get position in the dock */
1692 value = WMGetFromPLDictionary(info, dPosition);
1693 if (value && WMIsPLString(value)) {
1694 if (sscanf(WMGetFromPLString(value), "%hi,%hi", &aicon->xindex, &aicon->yindex) != 2)
1695 wwarning(_("bad value in docked icon state info %s"), WMGetFromPLString(dPosition));
1697 /* check position sanity */
1698 /* *Very* incomplete section! */
1699 if (type == WM_DOCK) {
1700 aicon->xindex = 0;
1702 } else {
1703 aicon->yindex = index;
1704 aicon->xindex = 0;
1707 /* check if icon is omnipresent */
1708 value = WMGetFromPLDictionary(info, dOmnipresent);
1710 aicon->omnipresent = getBooleanDockValue(value, dOmnipresent);
1712 aicon->running = 0;
1713 aicon->docked = 1;
1715 return aicon;
1718 #define COMPLAIN(key) wwarning(_("bad value in dock state info:%s"), key)
1720 WAppIcon *wClipRestoreState(WScreen *scr, WMPropList *clip_state)
1722 WAppIcon *icon;
1723 WMPropList *value;
1725 icon = mainIconCreate(scr, WM_CLIP, NULL);
1727 if (!clip_state)
1728 return icon;
1730 WMRetainPropList(clip_state);
1732 /* restore position */
1734 value = WMGetFromPLDictionary(clip_state, dPosition);
1736 if (value) {
1737 if (!WMIsPLString(value)) {
1738 COMPLAIN("Position");
1739 } else {
1740 if (sscanf(WMGetFromPLString(value), "%i,%i", &icon->x_pos, &icon->y_pos) != 2)
1741 COMPLAIN("Position");
1743 /* check position sanity */
1744 if (!onScreen(scr, icon->x_pos, icon->y_pos))
1745 wScreenKeepInside(scr, &icon->x_pos, &icon->y_pos, ICON_SIZE, ICON_SIZE);
1748 #ifdef XDND /* was OFFIX */
1749 value = WMGetFromPLDictionary(clip_state, dDropCommand);
1750 if (value && WMIsPLString(value))
1751 icon->dnd_command = wstrdup(WMGetFromPLString(value));
1752 #endif
1754 value = WMGetFromPLDictionary(clip_state, dPasteCommand);
1755 if (value && WMIsPLString(value))
1756 icon->paste_command = wstrdup(WMGetFromPLString(value));
1758 WMReleasePropList(clip_state);
1760 return icon;
1763 WDock *wDockRestoreState(WScreen *scr, WMPropList *dock_state, int type)
1765 WDock *dock;
1766 WMPropList *apps;
1767 WMPropList *value;
1768 WAppIcon *aicon, *old_top;
1769 int count, i;
1771 dock = wDockCreate(scr, type, NULL);
1773 if (!dock_state)
1774 return dock;
1776 WMRetainPropList(dock_state);
1778 /* restore position */
1779 value = WMGetFromPLDictionary(dock_state, dPosition);
1780 if (value) {
1781 if (!WMIsPLString(value)) {
1782 COMPLAIN("Position");
1783 } else {
1784 if (sscanf(WMGetFromPLString(value), "%i,%i", &dock->x_pos, &dock->y_pos) != 2)
1785 COMPLAIN("Position");
1787 /* check position sanity */
1788 if (!onScreen(scr, dock->x_pos, dock->y_pos)) {
1789 int x = dock->x_pos;
1790 wScreenKeepInside(scr, &x, &dock->y_pos, ICON_SIZE, ICON_SIZE);
1793 /* Is this needed any more? */
1794 if (type == WM_CLIP) {
1795 if (dock->x_pos < 0) {
1796 dock->x_pos = 0;
1797 } else if (dock->x_pos > scr->scr_width - ICON_SIZE) {
1798 dock->x_pos = scr->scr_width - ICON_SIZE;
1800 } else {
1801 if (dock->x_pos >= 0) {
1802 dock->x_pos = DOCK_EXTRA_SPACE;
1803 dock->on_right_side = 0;
1804 } else {
1805 dock->x_pos = scr->scr_width - DOCK_EXTRA_SPACE - ICON_SIZE;
1806 dock->on_right_side = 1;
1812 /* restore lowered/raised state */
1813 dock->lowered = 0;
1815 value = WMGetFromPLDictionary(dock_state, dLowered);
1816 if (value) {
1817 if (!WMIsPLString(value)) {
1818 COMPLAIN("Lowered");
1819 } else {
1820 if (strcasecmp(WMGetFromPLString(value), "YES") == 0)
1821 dock->lowered = 1;
1825 /* restore collapsed state */
1826 dock->collapsed = 0;
1828 value = WMGetFromPLDictionary(dock_state, dCollapsed);
1829 if (value) {
1830 if (!WMIsPLString(value)) {
1831 COMPLAIN("Collapsed");
1832 } else {
1833 if (strcasecmp(WMGetFromPLString(value), "YES") == 0)
1834 dock->collapsed = 1;
1838 /* restore auto-collapsed state */
1839 value = WMGetFromPLDictionary(dock_state, dAutoCollapse);
1840 if (value) {
1841 if (!WMIsPLString(value)) {
1842 COMPLAIN("AutoCollapse");
1843 } else {
1844 if (strcasecmp(WMGetFromPLString(value), "YES") == 0) {
1845 dock->auto_collapse = 1;
1846 dock->collapsed = 1;
1851 /* restore auto-raise/lower state */
1852 value = WMGetFromPLDictionary(dock_state, dAutoRaiseLower);
1853 if (value) {
1854 if (!WMIsPLString(value)) {
1855 COMPLAIN("AutoRaiseLower");
1856 } else {
1857 if (strcasecmp(WMGetFromPLString(value), "YES") == 0)
1858 dock->auto_raise_lower = 1;
1862 /* restore attract icons state */
1863 dock->attract_icons = 0;
1865 value = WMGetFromPLDictionary(dock_state, dAutoAttractIcons);
1866 if (value) {
1867 if (!WMIsPLString(value)) {
1868 COMPLAIN("AutoAttractIcons");
1869 } else {
1870 if (strcasecmp(WMGetFromPLString(value), "YES") == 0)
1871 dock->attract_icons = 1;
1875 /* application list */
1878 WMPropList *tmp;
1879 char buffer[64];
1882 * When saving, it saves the dock state in
1883 * Applications and Applicationsnnn
1885 * When loading, it will first try Applicationsnnn.
1886 * If it does not exist, use Applications as default.
1889 snprintf(buffer, sizeof(buffer), "Applications%i", scr->scr_height);
1891 tmp = WMCreatePLString(buffer);
1892 apps = WMGetFromPLDictionary(dock_state, tmp);
1893 WMReleasePropList(tmp);
1895 if (!apps)
1896 apps = WMGetFromPLDictionary(dock_state, dApplications);
1899 if (!apps)
1900 goto finish;
1902 count = WMGetPropListItemCount(apps);
1903 if (count == 0)
1904 goto finish;
1906 old_top = dock->icon_array[0];
1908 /* dock->icon_count is set to 1 when dock is created.
1909 * Since Clip is already restored, we want to keep it so for clip,
1910 * but for dock we may change the default top tile, so we set it to 0.
1912 if (type == WM_DOCK)
1913 dock->icon_count = 0;
1915 for (i = 0; i < count; i++) {
1916 if (dock->icon_count >= dock->max_icons) {
1917 wwarning(_("there are too many icons stored in dock. Ignoring what doesn't fit"));
1918 break;
1921 value = WMGetFromPLArray(apps, i);
1922 aicon = restore_icon_state(scr, value, type, dock->icon_count);
1924 dock->icon_array[dock->icon_count] = aicon;
1926 if (aicon) {
1927 aicon->dock = dock;
1928 aicon->x_pos = dock->x_pos + (aicon->xindex * ICON_SIZE);
1929 aicon->y_pos = dock->y_pos + (aicon->yindex * ICON_SIZE);
1931 if (dock->lowered)
1932 ChangeStackingLevel(aicon->icon->core, WMNormalLevel);
1933 else
1934 ChangeStackingLevel(aicon->icon->core, WMDockLevel);
1936 wCoreConfigure(aicon->icon->core, aicon->x_pos, aicon->y_pos, 0, 0);
1937 if (!dock->collapsed)
1938 XMapWindow(dpy, aicon->icon->core->window);
1940 wRaiseFrame(aicon->icon->core);
1942 dock->icon_count++;
1943 } else if (dock->icon_count == 0 && type == WM_DOCK) {
1944 dock->icon_count++;
1948 /* if the first icon is not defined, use the default */
1949 if (dock->icon_array[0] == NULL) {
1950 /* update default icon */
1951 old_top->x_pos = dock->x_pos;
1952 old_top->y_pos = dock->y_pos;
1953 if (dock->lowered)
1954 ChangeStackingLevel(old_top->icon->core, WMNormalLevel);
1955 else
1956 ChangeStackingLevel(old_top->icon->core, WMDockLevel);
1958 dock->icon_array[0] = old_top;
1959 XMoveWindow(dpy, old_top->icon->core->window, dock->x_pos, dock->y_pos);
1960 /* we don't need to increment dock->icon_count here because it was
1961 * incremented in the loop above.
1963 } else if (old_top != dock->icon_array[0]) {
1964 if (old_top == w_global.clip.icon) /* TODO dande: understand the logic */
1965 w_global.clip.icon = dock->icon_array[0];
1967 wAppIconDestroy(old_top);
1970 finish:
1971 WMReleasePropList(dock_state);
1973 return dock;
1976 void wDockLaunchWithState(WDock *dock, WAppIcon *btn, WSavedState *state)
1978 if (btn && btn->command && !btn->running && !btn->launching) {
1979 btn->drop_launch = 0;
1980 btn->paste_launch = 0;
1982 btn->pid = execCommand(btn, btn->command, state);
1984 if (btn->pid > 0) {
1985 if (!btn->forced_dock && !btn->buggy_app) {
1986 btn->launching = 1;
1987 dockIconPaint(btn);
1990 } else {
1991 wfree(state);
1995 void wDockDoAutoLaunch(WDock *dock, int workspace)
1997 WAppIcon *btn;
1998 WSavedState *state;
1999 int i;
2001 for (i = 0; i < dock->max_icons; i++) {
2002 btn = dock->icon_array[i];
2003 if (!btn || !btn->auto_launch)
2004 continue;
2006 state = wmalloc(sizeof(WSavedState));
2007 state->workspace = workspace;
2008 /* TODO: this is klugy and is very difficult to understand
2009 * what's going on. Try to clean up */
2010 wDockLaunchWithState(dock, btn, state);
2014 #ifdef XDND /* was OFFIX */
2015 static WDock *findDock(WScreen *scr, XEvent *event, int *icon_pos)
2017 WDock *dock;
2018 int i;
2020 *icon_pos = -1;
2021 if ((dock = scr->dock) != NULL) {
2022 for (i = 0; i < dock->max_icons; i++) {
2023 if (dock->icon_array[i]
2024 && dock->icon_array[i]->icon->core->window == event->xclient.window) {
2025 *icon_pos = i;
2026 break;
2030 if (*icon_pos < 0 && (dock = w_global.workspace.array[w_global.workspace.current]->clip) != NULL) {
2031 for (i = 0; i < dock->max_icons; i++) {
2032 if (dock->icon_array[i]
2033 && dock->icon_array[i]->icon->core->window == event->xclient.window) {
2034 *icon_pos = i;
2035 break;
2039 if (*icon_pos >= 0)
2040 return dock;
2041 return NULL;
2044 int wDockReceiveDNDDrop(WScreen *scr, XEvent *event)
2046 WDock *dock;
2047 WAppIcon *btn;
2048 int icon_pos;
2050 dock = findDock(scr, event, &icon_pos);
2051 if (!dock)
2052 return False;
2055 * Return True if the drop was on an application icon window.
2056 * In this case, let the ClientMessage handler redirect the
2057 * message to the app.
2059 if (dock->icon_array[icon_pos]->icon->icon_win != None)
2060 return True;
2062 if (dock->icon_array[icon_pos]->dnd_command != NULL) {
2063 scr->flags.dnd_data_convertion_status = 0;
2065 btn = dock->icon_array[icon_pos];
2067 if (!btn->forced_dock) {
2068 btn->relaunching = btn->running;
2069 btn->running = 1;
2071 if (btn->wm_instance || btn->wm_class) {
2072 WWindowAttributes attr;
2073 memset(&attr, 0, sizeof(WWindowAttributes));
2074 wDefaultFillAttributes(btn->wm_instance, btn->wm_class, &attr, NULL, True);
2076 if (!attr.no_appicon)
2077 btn->launching = 1;
2078 else
2079 btn->running = 0;
2082 btn->paste_launch = 0;
2083 btn->drop_launch = 1;
2084 scr->last_dock = dock;
2085 btn->pid = execCommand(btn, btn->dnd_command, NULL);
2086 if (btn->pid > 0) {
2087 dockIconPaint(btn);
2088 } else {
2089 btn->launching = 0;
2090 if (!btn->relaunching)
2091 btn->running = 0;
2094 return False;
2096 #endif /* XDND */
2098 Bool wDockAttachIcon(WDock *dock, WAppIcon *icon, int x, int y, Bool update_icon)
2100 WWindow *wwin;
2101 Bool lupdate_icon = False;
2102 char *command = NULL;
2103 int index;
2105 icon->editing = 0;
2107 if (update_icon)
2108 lupdate_icon = True;
2110 if (icon->command == NULL) {
2111 /* If icon->owner exists, it means the application is running */
2112 if (icon->icon->owner) {
2113 wwin = icon->icon->owner;
2114 command = GetCommandForWindow(wwin->client_win);
2117 if (command) {
2118 icon->command = command;
2119 } else {
2120 /* icon->forced_dock = 1; */
2121 if (dock->type != WM_CLIP || !icon->attracted) {
2122 icon->editing = 1;
2123 if (wInputDialog(dock->screen_ptr, _("Dock Icon"),
2124 _("Type the command used to launch the application"), &command)) {
2125 if (command && (command[0] == 0 || (command[0] == '-' && command[1] == 0))) {
2126 wfree(command);
2127 command = NULL;
2129 icon->command = command;
2130 icon->editing = 0;
2131 } else {
2132 icon->editing = 0;
2133 if (command)
2134 wfree(command);
2135 /* If the target is the dock, reject the icon. If
2136 * the target is the clip, make it an attracted icon
2138 if (dock->type == WM_CLIP) {
2139 icon->attracted = 1;
2140 if (!icon->icon->shadowed) {
2141 icon->icon->shadowed = 1;
2142 lupdate_icon = True;
2144 } else {
2145 return False;
2152 for (index = 1; index < dock->max_icons; index++)
2153 if (dock->icon_array[index] == NULL)
2154 break;
2155 /* if (index == dock->max_icons)
2156 return; */
2158 assert(index < dock->max_icons);
2160 dock->icon_array[index] = icon;
2161 icon->yindex = y;
2162 icon->xindex = x;
2164 icon->omnipresent = 0;
2166 icon->x_pos = dock->x_pos + x * ICON_SIZE;
2167 icon->y_pos = dock->y_pos + y * ICON_SIZE;
2169 dock->icon_count++;
2171 icon->running = 1;
2172 icon->launching = 0;
2173 icon->docked = 1;
2174 icon->dock = dock;
2175 icon->icon->core->descriptor.handle_mousedown = iconMouseDown;
2176 icon->icon->core->descriptor.handle_enternotify = clipEnterNotify;
2177 icon->icon->core->descriptor.handle_leavenotify = clipLeaveNotify;
2178 icon->icon->core->descriptor.parent_type = WCLASS_DOCK_ICON;
2179 icon->icon->core->descriptor.parent = icon;
2181 MoveInStackListUnder(dock->icon_array[index - 1]->icon->core, icon->icon->core);
2182 wAppIconMove(icon, icon->x_pos, icon->y_pos);
2185 * Update icon pixmap, RImage doesn't change,
2186 * so call wIconUpdate is not needed
2188 if (lupdate_icon)
2189 update_icon_pixmap(icon->icon);
2191 /* Paint it */
2192 wAppIconPaint(icon);
2194 /* Save it */
2195 save_appicon(icon, True);
2197 if (wPreferences.auto_arrange_icons)
2198 wArrangeIcons(dock->screen_ptr, True);
2200 #ifdef XDND /* was OFFIX */
2201 if (icon->command && !icon->dnd_command) {
2202 int len = strlen(icon->command) + 8;
2203 icon->dnd_command = wmalloc(len);
2204 snprintf(icon->dnd_command, len, "%s %%d", icon->command);
2206 #endif
2208 if (icon->command && !icon->paste_command) {
2209 int len = strlen(icon->command) + 8;
2210 icon->paste_command = wmalloc(len);
2211 snprintf(icon->paste_command, len, "%s %%s", icon->command);
2214 return True;
2217 void wDockReattachIcon(WDock *dock, WAppIcon *icon, int x, int y)
2219 int index;
2221 for (index = 1; index < dock->max_icons; index++) {
2222 if (dock->icon_array[index] == icon)
2223 break;
2225 assert(index < dock->max_icons);
2227 icon->yindex = y;
2228 icon->xindex = x;
2230 icon->x_pos = dock->x_pos + x * ICON_SIZE;
2231 icon->y_pos = dock->y_pos + y * ICON_SIZE;
2234 Bool wDockMoveIconBetweenDocks(WDock *src, WDock *dest, WAppIcon *icon, int x, int y)
2236 WWindow *wwin;
2237 char *command = NULL;
2238 int index;
2239 Bool update_icon = False;
2241 if (src == dest)
2242 return True; /* No move needed, we're already there */
2244 if (dest == NULL)
2245 return False;
2248 * For the moment we can't do this if we move icons in Clip from one
2249 * workspace to other, because if we move two or more icons without
2250 * command, the dialog box will not be able to tell us to which of the
2251 * moved icons it applies. -Dan
2253 if ((dest->type == WM_DOCK /*|| dest->keep_attracted */ ) && icon->command == NULL) {
2254 /* If icon->owner exists, it means the application is running */
2255 if (icon->icon->owner) {
2256 wwin = icon->icon->owner;
2257 command = GetCommandForWindow(wwin->client_win);
2260 if (command) {
2261 icon->command = command;
2262 } else {
2263 icon->editing = 1;
2264 /* icon->forced_dock = 1; */
2265 if (wInputDialog(src->screen_ptr, _("Dock Icon"),
2266 _("Type the command used to launch the application"), &command)) {
2267 if (command && (command[0] == 0 || (command[0] == '-' && command[1] == 0))) {
2268 wfree(command);
2269 command = NULL;
2271 icon->command = command;
2272 } else {
2273 icon->editing = 0;
2274 if (command)
2275 wfree(command);
2276 return False;
2278 icon->editing = 0;
2282 if (dest->type == WM_DOCK || dest->type == WM_DRAWER)
2283 wClipMakeIconOmnipresent(icon, False);
2285 for (index = 1; index < src->max_icons; index++) {
2286 if (src->icon_array[index] == icon)
2287 break;
2289 assert(index < src->max_icons);
2291 src->icon_array[index] = NULL;
2292 src->icon_count--;
2294 for (index = 1; index < dest->max_icons; index++) {
2295 if (dest->icon_array[index] == NULL)
2296 break;
2299 assert(index < dest->max_icons);
2301 dest->icon_array[index] = icon;
2302 icon->dock = dest;
2304 /* deselect the icon */
2305 if (icon->icon->selected)
2306 wIconSelect(icon->icon);
2308 icon->icon->core->descriptor.handle_enternotify = clipEnterNotify;
2309 icon->icon->core->descriptor.handle_leavenotify = clipLeaveNotify;
2311 /* set it to be kept when moving to dock.
2312 * Unless the icon does not have a command set
2314 if (icon->command && (dest->type == WM_DOCK || dest->type == WM_DRAWER)) {
2315 icon->attracted = 0;
2316 if (icon->icon->shadowed) {
2317 icon->icon->shadowed = 0;
2318 update_icon = True;
2320 save_appicon(icon, True);
2323 if (src->auto_collapse || src->auto_raise_lower)
2324 clipLeave(src);
2326 icon->yindex = y;
2327 icon->xindex = x;
2329 icon->x_pos = dest->x_pos + x * ICON_SIZE;
2330 icon->y_pos = dest->y_pos + y * ICON_SIZE;
2332 dest->icon_count++;
2334 MoveInStackListUnder(dest->icon_array[index - 1]->icon->core, icon->icon->core);
2337 * Update icon pixmap, RImage doesn't change,
2338 * so call wIconUpdate is not needed
2340 if (update_icon)
2341 update_icon_pixmap(icon->icon);
2343 /* Paint it */
2344 wAppIconPaint(icon);
2346 return True;
2349 void wDockDetach(WDock *dock, WAppIcon *icon)
2351 int index;
2352 Bool update_icon = False;
2354 /* make the settings panel be closed */
2355 if (icon->panel)
2356 DestroyDockAppSettingsPanel(icon->panel);
2358 /* This must be called before icon->dock is set to NULL.
2359 * Don't move it. -Dan
2361 wClipMakeIconOmnipresent(icon, False);
2363 icon->docked = 0;
2364 icon->dock = NULL;
2365 icon->attracted = 0;
2366 icon->auto_launch = 0;
2367 if (icon->icon->shadowed) {
2368 icon->icon->shadowed = 0;
2369 update_icon = True;
2372 /* deselect the icon */
2373 if (icon->icon->selected)
2374 wIconSelect(icon->icon);
2376 if (icon->command) {
2377 wfree(icon->command);
2378 icon->command = NULL;
2380 #ifdef XDND /* was OFFIX */
2381 if (icon->dnd_command) {
2382 wfree(icon->dnd_command);
2383 icon->dnd_command = NULL;
2385 #endif
2386 if (icon->paste_command) {
2387 wfree(icon->paste_command);
2388 icon->paste_command = NULL;
2391 for (index = 1; index < dock->max_icons; index++)
2392 if (dock->icon_array[index] == icon)
2393 break;
2395 assert(index < dock->max_icons);
2396 dock->icon_array[index] = NULL;
2397 icon->yindex = -1;
2398 icon->xindex = -1;
2400 dock->icon_count--;
2402 /* if the dock is not attached to an application or
2403 * the application did not set the appropriate hints yet,
2404 * destroy the icon */
2405 if (!icon->running || !wApplicationOf(icon->main_window)) {
2406 wAppIconDestroy(icon);
2407 } else {
2408 icon->icon->core->descriptor.handle_mousedown = appIconMouseDown;
2409 icon->icon->core->descriptor.handle_enternotify = NULL;
2410 icon->icon->core->descriptor.handle_leavenotify = NULL;
2411 icon->icon->core->descriptor.parent_type = WCLASS_APPICON;
2412 icon->icon->core->descriptor.parent = icon;
2414 ChangeStackingLevel(icon->icon->core, NORMAL_ICON_LEVEL);
2417 * Update icon pixmap, RImage doesn't change,
2418 * so call wIconUpdate is not needed
2420 if (update_icon)
2421 update_icon_pixmap(icon->icon);
2423 /* Paint it */
2424 wAppIconPaint(icon);
2426 if (wPreferences.auto_arrange_icons)
2427 wArrangeIcons(dock->screen_ptr, True);
2429 if (dock->auto_collapse || dock->auto_raise_lower)
2430 clipLeave(dock);
2434 * returns the closest Dock slot index for the passed
2435 * coordinates.
2437 * Returns False if icon can't be docked.
2439 * Note: this function should NEVER alter ret_x or ret_y, unless it will
2440 * return True. -Dan
2442 /* Redocking == true means either icon->dock == dock (normal case)
2443 * or we are called from handleDockMove for a drawer */
2444 Bool wDockSnapIcon(WDock *dock, WAppIcon *icon, int req_x, int req_y, int *ret_x, int *ret_y, int redocking)
2446 WScreen *scr = dock->screen_ptr;
2447 int dx, dy;
2448 int ex_x, ex_y;
2449 int i, offset = ICON_SIZE / 2;
2450 WAppIcon *aicon = NULL;
2451 WAppIcon *nicon = NULL;
2453 if (wPreferences.flags.noupdates)
2454 return False;
2456 dx = dock->x_pos;
2457 dy = dock->y_pos;
2459 /* if the dock is full */
2460 if (!redocking && (dock->icon_count >= dock->max_icons))
2461 return False;
2463 /* exact position */
2464 if (req_y < dy)
2465 ex_y = (req_y - offset - dy) / ICON_SIZE;
2466 else
2467 ex_y = (req_y + offset - dy) / ICON_SIZE;
2469 if (req_x < dx)
2470 ex_x = (req_x - offset - dx) / ICON_SIZE;
2471 else
2472 ex_x = (req_x + offset - dx) / ICON_SIZE;
2474 /* check if the icon is outside the screen boundaries */
2475 if (!onScreen(scr, dx + ex_x * ICON_SIZE, dy + ex_y * ICON_SIZE))
2476 return False;
2478 switch (dock->type) {
2479 case WM_DOCK:
2480 /* We can return False right away if
2481 * - we do not come from this dock (which is a WM_DOCK),
2482 * - we are not right over it, and
2483 * - we are not the main tile of a drawer.
2484 * In the latter case, we are called from handleDockMove. */
2485 if (icon->dock != dock && ex_x != 0 &&
2486 !(icon->dock && icon->dock->type == WM_DRAWER && icon == icon->dock->icon_array[0]))
2487 return False;
2489 if (!redocking && ex_x != 0)
2490 return False;
2492 if (getDrawer(scr, ex_y)) /* Return false so that the drawer gets it. */
2493 return False;
2495 aicon = NULL;
2496 for (i = 0; i < dock->max_icons; i++) {
2497 nicon = dock->icon_array[i];
2498 if (nicon && nicon->yindex == ex_y) {
2499 aicon = nicon;
2500 break;
2504 if (redocking) {
2505 int sig, done, closest;
2507 /* Possible cases when redocking:
2509 * icon dragged out of range of any slot -> false
2510 * icon dragged on a drawer -> false (to open the drawer)
2511 * icon dragged to range of free slot
2512 * icon dragged to range of same slot
2513 * icon dragged to range of different icon
2515 if (abs(ex_x) > DOCK_DETTACH_THRESHOLD)
2516 return False;
2518 if (aicon == icon || !aicon) {
2519 *ret_x = 0;
2520 *ret_y = ex_y;
2521 return True;
2524 /* start looking at the upper slot or lower? */
2525 if (ex_y * ICON_SIZE < (req_y + offset - dy))
2526 sig = 1;
2527 else
2528 sig = -1;
2530 done = 0;
2531 /* look for closest free slot */
2532 for (i = 0; i < (DOCK_DETTACH_THRESHOLD + 1) * 2 && !done; i++) {
2533 int j;
2535 done = 1;
2536 closest = sig * (i / 2) + ex_y;
2537 /* check if this slot is fully on the screen and not used */
2538 if (onScreen(scr, dx, dy + closest * ICON_SIZE)) {
2539 for (j = 0; j < dock->max_icons; j++) {
2540 if (dock->icon_array[j]
2541 && dock->icon_array[j]->yindex == closest) {
2542 /* slot is used by someone else */
2543 if (dock->icon_array[j] != icon)
2544 done = 0;
2545 break;
2548 /* slot is used by a drawer */
2549 done = done && !getDrawer(scr, closest);
2551 else // !onScreen
2552 done = 0;
2553 sig = -sig;
2555 if (done &&
2556 ((ex_y >= closest && ex_y - closest < DOCK_DETTACH_THRESHOLD + 1)
2557 || (ex_y < closest && closest - ex_y <= DOCK_DETTACH_THRESHOLD + 1))) {
2558 *ret_x = 0;
2559 *ret_y = closest;
2560 return True;
2562 } else { /* !redocking */
2564 /* if slot is free and the icon is close enough, return it */
2565 if (!aicon && ex_x == 0) {
2566 *ret_x = 0;
2567 *ret_y = ex_y;
2568 return True;
2571 break;
2572 case WM_CLIP:
2574 int neighbours = 0;
2575 int start, stop, k;
2577 start = icon->omnipresent ? 0 : w_global.workspace.current;
2578 stop = icon->omnipresent ? w_global.workspace.count : start + 1;
2580 aicon = NULL;
2581 for (k = start; k < stop; k++) {
2582 WDock *tmp = w_global.workspace.array[k]->clip;
2583 if (!tmp)
2584 continue;
2585 for (i = 0; i < tmp->max_icons; i++) {
2586 nicon = tmp->icon_array[i];
2587 if (nicon && nicon->xindex == ex_x && nicon->yindex == ex_y) {
2588 aicon = nicon;
2589 break;
2592 if (aicon)
2593 break;
2595 for (k = start; k < stop; k++) {
2596 WDock *tmp = w_global.workspace.array[k]->clip;
2597 if (!tmp)
2598 continue;
2599 for (i = 0; i < tmp->max_icons; i++) {
2600 nicon = tmp->icon_array[i];
2601 if (nicon && nicon != icon && /* Icon can't be it's own neighbour */
2602 (abs(nicon->xindex - ex_x) <= CLIP_ATTACH_VICINITY &&
2603 abs(nicon->yindex - ex_y) <= CLIP_ATTACH_VICINITY)) {
2604 neighbours = 1;
2605 break;
2608 if (neighbours)
2609 break;
2612 if (neighbours && (aicon == NULL || (redocking && aicon == icon))) {
2613 *ret_x = ex_x;
2614 *ret_y = ex_y;
2615 return True;
2617 break;
2619 case WM_DRAWER:
2621 WAppIcon *aicons_to_shift[ dock->icon_count ];
2622 int index_of_hole, j;
2624 if (ex_y != 0 ||
2625 abs(ex_x) - dock->icon_count > DOCK_DETTACH_THRESHOLD ||
2626 (ex_x < 0 && !dock->on_right_side) ||
2627 (ex_x > 0 && dock->on_right_side)) {
2628 return False;
2631 if (ex_x == 0)
2632 ex_x = (dock->on_right_side ? -1 : 1);
2634 /* "Reduce" ex_x but keep its sign */
2635 if (redocking) {
2636 if (abs(ex_x) > dock->icon_count - 1) /* minus 1: do not take icon_array[0] into account */
2637 ex_x = ex_x * (dock->icon_count - 1) / abs(ex_x); /* don't use *= ! */
2638 } else {
2639 if (abs(ex_x) > dock->icon_count)
2640 ex_x = ex_x * dock->icon_count / abs(ex_x);
2642 index_of_hole = indexOfHole(dock, icon, redocking);
2644 /* Find the appicons between where icon was (index_of_hole) and where
2645 * it wants to be (ex_x) and slide them. */
2646 j = 0;
2647 for (i = 1; i < dock->max_icons; i++) {
2648 if ((aicon = dock->icon_array[ i ]) && aicon != icon &&
2649 ((ex_x <= aicon->xindex && aicon->xindex < index_of_hole) ||
2650 (index_of_hole < aicon->xindex && aicon->xindex <= ex_x)))
2651 aicons_to_shift[ j++ ] = aicon;
2653 assert(j == abs(ex_x - index_of_hole));
2655 wSlideAppicons(aicons_to_shift, j, (index_of_hole < ex_x));
2657 *ret_x = ex_x;
2658 *ret_y = ex_y;
2659 return True;
2662 return False;
2665 static int onScreen(WScreen *scr, int x, int y)
2667 WMRect rect;
2668 int flags;
2670 rect.pos.x = x;
2671 rect.pos.y = y;
2672 rect.size.width = rect.size.height = ICON_SIZE;
2674 wGetRectPlacementInfo(scr, rect, &flags);
2676 return !(flags & (XFLAG_DEAD | XFLAG_PARTIAL));
2680 * returns true if it can find a free slot in the dock,
2681 * in which case it changes x_pos and y_pos accordingly.
2682 * Else returns false.
2684 Bool wDockFindFreeSlot(WDock *dock, int *x_pos, int *y_pos)
2686 WScreen *scr = dock->screen_ptr;
2687 WAppIcon *btn;
2688 WAppIconChain *chain;
2689 unsigned char *slot_map;
2690 int mwidth;
2691 int r;
2692 int x, y;
2693 int i, done = False;
2694 int corner;
2695 int sx = 0, ex = scr->scr_width, ey = scr->scr_height;
2696 int extra_count = 0;
2698 if (dock->type == WM_DRAWER) {
2699 if (dock->icon_count >= dock->max_icons) { /* drawer is full */
2700 return False;
2702 *x_pos = dock->icon_count * (dock->on_right_side ? -1 : 1);
2703 *y_pos = 0;
2704 return True;
2707 if (dock->type == WM_CLIP && dock != w_global.workspace.array[w_global.workspace.current]->clip)
2708 extra_count = scr->global_icon_count;
2710 /* if the dock is full */
2711 if (dock->icon_count + extra_count >= dock->max_icons)
2712 return False;
2714 if (!wPreferences.flags.nodock && scr->dock) {
2715 if (scr->dock->on_right_side)
2716 ex -= ICON_SIZE + DOCK_EXTRA_SPACE;
2717 else
2718 sx += ICON_SIZE + DOCK_EXTRA_SPACE;
2721 if (ex < dock->x_pos)
2722 ex = dock->x_pos;
2723 if (sx > dock->x_pos + ICON_SIZE)
2724 sx = dock->x_pos + ICON_SIZE;
2725 #define C_NONE 0
2726 #define C_NW 1
2727 #define C_NE 2
2728 #define C_SW 3
2729 #define C_SE 4
2731 /* check if clip is in a corner */
2732 if (dock->type == WM_CLIP) {
2733 if (dock->x_pos < 1 && dock->y_pos < 1)
2734 corner = C_NE;
2735 else if (dock->x_pos < 1 && dock->y_pos >= (ey - ICON_SIZE))
2736 corner = C_SE;
2737 else if (dock->x_pos >= (ex - ICON_SIZE) && dock->y_pos >= (ey - ICON_SIZE))
2738 corner = C_SW;
2739 else if (dock->x_pos >= (ex - ICON_SIZE) && dock->y_pos < 1)
2740 corner = C_NW;
2741 else
2742 corner = C_NONE;
2743 } else {
2744 corner = C_NONE;
2747 /* If the clip is in the corner, use only slots that are in the border
2748 * of the screen */
2749 if (corner != C_NONE) {
2750 char *hmap, *vmap;
2751 int hcount, vcount;
2753 hcount = WMIN(dock->max_icons, scr->scr_width / ICON_SIZE);
2754 vcount = WMIN(dock->max_icons, scr->scr_height / ICON_SIZE);
2755 hmap = wmalloc(hcount + 1);
2756 vmap = wmalloc(vcount + 1);
2758 /* mark used positions */
2759 switch (corner) {
2760 case C_NE:
2761 for (i = 0; i < dock->max_icons; i++) {
2762 btn = dock->icon_array[i];
2763 if (!btn)
2764 continue;
2766 if (btn->xindex == 0 && btn->yindex > 0 && btn->yindex < vcount)
2767 vmap[btn->yindex] = 1;
2768 else if (btn->yindex == 0 && btn->xindex > 0 && btn->xindex < hcount)
2769 hmap[btn->xindex] = 1;
2771 for (chain = scr->global_icons; chain != NULL; chain = chain->next) {
2772 btn = chain->aicon;
2773 if (btn->xindex == 0 && btn->yindex > 0 && btn->yindex < vcount)
2774 vmap[btn->yindex] = 1;
2775 else if (btn->yindex == 0 && btn->xindex > 0 && btn->xindex < hcount)
2776 hmap[btn->xindex] = 1;
2778 break;
2779 case C_NW:
2780 for (i = 0; i < dock->max_icons; i++) {
2781 btn = dock->icon_array[i];
2782 if (!btn)
2783 continue;
2785 if (btn->xindex == 0 && btn->yindex > 0 && btn->yindex < vcount)
2786 vmap[btn->yindex] = 1;
2787 else if (btn->yindex == 0 && btn->xindex < 0 && btn->xindex > -hcount)
2788 hmap[-btn->xindex] = 1;
2790 for (chain = scr->global_icons; chain != NULL; chain = chain->next) {
2791 btn = chain->aicon;
2792 if (btn->xindex == 0 && btn->yindex > 0 && btn->yindex < vcount)
2793 vmap[btn->yindex] = 1;
2794 else if (btn->yindex == 0 && btn->xindex < 0 && btn->xindex > -hcount)
2795 hmap[-btn->xindex] = 1;
2797 break;
2798 case C_SE:
2799 for (i = 0; i < dock->max_icons; i++) {
2800 btn = dock->icon_array[i];
2801 if (!btn)
2802 continue;
2804 if (btn->xindex == 0 && btn->yindex < 0 && btn->yindex > -vcount)
2805 vmap[-btn->yindex] = 1;
2806 else if (btn->yindex == 0 && btn->xindex > 0 && btn->xindex < hcount)
2807 hmap[btn->xindex] = 1;
2809 for (chain = scr->global_icons; chain != NULL; chain = chain->next) {
2810 btn = chain->aicon;
2811 if (btn->xindex == 0 && btn->yindex < 0 && btn->yindex > -vcount)
2812 vmap[-btn->yindex] = 1;
2813 else if (btn->yindex == 0 && btn->xindex > 0 && btn->xindex < hcount)
2814 hmap[btn->xindex] = 1;
2816 break;
2817 case C_SW:
2818 default:
2819 for (i = 0; i < dock->max_icons; i++) {
2820 btn = dock->icon_array[i];
2821 if (!btn)
2822 continue;
2824 if (btn->xindex == 0 && btn->yindex < 0 && btn->yindex > -vcount)
2825 vmap[-btn->yindex] = 1;
2826 else if (btn->yindex == 0 && btn->xindex < 0 && btn->xindex > -hcount)
2827 hmap[-btn->xindex] = 1;
2829 for (chain = scr->global_icons; chain != NULL; chain = chain->next) {
2830 btn = chain->aicon;
2831 if (btn->xindex == 0 && btn->yindex < 0 && btn->yindex > -vcount)
2832 vmap[-btn->yindex] = 1;
2833 else if (btn->yindex == 0 && btn->xindex < 0 && btn->xindex > -hcount)
2834 hmap[-btn->xindex] = 1;
2837 x = 0;
2838 y = 0;
2839 done = 0;
2840 /* search a vacant slot */
2841 for (i = 1; i < WMAX(vcount, hcount); i++) {
2842 if (i < vcount && vmap[i] == 0) {
2843 /* found a slot */
2844 x = 0;
2845 y = i;
2846 done = 1;
2847 break;
2848 } else if (i < hcount && hmap[i] == 0) {
2849 /* found a slot */
2850 x = i;
2851 y = 0;
2852 done = 1;
2853 break;
2856 wfree(vmap);
2857 wfree(hmap);
2858 /* If found a slot, translate and return */
2859 if (done) {
2860 if (corner == C_NW || corner == C_NE)
2861 *y_pos = y;
2862 else
2863 *y_pos = -y;
2865 if (corner == C_NE || corner == C_SE)
2866 *x_pos = x;
2867 else
2868 *x_pos = -x;
2870 return True;
2872 /* else, try to find a slot somewhere else */
2875 /* a map of mwidth x mwidth would be enough if we allowed icons to be
2876 * placed outside of screen */
2877 mwidth = (int)ceil(sqrt(dock->max_icons));
2879 /* In the worst case (the clip is in the corner of the screen),
2880 * the amount of icons that fit in the clip is smaller.
2881 * Double the map to get a safe value.
2883 mwidth += mwidth;
2885 r = (mwidth - 1) / 2;
2887 slot_map = wmalloc(mwidth * mwidth);
2889 #define XY2OFS(x,y) (WMAX(abs(x),abs(y)) > r) ? 0 : (((y)+r)*(mwidth)+(x)+r)
2891 /* mark used slots in the map. If the slot falls outside the map
2892 * (for example, when all icons are placed in line), ignore them. */
2893 for (i = 0; i < dock->max_icons; i++) {
2894 btn = dock->icon_array[i];
2895 if (btn)
2896 slot_map[XY2OFS(btn->xindex, btn->yindex)] = 1;
2899 for (chain = scr->global_icons; chain != NULL; chain = chain->next)
2900 slot_map[XY2OFS(chain->aicon->xindex, chain->aicon->yindex)] = 1;
2902 /* Find closest slot from the center that is free by scanning the
2903 * map from the center to outward in circular passes.
2904 * This will not result in a neat layout, but will be optimal
2905 * in the sense that there will not be holes left.
2907 done = 0;
2908 for (i = 1; i <= r && !done; i++) {
2909 int tx, ty;
2911 /* top and bottom parts of the ring */
2912 for (x = -i; x <= i && !done; x++) {
2913 tx = dock->x_pos + x * ICON_SIZE;
2914 y = -i;
2915 ty = dock->y_pos + y * ICON_SIZE;
2916 if (slot_map[XY2OFS(x, y)] == 0 && onScreen(scr, tx, ty)) {
2917 *x_pos = x;
2918 *y_pos = y;
2919 done = 1;
2920 break;
2922 y = i;
2923 ty = dock->y_pos + y * ICON_SIZE;
2924 if (slot_map[XY2OFS(x, y)] == 0 && onScreen(scr, tx, ty)) {
2925 *x_pos = x;
2926 *y_pos = y;
2927 done = 1;
2928 break;
2931 /* left and right parts of the ring */
2932 for (y = -i + 1; y <= i - 1; y++) {
2933 ty = dock->y_pos + y * ICON_SIZE;
2934 x = -i;
2935 tx = dock->x_pos + x * ICON_SIZE;
2936 if (slot_map[XY2OFS(x, y)] == 0 && onScreen(scr, tx, ty)) {
2937 *x_pos = x;
2938 *y_pos = y;
2939 done = 1;
2940 break;
2942 x = i;
2943 tx = dock->x_pos + x * ICON_SIZE;
2944 if (slot_map[XY2OFS(x, y)] == 0 && onScreen(scr, tx, ty)) {
2945 *x_pos = x;
2946 *y_pos = y;
2947 done = 1;
2948 break;
2952 wfree(slot_map);
2953 #undef XY2OFS
2954 return done;
2957 static void moveDock(WDock *dock, int new_x, int new_y)
2959 WAppIcon *btn;
2960 WDrawerChain *dc;
2961 int i;
2963 if (dock->type == WM_DOCK) {
2964 for (dc = dock->screen_ptr->drawers; dc != NULL; dc = dc->next)
2965 moveDock(dc->adrawer, new_x, dc->adrawer->y_pos - dock->y_pos + new_y);
2968 dock->x_pos = new_x;
2969 dock->y_pos = new_y;
2970 for (i = 0; i < dock->max_icons; i++) {
2971 btn = dock->icon_array[i];
2972 if (btn) {
2973 btn->x_pos = new_x + btn->xindex * ICON_SIZE;
2974 btn->y_pos = new_y + btn->yindex * ICON_SIZE;
2975 XMoveWindow(dpy, btn->icon->core->window, btn->x_pos, btn->y_pos);
2980 static void swapDock(WDock *dock)
2982 WScreen *scr = dock->screen_ptr;
2983 WAppIcon *btn;
2984 int x, i;
2986 if (dock->on_right_side)
2987 x = dock->x_pos = scr->scr_width - ICON_SIZE - DOCK_EXTRA_SPACE;
2988 else
2989 x = dock->x_pos = DOCK_EXTRA_SPACE;
2991 swapDrawers(scr, dock->on_right_side, x);
2993 for (i = 0; i < dock->max_icons; i++) {
2994 btn = dock->icon_array[i];
2995 if (btn) {
2996 btn->x_pos = x;
2997 XMoveWindow(dpy, btn->icon->core->window, btn->x_pos, btn->y_pos);
3001 wScreenUpdateUsableArea(scr);
3004 static pid_t execCommand(WAppIcon *btn, const char *command, WSavedState *state)
3006 WScreen *scr = btn->icon->core->screen_ptr;
3007 pid_t pid;
3008 char **argv;
3009 int argc;
3010 char *cmdline;
3012 cmdline = ExpandOptions(scr, command);
3014 if (scr->flags.dnd_data_convertion_status || !cmdline) {
3015 if (cmdline)
3016 wfree(cmdline);
3017 if (state)
3018 wfree(state);
3019 return 0;
3022 wtokensplit(cmdline, &argv, &argc);
3024 if (!argc) {
3025 if (cmdline)
3026 wfree(cmdline);
3027 if (state)
3028 wfree(state);
3029 return 0;
3032 if ((pid = fork()) == 0) {
3033 char **args;
3034 int i;
3036 SetupEnvironment(scr);
3038 #ifdef HAVE_SETSID
3039 setsid();
3040 #endif
3042 args = malloc(sizeof(char *) * (argc + 1));
3043 if (!args)
3044 exit(111);
3046 for (i = 0; i < argc; i++)
3047 args[i] = argv[i];
3049 args[argc] = NULL;
3050 execvp(argv[0], args);
3051 exit(111);
3053 wtokenfree(argv, argc);
3055 if (pid > 0) {
3056 if (!state) {
3057 state = wmalloc(sizeof(WSavedState));
3058 state->hidden = -1;
3059 state->miniaturized = -1;
3060 state->shaded = -1;
3061 if (btn->dock == scr->dock || btn->dock->type == WM_DRAWER || btn->omnipresent)
3062 state->workspace = -1;
3063 else
3064 state->workspace = w_global.workspace.current;
3066 wWindowAddSavedState(btn->wm_instance, btn->wm_class, cmdline, pid, state);
3067 wAddDeathHandler(pid, (WDeathHandler *) trackDeadProcess, btn->dock);
3068 } else if (state) {
3069 wfree(state);
3071 wfree(cmdline);
3072 return pid;
3075 void wDockHideIcons(WDock *dock)
3077 int i;
3079 if (dock == NULL)
3080 return;
3082 for (i = 1; i < dock->max_icons; i++) {
3083 if (dock->icon_array[i])
3084 XUnmapWindow(dpy, dock->icon_array[i]->icon->core->window);
3086 dock->mapped = 0;
3088 dockIconPaint(dock->icon_array[0]);
3091 void wDockShowIcons(WDock *dock)
3093 int i;
3094 WAppIcon *btn;
3096 if (dock == NULL)
3097 return;
3099 btn = dock->icon_array[0];
3100 moveDock(dock, btn->x_pos, btn->y_pos);
3102 /* Deleting any change in stacking level, this function is now only about
3103 mapping icons */
3105 if (!dock->collapsed) {
3106 for (i = 1; i < dock->max_icons; i++) {
3107 if (dock->icon_array[i])
3108 XMapWindow(dpy, dock->icon_array[i]->icon->core->window);
3111 dock->mapped = 1;
3113 dockIconPaint(btn);
3116 void wDockLower(WDock *dock)
3118 int i;
3119 WDrawerChain *dc;
3121 if (dock->type == WM_DOCK) {
3122 for (dc = dock->screen_ptr->drawers; dc != NULL; dc = dc->next)
3123 wDockLower(dc->adrawer);
3125 for (i = 0; i < dock->max_icons; i++) {
3126 if (dock->icon_array[i])
3127 wLowerFrame(dock->icon_array[i]->icon->core);
3131 void wDockRaise(WDock *dock)
3133 int i;
3134 WDrawerChain *dc;
3136 for (i = dock->max_icons - 1; i >= 0; i--) {
3137 if (dock->icon_array[i])
3138 wRaiseFrame(dock->icon_array[i]->icon->core);
3140 if (dock->type == WM_DOCK) {
3141 for (dc = dock->screen_ptr->drawers; dc != NULL; dc = dc->next)
3142 wDockRaise(dc->adrawer);
3146 void wDockRaiseLower(WDock *dock)
3148 if (!dock->icon_array[0]->icon->core->stacking->above
3149 || (dock->icon_array[0]->icon->core->stacking->window_level
3150 != dock->icon_array[0]->icon->core->stacking->above->stacking->window_level))
3151 wDockLower(dock);
3152 else
3153 wDockRaise(dock);
3156 void wDockFinishLaunch(WDock *dock, WAppIcon *icon)
3158 icon->launching = 0;
3159 icon->relaunching = 0;
3160 dockIconPaint(icon);
3163 WAppIcon *wDockFindIconForWindow(WDock *dock, Window window)
3165 WAppIcon *icon;
3166 int i;
3168 for (i = 0; i < dock->max_icons; i++) {
3169 icon = dock->icon_array[i];
3170 if (icon && icon->main_window == window)
3171 return icon;
3173 return NULL;
3176 void wDockTrackWindowLaunch(WDock *dock, Window window)
3178 WAppIcon *icon;
3179 char *wm_class, *wm_instance;
3180 int i;
3181 Bool firstPass = True;
3182 Bool found = False;
3183 char *command = NULL;
3185 if (!PropGetWMClass(window, &wm_class, &wm_instance) || (!wm_class && !wm_instance))
3186 return;
3188 command = GetCommandForWindow(window);
3189 retry:
3190 for (i = 0; i < dock->max_icons; i++) {
3191 icon = dock->icon_array[i];
3192 if (!icon)
3193 continue;
3195 /* app is already attached to icon */
3196 if (icon->main_window == window) {
3197 found = True;
3198 break;
3201 if ((icon->wm_instance || icon->wm_class)
3202 && (icon->launching || !icon->running)) {
3204 if (icon->wm_instance && wm_instance && strcmp(icon->wm_instance, wm_instance) != 0)
3205 continue;
3207 if (icon->wm_class && wm_class && strcmp(icon->wm_class, wm_class) != 0)
3208 continue;
3210 if (firstPass && command && strcmp(icon->command, command) != 0)
3211 continue;
3213 if (!icon->relaunching) {
3214 WApplication *wapp;
3216 /* Possibly an application that was docked with dockit,
3217 * but the user did not update WMState to indicate that
3218 * it was docked by force */
3219 wapp = wApplicationOf(window);
3220 if (!wapp) {
3221 icon->forced_dock = 1;
3222 icon->running = 0;
3224 if (!icon->forced_dock)
3225 icon->main_window = window;
3227 found = True;
3228 if (!wPreferences.no_animations && !icon->launching &&
3229 !dock->screen_ptr->flags.startup && !dock->collapsed) {
3230 WAppIcon *aicon;
3231 int x0, y0;
3233 icon->launching = 1;
3234 dockIconPaint(icon);
3236 aicon = wAppIconCreateForDock(dock->screen_ptr, NULL,
3237 wm_instance, wm_class, TILE_NORMAL);
3238 /* XXX: can: aicon->icon == NULL ? */
3239 PlaceIcon(dock->screen_ptr, &x0, &y0, wGetHeadForWindow(aicon->icon->owner));
3240 wAppIconMove(aicon, x0, y0);
3241 /* Should this always be lowered? -Dan */
3242 if (dock->lowered)
3243 wLowerFrame(aicon->icon->core);
3244 XMapWindow(dpy, aicon->icon->core->window);
3245 aicon->launching = 1;
3246 wAppIconPaint(aicon);
3247 SlideWindow(aicon->icon->core->window, x0, y0, icon->x_pos, icon->y_pos);
3248 XUnmapWindow(dpy, aicon->icon->core->window);
3249 wAppIconDestroy(aicon);
3251 wDockFinishLaunch(dock, icon);
3252 break;
3256 if (firstPass && !found) {
3257 firstPass = False;
3258 goto retry;
3261 if (command)
3262 wfree(command);
3264 if (wm_class)
3265 free(wm_class);
3266 if (wm_instance)
3267 free(wm_instance);
3270 void wClipUpdateForWorkspaceChange(WScreen *scr, int workspace)
3272 if (!wPreferences.flags.noclip) {
3273 w_global.clip.icon->dock = w_global.workspace.array[workspace]->clip;
3274 if (w_global.workspace.current != workspace) {
3275 WDock *old_clip = w_global.workspace.array[w_global.workspace.current]->clip;
3276 WAppIconChain *chain = scr->global_icons;
3278 while (chain) {
3279 wDockMoveIconBetweenDocks(chain->aicon->dock,
3280 w_global.workspace.array[workspace]->clip,
3281 chain->aicon, chain->aicon->xindex, chain->aicon->yindex);
3282 if (w_global.workspace.array[workspace]->clip->collapsed)
3283 XUnmapWindow(dpy, chain->aicon->icon->core->window);
3284 chain = chain->next;
3287 wDockHideIcons(old_clip);
3288 if (old_clip->auto_raise_lower) {
3289 if (old_clip->auto_raise_magic) {
3290 WMDeleteTimerHandler(old_clip->auto_raise_magic);
3291 old_clip->auto_raise_magic = NULL;
3293 wDockLower(old_clip);
3295 if (old_clip->auto_collapse) {
3296 if (old_clip->auto_expand_magic) {
3297 WMDeleteTimerHandler(old_clip->auto_expand_magic);
3298 old_clip->auto_expand_magic = NULL;
3300 old_clip->collapsed = 1;
3302 wDockShowIcons(w_global.workspace.array[workspace]->clip);
3307 static void trackDeadProcess(pid_t pid, unsigned char status, WDock *dock)
3309 WAppIcon *icon;
3310 int i;
3312 for (i = 0; i < dock->max_icons; i++) {
3313 icon = dock->icon_array[i];
3314 if (!icon)
3315 continue;
3317 if (icon->launching && icon->pid == pid) {
3318 if (!icon->relaunching) {
3319 icon->running = 0;
3320 icon->main_window = None;
3322 wDockFinishLaunch(dock, icon);
3323 icon->pid = 0;
3324 if (status == 111) {
3325 char msg[PATH_MAX];
3326 char *cmd;
3328 #ifdef XDND
3329 if (icon->drop_launch)
3330 cmd = icon->dnd_command;
3331 else
3332 #endif
3333 if (icon->paste_launch)
3334 cmd = icon->paste_command;
3335 else
3336 cmd = icon->command;
3338 snprintf(msg, sizeof(msg), _("Could not execute command \"%s\""), cmd);
3340 wMessageDialog(dock->screen_ptr, _("Error"), msg, _("OK"), NULL, NULL);
3342 break;
3347 /* This function is called when the dock switches state between
3348 * "normal" (including auto-raise/lower) and "keep on top". It is
3349 * therefore clearly distinct from wDockLower/Raise, which are called
3350 * each time a not-kept-on-top dock is lowered/raised. */
3351 static void toggleLowered(WDock *dock)
3353 WAppIcon *tmp;
3354 WDrawerChain *dc;
3355 int newlevel, i;
3357 if (!dock->lowered) {
3358 newlevel = WMNormalLevel;
3359 dock->lowered = 1;
3360 } else {
3361 newlevel = WMDockLevel;
3362 dock->lowered = 0;
3365 for (i = 0; i < dock->max_icons; i++) {
3366 tmp = dock->icon_array[i];
3367 if (!tmp)
3368 continue;
3370 ChangeStackingLevel(tmp->icon->core, newlevel);
3372 /* When the dock is no longer "on top", explicitly lower it as well.
3373 * It saves some CPU cycles (probably) to do it ourselves here
3374 * rather than calling wDockLower at the end of toggleLowered */
3375 if (dock->lowered)
3376 wLowerFrame(tmp->icon->core);
3379 if (dock->type == WM_DOCK) {
3380 for (dc = dock->screen_ptr->drawers; dc != NULL; dc = dc->next) {
3381 toggleLowered(dc->adrawer);
3383 wScreenUpdateUsableArea(dock->screen_ptr);
3387 static void toggleCollapsed(WDock *dock)
3389 if (dock->collapsed) {
3390 dock->collapsed = 0;
3391 wDockShowIcons(dock);
3392 } else {
3393 dock->collapsed = 1;
3394 wDockHideIcons(dock);
3398 static void openDockMenu(WDock *dock, WAppIcon *aicon, XEvent *event)
3400 WScreen *scr = dock->screen_ptr;
3401 WObjDescriptor *desc;
3402 WMenuEntry *entry;
3403 WApplication *wapp = NULL;
3404 int index = 0;
3405 int x_pos;
3406 int n_selected;
3407 int appIsRunning = aicon->running && aicon->icon && aicon->icon->owner;
3409 if (dock->type == WM_DOCK) {
3410 /* Dock position menu */
3411 updateDockPositionMenu(w_global.dock.pos_menu, dock);
3412 dock->menu->flags.realized = 0;
3413 if (!wPreferences.flags.nodrawer) {
3414 /* add a drawer */
3415 entry = dock->menu->entries[++index];
3416 entry->clientdata = aicon;
3417 wMenuSetEnabled(dock->menu, index, True);
3419 } else {
3420 /* clip/drawer options */
3421 if (w_global.clip.opt_menu)
3422 updateClipOptionsMenu(w_global.clip.opt_menu, dock);
3424 n_selected = numberOfSelectedIcons(dock);
3426 if (dock->type == WM_CLIP) {
3427 /* Rename Workspace */
3428 entry = dock->menu->entries[++index];
3429 if (aicon == w_global.clip.icon) {
3430 entry->callback = renameCallback;
3431 entry->clientdata = dock;
3432 entry->flags.indicator = 0;
3433 entry->text = _("Rename Workspace");
3434 } else {
3435 entry->callback = omnipresentCallback;
3436 entry->clientdata = aicon;
3437 if (n_selected > 0) {
3438 entry->flags.indicator = 0;
3439 entry->text = _("Toggle Omnipresent");
3440 } else {
3441 entry->flags.indicator = 1;
3442 entry->flags.indicator_on = aicon->omnipresent;
3443 entry->flags.indicator_type = MI_CHECK;
3444 entry->text = _("Omnipresent");
3449 /* select/unselect icon */
3450 entry = dock->menu->entries[++index];
3451 entry->clientdata = aicon;
3452 entry->flags.indicator_on = aicon->icon->selected;
3453 wMenuSetEnabled(dock->menu, index, aicon != w_global.clip.icon && !wIsADrawer(aicon));
3455 /* select/unselect all icons */
3456 entry = dock->menu->entries[++index];
3457 entry->clientdata = aicon;
3458 if (n_selected > 0)
3459 entry->text = _("Unselect All Icons");
3460 else
3461 entry->text = _("Select All Icons");
3463 wMenuSetEnabled(dock->menu, index, dock->icon_count > 1);
3465 /* keep icon(s) */
3466 entry = dock->menu->entries[++index];
3467 entry->clientdata = aicon;
3468 if (n_selected > 1)
3469 entry->text = _("Keep Icons");
3470 else
3471 entry->text = _("Keep Icon");
3473 wMenuSetEnabled(dock->menu, index, dock->icon_count > 1);
3475 if (dock->type == WM_CLIP) {
3476 /* this is the workspace submenu part */
3477 entry = dock->menu->entries[++index];
3478 if (n_selected > 1)
3479 entry->text = _("Move Icons To");
3480 else
3481 entry->text = _("Move Icon To");
3483 if (w_global.clip.submenu)
3484 updateWorkspaceMenu(w_global.clip.submenu, aicon);
3486 wMenuSetEnabled(dock->menu, index, !aicon->omnipresent);
3489 /* remove icon(s) */
3490 entry = dock->menu->entries[++index];
3491 entry->clientdata = aicon;
3492 if (n_selected > 1)
3493 entry->text = _("Remove Icons");
3494 else
3495 entry->text = _("Remove Icon");
3497 wMenuSetEnabled(dock->menu, index, dock->icon_count > 1);
3499 /* attract icon(s) */
3500 entry = dock->menu->entries[++index];
3501 entry->clientdata = aicon;
3503 dock->menu->flags.realized = 0;
3504 wMenuRealize(dock->menu);
3507 if (aicon->icon->owner)
3508 wapp = wApplicationOf(aicon->icon->owner->main_window);
3509 else
3510 wapp = NULL;
3512 /* launch */
3513 entry = dock->menu->entries[++index];
3514 entry->clientdata = aicon;
3515 wMenuSetEnabled(dock->menu, index, aicon->command != NULL);
3517 /* unhide here */
3518 entry = dock->menu->entries[++index];
3519 entry->clientdata = aicon;
3520 if (wapp && wapp->flags.hidden)
3521 entry->text = _("Unhide Here");
3522 else
3523 entry->text = _("Bring Here");
3525 wMenuSetEnabled(dock->menu, index, appIsRunning);
3527 /* hide */
3528 entry = dock->menu->entries[++index];
3529 entry->clientdata = aicon;
3530 if (wapp && wapp->flags.hidden)
3531 entry->text = _("Unhide");
3532 else
3533 entry->text = _("Hide");
3535 wMenuSetEnabled(dock->menu, index, appIsRunning);
3537 /* settings */
3538 entry = dock->menu->entries[++index];
3539 entry->clientdata = aicon;
3540 wMenuSetEnabled(dock->menu, index, !aicon->editing && !wPreferences.flags.noupdates);
3542 /* kill or remove drawer */
3543 entry = dock->menu->entries[++index];
3544 entry->clientdata = aicon;
3545 if (wIsADrawer(aicon)) {
3546 entry->callback = removeDrawerCallback;
3547 entry->text = _("Remove drawer");
3548 wMenuSetEnabled(dock->menu, index, True);
3549 } else {
3550 entry->callback = killCallback;
3551 entry->text = _("Kill");
3552 wMenuSetEnabled(dock->menu, index, appIsRunning);
3555 if (!dock->menu->flags.realized)
3556 wMenuRealize(dock->menu);
3558 if (dock->type == WM_CLIP || dock->type == WM_DRAWER) {
3559 /*x_pos = event->xbutton.x_root+2; */
3560 x_pos = event->xbutton.x_root - dock->menu->frame->core->width / 2 - 1;
3561 if (x_pos < 0) {
3562 x_pos = 0;
3563 } else if (x_pos + dock->menu->frame->core->width > scr->scr_width - 2) {
3564 x_pos = scr->scr_width - dock->menu->frame->core->width - 4;
3566 } else {
3567 x_pos = dock->on_right_side ? scr->scr_width - dock->menu->frame->core->width - 3 : 0;
3570 wMenuMapAt(dock->menu, x_pos, event->xbutton.y_root + 2, False);
3572 /* allow drag select */
3573 event->xany.send_event = True;
3574 desc = &dock->menu->menu->descriptor;
3575 (*desc->handle_mousedown) (desc, event);
3578 /******************************************************************/
3579 static void iconDblClick(WObjDescriptor *desc, XEvent *event)
3581 WAppIcon *btn = desc->parent;
3582 WDock *dock = btn->dock;
3583 WApplication *wapp = NULL;
3584 int unhideHere = 0;
3586 if (btn->icon->owner && !(event->xbutton.state & ControlMask)) {
3587 wapp = wApplicationOf(btn->icon->owner->main_window);
3589 assert(wapp != NULL);
3591 unhideHere = (event->xbutton.state & ShiftMask);
3593 /* go to the last workspace that the user worked on the app */
3594 if (wapp->last_workspace != w_global.workspace.current && !unhideHere)
3595 wWorkspaceChange(dock->screen_ptr, wapp->last_workspace);
3597 wUnhideApplication(wapp, event->xbutton.button == Button2, unhideHere);
3599 if (event->xbutton.state & MOD_MASK)
3600 wHideOtherApplications(btn->icon->owner);
3601 } else {
3602 if (event->xbutton.button == Button1) {
3603 if (event->xbutton.state & MOD_MASK) {
3604 /* raise/lower dock */
3605 toggleLowered(dock);
3606 } else if (btn == w_global.clip.icon) {
3607 if (getClipButton(event->xbutton.x, event->xbutton.y) != CLIP_IDLE)
3608 handleClipChangeWorkspace(dock->screen_ptr, event);
3609 else if (wPreferences.flags.clip_merged_in_dock) {
3610 // Is actually the dock
3611 if (btn->command)
3613 if (!btn->launching && (!btn->running || (event->xbutton.state & ControlMask)))
3614 launchDockedApplication(btn, False);
3616 else
3618 wShowInfoPanel(dock->screen_ptr);
3621 else
3622 toggleCollapsed(dock);
3623 } else if (wIsADrawer(btn)) {
3624 toggleCollapsed(dock);
3625 } else if (btn->command) {
3626 if (!btn->launching && (!btn->running || (event->xbutton.state & ControlMask)))
3627 launchDockedApplication(btn, False);
3628 } else if (btn->xindex == 0 && btn->yindex == 0 && btn->dock->type == WM_DOCK) {
3629 wShowInfoPanel(dock->screen_ptr);
3635 static void handleDockMove(WDock *dock, WAppIcon *aicon, XEvent *event)
3637 WScreen *scr = dock->screen_ptr;
3638 int ofs_x = event->xbutton.x, ofs_y = event->xbutton.y;
3639 WIcon *icon = aicon->icon;
3640 WAppIcon *tmpaicon;
3641 WDrawerChain *dc;
3642 int x = aicon->x_pos, y = aicon->y_pos;;
3643 int shad_x = x, shad_y = y;
3644 XEvent ev;
3645 int grabbed = 0, done, previously_on_right, now_on_right, previous_x_pos, i;
3646 Pixmap ghost = None;
3647 int superfluous = wPreferences.superfluous; /* we catch it to avoid problems */
3649 if (XGrabPointer(dpy, aicon->icon->core->window, True, ButtonMotionMask
3650 | ButtonReleaseMask | ButtonPressMask, GrabModeAsync,
3651 GrabModeAsync, None, None, CurrentTime) != GrabSuccess)
3652 wwarning("pointer grab failed for dock move");
3654 if (dock->type == WM_DRAWER) {
3655 Window wins[2];
3656 wins[0] = icon->core->window;
3657 wins[1] = scr->dock_shadow;
3658 XRestackWindows(dpy, wins, 2);
3659 XMoveResizeWindow(dpy, scr->dock_shadow, aicon->x_pos, aicon->y_pos,
3660 ICON_SIZE, ICON_SIZE);
3661 if (superfluous) {
3662 if (icon->pixmap!=None)
3663 ghost = MakeGhostIcon(scr, icon->pixmap);
3664 else
3665 ghost = MakeGhostIcon(scr, icon->core->window);
3667 XSetWindowBackgroundPixmap(dpy, scr->dock_shadow, ghost);
3668 XClearWindow(dpy, scr->dock_shadow);
3670 XMapWindow(dpy, scr->dock_shadow);
3673 previously_on_right = now_on_right = dock->on_right_side;
3674 previous_x_pos = dock->x_pos;
3675 done = 0;
3676 while (!done) {
3677 WMMaskEvent(dpy, PointerMotionMask | ButtonReleaseMask | ButtonPressMask
3678 | ButtonMotionMask | ExposureMask | EnterWindowMask, &ev);
3679 switch (ev.type) {
3680 case Expose:
3681 WMHandleEvent(&ev);
3682 break;
3684 case EnterNotify:
3685 /* It means the cursor moved so fast that it entered
3686 * something else (if moving slowly, it would have
3687 * stayed in the dock that is being moved. Ignore such
3688 * "spurious" EnterNotifiy's */
3689 break;
3691 case MotionNotify:
3692 if (!grabbed) {
3693 if (abs(ofs_x - ev.xmotion.x) >= MOVE_THRESHOLD
3694 || abs(ofs_y - ev.xmotion.y) >= MOVE_THRESHOLD) {
3695 XChangeActivePointerGrab(dpy, ButtonMotionMask
3696 | ButtonReleaseMask | ButtonPressMask,
3697 wPreferences.cursor[WCUR_MOVE], CurrentTime);
3698 grabbed = 1;
3700 break;
3702 switch (dock->type) {
3703 case WM_CLIP:
3704 x = ev.xmotion.x_root - ofs_x;
3705 y = ev.xmotion.y_root - ofs_y;
3706 wScreenKeepInside(scr, &x, &y, ICON_SIZE, ICON_SIZE);
3707 moveDock(dock, x, y);
3708 break;
3709 case WM_DOCK:
3710 x = ev.xmotion.x_root - ofs_x;
3711 y = ev.xmotion.y_root - ofs_y;
3712 if (previously_on_right)
3714 now_on_right = (ev.xmotion.x_root >= previous_x_pos - ICON_SIZE);
3716 else
3718 now_on_right = (ev.xmotion.x_root > previous_x_pos + ICON_SIZE * 2);
3720 if (now_on_right != dock->on_right_side)
3722 dock->on_right_side = now_on_right;
3723 swapDock(dock);
3724 wArrangeIcons(scr, False);
3726 // Also perform the vertical move
3727 wScreenKeepInside(scr, &x, &y, ICON_SIZE, ICON_SIZE);
3728 moveDock(dock, dock->x_pos, y);
3729 if (wPreferences.flags.wrap_appicons_in_dock)
3731 for (i = 0; i < dock->max_icons; i++) {
3732 int new_y, new_index, j, ok;
3733 if ((tmpaicon = dock->icon_array[i]) == NULL)
3734 continue;
3735 if (onScreen(scr, tmpaicon->x_pos, tmpaicon->y_pos))
3736 continue;
3737 new_y = (tmpaicon->y_pos + ICON_SIZE * dock->max_icons) % (ICON_SIZE * dock->max_icons);
3738 new_index = (new_y - dock->y_pos) / ICON_SIZE;
3739 if (!onScreen(scr, tmpaicon->x_pos, new_y))
3740 continue;
3741 ok = 1;
3742 for (j = 0; j < dock->max_icons; j++)
3744 if (dock->icon_array[j] != NULL &&
3745 dock->icon_array[j]->yindex == new_index)
3747 ok = 0;
3748 break;
3751 if (!ok || getDrawer(scr, new_index) != NULL)
3752 continue;
3753 wDockReattachIcon(dock, tmpaicon, tmpaicon->xindex, new_index);
3755 for (dc = scr->drawers; dc != NULL; dc = dc->next)
3757 int new_y, new_index, j, ok;
3758 tmpaicon = dc->adrawer->icon_array[0];
3759 if (onScreen(scr, tmpaicon->x_pos, tmpaicon->y_pos))
3760 continue;
3761 new_y = (tmpaicon->y_pos + ICON_SIZE * dock->max_icons) % (ICON_SIZE * dock->max_icons);
3762 new_index = (new_y - dock->y_pos) / ICON_SIZE;
3763 if (!onScreen(scr, tmpaicon->x_pos, new_y))
3764 continue;
3765 ok = 1;
3766 for (j = 0; j < dock->max_icons; j++)
3768 if (dock->icon_array[j] != NULL &&
3769 dock->icon_array[j]->yindex == new_index)
3771 ok = 0;
3772 break;
3775 if (!ok || getDrawer(scr, new_index) != NULL)
3776 continue;
3777 moveDock(dc->adrawer, tmpaicon->x_pos, new_y);
3780 break;
3781 case WM_DRAWER:
3783 WDock *real_dock = dock->screen_ptr->dock;
3784 Bool snapped;
3785 int ix, iy;
3786 x = ev.xmotion.x_root - ofs_x;
3787 y = ev.xmotion.y_root - ofs_y;
3788 snapped = wDockSnapIcon(real_dock, aicon, x, y, &ix, &iy, True);
3789 if (snapped) {
3790 shad_x = real_dock->x_pos + ix * wPreferences.icon_size;
3791 shad_y = real_dock->y_pos + iy * wPreferences.icon_size;
3792 XMoveWindow(dpy, scr->dock_shadow, shad_x, shad_y);
3794 moveDock(dock, x, y);
3795 break;
3798 break;
3800 case ButtonPress:
3801 break;
3803 case ButtonRelease:
3804 if (ev.xbutton.button != event->xbutton.button)
3805 break;
3806 XUngrabPointer(dpy, CurrentTime);
3807 if (dock->type == WM_DRAWER) {
3808 Window *wins[dock->icon_count];
3810 for (i = 0; i < dock->max_icons; i++) {
3811 if ((tmpaicon = dock->icon_array[i]) == NULL)
3812 continue;
3813 wins[ tmpaicon->xindex + (dock->on_right_side ? dock->icon_count - 1 : 0) ] = &tmpaicon->icon->core->window;
3815 SlideWindows(wins, dock->icon_count,
3816 (dock->on_right_side ? x - (dock->icon_count - 1) * ICON_SIZE : x),
3818 (dock->on_right_side ? shad_x - (dock->icon_count - 1) * ICON_SIZE : shad_x),
3819 shad_y);
3820 XUnmapWindow(dpy, scr->dock_shadow);
3821 moveDock(dock, shad_x, shad_y);
3822 XResizeWindow(dpy, scr->dock_shadow, ICON_SIZE, ICON_SIZE);
3824 done = 1;
3825 break;
3828 if (superfluous) {
3829 if (ghost != None)
3830 XFreePixmap(dpy, ghost);
3831 XSetWindowBackground(dpy, scr->dock_shadow, scr->white_pixel);
3836 static int getClipButton(int px, int py)
3838 int pt = (CLIP_BUTTON_SIZE + 2) * ICON_SIZE / 64;
3840 if (px < 0 || py < 0 || px >= ICON_SIZE || py >= ICON_SIZE)
3841 return CLIP_IDLE;
3843 if (py <= pt - ((int)ICON_SIZE - 1 - px))
3844 return CLIP_FORWARD;
3845 else if (px <= pt - ((int)ICON_SIZE - 1 - py))
3846 return CLIP_REWIND;
3848 return CLIP_IDLE;
3851 static void handleClipChangeWorkspace(WScreen *scr, XEvent *event)
3853 XEvent ev;
3854 int done, direction, new_ws;
3855 int new_dir;
3856 WDock *clip = w_global.clip.icon->dock;
3858 direction = getClipButton(event->xbutton.x, event->xbutton.y);
3860 clip->lclip_button_pushed = direction == CLIP_REWIND;
3861 clip->rclip_button_pushed = direction == CLIP_FORWARD;
3863 wClipIconPaint();
3864 done = 0;
3865 while (!done) {
3866 WMMaskEvent(dpy, ExposureMask | ButtonMotionMask | ButtonReleaseMask | ButtonPressMask, &ev);
3867 switch (ev.type) {
3868 case Expose:
3869 WMHandleEvent(&ev);
3870 break;
3872 case MotionNotify:
3873 new_dir = getClipButton(ev.xmotion.x, ev.xmotion.y);
3874 if (new_dir != direction) {
3875 direction = new_dir;
3876 clip->lclip_button_pushed = direction == CLIP_REWIND;
3877 clip->rclip_button_pushed = direction == CLIP_FORWARD;
3878 wClipIconPaint();
3880 break;
3882 case ButtonPress:
3883 break;
3885 case ButtonRelease:
3886 if (ev.xbutton.button == event->xbutton.button)
3887 done = 1;
3891 clip->lclip_button_pushed = 0;
3892 clip->rclip_button_pushed = 0;
3894 new_ws = wPreferences.ws_advance || (event->xbutton.state & ControlMask);
3896 if (direction == CLIP_FORWARD) {
3897 if (w_global.workspace.current < w_global.workspace.count - 1)
3898 wWorkspaceChange(scr, w_global.workspace.current + 1);
3899 else if (new_ws && w_global.workspace.current < MAX_WORKSPACES - 1)
3900 wWorkspaceChange(scr, w_global.workspace.current + 1);
3901 else if (wPreferences.ws_cycle)
3902 wWorkspaceChange(scr, 0);
3903 } else if (direction == CLIP_REWIND) {
3904 if (w_global.workspace.current > 0)
3905 wWorkspaceChange(scr, w_global.workspace.current - 1);
3906 else if (w_global.workspace.current == 0 && wPreferences.ws_cycle)
3907 wWorkspaceChange(scr, w_global.workspace.count - 1);
3910 wClipIconPaint();
3913 static void iconMouseDown(WObjDescriptor *desc, XEvent *event)
3915 WAppIcon *aicon = desc->parent;
3916 WDock *dock = aicon->dock;
3917 WScreen *scr = aicon->icon->core->screen_ptr;
3919 if (aicon->editing || WCHECK_STATE(WSTATE_MODAL))
3920 return;
3922 scr->last_dock = dock;
3924 if (dock->menu->flags.mapped)
3925 wMenuUnmap(dock->menu);
3927 if (IsDoubleClick(scr, event)) {
3928 /* double-click was not in the main clip icon */
3929 if (dock->type != WM_CLIP || aicon->xindex != 0 || aicon->yindex != 0
3930 || getClipButton(event->xbutton.x, event->xbutton.y) == CLIP_IDLE) {
3931 iconDblClick(desc, event);
3932 return;
3936 if (event->xbutton.button == Button1) {
3937 if (event->xbutton.state & MOD_MASK)
3938 wDockLower(dock);
3939 else
3940 wDockRaise(dock);
3942 if ((event->xbutton.state & ShiftMask) && aicon != w_global.clip.icon && dock->type != WM_DOCK) {
3943 wIconSelect(aicon->icon);
3944 return;
3947 if (aicon->yindex == 0 && aicon->xindex == 0) {
3948 if (getClipButton(event->xbutton.x, event->xbutton.y) != CLIP_IDLE &&
3949 (dock->type == WM_CLIP || (dock->type == WM_DOCK && wPreferences.flags.clip_merged_in_dock)))
3950 handleClipChangeWorkspace(scr, event);
3951 else
3952 handleDockMove(dock, aicon, event);
3953 } else {
3954 Bool hasMoved = wHandleAppIconMove(aicon, event);
3955 if (wPreferences.single_click && !hasMoved)
3956 iconDblClick(desc, event);
3958 } else if (event->xbutton.button == Button2 && aicon == w_global.clip.icon) {
3959 if (!w_global.clip.ws_menu)
3960 w_global.clip.ws_menu = wWorkspaceMenuMake(scr, False);
3962 if (w_global.clip.ws_menu) {
3963 WMenu *wsMenu = w_global.clip.ws_menu;
3964 int xpos;
3966 wWorkspaceMenuUpdate(wsMenu);
3968 xpos = event->xbutton.x_root - wsMenu->frame->core->width / 2 - 1;
3969 if (xpos < 0) {
3970 xpos = 0;
3971 } else if (xpos + wsMenu->frame->core->width > scr->scr_width - 2) {
3972 xpos = scr->scr_width - wsMenu->frame->core->width - 4;
3974 wMenuMapAt(wsMenu, xpos, event->xbutton.y_root + 2, False);
3976 desc = &wsMenu->menu->descriptor;
3977 event->xany.send_event = True;
3978 (*desc->handle_mousedown) (desc, event);
3980 } else if (event->xbutton.button == Button2 && dock->type == WM_CLIP &&
3981 (event->xbutton.state & ShiftMask) && aicon != w_global.clip.icon) {
3982 wClipMakeIconOmnipresent(aicon, !aicon->omnipresent);
3983 } else if (event->xbutton.button == Button3) {
3984 if (event->xbutton.send_event &&
3985 XGrabPointer(dpy, aicon->icon->core->window, True, ButtonMotionMask
3986 | ButtonReleaseMask | ButtonPressMask, GrabModeAsync,
3987 GrabModeAsync, None, None, CurrentTime) != GrabSuccess) {
3988 wwarning("pointer grab failed for dockicon menu");
3989 return;
3992 openDockMenu(dock, aicon, event);
3993 } else if (event->xbutton.button == Button2) {
3994 WAppIcon *btn = desc->parent;
3996 if (!btn->launching && (!btn->running || (event->xbutton.state & ControlMask)))
3997 launchDockedApplication(btn, True);
4001 static void clipEnterNotify(WObjDescriptor *desc, XEvent *event)
4003 WAppIcon *btn = (WAppIcon *) desc->parent;
4004 WDock *dock, *tmp;
4005 WScreen *scr;
4007 assert(event->type == EnterNotify);
4009 if (desc->parent_type != WCLASS_DOCK_ICON)
4010 return;
4012 scr = btn->icon->core->screen_ptr;
4013 dock = btn->dock;
4015 if (dock == NULL)
4016 return;
4018 /* The auto raise/lower code */
4019 tmp = (dock->type == WM_DRAWER ? scr->dock : dock);
4020 if (tmp->auto_lower_magic) {
4021 WMDeleteTimerHandler(tmp->auto_lower_magic);
4022 tmp->auto_lower_magic = NULL;
4024 if (tmp->auto_raise_lower && !tmp->auto_raise_magic)
4025 tmp->auto_raise_magic = WMAddTimerHandler(wPreferences.clip_auto_raise_delay, clipAutoRaise, (void *) tmp);
4027 if (dock->type != WM_CLIP && dock->type != WM_DRAWER)
4028 return;
4030 /* The auto expand/collapse code */
4031 if (dock->auto_collapse_magic) {
4032 WMDeleteTimerHandler(dock->auto_collapse_magic);
4033 dock->auto_collapse_magic = NULL;
4035 if (dock->auto_collapse && !dock->auto_expand_magic)
4036 dock->auto_expand_magic = WMAddTimerHandler(wPreferences.clip_auto_expand_delay, clipAutoExpand, (void *)dock);
4039 static void clipLeave(WDock *dock)
4041 XEvent event;
4042 WObjDescriptor *desc = NULL;
4043 WDock *tmp;
4045 if (dock == NULL)
4046 return;
4048 if (XCheckTypedEvent(dpy, EnterNotify, &event) != False) {
4049 if (XFindContext(dpy, event.xcrossing.window, w_global.context.client_win,
4050 (XPointer *) & desc) != XCNOENT
4051 && desc && desc->parent_type == WCLASS_DOCK_ICON
4052 && ((WAppIcon *) desc->parent)->dock == dock) {
4053 /* We haven't left the dock/clip/drawer yet */
4054 XPutBackEvent(dpy, &event);
4055 return;
4058 XPutBackEvent(dpy, &event);
4059 } else {
4060 /* We entered a withdrawn window, so we're still in Clip */
4061 return;
4064 tmp = (dock->type == WM_DRAWER ? dock->screen_ptr->dock : dock);
4065 if (tmp->auto_raise_magic) {
4066 WMDeleteTimerHandler(tmp->auto_raise_magic);
4067 tmp->auto_raise_magic = NULL;
4069 if (tmp->auto_raise_lower && !tmp->auto_lower_magic)
4070 tmp->auto_lower_magic = WMAddTimerHandler(wPreferences.clip_auto_lower_delay, clipAutoLower, (void *)tmp);
4072 if (dock->type != WM_CLIP && dock->type != WM_DRAWER)
4073 return;
4075 if (dock->auto_expand_magic) {
4076 WMDeleteTimerHandler(dock->auto_expand_magic);
4077 dock->auto_expand_magic = NULL;
4079 if (dock->auto_collapse && !dock->auto_collapse_magic)
4080 dock->auto_collapse_magic = WMAddTimerHandler(wPreferences.clip_auto_collapse_delay, clipAutoCollapse, (void *)dock);
4083 static void clipLeaveNotify(WObjDescriptor *desc, XEvent *event)
4085 WAppIcon *btn = (WAppIcon *) desc->parent;
4087 assert(event->type == LeaveNotify);
4089 if (desc->parent_type != WCLASS_DOCK_ICON)
4090 return;
4092 clipLeave(btn->dock);
4095 static void clipAutoCollapse(void *cdata)
4097 WDock *dock = (WDock *) cdata;
4099 if (dock->type != WM_CLIP && dock->type != WM_DRAWER)
4100 return;
4102 if (dock->auto_collapse) {
4103 dock->collapsed = 1;
4104 wDockHideIcons(dock);
4106 dock->auto_collapse_magic = NULL;
4109 static void clipAutoExpand(void *cdata)
4111 WDock *dock = (WDock *) cdata;
4113 if (dock->type != WM_CLIP && dock->type != WM_DRAWER)
4114 return;
4116 if (dock->auto_collapse) {
4117 dock->collapsed = 0;
4118 wDockShowIcons(dock);
4120 dock->auto_expand_magic = NULL;
4123 static void clipAutoLower(void *cdata)
4125 WDock *dock = (WDock *) cdata;
4127 if (dock->auto_raise_lower)
4128 wDockLower(dock);
4130 dock->auto_lower_magic = NULL;
4133 static void clipAutoRaise(void *cdata)
4135 WDock *dock = (WDock *) cdata;
4137 if (dock->auto_raise_lower)
4138 wDockRaise(dock);
4140 dock->auto_raise_magic = NULL;
4143 static Bool iconCanBeOmnipresent(WAppIcon *aicon)
4145 WScreen *scr = aicon->icon->core->screen_ptr;
4146 WDock *clip;
4147 WAppIcon *btn;
4148 int i, j;
4150 for (i = 0; i < w_global.workspace.count; i++) {
4151 clip = w_global.workspace.array[i]->clip;
4153 if (clip == aicon->dock)
4154 continue;
4156 if (clip->icon_count + scr->global_icon_count >= clip->max_icons)
4157 return False; /* Clip is full in some workspace */
4159 for (j = 0; j < clip->max_icons; j++) {
4160 btn = clip->icon_array[j];
4161 if (btn && btn->xindex == aicon->xindex && btn->yindex == aicon->yindex)
4162 return False;
4166 return True;
4169 int wClipMakeIconOmnipresent(WAppIcon *aicon, int omnipresent)
4171 WScreen *scr = aicon->icon->core->screen_ptr;
4172 WAppIconChain *new_entry, *tmp, *tmp1;
4173 int status = WO_SUCCESS;
4175 if ((scr->dock && aicon->dock == scr->dock) || aicon == w_global.clip.icon)
4176 return WO_NOT_APPLICABLE;
4178 if (aicon->omnipresent == omnipresent)
4179 return WO_SUCCESS;
4181 if (omnipresent) {
4182 if (iconCanBeOmnipresent(aicon)) {
4183 aicon->omnipresent = 1;
4184 new_entry = wmalloc(sizeof(WAppIconChain));
4185 new_entry->aicon = aicon;
4186 new_entry->next = scr->global_icons;
4187 scr->global_icons = new_entry;
4188 scr->global_icon_count++;
4189 } else {
4190 aicon->omnipresent = 0;
4191 status = WO_FAILED;
4193 } else {
4194 aicon->omnipresent = 0;
4195 if (aicon == scr->global_icons->aicon) {
4196 tmp = scr->global_icons->next;
4197 wfree(scr->global_icons);
4198 scr->global_icons = tmp;
4199 scr->global_icon_count--;
4200 } else {
4201 tmp = scr->global_icons;
4202 while (tmp->next) {
4203 if (tmp->next->aicon == aicon) {
4204 tmp1 = tmp->next->next;
4205 wfree(tmp->next);
4206 tmp->next = tmp1;
4207 scr->global_icon_count--;
4208 break;
4210 tmp = tmp->next;
4215 wAppIconPaint(aicon);
4217 return status;
4220 static void drawerAppendToChain(WScreen *scr, WDock *drawer)
4222 WDrawerChain **where_to_add;
4224 where_to_add = &scr->drawers;
4225 while ((*where_to_add) != NULL) {
4226 where_to_add = &(*where_to_add)->next;
4229 *where_to_add = wmalloc(sizeof(WDrawerChain));
4230 (*where_to_add)->adrawer = drawer;
4231 (*where_to_add)->next = NULL;
4232 scr->drawer_count++;
4236 static void drawerRemoveFromChain(WScreen *scr, WDock *drawer)
4238 WDrawerChain *next, **to_remove;
4240 to_remove = &scr->drawers;
4241 while (True) {
4242 if (*to_remove == NULL) {
4243 wwarning("The drawer to be removed can not be found.");
4244 return;
4246 if ((*to_remove)->adrawer == drawer)
4247 break;
4249 to_remove = &(*to_remove)->next;
4251 next = (*to_remove)->next;
4252 wfree(*to_remove);
4253 *to_remove = next;
4254 scr->drawer_count--;
4258 /* Don't free the returned string. Duplicate it. */
4259 static char * findUniqueName(WScreen *scr, const char *instance_basename)
4261 static char buffer[128];
4262 WDrawerChain *dc;
4263 int i;
4264 Bool already_in_use = True;
4266 #define UNIQUE_NAME_WATCHDOG 128
4267 for (i = 0; already_in_use && i < UNIQUE_NAME_WATCHDOG; i++) {
4268 snprintf(buffer, sizeof buffer, "%s%d", instance_basename, i);
4270 already_in_use = False;
4272 for (dc = scr->drawers; dc != NULL; dc = dc->next) {
4273 if (!strncmp(dc->adrawer->icon_array[0]->wm_instance, buffer,
4274 sizeof buffer)) {
4275 already_in_use = True;
4276 break;
4281 if (i == UNIQUE_NAME_WATCHDOG)
4282 wwarning("Couldn't find a unique name for drawer in %d attempts.", i);
4283 #undef UNIQUE_NAME_WATCHDOG
4285 return buffer;
4289 static void drawerIconExpose(WObjDescriptor *desc, XEvent *event)
4291 wDrawerIconPaint((WAppIcon *) desc->parent);
4295 static int addADrawer(WScreen *scr)
4297 int i, y, sig, found_y;
4298 WDock *drawer, *dock = scr->dock;
4299 WDrawerChain *dc;
4300 char can_be_here[2 * dock->max_icons - 1];
4302 if (dock->icon_count + scr->drawer_count >= dock->max_icons)
4303 return -1;
4305 for (y = -dock->max_icons + 1; y < dock->max_icons; y++) {
4306 can_be_here[y + dock->max_icons - 1] = True;
4308 for (i = 0; i < dock->max_icons; i++) {
4309 if (dock->icon_array[i] != NULL)
4310 can_be_here[dock->icon_array[i]->yindex + dock->max_icons - 1] = False;
4312 for (dc = scr->drawers; dc != NULL; dc = dc->next) {
4313 y = (int) ((dc->adrawer->y_pos - dock->y_pos) / ICON_SIZE);
4314 can_be_here[y + dock->max_icons - 1] = False;
4317 found_y = False;
4318 for (sig = 1; !found_y && sig > -2; sig -= 2) // 1, then -1
4320 for (y = sig; sig * y < dock->max_icons; y += sig)
4322 if (can_be_here[y + dock->max_icons - 1] &&
4323 onScreen(scr, dock->x_pos, dock->y_pos + y * ICON_SIZE))
4325 found_y = True;
4326 break;
4331 if (!found_y)
4332 /* This can happen even when dock->icon_count + scr->drawer_count
4333 * < dock->max_icons when the dock is not aligned on an
4334 * ICON_SIZE multiple, as some space is lost above and under it */
4335 return -1;
4337 drawer = wDockCreate(scr, WM_DRAWER, NULL);
4338 drawer->lowered = scr->dock->lowered;
4339 drawer->auto_raise_lower = scr->dock->auto_raise_lower;
4340 drawer->x_pos = dock->x_pos;
4341 drawer->y_pos = dock->y_pos + ICON_SIZE * y;
4342 drawer->icon_array[0]->xindex = 0;
4343 drawer->icon_array[0]->yindex = 0;
4344 drawer->icon_array[0]->x_pos = drawer->x_pos;
4345 drawer->icon_array[0]->y_pos = drawer->y_pos;
4346 XMoveWindow(dpy, drawer->icon_array[0]->icon->core->window,
4347 drawer->icon_array[0]->x_pos, drawer->icon_array[0]->y_pos);
4349 return 0;
4353 static void addADrawerCallback(WMenu *menu, WMenuEntry *entry)
4355 assert(entry->clientdata!=NULL);
4356 addADrawer(((WAppIcon *) entry->clientdata)->dock->screen_ptr);
4360 static void drawerDestroy(WDock *drawer)
4362 WScreen *scr;
4363 int i;
4364 WAppIcon *aicon;
4365 WMArray *icons;
4367 if (drawer == NULL)
4368 return;
4370 scr = drawer->screen_ptr;
4372 /* Note regarding menus: we can't delete any dock/clip/drawer menu, because
4373 * that would (attempt to) wfree some memory in gettext library (see menu
4374 * entries that have several "versions", such like "Hide" and "Unhide"). */
4376 wDefaultPurgeInfo(scr, drawer->icon_array[0]->wm_instance,
4377 drawer->icon_array[0]->wm_class);
4378 icons = WMCreateArray(drawer->icon_count - 1);
4379 for (i = 1; i < drawer->max_icons; i++) {
4380 if (!(aicon = drawer->icon_array[i]))
4381 continue;
4382 WMAddToArray(icons, aicon);
4384 removeIcons(icons, drawer);
4386 if (drawer->auto_collapse_magic) {
4387 WMDeleteTimerHandler(drawer->auto_collapse_magic);
4388 drawer->auto_collapse_magic = NULL;
4390 if (drawer->auto_lower_magic) {
4391 WMDeleteTimerHandler(drawer->auto_lower_magic);
4392 drawer->auto_lower_magic = NULL;
4395 wAppIconDestroy(drawer->icon_array[0]);
4396 wfree(drawer->icon_array);
4397 wfree(drawer);
4399 drawerRemoveFromChain(scr, drawer);
4400 if (scr->last_dock == drawer)
4401 scr->last_dock = NULL;
4402 if (scr->attracting_drawer == drawer)
4403 scr->attracting_drawer = NULL;
4407 static void removeDrawerCallback(WMenu *menu, WMenuEntry *entry)
4409 WDock *dock = ((WAppIcon*)entry->clientdata)->dock;
4410 assert(dock != NULL);
4412 if (dock->icon_count > 1) {
4413 if (wMessageDialog(dock->screen_ptr, _("Drawer"),
4414 _("All icons in this drawer will be detached!"),
4415 _("OK"), _("Cancel"), NULL) != WAPRDefault)
4416 return;
4418 drawerDestroy(dock);
4422 void wDrawerIconPaint(WAppIcon *dicon)
4424 Window win = dicon->icon->core->window;
4425 WScreen *scr = dicon->icon->core->screen_ptr;
4426 XPoint p[4];
4427 GC gc = scr->draw_gc;
4428 WMColor *color;
4430 wIconPaint(dicon->icon);
4432 if (!dicon->dock->collapsed)
4433 color = scr->clip_title_color[CLIP_NORMAL];
4434 else
4435 color = scr->clip_title_color[CLIP_COLLAPSED];
4436 XSetForeground(dpy, gc, WMColorPixel(color));
4438 if (dicon->dock->on_right_side) {
4439 p[0].x = p[3].x = 10;
4440 p[0].y = p[3].y = ICON_SIZE / 2 - 5;
4441 p[1].x = 10;
4442 p[1].y = ICON_SIZE / 2 + 5;
4443 p[2].x = 5;
4444 p[2].y = ICON_SIZE / 2;
4446 else {
4447 p[0].x = p[3].x = ICON_SIZE-1 - 10;
4448 p[0].y = p[3].y = ICON_SIZE / 2 - 5;
4449 p[1].x = ICON_SIZE-1 - 10;
4450 p[1].y = ICON_SIZE / 2 + 5;
4451 p[2].x = ICON_SIZE-1 - 5;
4452 p[2].y = ICON_SIZE / 2;
4454 XFillPolygon(dpy, win, gc, p,3,Convex,CoordModeOrigin);
4455 XDrawLines(dpy, win, gc, p,4,CoordModeOrigin);
4459 RImage* wDrawerMakeTile(WScreen *scr, RImage *normalTile)
4461 RImage *tile = RCloneImage(normalTile);
4462 RColor dark;
4463 RColor light;
4465 dark.alpha = 0;
4466 dark.red = dark.green = dark.blue = 60;
4468 light.alpha = 0;
4469 light.red = light.green = light.blue = 80;
4471 /* arrow bevel */
4472 if (!scr->dock || scr->dock->on_right_side) {
4473 ROperateLine(tile, RSubtractOperation, 11, ICON_SIZE / 2 - 7,
4474 4, ICON_SIZE / 2, &dark); /* / */
4475 ROperateLine(tile, RSubtractOperation, 11, ICON_SIZE / 2 + 7,
4476 4, ICON_SIZE / 2, &dark); /* \ */
4477 ROperateLine(tile, RAddOperation, 11, ICON_SIZE / 2 - 7,
4478 11, ICON_SIZE / 2 + 7, &light); /* | */
4480 else {
4481 ROperateLine(tile, RSubtractOperation, ICON_SIZE-1 - 11, ICON_SIZE / 2 - 7,
4482 ICON_SIZE-1 - 4, ICON_SIZE / 2, &dark); /* \ */
4483 ROperateLine(tile, RAddOperation, ICON_SIZE-1 - 11, ICON_SIZE / 2 + 7,
4484 ICON_SIZE-1 - 4, ICON_SIZE / 2, &light); /* / */
4485 ROperateLine(tile, RSubtractOperation, ICON_SIZE-1 - 11, ICON_SIZE / 2 - 7,
4486 ICON_SIZE-1 - 11, ICON_SIZE / 2 + 7, &dark); /* | */
4488 return tile;
4492 static void swapDrawer(WDock *drawer, int new_x)
4494 int i;
4496 drawer->on_right_side = !drawer->on_right_side;
4497 drawer->x_pos = new_x;
4499 for (i = 0; i < drawer->max_icons; i++) {
4500 WAppIcon *ai;
4501 if ((ai = drawer->icon_array[i]) == NULL)
4502 continue;
4503 ai->xindex *= -1; /* so A B C becomes C B A */
4504 ai->x_pos = new_x + ai->xindex * ICON_SIZE;
4506 /* Update drawer's tile */
4507 if (i == 0) {
4508 wIconUpdate(ai->icon);
4509 wDrawerIconPaint(ai);
4511 XMoveWindow(dpy, ai->icon->core->window, ai->x_pos, ai->y_pos);
4516 static void swapDrawers(WScreen *scr, int on_right_side, int new_x)
4518 WDrawerChain *dc;
4520 if (scr->drawer_tile) {
4521 RReleaseImage(scr->drawer_tile);
4523 scr->drawer_tile = wDrawerMakeTile(scr, scr->icon_tile);
4525 for (dc = scr->drawers; dc != NULL; dc = dc->next) {
4526 swapDrawer(dc->adrawer, new_x);
4527 assert(dc->adrawer->on_right_side == on_right_side);
4532 int wIsADrawer(WAppIcon *aicon)
4534 return aicon && aicon->dock &&
4535 aicon->dock->type == WM_DRAWER && aicon->dock->icon_array[0] == aicon;
4539 static WDock* getDrawer(WScreen *scr, int y_index)
4541 WDrawerChain *dc;
4543 for (dc = scr->drawers; dc != NULL; dc = dc->next) {
4544 if (dc->adrawer->y_pos - scr->dock->y_pos == y_index * ICON_SIZE)
4545 return dc->adrawer;
4547 return NULL;
4552 /* Find the "hole" a moving appicon created when snapped into the
4553 * drawer. redocking is a boolean. If the moving appicon comes from the
4554 * drawer, drawer->icon_count is correct. If not, redocking is then false and
4555 * there are now drawer->icon_count plus one appicons in the drawer. */
4556 static int indexOfHole(WDock *drawer, WAppIcon *moving_aicon, int redocking)
4558 int index_of_hole, i;
4560 /* Classic interview question...
4562 * We have n-1 (n = drawer->icon_count-1 or drawer->icon_count, see
4563 * redocking) appicons, whose xindex are unique in [1..n]. One is missing:
4564 * that's where the ghost of the moving appicon is, that's what the
4565 * function should return.
4567 * We compute 1+2+...+n (this sum is equal to n*(n+1)/2), we substract to
4568 * this sum the xindex of each of the n-1 appicons, and we get the correct
4569 * index! */
4571 if (redocking) {
4572 index_of_hole = (drawer->icon_count - 1) * drawer->icon_count / 2;
4573 } else {
4574 index_of_hole = drawer->icon_count * (drawer->icon_count + 1) / 2;
4576 index_of_hole *= (drawer->on_right_side ? -1 : 1);
4578 for (i = 1; i < drawer->max_icons; i++) {
4579 if (drawer->icon_array[i] && drawer->icon_array[i] != moving_aicon)
4580 index_of_hole -= drawer->icon_array[i]->xindex;
4582 /* wmessage(" Index of the moving appicon is %d (%sredocking)", index_of_hole, (redocking ? "" : "not ")); */
4583 if (abs(index_of_hole) > abs(drawer->icon_count) - (redocking ? 1 : 0))
4584 wwarning(" index_of_hole is too large ! (%d greater than %d)",
4585 index_of_hole, abs(drawer->icon_count) - (redocking ? 1 : 0));
4586 if (index_of_hole == 0)
4587 wwarning(" index_of_hole == 0 (%sredocking, icon_count == %d)", (redocking ? "" : "not "), drawer->icon_count);
4589 return index_of_hole;
4593 void wSlideAppicons(WAppIcon **appicons, int n, int to_the_left)
4595 int i;
4596 int leftmost = -1, min_index = 9999, from_x = -1; // leftmost and from_x initialized to avoid warning
4597 Window *wins[n];
4598 WAppIcon *aicon;
4600 if (n < 1)
4601 return;
4603 for (i = 0; i < n; i++) {
4604 aicon = appicons[i];
4605 aicon->xindex += (to_the_left ? -1 : +1);
4606 if (aicon->xindex < min_index) {
4607 min_index = aicon->xindex;
4608 leftmost = i;
4609 from_x = aicon->x_pos;
4611 aicon->x_pos += (to_the_left ? -ICON_SIZE : +ICON_SIZE);
4614 for (i = 0; i < n; i++) {
4615 aicon = appicons[i];
4616 wins[aicon->xindex - min_index] = &aicon->icon->core->window;
4618 aicon = appicons[leftmost];
4619 SlideWindows(wins, n, from_x, aicon->y_pos, aicon->x_pos, aicon->y_pos);
4623 void wDrawerFillTheGap(WDock *drawer, WAppIcon *aicon, Bool redocking)
4625 int i, j;
4626 int index_of_hole = indexOfHole(drawer, aicon, redocking);
4627 WAppIcon *aicons_to_shift[drawer->icon_count];
4629 j = 0;
4630 for (i = 0; i < drawer->max_icons; i++) {
4631 WAppIcon *ai = drawer->icon_array[i];
4632 if (ai && ai != aicon &&
4633 abs(ai->xindex) > abs(index_of_hole))
4634 aicons_to_shift[j++] = ai;
4636 if (j != drawer->icon_count - abs(index_of_hole) - (redocking ? 1 : 0))
4637 wwarning("Removing aicon at index %d from %s: j=%d but should be %d",
4638 index_of_hole, drawer->icon_array[0]->wm_instance,
4639 j, drawer->icon_count - abs(index_of_hole) - (redocking ? 1 : 0));
4640 wSlideAppicons(aicons_to_shift, j, !drawer->on_right_side);
4644 static void drawerConsolidateIcons(WDock *drawer)
4646 WAppIcon *aicons_to_shift[drawer->icon_count];
4647 int maxRemaining = 0;
4648 int sum = 0;
4649 int i;
4650 for (i = 0; i < drawer->max_icons; i++) {
4651 WAppIcon *ai = drawer->icon_array[i];
4652 if (ai == NULL)
4653 continue;
4654 sum += abs(ai->xindex);
4655 if (abs(ai->xindex) > maxRemaining)
4656 maxRemaining = abs(ai->xindex);
4658 while (sum != maxRemaining * (maxRemaining + 1) / 2) { // while there is a hole
4659 WAppIcon *ai;
4660 int n;
4661 // Look up for the hole at max position
4662 int maxDeleted;
4663 for (maxDeleted = maxRemaining - 1; maxDeleted > 0; maxDeleted--) {
4664 Bool foundAppIconThere = False;
4665 for (i = 0; i < drawer->max_icons; i++) {
4666 WAppIcon *ai = drawer->icon_array[i];
4667 if (ai == NULL)
4668 continue;
4669 if (abs(ai->xindex) == maxDeleted) {
4670 foundAppIconThere = True;
4671 break;
4674 if (!foundAppIconThere)
4675 break;
4677 assert(maxDeleted > 0); // would mean while test is wrong
4678 n = 0;
4679 for (i = 0; i < drawer->max_icons; i++) {
4680 ai = drawer->icon_array[i];
4681 if (ai != NULL && abs(ai->xindex) > maxDeleted)
4682 aicons_to_shift[n++] = ai;
4684 assert(n == maxRemaining - maxDeleted); // for the code review ;-)
4685 wSlideAppicons(aicons_to_shift, n, !drawer->on_right_side);
4686 // Efficient beancounting
4687 maxRemaining -= 1;
4688 sum -= n;
4694 /* similar to wDockRestoreState, but a lot a specific stuff too... */
4695 static WDock * drawerRestoreState(WScreen *scr, WMPropList *drawer_state)
4697 WDock *drawer;
4698 WMPropList *apps, *value, *dock_state;
4699 WAppIcon *aicon;
4700 int count, i;
4702 if (!drawer_state)
4703 return NULL;
4705 make_keys();
4707 WMRetainPropList(drawer_state);
4709 /* Get the instance name, and create a drawer */
4710 value = WMGetFromPLDictionary(drawer_state, dName);
4711 drawer = wDockCreate(scr, WM_DRAWER, WMGetFromPLString(value));
4713 /* restore DnD command and paste command */
4714 #ifdef XDND
4715 value = WMGetFromPLDictionary(drawer_state, dDropCommand);
4716 if (value && WMIsPLString(value))
4717 drawer->icon_array[0]->dnd_command = wstrdup(WMGetFromPLString(value));
4718 #endif /* XDND */
4720 value = WMGetFromPLDictionary(drawer_state, dPasteCommand);
4721 if (value && WMIsPLString(value))
4722 drawer->icon_array[0]->paste_command = wstrdup(WMGetFromPLString(value));
4724 /* restore position */
4725 value = WMGetFromPLDictionary(drawer_state, dPosition);
4726 if (!value || !WMIsPLString(value))
4727 COMPLAIN("Position");
4728 else {
4729 int x, y, y_index;
4730 if (sscanf(WMGetFromPLString(value), "%i,%i", &x, &y) != 2)
4731 COMPLAIN("Position");
4733 /* check position sanity */
4734 if (x != scr->dock->x_pos) {
4735 x = scr->dock->x_pos;
4737 y_index = (y - scr->dock->y_pos) / ICON_SIZE;
4738 if (y_index >= scr->dock->max_icons) {
4739 /* Here we should do something more intelligent, since it
4740 * can happen even if the user hasn't hand-edited his
4741 * G/D/State file (but uses a lower resolution). */
4742 y_index = scr->dock->max_icons - 1;
4744 y = scr->dock->y_pos + y_index * ICON_SIZE;
4745 moveDock(drawer, x, y);
4748 /* restore dock properties (applist and others) */
4749 dock_state = WMGetFromPLDictionary(drawer_state, dDock);
4751 /* restore lowered/raised state: same as scr->dock, no matter what */
4752 drawer->lowered = scr->dock->lowered;
4753 if (!drawer->lowered)
4754 ChangeStackingLevel(drawer->icon_array[0]->icon->core, WMDockLevel);
4755 else
4756 ChangeStackingLevel(drawer->icon_array[0]->icon->core, WMNormalLevel);
4757 wRaiseFrame(drawer->icon_array[0]->icon->core);
4759 /* restore collapsed state */
4760 drawer->collapsed = 0;
4761 value = WMGetFromPLDictionary(dock_state, dCollapsed);
4762 if (value && strcasecmp(WMGetFromPLString(value), "YES") == 0) {
4763 drawer->collapsed = 1;
4765 /* restore auto-collapsed state */
4766 value = WMGetFromPLDictionary(dock_state, dAutoCollapse);
4767 if (value && strcasecmp(WMGetFromPLString(value), "YES") == 0) {
4768 drawer->auto_collapse = 1;
4769 drawer->collapsed = 1;
4770 } else {
4771 drawer->auto_collapse = 0; // because wDockCreate sets it (drawers only)
4774 /* restore auto-raise/lower state: same as scr->dock, no matter what */
4775 drawer->auto_raise_lower = scr->dock->auto_raise_lower;
4777 /* restore attract icons state */
4778 drawer->attract_icons = 0;
4779 value = WMGetFromPLDictionary(dock_state, dAutoAttractIcons);
4780 if (value && strcasecmp(WMGetFromPLString(value), "YES") == 0) {
4781 drawer->attract_icons = 1;
4782 scr->attracting_drawer = drawer;
4785 /* application list */
4786 apps = WMGetFromPLDictionary(dock_state, dApplications);
4788 if (!apps) {
4789 goto finish;
4792 count = WMGetPropListItemCount(apps);
4794 if (count == 0)
4795 goto finish;
4797 for (i=0; i<count; i++) {
4798 if (drawer->icon_count >= drawer->max_icons) {
4799 wwarning(_("there are too many icons stored in drawer. Ignoring what doesn't fit"));
4800 break;
4803 value = WMGetFromPLArray(apps, i);
4804 aicon = restore_icon_state(scr, value, WM_DRAWER, drawer->icon_count);
4806 drawer->icon_array[drawer->icon_count] = aicon;
4808 if (aicon) {
4809 aicon->dock = drawer;
4810 aicon->x_pos = drawer->x_pos + (aicon->xindex * ICON_SIZE);
4811 aicon->y_pos = drawer->y_pos + (aicon->yindex * ICON_SIZE);
4813 if (!drawer->lowered)
4814 ChangeStackingLevel(aicon->icon->core, WMDockLevel);
4815 else
4816 ChangeStackingLevel(aicon->icon->core, WMNormalLevel);
4818 wCoreConfigure(aicon->icon->core, aicon->x_pos, aicon->y_pos, 0, 0);
4820 if (!drawer->collapsed)
4821 XMapWindow(dpy, aicon->icon->core->window);
4822 wRaiseFrame(aicon->icon->core);
4824 drawer->icon_count++;
4828 finish:
4829 WMReleasePropList(drawer_state);
4831 return drawer;
4835 /* Same kind of comment than for previous function: this function is
4836 * very similar to make_icon_state, but has substential differences as
4837 * well. */
4838 static WMPropList *drawerSaveState(WScreen *scr, WDock *drawer)
4840 WMPropList *pstr, *drawer_state;
4841 WAppIcon *ai;
4842 char buffer[64];
4844 ai = drawer->icon_array[0];
4845 /* Store its name */
4846 pstr = WMCreatePLString(ai->wm_instance);
4847 drawer_state = WMCreatePLDictionary(dName, pstr, NULL); /* we need this final NULL */
4848 WMReleasePropList(pstr);
4850 /* Store its position */
4851 snprintf(buffer, sizeof(buffer), "%i,%i", ai->x_pos, ai->y_pos);
4852 pstr = WMCreatePLString(buffer);
4853 WMPutInPLDictionary(drawer_state, dPosition, pstr);
4854 WMReleasePropList(pstr);
4856 #ifdef XDND
4857 /* Store its DnD command */
4858 if (ai->dnd_command) {
4859 pstr = WMCreatePLString(ai->dnd_command);
4860 WMPutInPLDictionary(drawer_state, dDropCommand, pstr);
4861 WMReleasePropList(pstr);
4863 #endif /* XDND */
4865 /* Store its paste command */
4866 if (ai->paste_command) {
4867 pstr = WMCreatePLString(ai->paste_command);
4868 WMPutInPLDictionary(drawer_state, dPasteCommand, pstr);
4869 WMReleasePropList(pstr);
4872 /* Store applications list and other properties */
4873 pstr = dockSaveState(drawer);
4874 WMPutInPLDictionary(drawer_state, dDock, pstr);
4875 WMReleasePropList(pstr);
4877 return drawer_state;
4881 void wDrawersSaveState(WScreen *scr)
4883 WMPropList *all_drawers, *drawer_state;
4884 int i;
4885 WDrawerChain *dc;
4887 make_keys();
4889 all_drawers = WMCreatePLArray(NULL);
4890 for (i=0, dc = scr->drawers;
4891 i < scr->drawer_count;
4892 i++, dc = dc->next) {
4893 drawer_state = drawerSaveState(scr, dc->adrawer);
4894 WMAddToPLArray(all_drawers, drawer_state);
4895 WMReleasePropList(drawer_state);
4897 WMPutInPLDictionary(w_global.session_state, dDrawers, all_drawers);
4898 WMReleasePropList(all_drawers);
4902 void wDrawersRestoreState(WScreen *scr)
4904 WMPropList *all_drawers, *drawer_state;
4905 int i;
4907 make_keys();
4909 if (w_global.session_state == NULL)
4910 return;
4912 all_drawers = WMGetFromPLDictionary(w_global.session_state, dDrawers);
4913 if (!all_drawers)
4914 return;
4916 for (i = 0; i < WMGetPropListItemCount(all_drawers); i++) {
4917 drawer_state = WMGetFromPLArray(all_drawers, i);
4918 drawerRestoreState(scr, drawer_state);
4919 // Note: scr->drawers was updated when the the drawer was created