Remove HEARTBEAT #ifdef's
[wmaker-crm.git] / src / dock.c
blob46d45f4a3cc05b6b0e51882f2bd86c7d3123c627
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 "xinerama.h"
57 /**** Local variables ****/
58 #define CLIP_REWIND 1
59 #define CLIP_IDLE 0
60 #define CLIP_FORWARD 2
62 /**** Global variables ****/
64 /* in dockedapp.c */
65 extern void DestroyDockAppSettingsPanel();
67 extern void ShowDockAppSettingsPanel(WAppIcon * aicon);
69 extern XContext wWinContext;
71 extern Cursor wCursor[WCUR_LAST];
73 extern WPreferences wPreferences;
75 extern XContext wWinContext;
77 #define MOD_MASK wPreferences.modifier_mask
79 extern void appIconMouseDown(WObjDescriptor * desc, XEvent * event);
81 #define ICON_SIZE wPreferences.icon_size
83 /***** Local variables ****/
85 static WMPropList *dCommand = NULL;
86 static WMPropList *dPasteCommand = NULL;
87 #ifdef XDND /* XXX was OFFIX */
88 static WMPropList *dDropCommand = NULL;
89 #endif
90 static WMPropList *dAutoLaunch, *dLock;
91 static WMPropList *dName, *dForced, *dBuggyApplication, *dYes, *dNo;
92 static WMPropList *dHost, *dDock, *dClip;
93 static WMPropList *dAutoAttractIcons;
95 static WMPropList *dPosition, *dApplications, *dLowered, *dCollapsed;
97 static WMPropList *dAutoCollapse, *dAutoRaiseLower, *dOmnipresent;
99 static void dockIconPaint(WAppIcon * btn);
101 static void iconMouseDown(WObjDescriptor * desc, XEvent * event);
103 static pid_t execCommand(WAppIcon * btn, char *command, WSavedState * state);
105 static void trackDeadProcess(pid_t pid, unsigned char status, WDock * dock);
107 static int getClipButton(int px, int py);
109 static void toggleLowered(WDock * dock);
111 static void toggleCollapsed(WDock * dock);
113 static void clipIconExpose(WObjDescriptor * desc, XEvent * event);
115 static void clipLeave(WDock * dock);
117 static void handleClipChangeWorkspace(WScreen * scr, XEvent * event);
119 Bool moveIconBetweenDocks(WDock * src, WDock * dest, WAppIcon * icon, int x, int y);
121 static void clipEnterNotify(WObjDescriptor * desc, XEvent * event);
122 static void clipLeaveNotify(WObjDescriptor * desc, XEvent * event);
123 static void clipAutoCollapse(void *cdata);
124 static void clipAutoExpand(void *cdata);
125 static void launchDockedApplication(WAppIcon * btn, Bool withSelection);
127 static void clipAutoLower(void *cdata);
128 static void clipAutoRaise(void *cdata);
130 static void showClipBalloon(WDock * dock, int workspace);
132 static void make_keys()
134 if (dCommand != NULL)
135 return;
137 dCommand = WMRetainPropList(WMCreatePLString("Command"));
138 dPasteCommand = WMRetainPropList(WMCreatePLString("PasteCommand"));
139 #ifdef XDND
140 dDropCommand = WMRetainPropList(WMCreatePLString("DropCommand"));
141 #endif
142 dLock = WMRetainPropList(WMCreatePLString("Lock"));
143 dAutoLaunch = WMRetainPropList(WMCreatePLString("AutoLaunch"));
144 dName = WMRetainPropList(WMCreatePLString("Name"));
145 dForced = WMRetainPropList(WMCreatePLString("Forced"));
146 dBuggyApplication = WMRetainPropList(WMCreatePLString("BuggyApplication"));
147 dYes = WMRetainPropList(WMCreatePLString("Yes"));
148 dNo = WMRetainPropList(WMCreatePLString("No"));
149 dHost = WMRetainPropList(WMCreatePLString("Host"));
151 dPosition = WMCreatePLString("Position");
152 dApplications = WMCreatePLString("Applications");
153 dLowered = WMCreatePLString("Lowered");
154 dCollapsed = WMCreatePLString("Collapsed");
155 dAutoCollapse = WMCreatePLString("AutoCollapse");
156 dAutoRaiseLower = WMCreatePLString("AutoRaiseLower");
157 dAutoAttractIcons = WMCreatePLString("AutoAttractIcons");
159 dOmnipresent = WMCreatePLString("Omnipresent");
161 dDock = WMCreatePLString("Dock");
162 dClip = WMCreatePLString("Clip");
165 static void renameCallback(WMenu * menu, WMenuEntry * entry)
167 WDock *dock = entry->clientdata;
168 char buffer[128];
169 int wspace;
170 char *name;
172 assert(entry->clientdata != NULL);
174 wspace = dock->screen_ptr->current_workspace;
176 name = wstrdup(dock->screen_ptr->workspaces[wspace]->name);
178 snprintf(buffer, sizeof(buffer), _("Type the name for workspace %i:"), wspace + 1);
179 if (wInputDialog(dock->screen_ptr, _("Rename Workspace"), buffer, &name)) {
180 wWorkspaceRename(dock->screen_ptr, wspace, name);
182 if (name) {
183 wfree(name);
187 static void toggleLoweredCallback(WMenu * menu, WMenuEntry * entry)
189 assert(entry->clientdata != NULL);
191 toggleLowered(entry->clientdata);
193 entry->flags.indicator_on = !(((WDock *) entry->clientdata)->lowered);
195 wMenuPaint(menu);
198 static int matchWindow(void *item, void *cdata)
200 return (((WFakeGroupLeader *) item)->leader == (Window) cdata);
203 static void killCallback(WMenu * menu, WMenuEntry * entry)
205 WScreen *scr = menu->menu->screen_ptr;
206 WAppIcon *icon;
207 WFakeGroupLeader *fPtr;
208 char *buffer, *shortname, **argv;
209 char *basename(const char *shortname);
210 int argc;
212 if (!WCHECK_STATE(WSTATE_NORMAL))
213 return;
215 assert(entry->clientdata != NULL);
217 icon = (WAppIcon *) entry->clientdata;
219 icon->editing = 1;
221 WCHANGE_STATE(WSTATE_MODAL);
223 /* strip away dir names */
224 shortname = basename(icon->command);
225 /* separate out command options */
226 wtokensplit(shortname, &argv, &argc);
228 buffer = wstrconcat(argv[0],
229 _(" will be forcibly closed.\n"
230 "Any unsaved changes will be lost.\n" "Please confirm."));
232 if (icon->icon && icon->icon->owner) {
233 fPtr = icon->icon->owner->fake_group;
234 } else {
235 /* is this really necessary? can we kill a non-running dock icon? */
236 Window win = icon->main_window;
237 int index;
239 index = WMFindInArray(scr->fakeGroupLeaders, matchWindow, (void *)win);
240 if (index != WANotFound)
241 fPtr = WMGetFromArray(scr->fakeGroupLeaders, index);
242 else
243 fPtr = NULL;
246 if (wPreferences.dont_confirm_kill
247 || wMessageDialog(menu->frame->screen_ptr, _("Kill Application"),
248 buffer, _("Yes"), _("No"), NULL) == WAPRDefault) {
249 if (fPtr != NULL) {
250 WWindow *wwin, *twin;
252 wwin = scr->focused_window;
253 while (wwin) {
254 twin = wwin->prev;
255 if (wwin->fake_group == fPtr) {
256 wClientKill(wwin);
258 wwin = twin;
260 } else if (icon->icon && icon->icon->owner) {
261 wClientKill(icon->icon->owner);
265 wfree(buffer);
266 wtokenfree(argv, argc);
268 icon->editing = 0;
270 WCHANGE_STATE(WSTATE_NORMAL);
273 /* TODO: replace this function with a member of the dock struct */
274 static int numberOfSelectedIcons(WDock * dock)
276 WAppIcon *aicon;
277 int i, n;
279 n = 0;
280 for (i = 1; i < dock->max_icons; i++) {
281 aicon = dock->icon_array[i];
282 if (aicon && aicon->icon->selected) {
283 n++;
287 return n;
290 static WMArray *getSelected(WDock * dock)
292 WMArray *ret = WMCreateArray(8);
293 WAppIcon *btn;
294 int i;
296 for (i = 1; i < dock->max_icons; i++) {
297 btn = dock->icon_array[i];
298 if (btn && btn->icon->selected) {
299 WMAddToArray(ret, btn);
303 return ret;
306 static void paintClipButtons(WAppIcon * clipIcon, Bool lpushed, Bool rpushed)
308 Window win = clipIcon->icon->core->window;
309 WScreen *scr = clipIcon->icon->core->screen_ptr;
310 XPoint p[4];
311 int pt = CLIP_BUTTON_SIZE * ICON_SIZE / 64;
312 int tp = ICON_SIZE - pt;
313 int as = pt - 15; /* 15 = 5+5+5 */
314 GC gc = scr->draw_gc; /* maybe use WMColorGC() instead here? */
315 WMColor *color;
316 #ifdef GRADIENT_CLIP_ARROW
317 Bool collapsed = clipIcon->dock->collapsed;
318 #endif
320 /*if (!clipIcon->dock->collapsed)
321 color = scr->clip_title_color[CLIP_NORMAL];
322 else
323 color = scr->clip_title_color[CLIP_COLLAPSED]; */
324 color = scr->clip_title_color[CLIP_NORMAL];
326 XSetForeground(dpy, gc, WMColorPixel(color));
328 if (rpushed) {
329 p[0].x = tp + 1;
330 p[0].y = 1;
331 p[1].x = ICON_SIZE - 2;
332 p[1].y = 1;
333 p[2].x = ICON_SIZE - 2;
334 p[2].y = pt - 1;
335 } else if (lpushed) {
336 p[0].x = 1;
337 p[0].y = tp;
338 p[1].x = pt;
339 p[1].y = ICON_SIZE - 2;
340 p[2].x = 1;
341 p[2].y = ICON_SIZE - 2;
343 if (lpushed || rpushed) {
344 XSetForeground(dpy, scr->draw_gc, scr->white_pixel);
345 XFillPolygon(dpy, win, scr->draw_gc, p, 3, Convex, CoordModeOrigin);
346 XSetForeground(dpy, scr->draw_gc, scr->black_pixel);
348 #ifdef GRADIENT_CLIP_ARROW
349 if (!collapsed) {
350 XSetFillStyle(dpy, scr->copy_gc, FillTiled);
351 XSetTile(dpy, scr->copy_gc, scr->clip_arrow_gradient);
352 XSetClipMask(dpy, scr->copy_gc, None);
353 gc = scr->copy_gc;
355 #endif /* GRADIENT_CLIP_ARROW */
357 /* top right arrow */
358 p[0].x = p[3].x = ICON_SIZE - 5 - as;
359 p[0].y = p[3].y = 5;
360 p[1].x = ICON_SIZE - 6;
361 p[1].y = 5;
362 p[2].x = ICON_SIZE - 6;
363 p[2].y = 4 + as;
364 if (rpushed) {
365 XFillPolygon(dpy, win, scr->draw_gc, p, 3, Convex, CoordModeOrigin);
366 XDrawLines(dpy, win, scr->draw_gc, p, 4, CoordModeOrigin);
367 } else {
368 #ifdef GRADIENT_CLIP_ARROW
369 if (!collapsed)
370 XSetTSOrigin(dpy, gc, ICON_SIZE - 6 - as, 5);
371 #endif
372 XFillPolygon(dpy, win, gc, p, 3, Convex, CoordModeOrigin);
373 XDrawLines(dpy, win, gc, p, 4, CoordModeOrigin);
376 /* bottom left arrow */
377 p[0].x = p[3].x = 5;
378 p[0].y = p[3].y = ICON_SIZE - 5 - as;
379 p[1].x = 5;
380 p[1].y = ICON_SIZE - 6;
381 p[2].x = 4 + as;
382 p[2].y = ICON_SIZE - 6;
383 if (lpushed) {
384 XFillPolygon(dpy, win, scr->draw_gc, p, 3, Convex, CoordModeOrigin);
385 XDrawLines(dpy, win, scr->draw_gc, p, 4, CoordModeOrigin);
386 } else {
387 #ifdef GRADIENT_CLIP_ARROW
388 if (!collapsed)
389 XSetTSOrigin(dpy, gc, 5, ICON_SIZE - 6 - as);
390 #endif
391 XFillPolygon(dpy, win, gc, p, 3, Convex, CoordModeOrigin);
392 XDrawLines(dpy, win, gc, p, 4, CoordModeOrigin);
394 #ifdef GRADIENT_CLIP_ARROW
395 if (!collapsed)
396 XSetFillStyle(dpy, scr->copy_gc, FillSolid);
397 #endif
400 RImage *wClipMakeTile(WScreen * scr, RImage * normalTile)
402 RImage *tile = RCloneImage(normalTile);
403 RColor black;
404 RColor dark;
405 RColor light;
406 int pt, tp;
407 int as;
409 pt = CLIP_BUTTON_SIZE * wPreferences.icon_size / 64;
410 tp = wPreferences.icon_size - 1 - pt;
411 as = pt - 15;
413 black.alpha = 255;
414 black.red = black.green = black.blue = 0;
416 dark.alpha = 0;
417 dark.red = dark.green = dark.blue = 60;
419 light.alpha = 0;
420 light.red = light.green = light.blue = 80;
422 /* top right */
423 ROperateLine(tile, RSubtractOperation, tp, 0, wPreferences.icon_size - 2, pt - 1, &dark);
424 RDrawLine(tile, tp - 1, 0, wPreferences.icon_size - 1, pt + 1, &black);
425 ROperateLine(tile, RAddOperation, tp, 2, wPreferences.icon_size - 3, pt, &light);
427 /* arrow bevel */
428 ROperateLine(tile, RSubtractOperation, ICON_SIZE - 7 - as, 4, ICON_SIZE - 5, 4, &dark);
429 ROperateLine(tile, RSubtractOperation, ICON_SIZE - 6 - as, 5, ICON_SIZE - 5, 6 + as, &dark);
430 ROperateLine(tile, RAddOperation, ICON_SIZE - 5, 4, ICON_SIZE - 5, 6 + as, &light);
432 /* bottom left */
433 ROperateLine(tile, RAddOperation, 2, tp + 2, pt - 2, wPreferences.icon_size - 3, &dark);
434 RDrawLine(tile, 0, tp - 1, pt + 1, wPreferences.icon_size - 1, &black);
435 ROperateLine(tile, RSubtractOperation, 0, tp - 2, pt + 1, wPreferences.icon_size - 2, &light);
437 /* arrow bevel */
438 ROperateLine(tile, RSubtractOperation, 4, ICON_SIZE - 7 - as, 4, ICON_SIZE - 5, &dark);
439 ROperateLine(tile, RSubtractOperation, 5, ICON_SIZE - 6 - as, 6 + as, ICON_SIZE - 5, &dark);
440 ROperateLine(tile, RAddOperation, 4, ICON_SIZE - 5, 6 + as, ICON_SIZE - 5, &light);
442 return tile;
445 static void omnipresentCallback(WMenu * menu, WMenuEntry * entry)
447 WAppIcon *clickedIcon = entry->clientdata;
448 WAppIcon *aicon;
449 WDock *dock;
450 WMArray *selectedIcons;
451 WMArrayIterator iter;
452 int failed;
454 assert(entry->clientdata != NULL);
456 dock = clickedIcon->dock;
458 selectedIcons = getSelected(dock);
460 if (!WMGetArrayItemCount(selectedIcons))
461 WMAddToArray(selectedIcons, clickedIcon);
463 failed = 0;
464 WM_ITERATE_ARRAY(selectedIcons, aicon, iter) {
465 if (wClipMakeIconOmnipresent(aicon, !aicon->omnipresent) == WO_FAILED)
466 failed++;
467 else if (aicon->icon->selected)
468 wIconSelect(aicon->icon);
470 WMFreeArray(selectedIcons);
472 if (failed > 1) {
473 wMessageDialog(dock->screen_ptr, _("Warning"),
474 _("Some icons cannot be made omnipresent. "
475 "Please make sure that no other icon is "
476 "docked in the same positions on the other "
477 "workspaces and the Clip is not full in "
478 "some workspace."), _("OK"), NULL, NULL);
479 } else if (failed == 1) {
480 wMessageDialog(dock->screen_ptr, _("Warning"),
481 _("Icon cannot be made omnipresent. "
482 "Please make sure that no other icon is "
483 "docked in the same position on the other "
484 "workspaces and the Clip is not full in "
485 "some workspace."), _("OK"), NULL, NULL);
489 static void removeIconsCallback(WMenu * menu, WMenuEntry * entry)
491 WAppIcon *clickedIcon = (WAppIcon *) entry->clientdata;
492 WDock *dock;
493 WAppIcon *aicon;
494 WMArray *selectedIcons;
495 int keepit;
496 WMArrayIterator it;
498 assert(clickedIcon != NULL);
500 dock = clickedIcon->dock;
502 selectedIcons = getSelected(dock);
504 if (WMGetArrayItemCount(selectedIcons)) {
505 if (wMessageDialog(dock->screen_ptr, _("Workspace Clip"),
506 _("All selected icons will be removed!"),
507 _("OK"), _("Cancel"), NULL) != WAPRDefault) {
508 WMFreeArray(selectedIcons);
509 return;
511 } else {
512 if (clickedIcon->xindex == 0 && clickedIcon->yindex == 0) {
513 WMFreeArray(selectedIcons);
514 return;
516 WMAddToArray(selectedIcons, clickedIcon);
519 WM_ITERATE_ARRAY(selectedIcons, aicon, it) {
520 keepit = aicon->running && wApplicationOf(aicon->main_window);
521 wDockDetach(dock, aicon);
522 if (keepit) {
523 /* XXX: can: aicon->icon == NULL ? */
524 PlaceIcon(dock->screen_ptr, &aicon->x_pos, &aicon->y_pos,
525 wGetHeadForWindow(aicon->icon->owner));
526 XMoveWindow(dpy, aicon->icon->core->window, aicon->x_pos, aicon->y_pos);
527 if (!dock->mapped || dock->collapsed)
528 XMapWindow(dpy, aicon->icon->core->window);
531 WMFreeArray(selectedIcons);
533 if (wPreferences.auto_arrange_icons)
534 wArrangeIcons(dock->screen_ptr, True);
537 static void keepIconsCallback(WMenu * menu, WMenuEntry * entry)
539 WAppIcon *clickedIcon = (WAppIcon *) entry->clientdata;
540 WDock *dock;
541 WAppIcon *aicon;
542 WMArray *selectedIcons;
543 WMArrayIterator it;
545 assert(clickedIcon != NULL);
546 dock = clickedIcon->dock;
548 selectedIcons = getSelected(dock);
550 if (!WMGetArrayItemCount(selectedIcons)
551 && clickedIcon != dock->screen_ptr->clip_icon) {
552 char *command = NULL;
554 if (!clickedIcon->command && !clickedIcon->editing) {
555 clickedIcon->editing = 1;
556 if (wInputDialog(dock->screen_ptr, _("Keep Icon"),
557 _("Type the command used to launch the application"), &command)) {
558 if (command && (command[0] == 0 || (command[0] == '-' && command[1] == 0))) {
559 wfree(command);
560 command = NULL;
562 clickedIcon->command = command;
563 clickedIcon->editing = 0;
564 } else {
565 clickedIcon->editing = 0;
566 if (command)
567 wfree(command);
568 WMFreeArray(selectedIcons);
569 return;
573 WMAddToArray(selectedIcons, clickedIcon);
576 WM_ITERATE_ARRAY(selectedIcons, aicon, it) {
577 if (aicon->icon->selected)
578 wIconSelect(aicon->icon);
579 if (aicon && aicon->attracted && aicon->command) {
580 aicon->attracted = 0;
581 if (aicon->icon->shadowed) {
582 aicon->icon->shadowed = 0;
583 aicon->icon->force_paint = 1;
584 wAppIconPaint(aicon);
588 WMFreeArray(selectedIcons);
591 static void toggleAutoAttractCallback(WMenu * menu, WMenuEntry * entry)
593 WDock *dock = (WDock *) entry->clientdata;
595 assert(entry->clientdata != NULL);
597 dock->attract_icons = !dock->attract_icons;
598 /*if (!dock->attract_icons)
599 dock->keep_attracted = 0; */
601 entry->flags.indicator_on = dock->attract_icons;
603 wMenuPaint(menu);
606 static void selectCallback(WMenu * menu, WMenuEntry * entry)
608 WAppIcon *icon = (WAppIcon *) entry->clientdata;
610 assert(icon != NULL);
612 wIconSelect(icon->icon);
614 wMenuPaint(menu);
617 static void colectIconsCallback(WMenu * menu, WMenuEntry * entry)
619 WAppIcon *clickedIcon = (WAppIcon *) entry->clientdata;
620 WDock *clip;
621 WAppIcon *aicon;
622 int x, y, x_pos, y_pos;
624 assert(entry->clientdata != NULL);
625 clip = clickedIcon->dock;
627 aicon = clip->screen_ptr->app_icon_list;
629 while (aicon) {
630 if (!aicon->docked && wDockFindFreeSlot(clip, &x, &y)) {
631 x_pos = clip->x_pos + x * ICON_SIZE;
632 y_pos = clip->y_pos + y * ICON_SIZE;
633 if (aicon->x_pos != x_pos || aicon->y_pos != y_pos) {
634 #ifdef ANIMATIONS
635 if (wPreferences.no_animations) {
636 XMoveWindow(dpy, aicon->icon->core->window, x_pos, y_pos);
637 } else {
638 SlideWindow(aicon->icon->core->window,
639 aicon->x_pos, aicon->y_pos, x_pos, y_pos);
641 #else
642 XMoveWindow(dpy, aicon->icon->core->window, x_pos, y_pos);
643 #endif /* ANIMATIONS */
645 aicon->attracted = 1;
646 if (!aicon->icon->shadowed) {
647 aicon->icon->shadowed = 1;
648 aicon->icon->force_paint = 1;
649 /* We don't do an wAppIconPaint() here because it's in
650 * wDockAttachIcon(). -Dan
653 wDockAttachIcon(clip, aicon, x, y);
654 if (clip->collapsed || !clip->mapped)
655 XUnmapWindow(dpy, aicon->icon->core->window);
657 aicon = aicon->next;
661 static void selectIconsCallback(WMenu * menu, WMenuEntry * entry)
663 WAppIcon *clickedIcon = (WAppIcon *) entry->clientdata;
664 WDock *dock;
665 WMArray *selectedIcons;
666 WMArrayIterator iter;
667 WAppIcon *btn;
668 int i;
670 assert(clickedIcon != NULL);
671 dock = clickedIcon->dock;
673 selectedIcons = getSelected(dock);
675 if (!WMGetArrayItemCount(selectedIcons)) {
676 for (i = 1; i < dock->max_icons; i++) {
677 btn = dock->icon_array[i];
678 if (btn && !btn->icon->selected) {
679 wIconSelect(btn->icon);
682 } else {
683 WM_ITERATE_ARRAY(selectedIcons, btn, iter) {
684 wIconSelect(btn->icon);
687 WMFreeArray(selectedIcons);
689 wMenuPaint(menu);
692 static void toggleCollapsedCallback(WMenu * menu, WMenuEntry * entry)
694 assert(entry->clientdata != NULL);
696 toggleCollapsed(entry->clientdata);
698 entry->flags.indicator_on = ((WDock *) entry->clientdata)->collapsed;
700 wMenuPaint(menu);
703 static void toggleAutoCollapseCallback(WMenu * menu, WMenuEntry * entry)
705 WDock *dock;
706 assert(entry->clientdata != NULL);
708 dock = (WDock *) entry->clientdata;
710 dock->auto_collapse = !dock->auto_collapse;
712 entry->flags.indicator_on = ((WDock *) entry->clientdata)->auto_collapse;
714 wMenuPaint(menu);
717 static void toggleAutoRaiseLowerCallback(WMenu * menu, WMenuEntry * entry)
719 WDock *dock;
720 assert(entry->clientdata != NULL);
722 dock = (WDock *) entry->clientdata;
724 dock->auto_raise_lower = !dock->auto_raise_lower;
726 entry->flags.indicator_on = ((WDock *) entry->clientdata)->auto_raise_lower;
728 wMenuPaint(menu);
731 static void launchCallback(WMenu * menu, WMenuEntry * entry)
733 WAppIcon *btn = (WAppIcon *) entry->clientdata;
735 launchDockedApplication(btn, False);
738 static void settingsCallback(WMenu * menu, WMenuEntry * entry)
740 WAppIcon *btn = (WAppIcon *) entry->clientdata;
742 if (btn->editing)
743 return;
744 ShowDockAppSettingsPanel(btn);
747 static void hideCallback(WMenu * menu, WMenuEntry * entry)
749 WApplication *wapp;
750 WAppIcon *btn = (WAppIcon *) entry->clientdata;
752 wapp = wApplicationOf(btn->icon->owner->main_window);
754 if (wapp->flags.hidden) {
755 wWorkspaceChange(btn->icon->core->screen_ptr, wapp->last_workspace);
756 wUnhideApplication(wapp, False, False);
757 } else {
758 wHideApplication(wapp);
762 static void unhideHereCallback(WMenu * menu, WMenuEntry * entry)
764 WApplication *wapp;
765 WAppIcon *btn = (WAppIcon *) entry->clientdata;
767 wapp = wApplicationOf(btn->icon->owner->main_window);
769 wUnhideApplication(wapp, False, True);
772 WAppIcon *mainIconCreate(WScreen * scr, int type)
774 WAppIcon *btn;
775 int x_pos;
777 if (type == WM_CLIP) {
778 if (scr->clip_icon)
779 return scr->clip_icon;
780 btn = wAppIconCreateForDock(scr, NULL, "Logo", "WMClip", TILE_CLIP);
781 btn->icon->core->descriptor.handle_expose = clipIconExpose;
782 btn->icon->core->descriptor.handle_enternotify = clipEnterNotify;
783 btn->icon->core->descriptor.handle_leavenotify = clipLeaveNotify;
784 /*x_pos = scr->scr_width - ICON_SIZE*2 - DOCK_EXTRA_SPACE; */
785 x_pos = 0;
786 } else {
787 btn = wAppIconCreateForDock(scr, NULL, "Logo", "WMDock", TILE_NORMAL);
788 x_pos = scr->scr_width - ICON_SIZE - DOCK_EXTRA_SPACE;
791 btn->xindex = 0;
792 btn->yindex = 0;
794 btn->icon->core->descriptor.handle_mousedown = iconMouseDown;
795 btn->icon->core->descriptor.parent_type = WCLASS_DOCK_ICON;
796 btn->icon->core->descriptor.parent = btn;
797 /*ChangeStackingLevel(btn->icon->core, WMDockLevel); */
798 XMapWindow(dpy, btn->icon->core->window);
799 btn->x_pos = x_pos;
800 btn->y_pos = 0;
801 btn->docked = 1;
802 if (type == WM_CLIP)
803 scr->clip_icon = btn;
805 return btn;
808 static void switchWSCommand(WMenu * menu, WMenuEntry * entry)
810 WAppIcon *btn, *icon = (WAppIcon *) entry->clientdata;
811 WScreen *scr = icon->icon->core->screen_ptr;
812 WDock *src, *dest;
813 WMArray *selectedIcons;
814 int x, y;
816 if (entry->order == scr->current_workspace)
817 return;
818 src = icon->dock;
819 dest = scr->workspaces[entry->order]->clip;
821 selectedIcons = getSelected(src);
823 if (WMGetArrayItemCount(selectedIcons)) {
824 WMArrayIterator iter;
826 WM_ITERATE_ARRAY(selectedIcons, btn, iter) {
827 if (wDockFindFreeSlot(dest, &x, &y)) {
828 moveIconBetweenDocks(src, dest, btn, x, y);
829 XUnmapWindow(dpy, btn->icon->core->window);
832 } else if (icon != scr->clip_icon) {
833 if (wDockFindFreeSlot(dest, &x, &y)) {
834 moveIconBetweenDocks(src, dest, icon, x, y);
835 XUnmapWindow(dpy, icon->icon->core->window);
838 WMFreeArray(selectedIcons);
841 static void launchDockedApplication(WAppIcon * btn, Bool withSelection)
843 WScreen *scr = btn->icon->core->screen_ptr;
845 if (!btn->launching &&
846 ((!withSelection && btn->command != NULL) || (withSelection && btn->paste_command != NULL))) {
847 if (!btn->forced_dock) {
848 btn->relaunching = btn->running;
849 btn->running = 1;
851 if (btn->wm_instance || btn->wm_class) {
852 WWindowAttributes attr;
853 memset(&attr, 0, sizeof(WWindowAttributes));
854 wDefaultFillAttributes(scr, btn->wm_instance, btn->wm_class, &attr, NULL, True);
856 if (!attr.no_appicon && !btn->buggy_app)
857 btn->launching = 1;
858 else
859 btn->running = 0;
861 btn->drop_launch = 0;
862 btn->paste_launch = withSelection;
863 scr->last_dock = btn->dock;
864 btn->pid = execCommand(btn, (withSelection ? btn->paste_command : btn->command), NULL);
865 if (btn->pid > 0) {
866 if (btn->buggy_app) {
867 /* give feedback that the app was launched */
868 btn->launching = 1;
869 dockIconPaint(btn);
870 btn->launching = 0;
871 WMAddTimerHandler(200, (WMCallback *) dockIconPaint, btn);
872 } else {
873 dockIconPaint(btn);
875 } else {
876 wwarning(_("could not launch application %s\n"), btn->command);
877 btn->launching = 0;
878 if (!btn->relaunching) {
879 btn->running = 0;
885 static void updateWorkspaceMenu(WMenu * menu, WAppIcon * icon)
887 WScreen *scr = menu->frame->screen_ptr;
888 char title[MAX_WORKSPACENAME_WIDTH + 1];
889 int i;
891 if (!menu || !icon)
892 return;
894 for (i = 0; i < scr->workspace_count; i++) {
895 if (i < menu->entry_no) {
896 if (strcmp(menu->entries[i]->text, scr->workspaces[i]->name) != 0) {
897 wfree(menu->entries[i]->text);
898 strcpy(title, scr->workspaces[i]->name);
899 menu->entries[i]->text = wstrdup(title);
900 menu->flags.realized = 0;
902 menu->entries[i]->clientdata = (void *)icon;
903 } else {
904 strcpy(title, scr->workspaces[i]->name);
906 wMenuAddCallback(menu, title, switchWSCommand, (void *)icon);
908 menu->flags.realized = 0;
910 if (i == scr->current_workspace) {
911 wMenuSetEnabled(menu, i, False);
912 } else {
913 wMenuSetEnabled(menu, i, True);
917 if (!menu->flags.realized)
918 wMenuRealize(menu);
921 static WMenu *makeWorkspaceMenu(WScreen * scr)
923 WMenu *menu;
925 menu = wMenuCreate(scr, NULL, False);
926 if (!menu)
927 wwarning(_("could not create workspace submenu for Clip menu"));
929 wMenuAddCallback(menu, "", switchWSCommand, (void *)scr->clip_icon);
931 menu->flags.realized = 0;
932 wMenuRealize(menu);
934 return menu;
937 static void updateClipOptionsMenu(WMenu * menu, WDock * dock)
939 WMenuEntry *entry;
940 int index = 0;
942 if (!menu || !dock)
943 return;
945 /* keep on top */
946 entry = menu->entries[index];
947 entry->flags.indicator_on = !dock->lowered;
948 entry->clientdata = dock;
950 /* collapsed */
951 entry = menu->entries[++index];
952 entry->flags.indicator_on = dock->collapsed;
953 entry->clientdata = dock;
955 /* auto-collapse */
956 entry = menu->entries[++index];
957 entry->flags.indicator_on = dock->auto_collapse;
958 entry->clientdata = dock;
960 /* auto-raise/lower */
961 entry = menu->entries[++index];
962 entry->flags.indicator_on = dock->auto_raise_lower;
963 entry->clientdata = dock;
964 wMenuSetEnabled(menu, index, dock->lowered);
966 /* attract icons */
967 entry = menu->entries[++index];
968 entry->flags.indicator_on = dock->attract_icons;
969 entry->clientdata = dock;
971 menu->flags.realized = 0;
972 wMenuRealize(menu);
975 static WMenu *makeClipOptionsMenu(WScreen * scr)
977 WMenu *menu;
978 WMenuEntry *entry;
980 menu = wMenuCreate(scr, NULL, False);
981 if (!menu) {
982 wwarning(_("could not create options submenu for Clip menu"));
983 return NULL;
986 entry = wMenuAddCallback(menu, _("Keep on Top"), toggleLoweredCallback, NULL);
987 entry->flags.indicator = 1;
988 entry->flags.indicator_on = 1;
989 entry->flags.indicator_type = MI_CHECK;
991 entry = wMenuAddCallback(menu, _("Collapsed"), toggleCollapsedCallback, NULL);
992 entry->flags.indicator = 1;
993 entry->flags.indicator_on = 1;
994 entry->flags.indicator_type = MI_CHECK;
996 entry = wMenuAddCallback(menu, _("Autocollapse"), toggleAutoCollapseCallback, NULL);
997 entry->flags.indicator = 1;
998 entry->flags.indicator_on = 1;
999 entry->flags.indicator_type = MI_CHECK;
1001 entry = wMenuAddCallback(menu, _("Autoraise"), toggleAutoRaiseLowerCallback, NULL);
1002 entry->flags.indicator = 1;
1003 entry->flags.indicator_on = 1;
1004 entry->flags.indicator_type = MI_CHECK;
1006 entry = wMenuAddCallback(menu, _("Autoattract Icons"), toggleAutoAttractCallback, NULL);
1007 entry->flags.indicator = 1;
1008 entry->flags.indicator_on = 1;
1009 entry->flags.indicator_type = MI_CHECK;
1011 menu->flags.realized = 0;
1012 wMenuRealize(menu);
1014 return menu;
1017 static WMenu *dockMenuCreate(WScreen * scr, int type)
1019 WMenu *menu;
1020 WMenuEntry *entry;
1022 if (type == WM_CLIP && scr->clip_menu)
1023 return scr->clip_menu;
1025 menu = wMenuCreate(scr, NULL, False);
1026 if (type != WM_CLIP) {
1027 entry = wMenuAddCallback(menu, _("Keep on Top"), toggleLoweredCallback, NULL);
1028 entry->flags.indicator = 1;
1029 entry->flags.indicator_on = 1;
1030 entry->flags.indicator_type = MI_CHECK;
1031 } else {
1032 entry = wMenuAddCallback(menu, _("Clip Options"), NULL, NULL);
1033 scr->clip_options = makeClipOptionsMenu(scr);
1034 if (scr->clip_options)
1035 wMenuEntrySetCascade(menu, entry, scr->clip_options);
1037 entry = wMenuAddCallback(menu, _("Rename Workspace"), renameCallback, NULL);
1038 wfree(entry->text);
1039 entry->text = _("Rename Workspace");
1041 entry = wMenuAddCallback(menu, _("Selected"), selectCallback, NULL);
1042 entry->flags.indicator = 1;
1043 entry->flags.indicator_on = 1;
1044 entry->flags.indicator_type = MI_CHECK;
1046 entry = wMenuAddCallback(menu, _("Select All Icons"), selectIconsCallback, NULL);
1047 wfree(entry->text);
1048 entry->text = _("Select All Icons");
1050 entry = wMenuAddCallback(menu, _("Keep Icon"), keepIconsCallback, NULL);
1051 wfree(entry->text);
1052 entry->text = _("Keep Icon");
1054 entry = wMenuAddCallback(menu, _("Move Icon To"), NULL, NULL);
1055 wfree(entry->text);
1056 entry->text = _("Move Icon To");
1057 scr->clip_submenu = makeWorkspaceMenu(scr);
1058 if (scr->clip_submenu)
1059 wMenuEntrySetCascade(menu, entry, scr->clip_submenu);
1061 entry = wMenuAddCallback(menu, _("Remove Icon"), removeIconsCallback, NULL);
1062 wfree(entry->text);
1063 entry->text = _("Remove Icon");
1065 wMenuAddCallback(menu, _("Attract Icons"), colectIconsCallback, NULL);
1068 wMenuAddCallback(menu, _("Launch"), launchCallback, NULL);
1070 wMenuAddCallback(menu, _("Unhide Here"), unhideHereCallback, NULL);
1072 entry = wMenuAddCallback(menu, _("Hide"), hideCallback, NULL);
1073 wfree(entry->text);
1074 entry->text = _("Hide");
1076 wMenuAddCallback(menu, _("Settings..."), settingsCallback, NULL);
1078 wMenuAddCallback(menu, _("Kill"), killCallback, NULL);
1080 if (type == WM_CLIP)
1081 scr->clip_menu = menu;
1083 return menu;
1086 WDock *wDockCreate(WScreen * scr, int type)
1088 WDock *dock;
1089 WAppIcon *btn;
1090 int icon_count;
1092 make_keys();
1094 dock = wmalloc(sizeof(WDock));
1095 memset(dock, 0, sizeof(WDock));
1097 if (type == WM_CLIP)
1098 icon_count = CLIP_MAX_ICONS;
1099 else
1100 icon_count = scr->scr_height / wPreferences.icon_size;
1102 dock->icon_array = wmalloc(sizeof(WAppIcon *) * icon_count);
1103 memset(dock->icon_array, 0, sizeof(WAppIcon *) * icon_count);
1105 dock->max_icons = icon_count;
1107 btn = mainIconCreate(scr, type);
1109 btn->dock = dock;
1111 dock->x_pos = btn->x_pos;
1112 dock->y_pos = btn->y_pos;
1113 dock->screen_ptr = scr;
1114 dock->type = type;
1115 dock->icon_count = 1;
1116 dock->on_right_side = 1;
1117 dock->collapsed = 0;
1118 dock->auto_collapse = 0;
1119 dock->auto_collapse_magic = NULL;
1120 dock->auto_raise_lower = 0;
1121 dock->auto_lower_magic = NULL;
1122 dock->auto_raise_magic = NULL;
1123 dock->attract_icons = 0;
1124 dock->lowered = 1;
1125 dock->icon_array[0] = btn;
1126 wRaiseFrame(btn->icon->core);
1127 XMoveWindow(dpy, btn->icon->core->window, btn->x_pos, btn->y_pos);
1129 /* create dock menu */
1130 dock->menu = dockMenuCreate(scr, type);
1132 return dock;
1135 void wDockDestroy(WDock * dock)
1137 int i;
1138 WAppIcon *aicon;
1140 for (i = (dock->type == WM_CLIP) ? 1 : 0; i < dock->max_icons; i++) {
1141 aicon = dock->icon_array[i];
1142 if (aicon) {
1143 int keepit = aicon->running && wApplicationOf(aicon->main_window);
1144 wDockDetach(dock, aicon);
1145 if (keepit) {
1146 /* XXX: can: aicon->icon == NULL ? */
1147 PlaceIcon(dock->screen_ptr, &aicon->x_pos, &aicon->y_pos,
1148 wGetHeadForWindow(aicon->icon->owner));
1149 XMoveWindow(dpy, aicon->icon->core->window, aicon->x_pos, aicon->y_pos);
1150 if (!dock->mapped || dock->collapsed)
1151 XMapWindow(dpy, aicon->icon->core->window);
1155 if (wPreferences.auto_arrange_icons)
1156 wArrangeIcons(dock->screen_ptr, True);
1157 wfree(dock->icon_array);
1158 if (dock->menu && dock->type != WM_CLIP)
1159 wMenuDestroy(dock->menu, True);
1160 if (dock->screen_ptr->last_dock == dock)
1161 dock->screen_ptr->last_dock = NULL;
1162 wfree(dock);
1165 void wClipIconPaint(WAppIcon * aicon)
1167 WScreen *scr = aicon->icon->core->screen_ptr;
1168 WWorkspace *workspace = scr->workspaces[scr->current_workspace];
1169 WMColor *color;
1170 Window win = aicon->icon->core->window;
1171 int length, nlength;
1172 char *ws_name, ws_number[10];
1173 int ty, tx;
1175 wIconPaint(aicon->icon);
1177 length = strlen(workspace->name);
1178 ws_name = wmalloc(length + 1);
1179 snprintf(ws_name, length + 1, "%s", workspace->name);
1180 snprintf(ws_number, sizeof(ws_number), "%i", scr->current_workspace + 1);
1181 nlength = strlen(ws_number);
1183 if (!workspace->clip->collapsed)
1184 color = scr->clip_title_color[CLIP_NORMAL];
1185 else
1186 color = scr->clip_title_color[CLIP_COLLAPSED];
1188 ty = ICON_SIZE - WMFontHeight(scr->clip_title_font) - 3;
1190 tx = CLIP_BUTTON_SIZE * ICON_SIZE / 64;
1192 WMDrawString(scr->wmscreen, win, color, scr->clip_title_font, tx, ty, ws_name, length);
1193 /*WMDrawString(scr->wmscreen, win, color, scr->clip_title_font, 4,
1194 2, ws_name, length); */
1196 tx = (ICON_SIZE / 2 - WMWidthOfString(scr->clip_title_font, ws_number, nlength)) / 2;
1198 WMDrawString(scr->wmscreen, win, color, scr->clip_title_font, tx, 2, ws_number, nlength);
1200 wfree(ws_name);
1202 if (aicon->launching) {
1203 XFillRectangle(dpy, aicon->icon->core->window, scr->stipple_gc,
1204 0, 0, wPreferences.icon_size, wPreferences.icon_size);
1206 paintClipButtons(aicon, aicon->dock->lclip_button_pushed, aicon->dock->rclip_button_pushed);
1209 static void clipIconExpose(WObjDescriptor * desc, XEvent * event)
1211 wClipIconPaint(desc->parent);
1214 static void dockIconPaint(WAppIcon * btn)
1216 if (btn == btn->icon->core->screen_ptr->clip_icon)
1217 wClipIconPaint(btn);
1218 else
1219 wAppIconPaint(btn);
1222 static WMPropList *make_icon_state(WAppIcon * btn)
1224 WMPropList *node = NULL;
1225 WMPropList *command, *autolaunch, *lock, *name, *forced, *host;
1226 WMPropList *position, *buggy, *omnipresent;
1227 char *tmp;
1228 char buffer[64];
1230 if (btn) {
1231 if (!btn->command)
1232 command = WMCreatePLString("-");
1233 else
1234 command = WMCreatePLString(btn->command);
1236 autolaunch = btn->auto_launch ? dYes : dNo;
1238 lock = btn->lock ? dYes : dNo;
1240 tmp = EscapeWM_CLASS(btn->wm_instance, btn->wm_class);
1242 name = WMCreatePLString(tmp);
1244 wfree(tmp);
1246 forced = btn->forced_dock ? dYes : dNo;
1248 buggy = btn->buggy_app ? dYes : dNo;
1250 if (btn == btn->icon->core->screen_ptr->clip_icon)
1251 snprintf(buffer, sizeof(buffer), "%i,%i", btn->x_pos, btn->y_pos);
1252 else
1253 snprintf(buffer, sizeof(buffer), "%hi,%hi", btn->xindex, btn->yindex);
1254 position = WMCreatePLString(buffer);
1256 node = WMCreatePLDictionary(dCommand, command,
1257 dName, name,
1258 dAutoLaunch, autolaunch,
1259 dLock, lock,
1260 dForced, forced, dBuggyApplication, buggy, dPosition, position, NULL);
1261 WMReleasePropList(command);
1262 WMReleasePropList(name);
1263 WMReleasePropList(position);
1265 omnipresent = btn->omnipresent ? dYes : dNo;
1266 if (btn->dock != btn->icon->core->screen_ptr->dock && (btn->xindex != 0 || btn->yindex != 0))
1267 WMPutInPLDictionary(node, dOmnipresent, omnipresent);
1269 #ifdef XDND /* was OFFIX */
1270 if (btn->dnd_command) {
1271 command = WMCreatePLString(btn->dnd_command);
1272 WMPutInPLDictionary(node, dDropCommand, command);
1273 WMReleasePropList(command);
1275 #endif /* XDND */
1277 if (btn->paste_command) {
1278 command = WMCreatePLString(btn->paste_command);
1279 WMPutInPLDictionary(node, dPasteCommand, command);
1280 WMReleasePropList(command);
1283 if (btn->client_machine && btn->remote_start) {
1284 host = WMCreatePLString(btn->client_machine);
1285 WMPutInPLDictionary(node, dHost, host);
1286 WMReleasePropList(host);
1290 return node;
1293 static WMPropList *dockSaveState(WDock * dock)
1295 int i;
1296 WMPropList *icon_info;
1297 WMPropList *list = NULL, *dock_state = NULL;
1298 WMPropList *value, *key;
1299 char buffer[256];
1301 list = WMCreatePLArray(NULL);
1303 for (i = (dock->type == WM_DOCK ? 0 : 1); i < dock->max_icons; i++) {
1304 WAppIcon *btn = dock->icon_array[i];
1306 if (!btn || btn->attracted)
1307 continue;
1309 if ((icon_info = make_icon_state(dock->icon_array[i]))) {
1310 WMAddToPLArray(list, icon_info);
1311 WMReleasePropList(icon_info);
1315 dock_state = WMCreatePLDictionary(dApplications, list, NULL);
1317 if (dock->type == WM_DOCK) {
1318 snprintf(buffer, sizeof(buffer), "Applications%i", dock->screen_ptr->scr_height);
1319 key = WMCreatePLString(buffer);
1320 WMPutInPLDictionary(dock_state, key, list);
1321 WMReleasePropList(key);
1323 snprintf(buffer, sizeof(buffer), "%i,%i", (dock->on_right_side ? -ICON_SIZE : 0), dock->y_pos);
1324 value = WMCreatePLString(buffer);
1325 WMPutInPLDictionary(dock_state, dPosition, value);
1326 WMReleasePropList(value);
1328 WMReleasePropList(list);
1330 value = (dock->lowered ? dYes : dNo);
1331 WMPutInPLDictionary(dock_state, dLowered, value);
1333 if (dock->type == WM_CLIP) {
1334 value = (dock->collapsed ? dYes : dNo);
1335 WMPutInPLDictionary(dock_state, dCollapsed, value);
1337 value = (dock->auto_collapse ? dYes : dNo);
1338 WMPutInPLDictionary(dock_state, dAutoCollapse, value);
1340 value = (dock->auto_raise_lower ? dYes : dNo);
1341 WMPutInPLDictionary(dock_state, dAutoRaiseLower, value);
1343 value = (dock->attract_icons ? dYes : dNo);
1344 WMPutInPLDictionary(dock_state, dAutoAttractIcons, value);
1347 return dock_state;
1350 void wDockSaveState(WScreen * scr, WMPropList * old_state)
1352 WMPropList *dock_state;
1353 WMPropList *keys;
1355 dock_state = dockSaveState(scr->dock);
1358 * Copy saved states of docks with different sizes.
1360 if (old_state) {
1361 int i;
1362 WMPropList *tmp;
1364 keys = WMGetPLDictionaryKeys(old_state);
1365 for (i = 0; i < WMGetPropListItemCount(keys); i++) {
1366 tmp = WMGetFromPLArray(keys, i);
1368 if (strncasecmp(WMGetFromPLString(tmp), "applications", 12) == 0
1369 && !WMGetFromPLDictionary(dock_state, tmp)) {
1371 WMPutInPLDictionary(dock_state, tmp, WMGetFromPLDictionary(old_state, tmp));
1374 WMReleasePropList(keys);
1377 WMPutInPLDictionary(scr->session_state, dDock, dock_state);
1379 WMReleasePropList(dock_state);
1382 void wClipSaveState(WScreen * scr)
1384 WMPropList *clip_state;
1386 clip_state = make_icon_state(scr->clip_icon);
1388 WMPutInPLDictionary(scr->session_state, dClip, clip_state);
1390 WMReleasePropList(clip_state);
1393 WMPropList *wClipSaveWorkspaceState(WScreen * scr, int workspace)
1395 return dockSaveState(scr->workspaces[workspace]->clip);
1398 static Bool getBooleanDockValue(WMPropList * value, WMPropList * key)
1400 if (value) {
1401 if (WMIsPLString(value)) {
1402 if (strcasecmp(WMGetFromPLString(value), "YES") == 0)
1403 return True;
1404 } else {
1405 wwarning(_("bad value in docked icon state info %s"), WMGetFromPLString(key));
1408 return False;
1411 static WAppIcon *restore_icon_state(WScreen * scr, WMPropList * info, int type, int index)
1413 WAppIcon *aicon;
1414 WMPropList *cmd, *value;
1416 cmd = WMGetFromPLDictionary(info, dCommand);
1417 if (!cmd || !WMIsPLString(cmd)) {
1418 return NULL;
1421 /* parse window name */
1422 value = WMGetFromPLDictionary(info, dName);
1423 if (!value)
1424 return NULL;
1427 char *wclass, *winstance;
1428 char *command;
1430 ParseWindowName(value, &winstance, &wclass, "dock");
1432 if (!winstance && !wclass) {
1433 return NULL;
1436 /* get commands */
1438 if (cmd)
1439 command = wstrdup(WMGetFromPLString(cmd));
1440 else
1441 command = NULL;
1443 if (!command || strcmp(command, "-") == 0) {
1444 if (command)
1445 wfree(command);
1446 if (wclass)
1447 wfree(wclass);
1448 if (winstance)
1449 wfree(winstance);
1451 return NULL;
1454 aicon = wAppIconCreateForDock(scr, command, winstance, wclass, TILE_NORMAL);
1455 if (wclass)
1456 wfree(wclass);
1457 if (winstance)
1458 wfree(winstance);
1459 if (command)
1460 wfree(command);
1463 aicon->icon->core->descriptor.handle_mousedown = iconMouseDown;
1464 if (type == WM_CLIP) {
1465 aicon->icon->core->descriptor.handle_enternotify = clipEnterNotify;
1466 aicon->icon->core->descriptor.handle_leavenotify = clipLeaveNotify;
1468 aicon->icon->core->descriptor.parent_type = WCLASS_DOCK_ICON;
1469 aicon->icon->core->descriptor.parent = aicon;
1471 #ifdef XDND /* was OFFIX */
1472 cmd = WMGetFromPLDictionary(info, dDropCommand);
1473 if (cmd)
1474 aicon->dnd_command = wstrdup(WMGetFromPLString(cmd));
1475 #endif
1477 cmd = WMGetFromPLDictionary(info, dPasteCommand);
1478 if (cmd)
1479 aicon->paste_command = wstrdup(WMGetFromPLString(cmd));
1481 /* check auto launch */
1482 value = WMGetFromPLDictionary(info, dAutoLaunch);
1484 aicon->auto_launch = getBooleanDockValue(value, dAutoLaunch);
1486 /* check lock */
1487 value = WMGetFromPLDictionary(info, dLock);
1489 aicon->lock = getBooleanDockValue(value, dLock);
1491 /* check if it wasn't normally docked */
1492 value = WMGetFromPLDictionary(info, dForced);
1494 aicon->forced_dock = getBooleanDockValue(value, dForced);
1496 /* check if we can rely on the stuff in the app */
1497 value = WMGetFromPLDictionary(info, dBuggyApplication);
1499 aicon->buggy_app = getBooleanDockValue(value, dBuggyApplication);
1501 /* get position in the dock */
1502 value = WMGetFromPLDictionary(info, dPosition);
1503 if (value && WMIsPLString(value)) {
1504 if (sscanf(WMGetFromPLString(value), "%hi,%hi", &aicon->xindex, &aicon->yindex) != 2)
1505 wwarning(_("bad value in docked icon state info %s"), WMGetFromPLString(dPosition));
1507 /* check position sanity */
1508 /* incomplete section! */
1509 if (type == WM_DOCK) {
1510 aicon->xindex = 0;
1511 if (aicon->yindex < 0)
1512 wwarning(_("bad value in docked icon position %i,%i"),
1513 aicon->xindex, aicon->yindex);
1515 } else {
1516 aicon->yindex = index;
1517 aicon->xindex = 0;
1520 /* check if icon is omnipresent */
1521 value = WMGetFromPLDictionary(info, dOmnipresent);
1523 aicon->omnipresent = getBooleanDockValue(value, dOmnipresent);
1525 aicon->running = 0;
1526 aicon->docked = 1;
1528 return aicon;
1531 #define COMPLAIN(key) wwarning(_("bad value in dock state info:%s"), key)
1533 WAppIcon *wClipRestoreState(WScreen * scr, WMPropList * clip_state)
1535 WAppIcon *icon;
1536 WMPropList *value;
1538 icon = mainIconCreate(scr, WM_CLIP);
1540 if (!clip_state)
1541 return icon;
1543 WMRetainPropList(clip_state);
1545 /* restore position */
1547 value = WMGetFromPLDictionary(clip_state, dPosition);
1549 if (value) {
1550 if (!WMIsPLString(value))
1551 COMPLAIN("Position");
1552 else {
1553 WMRect rect;
1554 int flags;
1556 if (sscanf(WMGetFromPLString(value), "%i,%i", &icon->x_pos, &icon->y_pos) != 2)
1557 COMPLAIN("Position");
1559 /* check position sanity */
1560 rect.pos.x = icon->x_pos;
1561 rect.pos.y = icon->y_pos;
1562 rect.size.width = rect.size.height = ICON_SIZE;
1564 wGetRectPlacementInfo(scr, rect, &flags);
1565 if (flags & (XFLAG_DEAD | XFLAG_PARTIAL))
1566 wScreenKeepInside(scr, &icon->x_pos, &icon->y_pos, ICON_SIZE, ICON_SIZE);
1569 #ifdef XDND /* was OFFIX */
1570 value = WMGetFromPLDictionary(clip_state, dDropCommand);
1571 if (value && WMIsPLString(value))
1572 icon->dnd_command = wstrdup(WMGetFromPLString(value));
1573 #endif
1575 value = WMGetFromPLDictionary(clip_state, dPasteCommand);
1576 if (value && WMIsPLString(value))
1577 icon->paste_command = wstrdup(WMGetFromPLString(value));
1579 WMReleasePropList(clip_state);
1581 return icon;
1584 WDock *wDockRestoreState(WScreen * scr, WMPropList * dock_state, int type)
1586 WDock *dock;
1587 WMPropList *apps;
1588 WMPropList *value;
1589 WAppIcon *aicon, *old_top;
1590 int count, i;
1592 dock = wDockCreate(scr, type);
1594 if (!dock_state)
1595 return dock;
1597 WMRetainPropList(dock_state);
1599 /* restore position */
1601 value = WMGetFromPLDictionary(dock_state, dPosition);
1603 if (value) {
1604 if (!WMIsPLString(value)) {
1605 COMPLAIN("Position");
1606 } else {
1607 WMRect rect;
1608 int flags;
1610 if (sscanf(WMGetFromPLString(value), "%i,%i", &dock->x_pos, &dock->y_pos) != 2)
1611 COMPLAIN("Position");
1613 /* check position sanity */
1614 rect.pos.x = dock->x_pos;
1615 rect.pos.y = dock->y_pos;
1616 rect.size.width = rect.size.height = ICON_SIZE;
1618 wGetRectPlacementInfo(scr, rect, &flags);
1619 if (flags & (XFLAG_DEAD | XFLAG_PARTIAL)) {
1620 int x = dock->x_pos;
1621 wScreenKeepInside(scr, &x, &dock->y_pos, ICON_SIZE, ICON_SIZE);
1624 /* Is this needed any more? */
1625 if (type == WM_CLIP) {
1626 if (dock->x_pos < 0) {
1627 dock->x_pos = 0;
1628 } else if (dock->x_pos > scr->scr_width - ICON_SIZE) {
1629 dock->x_pos = scr->scr_width - ICON_SIZE;
1631 } else {
1632 if (dock->x_pos >= 0) {
1633 dock->x_pos = DOCK_EXTRA_SPACE;
1634 dock->on_right_side = 0;
1635 } else {
1636 dock->x_pos = scr->scr_width - DOCK_EXTRA_SPACE - ICON_SIZE;
1637 dock->on_right_side = 1;
1643 /* restore lowered/raised state */
1645 dock->lowered = 0;
1647 value = WMGetFromPLDictionary(dock_state, dLowered);
1649 if (value) {
1650 if (!WMIsPLString(value)) {
1651 COMPLAIN("Lowered");
1652 } else {
1653 if (strcasecmp(WMGetFromPLString(value), "YES") == 0) {
1654 dock->lowered = 1;
1659 /* restore collapsed state */
1661 dock->collapsed = 0;
1663 value = WMGetFromPLDictionary(dock_state, dCollapsed);
1665 if (value) {
1666 if (!WMIsPLString(value)) {
1667 COMPLAIN("Collapsed");
1668 } else {
1669 if (strcasecmp(WMGetFromPLString(value), "YES") == 0) {
1670 dock->collapsed = 1;
1675 /* restore auto-collapsed state */
1677 value = WMGetFromPLDictionary(dock_state, dAutoCollapse);
1679 if (value) {
1680 if (!WMIsPLString(value)) {
1681 COMPLAIN("AutoCollapse");
1682 } else {
1683 if (strcasecmp(WMGetFromPLString(value), "YES") == 0) {
1684 dock->auto_collapse = 1;
1685 dock->collapsed = 1;
1690 /* restore auto-raise/lower state */
1692 value = WMGetFromPLDictionary(dock_state, dAutoRaiseLower);
1694 if (value) {
1695 if (!WMIsPLString(value)) {
1696 COMPLAIN("AutoRaiseLower");
1697 } else {
1698 if (strcasecmp(WMGetFromPLString(value), "YES") == 0) {
1699 dock->auto_raise_lower = 1;
1704 /* restore attract icons state */
1706 dock->attract_icons = 0;
1708 value = WMGetFromPLDictionary(dock_state, dAutoAttractIcons);
1710 if (value) {
1711 if (!WMIsPLString(value)) {
1712 COMPLAIN("AutoAttractIcons");
1713 } else {
1714 if (strcasecmp(WMGetFromPLString(value), "YES") == 0) {
1715 dock->attract_icons = 1;
1720 /* application list */
1723 WMPropList *tmp;
1724 char buffer[64];
1727 * When saving, it saves the dock state in
1728 * Applications and Applicationsnnn
1730 * When loading, it will first try Applicationsnnn.
1731 * If it does not exist, use Applications as default.
1734 snprintf(buffer, sizeof(buffer), "Applications%i", scr->scr_height);
1736 tmp = WMCreatePLString(buffer);
1737 apps = WMGetFromPLDictionary(dock_state, tmp);
1738 WMReleasePropList(tmp);
1740 if (!apps) {
1741 apps = WMGetFromPLDictionary(dock_state, dApplications);
1745 if (!apps) {
1746 goto finish;
1749 count = WMGetPropListItemCount(apps);
1751 if (count == 0)
1752 goto finish;
1754 old_top = dock->icon_array[0];
1756 /* dock->icon_count is set to 1 when dock is created.
1757 * Since Clip is already restored, we want to keep it so for clip,
1758 * but for dock we may change the default top tile, so we set it to 0.
1760 if (type == WM_DOCK)
1761 dock->icon_count = 0;
1763 for (i = 0; i < count; i++) {
1764 if (dock->icon_count >= dock->max_icons) {
1765 wwarning(_("there are too many icons stored in dock. Ignoring what doesn't fit"));
1766 break;
1769 value = WMGetFromPLArray(apps, i);
1770 aicon = restore_icon_state(scr, value, type, dock->icon_count);
1772 dock->icon_array[dock->icon_count] = aicon;
1774 if (aicon) {
1775 aicon->dock = dock;
1776 aicon->x_pos = dock->x_pos + (aicon->xindex * ICON_SIZE);
1777 aicon->y_pos = dock->y_pos + (aicon->yindex * ICON_SIZE);
1779 if (dock->lowered)
1780 ChangeStackingLevel(aicon->icon->core, WMNormalLevel);
1781 else
1782 ChangeStackingLevel(aicon->icon->core, WMDockLevel);
1784 wCoreConfigure(aicon->icon->core, aicon->x_pos, aicon->y_pos, 0, 0);
1786 if (!dock->collapsed)
1787 XMapWindow(dpy, aicon->icon->core->window);
1788 wRaiseFrame(aicon->icon->core);
1790 dock->icon_count++;
1791 } else if (dock->icon_count == 0 && type == WM_DOCK)
1792 dock->icon_count++;
1795 /* if the first icon is not defined, use the default */
1796 if (dock->icon_array[0] == NULL) {
1797 /* update default icon */
1798 old_top->x_pos = dock->x_pos;
1799 old_top->y_pos = dock->y_pos;
1800 if (dock->lowered)
1801 ChangeStackingLevel(old_top->icon->core, WMNormalLevel);
1802 else
1803 ChangeStackingLevel(old_top->icon->core, WMDockLevel);
1804 dock->icon_array[0] = old_top;
1805 XMoveWindow(dpy, old_top->icon->core->window, dock->x_pos, dock->y_pos);
1806 /* we don't need to increment dock->icon_count here because it was
1807 * incremented in the loop above.
1809 } else if (old_top != dock->icon_array[0]) {
1810 if (old_top == scr->clip_icon)
1811 scr->clip_icon = dock->icon_array[0];
1812 wAppIconDestroy(old_top);
1815 finish:
1816 WMReleasePropList(dock_state);
1818 return dock;
1821 void wDockLaunchWithState(WDock * dock, WAppIcon * btn, WSavedState * state)
1823 if (btn && btn->command && !btn->running && !btn->launching) {
1825 btn->drop_launch = 0;
1826 btn->paste_launch = 0;
1828 btn->pid = execCommand(btn, btn->command, state);
1830 if (btn->pid > 0) {
1831 if (!btn->forced_dock && !btn->buggy_app) {
1832 btn->launching = 1;
1833 dockIconPaint(btn);
1836 } else {
1837 wfree(state);
1841 void wDockDoAutoLaunch(WDock * dock, int workspace)
1843 WAppIcon *btn;
1844 WSavedState *state;
1845 int i;
1847 for (i = 0; i < dock->max_icons; i++) {
1848 btn = dock->icon_array[i];
1849 if (!btn || !btn->auto_launch)
1850 continue;
1852 state = wmalloc(sizeof(WSavedState));
1853 memset(state, 0, sizeof(WSavedState));
1854 state->workspace = workspace;
1855 /* TODO: this is klugy and is very difficult to understand
1856 * what's going on. Try to clean up */
1857 wDockLaunchWithState(dock, btn, state);
1861 #ifdef XDND /* was OFFIX */
1862 static WDock *findDock(WScreen * scr, XEvent * event, int *icon_pos)
1864 WDock *dock;
1865 int i;
1867 *icon_pos = -1;
1868 if ((dock = scr->dock) != NULL) {
1869 for (i = 0; i < dock->max_icons; i++) {
1870 if (dock->icon_array[i]
1871 && dock->icon_array[i]->icon->core->window == event->xclient.window) {
1872 *icon_pos = i;
1873 break;
1877 if (*icon_pos < 0 && (dock = scr->workspaces[scr->current_workspace]->clip) != NULL) {
1878 for (i = 0; i < dock->max_icons; i++) {
1879 if (dock->icon_array[i]
1880 && dock->icon_array[i]->icon->core->window == event->xclient.window) {
1881 *icon_pos = i;
1882 break;
1886 if (*icon_pos >= 0)
1887 return dock;
1888 return NULL;
1891 int wDockReceiveDNDDrop(WScreen * scr, XEvent * event)
1893 WDock *dock;
1894 WAppIcon *btn;
1895 int icon_pos;
1897 dock = findDock(scr, event, &icon_pos);
1898 if (!dock)
1899 return False;
1902 * Return True if the drop was on an application icon window.
1903 * In this case, let the ClientMessage handler redirect the
1904 * message to the app.
1906 if (dock->icon_array[icon_pos]->icon->icon_win != None)
1907 return True;
1909 if (dock->icon_array[icon_pos]->dnd_command != NULL) {
1910 scr->flags.dnd_data_convertion_status = 0;
1912 btn = dock->icon_array[icon_pos];
1914 if (!btn->forced_dock) {
1915 btn->relaunching = btn->running;
1916 btn->running = 1;
1918 if (btn->wm_instance || btn->wm_class) {
1919 WWindowAttributes attr;
1920 memset(&attr, 0, sizeof(WWindowAttributes));
1921 wDefaultFillAttributes(btn->icon->core->screen_ptr,
1922 btn->wm_instance, btn->wm_class, &attr, NULL, True);
1924 if (!attr.no_appicon)
1925 btn->launching = 1;
1926 else
1927 btn->running = 0;
1930 btn->paste_launch = 0;
1931 btn->drop_launch = 1;
1932 scr->last_dock = dock;
1933 btn->pid = execCommand(btn, btn->dnd_command, NULL);
1934 if (btn->pid > 0) {
1935 dockIconPaint(btn);
1936 } else {
1937 btn->launching = 0;
1938 if (!btn->relaunching) {
1939 btn->running = 0;
1943 return False;
1945 #endif /* XDND */
1947 Bool wDockAttachIcon(WDock * dock, WAppIcon * icon, int x, int y)
1949 WWindow *wwin;
1950 int index;
1952 wwin = icon->icon->owner;
1953 if (icon->command == NULL) {
1954 char *command;
1956 icon->editing = 0;
1958 command = GetCommandForWindow(wwin->client_win);
1959 if (command) {
1960 icon->command = command;
1961 } else {
1962 /* icon->forced_dock = 1; */
1963 if (dock->type != WM_CLIP || !icon->attracted) {
1964 icon->editing = 1;
1965 if (wInputDialog(dock->screen_ptr, _("Dock Icon"),
1966 _("Type the command used to launch the application"), &command)) {
1967 if (command && (command[0] == 0 || (command[0] == '-' && command[1] == 0))) {
1968 wfree(command);
1969 command = NULL;
1971 icon->command = command;
1972 icon->editing = 0;
1973 } else {
1974 icon->editing = 0;
1975 if (command)
1976 wfree(command);
1977 /* If the target is the dock, reject the icon. If
1978 * the target is the clip, make it an attracted icon
1980 if (dock->type == WM_CLIP) {
1981 icon->attracted = 1;
1982 if (!icon->icon->shadowed) {
1983 icon->icon->shadowed = 1;
1984 icon->icon->force_paint = 1;
1986 } else {
1987 return False;
1992 } else {
1993 icon->editing = 0;
1996 for (index = 1; index < dock->max_icons; index++)
1997 if (dock->icon_array[index] == NULL)
1998 break;
1999 /* if (index == dock->max_icons)
2000 return; */
2002 assert(index < dock->max_icons);
2004 dock->icon_array[index] = icon;
2005 icon->yindex = y;
2006 icon->xindex = x;
2008 icon->omnipresent = 0;
2010 icon->x_pos = dock->x_pos + x * ICON_SIZE;
2011 icon->y_pos = dock->y_pos + y * ICON_SIZE;
2013 dock->icon_count++;
2015 icon->running = 1;
2016 icon->launching = 0;
2017 icon->docked = 1;
2018 icon->dock = dock;
2019 icon->icon->core->descriptor.handle_mousedown = iconMouseDown;
2020 if (dock->type == WM_CLIP) {
2021 icon->icon->core->descriptor.handle_enternotify = clipEnterNotify;
2022 icon->icon->core->descriptor.handle_leavenotify = clipLeaveNotify;
2024 icon->icon->core->descriptor.parent_type = WCLASS_DOCK_ICON;
2025 icon->icon->core->descriptor.parent = icon;
2027 MoveInStackListUnder(dock->icon_array[index - 1]->icon->core, icon->icon->core);
2028 wAppIconMove(icon, icon->x_pos, icon->y_pos);
2029 wAppIconPaint(icon);
2031 if (wPreferences.auto_arrange_icons)
2032 wArrangeIcons(dock->screen_ptr, True);
2034 #ifdef XDND /* was OFFIX */
2035 if (icon->command && !icon->dnd_command) {
2036 int len = strlen(icon->command) + 8;
2037 icon->dnd_command = wmalloc(len);
2038 snprintf(icon->dnd_command, len, "%s %%d", icon->command);
2040 #endif
2042 if (icon->command && !icon->paste_command) {
2043 int len = strlen(icon->command) + 8;
2044 icon->paste_command = wmalloc(len);
2045 snprintf(icon->paste_command, len, "%s %%s", icon->command);
2048 return True;
2051 void reattachIcon(WDock * dock, WAppIcon * icon, int x, int y)
2053 int index;
2055 for (index = 1; index < dock->max_icons; index++) {
2056 if (dock->icon_array[index] == icon)
2057 break;
2059 assert(index < dock->max_icons);
2061 icon->yindex = y;
2062 icon->xindex = x;
2064 icon->x_pos = dock->x_pos + x * ICON_SIZE;
2065 icon->y_pos = dock->y_pos + y * ICON_SIZE;
2068 Bool moveIconBetweenDocks(WDock * src, WDock * dest, WAppIcon * icon, int x, int y)
2070 WWindow *wwin;
2071 char *command;
2072 int index;
2074 if (src == dest)
2075 return True; /* No move needed, we're already there */
2077 if (dest == NULL)
2078 return False;
2080 wwin = icon->icon->owner;
2083 * For the moment we can't do this if we move icons in Clip from one
2084 * workspace to other, because if we move two or more icons without
2085 * command, the dialog box will not be able to tell us to which of the
2086 * moved icons it applies. -Dan
2088 if ((dest->type == WM_DOCK /*|| dest->keep_attracted */ ) && icon->command == NULL) {
2089 command = GetCommandForWindow(wwin->client_win);
2090 if (command) {
2091 icon->command = command;
2092 } else {
2093 icon->editing = 1;
2094 /* icon->forced_dock = 1; */
2095 if (wInputDialog(src->screen_ptr, _("Dock Icon"),
2096 _("Type the command used to launch the application"), &command)) {
2097 if (command && (command[0] == 0 || (command[0] == '-' && command[1] == 0))) {
2098 wfree(command);
2099 command = NULL;
2101 icon->command = command;
2102 } else {
2103 icon->editing = 0;
2104 if (command)
2105 wfree(command);
2106 return False;
2108 icon->editing = 0;
2112 if (dest->type == WM_DOCK)
2113 wClipMakeIconOmnipresent(icon, False);
2115 for (index = 1; index < src->max_icons; index++) {
2116 if (src->icon_array[index] == icon)
2117 break;
2119 assert(index < src->max_icons);
2121 src->icon_array[index] = NULL;
2122 src->icon_count--;
2124 for (index = 1; index < dest->max_icons; index++) {
2125 if (dest->icon_array[index] == NULL)
2126 break;
2128 /* if (index == dest->max_icons)
2129 return; */
2131 assert(index < dest->max_icons);
2133 dest->icon_array[index] = icon;
2134 icon->dock = dest;
2136 /* deselect the icon */
2137 if (icon->icon->selected)
2138 wIconSelect(icon->icon);
2140 if (dest->type == WM_DOCK) {
2141 icon->icon->core->descriptor.handle_enternotify = NULL;
2142 icon->icon->core->descriptor.handle_leavenotify = NULL;
2143 } else {
2144 icon->icon->core->descriptor.handle_enternotify = clipEnterNotify;
2145 icon->icon->core->descriptor.handle_leavenotify = clipLeaveNotify;
2148 /* set it to be kept when moving to dock.
2149 * Unless the icon does not have a command set
2151 if (icon->command && dest->type == WM_DOCK) {
2152 icon->attracted = 0;
2153 if (icon->icon->shadowed) {
2154 icon->icon->shadowed = 0;
2155 icon->icon->force_paint = 1;
2159 if (src->auto_collapse || src->auto_raise_lower)
2160 clipLeave(src);
2162 icon->yindex = y;
2163 icon->xindex = x;
2165 icon->x_pos = dest->x_pos + x * ICON_SIZE;
2166 icon->y_pos = dest->y_pos + y * ICON_SIZE;
2168 dest->icon_count++;
2170 MoveInStackListUnder(dest->icon_array[index - 1]->icon->core, icon->icon->core);
2171 wAppIconPaint(icon);
2173 return True;
2176 void wDockDetach(WDock * dock, WAppIcon * icon)
2178 int index;
2180 /* make the settings panel be closed */
2181 if (icon->panel) {
2182 DestroyDockAppSettingsPanel(icon->panel);
2185 /* This must be called before icon->dock is set to NULL.
2186 * Don't move it. -Dan
2188 wClipMakeIconOmnipresent(icon, False);
2190 icon->docked = 0;
2191 icon->dock = NULL;
2192 icon->attracted = 0;
2193 icon->auto_launch = 0;
2194 if (icon->icon->shadowed) {
2195 icon->icon->shadowed = 0;
2196 icon->icon->force_paint = 1;
2199 /* deselect the icon */
2200 if (icon->icon->selected)
2201 wIconSelect(icon->icon);
2203 if (icon->command) {
2204 wfree(icon->command);
2205 icon->command = NULL;
2207 #ifdef XDND /* was OFFIX */
2208 if (icon->dnd_command) {
2209 wfree(icon->dnd_command);
2210 icon->dnd_command = NULL;
2212 #endif
2213 if (icon->paste_command) {
2214 wfree(icon->paste_command);
2215 icon->paste_command = NULL;
2218 for (index = 1; index < dock->max_icons; index++)
2219 if (dock->icon_array[index] == icon)
2220 break;
2221 assert(index < dock->max_icons);
2222 dock->icon_array[index] = NULL;
2223 icon->yindex = -1;
2224 icon->xindex = -1;
2226 dock->icon_count--;
2228 /* if the dock is not attached to an application or
2229 * the the application did not set the approriate hints yet,
2230 * destroy the icon */
2231 if (!icon->running || !wApplicationOf(icon->main_window))
2232 wAppIconDestroy(icon);
2233 else {
2234 icon->icon->core->descriptor.handle_mousedown = appIconMouseDown;
2235 icon->icon->core->descriptor.handle_enternotify = NULL;
2236 icon->icon->core->descriptor.handle_leavenotify = NULL;
2237 icon->icon->core->descriptor.parent_type = WCLASS_APPICON;
2238 icon->icon->core->descriptor.parent = icon;
2240 ChangeStackingLevel(icon->icon->core, NORMAL_ICON_LEVEL);
2242 wAppIconPaint(icon);
2243 if (wPreferences.auto_arrange_icons) {
2244 wArrangeIcons(dock->screen_ptr, True);
2247 if (dock->auto_collapse || dock->auto_raise_lower)
2248 clipLeave(dock);
2252 * returns the closest Dock slot index for the passed
2253 * coordinates.
2255 * Returns False if icon can't be docked.
2257 * Note: this function should NEVER alter ret_x or ret_y, unless it will
2258 * return True. -Dan
2260 Bool wDockSnapIcon(WDock * dock, WAppIcon * icon, int req_x, int req_y, int *ret_x, int *ret_y, int redocking)
2262 WScreen *scr = dock->screen_ptr;
2263 int dx, dy;
2264 int ex_x, ex_y;
2265 int i, offset = ICON_SIZE / 2;
2266 WAppIcon *aicon = NULL;
2267 WAppIcon *nicon = NULL;
2268 int max_y_icons, max_x_icons;
2270 /* TODO: XINERAMA, for these */
2271 max_x_icons = scr->scr_width / ICON_SIZE;
2272 max_y_icons = scr->scr_height / ICON_SIZE - 1;
2274 if (wPreferences.flags.noupdates)
2275 return False;
2277 dx = dock->x_pos;
2278 dy = dock->y_pos;
2280 /* if the dock is full */
2281 if (!redocking && (dock->icon_count >= dock->max_icons)) {
2282 return False;
2285 /* exact position */
2286 if (req_y < dy)
2287 ex_y = (req_y - offset - dy) / ICON_SIZE;
2288 else
2289 ex_y = (req_y + offset - dy) / ICON_SIZE;
2291 if (req_x < dx)
2292 ex_x = (req_x - offset - dx) / ICON_SIZE;
2293 else
2294 ex_x = (req_x + offset - dx) / ICON_SIZE;
2296 /* check if the icon is outside the screen boundaries */
2298 WMRect rect;
2299 int flags;
2301 rect.pos.x = dx + ex_x * ICON_SIZE;
2302 rect.pos.y = dy + ex_y * ICON_SIZE;
2303 rect.size.width = rect.size.height = ICON_SIZE;
2305 wGetRectPlacementInfo(scr, rect, &flags);
2306 if (flags & (XFLAG_DEAD | XFLAG_PARTIAL))
2307 return False;
2310 if (dock->type == WM_DOCK) {
2311 if (icon->dock != dock && ex_x != 0)
2312 return False;
2314 aicon = NULL;
2315 for (i = 0; i < dock->max_icons; i++) {
2316 nicon = dock->icon_array[i];
2317 if (nicon && nicon->yindex == ex_y) {
2318 aicon = nicon;
2319 break;
2323 if (redocking) {
2324 int sig, done, closest;
2326 /* Possible cases when redocking:
2328 * icon dragged out of range of any slot -> false
2329 * icon dragged to range of free slot
2330 * icon dragged to range of same slot
2331 * icon dragged to range of different icon
2333 if (abs(ex_x) > DOCK_DETTACH_THRESHOLD)
2334 return False;
2336 if (ex_y >= 0 && ex_y <= max_y_icons && (aicon == icon || !aicon)) {
2337 *ret_x = 0;
2338 *ret_y = ex_y;
2339 return True;
2342 /* start looking at the upper slot or lower? */
2343 if (ex_y * ICON_SIZE < (req_y + offset - dy))
2344 sig = 1;
2345 else
2346 sig = -1;
2348 closest = -1;
2349 done = 0;
2350 /* look for closest free slot */
2351 for (i = 0; i < (DOCK_DETTACH_THRESHOLD + 1) * 2 && !done; i++) {
2352 int j;
2354 done = 1;
2355 closest = sig * (i / 2) + ex_y;
2356 /* check if this slot is used */
2357 if (closest >= 0) {
2358 for (j = 0; j < dock->max_icons; j++) {
2359 if (dock->icon_array[j]
2360 && dock->icon_array[j]->yindex == closest) {
2361 /* slot is used by someone else */
2362 if (dock->icon_array[j] != icon)
2363 done = 0;
2364 break;
2368 sig = -sig;
2370 if (done && closest >= 0 && closest <= max_y_icons &&
2371 ((ex_y >= closest && ex_y - closest < DOCK_DETTACH_THRESHOLD + 1)
2372 || (ex_y < closest && closest - ex_y <= DOCK_DETTACH_THRESHOLD + 1))) {
2373 *ret_x = 0;
2374 *ret_y = closest;
2375 return True;
2377 } else { /* !redocking */
2379 /* if slot is free and the icon is close enough, return it */
2380 if (!aicon && ex_x == 0 && ex_y >= 0 && ex_y <= max_y_icons) {
2381 *ret_x = 0;
2382 *ret_y = ex_y;
2383 return True;
2386 } else { /* CLIP */
2387 int neighbours = 0;
2388 int start, stop, k;
2390 start = icon->omnipresent ? 0 : scr->current_workspace;
2391 stop = icon->omnipresent ? scr->workspace_count : start + 1;
2393 aicon = NULL;
2394 for (k = start; k < stop; k++) {
2395 WDock *tmp = scr->workspaces[k]->clip;
2396 if (!tmp)
2397 continue;
2398 for (i = 0; i < tmp->max_icons; i++) {
2399 nicon = tmp->icon_array[i];
2400 if (nicon && nicon->xindex == ex_x && nicon->yindex == ex_y) {
2401 aicon = nicon;
2402 break;
2405 if (aicon)
2406 break;
2408 for (k = start; k < stop; k++) {
2409 WDock *tmp = scr->workspaces[k]->clip;
2410 if (!tmp)
2411 continue;
2412 for (i = 0; i < tmp->max_icons; i++) {
2413 nicon = tmp->icon_array[i];
2414 if (nicon && nicon != icon && /* Icon can't be it's own neighbour */
2415 (abs(nicon->xindex - ex_x) <= CLIP_ATTACH_VICINITY &&
2416 abs(nicon->yindex - ex_y) <= CLIP_ATTACH_VICINITY)) {
2417 neighbours = 1;
2418 break;
2421 if (neighbours)
2422 break;
2425 if (neighbours && (aicon == NULL || (redocking && aicon == icon))) {
2426 *ret_x = ex_x;
2427 *ret_y = ex_y;
2428 return True;
2431 return False;
2434 static int onScreen(WScreen * scr, int x, int y, int sx, int ex, int sy, int ey)
2436 WMRect rect = wmkrect(x, y, ICON_SIZE, ICON_SIZE);
2437 int flags;
2439 wGetRectPlacementInfo(scr, rect, &flags);
2441 return !(flags & (XFLAG_DEAD | XFLAG_PARTIAL));
2445 * returns true if it can find a free slot in the dock,
2446 * in which case it changes x_pos and y_pos accordingly.
2447 * Else returns false.
2449 Bool wDockFindFreeSlot(WDock * dock, int *x_pos, int *y_pos)
2451 WScreen *scr = dock->screen_ptr;
2452 WAppIcon *btn;
2453 WAppIconChain *chain;
2454 unsigned char *slot_map;
2455 int mwidth;
2456 int r;
2457 int x, y;
2458 int i, done = False;
2459 int corner;
2460 int sx = 0, sy = 0, ex = scr->scr_width, ey = scr->scr_height;
2461 int extra_count = 0;
2463 if (dock->type == WM_CLIP && dock != scr->workspaces[scr->current_workspace]->clip)
2464 extra_count = scr->global_icon_count;
2466 /* if the dock is full */
2467 if (dock->icon_count + extra_count >= dock->max_icons) {
2468 return False;
2471 if (!wPreferences.flags.nodock && scr->dock) {
2472 if (scr->dock->on_right_side)
2473 ex -= ICON_SIZE + DOCK_EXTRA_SPACE;
2474 else
2475 sx += ICON_SIZE + DOCK_EXTRA_SPACE;
2478 if (ex < dock->x_pos)
2479 ex = dock->x_pos;
2480 if (sx > dock->x_pos + ICON_SIZE)
2481 sx = dock->x_pos + ICON_SIZE;
2482 #define C_NONE 0
2483 #define C_NW 1
2484 #define C_NE 2
2485 #define C_SW 3
2486 #define C_SE 4
2488 /* check if clip is in a corner */
2489 if (dock->type == WM_CLIP) {
2490 if (dock->x_pos < 1 && dock->y_pos < 1)
2491 corner = C_NE;
2492 else if (dock->x_pos < 1 && dock->y_pos >= (ey - ICON_SIZE))
2493 corner = C_SE;
2494 else if (dock->x_pos >= (ex - ICON_SIZE) && dock->y_pos >= (ey - ICON_SIZE))
2495 corner = C_SW;
2496 else if (dock->x_pos >= (ex - ICON_SIZE) && dock->y_pos < 1)
2497 corner = C_NW;
2498 else
2499 corner = C_NONE;
2500 } else
2501 corner = C_NONE;
2503 /* If the clip is in the corner, use only slots that are in the border
2504 * of the screen */
2505 if (corner != C_NONE) {
2506 char *hmap, *vmap;
2507 int hcount, vcount;
2509 hcount = WMIN(dock->max_icons, scr->scr_width / ICON_SIZE);
2510 vcount = WMIN(dock->max_icons, scr->scr_height / ICON_SIZE);
2511 hmap = wmalloc(hcount + 1);
2512 memset(hmap, 0, hcount + 1);
2513 vmap = wmalloc(vcount + 1);
2514 memset(vmap, 0, vcount + 1);
2516 /* mark used positions */
2517 switch (corner) {
2518 case C_NE:
2519 for (i = 0; i < dock->max_icons; i++) {
2520 btn = dock->icon_array[i];
2521 if (!btn)
2522 continue;
2524 if (btn->xindex == 0 && btn->yindex > 0 && btn->yindex < vcount)
2525 vmap[btn->yindex] = 1;
2526 else if (btn->yindex == 0 && btn->xindex > 0 && btn->xindex < hcount)
2527 hmap[btn->xindex] = 1;
2529 for (chain = scr->global_icons; chain != NULL; chain = chain->next) {
2530 btn = chain->aicon;
2531 if (btn->xindex == 0 && btn->yindex > 0 && btn->yindex < vcount)
2532 vmap[btn->yindex] = 1;
2533 else if (btn->yindex == 0 && btn->xindex > 0 && btn->xindex < hcount)
2534 hmap[btn->xindex] = 1;
2536 break;
2537 case C_NW:
2538 for (i = 0; i < dock->max_icons; i++) {
2539 btn = dock->icon_array[i];
2540 if (!btn)
2541 continue;
2543 if (btn->xindex == 0 && btn->yindex > 0 && btn->yindex < vcount)
2544 vmap[btn->yindex] = 1;
2545 else if (btn->yindex == 0 && btn->xindex < 0 && btn->xindex > -hcount)
2546 hmap[-btn->xindex] = 1;
2548 for (chain = scr->global_icons; chain != NULL; chain = chain->next) {
2549 btn = chain->aicon;
2550 if (btn->xindex == 0 && btn->yindex > 0 && btn->yindex < vcount)
2551 vmap[btn->yindex] = 1;
2552 else if (btn->yindex == 0 && btn->xindex < 0 && btn->xindex > -hcount)
2553 hmap[-btn->xindex] = 1;
2555 break;
2556 case C_SE:
2557 for (i = 0; i < dock->max_icons; i++) {
2558 btn = dock->icon_array[i];
2559 if (!btn)
2560 continue;
2562 if (btn->xindex == 0 && btn->yindex < 0 && btn->yindex > -vcount)
2563 vmap[-btn->yindex] = 1;
2564 else if (btn->yindex == 0 && btn->xindex > 0 && btn->xindex < hcount)
2565 hmap[btn->xindex] = 1;
2567 for (chain = scr->global_icons; chain != NULL; chain = chain->next) {
2568 btn = chain->aicon;
2569 if (btn->xindex == 0 && btn->yindex < 0 && btn->yindex > -vcount)
2570 vmap[-btn->yindex] = 1;
2571 else if (btn->yindex == 0 && btn->xindex > 0 && btn->xindex < hcount)
2572 hmap[btn->xindex] = 1;
2574 break;
2575 case C_SW:
2576 default:
2577 for (i = 0; i < dock->max_icons; i++) {
2578 btn = dock->icon_array[i];
2579 if (!btn)
2580 continue;
2582 if (btn->xindex == 0 && btn->yindex < 0 && btn->yindex > -vcount)
2583 vmap[-btn->yindex] = 1;
2584 else if (btn->yindex == 0 && btn->xindex < 0 && btn->xindex > -hcount)
2585 hmap[-btn->xindex] = 1;
2587 for (chain = scr->global_icons; chain != NULL; chain = chain->next) {
2588 btn = chain->aicon;
2589 if (btn->xindex == 0 && btn->yindex < 0 && btn->yindex > -vcount)
2590 vmap[-btn->yindex] = 1;
2591 else if (btn->yindex == 0 && btn->xindex < 0 && btn->xindex > -hcount)
2592 hmap[-btn->xindex] = 1;
2595 x = 0;
2596 y = 0;
2597 done = 0;
2598 /* search a vacant slot */
2599 for (i = 1; i < WMAX(vcount, hcount); i++) {
2600 if (i < vcount && vmap[i] == 0) {
2601 /* found a slot */
2602 x = 0;
2603 y = i;
2604 done = 1;
2605 break;
2606 } else if (i < hcount && hmap[i] == 0) {
2607 /* found a slot */
2608 x = i;
2609 y = 0;
2610 done = 1;
2611 break;
2614 wfree(vmap);
2615 wfree(hmap);
2616 /* If found a slot, translate and return */
2617 if (done) {
2618 if (corner == C_NW || corner == C_NE) {
2619 *y_pos = y;
2620 } else {
2621 *y_pos = -y;
2623 if (corner == C_NE || corner == C_SE) {
2624 *x_pos = x;
2625 } else {
2626 *x_pos = -x;
2628 return True;
2630 /* else, try to find a slot somewhere else */
2633 /* a map of mwidth x mwidth would be enough if we allowed icons to be
2634 * placed outside of screen */
2635 mwidth = (int)ceil(sqrt(dock->max_icons));
2637 /* In the worst case (the clip is in the corner of the screen),
2638 * the amount of icons that fit in the clip is smaller.
2639 * Double the map to get a safe value.
2641 mwidth += mwidth;
2643 r = (mwidth - 1) / 2;
2645 slot_map = wmalloc(mwidth * mwidth);
2646 memset(slot_map, 0, mwidth * mwidth);
2648 #define XY2OFS(x,y) (WMAX(abs(x),abs(y)) > r) ? 0 : (((y)+r)*(mwidth)+(x)+r)
2650 /* mark used slots in the map. If the slot falls outside the map
2651 * (for example, when all icons are placed in line), ignore them. */
2652 for (i = 0; i < dock->max_icons; i++) {
2653 btn = dock->icon_array[i];
2654 if (btn)
2655 slot_map[XY2OFS(btn->xindex, btn->yindex)] = 1;
2657 for (chain = scr->global_icons; chain != NULL; chain = chain->next) {
2658 slot_map[XY2OFS(chain->aicon->xindex, chain->aicon->yindex)] = 1;
2660 /* Find closest slot from the center that is free by scanning the
2661 * map from the center to outward in circular passes.
2662 * This will not result in a neat layout, but will be optimal
2663 * in the sense that there will not be holes left.
2665 done = 0;
2666 for (i = 1; i <= r && !done; i++) {
2667 int tx, ty;
2669 /* top and bottom parts of the ring */
2670 for (x = -i; x <= i && !done; x++) {
2671 tx = dock->x_pos + x * ICON_SIZE;
2672 y = -i;
2673 ty = dock->y_pos + y * ICON_SIZE;
2674 if (slot_map[XY2OFS(x, y)] == 0 && onScreen(scr, tx, ty, sx, ex, sy, ey)) {
2675 *x_pos = x;
2676 *y_pos = y;
2677 done = 1;
2678 break;
2680 y = i;
2681 ty = dock->y_pos + y * ICON_SIZE;
2682 if (slot_map[XY2OFS(x, y)] == 0 && onScreen(scr, tx, ty, sx, ex, sy, ey)) {
2683 *x_pos = x;
2684 *y_pos = y;
2685 done = 1;
2686 break;
2689 /* left and right parts of the ring */
2690 for (y = -i + 1; y <= i - 1; y++) {
2691 ty = dock->y_pos + y * ICON_SIZE;
2692 x = -i;
2693 tx = dock->x_pos + x * ICON_SIZE;
2694 if (slot_map[XY2OFS(x, y)] == 0 && onScreen(scr, tx, ty, sx, ex, sy, ey)) {
2695 *x_pos = x;
2696 *y_pos = y;
2697 done = 1;
2698 break;
2700 x = i;
2701 tx = dock->x_pos + x * ICON_SIZE;
2702 if (slot_map[XY2OFS(x, y)] == 0 && onScreen(scr, tx, ty, sx, ex, sy, ey)) {
2703 *x_pos = x;
2704 *y_pos = y;
2705 done = 1;
2706 break;
2710 wfree(slot_map);
2711 #undef XY2OFS
2712 return done;
2715 static void moveDock(WDock * dock, int new_x, int new_y)
2717 WAppIcon *btn;
2718 int i;
2720 dock->x_pos = new_x;
2721 dock->y_pos = new_y;
2722 for (i = 0; i < dock->max_icons; i++) {
2723 btn = dock->icon_array[i];
2724 if (btn) {
2725 btn->x_pos = new_x + btn->xindex * ICON_SIZE;
2726 btn->y_pos = new_y + btn->yindex * ICON_SIZE;
2727 XMoveWindow(dpy, btn->icon->core->window, btn->x_pos, btn->y_pos);
2732 static void swapDock(WDock * dock)
2734 WScreen *scr = dock->screen_ptr;
2735 WAppIcon *btn;
2736 int x, i;
2738 if (dock->on_right_side) {
2739 x = dock->x_pos = scr->scr_width - ICON_SIZE - DOCK_EXTRA_SPACE;
2740 } else {
2741 x = dock->x_pos = DOCK_EXTRA_SPACE;
2744 for (i = 0; i < dock->max_icons; i++) {
2745 btn = dock->icon_array[i];
2746 if (btn) {
2747 btn->x_pos = x;
2748 XMoveWindow(dpy, btn->icon->core->window, btn->x_pos, btn->y_pos);
2752 wScreenUpdateUsableArea(scr);
2755 static pid_t execCommand(WAppIcon * btn, char *command, WSavedState * state)
2757 WScreen *scr = btn->icon->core->screen_ptr;
2758 pid_t pid;
2759 char **argv;
2760 int argc;
2761 char *cmdline;
2763 cmdline = ExpandOptions(scr, command);
2765 if (scr->flags.dnd_data_convertion_status || !cmdline) {
2766 if (cmdline)
2767 wfree(cmdline);
2768 if (state)
2769 wfree(state);
2770 return 0;
2773 wtokensplit(cmdline, &argv, &argc);
2775 if (!argc) {
2776 if (cmdline)
2777 wfree(cmdline);
2778 if (state)
2779 wfree(state);
2780 return 0;
2783 if ((pid = fork()) == 0) {
2784 char **args;
2785 int i;
2787 SetupEnvironment(scr);
2789 #ifdef HAVE_SETSID
2790 setsid();
2791 #endif
2793 args = malloc(sizeof(char *) * (argc + 1));
2794 if (!args)
2795 exit(111);
2796 for (i = 0; i < argc; i++) {
2797 args[i] = argv[i];
2799 args[argc] = NULL;
2800 execvp(argv[0], args);
2801 exit(111);
2803 wtokenfree(argv, argc);
2805 if (pid > 0) {
2806 if (!state) {
2807 state = wmalloc(sizeof(WSavedState));
2808 memset(state, 0, sizeof(WSavedState));
2809 state->hidden = -1;
2810 state->miniaturized = -1;
2811 state->shaded = -1;
2812 if (btn->dock == scr->dock || btn->omnipresent)
2813 state->workspace = -1;
2814 else
2815 state->workspace = scr->current_workspace;
2817 wWindowAddSavedState(btn->wm_instance, btn->wm_class, cmdline, pid, state);
2818 wAddDeathHandler(pid, (WDeathHandler *) trackDeadProcess, btn->dock);
2819 } else if (state) {
2820 wfree(state);
2822 wfree(cmdline);
2823 return pid;
2826 void wDockHideIcons(WDock * dock)
2828 int i;
2830 if (dock == NULL)
2831 return;
2833 for (i = 1; i < dock->max_icons; i++) {
2834 if (dock->icon_array[i])
2835 XUnmapWindow(dpy, dock->icon_array[i]->icon->core->window);
2837 dock->mapped = 0;
2839 dockIconPaint(dock->icon_array[0]);
2842 void wDockShowIcons(WDock * dock)
2844 int i, newlevel;
2845 WAppIcon *btn;
2847 if (dock == NULL)
2848 return;
2850 btn = dock->icon_array[0];
2851 moveDock(dock, btn->x_pos, btn->y_pos);
2853 newlevel = dock->lowered ? WMNormalLevel : WMDockLevel;
2854 ChangeStackingLevel(btn->icon->core, newlevel);
2856 for (i = 1; i < dock->max_icons; i++) {
2857 if (dock->icon_array[i]) {
2858 MoveInStackListAbove(dock->icon_array[i]->icon->core, btn->icon->core);
2859 break;
2863 if (!dock->collapsed) {
2864 for (i = 1; i < dock->max_icons; i++) {
2865 if (dock->icon_array[i]) {
2866 XMapWindow(dpy, dock->icon_array[i]->icon->core->window);
2870 dock->mapped = 1;
2872 dockIconPaint(btn);
2875 void wDockLower(WDock * dock)
2877 int i;
2879 for (i = 0; i < dock->max_icons; i++) {
2880 if (dock->icon_array[i])
2881 wLowerFrame(dock->icon_array[i]->icon->core);
2885 void wDockRaise(WDock * dock)
2887 int i;
2889 for (i = dock->max_icons - 1; i >= 0; i--) {
2890 if (dock->icon_array[i])
2891 wRaiseFrame(dock->icon_array[i]->icon->core);
2895 void wDockRaiseLower(WDock * dock)
2897 if (!dock->icon_array[0]->icon->core->stacking->above
2898 || (dock->icon_array[0]->icon->core->stacking->window_level
2899 != dock->icon_array[0]->icon->core->stacking->above->stacking->window_level))
2900 wDockLower(dock);
2901 else
2902 wDockRaise(dock);
2905 void wDockFinishLaunch(WDock * dock, WAppIcon * icon)
2907 icon->launching = 0;
2908 icon->relaunching = 0;
2909 dockIconPaint(icon);
2912 WAppIcon *wDockFindIconForWindow(WDock * dock, Window window)
2914 WAppIcon *icon;
2915 int i;
2917 for (i = 0; i < dock->max_icons; i++) {
2918 icon = dock->icon_array[i];
2919 if (icon && icon->main_window == window)
2920 return icon;
2922 return NULL;
2925 void wDockTrackWindowLaunch(WDock * dock, Window window)
2927 WAppIcon *icon;
2928 char *wm_class, *wm_instance;
2929 int i;
2930 Bool firstPass = True;
2931 Bool found = False;
2932 char *command = NULL;
2934 command = GetCommandForWindow(window);
2936 if (!PropGetWMClass(window, &wm_class, &wm_instance) || (!wm_class && !wm_instance)) {
2938 if (command)
2939 wfree(command);
2940 return;
2943 retry:
2944 for (i = 0; i < dock->max_icons; i++) {
2945 icon = dock->icon_array[i];
2946 if (!icon)
2947 continue;
2949 /* app is already attached to icon */
2950 if (icon->main_window == window) {
2951 found = True;
2952 break;
2955 if ((icon->wm_instance || icon->wm_class)
2956 && (icon->launching || !icon->running)) {
2958 if (icon->wm_instance && wm_instance && strcmp(icon->wm_instance, wm_instance) != 0) {
2959 continue;
2961 if (icon->wm_class && wm_class && strcmp(icon->wm_class, wm_class) != 0) {
2962 continue;
2964 if (firstPass && command && strcmp(icon->command, command) != 0) {
2965 continue;
2968 if (!icon->relaunching) {
2969 WApplication *wapp;
2971 /* Possibly an application that was docked with dockit,
2972 * but the user did not update WMState to indicate that
2973 * it was docked by force */
2974 wapp = wApplicationOf(window);
2975 if (!wapp) {
2976 icon->forced_dock = 1;
2977 icon->running = 0;
2979 if (!icon->forced_dock) {
2980 icon->main_window = window;
2983 found = True;
2984 if (!wPreferences.no_animations && !icon->launching &&
2985 !dock->screen_ptr->flags.startup && !dock->collapsed) {
2986 WAppIcon *aicon;
2987 int x0, y0;
2989 icon->launching = 1;
2990 dockIconPaint(icon);
2992 aicon = wAppIconCreateForDock(dock->screen_ptr, NULL,
2993 wm_instance, wm_class, TILE_NORMAL);
2994 /* XXX: can: aicon->icon == NULL ? */
2995 PlaceIcon(dock->screen_ptr, &x0, &y0, wGetHeadForWindow(aicon->icon->owner));
2996 wAppIconMove(aicon, x0, y0);
2997 /* Should this always be lowered? -Dan */
2998 if (dock->lowered)
2999 wLowerFrame(aicon->icon->core);
3000 XMapWindow(dpy, aicon->icon->core->window);
3001 aicon->launching = 1;
3002 wAppIconPaint(aicon);
3003 SlideWindow(aicon->icon->core->window, x0, y0, icon->x_pos, icon->y_pos);
3004 XUnmapWindow(dpy, aicon->icon->core->window);
3005 wAppIconDestroy(aicon);
3007 wDockFinishLaunch(dock, icon);
3008 break;
3012 if (firstPass && !found) {
3013 firstPass = False;
3014 goto retry;
3017 if (command)
3018 wfree(command);
3020 if (wm_class)
3021 XFree(wm_class);
3022 if (wm_instance)
3023 XFree(wm_instance);
3026 void wClipUpdateForWorkspaceChange(WScreen * scr, int workspace)
3028 if (!wPreferences.flags.noclip) {
3029 scr->clip_icon->dock = scr->workspaces[workspace]->clip;
3030 if (scr->current_workspace != workspace) {
3031 WDock *old_clip = scr->workspaces[scr->current_workspace]->clip;
3032 WAppIconChain *chain = scr->global_icons;
3034 while (chain) {
3035 moveIconBetweenDocks(chain->aicon->dock,
3036 scr->workspaces[workspace]->clip,
3037 chain->aicon, chain->aicon->xindex, chain->aicon->yindex);
3038 if (scr->workspaces[workspace]->clip->collapsed)
3039 XUnmapWindow(dpy, chain->aicon->icon->core->window);
3040 chain = chain->next;
3043 wDockHideIcons(old_clip);
3044 if (old_clip->auto_raise_lower) {
3045 if (old_clip->auto_raise_magic) {
3046 WMDeleteTimerHandler(old_clip->auto_raise_magic);
3047 old_clip->auto_raise_magic = NULL;
3049 wDockLower(old_clip);
3051 if (old_clip->auto_collapse) {
3052 if (old_clip->auto_expand_magic) {
3053 WMDeleteTimerHandler(old_clip->auto_expand_magic);
3054 old_clip->auto_expand_magic = NULL;
3056 old_clip->collapsed = 1;
3058 wDockShowIcons(scr->workspaces[workspace]->clip);
3060 if (scr->flags.clip_balloon_mapped)
3061 showClipBalloon(scr->clip_icon->dock, workspace);
3065 static void trackDeadProcess(pid_t pid, unsigned char status, WDock * dock)
3067 WAppIcon *icon;
3068 int i;
3070 for (i = 0; i < dock->max_icons; i++) {
3071 icon = dock->icon_array[i];
3072 if (!icon)
3073 continue;
3075 if (icon->launching && icon->pid == pid) {
3076 if (!icon->relaunching) {
3077 icon->running = 0;
3078 icon->main_window = None;
3080 wDockFinishLaunch(dock, icon);
3081 icon->pid = 0;
3082 if (status == 111) {
3083 char msg[PATH_MAX];
3084 char *cmd;
3086 #ifdef XDND
3087 if (icon->drop_launch)
3088 cmd = icon->dnd_command;
3089 else
3090 #endif
3091 if (icon->paste_launch)
3092 cmd = icon->paste_command;
3093 else
3094 cmd = icon->command;
3096 snprintf(msg, sizeof(msg), _("Could not execute command \"%s\""), cmd);
3098 wMessageDialog(dock->screen_ptr, _("Error"), msg, _("OK"), NULL, NULL);
3100 break;
3105 static void toggleLowered(WDock * dock)
3107 WAppIcon *tmp;
3108 int newlevel, i;
3110 /* lower/raise Dock */
3111 if (!dock->lowered) {
3112 newlevel = WMNormalLevel;
3113 dock->lowered = 1;
3114 } else {
3115 newlevel = WMDockLevel;
3116 dock->lowered = 0;
3119 for (i = 0; i < dock->max_icons; i++) {
3120 tmp = dock->icon_array[i];
3121 if (!tmp)
3122 continue;
3124 ChangeStackingLevel(tmp->icon->core, newlevel);
3125 if (dock->lowered)
3126 wLowerFrame(tmp->icon->core);
3129 if (dock->type == WM_DOCK)
3130 wScreenUpdateUsableArea(dock->screen_ptr);
3133 static void toggleCollapsed(WDock * dock)
3135 if (dock->collapsed) {
3136 dock->collapsed = 0;
3137 wDockShowIcons(dock);
3138 } else {
3139 dock->collapsed = 1;
3140 wDockHideIcons(dock);
3144 static void openDockMenu(WDock * dock, WAppIcon * aicon, XEvent * event)
3146 WScreen *scr = dock->screen_ptr;
3147 WObjDescriptor *desc;
3148 WMenuEntry *entry;
3149 WApplication *wapp = NULL;
3150 int index = 0;
3151 int x_pos;
3152 int n_selected;
3153 int appIsRunning = aicon->running && aicon->icon && aicon->icon->owner;
3155 if (dock->type == WM_DOCK) {
3156 /* keep on top */
3157 entry = dock->menu->entries[index];
3158 entry->flags.indicator_on = !dock->lowered;
3159 entry->clientdata = dock;
3160 dock->menu->flags.realized = 0;
3161 } else {
3162 /* clip options */
3163 if (scr->clip_options)
3164 updateClipOptionsMenu(scr->clip_options, dock);
3166 n_selected = numberOfSelectedIcons(dock);
3168 /* Rename Workspace */
3169 entry = dock->menu->entries[++index];
3170 if (aicon == scr->clip_icon) {
3171 entry->callback = renameCallback;
3172 entry->clientdata = dock;
3173 entry->flags.indicator = 0;
3174 entry->text = _("Rename Workspace");
3175 } else {
3176 entry->callback = omnipresentCallback;
3177 entry->clientdata = aicon;
3178 if (n_selected > 0) {
3179 entry->flags.indicator = 0;
3180 entry->text = _("Toggle Omnipresent");
3181 } else {
3182 entry->flags.indicator = 1;
3183 entry->flags.indicator_on = aicon->omnipresent;
3184 entry->flags.indicator_type = MI_CHECK;
3185 entry->text = _("Omnipresent");
3189 /* select/unselect icon */
3190 entry = dock->menu->entries[++index];
3191 entry->clientdata = aicon;
3192 entry->flags.indicator_on = aicon->icon->selected;
3193 wMenuSetEnabled(dock->menu, index, aicon != scr->clip_icon);
3195 /* select/unselect all icons */
3196 entry = dock->menu->entries[++index];
3197 entry->clientdata = aicon;
3198 if (n_selected > 0)
3199 entry->text = _("Unselect All Icons");
3200 else
3201 entry->text = _("Select All Icons");
3202 wMenuSetEnabled(dock->menu, index, dock->icon_count > 1);
3204 /* keep icon(s) */
3205 entry = dock->menu->entries[++index];
3206 entry->clientdata = aicon;
3207 if (n_selected > 1)
3208 entry->text = _("Keep Icons");
3209 else
3210 entry->text = _("Keep Icon");
3211 wMenuSetEnabled(dock->menu, index, dock->icon_count > 1);
3213 /* this is the workspace submenu part */
3214 entry = dock->menu->entries[++index];
3215 if (n_selected > 1)
3216 entry->text = _("Move Icons To");
3217 else
3218 entry->text = _("Move Icon To");
3219 if (scr->clip_submenu)
3220 updateWorkspaceMenu(scr->clip_submenu, aicon);
3221 wMenuSetEnabled(dock->menu, index, !aicon->omnipresent);
3223 /* remove icon(s) */
3224 entry = dock->menu->entries[++index];
3225 entry->clientdata = aicon;
3226 if (n_selected > 1)
3227 entry->text = _("Remove Icons");
3228 else
3229 entry->text = _("Remove Icon");
3230 wMenuSetEnabled(dock->menu, index, dock->icon_count > 1);
3232 /* attract icon(s) */
3233 entry = dock->menu->entries[++index];
3234 entry->clientdata = aicon;
3236 dock->menu->flags.realized = 0;
3237 wMenuRealize(dock->menu);
3240 if (aicon->icon->owner) {
3241 wapp = wApplicationOf(aicon->icon->owner->main_window);
3242 } else {
3243 wapp = NULL;
3246 /* launch */
3247 entry = dock->menu->entries[++index];
3248 entry->clientdata = aicon;
3249 wMenuSetEnabled(dock->menu, index, aicon->command != NULL);
3251 /* unhide here */
3252 entry = dock->menu->entries[++index];
3253 entry->clientdata = aicon;
3254 if (wapp && wapp->flags.hidden) {
3255 entry->text = _("Unhide Here");
3256 } else {
3257 entry->text = _("Bring Here");
3259 wMenuSetEnabled(dock->menu, index, appIsRunning);
3261 /* hide */
3262 entry = dock->menu->entries[++index];
3263 entry->clientdata = aicon;
3264 if (wapp && wapp->flags.hidden) {
3265 entry->text = _("Unhide");
3266 } else {
3267 entry->text = _("Hide");
3269 wMenuSetEnabled(dock->menu, index, appIsRunning);
3271 /* settings */
3272 entry = dock->menu->entries[++index];
3273 entry->clientdata = aicon;
3274 wMenuSetEnabled(dock->menu, index, !aicon->editing && !wPreferences.flags.noupdates);
3276 /* kill */
3277 entry = dock->menu->entries[++index];
3278 entry->clientdata = aicon;
3279 wMenuSetEnabled(dock->menu, index, appIsRunning);
3281 if (!dock->menu->flags.realized)
3282 wMenuRealize(dock->menu);
3284 if (dock->type == WM_CLIP) {
3285 /*x_pos = event->xbutton.x_root+2; */
3286 x_pos = event->xbutton.x_root - dock->menu->frame->core->width / 2 - 1;
3287 if (x_pos < 0) {
3288 x_pos = 0;
3289 } else if (x_pos + dock->menu->frame->core->width > scr->scr_width - 2) {
3290 x_pos = scr->scr_width - dock->menu->frame->core->width - 4;
3292 } else {
3293 x_pos = dock->on_right_side ? scr->scr_width - dock->menu->frame->core->width - 3 : 0;
3296 wMenuMapAt(dock->menu, x_pos, event->xbutton.y_root + 2, False);
3298 /* allow drag select */
3299 event->xany.send_event = True;
3300 desc = &dock->menu->menu->descriptor;
3301 (*desc->handle_mousedown) (desc, event);
3304 /******************************************************************/
3305 static void iconDblClick(WObjDescriptor * desc, XEvent * event)
3307 WAppIcon *btn = desc->parent;
3308 WDock *dock = btn->dock;
3309 WApplication *wapp = NULL;
3310 int unhideHere = 0;
3312 if (btn->icon->owner && !(event->xbutton.state & ControlMask)) {
3313 wapp = wApplicationOf(btn->icon->owner->main_window);
3315 assert(wapp != NULL);
3317 unhideHere = (event->xbutton.state & ShiftMask);
3319 /* go to the last workspace that the user worked on the app */
3320 if (wapp->last_workspace != dock->screen_ptr->current_workspace && !unhideHere) {
3321 wWorkspaceChange(dock->screen_ptr, wapp->last_workspace);
3324 wUnhideApplication(wapp, event->xbutton.button == Button2, unhideHere);
3326 if (event->xbutton.state & MOD_MASK) {
3327 wHideOtherApplications(btn->icon->owner);
3329 } else {
3330 if (event->xbutton.button == Button1) {
3332 if (event->xbutton.state & MOD_MASK) {
3333 /* raise/lower dock */
3334 toggleLowered(dock);
3335 } else if (btn == dock->screen_ptr->clip_icon) {
3336 if (getClipButton(event->xbutton.x, event->xbutton.y) == CLIP_IDLE)
3337 toggleCollapsed(dock);
3338 else
3339 handleClipChangeWorkspace(dock->screen_ptr, event);
3340 } else if (btn->command) {
3341 if (!btn->launching && (!btn->running || (event->xbutton.state & ControlMask))) {
3342 launchDockedApplication(btn, False);
3344 } else if (btn->xindex == 0 && btn->yindex == 0 && btn->dock->type == WM_DOCK) {
3345 wShowGNUstepPanel(dock->screen_ptr);
3351 static void handleDockMove(WDock * dock, WAppIcon * aicon, XEvent * event)
3353 WScreen *scr = dock->screen_ptr;
3354 int ofs_x = event->xbutton.x, ofs_y = event->xbutton.y;
3355 int x, y;
3356 XEvent ev;
3357 int grabbed = 0, swapped = 0, done;
3358 Pixmap ghost = None;
3359 int superfluous = wPreferences.superfluous; /* we catch it to avoid problems */
3361 #ifdef DEBUG
3362 puts("moving dock");
3363 #endif
3364 if (XGrabPointer(dpy, aicon->icon->core->window, True, ButtonMotionMask
3365 | ButtonReleaseMask | ButtonPressMask, GrabModeAsync,
3366 GrabModeAsync, None, None, CurrentTime) != GrabSuccess) {
3367 wwarning("pointer grab failed for dock move");
3369 y = 0;
3370 for (x = 0; x < dock->max_icons; x++) {
3371 if (dock->icon_array[x] != NULL && dock->icon_array[x]->yindex > y)
3372 y = dock->icon_array[x]->yindex;
3374 y++;
3375 XResizeWindow(dpy, scr->dock_shadow, ICON_SIZE, ICON_SIZE * y);
3377 done = 0;
3378 while (!done) {
3379 WMMaskEvent(dpy, PointerMotionMask | ButtonReleaseMask | ButtonPressMask
3380 | ButtonMotionMask | ExposureMask | EnterWindowMask, &ev);
3381 switch (ev.type) {
3382 case Expose:
3383 WMHandleEvent(&ev);
3384 break;
3386 case EnterNotify:
3387 /* It means the cursor moved so fast that it entered
3388 * something else (if moving slowly, it would have
3389 * stayed in the dock that is being moved. Ignore such
3390 * "spurious" EnterNotifiy's */
3391 break;
3393 case MotionNotify:
3394 if (!grabbed) {
3395 if (abs(ofs_x - ev.xmotion.x) >= MOVE_THRESHOLD
3396 || abs(ofs_y - ev.xmotion.y) >= MOVE_THRESHOLD) {
3397 XChangeActivePointerGrab(dpy, ButtonMotionMask
3398 | ButtonReleaseMask | ButtonPressMask,
3399 wCursor[WCUR_MOVE], CurrentTime);
3400 grabbed = 1;
3402 break;
3404 if (dock->type == WM_CLIP) {
3405 x = ev.xmotion.x_root - ofs_x;
3406 y = ev.xmotion.y_root - ofs_y;
3407 wScreenKeepInside(scr, &x, &y, ICON_SIZE, ICON_SIZE);
3409 moveDock(dock, x, y);
3410 } else {
3411 /* move vertically if pointer is inside the dock */
3412 if ((dock->on_right_side && ev.xmotion.x_root >= dock->x_pos - ICON_SIZE)
3413 || (!dock->on_right_side && ev.xmotion.x_root <= dock->x_pos + ICON_SIZE * 2)) {
3415 x = ev.xmotion.x_root - ofs_x;
3416 y = ev.xmotion.y_root - ofs_y;
3417 wScreenKeepInside(scr, &x, &y, ICON_SIZE, ICON_SIZE);
3418 moveDock(dock, dock->x_pos, y);
3420 /* move horizontally to change sides */
3421 x = ev.xmotion.x_root - ofs_x;
3422 if (!dock->on_right_side) {
3424 /* is on left */
3426 if (ev.xmotion.x_root > dock->x_pos + ICON_SIZE * 2) {
3427 XMoveWindow(dpy, scr->dock_shadow, scr->scr_width - ICON_SIZE
3428 - DOCK_EXTRA_SPACE - 1, dock->y_pos);
3429 if (superfluous && ghost == None) {
3430 ghost = MakeGhostDock(dock, dock->x_pos,
3431 scr->scr_width - ICON_SIZE
3432 - DOCK_EXTRA_SPACE - 1, dock->y_pos);
3433 XSetWindowBackgroundPixmap(dpy, scr->dock_shadow, ghost);
3434 XClearWindow(dpy, scr->dock_shadow);
3436 XMapRaised(dpy, scr->dock_shadow);
3437 swapped = 1;
3438 } else {
3439 if (superfluous && ghost != None) {
3440 XFreePixmap(dpy, ghost);
3441 ghost = None;
3443 XUnmapWindow(dpy, scr->dock_shadow);
3444 swapped = 0;
3446 } else {
3447 /* is on right */
3448 if (ev.xmotion.x_root < dock->x_pos - ICON_SIZE) {
3449 XMoveWindow(dpy, scr->dock_shadow, DOCK_EXTRA_SPACE, dock->y_pos);
3450 if (superfluous && ghost == None) {
3451 ghost = MakeGhostDock(dock, dock->x_pos,
3452 DOCK_EXTRA_SPACE, dock->y_pos);
3453 XSetWindowBackgroundPixmap(dpy, scr->dock_shadow, ghost);
3454 XClearWindow(dpy, scr->dock_shadow);
3456 XMapRaised(dpy, scr->dock_shadow);
3457 swapped = -1;
3458 } else {
3459 XUnmapWindow(dpy, scr->dock_shadow);
3460 swapped = 0;
3461 if (superfluous && ghost != None) {
3462 XFreePixmap(dpy, ghost);
3463 ghost = None;
3468 break;
3470 case ButtonPress:
3471 break;
3473 case ButtonRelease:
3474 if (ev.xbutton.button != event->xbutton.button)
3475 break;
3476 XUngrabPointer(dpy, CurrentTime);
3477 XUnmapWindow(dpy, scr->dock_shadow);
3478 XResizeWindow(dpy, scr->dock_shadow, ICON_SIZE, ICON_SIZE);
3479 if (dock->type == WM_DOCK) {
3480 if (swapped != 0) {
3481 if (swapped > 0)
3482 dock->on_right_side = 1;
3483 else
3484 dock->on_right_side = 0;
3485 swapDock(dock);
3486 wArrangeIcons(scr, False);
3489 done = 1;
3490 break;
3493 if (superfluous) {
3494 if (ghost != None)
3495 XFreePixmap(dpy, ghost);
3496 XSetWindowBackground(dpy, scr->dock_shadow, scr->white_pixel);
3498 #ifdef DEBUG
3499 puts("End dock move");
3500 #endif
3503 static Bool handleIconMove(WDock *dock, WAppIcon *aicon, XEvent *event)
3505 WScreen *scr = dock->screen_ptr;
3506 Window wins[2];
3507 WIcon *icon = aicon->icon;
3508 WDock *dock2 = NULL, *last_dock = dock, *clip = NULL;
3509 int ondock, grabbed = 0, change_dock = 0, collapsed = 0;
3510 XEvent ev;
3511 int x = aicon->x_pos, y = aicon->y_pos;
3512 int ofs_x = event->xbutton.x, ofs_y = event->xbutton.y;
3513 int shad_x = x, shad_y = y;
3514 int ix = aicon->xindex, iy = aicon->yindex;
3515 int tmp;
3516 Pixmap ghost = None;
3517 Bool docked;
3518 int superfluous = wPreferences.superfluous; /* we catch it to avoid problems */
3519 int omnipresent = aicon->omnipresent; /* this must be cached!!! */
3520 Bool hasMoved = False;
3522 if (wPreferences.flags.noupdates)
3523 return;
3525 if (XGrabPointer(dpy, icon->core->window, True, ButtonMotionMask
3526 | ButtonReleaseMask | ButtonPressMask, GrabModeAsync,
3527 GrabModeAsync, None, None, CurrentTime) != GrabSuccess) {
3528 #ifdef DEBUG0
3529 wwarning("pointer grab failed for icon move");
3530 #endif
3533 if (!(event->xbutton.state & MOD_MASK))
3534 wRaiseFrame(icon->core);
3536 if (!wPreferences.flags.noclip)
3537 clip = scr->workspaces[scr->current_workspace]->clip;
3539 if (dock == scr->dock && !wPreferences.flags.noclip)
3540 dock2 = clip;
3541 else if (dock != scr->dock && !wPreferences.flags.nodock)
3542 dock2 = scr->dock;
3544 wins[0] = icon->core->window;
3545 wins[1] = scr->dock_shadow;
3546 XRestackWindows(dpy, wins, 2);
3547 XMoveResizeWindow(dpy, scr->dock_shadow, aicon->x_pos, aicon->y_pos, ICON_SIZE, ICON_SIZE);
3548 if (superfluous) {
3549 if (icon->pixmap != None)
3550 ghost = MakeGhostIcon(scr, icon->pixmap);
3551 else
3552 ghost = MakeGhostIcon(scr, icon->core->window);
3554 XSetWindowBackgroundPixmap(dpy, scr->dock_shadow, ghost);
3555 XClearWindow(dpy, scr->dock_shadow);
3557 XMapWindow(dpy, scr->dock_shadow);
3559 ondock = 1;
3561 while (1) {
3562 XMaskEvent(dpy, PointerMotionMask | ButtonReleaseMask | ButtonPressMask
3563 | ButtonMotionMask | ExposureMask, &ev);
3564 switch (ev.type) {
3565 case Expose:
3566 WMHandleEvent(&ev);
3567 break;
3569 case MotionNotify:
3570 hasMoved = True;
3571 if (!grabbed) {
3572 if (abs(ofs_x - ev.xmotion.x) >= MOVE_THRESHOLD
3573 || abs(ofs_y - ev.xmotion.y) >= MOVE_THRESHOLD) {
3574 XChangeActivePointerGrab(dpy, ButtonMotionMask
3575 | ButtonReleaseMask | ButtonPressMask,
3576 wCursor[WCUR_MOVE], CurrentTime);
3577 grabbed = 1;
3578 } else {
3579 break;
3583 if (omnipresent) {
3584 int i;
3585 for (i = 0; i < scr->workspace_count; i++) {
3586 if (i == scr->current_workspace)
3587 continue;
3588 wDockShowIcons(scr->workspaces[i]->clip);
3592 x = ev.xmotion.x_root - ofs_x;
3593 y = ev.xmotion.y_root - ofs_y;
3594 tmp = wDockSnapIcon(dock, aicon, x, y, &ix, &iy, True);
3595 if (tmp && dock2) {
3596 change_dock = 0;
3597 if (last_dock != dock && collapsed) {
3598 last_dock->collapsed = 1;
3599 wDockHideIcons(last_dock);
3600 collapsed = 0;
3602 if (!collapsed && (collapsed = dock->collapsed)) {
3603 dock->collapsed = 0;
3604 wDockShowIcons(dock);
3606 if (dock->auto_raise_lower)
3607 wDockRaise(dock);
3608 last_dock = dock;
3609 } else if (dock2) {
3610 tmp = wDockSnapIcon(dock2, aicon, x, y, &ix, &iy, False);
3611 if (tmp) {
3612 change_dock = 1;
3613 if (last_dock != dock2 && collapsed) {
3614 last_dock->collapsed = 1;
3615 wDockHideIcons(last_dock);
3616 collapsed = 0;
3618 if (!collapsed && (collapsed = dock2->collapsed)) {
3619 dock2->collapsed = 0;
3620 wDockShowIcons(dock2);
3622 if (dock2->auto_raise_lower)
3623 wDockRaise(dock2);
3624 last_dock = dock2;
3627 if (aicon->launching || aicon->lock || (aicon->running && !(ev.xmotion.state & MOD_MASK))
3628 || (!aicon->running && tmp)) {
3629 shad_x = last_dock->x_pos + ix * wPreferences.icon_size;
3630 shad_y = last_dock->y_pos + iy * wPreferences.icon_size;
3632 XMoveWindow(dpy, scr->dock_shadow, shad_x, shad_y);
3634 if (!ondock) {
3635 XMapWindow(dpy, scr->dock_shadow);
3637 ondock = 1;
3638 } else {
3639 if (ondock) {
3640 XUnmapWindow(dpy, scr->dock_shadow);
3642 ondock = 0;
3644 XMoveWindow(dpy, icon->core->window, x, y);
3645 break;
3647 case ButtonPress:
3648 break;
3650 case ButtonRelease:
3651 if (ev.xbutton.button != event->xbutton.button)
3652 break;
3653 XUngrabPointer(dpy, CurrentTime);
3654 if (ondock) {
3655 SlideWindow(icon->core->window, x, y, shad_x, shad_y);
3656 XUnmapWindow(dpy, scr->dock_shadow);
3657 if (!change_dock) {
3658 reattachIcon(dock, aicon, ix, iy);
3659 if (clip && dock != clip && clip->auto_raise_lower)
3660 wDockLower(clip);
3661 } else {
3662 docked = moveIconBetweenDocks(dock, dock2, aicon, ix, iy);
3663 if (!docked) {
3664 /* Slide it back if dock rejected it */
3665 SlideWindow(icon->core->window, x, y, aicon->x_pos, aicon->y_pos);
3666 reattachIcon(dock, aicon, aicon->xindex, aicon->yindex);
3668 if (last_dock->type == WM_CLIP && last_dock->auto_collapse) {
3669 collapsed = 0;
3672 } else {
3673 aicon->x_pos = x;
3674 aicon->y_pos = y;
3675 if (superfluous) {
3676 if (!aicon->running && !wPreferences.no_animations) {
3677 /* We need to deselect it, even if is deselected in
3678 * wDockDetach(), because else DoKaboom() will fail.
3680 if (aicon->icon->selected)
3681 wIconSelect(aicon->icon);
3683 DoKaboom(scr, aicon->icon->core->window, x, y);
3686 if (clip && clip->auto_raise_lower)
3687 wDockLower(clip);
3688 wDockDetach(dock, aicon);
3690 if (collapsed) {
3691 last_dock->collapsed = 1;
3692 wDockHideIcons(last_dock);
3693 collapsed = 0;
3695 if (superfluous) {
3696 if (ghost != None)
3697 XFreePixmap(dpy, ghost);
3698 XSetWindowBackground(dpy, scr->dock_shadow, scr->white_pixel);
3700 if (omnipresent) {
3701 int i;
3702 for (i = 0; i < scr->workspace_count; i++) {
3703 if (i == scr->current_workspace)
3704 continue;
3705 wDockHideIcons(scr->workspaces[i]->clip);
3708 #ifdef DEBUG
3709 puts("End icon move");
3710 #endif
3711 return hasMoved;;
3714 return False; /* never reached */
3717 static int getClipButton(int px, int py)
3719 int pt = (CLIP_BUTTON_SIZE + 2) * ICON_SIZE / 64;
3721 if (px < 0 || py < 0 || px >= ICON_SIZE || py >= ICON_SIZE)
3722 return CLIP_IDLE;
3724 if (py <= pt - ((int)ICON_SIZE - 1 - px))
3725 return CLIP_FORWARD;
3726 else if (px <= pt - ((int)ICON_SIZE - 1 - py))
3727 return CLIP_REWIND;
3729 return CLIP_IDLE;
3732 static void handleClipChangeWorkspace(WScreen * scr, XEvent * event)
3734 XEvent ev;
3735 int done, direction, new_ws;
3736 int new_dir;
3737 WDock *clip = scr->clip_icon->dock;
3739 direction = getClipButton(event->xbutton.x, event->xbutton.y);
3741 clip->lclip_button_pushed = direction == CLIP_REWIND;
3742 clip->rclip_button_pushed = direction == CLIP_FORWARD;
3744 wClipIconPaint(scr->clip_icon);
3745 done = 0;
3746 while (!done) {
3747 WMMaskEvent(dpy, ExposureMask | ButtonMotionMask | ButtonReleaseMask | ButtonPressMask, &ev);
3748 switch (ev.type) {
3749 case Expose:
3750 WMHandleEvent(&ev);
3751 break;
3753 case MotionNotify:
3754 new_dir = getClipButton(ev.xmotion.x, ev.xmotion.y);
3755 if (new_dir != direction) {
3756 direction = new_dir;
3757 clip->lclip_button_pushed = direction == CLIP_REWIND;
3758 clip->rclip_button_pushed = direction == CLIP_FORWARD;
3759 wClipIconPaint(scr->clip_icon);
3761 break;
3763 case ButtonPress:
3764 break;
3766 case ButtonRelease:
3767 if (ev.xbutton.button == event->xbutton.button)
3768 done = 1;
3772 clip->lclip_button_pushed = 0;
3773 clip->rclip_button_pushed = 0;
3775 new_ws = wPreferences.ws_advance || (event->xbutton.state & ControlMask);
3777 if (direction == CLIP_FORWARD) {
3778 if (scr->current_workspace < scr->workspace_count - 1)
3779 wWorkspaceChange(scr, scr->current_workspace + 1);
3780 else if (new_ws && scr->current_workspace < MAX_WORKSPACES - 1)
3781 wWorkspaceChange(scr, scr->current_workspace + 1);
3782 else if (wPreferences.ws_cycle)
3783 wWorkspaceChange(scr, 0);
3784 } else if (direction == CLIP_REWIND) {
3785 if (scr->current_workspace > 0)
3786 wWorkspaceChange(scr, scr->current_workspace - 1);
3787 else if (scr->current_workspace == 0 && wPreferences.ws_cycle)
3788 wWorkspaceChange(scr, scr->workspace_count - 1);
3791 wClipIconPaint(scr->clip_icon);
3794 static void iconMouseDown(WObjDescriptor * desc, XEvent * event)
3796 WAppIcon *aicon = desc->parent;
3797 WDock *dock = aicon->dock;
3798 WScreen *scr = aicon->icon->core->screen_ptr;
3800 if (aicon->editing || WCHECK_STATE(WSTATE_MODAL))
3801 return;
3803 scr->last_dock = dock;
3805 if (dock->menu->flags.mapped)
3806 wMenuUnmap(dock->menu);
3808 if (IsDoubleClick(scr, event)) {
3809 /* double-click was not in the main clip icon */
3810 if (dock->type != WM_CLIP || aicon->xindex != 0 || aicon->yindex != 0
3811 || getClipButton(event->xbutton.x, event->xbutton.y) == CLIP_IDLE) {
3812 iconDblClick(desc, event);
3813 return;
3817 if (dock->type == WM_CLIP && scr->flags.clip_balloon_mapped) {
3818 XUnmapWindow(dpy, scr->clip_balloon);
3819 scr->flags.clip_balloon_mapped = 0;
3821 #ifdef DEBUG
3822 puts("handling dock");
3823 #endif
3824 if (event->xbutton.button == Button1) {
3825 if (event->xbutton.state & MOD_MASK)
3826 wDockLower(dock);
3827 else
3828 wDockRaise(dock);
3830 if ((event->xbutton.state & ShiftMask) && aicon != scr->clip_icon && dock->type != WM_DOCK) {
3831 wIconSelect(aicon->icon);
3832 return;
3835 if (aicon->yindex == 0 && aicon->xindex == 0) {
3836 if (getClipButton(event->xbutton.x, event->xbutton.y) != CLIP_IDLE
3837 && dock->type == WM_CLIP)
3838 handleClipChangeWorkspace(scr, event);
3839 else
3840 handleDockMove(dock, aicon, event);
3841 } else {
3842 Bool hasMoved = handleIconMove(dock, aicon, event);
3843 if (wPreferences.single_click && !hasMoved)
3844 iconDblClick(desc, event);
3846 } else if (event->xbutton.button == Button2 && dock->type == WM_CLIP && aicon == scr->clip_icon) {
3847 if (!scr->clip_ws_menu) {
3848 scr->clip_ws_menu = wWorkspaceMenuMake(scr, False);
3850 if (scr->clip_ws_menu) {
3851 WMenu *wsMenu = scr->clip_ws_menu;
3852 int xpos;
3854 wWorkspaceMenuUpdate(scr, wsMenu);
3856 xpos = event->xbutton.x_root - wsMenu->frame->core->width / 2 - 1;
3857 if (xpos < 0) {
3858 xpos = 0;
3859 } else if (xpos + wsMenu->frame->core->width > scr->scr_width - 2) {
3860 xpos = scr->scr_width - wsMenu->frame->core->width - 4;
3862 wMenuMapAt(wsMenu, xpos, event->xbutton.y_root + 2, False);
3864 desc = &wsMenu->menu->descriptor;
3865 event->xany.send_event = True;
3866 (*desc->handle_mousedown) (desc, event);
3868 } else if (event->xbutton.button == Button2 && dock->type == WM_CLIP &&
3869 (event->xbutton.state & ShiftMask) && aicon != scr->clip_icon) {
3870 wClipMakeIconOmnipresent(aicon, !aicon->omnipresent);
3871 } else if (event->xbutton.button == Button3) {
3872 if (event->xbutton.send_event &&
3873 XGrabPointer(dpy, aicon->icon->core->window, True, ButtonMotionMask
3874 | ButtonReleaseMask | ButtonPressMask, GrabModeAsync,
3875 GrabModeAsync, None, None, CurrentTime) != GrabSuccess) {
3876 wwarning("pointer grab failed for dockicon menu");
3877 return;
3880 openDockMenu(dock, aicon, event);
3881 } else if (event->xbutton.button == Button2) {
3882 WAppIcon *btn = desc->parent;
3884 if (!btn->launching && (!btn->running || (event->xbutton.state & ControlMask))) {
3885 launchDockedApplication(btn, True);
3890 static void showClipBalloon(WDock * dock, int workspace)
3892 int w, h;
3893 int x, y;
3894 WScreen *scr = dock->screen_ptr;
3895 char *text;
3896 Window stack[2];
3898 scr->flags.clip_balloon_mapped = 1;
3899 XMapWindow(dpy, scr->clip_balloon);
3901 text = scr->workspaces[workspace]->name;
3903 w = WMWidthOfString(scr->clip_title_font, text, strlen(text));
3905 h = WMFontHeight(scr->clip_title_font);
3906 XResizeWindow(dpy, scr->clip_balloon, w, h);
3908 x = dock->x_pos + CLIP_BUTTON_SIZE * ICON_SIZE / 64;
3909 y = dock->y_pos + ICON_SIZE - WMFontHeight(scr->clip_title_font) - 3;
3911 if (x + w > scr->scr_width) {
3912 x = scr->scr_width - w;
3913 if (dock->y_pos + ICON_SIZE + h > scr->scr_height)
3914 y = dock->y_pos - h - 1;
3915 else
3916 y = dock->y_pos + ICON_SIZE;
3917 XRaiseWindow(dpy, scr->clip_balloon);
3918 } else {
3919 stack[0] = scr->clip_icon->icon->core->window;
3920 stack[1] = scr->clip_balloon;
3921 XRestackWindows(dpy, stack, 2);
3923 XMoveWindow(dpy, scr->clip_balloon, x, y);
3924 XClearWindow(dpy, scr->clip_balloon);
3925 WMDrawString(scr->wmscreen, scr->clip_balloon,
3926 scr->clip_title_color[CLIP_NORMAL], scr->clip_title_font, 0, 0, text, strlen(text));
3929 static void clipEnterNotify(WObjDescriptor * desc, XEvent * event)
3931 WAppIcon *btn = (WAppIcon *) desc->parent;
3932 WDock *dock;
3933 WScreen *scr;
3935 assert(event->type == EnterNotify);
3937 if (desc->parent_type != WCLASS_DOCK_ICON)
3938 return;
3940 scr = btn->icon->core->screen_ptr;
3941 if (!btn->omnipresent)
3942 dock = btn->dock;
3943 else
3944 dock = scr->workspaces[scr->current_workspace]->clip;
3946 if (!dock || dock->type != WM_CLIP)
3947 return;
3949 /* The auto raise/lower code */
3950 if (dock->auto_lower_magic) {
3951 WMDeleteTimerHandler(dock->auto_lower_magic);
3952 dock->auto_lower_magic = NULL;
3954 if (dock->auto_raise_lower && !dock->auto_raise_magic) {
3955 dock->auto_raise_magic = WMAddTimerHandler(AUTO_RAISE_DELAY, clipAutoRaise, (void *)dock);
3958 /* The auto expand/collapse code */
3959 if (dock->auto_collapse_magic) {
3960 WMDeleteTimerHandler(dock->auto_collapse_magic);
3961 dock->auto_collapse_magic = NULL;
3963 if (dock->auto_collapse && !dock->auto_expand_magic) {
3964 dock->auto_expand_magic = WMAddTimerHandler(AUTO_EXPAND_DELAY, clipAutoExpand, (void *)dock);
3967 if (btn->xindex == 0 && btn->yindex == 0)
3968 showClipBalloon(dock, dock->screen_ptr->current_workspace);
3969 else {
3970 if (dock->screen_ptr->flags.clip_balloon_mapped) {
3971 XUnmapWindow(dpy, dock->screen_ptr->clip_balloon);
3972 dock->screen_ptr->flags.clip_balloon_mapped = 0;
3977 static void clipLeave(WDock * dock)
3979 XEvent event;
3980 WObjDescriptor *desc = NULL;
3982 if (!dock || dock->type != WM_CLIP)
3983 return;
3985 if (XCheckTypedEvent(dpy, EnterNotify, &event) != False) {
3986 if (XFindContext(dpy, event.xcrossing.window, wWinContext,
3987 (XPointer *) & desc) != XCNOENT
3988 && desc && desc->parent_type == WCLASS_DOCK_ICON
3989 && ((WAppIcon *) desc->parent)->dock && ((WAppIcon *) desc->parent)->dock->type == WM_CLIP) {
3990 /* We didn't left the Clip yet */
3991 XPutBackEvent(dpy, &event);
3992 return;
3995 XPutBackEvent(dpy, &event);
3996 } else {
3997 /* We entered a withdrawn window, so we're still in Clip */
3998 return;
4001 if (dock->auto_raise_magic) {
4002 WMDeleteTimerHandler(dock->auto_raise_magic);
4003 dock->auto_raise_magic = NULL;
4005 if (dock->auto_raise_lower && !dock->auto_lower_magic) {
4006 dock->auto_lower_magic = WMAddTimerHandler(AUTO_LOWER_DELAY, clipAutoLower, (void *)dock);
4009 if (dock->auto_expand_magic) {
4010 WMDeleteTimerHandler(dock->auto_expand_magic);
4011 dock->auto_expand_magic = NULL;
4013 if (dock->auto_collapse && !dock->auto_collapse_magic) {
4014 dock->auto_collapse_magic = WMAddTimerHandler(AUTO_COLLAPSE_DELAY, clipAutoCollapse, (void *)dock);
4018 static void clipLeaveNotify(WObjDescriptor * desc, XEvent * event)
4020 WAppIcon *btn = (WAppIcon *) desc->parent;
4022 assert(event->type == LeaveNotify);
4024 if (desc->parent_type != WCLASS_DOCK_ICON)
4025 return;
4027 clipLeave(btn->dock);
4030 static void clipAutoCollapse(void *cdata)
4032 WDock *dock = (WDock *) cdata;
4034 if (dock->type != WM_CLIP)
4035 return;
4037 if (dock->auto_collapse) {
4038 dock->collapsed = 1;
4039 wDockHideIcons(dock);
4041 dock->auto_collapse_magic = NULL;
4044 static void clipAutoExpand(void *cdata)
4046 WDock *dock = (WDock *) cdata;
4048 if (dock->type != WM_CLIP)
4049 return;
4051 if (dock->auto_collapse) {
4052 dock->collapsed = 0;
4053 wDockShowIcons(dock);
4055 dock->auto_expand_magic = NULL;
4058 static void clipAutoLower(void *cdata)
4060 WDock *dock = (WDock *) cdata;
4062 if (dock->type != WM_CLIP)
4063 return;
4065 if (dock->auto_raise_lower)
4066 wDockLower(dock);
4068 dock->auto_lower_magic = NULL;
4071 static void clipAutoRaise(void *cdata)
4073 WDock *dock = (WDock *) cdata;
4075 if (dock->type != WM_CLIP)
4076 return;
4078 if (dock->auto_raise_lower)
4079 wDockRaise(dock);
4081 if (dock->screen_ptr->flags.clip_balloon_mapped) {
4082 showClipBalloon(dock, dock->screen_ptr->current_workspace);
4085 dock->auto_raise_magic = NULL;
4088 static Bool iconCanBeOmnipresent(WAppIcon * aicon)
4090 WScreen *scr = aicon->icon->core->screen_ptr;
4091 WDock *clip;
4092 WAppIcon *btn;
4093 int i, j;
4095 for (i = 0; i < scr->workspace_count; i++) {
4096 clip = scr->workspaces[i]->clip;
4098 if (clip == aicon->dock)
4099 continue;
4101 if (clip->icon_count + scr->global_icon_count >= clip->max_icons)
4102 return False; /* Clip is full in some workspace */
4104 for (j = 0; j < clip->max_icons; j++) {
4105 btn = clip->icon_array[j];
4106 if (btn && btn->xindex == aicon->xindex && btn->yindex == aicon->yindex)
4107 return False;
4111 return True;
4114 int wClipMakeIconOmnipresent(WAppIcon * aicon, int omnipresent)
4116 WScreen *scr = aicon->icon->core->screen_ptr;
4117 WAppIconChain *new_entry, *tmp, *tmp1;
4118 int status = WO_SUCCESS;
4120 if ((scr->dock && aicon->dock == scr->dock) || aicon == scr->clip_icon) {
4121 return WO_NOT_APPLICABLE;
4124 if (aicon->omnipresent == omnipresent)
4125 return WO_SUCCESS;
4127 if (omnipresent) {
4128 if (iconCanBeOmnipresent(aicon)) {
4129 aicon->omnipresent = 1;
4130 new_entry = wmalloc(sizeof(WAppIconChain));
4131 new_entry->aicon = aicon;
4132 new_entry->next = scr->global_icons;
4133 scr->global_icons = new_entry;
4134 scr->global_icon_count++;
4135 } else {
4136 aicon->omnipresent = 0;
4137 status = WO_FAILED;
4139 } else {
4140 aicon->omnipresent = 0;
4141 if (aicon == scr->global_icons->aicon) {
4142 tmp = scr->global_icons->next;
4143 wfree(scr->global_icons);
4144 scr->global_icons = tmp;
4145 scr->global_icon_count--;
4146 } else {
4147 tmp = scr->global_icons;
4148 while (tmp->next) {
4149 if (tmp->next->aicon == aicon) {
4150 tmp1 = tmp->next->next;
4151 wfree(tmp->next);
4152 tmp->next = tmp1;
4153 scr->global_icon_count--;
4154 break;
4156 tmp = tmp->next;
4161 wAppIconPaint(aicon);
4163 return status;