Don't save app settings proplist when nothing changed
[wmaker-crm.git] / src / dock.c
blob736823cc204052d9194620594b53c6d9cbe24543
1 /* dock.c- built-in Dock module for WindowMaker
3 * Window Maker window manager
5 * Copyright (c) 1997-2003 Alfredo K. Kojima
6 * Copyright (c) 1998-2003 Dan Pascu
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
23 #include "wconfig.h"
25 #include <X11/Xlib.h>
26 #include <X11/Xutil.h>
27 #include <stdlib.h>
28 #include <string.h>
29 #include <unistd.h>
30 #include <math.h>
31 #include <limits.h>
33 #ifndef PATH_MAX
34 #define PATH_MAX DEFAULT_PATH_MAX
35 #endif
37 #include "WindowMaker.h"
38 #include "wcore.h"
39 #include "window.h"
40 #include "icon.h"
41 #include "appicon.h"
42 #include "actions.h"
43 #include "stacking.h"
44 #include "dock.h"
45 #include "dialog.h"
46 #include "funcs.h"
47 #include "properties.h"
48 #include "menu.h"
49 #include "client.h"
50 #include "defaults.h"
51 #include "workspace.h"
52 #include "framewin.h"
53 #include "superfluous.h"
54 #include "xinerama.h"
56 /**** Local variables ****/
57 #define CLIP_REWIND 1
58 #define CLIP_IDLE 0
59 #define CLIP_FORWARD 2
61 /**** Global variables ****/
63 /* in dockedapp.c */
64 extern void DestroyDockAppSettingsPanel();
65 extern void ShowDockAppSettingsPanel(WAppIcon * aicon);
66 extern Cursor wCursor[WCUR_LAST];
67 extern WPreferences wPreferences;
68 extern XContext wWinContext;
69 extern void appIconMouseDown(WObjDescriptor *desc, XEvent *event);
71 #define MOD_MASK wPreferences.modifier_mask
72 #define ICON_SIZE wPreferences.icon_size
74 /***** Local variables ****/
76 static WMPropList *dCommand = NULL;
77 static WMPropList *dPasteCommand = NULL;
78 #ifdef XDND /* XXX was OFFIX */
79 static WMPropList *dDropCommand = NULL;
80 #endif
81 static WMPropList *dAutoLaunch, *dLock;
82 static WMPropList *dName, *dForced, *dBuggyApplication, *dYes, *dNo;
83 static WMPropList *dHost, *dDock, *dClip;
84 static WMPropList *dAutoAttractIcons;
86 static WMPropList *dPosition, *dApplications, *dLowered, *dCollapsed;
88 static WMPropList *dAutoCollapse, *dAutoRaiseLower, *dOmnipresent;
90 static void dockIconPaint(WAppIcon * btn);
92 static void iconMouseDown(WObjDescriptor * desc, XEvent * event);
94 static pid_t execCommand(WAppIcon * btn, char *command, WSavedState * state);
96 static void trackDeadProcess(pid_t pid, unsigned char status, WDock * dock);
98 static int getClipButton(int px, int py);
100 static void toggleLowered(WDock * dock);
102 static void toggleCollapsed(WDock * dock);
104 static void clipIconExpose(WObjDescriptor * desc, XEvent * event);
106 static void clipLeave(WDock * dock);
108 static void handleClipChangeWorkspace(WScreen * scr, XEvent * event);
110 Bool moveIconBetweenDocks(WDock * src, WDock * dest, WAppIcon * icon, int x, int y);
112 static void clipEnterNotify(WObjDescriptor * desc, XEvent * event);
113 static void clipLeaveNotify(WObjDescriptor * desc, XEvent * event);
114 static void clipAutoCollapse(void *cdata);
115 static void clipAutoExpand(void *cdata);
116 static void launchDockedApplication(WAppIcon * btn, Bool withSelection);
118 static void clipAutoLower(void *cdata);
119 static void clipAutoRaise(void *cdata);
121 static void showClipBalloon(WDock * dock, int workspace);
123 static void make_keys(void)
125 if (dCommand != NULL)
126 return;
128 dCommand = WMRetainPropList(WMCreatePLString("Command"));
129 dPasteCommand = WMRetainPropList(WMCreatePLString("PasteCommand"));
130 #ifdef XDND
131 dDropCommand = WMRetainPropList(WMCreatePLString("DropCommand"));
132 #endif
133 dLock = WMRetainPropList(WMCreatePLString("Lock"));
134 dAutoLaunch = WMRetainPropList(WMCreatePLString("AutoLaunch"));
135 dName = WMRetainPropList(WMCreatePLString("Name"));
136 dForced = WMRetainPropList(WMCreatePLString("Forced"));
137 dBuggyApplication = WMRetainPropList(WMCreatePLString("BuggyApplication"));
138 dYes = WMRetainPropList(WMCreatePLString("Yes"));
139 dNo = WMRetainPropList(WMCreatePLString("No"));
140 dHost = WMRetainPropList(WMCreatePLString("Host"));
142 dPosition = WMCreatePLString("Position");
143 dApplications = WMCreatePLString("Applications");
144 dLowered = WMCreatePLString("Lowered");
145 dCollapsed = WMCreatePLString("Collapsed");
146 dAutoCollapse = WMCreatePLString("AutoCollapse");
147 dAutoRaiseLower = WMCreatePLString("AutoRaiseLower");
148 dAutoAttractIcons = WMCreatePLString("AutoAttractIcons");
150 dOmnipresent = WMCreatePLString("Omnipresent");
152 dDock = WMCreatePLString("Dock");
153 dClip = WMCreatePLString("Clip");
156 static void renameCallback(WMenu * menu, WMenuEntry * entry)
158 WDock *dock = entry->clientdata;
159 char buffer[128];
160 int wspace;
161 char *name;
163 assert(entry->clientdata != NULL);
165 wspace = dock->screen_ptr->current_workspace;
167 name = wstrdup(dock->screen_ptr->workspaces[wspace]->name);
169 snprintf(buffer, sizeof(buffer), _("Type the name for workspace %i:"), wspace + 1);
170 if (wInputDialog(dock->screen_ptr, _("Rename Workspace"), buffer, &name)) {
171 wWorkspaceRename(dock->screen_ptr, wspace, name);
173 if (name) {
174 wfree(name);
178 static void toggleLoweredCallback(WMenu * menu, WMenuEntry * entry)
180 assert(entry->clientdata != NULL);
182 toggleLowered(entry->clientdata);
184 entry->flags.indicator_on = !(((WDock *) entry->clientdata)->lowered);
186 wMenuPaint(menu);
189 static int matchWindow(const void *item, const void *cdata)
191 return (((WFakeGroupLeader *) item)->leader == (Window) cdata);
194 static void killCallback(WMenu * menu, WMenuEntry * entry)
196 WScreen *scr = menu->menu->screen_ptr;
197 WAppIcon *icon;
198 WFakeGroupLeader *fPtr;
199 char *buffer, *shortname, **argv;
200 char *basename(const char *shortname);
201 int argc;
203 if (!WCHECK_STATE(WSTATE_NORMAL))
204 return;
206 assert(entry->clientdata != NULL);
208 icon = (WAppIcon *) entry->clientdata;
210 icon->editing = 1;
212 WCHANGE_STATE(WSTATE_MODAL);
214 /* strip away dir names */
215 shortname = basename(icon->command);
216 /* separate out command options */
217 wtokensplit(shortname, &argv, &argc);
219 buffer = wstrconcat(argv[0],
220 _(" will be forcibly closed.\n"
221 "Any unsaved changes will be lost.\n" "Please confirm."));
223 if (icon->icon && icon->icon->owner) {
224 fPtr = icon->icon->owner->fake_group;
225 } else {
226 /* is this really necessary? can we kill a non-running dock icon? */
227 Window win = icon->main_window;
228 int index;
230 index = WMFindInArray(scr->fakeGroupLeaders, matchWindow, (void *)win);
231 if (index != WANotFound)
232 fPtr = WMGetFromArray(scr->fakeGroupLeaders, index);
233 else
234 fPtr = NULL;
237 if (wPreferences.dont_confirm_kill
238 || wMessageDialog(menu->frame->screen_ptr, _("Kill Application"),
239 buffer, _("Yes"), _("No"), NULL) == WAPRDefault) {
240 if (fPtr != NULL) {
241 WWindow *wwin, *twin;
243 wwin = scr->focused_window;
244 while (wwin) {
245 twin = wwin->prev;
246 if (wwin->fake_group == fPtr) {
247 wClientKill(wwin);
249 wwin = twin;
251 } else if (icon->icon && icon->icon->owner) {
252 wClientKill(icon->icon->owner);
256 wfree(buffer);
257 wtokenfree(argv, argc);
259 icon->editing = 0;
261 WCHANGE_STATE(WSTATE_NORMAL);
264 /* TODO: replace this function with a member of the dock struct */
265 static int numberOfSelectedIcons(WDock * dock)
267 WAppIcon *aicon;
268 int i, n;
270 n = 0;
271 for (i = 1; i < dock->max_icons; i++) {
272 aicon = dock->icon_array[i];
273 if (aicon && aicon->icon->selected) {
274 n++;
278 return n;
281 static WMArray *getSelected(WDock * dock)
283 WMArray *ret = WMCreateArray(8);
284 WAppIcon *btn;
285 int i;
287 for (i = 1; i < dock->max_icons; i++) {
288 btn = dock->icon_array[i];
289 if (btn && btn->icon->selected) {
290 WMAddToArray(ret, btn);
294 return ret;
297 static void paintClipButtons(WAppIcon * clipIcon, Bool lpushed, Bool rpushed)
299 Window win = clipIcon->icon->core->window;
300 WScreen *scr = clipIcon->icon->core->screen_ptr;
301 XPoint p[4];
302 int pt = CLIP_BUTTON_SIZE * ICON_SIZE / 64;
303 int tp = ICON_SIZE - pt;
304 int as = pt - 15; /* 15 = 5+5+5 */
305 GC gc = scr->draw_gc; /* maybe use WMColorGC() instead here? */
306 WMColor *color;
307 #ifdef GRADIENT_CLIP_ARROW
308 Bool collapsed = clipIcon->dock->collapsed;
309 #endif
311 /*if (!clipIcon->dock->collapsed)
312 color = scr->clip_title_color[CLIP_NORMAL];
313 else
314 color = scr->clip_title_color[CLIP_COLLAPSED]; */
315 color = scr->clip_title_color[CLIP_NORMAL];
317 XSetForeground(dpy, gc, WMColorPixel(color));
319 if (rpushed) {
320 p[0].x = tp + 1;
321 p[0].y = 1;
322 p[1].x = ICON_SIZE - 2;
323 p[1].y = 1;
324 p[2].x = ICON_SIZE - 2;
325 p[2].y = pt - 1;
326 } else if (lpushed) {
327 p[0].x = 1;
328 p[0].y = tp;
329 p[1].x = pt;
330 p[1].y = ICON_SIZE - 2;
331 p[2].x = 1;
332 p[2].y = ICON_SIZE - 2;
334 if (lpushed || rpushed) {
335 XSetForeground(dpy, scr->draw_gc, scr->white_pixel);
336 XFillPolygon(dpy, win, scr->draw_gc, p, 3, Convex, CoordModeOrigin);
337 XSetForeground(dpy, scr->draw_gc, scr->black_pixel);
339 #ifdef GRADIENT_CLIP_ARROW
340 if (!collapsed) {
341 XSetFillStyle(dpy, scr->copy_gc, FillTiled);
342 XSetTile(dpy, scr->copy_gc, scr->clip_arrow_gradient);
343 XSetClipMask(dpy, scr->copy_gc, None);
344 gc = scr->copy_gc;
346 #endif /* GRADIENT_CLIP_ARROW */
348 /* top right arrow */
349 p[0].x = p[3].x = ICON_SIZE - 5 - as;
350 p[0].y = p[3].y = 5;
351 p[1].x = ICON_SIZE - 6;
352 p[1].y = 5;
353 p[2].x = ICON_SIZE - 6;
354 p[2].y = 4 + as;
355 if (rpushed) {
356 XFillPolygon(dpy, win, scr->draw_gc, p, 3, Convex, CoordModeOrigin);
357 XDrawLines(dpy, win, scr->draw_gc, p, 4, CoordModeOrigin);
358 } else {
359 #ifdef GRADIENT_CLIP_ARROW
360 if (!collapsed)
361 XSetTSOrigin(dpy, gc, ICON_SIZE - 6 - as, 5);
362 #endif
363 XFillPolygon(dpy, win, gc, p, 3, Convex, CoordModeOrigin);
364 XDrawLines(dpy, win, gc, p, 4, CoordModeOrigin);
367 /* bottom left arrow */
368 p[0].x = p[3].x = 5;
369 p[0].y = p[3].y = ICON_SIZE - 5 - as;
370 p[1].x = 5;
371 p[1].y = ICON_SIZE - 6;
372 p[2].x = 4 + as;
373 p[2].y = ICON_SIZE - 6;
374 if (lpushed) {
375 XFillPolygon(dpy, win, scr->draw_gc, p, 3, Convex, CoordModeOrigin);
376 XDrawLines(dpy, win, scr->draw_gc, p, 4, CoordModeOrigin);
377 } else {
378 #ifdef GRADIENT_CLIP_ARROW
379 if (!collapsed)
380 XSetTSOrigin(dpy, gc, 5, ICON_SIZE - 6 - as);
381 #endif
382 XFillPolygon(dpy, win, gc, p, 3, Convex, CoordModeOrigin);
383 XDrawLines(dpy, win, gc, p, 4, CoordModeOrigin);
385 #ifdef GRADIENT_CLIP_ARROW
386 if (!collapsed)
387 XSetFillStyle(dpy, scr->copy_gc, FillSolid);
388 #endif
391 RImage *wClipMakeTile(WScreen * scr, RImage * normalTile)
393 RImage *tile = RCloneImage(normalTile);
394 RColor black;
395 RColor dark;
396 RColor light;
397 int pt, tp;
398 int as;
400 pt = CLIP_BUTTON_SIZE * wPreferences.icon_size / 64;
401 tp = wPreferences.icon_size - 1 - pt;
402 as = pt - 15;
404 black.alpha = 255;
405 black.red = black.green = black.blue = 0;
407 dark.alpha = 0;
408 dark.red = dark.green = dark.blue = 60;
410 light.alpha = 0;
411 light.red = light.green = light.blue = 80;
413 /* top right */
414 ROperateLine(tile, RSubtractOperation, tp, 0, wPreferences.icon_size - 2, pt - 1, &dark);
415 RDrawLine(tile, tp - 1, 0, wPreferences.icon_size - 1, pt + 1, &black);
416 ROperateLine(tile, RAddOperation, tp, 2, wPreferences.icon_size - 3, pt, &light);
418 /* arrow bevel */
419 ROperateLine(tile, RSubtractOperation, ICON_SIZE - 7 - as, 4, ICON_SIZE - 5, 4, &dark);
420 ROperateLine(tile, RSubtractOperation, ICON_SIZE - 6 - as, 5, ICON_SIZE - 5, 6 + as, &dark);
421 ROperateLine(tile, RAddOperation, ICON_SIZE - 5, 4, ICON_SIZE - 5, 6 + as, &light);
423 /* bottom left */
424 ROperateLine(tile, RAddOperation, 2, tp + 2, pt - 2, wPreferences.icon_size - 3, &dark);
425 RDrawLine(tile, 0, tp - 1, pt + 1, wPreferences.icon_size - 1, &black);
426 ROperateLine(tile, RSubtractOperation, 0, tp - 2, pt + 1, wPreferences.icon_size - 2, &light);
428 /* arrow bevel */
429 ROperateLine(tile, RSubtractOperation, 4, ICON_SIZE - 7 - as, 4, ICON_SIZE - 5, &dark);
430 ROperateLine(tile, RSubtractOperation, 5, ICON_SIZE - 6 - as, 6 + as, ICON_SIZE - 5, &dark);
431 ROperateLine(tile, RAddOperation, 4, ICON_SIZE - 5, 6 + as, ICON_SIZE - 5, &light);
433 return tile;
436 static void omnipresentCallback(WMenu * menu, WMenuEntry * entry)
438 WAppIcon *clickedIcon = entry->clientdata;
439 WAppIcon *aicon;
440 WDock *dock;
441 WMArray *selectedIcons;
442 WMArrayIterator iter;
443 int failed;
445 assert(entry->clientdata != NULL);
447 dock = clickedIcon->dock;
449 selectedIcons = getSelected(dock);
451 if (!WMGetArrayItemCount(selectedIcons))
452 WMAddToArray(selectedIcons, clickedIcon);
454 failed = 0;
455 WM_ITERATE_ARRAY(selectedIcons, aicon, iter) {
456 if (wClipMakeIconOmnipresent(aicon, !aicon->omnipresent) == WO_FAILED)
457 failed++;
458 else if (aicon->icon->selected)
459 wIconSelect(aicon->icon);
461 WMFreeArray(selectedIcons);
463 if (failed > 1) {
464 wMessageDialog(dock->screen_ptr, _("Warning"),
465 _("Some icons cannot be made omnipresent. "
466 "Please make sure that no other icon is "
467 "docked in the same positions on the other "
468 "workspaces and the Clip is not full in "
469 "some workspace."), _("OK"), NULL, NULL);
470 } else if (failed == 1) {
471 wMessageDialog(dock->screen_ptr, _("Warning"),
472 _("Icon cannot be made omnipresent. "
473 "Please make sure that no other icon is "
474 "docked in the same position on the other "
475 "workspaces and the Clip is not full in "
476 "some workspace."), _("OK"), NULL, NULL);
480 static void removeIconsCallback(WMenu * menu, WMenuEntry * entry)
482 WAppIcon *clickedIcon = (WAppIcon *) entry->clientdata;
483 WDock *dock;
484 WAppIcon *aicon;
485 WMArray *selectedIcons;
486 int keepit;
487 WMArrayIterator it;
489 assert(clickedIcon != NULL);
491 dock = clickedIcon->dock;
493 selectedIcons = getSelected(dock);
495 if (WMGetArrayItemCount(selectedIcons)) {
496 if (wMessageDialog(dock->screen_ptr, _("Workspace Clip"),
497 _("All selected icons will be removed!"),
498 _("OK"), _("Cancel"), NULL) != WAPRDefault) {
499 WMFreeArray(selectedIcons);
500 return;
502 } else {
503 if (clickedIcon->xindex == 0 && clickedIcon->yindex == 0) {
504 WMFreeArray(selectedIcons);
505 return;
507 WMAddToArray(selectedIcons, clickedIcon);
510 WM_ITERATE_ARRAY(selectedIcons, aicon, it) {
511 keepit = aicon->running && wApplicationOf(aicon->main_window);
512 wDockDetach(dock, aicon);
513 if (keepit) {
514 /* XXX: can: aicon->icon == NULL ? */
515 PlaceIcon(dock->screen_ptr, &aicon->x_pos, &aicon->y_pos,
516 wGetHeadForWindow(aicon->icon->owner));
517 XMoveWindow(dpy, aicon->icon->core->window, aicon->x_pos, aicon->y_pos);
518 if (!dock->mapped || dock->collapsed)
519 XMapWindow(dpy, aicon->icon->core->window);
522 WMFreeArray(selectedIcons);
524 if (wPreferences.auto_arrange_icons)
525 wArrangeIcons(dock->screen_ptr, True);
528 static void keepIconsCallback(WMenu * menu, WMenuEntry * entry)
530 WAppIcon *clickedIcon = (WAppIcon *) entry->clientdata;
531 WDock *dock;
532 WAppIcon *aicon;
533 WMArray *selectedIcons;
534 WMArrayIterator it;
536 assert(clickedIcon != NULL);
537 dock = clickedIcon->dock;
539 selectedIcons = getSelected(dock);
541 if (!WMGetArrayItemCount(selectedIcons)
542 && clickedIcon != dock->screen_ptr->clip_icon) {
543 char *command = NULL;
545 if (!clickedIcon->command && !clickedIcon->editing) {
546 clickedIcon->editing = 1;
547 if (wInputDialog(dock->screen_ptr, _("Keep Icon"),
548 _("Type the command used to launch the application"), &command)) {
549 if (command && (command[0] == 0 || (command[0] == '-' && command[1] == 0))) {
550 wfree(command);
551 command = NULL;
553 clickedIcon->command = command;
554 clickedIcon->editing = 0;
555 } else {
556 clickedIcon->editing = 0;
557 if (command)
558 wfree(command);
559 WMFreeArray(selectedIcons);
560 return;
564 WMAddToArray(selectedIcons, clickedIcon);
567 WM_ITERATE_ARRAY(selectedIcons, aicon, it) {
568 if (aicon->icon->selected)
569 wIconSelect(aicon->icon);
570 if (aicon && aicon->attracted && aicon->command) {
571 aicon->attracted = 0;
572 if (aicon->icon->shadowed) {
573 aicon->icon->shadowed = 0;
574 aicon->icon->force_paint = 1;
575 wAppIconPaint(aicon);
578 wAppIconSave(aicon);
580 WMFreeArray(selectedIcons);
583 static void toggleAutoAttractCallback(WMenu * menu, WMenuEntry * entry)
585 WDock *dock = (WDock *) entry->clientdata;
587 assert(entry->clientdata != NULL);
589 dock->attract_icons = !dock->attract_icons;
590 /*if (!dock->attract_icons)
591 dock->keep_attracted = 0; */
593 entry->flags.indicator_on = dock->attract_icons;
595 wMenuPaint(menu);
598 static void selectCallback(WMenu * menu, WMenuEntry * entry)
600 WAppIcon *icon = (WAppIcon *) entry->clientdata;
602 assert(icon != NULL);
604 wIconSelect(icon->icon);
606 wMenuPaint(menu);
609 static void colectIconsCallback(WMenu * menu, WMenuEntry * entry)
611 WAppIcon *clickedIcon = (WAppIcon *) entry->clientdata;
612 WDock *clip;
613 WAppIcon *aicon;
614 int x, y, x_pos, y_pos;
616 assert(entry->clientdata != NULL);
617 clip = clickedIcon->dock;
619 aicon = clip->screen_ptr->app_icon_list;
621 while (aicon) {
622 if (!aicon->docked && wDockFindFreeSlot(clip, &x, &y)) {
623 x_pos = clip->x_pos + x * ICON_SIZE;
624 y_pos = clip->y_pos + y * ICON_SIZE;
625 if (aicon->x_pos != x_pos || aicon->y_pos != y_pos) {
626 #ifdef ANIMATIONS
627 if (wPreferences.no_animations) {
628 XMoveWindow(dpy, aicon->icon->core->window, x_pos, y_pos);
629 } else {
630 SlideWindow(aicon->icon->core->window,
631 aicon->x_pos, aicon->y_pos, x_pos, y_pos);
633 #else
634 XMoveWindow(dpy, aicon->icon->core->window, x_pos, y_pos);
635 #endif /* ANIMATIONS */
637 aicon->attracted = 1;
638 if (!aicon->icon->shadowed) {
639 aicon->icon->shadowed = 1;
640 aicon->icon->force_paint = 1;
641 /* We don't do an wAppIconPaint() here because it's in
642 * wDockAttachIcon(). -Dan
645 wDockAttachIcon(clip, aicon, x, y);
646 if (clip->collapsed || !clip->mapped)
647 XUnmapWindow(dpy, aicon->icon->core->window);
649 aicon = aicon->next;
653 static void selectIconsCallback(WMenu * menu, WMenuEntry * entry)
655 WAppIcon *clickedIcon = (WAppIcon *) entry->clientdata;
656 WDock *dock;
657 WMArray *selectedIcons;
658 WMArrayIterator iter;
659 WAppIcon *btn;
660 int i;
662 assert(clickedIcon != NULL);
663 dock = clickedIcon->dock;
665 selectedIcons = getSelected(dock);
667 if (!WMGetArrayItemCount(selectedIcons)) {
668 for (i = 1; i < dock->max_icons; i++) {
669 btn = dock->icon_array[i];
670 if (btn && !btn->icon->selected) {
671 wIconSelect(btn->icon);
674 } else {
675 WM_ITERATE_ARRAY(selectedIcons, btn, iter) {
676 wIconSelect(btn->icon);
679 WMFreeArray(selectedIcons);
681 wMenuPaint(menu);
684 static void toggleCollapsedCallback(WMenu * menu, WMenuEntry * entry)
686 assert(entry->clientdata != NULL);
688 toggleCollapsed(entry->clientdata);
690 entry->flags.indicator_on = ((WDock *) entry->clientdata)->collapsed;
692 wMenuPaint(menu);
695 static void toggleAutoCollapseCallback(WMenu * menu, WMenuEntry * entry)
697 WDock *dock;
698 assert(entry->clientdata != NULL);
700 dock = (WDock *) entry->clientdata;
702 dock->auto_collapse = !dock->auto_collapse;
704 entry->flags.indicator_on = ((WDock *) entry->clientdata)->auto_collapse;
706 wMenuPaint(menu);
709 static void toggleAutoRaiseLowerCallback(WMenu * menu, WMenuEntry * entry)
711 WDock *dock;
712 assert(entry->clientdata != NULL);
714 dock = (WDock *) entry->clientdata;
716 dock->auto_raise_lower = !dock->auto_raise_lower;
718 entry->flags.indicator_on = ((WDock *) entry->clientdata)->auto_raise_lower;
720 wMenuPaint(menu);
723 static void launchCallback(WMenu * menu, WMenuEntry * entry)
725 WAppIcon *btn = (WAppIcon *) entry->clientdata;
727 launchDockedApplication(btn, False);
730 static void settingsCallback(WMenu * menu, WMenuEntry * entry)
732 WAppIcon *btn = (WAppIcon *) entry->clientdata;
734 if (btn->editing)
735 return;
736 ShowDockAppSettingsPanel(btn);
739 static void hideCallback(WMenu * menu, WMenuEntry * entry)
741 WApplication *wapp;
742 WAppIcon *btn = (WAppIcon *) entry->clientdata;
744 wapp = wApplicationOf(btn->icon->owner->main_window);
746 if (wapp->flags.hidden) {
747 wWorkspaceChange(btn->icon->core->screen_ptr, wapp->last_workspace);
748 wUnhideApplication(wapp, False, False);
749 } else {
750 wHideApplication(wapp);
754 static void unhideHereCallback(WMenu * menu, WMenuEntry * entry)
756 WApplication *wapp;
757 WAppIcon *btn = (WAppIcon *) entry->clientdata;
759 wapp = wApplicationOf(btn->icon->owner->main_window);
761 wUnhideApplication(wapp, False, True);
764 WAppIcon *mainIconCreate(WScreen * scr, int type)
766 WAppIcon *btn;
767 int x_pos;
769 if (type == WM_CLIP) {
770 if (scr->clip_icon)
771 return scr->clip_icon;
772 btn = wAppIconCreateForDock(scr, NULL, "Logo", "WMClip", TILE_CLIP);
773 btn->icon->core->descriptor.handle_expose = clipIconExpose;
774 btn->icon->core->descriptor.handle_enternotify = clipEnterNotify;
775 btn->icon->core->descriptor.handle_leavenotify = clipLeaveNotify;
776 /*x_pos = scr->scr_width - ICON_SIZE*2 - DOCK_EXTRA_SPACE; */
777 x_pos = 0;
778 } else {
779 btn = wAppIconCreateForDock(scr, NULL, "Logo", "WMDock", TILE_NORMAL);
780 x_pos = scr->scr_width - ICON_SIZE - DOCK_EXTRA_SPACE;
783 btn->xindex = 0;
784 btn->yindex = 0;
786 btn->icon->core->descriptor.handle_mousedown = iconMouseDown;
787 btn->icon->core->descriptor.parent_type = WCLASS_DOCK_ICON;
788 btn->icon->core->descriptor.parent = btn;
789 /*ChangeStackingLevel(btn->icon->core, WMDockLevel); */
790 XMapWindow(dpy, btn->icon->core->window);
791 btn->x_pos = x_pos;
792 btn->y_pos = 0;
793 btn->docked = 1;
794 if (type == WM_CLIP)
795 scr->clip_icon = btn;
797 return btn;
800 static void switchWSCommand(WMenu * menu, WMenuEntry * entry)
802 WAppIcon *btn, *icon = (WAppIcon *) entry->clientdata;
803 WScreen *scr = icon->icon->core->screen_ptr;
804 WDock *src, *dest;
805 WMArray *selectedIcons;
806 int x, y;
808 if (entry->order == scr->current_workspace)
809 return;
810 src = icon->dock;
811 dest = scr->workspaces[entry->order]->clip;
813 selectedIcons = getSelected(src);
815 if (WMGetArrayItemCount(selectedIcons)) {
816 WMArrayIterator iter;
818 WM_ITERATE_ARRAY(selectedIcons, btn, iter) {
819 if (wDockFindFreeSlot(dest, &x, &y)) {
820 moveIconBetweenDocks(src, dest, btn, x, y);
821 XUnmapWindow(dpy, btn->icon->core->window);
824 } else if (icon != scr->clip_icon) {
825 if (wDockFindFreeSlot(dest, &x, &y)) {
826 moveIconBetweenDocks(src, dest, icon, x, y);
827 XUnmapWindow(dpy, icon->icon->core->window);
830 WMFreeArray(selectedIcons);
833 static void launchDockedApplication(WAppIcon * btn, Bool withSelection)
835 WScreen *scr = btn->icon->core->screen_ptr;
837 if (!btn->launching &&
838 ((!withSelection && btn->command != NULL) || (withSelection && btn->paste_command != NULL))) {
839 if (!btn->forced_dock) {
840 btn->relaunching = btn->running;
841 btn->running = 1;
843 if (btn->wm_instance || btn->wm_class) {
844 WWindowAttributes attr;
845 memset(&attr, 0, sizeof(WWindowAttributes));
846 wDefaultFillAttributes(scr, btn->wm_instance, btn->wm_class, &attr, NULL, True);
848 if (!attr.no_appicon && !btn->buggy_app)
849 btn->launching = 1;
850 else
851 btn->running = 0;
853 btn->drop_launch = 0;
854 btn->paste_launch = withSelection;
855 scr->last_dock = btn->dock;
856 btn->pid = execCommand(btn, (withSelection ? btn->paste_command : btn->command), NULL);
857 if (btn->pid > 0) {
858 if (btn->buggy_app) {
859 /* give feedback that the app was launched */
860 btn->launching = 1;
861 dockIconPaint(btn);
862 btn->launching = 0;
863 WMAddTimerHandler(200, (WMCallback *) dockIconPaint, btn);
864 } else {
865 dockIconPaint(btn);
867 } else {
868 wwarning(_("could not launch application %s"), btn->command);
869 btn->launching = 0;
870 if (!btn->relaunching) {
871 btn->running = 0;
877 static void updateWorkspaceMenu(WMenu * menu, WAppIcon * icon)
879 WScreen *scr = menu->frame->screen_ptr;
880 char title[MAX_WORKSPACENAME_WIDTH + 1];
881 int i;
883 if (!menu || !icon)
884 return;
886 for (i = 0; i < scr->workspace_count; i++) {
887 if (i < menu->entry_no) {
888 if (strcmp(menu->entries[i]->text, scr->workspaces[i]->name) != 0) {
889 wfree(menu->entries[i]->text);
890 strcpy(title, scr->workspaces[i]->name);
891 menu->entries[i]->text = wstrdup(title);
892 menu->flags.realized = 0;
894 menu->entries[i]->clientdata = (void *)icon;
895 } else {
896 strcpy(title, scr->workspaces[i]->name);
898 wMenuAddCallback(menu, title, switchWSCommand, (void *)icon);
900 menu->flags.realized = 0;
902 if (i == scr->current_workspace) {
903 wMenuSetEnabled(menu, i, False);
904 } else {
905 wMenuSetEnabled(menu, i, True);
909 if (!menu->flags.realized)
910 wMenuRealize(menu);
913 static WMenu *makeWorkspaceMenu(WScreen * scr)
915 WMenu *menu;
917 menu = wMenuCreate(scr, NULL, False);
918 if (!menu)
919 wwarning(_("could not create workspace submenu for Clip menu"));
921 wMenuAddCallback(menu, "", switchWSCommand, (void *)scr->clip_icon);
923 menu->flags.realized = 0;
924 wMenuRealize(menu);
926 return menu;
929 static void updateClipOptionsMenu(WMenu * menu, WDock * dock)
931 WMenuEntry *entry;
932 int index = 0;
934 if (!menu || !dock)
935 return;
937 /* keep on top */
938 entry = menu->entries[index];
939 entry->flags.indicator_on = !dock->lowered;
940 entry->clientdata = dock;
942 /* collapsed */
943 entry = menu->entries[++index];
944 entry->flags.indicator_on = dock->collapsed;
945 entry->clientdata = dock;
947 /* auto-collapse */
948 entry = menu->entries[++index];
949 entry->flags.indicator_on = dock->auto_collapse;
950 entry->clientdata = dock;
952 /* auto-raise/lower */
953 entry = menu->entries[++index];
954 entry->flags.indicator_on = dock->auto_raise_lower;
955 entry->clientdata = dock;
956 wMenuSetEnabled(menu, index, dock->lowered);
958 /* attract icons */
959 entry = menu->entries[++index];
960 entry->flags.indicator_on = dock->attract_icons;
961 entry->clientdata = dock;
963 menu->flags.realized = 0;
964 wMenuRealize(menu);
967 static WMenu *makeClipOptionsMenu(WScreen * scr)
969 WMenu *menu;
970 WMenuEntry *entry;
972 menu = wMenuCreate(scr, NULL, False);
973 if (!menu) {
974 wwarning(_("could not create options submenu for Clip menu"));
975 return NULL;
978 entry = wMenuAddCallback(menu, _("Keep on Top"), toggleLoweredCallback, NULL);
979 entry->flags.indicator = 1;
980 entry->flags.indicator_on = 1;
981 entry->flags.indicator_type = MI_CHECK;
983 entry = wMenuAddCallback(menu, _("Collapsed"), toggleCollapsedCallback, NULL);
984 entry->flags.indicator = 1;
985 entry->flags.indicator_on = 1;
986 entry->flags.indicator_type = MI_CHECK;
988 entry = wMenuAddCallback(menu, _("Autocollapse"), toggleAutoCollapseCallback, NULL);
989 entry->flags.indicator = 1;
990 entry->flags.indicator_on = 1;
991 entry->flags.indicator_type = MI_CHECK;
993 entry = wMenuAddCallback(menu, _("Autoraise"), toggleAutoRaiseLowerCallback, NULL);
994 entry->flags.indicator = 1;
995 entry->flags.indicator_on = 1;
996 entry->flags.indicator_type = MI_CHECK;
998 entry = wMenuAddCallback(menu, _("Autoattract Icons"), toggleAutoAttractCallback, NULL);
999 entry->flags.indicator = 1;
1000 entry->flags.indicator_on = 1;
1001 entry->flags.indicator_type = MI_CHECK;
1003 menu->flags.realized = 0;
1004 wMenuRealize(menu);
1006 return menu;
1009 static WMenu *dockMenuCreate(WScreen * scr, int type)
1011 WMenu *menu;
1012 WMenuEntry *entry;
1014 if (type == WM_CLIP && scr->clip_menu)
1015 return scr->clip_menu;
1017 menu = wMenuCreate(scr, NULL, False);
1018 if (type != WM_CLIP) {
1019 entry = wMenuAddCallback(menu, _("Keep on Top"), toggleLoweredCallback, NULL);
1020 entry->flags.indicator = 1;
1021 entry->flags.indicator_on = 1;
1022 entry->flags.indicator_type = MI_CHECK;
1023 } else {
1024 entry = wMenuAddCallback(menu, _("Clip Options"), NULL, NULL);
1025 scr->clip_options = makeClipOptionsMenu(scr);
1026 if (scr->clip_options)
1027 wMenuEntrySetCascade(menu, entry, scr->clip_options);
1029 entry = wMenuAddCallback(menu, _("Rename Workspace"), renameCallback, NULL);
1030 wfree(entry->text);
1031 entry->text = _("Rename Workspace");
1033 entry = wMenuAddCallback(menu, _("Selected"), selectCallback, NULL);
1034 entry->flags.indicator = 1;
1035 entry->flags.indicator_on = 1;
1036 entry->flags.indicator_type = MI_CHECK;
1038 entry = wMenuAddCallback(menu, _("Select All Icons"), selectIconsCallback, NULL);
1039 wfree(entry->text);
1040 entry->text = _("Select All Icons");
1042 entry = wMenuAddCallback(menu, _("Keep Icon"), keepIconsCallback, NULL);
1043 wfree(entry->text);
1044 entry->text = _("Keep Icon");
1046 entry = wMenuAddCallback(menu, _("Move Icon To"), NULL, NULL);
1047 wfree(entry->text);
1048 entry->text = _("Move Icon To");
1049 scr->clip_submenu = makeWorkspaceMenu(scr);
1050 if (scr->clip_submenu)
1051 wMenuEntrySetCascade(menu, entry, scr->clip_submenu);
1053 entry = wMenuAddCallback(menu, _("Remove Icon"), removeIconsCallback, NULL);
1054 wfree(entry->text);
1055 entry->text = _("Remove Icon");
1057 wMenuAddCallback(menu, _("Attract Icons"), colectIconsCallback, NULL);
1060 wMenuAddCallback(menu, _("Launch"), launchCallback, NULL);
1062 wMenuAddCallback(menu, _("Unhide Here"), unhideHereCallback, NULL);
1064 entry = wMenuAddCallback(menu, _("Hide"), hideCallback, NULL);
1065 wfree(entry->text);
1066 entry->text = _("Hide");
1068 wMenuAddCallback(menu, _("Settings..."), settingsCallback, NULL);
1070 wMenuAddCallback(menu, _("Kill"), killCallback, NULL);
1072 if (type == WM_CLIP)
1073 scr->clip_menu = menu;
1075 return menu;
1078 WDock *wDockCreate(WScreen * scr, int type)
1080 WDock *dock;
1081 WAppIcon *btn;
1082 int icon_count;
1084 make_keys();
1086 dock = wmalloc(sizeof(WDock));
1087 memset(dock, 0, sizeof(WDock));
1089 if (type == WM_CLIP)
1090 icon_count = CLIP_MAX_ICONS;
1091 else
1092 icon_count = scr->scr_height / wPreferences.icon_size;
1094 dock->icon_array = wmalloc(sizeof(WAppIcon *) * icon_count);
1095 memset(dock->icon_array, 0, sizeof(WAppIcon *) * icon_count);
1097 dock->max_icons = icon_count;
1099 btn = mainIconCreate(scr, type);
1101 btn->dock = dock;
1103 dock->x_pos = btn->x_pos;
1104 dock->y_pos = btn->y_pos;
1105 dock->screen_ptr = scr;
1106 dock->type = type;
1107 dock->icon_count = 1;
1108 dock->on_right_side = 1;
1109 dock->collapsed = 0;
1110 dock->auto_collapse = 0;
1111 dock->auto_collapse_magic = NULL;
1112 dock->auto_raise_lower = 0;
1113 dock->auto_lower_magic = NULL;
1114 dock->auto_raise_magic = NULL;
1115 dock->attract_icons = 0;
1116 dock->lowered = 1;
1117 dock->icon_array[0] = btn;
1118 wRaiseFrame(btn->icon->core);
1119 XMoveWindow(dpy, btn->icon->core->window, btn->x_pos, btn->y_pos);
1121 /* create dock menu */
1122 dock->menu = dockMenuCreate(scr, type);
1124 return dock;
1127 void wDockDestroy(WDock * dock)
1129 int i;
1130 WAppIcon *aicon;
1132 for (i = (dock->type == WM_CLIP) ? 1 : 0; i < dock->max_icons; i++) {
1133 aicon = dock->icon_array[i];
1134 if (aicon) {
1135 int keepit = aicon->running && wApplicationOf(aicon->main_window);
1136 wDockDetach(dock, aicon);
1137 if (keepit) {
1138 /* XXX: can: aicon->icon == NULL ? */
1139 PlaceIcon(dock->screen_ptr, &aicon->x_pos, &aicon->y_pos,
1140 wGetHeadForWindow(aicon->icon->owner));
1141 XMoveWindow(dpy, aicon->icon->core->window, aicon->x_pos, aicon->y_pos);
1142 if (!dock->mapped || dock->collapsed)
1143 XMapWindow(dpy, aicon->icon->core->window);
1147 if (wPreferences.auto_arrange_icons)
1148 wArrangeIcons(dock->screen_ptr, True);
1149 wfree(dock->icon_array);
1150 if (dock->menu && dock->type != WM_CLIP)
1151 wMenuDestroy(dock->menu, True);
1152 if (dock->screen_ptr->last_dock == dock)
1153 dock->screen_ptr->last_dock = NULL;
1154 wfree(dock);
1157 void wClipIconPaint(WAppIcon * aicon)
1159 WScreen *scr = aicon->icon->core->screen_ptr;
1160 WWorkspace *workspace = scr->workspaces[scr->current_workspace];
1161 WMColor *color;
1162 Window win = aicon->icon->core->window;
1163 int length, nlength;
1164 char *ws_name, ws_number[10];
1165 int ty, tx;
1167 wIconPaint(aicon->icon);
1169 length = strlen(workspace->name);
1170 ws_name = wmalloc(length + 1);
1171 snprintf(ws_name, length + 1, "%s", workspace->name);
1172 snprintf(ws_number, sizeof(ws_number), "%i", scr->current_workspace + 1);
1173 nlength = strlen(ws_number);
1175 if (!workspace->clip->collapsed)
1176 color = scr->clip_title_color[CLIP_NORMAL];
1177 else
1178 color = scr->clip_title_color[CLIP_COLLAPSED];
1180 ty = ICON_SIZE - WMFontHeight(scr->clip_title_font) - 3;
1182 tx = CLIP_BUTTON_SIZE * ICON_SIZE / 64;
1184 if(wPreferences.show_clip_title)
1185 WMDrawString(scr->wmscreen, win, color, scr->clip_title_font, tx, ty, ws_name, length);
1186 /*WMDrawString(scr->wmscreen, win, color, scr->clip_title_font, 4,
1187 2, ws_name, length); */
1189 tx = (ICON_SIZE / 2 - WMWidthOfString(scr->clip_title_font, ws_number, nlength)) / 2;
1191 WMDrawString(scr->wmscreen, win, color, scr->clip_title_font, tx, 2, ws_number, nlength);
1193 wfree(ws_name);
1195 if (aicon->launching) {
1196 XFillRectangle(dpy, aicon->icon->core->window, scr->stipple_gc,
1197 0, 0, wPreferences.icon_size, wPreferences.icon_size);
1199 paintClipButtons(aicon, aicon->dock->lclip_button_pushed, aicon->dock->rclip_button_pushed);
1202 static void clipIconExpose(WObjDescriptor * desc, XEvent * event)
1204 wClipIconPaint(desc->parent);
1207 static void dockIconPaint(WAppIcon * btn)
1209 if (btn == btn->icon->core->screen_ptr->clip_icon)
1210 wClipIconPaint(btn);
1211 else {
1212 wAppIconPaint(btn);
1213 wAppIconSave(btn);
1217 static WMPropList *make_icon_state(WAppIcon * btn)
1219 WMPropList *node = NULL;
1220 WMPropList *command, *autolaunch, *lock, *name, *forced, *host;
1221 WMPropList *position, *buggy, *omnipresent;
1222 char *tmp;
1223 char buffer[64];
1225 if (btn) {
1226 if (!btn->command)
1227 command = WMCreatePLString("-");
1228 else
1229 command = WMCreatePLString(btn->command);
1231 autolaunch = btn->auto_launch ? dYes : dNo;
1233 lock = btn->lock ? dYes : dNo;
1235 tmp = EscapeWM_CLASS(btn->wm_instance, btn->wm_class);
1237 name = WMCreatePLString(tmp);
1239 wfree(tmp);
1241 forced = btn->forced_dock ? dYes : dNo;
1243 buggy = btn->buggy_app ? dYes : dNo;
1245 if (btn == btn->icon->core->screen_ptr->clip_icon)
1246 snprintf(buffer, sizeof(buffer), "%i,%i", btn->x_pos, btn->y_pos);
1247 else
1248 snprintf(buffer, sizeof(buffer), "%hi,%hi", btn->xindex, btn->yindex);
1249 position = WMCreatePLString(buffer);
1251 node = WMCreatePLDictionary(dCommand, command,
1252 dName, name,
1253 dAutoLaunch, autolaunch,
1254 dLock, lock,
1255 dForced, forced, dBuggyApplication, buggy, dPosition, position, NULL);
1256 WMReleasePropList(command);
1257 WMReleasePropList(name);
1258 WMReleasePropList(position);
1260 omnipresent = btn->omnipresent ? dYes : dNo;
1261 if (btn->dock != btn->icon->core->screen_ptr->dock && (btn->xindex != 0 || btn->yindex != 0))
1262 WMPutInPLDictionary(node, dOmnipresent, omnipresent);
1264 #ifdef XDND /* was OFFIX */
1265 if (btn->dnd_command) {
1266 command = WMCreatePLString(btn->dnd_command);
1267 WMPutInPLDictionary(node, dDropCommand, command);
1268 WMReleasePropList(command);
1270 #endif /* XDND */
1272 if (btn->paste_command) {
1273 command = WMCreatePLString(btn->paste_command);
1274 WMPutInPLDictionary(node, dPasteCommand, command);
1275 WMReleasePropList(command);
1278 if (btn->client_machine && btn->remote_start) {
1279 host = WMCreatePLString(btn->client_machine);
1280 WMPutInPLDictionary(node, dHost, host);
1281 WMReleasePropList(host);
1285 return node;
1288 static WMPropList *dockSaveState(WDock * dock)
1290 int i;
1291 WMPropList *icon_info;
1292 WMPropList *list = NULL, *dock_state = NULL;
1293 WMPropList *value, *key;
1294 char buffer[256];
1296 list = WMCreatePLArray(NULL);
1298 for (i = (dock->type == WM_DOCK ? 0 : 1); i < dock->max_icons; i++) {
1299 WAppIcon *btn = dock->icon_array[i];
1301 if (!btn || btn->attracted)
1302 continue;
1304 if ((icon_info = make_icon_state(dock->icon_array[i]))) {
1305 WMAddToPLArray(list, icon_info);
1306 WMReleasePropList(icon_info);
1310 dock_state = WMCreatePLDictionary(dApplications, list, NULL);
1312 if (dock->type == WM_DOCK) {
1313 snprintf(buffer, sizeof(buffer), "Applications%i", dock->screen_ptr->scr_height);
1314 key = WMCreatePLString(buffer);
1315 WMPutInPLDictionary(dock_state, key, list);
1316 WMReleasePropList(key);
1318 snprintf(buffer, sizeof(buffer), "%i,%i", (dock->on_right_side ? -ICON_SIZE : 0), dock->y_pos);
1319 value = WMCreatePLString(buffer);
1320 WMPutInPLDictionary(dock_state, dPosition, value);
1321 WMReleasePropList(value);
1323 WMReleasePropList(list);
1325 value = (dock->lowered ? dYes : dNo);
1326 WMPutInPLDictionary(dock_state, dLowered, value);
1328 if (dock->type == WM_CLIP) {
1329 value = (dock->collapsed ? dYes : dNo);
1330 WMPutInPLDictionary(dock_state, dCollapsed, value);
1332 value = (dock->auto_collapse ? dYes : dNo);
1333 WMPutInPLDictionary(dock_state, dAutoCollapse, value);
1335 value = (dock->auto_raise_lower ? dYes : dNo);
1336 WMPutInPLDictionary(dock_state, dAutoRaiseLower, value);
1338 value = (dock->attract_icons ? dYes : dNo);
1339 WMPutInPLDictionary(dock_state, dAutoAttractIcons, value);
1342 return dock_state;
1345 void wDockSaveState(WScreen * scr, WMPropList * old_state)
1347 WMPropList *dock_state;
1348 WMPropList *keys;
1350 dock_state = dockSaveState(scr->dock);
1353 * Copy saved states of docks with different sizes.
1355 if (old_state) {
1356 int i;
1357 WMPropList *tmp;
1359 keys = WMGetPLDictionaryKeys(old_state);
1360 for (i = 0; i < WMGetPropListItemCount(keys); i++) {
1361 tmp = WMGetFromPLArray(keys, i);
1363 if (strncasecmp(WMGetFromPLString(tmp), "applications", 12) == 0
1364 && !WMGetFromPLDictionary(dock_state, tmp)) {
1366 WMPutInPLDictionary(dock_state, tmp, WMGetFromPLDictionary(old_state, tmp));
1369 WMReleasePropList(keys);
1372 WMPutInPLDictionary(scr->session_state, dDock, dock_state);
1374 WMReleasePropList(dock_state);
1377 void wClipSaveState(WScreen * scr)
1379 WMPropList *clip_state;
1381 clip_state = make_icon_state(scr->clip_icon);
1383 WMPutInPLDictionary(scr->session_state, dClip, clip_state);
1385 WMReleasePropList(clip_state);
1388 WMPropList *wClipSaveWorkspaceState(WScreen * scr, int workspace)
1390 return dockSaveState(scr->workspaces[workspace]->clip);
1393 static Bool getBooleanDockValue(WMPropList * value, WMPropList * key)
1395 if (value) {
1396 if (WMIsPLString(value)) {
1397 if (strcasecmp(WMGetFromPLString(value), "YES") == 0)
1398 return True;
1399 } else {
1400 wwarning(_("bad value in docked icon state info %s"), WMGetFromPLString(key));
1403 return False;
1406 static WAppIcon *restore_icon_state(WScreen * scr, WMPropList * info, int type, int index)
1408 WAppIcon *aicon;
1409 WMPropList *cmd, *value;
1411 cmd = WMGetFromPLDictionary(info, dCommand);
1412 if (!cmd || !WMIsPLString(cmd)) {
1413 return NULL;
1416 /* parse window name */
1417 value = WMGetFromPLDictionary(info, dName);
1418 if (!value)
1419 return NULL;
1422 char *wclass, *winstance;
1423 char *command;
1425 ParseWindowName(value, &winstance, &wclass, "dock");
1427 if (!winstance && !wclass) {
1428 return NULL;
1431 /* get commands */
1433 if (cmd)
1434 command = wstrdup(WMGetFromPLString(cmd));
1435 else
1436 command = NULL;
1438 if (!command || strcmp(command, "-") == 0) {
1439 if (command)
1440 wfree(command);
1441 if (wclass)
1442 wfree(wclass);
1443 if (winstance)
1444 wfree(winstance);
1446 return NULL;
1449 aicon = wAppIconCreateForDock(scr, command, winstance, wclass, TILE_NORMAL);
1450 if (wclass)
1451 wfree(wclass);
1452 if (winstance)
1453 wfree(winstance);
1454 if (command)
1455 wfree(command);
1458 aicon->icon->core->descriptor.handle_mousedown = iconMouseDown;
1459 if (type == WM_CLIP) {
1460 aicon->icon->core->descriptor.handle_enternotify = clipEnterNotify;
1461 aicon->icon->core->descriptor.handle_leavenotify = clipLeaveNotify;
1463 aicon->icon->core->descriptor.parent_type = WCLASS_DOCK_ICON;
1464 aicon->icon->core->descriptor.parent = aicon;
1466 #ifdef XDND /* was OFFIX */
1467 cmd = WMGetFromPLDictionary(info, dDropCommand);
1468 if (cmd)
1469 aicon->dnd_command = wstrdup(WMGetFromPLString(cmd));
1470 #endif
1472 cmd = WMGetFromPLDictionary(info, dPasteCommand);
1473 if (cmd)
1474 aicon->paste_command = wstrdup(WMGetFromPLString(cmd));
1476 /* check auto launch */
1477 value = WMGetFromPLDictionary(info, dAutoLaunch);
1479 aicon->auto_launch = getBooleanDockValue(value, dAutoLaunch);
1481 /* check lock */
1482 value = WMGetFromPLDictionary(info, dLock);
1484 aicon->lock = getBooleanDockValue(value, dLock);
1486 /* check if it wasn't normally docked */
1487 value = WMGetFromPLDictionary(info, dForced);
1489 aicon->forced_dock = getBooleanDockValue(value, dForced);
1491 /* check if we can rely on the stuff in the app */
1492 value = WMGetFromPLDictionary(info, dBuggyApplication);
1494 aicon->buggy_app = getBooleanDockValue(value, dBuggyApplication);
1496 /* get position in the dock */
1497 value = WMGetFromPLDictionary(info, dPosition);
1498 if (value && WMIsPLString(value)) {
1499 if (sscanf(WMGetFromPLString(value), "%hi,%hi", &aicon->xindex, &aicon->yindex) != 2)
1500 wwarning(_("bad value in docked icon state info %s"), WMGetFromPLString(dPosition));
1502 /* check position sanity */
1503 /* incomplete section! */
1504 if (type == WM_DOCK) {
1505 aicon->xindex = 0;
1506 if (aicon->yindex < 0)
1507 wwarning(_("bad value in docked icon position %i,%i"),
1508 aicon->xindex, aicon->yindex);
1510 } else {
1511 aicon->yindex = index;
1512 aicon->xindex = 0;
1515 /* check if icon is omnipresent */
1516 value = WMGetFromPLDictionary(info, dOmnipresent);
1518 aicon->omnipresent = getBooleanDockValue(value, dOmnipresent);
1520 aicon->running = 0;
1521 aicon->docked = 1;
1523 return aicon;
1526 #define COMPLAIN(key) wwarning(_("bad value in dock state info:%s"), key)
1528 WAppIcon *wClipRestoreState(WScreen * scr, WMPropList * clip_state)
1530 WAppIcon *icon;
1531 WMPropList *value;
1533 icon = mainIconCreate(scr, WM_CLIP);
1535 if (!clip_state)
1536 return icon;
1538 WMRetainPropList(clip_state);
1540 /* restore position */
1542 value = WMGetFromPLDictionary(clip_state, dPosition);
1544 if (value) {
1545 if (!WMIsPLString(value))
1546 COMPLAIN("Position");
1547 else {
1548 WMRect rect;
1549 int flags;
1551 if (sscanf(WMGetFromPLString(value), "%i,%i", &icon->x_pos, &icon->y_pos) != 2)
1552 COMPLAIN("Position");
1554 /* check position sanity */
1555 rect.pos.x = icon->x_pos;
1556 rect.pos.y = icon->y_pos;
1557 rect.size.width = rect.size.height = ICON_SIZE;
1559 wGetRectPlacementInfo(scr, rect, &flags);
1560 if (flags & (XFLAG_DEAD | XFLAG_PARTIAL))
1561 wScreenKeepInside(scr, &icon->x_pos, &icon->y_pos, ICON_SIZE, ICON_SIZE);
1564 #ifdef XDND /* was OFFIX */
1565 value = WMGetFromPLDictionary(clip_state, dDropCommand);
1566 if (value && WMIsPLString(value))
1567 icon->dnd_command = wstrdup(WMGetFromPLString(value));
1568 #endif
1570 value = WMGetFromPLDictionary(clip_state, dPasteCommand);
1571 if (value && WMIsPLString(value))
1572 icon->paste_command = wstrdup(WMGetFromPLString(value));
1574 WMReleasePropList(clip_state);
1576 return icon;
1579 WDock *wDockRestoreState(WScreen * scr, WMPropList * dock_state, int type)
1581 WDock *dock;
1582 WMPropList *apps;
1583 WMPropList *value;
1584 WAppIcon *aicon, *old_top;
1585 int count, i;
1587 dock = wDockCreate(scr, type);
1589 if (!dock_state)
1590 return dock;
1592 WMRetainPropList(dock_state);
1594 /* restore position */
1596 value = WMGetFromPLDictionary(dock_state, dPosition);
1598 if (value) {
1599 if (!WMIsPLString(value)) {
1600 COMPLAIN("Position");
1601 } else {
1602 WMRect rect;
1603 int flags;
1605 if (sscanf(WMGetFromPLString(value), "%i,%i", &dock->x_pos, &dock->y_pos) != 2)
1606 COMPLAIN("Position");
1608 /* check position sanity */
1609 rect.pos.x = dock->x_pos;
1610 rect.pos.y = dock->y_pos;
1611 rect.size.width = rect.size.height = ICON_SIZE;
1613 wGetRectPlacementInfo(scr, rect, &flags);
1614 if (flags & (XFLAG_DEAD | XFLAG_PARTIAL)) {
1615 int x = dock->x_pos;
1616 wScreenKeepInside(scr, &x, &dock->y_pos, ICON_SIZE, ICON_SIZE);
1619 /* Is this needed any more? */
1620 if (type == WM_CLIP) {
1621 if (dock->x_pos < 0) {
1622 dock->x_pos = 0;
1623 } else if (dock->x_pos > scr->scr_width - ICON_SIZE) {
1624 dock->x_pos = scr->scr_width - ICON_SIZE;
1626 } else {
1627 if (dock->x_pos >= 0) {
1628 dock->x_pos = DOCK_EXTRA_SPACE;
1629 dock->on_right_side = 0;
1630 } else {
1631 dock->x_pos = scr->scr_width - DOCK_EXTRA_SPACE - ICON_SIZE;
1632 dock->on_right_side = 1;
1638 /* restore lowered/raised state */
1640 dock->lowered = 0;
1642 value = WMGetFromPLDictionary(dock_state, dLowered);
1644 if (value) {
1645 if (!WMIsPLString(value)) {
1646 COMPLAIN("Lowered");
1647 } else {
1648 if (strcasecmp(WMGetFromPLString(value), "YES") == 0) {
1649 dock->lowered = 1;
1654 /* restore collapsed state */
1656 dock->collapsed = 0;
1658 value = WMGetFromPLDictionary(dock_state, dCollapsed);
1660 if (value) {
1661 if (!WMIsPLString(value)) {
1662 COMPLAIN("Collapsed");
1663 } else {
1664 if (strcasecmp(WMGetFromPLString(value), "YES") == 0) {
1665 dock->collapsed = 1;
1670 /* restore auto-collapsed state */
1672 value = WMGetFromPLDictionary(dock_state, dAutoCollapse);
1674 if (value) {
1675 if (!WMIsPLString(value)) {
1676 COMPLAIN("AutoCollapse");
1677 } else {
1678 if (strcasecmp(WMGetFromPLString(value), "YES") == 0) {
1679 dock->auto_collapse = 1;
1680 dock->collapsed = 1;
1685 /* restore auto-raise/lower state */
1687 value = WMGetFromPLDictionary(dock_state, dAutoRaiseLower);
1689 if (value) {
1690 if (!WMIsPLString(value)) {
1691 COMPLAIN("AutoRaiseLower");
1692 } else {
1693 if (strcasecmp(WMGetFromPLString(value), "YES") == 0) {
1694 dock->auto_raise_lower = 1;
1699 /* restore attract icons state */
1701 dock->attract_icons = 0;
1703 value = WMGetFromPLDictionary(dock_state, dAutoAttractIcons);
1705 if (value) {
1706 if (!WMIsPLString(value)) {
1707 COMPLAIN("AutoAttractIcons");
1708 } else {
1709 if (strcasecmp(WMGetFromPLString(value), "YES") == 0) {
1710 dock->attract_icons = 1;
1715 /* application list */
1718 WMPropList *tmp;
1719 char buffer[64];
1722 * When saving, it saves the dock state in
1723 * Applications and Applicationsnnn
1725 * When loading, it will first try Applicationsnnn.
1726 * If it does not exist, use Applications as default.
1729 snprintf(buffer, sizeof(buffer), "Applications%i", scr->scr_height);
1731 tmp = WMCreatePLString(buffer);
1732 apps = WMGetFromPLDictionary(dock_state, tmp);
1733 WMReleasePropList(tmp);
1735 if (!apps) {
1736 apps = WMGetFromPLDictionary(dock_state, dApplications);
1740 if (!apps) {
1741 goto finish;
1744 count = WMGetPropListItemCount(apps);
1746 if (count == 0)
1747 goto finish;
1749 old_top = dock->icon_array[0];
1751 /* dock->icon_count is set to 1 when dock is created.
1752 * Since Clip is already restored, we want to keep it so for clip,
1753 * but for dock we may change the default top tile, so we set it to 0.
1755 if (type == WM_DOCK)
1756 dock->icon_count = 0;
1758 for (i = 0; i < count; i++) {
1759 if (dock->icon_count >= dock->max_icons) {
1760 wwarning(_("there are too many icons stored in dock. Ignoring what doesn't fit"));
1761 break;
1764 value = WMGetFromPLArray(apps, i);
1765 aicon = restore_icon_state(scr, value, type, dock->icon_count);
1767 dock->icon_array[dock->icon_count] = aicon;
1769 if (aicon) {
1770 aicon->dock = dock;
1771 aicon->x_pos = dock->x_pos + (aicon->xindex * ICON_SIZE);
1772 aicon->y_pos = dock->y_pos + (aicon->yindex * ICON_SIZE);
1774 if (dock->lowered)
1775 ChangeStackingLevel(aicon->icon->core, WMNormalLevel);
1776 else
1777 ChangeStackingLevel(aicon->icon->core, WMDockLevel);
1779 wCoreConfigure(aicon->icon->core, aicon->x_pos, aicon->y_pos, 0, 0);
1781 if (!dock->collapsed)
1782 XMapWindow(dpy, aicon->icon->core->window);
1783 wRaiseFrame(aicon->icon->core);
1785 dock->icon_count++;
1786 } else if (dock->icon_count == 0 && type == WM_DOCK)
1787 dock->icon_count++;
1790 /* if the first icon is not defined, use the default */
1791 if (dock->icon_array[0] == NULL) {
1792 /* update default icon */
1793 old_top->x_pos = dock->x_pos;
1794 old_top->y_pos = dock->y_pos;
1795 if (dock->lowered)
1796 ChangeStackingLevel(old_top->icon->core, WMNormalLevel);
1797 else
1798 ChangeStackingLevel(old_top->icon->core, WMDockLevel);
1799 dock->icon_array[0] = old_top;
1800 XMoveWindow(dpy, old_top->icon->core->window, dock->x_pos, dock->y_pos);
1801 /* we don't need to increment dock->icon_count here because it was
1802 * incremented in the loop above.
1804 } else if (old_top != dock->icon_array[0]) {
1805 if (old_top == scr->clip_icon)
1806 scr->clip_icon = dock->icon_array[0];
1807 wAppIconDestroy(old_top);
1810 finish:
1811 WMReleasePropList(dock_state);
1813 return dock;
1816 void wDockLaunchWithState(WDock * dock, WAppIcon * btn, WSavedState * state)
1818 if (btn && btn->command && !btn->running && !btn->launching) {
1820 btn->drop_launch = 0;
1821 btn->paste_launch = 0;
1823 btn->pid = execCommand(btn, btn->command, state);
1825 if (btn->pid > 0) {
1826 if (!btn->forced_dock && !btn->buggy_app) {
1827 btn->launching = 1;
1828 dockIconPaint(btn);
1831 } else {
1832 wfree(state);
1836 void wDockDoAutoLaunch(WDock * dock, int workspace)
1838 WAppIcon *btn;
1839 WSavedState *state;
1840 int i;
1842 for (i = 0; i < dock->max_icons; i++) {
1843 btn = dock->icon_array[i];
1844 if (!btn || !btn->auto_launch)
1845 continue;
1847 state = wmalloc(sizeof(WSavedState));
1848 memset(state, 0, sizeof(WSavedState));
1849 state->workspace = workspace;
1850 /* TODO: this is klugy and is very difficult to understand
1851 * what's going on. Try to clean up */
1852 wDockLaunchWithState(dock, btn, state);
1856 #ifdef XDND /* was OFFIX */
1857 static WDock *findDock(WScreen * scr, XEvent * event, int *icon_pos)
1859 WDock *dock;
1860 int i;
1862 *icon_pos = -1;
1863 if ((dock = scr->dock) != NULL) {
1864 for (i = 0; i < dock->max_icons; i++) {
1865 if (dock->icon_array[i]
1866 && dock->icon_array[i]->icon->core->window == event->xclient.window) {
1867 *icon_pos = i;
1868 break;
1872 if (*icon_pos < 0 && (dock = scr->workspaces[scr->current_workspace]->clip) != NULL) {
1873 for (i = 0; i < dock->max_icons; i++) {
1874 if (dock->icon_array[i]
1875 && dock->icon_array[i]->icon->core->window == event->xclient.window) {
1876 *icon_pos = i;
1877 break;
1881 if (*icon_pos >= 0)
1882 return dock;
1883 return NULL;
1886 int wDockReceiveDNDDrop(WScreen * scr, XEvent * event)
1888 WDock *dock;
1889 WAppIcon *btn;
1890 int icon_pos;
1892 dock = findDock(scr, event, &icon_pos);
1893 if (!dock)
1894 return False;
1897 * Return True if the drop was on an application icon window.
1898 * In this case, let the ClientMessage handler redirect the
1899 * message to the app.
1901 if (dock->icon_array[icon_pos]->icon->icon_win != None)
1902 return True;
1904 if (dock->icon_array[icon_pos]->dnd_command != NULL) {
1905 scr->flags.dnd_data_convertion_status = 0;
1907 btn = dock->icon_array[icon_pos];
1909 if (!btn->forced_dock) {
1910 btn->relaunching = btn->running;
1911 btn->running = 1;
1913 if (btn->wm_instance || btn->wm_class) {
1914 WWindowAttributes attr;
1915 memset(&attr, 0, sizeof(WWindowAttributes));
1916 wDefaultFillAttributes(btn->icon->core->screen_ptr,
1917 btn->wm_instance, btn->wm_class, &attr, NULL, True);
1919 if (!attr.no_appicon)
1920 btn->launching = 1;
1921 else
1922 btn->running = 0;
1925 btn->paste_launch = 0;
1926 btn->drop_launch = 1;
1927 scr->last_dock = dock;
1928 btn->pid = execCommand(btn, btn->dnd_command, NULL);
1929 if (btn->pid > 0) {
1930 dockIconPaint(btn);
1931 } else {
1932 btn->launching = 0;
1933 if (!btn->relaunching) {
1934 btn->running = 0;
1938 return False;
1940 #endif /* XDND */
1942 Bool wDockAttachIcon(WDock * dock, WAppIcon * icon, int x, int y)
1944 WWindow *wwin;
1945 int index;
1947 wwin = icon->icon->owner;
1948 if (icon->command == NULL) {
1949 char *command;
1951 icon->editing = 0;
1953 command = GetCommandForWindow(wwin->client_win);
1954 if (command) {
1955 icon->command = command;
1956 } else {
1957 /* icon->forced_dock = 1; */
1958 if (dock->type != WM_CLIP || !icon->attracted) {
1959 icon->editing = 1;
1960 if (wInputDialog(dock->screen_ptr, _("Dock Icon"),
1961 _("Type the command used to launch the application"), &command)) {
1962 if (command && (command[0] == 0 || (command[0] == '-' && command[1] == 0))) {
1963 wfree(command);
1964 command = NULL;
1966 icon->command = command;
1967 icon->editing = 0;
1968 } else {
1969 icon->editing = 0;
1970 if (command)
1971 wfree(command);
1972 /* If the target is the dock, reject the icon. If
1973 * the target is the clip, make it an attracted icon
1975 if (dock->type == WM_CLIP) {
1976 icon->attracted = 1;
1977 if (!icon->icon->shadowed) {
1978 icon->icon->shadowed = 1;
1979 icon->icon->force_paint = 1;
1981 } else {
1982 return False;
1987 } else {
1988 icon->editing = 0;
1991 for (index = 1; index < dock->max_icons; index++)
1992 if (dock->icon_array[index] == NULL)
1993 break;
1994 /* if (index == dock->max_icons)
1995 return; */
1997 assert(index < dock->max_icons);
1999 dock->icon_array[index] = icon;
2000 icon->yindex = y;
2001 icon->xindex = x;
2003 icon->omnipresent = 0;
2005 icon->x_pos = dock->x_pos + x * ICON_SIZE;
2006 icon->y_pos = dock->y_pos + y * ICON_SIZE;
2008 dock->icon_count++;
2010 icon->running = 1;
2011 icon->launching = 0;
2012 icon->docked = 1;
2013 icon->dock = dock;
2014 icon->icon->core->descriptor.handle_mousedown = iconMouseDown;
2015 if (dock->type == WM_CLIP) {
2016 icon->icon->core->descriptor.handle_enternotify = clipEnterNotify;
2017 icon->icon->core->descriptor.handle_leavenotify = clipLeaveNotify;
2019 icon->icon->core->descriptor.parent_type = WCLASS_DOCK_ICON;
2020 icon->icon->core->descriptor.parent = icon;
2022 MoveInStackListUnder(dock->icon_array[index - 1]->icon->core, icon->icon->core);
2023 wAppIconMove(icon, icon->x_pos, icon->y_pos);
2024 wAppIconPaint(icon);
2025 wAppIconSave(icon);
2027 if (wPreferences.auto_arrange_icons)
2028 wArrangeIcons(dock->screen_ptr, True);
2030 #ifdef XDND /* was OFFIX */
2031 if (icon->command && !icon->dnd_command) {
2032 int len = strlen(icon->command) + 8;
2033 icon->dnd_command = wmalloc(len);
2034 snprintf(icon->dnd_command, len, "%s %%d", icon->command);
2036 #endif
2038 if (icon->command && !icon->paste_command) {
2039 int len = strlen(icon->command) + 8;
2040 icon->paste_command = wmalloc(len);
2041 snprintf(icon->paste_command, len, "%s %%s", icon->command);
2044 return True;
2047 void reattachIcon(WDock * dock, WAppIcon * icon, int x, int y)
2049 int index;
2051 for (index = 1; index < dock->max_icons; index++) {
2052 if (dock->icon_array[index] == icon)
2053 break;
2055 assert(index < dock->max_icons);
2057 icon->yindex = y;
2058 icon->xindex = x;
2060 icon->x_pos = dock->x_pos + x * ICON_SIZE;
2061 icon->y_pos = dock->y_pos + y * ICON_SIZE;
2064 Bool moveIconBetweenDocks(WDock * src, WDock * dest, WAppIcon * icon, int x, int y)
2066 WWindow *wwin;
2067 char *command;
2068 int index;
2070 if (src == dest)
2071 return True; /* No move needed, we're already there */
2073 if (dest == NULL)
2074 return False;
2076 wwin = icon->icon->owner;
2079 * For the moment we can't do this if we move icons in Clip from one
2080 * workspace to other, because if we move two or more icons without
2081 * command, the dialog box will not be able to tell us to which of the
2082 * moved icons it applies. -Dan
2084 if ((dest->type == WM_DOCK /*|| dest->keep_attracted */ ) && icon->command == NULL) {
2085 command = GetCommandForWindow(wwin->client_win);
2086 if (command) {
2087 icon->command = command;
2088 } else {
2089 icon->editing = 1;
2090 /* icon->forced_dock = 1; */
2091 if (wInputDialog(src->screen_ptr, _("Dock Icon"),
2092 _("Type the command used to launch the application"), &command)) {
2093 if (command && (command[0] == 0 || (command[0] == '-' && command[1] == 0))) {
2094 wfree(command);
2095 command = NULL;
2097 icon->command = command;
2098 } else {
2099 icon->editing = 0;
2100 if (command)
2101 wfree(command);
2102 return False;
2104 icon->editing = 0;
2108 if (dest->type == WM_DOCK)
2109 wClipMakeIconOmnipresent(icon, False);
2111 for (index = 1; index < src->max_icons; index++) {
2112 if (src->icon_array[index] == icon)
2113 break;
2115 assert(index < src->max_icons);
2117 src->icon_array[index] = NULL;
2118 src->icon_count--;
2120 for (index = 1; index < dest->max_icons; index++) {
2121 if (dest->icon_array[index] == NULL)
2122 break;
2124 /* if (index == dest->max_icons)
2125 return; */
2127 assert(index < dest->max_icons);
2129 dest->icon_array[index] = icon;
2130 icon->dock = dest;
2132 /* deselect the icon */
2133 if (icon->icon->selected)
2134 wIconSelect(icon->icon);
2136 if (dest->type == WM_DOCK) {
2137 icon->icon->core->descriptor.handle_enternotify = NULL;
2138 icon->icon->core->descriptor.handle_leavenotify = NULL;
2139 } else {
2140 icon->icon->core->descriptor.handle_enternotify = clipEnterNotify;
2141 icon->icon->core->descriptor.handle_leavenotify = clipLeaveNotify;
2144 /* set it to be kept when moving to dock.
2145 * Unless the icon does not have a command set
2147 if (icon->command && dest->type == WM_DOCK) {
2148 icon->attracted = 0;
2149 if (icon->icon->shadowed) {
2150 icon->icon->shadowed = 0;
2151 icon->icon->force_paint = 1;
2153 wAppIconSave(icon);
2156 if (src->auto_collapse || src->auto_raise_lower)
2157 clipLeave(src);
2159 icon->yindex = y;
2160 icon->xindex = x;
2162 icon->x_pos = dest->x_pos + x * ICON_SIZE;
2163 icon->y_pos = dest->y_pos + y * ICON_SIZE;
2165 dest->icon_count++;
2167 MoveInStackListUnder(dest->icon_array[index - 1]->icon->core, icon->icon->core);
2168 wAppIconPaint(icon);
2170 return True;
2173 void wDockDetach(WDock * dock, WAppIcon * icon)
2175 int index;
2177 /* make the settings panel be closed */
2178 if (icon->panel) {
2179 DestroyDockAppSettingsPanel(icon->panel);
2182 /* This must be called before icon->dock is set to NULL.
2183 * Don't move it. -Dan
2185 wClipMakeIconOmnipresent(icon, False);
2187 icon->docked = 0;
2188 icon->dock = NULL;
2189 icon->attracted = 0;
2190 icon->auto_launch = 0;
2191 if (icon->icon->shadowed) {
2192 icon->icon->shadowed = 0;
2193 icon->icon->force_paint = 1;
2196 /* deselect the icon */
2197 if (icon->icon->selected)
2198 wIconSelect(icon->icon);
2200 if (icon->command) {
2201 wfree(icon->command);
2202 icon->command = NULL;
2204 #ifdef XDND /* was OFFIX */
2205 if (icon->dnd_command) {
2206 wfree(icon->dnd_command);
2207 icon->dnd_command = NULL;
2209 #endif
2210 if (icon->paste_command) {
2211 wfree(icon->paste_command);
2212 icon->paste_command = NULL;
2215 for (index = 1; index < dock->max_icons; index++)
2216 if (dock->icon_array[index] == icon)
2217 break;
2218 assert(index < dock->max_icons);
2219 dock->icon_array[index] = NULL;
2220 icon->yindex = -1;
2221 icon->xindex = -1;
2223 dock->icon_count--;
2225 /* if the dock is not attached to an application or
2226 * the the application did not set the approriate hints yet,
2227 * destroy the icon */
2228 if (!icon->running || !wApplicationOf(icon->main_window))
2229 wAppIconDestroy(icon);
2230 else {
2231 icon->icon->core->descriptor.handle_mousedown = appIconMouseDown;
2232 icon->icon->core->descriptor.handle_enternotify = NULL;
2233 icon->icon->core->descriptor.handle_leavenotify = NULL;
2234 icon->icon->core->descriptor.parent_type = WCLASS_APPICON;
2235 icon->icon->core->descriptor.parent = icon;
2237 ChangeStackingLevel(icon->icon->core, NORMAL_ICON_LEVEL);
2239 wAppIconPaint(icon);
2240 if (wPreferences.auto_arrange_icons) {
2241 wArrangeIcons(dock->screen_ptr, True);
2244 if (dock->auto_collapse || dock->auto_raise_lower)
2245 clipLeave(dock);
2249 * returns the closest Dock slot index for the passed
2250 * coordinates.
2252 * Returns False if icon can't be docked.
2254 * Note: this function should NEVER alter ret_x or ret_y, unless it will
2255 * return True. -Dan
2257 Bool wDockSnapIcon(WDock * dock, WAppIcon * icon, int req_x, int req_y, int *ret_x, int *ret_y, int redocking)
2259 WScreen *scr = dock->screen_ptr;
2260 int dx, dy;
2261 int ex_x, ex_y;
2262 int i, offset = ICON_SIZE / 2;
2263 WAppIcon *aicon = NULL;
2264 WAppIcon *nicon = NULL;
2265 int max_y_icons, max_x_icons;
2267 /* TODO: XINERAMA, for these */
2268 max_x_icons = scr->scr_width / ICON_SIZE;
2269 max_y_icons = scr->scr_height / ICON_SIZE - 1;
2271 if (wPreferences.flags.noupdates)
2272 return False;
2274 dx = dock->x_pos;
2275 dy = dock->y_pos;
2277 /* if the dock is full */
2278 if (!redocking && (dock->icon_count >= dock->max_icons)) {
2279 return False;
2282 /* exact position */
2283 if (req_y < dy)
2284 ex_y = (req_y - offset - dy) / ICON_SIZE;
2285 else
2286 ex_y = (req_y + offset - dy) / ICON_SIZE;
2288 if (req_x < dx)
2289 ex_x = (req_x - offset - dx) / ICON_SIZE;
2290 else
2291 ex_x = (req_x + offset - dx) / ICON_SIZE;
2293 /* check if the icon is outside the screen boundaries */
2295 WMRect rect;
2296 int flags;
2298 rect.pos.x = dx + ex_x * ICON_SIZE;
2299 rect.pos.y = dy + ex_y * ICON_SIZE;
2300 rect.size.width = rect.size.height = ICON_SIZE;
2302 wGetRectPlacementInfo(scr, rect, &flags);
2303 if (flags & (XFLAG_DEAD | XFLAG_PARTIAL))
2304 return False;
2307 if (dock->type == WM_DOCK) {
2308 if (icon->dock != dock && ex_x != 0)
2309 return False;
2311 aicon = NULL;
2312 for (i = 0; i < dock->max_icons; i++) {
2313 nicon = dock->icon_array[i];
2314 if (nicon && nicon->yindex == ex_y) {
2315 aicon = nicon;
2316 break;
2320 if (redocking) {
2321 int sig, done, closest;
2323 /* Possible cases when redocking:
2325 * icon dragged out of range of any slot -> false
2326 * icon dragged to range of free slot
2327 * icon dragged to range of same slot
2328 * icon dragged to range of different icon
2330 if (abs(ex_x) > DOCK_DETTACH_THRESHOLD)
2331 return False;
2333 if (ex_y >= 0 && ex_y <= max_y_icons && (aicon == icon || !aicon)) {
2334 *ret_x = 0;
2335 *ret_y = ex_y;
2336 return True;
2339 /* start looking at the upper slot or lower? */
2340 if (ex_y * ICON_SIZE < (req_y + offset - dy))
2341 sig = 1;
2342 else
2343 sig = -1;
2345 closest = -1;
2346 done = 0;
2347 /* look for closest free slot */
2348 for (i = 0; i < (DOCK_DETTACH_THRESHOLD + 1) * 2 && !done; i++) {
2349 int j;
2351 done = 1;
2352 closest = sig * (i / 2) + ex_y;
2353 /* check if this slot is used */
2354 if (closest >= 0) {
2355 for (j = 0; j < dock->max_icons; j++) {
2356 if (dock->icon_array[j]
2357 && dock->icon_array[j]->yindex == closest) {
2358 /* slot is used by someone else */
2359 if (dock->icon_array[j] != icon)
2360 done = 0;
2361 break;
2365 sig = -sig;
2367 if (done && closest >= 0 && closest <= max_y_icons &&
2368 ((ex_y >= closest && ex_y - closest < DOCK_DETTACH_THRESHOLD + 1)
2369 || (ex_y < closest && closest - ex_y <= DOCK_DETTACH_THRESHOLD + 1))) {
2370 *ret_x = 0;
2371 *ret_y = closest;
2372 return True;
2374 } else { /* !redocking */
2376 /* if slot is free and the icon is close enough, return it */
2377 if (!aicon && ex_x == 0 && ex_y >= 0 && ex_y <= max_y_icons) {
2378 *ret_x = 0;
2379 *ret_y = ex_y;
2380 return True;
2383 } else { /* CLIP */
2384 int neighbours = 0;
2385 int start, stop, k;
2387 start = icon->omnipresent ? 0 : scr->current_workspace;
2388 stop = icon->omnipresent ? scr->workspace_count : start + 1;
2390 aicon = NULL;
2391 for (k = start; k < stop; k++) {
2392 WDock *tmp = scr->workspaces[k]->clip;
2393 if (!tmp)
2394 continue;
2395 for (i = 0; i < tmp->max_icons; i++) {
2396 nicon = tmp->icon_array[i];
2397 if (nicon && nicon->xindex == ex_x && nicon->yindex == ex_y) {
2398 aicon = nicon;
2399 break;
2402 if (aicon)
2403 break;
2405 for (k = start; k < stop; k++) {
2406 WDock *tmp = scr->workspaces[k]->clip;
2407 if (!tmp)
2408 continue;
2409 for (i = 0; i < tmp->max_icons; i++) {
2410 nicon = tmp->icon_array[i];
2411 if (nicon && nicon != icon && /* Icon can't be it's own neighbour */
2412 (abs(nicon->xindex - ex_x) <= CLIP_ATTACH_VICINITY &&
2413 abs(nicon->yindex - ex_y) <= CLIP_ATTACH_VICINITY)) {
2414 neighbours = 1;
2415 break;
2418 if (neighbours)
2419 break;
2422 if (neighbours && (aicon == NULL || (redocking && aicon == icon))) {
2423 *ret_x = ex_x;
2424 *ret_y = ex_y;
2425 return True;
2428 return False;
2431 static int onScreen(WScreen * scr, int x, int y, int sx, int ex, int sy, int ey)
2433 WMRect rect = wmkrect(x, y, ICON_SIZE, ICON_SIZE);
2434 int flags;
2436 wGetRectPlacementInfo(scr, rect, &flags);
2438 return !(flags & (XFLAG_DEAD | XFLAG_PARTIAL));
2442 * returns true if it can find a free slot in the dock,
2443 * in which case it changes x_pos and y_pos accordingly.
2444 * Else returns false.
2446 Bool wDockFindFreeSlot(WDock * dock, int *x_pos, int *y_pos)
2448 WScreen *scr = dock->screen_ptr;
2449 WAppIcon *btn;
2450 WAppIconChain *chain;
2451 unsigned char *slot_map;
2452 int mwidth;
2453 int r;
2454 int x, y;
2455 int i, done = False;
2456 int corner;
2457 int sx = 0, sy = 0, ex = scr->scr_width, ey = scr->scr_height;
2458 int extra_count = 0;
2460 if (dock->type == WM_CLIP && dock != scr->workspaces[scr->current_workspace]->clip)
2461 extra_count = scr->global_icon_count;
2463 /* if the dock is full */
2464 if (dock->icon_count + extra_count >= dock->max_icons) {
2465 return False;
2468 if (!wPreferences.flags.nodock && scr->dock) {
2469 if (scr->dock->on_right_side)
2470 ex -= ICON_SIZE + DOCK_EXTRA_SPACE;
2471 else
2472 sx += ICON_SIZE + DOCK_EXTRA_SPACE;
2475 if (ex < dock->x_pos)
2476 ex = dock->x_pos;
2477 if (sx > dock->x_pos + ICON_SIZE)
2478 sx = dock->x_pos + ICON_SIZE;
2479 #define C_NONE 0
2480 #define C_NW 1
2481 #define C_NE 2
2482 #define C_SW 3
2483 #define C_SE 4
2485 /* check if clip is in a corner */
2486 if (dock->type == WM_CLIP) {
2487 if (dock->x_pos < 1 && dock->y_pos < 1)
2488 corner = C_NE;
2489 else if (dock->x_pos < 1 && dock->y_pos >= (ey - ICON_SIZE))
2490 corner = C_SE;
2491 else if (dock->x_pos >= (ex - ICON_SIZE) && dock->y_pos >= (ey - ICON_SIZE))
2492 corner = C_SW;
2493 else if (dock->x_pos >= (ex - ICON_SIZE) && dock->y_pos < 1)
2494 corner = C_NW;
2495 else
2496 corner = C_NONE;
2497 } else
2498 corner = C_NONE;
2500 /* If the clip is in the corner, use only slots that are in the border
2501 * of the screen */
2502 if (corner != C_NONE) {
2503 char *hmap, *vmap;
2504 int hcount, vcount;
2506 hcount = WMIN(dock->max_icons, scr->scr_width / ICON_SIZE);
2507 vcount = WMIN(dock->max_icons, scr->scr_height / ICON_SIZE);
2508 hmap = wmalloc(hcount + 1);
2509 memset(hmap, 0, hcount + 1);
2510 vmap = wmalloc(vcount + 1);
2511 memset(vmap, 0, vcount + 1);
2513 /* mark used positions */
2514 switch (corner) {
2515 case C_NE:
2516 for (i = 0; i < dock->max_icons; i++) {
2517 btn = dock->icon_array[i];
2518 if (!btn)
2519 continue;
2521 if (btn->xindex == 0 && btn->yindex > 0 && btn->yindex < vcount)
2522 vmap[btn->yindex] = 1;
2523 else if (btn->yindex == 0 && btn->xindex > 0 && btn->xindex < hcount)
2524 hmap[btn->xindex] = 1;
2526 for (chain = scr->global_icons; chain != NULL; chain = chain->next) {
2527 btn = chain->aicon;
2528 if (btn->xindex == 0 && btn->yindex > 0 && btn->yindex < vcount)
2529 vmap[btn->yindex] = 1;
2530 else if (btn->yindex == 0 && btn->xindex > 0 && btn->xindex < hcount)
2531 hmap[btn->xindex] = 1;
2533 break;
2534 case C_NW:
2535 for (i = 0; i < dock->max_icons; i++) {
2536 btn = dock->icon_array[i];
2537 if (!btn)
2538 continue;
2540 if (btn->xindex == 0 && btn->yindex > 0 && btn->yindex < vcount)
2541 vmap[btn->yindex] = 1;
2542 else if (btn->yindex == 0 && btn->xindex < 0 && btn->xindex > -hcount)
2543 hmap[-btn->xindex] = 1;
2545 for (chain = scr->global_icons; chain != NULL; chain = chain->next) {
2546 btn = chain->aicon;
2547 if (btn->xindex == 0 && btn->yindex > 0 && btn->yindex < vcount)
2548 vmap[btn->yindex] = 1;
2549 else if (btn->yindex == 0 && btn->xindex < 0 && btn->xindex > -hcount)
2550 hmap[-btn->xindex] = 1;
2552 break;
2553 case C_SE:
2554 for (i = 0; i < dock->max_icons; i++) {
2555 btn = dock->icon_array[i];
2556 if (!btn)
2557 continue;
2559 if (btn->xindex == 0 && btn->yindex < 0 && btn->yindex > -vcount)
2560 vmap[-btn->yindex] = 1;
2561 else if (btn->yindex == 0 && btn->xindex > 0 && btn->xindex < hcount)
2562 hmap[btn->xindex] = 1;
2564 for (chain = scr->global_icons; chain != NULL; chain = chain->next) {
2565 btn = chain->aicon;
2566 if (btn->xindex == 0 && btn->yindex < 0 && btn->yindex > -vcount)
2567 vmap[-btn->yindex] = 1;
2568 else if (btn->yindex == 0 && btn->xindex > 0 && btn->xindex < hcount)
2569 hmap[btn->xindex] = 1;
2571 break;
2572 case C_SW:
2573 default:
2574 for (i = 0; i < dock->max_icons; i++) {
2575 btn = dock->icon_array[i];
2576 if (!btn)
2577 continue;
2579 if (btn->xindex == 0 && btn->yindex < 0 && btn->yindex > -vcount)
2580 vmap[-btn->yindex] = 1;
2581 else if (btn->yindex == 0 && btn->xindex < 0 && btn->xindex > -hcount)
2582 hmap[-btn->xindex] = 1;
2584 for (chain = scr->global_icons; chain != NULL; chain = chain->next) {
2585 btn = chain->aicon;
2586 if (btn->xindex == 0 && btn->yindex < 0 && btn->yindex > -vcount)
2587 vmap[-btn->yindex] = 1;
2588 else if (btn->yindex == 0 && btn->xindex < 0 && btn->xindex > -hcount)
2589 hmap[-btn->xindex] = 1;
2592 x = 0;
2593 y = 0;
2594 done = 0;
2595 /* search a vacant slot */
2596 for (i = 1; i < WMAX(vcount, hcount); i++) {
2597 if (i < vcount && vmap[i] == 0) {
2598 /* found a slot */
2599 x = 0;
2600 y = i;
2601 done = 1;
2602 break;
2603 } else if (i < hcount && hmap[i] == 0) {
2604 /* found a slot */
2605 x = i;
2606 y = 0;
2607 done = 1;
2608 break;
2611 wfree(vmap);
2612 wfree(hmap);
2613 /* If found a slot, translate and return */
2614 if (done) {
2615 if (corner == C_NW || corner == C_NE) {
2616 *y_pos = y;
2617 } else {
2618 *y_pos = -y;
2620 if (corner == C_NE || corner == C_SE) {
2621 *x_pos = x;
2622 } else {
2623 *x_pos = -x;
2625 return True;
2627 /* else, try to find a slot somewhere else */
2630 /* a map of mwidth x mwidth would be enough if we allowed icons to be
2631 * placed outside of screen */
2632 mwidth = (int)ceil(sqrt(dock->max_icons));
2634 /* In the worst case (the clip is in the corner of the screen),
2635 * the amount of icons that fit in the clip is smaller.
2636 * Double the map to get a safe value.
2638 mwidth += mwidth;
2640 r = (mwidth - 1) / 2;
2642 slot_map = wmalloc(mwidth * mwidth);
2643 memset(slot_map, 0, mwidth * mwidth);
2645 #define XY2OFS(x,y) (WMAX(abs(x),abs(y)) > r) ? 0 : (((y)+r)*(mwidth)+(x)+r)
2647 /* mark used slots in the map. If the slot falls outside the map
2648 * (for example, when all icons are placed in line), ignore them. */
2649 for (i = 0; i < dock->max_icons; i++) {
2650 btn = dock->icon_array[i];
2651 if (btn)
2652 slot_map[XY2OFS(btn->xindex, btn->yindex)] = 1;
2654 for (chain = scr->global_icons; chain != NULL; chain = chain->next) {
2655 slot_map[XY2OFS(chain->aicon->xindex, chain->aicon->yindex)] = 1;
2657 /* Find closest slot from the center that is free by scanning the
2658 * map from the center to outward in circular passes.
2659 * This will not result in a neat layout, but will be optimal
2660 * in the sense that there will not be holes left.
2662 done = 0;
2663 for (i = 1; i <= r && !done; i++) {
2664 int tx, ty;
2666 /* top and bottom parts of the ring */
2667 for (x = -i; x <= i && !done; x++) {
2668 tx = dock->x_pos + x * ICON_SIZE;
2669 y = -i;
2670 ty = dock->y_pos + y * ICON_SIZE;
2671 if (slot_map[XY2OFS(x, y)] == 0 && onScreen(scr, tx, ty, sx, ex, sy, ey)) {
2672 *x_pos = x;
2673 *y_pos = y;
2674 done = 1;
2675 break;
2677 y = i;
2678 ty = dock->y_pos + y * ICON_SIZE;
2679 if (slot_map[XY2OFS(x, y)] == 0 && onScreen(scr, tx, ty, sx, ex, sy, ey)) {
2680 *x_pos = x;
2681 *y_pos = y;
2682 done = 1;
2683 break;
2686 /* left and right parts of the ring */
2687 for (y = -i + 1; y <= i - 1; y++) {
2688 ty = dock->y_pos + y * ICON_SIZE;
2689 x = -i;
2690 tx = dock->x_pos + x * ICON_SIZE;
2691 if (slot_map[XY2OFS(x, y)] == 0 && onScreen(scr, tx, ty, sx, ex, sy, ey)) {
2692 *x_pos = x;
2693 *y_pos = y;
2694 done = 1;
2695 break;
2697 x = i;
2698 tx = dock->x_pos + x * ICON_SIZE;
2699 if (slot_map[XY2OFS(x, y)] == 0 && onScreen(scr, tx, ty, sx, ex, sy, ey)) {
2700 *x_pos = x;
2701 *y_pos = y;
2702 done = 1;
2703 break;
2707 wfree(slot_map);
2708 #undef XY2OFS
2709 return done;
2712 static void moveDock(WDock * dock, int new_x, int new_y)
2714 WAppIcon *btn;
2715 int i;
2717 dock->x_pos = new_x;
2718 dock->y_pos = new_y;
2719 for (i = 0; i < dock->max_icons; i++) {
2720 btn = dock->icon_array[i];
2721 if (btn) {
2722 btn->x_pos = new_x + btn->xindex * ICON_SIZE;
2723 btn->y_pos = new_y + btn->yindex * ICON_SIZE;
2724 XMoveWindow(dpy, btn->icon->core->window, btn->x_pos, btn->y_pos);
2729 static void swapDock(WDock * dock)
2731 WScreen *scr = dock->screen_ptr;
2732 WAppIcon *btn;
2733 int x, i;
2735 if (dock->on_right_side) {
2736 x = dock->x_pos = scr->scr_width - ICON_SIZE - DOCK_EXTRA_SPACE;
2737 } else {
2738 x = dock->x_pos = DOCK_EXTRA_SPACE;
2741 for (i = 0; i < dock->max_icons; i++) {
2742 btn = dock->icon_array[i];
2743 if (btn) {
2744 btn->x_pos = x;
2745 XMoveWindow(dpy, btn->icon->core->window, btn->x_pos, btn->y_pos);
2749 wScreenUpdateUsableArea(scr);
2752 static pid_t execCommand(WAppIcon * btn, char *command, WSavedState * state)
2754 WScreen *scr = btn->icon->core->screen_ptr;
2755 pid_t pid;
2756 char **argv;
2757 int argc;
2758 char *cmdline;
2760 cmdline = ExpandOptions(scr, command);
2762 if (scr->flags.dnd_data_convertion_status || !cmdline) {
2763 if (cmdline)
2764 wfree(cmdline);
2765 if (state)
2766 wfree(state);
2767 return 0;
2770 wtokensplit(cmdline, &argv, &argc);
2772 if (!argc) {
2773 if (cmdline)
2774 wfree(cmdline);
2775 if (state)
2776 wfree(state);
2777 return 0;
2780 if ((pid = fork()) == 0) {
2781 char **args;
2782 int i;
2784 SetupEnvironment(scr);
2786 #ifdef HAVE_SETSID
2787 setsid();
2788 #endif
2790 args = malloc(sizeof(char *) * (argc + 1));
2791 if (!args)
2792 exit(111);
2793 for (i = 0; i < argc; i++) {
2794 args[i] = argv[i];
2796 args[argc] = NULL;
2797 execvp(argv[0], args);
2798 exit(111);
2800 wtokenfree(argv, argc);
2802 if (pid > 0) {
2803 if (!state) {
2804 state = wmalloc(sizeof(WSavedState));
2805 memset(state, 0, sizeof(WSavedState));
2806 state->hidden = -1;
2807 state->miniaturized = -1;
2808 state->shaded = -1;
2809 if (btn->dock == scr->dock || btn->omnipresent)
2810 state->workspace = -1;
2811 else
2812 state->workspace = scr->current_workspace;
2814 wWindowAddSavedState(btn->wm_instance, btn->wm_class, cmdline, pid, state);
2815 wAddDeathHandler(pid, (WDeathHandler *) trackDeadProcess, btn->dock);
2816 } else if (state) {
2817 wfree(state);
2819 wfree(cmdline);
2820 return pid;
2823 void wDockHideIcons(WDock * dock)
2825 int i;
2827 if (dock == NULL)
2828 return;
2830 for (i = 1; i < dock->max_icons; i++) {
2831 if (dock->icon_array[i])
2832 XUnmapWindow(dpy, dock->icon_array[i]->icon->core->window);
2834 dock->mapped = 0;
2836 dockIconPaint(dock->icon_array[0]);
2839 void wDockShowIcons(WDock * dock)
2841 int i, newlevel;
2842 WAppIcon *btn;
2844 if (dock == NULL)
2845 return;
2847 btn = dock->icon_array[0];
2848 moveDock(dock, btn->x_pos, btn->y_pos);
2850 newlevel = dock->lowered ? WMNormalLevel : WMDockLevel;
2851 ChangeStackingLevel(btn->icon->core, newlevel);
2853 for (i = 1; i < dock->max_icons; i++) {
2854 if (dock->icon_array[i]) {
2855 MoveInStackListAbove(dock->icon_array[i]->icon->core, btn->icon->core);
2856 break;
2860 if (!dock->collapsed) {
2861 for (i = 1; i < dock->max_icons; i++) {
2862 if (dock->icon_array[i]) {
2863 XMapWindow(dpy, dock->icon_array[i]->icon->core->window);
2867 dock->mapped = 1;
2869 dockIconPaint(btn);
2872 void wDockLower(WDock * dock)
2874 int i;
2876 for (i = 0; i < dock->max_icons; i++) {
2877 if (dock->icon_array[i])
2878 wLowerFrame(dock->icon_array[i]->icon->core);
2882 void wDockRaise(WDock * dock)
2884 int i;
2886 for (i = dock->max_icons - 1; i >= 0; i--) {
2887 if (dock->icon_array[i])
2888 wRaiseFrame(dock->icon_array[i]->icon->core);
2892 void wDockRaiseLower(WDock * dock)
2894 if (!dock->icon_array[0]->icon->core->stacking->above
2895 || (dock->icon_array[0]->icon->core->stacking->window_level
2896 != dock->icon_array[0]->icon->core->stacking->above->stacking->window_level))
2897 wDockLower(dock);
2898 else
2899 wDockRaise(dock);
2902 void wDockFinishLaunch(WDock * dock, WAppIcon * icon)
2904 icon->launching = 0;
2905 icon->relaunching = 0;
2906 dockIconPaint(icon);
2909 WAppIcon *wDockFindIconForWindow(WDock * dock, Window window)
2911 WAppIcon *icon;
2912 int i;
2914 for (i = 0; i < dock->max_icons; i++) {
2915 icon = dock->icon_array[i];
2916 if (icon && icon->main_window == window)
2917 return icon;
2919 return NULL;
2922 void wDockTrackWindowLaunch(WDock * dock, Window window)
2924 WAppIcon *icon;
2925 char *wm_class, *wm_instance;
2926 int i;
2927 Bool firstPass = True;
2928 Bool found = False;
2929 char *command = NULL;
2931 command = GetCommandForWindow(window);
2933 if (!PropGetWMClass(window, &wm_class, &wm_instance) || (!wm_class && !wm_instance)) {
2935 if (command)
2936 wfree(command);
2937 return;
2940 retry:
2941 for (i = 0; i < dock->max_icons; i++) {
2942 icon = dock->icon_array[i];
2943 if (!icon)
2944 continue;
2946 /* app is already attached to icon */
2947 if (icon->main_window == window) {
2948 found = True;
2949 break;
2952 if ((icon->wm_instance || icon->wm_class)
2953 && (icon->launching || !icon->running)) {
2955 if (icon->wm_instance && wm_instance && strcmp(icon->wm_instance, wm_instance) != 0) {
2956 continue;
2958 if (icon->wm_class && wm_class && strcmp(icon->wm_class, wm_class) != 0) {
2959 continue;
2961 if (firstPass && command && strcmp(icon->command, command) != 0) {
2962 continue;
2965 if (!icon->relaunching) {
2966 WApplication *wapp;
2968 /* Possibly an application that was docked with dockit,
2969 * but the user did not update WMState to indicate that
2970 * it was docked by force */
2971 wapp = wApplicationOf(window);
2972 if (!wapp) {
2973 icon->forced_dock = 1;
2974 icon->running = 0;
2976 if (!icon->forced_dock) {
2977 icon->main_window = window;
2980 found = True;
2981 if (!wPreferences.no_animations && !icon->launching &&
2982 !dock->screen_ptr->flags.startup && !dock->collapsed) {
2983 WAppIcon *aicon;
2984 int x0, y0;
2986 icon->launching = 1;
2987 dockIconPaint(icon);
2989 aicon = wAppIconCreateForDock(dock->screen_ptr, NULL,
2990 wm_instance, wm_class, TILE_NORMAL);
2991 /* XXX: can: aicon->icon == NULL ? */
2992 PlaceIcon(dock->screen_ptr, &x0, &y0, wGetHeadForWindow(aicon->icon->owner));
2993 wAppIconMove(aicon, x0, y0);
2994 /* Should this always be lowered? -Dan */
2995 if (dock->lowered)
2996 wLowerFrame(aicon->icon->core);
2997 XMapWindow(dpy, aicon->icon->core->window);
2998 aicon->launching = 1;
2999 wAppIconPaint(aicon);
3000 SlideWindow(aicon->icon->core->window, x0, y0, icon->x_pos, icon->y_pos);
3001 XUnmapWindow(dpy, aicon->icon->core->window);
3002 wAppIconDestroy(aicon);
3004 wDockFinishLaunch(dock, icon);
3005 break;
3009 if (firstPass && !found) {
3010 firstPass = False;
3011 goto retry;
3014 if (command)
3015 wfree(command);
3017 if (wm_class)
3018 XFree(wm_class);
3019 if (wm_instance)
3020 XFree(wm_instance);
3023 void wClipUpdateForWorkspaceChange(WScreen * scr, int workspace)
3025 if (!wPreferences.flags.noclip) {
3026 scr->clip_icon->dock = scr->workspaces[workspace]->clip;
3027 if (scr->current_workspace != workspace) {
3028 WDock *old_clip = scr->workspaces[scr->current_workspace]->clip;
3029 WAppIconChain *chain = scr->global_icons;
3031 while (chain) {
3032 moveIconBetweenDocks(chain->aicon->dock,
3033 scr->workspaces[workspace]->clip,
3034 chain->aicon, chain->aicon->xindex, chain->aicon->yindex);
3035 if (scr->workspaces[workspace]->clip->collapsed)
3036 XUnmapWindow(dpy, chain->aicon->icon->core->window);
3037 chain = chain->next;
3040 wDockHideIcons(old_clip);
3041 if (old_clip->auto_raise_lower) {
3042 if (old_clip->auto_raise_magic) {
3043 WMDeleteTimerHandler(old_clip->auto_raise_magic);
3044 old_clip->auto_raise_magic = NULL;
3046 wDockLower(old_clip);
3048 if (old_clip->auto_collapse) {
3049 if (old_clip->auto_expand_magic) {
3050 WMDeleteTimerHandler(old_clip->auto_expand_magic);
3051 old_clip->auto_expand_magic = NULL;
3053 old_clip->collapsed = 1;
3055 wDockShowIcons(scr->workspaces[workspace]->clip);
3057 if (scr->flags.clip_balloon_mapped)
3058 showClipBalloon(scr->clip_icon->dock, workspace);
3062 static void trackDeadProcess(pid_t pid, unsigned char status, WDock * dock)
3064 WAppIcon *icon;
3065 int i;
3067 for (i = 0; i < dock->max_icons; i++) {
3068 icon = dock->icon_array[i];
3069 if (!icon)
3070 continue;
3072 if (icon->launching && icon->pid == pid) {
3073 if (!icon->relaunching) {
3074 icon->running = 0;
3075 icon->main_window = None;
3077 wDockFinishLaunch(dock, icon);
3078 icon->pid = 0;
3079 if (status == 111) {
3080 char msg[PATH_MAX];
3081 char *cmd;
3083 #ifdef XDND
3084 if (icon->drop_launch)
3085 cmd = icon->dnd_command;
3086 else
3087 #endif
3088 if (icon->paste_launch)
3089 cmd = icon->paste_command;
3090 else
3091 cmd = icon->command;
3093 snprintf(msg, sizeof(msg), _("Could not execute command \"%s\""), cmd);
3095 wMessageDialog(dock->screen_ptr, _("Error"), msg, _("OK"), NULL, NULL);
3097 break;
3102 static void toggleLowered(WDock * dock)
3104 WAppIcon *tmp;
3105 int newlevel, i;
3107 /* lower/raise Dock */
3108 if (!dock->lowered) {
3109 newlevel = WMNormalLevel;
3110 dock->lowered = 1;
3111 } else {
3112 newlevel = WMDockLevel;
3113 dock->lowered = 0;
3116 for (i = 0; i < dock->max_icons; i++) {
3117 tmp = dock->icon_array[i];
3118 if (!tmp)
3119 continue;
3121 ChangeStackingLevel(tmp->icon->core, newlevel);
3122 if (dock->lowered)
3123 wLowerFrame(tmp->icon->core);
3126 if (dock->type == WM_DOCK)
3127 wScreenUpdateUsableArea(dock->screen_ptr);
3130 static void toggleCollapsed(WDock * dock)
3132 if (dock->collapsed) {
3133 dock->collapsed = 0;
3134 wDockShowIcons(dock);
3135 } else {
3136 dock->collapsed = 1;
3137 wDockHideIcons(dock);
3141 static void openDockMenu(WDock * dock, WAppIcon * aicon, XEvent * event)
3143 WScreen *scr = dock->screen_ptr;
3144 WObjDescriptor *desc;
3145 WMenuEntry *entry;
3146 WApplication *wapp = NULL;
3147 int index = 0;
3148 int x_pos;
3149 int n_selected;
3150 int appIsRunning = aicon->running && aicon->icon && aicon->icon->owner;
3152 if (dock->type == WM_DOCK) {
3153 /* keep on top */
3154 entry = dock->menu->entries[index];
3155 entry->flags.indicator_on = !dock->lowered;
3156 entry->clientdata = dock;
3157 dock->menu->flags.realized = 0;
3158 } else {
3159 /* clip options */
3160 if (scr->clip_options)
3161 updateClipOptionsMenu(scr->clip_options, dock);
3163 n_selected = numberOfSelectedIcons(dock);
3165 /* Rename Workspace */
3166 entry = dock->menu->entries[++index];
3167 if (aicon == scr->clip_icon) {
3168 entry->callback = renameCallback;
3169 entry->clientdata = dock;
3170 entry->flags.indicator = 0;
3171 entry->text = _("Rename Workspace");
3172 } else {
3173 entry->callback = omnipresentCallback;
3174 entry->clientdata = aicon;
3175 if (n_selected > 0) {
3176 entry->flags.indicator = 0;
3177 entry->text = _("Toggle Omnipresent");
3178 } else {
3179 entry->flags.indicator = 1;
3180 entry->flags.indicator_on = aicon->omnipresent;
3181 entry->flags.indicator_type = MI_CHECK;
3182 entry->text = _("Omnipresent");
3186 /* select/unselect icon */
3187 entry = dock->menu->entries[++index];
3188 entry->clientdata = aicon;
3189 entry->flags.indicator_on = aicon->icon->selected;
3190 wMenuSetEnabled(dock->menu, index, aicon != scr->clip_icon);
3192 /* select/unselect all icons */
3193 entry = dock->menu->entries[++index];
3194 entry->clientdata = aicon;
3195 if (n_selected > 0)
3196 entry->text = _("Unselect All Icons");
3197 else
3198 entry->text = _("Select All Icons");
3199 wMenuSetEnabled(dock->menu, index, dock->icon_count > 1);
3201 /* keep icon(s) */
3202 entry = dock->menu->entries[++index];
3203 entry->clientdata = aicon;
3204 if (n_selected > 1)
3205 entry->text = _("Keep Icons");
3206 else
3207 entry->text = _("Keep Icon");
3208 wMenuSetEnabled(dock->menu, index, dock->icon_count > 1);
3210 /* this is the workspace submenu part */
3211 entry = dock->menu->entries[++index];
3212 if (n_selected > 1)
3213 entry->text = _("Move Icons To");
3214 else
3215 entry->text = _("Move Icon To");
3216 if (scr->clip_submenu)
3217 updateWorkspaceMenu(scr->clip_submenu, aicon);
3218 wMenuSetEnabled(dock->menu, index, !aicon->omnipresent);
3220 /* remove icon(s) */
3221 entry = dock->menu->entries[++index];
3222 entry->clientdata = aicon;
3223 if (n_selected > 1)
3224 entry->text = _("Remove Icons");
3225 else
3226 entry->text = _("Remove Icon");
3227 wMenuSetEnabled(dock->menu, index, dock->icon_count > 1);
3229 /* attract icon(s) */
3230 entry = dock->menu->entries[++index];
3231 entry->clientdata = aicon;
3233 dock->menu->flags.realized = 0;
3234 wMenuRealize(dock->menu);
3237 if (aicon->icon->owner) {
3238 wapp = wApplicationOf(aicon->icon->owner->main_window);
3239 } else {
3240 wapp = NULL;
3243 /* launch */
3244 entry = dock->menu->entries[++index];
3245 entry->clientdata = aicon;
3246 wMenuSetEnabled(dock->menu, index, aicon->command != NULL);
3248 /* unhide here */
3249 entry = dock->menu->entries[++index];
3250 entry->clientdata = aicon;
3251 if (wapp && wapp->flags.hidden) {
3252 entry->text = _("Unhide Here");
3253 } else {
3254 entry->text = _("Bring Here");
3256 wMenuSetEnabled(dock->menu, index, appIsRunning);
3258 /* hide */
3259 entry = dock->menu->entries[++index];
3260 entry->clientdata = aicon;
3261 if (wapp && wapp->flags.hidden) {
3262 entry->text = _("Unhide");
3263 } else {
3264 entry->text = _("Hide");
3266 wMenuSetEnabled(dock->menu, index, appIsRunning);
3268 /* settings */
3269 entry = dock->menu->entries[++index];
3270 entry->clientdata = aicon;
3271 wMenuSetEnabled(dock->menu, index, !aicon->editing && !wPreferences.flags.noupdates);
3273 /* kill */
3274 entry = dock->menu->entries[++index];
3275 entry->clientdata = aicon;
3276 wMenuSetEnabled(dock->menu, index, appIsRunning);
3278 if (!dock->menu->flags.realized)
3279 wMenuRealize(dock->menu);
3281 if (dock->type == WM_CLIP) {
3282 /*x_pos = event->xbutton.x_root+2; */
3283 x_pos = event->xbutton.x_root - dock->menu->frame->core->width / 2 - 1;
3284 if (x_pos < 0) {
3285 x_pos = 0;
3286 } else if (x_pos + dock->menu->frame->core->width > scr->scr_width - 2) {
3287 x_pos = scr->scr_width - dock->menu->frame->core->width - 4;
3289 } else {
3290 x_pos = dock->on_right_side ? scr->scr_width - dock->menu->frame->core->width - 3 : 0;
3293 wMenuMapAt(dock->menu, x_pos, event->xbutton.y_root + 2, False);
3295 /* allow drag select */
3296 event->xany.send_event = True;
3297 desc = &dock->menu->menu->descriptor;
3298 (*desc->handle_mousedown) (desc, event);
3301 /******************************************************************/
3302 static void iconDblClick(WObjDescriptor * desc, XEvent * event)
3304 WAppIcon *btn = desc->parent;
3305 WDock *dock = btn->dock;
3306 WApplication *wapp = NULL;
3307 int unhideHere = 0;
3309 if (btn->icon->owner && !(event->xbutton.state & ControlMask)) {
3310 wapp = wApplicationOf(btn->icon->owner->main_window);
3312 assert(wapp != NULL);
3314 unhideHere = (event->xbutton.state & ShiftMask);
3316 /* go to the last workspace that the user worked on the app */
3317 if (wapp->last_workspace != dock->screen_ptr->current_workspace && !unhideHere) {
3318 wWorkspaceChange(dock->screen_ptr, wapp->last_workspace);
3321 wUnhideApplication(wapp, event->xbutton.button == Button2, unhideHere);
3323 if (event->xbutton.state & MOD_MASK) {
3324 wHideOtherApplications(btn->icon->owner);
3326 } else {
3327 if (event->xbutton.button == Button1) {
3329 if (event->xbutton.state & MOD_MASK) {
3330 /* raise/lower dock */
3331 toggleLowered(dock);
3332 } else if (btn == dock->screen_ptr->clip_icon) {
3333 if (getClipButton(event->xbutton.x, event->xbutton.y) == CLIP_IDLE)
3334 toggleCollapsed(dock);
3335 else
3336 handleClipChangeWorkspace(dock->screen_ptr, event);
3337 } else if (btn->command) {
3338 if (!btn->launching && (!btn->running || (event->xbutton.state & ControlMask))) {
3339 launchDockedApplication(btn, False);
3341 } else if (btn->xindex == 0 && btn->yindex == 0 && btn->dock->type == WM_DOCK) {
3342 wShowGNUstepPanel(dock->screen_ptr);
3348 static void handleDockMove(WDock * dock, WAppIcon * aicon, XEvent * event)
3350 WScreen *scr = dock->screen_ptr;
3351 int ofs_x = event->xbutton.x, ofs_y = event->xbutton.y;
3352 int x, y;
3353 XEvent ev;
3354 int grabbed = 0, swapped = 0, done;
3355 Pixmap ghost = None;
3356 int superfluous = wPreferences.superfluous; /* we catch it to avoid problems */
3358 if (XGrabPointer(dpy, aicon->icon->core->window, True, ButtonMotionMask
3359 | ButtonReleaseMask | ButtonPressMask, GrabModeAsync,
3360 GrabModeAsync, None, None, CurrentTime) != GrabSuccess) {
3361 wwarning("pointer grab failed for dock move");
3363 y = 0;
3364 for (x = 0; x < dock->max_icons; x++) {
3365 if (dock->icon_array[x] != NULL && dock->icon_array[x]->yindex > y)
3366 y = dock->icon_array[x]->yindex;
3368 y++;
3369 XResizeWindow(dpy, scr->dock_shadow, ICON_SIZE, ICON_SIZE * y);
3371 done = 0;
3372 while (!done) {
3373 WMMaskEvent(dpy, PointerMotionMask | ButtonReleaseMask | ButtonPressMask
3374 | ButtonMotionMask | ExposureMask | EnterWindowMask, &ev);
3375 switch (ev.type) {
3376 case Expose:
3377 WMHandleEvent(&ev);
3378 break;
3380 case EnterNotify:
3381 /* It means the cursor moved so fast that it entered
3382 * something else (if moving slowly, it would have
3383 * stayed in the dock that is being moved. Ignore such
3384 * "spurious" EnterNotifiy's */
3385 break;
3387 case MotionNotify:
3388 if (!grabbed) {
3389 if (abs(ofs_x - ev.xmotion.x) >= MOVE_THRESHOLD
3390 || abs(ofs_y - ev.xmotion.y) >= MOVE_THRESHOLD) {
3391 XChangeActivePointerGrab(dpy, ButtonMotionMask
3392 | ButtonReleaseMask | ButtonPressMask,
3393 wCursor[WCUR_MOVE], CurrentTime);
3394 grabbed = 1;
3396 break;
3398 if (dock->type == WM_CLIP) {
3399 x = ev.xmotion.x_root - ofs_x;
3400 y = ev.xmotion.y_root - ofs_y;
3401 wScreenKeepInside(scr, &x, &y, ICON_SIZE, ICON_SIZE);
3403 moveDock(dock, x, y);
3404 } else {
3405 /* move vertically if pointer is inside the dock */
3406 if ((dock->on_right_side && ev.xmotion.x_root >= dock->x_pos - ICON_SIZE)
3407 || (!dock->on_right_side && ev.xmotion.x_root <= dock->x_pos + ICON_SIZE * 2)) {
3409 x = ev.xmotion.x_root - ofs_x;
3410 y = ev.xmotion.y_root - ofs_y;
3411 wScreenKeepInside(scr, &x, &y, ICON_SIZE, ICON_SIZE);
3412 moveDock(dock, dock->x_pos, y);
3414 /* move horizontally to change sides */
3415 x = ev.xmotion.x_root - ofs_x;
3416 if (!dock->on_right_side) {
3418 /* is on left */
3420 if (ev.xmotion.x_root > dock->x_pos + ICON_SIZE * 2) {
3421 XMoveWindow(dpy, scr->dock_shadow, scr->scr_width - ICON_SIZE
3422 - DOCK_EXTRA_SPACE - 1, dock->y_pos);
3423 if (superfluous && ghost == None) {
3424 ghost = MakeGhostDock(dock, dock->x_pos,
3425 scr->scr_width - ICON_SIZE
3426 - DOCK_EXTRA_SPACE - 1, dock->y_pos);
3427 XSetWindowBackgroundPixmap(dpy, scr->dock_shadow, ghost);
3428 XClearWindow(dpy, scr->dock_shadow);
3430 XMapRaised(dpy, scr->dock_shadow);
3431 swapped = 1;
3432 } else {
3433 if (superfluous && ghost != None) {
3434 XFreePixmap(dpy, ghost);
3435 ghost = None;
3437 XUnmapWindow(dpy, scr->dock_shadow);
3438 swapped = 0;
3440 } else {
3441 /* is on right */
3442 if (ev.xmotion.x_root < dock->x_pos - ICON_SIZE) {
3443 XMoveWindow(dpy, scr->dock_shadow, DOCK_EXTRA_SPACE, dock->y_pos);
3444 if (superfluous && ghost == None) {
3445 ghost = MakeGhostDock(dock, dock->x_pos,
3446 DOCK_EXTRA_SPACE, dock->y_pos);
3447 XSetWindowBackgroundPixmap(dpy, scr->dock_shadow, ghost);
3448 XClearWindow(dpy, scr->dock_shadow);
3450 XMapRaised(dpy, scr->dock_shadow);
3451 swapped = -1;
3452 } else {
3453 XUnmapWindow(dpy, scr->dock_shadow);
3454 swapped = 0;
3455 if (superfluous && ghost != None) {
3456 XFreePixmap(dpy, ghost);
3457 ghost = None;
3462 break;
3464 case ButtonPress:
3465 break;
3467 case ButtonRelease:
3468 if (ev.xbutton.button != event->xbutton.button)
3469 break;
3470 XUngrabPointer(dpy, CurrentTime);
3471 XUnmapWindow(dpy, scr->dock_shadow);
3472 XResizeWindow(dpy, scr->dock_shadow, ICON_SIZE, ICON_SIZE);
3473 if (dock->type == WM_DOCK) {
3474 if (swapped != 0) {
3475 if (swapped > 0)
3476 dock->on_right_side = 1;
3477 else
3478 dock->on_right_side = 0;
3479 swapDock(dock);
3480 wArrangeIcons(scr, False);
3483 done = 1;
3484 break;
3487 if (superfluous) {
3488 if (ghost != None)
3489 XFreePixmap(dpy, ghost);
3490 XSetWindowBackground(dpy, scr->dock_shadow, scr->white_pixel);
3494 static Bool handleIconMove(WDock *dock, WAppIcon *aicon, XEvent *event)
3496 WScreen *scr = dock->screen_ptr;
3497 Window wins[2];
3498 WIcon *icon = aicon->icon;
3499 WDock *dock2 = NULL, *last_dock = dock, *clip = NULL;
3500 int ondock, grabbed = 0, change_dock = 0, collapsed = 0;
3501 XEvent ev;
3502 int x = aicon->x_pos, y = aicon->y_pos;
3503 int ofs_x = event->xbutton.x, ofs_y = event->xbutton.y;
3504 int shad_x = x, shad_y = y;
3505 int ix = aicon->xindex, iy = aicon->yindex;
3506 int tmp;
3507 Pixmap ghost = None;
3508 Bool docked;
3509 int superfluous = wPreferences.superfluous; /* we catch it to avoid problems */
3510 int omnipresent = aicon->omnipresent; /* this must be cached!!! */
3511 Bool hasMoved = False;
3513 if (wPreferences.flags.noupdates)
3514 return hasMoved;
3516 if (XGrabPointer(dpy, icon->core->window, True, ButtonMotionMask
3517 | ButtonReleaseMask | ButtonPressMask, GrabModeAsync,
3518 GrabModeAsync, None, None, CurrentTime) != GrabSuccess) {
3521 if (!(event->xbutton.state & MOD_MASK))
3522 wRaiseFrame(icon->core);
3524 if (!wPreferences.flags.noclip)
3525 clip = scr->workspaces[scr->current_workspace]->clip;
3527 if (dock == scr->dock && !wPreferences.flags.noclip)
3528 dock2 = clip;
3529 else if (dock != scr->dock && !wPreferences.flags.nodock)
3530 dock2 = scr->dock;
3532 wins[0] = icon->core->window;
3533 wins[1] = scr->dock_shadow;
3534 XRestackWindows(dpy, wins, 2);
3535 XMoveResizeWindow(dpy, scr->dock_shadow, aicon->x_pos, aicon->y_pos, ICON_SIZE, ICON_SIZE);
3536 if (superfluous) {
3537 if (icon->pixmap != None)
3538 ghost = MakeGhostIcon(scr, icon->pixmap);
3539 else
3540 ghost = MakeGhostIcon(scr, icon->core->window);
3542 XSetWindowBackgroundPixmap(dpy, scr->dock_shadow, ghost);
3543 XClearWindow(dpy, scr->dock_shadow);
3545 XMapWindow(dpy, scr->dock_shadow);
3547 ondock = 1;
3549 while (1) {
3550 XMaskEvent(dpy, PointerMotionMask | ButtonReleaseMask | ButtonPressMask
3551 | ButtonMotionMask | ExposureMask, &ev);
3552 switch (ev.type) {
3553 case Expose:
3554 WMHandleEvent(&ev);
3555 break;
3557 case MotionNotify:
3558 hasMoved = True;
3559 if (!grabbed) {
3560 if (abs(ofs_x - ev.xmotion.x) >= MOVE_THRESHOLD
3561 || abs(ofs_y - ev.xmotion.y) >= MOVE_THRESHOLD) {
3562 XChangeActivePointerGrab(dpy, ButtonMotionMask
3563 | ButtonReleaseMask | ButtonPressMask,
3564 wCursor[WCUR_MOVE], CurrentTime);
3565 grabbed = 1;
3566 } else {
3567 break;
3571 if (omnipresent) {
3572 int i;
3573 for (i = 0; i < scr->workspace_count; i++) {
3574 if (i == scr->current_workspace)
3575 continue;
3576 wDockShowIcons(scr->workspaces[i]->clip);
3580 x = ev.xmotion.x_root - ofs_x;
3581 y = ev.xmotion.y_root - ofs_y;
3582 tmp = wDockSnapIcon(dock, aicon, x, y, &ix, &iy, True);
3583 if (tmp && dock2) {
3584 change_dock = 0;
3585 if (last_dock != dock && collapsed) {
3586 last_dock->collapsed = 1;
3587 wDockHideIcons(last_dock);
3588 collapsed = 0;
3590 if (!collapsed && (collapsed = dock->collapsed)) {
3591 dock->collapsed = 0;
3592 wDockShowIcons(dock);
3594 if (dock->auto_raise_lower)
3595 wDockRaise(dock);
3596 last_dock = dock;
3597 } else if (dock2) {
3598 tmp = wDockSnapIcon(dock2, aicon, x, y, &ix, &iy, False);
3599 if (tmp) {
3600 change_dock = 1;
3601 if (last_dock != dock2 && collapsed) {
3602 last_dock->collapsed = 1;
3603 wDockHideIcons(last_dock);
3604 collapsed = 0;
3606 if (!collapsed && (collapsed = dock2->collapsed)) {
3607 dock2->collapsed = 0;
3608 wDockShowIcons(dock2);
3610 if (dock2->auto_raise_lower)
3611 wDockRaise(dock2);
3612 last_dock = dock2;
3615 if (aicon->launching || aicon->lock || (aicon->running && !(ev.xmotion.state & MOD_MASK))
3616 || (!aicon->running && tmp)) {
3617 shad_x = last_dock->x_pos + ix * wPreferences.icon_size;
3618 shad_y = last_dock->y_pos + iy * wPreferences.icon_size;
3620 XMoveWindow(dpy, scr->dock_shadow, shad_x, shad_y);
3622 if (!ondock) {
3623 XMapWindow(dpy, scr->dock_shadow);
3625 ondock = 1;
3626 } else {
3627 if (ondock) {
3628 XUnmapWindow(dpy, scr->dock_shadow);
3630 ondock = 0;
3632 XMoveWindow(dpy, icon->core->window, x, y);
3633 break;
3635 case ButtonPress:
3636 break;
3638 case ButtonRelease:
3639 if (ev.xbutton.button != event->xbutton.button)
3640 break;
3641 XUngrabPointer(dpy, CurrentTime);
3642 if (ondock) {
3643 SlideWindow(icon->core->window, x, y, shad_x, shad_y);
3644 XUnmapWindow(dpy, scr->dock_shadow);
3645 if (!change_dock) {
3646 reattachIcon(dock, aicon, ix, iy);
3647 if (clip && dock != clip && clip->auto_raise_lower)
3648 wDockLower(clip);
3649 } else {
3650 docked = moveIconBetweenDocks(dock, dock2, aicon, ix, iy);
3651 if (!docked) {
3652 /* Slide it back if dock rejected it */
3653 SlideWindow(icon->core->window, x, y, aicon->x_pos, aicon->y_pos);
3654 reattachIcon(dock, aicon, aicon->xindex, aicon->yindex);
3656 if (last_dock->type == WM_CLIP && last_dock->auto_collapse) {
3657 collapsed = 0;
3660 } else {
3661 aicon->x_pos = x;
3662 aicon->y_pos = y;
3663 if (superfluous) {
3664 if (!aicon->running && !wPreferences.no_animations) {
3665 /* We need to deselect it, even if is deselected in
3666 * wDockDetach(), because else DoKaboom() will fail.
3668 if (aicon->icon->selected)
3669 wIconSelect(aicon->icon);
3671 DoKaboom(scr, aicon->icon->core->window, x, y);
3674 if (clip && clip->auto_raise_lower)
3675 wDockLower(clip);
3676 wDockDetach(dock, aicon);
3678 if (collapsed) {
3679 last_dock->collapsed = 1;
3680 wDockHideIcons(last_dock);
3681 collapsed = 0;
3683 if (superfluous) {
3684 if (ghost != None)
3685 XFreePixmap(dpy, ghost);
3686 XSetWindowBackground(dpy, scr->dock_shadow, scr->white_pixel);
3688 if (omnipresent) {
3689 int i;
3690 for (i = 0; i < scr->workspace_count; i++) {
3691 if (i == scr->current_workspace)
3692 continue;
3693 wDockHideIcons(scr->workspaces[i]->clip);
3696 return hasMoved;;
3699 return False; /* never reached */
3702 static int getClipButton(int px, int py)
3704 int pt = (CLIP_BUTTON_SIZE + 2) * ICON_SIZE / 64;
3706 if (px < 0 || py < 0 || px >= ICON_SIZE || py >= ICON_SIZE)
3707 return CLIP_IDLE;
3709 if (py <= pt - ((int)ICON_SIZE - 1 - px))
3710 return CLIP_FORWARD;
3711 else if (px <= pt - ((int)ICON_SIZE - 1 - py))
3712 return CLIP_REWIND;
3714 return CLIP_IDLE;
3717 static void handleClipChangeWorkspace(WScreen * scr, XEvent * event)
3719 XEvent ev;
3720 int done, direction, new_ws;
3721 int new_dir;
3722 WDock *clip = scr->clip_icon->dock;
3724 direction = getClipButton(event->xbutton.x, event->xbutton.y);
3726 clip->lclip_button_pushed = direction == CLIP_REWIND;
3727 clip->rclip_button_pushed = direction == CLIP_FORWARD;
3729 wClipIconPaint(scr->clip_icon);
3730 done = 0;
3731 while (!done) {
3732 WMMaskEvent(dpy, ExposureMask | ButtonMotionMask | ButtonReleaseMask | ButtonPressMask, &ev);
3733 switch (ev.type) {
3734 case Expose:
3735 WMHandleEvent(&ev);
3736 break;
3738 case MotionNotify:
3739 new_dir = getClipButton(ev.xmotion.x, ev.xmotion.y);
3740 if (new_dir != direction) {
3741 direction = new_dir;
3742 clip->lclip_button_pushed = direction == CLIP_REWIND;
3743 clip->rclip_button_pushed = direction == CLIP_FORWARD;
3744 wClipIconPaint(scr->clip_icon);
3746 break;
3748 case ButtonPress:
3749 break;
3751 case ButtonRelease:
3752 if (ev.xbutton.button == event->xbutton.button)
3753 done = 1;
3757 clip->lclip_button_pushed = 0;
3758 clip->rclip_button_pushed = 0;
3760 new_ws = wPreferences.ws_advance || (event->xbutton.state & ControlMask);
3762 if (direction == CLIP_FORWARD) {
3763 if (scr->current_workspace < scr->workspace_count - 1)
3764 wWorkspaceChange(scr, scr->current_workspace + 1);
3765 else if (new_ws && scr->current_workspace < MAX_WORKSPACES - 1)
3766 wWorkspaceChange(scr, scr->current_workspace + 1);
3767 else if (wPreferences.ws_cycle)
3768 wWorkspaceChange(scr, 0);
3769 } else if (direction == CLIP_REWIND) {
3770 if (scr->current_workspace > 0)
3771 wWorkspaceChange(scr, scr->current_workspace - 1);
3772 else if (scr->current_workspace == 0 && wPreferences.ws_cycle)
3773 wWorkspaceChange(scr, scr->workspace_count - 1);
3776 wClipIconPaint(scr->clip_icon);
3779 static void iconMouseDown(WObjDescriptor * desc, XEvent * event)
3781 WAppIcon *aicon = desc->parent;
3782 WDock *dock = aicon->dock;
3783 WScreen *scr = aicon->icon->core->screen_ptr;
3785 if (aicon->editing || WCHECK_STATE(WSTATE_MODAL))
3786 return;
3788 scr->last_dock = dock;
3790 if (dock->menu->flags.mapped)
3791 wMenuUnmap(dock->menu);
3793 if (IsDoubleClick(scr, event)) {
3794 /* double-click was not in the main clip icon */
3795 if (dock->type != WM_CLIP || aicon->xindex != 0 || aicon->yindex != 0
3796 || getClipButton(event->xbutton.x, event->xbutton.y) == CLIP_IDLE) {
3797 iconDblClick(desc, event);
3798 return;
3802 if (dock->type == WM_CLIP && scr->flags.clip_balloon_mapped) {
3803 XUnmapWindow(dpy, scr->clip_balloon);
3804 scr->flags.clip_balloon_mapped = 0;
3806 if (event->xbutton.button == Button1) {
3807 if (event->xbutton.state & MOD_MASK)
3808 wDockLower(dock);
3809 else
3810 wDockRaise(dock);
3812 if ((event->xbutton.state & ShiftMask) && aicon != scr->clip_icon && dock->type != WM_DOCK) {
3813 wIconSelect(aicon->icon);
3814 return;
3817 if (aicon->yindex == 0 && aicon->xindex == 0) {
3818 if (getClipButton(event->xbutton.x, event->xbutton.y) != CLIP_IDLE
3819 && dock->type == WM_CLIP)
3820 handleClipChangeWorkspace(scr, event);
3821 else
3822 handleDockMove(dock, aicon, event);
3823 } else {
3824 Bool hasMoved = handleIconMove(dock, aicon, event);
3825 if (wPreferences.single_click && !hasMoved)
3826 iconDblClick(desc, event);
3828 } else if (event->xbutton.button == Button2 && dock->type == WM_CLIP && aicon == scr->clip_icon) {
3829 if (!scr->clip_ws_menu) {
3830 scr->clip_ws_menu = wWorkspaceMenuMake(scr, False);
3832 if (scr->clip_ws_menu) {
3833 WMenu *wsMenu = scr->clip_ws_menu;
3834 int xpos;
3836 wWorkspaceMenuUpdate(scr, wsMenu);
3838 xpos = event->xbutton.x_root - wsMenu->frame->core->width / 2 - 1;
3839 if (xpos < 0) {
3840 xpos = 0;
3841 } else if (xpos + wsMenu->frame->core->width > scr->scr_width - 2) {
3842 xpos = scr->scr_width - wsMenu->frame->core->width - 4;
3844 wMenuMapAt(wsMenu, xpos, event->xbutton.y_root + 2, False);
3846 desc = &wsMenu->menu->descriptor;
3847 event->xany.send_event = True;
3848 (*desc->handle_mousedown) (desc, event);
3850 } else if (event->xbutton.button == Button2 && dock->type == WM_CLIP &&
3851 (event->xbutton.state & ShiftMask) && aicon != scr->clip_icon) {
3852 wClipMakeIconOmnipresent(aicon, !aicon->omnipresent);
3853 } else if (event->xbutton.button == Button3) {
3854 if (event->xbutton.send_event &&
3855 XGrabPointer(dpy, aicon->icon->core->window, True, ButtonMotionMask
3856 | ButtonReleaseMask | ButtonPressMask, GrabModeAsync,
3857 GrabModeAsync, None, None, CurrentTime) != GrabSuccess) {
3858 wwarning("pointer grab failed for dockicon menu");
3859 return;
3862 openDockMenu(dock, aicon, event);
3863 } else if (event->xbutton.button == Button2) {
3864 WAppIcon *btn = desc->parent;
3866 if (!btn->launching && (!btn->running || (event->xbutton.state & ControlMask))) {
3867 launchDockedApplication(btn, True);
3872 static void showClipBalloon(WDock * dock, int workspace)
3874 int w, h;
3875 int x, y;
3876 WScreen *scr = dock->screen_ptr;
3877 char *text;
3878 Window stack[2];
3880 scr->flags.clip_balloon_mapped = 1;
3881 XMapWindow(dpy, scr->clip_balloon);
3883 text = scr->workspaces[workspace]->name;
3885 w = WMWidthOfString(scr->clip_title_font, text, strlen(text));
3887 h = WMFontHeight(scr->clip_title_font);
3888 XResizeWindow(dpy, scr->clip_balloon, w, h);
3890 x = dock->x_pos + CLIP_BUTTON_SIZE * ICON_SIZE / 64;
3891 y = dock->y_pos + ICON_SIZE - WMFontHeight(scr->clip_title_font) - 3;
3893 if (x + w > scr->scr_width) {
3894 x = scr->scr_width - w;
3895 if (dock->y_pos + ICON_SIZE + h > scr->scr_height)
3896 y = dock->y_pos - h - 1;
3897 else
3898 y = dock->y_pos + ICON_SIZE;
3899 XRaiseWindow(dpy, scr->clip_balloon);
3900 } else {
3901 stack[0] = scr->clip_icon->icon->core->window;
3902 stack[1] = scr->clip_balloon;
3903 XRestackWindows(dpy, stack, 2);
3905 XMoveWindow(dpy, scr->clip_balloon, x, y);
3906 XClearWindow(dpy, scr->clip_balloon);
3907 WMDrawString(scr->wmscreen, scr->clip_balloon,
3908 scr->clip_title_color[CLIP_NORMAL], scr->clip_title_font, 0, 0, text, strlen(text));
3911 static void clipEnterNotify(WObjDescriptor * desc, XEvent * event)
3913 WAppIcon *btn = (WAppIcon *) desc->parent;
3914 WDock *dock;
3915 WScreen *scr;
3917 assert(event->type == EnterNotify);
3919 if (desc->parent_type != WCLASS_DOCK_ICON)
3920 return;
3922 scr = btn->icon->core->screen_ptr;
3923 if (!btn->omnipresent)
3924 dock = btn->dock;
3925 else
3926 dock = scr->workspaces[scr->current_workspace]->clip;
3928 if (!dock || dock->type != WM_CLIP)
3929 return;
3931 /* The auto raise/lower code */
3932 if (dock->auto_lower_magic) {
3933 WMDeleteTimerHandler(dock->auto_lower_magic);
3934 dock->auto_lower_magic = NULL;
3936 if (dock->auto_raise_lower && !dock->auto_raise_magic) {
3937 dock->auto_raise_magic = WMAddTimerHandler(AUTO_RAISE_DELAY, clipAutoRaise, (void *)dock);
3940 /* The auto expand/collapse code */
3941 if (dock->auto_collapse_magic) {
3942 WMDeleteTimerHandler(dock->auto_collapse_magic);
3943 dock->auto_collapse_magic = NULL;
3945 if (dock->auto_collapse && !dock->auto_expand_magic) {
3946 dock->auto_expand_magic = WMAddTimerHandler(AUTO_EXPAND_DELAY, clipAutoExpand, (void *)dock);
3949 if (btn->xindex == 0 && btn->yindex == 0)
3950 showClipBalloon(dock, dock->screen_ptr->current_workspace);
3951 else {
3952 if (dock->screen_ptr->flags.clip_balloon_mapped) {
3953 XUnmapWindow(dpy, dock->screen_ptr->clip_balloon);
3954 dock->screen_ptr->flags.clip_balloon_mapped = 0;
3959 static void clipLeave(WDock * dock)
3961 XEvent event;
3962 WObjDescriptor *desc = NULL;
3964 if (!dock || dock->type != WM_CLIP)
3965 return;
3967 if (XCheckTypedEvent(dpy, EnterNotify, &event) != False) {
3968 if (XFindContext(dpy, event.xcrossing.window, wWinContext,
3969 (XPointer *) & desc) != XCNOENT
3970 && desc && desc->parent_type == WCLASS_DOCK_ICON
3971 && ((WAppIcon *) desc->parent)->dock && ((WAppIcon *) desc->parent)->dock->type == WM_CLIP) {
3972 /* We didn't left the Clip yet */
3973 XPutBackEvent(dpy, &event);
3974 return;
3977 XPutBackEvent(dpy, &event);
3978 } else {
3979 /* We entered a withdrawn window, so we're still in Clip */
3980 return;
3983 if (dock->auto_raise_magic) {
3984 WMDeleteTimerHandler(dock->auto_raise_magic);
3985 dock->auto_raise_magic = NULL;
3987 if (dock->auto_raise_lower && !dock->auto_lower_magic) {
3988 dock->auto_lower_magic = WMAddTimerHandler(AUTO_LOWER_DELAY, clipAutoLower, (void *)dock);
3991 if (dock->auto_expand_magic) {
3992 WMDeleteTimerHandler(dock->auto_expand_magic);
3993 dock->auto_expand_magic = NULL;
3995 if (dock->auto_collapse && !dock->auto_collapse_magic) {
3996 dock->auto_collapse_magic = WMAddTimerHandler(AUTO_COLLAPSE_DELAY, clipAutoCollapse, (void *)dock);
4000 static void clipLeaveNotify(WObjDescriptor * desc, XEvent * event)
4002 WAppIcon *btn = (WAppIcon *) desc->parent;
4004 assert(event->type == LeaveNotify);
4006 if (desc->parent_type != WCLASS_DOCK_ICON)
4007 return;
4009 clipLeave(btn->dock);
4012 static void clipAutoCollapse(void *cdata)
4014 WDock *dock = (WDock *) cdata;
4016 if (dock->type != WM_CLIP)
4017 return;
4019 if (dock->auto_collapse) {
4020 dock->collapsed = 1;
4021 wDockHideIcons(dock);
4023 dock->auto_collapse_magic = NULL;
4026 static void clipAutoExpand(void *cdata)
4028 WDock *dock = (WDock *) cdata;
4030 if (dock->type != WM_CLIP)
4031 return;
4033 if (dock->auto_collapse) {
4034 dock->collapsed = 0;
4035 wDockShowIcons(dock);
4037 dock->auto_expand_magic = NULL;
4040 static void clipAutoLower(void *cdata)
4042 WDock *dock = (WDock *) cdata;
4044 if (dock->type != WM_CLIP)
4045 return;
4047 if (dock->auto_raise_lower)
4048 wDockLower(dock);
4050 dock->auto_lower_magic = NULL;
4053 static void clipAutoRaise(void *cdata)
4055 WDock *dock = (WDock *) cdata;
4057 if (dock->type != WM_CLIP)
4058 return;
4060 if (dock->auto_raise_lower)
4061 wDockRaise(dock);
4063 if (dock->screen_ptr->flags.clip_balloon_mapped) {
4064 showClipBalloon(dock, dock->screen_ptr->current_workspace);
4067 dock->auto_raise_magic = NULL;
4070 static Bool iconCanBeOmnipresent(WAppIcon * aicon)
4072 WScreen *scr = aicon->icon->core->screen_ptr;
4073 WDock *clip;
4074 WAppIcon *btn;
4075 int i, j;
4077 for (i = 0; i < scr->workspace_count; i++) {
4078 clip = scr->workspaces[i]->clip;
4080 if (clip == aicon->dock)
4081 continue;
4083 if (clip->icon_count + scr->global_icon_count >= clip->max_icons)
4084 return False; /* Clip is full in some workspace */
4086 for (j = 0; j < clip->max_icons; j++) {
4087 btn = clip->icon_array[j];
4088 if (btn && btn->xindex == aicon->xindex && btn->yindex == aicon->yindex)
4089 return False;
4093 return True;
4096 int wClipMakeIconOmnipresent(WAppIcon * aicon, int omnipresent)
4098 WScreen *scr = aicon->icon->core->screen_ptr;
4099 WAppIconChain *new_entry, *tmp, *tmp1;
4100 int status = WO_SUCCESS;
4102 if ((scr->dock && aicon->dock == scr->dock) || aicon == scr->clip_icon) {
4103 return WO_NOT_APPLICABLE;
4106 if (aicon->omnipresent == omnipresent)
4107 return WO_SUCCESS;
4109 if (omnipresent) {
4110 if (iconCanBeOmnipresent(aicon)) {
4111 aicon->omnipresent = 1;
4112 new_entry = wmalloc(sizeof(WAppIconChain));
4113 new_entry->aicon = aicon;
4114 new_entry->next = scr->global_icons;
4115 scr->global_icons = new_entry;
4116 scr->global_icon_count++;
4117 } else {
4118 aicon->omnipresent = 0;
4119 status = WO_FAILED;
4121 } else {
4122 aicon->omnipresent = 0;
4123 if (aicon == scr->global_icons->aicon) {
4124 tmp = scr->global_icons->next;
4125 wfree(scr->global_icons);
4126 scr->global_icons = tmp;
4127 scr->global_icon_count--;
4128 } else {
4129 tmp = scr->global_icons;
4130 while (tmp->next) {
4131 if (tmp->next->aicon == aicon) {
4132 tmp1 = tmp->next->next;
4133 wfree(tmp->next);
4134 tmp->next = tmp1;
4135 scr->global_icon_count--;
4136 break;
4138 tmp = tmp->next;
4143 wAppIconPaint(aicon);
4145 return status;