Bring back some "About" information when double-clicking the dock
[wmaker-crm.git] / src / dock.c
blob476f59e19c3edf6fbddb48524fea215b7c3a551b
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 <stdlib.h>
28 #include <string.h>
29 #include <strings.h>
30 #include <unistd.h>
31 #include <math.h>
32 #include <limits.h>
34 #ifndef PATH_MAX
35 #define PATH_MAX DEFAULT_PATH_MAX
36 #endif
38 #include "WindowMaker.h"
39 #include "wcore.h"
40 #include "window.h"
41 #include "icon.h"
42 #include "appicon.h"
43 #include "actions.h"
44 #include "stacking.h"
45 #include "dock.h"
46 #include "dockedapp.h"
47 #include "dialog.h"
48 #include "main.h"
49 #include "properties.h"
50 #include "menu.h"
51 #include "client.h"
52 #include "defaults.h"
53 #include "workspace.h"
54 #include "framewin.h"
55 #include "superfluous.h"
56 #include "xinerama.h"
58 /**** Local variables ****/
59 #define CLIP_REWIND 1
60 #define CLIP_IDLE 0
61 #define CLIP_FORWARD 2
63 /**** Global variables ****/
64 extern Cursor wCursor[WCUR_LAST];
65 extern WPreferences wPreferences;
66 extern XContext wWinContext;
67 extern void appIconMouseDown(WObjDescriptor *desc, XEvent *event);
69 #define MOD_MASK wPreferences.modifier_mask
70 #define ICON_SIZE wPreferences.icon_size
72 /***** Local variables ****/
74 static WMPropList *dCommand = NULL;
75 static WMPropList *dPasteCommand = NULL;
76 #ifdef XDND /* XXX was OFFIX */
77 static WMPropList *dDropCommand = NULL;
78 #endif
79 static WMPropList *dAutoLaunch, *dLock;
80 static WMPropList *dName, *dForced, *dBuggyApplication, *dYes, *dNo;
81 static WMPropList *dHost, *dDock, *dClip;
82 static WMPropList *dAutoAttractIcons;
84 static WMPropList *dPosition, *dApplications, *dLowered, *dCollapsed;
86 static WMPropList *dAutoCollapse, *dAutoRaiseLower, *dOmnipresent;
88 static void dockIconPaint(WAppIcon *btn);
90 static void iconMouseDown(WObjDescriptor *desc, XEvent *event);
92 static pid_t execCommand(WAppIcon *btn, char *command, WSavedState *state);
94 static void trackDeadProcess(pid_t pid, unsigned char status, WDock *dock);
96 static int getClipButton(int px, int py);
98 static void toggleLowered(WDock *dock);
100 static void toggleCollapsed(WDock *dock);
102 static void clipIconExpose(WObjDescriptor *desc, XEvent *event);
104 static void clipLeave(WDock *dock);
106 static void handleClipChangeWorkspace(WScreen *scr, XEvent *event);
108 static Bool moveIconBetweenDocks(WDock *src, WDock *dest, WAppIcon *icon, int x, int y);
110 static void clipEnterNotify(WObjDescriptor *desc, XEvent *event);
111 static void clipLeaveNotify(WObjDescriptor *desc, XEvent *event);
112 static void clipAutoCollapse(void *cdata);
113 static void clipAutoExpand(void *cdata);
114 static void launchDockedApplication(WAppIcon *btn, Bool withSelection);
116 static void clipAutoLower(void *cdata);
117 static void clipAutoRaise(void *cdata);
118 static void reattachIcon(WDock *dock, WAppIcon *icon, int x, int y);
119 static WAppIcon *mainIconCreate(WScreen *scr, int type);
121 static int onScreen(WScreen *scr, int x, int y);
123 static void make_keys(void)
125 if (dCommand != NULL)
126 return;
128 dCommand = WMRetainPropList(WMCreatePLString("Command"));
129 dPasteCommand = WMRetainPropList(WMCreatePLString("PasteCommand"));
130 #ifdef XDND
131 dDropCommand = WMRetainPropList(WMCreatePLString("DropCommand"));
132 #endif
133 dLock = WMRetainPropList(WMCreatePLString("Lock"));
134 dAutoLaunch = WMRetainPropList(WMCreatePLString("AutoLaunch"));
135 dName = WMRetainPropList(WMCreatePLString("Name"));
136 dForced = WMRetainPropList(WMCreatePLString("Forced"));
137 dBuggyApplication = WMRetainPropList(WMCreatePLString("BuggyApplication"));
138 dYes = WMRetainPropList(WMCreatePLString("Yes"));
139 dNo = WMRetainPropList(WMCreatePLString("No"));
140 dHost = WMRetainPropList(WMCreatePLString("Host"));
142 dPosition = WMCreatePLString("Position");
143 dApplications = WMCreatePLString("Applications");
144 dLowered = WMCreatePLString("Lowered");
145 dCollapsed = WMCreatePLString("Collapsed");
146 dAutoCollapse = WMCreatePLString("AutoCollapse");
147 dAutoRaiseLower = WMCreatePLString("AutoRaiseLower");
148 dAutoAttractIcons = WMCreatePLString("AutoAttractIcons");
150 dOmnipresent = WMCreatePLString("Omnipresent");
152 dDock = WMCreatePLString("Dock");
153 dClip = WMCreatePLString("Clip");
156 static void renameCallback(WMenu *menu, WMenuEntry *entry)
158 WDock *dock = entry->clientdata;
159 char buffer[128];
160 int wspace;
161 char *name;
163 assert(entry->clientdata != NULL);
165 wspace = dock->screen_ptr->current_workspace;
167 name = wstrdup(dock->screen_ptr->workspaces[wspace]->name);
169 snprintf(buffer, sizeof(buffer), _("Type the name for workspace %i:"), wspace + 1);
170 if (wInputDialog(dock->screen_ptr, _("Rename Workspace"), buffer, &name))
171 wWorkspaceRename(dock->screen_ptr, wspace, name);
173 wfree(name);
176 static void toggleLoweredCallback(WMenu *menu, WMenuEntry *entry)
178 assert(entry->clientdata != NULL);
180 toggleLowered(entry->clientdata);
182 entry->flags.indicator_on = !(((WDock *) entry->clientdata)->lowered);
184 wMenuPaint(menu);
187 static int matchWindow(const void *item, const void *cdata)
189 return (((WFakeGroupLeader *) item)->leader == (Window) cdata);
192 static void killCallback(WMenu *menu, WMenuEntry *entry)
194 WScreen *scr = menu->menu->screen_ptr;
195 WAppIcon *icon;
196 WFakeGroupLeader *fPtr;
197 char *buffer, *shortname, **argv;
198 char *basename(const char *shortname);
199 int argc;
201 if (!WCHECK_STATE(WSTATE_NORMAL))
202 return;
204 assert(entry->clientdata != NULL);
206 icon = (WAppIcon *) entry->clientdata;
208 icon->editing = 1;
210 WCHANGE_STATE(WSTATE_MODAL);
212 /* strip away dir names */
213 shortname = basename(icon->command);
214 /* separate out command options */
215 wtokensplit(shortname, &argv, &argc);
217 buffer = wstrconcat(argv[0],
218 _(" will be forcibly closed.\n"
219 "Any unsaved changes will be lost.\n" "Please confirm."));
221 if (icon->icon && icon->icon->owner) {
222 fPtr = icon->icon->owner->fake_group;
223 } else {
224 /* is this really necessary? can we kill a non-running dock icon? */
225 Window win = icon->main_window;
226 int index;
228 index = WMFindInArray(scr->fakeGroupLeaders, matchWindow, (void *)win);
229 if (index != WANotFound)
230 fPtr = WMGetFromArray(scr->fakeGroupLeaders, index);
231 else
232 fPtr = NULL;
235 if (wPreferences.dont_confirm_kill
236 || wMessageDialog(menu->frame->screen_ptr, _("Kill Application"),
237 buffer, _("Yes"), _("No"), NULL) == WAPRDefault) {
238 if (fPtr != NULL) {
239 WWindow *wwin, *twin;
241 wwin = scr->focused_window;
242 while (wwin) {
243 twin = wwin->prev;
244 if (wwin->fake_group == fPtr)
245 wClientKill(wwin);
247 wwin = twin;
249 } else if (icon->icon && icon->icon->owner) {
250 wClientKill(icon->icon->owner);
254 wfree(buffer);
255 wtokenfree(argv, argc);
257 icon->editing = 0;
259 WCHANGE_STATE(WSTATE_NORMAL);
262 /* TODO: replace this function with a member of the dock struct */
263 static int numberOfSelectedIcons(WDock *dock)
265 WAppIcon *aicon;
266 int i, n;
268 n = 0;
269 for (i = 1; i < dock->max_icons; i++) {
270 aicon = dock->icon_array[i];
271 if (aicon && aicon->icon->selected)
272 n++;
275 return n;
278 static WMArray *getSelected(WDock *dock)
280 WMArray *ret = WMCreateArray(8);
281 WAppIcon *btn;
282 int i;
284 for (i = 1; i < dock->max_icons; i++) {
285 btn = dock->icon_array[i];
286 if (btn && btn->icon->selected)
287 WMAddToArray(ret, btn);
290 return ret;
293 static void paintClipButtons(WAppIcon *clipIcon, Bool lpushed, Bool rpushed)
295 Window win = clipIcon->icon->core->window;
296 WScreen *scr = clipIcon->icon->core->screen_ptr;
297 XPoint p[4];
298 int pt = CLIP_BUTTON_SIZE * ICON_SIZE / 64;
299 int tp = ICON_SIZE - pt;
300 int as = pt - 15; /* 15 = 5+5+5 */
301 GC gc = scr->draw_gc; /* maybe use WMColorGC() instead here? */
302 WMColor *color;
304 color = scr->clip_title_color[CLIP_NORMAL];
306 XSetForeground(dpy, gc, WMColorPixel(color));
308 if (rpushed) {
309 p[0].x = tp + 1;
310 p[0].y = 1;
311 p[1].x = ICON_SIZE - 2;
312 p[1].y = 1;
313 p[2].x = ICON_SIZE - 2;
314 p[2].y = pt - 1;
315 } else if (lpushed) {
316 p[0].x = 1;
317 p[0].y = tp;
318 p[1].x = pt;
319 p[1].y = ICON_SIZE - 2;
320 p[2].x = 1;
321 p[2].y = ICON_SIZE - 2;
323 if (lpushed || rpushed) {
324 XSetForeground(dpy, scr->draw_gc, scr->white_pixel);
325 XFillPolygon(dpy, win, scr->draw_gc, p, 3, Convex, CoordModeOrigin);
326 XSetForeground(dpy, scr->draw_gc, scr->black_pixel);
329 /* top right arrow */
330 p[0].x = p[3].x = ICON_SIZE - 5 - as;
331 p[0].y = p[3].y = 5;
332 p[1].x = ICON_SIZE - 6;
333 p[1].y = 5;
334 p[2].x = ICON_SIZE - 6;
335 p[2].y = 4 + as;
336 if (rpushed) {
337 XFillPolygon(dpy, win, scr->draw_gc, p, 3, Convex, CoordModeOrigin);
338 XDrawLines(dpy, win, scr->draw_gc, p, 4, CoordModeOrigin);
339 } else {
340 XFillPolygon(dpy, win, gc, p, 3, Convex, CoordModeOrigin);
341 XDrawLines(dpy, win, gc, p, 4, CoordModeOrigin);
344 /* bottom left arrow */
345 p[0].x = p[3].x = 5;
346 p[0].y = p[3].y = ICON_SIZE - 5 - as;
347 p[1].x = 5;
348 p[1].y = ICON_SIZE - 6;
349 p[2].x = 4 + as;
350 p[2].y = ICON_SIZE - 6;
351 if (lpushed) {
352 XFillPolygon(dpy, win, scr->draw_gc, p, 3, Convex, CoordModeOrigin);
353 XDrawLines(dpy, win, scr->draw_gc, p, 4, CoordModeOrigin);
354 } else {
355 XFillPolygon(dpy, win, gc, p, 3, Convex, CoordModeOrigin);
356 XDrawLines(dpy, win, gc, p, 4, CoordModeOrigin);
360 RImage *wClipMakeTile(WScreen *scr, RImage *normalTile)
362 RImage *tile = RCloneImage(normalTile);
363 RColor black;
364 RColor dark;
365 RColor light;
366 int pt, tp;
367 int as;
369 pt = CLIP_BUTTON_SIZE * wPreferences.icon_size / 64;
370 tp = wPreferences.icon_size - 1 - pt;
371 as = pt - 15;
373 black.alpha = 255;
374 black.red = black.green = black.blue = 0;
376 dark.alpha = 0;
377 dark.red = dark.green = dark.blue = 60;
379 light.alpha = 0;
380 light.red = light.green = light.blue = 80;
382 /* top right */
383 ROperateLine(tile, RSubtractOperation, tp, 0, wPreferences.icon_size - 2, pt - 1, &dark);
384 RDrawLine(tile, tp - 1, 0, wPreferences.icon_size - 1, pt + 1, &black);
385 ROperateLine(tile, RAddOperation, tp, 2, wPreferences.icon_size - 3, pt, &light);
387 /* arrow bevel */
388 ROperateLine(tile, RSubtractOperation, ICON_SIZE - 7 - as, 4, ICON_SIZE - 5, 4, &dark);
389 ROperateLine(tile, RSubtractOperation, ICON_SIZE - 6 - as, 5, ICON_SIZE - 5, 6 + as, &dark);
390 ROperateLine(tile, RAddOperation, ICON_SIZE - 5, 4, ICON_SIZE - 5, 6 + as, &light);
392 /* bottom left */
393 ROperateLine(tile, RAddOperation, 2, tp + 2, pt - 2, wPreferences.icon_size - 3, &dark);
394 RDrawLine(tile, 0, tp - 1, pt + 1, wPreferences.icon_size - 1, &black);
395 ROperateLine(tile, RSubtractOperation, 0, tp - 2, pt + 1, wPreferences.icon_size - 2, &light);
397 /* arrow bevel */
398 ROperateLine(tile, RSubtractOperation, 4, ICON_SIZE - 7 - as, 4, ICON_SIZE - 5, &dark);
399 ROperateLine(tile, RSubtractOperation, 5, ICON_SIZE - 6 - as, 6 + as, ICON_SIZE - 5, &dark);
400 ROperateLine(tile, RAddOperation, 4, ICON_SIZE - 5, 6 + as, ICON_SIZE - 5, &light);
402 return tile;
405 static void omnipresentCallback(WMenu *menu, WMenuEntry *entry)
407 WAppIcon *clickedIcon = entry->clientdata;
408 WAppIcon *aicon;
409 WDock *dock;
410 WMArray *selectedIcons;
411 WMArrayIterator iter;
412 int failed;
414 assert(entry->clientdata != NULL);
416 dock = clickedIcon->dock;
418 selectedIcons = getSelected(dock);
420 if (!WMGetArrayItemCount(selectedIcons))
421 WMAddToArray(selectedIcons, clickedIcon);
423 failed = 0;
424 WM_ITERATE_ARRAY(selectedIcons, aicon, iter) {
425 if (wClipMakeIconOmnipresent(aicon, !aicon->omnipresent) == WO_FAILED)
426 failed++;
427 else if (aicon->icon->selected)
428 wIconSelect(aicon->icon);
430 WMFreeArray(selectedIcons);
432 if (failed > 1) {
433 wMessageDialog(dock->screen_ptr, _("Warning"),
434 _("Some icons cannot be made omnipresent. "
435 "Please make sure that no other icon is "
436 "docked in the same positions on the other "
437 "workspaces and the Clip is not full in "
438 "some workspace."), _("OK"), NULL, NULL);
439 } else if (failed == 1) {
440 wMessageDialog(dock->screen_ptr, _("Warning"),
441 _("Icon cannot be made omnipresent. "
442 "Please make sure that no other icon is "
443 "docked in the same position on the other "
444 "workspaces and the Clip is not full in "
445 "some workspace."), _("OK"), NULL, NULL);
449 static void removeIconsCallback(WMenu *menu, WMenuEntry *entry)
451 WAppIcon *clickedIcon = (WAppIcon *) entry->clientdata;
452 WDock *dock;
453 WAppIcon *aicon;
454 WMArray *selectedIcons;
455 int keepit;
456 WMArrayIterator it;
458 assert(clickedIcon != NULL);
460 dock = clickedIcon->dock;
462 selectedIcons = getSelected(dock);
464 if (WMGetArrayItemCount(selectedIcons)) {
465 if (wMessageDialog(dock->screen_ptr, _("Workspace Clip"),
466 _("All selected icons will be removed!"),
467 _("OK"), _("Cancel"), NULL) != WAPRDefault) {
468 WMFreeArray(selectedIcons);
469 return;
471 } else {
472 if (clickedIcon->xindex == 0 && clickedIcon->yindex == 0) {
473 WMFreeArray(selectedIcons);
474 return;
476 WMAddToArray(selectedIcons, clickedIcon);
479 WM_ITERATE_ARRAY(selectedIcons, aicon, it) {
480 keepit = aicon->running && wApplicationOf(aicon->main_window);
481 wDockDetach(dock, aicon);
482 if (keepit) {
483 /* XXX: can: aicon->icon == NULL ? */
484 PlaceIcon(dock->screen_ptr, &aicon->x_pos, &aicon->y_pos,
485 wGetHeadForWindow(aicon->icon->owner));
486 XMoveWindow(dpy, aicon->icon->core->window, aicon->x_pos, aicon->y_pos);
487 if (!dock->mapped || dock->collapsed)
488 XMapWindow(dpy, aicon->icon->core->window);
491 WMFreeArray(selectedIcons);
493 if (wPreferences.auto_arrange_icons)
494 wArrangeIcons(dock->screen_ptr, True);
497 static void keepIconsCallback(WMenu *menu, WMenuEntry *entry)
499 WAppIcon *clickedIcon = (WAppIcon *) entry->clientdata;
500 WDock *dock;
501 WAppIcon *aicon;
502 WMArray *selectedIcons;
503 WMArrayIterator it;
505 assert(clickedIcon != NULL);
506 dock = clickedIcon->dock;
508 selectedIcons = getSelected(dock);
510 if (!WMGetArrayItemCount(selectedIcons)
511 && clickedIcon != dock->screen_ptr->clip_icon) {
512 char *command = NULL;
514 if (!clickedIcon->command && !clickedIcon->editing) {
515 clickedIcon->editing = 1;
516 if (wInputDialog(dock->screen_ptr, _("Keep Icon"),
517 _("Type the command used to launch the application"), &command)) {
518 if (command && (command[0] == 0 || (command[0] == '-' && command[1] == 0))) {
519 wfree(command);
520 command = NULL;
522 clickedIcon->command = command;
523 clickedIcon->editing = 0;
524 } else {
525 clickedIcon->editing = 0;
526 if (command)
527 wfree(command);
528 WMFreeArray(selectedIcons);
529 return;
533 WMAddToArray(selectedIcons, clickedIcon);
536 WM_ITERATE_ARRAY(selectedIcons, aicon, it) {
537 if (aicon->icon->selected)
538 wIconSelect(aicon->icon);
540 if (aicon && aicon->attracted && aicon->command) {
541 aicon->attracted = 0;
542 if (aicon->icon->shadowed) {
543 aicon->icon->shadowed = 0;
545 /* Update the icon images */
546 wIconUpdate(aicon->icon, NULL);
548 /* Paint it */
549 wAppIconPaint(aicon);
552 save_appicon(aicon, True);
554 WMFreeArray(selectedIcons);
557 static void toggleAutoAttractCallback(WMenu *menu, WMenuEntry *entry)
559 WDock *dock = (WDock *) entry->clientdata;
561 assert(entry->clientdata != NULL);
563 dock->attract_icons = !dock->attract_icons;
565 entry->flags.indicator_on = dock->attract_icons;
567 wMenuPaint(menu);
570 static void selectCallback(WMenu *menu, WMenuEntry *entry)
572 WAppIcon *icon = (WAppIcon *) entry->clientdata;
574 assert(icon != NULL);
576 wIconSelect(icon->icon);
578 wMenuPaint(menu);
581 static void colectIconsCallback(WMenu *menu, WMenuEntry *entry)
583 WAppIcon *clickedIcon = (WAppIcon *) entry->clientdata;
584 WDock *clip;
585 WAppIcon *aicon;
586 int x, y, x_pos, y_pos;
587 Bool update_icon = False;
589 assert(entry->clientdata != NULL);
590 clip = clickedIcon->dock;
592 aicon = clip->screen_ptr->app_icon_list;
594 while (aicon) {
595 if (!aicon->docked && wDockFindFreeSlot(clip, &x, &y)) {
596 x_pos = clip->x_pos + x * ICON_SIZE;
597 y_pos = clip->y_pos + y * ICON_SIZE;
598 if (aicon->x_pos != x_pos || aicon->y_pos != y_pos)
599 move_window(aicon->icon->core->window, aicon->x_pos, aicon->y_pos, x_pos, y_pos);
601 aicon->attracted = 1;
602 if (!aicon->icon->shadowed) {
603 aicon->icon->shadowed = 1;
604 update_icon = True;
606 wDockAttachIcon(clip, aicon, x, y, update_icon);
607 if (clip->collapsed || !clip->mapped)
608 XUnmapWindow(dpy, aicon->icon->core->window);
610 aicon = aicon->next;
614 static void selectIconsCallback(WMenu *menu, WMenuEntry *entry)
616 WAppIcon *clickedIcon = (WAppIcon *) entry->clientdata;
617 WDock *dock;
618 WMArray *selectedIcons;
619 WMArrayIterator iter;
620 WAppIcon *btn;
621 int i;
623 assert(clickedIcon != NULL);
624 dock = clickedIcon->dock;
626 selectedIcons = getSelected(dock);
628 if (!WMGetArrayItemCount(selectedIcons)) {
629 for (i = 1; i < dock->max_icons; i++) {
630 btn = dock->icon_array[i];
631 if (btn && !btn->icon->selected)
632 wIconSelect(btn->icon);
634 } else {
635 WM_ITERATE_ARRAY(selectedIcons, btn, iter) {
636 wIconSelect(btn->icon);
639 WMFreeArray(selectedIcons);
641 wMenuPaint(menu);
644 static void toggleCollapsedCallback(WMenu *menu, WMenuEntry *entry)
646 assert(entry->clientdata != NULL);
648 toggleCollapsed(entry->clientdata);
650 entry->flags.indicator_on = ((WDock *) entry->clientdata)->collapsed;
652 wMenuPaint(menu);
655 static void toggleAutoCollapseCallback(WMenu *menu, WMenuEntry *entry)
657 WDock *dock;
658 assert(entry->clientdata != NULL);
660 dock = (WDock *) entry->clientdata;
662 dock->auto_collapse = !dock->auto_collapse;
664 entry->flags.indicator_on = ((WDock *) entry->clientdata)->auto_collapse;
666 wMenuPaint(menu);
669 static void toggleAutoRaiseLowerCallback(WMenu *menu, WMenuEntry *entry)
671 WDock *dock;
672 assert(entry->clientdata != NULL);
674 dock = (WDock *) entry->clientdata;
676 dock->auto_raise_lower = !dock->auto_raise_lower;
678 entry->flags.indicator_on = ((WDock *) entry->clientdata)->auto_raise_lower;
680 wMenuPaint(menu);
683 static void launchCallback(WMenu *menu, WMenuEntry *entry)
685 WAppIcon *btn = (WAppIcon *) entry->clientdata;
687 launchDockedApplication(btn, False);
690 static void settingsCallback(WMenu *menu, WMenuEntry *entry)
692 WAppIcon *btn = (WAppIcon *) entry->clientdata;
694 if (btn->editing)
695 return;
696 ShowDockAppSettingsPanel(btn);
699 static void hideCallback(WMenu *menu, WMenuEntry *entry)
701 WApplication *wapp;
702 WAppIcon *btn = (WAppIcon *) entry->clientdata;
704 wapp = wApplicationOf(btn->icon->owner->main_window);
706 if (wapp->flags.hidden) {
707 wWorkspaceChange(btn->icon->core->screen_ptr, wapp->last_workspace);
708 wUnhideApplication(wapp, False, False);
709 } else {
710 wHideApplication(wapp);
714 static void unhideHereCallback(WMenu *menu, WMenuEntry *entry)
716 WApplication *wapp;
717 WAppIcon *btn = (WAppIcon *) entry->clientdata;
719 wapp = wApplicationOf(btn->icon->owner->main_window);
721 wUnhideApplication(wapp, False, True);
724 static WAppIcon *mainIconCreate(WScreen *scr, int type)
726 WAppIcon *btn;
727 int x_pos;
729 if (type == WM_CLIP) {
730 if (scr->clip_icon)
731 return scr->clip_icon;
732 btn = wAppIconCreateForDock(scr, NULL, "Logo", "WMClip", TILE_CLIP);
733 btn->icon->core->descriptor.handle_expose = clipIconExpose;
734 btn->icon->core->descriptor.handle_enternotify = clipEnterNotify;
735 btn->icon->core->descriptor.handle_leavenotify = clipLeaveNotify;
736 x_pos = 0;
737 } else {
738 btn = wAppIconCreateForDock(scr, NULL, "Logo", "WMDock", TILE_NORMAL);
739 x_pos = scr->scr_width - ICON_SIZE - DOCK_EXTRA_SPACE;
742 btn->xindex = 0;
743 btn->yindex = 0;
745 btn->icon->core->descriptor.handle_mousedown = iconMouseDown;
746 btn->icon->core->descriptor.parent_type = WCLASS_DOCK_ICON;
747 btn->icon->core->descriptor.parent = btn;
748 XMapWindow(dpy, btn->icon->core->window);
749 btn->x_pos = x_pos;
750 btn->y_pos = 0;
751 btn->docked = 1;
752 if (type == WM_CLIP)
753 scr->clip_icon = btn;
755 return btn;
758 static void switchWSCommand(WMenu *menu, WMenuEntry *entry)
760 WAppIcon *btn, *icon = (WAppIcon *) entry->clientdata;
761 WScreen *scr = icon->icon->core->screen_ptr;
762 WDock *src, *dest;
763 WMArray *selectedIcons;
764 int x, y;
766 if (entry->order == scr->current_workspace)
767 return;
768 src = icon->dock;
769 dest = scr->workspaces[entry->order]->clip;
771 selectedIcons = getSelected(src);
773 if (WMGetArrayItemCount(selectedIcons)) {
774 WMArrayIterator iter;
776 WM_ITERATE_ARRAY(selectedIcons, btn, iter) {
777 if (wDockFindFreeSlot(dest, &x, &y)) {
778 moveIconBetweenDocks(src, dest, btn, x, y);
779 XUnmapWindow(dpy, btn->icon->core->window);
782 } else if (icon != scr->clip_icon) {
783 if (wDockFindFreeSlot(dest, &x, &y)) {
784 moveIconBetweenDocks(src, dest, icon, x, y);
785 XUnmapWindow(dpy, icon->icon->core->window);
788 WMFreeArray(selectedIcons);
791 static void launchDockedApplication(WAppIcon *btn, Bool withSelection)
793 WScreen *scr = btn->icon->core->screen_ptr;
795 if (!btn->launching &&
796 ((!withSelection && btn->command != NULL) || (withSelection && btn->paste_command != NULL))) {
797 if (!btn->forced_dock) {
798 btn->relaunching = btn->running;
799 btn->running = 1;
801 if (btn->wm_instance || btn->wm_class) {
802 WWindowAttributes attr;
803 memset(&attr, 0, sizeof(WWindowAttributes));
804 wDefaultFillAttributes(btn->wm_instance, btn->wm_class, &attr, NULL, True);
806 if (!attr.no_appicon && !btn->buggy_app)
807 btn->launching = 1;
808 else
809 btn->running = 0;
811 btn->drop_launch = 0;
812 btn->paste_launch = withSelection;
813 scr->last_dock = btn->dock;
814 btn->pid = execCommand(btn, (withSelection ? btn->paste_command : btn->command), NULL);
815 if (btn->pid > 0) {
816 if (btn->buggy_app) {
817 /* give feedback that the app was launched */
818 btn->launching = 1;
819 dockIconPaint(btn);
820 btn->launching = 0;
821 WMAddTimerHandler(200, (WMCallback *) dockIconPaint, btn);
822 } else {
823 dockIconPaint(btn);
825 } else {
826 wwarning(_("could not launch application %s"), btn->command);
827 btn->launching = 0;
828 if (!btn->relaunching)
829 btn->running = 0;
834 static void updateWorkspaceMenu(WMenu *menu, WAppIcon *icon)
836 WScreen *scr = menu->frame->screen_ptr;
837 char title[MAX_WORKSPACENAME_WIDTH + 1];
838 int i;
840 if (!menu || !icon)
841 return;
843 for (i = 0; i < scr->workspace_count; i++) {
844 if (i < menu->entry_no) {
845 if (strcmp(menu->entries[i]->text, scr->workspaces[i]->name) != 0) {
846 wfree(menu->entries[i]->text);
847 strcpy(title, scr->workspaces[i]->name);
848 menu->entries[i]->text = wstrdup(title);
849 menu->flags.realized = 0;
851 menu->entries[i]->clientdata = (void *)icon;
852 } else {
853 strcpy(title, scr->workspaces[i]->name);
855 wMenuAddCallback(menu, title, switchWSCommand, (void *)icon);
857 menu->flags.realized = 0;
860 if (i == scr->current_workspace)
861 wMenuSetEnabled(menu, i, False);
862 else
863 wMenuSetEnabled(menu, i, True);
866 if (!menu->flags.realized)
867 wMenuRealize(menu);
870 static WMenu *makeWorkspaceMenu(WScreen *scr)
872 WMenu *menu;
874 menu = wMenuCreate(scr, NULL, False);
875 if (!menu)
876 wwarning(_("could not create workspace submenu for Clip menu"));
878 wMenuAddCallback(menu, "", switchWSCommand, (void *)scr->clip_icon);
880 menu->flags.realized = 0;
881 wMenuRealize(menu);
883 return menu;
886 static void updateClipOptionsMenu(WMenu *menu, WDock *dock)
888 WMenuEntry *entry;
889 int index = 0;
891 if (!menu || !dock)
892 return;
894 /* keep on top */
895 entry = menu->entries[index];
896 entry->flags.indicator_on = !dock->lowered;
897 entry->clientdata = dock;
899 /* collapsed */
900 entry = menu->entries[++index];
901 entry->flags.indicator_on = dock->collapsed;
902 entry->clientdata = dock;
904 /* auto-collapse */
905 entry = menu->entries[++index];
906 entry->flags.indicator_on = dock->auto_collapse;
907 entry->clientdata = dock;
909 /* auto-raise/lower */
910 entry = menu->entries[++index];
911 entry->flags.indicator_on = dock->auto_raise_lower;
912 entry->clientdata = dock;
913 wMenuSetEnabled(menu, index, dock->lowered);
915 /* attract icons */
916 entry = menu->entries[++index];
917 entry->flags.indicator_on = dock->attract_icons;
918 entry->clientdata = dock;
920 menu->flags.realized = 0;
921 wMenuRealize(menu);
924 static WMenu *makeClipOptionsMenu(WScreen *scr)
926 WMenu *menu;
927 WMenuEntry *entry;
929 menu = wMenuCreate(scr, NULL, False);
930 if (!menu) {
931 wwarning(_("could not create options submenu for Clip menu"));
932 return NULL;
935 entry = wMenuAddCallback(menu, _("Keep on Top"), toggleLoweredCallback, NULL);
936 entry->flags.indicator = 1;
937 entry->flags.indicator_on = 1;
938 entry->flags.indicator_type = MI_CHECK;
940 entry = wMenuAddCallback(menu, _("Collapsed"), toggleCollapsedCallback, NULL);
941 entry->flags.indicator = 1;
942 entry->flags.indicator_on = 1;
943 entry->flags.indicator_type = MI_CHECK;
945 entry = wMenuAddCallback(menu, _("Autocollapse"), toggleAutoCollapseCallback, NULL);
946 entry->flags.indicator = 1;
947 entry->flags.indicator_on = 1;
948 entry->flags.indicator_type = MI_CHECK;
950 entry = wMenuAddCallback(menu, _("Autoraise"), toggleAutoRaiseLowerCallback, NULL);
951 entry->flags.indicator = 1;
952 entry->flags.indicator_on = 1;
953 entry->flags.indicator_type = MI_CHECK;
955 entry = wMenuAddCallback(menu, _("Autoattract Icons"), toggleAutoAttractCallback, NULL);
956 entry->flags.indicator = 1;
957 entry->flags.indicator_on = 1;
958 entry->flags.indicator_type = MI_CHECK;
960 menu->flags.realized = 0;
961 wMenuRealize(menu);
963 return menu;
966 static WMenu *dockMenuCreate(WScreen *scr, int type)
968 WMenu *menu;
969 WMenuEntry *entry;
971 if (type == WM_CLIP && scr->clip_menu)
972 return scr->clip_menu;
974 menu = wMenuCreate(scr, NULL, False);
975 if (type != WM_CLIP) {
976 entry = wMenuAddCallback(menu, _("Keep on Top"), toggleLoweredCallback, NULL);
977 entry->flags.indicator = 1;
978 entry->flags.indicator_on = 1;
979 entry->flags.indicator_type = MI_CHECK;
980 } else {
981 entry = wMenuAddCallback(menu, _("Clip Options"), NULL, NULL);
982 scr->clip_options = makeClipOptionsMenu(scr);
983 if (scr->clip_options)
984 wMenuEntrySetCascade(menu, entry, scr->clip_options);
986 entry = wMenuAddCallback(menu, _("Rename Workspace"), renameCallback, NULL);
987 wfree(entry->text);
988 entry->text = _("Rename Workspace");
990 entry = wMenuAddCallback(menu, _("Selected"), selectCallback, NULL);
991 entry->flags.indicator = 1;
992 entry->flags.indicator_on = 1;
993 entry->flags.indicator_type = MI_CHECK;
995 entry = wMenuAddCallback(menu, _("Select All Icons"), selectIconsCallback, NULL);
996 wfree(entry->text);
997 entry->text = _("Select All Icons");
999 entry = wMenuAddCallback(menu, _("Keep Icon"), keepIconsCallback, NULL);
1000 wfree(entry->text);
1001 entry->text = _("Keep Icon");
1003 entry = wMenuAddCallback(menu, _("Move Icon To"), NULL, NULL);
1004 wfree(entry->text);
1005 entry->text = _("Move Icon To");
1006 scr->clip_submenu = makeWorkspaceMenu(scr);
1007 if (scr->clip_submenu)
1008 wMenuEntrySetCascade(menu, entry, scr->clip_submenu);
1010 entry = wMenuAddCallback(menu, _("Remove Icon"), removeIconsCallback, NULL);
1011 wfree(entry->text);
1012 entry->text = _("Remove Icon");
1014 wMenuAddCallback(menu, _("Attract Icons"), colectIconsCallback, NULL);
1017 wMenuAddCallback(menu, _("Launch"), launchCallback, NULL);
1019 wMenuAddCallback(menu, _("Unhide Here"), unhideHereCallback, NULL);
1021 entry = wMenuAddCallback(menu, _("Hide"), hideCallback, NULL);
1022 wfree(entry->text);
1023 entry->text = _("Hide");
1025 wMenuAddCallback(menu, _("Settings..."), settingsCallback, NULL);
1027 wMenuAddCallback(menu, _("Kill"), killCallback, NULL);
1029 if (type == WM_CLIP)
1030 scr->clip_menu = menu;
1032 return menu;
1035 WDock *wDockCreate(WScreen *scr, int type)
1037 WDock *dock;
1038 WAppIcon *btn;
1040 make_keys();
1042 dock = wmalloc(sizeof(WDock));
1044 dock->max_icons = DOCK_MAX_ICONS;
1046 dock->icon_array = wmalloc(sizeof(WAppIcon *) * dock->max_icons);
1048 btn = mainIconCreate(scr, type);
1050 btn->dock = dock;
1052 dock->x_pos = btn->x_pos;
1053 dock->y_pos = btn->y_pos;
1054 dock->screen_ptr = scr;
1055 dock->type = type;
1056 dock->icon_count = 1;
1057 dock->on_right_side = 1;
1058 dock->collapsed = 0;
1059 dock->auto_collapse = 0;
1060 dock->auto_collapse_magic = NULL;
1061 dock->auto_raise_lower = 0;
1062 dock->auto_lower_magic = NULL;
1063 dock->auto_raise_magic = NULL;
1064 dock->attract_icons = 0;
1065 dock->lowered = 1;
1066 dock->icon_array[0] = btn;
1067 wRaiseFrame(btn->icon->core);
1068 XMoveWindow(dpy, btn->icon->core->window, btn->x_pos, btn->y_pos);
1070 /* create dock menu */
1071 dock->menu = dockMenuCreate(scr, type);
1073 return dock;
1076 void wDockDestroy(WDock *dock)
1078 int i;
1079 WAppIcon *aicon;
1081 for (i = (dock->type == WM_CLIP) ? 1 : 0; i < dock->max_icons; i++) {
1082 aicon = dock->icon_array[i];
1083 if (aicon) {
1084 int keepit = aicon->running && wApplicationOf(aicon->main_window);
1085 wDockDetach(dock, aicon);
1086 if (keepit) {
1087 /* XXX: can: aicon->icon == NULL ? */
1088 PlaceIcon(dock->screen_ptr, &aicon->x_pos, &aicon->y_pos,
1089 wGetHeadForWindow(aicon->icon->owner));
1090 XMoveWindow(dpy, aicon->icon->core->window, aicon->x_pos, aicon->y_pos);
1091 if (!dock->mapped || dock->collapsed)
1092 XMapWindow(dpy, aicon->icon->core->window);
1096 if (wPreferences.auto_arrange_icons)
1097 wArrangeIcons(dock->screen_ptr, True);
1098 wfree(dock->icon_array);
1099 if (dock->menu && dock->type != WM_CLIP)
1100 wMenuDestroy(dock->menu, True);
1101 if (dock->screen_ptr->last_dock == dock)
1102 dock->screen_ptr->last_dock = NULL;
1103 wfree(dock);
1106 void wClipIconPaint(WAppIcon *aicon)
1108 WScreen *scr = aicon->icon->core->screen_ptr;
1109 WWorkspace *workspace = scr->workspaces[scr->current_workspace];
1110 WMColor *color;
1111 Window win = aicon->icon->core->window;
1112 int length, nlength;
1113 char *ws_name, ws_number[10];
1114 int ty, tx;
1116 wIconPaint(aicon->icon);
1118 length = strlen(workspace->name);
1119 ws_name = wmalloc(length + 1);
1120 snprintf(ws_name, length + 1, "%s", workspace->name);
1121 snprintf(ws_number, sizeof(ws_number), "%i", scr->current_workspace + 1);
1122 nlength = strlen(ws_number);
1124 if (!workspace->clip->collapsed)
1125 color = scr->clip_title_color[CLIP_NORMAL];
1126 else
1127 color = scr->clip_title_color[CLIP_COLLAPSED];
1129 ty = ICON_SIZE - WMFontHeight(scr->clip_title_font) - 3;
1131 tx = CLIP_BUTTON_SIZE * ICON_SIZE / 64;
1133 if(wPreferences.show_clip_title)
1134 WMDrawString(scr->wmscreen, win, color, scr->clip_title_font, tx, ty, ws_name, length);
1136 tx = (ICON_SIZE / 2 - WMWidthOfString(scr->clip_title_font, ws_number, nlength)) / 2;
1138 WMDrawString(scr->wmscreen, win, color, scr->clip_title_font, tx, 2, ws_number, nlength);
1140 wfree(ws_name);
1142 if (aicon->launching)
1143 XFillRectangle(dpy, aicon->icon->core->window, scr->stipple_gc,
1144 0, 0, wPreferences.icon_size, wPreferences.icon_size);
1146 paintClipButtons(aicon, aicon->dock->lclip_button_pushed, aicon->dock->rclip_button_pushed);
1149 static void clipIconExpose(WObjDescriptor *desc, XEvent *event)
1151 wClipIconPaint(desc->parent);
1154 static void dockIconPaint(WAppIcon *btn)
1156 if (btn == btn->icon->core->screen_ptr->clip_icon) {
1157 wClipIconPaint(btn);
1158 } else {
1159 wAppIconPaint(btn);
1160 save_appicon(btn, True);
1164 static WMPropList *make_icon_state(WAppIcon *btn)
1166 WMPropList *node = NULL;
1167 WMPropList *command, *autolaunch, *lock, *name, *forced;
1168 WMPropList *position, *buggy, *omnipresent;
1169 char *tmp;
1170 char buffer[64];
1172 if (btn) {
1173 if (!btn->command)
1174 command = WMCreatePLString("-");
1175 else
1176 command = WMCreatePLString(btn->command);
1178 autolaunch = btn->auto_launch ? dYes : dNo;
1180 lock = btn->lock ? dYes : dNo;
1182 tmp = EscapeWM_CLASS(btn->wm_instance, btn->wm_class);
1184 name = WMCreatePLString(tmp);
1186 wfree(tmp);
1188 forced = btn->forced_dock ? dYes : dNo;
1190 buggy = btn->buggy_app ? dYes : dNo;
1192 if (btn == btn->icon->core->screen_ptr->clip_icon)
1193 snprintf(buffer, sizeof(buffer), "%i,%i", btn->x_pos, btn->y_pos);
1194 else
1195 snprintf(buffer, sizeof(buffer), "%hi,%hi", btn->xindex, btn->yindex);
1196 position = WMCreatePLString(buffer);
1198 node = WMCreatePLDictionary(dCommand, command,
1199 dName, name,
1200 dAutoLaunch, autolaunch,
1201 dLock, lock,
1202 dForced, forced, dBuggyApplication, buggy, dPosition, position, NULL);
1203 WMReleasePropList(command);
1204 WMReleasePropList(name);
1205 WMReleasePropList(position);
1207 omnipresent = btn->omnipresent ? dYes : dNo;
1208 if (btn->dock != btn->icon->core->screen_ptr->dock && (btn->xindex != 0 || btn->yindex != 0))
1209 WMPutInPLDictionary(node, dOmnipresent, omnipresent);
1211 #ifdef XDND /* was OFFIX */
1212 if (btn->dnd_command) {
1213 command = WMCreatePLString(btn->dnd_command);
1214 WMPutInPLDictionary(node, dDropCommand, command);
1215 WMReleasePropList(command);
1217 #endif /* XDND */
1219 if (btn->paste_command) {
1220 command = WMCreatePLString(btn->paste_command);
1221 WMPutInPLDictionary(node, dPasteCommand, command);
1222 WMReleasePropList(command);
1226 return node;
1229 static WMPropList *dockSaveState(WDock *dock)
1231 int i;
1232 WMPropList *icon_info;
1233 WMPropList *list = NULL, *dock_state = NULL;
1234 WMPropList *value, *key;
1235 char buffer[256];
1237 list = WMCreatePLArray(NULL);
1239 for (i = (dock->type == WM_DOCK ? 0 : 1); i < dock->max_icons; i++) {
1240 WAppIcon *btn = dock->icon_array[i];
1242 if (!btn || btn->attracted)
1243 continue;
1245 if ((icon_info = make_icon_state(dock->icon_array[i]))) {
1246 WMAddToPLArray(list, icon_info);
1247 WMReleasePropList(icon_info);
1251 dock_state = WMCreatePLDictionary(dApplications, list, NULL);
1253 if (dock->type == WM_DOCK) {
1254 snprintf(buffer, sizeof(buffer), "Applications%i", dock->screen_ptr->scr_height);
1255 key = WMCreatePLString(buffer);
1256 WMPutInPLDictionary(dock_state, key, list);
1257 WMReleasePropList(key);
1259 snprintf(buffer, sizeof(buffer), "%i,%i", (dock->on_right_side ? -ICON_SIZE : 0), dock->y_pos);
1260 value = WMCreatePLString(buffer);
1261 WMPutInPLDictionary(dock_state, dPosition, value);
1262 WMReleasePropList(value);
1264 WMReleasePropList(list);
1266 value = (dock->lowered ? dYes : dNo);
1267 WMPutInPLDictionary(dock_state, dLowered, value);
1269 if (dock->type == WM_CLIP) {
1270 value = (dock->collapsed ? dYes : dNo);
1271 WMPutInPLDictionary(dock_state, dCollapsed, value);
1273 value = (dock->auto_collapse ? dYes : dNo);
1274 WMPutInPLDictionary(dock_state, dAutoCollapse, value);
1276 value = (dock->auto_raise_lower ? dYes : dNo);
1277 WMPutInPLDictionary(dock_state, dAutoRaiseLower, value);
1279 value = (dock->attract_icons ? dYes : dNo);
1280 WMPutInPLDictionary(dock_state, dAutoAttractIcons, value);
1283 return dock_state;
1286 void wDockSaveState(WScreen *scr, WMPropList *old_state)
1288 WMPropList *dock_state;
1289 WMPropList *keys;
1291 dock_state = dockSaveState(scr->dock);
1294 * Copy saved states of docks with different sizes.
1296 if (old_state) {
1297 int i;
1298 WMPropList *tmp;
1300 keys = WMGetPLDictionaryKeys(old_state);
1301 for (i = 0; i < WMGetPropListItemCount(keys); i++) {
1302 tmp = WMGetFromPLArray(keys, i);
1304 if (strncasecmp(WMGetFromPLString(tmp), "applications", 12) == 0
1305 && !WMGetFromPLDictionary(dock_state, tmp)) {
1307 WMPutInPLDictionary(dock_state, tmp, WMGetFromPLDictionary(old_state, tmp));
1310 WMReleasePropList(keys);
1313 WMPutInPLDictionary(scr->session_state, dDock, dock_state);
1315 WMReleasePropList(dock_state);
1318 void wClipSaveState(WScreen *scr)
1320 WMPropList *clip_state;
1322 clip_state = make_icon_state(scr->clip_icon);
1324 WMPutInPLDictionary(scr->session_state, dClip, clip_state);
1326 WMReleasePropList(clip_state);
1329 WMPropList *wClipSaveWorkspaceState(WScreen *scr, int workspace)
1331 return dockSaveState(scr->workspaces[workspace]->clip);
1334 static Bool getBooleanDockValue(WMPropList *value, WMPropList *key)
1336 if (value) {
1337 if (WMIsPLString(value)) {
1338 if (strcasecmp(WMGetFromPLString(value), "YES") == 0)
1339 return True;
1340 } else {
1341 wwarning(_("bad value in docked icon state info %s"), WMGetFromPLString(key));
1344 return False;
1347 static WAppIcon *restore_icon_state(WScreen *scr, WMPropList *info, int type, int index)
1349 WAppIcon *aicon;
1350 WMPropList *cmd, *value;
1351 char *wclass, *winstance, *command;
1353 cmd = WMGetFromPLDictionary(info, dCommand);
1354 if (!cmd || !WMIsPLString(cmd))
1355 return NULL;
1357 /* parse window name */
1358 value = WMGetFromPLDictionary(info, dName);
1359 if (!value)
1360 return NULL;
1362 ParseWindowName(value, &winstance, &wclass, "dock");
1364 if (!winstance && !wclass)
1365 return NULL;
1367 /* get commands */
1368 if (cmd)
1369 command = wstrdup(WMGetFromPLString(cmd));
1370 else
1371 command = NULL;
1373 if (!command || strcmp(command, "-") == 0) {
1374 if (command)
1375 wfree(command);
1376 if (wclass)
1377 wfree(wclass);
1378 if (winstance)
1379 wfree(winstance);
1381 return NULL;
1384 aicon = wAppIconCreateForDock(scr, command, winstance, wclass, TILE_NORMAL);
1385 if (wclass)
1386 wfree(wclass);
1387 if (winstance)
1388 wfree(winstance);
1389 if (command)
1390 wfree(command);
1392 aicon->icon->core->descriptor.handle_mousedown = iconMouseDown;
1393 if (type == WM_CLIP) {
1394 aicon->icon->core->descriptor.handle_enternotify = clipEnterNotify;
1395 aicon->icon->core->descriptor.handle_leavenotify = clipLeaveNotify;
1397 aicon->icon->core->descriptor.parent_type = WCLASS_DOCK_ICON;
1398 aicon->icon->core->descriptor.parent = aicon;
1400 #ifdef XDND /* was OFFIX */
1401 cmd = WMGetFromPLDictionary(info, dDropCommand);
1402 if (cmd)
1403 aicon->dnd_command = wstrdup(WMGetFromPLString(cmd));
1404 #endif
1406 cmd = WMGetFromPLDictionary(info, dPasteCommand);
1407 if (cmd)
1408 aicon->paste_command = wstrdup(WMGetFromPLString(cmd));
1410 /* check auto launch */
1411 value = WMGetFromPLDictionary(info, dAutoLaunch);
1413 aicon->auto_launch = getBooleanDockValue(value, dAutoLaunch);
1415 /* check lock */
1416 value = WMGetFromPLDictionary(info, dLock);
1418 aicon->lock = getBooleanDockValue(value, dLock);
1420 /* check if it wasn't normally docked */
1421 value = WMGetFromPLDictionary(info, dForced);
1423 aicon->forced_dock = getBooleanDockValue(value, dForced);
1425 /* check if we can rely on the stuff in the app */
1426 value = WMGetFromPLDictionary(info, dBuggyApplication);
1428 aicon->buggy_app = getBooleanDockValue(value, dBuggyApplication);
1430 /* get position in the dock */
1431 value = WMGetFromPLDictionary(info, dPosition);
1432 if (value && WMIsPLString(value)) {
1433 if (sscanf(WMGetFromPLString(value), "%hi,%hi", &aicon->xindex, &aicon->yindex) != 2)
1434 wwarning(_("bad value in docked icon state info %s"), WMGetFromPLString(dPosition));
1436 /* check position sanity */
1437 /* incomplete section! */
1438 if (type == WM_DOCK) {
1439 aicon->xindex = 0;
1440 if (aicon->yindex < 0)
1441 wwarning(_("bad value in docked icon position %i,%i"),
1442 aicon->xindex, aicon->yindex);
1444 } else {
1445 aicon->yindex = index;
1446 aicon->xindex = 0;
1449 /* check if icon is omnipresent */
1450 value = WMGetFromPLDictionary(info, dOmnipresent);
1452 aicon->omnipresent = getBooleanDockValue(value, dOmnipresent);
1454 aicon->running = 0;
1455 aicon->docked = 1;
1457 return aicon;
1460 #define COMPLAIN(key) wwarning(_("bad value in dock state info:%s"), key)
1462 WAppIcon *wClipRestoreState(WScreen *scr, WMPropList *clip_state)
1464 WAppIcon *icon;
1465 WMPropList *value;
1467 icon = mainIconCreate(scr, WM_CLIP);
1469 if (!clip_state)
1470 return icon;
1472 WMRetainPropList(clip_state);
1474 /* restore position */
1476 value = WMGetFromPLDictionary(clip_state, dPosition);
1478 if (value) {
1479 if (!WMIsPLString(value)) {
1480 COMPLAIN("Position");
1481 } else {
1482 if (sscanf(WMGetFromPLString(value), "%i,%i", &icon->x_pos, &icon->y_pos) != 2)
1483 COMPLAIN("Position");
1485 /* check position sanity */
1486 if (!onScreen(scr, icon->x_pos, icon->y_pos))
1487 wScreenKeepInside(scr, &icon->x_pos, &icon->y_pos, ICON_SIZE, ICON_SIZE);
1490 #ifdef XDND /* was OFFIX */
1491 value = WMGetFromPLDictionary(clip_state, dDropCommand);
1492 if (value && WMIsPLString(value))
1493 icon->dnd_command = wstrdup(WMGetFromPLString(value));
1494 #endif
1496 value = WMGetFromPLDictionary(clip_state, dPasteCommand);
1497 if (value && WMIsPLString(value))
1498 icon->paste_command = wstrdup(WMGetFromPLString(value));
1500 WMReleasePropList(clip_state);
1502 return icon;
1505 WDock *wDockRestoreState(WScreen *scr, WMPropList *dock_state, int type)
1507 WDock *dock;
1508 WMPropList *apps;
1509 WMPropList *value;
1510 WAppIcon *aicon, *old_top;
1511 int count, i;
1513 dock = wDockCreate(scr, type);
1515 if (!dock_state)
1516 return dock;
1518 WMRetainPropList(dock_state);
1520 /* restore position */
1521 value = WMGetFromPLDictionary(dock_state, dPosition);
1522 if (value) {
1523 if (!WMIsPLString(value)) {
1524 COMPLAIN("Position");
1525 } else {
1526 if (sscanf(WMGetFromPLString(value), "%i,%i", &dock->x_pos, &dock->y_pos) != 2)
1527 COMPLAIN("Position");
1529 /* check position sanity */
1530 if (!onScreen(scr, dock->x_pos, dock->y_pos)) {
1531 int x = dock->x_pos;
1532 wScreenKeepInside(scr, &x, &dock->y_pos, ICON_SIZE, ICON_SIZE);
1535 /* Is this needed any more? */
1536 if (type == WM_CLIP) {
1537 if (dock->x_pos < 0) {
1538 dock->x_pos = 0;
1539 } else if (dock->x_pos > scr->scr_width - ICON_SIZE) {
1540 dock->x_pos = scr->scr_width - ICON_SIZE;
1542 } else {
1543 if (dock->x_pos >= 0) {
1544 dock->x_pos = DOCK_EXTRA_SPACE;
1545 dock->on_right_side = 0;
1546 } else {
1547 dock->x_pos = scr->scr_width - DOCK_EXTRA_SPACE - ICON_SIZE;
1548 dock->on_right_side = 1;
1554 /* restore lowered/raised state */
1555 dock->lowered = 0;
1557 value = WMGetFromPLDictionary(dock_state, dLowered);
1558 if (value) {
1559 if (!WMIsPLString(value)) {
1560 COMPLAIN("Lowered");
1561 } else {
1562 if (strcasecmp(WMGetFromPLString(value), "YES") == 0)
1563 dock->lowered = 1;
1567 /* restore collapsed state */
1568 dock->collapsed = 0;
1570 value = WMGetFromPLDictionary(dock_state, dCollapsed);
1571 if (value) {
1572 if (!WMIsPLString(value)) {
1573 COMPLAIN("Collapsed");
1574 } else {
1575 if (strcasecmp(WMGetFromPLString(value), "YES") == 0)
1576 dock->collapsed = 1;
1580 /* restore auto-collapsed state */
1581 value = WMGetFromPLDictionary(dock_state, dAutoCollapse);
1582 if (value) {
1583 if (!WMIsPLString(value)) {
1584 COMPLAIN("AutoCollapse");
1585 } else {
1586 if (strcasecmp(WMGetFromPLString(value), "YES") == 0) {
1587 dock->auto_collapse = 1;
1588 dock->collapsed = 1;
1593 /* restore auto-raise/lower state */
1594 value = WMGetFromPLDictionary(dock_state, dAutoRaiseLower);
1595 if (value) {
1596 if (!WMIsPLString(value)) {
1597 COMPLAIN("AutoRaiseLower");
1598 } else {
1599 if (strcasecmp(WMGetFromPLString(value), "YES") == 0)
1600 dock->auto_raise_lower = 1;
1604 /* restore attract icons state */
1605 dock->attract_icons = 0;
1607 value = WMGetFromPLDictionary(dock_state, dAutoAttractIcons);
1608 if (value) {
1609 if (!WMIsPLString(value)) {
1610 COMPLAIN("AutoAttractIcons");
1611 } else {
1612 if (strcasecmp(WMGetFromPLString(value), "YES") == 0)
1613 dock->attract_icons = 1;
1617 /* application list */
1620 WMPropList *tmp;
1621 char buffer[64];
1624 * When saving, it saves the dock state in
1625 * Applications and Applicationsnnn
1627 * When loading, it will first try Applicationsnnn.
1628 * If it does not exist, use Applications as default.
1631 snprintf(buffer, sizeof(buffer), "Applications%i", scr->scr_height);
1633 tmp = WMCreatePLString(buffer);
1634 apps = WMGetFromPLDictionary(dock_state, tmp);
1635 WMReleasePropList(tmp);
1637 if (!apps)
1638 apps = WMGetFromPLDictionary(dock_state, dApplications);
1641 if (!apps)
1642 goto finish;
1644 count = WMGetPropListItemCount(apps);
1645 if (count == 0)
1646 goto finish;
1648 old_top = dock->icon_array[0];
1650 /* dock->icon_count is set to 1 when dock is created.
1651 * Since Clip is already restored, we want to keep it so for clip,
1652 * but for dock we may change the default top tile, so we set it to 0.
1654 if (type == WM_DOCK)
1655 dock->icon_count = 0;
1657 for (i = 0; i < count; i++) {
1658 if (dock->icon_count >= dock->max_icons) {
1659 wwarning(_("there are too many icons stored in dock. Ignoring what doesn't fit"));
1660 break;
1663 value = WMGetFromPLArray(apps, i);
1664 aicon = restore_icon_state(scr, value, type, dock->icon_count);
1666 dock->icon_array[dock->icon_count] = aicon;
1668 if (aicon) {
1669 aicon->dock = dock;
1670 aicon->x_pos = dock->x_pos + (aicon->xindex * ICON_SIZE);
1671 aicon->y_pos = dock->y_pos + (aicon->yindex * ICON_SIZE);
1673 if (dock->lowered)
1674 ChangeStackingLevel(aicon->icon->core, WMNormalLevel);
1675 else
1676 ChangeStackingLevel(aicon->icon->core, WMDockLevel);
1678 wCoreConfigure(aicon->icon->core, aicon->x_pos, aicon->y_pos, 0, 0);
1679 if (!dock->collapsed)
1680 XMapWindow(dpy, aicon->icon->core->window);
1682 wRaiseFrame(aicon->icon->core);
1684 dock->icon_count++;
1685 } else if (dock->icon_count == 0 && type == WM_DOCK) {
1686 dock->icon_count++;
1690 /* if the first icon is not defined, use the default */
1691 if (dock->icon_array[0] == NULL) {
1692 /* update default icon */
1693 old_top->x_pos = dock->x_pos;
1694 old_top->y_pos = dock->y_pos;
1695 if (dock->lowered)
1696 ChangeStackingLevel(old_top->icon->core, WMNormalLevel);
1697 else
1698 ChangeStackingLevel(old_top->icon->core, WMDockLevel);
1700 dock->icon_array[0] = old_top;
1701 XMoveWindow(dpy, old_top->icon->core->window, dock->x_pos, dock->y_pos);
1702 /* we don't need to increment dock->icon_count here because it was
1703 * incremented in the loop above.
1705 } else if (old_top != dock->icon_array[0]) {
1706 if (old_top == scr->clip_icon)
1707 scr->clip_icon = dock->icon_array[0];
1709 wAppIconDestroy(old_top);
1712 finish:
1713 WMReleasePropList(dock_state);
1715 return dock;
1718 void wDockLaunchWithState(WDock *dock, WAppIcon *btn, WSavedState *state)
1720 if (btn && btn->command && !btn->running && !btn->launching) {
1721 btn->drop_launch = 0;
1722 btn->paste_launch = 0;
1724 btn->pid = execCommand(btn, btn->command, state);
1726 if (btn->pid > 0) {
1727 if (!btn->forced_dock && !btn->buggy_app) {
1728 btn->launching = 1;
1729 dockIconPaint(btn);
1732 } else {
1733 wfree(state);
1737 void wDockDoAutoLaunch(WDock *dock, int workspace)
1739 WAppIcon *btn;
1740 WSavedState *state;
1741 int i;
1743 for (i = 0; i < dock->max_icons; i++) {
1744 btn = dock->icon_array[i];
1745 if (!btn || !btn->auto_launch)
1746 continue;
1748 state = wmalloc(sizeof(WSavedState));
1749 state->workspace = workspace;
1750 /* TODO: this is klugy and is very difficult to understand
1751 * what's going on. Try to clean up */
1752 wDockLaunchWithState(dock, btn, state);
1756 #ifdef XDND /* was OFFIX */
1757 static WDock *findDock(WScreen *scr, XEvent *event, int *icon_pos)
1759 WDock *dock;
1760 int i;
1762 *icon_pos = -1;
1763 if ((dock = scr->dock) != NULL) {
1764 for (i = 0; i < dock->max_icons; i++) {
1765 if (dock->icon_array[i]
1766 && dock->icon_array[i]->icon->core->window == event->xclient.window) {
1767 *icon_pos = i;
1768 break;
1772 if (*icon_pos < 0 && (dock = scr->workspaces[scr->current_workspace]->clip) != NULL) {
1773 for (i = 0; i < dock->max_icons; i++) {
1774 if (dock->icon_array[i]
1775 && dock->icon_array[i]->icon->core->window == event->xclient.window) {
1776 *icon_pos = i;
1777 break;
1781 if (*icon_pos >= 0)
1782 return dock;
1783 return NULL;
1786 int wDockReceiveDNDDrop(WScreen *scr, XEvent *event)
1788 WDock *dock;
1789 WAppIcon *btn;
1790 int icon_pos;
1792 dock = findDock(scr, event, &icon_pos);
1793 if (!dock)
1794 return False;
1797 * Return True if the drop was on an application icon window.
1798 * In this case, let the ClientMessage handler redirect the
1799 * message to the app.
1801 if (dock->icon_array[icon_pos]->icon->icon_win != None)
1802 return True;
1804 if (dock->icon_array[icon_pos]->dnd_command != NULL) {
1805 scr->flags.dnd_data_convertion_status = 0;
1807 btn = dock->icon_array[icon_pos];
1809 if (!btn->forced_dock) {
1810 btn->relaunching = btn->running;
1811 btn->running = 1;
1813 if (btn->wm_instance || btn->wm_class) {
1814 WWindowAttributes attr;
1815 memset(&attr, 0, sizeof(WWindowAttributes));
1816 wDefaultFillAttributes(btn->wm_instance, btn->wm_class, &attr, NULL, True);
1818 if (!attr.no_appicon)
1819 btn->launching = 1;
1820 else
1821 btn->running = 0;
1824 btn->paste_launch = 0;
1825 btn->drop_launch = 1;
1826 scr->last_dock = dock;
1827 btn->pid = execCommand(btn, btn->dnd_command, NULL);
1828 if (btn->pid > 0) {
1829 dockIconPaint(btn);
1830 } else {
1831 btn->launching = 0;
1832 if (!btn->relaunching)
1833 btn->running = 0;
1836 return False;
1838 #endif /* XDND */
1840 Bool wDockAttachIcon(WDock *dock, WAppIcon *icon, int x, int y, Bool update_icon)
1842 WWindow *wwin;
1843 Bool lupdate_icon = False;
1844 char *command = NULL;
1845 int index;
1847 icon->editing = 0;
1849 if (update_icon)
1850 lupdate_icon = True;
1852 if (icon->command == NULL) {
1853 /* If icon->owner exists, it means the application is running */
1854 if (icon->icon->owner) {
1855 wwin = icon->icon->owner;
1856 command = GetCommandForWindow(wwin->client_win);
1859 if (command) {
1860 icon->command = command;
1861 } else {
1862 /* icon->forced_dock = 1; */
1863 if (dock->type != WM_CLIP || !icon->attracted) {
1864 icon->editing = 1;
1865 if (wInputDialog(dock->screen_ptr, _("Dock Icon"),
1866 _("Type the command used to launch the application"), &command)) {
1867 if (command && (command[0] == 0 || (command[0] == '-' && command[1] == 0))) {
1868 wfree(command);
1869 command = NULL;
1871 icon->command = command;
1872 icon->editing = 0;
1873 } else {
1874 icon->editing = 0;
1875 if (command)
1876 wfree(command);
1877 /* If the target is the dock, reject the icon. If
1878 * the target is the clip, make it an attracted icon
1880 if (dock->type == WM_CLIP) {
1881 icon->attracted = 1;
1882 if (!icon->icon->shadowed) {
1883 icon->icon->shadowed = 1;
1884 lupdate_icon = True;
1886 } else {
1887 return False;
1894 for (index = 1; index < dock->max_icons; index++)
1895 if (dock->icon_array[index] == NULL)
1896 break;
1897 /* if (index == dock->max_icons)
1898 return; */
1900 assert(index < dock->max_icons);
1902 dock->icon_array[index] = icon;
1903 icon->yindex = y;
1904 icon->xindex = x;
1906 icon->omnipresent = 0;
1908 icon->x_pos = dock->x_pos + x * ICON_SIZE;
1909 icon->y_pos = dock->y_pos + y * ICON_SIZE;
1911 dock->icon_count++;
1913 icon->running = 1;
1914 icon->launching = 0;
1915 icon->docked = 1;
1916 icon->dock = dock;
1917 icon->icon->core->descriptor.handle_mousedown = iconMouseDown;
1918 if (dock->type == WM_CLIP) {
1919 icon->icon->core->descriptor.handle_enternotify = clipEnterNotify;
1920 icon->icon->core->descriptor.handle_leavenotify = clipLeaveNotify;
1922 icon->icon->core->descriptor.parent_type = WCLASS_DOCK_ICON;
1923 icon->icon->core->descriptor.parent = icon;
1925 MoveInStackListUnder(dock->icon_array[index - 1]->icon->core, icon->icon->core);
1926 wAppIconMove(icon, icon->x_pos, icon->y_pos);
1928 /* Update the icon images */
1929 if (lupdate_icon)
1930 wIconUpdate(icon->icon, NULL);
1932 /* Paint it */
1933 wAppIconPaint(icon);
1935 /* Save it */
1936 save_appicon(icon, True);
1938 if (wPreferences.auto_arrange_icons)
1939 wArrangeIcons(dock->screen_ptr, True);
1941 #ifdef XDND /* was OFFIX */
1942 if (icon->command && !icon->dnd_command) {
1943 int len = strlen(icon->command) + 8;
1944 icon->dnd_command = wmalloc(len);
1945 snprintf(icon->dnd_command, len, "%s %%d", icon->command);
1947 #endif
1949 if (icon->command && !icon->paste_command) {
1950 int len = strlen(icon->command) + 8;
1951 icon->paste_command = wmalloc(len);
1952 snprintf(icon->paste_command, len, "%s %%s", icon->command);
1955 return True;
1958 static void reattachIcon(WDock *dock, WAppIcon *icon, int x, int y)
1960 int index;
1962 for (index = 1; index < dock->max_icons; index++) {
1963 if (dock->icon_array[index] == icon)
1964 break;
1966 assert(index < dock->max_icons);
1968 icon->yindex = y;
1969 icon->xindex = x;
1971 icon->x_pos = dock->x_pos + x * ICON_SIZE;
1972 icon->y_pos = dock->y_pos + y * ICON_SIZE;
1975 static Bool moveIconBetweenDocks(WDock *src, WDock *dest, WAppIcon *icon, int x, int y)
1977 WWindow *wwin;
1978 char *command = NULL;
1979 int index;
1980 Bool update_icon = False;
1982 if (src == dest)
1983 return True; /* No move needed, we're already there */
1985 if (dest == NULL)
1986 return False;
1989 * For the moment we can't do this if we move icons in Clip from one
1990 * workspace to other, because if we move two or more icons without
1991 * command, the dialog box will not be able to tell us to which of the
1992 * moved icons it applies. -Dan
1994 if ((dest->type == WM_DOCK /*|| dest->keep_attracted */ ) && icon->command == NULL) {
1995 /* If icon->owner exists, it means the application is running */
1996 if (icon->icon->owner) {
1997 wwin = icon->icon->owner;
1998 command = GetCommandForWindow(wwin->client_win);
2001 if (command) {
2002 icon->command = command;
2003 } else {
2004 icon->editing = 1;
2005 /* icon->forced_dock = 1; */
2006 if (wInputDialog(src->screen_ptr, _("Dock Icon"),
2007 _("Type the command used to launch the application"), &command)) {
2008 if (command && (command[0] == 0 || (command[0] == '-' && command[1] == 0))) {
2009 wfree(command);
2010 command = NULL;
2012 icon->command = command;
2013 } else {
2014 icon->editing = 0;
2015 if (command)
2016 wfree(command);
2017 return False;
2019 icon->editing = 0;
2023 if (dest->type == WM_DOCK)
2024 wClipMakeIconOmnipresent(icon, False);
2026 for (index = 1; index < src->max_icons; index++) {
2027 if (src->icon_array[index] == icon)
2028 break;
2030 assert(index < src->max_icons);
2032 src->icon_array[index] = NULL;
2033 src->icon_count--;
2035 for (index = 1; index < dest->max_icons; index++) {
2036 if (dest->icon_array[index] == NULL)
2037 break;
2040 assert(index < dest->max_icons);
2042 dest->icon_array[index] = icon;
2043 icon->dock = dest;
2045 /* deselect the icon */
2046 if (icon->icon->selected)
2047 wIconSelect(icon->icon);
2049 if (dest->type == WM_DOCK) {
2050 icon->icon->core->descriptor.handle_enternotify = NULL;
2051 icon->icon->core->descriptor.handle_leavenotify = NULL;
2052 } else {
2053 icon->icon->core->descriptor.handle_enternotify = clipEnterNotify;
2054 icon->icon->core->descriptor.handle_leavenotify = clipLeaveNotify;
2057 /* set it to be kept when moving to dock.
2058 * Unless the icon does not have a command set
2060 if (icon->command && dest->type == WM_DOCK) {
2061 icon->attracted = 0;
2062 if (icon->icon->shadowed) {
2063 icon->icon->shadowed = 0;
2064 update_icon = True;
2066 save_appicon(icon, True);
2069 if (src->auto_collapse || src->auto_raise_lower)
2070 clipLeave(src);
2072 icon->yindex = y;
2073 icon->xindex = x;
2075 icon->x_pos = dest->x_pos + x * ICON_SIZE;
2076 icon->y_pos = dest->y_pos + y * ICON_SIZE;
2078 dest->icon_count++;
2080 MoveInStackListUnder(dest->icon_array[index - 1]->icon->core, icon->icon->core);
2082 /* Update the icon images */
2083 if (update_icon)
2084 wIconUpdate(icon->icon, NULL);
2086 /* Paint it */
2087 wAppIconPaint(icon);
2089 return True;
2092 void wDockDetach(WDock *dock, WAppIcon *icon)
2094 int index;
2095 Bool update_icon = False;
2097 /* make the settings panel be closed */
2098 if (icon->panel)
2099 DestroyDockAppSettingsPanel(icon->panel);
2101 /* This must be called before icon->dock is set to NULL.
2102 * Don't move it. -Dan
2104 wClipMakeIconOmnipresent(icon, False);
2106 icon->docked = 0;
2107 icon->dock = NULL;
2108 icon->attracted = 0;
2109 icon->auto_launch = 0;
2110 if (icon->icon->shadowed) {
2111 icon->icon->shadowed = 0;
2112 update_icon = True;
2115 /* deselect the icon */
2116 if (icon->icon->selected)
2117 wIconSelect(icon->icon);
2119 if (icon->command) {
2120 wfree(icon->command);
2121 icon->command = NULL;
2123 #ifdef XDND /* was OFFIX */
2124 if (icon->dnd_command) {
2125 wfree(icon->dnd_command);
2126 icon->dnd_command = NULL;
2128 #endif
2129 if (icon->paste_command) {
2130 wfree(icon->paste_command);
2131 icon->paste_command = NULL;
2134 for (index = 1; index < dock->max_icons; index++)
2135 if (dock->icon_array[index] == icon)
2136 break;
2138 assert(index < dock->max_icons);
2139 dock->icon_array[index] = NULL;
2140 icon->yindex = -1;
2141 icon->xindex = -1;
2143 dock->icon_count--;
2145 /* if the dock is not attached to an application or
2146 * the application did not set the appropriate hints yet,
2147 * destroy the icon */
2148 if (!icon->running || !wApplicationOf(icon->main_window)) {
2149 wAppIconDestroy(icon);
2150 } else {
2151 icon->icon->core->descriptor.handle_mousedown = appIconMouseDown;
2152 icon->icon->core->descriptor.handle_enternotify = NULL;
2153 icon->icon->core->descriptor.handle_leavenotify = NULL;
2154 icon->icon->core->descriptor.parent_type = WCLASS_APPICON;
2155 icon->icon->core->descriptor.parent = icon;
2157 ChangeStackingLevel(icon->icon->core, NORMAL_ICON_LEVEL);
2159 /* Update the icon images */
2160 if (update_icon)
2161 wIconUpdate(icon->icon, NULL);
2163 /* Paint it */
2164 wAppIconPaint(icon);
2166 if (wPreferences.auto_arrange_icons)
2167 wArrangeIcons(dock->screen_ptr, True);
2169 if (dock->auto_collapse || dock->auto_raise_lower)
2170 clipLeave(dock);
2174 * returns the closest Dock slot index for the passed
2175 * coordinates.
2177 * Returns False if icon can't be docked.
2179 * Note: this function should NEVER alter ret_x or ret_y, unless it will
2180 * return True. -Dan
2182 Bool wDockSnapIcon(WDock *dock, WAppIcon *icon, int req_x, int req_y, int *ret_x, int *ret_y, int redocking)
2184 WScreen *scr = dock->screen_ptr;
2185 int dx, dy;
2186 int ex_x, ex_y;
2187 int i, offset = ICON_SIZE / 2;
2188 WAppIcon *aicon = NULL;
2189 WAppIcon *nicon = NULL;
2190 int max_y_icons;
2192 /* TODO: XINERAMA, for these */
2193 max_y_icons = scr->scr_height / ICON_SIZE - 1;
2195 if (wPreferences.flags.noupdates)
2196 return False;
2198 dx = dock->x_pos;
2199 dy = dock->y_pos;
2201 /* if the dock is full */
2202 if (!redocking && (dock->icon_count >= dock->max_icons))
2203 return False;
2205 /* exact position */
2206 if (req_y < dy)
2207 ex_y = (req_y - offset - dy) / ICON_SIZE;
2208 else
2209 ex_y = (req_y + offset - dy) / ICON_SIZE;
2211 if (req_x < dx)
2212 ex_x = (req_x - offset - dx) / ICON_SIZE;
2213 else
2214 ex_x = (req_x + offset - dx) / ICON_SIZE;
2216 /* check if the icon is outside the screen boundaries */
2217 if (!onScreen(scr, dx + ex_x * ICON_SIZE, dy + ex_y * ICON_SIZE))
2218 return False;
2220 if (dock->type == WM_DOCK) {
2221 if (icon->dock != dock && ex_x != 0)
2222 return False;
2224 aicon = NULL;
2225 for (i = 0; i < dock->max_icons; i++) {
2226 nicon = dock->icon_array[i];
2227 if (nicon && nicon->yindex == ex_y) {
2228 aicon = nicon;
2229 break;
2233 if (redocking) {
2234 int sig, done, closest;
2236 /* Possible cases when redocking:
2238 * icon dragged out of range of any slot -> false
2239 * icon dragged to range of free slot
2240 * icon dragged to range of same slot
2241 * icon dragged to range of different icon
2243 if (abs(ex_x) > DOCK_DETTACH_THRESHOLD)
2244 return False;
2246 if (ex_y >= 0 && ex_y <= max_y_icons && (aicon == icon || !aicon)) {
2247 *ret_x = 0;
2248 *ret_y = ex_y;
2249 return True;
2252 /* start looking at the upper slot or lower? */
2253 if (ex_y * ICON_SIZE < (req_y + offset - dy))
2254 sig = 1;
2255 else
2256 sig = -1;
2258 closest = -1;
2259 done = 0;
2260 /* look for closest free slot */
2261 for (i = 0; i < (DOCK_DETTACH_THRESHOLD + 1) * 2 && !done; i++) {
2262 int j;
2264 done = 1;
2265 closest = sig * (i / 2) + ex_y;
2266 /* check if this slot is used */
2267 if (closest >= 0) {
2268 for (j = 0; j < dock->max_icons; j++) {
2269 if (dock->icon_array[j]
2270 && dock->icon_array[j]->yindex == closest) {
2271 /* slot is used by someone else */
2272 if (dock->icon_array[j] != icon)
2273 done = 0;
2274 break;
2278 sig = -sig;
2280 if (done && closest >= 0 && closest <= max_y_icons &&
2281 ((ex_y >= closest && ex_y - closest < DOCK_DETTACH_THRESHOLD + 1)
2282 || (ex_y < closest && closest - ex_y <= DOCK_DETTACH_THRESHOLD + 1))) {
2283 *ret_x = 0;
2284 *ret_y = closest;
2285 return True;
2287 } else { /* !redocking */
2289 /* if slot is free and the icon is close enough, return it */
2290 if (!aicon && ex_x == 0 && ex_y >= 0 && ex_y <= max_y_icons) {
2291 *ret_x = 0;
2292 *ret_y = ex_y;
2293 return True;
2296 } else { /* CLIP */
2297 int neighbours = 0;
2298 int start, stop, k;
2300 start = icon->omnipresent ? 0 : scr->current_workspace;
2301 stop = icon->omnipresent ? scr->workspace_count : start + 1;
2303 aicon = NULL;
2304 for (k = start; k < stop; k++) {
2305 WDock *tmp = scr->workspaces[k]->clip;
2306 if (!tmp)
2307 continue;
2308 for (i = 0; i < tmp->max_icons; i++) {
2309 nicon = tmp->icon_array[i];
2310 if (nicon && nicon->xindex == ex_x && nicon->yindex == ex_y) {
2311 aicon = nicon;
2312 break;
2315 if (aicon)
2316 break;
2318 for (k = start; k < stop; k++) {
2319 WDock *tmp = scr->workspaces[k]->clip;
2320 if (!tmp)
2321 continue;
2322 for (i = 0; i < tmp->max_icons; i++) {
2323 nicon = tmp->icon_array[i];
2324 if (nicon && nicon != icon && /* Icon can't be it's own neighbour */
2325 (abs(nicon->xindex - ex_x) <= CLIP_ATTACH_VICINITY &&
2326 abs(nicon->yindex - ex_y) <= CLIP_ATTACH_VICINITY)) {
2327 neighbours = 1;
2328 break;
2331 if (neighbours)
2332 break;
2335 if (neighbours && (aicon == NULL || (redocking && aicon == icon))) {
2336 *ret_x = ex_x;
2337 *ret_y = ex_y;
2338 return True;
2341 return False;
2344 static int onScreen(WScreen *scr, int x, int y)
2346 WMRect rect;
2347 int flags;
2349 rect.pos.x = x;
2350 rect.pos.y = y;
2351 rect.size.width = rect.size.height = ICON_SIZE;
2353 wGetRectPlacementInfo(scr, rect, &flags);
2355 return !(flags & (XFLAG_DEAD | XFLAG_PARTIAL));
2359 * returns true if it can find a free slot in the dock,
2360 * in which case it changes x_pos and y_pos accordingly.
2361 * Else returns false.
2363 Bool wDockFindFreeSlot(WDock *dock, int *x_pos, int *y_pos)
2365 WScreen *scr = dock->screen_ptr;
2366 WAppIcon *btn;
2367 WAppIconChain *chain;
2368 unsigned char *slot_map;
2369 int mwidth;
2370 int r;
2371 int x, y;
2372 int i, done = False;
2373 int corner;
2374 int sx = 0, ex = scr->scr_width, ey = scr->scr_height;
2375 int extra_count = 0;
2377 if (dock->type == WM_CLIP && dock != scr->workspaces[scr->current_workspace]->clip)
2378 extra_count = scr->global_icon_count;
2380 /* if the dock is full */
2381 if (dock->icon_count + extra_count >= dock->max_icons)
2382 return False;
2384 if (!wPreferences.flags.nodock && scr->dock) {
2385 if (scr->dock->on_right_side)
2386 ex -= ICON_SIZE + DOCK_EXTRA_SPACE;
2387 else
2388 sx += ICON_SIZE + DOCK_EXTRA_SPACE;
2391 if (ex < dock->x_pos)
2392 ex = dock->x_pos;
2393 if (sx > dock->x_pos + ICON_SIZE)
2394 sx = dock->x_pos + ICON_SIZE;
2395 #define C_NONE 0
2396 #define C_NW 1
2397 #define C_NE 2
2398 #define C_SW 3
2399 #define C_SE 4
2401 /* check if clip is in a corner */
2402 if (dock->type == WM_CLIP) {
2403 if (dock->x_pos < 1 && dock->y_pos < 1)
2404 corner = C_NE;
2405 else if (dock->x_pos < 1 && dock->y_pos >= (ey - ICON_SIZE))
2406 corner = C_SE;
2407 else if (dock->x_pos >= (ex - ICON_SIZE) && dock->y_pos >= (ey - ICON_SIZE))
2408 corner = C_SW;
2409 else if (dock->x_pos >= (ex - ICON_SIZE) && dock->y_pos < 1)
2410 corner = C_NW;
2411 else
2412 corner = C_NONE;
2413 } else {
2414 corner = C_NONE;
2417 /* If the clip is in the corner, use only slots that are in the border
2418 * of the screen */
2419 if (corner != C_NONE) {
2420 char *hmap, *vmap;
2421 int hcount, vcount;
2423 hcount = WMIN(dock->max_icons, scr->scr_width / ICON_SIZE);
2424 vcount = WMIN(dock->max_icons, scr->scr_height / ICON_SIZE);
2425 hmap = wmalloc(hcount + 1);
2426 vmap = wmalloc(vcount + 1);
2428 /* mark used positions */
2429 switch (corner) {
2430 case C_NE:
2431 for (i = 0; i < dock->max_icons; i++) {
2432 btn = dock->icon_array[i];
2433 if (!btn)
2434 continue;
2436 if (btn->xindex == 0 && btn->yindex > 0 && btn->yindex < vcount)
2437 vmap[btn->yindex] = 1;
2438 else if (btn->yindex == 0 && btn->xindex > 0 && btn->xindex < hcount)
2439 hmap[btn->xindex] = 1;
2441 for (chain = scr->global_icons; chain != NULL; chain = chain->next) {
2442 btn = chain->aicon;
2443 if (btn->xindex == 0 && btn->yindex > 0 && btn->yindex < vcount)
2444 vmap[btn->yindex] = 1;
2445 else if (btn->yindex == 0 && btn->xindex > 0 && btn->xindex < hcount)
2446 hmap[btn->xindex] = 1;
2448 break;
2449 case C_NW:
2450 for (i = 0; i < dock->max_icons; i++) {
2451 btn = dock->icon_array[i];
2452 if (!btn)
2453 continue;
2455 if (btn->xindex == 0 && btn->yindex > 0 && btn->yindex < vcount)
2456 vmap[btn->yindex] = 1;
2457 else if (btn->yindex == 0 && btn->xindex < 0 && btn->xindex > -hcount)
2458 hmap[-btn->xindex] = 1;
2460 for (chain = scr->global_icons; chain != NULL; chain = chain->next) {
2461 btn = chain->aicon;
2462 if (btn->xindex == 0 && btn->yindex > 0 && btn->yindex < vcount)
2463 vmap[btn->yindex] = 1;
2464 else if (btn->yindex == 0 && btn->xindex < 0 && btn->xindex > -hcount)
2465 hmap[-btn->xindex] = 1;
2467 break;
2468 case C_SE:
2469 for (i = 0; i < dock->max_icons; i++) {
2470 btn = dock->icon_array[i];
2471 if (!btn)
2472 continue;
2474 if (btn->xindex == 0 && btn->yindex < 0 && btn->yindex > -vcount)
2475 vmap[-btn->yindex] = 1;
2476 else if (btn->yindex == 0 && btn->xindex > 0 && btn->xindex < hcount)
2477 hmap[btn->xindex] = 1;
2479 for (chain = scr->global_icons; chain != NULL; chain = chain->next) {
2480 btn = chain->aicon;
2481 if (btn->xindex == 0 && btn->yindex < 0 && btn->yindex > -vcount)
2482 vmap[-btn->yindex] = 1;
2483 else if (btn->yindex == 0 && btn->xindex > 0 && btn->xindex < hcount)
2484 hmap[btn->xindex] = 1;
2486 break;
2487 case C_SW:
2488 default:
2489 for (i = 0; i < dock->max_icons; i++) {
2490 btn = dock->icon_array[i];
2491 if (!btn)
2492 continue;
2494 if (btn->xindex == 0 && btn->yindex < 0 && btn->yindex > -vcount)
2495 vmap[-btn->yindex] = 1;
2496 else if (btn->yindex == 0 && btn->xindex < 0 && btn->xindex > -hcount)
2497 hmap[-btn->xindex] = 1;
2499 for (chain = scr->global_icons; chain != NULL; chain = chain->next) {
2500 btn = chain->aicon;
2501 if (btn->xindex == 0 && btn->yindex < 0 && btn->yindex > -vcount)
2502 vmap[-btn->yindex] = 1;
2503 else if (btn->yindex == 0 && btn->xindex < 0 && btn->xindex > -hcount)
2504 hmap[-btn->xindex] = 1;
2507 x = 0;
2508 y = 0;
2509 done = 0;
2510 /* search a vacant slot */
2511 for (i = 1; i < WMAX(vcount, hcount); i++) {
2512 if (i < vcount && vmap[i] == 0) {
2513 /* found a slot */
2514 x = 0;
2515 y = i;
2516 done = 1;
2517 break;
2518 } else if (i < hcount && hmap[i] == 0) {
2519 /* found a slot */
2520 x = i;
2521 y = 0;
2522 done = 1;
2523 break;
2526 wfree(vmap);
2527 wfree(hmap);
2528 /* If found a slot, translate and return */
2529 if (done) {
2530 if (corner == C_NW || corner == C_NE)
2531 *y_pos = y;
2532 else
2533 *y_pos = -y;
2535 if (corner == C_NE || corner == C_SE)
2536 *x_pos = x;
2537 else
2538 *x_pos = -x;
2540 return True;
2542 /* else, try to find a slot somewhere else */
2545 /* a map of mwidth x mwidth would be enough if we allowed icons to be
2546 * placed outside of screen */
2547 mwidth = (int)ceil(sqrt(dock->max_icons));
2549 /* In the worst case (the clip is in the corner of the screen),
2550 * the amount of icons that fit in the clip is smaller.
2551 * Double the map to get a safe value.
2553 mwidth += mwidth;
2555 r = (mwidth - 1) / 2;
2557 slot_map = wmalloc(mwidth * mwidth);
2559 #define XY2OFS(x,y) (WMAX(abs(x),abs(y)) > r) ? 0 : (((y)+r)*(mwidth)+(x)+r)
2561 /* mark used slots in the map. If the slot falls outside the map
2562 * (for example, when all icons are placed in line), ignore them. */
2563 for (i = 0; i < dock->max_icons; i++) {
2564 btn = dock->icon_array[i];
2565 if (btn)
2566 slot_map[XY2OFS(btn->xindex, btn->yindex)] = 1;
2569 for (chain = scr->global_icons; chain != NULL; chain = chain->next)
2570 slot_map[XY2OFS(chain->aicon->xindex, chain->aicon->yindex)] = 1;
2572 /* Find closest slot from the center that is free by scanning the
2573 * map from the center to outward in circular passes.
2574 * This will not result in a neat layout, but will be optimal
2575 * in the sense that there will not be holes left.
2577 done = 0;
2578 for (i = 1; i <= r && !done; i++) {
2579 int tx, ty;
2581 /* top and bottom parts of the ring */
2582 for (x = -i; x <= i && !done; x++) {
2583 tx = dock->x_pos + x * ICON_SIZE;
2584 y = -i;
2585 ty = dock->y_pos + y * ICON_SIZE;
2586 if (slot_map[XY2OFS(x, y)] == 0 && onScreen(scr, tx, ty)) {
2587 *x_pos = x;
2588 *y_pos = y;
2589 done = 1;
2590 break;
2592 y = i;
2593 ty = dock->y_pos + y * ICON_SIZE;
2594 if (slot_map[XY2OFS(x, y)] == 0 && onScreen(scr, tx, ty)) {
2595 *x_pos = x;
2596 *y_pos = y;
2597 done = 1;
2598 break;
2601 /* left and right parts of the ring */
2602 for (y = -i + 1; y <= i - 1; y++) {
2603 ty = dock->y_pos + y * ICON_SIZE;
2604 x = -i;
2605 tx = dock->x_pos + x * ICON_SIZE;
2606 if (slot_map[XY2OFS(x, y)] == 0 && onScreen(scr, tx, ty)) {
2607 *x_pos = x;
2608 *y_pos = y;
2609 done = 1;
2610 break;
2612 x = i;
2613 tx = dock->x_pos + x * ICON_SIZE;
2614 if (slot_map[XY2OFS(x, y)] == 0 && onScreen(scr, tx, ty)) {
2615 *x_pos = x;
2616 *y_pos = y;
2617 done = 1;
2618 break;
2622 wfree(slot_map);
2623 #undef XY2OFS
2624 return done;
2627 static void moveDock(WDock *dock, int new_x, int new_y)
2629 WAppIcon *btn;
2630 int i;
2632 dock->x_pos = new_x;
2633 dock->y_pos = new_y;
2634 for (i = 0; i < dock->max_icons; i++) {
2635 btn = dock->icon_array[i];
2636 if (btn) {
2637 btn->x_pos = new_x + btn->xindex * ICON_SIZE;
2638 btn->y_pos = new_y + btn->yindex * ICON_SIZE;
2639 XMoveWindow(dpy, btn->icon->core->window, btn->x_pos, btn->y_pos);
2644 static void swapDock(WDock *dock)
2646 WScreen *scr = dock->screen_ptr;
2647 WAppIcon *btn;
2648 int x, i;
2650 if (dock->on_right_side)
2651 x = dock->x_pos = scr->scr_width - ICON_SIZE - DOCK_EXTRA_SPACE;
2652 else
2653 x = dock->x_pos = DOCK_EXTRA_SPACE;
2655 for (i = 0; i < dock->max_icons; i++) {
2656 btn = dock->icon_array[i];
2657 if (btn) {
2658 btn->x_pos = x;
2659 XMoveWindow(dpy, btn->icon->core->window, btn->x_pos, btn->y_pos);
2663 wScreenUpdateUsableArea(scr);
2666 static pid_t execCommand(WAppIcon *btn, char *command, WSavedState *state)
2668 WScreen *scr = btn->icon->core->screen_ptr;
2669 pid_t pid;
2670 char **argv;
2671 int argc;
2672 char *cmdline;
2674 cmdline = ExpandOptions(scr, command);
2676 if (scr->flags.dnd_data_convertion_status || !cmdline) {
2677 if (cmdline)
2678 wfree(cmdline);
2679 if (state)
2680 wfree(state);
2681 return 0;
2684 wtokensplit(cmdline, &argv, &argc);
2686 if (!argc) {
2687 if (cmdline)
2688 wfree(cmdline);
2689 if (state)
2690 wfree(state);
2691 return 0;
2694 if ((pid = fork()) == 0) {
2695 char **args;
2696 int i;
2698 SetupEnvironment(scr);
2700 #ifdef HAVE_SETSID
2701 setsid();
2702 #endif
2704 args = malloc(sizeof(char *) * (argc + 1));
2705 if (!args)
2706 exit(111);
2708 for (i = 0; i < argc; i++)
2709 args[i] = argv[i];
2711 args[argc] = NULL;
2712 execvp(argv[0], args);
2713 exit(111);
2715 wtokenfree(argv, argc);
2717 if (pid > 0) {
2718 if (!state) {
2719 state = wmalloc(sizeof(WSavedState));
2720 state->hidden = -1;
2721 state->miniaturized = -1;
2722 state->shaded = -1;
2723 if (btn->dock == scr->dock || btn->omnipresent)
2724 state->workspace = -1;
2725 else
2726 state->workspace = scr->current_workspace;
2728 wWindowAddSavedState(btn->wm_instance, btn->wm_class, cmdline, pid, state);
2729 wAddDeathHandler(pid, (WDeathHandler *) trackDeadProcess, btn->dock);
2730 } else if (state) {
2731 wfree(state);
2733 wfree(cmdline);
2734 return pid;
2737 void wDockHideIcons(WDock *dock)
2739 int i;
2741 if (dock == NULL)
2742 return;
2744 for (i = 1; i < dock->max_icons; i++) {
2745 if (dock->icon_array[i])
2746 XUnmapWindow(dpy, dock->icon_array[i]->icon->core->window);
2748 dock->mapped = 0;
2750 dockIconPaint(dock->icon_array[0]);
2753 void wDockShowIcons(WDock *dock)
2755 int i, newlevel;
2756 WAppIcon *btn;
2758 if (dock == NULL)
2759 return;
2761 btn = dock->icon_array[0];
2762 moveDock(dock, btn->x_pos, btn->y_pos);
2764 newlevel = dock->lowered ? WMNormalLevel : WMDockLevel;
2765 ChangeStackingLevel(btn->icon->core, newlevel);
2767 for (i = 1; i < dock->max_icons; i++) {
2768 if (dock->icon_array[i]) {
2769 MoveInStackListAbove(dock->icon_array[i]->icon->core, btn->icon->core);
2770 break;
2774 if (!dock->collapsed) {
2775 for (i = 1; i < dock->max_icons; i++) {
2776 if (dock->icon_array[i])
2777 XMapWindow(dpy, dock->icon_array[i]->icon->core->window);
2780 dock->mapped = 1;
2782 dockIconPaint(btn);
2785 void wDockLower(WDock *dock)
2787 int i;
2789 for (i = 0; i < dock->max_icons; i++) {
2790 if (dock->icon_array[i])
2791 wLowerFrame(dock->icon_array[i]->icon->core);
2795 void wDockRaise(WDock *dock)
2797 int i;
2799 for (i = dock->max_icons - 1; i >= 0; i--) {
2800 if (dock->icon_array[i])
2801 wRaiseFrame(dock->icon_array[i]->icon->core);
2805 void wDockRaiseLower(WDock *dock)
2807 if (!dock->icon_array[0]->icon->core->stacking->above
2808 || (dock->icon_array[0]->icon->core->stacking->window_level
2809 != dock->icon_array[0]->icon->core->stacking->above->stacking->window_level))
2810 wDockLower(dock);
2811 else
2812 wDockRaise(dock);
2815 void wDockFinishLaunch(WDock *dock, WAppIcon *icon)
2817 icon->launching = 0;
2818 icon->relaunching = 0;
2819 dockIconPaint(icon);
2822 WAppIcon *wDockFindIconForWindow(WDock *dock, Window window)
2824 WAppIcon *icon;
2825 int i;
2827 for (i = 0; i < dock->max_icons; i++) {
2828 icon = dock->icon_array[i];
2829 if (icon && icon->main_window == window)
2830 return icon;
2832 return NULL;
2835 void wDockTrackWindowLaunch(WDock *dock, Window window)
2837 WAppIcon *icon;
2838 char *wm_class, *wm_instance;
2839 int i;
2840 Bool firstPass = True;
2841 Bool found = False;
2842 char *command = NULL;
2844 if (!PropGetWMClass(window, &wm_class, &wm_instance) || (!wm_class && !wm_instance))
2845 return;
2847 command = GetCommandForWindow(window);
2848 retry:
2849 for (i = 0; i < dock->max_icons; i++) {
2850 icon = dock->icon_array[i];
2851 if (!icon)
2852 continue;
2854 /* app is already attached to icon */
2855 if (icon->main_window == window) {
2856 found = True;
2857 break;
2860 if ((icon->wm_instance || icon->wm_class)
2861 && (icon->launching || !icon->running)) {
2863 if (icon->wm_instance && wm_instance && strcmp(icon->wm_instance, wm_instance) != 0)
2864 continue;
2866 if (icon->wm_class && wm_class && strcmp(icon->wm_class, wm_class) != 0)
2867 continue;
2869 if (firstPass && command && strcmp(icon->command, command) != 0)
2870 continue;
2872 if (!icon->relaunching) {
2873 WApplication *wapp;
2875 /* Possibly an application that was docked with dockit,
2876 * but the user did not update WMState to indicate that
2877 * it was docked by force */
2878 wapp = wApplicationOf(window);
2879 if (!wapp) {
2880 icon->forced_dock = 1;
2881 icon->running = 0;
2883 if (!icon->forced_dock)
2884 icon->main_window = window;
2886 found = True;
2887 if (!wPreferences.no_animations && !icon->launching &&
2888 !dock->screen_ptr->flags.startup && !dock->collapsed) {
2889 WAppIcon *aicon;
2890 int x0, y0;
2892 icon->launching = 1;
2893 dockIconPaint(icon);
2895 aicon = wAppIconCreateForDock(dock->screen_ptr, NULL,
2896 wm_instance, wm_class, TILE_NORMAL);
2897 /* XXX: can: aicon->icon == NULL ? */
2898 PlaceIcon(dock->screen_ptr, &x0, &y0, wGetHeadForWindow(aicon->icon->owner));
2899 wAppIconMove(aicon, x0, y0);
2900 /* Should this always be lowered? -Dan */
2901 if (dock->lowered)
2902 wLowerFrame(aicon->icon->core);
2903 XMapWindow(dpy, aicon->icon->core->window);
2904 aicon->launching = 1;
2905 wAppIconPaint(aicon);
2906 SlideWindow(aicon->icon->core->window, x0, y0, icon->x_pos, icon->y_pos);
2907 XUnmapWindow(dpy, aicon->icon->core->window);
2908 wAppIconDestroy(aicon);
2910 wDockFinishLaunch(dock, icon);
2911 break;
2915 if (firstPass && !found) {
2916 firstPass = False;
2917 goto retry;
2920 if (command)
2921 wfree(command);
2923 if (wm_class)
2924 free(wm_class);
2925 if (wm_instance)
2926 free(wm_instance);
2929 void wClipUpdateForWorkspaceChange(WScreen *scr, int workspace)
2931 if (!wPreferences.flags.noclip) {
2932 scr->clip_icon->dock = scr->workspaces[workspace]->clip;
2933 if (scr->current_workspace != workspace) {
2934 WDock *old_clip = scr->workspaces[scr->current_workspace]->clip;
2935 WAppIconChain *chain = scr->global_icons;
2937 while (chain) {
2938 moveIconBetweenDocks(chain->aicon->dock,
2939 scr->workspaces[workspace]->clip,
2940 chain->aicon, chain->aicon->xindex, chain->aicon->yindex);
2941 if (scr->workspaces[workspace]->clip->collapsed)
2942 XUnmapWindow(dpy, chain->aicon->icon->core->window);
2943 chain = chain->next;
2946 wDockHideIcons(old_clip);
2947 if (old_clip->auto_raise_lower) {
2948 if (old_clip->auto_raise_magic) {
2949 WMDeleteTimerHandler(old_clip->auto_raise_magic);
2950 old_clip->auto_raise_magic = NULL;
2952 wDockLower(old_clip);
2954 if (old_clip->auto_collapse) {
2955 if (old_clip->auto_expand_magic) {
2956 WMDeleteTimerHandler(old_clip->auto_expand_magic);
2957 old_clip->auto_expand_magic = NULL;
2959 old_clip->collapsed = 1;
2961 wDockShowIcons(scr->workspaces[workspace]->clip);
2966 static void trackDeadProcess(pid_t pid, unsigned char status, WDock *dock)
2968 WAppIcon *icon;
2969 int i;
2971 for (i = 0; i < dock->max_icons; i++) {
2972 icon = dock->icon_array[i];
2973 if (!icon)
2974 continue;
2976 if (icon->launching && icon->pid == pid) {
2977 if (!icon->relaunching) {
2978 icon->running = 0;
2979 icon->main_window = None;
2981 wDockFinishLaunch(dock, icon);
2982 icon->pid = 0;
2983 if (status == 111) {
2984 char msg[PATH_MAX];
2985 char *cmd;
2987 #ifdef XDND
2988 if (icon->drop_launch)
2989 cmd = icon->dnd_command;
2990 else
2991 #endif
2992 if (icon->paste_launch)
2993 cmd = icon->paste_command;
2994 else
2995 cmd = icon->command;
2997 snprintf(msg, sizeof(msg), _("Could not execute command \"%s\""), cmd);
2999 wMessageDialog(dock->screen_ptr, _("Error"), msg, _("OK"), NULL, NULL);
3001 break;
3006 static void toggleLowered(WDock *dock)
3008 WAppIcon *tmp;
3009 int newlevel, i;
3011 /* lower/raise Dock */
3012 if (!dock->lowered) {
3013 newlevel = WMNormalLevel;
3014 dock->lowered = 1;
3015 } else {
3016 newlevel = WMDockLevel;
3017 dock->lowered = 0;
3020 for (i = 0; i < dock->max_icons; i++) {
3021 tmp = dock->icon_array[i];
3022 if (!tmp)
3023 continue;
3025 ChangeStackingLevel(tmp->icon->core, newlevel);
3026 if (dock->lowered)
3027 wLowerFrame(tmp->icon->core);
3030 if (dock->type == WM_DOCK)
3031 wScreenUpdateUsableArea(dock->screen_ptr);
3034 static void toggleCollapsed(WDock *dock)
3036 if (dock->collapsed) {
3037 dock->collapsed = 0;
3038 wDockShowIcons(dock);
3039 } else {
3040 dock->collapsed = 1;
3041 wDockHideIcons(dock);
3045 static void openDockMenu(WDock *dock, WAppIcon *aicon, XEvent *event)
3047 WScreen *scr = dock->screen_ptr;
3048 WObjDescriptor *desc;
3049 WMenuEntry *entry;
3050 WApplication *wapp = NULL;
3051 int index = 0;
3052 int x_pos;
3053 int n_selected;
3054 int appIsRunning = aicon->running && aicon->icon && aicon->icon->owner;
3056 if (dock->type == WM_DOCK) {
3057 /* keep on top */
3058 entry = dock->menu->entries[index];
3059 entry->flags.indicator_on = !dock->lowered;
3060 entry->clientdata = dock;
3061 dock->menu->flags.realized = 0;
3062 } else {
3063 /* clip options */
3064 if (scr->clip_options)
3065 updateClipOptionsMenu(scr->clip_options, dock);
3067 n_selected = numberOfSelectedIcons(dock);
3069 /* Rename Workspace */
3070 entry = dock->menu->entries[++index];
3071 if (aicon == scr->clip_icon) {
3072 entry->callback = renameCallback;
3073 entry->clientdata = dock;
3074 entry->flags.indicator = 0;
3075 entry->text = _("Rename Workspace");
3076 } else {
3077 entry->callback = omnipresentCallback;
3078 entry->clientdata = aicon;
3079 if (n_selected > 0) {
3080 entry->flags.indicator = 0;
3081 entry->text = _("Toggle Omnipresent");
3082 } else {
3083 entry->flags.indicator = 1;
3084 entry->flags.indicator_on = aicon->omnipresent;
3085 entry->flags.indicator_type = MI_CHECK;
3086 entry->text = _("Omnipresent");
3090 /* select/unselect icon */
3091 entry = dock->menu->entries[++index];
3092 entry->clientdata = aicon;
3093 entry->flags.indicator_on = aicon->icon->selected;
3094 wMenuSetEnabled(dock->menu, index, aicon != scr->clip_icon);
3096 /* select/unselect all icons */
3097 entry = dock->menu->entries[++index];
3098 entry->clientdata = aicon;
3099 if (n_selected > 0)
3100 entry->text = _("Unselect All Icons");
3101 else
3102 entry->text = _("Select All Icons");
3104 wMenuSetEnabled(dock->menu, index, dock->icon_count > 1);
3106 /* keep icon(s) */
3107 entry = dock->menu->entries[++index];
3108 entry->clientdata = aicon;
3109 if (n_selected > 1)
3110 entry->text = _("Keep Icons");
3111 else
3112 entry->text = _("Keep Icon");
3114 wMenuSetEnabled(dock->menu, index, dock->icon_count > 1);
3116 /* this is the workspace submenu part */
3117 entry = dock->menu->entries[++index];
3118 if (n_selected > 1)
3119 entry->text = _("Move Icons To");
3120 else
3121 entry->text = _("Move Icon To");
3123 if (scr->clip_submenu)
3124 updateWorkspaceMenu(scr->clip_submenu, aicon);
3126 wMenuSetEnabled(dock->menu, index, !aicon->omnipresent);
3128 /* remove icon(s) */
3129 entry = dock->menu->entries[++index];
3130 entry->clientdata = aicon;
3131 if (n_selected > 1)
3132 entry->text = _("Remove Icons");
3133 else
3134 entry->text = _("Remove Icon");
3136 wMenuSetEnabled(dock->menu, index, dock->icon_count > 1);
3138 /* attract icon(s) */
3139 entry = dock->menu->entries[++index];
3140 entry->clientdata = aicon;
3142 dock->menu->flags.realized = 0;
3143 wMenuRealize(dock->menu);
3146 if (aicon->icon->owner)
3147 wapp = wApplicationOf(aicon->icon->owner->main_window);
3148 else
3149 wapp = NULL;
3151 /* launch */
3152 entry = dock->menu->entries[++index];
3153 entry->clientdata = aicon;
3154 wMenuSetEnabled(dock->menu, index, aicon->command != NULL);
3156 /* unhide here */
3157 entry = dock->menu->entries[++index];
3158 entry->clientdata = aicon;
3159 if (wapp && wapp->flags.hidden)
3160 entry->text = _("Unhide Here");
3161 else
3162 entry->text = _("Bring Here");
3164 wMenuSetEnabled(dock->menu, index, appIsRunning);
3166 /* hide */
3167 entry = dock->menu->entries[++index];
3168 entry->clientdata = aicon;
3169 if (wapp && wapp->flags.hidden)
3170 entry->text = _("Unhide");
3171 else
3172 entry->text = _("Hide");
3174 wMenuSetEnabled(dock->menu, index, appIsRunning);
3176 /* settings */
3177 entry = dock->menu->entries[++index];
3178 entry->clientdata = aicon;
3179 wMenuSetEnabled(dock->menu, index, !aicon->editing && !wPreferences.flags.noupdates);
3181 /* kill */
3182 entry = dock->menu->entries[++index];
3183 entry->clientdata = aicon;
3184 wMenuSetEnabled(dock->menu, index, appIsRunning);
3186 if (!dock->menu->flags.realized)
3187 wMenuRealize(dock->menu);
3189 if (dock->type == WM_CLIP) {
3190 /*x_pos = event->xbutton.x_root+2; */
3191 x_pos = event->xbutton.x_root - dock->menu->frame->core->width / 2 - 1;
3192 if (x_pos < 0) {
3193 x_pos = 0;
3194 } else if (x_pos + dock->menu->frame->core->width > scr->scr_width - 2) {
3195 x_pos = scr->scr_width - dock->menu->frame->core->width - 4;
3197 } else {
3198 x_pos = dock->on_right_side ? scr->scr_width - dock->menu->frame->core->width - 3 : 0;
3201 wMenuMapAt(dock->menu, x_pos, event->xbutton.y_root + 2, False);
3203 /* allow drag select */
3204 event->xany.send_event = True;
3205 desc = &dock->menu->menu->descriptor;
3206 (*desc->handle_mousedown) (desc, event);
3209 /******************************************************************/
3210 static void iconDblClick(WObjDescriptor *desc, XEvent *event)
3212 WAppIcon *btn = desc->parent;
3213 WDock *dock = btn->dock;
3214 WApplication *wapp = NULL;
3215 int unhideHere = 0;
3217 if (btn->icon->owner && !(event->xbutton.state & ControlMask)) {
3218 wapp = wApplicationOf(btn->icon->owner->main_window);
3220 assert(wapp != NULL);
3222 unhideHere = (event->xbutton.state & ShiftMask);
3224 /* go to the last workspace that the user worked on the app */
3225 if (wapp->last_workspace != dock->screen_ptr->current_workspace && !unhideHere)
3226 wWorkspaceChange(dock->screen_ptr, wapp->last_workspace);
3228 wUnhideApplication(wapp, event->xbutton.button == Button2, unhideHere);
3230 if (event->xbutton.state & MOD_MASK)
3231 wHideOtherApplications(btn->icon->owner);
3232 } else {
3233 if (event->xbutton.button == Button1) {
3234 if (event->xbutton.state & MOD_MASK) {
3235 /* raise/lower dock */
3236 toggleLowered(dock);
3237 } else if (btn == dock->screen_ptr->clip_icon) {
3238 if (getClipButton(event->xbutton.x, event->xbutton.y) == CLIP_IDLE)
3239 toggleCollapsed(dock);
3240 else
3241 handleClipChangeWorkspace(dock->screen_ptr, event);
3242 } else if (btn->command) {
3243 if (!btn->launching && (!btn->running || (event->xbutton.state & ControlMask)))
3244 launchDockedApplication(btn, False);
3245 } else if (btn->xindex == 0 && btn->yindex == 0 && btn->dock->type == WM_DOCK) {
3246 wShowInfoPanel(dock->screen_ptr);
3252 static void handleDockMove(WDock *dock, WAppIcon *aicon, XEvent *event)
3254 WScreen *scr = dock->screen_ptr;
3255 int ofs_x = event->xbutton.x, ofs_y = event->xbutton.y;
3256 int x, y;
3257 XEvent ev;
3258 int grabbed = 0, swapped = 0, done;
3259 Pixmap ghost = None;
3260 int superfluous = wPreferences.superfluous; /* we catch it to avoid problems */
3262 if (XGrabPointer(dpy, aicon->icon->core->window, True, ButtonMotionMask
3263 | ButtonReleaseMask | ButtonPressMask, GrabModeAsync,
3264 GrabModeAsync, None, None, CurrentTime) != GrabSuccess)
3265 wwarning("pointer grab failed for dock move");
3267 y = 0;
3268 for (x = 0; x < dock->max_icons; x++) {
3269 if (dock->icon_array[x] != NULL && dock->icon_array[x]->yindex > y)
3270 y = dock->icon_array[x]->yindex;
3272 y++;
3273 XResizeWindow(dpy, scr->dock_shadow, ICON_SIZE, ICON_SIZE * y);
3275 done = 0;
3276 while (!done) {
3277 WMMaskEvent(dpy, PointerMotionMask | ButtonReleaseMask | ButtonPressMask
3278 | ButtonMotionMask | ExposureMask | EnterWindowMask, &ev);
3279 switch (ev.type) {
3280 case Expose:
3281 WMHandleEvent(&ev);
3282 break;
3284 case EnterNotify:
3285 /* It means the cursor moved so fast that it entered
3286 * something else (if moving slowly, it would have
3287 * stayed in the dock that is being moved. Ignore such
3288 * "spurious" EnterNotifiy's */
3289 break;
3291 case MotionNotify:
3292 if (!grabbed) {
3293 if (abs(ofs_x - ev.xmotion.x) >= MOVE_THRESHOLD
3294 || abs(ofs_y - ev.xmotion.y) >= MOVE_THRESHOLD) {
3295 XChangeActivePointerGrab(dpy, ButtonMotionMask
3296 | ButtonReleaseMask | ButtonPressMask,
3297 wCursor[WCUR_MOVE], CurrentTime);
3298 grabbed = 1;
3300 break;
3302 if (dock->type == WM_CLIP) {
3303 x = ev.xmotion.x_root - ofs_x;
3304 y = ev.xmotion.y_root - ofs_y;
3305 wScreenKeepInside(scr, &x, &y, ICON_SIZE, ICON_SIZE);
3307 moveDock(dock, x, y);
3308 } else {
3309 /* move vertically if pointer is inside the dock */
3310 if ((dock->on_right_side && ev.xmotion.x_root >= dock->x_pos - ICON_SIZE)
3311 || (!dock->on_right_side && ev.xmotion.x_root <= dock->x_pos + ICON_SIZE * 2)) {
3313 x = ev.xmotion.x_root - ofs_x;
3314 y = ev.xmotion.y_root - ofs_y;
3315 wScreenKeepInside(scr, &x, &y, ICON_SIZE, ICON_SIZE);
3316 moveDock(dock, dock->x_pos, y);
3318 /* move horizontally to change sides */
3319 x = ev.xmotion.x_root - ofs_x;
3320 if (!dock->on_right_side) {
3322 /* is on left */
3323 if (ev.xmotion.x_root > dock->x_pos + ICON_SIZE * 2) {
3324 XMoveWindow(dpy, scr->dock_shadow, scr->scr_width - ICON_SIZE
3325 - DOCK_EXTRA_SPACE - 1, dock->y_pos);
3326 if (superfluous && ghost == None) {
3327 ghost = MakeGhostDock(dock, dock->x_pos,
3328 scr->scr_width - ICON_SIZE
3329 - DOCK_EXTRA_SPACE - 1, dock->y_pos);
3330 XSetWindowBackgroundPixmap(dpy, scr->dock_shadow, ghost);
3331 XClearWindow(dpy, scr->dock_shadow);
3333 XMapRaised(dpy, scr->dock_shadow);
3334 swapped = 1;
3335 } else {
3336 if (superfluous && ghost != None) {
3337 XFreePixmap(dpy, ghost);
3338 ghost = None;
3340 XUnmapWindow(dpy, scr->dock_shadow);
3341 swapped = 0;
3343 } else {
3344 /* is on right */
3345 if (ev.xmotion.x_root < dock->x_pos - ICON_SIZE) {
3346 XMoveWindow(dpy, scr->dock_shadow, DOCK_EXTRA_SPACE, dock->y_pos);
3347 if (superfluous && ghost == None) {
3348 ghost = MakeGhostDock(dock, dock->x_pos,
3349 DOCK_EXTRA_SPACE, dock->y_pos);
3350 XSetWindowBackgroundPixmap(dpy, scr->dock_shadow, ghost);
3351 XClearWindow(dpy, scr->dock_shadow);
3353 XMapRaised(dpy, scr->dock_shadow);
3354 swapped = -1;
3355 } else {
3356 XUnmapWindow(dpy, scr->dock_shadow);
3357 swapped = 0;
3358 if (superfluous && ghost != None) {
3359 XFreePixmap(dpy, ghost);
3360 ghost = None;
3365 break;
3367 case ButtonPress:
3368 break;
3370 case ButtonRelease:
3371 if (ev.xbutton.button != event->xbutton.button)
3372 break;
3373 XUngrabPointer(dpy, CurrentTime);
3374 XUnmapWindow(dpy, scr->dock_shadow);
3375 XResizeWindow(dpy, scr->dock_shadow, ICON_SIZE, ICON_SIZE);
3376 if (dock->type == WM_DOCK) {
3377 if (swapped != 0) {
3378 if (swapped > 0)
3379 dock->on_right_side = 1;
3380 else
3381 dock->on_right_side = 0;
3382 swapDock(dock);
3383 wArrangeIcons(scr, False);
3386 done = 1;
3387 break;
3390 if (superfluous) {
3391 if (ghost != None)
3392 XFreePixmap(dpy, ghost);
3393 XSetWindowBackground(dpy, scr->dock_shadow, scr->white_pixel);
3397 static Bool handleIconMove(WDock *dock, WAppIcon *aicon, XEvent *event)
3399 WScreen *scr = dock->screen_ptr;
3400 Window wins[2];
3401 WIcon *icon = aicon->icon;
3402 WDock *dock2 = NULL, *last_dock = dock, *clip = NULL;
3403 int ondock, grabbed = 0, change_dock = 0, collapsed = 0;
3404 XEvent ev;
3405 int x = aicon->x_pos, y = aicon->y_pos;
3406 int ofs_x = event->xbutton.x, ofs_y = event->xbutton.y;
3407 int shad_x = x, shad_y = y;
3408 int ix = aicon->xindex, iy = aicon->yindex;
3409 int tmp;
3410 Pixmap ghost = None;
3411 Bool docked;
3412 int superfluous = wPreferences.superfluous; /* we catch it to avoid problems */
3413 int omnipresent = aicon->omnipresent; /* this must be cached!!! */
3414 Bool showed_all_clips = False;
3415 Bool hasMoved = False;
3417 if (wPreferences.flags.noupdates)
3418 return hasMoved;
3420 if (XGrabPointer(dpy, icon->core->window, True, ButtonMotionMask
3421 | ButtonReleaseMask | ButtonPressMask, GrabModeAsync,
3422 GrabModeAsync, None, None, CurrentTime) != GrabSuccess) {
3425 if (!(event->xbutton.state & MOD_MASK))
3426 wRaiseFrame(icon->core);
3428 if (!wPreferences.flags.noclip)
3429 clip = scr->workspaces[scr->current_workspace]->clip;
3431 if (dock == scr->dock && !wPreferences.flags.noclip)
3432 dock2 = clip;
3433 else if (dock != scr->dock && !wPreferences.flags.nodock)
3434 dock2 = scr->dock;
3436 wins[0] = icon->core->window;
3437 wins[1] = scr->dock_shadow;
3438 XRestackWindows(dpy, wins, 2);
3439 XMoveResizeWindow(dpy, scr->dock_shadow, aicon->x_pos, aicon->y_pos, ICON_SIZE, ICON_SIZE);
3440 if (superfluous) {
3441 if (icon->pixmap != None)
3442 ghost = MakeGhostIcon(scr, icon->pixmap);
3443 else
3444 ghost = MakeGhostIcon(scr, icon->core->window);
3446 XSetWindowBackgroundPixmap(dpy, scr->dock_shadow, ghost);
3447 XClearWindow(dpy, scr->dock_shadow);
3449 XMapWindow(dpy, scr->dock_shadow);
3451 ondock = 1;
3453 while (1) {
3454 XMaskEvent(dpy, PointerMotionMask | ButtonReleaseMask | ButtonPressMask
3455 | ButtonMotionMask | ExposureMask, &ev);
3456 switch (ev.type) {
3457 case Expose:
3458 WMHandleEvent(&ev);
3459 break;
3461 case MotionNotify:
3462 hasMoved = True;
3463 if (!grabbed) {
3464 if (abs(ofs_x - ev.xmotion.x) >= MOVE_THRESHOLD
3465 || abs(ofs_y - ev.xmotion.y) >= MOVE_THRESHOLD) {
3466 XChangeActivePointerGrab(dpy, ButtonMotionMask
3467 | ButtonReleaseMask | ButtonPressMask,
3468 wCursor[WCUR_MOVE], CurrentTime);
3469 grabbed = 1;
3470 } else {
3471 break;
3475 if (omnipresent && !showed_all_clips) {
3476 int i;
3477 for (i = 0; i < scr->workspace_count; i++) {
3478 if (i == scr->current_workspace)
3479 continue;
3480 wDockShowIcons(scr->workspaces[i]->clip);
3481 /* Note: if dock is collapsed (for instance,
3482 because it auto-collapses), its icons
3483 still won't show up */
3485 showed_all_clips = True; /* To prevent flickering */
3488 x = ev.xmotion.x_root - ofs_x;
3489 y = ev.xmotion.y_root - ofs_y;
3490 tmp = wDockSnapIcon(dock, aicon, x, y, &ix, &iy, True);
3491 if (tmp && dock2) {
3492 change_dock = 0;
3493 if (last_dock != dock && collapsed) {
3494 last_dock->collapsed = 1;
3495 wDockHideIcons(last_dock);
3496 collapsed = 0;
3498 if (!collapsed && (collapsed = dock->collapsed)) {
3499 dock->collapsed = 0;
3500 wDockShowIcons(dock);
3502 if (dock->auto_raise_lower)
3503 wDockRaise(dock);
3504 last_dock = dock;
3505 } else if (dock2) {
3506 tmp = wDockSnapIcon(dock2, aicon, x, y, &ix, &iy, False);
3507 if (tmp) {
3508 change_dock = 1;
3509 if (last_dock != dock2 && collapsed) {
3510 last_dock->collapsed = 1;
3511 wDockHideIcons(last_dock);
3512 collapsed = 0;
3514 if (!collapsed && (collapsed = dock2->collapsed)) {
3515 dock2->collapsed = 0;
3516 wDockShowIcons(dock2);
3518 if (dock2->auto_raise_lower)
3519 wDockRaise(dock2);
3520 last_dock = dock2;
3523 if (aicon->launching || aicon->lock || (aicon->running && !(ev.xmotion.state & MOD_MASK))
3524 || (!aicon->running && tmp)) {
3525 shad_x = last_dock->x_pos + ix * wPreferences.icon_size;
3526 shad_y = last_dock->y_pos + iy * wPreferences.icon_size;
3528 XMoveWindow(dpy, scr->dock_shadow, shad_x, shad_y);
3530 if (!ondock)
3531 XMapWindow(dpy, scr->dock_shadow);
3533 ondock = 1;
3534 } else {
3535 if (ondock)
3536 XUnmapWindow(dpy, scr->dock_shadow);
3538 ondock = 0;
3540 XMoveWindow(dpy, icon->core->window, x, y);
3541 break;
3543 case ButtonPress:
3544 break;
3546 case ButtonRelease:
3547 if (ev.xbutton.button != event->xbutton.button)
3548 break;
3549 XUngrabPointer(dpy, CurrentTime);
3550 if (ondock) {
3551 SlideWindow(icon->core->window, x, y, shad_x, shad_y);
3552 XUnmapWindow(dpy, scr->dock_shadow);
3553 if (!change_dock) {
3554 reattachIcon(dock, aicon, ix, iy);
3555 if (clip && dock != clip && clip->auto_raise_lower)
3556 wDockLower(clip);
3557 } else {
3558 docked = moveIconBetweenDocks(dock, dock2, aicon, ix, iy);
3559 if (!docked) {
3560 /* Slide it back if dock rejected it */
3561 SlideWindow(icon->core->window, x, y, aicon->x_pos, aicon->y_pos);
3562 reattachIcon(dock, aicon, aicon->xindex, aicon->yindex);
3564 if (last_dock->type == WM_CLIP && last_dock->auto_collapse)
3565 collapsed = 0;
3567 } else {
3568 aicon->x_pos = x;
3569 aicon->y_pos = y;
3570 if (superfluous) {
3571 if (!aicon->running && !wPreferences.no_animations) {
3572 /* We need to deselect it, even if is deselected in
3573 * wDockDetach(), because else DoKaboom() will fail.
3575 if (aicon->icon->selected)
3576 wIconSelect(aicon->icon);
3578 DoKaboom(scr, aicon->icon->core->window, x, y);
3581 if (clip && clip->auto_raise_lower)
3582 wDockLower(clip);
3583 wDockDetach(dock, aicon);
3585 if (collapsed) {
3586 last_dock->collapsed = 1;
3587 wDockHideIcons(last_dock);
3588 collapsed = 0;
3590 if (superfluous) {
3591 if (ghost != None)
3592 XFreePixmap(dpy, ghost);
3593 XSetWindowBackground(dpy, scr->dock_shadow, scr->white_pixel);
3595 if (showed_all_clips) {
3596 int i;
3597 for (i = 0; i < scr->workspace_count; i++) {
3598 if (i == scr->current_workspace)
3599 continue;
3600 wDockHideIcons(scr->workspaces[i]->clip);
3603 return hasMoved;;
3606 return False; /* never reached */
3609 static int getClipButton(int px, int py)
3611 int pt = (CLIP_BUTTON_SIZE + 2) * ICON_SIZE / 64;
3613 if (px < 0 || py < 0 || px >= ICON_SIZE || py >= ICON_SIZE)
3614 return CLIP_IDLE;
3616 if (py <= pt - ((int)ICON_SIZE - 1 - px))
3617 return CLIP_FORWARD;
3618 else if (px <= pt - ((int)ICON_SIZE - 1 - py))
3619 return CLIP_REWIND;
3621 return CLIP_IDLE;
3624 static void handleClipChangeWorkspace(WScreen *scr, XEvent *event)
3626 XEvent ev;
3627 int done, direction, new_ws;
3628 int new_dir;
3629 WDock *clip = scr->clip_icon->dock;
3631 direction = getClipButton(event->xbutton.x, event->xbutton.y);
3633 clip->lclip_button_pushed = direction == CLIP_REWIND;
3634 clip->rclip_button_pushed = direction == CLIP_FORWARD;
3636 wClipIconPaint(scr->clip_icon);
3637 done = 0;
3638 while (!done) {
3639 WMMaskEvent(dpy, ExposureMask | ButtonMotionMask | ButtonReleaseMask | ButtonPressMask, &ev);
3640 switch (ev.type) {
3641 case Expose:
3642 WMHandleEvent(&ev);
3643 break;
3645 case MotionNotify:
3646 new_dir = getClipButton(ev.xmotion.x, ev.xmotion.y);
3647 if (new_dir != direction) {
3648 direction = new_dir;
3649 clip->lclip_button_pushed = direction == CLIP_REWIND;
3650 clip->rclip_button_pushed = direction == CLIP_FORWARD;
3651 wClipIconPaint(scr->clip_icon);
3653 break;
3655 case ButtonPress:
3656 break;
3658 case ButtonRelease:
3659 if (ev.xbutton.button == event->xbutton.button)
3660 done = 1;
3664 clip->lclip_button_pushed = 0;
3665 clip->rclip_button_pushed = 0;
3667 new_ws = wPreferences.ws_advance || (event->xbutton.state & ControlMask);
3669 if (direction == CLIP_FORWARD) {
3670 if (scr->current_workspace < scr->workspace_count - 1)
3671 wWorkspaceChange(scr, scr->current_workspace + 1);
3672 else if (new_ws && scr->current_workspace < MAX_WORKSPACES - 1)
3673 wWorkspaceChange(scr, scr->current_workspace + 1);
3674 else if (wPreferences.ws_cycle)
3675 wWorkspaceChange(scr, 0);
3676 } else if (direction == CLIP_REWIND) {
3677 if (scr->current_workspace > 0)
3678 wWorkspaceChange(scr, scr->current_workspace - 1);
3679 else if (scr->current_workspace == 0 && wPreferences.ws_cycle)
3680 wWorkspaceChange(scr, scr->workspace_count - 1);
3683 wClipIconPaint(scr->clip_icon);
3686 static void iconMouseDown(WObjDescriptor *desc, XEvent *event)
3688 WAppIcon *aicon = desc->parent;
3689 WDock *dock = aicon->dock;
3690 WScreen *scr = aicon->icon->core->screen_ptr;
3692 if (aicon->editing || WCHECK_STATE(WSTATE_MODAL))
3693 return;
3695 scr->last_dock = dock;
3697 if (dock->menu->flags.mapped)
3698 wMenuUnmap(dock->menu);
3700 if (IsDoubleClick(scr, event)) {
3701 /* double-click was not in the main clip icon */
3702 if (dock->type != WM_CLIP || aicon->xindex != 0 || aicon->yindex != 0
3703 || getClipButton(event->xbutton.x, event->xbutton.y) == CLIP_IDLE) {
3704 iconDblClick(desc, event);
3705 return;
3709 if (event->xbutton.button == Button1) {
3710 if (event->xbutton.state & MOD_MASK)
3711 wDockLower(dock);
3712 else
3713 wDockRaise(dock);
3715 if ((event->xbutton.state & ShiftMask) && aicon != scr->clip_icon && dock->type != WM_DOCK) {
3716 wIconSelect(aicon->icon);
3717 return;
3720 if (aicon->yindex == 0 && aicon->xindex == 0) {
3721 if (getClipButton(event->xbutton.x, event->xbutton.y) != CLIP_IDLE
3722 && dock->type == WM_CLIP)
3723 handleClipChangeWorkspace(scr, event);
3724 else
3725 handleDockMove(dock, aicon, event);
3726 } else {
3727 Bool hasMoved = handleIconMove(dock, aicon, event);
3728 if (wPreferences.single_click && !hasMoved)
3729 iconDblClick(desc, event);
3731 } else if (event->xbutton.button == Button2 && dock->type == WM_CLIP && aicon == scr->clip_icon) {
3732 if (!scr->clip_ws_menu) {
3733 scr->clip_ws_menu = wWorkspaceMenuMake(scr, False);
3735 if (scr->clip_ws_menu) {
3736 WMenu *wsMenu = scr->clip_ws_menu;
3737 int xpos;
3739 wWorkspaceMenuUpdate(scr, wsMenu);
3741 xpos = event->xbutton.x_root - wsMenu->frame->core->width / 2 - 1;
3742 if (xpos < 0) {
3743 xpos = 0;
3744 } else if (xpos + wsMenu->frame->core->width > scr->scr_width - 2) {
3745 xpos = scr->scr_width - wsMenu->frame->core->width - 4;
3747 wMenuMapAt(wsMenu, xpos, event->xbutton.y_root + 2, False);
3749 desc = &wsMenu->menu->descriptor;
3750 event->xany.send_event = True;
3751 (*desc->handle_mousedown) (desc, event);
3753 } else if (event->xbutton.button == Button2 && dock->type == WM_CLIP &&
3754 (event->xbutton.state & ShiftMask) && aicon != scr->clip_icon) {
3755 wClipMakeIconOmnipresent(aicon, !aicon->omnipresent);
3756 } else if (event->xbutton.button == Button3) {
3757 if (event->xbutton.send_event &&
3758 XGrabPointer(dpy, aicon->icon->core->window, True, ButtonMotionMask
3759 | ButtonReleaseMask | ButtonPressMask, GrabModeAsync,
3760 GrabModeAsync, None, None, CurrentTime) != GrabSuccess) {
3761 wwarning("pointer grab failed for dockicon menu");
3762 return;
3765 openDockMenu(dock, aicon, event);
3766 } else if (event->xbutton.button == Button2) {
3767 WAppIcon *btn = desc->parent;
3769 if (!btn->launching && (!btn->running || (event->xbutton.state & ControlMask)))
3770 launchDockedApplication(btn, True);
3774 static void clipEnterNotify(WObjDescriptor *desc, XEvent *event)
3776 WAppIcon *btn = (WAppIcon *) desc->parent;
3777 WDock *dock;
3778 WScreen *scr;
3780 assert(event->type == EnterNotify);
3782 if (desc->parent_type != WCLASS_DOCK_ICON)
3783 return;
3785 scr = btn->icon->core->screen_ptr;
3786 dock = btn->dock;
3788 if (!dock || dock->type != WM_CLIP)
3789 return;
3791 /* The auto raise/lower code */
3792 if (dock->auto_lower_magic) {
3793 WMDeleteTimerHandler(dock->auto_lower_magic);
3794 dock->auto_lower_magic = NULL;
3796 if (dock->auto_raise_lower && !dock->auto_raise_magic)
3797 dock->auto_raise_magic = WMAddTimerHandler(wPreferences.clip_auto_raise_delay, clipAutoRaise, (void *)dock);
3799 /* The auto expand/collapse code */
3800 if (dock->auto_collapse_magic) {
3801 WMDeleteTimerHandler(dock->auto_collapse_magic);
3802 dock->auto_collapse_magic = NULL;
3804 if (dock->auto_collapse && !dock->auto_expand_magic)
3805 dock->auto_expand_magic = WMAddTimerHandler(wPreferences.clip_auto_expand_delay, clipAutoExpand, (void *)dock);
3808 static void clipLeave(WDock *dock)
3810 XEvent event;
3811 WObjDescriptor *desc = NULL;
3813 if (!dock || dock->type != WM_CLIP)
3814 return;
3816 if (XCheckTypedEvent(dpy, EnterNotify, &event) != False) {
3817 if (XFindContext(dpy, event.xcrossing.window, wWinContext,
3818 (XPointer *) & desc) != XCNOENT
3819 && desc && desc->parent_type == WCLASS_DOCK_ICON
3820 && ((WAppIcon *) desc->parent)->dock && ((WAppIcon *) desc->parent)->dock->type == WM_CLIP) {
3821 /* We didn't left the Clip yet */
3822 XPutBackEvent(dpy, &event);
3823 return;
3826 XPutBackEvent(dpy, &event);
3827 } else {
3828 /* We entered a withdrawn window, so we're still in Clip */
3829 return;
3832 if (dock->auto_raise_magic) {
3833 WMDeleteTimerHandler(dock->auto_raise_magic);
3834 dock->auto_raise_magic = NULL;
3836 if (dock->auto_raise_lower && !dock->auto_lower_magic)
3837 dock->auto_lower_magic = WMAddTimerHandler(wPreferences.clip_auto_lower_delay, clipAutoLower, (void *)dock);
3839 if (dock->auto_expand_magic) {
3840 WMDeleteTimerHandler(dock->auto_expand_magic);
3841 dock->auto_expand_magic = NULL;
3843 if (dock->auto_collapse && !dock->auto_collapse_magic)
3844 dock->auto_collapse_magic = WMAddTimerHandler(wPreferences.clip_auto_collapse_delay, clipAutoCollapse, (void *)dock);
3847 static void clipLeaveNotify(WObjDescriptor *desc, XEvent *event)
3849 WAppIcon *btn = (WAppIcon *) desc->parent;
3851 assert(event->type == LeaveNotify);
3853 if (desc->parent_type != WCLASS_DOCK_ICON)
3854 return;
3856 clipLeave(btn->dock);
3859 static void clipAutoCollapse(void *cdata)
3861 WDock *dock = (WDock *) cdata;
3863 if (dock->type != WM_CLIP)
3864 return;
3866 if (dock->auto_collapse) {
3867 dock->collapsed = 1;
3868 wDockHideIcons(dock);
3870 dock->auto_collapse_magic = NULL;
3873 static void clipAutoExpand(void *cdata)
3875 WDock *dock = (WDock *) cdata;
3877 if (dock->type != WM_CLIP)
3878 return;
3880 if (dock->auto_collapse) {
3881 dock->collapsed = 0;
3882 wDockShowIcons(dock);
3884 dock->auto_expand_magic = NULL;
3887 static void clipAutoLower(void *cdata)
3889 WDock *dock = (WDock *) cdata;
3891 if (dock->type != WM_CLIP)
3892 return;
3894 if (dock->auto_raise_lower)
3895 wDockLower(dock);
3897 dock->auto_lower_magic = NULL;
3900 static void clipAutoRaise(void *cdata)
3902 WDock *dock = (WDock *) cdata;
3904 if (dock->type != WM_CLIP)
3905 return;
3907 if (dock->auto_raise_lower)
3908 wDockRaise(dock);
3910 dock->auto_raise_magic = NULL;
3913 static Bool iconCanBeOmnipresent(WAppIcon *aicon)
3915 WScreen *scr = aicon->icon->core->screen_ptr;
3916 WDock *clip;
3917 WAppIcon *btn;
3918 int i, j;
3920 for (i = 0; i < scr->workspace_count; i++) {
3921 clip = scr->workspaces[i]->clip;
3923 if (clip == aicon->dock)
3924 continue;
3926 if (clip->icon_count + scr->global_icon_count >= clip->max_icons)
3927 return False; /* Clip is full in some workspace */
3929 for (j = 0; j < clip->max_icons; j++) {
3930 btn = clip->icon_array[j];
3931 if (btn && btn->xindex == aicon->xindex && btn->yindex == aicon->yindex)
3932 return False;
3936 return True;
3939 int wClipMakeIconOmnipresent(WAppIcon *aicon, int omnipresent)
3941 WScreen *scr = aicon->icon->core->screen_ptr;
3942 WAppIconChain *new_entry, *tmp, *tmp1;
3943 int status = WO_SUCCESS;
3945 if ((scr->dock && aicon->dock == scr->dock) || aicon == scr->clip_icon)
3946 return WO_NOT_APPLICABLE;
3948 if (aicon->omnipresent == omnipresent)
3949 return WO_SUCCESS;
3951 if (omnipresent) {
3952 if (iconCanBeOmnipresent(aicon)) {
3953 aicon->omnipresent = 1;
3954 new_entry = wmalloc(sizeof(WAppIconChain));
3955 new_entry->aicon = aicon;
3956 new_entry->next = scr->global_icons;
3957 scr->global_icons = new_entry;
3958 scr->global_icon_count++;
3959 } else {
3960 aicon->omnipresent = 0;
3961 status = WO_FAILED;
3963 } else {
3964 aicon->omnipresent = 0;
3965 if (aicon == scr->global_icons->aicon) {
3966 tmp = scr->global_icons->next;
3967 wfree(scr->global_icons);
3968 scr->global_icons = tmp;
3969 scr->global_icon_count--;
3970 } else {
3971 tmp = scr->global_icons;
3972 while (tmp->next) {
3973 if (tmp->next->aicon == aicon) {
3974 tmp1 = tmp->next->next;
3975 wfree(tmp->next);
3976 tmp->next = tmp1;
3977 scr->global_icon_count--;
3978 break;
3980 tmp = tmp->next;
3985 wAppIconPaint(aicon);
3987 return status;