No need to call memset after wmalloc
[wmaker-crm.git] / src / dock.c
blob9d12814a6e03da192ad7fac845b51a5ef629d4f7
1 /* dock.c- built-in Dock module for WindowMaker
3 * Window Maker window manager
5 * Copyright (c) 1997-2003 Alfredo K. Kojima
6 * Copyright (c) 1998-2003 Dan Pascu
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
23 #include "wconfig.h"
25 #include <X11/Xlib.h>
26 #include <X11/Xutil.h>
27 #include <stdlib.h>
28 #include <string.h>
29 #include <strings.h>
30 #include <unistd.h>
31 #include <math.h>
32 #include <limits.h>
34 #ifndef PATH_MAX
35 #define PATH_MAX DEFAULT_PATH_MAX
36 #endif
38 #include "WindowMaker.h"
39 #include "wcore.h"
40 #include "window.h"
41 #include "icon.h"
42 #include "appicon.h"
43 #include "actions.h"
44 #include "stacking.h"
45 #include "dock.h"
46 #include "dialog.h"
47 #include "funcs.h"
48 #include "properties.h"
49 #include "menu.h"
50 #include "client.h"
51 #include "defaults.h"
52 #include "workspace.h"
53 #include "framewin.h"
54 #include "superfluous.h"
55 #include "xinerama.h"
57 /**** Local variables ****/
58 #define CLIP_REWIND 1
59 #define CLIP_IDLE 0
60 #define CLIP_FORWARD 2
62 /**** Global variables ****/
64 /* in dockedapp.c */
65 extern void DestroyDockAppSettingsPanel();
66 extern void ShowDockAppSettingsPanel(WAppIcon * aicon);
67 extern Cursor wCursor[WCUR_LAST];
68 extern WPreferences wPreferences;
69 extern XContext wWinContext;
70 extern void appIconMouseDown(WObjDescriptor *desc, XEvent *event);
72 #define MOD_MASK wPreferences.modifier_mask
73 #define ICON_SIZE wPreferences.icon_size
75 /***** Local variables ****/
77 static WMPropList *dCommand = NULL;
78 static WMPropList *dPasteCommand = NULL;
79 #ifdef XDND /* XXX was OFFIX */
80 static WMPropList *dDropCommand = NULL;
81 #endif
82 static WMPropList *dAutoLaunch, *dLock;
83 static WMPropList *dName, *dForced, *dBuggyApplication, *dYes, *dNo;
84 static WMPropList *dHost, *dDock, *dClip;
85 static WMPropList *dAutoAttractIcons;
87 static WMPropList *dPosition, *dApplications, *dLowered, *dCollapsed;
89 static WMPropList *dAutoCollapse, *dAutoRaiseLower, *dOmnipresent;
91 static void dockIconPaint(WAppIcon *btn);
93 static void iconMouseDown(WObjDescriptor *desc, XEvent *event);
95 static pid_t execCommand(WAppIcon *btn, char *command, WSavedState *state);
97 static void trackDeadProcess(pid_t pid, unsigned char status, WDock *dock);
99 static int getClipButton(int px, int py);
101 static void toggleLowered(WDock *dock);
103 static void toggleCollapsed(WDock *dock);
105 static void clipIconExpose(WObjDescriptor *desc, XEvent *event);
107 static void clipLeave(WDock *dock);
109 static void handleClipChangeWorkspace(WScreen *scr, XEvent *event);
111 static Bool moveIconBetweenDocks(WDock *src, WDock *dest, WAppIcon *icon, int x, int y);
113 static void clipEnterNotify(WObjDescriptor *desc, XEvent *event);
114 static void clipLeaveNotify(WObjDescriptor *desc, XEvent *event);
115 static void clipAutoCollapse(void *cdata);
116 static void clipAutoExpand(void *cdata);
117 static void launchDockedApplication(WAppIcon *btn, Bool withSelection);
119 static void clipAutoLower(void *cdata);
120 static void clipAutoRaise(void *cdata);
121 static void reattachIcon(WDock *dock, WAppIcon *icon, int x, int y);
122 static WAppIcon *mainIconCreate(WScreen *scr, int type);
124 static void make_keys(void)
126 if (dCommand != NULL)
127 return;
129 dCommand = WMRetainPropList(WMCreatePLString("Command"));
130 dPasteCommand = WMRetainPropList(WMCreatePLString("PasteCommand"));
131 #ifdef XDND
132 dDropCommand = WMRetainPropList(WMCreatePLString("DropCommand"));
133 #endif
134 dLock = WMRetainPropList(WMCreatePLString("Lock"));
135 dAutoLaunch = WMRetainPropList(WMCreatePLString("AutoLaunch"));
136 dName = WMRetainPropList(WMCreatePLString("Name"));
137 dForced = WMRetainPropList(WMCreatePLString("Forced"));
138 dBuggyApplication = WMRetainPropList(WMCreatePLString("BuggyApplication"));
139 dYes = WMRetainPropList(WMCreatePLString("Yes"));
140 dNo = WMRetainPropList(WMCreatePLString("No"));
141 dHost = WMRetainPropList(WMCreatePLString("Host"));
143 dPosition = WMCreatePLString("Position");
144 dApplications = WMCreatePLString("Applications");
145 dLowered = WMCreatePLString("Lowered");
146 dCollapsed = WMCreatePLString("Collapsed");
147 dAutoCollapse = WMCreatePLString("AutoCollapse");
148 dAutoRaiseLower = WMCreatePLString("AutoRaiseLower");
149 dAutoAttractIcons = WMCreatePLString("AutoAttractIcons");
151 dOmnipresent = WMCreatePLString("Omnipresent");
153 dDock = WMCreatePLString("Dock");
154 dClip = WMCreatePLString("Clip");
157 static void renameCallback(WMenu *menu, WMenuEntry *entry)
159 WDock *dock = entry->clientdata;
160 char buffer[128];
161 int wspace;
162 char *name;
164 assert(entry->clientdata != NULL);
166 wspace = dock->screen_ptr->current_workspace;
168 name = wstrdup(dock->screen_ptr->workspaces[wspace]->name);
170 snprintf(buffer, sizeof(buffer), _("Type the name for workspace %i:"), wspace + 1);
171 if (wInputDialog(dock->screen_ptr, _("Rename Workspace"), buffer, &name)) {
172 wWorkspaceRename(dock->screen_ptr, wspace, name);
174 if (name) {
175 wfree(name);
179 static void toggleLoweredCallback(WMenu *menu, WMenuEntry *entry)
181 assert(entry->clientdata != NULL);
183 toggleLowered(entry->clientdata);
185 entry->flags.indicator_on = !(((WDock *) entry->clientdata)->lowered);
187 wMenuPaint(menu);
190 static int matchWindow(const void *item, const void *cdata)
192 return (((WFakeGroupLeader *) item)->leader == (Window) cdata);
195 static void killCallback(WMenu *menu, WMenuEntry *entry)
197 WScreen *scr = menu->menu->screen_ptr;
198 WAppIcon *icon;
199 WFakeGroupLeader *fPtr;
200 char *buffer, *shortname, **argv;
201 char *basename(const char *shortname);
202 int argc;
204 if (!WCHECK_STATE(WSTATE_NORMAL))
205 return;
207 assert(entry->clientdata != NULL);
209 icon = (WAppIcon *) entry->clientdata;
211 icon->editing = 1;
213 WCHANGE_STATE(WSTATE_MODAL);
215 /* strip away dir names */
216 shortname = basename(icon->command);
217 /* separate out command options */
218 wtokensplit(shortname, &argv, &argc);
220 buffer = wstrconcat(argv[0],
221 _(" will be forcibly closed.\n"
222 "Any unsaved changes will be lost.\n" "Please confirm."));
224 if (icon->icon && icon->icon->owner) {
225 fPtr = icon->icon->owner->fake_group;
226 } else {
227 /* is this really necessary? can we kill a non-running dock icon? */
228 Window win = icon->main_window;
229 int index;
231 index = WMFindInArray(scr->fakeGroupLeaders, matchWindow, (void *)win);
232 if (index != WANotFound)
233 fPtr = WMGetFromArray(scr->fakeGroupLeaders, index);
234 else
235 fPtr = NULL;
238 if (wPreferences.dont_confirm_kill
239 || wMessageDialog(menu->frame->screen_ptr, _("Kill Application"),
240 buffer, _("Yes"), _("No"), NULL) == WAPRDefault) {
241 if (fPtr != NULL) {
242 WWindow *wwin, *twin;
244 wwin = scr->focused_window;
245 while (wwin) {
246 twin = wwin->prev;
247 if (wwin->fake_group == fPtr) {
248 wClientKill(wwin);
250 wwin = twin;
252 } else if (icon->icon && icon->icon->owner) {
253 wClientKill(icon->icon->owner);
257 wfree(buffer);
258 wtokenfree(argv, argc);
260 icon->editing = 0;
262 WCHANGE_STATE(WSTATE_NORMAL);
265 /* TODO: replace this function with a member of the dock struct */
266 static int numberOfSelectedIcons(WDock *dock)
268 WAppIcon *aicon;
269 int i, n;
271 n = 0;
272 for (i = 1; i < dock->max_icons; i++) {
273 aicon = dock->icon_array[i];
274 if (aicon && aicon->icon->selected) {
275 n++;
279 return n;
282 static WMArray *getSelected(WDock *dock)
284 WMArray *ret = WMCreateArray(8);
285 WAppIcon *btn;
286 int i;
288 for (i = 1; i < dock->max_icons; i++) {
289 btn = dock->icon_array[i];
290 if (btn && btn->icon->selected) {
291 WMAddToArray(ret, btn);
295 return ret;
298 static void paintClipButtons(WAppIcon *clipIcon, Bool lpushed, Bool rpushed)
300 Window win = clipIcon->icon->core->window;
301 WScreen *scr = clipIcon->icon->core->screen_ptr;
302 XPoint p[4];
303 int pt = CLIP_BUTTON_SIZE * ICON_SIZE / 64;
304 int tp = ICON_SIZE - pt;
305 int as = pt - 15; /* 15 = 5+5+5 */
306 GC gc = scr->draw_gc; /* maybe use WMColorGC() instead here? */
307 WMColor *color;
309 color = scr->clip_title_color[CLIP_NORMAL];
311 XSetForeground(dpy, gc, WMColorPixel(color));
313 if (rpushed) {
314 p[0].x = tp + 1;
315 p[0].y = 1;
316 p[1].x = ICON_SIZE - 2;
317 p[1].y = 1;
318 p[2].x = ICON_SIZE - 2;
319 p[2].y = pt - 1;
320 } else if (lpushed) {
321 p[0].x = 1;
322 p[0].y = tp;
323 p[1].x = pt;
324 p[1].y = ICON_SIZE - 2;
325 p[2].x = 1;
326 p[2].y = ICON_SIZE - 2;
328 if (lpushed || rpushed) {
329 XSetForeground(dpy, scr->draw_gc, scr->white_pixel);
330 XFillPolygon(dpy, win, scr->draw_gc, p, 3, Convex, CoordModeOrigin);
331 XSetForeground(dpy, scr->draw_gc, scr->black_pixel);
334 /* top right arrow */
335 p[0].x = p[3].x = ICON_SIZE - 5 - as;
336 p[0].y = p[3].y = 5;
337 p[1].x = ICON_SIZE - 6;
338 p[1].y = 5;
339 p[2].x = ICON_SIZE - 6;
340 p[2].y = 4 + as;
341 if (rpushed) {
342 XFillPolygon(dpy, win, scr->draw_gc, p, 3, Convex, CoordModeOrigin);
343 XDrawLines(dpy, win, scr->draw_gc, p, 4, CoordModeOrigin);
344 } else {
345 XFillPolygon(dpy, win, gc, p, 3, Convex, CoordModeOrigin);
346 XDrawLines(dpy, win, gc, p, 4, CoordModeOrigin);
349 /* bottom left arrow */
350 p[0].x = p[3].x = 5;
351 p[0].y = p[3].y = ICON_SIZE - 5 - as;
352 p[1].x = 5;
353 p[1].y = ICON_SIZE - 6;
354 p[2].x = 4 + as;
355 p[2].y = ICON_SIZE - 6;
356 if (lpushed) {
357 XFillPolygon(dpy, win, scr->draw_gc, p, 3, Convex, CoordModeOrigin);
358 XDrawLines(dpy, win, scr->draw_gc, p, 4, CoordModeOrigin);
359 } else {
360 XFillPolygon(dpy, win, gc, p, 3, Convex, CoordModeOrigin);
361 XDrawLines(dpy, win, gc, p, 4, CoordModeOrigin);
365 RImage *wClipMakeTile(WScreen *scr, RImage *normalTile)
367 RImage *tile = RCloneImage(normalTile);
368 RColor black;
369 RColor dark;
370 RColor light;
371 int pt, tp;
372 int as;
374 pt = CLIP_BUTTON_SIZE * wPreferences.icon_size / 64;
375 tp = wPreferences.icon_size - 1 - pt;
376 as = pt - 15;
378 black.alpha = 255;
379 black.red = black.green = black.blue = 0;
381 dark.alpha = 0;
382 dark.red = dark.green = dark.blue = 60;
384 light.alpha = 0;
385 light.red = light.green = light.blue = 80;
387 /* top right */
388 ROperateLine(tile, RSubtractOperation, tp, 0, wPreferences.icon_size - 2, pt - 1, &dark);
389 RDrawLine(tile, tp - 1, 0, wPreferences.icon_size - 1, pt + 1, &black);
390 ROperateLine(tile, RAddOperation, tp, 2, wPreferences.icon_size - 3, pt, &light);
392 /* arrow bevel */
393 ROperateLine(tile, RSubtractOperation, ICON_SIZE - 7 - as, 4, ICON_SIZE - 5, 4, &dark);
394 ROperateLine(tile, RSubtractOperation, ICON_SIZE - 6 - as, 5, ICON_SIZE - 5, 6 + as, &dark);
395 ROperateLine(tile, RAddOperation, ICON_SIZE - 5, 4, ICON_SIZE - 5, 6 + as, &light);
397 /* bottom left */
398 ROperateLine(tile, RAddOperation, 2, tp + 2, pt - 2, wPreferences.icon_size - 3, &dark);
399 RDrawLine(tile, 0, tp - 1, pt + 1, wPreferences.icon_size - 1, &black);
400 ROperateLine(tile, RSubtractOperation, 0, tp - 2, pt + 1, wPreferences.icon_size - 2, &light);
402 /* arrow bevel */
403 ROperateLine(tile, RSubtractOperation, 4, ICON_SIZE - 7 - as, 4, ICON_SIZE - 5, &dark);
404 ROperateLine(tile, RSubtractOperation, 5, ICON_SIZE - 6 - as, 6 + as, ICON_SIZE - 5, &dark);
405 ROperateLine(tile, RAddOperation, 4, ICON_SIZE - 5, 6 + as, ICON_SIZE - 5, &light);
407 return tile;
410 static void omnipresentCallback(WMenu *menu, WMenuEntry *entry)
412 WAppIcon *clickedIcon = entry->clientdata;
413 WAppIcon *aicon;
414 WDock *dock;
415 WMArray *selectedIcons;
416 WMArrayIterator iter;
417 int failed;
419 assert(entry->clientdata != NULL);
421 dock = clickedIcon->dock;
423 selectedIcons = getSelected(dock);
425 if (!WMGetArrayItemCount(selectedIcons))
426 WMAddToArray(selectedIcons, clickedIcon);
428 failed = 0;
429 WM_ITERATE_ARRAY(selectedIcons, aicon, iter) {
430 if (wClipMakeIconOmnipresent(aicon, !aicon->omnipresent) == WO_FAILED)
431 failed++;
432 else if (aicon->icon->selected)
433 wIconSelect(aicon->icon);
435 WMFreeArray(selectedIcons);
437 if (failed > 1) {
438 wMessageDialog(dock->screen_ptr, _("Warning"),
439 _("Some icons cannot be made omnipresent. "
440 "Please make sure that no other icon is "
441 "docked in the same positions on the other "
442 "workspaces and the Clip is not full in "
443 "some workspace."), _("OK"), NULL, NULL);
444 } else if (failed == 1) {
445 wMessageDialog(dock->screen_ptr, _("Warning"),
446 _("Icon cannot be made omnipresent. "
447 "Please make sure that no other icon is "
448 "docked in the same position on the other "
449 "workspaces and the Clip is not full in "
450 "some workspace."), _("OK"), NULL, NULL);
454 static void removeIconsCallback(WMenu *menu, WMenuEntry *entry)
456 WAppIcon *clickedIcon = (WAppIcon *) entry->clientdata;
457 WDock *dock;
458 WAppIcon *aicon;
459 WMArray *selectedIcons;
460 int keepit;
461 WMArrayIterator it;
463 assert(clickedIcon != NULL);
465 dock = clickedIcon->dock;
467 selectedIcons = getSelected(dock);
469 if (WMGetArrayItemCount(selectedIcons)) {
470 if (wMessageDialog(dock->screen_ptr, _("Workspace Clip"),
471 _("All selected icons will be removed!"),
472 _("OK"), _("Cancel"), NULL) != WAPRDefault) {
473 WMFreeArray(selectedIcons);
474 return;
476 } else {
477 if (clickedIcon->xindex == 0 && clickedIcon->yindex == 0) {
478 WMFreeArray(selectedIcons);
479 return;
481 WMAddToArray(selectedIcons, clickedIcon);
484 WM_ITERATE_ARRAY(selectedIcons, aicon, it) {
485 keepit = aicon->running && wApplicationOf(aicon->main_window);
486 wDockDetach(dock, aicon);
487 if (keepit) {
488 /* XXX: can: aicon->icon == NULL ? */
489 PlaceIcon(dock->screen_ptr, &aicon->x_pos, &aicon->y_pos,
490 wGetHeadForWindow(aicon->icon->owner));
491 XMoveWindow(dpy, aicon->icon->core->window, aicon->x_pos, aicon->y_pos);
492 if (!dock->mapped || dock->collapsed)
493 XMapWindow(dpy, aicon->icon->core->window);
496 WMFreeArray(selectedIcons);
498 if (wPreferences.auto_arrange_icons)
499 wArrangeIcons(dock->screen_ptr, True);
502 static void keepIconsCallback(WMenu *menu, WMenuEntry *entry)
504 WAppIcon *clickedIcon = (WAppIcon *) entry->clientdata;
505 WDock *dock;
506 WAppIcon *aicon;
507 WMArray *selectedIcons;
508 WMArrayIterator it;
510 assert(clickedIcon != NULL);
511 dock = clickedIcon->dock;
513 selectedIcons = getSelected(dock);
515 if (!WMGetArrayItemCount(selectedIcons)
516 && clickedIcon != dock->screen_ptr->clip_icon) {
517 char *command = NULL;
519 if (!clickedIcon->command && !clickedIcon->editing) {
520 clickedIcon->editing = 1;
521 if (wInputDialog(dock->screen_ptr, _("Keep Icon"),
522 _("Type the command used to launch the application"), &command)) {
523 if (command && (command[0] == 0 || (command[0] == '-' && command[1] == 0))) {
524 wfree(command);
525 command = NULL;
527 clickedIcon->command = command;
528 clickedIcon->editing = 0;
529 } else {
530 clickedIcon->editing = 0;
531 if (command)
532 wfree(command);
533 WMFreeArray(selectedIcons);
534 return;
538 WMAddToArray(selectedIcons, clickedIcon);
541 WM_ITERATE_ARRAY(selectedIcons, aicon, it) {
542 if (aicon->icon->selected)
543 wIconSelect(aicon->icon);
544 if (aicon && aicon->attracted && aicon->command) {
545 aicon->attracted = 0;
546 if (aicon->icon->shadowed) {
547 aicon->icon->shadowed = 0;
548 aicon->icon->force_paint = 1;
549 wAppIconPaint(aicon);
552 wAppIconSave(aicon);
554 WMFreeArray(selectedIcons);
557 static void toggleAutoAttractCallback(WMenu *menu, WMenuEntry *entry)
559 WDock *dock = (WDock *) entry->clientdata;
561 assert(entry->clientdata != NULL);
563 dock->attract_icons = !dock->attract_icons;
564 /*if (!dock->attract_icons)
565 dock->keep_attracted = 0; */
567 entry->flags.indicator_on = dock->attract_icons;
569 wMenuPaint(menu);
572 static void selectCallback(WMenu *menu, WMenuEntry *entry)
574 WAppIcon *icon = (WAppIcon *) entry->clientdata;
576 assert(icon != NULL);
578 wIconSelect(icon->icon);
580 wMenuPaint(menu);
583 static void colectIconsCallback(WMenu *menu, WMenuEntry *entry)
585 WAppIcon *clickedIcon = (WAppIcon *) entry->clientdata;
586 WDock *clip;
587 WAppIcon *aicon;
588 int x, y, x_pos, y_pos;
590 assert(entry->clientdata != NULL);
591 clip = clickedIcon->dock;
593 aicon = clip->screen_ptr->app_icon_list;
595 while (aicon) {
596 if (!aicon->docked && wDockFindFreeSlot(clip, &x, &y)) {
597 x_pos = clip->x_pos + x * ICON_SIZE;
598 y_pos = clip->y_pos + y * ICON_SIZE;
599 if (aicon->x_pos != x_pos || aicon->y_pos != y_pos) {
600 #ifdef ANIMATIONS
601 if (wPreferences.no_animations) {
602 XMoveWindow(dpy, aicon->icon->core->window, x_pos, y_pos);
603 } else {
604 SlideWindow(aicon->icon->core->window,
605 aicon->x_pos, aicon->y_pos, x_pos, y_pos);
607 #else
608 XMoveWindow(dpy, aicon->icon->core->window, x_pos, y_pos);
609 #endif /* ANIMATIONS */
611 aicon->attracted = 1;
612 if (!aicon->icon->shadowed) {
613 aicon->icon->shadowed = 1;
614 aicon->icon->force_paint = 1;
615 /* We don't do an wAppIconPaint() here because it's in
616 * wDockAttachIcon(). -Dan
619 wDockAttachIcon(clip, aicon, x, y);
620 if (clip->collapsed || !clip->mapped)
621 XUnmapWindow(dpy, aicon->icon->core->window);
623 aicon = aicon->next;
627 static void selectIconsCallback(WMenu *menu, WMenuEntry *entry)
629 WAppIcon *clickedIcon = (WAppIcon *) entry->clientdata;
630 WDock *dock;
631 WMArray *selectedIcons;
632 WMArrayIterator iter;
633 WAppIcon *btn;
634 int i;
636 assert(clickedIcon != NULL);
637 dock = clickedIcon->dock;
639 selectedIcons = getSelected(dock);
641 if (!WMGetArrayItemCount(selectedIcons)) {
642 for (i = 1; i < dock->max_icons; i++) {
643 btn = dock->icon_array[i];
644 if (btn && !btn->icon->selected) {
645 wIconSelect(btn->icon);
648 } else {
649 WM_ITERATE_ARRAY(selectedIcons, btn, iter) {
650 wIconSelect(btn->icon);
653 WMFreeArray(selectedIcons);
655 wMenuPaint(menu);
658 static void toggleCollapsedCallback(WMenu *menu, WMenuEntry *entry)
660 assert(entry->clientdata != NULL);
662 toggleCollapsed(entry->clientdata);
664 entry->flags.indicator_on = ((WDock *) entry->clientdata)->collapsed;
666 wMenuPaint(menu);
669 static void toggleAutoCollapseCallback(WMenu *menu, WMenuEntry *entry)
671 WDock *dock;
672 assert(entry->clientdata != NULL);
674 dock = (WDock *) entry->clientdata;
676 dock->auto_collapse = !dock->auto_collapse;
678 entry->flags.indicator_on = ((WDock *) entry->clientdata)->auto_collapse;
680 wMenuPaint(menu);
683 static void toggleAutoRaiseLowerCallback(WMenu *menu, WMenuEntry *entry)
685 WDock *dock;
686 assert(entry->clientdata != NULL);
688 dock = (WDock *) entry->clientdata;
690 dock->auto_raise_lower = !dock->auto_raise_lower;
692 entry->flags.indicator_on = ((WDock *) entry->clientdata)->auto_raise_lower;
694 wMenuPaint(menu);
697 static void launchCallback(WMenu *menu, WMenuEntry *entry)
699 WAppIcon *btn = (WAppIcon *) entry->clientdata;
701 launchDockedApplication(btn, False);
704 static void settingsCallback(WMenu *menu, WMenuEntry *entry)
706 WAppIcon *btn = (WAppIcon *) entry->clientdata;
708 if (btn->editing)
709 return;
710 ShowDockAppSettingsPanel(btn);
713 static void hideCallback(WMenu *menu, WMenuEntry *entry)
715 WApplication *wapp;
716 WAppIcon *btn = (WAppIcon *) entry->clientdata;
718 wapp = wApplicationOf(btn->icon->owner->main_window);
720 if (wapp->flags.hidden) {
721 wWorkspaceChange(btn->icon->core->screen_ptr, wapp->last_workspace);
722 wUnhideApplication(wapp, False, False);
723 } else {
724 wHideApplication(wapp);
728 static void unhideHereCallback(WMenu *menu, WMenuEntry *entry)
730 WApplication *wapp;
731 WAppIcon *btn = (WAppIcon *) entry->clientdata;
733 wapp = wApplicationOf(btn->icon->owner->main_window);
735 wUnhideApplication(wapp, False, True);
738 static WAppIcon *mainIconCreate(WScreen *scr, int type)
740 WAppIcon *btn;
741 int x_pos;
743 if (type == WM_CLIP) {
744 if (scr->clip_icon)
745 return scr->clip_icon;
746 btn = wAppIconCreateForDock(scr, NULL, "Logo", "WMClip", TILE_CLIP);
747 btn->icon->core->descriptor.handle_expose = clipIconExpose;
748 btn->icon->core->descriptor.handle_enternotify = clipEnterNotify;
749 btn->icon->core->descriptor.handle_leavenotify = clipLeaveNotify;
750 /*x_pos = scr->scr_width - ICON_SIZE*2 - DOCK_EXTRA_SPACE; */
751 x_pos = 0;
752 } else {
753 btn = wAppIconCreateForDock(scr, NULL, "Logo", "WMDock", TILE_NORMAL);
754 x_pos = scr->scr_width - ICON_SIZE - DOCK_EXTRA_SPACE;
757 btn->xindex = 0;
758 btn->yindex = 0;
760 btn->icon->core->descriptor.handle_mousedown = iconMouseDown;
761 btn->icon->core->descriptor.parent_type = WCLASS_DOCK_ICON;
762 btn->icon->core->descriptor.parent = btn;
763 /*ChangeStackingLevel(btn->icon->core, WMDockLevel); */
764 XMapWindow(dpy, btn->icon->core->window);
765 btn->x_pos = x_pos;
766 btn->y_pos = 0;
767 btn->docked = 1;
768 if (type == WM_CLIP)
769 scr->clip_icon = btn;
771 return btn;
774 static void switchWSCommand(WMenu *menu, WMenuEntry *entry)
776 WAppIcon *btn, *icon = (WAppIcon *) entry->clientdata;
777 WScreen *scr = icon->icon->core->screen_ptr;
778 WDock *src, *dest;
779 WMArray *selectedIcons;
780 int x, y;
782 if (entry->order == scr->current_workspace)
783 return;
784 src = icon->dock;
785 dest = scr->workspaces[entry->order]->clip;
787 selectedIcons = getSelected(src);
789 if (WMGetArrayItemCount(selectedIcons)) {
790 WMArrayIterator iter;
792 WM_ITERATE_ARRAY(selectedIcons, btn, iter) {
793 if (wDockFindFreeSlot(dest, &x, &y)) {
794 moveIconBetweenDocks(src, dest, btn, x, y);
795 XUnmapWindow(dpy, btn->icon->core->window);
798 } else if (icon != scr->clip_icon) {
799 if (wDockFindFreeSlot(dest, &x, &y)) {
800 moveIconBetweenDocks(src, dest, icon, x, y);
801 XUnmapWindow(dpy, icon->icon->core->window);
804 WMFreeArray(selectedIcons);
807 static void launchDockedApplication(WAppIcon *btn, Bool withSelection)
809 WScreen *scr = btn->icon->core->screen_ptr;
811 if (!btn->launching &&
812 ((!withSelection && btn->command != NULL) || (withSelection && btn->paste_command != NULL))) {
813 if (!btn->forced_dock) {
814 btn->relaunching = btn->running;
815 btn->running = 1;
817 if (btn->wm_instance || btn->wm_class) {
818 WWindowAttributes attr;
819 memset(&attr, 0, sizeof(WWindowAttributes));
820 wDefaultFillAttributes(scr, btn->wm_instance, btn->wm_class, &attr, NULL, True);
822 if (!attr.no_appicon && !btn->buggy_app)
823 btn->launching = 1;
824 else
825 btn->running = 0;
827 btn->drop_launch = 0;
828 btn->paste_launch = withSelection;
829 scr->last_dock = btn->dock;
830 btn->pid = execCommand(btn, (withSelection ? btn->paste_command : btn->command), NULL);
831 if (btn->pid > 0) {
832 if (btn->buggy_app) {
833 /* give feedback that the app was launched */
834 btn->launching = 1;
835 dockIconPaint(btn);
836 btn->launching = 0;
837 WMAddTimerHandler(200, (WMCallback *) dockIconPaint, btn);
838 } else {
839 dockIconPaint(btn);
841 } else {
842 wwarning(_("could not launch application %s"), btn->command);
843 btn->launching = 0;
844 if (!btn->relaunching) {
845 btn->running = 0;
851 static void updateWorkspaceMenu(WMenu *menu, WAppIcon *icon)
853 WScreen *scr = menu->frame->screen_ptr;
854 char title[MAX_WORKSPACENAME_WIDTH + 1];
855 int i;
857 if (!menu || !icon)
858 return;
860 for (i = 0; i < scr->workspace_count; i++) {
861 if (i < menu->entry_no) {
862 if (strcmp(menu->entries[i]->text, scr->workspaces[i]->name) != 0) {
863 wfree(menu->entries[i]->text);
864 strcpy(title, scr->workspaces[i]->name);
865 menu->entries[i]->text = wstrdup(title);
866 menu->flags.realized = 0;
868 menu->entries[i]->clientdata = (void *)icon;
869 } else {
870 strcpy(title, scr->workspaces[i]->name);
872 wMenuAddCallback(menu, title, switchWSCommand, (void *)icon);
874 menu->flags.realized = 0;
876 if (i == scr->current_workspace) {
877 wMenuSetEnabled(menu, i, False);
878 } else {
879 wMenuSetEnabled(menu, i, True);
883 if (!menu->flags.realized)
884 wMenuRealize(menu);
887 static WMenu *makeWorkspaceMenu(WScreen *scr)
889 WMenu *menu;
891 menu = wMenuCreate(scr, NULL, False);
892 if (!menu)
893 wwarning(_("could not create workspace submenu for Clip menu"));
895 wMenuAddCallback(menu, "", switchWSCommand, (void *)scr->clip_icon);
897 menu->flags.realized = 0;
898 wMenuRealize(menu);
900 return menu;
903 static void updateClipOptionsMenu(WMenu *menu, WDock *dock)
905 WMenuEntry *entry;
906 int index = 0;
908 if (!menu || !dock)
909 return;
911 /* keep on top */
912 entry = menu->entries[index];
913 entry->flags.indicator_on = !dock->lowered;
914 entry->clientdata = dock;
916 /* collapsed */
917 entry = menu->entries[++index];
918 entry->flags.indicator_on = dock->collapsed;
919 entry->clientdata = dock;
921 /* auto-collapse */
922 entry = menu->entries[++index];
923 entry->flags.indicator_on = dock->auto_collapse;
924 entry->clientdata = dock;
926 /* auto-raise/lower */
927 entry = menu->entries[++index];
928 entry->flags.indicator_on = dock->auto_raise_lower;
929 entry->clientdata = dock;
930 wMenuSetEnabled(menu, index, dock->lowered);
932 /* attract icons */
933 entry = menu->entries[++index];
934 entry->flags.indicator_on = dock->attract_icons;
935 entry->clientdata = dock;
937 menu->flags.realized = 0;
938 wMenuRealize(menu);
941 static WMenu *makeClipOptionsMenu(WScreen *scr)
943 WMenu *menu;
944 WMenuEntry *entry;
946 menu = wMenuCreate(scr, NULL, False);
947 if (!menu) {
948 wwarning(_("could not create options submenu for Clip menu"));
949 return NULL;
952 entry = wMenuAddCallback(menu, _("Keep on Top"), toggleLoweredCallback, NULL);
953 entry->flags.indicator = 1;
954 entry->flags.indicator_on = 1;
955 entry->flags.indicator_type = MI_CHECK;
957 entry = wMenuAddCallback(menu, _("Collapsed"), toggleCollapsedCallback, NULL);
958 entry->flags.indicator = 1;
959 entry->flags.indicator_on = 1;
960 entry->flags.indicator_type = MI_CHECK;
962 entry = wMenuAddCallback(menu, _("Autocollapse"), toggleAutoCollapseCallback, NULL);
963 entry->flags.indicator = 1;
964 entry->flags.indicator_on = 1;
965 entry->flags.indicator_type = MI_CHECK;
967 entry = wMenuAddCallback(menu, _("Autoraise"), toggleAutoRaiseLowerCallback, NULL);
968 entry->flags.indicator = 1;
969 entry->flags.indicator_on = 1;
970 entry->flags.indicator_type = MI_CHECK;
972 entry = wMenuAddCallback(menu, _("Autoattract Icons"), toggleAutoAttractCallback, NULL);
973 entry->flags.indicator = 1;
974 entry->flags.indicator_on = 1;
975 entry->flags.indicator_type = MI_CHECK;
977 menu->flags.realized = 0;
978 wMenuRealize(menu);
980 return menu;
983 static WMenu *dockMenuCreate(WScreen *scr, int type)
985 WMenu *menu;
986 WMenuEntry *entry;
988 if (type == WM_CLIP && scr->clip_menu)
989 return scr->clip_menu;
991 menu = wMenuCreate(scr, NULL, False);
992 if (type != WM_CLIP) {
993 entry = wMenuAddCallback(menu, _("Keep on Top"), toggleLoweredCallback, NULL);
994 entry->flags.indicator = 1;
995 entry->flags.indicator_on = 1;
996 entry->flags.indicator_type = MI_CHECK;
997 } else {
998 entry = wMenuAddCallback(menu, _("Clip Options"), NULL, NULL);
999 scr->clip_options = makeClipOptionsMenu(scr);
1000 if (scr->clip_options)
1001 wMenuEntrySetCascade(menu, entry, scr->clip_options);
1003 entry = wMenuAddCallback(menu, _("Rename Workspace"), renameCallback, NULL);
1004 wfree(entry->text);
1005 entry->text = _("Rename Workspace");
1007 entry = wMenuAddCallback(menu, _("Selected"), selectCallback, NULL);
1008 entry->flags.indicator = 1;
1009 entry->flags.indicator_on = 1;
1010 entry->flags.indicator_type = MI_CHECK;
1012 entry = wMenuAddCallback(menu, _("Select All Icons"), selectIconsCallback, NULL);
1013 wfree(entry->text);
1014 entry->text = _("Select All Icons");
1016 entry = wMenuAddCallback(menu, _("Keep Icon"), keepIconsCallback, NULL);
1017 wfree(entry->text);
1018 entry->text = _("Keep Icon");
1020 entry = wMenuAddCallback(menu, _("Move Icon To"), NULL, NULL);
1021 wfree(entry->text);
1022 entry->text = _("Move Icon To");
1023 scr->clip_submenu = makeWorkspaceMenu(scr);
1024 if (scr->clip_submenu)
1025 wMenuEntrySetCascade(menu, entry, scr->clip_submenu);
1027 entry = wMenuAddCallback(menu, _("Remove Icon"), removeIconsCallback, NULL);
1028 wfree(entry->text);
1029 entry->text = _("Remove Icon");
1031 wMenuAddCallback(menu, _("Attract Icons"), colectIconsCallback, NULL);
1034 wMenuAddCallback(menu, _("Launch"), launchCallback, NULL);
1036 wMenuAddCallback(menu, _("Unhide Here"), unhideHereCallback, NULL);
1038 entry = wMenuAddCallback(menu, _("Hide"), hideCallback, NULL);
1039 wfree(entry->text);
1040 entry->text = _("Hide");
1042 wMenuAddCallback(menu, _("Settings..."), settingsCallback, NULL);
1044 wMenuAddCallback(menu, _("Kill"), killCallback, NULL);
1046 if (type == WM_CLIP)
1047 scr->clip_menu = menu;
1049 return menu;
1052 WDock *wDockCreate(WScreen *scr, int type)
1054 WDock *dock;
1055 WAppIcon *btn;
1057 make_keys();
1059 dock = wmalloc(sizeof(WDock));
1061 dock->max_icons = DOCK_MAX_ICONS;
1063 dock->icon_array = wmalloc(sizeof(WAppIcon *) * dock->max_icons);
1065 btn = mainIconCreate(scr, type);
1067 btn->dock = dock;
1069 dock->x_pos = btn->x_pos;
1070 dock->y_pos = btn->y_pos;
1071 dock->screen_ptr = scr;
1072 dock->type = type;
1073 dock->icon_count = 1;
1074 dock->on_right_side = 1;
1075 dock->collapsed = 0;
1076 dock->auto_collapse = 0;
1077 dock->auto_collapse_magic = NULL;
1078 dock->auto_raise_lower = 0;
1079 dock->auto_lower_magic = NULL;
1080 dock->auto_raise_magic = NULL;
1081 dock->attract_icons = 0;
1082 dock->lowered = 1;
1083 dock->icon_array[0] = btn;
1084 wRaiseFrame(btn->icon->core);
1085 XMoveWindow(dpy, btn->icon->core->window, btn->x_pos, btn->y_pos);
1087 /* create dock menu */
1088 dock->menu = dockMenuCreate(scr, type);
1090 return dock;
1093 void wDockDestroy(WDock *dock)
1095 int i;
1096 WAppIcon *aicon;
1098 for (i = (dock->type == WM_CLIP) ? 1 : 0; i < dock->max_icons; i++) {
1099 aicon = dock->icon_array[i];
1100 if (aicon) {
1101 int keepit = aicon->running && wApplicationOf(aicon->main_window);
1102 wDockDetach(dock, aicon);
1103 if (keepit) {
1104 /* XXX: can: aicon->icon == NULL ? */
1105 PlaceIcon(dock->screen_ptr, &aicon->x_pos, &aicon->y_pos,
1106 wGetHeadForWindow(aicon->icon->owner));
1107 XMoveWindow(dpy, aicon->icon->core->window, aicon->x_pos, aicon->y_pos);
1108 if (!dock->mapped || dock->collapsed)
1109 XMapWindow(dpy, aicon->icon->core->window);
1113 if (wPreferences.auto_arrange_icons)
1114 wArrangeIcons(dock->screen_ptr, True);
1115 wfree(dock->icon_array);
1116 if (dock->menu && dock->type != WM_CLIP)
1117 wMenuDestroy(dock->menu, True);
1118 if (dock->screen_ptr->last_dock == dock)
1119 dock->screen_ptr->last_dock = NULL;
1120 wfree(dock);
1123 void wClipIconPaint(WAppIcon *aicon)
1125 WScreen *scr = aicon->icon->core->screen_ptr;
1126 WWorkspace *workspace = scr->workspaces[scr->current_workspace];
1127 WMColor *color;
1128 Window win = aicon->icon->core->window;
1129 int length, nlength;
1130 char *ws_name, ws_number[10];
1131 int ty, tx;
1133 wIconPaint(aicon->icon);
1135 length = strlen(workspace->name);
1136 ws_name = wmalloc(length + 1);
1137 snprintf(ws_name, length + 1, "%s", workspace->name);
1138 snprintf(ws_number, sizeof(ws_number), "%i", scr->current_workspace + 1);
1139 nlength = strlen(ws_number);
1141 if (!workspace->clip->collapsed)
1142 color = scr->clip_title_color[CLIP_NORMAL];
1143 else
1144 color = scr->clip_title_color[CLIP_COLLAPSED];
1146 ty = ICON_SIZE - WMFontHeight(scr->clip_title_font) - 3;
1148 tx = CLIP_BUTTON_SIZE * ICON_SIZE / 64;
1150 if(wPreferences.show_clip_title)
1151 WMDrawString(scr->wmscreen, win, color, scr->clip_title_font, tx, ty, ws_name, length);
1153 tx = (ICON_SIZE / 2 - WMWidthOfString(scr->clip_title_font, ws_number, nlength)) / 2;
1155 WMDrawString(scr->wmscreen, win, color, scr->clip_title_font, tx, 2, ws_number, nlength);
1157 wfree(ws_name);
1159 if (aicon->launching) {
1160 XFillRectangle(dpy, aicon->icon->core->window, scr->stipple_gc,
1161 0, 0, wPreferences.icon_size, wPreferences.icon_size);
1163 paintClipButtons(aicon, aicon->dock->lclip_button_pushed, aicon->dock->rclip_button_pushed);
1166 static void clipIconExpose(WObjDescriptor *desc, XEvent *event)
1168 wClipIconPaint(desc->parent);
1171 static void dockIconPaint(WAppIcon *btn)
1173 if (btn == btn->icon->core->screen_ptr->clip_icon)
1174 wClipIconPaint(btn);
1175 else {
1176 wAppIconPaint(btn);
1177 wAppIconSave(btn);
1181 static WMPropList *make_icon_state(WAppIcon *btn)
1183 WMPropList *node = NULL;
1184 WMPropList *command, *autolaunch, *lock, *name, *forced;
1185 WMPropList *position, *buggy, *omnipresent;
1186 char *tmp;
1187 char buffer[64];
1189 if (btn) {
1190 if (!btn->command)
1191 command = WMCreatePLString("-");
1192 else
1193 command = WMCreatePLString(btn->command);
1195 autolaunch = btn->auto_launch ? dYes : dNo;
1197 lock = btn->lock ? dYes : dNo;
1199 tmp = EscapeWM_CLASS(btn->wm_instance, btn->wm_class);
1201 name = WMCreatePLString(tmp);
1203 wfree(tmp);
1205 forced = btn->forced_dock ? dYes : dNo;
1207 buggy = btn->buggy_app ? dYes : dNo;
1209 if (btn == btn->icon->core->screen_ptr->clip_icon)
1210 snprintf(buffer, sizeof(buffer), "%i,%i", btn->x_pos, btn->y_pos);
1211 else
1212 snprintf(buffer, sizeof(buffer), "%hi,%hi", btn->xindex, btn->yindex);
1213 position = WMCreatePLString(buffer);
1215 node = WMCreatePLDictionary(dCommand, command,
1216 dName, name,
1217 dAutoLaunch, autolaunch,
1218 dLock, lock,
1219 dForced, forced, dBuggyApplication, buggy, dPosition, position, NULL);
1220 WMReleasePropList(command);
1221 WMReleasePropList(name);
1222 WMReleasePropList(position);
1224 omnipresent = btn->omnipresent ? dYes : dNo;
1225 if (btn->dock != btn->icon->core->screen_ptr->dock && (btn->xindex != 0 || btn->yindex != 0))
1226 WMPutInPLDictionary(node, dOmnipresent, omnipresent);
1228 #ifdef XDND /* was OFFIX */
1229 if (btn->dnd_command) {
1230 command = WMCreatePLString(btn->dnd_command);
1231 WMPutInPLDictionary(node, dDropCommand, command);
1232 WMReleasePropList(command);
1234 #endif /* XDND */
1236 if (btn->paste_command) {
1237 command = WMCreatePLString(btn->paste_command);
1238 WMPutInPLDictionary(node, dPasteCommand, command);
1239 WMReleasePropList(command);
1243 return node;
1246 static WMPropList *dockSaveState(WDock *dock)
1248 int i;
1249 WMPropList *icon_info;
1250 WMPropList *list = NULL, *dock_state = NULL;
1251 WMPropList *value, *key;
1252 char buffer[256];
1254 list = WMCreatePLArray(NULL);
1256 for (i = (dock->type == WM_DOCK ? 0 : 1); i < dock->max_icons; i++) {
1257 WAppIcon *btn = dock->icon_array[i];
1259 if (!btn || btn->attracted)
1260 continue;
1262 if ((icon_info = make_icon_state(dock->icon_array[i]))) {
1263 WMAddToPLArray(list, icon_info);
1264 WMReleasePropList(icon_info);
1268 dock_state = WMCreatePLDictionary(dApplications, list, NULL);
1270 if (dock->type == WM_DOCK) {
1271 snprintf(buffer, sizeof(buffer), "Applications%i", dock->screen_ptr->scr_height);
1272 key = WMCreatePLString(buffer);
1273 WMPutInPLDictionary(dock_state, key, list);
1274 WMReleasePropList(key);
1276 snprintf(buffer, sizeof(buffer), "%i,%i", (dock->on_right_side ? -ICON_SIZE : 0), dock->y_pos);
1277 value = WMCreatePLString(buffer);
1278 WMPutInPLDictionary(dock_state, dPosition, value);
1279 WMReleasePropList(value);
1281 WMReleasePropList(list);
1283 value = (dock->lowered ? dYes : dNo);
1284 WMPutInPLDictionary(dock_state, dLowered, value);
1286 if (dock->type == WM_CLIP) {
1287 value = (dock->collapsed ? dYes : dNo);
1288 WMPutInPLDictionary(dock_state, dCollapsed, value);
1290 value = (dock->auto_collapse ? dYes : dNo);
1291 WMPutInPLDictionary(dock_state, dAutoCollapse, value);
1293 value = (dock->auto_raise_lower ? dYes : dNo);
1294 WMPutInPLDictionary(dock_state, dAutoRaiseLower, value);
1296 value = (dock->attract_icons ? dYes : dNo);
1297 WMPutInPLDictionary(dock_state, dAutoAttractIcons, value);
1300 return dock_state;
1303 void wDockSaveState(WScreen *scr, WMPropList *old_state)
1305 WMPropList *dock_state;
1306 WMPropList *keys;
1308 dock_state = dockSaveState(scr->dock);
1311 * Copy saved states of docks with different sizes.
1313 if (old_state) {
1314 int i;
1315 WMPropList *tmp;
1317 keys = WMGetPLDictionaryKeys(old_state);
1318 for (i = 0; i < WMGetPropListItemCount(keys); i++) {
1319 tmp = WMGetFromPLArray(keys, i);
1321 if (strncasecmp(WMGetFromPLString(tmp), "applications", 12) == 0
1322 && !WMGetFromPLDictionary(dock_state, tmp)) {
1324 WMPutInPLDictionary(dock_state, tmp, WMGetFromPLDictionary(old_state, tmp));
1327 WMReleasePropList(keys);
1330 WMPutInPLDictionary(scr->session_state, dDock, dock_state);
1332 WMReleasePropList(dock_state);
1335 void wClipSaveState(WScreen *scr)
1337 WMPropList *clip_state;
1339 clip_state = make_icon_state(scr->clip_icon);
1341 WMPutInPLDictionary(scr->session_state, dClip, clip_state);
1343 WMReleasePropList(clip_state);
1346 WMPropList *wClipSaveWorkspaceState(WScreen *scr, int workspace)
1348 return dockSaveState(scr->workspaces[workspace]->clip);
1351 static Bool getBooleanDockValue(WMPropList *value, WMPropList *key)
1353 if (value) {
1354 if (WMIsPLString(value)) {
1355 if (strcasecmp(WMGetFromPLString(value), "YES") == 0)
1356 return True;
1357 } else {
1358 wwarning(_("bad value in docked icon state info %s"), WMGetFromPLString(key));
1361 return False;
1364 static WAppIcon *restore_icon_state(WScreen *scr, WMPropList *info, int type, int index)
1366 WAppIcon *aicon;
1367 WMPropList *cmd, *value;
1369 cmd = WMGetFromPLDictionary(info, dCommand);
1370 if (!cmd || !WMIsPLString(cmd)) {
1371 return NULL;
1374 /* parse window name */
1375 value = WMGetFromPLDictionary(info, dName);
1376 if (!value)
1377 return NULL;
1380 char *wclass, *winstance;
1381 char *command;
1383 ParseWindowName(value, &winstance, &wclass, "dock");
1385 if (!winstance && !wclass) {
1386 return NULL;
1389 /* get commands */
1391 if (cmd)
1392 command = wstrdup(WMGetFromPLString(cmd));
1393 else
1394 command = NULL;
1396 if (!command || strcmp(command, "-") == 0) {
1397 if (command)
1398 wfree(command);
1399 if (wclass)
1400 wfree(wclass);
1401 if (winstance)
1402 wfree(winstance);
1404 return NULL;
1407 aicon = wAppIconCreateForDock(scr, command, winstance, wclass, TILE_NORMAL);
1408 if (wclass)
1409 wfree(wclass);
1410 if (winstance)
1411 wfree(winstance);
1412 if (command)
1413 wfree(command);
1416 aicon->icon->core->descriptor.handle_mousedown = iconMouseDown;
1417 if (type == WM_CLIP) {
1418 aicon->icon->core->descriptor.handle_enternotify = clipEnterNotify;
1419 aicon->icon->core->descriptor.handle_leavenotify = clipLeaveNotify;
1421 aicon->icon->core->descriptor.parent_type = WCLASS_DOCK_ICON;
1422 aicon->icon->core->descriptor.parent = aicon;
1424 #ifdef XDND /* was OFFIX */
1425 cmd = WMGetFromPLDictionary(info, dDropCommand);
1426 if (cmd)
1427 aicon->dnd_command = wstrdup(WMGetFromPLString(cmd));
1428 #endif
1430 cmd = WMGetFromPLDictionary(info, dPasteCommand);
1431 if (cmd)
1432 aicon->paste_command = wstrdup(WMGetFromPLString(cmd));
1434 /* check auto launch */
1435 value = WMGetFromPLDictionary(info, dAutoLaunch);
1437 aicon->auto_launch = getBooleanDockValue(value, dAutoLaunch);
1439 /* check lock */
1440 value = WMGetFromPLDictionary(info, dLock);
1442 aicon->lock = getBooleanDockValue(value, dLock);
1444 /* check if it wasn't normally docked */
1445 value = WMGetFromPLDictionary(info, dForced);
1447 aicon->forced_dock = getBooleanDockValue(value, dForced);
1449 /* check if we can rely on the stuff in the app */
1450 value = WMGetFromPLDictionary(info, dBuggyApplication);
1452 aicon->buggy_app = getBooleanDockValue(value, dBuggyApplication);
1454 /* get position in the dock */
1455 value = WMGetFromPLDictionary(info, dPosition);
1456 if (value && WMIsPLString(value)) {
1457 if (sscanf(WMGetFromPLString(value), "%hi,%hi", &aicon->xindex, &aicon->yindex) != 2)
1458 wwarning(_("bad value in docked icon state info %s"), WMGetFromPLString(dPosition));
1460 /* check position sanity */
1461 /* incomplete section! */
1462 if (type == WM_DOCK) {
1463 aicon->xindex = 0;
1464 if (aicon->yindex < 0)
1465 wwarning(_("bad value in docked icon position %i,%i"),
1466 aicon->xindex, aicon->yindex);
1468 } else {
1469 aicon->yindex = index;
1470 aicon->xindex = 0;
1473 /* check if icon is omnipresent */
1474 value = WMGetFromPLDictionary(info, dOmnipresent);
1476 aicon->omnipresent = getBooleanDockValue(value, dOmnipresent);
1478 aicon->running = 0;
1479 aicon->docked = 1;
1481 return aicon;
1484 #define COMPLAIN(key) wwarning(_("bad value in dock state info:%s"), key)
1486 WAppIcon *wClipRestoreState(WScreen *scr, WMPropList *clip_state)
1488 WAppIcon *icon;
1489 WMPropList *value;
1491 icon = mainIconCreate(scr, WM_CLIP);
1493 if (!clip_state)
1494 return icon;
1496 WMRetainPropList(clip_state);
1498 /* restore position */
1500 value = WMGetFromPLDictionary(clip_state, dPosition);
1502 if (value) {
1503 if (!WMIsPLString(value))
1504 COMPLAIN("Position");
1505 else {
1506 WMRect rect;
1507 int flags;
1509 if (sscanf(WMGetFromPLString(value), "%i,%i", &icon->x_pos, &icon->y_pos) != 2)
1510 COMPLAIN("Position");
1512 /* check position sanity */
1513 rect.pos.x = icon->x_pos;
1514 rect.pos.y = icon->y_pos;
1515 rect.size.width = rect.size.height = ICON_SIZE;
1517 wGetRectPlacementInfo(scr, rect, &flags);
1518 if (flags & (XFLAG_DEAD | XFLAG_PARTIAL))
1519 wScreenKeepInside(scr, &icon->x_pos, &icon->y_pos, ICON_SIZE, ICON_SIZE);
1522 #ifdef XDND /* was OFFIX */
1523 value = WMGetFromPLDictionary(clip_state, dDropCommand);
1524 if (value && WMIsPLString(value))
1525 icon->dnd_command = wstrdup(WMGetFromPLString(value));
1526 #endif
1528 value = WMGetFromPLDictionary(clip_state, dPasteCommand);
1529 if (value && WMIsPLString(value))
1530 icon->paste_command = wstrdup(WMGetFromPLString(value));
1532 WMReleasePropList(clip_state);
1534 return icon;
1537 WDock *wDockRestoreState(WScreen *scr, WMPropList *dock_state, int type)
1539 WDock *dock;
1540 WMPropList *apps;
1541 WMPropList *value;
1542 WAppIcon *aicon, *old_top;
1543 int count, i;
1545 dock = wDockCreate(scr, type);
1547 if (!dock_state)
1548 return dock;
1550 WMRetainPropList(dock_state);
1552 /* restore position */
1553 value = WMGetFromPLDictionary(dock_state, dPosition);
1554 if (value) {
1555 if (!WMIsPLString(value)) {
1556 COMPLAIN("Position");
1557 } else {
1558 WMRect rect;
1559 int flags;
1561 if (sscanf(WMGetFromPLString(value), "%i,%i", &dock->x_pos, &dock->y_pos) != 2)
1562 COMPLAIN("Position");
1564 /* check position sanity */
1565 rect.pos.x = dock->x_pos;
1566 rect.pos.y = dock->y_pos;
1567 rect.size.width = rect.size.height = ICON_SIZE;
1569 wGetRectPlacementInfo(scr, rect, &flags);
1570 if (flags & (XFLAG_DEAD | XFLAG_PARTIAL)) {
1571 int x = dock->x_pos;
1572 wScreenKeepInside(scr, &x, &dock->y_pos, ICON_SIZE, ICON_SIZE);
1575 /* Is this needed any more? */
1576 if (type == WM_CLIP) {
1577 if (dock->x_pos < 0) {
1578 dock->x_pos = 0;
1579 } else if (dock->x_pos > scr->scr_width - ICON_SIZE) {
1580 dock->x_pos = scr->scr_width - ICON_SIZE;
1582 } else {
1583 if (dock->x_pos >= 0) {
1584 dock->x_pos = DOCK_EXTRA_SPACE;
1585 dock->on_right_side = 0;
1586 } else {
1587 dock->x_pos = scr->scr_width - DOCK_EXTRA_SPACE - ICON_SIZE;
1588 dock->on_right_side = 1;
1594 /* restore lowered/raised state */
1595 dock->lowered = 0;
1597 value = WMGetFromPLDictionary(dock_state, dLowered);
1598 if (value) {
1599 if (!WMIsPLString(value)) {
1600 COMPLAIN("Lowered");
1601 } else {
1602 if (strcasecmp(WMGetFromPLString(value), "YES") == 0) {
1603 dock->lowered = 1;
1608 /* restore collapsed state */
1609 dock->collapsed = 0;
1611 value = WMGetFromPLDictionary(dock_state, dCollapsed);
1612 if (value) {
1613 if (!WMIsPLString(value)) {
1614 COMPLAIN("Collapsed");
1615 } else {
1616 if (strcasecmp(WMGetFromPLString(value), "YES") == 0) {
1617 dock->collapsed = 1;
1622 /* restore auto-collapsed state */
1623 value = WMGetFromPLDictionary(dock_state, dAutoCollapse);
1624 if (value) {
1625 if (!WMIsPLString(value)) {
1626 COMPLAIN("AutoCollapse");
1627 } else {
1628 if (strcasecmp(WMGetFromPLString(value), "YES") == 0) {
1629 dock->auto_collapse = 1;
1630 dock->collapsed = 1;
1635 /* restore auto-raise/lower state */
1636 value = WMGetFromPLDictionary(dock_state, dAutoRaiseLower);
1637 if (value) {
1638 if (!WMIsPLString(value)) {
1639 COMPLAIN("AutoRaiseLower");
1640 } else {
1641 if (strcasecmp(WMGetFromPLString(value), "YES") == 0)
1642 dock->auto_raise_lower = 1;
1646 /* restore attract icons state */
1647 dock->attract_icons = 0;
1649 value = WMGetFromPLDictionary(dock_state, dAutoAttractIcons);
1650 if (value) {
1651 if (!WMIsPLString(value)) {
1652 COMPLAIN("AutoAttractIcons");
1653 } else {
1654 if (strcasecmp(WMGetFromPLString(value), "YES") == 0)
1655 dock->attract_icons = 1;
1659 /* application list */
1662 WMPropList *tmp;
1663 char buffer[64];
1666 * When saving, it saves the dock state in
1667 * Applications and Applicationsnnn
1669 * When loading, it will first try Applicationsnnn.
1670 * If it does not exist, use Applications as default.
1673 snprintf(buffer, sizeof(buffer), "Applications%i", scr->scr_height);
1675 tmp = WMCreatePLString(buffer);
1676 apps = WMGetFromPLDictionary(dock_state, tmp);
1677 WMReleasePropList(tmp);
1679 if (!apps)
1680 apps = WMGetFromPLDictionary(dock_state, dApplications);
1683 if (!apps)
1684 goto finish;
1686 count = WMGetPropListItemCount(apps);
1687 if (count == 0)
1688 goto finish;
1690 old_top = dock->icon_array[0];
1692 /* dock->icon_count is set to 1 when dock is created.
1693 * Since Clip is already restored, we want to keep it so for clip,
1694 * but for dock we may change the default top tile, so we set it to 0.
1696 if (type == WM_DOCK)
1697 dock->icon_count = 0;
1699 for (i = 0; i < count; i++) {
1700 if (dock->icon_count >= dock->max_icons) {
1701 wwarning(_("there are too many icons stored in dock. Ignoring what doesn't fit"));
1702 break;
1705 value = WMGetFromPLArray(apps, i);
1706 aicon = restore_icon_state(scr, value, type, dock->icon_count);
1708 dock->icon_array[dock->icon_count] = aicon;
1710 if (aicon) {
1711 aicon->dock = dock;
1712 aicon->x_pos = dock->x_pos + (aicon->xindex * ICON_SIZE);
1713 aicon->y_pos = dock->y_pos + (aicon->yindex * ICON_SIZE);
1715 if (dock->lowered)
1716 ChangeStackingLevel(aicon->icon->core, WMNormalLevel);
1717 else
1718 ChangeStackingLevel(aicon->icon->core, WMDockLevel);
1720 wCoreConfigure(aicon->icon->core, aicon->x_pos, aicon->y_pos, 0, 0);
1722 if (!dock->collapsed)
1723 XMapWindow(dpy, aicon->icon->core->window);
1724 wRaiseFrame(aicon->icon->core);
1726 dock->icon_count++;
1727 } else if (dock->icon_count == 0 && type == WM_DOCK)
1728 dock->icon_count++;
1731 /* if the first icon is not defined, use the default */
1732 if (dock->icon_array[0] == NULL) {
1733 /* update default icon */
1734 old_top->x_pos = dock->x_pos;
1735 old_top->y_pos = dock->y_pos;
1736 if (dock->lowered)
1737 ChangeStackingLevel(old_top->icon->core, WMNormalLevel);
1738 else
1739 ChangeStackingLevel(old_top->icon->core, WMDockLevel);
1740 dock->icon_array[0] = old_top;
1741 XMoveWindow(dpy, old_top->icon->core->window, dock->x_pos, dock->y_pos);
1742 /* we don't need to increment dock->icon_count here because it was
1743 * incremented in the loop above.
1745 } else if (old_top != dock->icon_array[0]) {
1746 if (old_top == scr->clip_icon)
1747 scr->clip_icon = dock->icon_array[0];
1748 wAppIconDestroy(old_top);
1751 finish:
1752 WMReleasePropList(dock_state);
1754 return dock;
1757 void wDockLaunchWithState(WDock *dock, WAppIcon *btn, WSavedState *state)
1759 if (btn && btn->command && !btn->running && !btn->launching) {
1761 btn->drop_launch = 0;
1762 btn->paste_launch = 0;
1764 btn->pid = execCommand(btn, btn->command, state);
1766 if (btn->pid > 0) {
1767 if (!btn->forced_dock && !btn->buggy_app) {
1768 btn->launching = 1;
1769 dockIconPaint(btn);
1772 } else {
1773 wfree(state);
1777 void wDockDoAutoLaunch(WDock *dock, int workspace)
1779 WAppIcon *btn;
1780 WSavedState *state;
1781 int i;
1783 for (i = 0; i < dock->max_icons; i++) {
1784 btn = dock->icon_array[i];
1785 if (!btn || !btn->auto_launch)
1786 continue;
1788 state = wmalloc(sizeof(WSavedState));
1789 state->workspace = workspace;
1790 /* TODO: this is klugy and is very difficult to understand
1791 * what's going on. Try to clean up */
1792 wDockLaunchWithState(dock, btn, state);
1796 #ifdef XDND /* was OFFIX */
1797 static WDock *findDock(WScreen *scr, XEvent *event, int *icon_pos)
1799 WDock *dock;
1800 int i;
1802 *icon_pos = -1;
1803 if ((dock = scr->dock) != NULL) {
1804 for (i = 0; i < dock->max_icons; i++) {
1805 if (dock->icon_array[i]
1806 && dock->icon_array[i]->icon->core->window == event->xclient.window) {
1807 *icon_pos = i;
1808 break;
1812 if (*icon_pos < 0 && (dock = scr->workspaces[scr->current_workspace]->clip) != NULL) {
1813 for (i = 0; i < dock->max_icons; i++) {
1814 if (dock->icon_array[i]
1815 && dock->icon_array[i]->icon->core->window == event->xclient.window) {
1816 *icon_pos = i;
1817 break;
1821 if (*icon_pos >= 0)
1822 return dock;
1823 return NULL;
1826 int wDockReceiveDNDDrop(WScreen *scr, XEvent *event)
1828 WDock *dock;
1829 WAppIcon *btn;
1830 int icon_pos;
1832 dock = findDock(scr, event, &icon_pos);
1833 if (!dock)
1834 return False;
1837 * Return True if the drop was on an application icon window.
1838 * In this case, let the ClientMessage handler redirect the
1839 * message to the app.
1841 if (dock->icon_array[icon_pos]->icon->icon_win != None)
1842 return True;
1844 if (dock->icon_array[icon_pos]->dnd_command != NULL) {
1845 scr->flags.dnd_data_convertion_status = 0;
1847 btn = dock->icon_array[icon_pos];
1849 if (!btn->forced_dock) {
1850 btn->relaunching = btn->running;
1851 btn->running = 1;
1853 if (btn->wm_instance || btn->wm_class) {
1854 WWindowAttributes attr;
1855 memset(&attr, 0, sizeof(WWindowAttributes));
1856 wDefaultFillAttributes(btn->icon->core->screen_ptr,
1857 btn->wm_instance, btn->wm_class, &attr, NULL, True);
1859 if (!attr.no_appicon)
1860 btn->launching = 1;
1861 else
1862 btn->running = 0;
1865 btn->paste_launch = 0;
1866 btn->drop_launch = 1;
1867 scr->last_dock = dock;
1868 btn->pid = execCommand(btn, btn->dnd_command, NULL);
1869 if (btn->pid > 0) {
1870 dockIconPaint(btn);
1871 } else {
1872 btn->launching = 0;
1873 if (!btn->relaunching) {
1874 btn->running = 0;
1878 return False;
1880 #endif /* XDND */
1882 Bool wDockAttachIcon(WDock *dock, WAppIcon *icon, int x, int y)
1884 WWindow *wwin;
1885 int index;
1887 wwin = icon->icon->owner;
1888 if (icon->command == NULL) {
1889 char *command;
1891 icon->editing = 0;
1893 command = GetCommandForWindow(wwin->client_win);
1894 if (command) {
1895 icon->command = command;
1896 } else {
1897 /* icon->forced_dock = 1; */
1898 if (dock->type != WM_CLIP || !icon->attracted) {
1899 icon->editing = 1;
1900 if (wInputDialog(dock->screen_ptr, _("Dock Icon"),
1901 _("Type the command used to launch the application"), &command)) {
1902 if (command && (command[0] == 0 || (command[0] == '-' && command[1] == 0))) {
1903 wfree(command);
1904 command = NULL;
1906 icon->command = command;
1907 icon->editing = 0;
1908 } else {
1909 icon->editing = 0;
1910 if (command)
1911 wfree(command);
1912 /* If the target is the dock, reject the icon. If
1913 * the target is the clip, make it an attracted icon
1915 if (dock->type == WM_CLIP) {
1916 icon->attracted = 1;
1917 if (!icon->icon->shadowed) {
1918 icon->icon->shadowed = 1;
1919 icon->icon->force_paint = 1;
1921 } else {
1922 return False;
1927 } else {
1928 icon->editing = 0;
1931 for (index = 1; index < dock->max_icons; index++)
1932 if (dock->icon_array[index] == NULL)
1933 break;
1934 /* if (index == dock->max_icons)
1935 return; */
1937 assert(index < dock->max_icons);
1939 dock->icon_array[index] = icon;
1940 icon->yindex = y;
1941 icon->xindex = x;
1943 icon->omnipresent = 0;
1945 icon->x_pos = dock->x_pos + x * ICON_SIZE;
1946 icon->y_pos = dock->y_pos + y * ICON_SIZE;
1948 dock->icon_count++;
1950 icon->running = 1;
1951 icon->launching = 0;
1952 icon->docked = 1;
1953 icon->dock = dock;
1954 icon->icon->core->descriptor.handle_mousedown = iconMouseDown;
1955 if (dock->type == WM_CLIP) {
1956 icon->icon->core->descriptor.handle_enternotify = clipEnterNotify;
1957 icon->icon->core->descriptor.handle_leavenotify = clipLeaveNotify;
1959 icon->icon->core->descriptor.parent_type = WCLASS_DOCK_ICON;
1960 icon->icon->core->descriptor.parent = icon;
1962 MoveInStackListUnder(dock->icon_array[index - 1]->icon->core, icon->icon->core);
1963 wAppIconMove(icon, icon->x_pos, icon->y_pos);
1964 wAppIconPaint(icon);
1965 wAppIconSave(icon);
1967 if (wPreferences.auto_arrange_icons)
1968 wArrangeIcons(dock->screen_ptr, True);
1970 #ifdef XDND /* was OFFIX */
1971 if (icon->command && !icon->dnd_command) {
1972 int len = strlen(icon->command) + 8;
1973 icon->dnd_command = wmalloc(len);
1974 snprintf(icon->dnd_command, len, "%s %%d", icon->command);
1976 #endif
1978 if (icon->command && !icon->paste_command) {
1979 int len = strlen(icon->command) + 8;
1980 icon->paste_command = wmalloc(len);
1981 snprintf(icon->paste_command, len, "%s %%s", icon->command);
1984 return True;
1987 static void reattachIcon(WDock *dock, WAppIcon *icon, int x, int y)
1989 int index;
1991 for (index = 1; index < dock->max_icons; index++) {
1992 if (dock->icon_array[index] == icon)
1993 break;
1995 assert(index < dock->max_icons);
1997 icon->yindex = y;
1998 icon->xindex = x;
2000 icon->x_pos = dock->x_pos + x * ICON_SIZE;
2001 icon->y_pos = dock->y_pos + y * ICON_SIZE;
2004 static Bool moveIconBetweenDocks(WDock *src, WDock *dest, WAppIcon *icon, int x, int y)
2006 WWindow *wwin;
2007 char *command;
2008 int index;
2010 if (src == dest)
2011 return True; /* No move needed, we're already there */
2013 if (dest == NULL)
2014 return False;
2016 wwin = icon->icon->owner;
2019 * For the moment we can't do this if we move icons in Clip from one
2020 * workspace to other, because if we move two or more icons without
2021 * command, the dialog box will not be able to tell us to which of the
2022 * moved icons it applies. -Dan
2024 if ((dest->type == WM_DOCK /*|| dest->keep_attracted */ ) && icon->command == NULL) {
2025 command = GetCommandForWindow(wwin->client_win);
2026 if (command) {
2027 icon->command = command;
2028 } else {
2029 icon->editing = 1;
2030 /* icon->forced_dock = 1; */
2031 if (wInputDialog(src->screen_ptr, _("Dock Icon"),
2032 _("Type the command used to launch the application"), &command)) {
2033 if (command && (command[0] == 0 || (command[0] == '-' && command[1] == 0))) {
2034 wfree(command);
2035 command = NULL;
2037 icon->command = command;
2038 } else {
2039 icon->editing = 0;
2040 if (command)
2041 wfree(command);
2042 return False;
2044 icon->editing = 0;
2048 if (dest->type == WM_DOCK)
2049 wClipMakeIconOmnipresent(icon, False);
2051 for (index = 1; index < src->max_icons; index++) {
2052 if (src->icon_array[index] == icon)
2053 break;
2055 assert(index < src->max_icons);
2057 src->icon_array[index] = NULL;
2058 src->icon_count--;
2060 for (index = 1; index < dest->max_icons; index++) {
2061 if (dest->icon_array[index] == NULL)
2062 break;
2064 /* if (index == dest->max_icons)
2065 return; */
2067 assert(index < dest->max_icons);
2069 dest->icon_array[index] = icon;
2070 icon->dock = dest;
2072 /* deselect the icon */
2073 if (icon->icon->selected)
2074 wIconSelect(icon->icon);
2076 if (dest->type == WM_DOCK) {
2077 icon->icon->core->descriptor.handle_enternotify = NULL;
2078 icon->icon->core->descriptor.handle_leavenotify = NULL;
2079 } else {
2080 icon->icon->core->descriptor.handle_enternotify = clipEnterNotify;
2081 icon->icon->core->descriptor.handle_leavenotify = clipLeaveNotify;
2084 /* set it to be kept when moving to dock.
2085 * Unless the icon does not have a command set
2087 if (icon->command && dest->type == WM_DOCK) {
2088 icon->attracted = 0;
2089 if (icon->icon->shadowed) {
2090 icon->icon->shadowed = 0;
2091 icon->icon->force_paint = 1;
2093 wAppIconSave(icon);
2096 if (src->auto_collapse || src->auto_raise_lower)
2097 clipLeave(src);
2099 icon->yindex = y;
2100 icon->xindex = x;
2102 icon->x_pos = dest->x_pos + x * ICON_SIZE;
2103 icon->y_pos = dest->y_pos + y * ICON_SIZE;
2105 dest->icon_count++;
2107 MoveInStackListUnder(dest->icon_array[index - 1]->icon->core, icon->icon->core);
2108 wAppIconPaint(icon);
2110 return True;
2113 void wDockDetach(WDock *dock, WAppIcon *icon)
2115 int index;
2117 /* make the settings panel be closed */
2118 if (icon->panel) {
2119 DestroyDockAppSettingsPanel(icon->panel);
2122 /* This must be called before icon->dock is set to NULL.
2123 * Don't move it. -Dan
2125 wClipMakeIconOmnipresent(icon, False);
2127 icon->docked = 0;
2128 icon->dock = NULL;
2129 icon->attracted = 0;
2130 icon->auto_launch = 0;
2131 if (icon->icon->shadowed) {
2132 icon->icon->shadowed = 0;
2133 icon->icon->force_paint = 1;
2136 /* deselect the icon */
2137 if (icon->icon->selected)
2138 wIconSelect(icon->icon);
2140 if (icon->command) {
2141 wfree(icon->command);
2142 icon->command = NULL;
2144 #ifdef XDND /* was OFFIX */
2145 if (icon->dnd_command) {
2146 wfree(icon->dnd_command);
2147 icon->dnd_command = NULL;
2149 #endif
2150 if (icon->paste_command) {
2151 wfree(icon->paste_command);
2152 icon->paste_command = NULL;
2155 for (index = 1; index < dock->max_icons; index++)
2156 if (dock->icon_array[index] == icon)
2157 break;
2158 assert(index < dock->max_icons);
2159 dock->icon_array[index] = NULL;
2160 icon->yindex = -1;
2161 icon->xindex = -1;
2163 dock->icon_count--;
2165 /* if the dock is not attached to an application or
2166 * the application did not set the appropriate hints yet,
2167 * destroy the icon */
2168 if (!icon->running || !wApplicationOf(icon->main_window))
2169 wAppIconDestroy(icon);
2170 else {
2171 icon->icon->core->descriptor.handle_mousedown = appIconMouseDown;
2172 icon->icon->core->descriptor.handle_enternotify = NULL;
2173 icon->icon->core->descriptor.handle_leavenotify = NULL;
2174 icon->icon->core->descriptor.parent_type = WCLASS_APPICON;
2175 icon->icon->core->descriptor.parent = icon;
2177 ChangeStackingLevel(icon->icon->core, NORMAL_ICON_LEVEL);
2179 wAppIconPaint(icon);
2180 if (wPreferences.auto_arrange_icons) {
2181 wArrangeIcons(dock->screen_ptr, True);
2184 if (dock->auto_collapse || dock->auto_raise_lower)
2185 clipLeave(dock);
2189 * returns the closest Dock slot index for the passed
2190 * coordinates.
2192 * Returns False if icon can't be docked.
2194 * Note: this function should NEVER alter ret_x or ret_y, unless it will
2195 * return True. -Dan
2197 Bool wDockSnapIcon(WDock *dock, WAppIcon *icon, int req_x, int req_y, int *ret_x, int *ret_y, int redocking)
2199 WScreen *scr = dock->screen_ptr;
2200 int dx, dy;
2201 int ex_x, ex_y;
2202 int i, offset = ICON_SIZE / 2;
2203 WAppIcon *aicon = NULL;
2204 WAppIcon *nicon = NULL;
2205 int max_y_icons;
2207 /* TODO: XINERAMA, for these */
2208 max_y_icons = scr->scr_height / ICON_SIZE - 1;
2210 if (wPreferences.flags.noupdates)
2211 return False;
2213 dx = dock->x_pos;
2214 dy = dock->y_pos;
2216 /* if the dock is full */
2217 if (!redocking && (dock->icon_count >= dock->max_icons)) {
2218 return False;
2221 /* exact position */
2222 if (req_y < dy)
2223 ex_y = (req_y - offset - dy) / ICON_SIZE;
2224 else
2225 ex_y = (req_y + offset - dy) / ICON_SIZE;
2227 if (req_x < dx)
2228 ex_x = (req_x - offset - dx) / ICON_SIZE;
2229 else
2230 ex_x = (req_x + offset - dx) / ICON_SIZE;
2232 /* check if the icon is outside the screen boundaries */
2234 WMRect rect;
2235 int flags;
2237 rect.pos.x = dx + ex_x * ICON_SIZE;
2238 rect.pos.y = dy + ex_y * ICON_SIZE;
2239 rect.size.width = rect.size.height = ICON_SIZE;
2241 wGetRectPlacementInfo(scr, rect, &flags);
2242 if (flags & (XFLAG_DEAD | XFLAG_PARTIAL))
2243 return False;
2246 if (dock->type == WM_DOCK) {
2247 if (icon->dock != dock && ex_x != 0)
2248 return False;
2250 aicon = NULL;
2251 for (i = 0; i < dock->max_icons; i++) {
2252 nicon = dock->icon_array[i];
2253 if (nicon && nicon->yindex == ex_y) {
2254 aicon = nicon;
2255 break;
2259 if (redocking) {
2260 int sig, done, closest;
2262 /* Possible cases when redocking:
2264 * icon dragged out of range of any slot -> false
2265 * icon dragged to range of free slot
2266 * icon dragged to range of same slot
2267 * icon dragged to range of different icon
2269 if (abs(ex_x) > DOCK_DETTACH_THRESHOLD)
2270 return False;
2272 if (ex_y >= 0 && ex_y <= max_y_icons && (aicon == icon || !aicon)) {
2273 *ret_x = 0;
2274 *ret_y = ex_y;
2275 return True;
2278 /* start looking at the upper slot or lower? */
2279 if (ex_y * ICON_SIZE < (req_y + offset - dy))
2280 sig = 1;
2281 else
2282 sig = -1;
2284 closest = -1;
2285 done = 0;
2286 /* look for closest free slot */
2287 for (i = 0; i < (DOCK_DETTACH_THRESHOLD + 1) * 2 && !done; i++) {
2288 int j;
2290 done = 1;
2291 closest = sig * (i / 2) + ex_y;
2292 /* check if this slot is used */
2293 if (closest >= 0) {
2294 for (j = 0; j < dock->max_icons; j++) {
2295 if (dock->icon_array[j]
2296 && dock->icon_array[j]->yindex == closest) {
2297 /* slot is used by someone else */
2298 if (dock->icon_array[j] != icon)
2299 done = 0;
2300 break;
2304 sig = -sig;
2306 if (done && closest >= 0 && closest <= max_y_icons &&
2307 ((ex_y >= closest && ex_y - closest < DOCK_DETTACH_THRESHOLD + 1)
2308 || (ex_y < closest && closest - ex_y <= DOCK_DETTACH_THRESHOLD + 1))) {
2309 *ret_x = 0;
2310 *ret_y = closest;
2311 return True;
2313 } else { /* !redocking */
2315 /* if slot is free and the icon is close enough, return it */
2316 if (!aicon && ex_x == 0 && ex_y >= 0 && ex_y <= max_y_icons) {
2317 *ret_x = 0;
2318 *ret_y = ex_y;
2319 return True;
2322 } else { /* CLIP */
2323 int neighbours = 0;
2324 int start, stop, k;
2326 start = icon->omnipresent ? 0 : scr->current_workspace;
2327 stop = icon->omnipresent ? scr->workspace_count : start + 1;
2329 aicon = NULL;
2330 for (k = start; k < stop; k++) {
2331 WDock *tmp = scr->workspaces[k]->clip;
2332 if (!tmp)
2333 continue;
2334 for (i = 0; i < tmp->max_icons; i++) {
2335 nicon = tmp->icon_array[i];
2336 if (nicon && nicon->xindex == ex_x && nicon->yindex == ex_y) {
2337 aicon = nicon;
2338 break;
2341 if (aicon)
2342 break;
2344 for (k = start; k < stop; k++) {
2345 WDock *tmp = scr->workspaces[k]->clip;
2346 if (!tmp)
2347 continue;
2348 for (i = 0; i < tmp->max_icons; i++) {
2349 nicon = tmp->icon_array[i];
2350 if (nicon && nicon != icon && /* Icon can't be it's own neighbour */
2351 (abs(nicon->xindex - ex_x) <= CLIP_ATTACH_VICINITY &&
2352 abs(nicon->yindex - ex_y) <= CLIP_ATTACH_VICINITY)) {
2353 neighbours = 1;
2354 break;
2357 if (neighbours)
2358 break;
2361 if (neighbours && (aicon == NULL || (redocking && aicon == icon))) {
2362 *ret_x = ex_x;
2363 *ret_y = ex_y;
2364 return True;
2367 return False;
2370 static int onScreen(WScreen *scr, int x, int y, int sx, int ex, int sy, int ey)
2372 WMRect rect = wmkrect(x, y, ICON_SIZE, ICON_SIZE);
2373 int flags;
2375 wGetRectPlacementInfo(scr, rect, &flags);
2377 return !(flags & (XFLAG_DEAD | XFLAG_PARTIAL));
2381 * returns true if it can find a free slot in the dock,
2382 * in which case it changes x_pos and y_pos accordingly.
2383 * Else returns false.
2385 Bool wDockFindFreeSlot(WDock *dock, int *x_pos, int *y_pos)
2387 WScreen *scr = dock->screen_ptr;
2388 WAppIcon *btn;
2389 WAppIconChain *chain;
2390 unsigned char *slot_map;
2391 int mwidth;
2392 int r;
2393 int x, y;
2394 int i, done = False;
2395 int corner;
2396 int sx = 0, sy = 0, ex = scr->scr_width, ey = scr->scr_height;
2397 int extra_count = 0;
2399 if (dock->type == WM_CLIP && dock != scr->workspaces[scr->current_workspace]->clip)
2400 extra_count = scr->global_icon_count;
2402 /* if the dock is full */
2403 if (dock->icon_count + extra_count >= dock->max_icons) {
2404 return False;
2407 if (!wPreferences.flags.nodock && scr->dock) {
2408 if (scr->dock->on_right_side)
2409 ex -= ICON_SIZE + DOCK_EXTRA_SPACE;
2410 else
2411 sx += ICON_SIZE + DOCK_EXTRA_SPACE;
2414 if (ex < dock->x_pos)
2415 ex = dock->x_pos;
2416 if (sx > dock->x_pos + ICON_SIZE)
2417 sx = dock->x_pos + ICON_SIZE;
2418 #define C_NONE 0
2419 #define C_NW 1
2420 #define C_NE 2
2421 #define C_SW 3
2422 #define C_SE 4
2424 /* check if clip is in a corner */
2425 if (dock->type == WM_CLIP) {
2426 if (dock->x_pos < 1 && dock->y_pos < 1)
2427 corner = C_NE;
2428 else if (dock->x_pos < 1 && dock->y_pos >= (ey - ICON_SIZE))
2429 corner = C_SE;
2430 else if (dock->x_pos >= (ex - ICON_SIZE) && dock->y_pos >= (ey - ICON_SIZE))
2431 corner = C_SW;
2432 else if (dock->x_pos >= (ex - ICON_SIZE) && dock->y_pos < 1)
2433 corner = C_NW;
2434 else
2435 corner = C_NONE;
2436 } else
2437 corner = C_NONE;
2439 /* If the clip is in the corner, use only slots that are in the border
2440 * of the screen */
2441 if (corner != C_NONE) {
2442 char *hmap, *vmap;
2443 int hcount, vcount;
2445 hcount = WMIN(dock->max_icons, scr->scr_width / ICON_SIZE);
2446 vcount = WMIN(dock->max_icons, scr->scr_height / ICON_SIZE);
2447 hmap = wmalloc(hcount + 1);
2448 vmap = wmalloc(vcount + 1);
2450 /* mark used positions */
2451 switch (corner) {
2452 case C_NE:
2453 for (i = 0; i < dock->max_icons; i++) {
2454 btn = dock->icon_array[i];
2455 if (!btn)
2456 continue;
2458 if (btn->xindex == 0 && btn->yindex > 0 && btn->yindex < vcount)
2459 vmap[btn->yindex] = 1;
2460 else if (btn->yindex == 0 && btn->xindex > 0 && btn->xindex < hcount)
2461 hmap[btn->xindex] = 1;
2463 for (chain = scr->global_icons; chain != NULL; chain = chain->next) {
2464 btn = chain->aicon;
2465 if (btn->xindex == 0 && btn->yindex > 0 && btn->yindex < vcount)
2466 vmap[btn->yindex] = 1;
2467 else if (btn->yindex == 0 && btn->xindex > 0 && btn->xindex < hcount)
2468 hmap[btn->xindex] = 1;
2470 break;
2471 case C_NW:
2472 for (i = 0; i < dock->max_icons; i++) {
2473 btn = dock->icon_array[i];
2474 if (!btn)
2475 continue;
2477 if (btn->xindex == 0 && btn->yindex > 0 && btn->yindex < vcount)
2478 vmap[btn->yindex] = 1;
2479 else if (btn->yindex == 0 && btn->xindex < 0 && btn->xindex > -hcount)
2480 hmap[-btn->xindex] = 1;
2482 for (chain = scr->global_icons; chain != NULL; chain = chain->next) {
2483 btn = chain->aicon;
2484 if (btn->xindex == 0 && btn->yindex > 0 && btn->yindex < vcount)
2485 vmap[btn->yindex] = 1;
2486 else if (btn->yindex == 0 && btn->xindex < 0 && btn->xindex > -hcount)
2487 hmap[-btn->xindex] = 1;
2489 break;
2490 case C_SE:
2491 for (i = 0; i < dock->max_icons; i++) {
2492 btn = dock->icon_array[i];
2493 if (!btn)
2494 continue;
2496 if (btn->xindex == 0 && btn->yindex < 0 && btn->yindex > -vcount)
2497 vmap[-btn->yindex] = 1;
2498 else if (btn->yindex == 0 && btn->xindex > 0 && btn->xindex < hcount)
2499 hmap[btn->xindex] = 1;
2501 for (chain = scr->global_icons; chain != NULL; chain = chain->next) {
2502 btn = chain->aicon;
2503 if (btn->xindex == 0 && btn->yindex < 0 && btn->yindex > -vcount)
2504 vmap[-btn->yindex] = 1;
2505 else if (btn->yindex == 0 && btn->xindex > 0 && btn->xindex < hcount)
2506 hmap[btn->xindex] = 1;
2508 break;
2509 case C_SW:
2510 default:
2511 for (i = 0; i < dock->max_icons; i++) {
2512 btn = dock->icon_array[i];
2513 if (!btn)
2514 continue;
2516 if (btn->xindex == 0 && btn->yindex < 0 && btn->yindex > -vcount)
2517 vmap[-btn->yindex] = 1;
2518 else if (btn->yindex == 0 && btn->xindex < 0 && btn->xindex > -hcount)
2519 hmap[-btn->xindex] = 1;
2521 for (chain = scr->global_icons; chain != NULL; chain = chain->next) {
2522 btn = chain->aicon;
2523 if (btn->xindex == 0 && btn->yindex < 0 && btn->yindex > -vcount)
2524 vmap[-btn->yindex] = 1;
2525 else if (btn->yindex == 0 && btn->xindex < 0 && btn->xindex > -hcount)
2526 hmap[-btn->xindex] = 1;
2529 x = 0;
2530 y = 0;
2531 done = 0;
2532 /* search a vacant slot */
2533 for (i = 1; i < WMAX(vcount, hcount); i++) {
2534 if (i < vcount && vmap[i] == 0) {
2535 /* found a slot */
2536 x = 0;
2537 y = i;
2538 done = 1;
2539 break;
2540 } else if (i < hcount && hmap[i] == 0) {
2541 /* found a slot */
2542 x = i;
2543 y = 0;
2544 done = 1;
2545 break;
2548 wfree(vmap);
2549 wfree(hmap);
2550 /* If found a slot, translate and return */
2551 if (done) {
2552 if (corner == C_NW || corner == C_NE) {
2553 *y_pos = y;
2554 } else {
2555 *y_pos = -y;
2557 if (corner == C_NE || corner == C_SE) {
2558 *x_pos = x;
2559 } else {
2560 *x_pos = -x;
2562 return True;
2564 /* else, try to find a slot somewhere else */
2567 /* a map of mwidth x mwidth would be enough if we allowed icons to be
2568 * placed outside of screen */
2569 mwidth = (int)ceil(sqrt(dock->max_icons));
2571 /* In the worst case (the clip is in the corner of the screen),
2572 * the amount of icons that fit in the clip is smaller.
2573 * Double the map to get a safe value.
2575 mwidth += mwidth;
2577 r = (mwidth - 1) / 2;
2579 slot_map = wmalloc(mwidth * mwidth);
2581 #define XY2OFS(x,y) (WMAX(abs(x),abs(y)) > r) ? 0 : (((y)+r)*(mwidth)+(x)+r)
2583 /* mark used slots in the map. If the slot falls outside the map
2584 * (for example, when all icons are placed in line), ignore them. */
2585 for (i = 0; i < dock->max_icons; i++) {
2586 btn = dock->icon_array[i];
2587 if (btn)
2588 slot_map[XY2OFS(btn->xindex, btn->yindex)] = 1;
2590 for (chain = scr->global_icons; chain != NULL; chain = chain->next) {
2591 slot_map[XY2OFS(chain->aicon->xindex, chain->aicon->yindex)] = 1;
2593 /* Find closest slot from the center that is free by scanning the
2594 * map from the center to outward in circular passes.
2595 * This will not result in a neat layout, but will be optimal
2596 * in the sense that there will not be holes left.
2598 done = 0;
2599 for (i = 1; i <= r && !done; i++) {
2600 int tx, ty;
2602 /* top and bottom parts of the ring */
2603 for (x = -i; x <= i && !done; x++) {
2604 tx = dock->x_pos + x * ICON_SIZE;
2605 y = -i;
2606 ty = dock->y_pos + y * ICON_SIZE;
2607 if (slot_map[XY2OFS(x, y)] == 0 && onScreen(scr, tx, ty, sx, ex, sy, ey)) {
2608 *x_pos = x;
2609 *y_pos = y;
2610 done = 1;
2611 break;
2613 y = i;
2614 ty = dock->y_pos + y * ICON_SIZE;
2615 if (slot_map[XY2OFS(x, y)] == 0 && onScreen(scr, tx, ty, sx, ex, sy, ey)) {
2616 *x_pos = x;
2617 *y_pos = y;
2618 done = 1;
2619 break;
2622 /* left and right parts of the ring */
2623 for (y = -i + 1; y <= i - 1; y++) {
2624 ty = dock->y_pos + y * ICON_SIZE;
2625 x = -i;
2626 tx = dock->x_pos + x * 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;
2633 x = i;
2634 tx = dock->x_pos + x * ICON_SIZE;
2635 if (slot_map[XY2OFS(x, y)] == 0 && onScreen(scr, tx, ty, sx, ex, sy, ey)) {
2636 *x_pos = x;
2637 *y_pos = y;
2638 done = 1;
2639 break;
2643 wfree(slot_map);
2644 #undef XY2OFS
2645 return done;
2648 static void moveDock(WDock *dock, int new_x, int new_y)
2650 WAppIcon *btn;
2651 int i;
2653 dock->x_pos = new_x;
2654 dock->y_pos = new_y;
2655 for (i = 0; i < dock->max_icons; i++) {
2656 btn = dock->icon_array[i];
2657 if (btn) {
2658 btn->x_pos = new_x + btn->xindex * ICON_SIZE;
2659 btn->y_pos = new_y + btn->yindex * ICON_SIZE;
2660 XMoveWindow(dpy, btn->icon->core->window, btn->x_pos, btn->y_pos);
2665 static void swapDock(WDock *dock)
2667 WScreen *scr = dock->screen_ptr;
2668 WAppIcon *btn;
2669 int x, i;
2671 if (dock->on_right_side) {
2672 x = dock->x_pos = scr->scr_width - ICON_SIZE - DOCK_EXTRA_SPACE;
2673 } else {
2674 x = dock->x_pos = DOCK_EXTRA_SPACE;
2677 for (i = 0; i < dock->max_icons; i++) {
2678 btn = dock->icon_array[i];
2679 if (btn) {
2680 btn->x_pos = x;
2681 XMoveWindow(dpy, btn->icon->core->window, btn->x_pos, btn->y_pos);
2685 wScreenUpdateUsableArea(scr);
2688 static pid_t execCommand(WAppIcon *btn, char *command, WSavedState *state)
2690 WScreen *scr = btn->icon->core->screen_ptr;
2691 pid_t pid;
2692 char **argv;
2693 int argc;
2694 char *cmdline;
2696 cmdline = ExpandOptions(scr, command);
2698 if (scr->flags.dnd_data_convertion_status || !cmdline) {
2699 if (cmdline)
2700 wfree(cmdline);
2701 if (state)
2702 wfree(state);
2703 return 0;
2706 wtokensplit(cmdline, &argv, &argc);
2708 if (!argc) {
2709 if (cmdline)
2710 wfree(cmdline);
2711 if (state)
2712 wfree(state);
2713 return 0;
2716 if ((pid = fork()) == 0) {
2717 char **args;
2718 int i;
2720 SetupEnvironment(scr);
2722 #ifdef HAVE_SETSID
2723 setsid();
2724 #endif
2726 args = malloc(sizeof(char *) * (argc + 1));
2727 if (!args)
2728 exit(111);
2729 for (i = 0; i < argc; i++) {
2730 args[i] = argv[i];
2732 args[argc] = NULL;
2733 execvp(argv[0], args);
2734 exit(111);
2736 wtokenfree(argv, argc);
2738 if (pid > 0) {
2739 if (!state) {
2740 state = wmalloc(sizeof(WSavedState));
2741 state->hidden = -1;
2742 state->miniaturized = -1;
2743 state->shaded = -1;
2744 if (btn->dock == scr->dock || btn->omnipresent)
2745 state->workspace = -1;
2746 else
2747 state->workspace = scr->current_workspace;
2749 wWindowAddSavedState(btn->wm_instance, btn->wm_class, cmdline, pid, state);
2750 wAddDeathHandler(pid, (WDeathHandler *) trackDeadProcess, btn->dock);
2751 } else if (state) {
2752 wfree(state);
2754 wfree(cmdline);
2755 return pid;
2758 void wDockHideIcons(WDock *dock)
2760 int i;
2762 if (dock == NULL)
2763 return;
2765 for (i = 1; i < dock->max_icons; i++) {
2766 if (dock->icon_array[i])
2767 XUnmapWindow(dpy, dock->icon_array[i]->icon->core->window);
2769 dock->mapped = 0;
2771 dockIconPaint(dock->icon_array[0]);
2774 void wDockShowIcons(WDock *dock)
2776 int i, newlevel;
2777 WAppIcon *btn;
2779 if (dock == NULL)
2780 return;
2782 btn = dock->icon_array[0];
2783 moveDock(dock, btn->x_pos, btn->y_pos);
2785 newlevel = dock->lowered ? WMNormalLevel : WMDockLevel;
2786 ChangeStackingLevel(btn->icon->core, newlevel);
2788 for (i = 1; i < dock->max_icons; i++) {
2789 if (dock->icon_array[i]) {
2790 MoveInStackListAbove(dock->icon_array[i]->icon->core, btn->icon->core);
2791 break;
2795 if (!dock->collapsed) {
2796 for (i = 1; i < dock->max_icons; i++) {
2797 if (dock->icon_array[i]) {
2798 XMapWindow(dpy, dock->icon_array[i]->icon->core->window);
2802 dock->mapped = 1;
2804 dockIconPaint(btn);
2807 void wDockLower(WDock *dock)
2809 int i;
2811 for (i = 0; i < dock->max_icons; i++) {
2812 if (dock->icon_array[i])
2813 wLowerFrame(dock->icon_array[i]->icon->core);
2817 void wDockRaise(WDock *dock)
2819 int i;
2821 for (i = dock->max_icons - 1; i >= 0; i--) {
2822 if (dock->icon_array[i])
2823 wRaiseFrame(dock->icon_array[i]->icon->core);
2827 void wDockRaiseLower(WDock *dock)
2829 if (!dock->icon_array[0]->icon->core->stacking->above
2830 || (dock->icon_array[0]->icon->core->stacking->window_level
2831 != dock->icon_array[0]->icon->core->stacking->above->stacking->window_level))
2832 wDockLower(dock);
2833 else
2834 wDockRaise(dock);
2837 void wDockFinishLaunch(WDock *dock, WAppIcon *icon)
2839 icon->launching = 0;
2840 icon->relaunching = 0;
2841 dockIconPaint(icon);
2844 WAppIcon *wDockFindIconForWindow(WDock *dock, Window window)
2846 WAppIcon *icon;
2847 int i;
2849 for (i = 0; i < dock->max_icons; i++) {
2850 icon = dock->icon_array[i];
2851 if (icon && icon->main_window == window)
2852 return icon;
2854 return NULL;
2857 void wDockTrackWindowLaunch(WDock *dock, Window window)
2859 WAppIcon *icon;
2860 char *wm_class, *wm_instance;
2861 int i;
2862 Bool firstPass = True;
2863 Bool found = False;
2864 char *command = NULL;
2866 command = GetCommandForWindow(window);
2868 if (!PropGetWMClass(window, &wm_class, &wm_instance) || (!wm_class && !wm_instance)) {
2870 if (command)
2871 wfree(command);
2872 return;
2875 retry:
2876 for (i = 0; i < dock->max_icons; i++) {
2877 icon = dock->icon_array[i];
2878 if (!icon)
2879 continue;
2881 /* app is already attached to icon */
2882 if (icon->main_window == window) {
2883 found = True;
2884 break;
2887 if ((icon->wm_instance || icon->wm_class)
2888 && (icon->launching || !icon->running)) {
2890 if (icon->wm_instance && wm_instance && strcmp(icon->wm_instance, wm_instance) != 0) {
2891 continue;
2893 if (icon->wm_class && wm_class && strcmp(icon->wm_class, wm_class) != 0) {
2894 continue;
2896 if (firstPass && command && strcmp(icon->command, command) != 0) {
2897 continue;
2900 if (!icon->relaunching) {
2901 WApplication *wapp;
2903 /* Possibly an application that was docked with dockit,
2904 * but the user did not update WMState to indicate that
2905 * it was docked by force */
2906 wapp = wApplicationOf(window);
2907 if (!wapp) {
2908 icon->forced_dock = 1;
2909 icon->running = 0;
2911 if (!icon->forced_dock) {
2912 icon->main_window = window;
2915 found = True;
2916 if (!wPreferences.no_animations && !icon->launching &&
2917 !dock->screen_ptr->flags.startup && !dock->collapsed) {
2918 WAppIcon *aicon;
2919 int x0, y0;
2921 icon->launching = 1;
2922 dockIconPaint(icon);
2924 aicon = wAppIconCreateForDock(dock->screen_ptr, NULL,
2925 wm_instance, wm_class, TILE_NORMAL);
2926 /* XXX: can: aicon->icon == NULL ? */
2927 PlaceIcon(dock->screen_ptr, &x0, &y0, wGetHeadForWindow(aicon->icon->owner));
2928 wAppIconMove(aicon, x0, y0);
2929 /* Should this always be lowered? -Dan */
2930 if (dock->lowered)
2931 wLowerFrame(aicon->icon->core);
2932 XMapWindow(dpy, aicon->icon->core->window);
2933 aicon->launching = 1;
2934 wAppIconPaint(aicon);
2935 SlideWindow(aicon->icon->core->window, x0, y0, icon->x_pos, icon->y_pos);
2936 XUnmapWindow(dpy, aicon->icon->core->window);
2937 wAppIconDestroy(aicon);
2939 wDockFinishLaunch(dock, icon);
2940 break;
2944 if (firstPass && !found) {
2945 firstPass = False;
2946 goto retry;
2949 if (command)
2950 wfree(command);
2952 if (wm_class)
2953 free(wm_class);
2954 if (wm_instance)
2955 free(wm_instance);
2958 void wClipUpdateForWorkspaceChange(WScreen *scr, int workspace)
2960 if (!wPreferences.flags.noclip) {
2961 scr->clip_icon->dock = scr->workspaces[workspace]->clip;
2962 if (scr->current_workspace != workspace) {
2963 WDock *old_clip = scr->workspaces[scr->current_workspace]->clip;
2964 WAppIconChain *chain = scr->global_icons;
2966 while (chain) {
2967 moveIconBetweenDocks(chain->aicon->dock,
2968 scr->workspaces[workspace]->clip,
2969 chain->aicon, chain->aicon->xindex, chain->aicon->yindex);
2970 if (scr->workspaces[workspace]->clip->collapsed)
2971 XUnmapWindow(dpy, chain->aicon->icon->core->window);
2972 chain = chain->next;
2975 wDockHideIcons(old_clip);
2976 if (old_clip->auto_raise_lower) {
2977 if (old_clip->auto_raise_magic) {
2978 WMDeleteTimerHandler(old_clip->auto_raise_magic);
2979 old_clip->auto_raise_magic = NULL;
2981 wDockLower(old_clip);
2983 if (old_clip->auto_collapse) {
2984 if (old_clip->auto_expand_magic) {
2985 WMDeleteTimerHandler(old_clip->auto_expand_magic);
2986 old_clip->auto_expand_magic = NULL;
2988 old_clip->collapsed = 1;
2990 wDockShowIcons(scr->workspaces[workspace]->clip);
2995 static void trackDeadProcess(pid_t pid, unsigned char status, WDock *dock)
2997 WAppIcon *icon;
2998 int i;
3000 for (i = 0; i < dock->max_icons; i++) {
3001 icon = dock->icon_array[i];
3002 if (!icon)
3003 continue;
3005 if (icon->launching && icon->pid == pid) {
3006 if (!icon->relaunching) {
3007 icon->running = 0;
3008 icon->main_window = None;
3010 wDockFinishLaunch(dock, icon);
3011 icon->pid = 0;
3012 if (status == 111) {
3013 char msg[PATH_MAX];
3014 char *cmd;
3016 #ifdef XDND
3017 if (icon->drop_launch)
3018 cmd = icon->dnd_command;
3019 else
3020 #endif
3021 if (icon->paste_launch)
3022 cmd = icon->paste_command;
3023 else
3024 cmd = icon->command;
3026 snprintf(msg, sizeof(msg), _("Could not execute command \"%s\""), cmd);
3028 wMessageDialog(dock->screen_ptr, _("Error"), msg, _("OK"), NULL, NULL);
3030 break;
3035 static void toggleLowered(WDock *dock)
3037 WAppIcon *tmp;
3038 int newlevel, i;
3040 /* lower/raise Dock */
3041 if (!dock->lowered) {
3042 newlevel = WMNormalLevel;
3043 dock->lowered = 1;
3044 } else {
3045 newlevel = WMDockLevel;
3046 dock->lowered = 0;
3049 for (i = 0; i < dock->max_icons; i++) {
3050 tmp = dock->icon_array[i];
3051 if (!tmp)
3052 continue;
3054 ChangeStackingLevel(tmp->icon->core, newlevel);
3055 if (dock->lowered)
3056 wLowerFrame(tmp->icon->core);
3059 if (dock->type == WM_DOCK)
3060 wScreenUpdateUsableArea(dock->screen_ptr);
3063 static void toggleCollapsed(WDock *dock)
3065 if (dock->collapsed) {
3066 dock->collapsed = 0;
3067 wDockShowIcons(dock);
3068 } else {
3069 dock->collapsed = 1;
3070 wDockHideIcons(dock);
3074 static void openDockMenu(WDock *dock, WAppIcon *aicon, XEvent *event)
3076 WScreen *scr = dock->screen_ptr;
3077 WObjDescriptor *desc;
3078 WMenuEntry *entry;
3079 WApplication *wapp = NULL;
3080 int index = 0;
3081 int x_pos;
3082 int n_selected;
3083 int appIsRunning = aicon->running && aicon->icon && aicon->icon->owner;
3085 if (dock->type == WM_DOCK) {
3086 /* keep on top */
3087 entry = dock->menu->entries[index];
3088 entry->flags.indicator_on = !dock->lowered;
3089 entry->clientdata = dock;
3090 dock->menu->flags.realized = 0;
3091 } else {
3092 /* clip options */
3093 if (scr->clip_options)
3094 updateClipOptionsMenu(scr->clip_options, dock);
3096 n_selected = numberOfSelectedIcons(dock);
3098 /* Rename Workspace */
3099 entry = dock->menu->entries[++index];
3100 if (aicon == scr->clip_icon) {
3101 entry->callback = renameCallback;
3102 entry->clientdata = dock;
3103 entry->flags.indicator = 0;
3104 entry->text = _("Rename Workspace");
3105 } else {
3106 entry->callback = omnipresentCallback;
3107 entry->clientdata = aicon;
3108 if (n_selected > 0) {
3109 entry->flags.indicator = 0;
3110 entry->text = _("Toggle Omnipresent");
3111 } else {
3112 entry->flags.indicator = 1;
3113 entry->flags.indicator_on = aicon->omnipresent;
3114 entry->flags.indicator_type = MI_CHECK;
3115 entry->text = _("Omnipresent");
3119 /* select/unselect icon */
3120 entry = dock->menu->entries[++index];
3121 entry->clientdata = aicon;
3122 entry->flags.indicator_on = aicon->icon->selected;
3123 wMenuSetEnabled(dock->menu, index, aicon != scr->clip_icon);
3125 /* select/unselect all icons */
3126 entry = dock->menu->entries[++index];
3127 entry->clientdata = aicon;
3128 if (n_selected > 0)
3129 entry->text = _("Unselect All Icons");
3130 else
3131 entry->text = _("Select All Icons");
3132 wMenuSetEnabled(dock->menu, index, dock->icon_count > 1);
3134 /* keep icon(s) */
3135 entry = dock->menu->entries[++index];
3136 entry->clientdata = aicon;
3137 if (n_selected > 1)
3138 entry->text = _("Keep Icons");
3139 else
3140 entry->text = _("Keep Icon");
3141 wMenuSetEnabled(dock->menu, index, dock->icon_count > 1);
3143 /* this is the workspace submenu part */
3144 entry = dock->menu->entries[++index];
3145 if (n_selected > 1)
3146 entry->text = _("Move Icons To");
3147 else
3148 entry->text = _("Move Icon To");
3149 if (scr->clip_submenu)
3150 updateWorkspaceMenu(scr->clip_submenu, aicon);
3151 wMenuSetEnabled(dock->menu, index, !aicon->omnipresent);
3153 /* remove icon(s) */
3154 entry = dock->menu->entries[++index];
3155 entry->clientdata = aicon;
3156 if (n_selected > 1)
3157 entry->text = _("Remove Icons");
3158 else
3159 entry->text = _("Remove Icon");
3160 wMenuSetEnabled(dock->menu, index, dock->icon_count > 1);
3162 /* attract icon(s) */
3163 entry = dock->menu->entries[++index];
3164 entry->clientdata = aicon;
3166 dock->menu->flags.realized = 0;
3167 wMenuRealize(dock->menu);
3170 if (aicon->icon->owner) {
3171 wapp = wApplicationOf(aicon->icon->owner->main_window);
3172 } else {
3173 wapp = NULL;
3176 /* launch */
3177 entry = dock->menu->entries[++index];
3178 entry->clientdata = aicon;
3179 wMenuSetEnabled(dock->menu, index, aicon->command != NULL);
3181 /* unhide here */
3182 entry = dock->menu->entries[++index];
3183 entry->clientdata = aicon;
3184 if (wapp && wapp->flags.hidden) {
3185 entry->text = _("Unhide Here");
3186 } else {
3187 entry->text = _("Bring Here");
3189 wMenuSetEnabled(dock->menu, index, appIsRunning);
3191 /* hide */
3192 entry = dock->menu->entries[++index];
3193 entry->clientdata = aicon;
3194 if (wapp && wapp->flags.hidden) {
3195 entry->text = _("Unhide");
3196 } else {
3197 entry->text = _("Hide");
3199 wMenuSetEnabled(dock->menu, index, appIsRunning);
3201 /* settings */
3202 entry = dock->menu->entries[++index];
3203 entry->clientdata = aicon;
3204 wMenuSetEnabled(dock->menu, index, !aicon->editing && !wPreferences.flags.noupdates);
3206 /* kill */
3207 entry = dock->menu->entries[++index];
3208 entry->clientdata = aicon;
3209 wMenuSetEnabled(dock->menu, index, appIsRunning);
3211 if (!dock->menu->flags.realized)
3212 wMenuRealize(dock->menu);
3214 if (dock->type == WM_CLIP) {
3215 /*x_pos = event->xbutton.x_root+2; */
3216 x_pos = event->xbutton.x_root - dock->menu->frame->core->width / 2 - 1;
3217 if (x_pos < 0) {
3218 x_pos = 0;
3219 } else if (x_pos + dock->menu->frame->core->width > scr->scr_width - 2) {
3220 x_pos = scr->scr_width - dock->menu->frame->core->width - 4;
3222 } else {
3223 x_pos = dock->on_right_side ? scr->scr_width - dock->menu->frame->core->width - 3 : 0;
3226 wMenuMapAt(dock->menu, x_pos, event->xbutton.y_root + 2, False);
3228 /* allow drag select */
3229 event->xany.send_event = True;
3230 desc = &dock->menu->menu->descriptor;
3231 (*desc->handle_mousedown) (desc, event);
3234 /******************************************************************/
3235 static void iconDblClick(WObjDescriptor *desc, XEvent *event)
3237 WAppIcon *btn = desc->parent;
3238 WDock *dock = btn->dock;
3239 WApplication *wapp = NULL;
3240 int unhideHere = 0;
3242 if (btn->icon->owner && !(event->xbutton.state & ControlMask)) {
3243 wapp = wApplicationOf(btn->icon->owner->main_window);
3245 assert(wapp != NULL);
3247 unhideHere = (event->xbutton.state & ShiftMask);
3249 /* go to the last workspace that the user worked on the app */
3250 if (wapp->last_workspace != dock->screen_ptr->current_workspace && !unhideHere) {
3251 wWorkspaceChange(dock->screen_ptr, wapp->last_workspace);
3254 wUnhideApplication(wapp, event->xbutton.button == Button2, unhideHere);
3256 if (event->xbutton.state & MOD_MASK) {
3257 wHideOtherApplications(btn->icon->owner);
3259 } else {
3260 if (event->xbutton.button == Button1) {
3262 if (event->xbutton.state & MOD_MASK) {
3263 /* raise/lower dock */
3264 toggleLowered(dock);
3265 } else if (btn == dock->screen_ptr->clip_icon) {
3266 if (getClipButton(event->xbutton.x, event->xbutton.y) == CLIP_IDLE)
3267 toggleCollapsed(dock);
3268 else
3269 handleClipChangeWorkspace(dock->screen_ptr, event);
3270 } else if (btn->command) {
3271 if (!btn->launching && (!btn->running || (event->xbutton.state & ControlMask)))
3272 launchDockedApplication(btn, False);
3278 static void handleDockMove(WDock *dock, WAppIcon *aicon, XEvent *event)
3280 WScreen *scr = dock->screen_ptr;
3281 int ofs_x = event->xbutton.x, ofs_y = event->xbutton.y;
3282 int x, y;
3283 XEvent ev;
3284 int grabbed = 0, swapped = 0, done;
3285 Pixmap ghost = None;
3286 int superfluous = wPreferences.superfluous; /* we catch it to avoid problems */
3288 if (XGrabPointer(dpy, aicon->icon->core->window, True, ButtonMotionMask
3289 | ButtonReleaseMask | ButtonPressMask, GrabModeAsync,
3290 GrabModeAsync, None, None, CurrentTime) != GrabSuccess) {
3291 wwarning("pointer grab failed for dock move");
3293 y = 0;
3294 for (x = 0; x < dock->max_icons; x++) {
3295 if (dock->icon_array[x] != NULL && dock->icon_array[x]->yindex > y)
3296 y = dock->icon_array[x]->yindex;
3298 y++;
3299 XResizeWindow(dpy, scr->dock_shadow, ICON_SIZE, ICON_SIZE * y);
3301 done = 0;
3302 while (!done) {
3303 WMMaskEvent(dpy, PointerMotionMask | ButtonReleaseMask | ButtonPressMask
3304 | ButtonMotionMask | ExposureMask | EnterWindowMask, &ev);
3305 switch (ev.type) {
3306 case Expose:
3307 WMHandleEvent(&ev);
3308 break;
3310 case EnterNotify:
3311 /* It means the cursor moved so fast that it entered
3312 * something else (if moving slowly, it would have
3313 * stayed in the dock that is being moved. Ignore such
3314 * "spurious" EnterNotifiy's */
3315 break;
3317 case MotionNotify:
3318 if (!grabbed) {
3319 if (abs(ofs_x - ev.xmotion.x) >= MOVE_THRESHOLD
3320 || abs(ofs_y - ev.xmotion.y) >= MOVE_THRESHOLD) {
3321 XChangeActivePointerGrab(dpy, ButtonMotionMask
3322 | ButtonReleaseMask | ButtonPressMask,
3323 wCursor[WCUR_MOVE], CurrentTime);
3324 grabbed = 1;
3326 break;
3328 if (dock->type == WM_CLIP) {
3329 x = ev.xmotion.x_root - ofs_x;
3330 y = ev.xmotion.y_root - ofs_y;
3331 wScreenKeepInside(scr, &x, &y, ICON_SIZE, ICON_SIZE);
3333 moveDock(dock, x, y);
3334 } else {
3335 /* move vertically if pointer is inside the dock */
3336 if ((dock->on_right_side && ev.xmotion.x_root >= dock->x_pos - ICON_SIZE)
3337 || (!dock->on_right_side && ev.xmotion.x_root <= dock->x_pos + ICON_SIZE * 2)) {
3339 x = ev.xmotion.x_root - ofs_x;
3340 y = ev.xmotion.y_root - ofs_y;
3341 wScreenKeepInside(scr, &x, &y, ICON_SIZE, ICON_SIZE);
3342 moveDock(dock, dock->x_pos, y);
3344 /* move horizontally to change sides */
3345 x = ev.xmotion.x_root - ofs_x;
3346 if (!dock->on_right_side) {
3348 /* is on left */
3350 if (ev.xmotion.x_root > dock->x_pos + ICON_SIZE * 2) {
3351 XMoveWindow(dpy, scr->dock_shadow, scr->scr_width - ICON_SIZE
3352 - DOCK_EXTRA_SPACE - 1, dock->y_pos);
3353 if (superfluous && ghost == None) {
3354 ghost = MakeGhostDock(dock, dock->x_pos,
3355 scr->scr_width - ICON_SIZE
3356 - DOCK_EXTRA_SPACE - 1, dock->y_pos);
3357 XSetWindowBackgroundPixmap(dpy, scr->dock_shadow, ghost);
3358 XClearWindow(dpy, scr->dock_shadow);
3360 XMapRaised(dpy, scr->dock_shadow);
3361 swapped = 1;
3362 } else {
3363 if (superfluous && ghost != None) {
3364 XFreePixmap(dpy, ghost);
3365 ghost = None;
3367 XUnmapWindow(dpy, scr->dock_shadow);
3368 swapped = 0;
3370 } else {
3371 /* is on right */
3372 if (ev.xmotion.x_root < dock->x_pos - ICON_SIZE) {
3373 XMoveWindow(dpy, scr->dock_shadow, DOCK_EXTRA_SPACE, dock->y_pos);
3374 if (superfluous && ghost == None) {
3375 ghost = MakeGhostDock(dock, dock->x_pos,
3376 DOCK_EXTRA_SPACE, dock->y_pos);
3377 XSetWindowBackgroundPixmap(dpy, scr->dock_shadow, ghost);
3378 XClearWindow(dpy, scr->dock_shadow);
3380 XMapRaised(dpy, scr->dock_shadow);
3381 swapped = -1;
3382 } else {
3383 XUnmapWindow(dpy, scr->dock_shadow);
3384 swapped = 0;
3385 if (superfluous && ghost != None) {
3386 XFreePixmap(dpy, ghost);
3387 ghost = None;
3392 break;
3394 case ButtonPress:
3395 break;
3397 case ButtonRelease:
3398 if (ev.xbutton.button != event->xbutton.button)
3399 break;
3400 XUngrabPointer(dpy, CurrentTime);
3401 XUnmapWindow(dpy, scr->dock_shadow);
3402 XResizeWindow(dpy, scr->dock_shadow, ICON_SIZE, ICON_SIZE);
3403 if (dock->type == WM_DOCK) {
3404 if (swapped != 0) {
3405 if (swapped > 0)
3406 dock->on_right_side = 1;
3407 else
3408 dock->on_right_side = 0;
3409 swapDock(dock);
3410 wArrangeIcons(scr, False);
3413 done = 1;
3414 break;
3417 if (superfluous) {
3418 if (ghost != None)
3419 XFreePixmap(dpy, ghost);
3420 XSetWindowBackground(dpy, scr->dock_shadow, scr->white_pixel);
3424 static Bool handleIconMove(WDock *dock, WAppIcon *aicon, XEvent *event)
3426 WScreen *scr = dock->screen_ptr;
3427 Window wins[2];
3428 WIcon *icon = aicon->icon;
3429 WDock *dock2 = NULL, *last_dock = dock, *clip = NULL;
3430 int ondock, grabbed = 0, change_dock = 0, collapsed = 0;
3431 XEvent ev;
3432 int x = aicon->x_pos, y = aicon->y_pos;
3433 int ofs_x = event->xbutton.x, ofs_y = event->xbutton.y;
3434 int shad_x = x, shad_y = y;
3435 int ix = aicon->xindex, iy = aicon->yindex;
3436 int tmp;
3437 Pixmap ghost = None;
3438 Bool docked;
3439 int superfluous = wPreferences.superfluous; /* we catch it to avoid problems */
3440 int omnipresent = aicon->omnipresent; /* this must be cached!!! */
3441 Bool hasMoved = False;
3443 if (wPreferences.flags.noupdates)
3444 return hasMoved;
3446 if (XGrabPointer(dpy, icon->core->window, True, ButtonMotionMask
3447 | ButtonReleaseMask | ButtonPressMask, GrabModeAsync,
3448 GrabModeAsync, None, None, CurrentTime) != GrabSuccess) {
3451 if (!(event->xbutton.state & MOD_MASK))
3452 wRaiseFrame(icon->core);
3454 if (!wPreferences.flags.noclip)
3455 clip = scr->workspaces[scr->current_workspace]->clip;
3457 if (dock == scr->dock && !wPreferences.flags.noclip)
3458 dock2 = clip;
3459 else if (dock != scr->dock && !wPreferences.flags.nodock)
3460 dock2 = scr->dock;
3462 wins[0] = icon->core->window;
3463 wins[1] = scr->dock_shadow;
3464 XRestackWindows(dpy, wins, 2);
3465 XMoveResizeWindow(dpy, scr->dock_shadow, aicon->x_pos, aicon->y_pos, ICON_SIZE, ICON_SIZE);
3466 if (superfluous) {
3467 if (icon->pixmap != None)
3468 ghost = MakeGhostIcon(scr, icon->pixmap);
3469 else
3470 ghost = MakeGhostIcon(scr, icon->core->window);
3472 XSetWindowBackgroundPixmap(dpy, scr->dock_shadow, ghost);
3473 XClearWindow(dpy, scr->dock_shadow);
3475 XMapWindow(dpy, scr->dock_shadow);
3477 ondock = 1;
3479 while (1) {
3480 XMaskEvent(dpy, PointerMotionMask | ButtonReleaseMask | ButtonPressMask
3481 | ButtonMotionMask | ExposureMask, &ev);
3482 switch (ev.type) {
3483 case Expose:
3484 WMHandleEvent(&ev);
3485 break;
3487 case MotionNotify:
3488 hasMoved = True;
3489 if (!grabbed) {
3490 if (abs(ofs_x - ev.xmotion.x) >= MOVE_THRESHOLD
3491 || abs(ofs_y - ev.xmotion.y) >= MOVE_THRESHOLD) {
3492 XChangeActivePointerGrab(dpy, ButtonMotionMask
3493 | ButtonReleaseMask | ButtonPressMask,
3494 wCursor[WCUR_MOVE], CurrentTime);
3495 grabbed = 1;
3496 } else {
3497 break;
3501 if (omnipresent) {
3502 int i;
3503 for (i = 0; i < scr->workspace_count; i++) {
3504 if (i == scr->current_workspace)
3505 continue;
3506 wDockShowIcons(scr->workspaces[i]->clip);
3510 x = ev.xmotion.x_root - ofs_x;
3511 y = ev.xmotion.y_root - ofs_y;
3512 tmp = wDockSnapIcon(dock, aicon, x, y, &ix, &iy, True);
3513 if (tmp && dock2) {
3514 change_dock = 0;
3515 if (last_dock != dock && collapsed) {
3516 last_dock->collapsed = 1;
3517 wDockHideIcons(last_dock);
3518 collapsed = 0;
3520 if (!collapsed && (collapsed = dock->collapsed)) {
3521 dock->collapsed = 0;
3522 wDockShowIcons(dock);
3524 if (dock->auto_raise_lower)
3525 wDockRaise(dock);
3526 last_dock = dock;
3527 } else if (dock2) {
3528 tmp = wDockSnapIcon(dock2, aicon, x, y, &ix, &iy, False);
3529 if (tmp) {
3530 change_dock = 1;
3531 if (last_dock != dock2 && collapsed) {
3532 last_dock->collapsed = 1;
3533 wDockHideIcons(last_dock);
3534 collapsed = 0;
3536 if (!collapsed && (collapsed = dock2->collapsed)) {
3537 dock2->collapsed = 0;
3538 wDockShowIcons(dock2);
3540 if (dock2->auto_raise_lower)
3541 wDockRaise(dock2);
3542 last_dock = dock2;
3545 if (aicon->launching || aicon->lock || (aicon->running && !(ev.xmotion.state & MOD_MASK))
3546 || (!aicon->running && tmp)) {
3547 shad_x = last_dock->x_pos + ix * wPreferences.icon_size;
3548 shad_y = last_dock->y_pos + iy * wPreferences.icon_size;
3550 XMoveWindow(dpy, scr->dock_shadow, shad_x, shad_y);
3552 if (!ondock) {
3553 XMapWindow(dpy, scr->dock_shadow);
3555 ondock = 1;
3556 } else {
3557 if (ondock) {
3558 XUnmapWindow(dpy, scr->dock_shadow);
3560 ondock = 0;
3562 XMoveWindow(dpy, icon->core->window, x, y);
3563 break;
3565 case ButtonPress:
3566 break;
3568 case ButtonRelease:
3569 if (ev.xbutton.button != event->xbutton.button)
3570 break;
3571 XUngrabPointer(dpy, CurrentTime);
3572 if (ondock) {
3573 SlideWindow(icon->core->window, x, y, shad_x, shad_y);
3574 XUnmapWindow(dpy, scr->dock_shadow);
3575 if (!change_dock) {
3576 reattachIcon(dock, aicon, ix, iy);
3577 if (clip && dock != clip && clip->auto_raise_lower)
3578 wDockLower(clip);
3579 } else {
3580 docked = moveIconBetweenDocks(dock, dock2, aicon, ix, iy);
3581 if (!docked) {
3582 /* Slide it back if dock rejected it */
3583 SlideWindow(icon->core->window, x, y, aicon->x_pos, aicon->y_pos);
3584 reattachIcon(dock, aicon, aicon->xindex, aicon->yindex);
3586 if (last_dock->type == WM_CLIP && last_dock->auto_collapse) {
3587 collapsed = 0;
3590 } else {
3591 aicon->x_pos = x;
3592 aicon->y_pos = y;
3593 if (superfluous) {
3594 if (!aicon->running && !wPreferences.no_animations) {
3595 /* We need to deselect it, even if is deselected in
3596 * wDockDetach(), because else DoKaboom() will fail.
3598 if (aicon->icon->selected)
3599 wIconSelect(aicon->icon);
3601 DoKaboom(scr, aicon->icon->core->window, x, y);
3604 if (clip && clip->auto_raise_lower)
3605 wDockLower(clip);
3606 wDockDetach(dock, aicon);
3608 if (collapsed) {
3609 last_dock->collapsed = 1;
3610 wDockHideIcons(last_dock);
3611 collapsed = 0;
3613 if (superfluous) {
3614 if (ghost != None)
3615 XFreePixmap(dpy, ghost);
3616 XSetWindowBackground(dpy, scr->dock_shadow, scr->white_pixel);
3618 if (omnipresent) {
3619 int i;
3620 for (i = 0; i < scr->workspace_count; i++) {
3621 if (i == scr->current_workspace)
3622 continue;
3623 wDockHideIcons(scr->workspaces[i]->clip);
3626 return hasMoved;;
3629 return False; /* never reached */
3632 static int getClipButton(int px, int py)
3634 int pt = (CLIP_BUTTON_SIZE + 2) * ICON_SIZE / 64;
3636 if (px < 0 || py < 0 || px >= ICON_SIZE || py >= ICON_SIZE)
3637 return CLIP_IDLE;
3639 if (py <= pt - ((int)ICON_SIZE - 1 - px))
3640 return CLIP_FORWARD;
3641 else if (px <= pt - ((int)ICON_SIZE - 1 - py))
3642 return CLIP_REWIND;
3644 return CLIP_IDLE;
3647 static void handleClipChangeWorkspace(WScreen *scr, XEvent *event)
3649 XEvent ev;
3650 int done, direction, new_ws;
3651 int new_dir;
3652 WDock *clip = scr->clip_icon->dock;
3654 direction = getClipButton(event->xbutton.x, event->xbutton.y);
3656 clip->lclip_button_pushed = direction == CLIP_REWIND;
3657 clip->rclip_button_pushed = direction == CLIP_FORWARD;
3659 wClipIconPaint(scr->clip_icon);
3660 done = 0;
3661 while (!done) {
3662 WMMaskEvent(dpy, ExposureMask | ButtonMotionMask | ButtonReleaseMask | ButtonPressMask, &ev);
3663 switch (ev.type) {
3664 case Expose:
3665 WMHandleEvent(&ev);
3666 break;
3668 case MotionNotify:
3669 new_dir = getClipButton(ev.xmotion.x, ev.xmotion.y);
3670 if (new_dir != direction) {
3671 direction = new_dir;
3672 clip->lclip_button_pushed = direction == CLIP_REWIND;
3673 clip->rclip_button_pushed = direction == CLIP_FORWARD;
3674 wClipIconPaint(scr->clip_icon);
3676 break;
3678 case ButtonPress:
3679 break;
3681 case ButtonRelease:
3682 if (ev.xbutton.button == event->xbutton.button)
3683 done = 1;
3687 clip->lclip_button_pushed = 0;
3688 clip->rclip_button_pushed = 0;
3690 new_ws = wPreferences.ws_advance || (event->xbutton.state & ControlMask);
3692 if (direction == CLIP_FORWARD) {
3693 if (scr->current_workspace < scr->workspace_count - 1)
3694 wWorkspaceChange(scr, scr->current_workspace + 1);
3695 else if (new_ws && scr->current_workspace < MAX_WORKSPACES - 1)
3696 wWorkspaceChange(scr, scr->current_workspace + 1);
3697 else if (wPreferences.ws_cycle)
3698 wWorkspaceChange(scr, 0);
3699 } else if (direction == CLIP_REWIND) {
3700 if (scr->current_workspace > 0)
3701 wWorkspaceChange(scr, scr->current_workspace - 1);
3702 else if (scr->current_workspace == 0 && wPreferences.ws_cycle)
3703 wWorkspaceChange(scr, scr->workspace_count - 1);
3706 wClipIconPaint(scr->clip_icon);
3709 static void iconMouseDown(WObjDescriptor *desc, XEvent *event)
3711 WAppIcon *aicon = desc->parent;
3712 WDock *dock = aicon->dock;
3713 WScreen *scr = aicon->icon->core->screen_ptr;
3715 if (aicon->editing || WCHECK_STATE(WSTATE_MODAL))
3716 return;
3718 scr->last_dock = dock;
3720 if (dock->menu->flags.mapped)
3721 wMenuUnmap(dock->menu);
3723 if (IsDoubleClick(scr, event)) {
3724 /* double-click was not in the main clip icon */
3725 if (dock->type != WM_CLIP || aicon->xindex != 0 || aicon->yindex != 0
3726 || getClipButton(event->xbutton.x, event->xbutton.y) == CLIP_IDLE) {
3727 iconDblClick(desc, event);
3728 return;
3732 if (event->xbutton.button == Button1) {
3733 if (event->xbutton.state & MOD_MASK)
3734 wDockLower(dock);
3735 else
3736 wDockRaise(dock);
3738 if ((event->xbutton.state & ShiftMask) && aicon != scr->clip_icon && dock->type != WM_DOCK) {
3739 wIconSelect(aicon->icon);
3740 return;
3743 if (aicon->yindex == 0 && aicon->xindex == 0) {
3744 if (getClipButton(event->xbutton.x, event->xbutton.y) != CLIP_IDLE
3745 && dock->type == WM_CLIP)
3746 handleClipChangeWorkspace(scr, event);
3747 else
3748 handleDockMove(dock, aicon, event);
3749 } else {
3750 Bool hasMoved = handleIconMove(dock, aicon, event);
3751 if (wPreferences.single_click && !hasMoved)
3752 iconDblClick(desc, event);
3754 } else if (event->xbutton.button == Button2 && dock->type == WM_CLIP && aicon == scr->clip_icon) {
3755 if (!scr->clip_ws_menu) {
3756 scr->clip_ws_menu = wWorkspaceMenuMake(scr, False);
3758 if (scr->clip_ws_menu) {
3759 WMenu *wsMenu = scr->clip_ws_menu;
3760 int xpos;
3762 wWorkspaceMenuUpdate(scr, wsMenu);
3764 xpos = event->xbutton.x_root - wsMenu->frame->core->width / 2 - 1;
3765 if (xpos < 0) {
3766 xpos = 0;
3767 } else if (xpos + wsMenu->frame->core->width > scr->scr_width - 2) {
3768 xpos = scr->scr_width - wsMenu->frame->core->width - 4;
3770 wMenuMapAt(wsMenu, xpos, event->xbutton.y_root + 2, False);
3772 desc = &wsMenu->menu->descriptor;
3773 event->xany.send_event = True;
3774 (*desc->handle_mousedown) (desc, event);
3776 } else if (event->xbutton.button == Button2 && dock->type == WM_CLIP &&
3777 (event->xbutton.state & ShiftMask) && aicon != scr->clip_icon) {
3778 wClipMakeIconOmnipresent(aicon, !aicon->omnipresent);
3779 } else if (event->xbutton.button == Button3) {
3780 if (event->xbutton.send_event &&
3781 XGrabPointer(dpy, aicon->icon->core->window, True, ButtonMotionMask
3782 | ButtonReleaseMask | ButtonPressMask, GrabModeAsync,
3783 GrabModeAsync, None, None, CurrentTime) != GrabSuccess) {
3784 wwarning("pointer grab failed for dockicon menu");
3785 return;
3788 openDockMenu(dock, aicon, event);
3789 } else if (event->xbutton.button == Button2) {
3790 WAppIcon *btn = desc->parent;
3792 if (!btn->launching && (!btn->running || (event->xbutton.state & ControlMask))) {
3793 launchDockedApplication(btn, True);
3798 static void clipEnterNotify(WObjDescriptor *desc, XEvent *event)
3800 WAppIcon *btn = (WAppIcon *) desc->parent;
3801 WDock *dock;
3802 WScreen *scr;
3804 assert(event->type == EnterNotify);
3806 if (desc->parent_type != WCLASS_DOCK_ICON)
3807 return;
3809 scr = btn->icon->core->screen_ptr;
3810 if (!btn->omnipresent)
3811 dock = btn->dock;
3812 else
3813 dock = scr->workspaces[scr->current_workspace]->clip;
3815 if (!dock || dock->type != WM_CLIP)
3816 return;
3818 /* The auto raise/lower code */
3819 if (dock->auto_lower_magic) {
3820 WMDeleteTimerHandler(dock->auto_lower_magic);
3821 dock->auto_lower_magic = NULL;
3823 if (dock->auto_raise_lower && !dock->auto_raise_magic) {
3824 dock->auto_raise_magic = WMAddTimerHandler(AUTO_RAISE_DELAY, clipAutoRaise, (void *)dock);
3827 /* The auto expand/collapse code */
3828 if (dock->auto_collapse_magic) {
3829 WMDeleteTimerHandler(dock->auto_collapse_magic);
3830 dock->auto_collapse_magic = NULL;
3832 if (dock->auto_collapse && !dock->auto_expand_magic) {
3833 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);
3869 if (dock->auto_expand_magic) {
3870 WMDeleteTimerHandler(dock->auto_expand_magic);
3871 dock->auto_expand_magic = NULL;
3873 if (dock->auto_collapse && !dock->auto_collapse_magic) {
3874 dock->auto_collapse_magic = WMAddTimerHandler(AUTO_COLLAPSE_DELAY, clipAutoCollapse, (void *)dock);
3878 static void clipLeaveNotify(WObjDescriptor *desc, XEvent *event)
3880 WAppIcon *btn = (WAppIcon *) desc->parent;
3882 assert(event->type == LeaveNotify);
3884 if (desc->parent_type != WCLASS_DOCK_ICON)
3885 return;
3887 clipLeave(btn->dock);
3890 static void clipAutoCollapse(void *cdata)
3892 WDock *dock = (WDock *) cdata;
3894 if (dock->type != WM_CLIP)
3895 return;
3897 if (dock->auto_collapse) {
3898 dock->collapsed = 1;
3899 wDockHideIcons(dock);
3901 dock->auto_collapse_magic = NULL;
3904 static void clipAutoExpand(void *cdata)
3906 WDock *dock = (WDock *) cdata;
3908 if (dock->type != WM_CLIP)
3909 return;
3911 if (dock->auto_collapse) {
3912 dock->collapsed = 0;
3913 wDockShowIcons(dock);
3915 dock->auto_expand_magic = NULL;
3918 static void clipAutoLower(void *cdata)
3920 WDock *dock = (WDock *) cdata;
3922 if (dock->type != WM_CLIP)
3923 return;
3925 if (dock->auto_raise_lower)
3926 wDockLower(dock);
3928 dock->auto_lower_magic = NULL;
3931 static void clipAutoRaise(void *cdata)
3933 WDock *dock = (WDock *) cdata;
3935 if (dock->type != WM_CLIP)
3936 return;
3938 if (dock->auto_raise_lower)
3939 wDockRaise(dock);
3941 dock->auto_raise_magic = NULL;
3944 static Bool iconCanBeOmnipresent(WAppIcon *aicon)
3946 WScreen *scr = aicon->icon->core->screen_ptr;
3947 WDock *clip;
3948 WAppIcon *btn;
3949 int i, j;
3951 for (i = 0; i < scr->workspace_count; i++) {
3952 clip = scr->workspaces[i]->clip;
3954 if (clip == aicon->dock)
3955 continue;
3957 if (clip->icon_count + scr->global_icon_count >= clip->max_icons)
3958 return False; /* Clip is full in some workspace */
3960 for (j = 0; j < clip->max_icons; j++) {
3961 btn = clip->icon_array[j];
3962 if (btn && btn->xindex == aicon->xindex && btn->yindex == aicon->yindex)
3963 return False;
3967 return True;
3970 int wClipMakeIconOmnipresent(WAppIcon *aicon, int omnipresent)
3972 WScreen *scr = aicon->icon->core->screen_ptr;
3973 WAppIconChain *new_entry, *tmp, *tmp1;
3974 int status = WO_SUCCESS;
3976 if ((scr->dock && aicon->dock == scr->dock) || aicon == scr->clip_icon) {
3977 return WO_NOT_APPLICABLE;
3980 if (aicon->omnipresent == omnipresent)
3981 return WO_SUCCESS;
3983 if (omnipresent) {
3984 if (iconCanBeOmnipresent(aicon)) {
3985 aicon->omnipresent = 1;
3986 new_entry = wmalloc(sizeof(WAppIconChain));
3987 new_entry->aicon = aicon;
3988 new_entry->next = scr->global_icons;
3989 scr->global_icons = new_entry;
3990 scr->global_icon_count++;
3991 } else {
3992 aicon->omnipresent = 0;
3993 status = WO_FAILED;
3995 } else {
3996 aicon->omnipresent = 0;
3997 if (aicon == scr->global_icons->aicon) {
3998 tmp = scr->global_icons->next;
3999 wfree(scr->global_icons);
4000 scr->global_icons = tmp;
4001 scr->global_icon_count--;
4002 } else {
4003 tmp = scr->global_icons;
4004 while (tmp->next) {
4005 if (tmp->next->aicon == aicon) {
4006 tmp1 = tmp->next->next;
4007 wfree(tmp->next);
4008 tmp->next = tmp1;
4009 scr->global_icon_count--;
4010 break;
4012 tmp = tmp->next;
4017 wAppIconPaint(aicon);
4019 return status;