Add option to allow starting DockApps with a single click
[wmaker-crm.git] / src / dock.c
blobbd5b010ff24f5219c04bc80914c8faa0a5a2722f
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
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
21 * USA.
24 #include "wconfig.h"
26 #include <X11/Xlib.h>
27 #include <X11/Xutil.h>
28 #include <stdlib.h>
29 #include <string.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 "wsound.h"
56 #include "xinerama.h"
58 /**** Local variables ****/
59 #define CLIP_REWIND 1
60 #define CLIP_IDLE 0
61 #define CLIP_FORWARD 2
63 /**** Global variables ****/
65 /* in dockedapp.c */
66 extern void DestroyDockAppSettingsPanel();
68 extern void ShowDockAppSettingsPanel(WAppIcon * aicon);
70 extern XContext wWinContext;
72 extern Cursor wCursor[WCUR_LAST];
74 extern WPreferences wPreferences;
76 extern XContext wWinContext;
78 #define MOD_MASK wPreferences.modifier_mask
80 extern void appIconMouseDown(WObjDescriptor * desc, XEvent * event);
82 #define ICON_SIZE wPreferences.icon_size
84 /***** Local variables ****/
86 static WMPropList *dCommand = NULL;
87 static WMPropList *dPasteCommand = NULL;
88 #ifdef XDND /* XXX was OFFIX */
89 static WMPropList *dDropCommand = NULL;
90 #endif
91 static WMPropList *dAutoLaunch, *dLock;
92 static WMPropList *dName, *dForced, *dBuggyApplication, *dYes, *dNo;
93 static WMPropList *dHost, *dDock, *dClip;
94 static WMPropList *dAutoAttractIcons;
96 static WMPropList *dPosition, *dApplications, *dLowered, *dCollapsed;
98 static WMPropList *dAutoCollapse, *dAutoRaiseLower, *dOmnipresent;
100 static void dockIconPaint(WAppIcon * btn);
102 static void iconMouseDown(WObjDescriptor * desc, XEvent * event);
104 static pid_t execCommand(WAppIcon * btn, char *command, WSavedState * state);
106 static void trackDeadProcess(pid_t pid, unsigned char status, WDock * dock);
108 static int getClipButton(int px, int py);
110 static void toggleLowered(WDock * dock);
112 static void toggleCollapsed(WDock * dock);
114 static void clipIconExpose(WObjDescriptor * desc, XEvent * event);
116 static void clipLeave(WDock * dock);
118 static void handleClipChangeWorkspace(WScreen * scr, XEvent * event);
120 Bool moveIconBetweenDocks(WDock * src, WDock * dest, WAppIcon * icon, int x, int y);
122 static void clipEnterNotify(WObjDescriptor * desc, XEvent * event);
123 static void clipLeaveNotify(WObjDescriptor * desc, XEvent * event);
124 static void clipAutoCollapse(void *cdata);
125 static void clipAutoExpand(void *cdata);
126 static void launchDockedApplication(WAppIcon * btn, Bool withSelection);
128 static void clipAutoLower(void *cdata);
129 static void clipAutoRaise(void *cdata);
131 static void showClipBalloon(WDock * dock, int workspace);
133 static void make_keys()
135 if (dCommand != NULL)
136 return;
138 dCommand = WMRetainPropList(WMCreatePLString("Command"));
139 dPasteCommand = WMRetainPropList(WMCreatePLString("PasteCommand"));
140 #ifdef XDND
141 dDropCommand = WMRetainPropList(WMCreatePLString("DropCommand"));
142 #endif
143 dLock = WMRetainPropList(WMCreatePLString("Lock"));
144 dAutoLaunch = WMRetainPropList(WMCreatePLString("AutoLaunch"));
145 dName = WMRetainPropList(WMCreatePLString("Name"));
146 dForced = WMRetainPropList(WMCreatePLString("Forced"));
147 dBuggyApplication = WMRetainPropList(WMCreatePLString("BuggyApplication"));
148 dYes = WMRetainPropList(WMCreatePLString("Yes"));
149 dNo = WMRetainPropList(WMCreatePLString("No"));
150 dHost = WMRetainPropList(WMCreatePLString("Host"));
152 dPosition = WMCreatePLString("Position");
153 dApplications = WMCreatePLString("Applications");
154 dLowered = WMCreatePLString("Lowered");
155 dCollapsed = WMCreatePLString("Collapsed");
156 dAutoCollapse = WMCreatePLString("AutoCollapse");
157 dAutoRaiseLower = WMCreatePLString("AutoRaiseLower");
158 dAutoAttractIcons = WMCreatePLString("AutoAttractIcons");
160 dOmnipresent = WMCreatePLString("Omnipresent");
162 dDock = WMCreatePLString("Dock");
163 dClip = WMCreatePLString("Clip");
166 static void renameCallback(WMenu * menu, WMenuEntry * entry)
168 WDock *dock = entry->clientdata;
169 char buffer[128];
170 int wspace;
171 char *name;
173 assert(entry->clientdata != NULL);
175 wspace = dock->screen_ptr->current_workspace;
177 name = wstrdup(dock->screen_ptr->workspaces[wspace]->name);
179 snprintf(buffer, sizeof(buffer), _("Type the name for workspace %i:"), wspace + 1);
180 if (wInputDialog(dock->screen_ptr, _("Rename Workspace"), buffer, &name)) {
181 wWorkspaceRename(dock->screen_ptr, wspace, name);
183 if (name) {
184 wfree(name);
188 static void toggleLoweredCallback(WMenu * menu, WMenuEntry * entry)
190 assert(entry->clientdata != NULL);
192 toggleLowered(entry->clientdata);
194 entry->flags.indicator_on = !(((WDock *) entry->clientdata)->lowered);
196 wMenuPaint(menu);
199 static int matchWindow(void *item, void *cdata)
201 return (((WFakeGroupLeader *) item)->leader == (Window) cdata);
204 static void killCallback(WMenu * menu, WMenuEntry * entry)
206 WScreen *scr = menu->menu->screen_ptr;
207 WAppIcon *icon;
208 WFakeGroupLeader *fPtr;
209 char *buffer, *shortname, **argv;
210 char *basename(const char *shortname);
211 int argc;
213 if (!WCHECK_STATE(WSTATE_NORMAL))
214 return;
216 assert(entry->clientdata != NULL);
218 icon = (WAppIcon *) entry->clientdata;
220 icon->editing = 1;
222 WCHANGE_STATE(WSTATE_MODAL);
224 /* strip away dir names */
225 shortname = basename(icon->command);
226 /* separate out command options */
227 wtokensplit(shortname, &argv, &argc);
229 buffer = wstrconcat(argv[0],
230 _(" will be forcibly closed.\n"
231 "Any unsaved changes will be lost.\n" "Please confirm."));
233 if (icon->icon && icon->icon->owner) {
234 fPtr = icon->icon->owner->fake_group;
235 } else {
236 /* is this really necessary? can we kill a non-running dock icon? */
237 Window win = icon->main_window;
238 int index;
240 index = WMFindInArray(scr->fakeGroupLeaders, matchWindow, (void *)win);
241 if (index != WANotFound)
242 fPtr = WMGetFromArray(scr->fakeGroupLeaders, index);
243 else
244 fPtr = NULL;
247 if (wPreferences.dont_confirm_kill
248 || wMessageDialog(menu->frame->screen_ptr, _("Kill Application"),
249 buffer, _("Yes"), _("No"), NULL) == WAPRDefault) {
250 if (fPtr != NULL) {
251 WWindow *wwin, *twin;
253 wwin = scr->focused_window;
254 while (wwin) {
255 twin = wwin->prev;
256 if (wwin->fake_group == fPtr) {
257 wClientKill(wwin);
259 wwin = twin;
261 } else if (icon->icon && icon->icon->owner) {
262 wClientKill(icon->icon->owner);
266 wfree(buffer);
267 wtokenfree(argv, argc);
269 icon->editing = 0;
271 WCHANGE_STATE(WSTATE_NORMAL);
274 /* TODO: replace this function with a member of the dock struct */
275 static int numberOfSelectedIcons(WDock * dock)
277 WAppIcon *aicon;
278 int i, n;
280 n = 0;
281 for (i = 1; i < dock->max_icons; i++) {
282 aicon = dock->icon_array[i];
283 if (aicon && aicon->icon->selected) {
284 n++;
288 return n;
291 static WMArray *getSelected(WDock * dock)
293 WMArray *ret = WMCreateArray(8);
294 WAppIcon *btn;
295 int i;
297 for (i = 1; i < dock->max_icons; i++) {
298 btn = dock->icon_array[i];
299 if (btn && btn->icon->selected) {
300 WMAddToArray(ret, btn);
304 return ret;
307 static void paintClipButtons(WAppIcon * clipIcon, Bool lpushed, Bool rpushed)
309 Window win = clipIcon->icon->core->window;
310 WScreen *scr = clipIcon->icon->core->screen_ptr;
311 XPoint p[4];
312 int pt = CLIP_BUTTON_SIZE * ICON_SIZE / 64;
313 int tp = ICON_SIZE - pt;
314 int as = pt - 15; /* 15 = 5+5+5 */
315 GC gc = scr->draw_gc; /* maybe use WMColorGC() instead here? */
316 WMColor *color;
317 #ifdef GRADIENT_CLIP_ARROW
318 Bool collapsed = clipIcon->dock->collapsed;
319 #endif
321 /*if (!clipIcon->dock->collapsed)
322 color = scr->clip_title_color[CLIP_NORMAL];
323 else
324 color = scr->clip_title_color[CLIP_COLLAPSED]; */
325 color = scr->clip_title_color[CLIP_NORMAL];
327 XSetForeground(dpy, gc, WMColorPixel(color));
329 if (rpushed) {
330 p[0].x = tp + 1;
331 p[0].y = 1;
332 p[1].x = ICON_SIZE - 2;
333 p[1].y = 1;
334 p[2].x = ICON_SIZE - 2;
335 p[2].y = pt - 1;
336 } else if (lpushed) {
337 p[0].x = 1;
338 p[0].y = tp;
339 p[1].x = pt;
340 p[1].y = ICON_SIZE - 2;
341 p[2].x = 1;
342 p[2].y = ICON_SIZE - 2;
344 if (lpushed || rpushed) {
345 XSetForeground(dpy, scr->draw_gc, scr->white_pixel);
346 XFillPolygon(dpy, win, scr->draw_gc, p, 3, Convex, CoordModeOrigin);
347 XSetForeground(dpy, scr->draw_gc, scr->black_pixel);
349 #ifdef GRADIENT_CLIP_ARROW
350 if (!collapsed) {
351 XSetFillStyle(dpy, scr->copy_gc, FillTiled);
352 XSetTile(dpy, scr->copy_gc, scr->clip_arrow_gradient);
353 XSetClipMask(dpy, scr->copy_gc, None);
354 gc = scr->copy_gc;
356 #endif /* GRADIENT_CLIP_ARROW */
358 /* top right arrow */
359 p[0].x = p[3].x = ICON_SIZE - 5 - as;
360 p[0].y = p[3].y = 5;
361 p[1].x = ICON_SIZE - 6;
362 p[1].y = 5;
363 p[2].x = ICON_SIZE - 6;
364 p[2].y = 4 + as;
365 if (rpushed) {
366 XFillPolygon(dpy, win, scr->draw_gc, p, 3, Convex, CoordModeOrigin);
367 XDrawLines(dpy, win, scr->draw_gc, p, 4, CoordModeOrigin);
368 } else {
369 #ifdef GRADIENT_CLIP_ARROW
370 if (!collapsed)
371 XSetTSOrigin(dpy, gc, ICON_SIZE - 6 - as, 5);
372 #endif
373 XFillPolygon(dpy, win, gc, p, 3, Convex, CoordModeOrigin);
374 XDrawLines(dpy, win, gc, p, 4, CoordModeOrigin);
377 /* bottom left arrow */
378 p[0].x = p[3].x = 5;
379 p[0].y = p[3].y = ICON_SIZE - 5 - as;
380 p[1].x = 5;
381 p[1].y = ICON_SIZE - 6;
382 p[2].x = 4 + as;
383 p[2].y = ICON_SIZE - 6;
384 if (lpushed) {
385 XFillPolygon(dpy, win, scr->draw_gc, p, 3, Convex, CoordModeOrigin);
386 XDrawLines(dpy, win, scr->draw_gc, p, 4, CoordModeOrigin);
387 } else {
388 #ifdef GRADIENT_CLIP_ARROW
389 if (!collapsed)
390 XSetTSOrigin(dpy, gc, 5, ICON_SIZE - 6 - as);
391 #endif
392 XFillPolygon(dpy, win, gc, p, 3, Convex, CoordModeOrigin);
393 XDrawLines(dpy, win, gc, p, 4, CoordModeOrigin);
395 #ifdef GRADIENT_CLIP_ARROW
396 if (!collapsed)
397 XSetFillStyle(dpy, scr->copy_gc, FillSolid);
398 #endif
401 RImage *wClipMakeTile(WScreen * scr, RImage * normalTile)
403 RImage *tile = RCloneImage(normalTile);
404 RColor black;
405 RColor dark;
406 RColor light;
407 int pt, tp;
408 int as;
410 pt = CLIP_BUTTON_SIZE * wPreferences.icon_size / 64;
411 tp = wPreferences.icon_size - 1 - pt;
412 as = pt - 15;
414 black.alpha = 255;
415 black.red = black.green = black.blue = 0;
417 dark.alpha = 0;
418 dark.red = dark.green = dark.blue = 60;
420 light.alpha = 0;
421 light.red = light.green = light.blue = 80;
423 /* top right */
424 ROperateLine(tile, RSubtractOperation, tp, 0, wPreferences.icon_size - 2, pt - 1, &dark);
425 RDrawLine(tile, tp - 1, 0, wPreferences.icon_size - 1, pt + 1, &black);
426 ROperateLine(tile, RAddOperation, tp, 2, wPreferences.icon_size - 3, pt, &light);
428 /* arrow bevel */
429 ROperateLine(tile, RSubtractOperation, ICON_SIZE - 7 - as, 4, ICON_SIZE - 5, 4, &dark);
430 ROperateLine(tile, RSubtractOperation, ICON_SIZE - 6 - as, 5, ICON_SIZE - 5, 6 + as, &dark);
431 ROperateLine(tile, RAddOperation, ICON_SIZE - 5, 4, ICON_SIZE - 5, 6 + as, &light);
433 /* bottom left */
434 ROperateLine(tile, RAddOperation, 2, tp + 2, pt - 2, wPreferences.icon_size - 3, &dark);
435 RDrawLine(tile, 0, tp - 1, pt + 1, wPreferences.icon_size - 1, &black);
436 ROperateLine(tile, RSubtractOperation, 0, tp - 2, pt + 1, wPreferences.icon_size - 2, &light);
438 /* arrow bevel */
439 ROperateLine(tile, RSubtractOperation, 4, ICON_SIZE - 7 - as, 4, ICON_SIZE - 5, &dark);
440 ROperateLine(tile, RSubtractOperation, 5, ICON_SIZE - 6 - as, 6 + as, ICON_SIZE - 5, &dark);
441 ROperateLine(tile, RAddOperation, 4, ICON_SIZE - 5, 6 + as, ICON_SIZE - 5, &light);
443 return tile;
446 static void omnipresentCallback(WMenu * menu, WMenuEntry * entry)
448 WAppIcon *clickedIcon = entry->clientdata;
449 WAppIcon *aicon;
450 WDock *dock;
451 WMArray *selectedIcons;
452 WMArrayIterator iter;
453 int failed;
455 assert(entry->clientdata != NULL);
457 dock = clickedIcon->dock;
459 selectedIcons = getSelected(dock);
461 if (!WMGetArrayItemCount(selectedIcons))
462 WMAddToArray(selectedIcons, clickedIcon);
464 failed = 0;
465 WM_ITERATE_ARRAY(selectedIcons, aicon, iter) {
466 if (wClipMakeIconOmnipresent(aicon, !aicon->omnipresent) == WO_FAILED)
467 failed++;
468 else if (aicon->icon->selected)
469 wIconSelect(aicon->icon);
471 WMFreeArray(selectedIcons);
473 if (failed > 1) {
474 wMessageDialog(dock->screen_ptr, _("Warning"),
475 _("Some icons cannot be made omnipresent. "
476 "Please make sure that no other icon is "
477 "docked in the same positions on the other "
478 "workspaces and the Clip is not full in "
479 "some workspace."), _("OK"), NULL, NULL);
480 } else if (failed == 1) {
481 wMessageDialog(dock->screen_ptr, _("Warning"),
482 _("Icon cannot be made omnipresent. "
483 "Please make sure that no other icon is "
484 "docked in the same position on the other "
485 "workspaces and the Clip is not full in "
486 "some workspace."), _("OK"), NULL, NULL);
490 static void removeIconsCallback(WMenu * menu, WMenuEntry * entry)
492 WAppIcon *clickedIcon = (WAppIcon *) entry->clientdata;
493 WDock *dock;
494 WAppIcon *aicon;
495 WMArray *selectedIcons;
496 int keepit;
497 WMArrayIterator it;
499 assert(clickedIcon != NULL);
501 dock = clickedIcon->dock;
503 selectedIcons = getSelected(dock);
505 if (WMGetArrayItemCount(selectedIcons)) {
506 if (wMessageDialog(dock->screen_ptr, _("Workspace Clip"),
507 _("All selected icons will be removed!"),
508 _("OK"), _("Cancel"), NULL) != WAPRDefault) {
509 WMFreeArray(selectedIcons);
510 return;
512 } else {
513 if (clickedIcon->xindex == 0 && clickedIcon->yindex == 0) {
514 WMFreeArray(selectedIcons);
515 return;
517 WMAddToArray(selectedIcons, clickedIcon);
520 WM_ITERATE_ARRAY(selectedIcons, aicon, it) {
521 keepit = aicon->running && wApplicationOf(aicon->main_window);
522 wDockDetach(dock, aicon);
523 if (keepit) {
524 /* XXX: can: aicon->icon == NULL ? */
525 PlaceIcon(dock->screen_ptr, &aicon->x_pos, &aicon->y_pos,
526 wGetHeadForWindow(aicon->icon->owner));
527 XMoveWindow(dpy, aicon->icon->core->window, aicon->x_pos, aicon->y_pos);
528 if (!dock->mapped || dock->collapsed)
529 XMapWindow(dpy, aicon->icon->core->window);
532 WMFreeArray(selectedIcons);
534 if (wPreferences.auto_arrange_icons)
535 wArrangeIcons(dock->screen_ptr, True);
538 static void keepIconsCallback(WMenu * menu, WMenuEntry * entry)
540 WAppIcon *clickedIcon = (WAppIcon *) entry->clientdata;
541 WDock *dock;
542 WAppIcon *aicon;
543 WMArray *selectedIcons;
544 WMArrayIterator it;
546 assert(clickedIcon != NULL);
547 dock = clickedIcon->dock;
549 selectedIcons = getSelected(dock);
551 if (!WMGetArrayItemCount(selectedIcons)
552 && clickedIcon != dock->screen_ptr->clip_icon) {
553 char *command = NULL;
555 if (!clickedIcon->command && !clickedIcon->editing) {
556 clickedIcon->editing = 1;
557 if (wInputDialog(dock->screen_ptr, _("Keep Icon"),
558 _("Type the command used to launch the application"), &command)) {
559 if (command && (command[0] == 0 || (command[0] == '-' && command[1] == 0))) {
560 wfree(command);
561 command = NULL;
563 clickedIcon->command = command;
564 clickedIcon->editing = 0;
565 } else {
566 clickedIcon->editing = 0;
567 if (command)
568 wfree(command);
569 WMFreeArray(selectedIcons);
570 return;
574 WMAddToArray(selectedIcons, clickedIcon);
577 WM_ITERATE_ARRAY(selectedIcons, aicon, it) {
578 if (aicon->icon->selected)
579 wIconSelect(aicon->icon);
580 if (aicon && aicon->attracted && aicon->command) {
581 aicon->attracted = 0;
582 if (aicon->icon->shadowed) {
583 aicon->icon->shadowed = 0;
584 aicon->icon->force_paint = 1;
585 wAppIconPaint(aicon);
589 WMFreeArray(selectedIcons);
592 static void toggleAutoAttractCallback(WMenu * menu, WMenuEntry * entry)
594 WDock *dock = (WDock *) entry->clientdata;
596 assert(entry->clientdata != NULL);
598 dock->attract_icons = !dock->attract_icons;
599 /*if (!dock->attract_icons)
600 dock->keep_attracted = 0; */
602 entry->flags.indicator_on = dock->attract_icons;
604 wMenuPaint(menu);
607 static void selectCallback(WMenu * menu, WMenuEntry * entry)
609 WAppIcon *icon = (WAppIcon *) entry->clientdata;
611 assert(icon != NULL);
613 wIconSelect(icon->icon);
615 wMenuPaint(menu);
618 static void colectIconsCallback(WMenu * menu, WMenuEntry * entry)
620 WAppIcon *clickedIcon = (WAppIcon *) entry->clientdata;
621 WDock *clip;
622 WAppIcon *aicon;
623 int x, y, x_pos, y_pos;
625 assert(entry->clientdata != NULL);
626 clip = clickedIcon->dock;
628 aicon = clip->screen_ptr->app_icon_list;
630 while (aicon) {
631 if (!aicon->docked && wDockFindFreeSlot(clip, &x, &y)) {
632 x_pos = clip->x_pos + x * ICON_SIZE;
633 y_pos = clip->y_pos + y * ICON_SIZE;
634 if (aicon->x_pos != x_pos || aicon->y_pos != y_pos) {
635 #ifdef ANIMATIONS
636 if (wPreferences.no_animations) {
637 XMoveWindow(dpy, aicon->icon->core->window, x_pos, y_pos);
638 } else {
639 SlideWindow(aicon->icon->core->window,
640 aicon->x_pos, aicon->y_pos, x_pos, y_pos);
642 #else
643 XMoveWindow(dpy, aicon->icon->core->window, x_pos, y_pos);
644 #endif /* ANIMATIONS */
646 aicon->attracted = 1;
647 if (!aicon->icon->shadowed) {
648 aicon->icon->shadowed = 1;
649 aicon->icon->force_paint = 1;
650 /* We don't do an wAppIconPaint() here because it's in
651 * wDockAttachIcon(). -Dan
654 wDockAttachIcon(clip, aicon, x, y);
655 if (clip->collapsed || !clip->mapped)
656 XUnmapWindow(dpy, aicon->icon->core->window);
658 aicon = aicon->next;
662 static void selectIconsCallback(WMenu * menu, WMenuEntry * entry)
664 WAppIcon *clickedIcon = (WAppIcon *) entry->clientdata;
665 WDock *dock;
666 WMArray *selectedIcons;
667 WMArrayIterator iter;
668 WAppIcon *btn;
669 int i;
671 assert(clickedIcon != NULL);
672 dock = clickedIcon->dock;
674 selectedIcons = getSelected(dock);
676 if (!WMGetArrayItemCount(selectedIcons)) {
677 for (i = 1; i < dock->max_icons; i++) {
678 btn = dock->icon_array[i];
679 if (btn && !btn->icon->selected) {
680 wIconSelect(btn->icon);
683 } else {
684 WM_ITERATE_ARRAY(selectedIcons, btn, iter) {
685 wIconSelect(btn->icon);
688 WMFreeArray(selectedIcons);
690 wMenuPaint(menu);
693 static void toggleCollapsedCallback(WMenu * menu, WMenuEntry * entry)
695 assert(entry->clientdata != NULL);
697 toggleCollapsed(entry->clientdata);
699 entry->flags.indicator_on = ((WDock *) entry->clientdata)->collapsed;
701 wMenuPaint(menu);
704 static void toggleAutoCollapseCallback(WMenu * menu, WMenuEntry * entry)
706 WDock *dock;
707 assert(entry->clientdata != NULL);
709 dock = (WDock *) entry->clientdata;
711 dock->auto_collapse = !dock->auto_collapse;
713 entry->flags.indicator_on = ((WDock *) entry->clientdata)->auto_collapse;
715 wMenuPaint(menu);
718 static void toggleAutoRaiseLowerCallback(WMenu * menu, WMenuEntry * entry)
720 WDock *dock;
721 assert(entry->clientdata != NULL);
723 dock = (WDock *) entry->clientdata;
725 dock->auto_raise_lower = !dock->auto_raise_lower;
727 entry->flags.indicator_on = ((WDock *) entry->clientdata)->auto_raise_lower;
729 wMenuPaint(menu);
732 static void launchCallback(WMenu * menu, WMenuEntry * entry)
734 WAppIcon *btn = (WAppIcon *) entry->clientdata;
736 launchDockedApplication(btn, False);
739 static void settingsCallback(WMenu * menu, WMenuEntry * entry)
741 WAppIcon *btn = (WAppIcon *) entry->clientdata;
743 if (btn->editing)
744 return;
745 ShowDockAppSettingsPanel(btn);
748 static void hideCallback(WMenu * menu, WMenuEntry * entry)
750 WApplication *wapp;
751 WAppIcon *btn = (WAppIcon *) entry->clientdata;
753 wapp = wApplicationOf(btn->icon->owner->main_window);
755 if (wapp->flags.hidden) {
756 wWorkspaceChange(btn->icon->core->screen_ptr, wapp->last_workspace);
757 wUnhideApplication(wapp, False, False);
758 } else {
759 wHideApplication(wapp);
763 static void unhideHereCallback(WMenu * menu, WMenuEntry * entry)
765 WApplication *wapp;
766 WAppIcon *btn = (WAppIcon *) entry->clientdata;
768 wapp = wApplicationOf(btn->icon->owner->main_window);
770 wUnhideApplication(wapp, False, True);
773 WAppIcon *mainIconCreate(WScreen * scr, int type)
775 WAppIcon *btn;
776 int x_pos;
778 if (type == WM_CLIP) {
779 if (scr->clip_icon)
780 return scr->clip_icon;
781 btn = wAppIconCreateForDock(scr, NULL, "Logo", "WMClip", TILE_CLIP);
782 btn->icon->core->descriptor.handle_expose = clipIconExpose;
783 btn->icon->core->descriptor.handle_enternotify = clipEnterNotify;
784 btn->icon->core->descriptor.handle_leavenotify = clipLeaveNotify;
785 /*x_pos = scr->scr_width - ICON_SIZE*2 - DOCK_EXTRA_SPACE; */
786 x_pos = 0;
787 } else {
788 btn = wAppIconCreateForDock(scr, NULL, "Logo", "WMDock", TILE_NORMAL);
789 x_pos = scr->scr_width - ICON_SIZE - DOCK_EXTRA_SPACE;
792 btn->xindex = 0;
793 btn->yindex = 0;
795 btn->icon->core->descriptor.handle_mousedown = iconMouseDown;
796 btn->icon->core->descriptor.parent_type = WCLASS_DOCK_ICON;
797 btn->icon->core->descriptor.parent = btn;
798 /*ChangeStackingLevel(btn->icon->core, WMDockLevel); */
799 XMapWindow(dpy, btn->icon->core->window);
800 btn->x_pos = x_pos;
801 btn->y_pos = 0;
802 btn->docked = 1;
803 if (type == WM_CLIP)
804 scr->clip_icon = btn;
806 return btn;
809 static void switchWSCommand(WMenu * menu, WMenuEntry * entry)
811 WAppIcon *btn, *icon = (WAppIcon *) entry->clientdata;
812 WScreen *scr = icon->icon->core->screen_ptr;
813 WDock *src, *dest;
814 WMArray *selectedIcons;
815 int x, y;
817 if (entry->order == scr->current_workspace)
818 return;
819 src = icon->dock;
820 dest = scr->workspaces[entry->order]->clip;
822 selectedIcons = getSelected(src);
824 if (WMGetArrayItemCount(selectedIcons)) {
825 WMArrayIterator iter;
827 WM_ITERATE_ARRAY(selectedIcons, btn, iter) {
828 if (wDockFindFreeSlot(dest, &x, &y)) {
829 moveIconBetweenDocks(src, dest, btn, x, y);
830 XUnmapWindow(dpy, btn->icon->core->window);
833 } else if (icon != scr->clip_icon) {
834 if (wDockFindFreeSlot(dest, &x, &y)) {
835 moveIconBetweenDocks(src, dest, icon, x, y);
836 XUnmapWindow(dpy, icon->icon->core->window);
839 WMFreeArray(selectedIcons);
842 static void launchDockedApplication(WAppIcon * btn, Bool withSelection)
844 WScreen *scr = btn->icon->core->screen_ptr;
846 if (!btn->launching &&
847 ((!withSelection && btn->command != NULL) || (withSelection && btn->paste_command != NULL))) {
848 if (!btn->forced_dock) {
849 btn->relaunching = btn->running;
850 btn->running = 1;
852 if (btn->wm_instance || btn->wm_class) {
853 WWindowAttributes attr;
854 memset(&attr, 0, sizeof(WWindowAttributes));
855 wDefaultFillAttributes(scr, btn->wm_instance, btn->wm_class, &attr, NULL, True);
857 if (!attr.no_appicon && !btn->buggy_app)
858 btn->launching = 1;
859 else
860 btn->running = 0;
862 btn->drop_launch = 0;
863 btn->paste_launch = withSelection;
864 scr->last_dock = btn->dock;
865 btn->pid = execCommand(btn, (withSelection ? btn->paste_command : btn->command), NULL);
866 if (btn->pid > 0) {
867 if (btn->buggy_app) {
868 /* give feedback that the app was launched */
869 btn->launching = 1;
870 dockIconPaint(btn);
871 btn->launching = 0;
872 WMAddTimerHandler(200, (WMCallback *) dockIconPaint, btn);
873 } else {
874 dockIconPaint(btn);
876 } else {
877 wwarning(_("could not launch application %s\n"), btn->command);
878 btn->launching = 0;
879 if (!btn->relaunching) {
880 btn->running = 0;
886 static void updateWorkspaceMenu(WMenu * menu, WAppIcon * icon)
888 WScreen *scr = menu->frame->screen_ptr;
889 char title[MAX_WORKSPACENAME_WIDTH + 1];
890 int i;
892 if (!menu || !icon)
893 return;
895 for (i = 0; i < scr->workspace_count; i++) {
896 if (i < menu->entry_no) {
897 if (strcmp(menu->entries[i]->text, scr->workspaces[i]->name) != 0) {
898 wfree(menu->entries[i]->text);
899 strcpy(title, scr->workspaces[i]->name);
900 menu->entries[i]->text = wstrdup(title);
901 menu->flags.realized = 0;
903 menu->entries[i]->clientdata = (void *)icon;
904 } else {
905 strcpy(title, scr->workspaces[i]->name);
907 wMenuAddCallback(menu, title, switchWSCommand, (void *)icon);
909 menu->flags.realized = 0;
911 if (i == scr->current_workspace) {
912 wMenuSetEnabled(menu, i, False);
913 } else {
914 wMenuSetEnabled(menu, i, True);
918 if (!menu->flags.realized)
919 wMenuRealize(menu);
922 static WMenu *makeWorkspaceMenu(WScreen * scr)
924 WMenu *menu;
926 menu = wMenuCreate(scr, NULL, False);
927 if (!menu)
928 wwarning(_("could not create workspace submenu for Clip menu"));
930 wMenuAddCallback(menu, "", switchWSCommand, (void *)scr->clip_icon);
932 menu->flags.realized = 0;
933 wMenuRealize(menu);
935 return menu;
938 static void updateClipOptionsMenu(WMenu * menu, WDock * dock)
940 WMenuEntry *entry;
941 int index = 0;
943 if (!menu || !dock)
944 return;
946 /* keep on top */
947 entry = menu->entries[index];
948 entry->flags.indicator_on = !dock->lowered;
949 entry->clientdata = dock;
951 /* collapsed */
952 entry = menu->entries[++index];
953 entry->flags.indicator_on = dock->collapsed;
954 entry->clientdata = dock;
956 /* auto-collapse */
957 entry = menu->entries[++index];
958 entry->flags.indicator_on = dock->auto_collapse;
959 entry->clientdata = dock;
961 /* auto-raise/lower */
962 entry = menu->entries[++index];
963 entry->flags.indicator_on = dock->auto_raise_lower;
964 entry->clientdata = dock;
965 wMenuSetEnabled(menu, index, dock->lowered);
967 /* attract icons */
968 entry = menu->entries[++index];
969 entry->flags.indicator_on = dock->attract_icons;
970 entry->clientdata = dock;
972 menu->flags.realized = 0;
973 wMenuRealize(menu);
976 static WMenu *makeClipOptionsMenu(WScreen * scr)
978 WMenu *menu;
979 WMenuEntry *entry;
981 menu = wMenuCreate(scr, NULL, False);
982 if (!menu) {
983 wwarning(_("could not create options submenu for Clip menu"));
984 return NULL;
987 entry = wMenuAddCallback(menu, _("Keep on Top"), toggleLoweredCallback, NULL);
988 entry->flags.indicator = 1;
989 entry->flags.indicator_on = 1;
990 entry->flags.indicator_type = MI_CHECK;
992 entry = wMenuAddCallback(menu, _("Collapsed"), toggleCollapsedCallback, NULL);
993 entry->flags.indicator = 1;
994 entry->flags.indicator_on = 1;
995 entry->flags.indicator_type = MI_CHECK;
997 entry = wMenuAddCallback(menu, _("Autocollapse"), toggleAutoCollapseCallback, NULL);
998 entry->flags.indicator = 1;
999 entry->flags.indicator_on = 1;
1000 entry->flags.indicator_type = MI_CHECK;
1002 entry = wMenuAddCallback(menu, _("Autoraise"), toggleAutoRaiseLowerCallback, NULL);
1003 entry->flags.indicator = 1;
1004 entry->flags.indicator_on = 1;
1005 entry->flags.indicator_type = MI_CHECK;
1007 entry = wMenuAddCallback(menu, _("Autoattract Icons"), toggleAutoAttractCallback, NULL);
1008 entry->flags.indicator = 1;
1009 entry->flags.indicator_on = 1;
1010 entry->flags.indicator_type = MI_CHECK;
1012 menu->flags.realized = 0;
1013 wMenuRealize(menu);
1015 return menu;
1018 static WMenu *dockMenuCreate(WScreen * scr, int type)
1020 WMenu *menu;
1021 WMenuEntry *entry;
1023 if (type == WM_CLIP && scr->clip_menu)
1024 return scr->clip_menu;
1026 menu = wMenuCreate(scr, NULL, False);
1027 if (type != WM_CLIP) {
1028 entry = wMenuAddCallback(menu, _("Keep on Top"), toggleLoweredCallback, NULL);
1029 entry->flags.indicator = 1;
1030 entry->flags.indicator_on = 1;
1031 entry->flags.indicator_type = MI_CHECK;
1032 } else {
1033 entry = wMenuAddCallback(menu, _("Clip Options"), NULL, NULL);
1034 scr->clip_options = makeClipOptionsMenu(scr);
1035 if (scr->clip_options)
1036 wMenuEntrySetCascade(menu, entry, scr->clip_options);
1038 entry = wMenuAddCallback(menu, _("Rename Workspace"), renameCallback, NULL);
1039 wfree(entry->text);
1040 entry->text = _("Rename Workspace");
1042 entry = wMenuAddCallback(menu, _("Selected"), selectCallback, NULL);
1043 entry->flags.indicator = 1;
1044 entry->flags.indicator_on = 1;
1045 entry->flags.indicator_type = MI_CHECK;
1047 entry = wMenuAddCallback(menu, _("Select All Icons"), selectIconsCallback, NULL);
1048 wfree(entry->text);
1049 entry->text = _("Select All Icons");
1051 entry = wMenuAddCallback(menu, _("Keep Icon"), keepIconsCallback, NULL);
1052 wfree(entry->text);
1053 entry->text = _("Keep Icon");
1055 entry = wMenuAddCallback(menu, _("Move Icon To"), NULL, NULL);
1056 wfree(entry->text);
1057 entry->text = _("Move Icon To");
1058 scr->clip_submenu = makeWorkspaceMenu(scr);
1059 if (scr->clip_submenu)
1060 wMenuEntrySetCascade(menu, entry, scr->clip_submenu);
1062 entry = wMenuAddCallback(menu, _("Remove Icon"), removeIconsCallback, NULL);
1063 wfree(entry->text);
1064 entry->text = _("Remove Icon");
1066 wMenuAddCallback(menu, _("Attract Icons"), colectIconsCallback, NULL);
1069 wMenuAddCallback(menu, _("Launch"), launchCallback, NULL);
1071 wMenuAddCallback(menu, _("Unhide Here"), unhideHereCallback, NULL);
1073 entry = wMenuAddCallback(menu, _("Hide"), hideCallback, NULL);
1074 wfree(entry->text);
1075 entry->text = _("Hide");
1077 wMenuAddCallback(menu, _("Settings..."), settingsCallback, NULL);
1079 wMenuAddCallback(menu, _("Kill"), killCallback, NULL);
1081 if (type == WM_CLIP)
1082 scr->clip_menu = menu;
1084 return menu;
1087 WDock *wDockCreate(WScreen * scr, int type)
1089 WDock *dock;
1090 WAppIcon *btn;
1091 int icon_count;
1093 make_keys();
1095 dock = wmalloc(sizeof(WDock));
1096 memset(dock, 0, sizeof(WDock));
1098 if (type == WM_CLIP)
1099 icon_count = CLIP_MAX_ICONS;
1100 else
1101 icon_count = scr->scr_height / wPreferences.icon_size;
1103 dock->icon_array = wmalloc(sizeof(WAppIcon *) * icon_count);
1104 memset(dock->icon_array, 0, sizeof(WAppIcon *) * icon_count);
1106 dock->max_icons = icon_count;
1108 btn = mainIconCreate(scr, type);
1110 btn->dock = dock;
1112 dock->x_pos = btn->x_pos;
1113 dock->y_pos = btn->y_pos;
1114 dock->screen_ptr = scr;
1115 dock->type = type;
1116 dock->icon_count = 1;
1117 dock->on_right_side = 1;
1118 dock->collapsed = 0;
1119 dock->auto_collapse = 0;
1120 dock->auto_collapse_magic = NULL;
1121 dock->auto_raise_lower = 0;
1122 dock->auto_lower_magic = NULL;
1123 dock->auto_raise_magic = NULL;
1124 dock->attract_icons = 0;
1125 dock->lowered = 1;
1126 dock->icon_array[0] = btn;
1127 wRaiseFrame(btn->icon->core);
1128 XMoveWindow(dpy, btn->icon->core->window, btn->x_pos, btn->y_pos);
1130 /* create dock menu */
1131 dock->menu = dockMenuCreate(scr, type);
1133 return dock;
1136 void wDockDestroy(WDock * dock)
1138 int i;
1139 WAppIcon *aicon;
1141 for (i = (dock->type == WM_CLIP) ? 1 : 0; i < dock->max_icons; i++) {
1142 aicon = dock->icon_array[i];
1143 if (aicon) {
1144 int keepit = aicon->running && wApplicationOf(aicon->main_window);
1145 wDockDetach(dock, aicon);
1146 if (keepit) {
1147 /* XXX: can: aicon->icon == NULL ? */
1148 PlaceIcon(dock->screen_ptr, &aicon->x_pos, &aicon->y_pos,
1149 wGetHeadForWindow(aicon->icon->owner));
1150 XMoveWindow(dpy, aicon->icon->core->window, aicon->x_pos, aicon->y_pos);
1151 if (!dock->mapped || dock->collapsed)
1152 XMapWindow(dpy, aicon->icon->core->window);
1156 if (wPreferences.auto_arrange_icons)
1157 wArrangeIcons(dock->screen_ptr, True);
1158 wfree(dock->icon_array);
1159 if (dock->menu && dock->type != WM_CLIP)
1160 wMenuDestroy(dock->menu, True);
1161 if (dock->screen_ptr->last_dock == dock)
1162 dock->screen_ptr->last_dock = NULL;
1163 wfree(dock);
1166 void wClipIconPaint(WAppIcon * aicon)
1168 WScreen *scr = aicon->icon->core->screen_ptr;
1169 WWorkspace *workspace = scr->workspaces[scr->current_workspace];
1170 WMColor *color;
1171 Window win = aicon->icon->core->window;
1172 int length, nlength;
1173 char *ws_name, ws_number[10];
1174 int ty, tx;
1176 wIconPaint(aicon->icon);
1178 length = strlen(workspace->name);
1179 ws_name = wmalloc(length + 1);
1180 snprintf(ws_name, length + 1, "%s", workspace->name);
1181 snprintf(ws_number, sizeof(ws_number), "%i", scr->current_workspace + 1);
1182 nlength = strlen(ws_number);
1184 if (!workspace->clip->collapsed)
1185 color = scr->clip_title_color[CLIP_NORMAL];
1186 else
1187 color = scr->clip_title_color[CLIP_COLLAPSED];
1189 ty = ICON_SIZE - WMFontHeight(scr->clip_title_font) - 3;
1191 tx = CLIP_BUTTON_SIZE * ICON_SIZE / 64;
1193 WMDrawString(scr->wmscreen, win, color, scr->clip_title_font, tx, ty, ws_name, length);
1194 /*WMDrawString(scr->wmscreen, win, color, scr->clip_title_font, 4,
1195 2, ws_name, length); */
1197 tx = (ICON_SIZE / 2 - WMWidthOfString(scr->clip_title_font, ws_number, nlength)) / 2;
1199 WMDrawString(scr->wmscreen, win, color, scr->clip_title_font, tx, 2, ws_number, nlength);
1201 wfree(ws_name);
1203 if (aicon->launching) {
1204 XFillRectangle(dpy, aicon->icon->core->window, scr->stipple_gc,
1205 0, 0, wPreferences.icon_size, wPreferences.icon_size);
1207 paintClipButtons(aicon, aicon->dock->lclip_button_pushed, aicon->dock->rclip_button_pushed);
1210 static void clipIconExpose(WObjDescriptor * desc, XEvent * event)
1212 wClipIconPaint(desc->parent);
1215 static void dockIconPaint(WAppIcon * btn)
1217 if (btn == btn->icon->core->screen_ptr->clip_icon)
1218 wClipIconPaint(btn);
1219 else
1220 wAppIconPaint(btn);
1223 static WMPropList *make_icon_state(WAppIcon * btn)
1225 WMPropList *node = NULL;
1226 WMPropList *command, *autolaunch, *lock, *name, *forced, *host;
1227 WMPropList *position, *buggy, *omnipresent;
1228 char *tmp;
1229 char buffer[64];
1231 if (btn) {
1232 if (!btn->command)
1233 command = WMCreatePLString("-");
1234 else
1235 command = WMCreatePLString(btn->command);
1237 autolaunch = btn->auto_launch ? dYes : dNo;
1239 lock = btn->lock ? dYes : dNo;
1241 tmp = EscapeWM_CLASS(btn->wm_instance, btn->wm_class);
1243 name = WMCreatePLString(tmp);
1245 wfree(tmp);
1247 forced = btn->forced_dock ? dYes : dNo;
1249 buggy = btn->buggy_app ? dYes : dNo;
1251 if (btn == btn->icon->core->screen_ptr->clip_icon)
1252 snprintf(buffer, sizeof(buffer), "%i,%i", btn->x_pos, btn->y_pos);
1253 else
1254 snprintf(buffer, sizeof(buffer), "%hi,%hi", btn->xindex, btn->yindex);
1255 position = WMCreatePLString(buffer);
1257 node = WMCreatePLDictionary(dCommand, command,
1258 dName, name,
1259 dAutoLaunch, autolaunch,
1260 dLock, lock,
1261 dForced, forced, dBuggyApplication, buggy, dPosition, position, NULL);
1262 WMReleasePropList(command);
1263 WMReleasePropList(name);
1264 WMReleasePropList(position);
1266 omnipresent = btn->omnipresent ? dYes : dNo;
1267 if (btn->dock != btn->icon->core->screen_ptr->dock && (btn->xindex != 0 || btn->yindex != 0))
1268 WMPutInPLDictionary(node, dOmnipresent, omnipresent);
1270 #ifdef XDND /* was OFFIX */
1271 if (btn->dnd_command) {
1272 command = WMCreatePLString(btn->dnd_command);
1273 WMPutInPLDictionary(node, dDropCommand, command);
1274 WMReleasePropList(command);
1276 #endif /* XDND */
1278 if (btn->paste_command) {
1279 command = WMCreatePLString(btn->paste_command);
1280 WMPutInPLDictionary(node, dPasteCommand, command);
1281 WMReleasePropList(command);
1284 if (btn->client_machine && btn->remote_start) {
1285 host = WMCreatePLString(btn->client_machine);
1286 WMPutInPLDictionary(node, dHost, host);
1287 WMReleasePropList(host);
1291 return node;
1294 static WMPropList *dockSaveState(WDock * dock)
1296 int i;
1297 WMPropList *icon_info;
1298 WMPropList *list = NULL, *dock_state = NULL;
1299 WMPropList *value, *key;
1300 char buffer[256];
1302 list = WMCreatePLArray(NULL);
1304 for (i = (dock->type == WM_DOCK ? 0 : 1); i < dock->max_icons; i++) {
1305 WAppIcon *btn = dock->icon_array[i];
1307 if (!btn || btn->attracted)
1308 continue;
1310 if ((icon_info = make_icon_state(dock->icon_array[i]))) {
1311 WMAddToPLArray(list, icon_info);
1312 WMReleasePropList(icon_info);
1316 dock_state = WMCreatePLDictionary(dApplications, list, NULL);
1318 if (dock->type == WM_DOCK) {
1319 snprintf(buffer, sizeof(buffer), "Applications%i", dock->screen_ptr->scr_height);
1320 key = WMCreatePLString(buffer);
1321 WMPutInPLDictionary(dock_state, key, list);
1322 WMReleasePropList(key);
1324 snprintf(buffer, sizeof(buffer), "%i,%i", (dock->on_right_side ? -ICON_SIZE : 0), dock->y_pos);
1325 value = WMCreatePLString(buffer);
1326 WMPutInPLDictionary(dock_state, dPosition, value);
1327 WMReleasePropList(value);
1329 WMReleasePropList(list);
1331 value = (dock->lowered ? dYes : dNo);
1332 WMPutInPLDictionary(dock_state, dLowered, value);
1334 if (dock->type == WM_CLIP) {
1335 value = (dock->collapsed ? dYes : dNo);
1336 WMPutInPLDictionary(dock_state, dCollapsed, value);
1338 value = (dock->auto_collapse ? dYes : dNo);
1339 WMPutInPLDictionary(dock_state, dAutoCollapse, value);
1341 value = (dock->auto_raise_lower ? dYes : dNo);
1342 WMPutInPLDictionary(dock_state, dAutoRaiseLower, value);
1344 value = (dock->attract_icons ? dYes : dNo);
1345 WMPutInPLDictionary(dock_state, dAutoAttractIcons, value);
1348 return dock_state;
1351 void wDockSaveState(WScreen * scr, WMPropList * old_state)
1353 WMPropList *dock_state;
1354 WMPropList *keys;
1356 dock_state = dockSaveState(scr->dock);
1359 * Copy saved states of docks with different sizes.
1361 if (old_state) {
1362 int i;
1363 WMPropList *tmp;
1365 keys = WMGetPLDictionaryKeys(old_state);
1366 for (i = 0; i < WMGetPropListItemCount(keys); i++) {
1367 tmp = WMGetFromPLArray(keys, i);
1369 if (strncasecmp(WMGetFromPLString(tmp), "applications", 12) == 0
1370 && !WMGetFromPLDictionary(dock_state, tmp)) {
1372 WMPutInPLDictionary(dock_state, tmp, WMGetFromPLDictionary(old_state, tmp));
1375 WMReleasePropList(keys);
1378 WMPutInPLDictionary(scr->session_state, dDock, dock_state);
1380 WMReleasePropList(dock_state);
1383 void wClipSaveState(WScreen * scr)
1385 WMPropList *clip_state;
1387 clip_state = make_icon_state(scr->clip_icon);
1389 WMPutInPLDictionary(scr->session_state, dClip, clip_state);
1391 WMReleasePropList(clip_state);
1394 WMPropList *wClipSaveWorkspaceState(WScreen * scr, int workspace)
1396 return dockSaveState(scr->workspaces[workspace]->clip);
1399 static Bool getBooleanDockValue(WMPropList * value, WMPropList * key)
1401 if (value) {
1402 if (WMIsPLString(value)) {
1403 if (strcasecmp(WMGetFromPLString(value), "YES") == 0)
1404 return True;
1405 } else {
1406 wwarning(_("bad value in docked icon state info %s"), WMGetFromPLString(key));
1409 return False;
1412 static WAppIcon *restore_icon_state(WScreen * scr, WMPropList * info, int type, int index)
1414 WAppIcon *aicon;
1415 WMPropList *cmd, *value;
1417 cmd = WMGetFromPLDictionary(info, dCommand);
1418 if (!cmd || !WMIsPLString(cmd)) {
1419 return NULL;
1422 /* parse window name */
1423 value = WMGetFromPLDictionary(info, dName);
1424 if (!value)
1425 return NULL;
1428 char *wclass, *winstance;
1429 char *command;
1431 ParseWindowName(value, &winstance, &wclass, "dock");
1433 if (!winstance && !wclass) {
1434 return NULL;
1437 /* get commands */
1439 if (cmd)
1440 command = wstrdup(WMGetFromPLString(cmd));
1441 else
1442 command = NULL;
1444 if (!command || strcmp(command, "-") == 0) {
1445 if (command)
1446 wfree(command);
1447 if (wclass)
1448 wfree(wclass);
1449 if (winstance)
1450 wfree(winstance);
1452 return NULL;
1455 aicon = wAppIconCreateForDock(scr, command, winstance, wclass, TILE_NORMAL);
1456 if (wclass)
1457 wfree(wclass);
1458 if (winstance)
1459 wfree(winstance);
1460 if (command)
1461 wfree(command);
1464 aicon->icon->core->descriptor.handle_mousedown = iconMouseDown;
1465 if (type == WM_CLIP) {
1466 aicon->icon->core->descriptor.handle_enternotify = clipEnterNotify;
1467 aicon->icon->core->descriptor.handle_leavenotify = clipLeaveNotify;
1469 aicon->icon->core->descriptor.parent_type = WCLASS_DOCK_ICON;
1470 aicon->icon->core->descriptor.parent = aicon;
1472 #ifdef XDND /* was OFFIX */
1473 cmd = WMGetFromPLDictionary(info, dDropCommand);
1474 if (cmd)
1475 aicon->dnd_command = wstrdup(WMGetFromPLString(cmd));
1476 #endif
1478 cmd = WMGetFromPLDictionary(info, dPasteCommand);
1479 if (cmd)
1480 aicon->paste_command = wstrdup(WMGetFromPLString(cmd));
1482 /* check auto launch */
1483 value = WMGetFromPLDictionary(info, dAutoLaunch);
1485 aicon->auto_launch = getBooleanDockValue(value, dAutoLaunch);
1487 /* check lock */
1488 value = WMGetFromPLDictionary(info, dLock);
1490 aicon->lock = getBooleanDockValue(value, dLock);
1492 /* check if it wasn't normally docked */
1493 value = WMGetFromPLDictionary(info, dForced);
1495 aicon->forced_dock = getBooleanDockValue(value, dForced);
1497 /* check if we can rely on the stuff in the app */
1498 value = WMGetFromPLDictionary(info, dBuggyApplication);
1500 aicon->buggy_app = getBooleanDockValue(value, dBuggyApplication);
1502 /* get position in the dock */
1503 value = WMGetFromPLDictionary(info, dPosition);
1504 if (value && WMIsPLString(value)) {
1505 if (sscanf(WMGetFromPLString(value), "%hi,%hi", &aicon->xindex, &aicon->yindex) != 2)
1506 wwarning(_("bad value in docked icon state info %s"), WMGetFromPLString(dPosition));
1508 /* check position sanity */
1509 /* incomplete section! */
1510 if (type == WM_DOCK) {
1511 aicon->xindex = 0;
1512 if (aicon->yindex < 0)
1513 wwarning(_("bad value in docked icon position %i,%i"),
1514 aicon->xindex, aicon->yindex);
1516 } else {
1517 aicon->yindex = index;
1518 aicon->xindex = 0;
1521 /* check if icon is omnipresent */
1522 value = WMGetFromPLDictionary(info, dOmnipresent);
1524 aicon->omnipresent = getBooleanDockValue(value, dOmnipresent);
1526 aicon->running = 0;
1527 aicon->docked = 1;
1529 return aicon;
1532 #define COMPLAIN(key) wwarning(_("bad value in dock state info:%s"), key)
1534 WAppIcon *wClipRestoreState(WScreen * scr, WMPropList * clip_state)
1536 WAppIcon *icon;
1537 WMPropList *value;
1539 icon = mainIconCreate(scr, WM_CLIP);
1541 if (!clip_state)
1542 return icon;
1544 WMRetainPropList(clip_state);
1546 /* restore position */
1548 value = WMGetFromPLDictionary(clip_state, dPosition);
1550 if (value) {
1551 if (!WMIsPLString(value))
1552 COMPLAIN("Position");
1553 else {
1554 WMRect rect;
1555 int flags;
1557 if (sscanf(WMGetFromPLString(value), "%i,%i", &icon->x_pos, &icon->y_pos) != 2)
1558 COMPLAIN("Position");
1560 /* check position sanity */
1561 rect.pos.x = icon->x_pos;
1562 rect.pos.y = icon->y_pos;
1563 rect.size.width = rect.size.height = ICON_SIZE;
1565 wGetRectPlacementInfo(scr, rect, &flags);
1566 if (flags & (XFLAG_DEAD | XFLAG_PARTIAL))
1567 wScreenKeepInside(scr, &icon->x_pos, &icon->y_pos, ICON_SIZE, ICON_SIZE);
1570 #ifdef XDND /* was OFFIX */
1571 value = WMGetFromPLDictionary(clip_state, dDropCommand);
1572 if (value && WMIsPLString(value))
1573 icon->dnd_command = wstrdup(WMGetFromPLString(value));
1574 #endif
1576 value = WMGetFromPLDictionary(clip_state, dPasteCommand);
1577 if (value && WMIsPLString(value))
1578 icon->paste_command = wstrdup(WMGetFromPLString(value));
1580 WMReleasePropList(clip_state);
1582 return icon;
1585 WDock *wDockRestoreState(WScreen * scr, WMPropList * dock_state, int type)
1587 WDock *dock;
1588 WMPropList *apps;
1589 WMPropList *value;
1590 WAppIcon *aicon, *old_top;
1591 int count, i;
1593 dock = wDockCreate(scr, type);
1595 if (!dock_state)
1596 return dock;
1598 WMRetainPropList(dock_state);
1600 /* restore position */
1602 value = WMGetFromPLDictionary(dock_state, dPosition);
1604 if (value) {
1605 if (!WMIsPLString(value)) {
1606 COMPLAIN("Position");
1607 } else {
1608 WMRect rect;
1609 int flags;
1611 if (sscanf(WMGetFromPLString(value), "%i,%i", &dock->x_pos, &dock->y_pos) != 2)
1612 COMPLAIN("Position");
1614 /* check position sanity */
1615 rect.pos.x = dock->x_pos;
1616 rect.pos.y = dock->y_pos;
1617 rect.size.width = rect.size.height = ICON_SIZE;
1619 wGetRectPlacementInfo(scr, rect, &flags);
1620 if (flags & (XFLAG_DEAD | XFLAG_PARTIAL)) {
1621 int x = dock->x_pos;
1622 wScreenKeepInside(scr, &x, &dock->y_pos, ICON_SIZE, ICON_SIZE);
1625 /* Is this needed any more? */
1626 if (type == WM_CLIP) {
1627 if (dock->x_pos < 0) {
1628 dock->x_pos = 0;
1629 } else if (dock->x_pos > scr->scr_width - ICON_SIZE) {
1630 dock->x_pos = scr->scr_width - ICON_SIZE;
1632 } else {
1633 if (dock->x_pos >= 0) {
1634 dock->x_pos = DOCK_EXTRA_SPACE;
1635 dock->on_right_side = 0;
1636 } else {
1637 dock->x_pos = scr->scr_width - DOCK_EXTRA_SPACE - ICON_SIZE;
1638 dock->on_right_side = 1;
1644 /* restore lowered/raised state */
1646 dock->lowered = 0;
1648 value = WMGetFromPLDictionary(dock_state, dLowered);
1650 if (value) {
1651 if (!WMIsPLString(value)) {
1652 COMPLAIN("Lowered");
1653 } else {
1654 if (strcasecmp(WMGetFromPLString(value), "YES") == 0) {
1655 dock->lowered = 1;
1660 /* restore collapsed state */
1662 dock->collapsed = 0;
1664 value = WMGetFromPLDictionary(dock_state, dCollapsed);
1666 if (value) {
1667 if (!WMIsPLString(value)) {
1668 COMPLAIN("Collapsed");
1669 } else {
1670 if (strcasecmp(WMGetFromPLString(value), "YES") == 0) {
1671 dock->collapsed = 1;
1676 /* restore auto-collapsed state */
1678 value = WMGetFromPLDictionary(dock_state, dAutoCollapse);
1680 if (value) {
1681 if (!WMIsPLString(value)) {
1682 COMPLAIN("AutoCollapse");
1683 } else {
1684 if (strcasecmp(WMGetFromPLString(value), "YES") == 0) {
1685 dock->auto_collapse = 1;
1686 dock->collapsed = 1;
1691 /* restore auto-raise/lower state */
1693 value = WMGetFromPLDictionary(dock_state, dAutoRaiseLower);
1695 if (value) {
1696 if (!WMIsPLString(value)) {
1697 COMPLAIN("AutoRaiseLower");
1698 } else {
1699 if (strcasecmp(WMGetFromPLString(value), "YES") == 0) {
1700 dock->auto_raise_lower = 1;
1705 /* restore attract icons state */
1707 dock->attract_icons = 0;
1709 value = WMGetFromPLDictionary(dock_state, dAutoAttractIcons);
1711 if (value) {
1712 if (!WMIsPLString(value)) {
1713 COMPLAIN("AutoAttractIcons");
1714 } else {
1715 if (strcasecmp(WMGetFromPLString(value), "YES") == 0) {
1716 dock->attract_icons = 1;
1721 /* application list */
1724 WMPropList *tmp;
1725 char buffer[64];
1728 * When saving, it saves the dock state in
1729 * Applications and Applicationsnnn
1731 * When loading, it will first try Applicationsnnn.
1732 * If it does not exist, use Applications as default.
1735 snprintf(buffer, sizeof(buffer), "Applications%i", scr->scr_height);
1737 tmp = WMCreatePLString(buffer);
1738 apps = WMGetFromPLDictionary(dock_state, tmp);
1739 WMReleasePropList(tmp);
1741 if (!apps) {
1742 apps = WMGetFromPLDictionary(dock_state, dApplications);
1746 if (!apps) {
1747 goto finish;
1750 count = WMGetPropListItemCount(apps);
1752 if (count == 0)
1753 goto finish;
1755 old_top = dock->icon_array[0];
1757 /* dock->icon_count is set to 1 when dock is created.
1758 * Since Clip is already restored, we want to keep it so for clip,
1759 * but for dock we may change the default top tile, so we set it to 0.
1761 if (type == WM_DOCK)
1762 dock->icon_count = 0;
1764 for (i = 0; i < count; i++) {
1765 if (dock->icon_count >= dock->max_icons) {
1766 wwarning(_("there are too many icons stored in dock. Ignoring what doesn't fit"));
1767 break;
1770 value = WMGetFromPLArray(apps, i);
1771 aicon = restore_icon_state(scr, value, type, dock->icon_count);
1773 dock->icon_array[dock->icon_count] = aicon;
1775 if (aicon) {
1776 aicon->dock = dock;
1777 aicon->x_pos = dock->x_pos + (aicon->xindex * ICON_SIZE);
1778 aicon->y_pos = dock->y_pos + (aicon->yindex * ICON_SIZE);
1780 if (dock->lowered)
1781 ChangeStackingLevel(aicon->icon->core, WMNormalLevel);
1782 else
1783 ChangeStackingLevel(aicon->icon->core, WMDockLevel);
1785 wCoreConfigure(aicon->icon->core, aicon->x_pos, aicon->y_pos, 0, 0);
1787 if (!dock->collapsed)
1788 XMapWindow(dpy, aicon->icon->core->window);
1789 wRaiseFrame(aicon->icon->core);
1791 dock->icon_count++;
1792 } else if (dock->icon_count == 0 && type == WM_DOCK)
1793 dock->icon_count++;
1796 /* if the first icon is not defined, use the default */
1797 if (dock->icon_array[0] == NULL) {
1798 /* update default icon */
1799 old_top->x_pos = dock->x_pos;
1800 old_top->y_pos = dock->y_pos;
1801 if (dock->lowered)
1802 ChangeStackingLevel(old_top->icon->core, WMNormalLevel);
1803 else
1804 ChangeStackingLevel(old_top->icon->core, WMDockLevel);
1805 dock->icon_array[0] = old_top;
1806 XMoveWindow(dpy, old_top->icon->core->window, dock->x_pos, dock->y_pos);
1807 /* we don't need to increment dock->icon_count here because it was
1808 * incremented in the loop above.
1810 } else if (old_top != dock->icon_array[0]) {
1811 if (old_top == scr->clip_icon)
1812 scr->clip_icon = dock->icon_array[0];
1813 wAppIconDestroy(old_top);
1816 finish:
1817 WMReleasePropList(dock_state);
1819 return dock;
1822 void wDockLaunchWithState(WDock * dock, WAppIcon * btn, WSavedState * state)
1824 if (btn && btn->command && !btn->running && !btn->launching) {
1826 btn->drop_launch = 0;
1827 btn->paste_launch = 0;
1829 btn->pid = execCommand(btn, btn->command, state);
1831 if (btn->pid > 0) {
1832 if (!btn->forced_dock && !btn->buggy_app) {
1833 btn->launching = 1;
1834 dockIconPaint(btn);
1837 } else {
1838 wfree(state);
1842 void wDockDoAutoLaunch(WDock * dock, int workspace)
1844 WAppIcon *btn;
1845 WSavedState *state;
1846 int i;
1848 for (i = 0; i < dock->max_icons; i++) {
1849 btn = dock->icon_array[i];
1850 if (!btn || !btn->auto_launch)
1851 continue;
1853 state = wmalloc(sizeof(WSavedState));
1854 memset(state, 0, sizeof(WSavedState));
1855 state->workspace = workspace;
1856 /* TODO: this is klugy and is very difficult to understand
1857 * what's going on. Try to clean up */
1858 wDockLaunchWithState(dock, btn, state);
1862 #ifdef XDND /* was OFFIX */
1863 static WDock *findDock(WScreen * scr, XEvent * event, int *icon_pos)
1865 WDock *dock;
1866 int i;
1868 *icon_pos = -1;
1869 if ((dock = scr->dock) != NULL) {
1870 for (i = 0; i < dock->max_icons; i++) {
1871 if (dock->icon_array[i]
1872 && dock->icon_array[i]->icon->core->window == event->xclient.window) {
1873 *icon_pos = i;
1874 break;
1878 if (*icon_pos < 0 && (dock = scr->workspaces[scr->current_workspace]->clip) != NULL) {
1879 for (i = 0; i < dock->max_icons; i++) {
1880 if (dock->icon_array[i]
1881 && dock->icon_array[i]->icon->core->window == event->xclient.window) {
1882 *icon_pos = i;
1883 break;
1887 if (*icon_pos >= 0)
1888 return dock;
1889 return NULL;
1892 int wDockReceiveDNDDrop(WScreen * scr, XEvent * event)
1894 WDock *dock;
1895 WAppIcon *btn;
1896 int icon_pos;
1898 dock = findDock(scr, event, &icon_pos);
1899 if (!dock)
1900 return False;
1903 * Return True if the drop was on an application icon window.
1904 * In this case, let the ClientMessage handler redirect the
1905 * message to the app.
1907 if (dock->icon_array[icon_pos]->icon->icon_win != None)
1908 return True;
1910 if (dock->icon_array[icon_pos]->dnd_command != NULL) {
1911 scr->flags.dnd_data_convertion_status = 0;
1913 btn = dock->icon_array[icon_pos];
1915 if (!btn->forced_dock) {
1916 btn->relaunching = btn->running;
1917 btn->running = 1;
1919 if (btn->wm_instance || btn->wm_class) {
1920 WWindowAttributes attr;
1921 memset(&attr, 0, sizeof(WWindowAttributes));
1922 wDefaultFillAttributes(btn->icon->core->screen_ptr,
1923 btn->wm_instance, btn->wm_class, &attr, NULL, True);
1925 if (!attr.no_appicon)
1926 btn->launching = 1;
1927 else
1928 btn->running = 0;
1931 btn->paste_launch = 0;
1932 btn->drop_launch = 1;
1933 scr->last_dock = dock;
1934 btn->pid = execCommand(btn, btn->dnd_command, NULL);
1935 if (btn->pid > 0) {
1936 dockIconPaint(btn);
1937 } else {
1938 btn->launching = 0;
1939 if (!btn->relaunching) {
1940 btn->running = 0;
1944 return False;
1946 #endif /* XDND */
1948 Bool wDockAttachIcon(WDock * dock, WAppIcon * icon, int x, int y)
1950 WWindow *wwin;
1951 int index;
1953 wwin = icon->icon->owner;
1954 if (icon->command == NULL) {
1955 char *command;
1957 icon->editing = 0;
1959 command = GetCommandForWindow(wwin->client_win);
1960 if (command) {
1961 icon->command = command;
1962 } else {
1963 /* icon->forced_dock = 1; */
1964 if (dock->type != WM_CLIP || !icon->attracted) {
1965 icon->editing = 1;
1966 if (wInputDialog(dock->screen_ptr, _("Dock Icon"),
1967 _("Type the command used to launch the application"), &command)) {
1968 if (command && (command[0] == 0 || (command[0] == '-' && command[1] == 0))) {
1969 wfree(command);
1970 command = NULL;
1972 icon->command = command;
1973 icon->editing = 0;
1974 } else {
1975 icon->editing = 0;
1976 if (command)
1977 wfree(command);
1978 /* If the target is the dock, reject the icon. If
1979 * the target is the clip, make it an attracted icon
1981 if (dock->type == WM_CLIP) {
1982 icon->attracted = 1;
1983 if (!icon->icon->shadowed) {
1984 icon->icon->shadowed = 1;
1985 icon->icon->force_paint = 1;
1987 } else {
1988 return False;
1993 } else {
1994 icon->editing = 0;
1997 for (index = 1; index < dock->max_icons; index++)
1998 if (dock->icon_array[index] == NULL)
1999 break;
2000 /* if (index == dock->max_icons)
2001 return; */
2003 assert(index < dock->max_icons);
2005 dock->icon_array[index] = icon;
2006 icon->yindex = y;
2007 icon->xindex = x;
2009 icon->omnipresent = 0;
2011 icon->x_pos = dock->x_pos + x * ICON_SIZE;
2012 icon->y_pos = dock->y_pos + y * ICON_SIZE;
2014 dock->icon_count++;
2016 icon->running = 1;
2017 icon->launching = 0;
2018 icon->docked = 1;
2019 icon->dock = dock;
2020 icon->icon->core->descriptor.handle_mousedown = iconMouseDown;
2021 if (dock->type == WM_CLIP) {
2022 icon->icon->core->descriptor.handle_enternotify = clipEnterNotify;
2023 icon->icon->core->descriptor.handle_leavenotify = clipLeaveNotify;
2025 icon->icon->core->descriptor.parent_type = WCLASS_DOCK_ICON;
2026 icon->icon->core->descriptor.parent = icon;
2028 MoveInStackListUnder(dock->icon_array[index - 1]->icon->core, icon->icon->core);
2029 wAppIconMove(icon, icon->x_pos, icon->y_pos);
2030 wAppIconPaint(icon);
2032 if (wPreferences.auto_arrange_icons)
2033 wArrangeIcons(dock->screen_ptr, True);
2035 #ifdef XDND /* was OFFIX */
2036 if (icon->command && !icon->dnd_command) {
2037 int len = strlen(icon->command) + 8;
2038 icon->dnd_command = wmalloc(len);
2039 snprintf(icon->dnd_command, len, "%s %%d", icon->command);
2041 #endif
2043 if (icon->command && !icon->paste_command) {
2044 int len = strlen(icon->command) + 8;
2045 icon->paste_command = wmalloc(len);
2046 snprintf(icon->paste_command, len, "%s %%s", icon->command);
2049 return True;
2052 void reattachIcon(WDock * dock, WAppIcon * icon, int x, int y)
2054 int index;
2056 for (index = 1; index < dock->max_icons; index++) {
2057 if (dock->icon_array[index] == icon)
2058 break;
2060 assert(index < dock->max_icons);
2062 icon->yindex = y;
2063 icon->xindex = x;
2065 icon->x_pos = dock->x_pos + x * ICON_SIZE;
2066 icon->y_pos = dock->y_pos + y * ICON_SIZE;
2069 Bool moveIconBetweenDocks(WDock * src, WDock * dest, WAppIcon * icon, int x, int y)
2071 WWindow *wwin;
2072 char *command;
2073 int index;
2075 if (src == dest)
2076 return True; /* No move needed, we're already there */
2078 if (dest == NULL)
2079 return False;
2081 wwin = icon->icon->owner;
2084 * For the moment we can't do this if we move icons in Clip from one
2085 * workspace to other, because if we move two or more icons without
2086 * command, the dialog box will not be able to tell us to which of the
2087 * moved icons it applies. -Dan
2089 if ((dest->type == WM_DOCK /*|| dest->keep_attracted */ ) && icon->command == NULL) {
2090 command = GetCommandForWindow(wwin->client_win);
2091 if (command) {
2092 icon->command = command;
2093 } else {
2094 icon->editing = 1;
2095 /* icon->forced_dock = 1; */
2096 if (wInputDialog(src->screen_ptr, _("Dock Icon"),
2097 _("Type the command used to launch the application"), &command)) {
2098 if (command && (command[0] == 0 || (command[0] == '-' && command[1] == 0))) {
2099 wfree(command);
2100 command = NULL;
2102 icon->command = command;
2103 } else {
2104 icon->editing = 0;
2105 if (command)
2106 wfree(command);
2107 return False;
2109 icon->editing = 0;
2113 if (dest->type == WM_DOCK)
2114 wClipMakeIconOmnipresent(icon, False);
2116 for (index = 1; index < src->max_icons; index++) {
2117 if (src->icon_array[index] == icon)
2118 break;
2120 assert(index < src->max_icons);
2122 src->icon_array[index] = NULL;
2123 src->icon_count--;
2125 for (index = 1; index < dest->max_icons; index++) {
2126 if (dest->icon_array[index] == NULL)
2127 break;
2129 /* if (index == dest->max_icons)
2130 return; */
2132 assert(index < dest->max_icons);
2134 dest->icon_array[index] = icon;
2135 icon->dock = dest;
2137 /* deselect the icon */
2138 if (icon->icon->selected)
2139 wIconSelect(icon->icon);
2141 if (dest->type == WM_DOCK) {
2142 icon->icon->core->descriptor.handle_enternotify = NULL;
2143 icon->icon->core->descriptor.handle_leavenotify = NULL;
2144 } else {
2145 icon->icon->core->descriptor.handle_enternotify = clipEnterNotify;
2146 icon->icon->core->descriptor.handle_leavenotify = clipLeaveNotify;
2149 /* set it to be kept when moving to dock.
2150 * Unless the icon does not have a command set
2152 if (icon->command && dest->type == WM_DOCK) {
2153 icon->attracted = 0;
2154 if (icon->icon->shadowed) {
2155 icon->icon->shadowed = 0;
2156 icon->icon->force_paint = 1;
2160 if (src->auto_collapse || src->auto_raise_lower)
2161 clipLeave(src);
2163 icon->yindex = y;
2164 icon->xindex = x;
2166 icon->x_pos = dest->x_pos + x * ICON_SIZE;
2167 icon->y_pos = dest->y_pos + y * ICON_SIZE;
2169 dest->icon_count++;
2171 MoveInStackListUnder(dest->icon_array[index - 1]->icon->core, icon->icon->core);
2172 wAppIconPaint(icon);
2174 return True;
2177 void wDockDetach(WDock * dock, WAppIcon * icon)
2179 int index;
2181 /* make the settings panel be closed */
2182 if (icon->panel) {
2183 DestroyDockAppSettingsPanel(icon->panel);
2186 /* This must be called before icon->dock is set to NULL.
2187 * Don't move it. -Dan
2189 wClipMakeIconOmnipresent(icon, False);
2191 icon->docked = 0;
2192 icon->dock = NULL;
2193 icon->attracted = 0;
2194 icon->auto_launch = 0;
2195 if (icon->icon->shadowed) {
2196 icon->icon->shadowed = 0;
2197 icon->icon->force_paint = 1;
2200 /* deselect the icon */
2201 if (icon->icon->selected)
2202 wIconSelect(icon->icon);
2204 if (icon->command) {
2205 wfree(icon->command);
2206 icon->command = NULL;
2208 #ifdef XDND /* was OFFIX */
2209 if (icon->dnd_command) {
2210 wfree(icon->dnd_command);
2211 icon->dnd_command = NULL;
2213 #endif
2214 if (icon->paste_command) {
2215 wfree(icon->paste_command);
2216 icon->paste_command = NULL;
2219 for (index = 1; index < dock->max_icons; index++)
2220 if (dock->icon_array[index] == icon)
2221 break;
2222 assert(index < dock->max_icons);
2223 dock->icon_array[index] = NULL;
2224 icon->yindex = -1;
2225 icon->xindex = -1;
2227 dock->icon_count--;
2229 /* if the dock is not attached to an application or
2230 * the the application did not set the approriate hints yet,
2231 * destroy the icon */
2232 if (!icon->running || !wApplicationOf(icon->main_window))
2233 wAppIconDestroy(icon);
2234 else {
2235 icon->icon->core->descriptor.handle_mousedown = appIconMouseDown;
2236 icon->icon->core->descriptor.handle_enternotify = NULL;
2237 icon->icon->core->descriptor.handle_leavenotify = NULL;
2238 icon->icon->core->descriptor.parent_type = WCLASS_APPICON;
2239 icon->icon->core->descriptor.parent = icon;
2241 ChangeStackingLevel(icon->icon->core, NORMAL_ICON_LEVEL);
2243 wAppIconPaint(icon);
2244 if (wPreferences.auto_arrange_icons) {
2245 wArrangeIcons(dock->screen_ptr, True);
2248 if (dock->auto_collapse || dock->auto_raise_lower)
2249 clipLeave(dock);
2253 * returns the closest Dock slot index for the passed
2254 * coordinates.
2256 * Returns False if icon can't be docked.
2258 * Note: this function should NEVER alter ret_x or ret_y, unless it will
2259 * return True. -Dan
2261 Bool wDockSnapIcon(WDock * dock, WAppIcon * icon, int req_x, int req_y, int *ret_x, int *ret_y, int redocking)
2263 WScreen *scr = dock->screen_ptr;
2264 int dx, dy;
2265 int ex_x, ex_y;
2266 int i, offset = ICON_SIZE / 2;
2267 WAppIcon *aicon = NULL;
2268 WAppIcon *nicon = NULL;
2269 int max_y_icons, max_x_icons;
2271 /* TODO: XINERAMA, for these */
2272 max_x_icons = scr->scr_width / ICON_SIZE;
2273 max_y_icons = scr->scr_height / ICON_SIZE - 1;
2275 if (wPreferences.flags.noupdates)
2276 return False;
2278 dx = dock->x_pos;
2279 dy = dock->y_pos;
2281 /* if the dock is full */
2282 if (!redocking && (dock->icon_count >= dock->max_icons)) {
2283 return False;
2286 /* exact position */
2287 if (req_y < dy)
2288 ex_y = (req_y - offset - dy) / ICON_SIZE;
2289 else
2290 ex_y = (req_y + offset - dy) / ICON_SIZE;
2292 if (req_x < dx)
2293 ex_x = (req_x - offset - dx) / ICON_SIZE;
2294 else
2295 ex_x = (req_x + offset - dx) / ICON_SIZE;
2297 /* check if the icon is outside the screen boundaries */
2299 WMRect rect;
2300 int flags;
2302 rect.pos.x = dx + ex_x * ICON_SIZE;
2303 rect.pos.y = dy + ex_y * ICON_SIZE;
2304 rect.size.width = rect.size.height = ICON_SIZE;
2306 wGetRectPlacementInfo(scr, rect, &flags);
2307 if (flags & (XFLAG_DEAD | XFLAG_PARTIAL))
2308 return False;
2311 if (dock->type == WM_DOCK) {
2312 if (icon->dock != dock && ex_x != 0)
2313 return False;
2315 aicon = NULL;
2316 for (i = 0; i < dock->max_icons; i++) {
2317 nicon = dock->icon_array[i];
2318 if (nicon && nicon->yindex == ex_y) {
2319 aicon = nicon;
2320 break;
2324 if (redocking) {
2325 int sig, done, closest;
2327 /* Possible cases when redocking:
2329 * icon dragged out of range of any slot -> false
2330 * icon dragged to range of free slot
2331 * icon dragged to range of same slot
2332 * icon dragged to range of different icon
2334 if (abs(ex_x) > DOCK_DETTACH_THRESHOLD)
2335 return False;
2337 if (ex_y >= 0 && ex_y <= max_y_icons && (aicon == icon || !aicon)) {
2338 *ret_x = 0;
2339 *ret_y = ex_y;
2340 return True;
2343 /* start looking at the upper slot or lower? */
2344 if (ex_y * ICON_SIZE < (req_y + offset - dy))
2345 sig = 1;
2346 else
2347 sig = -1;
2349 closest = -1;
2350 done = 0;
2351 /* look for closest free slot */
2352 for (i = 0; i < (DOCK_DETTACH_THRESHOLD + 1) * 2 && !done; i++) {
2353 int j;
2355 done = 1;
2356 closest = sig * (i / 2) + ex_y;
2357 /* check if this slot is used */
2358 if (closest >= 0) {
2359 for (j = 0; j < dock->max_icons; j++) {
2360 if (dock->icon_array[j]
2361 && dock->icon_array[j]->yindex == closest) {
2362 /* slot is used by someone else */
2363 if (dock->icon_array[j] != icon)
2364 done = 0;
2365 break;
2369 sig = -sig;
2371 if (done && closest >= 0 && closest <= max_y_icons &&
2372 ((ex_y >= closest && ex_y - closest < DOCK_DETTACH_THRESHOLD + 1)
2373 || (ex_y < closest && closest - ex_y <= DOCK_DETTACH_THRESHOLD + 1))) {
2374 *ret_x = 0;
2375 *ret_y = closest;
2376 return True;
2378 } else { /* !redocking */
2380 /* if slot is free and the icon is close enough, return it */
2381 if (!aicon && ex_x == 0 && ex_y >= 0 && ex_y <= max_y_icons) {
2382 *ret_x = 0;
2383 *ret_y = ex_y;
2384 return True;
2387 } else { /* CLIP */
2388 int neighbours = 0;
2389 int start, stop, k;
2391 start = icon->omnipresent ? 0 : scr->current_workspace;
2392 stop = icon->omnipresent ? scr->workspace_count : start + 1;
2394 aicon = NULL;
2395 for (k = start; k < stop; k++) {
2396 WDock *tmp = scr->workspaces[k]->clip;
2397 if (!tmp)
2398 continue;
2399 for (i = 0; i < tmp->max_icons; i++) {
2400 nicon = tmp->icon_array[i];
2401 if (nicon && nicon->xindex == ex_x && nicon->yindex == ex_y) {
2402 aicon = nicon;
2403 break;
2406 if (aicon)
2407 break;
2409 for (k = start; k < stop; k++) {
2410 WDock *tmp = scr->workspaces[k]->clip;
2411 if (!tmp)
2412 continue;
2413 for (i = 0; i < tmp->max_icons; i++) {
2414 nicon = tmp->icon_array[i];
2415 if (nicon && nicon != icon && /* Icon can't be it's own neighbour */
2416 (abs(nicon->xindex - ex_x) <= CLIP_ATTACH_VICINITY &&
2417 abs(nicon->yindex - ex_y) <= CLIP_ATTACH_VICINITY)) {
2418 neighbours = 1;
2419 break;
2422 if (neighbours)
2423 break;
2426 if (neighbours && (aicon == NULL || (redocking && aicon == icon))) {
2427 *ret_x = ex_x;
2428 *ret_y = ex_y;
2429 return True;
2432 return False;
2435 static int onScreen(WScreen * scr, int x, int y, int sx, int ex, int sy, int ey)
2437 WMRect rect = wmkrect(x, y, ICON_SIZE, ICON_SIZE);
2438 int flags;
2440 wGetRectPlacementInfo(scr, rect, &flags);
2442 return !(flags & (XFLAG_DEAD | XFLAG_PARTIAL));
2446 * returns true if it can find a free slot in the dock,
2447 * in which case it changes x_pos and y_pos accordingly.
2448 * Else returns false.
2450 Bool wDockFindFreeSlot(WDock * dock, int *x_pos, int *y_pos)
2452 WScreen *scr = dock->screen_ptr;
2453 WAppIcon *btn;
2454 WAppIconChain *chain;
2455 unsigned char *slot_map;
2456 int mwidth;
2457 int r;
2458 int x, y;
2459 int i, done = False;
2460 int corner;
2461 int sx = 0, sy = 0, ex = scr->scr_width, ey = scr->scr_height;
2462 int extra_count = 0;
2464 if (dock->type == WM_CLIP && dock != scr->workspaces[scr->current_workspace]->clip)
2465 extra_count = scr->global_icon_count;
2467 /* if the dock is full */
2468 if (dock->icon_count + extra_count >= dock->max_icons) {
2469 return False;
2472 if (!wPreferences.flags.nodock && scr->dock) {
2473 if (scr->dock->on_right_side)
2474 ex -= ICON_SIZE + DOCK_EXTRA_SPACE;
2475 else
2476 sx += ICON_SIZE + DOCK_EXTRA_SPACE;
2479 if (ex < dock->x_pos)
2480 ex = dock->x_pos;
2481 if (sx > dock->x_pos + ICON_SIZE)
2482 sx = dock->x_pos + ICON_SIZE;
2483 #define C_NONE 0
2484 #define C_NW 1
2485 #define C_NE 2
2486 #define C_SW 3
2487 #define C_SE 4
2489 /* check if clip is in a corner */
2490 if (dock->type == WM_CLIP) {
2491 if (dock->x_pos < 1 && dock->y_pos < 1)
2492 corner = C_NE;
2493 else if (dock->x_pos < 1 && dock->y_pos >= (ey - ICON_SIZE))
2494 corner = C_SE;
2495 else if (dock->x_pos >= (ex - ICON_SIZE) && dock->y_pos >= (ey - ICON_SIZE))
2496 corner = C_SW;
2497 else if (dock->x_pos >= (ex - ICON_SIZE) && dock->y_pos < 1)
2498 corner = C_NW;
2499 else
2500 corner = C_NONE;
2501 } else
2502 corner = C_NONE;
2504 /* If the clip is in the corner, use only slots that are in the border
2505 * of the screen */
2506 if (corner != C_NONE) {
2507 char *hmap, *vmap;
2508 int hcount, vcount;
2510 hcount = WMIN(dock->max_icons, scr->scr_width / ICON_SIZE);
2511 vcount = WMIN(dock->max_icons, scr->scr_height / ICON_SIZE);
2512 hmap = wmalloc(hcount + 1);
2513 memset(hmap, 0, hcount + 1);
2514 vmap = wmalloc(vcount + 1);
2515 memset(vmap, 0, vcount + 1);
2517 /* mark used positions */
2518 switch (corner) {
2519 case C_NE:
2520 for (i = 0; i < dock->max_icons; i++) {
2521 btn = dock->icon_array[i];
2522 if (!btn)
2523 continue;
2525 if (btn->xindex == 0 && btn->yindex > 0 && btn->yindex < vcount)
2526 vmap[btn->yindex] = 1;
2527 else if (btn->yindex == 0 && btn->xindex > 0 && btn->xindex < hcount)
2528 hmap[btn->xindex] = 1;
2530 for (chain = scr->global_icons; chain != NULL; chain = chain->next) {
2531 btn = chain->aicon;
2532 if (btn->xindex == 0 && btn->yindex > 0 && btn->yindex < vcount)
2533 vmap[btn->yindex] = 1;
2534 else if (btn->yindex == 0 && btn->xindex > 0 && btn->xindex < hcount)
2535 hmap[btn->xindex] = 1;
2537 break;
2538 case C_NW:
2539 for (i = 0; i < dock->max_icons; i++) {
2540 btn = dock->icon_array[i];
2541 if (!btn)
2542 continue;
2544 if (btn->xindex == 0 && btn->yindex > 0 && btn->yindex < vcount)
2545 vmap[btn->yindex] = 1;
2546 else if (btn->yindex == 0 && btn->xindex < 0 && btn->xindex > -hcount)
2547 hmap[-btn->xindex] = 1;
2549 for (chain = scr->global_icons; chain != NULL; chain = chain->next) {
2550 btn = chain->aicon;
2551 if (btn->xindex == 0 && btn->yindex > 0 && btn->yindex < vcount)
2552 vmap[btn->yindex] = 1;
2553 else if (btn->yindex == 0 && btn->xindex < 0 && btn->xindex > -hcount)
2554 hmap[-btn->xindex] = 1;
2556 break;
2557 case C_SE:
2558 for (i = 0; i < dock->max_icons; i++) {
2559 btn = dock->icon_array[i];
2560 if (!btn)
2561 continue;
2563 if (btn->xindex == 0 && btn->yindex < 0 && btn->yindex > -vcount)
2564 vmap[-btn->yindex] = 1;
2565 else if (btn->yindex == 0 && btn->xindex > 0 && btn->xindex < hcount)
2566 hmap[btn->xindex] = 1;
2568 for (chain = scr->global_icons; chain != NULL; chain = chain->next) {
2569 btn = chain->aicon;
2570 if (btn->xindex == 0 && btn->yindex < 0 && btn->yindex > -vcount)
2571 vmap[-btn->yindex] = 1;
2572 else if (btn->yindex == 0 && btn->xindex > 0 && btn->xindex < hcount)
2573 hmap[btn->xindex] = 1;
2575 break;
2576 case C_SW:
2577 default:
2578 for (i = 0; i < dock->max_icons; i++) {
2579 btn = dock->icon_array[i];
2580 if (!btn)
2581 continue;
2583 if (btn->xindex == 0 && btn->yindex < 0 && btn->yindex > -vcount)
2584 vmap[-btn->yindex] = 1;
2585 else if (btn->yindex == 0 && btn->xindex < 0 && btn->xindex > -hcount)
2586 hmap[-btn->xindex] = 1;
2588 for (chain = scr->global_icons; chain != NULL; chain = chain->next) {
2589 btn = chain->aicon;
2590 if (btn->xindex == 0 && btn->yindex < 0 && btn->yindex > -vcount)
2591 vmap[-btn->yindex] = 1;
2592 else if (btn->yindex == 0 && btn->xindex < 0 && btn->xindex > -hcount)
2593 hmap[-btn->xindex] = 1;
2596 x = 0;
2597 y = 0;
2598 done = 0;
2599 /* search a vacant slot */
2600 for (i = 1; i < WMAX(vcount, hcount); i++) {
2601 if (i < vcount && vmap[i] == 0) {
2602 /* found a slot */
2603 x = 0;
2604 y = i;
2605 done = 1;
2606 break;
2607 } else if (i < hcount && hmap[i] == 0) {
2608 /* found a slot */
2609 x = i;
2610 y = 0;
2611 done = 1;
2612 break;
2615 wfree(vmap);
2616 wfree(hmap);
2617 /* If found a slot, translate and return */
2618 if (done) {
2619 if (corner == C_NW || corner == C_NE) {
2620 *y_pos = y;
2621 } else {
2622 *y_pos = -y;
2624 if (corner == C_NE || corner == C_SE) {
2625 *x_pos = x;
2626 } else {
2627 *x_pos = -x;
2629 return True;
2631 /* else, try to find a slot somewhere else */
2634 /* a map of mwidth x mwidth would be enough if we allowed icons to be
2635 * placed outside of screen */
2636 mwidth = (int)ceil(sqrt(dock->max_icons));
2638 /* In the worst case (the clip is in the corner of the screen),
2639 * the amount of icons that fit in the clip is smaller.
2640 * Double the map to get a safe value.
2642 mwidth += mwidth;
2644 r = (mwidth - 1) / 2;
2646 slot_map = wmalloc(mwidth * mwidth);
2647 memset(slot_map, 0, mwidth * mwidth);
2649 #define XY2OFS(x,y) (WMAX(abs(x),abs(y)) > r) ? 0 : (((y)+r)*(mwidth)+(x)+r)
2651 /* mark used slots in the map. If the slot falls outside the map
2652 * (for example, when all icons are placed in line), ignore them. */
2653 for (i = 0; i < dock->max_icons; i++) {
2654 btn = dock->icon_array[i];
2655 if (btn)
2656 slot_map[XY2OFS(btn->xindex, btn->yindex)] = 1;
2658 for (chain = scr->global_icons; chain != NULL; chain = chain->next) {
2659 slot_map[XY2OFS(chain->aicon->xindex, chain->aicon->yindex)] = 1;
2661 /* Find closest slot from the center that is free by scanning the
2662 * map from the center to outward in circular passes.
2663 * This will not result in a neat layout, but will be optimal
2664 * in the sense that there will not be holes left.
2666 done = 0;
2667 for (i = 1; i <= r && !done; i++) {
2668 int tx, ty;
2670 /* top and bottom parts of the ring */
2671 for (x = -i; x <= i && !done; x++) {
2672 tx = dock->x_pos + x * ICON_SIZE;
2673 y = -i;
2674 ty = dock->y_pos + y * ICON_SIZE;
2675 if (slot_map[XY2OFS(x, y)] == 0 && onScreen(scr, tx, ty, sx, ex, sy, ey)) {
2676 *x_pos = x;
2677 *y_pos = y;
2678 done = 1;
2679 break;
2681 y = i;
2682 ty = dock->y_pos + y * ICON_SIZE;
2683 if (slot_map[XY2OFS(x, y)] == 0 && onScreen(scr, tx, ty, sx, ex, sy, ey)) {
2684 *x_pos = x;
2685 *y_pos = y;
2686 done = 1;
2687 break;
2690 /* left and right parts of the ring */
2691 for (y = -i + 1; y <= i - 1; y++) {
2692 ty = dock->y_pos + y * ICON_SIZE;
2693 x = -i;
2694 tx = dock->x_pos + x * ICON_SIZE;
2695 if (slot_map[XY2OFS(x, y)] == 0 && onScreen(scr, tx, ty, sx, ex, sy, ey)) {
2696 *x_pos = x;
2697 *y_pos = y;
2698 done = 1;
2699 break;
2701 x = i;
2702 tx = dock->x_pos + x * ICON_SIZE;
2703 if (slot_map[XY2OFS(x, y)] == 0 && onScreen(scr, tx, ty, sx, ex, sy, ey)) {
2704 *x_pos = x;
2705 *y_pos = y;
2706 done = 1;
2707 break;
2711 wfree(slot_map);
2712 #undef XY2OFS
2713 return done;
2716 static void moveDock(WDock * dock, int new_x, int new_y)
2718 WAppIcon *btn;
2719 int i;
2721 dock->x_pos = new_x;
2722 dock->y_pos = new_y;
2723 for (i = 0; i < dock->max_icons; i++) {
2724 btn = dock->icon_array[i];
2725 if (btn) {
2726 btn->x_pos = new_x + btn->xindex * ICON_SIZE;
2727 btn->y_pos = new_y + btn->yindex * ICON_SIZE;
2728 XMoveWindow(dpy, btn->icon->core->window, btn->x_pos, btn->y_pos);
2733 static void swapDock(WDock * dock)
2735 WScreen *scr = dock->screen_ptr;
2736 WAppIcon *btn;
2737 int x, i;
2739 if (dock->on_right_side) {
2740 x = dock->x_pos = scr->scr_width - ICON_SIZE - DOCK_EXTRA_SPACE;
2741 } else {
2742 x = dock->x_pos = DOCK_EXTRA_SPACE;
2745 for (i = 0; i < dock->max_icons; i++) {
2746 btn = dock->icon_array[i];
2747 if (btn) {
2748 btn->x_pos = x;
2749 XMoveWindow(dpy, btn->icon->core->window, btn->x_pos, btn->y_pos);
2753 wScreenUpdateUsableArea(scr);
2756 static pid_t execCommand(WAppIcon * btn, char *command, WSavedState * state)
2758 WScreen *scr = btn->icon->core->screen_ptr;
2759 pid_t pid;
2760 char **argv;
2761 int argc;
2762 char *cmdline;
2764 cmdline = ExpandOptions(scr, command);
2766 if (scr->flags.dnd_data_convertion_status || !cmdline) {
2767 if (cmdline)
2768 wfree(cmdline);
2769 if (state)
2770 wfree(state);
2771 return 0;
2774 wtokensplit(cmdline, &argv, &argc);
2776 if (!argc) {
2777 if (cmdline)
2778 wfree(cmdline);
2779 if (state)
2780 wfree(state);
2781 return 0;
2784 if ((pid = fork()) == 0) {
2785 char **args;
2786 int i;
2788 SetupEnvironment(scr);
2790 #ifdef HAVE_SETSID
2791 setsid();
2792 #endif
2794 args = malloc(sizeof(char *) * (argc + 1));
2795 if (!args)
2796 exit(111);
2797 for (i = 0; i < argc; i++) {
2798 args[i] = argv[i];
2800 args[argc] = NULL;
2801 execvp(argv[0], args);
2802 exit(111);
2804 wtokenfree(argv, argc);
2806 if (pid > 0) {
2807 if (!state) {
2808 state = wmalloc(sizeof(WSavedState));
2809 memset(state, 0, sizeof(WSavedState));
2810 state->hidden = -1;
2811 state->miniaturized = -1;
2812 state->shaded = -1;
2813 if (btn->dock == scr->dock || btn->omnipresent)
2814 state->workspace = -1;
2815 else
2816 state->workspace = scr->current_workspace;
2818 wWindowAddSavedState(btn->wm_instance, btn->wm_class, cmdline, pid, state);
2819 wAddDeathHandler(pid, (WDeathHandler *) trackDeadProcess, btn->dock);
2820 } else if (state) {
2821 wfree(state);
2823 wfree(cmdline);
2824 return pid;
2827 void wDockHideIcons(WDock * dock)
2829 int i;
2831 if (dock == NULL)
2832 return;
2834 for (i = 1; i < dock->max_icons; i++) {
2835 if (dock->icon_array[i])
2836 XUnmapWindow(dpy, dock->icon_array[i]->icon->core->window);
2838 dock->mapped = 0;
2840 dockIconPaint(dock->icon_array[0]);
2843 void wDockShowIcons(WDock * dock)
2845 int i, newlevel;
2846 WAppIcon *btn;
2848 if (dock == NULL)
2849 return;
2851 btn = dock->icon_array[0];
2852 moveDock(dock, btn->x_pos, btn->y_pos);
2854 newlevel = dock->lowered ? WMNormalLevel : WMDockLevel;
2855 ChangeStackingLevel(btn->icon->core, newlevel);
2857 for (i = 1; i < dock->max_icons; i++) {
2858 if (dock->icon_array[i]) {
2859 MoveInStackListAbove(dock->icon_array[i]->icon->core, btn->icon->core);
2860 break;
2864 if (!dock->collapsed) {
2865 for (i = 1; i < dock->max_icons; i++) {
2866 if (dock->icon_array[i]) {
2867 XMapWindow(dpy, dock->icon_array[i]->icon->core->window);
2871 dock->mapped = 1;
2873 dockIconPaint(btn);
2876 void wDockLower(WDock * dock)
2878 int i;
2880 for (i = 0; i < dock->max_icons; i++) {
2881 if (dock->icon_array[i])
2882 wLowerFrame(dock->icon_array[i]->icon->core);
2886 void wDockRaise(WDock * dock)
2888 int i;
2890 for (i = dock->max_icons - 1; i >= 0; i--) {
2891 if (dock->icon_array[i])
2892 wRaiseFrame(dock->icon_array[i]->icon->core);
2896 void wDockRaiseLower(WDock * dock)
2898 if (!dock->icon_array[0]->icon->core->stacking->above
2899 || (dock->icon_array[0]->icon->core->stacking->window_level
2900 != dock->icon_array[0]->icon->core->stacking->above->stacking->window_level))
2901 wDockLower(dock);
2902 else
2903 wDockRaise(dock);
2906 void wDockFinishLaunch(WDock * dock, WAppIcon * icon)
2908 icon->launching = 0;
2909 icon->relaunching = 0;
2910 dockIconPaint(icon);
2913 WAppIcon *wDockFindIconForWindow(WDock * dock, Window window)
2915 WAppIcon *icon;
2916 int i;
2918 for (i = 0; i < dock->max_icons; i++) {
2919 icon = dock->icon_array[i];
2920 if (icon && icon->main_window == window)
2921 return icon;
2923 return NULL;
2926 void wDockTrackWindowLaunch(WDock * dock, Window window)
2928 WAppIcon *icon;
2929 char *wm_class, *wm_instance;
2930 int i;
2931 Bool firstPass = True;
2932 Bool found = False;
2933 char *command = NULL;
2935 command = GetCommandForWindow(window);
2937 if (!PropGetWMClass(window, &wm_class, &wm_instance) || (!wm_class && !wm_instance)) {
2939 if (command)
2940 wfree(command);
2941 return;
2944 retry:
2945 for (i = 0; i < dock->max_icons; i++) {
2946 icon = dock->icon_array[i];
2947 if (!icon)
2948 continue;
2950 /* app is already attached to icon */
2951 if (icon->main_window == window) {
2952 found = True;
2953 break;
2956 if ((icon->wm_instance || icon->wm_class)
2957 && (icon->launching || !icon->running)) {
2959 if (icon->wm_instance && wm_instance && strcmp(icon->wm_instance, wm_instance) != 0) {
2960 continue;
2962 if (icon->wm_class && wm_class && strcmp(icon->wm_class, wm_class) != 0) {
2963 continue;
2965 if (firstPass && command && strcmp(icon->command, command) != 0) {
2966 continue;
2969 if (!icon->relaunching) {
2970 WApplication *wapp;
2972 /* Possibly an application that was docked with dockit,
2973 * but the user did not update WMState to indicate that
2974 * it was docked by force */
2975 wapp = wApplicationOf(window);
2976 if (!wapp) {
2977 icon->forced_dock = 1;
2978 icon->running = 0;
2980 if (!icon->forced_dock) {
2981 icon->main_window = window;
2984 found = True;
2985 if (!wPreferences.no_animations && !icon->launching &&
2986 !dock->screen_ptr->flags.startup && !dock->collapsed) {
2987 WAppIcon *aicon;
2988 int x0, y0;
2990 icon->launching = 1;
2991 dockIconPaint(icon);
2993 aicon = wAppIconCreateForDock(dock->screen_ptr, NULL,
2994 wm_instance, wm_class, TILE_NORMAL);
2995 /* XXX: can: aicon->icon == NULL ? */
2996 PlaceIcon(dock->screen_ptr, &x0, &y0, wGetHeadForWindow(aicon->icon->owner));
2997 wAppIconMove(aicon, x0, y0);
2998 /* Should this always be lowered? -Dan */
2999 if (dock->lowered)
3000 wLowerFrame(aicon->icon->core);
3001 XMapWindow(dpy, aicon->icon->core->window);
3002 aicon->launching = 1;
3003 wAppIconPaint(aicon);
3004 SlideWindow(aicon->icon->core->window, x0, y0, icon->x_pos, icon->y_pos);
3005 XUnmapWindow(dpy, aicon->icon->core->window);
3006 wAppIconDestroy(aicon);
3008 wDockFinishLaunch(dock, icon);
3009 break;
3013 if (firstPass && !found) {
3014 firstPass = False;
3015 goto retry;
3018 if (command)
3019 wfree(command);
3021 if (wm_class)
3022 XFree(wm_class);
3023 if (wm_instance)
3024 XFree(wm_instance);
3027 void wClipUpdateForWorkspaceChange(WScreen * scr, int workspace)
3029 if (!wPreferences.flags.noclip) {
3030 scr->clip_icon->dock = scr->workspaces[workspace]->clip;
3031 if (scr->current_workspace != workspace) {
3032 WDock *old_clip = scr->workspaces[scr->current_workspace]->clip;
3033 WAppIconChain *chain = scr->global_icons;
3035 while (chain) {
3036 moveIconBetweenDocks(chain->aicon->dock,
3037 scr->workspaces[workspace]->clip,
3038 chain->aicon, chain->aicon->xindex, chain->aicon->yindex);
3039 if (scr->workspaces[workspace]->clip->collapsed)
3040 XUnmapWindow(dpy, chain->aicon->icon->core->window);
3041 chain = chain->next;
3044 wDockHideIcons(old_clip);
3045 if (old_clip->auto_raise_lower) {
3046 if (old_clip->auto_raise_magic) {
3047 WMDeleteTimerHandler(old_clip->auto_raise_magic);
3048 old_clip->auto_raise_magic = NULL;
3050 wDockLower(old_clip);
3052 if (old_clip->auto_collapse) {
3053 if (old_clip->auto_expand_magic) {
3054 WMDeleteTimerHandler(old_clip->auto_expand_magic);
3055 old_clip->auto_expand_magic = NULL;
3057 old_clip->collapsed = 1;
3059 wDockShowIcons(scr->workspaces[workspace]->clip);
3061 if (scr->flags.clip_balloon_mapped)
3062 showClipBalloon(scr->clip_icon->dock, workspace);
3066 static void trackDeadProcess(pid_t pid, unsigned char status, WDock * dock)
3068 WAppIcon *icon;
3069 int i;
3071 for (i = 0; i < dock->max_icons; i++) {
3072 icon = dock->icon_array[i];
3073 if (!icon)
3074 continue;
3076 if (icon->launching && icon->pid == pid) {
3077 if (!icon->relaunching) {
3078 icon->running = 0;
3079 icon->main_window = None;
3081 wDockFinishLaunch(dock, icon);
3082 icon->pid = 0;
3083 if (status == 111) {
3084 char msg[PATH_MAX];
3085 char *cmd;
3087 #ifdef XDND
3088 if (icon->drop_launch)
3089 cmd = icon->dnd_command;
3090 else
3091 #endif
3092 if (icon->paste_launch)
3093 cmd = icon->paste_command;
3094 else
3095 cmd = icon->command;
3097 snprintf(msg, sizeof(msg), _("Could not execute command \"%s\""), cmd);
3099 wMessageDialog(dock->screen_ptr, _("Error"), msg, _("OK"), NULL, NULL);
3101 break;
3106 static void toggleLowered(WDock * dock)
3108 WAppIcon *tmp;
3109 int newlevel, i;
3111 /* lower/raise Dock */
3112 if (!dock->lowered) {
3113 newlevel = WMNormalLevel;
3114 dock->lowered = 1;
3115 } else {
3116 newlevel = WMDockLevel;
3117 dock->lowered = 0;
3120 for (i = 0; i < dock->max_icons; i++) {
3121 tmp = dock->icon_array[i];
3122 if (!tmp)
3123 continue;
3125 ChangeStackingLevel(tmp->icon->core, newlevel);
3126 if (dock->lowered)
3127 wLowerFrame(tmp->icon->core);
3130 if (dock->type == WM_DOCK)
3131 wScreenUpdateUsableArea(dock->screen_ptr);
3134 static void toggleCollapsed(WDock * dock)
3136 if (dock->collapsed) {
3137 dock->collapsed = 0;
3138 wDockShowIcons(dock);
3139 } else {
3140 dock->collapsed = 1;
3141 wDockHideIcons(dock);
3145 static void openDockMenu(WDock * dock, WAppIcon * aicon, XEvent * event)
3147 WScreen *scr = dock->screen_ptr;
3148 WObjDescriptor *desc;
3149 WMenuEntry *entry;
3150 WApplication *wapp = NULL;
3151 int index = 0;
3152 int x_pos;
3153 int n_selected;
3154 int appIsRunning = aicon->running && aicon->icon && aicon->icon->owner;
3156 if (dock->type == WM_DOCK) {
3157 /* keep on top */
3158 entry = dock->menu->entries[index];
3159 entry->flags.indicator_on = !dock->lowered;
3160 entry->clientdata = dock;
3161 dock->menu->flags.realized = 0;
3162 } else {
3163 /* clip options */
3164 if (scr->clip_options)
3165 updateClipOptionsMenu(scr->clip_options, dock);
3167 n_selected = numberOfSelectedIcons(dock);
3169 /* Rename Workspace */
3170 entry = dock->menu->entries[++index];
3171 if (aicon == scr->clip_icon) {
3172 entry->callback = renameCallback;
3173 entry->clientdata = dock;
3174 entry->flags.indicator = 0;
3175 entry->text = _("Rename Workspace");
3176 } else {
3177 entry->callback = omnipresentCallback;
3178 entry->clientdata = aicon;
3179 if (n_selected > 0) {
3180 entry->flags.indicator = 0;
3181 entry->text = _("Toggle Omnipresent");
3182 } else {
3183 entry->flags.indicator = 1;
3184 entry->flags.indicator_on = aicon->omnipresent;
3185 entry->flags.indicator_type = MI_CHECK;
3186 entry->text = _("Omnipresent");
3190 /* select/unselect icon */
3191 entry = dock->menu->entries[++index];
3192 entry->clientdata = aicon;
3193 entry->flags.indicator_on = aicon->icon->selected;
3194 wMenuSetEnabled(dock->menu, index, aicon != scr->clip_icon);
3196 /* select/unselect all icons */
3197 entry = dock->menu->entries[++index];
3198 entry->clientdata = aicon;
3199 if (n_selected > 0)
3200 entry->text = _("Unselect All Icons");
3201 else
3202 entry->text = _("Select All Icons");
3203 wMenuSetEnabled(dock->menu, index, dock->icon_count > 1);
3205 /* keep icon(s) */
3206 entry = dock->menu->entries[++index];
3207 entry->clientdata = aicon;
3208 if (n_selected > 1)
3209 entry->text = _("Keep Icons");
3210 else
3211 entry->text = _("Keep Icon");
3212 wMenuSetEnabled(dock->menu, index, dock->icon_count > 1);
3214 /* this is the workspace submenu part */
3215 entry = dock->menu->entries[++index];
3216 if (n_selected > 1)
3217 entry->text = _("Move Icons To");
3218 else
3219 entry->text = _("Move Icon To");
3220 if (scr->clip_submenu)
3221 updateWorkspaceMenu(scr->clip_submenu, aicon);
3222 wMenuSetEnabled(dock->menu, index, !aicon->omnipresent);
3224 /* remove icon(s) */
3225 entry = dock->menu->entries[++index];
3226 entry->clientdata = aicon;
3227 if (n_selected > 1)
3228 entry->text = _("Remove Icons");
3229 else
3230 entry->text = _("Remove Icon");
3231 wMenuSetEnabled(dock->menu, index, dock->icon_count > 1);
3233 /* attract icon(s) */
3234 entry = dock->menu->entries[++index];
3235 entry->clientdata = aicon;
3237 dock->menu->flags.realized = 0;
3238 wMenuRealize(dock->menu);
3241 if (aicon->icon->owner) {
3242 wapp = wApplicationOf(aicon->icon->owner->main_window);
3243 } else {
3244 wapp = NULL;
3247 /* launch */
3248 entry = dock->menu->entries[++index];
3249 entry->clientdata = aicon;
3250 wMenuSetEnabled(dock->menu, index, aicon->command != NULL);
3252 /* unhide here */
3253 entry = dock->menu->entries[++index];
3254 entry->clientdata = aicon;
3255 if (wapp && wapp->flags.hidden) {
3256 entry->text = _("Unhide Here");
3257 } else {
3258 entry->text = _("Bring Here");
3260 wMenuSetEnabled(dock->menu, index, appIsRunning);
3262 /* hide */
3263 entry = dock->menu->entries[++index];
3264 entry->clientdata = aicon;
3265 if (wapp && wapp->flags.hidden) {
3266 entry->text = _("Unhide");
3267 } else {
3268 entry->text = _("Hide");
3270 wMenuSetEnabled(dock->menu, index, appIsRunning);
3272 /* settings */
3273 entry = dock->menu->entries[++index];
3274 entry->clientdata = aicon;
3275 wMenuSetEnabled(dock->menu, index, !aicon->editing && !wPreferences.flags.noupdates);
3277 /* kill */
3278 entry = dock->menu->entries[++index];
3279 entry->clientdata = aicon;
3280 wMenuSetEnabled(dock->menu, index, appIsRunning);
3282 if (!dock->menu->flags.realized)
3283 wMenuRealize(dock->menu);
3285 if (dock->type == WM_CLIP) {
3286 /*x_pos = event->xbutton.x_root+2; */
3287 x_pos = event->xbutton.x_root - dock->menu->frame->core->width / 2 - 1;
3288 if (x_pos < 0) {
3289 x_pos = 0;
3290 } else if (x_pos + dock->menu->frame->core->width > scr->scr_width - 2) {
3291 x_pos = scr->scr_width - dock->menu->frame->core->width - 4;
3293 } else {
3294 x_pos = dock->on_right_side ? scr->scr_width - dock->menu->frame->core->width - 3 : 0;
3297 wMenuMapAt(dock->menu, x_pos, event->xbutton.y_root + 2, False);
3299 /* allow drag select */
3300 event->xany.send_event = True;
3301 desc = &dock->menu->menu->descriptor;
3302 (*desc->handle_mousedown) (desc, event);
3305 /******************************************************************/
3306 static void iconDblClick(WObjDescriptor * desc, XEvent * event)
3308 WAppIcon *btn = desc->parent;
3309 WDock *dock = btn->dock;
3310 WApplication *wapp = NULL;
3311 int unhideHere = 0;
3313 if (btn->icon->owner && !(event->xbutton.state & ControlMask)) {
3314 wapp = wApplicationOf(btn->icon->owner->main_window);
3316 assert(wapp != NULL);
3318 unhideHere = (event->xbutton.state & ShiftMask);
3320 /* go to the last workspace that the user worked on the app */
3321 if (wapp->last_workspace != dock->screen_ptr->current_workspace && !unhideHere) {
3322 wWorkspaceChange(dock->screen_ptr, wapp->last_workspace);
3325 wUnhideApplication(wapp, event->xbutton.button == Button2, unhideHere);
3327 if (event->xbutton.state & MOD_MASK) {
3328 wHideOtherApplications(btn->icon->owner);
3330 } else {
3331 if (event->xbutton.button == Button1) {
3333 if (event->xbutton.state & MOD_MASK) {
3334 /* raise/lower dock */
3335 toggleLowered(dock);
3336 } else if (btn == dock->screen_ptr->clip_icon) {
3337 if (getClipButton(event->xbutton.x, event->xbutton.y) == CLIP_IDLE)
3338 toggleCollapsed(dock);
3339 else
3340 handleClipChangeWorkspace(dock->screen_ptr, event);
3341 } else if (btn->command) {
3342 if (!btn->launching && (!btn->running || (event->xbutton.state & ControlMask))) {
3343 launchDockedApplication(btn, False);
3345 } else if (btn->xindex == 0 && btn->yindex == 0 && btn->dock->type == WM_DOCK) {
3346 wShowGNUstepPanel(dock->screen_ptr);
3352 static void handleDockMove(WDock * dock, WAppIcon * aicon, XEvent * event)
3354 WScreen *scr = dock->screen_ptr;
3355 int ofs_x = event->xbutton.x, ofs_y = event->xbutton.y;
3356 int x, y;
3357 XEvent ev;
3358 int grabbed = 0, swapped = 0, done;
3359 Pixmap ghost = None;
3360 int superfluous = wPreferences.superfluous; /* we catch it to avoid problems */
3362 #ifdef DEBUG
3363 puts("moving dock");
3364 #endif
3365 if (XGrabPointer(dpy, aicon->icon->core->window, True, ButtonMotionMask
3366 | ButtonReleaseMask | ButtonPressMask, GrabModeAsync,
3367 GrabModeAsync, None, None, CurrentTime) != GrabSuccess) {
3368 wwarning("pointer grab failed for dock move");
3370 y = 0;
3371 for (x = 0; x < dock->max_icons; x++) {
3372 if (dock->icon_array[x] != NULL && dock->icon_array[x]->yindex > y)
3373 y = dock->icon_array[x]->yindex;
3375 y++;
3376 XResizeWindow(dpy, scr->dock_shadow, ICON_SIZE, ICON_SIZE * y);
3378 done = 0;
3379 while (!done) {
3380 WMMaskEvent(dpy, PointerMotionMask | ButtonReleaseMask | ButtonPressMask
3381 | ButtonMotionMask | ExposureMask | EnterWindowMask, &ev);
3382 switch (ev.type) {
3383 case Expose:
3384 WMHandleEvent(&ev);
3385 break;
3387 case EnterNotify:
3388 /* It means the cursor moved so fast that it entered
3389 * something else (if moving slowly, it would have
3390 * stayed in the dock that is being moved. Ignore such
3391 * "spurious" EnterNotifiy's */
3392 break;
3394 case MotionNotify:
3395 if (!grabbed) {
3396 if (abs(ofs_x - ev.xmotion.x) >= MOVE_THRESHOLD
3397 || abs(ofs_y - ev.xmotion.y) >= MOVE_THRESHOLD) {
3398 XChangeActivePointerGrab(dpy, ButtonMotionMask
3399 | ButtonReleaseMask | ButtonPressMask,
3400 wCursor[WCUR_MOVE], CurrentTime);
3401 grabbed = 1;
3403 break;
3405 if (dock->type == WM_CLIP) {
3406 x = ev.xmotion.x_root - ofs_x;
3407 y = ev.xmotion.y_root - ofs_y;
3408 wScreenKeepInside(scr, &x, &y, ICON_SIZE, ICON_SIZE);
3410 moveDock(dock, x, y);
3411 } else {
3412 /* move vertically if pointer is inside the dock */
3413 if ((dock->on_right_side && ev.xmotion.x_root >= dock->x_pos - ICON_SIZE)
3414 || (!dock->on_right_side && ev.xmotion.x_root <= dock->x_pos + ICON_SIZE * 2)) {
3416 x = ev.xmotion.x_root - ofs_x;
3417 y = ev.xmotion.y_root - ofs_y;
3418 wScreenKeepInside(scr, &x, &y, ICON_SIZE, ICON_SIZE);
3419 moveDock(dock, dock->x_pos, y);
3421 /* move horizontally to change sides */
3422 x = ev.xmotion.x_root - ofs_x;
3423 if (!dock->on_right_side) {
3425 /* is on left */
3427 if (ev.xmotion.x_root > dock->x_pos + ICON_SIZE * 2) {
3428 XMoveWindow(dpy, scr->dock_shadow, scr->scr_width - ICON_SIZE
3429 - DOCK_EXTRA_SPACE - 1, dock->y_pos);
3430 if (superfluous && ghost == None) {
3431 ghost = MakeGhostDock(dock, dock->x_pos,
3432 scr->scr_width - ICON_SIZE
3433 - DOCK_EXTRA_SPACE - 1, dock->y_pos);
3434 XSetWindowBackgroundPixmap(dpy, scr->dock_shadow, ghost);
3435 XClearWindow(dpy, scr->dock_shadow);
3437 XMapRaised(dpy, scr->dock_shadow);
3438 swapped = 1;
3439 } else {
3440 if (superfluous && ghost != None) {
3441 XFreePixmap(dpy, ghost);
3442 ghost = None;
3444 XUnmapWindow(dpy, scr->dock_shadow);
3445 swapped = 0;
3447 } else {
3448 /* is on right */
3449 if (ev.xmotion.x_root < dock->x_pos - ICON_SIZE) {
3450 XMoveWindow(dpy, scr->dock_shadow, DOCK_EXTRA_SPACE, dock->y_pos);
3451 if (superfluous && ghost == None) {
3452 ghost = MakeGhostDock(dock, dock->x_pos,
3453 DOCK_EXTRA_SPACE, dock->y_pos);
3454 XSetWindowBackgroundPixmap(dpy, scr->dock_shadow, ghost);
3455 XClearWindow(dpy, scr->dock_shadow);
3457 XMapRaised(dpy, scr->dock_shadow);
3458 swapped = -1;
3459 } else {
3460 XUnmapWindow(dpy, scr->dock_shadow);
3461 swapped = 0;
3462 if (superfluous && ghost != None) {
3463 XFreePixmap(dpy, ghost);
3464 ghost = None;
3469 break;
3471 case ButtonPress:
3472 break;
3474 case ButtonRelease:
3475 if (ev.xbutton.button != event->xbutton.button)
3476 break;
3477 XUngrabPointer(dpy, CurrentTime);
3478 XUnmapWindow(dpy, scr->dock_shadow);
3479 XResizeWindow(dpy, scr->dock_shadow, ICON_SIZE, ICON_SIZE);
3480 if (dock->type == WM_DOCK) {
3481 if (swapped != 0) {
3482 if (swapped > 0)
3483 dock->on_right_side = 1;
3484 else
3485 dock->on_right_side = 0;
3486 swapDock(dock);
3487 wArrangeIcons(scr, False);
3490 done = 1;
3491 break;
3494 if (superfluous) {
3495 if (ghost != None)
3496 XFreePixmap(dpy, ghost);
3497 XSetWindowBackground(dpy, scr->dock_shadow, scr->white_pixel);
3499 #ifdef DEBUG
3500 puts("End dock move");
3501 #endif
3504 static Bool handleIconMove(WDock *dock, WAppIcon *aicon, XEvent *event)
3506 WScreen *scr = dock->screen_ptr;
3507 Window wins[2];
3508 WIcon *icon = aicon->icon;
3509 WDock *dock2 = NULL, *last_dock = dock, *clip = NULL;
3510 int ondock, grabbed = 0, change_dock = 0, collapsed = 0;
3511 XEvent ev;
3512 int x = aicon->x_pos, y = aicon->y_pos;
3513 int ofs_x = event->xbutton.x, ofs_y = event->xbutton.y;
3514 int shad_x = x, shad_y = y;
3515 int ix = aicon->xindex, iy = aicon->yindex;
3516 int tmp;
3517 Pixmap ghost = None;
3518 Bool docked;
3519 int superfluous = wPreferences.superfluous; /* we catch it to avoid problems */
3520 int omnipresent = aicon->omnipresent; /* this must be cached!!! */
3521 Bool hasMoved = False;
3523 if (wPreferences.flags.noupdates)
3524 return;
3526 if (XGrabPointer(dpy, icon->core->window, True, ButtonMotionMask
3527 | ButtonReleaseMask | ButtonPressMask, GrabModeAsync,
3528 GrabModeAsync, None, None, CurrentTime) != GrabSuccess) {
3529 #ifdef DEBUG0
3530 wwarning("pointer grab failed for icon move");
3531 #endif
3534 if (!(event->xbutton.state & MOD_MASK))
3535 wRaiseFrame(icon->core);
3537 if (!wPreferences.flags.noclip)
3538 clip = scr->workspaces[scr->current_workspace]->clip;
3540 if (dock == scr->dock && !wPreferences.flags.noclip)
3541 dock2 = clip;
3542 else if (dock != scr->dock && !wPreferences.flags.nodock)
3543 dock2 = scr->dock;
3545 wins[0] = icon->core->window;
3546 wins[1] = scr->dock_shadow;
3547 XRestackWindows(dpy, wins, 2);
3548 XMoveResizeWindow(dpy, scr->dock_shadow, aicon->x_pos, aicon->y_pos, ICON_SIZE, ICON_SIZE);
3549 if (superfluous) {
3550 if (icon->pixmap != None)
3551 ghost = MakeGhostIcon(scr, icon->pixmap);
3552 else
3553 ghost = MakeGhostIcon(scr, icon->core->window);
3555 XSetWindowBackgroundPixmap(dpy, scr->dock_shadow, ghost);
3556 XClearWindow(dpy, scr->dock_shadow);
3558 XMapWindow(dpy, scr->dock_shadow);
3560 ondock = 1;
3562 while (1) {
3563 XMaskEvent(dpy, PointerMotionMask | ButtonReleaseMask | ButtonPressMask
3564 | ButtonMotionMask | ExposureMask, &ev);
3565 switch (ev.type) {
3566 case Expose:
3567 WMHandleEvent(&ev);
3568 break;
3570 case MotionNotify:
3571 hasMoved = True;
3572 if (!grabbed) {
3573 if (abs(ofs_x - ev.xmotion.x) >= MOVE_THRESHOLD
3574 || abs(ofs_y - ev.xmotion.y) >= MOVE_THRESHOLD) {
3575 XChangeActivePointerGrab(dpy, ButtonMotionMask
3576 | ButtonReleaseMask | ButtonPressMask,
3577 wCursor[WCUR_MOVE], CurrentTime);
3578 grabbed = 1;
3579 } else {
3580 break;
3584 if (omnipresent) {
3585 int i;
3586 for (i = 0; i < scr->workspace_count; i++) {
3587 if (i == scr->current_workspace)
3588 continue;
3589 wDockShowIcons(scr->workspaces[i]->clip);
3593 x = ev.xmotion.x_root - ofs_x;
3594 y = ev.xmotion.y_root - ofs_y;
3595 tmp = wDockSnapIcon(dock, aicon, x, y, &ix, &iy, True);
3596 if (tmp && dock2) {
3597 change_dock = 0;
3598 if (last_dock != dock && collapsed) {
3599 last_dock->collapsed = 1;
3600 wDockHideIcons(last_dock);
3601 collapsed = 0;
3603 if (!collapsed && (collapsed = dock->collapsed)) {
3604 dock->collapsed = 0;
3605 wDockShowIcons(dock);
3607 if (dock->auto_raise_lower)
3608 wDockRaise(dock);
3609 last_dock = dock;
3610 } else if (dock2) {
3611 tmp = wDockSnapIcon(dock2, aicon, x, y, &ix, &iy, False);
3612 if (tmp) {
3613 change_dock = 1;
3614 if (last_dock != dock2 && collapsed) {
3615 last_dock->collapsed = 1;
3616 wDockHideIcons(last_dock);
3617 collapsed = 0;
3619 if (!collapsed && (collapsed = dock2->collapsed)) {
3620 dock2->collapsed = 0;
3621 wDockShowIcons(dock2);
3623 if (dock2->auto_raise_lower)
3624 wDockRaise(dock2);
3625 last_dock = dock2;
3628 if (aicon->launching || aicon->lock || (aicon->running && !(ev.xmotion.state & MOD_MASK))
3629 || (!aicon->running && tmp)) {
3630 shad_x = last_dock->x_pos + ix * wPreferences.icon_size;
3631 shad_y = last_dock->y_pos + iy * wPreferences.icon_size;
3633 XMoveWindow(dpy, scr->dock_shadow, shad_x, shad_y);
3635 if (!ondock) {
3636 XMapWindow(dpy, scr->dock_shadow);
3638 ondock = 1;
3639 } else {
3640 if (ondock) {
3641 XUnmapWindow(dpy, scr->dock_shadow);
3643 ondock = 0;
3645 XMoveWindow(dpy, icon->core->window, x, y);
3646 break;
3648 case ButtonPress:
3649 break;
3651 case ButtonRelease:
3652 if (ev.xbutton.button != event->xbutton.button)
3653 break;
3654 XUngrabPointer(dpy, CurrentTime);
3655 if (ondock) {
3656 SlideWindow(icon->core->window, x, y, shad_x, shad_y);
3657 XUnmapWindow(dpy, scr->dock_shadow);
3658 if (!change_dock) {
3659 reattachIcon(dock, aicon, ix, iy);
3660 if (clip && dock != clip && clip->auto_raise_lower)
3661 wDockLower(clip);
3662 } else {
3663 docked = moveIconBetweenDocks(dock, dock2, aicon, ix, iy);
3664 if (!docked) {
3665 /* Slide it back if dock rejected it */
3666 SlideWindow(icon->core->window, x, y, aicon->x_pos, aicon->y_pos);
3667 reattachIcon(dock, aicon, aicon->xindex, aicon->yindex);
3669 if (last_dock->type == WM_CLIP && last_dock->auto_collapse) {
3670 collapsed = 0;
3673 } else {
3674 aicon->x_pos = x;
3675 aicon->y_pos = y;
3676 if (superfluous) {
3677 if (!aicon->running && !wPreferences.no_animations) {
3678 /* We need to deselect it, even if is deselected in
3679 * wDockDetach(), because else DoKaboom() will fail.
3681 if (aicon->icon->selected)
3682 wIconSelect(aicon->icon);
3684 wSoundPlay(WSOUND_KABOOM);
3685 DoKaboom(scr, aicon->icon->core->window, x, y);
3686 } else {
3687 wSoundPlay(WSOUND_UNDOCK);
3689 } else {
3690 wSoundPlay(WSOUND_UNDOCK);
3692 if (clip && clip->auto_raise_lower)
3693 wDockLower(clip);
3694 wDockDetach(dock, aicon);
3696 if (collapsed) {
3697 last_dock->collapsed = 1;
3698 wDockHideIcons(last_dock);
3699 collapsed = 0;
3701 if (superfluous) {
3702 if (ghost != None)
3703 XFreePixmap(dpy, ghost);
3704 XSetWindowBackground(dpy, scr->dock_shadow, scr->white_pixel);
3706 if (omnipresent) {
3707 int i;
3708 for (i = 0; i < scr->workspace_count; i++) {
3709 if (i == scr->current_workspace)
3710 continue;
3711 wDockHideIcons(scr->workspaces[i]->clip);
3714 #ifdef DEBUG
3715 puts("End icon move");
3716 #endif
3717 return hasMoved;;
3720 return False; /* never reached */
3723 static int getClipButton(int px, int py)
3725 int pt = (CLIP_BUTTON_SIZE + 2) * ICON_SIZE / 64;
3727 if (px < 0 || py < 0 || px >= ICON_SIZE || py >= ICON_SIZE)
3728 return CLIP_IDLE;
3730 if (py <= pt - ((int)ICON_SIZE - 1 - px))
3731 return CLIP_FORWARD;
3732 else if (px <= pt - ((int)ICON_SIZE - 1 - py))
3733 return CLIP_REWIND;
3735 return CLIP_IDLE;
3738 static void handleClipChangeWorkspace(WScreen * scr, XEvent * event)
3740 XEvent ev;
3741 int done, direction, new_ws;
3742 int new_dir;
3743 WDock *clip = scr->clip_icon->dock;
3745 direction = getClipButton(event->xbutton.x, event->xbutton.y);
3747 clip->lclip_button_pushed = direction == CLIP_REWIND;
3748 clip->rclip_button_pushed = direction == CLIP_FORWARD;
3750 wClipIconPaint(scr->clip_icon);
3751 done = 0;
3752 while (!done) {
3753 WMMaskEvent(dpy, ExposureMask | ButtonMotionMask | ButtonReleaseMask | ButtonPressMask, &ev);
3754 switch (ev.type) {
3755 case Expose:
3756 WMHandleEvent(&ev);
3757 break;
3759 case MotionNotify:
3760 new_dir = getClipButton(ev.xmotion.x, ev.xmotion.y);
3761 if (new_dir != direction) {
3762 direction = new_dir;
3763 clip->lclip_button_pushed = direction == CLIP_REWIND;
3764 clip->rclip_button_pushed = direction == CLIP_FORWARD;
3765 wClipIconPaint(scr->clip_icon);
3767 break;
3769 case ButtonPress:
3770 break;
3772 case ButtonRelease:
3773 if (ev.xbutton.button == event->xbutton.button)
3774 done = 1;
3778 clip->lclip_button_pushed = 0;
3779 clip->rclip_button_pushed = 0;
3781 new_ws = wPreferences.ws_advance || (event->xbutton.state & ControlMask);
3783 if (direction == CLIP_FORWARD) {
3784 if (scr->current_workspace < scr->workspace_count - 1)
3785 wWorkspaceChange(scr, scr->current_workspace + 1);
3786 else if (new_ws && scr->current_workspace < MAX_WORKSPACES - 1)
3787 wWorkspaceChange(scr, scr->current_workspace + 1);
3788 else if (wPreferences.ws_cycle)
3789 wWorkspaceChange(scr, 0);
3790 } else if (direction == CLIP_REWIND) {
3791 if (scr->current_workspace > 0)
3792 wWorkspaceChange(scr, scr->current_workspace - 1);
3793 else if (scr->current_workspace == 0 && wPreferences.ws_cycle)
3794 wWorkspaceChange(scr, scr->workspace_count - 1);
3797 wClipIconPaint(scr->clip_icon);
3800 static void iconMouseDown(WObjDescriptor * desc, XEvent * event)
3802 WAppIcon *aicon = desc->parent;
3803 WDock *dock = aicon->dock;
3804 WScreen *scr = aicon->icon->core->screen_ptr;
3806 if (aicon->editing || WCHECK_STATE(WSTATE_MODAL))
3807 return;
3809 scr->last_dock = dock;
3811 if (dock->menu->flags.mapped)
3812 wMenuUnmap(dock->menu);
3814 if (IsDoubleClick(scr, event)) {
3815 /* double-click was not in the main clip icon */
3816 if (dock->type != WM_CLIP || aicon->xindex != 0 || aicon->yindex != 0
3817 || getClipButton(event->xbutton.x, event->xbutton.y) == CLIP_IDLE) {
3818 iconDblClick(desc, event);
3819 return;
3823 if (dock->type == WM_CLIP && scr->flags.clip_balloon_mapped) {
3824 XUnmapWindow(dpy, scr->clip_balloon);
3825 scr->flags.clip_balloon_mapped = 0;
3827 #ifdef DEBUG
3828 puts("handling dock");
3829 #endif
3830 if (event->xbutton.button == Button1) {
3831 if (event->xbutton.state & MOD_MASK)
3832 wDockLower(dock);
3833 else
3834 wDockRaise(dock);
3836 if ((event->xbutton.state & ShiftMask) && aicon != scr->clip_icon && dock->type != WM_DOCK) {
3837 wIconSelect(aicon->icon);
3838 return;
3841 if (aicon->yindex == 0 && aicon->xindex == 0) {
3842 if (getClipButton(event->xbutton.x, event->xbutton.y) != CLIP_IDLE
3843 && dock->type == WM_CLIP)
3844 handleClipChangeWorkspace(scr, event);
3845 else
3846 handleDockMove(dock, aicon, event);
3847 } else {
3848 Bool hasMoved = handleIconMove(dock, aicon, event);
3849 if (wPreferences.single_click && !hasMoved)
3850 iconDblClick(desc, event);
3852 } else if (event->xbutton.button == Button2 && dock->type == WM_CLIP && aicon == scr->clip_icon) {
3853 if (!scr->clip_ws_menu) {
3854 scr->clip_ws_menu = wWorkspaceMenuMake(scr, False);
3856 if (scr->clip_ws_menu) {
3857 WMenu *wsMenu = scr->clip_ws_menu;
3858 int xpos;
3860 wWorkspaceMenuUpdate(scr, wsMenu);
3862 xpos = event->xbutton.x_root - wsMenu->frame->core->width / 2 - 1;
3863 if (xpos < 0) {
3864 xpos = 0;
3865 } else if (xpos + wsMenu->frame->core->width > scr->scr_width - 2) {
3866 xpos = scr->scr_width - wsMenu->frame->core->width - 4;
3868 wMenuMapAt(wsMenu, xpos, event->xbutton.y_root + 2, False);
3870 desc = &wsMenu->menu->descriptor;
3871 event->xany.send_event = True;
3872 (*desc->handle_mousedown) (desc, event);
3874 } else if (event->xbutton.button == Button2 && dock->type == WM_CLIP &&
3875 (event->xbutton.state & ShiftMask) && aicon != scr->clip_icon) {
3876 wClipMakeIconOmnipresent(aicon, !aicon->omnipresent);
3877 } else if (event->xbutton.button == Button3) {
3878 if (event->xbutton.send_event &&
3879 XGrabPointer(dpy, aicon->icon->core->window, True, ButtonMotionMask
3880 | ButtonReleaseMask | ButtonPressMask, GrabModeAsync,
3881 GrabModeAsync, None, None, CurrentTime) != GrabSuccess) {
3882 wwarning("pointer grab failed for dockicon menu");
3883 return;
3886 openDockMenu(dock, aicon, event);
3887 } else if (event->xbutton.button == Button2) {
3888 WAppIcon *btn = desc->parent;
3890 if (!btn->launching && (!btn->running || (event->xbutton.state & ControlMask))) {
3891 launchDockedApplication(btn, True);
3896 static void showClipBalloon(WDock * dock, int workspace)
3898 int w, h;
3899 int x, y;
3900 WScreen *scr = dock->screen_ptr;
3901 char *text;
3902 Window stack[2];
3904 scr->flags.clip_balloon_mapped = 1;
3905 XMapWindow(dpy, scr->clip_balloon);
3907 text = scr->workspaces[workspace]->name;
3909 w = WMWidthOfString(scr->clip_title_font, text, strlen(text));
3911 h = WMFontHeight(scr->clip_title_font);
3912 XResizeWindow(dpy, scr->clip_balloon, w, h);
3914 x = dock->x_pos + CLIP_BUTTON_SIZE * ICON_SIZE / 64;
3915 y = dock->y_pos + ICON_SIZE - WMFontHeight(scr->clip_title_font) - 3;
3917 if (x + w > scr->scr_width) {
3918 x = scr->scr_width - w;
3919 if (dock->y_pos + ICON_SIZE + h > scr->scr_height)
3920 y = dock->y_pos - h - 1;
3921 else
3922 y = dock->y_pos + ICON_SIZE;
3923 XRaiseWindow(dpy, scr->clip_balloon);
3924 } else {
3925 stack[0] = scr->clip_icon->icon->core->window;
3926 stack[1] = scr->clip_balloon;
3927 XRestackWindows(dpy, stack, 2);
3929 XMoveWindow(dpy, scr->clip_balloon, x, y);
3930 XClearWindow(dpy, scr->clip_balloon);
3931 WMDrawString(scr->wmscreen, scr->clip_balloon,
3932 scr->clip_title_color[CLIP_NORMAL], scr->clip_title_font, 0, 0, text, strlen(text));
3935 static void clipEnterNotify(WObjDescriptor * desc, XEvent * event)
3937 WAppIcon *btn = (WAppIcon *) desc->parent;
3938 WDock *dock;
3939 WScreen *scr;
3941 assert(event->type == EnterNotify);
3943 if (desc->parent_type != WCLASS_DOCK_ICON)
3944 return;
3946 scr = btn->icon->core->screen_ptr;
3947 if (!btn->omnipresent)
3948 dock = btn->dock;
3949 else
3950 dock = scr->workspaces[scr->current_workspace]->clip;
3952 if (!dock || dock->type != WM_CLIP)
3953 return;
3955 /* The auto raise/lower code */
3956 if (dock->auto_lower_magic) {
3957 WMDeleteTimerHandler(dock->auto_lower_magic);
3958 dock->auto_lower_magic = NULL;
3960 if (dock->auto_raise_lower && !dock->auto_raise_magic) {
3961 dock->auto_raise_magic = WMAddTimerHandler(AUTO_RAISE_DELAY, clipAutoRaise, (void *)dock);
3964 /* The auto expand/collapse code */
3965 if (dock->auto_collapse_magic) {
3966 WMDeleteTimerHandler(dock->auto_collapse_magic);
3967 dock->auto_collapse_magic = NULL;
3969 if (dock->auto_collapse && !dock->auto_expand_magic) {
3970 dock->auto_expand_magic = WMAddTimerHandler(AUTO_EXPAND_DELAY, clipAutoExpand, (void *)dock);
3973 if (btn->xindex == 0 && btn->yindex == 0)
3974 showClipBalloon(dock, dock->screen_ptr->current_workspace);
3975 else {
3976 if (dock->screen_ptr->flags.clip_balloon_mapped) {
3977 XUnmapWindow(dpy, dock->screen_ptr->clip_balloon);
3978 dock->screen_ptr->flags.clip_balloon_mapped = 0;
3983 static void clipLeave(WDock * dock)
3985 XEvent event;
3986 WObjDescriptor *desc = NULL;
3988 if (!dock || dock->type != WM_CLIP)
3989 return;
3991 if (XCheckTypedEvent(dpy, EnterNotify, &event) != False) {
3992 if (XFindContext(dpy, event.xcrossing.window, wWinContext,
3993 (XPointer *) & desc) != XCNOENT
3994 && desc && desc->parent_type == WCLASS_DOCK_ICON
3995 && ((WAppIcon *) desc->parent)->dock && ((WAppIcon *) desc->parent)->dock->type == WM_CLIP) {
3996 /* We didn't left the Clip yet */
3997 XPutBackEvent(dpy, &event);
3998 return;
4001 XPutBackEvent(dpy, &event);
4002 } else {
4003 /* We entered a withdrawn window, so we're still in Clip */
4004 return;
4007 if (dock->auto_raise_magic) {
4008 WMDeleteTimerHandler(dock->auto_raise_magic);
4009 dock->auto_raise_magic = NULL;
4011 if (dock->auto_raise_lower && !dock->auto_lower_magic) {
4012 dock->auto_lower_magic = WMAddTimerHandler(AUTO_LOWER_DELAY, clipAutoLower, (void *)dock);
4015 if (dock->auto_expand_magic) {
4016 WMDeleteTimerHandler(dock->auto_expand_magic);
4017 dock->auto_expand_magic = NULL;
4019 if (dock->auto_collapse && !dock->auto_collapse_magic) {
4020 dock->auto_collapse_magic = WMAddTimerHandler(AUTO_COLLAPSE_DELAY, clipAutoCollapse, (void *)dock);
4024 static void clipLeaveNotify(WObjDescriptor * desc, XEvent * event)
4026 WAppIcon *btn = (WAppIcon *) desc->parent;
4028 assert(event->type == LeaveNotify);
4030 if (desc->parent_type != WCLASS_DOCK_ICON)
4031 return;
4033 clipLeave(btn->dock);
4036 static void clipAutoCollapse(void *cdata)
4038 WDock *dock = (WDock *) cdata;
4040 if (dock->type != WM_CLIP)
4041 return;
4043 if (dock->auto_collapse) {
4044 dock->collapsed = 1;
4045 wDockHideIcons(dock);
4047 dock->auto_collapse_magic = NULL;
4050 static void clipAutoExpand(void *cdata)
4052 WDock *dock = (WDock *) cdata;
4054 if (dock->type != WM_CLIP)
4055 return;
4057 if (dock->auto_collapse) {
4058 dock->collapsed = 0;
4059 wDockShowIcons(dock);
4061 dock->auto_expand_magic = NULL;
4064 static void clipAutoLower(void *cdata)
4066 WDock *dock = (WDock *) cdata;
4068 if (dock->type != WM_CLIP)
4069 return;
4071 if (dock->auto_raise_lower)
4072 wDockLower(dock);
4074 dock->auto_lower_magic = NULL;
4077 static void clipAutoRaise(void *cdata)
4079 WDock *dock = (WDock *) cdata;
4081 if (dock->type != WM_CLIP)
4082 return;
4084 if (dock->auto_raise_lower)
4085 wDockRaise(dock);
4087 if (dock->screen_ptr->flags.clip_balloon_mapped) {
4088 showClipBalloon(dock, dock->screen_ptr->current_workspace);
4091 dock->auto_raise_magic = NULL;
4094 static Bool iconCanBeOmnipresent(WAppIcon * aicon)
4096 WScreen *scr = aicon->icon->core->screen_ptr;
4097 WDock *clip;
4098 WAppIcon *btn;
4099 int i, j;
4101 for (i = 0; i < scr->workspace_count; i++) {
4102 clip = scr->workspaces[i]->clip;
4104 if (clip == aicon->dock)
4105 continue;
4107 if (clip->icon_count + scr->global_icon_count >= clip->max_icons)
4108 return False; /* Clip is full in some workspace */
4110 for (j = 0; j < clip->max_icons; j++) {
4111 btn = clip->icon_array[j];
4112 if (btn && btn->xindex == aicon->xindex && btn->yindex == aicon->yindex)
4113 return False;
4117 return True;
4120 int wClipMakeIconOmnipresent(WAppIcon * aicon, int omnipresent)
4122 WScreen *scr = aicon->icon->core->screen_ptr;
4123 WAppIconChain *new_entry, *tmp, *tmp1;
4124 int status = WO_SUCCESS;
4126 if ((scr->dock && aicon->dock == scr->dock) || aicon == scr->clip_icon) {
4127 return WO_NOT_APPLICABLE;
4130 if (aicon->omnipresent == omnipresent)
4131 return WO_SUCCESS;
4133 if (omnipresent) {
4134 if (iconCanBeOmnipresent(aicon)) {
4135 aicon->omnipresent = 1;
4136 new_entry = wmalloc(sizeof(WAppIconChain));
4137 new_entry->aicon = aicon;
4138 new_entry->next = scr->global_icons;
4139 scr->global_icons = new_entry;
4140 scr->global_icon_count++;
4141 } else {
4142 aicon->omnipresent = 0;
4143 status = WO_FAILED;
4145 } else {
4146 aicon->omnipresent = 0;
4147 if (aicon == scr->global_icons->aicon) {
4148 tmp = scr->global_icons->next;
4149 wfree(scr->global_icons);
4150 scr->global_icons = tmp;
4151 scr->global_icon_count--;
4152 } else {
4153 tmp = scr->global_icons;
4154 while (tmp->next) {
4155 if (tmp->next->aicon == aicon) {
4156 tmp1 = tmp->next->next;
4157 wfree(tmp->next);
4158 tmp->next = tmp1;
4159 scr->global_icon_count--;
4160 break;
4162 tmp = tmp->next;
4167 wAppIconPaint(aicon);
4169 return status;