wmaker: Moved function prototype to the appropriate header
[wmaker-crm.git] / src / dock.c
blob69bebde8aa09dfeaa8517583d74e4de8a7870eed
1 /* dock.c- built-in Dock module for WindowMaker
3 * Window Maker window manager
5 * Copyright (c) 1997-2003 Alfredo K. Kojima
6 * Copyright (c) 1998-2003 Dan Pascu
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
23 #include "wconfig.h"
25 #include <X11/Xlib.h>
26 #include <X11/Xutil.h>
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <libgen.h>
30 #include <string.h>
31 #include <strings.h>
32 #include <unistd.h>
33 #include <math.h>
34 #include <limits.h>
36 #ifndef PATH_MAX
37 #define PATH_MAX DEFAULT_PATH_MAX
38 #endif
40 #include "WindowMaker.h"
41 #include "wcore.h"
42 #include "window.h"
43 #include "icon.h"
44 #include "appicon.h"
45 #include "actions.h"
46 #include "stacking.h"
47 #include "dock.h"
48 #include "dockedapp.h"
49 #include "dialog.h"
50 #include "main.h"
51 #include "properties.h"
52 #include "menu.h"
53 #include "client.h"
54 #include "defaults.h"
55 #include "workspace.h"
56 #include "framewin.h"
57 #include "superfluous.h"
58 #include "xinerama.h"
59 #include "placement.h"
60 #include "misc.h"
61 #include "event.h"
63 /**** Local variables ****/
64 #define CLIP_REWIND 1
65 #define CLIP_IDLE 0
66 #define CLIP_FORWARD 2
68 /**** Global variables ****/
69 extern Cursor wCursor[WCUR_LAST];
70 extern XContext wWinContext;
72 #define MOD_MASK wPreferences.modifier_mask
73 #define ICON_SIZE wPreferences.icon_size
75 /***** Local variables ****/
77 static WMPropList *dCommand = NULL;
78 static WMPropList *dPasteCommand = NULL;
79 #ifdef XDND /* XXX was OFFIX */
80 static WMPropList *dDropCommand = NULL;
81 #endif
82 static WMPropList *dAutoLaunch, *dLock;
83 static WMPropList *dName, *dForced, *dBuggyApplication, *dYes, *dNo;
84 static WMPropList *dHost, *dDock, *dClip;
85 static WMPropList *dAutoAttractIcons;
87 static WMPropList *dPosition, *dApplications, *dLowered, *dCollapsed;
89 static WMPropList *dAutoCollapse, *dAutoRaiseLower, *dOmnipresent;
91 static WMPropList *dDrawers = NULL;
93 static void dockIconPaint(WAppIcon *btn);
95 static void iconMouseDown(WObjDescriptor *desc, XEvent *event);
97 static pid_t execCommand(WAppIcon *btn, const char *command, WSavedState *state);
99 static void trackDeadProcess(pid_t pid, unsigned char status, WDock *dock);
101 static int getClipButton(int px, int py);
103 static void toggleLowered(WDock *dock);
105 static void toggleCollapsed(WDock *dock);
107 static void clipIconExpose(WObjDescriptor *desc, XEvent *event);
109 static void clipLeave(WDock *dock);
111 static void handleClipChangeWorkspace(WScreen *scr, XEvent *event);
113 static void clipEnterNotify(WObjDescriptor *desc, XEvent *event);
114 static void clipLeaveNotify(WObjDescriptor *desc, XEvent *event);
115 static void clipAutoCollapse(void *cdata);
116 static void clipAutoExpand(void *cdata);
117 static void launchDockedApplication(WAppIcon *btn, Bool withSelection);
119 static void clipAutoLower(void *cdata);
120 static void clipAutoRaise(void *cdata);
121 static WAppIcon *mainIconCreate(WScreen *scr, int type, const char *name);
123 static void drawerIconExpose(WObjDescriptor *desc, XEvent *event);
124 static void removeDrawerCallback(WMenu *menu, WMenuEntry *entry);
125 static void drawerAppendToChain(WScreen *scr, WDock *drawer);
126 static char *findUniqueName(WScreen *scr, const char *instance_basename);
127 static void addADrawerCallback(WMenu *menu, WMenuEntry *entry);
128 static void swapDrawers(WScreen *scr, int on_right_side, int new_x);
129 static WDock* getDrawer(WScreen *scr, int y_index);
130 static int indexOfHole(WDock *drawer, WAppIcon *moving_aicon, int redocking);
131 static void drawerConsolidateIcons(WDock *drawer);
133 static int onScreen(WScreen *scr, int x, int y);
135 static void make_keys(void)
137 if (dCommand != NULL)
138 return;
140 dCommand = WMRetainPropList(WMCreatePLString("Command"));
141 dPasteCommand = WMRetainPropList(WMCreatePLString("PasteCommand"));
142 #ifdef XDND
143 dDropCommand = WMRetainPropList(WMCreatePLString("DropCommand"));
144 #endif
145 dLock = WMRetainPropList(WMCreatePLString("Lock"));
146 dAutoLaunch = WMRetainPropList(WMCreatePLString("AutoLaunch"));
147 dName = WMRetainPropList(WMCreatePLString("Name"));
148 dForced = WMRetainPropList(WMCreatePLString("Forced"));
149 dBuggyApplication = WMRetainPropList(WMCreatePLString("BuggyApplication"));
150 dYes = WMRetainPropList(WMCreatePLString("Yes"));
151 dNo = WMRetainPropList(WMCreatePLString("No"));
152 dHost = WMRetainPropList(WMCreatePLString("Host"));
154 dPosition = WMCreatePLString("Position");
155 dApplications = WMCreatePLString("Applications");
156 dLowered = WMCreatePLString("Lowered");
157 dCollapsed = WMCreatePLString("Collapsed");
158 dAutoCollapse = WMCreatePLString("AutoCollapse");
159 dAutoRaiseLower = WMCreatePLString("AutoRaiseLower");
160 dAutoAttractIcons = WMCreatePLString("AutoAttractIcons");
162 dOmnipresent = WMCreatePLString("Omnipresent");
164 dDock = WMCreatePLString("Dock");
165 dClip = WMCreatePLString("Clip");
166 dDrawers = WMCreatePLString("Drawers");
169 static void renameCallback(WMenu *menu, WMenuEntry *entry)
171 WDock *dock = entry->clientdata;
172 char buffer[128];
173 int wspace;
174 char *name;
176 assert(entry->clientdata != NULL);
178 wspace = dock->screen_ptr->current_workspace;
180 name = wstrdup(dock->screen_ptr->workspaces[wspace]->name);
182 snprintf(buffer, sizeof(buffer), _("Type the name for workspace %i:"), wspace + 1);
183 if (wInputDialog(dock->screen_ptr, _("Rename Workspace"), buffer, &name))
184 wWorkspaceRename(dock->screen_ptr, wspace, name);
186 wfree(name);
189 static void toggleLoweredCallback(WMenu *menu, WMenuEntry *entry)
191 assert(entry->clientdata != NULL);
193 toggleLowered(entry->clientdata);
195 entry->flags.indicator_on = !(((WDock *) entry->clientdata)->lowered);
197 wMenuPaint(menu);
200 static int matchWindow(const void *item, const void *cdata)
202 return (((WFakeGroupLeader *) item)->leader == (Window) cdata);
205 static void killCallback(WMenu *menu, WMenuEntry *entry)
207 WScreen *scr = menu->menu->screen_ptr;
208 WAppIcon *icon;
209 WFakeGroupLeader *fPtr;
210 char *buffer, *shortname, **argv;
211 int argc;
213 if (!WCHECK_STATE(WSTATE_NORMAL))
214 return;
216 assert(entry->clientdata != NULL);
218 icon = (WAppIcon *) entry->clientdata;
220 icon->editing = 1;
222 WCHANGE_STATE(WSTATE_MODAL);
224 /* strip away dir names */
225 shortname = basename(icon->command);
226 /* separate out command options */
227 wtokensplit(shortname, &argv, &argc);
229 buffer = wstrconcat(argv[0],
230 _(" will be forcibly closed.\n"
231 "Any unsaved changes will be lost.\n" "Please confirm."));
233 if (icon->icon && icon->icon->owner) {
234 fPtr = icon->icon->owner->fake_group;
235 } else {
236 /* is this really necessary? can we kill a non-running dock icon? */
237 Window win = icon->main_window;
238 int index;
240 index = WMFindInArray(scr->fakeGroupLeaders, matchWindow, (void *)win);
241 if (index != WANotFound)
242 fPtr = WMGetFromArray(scr->fakeGroupLeaders, index);
243 else
244 fPtr = NULL;
247 if (wPreferences.dont_confirm_kill
248 || wMessageDialog(menu->frame->screen_ptr, _("Kill Application"),
249 buffer, _("Yes"), _("No"), NULL) == WAPRDefault) {
250 if (fPtr != NULL) {
251 WWindow *wwin, *twin;
253 wwin = scr->focused_window;
254 while (wwin) {
255 twin = wwin->prev;
256 if (wwin->fake_group == fPtr)
257 wClientKill(wwin);
259 wwin = twin;
261 } else if (icon->icon && icon->icon->owner) {
262 wClientKill(icon->icon->owner);
266 wfree(buffer);
267 wtokenfree(argv, argc);
269 icon->editing = 0;
271 WCHANGE_STATE(WSTATE_NORMAL);
274 /* TODO: replace this function with a member of the dock struct */
275 static int numberOfSelectedIcons(WDock *dock)
277 WAppIcon *aicon;
278 int i, n;
280 n = 0;
281 for (i = 1; i < dock->max_icons; i++) {
282 aicon = dock->icon_array[i];
283 if (aicon && aicon->icon->selected)
284 n++;
287 return n;
290 static WMArray *getSelected(WDock *dock)
292 WMArray *ret = WMCreateArray(8);
293 WAppIcon *btn;
294 int i;
296 for (i = 1; i < dock->max_icons; i++) {
297 btn = dock->icon_array[i];
298 if (btn && btn->icon->selected)
299 WMAddToArray(ret, btn);
302 return ret;
305 static void paintClipButtons(WAppIcon *clipIcon, Bool lpushed, Bool rpushed)
307 Window win = clipIcon->icon->core->window;
308 WScreen *scr = clipIcon->icon->core->screen_ptr;
309 XPoint p[4];
310 int pt = CLIP_BUTTON_SIZE * ICON_SIZE / 64;
311 int tp = ICON_SIZE - pt;
312 int as = pt - 15; /* 15 = 5+5+5 */
313 GC gc = scr->draw_gc; /* maybe use WMColorGC() instead here? */
314 WMColor *color;
316 color = scr->clip_title_color[CLIP_NORMAL];
318 XSetForeground(dpy, gc, WMColorPixel(color));
320 if (rpushed) {
321 p[0].x = tp + 1;
322 p[0].y = 1;
323 p[1].x = ICON_SIZE - 2;
324 p[1].y = 1;
325 p[2].x = ICON_SIZE - 2;
326 p[2].y = pt - 1;
327 } else if (lpushed) {
328 p[0].x = 1;
329 p[0].y = tp;
330 p[1].x = pt;
331 p[1].y = ICON_SIZE - 2;
332 p[2].x = 1;
333 p[2].y = ICON_SIZE - 2;
335 if (lpushed || rpushed) {
336 XSetForeground(dpy, scr->draw_gc, scr->white_pixel);
337 XFillPolygon(dpy, win, scr->draw_gc, p, 3, Convex, CoordModeOrigin);
338 XSetForeground(dpy, scr->draw_gc, scr->black_pixel);
341 /* top right arrow */
342 p[0].x = p[3].x = ICON_SIZE - 5 - as;
343 p[0].y = p[3].y = 5;
344 p[1].x = ICON_SIZE - 6;
345 p[1].y = 5;
346 p[2].x = ICON_SIZE - 6;
347 p[2].y = 4 + as;
348 if (rpushed) {
349 XFillPolygon(dpy, win, scr->draw_gc, p, 3, Convex, CoordModeOrigin);
350 XDrawLines(dpy, win, scr->draw_gc, p, 4, CoordModeOrigin);
351 } else {
352 XFillPolygon(dpy, win, gc, p, 3, Convex, CoordModeOrigin);
353 XDrawLines(dpy, win, gc, p, 4, CoordModeOrigin);
356 /* bottom left arrow */
357 p[0].x = p[3].x = 5;
358 p[0].y = p[3].y = ICON_SIZE - 5 - as;
359 p[1].x = 5;
360 p[1].y = ICON_SIZE - 6;
361 p[2].x = 4 + as;
362 p[2].y = ICON_SIZE - 6;
363 if (lpushed) {
364 XFillPolygon(dpy, win, scr->draw_gc, p, 3, Convex, CoordModeOrigin);
365 XDrawLines(dpy, win, scr->draw_gc, p, 4, CoordModeOrigin);
366 } else {
367 XFillPolygon(dpy, win, gc, p, 3, Convex, CoordModeOrigin);
368 XDrawLines(dpy, win, gc, p, 4, CoordModeOrigin);
372 RImage *wClipMakeTile(WScreen *scr, RImage *normalTile)
374 RImage *tile = RCloneImage(normalTile);
375 RColor black;
376 RColor dark;
377 RColor light;
378 int pt, tp;
379 int as;
381 pt = CLIP_BUTTON_SIZE * wPreferences.icon_size / 64;
382 tp = wPreferences.icon_size - 1 - pt;
383 as = pt - 15;
385 black.alpha = 255;
386 black.red = black.green = black.blue = 0;
388 dark.alpha = 0;
389 dark.red = dark.green = dark.blue = 60;
391 light.alpha = 0;
392 light.red = light.green = light.blue = 80;
394 /* top right */
395 ROperateLine(tile, RSubtractOperation, tp, 0, wPreferences.icon_size - 2, pt - 1, &dark);
396 RDrawLine(tile, tp - 1, 0, wPreferences.icon_size - 1, pt + 1, &black);
397 ROperateLine(tile, RAddOperation, tp, 2, wPreferences.icon_size - 3, pt, &light);
399 /* arrow bevel */
400 ROperateLine(tile, RSubtractOperation, ICON_SIZE - 7 - as, 4, ICON_SIZE - 5, 4, &dark);
401 ROperateLine(tile, RSubtractOperation, ICON_SIZE - 6 - as, 5, ICON_SIZE - 5, 6 + as, &dark);
402 ROperateLine(tile, RAddOperation, ICON_SIZE - 5, 4, ICON_SIZE - 5, 6 + as, &light);
404 /* bottom left */
405 ROperateLine(tile, RAddOperation, 2, tp + 2, pt - 2, wPreferences.icon_size - 3, &dark);
406 RDrawLine(tile, 0, tp - 1, pt + 1, wPreferences.icon_size - 1, &black);
407 ROperateLine(tile, RSubtractOperation, 0, tp - 2, pt + 1, wPreferences.icon_size - 2, &light);
409 /* arrow bevel */
410 ROperateLine(tile, RSubtractOperation, 4, ICON_SIZE - 7 - as, 4, ICON_SIZE - 5, &dark);
411 ROperateLine(tile, RSubtractOperation, 5, ICON_SIZE - 6 - as, 6 + as, ICON_SIZE - 5, &dark);
412 ROperateLine(tile, RAddOperation, 4, ICON_SIZE - 5, 6 + as, ICON_SIZE - 5, &light);
414 return tile;
417 static void omnipresentCallback(WMenu *menu, WMenuEntry *entry)
419 WAppIcon *clickedIcon = entry->clientdata;
420 WAppIcon *aicon;
421 WDock *dock;
422 WMArray *selectedIcons;
423 WMArrayIterator iter;
424 int failed;
426 assert(entry->clientdata != NULL);
428 dock = clickedIcon->dock;
430 selectedIcons = getSelected(dock);
432 if (!WMGetArrayItemCount(selectedIcons))
433 WMAddToArray(selectedIcons, clickedIcon);
435 failed = 0;
436 WM_ITERATE_ARRAY(selectedIcons, aicon, iter) {
437 if (wClipMakeIconOmnipresent(aicon, !aicon->omnipresent) == WO_FAILED)
438 failed++;
439 else if (aicon->icon->selected)
440 wIconSelect(aicon->icon);
442 WMFreeArray(selectedIcons);
444 if (failed > 1) {
445 wMessageDialog(dock->screen_ptr, _("Warning"),
446 _("Some icons cannot be made omnipresent. "
447 "Please make sure that no other icon is "
448 "docked in the same positions on the other "
449 "workspaces and the Clip is not full in "
450 "some workspace."), _("OK"), NULL, NULL);
451 } else if (failed == 1) {
452 wMessageDialog(dock->screen_ptr, _("Warning"),
453 _("Icon cannot be made omnipresent. "
454 "Please make sure that no other icon is "
455 "docked in the same position on the other "
456 "workspaces and the Clip is not full in "
457 "some workspace."), _("OK"), NULL, NULL);
461 static void removeIcons(WMArray *icons, WDock *dock)
463 WAppIcon *aicon;
464 int keepit;
465 WMArrayIterator it;
467 WM_ITERATE_ARRAY(icons, aicon, it) {
468 keepit = aicon->running && wApplicationOf(aicon->main_window);
469 wDockDetach(dock, aicon);
470 if (keepit) {
471 /* XXX: can: aicon->icon == NULL ? */
472 PlaceIcon(dock->screen_ptr, &aicon->x_pos, &aicon->y_pos,
473 wGetHeadForWindow(aicon->icon->owner));
474 XMoveWindow(dpy, aicon->icon->core->window, aicon->x_pos, aicon->y_pos);
475 if (!dock->mapped || dock->collapsed)
476 XMapWindow(dpy, aicon->icon->core->window);
479 WMFreeArray(icons);
481 if (wPreferences.auto_arrange_icons)
482 wArrangeIcons(dock->screen_ptr, True);
485 static void removeIconsCallback(WMenu *menu, WMenuEntry *entry)
487 WAppIcon *clickedIcon = (WAppIcon *) entry->clientdata;
488 WDock *dock;
489 WMArray *selectedIcons;
491 assert(clickedIcon != NULL);
493 dock = clickedIcon->dock;
495 selectedIcons = getSelected(dock);
497 if (WMGetArrayItemCount(selectedIcons)) {
498 if (wMessageDialog(dock->screen_ptr,
499 dock->type == WM_CLIP ? _("Workspace Clip") : _("Drawer"),
500 _("All selected icons will be removed!"),
501 _("OK"), _("Cancel"), NULL) != WAPRDefault) {
502 WMFreeArray(selectedIcons);
503 return;
505 } else {
506 if (clickedIcon->xindex == 0 && clickedIcon->yindex == 0) {
507 WMFreeArray(selectedIcons);
508 return;
510 WMAddToArray(selectedIcons, clickedIcon);
513 removeIcons(selectedIcons, dock);
515 if (dock->type == WM_DRAWER) {
516 drawerConsolidateIcons(dock);
520 static void keepIconsCallback(WMenu *menu, WMenuEntry *entry)
522 WAppIcon *clickedIcon = (WAppIcon *) entry->clientdata;
523 WDock *dock;
524 WAppIcon *aicon;
525 WMArray *selectedIcons;
526 WMArrayIterator it;
528 assert(clickedIcon != NULL);
529 dock = clickedIcon->dock;
531 selectedIcons = getSelected(dock);
533 if (!WMGetArrayItemCount(selectedIcons)
534 && clickedIcon != dock->screen_ptr->clip_icon) {
535 char *command = NULL;
537 if (!clickedIcon->command && !clickedIcon->editing) {
538 clickedIcon->editing = 1;
539 if (wInputDialog(dock->screen_ptr, _("Keep Icon"),
540 _("Type the command used to launch the application"), &command)) {
541 if (command && (command[0] == 0 || (command[0] == '-' && command[1] == 0))) {
542 wfree(command);
543 command = NULL;
545 clickedIcon->command = command;
546 clickedIcon->editing = 0;
547 } else {
548 clickedIcon->editing = 0;
549 if (command)
550 wfree(command);
551 WMFreeArray(selectedIcons);
552 return;
556 WMAddToArray(selectedIcons, clickedIcon);
559 WM_ITERATE_ARRAY(selectedIcons, aicon, it) {
560 if (aicon->icon->selected)
561 wIconSelect(aicon->icon);
563 if (aicon && aicon->attracted && aicon->command) {
564 aicon->attracted = 0;
565 if (aicon->icon->shadowed) {
566 aicon->icon->shadowed = 0;
569 * Update icon pixmap, RImage doesn't change,
570 * so call wIconUpdate is not needed
572 update_icon_pixmap(aicon->icon);
574 /* Paint it */
575 wAppIconPaint(aicon);
578 save_appicon(aicon, True);
580 WMFreeArray(selectedIcons);
583 static void toggleAutoAttractCallback(WMenu *menu, WMenuEntry *entry)
585 WDock *dock = (WDock *) entry->clientdata;
586 WScreen *scr = dock->screen_ptr;
588 assert(entry->clientdata != NULL);
590 /* Setting a clip as autoattracting will disable an autoattracting drawer,
591 * and reciprocally. Ask for confirmation? */
592 #ifdef CONFIRM_AUTOATTRACT_CHANGE
593 if (dock->type == WM_CLIP && scr->attracting_drawer) {
594 if (wMessageDialog(scr, _("Workspace Clip"),
595 _("The drawer that auto-attracted icons so far won't do so anymore!"),
596 _("OK"), _("Cancel"), NULL) != WAPRDefault)
597 return;
598 } else if (dock->type == WM_DRAWER) {
599 /* check if at least one clip already auto attracts icons */
600 int i, ask_for_confirmation = False;
601 for (i = 0; i < scr->workspace_count; i++) {
602 if (scr->workspaces[ i ]->clip->attract_icons) {
603 ask_for_confirmation = True;
604 break;
607 if (ask_for_confirmation) {
608 if (wMessageDialog(scr, _("Drawer"),
609 _("Any clips that auto-attracted icons so far won't do so anymore!"),
610 _("OK"), _("Cancel"), NULL) != WAPRDefault)
611 return;
614 #endif
616 dock->attract_icons = !dock->attract_icons;
618 entry->flags.indicator_on = dock->attract_icons;
620 wMenuPaint(menu);
622 if (dock->attract_icons) {
623 if (dock->type == WM_DRAWER) {
624 /* The newly auto-attracting dock is a drawer: disable any clip and
625 * previously attracting drawer */
626 int i;
627 for (i = 0; i < scr->workspace_count; i++) {
628 scr->workspaces[ i ]->clip->attract_icons = False;
629 /* dock menu will be updated later, when opened */
631 if (scr->attracting_drawer != NULL)
632 scr->attracting_drawer->attract_icons = False;
633 scr->attracting_drawer = dock;
634 } else {
635 /* The newly auto-attracting dock is a clip: disable
636 * previously attracting drawer, if applicable */
637 if (scr->attracting_drawer != NULL) {
638 scr->attracting_drawer->attract_icons = False;
639 /* again, its menu will be updated, later. */
640 scr->attracting_drawer = NULL;
646 static void selectCallback(WMenu *menu, WMenuEntry *entry)
648 WAppIcon *icon = (WAppIcon *) entry->clientdata;
650 assert(icon != NULL);
652 wIconSelect(icon->icon);
654 wMenuPaint(menu);
657 static void colectIconsCallback(WMenu *menu, WMenuEntry *entry)
659 WAppIcon *clickedIcon = (WAppIcon *) entry->clientdata;
660 WDock *clip; /* clip... is a WM_CLIP or a WM_DRAWER */
661 WAppIcon *aicon;
662 int x, y, x_pos, y_pos;
663 Bool update_icon = False;
665 assert(entry->clientdata != NULL);
666 clip = clickedIcon->dock;
668 aicon = clip->screen_ptr->app_icon_list;
670 while (aicon) {
671 if (!aicon->docked && wDockFindFreeSlot(clip, &x, &y)) {
672 x_pos = clip->x_pos + x * ICON_SIZE;
673 y_pos = clip->y_pos + y * ICON_SIZE;
674 if (aicon->x_pos != x_pos || aicon->y_pos != y_pos)
675 move_window(aicon->icon->core->window, aicon->x_pos, aicon->y_pos, x_pos, y_pos);
677 aicon->attracted = 1;
678 if (!aicon->icon->shadowed) {
679 aicon->icon->shadowed = 1;
680 update_icon = True;
682 wDockAttachIcon(clip, aicon, x, y, update_icon);
683 if (clip->collapsed || !clip->mapped)
684 XUnmapWindow(dpy, aicon->icon->core->window);
686 aicon = aicon->next;
690 static void selectIconsCallback(WMenu *menu, WMenuEntry *entry)
692 WAppIcon *clickedIcon = (WAppIcon *) entry->clientdata;
693 WDock *dock;
694 WMArray *selectedIcons;
695 WMArrayIterator iter;
696 WAppIcon *btn;
697 int i;
699 assert(clickedIcon != NULL);
700 dock = clickedIcon->dock;
702 selectedIcons = getSelected(dock);
704 if (!WMGetArrayItemCount(selectedIcons)) {
705 for (i = 1; i < dock->max_icons; i++) {
706 btn = dock->icon_array[i];
707 if (btn && !btn->icon->selected)
708 wIconSelect(btn->icon);
710 } else {
711 WM_ITERATE_ARRAY(selectedIcons, btn, iter) {
712 wIconSelect(btn->icon);
715 WMFreeArray(selectedIcons);
717 wMenuPaint(menu);
720 static void toggleCollapsedCallback(WMenu *menu, WMenuEntry *entry)
722 assert(entry->clientdata != NULL);
724 toggleCollapsed(entry->clientdata);
726 entry->flags.indicator_on = ((WDock *) entry->clientdata)->collapsed;
728 wMenuPaint(menu);
731 static void toggleAutoCollapseCallback(WMenu *menu, WMenuEntry *entry)
733 WDock *dock;
734 assert(entry->clientdata != NULL);
736 dock = (WDock *) entry->clientdata;
738 dock->auto_collapse = !dock->auto_collapse;
740 entry->flags.indicator_on = ((WDock *) entry->clientdata)->auto_collapse;
742 wMenuPaint(menu);
745 static void toggleAutoRaiseLower(WDock *dock)
747 WDrawerChain *dc;
749 dock->auto_raise_lower = !dock->auto_raise_lower;
750 if (dock->type == WM_DOCK)
752 for (dc = dock->screen_ptr->drawers; dc != NULL; dc = dc->next) {
753 toggleAutoRaiseLower(dc->adrawer);
758 static void toggleAutoRaiseLowerCallback(WMenu *menu, WMenuEntry *entry)
760 WDock *dock;
761 assert(entry->clientdata != NULL);
763 dock = (WDock *) entry->clientdata;
765 toggleAutoRaiseLower(dock);
767 entry->flags.indicator_on = ((WDock *) entry->clientdata)->auto_raise_lower;
769 wMenuPaint(menu);
772 static void launchCallback(WMenu *menu, WMenuEntry *entry)
774 WAppIcon *btn = (WAppIcon *) entry->clientdata;
776 launchDockedApplication(btn, False);
779 static void settingsCallback(WMenu *menu, WMenuEntry *entry)
781 WAppIcon *btn = (WAppIcon *) entry->clientdata;
783 if (btn->editing)
784 return;
785 ShowDockAppSettingsPanel(btn);
788 static void hideCallback(WMenu *menu, WMenuEntry *entry)
790 WApplication *wapp;
791 WAppIcon *btn = (WAppIcon *) entry->clientdata;
793 wapp = wApplicationOf(btn->icon->owner->main_window);
795 if (wapp->flags.hidden) {
796 wWorkspaceChange(btn->icon->core->screen_ptr, wapp->last_workspace);
797 wUnhideApplication(wapp, False, False);
798 } else {
799 wHideApplication(wapp);
803 static void unhideHereCallback(WMenu *menu, WMenuEntry *entry)
805 WApplication *wapp;
806 WAppIcon *btn = (WAppIcon *) entry->clientdata;
808 wapp = wApplicationOf(btn->icon->owner->main_window);
810 wUnhideApplication(wapp, False, True);
813 /* Name is only used when type == WM_DRAWER and when restoring a specific
814 * drawer, with a specific name. When creating a drawer, leave name to NULL
815 * and mainIconCreate will find the first unused unique name */
816 static WAppIcon *mainIconCreate(WScreen *scr, int type, const char *name)
818 WAppIcon *btn;
819 int x_pos;
821 switch(type) {
822 case WM_CLIP:
823 if (scr->clip_icon)
824 return scr->clip_icon;
825 btn = wAppIconCreateForDock(scr, NULL, "Logo", "WMClip", TILE_CLIP);
826 btn->icon->core->descriptor.handle_expose = clipIconExpose;
827 x_pos = 0;
828 break;
829 case WM_DOCK:
830 default: /* to avoid a warning about btn and x_pos, basically */
831 btn = wAppIconCreateForDock(scr, NULL, "Logo", "WMDock", TILE_NORMAL);
832 if (wPreferences.flags.clip_merged_in_dock)
833 btn->icon->core->descriptor.handle_expose = clipIconExpose;
834 x_pos = scr->scr_width - ICON_SIZE - DOCK_EXTRA_SPACE;
835 break;
836 case WM_DRAWER:
837 if (name == NULL)
838 name = findUniqueName(scr, "Drawer");
839 btn = wAppIconCreateForDock(scr, NULL, name, "WMDrawer", TILE_DRAWER);
840 btn->icon->core->descriptor.handle_expose = drawerIconExpose;
841 x_pos = 0;
844 btn->xindex = 0;
845 btn->yindex = 0;
847 btn->icon->core->descriptor.handle_mousedown = iconMouseDown;
848 btn->icon->core->descriptor.handle_enternotify = clipEnterNotify;
849 btn->icon->core->descriptor.handle_leavenotify = clipLeaveNotify;
850 btn->icon->core->descriptor.parent_type = WCLASS_DOCK_ICON;
851 btn->icon->core->descriptor.parent = btn;
852 XMapWindow(dpy, btn->icon->core->window);
853 btn->x_pos = x_pos;
854 btn->y_pos = 0;
855 btn->docked = 1;
856 if (type == WM_CLIP ||
857 (type == WM_DOCK && wPreferences.flags.clip_merged_in_dock))
858 scr->clip_icon = btn;
860 return btn;
863 static void switchWSCommand(WMenu *menu, WMenuEntry *entry)
865 WAppIcon *btn, *icon = (WAppIcon *) entry->clientdata;
866 WScreen *scr = icon->icon->core->screen_ptr;
867 WDock *src, *dest;
868 WMArray *selectedIcons;
869 int x, y;
871 if (entry->order == scr->current_workspace)
872 return;
873 src = icon->dock;
874 dest = scr->workspaces[entry->order]->clip;
876 selectedIcons = getSelected(src);
878 if (WMGetArrayItemCount(selectedIcons)) {
879 WMArrayIterator iter;
881 WM_ITERATE_ARRAY(selectedIcons, btn, iter) {
882 if (wDockFindFreeSlot(dest, &x, &y)) {
883 wDockMoveIconBetweenDocks(src, dest, btn, x, y);
884 XUnmapWindow(dpy, btn->icon->core->window);
887 } else if (icon != scr->clip_icon) {
888 if (wDockFindFreeSlot(dest, &x, &y)) {
889 wDockMoveIconBetweenDocks(src, dest, icon, x, y);
890 XUnmapWindow(dpy, icon->icon->core->window);
893 WMFreeArray(selectedIcons);
896 static void launchDockedApplication(WAppIcon *btn, Bool withSelection)
898 WScreen *scr = btn->icon->core->screen_ptr;
900 if (!btn->launching &&
901 ((!withSelection && btn->command != NULL) || (withSelection && btn->paste_command != NULL))) {
902 if (!btn->forced_dock) {
903 btn->relaunching = btn->running;
904 btn->running = 1;
906 if (btn->wm_instance || btn->wm_class) {
907 WWindowAttributes attr;
908 memset(&attr, 0, sizeof(WWindowAttributes));
909 wDefaultFillAttributes(btn->wm_instance, btn->wm_class, &attr, NULL, True);
911 if (!attr.no_appicon && !btn->buggy_app)
912 btn->launching = 1;
913 else
914 btn->running = 0;
916 btn->drop_launch = 0;
917 btn->paste_launch = withSelection;
918 scr->last_dock = btn->dock;
919 btn->pid = execCommand(btn, (withSelection ? btn->paste_command : btn->command), NULL);
920 if (btn->pid > 0) {
921 if (btn->buggy_app) {
922 /* give feedback that the app was launched */
923 btn->launching = 1;
924 dockIconPaint(btn);
925 btn->launching = 0;
926 WMAddTimerHandler(200, (WMCallback *) dockIconPaint, btn);
927 } else {
928 dockIconPaint(btn);
930 } else {
931 wwarning(_("could not launch application %s"), btn->command);
932 btn->launching = 0;
933 if (!btn->relaunching)
934 btn->running = 0;
939 static void updateWorkspaceMenu(WMenu *menu, WAppIcon *icon)
941 WScreen *scr = menu->frame->screen_ptr;
942 char title[MAX_WORKSPACENAME_WIDTH + 1];
943 int i;
945 if (!menu || !icon)
946 return;
948 for (i = 0; i < scr->workspace_count; i++) {
949 if (i < menu->entry_no) {
950 if (strcmp(menu->entries[i]->text, scr->workspaces[i]->name) != 0) {
951 wfree(menu->entries[i]->text);
952 strcpy(title, scr->workspaces[i]->name);
953 menu->entries[i]->text = wstrdup(title);
954 menu->flags.realized = 0;
956 menu->entries[i]->clientdata = (void *)icon;
957 } else {
958 strcpy(title, scr->workspaces[i]->name);
960 wMenuAddCallback(menu, title, switchWSCommand, (void *)icon);
962 menu->flags.realized = 0;
965 if (i == scr->current_workspace)
966 wMenuSetEnabled(menu, i, False);
967 else
968 wMenuSetEnabled(menu, i, True);
971 if (!menu->flags.realized)
972 wMenuRealize(menu);
975 static WMenu *makeWorkspaceMenu(WScreen *scr)
977 WMenu *menu;
979 menu = wMenuCreate(scr, NULL, False);
980 if (!menu)
981 wwarning(_("could not create workspace submenu for Clip menu"));
983 wMenuAddCallback(menu, "", switchWSCommand, (void *)scr->clip_icon);
985 menu->flags.realized = 0;
986 wMenuRealize(menu);
988 return menu;
991 static void updateClipOptionsMenu(WMenu *menu, WDock *dock)
993 WMenuEntry *entry;
994 int index = 0;
996 if (!menu || !dock)
997 return;
999 /* keep on top */
1000 entry = menu->entries[index];
1001 entry->flags.indicator_on = !dock->lowered;
1002 entry->clientdata = dock;
1003 wMenuSetEnabled(menu, index, dock->type == WM_CLIP);
1005 /* collapsed */
1006 entry = menu->entries[++index];
1007 entry->flags.indicator_on = dock->collapsed;
1008 entry->clientdata = dock;
1010 /* auto-collapse */
1011 entry = menu->entries[++index];
1012 entry->flags.indicator_on = dock->auto_collapse;
1013 entry->clientdata = dock;
1015 /* auto-raise/lower */
1016 entry = menu->entries[++index];
1017 entry->flags.indicator_on = dock->auto_raise_lower;
1018 entry->clientdata = dock;
1019 wMenuSetEnabled(menu, index, dock->lowered && (dock->type == WM_CLIP));
1021 /* attract icons */
1022 entry = menu->entries[++index];
1023 entry->flags.indicator_on = dock->attract_icons;
1024 entry->clientdata = dock;
1026 menu->flags.realized = 0;
1027 wMenuRealize(menu);
1031 static WMenu *makeClipOptionsMenu(WScreen *scr)
1033 WMenu *menu;
1034 WMenuEntry *entry;
1036 menu = wMenuCreate(scr, NULL, False);
1037 if (!menu) {
1038 wwarning(_("could not create options submenu for Clip menu"));
1039 return NULL;
1042 entry = wMenuAddCallback(menu, _("Keep on Top"), toggleLoweredCallback, NULL);
1043 entry->flags.indicator = 1;
1044 entry->flags.indicator_on = 1;
1045 entry->flags.indicator_type = MI_CHECK;
1047 entry = wMenuAddCallback(menu, _("Collapsed"), toggleCollapsedCallback, NULL);
1048 entry->flags.indicator = 1;
1049 entry->flags.indicator_on = 1;
1050 entry->flags.indicator_type = MI_CHECK;
1052 entry = wMenuAddCallback(menu, _("Autocollapse"), toggleAutoCollapseCallback, NULL);
1053 entry->flags.indicator = 1;
1054 entry->flags.indicator_on = 1;
1055 entry->flags.indicator_type = MI_CHECK;
1057 entry = wMenuAddCallback(menu, _("Autoraise"), toggleAutoRaiseLowerCallback, NULL);
1058 entry->flags.indicator = 1;
1059 entry->flags.indicator_on = 1;
1060 entry->flags.indicator_type = MI_CHECK;
1062 entry = wMenuAddCallback(menu, _("Autoattract Icons"), toggleAutoAttractCallback, NULL);
1063 entry->flags.indicator = 1;
1064 entry->flags.indicator_on = 1;
1065 entry->flags.indicator_type = MI_CHECK;
1067 menu->flags.realized = 0;
1068 wMenuRealize(menu);
1070 return menu;
1074 static void setDockPositionNormalCallback(WMenu *menu, WMenuEntry *entry)
1076 WDock *dock = (WDock *) entry->clientdata;
1077 WDrawerChain *dc;
1078 if (entry->flags.indicator_on) // already set, nothing to do
1079 return;
1080 // Do we come from auto raise lower or keep on top?
1081 if (dock->auto_raise_lower)
1083 dock->auto_raise_lower = 0;
1084 // Only for aesthetic purposes, can be removed when Autoraise status is no longer exposed in drawer option menu
1085 for (dc = dock->screen_ptr->drawers; dc != NULL; dc = dc->next) {
1086 dc->adrawer->auto_raise_lower = 0;
1089 else
1091 // Will take care of setting lowered = 0 in drawers
1092 toggleLowered(dock);
1094 entry->flags.indicator_on = 1;
1097 static void setDockPositionAutoRaiseLowerCallback(WMenu *menu, WMenuEntry *entry)
1099 WDock *dock = (WDock *) entry->clientdata;
1100 WDrawerChain *dc;
1101 if (entry->flags.indicator_on) // already set, nothing to do
1102 return;
1103 // Do we come from normal or keep on top?
1104 if (!dock->lowered)
1106 toggleLowered(dock);
1108 dock->auto_raise_lower = 1;
1109 // Only for aesthetic purposes, can be removed when Autoraise status is no longer exposed in drawer option menu
1110 for (dc = dock->screen_ptr->drawers; dc != NULL; dc = dc->next) {
1111 dc->adrawer->auto_raise_lower = 1;
1113 entry->flags.indicator_on = 1;
1116 static void setDockPositionKeepOnTopCallback(WMenu *menu, WMenuEntry *entry)
1118 WDock *dock = (WDock *) entry->clientdata;
1119 WDrawerChain *dc;
1120 if (entry->flags.indicator_on) // already set, nothing to do
1121 return;
1122 dock->auto_raise_lower = 0;
1123 // Only for aesthetic purposes, can be removed when Autoraise status is no longer exposed in drawer option menu
1124 for (dc = dock->screen_ptr->drawers; dc != NULL; dc = dc->next) {
1125 dc->adrawer->auto_raise_lower = 0;
1127 toggleLowered(dock);
1128 entry->flags.indicator_on = 1;
1131 static void updateDockPositionMenu(WMenu *menu, WDock *dock)
1133 WMenuEntry *entry;
1134 int index = 0;
1136 assert(menu);
1137 assert(dock);
1139 /* Normal level */
1140 entry = menu->entries[index++];
1141 entry->flags.indicator_on = (dock->lowered && !dock->auto_raise_lower);
1142 entry->clientdata = dock;
1144 /* Auto-raise/lower */
1145 entry = menu->entries[index++];
1146 entry->flags.indicator_on = dock->auto_raise_lower;
1147 entry->clientdata = dock;
1149 /* Keep on top */
1150 entry = menu->entries[index++];
1151 entry->flags.indicator_on = !dock->lowered;
1152 entry->clientdata = dock;
1155 static WMenu *makeDockPositionMenu(WScreen *scr)
1157 /* When calling this, the dock is being created, so scr->dock is still not set
1158 * Therefore the callbacks' clientdata and the indicators can't be set,
1159 * they will be updated when the dock menu is opened. */
1160 WMenu *menu;
1161 WMenuEntry *entry;
1163 menu = wMenuCreate(scr, NULL, False);
1164 if (!menu) {
1165 wwarning(_("could not create options submenu for dock position menu"));
1166 return NULL;
1169 entry = wMenuAddCallback(menu, _("Normal"), setDockPositionNormalCallback, NULL);
1170 entry->flags.indicator = 1;
1171 entry->flags.indicator_type = MI_DIAMOND;
1173 entry = wMenuAddCallback(menu, _("Auto raise & lower"), setDockPositionAutoRaiseLowerCallback, NULL);
1174 entry->flags.indicator = 1;
1175 entry->flags.indicator_type = MI_DIAMOND;
1177 entry = wMenuAddCallback(menu, _("Keep on Top"), setDockPositionKeepOnTopCallback, NULL);
1178 entry->flags.indicator = 1;
1179 entry->flags.indicator_type = MI_DIAMOND;
1181 menu->flags.realized = 0;
1182 wMenuRealize(menu);
1184 return menu;
1188 static WMenu *dockMenuCreate(WScreen *scr, int type)
1190 WMenu *menu;
1191 WMenuEntry *entry;
1193 if (type == WM_CLIP && scr->clip_menu)
1194 return scr->clip_menu;
1196 if (type == WM_DRAWER && scr->drawer_menu)
1197 return scr->drawer_menu;
1199 menu = wMenuCreate(scr, NULL, False);
1200 if (type == WM_DOCK) {
1201 entry = wMenuAddCallback(menu, _("Dock position"), NULL, NULL);
1202 if (scr->dock_pos_menu == NULL)
1203 scr->dock_pos_menu = makeDockPositionMenu(scr);
1204 wMenuEntrySetCascade(menu, entry, scr->dock_pos_menu);
1206 if (!wPreferences.flags.nodrawer) {
1207 entry = wMenuAddCallback(menu, _("Add a drawer"), addADrawerCallback, NULL);
1209 } else {
1210 if (type == WM_CLIP)
1211 entry = wMenuAddCallback(menu, _("Clip Options"), NULL, NULL);
1212 else /* if (type == WM_DRAWER) */
1213 entry = wMenuAddCallback(menu, _("Drawer options"), NULL, NULL);
1215 if (scr->clip_options == NULL)
1216 scr->clip_options = makeClipOptionsMenu(scr);
1217 wMenuEntrySetCascade(menu, entry, scr->clip_options);
1219 /* The same menu is used for the dock and its appicons. If the menu
1220 * entry text is different between the two contexts, or if it can
1221 * change depending on some state, free the duplicated string (from
1222 * wMenuInsertCallback) and use gettext's string */
1223 if (type == WM_CLIP) {
1224 entry = wMenuAddCallback(menu, _("Rename Workspace"), renameCallback, NULL);
1225 wfree(entry->text);
1226 entry->text = _("Rename Workspace"); /* can be: (Toggle) Omnipresent */
1229 entry = wMenuAddCallback(menu, _("Selected"), selectCallback, NULL);
1230 entry->flags.indicator = 1;
1231 entry->flags.indicator_on = 1;
1232 entry->flags.indicator_type = MI_CHECK;
1234 entry = wMenuAddCallback(menu, _("Select All Icons"), selectIconsCallback, NULL);
1235 wfree(entry->text);
1236 entry->text = _("Select All Icons"); /* can be: Unselect all icons */
1238 entry = wMenuAddCallback(menu, _("Keep Icon"), keepIconsCallback, NULL);
1239 wfree(entry->text);
1240 entry->text = _("Keep Icon"); /* can be: Keep Icons */
1242 if (type == WM_CLIP) {
1243 entry = wMenuAddCallback(menu, _("Move Icon To"), NULL, NULL);
1244 wfree(entry->text);
1245 entry->text = _("Move Icon To"); /* can be: Move Icons to */
1246 scr->clip_submenu = makeWorkspaceMenu(scr);
1247 if (scr->clip_submenu)
1248 wMenuEntrySetCascade(menu, entry, scr->clip_submenu);
1251 entry = wMenuAddCallback(menu, _("Remove Icon"), removeIconsCallback, NULL);
1252 wfree(entry->text);
1253 entry->text = _("Remove Icon"); /* can be: Remove Icons */
1255 wMenuAddCallback(menu, _("Attract Icons"), colectIconsCallback, NULL);
1258 wMenuAddCallback(menu, _("Launch"), launchCallback, NULL);
1260 wMenuAddCallback(menu, _("Unhide Here"), unhideHereCallback, NULL);
1262 entry = wMenuAddCallback(menu, _("Hide"), hideCallback, NULL);
1263 wfree(entry->text);
1264 entry->text = _("Hide"); /* can be: Unhide */
1266 wMenuAddCallback(menu, _("Settings..."), settingsCallback, NULL);
1268 entry = wMenuAddCallback(menu, _("Kill"), killCallback, NULL);
1269 wfree(entry->text);
1270 entry->text = _("Kill"); /* can be: Remove drawer */
1272 if (type == WM_CLIP)
1273 scr->clip_menu = menu;
1275 if (type == WM_DRAWER)
1276 scr->drawer_menu = menu;
1278 return menu;
1281 WDock *wDockCreate(WScreen *scr, int type, const char *name)
1283 WDock *dock;
1284 WAppIcon *btn;
1286 make_keys();
1288 dock = wmalloc(sizeof(WDock));
1290 switch (type) {
1291 case WM_CLIP:
1292 dock->max_icons = DOCK_MAX_ICONS;
1293 break;
1294 case WM_DRAWER:
1295 dock->max_icons = scr->scr_width / wPreferences.icon_size;
1296 break;
1297 case WM_DOCK:
1298 default:
1299 dock->max_icons = scr->scr_height / wPreferences.icon_size;
1302 dock->icon_array = wmalloc(sizeof(WAppIcon *) * dock->max_icons);
1304 btn = mainIconCreate(scr, type, name);
1306 btn->dock = dock;
1308 dock->x_pos = btn->x_pos;
1309 dock->y_pos = btn->y_pos;
1310 dock->screen_ptr = scr;
1311 dock->type = type;
1312 dock->icon_count = 1;
1313 if (type == WM_DRAWER)
1314 dock->on_right_side = scr->dock->on_right_side;
1315 else
1316 dock->on_right_side = 1;
1317 dock->collapsed = 0;
1318 dock->auto_collapse = 0;
1319 dock->auto_collapse_magic = NULL;
1320 dock->auto_raise_lower = 0;
1321 dock->auto_lower_magic = NULL;
1322 dock->auto_raise_magic = NULL;
1323 dock->attract_icons = 0;
1324 dock->lowered = 1;
1325 dock->icon_array[0] = btn;
1326 wRaiseFrame(btn->icon->core);
1327 XMoveWindow(dpy, btn->icon->core->window, btn->x_pos, btn->y_pos);
1329 /* create dock menu */
1330 dock->menu = dockMenuCreate(scr, type);
1332 if (type == WM_DRAWER) {
1333 drawerAppendToChain(scr, dock);
1334 dock->auto_collapse = 1;
1337 return dock;
1340 void wDockDestroy(WDock *dock)
1342 int i;
1343 WAppIcon *aicon;
1345 for (i = (dock->type == WM_CLIP) ? 1 : 0; i < dock->max_icons; i++) {
1346 aicon = dock->icon_array[i];
1347 if (aicon) {
1348 int keepit = aicon->running && wApplicationOf(aicon->main_window);
1349 wDockDetach(dock, aicon);
1350 if (keepit) {
1351 /* XXX: can: aicon->icon == NULL ? */
1352 PlaceIcon(dock->screen_ptr, &aicon->x_pos, &aicon->y_pos,
1353 wGetHeadForWindow(aicon->icon->owner));
1354 XMoveWindow(dpy, aicon->icon->core->window, aicon->x_pos, aicon->y_pos);
1355 if (!dock->mapped || dock->collapsed)
1356 XMapWindow(dpy, aicon->icon->core->window);
1360 if (wPreferences.auto_arrange_icons)
1361 wArrangeIcons(dock->screen_ptr, True);
1362 wfree(dock->icon_array);
1363 if (dock->menu && dock->type != WM_CLIP)
1364 wMenuDestroy(dock->menu, True);
1365 if (dock->screen_ptr->last_dock == dock)
1366 dock->screen_ptr->last_dock = NULL;
1367 wfree(dock);
1370 void wClipIconPaint(WAppIcon *aicon)
1372 WScreen *scr = aicon->icon->core->screen_ptr;
1373 WWorkspace *workspace = scr->workspaces[scr->current_workspace];
1374 WMColor *color;
1375 Window win = aicon->icon->core->window;
1376 int length, nlength;
1377 char *ws_name, ws_number[10];
1378 int ty, tx;
1380 wIconPaint(aicon->icon);
1382 length = strlen(workspace->name);
1383 ws_name = wmalloc(length + 1);
1384 snprintf(ws_name, length + 1, "%s", workspace->name);
1385 snprintf(ws_number, sizeof(ws_number), "%i", scr->current_workspace + 1);
1386 nlength = strlen(ws_number);
1388 if (wPreferences.flags.noclip || !workspace->clip->collapsed)
1389 color = scr->clip_title_color[CLIP_NORMAL];
1390 else
1391 color = scr->clip_title_color[CLIP_COLLAPSED];
1393 ty = ICON_SIZE - WMFontHeight(scr->clip_title_font) - 3;
1395 tx = CLIP_BUTTON_SIZE * ICON_SIZE / 64;
1397 if(wPreferences.show_clip_title)
1398 WMDrawString(scr->wmscreen, win, color, scr->clip_title_font, tx, ty, ws_name, length);
1400 tx = (ICON_SIZE / 2 - WMWidthOfString(scr->clip_title_font, ws_number, nlength)) / 2;
1402 WMDrawString(scr->wmscreen, win, color, scr->clip_title_font, tx, 2, ws_number, nlength);
1404 wfree(ws_name);
1406 if (aicon->launching)
1407 XFillRectangle(dpy, aicon->icon->core->window, scr->stipple_gc,
1408 0, 0, wPreferences.icon_size, wPreferences.icon_size);
1410 paintClipButtons(aicon, aicon->dock->lclip_button_pushed, aicon->dock->rclip_button_pushed);
1413 static void clipIconExpose(WObjDescriptor *desc, XEvent *event)
1415 wClipIconPaint(desc->parent);
1418 static void dockIconPaint(WAppIcon *btn)
1420 if (btn == btn->icon->core->screen_ptr->clip_icon) {
1421 wClipIconPaint(btn);
1422 } else if (wIsADrawer(btn->icon->core->screen_ptr, btn)) {
1423 wDrawerIconPaint(btn);
1424 } else {
1425 wAppIconPaint(btn);
1426 save_appicon(btn, True);
1430 static WMPropList *make_icon_state(WAppIcon *btn)
1432 WMPropList *node = NULL;
1433 WMPropList *command, *autolaunch, *lock, *name, *forced;
1434 WMPropList *position, *buggy, *omnipresent;
1435 char *tmp;
1436 char buffer[64];
1438 if (btn) {
1439 if (!btn->command)
1440 command = WMCreatePLString("-");
1441 else
1442 command = WMCreatePLString(btn->command);
1444 autolaunch = btn->auto_launch ? dYes : dNo;
1446 lock = btn->lock ? dYes : dNo;
1448 tmp = EscapeWM_CLASS(btn->wm_instance, btn->wm_class);
1450 name = WMCreatePLString(tmp);
1452 wfree(tmp);
1454 forced = btn->forced_dock ? dYes : dNo;
1456 buggy = btn->buggy_app ? dYes : dNo;
1458 if (!wPreferences.flags.clip_merged_in_dock && btn == btn->icon->core->screen_ptr->clip_icon)
1459 snprintf(buffer, sizeof(buffer), "%i,%i", btn->x_pos, btn->y_pos);
1460 else
1461 snprintf(buffer, sizeof(buffer), "%hi,%hi", btn->xindex, btn->yindex);
1462 position = WMCreatePLString(buffer);
1464 node = WMCreatePLDictionary(dCommand, command,
1465 dName, name,
1466 dAutoLaunch, autolaunch,
1467 dLock, lock,
1468 dForced, forced, dBuggyApplication, buggy, dPosition, position, NULL);
1469 WMReleasePropList(command);
1470 WMReleasePropList(name);
1471 WMReleasePropList(position);
1473 omnipresent = btn->omnipresent ? dYes : dNo;
1474 if (btn->dock != btn->icon->core->screen_ptr->dock && (btn->xindex != 0 || btn->yindex != 0))
1475 WMPutInPLDictionary(node, dOmnipresent, omnipresent);
1477 #ifdef XDND /* was OFFIX */
1478 if (btn->dnd_command) {
1479 command = WMCreatePLString(btn->dnd_command);
1480 WMPutInPLDictionary(node, dDropCommand, command);
1481 WMReleasePropList(command);
1483 #endif /* XDND */
1485 if (btn->paste_command) {
1486 command = WMCreatePLString(btn->paste_command);
1487 WMPutInPLDictionary(node, dPasteCommand, command);
1488 WMReleasePropList(command);
1492 return node;
1495 static WMPropList *dockSaveState(WDock *dock)
1497 int i;
1498 WMPropList *icon_info;
1499 WMPropList *list = NULL, *dock_state = NULL;
1500 WMPropList *value, *key;
1501 char buffer[256];
1503 list = WMCreatePLArray(NULL);
1505 for (i = (dock->type == WM_DOCK ? 0 : 1); i < dock->max_icons; i++) {
1506 WAppIcon *btn = dock->icon_array[i];
1508 if (!btn || btn->attracted)
1509 continue;
1511 if ((icon_info = make_icon_state(dock->icon_array[i]))) {
1512 WMAddToPLArray(list, icon_info);
1513 WMReleasePropList(icon_info);
1517 dock_state = WMCreatePLDictionary(dApplications, list, NULL);
1519 if (dock->type == WM_DOCK) {
1520 snprintf(buffer, sizeof(buffer), "Applications%i", dock->screen_ptr->scr_height);
1521 key = WMCreatePLString(buffer);
1522 WMPutInPLDictionary(dock_state, key, list);
1523 WMReleasePropList(key);
1525 snprintf(buffer, sizeof(buffer), "%i,%i", (dock->on_right_side ? -ICON_SIZE : 0), dock->y_pos);
1526 value = WMCreatePLString(buffer);
1527 WMPutInPLDictionary(dock_state, dPosition, value);
1528 WMReleasePropList(value);
1530 WMReleasePropList(list);
1532 if (dock->type == WM_CLIP || dock->type == WM_DRAWER) {
1533 value = (dock->collapsed ? dYes : dNo);
1534 WMPutInPLDictionary(dock_state, dCollapsed, value);
1536 value = (dock->auto_collapse ? dYes : dNo);
1537 WMPutInPLDictionary(dock_state, dAutoCollapse, value);
1539 value = (dock->attract_icons ? dYes : dNo);
1540 WMPutInPLDictionary(dock_state, dAutoAttractIcons, value);
1543 if (dock->type == WM_DOCK || dock->type == WM_CLIP) {
1544 value = (dock->lowered ? dYes : dNo);
1545 WMPutInPLDictionary(dock_state, dLowered, value);
1547 value = (dock->auto_raise_lower ? dYes : dNo);
1548 WMPutInPLDictionary(dock_state, dAutoRaiseLower, value);
1551 return dock_state;
1554 void wDockSaveState(WScreen *scr, WMPropList *old_state)
1556 WMPropList *dock_state;
1557 WMPropList *keys;
1559 dock_state = dockSaveState(scr->dock);
1562 * Copy saved states of docks with different sizes.
1564 if (old_state) {
1565 int i;
1566 WMPropList *tmp;
1568 keys = WMGetPLDictionaryKeys(old_state);
1569 for (i = 0; i < WMGetPropListItemCount(keys); i++) {
1570 tmp = WMGetFromPLArray(keys, i);
1572 if (strncasecmp(WMGetFromPLString(tmp), "applications", 12) == 0
1573 && !WMGetFromPLDictionary(dock_state, tmp)) {
1575 WMPutInPLDictionary(dock_state, tmp, WMGetFromPLDictionary(old_state, tmp));
1578 WMReleasePropList(keys);
1581 WMPutInPLDictionary(scr->session_state, dDock, dock_state);
1583 WMReleasePropList(dock_state);
1586 void wClipSaveState(WScreen *scr)
1588 WMPropList *clip_state;
1590 clip_state = make_icon_state(scr->clip_icon);
1592 WMPutInPLDictionary(scr->session_state, dClip, clip_state);
1594 WMReleasePropList(clip_state);
1597 WMPropList *wClipSaveWorkspaceState(WScreen *scr, int workspace)
1599 return dockSaveState(scr->workspaces[workspace]->clip);
1602 static Bool getBooleanDockValue(WMPropList *value, WMPropList *key)
1604 if (value) {
1605 if (WMIsPLString(value)) {
1606 if (strcasecmp(WMGetFromPLString(value), "YES") == 0)
1607 return True;
1608 } else {
1609 wwarning(_("bad value in docked icon state info %s"), WMGetFromPLString(key));
1612 return False;
1615 static WAppIcon *restore_icon_state(WScreen *scr, WMPropList *info, int type, int index)
1617 WAppIcon *aicon;
1618 WMPropList *cmd, *value;
1619 char *wclass, *winstance, *command;
1621 cmd = WMGetFromPLDictionary(info, dCommand);
1622 if (!cmd || !WMIsPLString(cmd))
1623 return NULL;
1625 /* parse window name */
1626 value = WMGetFromPLDictionary(info, dName);
1627 if (!value)
1628 return NULL;
1630 ParseWindowName(value, &winstance, &wclass, "dock");
1632 if (!winstance && !wclass)
1633 return NULL;
1635 /* get commands */
1636 if (cmd)
1637 command = wstrdup(WMGetFromPLString(cmd));
1638 else
1639 command = NULL;
1641 if (!command || strcmp(command, "-") == 0) {
1642 if (command)
1643 wfree(command);
1644 if (wclass)
1645 wfree(wclass);
1646 if (winstance)
1647 wfree(winstance);
1649 return NULL;
1652 aicon = wAppIconCreateForDock(scr, command, winstance, wclass, TILE_NORMAL);
1653 if (wclass)
1654 wfree(wclass);
1655 if (winstance)
1656 wfree(winstance);
1657 if (command)
1658 wfree(command);
1660 aicon->icon->core->descriptor.handle_mousedown = iconMouseDown;
1661 aicon->icon->core->descriptor.handle_enternotify = clipEnterNotify;
1662 aicon->icon->core->descriptor.handle_leavenotify = clipLeaveNotify;
1663 aicon->icon->core->descriptor.parent_type = WCLASS_DOCK_ICON;
1664 aicon->icon->core->descriptor.parent = aicon;
1666 #ifdef XDND /* was OFFIX */
1667 cmd = WMGetFromPLDictionary(info, dDropCommand);
1668 if (cmd)
1669 aicon->dnd_command = wstrdup(WMGetFromPLString(cmd));
1670 #endif
1672 cmd = WMGetFromPLDictionary(info, dPasteCommand);
1673 if (cmd)
1674 aicon->paste_command = wstrdup(WMGetFromPLString(cmd));
1676 /* check auto launch */
1677 value = WMGetFromPLDictionary(info, dAutoLaunch);
1679 aicon->auto_launch = getBooleanDockValue(value, dAutoLaunch);
1681 /* check lock */
1682 value = WMGetFromPLDictionary(info, dLock);
1684 aicon->lock = getBooleanDockValue(value, dLock);
1686 /* check if it wasn't normally docked */
1687 value = WMGetFromPLDictionary(info, dForced);
1689 aicon->forced_dock = getBooleanDockValue(value, dForced);
1691 /* check if we can rely on the stuff in the app */
1692 value = WMGetFromPLDictionary(info, dBuggyApplication);
1694 aicon->buggy_app = getBooleanDockValue(value, dBuggyApplication);
1696 /* get position in the dock */
1697 value = WMGetFromPLDictionary(info, dPosition);
1698 if (value && WMIsPLString(value)) {
1699 if (sscanf(WMGetFromPLString(value), "%hi,%hi", &aicon->xindex, &aicon->yindex) != 2)
1700 wwarning(_("bad value in docked icon state info %s"), WMGetFromPLString(dPosition));
1702 /* check position sanity */
1703 /* *Very* incomplete section! */
1704 if (type == WM_DOCK) {
1705 aicon->xindex = 0;
1707 } else {
1708 aicon->yindex = index;
1709 aicon->xindex = 0;
1712 /* check if icon is omnipresent */
1713 value = WMGetFromPLDictionary(info, dOmnipresent);
1715 aicon->omnipresent = getBooleanDockValue(value, dOmnipresent);
1717 aicon->running = 0;
1718 aicon->docked = 1;
1720 return aicon;
1723 #define COMPLAIN(key) wwarning(_("bad value in dock state info:%s"), key)
1725 WAppIcon *wClipRestoreState(WScreen *scr, WMPropList *clip_state)
1727 WAppIcon *icon;
1728 WMPropList *value;
1730 icon = mainIconCreate(scr, WM_CLIP, NULL);
1732 if (!clip_state)
1733 return icon;
1735 WMRetainPropList(clip_state);
1737 /* restore position */
1739 value = WMGetFromPLDictionary(clip_state, dPosition);
1741 if (value) {
1742 if (!WMIsPLString(value)) {
1743 COMPLAIN("Position");
1744 } else {
1745 if (sscanf(WMGetFromPLString(value), "%i,%i", &icon->x_pos, &icon->y_pos) != 2)
1746 COMPLAIN("Position");
1748 /* check position sanity */
1749 if (!onScreen(scr, icon->x_pos, icon->y_pos))
1750 wScreenKeepInside(scr, &icon->x_pos, &icon->y_pos, ICON_SIZE, ICON_SIZE);
1753 #ifdef XDND /* was OFFIX */
1754 value = WMGetFromPLDictionary(clip_state, dDropCommand);
1755 if (value && WMIsPLString(value))
1756 icon->dnd_command = wstrdup(WMGetFromPLString(value));
1757 #endif
1759 value = WMGetFromPLDictionary(clip_state, dPasteCommand);
1760 if (value && WMIsPLString(value))
1761 icon->paste_command = wstrdup(WMGetFromPLString(value));
1763 WMReleasePropList(clip_state);
1765 return icon;
1768 WDock *wDockRestoreState(WScreen *scr, WMPropList *dock_state, int type)
1770 WDock *dock;
1771 WMPropList *apps;
1772 WMPropList *value;
1773 WAppIcon *aicon, *old_top;
1774 int count, i;
1776 dock = wDockCreate(scr, type, NULL);
1778 if (!dock_state)
1779 return dock;
1781 WMRetainPropList(dock_state);
1783 /* restore position */
1784 value = WMGetFromPLDictionary(dock_state, dPosition);
1785 if (value) {
1786 if (!WMIsPLString(value)) {
1787 COMPLAIN("Position");
1788 } else {
1789 if (sscanf(WMGetFromPLString(value), "%i,%i", &dock->x_pos, &dock->y_pos) != 2)
1790 COMPLAIN("Position");
1792 /* check position sanity */
1793 if (!onScreen(scr, dock->x_pos, dock->y_pos)) {
1794 int x = dock->x_pos;
1795 wScreenKeepInside(scr, &x, &dock->y_pos, ICON_SIZE, ICON_SIZE);
1798 /* Is this needed any more? */
1799 if (type == WM_CLIP) {
1800 if (dock->x_pos < 0) {
1801 dock->x_pos = 0;
1802 } else if (dock->x_pos > scr->scr_width - ICON_SIZE) {
1803 dock->x_pos = scr->scr_width - ICON_SIZE;
1805 } else {
1806 if (dock->x_pos >= 0) {
1807 dock->x_pos = DOCK_EXTRA_SPACE;
1808 dock->on_right_side = 0;
1809 } else {
1810 dock->x_pos = scr->scr_width - DOCK_EXTRA_SPACE - ICON_SIZE;
1811 dock->on_right_side = 1;
1817 /* restore lowered/raised state */
1818 dock->lowered = 0;
1820 value = WMGetFromPLDictionary(dock_state, dLowered);
1821 if (value) {
1822 if (!WMIsPLString(value)) {
1823 COMPLAIN("Lowered");
1824 } else {
1825 if (strcasecmp(WMGetFromPLString(value), "YES") == 0)
1826 dock->lowered = 1;
1830 /* restore collapsed state */
1831 dock->collapsed = 0;
1833 value = WMGetFromPLDictionary(dock_state, dCollapsed);
1834 if (value) {
1835 if (!WMIsPLString(value)) {
1836 COMPLAIN("Collapsed");
1837 } else {
1838 if (strcasecmp(WMGetFromPLString(value), "YES") == 0)
1839 dock->collapsed = 1;
1843 /* restore auto-collapsed state */
1844 value = WMGetFromPLDictionary(dock_state, dAutoCollapse);
1845 if (value) {
1846 if (!WMIsPLString(value)) {
1847 COMPLAIN("AutoCollapse");
1848 } else {
1849 if (strcasecmp(WMGetFromPLString(value), "YES") == 0) {
1850 dock->auto_collapse = 1;
1851 dock->collapsed = 1;
1856 /* restore auto-raise/lower state */
1857 value = WMGetFromPLDictionary(dock_state, dAutoRaiseLower);
1858 if (value) {
1859 if (!WMIsPLString(value)) {
1860 COMPLAIN("AutoRaiseLower");
1861 } else {
1862 if (strcasecmp(WMGetFromPLString(value), "YES") == 0)
1863 dock->auto_raise_lower = 1;
1867 /* restore attract icons state */
1868 dock->attract_icons = 0;
1870 value = WMGetFromPLDictionary(dock_state, dAutoAttractIcons);
1871 if (value) {
1872 if (!WMIsPLString(value)) {
1873 COMPLAIN("AutoAttractIcons");
1874 } else {
1875 if (strcasecmp(WMGetFromPLString(value), "YES") == 0)
1876 dock->attract_icons = 1;
1880 /* application list */
1883 WMPropList *tmp;
1884 char buffer[64];
1887 * When saving, it saves the dock state in
1888 * Applications and Applicationsnnn
1890 * When loading, it will first try Applicationsnnn.
1891 * If it does not exist, use Applications as default.
1894 snprintf(buffer, sizeof(buffer), "Applications%i", scr->scr_height);
1896 tmp = WMCreatePLString(buffer);
1897 apps = WMGetFromPLDictionary(dock_state, tmp);
1898 WMReleasePropList(tmp);
1900 if (!apps)
1901 apps = WMGetFromPLDictionary(dock_state, dApplications);
1904 if (!apps)
1905 goto finish;
1907 count = WMGetPropListItemCount(apps);
1908 if (count == 0)
1909 goto finish;
1911 old_top = dock->icon_array[0];
1913 /* dock->icon_count is set to 1 when dock is created.
1914 * Since Clip is already restored, we want to keep it so for clip,
1915 * but for dock we may change the default top tile, so we set it to 0.
1917 if (type == WM_DOCK)
1918 dock->icon_count = 0;
1920 for (i = 0; i < count; i++) {
1921 if (dock->icon_count >= dock->max_icons) {
1922 wwarning(_("there are too many icons stored in dock. Ignoring what doesn't fit"));
1923 break;
1926 value = WMGetFromPLArray(apps, i);
1927 aicon = restore_icon_state(scr, value, type, dock->icon_count);
1929 dock->icon_array[dock->icon_count] = aicon;
1931 if (aicon) {
1932 aicon->dock = dock;
1933 aicon->x_pos = dock->x_pos + (aicon->xindex * ICON_SIZE);
1934 aicon->y_pos = dock->y_pos + (aicon->yindex * ICON_SIZE);
1936 if (dock->lowered)
1937 ChangeStackingLevel(aicon->icon->core, WMNormalLevel);
1938 else
1939 ChangeStackingLevel(aicon->icon->core, WMDockLevel);
1941 wCoreConfigure(aicon->icon->core, aicon->x_pos, aicon->y_pos, 0, 0);
1942 if (!dock->collapsed)
1943 XMapWindow(dpy, aicon->icon->core->window);
1945 wRaiseFrame(aicon->icon->core);
1947 dock->icon_count++;
1948 } else if (dock->icon_count == 0 && type == WM_DOCK) {
1949 dock->icon_count++;
1953 /* if the first icon is not defined, use the default */
1954 if (dock->icon_array[0] == NULL) {
1955 /* update default icon */
1956 old_top->x_pos = dock->x_pos;
1957 old_top->y_pos = dock->y_pos;
1958 if (dock->lowered)
1959 ChangeStackingLevel(old_top->icon->core, WMNormalLevel);
1960 else
1961 ChangeStackingLevel(old_top->icon->core, WMDockLevel);
1963 dock->icon_array[0] = old_top;
1964 XMoveWindow(dpy, old_top->icon->core->window, dock->x_pos, dock->y_pos);
1965 /* we don't need to increment dock->icon_count here because it was
1966 * incremented in the loop above.
1968 } else if (old_top != dock->icon_array[0]) {
1969 if (old_top == scr->clip_icon) // TODO dande: understand the logic
1970 scr->clip_icon = dock->icon_array[0];
1972 wAppIconDestroy(old_top);
1975 finish:
1976 WMReleasePropList(dock_state);
1978 return dock;
1981 void wDockLaunchWithState(WDock *dock, WAppIcon *btn, WSavedState *state)
1983 if (btn && btn->command && !btn->running && !btn->launching) {
1984 btn->drop_launch = 0;
1985 btn->paste_launch = 0;
1987 btn->pid = execCommand(btn, btn->command, state);
1989 if (btn->pid > 0) {
1990 if (!btn->forced_dock && !btn->buggy_app) {
1991 btn->launching = 1;
1992 dockIconPaint(btn);
1995 } else {
1996 wfree(state);
2000 void wDockDoAutoLaunch(WDock *dock, int workspace)
2002 WAppIcon *btn;
2003 WSavedState *state;
2004 int i;
2006 for (i = 0; i < dock->max_icons; i++) {
2007 btn = dock->icon_array[i];
2008 if (!btn || !btn->auto_launch)
2009 continue;
2011 state = wmalloc(sizeof(WSavedState));
2012 state->workspace = workspace;
2013 /* TODO: this is klugy and is very difficult to understand
2014 * what's going on. Try to clean up */
2015 wDockLaunchWithState(dock, btn, state);
2019 #ifdef XDND /* was OFFIX */
2020 static WDock *findDock(WScreen *scr, XEvent *event, int *icon_pos)
2022 WDock *dock;
2023 int i;
2025 *icon_pos = -1;
2026 if ((dock = scr->dock) != NULL) {
2027 for (i = 0; i < dock->max_icons; i++) {
2028 if (dock->icon_array[i]
2029 && dock->icon_array[i]->icon->core->window == event->xclient.window) {
2030 *icon_pos = i;
2031 break;
2035 if (*icon_pos < 0 && (dock = scr->workspaces[scr->current_workspace]->clip) != NULL) {
2036 for (i = 0; i < dock->max_icons; i++) {
2037 if (dock->icon_array[i]
2038 && dock->icon_array[i]->icon->core->window == event->xclient.window) {
2039 *icon_pos = i;
2040 break;
2044 if (*icon_pos >= 0)
2045 return dock;
2046 return NULL;
2049 int wDockReceiveDNDDrop(WScreen *scr, XEvent *event)
2051 WDock *dock;
2052 WAppIcon *btn;
2053 int icon_pos;
2055 dock = findDock(scr, event, &icon_pos);
2056 if (!dock)
2057 return False;
2060 * Return True if the drop was on an application icon window.
2061 * In this case, let the ClientMessage handler redirect the
2062 * message to the app.
2064 if (dock->icon_array[icon_pos]->icon->icon_win != None)
2065 return True;
2067 if (dock->icon_array[icon_pos]->dnd_command != NULL) {
2068 scr->flags.dnd_data_convertion_status = 0;
2070 btn = dock->icon_array[icon_pos];
2072 if (!btn->forced_dock) {
2073 btn->relaunching = btn->running;
2074 btn->running = 1;
2076 if (btn->wm_instance || btn->wm_class) {
2077 WWindowAttributes attr;
2078 memset(&attr, 0, sizeof(WWindowAttributes));
2079 wDefaultFillAttributes(btn->wm_instance, btn->wm_class, &attr, NULL, True);
2081 if (!attr.no_appicon)
2082 btn->launching = 1;
2083 else
2084 btn->running = 0;
2087 btn->paste_launch = 0;
2088 btn->drop_launch = 1;
2089 scr->last_dock = dock;
2090 btn->pid = execCommand(btn, btn->dnd_command, NULL);
2091 if (btn->pid > 0) {
2092 dockIconPaint(btn);
2093 } else {
2094 btn->launching = 0;
2095 if (!btn->relaunching)
2096 btn->running = 0;
2099 return False;
2101 #endif /* XDND */
2103 Bool wDockAttachIcon(WDock *dock, WAppIcon *icon, int x, int y, Bool update_icon)
2105 WWindow *wwin;
2106 Bool lupdate_icon = False;
2107 char *command = NULL;
2108 int index;
2110 icon->editing = 0;
2112 if (update_icon)
2113 lupdate_icon = True;
2115 if (icon->command == NULL) {
2116 /* If icon->owner exists, it means the application is running */
2117 if (icon->icon->owner) {
2118 wwin = icon->icon->owner;
2119 command = GetCommandForWindow(wwin->client_win);
2122 if (command) {
2123 icon->command = command;
2124 } else {
2125 /* icon->forced_dock = 1; */
2126 if (dock->type != WM_CLIP || !icon->attracted) {
2127 icon->editing = 1;
2128 if (wInputDialog(dock->screen_ptr, _("Dock Icon"),
2129 _("Type the command used to launch the application"), &command)) {
2130 if (command && (command[0] == 0 || (command[0] == '-' && command[1] == 0))) {
2131 wfree(command);
2132 command = NULL;
2134 icon->command = command;
2135 icon->editing = 0;
2136 } else {
2137 icon->editing = 0;
2138 if (command)
2139 wfree(command);
2140 /* If the target is the dock, reject the icon. If
2141 * the target is the clip, make it an attracted icon
2143 if (dock->type == WM_CLIP) {
2144 icon->attracted = 1;
2145 if (!icon->icon->shadowed) {
2146 icon->icon->shadowed = 1;
2147 lupdate_icon = True;
2149 } else {
2150 return False;
2157 for (index = 1; index < dock->max_icons; index++)
2158 if (dock->icon_array[index] == NULL)
2159 break;
2160 /* if (index == dock->max_icons)
2161 return; */
2163 assert(index < dock->max_icons);
2165 dock->icon_array[index] = icon;
2166 icon->yindex = y;
2167 icon->xindex = x;
2169 icon->omnipresent = 0;
2171 icon->x_pos = dock->x_pos + x * ICON_SIZE;
2172 icon->y_pos = dock->y_pos + y * ICON_SIZE;
2174 dock->icon_count++;
2176 icon->running = 1;
2177 icon->launching = 0;
2178 icon->docked = 1;
2179 icon->dock = dock;
2180 icon->icon->core->descriptor.handle_mousedown = iconMouseDown;
2181 icon->icon->core->descriptor.handle_enternotify = clipEnterNotify;
2182 icon->icon->core->descriptor.handle_leavenotify = clipLeaveNotify;
2183 icon->icon->core->descriptor.parent_type = WCLASS_DOCK_ICON;
2184 icon->icon->core->descriptor.parent = icon;
2186 MoveInStackListUnder(dock->icon_array[index - 1]->icon->core, icon->icon->core);
2187 wAppIconMove(icon, icon->x_pos, icon->y_pos);
2190 * Update icon pixmap, RImage doesn't change,
2191 * so call wIconUpdate is not needed
2193 if (lupdate_icon)
2194 update_icon_pixmap(icon->icon);
2196 /* Paint it */
2197 wAppIconPaint(icon);
2199 /* Save it */
2200 save_appicon(icon, True);
2202 if (wPreferences.auto_arrange_icons)
2203 wArrangeIcons(dock->screen_ptr, True);
2205 #ifdef XDND /* was OFFIX */
2206 if (icon->command && !icon->dnd_command) {
2207 int len = strlen(icon->command) + 8;
2208 icon->dnd_command = wmalloc(len);
2209 snprintf(icon->dnd_command, len, "%s %%d", icon->command);
2211 #endif
2213 if (icon->command && !icon->paste_command) {
2214 int len = strlen(icon->command) + 8;
2215 icon->paste_command = wmalloc(len);
2216 snprintf(icon->paste_command, len, "%s %%s", icon->command);
2219 return True;
2222 void wDockReattachIcon(WDock *dock, WAppIcon *icon, int x, int y)
2224 int index;
2226 for (index = 1; index < dock->max_icons; index++) {
2227 if (dock->icon_array[index] == icon)
2228 break;
2230 assert(index < dock->max_icons);
2232 icon->yindex = y;
2233 icon->xindex = x;
2235 icon->x_pos = dock->x_pos + x * ICON_SIZE;
2236 icon->y_pos = dock->y_pos + y * ICON_SIZE;
2239 Bool wDockMoveIconBetweenDocks(WDock *src, WDock *dest, WAppIcon *icon, int x, int y)
2241 WWindow *wwin;
2242 char *command = NULL;
2243 int index;
2244 Bool update_icon = False;
2246 if (src == dest)
2247 return True; /* No move needed, we're already there */
2249 if (dest == NULL)
2250 return False;
2253 * For the moment we can't do this if we move icons in Clip from one
2254 * workspace to other, because if we move two or more icons without
2255 * command, the dialog box will not be able to tell us to which of the
2256 * moved icons it applies. -Dan
2258 if ((dest->type == WM_DOCK /*|| dest->keep_attracted */ ) && icon->command == NULL) {
2259 /* If icon->owner exists, it means the application is running */
2260 if (icon->icon->owner) {
2261 wwin = icon->icon->owner;
2262 command = GetCommandForWindow(wwin->client_win);
2265 if (command) {
2266 icon->command = command;
2267 } else {
2268 icon->editing = 1;
2269 /* icon->forced_dock = 1; */
2270 if (wInputDialog(src->screen_ptr, _("Dock Icon"),
2271 _("Type the command used to launch the application"), &command)) {
2272 if (command && (command[0] == 0 || (command[0] == '-' && command[1] == 0))) {
2273 wfree(command);
2274 command = NULL;
2276 icon->command = command;
2277 } else {
2278 icon->editing = 0;
2279 if (command)
2280 wfree(command);
2281 return False;
2283 icon->editing = 0;
2287 if (dest->type == WM_DOCK || dest->type == WM_DRAWER)
2288 wClipMakeIconOmnipresent(icon, False);
2290 for (index = 1; index < src->max_icons; index++) {
2291 if (src->icon_array[index] == icon)
2292 break;
2294 assert(index < src->max_icons);
2296 src->icon_array[index] = NULL;
2297 src->icon_count--;
2299 for (index = 1; index < dest->max_icons; index++) {
2300 if (dest->icon_array[index] == NULL)
2301 break;
2304 assert(index < dest->max_icons);
2306 dest->icon_array[index] = icon;
2307 icon->dock = dest;
2309 /* deselect the icon */
2310 if (icon->icon->selected)
2311 wIconSelect(icon->icon);
2313 icon->icon->core->descriptor.handle_enternotify = clipEnterNotify;
2314 icon->icon->core->descriptor.handle_leavenotify = clipLeaveNotify;
2316 /* set it to be kept when moving to dock.
2317 * Unless the icon does not have a command set
2319 if (icon->command && (dest->type == WM_DOCK || dest->type == WM_DRAWER)) {
2320 icon->attracted = 0;
2321 if (icon->icon->shadowed) {
2322 icon->icon->shadowed = 0;
2323 update_icon = True;
2325 save_appicon(icon, True);
2328 if (src->auto_collapse || src->auto_raise_lower)
2329 clipLeave(src);
2331 icon->yindex = y;
2332 icon->xindex = x;
2334 icon->x_pos = dest->x_pos + x * ICON_SIZE;
2335 icon->y_pos = dest->y_pos + y * ICON_SIZE;
2337 dest->icon_count++;
2339 MoveInStackListUnder(dest->icon_array[index - 1]->icon->core, icon->icon->core);
2342 * Update icon pixmap, RImage doesn't change,
2343 * so call wIconUpdate is not needed
2345 if (update_icon)
2346 update_icon_pixmap(icon->icon);
2348 /* Paint it */
2349 wAppIconPaint(icon);
2351 return True;
2354 void wDockDetach(WDock *dock, WAppIcon *icon)
2356 int index;
2357 Bool update_icon = False;
2359 /* make the settings panel be closed */
2360 if (icon->panel)
2361 DestroyDockAppSettingsPanel(icon->panel);
2363 /* This must be called before icon->dock is set to NULL.
2364 * Don't move it. -Dan
2366 wClipMakeIconOmnipresent(icon, False);
2368 icon->docked = 0;
2369 icon->dock = NULL;
2370 icon->attracted = 0;
2371 icon->auto_launch = 0;
2372 if (icon->icon->shadowed) {
2373 icon->icon->shadowed = 0;
2374 update_icon = True;
2377 /* deselect the icon */
2378 if (icon->icon->selected)
2379 wIconSelect(icon->icon);
2381 if (icon->command) {
2382 wfree(icon->command);
2383 icon->command = NULL;
2385 #ifdef XDND /* was OFFIX */
2386 if (icon->dnd_command) {
2387 wfree(icon->dnd_command);
2388 icon->dnd_command = NULL;
2390 #endif
2391 if (icon->paste_command) {
2392 wfree(icon->paste_command);
2393 icon->paste_command = NULL;
2396 for (index = 1; index < dock->max_icons; index++)
2397 if (dock->icon_array[index] == icon)
2398 break;
2400 assert(index < dock->max_icons);
2401 dock->icon_array[index] = NULL;
2402 icon->yindex = -1;
2403 icon->xindex = -1;
2405 dock->icon_count--;
2407 /* if the dock is not attached to an application or
2408 * the application did not set the appropriate hints yet,
2409 * destroy the icon */
2410 if (!icon->running || !wApplicationOf(icon->main_window)) {
2411 wAppIconDestroy(icon);
2412 } else {
2413 icon->icon->core->descriptor.handle_mousedown = appIconMouseDown;
2414 icon->icon->core->descriptor.handle_enternotify = NULL;
2415 icon->icon->core->descriptor.handle_leavenotify = NULL;
2416 icon->icon->core->descriptor.parent_type = WCLASS_APPICON;
2417 icon->icon->core->descriptor.parent = icon;
2419 ChangeStackingLevel(icon->icon->core, NORMAL_ICON_LEVEL);
2422 * Update icon pixmap, RImage doesn't change,
2423 * so call wIconUpdate is not needed
2425 if (update_icon)
2426 update_icon_pixmap(icon->icon);
2428 /* Paint it */
2429 wAppIconPaint(icon);
2431 if (wPreferences.auto_arrange_icons)
2432 wArrangeIcons(dock->screen_ptr, True);
2434 if (dock->auto_collapse || dock->auto_raise_lower)
2435 clipLeave(dock);
2439 * returns the closest Dock slot index for the passed
2440 * coordinates.
2442 * Returns False if icon can't be docked.
2444 * Note: this function should NEVER alter ret_x or ret_y, unless it will
2445 * return True. -Dan
2447 /* Redocking == true means either icon->dock == dock (normal case)
2448 * or we are called from handleDockMove for a drawer */
2449 Bool wDockSnapIcon(WDock *dock, WAppIcon *icon, int req_x, int req_y, int *ret_x, int *ret_y, int redocking)
2451 WScreen *scr = dock->screen_ptr;
2452 int dx, dy;
2453 int ex_x, ex_y;
2454 int i, offset = ICON_SIZE / 2;
2455 WAppIcon *aicon = NULL;
2456 WAppIcon *nicon = NULL;
2458 if (wPreferences.flags.noupdates)
2459 return False;
2461 dx = dock->x_pos;
2462 dy = dock->y_pos;
2464 /* if the dock is full */
2465 if (!redocking && (dock->icon_count >= dock->max_icons))
2466 return False;
2468 /* exact position */
2469 if (req_y < dy)
2470 ex_y = (req_y - offset - dy) / ICON_SIZE;
2471 else
2472 ex_y = (req_y + offset - dy) / ICON_SIZE;
2474 if (req_x < dx)
2475 ex_x = (req_x - offset - dx) / ICON_SIZE;
2476 else
2477 ex_x = (req_x + offset - dx) / ICON_SIZE;
2479 /* check if the icon is outside the screen boundaries */
2480 if (!onScreen(scr, dx + ex_x * ICON_SIZE, dy + ex_y * ICON_SIZE))
2481 return False;
2483 switch (dock->type) {
2484 case WM_DOCK:
2485 /* We can return False right away if
2486 * - we do not come from this dock (which is a WM_DOCK),
2487 * - we are not right over it, and
2488 * - we are not the main tile of a drawer.
2489 * In the latter case, we are called from handleDockMove. */
2490 if (icon->dock != dock && ex_x != 0 &&
2491 !(icon->dock && icon->dock->type == WM_DRAWER && icon == icon->dock->icon_array[0]))
2492 return False;
2494 if (!redocking && ex_x != 0)
2495 return False;
2497 if (getDrawer(scr, ex_y)) /* Return false so that the drawer gets it. */
2498 return False;
2500 aicon = NULL;
2501 for (i = 0; i < dock->max_icons; i++) {
2502 nicon = dock->icon_array[i];
2503 if (nicon && nicon->yindex == ex_y) {
2504 aicon = nicon;
2505 break;
2509 if (redocking) {
2510 int sig, done, closest;
2512 /* Possible cases when redocking:
2514 * icon dragged out of range of any slot -> false
2515 * icon dragged on a drawer -> false (to open the drawer)
2516 * icon dragged to range of free slot
2517 * icon dragged to range of same slot
2518 * icon dragged to range of different icon
2520 if (abs(ex_x) > DOCK_DETTACH_THRESHOLD)
2521 return False;
2523 if (aicon == icon || !aicon) {
2524 *ret_x = 0;
2525 *ret_y = ex_y;
2526 return True;
2529 /* start looking at the upper slot or lower? */
2530 if (ex_y * ICON_SIZE < (req_y + offset - dy))
2531 sig = 1;
2532 else
2533 sig = -1;
2535 done = 0;
2536 /* look for closest free slot */
2537 for (i = 0; i < (DOCK_DETTACH_THRESHOLD + 1) * 2 && !done; i++) {
2538 int j;
2540 done = 1;
2541 closest = sig * (i / 2) + ex_y;
2542 /* check if this slot is fully on the screen and not used */
2543 if (onScreen(scr, dx, dy + closest * ICON_SIZE)) {
2544 for (j = 0; j < dock->max_icons; j++) {
2545 if (dock->icon_array[j]
2546 && dock->icon_array[j]->yindex == closest) {
2547 /* slot is used by someone else */
2548 if (dock->icon_array[j] != icon)
2549 done = 0;
2550 break;
2553 /* slot is used by a drawer */
2554 done = done && !getDrawer(scr, closest);
2556 else // !onScreen
2557 done = 0;
2558 sig = -sig;
2560 if (done &&
2561 ((ex_y >= closest && ex_y - closest < DOCK_DETTACH_THRESHOLD + 1)
2562 || (ex_y < closest && closest - ex_y <= DOCK_DETTACH_THRESHOLD + 1))) {
2563 *ret_x = 0;
2564 *ret_y = closest;
2565 return True;
2567 } else { /* !redocking */
2569 /* if slot is free and the icon is close enough, return it */
2570 if (!aicon && ex_x == 0) {
2571 *ret_x = 0;
2572 *ret_y = ex_y;
2573 return True;
2576 break;
2577 case WM_CLIP:
2579 int neighbours = 0;
2580 int start, stop, k;
2582 start = icon->omnipresent ? 0 : scr->current_workspace;
2583 stop = icon->omnipresent ? scr->workspace_count : start + 1;
2585 aicon = NULL;
2586 for (k = start; k < stop; k++) {
2587 WDock *tmp = scr->workspaces[k]->clip;
2588 if (!tmp)
2589 continue;
2590 for (i = 0; i < tmp->max_icons; i++) {
2591 nicon = tmp->icon_array[i];
2592 if (nicon && nicon->xindex == ex_x && nicon->yindex == ex_y) {
2593 aicon = nicon;
2594 break;
2597 if (aicon)
2598 break;
2600 for (k = start; k < stop; k++) {
2601 WDock *tmp = scr->workspaces[k]->clip;
2602 if (!tmp)
2603 continue;
2604 for (i = 0; i < tmp->max_icons; i++) {
2605 nicon = tmp->icon_array[i];
2606 if (nicon && nicon != icon && /* Icon can't be it's own neighbour */
2607 (abs(nicon->xindex - ex_x) <= CLIP_ATTACH_VICINITY &&
2608 abs(nicon->yindex - ex_y) <= CLIP_ATTACH_VICINITY)) {
2609 neighbours = 1;
2610 break;
2613 if (neighbours)
2614 break;
2617 if (neighbours && (aicon == NULL || (redocking && aicon == icon))) {
2618 *ret_x = ex_x;
2619 *ret_y = ex_y;
2620 return True;
2622 break;
2624 case WM_DRAWER:
2626 WAppIcon *aicons_to_shift[ dock->icon_count ];
2627 int index_of_hole, j;
2629 if (ex_y != 0 ||
2630 abs(ex_x) - dock->icon_count > DOCK_DETTACH_THRESHOLD ||
2631 (ex_x < 0 && !dock->on_right_side) ||
2632 (ex_x > 0 && dock->on_right_side)) {
2633 return False;
2636 if (ex_x == 0)
2637 ex_x = (dock->on_right_side ? -1 : 1);
2639 /* "Reduce" ex_x but keep its sign */
2640 if (redocking) {
2641 if (abs(ex_x) > dock->icon_count - 1) /* minus 1: do not take icon_array[0] into account */
2642 ex_x = ex_x * (dock->icon_count - 1) / abs(ex_x); /* don't use *= ! */
2643 } else {
2644 if (abs(ex_x) > dock->icon_count)
2645 ex_x = ex_x * dock->icon_count / abs(ex_x);
2647 index_of_hole = indexOfHole(dock, icon, redocking);
2649 /* Find the appicons between where icon was (index_of_hole) and where
2650 * it wants to be (ex_x) and slide them. */
2651 j = 0;
2652 for (i = 1; i < dock->max_icons; i++) {
2653 if ((aicon = dock->icon_array[ i ]) && aicon != icon &&
2654 ((ex_x <= aicon->xindex && aicon->xindex < index_of_hole) ||
2655 (index_of_hole < aicon->xindex && aicon->xindex <= ex_x)))
2656 aicons_to_shift[ j++ ] = aicon;
2658 assert(j == abs(ex_x - index_of_hole));
2660 wSlideAppicons(aicons_to_shift, j, (index_of_hole < ex_x));
2662 *ret_x = ex_x;
2663 *ret_y = ex_y;
2664 return True;
2667 return False;
2670 static int onScreen(WScreen *scr, int x, int y)
2672 WMRect rect;
2673 int flags;
2675 rect.pos.x = x;
2676 rect.pos.y = y;
2677 rect.size.width = rect.size.height = ICON_SIZE;
2679 wGetRectPlacementInfo(scr, rect, &flags);
2681 return !(flags & (XFLAG_DEAD | XFLAG_PARTIAL));
2685 * returns true if it can find a free slot in the dock,
2686 * in which case it changes x_pos and y_pos accordingly.
2687 * Else returns false.
2689 Bool wDockFindFreeSlot(WDock *dock, int *x_pos, int *y_pos)
2691 WScreen *scr = dock->screen_ptr;
2692 WAppIcon *btn;
2693 WAppIconChain *chain;
2694 unsigned char *slot_map;
2695 int mwidth;
2696 int r;
2697 int x, y;
2698 int i, done = False;
2699 int corner;
2700 int sx = 0, ex = scr->scr_width, ey = scr->scr_height;
2701 int extra_count = 0;
2703 if (dock->type == WM_DRAWER) {
2704 if (dock->icon_count >= dock->max_icons) { /* drawer is full */
2705 return False;
2707 *x_pos = dock->icon_count * (dock->on_right_side ? -1 : 1);
2708 *y_pos = 0;
2709 return True;
2712 if (dock->type == WM_CLIP && dock != scr->workspaces[scr->current_workspace]->clip)
2713 extra_count = scr->global_icon_count;
2715 /* if the dock is full */
2716 if (dock->icon_count + extra_count >= dock->max_icons)
2717 return False;
2719 if (!wPreferences.flags.nodock && scr->dock) {
2720 if (scr->dock->on_right_side)
2721 ex -= ICON_SIZE + DOCK_EXTRA_SPACE;
2722 else
2723 sx += ICON_SIZE + DOCK_EXTRA_SPACE;
2726 if (ex < dock->x_pos)
2727 ex = dock->x_pos;
2728 if (sx > dock->x_pos + ICON_SIZE)
2729 sx = dock->x_pos + ICON_SIZE;
2730 #define C_NONE 0
2731 #define C_NW 1
2732 #define C_NE 2
2733 #define C_SW 3
2734 #define C_SE 4
2736 /* check if clip is in a corner */
2737 if (dock->type == WM_CLIP) {
2738 if (dock->x_pos < 1 && dock->y_pos < 1)
2739 corner = C_NE;
2740 else if (dock->x_pos < 1 && dock->y_pos >= (ey - ICON_SIZE))
2741 corner = C_SE;
2742 else if (dock->x_pos >= (ex - ICON_SIZE) && dock->y_pos >= (ey - ICON_SIZE))
2743 corner = C_SW;
2744 else if (dock->x_pos >= (ex - ICON_SIZE) && dock->y_pos < 1)
2745 corner = C_NW;
2746 else
2747 corner = C_NONE;
2748 } else {
2749 corner = C_NONE;
2752 /* If the clip is in the corner, use only slots that are in the border
2753 * of the screen */
2754 if (corner != C_NONE) {
2755 char *hmap, *vmap;
2756 int hcount, vcount;
2758 hcount = WMIN(dock->max_icons, scr->scr_width / ICON_SIZE);
2759 vcount = WMIN(dock->max_icons, scr->scr_height / ICON_SIZE);
2760 hmap = wmalloc(hcount + 1);
2761 vmap = wmalloc(vcount + 1);
2763 /* mark used positions */
2764 switch (corner) {
2765 case C_NE:
2766 for (i = 0; i < dock->max_icons; i++) {
2767 btn = dock->icon_array[i];
2768 if (!btn)
2769 continue;
2771 if (btn->xindex == 0 && btn->yindex > 0 && btn->yindex < vcount)
2772 vmap[btn->yindex] = 1;
2773 else if (btn->yindex == 0 && btn->xindex > 0 && btn->xindex < hcount)
2774 hmap[btn->xindex] = 1;
2776 for (chain = scr->global_icons; chain != NULL; chain = chain->next) {
2777 btn = chain->aicon;
2778 if (btn->xindex == 0 && btn->yindex > 0 && btn->yindex < vcount)
2779 vmap[btn->yindex] = 1;
2780 else if (btn->yindex == 0 && btn->xindex > 0 && btn->xindex < hcount)
2781 hmap[btn->xindex] = 1;
2783 break;
2784 case C_NW:
2785 for (i = 0; i < dock->max_icons; i++) {
2786 btn = dock->icon_array[i];
2787 if (!btn)
2788 continue;
2790 if (btn->xindex == 0 && btn->yindex > 0 && btn->yindex < vcount)
2791 vmap[btn->yindex] = 1;
2792 else if (btn->yindex == 0 && btn->xindex < 0 && btn->xindex > -hcount)
2793 hmap[-btn->xindex] = 1;
2795 for (chain = scr->global_icons; chain != NULL; chain = chain->next) {
2796 btn = chain->aicon;
2797 if (btn->xindex == 0 && btn->yindex > 0 && btn->yindex < vcount)
2798 vmap[btn->yindex] = 1;
2799 else if (btn->yindex == 0 && btn->xindex < 0 && btn->xindex > -hcount)
2800 hmap[-btn->xindex] = 1;
2802 break;
2803 case C_SE:
2804 for (i = 0; i < dock->max_icons; i++) {
2805 btn = dock->icon_array[i];
2806 if (!btn)
2807 continue;
2809 if (btn->xindex == 0 && btn->yindex < 0 && btn->yindex > -vcount)
2810 vmap[-btn->yindex] = 1;
2811 else if (btn->yindex == 0 && btn->xindex > 0 && btn->xindex < hcount)
2812 hmap[btn->xindex] = 1;
2814 for (chain = scr->global_icons; chain != NULL; chain = chain->next) {
2815 btn = chain->aicon;
2816 if (btn->xindex == 0 && btn->yindex < 0 && btn->yindex > -vcount)
2817 vmap[-btn->yindex] = 1;
2818 else if (btn->yindex == 0 && btn->xindex > 0 && btn->xindex < hcount)
2819 hmap[btn->xindex] = 1;
2821 break;
2822 case C_SW:
2823 default:
2824 for (i = 0; i < dock->max_icons; i++) {
2825 btn = dock->icon_array[i];
2826 if (!btn)
2827 continue;
2829 if (btn->xindex == 0 && btn->yindex < 0 && btn->yindex > -vcount)
2830 vmap[-btn->yindex] = 1;
2831 else if (btn->yindex == 0 && btn->xindex < 0 && btn->xindex > -hcount)
2832 hmap[-btn->xindex] = 1;
2834 for (chain = scr->global_icons; chain != NULL; chain = chain->next) {
2835 btn = chain->aicon;
2836 if (btn->xindex == 0 && btn->yindex < 0 && btn->yindex > -vcount)
2837 vmap[-btn->yindex] = 1;
2838 else if (btn->yindex == 0 && btn->xindex < 0 && btn->xindex > -hcount)
2839 hmap[-btn->xindex] = 1;
2842 x = 0;
2843 y = 0;
2844 done = 0;
2845 /* search a vacant slot */
2846 for (i = 1; i < WMAX(vcount, hcount); i++) {
2847 if (i < vcount && vmap[i] == 0) {
2848 /* found a slot */
2849 x = 0;
2850 y = i;
2851 done = 1;
2852 break;
2853 } else if (i < hcount && hmap[i] == 0) {
2854 /* found a slot */
2855 x = i;
2856 y = 0;
2857 done = 1;
2858 break;
2861 wfree(vmap);
2862 wfree(hmap);
2863 /* If found a slot, translate and return */
2864 if (done) {
2865 if (corner == C_NW || corner == C_NE)
2866 *y_pos = y;
2867 else
2868 *y_pos = -y;
2870 if (corner == C_NE || corner == C_SE)
2871 *x_pos = x;
2872 else
2873 *x_pos = -x;
2875 return True;
2877 /* else, try to find a slot somewhere else */
2880 /* a map of mwidth x mwidth would be enough if we allowed icons to be
2881 * placed outside of screen */
2882 mwidth = (int)ceil(sqrt(dock->max_icons));
2884 /* In the worst case (the clip is in the corner of the screen),
2885 * the amount of icons that fit in the clip is smaller.
2886 * Double the map to get a safe value.
2888 mwidth += mwidth;
2890 r = (mwidth - 1) / 2;
2892 slot_map = wmalloc(mwidth * mwidth);
2894 #define XY2OFS(x,y) (WMAX(abs(x),abs(y)) > r) ? 0 : (((y)+r)*(mwidth)+(x)+r)
2896 /* mark used slots in the map. If the slot falls outside the map
2897 * (for example, when all icons are placed in line), ignore them. */
2898 for (i = 0; i < dock->max_icons; i++) {
2899 btn = dock->icon_array[i];
2900 if (btn)
2901 slot_map[XY2OFS(btn->xindex, btn->yindex)] = 1;
2904 for (chain = scr->global_icons; chain != NULL; chain = chain->next)
2905 slot_map[XY2OFS(chain->aicon->xindex, chain->aicon->yindex)] = 1;
2907 /* Find closest slot from the center that is free by scanning the
2908 * map from the center to outward in circular passes.
2909 * This will not result in a neat layout, but will be optimal
2910 * in the sense that there will not be holes left.
2912 done = 0;
2913 for (i = 1; i <= r && !done; i++) {
2914 int tx, ty;
2916 /* top and bottom parts of the ring */
2917 for (x = -i; x <= i && !done; x++) {
2918 tx = dock->x_pos + x * ICON_SIZE;
2919 y = -i;
2920 ty = dock->y_pos + y * ICON_SIZE;
2921 if (slot_map[XY2OFS(x, y)] == 0 && onScreen(scr, tx, ty)) {
2922 *x_pos = x;
2923 *y_pos = y;
2924 done = 1;
2925 break;
2927 y = i;
2928 ty = dock->y_pos + y * ICON_SIZE;
2929 if (slot_map[XY2OFS(x, y)] == 0 && onScreen(scr, tx, ty)) {
2930 *x_pos = x;
2931 *y_pos = y;
2932 done = 1;
2933 break;
2936 /* left and right parts of the ring */
2937 for (y = -i + 1; y <= i - 1; y++) {
2938 ty = dock->y_pos + y * ICON_SIZE;
2939 x = -i;
2940 tx = dock->x_pos + x * ICON_SIZE;
2941 if (slot_map[XY2OFS(x, y)] == 0 && onScreen(scr, tx, ty)) {
2942 *x_pos = x;
2943 *y_pos = y;
2944 done = 1;
2945 break;
2947 x = i;
2948 tx = dock->x_pos + x * ICON_SIZE;
2949 if (slot_map[XY2OFS(x, y)] == 0 && onScreen(scr, tx, ty)) {
2950 *x_pos = x;
2951 *y_pos = y;
2952 done = 1;
2953 break;
2957 wfree(slot_map);
2958 #undef XY2OFS
2959 return done;
2962 static void moveDock(WDock *dock, int new_x, int new_y)
2964 WAppIcon *btn;
2965 WDrawerChain *dc;
2966 int i;
2968 if (dock->type == WM_DOCK) {
2969 for (dc = dock->screen_ptr->drawers; dc != NULL; dc = dc->next)
2970 moveDock(dc->adrawer, new_x, dc->adrawer->y_pos - dock->y_pos + new_y);
2973 dock->x_pos = new_x;
2974 dock->y_pos = new_y;
2975 for (i = 0; i < dock->max_icons; i++) {
2976 btn = dock->icon_array[i];
2977 if (btn) {
2978 btn->x_pos = new_x + btn->xindex * ICON_SIZE;
2979 btn->y_pos = new_y + btn->yindex * ICON_SIZE;
2980 XMoveWindow(dpy, btn->icon->core->window, btn->x_pos, btn->y_pos);
2985 static void swapDock(WDock *dock)
2987 WScreen *scr = dock->screen_ptr;
2988 WAppIcon *btn;
2989 int x, i;
2991 if (dock->on_right_side)
2992 x = dock->x_pos = scr->scr_width - ICON_SIZE - DOCK_EXTRA_SPACE;
2993 else
2994 x = dock->x_pos = DOCK_EXTRA_SPACE;
2996 swapDrawers(scr, dock->on_right_side, x);
2998 for (i = 0; i < dock->max_icons; i++) {
2999 btn = dock->icon_array[i];
3000 if (btn) {
3001 btn->x_pos = x;
3002 XMoveWindow(dpy, btn->icon->core->window, btn->x_pos, btn->y_pos);
3006 wScreenUpdateUsableArea(scr);
3009 static pid_t execCommand(WAppIcon *btn, const char *command, WSavedState *state)
3011 WScreen *scr = btn->icon->core->screen_ptr;
3012 pid_t pid;
3013 char **argv;
3014 int argc;
3015 char *cmdline;
3017 cmdline = ExpandOptions(scr, command);
3019 if (scr->flags.dnd_data_convertion_status || !cmdline) {
3020 if (cmdline)
3021 wfree(cmdline);
3022 if (state)
3023 wfree(state);
3024 return 0;
3027 wtokensplit(cmdline, &argv, &argc);
3029 if (!argc) {
3030 if (cmdline)
3031 wfree(cmdline);
3032 if (state)
3033 wfree(state);
3034 return 0;
3037 if ((pid = fork()) == 0) {
3038 char **args;
3039 int i;
3041 SetupEnvironment(scr);
3043 #ifdef HAVE_SETSID
3044 setsid();
3045 #endif
3047 args = malloc(sizeof(char *) * (argc + 1));
3048 if (!args)
3049 exit(111);
3051 for (i = 0; i < argc; i++)
3052 args[i] = argv[i];
3054 args[argc] = NULL;
3055 execvp(argv[0], args);
3056 exit(111);
3058 wtokenfree(argv, argc);
3060 if (pid > 0) {
3061 if (!state) {
3062 state = wmalloc(sizeof(WSavedState));
3063 state->hidden = -1;
3064 state->miniaturized = -1;
3065 state->shaded = -1;
3066 if (btn->dock == scr->dock || btn->dock->type == WM_DRAWER || btn->omnipresent)
3067 state->workspace = -1;
3068 else
3069 state->workspace = scr->current_workspace;
3071 wWindowAddSavedState(btn->wm_instance, btn->wm_class, cmdline, pid, state);
3072 wAddDeathHandler(pid, (WDeathHandler *) trackDeadProcess, btn->dock);
3073 } else if (state) {
3074 wfree(state);
3076 wfree(cmdline);
3077 return pid;
3080 void wDockHideIcons(WDock *dock)
3082 int i;
3084 if (dock == NULL)
3085 return;
3087 for (i = 1; i < dock->max_icons; i++) {
3088 if (dock->icon_array[i])
3089 XUnmapWindow(dpy, dock->icon_array[i]->icon->core->window);
3091 dock->mapped = 0;
3093 dockIconPaint(dock->icon_array[0]);
3096 void wDockShowIcons(WDock *dock)
3098 int i;
3099 WAppIcon *btn;
3101 if (dock == NULL)
3102 return;
3104 btn = dock->icon_array[0];
3105 moveDock(dock, btn->x_pos, btn->y_pos);
3107 /* Deleting any change in stacking level, this function is now only about
3108 mapping icons */
3110 if (!dock->collapsed) {
3111 for (i = 1; i < dock->max_icons; i++) {
3112 if (dock->icon_array[i])
3113 XMapWindow(dpy, dock->icon_array[i]->icon->core->window);
3116 dock->mapped = 1;
3118 dockIconPaint(btn);
3121 void wDockLower(WDock *dock)
3123 int i;
3124 WDrawerChain *dc;
3126 if (dock->type == WM_DOCK) {
3127 for (dc = dock->screen_ptr->drawers; dc != NULL; dc = dc->next)
3128 wDockLower(dc->adrawer);
3130 for (i = 0; i < dock->max_icons; i++) {
3131 if (dock->icon_array[i])
3132 wLowerFrame(dock->icon_array[i]->icon->core);
3136 void wDockRaise(WDock *dock)
3138 int i;
3139 WDrawerChain *dc;
3141 for (i = dock->max_icons - 1; i >= 0; i--) {
3142 if (dock->icon_array[i])
3143 wRaiseFrame(dock->icon_array[i]->icon->core);
3145 if (dock->type == WM_DOCK) {
3146 for (dc = dock->screen_ptr->drawers; dc != NULL; dc = dc->next)
3147 wDockRaise(dc->adrawer);
3151 void wDockRaiseLower(WDock *dock)
3153 if (!dock->icon_array[0]->icon->core->stacking->above
3154 || (dock->icon_array[0]->icon->core->stacking->window_level
3155 != dock->icon_array[0]->icon->core->stacking->above->stacking->window_level))
3156 wDockLower(dock);
3157 else
3158 wDockRaise(dock);
3161 void wDockFinishLaunch(WDock *dock, WAppIcon *icon)
3163 icon->launching = 0;
3164 icon->relaunching = 0;
3165 dockIconPaint(icon);
3168 WAppIcon *wDockFindIconForWindow(WDock *dock, Window window)
3170 WAppIcon *icon;
3171 int i;
3173 for (i = 0; i < dock->max_icons; i++) {
3174 icon = dock->icon_array[i];
3175 if (icon && icon->main_window == window)
3176 return icon;
3178 return NULL;
3181 void wDockTrackWindowLaunch(WDock *dock, Window window)
3183 WAppIcon *icon;
3184 char *wm_class, *wm_instance;
3185 int i;
3186 Bool firstPass = True;
3187 Bool found = False;
3188 char *command = NULL;
3190 if (!PropGetWMClass(window, &wm_class, &wm_instance) || (!wm_class && !wm_instance))
3191 return;
3193 command = GetCommandForWindow(window);
3194 retry:
3195 for (i = 0; i < dock->max_icons; i++) {
3196 icon = dock->icon_array[i];
3197 if (!icon)
3198 continue;
3200 /* app is already attached to icon */
3201 if (icon->main_window == window) {
3202 found = True;
3203 break;
3206 if ((icon->wm_instance || icon->wm_class)
3207 && (icon->launching || !icon->running)) {
3209 if (icon->wm_instance && wm_instance && strcmp(icon->wm_instance, wm_instance) != 0)
3210 continue;
3212 if (icon->wm_class && wm_class && strcmp(icon->wm_class, wm_class) != 0)
3213 continue;
3215 if (firstPass && command && strcmp(icon->command, command) != 0)
3216 continue;
3218 if (!icon->relaunching) {
3219 WApplication *wapp;
3221 /* Possibly an application that was docked with dockit,
3222 * but the user did not update WMState to indicate that
3223 * it was docked by force */
3224 wapp = wApplicationOf(window);
3225 if (!wapp) {
3226 icon->forced_dock = 1;
3227 icon->running = 0;
3229 if (!icon->forced_dock)
3230 icon->main_window = window;
3232 found = True;
3233 if (!wPreferences.no_animations && !icon->launching &&
3234 !dock->screen_ptr->flags.startup && !dock->collapsed) {
3235 WAppIcon *aicon;
3236 int x0, y0;
3238 icon->launching = 1;
3239 dockIconPaint(icon);
3241 aicon = wAppIconCreateForDock(dock->screen_ptr, NULL,
3242 wm_instance, wm_class, TILE_NORMAL);
3243 /* XXX: can: aicon->icon == NULL ? */
3244 PlaceIcon(dock->screen_ptr, &x0, &y0, wGetHeadForWindow(aicon->icon->owner));
3245 wAppIconMove(aicon, x0, y0);
3246 /* Should this always be lowered? -Dan */
3247 if (dock->lowered)
3248 wLowerFrame(aicon->icon->core);
3249 XMapWindow(dpy, aicon->icon->core->window);
3250 aicon->launching = 1;
3251 wAppIconPaint(aicon);
3252 SlideWindow(aicon->icon->core->window, x0, y0, icon->x_pos, icon->y_pos);
3253 XUnmapWindow(dpy, aicon->icon->core->window);
3254 wAppIconDestroy(aicon);
3256 wDockFinishLaunch(dock, icon);
3257 break;
3261 if (firstPass && !found) {
3262 firstPass = False;
3263 goto retry;
3266 if (command)
3267 wfree(command);
3269 if (wm_class)
3270 free(wm_class);
3271 if (wm_instance)
3272 free(wm_instance);
3275 void wClipUpdateForWorkspaceChange(WScreen *scr, int workspace)
3277 if (!wPreferences.flags.noclip) {
3278 scr->clip_icon->dock = scr->workspaces[workspace]->clip;
3279 if (scr->current_workspace != workspace) {
3280 WDock *old_clip = scr->workspaces[scr->current_workspace]->clip;
3281 WAppIconChain *chain = scr->global_icons;
3283 while (chain) {
3284 wDockMoveIconBetweenDocks(chain->aicon->dock,
3285 scr->workspaces[workspace]->clip,
3286 chain->aicon, chain->aicon->xindex, chain->aicon->yindex);
3287 if (scr->workspaces[workspace]->clip->collapsed)
3288 XUnmapWindow(dpy, chain->aicon->icon->core->window);
3289 chain = chain->next;
3292 wDockHideIcons(old_clip);
3293 if (old_clip->auto_raise_lower) {
3294 if (old_clip->auto_raise_magic) {
3295 WMDeleteTimerHandler(old_clip->auto_raise_magic);
3296 old_clip->auto_raise_magic = NULL;
3298 wDockLower(old_clip);
3300 if (old_clip->auto_collapse) {
3301 if (old_clip->auto_expand_magic) {
3302 WMDeleteTimerHandler(old_clip->auto_expand_magic);
3303 old_clip->auto_expand_magic = NULL;
3305 old_clip->collapsed = 1;
3307 wDockShowIcons(scr->workspaces[workspace]->clip);
3312 static void trackDeadProcess(pid_t pid, unsigned char status, WDock *dock)
3314 WAppIcon *icon;
3315 int i;
3317 for (i = 0; i < dock->max_icons; i++) {
3318 icon = dock->icon_array[i];
3319 if (!icon)
3320 continue;
3322 if (icon->launching && icon->pid == pid) {
3323 if (!icon->relaunching) {
3324 icon->running = 0;
3325 icon->main_window = None;
3327 wDockFinishLaunch(dock, icon);
3328 icon->pid = 0;
3329 if (status == 111) {
3330 char msg[PATH_MAX];
3331 char *cmd;
3333 #ifdef XDND
3334 if (icon->drop_launch)
3335 cmd = icon->dnd_command;
3336 else
3337 #endif
3338 if (icon->paste_launch)
3339 cmd = icon->paste_command;
3340 else
3341 cmd = icon->command;
3343 snprintf(msg, sizeof(msg), _("Could not execute command \"%s\""), cmd);
3345 wMessageDialog(dock->screen_ptr, _("Error"), msg, _("OK"), NULL, NULL);
3347 break;
3352 /* This function is called when the dock switches state between
3353 * "normal" (including auto-raise/lower) and "keep on top". It is
3354 * therefore clearly distinct from wDockLower/Raise, which are called
3355 * each time a not-kept-on-top dock is lowered/raised. */
3356 static void toggleLowered(WDock *dock)
3358 WAppIcon *tmp;
3359 WDrawerChain *dc;
3360 int newlevel, i;
3362 if (!dock->lowered) {
3363 newlevel = WMNormalLevel;
3364 dock->lowered = 1;
3365 } else {
3366 newlevel = WMDockLevel;
3367 dock->lowered = 0;
3370 for (i = 0; i < dock->max_icons; i++) {
3371 tmp = dock->icon_array[i];
3372 if (!tmp)
3373 continue;
3375 ChangeStackingLevel(tmp->icon->core, newlevel);
3377 /* When the dock is no longer "on top", explicitly lower it as well.
3378 * It saves some CPU cycles (probably) to do it ourselves here
3379 * rather than calling wDockLower at the end of toggleLowered */
3380 if (dock->lowered)
3381 wLowerFrame(tmp->icon->core);
3384 if (dock->type == WM_DOCK) {
3385 for (dc = dock->screen_ptr->drawers; dc != NULL; dc = dc->next) {
3386 toggleLowered(dc->adrawer);
3388 wScreenUpdateUsableArea(dock->screen_ptr);
3392 static void toggleCollapsed(WDock *dock)
3394 if (dock->collapsed) {
3395 dock->collapsed = 0;
3396 wDockShowIcons(dock);
3397 } else {
3398 dock->collapsed = 1;
3399 wDockHideIcons(dock);
3403 static void openDockMenu(WDock *dock, WAppIcon *aicon, XEvent *event)
3405 WScreen *scr = dock->screen_ptr;
3406 WObjDescriptor *desc;
3407 WMenuEntry *entry;
3408 WApplication *wapp = NULL;
3409 int index = 0;
3410 int x_pos;
3411 int n_selected;
3412 int appIsRunning = aicon->running && aicon->icon && aicon->icon->owner;
3414 if (dock->type == WM_DOCK) {
3415 /* Dock position menu */
3416 updateDockPositionMenu(scr->dock_pos_menu, dock);
3417 dock->menu->flags.realized = 0;
3418 if (!wPreferences.flags.nodrawer) {
3419 /* add a drawer */
3420 entry = dock->menu->entries[++index];
3421 entry->clientdata = aicon;
3422 wMenuSetEnabled(dock->menu, index, True);
3424 } else {
3425 /* clip/drawer options */
3426 if (scr->clip_options)
3427 updateClipOptionsMenu(scr->clip_options, dock);
3429 n_selected = numberOfSelectedIcons(dock);
3431 if (dock->type == WM_CLIP) {
3432 /* Rename Workspace */
3433 entry = dock->menu->entries[++index];
3434 if (aicon == scr->clip_icon) {
3435 entry->callback = renameCallback;
3436 entry->clientdata = dock;
3437 entry->flags.indicator = 0;
3438 entry->text = _("Rename Workspace");
3439 } else {
3440 entry->callback = omnipresentCallback;
3441 entry->clientdata = aicon;
3442 if (n_selected > 0) {
3443 entry->flags.indicator = 0;
3444 entry->text = _("Toggle Omnipresent");
3445 } else {
3446 entry->flags.indicator = 1;
3447 entry->flags.indicator_on = aicon->omnipresent;
3448 entry->flags.indicator_type = MI_CHECK;
3449 entry->text = _("Omnipresent");
3454 /* select/unselect icon */
3455 entry = dock->menu->entries[++index];
3456 entry->clientdata = aicon;
3457 entry->flags.indicator_on = aicon->icon->selected;
3458 wMenuSetEnabled(dock->menu, index, aicon != scr->clip_icon && !wIsADrawer(scr, aicon));
3460 /* select/unselect all icons */
3461 entry = dock->menu->entries[++index];
3462 entry->clientdata = aicon;
3463 if (n_selected > 0)
3464 entry->text = _("Unselect All Icons");
3465 else
3466 entry->text = _("Select All Icons");
3468 wMenuSetEnabled(dock->menu, index, dock->icon_count > 1);
3470 /* keep icon(s) */
3471 entry = dock->menu->entries[++index];
3472 entry->clientdata = aicon;
3473 if (n_selected > 1)
3474 entry->text = _("Keep Icons");
3475 else
3476 entry->text = _("Keep Icon");
3478 wMenuSetEnabled(dock->menu, index, dock->icon_count > 1);
3480 if (dock->type == WM_CLIP) {
3481 /* this is the workspace submenu part */
3482 entry = dock->menu->entries[++index];
3483 if (n_selected > 1)
3484 entry->text = _("Move Icons To");
3485 else
3486 entry->text = _("Move Icon To");
3488 if (scr->clip_submenu)
3489 updateWorkspaceMenu(scr->clip_submenu, aicon);
3491 wMenuSetEnabled(dock->menu, index, !aicon->omnipresent);
3494 /* remove icon(s) */
3495 entry = dock->menu->entries[++index];
3496 entry->clientdata = aicon;
3497 if (n_selected > 1)
3498 entry->text = _("Remove Icons");
3499 else
3500 entry->text = _("Remove Icon");
3502 wMenuSetEnabled(dock->menu, index, dock->icon_count > 1);
3504 /* attract icon(s) */
3505 entry = dock->menu->entries[++index];
3506 entry->clientdata = aicon;
3508 dock->menu->flags.realized = 0;
3509 wMenuRealize(dock->menu);
3512 if (aicon->icon->owner)
3513 wapp = wApplicationOf(aicon->icon->owner->main_window);
3514 else
3515 wapp = NULL;
3517 /* launch */
3518 entry = dock->menu->entries[++index];
3519 entry->clientdata = aicon;
3520 wMenuSetEnabled(dock->menu, index, aicon->command != NULL);
3522 /* unhide here */
3523 entry = dock->menu->entries[++index];
3524 entry->clientdata = aicon;
3525 if (wapp && wapp->flags.hidden)
3526 entry->text = _("Unhide Here");
3527 else
3528 entry->text = _("Bring Here");
3530 wMenuSetEnabled(dock->menu, index, appIsRunning);
3532 /* hide */
3533 entry = dock->menu->entries[++index];
3534 entry->clientdata = aicon;
3535 if (wapp && wapp->flags.hidden)
3536 entry->text = _("Unhide");
3537 else
3538 entry->text = _("Hide");
3540 wMenuSetEnabled(dock->menu, index, appIsRunning);
3542 /* settings */
3543 entry = dock->menu->entries[++index];
3544 entry->clientdata = aicon;
3545 wMenuSetEnabled(dock->menu, index, !aicon->editing && !wPreferences.flags.noupdates);
3547 /* kill or remove drawer */
3548 entry = dock->menu->entries[++index];
3549 entry->clientdata = aicon;
3550 if (wIsADrawer(scr, aicon)) {
3551 entry->callback = removeDrawerCallback;
3552 entry->text = _("Remove drawer");
3553 wMenuSetEnabled(dock->menu, index, True);
3554 } else {
3555 entry->callback = killCallback;
3556 entry->text = _("Kill");
3557 wMenuSetEnabled(dock->menu, index, appIsRunning);
3560 if (!dock->menu->flags.realized)
3561 wMenuRealize(dock->menu);
3563 if (dock->type == WM_CLIP || dock->type == WM_DRAWER) {
3564 /*x_pos = event->xbutton.x_root+2; */
3565 x_pos = event->xbutton.x_root - dock->menu->frame->core->width / 2 - 1;
3566 if (x_pos < 0) {
3567 x_pos = 0;
3568 } else if (x_pos + dock->menu->frame->core->width > scr->scr_width - 2) {
3569 x_pos = scr->scr_width - dock->menu->frame->core->width - 4;
3571 } else {
3572 x_pos = dock->on_right_side ? scr->scr_width - dock->menu->frame->core->width - 3 : 0;
3575 wMenuMapAt(dock->menu, x_pos, event->xbutton.y_root + 2, False);
3577 /* allow drag select */
3578 event->xany.send_event = True;
3579 desc = &dock->menu->menu->descriptor;
3580 (*desc->handle_mousedown) (desc, event);
3583 /******************************************************************/
3584 static void iconDblClick(WObjDescriptor *desc, XEvent *event)
3586 WAppIcon *btn = desc->parent;
3587 WDock *dock = btn->dock;
3588 WApplication *wapp = NULL;
3589 int unhideHere = 0;
3591 if (btn->icon->owner && !(event->xbutton.state & ControlMask)) {
3592 wapp = wApplicationOf(btn->icon->owner->main_window);
3594 assert(wapp != NULL);
3596 unhideHere = (event->xbutton.state & ShiftMask);
3598 /* go to the last workspace that the user worked on the app */
3599 if (wapp->last_workspace != dock->screen_ptr->current_workspace && !unhideHere)
3600 wWorkspaceChange(dock->screen_ptr, wapp->last_workspace);
3602 wUnhideApplication(wapp, event->xbutton.button == Button2, unhideHere);
3604 if (event->xbutton.state & MOD_MASK)
3605 wHideOtherApplications(btn->icon->owner);
3606 } else {
3607 if (event->xbutton.button == Button1) {
3608 if (event->xbutton.state & MOD_MASK) {
3609 /* raise/lower dock */
3610 toggleLowered(dock);
3611 } else if (btn == dock->screen_ptr->clip_icon) {
3612 if (getClipButton(event->xbutton.x, event->xbutton.y) != CLIP_IDLE)
3613 handleClipChangeWorkspace(dock->screen_ptr, event);
3614 else if (wPreferences.flags.clip_merged_in_dock) {
3615 // Is actually the dock
3616 if (btn->command)
3618 if (!btn->launching && (!btn->running || (event->xbutton.state & ControlMask)))
3619 launchDockedApplication(btn, False);
3621 else
3623 wShowInfoPanel(dock->screen_ptr);
3626 else
3627 toggleCollapsed(dock);
3628 } else if (wIsADrawer(dock->screen_ptr, btn)) {
3629 toggleCollapsed(dock);
3630 } else if (btn->command) {
3631 if (!btn->launching && (!btn->running || (event->xbutton.state & ControlMask)))
3632 launchDockedApplication(btn, False);
3633 } else if (btn->xindex == 0 && btn->yindex == 0 && btn->dock->type == WM_DOCK) {
3634 wShowInfoPanel(dock->screen_ptr);
3640 static void handleDockMove(WDock *dock, WAppIcon *aicon, XEvent *event)
3642 WScreen *scr = dock->screen_ptr;
3643 int ofs_x = event->xbutton.x, ofs_y = event->xbutton.y;
3644 WIcon *icon = aicon->icon;
3645 WAppIcon *tmpaicon;
3646 WDrawerChain *dc;
3647 int x = aicon->x_pos, y = aicon->y_pos;;
3648 int shad_x = x, shad_y = y;
3649 XEvent ev;
3650 int grabbed = 0, done, previously_on_right, now_on_right, previous_x_pos, i;
3651 Pixmap ghost = None;
3652 int superfluous = wPreferences.superfluous; /* we catch it to avoid problems */
3654 if (XGrabPointer(dpy, aicon->icon->core->window, True, ButtonMotionMask
3655 | ButtonReleaseMask | ButtonPressMask, GrabModeAsync,
3656 GrabModeAsync, None, None, CurrentTime) != GrabSuccess)
3657 wwarning("pointer grab failed for dock move");
3659 if (dock->type == WM_DRAWER) {
3660 Window wins[2];
3661 wins[0] = icon->core->window;
3662 wins[1] = scr->dock_shadow;
3663 XRestackWindows(dpy, wins, 2);
3664 XMoveResizeWindow(dpy, scr->dock_shadow, aicon->x_pos, aicon->y_pos,
3665 ICON_SIZE, ICON_SIZE);
3666 if (superfluous) {
3667 if (icon->pixmap!=None)
3668 ghost = MakeGhostIcon(scr, icon->pixmap);
3669 else
3670 ghost = MakeGhostIcon(scr, icon->core->window);
3672 XSetWindowBackgroundPixmap(dpy, scr->dock_shadow, ghost);
3673 XClearWindow(dpy, scr->dock_shadow);
3675 XMapWindow(dpy, scr->dock_shadow);
3678 previously_on_right = now_on_right = dock->on_right_side;
3679 previous_x_pos = dock->x_pos;
3680 done = 0;
3681 while (!done) {
3682 WMMaskEvent(dpy, PointerMotionMask | ButtonReleaseMask | ButtonPressMask
3683 | ButtonMotionMask | ExposureMask | EnterWindowMask, &ev);
3684 switch (ev.type) {
3685 case Expose:
3686 WMHandleEvent(&ev);
3687 break;
3689 case EnterNotify:
3690 /* It means the cursor moved so fast that it entered
3691 * something else (if moving slowly, it would have
3692 * stayed in the dock that is being moved. Ignore such
3693 * "spurious" EnterNotifiy's */
3694 break;
3696 case MotionNotify:
3697 if (!grabbed) {
3698 if (abs(ofs_x - ev.xmotion.x) >= MOVE_THRESHOLD
3699 || abs(ofs_y - ev.xmotion.y) >= MOVE_THRESHOLD) {
3700 XChangeActivePointerGrab(dpy, ButtonMotionMask
3701 | ButtonReleaseMask | ButtonPressMask,
3702 wCursor[WCUR_MOVE], CurrentTime);
3703 grabbed = 1;
3705 break;
3707 switch (dock->type) {
3708 case WM_CLIP:
3709 x = ev.xmotion.x_root - ofs_x;
3710 y = ev.xmotion.y_root - ofs_y;
3711 wScreenKeepInside(scr, &x, &y, ICON_SIZE, ICON_SIZE);
3712 moveDock(dock, x, y);
3713 break;
3714 case WM_DOCK:
3715 x = ev.xmotion.x_root - ofs_x;
3716 y = ev.xmotion.y_root - ofs_y;
3717 if (previously_on_right)
3719 now_on_right = (ev.xmotion.x_root >= previous_x_pos - ICON_SIZE);
3721 else
3723 now_on_right = (ev.xmotion.x_root > previous_x_pos + ICON_SIZE * 2);
3725 if (now_on_right != dock->on_right_side)
3727 dock->on_right_side = now_on_right;
3728 swapDock(dock);
3729 wArrangeIcons(scr, False);
3731 // Also perform the vertical move
3732 wScreenKeepInside(scr, &x, &y, ICON_SIZE, ICON_SIZE);
3733 moveDock(dock, dock->x_pos, y);
3734 if (wPreferences.flags.wrap_appicons_in_dock)
3736 for (i = 0; i < dock->max_icons; i++) {
3737 int new_y, new_index, j, ok;
3738 if ((tmpaicon = dock->icon_array[i]) == NULL)
3739 continue;
3740 if (onScreen(scr, tmpaicon->x_pos, tmpaicon->y_pos))
3741 continue;
3742 new_y = (tmpaicon->y_pos + ICON_SIZE * dock->max_icons) % (ICON_SIZE * dock->max_icons);
3743 new_index = (new_y - dock->y_pos) / ICON_SIZE;
3744 if (!onScreen(scr, tmpaicon->x_pos, new_y))
3745 continue;
3746 ok = 1;
3747 for (j = 0; j < dock->max_icons; j++)
3749 if (dock->icon_array[j] != NULL &&
3750 dock->icon_array[j]->yindex == new_index)
3752 ok = 0;
3753 break;
3756 if (!ok || getDrawer(scr, new_index) != NULL)
3757 continue;
3758 wDockReattachIcon(dock, tmpaicon, tmpaicon->xindex, new_index);
3760 for (dc = scr->drawers; dc != NULL; dc = dc->next)
3762 int new_y, new_index, j, ok;
3763 tmpaicon = dc->adrawer->icon_array[0];
3764 if (onScreen(scr, tmpaicon->x_pos, tmpaicon->y_pos))
3765 continue;
3766 new_y = (tmpaicon->y_pos + ICON_SIZE * dock->max_icons) % (ICON_SIZE * dock->max_icons);
3767 new_index = (new_y - dock->y_pos) / ICON_SIZE;
3768 if (!onScreen(scr, tmpaicon->x_pos, new_y))
3769 continue;
3770 ok = 1;
3771 for (j = 0; j < dock->max_icons; j++)
3773 if (dock->icon_array[j] != NULL &&
3774 dock->icon_array[j]->yindex == new_index)
3776 ok = 0;
3777 break;
3780 if (!ok || getDrawer(scr, new_index) != NULL)
3781 continue;
3782 moveDock(dc->adrawer, tmpaicon->x_pos, new_y);
3785 break;
3786 case WM_DRAWER:
3788 WDock *real_dock = dock->screen_ptr->dock;
3789 Bool snapped;
3790 int ix, iy;
3791 x = ev.xmotion.x_root - ofs_x;
3792 y = ev.xmotion.y_root - ofs_y;
3793 snapped = wDockSnapIcon(real_dock, aicon, x, y, &ix, &iy, True);
3794 if (snapped) {
3795 shad_x = real_dock->x_pos + ix * wPreferences.icon_size;
3796 shad_y = real_dock->y_pos + iy * wPreferences.icon_size;
3797 XMoveWindow(dpy, scr->dock_shadow, shad_x, shad_y);
3799 moveDock(dock, x, y);
3800 break;
3803 break;
3805 case ButtonPress:
3806 break;
3808 case ButtonRelease:
3809 if (ev.xbutton.button != event->xbutton.button)
3810 break;
3811 XUngrabPointer(dpy, CurrentTime);
3812 if (dock->type == WM_DRAWER) {
3813 Window *wins[dock->icon_count];
3815 for (i = 0; i < dock->max_icons; i++) {
3816 if ((tmpaicon = dock->icon_array[i]) == NULL)
3817 continue;
3818 wins[ tmpaicon->xindex + (dock->on_right_side ? dock->icon_count - 1 : 0) ] = &tmpaicon->icon->core->window;
3820 SlideWindows(wins, dock->icon_count,
3821 (dock->on_right_side ? x - (dock->icon_count - 1) * ICON_SIZE : x),
3823 (dock->on_right_side ? shad_x - (dock->icon_count - 1) * ICON_SIZE : shad_x),
3824 shad_y);
3825 XUnmapWindow(dpy, scr->dock_shadow);
3826 moveDock(dock, shad_x, shad_y);
3827 XResizeWindow(dpy, scr->dock_shadow, ICON_SIZE, ICON_SIZE);
3829 done = 1;
3830 break;
3833 if (superfluous) {
3834 if (ghost != None)
3835 XFreePixmap(dpy, ghost);
3836 XSetWindowBackground(dpy, scr->dock_shadow, scr->white_pixel);
3841 static int getClipButton(int px, int py)
3843 int pt = (CLIP_BUTTON_SIZE + 2) * ICON_SIZE / 64;
3845 if (px < 0 || py < 0 || px >= ICON_SIZE || py >= ICON_SIZE)
3846 return CLIP_IDLE;
3848 if (py <= pt - ((int)ICON_SIZE - 1 - px))
3849 return CLIP_FORWARD;
3850 else if (px <= pt - ((int)ICON_SIZE - 1 - py))
3851 return CLIP_REWIND;
3853 return CLIP_IDLE;
3856 static void handleClipChangeWorkspace(WScreen *scr, XEvent *event)
3858 XEvent ev;
3859 int done, direction, new_ws;
3860 int new_dir;
3861 WDock *clip = scr->clip_icon->dock;
3863 direction = getClipButton(event->xbutton.x, event->xbutton.y);
3865 clip->lclip_button_pushed = direction == CLIP_REWIND;
3866 clip->rclip_button_pushed = direction == CLIP_FORWARD;
3868 wClipIconPaint(scr->clip_icon);
3869 done = 0;
3870 while (!done) {
3871 WMMaskEvent(dpy, ExposureMask | ButtonMotionMask | ButtonReleaseMask | ButtonPressMask, &ev);
3872 switch (ev.type) {
3873 case Expose:
3874 WMHandleEvent(&ev);
3875 break;
3877 case MotionNotify:
3878 new_dir = getClipButton(ev.xmotion.x, ev.xmotion.y);
3879 if (new_dir != direction) {
3880 direction = new_dir;
3881 clip->lclip_button_pushed = direction == CLIP_REWIND;
3882 clip->rclip_button_pushed = direction == CLIP_FORWARD;
3883 wClipIconPaint(scr->clip_icon);
3885 break;
3887 case ButtonPress:
3888 break;
3890 case ButtonRelease:
3891 if (ev.xbutton.button == event->xbutton.button)
3892 done = 1;
3896 clip->lclip_button_pushed = 0;
3897 clip->rclip_button_pushed = 0;
3899 new_ws = wPreferences.ws_advance || (event->xbutton.state & ControlMask);
3901 if (direction == CLIP_FORWARD) {
3902 if (scr->current_workspace < scr->workspace_count - 1)
3903 wWorkspaceChange(scr, scr->current_workspace + 1);
3904 else if (new_ws && scr->current_workspace < MAX_WORKSPACES - 1)
3905 wWorkspaceChange(scr, scr->current_workspace + 1);
3906 else if (wPreferences.ws_cycle)
3907 wWorkspaceChange(scr, 0);
3908 } else if (direction == CLIP_REWIND) {
3909 if (scr->current_workspace > 0)
3910 wWorkspaceChange(scr, scr->current_workspace - 1);
3911 else if (scr->current_workspace == 0 && wPreferences.ws_cycle)
3912 wWorkspaceChange(scr, scr->workspace_count - 1);
3915 wClipIconPaint(scr->clip_icon);
3918 static void iconMouseDown(WObjDescriptor *desc, XEvent *event)
3920 WAppIcon *aicon = desc->parent;
3921 WDock *dock = aicon->dock;
3922 WScreen *scr = aicon->icon->core->screen_ptr;
3924 if (aicon->editing || WCHECK_STATE(WSTATE_MODAL))
3925 return;
3927 scr->last_dock = dock;
3929 if (dock->menu->flags.mapped)
3930 wMenuUnmap(dock->menu);
3932 if (IsDoubleClick(scr, event)) {
3933 /* double-click was not in the main clip icon */
3934 if (dock->type != WM_CLIP || aicon->xindex != 0 || aicon->yindex != 0
3935 || getClipButton(event->xbutton.x, event->xbutton.y) == CLIP_IDLE) {
3936 iconDblClick(desc, event);
3937 return;
3941 if (event->xbutton.button == Button1) {
3942 if (event->xbutton.state & MOD_MASK)
3943 wDockLower(dock);
3944 else
3945 wDockRaise(dock);
3947 if ((event->xbutton.state & ShiftMask) && aicon != scr->clip_icon && dock->type != WM_DOCK) {
3948 wIconSelect(aicon->icon);
3949 return;
3952 if (aicon->yindex == 0 && aicon->xindex == 0) {
3953 if (getClipButton(event->xbutton.x, event->xbutton.y) != CLIP_IDLE &&
3954 (dock->type == WM_CLIP || (dock->type == WM_DOCK && wPreferences.flags.clip_merged_in_dock)))
3955 handleClipChangeWorkspace(scr, event);
3956 else
3957 handleDockMove(dock, aicon, event);
3958 } else {
3959 Bool hasMoved = wHandleAppIconMove(aicon, event);
3960 if (wPreferences.single_click && !hasMoved)
3961 iconDblClick(desc, event);
3963 } else if (event->xbutton.button == Button2 && aicon == scr->clip_icon) {
3964 if (!scr->clip_ws_menu) {
3965 scr->clip_ws_menu = wWorkspaceMenuMake(scr, False);
3967 if (scr->clip_ws_menu) {
3968 WMenu *wsMenu = scr->clip_ws_menu;
3969 int xpos;
3971 wWorkspaceMenuUpdate(scr, wsMenu);
3973 xpos = event->xbutton.x_root - wsMenu->frame->core->width / 2 - 1;
3974 if (xpos < 0) {
3975 xpos = 0;
3976 } else if (xpos + wsMenu->frame->core->width > scr->scr_width - 2) {
3977 xpos = scr->scr_width - wsMenu->frame->core->width - 4;
3979 wMenuMapAt(wsMenu, xpos, event->xbutton.y_root + 2, False);
3981 desc = &wsMenu->menu->descriptor;
3982 event->xany.send_event = True;
3983 (*desc->handle_mousedown) (desc, event);
3985 } else if (event->xbutton.button == Button2 && dock->type == WM_CLIP &&
3986 (event->xbutton.state & ShiftMask) && aicon != scr->clip_icon) {
3987 wClipMakeIconOmnipresent(aicon, !aicon->omnipresent);
3988 } else if (event->xbutton.button == Button3) {
3989 if (event->xbutton.send_event &&
3990 XGrabPointer(dpy, aicon->icon->core->window, True, ButtonMotionMask
3991 | ButtonReleaseMask | ButtonPressMask, GrabModeAsync,
3992 GrabModeAsync, None, None, CurrentTime) != GrabSuccess) {
3993 wwarning("pointer grab failed for dockicon menu");
3994 return;
3997 openDockMenu(dock, aicon, event);
3998 } else if (event->xbutton.button == Button2) {
3999 WAppIcon *btn = desc->parent;
4001 if (!btn->launching && (!btn->running || (event->xbutton.state & ControlMask)))
4002 launchDockedApplication(btn, True);
4006 static void clipEnterNotify(WObjDescriptor *desc, XEvent *event)
4008 WAppIcon *btn = (WAppIcon *) desc->parent;
4009 WDock *dock, *tmp;
4010 WScreen *scr;
4012 assert(event->type == EnterNotify);
4014 if (desc->parent_type != WCLASS_DOCK_ICON)
4015 return;
4017 scr = btn->icon->core->screen_ptr;
4018 dock = btn->dock;
4020 if (dock == NULL)
4021 return;
4023 /* The auto raise/lower code */
4024 tmp = (dock->type == WM_DRAWER ? scr->dock : dock);
4025 if (tmp->auto_lower_magic) {
4026 WMDeleteTimerHandler(tmp->auto_lower_magic);
4027 tmp->auto_lower_magic = NULL;
4029 if (tmp->auto_raise_lower && !tmp->auto_raise_magic)
4030 tmp->auto_raise_magic = WMAddTimerHandler(wPreferences.clip_auto_raise_delay, clipAutoRaise, (void *) tmp);
4032 if (dock->type != WM_CLIP && dock->type != WM_DRAWER)
4033 return;
4035 /* The auto expand/collapse code */
4036 if (dock->auto_collapse_magic) {
4037 WMDeleteTimerHandler(dock->auto_collapse_magic);
4038 dock->auto_collapse_magic = NULL;
4040 if (dock->auto_collapse && !dock->auto_expand_magic)
4041 dock->auto_expand_magic = WMAddTimerHandler(wPreferences.clip_auto_expand_delay, clipAutoExpand, (void *)dock);
4044 static void clipLeave(WDock *dock)
4046 XEvent event;
4047 WObjDescriptor *desc = NULL;
4048 WDock *tmp;
4050 if (dock == NULL)
4051 return;
4053 if (XCheckTypedEvent(dpy, EnterNotify, &event) != False) {
4054 if (XFindContext(dpy, event.xcrossing.window, wWinContext,
4055 (XPointer *) & desc) != XCNOENT
4056 && desc && desc->parent_type == WCLASS_DOCK_ICON
4057 && ((WAppIcon *) desc->parent)->dock == dock) {
4058 /* We haven't left the dock/clip/drawer yet */
4059 XPutBackEvent(dpy, &event);
4060 return;
4063 XPutBackEvent(dpy, &event);
4064 } else {
4065 /* We entered a withdrawn window, so we're still in Clip */
4066 return;
4069 tmp = (dock->type == WM_DRAWER ? dock->screen_ptr->dock : dock);
4070 if (tmp->auto_raise_magic) {
4071 WMDeleteTimerHandler(tmp->auto_raise_magic);
4072 tmp->auto_raise_magic = NULL;
4074 if (tmp->auto_raise_lower && !tmp->auto_lower_magic)
4075 tmp->auto_lower_magic = WMAddTimerHandler(wPreferences.clip_auto_lower_delay, clipAutoLower, (void *)tmp);
4077 if (dock->type != WM_CLIP && dock->type != WM_DRAWER)
4078 return;
4080 if (dock->auto_expand_magic) {
4081 WMDeleteTimerHandler(dock->auto_expand_magic);
4082 dock->auto_expand_magic = NULL;
4084 if (dock->auto_collapse && !dock->auto_collapse_magic)
4085 dock->auto_collapse_magic = WMAddTimerHandler(wPreferences.clip_auto_collapse_delay, clipAutoCollapse, (void *)dock);
4088 static void clipLeaveNotify(WObjDescriptor *desc, XEvent *event)
4090 WAppIcon *btn = (WAppIcon *) desc->parent;
4092 assert(event->type == LeaveNotify);
4094 if (desc->parent_type != WCLASS_DOCK_ICON)
4095 return;
4097 clipLeave(btn->dock);
4100 static void clipAutoCollapse(void *cdata)
4102 WDock *dock = (WDock *) cdata;
4104 if (dock->type != WM_CLIP && dock->type != WM_DRAWER)
4105 return;
4107 if (dock->auto_collapse) {
4108 dock->collapsed = 1;
4109 wDockHideIcons(dock);
4111 dock->auto_collapse_magic = NULL;
4114 static void clipAutoExpand(void *cdata)
4116 WDock *dock = (WDock *) cdata;
4118 if (dock->type != WM_CLIP && dock->type != WM_DRAWER)
4119 return;
4121 if (dock->auto_collapse) {
4122 dock->collapsed = 0;
4123 wDockShowIcons(dock);
4125 dock->auto_expand_magic = NULL;
4128 static void clipAutoLower(void *cdata)
4130 WDock *dock = (WDock *) cdata;
4132 if (dock->auto_raise_lower)
4133 wDockLower(dock);
4135 dock->auto_lower_magic = NULL;
4138 static void clipAutoRaise(void *cdata)
4140 WDock *dock = (WDock *) cdata;
4142 if (dock->auto_raise_lower)
4143 wDockRaise(dock);
4145 dock->auto_raise_magic = NULL;
4148 static Bool iconCanBeOmnipresent(WAppIcon *aicon)
4150 WScreen *scr = aicon->icon->core->screen_ptr;
4151 WDock *clip;
4152 WAppIcon *btn;
4153 int i, j;
4155 for (i = 0; i < scr->workspace_count; i++) {
4156 clip = scr->workspaces[i]->clip;
4158 if (clip == aicon->dock)
4159 continue;
4161 if (clip->icon_count + scr->global_icon_count >= clip->max_icons)
4162 return False; /* Clip is full in some workspace */
4164 for (j = 0; j < clip->max_icons; j++) {
4165 btn = clip->icon_array[j];
4166 if (btn && btn->xindex == aicon->xindex && btn->yindex == aicon->yindex)
4167 return False;
4171 return True;
4174 int wClipMakeIconOmnipresent(WAppIcon *aicon, int omnipresent)
4176 WScreen *scr = aicon->icon->core->screen_ptr;
4177 WAppIconChain *new_entry, *tmp, *tmp1;
4178 int status = WO_SUCCESS;
4180 if ((scr->dock && aicon->dock == scr->dock) || aicon == scr->clip_icon)
4181 return WO_NOT_APPLICABLE;
4183 if (aicon->omnipresent == omnipresent)
4184 return WO_SUCCESS;
4186 if (omnipresent) {
4187 if (iconCanBeOmnipresent(aicon)) {
4188 aicon->omnipresent = 1;
4189 new_entry = wmalloc(sizeof(WAppIconChain));
4190 new_entry->aicon = aicon;
4191 new_entry->next = scr->global_icons;
4192 scr->global_icons = new_entry;
4193 scr->global_icon_count++;
4194 } else {
4195 aicon->omnipresent = 0;
4196 status = WO_FAILED;
4198 } else {
4199 aicon->omnipresent = 0;
4200 if (aicon == scr->global_icons->aicon) {
4201 tmp = scr->global_icons->next;
4202 wfree(scr->global_icons);
4203 scr->global_icons = tmp;
4204 scr->global_icon_count--;
4205 } else {
4206 tmp = scr->global_icons;
4207 while (tmp->next) {
4208 if (tmp->next->aicon == aicon) {
4209 tmp1 = tmp->next->next;
4210 wfree(tmp->next);
4211 tmp->next = tmp1;
4212 scr->global_icon_count--;
4213 break;
4215 tmp = tmp->next;
4220 wAppIconPaint(aicon);
4222 return status;
4225 static void drawerAppendToChain(WScreen *scr, WDock *drawer)
4227 WDrawerChain **where_to_add;
4229 where_to_add = &scr->drawers;
4230 while ((*where_to_add) != NULL) {
4231 where_to_add = &(*where_to_add)->next;
4234 *where_to_add = wmalloc(sizeof(WDrawerChain));
4235 (*where_to_add)->adrawer = drawer;
4236 (*where_to_add)->next = NULL;
4237 scr->drawer_count++;
4241 static void drawerRemoveFromChain(WScreen *scr, WDock *drawer)
4243 WDrawerChain *next, **to_remove;
4245 to_remove = &scr->drawers;
4246 while (True) {
4247 if (*to_remove == NULL) {
4248 wwarning("The drawer to be removed can not be found.");
4249 return;
4251 if ((*to_remove)->adrawer == drawer)
4252 break;
4254 to_remove = &(*to_remove)->next;
4256 next = (*to_remove)->next;
4257 wfree(*to_remove);
4258 *to_remove = next;
4259 scr->drawer_count--;
4263 /* Don't free the returned string. Duplicate it. */
4264 static char * findUniqueName(WScreen *scr, const char *instance_basename)
4266 static char buffer[128];
4267 WDrawerChain *dc;
4268 int i;
4269 Bool already_in_use = True;
4271 #define UNIQUE_NAME_WATCHDOG 128
4272 for (i = 0; already_in_use && i < UNIQUE_NAME_WATCHDOG; i++) {
4273 snprintf(buffer, sizeof buffer, "%s%d", instance_basename, i);
4275 already_in_use = False;
4277 for (dc = scr->drawers; dc != NULL; dc = dc->next) {
4278 if (!strncmp(dc->adrawer->icon_array[0]->wm_instance, buffer,
4279 sizeof buffer)) {
4280 already_in_use = True;
4281 break;
4286 if (i == UNIQUE_NAME_WATCHDOG)
4287 wwarning("Couldn't find a unique name for drawer in %d attemps.", i);
4288 #undef UNIQUE_NAME_WATCHDOG
4290 return buffer;
4294 static void drawerIconExpose(WObjDescriptor *desc, XEvent *event)
4296 wDrawerIconPaint((WAppIcon *) desc->parent);
4300 static int addADrawer(WScreen *scr)
4302 int i, y, sig, found_y;
4303 WDock *drawer, *dock = scr->dock;
4304 WDrawerChain *dc;
4305 char can_be_here[2 * dock->max_icons - 1];
4307 if (dock->icon_count + scr->drawer_count >= dock->max_icons)
4308 return -1;
4310 for (y = -dock->max_icons + 1; y < dock->max_icons; y++) {
4311 can_be_here[y + dock->max_icons - 1] = True;
4313 for (i = 0; i < dock->max_icons; i++) {
4314 if (dock->icon_array[i] != NULL)
4315 can_be_here[dock->icon_array[i]->yindex + dock->max_icons - 1] = False;
4317 for (dc = scr->drawers; dc != NULL; dc = dc->next) {
4318 y = (int) ((dc->adrawer->y_pos - dock->y_pos) / ICON_SIZE);
4319 can_be_here[y + dock->max_icons - 1] = False;
4322 found_y = False;
4323 for (sig = 1; !found_y && sig > -2; sig -= 2) // 1, then -1
4325 for (y = sig; sig * y < dock->max_icons; y += sig)
4327 if (can_be_here[y + dock->max_icons - 1] &&
4328 onScreen(scr, dock->x_pos, dock->y_pos + y * ICON_SIZE))
4330 found_y = True;
4331 break;
4336 if (!found_y)
4337 /* This can happen even when dock->icon_count + scr->drawer_count
4338 * < dock->max_icons when the dock is not aligned on an
4339 * ICON_SIZE multiple, as some space is lost above and under it */
4340 return -1;
4342 drawer = wDockCreate(scr, WM_DRAWER, NULL);
4343 drawer->lowered = scr->dock->lowered;
4344 drawer->auto_raise_lower = scr->dock->auto_raise_lower;
4345 drawer->x_pos = dock->x_pos;
4346 drawer->y_pos = dock->y_pos + ICON_SIZE * y;
4347 drawer->icon_array[0]->xindex = 0;
4348 drawer->icon_array[0]->yindex = 0;
4349 drawer->icon_array[0]->x_pos = drawer->x_pos;
4350 drawer->icon_array[0]->y_pos = drawer->y_pos;
4351 XMoveWindow(dpy, drawer->icon_array[0]->icon->core->window,
4352 drawer->icon_array[0]->x_pos, drawer->icon_array[0]->y_pos);
4354 return 0;
4358 static void addADrawerCallback(WMenu *menu, WMenuEntry *entry)
4360 assert(entry->clientdata!=NULL);
4361 addADrawer(((WAppIcon *) entry->clientdata)->dock->screen_ptr);
4365 static void drawerDestroy(WDock *drawer)
4367 WScreen *scr;
4368 int i;
4369 WAppIcon *aicon;
4370 WMArray *icons;
4372 if (drawer == NULL)
4373 return;
4375 scr = drawer->screen_ptr;
4377 /* Note regarding menus: we can't delete any dock/clip/drawer menu, because
4378 * that would (attempt to) wfree some memory in gettext library (see menu
4379 * entries that have several "versions", such like "Hide" and "Unhide"). */
4381 wDefaultPurgeInfo(scr, drawer->icon_array[0]->wm_instance,
4382 drawer->icon_array[0]->wm_class);
4383 icons = WMCreateArray(drawer->icon_count - 1);
4384 for (i = 1; i < drawer->max_icons; i++) {
4385 if (!(aicon = drawer->icon_array[i]))
4386 continue;
4387 WMAddToArray(icons, aicon);
4389 removeIcons(icons, drawer);
4391 if (drawer->auto_collapse_magic) {
4392 WMDeleteTimerHandler(drawer->auto_collapse_magic);
4393 drawer->auto_collapse_magic = NULL;
4395 if (drawer->auto_lower_magic) {
4396 WMDeleteTimerHandler(drawer->auto_lower_magic);
4397 drawer->auto_lower_magic = NULL;
4400 wAppIconDestroy(drawer->icon_array[0]);
4401 wfree(drawer->icon_array);
4402 wfree(drawer);
4404 drawerRemoveFromChain(scr, drawer);
4405 if (scr->last_dock == drawer)
4406 scr->last_dock = NULL;
4407 if (scr->attracting_drawer == drawer)
4408 scr->attracting_drawer = NULL;
4412 static void removeDrawerCallback(WMenu *menu, WMenuEntry *entry)
4414 WDock *dock = ((WAppIcon*)entry->clientdata)->dock;
4415 assert(dock != NULL);
4417 if (dock->icon_count > 1) {
4418 if (wMessageDialog(dock->screen_ptr, _("Drawer"),
4419 _("All icons in this drawer will be detached!"),
4420 _("OK"), _("Cancel"), NULL) != WAPRDefault)
4421 return;
4423 drawerDestroy(dock);
4427 void wDrawerIconPaint(WAppIcon *dicon)
4429 Window win = dicon->icon->core->window;
4430 WScreen *scr = dicon->icon->core->screen_ptr;
4431 XPoint p[4];
4432 GC gc = scr->draw_gc;
4433 WMColor *color;
4435 wIconPaint(dicon->icon);
4437 if (!dicon->dock->collapsed)
4438 color = scr->clip_title_color[CLIP_NORMAL];
4439 else
4440 color = scr->clip_title_color[CLIP_COLLAPSED];
4441 XSetForeground(dpy, gc, WMColorPixel(color));
4443 if (dicon->dock->on_right_side) {
4444 p[0].x = p[3].x = 10;
4445 p[0].y = p[3].y = ICON_SIZE / 2 - 5;
4446 p[1].x = 10;
4447 p[1].y = ICON_SIZE / 2 + 5;
4448 p[2].x = 5;
4449 p[2].y = ICON_SIZE / 2;
4451 else {
4452 p[0].x = p[3].x = ICON_SIZE-1 - 10;
4453 p[0].y = p[3].y = ICON_SIZE / 2 - 5;
4454 p[1].x = ICON_SIZE-1 - 10;
4455 p[1].y = ICON_SIZE / 2 + 5;
4456 p[2].x = ICON_SIZE-1 - 5;
4457 p[2].y = ICON_SIZE / 2;
4459 XFillPolygon(dpy, win, gc, p,3,Convex,CoordModeOrigin);
4460 XDrawLines(dpy, win, gc, p,4,CoordModeOrigin);
4464 RImage* wDrawerMakeTile(WScreen *scr, RImage *normalTile)
4466 RImage *tile = RCloneImage(normalTile);
4467 RColor dark;
4468 RColor light;
4470 dark.alpha = 0;
4471 dark.red = dark.green = dark.blue = 60;
4473 light.alpha = 0;
4474 light.red = light.green = light.blue = 80;
4476 /* arrow bevel */
4477 if (!scr->dock || scr->dock->on_right_side) {
4478 ROperateLine(tile, RSubtractOperation, 11, ICON_SIZE / 2 - 7,
4479 4, ICON_SIZE / 2, &dark); /* / */
4480 ROperateLine(tile, RSubtractOperation, 11, ICON_SIZE / 2 + 7,
4481 4, ICON_SIZE / 2, &dark); /* \ */
4482 ROperateLine(tile, RAddOperation, 11, ICON_SIZE / 2 - 7,
4483 11, ICON_SIZE / 2 + 7, &light); /* | */
4485 else {
4486 ROperateLine(tile, RSubtractOperation, ICON_SIZE-1 - 11, ICON_SIZE / 2 - 7,
4487 ICON_SIZE-1 - 4, ICON_SIZE / 2, &dark); /* \ */
4488 ROperateLine(tile, RAddOperation, ICON_SIZE-1 - 11, ICON_SIZE / 2 + 7,
4489 ICON_SIZE-1 - 4, ICON_SIZE / 2, &light); /* / */
4490 ROperateLine(tile, RSubtractOperation, ICON_SIZE-1 - 11, ICON_SIZE / 2 - 7,
4491 ICON_SIZE-1 - 11, ICON_SIZE / 2 + 7, &dark); /* | */
4493 return tile;
4497 static void swapDrawer(WDock *drawer, int new_x)
4499 int i;
4501 drawer->on_right_side = !drawer->on_right_side;
4502 drawer->x_pos = new_x;
4504 for (i = 0; i < drawer->max_icons; i++) {
4505 WAppIcon *ai;
4506 if ((ai = drawer->icon_array[i]) == NULL)
4507 continue;
4508 ai->xindex *= -1; /* so A B C becomes C B A */
4509 ai->x_pos = new_x + ai->xindex * ICON_SIZE;
4511 /* Update drawer's tile */
4512 if (i == 0) {
4513 wIconUpdate(ai->icon);
4514 wDrawerIconPaint(ai);
4516 XMoveWindow(dpy, ai->icon->core->window, ai->x_pos, ai->y_pos);
4521 static void swapDrawers(WScreen *scr, int on_right_side, int new_x)
4523 WDrawerChain *dc;
4525 if (scr->drawer_tile) {
4526 RReleaseImage(scr->drawer_tile);
4528 scr->drawer_tile = wDrawerMakeTile(scr, scr->icon_tile);
4530 for (dc = scr->drawers; dc != NULL; dc = dc->next) {
4531 swapDrawer(dc->adrawer, new_x);
4532 assert(dc->adrawer->on_right_side == on_right_side);
4537 int wIsADrawer(WScreen *scr, WAppIcon *aicon)
4539 return aicon && aicon->dock &&
4540 aicon->dock->type == WM_DRAWER && aicon->dock->icon_array[0] == aicon;
4544 static WDock* getDrawer(WScreen *scr, int y_index)
4546 WDrawerChain *dc;
4548 for (dc = scr->drawers; dc != NULL; dc = dc->next) {
4549 if (dc->adrawer->y_pos - scr->dock->y_pos == y_index * ICON_SIZE)
4550 return dc->adrawer;
4552 return NULL;
4557 /* Find the "hole" a moving appicon created when snapped into the
4558 * drawer. redocking is a boolean. If the moving appicon comes from the
4559 * drawer, drawer->icon_count is correct. If not, redocking is then false and
4560 * there are now drawer->icon_count plus one appicons in the drawer. */
4561 static int indexOfHole(WDock *drawer, WAppIcon *moving_aicon, int redocking)
4563 int index_of_hole, i;
4565 /* Classic interview question...
4567 * We have n-1 (n = drawer->icon_count-1 or drawer->icon_count, see
4568 * redocking) appicons, whose xindex are unique in [1..n]. One is missing:
4569 * that's where the ghost of the moving appicon is, that's what the
4570 * function should return.
4572 * We compute 1+2+...+n (this sum is equal to n*(n+1)/2), we substract to
4573 * this sum the xindex of each of the n-1 appicons, and we get the correct
4574 * index! */
4576 if (redocking) {
4577 index_of_hole = (drawer->icon_count - 1) * drawer->icon_count / 2;
4578 } else {
4579 index_of_hole = drawer->icon_count * (drawer->icon_count + 1) / 2;
4581 index_of_hole *= (drawer->on_right_side ? -1 : 1);
4583 for (i = 1; i < drawer->max_icons; i++) {
4584 if (drawer->icon_array[i] && drawer->icon_array[i] != moving_aicon)
4585 index_of_hole -= drawer->icon_array[i]->xindex;
4587 /* wmessage(" Index of the moving appicon is %d (%sredocking)", index_of_hole, (redocking ? "" : "not ")); */
4588 if (abs(index_of_hole) > abs(drawer->icon_count) - (redocking ? 1 : 0))
4589 wwarning(" index_of_hole is too large ! (%d greater than %d)",
4590 index_of_hole, abs(drawer->icon_count) - (redocking ? 1 : 0));
4591 if (index_of_hole == 0)
4592 wwarning(" index_of_hole == 0 (%sredocking, icon_count == %d)", (redocking ? "" : "not "), drawer->icon_count);
4594 return index_of_hole;
4598 void wSlideAppicons(WAppIcon **appicons, int n, int to_the_left)
4600 int i;
4601 int leftmost = -1, min_index = 9999, from_x = -1; // leftmost and from_x initialized to avoid warning
4602 Window *wins[n];
4603 WAppIcon *aicon;
4605 if (n < 1)
4606 return;
4608 for (i = 0; i < n; i++) {
4609 aicon = appicons[i];
4610 aicon->xindex += (to_the_left ? -1 : +1);
4611 if (aicon->xindex < min_index) {
4612 min_index = aicon->xindex;
4613 leftmost = i;
4614 from_x = aicon->x_pos;
4616 aicon->x_pos += (to_the_left ? -ICON_SIZE : +ICON_SIZE);
4619 for (i = 0; i < n; i++) {
4620 aicon = appicons[i];
4621 wins[aicon->xindex - min_index] = &aicon->icon->core->window;
4623 aicon = appicons[leftmost];
4624 SlideWindows(wins, n, from_x, aicon->y_pos, aicon->x_pos, aicon->y_pos);
4628 void wDrawerFillTheGap(WDock *drawer, WAppIcon *aicon, Bool redocking)
4630 int i, j;
4631 int index_of_hole = indexOfHole(drawer, aicon, redocking);
4632 WAppIcon *aicons_to_shift[drawer->icon_count];
4634 j = 0;
4635 for (i = 0; i < drawer->max_icons; i++) {
4636 WAppIcon *ai = drawer->icon_array[i];
4637 if (ai && ai != aicon &&
4638 abs(ai->xindex) > abs(index_of_hole))
4639 aicons_to_shift[j++] = ai;
4641 if (j != drawer->icon_count - abs(index_of_hole) - (redocking ? 1 : 0))
4642 wwarning("Removing aicon at index %d from %s: j=%d but should be %d",
4643 index_of_hole, drawer->icon_array[0]->wm_instance,
4644 j, drawer->icon_count - abs(index_of_hole) - (redocking ? 1 : 0));
4645 wSlideAppicons(aicons_to_shift, j, !drawer->on_right_side);
4649 static void drawerConsolidateIcons(WDock *drawer)
4651 WAppIcon *aicons_to_shift[drawer->icon_count];
4652 int maxRemaining = 0;
4653 int sum = 0;
4654 int i;
4655 for (i = 0; i < drawer->max_icons; i++) {
4656 WAppIcon *ai = drawer->icon_array[i];
4657 if (ai == NULL)
4658 continue;
4659 sum += abs(ai->xindex);
4660 if (abs(ai->xindex) > maxRemaining)
4661 maxRemaining = abs(ai->xindex);
4663 while (sum != maxRemaining * (maxRemaining + 1) / 2) { // while there is a hole
4664 WAppIcon *ai;
4665 int n;
4666 // Look up for the hole at max position
4667 int maxDeleted;
4668 for (maxDeleted = maxRemaining - 1; maxDeleted > 0; maxDeleted--) {
4669 Bool foundAppIconThere = False;
4670 for (i = 0; i < drawer->max_icons; i++) {
4671 WAppIcon *ai = drawer->icon_array[i];
4672 if (ai == NULL)
4673 continue;
4674 if (abs(ai->xindex) == maxDeleted) {
4675 foundAppIconThere = True;
4676 break;
4679 if (!foundAppIconThere)
4680 break;
4682 assert(maxDeleted > 0); // would mean while test is wrong
4683 n = 0;
4684 for (i = 0; i < drawer->max_icons; i++) {
4685 ai = drawer->icon_array[i];
4686 if (ai != NULL && abs(ai->xindex) > maxDeleted)
4687 aicons_to_shift[n++] = ai;
4689 assert(n == maxRemaining - maxDeleted); // for the code review ;-)
4690 wSlideAppicons(aicons_to_shift, n, !drawer->on_right_side);
4691 // Efficient beancounting
4692 maxRemaining -= 1;
4693 sum -= n;
4699 /* similar to wDockRestoreState, but a lot a specific stuff too... */
4700 static WDock * drawerRestoreState(WScreen *scr, WMPropList *drawer_state)
4702 WDock *drawer;
4703 WMPropList *apps, *value, *dock_state;
4704 WAppIcon *aicon;
4705 int count, i;
4707 if (!drawer_state)
4708 return NULL;
4710 make_keys();
4712 WMRetainPropList(drawer_state);
4714 /* Get the instance name, and create a drawer */
4715 value = WMGetFromPLDictionary(drawer_state, dName);
4716 drawer = wDockCreate(scr, WM_DRAWER, WMGetFromPLString(value));
4718 /* restore DnD command and paste command */
4719 #ifdef XDND
4720 value = WMGetFromPLDictionary(drawer_state, dDropCommand);
4721 if (value && WMIsPLString(value))
4722 drawer->icon_array[0]->dnd_command = wstrdup(WMGetFromPLString(value));
4723 #endif /* XDND */
4725 value = WMGetFromPLDictionary(drawer_state, dPasteCommand);
4726 if (value && WMIsPLString(value))
4727 drawer->icon_array[0]->paste_command = wstrdup(WMGetFromPLString(value));
4729 /* restore position */
4730 value = WMGetFromPLDictionary(drawer_state, dPosition);
4731 if (!value || !WMIsPLString(value))
4732 COMPLAIN("Position");
4733 else {
4734 int x, y, y_index;
4735 if (sscanf(WMGetFromPLString(value), "%i,%i", &x, &y) != 2)
4736 COMPLAIN("Position");
4738 /* check position sanity */
4739 if (x != scr->dock->x_pos) {
4740 x = scr->dock->x_pos;
4742 y_index = (y - scr->dock->y_pos) / ICON_SIZE;
4743 if (y_index >= scr->dock->max_icons) {
4744 /* Here we should do something more intelligent, since it
4745 * can happen even if the user hasn't hand-edited his
4746 * G/D/State file (but uses a lower resolution). */
4747 y_index = scr->dock->max_icons - 1;
4749 y = scr->dock->y_pos + y_index * ICON_SIZE;
4750 moveDock(drawer, x, y);
4753 /* restore dock properties (applist and others) */
4754 dock_state = WMGetFromPLDictionary(drawer_state, dDock);
4756 /* restore lowered/raised state: same as scr->dock, no matter what */
4757 drawer->lowered = scr->dock->lowered;
4758 if (!drawer->lowered)
4759 ChangeStackingLevel(drawer->icon_array[0]->icon->core, WMDockLevel);
4760 else
4761 ChangeStackingLevel(drawer->icon_array[0]->icon->core, WMNormalLevel);
4762 wRaiseFrame(drawer->icon_array[0]->icon->core);
4764 /* restore collapsed state */
4765 drawer->collapsed = 0;
4766 value = WMGetFromPLDictionary(dock_state, dCollapsed);
4767 if (value && strcasecmp(WMGetFromPLString(value), "YES") == 0) {
4768 drawer->collapsed = 1;
4770 /* restore auto-collapsed state */
4771 value = WMGetFromPLDictionary(dock_state, dAutoCollapse);
4772 if (value && strcasecmp(WMGetFromPLString(value), "YES") == 0) {
4773 drawer->auto_collapse = 1;
4774 drawer->collapsed = 1;
4775 } else {
4776 drawer->auto_collapse = 0; // because wDockCreate sets it (drawers only)
4779 /* restore auto-raise/lower state: same as scr->dock, no matter what */
4780 drawer->auto_raise_lower = scr->dock->auto_raise_lower;
4782 /* restore attract icons state */
4783 drawer->attract_icons = 0;
4784 value = WMGetFromPLDictionary(dock_state, dAutoAttractIcons);
4785 if (value && strcasecmp(WMGetFromPLString(value), "YES") == 0) {
4786 drawer->attract_icons = 1;
4787 scr->attracting_drawer = drawer;
4790 /* application list */
4791 apps = WMGetFromPLDictionary(dock_state, dApplications);
4793 if (!apps) {
4794 goto finish;
4797 count = WMGetPropListItemCount(apps);
4799 if (count == 0)
4800 goto finish;
4802 for (i=0; i<count; i++) {
4803 if (drawer->icon_count >= drawer->max_icons) {
4804 wwarning(_("there are too many icons stored in drawer. Ignoring what doesn't fit"));
4805 break;
4808 value = WMGetFromPLArray(apps, i);
4809 aicon = restore_icon_state(scr, value, WM_DRAWER, drawer->icon_count);
4811 drawer->icon_array[drawer->icon_count] = aicon;
4813 if (aicon) {
4814 aicon->dock = drawer;
4815 aicon->x_pos = drawer->x_pos + (aicon->xindex * ICON_SIZE);
4816 aicon->y_pos = drawer->y_pos + (aicon->yindex * ICON_SIZE);
4818 if (!drawer->lowered)
4819 ChangeStackingLevel(aicon->icon->core, WMDockLevel);
4820 else
4821 ChangeStackingLevel(aicon->icon->core, WMNormalLevel);
4823 wCoreConfigure(aicon->icon->core, aicon->x_pos, aicon->y_pos, 0, 0);
4825 if (!drawer->collapsed)
4826 XMapWindow(dpy, aicon->icon->core->window);
4827 wRaiseFrame(aicon->icon->core);
4829 drawer->icon_count++;
4833 finish:
4834 WMReleasePropList(drawer_state);
4836 return drawer;
4840 /* Same kind of comment than for previous function: this function is
4841 * very similar to make_icon_state, but has substential differences as
4842 * well. */
4843 static WMPropList *drawerSaveState(WScreen *scr, WDock *drawer)
4845 WMPropList *pstr, *drawer_state;
4846 WAppIcon *ai;
4847 char buffer[64];
4849 ai = drawer->icon_array[0];
4850 /* Store its name */
4851 pstr = WMCreatePLString(ai->wm_instance);
4852 drawer_state = WMCreatePLDictionary(dName, pstr, NULL); /* we need this final NULL */
4853 WMReleasePropList(pstr);
4855 /* Store its position */
4856 snprintf(buffer, sizeof(buffer), "%i,%i", ai->x_pos, ai->y_pos);
4857 pstr = WMCreatePLString(buffer);
4858 WMPutInPLDictionary(drawer_state, dPosition, pstr);
4859 WMReleasePropList(pstr);
4861 #ifdef XDND
4862 /* Store its DnD command */
4863 if (ai->dnd_command) {
4864 pstr = WMCreatePLString(ai->dnd_command);
4865 WMPutInPLDictionary(drawer_state, dDropCommand, pstr);
4866 WMReleasePropList(pstr);
4868 #endif /* XDND */
4870 /* Store its paste command */
4871 if (ai->paste_command) {
4872 pstr = WMCreatePLString(ai->paste_command);
4873 WMPutInPLDictionary(drawer_state, dPasteCommand, pstr);
4874 WMReleasePropList(pstr);
4877 /* Store applications list and other properties */
4878 pstr = dockSaveState(drawer);
4879 WMPutInPLDictionary(drawer_state, dDock, pstr);
4880 WMReleasePropList(pstr);
4882 return drawer_state;
4886 void wDrawersSaveState(WScreen *scr)
4888 WMPropList *all_drawers, *drawer_state;
4889 int i;
4890 WDrawerChain *dc;
4892 make_keys();
4894 all_drawers = WMCreatePLArray(NULL);
4895 for (i=0, dc = scr->drawers;
4896 i < scr->drawer_count;
4897 i++, dc = dc->next) {
4898 drawer_state = drawerSaveState(scr, dc->adrawer);
4899 WMAddToPLArray(all_drawers, drawer_state);
4900 WMReleasePropList(drawer_state);
4902 WMPutInPLDictionary(scr->session_state, dDrawers, all_drawers);
4903 WMReleasePropList(all_drawers);
4907 void wDrawersRestoreState(WScreen *scr)
4909 WMPropList *all_drawers, *drawer_state;
4910 int i;
4912 make_keys();
4914 if (scr->session_state == NULL)
4915 return;
4917 all_drawers = WMGetFromPLDictionary(scr->session_state, dDrawers);
4918 if (!all_drawers)
4919 return;
4921 for (i = 0; i < WMGetPropListItemCount(all_drawers); i++) {
4922 drawer_state = WMGetFromPLArray(all_drawers, i);
4923 drawerRestoreState(scr, drawer_state);
4924 // Note: scr->drawers was updated when the the drawer was created