wmaker: Added 'const' attribute to most local functions
[wmaker-crm.git] / src / dock.c
blobdf924868cecba925257b3d5855abc999ec853c3d
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 <string.h>
30 #include <strings.h>
31 #include <unistd.h>
32 #include <math.h>
33 #include <limits.h>
35 #ifndef PATH_MAX
36 #define PATH_MAX DEFAULT_PATH_MAX
37 #endif
39 #include "WindowMaker.h"
40 #include "wcore.h"
41 #include "window.h"
42 #include "icon.h"
43 #include "appicon.h"
44 #include "actions.h"
45 #include "stacking.h"
46 #include "dock.h"
47 #include "dockedapp.h"
48 #include "dialog.h"
49 #include "main.h"
50 #include "properties.h"
51 #include "menu.h"
52 #include "client.h"
53 #include "defaults.h"
54 #include "workspace.h"
55 #include "framewin.h"
56 #include "superfluous.h"
57 #include "xinerama.h"
58 #include "placement.h"
59 #include "misc.h"
60 #include "event.h"
62 /**** Local variables ****/
63 #define CLIP_REWIND 1
64 #define CLIP_IDLE 0
65 #define CLIP_FORWARD 2
67 /**** Global variables ****/
68 extern Cursor wCursor[WCUR_LAST];
69 extern WPreferences wPreferences;
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, 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, 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 char *basename(const char *shortname);
213 int argc;
215 if (!WCHECK_STATE(WSTATE_NORMAL))
216 return;
218 assert(entry->clientdata != NULL);
220 icon = (WAppIcon *) entry->clientdata;
222 icon->editing = 1;
224 WCHANGE_STATE(WSTATE_MODAL);
226 /* strip away dir names */
227 shortname = basename(icon->command);
228 /* separate out command options */
229 wtokensplit(shortname, &argv, &argc);
231 buffer = wstrconcat(argv[0],
232 _(" will be forcibly closed.\n"
233 "Any unsaved changes will be lost.\n" "Please confirm."));
235 if (icon->icon && icon->icon->owner) {
236 fPtr = icon->icon->owner->fake_group;
237 } else {
238 /* is this really necessary? can we kill a non-running dock icon? */
239 Window win = icon->main_window;
240 int index;
242 index = WMFindInArray(scr->fakeGroupLeaders, matchWindow, (void *)win);
243 if (index != WANotFound)
244 fPtr = WMGetFromArray(scr->fakeGroupLeaders, index);
245 else
246 fPtr = NULL;
249 if (wPreferences.dont_confirm_kill
250 || wMessageDialog(menu->frame->screen_ptr, _("Kill Application"),
251 buffer, _("Yes"), _("No"), NULL) == WAPRDefault) {
252 if (fPtr != NULL) {
253 WWindow *wwin, *twin;
255 wwin = scr->focused_window;
256 while (wwin) {
257 twin = wwin->prev;
258 if (wwin->fake_group == fPtr)
259 wClientKill(wwin);
261 wwin = twin;
263 } else if (icon->icon && icon->icon->owner) {
264 wClientKill(icon->icon->owner);
268 wfree(buffer);
269 wtokenfree(argv, argc);
271 icon->editing = 0;
273 WCHANGE_STATE(WSTATE_NORMAL);
276 /* TODO: replace this function with a member of the dock struct */
277 static int numberOfSelectedIcons(WDock *dock)
279 WAppIcon *aicon;
280 int i, n;
282 n = 0;
283 for (i = 1; i < dock->max_icons; i++) {
284 aicon = dock->icon_array[i];
285 if (aicon && aicon->icon->selected)
286 n++;
289 return n;
292 static WMArray *getSelected(WDock *dock)
294 WMArray *ret = WMCreateArray(8);
295 WAppIcon *btn;
296 int i;
298 for (i = 1; i < dock->max_icons; i++) {
299 btn = dock->icon_array[i];
300 if (btn && btn->icon->selected)
301 WMAddToArray(ret, btn);
304 return ret;
307 static void paintClipButtons(WAppIcon *clipIcon, Bool lpushed, Bool rpushed)
309 Window win = clipIcon->icon->core->window;
310 WScreen *scr = clipIcon->icon->core->screen_ptr;
311 XPoint p[4];
312 int pt = CLIP_BUTTON_SIZE * ICON_SIZE / 64;
313 int tp = ICON_SIZE - pt;
314 int as = pt - 15; /* 15 = 5+5+5 */
315 GC gc = scr->draw_gc; /* maybe use WMColorGC() instead here? */
316 WMColor *color;
318 color = scr->clip_title_color[CLIP_NORMAL];
320 XSetForeground(dpy, gc, WMColorPixel(color));
322 if (rpushed) {
323 p[0].x = tp + 1;
324 p[0].y = 1;
325 p[1].x = ICON_SIZE - 2;
326 p[1].y = 1;
327 p[2].x = ICON_SIZE - 2;
328 p[2].y = pt - 1;
329 } else if (lpushed) {
330 p[0].x = 1;
331 p[0].y = tp;
332 p[1].x = pt;
333 p[1].y = ICON_SIZE - 2;
334 p[2].x = 1;
335 p[2].y = ICON_SIZE - 2;
337 if (lpushed || rpushed) {
338 XSetForeground(dpy, scr->draw_gc, scr->white_pixel);
339 XFillPolygon(dpy, win, scr->draw_gc, p, 3, Convex, CoordModeOrigin);
340 XSetForeground(dpy, scr->draw_gc, scr->black_pixel);
343 /* top right arrow */
344 p[0].x = p[3].x = ICON_SIZE - 5 - as;
345 p[0].y = p[3].y = 5;
346 p[1].x = ICON_SIZE - 6;
347 p[1].y = 5;
348 p[2].x = ICON_SIZE - 6;
349 p[2].y = 4 + as;
350 if (rpushed) {
351 XFillPolygon(dpy, win, scr->draw_gc, p, 3, Convex, CoordModeOrigin);
352 XDrawLines(dpy, win, scr->draw_gc, p, 4, CoordModeOrigin);
353 } else {
354 XFillPolygon(dpy, win, gc, p, 3, Convex, CoordModeOrigin);
355 XDrawLines(dpy, win, gc, p, 4, CoordModeOrigin);
358 /* bottom left arrow */
359 p[0].x = p[3].x = 5;
360 p[0].y = p[3].y = ICON_SIZE - 5 - as;
361 p[1].x = 5;
362 p[1].y = ICON_SIZE - 6;
363 p[2].x = 4 + as;
364 p[2].y = ICON_SIZE - 6;
365 if (lpushed) {
366 XFillPolygon(dpy, win, scr->draw_gc, p, 3, Convex, CoordModeOrigin);
367 XDrawLines(dpy, win, scr->draw_gc, p, 4, CoordModeOrigin);
368 } else {
369 XFillPolygon(dpy, win, gc, p, 3, Convex, CoordModeOrigin);
370 XDrawLines(dpy, win, gc, p, 4, CoordModeOrigin);
374 RImage *wClipMakeTile(WScreen *scr, RImage *normalTile)
376 RImage *tile = RCloneImage(normalTile);
377 RColor black;
378 RColor dark;
379 RColor light;
380 int pt, tp;
381 int as;
383 pt = CLIP_BUTTON_SIZE * wPreferences.icon_size / 64;
384 tp = wPreferences.icon_size - 1 - pt;
385 as = pt - 15;
387 black.alpha = 255;
388 black.red = black.green = black.blue = 0;
390 dark.alpha = 0;
391 dark.red = dark.green = dark.blue = 60;
393 light.alpha = 0;
394 light.red = light.green = light.blue = 80;
396 /* top right */
397 ROperateLine(tile, RSubtractOperation, tp, 0, wPreferences.icon_size - 2, pt - 1, &dark);
398 RDrawLine(tile, tp - 1, 0, wPreferences.icon_size - 1, pt + 1, &black);
399 ROperateLine(tile, RAddOperation, tp, 2, wPreferences.icon_size - 3, pt, &light);
401 /* arrow bevel */
402 ROperateLine(tile, RSubtractOperation, ICON_SIZE - 7 - as, 4, ICON_SIZE - 5, 4, &dark);
403 ROperateLine(tile, RSubtractOperation, ICON_SIZE - 6 - as, 5, ICON_SIZE - 5, 6 + as, &dark);
404 ROperateLine(tile, RAddOperation, ICON_SIZE - 5, 4, ICON_SIZE - 5, 6 + as, &light);
406 /* bottom left */
407 ROperateLine(tile, RAddOperation, 2, tp + 2, pt - 2, wPreferences.icon_size - 3, &dark);
408 RDrawLine(tile, 0, tp - 1, pt + 1, wPreferences.icon_size - 1, &black);
409 ROperateLine(tile, RSubtractOperation, 0, tp - 2, pt + 1, wPreferences.icon_size - 2, &light);
411 /* arrow bevel */
412 ROperateLine(tile, RSubtractOperation, 4, ICON_SIZE - 7 - as, 4, ICON_SIZE - 5, &dark);
413 ROperateLine(tile, RSubtractOperation, 5, ICON_SIZE - 6 - as, 6 + as, ICON_SIZE - 5, &dark);
414 ROperateLine(tile, RAddOperation, 4, ICON_SIZE - 5, 6 + as, ICON_SIZE - 5, &light);
416 return tile;
419 static void omnipresentCallback(WMenu *menu, WMenuEntry *entry)
421 WAppIcon *clickedIcon = entry->clientdata;
422 WAppIcon *aicon;
423 WDock *dock;
424 WMArray *selectedIcons;
425 WMArrayIterator iter;
426 int failed;
428 assert(entry->clientdata != NULL);
430 dock = clickedIcon->dock;
432 selectedIcons = getSelected(dock);
434 if (!WMGetArrayItemCount(selectedIcons))
435 WMAddToArray(selectedIcons, clickedIcon);
437 failed = 0;
438 WM_ITERATE_ARRAY(selectedIcons, aicon, iter) {
439 if (wClipMakeIconOmnipresent(aicon, !aicon->omnipresent) == WO_FAILED)
440 failed++;
441 else if (aicon->icon->selected)
442 wIconSelect(aicon->icon);
444 WMFreeArray(selectedIcons);
446 if (failed > 1) {
447 wMessageDialog(dock->screen_ptr, _("Warning"),
448 _("Some icons cannot be made omnipresent. "
449 "Please make sure that no other icon is "
450 "docked in the same positions on the other "
451 "workspaces and the Clip is not full in "
452 "some workspace."), _("OK"), NULL, NULL);
453 } else if (failed == 1) {
454 wMessageDialog(dock->screen_ptr, _("Warning"),
455 _("Icon cannot be made omnipresent. "
456 "Please make sure that no other icon is "
457 "docked in the same position on the other "
458 "workspaces and the Clip is not full in "
459 "some workspace."), _("OK"), NULL, NULL);
463 static void removeIcons(WMArray *icons, WDock *dock)
465 WAppIcon *aicon;
466 int keepit;
467 WMArrayIterator it;
469 WM_ITERATE_ARRAY(icons, aicon, it) {
470 keepit = aicon->running && wApplicationOf(aicon->main_window);
471 wDockDetach(dock, aicon);
472 if (keepit) {
473 /* XXX: can: aicon->icon == NULL ? */
474 PlaceIcon(dock->screen_ptr, &aicon->x_pos, &aicon->y_pos,
475 wGetHeadForWindow(aicon->icon->owner));
476 XMoveWindow(dpy, aicon->icon->core->window, aicon->x_pos, aicon->y_pos);
477 if (!dock->mapped || dock->collapsed)
478 XMapWindow(dpy, aicon->icon->core->window);
481 WMFreeArray(icons);
483 if (wPreferences.auto_arrange_icons)
484 wArrangeIcons(dock->screen_ptr, True);
487 static void removeIconsCallback(WMenu *menu, WMenuEntry *entry)
489 WAppIcon *clickedIcon = (WAppIcon *) entry->clientdata;
490 WDock *dock;
491 WMArray *selectedIcons;
493 assert(clickedIcon != NULL);
495 dock = clickedIcon->dock;
497 selectedIcons = getSelected(dock);
499 if (WMGetArrayItemCount(selectedIcons)) {
500 if (wMessageDialog(dock->screen_ptr,
501 dock->type == WM_CLIP ? _("Workspace Clip") : _("Drawer"),
502 _("All selected icons will be removed!"),
503 _("OK"), _("Cancel"), NULL) != WAPRDefault) {
504 WMFreeArray(selectedIcons);
505 return;
507 } else {
508 if (clickedIcon->xindex == 0 && clickedIcon->yindex == 0) {
509 WMFreeArray(selectedIcons);
510 return;
512 WMAddToArray(selectedIcons, clickedIcon);
515 removeIcons(selectedIcons, dock);
517 if (dock->type == WM_DRAWER) {
518 drawerConsolidateIcons(dock);
522 static void keepIconsCallback(WMenu *menu, WMenuEntry *entry)
524 WAppIcon *clickedIcon = (WAppIcon *) entry->clientdata;
525 WDock *dock;
526 WAppIcon *aicon;
527 WMArray *selectedIcons;
528 WMArrayIterator it;
530 assert(clickedIcon != NULL);
531 dock = clickedIcon->dock;
533 selectedIcons = getSelected(dock);
535 if (!WMGetArrayItemCount(selectedIcons)
536 && clickedIcon != dock->screen_ptr->clip_icon) {
537 char *command = NULL;
539 if (!clickedIcon->command && !clickedIcon->editing) {
540 clickedIcon->editing = 1;
541 if (wInputDialog(dock->screen_ptr, _("Keep Icon"),
542 _("Type the command used to launch the application"), &command)) {
543 if (command && (command[0] == 0 || (command[0] == '-' && command[1] == 0))) {
544 wfree(command);
545 command = NULL;
547 clickedIcon->command = command;
548 clickedIcon->editing = 0;
549 } else {
550 clickedIcon->editing = 0;
551 if (command)
552 wfree(command);
553 WMFreeArray(selectedIcons);
554 return;
558 WMAddToArray(selectedIcons, clickedIcon);
561 WM_ITERATE_ARRAY(selectedIcons, aicon, it) {
562 if (aicon->icon->selected)
563 wIconSelect(aicon->icon);
565 if (aicon && aicon->attracted && aicon->command) {
566 aicon->attracted = 0;
567 if (aicon->icon->shadowed) {
568 aicon->icon->shadowed = 0;
571 * Update icon pixmap, RImage doesn't change,
572 * so call wIconUpdate is not needed
574 update_icon_pixmap(aicon->icon);
576 /* Paint it */
577 wAppIconPaint(aicon);
580 save_appicon(aicon, True);
582 WMFreeArray(selectedIcons);
585 static void toggleAutoAttractCallback(WMenu *menu, WMenuEntry *entry)
587 WDock *dock = (WDock *) entry->clientdata;
588 WScreen *scr = dock->screen_ptr;
590 assert(entry->clientdata != NULL);
592 /* Setting a clip as autoattracting will disable an autoattracting drawer,
593 * and reciprocally. Ask for confirmation? */
594 #ifdef CONFIRM_AUTOATTRACT_CHANGE
595 if (dock->type == WM_CLIP && scr->attracting_drawer) {
596 if (wMessageDialog(scr, _("Workspace Clip"),
597 _("The drawer that auto-attracted icons so far won't do so anymore!"),
598 _("OK"), _("Cancel"), NULL) != WAPRDefault)
599 return;
600 } else if (dock->type == WM_DRAWER) {
601 /* check if at least one clip already auto attracts icons */
602 int i, ask_for_confirmation = False;
603 for (i = 0; i < scr->workspace_count; i++) {
604 if (scr->workspaces[ i ]->clip->attract_icons) {
605 ask_for_confirmation = True;
606 break;
609 if (ask_for_confirmation) {
610 if (wMessageDialog(scr, _("Drawer"),
611 _("Any clips that auto-attracted icons so far won't do so anymore!"),
612 _("OK"), _("Cancel"), NULL) != WAPRDefault)
613 return;
616 #endif
618 dock->attract_icons = !dock->attract_icons;
620 entry->flags.indicator_on = dock->attract_icons;
622 wMenuPaint(menu);
624 if (dock->attract_icons) {
625 if (dock->type == WM_DRAWER) {
626 /* The newly auto-attracting dock is a drawer: disable any clip and
627 * previously attracting drawer */
628 int i;
629 for (i = 0; i < scr->workspace_count; i++) {
630 scr->workspaces[ i ]->clip->attract_icons = False;
631 /* dock menu will be updated later, when opened */
633 if (scr->attracting_drawer != NULL)
634 scr->attracting_drawer->attract_icons = False;
635 scr->attracting_drawer = dock;
636 } else {
637 /* The newly auto-attracting dock is a clip: disable
638 * previously attracting drawer, if applicable */
639 if (scr->attracting_drawer != NULL) {
640 scr->attracting_drawer->attract_icons = False;
641 /* again, its menu will be updated, later. */
642 scr->attracting_drawer = NULL;
648 static void selectCallback(WMenu *menu, WMenuEntry *entry)
650 WAppIcon *icon = (WAppIcon *) entry->clientdata;
652 assert(icon != NULL);
654 wIconSelect(icon->icon);
656 wMenuPaint(menu);
659 static void colectIconsCallback(WMenu *menu, WMenuEntry *entry)
661 WAppIcon *clickedIcon = (WAppIcon *) entry->clientdata;
662 WDock *clip; /* clip... is a WM_CLIP or a WM_DRAWER */
663 WAppIcon *aicon;
664 int x, y, x_pos, y_pos;
665 Bool update_icon = False;
667 assert(entry->clientdata != NULL);
668 clip = clickedIcon->dock;
670 aicon = clip->screen_ptr->app_icon_list;
672 while (aicon) {
673 if (!aicon->docked && wDockFindFreeSlot(clip, &x, &y)) {
674 x_pos = clip->x_pos + x * ICON_SIZE;
675 y_pos = clip->y_pos + y * ICON_SIZE;
676 if (aicon->x_pos != x_pos || aicon->y_pos != y_pos)
677 move_window(aicon->icon->core->window, aicon->x_pos, aicon->y_pos, x_pos, y_pos);
679 aicon->attracted = 1;
680 if (!aicon->icon->shadowed) {
681 aicon->icon->shadowed = 1;
682 update_icon = True;
684 wDockAttachIcon(clip, aicon, x, y, update_icon);
685 if (clip->collapsed || !clip->mapped)
686 XUnmapWindow(dpy, aicon->icon->core->window);
688 aicon = aicon->next;
692 static void selectIconsCallback(WMenu *menu, WMenuEntry *entry)
694 WAppIcon *clickedIcon = (WAppIcon *) entry->clientdata;
695 WDock *dock;
696 WMArray *selectedIcons;
697 WMArrayIterator iter;
698 WAppIcon *btn;
699 int i;
701 assert(clickedIcon != NULL);
702 dock = clickedIcon->dock;
704 selectedIcons = getSelected(dock);
706 if (!WMGetArrayItemCount(selectedIcons)) {
707 for (i = 1; i < dock->max_icons; i++) {
708 btn = dock->icon_array[i];
709 if (btn && !btn->icon->selected)
710 wIconSelect(btn->icon);
712 } else {
713 WM_ITERATE_ARRAY(selectedIcons, btn, iter) {
714 wIconSelect(btn->icon);
717 WMFreeArray(selectedIcons);
719 wMenuPaint(menu);
722 static void toggleCollapsedCallback(WMenu *menu, WMenuEntry *entry)
724 assert(entry->clientdata != NULL);
726 toggleCollapsed(entry->clientdata);
728 entry->flags.indicator_on = ((WDock *) entry->clientdata)->collapsed;
730 wMenuPaint(menu);
733 static void toggleAutoCollapseCallback(WMenu *menu, WMenuEntry *entry)
735 WDock *dock;
736 assert(entry->clientdata != NULL);
738 dock = (WDock *) entry->clientdata;
740 dock->auto_collapse = !dock->auto_collapse;
742 entry->flags.indicator_on = ((WDock *) entry->clientdata)->auto_collapse;
744 wMenuPaint(menu);
747 static void toggleAutoRaiseLower(WDock *dock)
749 WDrawerChain *dc;
751 dock->auto_raise_lower = !dock->auto_raise_lower;
752 if (dock->type == WM_DOCK)
754 for (dc = dock->screen_ptr->drawers; dc != NULL; dc = dc->next) {
755 toggleAutoRaiseLower(dc->adrawer);
760 static void toggleAutoRaiseLowerCallback(WMenu *menu, WMenuEntry *entry)
762 WDock *dock;
763 assert(entry->clientdata != NULL);
765 dock = (WDock *) entry->clientdata;
767 toggleAutoRaiseLower(dock);
769 entry->flags.indicator_on = ((WDock *) entry->clientdata)->auto_raise_lower;
771 wMenuPaint(menu);
774 static void launchCallback(WMenu *menu, WMenuEntry *entry)
776 WAppIcon *btn = (WAppIcon *) entry->clientdata;
778 launchDockedApplication(btn, False);
781 static void settingsCallback(WMenu *menu, WMenuEntry *entry)
783 WAppIcon *btn = (WAppIcon *) entry->clientdata;
785 if (btn->editing)
786 return;
787 ShowDockAppSettingsPanel(btn);
790 static void hideCallback(WMenu *menu, WMenuEntry *entry)
792 WApplication *wapp;
793 WAppIcon *btn = (WAppIcon *) entry->clientdata;
795 wapp = wApplicationOf(btn->icon->owner->main_window);
797 if (wapp->flags.hidden) {
798 wWorkspaceChange(btn->icon->core->screen_ptr, wapp->last_workspace);
799 wUnhideApplication(wapp, False, False);
800 } else {
801 wHideApplication(wapp);
805 static void unhideHereCallback(WMenu *menu, WMenuEntry *entry)
807 WApplication *wapp;
808 WAppIcon *btn = (WAppIcon *) entry->clientdata;
810 wapp = wApplicationOf(btn->icon->owner->main_window);
812 wUnhideApplication(wapp, False, True);
815 /* Name is only used when type == WM_DRAWER and when restoring a specific
816 * drawer, with a specific name. When creating a drawer, leave name to NULL
817 * and mainIconCreate will find the first unused unique name */
818 WAppIcon *mainIconCreate(WScreen *scr, int type, char *name)
820 WAppIcon *btn;
821 int x_pos;
823 switch(type) {
824 case WM_CLIP:
825 if (scr->clip_icon)
826 return scr->clip_icon;
827 btn = wAppIconCreateForDock(scr, NULL, "Logo", "WMClip", TILE_CLIP);
828 btn->icon->core->descriptor.handle_expose = clipIconExpose;
829 x_pos = 0;
830 break;
831 case WM_DOCK:
832 default: /* to avoid a warning about btn and x_pos, basically */
833 btn = wAppIconCreateForDock(scr, NULL, "Logo", "WMDock", TILE_NORMAL);
834 if (wPreferences.flags.clip_merged_in_dock)
835 btn->icon->core->descriptor.handle_expose = clipIconExpose;
836 x_pos = scr->scr_width - ICON_SIZE - DOCK_EXTRA_SPACE;
837 break;
838 case WM_DRAWER:
839 if (name == NULL)
840 name = findUniqueName(scr, "Drawer");
841 btn = wAppIconCreateForDock(scr, NULL, name, "WMDrawer", TILE_DRAWER);
842 btn->icon->core->descriptor.handle_expose = drawerIconExpose;
843 x_pos = 0;
846 btn->xindex = 0;
847 btn->yindex = 0;
849 btn->icon->core->descriptor.handle_mousedown = iconMouseDown;
850 btn->icon->core->descriptor.handle_enternotify = clipEnterNotify;
851 btn->icon->core->descriptor.handle_leavenotify = clipLeaveNotify;
852 btn->icon->core->descriptor.parent_type = WCLASS_DOCK_ICON;
853 btn->icon->core->descriptor.parent = btn;
854 XMapWindow(dpy, btn->icon->core->window);
855 btn->x_pos = x_pos;
856 btn->y_pos = 0;
857 btn->docked = 1;
858 if (type == WM_CLIP ||
859 (type == WM_DOCK && wPreferences.flags.clip_merged_in_dock))
860 scr->clip_icon = btn;
862 return btn;
865 static void switchWSCommand(WMenu *menu, WMenuEntry *entry)
867 WAppIcon *btn, *icon = (WAppIcon *) entry->clientdata;
868 WScreen *scr = icon->icon->core->screen_ptr;
869 WDock *src, *dest;
870 WMArray *selectedIcons;
871 int x, y;
873 if (entry->order == scr->current_workspace)
874 return;
875 src = icon->dock;
876 dest = scr->workspaces[entry->order]->clip;
878 selectedIcons = getSelected(src);
880 if (WMGetArrayItemCount(selectedIcons)) {
881 WMArrayIterator iter;
883 WM_ITERATE_ARRAY(selectedIcons, btn, iter) {
884 if (wDockFindFreeSlot(dest, &x, &y)) {
885 wDockMoveIconBetweenDocks(src, dest, btn, x, y);
886 XUnmapWindow(dpy, btn->icon->core->window);
889 } else if (icon != scr->clip_icon) {
890 if (wDockFindFreeSlot(dest, &x, &y)) {
891 wDockMoveIconBetweenDocks(src, dest, icon, x, y);
892 XUnmapWindow(dpy, icon->icon->core->window);
895 WMFreeArray(selectedIcons);
898 static void launchDockedApplication(WAppIcon *btn, Bool withSelection)
900 WScreen *scr = btn->icon->core->screen_ptr;
902 if (!btn->launching &&
903 ((!withSelection && btn->command != NULL) || (withSelection && btn->paste_command != NULL))) {
904 if (!btn->forced_dock) {
905 btn->relaunching = btn->running;
906 btn->running = 1;
908 if (btn->wm_instance || btn->wm_class) {
909 WWindowAttributes attr;
910 memset(&attr, 0, sizeof(WWindowAttributes));
911 wDefaultFillAttributes(btn->wm_instance, btn->wm_class, &attr, NULL, True);
913 if (!attr.no_appicon && !btn->buggy_app)
914 btn->launching = 1;
915 else
916 btn->running = 0;
918 btn->drop_launch = 0;
919 btn->paste_launch = withSelection;
920 scr->last_dock = btn->dock;
921 btn->pid = execCommand(btn, (withSelection ? btn->paste_command : btn->command), NULL);
922 if (btn->pid > 0) {
923 if (btn->buggy_app) {
924 /* give feedback that the app was launched */
925 btn->launching = 1;
926 dockIconPaint(btn);
927 btn->launching = 0;
928 WMAddTimerHandler(200, (WMCallback *) dockIconPaint, btn);
929 } else {
930 dockIconPaint(btn);
932 } else {
933 wwarning(_("could not launch application %s"), btn->command);
934 btn->launching = 0;
935 if (!btn->relaunching)
936 btn->running = 0;
941 static void updateWorkspaceMenu(WMenu *menu, WAppIcon *icon)
943 WScreen *scr = menu->frame->screen_ptr;
944 char title[MAX_WORKSPACENAME_WIDTH + 1];
945 int i;
947 if (!menu || !icon)
948 return;
950 for (i = 0; i < scr->workspace_count; i++) {
951 if (i < menu->entry_no) {
952 if (strcmp(menu->entries[i]->text, scr->workspaces[i]->name) != 0) {
953 wfree(menu->entries[i]->text);
954 strcpy(title, scr->workspaces[i]->name);
955 menu->entries[i]->text = wstrdup(title);
956 menu->flags.realized = 0;
958 menu->entries[i]->clientdata = (void *)icon;
959 } else {
960 strcpy(title, scr->workspaces[i]->name);
962 wMenuAddCallback(menu, title, switchWSCommand, (void *)icon);
964 menu->flags.realized = 0;
967 if (i == scr->current_workspace)
968 wMenuSetEnabled(menu, i, False);
969 else
970 wMenuSetEnabled(menu, i, True);
973 if (!menu->flags.realized)
974 wMenuRealize(menu);
977 static WMenu *makeWorkspaceMenu(WScreen *scr)
979 WMenu *menu;
981 menu = wMenuCreate(scr, NULL, False);
982 if (!menu)
983 wwarning(_("could not create workspace submenu for Clip menu"));
985 wMenuAddCallback(menu, "", switchWSCommand, (void *)scr->clip_icon);
987 menu->flags.realized = 0;
988 wMenuRealize(menu);
990 return menu;
993 static void updateClipOptionsMenu(WMenu *menu, WDock *dock)
995 WMenuEntry *entry;
996 int index = 0;
998 if (!menu || !dock)
999 return;
1001 /* keep on top */
1002 entry = menu->entries[index];
1003 entry->flags.indicator_on = !dock->lowered;
1004 entry->clientdata = dock;
1005 wMenuSetEnabled(menu, index, dock->type == WM_CLIP);
1007 /* collapsed */
1008 entry = menu->entries[++index];
1009 entry->flags.indicator_on = dock->collapsed;
1010 entry->clientdata = dock;
1012 /* auto-collapse */
1013 entry = menu->entries[++index];
1014 entry->flags.indicator_on = dock->auto_collapse;
1015 entry->clientdata = dock;
1017 /* auto-raise/lower */
1018 entry = menu->entries[++index];
1019 entry->flags.indicator_on = dock->auto_raise_lower;
1020 entry->clientdata = dock;
1021 wMenuSetEnabled(menu, index, dock->lowered && (dock->type == WM_CLIP));
1023 /* attract icons */
1024 entry = menu->entries[++index];
1025 entry->flags.indicator_on = dock->attract_icons;
1026 entry->clientdata = dock;
1028 menu->flags.realized = 0;
1029 wMenuRealize(menu);
1033 static WMenu *makeClipOptionsMenu(WScreen *scr)
1035 WMenu *menu;
1036 WMenuEntry *entry;
1038 menu = wMenuCreate(scr, NULL, False);
1039 if (!menu) {
1040 wwarning(_("could not create options submenu for Clip menu"));
1041 return NULL;
1044 entry = wMenuAddCallback(menu, _("Keep on Top"), toggleLoweredCallback, NULL);
1045 entry->flags.indicator = 1;
1046 entry->flags.indicator_on = 1;
1047 entry->flags.indicator_type = MI_CHECK;
1049 entry = wMenuAddCallback(menu, _("Collapsed"), toggleCollapsedCallback, NULL);
1050 entry->flags.indicator = 1;
1051 entry->flags.indicator_on = 1;
1052 entry->flags.indicator_type = MI_CHECK;
1054 entry = wMenuAddCallback(menu, _("Autocollapse"), toggleAutoCollapseCallback, NULL);
1055 entry->flags.indicator = 1;
1056 entry->flags.indicator_on = 1;
1057 entry->flags.indicator_type = MI_CHECK;
1059 entry = wMenuAddCallback(menu, _("Autoraise"), toggleAutoRaiseLowerCallback, NULL);
1060 entry->flags.indicator = 1;
1061 entry->flags.indicator_on = 1;
1062 entry->flags.indicator_type = MI_CHECK;
1064 entry = wMenuAddCallback(menu, _("Autoattract Icons"), toggleAutoAttractCallback, NULL);
1065 entry->flags.indicator = 1;
1066 entry->flags.indicator_on = 1;
1067 entry->flags.indicator_type = MI_CHECK;
1069 menu->flags.realized = 0;
1070 wMenuRealize(menu);
1072 return menu;
1076 static void setDockPositionNormalCallback(WMenu *menu, WMenuEntry *entry)
1078 WDock *dock = (WDock *) entry->clientdata;
1079 WDrawerChain *dc;
1080 if (entry->flags.indicator_on) // already set, nothing to do
1081 return;
1082 // Do we come from auto raise lower or keep on top?
1083 if (dock->auto_raise_lower)
1085 dock->auto_raise_lower = 0;
1086 // Only for aesthetic purposes, can be removed when Autoraise status is no longer exposed in drawer option menu
1087 for (dc = dock->screen_ptr->drawers; dc != NULL; dc = dc->next) {
1088 dc->adrawer->auto_raise_lower = 0;
1091 else
1093 // Will take care of setting lowered = 0 in drawers
1094 toggleLowered(dock);
1096 entry->flags.indicator_on = 1;
1099 static void setDockPositionAutoRaiseLowerCallback(WMenu *menu, WMenuEntry *entry)
1101 WDock *dock = (WDock *) entry->clientdata;
1102 WDrawerChain *dc;
1103 if (entry->flags.indicator_on) // already set, nothing to do
1104 return;
1105 // Do we come from normal or keep on top?
1106 if (!dock->lowered)
1108 toggleLowered(dock);
1110 dock->auto_raise_lower = 1;
1111 // Only for aesthetic purposes, can be removed when Autoraise status is no longer exposed in drawer option menu
1112 for (dc = dock->screen_ptr->drawers; dc != NULL; dc = dc->next) {
1113 dc->adrawer->auto_raise_lower = 1;
1115 entry->flags.indicator_on = 1;
1118 static void setDockPositionKeepOnTopCallback(WMenu *menu, WMenuEntry *entry)
1120 WDock *dock = (WDock *) entry->clientdata;
1121 WDrawerChain *dc;
1122 if (entry->flags.indicator_on) // already set, nothing to do
1123 return;
1124 dock->auto_raise_lower = 0;
1125 // Only for aesthetic purposes, can be removed when Autoraise status is no longer exposed in drawer option menu
1126 for (dc = dock->screen_ptr->drawers; dc != NULL; dc = dc->next) {
1127 dc->adrawer->auto_raise_lower = 0;
1129 toggleLowered(dock);
1130 entry->flags.indicator_on = 1;
1133 static void updateDockPositionMenu(WMenu *menu, WDock *dock)
1135 WMenuEntry *entry;
1136 int index = 0;
1138 assert(menu);
1139 assert(dock);
1141 /* Normal level */
1142 entry = menu->entries[index++];
1143 entry->flags.indicator_on = (dock->lowered && !dock->auto_raise_lower);
1144 entry->clientdata = dock;
1146 /* Auto-raise/lower */
1147 entry = menu->entries[index++];
1148 entry->flags.indicator_on = dock->auto_raise_lower;
1149 entry->clientdata = dock;
1151 /* Keep on top */
1152 entry = menu->entries[index++];
1153 entry->flags.indicator_on = !dock->lowered;
1154 entry->clientdata = dock;
1157 static WMenu *makeDockPositionMenu(WScreen *scr)
1159 /* When calling this, the dock is being created, so scr->dock is still not set
1160 * Therefore the callbacks' clientdata and the indicators can't be set,
1161 * they will be updated when the dock menu is opened. */
1162 WMenu *menu;
1163 WMenuEntry *entry;
1165 menu = wMenuCreate(scr, NULL, False);
1166 if (!menu) {
1167 wwarning(_("could not create options submenu for dock position menu"));
1168 return NULL;
1171 entry = wMenuAddCallback(menu, _("Normal"), setDockPositionNormalCallback, NULL);
1172 entry->flags.indicator = 1;
1173 entry->flags.indicator_type = MI_DIAMOND;
1175 entry = wMenuAddCallback(menu, _("Auto raise & lower"), setDockPositionAutoRaiseLowerCallback, NULL);
1176 entry->flags.indicator = 1;
1177 entry->flags.indicator_type = MI_DIAMOND;
1179 entry = wMenuAddCallback(menu, _("Keep on Top"), setDockPositionKeepOnTopCallback, NULL);
1180 entry->flags.indicator = 1;
1181 entry->flags.indicator_type = MI_DIAMOND;
1183 menu->flags.realized = 0;
1184 wMenuRealize(menu);
1186 return menu;
1190 static WMenu *dockMenuCreate(WScreen *scr, int type)
1192 WMenu *menu;
1193 WMenuEntry *entry;
1195 if (type == WM_CLIP && scr->clip_menu)
1196 return scr->clip_menu;
1198 if (type == WM_DRAWER && scr->drawer_menu)
1199 return scr->drawer_menu;
1201 menu = wMenuCreate(scr, NULL, False);
1202 if (type == WM_DOCK) {
1203 entry = wMenuAddCallback(menu, _("Dock position"), NULL, NULL);
1204 if (scr->dock_pos_menu == NULL)
1205 scr->dock_pos_menu = makeDockPositionMenu(scr);
1206 wMenuEntrySetCascade(menu, entry, scr->dock_pos_menu);
1208 if (!wPreferences.flags.nodrawer) {
1209 entry = wMenuAddCallback(menu, _("Add a drawer"), addADrawerCallback, NULL);
1211 } else {
1212 if (type == WM_CLIP)
1213 entry = wMenuAddCallback(menu, _("Clip Options"), NULL, NULL);
1214 else /* if (type == WM_DRAWER) */
1215 entry = wMenuAddCallback(menu, _("Drawer options"), NULL, NULL);
1217 if (scr->clip_options == NULL)
1218 scr->clip_options = makeClipOptionsMenu(scr);
1219 wMenuEntrySetCascade(menu, entry, scr->clip_options);
1221 /* The same menu is used for the dock and its appicons. If the menu
1222 * entry text is different between the two contexts, or if it can
1223 * change depending on some state, free the duplicated string (from
1224 * wMenuInsertCallback) and use gettext's string */
1225 if (type == WM_CLIP) {
1226 entry = wMenuAddCallback(menu, _("Rename Workspace"), renameCallback, NULL);
1227 wfree(entry->text);
1228 entry->text = _("Rename Workspace"); /* can be: (Toggle) Omnipresent */
1231 entry = wMenuAddCallback(menu, _("Selected"), selectCallback, NULL);
1232 entry->flags.indicator = 1;
1233 entry->flags.indicator_on = 1;
1234 entry->flags.indicator_type = MI_CHECK;
1236 entry = wMenuAddCallback(menu, _("Select All Icons"), selectIconsCallback, NULL);
1237 wfree(entry->text);
1238 entry->text = _("Select All Icons"); /* can be: Unselect all icons */
1240 entry = wMenuAddCallback(menu, _("Keep Icon"), keepIconsCallback, NULL);
1241 wfree(entry->text);
1242 entry->text = _("Keep Icon"); /* can be: Keep Icons */
1244 if (type == WM_CLIP) {
1245 entry = wMenuAddCallback(menu, _("Move Icon To"), NULL, NULL);
1246 wfree(entry->text);
1247 entry->text = _("Move Icon To"); /* can be: Move Icons to */
1248 scr->clip_submenu = makeWorkspaceMenu(scr);
1249 if (scr->clip_submenu)
1250 wMenuEntrySetCascade(menu, entry, scr->clip_submenu);
1253 entry = wMenuAddCallback(menu, _("Remove Icon"), removeIconsCallback, NULL);
1254 wfree(entry->text);
1255 entry->text = _("Remove Icon"); /* can be: Remove Icons */
1257 wMenuAddCallback(menu, _("Attract Icons"), colectIconsCallback, NULL);
1260 wMenuAddCallback(menu, _("Launch"), launchCallback, NULL);
1262 wMenuAddCallback(menu, _("Unhide Here"), unhideHereCallback, NULL);
1264 entry = wMenuAddCallback(menu, _("Hide"), hideCallback, NULL);
1265 wfree(entry->text);
1266 entry->text = _("Hide"); /* can be: Unhide */
1268 wMenuAddCallback(menu, _("Settings..."), settingsCallback, NULL);
1270 entry = wMenuAddCallback(menu, _("Kill"), killCallback, NULL);
1271 wfree(entry->text);
1272 entry->text = _("Kill"); /* can be: Remove drawer */
1274 if (type == WM_CLIP)
1275 scr->clip_menu = menu;
1277 if (type == WM_DRAWER)
1278 scr->drawer_menu = menu;
1280 return menu;
1283 WDock *wDockCreate(WScreen *scr, int type, char *name)
1285 WDock *dock;
1286 WAppIcon *btn;
1288 make_keys();
1290 dock = wmalloc(sizeof(WDock));
1292 switch (type) {
1293 case WM_CLIP:
1294 dock->max_icons = DOCK_MAX_ICONS;
1295 break;
1296 case WM_DRAWER:
1297 dock->max_icons = scr->scr_width / wPreferences.icon_size;
1298 break;
1299 case WM_DOCK:
1300 default:
1301 dock->max_icons = scr->scr_height / wPreferences.icon_size;
1304 dock->icon_array = wmalloc(sizeof(WAppIcon *) * dock->max_icons);
1306 btn = mainIconCreate(scr, type, name);
1308 btn->dock = dock;
1310 dock->x_pos = btn->x_pos;
1311 dock->y_pos = btn->y_pos;
1312 dock->screen_ptr = scr;
1313 dock->type = type;
1314 dock->icon_count = 1;
1315 if (type == WM_DRAWER)
1316 dock->on_right_side = scr->dock->on_right_side;
1317 else
1318 dock->on_right_side = 1;
1319 dock->collapsed = 0;
1320 dock->auto_collapse = 0;
1321 dock->auto_collapse_magic = NULL;
1322 dock->auto_raise_lower = 0;
1323 dock->auto_lower_magic = NULL;
1324 dock->auto_raise_magic = NULL;
1325 dock->attract_icons = 0;
1326 dock->lowered = 1;
1327 dock->icon_array[0] = btn;
1328 wRaiseFrame(btn->icon->core);
1329 XMoveWindow(dpy, btn->icon->core->window, btn->x_pos, btn->y_pos);
1331 /* create dock menu */
1332 dock->menu = dockMenuCreate(scr, type);
1334 if (type == WM_DRAWER) {
1335 drawerAppendToChain(scr, dock);
1336 dock->auto_collapse = 1;
1339 return dock;
1342 void wDockDestroy(WDock *dock)
1344 int i;
1345 WAppIcon *aicon;
1347 for (i = (dock->type == WM_CLIP) ? 1 : 0; i < dock->max_icons; i++) {
1348 aicon = dock->icon_array[i];
1349 if (aicon) {
1350 int keepit = aicon->running && wApplicationOf(aicon->main_window);
1351 wDockDetach(dock, aicon);
1352 if (keepit) {
1353 /* XXX: can: aicon->icon == NULL ? */
1354 PlaceIcon(dock->screen_ptr, &aicon->x_pos, &aicon->y_pos,
1355 wGetHeadForWindow(aicon->icon->owner));
1356 XMoveWindow(dpy, aicon->icon->core->window, aicon->x_pos, aicon->y_pos);
1357 if (!dock->mapped || dock->collapsed)
1358 XMapWindow(dpy, aicon->icon->core->window);
1362 if (wPreferences.auto_arrange_icons)
1363 wArrangeIcons(dock->screen_ptr, True);
1364 wfree(dock->icon_array);
1365 if (dock->menu && dock->type != WM_CLIP)
1366 wMenuDestroy(dock->menu, True);
1367 if (dock->screen_ptr->last_dock == dock)
1368 dock->screen_ptr->last_dock = NULL;
1369 wfree(dock);
1372 void wClipIconPaint(WAppIcon *aicon)
1374 WScreen *scr = aicon->icon->core->screen_ptr;
1375 WWorkspace *workspace = scr->workspaces[scr->current_workspace];
1376 WMColor *color;
1377 Window win = aicon->icon->core->window;
1378 int length, nlength;
1379 char *ws_name, ws_number[10];
1380 int ty, tx;
1382 wIconPaint(aicon->icon);
1384 length = strlen(workspace->name);
1385 ws_name = wmalloc(length + 1);
1386 snprintf(ws_name, length + 1, "%s", workspace->name);
1387 snprintf(ws_number, sizeof(ws_number), "%i", scr->current_workspace + 1);
1388 nlength = strlen(ws_number);
1390 if (wPreferences.flags.noclip || !workspace->clip->collapsed)
1391 color = scr->clip_title_color[CLIP_NORMAL];
1392 else
1393 color = scr->clip_title_color[CLIP_COLLAPSED];
1395 ty = ICON_SIZE - WMFontHeight(scr->clip_title_font) - 3;
1397 tx = CLIP_BUTTON_SIZE * ICON_SIZE / 64;
1399 if(wPreferences.show_clip_title)
1400 WMDrawString(scr->wmscreen, win, color, scr->clip_title_font, tx, ty, ws_name, length);
1402 tx = (ICON_SIZE / 2 - WMWidthOfString(scr->clip_title_font, ws_number, nlength)) / 2;
1404 WMDrawString(scr->wmscreen, win, color, scr->clip_title_font, tx, 2, ws_number, nlength);
1406 wfree(ws_name);
1408 if (aicon->launching)
1409 XFillRectangle(dpy, aicon->icon->core->window, scr->stipple_gc,
1410 0, 0, wPreferences.icon_size, wPreferences.icon_size);
1412 paintClipButtons(aicon, aicon->dock->lclip_button_pushed, aicon->dock->rclip_button_pushed);
1415 static void clipIconExpose(WObjDescriptor *desc, XEvent *event)
1417 wClipIconPaint(desc->parent);
1420 static void dockIconPaint(WAppIcon *btn)
1422 if (btn == btn->icon->core->screen_ptr->clip_icon) {
1423 wClipIconPaint(btn);
1424 } else if (wIsADrawer(btn->icon->core->screen_ptr, btn)) {
1425 wDrawerIconPaint(btn);
1426 } else {
1427 wAppIconPaint(btn);
1428 save_appicon(btn, True);
1432 static WMPropList *make_icon_state(WAppIcon *btn)
1434 WMPropList *node = NULL;
1435 WMPropList *command, *autolaunch, *lock, *name, *forced;
1436 WMPropList *position, *buggy, *omnipresent;
1437 char *tmp;
1438 char buffer[64];
1440 if (btn) {
1441 if (!btn->command)
1442 command = WMCreatePLString("-");
1443 else
1444 command = WMCreatePLString(btn->command);
1446 autolaunch = btn->auto_launch ? dYes : dNo;
1448 lock = btn->lock ? dYes : dNo;
1450 tmp = EscapeWM_CLASS(btn->wm_instance, btn->wm_class);
1452 name = WMCreatePLString(tmp);
1454 wfree(tmp);
1456 forced = btn->forced_dock ? dYes : dNo;
1458 buggy = btn->buggy_app ? dYes : dNo;
1460 if (!wPreferences.flags.clip_merged_in_dock && btn == btn->icon->core->screen_ptr->clip_icon)
1461 snprintf(buffer, sizeof(buffer), "%i,%i", btn->x_pos, btn->y_pos);
1462 else
1463 snprintf(buffer, sizeof(buffer), "%hi,%hi", btn->xindex, btn->yindex);
1464 position = WMCreatePLString(buffer);
1466 node = WMCreatePLDictionary(dCommand, command,
1467 dName, name,
1468 dAutoLaunch, autolaunch,
1469 dLock, lock,
1470 dForced, forced, dBuggyApplication, buggy, dPosition, position, NULL);
1471 WMReleasePropList(command);
1472 WMReleasePropList(name);
1473 WMReleasePropList(position);
1475 omnipresent = btn->omnipresent ? dYes : dNo;
1476 if (btn->dock != btn->icon->core->screen_ptr->dock && (btn->xindex != 0 || btn->yindex != 0))
1477 WMPutInPLDictionary(node, dOmnipresent, omnipresent);
1479 #ifdef XDND /* was OFFIX */
1480 if (btn->dnd_command) {
1481 command = WMCreatePLString(btn->dnd_command);
1482 WMPutInPLDictionary(node, dDropCommand, command);
1483 WMReleasePropList(command);
1485 #endif /* XDND */
1487 if (btn->paste_command) {
1488 command = WMCreatePLString(btn->paste_command);
1489 WMPutInPLDictionary(node, dPasteCommand, command);
1490 WMReleasePropList(command);
1494 return node;
1497 static WMPropList *dockSaveState(WDock *dock)
1499 int i;
1500 WMPropList *icon_info;
1501 WMPropList *list = NULL, *dock_state = NULL;
1502 WMPropList *value, *key;
1503 char buffer[256];
1505 list = WMCreatePLArray(NULL);
1507 for (i = (dock->type == WM_DOCK ? 0 : 1); i < dock->max_icons; i++) {
1508 WAppIcon *btn = dock->icon_array[i];
1510 if (!btn || btn->attracted)
1511 continue;
1513 if ((icon_info = make_icon_state(dock->icon_array[i]))) {
1514 WMAddToPLArray(list, icon_info);
1515 WMReleasePropList(icon_info);
1519 dock_state = WMCreatePLDictionary(dApplications, list, NULL);
1521 if (dock->type == WM_DOCK) {
1522 snprintf(buffer, sizeof(buffer), "Applications%i", dock->screen_ptr->scr_height);
1523 key = WMCreatePLString(buffer);
1524 WMPutInPLDictionary(dock_state, key, list);
1525 WMReleasePropList(key);
1527 snprintf(buffer, sizeof(buffer), "%i,%i", (dock->on_right_side ? -ICON_SIZE : 0), dock->y_pos);
1528 value = WMCreatePLString(buffer);
1529 WMPutInPLDictionary(dock_state, dPosition, value);
1530 WMReleasePropList(value);
1532 WMReleasePropList(list);
1534 if (dock->type == WM_CLIP || dock->type == WM_DRAWER) {
1535 value = (dock->collapsed ? dYes : dNo);
1536 WMPutInPLDictionary(dock_state, dCollapsed, value);
1538 value = (dock->auto_collapse ? dYes : dNo);
1539 WMPutInPLDictionary(dock_state, dAutoCollapse, value);
1541 value = (dock->attract_icons ? dYes : dNo);
1542 WMPutInPLDictionary(dock_state, dAutoAttractIcons, value);
1545 if (dock->type == WM_DOCK || dock->type == WM_CLIP) {
1546 value = (dock->lowered ? dYes : dNo);
1547 WMPutInPLDictionary(dock_state, dLowered, value);
1549 value = (dock->auto_raise_lower ? dYes : dNo);
1550 WMPutInPLDictionary(dock_state, dAutoRaiseLower, value);
1553 return dock_state;
1556 void wDockSaveState(WScreen *scr, WMPropList *old_state)
1558 WMPropList *dock_state;
1559 WMPropList *keys;
1561 dock_state = dockSaveState(scr->dock);
1564 * Copy saved states of docks with different sizes.
1566 if (old_state) {
1567 int i;
1568 WMPropList *tmp;
1570 keys = WMGetPLDictionaryKeys(old_state);
1571 for (i = 0; i < WMGetPropListItemCount(keys); i++) {
1572 tmp = WMGetFromPLArray(keys, i);
1574 if (strncasecmp(WMGetFromPLString(tmp), "applications", 12) == 0
1575 && !WMGetFromPLDictionary(dock_state, tmp)) {
1577 WMPutInPLDictionary(dock_state, tmp, WMGetFromPLDictionary(old_state, tmp));
1580 WMReleasePropList(keys);
1583 WMPutInPLDictionary(scr->session_state, dDock, dock_state);
1585 WMReleasePropList(dock_state);
1588 void wClipSaveState(WScreen *scr)
1590 WMPropList *clip_state;
1592 clip_state = make_icon_state(scr->clip_icon);
1594 WMPutInPLDictionary(scr->session_state, dClip, clip_state);
1596 WMReleasePropList(clip_state);
1599 WMPropList *wClipSaveWorkspaceState(WScreen *scr, int workspace)
1601 return dockSaveState(scr->workspaces[workspace]->clip);
1604 static Bool getBooleanDockValue(WMPropList *value, WMPropList *key)
1606 if (value) {
1607 if (WMIsPLString(value)) {
1608 if (strcasecmp(WMGetFromPLString(value), "YES") == 0)
1609 return True;
1610 } else {
1611 wwarning(_("bad value in docked icon state info %s"), WMGetFromPLString(key));
1614 return False;
1617 static WAppIcon *restore_icon_state(WScreen *scr, WMPropList *info, int type, int index)
1619 WAppIcon *aicon;
1620 WMPropList *cmd, *value;
1621 char *wclass, *winstance, *command;
1623 cmd = WMGetFromPLDictionary(info, dCommand);
1624 if (!cmd || !WMIsPLString(cmd))
1625 return NULL;
1627 /* parse window name */
1628 value = WMGetFromPLDictionary(info, dName);
1629 if (!value)
1630 return NULL;
1632 ParseWindowName(value, &winstance, &wclass, "dock");
1634 if (!winstance && !wclass)
1635 return NULL;
1637 /* get commands */
1638 if (cmd)
1639 command = wstrdup(WMGetFromPLString(cmd));
1640 else
1641 command = NULL;
1643 if (!command || strcmp(command, "-") == 0) {
1644 if (command)
1645 wfree(command);
1646 if (wclass)
1647 wfree(wclass);
1648 if (winstance)
1649 wfree(winstance);
1651 return NULL;
1654 aicon = wAppIconCreateForDock(scr, command, winstance, wclass, TILE_NORMAL);
1655 if (wclass)
1656 wfree(wclass);
1657 if (winstance)
1658 wfree(winstance);
1659 if (command)
1660 wfree(command);
1662 aicon->icon->core->descriptor.handle_mousedown = iconMouseDown;
1663 aicon->icon->core->descriptor.handle_enternotify = clipEnterNotify;
1664 aicon->icon->core->descriptor.handle_leavenotify = clipLeaveNotify;
1665 aicon->icon->core->descriptor.parent_type = WCLASS_DOCK_ICON;
1666 aicon->icon->core->descriptor.parent = aicon;
1668 #ifdef XDND /* was OFFIX */
1669 cmd = WMGetFromPLDictionary(info, dDropCommand);
1670 if (cmd)
1671 aicon->dnd_command = wstrdup(WMGetFromPLString(cmd));
1672 #endif
1674 cmd = WMGetFromPLDictionary(info, dPasteCommand);
1675 if (cmd)
1676 aicon->paste_command = wstrdup(WMGetFromPLString(cmd));
1678 /* check auto launch */
1679 value = WMGetFromPLDictionary(info, dAutoLaunch);
1681 aicon->auto_launch = getBooleanDockValue(value, dAutoLaunch);
1683 /* check lock */
1684 value = WMGetFromPLDictionary(info, dLock);
1686 aicon->lock = getBooleanDockValue(value, dLock);
1688 /* check if it wasn't normally docked */
1689 value = WMGetFromPLDictionary(info, dForced);
1691 aicon->forced_dock = getBooleanDockValue(value, dForced);
1693 /* check if we can rely on the stuff in the app */
1694 value = WMGetFromPLDictionary(info, dBuggyApplication);
1696 aicon->buggy_app = getBooleanDockValue(value, dBuggyApplication);
1698 /* get position in the dock */
1699 value = WMGetFromPLDictionary(info, dPosition);
1700 if (value && WMIsPLString(value)) {
1701 if (sscanf(WMGetFromPLString(value), "%hi,%hi", &aicon->xindex, &aicon->yindex) != 2)
1702 wwarning(_("bad value in docked icon state info %s"), WMGetFromPLString(dPosition));
1704 /* check position sanity */
1705 /* *Very* incomplete section! */
1706 if (type == WM_DOCK) {
1707 aicon->xindex = 0;
1709 } else {
1710 aicon->yindex = index;
1711 aicon->xindex = 0;
1714 /* check if icon is omnipresent */
1715 value = WMGetFromPLDictionary(info, dOmnipresent);
1717 aicon->omnipresent = getBooleanDockValue(value, dOmnipresent);
1719 aicon->running = 0;
1720 aicon->docked = 1;
1722 return aicon;
1725 #define COMPLAIN(key) wwarning(_("bad value in dock state info:%s"), key)
1727 WAppIcon *wClipRestoreState(WScreen *scr, WMPropList *clip_state)
1729 WAppIcon *icon;
1730 WMPropList *value;
1732 icon = mainIconCreate(scr, WM_CLIP, NULL);
1734 if (!clip_state)
1735 return icon;
1737 WMRetainPropList(clip_state);
1739 /* restore position */
1741 value = WMGetFromPLDictionary(clip_state, dPosition);
1743 if (value) {
1744 if (!WMIsPLString(value)) {
1745 COMPLAIN("Position");
1746 } else {
1747 if (sscanf(WMGetFromPLString(value), "%i,%i", &icon->x_pos, &icon->y_pos) != 2)
1748 COMPLAIN("Position");
1750 /* check position sanity */
1751 if (!onScreen(scr, icon->x_pos, icon->y_pos))
1752 wScreenKeepInside(scr, &icon->x_pos, &icon->y_pos, ICON_SIZE, ICON_SIZE);
1755 #ifdef XDND /* was OFFIX */
1756 value = WMGetFromPLDictionary(clip_state, dDropCommand);
1757 if (value && WMIsPLString(value))
1758 icon->dnd_command = wstrdup(WMGetFromPLString(value));
1759 #endif
1761 value = WMGetFromPLDictionary(clip_state, dPasteCommand);
1762 if (value && WMIsPLString(value))
1763 icon->paste_command = wstrdup(WMGetFromPLString(value));
1765 WMReleasePropList(clip_state);
1767 return icon;
1770 WDock *wDockRestoreState(WScreen *scr, WMPropList *dock_state, int type)
1772 WDock *dock;
1773 WMPropList *apps;
1774 WMPropList *value;
1775 WAppIcon *aicon, *old_top;
1776 int count, i;
1778 dock = wDockCreate(scr, type, NULL);
1780 if (!dock_state)
1781 return dock;
1783 WMRetainPropList(dock_state);
1785 /* restore position */
1786 value = WMGetFromPLDictionary(dock_state, dPosition);
1787 if (value) {
1788 if (!WMIsPLString(value)) {
1789 COMPLAIN("Position");
1790 } else {
1791 if (sscanf(WMGetFromPLString(value), "%i,%i", &dock->x_pos, &dock->y_pos) != 2)
1792 COMPLAIN("Position");
1794 /* check position sanity */
1795 if (!onScreen(scr, dock->x_pos, dock->y_pos)) {
1796 int x = dock->x_pos;
1797 wScreenKeepInside(scr, &x, &dock->y_pos, ICON_SIZE, ICON_SIZE);
1800 /* Is this needed any more? */
1801 if (type == WM_CLIP) {
1802 if (dock->x_pos < 0) {
1803 dock->x_pos = 0;
1804 } else if (dock->x_pos > scr->scr_width - ICON_SIZE) {
1805 dock->x_pos = scr->scr_width - ICON_SIZE;
1807 } else {
1808 if (dock->x_pos >= 0) {
1809 dock->x_pos = DOCK_EXTRA_SPACE;
1810 dock->on_right_side = 0;
1811 } else {
1812 dock->x_pos = scr->scr_width - DOCK_EXTRA_SPACE - ICON_SIZE;
1813 dock->on_right_side = 1;
1819 /* restore lowered/raised state */
1820 dock->lowered = 0;
1822 value = WMGetFromPLDictionary(dock_state, dLowered);
1823 if (value) {
1824 if (!WMIsPLString(value)) {
1825 COMPLAIN("Lowered");
1826 } else {
1827 if (strcasecmp(WMGetFromPLString(value), "YES") == 0)
1828 dock->lowered = 1;
1832 /* restore collapsed state */
1833 dock->collapsed = 0;
1835 value = WMGetFromPLDictionary(dock_state, dCollapsed);
1836 if (value) {
1837 if (!WMIsPLString(value)) {
1838 COMPLAIN("Collapsed");
1839 } else {
1840 if (strcasecmp(WMGetFromPLString(value), "YES") == 0)
1841 dock->collapsed = 1;
1845 /* restore auto-collapsed state */
1846 value = WMGetFromPLDictionary(dock_state, dAutoCollapse);
1847 if (value) {
1848 if (!WMIsPLString(value)) {
1849 COMPLAIN("AutoCollapse");
1850 } else {
1851 if (strcasecmp(WMGetFromPLString(value), "YES") == 0) {
1852 dock->auto_collapse = 1;
1853 dock->collapsed = 1;
1858 /* restore auto-raise/lower state */
1859 value = WMGetFromPLDictionary(dock_state, dAutoRaiseLower);
1860 if (value) {
1861 if (!WMIsPLString(value)) {
1862 COMPLAIN("AutoRaiseLower");
1863 } else {
1864 if (strcasecmp(WMGetFromPLString(value), "YES") == 0)
1865 dock->auto_raise_lower = 1;
1869 /* restore attract icons state */
1870 dock->attract_icons = 0;
1872 value = WMGetFromPLDictionary(dock_state, dAutoAttractIcons);
1873 if (value) {
1874 if (!WMIsPLString(value)) {
1875 COMPLAIN("AutoAttractIcons");
1876 } else {
1877 if (strcasecmp(WMGetFromPLString(value), "YES") == 0)
1878 dock->attract_icons = 1;
1882 /* application list */
1885 WMPropList *tmp;
1886 char buffer[64];
1889 * When saving, it saves the dock state in
1890 * Applications and Applicationsnnn
1892 * When loading, it will first try Applicationsnnn.
1893 * If it does not exist, use Applications as default.
1896 snprintf(buffer, sizeof(buffer), "Applications%i", scr->scr_height);
1898 tmp = WMCreatePLString(buffer);
1899 apps = WMGetFromPLDictionary(dock_state, tmp);
1900 WMReleasePropList(tmp);
1902 if (!apps)
1903 apps = WMGetFromPLDictionary(dock_state, dApplications);
1906 if (!apps)
1907 goto finish;
1909 count = WMGetPropListItemCount(apps);
1910 if (count == 0)
1911 goto finish;
1913 old_top = dock->icon_array[0];
1915 /* dock->icon_count is set to 1 when dock is created.
1916 * Since Clip is already restored, we want to keep it so for clip,
1917 * but for dock we may change the default top tile, so we set it to 0.
1919 if (type == WM_DOCK)
1920 dock->icon_count = 0;
1922 for (i = 0; i < count; i++) {
1923 if (dock->icon_count >= dock->max_icons) {
1924 wwarning(_("there are too many icons stored in dock. Ignoring what doesn't fit"));
1925 break;
1928 value = WMGetFromPLArray(apps, i);
1929 aicon = restore_icon_state(scr, value, type, dock->icon_count);
1931 dock->icon_array[dock->icon_count] = aicon;
1933 if (aicon) {
1934 aicon->dock = dock;
1935 aicon->x_pos = dock->x_pos + (aicon->xindex * ICON_SIZE);
1936 aicon->y_pos = dock->y_pos + (aicon->yindex * ICON_SIZE);
1938 if (dock->lowered)
1939 ChangeStackingLevel(aicon->icon->core, WMNormalLevel);
1940 else
1941 ChangeStackingLevel(aicon->icon->core, WMDockLevel);
1943 wCoreConfigure(aicon->icon->core, aicon->x_pos, aicon->y_pos, 0, 0);
1944 if (!dock->collapsed)
1945 XMapWindow(dpy, aicon->icon->core->window);
1947 wRaiseFrame(aicon->icon->core);
1949 dock->icon_count++;
1950 } else if (dock->icon_count == 0 && type == WM_DOCK) {
1951 dock->icon_count++;
1955 /* if the first icon is not defined, use the default */
1956 if (dock->icon_array[0] == NULL) {
1957 /* update default icon */
1958 old_top->x_pos = dock->x_pos;
1959 old_top->y_pos = dock->y_pos;
1960 if (dock->lowered)
1961 ChangeStackingLevel(old_top->icon->core, WMNormalLevel);
1962 else
1963 ChangeStackingLevel(old_top->icon->core, WMDockLevel);
1965 dock->icon_array[0] = old_top;
1966 XMoveWindow(dpy, old_top->icon->core->window, dock->x_pos, dock->y_pos);
1967 /* we don't need to increment dock->icon_count here because it was
1968 * incremented in the loop above.
1970 } else if (old_top != dock->icon_array[0]) {
1971 if (old_top == scr->clip_icon) // TODO dande: understand the logic
1972 scr->clip_icon = dock->icon_array[0];
1974 wAppIconDestroy(old_top);
1977 finish:
1978 WMReleasePropList(dock_state);
1980 return dock;
1983 void wDockLaunchWithState(WDock *dock, WAppIcon *btn, WSavedState *state)
1985 if (btn && btn->command && !btn->running && !btn->launching) {
1986 btn->drop_launch = 0;
1987 btn->paste_launch = 0;
1989 btn->pid = execCommand(btn, btn->command, state);
1991 if (btn->pid > 0) {
1992 if (!btn->forced_dock && !btn->buggy_app) {
1993 btn->launching = 1;
1994 dockIconPaint(btn);
1997 } else {
1998 wfree(state);
2002 void wDockDoAutoLaunch(WDock *dock, int workspace)
2004 WAppIcon *btn;
2005 WSavedState *state;
2006 int i;
2008 for (i = 0; i < dock->max_icons; i++) {
2009 btn = dock->icon_array[i];
2010 if (!btn || !btn->auto_launch)
2011 continue;
2013 state = wmalloc(sizeof(WSavedState));
2014 state->workspace = workspace;
2015 /* TODO: this is klugy and is very difficult to understand
2016 * what's going on. Try to clean up */
2017 wDockLaunchWithState(dock, btn, state);
2021 #ifdef XDND /* was OFFIX */
2022 static WDock *findDock(WScreen *scr, XEvent *event, int *icon_pos)
2024 WDock *dock;
2025 int i;
2027 *icon_pos = -1;
2028 if ((dock = scr->dock) != NULL) {
2029 for (i = 0; i < dock->max_icons; i++) {
2030 if (dock->icon_array[i]
2031 && dock->icon_array[i]->icon->core->window == event->xclient.window) {
2032 *icon_pos = i;
2033 break;
2037 if (*icon_pos < 0 && (dock = scr->workspaces[scr->current_workspace]->clip) != NULL) {
2038 for (i = 0; i < dock->max_icons; i++) {
2039 if (dock->icon_array[i]
2040 && dock->icon_array[i]->icon->core->window == event->xclient.window) {
2041 *icon_pos = i;
2042 break;
2046 if (*icon_pos >= 0)
2047 return dock;
2048 return NULL;
2051 int wDockReceiveDNDDrop(WScreen *scr, XEvent *event)
2053 WDock *dock;
2054 WAppIcon *btn;
2055 int icon_pos;
2057 dock = findDock(scr, event, &icon_pos);
2058 if (!dock)
2059 return False;
2062 * Return True if the drop was on an application icon window.
2063 * In this case, let the ClientMessage handler redirect the
2064 * message to the app.
2066 if (dock->icon_array[icon_pos]->icon->icon_win != None)
2067 return True;
2069 if (dock->icon_array[icon_pos]->dnd_command != NULL) {
2070 scr->flags.dnd_data_convertion_status = 0;
2072 btn = dock->icon_array[icon_pos];
2074 if (!btn->forced_dock) {
2075 btn->relaunching = btn->running;
2076 btn->running = 1;
2078 if (btn->wm_instance || btn->wm_class) {
2079 WWindowAttributes attr;
2080 memset(&attr, 0, sizeof(WWindowAttributes));
2081 wDefaultFillAttributes(btn->wm_instance, btn->wm_class, &attr, NULL, True);
2083 if (!attr.no_appicon)
2084 btn->launching = 1;
2085 else
2086 btn->running = 0;
2089 btn->paste_launch = 0;
2090 btn->drop_launch = 1;
2091 scr->last_dock = dock;
2092 btn->pid = execCommand(btn, btn->dnd_command, NULL);
2093 if (btn->pid > 0) {
2094 dockIconPaint(btn);
2095 } else {
2096 btn->launching = 0;
2097 if (!btn->relaunching)
2098 btn->running = 0;
2101 return False;
2103 #endif /* XDND */
2105 Bool wDockAttachIcon(WDock *dock, WAppIcon *icon, int x, int y, Bool update_icon)
2107 WWindow *wwin;
2108 Bool lupdate_icon = False;
2109 char *command = NULL;
2110 int index;
2112 icon->editing = 0;
2114 if (update_icon)
2115 lupdate_icon = True;
2117 if (icon->command == NULL) {
2118 /* If icon->owner exists, it means the application is running */
2119 if (icon->icon->owner) {
2120 wwin = icon->icon->owner;
2121 command = GetCommandForWindow(wwin->client_win);
2124 if (command) {
2125 icon->command = command;
2126 } else {
2127 /* icon->forced_dock = 1; */
2128 if (dock->type != WM_CLIP || !icon->attracted) {
2129 icon->editing = 1;
2130 if (wInputDialog(dock->screen_ptr, _("Dock Icon"),
2131 _("Type the command used to launch the application"), &command)) {
2132 if (command && (command[0] == 0 || (command[0] == '-' && command[1] == 0))) {
2133 wfree(command);
2134 command = NULL;
2136 icon->command = command;
2137 icon->editing = 0;
2138 } else {
2139 icon->editing = 0;
2140 if (command)
2141 wfree(command);
2142 /* If the target is the dock, reject the icon. If
2143 * the target is the clip, make it an attracted icon
2145 if (dock->type == WM_CLIP) {
2146 icon->attracted = 1;
2147 if (!icon->icon->shadowed) {
2148 icon->icon->shadowed = 1;
2149 lupdate_icon = True;
2151 } else {
2152 return False;
2159 for (index = 1; index < dock->max_icons; index++)
2160 if (dock->icon_array[index] == NULL)
2161 break;
2162 /* if (index == dock->max_icons)
2163 return; */
2165 assert(index < dock->max_icons);
2167 dock->icon_array[index] = icon;
2168 icon->yindex = y;
2169 icon->xindex = x;
2171 icon->omnipresent = 0;
2173 icon->x_pos = dock->x_pos + x * ICON_SIZE;
2174 icon->y_pos = dock->y_pos + y * ICON_SIZE;
2176 dock->icon_count++;
2178 icon->running = 1;
2179 icon->launching = 0;
2180 icon->docked = 1;
2181 icon->dock = dock;
2182 icon->icon->core->descriptor.handle_mousedown = iconMouseDown;
2183 icon->icon->core->descriptor.handle_enternotify = clipEnterNotify;
2184 icon->icon->core->descriptor.handle_leavenotify = clipLeaveNotify;
2185 icon->icon->core->descriptor.parent_type = WCLASS_DOCK_ICON;
2186 icon->icon->core->descriptor.parent = icon;
2188 MoveInStackListUnder(dock->icon_array[index - 1]->icon->core, icon->icon->core);
2189 wAppIconMove(icon, icon->x_pos, icon->y_pos);
2192 * Update icon pixmap, RImage doesn't change,
2193 * so call wIconUpdate is not needed
2195 if (lupdate_icon)
2196 update_icon_pixmap(icon->icon);
2198 /* Paint it */
2199 wAppIconPaint(icon);
2201 /* Save it */
2202 save_appicon(icon, True);
2204 if (wPreferences.auto_arrange_icons)
2205 wArrangeIcons(dock->screen_ptr, True);
2207 #ifdef XDND /* was OFFIX */
2208 if (icon->command && !icon->dnd_command) {
2209 int len = strlen(icon->command) + 8;
2210 icon->dnd_command = wmalloc(len);
2211 snprintf(icon->dnd_command, len, "%s %%d", icon->command);
2213 #endif
2215 if (icon->command && !icon->paste_command) {
2216 int len = strlen(icon->command) + 8;
2217 icon->paste_command = wmalloc(len);
2218 snprintf(icon->paste_command, len, "%s %%s", icon->command);
2221 return True;
2224 void wDockReattachIcon(WDock *dock, WAppIcon *icon, int x, int y)
2226 int index;
2228 for (index = 1; index < dock->max_icons; index++) {
2229 if (dock->icon_array[index] == icon)
2230 break;
2232 assert(index < dock->max_icons);
2234 icon->yindex = y;
2235 icon->xindex = x;
2237 icon->x_pos = dock->x_pos + x * ICON_SIZE;
2238 icon->y_pos = dock->y_pos + y * ICON_SIZE;
2241 Bool wDockMoveIconBetweenDocks(WDock *src, WDock *dest, WAppIcon *icon, int x, int y)
2243 WWindow *wwin;
2244 char *command = NULL;
2245 int index;
2246 Bool update_icon = False;
2248 if (src == dest)
2249 return True; /* No move needed, we're already there */
2251 if (dest == NULL)
2252 return False;
2255 * For the moment we can't do this if we move icons in Clip from one
2256 * workspace to other, because if we move two or more icons without
2257 * command, the dialog box will not be able to tell us to which of the
2258 * moved icons it applies. -Dan
2260 if ((dest->type == WM_DOCK /*|| dest->keep_attracted */ ) && icon->command == NULL) {
2261 /* If icon->owner exists, it means the application is running */
2262 if (icon->icon->owner) {
2263 wwin = icon->icon->owner;
2264 command = GetCommandForWindow(wwin->client_win);
2267 if (command) {
2268 icon->command = command;
2269 } else {
2270 icon->editing = 1;
2271 /* icon->forced_dock = 1; */
2272 if (wInputDialog(src->screen_ptr, _("Dock Icon"),
2273 _("Type the command used to launch the application"), &command)) {
2274 if (command && (command[0] == 0 || (command[0] == '-' && command[1] == 0))) {
2275 wfree(command);
2276 command = NULL;
2278 icon->command = command;
2279 } else {
2280 icon->editing = 0;
2281 if (command)
2282 wfree(command);
2283 return False;
2285 icon->editing = 0;
2289 if (dest->type == WM_DOCK || dest->type == WM_DRAWER)
2290 wClipMakeIconOmnipresent(icon, False);
2292 for (index = 1; index < src->max_icons; index++) {
2293 if (src->icon_array[index] == icon)
2294 break;
2296 assert(index < src->max_icons);
2298 src->icon_array[index] = NULL;
2299 src->icon_count--;
2301 for (index = 1; index < dest->max_icons; index++) {
2302 if (dest->icon_array[index] == NULL)
2303 break;
2306 assert(index < dest->max_icons);
2308 dest->icon_array[index] = icon;
2309 icon->dock = dest;
2311 /* deselect the icon */
2312 if (icon->icon->selected)
2313 wIconSelect(icon->icon);
2315 icon->icon->core->descriptor.handle_enternotify = clipEnterNotify;
2316 icon->icon->core->descriptor.handle_leavenotify = clipLeaveNotify;
2318 /* set it to be kept when moving to dock.
2319 * Unless the icon does not have a command set
2321 if (icon->command && (dest->type == WM_DOCK || dest->type == WM_DRAWER)) {
2322 icon->attracted = 0;
2323 if (icon->icon->shadowed) {
2324 icon->icon->shadowed = 0;
2325 update_icon = True;
2327 save_appicon(icon, True);
2330 if (src->auto_collapse || src->auto_raise_lower)
2331 clipLeave(src);
2333 icon->yindex = y;
2334 icon->xindex = x;
2336 icon->x_pos = dest->x_pos + x * ICON_SIZE;
2337 icon->y_pos = dest->y_pos + y * ICON_SIZE;
2339 dest->icon_count++;
2341 MoveInStackListUnder(dest->icon_array[index - 1]->icon->core, icon->icon->core);
2344 * Update icon pixmap, RImage doesn't change,
2345 * so call wIconUpdate is not needed
2347 if (update_icon)
2348 update_icon_pixmap(icon->icon);
2350 /* Paint it */
2351 wAppIconPaint(icon);
2353 return True;
2356 void wDockDetach(WDock *dock, WAppIcon *icon)
2358 int index;
2359 Bool update_icon = False;
2361 /* make the settings panel be closed */
2362 if (icon->panel)
2363 DestroyDockAppSettingsPanel(icon->panel);
2365 /* This must be called before icon->dock is set to NULL.
2366 * Don't move it. -Dan
2368 wClipMakeIconOmnipresent(icon, False);
2370 icon->docked = 0;
2371 icon->dock = NULL;
2372 icon->attracted = 0;
2373 icon->auto_launch = 0;
2374 if (icon->icon->shadowed) {
2375 icon->icon->shadowed = 0;
2376 update_icon = True;
2379 /* deselect the icon */
2380 if (icon->icon->selected)
2381 wIconSelect(icon->icon);
2383 if (icon->command) {
2384 wfree(icon->command);
2385 icon->command = NULL;
2387 #ifdef XDND /* was OFFIX */
2388 if (icon->dnd_command) {
2389 wfree(icon->dnd_command);
2390 icon->dnd_command = NULL;
2392 #endif
2393 if (icon->paste_command) {
2394 wfree(icon->paste_command);
2395 icon->paste_command = NULL;
2398 for (index = 1; index < dock->max_icons; index++)
2399 if (dock->icon_array[index] == icon)
2400 break;
2402 assert(index < dock->max_icons);
2403 dock->icon_array[index] = NULL;
2404 icon->yindex = -1;
2405 icon->xindex = -1;
2407 dock->icon_count--;
2409 /* if the dock is not attached to an application or
2410 * the application did not set the appropriate hints yet,
2411 * destroy the icon */
2412 if (!icon->running || !wApplicationOf(icon->main_window)) {
2413 wAppIconDestroy(icon);
2414 } else {
2415 icon->icon->core->descriptor.handle_mousedown = appIconMouseDown;
2416 icon->icon->core->descriptor.handle_enternotify = NULL;
2417 icon->icon->core->descriptor.handle_leavenotify = NULL;
2418 icon->icon->core->descriptor.parent_type = WCLASS_APPICON;
2419 icon->icon->core->descriptor.parent = icon;
2421 ChangeStackingLevel(icon->icon->core, NORMAL_ICON_LEVEL);
2424 * Update icon pixmap, RImage doesn't change,
2425 * so call wIconUpdate is not needed
2427 if (update_icon)
2428 update_icon_pixmap(icon->icon);
2430 /* Paint it */
2431 wAppIconPaint(icon);
2433 if (wPreferences.auto_arrange_icons)
2434 wArrangeIcons(dock->screen_ptr, True);
2436 if (dock->auto_collapse || dock->auto_raise_lower)
2437 clipLeave(dock);
2441 * returns the closest Dock slot index for the passed
2442 * coordinates.
2444 * Returns False if icon can't be docked.
2446 * Note: this function should NEVER alter ret_x or ret_y, unless it will
2447 * return True. -Dan
2449 /* Redocking == true means either icon->dock == dock (normal case)
2450 * or we are called from handleDockMove for a drawer */
2451 Bool wDockSnapIcon(WDock *dock, WAppIcon *icon, int req_x, int req_y, int *ret_x, int *ret_y, int redocking)
2453 WScreen *scr = dock->screen_ptr;
2454 int dx, dy;
2455 int ex_x, ex_y;
2456 int i, offset = ICON_SIZE / 2;
2457 WAppIcon *aicon = NULL;
2458 WAppIcon *nicon = NULL;
2460 if (wPreferences.flags.noupdates)
2461 return False;
2463 dx = dock->x_pos;
2464 dy = dock->y_pos;
2466 /* if the dock is full */
2467 if (!redocking && (dock->icon_count >= dock->max_icons))
2468 return False;
2470 /* exact position */
2471 if (req_y < dy)
2472 ex_y = (req_y - offset - dy) / ICON_SIZE;
2473 else
2474 ex_y = (req_y + offset - dy) / ICON_SIZE;
2476 if (req_x < dx)
2477 ex_x = (req_x - offset - dx) / ICON_SIZE;
2478 else
2479 ex_x = (req_x + offset - dx) / ICON_SIZE;
2481 /* check if the icon is outside the screen boundaries */
2482 if (!onScreen(scr, dx + ex_x * ICON_SIZE, dy + ex_y * ICON_SIZE))
2483 return False;
2485 switch (dock->type) {
2486 case WM_DOCK:
2487 /* We can return False right away if
2488 * - we do not come from this dock (which is a WM_DOCK),
2489 * - we are not right over it, and
2490 * - we are not the main tile of a drawer.
2491 * In the latter case, we are called from handleDockMove. */
2492 if (icon->dock != dock && ex_x != 0 &&
2493 !(icon->dock && icon->dock->type == WM_DRAWER && icon == icon->dock->icon_array[0]))
2494 return False;
2496 if (!redocking && ex_x != 0)
2497 return False;
2499 if (getDrawer(scr, ex_y)) /* Return false so that the drawer gets it. */
2500 return False;
2502 aicon = NULL;
2503 for (i = 0; i < dock->max_icons; i++) {
2504 nicon = dock->icon_array[i];
2505 if (nicon && nicon->yindex == ex_y) {
2506 aicon = nicon;
2507 break;
2511 if (redocking) {
2512 int sig, done, closest;
2514 /* Possible cases when redocking:
2516 * icon dragged out of range of any slot -> false
2517 * icon dragged on a drawer -> false (to open the drawer)
2518 * icon dragged to range of free slot
2519 * icon dragged to range of same slot
2520 * icon dragged to range of different icon
2522 if (abs(ex_x) > DOCK_DETTACH_THRESHOLD)
2523 return False;
2525 if (aicon == icon || !aicon) {
2526 *ret_x = 0;
2527 *ret_y = ex_y;
2528 return True;
2531 /* start looking at the upper slot or lower? */
2532 if (ex_y * ICON_SIZE < (req_y + offset - dy))
2533 sig = 1;
2534 else
2535 sig = -1;
2537 done = 0;
2538 /* look for closest free slot */
2539 for (i = 0; i < (DOCK_DETTACH_THRESHOLD + 1) * 2 && !done; i++) {
2540 int j;
2542 done = 1;
2543 closest = sig * (i / 2) + ex_y;
2544 /* check if this slot is fully on the screen and not used */
2545 if (onScreen(scr, dx, dy + closest * ICON_SIZE)) {
2546 for (j = 0; j < dock->max_icons; j++) {
2547 if (dock->icon_array[j]
2548 && dock->icon_array[j]->yindex == closest) {
2549 /* slot is used by someone else */
2550 if (dock->icon_array[j] != icon)
2551 done = 0;
2552 break;
2555 /* slot is used by a drawer */
2556 done = done && !getDrawer(scr, closest);
2558 else // !onScreen
2559 done = 0;
2560 sig = -sig;
2562 if (done &&
2563 ((ex_y >= closest && ex_y - closest < DOCK_DETTACH_THRESHOLD + 1)
2564 || (ex_y < closest && closest - ex_y <= DOCK_DETTACH_THRESHOLD + 1))) {
2565 *ret_x = 0;
2566 *ret_y = closest;
2567 return True;
2569 } else { /* !redocking */
2571 /* if slot is free and the icon is close enough, return it */
2572 if (!aicon && ex_x == 0) {
2573 *ret_x = 0;
2574 *ret_y = ex_y;
2575 return True;
2578 break;
2579 case WM_CLIP:
2581 int neighbours = 0;
2582 int start, stop, k;
2584 start = icon->omnipresent ? 0 : scr->current_workspace;
2585 stop = icon->omnipresent ? scr->workspace_count : start + 1;
2587 aicon = NULL;
2588 for (k = start; k < stop; k++) {
2589 WDock *tmp = scr->workspaces[k]->clip;
2590 if (!tmp)
2591 continue;
2592 for (i = 0; i < tmp->max_icons; i++) {
2593 nicon = tmp->icon_array[i];
2594 if (nicon && nicon->xindex == ex_x && nicon->yindex == ex_y) {
2595 aicon = nicon;
2596 break;
2599 if (aicon)
2600 break;
2602 for (k = start; k < stop; k++) {
2603 WDock *tmp = scr->workspaces[k]->clip;
2604 if (!tmp)
2605 continue;
2606 for (i = 0; i < tmp->max_icons; i++) {
2607 nicon = tmp->icon_array[i];
2608 if (nicon && nicon != icon && /* Icon can't be it's own neighbour */
2609 (abs(nicon->xindex - ex_x) <= CLIP_ATTACH_VICINITY &&
2610 abs(nicon->yindex - ex_y) <= CLIP_ATTACH_VICINITY)) {
2611 neighbours = 1;
2612 break;
2615 if (neighbours)
2616 break;
2619 if (neighbours && (aicon == NULL || (redocking && aicon == icon))) {
2620 *ret_x = ex_x;
2621 *ret_y = ex_y;
2622 return True;
2624 break;
2626 case WM_DRAWER:
2628 WAppIcon *aicons_to_shift[ dock->icon_count ];
2629 int index_of_hole, j;
2631 if (ex_y != 0 ||
2632 abs(ex_x) - dock->icon_count > DOCK_DETTACH_THRESHOLD ||
2633 (ex_x < 0 && !dock->on_right_side) ||
2634 (ex_x > 0 && dock->on_right_side)) {
2635 return False;
2638 if (ex_x == 0)
2639 ex_x = (dock->on_right_side ? -1 : 1);
2641 /* "Reduce" ex_x but keep its sign */
2642 if (redocking) {
2643 if (abs(ex_x) > dock->icon_count - 1) /* minus 1: do not take icon_array[0] into account */
2644 ex_x = ex_x * (dock->icon_count - 1) / abs(ex_x); /* don't use *= ! */
2645 } else {
2646 if (abs(ex_x) > dock->icon_count)
2647 ex_x = ex_x * dock->icon_count / abs(ex_x);
2649 index_of_hole = indexOfHole(dock, icon, redocking);
2651 /* Find the appicons between where icon was (index_of_hole) and where
2652 * it wants to be (ex_x) and slide them. */
2653 j = 0;
2654 for (i = 1; i < dock->max_icons; i++) {
2655 if ((aicon = dock->icon_array[ i ]) && aicon != icon &&
2656 ((ex_x <= aicon->xindex && aicon->xindex < index_of_hole) ||
2657 (index_of_hole < aicon->xindex && aicon->xindex <= ex_x)))
2658 aicons_to_shift[ j++ ] = aicon;
2660 assert(j == abs(ex_x - index_of_hole));
2662 wSlideAppicons(aicons_to_shift, j, (index_of_hole < ex_x));
2664 *ret_x = ex_x;
2665 *ret_y = ex_y;
2666 return True;
2669 return False;
2672 static int onScreen(WScreen *scr, int x, int y)
2674 WMRect rect;
2675 int flags;
2677 rect.pos.x = x;
2678 rect.pos.y = y;
2679 rect.size.width = rect.size.height = ICON_SIZE;
2681 wGetRectPlacementInfo(scr, rect, &flags);
2683 return !(flags & (XFLAG_DEAD | XFLAG_PARTIAL));
2687 * returns true if it can find a free slot in the dock,
2688 * in which case it changes x_pos and y_pos accordingly.
2689 * Else returns false.
2691 Bool wDockFindFreeSlot(WDock *dock, int *x_pos, int *y_pos)
2693 WScreen *scr = dock->screen_ptr;
2694 WAppIcon *btn;
2695 WAppIconChain *chain;
2696 unsigned char *slot_map;
2697 int mwidth;
2698 int r;
2699 int x, y;
2700 int i, done = False;
2701 int corner;
2702 int sx = 0, ex = scr->scr_width, ey = scr->scr_height;
2703 int extra_count = 0;
2705 if (dock->type == WM_DRAWER) {
2706 if (dock->icon_count >= dock->max_icons) { /* drawer is full */
2707 return False;
2709 *x_pos = dock->icon_count * (dock->on_right_side ? -1 : 1);
2710 *y_pos = 0;
2711 return True;
2714 if (dock->type == WM_CLIP && dock != scr->workspaces[scr->current_workspace]->clip)
2715 extra_count = scr->global_icon_count;
2717 /* if the dock is full */
2718 if (dock->icon_count + extra_count >= dock->max_icons)
2719 return False;
2721 if (!wPreferences.flags.nodock && scr->dock) {
2722 if (scr->dock->on_right_side)
2723 ex -= ICON_SIZE + DOCK_EXTRA_SPACE;
2724 else
2725 sx += ICON_SIZE + DOCK_EXTRA_SPACE;
2728 if (ex < dock->x_pos)
2729 ex = dock->x_pos;
2730 if (sx > dock->x_pos + ICON_SIZE)
2731 sx = dock->x_pos + ICON_SIZE;
2732 #define C_NONE 0
2733 #define C_NW 1
2734 #define C_NE 2
2735 #define C_SW 3
2736 #define C_SE 4
2738 /* check if clip is in a corner */
2739 if (dock->type == WM_CLIP) {
2740 if (dock->x_pos < 1 && dock->y_pos < 1)
2741 corner = C_NE;
2742 else if (dock->x_pos < 1 && dock->y_pos >= (ey - ICON_SIZE))
2743 corner = C_SE;
2744 else if (dock->x_pos >= (ex - ICON_SIZE) && dock->y_pos >= (ey - ICON_SIZE))
2745 corner = C_SW;
2746 else if (dock->x_pos >= (ex - ICON_SIZE) && dock->y_pos < 1)
2747 corner = C_NW;
2748 else
2749 corner = C_NONE;
2750 } else {
2751 corner = C_NONE;
2754 /* If the clip is in the corner, use only slots that are in the border
2755 * of the screen */
2756 if (corner != C_NONE) {
2757 char *hmap, *vmap;
2758 int hcount, vcount;
2760 hcount = WMIN(dock->max_icons, scr->scr_width / ICON_SIZE);
2761 vcount = WMIN(dock->max_icons, scr->scr_height / ICON_SIZE);
2762 hmap = wmalloc(hcount + 1);
2763 vmap = wmalloc(vcount + 1);
2765 /* mark used positions */
2766 switch (corner) {
2767 case C_NE:
2768 for (i = 0; i < dock->max_icons; i++) {
2769 btn = dock->icon_array[i];
2770 if (!btn)
2771 continue;
2773 if (btn->xindex == 0 && btn->yindex > 0 && btn->yindex < vcount)
2774 vmap[btn->yindex] = 1;
2775 else if (btn->yindex == 0 && btn->xindex > 0 && btn->xindex < hcount)
2776 hmap[btn->xindex] = 1;
2778 for (chain = scr->global_icons; chain != NULL; chain = chain->next) {
2779 btn = chain->aicon;
2780 if (btn->xindex == 0 && btn->yindex > 0 && btn->yindex < vcount)
2781 vmap[btn->yindex] = 1;
2782 else if (btn->yindex == 0 && btn->xindex > 0 && btn->xindex < hcount)
2783 hmap[btn->xindex] = 1;
2785 break;
2786 case C_NW:
2787 for (i = 0; i < dock->max_icons; i++) {
2788 btn = dock->icon_array[i];
2789 if (!btn)
2790 continue;
2792 if (btn->xindex == 0 && btn->yindex > 0 && btn->yindex < vcount)
2793 vmap[btn->yindex] = 1;
2794 else if (btn->yindex == 0 && btn->xindex < 0 && btn->xindex > -hcount)
2795 hmap[-btn->xindex] = 1;
2797 for (chain = scr->global_icons; chain != NULL; chain = chain->next) {
2798 btn = chain->aicon;
2799 if (btn->xindex == 0 && btn->yindex > 0 && btn->yindex < vcount)
2800 vmap[btn->yindex] = 1;
2801 else if (btn->yindex == 0 && btn->xindex < 0 && btn->xindex > -hcount)
2802 hmap[-btn->xindex] = 1;
2804 break;
2805 case C_SE:
2806 for (i = 0; i < dock->max_icons; i++) {
2807 btn = dock->icon_array[i];
2808 if (!btn)
2809 continue;
2811 if (btn->xindex == 0 && btn->yindex < 0 && btn->yindex > -vcount)
2812 vmap[-btn->yindex] = 1;
2813 else if (btn->yindex == 0 && btn->xindex > 0 && btn->xindex < hcount)
2814 hmap[btn->xindex] = 1;
2816 for (chain = scr->global_icons; chain != NULL; chain = chain->next) {
2817 btn = chain->aicon;
2818 if (btn->xindex == 0 && btn->yindex < 0 && btn->yindex > -vcount)
2819 vmap[-btn->yindex] = 1;
2820 else if (btn->yindex == 0 && btn->xindex > 0 && btn->xindex < hcount)
2821 hmap[btn->xindex] = 1;
2823 break;
2824 case C_SW:
2825 default:
2826 for (i = 0; i < dock->max_icons; i++) {
2827 btn = dock->icon_array[i];
2828 if (!btn)
2829 continue;
2831 if (btn->xindex == 0 && btn->yindex < 0 && btn->yindex > -vcount)
2832 vmap[-btn->yindex] = 1;
2833 else if (btn->yindex == 0 && btn->xindex < 0 && btn->xindex > -hcount)
2834 hmap[-btn->xindex] = 1;
2836 for (chain = scr->global_icons; chain != NULL; chain = chain->next) {
2837 btn = chain->aicon;
2838 if (btn->xindex == 0 && btn->yindex < 0 && btn->yindex > -vcount)
2839 vmap[-btn->yindex] = 1;
2840 else if (btn->yindex == 0 && btn->xindex < 0 && btn->xindex > -hcount)
2841 hmap[-btn->xindex] = 1;
2844 x = 0;
2845 y = 0;
2846 done = 0;
2847 /* search a vacant slot */
2848 for (i = 1; i < WMAX(vcount, hcount); i++) {
2849 if (i < vcount && vmap[i] == 0) {
2850 /* found a slot */
2851 x = 0;
2852 y = i;
2853 done = 1;
2854 break;
2855 } else if (i < hcount && hmap[i] == 0) {
2856 /* found a slot */
2857 x = i;
2858 y = 0;
2859 done = 1;
2860 break;
2863 wfree(vmap);
2864 wfree(hmap);
2865 /* If found a slot, translate and return */
2866 if (done) {
2867 if (corner == C_NW || corner == C_NE)
2868 *y_pos = y;
2869 else
2870 *y_pos = -y;
2872 if (corner == C_NE || corner == C_SE)
2873 *x_pos = x;
2874 else
2875 *x_pos = -x;
2877 return True;
2879 /* else, try to find a slot somewhere else */
2882 /* a map of mwidth x mwidth would be enough if we allowed icons to be
2883 * placed outside of screen */
2884 mwidth = (int)ceil(sqrt(dock->max_icons));
2886 /* In the worst case (the clip is in the corner of the screen),
2887 * the amount of icons that fit in the clip is smaller.
2888 * Double the map to get a safe value.
2890 mwidth += mwidth;
2892 r = (mwidth - 1) / 2;
2894 slot_map = wmalloc(mwidth * mwidth);
2896 #define XY2OFS(x,y) (WMAX(abs(x),abs(y)) > r) ? 0 : (((y)+r)*(mwidth)+(x)+r)
2898 /* mark used slots in the map. If the slot falls outside the map
2899 * (for example, when all icons are placed in line), ignore them. */
2900 for (i = 0; i < dock->max_icons; i++) {
2901 btn = dock->icon_array[i];
2902 if (btn)
2903 slot_map[XY2OFS(btn->xindex, btn->yindex)] = 1;
2906 for (chain = scr->global_icons; chain != NULL; chain = chain->next)
2907 slot_map[XY2OFS(chain->aicon->xindex, chain->aicon->yindex)] = 1;
2909 /* Find closest slot from the center that is free by scanning the
2910 * map from the center to outward in circular passes.
2911 * This will not result in a neat layout, but will be optimal
2912 * in the sense that there will not be holes left.
2914 done = 0;
2915 for (i = 1; i <= r && !done; i++) {
2916 int tx, ty;
2918 /* top and bottom parts of the ring */
2919 for (x = -i; x <= i && !done; x++) {
2920 tx = dock->x_pos + x * ICON_SIZE;
2921 y = -i;
2922 ty = dock->y_pos + y * ICON_SIZE;
2923 if (slot_map[XY2OFS(x, y)] == 0 && onScreen(scr, tx, ty)) {
2924 *x_pos = x;
2925 *y_pos = y;
2926 done = 1;
2927 break;
2929 y = i;
2930 ty = dock->y_pos + y * ICON_SIZE;
2931 if (slot_map[XY2OFS(x, y)] == 0 && onScreen(scr, tx, ty)) {
2932 *x_pos = x;
2933 *y_pos = y;
2934 done = 1;
2935 break;
2938 /* left and right parts of the ring */
2939 for (y = -i + 1; y <= i - 1; y++) {
2940 ty = dock->y_pos + y * ICON_SIZE;
2941 x = -i;
2942 tx = dock->x_pos + x * ICON_SIZE;
2943 if (slot_map[XY2OFS(x, y)] == 0 && onScreen(scr, tx, ty)) {
2944 *x_pos = x;
2945 *y_pos = y;
2946 done = 1;
2947 break;
2949 x = i;
2950 tx = dock->x_pos + x * ICON_SIZE;
2951 if (slot_map[XY2OFS(x, y)] == 0 && onScreen(scr, tx, ty)) {
2952 *x_pos = x;
2953 *y_pos = y;
2954 done = 1;
2955 break;
2959 wfree(slot_map);
2960 #undef XY2OFS
2961 return done;
2964 static void moveDock(WDock *dock, int new_x, int new_y)
2966 WAppIcon *btn;
2967 WDrawerChain *dc;
2968 int i;
2970 if (dock->type == WM_DOCK) {
2971 for (dc = dock->screen_ptr->drawers; dc != NULL; dc = dc->next)
2972 moveDock(dc->adrawer, new_x, dc->adrawer->y_pos - dock->y_pos + new_y);
2975 dock->x_pos = new_x;
2976 dock->y_pos = new_y;
2977 for (i = 0; i < dock->max_icons; i++) {
2978 btn = dock->icon_array[i];
2979 if (btn) {
2980 btn->x_pos = new_x + btn->xindex * ICON_SIZE;
2981 btn->y_pos = new_y + btn->yindex * ICON_SIZE;
2982 XMoveWindow(dpy, btn->icon->core->window, btn->x_pos, btn->y_pos);
2987 static void swapDock(WDock *dock)
2989 WScreen *scr = dock->screen_ptr;
2990 WAppIcon *btn;
2991 int x, i;
2993 if (dock->on_right_side)
2994 x = dock->x_pos = scr->scr_width - ICON_SIZE - DOCK_EXTRA_SPACE;
2995 else
2996 x = dock->x_pos = DOCK_EXTRA_SPACE;
2998 swapDrawers(scr, dock->on_right_side, x);
3000 for (i = 0; i < dock->max_icons; i++) {
3001 btn = dock->icon_array[i];
3002 if (btn) {
3003 btn->x_pos = x;
3004 XMoveWindow(dpy, btn->icon->core->window, btn->x_pos, btn->y_pos);
3008 wScreenUpdateUsableArea(scr);
3011 static pid_t execCommand(WAppIcon *btn, char *command, WSavedState *state)
3013 WScreen *scr = btn->icon->core->screen_ptr;
3014 pid_t pid;
3015 char **argv;
3016 int argc;
3017 char *cmdline;
3019 cmdline = ExpandOptions(scr, command);
3021 if (scr->flags.dnd_data_convertion_status || !cmdline) {
3022 if (cmdline)
3023 wfree(cmdline);
3024 if (state)
3025 wfree(state);
3026 return 0;
3029 wtokensplit(cmdline, &argv, &argc);
3031 if (!argc) {
3032 if (cmdline)
3033 wfree(cmdline);
3034 if (state)
3035 wfree(state);
3036 return 0;
3039 if ((pid = fork()) == 0) {
3040 char **args;
3041 int i;
3043 SetupEnvironment(scr);
3045 #ifdef HAVE_SETSID
3046 setsid();
3047 #endif
3049 args = malloc(sizeof(char *) * (argc + 1));
3050 if (!args)
3051 exit(111);
3053 for (i = 0; i < argc; i++)
3054 args[i] = argv[i];
3056 args[argc] = NULL;
3057 execvp(argv[0], args);
3058 exit(111);
3060 wtokenfree(argv, argc);
3062 if (pid > 0) {
3063 if (!state) {
3064 state = wmalloc(sizeof(WSavedState));
3065 state->hidden = -1;
3066 state->miniaturized = -1;
3067 state->shaded = -1;
3068 if (btn->dock == scr->dock || btn->dock->type == WM_DRAWER || btn->omnipresent)
3069 state->workspace = -1;
3070 else
3071 state->workspace = scr->current_workspace;
3073 wWindowAddSavedState(btn->wm_instance, btn->wm_class, cmdline, pid, state);
3074 wAddDeathHandler(pid, (WDeathHandler *) trackDeadProcess, btn->dock);
3075 } else if (state) {
3076 wfree(state);
3078 wfree(cmdline);
3079 return pid;
3082 void wDockHideIcons(WDock *dock)
3084 int i;
3086 if (dock == NULL)
3087 return;
3089 for (i = 1; i < dock->max_icons; i++) {
3090 if (dock->icon_array[i])
3091 XUnmapWindow(dpy, dock->icon_array[i]->icon->core->window);
3093 dock->mapped = 0;
3095 dockIconPaint(dock->icon_array[0]);
3098 void wDockShowIcons(WDock *dock)
3100 int i;
3101 WAppIcon *btn;
3103 if (dock == NULL)
3104 return;
3106 btn = dock->icon_array[0];
3107 moveDock(dock, btn->x_pos, btn->y_pos);
3109 /* Deleting any change in stacking level, this function is now only about
3110 mapping icons */
3112 if (!dock->collapsed) {
3113 for (i = 1; i < dock->max_icons; i++) {
3114 if (dock->icon_array[i])
3115 XMapWindow(dpy, dock->icon_array[i]->icon->core->window);
3118 dock->mapped = 1;
3120 dockIconPaint(btn);
3123 void wDockLower(WDock *dock)
3125 int i;
3126 WDrawerChain *dc;
3128 if (dock->type == WM_DOCK) {
3129 for (dc = dock->screen_ptr->drawers; dc != NULL; dc = dc->next)
3130 wDockLower(dc->adrawer);
3132 for (i = 0; i < dock->max_icons; i++) {
3133 if (dock->icon_array[i])
3134 wLowerFrame(dock->icon_array[i]->icon->core);
3138 void wDockRaise(WDock *dock)
3140 int i;
3141 WDrawerChain *dc;
3143 for (i = dock->max_icons - 1; i >= 0; i--) {
3144 if (dock->icon_array[i])
3145 wRaiseFrame(dock->icon_array[i]->icon->core);
3147 if (dock->type == WM_DOCK) {
3148 for (dc = dock->screen_ptr->drawers; dc != NULL; dc = dc->next)
3149 wDockRaise(dc->adrawer);
3153 void wDockRaiseLower(WDock *dock)
3155 if (!dock->icon_array[0]->icon->core->stacking->above
3156 || (dock->icon_array[0]->icon->core->stacking->window_level
3157 != dock->icon_array[0]->icon->core->stacking->above->stacking->window_level))
3158 wDockLower(dock);
3159 else
3160 wDockRaise(dock);
3163 void wDockFinishLaunch(WDock *dock, WAppIcon *icon)
3165 icon->launching = 0;
3166 icon->relaunching = 0;
3167 dockIconPaint(icon);
3170 WAppIcon *wDockFindIconForWindow(WDock *dock, Window window)
3172 WAppIcon *icon;
3173 int i;
3175 for (i = 0; i < dock->max_icons; i++) {
3176 icon = dock->icon_array[i];
3177 if (icon && icon->main_window == window)
3178 return icon;
3180 return NULL;
3183 void wDockTrackWindowLaunch(WDock *dock, Window window)
3185 WAppIcon *icon;
3186 char *wm_class, *wm_instance;
3187 int i;
3188 Bool firstPass = True;
3189 Bool found = False;
3190 char *command = NULL;
3192 if (!PropGetWMClass(window, &wm_class, &wm_instance) || (!wm_class && !wm_instance))
3193 return;
3195 command = GetCommandForWindow(window);
3196 retry:
3197 for (i = 0; i < dock->max_icons; i++) {
3198 icon = dock->icon_array[i];
3199 if (!icon)
3200 continue;
3202 /* app is already attached to icon */
3203 if (icon->main_window == window) {
3204 found = True;
3205 break;
3208 if ((icon->wm_instance || icon->wm_class)
3209 && (icon->launching || !icon->running)) {
3211 if (icon->wm_instance && wm_instance && strcmp(icon->wm_instance, wm_instance) != 0)
3212 continue;
3214 if (icon->wm_class && wm_class && strcmp(icon->wm_class, wm_class) != 0)
3215 continue;
3217 if (firstPass && command && strcmp(icon->command, command) != 0)
3218 continue;
3220 if (!icon->relaunching) {
3221 WApplication *wapp;
3223 /* Possibly an application that was docked with dockit,
3224 * but the user did not update WMState to indicate that
3225 * it was docked by force */
3226 wapp = wApplicationOf(window);
3227 if (!wapp) {
3228 icon->forced_dock = 1;
3229 icon->running = 0;
3231 if (!icon->forced_dock)
3232 icon->main_window = window;
3234 found = True;
3235 if (!wPreferences.no_animations && !icon->launching &&
3236 !dock->screen_ptr->flags.startup && !dock->collapsed) {
3237 WAppIcon *aicon;
3238 int x0, y0;
3240 icon->launching = 1;
3241 dockIconPaint(icon);
3243 aicon = wAppIconCreateForDock(dock->screen_ptr, NULL,
3244 wm_instance, wm_class, TILE_NORMAL);
3245 /* XXX: can: aicon->icon == NULL ? */
3246 PlaceIcon(dock->screen_ptr, &x0, &y0, wGetHeadForWindow(aicon->icon->owner));
3247 wAppIconMove(aicon, x0, y0);
3248 /* Should this always be lowered? -Dan */
3249 if (dock->lowered)
3250 wLowerFrame(aicon->icon->core);
3251 XMapWindow(dpy, aicon->icon->core->window);
3252 aicon->launching = 1;
3253 wAppIconPaint(aicon);
3254 SlideWindow(aicon->icon->core->window, x0, y0, icon->x_pos, icon->y_pos);
3255 XUnmapWindow(dpy, aicon->icon->core->window);
3256 wAppIconDestroy(aicon);
3258 wDockFinishLaunch(dock, icon);
3259 break;
3263 if (firstPass && !found) {
3264 firstPass = False;
3265 goto retry;
3268 if (command)
3269 wfree(command);
3271 if (wm_class)
3272 free(wm_class);
3273 if (wm_instance)
3274 free(wm_instance);
3277 void wClipUpdateForWorkspaceChange(WScreen *scr, int workspace)
3279 if (!wPreferences.flags.noclip) {
3280 scr->clip_icon->dock = scr->workspaces[workspace]->clip;
3281 if (scr->current_workspace != workspace) {
3282 WDock *old_clip = scr->workspaces[scr->current_workspace]->clip;
3283 WAppIconChain *chain = scr->global_icons;
3285 while (chain) {
3286 wDockMoveIconBetweenDocks(chain->aicon->dock,
3287 scr->workspaces[workspace]->clip,
3288 chain->aicon, chain->aicon->xindex, chain->aicon->yindex);
3289 if (scr->workspaces[workspace]->clip->collapsed)
3290 XUnmapWindow(dpy, chain->aicon->icon->core->window);
3291 chain = chain->next;
3294 wDockHideIcons(old_clip);
3295 if (old_clip->auto_raise_lower) {
3296 if (old_clip->auto_raise_magic) {
3297 WMDeleteTimerHandler(old_clip->auto_raise_magic);
3298 old_clip->auto_raise_magic = NULL;
3300 wDockLower(old_clip);
3302 if (old_clip->auto_collapse) {
3303 if (old_clip->auto_expand_magic) {
3304 WMDeleteTimerHandler(old_clip->auto_expand_magic);
3305 old_clip->auto_expand_magic = NULL;
3307 old_clip->collapsed = 1;
3309 wDockShowIcons(scr->workspaces[workspace]->clip);
3314 static void trackDeadProcess(pid_t pid, unsigned char status, WDock *dock)
3316 WAppIcon *icon;
3317 int i;
3319 for (i = 0; i < dock->max_icons; i++) {
3320 icon = dock->icon_array[i];
3321 if (!icon)
3322 continue;
3324 if (icon->launching && icon->pid == pid) {
3325 if (!icon->relaunching) {
3326 icon->running = 0;
3327 icon->main_window = None;
3329 wDockFinishLaunch(dock, icon);
3330 icon->pid = 0;
3331 if (status == 111) {
3332 char msg[PATH_MAX];
3333 char *cmd;
3335 #ifdef XDND
3336 if (icon->drop_launch)
3337 cmd = icon->dnd_command;
3338 else
3339 #endif
3340 if (icon->paste_launch)
3341 cmd = icon->paste_command;
3342 else
3343 cmd = icon->command;
3345 snprintf(msg, sizeof(msg), _("Could not execute command \"%s\""), cmd);
3347 wMessageDialog(dock->screen_ptr, _("Error"), msg, _("OK"), NULL, NULL);
3349 break;
3354 /* This function is called when the dock switches state between
3355 * "normal" (including auto-raise/lower) and "keep on top". It is
3356 * therefore clearly distinct from wDockLower/Raise, which are called
3357 * each time a not-kept-on-top dock is lowered/raised. */
3358 static void toggleLowered(WDock *dock)
3360 WAppIcon *tmp;
3361 WDrawerChain *dc;
3362 int newlevel, i;
3364 if (!dock->lowered) {
3365 newlevel = WMNormalLevel;
3366 dock->lowered = 1;
3367 } else {
3368 newlevel = WMDockLevel;
3369 dock->lowered = 0;
3372 for (i = 0; i < dock->max_icons; i++) {
3373 tmp = dock->icon_array[i];
3374 if (!tmp)
3375 continue;
3377 ChangeStackingLevel(tmp->icon->core, newlevel);
3379 /* When the dock is no longer "on top", explicitly lower it as well.
3380 * It saves some CPU cycles (probably) to do it ourselves here
3381 * rather than calling wDockLower at the end of toggleLowered */
3382 if (dock->lowered)
3383 wLowerFrame(tmp->icon->core);
3386 if (dock->type == WM_DOCK) {
3387 for (dc = dock->screen_ptr->drawers; dc != NULL; dc = dc->next) {
3388 toggleLowered(dc->adrawer);
3390 wScreenUpdateUsableArea(dock->screen_ptr);
3394 static void toggleCollapsed(WDock *dock)
3396 if (dock->collapsed) {
3397 dock->collapsed = 0;
3398 wDockShowIcons(dock);
3399 } else {
3400 dock->collapsed = 1;
3401 wDockHideIcons(dock);
3405 static void openDockMenu(WDock *dock, WAppIcon *aicon, XEvent *event)
3407 WScreen *scr = dock->screen_ptr;
3408 WObjDescriptor *desc;
3409 WMenuEntry *entry;
3410 WApplication *wapp = NULL;
3411 int index = 0;
3412 int x_pos;
3413 int n_selected;
3414 int appIsRunning = aicon->running && aicon->icon && aicon->icon->owner;
3416 if (dock->type == WM_DOCK) {
3417 /* Dock position menu */
3418 updateDockPositionMenu(scr->dock_pos_menu, dock);
3419 dock->menu->flags.realized = 0;
3420 if (!wPreferences.flags.nodrawer) {
3421 /* add a drawer */
3422 entry = dock->menu->entries[++index];
3423 entry->clientdata = aicon;
3424 wMenuSetEnabled(dock->menu, index, True);
3426 } else {
3427 /* clip/drawer options */
3428 if (scr->clip_options)
3429 updateClipOptionsMenu(scr->clip_options, dock);
3431 n_selected = numberOfSelectedIcons(dock);
3433 if (dock->type == WM_CLIP) {
3434 /* Rename Workspace */
3435 entry = dock->menu->entries[++index];
3436 if (aicon == scr->clip_icon) {
3437 entry->callback = renameCallback;
3438 entry->clientdata = dock;
3439 entry->flags.indicator = 0;
3440 entry->text = _("Rename Workspace");
3441 } else {
3442 entry->callback = omnipresentCallback;
3443 entry->clientdata = aicon;
3444 if (n_selected > 0) {
3445 entry->flags.indicator = 0;
3446 entry->text = _("Toggle Omnipresent");
3447 } else {
3448 entry->flags.indicator = 1;
3449 entry->flags.indicator_on = aicon->omnipresent;
3450 entry->flags.indicator_type = MI_CHECK;
3451 entry->text = _("Omnipresent");
3456 /* select/unselect icon */
3457 entry = dock->menu->entries[++index];
3458 entry->clientdata = aicon;
3459 entry->flags.indicator_on = aicon->icon->selected;
3460 wMenuSetEnabled(dock->menu, index, aicon != scr->clip_icon && !wIsADrawer(scr, aicon));
3462 /* select/unselect all icons */
3463 entry = dock->menu->entries[++index];
3464 entry->clientdata = aicon;
3465 if (n_selected > 0)
3466 entry->text = _("Unselect All Icons");
3467 else
3468 entry->text = _("Select All Icons");
3470 wMenuSetEnabled(dock->menu, index, dock->icon_count > 1);
3472 /* keep icon(s) */
3473 entry = dock->menu->entries[++index];
3474 entry->clientdata = aicon;
3475 if (n_selected > 1)
3476 entry->text = _("Keep Icons");
3477 else
3478 entry->text = _("Keep Icon");
3480 wMenuSetEnabled(dock->menu, index, dock->icon_count > 1);
3482 if (dock->type == WM_CLIP) {
3483 /* this is the workspace submenu part */
3484 entry = dock->menu->entries[++index];
3485 if (n_selected > 1)
3486 entry->text = _("Move Icons To");
3487 else
3488 entry->text = _("Move Icon To");
3490 if (scr->clip_submenu)
3491 updateWorkspaceMenu(scr->clip_submenu, aicon);
3493 wMenuSetEnabled(dock->menu, index, !aicon->omnipresent);
3496 /* remove icon(s) */
3497 entry = dock->menu->entries[++index];
3498 entry->clientdata = aicon;
3499 if (n_selected > 1)
3500 entry->text = _("Remove Icons");
3501 else
3502 entry->text = _("Remove Icon");
3504 wMenuSetEnabled(dock->menu, index, dock->icon_count > 1);
3506 /* attract icon(s) */
3507 entry = dock->menu->entries[++index];
3508 entry->clientdata = aicon;
3510 dock->menu->flags.realized = 0;
3511 wMenuRealize(dock->menu);
3514 if (aicon->icon->owner)
3515 wapp = wApplicationOf(aicon->icon->owner->main_window);
3516 else
3517 wapp = NULL;
3519 /* launch */
3520 entry = dock->menu->entries[++index];
3521 entry->clientdata = aicon;
3522 wMenuSetEnabled(dock->menu, index, aicon->command != NULL);
3524 /* unhide here */
3525 entry = dock->menu->entries[++index];
3526 entry->clientdata = aicon;
3527 if (wapp && wapp->flags.hidden)
3528 entry->text = _("Unhide Here");
3529 else
3530 entry->text = _("Bring Here");
3532 wMenuSetEnabled(dock->menu, index, appIsRunning);
3534 /* hide */
3535 entry = dock->menu->entries[++index];
3536 entry->clientdata = aicon;
3537 if (wapp && wapp->flags.hidden)
3538 entry->text = _("Unhide");
3539 else
3540 entry->text = _("Hide");
3542 wMenuSetEnabled(dock->menu, index, appIsRunning);
3544 /* settings */
3545 entry = dock->menu->entries[++index];
3546 entry->clientdata = aicon;
3547 wMenuSetEnabled(dock->menu, index, !aicon->editing && !wPreferences.flags.noupdates);
3549 /* kill or remove drawer */
3550 entry = dock->menu->entries[++index];
3551 entry->clientdata = aicon;
3552 if (wIsADrawer(scr, aicon)) {
3553 entry->callback = removeDrawerCallback;
3554 entry->text = _("Remove drawer");
3555 wMenuSetEnabled(dock->menu, index, True);
3556 } else {
3557 entry->callback = killCallback;
3558 entry->text = _("Kill");
3559 wMenuSetEnabled(dock->menu, index, appIsRunning);
3562 if (!dock->menu->flags.realized)
3563 wMenuRealize(dock->menu);
3565 if (dock->type == WM_CLIP || dock->type == WM_DRAWER) {
3566 /*x_pos = event->xbutton.x_root+2; */
3567 x_pos = event->xbutton.x_root - dock->menu->frame->core->width / 2 - 1;
3568 if (x_pos < 0) {
3569 x_pos = 0;
3570 } else if (x_pos + dock->menu->frame->core->width > scr->scr_width - 2) {
3571 x_pos = scr->scr_width - dock->menu->frame->core->width - 4;
3573 } else {
3574 x_pos = dock->on_right_side ? scr->scr_width - dock->menu->frame->core->width - 3 : 0;
3577 wMenuMapAt(dock->menu, x_pos, event->xbutton.y_root + 2, False);
3579 /* allow drag select */
3580 event->xany.send_event = True;
3581 desc = &dock->menu->menu->descriptor;
3582 (*desc->handle_mousedown) (desc, event);
3585 /******************************************************************/
3586 static void iconDblClick(WObjDescriptor *desc, XEvent *event)
3588 WAppIcon *btn = desc->parent;
3589 WDock *dock = btn->dock;
3590 WApplication *wapp = NULL;
3591 int unhideHere = 0;
3593 if (btn->icon->owner && !(event->xbutton.state & ControlMask)) {
3594 wapp = wApplicationOf(btn->icon->owner->main_window);
3596 assert(wapp != NULL);
3598 unhideHere = (event->xbutton.state & ShiftMask);
3600 /* go to the last workspace that the user worked on the app */
3601 if (wapp->last_workspace != dock->screen_ptr->current_workspace && !unhideHere)
3602 wWorkspaceChange(dock->screen_ptr, wapp->last_workspace);
3604 wUnhideApplication(wapp, event->xbutton.button == Button2, unhideHere);
3606 if (event->xbutton.state & MOD_MASK)
3607 wHideOtherApplications(btn->icon->owner);
3608 } else {
3609 if (event->xbutton.button == Button1) {
3610 if (event->xbutton.state & MOD_MASK) {
3611 /* raise/lower dock */
3612 toggleLowered(dock);
3613 } else if (btn == dock->screen_ptr->clip_icon) {
3614 if (getClipButton(event->xbutton.x, event->xbutton.y) != CLIP_IDLE)
3615 handleClipChangeWorkspace(dock->screen_ptr, event);
3616 else if (wPreferences.flags.clip_merged_in_dock) {
3617 // Is actually the dock
3618 if (btn->command)
3620 if (!btn->launching && (!btn->running || (event->xbutton.state & ControlMask)))
3621 launchDockedApplication(btn, False);
3623 else
3625 wShowInfoPanel(dock->screen_ptr);
3628 else
3629 toggleCollapsed(dock);
3630 } else if (wIsADrawer(dock->screen_ptr, btn)) {
3631 toggleCollapsed(dock);
3632 } else if (btn->command) {
3633 if (!btn->launching && (!btn->running || (event->xbutton.state & ControlMask)))
3634 launchDockedApplication(btn, False);
3635 } else if (btn->xindex == 0 && btn->yindex == 0 && btn->dock->type == WM_DOCK) {
3636 wShowInfoPanel(dock->screen_ptr);
3642 static void handleDockMove(WDock *dock, WAppIcon *aicon, XEvent *event)
3644 WScreen *scr = dock->screen_ptr;
3645 int ofs_x = event->xbutton.x, ofs_y = event->xbutton.y;
3646 WIcon *icon = aicon->icon;
3647 WAppIcon *tmpaicon;
3648 WDrawerChain *dc;
3649 int x = aicon->x_pos, y = aicon->y_pos;;
3650 int shad_x = x, shad_y = y;
3651 XEvent ev;
3652 int grabbed = 0, done, previously_on_right, now_on_right, previous_x_pos, i;
3653 Pixmap ghost = None;
3654 int superfluous = wPreferences.superfluous; /* we catch it to avoid problems */
3656 if (XGrabPointer(dpy, aicon->icon->core->window, True, ButtonMotionMask
3657 | ButtonReleaseMask | ButtonPressMask, GrabModeAsync,
3658 GrabModeAsync, None, None, CurrentTime) != GrabSuccess)
3659 wwarning("pointer grab failed for dock move");
3661 if (dock->type == WM_DRAWER) {
3662 Window wins[2];
3663 wins[0] = icon->core->window;
3664 wins[1] = scr->dock_shadow;
3665 XRestackWindows(dpy, wins, 2);
3666 XMoveResizeWindow(dpy, scr->dock_shadow, aicon->x_pos, aicon->y_pos,
3667 ICON_SIZE, ICON_SIZE);
3668 if (superfluous) {
3669 if (icon->pixmap!=None)
3670 ghost = MakeGhostIcon(scr, icon->pixmap);
3671 else
3672 ghost = MakeGhostIcon(scr, icon->core->window);
3674 XSetWindowBackgroundPixmap(dpy, scr->dock_shadow, ghost);
3675 XClearWindow(dpy, scr->dock_shadow);
3677 XMapWindow(dpy, scr->dock_shadow);
3680 previously_on_right = now_on_right = dock->on_right_side;
3681 previous_x_pos = dock->x_pos;
3682 done = 0;
3683 while (!done) {
3684 WMMaskEvent(dpy, PointerMotionMask | ButtonReleaseMask | ButtonPressMask
3685 | ButtonMotionMask | ExposureMask | EnterWindowMask, &ev);
3686 switch (ev.type) {
3687 case Expose:
3688 WMHandleEvent(&ev);
3689 break;
3691 case EnterNotify:
3692 /* It means the cursor moved so fast that it entered
3693 * something else (if moving slowly, it would have
3694 * stayed in the dock that is being moved. Ignore such
3695 * "spurious" EnterNotifiy's */
3696 break;
3698 case MotionNotify:
3699 if (!grabbed) {
3700 if (abs(ofs_x - ev.xmotion.x) >= MOVE_THRESHOLD
3701 || abs(ofs_y - ev.xmotion.y) >= MOVE_THRESHOLD) {
3702 XChangeActivePointerGrab(dpy, ButtonMotionMask
3703 | ButtonReleaseMask | ButtonPressMask,
3704 wCursor[WCUR_MOVE], CurrentTime);
3705 grabbed = 1;
3707 break;
3709 switch (dock->type) {
3710 case WM_CLIP:
3711 x = ev.xmotion.x_root - ofs_x;
3712 y = ev.xmotion.y_root - ofs_y;
3713 wScreenKeepInside(scr, &x, &y, ICON_SIZE, ICON_SIZE);
3714 moveDock(dock, x, y);
3715 break;
3716 case WM_DOCK:
3717 x = ev.xmotion.x_root - ofs_x;
3718 y = ev.xmotion.y_root - ofs_y;
3719 if (previously_on_right)
3721 now_on_right = (ev.xmotion.x_root >= previous_x_pos - ICON_SIZE);
3723 else
3725 now_on_right = (ev.xmotion.x_root > previous_x_pos + ICON_SIZE * 2);
3727 if (now_on_right != dock->on_right_side)
3729 dock->on_right_side = now_on_right;
3730 swapDock(dock);
3731 wArrangeIcons(scr, False);
3733 // Also perform the vertical move
3734 wScreenKeepInside(scr, &x, &y, ICON_SIZE, ICON_SIZE);
3735 moveDock(dock, dock->x_pos, y);
3736 if (wPreferences.flags.wrap_appicons_in_dock)
3738 for (i = 0; i < dock->max_icons; i++) {
3739 int new_y, new_index, j, ok;
3740 if ((tmpaicon = dock->icon_array[i]) == NULL)
3741 continue;
3742 if (onScreen(scr, tmpaicon->x_pos, tmpaicon->y_pos))
3743 continue;
3744 new_y = (tmpaicon->y_pos + ICON_SIZE * dock->max_icons) % (ICON_SIZE * dock->max_icons);
3745 new_index = (new_y - dock->y_pos) / ICON_SIZE;
3746 if (!onScreen(scr, tmpaicon->x_pos, new_y))
3747 continue;
3748 ok = 1;
3749 for (j = 0; j < dock->max_icons; j++)
3751 if (dock->icon_array[j] != NULL &&
3752 dock->icon_array[j]->yindex == new_index)
3754 ok = 0;
3755 break;
3758 if (!ok || getDrawer(scr, new_index) != NULL)
3759 continue;
3760 wDockReattachIcon(dock, tmpaicon, tmpaicon->xindex, new_index);
3762 for (dc = scr->drawers; dc != NULL; dc = dc->next)
3764 int new_y, new_index, j, ok;
3765 tmpaicon = dc->adrawer->icon_array[0];
3766 if (onScreen(scr, tmpaicon->x_pos, tmpaicon->y_pos))
3767 continue;
3768 new_y = (tmpaicon->y_pos + ICON_SIZE * dock->max_icons) % (ICON_SIZE * dock->max_icons);
3769 new_index = (new_y - dock->y_pos) / ICON_SIZE;
3770 if (!onScreen(scr, tmpaicon->x_pos, new_y))
3771 continue;
3772 ok = 1;
3773 for (j = 0; j < dock->max_icons; j++)
3775 if (dock->icon_array[j] != NULL &&
3776 dock->icon_array[j]->yindex == new_index)
3778 ok = 0;
3779 break;
3782 if (!ok || getDrawer(scr, new_index) != NULL)
3783 continue;
3784 moveDock(dc->adrawer, tmpaicon->x_pos, new_y);
3787 break;
3788 case WM_DRAWER:
3790 WDock *real_dock = dock->screen_ptr->dock;
3791 Bool snapped;
3792 int ix, iy;
3793 x = ev.xmotion.x_root - ofs_x;
3794 y = ev.xmotion.y_root - ofs_y;
3795 snapped = wDockSnapIcon(real_dock, aicon, x, y, &ix, &iy, True);
3796 if (snapped) {
3797 shad_x = real_dock->x_pos + ix * wPreferences.icon_size;
3798 shad_y = real_dock->y_pos + iy * wPreferences.icon_size;
3799 XMoveWindow(dpy, scr->dock_shadow, shad_x, shad_y);
3801 moveDock(dock, x, y);
3802 break;
3805 break;
3807 case ButtonPress:
3808 break;
3810 case ButtonRelease:
3811 if (ev.xbutton.button != event->xbutton.button)
3812 break;
3813 XUngrabPointer(dpy, CurrentTime);
3814 if (dock->type == WM_DRAWER) {
3815 Window *wins[dock->icon_count];
3817 for (i = 0; i < dock->max_icons; i++) {
3818 if ((tmpaicon = dock->icon_array[i]) == NULL)
3819 continue;
3820 wins[ tmpaicon->xindex + (dock->on_right_side ? dock->icon_count - 1 : 0) ] = &tmpaicon->icon->core->window;
3822 SlideWindows(wins, dock->icon_count,
3823 (dock->on_right_side ? x - (dock->icon_count - 1) * ICON_SIZE : x),
3825 (dock->on_right_side ? shad_x - (dock->icon_count - 1) * ICON_SIZE : shad_x),
3826 shad_y);
3827 XUnmapWindow(dpy, scr->dock_shadow);
3828 moveDock(dock, shad_x, shad_y);
3829 XResizeWindow(dpy, scr->dock_shadow, ICON_SIZE, ICON_SIZE);
3831 done = 1;
3832 break;
3835 if (superfluous) {
3836 if (ghost != None)
3837 XFreePixmap(dpy, ghost);
3838 XSetWindowBackground(dpy, scr->dock_shadow, scr->white_pixel);
3843 static int getClipButton(int px, int py)
3845 int pt = (CLIP_BUTTON_SIZE + 2) * ICON_SIZE / 64;
3847 if (px < 0 || py < 0 || px >= ICON_SIZE || py >= ICON_SIZE)
3848 return CLIP_IDLE;
3850 if (py <= pt - ((int)ICON_SIZE - 1 - px))
3851 return CLIP_FORWARD;
3852 else if (px <= pt - ((int)ICON_SIZE - 1 - py))
3853 return CLIP_REWIND;
3855 return CLIP_IDLE;
3858 static void handleClipChangeWorkspace(WScreen *scr, XEvent *event)
3860 XEvent ev;
3861 int done, direction, new_ws;
3862 int new_dir;
3863 WDock *clip = scr->clip_icon->dock;
3865 direction = getClipButton(event->xbutton.x, event->xbutton.y);
3867 clip->lclip_button_pushed = direction == CLIP_REWIND;
3868 clip->rclip_button_pushed = direction == CLIP_FORWARD;
3870 wClipIconPaint(scr->clip_icon);
3871 done = 0;
3872 while (!done) {
3873 WMMaskEvent(dpy, ExposureMask | ButtonMotionMask | ButtonReleaseMask | ButtonPressMask, &ev);
3874 switch (ev.type) {
3875 case Expose:
3876 WMHandleEvent(&ev);
3877 break;
3879 case MotionNotify:
3880 new_dir = getClipButton(ev.xmotion.x, ev.xmotion.y);
3881 if (new_dir != direction) {
3882 direction = new_dir;
3883 clip->lclip_button_pushed = direction == CLIP_REWIND;
3884 clip->rclip_button_pushed = direction == CLIP_FORWARD;
3885 wClipIconPaint(scr->clip_icon);
3887 break;
3889 case ButtonPress:
3890 break;
3892 case ButtonRelease:
3893 if (ev.xbutton.button == event->xbutton.button)
3894 done = 1;
3898 clip->lclip_button_pushed = 0;
3899 clip->rclip_button_pushed = 0;
3901 new_ws = wPreferences.ws_advance || (event->xbutton.state & ControlMask);
3903 if (direction == CLIP_FORWARD) {
3904 if (scr->current_workspace < scr->workspace_count - 1)
3905 wWorkspaceChange(scr, scr->current_workspace + 1);
3906 else if (new_ws && scr->current_workspace < MAX_WORKSPACES - 1)
3907 wWorkspaceChange(scr, scr->current_workspace + 1);
3908 else if (wPreferences.ws_cycle)
3909 wWorkspaceChange(scr, 0);
3910 } else if (direction == CLIP_REWIND) {
3911 if (scr->current_workspace > 0)
3912 wWorkspaceChange(scr, scr->current_workspace - 1);
3913 else if (scr->current_workspace == 0 && wPreferences.ws_cycle)
3914 wWorkspaceChange(scr, scr->workspace_count - 1);
3917 wClipIconPaint(scr->clip_icon);
3920 static void iconMouseDown(WObjDescriptor *desc, XEvent *event)
3922 WAppIcon *aicon = desc->parent;
3923 WDock *dock = aicon->dock;
3924 WScreen *scr = aicon->icon->core->screen_ptr;
3926 if (aicon->editing || WCHECK_STATE(WSTATE_MODAL))
3927 return;
3929 scr->last_dock = dock;
3931 if (dock->menu->flags.mapped)
3932 wMenuUnmap(dock->menu);
3934 if (IsDoubleClick(scr, event)) {
3935 /* double-click was not in the main clip icon */
3936 if (dock->type != WM_CLIP || aicon->xindex != 0 || aicon->yindex != 0
3937 || getClipButton(event->xbutton.x, event->xbutton.y) == CLIP_IDLE) {
3938 iconDblClick(desc, event);
3939 return;
3943 if (event->xbutton.button == Button1) {
3944 if (event->xbutton.state & MOD_MASK)
3945 wDockLower(dock);
3946 else
3947 wDockRaise(dock);
3949 if ((event->xbutton.state & ShiftMask) && aicon != scr->clip_icon && dock->type != WM_DOCK) {
3950 wIconSelect(aicon->icon);
3951 return;
3954 if (aicon->yindex == 0 && aicon->xindex == 0) {
3955 if (getClipButton(event->xbutton.x, event->xbutton.y) != CLIP_IDLE &&
3956 (dock->type == WM_CLIP || (dock->type == WM_DOCK && wPreferences.flags.clip_merged_in_dock)))
3957 handleClipChangeWorkspace(scr, event);
3958 else
3959 handleDockMove(dock, aicon, event);
3960 } else {
3961 Bool hasMoved = wHandleAppIconMove(aicon, event);
3962 if (wPreferences.single_click && !hasMoved)
3963 iconDblClick(desc, event);
3965 } else if (event->xbutton.button == Button2 && aicon == scr->clip_icon) {
3966 if (!scr->clip_ws_menu) {
3967 scr->clip_ws_menu = wWorkspaceMenuMake(scr, False);
3969 if (scr->clip_ws_menu) {
3970 WMenu *wsMenu = scr->clip_ws_menu;
3971 int xpos;
3973 wWorkspaceMenuUpdate(scr, wsMenu);
3975 xpos = event->xbutton.x_root - wsMenu->frame->core->width / 2 - 1;
3976 if (xpos < 0) {
3977 xpos = 0;
3978 } else if (xpos + wsMenu->frame->core->width > scr->scr_width - 2) {
3979 xpos = scr->scr_width - wsMenu->frame->core->width - 4;
3981 wMenuMapAt(wsMenu, xpos, event->xbutton.y_root + 2, False);
3983 desc = &wsMenu->menu->descriptor;
3984 event->xany.send_event = True;
3985 (*desc->handle_mousedown) (desc, event);
3987 } else if (event->xbutton.button == Button2 && dock->type == WM_CLIP &&
3988 (event->xbutton.state & ShiftMask) && aicon != scr->clip_icon) {
3989 wClipMakeIconOmnipresent(aicon, !aicon->omnipresent);
3990 } else if (event->xbutton.button == Button3) {
3991 if (event->xbutton.send_event &&
3992 XGrabPointer(dpy, aicon->icon->core->window, True, ButtonMotionMask
3993 | ButtonReleaseMask | ButtonPressMask, GrabModeAsync,
3994 GrabModeAsync, None, None, CurrentTime) != GrabSuccess) {
3995 wwarning("pointer grab failed for dockicon menu");
3996 return;
3999 openDockMenu(dock, aicon, event);
4000 } else if (event->xbutton.button == Button2) {
4001 WAppIcon *btn = desc->parent;
4003 if (!btn->launching && (!btn->running || (event->xbutton.state & ControlMask)))
4004 launchDockedApplication(btn, True);
4008 static void clipEnterNotify(WObjDescriptor *desc, XEvent *event)
4010 WAppIcon *btn = (WAppIcon *) desc->parent;
4011 WDock *dock, *tmp;
4012 WScreen *scr;
4014 assert(event->type == EnterNotify);
4016 if (desc->parent_type != WCLASS_DOCK_ICON)
4017 return;
4019 scr = btn->icon->core->screen_ptr;
4020 dock = btn->dock;
4022 if (dock == NULL)
4023 return;
4025 /* The auto raise/lower code */
4026 tmp = (dock->type == WM_DRAWER ? scr->dock : dock);
4027 if (tmp->auto_lower_magic) {
4028 WMDeleteTimerHandler(tmp->auto_lower_magic);
4029 tmp->auto_lower_magic = NULL;
4031 if (tmp->auto_raise_lower && !tmp->auto_raise_magic)
4032 tmp->auto_raise_magic = WMAddTimerHandler(wPreferences.clip_auto_raise_delay, clipAutoRaise, (void *) tmp);
4034 if (dock->type != WM_CLIP && dock->type != WM_DRAWER)
4035 return;
4037 /* The auto expand/collapse code */
4038 if (dock->auto_collapse_magic) {
4039 WMDeleteTimerHandler(dock->auto_collapse_magic);
4040 dock->auto_collapse_magic = NULL;
4042 if (dock->auto_collapse && !dock->auto_expand_magic)
4043 dock->auto_expand_magic = WMAddTimerHandler(wPreferences.clip_auto_expand_delay, clipAutoExpand, (void *)dock);
4046 static void clipLeave(WDock *dock)
4048 XEvent event;
4049 WObjDescriptor *desc = NULL;
4050 WDock *tmp;
4052 if (dock == NULL)
4053 return;
4055 if (XCheckTypedEvent(dpy, EnterNotify, &event) != False) {
4056 if (XFindContext(dpy, event.xcrossing.window, wWinContext,
4057 (XPointer *) & desc) != XCNOENT
4058 && desc && desc->parent_type == WCLASS_DOCK_ICON
4059 && ((WAppIcon *) desc->parent)->dock == dock) {
4060 /* We haven't left the dock/clip/drawer yet */
4061 XPutBackEvent(dpy, &event);
4062 return;
4065 XPutBackEvent(dpy, &event);
4066 } else {
4067 /* We entered a withdrawn window, so we're still in Clip */
4068 return;
4071 tmp = (dock->type == WM_DRAWER ? dock->screen_ptr->dock : dock);
4072 if (tmp->auto_raise_magic) {
4073 WMDeleteTimerHandler(tmp->auto_raise_magic);
4074 tmp->auto_raise_magic = NULL;
4076 if (tmp->auto_raise_lower && !tmp->auto_lower_magic)
4077 tmp->auto_lower_magic = WMAddTimerHandler(wPreferences.clip_auto_lower_delay, clipAutoLower, (void *)tmp);
4079 if (dock->type != WM_CLIP && dock->type != WM_DRAWER)
4080 return;
4082 if (dock->auto_expand_magic) {
4083 WMDeleteTimerHandler(dock->auto_expand_magic);
4084 dock->auto_expand_magic = NULL;
4086 if (dock->auto_collapse && !dock->auto_collapse_magic)
4087 dock->auto_collapse_magic = WMAddTimerHandler(wPreferences.clip_auto_collapse_delay, clipAutoCollapse, (void *)dock);
4090 static void clipLeaveNotify(WObjDescriptor *desc, XEvent *event)
4092 WAppIcon *btn = (WAppIcon *) desc->parent;
4094 assert(event->type == LeaveNotify);
4096 if (desc->parent_type != WCLASS_DOCK_ICON)
4097 return;
4099 clipLeave(btn->dock);
4102 static void clipAutoCollapse(void *cdata)
4104 WDock *dock = (WDock *) cdata;
4106 if (dock->type != WM_CLIP && dock->type != WM_DRAWER)
4107 return;
4109 if (dock->auto_collapse) {
4110 dock->collapsed = 1;
4111 wDockHideIcons(dock);
4113 dock->auto_collapse_magic = NULL;
4116 static void clipAutoExpand(void *cdata)
4118 WDock *dock = (WDock *) cdata;
4120 if (dock->type != WM_CLIP && dock->type != WM_DRAWER)
4121 return;
4123 if (dock->auto_collapse) {
4124 dock->collapsed = 0;
4125 wDockShowIcons(dock);
4127 dock->auto_expand_magic = NULL;
4130 static void clipAutoLower(void *cdata)
4132 WDock *dock = (WDock *) cdata;
4134 if (dock->auto_raise_lower)
4135 wDockLower(dock);
4137 dock->auto_lower_magic = NULL;
4140 static void clipAutoRaise(void *cdata)
4142 WDock *dock = (WDock *) cdata;
4144 if (dock->auto_raise_lower)
4145 wDockRaise(dock);
4147 dock->auto_raise_magic = NULL;
4150 static Bool iconCanBeOmnipresent(WAppIcon *aicon)
4152 WScreen *scr = aicon->icon->core->screen_ptr;
4153 WDock *clip;
4154 WAppIcon *btn;
4155 int i, j;
4157 for (i = 0; i < scr->workspace_count; i++) {
4158 clip = scr->workspaces[i]->clip;
4160 if (clip == aicon->dock)
4161 continue;
4163 if (clip->icon_count + scr->global_icon_count >= clip->max_icons)
4164 return False; /* Clip is full in some workspace */
4166 for (j = 0; j < clip->max_icons; j++) {
4167 btn = clip->icon_array[j];
4168 if (btn && btn->xindex == aicon->xindex && btn->yindex == aicon->yindex)
4169 return False;
4173 return True;
4176 int wClipMakeIconOmnipresent(WAppIcon *aicon, int omnipresent)
4178 WScreen *scr = aicon->icon->core->screen_ptr;
4179 WAppIconChain *new_entry, *tmp, *tmp1;
4180 int status = WO_SUCCESS;
4182 if ((scr->dock && aicon->dock == scr->dock) || aicon == scr->clip_icon)
4183 return WO_NOT_APPLICABLE;
4185 if (aicon->omnipresent == omnipresent)
4186 return WO_SUCCESS;
4188 if (omnipresent) {
4189 if (iconCanBeOmnipresent(aicon)) {
4190 aicon->omnipresent = 1;
4191 new_entry = wmalloc(sizeof(WAppIconChain));
4192 new_entry->aicon = aicon;
4193 new_entry->next = scr->global_icons;
4194 scr->global_icons = new_entry;
4195 scr->global_icon_count++;
4196 } else {
4197 aicon->omnipresent = 0;
4198 status = WO_FAILED;
4200 } else {
4201 aicon->omnipresent = 0;
4202 if (aicon == scr->global_icons->aicon) {
4203 tmp = scr->global_icons->next;
4204 wfree(scr->global_icons);
4205 scr->global_icons = tmp;
4206 scr->global_icon_count--;
4207 } else {
4208 tmp = scr->global_icons;
4209 while (tmp->next) {
4210 if (tmp->next->aicon == aicon) {
4211 tmp1 = tmp->next->next;
4212 wfree(tmp->next);
4213 tmp->next = tmp1;
4214 scr->global_icon_count--;
4215 break;
4217 tmp = tmp->next;
4222 wAppIconPaint(aicon);
4224 return status;
4227 static void drawerAppendToChain(WScreen *scr, WDock *drawer)
4229 WDrawerChain **where_to_add;
4231 where_to_add = &scr->drawers;
4232 while ((*where_to_add) != NULL) {
4233 where_to_add = &(*where_to_add)->next;
4236 *where_to_add = wmalloc(sizeof(WDrawerChain));
4237 (*where_to_add)->adrawer = drawer;
4238 (*where_to_add)->next = NULL;
4239 scr->drawer_count++;
4243 static void drawerRemoveFromChain(WScreen *scr, WDock *drawer)
4245 WDrawerChain *next, **to_remove;
4247 to_remove = &scr->drawers;
4248 while (True) {
4249 if (*to_remove == NULL) {
4250 wwarning("The drawer to be removed can not be found.");
4251 return;
4253 if ((*to_remove)->adrawer == drawer)
4254 break;
4256 to_remove = &(*to_remove)->next;
4258 next = (*to_remove)->next;
4259 wfree(*to_remove);
4260 *to_remove = next;
4261 scr->drawer_count--;
4265 /* Don't free the returned string. Duplicate it. */
4266 static char * findUniqueName(WScreen *scr, const char *instance_basename)
4268 static char buffer[128];
4269 WDrawerChain *dc;
4270 int i;
4271 Bool already_in_use = True;
4273 #define UNIQUE_NAME_WATCHDOG 128
4274 for (i = 0; already_in_use && i < UNIQUE_NAME_WATCHDOG; i++) {
4275 snprintf(buffer, sizeof buffer, "%s%d", instance_basename, i);
4277 already_in_use = False;
4279 for (dc = scr->drawers; dc != NULL; dc = dc->next) {
4280 if (!strncmp(dc->adrawer->icon_array[0]->wm_instance, buffer,
4281 sizeof buffer)) {
4282 already_in_use = True;
4283 break;
4288 if (i == UNIQUE_NAME_WATCHDOG)
4289 wwarning("Couldn't find a unique name for drawer in %d attemps.", i);
4290 #undef UNIQUE_NAME_WATCHDOG
4292 return buffer;
4296 static void drawerIconExpose(WObjDescriptor *desc, XEvent *event)
4298 wDrawerIconPaint((WAppIcon *) desc->parent);
4302 static int addADrawer(WScreen *scr)
4304 int i, y, sig, found_y;
4305 WDock *drawer, *dock = scr->dock;
4306 WDrawerChain *dc;
4307 char can_be_here[2 * dock->max_icons - 1];
4309 if (dock->icon_count + scr->drawer_count >= dock->max_icons)
4310 return -1;
4312 for (y = -dock->max_icons + 1; y < dock->max_icons; y++) {
4313 can_be_here[y + dock->max_icons - 1] = True;
4315 for (i = 0; i < dock->max_icons; i++) {
4316 if (dock->icon_array[i] != NULL)
4317 can_be_here[dock->icon_array[i]->yindex + dock->max_icons - 1] = False;
4319 for (dc = scr->drawers; dc != NULL; dc = dc->next) {
4320 y = (int) ((dc->adrawer->y_pos - dock->y_pos) / ICON_SIZE);
4321 can_be_here[y + dock->max_icons - 1] = False;
4324 found_y = False;
4325 for (sig = 1; !found_y && sig > -2; sig -= 2) // 1, then -1
4327 for (y = sig; sig * y < dock->max_icons; y += sig)
4329 if (can_be_here[y + dock->max_icons - 1] &&
4330 onScreen(scr, dock->x_pos, dock->y_pos + y * ICON_SIZE))
4332 found_y = True;
4333 break;
4338 if (!found_y)
4339 /* This can happen even when dock->icon_count + scr->drawer_count
4340 * < dock->max_icons when the dock is not aligned on an
4341 * ICON_SIZE multiple, as some space is lost above and under it */
4342 return -1;
4344 drawer = wDockCreate(scr, WM_DRAWER, NULL);
4345 drawer->lowered = scr->dock->lowered;
4346 drawer->auto_raise_lower = scr->dock->auto_raise_lower;
4347 drawer->x_pos = dock->x_pos;
4348 drawer->y_pos = dock->y_pos + ICON_SIZE * y;
4349 drawer->icon_array[0]->xindex = 0;
4350 drawer->icon_array[0]->yindex = 0;
4351 drawer->icon_array[0]->x_pos = drawer->x_pos;
4352 drawer->icon_array[0]->y_pos = drawer->y_pos;
4353 XMoveWindow(dpy, drawer->icon_array[0]->icon->core->window,
4354 drawer->icon_array[0]->x_pos, drawer->icon_array[0]->y_pos);
4356 return 0;
4360 static void addADrawerCallback(WMenu *menu, WMenuEntry *entry)
4362 assert(entry->clientdata!=NULL);
4363 addADrawer(((WAppIcon *) entry->clientdata)->dock->screen_ptr);
4367 static void drawerDestroy(WDock *drawer)
4369 WScreen *scr;
4370 int i;
4371 WAppIcon *aicon;
4372 WMArray *icons;
4374 if (drawer == NULL)
4375 return;
4377 scr = drawer->screen_ptr;
4379 /* Note regarding menus: we can't delete any dock/clip/drawer menu, because
4380 * that would (attempt to) wfree some memory in gettext library (see menu
4381 * entries that have several "versions", such like "Hide" and "Unhide"). */
4383 wDefaultPurgeInfo(scr, drawer->icon_array[0]->wm_instance,
4384 drawer->icon_array[0]->wm_class);
4385 icons = WMCreateArray(drawer->icon_count - 1);
4386 for (i = 1; i < drawer->max_icons; i++) {
4387 if (!(aicon = drawer->icon_array[i]))
4388 continue;
4389 WMAddToArray(icons, aicon);
4391 removeIcons(icons, drawer);
4393 if (drawer->auto_collapse_magic) {
4394 WMDeleteTimerHandler(drawer->auto_collapse_magic);
4395 drawer->auto_collapse_magic = NULL;
4397 if (drawer->auto_lower_magic) {
4398 WMDeleteTimerHandler(drawer->auto_lower_magic);
4399 drawer->auto_lower_magic = NULL;
4402 wAppIconDestroy(drawer->icon_array[0]);
4403 wfree(drawer->icon_array);
4404 wfree(drawer);
4406 drawerRemoveFromChain(scr, drawer);
4407 if (scr->last_dock == drawer)
4408 scr->last_dock = NULL;
4409 if (scr->attracting_drawer == drawer)
4410 scr->attracting_drawer = NULL;
4414 static void removeDrawerCallback(WMenu *menu, WMenuEntry *entry)
4416 WDock *dock = ((WAppIcon*)entry->clientdata)->dock;
4417 assert(dock != NULL);
4419 if (dock->icon_count > 1) {
4420 if (wMessageDialog(dock->screen_ptr, _("Drawer"),
4421 _("All icons in this drawer will be detached!"),
4422 _("OK"), _("Cancel"), NULL) != WAPRDefault)
4423 return;
4425 drawerDestroy(dock);
4429 void wDrawerIconPaint(WAppIcon *dicon)
4431 Window win = dicon->icon->core->window;
4432 WScreen *scr = dicon->icon->core->screen_ptr;
4433 XPoint p[4];
4434 GC gc = scr->draw_gc;
4435 WMColor *color;
4437 wIconPaint(dicon->icon);
4439 if (!dicon->dock->collapsed)
4440 color = scr->clip_title_color[CLIP_NORMAL];
4441 else
4442 color = scr->clip_title_color[CLIP_COLLAPSED];
4443 XSetForeground(dpy, gc, WMColorPixel(color));
4445 if (dicon->dock->on_right_side) {
4446 p[0].x = p[3].x = 10;
4447 p[0].y = p[3].y = ICON_SIZE / 2 - 5;
4448 p[1].x = 10;
4449 p[1].y = ICON_SIZE / 2 + 5;
4450 p[2].x = 5;
4451 p[2].y = ICON_SIZE / 2;
4453 else {
4454 p[0].x = p[3].x = ICON_SIZE-1 - 10;
4455 p[0].y = p[3].y = ICON_SIZE / 2 - 5;
4456 p[1].x = ICON_SIZE-1 - 10;
4457 p[1].y = ICON_SIZE / 2 + 5;
4458 p[2].x = ICON_SIZE-1 - 5;
4459 p[2].y = ICON_SIZE / 2;
4461 XFillPolygon(dpy, win, gc, p,3,Convex,CoordModeOrigin);
4462 XDrawLines(dpy, win, gc, p,4,CoordModeOrigin);
4466 RImage* wDrawerMakeTile(WScreen *scr, RImage *normalTile)
4468 RImage *tile = RCloneImage(normalTile);
4469 RColor dark;
4470 RColor light;
4472 dark.alpha = 0;
4473 dark.red = dark.green = dark.blue = 60;
4475 light.alpha = 0;
4476 light.red = light.green = light.blue = 80;
4478 /* arrow bevel */
4479 if (!scr->dock || scr->dock->on_right_side) {
4480 ROperateLine(tile, RSubtractOperation, 11, ICON_SIZE / 2 - 7,
4481 4, ICON_SIZE / 2, &dark); /* / */
4482 ROperateLine(tile, RSubtractOperation, 11, ICON_SIZE / 2 + 7,
4483 4, ICON_SIZE / 2, &dark); /* \ */
4484 ROperateLine(tile, RAddOperation, 11, ICON_SIZE / 2 - 7,
4485 11, ICON_SIZE / 2 + 7, &light); /* | */
4487 else {
4488 ROperateLine(tile, RSubtractOperation, ICON_SIZE-1 - 11, ICON_SIZE / 2 - 7,
4489 ICON_SIZE-1 - 4, ICON_SIZE / 2, &dark); /* \ */
4490 ROperateLine(tile, RAddOperation, ICON_SIZE-1 - 11, ICON_SIZE / 2 + 7,
4491 ICON_SIZE-1 - 4, ICON_SIZE / 2, &light); /* / */
4492 ROperateLine(tile, RSubtractOperation, ICON_SIZE-1 - 11, ICON_SIZE / 2 - 7,
4493 ICON_SIZE-1 - 11, ICON_SIZE / 2 + 7, &dark); /* | */
4495 return tile;
4499 static void swapDrawer(WDock *drawer, int new_x)
4501 int i;
4503 drawer->on_right_side = !drawer->on_right_side;
4504 drawer->x_pos = new_x;
4506 for (i = 0; i < drawer->max_icons; i++) {
4507 WAppIcon *ai;
4508 if ((ai = drawer->icon_array[i]) == NULL)
4509 continue;
4510 ai->xindex *= -1; /* so A B C becomes C B A */
4511 ai->x_pos = new_x + ai->xindex * ICON_SIZE;
4513 /* Update drawer's tile */
4514 if (i == 0) {
4515 wIconUpdate(ai->icon);
4516 wDrawerIconPaint(ai);
4518 XMoveWindow(dpy, ai->icon->core->window, ai->x_pos, ai->y_pos);
4523 static void swapDrawers(WScreen *scr, int on_right_side, int new_x)
4525 WDrawerChain *dc;
4527 if (scr->drawer_tile) {
4528 RReleaseImage(scr->drawer_tile);
4530 scr->drawer_tile = wDrawerMakeTile(scr, scr->icon_tile);
4532 for (dc = scr->drawers; dc != NULL; dc = dc->next) {
4533 swapDrawer(dc->adrawer, new_x);
4534 assert(dc->adrawer->on_right_side == on_right_side);
4539 int wIsADrawer(WScreen *scr, WAppIcon *aicon)
4541 return aicon && aicon->dock &&
4542 aicon->dock->type == WM_DRAWER && aicon->dock->icon_array[0] == aicon;
4546 static WDock* getDrawer(WScreen *scr, int y_index)
4548 WDrawerChain *dc;
4550 for (dc = scr->drawers; dc != NULL; dc = dc->next) {
4551 if (dc->adrawer->y_pos - scr->dock->y_pos == y_index * ICON_SIZE)
4552 return dc->adrawer;
4554 return NULL;
4559 /* Find the "hole" a moving appicon created when snapped into the
4560 * drawer. redocking is a boolean. If the moving appicon comes from the
4561 * drawer, drawer->icon_count is correct. If not, redocking is then false and
4562 * there are now drawer->icon_count plus one appicons in the drawer. */
4563 static int indexOfHole(WDock *drawer, WAppIcon *moving_aicon, int redocking)
4565 int index_of_hole, i;
4567 /* Classic interview question...
4569 * We have n-1 (n = drawer->icon_count-1 or drawer->icon_count, see
4570 * redocking) appicons, whose xindex are unique in [1..n]. One is missing:
4571 * that's where the ghost of the moving appicon is, that's what the
4572 * function should return.
4574 * We compute 1+2+...+n (this sum is equal to n*(n+1)/2), we substract to
4575 * this sum the xindex of each of the n-1 appicons, and we get the correct
4576 * index! */
4578 if (redocking) {
4579 index_of_hole = (drawer->icon_count - 1) * drawer->icon_count / 2;
4580 } else {
4581 index_of_hole = drawer->icon_count * (drawer->icon_count + 1) / 2;
4583 index_of_hole *= (drawer->on_right_side ? -1 : 1);
4585 for (i = 1; i < drawer->max_icons; i++) {
4586 if (drawer->icon_array[i] && drawer->icon_array[i] != moving_aicon)
4587 index_of_hole -= drawer->icon_array[i]->xindex;
4589 /* wmessage(" Index of the moving appicon is %d (%sredocking)", index_of_hole, (redocking ? "" : "not ")); */
4590 if (abs(index_of_hole) > abs(drawer->icon_count) - (redocking ? 1 : 0))
4591 wwarning(" index_of_hole is too large ! (%d greater than %d)",
4592 index_of_hole, abs(drawer->icon_count) - (redocking ? 1 : 0));
4593 if (index_of_hole == 0)
4594 wwarning(" index_of_hole == 0 (%sredocking, icon_count == %d)", (redocking ? "" : "not "), drawer->icon_count);
4596 return index_of_hole;
4600 void wSlideAppicons(WAppIcon **appicons, int n, int to_the_left)
4602 int i;
4603 int leftmost = -1, min_index = 9999, from_x = -1; // leftmost and from_x initialized to avoid warning
4604 Window *wins[n];
4605 WAppIcon *aicon;
4607 if (n < 1)
4608 return;
4610 for (i = 0; i < n; i++) {
4611 aicon = appicons[i];
4612 aicon->xindex += (to_the_left ? -1 : +1);
4613 if (aicon->xindex < min_index) {
4614 min_index = aicon->xindex;
4615 leftmost = i;
4616 from_x = aicon->x_pos;
4618 aicon->x_pos += (to_the_left ? -ICON_SIZE : +ICON_SIZE);
4621 for (i = 0; i < n; i++) {
4622 aicon = appicons[i];
4623 wins[aicon->xindex - min_index] = &aicon->icon->core->window;
4625 aicon = appicons[leftmost];
4626 SlideWindows(wins, n, from_x, aicon->y_pos, aicon->x_pos, aicon->y_pos);
4630 void wDrawerFillTheGap(WDock *drawer, WAppIcon *aicon, Bool redocking)
4632 int i, j;
4633 int index_of_hole = indexOfHole(drawer, aicon, redocking);
4634 WAppIcon *aicons_to_shift[drawer->icon_count];
4636 j = 0;
4637 for (i = 0; i < drawer->max_icons; i++) {
4638 WAppIcon *ai = drawer->icon_array[i];
4639 if (ai && ai != aicon &&
4640 abs(ai->xindex) > abs(index_of_hole))
4641 aicons_to_shift[j++] = ai;
4643 if (j != drawer->icon_count - abs(index_of_hole) - (redocking ? 1 : 0))
4644 wwarning("Removing aicon at index %d from %s: j=%d but should be %d",
4645 index_of_hole, drawer->icon_array[0]->wm_instance,
4646 j, drawer->icon_count - abs(index_of_hole) - (redocking ? 1 : 0));
4647 wSlideAppicons(aicons_to_shift, j, !drawer->on_right_side);
4651 static void drawerConsolidateIcons(WDock *drawer)
4653 WAppIcon *aicons_to_shift[drawer->icon_count];
4654 int maxRemaining = 0;
4655 int sum = 0;
4656 int i;
4657 for (i = 0; i < drawer->max_icons; i++) {
4658 WAppIcon *ai = drawer->icon_array[i];
4659 if (ai == NULL)
4660 continue;
4661 sum += abs(ai->xindex);
4662 if (abs(ai->xindex) > maxRemaining)
4663 maxRemaining = abs(ai->xindex);
4665 while (sum != maxRemaining * (maxRemaining + 1) / 2) { // while there is a hole
4666 WAppIcon *ai;
4667 int n;
4668 // Look up for the hole at max position
4669 int maxDeleted;
4670 for (maxDeleted = maxRemaining - 1; maxDeleted > 0; maxDeleted--) {
4671 Bool foundAppIconThere = False;
4672 for (i = 0; i < drawer->max_icons; i++) {
4673 WAppIcon *ai = drawer->icon_array[i];
4674 if (ai == NULL)
4675 continue;
4676 if (abs(ai->xindex) == maxDeleted) {
4677 foundAppIconThere = True;
4678 break;
4681 if (!foundAppIconThere)
4682 break;
4684 assert(maxDeleted > 0); // would mean while test is wrong
4685 n = 0;
4686 for (i = 0; i < drawer->max_icons; i++) {
4687 ai = drawer->icon_array[i];
4688 if (ai != NULL && abs(ai->xindex) > maxDeleted)
4689 aicons_to_shift[n++] = ai;
4691 assert(n == maxRemaining - maxDeleted); // for the code review ;-)
4692 wSlideAppicons(aicons_to_shift, n, !drawer->on_right_side);
4693 // Efficient beancounting
4694 maxRemaining -= 1;
4695 sum -= n;
4701 /* similar to wDockRestoreState, but a lot a specific stuff too... */
4702 static WDock * drawerRestoreState(WScreen *scr, WMPropList *drawer_state)
4704 WDock *drawer;
4705 WMPropList *apps, *value, *dock_state;
4706 WAppIcon *aicon;
4707 int count, i;
4709 if (!drawer_state)
4710 return NULL;
4712 make_keys();
4714 WMRetainPropList(drawer_state);
4716 /* Get the instance name, and create a drawer */
4717 value = WMGetFromPLDictionary(drawer_state, dName);
4718 drawer = wDockCreate(scr, WM_DRAWER, WMGetFromPLString(value));
4720 /* restore DnD command and paste command */
4721 #ifdef XDND
4722 value = WMGetFromPLDictionary(drawer_state, dDropCommand);
4723 if (value && WMIsPLString(value))
4724 drawer->icon_array[0]->dnd_command = wstrdup(WMGetFromPLString(value));
4725 #endif /* XDND */
4727 value = WMGetFromPLDictionary(drawer_state, dPasteCommand);
4728 if (value && WMIsPLString(value))
4729 drawer->icon_array[0]->paste_command = wstrdup(WMGetFromPLString(value));
4731 /* restore position */
4732 value = WMGetFromPLDictionary(drawer_state, dPosition);
4733 if (!value || !WMIsPLString(value))
4734 COMPLAIN("Position");
4735 else {
4736 int x, y, y_index;
4737 if (sscanf(WMGetFromPLString(value), "%i,%i", &x, &y) != 2)
4738 COMPLAIN("Position");
4740 /* check position sanity */
4741 if (x != scr->dock->x_pos) {
4742 x = scr->dock->x_pos;
4744 y_index = (y - scr->dock->y_pos) / ICON_SIZE;
4745 if (y_index >= scr->dock->max_icons) {
4746 /* Here we should do something more intelligent, since it
4747 * can happen even if the user hasn't hand-edited his
4748 * G/D/State file (but uses a lower resolution). */
4749 y_index = scr->dock->max_icons - 1;
4751 y = scr->dock->y_pos + y_index * ICON_SIZE;
4752 moveDock(drawer, x, y);
4755 /* restore dock properties (applist and others) */
4756 dock_state = WMGetFromPLDictionary(drawer_state, dDock);
4758 /* restore lowered/raised state: same as scr->dock, no matter what */
4759 drawer->lowered = scr->dock->lowered;
4760 if (!drawer->lowered)
4761 ChangeStackingLevel(drawer->icon_array[0]->icon->core, WMDockLevel);
4762 else
4763 ChangeStackingLevel(drawer->icon_array[0]->icon->core, WMNormalLevel);
4764 wRaiseFrame(drawer->icon_array[0]->icon->core);
4766 /* restore collapsed state */
4767 drawer->collapsed = 0;
4768 value = WMGetFromPLDictionary(dock_state, dCollapsed);
4769 if (value && strcasecmp(WMGetFromPLString(value), "YES") == 0) {
4770 drawer->collapsed = 1;
4772 /* restore auto-collapsed state */
4773 value = WMGetFromPLDictionary(dock_state, dAutoCollapse);
4774 if (value && strcasecmp(WMGetFromPLString(value), "YES") == 0) {
4775 drawer->auto_collapse = 1;
4776 drawer->collapsed = 1;
4777 } else {
4778 drawer->auto_collapse = 0; // because wDockCreate sets it (drawers only)
4781 /* restore auto-raise/lower state: same as scr->dock, no matter what */
4782 drawer->auto_raise_lower = scr->dock->auto_raise_lower;
4784 /* restore attract icons state */
4785 drawer->attract_icons = 0;
4786 value = WMGetFromPLDictionary(dock_state, dAutoAttractIcons);
4787 if (value && strcasecmp(WMGetFromPLString(value), "YES") == 0) {
4788 drawer->attract_icons = 1;
4789 scr->attracting_drawer = drawer;
4792 /* application list */
4793 apps = WMGetFromPLDictionary(dock_state, dApplications);
4795 if (!apps) {
4796 goto finish;
4799 count = WMGetPropListItemCount(apps);
4801 if (count == 0)
4802 goto finish;
4804 for (i=0; i<count; i++) {
4805 if (drawer->icon_count >= drawer->max_icons) {
4806 wwarning(_("there are too many icons stored in drawer. Ignoring what doesn't fit"));
4807 break;
4810 value = WMGetFromPLArray(apps, i);
4811 aicon = restore_icon_state(scr, value, WM_DRAWER, drawer->icon_count);
4813 drawer->icon_array[drawer->icon_count] = aicon;
4815 if (aicon) {
4816 aicon->dock = drawer;
4817 aicon->x_pos = drawer->x_pos + (aicon->xindex * ICON_SIZE);
4818 aicon->y_pos = drawer->y_pos + (aicon->yindex * ICON_SIZE);
4820 if (!drawer->lowered)
4821 ChangeStackingLevel(aicon->icon->core, WMDockLevel);
4822 else
4823 ChangeStackingLevel(aicon->icon->core, WMNormalLevel);
4825 wCoreConfigure(aicon->icon->core, aicon->x_pos, aicon->y_pos, 0, 0);
4827 if (!drawer->collapsed)
4828 XMapWindow(dpy, aicon->icon->core->window);
4829 wRaiseFrame(aicon->icon->core);
4831 drawer->icon_count++;
4835 finish:
4836 WMReleasePropList(drawer_state);
4838 return drawer;
4842 /* Same kind of comment than for previous function: this function is
4843 * very similar to make_icon_state, but has substential differences as
4844 * well. */
4845 static WMPropList *drawerSaveState(WScreen *scr, WDock *drawer)
4847 WMPropList *pstr, *drawer_state;
4848 WAppIcon *ai;
4849 char buffer[64];
4851 ai = drawer->icon_array[0];
4852 /* Store its name */
4853 pstr = WMCreatePLString(ai->wm_instance);
4854 drawer_state = WMCreatePLDictionary(dName, pstr, NULL); /* we need this final NULL */
4855 WMReleasePropList(pstr);
4857 /* Store its position */
4858 snprintf(buffer, sizeof(buffer), "%i,%i", ai->x_pos, ai->y_pos);
4859 pstr = WMCreatePLString(buffer);
4860 WMPutInPLDictionary(drawer_state, dPosition, pstr);
4861 WMReleasePropList(pstr);
4863 #ifdef XDND
4864 /* Store its DnD command */
4865 if (ai->dnd_command) {
4866 pstr = WMCreatePLString(ai->dnd_command);
4867 WMPutInPLDictionary(drawer_state, dDropCommand, pstr);
4868 WMReleasePropList(pstr);
4870 #endif /* XDND */
4872 /* Store its paste command */
4873 if (ai->paste_command) {
4874 pstr = WMCreatePLString(ai->paste_command);
4875 WMPutInPLDictionary(drawer_state, dPasteCommand, pstr);
4876 WMReleasePropList(pstr);
4879 /* Store applications list and other properties */
4880 pstr = dockSaveState(drawer);
4881 WMPutInPLDictionary(drawer_state, dDock, pstr);
4882 WMReleasePropList(pstr);
4884 return drawer_state;
4888 void wDrawersSaveState(WScreen *scr)
4890 WMPropList *all_drawers, *drawer_state;
4891 int i;
4892 WDrawerChain *dc;
4894 make_keys();
4896 all_drawers = WMCreatePLArray(NULL);
4897 for (i=0, dc = scr->drawers;
4898 i < scr->drawer_count;
4899 i++, dc = dc->next) {
4900 drawer_state = drawerSaveState(scr, dc->adrawer);
4901 WMAddToPLArray(all_drawers, drawer_state);
4902 WMReleasePropList(drawer_state);
4904 WMPutInPLDictionary(scr->session_state, dDrawers, all_drawers);
4905 WMReleasePropList(all_drawers);
4909 void wDrawersRestoreState(WScreen *scr)
4911 WMPropList *all_drawers, *drawer_state;
4912 int i;
4914 make_keys();
4916 if (scr->session_state == NULL)
4917 return;
4919 all_drawers = WMGetFromPLDictionary(scr->session_state, dDrawers);
4920 if (!all_drawers)
4921 return;
4923 for (i = 0; i < WMGetPropListItemCount(all_drawers); i++) {
4924 drawer_state = WMGetFromPLArray(all_drawers, i);
4925 drawerRestoreState(scr, drawer_state);
4926 // Note: scr->drawers was updated when the the drawer was created