WindowMaker: appicon.h clean code
[wmaker-crm.git] / src / dock.c
blob88ea6eb5be8d3a1a2d73f28630c6d7980c5b7c8e
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 "dialog.h"
47 #include "funcs.h"
48 #include "properties.h"
49 #include "menu.h"
50 #include "client.h"
51 #include "defaults.h"
52 #include "workspace.h"
53 #include "framewin.h"
54 #include "superfluous.h"
55 #include "xinerama.h"
57 /**** Local variables ****/
58 #define CLIP_REWIND 1
59 #define CLIP_IDLE 0
60 #define CLIP_FORWARD 2
62 /**** Global variables ****/
64 /* in dockedapp.c */
65 extern void DestroyDockAppSettingsPanel();
66 extern void ShowDockAppSettingsPanel(WAppIcon * aicon);
67 extern Cursor wCursor[WCUR_LAST];
68 extern WPreferences wPreferences;
69 extern XContext wWinContext;
70 extern void appIconMouseDown(WObjDescriptor *desc, XEvent *event);
72 #define MOD_MASK wPreferences.modifier_mask
73 #define ICON_SIZE wPreferences.icon_size
75 /***** Local variables ****/
77 static WMPropList *dCommand = NULL;
78 static WMPropList *dPasteCommand = NULL;
79 #ifdef XDND /* XXX was OFFIX */
80 static WMPropList *dDropCommand = NULL;
81 #endif
82 static WMPropList *dAutoLaunch, *dLock;
83 static WMPropList *dName, *dForced, *dBuggyApplication, *dYes, *dNo;
84 static WMPropList *dHost, *dDock, *dClip;
85 static WMPropList *dAutoAttractIcons;
87 static WMPropList *dPosition, *dApplications, *dLowered, *dCollapsed;
89 static WMPropList *dAutoCollapse, *dAutoRaiseLower, *dOmnipresent;
91 static void dockIconPaint(WAppIcon *btn);
93 static void iconMouseDown(WObjDescriptor *desc, XEvent *event);
95 static pid_t execCommand(WAppIcon *btn, char *command, WSavedState *state);
97 static void trackDeadProcess(pid_t pid, unsigned char status, WDock *dock);
99 static int getClipButton(int px, int py);
101 static void toggleLowered(WDock *dock);
103 static void toggleCollapsed(WDock *dock);
105 static void clipIconExpose(WObjDescriptor *desc, XEvent *event);
107 static void clipLeave(WDock *dock);
109 static void handleClipChangeWorkspace(WScreen *scr, XEvent *event);
111 static Bool moveIconBetweenDocks(WDock *src, WDock *dest, WAppIcon *icon, int x, int y);
113 static void clipEnterNotify(WObjDescriptor *desc, XEvent *event);
114 static void clipLeaveNotify(WObjDescriptor *desc, XEvent *event);
115 static void clipAutoCollapse(void *cdata);
116 static void clipAutoExpand(void *cdata);
117 static void launchDockedApplication(WAppIcon *btn, Bool withSelection);
119 static void clipAutoLower(void *cdata);
120 static void clipAutoRaise(void *cdata);
121 static void reattachIcon(WDock *dock, WAppIcon *icon, int x, int y);
122 static WAppIcon *mainIconCreate(WScreen *scr, int type);
124 static void make_keys(void)
126 if (dCommand != NULL)
127 return;
129 dCommand = WMRetainPropList(WMCreatePLString("Command"));
130 dPasteCommand = WMRetainPropList(WMCreatePLString("PasteCommand"));
131 #ifdef XDND
132 dDropCommand = WMRetainPropList(WMCreatePLString("DropCommand"));
133 #endif
134 dLock = WMRetainPropList(WMCreatePLString("Lock"));
135 dAutoLaunch = WMRetainPropList(WMCreatePLString("AutoLaunch"));
136 dName = WMRetainPropList(WMCreatePLString("Name"));
137 dForced = WMRetainPropList(WMCreatePLString("Forced"));
138 dBuggyApplication = WMRetainPropList(WMCreatePLString("BuggyApplication"));
139 dYes = WMRetainPropList(WMCreatePLString("Yes"));
140 dNo = WMRetainPropList(WMCreatePLString("No"));
141 dHost = WMRetainPropList(WMCreatePLString("Host"));
143 dPosition = WMCreatePLString("Position");
144 dApplications = WMCreatePLString("Applications");
145 dLowered = WMCreatePLString("Lowered");
146 dCollapsed = WMCreatePLString("Collapsed");
147 dAutoCollapse = WMCreatePLString("AutoCollapse");
148 dAutoRaiseLower = WMCreatePLString("AutoRaiseLower");
149 dAutoAttractIcons = WMCreatePLString("AutoAttractIcons");
151 dOmnipresent = WMCreatePLString("Omnipresent");
153 dDock = WMCreatePLString("Dock");
154 dClip = WMCreatePLString("Clip");
157 static void renameCallback(WMenu *menu, WMenuEntry *entry)
159 WDock *dock = entry->clientdata;
160 char buffer[128];
161 int wspace;
162 char *name;
164 assert(entry->clientdata != NULL);
166 wspace = dock->screen_ptr->current_workspace;
168 name = wstrdup(dock->screen_ptr->workspaces[wspace]->name);
170 snprintf(buffer, sizeof(buffer), _("Type the name for workspace %i:"), wspace + 1);
171 if (wInputDialog(dock->screen_ptr, _("Rename Workspace"), buffer, &name)) {
172 wWorkspaceRename(dock->screen_ptr, wspace, name);
174 if (name) {
175 wfree(name);
179 static void toggleLoweredCallback(WMenu *menu, WMenuEntry *entry)
181 assert(entry->clientdata != NULL);
183 toggleLowered(entry->clientdata);
185 entry->flags.indicator_on = !(((WDock *) entry->clientdata)->lowered);
187 wMenuPaint(menu);
190 static int matchWindow(const void *item, const void *cdata)
192 return (((WFakeGroupLeader *) item)->leader == (Window) cdata);
195 static void killCallback(WMenu *menu, WMenuEntry *entry)
197 WScreen *scr = menu->menu->screen_ptr;
198 WAppIcon *icon;
199 WFakeGroupLeader *fPtr;
200 char *buffer, *shortname, **argv;
201 char *basename(const char *shortname);
202 int argc;
204 if (!WCHECK_STATE(WSTATE_NORMAL))
205 return;
207 assert(entry->clientdata != NULL);
209 icon = (WAppIcon *) entry->clientdata;
211 icon->editing = 1;
213 WCHANGE_STATE(WSTATE_MODAL);
215 /* strip away dir names */
216 shortname = basename(icon->command);
217 /* separate out command options */
218 wtokensplit(shortname, &argv, &argc);
220 buffer = wstrconcat(argv[0],
221 _(" will be forcibly closed.\n"
222 "Any unsaved changes will be lost.\n" "Please confirm."));
224 if (icon->icon && icon->icon->owner) {
225 fPtr = icon->icon->owner->fake_group;
226 } else {
227 /* is this really necessary? can we kill a non-running dock icon? */
228 Window win = icon->main_window;
229 int index;
231 index = WMFindInArray(scr->fakeGroupLeaders, matchWindow, (void *)win);
232 if (index != WANotFound)
233 fPtr = WMGetFromArray(scr->fakeGroupLeaders, index);
234 else
235 fPtr = NULL;
238 if (wPreferences.dont_confirm_kill
239 || wMessageDialog(menu->frame->screen_ptr, _("Kill Application"),
240 buffer, _("Yes"), _("No"), NULL) == WAPRDefault) {
241 if (fPtr != NULL) {
242 WWindow *wwin, *twin;
244 wwin = scr->focused_window;
245 while (wwin) {
246 twin = wwin->prev;
247 if (wwin->fake_group == fPtr) {
248 wClientKill(wwin);
250 wwin = twin;
252 } else if (icon->icon && icon->icon->owner) {
253 wClientKill(icon->icon->owner);
257 wfree(buffer);
258 wtokenfree(argv, argc);
260 icon->editing = 0;
262 WCHANGE_STATE(WSTATE_NORMAL);
265 /* TODO: replace this function with a member of the dock struct */
266 static int numberOfSelectedIcons(WDock *dock)
268 WAppIcon *aicon;
269 int i, n;
271 n = 0;
272 for (i = 1; i < dock->max_icons; i++) {
273 aicon = dock->icon_array[i];
274 if (aicon && aicon->icon->selected) {
275 n++;
279 return n;
282 static WMArray *getSelected(WDock *dock)
284 WMArray *ret = WMCreateArray(8);
285 WAppIcon *btn;
286 int i;
288 for (i = 1; i < dock->max_icons; i++) {
289 btn = dock->icon_array[i];
290 if (btn && btn->icon->selected) {
291 WMAddToArray(ret, btn);
295 return ret;
298 static void paintClipButtons(WAppIcon *clipIcon, Bool lpushed, Bool rpushed)
300 Window win = clipIcon->icon->core->window;
301 WScreen *scr = clipIcon->icon->core->screen_ptr;
302 XPoint p[4];
303 int pt = CLIP_BUTTON_SIZE * ICON_SIZE / 64;
304 int tp = ICON_SIZE - pt;
305 int as = pt - 15; /* 15 = 5+5+5 */
306 GC gc = scr->draw_gc; /* maybe use WMColorGC() instead here? */
307 WMColor *color;
309 color = scr->clip_title_color[CLIP_NORMAL];
311 XSetForeground(dpy, gc, WMColorPixel(color));
313 if (rpushed) {
314 p[0].x = tp + 1;
315 p[0].y = 1;
316 p[1].x = ICON_SIZE - 2;
317 p[1].y = 1;
318 p[2].x = ICON_SIZE - 2;
319 p[2].y = pt - 1;
320 } else if (lpushed) {
321 p[0].x = 1;
322 p[0].y = tp;
323 p[1].x = pt;
324 p[1].y = ICON_SIZE - 2;
325 p[2].x = 1;
326 p[2].y = ICON_SIZE - 2;
328 if (lpushed || rpushed) {
329 XSetForeground(dpy, scr->draw_gc, scr->white_pixel);
330 XFillPolygon(dpy, win, scr->draw_gc, p, 3, Convex, CoordModeOrigin);
331 XSetForeground(dpy, scr->draw_gc, scr->black_pixel);
334 /* top right arrow */
335 p[0].x = p[3].x = ICON_SIZE - 5 - as;
336 p[0].y = p[3].y = 5;
337 p[1].x = ICON_SIZE - 6;
338 p[1].y = 5;
339 p[2].x = ICON_SIZE - 6;
340 p[2].y = 4 + as;
341 if (rpushed) {
342 XFillPolygon(dpy, win, scr->draw_gc, p, 3, Convex, CoordModeOrigin);
343 XDrawLines(dpy, win, scr->draw_gc, p, 4, CoordModeOrigin);
344 } else {
345 XFillPolygon(dpy, win, gc, p, 3, Convex, CoordModeOrigin);
346 XDrawLines(dpy, win, gc, p, 4, CoordModeOrigin);
349 /* bottom left arrow */
350 p[0].x = p[3].x = 5;
351 p[0].y = p[3].y = ICON_SIZE - 5 - as;
352 p[1].x = 5;
353 p[1].y = ICON_SIZE - 6;
354 p[2].x = 4 + as;
355 p[2].y = ICON_SIZE - 6;
356 if (lpushed) {
357 XFillPolygon(dpy, win, scr->draw_gc, p, 3, Convex, CoordModeOrigin);
358 XDrawLines(dpy, win, scr->draw_gc, p, 4, CoordModeOrigin);
359 } else {
360 XFillPolygon(dpy, win, gc, p, 3, Convex, CoordModeOrigin);
361 XDrawLines(dpy, win, gc, p, 4, CoordModeOrigin);
365 RImage *wClipMakeTile(WScreen *scr, RImage *normalTile)
367 RImage *tile = RCloneImage(normalTile);
368 RColor black;
369 RColor dark;
370 RColor light;
371 int pt, tp;
372 int as;
374 pt = CLIP_BUTTON_SIZE * wPreferences.icon_size / 64;
375 tp = wPreferences.icon_size - 1 - pt;
376 as = pt - 15;
378 black.alpha = 255;
379 black.red = black.green = black.blue = 0;
381 dark.alpha = 0;
382 dark.red = dark.green = dark.blue = 60;
384 light.alpha = 0;
385 light.red = light.green = light.blue = 80;
387 /* top right */
388 ROperateLine(tile, RSubtractOperation, tp, 0, wPreferences.icon_size - 2, pt - 1, &dark);
389 RDrawLine(tile, tp - 1, 0, wPreferences.icon_size - 1, pt + 1, &black);
390 ROperateLine(tile, RAddOperation, tp, 2, wPreferences.icon_size - 3, pt, &light);
392 /* arrow bevel */
393 ROperateLine(tile, RSubtractOperation, ICON_SIZE - 7 - as, 4, ICON_SIZE - 5, 4, &dark);
394 ROperateLine(tile, RSubtractOperation, ICON_SIZE - 6 - as, 5, ICON_SIZE - 5, 6 + as, &dark);
395 ROperateLine(tile, RAddOperation, ICON_SIZE - 5, 4, ICON_SIZE - 5, 6 + as, &light);
397 /* bottom left */
398 ROperateLine(tile, RAddOperation, 2, tp + 2, pt - 2, wPreferences.icon_size - 3, &dark);
399 RDrawLine(tile, 0, tp - 1, pt + 1, wPreferences.icon_size - 1, &black);
400 ROperateLine(tile, RSubtractOperation, 0, tp - 2, pt + 1, wPreferences.icon_size - 2, &light);
402 /* arrow bevel */
403 ROperateLine(tile, RSubtractOperation, 4, ICON_SIZE - 7 - as, 4, ICON_SIZE - 5, &dark);
404 ROperateLine(tile, RSubtractOperation, 5, ICON_SIZE - 6 - as, 6 + as, ICON_SIZE - 5, &dark);
405 ROperateLine(tile, RAddOperation, 4, ICON_SIZE - 5, 6 + as, ICON_SIZE - 5, &light);
407 return tile;
410 static void omnipresentCallback(WMenu *menu, WMenuEntry *entry)
412 WAppIcon *clickedIcon = entry->clientdata;
413 WAppIcon *aicon;
414 WDock *dock;
415 WMArray *selectedIcons;
416 WMArrayIterator iter;
417 int failed;
419 assert(entry->clientdata != NULL);
421 dock = clickedIcon->dock;
423 selectedIcons = getSelected(dock);
425 if (!WMGetArrayItemCount(selectedIcons))
426 WMAddToArray(selectedIcons, clickedIcon);
428 failed = 0;
429 WM_ITERATE_ARRAY(selectedIcons, aicon, iter) {
430 if (wClipMakeIconOmnipresent(aicon, !aicon->omnipresent) == WO_FAILED)
431 failed++;
432 else if (aicon->icon->selected)
433 wIconSelect(aicon->icon);
435 WMFreeArray(selectedIcons);
437 if (failed > 1) {
438 wMessageDialog(dock->screen_ptr, _("Warning"),
439 _("Some icons cannot be made omnipresent. "
440 "Please make sure that no other icon is "
441 "docked in the same positions on the other "
442 "workspaces and the Clip is not full in "
443 "some workspace."), _("OK"), NULL, NULL);
444 } else if (failed == 1) {
445 wMessageDialog(dock->screen_ptr, _("Warning"),
446 _("Icon cannot be made omnipresent. "
447 "Please make sure that no other icon is "
448 "docked in the same position on the other "
449 "workspaces and the Clip is not full in "
450 "some workspace."), _("OK"), NULL, NULL);
454 static void removeIconsCallback(WMenu *menu, WMenuEntry *entry)
456 WAppIcon *clickedIcon = (WAppIcon *) entry->clientdata;
457 WDock *dock;
458 WAppIcon *aicon;
459 WMArray *selectedIcons;
460 int keepit;
461 WMArrayIterator it;
463 assert(clickedIcon != NULL);
465 dock = clickedIcon->dock;
467 selectedIcons = getSelected(dock);
469 if (WMGetArrayItemCount(selectedIcons)) {
470 if (wMessageDialog(dock->screen_ptr, _("Workspace Clip"),
471 _("All selected icons will be removed!"),
472 _("OK"), _("Cancel"), NULL) != WAPRDefault) {
473 WMFreeArray(selectedIcons);
474 return;
476 } else {
477 if (clickedIcon->xindex == 0 && clickedIcon->yindex == 0) {
478 WMFreeArray(selectedIcons);
479 return;
481 WMAddToArray(selectedIcons, clickedIcon);
484 WM_ITERATE_ARRAY(selectedIcons, aicon, it) {
485 keepit = aicon->running && wApplicationOf(aicon->main_window);
486 wDockDetach(dock, aicon);
487 if (keepit) {
488 /* XXX: can: aicon->icon == NULL ? */
489 PlaceIcon(dock->screen_ptr, &aicon->x_pos, &aicon->y_pos,
490 wGetHeadForWindow(aicon->icon->owner));
491 XMoveWindow(dpy, aicon->icon->core->window, aicon->x_pos, aicon->y_pos);
492 if (!dock->mapped || dock->collapsed)
493 XMapWindow(dpy, aicon->icon->core->window);
496 WMFreeArray(selectedIcons);
498 if (wPreferences.auto_arrange_icons)
499 wArrangeIcons(dock->screen_ptr, True);
502 static void keepIconsCallback(WMenu *menu, WMenuEntry *entry)
504 WAppIcon *clickedIcon = (WAppIcon *) entry->clientdata;
505 WDock *dock;
506 WAppIcon *aicon;
507 WMArray *selectedIcons;
508 WMArrayIterator it;
510 assert(clickedIcon != NULL);
511 dock = clickedIcon->dock;
513 selectedIcons = getSelected(dock);
515 if (!WMGetArrayItemCount(selectedIcons)
516 && clickedIcon != dock->screen_ptr->clip_icon) {
517 char *command = NULL;
519 if (!clickedIcon->command && !clickedIcon->editing) {
520 clickedIcon->editing = 1;
521 if (wInputDialog(dock->screen_ptr, _("Keep Icon"),
522 _("Type the command used to launch the application"), &command)) {
523 if (command && (command[0] == 0 || (command[0] == '-' && command[1] == 0))) {
524 wfree(command);
525 command = NULL;
527 clickedIcon->command = command;
528 clickedIcon->editing = 0;
529 } else {
530 clickedIcon->editing = 0;
531 if (command)
532 wfree(command);
533 WMFreeArray(selectedIcons);
534 return;
538 WMAddToArray(selectedIcons, clickedIcon);
541 WM_ITERATE_ARRAY(selectedIcons, aicon, it) {
542 if (aicon->icon->selected)
543 wIconSelect(aicon->icon);
544 if (aicon && aicon->attracted && aicon->command) {
545 aicon->attracted = 0;
546 if (aicon->icon->shadowed) {
547 aicon->icon->shadowed = 0;
548 aicon->icon->force_paint = 1;
549 wAppIconPaint(aicon);
552 wAppIconSave(aicon);
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;
564 /*if (!dock->attract_icons)
565 dock->keep_attracted = 0; */
567 entry->flags.indicator_on = dock->attract_icons;
569 wMenuPaint(menu);
572 static void selectCallback(WMenu *menu, WMenuEntry *entry)
574 WAppIcon *icon = (WAppIcon *) entry->clientdata;
576 assert(icon != NULL);
578 wIconSelect(icon->icon);
580 wMenuPaint(menu);
583 static void colectIconsCallback(WMenu *menu, WMenuEntry *entry)
585 WAppIcon *clickedIcon = (WAppIcon *) entry->clientdata;
586 WDock *clip;
587 WAppIcon *aicon;
588 int x, y, x_pos, y_pos;
590 assert(entry->clientdata != NULL);
591 clip = clickedIcon->dock;
593 aicon = clip->screen_ptr->app_icon_list;
595 while (aicon) {
596 if (!aicon->docked && wDockFindFreeSlot(clip, &x, &y)) {
597 x_pos = clip->x_pos + x * ICON_SIZE;
598 y_pos = clip->y_pos + y * ICON_SIZE;
599 if (aicon->x_pos != x_pos || aicon->y_pos != y_pos) {
600 #ifdef ANIMATIONS
601 if (wPreferences.no_animations) {
602 XMoveWindow(dpy, aicon->icon->core->window, x_pos, y_pos);
603 } else {
604 SlideWindow(aicon->icon->core->window,
605 aicon->x_pos, aicon->y_pos, x_pos, y_pos);
607 #else
608 XMoveWindow(dpy, aicon->icon->core->window, x_pos, y_pos);
609 #endif /* ANIMATIONS */
611 aicon->attracted = 1;
612 if (!aicon->icon->shadowed) {
613 aicon->icon->shadowed = 1;
614 aicon->icon->force_paint = 1;
615 /* We don't do an wAppIconPaint() here because it's in
616 * wDockAttachIcon(). -Dan
619 wDockAttachIcon(clip, aicon, x, y);
620 if (clip->collapsed || !clip->mapped)
621 XUnmapWindow(dpy, aicon->icon->core->window);
623 aicon = aicon->next;
627 static void selectIconsCallback(WMenu *menu, WMenuEntry *entry)
629 WAppIcon *clickedIcon = (WAppIcon *) entry->clientdata;
630 WDock *dock;
631 WMArray *selectedIcons;
632 WMArrayIterator iter;
633 WAppIcon *btn;
634 int i;
636 assert(clickedIcon != NULL);
637 dock = clickedIcon->dock;
639 selectedIcons = getSelected(dock);
641 if (!WMGetArrayItemCount(selectedIcons)) {
642 for (i = 1; i < dock->max_icons; i++) {
643 btn = dock->icon_array[i];
644 if (btn && !btn->icon->selected) {
645 wIconSelect(btn->icon);
648 } else {
649 WM_ITERATE_ARRAY(selectedIcons, btn, iter) {
650 wIconSelect(btn->icon);
653 WMFreeArray(selectedIcons);
655 wMenuPaint(menu);
658 static void toggleCollapsedCallback(WMenu *menu, WMenuEntry *entry)
660 assert(entry->clientdata != NULL);
662 toggleCollapsed(entry->clientdata);
664 entry->flags.indicator_on = ((WDock *) entry->clientdata)->collapsed;
666 wMenuPaint(menu);
669 static void toggleAutoCollapseCallback(WMenu *menu, WMenuEntry *entry)
671 WDock *dock;
672 assert(entry->clientdata != NULL);
674 dock = (WDock *) entry->clientdata;
676 dock->auto_collapse = !dock->auto_collapse;
678 entry->flags.indicator_on = ((WDock *) entry->clientdata)->auto_collapse;
680 wMenuPaint(menu);
683 static void toggleAutoRaiseLowerCallback(WMenu *menu, WMenuEntry *entry)
685 WDock *dock;
686 assert(entry->clientdata != NULL);
688 dock = (WDock *) entry->clientdata;
690 dock->auto_raise_lower = !dock->auto_raise_lower;
692 entry->flags.indicator_on = ((WDock *) entry->clientdata)->auto_raise_lower;
694 wMenuPaint(menu);
697 static void launchCallback(WMenu *menu, WMenuEntry *entry)
699 WAppIcon *btn = (WAppIcon *) entry->clientdata;
701 launchDockedApplication(btn, False);
704 static void settingsCallback(WMenu *menu, WMenuEntry *entry)
706 WAppIcon *btn = (WAppIcon *) entry->clientdata;
708 if (btn->editing)
709 return;
710 ShowDockAppSettingsPanel(btn);
713 static void hideCallback(WMenu *menu, WMenuEntry *entry)
715 WApplication *wapp;
716 WAppIcon *btn = (WAppIcon *) entry->clientdata;
718 wapp = wApplicationOf(btn->icon->owner->main_window);
720 if (wapp->flags.hidden) {
721 wWorkspaceChange(btn->icon->core->screen_ptr, wapp->last_workspace);
722 wUnhideApplication(wapp, False, False);
723 } else {
724 wHideApplication(wapp);
728 static void unhideHereCallback(WMenu *menu, WMenuEntry *entry)
730 WApplication *wapp;
731 WAppIcon *btn = (WAppIcon *) entry->clientdata;
733 wapp = wApplicationOf(btn->icon->owner->main_window);
735 wUnhideApplication(wapp, False, True);
738 static WAppIcon *mainIconCreate(WScreen *scr, int type)
740 WAppIcon *btn;
741 int x_pos;
743 if (type == WM_CLIP) {
744 if (scr->clip_icon)
745 return scr->clip_icon;
746 btn = wAppIconCreateForDock(scr, NULL, "Logo", "WMClip", TILE_CLIP);
747 btn->icon->core->descriptor.handle_expose = clipIconExpose;
748 btn->icon->core->descriptor.handle_enternotify = clipEnterNotify;
749 btn->icon->core->descriptor.handle_leavenotify = clipLeaveNotify;
750 /*x_pos = scr->scr_width - ICON_SIZE*2 - DOCK_EXTRA_SPACE; */
751 x_pos = 0;
752 } else {
753 btn = wAppIconCreateForDock(scr, NULL, "Logo", "WMDock", TILE_NORMAL);
754 x_pos = scr->scr_width - ICON_SIZE - DOCK_EXTRA_SPACE;
757 btn->xindex = 0;
758 btn->yindex = 0;
760 btn->icon->core->descriptor.handle_mousedown = iconMouseDown;
761 btn->icon->core->descriptor.parent_type = WCLASS_DOCK_ICON;
762 btn->icon->core->descriptor.parent = btn;
763 /*ChangeStackingLevel(btn->icon->core, WMDockLevel); */
764 XMapWindow(dpy, btn->icon->core->window);
765 btn->x_pos = x_pos;
766 btn->y_pos = 0;
767 btn->docked = 1;
768 if (type == WM_CLIP)
769 scr->clip_icon = btn;
771 return btn;
774 static void switchWSCommand(WMenu *menu, WMenuEntry *entry)
776 WAppIcon *btn, *icon = (WAppIcon *) entry->clientdata;
777 WScreen *scr = icon->icon->core->screen_ptr;
778 WDock *src, *dest;
779 WMArray *selectedIcons;
780 int x, y;
782 if (entry->order == scr->current_workspace)
783 return;
784 src = icon->dock;
785 dest = scr->workspaces[entry->order]->clip;
787 selectedIcons = getSelected(src);
789 if (WMGetArrayItemCount(selectedIcons)) {
790 WMArrayIterator iter;
792 WM_ITERATE_ARRAY(selectedIcons, btn, iter) {
793 if (wDockFindFreeSlot(dest, &x, &y)) {
794 moveIconBetweenDocks(src, dest, btn, x, y);
795 XUnmapWindow(dpy, btn->icon->core->window);
798 } else if (icon != scr->clip_icon) {
799 if (wDockFindFreeSlot(dest, &x, &y)) {
800 moveIconBetweenDocks(src, dest, icon, x, y);
801 XUnmapWindow(dpy, icon->icon->core->window);
804 WMFreeArray(selectedIcons);
807 static void launchDockedApplication(WAppIcon *btn, Bool withSelection)
809 WScreen *scr = btn->icon->core->screen_ptr;
811 if (!btn->launching &&
812 ((!withSelection && btn->command != NULL) || (withSelection && btn->paste_command != NULL))) {
813 if (!btn->forced_dock) {
814 btn->relaunching = btn->running;
815 btn->running = 1;
817 if (btn->wm_instance || btn->wm_class) {
818 WWindowAttributes attr;
819 memset(&attr, 0, sizeof(WWindowAttributes));
820 wDefaultFillAttributes(scr, btn->wm_instance, btn->wm_class, &attr, NULL, True);
822 if (!attr.no_appicon && !btn->buggy_app)
823 btn->launching = 1;
824 else
825 btn->running = 0;
827 btn->drop_launch = 0;
828 btn->paste_launch = withSelection;
829 scr->last_dock = btn->dock;
830 btn->pid = execCommand(btn, (withSelection ? btn->paste_command : btn->command), NULL);
831 if (btn->pid > 0) {
832 if (btn->buggy_app) {
833 /* give feedback that the app was launched */
834 btn->launching = 1;
835 dockIconPaint(btn);
836 btn->launching = 0;
837 WMAddTimerHandler(200, (WMCallback *) dockIconPaint, btn);
838 } else {
839 dockIconPaint(btn);
841 } else {
842 wwarning(_("could not launch application %s"), btn->command);
843 btn->launching = 0;
844 if (!btn->relaunching) {
845 btn->running = 0;
851 static void updateWorkspaceMenu(WMenu *menu, WAppIcon *icon)
853 WScreen *scr = menu->frame->screen_ptr;
854 char title[MAX_WORKSPACENAME_WIDTH + 1];
855 int i;
857 if (!menu || !icon)
858 return;
860 for (i = 0; i < scr->workspace_count; i++) {
861 if (i < menu->entry_no) {
862 if (strcmp(menu->entries[i]->text, scr->workspaces[i]->name) != 0) {
863 wfree(menu->entries[i]->text);
864 strcpy(title, scr->workspaces[i]->name);
865 menu->entries[i]->text = wstrdup(title);
866 menu->flags.realized = 0;
868 menu->entries[i]->clientdata = (void *)icon;
869 } else {
870 strcpy(title, scr->workspaces[i]->name);
872 wMenuAddCallback(menu, title, switchWSCommand, (void *)icon);
874 menu->flags.realized = 0;
876 if (i == scr->current_workspace) {
877 wMenuSetEnabled(menu, i, False);
878 } else {
879 wMenuSetEnabled(menu, i, True);
883 if (!menu->flags.realized)
884 wMenuRealize(menu);
887 static WMenu *makeWorkspaceMenu(WScreen *scr)
889 WMenu *menu;
891 menu = wMenuCreate(scr, NULL, False);
892 if (!menu)
893 wwarning(_("could not create workspace submenu for Clip menu"));
895 wMenuAddCallback(menu, "", switchWSCommand, (void *)scr->clip_icon);
897 menu->flags.realized = 0;
898 wMenuRealize(menu);
900 return menu;
903 static void updateClipOptionsMenu(WMenu *menu, WDock *dock)
905 WMenuEntry *entry;
906 int index = 0;
908 if (!menu || !dock)
909 return;
911 /* keep on top */
912 entry = menu->entries[index];
913 entry->flags.indicator_on = !dock->lowered;
914 entry->clientdata = dock;
916 /* collapsed */
917 entry = menu->entries[++index];
918 entry->flags.indicator_on = dock->collapsed;
919 entry->clientdata = dock;
921 /* auto-collapse */
922 entry = menu->entries[++index];
923 entry->flags.indicator_on = dock->auto_collapse;
924 entry->clientdata = dock;
926 /* auto-raise/lower */
927 entry = menu->entries[++index];
928 entry->flags.indicator_on = dock->auto_raise_lower;
929 entry->clientdata = dock;
930 wMenuSetEnabled(menu, index, dock->lowered);
932 /* attract icons */
933 entry = menu->entries[++index];
934 entry->flags.indicator_on = dock->attract_icons;
935 entry->clientdata = dock;
937 menu->flags.realized = 0;
938 wMenuRealize(menu);
941 static WMenu *makeClipOptionsMenu(WScreen *scr)
943 WMenu *menu;
944 WMenuEntry *entry;
946 menu = wMenuCreate(scr, NULL, False);
947 if (!menu) {
948 wwarning(_("could not create options submenu for Clip menu"));
949 return NULL;
952 entry = wMenuAddCallback(menu, _("Keep on Top"), toggleLoweredCallback, NULL);
953 entry->flags.indicator = 1;
954 entry->flags.indicator_on = 1;
955 entry->flags.indicator_type = MI_CHECK;
957 entry = wMenuAddCallback(menu, _("Collapsed"), toggleCollapsedCallback, NULL);
958 entry->flags.indicator = 1;
959 entry->flags.indicator_on = 1;
960 entry->flags.indicator_type = MI_CHECK;
962 entry = wMenuAddCallback(menu, _("Autocollapse"), toggleAutoCollapseCallback, NULL);
963 entry->flags.indicator = 1;
964 entry->flags.indicator_on = 1;
965 entry->flags.indicator_type = MI_CHECK;
967 entry = wMenuAddCallback(menu, _("Autoraise"), toggleAutoRaiseLowerCallback, NULL);
968 entry->flags.indicator = 1;
969 entry->flags.indicator_on = 1;
970 entry->flags.indicator_type = MI_CHECK;
972 entry = wMenuAddCallback(menu, _("Autoattract Icons"), toggleAutoAttractCallback, NULL);
973 entry->flags.indicator = 1;
974 entry->flags.indicator_on = 1;
975 entry->flags.indicator_type = MI_CHECK;
977 menu->flags.realized = 0;
978 wMenuRealize(menu);
980 return menu;
983 static WMenu *dockMenuCreate(WScreen *scr, int type)
985 WMenu *menu;
986 WMenuEntry *entry;
988 if (type == WM_CLIP && scr->clip_menu)
989 return scr->clip_menu;
991 menu = wMenuCreate(scr, NULL, False);
992 if (type != WM_CLIP) {
993 entry = wMenuAddCallback(menu, _("Keep on Top"), toggleLoweredCallback, NULL);
994 entry->flags.indicator = 1;
995 entry->flags.indicator_on = 1;
996 entry->flags.indicator_type = MI_CHECK;
997 } else {
998 entry = wMenuAddCallback(menu, _("Clip Options"), NULL, NULL);
999 scr->clip_options = makeClipOptionsMenu(scr);
1000 if (scr->clip_options)
1001 wMenuEntrySetCascade(menu, entry, scr->clip_options);
1003 entry = wMenuAddCallback(menu, _("Rename Workspace"), renameCallback, NULL);
1004 wfree(entry->text);
1005 entry->text = _("Rename Workspace");
1007 entry = wMenuAddCallback(menu, _("Selected"), selectCallback, NULL);
1008 entry->flags.indicator = 1;
1009 entry->flags.indicator_on = 1;
1010 entry->flags.indicator_type = MI_CHECK;
1012 entry = wMenuAddCallback(menu, _("Select All Icons"), selectIconsCallback, NULL);
1013 wfree(entry->text);
1014 entry->text = _("Select All Icons");
1016 entry = wMenuAddCallback(menu, _("Keep Icon"), keepIconsCallback, NULL);
1017 wfree(entry->text);
1018 entry->text = _("Keep Icon");
1020 entry = wMenuAddCallback(menu, _("Move Icon To"), NULL, NULL);
1021 wfree(entry->text);
1022 entry->text = _("Move Icon To");
1023 scr->clip_submenu = makeWorkspaceMenu(scr);
1024 if (scr->clip_submenu)
1025 wMenuEntrySetCascade(menu, entry, scr->clip_submenu);
1027 entry = wMenuAddCallback(menu, _("Remove Icon"), removeIconsCallback, NULL);
1028 wfree(entry->text);
1029 entry->text = _("Remove Icon");
1031 wMenuAddCallback(menu, _("Attract Icons"), colectIconsCallback, NULL);
1034 wMenuAddCallback(menu, _("Launch"), launchCallback, NULL);
1036 wMenuAddCallback(menu, _("Unhide Here"), unhideHereCallback, NULL);
1038 entry = wMenuAddCallback(menu, _("Hide"), hideCallback, NULL);
1039 wfree(entry->text);
1040 entry->text = _("Hide");
1042 wMenuAddCallback(menu, _("Settings..."), settingsCallback, NULL);
1044 wMenuAddCallback(menu, _("Kill"), killCallback, NULL);
1046 if (type == WM_CLIP)
1047 scr->clip_menu = menu;
1049 return menu;
1052 WDock *wDockCreate(WScreen *scr, int type)
1054 WDock *dock;
1055 WAppIcon *btn;
1057 make_keys();
1059 dock = wmalloc(sizeof(WDock));
1060 memset(dock, 0, sizeof(WDock));
1062 dock->max_icons = DOCK_MAX_ICONS;
1064 dock->icon_array = wmalloc(sizeof(WAppIcon *) * dock->max_icons);
1065 memset(dock->icon_array, 0, sizeof(WAppIcon *) * dock->max_icons);
1067 btn = mainIconCreate(scr, type);
1069 btn->dock = dock;
1071 dock->x_pos = btn->x_pos;
1072 dock->y_pos = btn->y_pos;
1073 dock->screen_ptr = scr;
1074 dock->type = type;
1075 dock->icon_count = 1;
1076 dock->on_right_side = 1;
1077 dock->collapsed = 0;
1078 dock->auto_collapse = 0;
1079 dock->auto_collapse_magic = NULL;
1080 dock->auto_raise_lower = 0;
1081 dock->auto_lower_magic = NULL;
1082 dock->auto_raise_magic = NULL;
1083 dock->attract_icons = 0;
1084 dock->lowered = 1;
1085 dock->icon_array[0] = btn;
1086 wRaiseFrame(btn->icon->core);
1087 XMoveWindow(dpy, btn->icon->core->window, btn->x_pos, btn->y_pos);
1089 /* create dock menu */
1090 dock->menu = dockMenuCreate(scr, type);
1092 return dock;
1095 void wDockDestroy(WDock *dock)
1097 int i;
1098 WAppIcon *aicon;
1100 for (i = (dock->type == WM_CLIP) ? 1 : 0; i < dock->max_icons; i++) {
1101 aicon = dock->icon_array[i];
1102 if (aicon) {
1103 int keepit = aicon->running && wApplicationOf(aicon->main_window);
1104 wDockDetach(dock, aicon);
1105 if (keepit) {
1106 /* XXX: can: aicon->icon == NULL ? */
1107 PlaceIcon(dock->screen_ptr, &aicon->x_pos, &aicon->y_pos,
1108 wGetHeadForWindow(aicon->icon->owner));
1109 XMoveWindow(dpy, aicon->icon->core->window, aicon->x_pos, aicon->y_pos);
1110 if (!dock->mapped || dock->collapsed)
1111 XMapWindow(dpy, aicon->icon->core->window);
1115 if (wPreferences.auto_arrange_icons)
1116 wArrangeIcons(dock->screen_ptr, True);
1117 wfree(dock->icon_array);
1118 if (dock->menu && dock->type != WM_CLIP)
1119 wMenuDestroy(dock->menu, True);
1120 if (dock->screen_ptr->last_dock == dock)
1121 dock->screen_ptr->last_dock = NULL;
1122 wfree(dock);
1125 void wClipIconPaint(WAppIcon *aicon)
1127 WScreen *scr = aicon->icon->core->screen_ptr;
1128 WWorkspace *workspace = scr->workspaces[scr->current_workspace];
1129 WMColor *color;
1130 Window win = aicon->icon->core->window;
1131 int length, nlength;
1132 char *ws_name, ws_number[10];
1133 int ty, tx;
1135 wIconPaint(aicon->icon);
1137 length = strlen(workspace->name);
1138 ws_name = wmalloc(length + 1);
1139 snprintf(ws_name, length + 1, "%s", workspace->name);
1140 snprintf(ws_number, sizeof(ws_number), "%i", scr->current_workspace + 1);
1141 nlength = strlen(ws_number);
1143 if (!workspace->clip->collapsed)
1144 color = scr->clip_title_color[CLIP_NORMAL];
1145 else
1146 color = scr->clip_title_color[CLIP_COLLAPSED];
1148 ty = ICON_SIZE - WMFontHeight(scr->clip_title_font) - 3;
1150 tx = CLIP_BUTTON_SIZE * ICON_SIZE / 64;
1152 if(wPreferences.show_clip_title)
1153 WMDrawString(scr->wmscreen, win, color, scr->clip_title_font, tx, ty, ws_name, length);
1155 tx = (ICON_SIZE / 2 - WMWidthOfString(scr->clip_title_font, ws_number, nlength)) / 2;
1157 WMDrawString(scr->wmscreen, win, color, scr->clip_title_font, tx, 2, ws_number, nlength);
1159 wfree(ws_name);
1161 if (aicon->launching) {
1162 XFillRectangle(dpy, aicon->icon->core->window, scr->stipple_gc,
1163 0, 0, wPreferences.icon_size, wPreferences.icon_size);
1165 paintClipButtons(aicon, aicon->dock->lclip_button_pushed, aicon->dock->rclip_button_pushed);
1168 static void clipIconExpose(WObjDescriptor *desc, XEvent *event)
1170 wClipIconPaint(desc->parent);
1173 static void dockIconPaint(WAppIcon *btn)
1175 if (btn == btn->icon->core->screen_ptr->clip_icon)
1176 wClipIconPaint(btn);
1177 else {
1178 wAppIconPaint(btn);
1179 wAppIconSave(btn);
1183 static WMPropList *make_icon_state(WAppIcon *btn)
1185 WMPropList *node = NULL;
1186 WMPropList *command, *autolaunch, *lock, *name, *forced;
1187 WMPropList *position, *buggy, *omnipresent;
1188 char *tmp;
1189 char buffer[64];
1191 if (btn) {
1192 if (!btn->command)
1193 command = WMCreatePLString("-");
1194 else
1195 command = WMCreatePLString(btn->command);
1197 autolaunch = btn->auto_launch ? dYes : dNo;
1199 lock = btn->lock ? dYes : dNo;
1201 tmp = EscapeWM_CLASS(btn->wm_instance, btn->wm_class);
1203 name = WMCreatePLString(tmp);
1205 wfree(tmp);
1207 forced = btn->forced_dock ? dYes : dNo;
1209 buggy = btn->buggy_app ? dYes : dNo;
1211 if (btn == btn->icon->core->screen_ptr->clip_icon)
1212 snprintf(buffer, sizeof(buffer), "%i,%i", btn->x_pos, btn->y_pos);
1213 else
1214 snprintf(buffer, sizeof(buffer), "%hi,%hi", btn->xindex, btn->yindex);
1215 position = WMCreatePLString(buffer);
1217 node = WMCreatePLDictionary(dCommand, command,
1218 dName, name,
1219 dAutoLaunch, autolaunch,
1220 dLock, lock,
1221 dForced, forced, dBuggyApplication, buggy, dPosition, position, NULL);
1222 WMReleasePropList(command);
1223 WMReleasePropList(name);
1224 WMReleasePropList(position);
1226 omnipresent = btn->omnipresent ? dYes : dNo;
1227 if (btn->dock != btn->icon->core->screen_ptr->dock && (btn->xindex != 0 || btn->yindex != 0))
1228 WMPutInPLDictionary(node, dOmnipresent, omnipresent);
1230 #ifdef XDND /* was OFFIX */
1231 if (btn->dnd_command) {
1232 command = WMCreatePLString(btn->dnd_command);
1233 WMPutInPLDictionary(node, dDropCommand, command);
1234 WMReleasePropList(command);
1236 #endif /* XDND */
1238 if (btn->paste_command) {
1239 command = WMCreatePLString(btn->paste_command);
1240 WMPutInPLDictionary(node, dPasteCommand, command);
1241 WMReleasePropList(command);
1245 return node;
1248 static WMPropList *dockSaveState(WDock *dock)
1250 int i;
1251 WMPropList *icon_info;
1252 WMPropList *list = NULL, *dock_state = NULL;
1253 WMPropList *value, *key;
1254 char buffer[256];
1256 list = WMCreatePLArray(NULL);
1258 for (i = (dock->type == WM_DOCK ? 0 : 1); i < dock->max_icons; i++) {
1259 WAppIcon *btn = dock->icon_array[i];
1261 if (!btn || btn->attracted)
1262 continue;
1264 if ((icon_info = make_icon_state(dock->icon_array[i]))) {
1265 WMAddToPLArray(list, icon_info);
1266 WMReleasePropList(icon_info);
1270 dock_state = WMCreatePLDictionary(dApplications, list, NULL);
1272 if (dock->type == WM_DOCK) {
1273 snprintf(buffer, sizeof(buffer), "Applications%i", dock->screen_ptr->scr_height);
1274 key = WMCreatePLString(buffer);
1275 WMPutInPLDictionary(dock_state, key, list);
1276 WMReleasePropList(key);
1278 snprintf(buffer, sizeof(buffer), "%i,%i", (dock->on_right_side ? -ICON_SIZE : 0), dock->y_pos);
1279 value = WMCreatePLString(buffer);
1280 WMPutInPLDictionary(dock_state, dPosition, value);
1281 WMReleasePropList(value);
1283 WMReleasePropList(list);
1285 value = (dock->lowered ? dYes : dNo);
1286 WMPutInPLDictionary(dock_state, dLowered, value);
1288 if (dock->type == WM_CLIP) {
1289 value = (dock->collapsed ? dYes : dNo);
1290 WMPutInPLDictionary(dock_state, dCollapsed, value);
1292 value = (dock->auto_collapse ? dYes : dNo);
1293 WMPutInPLDictionary(dock_state, dAutoCollapse, value);
1295 value = (dock->auto_raise_lower ? dYes : dNo);
1296 WMPutInPLDictionary(dock_state, dAutoRaiseLower, value);
1298 value = (dock->attract_icons ? dYes : dNo);
1299 WMPutInPLDictionary(dock_state, dAutoAttractIcons, value);
1302 return dock_state;
1305 void wDockSaveState(WScreen *scr, WMPropList *old_state)
1307 WMPropList *dock_state;
1308 WMPropList *keys;
1310 dock_state = dockSaveState(scr->dock);
1313 * Copy saved states of docks with different sizes.
1315 if (old_state) {
1316 int i;
1317 WMPropList *tmp;
1319 keys = WMGetPLDictionaryKeys(old_state);
1320 for (i = 0; i < WMGetPropListItemCount(keys); i++) {
1321 tmp = WMGetFromPLArray(keys, i);
1323 if (strncasecmp(WMGetFromPLString(tmp), "applications", 12) == 0
1324 && !WMGetFromPLDictionary(dock_state, tmp)) {
1326 WMPutInPLDictionary(dock_state, tmp, WMGetFromPLDictionary(old_state, tmp));
1329 WMReleasePropList(keys);
1332 WMPutInPLDictionary(scr->session_state, dDock, dock_state);
1334 WMReleasePropList(dock_state);
1337 void wClipSaveState(WScreen *scr)
1339 WMPropList *clip_state;
1341 clip_state = make_icon_state(scr->clip_icon);
1343 WMPutInPLDictionary(scr->session_state, dClip, clip_state);
1345 WMReleasePropList(clip_state);
1348 WMPropList *wClipSaveWorkspaceState(WScreen *scr, int workspace)
1350 return dockSaveState(scr->workspaces[workspace]->clip);
1353 static Bool getBooleanDockValue(WMPropList *value, WMPropList *key)
1355 if (value) {
1356 if (WMIsPLString(value)) {
1357 if (strcasecmp(WMGetFromPLString(value), "YES") == 0)
1358 return True;
1359 } else {
1360 wwarning(_("bad value in docked icon state info %s"), WMGetFromPLString(key));
1363 return False;
1366 static WAppIcon *restore_icon_state(WScreen *scr, WMPropList *info, int type, int index)
1368 WAppIcon *aicon;
1369 WMPropList *cmd, *value;
1371 cmd = WMGetFromPLDictionary(info, dCommand);
1372 if (!cmd || !WMIsPLString(cmd)) {
1373 return NULL;
1376 /* parse window name */
1377 value = WMGetFromPLDictionary(info, dName);
1378 if (!value)
1379 return NULL;
1382 char *wclass, *winstance;
1383 char *command;
1385 ParseWindowName(value, &winstance, &wclass, "dock");
1387 if (!winstance && !wclass) {
1388 return NULL;
1391 /* get commands */
1393 if (cmd)
1394 command = wstrdup(WMGetFromPLString(cmd));
1395 else
1396 command = NULL;
1398 if (!command || strcmp(command, "-") == 0) {
1399 if (command)
1400 wfree(command);
1401 if (wclass)
1402 wfree(wclass);
1403 if (winstance)
1404 wfree(winstance);
1406 return NULL;
1409 aicon = wAppIconCreateForDock(scr, command, winstance, wclass, TILE_NORMAL);
1410 if (wclass)
1411 wfree(wclass);
1412 if (winstance)
1413 wfree(winstance);
1414 if (command)
1415 wfree(command);
1418 aicon->icon->core->descriptor.handle_mousedown = iconMouseDown;
1419 if (type == WM_CLIP) {
1420 aicon->icon->core->descriptor.handle_enternotify = clipEnterNotify;
1421 aicon->icon->core->descriptor.handle_leavenotify = clipLeaveNotify;
1423 aicon->icon->core->descriptor.parent_type = WCLASS_DOCK_ICON;
1424 aicon->icon->core->descriptor.parent = aicon;
1426 #ifdef XDND /* was OFFIX */
1427 cmd = WMGetFromPLDictionary(info, dDropCommand);
1428 if (cmd)
1429 aicon->dnd_command = wstrdup(WMGetFromPLString(cmd));
1430 #endif
1432 cmd = WMGetFromPLDictionary(info, dPasteCommand);
1433 if (cmd)
1434 aicon->paste_command = wstrdup(WMGetFromPLString(cmd));
1436 /* check auto launch */
1437 value = WMGetFromPLDictionary(info, dAutoLaunch);
1439 aicon->auto_launch = getBooleanDockValue(value, dAutoLaunch);
1441 /* check lock */
1442 value = WMGetFromPLDictionary(info, dLock);
1444 aicon->lock = getBooleanDockValue(value, dLock);
1446 /* check if it wasn't normally docked */
1447 value = WMGetFromPLDictionary(info, dForced);
1449 aicon->forced_dock = getBooleanDockValue(value, dForced);
1451 /* check if we can rely on the stuff in the app */
1452 value = WMGetFromPLDictionary(info, dBuggyApplication);
1454 aicon->buggy_app = getBooleanDockValue(value, dBuggyApplication);
1456 /* get position in the dock */
1457 value = WMGetFromPLDictionary(info, dPosition);
1458 if (value && WMIsPLString(value)) {
1459 if (sscanf(WMGetFromPLString(value), "%hi,%hi", &aicon->xindex, &aicon->yindex) != 2)
1460 wwarning(_("bad value in docked icon state info %s"), WMGetFromPLString(dPosition));
1462 /* check position sanity */
1463 /* incomplete section! */
1464 if (type == WM_DOCK) {
1465 aicon->xindex = 0;
1466 if (aicon->yindex < 0)
1467 wwarning(_("bad value in docked icon position %i,%i"),
1468 aicon->xindex, aicon->yindex);
1470 } else {
1471 aicon->yindex = index;
1472 aicon->xindex = 0;
1475 /* check if icon is omnipresent */
1476 value = WMGetFromPLDictionary(info, dOmnipresent);
1478 aicon->omnipresent = getBooleanDockValue(value, dOmnipresent);
1480 aicon->running = 0;
1481 aicon->docked = 1;
1483 return aicon;
1486 #define COMPLAIN(key) wwarning(_("bad value in dock state info:%s"), key)
1488 WAppIcon *wClipRestoreState(WScreen *scr, WMPropList *clip_state)
1490 WAppIcon *icon;
1491 WMPropList *value;
1493 icon = mainIconCreate(scr, WM_CLIP);
1495 if (!clip_state)
1496 return icon;
1498 WMRetainPropList(clip_state);
1500 /* restore position */
1502 value = WMGetFromPLDictionary(clip_state, dPosition);
1504 if (value) {
1505 if (!WMIsPLString(value))
1506 COMPLAIN("Position");
1507 else {
1508 WMRect rect;
1509 int flags;
1511 if (sscanf(WMGetFromPLString(value), "%i,%i", &icon->x_pos, &icon->y_pos) != 2)
1512 COMPLAIN("Position");
1514 /* check position sanity */
1515 rect.pos.x = icon->x_pos;
1516 rect.pos.y = icon->y_pos;
1517 rect.size.width = rect.size.height = ICON_SIZE;
1519 wGetRectPlacementInfo(scr, rect, &flags);
1520 if (flags & (XFLAG_DEAD | XFLAG_PARTIAL))
1521 wScreenKeepInside(scr, &icon->x_pos, &icon->y_pos, ICON_SIZE, ICON_SIZE);
1524 #ifdef XDND /* was OFFIX */
1525 value = WMGetFromPLDictionary(clip_state, dDropCommand);
1526 if (value && WMIsPLString(value))
1527 icon->dnd_command = wstrdup(WMGetFromPLString(value));
1528 #endif
1530 value = WMGetFromPLDictionary(clip_state, dPasteCommand);
1531 if (value && WMIsPLString(value))
1532 icon->paste_command = wstrdup(WMGetFromPLString(value));
1534 WMReleasePropList(clip_state);
1536 return icon;
1539 WDock *wDockRestoreState(WScreen *scr, WMPropList *dock_state, int type)
1541 WDock *dock;
1542 WMPropList *apps;
1543 WMPropList *value;
1544 WAppIcon *aicon, *old_top;
1545 int count, i;
1547 dock = wDockCreate(scr, type);
1549 if (!dock_state)
1550 return dock;
1552 WMRetainPropList(dock_state);
1554 /* restore position */
1555 value = WMGetFromPLDictionary(dock_state, dPosition);
1556 if (value) {
1557 if (!WMIsPLString(value)) {
1558 COMPLAIN("Position");
1559 } else {
1560 WMRect rect;
1561 int flags;
1563 if (sscanf(WMGetFromPLString(value), "%i,%i", &dock->x_pos, &dock->y_pos) != 2)
1564 COMPLAIN("Position");
1566 /* check position sanity */
1567 rect.pos.x = dock->x_pos;
1568 rect.pos.y = dock->y_pos;
1569 rect.size.width = rect.size.height = ICON_SIZE;
1571 wGetRectPlacementInfo(scr, rect, &flags);
1572 if (flags & (XFLAG_DEAD | XFLAG_PARTIAL)) {
1573 int x = dock->x_pos;
1574 wScreenKeepInside(scr, &x, &dock->y_pos, ICON_SIZE, ICON_SIZE);
1577 /* Is this needed any more? */
1578 if (type == WM_CLIP) {
1579 if (dock->x_pos < 0) {
1580 dock->x_pos = 0;
1581 } else if (dock->x_pos > scr->scr_width - ICON_SIZE) {
1582 dock->x_pos = scr->scr_width - ICON_SIZE;
1584 } else {
1585 if (dock->x_pos >= 0) {
1586 dock->x_pos = DOCK_EXTRA_SPACE;
1587 dock->on_right_side = 0;
1588 } else {
1589 dock->x_pos = scr->scr_width - DOCK_EXTRA_SPACE - ICON_SIZE;
1590 dock->on_right_side = 1;
1596 /* restore lowered/raised state */
1597 dock->lowered = 0;
1599 value = WMGetFromPLDictionary(dock_state, dLowered);
1600 if (value) {
1601 if (!WMIsPLString(value)) {
1602 COMPLAIN("Lowered");
1603 } else {
1604 if (strcasecmp(WMGetFromPLString(value), "YES") == 0) {
1605 dock->lowered = 1;
1610 /* restore collapsed state */
1611 dock->collapsed = 0;
1613 value = WMGetFromPLDictionary(dock_state, dCollapsed);
1614 if (value) {
1615 if (!WMIsPLString(value)) {
1616 COMPLAIN("Collapsed");
1617 } else {
1618 if (strcasecmp(WMGetFromPLString(value), "YES") == 0) {
1619 dock->collapsed = 1;
1624 /* restore auto-collapsed state */
1625 value = WMGetFromPLDictionary(dock_state, dAutoCollapse);
1626 if (value) {
1627 if (!WMIsPLString(value)) {
1628 COMPLAIN("AutoCollapse");
1629 } else {
1630 if (strcasecmp(WMGetFromPLString(value), "YES") == 0) {
1631 dock->auto_collapse = 1;
1632 dock->collapsed = 1;
1637 /* restore auto-raise/lower state */
1638 value = WMGetFromPLDictionary(dock_state, dAutoRaiseLower);
1639 if (value) {
1640 if (!WMIsPLString(value)) {
1641 COMPLAIN("AutoRaiseLower");
1642 } else {
1643 if (strcasecmp(WMGetFromPLString(value), "YES") == 0)
1644 dock->auto_raise_lower = 1;
1648 /* restore attract icons state */
1649 dock->attract_icons = 0;
1651 value = WMGetFromPLDictionary(dock_state, dAutoAttractIcons);
1652 if (value) {
1653 if (!WMIsPLString(value)) {
1654 COMPLAIN("AutoAttractIcons");
1655 } else {
1656 if (strcasecmp(WMGetFromPLString(value), "YES") == 0)
1657 dock->attract_icons = 1;
1661 /* application list */
1664 WMPropList *tmp;
1665 char buffer[64];
1668 * When saving, it saves the dock state in
1669 * Applications and Applicationsnnn
1671 * When loading, it will first try Applicationsnnn.
1672 * If it does not exist, use Applications as default.
1675 snprintf(buffer, sizeof(buffer), "Applications%i", scr->scr_height);
1677 tmp = WMCreatePLString(buffer);
1678 apps = WMGetFromPLDictionary(dock_state, tmp);
1679 WMReleasePropList(tmp);
1681 if (!apps)
1682 apps = WMGetFromPLDictionary(dock_state, dApplications);
1685 if (!apps)
1686 goto finish;
1688 count = WMGetPropListItemCount(apps);
1689 if (count == 0)
1690 goto finish;
1692 old_top = dock->icon_array[0];
1694 /* dock->icon_count is set to 1 when dock is created.
1695 * Since Clip is already restored, we want to keep it so for clip,
1696 * but for dock we may change the default top tile, so we set it to 0.
1698 if (type == WM_DOCK)
1699 dock->icon_count = 0;
1701 for (i = 0; i < count; i++) {
1702 if (dock->icon_count >= dock->max_icons) {
1703 wwarning(_("there are too many icons stored in dock. Ignoring what doesn't fit"));
1704 break;
1707 value = WMGetFromPLArray(apps, i);
1708 aicon = restore_icon_state(scr, value, type, dock->icon_count);
1710 dock->icon_array[dock->icon_count] = aicon;
1712 if (aicon) {
1713 aicon->dock = dock;
1714 aicon->x_pos = dock->x_pos + (aicon->xindex * ICON_SIZE);
1715 aicon->y_pos = dock->y_pos + (aicon->yindex * ICON_SIZE);
1717 if (dock->lowered)
1718 ChangeStackingLevel(aicon->icon->core, WMNormalLevel);
1719 else
1720 ChangeStackingLevel(aicon->icon->core, WMDockLevel);
1722 wCoreConfigure(aicon->icon->core, aicon->x_pos, aicon->y_pos, 0, 0);
1724 if (!dock->collapsed)
1725 XMapWindow(dpy, aicon->icon->core->window);
1726 wRaiseFrame(aicon->icon->core);
1728 dock->icon_count++;
1729 } else if (dock->icon_count == 0 && type == WM_DOCK)
1730 dock->icon_count++;
1733 /* if the first icon is not defined, use the default */
1734 if (dock->icon_array[0] == NULL) {
1735 /* update default icon */
1736 old_top->x_pos = dock->x_pos;
1737 old_top->y_pos = dock->y_pos;
1738 if (dock->lowered)
1739 ChangeStackingLevel(old_top->icon->core, WMNormalLevel);
1740 else
1741 ChangeStackingLevel(old_top->icon->core, WMDockLevel);
1742 dock->icon_array[0] = old_top;
1743 XMoveWindow(dpy, old_top->icon->core->window, dock->x_pos, dock->y_pos);
1744 /* we don't need to increment dock->icon_count here because it was
1745 * incremented in the loop above.
1747 } else if (old_top != dock->icon_array[0]) {
1748 if (old_top == scr->clip_icon)
1749 scr->clip_icon = dock->icon_array[0];
1750 wAppIconDestroy(old_top);
1753 finish:
1754 WMReleasePropList(dock_state);
1756 return dock;
1759 void wDockLaunchWithState(WDock *dock, WAppIcon *btn, WSavedState *state)
1761 if (btn && btn->command && !btn->running && !btn->launching) {
1763 btn->drop_launch = 0;
1764 btn->paste_launch = 0;
1766 btn->pid = execCommand(btn, btn->command, state);
1768 if (btn->pid > 0) {
1769 if (!btn->forced_dock && !btn->buggy_app) {
1770 btn->launching = 1;
1771 dockIconPaint(btn);
1774 } else {
1775 wfree(state);
1779 void wDockDoAutoLaunch(WDock *dock, int workspace)
1781 WAppIcon *btn;
1782 WSavedState *state;
1783 int i;
1785 for (i = 0; i < dock->max_icons; i++) {
1786 btn = dock->icon_array[i];
1787 if (!btn || !btn->auto_launch)
1788 continue;
1790 state = wmalloc(sizeof(WSavedState));
1791 memset(state, 0, sizeof(WSavedState));
1792 state->workspace = workspace;
1793 /* TODO: this is klugy and is very difficult to understand
1794 * what's going on. Try to clean up */
1795 wDockLaunchWithState(dock, btn, state);
1799 #ifdef XDND /* was OFFIX */
1800 static WDock *findDock(WScreen *scr, XEvent *event, int *icon_pos)
1802 WDock *dock;
1803 int i;
1805 *icon_pos = -1;
1806 if ((dock = scr->dock) != NULL) {
1807 for (i = 0; i < dock->max_icons; i++) {
1808 if (dock->icon_array[i]
1809 && dock->icon_array[i]->icon->core->window == event->xclient.window) {
1810 *icon_pos = i;
1811 break;
1815 if (*icon_pos < 0 && (dock = scr->workspaces[scr->current_workspace]->clip) != NULL) {
1816 for (i = 0; i < dock->max_icons; i++) {
1817 if (dock->icon_array[i]
1818 && dock->icon_array[i]->icon->core->window == event->xclient.window) {
1819 *icon_pos = i;
1820 break;
1824 if (*icon_pos >= 0)
1825 return dock;
1826 return NULL;
1829 int wDockReceiveDNDDrop(WScreen *scr, XEvent *event)
1831 WDock *dock;
1832 WAppIcon *btn;
1833 int icon_pos;
1835 dock = findDock(scr, event, &icon_pos);
1836 if (!dock)
1837 return False;
1840 * Return True if the drop was on an application icon window.
1841 * In this case, let the ClientMessage handler redirect the
1842 * message to the app.
1844 if (dock->icon_array[icon_pos]->icon->icon_win != None)
1845 return True;
1847 if (dock->icon_array[icon_pos]->dnd_command != NULL) {
1848 scr->flags.dnd_data_convertion_status = 0;
1850 btn = dock->icon_array[icon_pos];
1852 if (!btn->forced_dock) {
1853 btn->relaunching = btn->running;
1854 btn->running = 1;
1856 if (btn->wm_instance || btn->wm_class) {
1857 WWindowAttributes attr;
1858 memset(&attr, 0, sizeof(WWindowAttributes));
1859 wDefaultFillAttributes(btn->icon->core->screen_ptr,
1860 btn->wm_instance, btn->wm_class, &attr, NULL, True);
1862 if (!attr.no_appicon)
1863 btn->launching = 1;
1864 else
1865 btn->running = 0;
1868 btn->paste_launch = 0;
1869 btn->drop_launch = 1;
1870 scr->last_dock = dock;
1871 btn->pid = execCommand(btn, btn->dnd_command, NULL);
1872 if (btn->pid > 0) {
1873 dockIconPaint(btn);
1874 } else {
1875 btn->launching = 0;
1876 if (!btn->relaunching) {
1877 btn->running = 0;
1881 return False;
1883 #endif /* XDND */
1885 Bool wDockAttachIcon(WDock *dock, WAppIcon *icon, int x, int y)
1887 WWindow *wwin;
1888 int index;
1890 wwin = icon->icon->owner;
1891 if (icon->command == NULL) {
1892 char *command;
1894 icon->editing = 0;
1896 command = GetCommandForWindow(wwin->client_win);
1897 if (command) {
1898 icon->command = command;
1899 } else {
1900 /* icon->forced_dock = 1; */
1901 if (dock->type != WM_CLIP || !icon->attracted) {
1902 icon->editing = 1;
1903 if (wInputDialog(dock->screen_ptr, _("Dock Icon"),
1904 _("Type the command used to launch the application"), &command)) {
1905 if (command && (command[0] == 0 || (command[0] == '-' && command[1] == 0))) {
1906 wfree(command);
1907 command = NULL;
1909 icon->command = command;
1910 icon->editing = 0;
1911 } else {
1912 icon->editing = 0;
1913 if (command)
1914 wfree(command);
1915 /* If the target is the dock, reject the icon. If
1916 * the target is the clip, make it an attracted icon
1918 if (dock->type == WM_CLIP) {
1919 icon->attracted = 1;
1920 if (!icon->icon->shadowed) {
1921 icon->icon->shadowed = 1;
1922 icon->icon->force_paint = 1;
1924 } else {
1925 return False;
1930 } else {
1931 icon->editing = 0;
1934 for (index = 1; index < dock->max_icons; index++)
1935 if (dock->icon_array[index] == NULL)
1936 break;
1937 /* if (index == dock->max_icons)
1938 return; */
1940 assert(index < dock->max_icons);
1942 dock->icon_array[index] = icon;
1943 icon->yindex = y;
1944 icon->xindex = x;
1946 icon->omnipresent = 0;
1948 icon->x_pos = dock->x_pos + x * ICON_SIZE;
1949 icon->y_pos = dock->y_pos + y * ICON_SIZE;
1951 dock->icon_count++;
1953 icon->running = 1;
1954 icon->launching = 0;
1955 icon->docked = 1;
1956 icon->dock = dock;
1957 icon->icon->core->descriptor.handle_mousedown = iconMouseDown;
1958 if (dock->type == WM_CLIP) {
1959 icon->icon->core->descriptor.handle_enternotify = clipEnterNotify;
1960 icon->icon->core->descriptor.handle_leavenotify = clipLeaveNotify;
1962 icon->icon->core->descriptor.parent_type = WCLASS_DOCK_ICON;
1963 icon->icon->core->descriptor.parent = icon;
1965 MoveInStackListUnder(dock->icon_array[index - 1]->icon->core, icon->icon->core);
1966 wAppIconMove(icon, icon->x_pos, icon->y_pos);
1967 wAppIconPaint(icon);
1968 wAppIconSave(icon);
1970 if (wPreferences.auto_arrange_icons)
1971 wArrangeIcons(dock->screen_ptr, True);
1973 #ifdef XDND /* was OFFIX */
1974 if (icon->command && !icon->dnd_command) {
1975 int len = strlen(icon->command) + 8;
1976 icon->dnd_command = wmalloc(len);
1977 snprintf(icon->dnd_command, len, "%s %%d", icon->command);
1979 #endif
1981 if (icon->command && !icon->paste_command) {
1982 int len = strlen(icon->command) + 8;
1983 icon->paste_command = wmalloc(len);
1984 snprintf(icon->paste_command, len, "%s %%s", icon->command);
1987 return True;
1990 static void reattachIcon(WDock *dock, WAppIcon *icon, int x, int y)
1992 int index;
1994 for (index = 1; index < dock->max_icons; index++) {
1995 if (dock->icon_array[index] == icon)
1996 break;
1998 assert(index < dock->max_icons);
2000 icon->yindex = y;
2001 icon->xindex = x;
2003 icon->x_pos = dock->x_pos + x * ICON_SIZE;
2004 icon->y_pos = dock->y_pos + y * ICON_SIZE;
2007 static Bool moveIconBetweenDocks(WDock *src, WDock *dest, WAppIcon *icon, int x, int y)
2009 WWindow *wwin;
2010 char *command;
2011 int index;
2013 if (src == dest)
2014 return True; /* No move needed, we're already there */
2016 if (dest == NULL)
2017 return False;
2019 wwin = icon->icon->owner;
2022 * For the moment we can't do this if we move icons in Clip from one
2023 * workspace to other, because if we move two or more icons without
2024 * command, the dialog box will not be able to tell us to which of the
2025 * moved icons it applies. -Dan
2027 if ((dest->type == WM_DOCK /*|| dest->keep_attracted */ ) && icon->command == NULL) {
2028 command = GetCommandForWindow(wwin->client_win);
2029 if (command) {
2030 icon->command = command;
2031 } else {
2032 icon->editing = 1;
2033 /* icon->forced_dock = 1; */
2034 if (wInputDialog(src->screen_ptr, _("Dock Icon"),
2035 _("Type the command used to launch the application"), &command)) {
2036 if (command && (command[0] == 0 || (command[0] == '-' && command[1] == 0))) {
2037 wfree(command);
2038 command = NULL;
2040 icon->command = command;
2041 } else {
2042 icon->editing = 0;
2043 if (command)
2044 wfree(command);
2045 return False;
2047 icon->editing = 0;
2051 if (dest->type == WM_DOCK)
2052 wClipMakeIconOmnipresent(icon, False);
2054 for (index = 1; index < src->max_icons; index++) {
2055 if (src->icon_array[index] == icon)
2056 break;
2058 assert(index < src->max_icons);
2060 src->icon_array[index] = NULL;
2061 src->icon_count--;
2063 for (index = 1; index < dest->max_icons; index++) {
2064 if (dest->icon_array[index] == NULL)
2065 break;
2067 /* if (index == dest->max_icons)
2068 return; */
2070 assert(index < dest->max_icons);
2072 dest->icon_array[index] = icon;
2073 icon->dock = dest;
2075 /* deselect the icon */
2076 if (icon->icon->selected)
2077 wIconSelect(icon->icon);
2079 if (dest->type == WM_DOCK) {
2080 icon->icon->core->descriptor.handle_enternotify = NULL;
2081 icon->icon->core->descriptor.handle_leavenotify = NULL;
2082 } else {
2083 icon->icon->core->descriptor.handle_enternotify = clipEnterNotify;
2084 icon->icon->core->descriptor.handle_leavenotify = clipLeaveNotify;
2087 /* set it to be kept when moving to dock.
2088 * Unless the icon does not have a command set
2090 if (icon->command && dest->type == WM_DOCK) {
2091 icon->attracted = 0;
2092 if (icon->icon->shadowed) {
2093 icon->icon->shadowed = 0;
2094 icon->icon->force_paint = 1;
2096 wAppIconSave(icon);
2099 if (src->auto_collapse || src->auto_raise_lower)
2100 clipLeave(src);
2102 icon->yindex = y;
2103 icon->xindex = x;
2105 icon->x_pos = dest->x_pos + x * ICON_SIZE;
2106 icon->y_pos = dest->y_pos + y * ICON_SIZE;
2108 dest->icon_count++;
2110 MoveInStackListUnder(dest->icon_array[index - 1]->icon->core, icon->icon->core);
2111 wAppIconPaint(icon);
2113 return True;
2116 void wDockDetach(WDock *dock, WAppIcon *icon)
2118 int index;
2120 /* make the settings panel be closed */
2121 if (icon->panel) {
2122 DestroyDockAppSettingsPanel(icon->panel);
2125 /* This must be called before icon->dock is set to NULL.
2126 * Don't move it. -Dan
2128 wClipMakeIconOmnipresent(icon, False);
2130 icon->docked = 0;
2131 icon->dock = NULL;
2132 icon->attracted = 0;
2133 icon->auto_launch = 0;
2134 if (icon->icon->shadowed) {
2135 icon->icon->shadowed = 0;
2136 icon->icon->force_paint = 1;
2139 /* deselect the icon */
2140 if (icon->icon->selected)
2141 wIconSelect(icon->icon);
2143 if (icon->command) {
2144 wfree(icon->command);
2145 icon->command = NULL;
2147 #ifdef XDND /* was OFFIX */
2148 if (icon->dnd_command) {
2149 wfree(icon->dnd_command);
2150 icon->dnd_command = NULL;
2152 #endif
2153 if (icon->paste_command) {
2154 wfree(icon->paste_command);
2155 icon->paste_command = NULL;
2158 for (index = 1; index < dock->max_icons; index++)
2159 if (dock->icon_array[index] == icon)
2160 break;
2161 assert(index < dock->max_icons);
2162 dock->icon_array[index] = NULL;
2163 icon->yindex = -1;
2164 icon->xindex = -1;
2166 dock->icon_count--;
2168 /* if the dock is not attached to an application or
2169 * the the application did not set the approriate hints yet,
2170 * destroy the icon */
2171 if (!icon->running || !wApplicationOf(icon->main_window))
2172 wAppIconDestroy(icon);
2173 else {
2174 icon->icon->core->descriptor.handle_mousedown = appIconMouseDown;
2175 icon->icon->core->descriptor.handle_enternotify = NULL;
2176 icon->icon->core->descriptor.handle_leavenotify = NULL;
2177 icon->icon->core->descriptor.parent_type = WCLASS_APPICON;
2178 icon->icon->core->descriptor.parent = icon;
2180 ChangeStackingLevel(icon->icon->core, NORMAL_ICON_LEVEL);
2182 wAppIconPaint(icon);
2183 if (wPreferences.auto_arrange_icons) {
2184 wArrangeIcons(dock->screen_ptr, True);
2187 if (dock->auto_collapse || dock->auto_raise_lower)
2188 clipLeave(dock);
2192 * returns the closest Dock slot index for the passed
2193 * coordinates.
2195 * Returns False if icon can't be docked.
2197 * Note: this function should NEVER alter ret_x or ret_y, unless it will
2198 * return True. -Dan
2200 Bool wDockSnapIcon(WDock *dock, WAppIcon *icon, int req_x, int req_y, int *ret_x, int *ret_y, int redocking)
2202 WScreen *scr = dock->screen_ptr;
2203 int dx, dy;
2204 int ex_x, ex_y;
2205 int i, offset = ICON_SIZE / 2;
2206 WAppIcon *aicon = NULL;
2207 WAppIcon *nicon = NULL;
2208 int max_y_icons;
2210 /* TODO: XINERAMA, for these */
2211 max_y_icons = scr->scr_height / ICON_SIZE - 1;
2213 if (wPreferences.flags.noupdates)
2214 return False;
2216 dx = dock->x_pos;
2217 dy = dock->y_pos;
2219 /* if the dock is full */
2220 if (!redocking && (dock->icon_count >= dock->max_icons)) {
2221 return False;
2224 /* exact position */
2225 if (req_y < dy)
2226 ex_y = (req_y - offset - dy) / ICON_SIZE;
2227 else
2228 ex_y = (req_y + offset - dy) / ICON_SIZE;
2230 if (req_x < dx)
2231 ex_x = (req_x - offset - dx) / ICON_SIZE;
2232 else
2233 ex_x = (req_x + offset - dx) / ICON_SIZE;
2235 /* check if the icon is outside the screen boundaries */
2237 WMRect rect;
2238 int flags;
2240 rect.pos.x = dx + ex_x * ICON_SIZE;
2241 rect.pos.y = dy + ex_y * ICON_SIZE;
2242 rect.size.width = rect.size.height = ICON_SIZE;
2244 wGetRectPlacementInfo(scr, rect, &flags);
2245 if (flags & (XFLAG_DEAD | XFLAG_PARTIAL))
2246 return False;
2249 if (dock->type == WM_DOCK) {
2250 if (icon->dock != dock && ex_x != 0)
2251 return False;
2253 aicon = NULL;
2254 for (i = 0; i < dock->max_icons; i++) {
2255 nicon = dock->icon_array[i];
2256 if (nicon && nicon->yindex == ex_y) {
2257 aicon = nicon;
2258 break;
2262 if (redocking) {
2263 int sig, done, closest;
2265 /* Possible cases when redocking:
2267 * icon dragged out of range of any slot -> false
2268 * icon dragged to range of free slot
2269 * icon dragged to range of same slot
2270 * icon dragged to range of different icon
2272 if (abs(ex_x) > DOCK_DETTACH_THRESHOLD)
2273 return False;
2275 if (ex_y >= 0 && ex_y <= max_y_icons && (aicon == icon || !aicon)) {
2276 *ret_x = 0;
2277 *ret_y = ex_y;
2278 return True;
2281 /* start looking at the upper slot or lower? */
2282 if (ex_y * ICON_SIZE < (req_y + offset - dy))
2283 sig = 1;
2284 else
2285 sig = -1;
2287 closest = -1;
2288 done = 0;
2289 /* look for closest free slot */
2290 for (i = 0; i < (DOCK_DETTACH_THRESHOLD + 1) * 2 && !done; i++) {
2291 int j;
2293 done = 1;
2294 closest = sig * (i / 2) + ex_y;
2295 /* check if this slot is used */
2296 if (closest >= 0) {
2297 for (j = 0; j < dock->max_icons; j++) {
2298 if (dock->icon_array[j]
2299 && dock->icon_array[j]->yindex == closest) {
2300 /* slot is used by someone else */
2301 if (dock->icon_array[j] != icon)
2302 done = 0;
2303 break;
2307 sig = -sig;
2309 if (done && closest >= 0 && closest <= max_y_icons &&
2310 ((ex_y >= closest && ex_y - closest < DOCK_DETTACH_THRESHOLD + 1)
2311 || (ex_y < closest && closest - ex_y <= DOCK_DETTACH_THRESHOLD + 1))) {
2312 *ret_x = 0;
2313 *ret_y = closest;
2314 return True;
2316 } else { /* !redocking */
2318 /* if slot is free and the icon is close enough, return it */
2319 if (!aicon && ex_x == 0 && ex_y >= 0 && ex_y <= max_y_icons) {
2320 *ret_x = 0;
2321 *ret_y = ex_y;
2322 return True;
2325 } else { /* CLIP */
2326 int neighbours = 0;
2327 int start, stop, k;
2329 start = icon->omnipresent ? 0 : scr->current_workspace;
2330 stop = icon->omnipresent ? scr->workspace_count : start + 1;
2332 aicon = NULL;
2333 for (k = start; k < stop; k++) {
2334 WDock *tmp = scr->workspaces[k]->clip;
2335 if (!tmp)
2336 continue;
2337 for (i = 0; i < tmp->max_icons; i++) {
2338 nicon = tmp->icon_array[i];
2339 if (nicon && nicon->xindex == ex_x && nicon->yindex == ex_y) {
2340 aicon = nicon;
2341 break;
2344 if (aicon)
2345 break;
2347 for (k = start; k < stop; k++) {
2348 WDock *tmp = scr->workspaces[k]->clip;
2349 if (!tmp)
2350 continue;
2351 for (i = 0; i < tmp->max_icons; i++) {
2352 nicon = tmp->icon_array[i];
2353 if (nicon && nicon != icon && /* Icon can't be it's own neighbour */
2354 (abs(nicon->xindex - ex_x) <= CLIP_ATTACH_VICINITY &&
2355 abs(nicon->yindex - ex_y) <= CLIP_ATTACH_VICINITY)) {
2356 neighbours = 1;
2357 break;
2360 if (neighbours)
2361 break;
2364 if (neighbours && (aicon == NULL || (redocking && aicon == icon))) {
2365 *ret_x = ex_x;
2366 *ret_y = ex_y;
2367 return True;
2370 return False;
2373 static int onScreen(WScreen *scr, int x, int y, int sx, int ex, int sy, int ey)
2375 WMRect rect = wmkrect(x, y, ICON_SIZE, ICON_SIZE);
2376 int flags;
2378 wGetRectPlacementInfo(scr, rect, &flags);
2380 return !(flags & (XFLAG_DEAD | XFLAG_PARTIAL));
2384 * returns true if it can find a free slot in the dock,
2385 * in which case it changes x_pos and y_pos accordingly.
2386 * Else returns false.
2388 Bool wDockFindFreeSlot(WDock *dock, int *x_pos, int *y_pos)
2390 WScreen *scr = dock->screen_ptr;
2391 WAppIcon *btn;
2392 WAppIconChain *chain;
2393 unsigned char *slot_map;
2394 int mwidth;
2395 int r;
2396 int x, y;
2397 int i, done = False;
2398 int corner;
2399 int sx = 0, sy = 0, ex = scr->scr_width, ey = scr->scr_height;
2400 int extra_count = 0;
2402 if (dock->type == WM_CLIP && dock != scr->workspaces[scr->current_workspace]->clip)
2403 extra_count = scr->global_icon_count;
2405 /* if the dock is full */
2406 if (dock->icon_count + extra_count >= dock->max_icons) {
2407 return False;
2410 if (!wPreferences.flags.nodock && scr->dock) {
2411 if (scr->dock->on_right_side)
2412 ex -= ICON_SIZE + DOCK_EXTRA_SPACE;
2413 else
2414 sx += ICON_SIZE + DOCK_EXTRA_SPACE;
2417 if (ex < dock->x_pos)
2418 ex = dock->x_pos;
2419 if (sx > dock->x_pos + ICON_SIZE)
2420 sx = dock->x_pos + ICON_SIZE;
2421 #define C_NONE 0
2422 #define C_NW 1
2423 #define C_NE 2
2424 #define C_SW 3
2425 #define C_SE 4
2427 /* check if clip is in a corner */
2428 if (dock->type == WM_CLIP) {
2429 if (dock->x_pos < 1 && dock->y_pos < 1)
2430 corner = C_NE;
2431 else if (dock->x_pos < 1 && dock->y_pos >= (ey - ICON_SIZE))
2432 corner = C_SE;
2433 else if (dock->x_pos >= (ex - ICON_SIZE) && dock->y_pos >= (ey - ICON_SIZE))
2434 corner = C_SW;
2435 else if (dock->x_pos >= (ex - ICON_SIZE) && dock->y_pos < 1)
2436 corner = C_NW;
2437 else
2438 corner = C_NONE;
2439 } else
2440 corner = C_NONE;
2442 /* If the clip is in the corner, use only slots that are in the border
2443 * of the screen */
2444 if (corner != C_NONE) {
2445 char *hmap, *vmap;
2446 int hcount, vcount;
2448 hcount = WMIN(dock->max_icons, scr->scr_width / ICON_SIZE);
2449 vcount = WMIN(dock->max_icons, scr->scr_height / ICON_SIZE);
2450 hmap = wmalloc(hcount + 1);
2451 memset(hmap, 0, hcount + 1);
2452 vmap = wmalloc(vcount + 1);
2453 memset(vmap, 0, vcount + 1);
2455 /* mark used positions */
2456 switch (corner) {
2457 case C_NE:
2458 for (i = 0; i < dock->max_icons; i++) {
2459 btn = dock->icon_array[i];
2460 if (!btn)
2461 continue;
2463 if (btn->xindex == 0 && btn->yindex > 0 && btn->yindex < vcount)
2464 vmap[btn->yindex] = 1;
2465 else if (btn->yindex == 0 && btn->xindex > 0 && btn->xindex < hcount)
2466 hmap[btn->xindex] = 1;
2468 for (chain = scr->global_icons; chain != NULL; chain = chain->next) {
2469 btn = chain->aicon;
2470 if (btn->xindex == 0 && btn->yindex > 0 && btn->yindex < vcount)
2471 vmap[btn->yindex] = 1;
2472 else if (btn->yindex == 0 && btn->xindex > 0 && btn->xindex < hcount)
2473 hmap[btn->xindex] = 1;
2475 break;
2476 case C_NW:
2477 for (i = 0; i < dock->max_icons; i++) {
2478 btn = dock->icon_array[i];
2479 if (!btn)
2480 continue;
2482 if (btn->xindex == 0 && btn->yindex > 0 && btn->yindex < vcount)
2483 vmap[btn->yindex] = 1;
2484 else if (btn->yindex == 0 && btn->xindex < 0 && btn->xindex > -hcount)
2485 hmap[-btn->xindex] = 1;
2487 for (chain = scr->global_icons; chain != NULL; chain = chain->next) {
2488 btn = chain->aicon;
2489 if (btn->xindex == 0 && btn->yindex > 0 && btn->yindex < vcount)
2490 vmap[btn->yindex] = 1;
2491 else if (btn->yindex == 0 && btn->xindex < 0 && btn->xindex > -hcount)
2492 hmap[-btn->xindex] = 1;
2494 break;
2495 case C_SE:
2496 for (i = 0; i < dock->max_icons; i++) {
2497 btn = dock->icon_array[i];
2498 if (!btn)
2499 continue;
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;
2506 for (chain = scr->global_icons; chain != NULL; chain = chain->next) {
2507 btn = chain->aicon;
2508 if (btn->xindex == 0 && btn->yindex < 0 && btn->yindex > -vcount)
2509 vmap[-btn->yindex] = 1;
2510 else if (btn->yindex == 0 && btn->xindex > 0 && btn->xindex < hcount)
2511 hmap[btn->xindex] = 1;
2513 break;
2514 case C_SW:
2515 default:
2516 for (i = 0; i < dock->max_icons; i++) {
2517 btn = dock->icon_array[i];
2518 if (!btn)
2519 continue;
2521 if (btn->xindex == 0 && btn->yindex < 0 && btn->yindex > -vcount)
2522 vmap[-btn->yindex] = 1;
2523 else if (btn->yindex == 0 && btn->xindex < 0 && btn->xindex > -hcount)
2524 hmap[-btn->xindex] = 1;
2526 for (chain = scr->global_icons; chain != NULL; chain = chain->next) {
2527 btn = chain->aicon;
2528 if (btn->xindex == 0 && btn->yindex < 0 && btn->yindex > -vcount)
2529 vmap[-btn->yindex] = 1;
2530 else if (btn->yindex == 0 && btn->xindex < 0 && btn->xindex > -hcount)
2531 hmap[-btn->xindex] = 1;
2534 x = 0;
2535 y = 0;
2536 done = 0;
2537 /* search a vacant slot */
2538 for (i = 1; i < WMAX(vcount, hcount); i++) {
2539 if (i < vcount && vmap[i] == 0) {
2540 /* found a slot */
2541 x = 0;
2542 y = i;
2543 done = 1;
2544 break;
2545 } else if (i < hcount && hmap[i] == 0) {
2546 /* found a slot */
2547 x = i;
2548 y = 0;
2549 done = 1;
2550 break;
2553 wfree(vmap);
2554 wfree(hmap);
2555 /* If found a slot, translate and return */
2556 if (done) {
2557 if (corner == C_NW || corner == C_NE) {
2558 *y_pos = y;
2559 } else {
2560 *y_pos = -y;
2562 if (corner == C_NE || corner == C_SE) {
2563 *x_pos = x;
2564 } else {
2565 *x_pos = -x;
2567 return True;
2569 /* else, try to find a slot somewhere else */
2572 /* a map of mwidth x mwidth would be enough if we allowed icons to be
2573 * placed outside of screen */
2574 mwidth = (int)ceil(sqrt(dock->max_icons));
2576 /* In the worst case (the clip is in the corner of the screen),
2577 * the amount of icons that fit in the clip is smaller.
2578 * Double the map to get a safe value.
2580 mwidth += mwidth;
2582 r = (mwidth - 1) / 2;
2584 slot_map = wmalloc(mwidth * mwidth);
2585 memset(slot_map, 0, mwidth * mwidth);
2587 #define XY2OFS(x,y) (WMAX(abs(x),abs(y)) > r) ? 0 : (((y)+r)*(mwidth)+(x)+r)
2589 /* mark used slots in the map. If the slot falls outside the map
2590 * (for example, when all icons are placed in line), ignore them. */
2591 for (i = 0; i < dock->max_icons; i++) {
2592 btn = dock->icon_array[i];
2593 if (btn)
2594 slot_map[XY2OFS(btn->xindex, btn->yindex)] = 1;
2596 for (chain = scr->global_icons; chain != NULL; chain = chain->next) {
2597 slot_map[XY2OFS(chain->aicon->xindex, chain->aicon->yindex)] = 1;
2599 /* Find closest slot from the center that is free by scanning the
2600 * map from the center to outward in circular passes.
2601 * This will not result in a neat layout, but will be optimal
2602 * in the sense that there will not be holes left.
2604 done = 0;
2605 for (i = 1; i <= r && !done; i++) {
2606 int tx, ty;
2608 /* top and bottom parts of the ring */
2609 for (x = -i; x <= i && !done; x++) {
2610 tx = dock->x_pos + x * ICON_SIZE;
2611 y = -i;
2612 ty = dock->y_pos + y * ICON_SIZE;
2613 if (slot_map[XY2OFS(x, y)] == 0 && onScreen(scr, tx, ty, sx, ex, sy, ey)) {
2614 *x_pos = x;
2615 *y_pos = y;
2616 done = 1;
2617 break;
2619 y = i;
2620 ty = dock->y_pos + y * ICON_SIZE;
2621 if (slot_map[XY2OFS(x, y)] == 0 && onScreen(scr, tx, ty, sx, ex, sy, ey)) {
2622 *x_pos = x;
2623 *y_pos = y;
2624 done = 1;
2625 break;
2628 /* left and right parts of the ring */
2629 for (y = -i + 1; y <= i - 1; y++) {
2630 ty = dock->y_pos + y * ICON_SIZE;
2631 x = -i;
2632 tx = dock->x_pos + x * ICON_SIZE;
2633 if (slot_map[XY2OFS(x, y)] == 0 && onScreen(scr, tx, ty, sx, ex, sy, ey)) {
2634 *x_pos = x;
2635 *y_pos = y;
2636 done = 1;
2637 break;
2639 x = i;
2640 tx = dock->x_pos + x * ICON_SIZE;
2641 if (slot_map[XY2OFS(x, y)] == 0 && onScreen(scr, tx, ty, sx, ex, sy, ey)) {
2642 *x_pos = x;
2643 *y_pos = y;
2644 done = 1;
2645 break;
2649 wfree(slot_map);
2650 #undef XY2OFS
2651 return done;
2654 static void moveDock(WDock *dock, int new_x, int new_y)
2656 WAppIcon *btn;
2657 int i;
2659 dock->x_pos = new_x;
2660 dock->y_pos = new_y;
2661 for (i = 0; i < dock->max_icons; i++) {
2662 btn = dock->icon_array[i];
2663 if (btn) {
2664 btn->x_pos = new_x + btn->xindex * ICON_SIZE;
2665 btn->y_pos = new_y + btn->yindex * ICON_SIZE;
2666 XMoveWindow(dpy, btn->icon->core->window, btn->x_pos, btn->y_pos);
2671 static void swapDock(WDock *dock)
2673 WScreen *scr = dock->screen_ptr;
2674 WAppIcon *btn;
2675 int x, i;
2677 if (dock->on_right_side) {
2678 x = dock->x_pos = scr->scr_width - ICON_SIZE - DOCK_EXTRA_SPACE;
2679 } else {
2680 x = dock->x_pos = DOCK_EXTRA_SPACE;
2683 for (i = 0; i < dock->max_icons; i++) {
2684 btn = dock->icon_array[i];
2685 if (btn) {
2686 btn->x_pos = x;
2687 XMoveWindow(dpy, btn->icon->core->window, btn->x_pos, btn->y_pos);
2691 wScreenUpdateUsableArea(scr);
2694 static pid_t execCommand(WAppIcon *btn, char *command, WSavedState *state)
2696 WScreen *scr = btn->icon->core->screen_ptr;
2697 pid_t pid;
2698 char **argv;
2699 int argc;
2700 char *cmdline;
2702 cmdline = ExpandOptions(scr, command);
2704 if (scr->flags.dnd_data_convertion_status || !cmdline) {
2705 if (cmdline)
2706 wfree(cmdline);
2707 if (state)
2708 wfree(state);
2709 return 0;
2712 wtokensplit(cmdline, &argv, &argc);
2714 if (!argc) {
2715 if (cmdline)
2716 wfree(cmdline);
2717 if (state)
2718 wfree(state);
2719 return 0;
2722 if ((pid = fork()) == 0) {
2723 char **args;
2724 int i;
2726 SetupEnvironment(scr);
2728 #ifdef HAVE_SETSID
2729 setsid();
2730 #endif
2732 args = malloc(sizeof(char *) * (argc + 1));
2733 if (!args)
2734 exit(111);
2735 for (i = 0; i < argc; i++) {
2736 args[i] = argv[i];
2738 args[argc] = NULL;
2739 execvp(argv[0], args);
2740 exit(111);
2742 wtokenfree(argv, argc);
2744 if (pid > 0) {
2745 if (!state) {
2746 state = wmalloc(sizeof(WSavedState));
2747 memset(state, 0, sizeof(WSavedState));
2748 state->hidden = -1;
2749 state->miniaturized = -1;
2750 state->shaded = -1;
2751 if (btn->dock == scr->dock || btn->omnipresent)
2752 state->workspace = -1;
2753 else
2754 state->workspace = scr->current_workspace;
2756 wWindowAddSavedState(btn->wm_instance, btn->wm_class, cmdline, pid, state);
2757 wAddDeathHandler(pid, (WDeathHandler *) trackDeadProcess, btn->dock);
2758 } else if (state) {
2759 wfree(state);
2761 wfree(cmdline);
2762 return pid;
2765 void wDockHideIcons(WDock *dock)
2767 int i;
2769 if (dock == NULL)
2770 return;
2772 for (i = 1; i < dock->max_icons; i++) {
2773 if (dock->icon_array[i])
2774 XUnmapWindow(dpy, dock->icon_array[i]->icon->core->window);
2776 dock->mapped = 0;
2778 dockIconPaint(dock->icon_array[0]);
2781 void wDockShowIcons(WDock *dock)
2783 int i, newlevel;
2784 WAppIcon *btn;
2786 if (dock == NULL)
2787 return;
2789 btn = dock->icon_array[0];
2790 moveDock(dock, btn->x_pos, btn->y_pos);
2792 newlevel = dock->lowered ? WMNormalLevel : WMDockLevel;
2793 ChangeStackingLevel(btn->icon->core, newlevel);
2795 for (i = 1; i < dock->max_icons; i++) {
2796 if (dock->icon_array[i]) {
2797 MoveInStackListAbove(dock->icon_array[i]->icon->core, btn->icon->core);
2798 break;
2802 if (!dock->collapsed) {
2803 for (i = 1; i < dock->max_icons; i++) {
2804 if (dock->icon_array[i]) {
2805 XMapWindow(dpy, dock->icon_array[i]->icon->core->window);
2809 dock->mapped = 1;
2811 dockIconPaint(btn);
2814 void wDockLower(WDock *dock)
2816 int i;
2818 for (i = 0; i < dock->max_icons; i++) {
2819 if (dock->icon_array[i])
2820 wLowerFrame(dock->icon_array[i]->icon->core);
2824 void wDockRaise(WDock *dock)
2826 int i;
2828 for (i = dock->max_icons - 1; i >= 0; i--) {
2829 if (dock->icon_array[i])
2830 wRaiseFrame(dock->icon_array[i]->icon->core);
2834 void wDockRaiseLower(WDock *dock)
2836 if (!dock->icon_array[0]->icon->core->stacking->above
2837 || (dock->icon_array[0]->icon->core->stacking->window_level
2838 != dock->icon_array[0]->icon->core->stacking->above->stacking->window_level))
2839 wDockLower(dock);
2840 else
2841 wDockRaise(dock);
2844 void wDockFinishLaunch(WDock *dock, WAppIcon *icon)
2846 icon->launching = 0;
2847 icon->relaunching = 0;
2848 dockIconPaint(icon);
2851 WAppIcon *wDockFindIconForWindow(WDock *dock, Window window)
2853 WAppIcon *icon;
2854 int i;
2856 for (i = 0; i < dock->max_icons; i++) {
2857 icon = dock->icon_array[i];
2858 if (icon && icon->main_window == window)
2859 return icon;
2861 return NULL;
2864 void wDockTrackWindowLaunch(WDock *dock, Window window)
2866 WAppIcon *icon;
2867 char *wm_class, *wm_instance;
2868 int i;
2869 Bool firstPass = True;
2870 Bool found = False;
2871 char *command = NULL;
2873 command = GetCommandForWindow(window);
2875 if (!PropGetWMClass(window, &wm_class, &wm_instance) || (!wm_class && !wm_instance)) {
2877 if (command)
2878 wfree(command);
2879 return;
2882 retry:
2883 for (i = 0; i < dock->max_icons; i++) {
2884 icon = dock->icon_array[i];
2885 if (!icon)
2886 continue;
2888 /* app is already attached to icon */
2889 if (icon->main_window == window) {
2890 found = True;
2891 break;
2894 if ((icon->wm_instance || icon->wm_class)
2895 && (icon->launching || !icon->running)) {
2897 if (icon->wm_instance && wm_instance && strcmp(icon->wm_instance, wm_instance) != 0) {
2898 continue;
2900 if (icon->wm_class && wm_class && strcmp(icon->wm_class, wm_class) != 0) {
2901 continue;
2903 if (firstPass && command && strcmp(icon->command, command) != 0) {
2904 continue;
2907 if (!icon->relaunching) {
2908 WApplication *wapp;
2910 /* Possibly an application that was docked with dockit,
2911 * but the user did not update WMState to indicate that
2912 * it was docked by force */
2913 wapp = wApplicationOf(window);
2914 if (!wapp) {
2915 icon->forced_dock = 1;
2916 icon->running = 0;
2918 if (!icon->forced_dock) {
2919 icon->main_window = window;
2922 found = True;
2923 if (!wPreferences.no_animations && !icon->launching &&
2924 !dock->screen_ptr->flags.startup && !dock->collapsed) {
2925 WAppIcon *aicon;
2926 int x0, y0;
2928 icon->launching = 1;
2929 dockIconPaint(icon);
2931 aicon = wAppIconCreateForDock(dock->screen_ptr, NULL,
2932 wm_instance, wm_class, TILE_NORMAL);
2933 /* XXX: can: aicon->icon == NULL ? */
2934 PlaceIcon(dock->screen_ptr, &x0, &y0, wGetHeadForWindow(aicon->icon->owner));
2935 wAppIconMove(aicon, x0, y0);
2936 /* Should this always be lowered? -Dan */
2937 if (dock->lowered)
2938 wLowerFrame(aicon->icon->core);
2939 XMapWindow(dpy, aicon->icon->core->window);
2940 aicon->launching = 1;
2941 wAppIconPaint(aicon);
2942 SlideWindow(aicon->icon->core->window, x0, y0, icon->x_pos, icon->y_pos);
2943 XUnmapWindow(dpy, aicon->icon->core->window);
2944 wAppIconDestroy(aicon);
2946 wDockFinishLaunch(dock, icon);
2947 break;
2951 if (firstPass && !found) {
2952 firstPass = False;
2953 goto retry;
2956 if (command)
2957 wfree(command);
2959 if (wm_class)
2960 free(wm_class);
2961 if (wm_instance)
2962 free(wm_instance);
2965 void wClipUpdateForWorkspaceChange(WScreen *scr, int workspace)
2967 if (!wPreferences.flags.noclip) {
2968 scr->clip_icon->dock = scr->workspaces[workspace]->clip;
2969 if (scr->current_workspace != workspace) {
2970 WDock *old_clip = scr->workspaces[scr->current_workspace]->clip;
2971 WAppIconChain *chain = scr->global_icons;
2973 while (chain) {
2974 moveIconBetweenDocks(chain->aicon->dock,
2975 scr->workspaces[workspace]->clip,
2976 chain->aicon, chain->aicon->xindex, chain->aicon->yindex);
2977 if (scr->workspaces[workspace]->clip->collapsed)
2978 XUnmapWindow(dpy, chain->aicon->icon->core->window);
2979 chain = chain->next;
2982 wDockHideIcons(old_clip);
2983 if (old_clip->auto_raise_lower) {
2984 if (old_clip->auto_raise_magic) {
2985 WMDeleteTimerHandler(old_clip->auto_raise_magic);
2986 old_clip->auto_raise_magic = NULL;
2988 wDockLower(old_clip);
2990 if (old_clip->auto_collapse) {
2991 if (old_clip->auto_expand_magic) {
2992 WMDeleteTimerHandler(old_clip->auto_expand_magic);
2993 old_clip->auto_expand_magic = NULL;
2995 old_clip->collapsed = 1;
2997 wDockShowIcons(scr->workspaces[workspace]->clip);
3002 static void trackDeadProcess(pid_t pid, unsigned char status, WDock *dock)
3004 WAppIcon *icon;
3005 int i;
3007 for (i = 0; i < dock->max_icons; i++) {
3008 icon = dock->icon_array[i];
3009 if (!icon)
3010 continue;
3012 if (icon->launching && icon->pid == pid) {
3013 if (!icon->relaunching) {
3014 icon->running = 0;
3015 icon->main_window = None;
3017 wDockFinishLaunch(dock, icon);
3018 icon->pid = 0;
3019 if (status == 111) {
3020 char msg[PATH_MAX];
3021 char *cmd;
3023 #ifdef XDND
3024 if (icon->drop_launch)
3025 cmd = icon->dnd_command;
3026 else
3027 #endif
3028 if (icon->paste_launch)
3029 cmd = icon->paste_command;
3030 else
3031 cmd = icon->command;
3033 snprintf(msg, sizeof(msg), _("Could not execute command \"%s\""), cmd);
3035 wMessageDialog(dock->screen_ptr, _("Error"), msg, _("OK"), NULL, NULL);
3037 break;
3042 static void toggleLowered(WDock *dock)
3044 WAppIcon *tmp;
3045 int newlevel, i;
3047 /* lower/raise Dock */
3048 if (!dock->lowered) {
3049 newlevel = WMNormalLevel;
3050 dock->lowered = 1;
3051 } else {
3052 newlevel = WMDockLevel;
3053 dock->lowered = 0;
3056 for (i = 0; i < dock->max_icons; i++) {
3057 tmp = dock->icon_array[i];
3058 if (!tmp)
3059 continue;
3061 ChangeStackingLevel(tmp->icon->core, newlevel);
3062 if (dock->lowered)
3063 wLowerFrame(tmp->icon->core);
3066 if (dock->type == WM_DOCK)
3067 wScreenUpdateUsableArea(dock->screen_ptr);
3070 static void toggleCollapsed(WDock *dock)
3072 if (dock->collapsed) {
3073 dock->collapsed = 0;
3074 wDockShowIcons(dock);
3075 } else {
3076 dock->collapsed = 1;
3077 wDockHideIcons(dock);
3081 static void openDockMenu(WDock *dock, WAppIcon *aicon, XEvent *event)
3083 WScreen *scr = dock->screen_ptr;
3084 WObjDescriptor *desc;
3085 WMenuEntry *entry;
3086 WApplication *wapp = NULL;
3087 int index = 0;
3088 int x_pos;
3089 int n_selected;
3090 int appIsRunning = aicon->running && aicon->icon && aicon->icon->owner;
3092 if (dock->type == WM_DOCK) {
3093 /* keep on top */
3094 entry = dock->menu->entries[index];
3095 entry->flags.indicator_on = !dock->lowered;
3096 entry->clientdata = dock;
3097 dock->menu->flags.realized = 0;
3098 } else {
3099 /* clip options */
3100 if (scr->clip_options)
3101 updateClipOptionsMenu(scr->clip_options, dock);
3103 n_selected = numberOfSelectedIcons(dock);
3105 /* Rename Workspace */
3106 entry = dock->menu->entries[++index];
3107 if (aicon == scr->clip_icon) {
3108 entry->callback = renameCallback;
3109 entry->clientdata = dock;
3110 entry->flags.indicator = 0;
3111 entry->text = _("Rename Workspace");
3112 } else {
3113 entry->callback = omnipresentCallback;
3114 entry->clientdata = aicon;
3115 if (n_selected > 0) {
3116 entry->flags.indicator = 0;
3117 entry->text = _("Toggle Omnipresent");
3118 } else {
3119 entry->flags.indicator = 1;
3120 entry->flags.indicator_on = aicon->omnipresent;
3121 entry->flags.indicator_type = MI_CHECK;
3122 entry->text = _("Omnipresent");
3126 /* select/unselect icon */
3127 entry = dock->menu->entries[++index];
3128 entry->clientdata = aicon;
3129 entry->flags.indicator_on = aicon->icon->selected;
3130 wMenuSetEnabled(dock->menu, index, aicon != scr->clip_icon);
3132 /* select/unselect all icons */
3133 entry = dock->menu->entries[++index];
3134 entry->clientdata = aicon;
3135 if (n_selected > 0)
3136 entry->text = _("Unselect All Icons");
3137 else
3138 entry->text = _("Select All Icons");
3139 wMenuSetEnabled(dock->menu, index, dock->icon_count > 1);
3141 /* keep icon(s) */
3142 entry = dock->menu->entries[++index];
3143 entry->clientdata = aicon;
3144 if (n_selected > 1)
3145 entry->text = _("Keep Icons");
3146 else
3147 entry->text = _("Keep Icon");
3148 wMenuSetEnabled(dock->menu, index, dock->icon_count > 1);
3150 /* this is the workspace submenu part */
3151 entry = dock->menu->entries[++index];
3152 if (n_selected > 1)
3153 entry->text = _("Move Icons To");
3154 else
3155 entry->text = _("Move Icon To");
3156 if (scr->clip_submenu)
3157 updateWorkspaceMenu(scr->clip_submenu, aicon);
3158 wMenuSetEnabled(dock->menu, index, !aicon->omnipresent);
3160 /* remove icon(s) */
3161 entry = dock->menu->entries[++index];
3162 entry->clientdata = aicon;
3163 if (n_selected > 1)
3164 entry->text = _("Remove Icons");
3165 else
3166 entry->text = _("Remove Icon");
3167 wMenuSetEnabled(dock->menu, index, dock->icon_count > 1);
3169 /* attract icon(s) */
3170 entry = dock->menu->entries[++index];
3171 entry->clientdata = aicon;
3173 dock->menu->flags.realized = 0;
3174 wMenuRealize(dock->menu);
3177 if (aicon->icon->owner) {
3178 wapp = wApplicationOf(aicon->icon->owner->main_window);
3179 } else {
3180 wapp = NULL;
3183 /* launch */
3184 entry = dock->menu->entries[++index];
3185 entry->clientdata = aicon;
3186 wMenuSetEnabled(dock->menu, index, aicon->command != NULL);
3188 /* unhide here */
3189 entry = dock->menu->entries[++index];
3190 entry->clientdata = aicon;
3191 if (wapp && wapp->flags.hidden) {
3192 entry->text = _("Unhide Here");
3193 } else {
3194 entry->text = _("Bring Here");
3196 wMenuSetEnabled(dock->menu, index, appIsRunning);
3198 /* hide */
3199 entry = dock->menu->entries[++index];
3200 entry->clientdata = aicon;
3201 if (wapp && wapp->flags.hidden) {
3202 entry->text = _("Unhide");
3203 } else {
3204 entry->text = _("Hide");
3206 wMenuSetEnabled(dock->menu, index, appIsRunning);
3208 /* settings */
3209 entry = dock->menu->entries[++index];
3210 entry->clientdata = aicon;
3211 wMenuSetEnabled(dock->menu, index, !aicon->editing && !wPreferences.flags.noupdates);
3213 /* kill */
3214 entry = dock->menu->entries[++index];
3215 entry->clientdata = aicon;
3216 wMenuSetEnabled(dock->menu, index, appIsRunning);
3218 if (!dock->menu->flags.realized)
3219 wMenuRealize(dock->menu);
3221 if (dock->type == WM_CLIP) {
3222 /*x_pos = event->xbutton.x_root+2; */
3223 x_pos = event->xbutton.x_root - dock->menu->frame->core->width / 2 - 1;
3224 if (x_pos < 0) {
3225 x_pos = 0;
3226 } else if (x_pos + dock->menu->frame->core->width > scr->scr_width - 2) {
3227 x_pos = scr->scr_width - dock->menu->frame->core->width - 4;
3229 } else {
3230 x_pos = dock->on_right_side ? scr->scr_width - dock->menu->frame->core->width - 3 : 0;
3233 wMenuMapAt(dock->menu, x_pos, event->xbutton.y_root + 2, False);
3235 /* allow drag select */
3236 event->xany.send_event = True;
3237 desc = &dock->menu->menu->descriptor;
3238 (*desc->handle_mousedown) (desc, event);
3241 /******************************************************************/
3242 static void iconDblClick(WObjDescriptor *desc, XEvent *event)
3244 WAppIcon *btn = desc->parent;
3245 WDock *dock = btn->dock;
3246 WApplication *wapp = NULL;
3247 int unhideHere = 0;
3249 if (btn->icon->owner && !(event->xbutton.state & ControlMask)) {
3250 wapp = wApplicationOf(btn->icon->owner->main_window);
3252 assert(wapp != NULL);
3254 unhideHere = (event->xbutton.state & ShiftMask);
3256 /* go to the last workspace that the user worked on the app */
3257 if (wapp->last_workspace != dock->screen_ptr->current_workspace && !unhideHere) {
3258 wWorkspaceChange(dock->screen_ptr, wapp->last_workspace);
3261 wUnhideApplication(wapp, event->xbutton.button == Button2, unhideHere);
3263 if (event->xbutton.state & MOD_MASK) {
3264 wHideOtherApplications(btn->icon->owner);
3266 } else {
3267 if (event->xbutton.button == Button1) {
3269 if (event->xbutton.state & MOD_MASK) {
3270 /* raise/lower dock */
3271 toggleLowered(dock);
3272 } else if (btn == dock->screen_ptr->clip_icon) {
3273 if (getClipButton(event->xbutton.x, event->xbutton.y) == CLIP_IDLE)
3274 toggleCollapsed(dock);
3275 else
3276 handleClipChangeWorkspace(dock->screen_ptr, event);
3277 } else if (btn->command) {
3278 if (!btn->launching && (!btn->running || (event->xbutton.state & ControlMask)))
3279 launchDockedApplication(btn, False);
3285 static void handleDockMove(WDock *dock, WAppIcon *aicon, XEvent *event)
3287 WScreen *scr = dock->screen_ptr;
3288 int ofs_x = event->xbutton.x, ofs_y = event->xbutton.y;
3289 int x, y;
3290 XEvent ev;
3291 int grabbed = 0, swapped = 0, done;
3292 Pixmap ghost = None;
3293 int superfluous = wPreferences.superfluous; /* we catch it to avoid problems */
3295 if (XGrabPointer(dpy, aicon->icon->core->window, True, ButtonMotionMask
3296 | ButtonReleaseMask | ButtonPressMask, GrabModeAsync,
3297 GrabModeAsync, None, None, CurrentTime) != GrabSuccess) {
3298 wwarning("pointer grab failed for dock move");
3300 y = 0;
3301 for (x = 0; x < dock->max_icons; x++) {
3302 if (dock->icon_array[x] != NULL && dock->icon_array[x]->yindex > y)
3303 y = dock->icon_array[x]->yindex;
3305 y++;
3306 XResizeWindow(dpy, scr->dock_shadow, ICON_SIZE, ICON_SIZE * y);
3308 done = 0;
3309 while (!done) {
3310 WMMaskEvent(dpy, PointerMotionMask | ButtonReleaseMask | ButtonPressMask
3311 | ButtonMotionMask | ExposureMask | EnterWindowMask, &ev);
3312 switch (ev.type) {
3313 case Expose:
3314 WMHandleEvent(&ev);
3315 break;
3317 case EnterNotify:
3318 /* It means the cursor moved so fast that it entered
3319 * something else (if moving slowly, it would have
3320 * stayed in the dock that is being moved. Ignore such
3321 * "spurious" EnterNotifiy's */
3322 break;
3324 case MotionNotify:
3325 if (!grabbed) {
3326 if (abs(ofs_x - ev.xmotion.x) >= MOVE_THRESHOLD
3327 || abs(ofs_y - ev.xmotion.y) >= MOVE_THRESHOLD) {
3328 XChangeActivePointerGrab(dpy, ButtonMotionMask
3329 | ButtonReleaseMask | ButtonPressMask,
3330 wCursor[WCUR_MOVE], CurrentTime);
3331 grabbed = 1;
3333 break;
3335 if (dock->type == WM_CLIP) {
3336 x = ev.xmotion.x_root - ofs_x;
3337 y = ev.xmotion.y_root - ofs_y;
3338 wScreenKeepInside(scr, &x, &y, ICON_SIZE, ICON_SIZE);
3340 moveDock(dock, x, y);
3341 } else {
3342 /* move vertically if pointer is inside the dock */
3343 if ((dock->on_right_side && ev.xmotion.x_root >= dock->x_pos - ICON_SIZE)
3344 || (!dock->on_right_side && ev.xmotion.x_root <= dock->x_pos + ICON_SIZE * 2)) {
3346 x = ev.xmotion.x_root - ofs_x;
3347 y = ev.xmotion.y_root - ofs_y;
3348 wScreenKeepInside(scr, &x, &y, ICON_SIZE, ICON_SIZE);
3349 moveDock(dock, dock->x_pos, y);
3351 /* move horizontally to change sides */
3352 x = ev.xmotion.x_root - ofs_x;
3353 if (!dock->on_right_side) {
3355 /* is on left */
3357 if (ev.xmotion.x_root > dock->x_pos + ICON_SIZE * 2) {
3358 XMoveWindow(dpy, scr->dock_shadow, scr->scr_width - ICON_SIZE
3359 - DOCK_EXTRA_SPACE - 1, dock->y_pos);
3360 if (superfluous && ghost == None) {
3361 ghost = MakeGhostDock(dock, dock->x_pos,
3362 scr->scr_width - ICON_SIZE
3363 - DOCK_EXTRA_SPACE - 1, dock->y_pos);
3364 XSetWindowBackgroundPixmap(dpy, scr->dock_shadow, ghost);
3365 XClearWindow(dpy, scr->dock_shadow);
3367 XMapRaised(dpy, scr->dock_shadow);
3368 swapped = 1;
3369 } else {
3370 if (superfluous && ghost != None) {
3371 XFreePixmap(dpy, ghost);
3372 ghost = None;
3374 XUnmapWindow(dpy, scr->dock_shadow);
3375 swapped = 0;
3377 } else {
3378 /* is on right */
3379 if (ev.xmotion.x_root < dock->x_pos - ICON_SIZE) {
3380 XMoveWindow(dpy, scr->dock_shadow, DOCK_EXTRA_SPACE, dock->y_pos);
3381 if (superfluous && ghost == None) {
3382 ghost = MakeGhostDock(dock, dock->x_pos,
3383 DOCK_EXTRA_SPACE, dock->y_pos);
3384 XSetWindowBackgroundPixmap(dpy, scr->dock_shadow, ghost);
3385 XClearWindow(dpy, scr->dock_shadow);
3387 XMapRaised(dpy, scr->dock_shadow);
3388 swapped = -1;
3389 } else {
3390 XUnmapWindow(dpy, scr->dock_shadow);
3391 swapped = 0;
3392 if (superfluous && ghost != None) {
3393 XFreePixmap(dpy, ghost);
3394 ghost = None;
3399 break;
3401 case ButtonPress:
3402 break;
3404 case ButtonRelease:
3405 if (ev.xbutton.button != event->xbutton.button)
3406 break;
3407 XUngrabPointer(dpy, CurrentTime);
3408 XUnmapWindow(dpy, scr->dock_shadow);
3409 XResizeWindow(dpy, scr->dock_shadow, ICON_SIZE, ICON_SIZE);
3410 if (dock->type == WM_DOCK) {
3411 if (swapped != 0) {
3412 if (swapped > 0)
3413 dock->on_right_side = 1;
3414 else
3415 dock->on_right_side = 0;
3416 swapDock(dock);
3417 wArrangeIcons(scr, False);
3420 done = 1;
3421 break;
3424 if (superfluous) {
3425 if (ghost != None)
3426 XFreePixmap(dpy, ghost);
3427 XSetWindowBackground(dpy, scr->dock_shadow, scr->white_pixel);
3431 static Bool handleIconMove(WDock *dock, WAppIcon *aicon, XEvent *event)
3433 WScreen *scr = dock->screen_ptr;
3434 Window wins[2];
3435 WIcon *icon = aicon->icon;
3436 WDock *dock2 = NULL, *last_dock = dock, *clip = NULL;
3437 int ondock, grabbed = 0, change_dock = 0, collapsed = 0;
3438 XEvent ev;
3439 int x = aicon->x_pos, y = aicon->y_pos;
3440 int ofs_x = event->xbutton.x, ofs_y = event->xbutton.y;
3441 int shad_x = x, shad_y = y;
3442 int ix = aicon->xindex, iy = aicon->yindex;
3443 int tmp;
3444 Pixmap ghost = None;
3445 Bool docked;
3446 int superfluous = wPreferences.superfluous; /* we catch it to avoid problems */
3447 int omnipresent = aicon->omnipresent; /* this must be cached!!! */
3448 Bool hasMoved = False;
3450 if (wPreferences.flags.noupdates)
3451 return hasMoved;
3453 if (XGrabPointer(dpy, icon->core->window, True, ButtonMotionMask
3454 | ButtonReleaseMask | ButtonPressMask, GrabModeAsync,
3455 GrabModeAsync, None, None, CurrentTime) != GrabSuccess) {
3458 if (!(event->xbutton.state & MOD_MASK))
3459 wRaiseFrame(icon->core);
3461 if (!wPreferences.flags.noclip)
3462 clip = scr->workspaces[scr->current_workspace]->clip;
3464 if (dock == scr->dock && !wPreferences.flags.noclip)
3465 dock2 = clip;
3466 else if (dock != scr->dock && !wPreferences.flags.nodock)
3467 dock2 = scr->dock;
3469 wins[0] = icon->core->window;
3470 wins[1] = scr->dock_shadow;
3471 XRestackWindows(dpy, wins, 2);
3472 XMoveResizeWindow(dpy, scr->dock_shadow, aicon->x_pos, aicon->y_pos, ICON_SIZE, ICON_SIZE);
3473 if (superfluous) {
3474 if (icon->pixmap != None)
3475 ghost = MakeGhostIcon(scr, icon->pixmap);
3476 else
3477 ghost = MakeGhostIcon(scr, icon->core->window);
3479 XSetWindowBackgroundPixmap(dpy, scr->dock_shadow, ghost);
3480 XClearWindow(dpy, scr->dock_shadow);
3482 XMapWindow(dpy, scr->dock_shadow);
3484 ondock = 1;
3486 while (1) {
3487 XMaskEvent(dpy, PointerMotionMask | ButtonReleaseMask | ButtonPressMask
3488 | ButtonMotionMask | ExposureMask, &ev);
3489 switch (ev.type) {
3490 case Expose:
3491 WMHandleEvent(&ev);
3492 break;
3494 case MotionNotify:
3495 hasMoved = True;
3496 if (!grabbed) {
3497 if (abs(ofs_x - ev.xmotion.x) >= MOVE_THRESHOLD
3498 || abs(ofs_y - ev.xmotion.y) >= MOVE_THRESHOLD) {
3499 XChangeActivePointerGrab(dpy, ButtonMotionMask
3500 | ButtonReleaseMask | ButtonPressMask,
3501 wCursor[WCUR_MOVE], CurrentTime);
3502 grabbed = 1;
3503 } else {
3504 break;
3508 if (omnipresent) {
3509 int i;
3510 for (i = 0; i < scr->workspace_count; i++) {
3511 if (i == scr->current_workspace)
3512 continue;
3513 wDockShowIcons(scr->workspaces[i]->clip);
3517 x = ev.xmotion.x_root - ofs_x;
3518 y = ev.xmotion.y_root - ofs_y;
3519 tmp = wDockSnapIcon(dock, aicon, x, y, &ix, &iy, True);
3520 if (tmp && dock2) {
3521 change_dock = 0;
3522 if (last_dock != dock && collapsed) {
3523 last_dock->collapsed = 1;
3524 wDockHideIcons(last_dock);
3525 collapsed = 0;
3527 if (!collapsed && (collapsed = dock->collapsed)) {
3528 dock->collapsed = 0;
3529 wDockShowIcons(dock);
3531 if (dock->auto_raise_lower)
3532 wDockRaise(dock);
3533 last_dock = dock;
3534 } else if (dock2) {
3535 tmp = wDockSnapIcon(dock2, aicon, x, y, &ix, &iy, False);
3536 if (tmp) {
3537 change_dock = 1;
3538 if (last_dock != dock2 && collapsed) {
3539 last_dock->collapsed = 1;
3540 wDockHideIcons(last_dock);
3541 collapsed = 0;
3543 if (!collapsed && (collapsed = dock2->collapsed)) {
3544 dock2->collapsed = 0;
3545 wDockShowIcons(dock2);
3547 if (dock2->auto_raise_lower)
3548 wDockRaise(dock2);
3549 last_dock = dock2;
3552 if (aicon->launching || aicon->lock || (aicon->running && !(ev.xmotion.state & MOD_MASK))
3553 || (!aicon->running && tmp)) {
3554 shad_x = last_dock->x_pos + ix * wPreferences.icon_size;
3555 shad_y = last_dock->y_pos + iy * wPreferences.icon_size;
3557 XMoveWindow(dpy, scr->dock_shadow, shad_x, shad_y);
3559 if (!ondock) {
3560 XMapWindow(dpy, scr->dock_shadow);
3562 ondock = 1;
3563 } else {
3564 if (ondock) {
3565 XUnmapWindow(dpy, scr->dock_shadow);
3567 ondock = 0;
3569 XMoveWindow(dpy, icon->core->window, x, y);
3570 break;
3572 case ButtonPress:
3573 break;
3575 case ButtonRelease:
3576 if (ev.xbutton.button != event->xbutton.button)
3577 break;
3578 XUngrabPointer(dpy, CurrentTime);
3579 if (ondock) {
3580 SlideWindow(icon->core->window, x, y, shad_x, shad_y);
3581 XUnmapWindow(dpy, scr->dock_shadow);
3582 if (!change_dock) {
3583 reattachIcon(dock, aicon, ix, iy);
3584 if (clip && dock != clip && clip->auto_raise_lower)
3585 wDockLower(clip);
3586 } else {
3587 docked = moveIconBetweenDocks(dock, dock2, aicon, ix, iy);
3588 if (!docked) {
3589 /* Slide it back if dock rejected it */
3590 SlideWindow(icon->core->window, x, y, aicon->x_pos, aicon->y_pos);
3591 reattachIcon(dock, aicon, aicon->xindex, aicon->yindex);
3593 if (last_dock->type == WM_CLIP && last_dock->auto_collapse) {
3594 collapsed = 0;
3597 } else {
3598 aicon->x_pos = x;
3599 aicon->y_pos = y;
3600 if (superfluous) {
3601 if (!aicon->running && !wPreferences.no_animations) {
3602 /* We need to deselect it, even if is deselected in
3603 * wDockDetach(), because else DoKaboom() will fail.
3605 if (aicon->icon->selected)
3606 wIconSelect(aicon->icon);
3608 DoKaboom(scr, aicon->icon->core->window, x, y);
3611 if (clip && clip->auto_raise_lower)
3612 wDockLower(clip);
3613 wDockDetach(dock, aicon);
3615 if (collapsed) {
3616 last_dock->collapsed = 1;
3617 wDockHideIcons(last_dock);
3618 collapsed = 0;
3620 if (superfluous) {
3621 if (ghost != None)
3622 XFreePixmap(dpy, ghost);
3623 XSetWindowBackground(dpy, scr->dock_shadow, scr->white_pixel);
3625 if (omnipresent) {
3626 int i;
3627 for (i = 0; i < scr->workspace_count; i++) {
3628 if (i == scr->current_workspace)
3629 continue;
3630 wDockHideIcons(scr->workspaces[i]->clip);
3633 return hasMoved;;
3636 return False; /* never reached */
3639 static int getClipButton(int px, int py)
3641 int pt = (CLIP_BUTTON_SIZE + 2) * ICON_SIZE / 64;
3643 if (px < 0 || py < 0 || px >= ICON_SIZE || py >= ICON_SIZE)
3644 return CLIP_IDLE;
3646 if (py <= pt - ((int)ICON_SIZE - 1 - px))
3647 return CLIP_FORWARD;
3648 else if (px <= pt - ((int)ICON_SIZE - 1 - py))
3649 return CLIP_REWIND;
3651 return CLIP_IDLE;
3654 static void handleClipChangeWorkspace(WScreen *scr, XEvent *event)
3656 XEvent ev;
3657 int done, direction, new_ws;
3658 int new_dir;
3659 WDock *clip = scr->clip_icon->dock;
3661 direction = getClipButton(event->xbutton.x, event->xbutton.y);
3663 clip->lclip_button_pushed = direction == CLIP_REWIND;
3664 clip->rclip_button_pushed = direction == CLIP_FORWARD;
3666 wClipIconPaint(scr->clip_icon);
3667 done = 0;
3668 while (!done) {
3669 WMMaskEvent(dpy, ExposureMask | ButtonMotionMask | ButtonReleaseMask | ButtonPressMask, &ev);
3670 switch (ev.type) {
3671 case Expose:
3672 WMHandleEvent(&ev);
3673 break;
3675 case MotionNotify:
3676 new_dir = getClipButton(ev.xmotion.x, ev.xmotion.y);
3677 if (new_dir != direction) {
3678 direction = new_dir;
3679 clip->lclip_button_pushed = direction == CLIP_REWIND;
3680 clip->rclip_button_pushed = direction == CLIP_FORWARD;
3681 wClipIconPaint(scr->clip_icon);
3683 break;
3685 case ButtonPress:
3686 break;
3688 case ButtonRelease:
3689 if (ev.xbutton.button == event->xbutton.button)
3690 done = 1;
3694 clip->lclip_button_pushed = 0;
3695 clip->rclip_button_pushed = 0;
3697 new_ws = wPreferences.ws_advance || (event->xbutton.state & ControlMask);
3699 if (direction == CLIP_FORWARD) {
3700 if (scr->current_workspace < scr->workspace_count - 1)
3701 wWorkspaceChange(scr, scr->current_workspace + 1);
3702 else if (new_ws && scr->current_workspace < MAX_WORKSPACES - 1)
3703 wWorkspaceChange(scr, scr->current_workspace + 1);
3704 else if (wPreferences.ws_cycle)
3705 wWorkspaceChange(scr, 0);
3706 } else if (direction == CLIP_REWIND) {
3707 if (scr->current_workspace > 0)
3708 wWorkspaceChange(scr, scr->current_workspace - 1);
3709 else if (scr->current_workspace == 0 && wPreferences.ws_cycle)
3710 wWorkspaceChange(scr, scr->workspace_count - 1);
3713 wClipIconPaint(scr->clip_icon);
3716 static void iconMouseDown(WObjDescriptor *desc, XEvent *event)
3718 WAppIcon *aicon = desc->parent;
3719 WDock *dock = aicon->dock;
3720 WScreen *scr = aicon->icon->core->screen_ptr;
3722 if (aicon->editing || WCHECK_STATE(WSTATE_MODAL))
3723 return;
3725 scr->last_dock = dock;
3727 if (dock->menu->flags.mapped)
3728 wMenuUnmap(dock->menu);
3730 if (IsDoubleClick(scr, event)) {
3731 /* double-click was not in the main clip icon */
3732 if (dock->type != WM_CLIP || aicon->xindex != 0 || aicon->yindex != 0
3733 || getClipButton(event->xbutton.x, event->xbutton.y) == CLIP_IDLE) {
3734 iconDblClick(desc, event);
3735 return;
3739 if (event->xbutton.button == Button1) {
3740 if (event->xbutton.state & MOD_MASK)
3741 wDockLower(dock);
3742 else
3743 wDockRaise(dock);
3745 if ((event->xbutton.state & ShiftMask) && aicon != scr->clip_icon && dock->type != WM_DOCK) {
3746 wIconSelect(aicon->icon);
3747 return;
3750 if (aicon->yindex == 0 && aicon->xindex == 0) {
3751 if (getClipButton(event->xbutton.x, event->xbutton.y) != CLIP_IDLE
3752 && dock->type == WM_CLIP)
3753 handleClipChangeWorkspace(scr, event);
3754 else
3755 handleDockMove(dock, aicon, event);
3756 } else {
3757 Bool hasMoved = handleIconMove(dock, aicon, event);
3758 if (wPreferences.single_click && !hasMoved)
3759 iconDblClick(desc, event);
3761 } else if (event->xbutton.button == Button2 && dock->type == WM_CLIP && aicon == scr->clip_icon) {
3762 if (!scr->clip_ws_menu) {
3763 scr->clip_ws_menu = wWorkspaceMenuMake(scr, False);
3765 if (scr->clip_ws_menu) {
3766 WMenu *wsMenu = scr->clip_ws_menu;
3767 int xpos;
3769 wWorkspaceMenuUpdate(scr, wsMenu);
3771 xpos = event->xbutton.x_root - wsMenu->frame->core->width / 2 - 1;
3772 if (xpos < 0) {
3773 xpos = 0;
3774 } else if (xpos + wsMenu->frame->core->width > scr->scr_width - 2) {
3775 xpos = scr->scr_width - wsMenu->frame->core->width - 4;
3777 wMenuMapAt(wsMenu, xpos, event->xbutton.y_root + 2, False);
3779 desc = &wsMenu->menu->descriptor;
3780 event->xany.send_event = True;
3781 (*desc->handle_mousedown) (desc, event);
3783 } else if (event->xbutton.button == Button2 && dock->type == WM_CLIP &&
3784 (event->xbutton.state & ShiftMask) && aicon != scr->clip_icon) {
3785 wClipMakeIconOmnipresent(aicon, !aicon->omnipresent);
3786 } else if (event->xbutton.button == Button3) {
3787 if (event->xbutton.send_event &&
3788 XGrabPointer(dpy, aicon->icon->core->window, True, ButtonMotionMask
3789 | ButtonReleaseMask | ButtonPressMask, GrabModeAsync,
3790 GrabModeAsync, None, None, CurrentTime) != GrabSuccess) {
3791 wwarning("pointer grab failed for dockicon menu");
3792 return;
3795 openDockMenu(dock, aicon, event);
3796 } else if (event->xbutton.button == Button2) {
3797 WAppIcon *btn = desc->parent;
3799 if (!btn->launching && (!btn->running || (event->xbutton.state & ControlMask))) {
3800 launchDockedApplication(btn, True);
3805 static void clipEnterNotify(WObjDescriptor *desc, XEvent *event)
3807 WAppIcon *btn = (WAppIcon *) desc->parent;
3808 WDock *dock;
3809 WScreen *scr;
3811 assert(event->type == EnterNotify);
3813 if (desc->parent_type != WCLASS_DOCK_ICON)
3814 return;
3816 scr = btn->icon->core->screen_ptr;
3817 if (!btn->omnipresent)
3818 dock = btn->dock;
3819 else
3820 dock = scr->workspaces[scr->current_workspace]->clip;
3822 if (!dock || dock->type != WM_CLIP)
3823 return;
3825 /* The auto raise/lower code */
3826 if (dock->auto_lower_magic) {
3827 WMDeleteTimerHandler(dock->auto_lower_magic);
3828 dock->auto_lower_magic = NULL;
3830 if (dock->auto_raise_lower && !dock->auto_raise_magic) {
3831 dock->auto_raise_magic = WMAddTimerHandler(AUTO_RAISE_DELAY, clipAutoRaise, (void *)dock);
3834 /* The auto expand/collapse code */
3835 if (dock->auto_collapse_magic) {
3836 WMDeleteTimerHandler(dock->auto_collapse_magic);
3837 dock->auto_collapse_magic = NULL;
3839 if (dock->auto_collapse && !dock->auto_expand_magic) {
3840 dock->auto_expand_magic = WMAddTimerHandler(AUTO_EXPAND_DELAY, clipAutoExpand, (void *)dock);
3844 static void clipLeave(WDock *dock)
3846 XEvent event;
3847 WObjDescriptor *desc = NULL;
3849 if (!dock || dock->type != WM_CLIP)
3850 return;
3852 if (XCheckTypedEvent(dpy, EnterNotify, &event) != False) {
3853 if (XFindContext(dpy, event.xcrossing.window, wWinContext,
3854 (XPointer *) & desc) != XCNOENT
3855 && desc && desc->parent_type == WCLASS_DOCK_ICON
3856 && ((WAppIcon *) desc->parent)->dock && ((WAppIcon *) desc->parent)->dock->type == WM_CLIP) {
3857 /* We didn't left the Clip yet */
3858 XPutBackEvent(dpy, &event);
3859 return;
3862 XPutBackEvent(dpy, &event);
3863 } else {
3864 /* We entered a withdrawn window, so we're still in Clip */
3865 return;
3868 if (dock->auto_raise_magic) {
3869 WMDeleteTimerHandler(dock->auto_raise_magic);
3870 dock->auto_raise_magic = NULL;
3872 if (dock->auto_raise_lower && !dock->auto_lower_magic) {
3873 dock->auto_lower_magic = WMAddTimerHandler(AUTO_LOWER_DELAY, clipAutoLower, (void *)dock);
3876 if (dock->auto_expand_magic) {
3877 WMDeleteTimerHandler(dock->auto_expand_magic);
3878 dock->auto_expand_magic = NULL;
3880 if (dock->auto_collapse && !dock->auto_collapse_magic) {
3881 dock->auto_collapse_magic = WMAddTimerHandler(AUTO_COLLAPSE_DELAY, clipAutoCollapse, (void *)dock);
3885 static void clipLeaveNotify(WObjDescriptor *desc, XEvent *event)
3887 WAppIcon *btn = (WAppIcon *) desc->parent;
3889 assert(event->type == LeaveNotify);
3891 if (desc->parent_type != WCLASS_DOCK_ICON)
3892 return;
3894 clipLeave(btn->dock);
3897 static void clipAutoCollapse(void *cdata)
3899 WDock *dock = (WDock *) cdata;
3901 if (dock->type != WM_CLIP)
3902 return;
3904 if (dock->auto_collapse) {
3905 dock->collapsed = 1;
3906 wDockHideIcons(dock);
3908 dock->auto_collapse_magic = NULL;
3911 static void clipAutoExpand(void *cdata)
3913 WDock *dock = (WDock *) cdata;
3915 if (dock->type != WM_CLIP)
3916 return;
3918 if (dock->auto_collapse) {
3919 dock->collapsed = 0;
3920 wDockShowIcons(dock);
3922 dock->auto_expand_magic = NULL;
3925 static void clipAutoLower(void *cdata)
3927 WDock *dock = (WDock *) cdata;
3929 if (dock->type != WM_CLIP)
3930 return;
3932 if (dock->auto_raise_lower)
3933 wDockLower(dock);
3935 dock->auto_lower_magic = NULL;
3938 static void clipAutoRaise(void *cdata)
3940 WDock *dock = (WDock *) cdata;
3942 if (dock->type != WM_CLIP)
3943 return;
3945 if (dock->auto_raise_lower)
3946 wDockRaise(dock);
3948 dock->auto_raise_magic = NULL;
3951 static Bool iconCanBeOmnipresent(WAppIcon *aicon)
3953 WScreen *scr = aicon->icon->core->screen_ptr;
3954 WDock *clip;
3955 WAppIcon *btn;
3956 int i, j;
3958 for (i = 0; i < scr->workspace_count; i++) {
3959 clip = scr->workspaces[i]->clip;
3961 if (clip == aicon->dock)
3962 continue;
3964 if (clip->icon_count + scr->global_icon_count >= clip->max_icons)
3965 return False; /* Clip is full in some workspace */
3967 for (j = 0; j < clip->max_icons; j++) {
3968 btn = clip->icon_array[j];
3969 if (btn && btn->xindex == aicon->xindex && btn->yindex == aicon->yindex)
3970 return False;
3974 return True;
3977 int wClipMakeIconOmnipresent(WAppIcon *aicon, int omnipresent)
3979 WScreen *scr = aicon->icon->core->screen_ptr;
3980 WAppIconChain *new_entry, *tmp, *tmp1;
3981 int status = WO_SUCCESS;
3983 if ((scr->dock && aicon->dock == scr->dock) || aicon == scr->clip_icon) {
3984 return WO_NOT_APPLICABLE;
3987 if (aicon->omnipresent == omnipresent)
3988 return WO_SUCCESS;
3990 if (omnipresent) {
3991 if (iconCanBeOmnipresent(aicon)) {
3992 aicon->omnipresent = 1;
3993 new_entry = wmalloc(sizeof(WAppIconChain));
3994 new_entry->aicon = aicon;
3995 new_entry->next = scr->global_icons;
3996 scr->global_icons = new_entry;
3997 scr->global_icon_count++;
3998 } else {
3999 aicon->omnipresent = 0;
4000 status = WO_FAILED;
4002 } else {
4003 aicon->omnipresent = 0;
4004 if (aicon == scr->global_icons->aicon) {
4005 tmp = scr->global_icons->next;
4006 wfree(scr->global_icons);
4007 scr->global_icons = tmp;
4008 scr->global_icon_count--;
4009 } else {
4010 tmp = scr->global_icons;
4011 while (tmp->next) {
4012 if (tmp->next->aicon == aicon) {
4013 tmp1 = tmp->next->next;
4014 wfree(tmp->next);
4015 tmp->next = tmp1;
4016 scr->global_icon_count--;
4017 break;
4019 tmp = tmp->next;
4024 wAppIconPaint(aicon);
4026 return status;