Merging appicon.c:appIconMouseDown and dock.c:handleIconMove into appicon.c:wHandleAp...
[wmaker-crm.git] / src / dock.c
blobe2352fd4a771f780fe1c1bfe7829ab340bc1dde8
1 /* dock.c- built-in Dock module for WindowMaker
3 * Window Maker window manager
5 * Copyright (c) 1997-2003 Alfredo K. Kojima
6 * Copyright (c) 1998-2003 Dan Pascu
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
23 #include "wconfig.h"
25 #include <X11/Xlib.h>
26 #include <X11/Xutil.h>
27 #include <stdlib.h>
28 #include <string.h>
29 #include <strings.h>
30 #include <unistd.h>
31 #include <math.h>
32 #include <limits.h>
34 #ifndef PATH_MAX
35 #define PATH_MAX DEFAULT_PATH_MAX
36 #endif
38 #include "WindowMaker.h"
39 #include "wcore.h"
40 #include "window.h"
41 #include "icon.h"
42 #include "appicon.h"
43 #include "actions.h"
44 #include "stacking.h"
45 #include "dock.h"
46 #include "dockedapp.h"
47 #include "dialog.h"
48 #include "main.h"
49 #include "properties.h"
50 #include "menu.h"
51 #include "client.h"
52 #include "defaults.h"
53 #include "workspace.h"
54 #include "framewin.h"
55 #include "superfluous.h"
56 #include "xinerama.h"
58 /**** Local variables ****/
59 #define CLIP_REWIND 1
60 #define CLIP_IDLE 0
61 #define CLIP_FORWARD 2
63 /**** Global variables ****/
64 extern Cursor wCursor[WCUR_LAST];
65 extern WPreferences wPreferences;
66 extern XContext wWinContext;
67 extern void appIconMouseDown(WObjDescriptor *desc, XEvent *event);
69 #define MOD_MASK wPreferences.modifier_mask
70 #define ICON_SIZE wPreferences.icon_size
72 /***** Local variables ****/
74 static WMPropList *dCommand = NULL;
75 static WMPropList *dPasteCommand = NULL;
76 #ifdef XDND /* XXX was OFFIX */
77 static WMPropList *dDropCommand = NULL;
78 #endif
79 static WMPropList *dAutoLaunch, *dLock;
80 static WMPropList *dName, *dForced, *dBuggyApplication, *dYes, *dNo;
81 static WMPropList *dHost, *dDock, *dClip;
82 static WMPropList *dAutoAttractIcons;
84 static WMPropList *dPosition, *dApplications, *dLowered, *dCollapsed;
86 static WMPropList *dAutoCollapse, *dAutoRaiseLower, *dOmnipresent;
88 static void dockIconPaint(WAppIcon *btn);
90 static void iconMouseDown(WObjDescriptor *desc, XEvent *event);
92 static pid_t execCommand(WAppIcon *btn, char *command, WSavedState *state);
94 static void trackDeadProcess(pid_t pid, unsigned char status, WDock *dock);
96 static int getClipButton(int px, int py);
98 static void toggleLowered(WDock *dock);
100 static void toggleCollapsed(WDock *dock);
102 static void clipIconExpose(WObjDescriptor *desc, XEvent *event);
104 static void clipLeave(WDock *dock);
106 static void handleClipChangeWorkspace(WScreen *scr, XEvent *event);
108 static void clipEnterNotify(WObjDescriptor *desc, XEvent *event);
109 static void clipLeaveNotify(WObjDescriptor *desc, XEvent *event);
110 static void clipAutoCollapse(void *cdata);
111 static void clipAutoExpand(void *cdata);
112 static void launchDockedApplication(WAppIcon *btn, Bool withSelection);
114 static void clipAutoLower(void *cdata);
115 static void clipAutoRaise(void *cdata);
116 static WAppIcon *mainIconCreate(WScreen *scr, int type);
118 static int onScreen(WScreen *scr, int x, int y);
120 static void make_keys(void)
122 if (dCommand != NULL)
123 return;
125 dCommand = WMRetainPropList(WMCreatePLString("Command"));
126 dPasteCommand = WMRetainPropList(WMCreatePLString("PasteCommand"));
127 #ifdef XDND
128 dDropCommand = WMRetainPropList(WMCreatePLString("DropCommand"));
129 #endif
130 dLock = WMRetainPropList(WMCreatePLString("Lock"));
131 dAutoLaunch = WMRetainPropList(WMCreatePLString("AutoLaunch"));
132 dName = WMRetainPropList(WMCreatePLString("Name"));
133 dForced = WMRetainPropList(WMCreatePLString("Forced"));
134 dBuggyApplication = WMRetainPropList(WMCreatePLString("BuggyApplication"));
135 dYes = WMRetainPropList(WMCreatePLString("Yes"));
136 dNo = WMRetainPropList(WMCreatePLString("No"));
137 dHost = WMRetainPropList(WMCreatePLString("Host"));
139 dPosition = WMCreatePLString("Position");
140 dApplications = WMCreatePLString("Applications");
141 dLowered = WMCreatePLString("Lowered");
142 dCollapsed = WMCreatePLString("Collapsed");
143 dAutoCollapse = WMCreatePLString("AutoCollapse");
144 dAutoRaiseLower = WMCreatePLString("AutoRaiseLower");
145 dAutoAttractIcons = WMCreatePLString("AutoAttractIcons");
147 dOmnipresent = WMCreatePLString("Omnipresent");
149 dDock = WMCreatePLString("Dock");
150 dClip = WMCreatePLString("Clip");
153 static void renameCallback(WMenu *menu, WMenuEntry *entry)
155 WDock *dock = entry->clientdata;
156 char buffer[128];
157 int wspace;
158 char *name;
160 assert(entry->clientdata != NULL);
162 wspace = dock->screen_ptr->current_workspace;
164 name = wstrdup(dock->screen_ptr->workspaces[wspace]->name);
166 snprintf(buffer, sizeof(buffer), _("Type the name for workspace %i:"), wspace + 1);
167 if (wInputDialog(dock->screen_ptr, _("Rename Workspace"), buffer, &name))
168 wWorkspaceRename(dock->screen_ptr, wspace, name);
170 wfree(name);
173 static void toggleLoweredCallback(WMenu *menu, WMenuEntry *entry)
175 assert(entry->clientdata != NULL);
177 toggleLowered(entry->clientdata);
179 entry->flags.indicator_on = !(((WDock *) entry->clientdata)->lowered);
181 wMenuPaint(menu);
184 static int matchWindow(const void *item, const void *cdata)
186 return (((WFakeGroupLeader *) item)->leader == (Window) cdata);
189 static void killCallback(WMenu *menu, WMenuEntry *entry)
191 WScreen *scr = menu->menu->screen_ptr;
192 WAppIcon *icon;
193 WFakeGroupLeader *fPtr;
194 char *buffer, *shortname, **argv;
195 char *basename(const char *shortname);
196 int argc;
198 if (!WCHECK_STATE(WSTATE_NORMAL))
199 return;
201 assert(entry->clientdata != NULL);
203 icon = (WAppIcon *) entry->clientdata;
205 icon->editing = 1;
207 WCHANGE_STATE(WSTATE_MODAL);
209 /* strip away dir names */
210 shortname = basename(icon->command);
211 /* separate out command options */
212 wtokensplit(shortname, &argv, &argc);
214 buffer = wstrconcat(argv[0],
215 _(" will be forcibly closed.\n"
216 "Any unsaved changes will be lost.\n" "Please confirm."));
218 if (icon->icon && icon->icon->owner) {
219 fPtr = icon->icon->owner->fake_group;
220 } else {
221 /* is this really necessary? can we kill a non-running dock icon? */
222 Window win = icon->main_window;
223 int index;
225 index = WMFindInArray(scr->fakeGroupLeaders, matchWindow, (void *)win);
226 if (index != WANotFound)
227 fPtr = WMGetFromArray(scr->fakeGroupLeaders, index);
228 else
229 fPtr = NULL;
232 if (wPreferences.dont_confirm_kill
233 || wMessageDialog(menu->frame->screen_ptr, _("Kill Application"),
234 buffer, _("Yes"), _("No"), NULL) == WAPRDefault) {
235 if (fPtr != NULL) {
236 WWindow *wwin, *twin;
238 wwin = scr->focused_window;
239 while (wwin) {
240 twin = wwin->prev;
241 if (wwin->fake_group == fPtr)
242 wClientKill(wwin);
244 wwin = twin;
246 } else if (icon->icon && icon->icon->owner) {
247 wClientKill(icon->icon->owner);
251 wfree(buffer);
252 wtokenfree(argv, argc);
254 icon->editing = 0;
256 WCHANGE_STATE(WSTATE_NORMAL);
259 /* TODO: replace this function with a member of the dock struct */
260 static int numberOfSelectedIcons(WDock *dock)
262 WAppIcon *aicon;
263 int i, n;
265 n = 0;
266 for (i = 1; i < dock->max_icons; i++) {
267 aicon = dock->icon_array[i];
268 if (aicon && aicon->icon->selected)
269 n++;
272 return n;
275 static WMArray *getSelected(WDock *dock)
277 WMArray *ret = WMCreateArray(8);
278 WAppIcon *btn;
279 int i;
281 for (i = 1; i < dock->max_icons; i++) {
282 btn = dock->icon_array[i];
283 if (btn && btn->icon->selected)
284 WMAddToArray(ret, btn);
287 return ret;
290 static void paintClipButtons(WAppIcon *clipIcon, Bool lpushed, Bool rpushed)
292 Window win = clipIcon->icon->core->window;
293 WScreen *scr = clipIcon->icon->core->screen_ptr;
294 XPoint p[4];
295 int pt = CLIP_BUTTON_SIZE * ICON_SIZE / 64;
296 int tp = ICON_SIZE - pt;
297 int as = pt - 15; /* 15 = 5+5+5 */
298 GC gc = scr->draw_gc; /* maybe use WMColorGC() instead here? */
299 WMColor *color;
301 color = scr->clip_title_color[CLIP_NORMAL];
303 XSetForeground(dpy, gc, WMColorPixel(color));
305 if (rpushed) {
306 p[0].x = tp + 1;
307 p[0].y = 1;
308 p[1].x = ICON_SIZE - 2;
309 p[1].y = 1;
310 p[2].x = ICON_SIZE - 2;
311 p[2].y = pt - 1;
312 } else if (lpushed) {
313 p[0].x = 1;
314 p[0].y = tp;
315 p[1].x = pt;
316 p[1].y = ICON_SIZE - 2;
317 p[2].x = 1;
318 p[2].y = ICON_SIZE - 2;
320 if (lpushed || rpushed) {
321 XSetForeground(dpy, scr->draw_gc, scr->white_pixel);
322 XFillPolygon(dpy, win, scr->draw_gc, p, 3, Convex, CoordModeOrigin);
323 XSetForeground(dpy, scr->draw_gc, scr->black_pixel);
326 /* top right arrow */
327 p[0].x = p[3].x = ICON_SIZE - 5 - as;
328 p[0].y = p[3].y = 5;
329 p[1].x = ICON_SIZE - 6;
330 p[1].y = 5;
331 p[2].x = ICON_SIZE - 6;
332 p[2].y = 4 + as;
333 if (rpushed) {
334 XFillPolygon(dpy, win, scr->draw_gc, p, 3, Convex, CoordModeOrigin);
335 XDrawLines(dpy, win, scr->draw_gc, p, 4, CoordModeOrigin);
336 } else {
337 XFillPolygon(dpy, win, gc, p, 3, Convex, CoordModeOrigin);
338 XDrawLines(dpy, win, gc, p, 4, CoordModeOrigin);
341 /* bottom left arrow */
342 p[0].x = p[3].x = 5;
343 p[0].y = p[3].y = ICON_SIZE - 5 - as;
344 p[1].x = 5;
345 p[1].y = ICON_SIZE - 6;
346 p[2].x = 4 + as;
347 p[2].y = ICON_SIZE - 6;
348 if (lpushed) {
349 XFillPolygon(dpy, win, scr->draw_gc, p, 3, Convex, CoordModeOrigin);
350 XDrawLines(dpy, win, scr->draw_gc, p, 4, CoordModeOrigin);
351 } else {
352 XFillPolygon(dpy, win, gc, p, 3, Convex, CoordModeOrigin);
353 XDrawLines(dpy, win, gc, p, 4, CoordModeOrigin);
357 RImage *wClipMakeTile(WScreen *scr, RImage *normalTile)
359 RImage *tile = RCloneImage(normalTile);
360 RColor black;
361 RColor dark;
362 RColor light;
363 int pt, tp;
364 int as;
366 pt = CLIP_BUTTON_SIZE * wPreferences.icon_size / 64;
367 tp = wPreferences.icon_size - 1 - pt;
368 as = pt - 15;
370 black.alpha = 255;
371 black.red = black.green = black.blue = 0;
373 dark.alpha = 0;
374 dark.red = dark.green = dark.blue = 60;
376 light.alpha = 0;
377 light.red = light.green = light.blue = 80;
379 /* top right */
380 ROperateLine(tile, RSubtractOperation, tp, 0, wPreferences.icon_size - 2, pt - 1, &dark);
381 RDrawLine(tile, tp - 1, 0, wPreferences.icon_size - 1, pt + 1, &black);
382 ROperateLine(tile, RAddOperation, tp, 2, wPreferences.icon_size - 3, pt, &light);
384 /* arrow bevel */
385 ROperateLine(tile, RSubtractOperation, ICON_SIZE - 7 - as, 4, ICON_SIZE - 5, 4, &dark);
386 ROperateLine(tile, RSubtractOperation, ICON_SIZE - 6 - as, 5, ICON_SIZE - 5, 6 + as, &dark);
387 ROperateLine(tile, RAddOperation, ICON_SIZE - 5, 4, ICON_SIZE - 5, 6 + as, &light);
389 /* bottom left */
390 ROperateLine(tile, RAddOperation, 2, tp + 2, pt - 2, wPreferences.icon_size - 3, &dark);
391 RDrawLine(tile, 0, tp - 1, pt + 1, wPreferences.icon_size - 1, &black);
392 ROperateLine(tile, RSubtractOperation, 0, tp - 2, pt + 1, wPreferences.icon_size - 2, &light);
394 /* arrow bevel */
395 ROperateLine(tile, RSubtractOperation, 4, ICON_SIZE - 7 - as, 4, ICON_SIZE - 5, &dark);
396 ROperateLine(tile, RSubtractOperation, 5, ICON_SIZE - 6 - as, 6 + as, ICON_SIZE - 5, &dark);
397 ROperateLine(tile, RAddOperation, 4, ICON_SIZE - 5, 6 + as, ICON_SIZE - 5, &light);
399 return tile;
402 static void omnipresentCallback(WMenu *menu, WMenuEntry *entry)
404 WAppIcon *clickedIcon = entry->clientdata;
405 WAppIcon *aicon;
406 WDock *dock;
407 WMArray *selectedIcons;
408 WMArrayIterator iter;
409 int failed;
411 assert(entry->clientdata != NULL);
413 dock = clickedIcon->dock;
415 selectedIcons = getSelected(dock);
417 if (!WMGetArrayItemCount(selectedIcons))
418 WMAddToArray(selectedIcons, clickedIcon);
420 failed = 0;
421 WM_ITERATE_ARRAY(selectedIcons, aicon, iter) {
422 if (wClipMakeIconOmnipresent(aicon, !aicon->omnipresent) == WO_FAILED)
423 failed++;
424 else if (aicon->icon->selected)
425 wIconSelect(aicon->icon);
427 WMFreeArray(selectedIcons);
429 if (failed > 1) {
430 wMessageDialog(dock->screen_ptr, _("Warning"),
431 _("Some icons cannot be made omnipresent. "
432 "Please make sure that no other icon is "
433 "docked in the same positions on the other "
434 "workspaces and the Clip is not full in "
435 "some workspace."), _("OK"), NULL, NULL);
436 } else if (failed == 1) {
437 wMessageDialog(dock->screen_ptr, _("Warning"),
438 _("Icon cannot be made omnipresent. "
439 "Please make sure that no other icon is "
440 "docked in the same position on the other "
441 "workspaces and the Clip is not full in "
442 "some workspace."), _("OK"), NULL, NULL);
446 static void removeIcons(WMArray *icons, WDock *dock)
448 WAppIcon *aicon;
449 int keepit;
450 WMArrayIterator it;
452 WM_ITERATE_ARRAY(icons, aicon, it) {
453 keepit = aicon->running && wApplicationOf(aicon->main_window);
454 wDockDetach(dock, aicon);
455 if (keepit) {
456 /* XXX: can: aicon->icon == NULL ? */
457 PlaceIcon(dock->screen_ptr, &aicon->x_pos, &aicon->y_pos,
458 wGetHeadForWindow(aicon->icon->owner));
459 XMoveWindow(dpy, aicon->icon->core->window, aicon->x_pos, aicon->y_pos);
460 if (!dock->mapped || dock->collapsed)
461 XMapWindow(dpy, aicon->icon->core->window);
464 WMFreeArray(icons);
466 if (wPreferences.auto_arrange_icons)
467 wArrangeIcons(dock->screen_ptr, True);
470 static void removeIconsCallback(WMenu *menu, WMenuEntry *entry)
472 WAppIcon *clickedIcon = (WAppIcon *) entry->clientdata;
473 WDock *dock;
474 WMArray *selectedIcons;
476 assert(clickedIcon != NULL);
478 dock = clickedIcon->dock;
480 selectedIcons = getSelected(dock);
482 if (WMGetArrayItemCount(selectedIcons)) {
483 if (wMessageDialog(dock->screen_ptr, _("Workspace Clip"),
484 _("All selected icons will be removed!"),
485 _("OK"), _("Cancel"), NULL) != WAPRDefault) {
486 WMFreeArray(selectedIcons);
487 return;
489 } else {
490 if (clickedIcon->xindex == 0 && clickedIcon->yindex == 0) {
491 WMFreeArray(selectedIcons);
492 return;
494 WMAddToArray(selectedIcons, clickedIcon);
497 removeIcons(selectedIcons, dock);
500 static void keepIconsCallback(WMenu *menu, WMenuEntry *entry)
502 WAppIcon *clickedIcon = (WAppIcon *) entry->clientdata;
503 WDock *dock;
504 WAppIcon *aicon;
505 WMArray *selectedIcons;
506 WMArrayIterator it;
508 assert(clickedIcon != NULL);
509 dock = clickedIcon->dock;
511 selectedIcons = getSelected(dock);
513 if (!WMGetArrayItemCount(selectedIcons)
514 && clickedIcon != dock->screen_ptr->clip_icon) {
515 char *command = NULL;
517 if (!clickedIcon->command && !clickedIcon->editing) {
518 clickedIcon->editing = 1;
519 if (wInputDialog(dock->screen_ptr, _("Keep Icon"),
520 _("Type the command used to launch the application"), &command)) {
521 if (command && (command[0] == 0 || (command[0] == '-' && command[1] == 0))) {
522 wfree(command);
523 command = NULL;
525 clickedIcon->command = command;
526 clickedIcon->editing = 0;
527 } else {
528 clickedIcon->editing = 0;
529 if (command)
530 wfree(command);
531 WMFreeArray(selectedIcons);
532 return;
536 WMAddToArray(selectedIcons, clickedIcon);
539 WM_ITERATE_ARRAY(selectedIcons, aicon, it) {
540 if (aicon->icon->selected)
541 wIconSelect(aicon->icon);
543 if (aicon && aicon->attracted && aicon->command) {
544 aicon->attracted = 0;
545 if (aicon->icon->shadowed) {
546 aicon->icon->shadowed = 0;
548 /* Update the icon images */
549 wIconUpdate(aicon->icon, NULL);
551 /* Paint it */
552 wAppIconPaint(aicon);
555 save_appicon(aicon, True);
557 WMFreeArray(selectedIcons);
560 static void toggleAutoAttractCallback(WMenu *menu, WMenuEntry *entry)
562 WDock *dock = (WDock *) entry->clientdata;
564 assert(entry->clientdata != NULL);
566 dock->attract_icons = !dock->attract_icons;
568 entry->flags.indicator_on = dock->attract_icons;
570 wMenuPaint(menu);
573 static void selectCallback(WMenu *menu, WMenuEntry *entry)
575 WAppIcon *icon = (WAppIcon *) entry->clientdata;
577 assert(icon != NULL);
579 wIconSelect(icon->icon);
581 wMenuPaint(menu);
584 static void colectIconsCallback(WMenu *menu, WMenuEntry *entry)
586 WAppIcon *clickedIcon = (WAppIcon *) entry->clientdata;
587 WDock *clip;
588 WAppIcon *aicon;
589 int x, y, x_pos, y_pos;
590 Bool update_icon = False;
592 assert(entry->clientdata != NULL);
593 clip = clickedIcon->dock;
595 aicon = clip->screen_ptr->app_icon_list;
597 while (aicon) {
598 if (!aicon->docked && wDockFindFreeSlot(clip, &x, &y)) {
599 x_pos = clip->x_pos + x * ICON_SIZE;
600 y_pos = clip->y_pos + y * ICON_SIZE;
601 if (aicon->x_pos != x_pos || aicon->y_pos != y_pos)
602 move_window(aicon->icon->core->window, aicon->x_pos, aicon->y_pos, x_pos, y_pos);
604 aicon->attracted = 1;
605 if (!aicon->icon->shadowed) {
606 aicon->icon->shadowed = 1;
607 update_icon = True;
609 wDockAttachIcon(clip, aicon, x, y, update_icon);
610 if (clip->collapsed || !clip->mapped)
611 XUnmapWindow(dpy, aicon->icon->core->window);
613 aicon = aicon->next;
617 static void selectIconsCallback(WMenu *menu, WMenuEntry *entry)
619 WAppIcon *clickedIcon = (WAppIcon *) entry->clientdata;
620 WDock *dock;
621 WMArray *selectedIcons;
622 WMArrayIterator iter;
623 WAppIcon *btn;
624 int i;
626 assert(clickedIcon != NULL);
627 dock = clickedIcon->dock;
629 selectedIcons = getSelected(dock);
631 if (!WMGetArrayItemCount(selectedIcons)) {
632 for (i = 1; i < dock->max_icons; i++) {
633 btn = dock->icon_array[i];
634 if (btn && !btn->icon->selected)
635 wIconSelect(btn->icon);
637 } else {
638 WM_ITERATE_ARRAY(selectedIcons, btn, iter) {
639 wIconSelect(btn->icon);
642 WMFreeArray(selectedIcons);
644 wMenuPaint(menu);
647 static void toggleCollapsedCallback(WMenu *menu, WMenuEntry *entry)
649 assert(entry->clientdata != NULL);
651 toggleCollapsed(entry->clientdata);
653 entry->flags.indicator_on = ((WDock *) entry->clientdata)->collapsed;
655 wMenuPaint(menu);
658 static void toggleAutoCollapseCallback(WMenu *menu, WMenuEntry *entry)
660 WDock *dock;
661 assert(entry->clientdata != NULL);
663 dock = (WDock *) entry->clientdata;
665 dock->auto_collapse = !dock->auto_collapse;
667 entry->flags.indicator_on = ((WDock *) entry->clientdata)->auto_collapse;
669 wMenuPaint(menu);
672 static void toggleAutoRaiseLowerCallback(WMenu *menu, WMenuEntry *entry)
674 WDock *dock;
675 assert(entry->clientdata != NULL);
677 dock = (WDock *) entry->clientdata;
679 dock->auto_raise_lower = !dock->auto_raise_lower;
681 entry->flags.indicator_on = ((WDock *) entry->clientdata)->auto_raise_lower;
683 wMenuPaint(menu);
686 static void launchCallback(WMenu *menu, WMenuEntry *entry)
688 WAppIcon *btn = (WAppIcon *) entry->clientdata;
690 launchDockedApplication(btn, False);
693 static void settingsCallback(WMenu *menu, WMenuEntry *entry)
695 WAppIcon *btn = (WAppIcon *) entry->clientdata;
697 if (btn->editing)
698 return;
699 ShowDockAppSettingsPanel(btn);
702 static void hideCallback(WMenu *menu, WMenuEntry *entry)
704 WApplication *wapp;
705 WAppIcon *btn = (WAppIcon *) entry->clientdata;
707 wapp = wApplicationOf(btn->icon->owner->main_window);
709 if (wapp->flags.hidden) {
710 wWorkspaceChange(btn->icon->core->screen_ptr, wapp->last_workspace);
711 wUnhideApplication(wapp, False, False);
712 } else {
713 wHideApplication(wapp);
717 static void unhideHereCallback(WMenu *menu, WMenuEntry *entry)
719 WApplication *wapp;
720 WAppIcon *btn = (WAppIcon *) entry->clientdata;
722 wapp = wApplicationOf(btn->icon->owner->main_window);
724 wUnhideApplication(wapp, False, True);
727 static WAppIcon *mainIconCreate(WScreen *scr, int type)
729 WAppIcon *btn;
730 int x_pos;
732 if (type == WM_CLIP) {
733 if (scr->clip_icon)
734 return scr->clip_icon;
735 btn = wAppIconCreateForDock(scr, NULL, "Logo", "WMClip", TILE_CLIP);
736 btn->icon->core->descriptor.handle_expose = clipIconExpose;
737 btn->icon->core->descriptor.handle_enternotify = clipEnterNotify;
738 btn->icon->core->descriptor.handle_leavenotify = clipLeaveNotify;
739 x_pos = 0;
740 } else {
741 btn = wAppIconCreateForDock(scr, NULL, "Logo", "WMDock", TILE_NORMAL);
742 x_pos = scr->scr_width - ICON_SIZE - DOCK_EXTRA_SPACE;
745 btn->xindex = 0;
746 btn->yindex = 0;
748 btn->icon->core->descriptor.handle_mousedown = iconMouseDown;
749 btn->icon->core->descriptor.parent_type = WCLASS_DOCK_ICON;
750 btn->icon->core->descriptor.parent = btn;
751 XMapWindow(dpy, btn->icon->core->window);
752 btn->x_pos = x_pos;
753 btn->y_pos = 0;
754 btn->docked = 1;
755 if (type == WM_CLIP)
756 scr->clip_icon = btn;
758 return btn;
761 static void switchWSCommand(WMenu *menu, WMenuEntry *entry)
763 WAppIcon *btn, *icon = (WAppIcon *) entry->clientdata;
764 WScreen *scr = icon->icon->core->screen_ptr;
765 WDock *src, *dest;
766 WMArray *selectedIcons;
767 int x, y;
769 if (entry->order == scr->current_workspace)
770 return;
771 src = icon->dock;
772 dest = scr->workspaces[entry->order]->clip;
774 selectedIcons = getSelected(src);
776 if (WMGetArrayItemCount(selectedIcons)) {
777 WMArrayIterator iter;
779 WM_ITERATE_ARRAY(selectedIcons, btn, iter) {
780 if (wDockFindFreeSlot(dest, &x, &y)) {
781 wDockMoveIconBetweenDocks(src, dest, btn, x, y);
782 XUnmapWindow(dpy, btn->icon->core->window);
785 } else if (icon != scr->clip_icon) {
786 if (wDockFindFreeSlot(dest, &x, &y)) {
787 wDockMoveIconBetweenDocks(src, dest, icon, x, y);
788 XUnmapWindow(dpy, icon->icon->core->window);
791 WMFreeArray(selectedIcons);
794 static void launchDockedApplication(WAppIcon *btn, Bool withSelection)
796 WScreen *scr = btn->icon->core->screen_ptr;
798 if (!btn->launching &&
799 ((!withSelection && btn->command != NULL) || (withSelection && btn->paste_command != NULL))) {
800 if (!btn->forced_dock) {
801 btn->relaunching = btn->running;
802 btn->running = 1;
804 if (btn->wm_instance || btn->wm_class) {
805 WWindowAttributes attr;
806 memset(&attr, 0, sizeof(WWindowAttributes));
807 wDefaultFillAttributes(btn->wm_instance, btn->wm_class, &attr, NULL, True);
809 if (!attr.no_appicon && !btn->buggy_app)
810 btn->launching = 1;
811 else
812 btn->running = 0;
814 btn->drop_launch = 0;
815 btn->paste_launch = withSelection;
816 scr->last_dock = btn->dock;
817 btn->pid = execCommand(btn, (withSelection ? btn->paste_command : btn->command), NULL);
818 if (btn->pid > 0) {
819 if (btn->buggy_app) {
820 /* give feedback that the app was launched */
821 btn->launching = 1;
822 dockIconPaint(btn);
823 btn->launching = 0;
824 WMAddTimerHandler(200, (WMCallback *) dockIconPaint, btn);
825 } else {
826 dockIconPaint(btn);
828 } else {
829 wwarning(_("could not launch application %s"), btn->command);
830 btn->launching = 0;
831 if (!btn->relaunching)
832 btn->running = 0;
837 static void updateWorkspaceMenu(WMenu *menu, WAppIcon *icon)
839 WScreen *scr = menu->frame->screen_ptr;
840 char title[MAX_WORKSPACENAME_WIDTH + 1];
841 int i;
843 if (!menu || !icon)
844 return;
846 for (i = 0; i < scr->workspace_count; i++) {
847 if (i < menu->entry_no) {
848 if (strcmp(menu->entries[i]->text, scr->workspaces[i]->name) != 0) {
849 wfree(menu->entries[i]->text);
850 strcpy(title, scr->workspaces[i]->name);
851 menu->entries[i]->text = wstrdup(title);
852 menu->flags.realized = 0;
854 menu->entries[i]->clientdata = (void *)icon;
855 } else {
856 strcpy(title, scr->workspaces[i]->name);
858 wMenuAddCallback(menu, title, switchWSCommand, (void *)icon);
860 menu->flags.realized = 0;
863 if (i == scr->current_workspace)
864 wMenuSetEnabled(menu, i, False);
865 else
866 wMenuSetEnabled(menu, i, True);
869 if (!menu->flags.realized)
870 wMenuRealize(menu);
873 static WMenu *makeWorkspaceMenu(WScreen *scr)
875 WMenu *menu;
877 menu = wMenuCreate(scr, NULL, False);
878 if (!menu)
879 wwarning(_("could not create workspace submenu for Clip menu"));
881 wMenuAddCallback(menu, "", switchWSCommand, (void *)scr->clip_icon);
883 menu->flags.realized = 0;
884 wMenuRealize(menu);
886 return menu;
889 static void updateClipOptionsMenu(WMenu *menu, WDock *dock)
891 WMenuEntry *entry;
892 int index = 0;
894 if (!menu || !dock)
895 return;
897 /* keep on top */
898 entry = menu->entries[index];
899 entry->flags.indicator_on = !dock->lowered;
900 entry->clientdata = dock;
902 /* collapsed */
903 entry = menu->entries[++index];
904 entry->flags.indicator_on = dock->collapsed;
905 entry->clientdata = dock;
907 /* auto-collapse */
908 entry = menu->entries[++index];
909 entry->flags.indicator_on = dock->auto_collapse;
910 entry->clientdata = dock;
912 /* auto-raise/lower */
913 entry = menu->entries[++index];
914 entry->flags.indicator_on = dock->auto_raise_lower;
915 entry->clientdata = dock;
916 wMenuSetEnabled(menu, index, dock->lowered);
918 /* attract icons */
919 entry = menu->entries[++index];
920 entry->flags.indicator_on = dock->attract_icons;
921 entry->clientdata = dock;
923 menu->flags.realized = 0;
924 wMenuRealize(menu);
927 static WMenu *makeClipOptionsMenu(WScreen *scr)
929 WMenu *menu;
930 WMenuEntry *entry;
932 menu = wMenuCreate(scr, NULL, False);
933 if (!menu) {
934 wwarning(_("could not create options submenu for Clip menu"));
935 return NULL;
938 entry = wMenuAddCallback(menu, _("Keep on Top"), toggleLoweredCallback, NULL);
939 entry->flags.indicator = 1;
940 entry->flags.indicator_on = 1;
941 entry->flags.indicator_type = MI_CHECK;
943 entry = wMenuAddCallback(menu, _("Collapsed"), toggleCollapsedCallback, NULL);
944 entry->flags.indicator = 1;
945 entry->flags.indicator_on = 1;
946 entry->flags.indicator_type = MI_CHECK;
948 entry = wMenuAddCallback(menu, _("Autocollapse"), toggleAutoCollapseCallback, NULL);
949 entry->flags.indicator = 1;
950 entry->flags.indicator_on = 1;
951 entry->flags.indicator_type = MI_CHECK;
953 entry = wMenuAddCallback(menu, _("Autoraise"), toggleAutoRaiseLowerCallback, NULL);
954 entry->flags.indicator = 1;
955 entry->flags.indicator_on = 1;
956 entry->flags.indicator_type = MI_CHECK;
958 entry = wMenuAddCallback(menu, _("Autoattract Icons"), toggleAutoAttractCallback, NULL);
959 entry->flags.indicator = 1;
960 entry->flags.indicator_on = 1;
961 entry->flags.indicator_type = MI_CHECK;
963 menu->flags.realized = 0;
964 wMenuRealize(menu);
966 return menu;
969 static WMenu *dockMenuCreate(WScreen *scr, int type)
971 WMenu *menu;
972 WMenuEntry *entry;
974 if (type == WM_CLIP && scr->clip_menu)
975 return scr->clip_menu;
977 menu = wMenuCreate(scr, NULL, False);
978 if (type != WM_CLIP) {
979 entry = wMenuAddCallback(menu, _("Keep on Top"), toggleLoweredCallback, NULL);
980 entry->flags.indicator = 1;
981 entry->flags.indicator_on = 1;
982 entry->flags.indicator_type = MI_CHECK;
983 } else {
984 entry = wMenuAddCallback(menu, _("Clip Options"), NULL, NULL);
985 scr->clip_options = makeClipOptionsMenu(scr);
986 if (scr->clip_options)
987 wMenuEntrySetCascade(menu, entry, scr->clip_options);
989 entry = wMenuAddCallback(menu, _("Rename Workspace"), renameCallback, NULL);
990 wfree(entry->text);
991 entry->text = _("Rename Workspace");
993 entry = wMenuAddCallback(menu, _("Selected"), selectCallback, NULL);
994 entry->flags.indicator = 1;
995 entry->flags.indicator_on = 1;
996 entry->flags.indicator_type = MI_CHECK;
998 entry = wMenuAddCallback(menu, _("Select All Icons"), selectIconsCallback, NULL);
999 wfree(entry->text);
1000 entry->text = _("Select All Icons");
1002 entry = wMenuAddCallback(menu, _("Keep Icon"), keepIconsCallback, NULL);
1003 wfree(entry->text);
1004 entry->text = _("Keep Icon");
1006 entry = wMenuAddCallback(menu, _("Move Icon To"), NULL, NULL);
1007 wfree(entry->text);
1008 entry->text = _("Move Icon To");
1009 scr->clip_submenu = makeWorkspaceMenu(scr);
1010 if (scr->clip_submenu)
1011 wMenuEntrySetCascade(menu, entry, scr->clip_submenu);
1013 entry = wMenuAddCallback(menu, _("Remove Icon"), removeIconsCallback, NULL);
1014 wfree(entry->text);
1015 entry->text = _("Remove Icon");
1017 wMenuAddCallback(menu, _("Attract Icons"), colectIconsCallback, NULL);
1020 wMenuAddCallback(menu, _("Launch"), launchCallback, NULL);
1022 wMenuAddCallback(menu, _("Unhide Here"), unhideHereCallback, NULL);
1024 entry = wMenuAddCallback(menu, _("Hide"), hideCallback, NULL);
1025 wfree(entry->text);
1026 entry->text = _("Hide");
1028 wMenuAddCallback(menu, _("Settings..."), settingsCallback, NULL);
1030 wMenuAddCallback(menu, _("Kill"), killCallback, NULL);
1032 if (type == WM_CLIP)
1033 scr->clip_menu = menu;
1035 return menu;
1038 WDock *wDockCreate(WScreen *scr, int type)
1040 WDock *dock;
1041 WAppIcon *btn;
1043 make_keys();
1045 dock = wmalloc(sizeof(WDock));
1047 dock->max_icons = DOCK_MAX_ICONS;
1049 dock->icon_array = wmalloc(sizeof(WAppIcon *) * dock->max_icons);
1051 btn = mainIconCreate(scr, type);
1053 btn->dock = dock;
1055 dock->x_pos = btn->x_pos;
1056 dock->y_pos = btn->y_pos;
1057 dock->screen_ptr = scr;
1058 dock->type = type;
1059 dock->icon_count = 1;
1060 dock->on_right_side = 1;
1061 dock->collapsed = 0;
1062 dock->auto_collapse = 0;
1063 dock->auto_collapse_magic = NULL;
1064 dock->auto_raise_lower = 0;
1065 dock->auto_lower_magic = NULL;
1066 dock->auto_raise_magic = NULL;
1067 dock->attract_icons = 0;
1068 dock->lowered = 1;
1069 dock->icon_array[0] = btn;
1070 wRaiseFrame(btn->icon->core);
1071 XMoveWindow(dpy, btn->icon->core->window, btn->x_pos, btn->y_pos);
1073 /* create dock menu */
1074 dock->menu = dockMenuCreate(scr, type);
1076 return dock;
1079 void wDockDestroy(WDock *dock)
1081 int i;
1082 WAppIcon *aicon;
1084 for (i = (dock->type == WM_CLIP) ? 1 : 0; i < dock->max_icons; i++) {
1085 aicon = dock->icon_array[i];
1086 if (aicon) {
1087 int keepit = aicon->running && wApplicationOf(aicon->main_window);
1088 wDockDetach(dock, aicon);
1089 if (keepit) {
1090 /* XXX: can: aicon->icon == NULL ? */
1091 PlaceIcon(dock->screen_ptr, &aicon->x_pos, &aicon->y_pos,
1092 wGetHeadForWindow(aicon->icon->owner));
1093 XMoveWindow(dpy, aicon->icon->core->window, aicon->x_pos, aicon->y_pos);
1094 if (!dock->mapped || dock->collapsed)
1095 XMapWindow(dpy, aicon->icon->core->window);
1099 if (wPreferences.auto_arrange_icons)
1100 wArrangeIcons(dock->screen_ptr, True);
1101 wfree(dock->icon_array);
1102 if (dock->menu && dock->type != WM_CLIP)
1103 wMenuDestroy(dock->menu, True);
1104 if (dock->screen_ptr->last_dock == dock)
1105 dock->screen_ptr->last_dock = NULL;
1106 wfree(dock);
1109 void wClipIconPaint(WAppIcon *aicon)
1111 WScreen *scr = aicon->icon->core->screen_ptr;
1112 WWorkspace *workspace = scr->workspaces[scr->current_workspace];
1113 WMColor *color;
1114 Window win = aicon->icon->core->window;
1115 int length, nlength;
1116 char *ws_name, ws_number[10];
1117 int ty, tx;
1119 wIconPaint(aicon->icon);
1121 length = strlen(workspace->name);
1122 ws_name = wmalloc(length + 1);
1123 snprintf(ws_name, length + 1, "%s", workspace->name);
1124 snprintf(ws_number, sizeof(ws_number), "%i", scr->current_workspace + 1);
1125 nlength = strlen(ws_number);
1127 if (!workspace->clip->collapsed)
1128 color = scr->clip_title_color[CLIP_NORMAL];
1129 else
1130 color = scr->clip_title_color[CLIP_COLLAPSED];
1132 ty = ICON_SIZE - WMFontHeight(scr->clip_title_font) - 3;
1134 tx = CLIP_BUTTON_SIZE * ICON_SIZE / 64;
1136 if(wPreferences.show_clip_title)
1137 WMDrawString(scr->wmscreen, win, color, scr->clip_title_font, tx, ty, ws_name, length);
1139 tx = (ICON_SIZE / 2 - WMWidthOfString(scr->clip_title_font, ws_number, nlength)) / 2;
1141 WMDrawString(scr->wmscreen, win, color, scr->clip_title_font, tx, 2, ws_number, nlength);
1143 wfree(ws_name);
1145 if (aicon->launching)
1146 XFillRectangle(dpy, aicon->icon->core->window, scr->stipple_gc,
1147 0, 0, wPreferences.icon_size, wPreferences.icon_size);
1149 paintClipButtons(aicon, aicon->dock->lclip_button_pushed, aicon->dock->rclip_button_pushed);
1152 static void clipIconExpose(WObjDescriptor *desc, XEvent *event)
1154 wClipIconPaint(desc->parent);
1157 static void dockIconPaint(WAppIcon *btn)
1159 if (btn == btn->icon->core->screen_ptr->clip_icon) {
1160 wClipIconPaint(btn);
1161 } else {
1162 wAppIconPaint(btn);
1163 save_appicon(btn, True);
1167 static WMPropList *make_icon_state(WAppIcon *btn)
1169 WMPropList *node = NULL;
1170 WMPropList *command, *autolaunch, *lock, *name, *forced;
1171 WMPropList *position, *buggy, *omnipresent;
1172 char *tmp;
1173 char buffer[64];
1175 if (btn) {
1176 if (!btn->command)
1177 command = WMCreatePLString("-");
1178 else
1179 command = WMCreatePLString(btn->command);
1181 autolaunch = btn->auto_launch ? dYes : dNo;
1183 lock = btn->lock ? dYes : dNo;
1185 tmp = EscapeWM_CLASS(btn->wm_instance, btn->wm_class);
1187 name = WMCreatePLString(tmp);
1189 wfree(tmp);
1191 forced = btn->forced_dock ? dYes : dNo;
1193 buggy = btn->buggy_app ? dYes : dNo;
1195 if (btn == btn->icon->core->screen_ptr->clip_icon)
1196 snprintf(buffer, sizeof(buffer), "%i,%i", btn->x_pos, btn->y_pos);
1197 else
1198 snprintf(buffer, sizeof(buffer), "%hi,%hi", btn->xindex, btn->yindex);
1199 position = WMCreatePLString(buffer);
1201 node = WMCreatePLDictionary(dCommand, command,
1202 dName, name,
1203 dAutoLaunch, autolaunch,
1204 dLock, lock,
1205 dForced, forced, dBuggyApplication, buggy, dPosition, position, NULL);
1206 WMReleasePropList(command);
1207 WMReleasePropList(name);
1208 WMReleasePropList(position);
1210 omnipresent = btn->omnipresent ? dYes : dNo;
1211 if (btn->dock != btn->icon->core->screen_ptr->dock && (btn->xindex != 0 || btn->yindex != 0))
1212 WMPutInPLDictionary(node, dOmnipresent, omnipresent);
1214 #ifdef XDND /* was OFFIX */
1215 if (btn->dnd_command) {
1216 command = WMCreatePLString(btn->dnd_command);
1217 WMPutInPLDictionary(node, dDropCommand, command);
1218 WMReleasePropList(command);
1220 #endif /* XDND */
1222 if (btn->paste_command) {
1223 command = WMCreatePLString(btn->paste_command);
1224 WMPutInPLDictionary(node, dPasteCommand, command);
1225 WMReleasePropList(command);
1229 return node;
1232 static WMPropList *dockSaveState(WDock *dock)
1234 int i;
1235 WMPropList *icon_info;
1236 WMPropList *list = NULL, *dock_state = NULL;
1237 WMPropList *value, *key;
1238 char buffer[256];
1240 list = WMCreatePLArray(NULL);
1242 for (i = (dock->type == WM_DOCK ? 0 : 1); i < dock->max_icons; i++) {
1243 WAppIcon *btn = dock->icon_array[i];
1245 if (!btn || btn->attracted)
1246 continue;
1248 if ((icon_info = make_icon_state(dock->icon_array[i]))) {
1249 WMAddToPLArray(list, icon_info);
1250 WMReleasePropList(icon_info);
1254 dock_state = WMCreatePLDictionary(dApplications, list, NULL);
1256 if (dock->type == WM_DOCK) {
1257 snprintf(buffer, sizeof(buffer), "Applications%i", dock->screen_ptr->scr_height);
1258 key = WMCreatePLString(buffer);
1259 WMPutInPLDictionary(dock_state, key, list);
1260 WMReleasePropList(key);
1262 snprintf(buffer, sizeof(buffer), "%i,%i", (dock->on_right_side ? -ICON_SIZE : 0), dock->y_pos);
1263 value = WMCreatePLString(buffer);
1264 WMPutInPLDictionary(dock_state, dPosition, value);
1265 WMReleasePropList(value);
1267 WMReleasePropList(list);
1269 value = (dock->lowered ? dYes : dNo);
1270 WMPutInPLDictionary(dock_state, dLowered, value);
1272 if (dock->type == WM_CLIP) {
1273 value = (dock->collapsed ? dYes : dNo);
1274 WMPutInPLDictionary(dock_state, dCollapsed, value);
1276 value = (dock->auto_collapse ? dYes : dNo);
1277 WMPutInPLDictionary(dock_state, dAutoCollapse, value);
1279 value = (dock->auto_raise_lower ? dYes : dNo);
1280 WMPutInPLDictionary(dock_state, dAutoRaiseLower, value);
1282 value = (dock->attract_icons ? dYes : dNo);
1283 WMPutInPLDictionary(dock_state, dAutoAttractIcons, value);
1286 return dock_state;
1289 void wDockSaveState(WScreen *scr, WMPropList *old_state)
1291 WMPropList *dock_state;
1292 WMPropList *keys;
1294 dock_state = dockSaveState(scr->dock);
1297 * Copy saved states of docks with different sizes.
1299 if (old_state) {
1300 int i;
1301 WMPropList *tmp;
1303 keys = WMGetPLDictionaryKeys(old_state);
1304 for (i = 0; i < WMGetPropListItemCount(keys); i++) {
1305 tmp = WMGetFromPLArray(keys, i);
1307 if (strncasecmp(WMGetFromPLString(tmp), "applications", 12) == 0
1308 && !WMGetFromPLDictionary(dock_state, tmp)) {
1310 WMPutInPLDictionary(dock_state, tmp, WMGetFromPLDictionary(old_state, tmp));
1313 WMReleasePropList(keys);
1316 WMPutInPLDictionary(scr->session_state, dDock, dock_state);
1318 WMReleasePropList(dock_state);
1321 void wClipSaveState(WScreen *scr)
1323 WMPropList *clip_state;
1325 clip_state = make_icon_state(scr->clip_icon);
1327 WMPutInPLDictionary(scr->session_state, dClip, clip_state);
1329 WMReleasePropList(clip_state);
1332 WMPropList *wClipSaveWorkspaceState(WScreen *scr, int workspace)
1334 return dockSaveState(scr->workspaces[workspace]->clip);
1337 static Bool getBooleanDockValue(WMPropList *value, WMPropList *key)
1339 if (value) {
1340 if (WMIsPLString(value)) {
1341 if (strcasecmp(WMGetFromPLString(value), "YES") == 0)
1342 return True;
1343 } else {
1344 wwarning(_("bad value in docked icon state info %s"), WMGetFromPLString(key));
1347 return False;
1350 static WAppIcon *restore_icon_state(WScreen *scr, WMPropList *info, int type, int index)
1352 WAppIcon *aicon;
1353 WMPropList *cmd, *value;
1354 char *wclass, *winstance, *command;
1356 cmd = WMGetFromPLDictionary(info, dCommand);
1357 if (!cmd || !WMIsPLString(cmd))
1358 return NULL;
1360 /* parse window name */
1361 value = WMGetFromPLDictionary(info, dName);
1362 if (!value)
1363 return NULL;
1365 ParseWindowName(value, &winstance, &wclass, "dock");
1367 if (!winstance && !wclass)
1368 return NULL;
1370 /* get commands */
1371 if (cmd)
1372 command = wstrdup(WMGetFromPLString(cmd));
1373 else
1374 command = NULL;
1376 if (!command || strcmp(command, "-") == 0) {
1377 if (command)
1378 wfree(command);
1379 if (wclass)
1380 wfree(wclass);
1381 if (winstance)
1382 wfree(winstance);
1384 return NULL;
1387 aicon = wAppIconCreateForDock(scr, command, winstance, wclass, TILE_NORMAL);
1388 if (wclass)
1389 wfree(wclass);
1390 if (winstance)
1391 wfree(winstance);
1392 if (command)
1393 wfree(command);
1395 aicon->icon->core->descriptor.handle_mousedown = iconMouseDown;
1396 if (type == WM_CLIP) {
1397 aicon->icon->core->descriptor.handle_enternotify = clipEnterNotify;
1398 aicon->icon->core->descriptor.handle_leavenotify = clipLeaveNotify;
1400 aicon->icon->core->descriptor.parent_type = WCLASS_DOCK_ICON;
1401 aicon->icon->core->descriptor.parent = aicon;
1403 #ifdef XDND /* was OFFIX */
1404 cmd = WMGetFromPLDictionary(info, dDropCommand);
1405 if (cmd)
1406 aicon->dnd_command = wstrdup(WMGetFromPLString(cmd));
1407 #endif
1409 cmd = WMGetFromPLDictionary(info, dPasteCommand);
1410 if (cmd)
1411 aicon->paste_command = wstrdup(WMGetFromPLString(cmd));
1413 /* check auto launch */
1414 value = WMGetFromPLDictionary(info, dAutoLaunch);
1416 aicon->auto_launch = getBooleanDockValue(value, dAutoLaunch);
1418 /* check lock */
1419 value = WMGetFromPLDictionary(info, dLock);
1421 aicon->lock = getBooleanDockValue(value, dLock);
1423 /* check if it wasn't normally docked */
1424 value = WMGetFromPLDictionary(info, dForced);
1426 aicon->forced_dock = getBooleanDockValue(value, dForced);
1428 /* check if we can rely on the stuff in the app */
1429 value = WMGetFromPLDictionary(info, dBuggyApplication);
1431 aicon->buggy_app = getBooleanDockValue(value, dBuggyApplication);
1433 /* get position in the dock */
1434 value = WMGetFromPLDictionary(info, dPosition);
1435 if (value && WMIsPLString(value)) {
1436 if (sscanf(WMGetFromPLString(value), "%hi,%hi", &aicon->xindex, &aicon->yindex) != 2)
1437 wwarning(_("bad value in docked icon state info %s"), WMGetFromPLString(dPosition));
1439 /* check position sanity */
1440 /* incomplete section! */
1441 if (type == WM_DOCK) {
1442 aicon->xindex = 0;
1443 if (aicon->yindex < 0)
1444 wwarning(_("bad value in docked icon position %i,%i"),
1445 aicon->xindex, aicon->yindex);
1447 } else {
1448 aicon->yindex = index;
1449 aicon->xindex = 0;
1452 /* check if icon is omnipresent */
1453 value = WMGetFromPLDictionary(info, dOmnipresent);
1455 aicon->omnipresent = getBooleanDockValue(value, dOmnipresent);
1457 aicon->running = 0;
1458 aicon->docked = 1;
1460 return aicon;
1463 #define COMPLAIN(key) wwarning(_("bad value in dock state info:%s"), key)
1465 WAppIcon *wClipRestoreState(WScreen *scr, WMPropList *clip_state)
1467 WAppIcon *icon;
1468 WMPropList *value;
1470 icon = mainIconCreate(scr, WM_CLIP);
1472 if (!clip_state)
1473 return icon;
1475 WMRetainPropList(clip_state);
1477 /* restore position */
1479 value = WMGetFromPLDictionary(clip_state, dPosition);
1481 if (value) {
1482 if (!WMIsPLString(value)) {
1483 COMPLAIN("Position");
1484 } else {
1485 if (sscanf(WMGetFromPLString(value), "%i,%i", &icon->x_pos, &icon->y_pos) != 2)
1486 COMPLAIN("Position");
1488 /* check position sanity */
1489 if (!onScreen(scr, icon->x_pos, icon->y_pos))
1490 wScreenKeepInside(scr, &icon->x_pos, &icon->y_pos, ICON_SIZE, ICON_SIZE);
1493 #ifdef XDND /* was OFFIX */
1494 value = WMGetFromPLDictionary(clip_state, dDropCommand);
1495 if (value && WMIsPLString(value))
1496 icon->dnd_command = wstrdup(WMGetFromPLString(value));
1497 #endif
1499 value = WMGetFromPLDictionary(clip_state, dPasteCommand);
1500 if (value && WMIsPLString(value))
1501 icon->paste_command = wstrdup(WMGetFromPLString(value));
1503 WMReleasePropList(clip_state);
1505 return icon;
1508 WDock *wDockRestoreState(WScreen *scr, WMPropList *dock_state, int type)
1510 WDock *dock;
1511 WMPropList *apps;
1512 WMPropList *value;
1513 WAppIcon *aicon, *old_top;
1514 int count, i;
1516 dock = wDockCreate(scr, type);
1518 if (!dock_state)
1519 return dock;
1521 WMRetainPropList(dock_state);
1523 /* restore position */
1524 value = WMGetFromPLDictionary(dock_state, dPosition);
1525 if (value) {
1526 if (!WMIsPLString(value)) {
1527 COMPLAIN("Position");
1528 } else {
1529 if (sscanf(WMGetFromPLString(value), "%i,%i", &dock->x_pos, &dock->y_pos) != 2)
1530 COMPLAIN("Position");
1532 /* check position sanity */
1533 if (!onScreen(scr, dock->x_pos, dock->y_pos)) {
1534 int x = dock->x_pos;
1535 wScreenKeepInside(scr, &x, &dock->y_pos, ICON_SIZE, ICON_SIZE);
1538 /* Is this needed any more? */
1539 if (type == WM_CLIP) {
1540 if (dock->x_pos < 0) {
1541 dock->x_pos = 0;
1542 } else if (dock->x_pos > scr->scr_width - ICON_SIZE) {
1543 dock->x_pos = scr->scr_width - ICON_SIZE;
1545 } else {
1546 if (dock->x_pos >= 0) {
1547 dock->x_pos = DOCK_EXTRA_SPACE;
1548 dock->on_right_side = 0;
1549 } else {
1550 dock->x_pos = scr->scr_width - DOCK_EXTRA_SPACE - ICON_SIZE;
1551 dock->on_right_side = 1;
1557 /* restore lowered/raised state */
1558 dock->lowered = 0;
1560 value = WMGetFromPLDictionary(dock_state, dLowered);
1561 if (value) {
1562 if (!WMIsPLString(value)) {
1563 COMPLAIN("Lowered");
1564 } else {
1565 if (strcasecmp(WMGetFromPLString(value), "YES") == 0)
1566 dock->lowered = 1;
1570 /* restore collapsed state */
1571 dock->collapsed = 0;
1573 value = WMGetFromPLDictionary(dock_state, dCollapsed);
1574 if (value) {
1575 if (!WMIsPLString(value)) {
1576 COMPLAIN("Collapsed");
1577 } else {
1578 if (strcasecmp(WMGetFromPLString(value), "YES") == 0)
1579 dock->collapsed = 1;
1583 /* restore auto-collapsed state */
1584 value = WMGetFromPLDictionary(dock_state, dAutoCollapse);
1585 if (value) {
1586 if (!WMIsPLString(value)) {
1587 COMPLAIN("AutoCollapse");
1588 } else {
1589 if (strcasecmp(WMGetFromPLString(value), "YES") == 0) {
1590 dock->auto_collapse = 1;
1591 dock->collapsed = 1;
1596 /* restore auto-raise/lower state */
1597 value = WMGetFromPLDictionary(dock_state, dAutoRaiseLower);
1598 if (value) {
1599 if (!WMIsPLString(value)) {
1600 COMPLAIN("AutoRaiseLower");
1601 } else {
1602 if (strcasecmp(WMGetFromPLString(value), "YES") == 0)
1603 dock->auto_raise_lower = 1;
1607 /* restore attract icons state */
1608 dock->attract_icons = 0;
1610 value = WMGetFromPLDictionary(dock_state, dAutoAttractIcons);
1611 if (value) {
1612 if (!WMIsPLString(value)) {
1613 COMPLAIN("AutoAttractIcons");
1614 } else {
1615 if (strcasecmp(WMGetFromPLString(value), "YES") == 0)
1616 dock->attract_icons = 1;
1620 /* application list */
1623 WMPropList *tmp;
1624 char buffer[64];
1627 * When saving, it saves the dock state in
1628 * Applications and Applicationsnnn
1630 * When loading, it will first try Applicationsnnn.
1631 * If it does not exist, use Applications as default.
1634 snprintf(buffer, sizeof(buffer), "Applications%i", scr->scr_height);
1636 tmp = WMCreatePLString(buffer);
1637 apps = WMGetFromPLDictionary(dock_state, tmp);
1638 WMReleasePropList(tmp);
1640 if (!apps)
1641 apps = WMGetFromPLDictionary(dock_state, dApplications);
1644 if (!apps)
1645 goto finish;
1647 count = WMGetPropListItemCount(apps);
1648 if (count == 0)
1649 goto finish;
1651 old_top = dock->icon_array[0];
1653 /* dock->icon_count is set to 1 when dock is created.
1654 * Since Clip is already restored, we want to keep it so for clip,
1655 * but for dock we may change the default top tile, so we set it to 0.
1657 if (type == WM_DOCK)
1658 dock->icon_count = 0;
1660 for (i = 0; i < count; i++) {
1661 if (dock->icon_count >= dock->max_icons) {
1662 wwarning(_("there are too many icons stored in dock. Ignoring what doesn't fit"));
1663 break;
1666 value = WMGetFromPLArray(apps, i);
1667 aicon = restore_icon_state(scr, value, type, dock->icon_count);
1669 dock->icon_array[dock->icon_count] = aicon;
1671 if (aicon) {
1672 aicon->dock = dock;
1673 aicon->x_pos = dock->x_pos + (aicon->xindex * ICON_SIZE);
1674 aicon->y_pos = dock->y_pos + (aicon->yindex * ICON_SIZE);
1676 if (dock->lowered)
1677 ChangeStackingLevel(aicon->icon->core, WMNormalLevel);
1678 else
1679 ChangeStackingLevel(aicon->icon->core, WMDockLevel);
1681 wCoreConfigure(aicon->icon->core, aicon->x_pos, aicon->y_pos, 0, 0);
1682 if (!dock->collapsed)
1683 XMapWindow(dpy, aicon->icon->core->window);
1685 wRaiseFrame(aicon->icon->core);
1687 dock->icon_count++;
1688 } else if (dock->icon_count == 0 && type == WM_DOCK) {
1689 dock->icon_count++;
1693 /* if the first icon is not defined, use the default */
1694 if (dock->icon_array[0] == NULL) {
1695 /* update default icon */
1696 old_top->x_pos = dock->x_pos;
1697 old_top->y_pos = dock->y_pos;
1698 if (dock->lowered)
1699 ChangeStackingLevel(old_top->icon->core, WMNormalLevel);
1700 else
1701 ChangeStackingLevel(old_top->icon->core, WMDockLevel);
1703 dock->icon_array[0] = old_top;
1704 XMoveWindow(dpy, old_top->icon->core->window, dock->x_pos, dock->y_pos);
1705 /* we don't need to increment dock->icon_count here because it was
1706 * incremented in the loop above.
1708 } else if (old_top != dock->icon_array[0]) {
1709 if (old_top == scr->clip_icon)
1710 scr->clip_icon = dock->icon_array[0];
1712 wAppIconDestroy(old_top);
1715 finish:
1716 WMReleasePropList(dock_state);
1718 return dock;
1721 void wDockLaunchWithState(WDock *dock, WAppIcon *btn, WSavedState *state)
1723 if (btn && btn->command && !btn->running && !btn->launching) {
1724 btn->drop_launch = 0;
1725 btn->paste_launch = 0;
1727 btn->pid = execCommand(btn, btn->command, state);
1729 if (btn->pid > 0) {
1730 if (!btn->forced_dock && !btn->buggy_app) {
1731 btn->launching = 1;
1732 dockIconPaint(btn);
1735 } else {
1736 wfree(state);
1740 void wDockDoAutoLaunch(WDock *dock, int workspace)
1742 WAppIcon *btn;
1743 WSavedState *state;
1744 int i;
1746 for (i = 0; i < dock->max_icons; i++) {
1747 btn = dock->icon_array[i];
1748 if (!btn || !btn->auto_launch)
1749 continue;
1751 state = wmalloc(sizeof(WSavedState));
1752 state->workspace = workspace;
1753 /* TODO: this is klugy and is very difficult to understand
1754 * what's going on. Try to clean up */
1755 wDockLaunchWithState(dock, btn, state);
1759 #ifdef XDND /* was OFFIX */
1760 static WDock *findDock(WScreen *scr, XEvent *event, int *icon_pos)
1762 WDock *dock;
1763 int i;
1765 *icon_pos = -1;
1766 if ((dock = scr->dock) != NULL) {
1767 for (i = 0; i < dock->max_icons; i++) {
1768 if (dock->icon_array[i]
1769 && dock->icon_array[i]->icon->core->window == event->xclient.window) {
1770 *icon_pos = i;
1771 break;
1775 if (*icon_pos < 0 && (dock = scr->workspaces[scr->current_workspace]->clip) != NULL) {
1776 for (i = 0; i < dock->max_icons; i++) {
1777 if (dock->icon_array[i]
1778 && dock->icon_array[i]->icon->core->window == event->xclient.window) {
1779 *icon_pos = i;
1780 break;
1784 if (*icon_pos >= 0)
1785 return dock;
1786 return NULL;
1789 int wDockReceiveDNDDrop(WScreen *scr, XEvent *event)
1791 WDock *dock;
1792 WAppIcon *btn;
1793 int icon_pos;
1795 dock = findDock(scr, event, &icon_pos);
1796 if (!dock)
1797 return False;
1800 * Return True if the drop was on an application icon window.
1801 * In this case, let the ClientMessage handler redirect the
1802 * message to the app.
1804 if (dock->icon_array[icon_pos]->icon->icon_win != None)
1805 return True;
1807 if (dock->icon_array[icon_pos]->dnd_command != NULL) {
1808 scr->flags.dnd_data_convertion_status = 0;
1810 btn = dock->icon_array[icon_pos];
1812 if (!btn->forced_dock) {
1813 btn->relaunching = btn->running;
1814 btn->running = 1;
1816 if (btn->wm_instance || btn->wm_class) {
1817 WWindowAttributes attr;
1818 memset(&attr, 0, sizeof(WWindowAttributes));
1819 wDefaultFillAttributes(btn->wm_instance, btn->wm_class, &attr, NULL, True);
1821 if (!attr.no_appicon)
1822 btn->launching = 1;
1823 else
1824 btn->running = 0;
1827 btn->paste_launch = 0;
1828 btn->drop_launch = 1;
1829 scr->last_dock = dock;
1830 btn->pid = execCommand(btn, btn->dnd_command, NULL);
1831 if (btn->pid > 0) {
1832 dockIconPaint(btn);
1833 } else {
1834 btn->launching = 0;
1835 if (!btn->relaunching)
1836 btn->running = 0;
1839 return False;
1841 #endif /* XDND */
1843 Bool wDockAttachIcon(WDock *dock, WAppIcon *icon, int x, int y, Bool update_icon)
1845 WWindow *wwin;
1846 Bool lupdate_icon = False;
1847 char *command = NULL;
1848 int index;
1850 icon->editing = 0;
1852 if (update_icon)
1853 lupdate_icon = True;
1855 if (icon->command == NULL) {
1856 /* If icon->owner exists, it means the application is running */
1857 if (icon->icon->owner) {
1858 wwin = icon->icon->owner;
1859 command = GetCommandForWindow(wwin->client_win);
1862 if (command) {
1863 icon->command = command;
1864 } else {
1865 /* icon->forced_dock = 1; */
1866 if (dock->type != WM_CLIP || !icon->attracted) {
1867 icon->editing = 1;
1868 if (wInputDialog(dock->screen_ptr, _("Dock Icon"),
1869 _("Type the command used to launch the application"), &command)) {
1870 if (command && (command[0] == 0 || (command[0] == '-' && command[1] == 0))) {
1871 wfree(command);
1872 command = NULL;
1874 icon->command = command;
1875 icon->editing = 0;
1876 } else {
1877 icon->editing = 0;
1878 if (command)
1879 wfree(command);
1880 /* If the target is the dock, reject the icon. If
1881 * the target is the clip, make it an attracted icon
1883 if (dock->type == WM_CLIP) {
1884 icon->attracted = 1;
1885 if (!icon->icon->shadowed) {
1886 icon->icon->shadowed = 1;
1887 lupdate_icon = True;
1889 } else {
1890 return False;
1897 for (index = 1; index < dock->max_icons; index++)
1898 if (dock->icon_array[index] == NULL)
1899 break;
1900 /* if (index == dock->max_icons)
1901 return; */
1903 assert(index < dock->max_icons);
1905 dock->icon_array[index] = icon;
1906 icon->yindex = y;
1907 icon->xindex = x;
1909 icon->omnipresent = 0;
1911 icon->x_pos = dock->x_pos + x * ICON_SIZE;
1912 icon->y_pos = dock->y_pos + y * ICON_SIZE;
1914 dock->icon_count++;
1916 icon->running = 1;
1917 icon->launching = 0;
1918 icon->docked = 1;
1919 icon->dock = dock;
1920 icon->icon->core->descriptor.handle_mousedown = iconMouseDown;
1921 if (dock->type == WM_CLIP) {
1922 icon->icon->core->descriptor.handle_enternotify = clipEnterNotify;
1923 icon->icon->core->descriptor.handle_leavenotify = clipLeaveNotify;
1925 icon->icon->core->descriptor.parent_type = WCLASS_DOCK_ICON;
1926 icon->icon->core->descriptor.parent = icon;
1928 MoveInStackListUnder(dock->icon_array[index - 1]->icon->core, icon->icon->core);
1929 wAppIconMove(icon, icon->x_pos, icon->y_pos);
1931 /* Update the icon images */
1932 if (lupdate_icon)
1933 wIconUpdate(icon->icon, NULL);
1935 /* Paint it */
1936 wAppIconPaint(icon);
1938 /* Save it */
1939 save_appicon(icon, True);
1941 if (wPreferences.auto_arrange_icons)
1942 wArrangeIcons(dock->screen_ptr, True);
1944 #ifdef XDND /* was OFFIX */
1945 if (icon->command && !icon->dnd_command) {
1946 int len = strlen(icon->command) + 8;
1947 icon->dnd_command = wmalloc(len);
1948 snprintf(icon->dnd_command, len, "%s %%d", icon->command);
1950 #endif
1952 if (icon->command && !icon->paste_command) {
1953 int len = strlen(icon->command) + 8;
1954 icon->paste_command = wmalloc(len);
1955 snprintf(icon->paste_command, len, "%s %%s", icon->command);
1958 return True;
1961 void wDockReattachIcon(WDock *dock, WAppIcon *icon, int x, int y)
1963 int index;
1965 for (index = 1; index < dock->max_icons; index++) {
1966 if (dock->icon_array[index] == icon)
1967 break;
1969 assert(index < dock->max_icons);
1971 icon->yindex = y;
1972 icon->xindex = x;
1974 icon->x_pos = dock->x_pos + x * ICON_SIZE;
1975 icon->y_pos = dock->y_pos + y * ICON_SIZE;
1978 Bool wDockMoveIconBetweenDocks(WDock *src, WDock *dest, WAppIcon *icon, int x, int y)
1980 WWindow *wwin;
1981 char *command = NULL;
1982 int index;
1983 Bool update_icon = False;
1985 if (src == dest)
1986 return True; /* No move needed, we're already there */
1988 if (dest == NULL)
1989 return False;
1992 * For the moment we can't do this if we move icons in Clip from one
1993 * workspace to other, because if we move two or more icons without
1994 * command, the dialog box will not be able to tell us to which of the
1995 * moved icons it applies. -Dan
1997 if ((dest->type == WM_DOCK /*|| dest->keep_attracted */ ) && icon->command == NULL) {
1998 /* If icon->owner exists, it means the application is running */
1999 if (icon->icon->owner) {
2000 wwin = icon->icon->owner;
2001 command = GetCommandForWindow(wwin->client_win);
2004 if (command) {
2005 icon->command = command;
2006 } else {
2007 icon->editing = 1;
2008 /* icon->forced_dock = 1; */
2009 if (wInputDialog(src->screen_ptr, _("Dock Icon"),
2010 _("Type the command used to launch the application"), &command)) {
2011 if (command && (command[0] == 0 || (command[0] == '-' && command[1] == 0))) {
2012 wfree(command);
2013 command = NULL;
2015 icon->command = command;
2016 } else {
2017 icon->editing = 0;
2018 if (command)
2019 wfree(command);
2020 return False;
2022 icon->editing = 0;
2026 if (dest->type == WM_DOCK)
2027 wClipMakeIconOmnipresent(icon, False);
2029 for (index = 1; index < src->max_icons; index++) {
2030 if (src->icon_array[index] == icon)
2031 break;
2033 assert(index < src->max_icons);
2035 src->icon_array[index] = NULL;
2036 src->icon_count--;
2038 for (index = 1; index < dest->max_icons; index++) {
2039 if (dest->icon_array[index] == NULL)
2040 break;
2043 assert(index < dest->max_icons);
2045 dest->icon_array[index] = icon;
2046 icon->dock = dest;
2048 /* deselect the icon */
2049 if (icon->icon->selected)
2050 wIconSelect(icon->icon);
2052 if (dest->type == WM_DOCK) {
2053 icon->icon->core->descriptor.handle_enternotify = NULL;
2054 icon->icon->core->descriptor.handle_leavenotify = NULL;
2055 } else {
2056 icon->icon->core->descriptor.handle_enternotify = clipEnterNotify;
2057 icon->icon->core->descriptor.handle_leavenotify = clipLeaveNotify;
2060 /* set it to be kept when moving to dock.
2061 * Unless the icon does not have a command set
2063 if (icon->command && dest->type == WM_DOCK) {
2064 icon->attracted = 0;
2065 if (icon->icon->shadowed) {
2066 icon->icon->shadowed = 0;
2067 update_icon = True;
2069 save_appicon(icon, True);
2072 if (src->auto_collapse || src->auto_raise_lower)
2073 clipLeave(src);
2075 icon->yindex = y;
2076 icon->xindex = x;
2078 icon->x_pos = dest->x_pos + x * ICON_SIZE;
2079 icon->y_pos = dest->y_pos + y * ICON_SIZE;
2081 dest->icon_count++;
2083 MoveInStackListUnder(dest->icon_array[index - 1]->icon->core, icon->icon->core);
2085 /* Update the icon images */
2086 if (update_icon)
2087 wIconUpdate(icon->icon, NULL);
2089 /* Paint it */
2090 wAppIconPaint(icon);
2092 return True;
2095 void wDockDetach(WDock *dock, WAppIcon *icon)
2097 int index;
2098 Bool update_icon = False;
2100 /* make the settings panel be closed */
2101 if (icon->panel)
2102 DestroyDockAppSettingsPanel(icon->panel);
2104 /* This must be called before icon->dock is set to NULL.
2105 * Don't move it. -Dan
2107 wClipMakeIconOmnipresent(icon, False);
2109 icon->docked = 0;
2110 icon->dock = NULL;
2111 icon->attracted = 0;
2112 icon->auto_launch = 0;
2113 if (icon->icon->shadowed) {
2114 icon->icon->shadowed = 0;
2115 update_icon = True;
2118 /* deselect the icon */
2119 if (icon->icon->selected)
2120 wIconSelect(icon->icon);
2122 if (icon->command) {
2123 wfree(icon->command);
2124 icon->command = NULL;
2126 #ifdef XDND /* was OFFIX */
2127 if (icon->dnd_command) {
2128 wfree(icon->dnd_command);
2129 icon->dnd_command = NULL;
2131 #endif
2132 if (icon->paste_command) {
2133 wfree(icon->paste_command);
2134 icon->paste_command = NULL;
2137 for (index = 1; index < dock->max_icons; index++)
2138 if (dock->icon_array[index] == icon)
2139 break;
2141 assert(index < dock->max_icons);
2142 dock->icon_array[index] = NULL;
2143 icon->yindex = -1;
2144 icon->xindex = -1;
2146 dock->icon_count--;
2148 /* if the dock is not attached to an application or
2149 * the application did not set the appropriate hints yet,
2150 * destroy the icon */
2151 if (!icon->running || !wApplicationOf(icon->main_window)) {
2152 wAppIconDestroy(icon);
2153 } else {
2154 icon->icon->core->descriptor.handle_mousedown = appIconMouseDown;
2155 icon->icon->core->descriptor.handle_enternotify = NULL;
2156 icon->icon->core->descriptor.handle_leavenotify = NULL;
2157 icon->icon->core->descriptor.parent_type = WCLASS_APPICON;
2158 icon->icon->core->descriptor.parent = icon;
2160 ChangeStackingLevel(icon->icon->core, NORMAL_ICON_LEVEL);
2162 /* Update the icon images */
2163 if (update_icon)
2164 wIconUpdate(icon->icon, NULL);
2166 /* Paint it */
2167 wAppIconPaint(icon);
2169 if (wPreferences.auto_arrange_icons)
2170 wArrangeIcons(dock->screen_ptr, True);
2172 if (dock->auto_collapse || dock->auto_raise_lower)
2173 clipLeave(dock);
2177 * returns the closest Dock slot index for the passed
2178 * coordinates.
2180 * Returns False if icon can't be docked.
2182 * Note: this function should NEVER alter ret_x or ret_y, unless it will
2183 * return True. -Dan
2185 Bool wDockSnapIcon(WDock *dock, WAppIcon *icon, int req_x, int req_y, int *ret_x, int *ret_y, int redocking)
2187 WScreen *scr = dock->screen_ptr;
2188 int dx, dy;
2189 int ex_x, ex_y;
2190 int i, offset = ICON_SIZE / 2;
2191 WAppIcon *aicon = NULL;
2192 WAppIcon *nicon = NULL;
2193 int max_y_icons;
2195 /* TODO: XINERAMA, for these */
2196 max_y_icons = scr->scr_height / ICON_SIZE - 1;
2198 if (wPreferences.flags.noupdates)
2199 return False;
2201 dx = dock->x_pos;
2202 dy = dock->y_pos;
2204 /* if the dock is full */
2205 if (!redocking && (dock->icon_count >= dock->max_icons))
2206 return False;
2208 /* exact position */
2209 if (req_y < dy)
2210 ex_y = (req_y - offset - dy) / ICON_SIZE;
2211 else
2212 ex_y = (req_y + offset - dy) / ICON_SIZE;
2214 if (req_x < dx)
2215 ex_x = (req_x - offset - dx) / ICON_SIZE;
2216 else
2217 ex_x = (req_x + offset - dx) / ICON_SIZE;
2219 /* check if the icon is outside the screen boundaries */
2220 if (!onScreen(scr, dx + ex_x * ICON_SIZE, dy + ex_y * ICON_SIZE))
2221 return False;
2223 if (dock->type == WM_DOCK) {
2224 if (icon->dock != dock && ex_x != 0)
2225 return False;
2227 aicon = NULL;
2228 for (i = 0; i < dock->max_icons; i++) {
2229 nicon = dock->icon_array[i];
2230 if (nicon && nicon->yindex == ex_y) {
2231 aicon = nicon;
2232 break;
2236 if (redocking) {
2237 int sig, done, closest;
2239 /* Possible cases when redocking:
2241 * icon dragged out of range of any slot -> false
2242 * icon dragged to range of free slot
2243 * icon dragged to range of same slot
2244 * icon dragged to range of different icon
2246 if (abs(ex_x) > DOCK_DETTACH_THRESHOLD)
2247 return False;
2249 if (ex_y >= 0 && ex_y <= max_y_icons && (aicon == icon || !aicon)) {
2250 *ret_x = 0;
2251 *ret_y = ex_y;
2252 return True;
2255 /* start looking at the upper slot or lower? */
2256 if (ex_y * ICON_SIZE < (req_y + offset - dy))
2257 sig = 1;
2258 else
2259 sig = -1;
2261 closest = -1;
2262 done = 0;
2263 /* look for closest free slot */
2264 for (i = 0; i < (DOCK_DETTACH_THRESHOLD + 1) * 2 && !done; i++) {
2265 int j;
2267 done = 1;
2268 closest = sig * (i / 2) + ex_y;
2269 /* check if this slot is used */
2270 if (closest >= 0) {
2271 for (j = 0; j < dock->max_icons; j++) {
2272 if (dock->icon_array[j]
2273 && dock->icon_array[j]->yindex == closest) {
2274 /* slot is used by someone else */
2275 if (dock->icon_array[j] != icon)
2276 done = 0;
2277 break;
2281 sig = -sig;
2283 if (done && closest >= 0 && closest <= max_y_icons &&
2284 ((ex_y >= closest && ex_y - closest < DOCK_DETTACH_THRESHOLD + 1)
2285 || (ex_y < closest && closest - ex_y <= DOCK_DETTACH_THRESHOLD + 1))) {
2286 *ret_x = 0;
2287 *ret_y = closest;
2288 return True;
2290 } else { /* !redocking */
2292 /* if slot is free and the icon is close enough, return it */
2293 if (!aicon && ex_x == 0 && ex_y >= 0 && ex_y <= max_y_icons) {
2294 *ret_x = 0;
2295 *ret_y = ex_y;
2296 return True;
2299 } else { /* CLIP */
2300 int neighbours = 0;
2301 int start, stop, k;
2303 start = icon->omnipresent ? 0 : scr->current_workspace;
2304 stop = icon->omnipresent ? scr->workspace_count : start + 1;
2306 aicon = NULL;
2307 for (k = start; k < stop; k++) {
2308 WDock *tmp = scr->workspaces[k]->clip;
2309 if (!tmp)
2310 continue;
2311 for (i = 0; i < tmp->max_icons; i++) {
2312 nicon = tmp->icon_array[i];
2313 if (nicon && nicon->xindex == ex_x && nicon->yindex == ex_y) {
2314 aicon = nicon;
2315 break;
2318 if (aicon)
2319 break;
2321 for (k = start; k < stop; k++) {
2322 WDock *tmp = scr->workspaces[k]->clip;
2323 if (!tmp)
2324 continue;
2325 for (i = 0; i < tmp->max_icons; i++) {
2326 nicon = tmp->icon_array[i];
2327 if (nicon && nicon != icon && /* Icon can't be it's own neighbour */
2328 (abs(nicon->xindex - ex_x) <= CLIP_ATTACH_VICINITY &&
2329 abs(nicon->yindex - ex_y) <= CLIP_ATTACH_VICINITY)) {
2330 neighbours = 1;
2331 break;
2334 if (neighbours)
2335 break;
2338 if (neighbours && (aicon == NULL || (redocking && aicon == icon))) {
2339 *ret_x = ex_x;
2340 *ret_y = ex_y;
2341 return True;
2344 return False;
2347 static int onScreen(WScreen *scr, int x, int y)
2349 WMRect rect;
2350 int flags;
2352 rect.pos.x = x;
2353 rect.pos.y = y;
2354 rect.size.width = rect.size.height = ICON_SIZE;
2356 wGetRectPlacementInfo(scr, rect, &flags);
2358 return !(flags & (XFLAG_DEAD | XFLAG_PARTIAL));
2362 * returns true if it can find a free slot in the dock,
2363 * in which case it changes x_pos and y_pos accordingly.
2364 * Else returns false.
2366 Bool wDockFindFreeSlot(WDock *dock, int *x_pos, int *y_pos)
2368 WScreen *scr = dock->screen_ptr;
2369 WAppIcon *btn;
2370 WAppIconChain *chain;
2371 unsigned char *slot_map;
2372 int mwidth;
2373 int r;
2374 int x, y;
2375 int i, done = False;
2376 int corner;
2377 int sx = 0, ex = scr->scr_width, ey = scr->scr_height;
2378 int extra_count = 0;
2380 if (dock->type == WM_CLIP && dock != scr->workspaces[scr->current_workspace]->clip)
2381 extra_count = scr->global_icon_count;
2383 /* if the dock is full */
2384 if (dock->icon_count + extra_count >= dock->max_icons)
2385 return False;
2387 if (!wPreferences.flags.nodock && scr->dock) {
2388 if (scr->dock->on_right_side)
2389 ex -= ICON_SIZE + DOCK_EXTRA_SPACE;
2390 else
2391 sx += ICON_SIZE + DOCK_EXTRA_SPACE;
2394 if (ex < dock->x_pos)
2395 ex = dock->x_pos;
2396 if (sx > dock->x_pos + ICON_SIZE)
2397 sx = dock->x_pos + ICON_SIZE;
2398 #define C_NONE 0
2399 #define C_NW 1
2400 #define C_NE 2
2401 #define C_SW 3
2402 #define C_SE 4
2404 /* check if clip is in a corner */
2405 if (dock->type == WM_CLIP) {
2406 if (dock->x_pos < 1 && dock->y_pos < 1)
2407 corner = C_NE;
2408 else if (dock->x_pos < 1 && dock->y_pos >= (ey - ICON_SIZE))
2409 corner = C_SE;
2410 else if (dock->x_pos >= (ex - ICON_SIZE) && dock->y_pos >= (ey - ICON_SIZE))
2411 corner = C_SW;
2412 else if (dock->x_pos >= (ex - ICON_SIZE) && dock->y_pos < 1)
2413 corner = C_NW;
2414 else
2415 corner = C_NONE;
2416 } else {
2417 corner = C_NONE;
2420 /* If the clip is in the corner, use only slots that are in the border
2421 * of the screen */
2422 if (corner != C_NONE) {
2423 char *hmap, *vmap;
2424 int hcount, vcount;
2426 hcount = WMIN(dock->max_icons, scr->scr_width / ICON_SIZE);
2427 vcount = WMIN(dock->max_icons, scr->scr_height / ICON_SIZE);
2428 hmap = wmalloc(hcount + 1);
2429 vmap = wmalloc(vcount + 1);
2431 /* mark used positions */
2432 switch (corner) {
2433 case C_NE:
2434 for (i = 0; i < dock->max_icons; i++) {
2435 btn = dock->icon_array[i];
2436 if (!btn)
2437 continue;
2439 if (btn->xindex == 0 && btn->yindex > 0 && btn->yindex < vcount)
2440 vmap[btn->yindex] = 1;
2441 else if (btn->yindex == 0 && btn->xindex > 0 && btn->xindex < hcount)
2442 hmap[btn->xindex] = 1;
2444 for (chain = scr->global_icons; chain != NULL; chain = chain->next) {
2445 btn = chain->aicon;
2446 if (btn->xindex == 0 && btn->yindex > 0 && btn->yindex < vcount)
2447 vmap[btn->yindex] = 1;
2448 else if (btn->yindex == 0 && btn->xindex > 0 && btn->xindex < hcount)
2449 hmap[btn->xindex] = 1;
2451 break;
2452 case C_NW:
2453 for (i = 0; i < dock->max_icons; i++) {
2454 btn = dock->icon_array[i];
2455 if (!btn)
2456 continue;
2458 if (btn->xindex == 0 && btn->yindex > 0 && btn->yindex < vcount)
2459 vmap[btn->yindex] = 1;
2460 else if (btn->yindex == 0 && btn->xindex < 0 && btn->xindex > -hcount)
2461 hmap[-btn->xindex] = 1;
2463 for (chain = scr->global_icons; chain != NULL; chain = chain->next) {
2464 btn = chain->aicon;
2465 if (btn->xindex == 0 && btn->yindex > 0 && btn->yindex < vcount)
2466 vmap[btn->yindex] = 1;
2467 else if (btn->yindex == 0 && btn->xindex < 0 && btn->xindex > -hcount)
2468 hmap[-btn->xindex] = 1;
2470 break;
2471 case C_SE:
2472 for (i = 0; i < dock->max_icons; i++) {
2473 btn = dock->icon_array[i];
2474 if (!btn)
2475 continue;
2477 if (btn->xindex == 0 && btn->yindex < 0 && btn->yindex > -vcount)
2478 vmap[-btn->yindex] = 1;
2479 else if (btn->yindex == 0 && btn->xindex > 0 && btn->xindex < hcount)
2480 hmap[btn->xindex] = 1;
2482 for (chain = scr->global_icons; chain != NULL; chain = chain->next) {
2483 btn = chain->aicon;
2484 if (btn->xindex == 0 && btn->yindex < 0 && btn->yindex > -vcount)
2485 vmap[-btn->yindex] = 1;
2486 else if (btn->yindex == 0 && btn->xindex > 0 && btn->xindex < hcount)
2487 hmap[btn->xindex] = 1;
2489 break;
2490 case C_SW:
2491 default:
2492 for (i = 0; i < dock->max_icons; i++) {
2493 btn = dock->icon_array[i];
2494 if (!btn)
2495 continue;
2497 if (btn->xindex == 0 && btn->yindex < 0 && btn->yindex > -vcount)
2498 vmap[-btn->yindex] = 1;
2499 else if (btn->yindex == 0 && btn->xindex < 0 && btn->xindex > -hcount)
2500 hmap[-btn->xindex] = 1;
2502 for (chain = scr->global_icons; chain != NULL; chain = chain->next) {
2503 btn = chain->aicon;
2504 if (btn->xindex == 0 && btn->yindex < 0 && btn->yindex > -vcount)
2505 vmap[-btn->yindex] = 1;
2506 else if (btn->yindex == 0 && btn->xindex < 0 && btn->xindex > -hcount)
2507 hmap[-btn->xindex] = 1;
2510 x = 0;
2511 y = 0;
2512 done = 0;
2513 /* search a vacant slot */
2514 for (i = 1; i < WMAX(vcount, hcount); i++) {
2515 if (i < vcount && vmap[i] == 0) {
2516 /* found a slot */
2517 x = 0;
2518 y = i;
2519 done = 1;
2520 break;
2521 } else if (i < hcount && hmap[i] == 0) {
2522 /* found a slot */
2523 x = i;
2524 y = 0;
2525 done = 1;
2526 break;
2529 wfree(vmap);
2530 wfree(hmap);
2531 /* If found a slot, translate and return */
2532 if (done) {
2533 if (corner == C_NW || corner == C_NE)
2534 *y_pos = y;
2535 else
2536 *y_pos = -y;
2538 if (corner == C_NE || corner == C_SE)
2539 *x_pos = x;
2540 else
2541 *x_pos = -x;
2543 return True;
2545 /* else, try to find a slot somewhere else */
2548 /* a map of mwidth x mwidth would be enough if we allowed icons to be
2549 * placed outside of screen */
2550 mwidth = (int)ceil(sqrt(dock->max_icons));
2552 /* In the worst case (the clip is in the corner of the screen),
2553 * the amount of icons that fit in the clip is smaller.
2554 * Double the map to get a safe value.
2556 mwidth += mwidth;
2558 r = (mwidth - 1) / 2;
2560 slot_map = wmalloc(mwidth * mwidth);
2562 #define XY2OFS(x,y) (WMAX(abs(x),abs(y)) > r) ? 0 : (((y)+r)*(mwidth)+(x)+r)
2564 /* mark used slots in the map. If the slot falls outside the map
2565 * (for example, when all icons are placed in line), ignore them. */
2566 for (i = 0; i < dock->max_icons; i++) {
2567 btn = dock->icon_array[i];
2568 if (btn)
2569 slot_map[XY2OFS(btn->xindex, btn->yindex)] = 1;
2572 for (chain = scr->global_icons; chain != NULL; chain = chain->next)
2573 slot_map[XY2OFS(chain->aicon->xindex, chain->aicon->yindex)] = 1;
2575 /* Find closest slot from the center that is free by scanning the
2576 * map from the center to outward in circular passes.
2577 * This will not result in a neat layout, but will be optimal
2578 * in the sense that there will not be holes left.
2580 done = 0;
2581 for (i = 1; i <= r && !done; i++) {
2582 int tx, ty;
2584 /* top and bottom parts of the ring */
2585 for (x = -i; x <= i && !done; x++) {
2586 tx = dock->x_pos + x * ICON_SIZE;
2587 y = -i;
2588 ty = dock->y_pos + y * ICON_SIZE;
2589 if (slot_map[XY2OFS(x, y)] == 0 && onScreen(scr, tx, ty)) {
2590 *x_pos = x;
2591 *y_pos = y;
2592 done = 1;
2593 break;
2595 y = i;
2596 ty = dock->y_pos + y * ICON_SIZE;
2597 if (slot_map[XY2OFS(x, y)] == 0 && onScreen(scr, tx, ty)) {
2598 *x_pos = x;
2599 *y_pos = y;
2600 done = 1;
2601 break;
2604 /* left and right parts of the ring */
2605 for (y = -i + 1; y <= i - 1; y++) {
2606 ty = dock->y_pos + y * ICON_SIZE;
2607 x = -i;
2608 tx = dock->x_pos + x * ICON_SIZE;
2609 if (slot_map[XY2OFS(x, y)] == 0 && onScreen(scr, tx, ty)) {
2610 *x_pos = x;
2611 *y_pos = y;
2612 done = 1;
2613 break;
2615 x = i;
2616 tx = dock->x_pos + x * ICON_SIZE;
2617 if (slot_map[XY2OFS(x, y)] == 0 && onScreen(scr, tx, ty)) {
2618 *x_pos = x;
2619 *y_pos = y;
2620 done = 1;
2621 break;
2625 wfree(slot_map);
2626 #undef XY2OFS
2627 return done;
2630 static void moveDock(WDock *dock, int new_x, int new_y)
2632 WAppIcon *btn;
2633 int i;
2635 dock->x_pos = new_x;
2636 dock->y_pos = new_y;
2637 for (i = 0; i < dock->max_icons; i++) {
2638 btn = dock->icon_array[i];
2639 if (btn) {
2640 btn->x_pos = new_x + btn->xindex * ICON_SIZE;
2641 btn->y_pos = new_y + btn->yindex * ICON_SIZE;
2642 XMoveWindow(dpy, btn->icon->core->window, btn->x_pos, btn->y_pos);
2647 static void swapDock(WDock *dock)
2649 WScreen *scr = dock->screen_ptr;
2650 WAppIcon *btn;
2651 int x, i;
2653 if (dock->on_right_side)
2654 x = dock->x_pos = scr->scr_width - ICON_SIZE - DOCK_EXTRA_SPACE;
2655 else
2656 x = dock->x_pos = DOCK_EXTRA_SPACE;
2658 for (i = 0; i < dock->max_icons; i++) {
2659 btn = dock->icon_array[i];
2660 if (btn) {
2661 btn->x_pos = x;
2662 XMoveWindow(dpy, btn->icon->core->window, btn->x_pos, btn->y_pos);
2666 wScreenUpdateUsableArea(scr);
2669 static pid_t execCommand(WAppIcon *btn, char *command, WSavedState *state)
2671 WScreen *scr = btn->icon->core->screen_ptr;
2672 pid_t pid;
2673 char **argv;
2674 int argc;
2675 char *cmdline;
2677 cmdline = ExpandOptions(scr, command);
2679 if (scr->flags.dnd_data_convertion_status || !cmdline) {
2680 if (cmdline)
2681 wfree(cmdline);
2682 if (state)
2683 wfree(state);
2684 return 0;
2687 wtokensplit(cmdline, &argv, &argc);
2689 if (!argc) {
2690 if (cmdline)
2691 wfree(cmdline);
2692 if (state)
2693 wfree(state);
2694 return 0;
2697 if ((pid = fork()) == 0) {
2698 char **args;
2699 int i;
2701 SetupEnvironment(scr);
2703 #ifdef HAVE_SETSID
2704 setsid();
2705 #endif
2707 args = malloc(sizeof(char *) * (argc + 1));
2708 if (!args)
2709 exit(111);
2711 for (i = 0; i < argc; i++)
2712 args[i] = argv[i];
2714 args[argc] = NULL;
2715 execvp(argv[0], args);
2716 exit(111);
2718 wtokenfree(argv, argc);
2720 if (pid > 0) {
2721 if (!state) {
2722 state = wmalloc(sizeof(WSavedState));
2723 state->hidden = -1;
2724 state->miniaturized = -1;
2725 state->shaded = -1;
2726 if (btn->dock == scr->dock || btn->omnipresent)
2727 state->workspace = -1;
2728 else
2729 state->workspace = scr->current_workspace;
2731 wWindowAddSavedState(btn->wm_instance, btn->wm_class, cmdline, pid, state);
2732 wAddDeathHandler(pid, (WDeathHandler *) trackDeadProcess, btn->dock);
2733 } else if (state) {
2734 wfree(state);
2736 wfree(cmdline);
2737 return pid;
2740 void wDockHideIcons(WDock *dock)
2742 int i;
2744 if (dock == NULL)
2745 return;
2747 for (i = 1; i < dock->max_icons; i++) {
2748 if (dock->icon_array[i])
2749 XUnmapWindow(dpy, dock->icon_array[i]->icon->core->window);
2751 dock->mapped = 0;
2753 dockIconPaint(dock->icon_array[0]);
2756 void wDockShowIcons(WDock *dock)
2758 int i, newlevel;
2759 WAppIcon *btn;
2761 if (dock == NULL)
2762 return;
2764 btn = dock->icon_array[0];
2765 moveDock(dock, btn->x_pos, btn->y_pos);
2767 newlevel = dock->lowered ? WMNormalLevel : WMDockLevel;
2768 ChangeStackingLevel(btn->icon->core, newlevel);
2770 for (i = 1; i < dock->max_icons; i++) {
2771 if (dock->icon_array[i]) {
2772 MoveInStackListAbove(dock->icon_array[i]->icon->core, btn->icon->core);
2773 break;
2777 if (!dock->collapsed) {
2778 for (i = 1; i < dock->max_icons; i++) {
2779 if (dock->icon_array[i])
2780 XMapWindow(dpy, dock->icon_array[i]->icon->core->window);
2783 dock->mapped = 1;
2785 dockIconPaint(btn);
2788 void wDockLower(WDock *dock)
2790 int i;
2792 for (i = 0; i < dock->max_icons; i++) {
2793 if (dock->icon_array[i])
2794 wLowerFrame(dock->icon_array[i]->icon->core);
2798 void wDockRaise(WDock *dock)
2800 int i;
2802 for (i = dock->max_icons - 1; i >= 0; i--) {
2803 if (dock->icon_array[i])
2804 wRaiseFrame(dock->icon_array[i]->icon->core);
2808 void wDockRaiseLower(WDock *dock)
2810 if (!dock->icon_array[0]->icon->core->stacking->above
2811 || (dock->icon_array[0]->icon->core->stacking->window_level
2812 != dock->icon_array[0]->icon->core->stacking->above->stacking->window_level))
2813 wDockLower(dock);
2814 else
2815 wDockRaise(dock);
2818 void wDockFinishLaunch(WDock *dock, WAppIcon *icon)
2820 icon->launching = 0;
2821 icon->relaunching = 0;
2822 dockIconPaint(icon);
2825 WAppIcon *wDockFindIconForWindow(WDock *dock, Window window)
2827 WAppIcon *icon;
2828 int i;
2830 for (i = 0; i < dock->max_icons; i++) {
2831 icon = dock->icon_array[i];
2832 if (icon && icon->main_window == window)
2833 return icon;
2835 return NULL;
2838 void wDockTrackWindowLaunch(WDock *dock, Window window)
2840 WAppIcon *icon;
2841 char *wm_class, *wm_instance;
2842 int i;
2843 Bool firstPass = True;
2844 Bool found = False;
2845 char *command = NULL;
2847 if (!PropGetWMClass(window, &wm_class, &wm_instance) || (!wm_class && !wm_instance))
2848 return;
2850 command = GetCommandForWindow(window);
2851 retry:
2852 for (i = 0; i < dock->max_icons; i++) {
2853 icon = dock->icon_array[i];
2854 if (!icon)
2855 continue;
2857 /* app is already attached to icon */
2858 if (icon->main_window == window) {
2859 found = True;
2860 break;
2863 if ((icon->wm_instance || icon->wm_class)
2864 && (icon->launching || !icon->running)) {
2866 if (icon->wm_instance && wm_instance && strcmp(icon->wm_instance, wm_instance) != 0)
2867 continue;
2869 if (icon->wm_class && wm_class && strcmp(icon->wm_class, wm_class) != 0)
2870 continue;
2872 if (firstPass && command && strcmp(icon->command, command) != 0)
2873 continue;
2875 if (!icon->relaunching) {
2876 WApplication *wapp;
2878 /* Possibly an application that was docked with dockit,
2879 * but the user did not update WMState to indicate that
2880 * it was docked by force */
2881 wapp = wApplicationOf(window);
2882 if (!wapp) {
2883 icon->forced_dock = 1;
2884 icon->running = 0;
2886 if (!icon->forced_dock)
2887 icon->main_window = window;
2889 found = True;
2890 if (!wPreferences.no_animations && !icon->launching &&
2891 !dock->screen_ptr->flags.startup && !dock->collapsed) {
2892 WAppIcon *aicon;
2893 int x0, y0;
2895 icon->launching = 1;
2896 dockIconPaint(icon);
2898 aicon = wAppIconCreateForDock(dock->screen_ptr, NULL,
2899 wm_instance, wm_class, TILE_NORMAL);
2900 /* XXX: can: aicon->icon == NULL ? */
2901 PlaceIcon(dock->screen_ptr, &x0, &y0, wGetHeadForWindow(aicon->icon->owner));
2902 wAppIconMove(aicon, x0, y0);
2903 /* Should this always be lowered? -Dan */
2904 if (dock->lowered)
2905 wLowerFrame(aicon->icon->core);
2906 XMapWindow(dpy, aicon->icon->core->window);
2907 aicon->launching = 1;
2908 wAppIconPaint(aicon);
2909 SlideWindow(aicon->icon->core->window, x0, y0, icon->x_pos, icon->y_pos);
2910 XUnmapWindow(dpy, aicon->icon->core->window);
2911 wAppIconDestroy(aicon);
2913 wDockFinishLaunch(dock, icon);
2914 break;
2918 if (firstPass && !found) {
2919 firstPass = False;
2920 goto retry;
2923 if (command)
2924 wfree(command);
2926 if (wm_class)
2927 free(wm_class);
2928 if (wm_instance)
2929 free(wm_instance);
2932 void wClipUpdateForWorkspaceChange(WScreen *scr, int workspace)
2934 if (!wPreferences.flags.noclip) {
2935 scr->clip_icon->dock = scr->workspaces[workspace]->clip;
2936 if (scr->current_workspace != workspace) {
2937 WDock *old_clip = scr->workspaces[scr->current_workspace]->clip;
2938 WAppIconChain *chain = scr->global_icons;
2940 while (chain) {
2941 wDockMoveIconBetweenDocks(chain->aicon->dock,
2942 scr->workspaces[workspace]->clip,
2943 chain->aicon, chain->aicon->xindex, chain->aicon->yindex);
2944 if (scr->workspaces[workspace]->clip->collapsed)
2945 XUnmapWindow(dpy, chain->aicon->icon->core->window);
2946 chain = chain->next;
2949 wDockHideIcons(old_clip);
2950 if (old_clip->auto_raise_lower) {
2951 if (old_clip->auto_raise_magic) {
2952 WMDeleteTimerHandler(old_clip->auto_raise_magic);
2953 old_clip->auto_raise_magic = NULL;
2955 wDockLower(old_clip);
2957 if (old_clip->auto_collapse) {
2958 if (old_clip->auto_expand_magic) {
2959 WMDeleteTimerHandler(old_clip->auto_expand_magic);
2960 old_clip->auto_expand_magic = NULL;
2962 old_clip->collapsed = 1;
2964 wDockShowIcons(scr->workspaces[workspace]->clip);
2969 static void trackDeadProcess(pid_t pid, unsigned char status, WDock *dock)
2971 WAppIcon *icon;
2972 int i;
2974 for (i = 0; i < dock->max_icons; i++) {
2975 icon = dock->icon_array[i];
2976 if (!icon)
2977 continue;
2979 if (icon->launching && icon->pid == pid) {
2980 if (!icon->relaunching) {
2981 icon->running = 0;
2982 icon->main_window = None;
2984 wDockFinishLaunch(dock, icon);
2985 icon->pid = 0;
2986 if (status == 111) {
2987 char msg[PATH_MAX];
2988 char *cmd;
2990 #ifdef XDND
2991 if (icon->drop_launch)
2992 cmd = icon->dnd_command;
2993 else
2994 #endif
2995 if (icon->paste_launch)
2996 cmd = icon->paste_command;
2997 else
2998 cmd = icon->command;
3000 snprintf(msg, sizeof(msg), _("Could not execute command \"%s\""), cmd);
3002 wMessageDialog(dock->screen_ptr, _("Error"), msg, _("OK"), NULL, NULL);
3004 break;
3009 static void toggleLowered(WDock *dock)
3011 WAppIcon *tmp;
3012 int newlevel, i;
3014 /* lower/raise Dock */
3015 if (!dock->lowered) {
3016 newlevel = WMNormalLevel;
3017 dock->lowered = 1;
3018 } else {
3019 newlevel = WMDockLevel;
3020 dock->lowered = 0;
3023 for (i = 0; i < dock->max_icons; i++) {
3024 tmp = dock->icon_array[i];
3025 if (!tmp)
3026 continue;
3028 ChangeStackingLevel(tmp->icon->core, newlevel);
3029 if (dock->lowered)
3030 wLowerFrame(tmp->icon->core);
3033 if (dock->type == WM_DOCK)
3034 wScreenUpdateUsableArea(dock->screen_ptr);
3037 static void toggleCollapsed(WDock *dock)
3039 if (dock->collapsed) {
3040 dock->collapsed = 0;
3041 wDockShowIcons(dock);
3042 } else {
3043 dock->collapsed = 1;
3044 wDockHideIcons(dock);
3048 static void openDockMenu(WDock *dock, WAppIcon *aicon, XEvent *event)
3050 WScreen *scr = dock->screen_ptr;
3051 WObjDescriptor *desc;
3052 WMenuEntry *entry;
3053 WApplication *wapp = NULL;
3054 int index = 0;
3055 int x_pos;
3056 int n_selected;
3057 int appIsRunning = aicon->running && aicon->icon && aicon->icon->owner;
3059 if (dock->type == WM_DOCK) {
3060 /* keep on top */
3061 entry = dock->menu->entries[index];
3062 entry->flags.indicator_on = !dock->lowered;
3063 entry->clientdata = dock;
3064 dock->menu->flags.realized = 0;
3065 } else {
3066 /* clip options */
3067 if (scr->clip_options)
3068 updateClipOptionsMenu(scr->clip_options, dock);
3070 n_selected = numberOfSelectedIcons(dock);
3072 /* Rename Workspace */
3073 entry = dock->menu->entries[++index];
3074 if (aicon == scr->clip_icon) {
3075 entry->callback = renameCallback;
3076 entry->clientdata = dock;
3077 entry->flags.indicator = 0;
3078 entry->text = _("Rename Workspace");
3079 } else {
3080 entry->callback = omnipresentCallback;
3081 entry->clientdata = aicon;
3082 if (n_selected > 0) {
3083 entry->flags.indicator = 0;
3084 entry->text = _("Toggle Omnipresent");
3085 } else {
3086 entry->flags.indicator = 1;
3087 entry->flags.indicator_on = aicon->omnipresent;
3088 entry->flags.indicator_type = MI_CHECK;
3089 entry->text = _("Omnipresent");
3093 /* select/unselect icon */
3094 entry = dock->menu->entries[++index];
3095 entry->clientdata = aicon;
3096 entry->flags.indicator_on = aicon->icon->selected;
3097 wMenuSetEnabled(dock->menu, index, aicon != scr->clip_icon);
3099 /* select/unselect all icons */
3100 entry = dock->menu->entries[++index];
3101 entry->clientdata = aicon;
3102 if (n_selected > 0)
3103 entry->text = _("Unselect All Icons");
3104 else
3105 entry->text = _("Select All Icons");
3107 wMenuSetEnabled(dock->menu, index, dock->icon_count > 1);
3109 /* keep icon(s) */
3110 entry = dock->menu->entries[++index];
3111 entry->clientdata = aicon;
3112 if (n_selected > 1)
3113 entry->text = _("Keep Icons");
3114 else
3115 entry->text = _("Keep Icon");
3117 wMenuSetEnabled(dock->menu, index, dock->icon_count > 1);
3119 /* this is the workspace submenu part */
3120 entry = dock->menu->entries[++index];
3121 if (n_selected > 1)
3122 entry->text = _("Move Icons To");
3123 else
3124 entry->text = _("Move Icon To");
3126 if (scr->clip_submenu)
3127 updateWorkspaceMenu(scr->clip_submenu, aicon);
3129 wMenuSetEnabled(dock->menu, index, !aicon->omnipresent);
3131 /* remove icon(s) */
3132 entry = dock->menu->entries[++index];
3133 entry->clientdata = aicon;
3134 if (n_selected > 1)
3135 entry->text = _("Remove Icons");
3136 else
3137 entry->text = _("Remove Icon");
3139 wMenuSetEnabled(dock->menu, index, dock->icon_count > 1);
3141 /* attract icon(s) */
3142 entry = dock->menu->entries[++index];
3143 entry->clientdata = aicon;
3145 dock->menu->flags.realized = 0;
3146 wMenuRealize(dock->menu);
3149 if (aicon->icon->owner)
3150 wapp = wApplicationOf(aicon->icon->owner->main_window);
3151 else
3152 wapp = NULL;
3154 /* launch */
3155 entry = dock->menu->entries[++index];
3156 entry->clientdata = aicon;
3157 wMenuSetEnabled(dock->menu, index, aicon->command != NULL);
3159 /* unhide here */
3160 entry = dock->menu->entries[++index];
3161 entry->clientdata = aicon;
3162 if (wapp && wapp->flags.hidden)
3163 entry->text = _("Unhide Here");
3164 else
3165 entry->text = _("Bring Here");
3167 wMenuSetEnabled(dock->menu, index, appIsRunning);
3169 /* hide */
3170 entry = dock->menu->entries[++index];
3171 entry->clientdata = aicon;
3172 if (wapp && wapp->flags.hidden)
3173 entry->text = _("Unhide");
3174 else
3175 entry->text = _("Hide");
3177 wMenuSetEnabled(dock->menu, index, appIsRunning);
3179 /* settings */
3180 entry = dock->menu->entries[++index];
3181 entry->clientdata = aicon;
3182 wMenuSetEnabled(dock->menu, index, !aicon->editing && !wPreferences.flags.noupdates);
3184 /* kill */
3185 entry = dock->menu->entries[++index];
3186 entry->clientdata = aicon;
3187 wMenuSetEnabled(dock->menu, index, appIsRunning);
3189 if (!dock->menu->flags.realized)
3190 wMenuRealize(dock->menu);
3192 if (dock->type == WM_CLIP) {
3193 /*x_pos = event->xbutton.x_root+2; */
3194 x_pos = event->xbutton.x_root - dock->menu->frame->core->width / 2 - 1;
3195 if (x_pos < 0) {
3196 x_pos = 0;
3197 } else if (x_pos + dock->menu->frame->core->width > scr->scr_width - 2) {
3198 x_pos = scr->scr_width - dock->menu->frame->core->width - 4;
3200 } else {
3201 x_pos = dock->on_right_side ? scr->scr_width - dock->menu->frame->core->width - 3 : 0;
3204 wMenuMapAt(dock->menu, x_pos, event->xbutton.y_root + 2, False);
3206 /* allow drag select */
3207 event->xany.send_event = True;
3208 desc = &dock->menu->menu->descriptor;
3209 (*desc->handle_mousedown) (desc, event);
3212 /******************************************************************/
3213 static void iconDblClick(WObjDescriptor *desc, XEvent *event)
3215 WAppIcon *btn = desc->parent;
3216 WDock *dock = btn->dock;
3217 WApplication *wapp = NULL;
3218 int unhideHere = 0;
3220 if (btn->icon->owner && !(event->xbutton.state & ControlMask)) {
3221 wapp = wApplicationOf(btn->icon->owner->main_window);
3223 assert(wapp != NULL);
3225 unhideHere = (event->xbutton.state & ShiftMask);
3227 /* go to the last workspace that the user worked on the app */
3228 if (wapp->last_workspace != dock->screen_ptr->current_workspace && !unhideHere)
3229 wWorkspaceChange(dock->screen_ptr, wapp->last_workspace);
3231 wUnhideApplication(wapp, event->xbutton.button == Button2, unhideHere);
3233 if (event->xbutton.state & MOD_MASK)
3234 wHideOtherApplications(btn->icon->owner);
3235 } else {
3236 if (event->xbutton.button == Button1) {
3237 if (event->xbutton.state & MOD_MASK) {
3238 /* raise/lower dock */
3239 toggleLowered(dock);
3240 } else if (btn == dock->screen_ptr->clip_icon) {
3241 if (getClipButton(event->xbutton.x, event->xbutton.y) == CLIP_IDLE)
3242 toggleCollapsed(dock);
3243 else
3244 handleClipChangeWorkspace(dock->screen_ptr, event);
3245 } else if (btn->command) {
3246 if (!btn->launching && (!btn->running || (event->xbutton.state & ControlMask)))
3247 launchDockedApplication(btn, False);
3248 } else if (btn->xindex == 0 && btn->yindex == 0 && btn->dock->type == WM_DOCK) {
3249 wShowInfoPanel(dock->screen_ptr);
3255 static void handleDockMove(WDock *dock, WAppIcon *aicon, XEvent *event)
3257 WScreen *scr = dock->screen_ptr;
3258 int ofs_x = event->xbutton.x, ofs_y = event->xbutton.y;
3259 int x, y;
3260 XEvent ev;
3261 int grabbed = 0, swapped = 0, done;
3262 Pixmap ghost = None;
3263 int superfluous = wPreferences.superfluous; /* we catch it to avoid problems */
3265 if (XGrabPointer(dpy, aicon->icon->core->window, True, ButtonMotionMask
3266 | ButtonReleaseMask | ButtonPressMask, GrabModeAsync,
3267 GrabModeAsync, None, None, CurrentTime) != GrabSuccess)
3268 wwarning("pointer grab failed for dock move");
3270 y = 0;
3271 for (x = 0; x < dock->max_icons; x++) {
3272 if (dock->icon_array[x] != NULL && dock->icon_array[x]->yindex > y)
3273 y = dock->icon_array[x]->yindex;
3275 y++;
3276 XResizeWindow(dpy, scr->dock_shadow, ICON_SIZE, ICON_SIZE * y);
3278 done = 0;
3279 while (!done) {
3280 WMMaskEvent(dpy, PointerMotionMask | ButtonReleaseMask | ButtonPressMask
3281 | ButtonMotionMask | ExposureMask | EnterWindowMask, &ev);
3282 switch (ev.type) {
3283 case Expose:
3284 WMHandleEvent(&ev);
3285 break;
3287 case EnterNotify:
3288 /* It means the cursor moved so fast that it entered
3289 * something else (if moving slowly, it would have
3290 * stayed in the dock that is being moved. Ignore such
3291 * "spurious" EnterNotifiy's */
3292 break;
3294 case MotionNotify:
3295 if (!grabbed) {
3296 if (abs(ofs_x - ev.xmotion.x) >= MOVE_THRESHOLD
3297 || abs(ofs_y - ev.xmotion.y) >= MOVE_THRESHOLD) {
3298 XChangeActivePointerGrab(dpy, ButtonMotionMask
3299 | ButtonReleaseMask | ButtonPressMask,
3300 wCursor[WCUR_MOVE], CurrentTime);
3301 grabbed = 1;
3303 break;
3305 if (dock->type == WM_CLIP) {
3306 x = ev.xmotion.x_root - ofs_x;
3307 y = ev.xmotion.y_root - ofs_y;
3308 wScreenKeepInside(scr, &x, &y, ICON_SIZE, ICON_SIZE);
3310 moveDock(dock, x, y);
3311 } else {
3312 /* move vertically if pointer is inside the dock */
3313 if ((dock->on_right_side && ev.xmotion.x_root >= dock->x_pos - ICON_SIZE)
3314 || (!dock->on_right_side && ev.xmotion.x_root <= dock->x_pos + ICON_SIZE * 2)) {
3316 x = ev.xmotion.x_root - ofs_x;
3317 y = ev.xmotion.y_root - ofs_y;
3318 wScreenKeepInside(scr, &x, &y, ICON_SIZE, ICON_SIZE);
3319 moveDock(dock, dock->x_pos, y);
3321 /* move horizontally to change sides */
3322 x = ev.xmotion.x_root - ofs_x;
3323 if (!dock->on_right_side) {
3325 /* is on left */
3326 if (ev.xmotion.x_root > dock->x_pos + ICON_SIZE * 2) {
3327 XMoveWindow(dpy, scr->dock_shadow, scr->scr_width - ICON_SIZE
3328 - DOCK_EXTRA_SPACE - 1, dock->y_pos);
3329 if (superfluous && ghost == None) {
3330 ghost = MakeGhostDock(dock, dock->x_pos,
3331 scr->scr_width - ICON_SIZE
3332 - DOCK_EXTRA_SPACE - 1, dock->y_pos);
3333 XSetWindowBackgroundPixmap(dpy, scr->dock_shadow, ghost);
3334 XClearWindow(dpy, scr->dock_shadow);
3336 XMapRaised(dpy, scr->dock_shadow);
3337 swapped = 1;
3338 } else {
3339 if (superfluous && ghost != None) {
3340 XFreePixmap(dpy, ghost);
3341 ghost = None;
3343 XUnmapWindow(dpy, scr->dock_shadow);
3344 swapped = 0;
3346 } else {
3347 /* is on right */
3348 if (ev.xmotion.x_root < dock->x_pos - ICON_SIZE) {
3349 XMoveWindow(dpy, scr->dock_shadow, DOCK_EXTRA_SPACE, dock->y_pos);
3350 if (superfluous && ghost == None) {
3351 ghost = MakeGhostDock(dock, dock->x_pos,
3352 DOCK_EXTRA_SPACE, dock->y_pos);
3353 XSetWindowBackgroundPixmap(dpy, scr->dock_shadow, ghost);
3354 XClearWindow(dpy, scr->dock_shadow);
3356 XMapRaised(dpy, scr->dock_shadow);
3357 swapped = -1;
3358 } else {
3359 XUnmapWindow(dpy, scr->dock_shadow);
3360 swapped = 0;
3361 if (superfluous && ghost != None) {
3362 XFreePixmap(dpy, ghost);
3363 ghost = None;
3368 break;
3370 case ButtonPress:
3371 break;
3373 case ButtonRelease:
3374 if (ev.xbutton.button != event->xbutton.button)
3375 break;
3376 XUngrabPointer(dpy, CurrentTime);
3377 XUnmapWindow(dpy, scr->dock_shadow);
3378 XResizeWindow(dpy, scr->dock_shadow, ICON_SIZE, ICON_SIZE);
3379 if (dock->type == WM_DOCK) {
3380 if (swapped != 0) {
3381 if (swapped > 0)
3382 dock->on_right_side = 1;
3383 else
3384 dock->on_right_side = 0;
3385 swapDock(dock);
3386 wArrangeIcons(scr, False);
3389 done = 1;
3390 break;
3393 if (superfluous) {
3394 if (ghost != None)
3395 XFreePixmap(dpy, ghost);
3396 XSetWindowBackground(dpy, scr->dock_shadow, scr->white_pixel);
3401 static int getClipButton(int px, int py)
3403 int pt = (CLIP_BUTTON_SIZE + 2) * ICON_SIZE / 64;
3405 if (px < 0 || py < 0 || px >= ICON_SIZE || py >= ICON_SIZE)
3406 return CLIP_IDLE;
3408 if (py <= pt - ((int)ICON_SIZE - 1 - px))
3409 return CLIP_FORWARD;
3410 else if (px <= pt - ((int)ICON_SIZE - 1 - py))
3411 return CLIP_REWIND;
3413 return CLIP_IDLE;
3416 static void handleClipChangeWorkspace(WScreen *scr, XEvent *event)
3418 XEvent ev;
3419 int done, direction, new_ws;
3420 int new_dir;
3421 WDock *clip = scr->clip_icon->dock;
3423 direction = getClipButton(event->xbutton.x, event->xbutton.y);
3425 clip->lclip_button_pushed = direction == CLIP_REWIND;
3426 clip->rclip_button_pushed = direction == CLIP_FORWARD;
3428 wClipIconPaint(scr->clip_icon);
3429 done = 0;
3430 while (!done) {
3431 WMMaskEvent(dpy, ExposureMask | ButtonMotionMask | ButtonReleaseMask | ButtonPressMask, &ev);
3432 switch (ev.type) {
3433 case Expose:
3434 WMHandleEvent(&ev);
3435 break;
3437 case MotionNotify:
3438 new_dir = getClipButton(ev.xmotion.x, ev.xmotion.y);
3439 if (new_dir != direction) {
3440 direction = new_dir;
3441 clip->lclip_button_pushed = direction == CLIP_REWIND;
3442 clip->rclip_button_pushed = direction == CLIP_FORWARD;
3443 wClipIconPaint(scr->clip_icon);
3445 break;
3447 case ButtonPress:
3448 break;
3450 case ButtonRelease:
3451 if (ev.xbutton.button == event->xbutton.button)
3452 done = 1;
3456 clip->lclip_button_pushed = 0;
3457 clip->rclip_button_pushed = 0;
3459 new_ws = wPreferences.ws_advance || (event->xbutton.state & ControlMask);
3461 if (direction == CLIP_FORWARD) {
3462 if (scr->current_workspace < scr->workspace_count - 1)
3463 wWorkspaceChange(scr, scr->current_workspace + 1);
3464 else if (new_ws && scr->current_workspace < MAX_WORKSPACES - 1)
3465 wWorkspaceChange(scr, scr->current_workspace + 1);
3466 else if (wPreferences.ws_cycle)
3467 wWorkspaceChange(scr, 0);
3468 } else if (direction == CLIP_REWIND) {
3469 if (scr->current_workspace > 0)
3470 wWorkspaceChange(scr, scr->current_workspace - 1);
3471 else if (scr->current_workspace == 0 && wPreferences.ws_cycle)
3472 wWorkspaceChange(scr, scr->workspace_count - 1);
3475 wClipIconPaint(scr->clip_icon);
3478 static void iconMouseDown(WObjDescriptor *desc, XEvent *event)
3480 WAppIcon *aicon = desc->parent;
3481 WDock *dock = aicon->dock;
3482 WScreen *scr = aicon->icon->core->screen_ptr;
3484 if (aicon->editing || WCHECK_STATE(WSTATE_MODAL))
3485 return;
3487 scr->last_dock = dock;
3489 if (dock->menu->flags.mapped)
3490 wMenuUnmap(dock->menu);
3492 if (IsDoubleClick(scr, event)) {
3493 /* double-click was not in the main clip icon */
3494 if (dock->type != WM_CLIP || aicon->xindex != 0 || aicon->yindex != 0
3495 || getClipButton(event->xbutton.x, event->xbutton.y) == CLIP_IDLE) {
3496 iconDblClick(desc, event);
3497 return;
3501 if (event->xbutton.button == Button1) {
3502 if (event->xbutton.state & MOD_MASK)
3503 wDockLower(dock);
3504 else
3505 wDockRaise(dock);
3507 if ((event->xbutton.state & ShiftMask) && aicon != scr->clip_icon && dock->type != WM_DOCK) {
3508 wIconSelect(aicon->icon);
3509 return;
3512 if (aicon->yindex == 0 && aicon->xindex == 0) {
3513 if (getClipButton(event->xbutton.x, event->xbutton.y) != CLIP_IDLE
3514 && dock->type == WM_CLIP)
3515 handleClipChangeWorkspace(scr, event);
3516 else
3517 handleDockMove(dock, aicon, event);
3518 } else {
3519 Bool hasMoved = wHandleAppIconMove(aicon, event);
3520 if (wPreferences.single_click && !hasMoved)
3521 iconDblClick(desc, event);
3523 } else if (event->xbutton.button == Button2 && dock->type == WM_CLIP && aicon == scr->clip_icon) {
3524 if (!scr->clip_ws_menu) {
3525 scr->clip_ws_menu = wWorkspaceMenuMake(scr, False);
3527 if (scr->clip_ws_menu) {
3528 WMenu *wsMenu = scr->clip_ws_menu;
3529 int xpos;
3531 wWorkspaceMenuUpdate(scr, wsMenu);
3533 xpos = event->xbutton.x_root - wsMenu->frame->core->width / 2 - 1;
3534 if (xpos < 0) {
3535 xpos = 0;
3536 } else if (xpos + wsMenu->frame->core->width > scr->scr_width - 2) {
3537 xpos = scr->scr_width - wsMenu->frame->core->width - 4;
3539 wMenuMapAt(wsMenu, xpos, event->xbutton.y_root + 2, False);
3541 desc = &wsMenu->menu->descriptor;
3542 event->xany.send_event = True;
3543 (*desc->handle_mousedown) (desc, event);
3545 } else if (event->xbutton.button == Button2 && dock->type == WM_CLIP &&
3546 (event->xbutton.state & ShiftMask) && aicon != scr->clip_icon) {
3547 wClipMakeIconOmnipresent(aicon, !aicon->omnipresent);
3548 } else if (event->xbutton.button == Button3) {
3549 if (event->xbutton.send_event &&
3550 XGrabPointer(dpy, aicon->icon->core->window, True, ButtonMotionMask
3551 | ButtonReleaseMask | ButtonPressMask, GrabModeAsync,
3552 GrabModeAsync, None, None, CurrentTime) != GrabSuccess) {
3553 wwarning("pointer grab failed for dockicon menu");
3554 return;
3557 openDockMenu(dock, aicon, event);
3558 } else if (event->xbutton.button == Button2) {
3559 WAppIcon *btn = desc->parent;
3561 if (!btn->launching && (!btn->running || (event->xbutton.state & ControlMask)))
3562 launchDockedApplication(btn, True);
3566 static void clipEnterNotify(WObjDescriptor *desc, XEvent *event)
3568 WAppIcon *btn = (WAppIcon *) desc->parent;
3569 WDock *dock;
3570 WScreen *scr;
3572 assert(event->type == EnterNotify);
3574 if (desc->parent_type != WCLASS_DOCK_ICON)
3575 return;
3577 scr = btn->icon->core->screen_ptr;
3578 dock = btn->dock;
3580 if (!dock || dock->type != WM_CLIP)
3581 return;
3583 /* The auto raise/lower code */
3584 if (dock->auto_lower_magic) {
3585 WMDeleteTimerHandler(dock->auto_lower_magic);
3586 dock->auto_lower_magic = NULL;
3588 if (dock->auto_raise_lower && !dock->auto_raise_magic)
3589 dock->auto_raise_magic = WMAddTimerHandler(wPreferences.clip_auto_raise_delay, clipAutoRaise, (void *)dock);
3591 /* The auto expand/collapse code */
3592 if (dock->auto_collapse_magic) {
3593 WMDeleteTimerHandler(dock->auto_collapse_magic);
3594 dock->auto_collapse_magic = NULL;
3596 if (dock->auto_collapse && !dock->auto_expand_magic)
3597 dock->auto_expand_magic = WMAddTimerHandler(wPreferences.clip_auto_expand_delay, clipAutoExpand, (void *)dock);
3600 static void clipLeave(WDock *dock)
3602 XEvent event;
3603 WObjDescriptor *desc = NULL;
3605 if (!dock || dock->type != WM_CLIP)
3606 return;
3608 if (XCheckTypedEvent(dpy, EnterNotify, &event) != False) {
3609 if (XFindContext(dpy, event.xcrossing.window, wWinContext,
3610 (XPointer *) & desc) != XCNOENT
3611 && desc && desc->parent_type == WCLASS_DOCK_ICON
3612 && ((WAppIcon *) desc->parent)->dock && ((WAppIcon *) desc->parent)->dock->type == WM_CLIP) {
3613 /* We didn't left the Clip yet */
3614 XPutBackEvent(dpy, &event);
3615 return;
3618 XPutBackEvent(dpy, &event);
3619 } else {
3620 /* We entered a withdrawn window, so we're still in Clip */
3621 return;
3624 if (dock->auto_raise_magic) {
3625 WMDeleteTimerHandler(dock->auto_raise_magic);
3626 dock->auto_raise_magic = NULL;
3628 if (dock->auto_raise_lower && !dock->auto_lower_magic)
3629 dock->auto_lower_magic = WMAddTimerHandler(wPreferences.clip_auto_lower_delay, clipAutoLower, (void *)dock);
3631 if (dock->auto_expand_magic) {
3632 WMDeleteTimerHandler(dock->auto_expand_magic);
3633 dock->auto_expand_magic = NULL;
3635 if (dock->auto_collapse && !dock->auto_collapse_magic)
3636 dock->auto_collapse_magic = WMAddTimerHandler(wPreferences.clip_auto_collapse_delay, clipAutoCollapse, (void *)dock);
3639 static void clipLeaveNotify(WObjDescriptor *desc, XEvent *event)
3641 WAppIcon *btn = (WAppIcon *) desc->parent;
3643 assert(event->type == LeaveNotify);
3645 if (desc->parent_type != WCLASS_DOCK_ICON)
3646 return;
3648 clipLeave(btn->dock);
3651 static void clipAutoCollapse(void *cdata)
3653 WDock *dock = (WDock *) cdata;
3655 if (dock->type != WM_CLIP)
3656 return;
3658 if (dock->auto_collapse) {
3659 dock->collapsed = 1;
3660 wDockHideIcons(dock);
3662 dock->auto_collapse_magic = NULL;
3665 static void clipAutoExpand(void *cdata)
3667 WDock *dock = (WDock *) cdata;
3669 if (dock->type != WM_CLIP)
3670 return;
3672 if (dock->auto_collapse) {
3673 dock->collapsed = 0;
3674 wDockShowIcons(dock);
3676 dock->auto_expand_magic = NULL;
3679 static void clipAutoLower(void *cdata)
3681 WDock *dock = (WDock *) cdata;
3683 if (dock->type != WM_CLIP)
3684 return;
3686 if (dock->auto_raise_lower)
3687 wDockLower(dock);
3689 dock->auto_lower_magic = NULL;
3692 static void clipAutoRaise(void *cdata)
3694 WDock *dock = (WDock *) cdata;
3696 if (dock->type != WM_CLIP)
3697 return;
3699 if (dock->auto_raise_lower)
3700 wDockRaise(dock);
3702 dock->auto_raise_magic = NULL;
3705 static Bool iconCanBeOmnipresent(WAppIcon *aicon)
3707 WScreen *scr = aicon->icon->core->screen_ptr;
3708 WDock *clip;
3709 WAppIcon *btn;
3710 int i, j;
3712 for (i = 0; i < scr->workspace_count; i++) {
3713 clip = scr->workspaces[i]->clip;
3715 if (clip == aicon->dock)
3716 continue;
3718 if (clip->icon_count + scr->global_icon_count >= clip->max_icons)
3719 return False; /* Clip is full in some workspace */
3721 for (j = 0; j < clip->max_icons; j++) {
3722 btn = clip->icon_array[j];
3723 if (btn && btn->xindex == aicon->xindex && btn->yindex == aicon->yindex)
3724 return False;
3728 return True;
3731 int wClipMakeIconOmnipresent(WAppIcon *aicon, int omnipresent)
3733 WScreen *scr = aicon->icon->core->screen_ptr;
3734 WAppIconChain *new_entry, *tmp, *tmp1;
3735 int status = WO_SUCCESS;
3737 if ((scr->dock && aicon->dock == scr->dock) || aicon == scr->clip_icon)
3738 return WO_NOT_APPLICABLE;
3740 if (aicon->omnipresent == omnipresent)
3741 return WO_SUCCESS;
3743 if (omnipresent) {
3744 if (iconCanBeOmnipresent(aicon)) {
3745 aicon->omnipresent = 1;
3746 new_entry = wmalloc(sizeof(WAppIconChain));
3747 new_entry->aicon = aicon;
3748 new_entry->next = scr->global_icons;
3749 scr->global_icons = new_entry;
3750 scr->global_icon_count++;
3751 } else {
3752 aicon->omnipresent = 0;
3753 status = WO_FAILED;
3755 } else {
3756 aicon->omnipresent = 0;
3757 if (aicon == scr->global_icons->aicon) {
3758 tmp = scr->global_icons->next;
3759 wfree(scr->global_icons);
3760 scr->global_icons = tmp;
3761 scr->global_icon_count--;
3762 } else {
3763 tmp = scr->global_icons;
3764 while (tmp->next) {
3765 if (tmp->next->aicon == aicon) {
3766 tmp1 = tmp->next->next;
3767 wfree(tmp->next);
3768 tmp->next = tmp1;
3769 scr->global_icon_count--;
3770 break;
3772 tmp = tmp->next;
3777 wAppIconPaint(aicon);
3779 return status;