wmaker: Replaced local declaration of system function by proper header call
[wmaker-crm.git] / src / dock.c
blob8bd72c29ea5f588ac6a99d4e9fc66f3ae24da59e
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 /**** Global variables ****/
69 extern Cursor wCursor[WCUR_LAST];
70 extern XContext wWinContext;
71 extern void appIconMouseDown(WObjDescriptor *desc, XEvent *event);
73 #define MOD_MASK wPreferences.modifier_mask
74 #define ICON_SIZE wPreferences.icon_size
76 /***** Local variables ****/
78 static WMPropList *dCommand = NULL;
79 static WMPropList *dPasteCommand = NULL;
80 #ifdef XDND /* XXX was OFFIX */
81 static WMPropList *dDropCommand = NULL;
82 #endif
83 static WMPropList *dAutoLaunch, *dLock;
84 static WMPropList *dName, *dForced, *dBuggyApplication, *dYes, *dNo;
85 static WMPropList *dHost, *dDock, *dClip;
86 static WMPropList *dAutoAttractIcons;
88 static WMPropList *dPosition, *dApplications, *dLowered, *dCollapsed;
90 static WMPropList *dAutoCollapse, *dAutoRaiseLower, *dOmnipresent;
92 static WMPropList *dDrawers = NULL;
94 static void dockIconPaint(WAppIcon *btn);
96 static void iconMouseDown(WObjDescriptor *desc, XEvent *event);
98 static pid_t execCommand(WAppIcon *btn, const char *command, WSavedState *state);
100 static void trackDeadProcess(pid_t pid, unsigned char status, WDock *dock);
102 static int getClipButton(int px, int py);
104 static void toggleLowered(WDock *dock);
106 static void toggleCollapsed(WDock *dock);
108 static void clipIconExpose(WObjDescriptor *desc, XEvent *event);
110 static void clipLeave(WDock *dock);
112 static void handleClipChangeWorkspace(WScreen *scr, XEvent *event);
114 static void clipEnterNotify(WObjDescriptor *desc, XEvent *event);
115 static void clipLeaveNotify(WObjDescriptor *desc, XEvent *event);
116 static void clipAutoCollapse(void *cdata);
117 static void clipAutoExpand(void *cdata);
118 static void launchDockedApplication(WAppIcon *btn, Bool withSelection);
120 static void clipAutoLower(void *cdata);
121 static void clipAutoRaise(void *cdata);
122 static WAppIcon *mainIconCreate(WScreen *scr, int type, const char *name);
124 static void drawerIconExpose(WObjDescriptor *desc, XEvent *event);
125 static void removeDrawerCallback(WMenu *menu, WMenuEntry *entry);
126 static void drawerAppendToChain(WScreen *scr, WDock *drawer);
127 static char *findUniqueName(WScreen *scr, const char *instance_basename);
128 static void addADrawerCallback(WMenu *menu, WMenuEntry *entry);
129 static void swapDrawers(WScreen *scr, int on_right_side, int new_x);
130 static WDock* getDrawer(WScreen *scr, int y_index);
131 static int indexOfHole(WDock *drawer, WAppIcon *moving_aicon, int redocking);
132 static void drawerConsolidateIcons(WDock *drawer);
134 static int onScreen(WScreen *scr, int x, int y);
136 static void make_keys(void)
138 if (dCommand != NULL)
139 return;
141 dCommand = WMRetainPropList(WMCreatePLString("Command"));
142 dPasteCommand = WMRetainPropList(WMCreatePLString("PasteCommand"));
143 #ifdef XDND
144 dDropCommand = WMRetainPropList(WMCreatePLString("DropCommand"));
145 #endif
146 dLock = WMRetainPropList(WMCreatePLString("Lock"));
147 dAutoLaunch = WMRetainPropList(WMCreatePLString("AutoLaunch"));
148 dName = WMRetainPropList(WMCreatePLString("Name"));
149 dForced = WMRetainPropList(WMCreatePLString("Forced"));
150 dBuggyApplication = WMRetainPropList(WMCreatePLString("BuggyApplication"));
151 dYes = WMRetainPropList(WMCreatePLString("Yes"));
152 dNo = WMRetainPropList(WMCreatePLString("No"));
153 dHost = WMRetainPropList(WMCreatePLString("Host"));
155 dPosition = WMCreatePLString("Position");
156 dApplications = WMCreatePLString("Applications");
157 dLowered = WMCreatePLString("Lowered");
158 dCollapsed = WMCreatePLString("Collapsed");
159 dAutoCollapse = WMCreatePLString("AutoCollapse");
160 dAutoRaiseLower = WMCreatePLString("AutoRaiseLower");
161 dAutoAttractIcons = WMCreatePLString("AutoAttractIcons");
163 dOmnipresent = WMCreatePLString("Omnipresent");
165 dDock = WMCreatePLString("Dock");
166 dClip = WMCreatePLString("Clip");
167 dDrawers = WMCreatePLString("Drawers");
170 static void renameCallback(WMenu *menu, WMenuEntry *entry)
172 WDock *dock = entry->clientdata;
173 char buffer[128];
174 int wspace;
175 char *name;
177 assert(entry->clientdata != NULL);
179 wspace = dock->screen_ptr->current_workspace;
181 name = wstrdup(dock->screen_ptr->workspaces[wspace]->name);
183 snprintf(buffer, sizeof(buffer), _("Type the name for workspace %i:"), wspace + 1);
184 if (wInputDialog(dock->screen_ptr, _("Rename Workspace"), buffer, &name))
185 wWorkspaceRename(dock->screen_ptr, wspace, name);
187 wfree(name);
190 static void toggleLoweredCallback(WMenu *menu, WMenuEntry *entry)
192 assert(entry->clientdata != NULL);
194 toggleLowered(entry->clientdata);
196 entry->flags.indicator_on = !(((WDock *) entry->clientdata)->lowered);
198 wMenuPaint(menu);
201 static int matchWindow(const void *item, const void *cdata)
203 return (((WFakeGroupLeader *) item)->leader == (Window) cdata);
206 static void killCallback(WMenu *menu, WMenuEntry *entry)
208 WScreen *scr = menu->menu->screen_ptr;
209 WAppIcon *icon;
210 WFakeGroupLeader *fPtr;
211 char *buffer, *shortname, **argv;
212 int argc;
214 if (!WCHECK_STATE(WSTATE_NORMAL))
215 return;
217 assert(entry->clientdata != NULL);
219 icon = (WAppIcon *) entry->clientdata;
221 icon->editing = 1;
223 WCHANGE_STATE(WSTATE_MODAL);
225 /* strip away dir names */
226 shortname = basename(icon->command);
227 /* separate out command options */
228 wtokensplit(shortname, &argv, &argc);
230 buffer = wstrconcat(argv[0],
231 _(" will be forcibly closed.\n"
232 "Any unsaved changes will be lost.\n" "Please confirm."));
234 if (icon->icon && icon->icon->owner) {
235 fPtr = icon->icon->owner->fake_group;
236 } else {
237 /* is this really necessary? can we kill a non-running dock icon? */
238 Window win = icon->main_window;
239 int index;
241 index = WMFindInArray(scr->fakeGroupLeaders, matchWindow, (void *)win);
242 if (index != WANotFound)
243 fPtr = WMGetFromArray(scr->fakeGroupLeaders, index);
244 else
245 fPtr = NULL;
248 if (wPreferences.dont_confirm_kill
249 || wMessageDialog(menu->frame->screen_ptr, _("Kill Application"),
250 buffer, _("Yes"), _("No"), NULL) == WAPRDefault) {
251 if (fPtr != NULL) {
252 WWindow *wwin, *twin;
254 wwin = scr->focused_window;
255 while (wwin) {
256 twin = wwin->prev;
257 if (wwin->fake_group == fPtr)
258 wClientKill(wwin);
260 wwin = twin;
262 } else if (icon->icon && icon->icon->owner) {
263 wClientKill(icon->icon->owner);
267 wfree(buffer);
268 wtokenfree(argv, argc);
270 icon->editing = 0;
272 WCHANGE_STATE(WSTATE_NORMAL);
275 /* TODO: replace this function with a member of the dock struct */
276 static int numberOfSelectedIcons(WDock *dock)
278 WAppIcon *aicon;
279 int i, n;
281 n = 0;
282 for (i = 1; i < dock->max_icons; i++) {
283 aicon = dock->icon_array[i];
284 if (aicon && aicon->icon->selected)
285 n++;
288 return n;
291 static WMArray *getSelected(WDock *dock)
293 WMArray *ret = WMCreateArray(8);
294 WAppIcon *btn;
295 int i;
297 for (i = 1; i < dock->max_icons; i++) {
298 btn = dock->icon_array[i];
299 if (btn && btn->icon->selected)
300 WMAddToArray(ret, btn);
303 return ret;
306 static void paintClipButtons(WAppIcon *clipIcon, Bool lpushed, Bool rpushed)
308 Window win = clipIcon->icon->core->window;
309 WScreen *scr = clipIcon->icon->core->screen_ptr;
310 XPoint p[4];
311 int pt = CLIP_BUTTON_SIZE * ICON_SIZE / 64;
312 int tp = ICON_SIZE - pt;
313 int as = pt - 15; /* 15 = 5+5+5 */
314 GC gc = scr->draw_gc; /* maybe use WMColorGC() instead here? */
315 WMColor *color;
317 color = scr->clip_title_color[CLIP_NORMAL];
319 XSetForeground(dpy, gc, WMColorPixel(color));
321 if (rpushed) {
322 p[0].x = tp + 1;
323 p[0].y = 1;
324 p[1].x = ICON_SIZE - 2;
325 p[1].y = 1;
326 p[2].x = ICON_SIZE - 2;
327 p[2].y = pt - 1;
328 } else if (lpushed) {
329 p[0].x = 1;
330 p[0].y = tp;
331 p[1].x = pt;
332 p[1].y = ICON_SIZE - 2;
333 p[2].x = 1;
334 p[2].y = ICON_SIZE - 2;
336 if (lpushed || rpushed) {
337 XSetForeground(dpy, scr->draw_gc, scr->white_pixel);
338 XFillPolygon(dpy, win, scr->draw_gc, p, 3, Convex, CoordModeOrigin);
339 XSetForeground(dpy, scr->draw_gc, scr->black_pixel);
342 /* top right arrow */
343 p[0].x = p[3].x = ICON_SIZE - 5 - as;
344 p[0].y = p[3].y = 5;
345 p[1].x = ICON_SIZE - 6;
346 p[1].y = 5;
347 p[2].x = ICON_SIZE - 6;
348 p[2].y = 4 + as;
349 if (rpushed) {
350 XFillPolygon(dpy, win, scr->draw_gc, p, 3, Convex, CoordModeOrigin);
351 XDrawLines(dpy, win, scr->draw_gc, p, 4, CoordModeOrigin);
352 } else {
353 XFillPolygon(dpy, win, gc, p, 3, Convex, CoordModeOrigin);
354 XDrawLines(dpy, win, gc, p, 4, CoordModeOrigin);
357 /* bottom left arrow */
358 p[0].x = p[3].x = 5;
359 p[0].y = p[3].y = ICON_SIZE - 5 - as;
360 p[1].x = 5;
361 p[1].y = ICON_SIZE - 6;
362 p[2].x = 4 + as;
363 p[2].y = ICON_SIZE - 6;
364 if (lpushed) {
365 XFillPolygon(dpy, win, scr->draw_gc, p, 3, Convex, CoordModeOrigin);
366 XDrawLines(dpy, win, scr->draw_gc, p, 4, CoordModeOrigin);
367 } else {
368 XFillPolygon(dpy, win, gc, p, 3, Convex, CoordModeOrigin);
369 XDrawLines(dpy, win, gc, p, 4, CoordModeOrigin);
373 RImage *wClipMakeTile(WScreen *scr, RImage *normalTile)
375 RImage *tile = RCloneImage(normalTile);
376 RColor black;
377 RColor dark;
378 RColor light;
379 int pt, tp;
380 int as;
382 pt = CLIP_BUTTON_SIZE * wPreferences.icon_size / 64;
383 tp = wPreferences.icon_size - 1 - pt;
384 as = pt - 15;
386 black.alpha = 255;
387 black.red = black.green = black.blue = 0;
389 dark.alpha = 0;
390 dark.red = dark.green = dark.blue = 60;
392 light.alpha = 0;
393 light.red = light.green = light.blue = 80;
395 /* top right */
396 ROperateLine(tile, RSubtractOperation, tp, 0, wPreferences.icon_size - 2, pt - 1, &dark);
397 RDrawLine(tile, tp - 1, 0, wPreferences.icon_size - 1, pt + 1, &black);
398 ROperateLine(tile, RAddOperation, tp, 2, wPreferences.icon_size - 3, pt, &light);
400 /* arrow bevel */
401 ROperateLine(tile, RSubtractOperation, ICON_SIZE - 7 - as, 4, ICON_SIZE - 5, 4, &dark);
402 ROperateLine(tile, RSubtractOperation, ICON_SIZE - 6 - as, 5, ICON_SIZE - 5, 6 + as, &dark);
403 ROperateLine(tile, RAddOperation, ICON_SIZE - 5, 4, ICON_SIZE - 5, 6 + as, &light);
405 /* bottom left */
406 ROperateLine(tile, RAddOperation, 2, tp + 2, pt - 2, wPreferences.icon_size - 3, &dark);
407 RDrawLine(tile, 0, tp - 1, pt + 1, wPreferences.icon_size - 1, &black);
408 ROperateLine(tile, RSubtractOperation, 0, tp - 2, pt + 1, wPreferences.icon_size - 2, &light);
410 /* arrow bevel */
411 ROperateLine(tile, RSubtractOperation, 4, ICON_SIZE - 7 - as, 4, ICON_SIZE - 5, &dark);
412 ROperateLine(tile, RSubtractOperation, 5, ICON_SIZE - 6 - as, 6 + as, ICON_SIZE - 5, &dark);
413 ROperateLine(tile, RAddOperation, 4, ICON_SIZE - 5, 6 + as, ICON_SIZE - 5, &light);
415 return tile;
418 static void omnipresentCallback(WMenu *menu, WMenuEntry *entry)
420 WAppIcon *clickedIcon = entry->clientdata;
421 WAppIcon *aicon;
422 WDock *dock;
423 WMArray *selectedIcons;
424 WMArrayIterator iter;
425 int failed;
427 assert(entry->clientdata != NULL);
429 dock = clickedIcon->dock;
431 selectedIcons = getSelected(dock);
433 if (!WMGetArrayItemCount(selectedIcons))
434 WMAddToArray(selectedIcons, clickedIcon);
436 failed = 0;
437 WM_ITERATE_ARRAY(selectedIcons, aicon, iter) {
438 if (wClipMakeIconOmnipresent(aicon, !aicon->omnipresent) == WO_FAILED)
439 failed++;
440 else if (aicon->icon->selected)
441 wIconSelect(aicon->icon);
443 WMFreeArray(selectedIcons);
445 if (failed > 1) {
446 wMessageDialog(dock->screen_ptr, _("Warning"),
447 _("Some icons cannot be made omnipresent. "
448 "Please make sure that no other icon is "
449 "docked in the same positions on the other "
450 "workspaces and the Clip is not full in "
451 "some workspace."), _("OK"), NULL, NULL);
452 } else if (failed == 1) {
453 wMessageDialog(dock->screen_ptr, _("Warning"),
454 _("Icon cannot be made omnipresent. "
455 "Please make sure that no other icon is "
456 "docked in the same position on the other "
457 "workspaces and the Clip is not full in "
458 "some workspace."), _("OK"), NULL, NULL);
462 static void removeIcons(WMArray *icons, WDock *dock)
464 WAppIcon *aicon;
465 int keepit;
466 WMArrayIterator it;
468 WM_ITERATE_ARRAY(icons, aicon, it) {
469 keepit = aicon->running && wApplicationOf(aicon->main_window);
470 wDockDetach(dock, aicon);
471 if (keepit) {
472 /* XXX: can: aicon->icon == NULL ? */
473 PlaceIcon(dock->screen_ptr, &aicon->x_pos, &aicon->y_pos,
474 wGetHeadForWindow(aicon->icon->owner));
475 XMoveWindow(dpy, aicon->icon->core->window, aicon->x_pos, aicon->y_pos);
476 if (!dock->mapped || dock->collapsed)
477 XMapWindow(dpy, aicon->icon->core->window);
480 WMFreeArray(icons);
482 if (wPreferences.auto_arrange_icons)
483 wArrangeIcons(dock->screen_ptr, True);
486 static void removeIconsCallback(WMenu *menu, WMenuEntry *entry)
488 WAppIcon *clickedIcon = (WAppIcon *) entry->clientdata;
489 WDock *dock;
490 WMArray *selectedIcons;
492 assert(clickedIcon != NULL);
494 dock = clickedIcon->dock;
496 selectedIcons = getSelected(dock);
498 if (WMGetArrayItemCount(selectedIcons)) {
499 if (wMessageDialog(dock->screen_ptr,
500 dock->type == WM_CLIP ? _("Workspace Clip") : _("Drawer"),
501 _("All selected icons will be removed!"),
502 _("OK"), _("Cancel"), NULL) != WAPRDefault) {
503 WMFreeArray(selectedIcons);
504 return;
506 } else {
507 if (clickedIcon->xindex == 0 && clickedIcon->yindex == 0) {
508 WMFreeArray(selectedIcons);
509 return;
511 WMAddToArray(selectedIcons, clickedIcon);
514 removeIcons(selectedIcons, dock);
516 if (dock->type == WM_DRAWER) {
517 drawerConsolidateIcons(dock);
521 static void keepIconsCallback(WMenu *menu, WMenuEntry *entry)
523 WAppIcon *clickedIcon = (WAppIcon *) entry->clientdata;
524 WDock *dock;
525 WAppIcon *aicon;
526 WMArray *selectedIcons;
527 WMArrayIterator it;
529 assert(clickedIcon != NULL);
530 dock = clickedIcon->dock;
532 selectedIcons = getSelected(dock);
534 if (!WMGetArrayItemCount(selectedIcons)
535 && clickedIcon != dock->screen_ptr->clip_icon) {
536 char *command = NULL;
538 if (!clickedIcon->command && !clickedIcon->editing) {
539 clickedIcon->editing = 1;
540 if (wInputDialog(dock->screen_ptr, _("Keep Icon"),
541 _("Type the command used to launch the application"), &command)) {
542 if (command && (command[0] == 0 || (command[0] == '-' && command[1] == 0))) {
543 wfree(command);
544 command = NULL;
546 clickedIcon->command = command;
547 clickedIcon->editing = 0;
548 } else {
549 clickedIcon->editing = 0;
550 if (command)
551 wfree(command);
552 WMFreeArray(selectedIcons);
553 return;
557 WMAddToArray(selectedIcons, clickedIcon);
560 WM_ITERATE_ARRAY(selectedIcons, aicon, it) {
561 if (aicon->icon->selected)
562 wIconSelect(aicon->icon);
564 if (aicon && aicon->attracted && aicon->command) {
565 aicon->attracted = 0;
566 if (aicon->icon->shadowed) {
567 aicon->icon->shadowed = 0;
570 * Update icon pixmap, RImage doesn't change,
571 * so call wIconUpdate is not needed
573 update_icon_pixmap(aicon->icon);
575 /* Paint it */
576 wAppIconPaint(aicon);
579 save_appicon(aicon, True);
581 WMFreeArray(selectedIcons);
584 static void toggleAutoAttractCallback(WMenu *menu, WMenuEntry *entry)
586 WDock *dock = (WDock *) entry->clientdata;
587 WScreen *scr = dock->screen_ptr;
589 assert(entry->clientdata != NULL);
591 /* Setting a clip as autoattracting will disable an autoattracting drawer,
592 * and reciprocally. Ask for confirmation? */
593 #ifdef CONFIRM_AUTOATTRACT_CHANGE
594 if (dock->type == WM_CLIP && scr->attracting_drawer) {
595 if (wMessageDialog(scr, _("Workspace Clip"),
596 _("The drawer that auto-attracted icons so far won't do so anymore!"),
597 _("OK"), _("Cancel"), NULL) != WAPRDefault)
598 return;
599 } else if (dock->type == WM_DRAWER) {
600 /* check if at least one clip already auto attracts icons */
601 int i, ask_for_confirmation = False;
602 for (i = 0; i < scr->workspace_count; i++) {
603 if (scr->workspaces[ i ]->clip->attract_icons) {
604 ask_for_confirmation = True;
605 break;
608 if (ask_for_confirmation) {
609 if (wMessageDialog(scr, _("Drawer"),
610 _("Any clips that auto-attracted icons so far won't do so anymore!"),
611 _("OK"), _("Cancel"), NULL) != WAPRDefault)
612 return;
615 #endif
617 dock->attract_icons = !dock->attract_icons;
619 entry->flags.indicator_on = dock->attract_icons;
621 wMenuPaint(menu);
623 if (dock->attract_icons) {
624 if (dock->type == WM_DRAWER) {
625 /* The newly auto-attracting dock is a drawer: disable any clip and
626 * previously attracting drawer */
627 int i;
628 for (i = 0; i < scr->workspace_count; i++) {
629 scr->workspaces[ i ]->clip->attract_icons = False;
630 /* dock menu will be updated later, when opened */
632 if (scr->attracting_drawer != NULL)
633 scr->attracting_drawer->attract_icons = False;
634 scr->attracting_drawer = dock;
635 } else {
636 /* The newly auto-attracting dock is a clip: disable
637 * previously attracting drawer, if applicable */
638 if (scr->attracting_drawer != NULL) {
639 scr->attracting_drawer->attract_icons = False;
640 /* again, its menu will be updated, later. */
641 scr->attracting_drawer = NULL;
647 static void selectCallback(WMenu *menu, WMenuEntry *entry)
649 WAppIcon *icon = (WAppIcon *) entry->clientdata;
651 assert(icon != NULL);
653 wIconSelect(icon->icon);
655 wMenuPaint(menu);
658 static void colectIconsCallback(WMenu *menu, WMenuEntry *entry)
660 WAppIcon *clickedIcon = (WAppIcon *) entry->clientdata;
661 WDock *clip; /* clip... is a WM_CLIP or a WM_DRAWER */
662 WAppIcon *aicon;
663 int x, y, x_pos, y_pos;
664 Bool update_icon = False;
666 assert(entry->clientdata != NULL);
667 clip = clickedIcon->dock;
669 aicon = clip->screen_ptr->app_icon_list;
671 while (aicon) {
672 if (!aicon->docked && wDockFindFreeSlot(clip, &x, &y)) {
673 x_pos = clip->x_pos + x * ICON_SIZE;
674 y_pos = clip->y_pos + y * ICON_SIZE;
675 if (aicon->x_pos != x_pos || aicon->y_pos != y_pos)
676 move_window(aicon->icon->core->window, aicon->x_pos, aicon->y_pos, x_pos, y_pos);
678 aicon->attracted = 1;
679 if (!aicon->icon->shadowed) {
680 aicon->icon->shadowed = 1;
681 update_icon = True;
683 wDockAttachIcon(clip, aicon, x, y, update_icon);
684 if (clip->collapsed || !clip->mapped)
685 XUnmapWindow(dpy, aicon->icon->core->window);
687 aicon = aicon->next;
691 static void selectIconsCallback(WMenu *menu, WMenuEntry *entry)
693 WAppIcon *clickedIcon = (WAppIcon *) entry->clientdata;
694 WDock *dock;
695 WMArray *selectedIcons;
696 WMArrayIterator iter;
697 WAppIcon *btn;
698 int i;
700 assert(clickedIcon != NULL);
701 dock = clickedIcon->dock;
703 selectedIcons = getSelected(dock);
705 if (!WMGetArrayItemCount(selectedIcons)) {
706 for (i = 1; i < dock->max_icons; i++) {
707 btn = dock->icon_array[i];
708 if (btn && !btn->icon->selected)
709 wIconSelect(btn->icon);
711 } else {
712 WM_ITERATE_ARRAY(selectedIcons, btn, iter) {
713 wIconSelect(btn->icon);
716 WMFreeArray(selectedIcons);
718 wMenuPaint(menu);
721 static void toggleCollapsedCallback(WMenu *menu, WMenuEntry *entry)
723 assert(entry->clientdata != NULL);
725 toggleCollapsed(entry->clientdata);
727 entry->flags.indicator_on = ((WDock *) entry->clientdata)->collapsed;
729 wMenuPaint(menu);
732 static void toggleAutoCollapseCallback(WMenu *menu, WMenuEntry *entry)
734 WDock *dock;
735 assert(entry->clientdata != NULL);
737 dock = (WDock *) entry->clientdata;
739 dock->auto_collapse = !dock->auto_collapse;
741 entry->flags.indicator_on = ((WDock *) entry->clientdata)->auto_collapse;
743 wMenuPaint(menu);
746 static void toggleAutoRaiseLower(WDock *dock)
748 WDrawerChain *dc;
750 dock->auto_raise_lower = !dock->auto_raise_lower;
751 if (dock->type == WM_DOCK)
753 for (dc = dock->screen_ptr->drawers; dc != NULL; dc = dc->next) {
754 toggleAutoRaiseLower(dc->adrawer);
759 static void toggleAutoRaiseLowerCallback(WMenu *menu, WMenuEntry *entry)
761 WDock *dock;
762 assert(entry->clientdata != NULL);
764 dock = (WDock *) entry->clientdata;
766 toggleAutoRaiseLower(dock);
768 entry->flags.indicator_on = ((WDock *) entry->clientdata)->auto_raise_lower;
770 wMenuPaint(menu);
773 static void launchCallback(WMenu *menu, WMenuEntry *entry)
775 WAppIcon *btn = (WAppIcon *) entry->clientdata;
777 launchDockedApplication(btn, False);
780 static void settingsCallback(WMenu *menu, WMenuEntry *entry)
782 WAppIcon *btn = (WAppIcon *) entry->clientdata;
784 if (btn->editing)
785 return;
786 ShowDockAppSettingsPanel(btn);
789 static void hideCallback(WMenu *menu, WMenuEntry *entry)
791 WApplication *wapp;
792 WAppIcon *btn = (WAppIcon *) entry->clientdata;
794 wapp = wApplicationOf(btn->icon->owner->main_window);
796 if (wapp->flags.hidden) {
797 wWorkspaceChange(btn->icon->core->screen_ptr, wapp->last_workspace);
798 wUnhideApplication(wapp, False, False);
799 } else {
800 wHideApplication(wapp);
804 static void unhideHereCallback(WMenu *menu, WMenuEntry *entry)
806 WApplication *wapp;
807 WAppIcon *btn = (WAppIcon *) entry->clientdata;
809 wapp = wApplicationOf(btn->icon->owner->main_window);
811 wUnhideApplication(wapp, False, True);
814 /* Name is only used when type == WM_DRAWER and when restoring a specific
815 * drawer, with a specific name. When creating a drawer, leave name to NULL
816 * and mainIconCreate will find the first unused unique name */
817 static WAppIcon *mainIconCreate(WScreen *scr, int type, const char *name)
819 WAppIcon *btn;
820 int x_pos;
822 switch(type) {
823 case WM_CLIP:
824 if (scr->clip_icon)
825 return scr->clip_icon;
826 btn = wAppIconCreateForDock(scr, NULL, "Logo", "WMClip", TILE_CLIP);
827 btn->icon->core->descriptor.handle_expose = clipIconExpose;
828 x_pos = 0;
829 break;
830 case WM_DOCK:
831 default: /* to avoid a warning about btn and x_pos, basically */
832 btn = wAppIconCreateForDock(scr, NULL, "Logo", "WMDock", TILE_NORMAL);
833 if (wPreferences.flags.clip_merged_in_dock)
834 btn->icon->core->descriptor.handle_expose = clipIconExpose;
835 x_pos = scr->scr_width - ICON_SIZE - DOCK_EXTRA_SPACE;
836 break;
837 case WM_DRAWER:
838 if (name == NULL)
839 name = findUniqueName(scr, "Drawer");
840 btn = wAppIconCreateForDock(scr, NULL, name, "WMDrawer", TILE_DRAWER);
841 btn->icon->core->descriptor.handle_expose = drawerIconExpose;
842 x_pos = 0;
845 btn->xindex = 0;
846 btn->yindex = 0;
848 btn->icon->core->descriptor.handle_mousedown = iconMouseDown;
849 btn->icon->core->descriptor.handle_enternotify = clipEnterNotify;
850 btn->icon->core->descriptor.handle_leavenotify = clipLeaveNotify;
851 btn->icon->core->descriptor.parent_type = WCLASS_DOCK_ICON;
852 btn->icon->core->descriptor.parent = btn;
853 XMapWindow(dpy, btn->icon->core->window);
854 btn->x_pos = x_pos;
855 btn->y_pos = 0;
856 btn->docked = 1;
857 if (type == WM_CLIP ||
858 (type == WM_DOCK && wPreferences.flags.clip_merged_in_dock))
859 scr->clip_icon = btn;
861 return btn;
864 static void switchWSCommand(WMenu *menu, WMenuEntry *entry)
866 WAppIcon *btn, *icon = (WAppIcon *) entry->clientdata;
867 WScreen *scr = icon->icon->core->screen_ptr;
868 WDock *src, *dest;
869 WMArray *selectedIcons;
870 int x, y;
872 if (entry->order == scr->current_workspace)
873 return;
874 src = icon->dock;
875 dest = scr->workspaces[entry->order]->clip;
877 selectedIcons = getSelected(src);
879 if (WMGetArrayItemCount(selectedIcons)) {
880 WMArrayIterator iter;
882 WM_ITERATE_ARRAY(selectedIcons, btn, iter) {
883 if (wDockFindFreeSlot(dest, &x, &y)) {
884 wDockMoveIconBetweenDocks(src, dest, btn, x, y);
885 XUnmapWindow(dpy, btn->icon->core->window);
888 } else if (icon != scr->clip_icon) {
889 if (wDockFindFreeSlot(dest, &x, &y)) {
890 wDockMoveIconBetweenDocks(src, dest, icon, x, y);
891 XUnmapWindow(dpy, icon->icon->core->window);
894 WMFreeArray(selectedIcons);
897 static void launchDockedApplication(WAppIcon *btn, Bool withSelection)
899 WScreen *scr = btn->icon->core->screen_ptr;
901 if (!btn->launching &&
902 ((!withSelection && btn->command != NULL) || (withSelection && btn->paste_command != NULL))) {
903 if (!btn->forced_dock) {
904 btn->relaunching = btn->running;
905 btn->running = 1;
907 if (btn->wm_instance || btn->wm_class) {
908 WWindowAttributes attr;
909 memset(&attr, 0, sizeof(WWindowAttributes));
910 wDefaultFillAttributes(btn->wm_instance, btn->wm_class, &attr, NULL, True);
912 if (!attr.no_appicon && !btn->buggy_app)
913 btn->launching = 1;
914 else
915 btn->running = 0;
917 btn->drop_launch = 0;
918 btn->paste_launch = withSelection;
919 scr->last_dock = btn->dock;
920 btn->pid = execCommand(btn, (withSelection ? btn->paste_command : btn->command), NULL);
921 if (btn->pid > 0) {
922 if (btn->buggy_app) {
923 /* give feedback that the app was launched */
924 btn->launching = 1;
925 dockIconPaint(btn);
926 btn->launching = 0;
927 WMAddTimerHandler(200, (WMCallback *) dockIconPaint, btn);
928 } else {
929 dockIconPaint(btn);
931 } else {
932 wwarning(_("could not launch application %s"), btn->command);
933 btn->launching = 0;
934 if (!btn->relaunching)
935 btn->running = 0;
940 static void updateWorkspaceMenu(WMenu *menu, WAppIcon *icon)
942 WScreen *scr = menu->frame->screen_ptr;
943 char title[MAX_WORKSPACENAME_WIDTH + 1];
944 int i;
946 if (!menu || !icon)
947 return;
949 for (i = 0; i < scr->workspace_count; i++) {
950 if (i < menu->entry_no) {
951 if (strcmp(menu->entries[i]->text, scr->workspaces[i]->name) != 0) {
952 wfree(menu->entries[i]->text);
953 strcpy(title, scr->workspaces[i]->name);
954 menu->entries[i]->text = wstrdup(title);
955 menu->flags.realized = 0;
957 menu->entries[i]->clientdata = (void *)icon;
958 } else {
959 strcpy(title, scr->workspaces[i]->name);
961 wMenuAddCallback(menu, title, switchWSCommand, (void *)icon);
963 menu->flags.realized = 0;
966 if (i == scr->current_workspace)
967 wMenuSetEnabled(menu, i, False);
968 else
969 wMenuSetEnabled(menu, i, True);
972 if (!menu->flags.realized)
973 wMenuRealize(menu);
976 static WMenu *makeWorkspaceMenu(WScreen *scr)
978 WMenu *menu;
980 menu = wMenuCreate(scr, NULL, False);
981 if (!menu)
982 wwarning(_("could not create workspace submenu for Clip menu"));
984 wMenuAddCallback(menu, "", switchWSCommand, (void *)scr->clip_icon);
986 menu->flags.realized = 0;
987 wMenuRealize(menu);
989 return menu;
992 static void updateClipOptionsMenu(WMenu *menu, WDock *dock)
994 WMenuEntry *entry;
995 int index = 0;
997 if (!menu || !dock)
998 return;
1000 /* keep on top */
1001 entry = menu->entries[index];
1002 entry->flags.indicator_on = !dock->lowered;
1003 entry->clientdata = dock;
1004 wMenuSetEnabled(menu, index, dock->type == WM_CLIP);
1006 /* collapsed */
1007 entry = menu->entries[++index];
1008 entry->flags.indicator_on = dock->collapsed;
1009 entry->clientdata = dock;
1011 /* auto-collapse */
1012 entry = menu->entries[++index];
1013 entry->flags.indicator_on = dock->auto_collapse;
1014 entry->clientdata = dock;
1016 /* auto-raise/lower */
1017 entry = menu->entries[++index];
1018 entry->flags.indicator_on = dock->auto_raise_lower;
1019 entry->clientdata = dock;
1020 wMenuSetEnabled(menu, index, dock->lowered && (dock->type == WM_CLIP));
1022 /* attract icons */
1023 entry = menu->entries[++index];
1024 entry->flags.indicator_on = dock->attract_icons;
1025 entry->clientdata = dock;
1027 menu->flags.realized = 0;
1028 wMenuRealize(menu);
1032 static WMenu *makeClipOptionsMenu(WScreen *scr)
1034 WMenu *menu;
1035 WMenuEntry *entry;
1037 menu = wMenuCreate(scr, NULL, False);
1038 if (!menu) {
1039 wwarning(_("could not create options submenu for Clip menu"));
1040 return NULL;
1043 entry = wMenuAddCallback(menu, _("Keep on Top"), toggleLoweredCallback, NULL);
1044 entry->flags.indicator = 1;
1045 entry->flags.indicator_on = 1;
1046 entry->flags.indicator_type = MI_CHECK;
1048 entry = wMenuAddCallback(menu, _("Collapsed"), toggleCollapsedCallback, NULL);
1049 entry->flags.indicator = 1;
1050 entry->flags.indicator_on = 1;
1051 entry->flags.indicator_type = MI_CHECK;
1053 entry = wMenuAddCallback(menu, _("Autocollapse"), toggleAutoCollapseCallback, NULL);
1054 entry->flags.indicator = 1;
1055 entry->flags.indicator_on = 1;
1056 entry->flags.indicator_type = MI_CHECK;
1058 entry = wMenuAddCallback(menu, _("Autoraise"), toggleAutoRaiseLowerCallback, NULL);
1059 entry->flags.indicator = 1;
1060 entry->flags.indicator_on = 1;
1061 entry->flags.indicator_type = MI_CHECK;
1063 entry = wMenuAddCallback(menu, _("Autoattract Icons"), toggleAutoAttractCallback, NULL);
1064 entry->flags.indicator = 1;
1065 entry->flags.indicator_on = 1;
1066 entry->flags.indicator_type = MI_CHECK;
1068 menu->flags.realized = 0;
1069 wMenuRealize(menu);
1071 return menu;
1075 static void setDockPositionNormalCallback(WMenu *menu, WMenuEntry *entry)
1077 WDock *dock = (WDock *) entry->clientdata;
1078 WDrawerChain *dc;
1079 if (entry->flags.indicator_on) // already set, nothing to do
1080 return;
1081 // Do we come from auto raise lower or keep on top?
1082 if (dock->auto_raise_lower)
1084 dock->auto_raise_lower = 0;
1085 // Only for aesthetic purposes, can be removed when Autoraise status is no longer exposed in drawer option menu
1086 for (dc = dock->screen_ptr->drawers; dc != NULL; dc = dc->next) {
1087 dc->adrawer->auto_raise_lower = 0;
1090 else
1092 // Will take care of setting lowered = 0 in drawers
1093 toggleLowered(dock);
1095 entry->flags.indicator_on = 1;
1098 static void setDockPositionAutoRaiseLowerCallback(WMenu *menu, WMenuEntry *entry)
1100 WDock *dock = (WDock *) entry->clientdata;
1101 WDrawerChain *dc;
1102 if (entry->flags.indicator_on) // already set, nothing to do
1103 return;
1104 // Do we come from normal or keep on top?
1105 if (!dock->lowered)
1107 toggleLowered(dock);
1109 dock->auto_raise_lower = 1;
1110 // Only for aesthetic purposes, can be removed when Autoraise status is no longer exposed in drawer option menu
1111 for (dc = dock->screen_ptr->drawers; dc != NULL; dc = dc->next) {
1112 dc->adrawer->auto_raise_lower = 1;
1114 entry->flags.indicator_on = 1;
1117 static void setDockPositionKeepOnTopCallback(WMenu *menu, WMenuEntry *entry)
1119 WDock *dock = (WDock *) entry->clientdata;
1120 WDrawerChain *dc;
1121 if (entry->flags.indicator_on) // already set, nothing to do
1122 return;
1123 dock->auto_raise_lower = 0;
1124 // Only for aesthetic purposes, can be removed when Autoraise status is no longer exposed in drawer option menu
1125 for (dc = dock->screen_ptr->drawers; dc != NULL; dc = dc->next) {
1126 dc->adrawer->auto_raise_lower = 0;
1128 toggleLowered(dock);
1129 entry->flags.indicator_on = 1;
1132 static void updateDockPositionMenu(WMenu *menu, WDock *dock)
1134 WMenuEntry *entry;
1135 int index = 0;
1137 assert(menu);
1138 assert(dock);
1140 /* Normal level */
1141 entry = menu->entries[index++];
1142 entry->flags.indicator_on = (dock->lowered && !dock->auto_raise_lower);
1143 entry->clientdata = dock;
1145 /* Auto-raise/lower */
1146 entry = menu->entries[index++];
1147 entry->flags.indicator_on = dock->auto_raise_lower;
1148 entry->clientdata = dock;
1150 /* Keep on top */
1151 entry = menu->entries[index++];
1152 entry->flags.indicator_on = !dock->lowered;
1153 entry->clientdata = dock;
1156 static WMenu *makeDockPositionMenu(WScreen *scr)
1158 /* When calling this, the dock is being created, so scr->dock is still not set
1159 * Therefore the callbacks' clientdata and the indicators can't be set,
1160 * they will be updated when the dock menu is opened. */
1161 WMenu *menu;
1162 WMenuEntry *entry;
1164 menu = wMenuCreate(scr, NULL, False);
1165 if (!menu) {
1166 wwarning(_("could not create options submenu for dock position menu"));
1167 return NULL;
1170 entry = wMenuAddCallback(menu, _("Normal"), setDockPositionNormalCallback, NULL);
1171 entry->flags.indicator = 1;
1172 entry->flags.indicator_type = MI_DIAMOND;
1174 entry = wMenuAddCallback(menu, _("Auto raise & lower"), setDockPositionAutoRaiseLowerCallback, NULL);
1175 entry->flags.indicator = 1;
1176 entry->flags.indicator_type = MI_DIAMOND;
1178 entry = wMenuAddCallback(menu, _("Keep on Top"), setDockPositionKeepOnTopCallback, NULL);
1179 entry->flags.indicator = 1;
1180 entry->flags.indicator_type = MI_DIAMOND;
1182 menu->flags.realized = 0;
1183 wMenuRealize(menu);
1185 return menu;
1189 static WMenu *dockMenuCreate(WScreen *scr, int type)
1191 WMenu *menu;
1192 WMenuEntry *entry;
1194 if (type == WM_CLIP && scr->clip_menu)
1195 return scr->clip_menu;
1197 if (type == WM_DRAWER && scr->drawer_menu)
1198 return scr->drawer_menu;
1200 menu = wMenuCreate(scr, NULL, False);
1201 if (type == WM_DOCK) {
1202 entry = wMenuAddCallback(menu, _("Dock position"), NULL, NULL);
1203 if (scr->dock_pos_menu == NULL)
1204 scr->dock_pos_menu = makeDockPositionMenu(scr);
1205 wMenuEntrySetCascade(menu, entry, scr->dock_pos_menu);
1207 if (!wPreferences.flags.nodrawer) {
1208 entry = wMenuAddCallback(menu, _("Add a drawer"), addADrawerCallback, NULL);
1210 } else {
1211 if (type == WM_CLIP)
1212 entry = wMenuAddCallback(menu, _("Clip Options"), NULL, NULL);
1213 else /* if (type == WM_DRAWER) */
1214 entry = wMenuAddCallback(menu, _("Drawer options"), NULL, NULL);
1216 if (scr->clip_options == NULL)
1217 scr->clip_options = makeClipOptionsMenu(scr);
1218 wMenuEntrySetCascade(menu, entry, scr->clip_options);
1220 /* The same menu is used for the dock and its appicons. If the menu
1221 * entry text is different between the two contexts, or if it can
1222 * change depending on some state, free the duplicated string (from
1223 * wMenuInsertCallback) and use gettext's string */
1224 if (type == WM_CLIP) {
1225 entry = wMenuAddCallback(menu, _("Rename Workspace"), renameCallback, NULL);
1226 wfree(entry->text);
1227 entry->text = _("Rename Workspace"); /* can be: (Toggle) Omnipresent */
1230 entry = wMenuAddCallback(menu, _("Selected"), selectCallback, NULL);
1231 entry->flags.indicator = 1;
1232 entry->flags.indicator_on = 1;
1233 entry->flags.indicator_type = MI_CHECK;
1235 entry = wMenuAddCallback(menu, _("Select All Icons"), selectIconsCallback, NULL);
1236 wfree(entry->text);
1237 entry->text = _("Select All Icons"); /* can be: Unselect all icons */
1239 entry = wMenuAddCallback(menu, _("Keep Icon"), keepIconsCallback, NULL);
1240 wfree(entry->text);
1241 entry->text = _("Keep Icon"); /* can be: Keep Icons */
1243 if (type == WM_CLIP) {
1244 entry = wMenuAddCallback(menu, _("Move Icon To"), NULL, NULL);
1245 wfree(entry->text);
1246 entry->text = _("Move Icon To"); /* can be: Move Icons to */
1247 scr->clip_submenu = makeWorkspaceMenu(scr);
1248 if (scr->clip_submenu)
1249 wMenuEntrySetCascade(menu, entry, scr->clip_submenu);
1252 entry = wMenuAddCallback(menu, _("Remove Icon"), removeIconsCallback, NULL);
1253 wfree(entry->text);
1254 entry->text = _("Remove Icon"); /* can be: Remove Icons */
1256 wMenuAddCallback(menu, _("Attract Icons"), colectIconsCallback, NULL);
1259 wMenuAddCallback(menu, _("Launch"), launchCallback, NULL);
1261 wMenuAddCallback(menu, _("Unhide Here"), unhideHereCallback, NULL);
1263 entry = wMenuAddCallback(menu, _("Hide"), hideCallback, NULL);
1264 wfree(entry->text);
1265 entry->text = _("Hide"); /* can be: Unhide */
1267 wMenuAddCallback(menu, _("Settings..."), settingsCallback, NULL);
1269 entry = wMenuAddCallback(menu, _("Kill"), killCallback, NULL);
1270 wfree(entry->text);
1271 entry->text = _("Kill"); /* can be: Remove drawer */
1273 if (type == WM_CLIP)
1274 scr->clip_menu = menu;
1276 if (type == WM_DRAWER)
1277 scr->drawer_menu = menu;
1279 return menu;
1282 WDock *wDockCreate(WScreen *scr, int type, const char *name)
1284 WDock *dock;
1285 WAppIcon *btn;
1287 make_keys();
1289 dock = wmalloc(sizeof(WDock));
1291 switch (type) {
1292 case WM_CLIP:
1293 dock->max_icons = DOCK_MAX_ICONS;
1294 break;
1295 case WM_DRAWER:
1296 dock->max_icons = scr->scr_width / wPreferences.icon_size;
1297 break;
1298 case WM_DOCK:
1299 default:
1300 dock->max_icons = scr->scr_height / wPreferences.icon_size;
1303 dock->icon_array = wmalloc(sizeof(WAppIcon *) * dock->max_icons);
1305 btn = mainIconCreate(scr, type, name);
1307 btn->dock = dock;
1309 dock->x_pos = btn->x_pos;
1310 dock->y_pos = btn->y_pos;
1311 dock->screen_ptr = scr;
1312 dock->type = type;
1313 dock->icon_count = 1;
1314 if (type == WM_DRAWER)
1315 dock->on_right_side = scr->dock->on_right_side;
1316 else
1317 dock->on_right_side = 1;
1318 dock->collapsed = 0;
1319 dock->auto_collapse = 0;
1320 dock->auto_collapse_magic = NULL;
1321 dock->auto_raise_lower = 0;
1322 dock->auto_lower_magic = NULL;
1323 dock->auto_raise_magic = NULL;
1324 dock->attract_icons = 0;
1325 dock->lowered = 1;
1326 dock->icon_array[0] = btn;
1327 wRaiseFrame(btn->icon->core);
1328 XMoveWindow(dpy, btn->icon->core->window, btn->x_pos, btn->y_pos);
1330 /* create dock menu */
1331 dock->menu = dockMenuCreate(scr, type);
1333 if (type == WM_DRAWER) {
1334 drawerAppendToChain(scr, dock);
1335 dock->auto_collapse = 1;
1338 return dock;
1341 void wDockDestroy(WDock *dock)
1343 int i;
1344 WAppIcon *aicon;
1346 for (i = (dock->type == WM_CLIP) ? 1 : 0; i < dock->max_icons; i++) {
1347 aicon = dock->icon_array[i];
1348 if (aicon) {
1349 int keepit = aicon->running && wApplicationOf(aicon->main_window);
1350 wDockDetach(dock, aicon);
1351 if (keepit) {
1352 /* XXX: can: aicon->icon == NULL ? */
1353 PlaceIcon(dock->screen_ptr, &aicon->x_pos, &aicon->y_pos,
1354 wGetHeadForWindow(aicon->icon->owner));
1355 XMoveWindow(dpy, aicon->icon->core->window, aicon->x_pos, aicon->y_pos);
1356 if (!dock->mapped || dock->collapsed)
1357 XMapWindow(dpy, aicon->icon->core->window);
1361 if (wPreferences.auto_arrange_icons)
1362 wArrangeIcons(dock->screen_ptr, True);
1363 wfree(dock->icon_array);
1364 if (dock->menu && dock->type != WM_CLIP)
1365 wMenuDestroy(dock->menu, True);
1366 if (dock->screen_ptr->last_dock == dock)
1367 dock->screen_ptr->last_dock = NULL;
1368 wfree(dock);
1371 void wClipIconPaint(WAppIcon *aicon)
1373 WScreen *scr = aicon->icon->core->screen_ptr;
1374 WWorkspace *workspace = scr->workspaces[scr->current_workspace];
1375 WMColor *color;
1376 Window win = aicon->icon->core->window;
1377 int length, nlength;
1378 char *ws_name, ws_number[10];
1379 int ty, tx;
1381 wIconPaint(aicon->icon);
1383 length = strlen(workspace->name);
1384 ws_name = wmalloc(length + 1);
1385 snprintf(ws_name, length + 1, "%s", workspace->name);
1386 snprintf(ws_number, sizeof(ws_number), "%i", scr->current_workspace + 1);
1387 nlength = strlen(ws_number);
1389 if (wPreferences.flags.noclip || !workspace->clip->collapsed)
1390 color = scr->clip_title_color[CLIP_NORMAL];
1391 else
1392 color = scr->clip_title_color[CLIP_COLLAPSED];
1394 ty = ICON_SIZE - WMFontHeight(scr->clip_title_font) - 3;
1396 tx = CLIP_BUTTON_SIZE * ICON_SIZE / 64;
1398 if(wPreferences.show_clip_title)
1399 WMDrawString(scr->wmscreen, win, color, scr->clip_title_font, tx, ty, ws_name, length);
1401 tx = (ICON_SIZE / 2 - WMWidthOfString(scr->clip_title_font, ws_number, nlength)) / 2;
1403 WMDrawString(scr->wmscreen, win, color, scr->clip_title_font, tx, 2, ws_number, nlength);
1405 wfree(ws_name);
1407 if (aicon->launching)
1408 XFillRectangle(dpy, aicon->icon->core->window, scr->stipple_gc,
1409 0, 0, wPreferences.icon_size, wPreferences.icon_size);
1411 paintClipButtons(aicon, aicon->dock->lclip_button_pushed, aicon->dock->rclip_button_pushed);
1414 static void clipIconExpose(WObjDescriptor *desc, XEvent *event)
1416 wClipIconPaint(desc->parent);
1419 static void dockIconPaint(WAppIcon *btn)
1421 if (btn == btn->icon->core->screen_ptr->clip_icon) {
1422 wClipIconPaint(btn);
1423 } else if (wIsADrawer(btn->icon->core->screen_ptr, btn)) {
1424 wDrawerIconPaint(btn);
1425 } else {
1426 wAppIconPaint(btn);
1427 save_appicon(btn, True);
1431 static WMPropList *make_icon_state(WAppIcon *btn)
1433 WMPropList *node = NULL;
1434 WMPropList *command, *autolaunch, *lock, *name, *forced;
1435 WMPropList *position, *buggy, *omnipresent;
1436 char *tmp;
1437 char buffer[64];
1439 if (btn) {
1440 if (!btn->command)
1441 command = WMCreatePLString("-");
1442 else
1443 command = WMCreatePLString(btn->command);
1445 autolaunch = btn->auto_launch ? dYes : dNo;
1447 lock = btn->lock ? dYes : dNo;
1449 tmp = EscapeWM_CLASS(btn->wm_instance, btn->wm_class);
1451 name = WMCreatePLString(tmp);
1453 wfree(tmp);
1455 forced = btn->forced_dock ? dYes : dNo;
1457 buggy = btn->buggy_app ? dYes : dNo;
1459 if (!wPreferences.flags.clip_merged_in_dock && btn == btn->icon->core->screen_ptr->clip_icon)
1460 snprintf(buffer, sizeof(buffer), "%i,%i", btn->x_pos, btn->y_pos);
1461 else
1462 snprintf(buffer, sizeof(buffer), "%hi,%hi", btn->xindex, btn->yindex);
1463 position = WMCreatePLString(buffer);
1465 node = WMCreatePLDictionary(dCommand, command,
1466 dName, name,
1467 dAutoLaunch, autolaunch,
1468 dLock, lock,
1469 dForced, forced, dBuggyApplication, buggy, dPosition, position, NULL);
1470 WMReleasePropList(command);
1471 WMReleasePropList(name);
1472 WMReleasePropList(position);
1474 omnipresent = btn->omnipresent ? dYes : dNo;
1475 if (btn->dock != btn->icon->core->screen_ptr->dock && (btn->xindex != 0 || btn->yindex != 0))
1476 WMPutInPLDictionary(node, dOmnipresent, omnipresent);
1478 #ifdef XDND /* was OFFIX */
1479 if (btn->dnd_command) {
1480 command = WMCreatePLString(btn->dnd_command);
1481 WMPutInPLDictionary(node, dDropCommand, command);
1482 WMReleasePropList(command);
1484 #endif /* XDND */
1486 if (btn->paste_command) {
1487 command = WMCreatePLString(btn->paste_command);
1488 WMPutInPLDictionary(node, dPasteCommand, command);
1489 WMReleasePropList(command);
1493 return node;
1496 static WMPropList *dockSaveState(WDock *dock)
1498 int i;
1499 WMPropList *icon_info;
1500 WMPropList *list = NULL, *dock_state = NULL;
1501 WMPropList *value, *key;
1502 char buffer[256];
1504 list = WMCreatePLArray(NULL);
1506 for (i = (dock->type == WM_DOCK ? 0 : 1); i < dock->max_icons; i++) {
1507 WAppIcon *btn = dock->icon_array[i];
1509 if (!btn || btn->attracted)
1510 continue;
1512 if ((icon_info = make_icon_state(dock->icon_array[i]))) {
1513 WMAddToPLArray(list, icon_info);
1514 WMReleasePropList(icon_info);
1518 dock_state = WMCreatePLDictionary(dApplications, list, NULL);
1520 if (dock->type == WM_DOCK) {
1521 snprintf(buffer, sizeof(buffer), "Applications%i", dock->screen_ptr->scr_height);
1522 key = WMCreatePLString(buffer);
1523 WMPutInPLDictionary(dock_state, key, list);
1524 WMReleasePropList(key);
1526 snprintf(buffer, sizeof(buffer), "%i,%i", (dock->on_right_side ? -ICON_SIZE : 0), dock->y_pos);
1527 value = WMCreatePLString(buffer);
1528 WMPutInPLDictionary(dock_state, dPosition, value);
1529 WMReleasePropList(value);
1531 WMReleasePropList(list);
1533 if (dock->type == WM_CLIP || dock->type == WM_DRAWER) {
1534 value = (dock->collapsed ? dYes : dNo);
1535 WMPutInPLDictionary(dock_state, dCollapsed, value);
1537 value = (dock->auto_collapse ? dYes : dNo);
1538 WMPutInPLDictionary(dock_state, dAutoCollapse, value);
1540 value = (dock->attract_icons ? dYes : dNo);
1541 WMPutInPLDictionary(dock_state, dAutoAttractIcons, value);
1544 if (dock->type == WM_DOCK || dock->type == WM_CLIP) {
1545 value = (dock->lowered ? dYes : dNo);
1546 WMPutInPLDictionary(dock_state, dLowered, value);
1548 value = (dock->auto_raise_lower ? dYes : dNo);
1549 WMPutInPLDictionary(dock_state, dAutoRaiseLower, value);
1552 return dock_state;
1555 void wDockSaveState(WScreen *scr, WMPropList *old_state)
1557 WMPropList *dock_state;
1558 WMPropList *keys;
1560 dock_state = dockSaveState(scr->dock);
1563 * Copy saved states of docks with different sizes.
1565 if (old_state) {
1566 int i;
1567 WMPropList *tmp;
1569 keys = WMGetPLDictionaryKeys(old_state);
1570 for (i = 0; i < WMGetPropListItemCount(keys); i++) {
1571 tmp = WMGetFromPLArray(keys, i);
1573 if (strncasecmp(WMGetFromPLString(tmp), "applications", 12) == 0
1574 && !WMGetFromPLDictionary(dock_state, tmp)) {
1576 WMPutInPLDictionary(dock_state, tmp, WMGetFromPLDictionary(old_state, tmp));
1579 WMReleasePropList(keys);
1582 WMPutInPLDictionary(scr->session_state, dDock, dock_state);
1584 WMReleasePropList(dock_state);
1587 void wClipSaveState(WScreen *scr)
1589 WMPropList *clip_state;
1591 clip_state = make_icon_state(scr->clip_icon);
1593 WMPutInPLDictionary(scr->session_state, dClip, clip_state);
1595 WMReleasePropList(clip_state);
1598 WMPropList *wClipSaveWorkspaceState(WScreen *scr, int workspace)
1600 return dockSaveState(scr->workspaces[workspace]->clip);
1603 static Bool getBooleanDockValue(WMPropList *value, WMPropList *key)
1605 if (value) {
1606 if (WMIsPLString(value)) {
1607 if (strcasecmp(WMGetFromPLString(value), "YES") == 0)
1608 return True;
1609 } else {
1610 wwarning(_("bad value in docked icon state info %s"), WMGetFromPLString(key));
1613 return False;
1616 static WAppIcon *restore_icon_state(WScreen *scr, WMPropList *info, int type, int index)
1618 WAppIcon *aicon;
1619 WMPropList *cmd, *value;
1620 char *wclass, *winstance, *command;
1622 cmd = WMGetFromPLDictionary(info, dCommand);
1623 if (!cmd || !WMIsPLString(cmd))
1624 return NULL;
1626 /* parse window name */
1627 value = WMGetFromPLDictionary(info, dName);
1628 if (!value)
1629 return NULL;
1631 ParseWindowName(value, &winstance, &wclass, "dock");
1633 if (!winstance && !wclass)
1634 return NULL;
1636 /* get commands */
1637 if (cmd)
1638 command = wstrdup(WMGetFromPLString(cmd));
1639 else
1640 command = NULL;
1642 if (!command || strcmp(command, "-") == 0) {
1643 if (command)
1644 wfree(command);
1645 if (wclass)
1646 wfree(wclass);
1647 if (winstance)
1648 wfree(winstance);
1650 return NULL;
1653 aicon = wAppIconCreateForDock(scr, command, winstance, wclass, TILE_NORMAL);
1654 if (wclass)
1655 wfree(wclass);
1656 if (winstance)
1657 wfree(winstance);
1658 if (command)
1659 wfree(command);
1661 aicon->icon->core->descriptor.handle_mousedown = iconMouseDown;
1662 aicon->icon->core->descriptor.handle_enternotify = clipEnterNotify;
1663 aicon->icon->core->descriptor.handle_leavenotify = clipLeaveNotify;
1664 aicon->icon->core->descriptor.parent_type = WCLASS_DOCK_ICON;
1665 aicon->icon->core->descriptor.parent = aicon;
1667 #ifdef XDND /* was OFFIX */
1668 cmd = WMGetFromPLDictionary(info, dDropCommand);
1669 if (cmd)
1670 aicon->dnd_command = wstrdup(WMGetFromPLString(cmd));
1671 #endif
1673 cmd = WMGetFromPLDictionary(info, dPasteCommand);
1674 if (cmd)
1675 aicon->paste_command = wstrdup(WMGetFromPLString(cmd));
1677 /* check auto launch */
1678 value = WMGetFromPLDictionary(info, dAutoLaunch);
1680 aicon->auto_launch = getBooleanDockValue(value, dAutoLaunch);
1682 /* check lock */
1683 value = WMGetFromPLDictionary(info, dLock);
1685 aicon->lock = getBooleanDockValue(value, dLock);
1687 /* check if it wasn't normally docked */
1688 value = WMGetFromPLDictionary(info, dForced);
1690 aicon->forced_dock = getBooleanDockValue(value, dForced);
1692 /* check if we can rely on the stuff in the app */
1693 value = WMGetFromPLDictionary(info, dBuggyApplication);
1695 aicon->buggy_app = getBooleanDockValue(value, dBuggyApplication);
1697 /* get position in the dock */
1698 value = WMGetFromPLDictionary(info, dPosition);
1699 if (value && WMIsPLString(value)) {
1700 if (sscanf(WMGetFromPLString(value), "%hi,%hi", &aicon->xindex, &aicon->yindex) != 2)
1701 wwarning(_("bad value in docked icon state info %s"), WMGetFromPLString(dPosition));
1703 /* check position sanity */
1704 /* *Very* incomplete section! */
1705 if (type == WM_DOCK) {
1706 aicon->xindex = 0;
1708 } else {
1709 aicon->yindex = index;
1710 aicon->xindex = 0;
1713 /* check if icon is omnipresent */
1714 value = WMGetFromPLDictionary(info, dOmnipresent);
1716 aicon->omnipresent = getBooleanDockValue(value, dOmnipresent);
1718 aicon->running = 0;
1719 aicon->docked = 1;
1721 return aicon;
1724 #define COMPLAIN(key) wwarning(_("bad value in dock state info:%s"), key)
1726 WAppIcon *wClipRestoreState(WScreen *scr, WMPropList *clip_state)
1728 WAppIcon *icon;
1729 WMPropList *value;
1731 icon = mainIconCreate(scr, WM_CLIP, NULL);
1733 if (!clip_state)
1734 return icon;
1736 WMRetainPropList(clip_state);
1738 /* restore position */
1740 value = WMGetFromPLDictionary(clip_state, dPosition);
1742 if (value) {
1743 if (!WMIsPLString(value)) {
1744 COMPLAIN("Position");
1745 } else {
1746 if (sscanf(WMGetFromPLString(value), "%i,%i", &icon->x_pos, &icon->y_pos) != 2)
1747 COMPLAIN("Position");
1749 /* check position sanity */
1750 if (!onScreen(scr, icon->x_pos, icon->y_pos))
1751 wScreenKeepInside(scr, &icon->x_pos, &icon->y_pos, ICON_SIZE, ICON_SIZE);
1754 #ifdef XDND /* was OFFIX */
1755 value = WMGetFromPLDictionary(clip_state, dDropCommand);
1756 if (value && WMIsPLString(value))
1757 icon->dnd_command = wstrdup(WMGetFromPLString(value));
1758 #endif
1760 value = WMGetFromPLDictionary(clip_state, dPasteCommand);
1761 if (value && WMIsPLString(value))
1762 icon->paste_command = wstrdup(WMGetFromPLString(value));
1764 WMReleasePropList(clip_state);
1766 return icon;
1769 WDock *wDockRestoreState(WScreen *scr, WMPropList *dock_state, int type)
1771 WDock *dock;
1772 WMPropList *apps;
1773 WMPropList *value;
1774 WAppIcon *aicon, *old_top;
1775 int count, i;
1777 dock = wDockCreate(scr, type, NULL);
1779 if (!dock_state)
1780 return dock;
1782 WMRetainPropList(dock_state);
1784 /* restore position */
1785 value = WMGetFromPLDictionary(dock_state, dPosition);
1786 if (value) {
1787 if (!WMIsPLString(value)) {
1788 COMPLAIN("Position");
1789 } else {
1790 if (sscanf(WMGetFromPLString(value), "%i,%i", &dock->x_pos, &dock->y_pos) != 2)
1791 COMPLAIN("Position");
1793 /* check position sanity */
1794 if (!onScreen(scr, dock->x_pos, dock->y_pos)) {
1795 int x = dock->x_pos;
1796 wScreenKeepInside(scr, &x, &dock->y_pos, ICON_SIZE, ICON_SIZE);
1799 /* Is this needed any more? */
1800 if (type == WM_CLIP) {
1801 if (dock->x_pos < 0) {
1802 dock->x_pos = 0;
1803 } else if (dock->x_pos > scr->scr_width - ICON_SIZE) {
1804 dock->x_pos = scr->scr_width - ICON_SIZE;
1806 } else {
1807 if (dock->x_pos >= 0) {
1808 dock->x_pos = DOCK_EXTRA_SPACE;
1809 dock->on_right_side = 0;
1810 } else {
1811 dock->x_pos = scr->scr_width - DOCK_EXTRA_SPACE - ICON_SIZE;
1812 dock->on_right_side = 1;
1818 /* restore lowered/raised state */
1819 dock->lowered = 0;
1821 value = WMGetFromPLDictionary(dock_state, dLowered);
1822 if (value) {
1823 if (!WMIsPLString(value)) {
1824 COMPLAIN("Lowered");
1825 } else {
1826 if (strcasecmp(WMGetFromPLString(value), "YES") == 0)
1827 dock->lowered = 1;
1831 /* restore collapsed state */
1832 dock->collapsed = 0;
1834 value = WMGetFromPLDictionary(dock_state, dCollapsed);
1835 if (value) {
1836 if (!WMIsPLString(value)) {
1837 COMPLAIN("Collapsed");
1838 } else {
1839 if (strcasecmp(WMGetFromPLString(value), "YES") == 0)
1840 dock->collapsed = 1;
1844 /* restore auto-collapsed state */
1845 value = WMGetFromPLDictionary(dock_state, dAutoCollapse);
1846 if (value) {
1847 if (!WMIsPLString(value)) {
1848 COMPLAIN("AutoCollapse");
1849 } else {
1850 if (strcasecmp(WMGetFromPLString(value), "YES") == 0) {
1851 dock->auto_collapse = 1;
1852 dock->collapsed = 1;
1857 /* restore auto-raise/lower state */
1858 value = WMGetFromPLDictionary(dock_state, dAutoRaiseLower);
1859 if (value) {
1860 if (!WMIsPLString(value)) {
1861 COMPLAIN("AutoRaiseLower");
1862 } else {
1863 if (strcasecmp(WMGetFromPLString(value), "YES") == 0)
1864 dock->auto_raise_lower = 1;
1868 /* restore attract icons state */
1869 dock->attract_icons = 0;
1871 value = WMGetFromPLDictionary(dock_state, dAutoAttractIcons);
1872 if (value) {
1873 if (!WMIsPLString(value)) {
1874 COMPLAIN("AutoAttractIcons");
1875 } else {
1876 if (strcasecmp(WMGetFromPLString(value), "YES") == 0)
1877 dock->attract_icons = 1;
1881 /* application list */
1884 WMPropList *tmp;
1885 char buffer[64];
1888 * When saving, it saves the dock state in
1889 * Applications and Applicationsnnn
1891 * When loading, it will first try Applicationsnnn.
1892 * If it does not exist, use Applications as default.
1895 snprintf(buffer, sizeof(buffer), "Applications%i", scr->scr_height);
1897 tmp = WMCreatePLString(buffer);
1898 apps = WMGetFromPLDictionary(dock_state, tmp);
1899 WMReleasePropList(tmp);
1901 if (!apps)
1902 apps = WMGetFromPLDictionary(dock_state, dApplications);
1905 if (!apps)
1906 goto finish;
1908 count = WMGetPropListItemCount(apps);
1909 if (count == 0)
1910 goto finish;
1912 old_top = dock->icon_array[0];
1914 /* dock->icon_count is set to 1 when dock is created.
1915 * Since Clip is already restored, we want to keep it so for clip,
1916 * but for dock we may change the default top tile, so we set it to 0.
1918 if (type == WM_DOCK)
1919 dock->icon_count = 0;
1921 for (i = 0; i < count; i++) {
1922 if (dock->icon_count >= dock->max_icons) {
1923 wwarning(_("there are too many icons stored in dock. Ignoring what doesn't fit"));
1924 break;
1927 value = WMGetFromPLArray(apps, i);
1928 aicon = restore_icon_state(scr, value, type, dock->icon_count);
1930 dock->icon_array[dock->icon_count] = aicon;
1932 if (aicon) {
1933 aicon->dock = dock;
1934 aicon->x_pos = dock->x_pos + (aicon->xindex * ICON_SIZE);
1935 aicon->y_pos = dock->y_pos + (aicon->yindex * ICON_SIZE);
1937 if (dock->lowered)
1938 ChangeStackingLevel(aicon->icon->core, WMNormalLevel);
1939 else
1940 ChangeStackingLevel(aicon->icon->core, WMDockLevel);
1942 wCoreConfigure(aicon->icon->core, aicon->x_pos, aicon->y_pos, 0, 0);
1943 if (!dock->collapsed)
1944 XMapWindow(dpy, aicon->icon->core->window);
1946 wRaiseFrame(aicon->icon->core);
1948 dock->icon_count++;
1949 } else if (dock->icon_count == 0 && type == WM_DOCK) {
1950 dock->icon_count++;
1954 /* if the first icon is not defined, use the default */
1955 if (dock->icon_array[0] == NULL) {
1956 /* update default icon */
1957 old_top->x_pos = dock->x_pos;
1958 old_top->y_pos = dock->y_pos;
1959 if (dock->lowered)
1960 ChangeStackingLevel(old_top->icon->core, WMNormalLevel);
1961 else
1962 ChangeStackingLevel(old_top->icon->core, WMDockLevel);
1964 dock->icon_array[0] = old_top;
1965 XMoveWindow(dpy, old_top->icon->core->window, dock->x_pos, dock->y_pos);
1966 /* we don't need to increment dock->icon_count here because it was
1967 * incremented in the loop above.
1969 } else if (old_top != dock->icon_array[0]) {
1970 if (old_top == scr->clip_icon) // TODO dande: understand the logic
1971 scr->clip_icon = dock->icon_array[0];
1973 wAppIconDestroy(old_top);
1976 finish:
1977 WMReleasePropList(dock_state);
1979 return dock;
1982 void wDockLaunchWithState(WDock *dock, WAppIcon *btn, WSavedState *state)
1984 if (btn && btn->command && !btn->running && !btn->launching) {
1985 btn->drop_launch = 0;
1986 btn->paste_launch = 0;
1988 btn->pid = execCommand(btn, btn->command, state);
1990 if (btn->pid > 0) {
1991 if (!btn->forced_dock && !btn->buggy_app) {
1992 btn->launching = 1;
1993 dockIconPaint(btn);
1996 } else {
1997 wfree(state);
2001 void wDockDoAutoLaunch(WDock *dock, int workspace)
2003 WAppIcon *btn;
2004 WSavedState *state;
2005 int i;
2007 for (i = 0; i < dock->max_icons; i++) {
2008 btn = dock->icon_array[i];
2009 if (!btn || !btn->auto_launch)
2010 continue;
2012 state = wmalloc(sizeof(WSavedState));
2013 state->workspace = workspace;
2014 /* TODO: this is klugy and is very difficult to understand
2015 * what's going on. Try to clean up */
2016 wDockLaunchWithState(dock, btn, state);
2020 #ifdef XDND /* was OFFIX */
2021 static WDock *findDock(WScreen *scr, XEvent *event, int *icon_pos)
2023 WDock *dock;
2024 int i;
2026 *icon_pos = -1;
2027 if ((dock = scr->dock) != NULL) {
2028 for (i = 0; i < dock->max_icons; i++) {
2029 if (dock->icon_array[i]
2030 && dock->icon_array[i]->icon->core->window == event->xclient.window) {
2031 *icon_pos = i;
2032 break;
2036 if (*icon_pos < 0 && (dock = scr->workspaces[scr->current_workspace]->clip) != NULL) {
2037 for (i = 0; i < dock->max_icons; i++) {
2038 if (dock->icon_array[i]
2039 && dock->icon_array[i]->icon->core->window == event->xclient.window) {
2040 *icon_pos = i;
2041 break;
2045 if (*icon_pos >= 0)
2046 return dock;
2047 return NULL;
2050 int wDockReceiveDNDDrop(WScreen *scr, XEvent *event)
2052 WDock *dock;
2053 WAppIcon *btn;
2054 int icon_pos;
2056 dock = findDock(scr, event, &icon_pos);
2057 if (!dock)
2058 return False;
2061 * Return True if the drop was on an application icon window.
2062 * In this case, let the ClientMessage handler redirect the
2063 * message to the app.
2065 if (dock->icon_array[icon_pos]->icon->icon_win != None)
2066 return True;
2068 if (dock->icon_array[icon_pos]->dnd_command != NULL) {
2069 scr->flags.dnd_data_convertion_status = 0;
2071 btn = dock->icon_array[icon_pos];
2073 if (!btn->forced_dock) {
2074 btn->relaunching = btn->running;
2075 btn->running = 1;
2077 if (btn->wm_instance || btn->wm_class) {
2078 WWindowAttributes attr;
2079 memset(&attr, 0, sizeof(WWindowAttributes));
2080 wDefaultFillAttributes(btn->wm_instance, btn->wm_class, &attr, NULL, True);
2082 if (!attr.no_appicon)
2083 btn->launching = 1;
2084 else
2085 btn->running = 0;
2088 btn->paste_launch = 0;
2089 btn->drop_launch = 1;
2090 scr->last_dock = dock;
2091 btn->pid = execCommand(btn, btn->dnd_command, NULL);
2092 if (btn->pid > 0) {
2093 dockIconPaint(btn);
2094 } else {
2095 btn->launching = 0;
2096 if (!btn->relaunching)
2097 btn->running = 0;
2100 return False;
2102 #endif /* XDND */
2104 Bool wDockAttachIcon(WDock *dock, WAppIcon *icon, int x, int y, Bool update_icon)
2106 WWindow *wwin;
2107 Bool lupdate_icon = False;
2108 char *command = NULL;
2109 int index;
2111 icon->editing = 0;
2113 if (update_icon)
2114 lupdate_icon = True;
2116 if (icon->command == NULL) {
2117 /* If icon->owner exists, it means the application is running */
2118 if (icon->icon->owner) {
2119 wwin = icon->icon->owner;
2120 command = GetCommandForWindow(wwin->client_win);
2123 if (command) {
2124 icon->command = command;
2125 } else {
2126 /* icon->forced_dock = 1; */
2127 if (dock->type != WM_CLIP || !icon->attracted) {
2128 icon->editing = 1;
2129 if (wInputDialog(dock->screen_ptr, _("Dock Icon"),
2130 _("Type the command used to launch the application"), &command)) {
2131 if (command && (command[0] == 0 || (command[0] == '-' && command[1] == 0))) {
2132 wfree(command);
2133 command = NULL;
2135 icon->command = command;
2136 icon->editing = 0;
2137 } else {
2138 icon->editing = 0;
2139 if (command)
2140 wfree(command);
2141 /* If the target is the dock, reject the icon. If
2142 * the target is the clip, make it an attracted icon
2144 if (dock->type == WM_CLIP) {
2145 icon->attracted = 1;
2146 if (!icon->icon->shadowed) {
2147 icon->icon->shadowed = 1;
2148 lupdate_icon = True;
2150 } else {
2151 return False;
2158 for (index = 1; index < dock->max_icons; index++)
2159 if (dock->icon_array[index] == NULL)
2160 break;
2161 /* if (index == dock->max_icons)
2162 return; */
2164 assert(index < dock->max_icons);
2166 dock->icon_array[index] = icon;
2167 icon->yindex = y;
2168 icon->xindex = x;
2170 icon->omnipresent = 0;
2172 icon->x_pos = dock->x_pos + x * ICON_SIZE;
2173 icon->y_pos = dock->y_pos + y * ICON_SIZE;
2175 dock->icon_count++;
2177 icon->running = 1;
2178 icon->launching = 0;
2179 icon->docked = 1;
2180 icon->dock = dock;
2181 icon->icon->core->descriptor.handle_mousedown = iconMouseDown;
2182 icon->icon->core->descriptor.handle_enternotify = clipEnterNotify;
2183 icon->icon->core->descriptor.handle_leavenotify = clipLeaveNotify;
2184 icon->icon->core->descriptor.parent_type = WCLASS_DOCK_ICON;
2185 icon->icon->core->descriptor.parent = icon;
2187 MoveInStackListUnder(dock->icon_array[index - 1]->icon->core, icon->icon->core);
2188 wAppIconMove(icon, icon->x_pos, icon->y_pos);
2191 * Update icon pixmap, RImage doesn't change,
2192 * so call wIconUpdate is not needed
2194 if (lupdate_icon)
2195 update_icon_pixmap(icon->icon);
2197 /* Paint it */
2198 wAppIconPaint(icon);
2200 /* Save it */
2201 save_appicon(icon, True);
2203 if (wPreferences.auto_arrange_icons)
2204 wArrangeIcons(dock->screen_ptr, True);
2206 #ifdef XDND /* was OFFIX */
2207 if (icon->command && !icon->dnd_command) {
2208 int len = strlen(icon->command) + 8;
2209 icon->dnd_command = wmalloc(len);
2210 snprintf(icon->dnd_command, len, "%s %%d", icon->command);
2212 #endif
2214 if (icon->command && !icon->paste_command) {
2215 int len = strlen(icon->command) + 8;
2216 icon->paste_command = wmalloc(len);
2217 snprintf(icon->paste_command, len, "%s %%s", icon->command);
2220 return True;
2223 void wDockReattachIcon(WDock *dock, WAppIcon *icon, int x, int y)
2225 int index;
2227 for (index = 1; index < dock->max_icons; index++) {
2228 if (dock->icon_array[index] == icon)
2229 break;
2231 assert(index < dock->max_icons);
2233 icon->yindex = y;
2234 icon->xindex = x;
2236 icon->x_pos = dock->x_pos + x * ICON_SIZE;
2237 icon->y_pos = dock->y_pos + y * ICON_SIZE;
2240 Bool wDockMoveIconBetweenDocks(WDock *src, WDock *dest, WAppIcon *icon, int x, int y)
2242 WWindow *wwin;
2243 char *command = NULL;
2244 int index;
2245 Bool update_icon = False;
2247 if (src == dest)
2248 return True; /* No move needed, we're already there */
2250 if (dest == NULL)
2251 return False;
2254 * For the moment we can't do this if we move icons in Clip from one
2255 * workspace to other, because if we move two or more icons without
2256 * command, the dialog box will not be able to tell us to which of the
2257 * moved icons it applies. -Dan
2259 if ((dest->type == WM_DOCK /*|| dest->keep_attracted */ ) && icon->command == NULL) {
2260 /* If icon->owner exists, it means the application is running */
2261 if (icon->icon->owner) {
2262 wwin = icon->icon->owner;
2263 command = GetCommandForWindow(wwin->client_win);
2266 if (command) {
2267 icon->command = command;
2268 } else {
2269 icon->editing = 1;
2270 /* icon->forced_dock = 1; */
2271 if (wInputDialog(src->screen_ptr, _("Dock Icon"),
2272 _("Type the command used to launch the application"), &command)) {
2273 if (command && (command[0] == 0 || (command[0] == '-' && command[1] == 0))) {
2274 wfree(command);
2275 command = NULL;
2277 icon->command = command;
2278 } else {
2279 icon->editing = 0;
2280 if (command)
2281 wfree(command);
2282 return False;
2284 icon->editing = 0;
2288 if (dest->type == WM_DOCK || dest->type == WM_DRAWER)
2289 wClipMakeIconOmnipresent(icon, False);
2291 for (index = 1; index < src->max_icons; index++) {
2292 if (src->icon_array[index] == icon)
2293 break;
2295 assert(index < src->max_icons);
2297 src->icon_array[index] = NULL;
2298 src->icon_count--;
2300 for (index = 1; index < dest->max_icons; index++) {
2301 if (dest->icon_array[index] == NULL)
2302 break;
2305 assert(index < dest->max_icons);
2307 dest->icon_array[index] = icon;
2308 icon->dock = dest;
2310 /* deselect the icon */
2311 if (icon->icon->selected)
2312 wIconSelect(icon->icon);
2314 icon->icon->core->descriptor.handle_enternotify = clipEnterNotify;
2315 icon->icon->core->descriptor.handle_leavenotify = clipLeaveNotify;
2317 /* set it to be kept when moving to dock.
2318 * Unless the icon does not have a command set
2320 if (icon->command && (dest->type == WM_DOCK || dest->type == WM_DRAWER)) {
2321 icon->attracted = 0;
2322 if (icon->icon->shadowed) {
2323 icon->icon->shadowed = 0;
2324 update_icon = True;
2326 save_appicon(icon, True);
2329 if (src->auto_collapse || src->auto_raise_lower)
2330 clipLeave(src);
2332 icon->yindex = y;
2333 icon->xindex = x;
2335 icon->x_pos = dest->x_pos + x * ICON_SIZE;
2336 icon->y_pos = dest->y_pos + y * ICON_SIZE;
2338 dest->icon_count++;
2340 MoveInStackListUnder(dest->icon_array[index - 1]->icon->core, icon->icon->core);
2343 * Update icon pixmap, RImage doesn't change,
2344 * so call wIconUpdate is not needed
2346 if (update_icon)
2347 update_icon_pixmap(icon->icon);
2349 /* Paint it */
2350 wAppIconPaint(icon);
2352 return True;
2355 void wDockDetach(WDock *dock, WAppIcon *icon)
2357 int index;
2358 Bool update_icon = False;
2360 /* make the settings panel be closed */
2361 if (icon->panel)
2362 DestroyDockAppSettingsPanel(icon->panel);
2364 /* This must be called before icon->dock is set to NULL.
2365 * Don't move it. -Dan
2367 wClipMakeIconOmnipresent(icon, False);
2369 icon->docked = 0;
2370 icon->dock = NULL;
2371 icon->attracted = 0;
2372 icon->auto_launch = 0;
2373 if (icon->icon->shadowed) {
2374 icon->icon->shadowed = 0;
2375 update_icon = True;
2378 /* deselect the icon */
2379 if (icon->icon->selected)
2380 wIconSelect(icon->icon);
2382 if (icon->command) {
2383 wfree(icon->command);
2384 icon->command = NULL;
2386 #ifdef XDND /* was OFFIX */
2387 if (icon->dnd_command) {
2388 wfree(icon->dnd_command);
2389 icon->dnd_command = NULL;
2391 #endif
2392 if (icon->paste_command) {
2393 wfree(icon->paste_command);
2394 icon->paste_command = NULL;
2397 for (index = 1; index < dock->max_icons; index++)
2398 if (dock->icon_array[index] == icon)
2399 break;
2401 assert(index < dock->max_icons);
2402 dock->icon_array[index] = NULL;
2403 icon->yindex = -1;
2404 icon->xindex = -1;
2406 dock->icon_count--;
2408 /* if the dock is not attached to an application or
2409 * the application did not set the appropriate hints yet,
2410 * destroy the icon */
2411 if (!icon->running || !wApplicationOf(icon->main_window)) {
2412 wAppIconDestroy(icon);
2413 } else {
2414 icon->icon->core->descriptor.handle_mousedown = appIconMouseDown;
2415 icon->icon->core->descriptor.handle_enternotify = NULL;
2416 icon->icon->core->descriptor.handle_leavenotify = NULL;
2417 icon->icon->core->descriptor.parent_type = WCLASS_APPICON;
2418 icon->icon->core->descriptor.parent = icon;
2420 ChangeStackingLevel(icon->icon->core, NORMAL_ICON_LEVEL);
2423 * Update icon pixmap, RImage doesn't change,
2424 * so call wIconUpdate is not needed
2426 if (update_icon)
2427 update_icon_pixmap(icon->icon);
2429 /* Paint it */
2430 wAppIconPaint(icon);
2432 if (wPreferences.auto_arrange_icons)
2433 wArrangeIcons(dock->screen_ptr, True);
2435 if (dock->auto_collapse || dock->auto_raise_lower)
2436 clipLeave(dock);
2440 * returns the closest Dock slot index for the passed
2441 * coordinates.
2443 * Returns False if icon can't be docked.
2445 * Note: this function should NEVER alter ret_x or ret_y, unless it will
2446 * return True. -Dan
2448 /* Redocking == true means either icon->dock == dock (normal case)
2449 * or we are called from handleDockMove for a drawer */
2450 Bool wDockSnapIcon(WDock *dock, WAppIcon *icon, int req_x, int req_y, int *ret_x, int *ret_y, int redocking)
2452 WScreen *scr = dock->screen_ptr;
2453 int dx, dy;
2454 int ex_x, ex_y;
2455 int i, offset = ICON_SIZE / 2;
2456 WAppIcon *aicon = NULL;
2457 WAppIcon *nicon = NULL;
2459 if (wPreferences.flags.noupdates)
2460 return False;
2462 dx = dock->x_pos;
2463 dy = dock->y_pos;
2465 /* if the dock is full */
2466 if (!redocking && (dock->icon_count >= dock->max_icons))
2467 return False;
2469 /* exact position */
2470 if (req_y < dy)
2471 ex_y = (req_y - offset - dy) / ICON_SIZE;
2472 else
2473 ex_y = (req_y + offset - dy) / ICON_SIZE;
2475 if (req_x < dx)
2476 ex_x = (req_x - offset - dx) / ICON_SIZE;
2477 else
2478 ex_x = (req_x + offset - dx) / ICON_SIZE;
2480 /* check if the icon is outside the screen boundaries */
2481 if (!onScreen(scr, dx + ex_x * ICON_SIZE, dy + ex_y * ICON_SIZE))
2482 return False;
2484 switch (dock->type) {
2485 case WM_DOCK:
2486 /* We can return False right away if
2487 * - we do not come from this dock (which is a WM_DOCK),
2488 * - we are not right over it, and
2489 * - we are not the main tile of a drawer.
2490 * In the latter case, we are called from handleDockMove. */
2491 if (icon->dock != dock && ex_x != 0 &&
2492 !(icon->dock && icon->dock->type == WM_DRAWER && icon == icon->dock->icon_array[0]))
2493 return False;
2495 if (!redocking && ex_x != 0)
2496 return False;
2498 if (getDrawer(scr, ex_y)) /* Return false so that the drawer gets it. */
2499 return False;
2501 aicon = NULL;
2502 for (i = 0; i < dock->max_icons; i++) {
2503 nicon = dock->icon_array[i];
2504 if (nicon && nicon->yindex == ex_y) {
2505 aicon = nicon;
2506 break;
2510 if (redocking) {
2511 int sig, done, closest;
2513 /* Possible cases when redocking:
2515 * icon dragged out of range of any slot -> false
2516 * icon dragged on a drawer -> false (to open the drawer)
2517 * icon dragged to range of free slot
2518 * icon dragged to range of same slot
2519 * icon dragged to range of different icon
2521 if (abs(ex_x) > DOCK_DETTACH_THRESHOLD)
2522 return False;
2524 if (aicon == icon || !aicon) {
2525 *ret_x = 0;
2526 *ret_y = ex_y;
2527 return True;
2530 /* start looking at the upper slot or lower? */
2531 if (ex_y * ICON_SIZE < (req_y + offset - dy))
2532 sig = 1;
2533 else
2534 sig = -1;
2536 done = 0;
2537 /* look for closest free slot */
2538 for (i = 0; i < (DOCK_DETTACH_THRESHOLD + 1) * 2 && !done; i++) {
2539 int j;
2541 done = 1;
2542 closest = sig * (i / 2) + ex_y;
2543 /* check if this slot is fully on the screen and not used */
2544 if (onScreen(scr, dx, dy + closest * ICON_SIZE)) {
2545 for (j = 0; j < dock->max_icons; j++) {
2546 if (dock->icon_array[j]
2547 && dock->icon_array[j]->yindex == closest) {
2548 /* slot is used by someone else */
2549 if (dock->icon_array[j] != icon)
2550 done = 0;
2551 break;
2554 /* slot is used by a drawer */
2555 done = done && !getDrawer(scr, closest);
2557 else // !onScreen
2558 done = 0;
2559 sig = -sig;
2561 if (done &&
2562 ((ex_y >= closest && ex_y - closest < DOCK_DETTACH_THRESHOLD + 1)
2563 || (ex_y < closest && closest - ex_y <= DOCK_DETTACH_THRESHOLD + 1))) {
2564 *ret_x = 0;
2565 *ret_y = closest;
2566 return True;
2568 } else { /* !redocking */
2570 /* if slot is free and the icon is close enough, return it */
2571 if (!aicon && ex_x == 0) {
2572 *ret_x = 0;
2573 *ret_y = ex_y;
2574 return True;
2577 break;
2578 case WM_CLIP:
2580 int neighbours = 0;
2581 int start, stop, k;
2583 start = icon->omnipresent ? 0 : scr->current_workspace;
2584 stop = icon->omnipresent ? scr->workspace_count : start + 1;
2586 aicon = NULL;
2587 for (k = start; k < stop; k++) {
2588 WDock *tmp = scr->workspaces[k]->clip;
2589 if (!tmp)
2590 continue;
2591 for (i = 0; i < tmp->max_icons; i++) {
2592 nicon = tmp->icon_array[i];
2593 if (nicon && nicon->xindex == ex_x && nicon->yindex == ex_y) {
2594 aicon = nicon;
2595 break;
2598 if (aicon)
2599 break;
2601 for (k = start; k < stop; k++) {
2602 WDock *tmp = scr->workspaces[k]->clip;
2603 if (!tmp)
2604 continue;
2605 for (i = 0; i < tmp->max_icons; i++) {
2606 nicon = tmp->icon_array[i];
2607 if (nicon && nicon != icon && /* Icon can't be it's own neighbour */
2608 (abs(nicon->xindex - ex_x) <= CLIP_ATTACH_VICINITY &&
2609 abs(nicon->yindex - ex_y) <= CLIP_ATTACH_VICINITY)) {
2610 neighbours = 1;
2611 break;
2614 if (neighbours)
2615 break;
2618 if (neighbours && (aicon == NULL || (redocking && aicon == icon))) {
2619 *ret_x = ex_x;
2620 *ret_y = ex_y;
2621 return True;
2623 break;
2625 case WM_DRAWER:
2627 WAppIcon *aicons_to_shift[ dock->icon_count ];
2628 int index_of_hole, j;
2630 if (ex_y != 0 ||
2631 abs(ex_x) - dock->icon_count > DOCK_DETTACH_THRESHOLD ||
2632 (ex_x < 0 && !dock->on_right_side) ||
2633 (ex_x > 0 && dock->on_right_side)) {
2634 return False;
2637 if (ex_x == 0)
2638 ex_x = (dock->on_right_side ? -1 : 1);
2640 /* "Reduce" ex_x but keep its sign */
2641 if (redocking) {
2642 if (abs(ex_x) > dock->icon_count - 1) /* minus 1: do not take icon_array[0] into account */
2643 ex_x = ex_x * (dock->icon_count - 1) / abs(ex_x); /* don't use *= ! */
2644 } else {
2645 if (abs(ex_x) > dock->icon_count)
2646 ex_x = ex_x * dock->icon_count / abs(ex_x);
2648 index_of_hole = indexOfHole(dock, icon, redocking);
2650 /* Find the appicons between where icon was (index_of_hole) and where
2651 * it wants to be (ex_x) and slide them. */
2652 j = 0;
2653 for (i = 1; i < dock->max_icons; i++) {
2654 if ((aicon = dock->icon_array[ i ]) && aicon != icon &&
2655 ((ex_x <= aicon->xindex && aicon->xindex < index_of_hole) ||
2656 (index_of_hole < aicon->xindex && aicon->xindex <= ex_x)))
2657 aicons_to_shift[ j++ ] = aicon;
2659 assert(j == abs(ex_x - index_of_hole));
2661 wSlideAppicons(aicons_to_shift, j, (index_of_hole < ex_x));
2663 *ret_x = ex_x;
2664 *ret_y = ex_y;
2665 return True;
2668 return False;
2671 static int onScreen(WScreen *scr, int x, int y)
2673 WMRect rect;
2674 int flags;
2676 rect.pos.x = x;
2677 rect.pos.y = y;
2678 rect.size.width = rect.size.height = ICON_SIZE;
2680 wGetRectPlacementInfo(scr, rect, &flags);
2682 return !(flags & (XFLAG_DEAD | XFLAG_PARTIAL));
2686 * returns true if it can find a free slot in the dock,
2687 * in which case it changes x_pos and y_pos accordingly.
2688 * Else returns false.
2690 Bool wDockFindFreeSlot(WDock *dock, int *x_pos, int *y_pos)
2692 WScreen *scr = dock->screen_ptr;
2693 WAppIcon *btn;
2694 WAppIconChain *chain;
2695 unsigned char *slot_map;
2696 int mwidth;
2697 int r;
2698 int x, y;
2699 int i, done = False;
2700 int corner;
2701 int sx = 0, ex = scr->scr_width, ey = scr->scr_height;
2702 int extra_count = 0;
2704 if (dock->type == WM_DRAWER) {
2705 if (dock->icon_count >= dock->max_icons) { /* drawer is full */
2706 return False;
2708 *x_pos = dock->icon_count * (dock->on_right_side ? -1 : 1);
2709 *y_pos = 0;
2710 return True;
2713 if (dock->type == WM_CLIP && dock != scr->workspaces[scr->current_workspace]->clip)
2714 extra_count = scr->global_icon_count;
2716 /* if the dock is full */
2717 if (dock->icon_count + extra_count >= dock->max_icons)
2718 return False;
2720 if (!wPreferences.flags.nodock && scr->dock) {
2721 if (scr->dock->on_right_side)
2722 ex -= ICON_SIZE + DOCK_EXTRA_SPACE;
2723 else
2724 sx += ICON_SIZE + DOCK_EXTRA_SPACE;
2727 if (ex < dock->x_pos)
2728 ex = dock->x_pos;
2729 if (sx > dock->x_pos + ICON_SIZE)
2730 sx = dock->x_pos + ICON_SIZE;
2731 #define C_NONE 0
2732 #define C_NW 1
2733 #define C_NE 2
2734 #define C_SW 3
2735 #define C_SE 4
2737 /* check if clip is in a corner */
2738 if (dock->type == WM_CLIP) {
2739 if (dock->x_pos < 1 && dock->y_pos < 1)
2740 corner = C_NE;
2741 else if (dock->x_pos < 1 && dock->y_pos >= (ey - ICON_SIZE))
2742 corner = C_SE;
2743 else if (dock->x_pos >= (ex - ICON_SIZE) && dock->y_pos >= (ey - ICON_SIZE))
2744 corner = C_SW;
2745 else if (dock->x_pos >= (ex - ICON_SIZE) && dock->y_pos < 1)
2746 corner = C_NW;
2747 else
2748 corner = C_NONE;
2749 } else {
2750 corner = C_NONE;
2753 /* If the clip is in the corner, use only slots that are in the border
2754 * of the screen */
2755 if (corner != C_NONE) {
2756 char *hmap, *vmap;
2757 int hcount, vcount;
2759 hcount = WMIN(dock->max_icons, scr->scr_width / ICON_SIZE);
2760 vcount = WMIN(dock->max_icons, scr->scr_height / ICON_SIZE);
2761 hmap = wmalloc(hcount + 1);
2762 vmap = wmalloc(vcount + 1);
2764 /* mark used positions */
2765 switch (corner) {
2766 case C_NE:
2767 for (i = 0; i < dock->max_icons; i++) {
2768 btn = dock->icon_array[i];
2769 if (!btn)
2770 continue;
2772 if (btn->xindex == 0 && btn->yindex > 0 && btn->yindex < vcount)
2773 vmap[btn->yindex] = 1;
2774 else if (btn->yindex == 0 && btn->xindex > 0 && btn->xindex < hcount)
2775 hmap[btn->xindex] = 1;
2777 for (chain = scr->global_icons; chain != NULL; chain = chain->next) {
2778 btn = chain->aicon;
2779 if (btn->xindex == 0 && btn->yindex > 0 && btn->yindex < vcount)
2780 vmap[btn->yindex] = 1;
2781 else if (btn->yindex == 0 && btn->xindex > 0 && btn->xindex < hcount)
2782 hmap[btn->xindex] = 1;
2784 break;
2785 case C_NW:
2786 for (i = 0; i < dock->max_icons; i++) {
2787 btn = dock->icon_array[i];
2788 if (!btn)
2789 continue;
2791 if (btn->xindex == 0 && btn->yindex > 0 && btn->yindex < vcount)
2792 vmap[btn->yindex] = 1;
2793 else if (btn->yindex == 0 && btn->xindex < 0 && btn->xindex > -hcount)
2794 hmap[-btn->xindex] = 1;
2796 for (chain = scr->global_icons; chain != NULL; chain = chain->next) {
2797 btn = chain->aicon;
2798 if (btn->xindex == 0 && btn->yindex > 0 && btn->yindex < vcount)
2799 vmap[btn->yindex] = 1;
2800 else if (btn->yindex == 0 && btn->xindex < 0 && btn->xindex > -hcount)
2801 hmap[-btn->xindex] = 1;
2803 break;
2804 case C_SE:
2805 for (i = 0; i < dock->max_icons; i++) {
2806 btn = dock->icon_array[i];
2807 if (!btn)
2808 continue;
2810 if (btn->xindex == 0 && btn->yindex < 0 && btn->yindex > -vcount)
2811 vmap[-btn->yindex] = 1;
2812 else if (btn->yindex == 0 && btn->xindex > 0 && btn->xindex < hcount)
2813 hmap[btn->xindex] = 1;
2815 for (chain = scr->global_icons; chain != NULL; chain = chain->next) {
2816 btn = chain->aicon;
2817 if (btn->xindex == 0 && btn->yindex < 0 && btn->yindex > -vcount)
2818 vmap[-btn->yindex] = 1;
2819 else if (btn->yindex == 0 && btn->xindex > 0 && btn->xindex < hcount)
2820 hmap[btn->xindex] = 1;
2822 break;
2823 case C_SW:
2824 default:
2825 for (i = 0; i < dock->max_icons; i++) {
2826 btn = dock->icon_array[i];
2827 if (!btn)
2828 continue;
2830 if (btn->xindex == 0 && btn->yindex < 0 && btn->yindex > -vcount)
2831 vmap[-btn->yindex] = 1;
2832 else if (btn->yindex == 0 && btn->xindex < 0 && btn->xindex > -hcount)
2833 hmap[-btn->xindex] = 1;
2835 for (chain = scr->global_icons; chain != NULL; chain = chain->next) {
2836 btn = chain->aicon;
2837 if (btn->xindex == 0 && btn->yindex < 0 && btn->yindex > -vcount)
2838 vmap[-btn->yindex] = 1;
2839 else if (btn->yindex == 0 && btn->xindex < 0 && btn->xindex > -hcount)
2840 hmap[-btn->xindex] = 1;
2843 x = 0;
2844 y = 0;
2845 done = 0;
2846 /* search a vacant slot */
2847 for (i = 1; i < WMAX(vcount, hcount); i++) {
2848 if (i < vcount && vmap[i] == 0) {
2849 /* found a slot */
2850 x = 0;
2851 y = i;
2852 done = 1;
2853 break;
2854 } else if (i < hcount && hmap[i] == 0) {
2855 /* found a slot */
2856 x = i;
2857 y = 0;
2858 done = 1;
2859 break;
2862 wfree(vmap);
2863 wfree(hmap);
2864 /* If found a slot, translate and return */
2865 if (done) {
2866 if (corner == C_NW || corner == C_NE)
2867 *y_pos = y;
2868 else
2869 *y_pos = -y;
2871 if (corner == C_NE || corner == C_SE)
2872 *x_pos = x;
2873 else
2874 *x_pos = -x;
2876 return True;
2878 /* else, try to find a slot somewhere else */
2881 /* a map of mwidth x mwidth would be enough if we allowed icons to be
2882 * placed outside of screen */
2883 mwidth = (int)ceil(sqrt(dock->max_icons));
2885 /* In the worst case (the clip is in the corner of the screen),
2886 * the amount of icons that fit in the clip is smaller.
2887 * Double the map to get a safe value.
2889 mwidth += mwidth;
2891 r = (mwidth - 1) / 2;
2893 slot_map = wmalloc(mwidth * mwidth);
2895 #define XY2OFS(x,y) (WMAX(abs(x),abs(y)) > r) ? 0 : (((y)+r)*(mwidth)+(x)+r)
2897 /* mark used slots in the map. If the slot falls outside the map
2898 * (for example, when all icons are placed in line), ignore them. */
2899 for (i = 0; i < dock->max_icons; i++) {
2900 btn = dock->icon_array[i];
2901 if (btn)
2902 slot_map[XY2OFS(btn->xindex, btn->yindex)] = 1;
2905 for (chain = scr->global_icons; chain != NULL; chain = chain->next)
2906 slot_map[XY2OFS(chain->aicon->xindex, chain->aicon->yindex)] = 1;
2908 /* Find closest slot from the center that is free by scanning the
2909 * map from the center to outward in circular passes.
2910 * This will not result in a neat layout, but will be optimal
2911 * in the sense that there will not be holes left.
2913 done = 0;
2914 for (i = 1; i <= r && !done; i++) {
2915 int tx, ty;
2917 /* top and bottom parts of the ring */
2918 for (x = -i; x <= i && !done; x++) {
2919 tx = dock->x_pos + x * ICON_SIZE;
2920 y = -i;
2921 ty = dock->y_pos + y * ICON_SIZE;
2922 if (slot_map[XY2OFS(x, y)] == 0 && onScreen(scr, tx, ty)) {
2923 *x_pos = x;
2924 *y_pos = y;
2925 done = 1;
2926 break;
2928 y = i;
2929 ty = dock->y_pos + y * ICON_SIZE;
2930 if (slot_map[XY2OFS(x, y)] == 0 && onScreen(scr, tx, ty)) {
2931 *x_pos = x;
2932 *y_pos = y;
2933 done = 1;
2934 break;
2937 /* left and right parts of the ring */
2938 for (y = -i + 1; y <= i - 1; y++) {
2939 ty = dock->y_pos + y * ICON_SIZE;
2940 x = -i;
2941 tx = dock->x_pos + x * ICON_SIZE;
2942 if (slot_map[XY2OFS(x, y)] == 0 && onScreen(scr, tx, ty)) {
2943 *x_pos = x;
2944 *y_pos = y;
2945 done = 1;
2946 break;
2948 x = i;
2949 tx = dock->x_pos + x * ICON_SIZE;
2950 if (slot_map[XY2OFS(x, y)] == 0 && onScreen(scr, tx, ty)) {
2951 *x_pos = x;
2952 *y_pos = y;
2953 done = 1;
2954 break;
2958 wfree(slot_map);
2959 #undef XY2OFS
2960 return done;
2963 static void moveDock(WDock *dock, int new_x, int new_y)
2965 WAppIcon *btn;
2966 WDrawerChain *dc;
2967 int i;
2969 if (dock->type == WM_DOCK) {
2970 for (dc = dock->screen_ptr->drawers; dc != NULL; dc = dc->next)
2971 moveDock(dc->adrawer, new_x, dc->adrawer->y_pos - dock->y_pos + new_y);
2974 dock->x_pos = new_x;
2975 dock->y_pos = new_y;
2976 for (i = 0; i < dock->max_icons; i++) {
2977 btn = dock->icon_array[i];
2978 if (btn) {
2979 btn->x_pos = new_x + btn->xindex * ICON_SIZE;
2980 btn->y_pos = new_y + btn->yindex * ICON_SIZE;
2981 XMoveWindow(dpy, btn->icon->core->window, btn->x_pos, btn->y_pos);
2986 static void swapDock(WDock *dock)
2988 WScreen *scr = dock->screen_ptr;
2989 WAppIcon *btn;
2990 int x, i;
2992 if (dock->on_right_side)
2993 x = dock->x_pos = scr->scr_width - ICON_SIZE - DOCK_EXTRA_SPACE;
2994 else
2995 x = dock->x_pos = DOCK_EXTRA_SPACE;
2997 swapDrawers(scr, dock->on_right_side, x);
2999 for (i = 0; i < dock->max_icons; i++) {
3000 btn = dock->icon_array[i];
3001 if (btn) {
3002 btn->x_pos = x;
3003 XMoveWindow(dpy, btn->icon->core->window, btn->x_pos, btn->y_pos);
3007 wScreenUpdateUsableArea(scr);
3010 static pid_t execCommand(WAppIcon *btn, const char *command, WSavedState *state)
3012 WScreen *scr = btn->icon->core->screen_ptr;
3013 pid_t pid;
3014 char **argv;
3015 int argc;
3016 char *cmdline;
3018 cmdline = ExpandOptions(scr, command);
3020 if (scr->flags.dnd_data_convertion_status || !cmdline) {
3021 if (cmdline)
3022 wfree(cmdline);
3023 if (state)
3024 wfree(state);
3025 return 0;
3028 wtokensplit(cmdline, &argv, &argc);
3030 if (!argc) {
3031 if (cmdline)
3032 wfree(cmdline);
3033 if (state)
3034 wfree(state);
3035 return 0;
3038 if ((pid = fork()) == 0) {
3039 char **args;
3040 int i;
3042 SetupEnvironment(scr);
3044 #ifdef HAVE_SETSID
3045 setsid();
3046 #endif
3048 args = malloc(sizeof(char *) * (argc + 1));
3049 if (!args)
3050 exit(111);
3052 for (i = 0; i < argc; i++)
3053 args[i] = argv[i];
3055 args[argc] = NULL;
3056 execvp(argv[0], args);
3057 exit(111);
3059 wtokenfree(argv, argc);
3061 if (pid > 0) {
3062 if (!state) {
3063 state = wmalloc(sizeof(WSavedState));
3064 state->hidden = -1;
3065 state->miniaturized = -1;
3066 state->shaded = -1;
3067 if (btn->dock == scr->dock || btn->dock->type == WM_DRAWER || btn->omnipresent)
3068 state->workspace = -1;
3069 else
3070 state->workspace = scr->current_workspace;
3072 wWindowAddSavedState(btn->wm_instance, btn->wm_class, cmdline, pid, state);
3073 wAddDeathHandler(pid, (WDeathHandler *) trackDeadProcess, btn->dock);
3074 } else if (state) {
3075 wfree(state);
3077 wfree(cmdline);
3078 return pid;
3081 void wDockHideIcons(WDock *dock)
3083 int i;
3085 if (dock == NULL)
3086 return;
3088 for (i = 1; i < dock->max_icons; i++) {
3089 if (dock->icon_array[i])
3090 XUnmapWindow(dpy, dock->icon_array[i]->icon->core->window);
3092 dock->mapped = 0;
3094 dockIconPaint(dock->icon_array[0]);
3097 void wDockShowIcons(WDock *dock)
3099 int i;
3100 WAppIcon *btn;
3102 if (dock == NULL)
3103 return;
3105 btn = dock->icon_array[0];
3106 moveDock(dock, btn->x_pos, btn->y_pos);
3108 /* Deleting any change in stacking level, this function is now only about
3109 mapping icons */
3111 if (!dock->collapsed) {
3112 for (i = 1; i < dock->max_icons; i++) {
3113 if (dock->icon_array[i])
3114 XMapWindow(dpy, dock->icon_array[i]->icon->core->window);
3117 dock->mapped = 1;
3119 dockIconPaint(btn);
3122 void wDockLower(WDock *dock)
3124 int i;
3125 WDrawerChain *dc;
3127 if (dock->type == WM_DOCK) {
3128 for (dc = dock->screen_ptr->drawers; dc != NULL; dc = dc->next)
3129 wDockLower(dc->adrawer);
3131 for (i = 0; i < dock->max_icons; i++) {
3132 if (dock->icon_array[i])
3133 wLowerFrame(dock->icon_array[i]->icon->core);
3137 void wDockRaise(WDock *dock)
3139 int i;
3140 WDrawerChain *dc;
3142 for (i = dock->max_icons - 1; i >= 0; i--) {
3143 if (dock->icon_array[i])
3144 wRaiseFrame(dock->icon_array[i]->icon->core);
3146 if (dock->type == WM_DOCK) {
3147 for (dc = dock->screen_ptr->drawers; dc != NULL; dc = dc->next)
3148 wDockRaise(dc->adrawer);
3152 void wDockRaiseLower(WDock *dock)
3154 if (!dock->icon_array[0]->icon->core->stacking->above
3155 || (dock->icon_array[0]->icon->core->stacking->window_level
3156 != dock->icon_array[0]->icon->core->stacking->above->stacking->window_level))
3157 wDockLower(dock);
3158 else
3159 wDockRaise(dock);
3162 void wDockFinishLaunch(WDock *dock, WAppIcon *icon)
3164 icon->launching = 0;
3165 icon->relaunching = 0;
3166 dockIconPaint(icon);
3169 WAppIcon *wDockFindIconForWindow(WDock *dock, Window window)
3171 WAppIcon *icon;
3172 int i;
3174 for (i = 0; i < dock->max_icons; i++) {
3175 icon = dock->icon_array[i];
3176 if (icon && icon->main_window == window)
3177 return icon;
3179 return NULL;
3182 void wDockTrackWindowLaunch(WDock *dock, Window window)
3184 WAppIcon *icon;
3185 char *wm_class, *wm_instance;
3186 int i;
3187 Bool firstPass = True;
3188 Bool found = False;
3189 char *command = NULL;
3191 if (!PropGetWMClass(window, &wm_class, &wm_instance) || (!wm_class && !wm_instance))
3192 return;
3194 command = GetCommandForWindow(window);
3195 retry:
3196 for (i = 0; i < dock->max_icons; i++) {
3197 icon = dock->icon_array[i];
3198 if (!icon)
3199 continue;
3201 /* app is already attached to icon */
3202 if (icon->main_window == window) {
3203 found = True;
3204 break;
3207 if ((icon->wm_instance || icon->wm_class)
3208 && (icon->launching || !icon->running)) {
3210 if (icon->wm_instance && wm_instance && strcmp(icon->wm_instance, wm_instance) != 0)
3211 continue;
3213 if (icon->wm_class && wm_class && strcmp(icon->wm_class, wm_class) != 0)
3214 continue;
3216 if (firstPass && command && strcmp(icon->command, command) != 0)
3217 continue;
3219 if (!icon->relaunching) {
3220 WApplication *wapp;
3222 /* Possibly an application that was docked with dockit,
3223 * but the user did not update WMState to indicate that
3224 * it was docked by force */
3225 wapp = wApplicationOf(window);
3226 if (!wapp) {
3227 icon->forced_dock = 1;
3228 icon->running = 0;
3230 if (!icon->forced_dock)
3231 icon->main_window = window;
3233 found = True;
3234 if (!wPreferences.no_animations && !icon->launching &&
3235 !dock->screen_ptr->flags.startup && !dock->collapsed) {
3236 WAppIcon *aicon;
3237 int x0, y0;
3239 icon->launching = 1;
3240 dockIconPaint(icon);
3242 aicon = wAppIconCreateForDock(dock->screen_ptr, NULL,
3243 wm_instance, wm_class, TILE_NORMAL);
3244 /* XXX: can: aicon->icon == NULL ? */
3245 PlaceIcon(dock->screen_ptr, &x0, &y0, wGetHeadForWindow(aicon->icon->owner));
3246 wAppIconMove(aicon, x0, y0);
3247 /* Should this always be lowered? -Dan */
3248 if (dock->lowered)
3249 wLowerFrame(aicon->icon->core);
3250 XMapWindow(dpy, aicon->icon->core->window);
3251 aicon->launching = 1;
3252 wAppIconPaint(aicon);
3253 SlideWindow(aicon->icon->core->window, x0, y0, icon->x_pos, icon->y_pos);
3254 XUnmapWindow(dpy, aicon->icon->core->window);
3255 wAppIconDestroy(aicon);
3257 wDockFinishLaunch(dock, icon);
3258 break;
3262 if (firstPass && !found) {
3263 firstPass = False;
3264 goto retry;
3267 if (command)
3268 wfree(command);
3270 if (wm_class)
3271 free(wm_class);
3272 if (wm_instance)
3273 free(wm_instance);
3276 void wClipUpdateForWorkspaceChange(WScreen *scr, int workspace)
3278 if (!wPreferences.flags.noclip) {
3279 scr->clip_icon->dock = scr->workspaces[workspace]->clip;
3280 if (scr->current_workspace != workspace) {
3281 WDock *old_clip = scr->workspaces[scr->current_workspace]->clip;
3282 WAppIconChain *chain = scr->global_icons;
3284 while (chain) {
3285 wDockMoveIconBetweenDocks(chain->aicon->dock,
3286 scr->workspaces[workspace]->clip,
3287 chain->aicon, chain->aicon->xindex, chain->aicon->yindex);
3288 if (scr->workspaces[workspace]->clip->collapsed)
3289 XUnmapWindow(dpy, chain->aicon->icon->core->window);
3290 chain = chain->next;
3293 wDockHideIcons(old_clip);
3294 if (old_clip->auto_raise_lower) {
3295 if (old_clip->auto_raise_magic) {
3296 WMDeleteTimerHandler(old_clip->auto_raise_magic);
3297 old_clip->auto_raise_magic = NULL;
3299 wDockLower(old_clip);
3301 if (old_clip->auto_collapse) {
3302 if (old_clip->auto_expand_magic) {
3303 WMDeleteTimerHandler(old_clip->auto_expand_magic);
3304 old_clip->auto_expand_magic = NULL;
3306 old_clip->collapsed = 1;
3308 wDockShowIcons(scr->workspaces[workspace]->clip);
3313 static void trackDeadProcess(pid_t pid, unsigned char status, WDock *dock)
3315 WAppIcon *icon;
3316 int i;
3318 for (i = 0; i < dock->max_icons; i++) {
3319 icon = dock->icon_array[i];
3320 if (!icon)
3321 continue;
3323 if (icon->launching && icon->pid == pid) {
3324 if (!icon->relaunching) {
3325 icon->running = 0;
3326 icon->main_window = None;
3328 wDockFinishLaunch(dock, icon);
3329 icon->pid = 0;
3330 if (status == 111) {
3331 char msg[PATH_MAX];
3332 char *cmd;
3334 #ifdef XDND
3335 if (icon->drop_launch)
3336 cmd = icon->dnd_command;
3337 else
3338 #endif
3339 if (icon->paste_launch)
3340 cmd = icon->paste_command;
3341 else
3342 cmd = icon->command;
3344 snprintf(msg, sizeof(msg), _("Could not execute command \"%s\""), cmd);
3346 wMessageDialog(dock->screen_ptr, _("Error"), msg, _("OK"), NULL, NULL);
3348 break;
3353 /* This function is called when the dock switches state between
3354 * "normal" (including auto-raise/lower) and "keep on top". It is
3355 * therefore clearly distinct from wDockLower/Raise, which are called
3356 * each time a not-kept-on-top dock is lowered/raised. */
3357 static void toggleLowered(WDock *dock)
3359 WAppIcon *tmp;
3360 WDrawerChain *dc;
3361 int newlevel, i;
3363 if (!dock->lowered) {
3364 newlevel = WMNormalLevel;
3365 dock->lowered = 1;
3366 } else {
3367 newlevel = WMDockLevel;
3368 dock->lowered = 0;
3371 for (i = 0; i < dock->max_icons; i++) {
3372 tmp = dock->icon_array[i];
3373 if (!tmp)
3374 continue;
3376 ChangeStackingLevel(tmp->icon->core, newlevel);
3378 /* When the dock is no longer "on top", explicitly lower it as well.
3379 * It saves some CPU cycles (probably) to do it ourselves here
3380 * rather than calling wDockLower at the end of toggleLowered */
3381 if (dock->lowered)
3382 wLowerFrame(tmp->icon->core);
3385 if (dock->type == WM_DOCK) {
3386 for (dc = dock->screen_ptr->drawers; dc != NULL; dc = dc->next) {
3387 toggleLowered(dc->adrawer);
3389 wScreenUpdateUsableArea(dock->screen_ptr);
3393 static void toggleCollapsed(WDock *dock)
3395 if (dock->collapsed) {
3396 dock->collapsed = 0;
3397 wDockShowIcons(dock);
3398 } else {
3399 dock->collapsed = 1;
3400 wDockHideIcons(dock);
3404 static void openDockMenu(WDock *dock, WAppIcon *aicon, XEvent *event)
3406 WScreen *scr = dock->screen_ptr;
3407 WObjDescriptor *desc;
3408 WMenuEntry *entry;
3409 WApplication *wapp = NULL;
3410 int index = 0;
3411 int x_pos;
3412 int n_selected;
3413 int appIsRunning = aicon->running && aicon->icon && aicon->icon->owner;
3415 if (dock->type == WM_DOCK) {
3416 /* Dock position menu */
3417 updateDockPositionMenu(scr->dock_pos_menu, dock);
3418 dock->menu->flags.realized = 0;
3419 if (!wPreferences.flags.nodrawer) {
3420 /* add a drawer */
3421 entry = dock->menu->entries[++index];
3422 entry->clientdata = aicon;
3423 wMenuSetEnabled(dock->menu, index, True);
3425 } else {
3426 /* clip/drawer options */
3427 if (scr->clip_options)
3428 updateClipOptionsMenu(scr->clip_options, dock);
3430 n_selected = numberOfSelectedIcons(dock);
3432 if (dock->type == WM_CLIP) {
3433 /* Rename Workspace */
3434 entry = dock->menu->entries[++index];
3435 if (aicon == scr->clip_icon) {
3436 entry->callback = renameCallback;
3437 entry->clientdata = dock;
3438 entry->flags.indicator = 0;
3439 entry->text = _("Rename Workspace");
3440 } else {
3441 entry->callback = omnipresentCallback;
3442 entry->clientdata = aicon;
3443 if (n_selected > 0) {
3444 entry->flags.indicator = 0;
3445 entry->text = _("Toggle Omnipresent");
3446 } else {
3447 entry->flags.indicator = 1;
3448 entry->flags.indicator_on = aicon->omnipresent;
3449 entry->flags.indicator_type = MI_CHECK;
3450 entry->text = _("Omnipresent");
3455 /* select/unselect icon */
3456 entry = dock->menu->entries[++index];
3457 entry->clientdata = aicon;
3458 entry->flags.indicator_on = aicon->icon->selected;
3459 wMenuSetEnabled(dock->menu, index, aicon != scr->clip_icon && !wIsADrawer(scr, aicon));
3461 /* select/unselect all icons */
3462 entry = dock->menu->entries[++index];
3463 entry->clientdata = aicon;
3464 if (n_selected > 0)
3465 entry->text = _("Unselect All Icons");
3466 else
3467 entry->text = _("Select All Icons");
3469 wMenuSetEnabled(dock->menu, index, dock->icon_count > 1);
3471 /* keep icon(s) */
3472 entry = dock->menu->entries[++index];
3473 entry->clientdata = aicon;
3474 if (n_selected > 1)
3475 entry->text = _("Keep Icons");
3476 else
3477 entry->text = _("Keep Icon");
3479 wMenuSetEnabled(dock->menu, index, dock->icon_count > 1);
3481 if (dock->type == WM_CLIP) {
3482 /* this is the workspace submenu part */
3483 entry = dock->menu->entries[++index];
3484 if (n_selected > 1)
3485 entry->text = _("Move Icons To");
3486 else
3487 entry->text = _("Move Icon To");
3489 if (scr->clip_submenu)
3490 updateWorkspaceMenu(scr->clip_submenu, aicon);
3492 wMenuSetEnabled(dock->menu, index, !aicon->omnipresent);
3495 /* remove icon(s) */
3496 entry = dock->menu->entries[++index];
3497 entry->clientdata = aicon;
3498 if (n_selected > 1)
3499 entry->text = _("Remove Icons");
3500 else
3501 entry->text = _("Remove Icon");
3503 wMenuSetEnabled(dock->menu, index, dock->icon_count > 1);
3505 /* attract icon(s) */
3506 entry = dock->menu->entries[++index];
3507 entry->clientdata = aicon;
3509 dock->menu->flags.realized = 0;
3510 wMenuRealize(dock->menu);
3513 if (aicon->icon->owner)
3514 wapp = wApplicationOf(aicon->icon->owner->main_window);
3515 else
3516 wapp = NULL;
3518 /* launch */
3519 entry = dock->menu->entries[++index];
3520 entry->clientdata = aicon;
3521 wMenuSetEnabled(dock->menu, index, aicon->command != NULL);
3523 /* unhide here */
3524 entry = dock->menu->entries[++index];
3525 entry->clientdata = aicon;
3526 if (wapp && wapp->flags.hidden)
3527 entry->text = _("Unhide Here");
3528 else
3529 entry->text = _("Bring Here");
3531 wMenuSetEnabled(dock->menu, index, appIsRunning);
3533 /* hide */
3534 entry = dock->menu->entries[++index];
3535 entry->clientdata = aicon;
3536 if (wapp && wapp->flags.hidden)
3537 entry->text = _("Unhide");
3538 else
3539 entry->text = _("Hide");
3541 wMenuSetEnabled(dock->menu, index, appIsRunning);
3543 /* settings */
3544 entry = dock->menu->entries[++index];
3545 entry->clientdata = aicon;
3546 wMenuSetEnabled(dock->menu, index, !aicon->editing && !wPreferences.flags.noupdates);
3548 /* kill or remove drawer */
3549 entry = dock->menu->entries[++index];
3550 entry->clientdata = aicon;
3551 if (wIsADrawer(scr, aicon)) {
3552 entry->callback = removeDrawerCallback;
3553 entry->text = _("Remove drawer");
3554 wMenuSetEnabled(dock->menu, index, True);
3555 } else {
3556 entry->callback = killCallback;
3557 entry->text = _("Kill");
3558 wMenuSetEnabled(dock->menu, index, appIsRunning);
3561 if (!dock->menu->flags.realized)
3562 wMenuRealize(dock->menu);
3564 if (dock->type == WM_CLIP || dock->type == WM_DRAWER) {
3565 /*x_pos = event->xbutton.x_root+2; */
3566 x_pos = event->xbutton.x_root - dock->menu->frame->core->width / 2 - 1;
3567 if (x_pos < 0) {
3568 x_pos = 0;
3569 } else if (x_pos + dock->menu->frame->core->width > scr->scr_width - 2) {
3570 x_pos = scr->scr_width - dock->menu->frame->core->width - 4;
3572 } else {
3573 x_pos = dock->on_right_side ? scr->scr_width - dock->menu->frame->core->width - 3 : 0;
3576 wMenuMapAt(dock->menu, x_pos, event->xbutton.y_root + 2, False);
3578 /* allow drag select */
3579 event->xany.send_event = True;
3580 desc = &dock->menu->menu->descriptor;
3581 (*desc->handle_mousedown) (desc, event);
3584 /******************************************************************/
3585 static void iconDblClick(WObjDescriptor *desc, XEvent *event)
3587 WAppIcon *btn = desc->parent;
3588 WDock *dock = btn->dock;
3589 WApplication *wapp = NULL;
3590 int unhideHere = 0;
3592 if (btn->icon->owner && !(event->xbutton.state & ControlMask)) {
3593 wapp = wApplicationOf(btn->icon->owner->main_window);
3595 assert(wapp != NULL);
3597 unhideHere = (event->xbutton.state & ShiftMask);
3599 /* go to the last workspace that the user worked on the app */
3600 if (wapp->last_workspace != dock->screen_ptr->current_workspace && !unhideHere)
3601 wWorkspaceChange(dock->screen_ptr, wapp->last_workspace);
3603 wUnhideApplication(wapp, event->xbutton.button == Button2, unhideHere);
3605 if (event->xbutton.state & MOD_MASK)
3606 wHideOtherApplications(btn->icon->owner);
3607 } else {
3608 if (event->xbutton.button == Button1) {
3609 if (event->xbutton.state & MOD_MASK) {
3610 /* raise/lower dock */
3611 toggleLowered(dock);
3612 } else if (btn == dock->screen_ptr->clip_icon) {
3613 if (getClipButton(event->xbutton.x, event->xbutton.y) != CLIP_IDLE)
3614 handleClipChangeWorkspace(dock->screen_ptr, event);
3615 else if (wPreferences.flags.clip_merged_in_dock) {
3616 // Is actually the dock
3617 if (btn->command)
3619 if (!btn->launching && (!btn->running || (event->xbutton.state & ControlMask)))
3620 launchDockedApplication(btn, False);
3622 else
3624 wShowInfoPanel(dock->screen_ptr);
3627 else
3628 toggleCollapsed(dock);
3629 } else if (wIsADrawer(dock->screen_ptr, btn)) {
3630 toggleCollapsed(dock);
3631 } else if (btn->command) {
3632 if (!btn->launching && (!btn->running || (event->xbutton.state & ControlMask)))
3633 launchDockedApplication(btn, False);
3634 } else if (btn->xindex == 0 && btn->yindex == 0 && btn->dock->type == WM_DOCK) {
3635 wShowInfoPanel(dock->screen_ptr);
3641 static void handleDockMove(WDock *dock, WAppIcon *aicon, XEvent *event)
3643 WScreen *scr = dock->screen_ptr;
3644 int ofs_x = event->xbutton.x, ofs_y = event->xbutton.y;
3645 WIcon *icon = aicon->icon;
3646 WAppIcon *tmpaicon;
3647 WDrawerChain *dc;
3648 int x = aicon->x_pos, y = aicon->y_pos;;
3649 int shad_x = x, shad_y = y;
3650 XEvent ev;
3651 int grabbed = 0, done, previously_on_right, now_on_right, previous_x_pos, i;
3652 Pixmap ghost = None;
3653 int superfluous = wPreferences.superfluous; /* we catch it to avoid problems */
3655 if (XGrabPointer(dpy, aicon->icon->core->window, True, ButtonMotionMask
3656 | ButtonReleaseMask | ButtonPressMask, GrabModeAsync,
3657 GrabModeAsync, None, None, CurrentTime) != GrabSuccess)
3658 wwarning("pointer grab failed for dock move");
3660 if (dock->type == WM_DRAWER) {
3661 Window wins[2];
3662 wins[0] = icon->core->window;
3663 wins[1] = scr->dock_shadow;
3664 XRestackWindows(dpy, wins, 2);
3665 XMoveResizeWindow(dpy, scr->dock_shadow, aicon->x_pos, aicon->y_pos,
3666 ICON_SIZE, ICON_SIZE);
3667 if (superfluous) {
3668 if (icon->pixmap!=None)
3669 ghost = MakeGhostIcon(scr, icon->pixmap);
3670 else
3671 ghost = MakeGhostIcon(scr, icon->core->window);
3673 XSetWindowBackgroundPixmap(dpy, scr->dock_shadow, ghost);
3674 XClearWindow(dpy, scr->dock_shadow);
3676 XMapWindow(dpy, scr->dock_shadow);
3679 previously_on_right = now_on_right = dock->on_right_side;
3680 previous_x_pos = dock->x_pos;
3681 done = 0;
3682 while (!done) {
3683 WMMaskEvent(dpy, PointerMotionMask | ButtonReleaseMask | ButtonPressMask
3684 | ButtonMotionMask | ExposureMask | EnterWindowMask, &ev);
3685 switch (ev.type) {
3686 case Expose:
3687 WMHandleEvent(&ev);
3688 break;
3690 case EnterNotify:
3691 /* It means the cursor moved so fast that it entered
3692 * something else (if moving slowly, it would have
3693 * stayed in the dock that is being moved. Ignore such
3694 * "spurious" EnterNotifiy's */
3695 break;
3697 case MotionNotify:
3698 if (!grabbed) {
3699 if (abs(ofs_x - ev.xmotion.x) >= MOVE_THRESHOLD
3700 || abs(ofs_y - ev.xmotion.y) >= MOVE_THRESHOLD) {
3701 XChangeActivePointerGrab(dpy, ButtonMotionMask
3702 | ButtonReleaseMask | ButtonPressMask,
3703 wCursor[WCUR_MOVE], CurrentTime);
3704 grabbed = 1;
3706 break;
3708 switch (dock->type) {
3709 case WM_CLIP:
3710 x = ev.xmotion.x_root - ofs_x;
3711 y = ev.xmotion.y_root - ofs_y;
3712 wScreenKeepInside(scr, &x, &y, ICON_SIZE, ICON_SIZE);
3713 moveDock(dock, x, y);
3714 break;
3715 case WM_DOCK:
3716 x = ev.xmotion.x_root - ofs_x;
3717 y = ev.xmotion.y_root - ofs_y;
3718 if (previously_on_right)
3720 now_on_right = (ev.xmotion.x_root >= previous_x_pos - ICON_SIZE);
3722 else
3724 now_on_right = (ev.xmotion.x_root > previous_x_pos + ICON_SIZE * 2);
3726 if (now_on_right != dock->on_right_side)
3728 dock->on_right_side = now_on_right;
3729 swapDock(dock);
3730 wArrangeIcons(scr, False);
3732 // Also perform the vertical move
3733 wScreenKeepInside(scr, &x, &y, ICON_SIZE, ICON_SIZE);
3734 moveDock(dock, dock->x_pos, y);
3735 if (wPreferences.flags.wrap_appicons_in_dock)
3737 for (i = 0; i < dock->max_icons; i++) {
3738 int new_y, new_index, j, ok;
3739 if ((tmpaicon = dock->icon_array[i]) == NULL)
3740 continue;
3741 if (onScreen(scr, tmpaicon->x_pos, tmpaicon->y_pos))
3742 continue;
3743 new_y = (tmpaicon->y_pos + ICON_SIZE * dock->max_icons) % (ICON_SIZE * dock->max_icons);
3744 new_index = (new_y - dock->y_pos) / ICON_SIZE;
3745 if (!onScreen(scr, tmpaicon->x_pos, new_y))
3746 continue;
3747 ok = 1;
3748 for (j = 0; j < dock->max_icons; j++)
3750 if (dock->icon_array[j] != NULL &&
3751 dock->icon_array[j]->yindex == new_index)
3753 ok = 0;
3754 break;
3757 if (!ok || getDrawer(scr, new_index) != NULL)
3758 continue;
3759 wDockReattachIcon(dock, tmpaicon, tmpaicon->xindex, new_index);
3761 for (dc = scr->drawers; dc != NULL; dc = dc->next)
3763 int new_y, new_index, j, ok;
3764 tmpaicon = dc->adrawer->icon_array[0];
3765 if (onScreen(scr, tmpaicon->x_pos, tmpaicon->y_pos))
3766 continue;
3767 new_y = (tmpaicon->y_pos + ICON_SIZE * dock->max_icons) % (ICON_SIZE * dock->max_icons);
3768 new_index = (new_y - dock->y_pos) / ICON_SIZE;
3769 if (!onScreen(scr, tmpaicon->x_pos, new_y))
3770 continue;
3771 ok = 1;
3772 for (j = 0; j < dock->max_icons; j++)
3774 if (dock->icon_array[j] != NULL &&
3775 dock->icon_array[j]->yindex == new_index)
3777 ok = 0;
3778 break;
3781 if (!ok || getDrawer(scr, new_index) != NULL)
3782 continue;
3783 moveDock(dc->adrawer, tmpaicon->x_pos, new_y);
3786 break;
3787 case WM_DRAWER:
3789 WDock *real_dock = dock->screen_ptr->dock;
3790 Bool snapped;
3791 int ix, iy;
3792 x = ev.xmotion.x_root - ofs_x;
3793 y = ev.xmotion.y_root - ofs_y;
3794 snapped = wDockSnapIcon(real_dock, aicon, x, y, &ix, &iy, True);
3795 if (snapped) {
3796 shad_x = real_dock->x_pos + ix * wPreferences.icon_size;
3797 shad_y = real_dock->y_pos + iy * wPreferences.icon_size;
3798 XMoveWindow(dpy, scr->dock_shadow, shad_x, shad_y);
3800 moveDock(dock, x, y);
3801 break;
3804 break;
3806 case ButtonPress:
3807 break;
3809 case ButtonRelease:
3810 if (ev.xbutton.button != event->xbutton.button)
3811 break;
3812 XUngrabPointer(dpy, CurrentTime);
3813 if (dock->type == WM_DRAWER) {
3814 Window *wins[dock->icon_count];
3816 for (i = 0; i < dock->max_icons; i++) {
3817 if ((tmpaicon = dock->icon_array[i]) == NULL)
3818 continue;
3819 wins[ tmpaicon->xindex + (dock->on_right_side ? dock->icon_count - 1 : 0) ] = &tmpaicon->icon->core->window;
3821 SlideWindows(wins, dock->icon_count,
3822 (dock->on_right_side ? x - (dock->icon_count - 1) * ICON_SIZE : x),
3824 (dock->on_right_side ? shad_x - (dock->icon_count - 1) * ICON_SIZE : shad_x),
3825 shad_y);
3826 XUnmapWindow(dpy, scr->dock_shadow);
3827 moveDock(dock, shad_x, shad_y);
3828 XResizeWindow(dpy, scr->dock_shadow, ICON_SIZE, ICON_SIZE);
3830 done = 1;
3831 break;
3834 if (superfluous) {
3835 if (ghost != None)
3836 XFreePixmap(dpy, ghost);
3837 XSetWindowBackground(dpy, scr->dock_shadow, scr->white_pixel);
3842 static int getClipButton(int px, int py)
3844 int pt = (CLIP_BUTTON_SIZE + 2) * ICON_SIZE / 64;
3846 if (px < 0 || py < 0 || px >= ICON_SIZE || py >= ICON_SIZE)
3847 return CLIP_IDLE;
3849 if (py <= pt - ((int)ICON_SIZE - 1 - px))
3850 return CLIP_FORWARD;
3851 else if (px <= pt - ((int)ICON_SIZE - 1 - py))
3852 return CLIP_REWIND;
3854 return CLIP_IDLE;
3857 static void handleClipChangeWorkspace(WScreen *scr, XEvent *event)
3859 XEvent ev;
3860 int done, direction, new_ws;
3861 int new_dir;
3862 WDock *clip = scr->clip_icon->dock;
3864 direction = getClipButton(event->xbutton.x, event->xbutton.y);
3866 clip->lclip_button_pushed = direction == CLIP_REWIND;
3867 clip->rclip_button_pushed = direction == CLIP_FORWARD;
3869 wClipIconPaint(scr->clip_icon);
3870 done = 0;
3871 while (!done) {
3872 WMMaskEvent(dpy, ExposureMask | ButtonMotionMask | ButtonReleaseMask | ButtonPressMask, &ev);
3873 switch (ev.type) {
3874 case Expose:
3875 WMHandleEvent(&ev);
3876 break;
3878 case MotionNotify:
3879 new_dir = getClipButton(ev.xmotion.x, ev.xmotion.y);
3880 if (new_dir != direction) {
3881 direction = new_dir;
3882 clip->lclip_button_pushed = direction == CLIP_REWIND;
3883 clip->rclip_button_pushed = direction == CLIP_FORWARD;
3884 wClipIconPaint(scr->clip_icon);
3886 break;
3888 case ButtonPress:
3889 break;
3891 case ButtonRelease:
3892 if (ev.xbutton.button == event->xbutton.button)
3893 done = 1;
3897 clip->lclip_button_pushed = 0;
3898 clip->rclip_button_pushed = 0;
3900 new_ws = wPreferences.ws_advance || (event->xbutton.state & ControlMask);
3902 if (direction == CLIP_FORWARD) {
3903 if (scr->current_workspace < scr->workspace_count - 1)
3904 wWorkspaceChange(scr, scr->current_workspace + 1);
3905 else if (new_ws && scr->current_workspace < MAX_WORKSPACES - 1)
3906 wWorkspaceChange(scr, scr->current_workspace + 1);
3907 else if (wPreferences.ws_cycle)
3908 wWorkspaceChange(scr, 0);
3909 } else if (direction == CLIP_REWIND) {
3910 if (scr->current_workspace > 0)
3911 wWorkspaceChange(scr, scr->current_workspace - 1);
3912 else if (scr->current_workspace == 0 && wPreferences.ws_cycle)
3913 wWorkspaceChange(scr, scr->workspace_count - 1);
3916 wClipIconPaint(scr->clip_icon);
3919 static void iconMouseDown(WObjDescriptor *desc, XEvent *event)
3921 WAppIcon *aicon = desc->parent;
3922 WDock *dock = aicon->dock;
3923 WScreen *scr = aicon->icon->core->screen_ptr;
3925 if (aicon->editing || WCHECK_STATE(WSTATE_MODAL))
3926 return;
3928 scr->last_dock = dock;
3930 if (dock->menu->flags.mapped)
3931 wMenuUnmap(dock->menu);
3933 if (IsDoubleClick(scr, event)) {
3934 /* double-click was not in the main clip icon */
3935 if (dock->type != WM_CLIP || aicon->xindex != 0 || aicon->yindex != 0
3936 || getClipButton(event->xbutton.x, event->xbutton.y) == CLIP_IDLE) {
3937 iconDblClick(desc, event);
3938 return;
3942 if (event->xbutton.button == Button1) {
3943 if (event->xbutton.state & MOD_MASK)
3944 wDockLower(dock);
3945 else
3946 wDockRaise(dock);
3948 if ((event->xbutton.state & ShiftMask) && aicon != scr->clip_icon && dock->type != WM_DOCK) {
3949 wIconSelect(aicon->icon);
3950 return;
3953 if (aicon->yindex == 0 && aicon->xindex == 0) {
3954 if (getClipButton(event->xbutton.x, event->xbutton.y) != CLIP_IDLE &&
3955 (dock->type == WM_CLIP || (dock->type == WM_DOCK && wPreferences.flags.clip_merged_in_dock)))
3956 handleClipChangeWorkspace(scr, event);
3957 else
3958 handleDockMove(dock, aicon, event);
3959 } else {
3960 Bool hasMoved = wHandleAppIconMove(aicon, event);
3961 if (wPreferences.single_click && !hasMoved)
3962 iconDblClick(desc, event);
3964 } else if (event->xbutton.button == Button2 && aicon == scr->clip_icon) {
3965 if (!scr->clip_ws_menu) {
3966 scr->clip_ws_menu = wWorkspaceMenuMake(scr, False);
3968 if (scr->clip_ws_menu) {
3969 WMenu *wsMenu = scr->clip_ws_menu;
3970 int xpos;
3972 wWorkspaceMenuUpdate(scr, wsMenu);
3974 xpos = event->xbutton.x_root - wsMenu->frame->core->width / 2 - 1;
3975 if (xpos < 0) {
3976 xpos = 0;
3977 } else if (xpos + wsMenu->frame->core->width > scr->scr_width - 2) {
3978 xpos = scr->scr_width - wsMenu->frame->core->width - 4;
3980 wMenuMapAt(wsMenu, xpos, event->xbutton.y_root + 2, False);
3982 desc = &wsMenu->menu->descriptor;
3983 event->xany.send_event = True;
3984 (*desc->handle_mousedown) (desc, event);
3986 } else if (event->xbutton.button == Button2 && dock->type == WM_CLIP &&
3987 (event->xbutton.state & ShiftMask) && aicon != scr->clip_icon) {
3988 wClipMakeIconOmnipresent(aicon, !aicon->omnipresent);
3989 } else if (event->xbutton.button == Button3) {
3990 if (event->xbutton.send_event &&
3991 XGrabPointer(dpy, aicon->icon->core->window, True, ButtonMotionMask
3992 | ButtonReleaseMask | ButtonPressMask, GrabModeAsync,
3993 GrabModeAsync, None, None, CurrentTime) != GrabSuccess) {
3994 wwarning("pointer grab failed for dockicon menu");
3995 return;
3998 openDockMenu(dock, aicon, event);
3999 } else if (event->xbutton.button == Button2) {
4000 WAppIcon *btn = desc->parent;
4002 if (!btn->launching && (!btn->running || (event->xbutton.state & ControlMask)))
4003 launchDockedApplication(btn, True);
4007 static void clipEnterNotify(WObjDescriptor *desc, XEvent *event)
4009 WAppIcon *btn = (WAppIcon *) desc->parent;
4010 WDock *dock, *tmp;
4011 WScreen *scr;
4013 assert(event->type == EnterNotify);
4015 if (desc->parent_type != WCLASS_DOCK_ICON)
4016 return;
4018 scr = btn->icon->core->screen_ptr;
4019 dock = btn->dock;
4021 if (dock == NULL)
4022 return;
4024 /* The auto raise/lower code */
4025 tmp = (dock->type == WM_DRAWER ? scr->dock : dock);
4026 if (tmp->auto_lower_magic) {
4027 WMDeleteTimerHandler(tmp->auto_lower_magic);
4028 tmp->auto_lower_magic = NULL;
4030 if (tmp->auto_raise_lower && !tmp->auto_raise_magic)
4031 tmp->auto_raise_magic = WMAddTimerHandler(wPreferences.clip_auto_raise_delay, clipAutoRaise, (void *) tmp);
4033 if (dock->type != WM_CLIP && dock->type != WM_DRAWER)
4034 return;
4036 /* The auto expand/collapse code */
4037 if (dock->auto_collapse_magic) {
4038 WMDeleteTimerHandler(dock->auto_collapse_magic);
4039 dock->auto_collapse_magic = NULL;
4041 if (dock->auto_collapse && !dock->auto_expand_magic)
4042 dock->auto_expand_magic = WMAddTimerHandler(wPreferences.clip_auto_expand_delay, clipAutoExpand, (void *)dock);
4045 static void clipLeave(WDock *dock)
4047 XEvent event;
4048 WObjDescriptor *desc = NULL;
4049 WDock *tmp;
4051 if (dock == NULL)
4052 return;
4054 if (XCheckTypedEvent(dpy, EnterNotify, &event) != False) {
4055 if (XFindContext(dpy, event.xcrossing.window, wWinContext,
4056 (XPointer *) & desc) != XCNOENT
4057 && desc && desc->parent_type == WCLASS_DOCK_ICON
4058 && ((WAppIcon *) desc->parent)->dock == dock) {
4059 /* We haven't left the dock/clip/drawer yet */
4060 XPutBackEvent(dpy, &event);
4061 return;
4064 XPutBackEvent(dpy, &event);
4065 } else {
4066 /* We entered a withdrawn window, so we're still in Clip */
4067 return;
4070 tmp = (dock->type == WM_DRAWER ? dock->screen_ptr->dock : dock);
4071 if (tmp->auto_raise_magic) {
4072 WMDeleteTimerHandler(tmp->auto_raise_magic);
4073 tmp->auto_raise_magic = NULL;
4075 if (tmp->auto_raise_lower && !tmp->auto_lower_magic)
4076 tmp->auto_lower_magic = WMAddTimerHandler(wPreferences.clip_auto_lower_delay, clipAutoLower, (void *)tmp);
4078 if (dock->type != WM_CLIP && dock->type != WM_DRAWER)
4079 return;
4081 if (dock->auto_expand_magic) {
4082 WMDeleteTimerHandler(dock->auto_expand_magic);
4083 dock->auto_expand_magic = NULL;
4085 if (dock->auto_collapse && !dock->auto_collapse_magic)
4086 dock->auto_collapse_magic = WMAddTimerHandler(wPreferences.clip_auto_collapse_delay, clipAutoCollapse, (void *)dock);
4089 static void clipLeaveNotify(WObjDescriptor *desc, XEvent *event)
4091 WAppIcon *btn = (WAppIcon *) desc->parent;
4093 assert(event->type == LeaveNotify);
4095 if (desc->parent_type != WCLASS_DOCK_ICON)
4096 return;
4098 clipLeave(btn->dock);
4101 static void clipAutoCollapse(void *cdata)
4103 WDock *dock = (WDock *) cdata;
4105 if (dock->type != WM_CLIP && dock->type != WM_DRAWER)
4106 return;
4108 if (dock->auto_collapse) {
4109 dock->collapsed = 1;
4110 wDockHideIcons(dock);
4112 dock->auto_collapse_magic = NULL;
4115 static void clipAutoExpand(void *cdata)
4117 WDock *dock = (WDock *) cdata;
4119 if (dock->type != WM_CLIP && dock->type != WM_DRAWER)
4120 return;
4122 if (dock->auto_collapse) {
4123 dock->collapsed = 0;
4124 wDockShowIcons(dock);
4126 dock->auto_expand_magic = NULL;
4129 static void clipAutoLower(void *cdata)
4131 WDock *dock = (WDock *) cdata;
4133 if (dock->auto_raise_lower)
4134 wDockLower(dock);
4136 dock->auto_lower_magic = NULL;
4139 static void clipAutoRaise(void *cdata)
4141 WDock *dock = (WDock *) cdata;
4143 if (dock->auto_raise_lower)
4144 wDockRaise(dock);
4146 dock->auto_raise_magic = NULL;
4149 static Bool iconCanBeOmnipresent(WAppIcon *aicon)
4151 WScreen *scr = aicon->icon->core->screen_ptr;
4152 WDock *clip;
4153 WAppIcon *btn;
4154 int i, j;
4156 for (i = 0; i < scr->workspace_count; i++) {
4157 clip = scr->workspaces[i]->clip;
4159 if (clip == aicon->dock)
4160 continue;
4162 if (clip->icon_count + scr->global_icon_count >= clip->max_icons)
4163 return False; /* Clip is full in some workspace */
4165 for (j = 0; j < clip->max_icons; j++) {
4166 btn = clip->icon_array[j];
4167 if (btn && btn->xindex == aicon->xindex && btn->yindex == aicon->yindex)
4168 return False;
4172 return True;
4175 int wClipMakeIconOmnipresent(WAppIcon *aicon, int omnipresent)
4177 WScreen *scr = aicon->icon->core->screen_ptr;
4178 WAppIconChain *new_entry, *tmp, *tmp1;
4179 int status = WO_SUCCESS;
4181 if ((scr->dock && aicon->dock == scr->dock) || aicon == scr->clip_icon)
4182 return WO_NOT_APPLICABLE;
4184 if (aicon->omnipresent == omnipresent)
4185 return WO_SUCCESS;
4187 if (omnipresent) {
4188 if (iconCanBeOmnipresent(aicon)) {
4189 aicon->omnipresent = 1;
4190 new_entry = wmalloc(sizeof(WAppIconChain));
4191 new_entry->aicon = aicon;
4192 new_entry->next = scr->global_icons;
4193 scr->global_icons = new_entry;
4194 scr->global_icon_count++;
4195 } else {
4196 aicon->omnipresent = 0;
4197 status = WO_FAILED;
4199 } else {
4200 aicon->omnipresent = 0;
4201 if (aicon == scr->global_icons->aicon) {
4202 tmp = scr->global_icons->next;
4203 wfree(scr->global_icons);
4204 scr->global_icons = tmp;
4205 scr->global_icon_count--;
4206 } else {
4207 tmp = scr->global_icons;
4208 while (tmp->next) {
4209 if (tmp->next->aicon == aicon) {
4210 tmp1 = tmp->next->next;
4211 wfree(tmp->next);
4212 tmp->next = tmp1;
4213 scr->global_icon_count--;
4214 break;
4216 tmp = tmp->next;
4221 wAppIconPaint(aicon);
4223 return status;
4226 static void drawerAppendToChain(WScreen *scr, WDock *drawer)
4228 WDrawerChain **where_to_add;
4230 where_to_add = &scr->drawers;
4231 while ((*where_to_add) != NULL) {
4232 where_to_add = &(*where_to_add)->next;
4235 *where_to_add = wmalloc(sizeof(WDrawerChain));
4236 (*where_to_add)->adrawer = drawer;
4237 (*where_to_add)->next = NULL;
4238 scr->drawer_count++;
4242 static void drawerRemoveFromChain(WScreen *scr, WDock *drawer)
4244 WDrawerChain *next, **to_remove;
4246 to_remove = &scr->drawers;
4247 while (True) {
4248 if (*to_remove == NULL) {
4249 wwarning("The drawer to be removed can not be found.");
4250 return;
4252 if ((*to_remove)->adrawer == drawer)
4253 break;
4255 to_remove = &(*to_remove)->next;
4257 next = (*to_remove)->next;
4258 wfree(*to_remove);
4259 *to_remove = next;
4260 scr->drawer_count--;
4264 /* Don't free the returned string. Duplicate it. */
4265 static char * findUniqueName(WScreen *scr, const char *instance_basename)
4267 static char buffer[128];
4268 WDrawerChain *dc;
4269 int i;
4270 Bool already_in_use = True;
4272 #define UNIQUE_NAME_WATCHDOG 128
4273 for (i = 0; already_in_use && i < UNIQUE_NAME_WATCHDOG; i++) {
4274 snprintf(buffer, sizeof buffer, "%s%d", instance_basename, i);
4276 already_in_use = False;
4278 for (dc = scr->drawers; dc != NULL; dc = dc->next) {
4279 if (!strncmp(dc->adrawer->icon_array[0]->wm_instance, buffer,
4280 sizeof buffer)) {
4281 already_in_use = True;
4282 break;
4287 if (i == UNIQUE_NAME_WATCHDOG)
4288 wwarning("Couldn't find a unique name for drawer in %d attemps.", i);
4289 #undef UNIQUE_NAME_WATCHDOG
4291 return buffer;
4295 static void drawerIconExpose(WObjDescriptor *desc, XEvent *event)
4297 wDrawerIconPaint((WAppIcon *) desc->parent);
4301 static int addADrawer(WScreen *scr)
4303 int i, y, sig, found_y;
4304 WDock *drawer, *dock = scr->dock;
4305 WDrawerChain *dc;
4306 char can_be_here[2 * dock->max_icons - 1];
4308 if (dock->icon_count + scr->drawer_count >= dock->max_icons)
4309 return -1;
4311 for (y = -dock->max_icons + 1; y < dock->max_icons; y++) {
4312 can_be_here[y + dock->max_icons - 1] = True;
4314 for (i = 0; i < dock->max_icons; i++) {
4315 if (dock->icon_array[i] != NULL)
4316 can_be_here[dock->icon_array[i]->yindex + dock->max_icons - 1] = False;
4318 for (dc = scr->drawers; dc != NULL; dc = dc->next) {
4319 y = (int) ((dc->adrawer->y_pos - dock->y_pos) / ICON_SIZE);
4320 can_be_here[y + dock->max_icons - 1] = False;
4323 found_y = False;
4324 for (sig = 1; !found_y && sig > -2; sig -= 2) // 1, then -1
4326 for (y = sig; sig * y < dock->max_icons; y += sig)
4328 if (can_be_here[y + dock->max_icons - 1] &&
4329 onScreen(scr, dock->x_pos, dock->y_pos + y * ICON_SIZE))
4331 found_y = True;
4332 break;
4337 if (!found_y)
4338 /* This can happen even when dock->icon_count + scr->drawer_count
4339 * < dock->max_icons when the dock is not aligned on an
4340 * ICON_SIZE multiple, as some space is lost above and under it */
4341 return -1;
4343 drawer = wDockCreate(scr, WM_DRAWER, NULL);
4344 drawer->lowered = scr->dock->lowered;
4345 drawer->auto_raise_lower = scr->dock->auto_raise_lower;
4346 drawer->x_pos = dock->x_pos;
4347 drawer->y_pos = dock->y_pos + ICON_SIZE * y;
4348 drawer->icon_array[0]->xindex = 0;
4349 drawer->icon_array[0]->yindex = 0;
4350 drawer->icon_array[0]->x_pos = drawer->x_pos;
4351 drawer->icon_array[0]->y_pos = drawer->y_pos;
4352 XMoveWindow(dpy, drawer->icon_array[0]->icon->core->window,
4353 drawer->icon_array[0]->x_pos, drawer->icon_array[0]->y_pos);
4355 return 0;
4359 static void addADrawerCallback(WMenu *menu, WMenuEntry *entry)
4361 assert(entry->clientdata!=NULL);
4362 addADrawer(((WAppIcon *) entry->clientdata)->dock->screen_ptr);
4366 static void drawerDestroy(WDock *drawer)
4368 WScreen *scr;
4369 int i;
4370 WAppIcon *aicon;
4371 WMArray *icons;
4373 if (drawer == NULL)
4374 return;
4376 scr = drawer->screen_ptr;
4378 /* Note regarding menus: we can't delete any dock/clip/drawer menu, because
4379 * that would (attempt to) wfree some memory in gettext library (see menu
4380 * entries that have several "versions", such like "Hide" and "Unhide"). */
4382 wDefaultPurgeInfo(scr, drawer->icon_array[0]->wm_instance,
4383 drawer->icon_array[0]->wm_class);
4384 icons = WMCreateArray(drawer->icon_count - 1);
4385 for (i = 1; i < drawer->max_icons; i++) {
4386 if (!(aicon = drawer->icon_array[i]))
4387 continue;
4388 WMAddToArray(icons, aicon);
4390 removeIcons(icons, drawer);
4392 if (drawer->auto_collapse_magic) {
4393 WMDeleteTimerHandler(drawer->auto_collapse_magic);
4394 drawer->auto_collapse_magic = NULL;
4396 if (drawer->auto_lower_magic) {
4397 WMDeleteTimerHandler(drawer->auto_lower_magic);
4398 drawer->auto_lower_magic = NULL;
4401 wAppIconDestroy(drawer->icon_array[0]);
4402 wfree(drawer->icon_array);
4403 wfree(drawer);
4405 drawerRemoveFromChain(scr, drawer);
4406 if (scr->last_dock == drawer)
4407 scr->last_dock = NULL;
4408 if (scr->attracting_drawer == drawer)
4409 scr->attracting_drawer = NULL;
4413 static void removeDrawerCallback(WMenu *menu, WMenuEntry *entry)
4415 WDock *dock = ((WAppIcon*)entry->clientdata)->dock;
4416 assert(dock != NULL);
4418 if (dock->icon_count > 1) {
4419 if (wMessageDialog(dock->screen_ptr, _("Drawer"),
4420 _("All icons in this drawer will be detached!"),
4421 _("OK"), _("Cancel"), NULL) != WAPRDefault)
4422 return;
4424 drawerDestroy(dock);
4428 void wDrawerIconPaint(WAppIcon *dicon)
4430 Window win = dicon->icon->core->window;
4431 WScreen *scr = dicon->icon->core->screen_ptr;
4432 XPoint p[4];
4433 GC gc = scr->draw_gc;
4434 WMColor *color;
4436 wIconPaint(dicon->icon);
4438 if (!dicon->dock->collapsed)
4439 color = scr->clip_title_color[CLIP_NORMAL];
4440 else
4441 color = scr->clip_title_color[CLIP_COLLAPSED];
4442 XSetForeground(dpy, gc, WMColorPixel(color));
4444 if (dicon->dock->on_right_side) {
4445 p[0].x = p[3].x = 10;
4446 p[0].y = p[3].y = ICON_SIZE / 2 - 5;
4447 p[1].x = 10;
4448 p[1].y = ICON_SIZE / 2 + 5;
4449 p[2].x = 5;
4450 p[2].y = ICON_SIZE / 2;
4452 else {
4453 p[0].x = p[3].x = ICON_SIZE-1 - 10;
4454 p[0].y = p[3].y = ICON_SIZE / 2 - 5;
4455 p[1].x = ICON_SIZE-1 - 10;
4456 p[1].y = ICON_SIZE / 2 + 5;
4457 p[2].x = ICON_SIZE-1 - 5;
4458 p[2].y = ICON_SIZE / 2;
4460 XFillPolygon(dpy, win, gc, p,3,Convex,CoordModeOrigin);
4461 XDrawLines(dpy, win, gc, p,4,CoordModeOrigin);
4465 RImage* wDrawerMakeTile(WScreen *scr, RImage *normalTile)
4467 RImage *tile = RCloneImage(normalTile);
4468 RColor dark;
4469 RColor light;
4471 dark.alpha = 0;
4472 dark.red = dark.green = dark.blue = 60;
4474 light.alpha = 0;
4475 light.red = light.green = light.blue = 80;
4477 /* arrow bevel */
4478 if (!scr->dock || scr->dock->on_right_side) {
4479 ROperateLine(tile, RSubtractOperation, 11, ICON_SIZE / 2 - 7,
4480 4, ICON_SIZE / 2, &dark); /* / */
4481 ROperateLine(tile, RSubtractOperation, 11, ICON_SIZE / 2 + 7,
4482 4, ICON_SIZE / 2, &dark); /* \ */
4483 ROperateLine(tile, RAddOperation, 11, ICON_SIZE / 2 - 7,
4484 11, ICON_SIZE / 2 + 7, &light); /* | */
4486 else {
4487 ROperateLine(tile, RSubtractOperation, ICON_SIZE-1 - 11, ICON_SIZE / 2 - 7,
4488 ICON_SIZE-1 - 4, ICON_SIZE / 2, &dark); /* \ */
4489 ROperateLine(tile, RAddOperation, ICON_SIZE-1 - 11, ICON_SIZE / 2 + 7,
4490 ICON_SIZE-1 - 4, ICON_SIZE / 2, &light); /* / */
4491 ROperateLine(tile, RSubtractOperation, ICON_SIZE-1 - 11, ICON_SIZE / 2 - 7,
4492 ICON_SIZE-1 - 11, ICON_SIZE / 2 + 7, &dark); /* | */
4494 return tile;
4498 static void swapDrawer(WDock *drawer, int new_x)
4500 int i;
4502 drawer->on_right_side = !drawer->on_right_side;
4503 drawer->x_pos = new_x;
4505 for (i = 0; i < drawer->max_icons; i++) {
4506 WAppIcon *ai;
4507 if ((ai = drawer->icon_array[i]) == NULL)
4508 continue;
4509 ai->xindex *= -1; /* so A B C becomes C B A */
4510 ai->x_pos = new_x + ai->xindex * ICON_SIZE;
4512 /* Update drawer's tile */
4513 if (i == 0) {
4514 wIconUpdate(ai->icon);
4515 wDrawerIconPaint(ai);
4517 XMoveWindow(dpy, ai->icon->core->window, ai->x_pos, ai->y_pos);
4522 static void swapDrawers(WScreen *scr, int on_right_side, int new_x)
4524 WDrawerChain *dc;
4526 if (scr->drawer_tile) {
4527 RReleaseImage(scr->drawer_tile);
4529 scr->drawer_tile = wDrawerMakeTile(scr, scr->icon_tile);
4531 for (dc = scr->drawers; dc != NULL; dc = dc->next) {
4532 swapDrawer(dc->adrawer, new_x);
4533 assert(dc->adrawer->on_right_side == on_right_side);
4538 int wIsADrawer(WScreen *scr, WAppIcon *aicon)
4540 return aicon && aicon->dock &&
4541 aicon->dock->type == WM_DRAWER && aicon->dock->icon_array[0] == aicon;
4545 static WDock* getDrawer(WScreen *scr, int y_index)
4547 WDrawerChain *dc;
4549 for (dc = scr->drawers; dc != NULL; dc = dc->next) {
4550 if (dc->adrawer->y_pos - scr->dock->y_pos == y_index * ICON_SIZE)
4551 return dc->adrawer;
4553 return NULL;
4558 /* Find the "hole" a moving appicon created when snapped into the
4559 * drawer. redocking is a boolean. If the moving appicon comes from the
4560 * drawer, drawer->icon_count is correct. If not, redocking is then false and
4561 * there are now drawer->icon_count plus one appicons in the drawer. */
4562 static int indexOfHole(WDock *drawer, WAppIcon *moving_aicon, int redocking)
4564 int index_of_hole, i;
4566 /* Classic interview question...
4568 * We have n-1 (n = drawer->icon_count-1 or drawer->icon_count, see
4569 * redocking) appicons, whose xindex are unique in [1..n]. One is missing:
4570 * that's where the ghost of the moving appicon is, that's what the
4571 * function should return.
4573 * We compute 1+2+...+n (this sum is equal to n*(n+1)/2), we substract to
4574 * this sum the xindex of each of the n-1 appicons, and we get the correct
4575 * index! */
4577 if (redocking) {
4578 index_of_hole = (drawer->icon_count - 1) * drawer->icon_count / 2;
4579 } else {
4580 index_of_hole = drawer->icon_count * (drawer->icon_count + 1) / 2;
4582 index_of_hole *= (drawer->on_right_side ? -1 : 1);
4584 for (i = 1; i < drawer->max_icons; i++) {
4585 if (drawer->icon_array[i] && drawer->icon_array[i] != moving_aicon)
4586 index_of_hole -= drawer->icon_array[i]->xindex;
4588 /* wmessage(" Index of the moving appicon is %d (%sredocking)", index_of_hole, (redocking ? "" : "not ")); */
4589 if (abs(index_of_hole) > abs(drawer->icon_count) - (redocking ? 1 : 0))
4590 wwarning(" index_of_hole is too large ! (%d greater than %d)",
4591 index_of_hole, abs(drawer->icon_count) - (redocking ? 1 : 0));
4592 if (index_of_hole == 0)
4593 wwarning(" index_of_hole == 0 (%sredocking, icon_count == %d)", (redocking ? "" : "not "), drawer->icon_count);
4595 return index_of_hole;
4599 void wSlideAppicons(WAppIcon **appicons, int n, int to_the_left)
4601 int i;
4602 int leftmost = -1, min_index = 9999, from_x = -1; // leftmost and from_x initialized to avoid warning
4603 Window *wins[n];
4604 WAppIcon *aicon;
4606 if (n < 1)
4607 return;
4609 for (i = 0; i < n; i++) {
4610 aicon = appicons[i];
4611 aicon->xindex += (to_the_left ? -1 : +1);
4612 if (aicon->xindex < min_index) {
4613 min_index = aicon->xindex;
4614 leftmost = i;
4615 from_x = aicon->x_pos;
4617 aicon->x_pos += (to_the_left ? -ICON_SIZE : +ICON_SIZE);
4620 for (i = 0; i < n; i++) {
4621 aicon = appicons[i];
4622 wins[aicon->xindex - min_index] = &aicon->icon->core->window;
4624 aicon = appicons[leftmost];
4625 SlideWindows(wins, n, from_x, aicon->y_pos, aicon->x_pos, aicon->y_pos);
4629 void wDrawerFillTheGap(WDock *drawer, WAppIcon *aicon, Bool redocking)
4631 int i, j;
4632 int index_of_hole = indexOfHole(drawer, aicon, redocking);
4633 WAppIcon *aicons_to_shift[drawer->icon_count];
4635 j = 0;
4636 for (i = 0; i < drawer->max_icons; i++) {
4637 WAppIcon *ai = drawer->icon_array[i];
4638 if (ai && ai != aicon &&
4639 abs(ai->xindex) > abs(index_of_hole))
4640 aicons_to_shift[j++] = ai;
4642 if (j != drawer->icon_count - abs(index_of_hole) - (redocking ? 1 : 0))
4643 wwarning("Removing aicon at index %d from %s: j=%d but should be %d",
4644 index_of_hole, drawer->icon_array[0]->wm_instance,
4645 j, drawer->icon_count - abs(index_of_hole) - (redocking ? 1 : 0));
4646 wSlideAppicons(aicons_to_shift, j, !drawer->on_right_side);
4650 static void drawerConsolidateIcons(WDock *drawer)
4652 WAppIcon *aicons_to_shift[drawer->icon_count];
4653 int maxRemaining = 0;
4654 int sum = 0;
4655 int i;
4656 for (i = 0; i < drawer->max_icons; i++) {
4657 WAppIcon *ai = drawer->icon_array[i];
4658 if (ai == NULL)
4659 continue;
4660 sum += abs(ai->xindex);
4661 if (abs(ai->xindex) > maxRemaining)
4662 maxRemaining = abs(ai->xindex);
4664 while (sum != maxRemaining * (maxRemaining + 1) / 2) { // while there is a hole
4665 WAppIcon *ai;
4666 int n;
4667 // Look up for the hole at max position
4668 int maxDeleted;
4669 for (maxDeleted = maxRemaining - 1; maxDeleted > 0; maxDeleted--) {
4670 Bool foundAppIconThere = False;
4671 for (i = 0; i < drawer->max_icons; i++) {
4672 WAppIcon *ai = drawer->icon_array[i];
4673 if (ai == NULL)
4674 continue;
4675 if (abs(ai->xindex) == maxDeleted) {
4676 foundAppIconThere = True;
4677 break;
4680 if (!foundAppIconThere)
4681 break;
4683 assert(maxDeleted > 0); // would mean while test is wrong
4684 n = 0;
4685 for (i = 0; i < drawer->max_icons; i++) {
4686 ai = drawer->icon_array[i];
4687 if (ai != NULL && abs(ai->xindex) > maxDeleted)
4688 aicons_to_shift[n++] = ai;
4690 assert(n == maxRemaining - maxDeleted); // for the code review ;-)
4691 wSlideAppicons(aicons_to_shift, n, !drawer->on_right_side);
4692 // Efficient beancounting
4693 maxRemaining -= 1;
4694 sum -= n;
4700 /* similar to wDockRestoreState, but a lot a specific stuff too... */
4701 static WDock * drawerRestoreState(WScreen *scr, WMPropList *drawer_state)
4703 WDock *drawer;
4704 WMPropList *apps, *value, *dock_state;
4705 WAppIcon *aicon;
4706 int count, i;
4708 if (!drawer_state)
4709 return NULL;
4711 make_keys();
4713 WMRetainPropList(drawer_state);
4715 /* Get the instance name, and create a drawer */
4716 value = WMGetFromPLDictionary(drawer_state, dName);
4717 drawer = wDockCreate(scr, WM_DRAWER, WMGetFromPLString(value));
4719 /* restore DnD command and paste command */
4720 #ifdef XDND
4721 value = WMGetFromPLDictionary(drawer_state, dDropCommand);
4722 if (value && WMIsPLString(value))
4723 drawer->icon_array[0]->dnd_command = wstrdup(WMGetFromPLString(value));
4724 #endif /* XDND */
4726 value = WMGetFromPLDictionary(drawer_state, dPasteCommand);
4727 if (value && WMIsPLString(value))
4728 drawer->icon_array[0]->paste_command = wstrdup(WMGetFromPLString(value));
4730 /* restore position */
4731 value = WMGetFromPLDictionary(drawer_state, dPosition);
4732 if (!value || !WMIsPLString(value))
4733 COMPLAIN("Position");
4734 else {
4735 int x, y, y_index;
4736 if (sscanf(WMGetFromPLString(value), "%i,%i", &x, &y) != 2)
4737 COMPLAIN("Position");
4739 /* check position sanity */
4740 if (x != scr->dock->x_pos) {
4741 x = scr->dock->x_pos;
4743 y_index = (y - scr->dock->y_pos) / ICON_SIZE;
4744 if (y_index >= scr->dock->max_icons) {
4745 /* Here we should do something more intelligent, since it
4746 * can happen even if the user hasn't hand-edited his
4747 * G/D/State file (but uses a lower resolution). */
4748 y_index = scr->dock->max_icons - 1;
4750 y = scr->dock->y_pos + y_index * ICON_SIZE;
4751 moveDock(drawer, x, y);
4754 /* restore dock properties (applist and others) */
4755 dock_state = WMGetFromPLDictionary(drawer_state, dDock);
4757 /* restore lowered/raised state: same as scr->dock, no matter what */
4758 drawer->lowered = scr->dock->lowered;
4759 if (!drawer->lowered)
4760 ChangeStackingLevel(drawer->icon_array[0]->icon->core, WMDockLevel);
4761 else
4762 ChangeStackingLevel(drawer->icon_array[0]->icon->core, WMNormalLevel);
4763 wRaiseFrame(drawer->icon_array[0]->icon->core);
4765 /* restore collapsed state */
4766 drawer->collapsed = 0;
4767 value = WMGetFromPLDictionary(dock_state, dCollapsed);
4768 if (value && strcasecmp(WMGetFromPLString(value), "YES") == 0) {
4769 drawer->collapsed = 1;
4771 /* restore auto-collapsed state */
4772 value = WMGetFromPLDictionary(dock_state, dAutoCollapse);
4773 if (value && strcasecmp(WMGetFromPLString(value), "YES") == 0) {
4774 drawer->auto_collapse = 1;
4775 drawer->collapsed = 1;
4776 } else {
4777 drawer->auto_collapse = 0; // because wDockCreate sets it (drawers only)
4780 /* restore auto-raise/lower state: same as scr->dock, no matter what */
4781 drawer->auto_raise_lower = scr->dock->auto_raise_lower;
4783 /* restore attract icons state */
4784 drawer->attract_icons = 0;
4785 value = WMGetFromPLDictionary(dock_state, dAutoAttractIcons);
4786 if (value && strcasecmp(WMGetFromPLString(value), "YES") == 0) {
4787 drawer->attract_icons = 1;
4788 scr->attracting_drawer = drawer;
4791 /* application list */
4792 apps = WMGetFromPLDictionary(dock_state, dApplications);
4794 if (!apps) {
4795 goto finish;
4798 count = WMGetPropListItemCount(apps);
4800 if (count == 0)
4801 goto finish;
4803 for (i=0; i<count; i++) {
4804 if (drawer->icon_count >= drawer->max_icons) {
4805 wwarning(_("there are too many icons stored in drawer. Ignoring what doesn't fit"));
4806 break;
4809 value = WMGetFromPLArray(apps, i);
4810 aicon = restore_icon_state(scr, value, WM_DRAWER, drawer->icon_count);
4812 drawer->icon_array[drawer->icon_count] = aicon;
4814 if (aicon) {
4815 aicon->dock = drawer;
4816 aicon->x_pos = drawer->x_pos + (aicon->xindex * ICON_SIZE);
4817 aicon->y_pos = drawer->y_pos + (aicon->yindex * ICON_SIZE);
4819 if (!drawer->lowered)
4820 ChangeStackingLevel(aicon->icon->core, WMDockLevel);
4821 else
4822 ChangeStackingLevel(aicon->icon->core, WMNormalLevel);
4824 wCoreConfigure(aicon->icon->core, aicon->x_pos, aicon->y_pos, 0, 0);
4826 if (!drawer->collapsed)
4827 XMapWindow(dpy, aicon->icon->core->window);
4828 wRaiseFrame(aicon->icon->core);
4830 drawer->icon_count++;
4834 finish:
4835 WMReleasePropList(drawer_state);
4837 return drawer;
4841 /* Same kind of comment than for previous function: this function is
4842 * very similar to make_icon_state, but has substential differences as
4843 * well. */
4844 static WMPropList *drawerSaveState(WScreen *scr, WDock *drawer)
4846 WMPropList *pstr, *drawer_state;
4847 WAppIcon *ai;
4848 char buffer[64];
4850 ai = drawer->icon_array[0];
4851 /* Store its name */
4852 pstr = WMCreatePLString(ai->wm_instance);
4853 drawer_state = WMCreatePLDictionary(dName, pstr, NULL); /* we need this final NULL */
4854 WMReleasePropList(pstr);
4856 /* Store its position */
4857 snprintf(buffer, sizeof(buffer), "%i,%i", ai->x_pos, ai->y_pos);
4858 pstr = WMCreatePLString(buffer);
4859 WMPutInPLDictionary(drawer_state, dPosition, pstr);
4860 WMReleasePropList(pstr);
4862 #ifdef XDND
4863 /* Store its DnD command */
4864 if (ai->dnd_command) {
4865 pstr = WMCreatePLString(ai->dnd_command);
4866 WMPutInPLDictionary(drawer_state, dDropCommand, pstr);
4867 WMReleasePropList(pstr);
4869 #endif /* XDND */
4871 /* Store its paste command */
4872 if (ai->paste_command) {
4873 pstr = WMCreatePLString(ai->paste_command);
4874 WMPutInPLDictionary(drawer_state, dPasteCommand, pstr);
4875 WMReleasePropList(pstr);
4878 /* Store applications list and other properties */
4879 pstr = dockSaveState(drawer);
4880 WMPutInPLDictionary(drawer_state, dDock, pstr);
4881 WMReleasePropList(pstr);
4883 return drawer_state;
4887 void wDrawersSaveState(WScreen *scr)
4889 WMPropList *all_drawers, *drawer_state;
4890 int i;
4891 WDrawerChain *dc;
4893 make_keys();
4895 all_drawers = WMCreatePLArray(NULL);
4896 for (i=0, dc = scr->drawers;
4897 i < scr->drawer_count;
4898 i++, dc = dc->next) {
4899 drawer_state = drawerSaveState(scr, dc->adrawer);
4900 WMAddToPLArray(all_drawers, drawer_state);
4901 WMReleasePropList(drawer_state);
4903 WMPutInPLDictionary(scr->session_state, dDrawers, all_drawers);
4904 WMReleasePropList(all_drawers);
4908 void wDrawersRestoreState(WScreen *scr)
4910 WMPropList *all_drawers, *drawer_state;
4911 int i;
4913 make_keys();
4915 if (scr->session_state == NULL)
4916 return;
4918 all_drawers = WMGetFromPLDictionary(scr->session_state, dDrawers);
4919 if (!all_drawers)
4920 return;
4922 for (i = 0; i < WMGetPropListItemCount(all_drawers); i++) {
4923 drawer_state = WMGetFromPLArray(all_drawers, i);
4924 drawerRestoreState(scr, drawer_state);
4925 // Note: scr->drawers was updated when the the drawer was created