dock: Make some functions static
[wmaker-crm.git] / src / dock.c
blob8a1a21f1da8c4929b1c69161def519c1c08b853c
1 /* dock.c- built-in Dock module for WindowMaker
3 * Window Maker window manager
5 * Copyright (c) 1997-2003 Alfredo K. Kojima
6 * Copyright (c) 1998-2003 Dan Pascu
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
23 #include "wconfig.h"
25 #include <X11/Xlib.h>
26 #include <X11/Xutil.h>
27 #include <stdlib.h>
28 #include <string.h>
29 #include <strings.h>
30 #include <unistd.h>
31 #include <math.h>
32 #include <limits.h>
34 #ifndef PATH_MAX
35 #define PATH_MAX DEFAULT_PATH_MAX
36 #endif
38 #include "WindowMaker.h"
39 #include "wcore.h"
40 #include "window.h"
41 #include "icon.h"
42 #include "appicon.h"
43 #include "actions.h"
44 #include "stacking.h"
45 #include "dock.h"
46 #include "dialog.h"
47 #include "funcs.h"
48 #include "properties.h"
49 #include "menu.h"
50 #include "client.h"
51 #include "defaults.h"
52 #include "workspace.h"
53 #include "framewin.h"
54 #include "superfluous.h"
55 #include "xinerama.h"
57 /**** Local variables ****/
58 #define CLIP_REWIND 1
59 #define CLIP_IDLE 0
60 #define CLIP_FORWARD 2
62 /**** Global variables ****/
64 /* in dockedapp.c */
65 extern void DestroyDockAppSettingsPanel();
66 extern void ShowDockAppSettingsPanel(WAppIcon * aicon);
67 extern Cursor wCursor[WCUR_LAST];
68 extern WPreferences wPreferences;
69 extern XContext wWinContext;
70 extern void appIconMouseDown(WObjDescriptor *desc, XEvent *event);
72 #define MOD_MASK wPreferences.modifier_mask
73 #define ICON_SIZE wPreferences.icon_size
75 /***** Local variables ****/
77 static WMPropList *dCommand = NULL;
78 static WMPropList *dPasteCommand = NULL;
79 #ifdef XDND /* XXX was OFFIX */
80 static WMPropList *dDropCommand = NULL;
81 #endif
82 static WMPropList *dAutoLaunch, *dLock;
83 static WMPropList *dName, *dForced, *dBuggyApplication, *dYes, *dNo;
84 static WMPropList *dHost, *dDock, *dClip;
85 static WMPropList *dAutoAttractIcons;
87 static WMPropList *dPosition, *dApplications, *dLowered, *dCollapsed;
89 static WMPropList *dAutoCollapse, *dAutoRaiseLower, *dOmnipresent;
91 static void dockIconPaint(WAppIcon *btn);
93 static void iconMouseDown(WObjDescriptor *desc, XEvent *event);
95 static pid_t execCommand(WAppIcon *btn, char *command, WSavedState *state);
97 static void trackDeadProcess(pid_t pid, unsigned char status, WDock *dock);
99 static int getClipButton(int px, int py);
101 static void toggleLowered(WDock *dock);
103 static void toggleCollapsed(WDock *dock);
105 static void clipIconExpose(WObjDescriptor *desc, XEvent *event);
107 static void clipLeave(WDock *dock);
109 static void handleClipChangeWorkspace(WScreen *scr, XEvent *event);
111 static Bool moveIconBetweenDocks(WDock *src, WDock *dest, WAppIcon *icon, int x, int y);
113 static void clipEnterNotify(WObjDescriptor *desc, XEvent *event);
114 static void clipLeaveNotify(WObjDescriptor *desc, XEvent *event);
115 static void clipAutoCollapse(void *cdata);
116 static void clipAutoExpand(void *cdata);
117 static void launchDockedApplication(WAppIcon *btn, Bool withSelection);
119 static void clipAutoLower(void *cdata);
120 static void clipAutoRaise(void *cdata);
121 static void reattachIcon(WDock *dock, WAppIcon *icon, int x, int y);
122 static WAppIcon *mainIconCreate(WScreen *scr, int type);
124 static void make_keys(void)
126 if (dCommand != NULL)
127 return;
129 dCommand = WMRetainPropList(WMCreatePLString("Command"));
130 dPasteCommand = WMRetainPropList(WMCreatePLString("PasteCommand"));
131 #ifdef XDND
132 dDropCommand = WMRetainPropList(WMCreatePLString("DropCommand"));
133 #endif
134 dLock = WMRetainPropList(WMCreatePLString("Lock"));
135 dAutoLaunch = WMRetainPropList(WMCreatePLString("AutoLaunch"));
136 dName = WMRetainPropList(WMCreatePLString("Name"));
137 dForced = WMRetainPropList(WMCreatePLString("Forced"));
138 dBuggyApplication = WMRetainPropList(WMCreatePLString("BuggyApplication"));
139 dYes = WMRetainPropList(WMCreatePLString("Yes"));
140 dNo = WMRetainPropList(WMCreatePLString("No"));
141 dHost = WMRetainPropList(WMCreatePLString("Host"));
143 dPosition = WMCreatePLString("Position");
144 dApplications = WMCreatePLString("Applications");
145 dLowered = WMCreatePLString("Lowered");
146 dCollapsed = WMCreatePLString("Collapsed");
147 dAutoCollapse = WMCreatePLString("AutoCollapse");
148 dAutoRaiseLower = WMCreatePLString("AutoRaiseLower");
149 dAutoAttractIcons = WMCreatePLString("AutoAttractIcons");
151 dOmnipresent = WMCreatePLString("Omnipresent");
153 dDock = WMCreatePLString("Dock");
154 dClip = WMCreatePLString("Clip");
157 static void renameCallback(WMenu *menu, WMenuEntry *entry)
159 WDock *dock = entry->clientdata;
160 char buffer[128];
161 int wspace;
162 char *name;
164 assert(entry->clientdata != NULL);
166 wspace = dock->screen_ptr->current_workspace;
168 name = wstrdup(dock->screen_ptr->workspaces[wspace]->name);
170 snprintf(buffer, sizeof(buffer), _("Type the name for workspace %i:"), wspace + 1);
171 if (wInputDialog(dock->screen_ptr, _("Rename Workspace"), buffer, &name)) {
172 wWorkspaceRename(dock->screen_ptr, wspace, name);
174 if (name) {
175 wfree(name);
179 static void toggleLoweredCallback(WMenu *menu, WMenuEntry *entry)
181 assert(entry->clientdata != NULL);
183 toggleLowered(entry->clientdata);
185 entry->flags.indicator_on = !(((WDock *) entry->clientdata)->lowered);
187 wMenuPaint(menu);
190 static int matchWindow(const void *item, const void *cdata)
192 return (((WFakeGroupLeader *) item)->leader == (Window) cdata);
195 static void killCallback(WMenu *menu, WMenuEntry *entry)
197 WScreen *scr = menu->menu->screen_ptr;
198 WAppIcon *icon;
199 WFakeGroupLeader *fPtr;
200 char *buffer, *shortname, **argv;
201 char *basename(const char *shortname);
202 int argc;
204 if (!WCHECK_STATE(WSTATE_NORMAL))
205 return;
207 assert(entry->clientdata != NULL);
209 icon = (WAppIcon *) entry->clientdata;
211 icon->editing = 1;
213 WCHANGE_STATE(WSTATE_MODAL);
215 /* strip away dir names */
216 shortname = basename(icon->command);
217 /* separate out command options */
218 wtokensplit(shortname, &argv, &argc);
220 buffer = wstrconcat(argv[0],
221 _(" will be forcibly closed.\n"
222 "Any unsaved changes will be lost.\n" "Please confirm."));
224 if (icon->icon && icon->icon->owner) {
225 fPtr = icon->icon->owner->fake_group;
226 } else {
227 /* is this really necessary? can we kill a non-running dock icon? */
228 Window win = icon->main_window;
229 int index;
231 index = WMFindInArray(scr->fakeGroupLeaders, matchWindow, (void *)win);
232 if (index != WANotFound)
233 fPtr = WMGetFromArray(scr->fakeGroupLeaders, index);
234 else
235 fPtr = NULL;
238 if (wPreferences.dont_confirm_kill
239 || wMessageDialog(menu->frame->screen_ptr, _("Kill Application"),
240 buffer, _("Yes"), _("No"), NULL) == WAPRDefault) {
241 if (fPtr != NULL) {
242 WWindow *wwin, *twin;
244 wwin = scr->focused_window;
245 while (wwin) {
246 twin = wwin->prev;
247 if (wwin->fake_group == fPtr) {
248 wClientKill(wwin);
250 wwin = twin;
252 } else if (icon->icon && icon->icon->owner) {
253 wClientKill(icon->icon->owner);
257 wfree(buffer);
258 wtokenfree(argv, argc);
260 icon->editing = 0;
262 WCHANGE_STATE(WSTATE_NORMAL);
265 /* TODO: replace this function with a member of the dock struct */
266 static int numberOfSelectedIcons(WDock *dock)
268 WAppIcon *aicon;
269 int i, n;
271 n = 0;
272 for (i = 1; i < dock->max_icons; i++) {
273 aicon = dock->icon_array[i];
274 if (aicon && aicon->icon->selected) {
275 n++;
279 return n;
282 static WMArray *getSelected(WDock *dock)
284 WMArray *ret = WMCreateArray(8);
285 WAppIcon *btn;
286 int i;
288 for (i = 1; i < dock->max_icons; i++) {
289 btn = dock->icon_array[i];
290 if (btn && btn->icon->selected) {
291 WMAddToArray(ret, btn);
295 return ret;
298 static void paintClipButtons(WAppIcon *clipIcon, Bool lpushed, Bool rpushed)
300 Window win = clipIcon->icon->core->window;
301 WScreen *scr = clipIcon->icon->core->screen_ptr;
302 XPoint p[4];
303 int pt = CLIP_BUTTON_SIZE * ICON_SIZE / 64;
304 int tp = ICON_SIZE - pt;
305 int as = pt - 15; /* 15 = 5+5+5 */
306 GC gc = scr->draw_gc; /* maybe use WMColorGC() instead here? */
307 WMColor *color;
309 color = scr->clip_title_color[CLIP_NORMAL];
311 XSetForeground(dpy, gc, WMColorPixel(color));
313 if (rpushed) {
314 p[0].x = tp + 1;
315 p[0].y = 1;
316 p[1].x = ICON_SIZE - 2;
317 p[1].y = 1;
318 p[2].x = ICON_SIZE - 2;
319 p[2].y = pt - 1;
320 } else if (lpushed) {
321 p[0].x = 1;
322 p[0].y = tp;
323 p[1].x = pt;
324 p[1].y = ICON_SIZE - 2;
325 p[2].x = 1;
326 p[2].y = ICON_SIZE - 2;
328 if (lpushed || rpushed) {
329 XSetForeground(dpy, scr->draw_gc, scr->white_pixel);
330 XFillPolygon(dpy, win, scr->draw_gc, p, 3, Convex, CoordModeOrigin);
331 XSetForeground(dpy, scr->draw_gc, scr->black_pixel);
334 /* top right arrow */
335 p[0].x = p[3].x = ICON_SIZE - 5 - as;
336 p[0].y = p[3].y = 5;
337 p[1].x = ICON_SIZE - 6;
338 p[1].y = 5;
339 p[2].x = ICON_SIZE - 6;
340 p[2].y = 4 + as;
341 if (rpushed) {
342 XFillPolygon(dpy, win, scr->draw_gc, p, 3, Convex, CoordModeOrigin);
343 XDrawLines(dpy, win, scr->draw_gc, p, 4, CoordModeOrigin);
344 } else {
345 XFillPolygon(dpy, win, gc, p, 3, Convex, CoordModeOrigin);
346 XDrawLines(dpy, win, gc, p, 4, CoordModeOrigin);
349 /* bottom left arrow */
350 p[0].x = p[3].x = 5;
351 p[0].y = p[3].y = ICON_SIZE - 5 - as;
352 p[1].x = 5;
353 p[1].y = ICON_SIZE - 6;
354 p[2].x = 4 + as;
355 p[2].y = ICON_SIZE - 6;
356 if (lpushed) {
357 XFillPolygon(dpy, win, scr->draw_gc, p, 3, Convex, CoordModeOrigin);
358 XDrawLines(dpy, win, scr->draw_gc, p, 4, CoordModeOrigin);
359 } else {
360 XFillPolygon(dpy, win, gc, p, 3, Convex, CoordModeOrigin);
361 XDrawLines(dpy, win, gc, p, 4, CoordModeOrigin);
365 RImage *wClipMakeTile(WScreen *scr, RImage *normalTile)
367 RImage *tile = RCloneImage(normalTile);
368 RColor black;
369 RColor dark;
370 RColor light;
371 int pt, tp;
372 int as;
374 pt = CLIP_BUTTON_SIZE * wPreferences.icon_size / 64;
375 tp = wPreferences.icon_size - 1 - pt;
376 as = pt - 15;
378 black.alpha = 255;
379 black.red = black.green = black.blue = 0;
381 dark.alpha = 0;
382 dark.red = dark.green = dark.blue = 60;
384 light.alpha = 0;
385 light.red = light.green = light.blue = 80;
387 /* top right */
388 ROperateLine(tile, RSubtractOperation, tp, 0, wPreferences.icon_size - 2, pt - 1, &dark);
389 RDrawLine(tile, tp - 1, 0, wPreferences.icon_size - 1, pt + 1, &black);
390 ROperateLine(tile, RAddOperation, tp, 2, wPreferences.icon_size - 3, pt, &light);
392 /* arrow bevel */
393 ROperateLine(tile, RSubtractOperation, ICON_SIZE - 7 - as, 4, ICON_SIZE - 5, 4, &dark);
394 ROperateLine(tile, RSubtractOperation, ICON_SIZE - 6 - as, 5, ICON_SIZE - 5, 6 + as, &dark);
395 ROperateLine(tile, RAddOperation, ICON_SIZE - 5, 4, ICON_SIZE - 5, 6 + as, &light);
397 /* bottom left */
398 ROperateLine(tile, RAddOperation, 2, tp + 2, pt - 2, wPreferences.icon_size - 3, &dark);
399 RDrawLine(tile, 0, tp - 1, pt + 1, wPreferences.icon_size - 1, &black);
400 ROperateLine(tile, RSubtractOperation, 0, tp - 2, pt + 1, wPreferences.icon_size - 2, &light);
402 /* arrow bevel */
403 ROperateLine(tile, RSubtractOperation, 4, ICON_SIZE - 7 - as, 4, ICON_SIZE - 5, &dark);
404 ROperateLine(tile, RSubtractOperation, 5, ICON_SIZE - 6 - as, 6 + as, ICON_SIZE - 5, &dark);
405 ROperateLine(tile, RAddOperation, 4, ICON_SIZE - 5, 6 + as, ICON_SIZE - 5, &light);
407 return tile;
410 static void omnipresentCallback(WMenu *menu, WMenuEntry *entry)
412 WAppIcon *clickedIcon = entry->clientdata;
413 WAppIcon *aicon;
414 WDock *dock;
415 WMArray *selectedIcons;
416 WMArrayIterator iter;
417 int failed;
419 assert(entry->clientdata != NULL);
421 dock = clickedIcon->dock;
423 selectedIcons = getSelected(dock);
425 if (!WMGetArrayItemCount(selectedIcons))
426 WMAddToArray(selectedIcons, clickedIcon);
428 failed = 0;
429 WM_ITERATE_ARRAY(selectedIcons, aicon, iter) {
430 if (wClipMakeIconOmnipresent(aicon, !aicon->omnipresent) == WO_FAILED)
431 failed++;
432 else if (aicon->icon->selected)
433 wIconSelect(aicon->icon);
435 WMFreeArray(selectedIcons);
437 if (failed > 1) {
438 wMessageDialog(dock->screen_ptr, _("Warning"),
439 _("Some icons cannot be made omnipresent. "
440 "Please make sure that no other icon is "
441 "docked in the same positions on the other "
442 "workspaces and the Clip is not full in "
443 "some workspace."), _("OK"), NULL, NULL);
444 } else if (failed == 1) {
445 wMessageDialog(dock->screen_ptr, _("Warning"),
446 _("Icon cannot be made omnipresent. "
447 "Please make sure that no other icon is "
448 "docked in the same position on the other "
449 "workspaces and the Clip is not full in "
450 "some workspace."), _("OK"), NULL, NULL);
454 static void removeIconsCallback(WMenu *menu, WMenuEntry *entry)
456 WAppIcon *clickedIcon = (WAppIcon *) entry->clientdata;
457 WDock *dock;
458 WAppIcon *aicon;
459 WMArray *selectedIcons;
460 int keepit;
461 WMArrayIterator it;
463 assert(clickedIcon != NULL);
465 dock = clickedIcon->dock;
467 selectedIcons = getSelected(dock);
469 if (WMGetArrayItemCount(selectedIcons)) {
470 if (wMessageDialog(dock->screen_ptr, _("Workspace Clip"),
471 _("All selected icons will be removed!"),
472 _("OK"), _("Cancel"), NULL) != WAPRDefault) {
473 WMFreeArray(selectedIcons);
474 return;
476 } else {
477 if (clickedIcon->xindex == 0 && clickedIcon->yindex == 0) {
478 WMFreeArray(selectedIcons);
479 return;
481 WMAddToArray(selectedIcons, clickedIcon);
484 WM_ITERATE_ARRAY(selectedIcons, aicon, it) {
485 keepit = aicon->running && wApplicationOf(aicon->main_window);
486 wDockDetach(dock, aicon);
487 if (keepit) {
488 /* XXX: can: aicon->icon == NULL ? */
489 PlaceIcon(dock->screen_ptr, &aicon->x_pos, &aicon->y_pos,
490 wGetHeadForWindow(aicon->icon->owner));
491 XMoveWindow(dpy, aicon->icon->core->window, aicon->x_pos, aicon->y_pos);
492 if (!dock->mapped || dock->collapsed)
493 XMapWindow(dpy, aicon->icon->core->window);
496 WMFreeArray(selectedIcons);
498 if (wPreferences.auto_arrange_icons)
499 wArrangeIcons(dock->screen_ptr, True);
502 static void keepIconsCallback(WMenu *menu, WMenuEntry *entry)
504 WAppIcon *clickedIcon = (WAppIcon *) entry->clientdata;
505 WDock *dock;
506 WAppIcon *aicon;
507 WMArray *selectedIcons;
508 WMArrayIterator it;
510 assert(clickedIcon != NULL);
511 dock = clickedIcon->dock;
513 selectedIcons = getSelected(dock);
515 if (!WMGetArrayItemCount(selectedIcons)
516 && clickedIcon != dock->screen_ptr->clip_icon) {
517 char *command = NULL;
519 if (!clickedIcon->command && !clickedIcon->editing) {
520 clickedIcon->editing = 1;
521 if (wInputDialog(dock->screen_ptr, _("Keep Icon"),
522 _("Type the command used to launch the application"), &command)) {
523 if (command && (command[0] == 0 || (command[0] == '-' && command[1] == 0))) {
524 wfree(command);
525 command = NULL;
527 clickedIcon->command = command;
528 clickedIcon->editing = 0;
529 } else {
530 clickedIcon->editing = 0;
531 if (command)
532 wfree(command);
533 WMFreeArray(selectedIcons);
534 return;
538 WMAddToArray(selectedIcons, clickedIcon);
541 WM_ITERATE_ARRAY(selectedIcons, aicon, it) {
542 if (aicon->icon->selected)
543 wIconSelect(aicon->icon);
544 if (aicon && aicon->attracted && aicon->command) {
545 aicon->attracted = 0;
546 if (aicon->icon->shadowed) {
547 aicon->icon->shadowed = 0;
548 aicon->icon->force_paint = 1;
549 wAppIconPaint(aicon);
552 wAppIconSave(aicon);
554 WMFreeArray(selectedIcons);
557 static void toggleAutoAttractCallback(WMenu *menu, WMenuEntry *entry)
559 WDock *dock = (WDock *) entry->clientdata;
561 assert(entry->clientdata != NULL);
563 dock->attract_icons = !dock->attract_icons;
564 /*if (!dock->attract_icons)
565 dock->keep_attracted = 0; */
567 entry->flags.indicator_on = dock->attract_icons;
569 wMenuPaint(menu);
572 static void selectCallback(WMenu *menu, WMenuEntry *entry)
574 WAppIcon *icon = (WAppIcon *) entry->clientdata;
576 assert(icon != NULL);
578 wIconSelect(icon->icon);
580 wMenuPaint(menu);
583 static void colectIconsCallback(WMenu *menu, WMenuEntry *entry)
585 WAppIcon *clickedIcon = (WAppIcon *) entry->clientdata;
586 WDock *clip;
587 WAppIcon *aicon;
588 int x, y, x_pos, y_pos;
590 assert(entry->clientdata != NULL);
591 clip = clickedIcon->dock;
593 aicon = clip->screen_ptr->app_icon_list;
595 while (aicon) {
596 if (!aicon->docked && wDockFindFreeSlot(clip, &x, &y)) {
597 x_pos = clip->x_pos + x * ICON_SIZE;
598 y_pos = clip->y_pos + y * ICON_SIZE;
599 if (aicon->x_pos != x_pos || aicon->y_pos != y_pos) {
600 #ifdef ANIMATIONS
601 if (wPreferences.no_animations) {
602 XMoveWindow(dpy, aicon->icon->core->window, x_pos, y_pos);
603 } else {
604 SlideWindow(aicon->icon->core->window,
605 aicon->x_pos, aicon->y_pos, x_pos, y_pos);
607 #else
608 XMoveWindow(dpy, aicon->icon->core->window, x_pos, y_pos);
609 #endif /* ANIMATIONS */
611 aicon->attracted = 1;
612 if (!aicon->icon->shadowed) {
613 aicon->icon->shadowed = 1;
614 aicon->icon->force_paint = 1;
615 /* We don't do an wAppIconPaint() here because it's in
616 * wDockAttachIcon(). -Dan
619 wDockAttachIcon(clip, aicon, x, y);
620 if (clip->collapsed || !clip->mapped)
621 XUnmapWindow(dpy, aicon->icon->core->window);
623 aicon = aicon->next;
627 static void selectIconsCallback(WMenu *menu, WMenuEntry *entry)
629 WAppIcon *clickedIcon = (WAppIcon *) entry->clientdata;
630 WDock *dock;
631 WMArray *selectedIcons;
632 WMArrayIterator iter;
633 WAppIcon *btn;
634 int i;
636 assert(clickedIcon != NULL);
637 dock = clickedIcon->dock;
639 selectedIcons = getSelected(dock);
641 if (!WMGetArrayItemCount(selectedIcons)) {
642 for (i = 1; i < dock->max_icons; i++) {
643 btn = dock->icon_array[i];
644 if (btn && !btn->icon->selected) {
645 wIconSelect(btn->icon);
648 } else {
649 WM_ITERATE_ARRAY(selectedIcons, btn, iter) {
650 wIconSelect(btn->icon);
653 WMFreeArray(selectedIcons);
655 wMenuPaint(menu);
658 static void toggleCollapsedCallback(WMenu *menu, WMenuEntry *entry)
660 assert(entry->clientdata != NULL);
662 toggleCollapsed(entry->clientdata);
664 entry->flags.indicator_on = ((WDock *) entry->clientdata)->collapsed;
666 wMenuPaint(menu);
669 static void toggleAutoCollapseCallback(WMenu *menu, WMenuEntry *entry)
671 WDock *dock;
672 assert(entry->clientdata != NULL);
674 dock = (WDock *) entry->clientdata;
676 dock->auto_collapse = !dock->auto_collapse;
678 entry->flags.indicator_on = ((WDock *) entry->clientdata)->auto_collapse;
680 wMenuPaint(menu);
683 static void toggleAutoRaiseLowerCallback(WMenu *menu, WMenuEntry *entry)
685 WDock *dock;
686 assert(entry->clientdata != NULL);
688 dock = (WDock *) entry->clientdata;
690 dock->auto_raise_lower = !dock->auto_raise_lower;
692 entry->flags.indicator_on = ((WDock *) entry->clientdata)->auto_raise_lower;
694 wMenuPaint(menu);
697 static void launchCallback(WMenu *menu, WMenuEntry *entry)
699 WAppIcon *btn = (WAppIcon *) entry->clientdata;
701 launchDockedApplication(btn, False);
704 static void settingsCallback(WMenu *menu, WMenuEntry *entry)
706 WAppIcon *btn = (WAppIcon *) entry->clientdata;
708 if (btn->editing)
709 return;
710 ShowDockAppSettingsPanel(btn);
713 static void hideCallback(WMenu *menu, WMenuEntry *entry)
715 WApplication *wapp;
716 WAppIcon *btn = (WAppIcon *) entry->clientdata;
718 wapp = wApplicationOf(btn->icon->owner->main_window);
720 if (wapp->flags.hidden) {
721 wWorkspaceChange(btn->icon->core->screen_ptr, wapp->last_workspace);
722 wUnhideApplication(wapp, False, False);
723 } else {
724 wHideApplication(wapp);
728 static void unhideHereCallback(WMenu *menu, WMenuEntry *entry)
730 WApplication *wapp;
731 WAppIcon *btn = (WAppIcon *) entry->clientdata;
733 wapp = wApplicationOf(btn->icon->owner->main_window);
735 wUnhideApplication(wapp, False, True);
738 static WAppIcon *mainIconCreate(WScreen *scr, int type)
740 WAppIcon *btn;
741 int x_pos;
743 if (type == WM_CLIP) {
744 if (scr->clip_icon)
745 return scr->clip_icon;
746 btn = wAppIconCreateForDock(scr, NULL, "Logo", "WMClip", TILE_CLIP);
747 btn->icon->core->descriptor.handle_expose = clipIconExpose;
748 btn->icon->core->descriptor.handle_enternotify = clipEnterNotify;
749 btn->icon->core->descriptor.handle_leavenotify = clipLeaveNotify;
750 /*x_pos = scr->scr_width - ICON_SIZE*2 - DOCK_EXTRA_SPACE; */
751 x_pos = 0;
752 } else {
753 btn = wAppIconCreateForDock(scr, NULL, "Logo", "WMDock", TILE_NORMAL);
754 x_pos = scr->scr_width - ICON_SIZE - DOCK_EXTRA_SPACE;
757 btn->xindex = 0;
758 btn->yindex = 0;
760 btn->icon->core->descriptor.handle_mousedown = iconMouseDown;
761 btn->icon->core->descriptor.parent_type = WCLASS_DOCK_ICON;
762 btn->icon->core->descriptor.parent = btn;
763 /*ChangeStackingLevel(btn->icon->core, WMDockLevel); */
764 XMapWindow(dpy, btn->icon->core->window);
765 btn->x_pos = x_pos;
766 btn->y_pos = 0;
767 btn->docked = 1;
768 if (type == WM_CLIP)
769 scr->clip_icon = btn;
771 return btn;
774 static void switchWSCommand(WMenu *menu, WMenuEntry *entry)
776 WAppIcon *btn, *icon = (WAppIcon *) entry->clientdata;
777 WScreen *scr = icon->icon->core->screen_ptr;
778 WDock *src, *dest;
779 WMArray *selectedIcons;
780 int x, y;
782 if (entry->order == scr->current_workspace)
783 return;
784 src = icon->dock;
785 dest = scr->workspaces[entry->order]->clip;
787 selectedIcons = getSelected(src);
789 if (WMGetArrayItemCount(selectedIcons)) {
790 WMArrayIterator iter;
792 WM_ITERATE_ARRAY(selectedIcons, btn, iter) {
793 if (wDockFindFreeSlot(dest, &x, &y)) {
794 moveIconBetweenDocks(src, dest, btn, x, y);
795 XUnmapWindow(dpy, btn->icon->core->window);
798 } else if (icon != scr->clip_icon) {
799 if (wDockFindFreeSlot(dest, &x, &y)) {
800 moveIconBetweenDocks(src, dest, icon, x, y);
801 XUnmapWindow(dpy, icon->icon->core->window);
804 WMFreeArray(selectedIcons);
807 static void launchDockedApplication(WAppIcon *btn, Bool withSelection)
809 WScreen *scr = btn->icon->core->screen_ptr;
811 if (!btn->launching &&
812 ((!withSelection && btn->command != NULL) || (withSelection && btn->paste_command != NULL))) {
813 if (!btn->forced_dock) {
814 btn->relaunching = btn->running;
815 btn->running = 1;
817 if (btn->wm_instance || btn->wm_class) {
818 WWindowAttributes attr;
819 memset(&attr, 0, sizeof(WWindowAttributes));
820 wDefaultFillAttributes(scr, btn->wm_instance, btn->wm_class, &attr, NULL, True);
822 if (!attr.no_appicon && !btn->buggy_app)
823 btn->launching = 1;
824 else
825 btn->running = 0;
827 btn->drop_launch = 0;
828 btn->paste_launch = withSelection;
829 scr->last_dock = btn->dock;
830 btn->pid = execCommand(btn, (withSelection ? btn->paste_command : btn->command), NULL);
831 if (btn->pid > 0) {
832 if (btn->buggy_app) {
833 /* give feedback that the app was launched */
834 btn->launching = 1;
835 dockIconPaint(btn);
836 btn->launching = 0;
837 WMAddTimerHandler(200, (WMCallback *) dockIconPaint, btn);
838 } else {
839 dockIconPaint(btn);
841 } else {
842 wwarning(_("could not launch application %s"), btn->command);
843 btn->launching = 0;
844 if (!btn->relaunching) {
845 btn->running = 0;
851 static void updateWorkspaceMenu(WMenu *menu, WAppIcon *icon)
853 WScreen *scr = menu->frame->screen_ptr;
854 char title[MAX_WORKSPACENAME_WIDTH + 1];
855 int i;
857 if (!menu || !icon)
858 return;
860 for (i = 0; i < scr->workspace_count; i++) {
861 if (i < menu->entry_no) {
862 if (strcmp(menu->entries[i]->text, scr->workspaces[i]->name) != 0) {
863 wfree(menu->entries[i]->text);
864 strcpy(title, scr->workspaces[i]->name);
865 menu->entries[i]->text = wstrdup(title);
866 menu->flags.realized = 0;
868 menu->entries[i]->clientdata = (void *)icon;
869 } else {
870 strcpy(title, scr->workspaces[i]->name);
872 wMenuAddCallback(menu, title, switchWSCommand, (void *)icon);
874 menu->flags.realized = 0;
876 if (i == scr->current_workspace) {
877 wMenuSetEnabled(menu, i, False);
878 } else {
879 wMenuSetEnabled(menu, i, True);
883 if (!menu->flags.realized)
884 wMenuRealize(menu);
887 static WMenu *makeWorkspaceMenu(WScreen *scr)
889 WMenu *menu;
891 menu = wMenuCreate(scr, NULL, False);
892 if (!menu)
893 wwarning(_("could not create workspace submenu for Clip menu"));
895 wMenuAddCallback(menu, "", switchWSCommand, (void *)scr->clip_icon);
897 menu->flags.realized = 0;
898 wMenuRealize(menu);
900 return menu;
903 static void updateClipOptionsMenu(WMenu *menu, WDock *dock)
905 WMenuEntry *entry;
906 int index = 0;
908 if (!menu || !dock)
909 return;
911 /* keep on top */
912 entry = menu->entries[index];
913 entry->flags.indicator_on = !dock->lowered;
914 entry->clientdata = dock;
916 /* collapsed */
917 entry = menu->entries[++index];
918 entry->flags.indicator_on = dock->collapsed;
919 entry->clientdata = dock;
921 /* auto-collapse */
922 entry = menu->entries[++index];
923 entry->flags.indicator_on = dock->auto_collapse;
924 entry->clientdata = dock;
926 /* auto-raise/lower */
927 entry = menu->entries[++index];
928 entry->flags.indicator_on = dock->auto_raise_lower;
929 entry->clientdata = dock;
930 wMenuSetEnabled(menu, index, dock->lowered);
932 /* attract icons */
933 entry = menu->entries[++index];
934 entry->flags.indicator_on = dock->attract_icons;
935 entry->clientdata = dock;
937 menu->flags.realized = 0;
938 wMenuRealize(menu);
941 static WMenu *makeClipOptionsMenu(WScreen *scr)
943 WMenu *menu;
944 WMenuEntry *entry;
946 menu = wMenuCreate(scr, NULL, False);
947 if (!menu) {
948 wwarning(_("could not create options submenu for Clip menu"));
949 return NULL;
952 entry = wMenuAddCallback(menu, _("Keep on Top"), toggleLoweredCallback, NULL);
953 entry->flags.indicator = 1;
954 entry->flags.indicator_on = 1;
955 entry->flags.indicator_type = MI_CHECK;
957 entry = wMenuAddCallback(menu, _("Collapsed"), toggleCollapsedCallback, NULL);
958 entry->flags.indicator = 1;
959 entry->flags.indicator_on = 1;
960 entry->flags.indicator_type = MI_CHECK;
962 entry = wMenuAddCallback(menu, _("Autocollapse"), toggleAutoCollapseCallback, NULL);
963 entry->flags.indicator = 1;
964 entry->flags.indicator_on = 1;
965 entry->flags.indicator_type = MI_CHECK;
967 entry = wMenuAddCallback(menu, _("Autoraise"), toggleAutoRaiseLowerCallback, NULL);
968 entry->flags.indicator = 1;
969 entry->flags.indicator_on = 1;
970 entry->flags.indicator_type = MI_CHECK;
972 entry = wMenuAddCallback(menu, _("Autoattract Icons"), toggleAutoAttractCallback, NULL);
973 entry->flags.indicator = 1;
974 entry->flags.indicator_on = 1;
975 entry->flags.indicator_type = MI_CHECK;
977 menu->flags.realized = 0;
978 wMenuRealize(menu);
980 return menu;
983 static WMenu *dockMenuCreate(WScreen *scr, int type)
985 WMenu *menu;
986 WMenuEntry *entry;
988 if (type == WM_CLIP && scr->clip_menu)
989 return scr->clip_menu;
991 menu = wMenuCreate(scr, NULL, False);
992 if (type != WM_CLIP) {
993 entry = wMenuAddCallback(menu, _("Keep on Top"), toggleLoweredCallback, NULL);
994 entry->flags.indicator = 1;
995 entry->flags.indicator_on = 1;
996 entry->flags.indicator_type = MI_CHECK;
997 } else {
998 entry = wMenuAddCallback(menu, _("Clip Options"), NULL, NULL);
999 scr->clip_options = makeClipOptionsMenu(scr);
1000 if (scr->clip_options)
1001 wMenuEntrySetCascade(menu, entry, scr->clip_options);
1003 entry = wMenuAddCallback(menu, _("Rename Workspace"), renameCallback, NULL);
1004 wfree(entry->text);
1005 entry->text = _("Rename Workspace");
1007 entry = wMenuAddCallback(menu, _("Selected"), selectCallback, NULL);
1008 entry->flags.indicator = 1;
1009 entry->flags.indicator_on = 1;
1010 entry->flags.indicator_type = MI_CHECK;
1012 entry = wMenuAddCallback(menu, _("Select All Icons"), selectIconsCallback, NULL);
1013 wfree(entry->text);
1014 entry->text = _("Select All Icons");
1016 entry = wMenuAddCallback(menu, _("Keep Icon"), keepIconsCallback, NULL);
1017 wfree(entry->text);
1018 entry->text = _("Keep Icon");
1020 entry = wMenuAddCallback(menu, _("Move Icon To"), NULL, NULL);
1021 wfree(entry->text);
1022 entry->text = _("Move Icon To");
1023 scr->clip_submenu = makeWorkspaceMenu(scr);
1024 if (scr->clip_submenu)
1025 wMenuEntrySetCascade(menu, entry, scr->clip_submenu);
1027 entry = wMenuAddCallback(menu, _("Remove Icon"), removeIconsCallback, NULL);
1028 wfree(entry->text);
1029 entry->text = _("Remove Icon");
1031 wMenuAddCallback(menu, _("Attract Icons"), colectIconsCallback, NULL);
1034 wMenuAddCallback(menu, _("Launch"), launchCallback, NULL);
1036 wMenuAddCallback(menu, _("Unhide Here"), unhideHereCallback, NULL);
1038 entry = wMenuAddCallback(menu, _("Hide"), hideCallback, NULL);
1039 wfree(entry->text);
1040 entry->text = _("Hide");
1042 wMenuAddCallback(menu, _("Settings..."), settingsCallback, NULL);
1044 wMenuAddCallback(menu, _("Kill"), killCallback, NULL);
1046 if (type == WM_CLIP)
1047 scr->clip_menu = menu;
1049 return menu;
1052 WDock *wDockCreate(WScreen *scr, int type)
1054 WDock *dock;
1055 WAppIcon *btn;
1057 make_keys();
1059 dock = wmalloc(sizeof(WDock));
1060 memset(dock, 0, sizeof(WDock));
1062 dock->max_icons = DOCK_MAX_ICONS;
1064 dock->icon_array = wmalloc(sizeof(WAppIcon *) * dock->max_icons);
1065 memset(dock->icon_array, 0, sizeof(WAppIcon *) * dock->max_icons);
1067 btn = mainIconCreate(scr, type);
1069 btn->dock = dock;
1071 dock->x_pos = btn->x_pos;
1072 dock->y_pos = btn->y_pos;
1073 dock->screen_ptr = scr;
1074 dock->type = type;
1075 dock->icon_count = 1;
1076 dock->on_right_side = 1;
1077 dock->collapsed = 0;
1078 dock->auto_collapse = 0;
1079 dock->auto_collapse_magic = NULL;
1080 dock->auto_raise_lower = 0;
1081 dock->auto_lower_magic = NULL;
1082 dock->auto_raise_magic = NULL;
1083 dock->attract_icons = 0;
1084 dock->lowered = 1;
1085 dock->icon_array[0] = btn;
1086 wRaiseFrame(btn->icon->core);
1087 XMoveWindow(dpy, btn->icon->core->window, btn->x_pos, btn->y_pos);
1089 /* create dock menu */
1090 dock->menu = dockMenuCreate(scr, type);
1092 return dock;
1095 void wDockDestroy(WDock *dock)
1097 int i;
1098 WAppIcon *aicon;
1100 for (i = (dock->type == WM_CLIP) ? 1 : 0; i < dock->max_icons; i++) {
1101 aicon = dock->icon_array[i];
1102 if (aicon) {
1103 int keepit = aicon->running && wApplicationOf(aicon->main_window);
1104 wDockDetach(dock, aicon);
1105 if (keepit) {
1106 /* XXX: can: aicon->icon == NULL ? */
1107 PlaceIcon(dock->screen_ptr, &aicon->x_pos, &aicon->y_pos,
1108 wGetHeadForWindow(aicon->icon->owner));
1109 XMoveWindow(dpy, aicon->icon->core->window, aicon->x_pos, aicon->y_pos);
1110 if (!dock->mapped || dock->collapsed)
1111 XMapWindow(dpy, aicon->icon->core->window);
1115 if (wPreferences.auto_arrange_icons)
1116 wArrangeIcons(dock->screen_ptr, True);
1117 wfree(dock->icon_array);
1118 if (dock->menu && dock->type != WM_CLIP)
1119 wMenuDestroy(dock->menu, True);
1120 if (dock->screen_ptr->last_dock == dock)
1121 dock->screen_ptr->last_dock = NULL;
1122 wfree(dock);
1125 void wClipIconPaint(WAppIcon *aicon)
1127 WScreen *scr = aicon->icon->core->screen_ptr;
1128 WWorkspace *workspace = scr->workspaces[scr->current_workspace];
1129 WMColor *color;
1130 Window win = aicon->icon->core->window;
1131 int length, nlength;
1132 char *ws_name, ws_number[10];
1133 int ty, tx;
1135 wIconPaint(aicon->icon);
1137 length = strlen(workspace->name);
1138 ws_name = wmalloc(length + 1);
1139 snprintf(ws_name, length + 1, "%s", workspace->name);
1140 snprintf(ws_number, sizeof(ws_number), "%i", scr->current_workspace + 1);
1141 nlength = strlen(ws_number);
1143 if (!workspace->clip->collapsed)
1144 color = scr->clip_title_color[CLIP_NORMAL];
1145 else
1146 color = scr->clip_title_color[CLIP_COLLAPSED];
1148 ty = ICON_SIZE - WMFontHeight(scr->clip_title_font) - 3;
1150 tx = CLIP_BUTTON_SIZE * ICON_SIZE / 64;
1152 if(wPreferences.show_clip_title)
1153 WMDrawString(scr->wmscreen, win, color, scr->clip_title_font, tx, ty, ws_name, length);
1155 tx = (ICON_SIZE / 2 - WMWidthOfString(scr->clip_title_font, ws_number, nlength)) / 2;
1157 WMDrawString(scr->wmscreen, win, color, scr->clip_title_font, tx, 2, ws_number, nlength);
1159 wfree(ws_name);
1161 if (aicon->launching) {
1162 XFillRectangle(dpy, aicon->icon->core->window, scr->stipple_gc,
1163 0, 0, wPreferences.icon_size, wPreferences.icon_size);
1165 paintClipButtons(aicon, aicon->dock->lclip_button_pushed, aicon->dock->rclip_button_pushed);
1168 static void clipIconExpose(WObjDescriptor *desc, XEvent *event)
1170 wClipIconPaint(desc->parent);
1173 static void dockIconPaint(WAppIcon *btn)
1175 if (btn == btn->icon->core->screen_ptr->clip_icon)
1176 wClipIconPaint(btn);
1177 else {
1178 wAppIconPaint(btn);
1179 wAppIconSave(btn);
1183 static WMPropList *make_icon_state(WAppIcon *btn)
1185 WMPropList *node = NULL;
1186 WMPropList *command, *autolaunch, *lock, *name, *forced, *host;
1187 WMPropList *position, *buggy, *omnipresent;
1188 char *tmp;
1189 char buffer[64];
1191 if (btn) {
1192 if (!btn->command)
1193 command = WMCreatePLString("-");
1194 else
1195 command = WMCreatePLString(btn->command);
1197 autolaunch = btn->auto_launch ? dYes : dNo;
1199 lock = btn->lock ? dYes : dNo;
1201 tmp = EscapeWM_CLASS(btn->wm_instance, btn->wm_class);
1203 name = WMCreatePLString(tmp);
1205 wfree(tmp);
1207 forced = btn->forced_dock ? dYes : dNo;
1209 buggy = btn->buggy_app ? dYes : dNo;
1211 if (btn == btn->icon->core->screen_ptr->clip_icon)
1212 snprintf(buffer, sizeof(buffer), "%i,%i", btn->x_pos, btn->y_pos);
1213 else
1214 snprintf(buffer, sizeof(buffer), "%hi,%hi", btn->xindex, btn->yindex);
1215 position = WMCreatePLString(buffer);
1217 node = WMCreatePLDictionary(dCommand, command,
1218 dName, name,
1219 dAutoLaunch, autolaunch,
1220 dLock, lock,
1221 dForced, forced, dBuggyApplication, buggy, dPosition, position, NULL);
1222 WMReleasePropList(command);
1223 WMReleasePropList(name);
1224 WMReleasePropList(position);
1226 omnipresent = btn->omnipresent ? dYes : dNo;
1227 if (btn->dock != btn->icon->core->screen_ptr->dock && (btn->xindex != 0 || btn->yindex != 0))
1228 WMPutInPLDictionary(node, dOmnipresent, omnipresent);
1230 #ifdef XDND /* was OFFIX */
1231 if (btn->dnd_command) {
1232 command = WMCreatePLString(btn->dnd_command);
1233 WMPutInPLDictionary(node, dDropCommand, command);
1234 WMReleasePropList(command);
1236 #endif /* XDND */
1238 if (btn->paste_command) {
1239 command = WMCreatePLString(btn->paste_command);
1240 WMPutInPLDictionary(node, dPasteCommand, command);
1241 WMReleasePropList(command);
1244 if (btn->client_machine && btn->remote_start) {
1245 host = WMCreatePLString(btn->client_machine);
1246 WMPutInPLDictionary(node, dHost, host);
1247 WMReleasePropList(host);
1251 return node;
1254 static WMPropList *dockSaveState(WDock *dock)
1256 int i;
1257 WMPropList *icon_info;
1258 WMPropList *list = NULL, *dock_state = NULL;
1259 WMPropList *value, *key;
1260 char buffer[256];
1262 list = WMCreatePLArray(NULL);
1264 for (i = (dock->type == WM_DOCK ? 0 : 1); i < dock->max_icons; i++) {
1265 WAppIcon *btn = dock->icon_array[i];
1267 if (!btn || btn->attracted)
1268 continue;
1270 if ((icon_info = make_icon_state(dock->icon_array[i]))) {
1271 WMAddToPLArray(list, icon_info);
1272 WMReleasePropList(icon_info);
1276 dock_state = WMCreatePLDictionary(dApplications, list, NULL);
1278 if (dock->type == WM_DOCK) {
1279 snprintf(buffer, sizeof(buffer), "Applications%i", dock->screen_ptr->scr_height);
1280 key = WMCreatePLString(buffer);
1281 WMPutInPLDictionary(dock_state, key, list);
1282 WMReleasePropList(key);
1284 snprintf(buffer, sizeof(buffer), "%i,%i", (dock->on_right_side ? -ICON_SIZE : 0), dock->y_pos);
1285 value = WMCreatePLString(buffer);
1286 WMPutInPLDictionary(dock_state, dPosition, value);
1287 WMReleasePropList(value);
1289 WMReleasePropList(list);
1291 value = (dock->lowered ? dYes : dNo);
1292 WMPutInPLDictionary(dock_state, dLowered, value);
1294 if (dock->type == WM_CLIP) {
1295 value = (dock->collapsed ? dYes : dNo);
1296 WMPutInPLDictionary(dock_state, dCollapsed, value);
1298 value = (dock->auto_collapse ? dYes : dNo);
1299 WMPutInPLDictionary(dock_state, dAutoCollapse, value);
1301 value = (dock->auto_raise_lower ? dYes : dNo);
1302 WMPutInPLDictionary(dock_state, dAutoRaiseLower, value);
1304 value = (dock->attract_icons ? dYes : dNo);
1305 WMPutInPLDictionary(dock_state, dAutoAttractIcons, value);
1308 return dock_state;
1311 void wDockSaveState(WScreen *scr, WMPropList *old_state)
1313 WMPropList *dock_state;
1314 WMPropList *keys;
1316 dock_state = dockSaveState(scr->dock);
1319 * Copy saved states of docks with different sizes.
1321 if (old_state) {
1322 int i;
1323 WMPropList *tmp;
1325 keys = WMGetPLDictionaryKeys(old_state);
1326 for (i = 0; i < WMGetPropListItemCount(keys); i++) {
1327 tmp = WMGetFromPLArray(keys, i);
1329 if (strncasecmp(WMGetFromPLString(tmp), "applications", 12) == 0
1330 && !WMGetFromPLDictionary(dock_state, tmp)) {
1332 WMPutInPLDictionary(dock_state, tmp, WMGetFromPLDictionary(old_state, tmp));
1335 WMReleasePropList(keys);
1338 WMPutInPLDictionary(scr->session_state, dDock, dock_state);
1340 WMReleasePropList(dock_state);
1343 void wClipSaveState(WScreen *scr)
1345 WMPropList *clip_state;
1347 clip_state = make_icon_state(scr->clip_icon);
1349 WMPutInPLDictionary(scr->session_state, dClip, clip_state);
1351 WMReleasePropList(clip_state);
1354 WMPropList *wClipSaveWorkspaceState(WScreen *scr, int workspace)
1356 return dockSaveState(scr->workspaces[workspace]->clip);
1359 static Bool getBooleanDockValue(WMPropList *value, WMPropList *key)
1361 if (value) {
1362 if (WMIsPLString(value)) {
1363 if (strcasecmp(WMGetFromPLString(value), "YES") == 0)
1364 return True;
1365 } else {
1366 wwarning(_("bad value in docked icon state info %s"), WMGetFromPLString(key));
1369 return False;
1372 static WAppIcon *restore_icon_state(WScreen *scr, WMPropList *info, int type, int index)
1374 WAppIcon *aicon;
1375 WMPropList *cmd, *value;
1377 cmd = WMGetFromPLDictionary(info, dCommand);
1378 if (!cmd || !WMIsPLString(cmd)) {
1379 return NULL;
1382 /* parse window name */
1383 value = WMGetFromPLDictionary(info, dName);
1384 if (!value)
1385 return NULL;
1388 char *wclass, *winstance;
1389 char *command;
1391 ParseWindowName(value, &winstance, &wclass, "dock");
1393 if (!winstance && !wclass) {
1394 return NULL;
1397 /* get commands */
1399 if (cmd)
1400 command = wstrdup(WMGetFromPLString(cmd));
1401 else
1402 command = NULL;
1404 if (!command || strcmp(command, "-") == 0) {
1405 if (command)
1406 wfree(command);
1407 if (wclass)
1408 wfree(wclass);
1409 if (winstance)
1410 wfree(winstance);
1412 return NULL;
1415 aicon = wAppIconCreateForDock(scr, command, winstance, wclass, TILE_NORMAL);
1416 if (wclass)
1417 wfree(wclass);
1418 if (winstance)
1419 wfree(winstance);
1420 if (command)
1421 wfree(command);
1424 aicon->icon->core->descriptor.handle_mousedown = iconMouseDown;
1425 if (type == WM_CLIP) {
1426 aicon->icon->core->descriptor.handle_enternotify = clipEnterNotify;
1427 aicon->icon->core->descriptor.handle_leavenotify = clipLeaveNotify;
1429 aicon->icon->core->descriptor.parent_type = WCLASS_DOCK_ICON;
1430 aicon->icon->core->descriptor.parent = aicon;
1432 #ifdef XDND /* was OFFIX */
1433 cmd = WMGetFromPLDictionary(info, dDropCommand);
1434 if (cmd)
1435 aicon->dnd_command = wstrdup(WMGetFromPLString(cmd));
1436 #endif
1438 cmd = WMGetFromPLDictionary(info, dPasteCommand);
1439 if (cmd)
1440 aicon->paste_command = wstrdup(WMGetFromPLString(cmd));
1442 /* check auto launch */
1443 value = WMGetFromPLDictionary(info, dAutoLaunch);
1445 aicon->auto_launch = getBooleanDockValue(value, dAutoLaunch);
1447 /* check lock */
1448 value = WMGetFromPLDictionary(info, dLock);
1450 aicon->lock = getBooleanDockValue(value, dLock);
1452 /* check if it wasn't normally docked */
1453 value = WMGetFromPLDictionary(info, dForced);
1455 aicon->forced_dock = getBooleanDockValue(value, dForced);
1457 /* check if we can rely on the stuff in the app */
1458 value = WMGetFromPLDictionary(info, dBuggyApplication);
1460 aicon->buggy_app = getBooleanDockValue(value, dBuggyApplication);
1462 /* get position in the dock */
1463 value = WMGetFromPLDictionary(info, dPosition);
1464 if (value && WMIsPLString(value)) {
1465 if (sscanf(WMGetFromPLString(value), "%hi,%hi", &aicon->xindex, &aicon->yindex) != 2)
1466 wwarning(_("bad value in docked icon state info %s"), WMGetFromPLString(dPosition));
1468 /* check position sanity */
1469 /* incomplete section! */
1470 if (type == WM_DOCK) {
1471 aicon->xindex = 0;
1472 if (aicon->yindex < 0)
1473 wwarning(_("bad value in docked icon position %i,%i"),
1474 aicon->xindex, aicon->yindex);
1476 } else {
1477 aicon->yindex = index;
1478 aicon->xindex = 0;
1481 /* check if icon is omnipresent */
1482 value = WMGetFromPLDictionary(info, dOmnipresent);
1484 aicon->omnipresent = getBooleanDockValue(value, dOmnipresent);
1486 aicon->running = 0;
1487 aicon->docked = 1;
1489 return aicon;
1492 #define COMPLAIN(key) wwarning(_("bad value in dock state info:%s"), key)
1494 WAppIcon *wClipRestoreState(WScreen *scr, WMPropList *clip_state)
1496 WAppIcon *icon;
1497 WMPropList *value;
1499 icon = mainIconCreate(scr, WM_CLIP);
1501 if (!clip_state)
1502 return icon;
1504 WMRetainPropList(clip_state);
1506 /* restore position */
1508 value = WMGetFromPLDictionary(clip_state, dPosition);
1510 if (value) {
1511 if (!WMIsPLString(value))
1512 COMPLAIN("Position");
1513 else {
1514 WMRect rect;
1515 int flags;
1517 if (sscanf(WMGetFromPLString(value), "%i,%i", &icon->x_pos, &icon->y_pos) != 2)
1518 COMPLAIN("Position");
1520 /* check position sanity */
1521 rect.pos.x = icon->x_pos;
1522 rect.pos.y = icon->y_pos;
1523 rect.size.width = rect.size.height = ICON_SIZE;
1525 wGetRectPlacementInfo(scr, rect, &flags);
1526 if (flags & (XFLAG_DEAD | XFLAG_PARTIAL))
1527 wScreenKeepInside(scr, &icon->x_pos, &icon->y_pos, ICON_SIZE, ICON_SIZE);
1530 #ifdef XDND /* was OFFIX */
1531 value = WMGetFromPLDictionary(clip_state, dDropCommand);
1532 if (value && WMIsPLString(value))
1533 icon->dnd_command = wstrdup(WMGetFromPLString(value));
1534 #endif
1536 value = WMGetFromPLDictionary(clip_state, dPasteCommand);
1537 if (value && WMIsPLString(value))
1538 icon->paste_command = wstrdup(WMGetFromPLString(value));
1540 WMReleasePropList(clip_state);
1542 return icon;
1545 WDock *wDockRestoreState(WScreen *scr, WMPropList *dock_state, int type)
1547 WDock *dock;
1548 WMPropList *apps;
1549 WMPropList *value;
1550 WAppIcon *aicon, *old_top;
1551 int count, i;
1553 dock = wDockCreate(scr, type);
1555 if (!dock_state)
1556 return dock;
1558 WMRetainPropList(dock_state);
1560 /* restore position */
1561 value = WMGetFromPLDictionary(dock_state, dPosition);
1562 if (value) {
1563 if (!WMIsPLString(value)) {
1564 COMPLAIN("Position");
1565 } else {
1566 WMRect rect;
1567 int flags;
1569 if (sscanf(WMGetFromPLString(value), "%i,%i", &dock->x_pos, &dock->y_pos) != 2)
1570 COMPLAIN("Position");
1572 /* check position sanity */
1573 rect.pos.x = dock->x_pos;
1574 rect.pos.y = dock->y_pos;
1575 rect.size.width = rect.size.height = ICON_SIZE;
1577 wGetRectPlacementInfo(scr, rect, &flags);
1578 if (flags & (XFLAG_DEAD | XFLAG_PARTIAL)) {
1579 int x = dock->x_pos;
1580 wScreenKeepInside(scr, &x, &dock->y_pos, ICON_SIZE, ICON_SIZE);
1583 /* Is this needed any more? */
1584 if (type == WM_CLIP) {
1585 if (dock->x_pos < 0) {
1586 dock->x_pos = 0;
1587 } else if (dock->x_pos > scr->scr_width - ICON_SIZE) {
1588 dock->x_pos = scr->scr_width - ICON_SIZE;
1590 } else {
1591 if (dock->x_pos >= 0) {
1592 dock->x_pos = DOCK_EXTRA_SPACE;
1593 dock->on_right_side = 0;
1594 } else {
1595 dock->x_pos = scr->scr_width - DOCK_EXTRA_SPACE - ICON_SIZE;
1596 dock->on_right_side = 1;
1602 /* restore lowered/raised state */
1603 dock->lowered = 0;
1605 value = WMGetFromPLDictionary(dock_state, dLowered);
1606 if (value) {
1607 if (!WMIsPLString(value)) {
1608 COMPLAIN("Lowered");
1609 } else {
1610 if (strcasecmp(WMGetFromPLString(value), "YES") == 0) {
1611 dock->lowered = 1;
1616 /* restore collapsed state */
1617 dock->collapsed = 0;
1619 value = WMGetFromPLDictionary(dock_state, dCollapsed);
1620 if (value) {
1621 if (!WMIsPLString(value)) {
1622 COMPLAIN("Collapsed");
1623 } else {
1624 if (strcasecmp(WMGetFromPLString(value), "YES") == 0) {
1625 dock->collapsed = 1;
1630 /* restore auto-collapsed state */
1631 value = WMGetFromPLDictionary(dock_state, dAutoCollapse);
1632 if (value) {
1633 if (!WMIsPLString(value)) {
1634 COMPLAIN("AutoCollapse");
1635 } else {
1636 if (strcasecmp(WMGetFromPLString(value), "YES") == 0) {
1637 dock->auto_collapse = 1;
1638 dock->collapsed = 1;
1643 /* restore auto-raise/lower state */
1644 value = WMGetFromPLDictionary(dock_state, dAutoRaiseLower);
1645 if (value) {
1646 if (!WMIsPLString(value)) {
1647 COMPLAIN("AutoRaiseLower");
1648 } else {
1649 if (strcasecmp(WMGetFromPLString(value), "YES") == 0)
1650 dock->auto_raise_lower = 1;
1654 /* restore attract icons state */
1655 dock->attract_icons = 0;
1657 value = WMGetFromPLDictionary(dock_state, dAutoAttractIcons);
1658 if (value) {
1659 if (!WMIsPLString(value)) {
1660 COMPLAIN("AutoAttractIcons");
1661 } else {
1662 if (strcasecmp(WMGetFromPLString(value), "YES") == 0)
1663 dock->attract_icons = 1;
1667 /* application list */
1670 WMPropList *tmp;
1671 char buffer[64];
1674 * When saving, it saves the dock state in
1675 * Applications and Applicationsnnn
1677 * When loading, it will first try Applicationsnnn.
1678 * If it does not exist, use Applications as default.
1681 snprintf(buffer, sizeof(buffer), "Applications%i", scr->scr_height);
1683 tmp = WMCreatePLString(buffer);
1684 apps = WMGetFromPLDictionary(dock_state, tmp);
1685 WMReleasePropList(tmp);
1687 if (!apps)
1688 apps = WMGetFromPLDictionary(dock_state, dApplications);
1691 if (!apps)
1692 goto finish;
1694 count = WMGetPropListItemCount(apps);
1695 if (count == 0)
1696 goto finish;
1698 old_top = dock->icon_array[0];
1700 /* dock->icon_count is set to 1 when dock is created.
1701 * Since Clip is already restored, we want to keep it so for clip,
1702 * but for dock we may change the default top tile, so we set it to 0.
1704 if (type == WM_DOCK)
1705 dock->icon_count = 0;
1707 for (i = 0; i < count; i++) {
1708 if (dock->icon_count >= dock->max_icons) {
1709 wwarning(_("there are too many icons stored in dock. Ignoring what doesn't fit"));
1710 break;
1713 value = WMGetFromPLArray(apps, i);
1714 aicon = restore_icon_state(scr, value, type, dock->icon_count);
1716 dock->icon_array[dock->icon_count] = aicon;
1718 if (aicon) {
1719 aicon->dock = dock;
1720 aicon->x_pos = dock->x_pos + (aicon->xindex * ICON_SIZE);
1721 aicon->y_pos = dock->y_pos + (aicon->yindex * ICON_SIZE);
1723 if (dock->lowered)
1724 ChangeStackingLevel(aicon->icon->core, WMNormalLevel);
1725 else
1726 ChangeStackingLevel(aicon->icon->core, WMDockLevel);
1728 wCoreConfigure(aicon->icon->core, aicon->x_pos, aicon->y_pos, 0, 0);
1730 if (!dock->collapsed)
1731 XMapWindow(dpy, aicon->icon->core->window);
1732 wRaiseFrame(aicon->icon->core);
1734 dock->icon_count++;
1735 } else if (dock->icon_count == 0 && type == WM_DOCK)
1736 dock->icon_count++;
1739 /* if the first icon is not defined, use the default */
1740 if (dock->icon_array[0] == NULL) {
1741 /* update default icon */
1742 old_top->x_pos = dock->x_pos;
1743 old_top->y_pos = dock->y_pos;
1744 if (dock->lowered)
1745 ChangeStackingLevel(old_top->icon->core, WMNormalLevel);
1746 else
1747 ChangeStackingLevel(old_top->icon->core, WMDockLevel);
1748 dock->icon_array[0] = old_top;
1749 XMoveWindow(dpy, old_top->icon->core->window, dock->x_pos, dock->y_pos);
1750 /* we don't need to increment dock->icon_count here because it was
1751 * incremented in the loop above.
1753 } else if (old_top != dock->icon_array[0]) {
1754 if (old_top == scr->clip_icon)
1755 scr->clip_icon = dock->icon_array[0];
1756 wAppIconDestroy(old_top);
1759 finish:
1760 WMReleasePropList(dock_state);
1762 return dock;
1765 void wDockLaunchWithState(WDock *dock, WAppIcon *btn, WSavedState *state)
1767 if (btn && btn->command && !btn->running && !btn->launching) {
1769 btn->drop_launch = 0;
1770 btn->paste_launch = 0;
1772 btn->pid = execCommand(btn, btn->command, state);
1774 if (btn->pid > 0) {
1775 if (!btn->forced_dock && !btn->buggy_app) {
1776 btn->launching = 1;
1777 dockIconPaint(btn);
1780 } else {
1781 wfree(state);
1785 void wDockDoAutoLaunch(WDock *dock, int workspace)
1787 WAppIcon *btn;
1788 WSavedState *state;
1789 int i;
1791 for (i = 0; i < dock->max_icons; i++) {
1792 btn = dock->icon_array[i];
1793 if (!btn || !btn->auto_launch)
1794 continue;
1796 state = wmalloc(sizeof(WSavedState));
1797 memset(state, 0, sizeof(WSavedState));
1798 state->workspace = workspace;
1799 /* TODO: this is klugy and is very difficult to understand
1800 * what's going on. Try to clean up */
1801 wDockLaunchWithState(dock, btn, state);
1805 #ifdef XDND /* was OFFIX */
1806 static WDock *findDock(WScreen *scr, XEvent *event, int *icon_pos)
1808 WDock *dock;
1809 int i;
1811 *icon_pos = -1;
1812 if ((dock = scr->dock) != 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 && (dock = scr->workspaces[scr->current_workspace]->clip) != NULL) {
1822 for (i = 0; i < dock->max_icons; i++) {
1823 if (dock->icon_array[i]
1824 && dock->icon_array[i]->icon->core->window == event->xclient.window) {
1825 *icon_pos = i;
1826 break;
1830 if (*icon_pos >= 0)
1831 return dock;
1832 return NULL;
1835 int wDockReceiveDNDDrop(WScreen *scr, XEvent *event)
1837 WDock *dock;
1838 WAppIcon *btn;
1839 int icon_pos;
1841 dock = findDock(scr, event, &icon_pos);
1842 if (!dock)
1843 return False;
1846 * Return True if the drop was on an application icon window.
1847 * In this case, let the ClientMessage handler redirect the
1848 * message to the app.
1850 if (dock->icon_array[icon_pos]->icon->icon_win != None)
1851 return True;
1853 if (dock->icon_array[icon_pos]->dnd_command != NULL) {
1854 scr->flags.dnd_data_convertion_status = 0;
1856 btn = dock->icon_array[icon_pos];
1858 if (!btn->forced_dock) {
1859 btn->relaunching = btn->running;
1860 btn->running = 1;
1862 if (btn->wm_instance || btn->wm_class) {
1863 WWindowAttributes attr;
1864 memset(&attr, 0, sizeof(WWindowAttributes));
1865 wDefaultFillAttributes(btn->icon->core->screen_ptr,
1866 btn->wm_instance, btn->wm_class, &attr, NULL, True);
1868 if (!attr.no_appicon)
1869 btn->launching = 1;
1870 else
1871 btn->running = 0;
1874 btn->paste_launch = 0;
1875 btn->drop_launch = 1;
1876 scr->last_dock = dock;
1877 btn->pid = execCommand(btn, btn->dnd_command, NULL);
1878 if (btn->pid > 0) {
1879 dockIconPaint(btn);
1880 } else {
1881 btn->launching = 0;
1882 if (!btn->relaunching) {
1883 btn->running = 0;
1887 return False;
1889 #endif /* XDND */
1891 Bool wDockAttachIcon(WDock *dock, WAppIcon *icon, int x, int y)
1893 WWindow *wwin;
1894 int index;
1896 wwin = icon->icon->owner;
1897 if (icon->command == NULL) {
1898 char *command;
1900 icon->editing = 0;
1902 command = GetCommandForWindow(wwin->client_win);
1903 if (command) {
1904 icon->command = command;
1905 } else {
1906 /* icon->forced_dock = 1; */
1907 if (dock->type != WM_CLIP || !icon->attracted) {
1908 icon->editing = 1;
1909 if (wInputDialog(dock->screen_ptr, _("Dock Icon"),
1910 _("Type the command used to launch the application"), &command)) {
1911 if (command && (command[0] == 0 || (command[0] == '-' && command[1] == 0))) {
1912 wfree(command);
1913 command = NULL;
1915 icon->command = command;
1916 icon->editing = 0;
1917 } else {
1918 icon->editing = 0;
1919 if (command)
1920 wfree(command);
1921 /* If the target is the dock, reject the icon. If
1922 * the target is the clip, make it an attracted icon
1924 if (dock->type == WM_CLIP) {
1925 icon->attracted = 1;
1926 if (!icon->icon->shadowed) {
1927 icon->icon->shadowed = 1;
1928 icon->icon->force_paint = 1;
1930 } else {
1931 return False;
1936 } else {
1937 icon->editing = 0;
1940 for (index = 1; index < dock->max_icons; index++)
1941 if (dock->icon_array[index] == NULL)
1942 break;
1943 /* if (index == dock->max_icons)
1944 return; */
1946 assert(index < dock->max_icons);
1948 dock->icon_array[index] = icon;
1949 icon->yindex = y;
1950 icon->xindex = x;
1952 icon->omnipresent = 0;
1954 icon->x_pos = dock->x_pos + x * ICON_SIZE;
1955 icon->y_pos = dock->y_pos + y * ICON_SIZE;
1957 dock->icon_count++;
1959 icon->running = 1;
1960 icon->launching = 0;
1961 icon->docked = 1;
1962 icon->dock = dock;
1963 icon->icon->core->descriptor.handle_mousedown = iconMouseDown;
1964 if (dock->type == WM_CLIP) {
1965 icon->icon->core->descriptor.handle_enternotify = clipEnterNotify;
1966 icon->icon->core->descriptor.handle_leavenotify = clipLeaveNotify;
1968 icon->icon->core->descriptor.parent_type = WCLASS_DOCK_ICON;
1969 icon->icon->core->descriptor.parent = icon;
1971 MoveInStackListUnder(dock->icon_array[index - 1]->icon->core, icon->icon->core);
1972 wAppIconMove(icon, icon->x_pos, icon->y_pos);
1973 wAppIconPaint(icon);
1974 wAppIconSave(icon);
1976 if (wPreferences.auto_arrange_icons)
1977 wArrangeIcons(dock->screen_ptr, True);
1979 #ifdef XDND /* was OFFIX */
1980 if (icon->command && !icon->dnd_command) {
1981 int len = strlen(icon->command) + 8;
1982 icon->dnd_command = wmalloc(len);
1983 snprintf(icon->dnd_command, len, "%s %%d", icon->command);
1985 #endif
1987 if (icon->command && !icon->paste_command) {
1988 int len = strlen(icon->command) + 8;
1989 icon->paste_command = wmalloc(len);
1990 snprintf(icon->paste_command, len, "%s %%s", icon->command);
1993 return True;
1996 static void reattachIcon(WDock *dock, WAppIcon *icon, int x, int y)
1998 int index;
2000 for (index = 1; index < dock->max_icons; index++) {
2001 if (dock->icon_array[index] == icon)
2002 break;
2004 assert(index < dock->max_icons);
2006 icon->yindex = y;
2007 icon->xindex = x;
2009 icon->x_pos = dock->x_pos + x * ICON_SIZE;
2010 icon->y_pos = dock->y_pos + y * ICON_SIZE;
2013 static Bool moveIconBetweenDocks(WDock *src, WDock *dest, WAppIcon *icon, int x, int y)
2015 WWindow *wwin;
2016 char *command;
2017 int index;
2019 if (src == dest)
2020 return True; /* No move needed, we're already there */
2022 if (dest == NULL)
2023 return False;
2025 wwin = icon->icon->owner;
2028 * For the moment we can't do this if we move icons in Clip from one
2029 * workspace to other, because if we move two or more icons without
2030 * command, the dialog box will not be able to tell us to which of the
2031 * moved icons it applies. -Dan
2033 if ((dest->type == WM_DOCK /*|| dest->keep_attracted */ ) && icon->command == NULL) {
2034 command = GetCommandForWindow(wwin->client_win);
2035 if (command) {
2036 icon->command = command;
2037 } else {
2038 icon->editing = 1;
2039 /* icon->forced_dock = 1; */
2040 if (wInputDialog(src->screen_ptr, _("Dock Icon"),
2041 _("Type the command used to launch the application"), &command)) {
2042 if (command && (command[0] == 0 || (command[0] == '-' && command[1] == 0))) {
2043 wfree(command);
2044 command = NULL;
2046 icon->command = command;
2047 } else {
2048 icon->editing = 0;
2049 if (command)
2050 wfree(command);
2051 return False;
2053 icon->editing = 0;
2057 if (dest->type == WM_DOCK)
2058 wClipMakeIconOmnipresent(icon, False);
2060 for (index = 1; index < src->max_icons; index++) {
2061 if (src->icon_array[index] == icon)
2062 break;
2064 assert(index < src->max_icons);
2066 src->icon_array[index] = NULL;
2067 src->icon_count--;
2069 for (index = 1; index < dest->max_icons; index++) {
2070 if (dest->icon_array[index] == NULL)
2071 break;
2073 /* if (index == dest->max_icons)
2074 return; */
2076 assert(index < dest->max_icons);
2078 dest->icon_array[index] = icon;
2079 icon->dock = dest;
2081 /* deselect the icon */
2082 if (icon->icon->selected)
2083 wIconSelect(icon->icon);
2085 if (dest->type == WM_DOCK) {
2086 icon->icon->core->descriptor.handle_enternotify = NULL;
2087 icon->icon->core->descriptor.handle_leavenotify = NULL;
2088 } else {
2089 icon->icon->core->descriptor.handle_enternotify = clipEnterNotify;
2090 icon->icon->core->descriptor.handle_leavenotify = clipLeaveNotify;
2093 /* set it to be kept when moving to dock.
2094 * Unless the icon does not have a command set
2096 if (icon->command && dest->type == WM_DOCK) {
2097 icon->attracted = 0;
2098 if (icon->icon->shadowed) {
2099 icon->icon->shadowed = 0;
2100 icon->icon->force_paint = 1;
2102 wAppIconSave(icon);
2105 if (src->auto_collapse || src->auto_raise_lower)
2106 clipLeave(src);
2108 icon->yindex = y;
2109 icon->xindex = x;
2111 icon->x_pos = dest->x_pos + x * ICON_SIZE;
2112 icon->y_pos = dest->y_pos + y * ICON_SIZE;
2114 dest->icon_count++;
2116 MoveInStackListUnder(dest->icon_array[index - 1]->icon->core, icon->icon->core);
2117 wAppIconPaint(icon);
2119 return True;
2122 void wDockDetach(WDock *dock, WAppIcon *icon)
2124 int index;
2126 /* make the settings panel be closed */
2127 if (icon->panel) {
2128 DestroyDockAppSettingsPanel(icon->panel);
2131 /* This must be called before icon->dock is set to NULL.
2132 * Don't move it. -Dan
2134 wClipMakeIconOmnipresent(icon, False);
2136 icon->docked = 0;
2137 icon->dock = NULL;
2138 icon->attracted = 0;
2139 icon->auto_launch = 0;
2140 if (icon->icon->shadowed) {
2141 icon->icon->shadowed = 0;
2142 icon->icon->force_paint = 1;
2145 /* deselect the icon */
2146 if (icon->icon->selected)
2147 wIconSelect(icon->icon);
2149 if (icon->command) {
2150 wfree(icon->command);
2151 icon->command = NULL;
2153 #ifdef XDND /* was OFFIX */
2154 if (icon->dnd_command) {
2155 wfree(icon->dnd_command);
2156 icon->dnd_command = NULL;
2158 #endif
2159 if (icon->paste_command) {
2160 wfree(icon->paste_command);
2161 icon->paste_command = NULL;
2164 for (index = 1; index < dock->max_icons; index++)
2165 if (dock->icon_array[index] == icon)
2166 break;
2167 assert(index < dock->max_icons);
2168 dock->icon_array[index] = NULL;
2169 icon->yindex = -1;
2170 icon->xindex = -1;
2172 dock->icon_count--;
2174 /* if the dock is not attached to an application or
2175 * the the application did not set the approriate hints yet,
2176 * destroy the icon */
2177 if (!icon->running || !wApplicationOf(icon->main_window))
2178 wAppIconDestroy(icon);
2179 else {
2180 icon->icon->core->descriptor.handle_mousedown = appIconMouseDown;
2181 icon->icon->core->descriptor.handle_enternotify = NULL;
2182 icon->icon->core->descriptor.handle_leavenotify = NULL;
2183 icon->icon->core->descriptor.parent_type = WCLASS_APPICON;
2184 icon->icon->core->descriptor.parent = icon;
2186 ChangeStackingLevel(icon->icon->core, NORMAL_ICON_LEVEL);
2188 wAppIconPaint(icon);
2189 if (wPreferences.auto_arrange_icons) {
2190 wArrangeIcons(dock->screen_ptr, True);
2193 if (dock->auto_collapse || dock->auto_raise_lower)
2194 clipLeave(dock);
2198 * returns the closest Dock slot index for the passed
2199 * coordinates.
2201 * Returns False if icon can't be docked.
2203 * Note: this function should NEVER alter ret_x or ret_y, unless it will
2204 * return True. -Dan
2206 Bool wDockSnapIcon(WDock *dock, WAppIcon *icon, int req_x, int req_y, int *ret_x, int *ret_y, int redocking)
2208 WScreen *scr = dock->screen_ptr;
2209 int dx, dy;
2210 int ex_x, ex_y;
2211 int i, offset = ICON_SIZE / 2;
2212 WAppIcon *aicon = NULL;
2213 WAppIcon *nicon = NULL;
2214 int max_y_icons;
2216 /* TODO: XINERAMA, for these */
2217 max_y_icons = scr->scr_height / ICON_SIZE - 1;
2219 if (wPreferences.flags.noupdates)
2220 return False;
2222 dx = dock->x_pos;
2223 dy = dock->y_pos;
2225 /* if the dock is full */
2226 if (!redocking && (dock->icon_count >= dock->max_icons)) {
2227 return False;
2230 /* exact position */
2231 if (req_y < dy)
2232 ex_y = (req_y - offset - dy) / ICON_SIZE;
2233 else
2234 ex_y = (req_y + offset - dy) / ICON_SIZE;
2236 if (req_x < dx)
2237 ex_x = (req_x - offset - dx) / ICON_SIZE;
2238 else
2239 ex_x = (req_x + offset - dx) / ICON_SIZE;
2241 /* check if the icon is outside the screen boundaries */
2243 WMRect rect;
2244 int flags;
2246 rect.pos.x = dx + ex_x * ICON_SIZE;
2247 rect.pos.y = dy + ex_y * ICON_SIZE;
2248 rect.size.width = rect.size.height = ICON_SIZE;
2250 wGetRectPlacementInfo(scr, rect, &flags);
2251 if (flags & (XFLAG_DEAD | XFLAG_PARTIAL))
2252 return False;
2255 if (dock->type == WM_DOCK) {
2256 if (icon->dock != dock && ex_x != 0)
2257 return False;
2259 aicon = NULL;
2260 for (i = 0; i < dock->max_icons; i++) {
2261 nicon = dock->icon_array[i];
2262 if (nicon && nicon->yindex == ex_y) {
2263 aicon = nicon;
2264 break;
2268 if (redocking) {
2269 int sig, done, closest;
2271 /* Possible cases when redocking:
2273 * icon dragged out of range of any slot -> false
2274 * icon dragged to range of free slot
2275 * icon dragged to range of same slot
2276 * icon dragged to range of different icon
2278 if (abs(ex_x) > DOCK_DETTACH_THRESHOLD)
2279 return False;
2281 if (ex_y >= 0 && ex_y <= max_y_icons && (aicon == icon || !aicon)) {
2282 *ret_x = 0;
2283 *ret_y = ex_y;
2284 return True;
2287 /* start looking at the upper slot or lower? */
2288 if (ex_y * ICON_SIZE < (req_y + offset - dy))
2289 sig = 1;
2290 else
2291 sig = -1;
2293 closest = -1;
2294 done = 0;
2295 /* look for closest free slot */
2296 for (i = 0; i < (DOCK_DETTACH_THRESHOLD + 1) * 2 && !done; i++) {
2297 int j;
2299 done = 1;
2300 closest = sig * (i / 2) + ex_y;
2301 /* check if this slot is used */
2302 if (closest >= 0) {
2303 for (j = 0; j < dock->max_icons; j++) {
2304 if (dock->icon_array[j]
2305 && dock->icon_array[j]->yindex == closest) {
2306 /* slot is used by someone else */
2307 if (dock->icon_array[j] != icon)
2308 done = 0;
2309 break;
2313 sig = -sig;
2315 if (done && closest >= 0 && closest <= max_y_icons &&
2316 ((ex_y >= closest && ex_y - closest < DOCK_DETTACH_THRESHOLD + 1)
2317 || (ex_y < closest && closest - ex_y <= DOCK_DETTACH_THRESHOLD + 1))) {
2318 *ret_x = 0;
2319 *ret_y = closest;
2320 return True;
2322 } else { /* !redocking */
2324 /* if slot is free and the icon is close enough, return it */
2325 if (!aicon && ex_x == 0 && ex_y >= 0 && ex_y <= max_y_icons) {
2326 *ret_x = 0;
2327 *ret_y = ex_y;
2328 return True;
2331 } else { /* CLIP */
2332 int neighbours = 0;
2333 int start, stop, k;
2335 start = icon->omnipresent ? 0 : scr->current_workspace;
2336 stop = icon->omnipresent ? scr->workspace_count : start + 1;
2338 aicon = NULL;
2339 for (k = start; k < stop; k++) {
2340 WDock *tmp = scr->workspaces[k]->clip;
2341 if (!tmp)
2342 continue;
2343 for (i = 0; i < tmp->max_icons; i++) {
2344 nicon = tmp->icon_array[i];
2345 if (nicon && nicon->xindex == ex_x && nicon->yindex == ex_y) {
2346 aicon = nicon;
2347 break;
2350 if (aicon)
2351 break;
2353 for (k = start; k < stop; k++) {
2354 WDock *tmp = scr->workspaces[k]->clip;
2355 if (!tmp)
2356 continue;
2357 for (i = 0; i < tmp->max_icons; i++) {
2358 nicon = tmp->icon_array[i];
2359 if (nicon && nicon != icon && /* Icon can't be it's own neighbour */
2360 (abs(nicon->xindex - ex_x) <= CLIP_ATTACH_VICINITY &&
2361 abs(nicon->yindex - ex_y) <= CLIP_ATTACH_VICINITY)) {
2362 neighbours = 1;
2363 break;
2366 if (neighbours)
2367 break;
2370 if (neighbours && (aicon == NULL || (redocking && aicon == icon))) {
2371 *ret_x = ex_x;
2372 *ret_y = ex_y;
2373 return True;
2376 return False;
2379 static int onScreen(WScreen *scr, int x, int y, int sx, int ex, int sy, int ey)
2381 WMRect rect = wmkrect(x, y, ICON_SIZE, ICON_SIZE);
2382 int flags;
2384 wGetRectPlacementInfo(scr, rect, &flags);
2386 return !(flags & (XFLAG_DEAD | XFLAG_PARTIAL));
2390 * returns true if it can find a free slot in the dock,
2391 * in which case it changes x_pos and y_pos accordingly.
2392 * Else returns false.
2394 Bool wDockFindFreeSlot(WDock *dock, int *x_pos, int *y_pos)
2396 WScreen *scr = dock->screen_ptr;
2397 WAppIcon *btn;
2398 WAppIconChain *chain;
2399 unsigned char *slot_map;
2400 int mwidth;
2401 int r;
2402 int x, y;
2403 int i, done = False;
2404 int corner;
2405 int sx = 0, sy = 0, ex = scr->scr_width, ey = scr->scr_height;
2406 int extra_count = 0;
2408 if (dock->type == WM_CLIP && dock != scr->workspaces[scr->current_workspace]->clip)
2409 extra_count = scr->global_icon_count;
2411 /* if the dock is full */
2412 if (dock->icon_count + extra_count >= dock->max_icons) {
2413 return False;
2416 if (!wPreferences.flags.nodock && scr->dock) {
2417 if (scr->dock->on_right_side)
2418 ex -= ICON_SIZE + DOCK_EXTRA_SPACE;
2419 else
2420 sx += ICON_SIZE + DOCK_EXTRA_SPACE;
2423 if (ex < dock->x_pos)
2424 ex = dock->x_pos;
2425 if (sx > dock->x_pos + ICON_SIZE)
2426 sx = dock->x_pos + ICON_SIZE;
2427 #define C_NONE 0
2428 #define C_NW 1
2429 #define C_NE 2
2430 #define C_SW 3
2431 #define C_SE 4
2433 /* check if clip is in a corner */
2434 if (dock->type == WM_CLIP) {
2435 if (dock->x_pos < 1 && dock->y_pos < 1)
2436 corner = C_NE;
2437 else if (dock->x_pos < 1 && dock->y_pos >= (ey - ICON_SIZE))
2438 corner = C_SE;
2439 else if (dock->x_pos >= (ex - ICON_SIZE) && dock->y_pos >= (ey - ICON_SIZE))
2440 corner = C_SW;
2441 else if (dock->x_pos >= (ex - ICON_SIZE) && dock->y_pos < 1)
2442 corner = C_NW;
2443 else
2444 corner = C_NONE;
2445 } else
2446 corner = C_NONE;
2448 /* If the clip is in the corner, use only slots that are in the border
2449 * of the screen */
2450 if (corner != C_NONE) {
2451 char *hmap, *vmap;
2452 int hcount, vcount;
2454 hcount = WMIN(dock->max_icons, scr->scr_width / ICON_SIZE);
2455 vcount = WMIN(dock->max_icons, scr->scr_height / ICON_SIZE);
2456 hmap = wmalloc(hcount + 1);
2457 memset(hmap, 0, hcount + 1);
2458 vmap = wmalloc(vcount + 1);
2459 memset(vmap, 0, vcount + 1);
2461 /* mark used positions */
2462 switch (corner) {
2463 case C_NE:
2464 for (i = 0; i < dock->max_icons; i++) {
2465 btn = dock->icon_array[i];
2466 if (!btn)
2467 continue;
2469 if (btn->xindex == 0 && btn->yindex > 0 && btn->yindex < vcount)
2470 vmap[btn->yindex] = 1;
2471 else if (btn->yindex == 0 && btn->xindex > 0 && btn->xindex < hcount)
2472 hmap[btn->xindex] = 1;
2474 for (chain = scr->global_icons; chain != NULL; chain = chain->next) {
2475 btn = chain->aicon;
2476 if (btn->xindex == 0 && btn->yindex > 0 && btn->yindex < vcount)
2477 vmap[btn->yindex] = 1;
2478 else if (btn->yindex == 0 && btn->xindex > 0 && btn->xindex < hcount)
2479 hmap[btn->xindex] = 1;
2481 break;
2482 case C_NW:
2483 for (i = 0; i < dock->max_icons; i++) {
2484 btn = dock->icon_array[i];
2485 if (!btn)
2486 continue;
2488 if (btn->xindex == 0 && btn->yindex > 0 && btn->yindex < vcount)
2489 vmap[btn->yindex] = 1;
2490 else if (btn->yindex == 0 && btn->xindex < 0 && btn->xindex > -hcount)
2491 hmap[-btn->xindex] = 1;
2493 for (chain = scr->global_icons; chain != NULL; chain = chain->next) {
2494 btn = chain->aicon;
2495 if (btn->xindex == 0 && btn->yindex > 0 && btn->yindex < vcount)
2496 vmap[btn->yindex] = 1;
2497 else if (btn->yindex == 0 && btn->xindex < 0 && btn->xindex > -hcount)
2498 hmap[-btn->xindex] = 1;
2500 break;
2501 case C_SE:
2502 for (i = 0; i < dock->max_icons; i++) {
2503 btn = dock->icon_array[i];
2504 if (!btn)
2505 continue;
2507 if (btn->xindex == 0 && btn->yindex < 0 && btn->yindex > -vcount)
2508 vmap[-btn->yindex] = 1;
2509 else if (btn->yindex == 0 && btn->xindex > 0 && btn->xindex < hcount)
2510 hmap[btn->xindex] = 1;
2512 for (chain = scr->global_icons; chain != NULL; chain = chain->next) {
2513 btn = chain->aicon;
2514 if (btn->xindex == 0 && btn->yindex < 0 && btn->yindex > -vcount)
2515 vmap[-btn->yindex] = 1;
2516 else if (btn->yindex == 0 && btn->xindex > 0 && btn->xindex < hcount)
2517 hmap[btn->xindex] = 1;
2519 break;
2520 case C_SW:
2521 default:
2522 for (i = 0; i < dock->max_icons; i++) {
2523 btn = dock->icon_array[i];
2524 if (!btn)
2525 continue;
2527 if (btn->xindex == 0 && btn->yindex < 0 && btn->yindex > -vcount)
2528 vmap[-btn->yindex] = 1;
2529 else if (btn->yindex == 0 && btn->xindex < 0 && btn->xindex > -hcount)
2530 hmap[-btn->xindex] = 1;
2532 for (chain = scr->global_icons; chain != NULL; chain = chain->next) {
2533 btn = chain->aicon;
2534 if (btn->xindex == 0 && btn->yindex < 0 && btn->yindex > -vcount)
2535 vmap[-btn->yindex] = 1;
2536 else if (btn->yindex == 0 && btn->xindex < 0 && btn->xindex > -hcount)
2537 hmap[-btn->xindex] = 1;
2540 x = 0;
2541 y = 0;
2542 done = 0;
2543 /* search a vacant slot */
2544 for (i = 1; i < WMAX(vcount, hcount); i++) {
2545 if (i < vcount && vmap[i] == 0) {
2546 /* found a slot */
2547 x = 0;
2548 y = i;
2549 done = 1;
2550 break;
2551 } else if (i < hcount && hmap[i] == 0) {
2552 /* found a slot */
2553 x = i;
2554 y = 0;
2555 done = 1;
2556 break;
2559 wfree(vmap);
2560 wfree(hmap);
2561 /* If found a slot, translate and return */
2562 if (done) {
2563 if (corner == C_NW || corner == C_NE) {
2564 *y_pos = y;
2565 } else {
2566 *y_pos = -y;
2568 if (corner == C_NE || corner == C_SE) {
2569 *x_pos = x;
2570 } else {
2571 *x_pos = -x;
2573 return True;
2575 /* else, try to find a slot somewhere else */
2578 /* a map of mwidth x mwidth would be enough if we allowed icons to be
2579 * placed outside of screen */
2580 mwidth = (int)ceil(sqrt(dock->max_icons));
2582 /* In the worst case (the clip is in the corner of the screen),
2583 * the amount of icons that fit in the clip is smaller.
2584 * Double the map to get a safe value.
2586 mwidth += mwidth;
2588 r = (mwidth - 1) / 2;
2590 slot_map = wmalloc(mwidth * mwidth);
2591 memset(slot_map, 0, mwidth * mwidth);
2593 #define XY2OFS(x,y) (WMAX(abs(x),abs(y)) > r) ? 0 : (((y)+r)*(mwidth)+(x)+r)
2595 /* mark used slots in the map. If the slot falls outside the map
2596 * (for example, when all icons are placed in line), ignore them. */
2597 for (i = 0; i < dock->max_icons; i++) {
2598 btn = dock->icon_array[i];
2599 if (btn)
2600 slot_map[XY2OFS(btn->xindex, btn->yindex)] = 1;
2602 for (chain = scr->global_icons; chain != NULL; chain = chain->next) {
2603 slot_map[XY2OFS(chain->aicon->xindex, chain->aicon->yindex)] = 1;
2605 /* Find closest slot from the center that is free by scanning the
2606 * map from the center to outward in circular passes.
2607 * This will not result in a neat layout, but will be optimal
2608 * in the sense that there will not be holes left.
2610 done = 0;
2611 for (i = 1; i <= r && !done; i++) {
2612 int tx, ty;
2614 /* top and bottom parts of the ring */
2615 for (x = -i; x <= i && !done; x++) {
2616 tx = dock->x_pos + x * ICON_SIZE;
2617 y = -i;
2618 ty = dock->y_pos + y * ICON_SIZE;
2619 if (slot_map[XY2OFS(x, y)] == 0 && onScreen(scr, tx, ty, sx, ex, sy, ey)) {
2620 *x_pos = x;
2621 *y_pos = y;
2622 done = 1;
2623 break;
2625 y = i;
2626 ty = dock->y_pos + y * ICON_SIZE;
2627 if (slot_map[XY2OFS(x, y)] == 0 && onScreen(scr, tx, ty, sx, ex, sy, ey)) {
2628 *x_pos = x;
2629 *y_pos = y;
2630 done = 1;
2631 break;
2634 /* left and right parts of the ring */
2635 for (y = -i + 1; y <= i - 1; y++) {
2636 ty = dock->y_pos + y * ICON_SIZE;
2637 x = -i;
2638 tx = dock->x_pos + x * ICON_SIZE;
2639 if (slot_map[XY2OFS(x, y)] == 0 && onScreen(scr, tx, ty, sx, ex, sy, ey)) {
2640 *x_pos = x;
2641 *y_pos = y;
2642 done = 1;
2643 break;
2645 x = i;
2646 tx = dock->x_pos + x * ICON_SIZE;
2647 if (slot_map[XY2OFS(x, y)] == 0 && onScreen(scr, tx, ty, sx, ex, sy, ey)) {
2648 *x_pos = x;
2649 *y_pos = y;
2650 done = 1;
2651 break;
2655 wfree(slot_map);
2656 #undef XY2OFS
2657 return done;
2660 static void moveDock(WDock *dock, int new_x, int new_y)
2662 WAppIcon *btn;
2663 int i;
2665 dock->x_pos = new_x;
2666 dock->y_pos = new_y;
2667 for (i = 0; i < dock->max_icons; i++) {
2668 btn = dock->icon_array[i];
2669 if (btn) {
2670 btn->x_pos = new_x + btn->xindex * ICON_SIZE;
2671 btn->y_pos = new_y + btn->yindex * ICON_SIZE;
2672 XMoveWindow(dpy, btn->icon->core->window, btn->x_pos, btn->y_pos);
2677 static void swapDock(WDock *dock)
2679 WScreen *scr = dock->screen_ptr;
2680 WAppIcon *btn;
2681 int x, i;
2683 if (dock->on_right_side) {
2684 x = dock->x_pos = scr->scr_width - ICON_SIZE - DOCK_EXTRA_SPACE;
2685 } else {
2686 x = dock->x_pos = DOCK_EXTRA_SPACE;
2689 for (i = 0; i < dock->max_icons; i++) {
2690 btn = dock->icon_array[i];
2691 if (btn) {
2692 btn->x_pos = x;
2693 XMoveWindow(dpy, btn->icon->core->window, btn->x_pos, btn->y_pos);
2697 wScreenUpdateUsableArea(scr);
2700 static pid_t execCommand(WAppIcon *btn, char *command, WSavedState *state)
2702 WScreen *scr = btn->icon->core->screen_ptr;
2703 pid_t pid;
2704 char **argv;
2705 int argc;
2706 char *cmdline;
2708 cmdline = ExpandOptions(scr, command);
2710 if (scr->flags.dnd_data_convertion_status || !cmdline) {
2711 if (cmdline)
2712 wfree(cmdline);
2713 if (state)
2714 wfree(state);
2715 return 0;
2718 wtokensplit(cmdline, &argv, &argc);
2720 if (!argc) {
2721 if (cmdline)
2722 wfree(cmdline);
2723 if (state)
2724 wfree(state);
2725 return 0;
2728 if ((pid = fork()) == 0) {
2729 char **args;
2730 int i;
2732 SetupEnvironment(scr);
2734 #ifdef HAVE_SETSID
2735 setsid();
2736 #endif
2738 args = malloc(sizeof(char *) * (argc + 1));
2739 if (!args)
2740 exit(111);
2741 for (i = 0; i < argc; i++) {
2742 args[i] = argv[i];
2744 args[argc] = NULL;
2745 execvp(argv[0], args);
2746 exit(111);
2748 wtokenfree(argv, argc);
2750 if (pid > 0) {
2751 if (!state) {
2752 state = wmalloc(sizeof(WSavedState));
2753 memset(state, 0, sizeof(WSavedState));
2754 state->hidden = -1;
2755 state->miniaturized = -1;
2756 state->shaded = -1;
2757 if (btn->dock == scr->dock || btn->omnipresent)
2758 state->workspace = -1;
2759 else
2760 state->workspace = scr->current_workspace;
2762 wWindowAddSavedState(btn->wm_instance, btn->wm_class, cmdline, pid, state);
2763 wAddDeathHandler(pid, (WDeathHandler *) trackDeadProcess, btn->dock);
2764 } else if (state) {
2765 wfree(state);
2767 wfree(cmdline);
2768 return pid;
2771 void wDockHideIcons(WDock *dock)
2773 int i;
2775 if (dock == NULL)
2776 return;
2778 for (i = 1; i < dock->max_icons; i++) {
2779 if (dock->icon_array[i])
2780 XUnmapWindow(dpy, dock->icon_array[i]->icon->core->window);
2782 dock->mapped = 0;
2784 dockIconPaint(dock->icon_array[0]);
2787 void wDockShowIcons(WDock *dock)
2789 int i, newlevel;
2790 WAppIcon *btn;
2792 if (dock == NULL)
2793 return;
2795 btn = dock->icon_array[0];
2796 moveDock(dock, btn->x_pos, btn->y_pos);
2798 newlevel = dock->lowered ? WMNormalLevel : WMDockLevel;
2799 ChangeStackingLevel(btn->icon->core, newlevel);
2801 for (i = 1; i < dock->max_icons; i++) {
2802 if (dock->icon_array[i]) {
2803 MoveInStackListAbove(dock->icon_array[i]->icon->core, btn->icon->core);
2804 break;
2808 if (!dock->collapsed) {
2809 for (i = 1; i < dock->max_icons; i++) {
2810 if (dock->icon_array[i]) {
2811 XMapWindow(dpy, dock->icon_array[i]->icon->core->window);
2815 dock->mapped = 1;
2817 dockIconPaint(btn);
2820 void wDockLower(WDock *dock)
2822 int i;
2824 for (i = 0; i < dock->max_icons; i++) {
2825 if (dock->icon_array[i])
2826 wLowerFrame(dock->icon_array[i]->icon->core);
2830 void wDockRaise(WDock *dock)
2832 int i;
2834 for (i = dock->max_icons - 1; i >= 0; i--) {
2835 if (dock->icon_array[i])
2836 wRaiseFrame(dock->icon_array[i]->icon->core);
2840 void wDockRaiseLower(WDock *dock)
2842 if (!dock->icon_array[0]->icon->core->stacking->above
2843 || (dock->icon_array[0]->icon->core->stacking->window_level
2844 != dock->icon_array[0]->icon->core->stacking->above->stacking->window_level))
2845 wDockLower(dock);
2846 else
2847 wDockRaise(dock);
2850 void wDockFinishLaunch(WDock *dock, WAppIcon *icon)
2852 icon->launching = 0;
2853 icon->relaunching = 0;
2854 dockIconPaint(icon);
2857 WAppIcon *wDockFindIconForWindow(WDock *dock, Window window)
2859 WAppIcon *icon;
2860 int i;
2862 for (i = 0; i < dock->max_icons; i++) {
2863 icon = dock->icon_array[i];
2864 if (icon && icon->main_window == window)
2865 return icon;
2867 return NULL;
2870 void wDockTrackWindowLaunch(WDock *dock, Window window)
2872 WAppIcon *icon;
2873 char *wm_class, *wm_instance;
2874 int i;
2875 Bool firstPass = True;
2876 Bool found = False;
2877 char *command = NULL;
2879 command = GetCommandForWindow(window);
2881 if (!PropGetWMClass(window, &wm_class, &wm_instance) || (!wm_class && !wm_instance)) {
2883 if (command)
2884 wfree(command);
2885 return;
2888 retry:
2889 for (i = 0; i < dock->max_icons; i++) {
2890 icon = dock->icon_array[i];
2891 if (!icon)
2892 continue;
2894 /* app is already attached to icon */
2895 if (icon->main_window == window) {
2896 found = True;
2897 break;
2900 if ((icon->wm_instance || icon->wm_class)
2901 && (icon->launching || !icon->running)) {
2903 if (icon->wm_instance && wm_instance && strcmp(icon->wm_instance, wm_instance) != 0) {
2904 continue;
2906 if (icon->wm_class && wm_class && strcmp(icon->wm_class, wm_class) != 0) {
2907 continue;
2909 if (firstPass && command && strcmp(icon->command, command) != 0) {
2910 continue;
2913 if (!icon->relaunching) {
2914 WApplication *wapp;
2916 /* Possibly an application that was docked with dockit,
2917 * but the user did not update WMState to indicate that
2918 * it was docked by force */
2919 wapp = wApplicationOf(window);
2920 if (!wapp) {
2921 icon->forced_dock = 1;
2922 icon->running = 0;
2924 if (!icon->forced_dock) {
2925 icon->main_window = window;
2928 found = True;
2929 if (!wPreferences.no_animations && !icon->launching &&
2930 !dock->screen_ptr->flags.startup && !dock->collapsed) {
2931 WAppIcon *aicon;
2932 int x0, y0;
2934 icon->launching = 1;
2935 dockIconPaint(icon);
2937 aicon = wAppIconCreateForDock(dock->screen_ptr, NULL,
2938 wm_instance, wm_class, TILE_NORMAL);
2939 /* XXX: can: aicon->icon == NULL ? */
2940 PlaceIcon(dock->screen_ptr, &x0, &y0, wGetHeadForWindow(aicon->icon->owner));
2941 wAppIconMove(aicon, x0, y0);
2942 /* Should this always be lowered? -Dan */
2943 if (dock->lowered)
2944 wLowerFrame(aicon->icon->core);
2945 XMapWindow(dpy, aicon->icon->core->window);
2946 aicon->launching = 1;
2947 wAppIconPaint(aicon);
2948 SlideWindow(aicon->icon->core->window, x0, y0, icon->x_pos, icon->y_pos);
2949 XUnmapWindow(dpy, aicon->icon->core->window);
2950 wAppIconDestroy(aicon);
2952 wDockFinishLaunch(dock, icon);
2953 break;
2957 if (firstPass && !found) {
2958 firstPass = False;
2959 goto retry;
2962 if (command)
2963 wfree(command);
2965 if (wm_class)
2966 free(wm_class);
2967 if (wm_instance)
2968 free(wm_instance);
2971 void wClipUpdateForWorkspaceChange(WScreen *scr, int workspace)
2973 if (!wPreferences.flags.noclip) {
2974 scr->clip_icon->dock = scr->workspaces[workspace]->clip;
2975 if (scr->current_workspace != workspace) {
2976 WDock *old_clip = scr->workspaces[scr->current_workspace]->clip;
2977 WAppIconChain *chain = scr->global_icons;
2979 while (chain) {
2980 moveIconBetweenDocks(chain->aicon->dock,
2981 scr->workspaces[workspace]->clip,
2982 chain->aicon, chain->aicon->xindex, chain->aicon->yindex);
2983 if (scr->workspaces[workspace]->clip->collapsed)
2984 XUnmapWindow(dpy, chain->aicon->icon->core->window);
2985 chain = chain->next;
2988 wDockHideIcons(old_clip);
2989 if (old_clip->auto_raise_lower) {
2990 if (old_clip->auto_raise_magic) {
2991 WMDeleteTimerHandler(old_clip->auto_raise_magic);
2992 old_clip->auto_raise_magic = NULL;
2994 wDockLower(old_clip);
2996 if (old_clip->auto_collapse) {
2997 if (old_clip->auto_expand_magic) {
2998 WMDeleteTimerHandler(old_clip->auto_expand_magic);
2999 old_clip->auto_expand_magic = NULL;
3001 old_clip->collapsed = 1;
3003 wDockShowIcons(scr->workspaces[workspace]->clip);
3008 static void trackDeadProcess(pid_t pid, unsigned char status, WDock *dock)
3010 WAppIcon *icon;
3011 int i;
3013 for (i = 0; i < dock->max_icons; i++) {
3014 icon = dock->icon_array[i];
3015 if (!icon)
3016 continue;
3018 if (icon->launching && icon->pid == pid) {
3019 if (!icon->relaunching) {
3020 icon->running = 0;
3021 icon->main_window = None;
3023 wDockFinishLaunch(dock, icon);
3024 icon->pid = 0;
3025 if (status == 111) {
3026 char msg[PATH_MAX];
3027 char *cmd;
3029 #ifdef XDND
3030 if (icon->drop_launch)
3031 cmd = icon->dnd_command;
3032 else
3033 #endif
3034 if (icon->paste_launch)
3035 cmd = icon->paste_command;
3036 else
3037 cmd = icon->command;
3039 snprintf(msg, sizeof(msg), _("Could not execute command \"%s\""), cmd);
3041 wMessageDialog(dock->screen_ptr, _("Error"), msg, _("OK"), NULL, NULL);
3043 break;
3048 static void toggleLowered(WDock *dock)
3050 WAppIcon *tmp;
3051 int newlevel, i;
3053 /* lower/raise Dock */
3054 if (!dock->lowered) {
3055 newlevel = WMNormalLevel;
3056 dock->lowered = 1;
3057 } else {
3058 newlevel = WMDockLevel;
3059 dock->lowered = 0;
3062 for (i = 0; i < dock->max_icons; i++) {
3063 tmp = dock->icon_array[i];
3064 if (!tmp)
3065 continue;
3067 ChangeStackingLevel(tmp->icon->core, newlevel);
3068 if (dock->lowered)
3069 wLowerFrame(tmp->icon->core);
3072 if (dock->type == WM_DOCK)
3073 wScreenUpdateUsableArea(dock->screen_ptr);
3076 static void toggleCollapsed(WDock *dock)
3078 if (dock->collapsed) {
3079 dock->collapsed = 0;
3080 wDockShowIcons(dock);
3081 } else {
3082 dock->collapsed = 1;
3083 wDockHideIcons(dock);
3087 static void openDockMenu(WDock *dock, WAppIcon *aicon, XEvent *event)
3089 WScreen *scr = dock->screen_ptr;
3090 WObjDescriptor *desc;
3091 WMenuEntry *entry;
3092 WApplication *wapp = NULL;
3093 int index = 0;
3094 int x_pos;
3095 int n_selected;
3096 int appIsRunning = aicon->running && aicon->icon && aicon->icon->owner;
3098 if (dock->type == WM_DOCK) {
3099 /* keep on top */
3100 entry = dock->menu->entries[index];
3101 entry->flags.indicator_on = !dock->lowered;
3102 entry->clientdata = dock;
3103 dock->menu->flags.realized = 0;
3104 } else {
3105 /* clip options */
3106 if (scr->clip_options)
3107 updateClipOptionsMenu(scr->clip_options, dock);
3109 n_selected = numberOfSelectedIcons(dock);
3111 /* Rename Workspace */
3112 entry = dock->menu->entries[++index];
3113 if (aicon == scr->clip_icon) {
3114 entry->callback = renameCallback;
3115 entry->clientdata = dock;
3116 entry->flags.indicator = 0;
3117 entry->text = _("Rename Workspace");
3118 } else {
3119 entry->callback = omnipresentCallback;
3120 entry->clientdata = aicon;
3121 if (n_selected > 0) {
3122 entry->flags.indicator = 0;
3123 entry->text = _("Toggle Omnipresent");
3124 } else {
3125 entry->flags.indicator = 1;
3126 entry->flags.indicator_on = aicon->omnipresent;
3127 entry->flags.indicator_type = MI_CHECK;
3128 entry->text = _("Omnipresent");
3132 /* select/unselect icon */
3133 entry = dock->menu->entries[++index];
3134 entry->clientdata = aicon;
3135 entry->flags.indicator_on = aicon->icon->selected;
3136 wMenuSetEnabled(dock->menu, index, aicon != scr->clip_icon);
3138 /* select/unselect all icons */
3139 entry = dock->menu->entries[++index];
3140 entry->clientdata = aicon;
3141 if (n_selected > 0)
3142 entry->text = _("Unselect All Icons");
3143 else
3144 entry->text = _("Select All Icons");
3145 wMenuSetEnabled(dock->menu, index, dock->icon_count > 1);
3147 /* keep icon(s) */
3148 entry = dock->menu->entries[++index];
3149 entry->clientdata = aicon;
3150 if (n_selected > 1)
3151 entry->text = _("Keep Icons");
3152 else
3153 entry->text = _("Keep Icon");
3154 wMenuSetEnabled(dock->menu, index, dock->icon_count > 1);
3156 /* this is the workspace submenu part */
3157 entry = dock->menu->entries[++index];
3158 if (n_selected > 1)
3159 entry->text = _("Move Icons To");
3160 else
3161 entry->text = _("Move Icon To");
3162 if (scr->clip_submenu)
3163 updateWorkspaceMenu(scr->clip_submenu, aicon);
3164 wMenuSetEnabled(dock->menu, index, !aicon->omnipresent);
3166 /* remove icon(s) */
3167 entry = dock->menu->entries[++index];
3168 entry->clientdata = aicon;
3169 if (n_selected > 1)
3170 entry->text = _("Remove Icons");
3171 else
3172 entry->text = _("Remove Icon");
3173 wMenuSetEnabled(dock->menu, index, dock->icon_count > 1);
3175 /* attract icon(s) */
3176 entry = dock->menu->entries[++index];
3177 entry->clientdata = aicon;
3179 dock->menu->flags.realized = 0;
3180 wMenuRealize(dock->menu);
3183 if (aicon->icon->owner) {
3184 wapp = wApplicationOf(aicon->icon->owner->main_window);
3185 } else {
3186 wapp = NULL;
3189 /* launch */
3190 entry = dock->menu->entries[++index];
3191 entry->clientdata = aicon;
3192 wMenuSetEnabled(dock->menu, index, aicon->command != NULL);
3194 /* unhide here */
3195 entry = dock->menu->entries[++index];
3196 entry->clientdata = aicon;
3197 if (wapp && wapp->flags.hidden) {
3198 entry->text = _("Unhide Here");
3199 } else {
3200 entry->text = _("Bring Here");
3202 wMenuSetEnabled(dock->menu, index, appIsRunning);
3204 /* hide */
3205 entry = dock->menu->entries[++index];
3206 entry->clientdata = aicon;
3207 if (wapp && wapp->flags.hidden) {
3208 entry->text = _("Unhide");
3209 } else {
3210 entry->text = _("Hide");
3212 wMenuSetEnabled(dock->menu, index, appIsRunning);
3214 /* settings */
3215 entry = dock->menu->entries[++index];
3216 entry->clientdata = aicon;
3217 wMenuSetEnabled(dock->menu, index, !aicon->editing && !wPreferences.flags.noupdates);
3219 /* kill */
3220 entry = dock->menu->entries[++index];
3221 entry->clientdata = aicon;
3222 wMenuSetEnabled(dock->menu, index, appIsRunning);
3224 if (!dock->menu->flags.realized)
3225 wMenuRealize(dock->menu);
3227 if (dock->type == WM_CLIP) {
3228 /*x_pos = event->xbutton.x_root+2; */
3229 x_pos = event->xbutton.x_root - dock->menu->frame->core->width / 2 - 1;
3230 if (x_pos < 0) {
3231 x_pos = 0;
3232 } else if (x_pos + dock->menu->frame->core->width > scr->scr_width - 2) {
3233 x_pos = scr->scr_width - dock->menu->frame->core->width - 4;
3235 } else {
3236 x_pos = dock->on_right_side ? scr->scr_width - dock->menu->frame->core->width - 3 : 0;
3239 wMenuMapAt(dock->menu, x_pos, event->xbutton.y_root + 2, False);
3241 /* allow drag select */
3242 event->xany.send_event = True;
3243 desc = &dock->menu->menu->descriptor;
3244 (*desc->handle_mousedown) (desc, event);
3247 /******************************************************************/
3248 static void iconDblClick(WObjDescriptor *desc, XEvent *event)
3250 WAppIcon *btn = desc->parent;
3251 WDock *dock = btn->dock;
3252 WApplication *wapp = NULL;
3253 int unhideHere = 0;
3255 if (btn->icon->owner && !(event->xbutton.state & ControlMask)) {
3256 wapp = wApplicationOf(btn->icon->owner->main_window);
3258 assert(wapp != NULL);
3260 unhideHere = (event->xbutton.state & ShiftMask);
3262 /* go to the last workspace that the user worked on the app */
3263 if (wapp->last_workspace != dock->screen_ptr->current_workspace && !unhideHere) {
3264 wWorkspaceChange(dock->screen_ptr, wapp->last_workspace);
3267 wUnhideApplication(wapp, event->xbutton.button == Button2, unhideHere);
3269 if (event->xbutton.state & MOD_MASK) {
3270 wHideOtherApplications(btn->icon->owner);
3272 } else {
3273 if (event->xbutton.button == Button1) {
3275 if (event->xbutton.state & MOD_MASK) {
3276 /* raise/lower dock */
3277 toggleLowered(dock);
3278 } else if (btn == dock->screen_ptr->clip_icon) {
3279 if (getClipButton(event->xbutton.x, event->xbutton.y) == CLIP_IDLE)
3280 toggleCollapsed(dock);
3281 else
3282 handleClipChangeWorkspace(dock->screen_ptr, event);
3283 } else if (btn->command) {
3284 if (!btn->launching && (!btn->running || (event->xbutton.state & ControlMask))) {
3285 launchDockedApplication(btn, False);
3287 } else if (btn->xindex == 0 && btn->yindex == 0 && btn->dock->type == WM_DOCK) {
3288 wShowGNUstepPanel(dock->screen_ptr);
3294 static void handleDockMove(WDock *dock, WAppIcon *aicon, XEvent *event)
3296 WScreen *scr = dock->screen_ptr;
3297 int ofs_x = event->xbutton.x, ofs_y = event->xbutton.y;
3298 int x, y;
3299 XEvent ev;
3300 int grabbed = 0, swapped = 0, done;
3301 Pixmap ghost = None;
3302 int superfluous = wPreferences.superfluous; /* we catch it to avoid problems */
3304 if (XGrabPointer(dpy, aicon->icon->core->window, True, ButtonMotionMask
3305 | ButtonReleaseMask | ButtonPressMask, GrabModeAsync,
3306 GrabModeAsync, None, None, CurrentTime) != GrabSuccess) {
3307 wwarning("pointer grab failed for dock move");
3309 y = 0;
3310 for (x = 0; x < dock->max_icons; x++) {
3311 if (dock->icon_array[x] != NULL && dock->icon_array[x]->yindex > y)
3312 y = dock->icon_array[x]->yindex;
3314 y++;
3315 XResizeWindow(dpy, scr->dock_shadow, ICON_SIZE, ICON_SIZE * y);
3317 done = 0;
3318 while (!done) {
3319 WMMaskEvent(dpy, PointerMotionMask | ButtonReleaseMask | ButtonPressMask
3320 | ButtonMotionMask | ExposureMask | EnterWindowMask, &ev);
3321 switch (ev.type) {
3322 case Expose:
3323 WMHandleEvent(&ev);
3324 break;
3326 case EnterNotify:
3327 /* It means the cursor moved so fast that it entered
3328 * something else (if moving slowly, it would have
3329 * stayed in the dock that is being moved. Ignore such
3330 * "spurious" EnterNotifiy's */
3331 break;
3333 case MotionNotify:
3334 if (!grabbed) {
3335 if (abs(ofs_x - ev.xmotion.x) >= MOVE_THRESHOLD
3336 || abs(ofs_y - ev.xmotion.y) >= MOVE_THRESHOLD) {
3337 XChangeActivePointerGrab(dpy, ButtonMotionMask
3338 | ButtonReleaseMask | ButtonPressMask,
3339 wCursor[WCUR_MOVE], CurrentTime);
3340 grabbed = 1;
3342 break;
3344 if (dock->type == WM_CLIP) {
3345 x = ev.xmotion.x_root - ofs_x;
3346 y = ev.xmotion.y_root - ofs_y;
3347 wScreenKeepInside(scr, &x, &y, ICON_SIZE, ICON_SIZE);
3349 moveDock(dock, x, y);
3350 } else {
3351 /* move vertically if pointer is inside the dock */
3352 if ((dock->on_right_side && ev.xmotion.x_root >= dock->x_pos - ICON_SIZE)
3353 || (!dock->on_right_side && ev.xmotion.x_root <= dock->x_pos + ICON_SIZE * 2)) {
3355 x = ev.xmotion.x_root - ofs_x;
3356 y = ev.xmotion.y_root - ofs_y;
3357 wScreenKeepInside(scr, &x, &y, ICON_SIZE, ICON_SIZE);
3358 moveDock(dock, dock->x_pos, y);
3360 /* move horizontally to change sides */
3361 x = ev.xmotion.x_root - ofs_x;
3362 if (!dock->on_right_side) {
3364 /* is on left */
3366 if (ev.xmotion.x_root > dock->x_pos + ICON_SIZE * 2) {
3367 XMoveWindow(dpy, scr->dock_shadow, scr->scr_width - ICON_SIZE
3368 - DOCK_EXTRA_SPACE - 1, dock->y_pos);
3369 if (superfluous && ghost == None) {
3370 ghost = MakeGhostDock(dock, dock->x_pos,
3371 scr->scr_width - ICON_SIZE
3372 - DOCK_EXTRA_SPACE - 1, dock->y_pos);
3373 XSetWindowBackgroundPixmap(dpy, scr->dock_shadow, ghost);
3374 XClearWindow(dpy, scr->dock_shadow);
3376 XMapRaised(dpy, scr->dock_shadow);
3377 swapped = 1;
3378 } else {
3379 if (superfluous && ghost != None) {
3380 XFreePixmap(dpy, ghost);
3381 ghost = None;
3383 XUnmapWindow(dpy, scr->dock_shadow);
3384 swapped = 0;
3386 } else {
3387 /* is on right */
3388 if (ev.xmotion.x_root < dock->x_pos - ICON_SIZE) {
3389 XMoveWindow(dpy, scr->dock_shadow, DOCK_EXTRA_SPACE, dock->y_pos);
3390 if (superfluous && ghost == None) {
3391 ghost = MakeGhostDock(dock, dock->x_pos,
3392 DOCK_EXTRA_SPACE, dock->y_pos);
3393 XSetWindowBackgroundPixmap(dpy, scr->dock_shadow, ghost);
3394 XClearWindow(dpy, scr->dock_shadow);
3396 XMapRaised(dpy, scr->dock_shadow);
3397 swapped = -1;
3398 } else {
3399 XUnmapWindow(dpy, scr->dock_shadow);
3400 swapped = 0;
3401 if (superfluous && ghost != None) {
3402 XFreePixmap(dpy, ghost);
3403 ghost = None;
3408 break;
3410 case ButtonPress:
3411 break;
3413 case ButtonRelease:
3414 if (ev.xbutton.button != event->xbutton.button)
3415 break;
3416 XUngrabPointer(dpy, CurrentTime);
3417 XUnmapWindow(dpy, scr->dock_shadow);
3418 XResizeWindow(dpy, scr->dock_shadow, ICON_SIZE, ICON_SIZE);
3419 if (dock->type == WM_DOCK) {
3420 if (swapped != 0) {
3421 if (swapped > 0)
3422 dock->on_right_side = 1;
3423 else
3424 dock->on_right_side = 0;
3425 swapDock(dock);
3426 wArrangeIcons(scr, False);
3429 done = 1;
3430 break;
3433 if (superfluous) {
3434 if (ghost != None)
3435 XFreePixmap(dpy, ghost);
3436 XSetWindowBackground(dpy, scr->dock_shadow, scr->white_pixel);
3440 static Bool handleIconMove(WDock *dock, WAppIcon *aicon, XEvent *event)
3442 WScreen *scr = dock->screen_ptr;
3443 Window wins[2];
3444 WIcon *icon = aicon->icon;
3445 WDock *dock2 = NULL, *last_dock = dock, *clip = NULL;
3446 int ondock, grabbed = 0, change_dock = 0, collapsed = 0;
3447 XEvent ev;
3448 int x = aicon->x_pos, y = aicon->y_pos;
3449 int ofs_x = event->xbutton.x, ofs_y = event->xbutton.y;
3450 int shad_x = x, shad_y = y;
3451 int ix = aicon->xindex, iy = aicon->yindex;
3452 int tmp;
3453 Pixmap ghost = None;
3454 Bool docked;
3455 int superfluous = wPreferences.superfluous; /* we catch it to avoid problems */
3456 int omnipresent = aicon->omnipresent; /* this must be cached!!! */
3457 Bool hasMoved = False;
3459 if (wPreferences.flags.noupdates)
3460 return hasMoved;
3462 if (XGrabPointer(dpy, icon->core->window, True, ButtonMotionMask
3463 | ButtonReleaseMask | ButtonPressMask, GrabModeAsync,
3464 GrabModeAsync, None, None, CurrentTime) != GrabSuccess) {
3467 if (!(event->xbutton.state & MOD_MASK))
3468 wRaiseFrame(icon->core);
3470 if (!wPreferences.flags.noclip)
3471 clip = scr->workspaces[scr->current_workspace]->clip;
3473 if (dock == scr->dock && !wPreferences.flags.noclip)
3474 dock2 = clip;
3475 else if (dock != scr->dock && !wPreferences.flags.nodock)
3476 dock2 = scr->dock;
3478 wins[0] = icon->core->window;
3479 wins[1] = scr->dock_shadow;
3480 XRestackWindows(dpy, wins, 2);
3481 XMoveResizeWindow(dpy, scr->dock_shadow, aicon->x_pos, aicon->y_pos, ICON_SIZE, ICON_SIZE);
3482 if (superfluous) {
3483 if (icon->pixmap != None)
3484 ghost = MakeGhostIcon(scr, icon->pixmap);
3485 else
3486 ghost = MakeGhostIcon(scr, icon->core->window);
3488 XSetWindowBackgroundPixmap(dpy, scr->dock_shadow, ghost);
3489 XClearWindow(dpy, scr->dock_shadow);
3491 XMapWindow(dpy, scr->dock_shadow);
3493 ondock = 1;
3495 while (1) {
3496 XMaskEvent(dpy, PointerMotionMask | ButtonReleaseMask | ButtonPressMask
3497 | ButtonMotionMask | ExposureMask, &ev);
3498 switch (ev.type) {
3499 case Expose:
3500 WMHandleEvent(&ev);
3501 break;
3503 case MotionNotify:
3504 hasMoved = True;
3505 if (!grabbed) {
3506 if (abs(ofs_x - ev.xmotion.x) >= MOVE_THRESHOLD
3507 || abs(ofs_y - ev.xmotion.y) >= MOVE_THRESHOLD) {
3508 XChangeActivePointerGrab(dpy, ButtonMotionMask
3509 | ButtonReleaseMask | ButtonPressMask,
3510 wCursor[WCUR_MOVE], CurrentTime);
3511 grabbed = 1;
3512 } else {
3513 break;
3517 if (omnipresent) {
3518 int i;
3519 for (i = 0; i < scr->workspace_count; i++) {
3520 if (i == scr->current_workspace)
3521 continue;
3522 wDockShowIcons(scr->workspaces[i]->clip);
3526 x = ev.xmotion.x_root - ofs_x;
3527 y = ev.xmotion.y_root - ofs_y;
3528 tmp = wDockSnapIcon(dock, aicon, x, y, &ix, &iy, True);
3529 if (tmp && dock2) {
3530 change_dock = 0;
3531 if (last_dock != dock && collapsed) {
3532 last_dock->collapsed = 1;
3533 wDockHideIcons(last_dock);
3534 collapsed = 0;
3536 if (!collapsed && (collapsed = dock->collapsed)) {
3537 dock->collapsed = 0;
3538 wDockShowIcons(dock);
3540 if (dock->auto_raise_lower)
3541 wDockRaise(dock);
3542 last_dock = dock;
3543 } else if (dock2) {
3544 tmp = wDockSnapIcon(dock2, aicon, x, y, &ix, &iy, False);
3545 if (tmp) {
3546 change_dock = 1;
3547 if (last_dock != dock2 && collapsed) {
3548 last_dock->collapsed = 1;
3549 wDockHideIcons(last_dock);
3550 collapsed = 0;
3552 if (!collapsed && (collapsed = dock2->collapsed)) {
3553 dock2->collapsed = 0;
3554 wDockShowIcons(dock2);
3556 if (dock2->auto_raise_lower)
3557 wDockRaise(dock2);
3558 last_dock = dock2;
3561 if (aicon->launching || aicon->lock || (aicon->running && !(ev.xmotion.state & MOD_MASK))
3562 || (!aicon->running && tmp)) {
3563 shad_x = last_dock->x_pos + ix * wPreferences.icon_size;
3564 shad_y = last_dock->y_pos + iy * wPreferences.icon_size;
3566 XMoveWindow(dpy, scr->dock_shadow, shad_x, shad_y);
3568 if (!ondock) {
3569 XMapWindow(dpy, scr->dock_shadow);
3571 ondock = 1;
3572 } else {
3573 if (ondock) {
3574 XUnmapWindow(dpy, scr->dock_shadow);
3576 ondock = 0;
3578 XMoveWindow(dpy, icon->core->window, x, y);
3579 break;
3581 case ButtonPress:
3582 break;
3584 case ButtonRelease:
3585 if (ev.xbutton.button != event->xbutton.button)
3586 break;
3587 XUngrabPointer(dpy, CurrentTime);
3588 if (ondock) {
3589 SlideWindow(icon->core->window, x, y, shad_x, shad_y);
3590 XUnmapWindow(dpy, scr->dock_shadow);
3591 if (!change_dock) {
3592 reattachIcon(dock, aicon, ix, iy);
3593 if (clip && dock != clip && clip->auto_raise_lower)
3594 wDockLower(clip);
3595 } else {
3596 docked = moveIconBetweenDocks(dock, dock2, aicon, ix, iy);
3597 if (!docked) {
3598 /* Slide it back if dock rejected it */
3599 SlideWindow(icon->core->window, x, y, aicon->x_pos, aicon->y_pos);
3600 reattachIcon(dock, aicon, aicon->xindex, aicon->yindex);
3602 if (last_dock->type == WM_CLIP && last_dock->auto_collapse) {
3603 collapsed = 0;
3606 } else {
3607 aicon->x_pos = x;
3608 aicon->y_pos = y;
3609 if (superfluous) {
3610 if (!aicon->running && !wPreferences.no_animations) {
3611 /* We need to deselect it, even if is deselected in
3612 * wDockDetach(), because else DoKaboom() will fail.
3614 if (aicon->icon->selected)
3615 wIconSelect(aicon->icon);
3617 DoKaboom(scr, aicon->icon->core->window, x, y);
3620 if (clip && clip->auto_raise_lower)
3621 wDockLower(clip);
3622 wDockDetach(dock, aicon);
3624 if (collapsed) {
3625 last_dock->collapsed = 1;
3626 wDockHideIcons(last_dock);
3627 collapsed = 0;
3629 if (superfluous) {
3630 if (ghost != None)
3631 XFreePixmap(dpy, ghost);
3632 XSetWindowBackground(dpy, scr->dock_shadow, scr->white_pixel);
3634 if (omnipresent) {
3635 int i;
3636 for (i = 0; i < scr->workspace_count; i++) {
3637 if (i == scr->current_workspace)
3638 continue;
3639 wDockHideIcons(scr->workspaces[i]->clip);
3642 return hasMoved;;
3645 return False; /* never reached */
3648 static int getClipButton(int px, int py)
3650 int pt = (CLIP_BUTTON_SIZE + 2) * ICON_SIZE / 64;
3652 if (px < 0 || py < 0 || px >= ICON_SIZE || py >= ICON_SIZE)
3653 return CLIP_IDLE;
3655 if (py <= pt - ((int)ICON_SIZE - 1 - px))
3656 return CLIP_FORWARD;
3657 else if (px <= pt - ((int)ICON_SIZE - 1 - py))
3658 return CLIP_REWIND;
3660 return CLIP_IDLE;
3663 static void handleClipChangeWorkspace(WScreen *scr, XEvent *event)
3665 XEvent ev;
3666 int done, direction, new_ws;
3667 int new_dir;
3668 WDock *clip = scr->clip_icon->dock;
3670 direction = getClipButton(event->xbutton.x, event->xbutton.y);
3672 clip->lclip_button_pushed = direction == CLIP_REWIND;
3673 clip->rclip_button_pushed = direction == CLIP_FORWARD;
3675 wClipIconPaint(scr->clip_icon);
3676 done = 0;
3677 while (!done) {
3678 WMMaskEvent(dpy, ExposureMask | ButtonMotionMask | ButtonReleaseMask | ButtonPressMask, &ev);
3679 switch (ev.type) {
3680 case Expose:
3681 WMHandleEvent(&ev);
3682 break;
3684 case MotionNotify:
3685 new_dir = getClipButton(ev.xmotion.x, ev.xmotion.y);
3686 if (new_dir != direction) {
3687 direction = new_dir;
3688 clip->lclip_button_pushed = direction == CLIP_REWIND;
3689 clip->rclip_button_pushed = direction == CLIP_FORWARD;
3690 wClipIconPaint(scr->clip_icon);
3692 break;
3694 case ButtonPress:
3695 break;
3697 case ButtonRelease:
3698 if (ev.xbutton.button == event->xbutton.button)
3699 done = 1;
3703 clip->lclip_button_pushed = 0;
3704 clip->rclip_button_pushed = 0;
3706 new_ws = wPreferences.ws_advance || (event->xbutton.state & ControlMask);
3708 if (direction == CLIP_FORWARD) {
3709 if (scr->current_workspace < scr->workspace_count - 1)
3710 wWorkspaceChange(scr, scr->current_workspace + 1);
3711 else if (new_ws && scr->current_workspace < MAX_WORKSPACES - 1)
3712 wWorkspaceChange(scr, scr->current_workspace + 1);
3713 else if (wPreferences.ws_cycle)
3714 wWorkspaceChange(scr, 0);
3715 } else if (direction == CLIP_REWIND) {
3716 if (scr->current_workspace > 0)
3717 wWorkspaceChange(scr, scr->current_workspace - 1);
3718 else if (scr->current_workspace == 0 && wPreferences.ws_cycle)
3719 wWorkspaceChange(scr, scr->workspace_count - 1);
3722 wClipIconPaint(scr->clip_icon);
3725 static void iconMouseDown(WObjDescriptor *desc, XEvent *event)
3727 WAppIcon *aicon = desc->parent;
3728 WDock *dock = aicon->dock;
3729 WScreen *scr = aicon->icon->core->screen_ptr;
3731 if (aicon->editing || WCHECK_STATE(WSTATE_MODAL))
3732 return;
3734 scr->last_dock = dock;
3736 if (dock->menu->flags.mapped)
3737 wMenuUnmap(dock->menu);
3739 if (IsDoubleClick(scr, event)) {
3740 /* double-click was not in the main clip icon */
3741 if (dock->type != WM_CLIP || aicon->xindex != 0 || aicon->yindex != 0
3742 || getClipButton(event->xbutton.x, event->xbutton.y) == CLIP_IDLE) {
3743 iconDblClick(desc, event);
3744 return;
3748 if (event->xbutton.button == Button1) {
3749 if (event->xbutton.state & MOD_MASK)
3750 wDockLower(dock);
3751 else
3752 wDockRaise(dock);
3754 if ((event->xbutton.state & ShiftMask) && aicon != scr->clip_icon && dock->type != WM_DOCK) {
3755 wIconSelect(aicon->icon);
3756 return;
3759 if (aicon->yindex == 0 && aicon->xindex == 0) {
3760 if (getClipButton(event->xbutton.x, event->xbutton.y) != CLIP_IDLE
3761 && dock->type == WM_CLIP)
3762 handleClipChangeWorkspace(scr, event);
3763 else
3764 handleDockMove(dock, aicon, event);
3765 } else {
3766 Bool hasMoved = handleIconMove(dock, aicon, event);
3767 if (wPreferences.single_click && !hasMoved)
3768 iconDblClick(desc, event);
3770 } else if (event->xbutton.button == Button2 && dock->type == WM_CLIP && aicon == scr->clip_icon) {
3771 if (!scr->clip_ws_menu) {
3772 scr->clip_ws_menu = wWorkspaceMenuMake(scr, False);
3774 if (scr->clip_ws_menu) {
3775 WMenu *wsMenu = scr->clip_ws_menu;
3776 int xpos;
3778 wWorkspaceMenuUpdate(scr, wsMenu);
3780 xpos = event->xbutton.x_root - wsMenu->frame->core->width / 2 - 1;
3781 if (xpos < 0) {
3782 xpos = 0;
3783 } else if (xpos + wsMenu->frame->core->width > scr->scr_width - 2) {
3784 xpos = scr->scr_width - wsMenu->frame->core->width - 4;
3786 wMenuMapAt(wsMenu, xpos, event->xbutton.y_root + 2, False);
3788 desc = &wsMenu->menu->descriptor;
3789 event->xany.send_event = True;
3790 (*desc->handle_mousedown) (desc, event);
3792 } else if (event->xbutton.button == Button2 && dock->type == WM_CLIP &&
3793 (event->xbutton.state & ShiftMask) && aicon != scr->clip_icon) {
3794 wClipMakeIconOmnipresent(aicon, !aicon->omnipresent);
3795 } else if (event->xbutton.button == Button3) {
3796 if (event->xbutton.send_event &&
3797 XGrabPointer(dpy, aicon->icon->core->window, True, ButtonMotionMask
3798 | ButtonReleaseMask | ButtonPressMask, GrabModeAsync,
3799 GrabModeAsync, None, None, CurrentTime) != GrabSuccess) {
3800 wwarning("pointer grab failed for dockicon menu");
3801 return;
3804 openDockMenu(dock, aicon, event);
3805 } else if (event->xbutton.button == Button2) {
3806 WAppIcon *btn = desc->parent;
3808 if (!btn->launching && (!btn->running || (event->xbutton.state & ControlMask))) {
3809 launchDockedApplication(btn, True);
3814 static void clipEnterNotify(WObjDescriptor *desc, XEvent *event)
3816 WAppIcon *btn = (WAppIcon *) desc->parent;
3817 WDock *dock;
3818 WScreen *scr;
3820 assert(event->type == EnterNotify);
3822 if (desc->parent_type != WCLASS_DOCK_ICON)
3823 return;
3825 scr = btn->icon->core->screen_ptr;
3826 if (!btn->omnipresent)
3827 dock = btn->dock;
3828 else
3829 dock = scr->workspaces[scr->current_workspace]->clip;
3831 if (!dock || dock->type != WM_CLIP)
3832 return;
3834 /* The auto raise/lower code */
3835 if (dock->auto_lower_magic) {
3836 WMDeleteTimerHandler(dock->auto_lower_magic);
3837 dock->auto_lower_magic = NULL;
3839 if (dock->auto_raise_lower && !dock->auto_raise_magic) {
3840 dock->auto_raise_magic = WMAddTimerHandler(AUTO_RAISE_DELAY, clipAutoRaise, (void *)dock);
3843 /* The auto expand/collapse code */
3844 if (dock->auto_collapse_magic) {
3845 WMDeleteTimerHandler(dock->auto_collapse_magic);
3846 dock->auto_collapse_magic = NULL;
3848 if (dock->auto_collapse && !dock->auto_expand_magic) {
3849 dock->auto_expand_magic = WMAddTimerHandler(AUTO_EXPAND_DELAY, clipAutoExpand, (void *)dock);
3853 static void clipLeave(WDock *dock)
3855 XEvent event;
3856 WObjDescriptor *desc = NULL;
3858 if (!dock || dock->type != WM_CLIP)
3859 return;
3861 if (XCheckTypedEvent(dpy, EnterNotify, &event) != False) {
3862 if (XFindContext(dpy, event.xcrossing.window, wWinContext,
3863 (XPointer *) & desc) != XCNOENT
3864 && desc && desc->parent_type == WCLASS_DOCK_ICON
3865 && ((WAppIcon *) desc->parent)->dock && ((WAppIcon *) desc->parent)->dock->type == WM_CLIP) {
3866 /* We didn't left the Clip yet */
3867 XPutBackEvent(dpy, &event);
3868 return;
3871 XPutBackEvent(dpy, &event);
3872 } else {
3873 /* We entered a withdrawn window, so we're still in Clip */
3874 return;
3877 if (dock->auto_raise_magic) {
3878 WMDeleteTimerHandler(dock->auto_raise_magic);
3879 dock->auto_raise_magic = NULL;
3881 if (dock->auto_raise_lower && !dock->auto_lower_magic) {
3882 dock->auto_lower_magic = WMAddTimerHandler(AUTO_LOWER_DELAY, clipAutoLower, (void *)dock);
3885 if (dock->auto_expand_magic) {
3886 WMDeleteTimerHandler(dock->auto_expand_magic);
3887 dock->auto_expand_magic = NULL;
3889 if (dock->auto_collapse && !dock->auto_collapse_magic) {
3890 dock->auto_collapse_magic = WMAddTimerHandler(AUTO_COLLAPSE_DELAY, clipAutoCollapse, (void *)dock);
3894 static void clipLeaveNotify(WObjDescriptor *desc, XEvent *event)
3896 WAppIcon *btn = (WAppIcon *) desc->parent;
3898 assert(event->type == LeaveNotify);
3900 if (desc->parent_type != WCLASS_DOCK_ICON)
3901 return;
3903 clipLeave(btn->dock);
3906 static void clipAutoCollapse(void *cdata)
3908 WDock *dock = (WDock *) cdata;
3910 if (dock->type != WM_CLIP)
3911 return;
3913 if (dock->auto_collapse) {
3914 dock->collapsed = 1;
3915 wDockHideIcons(dock);
3917 dock->auto_collapse_magic = NULL;
3920 static void clipAutoExpand(void *cdata)
3922 WDock *dock = (WDock *) cdata;
3924 if (dock->type != WM_CLIP)
3925 return;
3927 if (dock->auto_collapse) {
3928 dock->collapsed = 0;
3929 wDockShowIcons(dock);
3931 dock->auto_expand_magic = NULL;
3934 static void clipAutoLower(void *cdata)
3936 WDock *dock = (WDock *) cdata;
3938 if (dock->type != WM_CLIP)
3939 return;
3941 if (dock->auto_raise_lower)
3942 wDockLower(dock);
3944 dock->auto_lower_magic = NULL;
3947 static void clipAutoRaise(void *cdata)
3949 WDock *dock = (WDock *) cdata;
3951 if (dock->type != WM_CLIP)
3952 return;
3954 if (dock->auto_raise_lower)
3955 wDockRaise(dock);
3957 dock->auto_raise_magic = NULL;
3960 static Bool iconCanBeOmnipresent(WAppIcon *aicon)
3962 WScreen *scr = aicon->icon->core->screen_ptr;
3963 WDock *clip;
3964 WAppIcon *btn;
3965 int i, j;
3967 for (i = 0; i < scr->workspace_count; i++) {
3968 clip = scr->workspaces[i]->clip;
3970 if (clip == aicon->dock)
3971 continue;
3973 if (clip->icon_count + scr->global_icon_count >= clip->max_icons)
3974 return False; /* Clip is full in some workspace */
3976 for (j = 0; j < clip->max_icons; j++) {
3977 btn = clip->icon_array[j];
3978 if (btn && btn->xindex == aicon->xindex && btn->yindex == aicon->yindex)
3979 return False;
3983 return True;
3986 int wClipMakeIconOmnipresent(WAppIcon *aicon, int omnipresent)
3988 WScreen *scr = aicon->icon->core->screen_ptr;
3989 WAppIconChain *new_entry, *tmp, *tmp1;
3990 int status = WO_SUCCESS;
3992 if ((scr->dock && aicon->dock == scr->dock) || aicon == scr->clip_icon) {
3993 return WO_NOT_APPLICABLE;
3996 if (aicon->omnipresent == omnipresent)
3997 return WO_SUCCESS;
3999 if (omnipresent) {
4000 if (iconCanBeOmnipresent(aicon)) {
4001 aicon->omnipresent = 1;
4002 new_entry = wmalloc(sizeof(WAppIconChain));
4003 new_entry->aicon = aicon;
4004 new_entry->next = scr->global_icons;
4005 scr->global_icons = new_entry;
4006 scr->global_icon_count++;
4007 } else {
4008 aicon->omnipresent = 0;
4009 status = WO_FAILED;
4011 } else {
4012 aicon->omnipresent = 0;
4013 if (aicon == scr->global_icons->aicon) {
4014 tmp = scr->global_icons->next;
4015 wfree(scr->global_icons);
4016 scr->global_icons = tmp;
4017 scr->global_icon_count--;
4018 } else {
4019 tmp = scr->global_icons;
4020 while (tmp->next) {
4021 if (tmp->next->aicon == aicon) {
4022 tmp1 = tmp->next->next;
4023 wfree(tmp->next);
4024 tmp->next = tmp1;
4025 scr->global_icon_count--;
4026 break;
4028 tmp = tmp->next;
4033 wAppIconPaint(aicon);
4035 return status;