configure: Add detection of library kvm that is used under OpenBSD
[wmaker-crm.git] / src / dock.c
blob60f87b5edaaca24d16dec0b88b83f728650f7971
1 /* dock.c- built-in Dock module for WindowMaker
3 * Window Maker window manager
5 * Copyright (c) 1997-2003 Alfredo K. Kojima
6 * Copyright (c) 1998-2003 Dan Pascu
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
23 #include "wconfig.h"
25 #include <X11/Xlib.h>
26 #include <X11/Xutil.h>
27 #include <stdlib.h>
28 #include <string.h>
29 #include <strings.h>
30 #include <unistd.h>
31 #include <math.h>
32 #include <limits.h>
34 #ifndef PATH_MAX
35 #define PATH_MAX DEFAULT_PATH_MAX
36 #endif
38 #include "WindowMaker.h"
39 #include "wcore.h"
40 #include "window.h"
41 #include "icon.h"
42 #include "appicon.h"
43 #include "actions.h"
44 #include "stacking.h"
45 #include "dock.h"
46 #include "dockedapp.h"
47 #include "dialog.h"
48 #include "main.h"
49 #include "properties.h"
50 #include "menu.h"
51 #include "client.h"
52 #include "defaults.h"
53 #include "workspace.h"
54 #include "framewin.h"
55 #include "superfluous.h"
56 #include "xinerama.h"
57 #include "placement.h"
59 /**** Local variables ****/
60 #define CLIP_REWIND 1
61 #define CLIP_IDLE 0
62 #define CLIP_FORWARD 2
64 /**** Global variables ****/
65 extern Cursor wCursor[WCUR_LAST];
66 extern WPreferences wPreferences;
67 extern XContext wWinContext;
68 extern void appIconMouseDown(WObjDescriptor *desc, XEvent *event);
70 #define MOD_MASK wPreferences.modifier_mask
71 #define ICON_SIZE wPreferences.icon_size
73 /***** Local variables ****/
75 static WMPropList *dCommand = NULL;
76 static WMPropList *dPasteCommand = NULL;
77 #ifdef XDND /* XXX was OFFIX */
78 static WMPropList *dDropCommand = NULL;
79 #endif
80 static WMPropList *dAutoLaunch, *dLock;
81 static WMPropList *dName, *dForced, *dBuggyApplication, *dYes, *dNo;
82 static WMPropList *dHost, *dDock, *dClip;
83 static WMPropList *dAutoAttractIcons;
85 static WMPropList *dPosition, *dApplications, *dLowered, *dCollapsed;
87 static WMPropList *dAutoCollapse, *dAutoRaiseLower, *dOmnipresent;
89 static void dockIconPaint(WAppIcon *btn);
91 static void iconMouseDown(WObjDescriptor *desc, XEvent *event);
93 static pid_t execCommand(WAppIcon *btn, char *command, WSavedState *state);
95 static void trackDeadProcess(pid_t pid, unsigned char status, WDock *dock);
97 static int getClipButton(int px, int py);
99 static void toggleLowered(WDock *dock);
101 static void toggleCollapsed(WDock *dock);
103 static void clipIconExpose(WObjDescriptor *desc, XEvent *event);
105 static void clipLeave(WDock *dock);
107 static void handleClipChangeWorkspace(WScreen *scr, XEvent *event);
109 static Bool moveIconBetweenDocks(WDock *src, WDock *dest, WAppIcon *icon, int x, int y);
111 static void clipEnterNotify(WObjDescriptor *desc, XEvent *event);
112 static void clipLeaveNotify(WObjDescriptor *desc, XEvent *event);
113 static void clipAutoCollapse(void *cdata);
114 static void clipAutoExpand(void *cdata);
115 static void launchDockedApplication(WAppIcon *btn, Bool withSelection);
117 static void clipAutoLower(void *cdata);
118 static void clipAutoRaise(void *cdata);
119 static void reattachIcon(WDock *dock, WAppIcon *icon, int x, int y);
120 static WAppIcon *mainIconCreate(WScreen *scr, int type);
122 static void make_keys(void)
124 if (dCommand != NULL)
125 return;
127 dCommand = WMRetainPropList(WMCreatePLString("Command"));
128 dPasteCommand = WMRetainPropList(WMCreatePLString("PasteCommand"));
129 #ifdef XDND
130 dDropCommand = WMRetainPropList(WMCreatePLString("DropCommand"));
131 #endif
132 dLock = WMRetainPropList(WMCreatePLString("Lock"));
133 dAutoLaunch = WMRetainPropList(WMCreatePLString("AutoLaunch"));
134 dName = WMRetainPropList(WMCreatePLString("Name"));
135 dForced = WMRetainPropList(WMCreatePLString("Forced"));
136 dBuggyApplication = WMRetainPropList(WMCreatePLString("BuggyApplication"));
137 dYes = WMRetainPropList(WMCreatePLString("Yes"));
138 dNo = WMRetainPropList(WMCreatePLString("No"));
139 dHost = WMRetainPropList(WMCreatePLString("Host"));
141 dPosition = WMCreatePLString("Position");
142 dApplications = WMCreatePLString("Applications");
143 dLowered = WMCreatePLString("Lowered");
144 dCollapsed = WMCreatePLString("Collapsed");
145 dAutoCollapse = WMCreatePLString("AutoCollapse");
146 dAutoRaiseLower = WMCreatePLString("AutoRaiseLower");
147 dAutoAttractIcons = WMCreatePLString("AutoAttractIcons");
149 dOmnipresent = WMCreatePLString("Omnipresent");
151 dDock = WMCreatePLString("Dock");
152 dClip = WMCreatePLString("Clip");
155 static void renameCallback(WMenu *menu, WMenuEntry *entry)
157 WDock *dock = entry->clientdata;
158 char buffer[128];
159 int wspace;
160 char *name;
162 assert(entry->clientdata != NULL);
164 wspace = dock->screen_ptr->current_workspace;
166 name = wstrdup(dock->screen_ptr->workspaces[wspace]->name);
168 snprintf(buffer, sizeof(buffer), _("Type the name for workspace %i:"), wspace + 1);
169 if (wInputDialog(dock->screen_ptr, _("Rename Workspace"), buffer, &name))
170 wWorkspaceRename(dock->screen_ptr, wspace, name);
172 wfree(name);
175 static void toggleLoweredCallback(WMenu *menu, WMenuEntry *entry)
177 assert(entry->clientdata != NULL);
179 toggleLowered(entry->clientdata);
181 entry->flags.indicator_on = !(((WDock *) entry->clientdata)->lowered);
183 wMenuPaint(menu);
186 static int matchWindow(const void *item, const void *cdata)
188 return (((WFakeGroupLeader *) item)->leader == (Window) cdata);
191 static void killCallback(WMenu *menu, WMenuEntry *entry)
193 WScreen *scr = menu->menu->screen_ptr;
194 WAppIcon *icon;
195 WFakeGroupLeader *fPtr;
196 char *buffer, *shortname, **argv;
197 char *basename(const char *shortname);
198 int argc;
200 if (!WCHECK_STATE(WSTATE_NORMAL))
201 return;
203 assert(entry->clientdata != NULL);
205 icon = (WAppIcon *) entry->clientdata;
207 icon->editing = 1;
209 WCHANGE_STATE(WSTATE_MODAL);
211 /* strip away dir names */
212 shortname = basename(icon->command);
213 /* separate out command options */
214 wtokensplit(shortname, &argv, &argc);
216 buffer = wstrconcat(argv[0],
217 _(" will be forcibly closed.\n"
218 "Any unsaved changes will be lost.\n" "Please confirm."));
220 if (icon->icon && icon->icon->owner) {
221 fPtr = icon->icon->owner->fake_group;
222 } else {
223 /* is this really necessary? can we kill a non-running dock icon? */
224 Window win = icon->main_window;
225 int index;
227 index = WMFindInArray(scr->fakeGroupLeaders, matchWindow, (void *)win);
228 if (index != WANotFound)
229 fPtr = WMGetFromArray(scr->fakeGroupLeaders, index);
230 else
231 fPtr = NULL;
234 if (wPreferences.dont_confirm_kill
235 || wMessageDialog(menu->frame->screen_ptr, _("Kill Application"),
236 buffer, _("Yes"), _("No"), NULL) == WAPRDefault) {
237 if (fPtr != NULL) {
238 WWindow *wwin, *twin;
240 wwin = scr->focused_window;
241 while (wwin) {
242 twin = wwin->prev;
243 if (wwin->fake_group == fPtr)
244 wClientKill(wwin);
246 wwin = twin;
248 } else if (icon->icon && icon->icon->owner) {
249 wClientKill(icon->icon->owner);
253 wfree(buffer);
254 wtokenfree(argv, argc);
256 icon->editing = 0;
258 WCHANGE_STATE(WSTATE_NORMAL);
261 /* TODO: replace this function with a member of the dock struct */
262 static int numberOfSelectedIcons(WDock *dock)
264 WAppIcon *aicon;
265 int i, n;
267 n = 0;
268 for (i = 1; i < dock->max_icons; i++) {
269 aicon = dock->icon_array[i];
270 if (aicon && aicon->icon->selected)
271 n++;
274 return n;
277 static WMArray *getSelected(WDock *dock)
279 WMArray *ret = WMCreateArray(8);
280 WAppIcon *btn;
281 int i;
283 for (i = 1; i < dock->max_icons; i++) {
284 btn = dock->icon_array[i];
285 if (btn && btn->icon->selected)
286 WMAddToArray(ret, btn);
289 return ret;
292 static void paintClipButtons(WAppIcon *clipIcon, Bool lpushed, Bool rpushed)
294 Window win = clipIcon->icon->core->window;
295 WScreen *scr = clipIcon->icon->core->screen_ptr;
296 XPoint p[4];
297 int pt = CLIP_BUTTON_SIZE * ICON_SIZE / 64;
298 int tp = ICON_SIZE - pt;
299 int as = pt - 15; /* 15 = 5+5+5 */
300 GC gc = scr->draw_gc; /* maybe use WMColorGC() instead here? */
301 WMColor *color;
303 color = scr->clip_title_color[CLIP_NORMAL];
305 XSetForeground(dpy, gc, WMColorPixel(color));
307 if (rpushed) {
308 p[0].x = tp + 1;
309 p[0].y = 1;
310 p[1].x = ICON_SIZE - 2;
311 p[1].y = 1;
312 p[2].x = ICON_SIZE - 2;
313 p[2].y = pt - 1;
314 } else if (lpushed) {
315 p[0].x = 1;
316 p[0].y = tp;
317 p[1].x = pt;
318 p[1].y = ICON_SIZE - 2;
319 p[2].x = 1;
320 p[2].y = ICON_SIZE - 2;
322 if (lpushed || rpushed) {
323 XSetForeground(dpy, scr->draw_gc, scr->white_pixel);
324 XFillPolygon(dpy, win, scr->draw_gc, p, 3, Convex, CoordModeOrigin);
325 XSetForeground(dpy, scr->draw_gc, scr->black_pixel);
328 /* top right arrow */
329 p[0].x = p[3].x = ICON_SIZE - 5 - as;
330 p[0].y = p[3].y = 5;
331 p[1].x = ICON_SIZE - 6;
332 p[1].y = 5;
333 p[2].x = ICON_SIZE - 6;
334 p[2].y = 4 + as;
335 if (rpushed) {
336 XFillPolygon(dpy, win, scr->draw_gc, p, 3, Convex, CoordModeOrigin);
337 XDrawLines(dpy, win, scr->draw_gc, p, 4, CoordModeOrigin);
338 } else {
339 XFillPolygon(dpy, win, gc, p, 3, Convex, CoordModeOrigin);
340 XDrawLines(dpy, win, gc, p, 4, CoordModeOrigin);
343 /* bottom left arrow */
344 p[0].x = p[3].x = 5;
345 p[0].y = p[3].y = ICON_SIZE - 5 - as;
346 p[1].x = 5;
347 p[1].y = ICON_SIZE - 6;
348 p[2].x = 4 + as;
349 p[2].y = ICON_SIZE - 6;
350 if (lpushed) {
351 XFillPolygon(dpy, win, scr->draw_gc, p, 3, Convex, CoordModeOrigin);
352 XDrawLines(dpy, win, scr->draw_gc, p, 4, CoordModeOrigin);
353 } else {
354 XFillPolygon(dpy, win, gc, p, 3, Convex, CoordModeOrigin);
355 XDrawLines(dpy, win, gc, p, 4, CoordModeOrigin);
359 RImage *wClipMakeTile(WScreen *scr, RImage *normalTile)
361 RImage *tile = RCloneImage(normalTile);
362 RColor black;
363 RColor dark;
364 RColor light;
365 int pt, tp;
366 int as;
368 pt = CLIP_BUTTON_SIZE * wPreferences.icon_size / 64;
369 tp = wPreferences.icon_size - 1 - pt;
370 as = pt - 15;
372 black.alpha = 255;
373 black.red = black.green = black.blue = 0;
375 dark.alpha = 0;
376 dark.red = dark.green = dark.blue = 60;
378 light.alpha = 0;
379 light.red = light.green = light.blue = 80;
381 /* top right */
382 ROperateLine(tile, RSubtractOperation, tp, 0, wPreferences.icon_size - 2, pt - 1, &dark);
383 RDrawLine(tile, tp - 1, 0, wPreferences.icon_size - 1, pt + 1, &black);
384 ROperateLine(tile, RAddOperation, tp, 2, wPreferences.icon_size - 3, pt, &light);
386 /* arrow bevel */
387 ROperateLine(tile, RSubtractOperation, ICON_SIZE - 7 - as, 4, ICON_SIZE - 5, 4, &dark);
388 ROperateLine(tile, RSubtractOperation, ICON_SIZE - 6 - as, 5, ICON_SIZE - 5, 6 + as, &dark);
389 ROperateLine(tile, RAddOperation, ICON_SIZE - 5, 4, ICON_SIZE - 5, 6 + as, &light);
391 /* bottom left */
392 ROperateLine(tile, RAddOperation, 2, tp + 2, pt - 2, wPreferences.icon_size - 3, &dark);
393 RDrawLine(tile, 0, tp - 1, pt + 1, wPreferences.icon_size - 1, &black);
394 ROperateLine(tile, RSubtractOperation, 0, tp - 2, pt + 1, wPreferences.icon_size - 2, &light);
396 /* arrow bevel */
397 ROperateLine(tile, RSubtractOperation, 4, ICON_SIZE - 7 - as, 4, ICON_SIZE - 5, &dark);
398 ROperateLine(tile, RSubtractOperation, 5, ICON_SIZE - 6 - as, 6 + as, ICON_SIZE - 5, &dark);
399 ROperateLine(tile, RAddOperation, 4, ICON_SIZE - 5, 6 + as, ICON_SIZE - 5, &light);
401 return tile;
404 static void omnipresentCallback(WMenu *menu, WMenuEntry *entry)
406 WAppIcon *clickedIcon = entry->clientdata;
407 WAppIcon *aicon;
408 WDock *dock;
409 WMArray *selectedIcons;
410 WMArrayIterator iter;
411 int failed;
413 assert(entry->clientdata != NULL);
415 dock = clickedIcon->dock;
417 selectedIcons = getSelected(dock);
419 if (!WMGetArrayItemCount(selectedIcons))
420 WMAddToArray(selectedIcons, clickedIcon);
422 failed = 0;
423 WM_ITERATE_ARRAY(selectedIcons, aicon, iter) {
424 if (wClipMakeIconOmnipresent(aicon, !aicon->omnipresent) == WO_FAILED)
425 failed++;
426 else if (aicon->icon->selected)
427 wIconSelect(aicon->icon);
429 WMFreeArray(selectedIcons);
431 if (failed > 1) {
432 wMessageDialog(dock->screen_ptr, _("Warning"),
433 _("Some icons cannot be made omnipresent. "
434 "Please make sure that no other icon is "
435 "docked in the same positions on the other "
436 "workspaces and the Clip is not full in "
437 "some workspace."), _("OK"), NULL, NULL);
438 } else if (failed == 1) {
439 wMessageDialog(dock->screen_ptr, _("Warning"),
440 _("Icon cannot be made omnipresent. "
441 "Please make sure that no other icon is "
442 "docked in the same position on the other "
443 "workspaces and the Clip is not full in "
444 "some workspace."), _("OK"), NULL, NULL);
448 static void removeIconsCallback(WMenu *menu, WMenuEntry *entry)
450 WAppIcon *clickedIcon = (WAppIcon *) entry->clientdata;
451 WDock *dock;
452 WAppIcon *aicon;
453 WMArray *selectedIcons;
454 int keepit;
455 WMArrayIterator it;
457 assert(clickedIcon != NULL);
459 dock = clickedIcon->dock;
461 selectedIcons = getSelected(dock);
463 if (WMGetArrayItemCount(selectedIcons)) {
464 if (wMessageDialog(dock->screen_ptr, _("Workspace Clip"),
465 _("All selected icons will be removed!"),
466 _("OK"), _("Cancel"), NULL) != WAPRDefault) {
467 WMFreeArray(selectedIcons);
468 return;
470 } else {
471 if (clickedIcon->xindex == 0 && clickedIcon->yindex == 0) {
472 WMFreeArray(selectedIcons);
473 return;
475 WMAddToArray(selectedIcons, clickedIcon);
478 WM_ITERATE_ARRAY(selectedIcons, aicon, it) {
479 keepit = aicon->running && wApplicationOf(aicon->main_window);
480 wDockDetach(dock, aicon);
481 if (keepit) {
482 /* XXX: can: aicon->icon == NULL ? */
483 PlaceIcon(dock->screen_ptr, &aicon->x_pos, &aicon->y_pos,
484 wGetHeadForWindow(aicon->icon->owner));
485 XMoveWindow(dpy, aicon->icon->core->window, aicon->x_pos, aicon->y_pos);
486 if (!dock->mapped || dock->collapsed)
487 XMapWindow(dpy, aicon->icon->core->window);
490 WMFreeArray(selectedIcons);
492 if (wPreferences.auto_arrange_icons)
493 wArrangeIcons(dock->screen_ptr, True);
496 static void keepIconsCallback(WMenu *menu, WMenuEntry *entry)
498 WAppIcon *clickedIcon = (WAppIcon *) entry->clientdata;
499 WDock *dock;
500 WAppIcon *aicon;
501 WMArray *selectedIcons;
502 WMArrayIterator it;
504 assert(clickedIcon != NULL);
505 dock = clickedIcon->dock;
507 selectedIcons = getSelected(dock);
509 if (!WMGetArrayItemCount(selectedIcons)
510 && clickedIcon != dock->screen_ptr->clip_icon) {
511 char *command = NULL;
513 if (!clickedIcon->command && !clickedIcon->editing) {
514 clickedIcon->editing = 1;
515 if (wInputDialog(dock->screen_ptr, _("Keep Icon"),
516 _("Type the command used to launch the application"), &command)) {
517 if (command && (command[0] == 0 || (command[0] == '-' && command[1] == 0))) {
518 wfree(command);
519 command = NULL;
521 clickedIcon->command = command;
522 clickedIcon->editing = 0;
523 } else {
524 clickedIcon->editing = 0;
525 if (command)
526 wfree(command);
527 WMFreeArray(selectedIcons);
528 return;
532 WMAddToArray(selectedIcons, clickedIcon);
535 WM_ITERATE_ARRAY(selectedIcons, aicon, it) {
536 if (aicon->icon->selected)
537 wIconSelect(aicon->icon);
539 if (aicon && aicon->attracted && aicon->command) {
540 aicon->attracted = 0;
541 if (aicon->icon->shadowed) {
542 aicon->icon->shadowed = 0;
545 * Update icon pixmap, RImage doesn't change,
546 * so call wIconUpdate is not needed
548 update_icon_pixmap(aicon->icon);
550 /* Paint it */
551 wAppIconPaint(aicon);
554 save_appicon(aicon, True);
556 WMFreeArray(selectedIcons);
559 static void toggleAutoAttractCallback(WMenu *menu, WMenuEntry *entry)
561 WDock *dock = (WDock *) entry->clientdata;
563 assert(entry->clientdata != NULL);
565 dock->attract_icons = !dock->attract_icons;
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;
589 Bool update_icon = False;
591 assert(entry->clientdata != NULL);
592 clip = clickedIcon->dock;
594 aicon = clip->screen_ptr->app_icon_list;
596 while (aicon) {
597 if (!aicon->docked && wDockFindFreeSlot(clip, &x, &y)) {
598 x_pos = clip->x_pos + x * ICON_SIZE;
599 y_pos = clip->y_pos + y * ICON_SIZE;
600 if (aicon->x_pos != x_pos || aicon->y_pos != y_pos)
601 move_window(aicon->icon->core->window, aicon->x_pos, aicon->y_pos, x_pos, y_pos);
603 aicon->attracted = 1;
604 if (!aicon->icon->shadowed) {
605 aicon->icon->shadowed = 1;
606 update_icon = True;
608 wDockAttachIcon(clip, aicon, x, y, update_icon);
609 if (clip->collapsed || !clip->mapped)
610 XUnmapWindow(dpy, aicon->icon->core->window);
612 aicon = aicon->next;
616 static void selectIconsCallback(WMenu *menu, WMenuEntry *entry)
618 WAppIcon *clickedIcon = (WAppIcon *) entry->clientdata;
619 WDock *dock;
620 WMArray *selectedIcons;
621 WMArrayIterator iter;
622 WAppIcon *btn;
623 int i;
625 assert(clickedIcon != NULL);
626 dock = clickedIcon->dock;
628 selectedIcons = getSelected(dock);
630 if (!WMGetArrayItemCount(selectedIcons)) {
631 for (i = 1; i < dock->max_icons; i++) {
632 btn = dock->icon_array[i];
633 if (btn && !btn->icon->selected)
634 wIconSelect(btn->icon);
636 } else {
637 WM_ITERATE_ARRAY(selectedIcons, btn, iter) {
638 wIconSelect(btn->icon);
641 WMFreeArray(selectedIcons);
643 wMenuPaint(menu);
646 static void toggleCollapsedCallback(WMenu *menu, WMenuEntry *entry)
648 assert(entry->clientdata != NULL);
650 toggleCollapsed(entry->clientdata);
652 entry->flags.indicator_on = ((WDock *) entry->clientdata)->collapsed;
654 wMenuPaint(menu);
657 static void toggleAutoCollapseCallback(WMenu *menu, WMenuEntry *entry)
659 WDock *dock;
660 assert(entry->clientdata != NULL);
662 dock = (WDock *) entry->clientdata;
664 dock->auto_collapse = !dock->auto_collapse;
666 entry->flags.indicator_on = ((WDock *) entry->clientdata)->auto_collapse;
668 wMenuPaint(menu);
671 static void toggleAutoRaiseLowerCallback(WMenu *menu, WMenuEntry *entry)
673 WDock *dock;
674 assert(entry->clientdata != NULL);
676 dock = (WDock *) entry->clientdata;
678 dock->auto_raise_lower = !dock->auto_raise_lower;
680 entry->flags.indicator_on = ((WDock *) entry->clientdata)->auto_raise_lower;
682 wMenuPaint(menu);
685 static void launchCallback(WMenu *menu, WMenuEntry *entry)
687 WAppIcon *btn = (WAppIcon *) entry->clientdata;
689 launchDockedApplication(btn, False);
692 static void settingsCallback(WMenu *menu, WMenuEntry *entry)
694 WAppIcon *btn = (WAppIcon *) entry->clientdata;
696 if (btn->editing)
697 return;
698 ShowDockAppSettingsPanel(btn);
701 static void hideCallback(WMenu *menu, WMenuEntry *entry)
703 WApplication *wapp;
704 WAppIcon *btn = (WAppIcon *) entry->clientdata;
706 wapp = wApplicationOf(btn->icon->owner->main_window);
708 if (wapp->flags.hidden) {
709 wWorkspaceChange(btn->icon->core->screen_ptr, wapp->last_workspace);
710 wUnhideApplication(wapp, False, False);
711 } else {
712 wHideApplication(wapp);
716 static void unhideHereCallback(WMenu *menu, WMenuEntry *entry)
718 WApplication *wapp;
719 WAppIcon *btn = (WAppIcon *) entry->clientdata;
721 wapp = wApplicationOf(btn->icon->owner->main_window);
723 wUnhideApplication(wapp, False, True);
726 static WAppIcon *mainIconCreate(WScreen *scr, int type)
728 WAppIcon *btn;
729 int x_pos;
731 if (type == WM_CLIP) {
732 if (scr->clip_icon)
733 return scr->clip_icon;
734 btn = wAppIconCreateForDock(scr, NULL, "Logo", "WMClip", TILE_CLIP);
735 btn->icon->core->descriptor.handle_expose = clipIconExpose;
736 btn->icon->core->descriptor.handle_enternotify = clipEnterNotify;
737 btn->icon->core->descriptor.handle_leavenotify = clipLeaveNotify;
738 x_pos = 0;
739 } else {
740 btn = wAppIconCreateForDock(scr, NULL, "Logo", "WMDock", TILE_NORMAL);
741 x_pos = scr->scr_width - ICON_SIZE - DOCK_EXTRA_SPACE;
744 btn->xindex = 0;
745 btn->yindex = 0;
747 btn->icon->core->descriptor.handle_mousedown = iconMouseDown;
748 btn->icon->core->descriptor.parent_type = WCLASS_DOCK_ICON;
749 btn->icon->core->descriptor.parent = btn;
750 XMapWindow(dpy, btn->icon->core->window);
751 btn->x_pos = x_pos;
752 btn->y_pos = 0;
753 btn->docked = 1;
754 if (type == WM_CLIP)
755 scr->clip_icon = btn;
757 return btn;
760 static void switchWSCommand(WMenu *menu, WMenuEntry *entry)
762 WAppIcon *btn, *icon = (WAppIcon *) entry->clientdata;
763 WScreen *scr = icon->icon->core->screen_ptr;
764 WDock *src, *dest;
765 WMArray *selectedIcons;
766 int x, y;
768 if (entry->order == scr->current_workspace)
769 return;
770 src = icon->dock;
771 dest = scr->workspaces[entry->order]->clip;
773 selectedIcons = getSelected(src);
775 if (WMGetArrayItemCount(selectedIcons)) {
776 WMArrayIterator iter;
778 WM_ITERATE_ARRAY(selectedIcons, btn, iter) {
779 if (wDockFindFreeSlot(dest, &x, &y)) {
780 moveIconBetweenDocks(src, dest, btn, x, y);
781 XUnmapWindow(dpy, btn->icon->core->window);
784 } else if (icon != scr->clip_icon) {
785 if (wDockFindFreeSlot(dest, &x, &y)) {
786 moveIconBetweenDocks(src, dest, icon, x, y);
787 XUnmapWindow(dpy, icon->icon->core->window);
790 WMFreeArray(selectedIcons);
793 static void launchDockedApplication(WAppIcon *btn, Bool withSelection)
795 WScreen *scr = btn->icon->core->screen_ptr;
797 if (!btn->launching &&
798 ((!withSelection && btn->command != NULL) || (withSelection && btn->paste_command != NULL))) {
799 if (!btn->forced_dock) {
800 btn->relaunching = btn->running;
801 btn->running = 1;
803 if (btn->wm_instance || btn->wm_class) {
804 WWindowAttributes attr;
805 memset(&attr, 0, sizeof(WWindowAttributes));
806 wDefaultFillAttributes(btn->wm_instance, btn->wm_class, &attr, NULL, True);
808 if (!attr.no_appicon && !btn->buggy_app)
809 btn->launching = 1;
810 else
811 btn->running = 0;
813 btn->drop_launch = 0;
814 btn->paste_launch = withSelection;
815 scr->last_dock = btn->dock;
816 btn->pid = execCommand(btn, (withSelection ? btn->paste_command : btn->command), NULL);
817 if (btn->pid > 0) {
818 if (btn->buggy_app) {
819 /* give feedback that the app was launched */
820 btn->launching = 1;
821 dockIconPaint(btn);
822 btn->launching = 0;
823 WMAddTimerHandler(200, (WMCallback *) dockIconPaint, btn);
824 } else {
825 dockIconPaint(btn);
827 } else {
828 wwarning(_("could not launch application %s"), btn->command);
829 btn->launching = 0;
830 if (!btn->relaunching)
831 btn->running = 0;
836 static void updateWorkspaceMenu(WMenu *menu, WAppIcon *icon)
838 WScreen *scr = menu->frame->screen_ptr;
839 char title[MAX_WORKSPACENAME_WIDTH + 1];
840 int i;
842 if (!menu || !icon)
843 return;
845 for (i = 0; i < scr->workspace_count; i++) {
846 if (i < menu->entry_no) {
847 if (strcmp(menu->entries[i]->text, scr->workspaces[i]->name) != 0) {
848 wfree(menu->entries[i]->text);
849 strcpy(title, scr->workspaces[i]->name);
850 menu->entries[i]->text = wstrdup(title);
851 menu->flags.realized = 0;
853 menu->entries[i]->clientdata = (void *)icon;
854 } else {
855 strcpy(title, scr->workspaces[i]->name);
857 wMenuAddCallback(menu, title, switchWSCommand, (void *)icon);
859 menu->flags.realized = 0;
862 if (i == scr->current_workspace)
863 wMenuSetEnabled(menu, i, False);
864 else
865 wMenuSetEnabled(menu, i, True);
868 if (!menu->flags.realized)
869 wMenuRealize(menu);
872 static WMenu *makeWorkspaceMenu(WScreen *scr)
874 WMenu *menu;
876 menu = wMenuCreate(scr, NULL, False);
877 if (!menu)
878 wwarning(_("could not create workspace submenu for Clip menu"));
880 wMenuAddCallback(menu, "", switchWSCommand, (void *)scr->clip_icon);
882 menu->flags.realized = 0;
883 wMenuRealize(menu);
885 return menu;
888 static void updateClipOptionsMenu(WMenu *menu, WDock *dock)
890 WMenuEntry *entry;
891 int index = 0;
893 if (!menu || !dock)
894 return;
896 /* keep on top */
897 entry = menu->entries[index];
898 entry->flags.indicator_on = !dock->lowered;
899 entry->clientdata = dock;
901 /* collapsed */
902 entry = menu->entries[++index];
903 entry->flags.indicator_on = dock->collapsed;
904 entry->clientdata = dock;
906 /* auto-collapse */
907 entry = menu->entries[++index];
908 entry->flags.indicator_on = dock->auto_collapse;
909 entry->clientdata = dock;
911 /* auto-raise/lower */
912 entry = menu->entries[++index];
913 entry->flags.indicator_on = dock->auto_raise_lower;
914 entry->clientdata = dock;
915 wMenuSetEnabled(menu, index, dock->lowered);
917 /* attract icons */
918 entry = menu->entries[++index];
919 entry->flags.indicator_on = dock->attract_icons;
920 entry->clientdata = dock;
922 menu->flags.realized = 0;
923 wMenuRealize(menu);
926 static WMenu *makeClipOptionsMenu(WScreen *scr)
928 WMenu *menu;
929 WMenuEntry *entry;
931 menu = wMenuCreate(scr, NULL, False);
932 if (!menu) {
933 wwarning(_("could not create options submenu for Clip menu"));
934 return NULL;
937 entry = wMenuAddCallback(menu, _("Keep on Top"), toggleLoweredCallback, NULL);
938 entry->flags.indicator = 1;
939 entry->flags.indicator_on = 1;
940 entry->flags.indicator_type = MI_CHECK;
942 entry = wMenuAddCallback(menu, _("Collapsed"), toggleCollapsedCallback, NULL);
943 entry->flags.indicator = 1;
944 entry->flags.indicator_on = 1;
945 entry->flags.indicator_type = MI_CHECK;
947 entry = wMenuAddCallback(menu, _("Autocollapse"), toggleAutoCollapseCallback, NULL);
948 entry->flags.indicator = 1;
949 entry->flags.indicator_on = 1;
950 entry->flags.indicator_type = MI_CHECK;
952 entry = wMenuAddCallback(menu, _("Autoraise"), toggleAutoRaiseLowerCallback, NULL);
953 entry->flags.indicator = 1;
954 entry->flags.indicator_on = 1;
955 entry->flags.indicator_type = MI_CHECK;
957 entry = wMenuAddCallback(menu, _("Autoattract Icons"), toggleAutoAttractCallback, NULL);
958 entry->flags.indicator = 1;
959 entry->flags.indicator_on = 1;
960 entry->flags.indicator_type = MI_CHECK;
962 menu->flags.realized = 0;
963 wMenuRealize(menu);
965 return menu;
968 static WMenu *dockMenuCreate(WScreen *scr, int type)
970 WMenu *menu;
971 WMenuEntry *entry;
973 if (type == WM_CLIP && scr->clip_menu)
974 return scr->clip_menu;
976 menu = wMenuCreate(scr, NULL, False);
977 if (type != WM_CLIP) {
978 entry = wMenuAddCallback(menu, _("Keep on Top"), toggleLoweredCallback, NULL);
979 entry->flags.indicator = 1;
980 entry->flags.indicator_on = 1;
981 entry->flags.indicator_type = MI_CHECK;
982 } else {
983 entry = wMenuAddCallback(menu, _("Clip Options"), NULL, NULL);
984 scr->clip_options = makeClipOptionsMenu(scr);
985 if (scr->clip_options)
986 wMenuEntrySetCascade(menu, entry, scr->clip_options);
988 entry = wMenuAddCallback(menu, _("Rename Workspace"), renameCallback, NULL);
989 wfree(entry->text);
990 entry->text = _("Rename Workspace");
992 entry = wMenuAddCallback(menu, _("Selected"), selectCallback, NULL);
993 entry->flags.indicator = 1;
994 entry->flags.indicator_on = 1;
995 entry->flags.indicator_type = MI_CHECK;
997 entry = wMenuAddCallback(menu, _("Select All Icons"), selectIconsCallback, NULL);
998 wfree(entry->text);
999 entry->text = _("Select All Icons");
1001 entry = wMenuAddCallback(menu, _("Keep Icon"), keepIconsCallback, NULL);
1002 wfree(entry->text);
1003 entry->text = _("Keep Icon");
1005 entry = wMenuAddCallback(menu, _("Move Icon To"), NULL, NULL);
1006 wfree(entry->text);
1007 entry->text = _("Move Icon To");
1008 scr->clip_submenu = makeWorkspaceMenu(scr);
1009 if (scr->clip_submenu)
1010 wMenuEntrySetCascade(menu, entry, scr->clip_submenu);
1012 entry = wMenuAddCallback(menu, _("Remove Icon"), removeIconsCallback, NULL);
1013 wfree(entry->text);
1014 entry->text = _("Remove Icon");
1016 wMenuAddCallback(menu, _("Attract Icons"), colectIconsCallback, NULL);
1019 wMenuAddCallback(menu, _("Launch"), launchCallback, NULL);
1021 wMenuAddCallback(menu, _("Unhide Here"), unhideHereCallback, NULL);
1023 entry = wMenuAddCallback(menu, _("Hide"), hideCallback, NULL);
1024 wfree(entry->text);
1025 entry->text = _("Hide");
1027 wMenuAddCallback(menu, _("Settings..."), settingsCallback, NULL);
1029 wMenuAddCallback(menu, _("Kill"), killCallback, NULL);
1031 if (type == WM_CLIP)
1032 scr->clip_menu = menu;
1034 return menu;
1037 WDock *wDockCreate(WScreen *scr, int type)
1039 WDock *dock;
1040 WAppIcon *btn;
1042 make_keys();
1044 dock = wmalloc(sizeof(WDock));
1046 dock->max_icons = DOCK_MAX_ICONS;
1048 dock->icon_array = wmalloc(sizeof(WAppIcon *) * dock->max_icons);
1050 btn = mainIconCreate(scr, type);
1052 btn->dock = dock;
1054 dock->x_pos = btn->x_pos;
1055 dock->y_pos = btn->y_pos;
1056 dock->screen_ptr = scr;
1057 dock->type = type;
1058 dock->icon_count = 1;
1059 dock->on_right_side = 1;
1060 dock->collapsed = 0;
1061 dock->auto_collapse = 0;
1062 dock->auto_collapse_magic = NULL;
1063 dock->auto_raise_lower = 0;
1064 dock->auto_lower_magic = NULL;
1065 dock->auto_raise_magic = NULL;
1066 dock->attract_icons = 0;
1067 dock->lowered = 1;
1068 dock->icon_array[0] = btn;
1069 wRaiseFrame(btn->icon->core);
1070 XMoveWindow(dpy, btn->icon->core->window, btn->x_pos, btn->y_pos);
1072 /* create dock menu */
1073 dock->menu = dockMenuCreate(scr, type);
1075 return dock;
1078 void wDockDestroy(WDock *dock)
1080 int i;
1081 WAppIcon *aicon;
1083 for (i = (dock->type == WM_CLIP) ? 1 : 0; i < dock->max_icons; i++) {
1084 aicon = dock->icon_array[i];
1085 if (aicon) {
1086 int keepit = aicon->running && wApplicationOf(aicon->main_window);
1087 wDockDetach(dock, aicon);
1088 if (keepit) {
1089 /* XXX: can: aicon->icon == NULL ? */
1090 PlaceIcon(dock->screen_ptr, &aicon->x_pos, &aicon->y_pos,
1091 wGetHeadForWindow(aicon->icon->owner));
1092 XMoveWindow(dpy, aicon->icon->core->window, aicon->x_pos, aicon->y_pos);
1093 if (!dock->mapped || dock->collapsed)
1094 XMapWindow(dpy, aicon->icon->core->window);
1098 if (wPreferences.auto_arrange_icons)
1099 wArrangeIcons(dock->screen_ptr, True);
1100 wfree(dock->icon_array);
1101 if (dock->menu && dock->type != WM_CLIP)
1102 wMenuDestroy(dock->menu, True);
1103 if (dock->screen_ptr->last_dock == dock)
1104 dock->screen_ptr->last_dock = NULL;
1105 wfree(dock);
1108 void wClipIconPaint(WAppIcon *aicon)
1110 WScreen *scr = aicon->icon->core->screen_ptr;
1111 WWorkspace *workspace = scr->workspaces[scr->current_workspace];
1112 WMColor *color;
1113 Window win = aicon->icon->core->window;
1114 int length, nlength;
1115 char *ws_name, ws_number[10];
1116 int ty, tx;
1118 wIconPaint(aicon->icon);
1120 length = strlen(workspace->name);
1121 ws_name = wmalloc(length + 1);
1122 snprintf(ws_name, length + 1, "%s", workspace->name);
1123 snprintf(ws_number, sizeof(ws_number), "%i", scr->current_workspace + 1);
1124 nlength = strlen(ws_number);
1126 if (!workspace->clip->collapsed)
1127 color = scr->clip_title_color[CLIP_NORMAL];
1128 else
1129 color = scr->clip_title_color[CLIP_COLLAPSED];
1131 ty = ICON_SIZE - WMFontHeight(scr->clip_title_font) - 3;
1133 tx = CLIP_BUTTON_SIZE * ICON_SIZE / 64;
1135 if(wPreferences.show_clip_title)
1136 WMDrawString(scr->wmscreen, win, color, scr->clip_title_font, tx, ty, ws_name, length);
1138 tx = (ICON_SIZE / 2 - WMWidthOfString(scr->clip_title_font, ws_number, nlength)) / 2;
1140 WMDrawString(scr->wmscreen, win, color, scr->clip_title_font, tx, 2, ws_number, nlength);
1142 wfree(ws_name);
1144 if (aicon->launching)
1145 XFillRectangle(dpy, aicon->icon->core->window, scr->stipple_gc,
1146 0, 0, wPreferences.icon_size, wPreferences.icon_size);
1148 paintClipButtons(aicon, aicon->dock->lclip_button_pushed, aicon->dock->rclip_button_pushed);
1151 static void clipIconExpose(WObjDescriptor *desc, XEvent *event)
1153 wClipIconPaint(desc->parent);
1156 static void dockIconPaint(WAppIcon *btn)
1158 if (btn == btn->icon->core->screen_ptr->clip_icon) {
1159 wClipIconPaint(btn);
1160 } else {
1161 wAppIconPaint(btn);
1162 save_appicon(btn, True);
1166 static WMPropList *make_icon_state(WAppIcon *btn)
1168 WMPropList *node = NULL;
1169 WMPropList *command, *autolaunch, *lock, *name, *forced;
1170 WMPropList *position, *buggy, *omnipresent;
1171 char *tmp;
1172 char buffer[64];
1174 if (btn) {
1175 if (!btn->command)
1176 command = WMCreatePLString("-");
1177 else
1178 command = WMCreatePLString(btn->command);
1180 autolaunch = btn->auto_launch ? dYes : dNo;
1182 lock = btn->lock ? dYes : dNo;
1184 tmp = EscapeWM_CLASS(btn->wm_instance, btn->wm_class);
1186 name = WMCreatePLString(tmp);
1188 wfree(tmp);
1190 forced = btn->forced_dock ? dYes : dNo;
1192 buggy = btn->buggy_app ? dYes : dNo;
1194 if (btn == btn->icon->core->screen_ptr->clip_icon)
1195 snprintf(buffer, sizeof(buffer), "%i,%i", btn->x_pos, btn->y_pos);
1196 else
1197 snprintf(buffer, sizeof(buffer), "%hi,%hi", btn->xindex, btn->yindex);
1198 position = WMCreatePLString(buffer);
1200 node = WMCreatePLDictionary(dCommand, command,
1201 dName, name,
1202 dAutoLaunch, autolaunch,
1203 dLock, lock,
1204 dForced, forced, dBuggyApplication, buggy, dPosition, position, NULL);
1205 WMReleasePropList(command);
1206 WMReleasePropList(name);
1207 WMReleasePropList(position);
1209 omnipresent = btn->omnipresent ? dYes : dNo;
1210 if (btn->dock != btn->icon->core->screen_ptr->dock && (btn->xindex != 0 || btn->yindex != 0))
1211 WMPutInPLDictionary(node, dOmnipresent, omnipresent);
1213 #ifdef XDND /* was OFFIX */
1214 if (btn->dnd_command) {
1215 command = WMCreatePLString(btn->dnd_command);
1216 WMPutInPLDictionary(node, dDropCommand, command);
1217 WMReleasePropList(command);
1219 #endif /* XDND */
1221 if (btn->paste_command) {
1222 command = WMCreatePLString(btn->paste_command);
1223 WMPutInPLDictionary(node, dPasteCommand, command);
1224 WMReleasePropList(command);
1228 return node;
1231 static WMPropList *dockSaveState(WDock *dock)
1233 int i;
1234 WMPropList *icon_info;
1235 WMPropList *list = NULL, *dock_state = NULL;
1236 WMPropList *value, *key;
1237 char buffer[256];
1239 list = WMCreatePLArray(NULL);
1241 for (i = (dock->type == WM_DOCK ? 0 : 1); i < dock->max_icons; i++) {
1242 WAppIcon *btn = dock->icon_array[i];
1244 if (!btn || btn->attracted)
1245 continue;
1247 if ((icon_info = make_icon_state(dock->icon_array[i]))) {
1248 WMAddToPLArray(list, icon_info);
1249 WMReleasePropList(icon_info);
1253 dock_state = WMCreatePLDictionary(dApplications, list, NULL);
1255 if (dock->type == WM_DOCK) {
1256 snprintf(buffer, sizeof(buffer), "Applications%i", dock->screen_ptr->scr_height);
1257 key = WMCreatePLString(buffer);
1258 WMPutInPLDictionary(dock_state, key, list);
1259 WMReleasePropList(key);
1261 snprintf(buffer, sizeof(buffer), "%i,%i", (dock->on_right_side ? -ICON_SIZE : 0), dock->y_pos);
1262 value = WMCreatePLString(buffer);
1263 WMPutInPLDictionary(dock_state, dPosition, value);
1264 WMReleasePropList(value);
1266 WMReleasePropList(list);
1268 value = (dock->lowered ? dYes : dNo);
1269 WMPutInPLDictionary(dock_state, dLowered, value);
1271 if (dock->type == WM_CLIP) {
1272 value = (dock->collapsed ? dYes : dNo);
1273 WMPutInPLDictionary(dock_state, dCollapsed, value);
1275 value = (dock->auto_collapse ? dYes : dNo);
1276 WMPutInPLDictionary(dock_state, dAutoCollapse, value);
1278 value = (dock->auto_raise_lower ? dYes : dNo);
1279 WMPutInPLDictionary(dock_state, dAutoRaiseLower, value);
1281 value = (dock->attract_icons ? dYes : dNo);
1282 WMPutInPLDictionary(dock_state, dAutoAttractIcons, value);
1285 return dock_state;
1288 void wDockSaveState(WScreen *scr, WMPropList *old_state)
1290 WMPropList *dock_state;
1291 WMPropList *keys;
1293 dock_state = dockSaveState(scr->dock);
1296 * Copy saved states of docks with different sizes.
1298 if (old_state) {
1299 int i;
1300 WMPropList *tmp;
1302 keys = WMGetPLDictionaryKeys(old_state);
1303 for (i = 0; i < WMGetPropListItemCount(keys); i++) {
1304 tmp = WMGetFromPLArray(keys, i);
1306 if (strncasecmp(WMGetFromPLString(tmp), "applications", 12) == 0
1307 && !WMGetFromPLDictionary(dock_state, tmp)) {
1309 WMPutInPLDictionary(dock_state, tmp, WMGetFromPLDictionary(old_state, tmp));
1312 WMReleasePropList(keys);
1315 WMPutInPLDictionary(scr->session_state, dDock, dock_state);
1317 WMReleasePropList(dock_state);
1320 void wClipSaveState(WScreen *scr)
1322 WMPropList *clip_state;
1324 clip_state = make_icon_state(scr->clip_icon);
1326 WMPutInPLDictionary(scr->session_state, dClip, clip_state);
1328 WMReleasePropList(clip_state);
1331 WMPropList *wClipSaveWorkspaceState(WScreen *scr, int workspace)
1333 return dockSaveState(scr->workspaces[workspace]->clip);
1336 static Bool getBooleanDockValue(WMPropList *value, WMPropList *key)
1338 if (value) {
1339 if (WMIsPLString(value)) {
1340 if (strcasecmp(WMGetFromPLString(value), "YES") == 0)
1341 return True;
1342 } else {
1343 wwarning(_("bad value in docked icon state info %s"), WMGetFromPLString(key));
1346 return False;
1349 static WAppIcon *restore_icon_state(WScreen *scr, WMPropList *info, int type, int index)
1351 WAppIcon *aicon;
1352 WMPropList *cmd, *value;
1353 char *wclass, *winstance, *command;
1355 cmd = WMGetFromPLDictionary(info, dCommand);
1356 if (!cmd || !WMIsPLString(cmd))
1357 return NULL;
1359 /* parse window name */
1360 value = WMGetFromPLDictionary(info, dName);
1361 if (!value)
1362 return NULL;
1364 ParseWindowName(value, &winstance, &wclass, "dock");
1366 if (!winstance && !wclass)
1367 return NULL;
1369 /* get commands */
1370 if (cmd)
1371 command = wstrdup(WMGetFromPLString(cmd));
1372 else
1373 command = NULL;
1375 if (!command || strcmp(command, "-") == 0) {
1376 if (command)
1377 wfree(command);
1378 if (wclass)
1379 wfree(wclass);
1380 if (winstance)
1381 wfree(winstance);
1383 return NULL;
1386 aicon = wAppIconCreateForDock(scr, command, winstance, wclass, TILE_NORMAL);
1387 if (wclass)
1388 wfree(wclass);
1389 if (winstance)
1390 wfree(winstance);
1391 if (command)
1392 wfree(command);
1394 aicon->icon->core->descriptor.handle_mousedown = iconMouseDown;
1395 if (type == WM_CLIP) {
1396 aicon->icon->core->descriptor.handle_enternotify = clipEnterNotify;
1397 aicon->icon->core->descriptor.handle_leavenotify = clipLeaveNotify;
1399 aicon->icon->core->descriptor.parent_type = WCLASS_DOCK_ICON;
1400 aicon->icon->core->descriptor.parent = aicon;
1402 #ifdef XDND /* was OFFIX */
1403 cmd = WMGetFromPLDictionary(info, dDropCommand);
1404 if (cmd)
1405 aicon->dnd_command = wstrdup(WMGetFromPLString(cmd));
1406 #endif
1408 cmd = WMGetFromPLDictionary(info, dPasteCommand);
1409 if (cmd)
1410 aicon->paste_command = wstrdup(WMGetFromPLString(cmd));
1412 /* check auto launch */
1413 value = WMGetFromPLDictionary(info, dAutoLaunch);
1415 aicon->auto_launch = getBooleanDockValue(value, dAutoLaunch);
1417 /* check lock */
1418 value = WMGetFromPLDictionary(info, dLock);
1420 aicon->lock = getBooleanDockValue(value, dLock);
1422 /* check if it wasn't normally docked */
1423 value = WMGetFromPLDictionary(info, dForced);
1425 aicon->forced_dock = getBooleanDockValue(value, dForced);
1427 /* check if we can rely on the stuff in the app */
1428 value = WMGetFromPLDictionary(info, dBuggyApplication);
1430 aicon->buggy_app = getBooleanDockValue(value, dBuggyApplication);
1432 /* get position in the dock */
1433 value = WMGetFromPLDictionary(info, dPosition);
1434 if (value && WMIsPLString(value)) {
1435 if (sscanf(WMGetFromPLString(value), "%hi,%hi", &aicon->xindex, &aicon->yindex) != 2)
1436 wwarning(_("bad value in docked icon state info %s"), WMGetFromPLString(dPosition));
1438 /* check position sanity */
1439 /* incomplete section! */
1440 if (type == WM_DOCK) {
1441 aicon->xindex = 0;
1442 if (aicon->yindex < 0)
1443 wwarning(_("bad value in docked icon position %i,%i"),
1444 aicon->xindex, aicon->yindex);
1446 } else {
1447 aicon->yindex = index;
1448 aicon->xindex = 0;
1451 /* check if icon is omnipresent */
1452 value = WMGetFromPLDictionary(info, dOmnipresent);
1454 aicon->omnipresent = getBooleanDockValue(value, dOmnipresent);
1456 aicon->running = 0;
1457 aicon->docked = 1;
1459 return aicon;
1462 #define COMPLAIN(key) wwarning(_("bad value in dock state info:%s"), key)
1464 WAppIcon *wClipRestoreState(WScreen *scr, WMPropList *clip_state)
1466 WAppIcon *icon;
1467 WMPropList *value;
1469 icon = mainIconCreate(scr, WM_CLIP);
1471 if (!clip_state)
1472 return icon;
1474 WMRetainPropList(clip_state);
1476 /* restore position */
1478 value = WMGetFromPLDictionary(clip_state, dPosition);
1480 if (value) {
1481 if (!WMIsPLString(value)) {
1482 COMPLAIN("Position");
1483 } else {
1484 WMRect rect;
1485 int flags;
1487 if (sscanf(WMGetFromPLString(value), "%i,%i", &icon->x_pos, &icon->y_pos) != 2)
1488 COMPLAIN("Position");
1490 /* check position sanity */
1491 rect.pos.x = icon->x_pos;
1492 rect.pos.y = icon->y_pos;
1493 rect.size.width = rect.size.height = ICON_SIZE;
1495 wGetRectPlacementInfo(scr, rect, &flags);
1496 if (flags & (XFLAG_DEAD | XFLAG_PARTIAL))
1497 wScreenKeepInside(scr, &icon->x_pos, &icon->y_pos, ICON_SIZE, ICON_SIZE);
1500 #ifdef XDND /* was OFFIX */
1501 value = WMGetFromPLDictionary(clip_state, dDropCommand);
1502 if (value && WMIsPLString(value))
1503 icon->dnd_command = wstrdup(WMGetFromPLString(value));
1504 #endif
1506 value = WMGetFromPLDictionary(clip_state, dPasteCommand);
1507 if (value && WMIsPLString(value))
1508 icon->paste_command = wstrdup(WMGetFromPLString(value));
1510 WMReleasePropList(clip_state);
1512 return icon;
1515 WDock *wDockRestoreState(WScreen *scr, WMPropList *dock_state, int type)
1517 WDock *dock;
1518 WMPropList *apps;
1519 WMPropList *value;
1520 WAppIcon *aicon, *old_top;
1521 int count, i;
1523 dock = wDockCreate(scr, type);
1525 if (!dock_state)
1526 return dock;
1528 WMRetainPropList(dock_state);
1530 /* restore position */
1531 value = WMGetFromPLDictionary(dock_state, dPosition);
1532 if (value) {
1533 if (!WMIsPLString(value)) {
1534 COMPLAIN("Position");
1535 } else {
1536 WMRect rect;
1537 int flags;
1539 if (sscanf(WMGetFromPLString(value), "%i,%i", &dock->x_pos, &dock->y_pos) != 2)
1540 COMPLAIN("Position");
1542 /* check position sanity */
1543 rect.pos.x = dock->x_pos;
1544 rect.pos.y = dock->y_pos;
1545 rect.size.width = rect.size.height = ICON_SIZE;
1547 wGetRectPlacementInfo(scr, rect, &flags);
1548 if (flags & (XFLAG_DEAD | XFLAG_PARTIAL)) {
1549 int x = dock->x_pos;
1550 wScreenKeepInside(scr, &x, &dock->y_pos, ICON_SIZE, ICON_SIZE);
1553 /* Is this needed any more? */
1554 if (type == WM_CLIP) {
1555 if (dock->x_pos < 0) {
1556 dock->x_pos = 0;
1557 } else if (dock->x_pos > scr->scr_width - ICON_SIZE) {
1558 dock->x_pos = scr->scr_width - ICON_SIZE;
1560 } else {
1561 if (dock->x_pos >= 0) {
1562 dock->x_pos = DOCK_EXTRA_SPACE;
1563 dock->on_right_side = 0;
1564 } else {
1565 dock->x_pos = scr->scr_width - DOCK_EXTRA_SPACE - ICON_SIZE;
1566 dock->on_right_side = 1;
1572 /* restore lowered/raised state */
1573 dock->lowered = 0;
1575 value = WMGetFromPLDictionary(dock_state, dLowered);
1576 if (value) {
1577 if (!WMIsPLString(value)) {
1578 COMPLAIN("Lowered");
1579 } else {
1580 if (strcasecmp(WMGetFromPLString(value), "YES") == 0)
1581 dock->lowered = 1;
1585 /* restore collapsed state */
1586 dock->collapsed = 0;
1588 value = WMGetFromPLDictionary(dock_state, dCollapsed);
1589 if (value) {
1590 if (!WMIsPLString(value)) {
1591 COMPLAIN("Collapsed");
1592 } else {
1593 if (strcasecmp(WMGetFromPLString(value), "YES") == 0)
1594 dock->collapsed = 1;
1598 /* restore auto-collapsed state */
1599 value = WMGetFromPLDictionary(dock_state, dAutoCollapse);
1600 if (value) {
1601 if (!WMIsPLString(value)) {
1602 COMPLAIN("AutoCollapse");
1603 } else {
1604 if (strcasecmp(WMGetFromPLString(value), "YES") == 0) {
1605 dock->auto_collapse = 1;
1606 dock->collapsed = 1;
1611 /* restore auto-raise/lower state */
1612 value = WMGetFromPLDictionary(dock_state, dAutoRaiseLower);
1613 if (value) {
1614 if (!WMIsPLString(value)) {
1615 COMPLAIN("AutoRaiseLower");
1616 } else {
1617 if (strcasecmp(WMGetFromPLString(value), "YES") == 0)
1618 dock->auto_raise_lower = 1;
1622 /* restore attract icons state */
1623 dock->attract_icons = 0;
1625 value = WMGetFromPLDictionary(dock_state, dAutoAttractIcons);
1626 if (value) {
1627 if (!WMIsPLString(value)) {
1628 COMPLAIN("AutoAttractIcons");
1629 } else {
1630 if (strcasecmp(WMGetFromPLString(value), "YES") == 0)
1631 dock->attract_icons = 1;
1635 /* application list */
1638 WMPropList *tmp;
1639 char buffer[64];
1642 * When saving, it saves the dock state in
1643 * Applications and Applicationsnnn
1645 * When loading, it will first try Applicationsnnn.
1646 * If it does not exist, use Applications as default.
1649 snprintf(buffer, sizeof(buffer), "Applications%i", scr->scr_height);
1651 tmp = WMCreatePLString(buffer);
1652 apps = WMGetFromPLDictionary(dock_state, tmp);
1653 WMReleasePropList(tmp);
1655 if (!apps)
1656 apps = WMGetFromPLDictionary(dock_state, dApplications);
1659 if (!apps)
1660 goto finish;
1662 count = WMGetPropListItemCount(apps);
1663 if (count == 0)
1664 goto finish;
1666 old_top = dock->icon_array[0];
1668 /* dock->icon_count is set to 1 when dock is created.
1669 * Since Clip is already restored, we want to keep it so for clip,
1670 * but for dock we may change the default top tile, so we set it to 0.
1672 if (type == WM_DOCK)
1673 dock->icon_count = 0;
1675 for (i = 0; i < count; i++) {
1676 if (dock->icon_count >= dock->max_icons) {
1677 wwarning(_("there are too many icons stored in dock. Ignoring what doesn't fit"));
1678 break;
1681 value = WMGetFromPLArray(apps, i);
1682 aicon = restore_icon_state(scr, value, type, dock->icon_count);
1684 dock->icon_array[dock->icon_count] = aicon;
1686 if (aicon) {
1687 aicon->dock = dock;
1688 aicon->x_pos = dock->x_pos + (aicon->xindex * ICON_SIZE);
1689 aicon->y_pos = dock->y_pos + (aicon->yindex * ICON_SIZE);
1691 if (dock->lowered)
1692 ChangeStackingLevel(aicon->icon->core, WMNormalLevel);
1693 else
1694 ChangeStackingLevel(aicon->icon->core, WMDockLevel);
1696 wCoreConfigure(aicon->icon->core, aicon->x_pos, aicon->y_pos, 0, 0);
1697 if (!dock->collapsed)
1698 XMapWindow(dpy, aicon->icon->core->window);
1700 wRaiseFrame(aicon->icon->core);
1702 dock->icon_count++;
1703 } else if (dock->icon_count == 0 && type == WM_DOCK) {
1704 dock->icon_count++;
1708 /* if the first icon is not defined, use the default */
1709 if (dock->icon_array[0] == NULL) {
1710 /* update default icon */
1711 old_top->x_pos = dock->x_pos;
1712 old_top->y_pos = dock->y_pos;
1713 if (dock->lowered)
1714 ChangeStackingLevel(old_top->icon->core, WMNormalLevel);
1715 else
1716 ChangeStackingLevel(old_top->icon->core, WMDockLevel);
1718 dock->icon_array[0] = old_top;
1719 XMoveWindow(dpy, old_top->icon->core->window, dock->x_pos, dock->y_pos);
1720 /* we don't need to increment dock->icon_count here because it was
1721 * incremented in the loop above.
1723 } else if (old_top != dock->icon_array[0]) {
1724 if (old_top == scr->clip_icon)
1725 scr->clip_icon = dock->icon_array[0];
1727 wAppIconDestroy(old_top);
1730 finish:
1731 WMReleasePropList(dock_state);
1733 return dock;
1736 void wDockLaunchWithState(WDock *dock, WAppIcon *btn, WSavedState *state)
1738 if (btn && btn->command && !btn->running && !btn->launching) {
1739 btn->drop_launch = 0;
1740 btn->paste_launch = 0;
1742 btn->pid = execCommand(btn, btn->command, state);
1744 if (btn->pid > 0) {
1745 if (!btn->forced_dock && !btn->buggy_app) {
1746 btn->launching = 1;
1747 dockIconPaint(btn);
1750 } else {
1751 wfree(state);
1755 void wDockDoAutoLaunch(WDock *dock, int workspace)
1757 WAppIcon *btn;
1758 WSavedState *state;
1759 int i;
1761 for (i = 0; i < dock->max_icons; i++) {
1762 btn = dock->icon_array[i];
1763 if (!btn || !btn->auto_launch)
1764 continue;
1766 state = wmalloc(sizeof(WSavedState));
1767 state->workspace = workspace;
1768 /* TODO: this is klugy and is very difficult to understand
1769 * what's going on. Try to clean up */
1770 wDockLaunchWithState(dock, btn, state);
1774 #ifdef XDND /* was OFFIX */
1775 static WDock *findDock(WScreen *scr, XEvent *event, int *icon_pos)
1777 WDock *dock;
1778 int i;
1780 *icon_pos = -1;
1781 if ((dock = scr->dock) != NULL) {
1782 for (i = 0; i < dock->max_icons; i++) {
1783 if (dock->icon_array[i]
1784 && dock->icon_array[i]->icon->core->window == event->xclient.window) {
1785 *icon_pos = i;
1786 break;
1790 if (*icon_pos < 0 && (dock = scr->workspaces[scr->current_workspace]->clip) != NULL) {
1791 for (i = 0; i < dock->max_icons; i++) {
1792 if (dock->icon_array[i]
1793 && dock->icon_array[i]->icon->core->window == event->xclient.window) {
1794 *icon_pos = i;
1795 break;
1799 if (*icon_pos >= 0)
1800 return dock;
1801 return NULL;
1804 int wDockReceiveDNDDrop(WScreen *scr, XEvent *event)
1806 WDock *dock;
1807 WAppIcon *btn;
1808 int icon_pos;
1810 dock = findDock(scr, event, &icon_pos);
1811 if (!dock)
1812 return False;
1815 * Return True if the drop was on an application icon window.
1816 * In this case, let the ClientMessage handler redirect the
1817 * message to the app.
1819 if (dock->icon_array[icon_pos]->icon->icon_win != None)
1820 return True;
1822 if (dock->icon_array[icon_pos]->dnd_command != NULL) {
1823 scr->flags.dnd_data_convertion_status = 0;
1825 btn = dock->icon_array[icon_pos];
1827 if (!btn->forced_dock) {
1828 btn->relaunching = btn->running;
1829 btn->running = 1;
1831 if (btn->wm_instance || btn->wm_class) {
1832 WWindowAttributes attr;
1833 memset(&attr, 0, sizeof(WWindowAttributes));
1834 wDefaultFillAttributes(btn->wm_instance, btn->wm_class, &attr, NULL, True);
1836 if (!attr.no_appicon)
1837 btn->launching = 1;
1838 else
1839 btn->running = 0;
1842 btn->paste_launch = 0;
1843 btn->drop_launch = 1;
1844 scr->last_dock = dock;
1845 btn->pid = execCommand(btn, btn->dnd_command, NULL);
1846 if (btn->pid > 0) {
1847 dockIconPaint(btn);
1848 } else {
1849 btn->launching = 0;
1850 if (!btn->relaunching)
1851 btn->running = 0;
1854 return False;
1856 #endif /* XDND */
1858 Bool wDockAttachIcon(WDock *dock, WAppIcon *icon, int x, int y, Bool update_icon)
1860 WWindow *wwin;
1861 Bool lupdate_icon = False;
1862 char *command = NULL;
1863 int index;
1865 icon->editing = 0;
1867 if (update_icon)
1868 lupdate_icon = True;
1870 if (icon->command == NULL) {
1871 /* If icon->owner exists, it means the application is running */
1872 if (icon->icon->owner) {
1873 wwin = icon->icon->owner;
1874 command = GetCommandForWindow(wwin->client_win);
1877 if (command) {
1878 icon->command = command;
1879 } else {
1880 /* icon->forced_dock = 1; */
1881 if (dock->type != WM_CLIP || !icon->attracted) {
1882 icon->editing = 1;
1883 if (wInputDialog(dock->screen_ptr, _("Dock Icon"),
1884 _("Type the command used to launch the application"), &command)) {
1885 if (command && (command[0] == 0 || (command[0] == '-' && command[1] == 0))) {
1886 wfree(command);
1887 command = NULL;
1889 icon->command = command;
1890 icon->editing = 0;
1891 } else {
1892 icon->editing = 0;
1893 if (command)
1894 wfree(command);
1895 /* If the target is the dock, reject the icon. If
1896 * the target is the clip, make it an attracted icon
1898 if (dock->type == WM_CLIP) {
1899 icon->attracted = 1;
1900 if (!icon->icon->shadowed) {
1901 icon->icon->shadowed = 1;
1902 lupdate_icon = True;
1904 } else {
1905 return False;
1912 for (index = 1; index < dock->max_icons; index++)
1913 if (dock->icon_array[index] == NULL)
1914 break;
1915 /* if (index == dock->max_icons)
1916 return; */
1918 assert(index < dock->max_icons);
1920 dock->icon_array[index] = icon;
1921 icon->yindex = y;
1922 icon->xindex = x;
1924 icon->omnipresent = 0;
1926 icon->x_pos = dock->x_pos + x * ICON_SIZE;
1927 icon->y_pos = dock->y_pos + y * ICON_SIZE;
1929 dock->icon_count++;
1931 icon->running = 1;
1932 icon->launching = 0;
1933 icon->docked = 1;
1934 icon->dock = dock;
1935 icon->icon->core->descriptor.handle_mousedown = iconMouseDown;
1936 if (dock->type == WM_CLIP) {
1937 icon->icon->core->descriptor.handle_enternotify = clipEnterNotify;
1938 icon->icon->core->descriptor.handle_leavenotify = clipLeaveNotify;
1940 icon->icon->core->descriptor.parent_type = WCLASS_DOCK_ICON;
1941 icon->icon->core->descriptor.parent = icon;
1943 MoveInStackListUnder(dock->icon_array[index - 1]->icon->core, icon->icon->core);
1944 wAppIconMove(icon, icon->x_pos, icon->y_pos);
1947 * Update icon pixmap, RImage doesn't change,
1948 * so call wIconUpdate is not needed
1950 if (lupdate_icon)
1951 update_icon_pixmap(icon->icon);
1953 /* Paint it */
1954 wAppIconPaint(icon);
1956 /* Save it */
1957 save_appicon(icon, True);
1959 if (wPreferences.auto_arrange_icons)
1960 wArrangeIcons(dock->screen_ptr, True);
1962 #ifdef XDND /* was OFFIX */
1963 if (icon->command && !icon->dnd_command) {
1964 int len = strlen(icon->command) + 8;
1965 icon->dnd_command = wmalloc(len);
1966 snprintf(icon->dnd_command, len, "%s %%d", icon->command);
1968 #endif
1970 if (icon->command && !icon->paste_command) {
1971 int len = strlen(icon->command) + 8;
1972 icon->paste_command = wmalloc(len);
1973 snprintf(icon->paste_command, len, "%s %%s", icon->command);
1976 return True;
1979 static void reattachIcon(WDock *dock, WAppIcon *icon, int x, int y)
1981 int index;
1983 for (index = 1; index < dock->max_icons; index++) {
1984 if (dock->icon_array[index] == icon)
1985 break;
1987 assert(index < dock->max_icons);
1989 icon->yindex = y;
1990 icon->xindex = x;
1992 icon->x_pos = dock->x_pos + x * ICON_SIZE;
1993 icon->y_pos = dock->y_pos + y * ICON_SIZE;
1996 static Bool moveIconBetweenDocks(WDock *src, WDock *dest, WAppIcon *icon, int x, int y)
1998 WWindow *wwin;
1999 char *command = NULL;
2000 int index;
2001 Bool update_icon = False;
2003 if (src == dest)
2004 return True; /* No move needed, we're already there */
2006 if (dest == NULL)
2007 return False;
2010 * For the moment we can't do this if we move icons in Clip from one
2011 * workspace to other, because if we move two or more icons without
2012 * command, the dialog box will not be able to tell us to which of the
2013 * moved icons it applies. -Dan
2015 if ((dest->type == WM_DOCK /*|| dest->keep_attracted */ ) && icon->command == NULL) {
2016 /* If icon->owner exists, it means the application is running */
2017 if (icon->icon->owner) {
2018 wwin = icon->icon->owner;
2019 command = GetCommandForWindow(wwin->client_win);
2022 if (command) {
2023 icon->command = command;
2024 } else {
2025 icon->editing = 1;
2026 /* icon->forced_dock = 1; */
2027 if (wInputDialog(src->screen_ptr, _("Dock Icon"),
2028 _("Type the command used to launch the application"), &command)) {
2029 if (command && (command[0] == 0 || (command[0] == '-' && command[1] == 0))) {
2030 wfree(command);
2031 command = NULL;
2033 icon->command = command;
2034 } else {
2035 icon->editing = 0;
2036 if (command)
2037 wfree(command);
2038 return False;
2040 icon->editing = 0;
2044 if (dest->type == WM_DOCK)
2045 wClipMakeIconOmnipresent(icon, False);
2047 for (index = 1; index < src->max_icons; index++) {
2048 if (src->icon_array[index] == icon)
2049 break;
2051 assert(index < src->max_icons);
2053 src->icon_array[index] = NULL;
2054 src->icon_count--;
2056 for (index = 1; index < dest->max_icons; index++) {
2057 if (dest->icon_array[index] == NULL)
2058 break;
2061 assert(index < dest->max_icons);
2063 dest->icon_array[index] = icon;
2064 icon->dock = dest;
2066 /* deselect the icon */
2067 if (icon->icon->selected)
2068 wIconSelect(icon->icon);
2070 if (dest->type == WM_DOCK) {
2071 icon->icon->core->descriptor.handle_enternotify = NULL;
2072 icon->icon->core->descriptor.handle_leavenotify = NULL;
2073 } else {
2074 icon->icon->core->descriptor.handle_enternotify = clipEnterNotify;
2075 icon->icon->core->descriptor.handle_leavenotify = clipLeaveNotify;
2078 /* set it to be kept when moving to dock.
2079 * Unless the icon does not have a command set
2081 if (icon->command && dest->type == WM_DOCK) {
2082 icon->attracted = 0;
2083 if (icon->icon->shadowed) {
2084 icon->icon->shadowed = 0;
2085 update_icon = True;
2087 save_appicon(icon, True);
2090 if (src->auto_collapse || src->auto_raise_lower)
2091 clipLeave(src);
2093 icon->yindex = y;
2094 icon->xindex = x;
2096 icon->x_pos = dest->x_pos + x * ICON_SIZE;
2097 icon->y_pos = dest->y_pos + y * ICON_SIZE;
2099 dest->icon_count++;
2101 MoveInStackListUnder(dest->icon_array[index - 1]->icon->core, icon->icon->core);
2104 * Update icon pixmap, RImage doesn't change,
2105 * so call wIconUpdate is not needed
2107 if (update_icon)
2108 update_icon_pixmap(icon->icon);
2110 /* Paint it */
2111 wAppIconPaint(icon);
2113 return True;
2116 void wDockDetach(WDock *dock, WAppIcon *icon)
2118 int index;
2119 Bool update_icon = False;
2121 /* make the settings panel be closed */
2122 if (icon->panel)
2123 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 update_icon = True;
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;
2162 assert(index < dock->max_icons);
2163 dock->icon_array[index] = NULL;
2164 icon->yindex = -1;
2165 icon->xindex = -1;
2167 dock->icon_count--;
2169 /* if the dock is not attached to an application or
2170 * the application did not set the appropriate hints yet,
2171 * destroy the icon */
2172 if (!icon->running || !wApplicationOf(icon->main_window)) {
2173 wAppIconDestroy(icon);
2174 } else {
2175 icon->icon->core->descriptor.handle_mousedown = appIconMouseDown;
2176 icon->icon->core->descriptor.handle_enternotify = NULL;
2177 icon->icon->core->descriptor.handle_leavenotify = NULL;
2178 icon->icon->core->descriptor.parent_type = WCLASS_APPICON;
2179 icon->icon->core->descriptor.parent = icon;
2181 ChangeStackingLevel(icon->icon->core, NORMAL_ICON_LEVEL);
2184 * Update icon pixmap, RImage doesn't change,
2185 * so call wIconUpdate is not needed
2187 if (update_icon)
2188 update_icon_pixmap(icon->icon);
2190 /* Paint it */
2191 wAppIconPaint(icon);
2193 if (wPreferences.auto_arrange_icons)
2194 wArrangeIcons(dock->screen_ptr, True);
2196 if (dock->auto_collapse || dock->auto_raise_lower)
2197 clipLeave(dock);
2201 * returns the closest Dock slot index for the passed
2202 * coordinates.
2204 * Returns False if icon can't be docked.
2206 * Note: this function should NEVER alter ret_x or ret_y, unless it will
2207 * return True. -Dan
2209 Bool wDockSnapIcon(WDock *dock, WAppIcon *icon, int req_x, int req_y, int *ret_x, int *ret_y, int redocking)
2211 WScreen *scr = dock->screen_ptr;
2212 int dx, dy;
2213 int ex_x, ex_y;
2214 int i, offset = ICON_SIZE / 2;
2215 WAppIcon *aicon = NULL;
2216 WAppIcon *nicon = NULL;
2217 int max_y_icons;
2219 /* TODO: XINERAMA, for these */
2220 max_y_icons = scr->scr_height / ICON_SIZE - 1;
2222 if (wPreferences.flags.noupdates)
2223 return False;
2225 dx = dock->x_pos;
2226 dy = dock->y_pos;
2228 /* if the dock is full */
2229 if (!redocking && (dock->icon_count >= dock->max_icons))
2230 return False;
2232 /* exact position */
2233 if (req_y < dy)
2234 ex_y = (req_y - offset - dy) / ICON_SIZE;
2235 else
2236 ex_y = (req_y + offset - dy) / ICON_SIZE;
2238 if (req_x < dx)
2239 ex_x = (req_x - offset - dx) / ICON_SIZE;
2240 else
2241 ex_x = (req_x + offset - dx) / ICON_SIZE;
2243 /* check if the icon is outside the screen boundaries */
2245 WMRect rect;
2246 int flags;
2248 rect.pos.x = dx + ex_x * ICON_SIZE;
2249 rect.pos.y = dy + ex_y * ICON_SIZE;
2250 rect.size.width = rect.size.height = ICON_SIZE;
2252 wGetRectPlacementInfo(scr, rect, &flags);
2253 if (flags & (XFLAG_DEAD | XFLAG_PARTIAL))
2254 return False;
2257 if (dock->type == WM_DOCK) {
2258 if (icon->dock != dock && ex_x != 0)
2259 return False;
2261 aicon = NULL;
2262 for (i = 0; i < dock->max_icons; i++) {
2263 nicon = dock->icon_array[i];
2264 if (nicon && nicon->yindex == ex_y) {
2265 aicon = nicon;
2266 break;
2270 if (redocking) {
2271 int sig, done, closest;
2273 /* Possible cases when redocking:
2275 * icon dragged out of range of any slot -> false
2276 * icon dragged to range of free slot
2277 * icon dragged to range of same slot
2278 * icon dragged to range of different icon
2280 if (abs(ex_x) > DOCK_DETTACH_THRESHOLD)
2281 return False;
2283 if (ex_y >= 0 && ex_y <= max_y_icons && (aicon == icon || !aicon)) {
2284 *ret_x = 0;
2285 *ret_y = ex_y;
2286 return True;
2289 /* start looking at the upper slot or lower? */
2290 if (ex_y * ICON_SIZE < (req_y + offset - dy))
2291 sig = 1;
2292 else
2293 sig = -1;
2295 closest = -1;
2296 done = 0;
2297 /* look for closest free slot */
2298 for (i = 0; i < (DOCK_DETTACH_THRESHOLD + 1) * 2 && !done; i++) {
2299 int j;
2301 done = 1;
2302 closest = sig * (i / 2) + ex_y;
2303 /* check if this slot is used */
2304 if (closest >= 0) {
2305 for (j = 0; j < dock->max_icons; j++) {
2306 if (dock->icon_array[j]
2307 && dock->icon_array[j]->yindex == closest) {
2308 /* slot is used by someone else */
2309 if (dock->icon_array[j] != icon)
2310 done = 0;
2311 break;
2315 sig = -sig;
2317 if (done && closest >= 0 && closest <= max_y_icons &&
2318 ((ex_y >= closest && ex_y - closest < DOCK_DETTACH_THRESHOLD + 1)
2319 || (ex_y < closest && closest - ex_y <= DOCK_DETTACH_THRESHOLD + 1))) {
2320 *ret_x = 0;
2321 *ret_y = closest;
2322 return True;
2324 } else { /* !redocking */
2326 /* if slot is free and the icon is close enough, return it */
2327 if (!aicon && ex_x == 0 && ex_y >= 0 && ex_y <= max_y_icons) {
2328 *ret_x = 0;
2329 *ret_y = ex_y;
2330 return True;
2333 } else { /* CLIP */
2334 int neighbours = 0;
2335 int start, stop, k;
2337 start = icon->omnipresent ? 0 : scr->current_workspace;
2338 stop = icon->omnipresent ? scr->workspace_count : start + 1;
2340 aicon = NULL;
2341 for (k = start; k < stop; k++) {
2342 WDock *tmp = scr->workspaces[k]->clip;
2343 if (!tmp)
2344 continue;
2345 for (i = 0; i < tmp->max_icons; i++) {
2346 nicon = tmp->icon_array[i];
2347 if (nicon && nicon->xindex == ex_x && nicon->yindex == ex_y) {
2348 aicon = nicon;
2349 break;
2352 if (aicon)
2353 break;
2355 for (k = start; k < stop; k++) {
2356 WDock *tmp = scr->workspaces[k]->clip;
2357 if (!tmp)
2358 continue;
2359 for (i = 0; i < tmp->max_icons; i++) {
2360 nicon = tmp->icon_array[i];
2361 if (nicon && nicon != icon && /* Icon can't be it's own neighbour */
2362 (abs(nicon->xindex - ex_x) <= CLIP_ATTACH_VICINITY &&
2363 abs(nicon->yindex - ex_y) <= CLIP_ATTACH_VICINITY)) {
2364 neighbours = 1;
2365 break;
2368 if (neighbours)
2369 break;
2372 if (neighbours && (aicon == NULL || (redocking && aicon == icon))) {
2373 *ret_x = ex_x;
2374 *ret_y = ex_y;
2375 return True;
2378 return False;
2381 static int onScreen(WScreen *scr, int x, int y, int sx, int ex, int sy, int ey)
2383 WMRect rect = wmkrect(x, y, ICON_SIZE, ICON_SIZE);
2384 int flags;
2386 wGetRectPlacementInfo(scr, rect, &flags);
2388 return !(flags & (XFLAG_DEAD | XFLAG_PARTIAL));
2392 * returns true if it can find a free slot in the dock,
2393 * in which case it changes x_pos and y_pos accordingly.
2394 * Else returns false.
2396 Bool wDockFindFreeSlot(WDock *dock, int *x_pos, int *y_pos)
2398 WScreen *scr = dock->screen_ptr;
2399 WAppIcon *btn;
2400 WAppIconChain *chain;
2401 unsigned char *slot_map;
2402 int mwidth;
2403 int r;
2404 int x, y;
2405 int i, done = False;
2406 int corner;
2407 int sx = 0, sy = 0, ex = scr->scr_width, ey = scr->scr_height;
2408 int extra_count = 0;
2410 if (dock->type == WM_CLIP && dock != scr->workspaces[scr->current_workspace]->clip)
2411 extra_count = scr->global_icon_count;
2413 /* if the dock is full */
2414 if (dock->icon_count + extra_count >= dock->max_icons)
2415 return False;
2417 if (!wPreferences.flags.nodock && scr->dock) {
2418 if (scr->dock->on_right_side)
2419 ex -= ICON_SIZE + DOCK_EXTRA_SPACE;
2420 else
2421 sx += ICON_SIZE + DOCK_EXTRA_SPACE;
2424 if (ex < dock->x_pos)
2425 ex = dock->x_pos;
2426 if (sx > dock->x_pos + ICON_SIZE)
2427 sx = dock->x_pos + ICON_SIZE;
2428 #define C_NONE 0
2429 #define C_NW 1
2430 #define C_NE 2
2431 #define C_SW 3
2432 #define C_SE 4
2434 /* check if clip is in a corner */
2435 if (dock->type == WM_CLIP) {
2436 if (dock->x_pos < 1 && dock->y_pos < 1)
2437 corner = C_NE;
2438 else if (dock->x_pos < 1 && dock->y_pos >= (ey - ICON_SIZE))
2439 corner = C_SE;
2440 else if (dock->x_pos >= (ex - ICON_SIZE) && dock->y_pos >= (ey - ICON_SIZE))
2441 corner = C_SW;
2442 else if (dock->x_pos >= (ex - ICON_SIZE) && dock->y_pos < 1)
2443 corner = C_NW;
2444 else
2445 corner = C_NONE;
2446 } else {
2447 corner = C_NONE;
2450 /* If the clip is in the corner, use only slots that are in the border
2451 * of the screen */
2452 if (corner != C_NONE) {
2453 char *hmap, *vmap;
2454 int hcount, vcount;
2456 hcount = WMIN(dock->max_icons, scr->scr_width / ICON_SIZE);
2457 vcount = WMIN(dock->max_icons, scr->scr_height / ICON_SIZE);
2458 hmap = wmalloc(hcount + 1);
2459 vmap = wmalloc(vcount + 1);
2461 /* mark used positions */
2462 switch (corner) {
2463 case C_NE:
2464 for (i = 0; i < dock->max_icons; i++) {
2465 btn = dock->icon_array[i];
2466 if (!btn)
2467 continue;
2469 if (btn->xindex == 0 && btn->yindex > 0 && btn->yindex < vcount)
2470 vmap[btn->yindex] = 1;
2471 else if (btn->yindex == 0 && btn->xindex > 0 && btn->xindex < hcount)
2472 hmap[btn->xindex] = 1;
2474 for (chain = scr->global_icons; chain != NULL; chain = chain->next) {
2475 btn = chain->aicon;
2476 if (btn->xindex == 0 && btn->yindex > 0 && btn->yindex < vcount)
2477 vmap[btn->yindex] = 1;
2478 else if (btn->yindex == 0 && btn->xindex > 0 && btn->xindex < hcount)
2479 hmap[btn->xindex] = 1;
2481 break;
2482 case C_NW:
2483 for (i = 0; i < dock->max_icons; i++) {
2484 btn = dock->icon_array[i];
2485 if (!btn)
2486 continue;
2488 if (btn->xindex == 0 && btn->yindex > 0 && btn->yindex < vcount)
2489 vmap[btn->yindex] = 1;
2490 else if (btn->yindex == 0 && btn->xindex < 0 && btn->xindex > -hcount)
2491 hmap[-btn->xindex] = 1;
2493 for (chain = scr->global_icons; chain != NULL; chain = chain->next) {
2494 btn = chain->aicon;
2495 if (btn->xindex == 0 && btn->yindex > 0 && btn->yindex < vcount)
2496 vmap[btn->yindex] = 1;
2497 else if (btn->yindex == 0 && btn->xindex < 0 && btn->xindex > -hcount)
2498 hmap[-btn->xindex] = 1;
2500 break;
2501 case C_SE:
2502 for (i = 0; i < dock->max_icons; i++) {
2503 btn = dock->icon_array[i];
2504 if (!btn)
2505 continue;
2507 if (btn->xindex == 0 && btn->yindex < 0 && btn->yindex > -vcount)
2508 vmap[-btn->yindex] = 1;
2509 else if (btn->yindex == 0 && btn->xindex > 0 && btn->xindex < hcount)
2510 hmap[btn->xindex] = 1;
2512 for (chain = scr->global_icons; chain != NULL; chain = chain->next) {
2513 btn = chain->aicon;
2514 if (btn->xindex == 0 && btn->yindex < 0 && btn->yindex > -vcount)
2515 vmap[-btn->yindex] = 1;
2516 else if (btn->yindex == 0 && btn->xindex > 0 && btn->xindex < hcount)
2517 hmap[btn->xindex] = 1;
2519 break;
2520 case C_SW:
2521 default:
2522 for (i = 0; i < dock->max_icons; i++) {
2523 btn = dock->icon_array[i];
2524 if (!btn)
2525 continue;
2527 if (btn->xindex == 0 && btn->yindex < 0 && btn->yindex > -vcount)
2528 vmap[-btn->yindex] = 1;
2529 else if (btn->yindex == 0 && btn->xindex < 0 && btn->xindex > -hcount)
2530 hmap[-btn->xindex] = 1;
2532 for (chain = scr->global_icons; chain != NULL; chain = chain->next) {
2533 btn = chain->aicon;
2534 if (btn->xindex == 0 && btn->yindex < 0 && btn->yindex > -vcount)
2535 vmap[-btn->yindex] = 1;
2536 else if (btn->yindex == 0 && btn->xindex < 0 && btn->xindex > -hcount)
2537 hmap[-btn->xindex] = 1;
2540 x = 0;
2541 y = 0;
2542 done = 0;
2543 /* search a vacant slot */
2544 for (i = 1; i < WMAX(vcount, hcount); i++) {
2545 if (i < vcount && vmap[i] == 0) {
2546 /* found a slot */
2547 x = 0;
2548 y = i;
2549 done = 1;
2550 break;
2551 } else if (i < hcount && hmap[i] == 0) {
2552 /* found a slot */
2553 x = i;
2554 y = 0;
2555 done = 1;
2556 break;
2559 wfree(vmap);
2560 wfree(hmap);
2561 /* If found a slot, translate and return */
2562 if (done) {
2563 if (corner == C_NW || corner == C_NE)
2564 *y_pos = y;
2565 else
2566 *y_pos = -y;
2568 if (corner == C_NE || corner == C_SE)
2569 *x_pos = x;
2570 else
2571 *x_pos = -x;
2573 return True;
2575 /* else, try to find a slot somewhere else */
2578 /* a map of mwidth x mwidth would be enough if we allowed icons to be
2579 * placed outside of screen */
2580 mwidth = (int)ceil(sqrt(dock->max_icons));
2582 /* In the worst case (the clip is in the corner of the screen),
2583 * the amount of icons that fit in the clip is smaller.
2584 * Double the map to get a safe value.
2586 mwidth += mwidth;
2588 r = (mwidth - 1) / 2;
2590 slot_map = wmalloc(mwidth * mwidth);
2592 #define XY2OFS(x,y) (WMAX(abs(x),abs(y)) > r) ? 0 : (((y)+r)*(mwidth)+(x)+r)
2594 /* mark used slots in the map. If the slot falls outside the map
2595 * (for example, when all icons are placed in line), ignore them. */
2596 for (i = 0; i < dock->max_icons; i++) {
2597 btn = dock->icon_array[i];
2598 if (btn)
2599 slot_map[XY2OFS(btn->xindex, btn->yindex)] = 1;
2602 for (chain = scr->global_icons; chain != NULL; chain = chain->next)
2603 slot_map[XY2OFS(chain->aicon->xindex, chain->aicon->yindex)] = 1;
2605 /* Find closest slot from the center that is free by scanning the
2606 * map from the center to outward in circular passes.
2607 * This will not result in a neat layout, but will be optimal
2608 * in the sense that there will not be holes left.
2610 done = 0;
2611 for (i = 1; i <= r && !done; i++) {
2612 int tx, ty;
2614 /* top and bottom parts of the ring */
2615 for (x = -i; x <= i && !done; x++) {
2616 tx = dock->x_pos + x * ICON_SIZE;
2617 y = -i;
2618 ty = dock->y_pos + y * ICON_SIZE;
2619 if (slot_map[XY2OFS(x, y)] == 0 && onScreen(scr, tx, ty, sx, ex, sy, ey)) {
2620 *x_pos = x;
2621 *y_pos = y;
2622 done = 1;
2623 break;
2625 y = i;
2626 ty = dock->y_pos + y * ICON_SIZE;
2627 if (slot_map[XY2OFS(x, y)] == 0 && onScreen(scr, tx, ty, sx, ex, sy, ey)) {
2628 *x_pos = x;
2629 *y_pos = y;
2630 done = 1;
2631 break;
2634 /* left and right parts of the ring */
2635 for (y = -i + 1; y <= i - 1; y++) {
2636 ty = dock->y_pos + y * ICON_SIZE;
2637 x = -i;
2638 tx = dock->x_pos + x * ICON_SIZE;
2639 if (slot_map[XY2OFS(x, y)] == 0 && onScreen(scr, tx, ty, sx, ex, sy, ey)) {
2640 *x_pos = x;
2641 *y_pos = y;
2642 done = 1;
2643 break;
2645 x = i;
2646 tx = dock->x_pos + x * ICON_SIZE;
2647 if (slot_map[XY2OFS(x, y)] == 0 && onScreen(scr, tx, ty, sx, ex, sy, ey)) {
2648 *x_pos = x;
2649 *y_pos = y;
2650 done = 1;
2651 break;
2655 wfree(slot_map);
2656 #undef XY2OFS
2657 return done;
2660 static void moveDock(WDock *dock, int new_x, int new_y)
2662 WAppIcon *btn;
2663 int i;
2665 dock->x_pos = new_x;
2666 dock->y_pos = new_y;
2667 for (i = 0; i < dock->max_icons; i++) {
2668 btn = dock->icon_array[i];
2669 if (btn) {
2670 btn->x_pos = new_x + btn->xindex * ICON_SIZE;
2671 btn->y_pos = new_y + btn->yindex * ICON_SIZE;
2672 XMoveWindow(dpy, btn->icon->core->window, btn->x_pos, btn->y_pos);
2677 static void swapDock(WDock *dock)
2679 WScreen *scr = dock->screen_ptr;
2680 WAppIcon *btn;
2681 int x, i;
2683 if (dock->on_right_side)
2684 x = dock->x_pos = scr->scr_width - ICON_SIZE - DOCK_EXTRA_SPACE;
2685 else
2686 x = dock->x_pos = DOCK_EXTRA_SPACE;
2688 for (i = 0; i < dock->max_icons; i++) {
2689 btn = dock->icon_array[i];
2690 if (btn) {
2691 btn->x_pos = x;
2692 XMoveWindow(dpy, btn->icon->core->window, btn->x_pos, btn->y_pos);
2696 wScreenUpdateUsableArea(scr);
2699 static pid_t execCommand(WAppIcon *btn, char *command, WSavedState *state)
2701 WScreen *scr = btn->icon->core->screen_ptr;
2702 pid_t pid;
2703 char **argv;
2704 int argc;
2705 char *cmdline;
2707 cmdline = ExpandOptions(scr, command);
2709 if (scr->flags.dnd_data_convertion_status || !cmdline) {
2710 if (cmdline)
2711 wfree(cmdline);
2712 if (state)
2713 wfree(state);
2714 return 0;
2717 wtokensplit(cmdline, &argv, &argc);
2719 if (!argc) {
2720 if (cmdline)
2721 wfree(cmdline);
2722 if (state)
2723 wfree(state);
2724 return 0;
2727 if ((pid = fork()) == 0) {
2728 char **args;
2729 int i;
2731 SetupEnvironment(scr);
2733 #ifdef HAVE_SETSID
2734 setsid();
2735 #endif
2737 args = malloc(sizeof(char *) * (argc + 1));
2738 if (!args)
2739 exit(111);
2741 for (i = 0; i < argc; i++)
2742 args[i] = argv[i];
2744 args[argc] = NULL;
2745 execvp(argv[0], args);
2746 exit(111);
2748 wtokenfree(argv, argc);
2750 if (pid > 0) {
2751 if (!state) {
2752 state = wmalloc(sizeof(WSavedState));
2753 state->hidden = -1;
2754 state->miniaturized = -1;
2755 state->shaded = -1;
2756 if (btn->dock == scr->dock || btn->omnipresent)
2757 state->workspace = -1;
2758 else
2759 state->workspace = scr->current_workspace;
2761 wWindowAddSavedState(btn->wm_instance, btn->wm_class, cmdline, pid, state);
2762 wAddDeathHandler(pid, (WDeathHandler *) trackDeadProcess, btn->dock);
2763 } else if (state) {
2764 wfree(state);
2766 wfree(cmdline);
2767 return pid;
2770 void wDockHideIcons(WDock *dock)
2772 int i;
2774 if (dock == NULL)
2775 return;
2777 for (i = 1; i < dock->max_icons; i++) {
2778 if (dock->icon_array[i])
2779 XUnmapWindow(dpy, dock->icon_array[i]->icon->core->window);
2781 dock->mapped = 0;
2783 dockIconPaint(dock->icon_array[0]);
2786 void wDockShowIcons(WDock *dock)
2788 int i, newlevel;
2789 WAppIcon *btn;
2791 if (dock == NULL)
2792 return;
2794 btn = dock->icon_array[0];
2795 moveDock(dock, btn->x_pos, btn->y_pos);
2797 newlevel = dock->lowered ? WMNormalLevel : WMDockLevel;
2798 ChangeStackingLevel(btn->icon->core, newlevel);
2800 for (i = 1; i < dock->max_icons; i++) {
2801 if (dock->icon_array[i]) {
2802 MoveInStackListAbove(dock->icon_array[i]->icon->core, btn->icon->core);
2803 break;
2807 if (!dock->collapsed) {
2808 for (i = 1; i < dock->max_icons; i++) {
2809 if (dock->icon_array[i])
2810 XMapWindow(dpy, dock->icon_array[i]->icon->core->window);
2813 dock->mapped = 1;
2815 dockIconPaint(btn);
2818 void wDockLower(WDock *dock)
2820 int i;
2822 for (i = 0; i < dock->max_icons; i++) {
2823 if (dock->icon_array[i])
2824 wLowerFrame(dock->icon_array[i]->icon->core);
2828 void wDockRaise(WDock *dock)
2830 int i;
2832 for (i = dock->max_icons - 1; i >= 0; i--) {
2833 if (dock->icon_array[i])
2834 wRaiseFrame(dock->icon_array[i]->icon->core);
2838 void wDockRaiseLower(WDock *dock)
2840 if (!dock->icon_array[0]->icon->core->stacking->above
2841 || (dock->icon_array[0]->icon->core->stacking->window_level
2842 != dock->icon_array[0]->icon->core->stacking->above->stacking->window_level))
2843 wDockLower(dock);
2844 else
2845 wDockRaise(dock);
2848 void wDockFinishLaunch(WDock *dock, WAppIcon *icon)
2850 icon->launching = 0;
2851 icon->relaunching = 0;
2852 dockIconPaint(icon);
2855 WAppIcon *wDockFindIconForWindow(WDock *dock, Window window)
2857 WAppIcon *icon;
2858 int i;
2860 for (i = 0; i < dock->max_icons; i++) {
2861 icon = dock->icon_array[i];
2862 if (icon && icon->main_window == window)
2863 return icon;
2865 return NULL;
2868 void wDockTrackWindowLaunch(WDock *dock, Window window)
2870 WAppIcon *icon;
2871 char *wm_class, *wm_instance;
2872 int i;
2873 Bool firstPass = True;
2874 Bool found = False;
2875 char *command = NULL;
2877 if (!PropGetWMClass(window, &wm_class, &wm_instance) || (!wm_class && !wm_instance))
2878 return;
2880 command = GetCommandForWindow(window);
2881 retry:
2882 for (i = 0; i < dock->max_icons; i++) {
2883 icon = dock->icon_array[i];
2884 if (!icon)
2885 continue;
2887 /* app is already attached to icon */
2888 if (icon->main_window == window) {
2889 found = True;
2890 break;
2893 if ((icon->wm_instance || icon->wm_class)
2894 && (icon->launching || !icon->running)) {
2896 if (icon->wm_instance && wm_instance && strcmp(icon->wm_instance, wm_instance) != 0)
2897 continue;
2899 if (icon->wm_class && wm_class && strcmp(icon->wm_class, wm_class) != 0)
2900 continue;
2902 if (firstPass && command && strcmp(icon->command, command) != 0)
2903 continue;
2905 if (!icon->relaunching) {
2906 WApplication *wapp;
2908 /* Possibly an application that was docked with dockit,
2909 * but the user did not update WMState to indicate that
2910 * it was docked by force */
2911 wapp = wApplicationOf(window);
2912 if (!wapp) {
2913 icon->forced_dock = 1;
2914 icon->running = 0;
2916 if (!icon->forced_dock)
2917 icon->main_window = window;
2919 found = True;
2920 if (!wPreferences.no_animations && !icon->launching &&
2921 !dock->screen_ptr->flags.startup && !dock->collapsed) {
2922 WAppIcon *aicon;
2923 int x0, y0;
2925 icon->launching = 1;
2926 dockIconPaint(icon);
2928 aicon = wAppIconCreateForDock(dock->screen_ptr, NULL,
2929 wm_instance, wm_class, TILE_NORMAL);
2930 /* XXX: can: aicon->icon == NULL ? */
2931 PlaceIcon(dock->screen_ptr, &x0, &y0, wGetHeadForWindow(aicon->icon->owner));
2932 wAppIconMove(aicon, x0, y0);
2933 /* Should this always be lowered? -Dan */
2934 if (dock->lowered)
2935 wLowerFrame(aicon->icon->core);
2936 XMapWindow(dpy, aicon->icon->core->window);
2937 aicon->launching = 1;
2938 wAppIconPaint(aicon);
2939 SlideWindow(aicon->icon->core->window, x0, y0, icon->x_pos, icon->y_pos);
2940 XUnmapWindow(dpy, aicon->icon->core->window);
2941 wAppIconDestroy(aicon);
2943 wDockFinishLaunch(dock, icon);
2944 break;
2948 if (firstPass && !found) {
2949 firstPass = False;
2950 goto retry;
2953 if (command)
2954 wfree(command);
2956 if (wm_class)
2957 free(wm_class);
2958 if (wm_instance)
2959 free(wm_instance);
2962 void wClipUpdateForWorkspaceChange(WScreen *scr, int workspace)
2964 if (!wPreferences.flags.noclip) {
2965 scr->clip_icon->dock = scr->workspaces[workspace]->clip;
2966 if (scr->current_workspace != workspace) {
2967 WDock *old_clip = scr->workspaces[scr->current_workspace]->clip;
2968 WAppIconChain *chain = scr->global_icons;
2970 while (chain) {
2971 moveIconBetweenDocks(chain->aicon->dock,
2972 scr->workspaces[workspace]->clip,
2973 chain->aicon, chain->aicon->xindex, chain->aicon->yindex);
2974 if (scr->workspaces[workspace]->clip->collapsed)
2975 XUnmapWindow(dpy, chain->aicon->icon->core->window);
2976 chain = chain->next;
2979 wDockHideIcons(old_clip);
2980 if (old_clip->auto_raise_lower) {
2981 if (old_clip->auto_raise_magic) {
2982 WMDeleteTimerHandler(old_clip->auto_raise_magic);
2983 old_clip->auto_raise_magic = NULL;
2985 wDockLower(old_clip);
2987 if (old_clip->auto_collapse) {
2988 if (old_clip->auto_expand_magic) {
2989 WMDeleteTimerHandler(old_clip->auto_expand_magic);
2990 old_clip->auto_expand_magic = NULL;
2992 old_clip->collapsed = 1;
2994 wDockShowIcons(scr->workspaces[workspace]->clip);
2999 static void trackDeadProcess(pid_t pid, unsigned char status, WDock *dock)
3001 WAppIcon *icon;
3002 int i;
3004 for (i = 0; i < dock->max_icons; i++) {
3005 icon = dock->icon_array[i];
3006 if (!icon)
3007 continue;
3009 if (icon->launching && icon->pid == pid) {
3010 if (!icon->relaunching) {
3011 icon->running = 0;
3012 icon->main_window = None;
3014 wDockFinishLaunch(dock, icon);
3015 icon->pid = 0;
3016 if (status == 111) {
3017 char msg[PATH_MAX];
3018 char *cmd;
3020 #ifdef XDND
3021 if (icon->drop_launch)
3022 cmd = icon->dnd_command;
3023 else
3024 #endif
3025 if (icon->paste_launch)
3026 cmd = icon->paste_command;
3027 else
3028 cmd = icon->command;
3030 snprintf(msg, sizeof(msg), _("Could not execute command \"%s\""), cmd);
3032 wMessageDialog(dock->screen_ptr, _("Error"), msg, _("OK"), NULL, NULL);
3034 break;
3039 static void toggleLowered(WDock *dock)
3041 WAppIcon *tmp;
3042 int newlevel, i;
3044 /* lower/raise Dock */
3045 if (!dock->lowered) {
3046 newlevel = WMNormalLevel;
3047 dock->lowered = 1;
3048 } else {
3049 newlevel = WMDockLevel;
3050 dock->lowered = 0;
3053 for (i = 0; i < dock->max_icons; i++) {
3054 tmp = dock->icon_array[i];
3055 if (!tmp)
3056 continue;
3058 ChangeStackingLevel(tmp->icon->core, newlevel);
3059 if (dock->lowered)
3060 wLowerFrame(tmp->icon->core);
3063 if (dock->type == WM_DOCK)
3064 wScreenUpdateUsableArea(dock->screen_ptr);
3067 static void toggleCollapsed(WDock *dock)
3069 if (dock->collapsed) {
3070 dock->collapsed = 0;
3071 wDockShowIcons(dock);
3072 } else {
3073 dock->collapsed = 1;
3074 wDockHideIcons(dock);
3078 static void openDockMenu(WDock *dock, WAppIcon *aicon, XEvent *event)
3080 WScreen *scr = dock->screen_ptr;
3081 WObjDescriptor *desc;
3082 WMenuEntry *entry;
3083 WApplication *wapp = NULL;
3084 int index = 0;
3085 int x_pos;
3086 int n_selected;
3087 int appIsRunning = aicon->running && aicon->icon && aicon->icon->owner;
3089 if (dock->type == WM_DOCK) {
3090 /* keep on top */
3091 entry = dock->menu->entries[index];
3092 entry->flags.indicator_on = !dock->lowered;
3093 entry->clientdata = dock;
3094 dock->menu->flags.realized = 0;
3095 } else {
3096 /* clip options */
3097 if (scr->clip_options)
3098 updateClipOptionsMenu(scr->clip_options, dock);
3100 n_selected = numberOfSelectedIcons(dock);
3102 /* Rename Workspace */
3103 entry = dock->menu->entries[++index];
3104 if (aicon == scr->clip_icon) {
3105 entry->callback = renameCallback;
3106 entry->clientdata = dock;
3107 entry->flags.indicator = 0;
3108 entry->text = _("Rename Workspace");
3109 } else {
3110 entry->callback = omnipresentCallback;
3111 entry->clientdata = aicon;
3112 if (n_selected > 0) {
3113 entry->flags.indicator = 0;
3114 entry->text = _("Toggle Omnipresent");
3115 } else {
3116 entry->flags.indicator = 1;
3117 entry->flags.indicator_on = aicon->omnipresent;
3118 entry->flags.indicator_type = MI_CHECK;
3119 entry->text = _("Omnipresent");
3123 /* select/unselect icon */
3124 entry = dock->menu->entries[++index];
3125 entry->clientdata = aicon;
3126 entry->flags.indicator_on = aicon->icon->selected;
3127 wMenuSetEnabled(dock->menu, index, aicon != scr->clip_icon);
3129 /* select/unselect all icons */
3130 entry = dock->menu->entries[++index];
3131 entry->clientdata = aicon;
3132 if (n_selected > 0)
3133 entry->text = _("Unselect All Icons");
3134 else
3135 entry->text = _("Select All Icons");
3137 wMenuSetEnabled(dock->menu, index, dock->icon_count > 1);
3139 /* keep icon(s) */
3140 entry = dock->menu->entries[++index];
3141 entry->clientdata = aicon;
3142 if (n_selected > 1)
3143 entry->text = _("Keep Icons");
3144 else
3145 entry->text = _("Keep Icon");
3147 wMenuSetEnabled(dock->menu, index, dock->icon_count > 1);
3149 /* this is the workspace submenu part */
3150 entry = dock->menu->entries[++index];
3151 if (n_selected > 1)
3152 entry->text = _("Move Icons To");
3153 else
3154 entry->text = _("Move Icon To");
3156 if (scr->clip_submenu)
3157 updateWorkspaceMenu(scr->clip_submenu, aicon);
3159 wMenuSetEnabled(dock->menu, index, !aicon->omnipresent);
3161 /* remove icon(s) */
3162 entry = dock->menu->entries[++index];
3163 entry->clientdata = aicon;
3164 if (n_selected > 1)
3165 entry->text = _("Remove Icons");
3166 else
3167 entry->text = _("Remove Icon");
3169 wMenuSetEnabled(dock->menu, index, dock->icon_count > 1);
3171 /* attract icon(s) */
3172 entry = dock->menu->entries[++index];
3173 entry->clientdata = aicon;
3175 dock->menu->flags.realized = 0;
3176 wMenuRealize(dock->menu);
3179 if (aicon->icon->owner)
3180 wapp = wApplicationOf(aicon->icon->owner->main_window);
3181 else
3182 wapp = NULL;
3184 /* launch */
3185 entry = dock->menu->entries[++index];
3186 entry->clientdata = aicon;
3187 wMenuSetEnabled(dock->menu, index, aicon->command != NULL);
3189 /* unhide here */
3190 entry = dock->menu->entries[++index];
3191 entry->clientdata = aicon;
3192 if (wapp && wapp->flags.hidden)
3193 entry->text = _("Unhide Here");
3194 else
3195 entry->text = _("Bring Here");
3197 wMenuSetEnabled(dock->menu, index, appIsRunning);
3199 /* hide */
3200 entry = dock->menu->entries[++index];
3201 entry->clientdata = aicon;
3202 if (wapp && wapp->flags.hidden)
3203 entry->text = _("Unhide");
3204 else
3205 entry->text = _("Hide");
3207 wMenuSetEnabled(dock->menu, index, appIsRunning);
3209 /* settings */
3210 entry = dock->menu->entries[++index];
3211 entry->clientdata = aicon;
3212 wMenuSetEnabled(dock->menu, index, !aicon->editing && !wPreferences.flags.noupdates);
3214 /* kill */
3215 entry = dock->menu->entries[++index];
3216 entry->clientdata = aicon;
3217 wMenuSetEnabled(dock->menu, index, appIsRunning);
3219 if (!dock->menu->flags.realized)
3220 wMenuRealize(dock->menu);
3222 if (dock->type == WM_CLIP) {
3223 /*x_pos = event->xbutton.x_root+2; */
3224 x_pos = event->xbutton.x_root - dock->menu->frame->core->width / 2 - 1;
3225 if (x_pos < 0) {
3226 x_pos = 0;
3227 } else if (x_pos + dock->menu->frame->core->width > scr->scr_width - 2) {
3228 x_pos = scr->scr_width - dock->menu->frame->core->width - 4;
3230 } else {
3231 x_pos = dock->on_right_side ? scr->scr_width - dock->menu->frame->core->width - 3 : 0;
3234 wMenuMapAt(dock->menu, x_pos, event->xbutton.y_root + 2, False);
3236 /* allow drag select */
3237 event->xany.send_event = True;
3238 desc = &dock->menu->menu->descriptor;
3239 (*desc->handle_mousedown) (desc, event);
3242 /******************************************************************/
3243 static void iconDblClick(WObjDescriptor *desc, XEvent *event)
3245 WAppIcon *btn = desc->parent;
3246 WDock *dock = btn->dock;
3247 WApplication *wapp = NULL;
3248 int unhideHere = 0;
3250 if (btn->icon->owner && !(event->xbutton.state & ControlMask)) {
3251 wapp = wApplicationOf(btn->icon->owner->main_window);
3253 assert(wapp != NULL);
3255 unhideHere = (event->xbutton.state & ShiftMask);
3257 /* go to the last workspace that the user worked on the app */
3258 if (wapp->last_workspace != dock->screen_ptr->current_workspace && !unhideHere)
3259 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);
3265 } else {
3266 if (event->xbutton.button == Button1) {
3267 if (event->xbutton.state & MOD_MASK) {
3268 /* raise/lower dock */
3269 toggleLowered(dock);
3270 } else if (btn == dock->screen_ptr->clip_icon) {
3271 if (getClipButton(event->xbutton.x, event->xbutton.y) == CLIP_IDLE)
3272 toggleCollapsed(dock);
3273 else
3274 handleClipChangeWorkspace(dock->screen_ptr, event);
3275 } else if (btn->command) {
3276 if (!btn->launching && (!btn->running || (event->xbutton.state & ControlMask)))
3277 launchDockedApplication(btn, False);
3283 static void handleDockMove(WDock *dock, WAppIcon *aicon, XEvent *event)
3285 WScreen *scr = dock->screen_ptr;
3286 int ofs_x = event->xbutton.x, ofs_y = event->xbutton.y;
3287 int x, y;
3288 XEvent ev;
3289 int grabbed = 0, swapped = 0, done;
3290 Pixmap ghost = None;
3291 int superfluous = wPreferences.superfluous; /* we catch it to avoid problems */
3293 if (XGrabPointer(dpy, aicon->icon->core->window, True, ButtonMotionMask
3294 | ButtonReleaseMask | ButtonPressMask, GrabModeAsync,
3295 GrabModeAsync, None, None, CurrentTime) != GrabSuccess)
3296 wwarning("pointer grab failed for dock move");
3298 y = 0;
3299 for (x = 0; x < dock->max_icons; x++) {
3300 if (dock->icon_array[x] != NULL && dock->icon_array[x]->yindex > y)
3301 y = dock->icon_array[x]->yindex;
3303 y++;
3304 XResizeWindow(dpy, scr->dock_shadow, ICON_SIZE, ICON_SIZE * y);
3306 done = 0;
3307 while (!done) {
3308 WMMaskEvent(dpy, PointerMotionMask | ButtonReleaseMask | ButtonPressMask
3309 | ButtonMotionMask | ExposureMask | EnterWindowMask, &ev);
3310 switch (ev.type) {
3311 case Expose:
3312 WMHandleEvent(&ev);
3313 break;
3315 case EnterNotify:
3316 /* It means the cursor moved so fast that it entered
3317 * something else (if moving slowly, it would have
3318 * stayed in the dock that is being moved. Ignore such
3319 * "spurious" EnterNotifiy's */
3320 break;
3322 case MotionNotify:
3323 if (!grabbed) {
3324 if (abs(ofs_x - ev.xmotion.x) >= MOVE_THRESHOLD
3325 || abs(ofs_y - ev.xmotion.y) >= MOVE_THRESHOLD) {
3326 XChangeActivePointerGrab(dpy, ButtonMotionMask
3327 | ButtonReleaseMask | ButtonPressMask,
3328 wCursor[WCUR_MOVE], CurrentTime);
3329 grabbed = 1;
3331 break;
3333 if (dock->type == WM_CLIP) {
3334 x = ev.xmotion.x_root - ofs_x;
3335 y = ev.xmotion.y_root - ofs_y;
3336 wScreenKeepInside(scr, &x, &y, ICON_SIZE, ICON_SIZE);
3338 moveDock(dock, x, y);
3339 } else {
3340 /* move vertically if pointer is inside the dock */
3341 if ((dock->on_right_side && ev.xmotion.x_root >= dock->x_pos - ICON_SIZE)
3342 || (!dock->on_right_side && ev.xmotion.x_root <= dock->x_pos + ICON_SIZE * 2)) {
3344 x = ev.xmotion.x_root - ofs_x;
3345 y = ev.xmotion.y_root - ofs_y;
3346 wScreenKeepInside(scr, &x, &y, ICON_SIZE, ICON_SIZE);
3347 moveDock(dock, dock->x_pos, y);
3349 /* move horizontally to change sides */
3350 x = ev.xmotion.x_root - ofs_x;
3351 if (!dock->on_right_side) {
3353 /* is on left */
3354 if (ev.xmotion.x_root > dock->x_pos + ICON_SIZE * 2) {
3355 XMoveWindow(dpy, scr->dock_shadow, scr->scr_width - ICON_SIZE
3356 - DOCK_EXTRA_SPACE - 1, dock->y_pos);
3357 if (superfluous && ghost == None) {
3358 ghost = MakeGhostDock(dock, dock->x_pos,
3359 scr->scr_width - ICON_SIZE
3360 - DOCK_EXTRA_SPACE - 1, dock->y_pos);
3361 XSetWindowBackgroundPixmap(dpy, scr->dock_shadow, ghost);
3362 XClearWindow(dpy, scr->dock_shadow);
3364 XMapRaised(dpy, scr->dock_shadow);
3365 swapped = 1;
3366 } else {
3367 if (superfluous && ghost != None) {
3368 XFreePixmap(dpy, ghost);
3369 ghost = None;
3371 XUnmapWindow(dpy, scr->dock_shadow);
3372 swapped = 0;
3374 } else {
3375 /* is on right */
3376 if (ev.xmotion.x_root < dock->x_pos - ICON_SIZE) {
3377 XMoveWindow(dpy, scr->dock_shadow, DOCK_EXTRA_SPACE, dock->y_pos);
3378 if (superfluous && ghost == None) {
3379 ghost = MakeGhostDock(dock, dock->x_pos,
3380 DOCK_EXTRA_SPACE, dock->y_pos);
3381 XSetWindowBackgroundPixmap(dpy, scr->dock_shadow, ghost);
3382 XClearWindow(dpy, scr->dock_shadow);
3384 XMapRaised(dpy, scr->dock_shadow);
3385 swapped = -1;
3386 } else {
3387 XUnmapWindow(dpy, scr->dock_shadow);
3388 swapped = 0;
3389 if (superfluous && ghost != None) {
3390 XFreePixmap(dpy, ghost);
3391 ghost = None;
3396 break;
3398 case ButtonPress:
3399 break;
3401 case ButtonRelease:
3402 if (ev.xbutton.button != event->xbutton.button)
3403 break;
3404 XUngrabPointer(dpy, CurrentTime);
3405 XUnmapWindow(dpy, scr->dock_shadow);
3406 XResizeWindow(dpy, scr->dock_shadow, ICON_SIZE, ICON_SIZE);
3407 if (dock->type == WM_DOCK) {
3408 if (swapped != 0) {
3409 if (swapped > 0)
3410 dock->on_right_side = 1;
3411 else
3412 dock->on_right_side = 0;
3413 swapDock(dock);
3414 wArrangeIcons(scr, False);
3417 done = 1;
3418 break;
3421 if (superfluous) {
3422 if (ghost != None)
3423 XFreePixmap(dpy, ghost);
3424 XSetWindowBackground(dpy, scr->dock_shadow, scr->white_pixel);
3428 static Bool handleIconMove(WDock *dock, WAppIcon *aicon, XEvent *event)
3430 WScreen *scr = dock->screen_ptr;
3431 Window wins[2];
3432 WIcon *icon = aicon->icon;
3433 WDock *dock2 = NULL, *last_dock = dock, *clip = NULL;
3434 int ondock, grabbed = 0, change_dock = 0, collapsed = 0;
3435 XEvent ev;
3436 int x = aicon->x_pos, y = aicon->y_pos;
3437 int ofs_x = event->xbutton.x, ofs_y = event->xbutton.y;
3438 int shad_x = x, shad_y = y;
3439 int ix = aicon->xindex, iy = aicon->yindex;
3440 int tmp;
3441 Pixmap ghost = None;
3442 Bool docked;
3443 int superfluous = wPreferences.superfluous; /* we catch it to avoid problems */
3444 int omnipresent = aicon->omnipresent; /* this must be cached!!! */
3445 Bool hasMoved = False;
3447 if (wPreferences.flags.noupdates)
3448 return hasMoved;
3450 if (XGrabPointer(dpy, icon->core->window, True, ButtonMotionMask
3451 | ButtonReleaseMask | ButtonPressMask, GrabModeAsync,
3452 GrabModeAsync, None, None, CurrentTime) != GrabSuccess) {
3455 if (!(event->xbutton.state & MOD_MASK))
3456 wRaiseFrame(icon->core);
3458 if (!wPreferences.flags.noclip)
3459 clip = scr->workspaces[scr->current_workspace]->clip;
3461 if (dock == scr->dock && !wPreferences.flags.noclip)
3462 dock2 = clip;
3463 else if (dock != scr->dock && !wPreferences.flags.nodock)
3464 dock2 = scr->dock;
3466 wins[0] = icon->core->window;
3467 wins[1] = scr->dock_shadow;
3468 XRestackWindows(dpy, wins, 2);
3469 XMoveResizeWindow(dpy, scr->dock_shadow, aicon->x_pos, aicon->y_pos, ICON_SIZE, ICON_SIZE);
3470 if (superfluous) {
3471 if (icon->pixmap != None)
3472 ghost = MakeGhostIcon(scr, icon->pixmap);
3473 else
3474 ghost = MakeGhostIcon(scr, icon->core->window);
3476 XSetWindowBackgroundPixmap(dpy, scr->dock_shadow, ghost);
3477 XClearWindow(dpy, scr->dock_shadow);
3479 XMapWindow(dpy, scr->dock_shadow);
3481 ondock = 1;
3483 while (1) {
3484 XMaskEvent(dpy, PointerMotionMask | ButtonReleaseMask | ButtonPressMask
3485 | ButtonMotionMask | ExposureMask, &ev);
3486 switch (ev.type) {
3487 case Expose:
3488 WMHandleEvent(&ev);
3489 break;
3491 case MotionNotify:
3492 hasMoved = True;
3493 if (!grabbed) {
3494 if (abs(ofs_x - ev.xmotion.x) >= MOVE_THRESHOLD
3495 || abs(ofs_y - ev.xmotion.y) >= MOVE_THRESHOLD) {
3496 XChangeActivePointerGrab(dpy, ButtonMotionMask
3497 | ButtonReleaseMask | ButtonPressMask,
3498 wCursor[WCUR_MOVE], CurrentTime);
3499 grabbed = 1;
3500 } else {
3501 break;
3505 if (omnipresent) {
3506 int i;
3507 for (i = 0; i < scr->workspace_count; i++) {
3508 if (i == scr->current_workspace)
3509 continue;
3510 wDockShowIcons(scr->workspaces[i]->clip);
3514 x = ev.xmotion.x_root - ofs_x;
3515 y = ev.xmotion.y_root - ofs_y;
3516 tmp = wDockSnapIcon(dock, aicon, x, y, &ix, &iy, True);
3517 if (tmp && dock2) {
3518 change_dock = 0;
3519 if (last_dock != dock && collapsed) {
3520 last_dock->collapsed = 1;
3521 wDockHideIcons(last_dock);
3522 collapsed = 0;
3524 if (!collapsed && (collapsed = dock->collapsed)) {
3525 dock->collapsed = 0;
3526 wDockShowIcons(dock);
3528 if (dock->auto_raise_lower)
3529 wDockRaise(dock);
3530 last_dock = dock;
3531 } else if (dock2) {
3532 tmp = wDockSnapIcon(dock2, aicon, x, y, &ix, &iy, False);
3533 if (tmp) {
3534 change_dock = 1;
3535 if (last_dock != dock2 && collapsed) {
3536 last_dock->collapsed = 1;
3537 wDockHideIcons(last_dock);
3538 collapsed = 0;
3540 if (!collapsed && (collapsed = dock2->collapsed)) {
3541 dock2->collapsed = 0;
3542 wDockShowIcons(dock2);
3544 if (dock2->auto_raise_lower)
3545 wDockRaise(dock2);
3546 last_dock = dock2;
3549 if (aicon->launching || aicon->lock || (aicon->running && !(ev.xmotion.state & MOD_MASK))
3550 || (!aicon->running && tmp)) {
3551 shad_x = last_dock->x_pos + ix * wPreferences.icon_size;
3552 shad_y = last_dock->y_pos + iy * wPreferences.icon_size;
3554 XMoveWindow(dpy, scr->dock_shadow, shad_x, shad_y);
3556 if (!ondock)
3557 XMapWindow(dpy, scr->dock_shadow);
3559 ondock = 1;
3560 } else {
3561 if (ondock)
3562 XUnmapWindow(dpy, scr->dock_shadow);
3564 ondock = 0;
3566 XMoveWindow(dpy, icon->core->window, x, y);
3567 break;
3569 case ButtonPress:
3570 break;
3572 case ButtonRelease:
3573 if (ev.xbutton.button != event->xbutton.button)
3574 break;
3575 XUngrabPointer(dpy, CurrentTime);
3576 if (ondock) {
3577 SlideWindow(icon->core->window, x, y, shad_x, shad_y);
3578 XUnmapWindow(dpy, scr->dock_shadow);
3579 if (!change_dock) {
3580 reattachIcon(dock, aicon, ix, iy);
3581 if (clip && dock != clip && clip->auto_raise_lower)
3582 wDockLower(clip);
3583 } else {
3584 docked = moveIconBetweenDocks(dock, dock2, aicon, ix, iy);
3585 if (!docked) {
3586 /* Slide it back if dock rejected it */
3587 SlideWindow(icon->core->window, x, y, aicon->x_pos, aicon->y_pos);
3588 reattachIcon(dock, aicon, aicon->xindex, aicon->yindex);
3590 if (last_dock->type == WM_CLIP && last_dock->auto_collapse)
3591 collapsed = 0;
3593 } else {
3594 aicon->x_pos = x;
3595 aicon->y_pos = y;
3596 if (superfluous) {
3597 if (!aicon->running && !wPreferences.no_animations) {
3598 /* We need to deselect it, even if is deselected in
3599 * wDockDetach(), because else DoKaboom() will fail.
3601 if (aicon->icon->selected)
3602 wIconSelect(aicon->icon);
3604 DoKaboom(scr, aicon->icon->core->window, x, y);
3607 if (clip && clip->auto_raise_lower)
3608 wDockLower(clip);
3609 wDockDetach(dock, aicon);
3611 if (collapsed) {
3612 last_dock->collapsed = 1;
3613 wDockHideIcons(last_dock);
3614 collapsed = 0;
3616 if (superfluous) {
3617 if (ghost != None)
3618 XFreePixmap(dpy, ghost);
3619 XSetWindowBackground(dpy, scr->dock_shadow, scr->white_pixel);
3621 if (omnipresent) {
3622 int i;
3623 for (i = 0; i < scr->workspace_count; i++) {
3624 if (i == scr->current_workspace)
3625 continue;
3626 wDockHideIcons(scr->workspaces[i]->clip);
3629 return hasMoved;;
3632 return False; /* never reached */
3635 static int getClipButton(int px, int py)
3637 int pt = (CLIP_BUTTON_SIZE + 2) * ICON_SIZE / 64;
3639 if (px < 0 || py < 0 || px >= ICON_SIZE || py >= ICON_SIZE)
3640 return CLIP_IDLE;
3642 if (py <= pt - ((int)ICON_SIZE - 1 - px))
3643 return CLIP_FORWARD;
3644 else if (px <= pt - ((int)ICON_SIZE - 1 - py))
3645 return CLIP_REWIND;
3647 return CLIP_IDLE;
3650 static void handleClipChangeWorkspace(WScreen *scr, XEvent *event)
3652 XEvent ev;
3653 int done, direction, new_ws;
3654 int new_dir;
3655 WDock *clip = scr->clip_icon->dock;
3657 direction = getClipButton(event->xbutton.x, event->xbutton.y);
3659 clip->lclip_button_pushed = direction == CLIP_REWIND;
3660 clip->rclip_button_pushed = direction == CLIP_FORWARD;
3662 wClipIconPaint(scr->clip_icon);
3663 done = 0;
3664 while (!done) {
3665 WMMaskEvent(dpy, ExposureMask | ButtonMotionMask | ButtonReleaseMask | ButtonPressMask, &ev);
3666 switch (ev.type) {
3667 case Expose:
3668 WMHandleEvent(&ev);
3669 break;
3671 case MotionNotify:
3672 new_dir = getClipButton(ev.xmotion.x, ev.xmotion.y);
3673 if (new_dir != direction) {
3674 direction = new_dir;
3675 clip->lclip_button_pushed = direction == CLIP_REWIND;
3676 clip->rclip_button_pushed = direction == CLIP_FORWARD;
3677 wClipIconPaint(scr->clip_icon);
3679 break;
3681 case ButtonPress:
3682 break;
3684 case ButtonRelease:
3685 if (ev.xbutton.button == event->xbutton.button)
3686 done = 1;
3690 clip->lclip_button_pushed = 0;
3691 clip->rclip_button_pushed = 0;
3693 new_ws = wPreferences.ws_advance || (event->xbutton.state & ControlMask);
3695 if (direction == CLIP_FORWARD) {
3696 if (scr->current_workspace < scr->workspace_count - 1)
3697 wWorkspaceChange(scr, scr->current_workspace + 1);
3698 else if (new_ws && scr->current_workspace < MAX_WORKSPACES - 1)
3699 wWorkspaceChange(scr, scr->current_workspace + 1);
3700 else if (wPreferences.ws_cycle)
3701 wWorkspaceChange(scr, 0);
3702 } else if (direction == CLIP_REWIND) {
3703 if (scr->current_workspace > 0)
3704 wWorkspaceChange(scr, scr->current_workspace - 1);
3705 else if (scr->current_workspace == 0 && wPreferences.ws_cycle)
3706 wWorkspaceChange(scr, scr->workspace_count - 1);
3709 wClipIconPaint(scr->clip_icon);
3712 static void iconMouseDown(WObjDescriptor *desc, XEvent *event)
3714 WAppIcon *aicon = desc->parent;
3715 WDock *dock = aicon->dock;
3716 WScreen *scr = aicon->icon->core->screen_ptr;
3718 if (aicon->editing || WCHECK_STATE(WSTATE_MODAL))
3719 return;
3721 scr->last_dock = dock;
3723 if (dock->menu->flags.mapped)
3724 wMenuUnmap(dock->menu);
3726 if (IsDoubleClick(scr, event)) {
3727 /* double-click was not in the main clip icon */
3728 if (dock->type != WM_CLIP || aicon->xindex != 0 || aicon->yindex != 0
3729 || getClipButton(event->xbutton.x, event->xbutton.y) == CLIP_IDLE) {
3730 iconDblClick(desc, event);
3731 return;
3735 if (event->xbutton.button == Button1) {
3736 if (event->xbutton.state & MOD_MASK)
3737 wDockLower(dock);
3738 else
3739 wDockRaise(dock);
3741 if ((event->xbutton.state & ShiftMask) && aicon != scr->clip_icon && dock->type != WM_DOCK) {
3742 wIconSelect(aicon->icon);
3743 return;
3746 if (aicon->yindex == 0 && aicon->xindex == 0) {
3747 if (getClipButton(event->xbutton.x, event->xbutton.y) != CLIP_IDLE
3748 && dock->type == WM_CLIP)
3749 handleClipChangeWorkspace(scr, event);
3750 else
3751 handleDockMove(dock, aicon, event);
3752 } else {
3753 Bool hasMoved = handleIconMove(dock, aicon, event);
3754 if (wPreferences.single_click && !hasMoved)
3755 iconDblClick(desc, event);
3757 } else if (event->xbutton.button == Button2 && dock->type == WM_CLIP && aicon == scr->clip_icon) {
3758 if (!scr->clip_ws_menu) {
3759 scr->clip_ws_menu = wWorkspaceMenuMake(scr, False);
3761 if (scr->clip_ws_menu) {
3762 WMenu *wsMenu = scr->clip_ws_menu;
3763 int xpos;
3765 wWorkspaceMenuUpdate(scr, wsMenu);
3767 xpos = event->xbutton.x_root - wsMenu->frame->core->width / 2 - 1;
3768 if (xpos < 0) {
3769 xpos = 0;
3770 } else if (xpos + wsMenu->frame->core->width > scr->scr_width - 2) {
3771 xpos = scr->scr_width - wsMenu->frame->core->width - 4;
3773 wMenuMapAt(wsMenu, xpos, event->xbutton.y_root + 2, False);
3775 desc = &wsMenu->menu->descriptor;
3776 event->xany.send_event = True;
3777 (*desc->handle_mousedown) (desc, event);
3779 } else if (event->xbutton.button == Button2 && dock->type == WM_CLIP &&
3780 (event->xbutton.state & ShiftMask) && aicon != scr->clip_icon) {
3781 wClipMakeIconOmnipresent(aicon, !aicon->omnipresent);
3782 } else if (event->xbutton.button == Button3) {
3783 if (event->xbutton.send_event &&
3784 XGrabPointer(dpy, aicon->icon->core->window, True, ButtonMotionMask
3785 | ButtonReleaseMask | ButtonPressMask, GrabModeAsync,
3786 GrabModeAsync, None, None, CurrentTime) != GrabSuccess) {
3787 wwarning("pointer grab failed for dockicon menu");
3788 return;
3791 openDockMenu(dock, aicon, event);
3792 } else if (event->xbutton.button == Button2) {
3793 WAppIcon *btn = desc->parent;
3795 if (!btn->launching && (!btn->running || (event->xbutton.state & ControlMask)))
3796 launchDockedApplication(btn, True);
3800 static void clipEnterNotify(WObjDescriptor *desc, XEvent *event)
3802 WAppIcon *btn = (WAppIcon *) desc->parent;
3803 WDock *dock;
3804 WScreen *scr;
3806 assert(event->type == EnterNotify);
3808 if (desc->parent_type != WCLASS_DOCK_ICON)
3809 return;
3811 scr = btn->icon->core->screen_ptr;
3812 if (!btn->omnipresent)
3813 dock = btn->dock;
3814 else
3815 dock = scr->workspaces[scr->current_workspace]->clip;
3817 if (!dock || dock->type != WM_CLIP)
3818 return;
3820 /* The auto raise/lower code */
3821 if (dock->auto_lower_magic) {
3822 WMDeleteTimerHandler(dock->auto_lower_magic);
3823 dock->auto_lower_magic = NULL;
3825 if (dock->auto_raise_lower && !dock->auto_raise_magic)
3826 dock->auto_raise_magic = WMAddTimerHandler(AUTO_RAISE_DELAY, clipAutoRaise, (void *)dock);
3828 /* The auto expand/collapse code */
3829 if (dock->auto_collapse_magic) {
3830 WMDeleteTimerHandler(dock->auto_collapse_magic);
3831 dock->auto_collapse_magic = NULL;
3833 if (dock->auto_collapse && !dock->auto_expand_magic)
3834 dock->auto_expand_magic = WMAddTimerHandler(AUTO_EXPAND_DELAY, clipAutoExpand, (void *)dock);
3837 static void clipLeave(WDock *dock)
3839 XEvent event;
3840 WObjDescriptor *desc = NULL;
3842 if (!dock || dock->type != WM_CLIP)
3843 return;
3845 if (XCheckTypedEvent(dpy, EnterNotify, &event) != False) {
3846 if (XFindContext(dpy, event.xcrossing.window, wWinContext,
3847 (XPointer *) & desc) != XCNOENT
3848 && desc && desc->parent_type == WCLASS_DOCK_ICON
3849 && ((WAppIcon *) desc->parent)->dock && ((WAppIcon *) desc->parent)->dock->type == WM_CLIP) {
3850 /* We didn't left the Clip yet */
3851 XPutBackEvent(dpy, &event);
3852 return;
3855 XPutBackEvent(dpy, &event);
3856 } else {
3857 /* We entered a withdrawn window, so we're still in Clip */
3858 return;
3861 if (dock->auto_raise_magic) {
3862 WMDeleteTimerHandler(dock->auto_raise_magic);
3863 dock->auto_raise_magic = NULL;
3865 if (dock->auto_raise_lower && !dock->auto_lower_magic)
3866 dock->auto_lower_magic = WMAddTimerHandler(AUTO_LOWER_DELAY, clipAutoLower, (void *)dock);
3868 if (dock->auto_expand_magic) {
3869 WMDeleteTimerHandler(dock->auto_expand_magic);
3870 dock->auto_expand_magic = NULL;
3872 if (dock->auto_collapse && !dock->auto_collapse_magic)
3873 dock->auto_collapse_magic = WMAddTimerHandler(AUTO_COLLAPSE_DELAY, clipAutoCollapse, (void *)dock);
3876 static void clipLeaveNotify(WObjDescriptor *desc, XEvent *event)
3878 WAppIcon *btn = (WAppIcon *) desc->parent;
3880 assert(event->type == LeaveNotify);
3882 if (desc->parent_type != WCLASS_DOCK_ICON)
3883 return;
3885 clipLeave(btn->dock);
3888 static void clipAutoCollapse(void *cdata)
3890 WDock *dock = (WDock *) cdata;
3892 if (dock->type != WM_CLIP)
3893 return;
3895 if (dock->auto_collapse) {
3896 dock->collapsed = 1;
3897 wDockHideIcons(dock);
3899 dock->auto_collapse_magic = NULL;
3902 static void clipAutoExpand(void *cdata)
3904 WDock *dock = (WDock *) cdata;
3906 if (dock->type != WM_CLIP)
3907 return;
3909 if (dock->auto_collapse) {
3910 dock->collapsed = 0;
3911 wDockShowIcons(dock);
3913 dock->auto_expand_magic = NULL;
3916 static void clipAutoLower(void *cdata)
3918 WDock *dock = (WDock *) cdata;
3920 if (dock->type != WM_CLIP)
3921 return;
3923 if (dock->auto_raise_lower)
3924 wDockLower(dock);
3926 dock->auto_lower_magic = NULL;
3929 static void clipAutoRaise(void *cdata)
3931 WDock *dock = (WDock *) cdata;
3933 if (dock->type != WM_CLIP)
3934 return;
3936 if (dock->auto_raise_lower)
3937 wDockRaise(dock);
3939 dock->auto_raise_magic = NULL;
3942 static Bool iconCanBeOmnipresent(WAppIcon *aicon)
3944 WScreen *scr = aicon->icon->core->screen_ptr;
3945 WDock *clip;
3946 WAppIcon *btn;
3947 int i, j;
3949 for (i = 0; i < scr->workspace_count; i++) {
3950 clip = scr->workspaces[i]->clip;
3952 if (clip == aicon->dock)
3953 continue;
3955 if (clip->icon_count + scr->global_icon_count >= clip->max_icons)
3956 return False; /* Clip is full in some workspace */
3958 for (j = 0; j < clip->max_icons; j++) {
3959 btn = clip->icon_array[j];
3960 if (btn && btn->xindex == aicon->xindex && btn->yindex == aicon->yindex)
3961 return False;
3965 return True;
3968 int wClipMakeIconOmnipresent(WAppIcon *aicon, int omnipresent)
3970 WScreen *scr = aicon->icon->core->screen_ptr;
3971 WAppIconChain *new_entry, *tmp, *tmp1;
3972 int status = WO_SUCCESS;
3974 if ((scr->dock && aicon->dock == scr->dock) || aicon == scr->clip_icon)
3975 return WO_NOT_APPLICABLE;
3977 if (aicon->omnipresent == omnipresent)
3978 return WO_SUCCESS;
3980 if (omnipresent) {
3981 if (iconCanBeOmnipresent(aicon)) {
3982 aicon->omnipresent = 1;
3983 new_entry = wmalloc(sizeof(WAppIconChain));
3984 new_entry->aicon = aicon;
3985 new_entry->next = scr->global_icons;
3986 scr->global_icons = new_entry;
3987 scr->global_icon_count++;
3988 } else {
3989 aicon->omnipresent = 0;
3990 status = WO_FAILED;
3992 } else {
3993 aicon->omnipresent = 0;
3994 if (aicon == scr->global_icons->aicon) {
3995 tmp = scr->global_icons->next;
3996 wfree(scr->global_icons);
3997 scr->global_icons = tmp;
3998 scr->global_icon_count--;
3999 } else {
4000 tmp = scr->global_icons;
4001 while (tmp->next) {
4002 if (tmp->next->aicon == aicon) {
4003 tmp1 = tmp->next->next;
4004 wfree(tmp->next);
4005 tmp->next = tmp1;
4006 scr->global_icon_count--;
4007 break;
4009 tmp = tmp->next;
4014 wAppIconPaint(aicon);
4016 return status;