Removed unused WScreen variable in wIsADrawer
[wmaker-crm.git] / src / dock.c
blob3320f6c5762c63f491afa6fef6a37048847501a0
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 != dock->screen_ptr->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 (scr->clip_icon)
820 return scr->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 scr->clip_icon = btn;
857 return btn;
860 static void switchWSCommand(WMenu *menu, WMenuEntry *entry)
862 WAppIcon *btn, *icon = (WAppIcon *) entry->clientdata;
863 WScreen *scr = icon->icon->core->screen_ptr;
864 WDock *src, *dest;
865 WMArray *selectedIcons;
866 int x, y;
868 if (entry->order == w_global.workspace.current)
869 return;
870 src = icon->dock;
871 dest = w_global.workspace.array[entry->order]->clip;
873 selectedIcons = getSelected(src);
875 if (WMGetArrayItemCount(selectedIcons)) {
876 WMArrayIterator iter;
878 WM_ITERATE_ARRAY(selectedIcons, btn, iter) {
879 if (wDockFindFreeSlot(dest, &x, &y)) {
880 wDockMoveIconBetweenDocks(src, dest, btn, x, y);
881 XUnmapWindow(dpy, btn->icon->core->window);
884 } else if (icon != scr->clip_icon) {
885 if (wDockFindFreeSlot(dest, &x, &y)) {
886 wDockMoveIconBetweenDocks(src, dest, icon, x, y);
887 XUnmapWindow(dpy, icon->icon->core->window);
890 WMFreeArray(selectedIcons);
893 static void launchDockedApplication(WAppIcon *btn, Bool withSelection)
895 WScreen *scr = btn->icon->core->screen_ptr;
897 if (!btn->launching &&
898 ((!withSelection && btn->command != NULL) || (withSelection && btn->paste_command != NULL))) {
899 if (!btn->forced_dock) {
900 btn->relaunching = btn->running;
901 btn->running = 1;
903 if (btn->wm_instance || btn->wm_class) {
904 WWindowAttributes attr;
905 memset(&attr, 0, sizeof(WWindowAttributes));
906 wDefaultFillAttributes(btn->wm_instance, btn->wm_class, &attr, NULL, True);
908 if (!attr.no_appicon && !btn->buggy_app)
909 btn->launching = 1;
910 else
911 btn->running = 0;
913 btn->drop_launch = 0;
914 btn->paste_launch = withSelection;
915 scr->last_dock = btn->dock;
916 btn->pid = execCommand(btn, (withSelection ? btn->paste_command : btn->command), NULL);
917 if (btn->pid > 0) {
918 if (btn->buggy_app) {
919 /* give feedback that the app was launched */
920 btn->launching = 1;
921 dockIconPaint(btn);
922 btn->launching = 0;
923 WMAddTimerHandler(200, (WMCallback *) dockIconPaint, btn);
924 } else {
925 dockIconPaint(btn);
927 } else {
928 wwarning(_("could not launch application %s"), btn->command);
929 btn->launching = 0;
930 if (!btn->relaunching)
931 btn->running = 0;
936 static void updateWorkspaceMenu(WMenu *menu, WAppIcon *icon)
938 char title[MAX_WORKSPACENAME_WIDTH + 1];
939 int i;
941 if (!menu || !icon)
942 return;
944 for (i = 0; i < w_global.workspace.count; i++) {
945 if (i < menu->entry_no) {
946 if (strcmp(menu->entries[i]->text, w_global.workspace.array[i]->name) != 0) {
947 wfree(menu->entries[i]->text);
948 strcpy(title, w_global.workspace.array[i]->name);
949 menu->entries[i]->text = wstrdup(title);
950 menu->flags.realized = 0;
952 menu->entries[i]->clientdata = (void *)icon;
953 } else {
954 strcpy(title, w_global.workspace.array[i]->name);
956 wMenuAddCallback(menu, title, switchWSCommand, (void *)icon);
958 menu->flags.realized = 0;
961 if (i == w_global.workspace.current)
962 wMenuSetEnabled(menu, i, False);
963 else
964 wMenuSetEnabled(menu, i, True);
967 if (!menu->flags.realized)
968 wMenuRealize(menu);
971 static WMenu *makeWorkspaceMenu(WScreen *scr)
973 WMenu *menu;
975 menu = wMenuCreate(scr, NULL, False);
976 if (!menu)
977 wwarning(_("could not create workspace submenu for Clip menu"));
979 wMenuAddCallback(menu, "", switchWSCommand, (void *)scr->clip_icon);
981 menu->flags.realized = 0;
982 wMenuRealize(menu);
984 return menu;
987 static void updateClipOptionsMenu(WMenu *menu, WDock *dock)
989 WMenuEntry *entry;
990 int index = 0;
992 if (!menu || !dock)
993 return;
995 /* keep on top */
996 entry = menu->entries[index];
997 entry->flags.indicator_on = !dock->lowered;
998 entry->clientdata = dock;
999 wMenuSetEnabled(menu, index, dock->type == WM_CLIP);
1001 /* collapsed */
1002 entry = menu->entries[++index];
1003 entry->flags.indicator_on = dock->collapsed;
1004 entry->clientdata = dock;
1006 /* auto-collapse */
1007 entry = menu->entries[++index];
1008 entry->flags.indicator_on = dock->auto_collapse;
1009 entry->clientdata = dock;
1011 /* auto-raise/lower */
1012 entry = menu->entries[++index];
1013 entry->flags.indicator_on = dock->auto_raise_lower;
1014 entry->clientdata = dock;
1015 wMenuSetEnabled(menu, index, dock->lowered && (dock->type == WM_CLIP));
1017 /* attract icons */
1018 entry = menu->entries[++index];
1019 entry->flags.indicator_on = dock->attract_icons;
1020 entry->clientdata = dock;
1022 menu->flags.realized = 0;
1023 wMenuRealize(menu);
1027 static WMenu *makeClipOptionsMenu(WScreen *scr)
1029 WMenu *menu;
1030 WMenuEntry *entry;
1032 menu = wMenuCreate(scr, NULL, False);
1033 if (!menu) {
1034 wwarning(_("could not create options submenu for Clip menu"));
1035 return NULL;
1038 entry = wMenuAddCallback(menu, _("Keep on Top"), toggleLoweredCallback, NULL);
1039 entry->flags.indicator = 1;
1040 entry->flags.indicator_on = 1;
1041 entry->flags.indicator_type = MI_CHECK;
1043 entry = wMenuAddCallback(menu, _("Collapsed"), toggleCollapsedCallback, NULL);
1044 entry->flags.indicator = 1;
1045 entry->flags.indicator_on = 1;
1046 entry->flags.indicator_type = MI_CHECK;
1048 entry = wMenuAddCallback(menu, _("Autocollapse"), toggleAutoCollapseCallback, NULL);
1049 entry->flags.indicator = 1;
1050 entry->flags.indicator_on = 1;
1051 entry->flags.indicator_type = MI_CHECK;
1053 entry = wMenuAddCallback(menu, _("Autoraise"), toggleAutoRaiseLowerCallback, NULL);
1054 entry->flags.indicator = 1;
1055 entry->flags.indicator_on = 1;
1056 entry->flags.indicator_type = MI_CHECK;
1058 entry = wMenuAddCallback(menu, _("Autoattract Icons"), toggleAutoAttractCallback, NULL);
1059 entry->flags.indicator = 1;
1060 entry->flags.indicator_on = 1;
1061 entry->flags.indicator_type = MI_CHECK;
1063 menu->flags.realized = 0;
1064 wMenuRealize(menu);
1066 return menu;
1070 static void setDockPositionNormalCallback(WMenu *menu, WMenuEntry *entry)
1072 WDock *dock = (WDock *) entry->clientdata;
1073 WDrawerChain *dc;
1074 if (entry->flags.indicator_on) // already set, nothing to do
1075 return;
1076 // Do we come from auto raise lower or keep on top?
1077 if (dock->auto_raise_lower)
1079 dock->auto_raise_lower = 0;
1080 // Only for aesthetic purposes, can be removed when Autoraise status is no longer exposed in drawer option menu
1081 for (dc = dock->screen_ptr->drawers; dc != NULL; dc = dc->next) {
1082 dc->adrawer->auto_raise_lower = 0;
1085 else
1087 // Will take care of setting lowered = 0 in drawers
1088 toggleLowered(dock);
1090 entry->flags.indicator_on = 1;
1093 static void setDockPositionAutoRaiseLowerCallback(WMenu *menu, WMenuEntry *entry)
1095 WDock *dock = (WDock *) entry->clientdata;
1096 WDrawerChain *dc;
1097 if (entry->flags.indicator_on) // already set, nothing to do
1098 return;
1099 // Do we come from normal or keep on top?
1100 if (!dock->lowered)
1102 toggleLowered(dock);
1104 dock->auto_raise_lower = 1;
1105 // Only for aesthetic purposes, can be removed when Autoraise status is no longer exposed in drawer option menu
1106 for (dc = dock->screen_ptr->drawers; dc != NULL; dc = dc->next) {
1107 dc->adrawer->auto_raise_lower = 1;
1109 entry->flags.indicator_on = 1;
1112 static void setDockPositionKeepOnTopCallback(WMenu *menu, WMenuEntry *entry)
1114 WDock *dock = (WDock *) entry->clientdata;
1115 WDrawerChain *dc;
1116 if (entry->flags.indicator_on) // already set, nothing to do
1117 return;
1118 dock->auto_raise_lower = 0;
1119 // Only for aesthetic purposes, can be removed when Autoraise status is no longer exposed in drawer option menu
1120 for (dc = dock->screen_ptr->drawers; dc != NULL; dc = dc->next) {
1121 dc->adrawer->auto_raise_lower = 0;
1123 toggleLowered(dock);
1124 entry->flags.indicator_on = 1;
1127 static void updateDockPositionMenu(WMenu *menu, WDock *dock)
1129 WMenuEntry *entry;
1130 int index = 0;
1132 assert(menu);
1133 assert(dock);
1135 /* Normal level */
1136 entry = menu->entries[index++];
1137 entry->flags.indicator_on = (dock->lowered && !dock->auto_raise_lower);
1138 entry->clientdata = dock;
1140 /* Auto-raise/lower */
1141 entry = menu->entries[index++];
1142 entry->flags.indicator_on = dock->auto_raise_lower;
1143 entry->clientdata = dock;
1145 /* Keep on top */
1146 entry = menu->entries[index++];
1147 entry->flags.indicator_on = !dock->lowered;
1148 entry->clientdata = dock;
1151 static WMenu *makeDockPositionMenu(WScreen *scr)
1153 /* When calling this, the dock is being created, so scr->dock is still not set
1154 * Therefore the callbacks' clientdata and the indicators can't be set,
1155 * they will be updated when the dock menu is opened. */
1156 WMenu *menu;
1157 WMenuEntry *entry;
1159 menu = wMenuCreate(scr, NULL, False);
1160 if (!menu) {
1161 wwarning(_("could not create options submenu for dock position menu"));
1162 return NULL;
1165 entry = wMenuAddCallback(menu, _("Normal"), setDockPositionNormalCallback, NULL);
1166 entry->flags.indicator = 1;
1167 entry->flags.indicator_type = MI_DIAMOND;
1169 entry = wMenuAddCallback(menu, _("Auto raise & lower"), setDockPositionAutoRaiseLowerCallback, NULL);
1170 entry->flags.indicator = 1;
1171 entry->flags.indicator_type = MI_DIAMOND;
1173 entry = wMenuAddCallback(menu, _("Keep on Top"), setDockPositionKeepOnTopCallback, NULL);
1174 entry->flags.indicator = 1;
1175 entry->flags.indicator_type = MI_DIAMOND;
1177 menu->flags.realized = 0;
1178 wMenuRealize(menu);
1180 return menu;
1184 static WMenu *dockMenuCreate(WScreen *scr, int type)
1186 WMenu *menu;
1187 WMenuEntry *entry;
1189 if (type == WM_CLIP && w_global.clip.menu)
1190 return w_global.clip.menu;
1192 if (type == WM_DRAWER && w_global.dock.drawer_menu)
1193 return w_global.dock.drawer_menu;
1195 menu = wMenuCreate(scr, NULL, False);
1196 if (type == WM_DOCK) {
1197 entry = wMenuAddCallback(menu, _("Dock position"), NULL, NULL);
1198 if (w_global.dock.pos_menu == NULL)
1199 w_global.dock.pos_menu = makeDockPositionMenu(scr);
1200 wMenuEntrySetCascade(menu, entry, w_global.dock.pos_menu);
1202 if (!wPreferences.flags.nodrawer) {
1203 entry = wMenuAddCallback(menu, _("Add a drawer"), addADrawerCallback, NULL);
1205 } else {
1206 if (type == WM_CLIP)
1207 entry = wMenuAddCallback(menu, _("Clip Options"), NULL, NULL);
1208 else /* if (type == WM_DRAWER) */
1209 entry = wMenuAddCallback(menu, _("Drawer options"), NULL, NULL);
1211 if (w_global.clip.opt_menu == NULL)
1212 w_global.clip.opt_menu = makeClipOptionsMenu(scr);
1214 wMenuEntrySetCascade(menu, entry, w_global.clip.opt_menu);
1216 /* The same menu is used for the dock and its appicons. If the menu
1217 * entry text is different between the two contexts, or if it can
1218 * change depending on some state, free the duplicated string (from
1219 * wMenuInsertCallback) and use gettext's string */
1220 if (type == WM_CLIP) {
1221 entry = wMenuAddCallback(menu, _("Rename Workspace"), renameCallback, NULL);
1222 wfree(entry->text);
1223 entry->text = _("Rename Workspace"); /* can be: (Toggle) Omnipresent */
1226 entry = wMenuAddCallback(menu, _("Selected"), selectCallback, NULL);
1227 entry->flags.indicator = 1;
1228 entry->flags.indicator_on = 1;
1229 entry->flags.indicator_type = MI_CHECK;
1231 entry = wMenuAddCallback(menu, _("Select All Icons"), selectIconsCallback, NULL);
1232 wfree(entry->text);
1233 entry->text = _("Select All Icons"); /* can be: Unselect all icons */
1235 entry = wMenuAddCallback(menu, _("Keep Icon"), keepIconsCallback, NULL);
1236 wfree(entry->text);
1237 entry->text = _("Keep Icon"); /* can be: Keep Icons */
1239 if (type == WM_CLIP) {
1240 entry = wMenuAddCallback(menu, _("Move Icon To"), NULL, NULL);
1241 wfree(entry->text);
1242 entry->text = _("Move Icon To"); /* can be: Move Icons to */
1243 w_global.clip.submenu = makeWorkspaceMenu(scr);
1244 if (w_global.clip.submenu)
1245 wMenuEntrySetCascade(menu, entry, w_global.clip.submenu);
1248 entry = wMenuAddCallback(menu, _("Remove Icon"), removeIconsCallback, NULL);
1249 wfree(entry->text);
1250 entry->text = _("Remove Icon"); /* can be: Remove Icons */
1252 wMenuAddCallback(menu, _("Attract Icons"), colectIconsCallback, NULL);
1255 wMenuAddCallback(menu, _("Launch"), launchCallback, NULL);
1257 wMenuAddCallback(menu, _("Unhide Here"), unhideHereCallback, NULL);
1259 entry = wMenuAddCallback(menu, _("Hide"), hideCallback, NULL);
1260 wfree(entry->text);
1261 entry->text = _("Hide"); /* can be: Unhide */
1263 wMenuAddCallback(menu, _("Settings..."), settingsCallback, NULL);
1265 entry = wMenuAddCallback(menu, _("Kill"), killCallback, NULL);
1266 wfree(entry->text);
1267 entry->text = _("Kill"); /* can be: Remove drawer */
1269 if (type == WM_CLIP)
1270 w_global.clip.menu = menu;
1272 if (type == WM_DRAWER)
1273 w_global.dock.drawer_menu = menu;
1275 return menu;
1278 WDock *wDockCreate(WScreen *scr, int type, const char *name)
1280 WDock *dock;
1281 WAppIcon *btn;
1283 make_keys();
1285 dock = wmalloc(sizeof(WDock));
1287 switch (type) {
1288 case WM_CLIP:
1289 dock->max_icons = DOCK_MAX_ICONS;
1290 break;
1291 case WM_DRAWER:
1292 dock->max_icons = scr->scr_width / wPreferences.icon_size;
1293 break;
1294 case WM_DOCK:
1295 default:
1296 dock->max_icons = scr->scr_height / wPreferences.icon_size;
1299 dock->icon_array = wmalloc(sizeof(WAppIcon *) * dock->max_icons);
1301 btn = mainIconCreate(scr, type, name);
1303 btn->dock = dock;
1305 dock->x_pos = btn->x_pos;
1306 dock->y_pos = btn->y_pos;
1307 dock->screen_ptr = scr;
1308 dock->type = type;
1309 dock->icon_count = 1;
1310 if (type == WM_DRAWER)
1311 dock->on_right_side = scr->dock->on_right_side;
1312 else
1313 dock->on_right_side = 1;
1314 dock->collapsed = 0;
1315 dock->auto_collapse = 0;
1316 dock->auto_collapse_magic = NULL;
1317 dock->auto_raise_lower = 0;
1318 dock->auto_lower_magic = NULL;
1319 dock->auto_raise_magic = NULL;
1320 dock->attract_icons = 0;
1321 dock->lowered = 1;
1322 dock->icon_array[0] = btn;
1323 wRaiseFrame(btn->icon->core);
1324 XMoveWindow(dpy, btn->icon->core->window, btn->x_pos, btn->y_pos);
1326 /* create dock menu */
1327 dock->menu = dockMenuCreate(scr, type);
1329 if (type == WM_DRAWER) {
1330 drawerAppendToChain(scr, dock);
1331 dock->auto_collapse = 1;
1334 return dock;
1337 void wDockDestroy(WDock *dock)
1339 int i;
1340 WAppIcon *aicon;
1342 for (i = (dock->type == WM_CLIP) ? 1 : 0; i < dock->max_icons; i++) {
1343 aicon = dock->icon_array[i];
1344 if (aicon) {
1345 int keepit = aicon->running && wApplicationOf(aicon->main_window);
1346 wDockDetach(dock, aicon);
1347 if (keepit) {
1348 /* XXX: can: aicon->icon == NULL ? */
1349 PlaceIcon(dock->screen_ptr, &aicon->x_pos, &aicon->y_pos,
1350 wGetHeadForWindow(aicon->icon->owner));
1351 XMoveWindow(dpy, aicon->icon->core->window, aicon->x_pos, aicon->y_pos);
1352 if (!dock->mapped || dock->collapsed)
1353 XMapWindow(dpy, aicon->icon->core->window);
1357 if (wPreferences.auto_arrange_icons)
1358 wArrangeIcons(dock->screen_ptr, True);
1359 wfree(dock->icon_array);
1360 if (dock->menu && dock->type != WM_CLIP)
1361 wMenuDestroy(dock->menu, True);
1362 if (dock->screen_ptr->last_dock == dock)
1363 dock->screen_ptr->last_dock = NULL;
1364 wfree(dock);
1367 void wClipIconPaint(WAppIcon *aicon)
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(desc->parent);
1415 static void dockIconPaint(WAppIcon *btn)
1417 if (btn == btn->icon->core->screen_ptr->clip_icon) {
1418 wClipIconPaint(btn);
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 == btn->icon->core->screen_ptr->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(scr->session_state, dDock, dock_state);
1580 WMReleasePropList(dock_state);
1583 void wClipSaveState(WScreen *scr)
1585 WMPropList *clip_state;
1587 clip_state = make_icon_state(scr->clip_icon);
1589 WMPutInPLDictionary(scr->session_state, dClip, clip_state);
1591 WMReleasePropList(clip_state);
1594 WMPropList *wClipSaveWorkspaceState(WScreen *scr, int workspace)
1596 return dockSaveState(w_global.workspace.array[workspace]->clip);
1599 static Bool getBooleanDockValue(WMPropList *value, WMPropList *key)
1601 if (value) {
1602 if (WMIsPLString(value)) {
1603 if (strcasecmp(WMGetFromPLString(value), "YES") == 0)
1604 return True;
1605 } else {
1606 wwarning(_("bad value in docked icon state info %s"), WMGetFromPLString(key));
1609 return False;
1612 static WAppIcon *restore_icon_state(WScreen *scr, WMPropList *info, int type, int index)
1614 WAppIcon *aicon;
1615 WMPropList *cmd, *value;
1616 char *wclass, *winstance, *command;
1618 cmd = WMGetFromPLDictionary(info, dCommand);
1619 if (!cmd || !WMIsPLString(cmd))
1620 return NULL;
1622 /* parse window name */
1623 value = WMGetFromPLDictionary(info, dName);
1624 if (!value)
1625 return NULL;
1627 ParseWindowName(value, &winstance, &wclass, "dock");
1629 if (!winstance && !wclass)
1630 return NULL;
1632 /* get commands */
1633 if (cmd)
1634 command = wstrdup(WMGetFromPLString(cmd));
1635 else
1636 command = NULL;
1638 if (!command || strcmp(command, "-") == 0) {
1639 if (command)
1640 wfree(command);
1641 if (wclass)
1642 wfree(wclass);
1643 if (winstance)
1644 wfree(winstance);
1646 return NULL;
1649 aicon = wAppIconCreateForDock(scr, command, winstance, wclass, TILE_NORMAL);
1650 if (wclass)
1651 wfree(wclass);
1652 if (winstance)
1653 wfree(winstance);
1654 if (command)
1655 wfree(command);
1657 aicon->icon->core->descriptor.handle_mousedown = iconMouseDown;
1658 aicon->icon->core->descriptor.handle_enternotify = clipEnterNotify;
1659 aicon->icon->core->descriptor.handle_leavenotify = clipLeaveNotify;
1660 aicon->icon->core->descriptor.parent_type = WCLASS_DOCK_ICON;
1661 aicon->icon->core->descriptor.parent = aicon;
1663 #ifdef XDND /* was OFFIX */
1664 cmd = WMGetFromPLDictionary(info, dDropCommand);
1665 if (cmd)
1666 aicon->dnd_command = wstrdup(WMGetFromPLString(cmd));
1667 #endif
1669 cmd = WMGetFromPLDictionary(info, dPasteCommand);
1670 if (cmd)
1671 aicon->paste_command = wstrdup(WMGetFromPLString(cmd));
1673 /* check auto launch */
1674 value = WMGetFromPLDictionary(info, dAutoLaunch);
1676 aicon->auto_launch = getBooleanDockValue(value, dAutoLaunch);
1678 /* check lock */
1679 value = WMGetFromPLDictionary(info, dLock);
1681 aicon->lock = getBooleanDockValue(value, dLock);
1683 /* check if it wasn't normally docked */
1684 value = WMGetFromPLDictionary(info, dForced);
1686 aicon->forced_dock = getBooleanDockValue(value, dForced);
1688 /* check if we can rely on the stuff in the app */
1689 value = WMGetFromPLDictionary(info, dBuggyApplication);
1691 aicon->buggy_app = getBooleanDockValue(value, dBuggyApplication);
1693 /* get position in the dock */
1694 value = WMGetFromPLDictionary(info, dPosition);
1695 if (value && WMIsPLString(value)) {
1696 if (sscanf(WMGetFromPLString(value), "%hi,%hi", &aicon->xindex, &aicon->yindex) != 2)
1697 wwarning(_("bad value in docked icon state info %s"), WMGetFromPLString(dPosition));
1699 /* check position sanity */
1700 /* *Very* incomplete section! */
1701 if (type == WM_DOCK) {
1702 aicon->xindex = 0;
1704 } else {
1705 aicon->yindex = index;
1706 aicon->xindex = 0;
1709 /* check if icon is omnipresent */
1710 value = WMGetFromPLDictionary(info, dOmnipresent);
1712 aicon->omnipresent = getBooleanDockValue(value, dOmnipresent);
1714 aicon->running = 0;
1715 aicon->docked = 1;
1717 return aicon;
1720 #define COMPLAIN(key) wwarning(_("bad value in dock state info:%s"), key)
1722 WAppIcon *wClipRestoreState(WScreen *scr, WMPropList *clip_state)
1724 WAppIcon *icon;
1725 WMPropList *value;
1727 icon = mainIconCreate(scr, WM_CLIP, NULL);
1729 if (!clip_state)
1730 return icon;
1732 WMRetainPropList(clip_state);
1734 /* restore position */
1736 value = WMGetFromPLDictionary(clip_state, dPosition);
1738 if (value) {
1739 if (!WMIsPLString(value)) {
1740 COMPLAIN("Position");
1741 } else {
1742 if (sscanf(WMGetFromPLString(value), "%i,%i", &icon->x_pos, &icon->y_pos) != 2)
1743 COMPLAIN("Position");
1745 /* check position sanity */
1746 if (!onScreen(scr, icon->x_pos, icon->y_pos))
1747 wScreenKeepInside(scr, &icon->x_pos, &icon->y_pos, ICON_SIZE, ICON_SIZE);
1750 #ifdef XDND /* was OFFIX */
1751 value = WMGetFromPLDictionary(clip_state, dDropCommand);
1752 if (value && WMIsPLString(value))
1753 icon->dnd_command = wstrdup(WMGetFromPLString(value));
1754 #endif
1756 value = WMGetFromPLDictionary(clip_state, dPasteCommand);
1757 if (value && WMIsPLString(value))
1758 icon->paste_command = wstrdup(WMGetFromPLString(value));
1760 WMReleasePropList(clip_state);
1762 return icon;
1765 WDock *wDockRestoreState(WScreen *scr, WMPropList *dock_state, int type)
1767 WDock *dock;
1768 WMPropList *apps;
1769 WMPropList *value;
1770 WAppIcon *aicon, *old_top;
1771 int count, i;
1773 dock = wDockCreate(scr, type, NULL);
1775 if (!dock_state)
1776 return dock;
1778 WMRetainPropList(dock_state);
1780 /* restore position */
1781 value = WMGetFromPLDictionary(dock_state, dPosition);
1782 if (value) {
1783 if (!WMIsPLString(value)) {
1784 COMPLAIN("Position");
1785 } else {
1786 if (sscanf(WMGetFromPLString(value), "%i,%i", &dock->x_pos, &dock->y_pos) != 2)
1787 COMPLAIN("Position");
1789 /* check position sanity */
1790 if (!onScreen(scr, dock->x_pos, dock->y_pos)) {
1791 int x = dock->x_pos;
1792 wScreenKeepInside(scr, &x, &dock->y_pos, ICON_SIZE, ICON_SIZE);
1795 /* Is this needed any more? */
1796 if (type == WM_CLIP) {
1797 if (dock->x_pos < 0) {
1798 dock->x_pos = 0;
1799 } else if (dock->x_pos > scr->scr_width - ICON_SIZE) {
1800 dock->x_pos = scr->scr_width - ICON_SIZE;
1802 } else {
1803 if (dock->x_pos >= 0) {
1804 dock->x_pos = DOCK_EXTRA_SPACE;
1805 dock->on_right_side = 0;
1806 } else {
1807 dock->x_pos = scr->scr_width - DOCK_EXTRA_SPACE - ICON_SIZE;
1808 dock->on_right_side = 1;
1814 /* restore lowered/raised state */
1815 dock->lowered = 0;
1817 value = WMGetFromPLDictionary(dock_state, dLowered);
1818 if (value) {
1819 if (!WMIsPLString(value)) {
1820 COMPLAIN("Lowered");
1821 } else {
1822 if (strcasecmp(WMGetFromPLString(value), "YES") == 0)
1823 dock->lowered = 1;
1827 /* restore collapsed state */
1828 dock->collapsed = 0;
1830 value = WMGetFromPLDictionary(dock_state, dCollapsed);
1831 if (value) {
1832 if (!WMIsPLString(value)) {
1833 COMPLAIN("Collapsed");
1834 } else {
1835 if (strcasecmp(WMGetFromPLString(value), "YES") == 0)
1836 dock->collapsed = 1;
1840 /* restore auto-collapsed state */
1841 value = WMGetFromPLDictionary(dock_state, dAutoCollapse);
1842 if (value) {
1843 if (!WMIsPLString(value)) {
1844 COMPLAIN("AutoCollapse");
1845 } else {
1846 if (strcasecmp(WMGetFromPLString(value), "YES") == 0) {
1847 dock->auto_collapse = 1;
1848 dock->collapsed = 1;
1853 /* restore auto-raise/lower state */
1854 value = WMGetFromPLDictionary(dock_state, dAutoRaiseLower);
1855 if (value) {
1856 if (!WMIsPLString(value)) {
1857 COMPLAIN("AutoRaiseLower");
1858 } else {
1859 if (strcasecmp(WMGetFromPLString(value), "YES") == 0)
1860 dock->auto_raise_lower = 1;
1864 /* restore attract icons state */
1865 dock->attract_icons = 0;
1867 value = WMGetFromPLDictionary(dock_state, dAutoAttractIcons);
1868 if (value) {
1869 if (!WMIsPLString(value)) {
1870 COMPLAIN("AutoAttractIcons");
1871 } else {
1872 if (strcasecmp(WMGetFromPLString(value), "YES") == 0)
1873 dock->attract_icons = 1;
1877 /* application list */
1880 WMPropList *tmp;
1881 char buffer[64];
1884 * When saving, it saves the dock state in
1885 * Applications and Applicationsnnn
1887 * When loading, it will first try Applicationsnnn.
1888 * If it does not exist, use Applications as default.
1891 snprintf(buffer, sizeof(buffer), "Applications%i", scr->scr_height);
1893 tmp = WMCreatePLString(buffer);
1894 apps = WMGetFromPLDictionary(dock_state, tmp);
1895 WMReleasePropList(tmp);
1897 if (!apps)
1898 apps = WMGetFromPLDictionary(dock_state, dApplications);
1901 if (!apps)
1902 goto finish;
1904 count = WMGetPropListItemCount(apps);
1905 if (count == 0)
1906 goto finish;
1908 old_top = dock->icon_array[0];
1910 /* dock->icon_count is set to 1 when dock is created.
1911 * Since Clip is already restored, we want to keep it so for clip,
1912 * but for dock we may change the default top tile, so we set it to 0.
1914 if (type == WM_DOCK)
1915 dock->icon_count = 0;
1917 for (i = 0; i < count; i++) {
1918 if (dock->icon_count >= dock->max_icons) {
1919 wwarning(_("there are too many icons stored in dock. Ignoring what doesn't fit"));
1920 break;
1923 value = WMGetFromPLArray(apps, i);
1924 aicon = restore_icon_state(scr, value, type, dock->icon_count);
1926 dock->icon_array[dock->icon_count] = aicon;
1928 if (aicon) {
1929 aicon->dock = dock;
1930 aicon->x_pos = dock->x_pos + (aicon->xindex * ICON_SIZE);
1931 aicon->y_pos = dock->y_pos + (aicon->yindex * ICON_SIZE);
1933 if (dock->lowered)
1934 ChangeStackingLevel(aicon->icon->core, WMNormalLevel);
1935 else
1936 ChangeStackingLevel(aicon->icon->core, WMDockLevel);
1938 wCoreConfigure(aicon->icon->core, aicon->x_pos, aicon->y_pos, 0, 0);
1939 if (!dock->collapsed)
1940 XMapWindow(dpy, aicon->icon->core->window);
1942 wRaiseFrame(aicon->icon->core);
1944 dock->icon_count++;
1945 } else if (dock->icon_count == 0 && type == WM_DOCK) {
1946 dock->icon_count++;
1950 /* if the first icon is not defined, use the default */
1951 if (dock->icon_array[0] == NULL) {
1952 /* update default icon */
1953 old_top->x_pos = dock->x_pos;
1954 old_top->y_pos = dock->y_pos;
1955 if (dock->lowered)
1956 ChangeStackingLevel(old_top->icon->core, WMNormalLevel);
1957 else
1958 ChangeStackingLevel(old_top->icon->core, WMDockLevel);
1960 dock->icon_array[0] = old_top;
1961 XMoveWindow(dpy, old_top->icon->core->window, dock->x_pos, dock->y_pos);
1962 /* we don't need to increment dock->icon_count here because it was
1963 * incremented in the loop above.
1965 } else if (old_top != dock->icon_array[0]) {
1966 if (old_top == scr->clip_icon) // TODO dande: understand the logic
1967 scr->clip_icon = dock->icon_array[0];
1969 wAppIconDestroy(old_top);
1972 finish:
1973 WMReleasePropList(dock_state);
1975 return dock;
1978 void wDockLaunchWithState(WDock *dock, WAppIcon *btn, WSavedState *state)
1980 if (btn && btn->command && !btn->running && !btn->launching) {
1981 btn->drop_launch = 0;
1982 btn->paste_launch = 0;
1984 btn->pid = execCommand(btn, btn->command, state);
1986 if (btn->pid > 0) {
1987 if (!btn->forced_dock && !btn->buggy_app) {
1988 btn->launching = 1;
1989 dockIconPaint(btn);
1992 } else {
1993 wfree(state);
1997 void wDockDoAutoLaunch(WDock *dock, int workspace)
1999 WAppIcon *btn;
2000 WSavedState *state;
2001 int i;
2003 for (i = 0; i < dock->max_icons; i++) {
2004 btn = dock->icon_array[i];
2005 if (!btn || !btn->auto_launch)
2006 continue;
2008 state = wmalloc(sizeof(WSavedState));
2009 state->workspace = workspace;
2010 /* TODO: this is klugy and is very difficult to understand
2011 * what's going on. Try to clean up */
2012 wDockLaunchWithState(dock, btn, state);
2016 #ifdef XDND /* was OFFIX */
2017 static WDock *findDock(WScreen *scr, XEvent *event, int *icon_pos)
2019 WDock *dock;
2020 int i;
2022 *icon_pos = -1;
2023 if ((dock = scr->dock) != NULL) {
2024 for (i = 0; i < dock->max_icons; i++) {
2025 if (dock->icon_array[i]
2026 && dock->icon_array[i]->icon->core->window == event->xclient.window) {
2027 *icon_pos = i;
2028 break;
2032 if (*icon_pos < 0 && (dock = w_global.workspace.array[w_global.workspace.current]->clip) != NULL) {
2033 for (i = 0; i < dock->max_icons; i++) {
2034 if (dock->icon_array[i]
2035 && dock->icon_array[i]->icon->core->window == event->xclient.window) {
2036 *icon_pos = i;
2037 break;
2041 if (*icon_pos >= 0)
2042 return dock;
2043 return NULL;
2046 int wDockReceiveDNDDrop(WScreen *scr, XEvent *event)
2048 WDock *dock;
2049 WAppIcon *btn;
2050 int icon_pos;
2052 dock = findDock(scr, event, &icon_pos);
2053 if (!dock)
2054 return False;
2057 * Return True if the drop was on an application icon window.
2058 * In this case, let the ClientMessage handler redirect the
2059 * message to the app.
2061 if (dock->icon_array[icon_pos]->icon->icon_win != None)
2062 return True;
2064 if (dock->icon_array[icon_pos]->dnd_command != NULL) {
2065 scr->flags.dnd_data_convertion_status = 0;
2067 btn = dock->icon_array[icon_pos];
2069 if (!btn->forced_dock) {
2070 btn->relaunching = btn->running;
2071 btn->running = 1;
2073 if (btn->wm_instance || btn->wm_class) {
2074 WWindowAttributes attr;
2075 memset(&attr, 0, sizeof(WWindowAttributes));
2076 wDefaultFillAttributes(btn->wm_instance, btn->wm_class, &attr, NULL, True);
2078 if (!attr.no_appicon)
2079 btn->launching = 1;
2080 else
2081 btn->running = 0;
2084 btn->paste_launch = 0;
2085 btn->drop_launch = 1;
2086 scr->last_dock = dock;
2087 btn->pid = execCommand(btn, btn->dnd_command, NULL);
2088 if (btn->pid > 0) {
2089 dockIconPaint(btn);
2090 } else {
2091 btn->launching = 0;
2092 if (!btn->relaunching)
2093 btn->running = 0;
2096 return False;
2098 #endif /* XDND */
2100 Bool wDockAttachIcon(WDock *dock, WAppIcon *icon, int x, int y, Bool update_icon)
2102 WWindow *wwin;
2103 Bool lupdate_icon = False;
2104 char *command = NULL;
2105 int index;
2107 icon->editing = 0;
2109 if (update_icon)
2110 lupdate_icon = True;
2112 if (icon->command == NULL) {
2113 /* If icon->owner exists, it means the application is running */
2114 if (icon->icon->owner) {
2115 wwin = icon->icon->owner;
2116 command = GetCommandForWindow(wwin->client_win);
2119 if (command) {
2120 icon->command = command;
2121 } else {
2122 /* icon->forced_dock = 1; */
2123 if (dock->type != WM_CLIP || !icon->attracted) {
2124 icon->editing = 1;
2125 if (wInputDialog(dock->screen_ptr, _("Dock Icon"),
2126 _("Type the command used to launch the application"), &command)) {
2127 if (command && (command[0] == 0 || (command[0] == '-' && command[1] == 0))) {
2128 wfree(command);
2129 command = NULL;
2131 icon->command = command;
2132 icon->editing = 0;
2133 } else {
2134 icon->editing = 0;
2135 if (command)
2136 wfree(command);
2137 /* If the target is the dock, reject the icon. If
2138 * the target is the clip, make it an attracted icon
2140 if (dock->type == WM_CLIP) {
2141 icon->attracted = 1;
2142 if (!icon->icon->shadowed) {
2143 icon->icon->shadowed = 1;
2144 lupdate_icon = True;
2146 } else {
2147 return False;
2154 for (index = 1; index < dock->max_icons; index++)
2155 if (dock->icon_array[index] == NULL)
2156 break;
2157 /* if (index == dock->max_icons)
2158 return; */
2160 assert(index < dock->max_icons);
2162 dock->icon_array[index] = icon;
2163 icon->yindex = y;
2164 icon->xindex = x;
2166 icon->omnipresent = 0;
2168 icon->x_pos = dock->x_pos + x * ICON_SIZE;
2169 icon->y_pos = dock->y_pos + y * ICON_SIZE;
2171 dock->icon_count++;
2173 icon->running = 1;
2174 icon->launching = 0;
2175 icon->docked = 1;
2176 icon->dock = dock;
2177 icon->icon->core->descriptor.handle_mousedown = iconMouseDown;
2178 icon->icon->core->descriptor.handle_enternotify = clipEnterNotify;
2179 icon->icon->core->descriptor.handle_leavenotify = clipLeaveNotify;
2180 icon->icon->core->descriptor.parent_type = WCLASS_DOCK_ICON;
2181 icon->icon->core->descriptor.parent = icon;
2183 MoveInStackListUnder(dock->icon_array[index - 1]->icon->core, icon->icon->core);
2184 wAppIconMove(icon, icon->x_pos, icon->y_pos);
2187 * Update icon pixmap, RImage doesn't change,
2188 * so call wIconUpdate is not needed
2190 if (lupdate_icon)
2191 update_icon_pixmap(icon->icon);
2193 /* Paint it */
2194 wAppIconPaint(icon);
2196 /* Save it */
2197 save_appicon(icon, True);
2199 if (wPreferences.auto_arrange_icons)
2200 wArrangeIcons(dock->screen_ptr, True);
2202 #ifdef XDND /* was OFFIX */
2203 if (icon->command && !icon->dnd_command) {
2204 int len = strlen(icon->command) + 8;
2205 icon->dnd_command = wmalloc(len);
2206 snprintf(icon->dnd_command, len, "%s %%d", icon->command);
2208 #endif
2210 if (icon->command && !icon->paste_command) {
2211 int len = strlen(icon->command) + 8;
2212 icon->paste_command = wmalloc(len);
2213 snprintf(icon->paste_command, len, "%s %%s", icon->command);
2216 return True;
2219 void wDockReattachIcon(WDock *dock, WAppIcon *icon, int x, int y)
2221 int index;
2223 for (index = 1; index < dock->max_icons; index++) {
2224 if (dock->icon_array[index] == icon)
2225 break;
2227 assert(index < dock->max_icons);
2229 icon->yindex = y;
2230 icon->xindex = x;
2232 icon->x_pos = dock->x_pos + x * ICON_SIZE;
2233 icon->y_pos = dock->y_pos + y * ICON_SIZE;
2236 Bool wDockMoveIconBetweenDocks(WDock *src, WDock *dest, WAppIcon *icon, int x, int y)
2238 WWindow *wwin;
2239 char *command = NULL;
2240 int index;
2241 Bool update_icon = False;
2243 if (src == dest)
2244 return True; /* No move needed, we're already there */
2246 if (dest == NULL)
2247 return False;
2250 * For the moment we can't do this if we move icons in Clip from one
2251 * workspace to other, because if we move two or more icons without
2252 * command, the dialog box will not be able to tell us to which of the
2253 * moved icons it applies. -Dan
2255 if ((dest->type == WM_DOCK /*|| dest->keep_attracted */ ) && icon->command == NULL) {
2256 /* If icon->owner exists, it means the application is running */
2257 if (icon->icon->owner) {
2258 wwin = icon->icon->owner;
2259 command = GetCommandForWindow(wwin->client_win);
2262 if (command) {
2263 icon->command = command;
2264 } else {
2265 icon->editing = 1;
2266 /* icon->forced_dock = 1; */
2267 if (wInputDialog(src->screen_ptr, _("Dock Icon"),
2268 _("Type the command used to launch the application"), &command)) {
2269 if (command && (command[0] == 0 || (command[0] == '-' && command[1] == 0))) {
2270 wfree(command);
2271 command = NULL;
2273 icon->command = command;
2274 } else {
2275 icon->editing = 0;
2276 if (command)
2277 wfree(command);
2278 return False;
2280 icon->editing = 0;
2284 if (dest->type == WM_DOCK || dest->type == WM_DRAWER)
2285 wClipMakeIconOmnipresent(icon, False);
2287 for (index = 1; index < src->max_icons; index++) {
2288 if (src->icon_array[index] == icon)
2289 break;
2291 assert(index < src->max_icons);
2293 src->icon_array[index] = NULL;
2294 src->icon_count--;
2296 for (index = 1; index < dest->max_icons; index++) {
2297 if (dest->icon_array[index] == NULL)
2298 break;
2301 assert(index < dest->max_icons);
2303 dest->icon_array[index] = icon;
2304 icon->dock = dest;
2306 /* deselect the icon */
2307 if (icon->icon->selected)
2308 wIconSelect(icon->icon);
2310 icon->icon->core->descriptor.handle_enternotify = clipEnterNotify;
2311 icon->icon->core->descriptor.handle_leavenotify = clipLeaveNotify;
2313 /* set it to be kept when moving to dock.
2314 * Unless the icon does not have a command set
2316 if (icon->command && (dest->type == WM_DOCK || dest->type == WM_DRAWER)) {
2317 icon->attracted = 0;
2318 if (icon->icon->shadowed) {
2319 icon->icon->shadowed = 0;
2320 update_icon = True;
2322 save_appicon(icon, True);
2325 if (src->auto_collapse || src->auto_raise_lower)
2326 clipLeave(src);
2328 icon->yindex = y;
2329 icon->xindex = x;
2331 icon->x_pos = dest->x_pos + x * ICON_SIZE;
2332 icon->y_pos = dest->y_pos + y * ICON_SIZE;
2334 dest->icon_count++;
2336 MoveInStackListUnder(dest->icon_array[index - 1]->icon->core, icon->icon->core);
2339 * Update icon pixmap, RImage doesn't change,
2340 * so call wIconUpdate is not needed
2342 if (update_icon)
2343 update_icon_pixmap(icon->icon);
2345 /* Paint it */
2346 wAppIconPaint(icon);
2348 return True;
2351 void wDockDetach(WDock *dock, WAppIcon *icon)
2353 int index;
2354 Bool update_icon = False;
2356 /* make the settings panel be closed */
2357 if (icon->panel)
2358 DestroyDockAppSettingsPanel(icon->panel);
2360 /* This must be called before icon->dock is set to NULL.
2361 * Don't move it. -Dan
2363 wClipMakeIconOmnipresent(icon, False);
2365 icon->docked = 0;
2366 icon->dock = NULL;
2367 icon->attracted = 0;
2368 icon->auto_launch = 0;
2369 if (icon->icon->shadowed) {
2370 icon->icon->shadowed = 0;
2371 update_icon = True;
2374 /* deselect the icon */
2375 if (icon->icon->selected)
2376 wIconSelect(icon->icon);
2378 if (icon->command) {
2379 wfree(icon->command);
2380 icon->command = NULL;
2382 #ifdef XDND /* was OFFIX */
2383 if (icon->dnd_command) {
2384 wfree(icon->dnd_command);
2385 icon->dnd_command = NULL;
2387 #endif
2388 if (icon->paste_command) {
2389 wfree(icon->paste_command);
2390 icon->paste_command = NULL;
2393 for (index = 1; index < dock->max_icons; index++)
2394 if (dock->icon_array[index] == icon)
2395 break;
2397 assert(index < dock->max_icons);
2398 dock->icon_array[index] = NULL;
2399 icon->yindex = -1;
2400 icon->xindex = -1;
2402 dock->icon_count--;
2404 /* if the dock is not attached to an application or
2405 * the application did not set the appropriate hints yet,
2406 * destroy the icon */
2407 if (!icon->running || !wApplicationOf(icon->main_window)) {
2408 wAppIconDestroy(icon);
2409 } else {
2410 icon->icon->core->descriptor.handle_mousedown = appIconMouseDown;
2411 icon->icon->core->descriptor.handle_enternotify = NULL;
2412 icon->icon->core->descriptor.handle_leavenotify = NULL;
2413 icon->icon->core->descriptor.parent_type = WCLASS_APPICON;
2414 icon->icon->core->descriptor.parent = icon;
2416 ChangeStackingLevel(icon->icon->core, NORMAL_ICON_LEVEL);
2419 * Update icon pixmap, RImage doesn't change,
2420 * so call wIconUpdate is not needed
2422 if (update_icon)
2423 update_icon_pixmap(icon->icon);
2425 /* Paint it */
2426 wAppIconPaint(icon);
2428 if (wPreferences.auto_arrange_icons)
2429 wArrangeIcons(dock->screen_ptr, True);
2431 if (dock->auto_collapse || dock->auto_raise_lower)
2432 clipLeave(dock);
2436 * returns the closest Dock slot index for the passed
2437 * coordinates.
2439 * Returns False if icon can't be docked.
2441 * Note: this function should NEVER alter ret_x or ret_y, unless it will
2442 * return True. -Dan
2444 /* Redocking == true means either icon->dock == dock (normal case)
2445 * or we are called from handleDockMove for a drawer */
2446 Bool wDockSnapIcon(WDock *dock, WAppIcon *icon, int req_x, int req_y, int *ret_x, int *ret_y, int redocking)
2448 WScreen *scr = dock->screen_ptr;
2449 int dx, dy;
2450 int ex_x, ex_y;
2451 int i, offset = ICON_SIZE / 2;
2452 WAppIcon *aicon = NULL;
2453 WAppIcon *nicon = NULL;
2455 if (wPreferences.flags.noupdates)
2456 return False;
2458 dx = dock->x_pos;
2459 dy = dock->y_pos;
2461 /* if the dock is full */
2462 if (!redocking && (dock->icon_count >= dock->max_icons))
2463 return False;
2465 /* exact position */
2466 if (req_y < dy)
2467 ex_y = (req_y - offset - dy) / ICON_SIZE;
2468 else
2469 ex_y = (req_y + offset - dy) / ICON_SIZE;
2471 if (req_x < dx)
2472 ex_x = (req_x - offset - dx) / ICON_SIZE;
2473 else
2474 ex_x = (req_x + offset - dx) / ICON_SIZE;
2476 /* check if the icon is outside the screen boundaries */
2477 if (!onScreen(scr, dx + ex_x * ICON_SIZE, dy + ex_y * ICON_SIZE))
2478 return False;
2480 switch (dock->type) {
2481 case WM_DOCK:
2482 /* We can return False right away if
2483 * - we do not come from this dock (which is a WM_DOCK),
2484 * - we are not right over it, and
2485 * - we are not the main tile of a drawer.
2486 * In the latter case, we are called from handleDockMove. */
2487 if (icon->dock != dock && ex_x != 0 &&
2488 !(icon->dock && icon->dock->type == WM_DRAWER && icon == icon->dock->icon_array[0]))
2489 return False;
2491 if (!redocking && ex_x != 0)
2492 return False;
2494 if (getDrawer(scr, ex_y)) /* Return false so that the drawer gets it. */
2495 return False;
2497 aicon = NULL;
2498 for (i = 0; i < dock->max_icons; i++) {
2499 nicon = dock->icon_array[i];
2500 if (nicon && nicon->yindex == ex_y) {
2501 aicon = nicon;
2502 break;
2506 if (redocking) {
2507 int sig, done, closest;
2509 /* Possible cases when redocking:
2511 * icon dragged out of range of any slot -> false
2512 * icon dragged on a drawer -> false (to open the drawer)
2513 * icon dragged to range of free slot
2514 * icon dragged to range of same slot
2515 * icon dragged to range of different icon
2517 if (abs(ex_x) > DOCK_DETTACH_THRESHOLD)
2518 return False;
2520 if (aicon == icon || !aicon) {
2521 *ret_x = 0;
2522 *ret_y = ex_y;
2523 return True;
2526 /* start looking at the upper slot or lower? */
2527 if (ex_y * ICON_SIZE < (req_y + offset - dy))
2528 sig = 1;
2529 else
2530 sig = -1;
2532 done = 0;
2533 /* look for closest free slot */
2534 for (i = 0; i < (DOCK_DETTACH_THRESHOLD + 1) * 2 && !done; i++) {
2535 int j;
2537 done = 1;
2538 closest = sig * (i / 2) + ex_y;
2539 /* check if this slot is fully on the screen and not used */
2540 if (onScreen(scr, dx, dy + closest * ICON_SIZE)) {
2541 for (j = 0; j < dock->max_icons; j++) {
2542 if (dock->icon_array[j]
2543 && dock->icon_array[j]->yindex == closest) {
2544 /* slot is used by someone else */
2545 if (dock->icon_array[j] != icon)
2546 done = 0;
2547 break;
2550 /* slot is used by a drawer */
2551 done = done && !getDrawer(scr, closest);
2553 else // !onScreen
2554 done = 0;
2555 sig = -sig;
2557 if (done &&
2558 ((ex_y >= closest && ex_y - closest < DOCK_DETTACH_THRESHOLD + 1)
2559 || (ex_y < closest && closest - ex_y <= DOCK_DETTACH_THRESHOLD + 1))) {
2560 *ret_x = 0;
2561 *ret_y = closest;
2562 return True;
2564 } else { /* !redocking */
2566 /* if slot is free and the icon is close enough, return it */
2567 if (!aicon && ex_x == 0) {
2568 *ret_x = 0;
2569 *ret_y = ex_y;
2570 return True;
2573 break;
2574 case WM_CLIP:
2576 int neighbours = 0;
2577 int start, stop, k;
2579 start = icon->omnipresent ? 0 : w_global.workspace.current;
2580 stop = icon->omnipresent ? w_global.workspace.count : start + 1;
2582 aicon = NULL;
2583 for (k = start; k < stop; k++) {
2584 WDock *tmp = w_global.workspace.array[k]->clip;
2585 if (!tmp)
2586 continue;
2587 for (i = 0; i < tmp->max_icons; i++) {
2588 nicon = tmp->icon_array[i];
2589 if (nicon && nicon->xindex == ex_x && nicon->yindex == ex_y) {
2590 aicon = nicon;
2591 break;
2594 if (aicon)
2595 break;
2597 for (k = start; k < stop; k++) {
2598 WDock *tmp = w_global.workspace.array[k]->clip;
2599 if (!tmp)
2600 continue;
2601 for (i = 0; i < tmp->max_icons; i++) {
2602 nicon = tmp->icon_array[i];
2603 if (nicon && nicon != icon && /* Icon can't be it's own neighbour */
2604 (abs(nicon->xindex - ex_x) <= CLIP_ATTACH_VICINITY &&
2605 abs(nicon->yindex - ex_y) <= CLIP_ATTACH_VICINITY)) {
2606 neighbours = 1;
2607 break;
2610 if (neighbours)
2611 break;
2614 if (neighbours && (aicon == NULL || (redocking && aicon == icon))) {
2615 *ret_x = ex_x;
2616 *ret_y = ex_y;
2617 return True;
2619 break;
2621 case WM_DRAWER:
2623 WAppIcon *aicons_to_shift[ dock->icon_count ];
2624 int index_of_hole, j;
2626 if (ex_y != 0 ||
2627 abs(ex_x) - dock->icon_count > DOCK_DETTACH_THRESHOLD ||
2628 (ex_x < 0 && !dock->on_right_side) ||
2629 (ex_x > 0 && dock->on_right_side)) {
2630 return False;
2633 if (ex_x == 0)
2634 ex_x = (dock->on_right_side ? -1 : 1);
2636 /* "Reduce" ex_x but keep its sign */
2637 if (redocking) {
2638 if (abs(ex_x) > dock->icon_count - 1) /* minus 1: do not take icon_array[0] into account */
2639 ex_x = ex_x * (dock->icon_count - 1) / abs(ex_x); /* don't use *= ! */
2640 } else {
2641 if (abs(ex_x) > dock->icon_count)
2642 ex_x = ex_x * dock->icon_count / abs(ex_x);
2644 index_of_hole = indexOfHole(dock, icon, redocking);
2646 /* Find the appicons between where icon was (index_of_hole) and where
2647 * it wants to be (ex_x) and slide them. */
2648 j = 0;
2649 for (i = 1; i < dock->max_icons; i++) {
2650 if ((aicon = dock->icon_array[ i ]) && aicon != icon &&
2651 ((ex_x <= aicon->xindex && aicon->xindex < index_of_hole) ||
2652 (index_of_hole < aicon->xindex && aicon->xindex <= ex_x)))
2653 aicons_to_shift[ j++ ] = aicon;
2655 assert(j == abs(ex_x - index_of_hole));
2657 wSlideAppicons(aicons_to_shift, j, (index_of_hole < ex_x));
2659 *ret_x = ex_x;
2660 *ret_y = ex_y;
2661 return True;
2664 return False;
2667 static int onScreen(WScreen *scr, int x, int y)
2669 WMRect rect;
2670 int flags;
2672 rect.pos.x = x;
2673 rect.pos.y = y;
2674 rect.size.width = rect.size.height = ICON_SIZE;
2676 wGetRectPlacementInfo(scr, rect, &flags);
2678 return !(flags & (XFLAG_DEAD | XFLAG_PARTIAL));
2682 * returns true if it can find a free slot in the dock,
2683 * in which case it changes x_pos and y_pos accordingly.
2684 * Else returns false.
2686 Bool wDockFindFreeSlot(WDock *dock, int *x_pos, int *y_pos)
2688 WScreen *scr = dock->screen_ptr;
2689 WAppIcon *btn;
2690 WAppIconChain *chain;
2691 unsigned char *slot_map;
2692 int mwidth;
2693 int r;
2694 int x, y;
2695 int i, done = False;
2696 int corner;
2697 int sx = 0, ex = scr->scr_width, ey = scr->scr_height;
2698 int extra_count = 0;
2700 if (dock->type == WM_DRAWER) {
2701 if (dock->icon_count >= dock->max_icons) { /* drawer is full */
2702 return False;
2704 *x_pos = dock->icon_count * (dock->on_right_side ? -1 : 1);
2705 *y_pos = 0;
2706 return True;
2709 if (dock->type == WM_CLIP && dock != w_global.workspace.array[w_global.workspace.current]->clip)
2710 extra_count = scr->global_icon_count;
2712 /* if the dock is full */
2713 if (dock->icon_count + extra_count >= dock->max_icons)
2714 return False;
2716 if (!wPreferences.flags.nodock && scr->dock) {
2717 if (scr->dock->on_right_side)
2718 ex -= ICON_SIZE + DOCK_EXTRA_SPACE;
2719 else
2720 sx += ICON_SIZE + DOCK_EXTRA_SPACE;
2723 if (ex < dock->x_pos)
2724 ex = dock->x_pos;
2725 if (sx > dock->x_pos + ICON_SIZE)
2726 sx = dock->x_pos + ICON_SIZE;
2727 #define C_NONE 0
2728 #define C_NW 1
2729 #define C_NE 2
2730 #define C_SW 3
2731 #define C_SE 4
2733 /* check if clip is in a corner */
2734 if (dock->type == WM_CLIP) {
2735 if (dock->x_pos < 1 && dock->y_pos < 1)
2736 corner = C_NE;
2737 else if (dock->x_pos < 1 && dock->y_pos >= (ey - ICON_SIZE))
2738 corner = C_SE;
2739 else if (dock->x_pos >= (ex - ICON_SIZE) && dock->y_pos >= (ey - ICON_SIZE))
2740 corner = C_SW;
2741 else if (dock->x_pos >= (ex - ICON_SIZE) && dock->y_pos < 1)
2742 corner = C_NW;
2743 else
2744 corner = C_NONE;
2745 } else {
2746 corner = C_NONE;
2749 /* If the clip is in the corner, use only slots that are in the border
2750 * of the screen */
2751 if (corner != C_NONE) {
2752 char *hmap, *vmap;
2753 int hcount, vcount;
2755 hcount = WMIN(dock->max_icons, scr->scr_width / ICON_SIZE);
2756 vcount = WMIN(dock->max_icons, scr->scr_height / ICON_SIZE);
2757 hmap = wmalloc(hcount + 1);
2758 vmap = wmalloc(vcount + 1);
2760 /* mark used positions */
2761 switch (corner) {
2762 case C_NE:
2763 for (i = 0; i < dock->max_icons; i++) {
2764 btn = dock->icon_array[i];
2765 if (!btn)
2766 continue;
2768 if (btn->xindex == 0 && btn->yindex > 0 && btn->yindex < vcount)
2769 vmap[btn->yindex] = 1;
2770 else if (btn->yindex == 0 && btn->xindex > 0 && btn->xindex < hcount)
2771 hmap[btn->xindex] = 1;
2773 for (chain = scr->global_icons; chain != NULL; chain = chain->next) {
2774 btn = chain->aicon;
2775 if (btn->xindex == 0 && btn->yindex > 0 && btn->yindex < vcount)
2776 vmap[btn->yindex] = 1;
2777 else if (btn->yindex == 0 && btn->xindex > 0 && btn->xindex < hcount)
2778 hmap[btn->xindex] = 1;
2780 break;
2781 case C_NW:
2782 for (i = 0; i < dock->max_icons; i++) {
2783 btn = dock->icon_array[i];
2784 if (!btn)
2785 continue;
2787 if (btn->xindex == 0 && btn->yindex > 0 && btn->yindex < vcount)
2788 vmap[btn->yindex] = 1;
2789 else if (btn->yindex == 0 && btn->xindex < 0 && btn->xindex > -hcount)
2790 hmap[-btn->xindex] = 1;
2792 for (chain = scr->global_icons; chain != NULL; chain = chain->next) {
2793 btn = chain->aicon;
2794 if (btn->xindex == 0 && btn->yindex > 0 && btn->yindex < vcount)
2795 vmap[btn->yindex] = 1;
2796 else if (btn->yindex == 0 && btn->xindex < 0 && btn->xindex > -hcount)
2797 hmap[-btn->xindex] = 1;
2799 break;
2800 case C_SE:
2801 for (i = 0; i < dock->max_icons; i++) {
2802 btn = dock->icon_array[i];
2803 if (!btn)
2804 continue;
2806 if (btn->xindex == 0 && btn->yindex < 0 && btn->yindex > -vcount)
2807 vmap[-btn->yindex] = 1;
2808 else if (btn->yindex == 0 && btn->xindex > 0 && btn->xindex < hcount)
2809 hmap[btn->xindex] = 1;
2811 for (chain = scr->global_icons; chain != NULL; chain = chain->next) {
2812 btn = chain->aicon;
2813 if (btn->xindex == 0 && btn->yindex < 0 && btn->yindex > -vcount)
2814 vmap[-btn->yindex] = 1;
2815 else if (btn->yindex == 0 && btn->xindex > 0 && btn->xindex < hcount)
2816 hmap[btn->xindex] = 1;
2818 break;
2819 case C_SW:
2820 default:
2821 for (i = 0; i < dock->max_icons; i++) {
2822 btn = dock->icon_array[i];
2823 if (!btn)
2824 continue;
2826 if (btn->xindex == 0 && btn->yindex < 0 && btn->yindex > -vcount)
2827 vmap[-btn->yindex] = 1;
2828 else if (btn->yindex == 0 && btn->xindex < 0 && btn->xindex > -hcount)
2829 hmap[-btn->xindex] = 1;
2831 for (chain = scr->global_icons; chain != NULL; chain = chain->next) {
2832 btn = chain->aicon;
2833 if (btn->xindex == 0 && btn->yindex < 0 && btn->yindex > -vcount)
2834 vmap[-btn->yindex] = 1;
2835 else if (btn->yindex == 0 && btn->xindex < 0 && btn->xindex > -hcount)
2836 hmap[-btn->xindex] = 1;
2839 x = 0;
2840 y = 0;
2841 done = 0;
2842 /* search a vacant slot */
2843 for (i = 1; i < WMAX(vcount, hcount); i++) {
2844 if (i < vcount && vmap[i] == 0) {
2845 /* found a slot */
2846 x = 0;
2847 y = i;
2848 done = 1;
2849 break;
2850 } else if (i < hcount && hmap[i] == 0) {
2851 /* found a slot */
2852 x = i;
2853 y = 0;
2854 done = 1;
2855 break;
2858 wfree(vmap);
2859 wfree(hmap);
2860 /* If found a slot, translate and return */
2861 if (done) {
2862 if (corner == C_NW || corner == C_NE)
2863 *y_pos = y;
2864 else
2865 *y_pos = -y;
2867 if (corner == C_NE || corner == C_SE)
2868 *x_pos = x;
2869 else
2870 *x_pos = -x;
2872 return True;
2874 /* else, try to find a slot somewhere else */
2877 /* a map of mwidth x mwidth would be enough if we allowed icons to be
2878 * placed outside of screen */
2879 mwidth = (int)ceil(sqrt(dock->max_icons));
2881 /* In the worst case (the clip is in the corner of the screen),
2882 * the amount of icons that fit in the clip is smaller.
2883 * Double the map to get a safe value.
2885 mwidth += mwidth;
2887 r = (mwidth - 1) / 2;
2889 slot_map = wmalloc(mwidth * mwidth);
2891 #define XY2OFS(x,y) (WMAX(abs(x),abs(y)) > r) ? 0 : (((y)+r)*(mwidth)+(x)+r)
2893 /* mark used slots in the map. If the slot falls outside the map
2894 * (for example, when all icons are placed in line), ignore them. */
2895 for (i = 0; i < dock->max_icons; i++) {
2896 btn = dock->icon_array[i];
2897 if (btn)
2898 slot_map[XY2OFS(btn->xindex, btn->yindex)] = 1;
2901 for (chain = scr->global_icons; chain != NULL; chain = chain->next)
2902 slot_map[XY2OFS(chain->aicon->xindex, chain->aicon->yindex)] = 1;
2904 /* Find closest slot from the center that is free by scanning the
2905 * map from the center to outward in circular passes.
2906 * This will not result in a neat layout, but will be optimal
2907 * in the sense that there will not be holes left.
2909 done = 0;
2910 for (i = 1; i <= r && !done; i++) {
2911 int tx, ty;
2913 /* top and bottom parts of the ring */
2914 for (x = -i; x <= i && !done; x++) {
2915 tx = dock->x_pos + x * ICON_SIZE;
2916 y = -i;
2917 ty = dock->y_pos + y * ICON_SIZE;
2918 if (slot_map[XY2OFS(x, y)] == 0 && onScreen(scr, tx, ty)) {
2919 *x_pos = x;
2920 *y_pos = y;
2921 done = 1;
2922 break;
2924 y = i;
2925 ty = dock->y_pos + y * ICON_SIZE;
2926 if (slot_map[XY2OFS(x, y)] == 0 && onScreen(scr, tx, ty)) {
2927 *x_pos = x;
2928 *y_pos = y;
2929 done = 1;
2930 break;
2933 /* left and right parts of the ring */
2934 for (y = -i + 1; y <= i - 1; y++) {
2935 ty = dock->y_pos + y * ICON_SIZE;
2936 x = -i;
2937 tx = dock->x_pos + x * ICON_SIZE;
2938 if (slot_map[XY2OFS(x, y)] == 0 && onScreen(scr, tx, ty)) {
2939 *x_pos = x;
2940 *y_pos = y;
2941 done = 1;
2942 break;
2944 x = i;
2945 tx = dock->x_pos + x * ICON_SIZE;
2946 if (slot_map[XY2OFS(x, y)] == 0 && onScreen(scr, tx, ty)) {
2947 *x_pos = x;
2948 *y_pos = y;
2949 done = 1;
2950 break;
2954 wfree(slot_map);
2955 #undef XY2OFS
2956 return done;
2959 static void moveDock(WDock *dock, int new_x, int new_y)
2961 WAppIcon *btn;
2962 WDrawerChain *dc;
2963 int i;
2965 if (dock->type == WM_DOCK) {
2966 for (dc = dock->screen_ptr->drawers; dc != NULL; dc = dc->next)
2967 moveDock(dc->adrawer, new_x, dc->adrawer->y_pos - dock->y_pos + new_y);
2970 dock->x_pos = new_x;
2971 dock->y_pos = new_y;
2972 for (i = 0; i < dock->max_icons; i++) {
2973 btn = dock->icon_array[i];
2974 if (btn) {
2975 btn->x_pos = new_x + btn->xindex * ICON_SIZE;
2976 btn->y_pos = new_y + btn->yindex * ICON_SIZE;
2977 XMoveWindow(dpy, btn->icon->core->window, btn->x_pos, btn->y_pos);
2982 static void swapDock(WDock *dock)
2984 WScreen *scr = dock->screen_ptr;
2985 WAppIcon *btn;
2986 int x, i;
2988 if (dock->on_right_side)
2989 x = dock->x_pos = scr->scr_width - ICON_SIZE - DOCK_EXTRA_SPACE;
2990 else
2991 x = dock->x_pos = DOCK_EXTRA_SPACE;
2993 swapDrawers(scr, dock->on_right_side, x);
2995 for (i = 0; i < dock->max_icons; i++) {
2996 btn = dock->icon_array[i];
2997 if (btn) {
2998 btn->x_pos = x;
2999 XMoveWindow(dpy, btn->icon->core->window, btn->x_pos, btn->y_pos);
3003 wScreenUpdateUsableArea(scr);
3006 static pid_t execCommand(WAppIcon *btn, const char *command, WSavedState *state)
3008 WScreen *scr = btn->icon->core->screen_ptr;
3009 pid_t pid;
3010 char **argv;
3011 int argc;
3012 char *cmdline;
3014 cmdline = ExpandOptions(scr, command);
3016 if (scr->flags.dnd_data_convertion_status || !cmdline) {
3017 if (cmdline)
3018 wfree(cmdline);
3019 if (state)
3020 wfree(state);
3021 return 0;
3024 wtokensplit(cmdline, &argv, &argc);
3026 if (!argc) {
3027 if (cmdline)
3028 wfree(cmdline);
3029 if (state)
3030 wfree(state);
3031 return 0;
3034 if ((pid = fork()) == 0) {
3035 char **args;
3036 int i;
3038 SetupEnvironment(scr);
3040 #ifdef HAVE_SETSID
3041 setsid();
3042 #endif
3044 args = malloc(sizeof(char *) * (argc + 1));
3045 if (!args)
3046 exit(111);
3048 for (i = 0; i < argc; i++)
3049 args[i] = argv[i];
3051 args[argc] = NULL;
3052 execvp(argv[0], args);
3053 exit(111);
3055 wtokenfree(argv, argc);
3057 if (pid > 0) {
3058 if (!state) {
3059 state = wmalloc(sizeof(WSavedState));
3060 state->hidden = -1;
3061 state->miniaturized = -1;
3062 state->shaded = -1;
3063 if (btn->dock == scr->dock || btn->dock->type == WM_DRAWER || btn->omnipresent)
3064 state->workspace = -1;
3065 else
3066 state->workspace = w_global.workspace.current;
3068 wWindowAddSavedState(btn->wm_instance, btn->wm_class, cmdline, pid, state);
3069 wAddDeathHandler(pid, (WDeathHandler *) trackDeadProcess, btn->dock);
3070 } else if (state) {
3071 wfree(state);
3073 wfree(cmdline);
3074 return pid;
3077 void wDockHideIcons(WDock *dock)
3079 int i;
3081 if (dock == NULL)
3082 return;
3084 for (i = 1; i < dock->max_icons; i++) {
3085 if (dock->icon_array[i])
3086 XUnmapWindow(dpy, dock->icon_array[i]->icon->core->window);
3088 dock->mapped = 0;
3090 dockIconPaint(dock->icon_array[0]);
3093 void wDockShowIcons(WDock *dock)
3095 int i;
3096 WAppIcon *btn;
3098 if (dock == NULL)
3099 return;
3101 btn = dock->icon_array[0];
3102 moveDock(dock, btn->x_pos, btn->y_pos);
3104 /* Deleting any change in stacking level, this function is now only about
3105 mapping icons */
3107 if (!dock->collapsed) {
3108 for (i = 1; i < dock->max_icons; i++) {
3109 if (dock->icon_array[i])
3110 XMapWindow(dpy, dock->icon_array[i]->icon->core->window);
3113 dock->mapped = 1;
3115 dockIconPaint(btn);
3118 void wDockLower(WDock *dock)
3120 int i;
3121 WDrawerChain *dc;
3123 if (dock->type == WM_DOCK) {
3124 for (dc = dock->screen_ptr->drawers; dc != NULL; dc = dc->next)
3125 wDockLower(dc->adrawer);
3127 for (i = 0; i < dock->max_icons; i++) {
3128 if (dock->icon_array[i])
3129 wLowerFrame(dock->icon_array[i]->icon->core);
3133 void wDockRaise(WDock *dock)
3135 int i;
3136 WDrawerChain *dc;
3138 for (i = dock->max_icons - 1; i >= 0; i--) {
3139 if (dock->icon_array[i])
3140 wRaiseFrame(dock->icon_array[i]->icon->core);
3142 if (dock->type == WM_DOCK) {
3143 for (dc = dock->screen_ptr->drawers; dc != NULL; dc = dc->next)
3144 wDockRaise(dc->adrawer);
3148 void wDockRaiseLower(WDock *dock)
3150 if (!dock->icon_array[0]->icon->core->stacking->above
3151 || (dock->icon_array[0]->icon->core->stacking->window_level
3152 != dock->icon_array[0]->icon->core->stacking->above->stacking->window_level))
3153 wDockLower(dock);
3154 else
3155 wDockRaise(dock);
3158 void wDockFinishLaunch(WDock *dock, WAppIcon *icon)
3160 icon->launching = 0;
3161 icon->relaunching = 0;
3162 dockIconPaint(icon);
3165 WAppIcon *wDockFindIconForWindow(WDock *dock, Window window)
3167 WAppIcon *icon;
3168 int i;
3170 for (i = 0; i < dock->max_icons; i++) {
3171 icon = dock->icon_array[i];
3172 if (icon && icon->main_window == window)
3173 return icon;
3175 return NULL;
3178 void wDockTrackWindowLaunch(WDock *dock, Window window)
3180 WAppIcon *icon;
3181 char *wm_class, *wm_instance;
3182 int i;
3183 Bool firstPass = True;
3184 Bool found = False;
3185 char *command = NULL;
3187 if (!PropGetWMClass(window, &wm_class, &wm_instance) || (!wm_class && !wm_instance))
3188 return;
3190 command = GetCommandForWindow(window);
3191 retry:
3192 for (i = 0; i < dock->max_icons; i++) {
3193 icon = dock->icon_array[i];
3194 if (!icon)
3195 continue;
3197 /* app is already attached to icon */
3198 if (icon->main_window == window) {
3199 found = True;
3200 break;
3203 if ((icon->wm_instance || icon->wm_class)
3204 && (icon->launching || !icon->running)) {
3206 if (icon->wm_instance && wm_instance && strcmp(icon->wm_instance, wm_instance) != 0)
3207 continue;
3209 if (icon->wm_class && wm_class && strcmp(icon->wm_class, wm_class) != 0)
3210 continue;
3212 if (firstPass && command && strcmp(icon->command, command) != 0)
3213 continue;
3215 if (!icon->relaunching) {
3216 WApplication *wapp;
3218 /* Possibly an application that was docked with dockit,
3219 * but the user did not update WMState to indicate that
3220 * it was docked by force */
3221 wapp = wApplicationOf(window);
3222 if (!wapp) {
3223 icon->forced_dock = 1;
3224 icon->running = 0;
3226 if (!icon->forced_dock)
3227 icon->main_window = window;
3229 found = True;
3230 if (!wPreferences.no_animations && !icon->launching &&
3231 !dock->screen_ptr->flags.startup && !dock->collapsed) {
3232 WAppIcon *aicon;
3233 int x0, y0;
3235 icon->launching = 1;
3236 dockIconPaint(icon);
3238 aicon = wAppIconCreateForDock(dock->screen_ptr, NULL,
3239 wm_instance, wm_class, TILE_NORMAL);
3240 /* XXX: can: aicon->icon == NULL ? */
3241 PlaceIcon(dock->screen_ptr, &x0, &y0, wGetHeadForWindow(aicon->icon->owner));
3242 wAppIconMove(aicon, x0, y0);
3243 /* Should this always be lowered? -Dan */
3244 if (dock->lowered)
3245 wLowerFrame(aicon->icon->core);
3246 XMapWindow(dpy, aicon->icon->core->window);
3247 aicon->launching = 1;
3248 wAppIconPaint(aicon);
3249 SlideWindow(aicon->icon->core->window, x0, y0, icon->x_pos, icon->y_pos);
3250 XUnmapWindow(dpy, aicon->icon->core->window);
3251 wAppIconDestroy(aicon);
3253 wDockFinishLaunch(dock, icon);
3254 break;
3258 if (firstPass && !found) {
3259 firstPass = False;
3260 goto retry;
3263 if (command)
3264 wfree(command);
3266 if (wm_class)
3267 free(wm_class);
3268 if (wm_instance)
3269 free(wm_instance);
3272 void wClipUpdateForWorkspaceChange(WScreen *scr, int workspace)
3274 if (!wPreferences.flags.noclip) {
3275 scr->clip_icon->dock = w_global.workspace.array[workspace]->clip;
3276 if (w_global.workspace.current != workspace) {
3277 WDock *old_clip = w_global.workspace.array[w_global.workspace.current]->clip;
3278 WAppIconChain *chain = scr->global_icons;
3280 while (chain) {
3281 wDockMoveIconBetweenDocks(chain->aicon->dock,
3282 w_global.workspace.array[workspace]->clip,
3283 chain->aicon, chain->aicon->xindex, chain->aicon->yindex);
3284 if (w_global.workspace.array[workspace]->clip->collapsed)
3285 XUnmapWindow(dpy, chain->aicon->icon->core->window);
3286 chain = chain->next;
3289 wDockHideIcons(old_clip);
3290 if (old_clip->auto_raise_lower) {
3291 if (old_clip->auto_raise_magic) {
3292 WMDeleteTimerHandler(old_clip->auto_raise_magic);
3293 old_clip->auto_raise_magic = NULL;
3295 wDockLower(old_clip);
3297 if (old_clip->auto_collapse) {
3298 if (old_clip->auto_expand_magic) {
3299 WMDeleteTimerHandler(old_clip->auto_expand_magic);
3300 old_clip->auto_expand_magic = NULL;
3302 old_clip->collapsed = 1;
3304 wDockShowIcons(w_global.workspace.array[workspace]->clip);
3309 static void trackDeadProcess(pid_t pid, unsigned char status, WDock *dock)
3311 WAppIcon *icon;
3312 int i;
3314 for (i = 0; i < dock->max_icons; i++) {
3315 icon = dock->icon_array[i];
3316 if (!icon)
3317 continue;
3319 if (icon->launching && icon->pid == pid) {
3320 if (!icon->relaunching) {
3321 icon->running = 0;
3322 icon->main_window = None;
3324 wDockFinishLaunch(dock, icon);
3325 icon->pid = 0;
3326 if (status == 111) {
3327 char msg[PATH_MAX];
3328 char *cmd;
3330 #ifdef XDND
3331 if (icon->drop_launch)
3332 cmd = icon->dnd_command;
3333 else
3334 #endif
3335 if (icon->paste_launch)
3336 cmd = icon->paste_command;
3337 else
3338 cmd = icon->command;
3340 snprintf(msg, sizeof(msg), _("Could not execute command \"%s\""), cmd);
3342 wMessageDialog(dock->screen_ptr, _("Error"), msg, _("OK"), NULL, NULL);
3344 break;
3349 /* This function is called when the dock switches state between
3350 * "normal" (including auto-raise/lower) and "keep on top". It is
3351 * therefore clearly distinct from wDockLower/Raise, which are called
3352 * each time a not-kept-on-top dock is lowered/raised. */
3353 static void toggleLowered(WDock *dock)
3355 WAppIcon *tmp;
3356 WDrawerChain *dc;
3357 int newlevel, i;
3359 if (!dock->lowered) {
3360 newlevel = WMNormalLevel;
3361 dock->lowered = 1;
3362 } else {
3363 newlevel = WMDockLevel;
3364 dock->lowered = 0;
3367 for (i = 0; i < dock->max_icons; i++) {
3368 tmp = dock->icon_array[i];
3369 if (!tmp)
3370 continue;
3372 ChangeStackingLevel(tmp->icon->core, newlevel);
3374 /* When the dock is no longer "on top", explicitly lower it as well.
3375 * It saves some CPU cycles (probably) to do it ourselves here
3376 * rather than calling wDockLower at the end of toggleLowered */
3377 if (dock->lowered)
3378 wLowerFrame(tmp->icon->core);
3381 if (dock->type == WM_DOCK) {
3382 for (dc = dock->screen_ptr->drawers; dc != NULL; dc = dc->next) {
3383 toggleLowered(dc->adrawer);
3385 wScreenUpdateUsableArea(dock->screen_ptr);
3389 static void toggleCollapsed(WDock *dock)
3391 if (dock->collapsed) {
3392 dock->collapsed = 0;
3393 wDockShowIcons(dock);
3394 } else {
3395 dock->collapsed = 1;
3396 wDockHideIcons(dock);
3400 static void openDockMenu(WDock *dock, WAppIcon *aicon, XEvent *event)
3402 WScreen *scr = dock->screen_ptr;
3403 WObjDescriptor *desc;
3404 WMenuEntry *entry;
3405 WApplication *wapp = NULL;
3406 int index = 0;
3407 int x_pos;
3408 int n_selected;
3409 int appIsRunning = aicon->running && aicon->icon && aicon->icon->owner;
3411 if (dock->type == WM_DOCK) {
3412 /* Dock position menu */
3413 updateDockPositionMenu(w_global.dock.pos_menu, dock);
3414 dock->menu->flags.realized = 0;
3415 if (!wPreferences.flags.nodrawer) {
3416 /* add a drawer */
3417 entry = dock->menu->entries[++index];
3418 entry->clientdata = aicon;
3419 wMenuSetEnabled(dock->menu, index, True);
3421 } else {
3422 /* clip/drawer options */
3423 if (w_global.clip.opt_menu)
3424 updateClipOptionsMenu(w_global.clip.opt_menu, dock);
3426 n_selected = numberOfSelectedIcons(dock);
3428 if (dock->type == WM_CLIP) {
3429 /* Rename Workspace */
3430 entry = dock->menu->entries[++index];
3431 if (aicon == scr->clip_icon) {
3432 entry->callback = renameCallback;
3433 entry->clientdata = dock;
3434 entry->flags.indicator = 0;
3435 entry->text = _("Rename Workspace");
3436 } else {
3437 entry->callback = omnipresentCallback;
3438 entry->clientdata = aicon;
3439 if (n_selected > 0) {
3440 entry->flags.indicator = 0;
3441 entry->text = _("Toggle Omnipresent");
3442 } else {
3443 entry->flags.indicator = 1;
3444 entry->flags.indicator_on = aicon->omnipresent;
3445 entry->flags.indicator_type = MI_CHECK;
3446 entry->text = _("Omnipresent");
3451 /* select/unselect icon */
3452 entry = dock->menu->entries[++index];
3453 entry->clientdata = aicon;
3454 entry->flags.indicator_on = aicon->icon->selected;
3455 wMenuSetEnabled(dock->menu, index, aicon != scr->clip_icon && !wIsADrawer(aicon));
3457 /* select/unselect all icons */
3458 entry = dock->menu->entries[++index];
3459 entry->clientdata = aicon;
3460 if (n_selected > 0)
3461 entry->text = _("Unselect All Icons");
3462 else
3463 entry->text = _("Select All Icons");
3465 wMenuSetEnabled(dock->menu, index, dock->icon_count > 1);
3467 /* keep icon(s) */
3468 entry = dock->menu->entries[++index];
3469 entry->clientdata = aicon;
3470 if (n_selected > 1)
3471 entry->text = _("Keep Icons");
3472 else
3473 entry->text = _("Keep Icon");
3475 wMenuSetEnabled(dock->menu, index, dock->icon_count > 1);
3477 if (dock->type == WM_CLIP) {
3478 /* this is the workspace submenu part */
3479 entry = dock->menu->entries[++index];
3480 if (n_selected > 1)
3481 entry->text = _("Move Icons To");
3482 else
3483 entry->text = _("Move Icon To");
3485 if (w_global.clip.submenu)
3486 updateWorkspaceMenu(w_global.clip.submenu, aicon);
3488 wMenuSetEnabled(dock->menu, index, !aicon->omnipresent);
3491 /* remove icon(s) */
3492 entry = dock->menu->entries[++index];
3493 entry->clientdata = aicon;
3494 if (n_selected > 1)
3495 entry->text = _("Remove Icons");
3496 else
3497 entry->text = _("Remove Icon");
3499 wMenuSetEnabled(dock->menu, index, dock->icon_count > 1);
3501 /* attract icon(s) */
3502 entry = dock->menu->entries[++index];
3503 entry->clientdata = aicon;
3505 dock->menu->flags.realized = 0;
3506 wMenuRealize(dock->menu);
3509 if (aicon->icon->owner)
3510 wapp = wApplicationOf(aicon->icon->owner->main_window);
3511 else
3512 wapp = NULL;
3514 /* launch */
3515 entry = dock->menu->entries[++index];
3516 entry->clientdata = aicon;
3517 wMenuSetEnabled(dock->menu, index, aicon->command != NULL);
3519 /* unhide here */
3520 entry = dock->menu->entries[++index];
3521 entry->clientdata = aicon;
3522 if (wapp && wapp->flags.hidden)
3523 entry->text = _("Unhide Here");
3524 else
3525 entry->text = _("Bring Here");
3527 wMenuSetEnabled(dock->menu, index, appIsRunning);
3529 /* hide */
3530 entry = dock->menu->entries[++index];
3531 entry->clientdata = aicon;
3532 if (wapp && wapp->flags.hidden)
3533 entry->text = _("Unhide");
3534 else
3535 entry->text = _("Hide");
3537 wMenuSetEnabled(dock->menu, index, appIsRunning);
3539 /* settings */
3540 entry = dock->menu->entries[++index];
3541 entry->clientdata = aicon;
3542 wMenuSetEnabled(dock->menu, index, !aicon->editing && !wPreferences.flags.noupdates);
3544 /* kill or remove drawer */
3545 entry = dock->menu->entries[++index];
3546 entry->clientdata = aicon;
3547 if (wIsADrawer(aicon)) {
3548 entry->callback = removeDrawerCallback;
3549 entry->text = _("Remove drawer");
3550 wMenuSetEnabled(dock->menu, index, True);
3551 } else {
3552 entry->callback = killCallback;
3553 entry->text = _("Kill");
3554 wMenuSetEnabled(dock->menu, index, appIsRunning);
3557 if (!dock->menu->flags.realized)
3558 wMenuRealize(dock->menu);
3560 if (dock->type == WM_CLIP || dock->type == WM_DRAWER) {
3561 /*x_pos = event->xbutton.x_root+2; */
3562 x_pos = event->xbutton.x_root - dock->menu->frame->core->width / 2 - 1;
3563 if (x_pos < 0) {
3564 x_pos = 0;
3565 } else if (x_pos + dock->menu->frame->core->width > scr->scr_width - 2) {
3566 x_pos = scr->scr_width - dock->menu->frame->core->width - 4;
3568 } else {
3569 x_pos = dock->on_right_side ? scr->scr_width - dock->menu->frame->core->width - 3 : 0;
3572 wMenuMapAt(dock->menu, x_pos, event->xbutton.y_root + 2, False);
3574 /* allow drag select */
3575 event->xany.send_event = True;
3576 desc = &dock->menu->menu->descriptor;
3577 (*desc->handle_mousedown) (desc, event);
3580 /******************************************************************/
3581 static void iconDblClick(WObjDescriptor *desc, XEvent *event)
3583 WAppIcon *btn = desc->parent;
3584 WDock *dock = btn->dock;
3585 WApplication *wapp = NULL;
3586 int unhideHere = 0;
3588 if (btn->icon->owner && !(event->xbutton.state & ControlMask)) {
3589 wapp = wApplicationOf(btn->icon->owner->main_window);
3591 assert(wapp != NULL);
3593 unhideHere = (event->xbutton.state & ShiftMask);
3595 /* go to the last workspace that the user worked on the app */
3596 if (wapp->last_workspace != w_global.workspace.current && !unhideHere)
3597 wWorkspaceChange(dock->screen_ptr, wapp->last_workspace);
3599 wUnhideApplication(wapp, event->xbutton.button == Button2, unhideHere);
3601 if (event->xbutton.state & MOD_MASK)
3602 wHideOtherApplications(btn->icon->owner);
3603 } else {
3604 if (event->xbutton.button == Button1) {
3605 if (event->xbutton.state & MOD_MASK) {
3606 /* raise/lower dock */
3607 toggleLowered(dock);
3608 } else if (btn == dock->screen_ptr->clip_icon) {
3609 if (getClipButton(event->xbutton.x, event->xbutton.y) != CLIP_IDLE)
3610 handleClipChangeWorkspace(dock->screen_ptr, event);
3611 else if (wPreferences.flags.clip_merged_in_dock) {
3612 // Is actually the dock
3613 if (btn->command)
3615 if (!btn->launching && (!btn->running || (event->xbutton.state & ControlMask)))
3616 launchDockedApplication(btn, False);
3618 else
3620 wShowInfoPanel(dock->screen_ptr);
3623 else
3624 toggleCollapsed(dock);
3625 } else if (wIsADrawer(btn)) {
3626 toggleCollapsed(dock);
3627 } else if (btn->command) {
3628 if (!btn->launching && (!btn->running || (event->xbutton.state & ControlMask)))
3629 launchDockedApplication(btn, False);
3630 } else if (btn->xindex == 0 && btn->yindex == 0 && btn->dock->type == WM_DOCK) {
3631 wShowInfoPanel(dock->screen_ptr);
3637 static void handleDockMove(WDock *dock, WAppIcon *aicon, XEvent *event)
3639 WScreen *scr = dock->screen_ptr;
3640 int ofs_x = event->xbutton.x, ofs_y = event->xbutton.y;
3641 WIcon *icon = aicon->icon;
3642 WAppIcon *tmpaicon;
3643 WDrawerChain *dc;
3644 int x = aicon->x_pos, y = aicon->y_pos;;
3645 int shad_x = x, shad_y = y;
3646 XEvent ev;
3647 int grabbed = 0, done, previously_on_right, now_on_right, previous_x_pos, i;
3648 Pixmap ghost = None;
3649 int superfluous = wPreferences.superfluous; /* we catch it to avoid problems */
3651 if (XGrabPointer(dpy, aicon->icon->core->window, True, ButtonMotionMask
3652 | ButtonReleaseMask | ButtonPressMask, GrabModeAsync,
3653 GrabModeAsync, None, None, CurrentTime) != GrabSuccess)
3654 wwarning("pointer grab failed for dock move");
3656 if (dock->type == WM_DRAWER) {
3657 Window wins[2];
3658 wins[0] = icon->core->window;
3659 wins[1] = scr->dock_shadow;
3660 XRestackWindows(dpy, wins, 2);
3661 XMoveResizeWindow(dpy, scr->dock_shadow, aicon->x_pos, aicon->y_pos,
3662 ICON_SIZE, ICON_SIZE);
3663 if (superfluous) {
3664 if (icon->pixmap!=None)
3665 ghost = MakeGhostIcon(scr, icon->pixmap);
3666 else
3667 ghost = MakeGhostIcon(scr, icon->core->window);
3669 XSetWindowBackgroundPixmap(dpy, scr->dock_shadow, ghost);
3670 XClearWindow(dpy, scr->dock_shadow);
3672 XMapWindow(dpy, scr->dock_shadow);
3675 previously_on_right = now_on_right = dock->on_right_side;
3676 previous_x_pos = dock->x_pos;
3677 done = 0;
3678 while (!done) {
3679 WMMaskEvent(dpy, PointerMotionMask | ButtonReleaseMask | ButtonPressMask
3680 | ButtonMotionMask | ExposureMask | EnterWindowMask, &ev);
3681 switch (ev.type) {
3682 case Expose:
3683 WMHandleEvent(&ev);
3684 break;
3686 case EnterNotify:
3687 /* It means the cursor moved so fast that it entered
3688 * something else (if moving slowly, it would have
3689 * stayed in the dock that is being moved. Ignore such
3690 * "spurious" EnterNotifiy's */
3691 break;
3693 case MotionNotify:
3694 if (!grabbed) {
3695 if (abs(ofs_x - ev.xmotion.x) >= MOVE_THRESHOLD
3696 || abs(ofs_y - ev.xmotion.y) >= MOVE_THRESHOLD) {
3697 XChangeActivePointerGrab(dpy, ButtonMotionMask
3698 | ButtonReleaseMask | ButtonPressMask,
3699 wPreferences.cursor[WCUR_MOVE], CurrentTime);
3700 grabbed = 1;
3702 break;
3704 switch (dock->type) {
3705 case WM_CLIP:
3706 x = ev.xmotion.x_root - ofs_x;
3707 y = ev.xmotion.y_root - ofs_y;
3708 wScreenKeepInside(scr, &x, &y, ICON_SIZE, ICON_SIZE);
3709 moveDock(dock, x, y);
3710 break;
3711 case WM_DOCK:
3712 x = ev.xmotion.x_root - ofs_x;
3713 y = ev.xmotion.y_root - ofs_y;
3714 if (previously_on_right)
3716 now_on_right = (ev.xmotion.x_root >= previous_x_pos - ICON_SIZE);
3718 else
3720 now_on_right = (ev.xmotion.x_root > previous_x_pos + ICON_SIZE * 2);
3722 if (now_on_right != dock->on_right_side)
3724 dock->on_right_side = now_on_right;
3725 swapDock(dock);
3726 wArrangeIcons(scr, False);
3728 // Also perform the vertical move
3729 wScreenKeepInside(scr, &x, &y, ICON_SIZE, ICON_SIZE);
3730 moveDock(dock, dock->x_pos, y);
3731 if (wPreferences.flags.wrap_appicons_in_dock)
3733 for (i = 0; i < dock->max_icons; i++) {
3734 int new_y, new_index, j, ok;
3735 if ((tmpaicon = dock->icon_array[i]) == NULL)
3736 continue;
3737 if (onScreen(scr, tmpaicon->x_pos, tmpaicon->y_pos))
3738 continue;
3739 new_y = (tmpaicon->y_pos + ICON_SIZE * dock->max_icons) % (ICON_SIZE * dock->max_icons);
3740 new_index = (new_y - dock->y_pos) / ICON_SIZE;
3741 if (!onScreen(scr, tmpaicon->x_pos, new_y))
3742 continue;
3743 ok = 1;
3744 for (j = 0; j < dock->max_icons; j++)
3746 if (dock->icon_array[j] != NULL &&
3747 dock->icon_array[j]->yindex == new_index)
3749 ok = 0;
3750 break;
3753 if (!ok || getDrawer(scr, new_index) != NULL)
3754 continue;
3755 wDockReattachIcon(dock, tmpaicon, tmpaicon->xindex, new_index);
3757 for (dc = scr->drawers; dc != NULL; dc = dc->next)
3759 int new_y, new_index, j, ok;
3760 tmpaicon = dc->adrawer->icon_array[0];
3761 if (onScreen(scr, tmpaicon->x_pos, tmpaicon->y_pos))
3762 continue;
3763 new_y = (tmpaicon->y_pos + ICON_SIZE * dock->max_icons) % (ICON_SIZE * dock->max_icons);
3764 new_index = (new_y - dock->y_pos) / ICON_SIZE;
3765 if (!onScreen(scr, tmpaicon->x_pos, new_y))
3766 continue;
3767 ok = 1;
3768 for (j = 0; j < dock->max_icons; j++)
3770 if (dock->icon_array[j] != NULL &&
3771 dock->icon_array[j]->yindex == new_index)
3773 ok = 0;
3774 break;
3777 if (!ok || getDrawer(scr, new_index) != NULL)
3778 continue;
3779 moveDock(dc->adrawer, tmpaicon->x_pos, new_y);
3782 break;
3783 case WM_DRAWER:
3785 WDock *real_dock = dock->screen_ptr->dock;
3786 Bool snapped;
3787 int ix, iy;
3788 x = ev.xmotion.x_root - ofs_x;
3789 y = ev.xmotion.y_root - ofs_y;
3790 snapped = wDockSnapIcon(real_dock, aicon, x, y, &ix, &iy, True);
3791 if (snapped) {
3792 shad_x = real_dock->x_pos + ix * wPreferences.icon_size;
3793 shad_y = real_dock->y_pos + iy * wPreferences.icon_size;
3794 XMoveWindow(dpy, scr->dock_shadow, shad_x, shad_y);
3796 moveDock(dock, x, y);
3797 break;
3800 break;
3802 case ButtonPress:
3803 break;
3805 case ButtonRelease:
3806 if (ev.xbutton.button != event->xbutton.button)
3807 break;
3808 XUngrabPointer(dpy, CurrentTime);
3809 if (dock->type == WM_DRAWER) {
3810 Window *wins[dock->icon_count];
3812 for (i = 0; i < dock->max_icons; i++) {
3813 if ((tmpaicon = dock->icon_array[i]) == NULL)
3814 continue;
3815 wins[ tmpaicon->xindex + (dock->on_right_side ? dock->icon_count - 1 : 0) ] = &tmpaicon->icon->core->window;
3817 SlideWindows(wins, dock->icon_count,
3818 (dock->on_right_side ? x - (dock->icon_count - 1) * ICON_SIZE : x),
3820 (dock->on_right_side ? shad_x - (dock->icon_count - 1) * ICON_SIZE : shad_x),
3821 shad_y);
3822 XUnmapWindow(dpy, scr->dock_shadow);
3823 moveDock(dock, shad_x, shad_y);
3824 XResizeWindow(dpy, scr->dock_shadow, ICON_SIZE, ICON_SIZE);
3826 done = 1;
3827 break;
3830 if (superfluous) {
3831 if (ghost != None)
3832 XFreePixmap(dpy, ghost);
3833 XSetWindowBackground(dpy, scr->dock_shadow, scr->white_pixel);
3838 static int getClipButton(int px, int py)
3840 int pt = (CLIP_BUTTON_SIZE + 2) * ICON_SIZE / 64;
3842 if (px < 0 || py < 0 || px >= ICON_SIZE || py >= ICON_SIZE)
3843 return CLIP_IDLE;
3845 if (py <= pt - ((int)ICON_SIZE - 1 - px))
3846 return CLIP_FORWARD;
3847 else if (px <= pt - ((int)ICON_SIZE - 1 - py))
3848 return CLIP_REWIND;
3850 return CLIP_IDLE;
3853 static void handleClipChangeWorkspace(WScreen *scr, XEvent *event)
3855 XEvent ev;
3856 int done, direction, new_ws;
3857 int new_dir;
3858 WDock *clip = scr->clip_icon->dock;
3860 direction = getClipButton(event->xbutton.x, event->xbutton.y);
3862 clip->lclip_button_pushed = direction == CLIP_REWIND;
3863 clip->rclip_button_pushed = direction == CLIP_FORWARD;
3865 wClipIconPaint(scr->clip_icon);
3866 done = 0;
3867 while (!done) {
3868 WMMaskEvent(dpy, ExposureMask | ButtonMotionMask | ButtonReleaseMask | ButtonPressMask, &ev);
3869 switch (ev.type) {
3870 case Expose:
3871 WMHandleEvent(&ev);
3872 break;
3874 case MotionNotify:
3875 new_dir = getClipButton(ev.xmotion.x, ev.xmotion.y);
3876 if (new_dir != direction) {
3877 direction = new_dir;
3878 clip->lclip_button_pushed = direction == CLIP_REWIND;
3879 clip->rclip_button_pushed = direction == CLIP_FORWARD;
3880 wClipIconPaint(scr->clip_icon);
3882 break;
3884 case ButtonPress:
3885 break;
3887 case ButtonRelease:
3888 if (ev.xbutton.button == event->xbutton.button)
3889 done = 1;
3893 clip->lclip_button_pushed = 0;
3894 clip->rclip_button_pushed = 0;
3896 new_ws = wPreferences.ws_advance || (event->xbutton.state & ControlMask);
3898 if (direction == CLIP_FORWARD) {
3899 if (w_global.workspace.current < w_global.workspace.count - 1)
3900 wWorkspaceChange(scr, w_global.workspace.current + 1);
3901 else if (new_ws && w_global.workspace.current < MAX_WORKSPACES - 1)
3902 wWorkspaceChange(scr, w_global.workspace.current + 1);
3903 else if (wPreferences.ws_cycle)
3904 wWorkspaceChange(scr, 0);
3905 } else if (direction == CLIP_REWIND) {
3906 if (w_global.workspace.current > 0)
3907 wWorkspaceChange(scr, w_global.workspace.current - 1);
3908 else if (w_global.workspace.current == 0 && wPreferences.ws_cycle)
3909 wWorkspaceChange(scr, w_global.workspace.count - 1);
3912 wClipIconPaint(scr->clip_icon);
3915 static void iconMouseDown(WObjDescriptor *desc, XEvent *event)
3917 WAppIcon *aicon = desc->parent;
3918 WDock *dock = aicon->dock;
3919 WScreen *scr = aicon->icon->core->screen_ptr;
3921 if (aicon->editing || WCHECK_STATE(WSTATE_MODAL))
3922 return;
3924 scr->last_dock = dock;
3926 if (dock->menu->flags.mapped)
3927 wMenuUnmap(dock->menu);
3929 if (IsDoubleClick(scr, event)) {
3930 /* double-click was not in the main clip icon */
3931 if (dock->type != WM_CLIP || aicon->xindex != 0 || aicon->yindex != 0
3932 || getClipButton(event->xbutton.x, event->xbutton.y) == CLIP_IDLE) {
3933 iconDblClick(desc, event);
3934 return;
3938 if (event->xbutton.button == Button1) {
3939 if (event->xbutton.state & MOD_MASK)
3940 wDockLower(dock);
3941 else
3942 wDockRaise(dock);
3944 if ((event->xbutton.state & ShiftMask) && aicon != scr->clip_icon && dock->type != WM_DOCK) {
3945 wIconSelect(aicon->icon);
3946 return;
3949 if (aicon->yindex == 0 && aicon->xindex == 0) {
3950 if (getClipButton(event->xbutton.x, event->xbutton.y) != CLIP_IDLE &&
3951 (dock->type == WM_CLIP || (dock->type == WM_DOCK && wPreferences.flags.clip_merged_in_dock)))
3952 handleClipChangeWorkspace(scr, event);
3953 else
3954 handleDockMove(dock, aicon, event);
3955 } else {
3956 Bool hasMoved = wHandleAppIconMove(aicon, event);
3957 if (wPreferences.single_click && !hasMoved)
3958 iconDblClick(desc, event);
3960 } else if (event->xbutton.button == Button2 && aicon == scr->clip_icon) {
3961 if (!w_global.clip.ws_menu)
3962 w_global.clip.ws_menu = wWorkspaceMenuMake(scr, False);
3964 if (w_global.clip.ws_menu) {
3965 WMenu *wsMenu = w_global.clip.ws_menu;
3966 int xpos;
3968 wWorkspaceMenuUpdate(wsMenu);
3970 xpos = event->xbutton.x_root - wsMenu->frame->core->width / 2 - 1;
3971 if (xpos < 0) {
3972 xpos = 0;
3973 } else if (xpos + wsMenu->frame->core->width > scr->scr_width - 2) {
3974 xpos = scr->scr_width - wsMenu->frame->core->width - 4;
3976 wMenuMapAt(wsMenu, xpos, event->xbutton.y_root + 2, False);
3978 desc = &wsMenu->menu->descriptor;
3979 event->xany.send_event = True;
3980 (*desc->handle_mousedown) (desc, event);
3982 } else if (event->xbutton.button == Button2 && dock->type == WM_CLIP &&
3983 (event->xbutton.state & ShiftMask) && aicon != scr->clip_icon) {
3984 wClipMakeIconOmnipresent(aicon, !aicon->omnipresent);
3985 } else if (event->xbutton.button == Button3) {
3986 if (event->xbutton.send_event &&
3987 XGrabPointer(dpy, aicon->icon->core->window, True, ButtonMotionMask
3988 | ButtonReleaseMask | ButtonPressMask, GrabModeAsync,
3989 GrabModeAsync, None, None, CurrentTime) != GrabSuccess) {
3990 wwarning("pointer grab failed for dockicon menu");
3991 return;
3994 openDockMenu(dock, aicon, event);
3995 } else if (event->xbutton.button == Button2) {
3996 WAppIcon *btn = desc->parent;
3998 if (!btn->launching && (!btn->running || (event->xbutton.state & ControlMask)))
3999 launchDockedApplication(btn, True);
4003 static void clipEnterNotify(WObjDescriptor *desc, XEvent *event)
4005 WAppIcon *btn = (WAppIcon *) desc->parent;
4006 WDock *dock, *tmp;
4007 WScreen *scr;
4009 assert(event->type == EnterNotify);
4011 if (desc->parent_type != WCLASS_DOCK_ICON)
4012 return;
4014 scr = btn->icon->core->screen_ptr;
4015 dock = btn->dock;
4017 if (dock == NULL)
4018 return;
4020 /* The auto raise/lower code */
4021 tmp = (dock->type == WM_DRAWER ? scr->dock : dock);
4022 if (tmp->auto_lower_magic) {
4023 WMDeleteTimerHandler(tmp->auto_lower_magic);
4024 tmp->auto_lower_magic = NULL;
4026 if (tmp->auto_raise_lower && !tmp->auto_raise_magic)
4027 tmp->auto_raise_magic = WMAddTimerHandler(wPreferences.clip_auto_raise_delay, clipAutoRaise, (void *) tmp);
4029 if (dock->type != WM_CLIP && dock->type != WM_DRAWER)
4030 return;
4032 /* The auto expand/collapse code */
4033 if (dock->auto_collapse_magic) {
4034 WMDeleteTimerHandler(dock->auto_collapse_magic);
4035 dock->auto_collapse_magic = NULL;
4037 if (dock->auto_collapse && !dock->auto_expand_magic)
4038 dock->auto_expand_magic = WMAddTimerHandler(wPreferences.clip_auto_expand_delay, clipAutoExpand, (void *)dock);
4041 static void clipLeave(WDock *dock)
4043 XEvent event;
4044 WObjDescriptor *desc = NULL;
4045 WDock *tmp;
4047 if (dock == NULL)
4048 return;
4050 if (XCheckTypedEvent(dpy, EnterNotify, &event) != False) {
4051 if (XFindContext(dpy, event.xcrossing.window, w_global.context.client_win,
4052 (XPointer *) & desc) != XCNOENT
4053 && desc && desc->parent_type == WCLASS_DOCK_ICON
4054 && ((WAppIcon *) desc->parent)->dock == dock) {
4055 /* We haven't left the dock/clip/drawer yet */
4056 XPutBackEvent(dpy, &event);
4057 return;
4060 XPutBackEvent(dpy, &event);
4061 } else {
4062 /* We entered a withdrawn window, so we're still in Clip */
4063 return;
4066 tmp = (dock->type == WM_DRAWER ? dock->screen_ptr->dock : dock);
4067 if (tmp->auto_raise_magic) {
4068 WMDeleteTimerHandler(tmp->auto_raise_magic);
4069 tmp->auto_raise_magic = NULL;
4071 if (tmp->auto_raise_lower && !tmp->auto_lower_magic)
4072 tmp->auto_lower_magic = WMAddTimerHandler(wPreferences.clip_auto_lower_delay, clipAutoLower, (void *)tmp);
4074 if (dock->type != WM_CLIP && dock->type != WM_DRAWER)
4075 return;
4077 if (dock->auto_expand_magic) {
4078 WMDeleteTimerHandler(dock->auto_expand_magic);
4079 dock->auto_expand_magic = NULL;
4081 if (dock->auto_collapse && !dock->auto_collapse_magic)
4082 dock->auto_collapse_magic = WMAddTimerHandler(wPreferences.clip_auto_collapse_delay, clipAutoCollapse, (void *)dock);
4085 static void clipLeaveNotify(WObjDescriptor *desc, XEvent *event)
4087 WAppIcon *btn = (WAppIcon *) desc->parent;
4089 assert(event->type == LeaveNotify);
4091 if (desc->parent_type != WCLASS_DOCK_ICON)
4092 return;
4094 clipLeave(btn->dock);
4097 static void clipAutoCollapse(void *cdata)
4099 WDock *dock = (WDock *) cdata;
4101 if (dock->type != WM_CLIP && dock->type != WM_DRAWER)
4102 return;
4104 if (dock->auto_collapse) {
4105 dock->collapsed = 1;
4106 wDockHideIcons(dock);
4108 dock->auto_collapse_magic = NULL;
4111 static void clipAutoExpand(void *cdata)
4113 WDock *dock = (WDock *) cdata;
4115 if (dock->type != WM_CLIP && dock->type != WM_DRAWER)
4116 return;
4118 if (dock->auto_collapse) {
4119 dock->collapsed = 0;
4120 wDockShowIcons(dock);
4122 dock->auto_expand_magic = NULL;
4125 static void clipAutoLower(void *cdata)
4127 WDock *dock = (WDock *) cdata;
4129 if (dock->auto_raise_lower)
4130 wDockLower(dock);
4132 dock->auto_lower_magic = NULL;
4135 static void clipAutoRaise(void *cdata)
4137 WDock *dock = (WDock *) cdata;
4139 if (dock->auto_raise_lower)
4140 wDockRaise(dock);
4142 dock->auto_raise_magic = NULL;
4145 static Bool iconCanBeOmnipresent(WAppIcon *aicon)
4147 WScreen *scr = aicon->icon->core->screen_ptr;
4148 WDock *clip;
4149 WAppIcon *btn;
4150 int i, j;
4152 for (i = 0; i < w_global.workspace.count; i++) {
4153 clip = w_global.workspace.array[i]->clip;
4155 if (clip == aicon->dock)
4156 continue;
4158 if (clip->icon_count + scr->global_icon_count >= clip->max_icons)
4159 return False; /* Clip is full in some workspace */
4161 for (j = 0; j < clip->max_icons; j++) {
4162 btn = clip->icon_array[j];
4163 if (btn && btn->xindex == aicon->xindex && btn->yindex == aicon->yindex)
4164 return False;
4168 return True;
4171 int wClipMakeIconOmnipresent(WAppIcon *aicon, int omnipresent)
4173 WScreen *scr = aicon->icon->core->screen_ptr;
4174 WAppIconChain *new_entry, *tmp, *tmp1;
4175 int status = WO_SUCCESS;
4177 if ((scr->dock && aicon->dock == scr->dock) || aicon == scr->clip_icon)
4178 return WO_NOT_APPLICABLE;
4180 if (aicon->omnipresent == omnipresent)
4181 return WO_SUCCESS;
4183 if (omnipresent) {
4184 if (iconCanBeOmnipresent(aicon)) {
4185 aicon->omnipresent = 1;
4186 new_entry = wmalloc(sizeof(WAppIconChain));
4187 new_entry->aicon = aicon;
4188 new_entry->next = scr->global_icons;
4189 scr->global_icons = new_entry;
4190 scr->global_icon_count++;
4191 } else {
4192 aicon->omnipresent = 0;
4193 status = WO_FAILED;
4195 } else {
4196 aicon->omnipresent = 0;
4197 if (aicon == scr->global_icons->aicon) {
4198 tmp = scr->global_icons->next;
4199 wfree(scr->global_icons);
4200 scr->global_icons = tmp;
4201 scr->global_icon_count--;
4202 } else {
4203 tmp = scr->global_icons;
4204 while (tmp->next) {
4205 if (tmp->next->aicon == aicon) {
4206 tmp1 = tmp->next->next;
4207 wfree(tmp->next);
4208 tmp->next = tmp1;
4209 scr->global_icon_count--;
4210 break;
4212 tmp = tmp->next;
4217 wAppIconPaint(aicon);
4219 return status;
4222 static void drawerAppendToChain(WScreen *scr, WDock *drawer)
4224 WDrawerChain **where_to_add;
4226 where_to_add = &scr->drawers;
4227 while ((*where_to_add) != NULL) {
4228 where_to_add = &(*where_to_add)->next;
4231 *where_to_add = wmalloc(sizeof(WDrawerChain));
4232 (*where_to_add)->adrawer = drawer;
4233 (*where_to_add)->next = NULL;
4234 scr->drawer_count++;
4238 static void drawerRemoveFromChain(WScreen *scr, WDock *drawer)
4240 WDrawerChain *next, **to_remove;
4242 to_remove = &scr->drawers;
4243 while (True) {
4244 if (*to_remove == NULL) {
4245 wwarning("The drawer to be removed can not be found.");
4246 return;
4248 if ((*to_remove)->adrawer == drawer)
4249 break;
4251 to_remove = &(*to_remove)->next;
4253 next = (*to_remove)->next;
4254 wfree(*to_remove);
4255 *to_remove = next;
4256 scr->drawer_count--;
4260 /* Don't free the returned string. Duplicate it. */
4261 static char * findUniqueName(WScreen *scr, const char *instance_basename)
4263 static char buffer[128];
4264 WDrawerChain *dc;
4265 int i;
4266 Bool already_in_use = True;
4268 #define UNIQUE_NAME_WATCHDOG 128
4269 for (i = 0; already_in_use && i < UNIQUE_NAME_WATCHDOG; i++) {
4270 snprintf(buffer, sizeof buffer, "%s%d", instance_basename, i);
4272 already_in_use = False;
4274 for (dc = scr->drawers; dc != NULL; dc = dc->next) {
4275 if (!strncmp(dc->adrawer->icon_array[0]->wm_instance, buffer,
4276 sizeof buffer)) {
4277 already_in_use = True;
4278 break;
4283 if (i == UNIQUE_NAME_WATCHDOG)
4284 wwarning("Couldn't find a unique name for drawer in %d attempts.", i);
4285 #undef UNIQUE_NAME_WATCHDOG
4287 return buffer;
4291 static void drawerIconExpose(WObjDescriptor *desc, XEvent *event)
4293 wDrawerIconPaint((WAppIcon *) desc->parent);
4297 static int addADrawer(WScreen *scr)
4299 int i, y, sig, found_y;
4300 WDock *drawer, *dock = scr->dock;
4301 WDrawerChain *dc;
4302 char can_be_here[2 * dock->max_icons - 1];
4304 if (dock->icon_count + scr->drawer_count >= dock->max_icons)
4305 return -1;
4307 for (y = -dock->max_icons + 1; y < dock->max_icons; y++) {
4308 can_be_here[y + dock->max_icons - 1] = True;
4310 for (i = 0; i < dock->max_icons; i++) {
4311 if (dock->icon_array[i] != NULL)
4312 can_be_here[dock->icon_array[i]->yindex + dock->max_icons - 1] = False;
4314 for (dc = scr->drawers; dc != NULL; dc = dc->next) {
4315 y = (int) ((dc->adrawer->y_pos - dock->y_pos) / ICON_SIZE);
4316 can_be_here[y + dock->max_icons - 1] = False;
4319 found_y = False;
4320 for (sig = 1; !found_y && sig > -2; sig -= 2) // 1, then -1
4322 for (y = sig; sig * y < dock->max_icons; y += sig)
4324 if (can_be_here[y + dock->max_icons - 1] &&
4325 onScreen(scr, dock->x_pos, dock->y_pos + y * ICON_SIZE))
4327 found_y = True;
4328 break;
4333 if (!found_y)
4334 /* This can happen even when dock->icon_count + scr->drawer_count
4335 * < dock->max_icons when the dock is not aligned on an
4336 * ICON_SIZE multiple, as some space is lost above and under it */
4337 return -1;
4339 drawer = wDockCreate(scr, WM_DRAWER, NULL);
4340 drawer->lowered = scr->dock->lowered;
4341 drawer->auto_raise_lower = scr->dock->auto_raise_lower;
4342 drawer->x_pos = dock->x_pos;
4343 drawer->y_pos = dock->y_pos + ICON_SIZE * y;
4344 drawer->icon_array[0]->xindex = 0;
4345 drawer->icon_array[0]->yindex = 0;
4346 drawer->icon_array[0]->x_pos = drawer->x_pos;
4347 drawer->icon_array[0]->y_pos = drawer->y_pos;
4348 XMoveWindow(dpy, drawer->icon_array[0]->icon->core->window,
4349 drawer->icon_array[0]->x_pos, drawer->icon_array[0]->y_pos);
4351 return 0;
4355 static void addADrawerCallback(WMenu *menu, WMenuEntry *entry)
4357 assert(entry->clientdata!=NULL);
4358 addADrawer(((WAppIcon *) entry->clientdata)->dock->screen_ptr);
4362 static void drawerDestroy(WDock *drawer)
4364 WScreen *scr;
4365 int i;
4366 WAppIcon *aicon;
4367 WMArray *icons;
4369 if (drawer == NULL)
4370 return;
4372 scr = drawer->screen_ptr;
4374 /* Note regarding menus: we can't delete any dock/clip/drawer menu, because
4375 * that would (attempt to) wfree some memory in gettext library (see menu
4376 * entries that have several "versions", such like "Hide" and "Unhide"). */
4378 wDefaultPurgeInfo(scr, drawer->icon_array[0]->wm_instance,
4379 drawer->icon_array[0]->wm_class);
4380 icons = WMCreateArray(drawer->icon_count - 1);
4381 for (i = 1; i < drawer->max_icons; i++) {
4382 if (!(aicon = drawer->icon_array[i]))
4383 continue;
4384 WMAddToArray(icons, aicon);
4386 removeIcons(icons, drawer);
4388 if (drawer->auto_collapse_magic) {
4389 WMDeleteTimerHandler(drawer->auto_collapse_magic);
4390 drawer->auto_collapse_magic = NULL;
4392 if (drawer->auto_lower_magic) {
4393 WMDeleteTimerHandler(drawer->auto_lower_magic);
4394 drawer->auto_lower_magic = NULL;
4397 wAppIconDestroy(drawer->icon_array[0]);
4398 wfree(drawer->icon_array);
4399 wfree(drawer);
4401 drawerRemoveFromChain(scr, drawer);
4402 if (scr->last_dock == drawer)
4403 scr->last_dock = NULL;
4404 if (scr->attracting_drawer == drawer)
4405 scr->attracting_drawer = NULL;
4409 static void removeDrawerCallback(WMenu *menu, WMenuEntry *entry)
4411 WDock *dock = ((WAppIcon*)entry->clientdata)->dock;
4412 assert(dock != NULL);
4414 if (dock->icon_count > 1) {
4415 if (wMessageDialog(dock->screen_ptr, _("Drawer"),
4416 _("All icons in this drawer will be detached!"),
4417 _("OK"), _("Cancel"), NULL) != WAPRDefault)
4418 return;
4420 drawerDestroy(dock);
4424 void wDrawerIconPaint(WAppIcon *dicon)
4426 Window win = dicon->icon->core->window;
4427 WScreen *scr = dicon->icon->core->screen_ptr;
4428 XPoint p[4];
4429 GC gc = scr->draw_gc;
4430 WMColor *color;
4432 wIconPaint(dicon->icon);
4434 if (!dicon->dock->collapsed)
4435 color = scr->clip_title_color[CLIP_NORMAL];
4436 else
4437 color = scr->clip_title_color[CLIP_COLLAPSED];
4438 XSetForeground(dpy, gc, WMColorPixel(color));
4440 if (dicon->dock->on_right_side) {
4441 p[0].x = p[3].x = 10;
4442 p[0].y = p[3].y = ICON_SIZE / 2 - 5;
4443 p[1].x = 10;
4444 p[1].y = ICON_SIZE / 2 + 5;
4445 p[2].x = 5;
4446 p[2].y = ICON_SIZE / 2;
4448 else {
4449 p[0].x = p[3].x = ICON_SIZE-1 - 10;
4450 p[0].y = p[3].y = ICON_SIZE / 2 - 5;
4451 p[1].x = ICON_SIZE-1 - 10;
4452 p[1].y = ICON_SIZE / 2 + 5;
4453 p[2].x = ICON_SIZE-1 - 5;
4454 p[2].y = ICON_SIZE / 2;
4456 XFillPolygon(dpy, win, gc, p,3,Convex,CoordModeOrigin);
4457 XDrawLines(dpy, win, gc, p,4,CoordModeOrigin);
4461 RImage* wDrawerMakeTile(WScreen *scr, RImage *normalTile)
4463 RImage *tile = RCloneImage(normalTile);
4464 RColor dark;
4465 RColor light;
4467 dark.alpha = 0;
4468 dark.red = dark.green = dark.blue = 60;
4470 light.alpha = 0;
4471 light.red = light.green = light.blue = 80;
4473 /* arrow bevel */
4474 if (!scr->dock || scr->dock->on_right_side) {
4475 ROperateLine(tile, RSubtractOperation, 11, ICON_SIZE / 2 - 7,
4476 4, ICON_SIZE / 2, &dark); /* / */
4477 ROperateLine(tile, RSubtractOperation, 11, ICON_SIZE / 2 + 7,
4478 4, ICON_SIZE / 2, &dark); /* \ */
4479 ROperateLine(tile, RAddOperation, 11, ICON_SIZE / 2 - 7,
4480 11, ICON_SIZE / 2 + 7, &light); /* | */
4482 else {
4483 ROperateLine(tile, RSubtractOperation, ICON_SIZE-1 - 11, ICON_SIZE / 2 - 7,
4484 ICON_SIZE-1 - 4, ICON_SIZE / 2, &dark); /* \ */
4485 ROperateLine(tile, RAddOperation, ICON_SIZE-1 - 11, ICON_SIZE / 2 + 7,
4486 ICON_SIZE-1 - 4, ICON_SIZE / 2, &light); /* / */
4487 ROperateLine(tile, RSubtractOperation, ICON_SIZE-1 - 11, ICON_SIZE / 2 - 7,
4488 ICON_SIZE-1 - 11, ICON_SIZE / 2 + 7, &dark); /* | */
4490 return tile;
4494 static void swapDrawer(WDock *drawer, int new_x)
4496 int i;
4498 drawer->on_right_side = !drawer->on_right_side;
4499 drawer->x_pos = new_x;
4501 for (i = 0; i < drawer->max_icons; i++) {
4502 WAppIcon *ai;
4503 if ((ai = drawer->icon_array[i]) == NULL)
4504 continue;
4505 ai->xindex *= -1; /* so A B C becomes C B A */
4506 ai->x_pos = new_x + ai->xindex * ICON_SIZE;
4508 /* Update drawer's tile */
4509 if (i == 0) {
4510 wIconUpdate(ai->icon);
4511 wDrawerIconPaint(ai);
4513 XMoveWindow(dpy, ai->icon->core->window, ai->x_pos, ai->y_pos);
4518 static void swapDrawers(WScreen *scr, int on_right_side, int new_x)
4520 WDrawerChain *dc;
4522 if (scr->drawer_tile) {
4523 RReleaseImage(scr->drawer_tile);
4525 scr->drawer_tile = wDrawerMakeTile(scr, scr->icon_tile);
4527 for (dc = scr->drawers; dc != NULL; dc = dc->next) {
4528 swapDrawer(dc->adrawer, new_x);
4529 assert(dc->adrawer->on_right_side == on_right_side);
4534 int wIsADrawer(WAppIcon *aicon)
4536 return aicon && aicon->dock &&
4537 aicon->dock->type == WM_DRAWER && aicon->dock->icon_array[0] == aicon;
4541 static WDock* getDrawer(WScreen *scr, int y_index)
4543 WDrawerChain *dc;
4545 for (dc = scr->drawers; dc != NULL; dc = dc->next) {
4546 if (dc->adrawer->y_pos - scr->dock->y_pos == y_index * ICON_SIZE)
4547 return dc->adrawer;
4549 return NULL;
4554 /* Find the "hole" a moving appicon created when snapped into the
4555 * drawer. redocking is a boolean. If the moving appicon comes from the
4556 * drawer, drawer->icon_count is correct. If not, redocking is then false and
4557 * there are now drawer->icon_count plus one appicons in the drawer. */
4558 static int indexOfHole(WDock *drawer, WAppIcon *moving_aicon, int redocking)
4560 int index_of_hole, i;
4562 /* Classic interview question...
4564 * We have n-1 (n = drawer->icon_count-1 or drawer->icon_count, see
4565 * redocking) appicons, whose xindex are unique in [1..n]. One is missing:
4566 * that's where the ghost of the moving appicon is, that's what the
4567 * function should return.
4569 * We compute 1+2+...+n (this sum is equal to n*(n+1)/2), we substract to
4570 * this sum the xindex of each of the n-1 appicons, and we get the correct
4571 * index! */
4573 if (redocking) {
4574 index_of_hole = (drawer->icon_count - 1) * drawer->icon_count / 2;
4575 } else {
4576 index_of_hole = drawer->icon_count * (drawer->icon_count + 1) / 2;
4578 index_of_hole *= (drawer->on_right_side ? -1 : 1);
4580 for (i = 1; i < drawer->max_icons; i++) {
4581 if (drawer->icon_array[i] && drawer->icon_array[i] != moving_aicon)
4582 index_of_hole -= drawer->icon_array[i]->xindex;
4584 /* wmessage(" Index of the moving appicon is %d (%sredocking)", index_of_hole, (redocking ? "" : "not ")); */
4585 if (abs(index_of_hole) > abs(drawer->icon_count) - (redocking ? 1 : 0))
4586 wwarning(" index_of_hole is too large ! (%d greater than %d)",
4587 index_of_hole, abs(drawer->icon_count) - (redocking ? 1 : 0));
4588 if (index_of_hole == 0)
4589 wwarning(" index_of_hole == 0 (%sredocking, icon_count == %d)", (redocking ? "" : "not "), drawer->icon_count);
4591 return index_of_hole;
4595 void wSlideAppicons(WAppIcon **appicons, int n, int to_the_left)
4597 int i;
4598 int leftmost = -1, min_index = 9999, from_x = -1; // leftmost and from_x initialized to avoid warning
4599 Window *wins[n];
4600 WAppIcon *aicon;
4602 if (n < 1)
4603 return;
4605 for (i = 0; i < n; i++) {
4606 aicon = appicons[i];
4607 aicon->xindex += (to_the_left ? -1 : +1);
4608 if (aicon->xindex < min_index) {
4609 min_index = aicon->xindex;
4610 leftmost = i;
4611 from_x = aicon->x_pos;
4613 aicon->x_pos += (to_the_left ? -ICON_SIZE : +ICON_SIZE);
4616 for (i = 0; i < n; i++) {
4617 aicon = appicons[i];
4618 wins[aicon->xindex - min_index] = &aicon->icon->core->window;
4620 aicon = appicons[leftmost];
4621 SlideWindows(wins, n, from_x, aicon->y_pos, aicon->x_pos, aicon->y_pos);
4625 void wDrawerFillTheGap(WDock *drawer, WAppIcon *aicon, Bool redocking)
4627 int i, j;
4628 int index_of_hole = indexOfHole(drawer, aicon, redocking);
4629 WAppIcon *aicons_to_shift[drawer->icon_count];
4631 j = 0;
4632 for (i = 0; i < drawer->max_icons; i++) {
4633 WAppIcon *ai = drawer->icon_array[i];
4634 if (ai && ai != aicon &&
4635 abs(ai->xindex) > abs(index_of_hole))
4636 aicons_to_shift[j++] = ai;
4638 if (j != drawer->icon_count - abs(index_of_hole) - (redocking ? 1 : 0))
4639 wwarning("Removing aicon at index %d from %s: j=%d but should be %d",
4640 index_of_hole, drawer->icon_array[0]->wm_instance,
4641 j, drawer->icon_count - abs(index_of_hole) - (redocking ? 1 : 0));
4642 wSlideAppicons(aicons_to_shift, j, !drawer->on_right_side);
4646 static void drawerConsolidateIcons(WDock *drawer)
4648 WAppIcon *aicons_to_shift[drawer->icon_count];
4649 int maxRemaining = 0;
4650 int sum = 0;
4651 int i;
4652 for (i = 0; i < drawer->max_icons; i++) {
4653 WAppIcon *ai = drawer->icon_array[i];
4654 if (ai == NULL)
4655 continue;
4656 sum += abs(ai->xindex);
4657 if (abs(ai->xindex) > maxRemaining)
4658 maxRemaining = abs(ai->xindex);
4660 while (sum != maxRemaining * (maxRemaining + 1) / 2) { // while there is a hole
4661 WAppIcon *ai;
4662 int n;
4663 // Look up for the hole at max position
4664 int maxDeleted;
4665 for (maxDeleted = maxRemaining - 1; maxDeleted > 0; maxDeleted--) {
4666 Bool foundAppIconThere = False;
4667 for (i = 0; i < drawer->max_icons; i++) {
4668 WAppIcon *ai = drawer->icon_array[i];
4669 if (ai == NULL)
4670 continue;
4671 if (abs(ai->xindex) == maxDeleted) {
4672 foundAppIconThere = True;
4673 break;
4676 if (!foundAppIconThere)
4677 break;
4679 assert(maxDeleted > 0); // would mean while test is wrong
4680 n = 0;
4681 for (i = 0; i < drawer->max_icons; i++) {
4682 ai = drawer->icon_array[i];
4683 if (ai != NULL && abs(ai->xindex) > maxDeleted)
4684 aicons_to_shift[n++] = ai;
4686 assert(n == maxRemaining - maxDeleted); // for the code review ;-)
4687 wSlideAppicons(aicons_to_shift, n, !drawer->on_right_side);
4688 // Efficient beancounting
4689 maxRemaining -= 1;
4690 sum -= n;
4696 /* similar to wDockRestoreState, but a lot a specific stuff too... */
4697 static WDock * drawerRestoreState(WScreen *scr, WMPropList *drawer_state)
4699 WDock *drawer;
4700 WMPropList *apps, *value, *dock_state;
4701 WAppIcon *aicon;
4702 int count, i;
4704 if (!drawer_state)
4705 return NULL;
4707 make_keys();
4709 WMRetainPropList(drawer_state);
4711 /* Get the instance name, and create a drawer */
4712 value = WMGetFromPLDictionary(drawer_state, dName);
4713 drawer = wDockCreate(scr, WM_DRAWER, WMGetFromPLString(value));
4715 /* restore DnD command and paste command */
4716 #ifdef XDND
4717 value = WMGetFromPLDictionary(drawer_state, dDropCommand);
4718 if (value && WMIsPLString(value))
4719 drawer->icon_array[0]->dnd_command = wstrdup(WMGetFromPLString(value));
4720 #endif /* XDND */
4722 value = WMGetFromPLDictionary(drawer_state, dPasteCommand);
4723 if (value && WMIsPLString(value))
4724 drawer->icon_array[0]->paste_command = wstrdup(WMGetFromPLString(value));
4726 /* restore position */
4727 value = WMGetFromPLDictionary(drawer_state, dPosition);
4728 if (!value || !WMIsPLString(value))
4729 COMPLAIN("Position");
4730 else {
4731 int x, y, y_index;
4732 if (sscanf(WMGetFromPLString(value), "%i,%i", &x, &y) != 2)
4733 COMPLAIN("Position");
4735 /* check position sanity */
4736 if (x != scr->dock->x_pos) {
4737 x = scr->dock->x_pos;
4739 y_index = (y - scr->dock->y_pos) / ICON_SIZE;
4740 if (y_index >= scr->dock->max_icons) {
4741 /* Here we should do something more intelligent, since it
4742 * can happen even if the user hasn't hand-edited his
4743 * G/D/State file (but uses a lower resolution). */
4744 y_index = scr->dock->max_icons - 1;
4746 y = scr->dock->y_pos + y_index * ICON_SIZE;
4747 moveDock(drawer, x, y);
4750 /* restore dock properties (applist and others) */
4751 dock_state = WMGetFromPLDictionary(drawer_state, dDock);
4753 /* restore lowered/raised state: same as scr->dock, no matter what */
4754 drawer->lowered = scr->dock->lowered;
4755 if (!drawer->lowered)
4756 ChangeStackingLevel(drawer->icon_array[0]->icon->core, WMDockLevel);
4757 else
4758 ChangeStackingLevel(drawer->icon_array[0]->icon->core, WMNormalLevel);
4759 wRaiseFrame(drawer->icon_array[0]->icon->core);
4761 /* restore collapsed state */
4762 drawer->collapsed = 0;
4763 value = WMGetFromPLDictionary(dock_state, dCollapsed);
4764 if (value && strcasecmp(WMGetFromPLString(value), "YES") == 0) {
4765 drawer->collapsed = 1;
4767 /* restore auto-collapsed state */
4768 value = WMGetFromPLDictionary(dock_state, dAutoCollapse);
4769 if (value && strcasecmp(WMGetFromPLString(value), "YES") == 0) {
4770 drawer->auto_collapse = 1;
4771 drawer->collapsed = 1;
4772 } else {
4773 drawer->auto_collapse = 0; // because wDockCreate sets it (drawers only)
4776 /* restore auto-raise/lower state: same as scr->dock, no matter what */
4777 drawer->auto_raise_lower = scr->dock->auto_raise_lower;
4779 /* restore attract icons state */
4780 drawer->attract_icons = 0;
4781 value = WMGetFromPLDictionary(dock_state, dAutoAttractIcons);
4782 if (value && strcasecmp(WMGetFromPLString(value), "YES") == 0) {
4783 drawer->attract_icons = 1;
4784 scr->attracting_drawer = drawer;
4787 /* application list */
4788 apps = WMGetFromPLDictionary(dock_state, dApplications);
4790 if (!apps) {
4791 goto finish;
4794 count = WMGetPropListItemCount(apps);
4796 if (count == 0)
4797 goto finish;
4799 for (i=0; i<count; i++) {
4800 if (drawer->icon_count >= drawer->max_icons) {
4801 wwarning(_("there are too many icons stored in drawer. Ignoring what doesn't fit"));
4802 break;
4805 value = WMGetFromPLArray(apps, i);
4806 aicon = restore_icon_state(scr, value, WM_DRAWER, drawer->icon_count);
4808 drawer->icon_array[drawer->icon_count] = aicon;
4810 if (aicon) {
4811 aicon->dock = drawer;
4812 aicon->x_pos = drawer->x_pos + (aicon->xindex * ICON_SIZE);
4813 aicon->y_pos = drawer->y_pos + (aicon->yindex * ICON_SIZE);
4815 if (!drawer->lowered)
4816 ChangeStackingLevel(aicon->icon->core, WMDockLevel);
4817 else
4818 ChangeStackingLevel(aicon->icon->core, WMNormalLevel);
4820 wCoreConfigure(aicon->icon->core, aicon->x_pos, aicon->y_pos, 0, 0);
4822 if (!drawer->collapsed)
4823 XMapWindow(dpy, aicon->icon->core->window);
4824 wRaiseFrame(aicon->icon->core);
4826 drawer->icon_count++;
4830 finish:
4831 WMReleasePropList(drawer_state);
4833 return drawer;
4837 /* Same kind of comment than for previous function: this function is
4838 * very similar to make_icon_state, but has substential differences as
4839 * well. */
4840 static WMPropList *drawerSaveState(WScreen *scr, WDock *drawer)
4842 WMPropList *pstr, *drawer_state;
4843 WAppIcon *ai;
4844 char buffer[64];
4846 ai = drawer->icon_array[0];
4847 /* Store its name */
4848 pstr = WMCreatePLString(ai->wm_instance);
4849 drawer_state = WMCreatePLDictionary(dName, pstr, NULL); /* we need this final NULL */
4850 WMReleasePropList(pstr);
4852 /* Store its position */
4853 snprintf(buffer, sizeof(buffer), "%i,%i", ai->x_pos, ai->y_pos);
4854 pstr = WMCreatePLString(buffer);
4855 WMPutInPLDictionary(drawer_state, dPosition, pstr);
4856 WMReleasePropList(pstr);
4858 #ifdef XDND
4859 /* Store its DnD command */
4860 if (ai->dnd_command) {
4861 pstr = WMCreatePLString(ai->dnd_command);
4862 WMPutInPLDictionary(drawer_state, dDropCommand, pstr);
4863 WMReleasePropList(pstr);
4865 #endif /* XDND */
4867 /* Store its paste command */
4868 if (ai->paste_command) {
4869 pstr = WMCreatePLString(ai->paste_command);
4870 WMPutInPLDictionary(drawer_state, dPasteCommand, pstr);
4871 WMReleasePropList(pstr);
4874 /* Store applications list and other properties */
4875 pstr = dockSaveState(drawer);
4876 WMPutInPLDictionary(drawer_state, dDock, pstr);
4877 WMReleasePropList(pstr);
4879 return drawer_state;
4883 void wDrawersSaveState(WScreen *scr)
4885 WMPropList *all_drawers, *drawer_state;
4886 int i;
4887 WDrawerChain *dc;
4889 make_keys();
4891 all_drawers = WMCreatePLArray(NULL);
4892 for (i=0, dc = scr->drawers;
4893 i < scr->drawer_count;
4894 i++, dc = dc->next) {
4895 drawer_state = drawerSaveState(scr, dc->adrawer);
4896 WMAddToPLArray(all_drawers, drawer_state);
4897 WMReleasePropList(drawer_state);
4899 WMPutInPLDictionary(scr->session_state, dDrawers, all_drawers);
4900 WMReleasePropList(all_drawers);
4904 void wDrawersRestoreState(WScreen *scr)
4906 WMPropList *all_drawers, *drawer_state;
4907 int i;
4909 make_keys();
4911 if (scr->session_state == NULL)
4912 return;
4914 all_drawers = WMGetFromPLDictionary(scr->session_state, dDrawers);
4915 if (!all_drawers)
4916 return;
4918 for (i = 0; i < WMGetPropListItemCount(all_drawers); i++) {
4919 drawer_state = WMGetFromPLArray(all_drawers, i);
4920 drawerRestoreState(scr, drawer_state);
4921 // Note: scr->drawers was updated when the the drawer was created