6e2e0e319203440f3e51f83d8124368d45cabf25
[wmaker-crm.git] / src / dock.c
blob6e2e0e319203440f3e51f83d8124368d45cabf25
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 Bool moveIconBetweenDocks(WDock *src, WDock *dest, WAppIcon *icon, int x, int y);
110 static void clipEnterNotify(WObjDescriptor *desc, XEvent *event);
111 static void clipLeaveNotify(WObjDescriptor *desc, XEvent *event);
112 static void clipAutoCollapse(void *cdata);
113 static void clipAutoExpand(void *cdata);
114 static void launchDockedApplication(WAppIcon *btn, Bool withSelection);
116 static void clipAutoLower(void *cdata);
117 static void clipAutoRaise(void *cdata);
118 static void reattachIcon(WDock *dock, WAppIcon *icon, int x, int y);
119 static WAppIcon *mainIconCreate(WScreen *scr, int type);
121 static void make_keys(void)
123 if (dCommand != NULL)
124 return;
126 dCommand = WMRetainPropList(WMCreatePLString("Command"));
127 dPasteCommand = WMRetainPropList(WMCreatePLString("PasteCommand"));
128 #ifdef XDND
129 dDropCommand = WMRetainPropList(WMCreatePLString("DropCommand"));
130 #endif
131 dLock = WMRetainPropList(WMCreatePLString("Lock"));
132 dAutoLaunch = WMRetainPropList(WMCreatePLString("AutoLaunch"));
133 dName = WMRetainPropList(WMCreatePLString("Name"));
134 dForced = WMRetainPropList(WMCreatePLString("Forced"));
135 dBuggyApplication = WMRetainPropList(WMCreatePLString("BuggyApplication"));
136 dYes = WMRetainPropList(WMCreatePLString("Yes"));
137 dNo = WMRetainPropList(WMCreatePLString("No"));
138 dHost = WMRetainPropList(WMCreatePLString("Host"));
140 dPosition = WMCreatePLString("Position");
141 dApplications = WMCreatePLString("Applications");
142 dLowered = WMCreatePLString("Lowered");
143 dCollapsed = WMCreatePLString("Collapsed");
144 dAutoCollapse = WMCreatePLString("AutoCollapse");
145 dAutoRaiseLower = WMCreatePLString("AutoRaiseLower");
146 dAutoAttractIcons = WMCreatePLString("AutoAttractIcons");
148 dOmnipresent = WMCreatePLString("Omnipresent");
150 dDock = WMCreatePLString("Dock");
151 dClip = WMCreatePLString("Clip");
154 static void renameCallback(WMenu *menu, WMenuEntry *entry)
156 WDock *dock = entry->clientdata;
157 char buffer[128];
158 int wspace;
159 char *name;
161 assert(entry->clientdata != NULL);
163 wspace = dock->screen_ptr->current_workspace;
165 name = wstrdup(dock->screen_ptr->workspaces[wspace]->name);
167 snprintf(buffer, sizeof(buffer), _("Type the name for workspace %i:"), wspace + 1);
168 if (wInputDialog(dock->screen_ptr, _("Rename Workspace"), buffer, &name))
169 wWorkspaceRename(dock->screen_ptr, wspace, name);
171 wfree(name);
174 static void toggleLoweredCallback(WMenu *menu, WMenuEntry *entry)
176 assert(entry->clientdata != NULL);
178 toggleLowered(entry->clientdata);
180 entry->flags.indicator_on = !(((WDock *) entry->clientdata)->lowered);
182 wMenuPaint(menu);
185 static int matchWindow(const void *item, const void *cdata)
187 return (((WFakeGroupLeader *) item)->leader == (Window) cdata);
190 static void killCallback(WMenu *menu, WMenuEntry *entry)
192 WScreen *scr = menu->menu->screen_ptr;
193 WAppIcon *icon;
194 WFakeGroupLeader *fPtr;
195 char *buffer, *shortname, **argv;
196 char *basename(const char *shortname);
197 int argc;
199 if (!WCHECK_STATE(WSTATE_NORMAL))
200 return;
202 assert(entry->clientdata != NULL);
204 icon = (WAppIcon *) entry->clientdata;
206 icon->editing = 1;
208 WCHANGE_STATE(WSTATE_MODAL);
210 /* strip away dir names */
211 shortname = basename(icon->command);
212 /* separate out command options */
213 wtokensplit(shortname, &argv, &argc);
215 buffer = wstrconcat(argv[0],
216 _(" will be forcibly closed.\n"
217 "Any unsaved changes will be lost.\n" "Please confirm."));
219 if (icon->icon && icon->icon->owner) {
220 fPtr = icon->icon->owner->fake_group;
221 } else {
222 /* is this really necessary? can we kill a non-running dock icon? */
223 Window win = icon->main_window;
224 int index;
226 index = WMFindInArray(scr->fakeGroupLeaders, matchWindow, (void *)win);
227 if (index != WANotFound)
228 fPtr = WMGetFromArray(scr->fakeGroupLeaders, index);
229 else
230 fPtr = NULL;
233 if (wPreferences.dont_confirm_kill
234 || wMessageDialog(menu->frame->screen_ptr, _("Kill Application"),
235 buffer, _("Yes"), _("No"), NULL) == WAPRDefault) {
236 if (fPtr != NULL) {
237 WWindow *wwin, *twin;
239 wwin = scr->focused_window;
240 while (wwin) {
241 twin = wwin->prev;
242 if (wwin->fake_group == fPtr)
243 wClientKill(wwin);
245 wwin = twin;
247 } else if (icon->icon && icon->icon->owner) {
248 wClientKill(icon->icon->owner);
252 wfree(buffer);
253 wtokenfree(argv, argc);
255 icon->editing = 0;
257 WCHANGE_STATE(WSTATE_NORMAL);
260 /* TODO: replace this function with a member of the dock struct */
261 static int numberOfSelectedIcons(WDock *dock)
263 WAppIcon *aicon;
264 int i, n;
266 n = 0;
267 for (i = 1; i < dock->max_icons; i++) {
268 aicon = dock->icon_array[i];
269 if (aicon && aicon->icon->selected)
270 n++;
273 return n;
276 static WMArray *getSelected(WDock *dock)
278 WMArray *ret = WMCreateArray(8);
279 WAppIcon *btn;
280 int i;
282 for (i = 1; i < dock->max_icons; i++) {
283 btn = dock->icon_array[i];
284 if (btn && btn->icon->selected)
285 WMAddToArray(ret, btn);
288 return ret;
291 static void paintClipButtons(WAppIcon *clipIcon, Bool lpushed, Bool rpushed)
293 Window win = clipIcon->icon->core->window;
294 WScreen *scr = clipIcon->icon->core->screen_ptr;
295 XPoint p[4];
296 int pt = CLIP_BUTTON_SIZE * ICON_SIZE / 64;
297 int tp = ICON_SIZE - pt;
298 int as = pt - 15; /* 15 = 5+5+5 */
299 GC gc = scr->draw_gc; /* maybe use WMColorGC() instead here? */
300 WMColor *color;
302 color = scr->clip_title_color[CLIP_NORMAL];
304 XSetForeground(dpy, gc, WMColorPixel(color));
306 if (rpushed) {
307 p[0].x = tp + 1;
308 p[0].y = 1;
309 p[1].x = ICON_SIZE - 2;
310 p[1].y = 1;
311 p[2].x = ICON_SIZE - 2;
312 p[2].y = pt - 1;
313 } else if (lpushed) {
314 p[0].x = 1;
315 p[0].y = tp;
316 p[1].x = pt;
317 p[1].y = ICON_SIZE - 2;
318 p[2].x = 1;
319 p[2].y = ICON_SIZE - 2;
321 if (lpushed || rpushed) {
322 XSetForeground(dpy, scr->draw_gc, scr->white_pixel);
323 XFillPolygon(dpy, win, scr->draw_gc, p, 3, Convex, CoordModeOrigin);
324 XSetForeground(dpy, scr->draw_gc, scr->black_pixel);
327 /* top right arrow */
328 p[0].x = p[3].x = ICON_SIZE - 5 - as;
329 p[0].y = p[3].y = 5;
330 p[1].x = ICON_SIZE - 6;
331 p[1].y = 5;
332 p[2].x = ICON_SIZE - 6;
333 p[2].y = 4 + as;
334 if (rpushed) {
335 XFillPolygon(dpy, win, scr->draw_gc, p, 3, Convex, CoordModeOrigin);
336 XDrawLines(dpy, win, scr->draw_gc, p, 4, CoordModeOrigin);
337 } else {
338 XFillPolygon(dpy, win, gc, p, 3, Convex, CoordModeOrigin);
339 XDrawLines(dpy, win, gc, p, 4, CoordModeOrigin);
342 /* bottom left arrow */
343 p[0].x = p[3].x = 5;
344 p[0].y = p[3].y = ICON_SIZE - 5 - as;
345 p[1].x = 5;
346 p[1].y = ICON_SIZE - 6;
347 p[2].x = 4 + as;
348 p[2].y = ICON_SIZE - 6;
349 if (lpushed) {
350 XFillPolygon(dpy, win, scr->draw_gc, p, 3, Convex, CoordModeOrigin);
351 XDrawLines(dpy, win, scr->draw_gc, p, 4, CoordModeOrigin);
352 } else {
353 XFillPolygon(dpy, win, gc, p, 3, Convex, CoordModeOrigin);
354 XDrawLines(dpy, win, gc, p, 4, CoordModeOrigin);
358 RImage *wClipMakeTile(WScreen *scr, RImage *normalTile)
360 RImage *tile = RCloneImage(normalTile);
361 RColor black;
362 RColor dark;
363 RColor light;
364 int pt, tp;
365 int as;
367 pt = CLIP_BUTTON_SIZE * wPreferences.icon_size / 64;
368 tp = wPreferences.icon_size - 1 - pt;
369 as = pt - 15;
371 black.alpha = 255;
372 black.red = black.green = black.blue = 0;
374 dark.alpha = 0;
375 dark.red = dark.green = dark.blue = 60;
377 light.alpha = 0;
378 light.red = light.green = light.blue = 80;
380 /* top right */
381 ROperateLine(tile, RSubtractOperation, tp, 0, wPreferences.icon_size - 2, pt - 1, &dark);
382 RDrawLine(tile, tp - 1, 0, wPreferences.icon_size - 1, pt + 1, &black);
383 ROperateLine(tile, RAddOperation, tp, 2, wPreferences.icon_size - 3, pt, &light);
385 /* arrow bevel */
386 ROperateLine(tile, RSubtractOperation, ICON_SIZE - 7 - as, 4, ICON_SIZE - 5, 4, &dark);
387 ROperateLine(tile, RSubtractOperation, ICON_SIZE - 6 - as, 5, ICON_SIZE - 5, 6 + as, &dark);
388 ROperateLine(tile, RAddOperation, ICON_SIZE - 5, 4, ICON_SIZE - 5, 6 + as, &light);
390 /* bottom left */
391 ROperateLine(tile, RAddOperation, 2, tp + 2, pt - 2, wPreferences.icon_size - 3, &dark);
392 RDrawLine(tile, 0, tp - 1, pt + 1, wPreferences.icon_size - 1, &black);
393 ROperateLine(tile, RSubtractOperation, 0, tp - 2, pt + 1, wPreferences.icon_size - 2, &light);
395 /* arrow bevel */
396 ROperateLine(tile, RSubtractOperation, 4, ICON_SIZE - 7 - as, 4, ICON_SIZE - 5, &dark);
397 ROperateLine(tile, RSubtractOperation, 5, ICON_SIZE - 6 - as, 6 + as, ICON_SIZE - 5, &dark);
398 ROperateLine(tile, RAddOperation, 4, ICON_SIZE - 5, 6 + as, ICON_SIZE - 5, &light);
400 return tile;
403 static void omnipresentCallback(WMenu *menu, WMenuEntry *entry)
405 WAppIcon *clickedIcon = entry->clientdata;
406 WAppIcon *aicon;
407 WDock *dock;
408 WMArray *selectedIcons;
409 WMArrayIterator iter;
410 int failed;
412 assert(entry->clientdata != NULL);
414 dock = clickedIcon->dock;
416 selectedIcons = getSelected(dock);
418 if (!WMGetArrayItemCount(selectedIcons))
419 WMAddToArray(selectedIcons, clickedIcon);
421 failed = 0;
422 WM_ITERATE_ARRAY(selectedIcons, aicon, iter) {
423 if (wClipMakeIconOmnipresent(aicon, !aicon->omnipresent) == WO_FAILED)
424 failed++;
425 else if (aicon->icon->selected)
426 wIconSelect(aicon->icon);
428 WMFreeArray(selectedIcons);
430 if (failed > 1) {
431 wMessageDialog(dock->screen_ptr, _("Warning"),
432 _("Some icons cannot be made omnipresent. "
433 "Please make sure that no other icon is "
434 "docked in the same positions on the other "
435 "workspaces and the Clip is not full in "
436 "some workspace."), _("OK"), NULL, NULL);
437 } else if (failed == 1) {
438 wMessageDialog(dock->screen_ptr, _("Warning"),
439 _("Icon cannot be made omnipresent. "
440 "Please make sure that no other icon is "
441 "docked in the same position on the other "
442 "workspaces and the Clip is not full in "
443 "some workspace."), _("OK"), NULL, NULL);
447 static void removeIconsCallback(WMenu *menu, WMenuEntry *entry)
449 WAppIcon *clickedIcon = (WAppIcon *) entry->clientdata;
450 WDock *dock;
451 WAppIcon *aicon;
452 WMArray *selectedIcons;
453 int keepit;
454 WMArrayIterator it;
456 assert(clickedIcon != NULL);
458 dock = clickedIcon->dock;
460 selectedIcons = getSelected(dock);
462 if (WMGetArrayItemCount(selectedIcons)) {
463 if (wMessageDialog(dock->screen_ptr, _("Workspace Clip"),
464 _("All selected icons will be removed!"),
465 _("OK"), _("Cancel"), NULL) != WAPRDefault) {
466 WMFreeArray(selectedIcons);
467 return;
469 } else {
470 if (clickedIcon->xindex == 0 && clickedIcon->yindex == 0) {
471 WMFreeArray(selectedIcons);
472 return;
474 WMAddToArray(selectedIcons, clickedIcon);
477 WM_ITERATE_ARRAY(selectedIcons, aicon, it) {
478 keepit = aicon->running && wApplicationOf(aicon->main_window);
479 wDockDetach(dock, aicon);
480 if (keepit) {
481 /* XXX: can: aicon->icon == NULL ? */
482 PlaceIcon(dock->screen_ptr, &aicon->x_pos, &aicon->y_pos,
483 wGetHeadForWindow(aicon->icon->owner));
484 XMoveWindow(dpy, aicon->icon->core->window, aicon->x_pos, aicon->y_pos);
485 if (!dock->mapped || dock->collapsed)
486 XMapWindow(dpy, aicon->icon->core->window);
489 WMFreeArray(selectedIcons);
491 if (wPreferences.auto_arrange_icons)
492 wArrangeIcons(dock->screen_ptr, True);
495 static void keepIconsCallback(WMenu *menu, WMenuEntry *entry)
497 WAppIcon *clickedIcon = (WAppIcon *) entry->clientdata;
498 WDock *dock;
499 WAppIcon *aicon;
500 WMArray *selectedIcons;
501 WMArrayIterator it;
503 assert(clickedIcon != NULL);
504 dock = clickedIcon->dock;
506 selectedIcons = getSelected(dock);
508 if (!WMGetArrayItemCount(selectedIcons)
509 && clickedIcon != dock->screen_ptr->clip_icon) {
510 char *command = NULL;
512 if (!clickedIcon->command && !clickedIcon->editing) {
513 clickedIcon->editing = 1;
514 if (wInputDialog(dock->screen_ptr, _("Keep Icon"),
515 _("Type the command used to launch the application"), &command)) {
516 if (command && (command[0] == 0 || (command[0] == '-' && command[1] == 0))) {
517 wfree(command);
518 command = NULL;
520 clickedIcon->command = command;
521 clickedIcon->editing = 0;
522 } else {
523 clickedIcon->editing = 0;
524 if (command)
525 wfree(command);
526 WMFreeArray(selectedIcons);
527 return;
531 WMAddToArray(selectedIcons, clickedIcon);
534 WM_ITERATE_ARRAY(selectedIcons, aicon, it) {
535 if (aicon->icon->selected)
536 wIconSelect(aicon->icon);
538 if (aicon && aicon->attracted && aicon->command) {
539 aicon->attracted = 0;
540 if (aicon->icon->shadowed) {
541 aicon->icon->shadowed = 0;
543 /* Update the icon images */
544 wIconUpdate(aicon->icon, NULL);
546 /* Paint it */
547 wAppIconPaint(aicon);
550 save_appicon(aicon, True);
552 WMFreeArray(selectedIcons);
555 static void toggleAutoAttractCallback(WMenu *menu, WMenuEntry *entry)
557 WDock *dock = (WDock *) entry->clientdata;
559 assert(entry->clientdata != NULL);
561 dock->attract_icons = !dock->attract_icons;
563 entry->flags.indicator_on = dock->attract_icons;
565 wMenuPaint(menu);
568 static void selectCallback(WMenu *menu, WMenuEntry *entry)
570 WAppIcon *icon = (WAppIcon *) entry->clientdata;
572 assert(icon != NULL);
574 wIconSelect(icon->icon);
576 wMenuPaint(menu);
579 static void colectIconsCallback(WMenu *menu, WMenuEntry *entry)
581 WAppIcon *clickedIcon = (WAppIcon *) entry->clientdata;
582 WDock *clip;
583 WAppIcon *aicon;
584 int x, y, x_pos, y_pos;
585 Bool update_icon = False;
587 assert(entry->clientdata != NULL);
588 clip = clickedIcon->dock;
590 aicon = clip->screen_ptr->app_icon_list;
592 while (aicon) {
593 if (!aicon->docked && wDockFindFreeSlot(clip, &x, &y)) {
594 x_pos = clip->x_pos + x * ICON_SIZE;
595 y_pos = clip->y_pos + y * ICON_SIZE;
596 if (aicon->x_pos != x_pos || aicon->y_pos != y_pos)
597 move_window(aicon->icon->core->window, aicon->x_pos, aicon->y_pos, x_pos, y_pos);
599 aicon->attracted = 1;
600 if (!aicon->icon->shadowed) {
601 aicon->icon->shadowed = 1;
602 update_icon = True;
604 wDockAttachIcon(clip, aicon, x, y, update_icon);
605 if (clip->collapsed || !clip->mapped)
606 XUnmapWindow(dpy, aicon->icon->core->window);
608 aicon = aicon->next;
612 static void selectIconsCallback(WMenu *menu, WMenuEntry *entry)
614 WAppIcon *clickedIcon = (WAppIcon *) entry->clientdata;
615 WDock *dock;
616 WMArray *selectedIcons;
617 WMArrayIterator iter;
618 WAppIcon *btn;
619 int i;
621 assert(clickedIcon != NULL);
622 dock = clickedIcon->dock;
624 selectedIcons = getSelected(dock);
626 if (!WMGetArrayItemCount(selectedIcons)) {
627 for (i = 1; i < dock->max_icons; i++) {
628 btn = dock->icon_array[i];
629 if (btn && !btn->icon->selected)
630 wIconSelect(btn->icon);
632 } else {
633 WM_ITERATE_ARRAY(selectedIcons, btn, iter) {
634 wIconSelect(btn->icon);
637 WMFreeArray(selectedIcons);
639 wMenuPaint(menu);
642 static void toggleCollapsedCallback(WMenu *menu, WMenuEntry *entry)
644 assert(entry->clientdata != NULL);
646 toggleCollapsed(entry->clientdata);
648 entry->flags.indicator_on = ((WDock *) entry->clientdata)->collapsed;
650 wMenuPaint(menu);
653 static void toggleAutoCollapseCallback(WMenu *menu, WMenuEntry *entry)
655 WDock *dock;
656 assert(entry->clientdata != NULL);
658 dock = (WDock *) entry->clientdata;
660 dock->auto_collapse = !dock->auto_collapse;
662 entry->flags.indicator_on = ((WDock *) entry->clientdata)->auto_collapse;
664 wMenuPaint(menu);
667 static void toggleAutoRaiseLowerCallback(WMenu *menu, WMenuEntry *entry)
669 WDock *dock;
670 assert(entry->clientdata != NULL);
672 dock = (WDock *) entry->clientdata;
674 dock->auto_raise_lower = !dock->auto_raise_lower;
676 entry->flags.indicator_on = ((WDock *) entry->clientdata)->auto_raise_lower;
678 wMenuPaint(menu);
681 static void launchCallback(WMenu *menu, WMenuEntry *entry)
683 WAppIcon *btn = (WAppIcon *) entry->clientdata;
685 launchDockedApplication(btn, False);
688 static void settingsCallback(WMenu *menu, WMenuEntry *entry)
690 WAppIcon *btn = (WAppIcon *) entry->clientdata;
692 if (btn->editing)
693 return;
694 ShowDockAppSettingsPanel(btn);
697 static void hideCallback(WMenu *menu, WMenuEntry *entry)
699 WApplication *wapp;
700 WAppIcon *btn = (WAppIcon *) entry->clientdata;
702 wapp = wApplicationOf(btn->icon->owner->main_window);
704 if (wapp->flags.hidden) {
705 wWorkspaceChange(btn->icon->core->screen_ptr, wapp->last_workspace);
706 wUnhideApplication(wapp, False, False);
707 } else {
708 wHideApplication(wapp);
712 static void unhideHereCallback(WMenu *menu, WMenuEntry *entry)
714 WApplication *wapp;
715 WAppIcon *btn = (WAppIcon *) entry->clientdata;
717 wapp = wApplicationOf(btn->icon->owner->main_window);
719 wUnhideApplication(wapp, False, True);
722 static WAppIcon *mainIconCreate(WScreen *scr, int type)
724 WAppIcon *btn;
725 int x_pos;
727 if (type == WM_CLIP) {
728 if (scr->clip_icon)
729 return scr->clip_icon;
730 btn = wAppIconCreateForDock(scr, NULL, "Logo", "WMClip", TILE_CLIP);
731 btn->icon->core->descriptor.handle_expose = clipIconExpose;
732 btn->icon->core->descriptor.handle_enternotify = clipEnterNotify;
733 btn->icon->core->descriptor.handle_leavenotify = clipLeaveNotify;
734 x_pos = 0;
735 } else {
736 btn = wAppIconCreateForDock(scr, NULL, "Logo", "WMDock", TILE_NORMAL);
737 x_pos = scr->scr_width - ICON_SIZE - DOCK_EXTRA_SPACE;
740 btn->xindex = 0;
741 btn->yindex = 0;
743 btn->icon->core->descriptor.handle_mousedown = iconMouseDown;
744 btn->icon->core->descriptor.parent_type = WCLASS_DOCK_ICON;
745 btn->icon->core->descriptor.parent = btn;
746 XMapWindow(dpy, btn->icon->core->window);
747 btn->x_pos = x_pos;
748 btn->y_pos = 0;
749 btn->docked = 1;
750 if (type == WM_CLIP)
751 scr->clip_icon = btn;
753 return btn;
756 static void switchWSCommand(WMenu *menu, WMenuEntry *entry)
758 WAppIcon *btn, *icon = (WAppIcon *) entry->clientdata;
759 WScreen *scr = icon->icon->core->screen_ptr;
760 WDock *src, *dest;
761 WMArray *selectedIcons;
762 int x, y;
764 if (entry->order == scr->current_workspace)
765 return;
766 src = icon->dock;
767 dest = scr->workspaces[entry->order]->clip;
769 selectedIcons = getSelected(src);
771 if (WMGetArrayItemCount(selectedIcons)) {
772 WMArrayIterator iter;
774 WM_ITERATE_ARRAY(selectedIcons, btn, iter) {
775 if (wDockFindFreeSlot(dest, &x, &y)) {
776 moveIconBetweenDocks(src, dest, btn, x, y);
777 XUnmapWindow(dpy, btn->icon->core->window);
780 } else if (icon != scr->clip_icon) {
781 if (wDockFindFreeSlot(dest, &x, &y)) {
782 moveIconBetweenDocks(src, dest, icon, x, y);
783 XUnmapWindow(dpy, icon->icon->core->window);
786 WMFreeArray(selectedIcons);
789 static void launchDockedApplication(WAppIcon *btn, Bool withSelection)
791 WScreen *scr = btn->icon->core->screen_ptr;
793 if (!btn->launching &&
794 ((!withSelection && btn->command != NULL) || (withSelection && btn->paste_command != NULL))) {
795 if (!btn->forced_dock) {
796 btn->relaunching = btn->running;
797 btn->running = 1;
799 if (btn->wm_instance || btn->wm_class) {
800 WWindowAttributes attr;
801 memset(&attr, 0, sizeof(WWindowAttributes));
802 wDefaultFillAttributes(btn->wm_instance, btn->wm_class, &attr, NULL, True);
804 if (!attr.no_appicon && !btn->buggy_app)
805 btn->launching = 1;
806 else
807 btn->running = 0;
809 btn->drop_launch = 0;
810 btn->paste_launch = withSelection;
811 scr->last_dock = btn->dock;
812 btn->pid = execCommand(btn, (withSelection ? btn->paste_command : btn->command), NULL);
813 if (btn->pid > 0) {
814 if (btn->buggy_app) {
815 /* give feedback that the app was launched */
816 btn->launching = 1;
817 dockIconPaint(btn);
818 btn->launching = 0;
819 WMAddTimerHandler(200, (WMCallback *) dockIconPaint, btn);
820 } else {
821 dockIconPaint(btn);
823 } else {
824 wwarning(_("could not launch application %s"), btn->command);
825 btn->launching = 0;
826 if (!btn->relaunching)
827 btn->running = 0;
832 static void updateWorkspaceMenu(WMenu *menu, WAppIcon *icon)
834 WScreen *scr = menu->frame->screen_ptr;
835 char title[MAX_WORKSPACENAME_WIDTH + 1];
836 int i;
838 if (!menu || !icon)
839 return;
841 for (i = 0; i < scr->workspace_count; i++) {
842 if (i < menu->entry_no) {
843 if (strcmp(menu->entries[i]->text, scr->workspaces[i]->name) != 0) {
844 wfree(menu->entries[i]->text);
845 strcpy(title, scr->workspaces[i]->name);
846 menu->entries[i]->text = wstrdup(title);
847 menu->flags.realized = 0;
849 menu->entries[i]->clientdata = (void *)icon;
850 } else {
851 strcpy(title, scr->workspaces[i]->name);
853 wMenuAddCallback(menu, title, switchWSCommand, (void *)icon);
855 menu->flags.realized = 0;
858 if (i == scr->current_workspace)
859 wMenuSetEnabled(menu, i, False);
860 else
861 wMenuSetEnabled(menu, i, True);
864 if (!menu->flags.realized)
865 wMenuRealize(menu);
868 static WMenu *makeWorkspaceMenu(WScreen *scr)
870 WMenu *menu;
872 menu = wMenuCreate(scr, NULL, False);
873 if (!menu)
874 wwarning(_("could not create workspace submenu for Clip menu"));
876 wMenuAddCallback(menu, "", switchWSCommand, (void *)scr->clip_icon);
878 menu->flags.realized = 0;
879 wMenuRealize(menu);
881 return menu;
884 static void updateClipOptionsMenu(WMenu *menu, WDock *dock)
886 WMenuEntry *entry;
887 int index = 0;
889 if (!menu || !dock)
890 return;
892 /* keep on top */
893 entry = menu->entries[index];
894 entry->flags.indicator_on = !dock->lowered;
895 entry->clientdata = dock;
897 /* collapsed */
898 entry = menu->entries[++index];
899 entry->flags.indicator_on = dock->collapsed;
900 entry->clientdata = dock;
902 /* auto-collapse */
903 entry = menu->entries[++index];
904 entry->flags.indicator_on = dock->auto_collapse;
905 entry->clientdata = dock;
907 /* auto-raise/lower */
908 entry = menu->entries[++index];
909 entry->flags.indicator_on = dock->auto_raise_lower;
910 entry->clientdata = dock;
911 wMenuSetEnabled(menu, index, dock->lowered);
913 /* attract icons */
914 entry = menu->entries[++index];
915 entry->flags.indicator_on = dock->attract_icons;
916 entry->clientdata = dock;
918 menu->flags.realized = 0;
919 wMenuRealize(menu);
922 static WMenu *makeClipOptionsMenu(WScreen *scr)
924 WMenu *menu;
925 WMenuEntry *entry;
927 menu = wMenuCreate(scr, NULL, False);
928 if (!menu) {
929 wwarning(_("could not create options submenu for Clip menu"));
930 return NULL;
933 entry = wMenuAddCallback(menu, _("Keep on Top"), toggleLoweredCallback, NULL);
934 entry->flags.indicator = 1;
935 entry->flags.indicator_on = 1;
936 entry->flags.indicator_type = MI_CHECK;
938 entry = wMenuAddCallback(menu, _("Collapsed"), toggleCollapsedCallback, NULL);
939 entry->flags.indicator = 1;
940 entry->flags.indicator_on = 1;
941 entry->flags.indicator_type = MI_CHECK;
943 entry = wMenuAddCallback(menu, _("Autocollapse"), toggleAutoCollapseCallback, NULL);
944 entry->flags.indicator = 1;
945 entry->flags.indicator_on = 1;
946 entry->flags.indicator_type = MI_CHECK;
948 entry = wMenuAddCallback(menu, _("Autoraise"), toggleAutoRaiseLowerCallback, NULL);
949 entry->flags.indicator = 1;
950 entry->flags.indicator_on = 1;
951 entry->flags.indicator_type = MI_CHECK;
953 entry = wMenuAddCallback(menu, _("Autoattract Icons"), toggleAutoAttractCallback, NULL);
954 entry->flags.indicator = 1;
955 entry->flags.indicator_on = 1;
956 entry->flags.indicator_type = MI_CHECK;
958 menu->flags.realized = 0;
959 wMenuRealize(menu);
961 return menu;
964 static WMenu *dockMenuCreate(WScreen *scr, int type)
966 WMenu *menu;
967 WMenuEntry *entry;
969 if (type == WM_CLIP && scr->clip_menu)
970 return scr->clip_menu;
972 menu = wMenuCreate(scr, NULL, False);
973 if (type != WM_CLIP) {
974 entry = wMenuAddCallback(menu, _("Keep on Top"), toggleLoweredCallback, NULL);
975 entry->flags.indicator = 1;
976 entry->flags.indicator_on = 1;
977 entry->flags.indicator_type = MI_CHECK;
978 } else {
979 entry = wMenuAddCallback(menu, _("Clip Options"), NULL, NULL);
980 scr->clip_options = makeClipOptionsMenu(scr);
981 if (scr->clip_options)
982 wMenuEntrySetCascade(menu, entry, scr->clip_options);
984 entry = wMenuAddCallback(menu, _("Rename Workspace"), renameCallback, NULL);
985 wfree(entry->text);
986 entry->text = _("Rename Workspace");
988 entry = wMenuAddCallback(menu, _("Selected"), selectCallback, NULL);
989 entry->flags.indicator = 1;
990 entry->flags.indicator_on = 1;
991 entry->flags.indicator_type = MI_CHECK;
993 entry = wMenuAddCallback(menu, _("Select All Icons"), selectIconsCallback, NULL);
994 wfree(entry->text);
995 entry->text = _("Select All Icons");
997 entry = wMenuAddCallback(menu, _("Keep Icon"), keepIconsCallback, NULL);
998 wfree(entry->text);
999 entry->text = _("Keep Icon");
1001 entry = wMenuAddCallback(menu, _("Move Icon To"), NULL, NULL);
1002 wfree(entry->text);
1003 entry->text = _("Move Icon To");
1004 scr->clip_submenu = makeWorkspaceMenu(scr);
1005 if (scr->clip_submenu)
1006 wMenuEntrySetCascade(menu, entry, scr->clip_submenu);
1008 entry = wMenuAddCallback(menu, _("Remove Icon"), removeIconsCallback, NULL);
1009 wfree(entry->text);
1010 entry->text = _("Remove Icon");
1012 wMenuAddCallback(menu, _("Attract Icons"), colectIconsCallback, NULL);
1015 wMenuAddCallback(menu, _("Launch"), launchCallback, NULL);
1017 wMenuAddCallback(menu, _("Unhide Here"), unhideHereCallback, NULL);
1019 entry = wMenuAddCallback(menu, _("Hide"), hideCallback, NULL);
1020 wfree(entry->text);
1021 entry->text = _("Hide");
1023 wMenuAddCallback(menu, _("Settings..."), settingsCallback, NULL);
1025 wMenuAddCallback(menu, _("Kill"), killCallback, NULL);
1027 if (type == WM_CLIP)
1028 scr->clip_menu = menu;
1030 return menu;
1033 WDock *wDockCreate(WScreen *scr, int type)
1035 WDock *dock;
1036 WAppIcon *btn;
1038 make_keys();
1040 dock = wmalloc(sizeof(WDock));
1042 dock->max_icons = DOCK_MAX_ICONS;
1044 dock->icon_array = wmalloc(sizeof(WAppIcon *) * dock->max_icons);
1046 btn = mainIconCreate(scr, type);
1048 btn->dock = dock;
1050 dock->x_pos = btn->x_pos;
1051 dock->y_pos = btn->y_pos;
1052 dock->screen_ptr = scr;
1053 dock->type = type;
1054 dock->icon_count = 1;
1055 dock->on_right_side = 1;
1056 dock->collapsed = 0;
1057 dock->auto_collapse = 0;
1058 dock->auto_collapse_magic = NULL;
1059 dock->auto_raise_lower = 0;
1060 dock->auto_lower_magic = NULL;
1061 dock->auto_raise_magic = NULL;
1062 dock->attract_icons = 0;
1063 dock->lowered = 1;
1064 dock->icon_array[0] = btn;
1065 wRaiseFrame(btn->icon->core);
1066 XMoveWindow(dpy, btn->icon->core->window, btn->x_pos, btn->y_pos);
1068 /* create dock menu */
1069 dock->menu = dockMenuCreate(scr, type);
1071 return dock;
1074 void wDockDestroy(WDock *dock)
1076 int i;
1077 WAppIcon *aicon;
1079 for (i = (dock->type == WM_CLIP) ? 1 : 0; i < dock->max_icons; i++) {
1080 aicon = dock->icon_array[i];
1081 if (aicon) {
1082 int keepit = aicon->running && wApplicationOf(aicon->main_window);
1083 wDockDetach(dock, aicon);
1084 if (keepit) {
1085 /* XXX: can: aicon->icon == NULL ? */
1086 PlaceIcon(dock->screen_ptr, &aicon->x_pos, &aicon->y_pos,
1087 wGetHeadForWindow(aicon->icon->owner));
1088 XMoveWindow(dpy, aicon->icon->core->window, aicon->x_pos, aicon->y_pos);
1089 if (!dock->mapped || dock->collapsed)
1090 XMapWindow(dpy, aicon->icon->core->window);
1094 if (wPreferences.auto_arrange_icons)
1095 wArrangeIcons(dock->screen_ptr, True);
1096 wfree(dock->icon_array);
1097 if (dock->menu && dock->type != WM_CLIP)
1098 wMenuDestroy(dock->menu, True);
1099 if (dock->screen_ptr->last_dock == dock)
1100 dock->screen_ptr->last_dock = NULL;
1101 wfree(dock);
1104 void wClipIconPaint(WAppIcon *aicon)
1106 WScreen *scr = aicon->icon->core->screen_ptr;
1107 WWorkspace *workspace = scr->workspaces[scr->current_workspace];
1108 WMColor *color;
1109 Window win = aicon->icon->core->window;
1110 int length, nlength;
1111 char *ws_name, ws_number[10];
1112 int ty, tx;
1114 wIconPaint(aicon->icon);
1116 length = strlen(workspace->name);
1117 ws_name = wmalloc(length + 1);
1118 snprintf(ws_name, length + 1, "%s", workspace->name);
1119 snprintf(ws_number, sizeof(ws_number), "%i", scr->current_workspace + 1);
1120 nlength = strlen(ws_number);
1122 if (!workspace->clip->collapsed)
1123 color = scr->clip_title_color[CLIP_NORMAL];
1124 else
1125 color = scr->clip_title_color[CLIP_COLLAPSED];
1127 ty = ICON_SIZE - WMFontHeight(scr->clip_title_font) - 3;
1129 tx = CLIP_BUTTON_SIZE * ICON_SIZE / 64;
1131 if(wPreferences.show_clip_title)
1132 WMDrawString(scr->wmscreen, win, color, scr->clip_title_font, tx, ty, ws_name, length);
1134 tx = (ICON_SIZE / 2 - WMWidthOfString(scr->clip_title_font, ws_number, nlength)) / 2;
1136 WMDrawString(scr->wmscreen, win, color, scr->clip_title_font, tx, 2, ws_number, nlength);
1138 wfree(ws_name);
1140 if (aicon->launching)
1141 XFillRectangle(dpy, aicon->icon->core->window, scr->stipple_gc,
1142 0, 0, wPreferences.icon_size, wPreferences.icon_size);
1144 paintClipButtons(aicon, aicon->dock->lclip_button_pushed, aicon->dock->rclip_button_pushed);
1147 static void clipIconExpose(WObjDescriptor *desc, XEvent *event)
1149 wClipIconPaint(desc->parent);
1152 static void dockIconPaint(WAppIcon *btn)
1154 if (btn == btn->icon->core->screen_ptr->clip_icon) {
1155 wClipIconPaint(btn);
1156 } else {
1157 wAppIconPaint(btn);
1158 save_appicon(btn, True);
1162 static WMPropList *make_icon_state(WAppIcon *btn)
1164 WMPropList *node = NULL;
1165 WMPropList *command, *autolaunch, *lock, *name, *forced;
1166 WMPropList *position, *buggy, *omnipresent;
1167 char *tmp;
1168 char buffer[64];
1170 if (btn) {
1171 if (!btn->command)
1172 command = WMCreatePLString("-");
1173 else
1174 command = WMCreatePLString(btn->command);
1176 autolaunch = btn->auto_launch ? dYes : dNo;
1178 lock = btn->lock ? dYes : dNo;
1180 tmp = EscapeWM_CLASS(btn->wm_instance, btn->wm_class);
1182 name = WMCreatePLString(tmp);
1184 wfree(tmp);
1186 forced = btn->forced_dock ? dYes : dNo;
1188 buggy = btn->buggy_app ? dYes : dNo;
1190 if (btn == btn->icon->core->screen_ptr->clip_icon)
1191 snprintf(buffer, sizeof(buffer), "%i,%i", btn->x_pos, btn->y_pos);
1192 else
1193 snprintf(buffer, sizeof(buffer), "%hi,%hi", btn->xindex, btn->yindex);
1194 position = WMCreatePLString(buffer);
1196 node = WMCreatePLDictionary(dCommand, command,
1197 dName, name,
1198 dAutoLaunch, autolaunch,
1199 dLock, lock,
1200 dForced, forced, dBuggyApplication, buggy, dPosition, position, NULL);
1201 WMReleasePropList(command);
1202 WMReleasePropList(name);
1203 WMReleasePropList(position);
1205 omnipresent = btn->omnipresent ? dYes : dNo;
1206 if (btn->dock != btn->icon->core->screen_ptr->dock && (btn->xindex != 0 || btn->yindex != 0))
1207 WMPutInPLDictionary(node, dOmnipresent, omnipresent);
1209 #ifdef XDND /* was OFFIX */
1210 if (btn->dnd_command) {
1211 command = WMCreatePLString(btn->dnd_command);
1212 WMPutInPLDictionary(node, dDropCommand, command);
1213 WMReleasePropList(command);
1215 #endif /* XDND */
1217 if (btn->paste_command) {
1218 command = WMCreatePLString(btn->paste_command);
1219 WMPutInPLDictionary(node, dPasteCommand, command);
1220 WMReleasePropList(command);
1224 return node;
1227 static WMPropList *dockSaveState(WDock *dock)
1229 int i;
1230 WMPropList *icon_info;
1231 WMPropList *list = NULL, *dock_state = NULL;
1232 WMPropList *value, *key;
1233 char buffer[256];
1235 list = WMCreatePLArray(NULL);
1237 for (i = (dock->type == WM_DOCK ? 0 : 1); i < dock->max_icons; i++) {
1238 WAppIcon *btn = dock->icon_array[i];
1240 if (!btn || btn->attracted)
1241 continue;
1243 if ((icon_info = make_icon_state(dock->icon_array[i]))) {
1244 WMAddToPLArray(list, icon_info);
1245 WMReleasePropList(icon_info);
1249 dock_state = WMCreatePLDictionary(dApplications, list, NULL);
1251 if (dock->type == WM_DOCK) {
1252 snprintf(buffer, sizeof(buffer), "Applications%i", dock->screen_ptr->scr_height);
1253 key = WMCreatePLString(buffer);
1254 WMPutInPLDictionary(dock_state, key, list);
1255 WMReleasePropList(key);
1257 snprintf(buffer, sizeof(buffer), "%i,%i", (dock->on_right_side ? -ICON_SIZE : 0), dock->y_pos);
1258 value = WMCreatePLString(buffer);
1259 WMPutInPLDictionary(dock_state, dPosition, value);
1260 WMReleasePropList(value);
1262 WMReleasePropList(list);
1264 value = (dock->lowered ? dYes : dNo);
1265 WMPutInPLDictionary(dock_state, dLowered, value);
1267 if (dock->type == WM_CLIP) {
1268 value = (dock->collapsed ? dYes : dNo);
1269 WMPutInPLDictionary(dock_state, dCollapsed, value);
1271 value = (dock->auto_collapse ? dYes : dNo);
1272 WMPutInPLDictionary(dock_state, dAutoCollapse, value);
1274 value = (dock->auto_raise_lower ? dYes : dNo);
1275 WMPutInPLDictionary(dock_state, dAutoRaiseLower, value);
1277 value = (dock->attract_icons ? dYes : dNo);
1278 WMPutInPLDictionary(dock_state, dAutoAttractIcons, value);
1281 return dock_state;
1284 void wDockSaveState(WScreen *scr, WMPropList *old_state)
1286 WMPropList *dock_state;
1287 WMPropList *keys;
1289 dock_state = dockSaveState(scr->dock);
1292 * Copy saved states of docks with different sizes.
1294 if (old_state) {
1295 int i;
1296 WMPropList *tmp;
1298 keys = WMGetPLDictionaryKeys(old_state);
1299 for (i = 0; i < WMGetPropListItemCount(keys); i++) {
1300 tmp = WMGetFromPLArray(keys, i);
1302 if (strncasecmp(WMGetFromPLString(tmp), "applications", 12) == 0
1303 && !WMGetFromPLDictionary(dock_state, tmp)) {
1305 WMPutInPLDictionary(dock_state, tmp, WMGetFromPLDictionary(old_state, tmp));
1308 WMReleasePropList(keys);
1311 WMPutInPLDictionary(scr->session_state, dDock, dock_state);
1313 WMReleasePropList(dock_state);
1316 void wClipSaveState(WScreen *scr)
1318 WMPropList *clip_state;
1320 clip_state = make_icon_state(scr->clip_icon);
1322 WMPutInPLDictionary(scr->session_state, dClip, clip_state);
1324 WMReleasePropList(clip_state);
1327 WMPropList *wClipSaveWorkspaceState(WScreen *scr, int workspace)
1329 return dockSaveState(scr->workspaces[workspace]->clip);
1332 static Bool getBooleanDockValue(WMPropList *value, WMPropList *key)
1334 if (value) {
1335 if (WMIsPLString(value)) {
1336 if (strcasecmp(WMGetFromPLString(value), "YES") == 0)
1337 return True;
1338 } else {
1339 wwarning(_("bad value in docked icon state info %s"), WMGetFromPLString(key));
1342 return False;
1345 static WAppIcon *restore_icon_state(WScreen *scr, WMPropList *info, int type, int index)
1347 WAppIcon *aicon;
1348 WMPropList *cmd, *value;
1349 char *wclass, *winstance, *command;
1351 cmd = WMGetFromPLDictionary(info, dCommand);
1352 if (!cmd || !WMIsPLString(cmd))
1353 return NULL;
1355 /* parse window name */
1356 value = WMGetFromPLDictionary(info, dName);
1357 if (!value)
1358 return NULL;
1360 ParseWindowName(value, &winstance, &wclass, "dock");
1362 if (!winstance && !wclass)
1363 return NULL;
1365 /* get commands */
1366 if (cmd)
1367 command = wstrdup(WMGetFromPLString(cmd));
1368 else
1369 command = NULL;
1371 if (!command || strcmp(command, "-") == 0) {
1372 if (command)
1373 wfree(command);
1374 if (wclass)
1375 wfree(wclass);
1376 if (winstance)
1377 wfree(winstance);
1379 return NULL;
1382 aicon = wAppIconCreateForDock(scr, command, winstance, wclass, TILE_NORMAL);
1383 if (wclass)
1384 wfree(wclass);
1385 if (winstance)
1386 wfree(winstance);
1387 if (command)
1388 wfree(command);
1390 aicon->icon->core->descriptor.handle_mousedown = iconMouseDown;
1391 if (type == WM_CLIP) {
1392 aicon->icon->core->descriptor.handle_enternotify = clipEnterNotify;
1393 aicon->icon->core->descriptor.handle_leavenotify = clipLeaveNotify;
1395 aicon->icon->core->descriptor.parent_type = WCLASS_DOCK_ICON;
1396 aicon->icon->core->descriptor.parent = aicon;
1398 #ifdef XDND /* was OFFIX */
1399 cmd = WMGetFromPLDictionary(info, dDropCommand);
1400 if (cmd)
1401 aicon->dnd_command = wstrdup(WMGetFromPLString(cmd));
1402 #endif
1404 cmd = WMGetFromPLDictionary(info, dPasteCommand);
1405 if (cmd)
1406 aicon->paste_command = wstrdup(WMGetFromPLString(cmd));
1408 /* check auto launch */
1409 value = WMGetFromPLDictionary(info, dAutoLaunch);
1411 aicon->auto_launch = getBooleanDockValue(value, dAutoLaunch);
1413 /* check lock */
1414 value = WMGetFromPLDictionary(info, dLock);
1416 aicon->lock = getBooleanDockValue(value, dLock);
1418 /* check if it wasn't normally docked */
1419 value = WMGetFromPLDictionary(info, dForced);
1421 aicon->forced_dock = getBooleanDockValue(value, dForced);
1423 /* check if we can rely on the stuff in the app */
1424 value = WMGetFromPLDictionary(info, dBuggyApplication);
1426 aicon->buggy_app = getBooleanDockValue(value, dBuggyApplication);
1428 /* get position in the dock */
1429 value = WMGetFromPLDictionary(info, dPosition);
1430 if (value && WMIsPLString(value)) {
1431 if (sscanf(WMGetFromPLString(value), "%hi,%hi", &aicon->xindex, &aicon->yindex) != 2)
1432 wwarning(_("bad value in docked icon state info %s"), WMGetFromPLString(dPosition));
1434 /* check position sanity */
1435 /* incomplete section! */
1436 if (type == WM_DOCK) {
1437 aicon->xindex = 0;
1438 if (aicon->yindex < 0)
1439 wwarning(_("bad value in docked icon position %i,%i"),
1440 aicon->xindex, aicon->yindex);
1442 } else {
1443 aicon->yindex = index;
1444 aicon->xindex = 0;
1447 /* check if icon is omnipresent */
1448 value = WMGetFromPLDictionary(info, dOmnipresent);
1450 aicon->omnipresent = getBooleanDockValue(value, dOmnipresent);
1452 aicon->running = 0;
1453 aicon->docked = 1;
1455 return aicon;
1458 #define COMPLAIN(key) wwarning(_("bad value in dock state info:%s"), key)
1460 WAppIcon *wClipRestoreState(WScreen *scr, WMPropList *clip_state)
1462 WAppIcon *icon;
1463 WMPropList *value;
1465 icon = mainIconCreate(scr, WM_CLIP);
1467 if (!clip_state)
1468 return icon;
1470 WMRetainPropList(clip_state);
1472 /* restore position */
1474 value = WMGetFromPLDictionary(clip_state, dPosition);
1476 if (value) {
1477 if (!WMIsPLString(value)) {
1478 COMPLAIN("Position");
1479 } else {
1480 WMRect rect;
1481 int flags;
1483 if (sscanf(WMGetFromPLString(value), "%i,%i", &icon->x_pos, &icon->y_pos) != 2)
1484 COMPLAIN("Position");
1486 /* check position sanity */
1487 rect.pos.x = icon->x_pos;
1488 rect.pos.y = icon->y_pos;
1489 rect.size.width = rect.size.height = ICON_SIZE;
1491 wGetRectPlacementInfo(scr, rect, &flags);
1492 if (flags & (XFLAG_DEAD | XFLAG_PARTIAL))
1493 wScreenKeepInside(scr, &icon->x_pos, &icon->y_pos, ICON_SIZE, ICON_SIZE);
1496 #ifdef XDND /* was OFFIX */
1497 value = WMGetFromPLDictionary(clip_state, dDropCommand);
1498 if (value && WMIsPLString(value))
1499 icon->dnd_command = wstrdup(WMGetFromPLString(value));
1500 #endif
1502 value = WMGetFromPLDictionary(clip_state, dPasteCommand);
1503 if (value && WMIsPLString(value))
1504 icon->paste_command = wstrdup(WMGetFromPLString(value));
1506 WMReleasePropList(clip_state);
1508 return icon;
1511 WDock *wDockRestoreState(WScreen *scr, WMPropList *dock_state, int type)
1513 WDock *dock;
1514 WMPropList *apps;
1515 WMPropList *value;
1516 WAppIcon *aicon, *old_top;
1517 int count, i;
1519 dock = wDockCreate(scr, type);
1521 if (!dock_state)
1522 return dock;
1524 WMRetainPropList(dock_state);
1526 /* restore position */
1527 value = WMGetFromPLDictionary(dock_state, dPosition);
1528 if (value) {
1529 if (!WMIsPLString(value)) {
1530 COMPLAIN("Position");
1531 } else {
1532 WMRect rect;
1533 int flags;
1535 if (sscanf(WMGetFromPLString(value), "%i,%i", &dock->x_pos, &dock->y_pos) != 2)
1536 COMPLAIN("Position");
1538 /* check position sanity */
1539 rect.pos.x = dock->x_pos;
1540 rect.pos.y = dock->y_pos;
1541 rect.size.width = rect.size.height = ICON_SIZE;
1543 wGetRectPlacementInfo(scr, rect, &flags);
1544 if (flags & (XFLAG_DEAD | XFLAG_PARTIAL)) {
1545 int x = dock->x_pos;
1546 wScreenKeepInside(scr, &x, &dock->y_pos, ICON_SIZE, ICON_SIZE);
1549 /* Is this needed any more? */
1550 if (type == WM_CLIP) {
1551 if (dock->x_pos < 0) {
1552 dock->x_pos = 0;
1553 } else if (dock->x_pos > scr->scr_width - ICON_SIZE) {
1554 dock->x_pos = scr->scr_width - ICON_SIZE;
1556 } else {
1557 if (dock->x_pos >= 0) {
1558 dock->x_pos = DOCK_EXTRA_SPACE;
1559 dock->on_right_side = 0;
1560 } else {
1561 dock->x_pos = scr->scr_width - DOCK_EXTRA_SPACE - ICON_SIZE;
1562 dock->on_right_side = 1;
1568 /* restore lowered/raised state */
1569 dock->lowered = 0;
1571 value = WMGetFromPLDictionary(dock_state, dLowered);
1572 if (value) {
1573 if (!WMIsPLString(value)) {
1574 COMPLAIN("Lowered");
1575 } else {
1576 if (strcasecmp(WMGetFromPLString(value), "YES") == 0)
1577 dock->lowered = 1;
1581 /* restore collapsed state */
1582 dock->collapsed = 0;
1584 value = WMGetFromPLDictionary(dock_state, dCollapsed);
1585 if (value) {
1586 if (!WMIsPLString(value)) {
1587 COMPLAIN("Collapsed");
1588 } else {
1589 if (strcasecmp(WMGetFromPLString(value), "YES") == 0)
1590 dock->collapsed = 1;
1594 /* restore auto-collapsed state */
1595 value = WMGetFromPLDictionary(dock_state, dAutoCollapse);
1596 if (value) {
1597 if (!WMIsPLString(value)) {
1598 COMPLAIN("AutoCollapse");
1599 } else {
1600 if (strcasecmp(WMGetFromPLString(value), "YES") == 0) {
1601 dock->auto_collapse = 1;
1602 dock->collapsed = 1;
1607 /* restore auto-raise/lower state */
1608 value = WMGetFromPLDictionary(dock_state, dAutoRaiseLower);
1609 if (value) {
1610 if (!WMIsPLString(value)) {
1611 COMPLAIN("AutoRaiseLower");
1612 } else {
1613 if (strcasecmp(WMGetFromPLString(value), "YES") == 0)
1614 dock->auto_raise_lower = 1;
1618 /* restore attract icons state */
1619 dock->attract_icons = 0;
1621 value = WMGetFromPLDictionary(dock_state, dAutoAttractIcons);
1622 if (value) {
1623 if (!WMIsPLString(value)) {
1624 COMPLAIN("AutoAttractIcons");
1625 } else {
1626 if (strcasecmp(WMGetFromPLString(value), "YES") == 0)
1627 dock->attract_icons = 1;
1631 /* application list */
1634 WMPropList *tmp;
1635 char buffer[64];
1638 * When saving, it saves the dock state in
1639 * Applications and Applicationsnnn
1641 * When loading, it will first try Applicationsnnn.
1642 * If it does not exist, use Applications as default.
1645 snprintf(buffer, sizeof(buffer), "Applications%i", scr->scr_height);
1647 tmp = WMCreatePLString(buffer);
1648 apps = WMGetFromPLDictionary(dock_state, tmp);
1649 WMReleasePropList(tmp);
1651 if (!apps)
1652 apps = WMGetFromPLDictionary(dock_state, dApplications);
1655 if (!apps)
1656 goto finish;
1658 count = WMGetPropListItemCount(apps);
1659 if (count == 0)
1660 goto finish;
1662 old_top = dock->icon_array[0];
1664 /* dock->icon_count is set to 1 when dock is created.
1665 * Since Clip is already restored, we want to keep it so for clip,
1666 * but for dock we may change the default top tile, so we set it to 0.
1668 if (type == WM_DOCK)
1669 dock->icon_count = 0;
1671 for (i = 0; i < count; i++) {
1672 if (dock->icon_count >= dock->max_icons) {
1673 wwarning(_("there are too many icons stored in dock. Ignoring what doesn't fit"));
1674 break;
1677 value = WMGetFromPLArray(apps, i);
1678 aicon = restore_icon_state(scr, value, type, dock->icon_count);
1680 dock->icon_array[dock->icon_count] = aicon;
1682 if (aicon) {
1683 aicon->dock = dock;
1684 aicon->x_pos = dock->x_pos + (aicon->xindex * ICON_SIZE);
1685 aicon->y_pos = dock->y_pos + (aicon->yindex * ICON_SIZE);
1687 if (dock->lowered)
1688 ChangeStackingLevel(aicon->icon->core, WMNormalLevel);
1689 else
1690 ChangeStackingLevel(aicon->icon->core, WMDockLevel);
1692 wCoreConfigure(aicon->icon->core, aicon->x_pos, aicon->y_pos, 0, 0);
1693 if (!dock->collapsed)
1694 XMapWindow(dpy, aicon->icon->core->window);
1696 wRaiseFrame(aicon->icon->core);
1698 dock->icon_count++;
1699 } else if (dock->icon_count == 0 && type == WM_DOCK) {
1700 dock->icon_count++;
1704 /* if the first icon is not defined, use the default */
1705 if (dock->icon_array[0] == NULL) {
1706 /* update default icon */
1707 old_top->x_pos = dock->x_pos;
1708 old_top->y_pos = dock->y_pos;
1709 if (dock->lowered)
1710 ChangeStackingLevel(old_top->icon->core, WMNormalLevel);
1711 else
1712 ChangeStackingLevel(old_top->icon->core, WMDockLevel);
1714 dock->icon_array[0] = old_top;
1715 XMoveWindow(dpy, old_top->icon->core->window, dock->x_pos, dock->y_pos);
1716 /* we don't need to increment dock->icon_count here because it was
1717 * incremented in the loop above.
1719 } else if (old_top != dock->icon_array[0]) {
1720 if (old_top == scr->clip_icon)
1721 scr->clip_icon = dock->icon_array[0];
1723 wAppIconDestroy(old_top);
1726 finish:
1727 WMReleasePropList(dock_state);
1729 return dock;
1732 void wDockLaunchWithState(WDock *dock, WAppIcon *btn, WSavedState *state)
1734 if (btn && btn->command && !btn->running && !btn->launching) {
1735 btn->drop_launch = 0;
1736 btn->paste_launch = 0;
1738 btn->pid = execCommand(btn, btn->command, state);
1740 if (btn->pid > 0) {
1741 if (!btn->forced_dock && !btn->buggy_app) {
1742 btn->launching = 1;
1743 dockIconPaint(btn);
1746 } else {
1747 wfree(state);
1751 void wDockDoAutoLaunch(WDock *dock, int workspace)
1753 WAppIcon *btn;
1754 WSavedState *state;
1755 int i;
1757 for (i = 0; i < dock->max_icons; i++) {
1758 btn = dock->icon_array[i];
1759 if (!btn || !btn->auto_launch)
1760 continue;
1762 state = wmalloc(sizeof(WSavedState));
1763 state->workspace = workspace;
1764 /* TODO: this is klugy and is very difficult to understand
1765 * what's going on. Try to clean up */
1766 wDockLaunchWithState(dock, btn, state);
1770 #ifdef XDND /* was OFFIX */
1771 static WDock *findDock(WScreen *scr, XEvent *event, int *icon_pos)
1773 WDock *dock;
1774 int i;
1776 *icon_pos = -1;
1777 if ((dock = scr->dock) != NULL) {
1778 for (i = 0; i < dock->max_icons; i++) {
1779 if (dock->icon_array[i]
1780 && dock->icon_array[i]->icon->core->window == event->xclient.window) {
1781 *icon_pos = i;
1782 break;
1786 if (*icon_pos < 0 && (dock = scr->workspaces[scr->current_workspace]->clip) != NULL) {
1787 for (i = 0; i < dock->max_icons; i++) {
1788 if (dock->icon_array[i]
1789 && dock->icon_array[i]->icon->core->window == event->xclient.window) {
1790 *icon_pos = i;
1791 break;
1795 if (*icon_pos >= 0)
1796 return dock;
1797 return NULL;
1800 int wDockReceiveDNDDrop(WScreen *scr, XEvent *event)
1802 WDock *dock;
1803 WAppIcon *btn;
1804 int icon_pos;
1806 dock = findDock(scr, event, &icon_pos);
1807 if (!dock)
1808 return False;
1811 * Return True if the drop was on an application icon window.
1812 * In this case, let the ClientMessage handler redirect the
1813 * message to the app.
1815 if (dock->icon_array[icon_pos]->icon->icon_win != None)
1816 return True;
1818 if (dock->icon_array[icon_pos]->dnd_command != NULL) {
1819 scr->flags.dnd_data_convertion_status = 0;
1821 btn = dock->icon_array[icon_pos];
1823 if (!btn->forced_dock) {
1824 btn->relaunching = btn->running;
1825 btn->running = 1;
1827 if (btn->wm_instance || btn->wm_class) {
1828 WWindowAttributes attr;
1829 memset(&attr, 0, sizeof(WWindowAttributes));
1830 wDefaultFillAttributes(btn->wm_instance, btn->wm_class, &attr, NULL, True);
1832 if (!attr.no_appicon)
1833 btn->launching = 1;
1834 else
1835 btn->running = 0;
1838 btn->paste_launch = 0;
1839 btn->drop_launch = 1;
1840 scr->last_dock = dock;
1841 btn->pid = execCommand(btn, btn->dnd_command, NULL);
1842 if (btn->pid > 0) {
1843 dockIconPaint(btn);
1844 } else {
1845 btn->launching = 0;
1846 if (!btn->relaunching)
1847 btn->running = 0;
1850 return False;
1852 #endif /* XDND */
1854 Bool wDockAttachIcon(WDock *dock, WAppIcon *icon, int x, int y, Bool update_icon)
1856 WWindow *wwin;
1857 Bool lupdate_icon = False;
1858 int index;
1860 wwin = icon->icon->owner;
1861 icon->editing = 0;
1863 if (update_icon)
1864 lupdate_icon = True;
1866 if (icon->command == NULL) {
1867 char *command;
1869 command = GetCommandForWindow(wwin->client_win);
1870 if (command) {
1871 icon->command = command;
1872 } else {
1873 /* icon->forced_dock = 1; */
1874 if (dock->type != WM_CLIP || !icon->attracted) {
1875 icon->editing = 1;
1876 if (wInputDialog(dock->screen_ptr, _("Dock Icon"),
1877 _("Type the command used to launch the application"), &command)) {
1878 if (command && (command[0] == 0 || (command[0] == '-' && command[1] == 0))) {
1879 wfree(command);
1880 command = NULL;
1882 icon->command = command;
1883 icon->editing = 0;
1884 } else {
1885 icon->editing = 0;
1886 if (command)
1887 wfree(command);
1888 /* If the target is the dock, reject the icon. If
1889 * the target is the clip, make it an attracted icon
1891 if (dock->type == WM_CLIP) {
1892 icon->attracted = 1;
1893 if (!icon->icon->shadowed) {
1894 icon->icon->shadowed = 1;
1895 lupdate_icon = True;
1897 } else {
1898 return False;
1905 for (index = 1; index < dock->max_icons; index++)
1906 if (dock->icon_array[index] == NULL)
1907 break;
1908 /* if (index == dock->max_icons)
1909 return; */
1911 assert(index < dock->max_icons);
1913 dock->icon_array[index] = icon;
1914 icon->yindex = y;
1915 icon->xindex = x;
1917 icon->omnipresent = 0;
1919 icon->x_pos = dock->x_pos + x * ICON_SIZE;
1920 icon->y_pos = dock->y_pos + y * ICON_SIZE;
1922 dock->icon_count++;
1924 icon->running = 1;
1925 icon->launching = 0;
1926 icon->docked = 1;
1927 icon->dock = dock;
1928 icon->icon->core->descriptor.handle_mousedown = iconMouseDown;
1929 if (dock->type == WM_CLIP) {
1930 icon->icon->core->descriptor.handle_enternotify = clipEnterNotify;
1931 icon->icon->core->descriptor.handle_leavenotify = clipLeaveNotify;
1933 icon->icon->core->descriptor.parent_type = WCLASS_DOCK_ICON;
1934 icon->icon->core->descriptor.parent = icon;
1936 MoveInStackListUnder(dock->icon_array[index - 1]->icon->core, icon->icon->core);
1937 wAppIconMove(icon, icon->x_pos, icon->y_pos);
1939 /* Update the icon images */
1940 if (lupdate_icon)
1941 wIconUpdate(icon->icon, NULL);
1943 /* Paint it */
1944 wAppIconPaint(icon);
1946 /* Save it */
1947 save_appicon(icon, True);
1949 if (wPreferences.auto_arrange_icons)
1950 wArrangeIcons(dock->screen_ptr, True);
1952 #ifdef XDND /* was OFFIX */
1953 if (icon->command && !icon->dnd_command) {
1954 int len = strlen(icon->command) + 8;
1955 icon->dnd_command = wmalloc(len);
1956 snprintf(icon->dnd_command, len, "%s %%d", icon->command);
1958 #endif
1960 if (icon->command && !icon->paste_command) {
1961 int len = strlen(icon->command) + 8;
1962 icon->paste_command = wmalloc(len);
1963 snprintf(icon->paste_command, len, "%s %%s", icon->command);
1966 return True;
1969 static void reattachIcon(WDock *dock, WAppIcon *icon, int x, int y)
1971 int index;
1973 for (index = 1; index < dock->max_icons; index++) {
1974 if (dock->icon_array[index] == icon)
1975 break;
1977 assert(index < dock->max_icons);
1979 icon->yindex = y;
1980 icon->xindex = x;
1982 icon->x_pos = dock->x_pos + x * ICON_SIZE;
1983 icon->y_pos = dock->y_pos + y * ICON_SIZE;
1986 static Bool moveIconBetweenDocks(WDock *src, WDock *dest, WAppIcon *icon, int x, int y)
1988 WWindow *wwin;
1989 char *command;
1990 int index;
1991 Bool update_icon = False;
1993 if (src == dest)
1994 return True; /* No move needed, we're already there */
1996 if (dest == NULL)
1997 return False;
1999 wwin = icon->icon->owner;
2002 * For the moment we can't do this if we move icons in Clip from one
2003 * workspace to other, because if we move two or more icons without
2004 * command, the dialog box will not be able to tell us to which of the
2005 * moved icons it applies. -Dan
2007 if ((dest->type == WM_DOCK /*|| dest->keep_attracted */ ) && icon->command == NULL) {
2008 command = GetCommandForWindow(wwin->client_win);
2009 if (command) {
2010 icon->command = command;
2011 } else {
2012 icon->editing = 1;
2013 /* icon->forced_dock = 1; */
2014 if (wInputDialog(src->screen_ptr, _("Dock Icon"),
2015 _("Type the command used to launch the application"), &command)) {
2016 if (command && (command[0] == 0 || (command[0] == '-' && command[1] == 0))) {
2017 wfree(command);
2018 command = NULL;
2020 icon->command = command;
2021 } else {
2022 icon->editing = 0;
2023 if (command)
2024 wfree(command);
2025 return False;
2027 icon->editing = 0;
2031 if (dest->type == WM_DOCK)
2032 wClipMakeIconOmnipresent(icon, False);
2034 for (index = 1; index < src->max_icons; index++) {
2035 if (src->icon_array[index] == icon)
2036 break;
2038 assert(index < src->max_icons);
2040 src->icon_array[index] = NULL;
2041 src->icon_count--;
2043 for (index = 1; index < dest->max_icons; index++) {
2044 if (dest->icon_array[index] == NULL)
2045 break;
2048 assert(index < dest->max_icons);
2050 dest->icon_array[index] = icon;
2051 icon->dock = dest;
2053 /* deselect the icon */
2054 if (icon->icon->selected)
2055 wIconSelect(icon->icon);
2057 if (dest->type == WM_DOCK) {
2058 icon->icon->core->descriptor.handle_enternotify = NULL;
2059 icon->icon->core->descriptor.handle_leavenotify = NULL;
2060 } else {
2061 icon->icon->core->descriptor.handle_enternotify = clipEnterNotify;
2062 icon->icon->core->descriptor.handle_leavenotify = clipLeaveNotify;
2065 /* set it to be kept when moving to dock.
2066 * Unless the icon does not have a command set
2068 if (icon->command && dest->type == WM_DOCK) {
2069 icon->attracted = 0;
2070 if (icon->icon->shadowed) {
2071 icon->icon->shadowed = 0;
2072 update_icon = True;
2074 save_appicon(icon, True);
2077 if (src->auto_collapse || src->auto_raise_lower)
2078 clipLeave(src);
2080 icon->yindex = y;
2081 icon->xindex = x;
2083 icon->x_pos = dest->x_pos + x * ICON_SIZE;
2084 icon->y_pos = dest->y_pos + y * ICON_SIZE;
2086 dest->icon_count++;
2088 MoveInStackListUnder(dest->icon_array[index - 1]->icon->core, icon->icon->core);
2090 /* Update the icon images */
2091 if (update_icon)
2092 wIconUpdate(icon->icon, NULL);
2094 /* Paint it */
2095 wAppIconPaint(icon);
2097 return True;
2100 void wDockDetach(WDock *dock, WAppIcon *icon)
2102 int index;
2103 Bool update_icon = False;
2105 /* make the settings panel be closed */
2106 if (icon->panel)
2107 DestroyDockAppSettingsPanel(icon->panel);
2109 /* This must be called before icon->dock is set to NULL.
2110 * Don't move it. -Dan
2112 wClipMakeIconOmnipresent(icon, False);
2114 icon->docked = 0;
2115 icon->dock = NULL;
2116 icon->attracted = 0;
2117 icon->auto_launch = 0;
2118 if (icon->icon->shadowed) {
2119 icon->icon->shadowed = 0;
2120 update_icon = True;
2123 /* deselect the icon */
2124 if (icon->icon->selected)
2125 wIconSelect(icon->icon);
2127 if (icon->command) {
2128 wfree(icon->command);
2129 icon->command = NULL;
2131 #ifdef XDND /* was OFFIX */
2132 if (icon->dnd_command) {
2133 wfree(icon->dnd_command);
2134 icon->dnd_command = NULL;
2136 #endif
2137 if (icon->paste_command) {
2138 wfree(icon->paste_command);
2139 icon->paste_command = NULL;
2142 for (index = 1; index < dock->max_icons; index++)
2143 if (dock->icon_array[index] == icon)
2144 break;
2146 assert(index < dock->max_icons);
2147 dock->icon_array[index] = NULL;
2148 icon->yindex = -1;
2149 icon->xindex = -1;
2151 dock->icon_count--;
2153 /* if the dock is not attached to an application or
2154 * the application did not set the appropriate hints yet,
2155 * destroy the icon */
2156 if (!icon->running || !wApplicationOf(icon->main_window)) {
2157 wAppIconDestroy(icon);
2158 } else {
2159 icon->icon->core->descriptor.handle_mousedown = appIconMouseDown;
2160 icon->icon->core->descriptor.handle_enternotify = NULL;
2161 icon->icon->core->descriptor.handle_leavenotify = NULL;
2162 icon->icon->core->descriptor.parent_type = WCLASS_APPICON;
2163 icon->icon->core->descriptor.parent = icon;
2165 ChangeStackingLevel(icon->icon->core, NORMAL_ICON_LEVEL);
2167 /* Update the icon images */
2168 if (update_icon)
2169 wIconUpdate(icon->icon, NULL);
2171 /* Paint it */
2172 wAppIconPaint(icon);
2174 if (wPreferences.auto_arrange_icons)
2175 wArrangeIcons(dock->screen_ptr, True);
2177 if (dock->auto_collapse || dock->auto_raise_lower)
2178 clipLeave(dock);
2182 * returns the closest Dock slot index for the passed
2183 * coordinates.
2185 * Returns False if icon can't be docked.
2187 * Note: this function should NEVER alter ret_x or ret_y, unless it will
2188 * return True. -Dan
2190 Bool wDockSnapIcon(WDock *dock, WAppIcon *icon, int req_x, int req_y, int *ret_x, int *ret_y, int redocking)
2192 WScreen *scr = dock->screen_ptr;
2193 int dx, dy;
2194 int ex_x, ex_y;
2195 int i, offset = ICON_SIZE / 2;
2196 WAppIcon *aicon = NULL;
2197 WAppIcon *nicon = NULL;
2198 int max_y_icons;
2200 /* TODO: XINERAMA, for these */
2201 max_y_icons = scr->scr_height / ICON_SIZE - 1;
2203 if (wPreferences.flags.noupdates)
2204 return False;
2206 dx = dock->x_pos;
2207 dy = dock->y_pos;
2209 /* if the dock is full */
2210 if (!redocking && (dock->icon_count >= dock->max_icons))
2211 return False;
2213 /* exact position */
2214 if (req_y < dy)
2215 ex_y = (req_y - offset - dy) / ICON_SIZE;
2216 else
2217 ex_y = (req_y + offset - dy) / ICON_SIZE;
2219 if (req_x < dx)
2220 ex_x = (req_x - offset - dx) / ICON_SIZE;
2221 else
2222 ex_x = (req_x + offset - dx) / ICON_SIZE;
2224 /* check if the icon is outside the screen boundaries */
2226 WMRect rect;
2227 int flags;
2229 rect.pos.x = dx + ex_x * ICON_SIZE;
2230 rect.pos.y = dy + ex_y * ICON_SIZE;
2231 rect.size.width = rect.size.height = ICON_SIZE;
2233 wGetRectPlacementInfo(scr, rect, &flags);
2234 if (flags & (XFLAG_DEAD | XFLAG_PARTIAL))
2235 return False;
2238 if (dock->type == WM_DOCK) {
2239 if (icon->dock != dock && ex_x != 0)
2240 return False;
2242 aicon = NULL;
2243 for (i = 0; i < dock->max_icons; i++) {
2244 nicon = dock->icon_array[i];
2245 if (nicon && nicon->yindex == ex_y) {
2246 aicon = nicon;
2247 break;
2251 if (redocking) {
2252 int sig, done, closest;
2254 /* Possible cases when redocking:
2256 * icon dragged out of range of any slot -> false
2257 * icon dragged to range of free slot
2258 * icon dragged to range of same slot
2259 * icon dragged to range of different icon
2261 if (abs(ex_x) > DOCK_DETTACH_THRESHOLD)
2262 return False;
2264 if (ex_y >= 0 && ex_y <= max_y_icons && (aicon == icon || !aicon)) {
2265 *ret_x = 0;
2266 *ret_y = ex_y;
2267 return True;
2270 /* start looking at the upper slot or lower? */
2271 if (ex_y * ICON_SIZE < (req_y + offset - dy))
2272 sig = 1;
2273 else
2274 sig = -1;
2276 closest = -1;
2277 done = 0;
2278 /* look for closest free slot */
2279 for (i = 0; i < (DOCK_DETTACH_THRESHOLD + 1) * 2 && !done; i++) {
2280 int j;
2282 done = 1;
2283 closest = sig * (i / 2) + ex_y;
2284 /* check if this slot is used */
2285 if (closest >= 0) {
2286 for (j = 0; j < dock->max_icons; j++) {
2287 if (dock->icon_array[j]
2288 && dock->icon_array[j]->yindex == closest) {
2289 /* slot is used by someone else */
2290 if (dock->icon_array[j] != icon)
2291 done = 0;
2292 break;
2296 sig = -sig;
2298 if (done && closest >= 0 && closest <= max_y_icons &&
2299 ((ex_y >= closest && ex_y - closest < DOCK_DETTACH_THRESHOLD + 1)
2300 || (ex_y < closest && closest - ex_y <= DOCK_DETTACH_THRESHOLD + 1))) {
2301 *ret_x = 0;
2302 *ret_y = closest;
2303 return True;
2305 } else { /* !redocking */
2307 /* if slot is free and the icon is close enough, return it */
2308 if (!aicon && ex_x == 0 && ex_y >= 0 && ex_y <= max_y_icons) {
2309 *ret_x = 0;
2310 *ret_y = ex_y;
2311 return True;
2314 } else { /* CLIP */
2315 int neighbours = 0;
2316 int start, stop, k;
2318 start = icon->omnipresent ? 0 : scr->current_workspace;
2319 stop = icon->omnipresent ? scr->workspace_count : start + 1;
2321 aicon = NULL;
2322 for (k = start; k < stop; k++) {
2323 WDock *tmp = scr->workspaces[k]->clip;
2324 if (!tmp)
2325 continue;
2326 for (i = 0; i < tmp->max_icons; i++) {
2327 nicon = tmp->icon_array[i];
2328 if (nicon && nicon->xindex == ex_x && nicon->yindex == ex_y) {
2329 aicon = nicon;
2330 break;
2333 if (aicon)
2334 break;
2336 for (k = start; k < stop; k++) {
2337 WDock *tmp = scr->workspaces[k]->clip;
2338 if (!tmp)
2339 continue;
2340 for (i = 0; i < tmp->max_icons; i++) {
2341 nicon = tmp->icon_array[i];
2342 if (nicon && nicon != icon && /* Icon can't be it's own neighbour */
2343 (abs(nicon->xindex - ex_x) <= CLIP_ATTACH_VICINITY &&
2344 abs(nicon->yindex - ex_y) <= CLIP_ATTACH_VICINITY)) {
2345 neighbours = 1;
2346 break;
2349 if (neighbours)
2350 break;
2353 if (neighbours && (aicon == NULL || (redocking && aicon == icon))) {
2354 *ret_x = ex_x;
2355 *ret_y = ex_y;
2356 return True;
2359 return False;
2362 static int onScreen(WScreen *scr, int x, int y, int sx, int ex, int sy, int ey)
2364 WMRect rect = wmkrect(x, y, ICON_SIZE, ICON_SIZE);
2365 int flags;
2367 wGetRectPlacementInfo(scr, rect, &flags);
2369 return !(flags & (XFLAG_DEAD | XFLAG_PARTIAL));
2373 * returns true if it can find a free slot in the dock,
2374 * in which case it changes x_pos and y_pos accordingly.
2375 * Else returns false.
2377 Bool wDockFindFreeSlot(WDock *dock, int *x_pos, int *y_pos)
2379 WScreen *scr = dock->screen_ptr;
2380 WAppIcon *btn;
2381 WAppIconChain *chain;
2382 unsigned char *slot_map;
2383 int mwidth;
2384 int r;
2385 int x, y;
2386 int i, done = False;
2387 int corner;
2388 int sx = 0, sy = 0, ex = scr->scr_width, ey = scr->scr_height;
2389 int extra_count = 0;
2391 if (dock->type == WM_CLIP && dock != scr->workspaces[scr->current_workspace]->clip)
2392 extra_count = scr->global_icon_count;
2394 /* if the dock is full */
2395 if (dock->icon_count + extra_count >= dock->max_icons)
2396 return False;
2398 if (!wPreferences.flags.nodock && scr->dock) {
2399 if (scr->dock->on_right_side)
2400 ex -= ICON_SIZE + DOCK_EXTRA_SPACE;
2401 else
2402 sx += ICON_SIZE + DOCK_EXTRA_SPACE;
2405 if (ex < dock->x_pos)
2406 ex = dock->x_pos;
2407 if (sx > dock->x_pos + ICON_SIZE)
2408 sx = dock->x_pos + ICON_SIZE;
2409 #define C_NONE 0
2410 #define C_NW 1
2411 #define C_NE 2
2412 #define C_SW 3
2413 #define C_SE 4
2415 /* check if clip is in a corner */
2416 if (dock->type == WM_CLIP) {
2417 if (dock->x_pos < 1 && dock->y_pos < 1)
2418 corner = C_NE;
2419 else if (dock->x_pos < 1 && dock->y_pos >= (ey - ICON_SIZE))
2420 corner = C_SE;
2421 else if (dock->x_pos >= (ex - ICON_SIZE) && dock->y_pos >= (ey - ICON_SIZE))
2422 corner = C_SW;
2423 else if (dock->x_pos >= (ex - ICON_SIZE) && dock->y_pos < 1)
2424 corner = C_NW;
2425 else
2426 corner = C_NONE;
2427 } else {
2428 corner = C_NONE;
2431 /* If the clip is in the corner, use only slots that are in the border
2432 * of the screen */
2433 if (corner != C_NONE) {
2434 char *hmap, *vmap;
2435 int hcount, vcount;
2437 hcount = WMIN(dock->max_icons, scr->scr_width / ICON_SIZE);
2438 vcount = WMIN(dock->max_icons, scr->scr_height / ICON_SIZE);
2439 hmap = wmalloc(hcount + 1);
2440 vmap = wmalloc(vcount + 1);
2442 /* mark used positions */
2443 switch (corner) {
2444 case C_NE:
2445 for (i = 0; i < dock->max_icons; i++) {
2446 btn = dock->icon_array[i];
2447 if (!btn)
2448 continue;
2450 if (btn->xindex == 0 && btn->yindex > 0 && btn->yindex < vcount)
2451 vmap[btn->yindex] = 1;
2452 else if (btn->yindex == 0 && btn->xindex > 0 && btn->xindex < hcount)
2453 hmap[btn->xindex] = 1;
2455 for (chain = scr->global_icons; chain != NULL; chain = chain->next) {
2456 btn = chain->aicon;
2457 if (btn->xindex == 0 && btn->yindex > 0 && btn->yindex < vcount)
2458 vmap[btn->yindex] = 1;
2459 else if (btn->yindex == 0 && btn->xindex > 0 && btn->xindex < hcount)
2460 hmap[btn->xindex] = 1;
2462 break;
2463 case C_NW:
2464 for (i = 0; i < dock->max_icons; i++) {
2465 btn = dock->icon_array[i];
2466 if (!btn)
2467 continue;
2469 if (btn->xindex == 0 && btn->yindex > 0 && btn->yindex < vcount)
2470 vmap[btn->yindex] = 1;
2471 else if (btn->yindex == 0 && btn->xindex < 0 && btn->xindex > -hcount)
2472 hmap[-btn->xindex] = 1;
2474 for (chain = scr->global_icons; chain != NULL; chain = chain->next) {
2475 btn = chain->aicon;
2476 if (btn->xindex == 0 && btn->yindex > 0 && btn->yindex < vcount)
2477 vmap[btn->yindex] = 1;
2478 else if (btn->yindex == 0 && btn->xindex < 0 && btn->xindex > -hcount)
2479 hmap[-btn->xindex] = 1;
2481 break;
2482 case C_SE:
2483 for (i = 0; i < dock->max_icons; i++) {
2484 btn = dock->icon_array[i];
2485 if (!btn)
2486 continue;
2488 if (btn->xindex == 0 && btn->yindex < 0 && btn->yindex > -vcount)
2489 vmap[-btn->yindex] = 1;
2490 else if (btn->yindex == 0 && btn->xindex > 0 && btn->xindex < hcount)
2491 hmap[btn->xindex] = 1;
2493 for (chain = scr->global_icons; chain != NULL; chain = chain->next) {
2494 btn = chain->aicon;
2495 if (btn->xindex == 0 && btn->yindex < 0 && btn->yindex > -vcount)
2496 vmap[-btn->yindex] = 1;
2497 else if (btn->yindex == 0 && btn->xindex > 0 && btn->xindex < hcount)
2498 hmap[btn->xindex] = 1;
2500 break;
2501 case C_SW:
2502 default:
2503 for (i = 0; i < dock->max_icons; i++) {
2504 btn = dock->icon_array[i];
2505 if (!btn)
2506 continue;
2508 if (btn->xindex == 0 && btn->yindex < 0 && btn->yindex > -vcount)
2509 vmap[-btn->yindex] = 1;
2510 else if (btn->yindex == 0 && btn->xindex < 0 && btn->xindex > -hcount)
2511 hmap[-btn->xindex] = 1;
2513 for (chain = scr->global_icons; chain != NULL; chain = chain->next) {
2514 btn = chain->aicon;
2515 if (btn->xindex == 0 && btn->yindex < 0 && btn->yindex > -vcount)
2516 vmap[-btn->yindex] = 1;
2517 else if (btn->yindex == 0 && btn->xindex < 0 && btn->xindex > -hcount)
2518 hmap[-btn->xindex] = 1;
2521 x = 0;
2522 y = 0;
2523 done = 0;
2524 /* search a vacant slot */
2525 for (i = 1; i < WMAX(vcount, hcount); i++) {
2526 if (i < vcount && vmap[i] == 0) {
2527 /* found a slot */
2528 x = 0;
2529 y = i;
2530 done = 1;
2531 break;
2532 } else if (i < hcount && hmap[i] == 0) {
2533 /* found a slot */
2534 x = i;
2535 y = 0;
2536 done = 1;
2537 break;
2540 wfree(vmap);
2541 wfree(hmap);
2542 /* If found a slot, translate and return */
2543 if (done) {
2544 if (corner == C_NW || corner == C_NE)
2545 *y_pos = y;
2546 else
2547 *y_pos = -y;
2549 if (corner == C_NE || corner == C_SE)
2550 *x_pos = x;
2551 else
2552 *x_pos = -x;
2554 return True;
2556 /* else, try to find a slot somewhere else */
2559 /* a map of mwidth x mwidth would be enough if we allowed icons to be
2560 * placed outside of screen */
2561 mwidth = (int)ceil(sqrt(dock->max_icons));
2563 /* In the worst case (the clip is in the corner of the screen),
2564 * the amount of icons that fit in the clip is smaller.
2565 * Double the map to get a safe value.
2567 mwidth += mwidth;
2569 r = (mwidth - 1) / 2;
2571 slot_map = wmalloc(mwidth * mwidth);
2573 #define XY2OFS(x,y) (WMAX(abs(x),abs(y)) > r) ? 0 : (((y)+r)*(mwidth)+(x)+r)
2575 /* mark used slots in the map. If the slot falls outside the map
2576 * (for example, when all icons are placed in line), ignore them. */
2577 for (i = 0; i < dock->max_icons; i++) {
2578 btn = dock->icon_array[i];
2579 if (btn)
2580 slot_map[XY2OFS(btn->xindex, btn->yindex)] = 1;
2583 for (chain = scr->global_icons; chain != NULL; chain = chain->next)
2584 slot_map[XY2OFS(chain->aicon->xindex, chain->aicon->yindex)] = 1;
2586 /* Find closest slot from the center that is free by scanning the
2587 * map from the center to outward in circular passes.
2588 * This will not result in a neat layout, but will be optimal
2589 * in the sense that there will not be holes left.
2591 done = 0;
2592 for (i = 1; i <= r && !done; i++) {
2593 int tx, ty;
2595 /* top and bottom parts of the ring */
2596 for (x = -i; x <= i && !done; x++) {
2597 tx = dock->x_pos + x * ICON_SIZE;
2598 y = -i;
2599 ty = dock->y_pos + y * ICON_SIZE;
2600 if (slot_map[XY2OFS(x, y)] == 0 && onScreen(scr, tx, ty, sx, ex, sy, ey)) {
2601 *x_pos = x;
2602 *y_pos = y;
2603 done = 1;
2604 break;
2606 y = i;
2607 ty = dock->y_pos + y * ICON_SIZE;
2608 if (slot_map[XY2OFS(x, y)] == 0 && onScreen(scr, tx, ty, sx, ex, sy, ey)) {
2609 *x_pos = x;
2610 *y_pos = y;
2611 done = 1;
2612 break;
2615 /* left and right parts of the ring */
2616 for (y = -i + 1; y <= i - 1; y++) {
2617 ty = dock->y_pos + y * ICON_SIZE;
2618 x = -i;
2619 tx = dock->x_pos + x * ICON_SIZE;
2620 if (slot_map[XY2OFS(x, y)] == 0 && onScreen(scr, tx, ty, sx, ex, sy, ey)) {
2621 *x_pos = x;
2622 *y_pos = y;
2623 done = 1;
2624 break;
2626 x = i;
2627 tx = dock->x_pos + x * ICON_SIZE;
2628 if (slot_map[XY2OFS(x, y)] == 0 && onScreen(scr, tx, ty, sx, ex, sy, ey)) {
2629 *x_pos = x;
2630 *y_pos = y;
2631 done = 1;
2632 break;
2636 wfree(slot_map);
2637 #undef XY2OFS
2638 return done;
2641 static void moveDock(WDock *dock, int new_x, int new_y)
2643 WAppIcon *btn;
2644 int i;
2646 dock->x_pos = new_x;
2647 dock->y_pos = new_y;
2648 for (i = 0; i < dock->max_icons; i++) {
2649 btn = dock->icon_array[i];
2650 if (btn) {
2651 btn->x_pos = new_x + btn->xindex * ICON_SIZE;
2652 btn->y_pos = new_y + btn->yindex * ICON_SIZE;
2653 XMoveWindow(dpy, btn->icon->core->window, btn->x_pos, btn->y_pos);
2658 static void swapDock(WDock *dock)
2660 WScreen *scr = dock->screen_ptr;
2661 WAppIcon *btn;
2662 int x, i;
2664 if (dock->on_right_side)
2665 x = dock->x_pos = scr->scr_width - ICON_SIZE - DOCK_EXTRA_SPACE;
2666 else
2667 x = dock->x_pos = DOCK_EXTRA_SPACE;
2669 for (i = 0; i < dock->max_icons; i++) {
2670 btn = dock->icon_array[i];
2671 if (btn) {
2672 btn->x_pos = x;
2673 XMoveWindow(dpy, btn->icon->core->window, btn->x_pos, btn->y_pos);
2677 wScreenUpdateUsableArea(scr);
2680 static pid_t execCommand(WAppIcon *btn, char *command, WSavedState *state)
2682 WScreen *scr = btn->icon->core->screen_ptr;
2683 pid_t pid;
2684 char **argv;
2685 int argc;
2686 char *cmdline;
2688 cmdline = ExpandOptions(scr, command);
2690 if (scr->flags.dnd_data_convertion_status || !cmdline) {
2691 if (cmdline)
2692 wfree(cmdline);
2693 if (state)
2694 wfree(state);
2695 return 0;
2698 wtokensplit(cmdline, &argv, &argc);
2700 if (!argc) {
2701 if (cmdline)
2702 wfree(cmdline);
2703 if (state)
2704 wfree(state);
2705 return 0;
2708 if ((pid = fork()) == 0) {
2709 char **args;
2710 int i;
2712 SetupEnvironment(scr);
2714 #ifdef HAVE_SETSID
2715 setsid();
2716 #endif
2718 args = malloc(sizeof(char *) * (argc + 1));
2719 if (!args)
2720 exit(111);
2722 for (i = 0; i < argc; i++)
2723 args[i] = argv[i];
2725 args[argc] = NULL;
2726 execvp(argv[0], args);
2727 exit(111);
2729 wtokenfree(argv, argc);
2731 if (pid > 0) {
2732 if (!state) {
2733 state = wmalloc(sizeof(WSavedState));
2734 state->hidden = -1;
2735 state->miniaturized = -1;
2736 state->shaded = -1;
2737 if (btn->dock == scr->dock || btn->omnipresent)
2738 state->workspace = -1;
2739 else
2740 state->workspace = scr->current_workspace;
2742 wWindowAddSavedState(btn->wm_instance, btn->wm_class, cmdline, pid, state);
2743 wAddDeathHandler(pid, (WDeathHandler *) trackDeadProcess, btn->dock);
2744 } else if (state) {
2745 wfree(state);
2747 wfree(cmdline);
2748 return pid;
2751 void wDockHideIcons(WDock *dock)
2753 int i;
2755 if (dock == NULL)
2756 return;
2758 for (i = 1; i < dock->max_icons; i++) {
2759 if (dock->icon_array[i])
2760 XUnmapWindow(dpy, dock->icon_array[i]->icon->core->window);
2762 dock->mapped = 0;
2764 dockIconPaint(dock->icon_array[0]);
2767 void wDockShowIcons(WDock *dock)
2769 int i, newlevel;
2770 WAppIcon *btn;
2772 if (dock == NULL)
2773 return;
2775 btn = dock->icon_array[0];
2776 moveDock(dock, btn->x_pos, btn->y_pos);
2778 newlevel = dock->lowered ? WMNormalLevel : WMDockLevel;
2779 ChangeStackingLevel(btn->icon->core, newlevel);
2781 for (i = 1; i < dock->max_icons; i++) {
2782 if (dock->icon_array[i]) {
2783 MoveInStackListAbove(dock->icon_array[i]->icon->core, btn->icon->core);
2784 break;
2788 if (!dock->collapsed) {
2789 for (i = 1; i < dock->max_icons; i++) {
2790 if (dock->icon_array[i])
2791 XMapWindow(dpy, dock->icon_array[i]->icon->core->window);
2794 dock->mapped = 1;
2796 dockIconPaint(btn);
2799 void wDockLower(WDock *dock)
2801 int i;
2803 for (i = 0; i < dock->max_icons; i++) {
2804 if (dock->icon_array[i])
2805 wLowerFrame(dock->icon_array[i]->icon->core);
2809 void wDockRaise(WDock *dock)
2811 int i;
2813 for (i = dock->max_icons - 1; i >= 0; i--) {
2814 if (dock->icon_array[i])
2815 wRaiseFrame(dock->icon_array[i]->icon->core);
2819 void wDockRaiseLower(WDock *dock)
2821 if (!dock->icon_array[0]->icon->core->stacking->above
2822 || (dock->icon_array[0]->icon->core->stacking->window_level
2823 != dock->icon_array[0]->icon->core->stacking->above->stacking->window_level))
2824 wDockLower(dock);
2825 else
2826 wDockRaise(dock);
2829 void wDockFinishLaunch(WDock *dock, WAppIcon *icon)
2831 icon->launching = 0;
2832 icon->relaunching = 0;
2833 dockIconPaint(icon);
2836 WAppIcon *wDockFindIconForWindow(WDock *dock, Window window)
2838 WAppIcon *icon;
2839 int i;
2841 for (i = 0; i < dock->max_icons; i++) {
2842 icon = dock->icon_array[i];
2843 if (icon && icon->main_window == window)
2844 return icon;
2846 return NULL;
2849 void wDockTrackWindowLaunch(WDock *dock, Window window)
2851 WAppIcon *icon;
2852 char *wm_class, *wm_instance;
2853 int i;
2854 Bool firstPass = True;
2855 Bool found = False;
2856 char *command = NULL;
2858 if (!PropGetWMClass(window, &wm_class, &wm_instance) || (!wm_class && !wm_instance))
2859 return;
2861 command = GetCommandForWindow(window);
2862 retry:
2863 for (i = 0; i < dock->max_icons; i++) {
2864 icon = dock->icon_array[i];
2865 if (!icon)
2866 continue;
2868 /* app is already attached to icon */
2869 if (icon->main_window == window) {
2870 found = True;
2871 break;
2874 if ((icon->wm_instance || icon->wm_class)
2875 && (icon->launching || !icon->running)) {
2877 if (icon->wm_instance && wm_instance && strcmp(icon->wm_instance, wm_instance) != 0)
2878 continue;
2880 if (icon->wm_class && wm_class && strcmp(icon->wm_class, wm_class) != 0)
2881 continue;
2883 if (firstPass && command && strcmp(icon->command, command) != 0)
2884 continue;
2886 if (!icon->relaunching) {
2887 WApplication *wapp;
2889 /* Possibly an application that was docked with dockit,
2890 * but the user did not update WMState to indicate that
2891 * it was docked by force */
2892 wapp = wApplicationOf(window);
2893 if (!wapp) {
2894 icon->forced_dock = 1;
2895 icon->running = 0;
2897 if (!icon->forced_dock)
2898 icon->main_window = window;
2900 found = True;
2901 if (!wPreferences.no_animations && !icon->launching &&
2902 !dock->screen_ptr->flags.startup && !dock->collapsed) {
2903 WAppIcon *aicon;
2904 int x0, y0;
2906 icon->launching = 1;
2907 dockIconPaint(icon);
2909 aicon = wAppIconCreateForDock(dock->screen_ptr, NULL,
2910 wm_instance, wm_class, TILE_NORMAL);
2911 /* XXX: can: aicon->icon == NULL ? */
2912 PlaceIcon(dock->screen_ptr, &x0, &y0, wGetHeadForWindow(aicon->icon->owner));
2913 wAppIconMove(aicon, x0, y0);
2914 /* Should this always be lowered? -Dan */
2915 if (dock->lowered)
2916 wLowerFrame(aicon->icon->core);
2917 XMapWindow(dpy, aicon->icon->core->window);
2918 aicon->launching = 1;
2919 wAppIconPaint(aicon);
2920 SlideWindow(aicon->icon->core->window, x0, y0, icon->x_pos, icon->y_pos);
2921 XUnmapWindow(dpy, aicon->icon->core->window);
2922 wAppIconDestroy(aicon);
2924 wDockFinishLaunch(dock, icon);
2925 break;
2929 if (firstPass && !found) {
2930 firstPass = False;
2931 goto retry;
2934 if (command)
2935 wfree(command);
2937 if (wm_class)
2938 free(wm_class);
2939 if (wm_instance)
2940 free(wm_instance);
2943 void wClipUpdateForWorkspaceChange(WScreen *scr, int workspace)
2945 if (!wPreferences.flags.noclip) {
2946 scr->clip_icon->dock = scr->workspaces[workspace]->clip;
2947 if (scr->current_workspace != workspace) {
2948 WDock *old_clip = scr->workspaces[scr->current_workspace]->clip;
2949 WAppIconChain *chain = scr->global_icons;
2951 while (chain) {
2952 moveIconBetweenDocks(chain->aicon->dock,
2953 scr->workspaces[workspace]->clip,
2954 chain->aicon, chain->aicon->xindex, chain->aicon->yindex);
2955 if (scr->workspaces[workspace]->clip->collapsed)
2956 XUnmapWindow(dpy, chain->aicon->icon->core->window);
2957 chain = chain->next;
2960 wDockHideIcons(old_clip);
2961 if (old_clip->auto_raise_lower) {
2962 if (old_clip->auto_raise_magic) {
2963 WMDeleteTimerHandler(old_clip->auto_raise_magic);
2964 old_clip->auto_raise_magic = NULL;
2966 wDockLower(old_clip);
2968 if (old_clip->auto_collapse) {
2969 if (old_clip->auto_expand_magic) {
2970 WMDeleteTimerHandler(old_clip->auto_expand_magic);
2971 old_clip->auto_expand_magic = NULL;
2973 old_clip->collapsed = 1;
2975 wDockShowIcons(scr->workspaces[workspace]->clip);
2980 static void trackDeadProcess(pid_t pid, unsigned char status, WDock *dock)
2982 WAppIcon *icon;
2983 int i;
2985 for (i = 0; i < dock->max_icons; i++) {
2986 icon = dock->icon_array[i];
2987 if (!icon)
2988 continue;
2990 if (icon->launching && icon->pid == pid) {
2991 if (!icon->relaunching) {
2992 icon->running = 0;
2993 icon->main_window = None;
2995 wDockFinishLaunch(dock, icon);
2996 icon->pid = 0;
2997 if (status == 111) {
2998 char msg[PATH_MAX];
2999 char *cmd;
3001 #ifdef XDND
3002 if (icon->drop_launch)
3003 cmd = icon->dnd_command;
3004 else
3005 #endif
3006 if (icon->paste_launch)
3007 cmd = icon->paste_command;
3008 else
3009 cmd = icon->command;
3011 snprintf(msg, sizeof(msg), _("Could not execute command \"%s\""), cmd);
3013 wMessageDialog(dock->screen_ptr, _("Error"), msg, _("OK"), NULL, NULL);
3015 break;
3020 static void toggleLowered(WDock *dock)
3022 WAppIcon *tmp;
3023 int newlevel, i;
3025 /* lower/raise Dock */
3026 if (!dock->lowered) {
3027 newlevel = WMNormalLevel;
3028 dock->lowered = 1;
3029 } else {
3030 newlevel = WMDockLevel;
3031 dock->lowered = 0;
3034 for (i = 0; i < dock->max_icons; i++) {
3035 tmp = dock->icon_array[i];
3036 if (!tmp)
3037 continue;
3039 ChangeStackingLevel(tmp->icon->core, newlevel);
3040 if (dock->lowered)
3041 wLowerFrame(tmp->icon->core);
3044 if (dock->type == WM_DOCK)
3045 wScreenUpdateUsableArea(dock->screen_ptr);
3048 static void toggleCollapsed(WDock *dock)
3050 if (dock->collapsed) {
3051 dock->collapsed = 0;
3052 wDockShowIcons(dock);
3053 } else {
3054 dock->collapsed = 1;
3055 wDockHideIcons(dock);
3059 static void openDockMenu(WDock *dock, WAppIcon *aicon, XEvent *event)
3061 WScreen *scr = dock->screen_ptr;
3062 WObjDescriptor *desc;
3063 WMenuEntry *entry;
3064 WApplication *wapp = NULL;
3065 int index = 0;
3066 int x_pos;
3067 int n_selected;
3068 int appIsRunning = aicon->running && aicon->icon && aicon->icon->owner;
3070 if (dock->type == WM_DOCK) {
3071 /* keep on top */
3072 entry = dock->menu->entries[index];
3073 entry->flags.indicator_on = !dock->lowered;
3074 entry->clientdata = dock;
3075 dock->menu->flags.realized = 0;
3076 } else {
3077 /* clip options */
3078 if (scr->clip_options)
3079 updateClipOptionsMenu(scr->clip_options, dock);
3081 n_selected = numberOfSelectedIcons(dock);
3083 /* Rename Workspace */
3084 entry = dock->menu->entries[++index];
3085 if (aicon == scr->clip_icon) {
3086 entry->callback = renameCallback;
3087 entry->clientdata = dock;
3088 entry->flags.indicator = 0;
3089 entry->text = _("Rename Workspace");
3090 } else {
3091 entry->callback = omnipresentCallback;
3092 entry->clientdata = aicon;
3093 if (n_selected > 0) {
3094 entry->flags.indicator = 0;
3095 entry->text = _("Toggle Omnipresent");
3096 } else {
3097 entry->flags.indicator = 1;
3098 entry->flags.indicator_on = aicon->omnipresent;
3099 entry->flags.indicator_type = MI_CHECK;
3100 entry->text = _("Omnipresent");
3104 /* select/unselect icon */
3105 entry = dock->menu->entries[++index];
3106 entry->clientdata = aicon;
3107 entry->flags.indicator_on = aicon->icon->selected;
3108 wMenuSetEnabled(dock->menu, index, aicon != scr->clip_icon);
3110 /* select/unselect all icons */
3111 entry = dock->menu->entries[++index];
3112 entry->clientdata = aicon;
3113 if (n_selected > 0)
3114 entry->text = _("Unselect All Icons");
3115 else
3116 entry->text = _("Select All Icons");
3118 wMenuSetEnabled(dock->menu, index, dock->icon_count > 1);
3120 /* keep icon(s) */
3121 entry = dock->menu->entries[++index];
3122 entry->clientdata = aicon;
3123 if (n_selected > 1)
3124 entry->text = _("Keep Icons");
3125 else
3126 entry->text = _("Keep Icon");
3128 wMenuSetEnabled(dock->menu, index, dock->icon_count > 1);
3130 /* this is the workspace submenu part */
3131 entry = dock->menu->entries[++index];
3132 if (n_selected > 1)
3133 entry->text = _("Move Icons To");
3134 else
3135 entry->text = _("Move Icon To");
3137 if (scr->clip_submenu)
3138 updateWorkspaceMenu(scr->clip_submenu, aicon);
3140 wMenuSetEnabled(dock->menu, index, !aicon->omnipresent);
3142 /* remove icon(s) */
3143 entry = dock->menu->entries[++index];
3144 entry->clientdata = aicon;
3145 if (n_selected > 1)
3146 entry->text = _("Remove Icons");
3147 else
3148 entry->text = _("Remove Icon");
3150 wMenuSetEnabled(dock->menu, index, dock->icon_count > 1);
3152 /* attract icon(s) */
3153 entry = dock->menu->entries[++index];
3154 entry->clientdata = aicon;
3156 dock->menu->flags.realized = 0;
3157 wMenuRealize(dock->menu);
3160 if (aicon->icon->owner)
3161 wapp = wApplicationOf(aicon->icon->owner->main_window);
3162 else
3163 wapp = NULL;
3165 /* launch */
3166 entry = dock->menu->entries[++index];
3167 entry->clientdata = aicon;
3168 wMenuSetEnabled(dock->menu, index, aicon->command != NULL);
3170 /* unhide here */
3171 entry = dock->menu->entries[++index];
3172 entry->clientdata = aicon;
3173 if (wapp && wapp->flags.hidden)
3174 entry->text = _("Unhide Here");
3175 else
3176 entry->text = _("Bring Here");
3178 wMenuSetEnabled(dock->menu, index, appIsRunning);
3180 /* hide */
3181 entry = dock->menu->entries[++index];
3182 entry->clientdata = aicon;
3183 if (wapp && wapp->flags.hidden)
3184 entry->text = _("Unhide");
3185 else
3186 entry->text = _("Hide");
3188 wMenuSetEnabled(dock->menu, index, appIsRunning);
3190 /* settings */
3191 entry = dock->menu->entries[++index];
3192 entry->clientdata = aicon;
3193 wMenuSetEnabled(dock->menu, index, !aicon->editing && !wPreferences.flags.noupdates);
3195 /* kill */
3196 entry = dock->menu->entries[++index];
3197 entry->clientdata = aicon;
3198 wMenuSetEnabled(dock->menu, index, appIsRunning);
3200 if (!dock->menu->flags.realized)
3201 wMenuRealize(dock->menu);
3203 if (dock->type == WM_CLIP) {
3204 /*x_pos = event->xbutton.x_root+2; */
3205 x_pos = event->xbutton.x_root - dock->menu->frame->core->width / 2 - 1;
3206 if (x_pos < 0) {
3207 x_pos = 0;
3208 } else if (x_pos + dock->menu->frame->core->width > scr->scr_width - 2) {
3209 x_pos = scr->scr_width - dock->menu->frame->core->width - 4;
3211 } else {
3212 x_pos = dock->on_right_side ? scr->scr_width - dock->menu->frame->core->width - 3 : 0;
3215 wMenuMapAt(dock->menu, x_pos, event->xbutton.y_root + 2, False);
3217 /* allow drag select */
3218 event->xany.send_event = True;
3219 desc = &dock->menu->menu->descriptor;
3220 (*desc->handle_mousedown) (desc, event);
3223 /******************************************************************/
3224 static void iconDblClick(WObjDescriptor *desc, XEvent *event)
3226 WAppIcon *btn = desc->parent;
3227 WDock *dock = btn->dock;
3228 WApplication *wapp = NULL;
3229 int unhideHere = 0;
3231 if (btn->icon->owner && !(event->xbutton.state & ControlMask)) {
3232 wapp = wApplicationOf(btn->icon->owner->main_window);
3234 assert(wapp != NULL);
3236 unhideHere = (event->xbutton.state & ShiftMask);
3238 /* go to the last workspace that the user worked on the app */
3239 if (wapp->last_workspace != dock->screen_ptr->current_workspace && !unhideHere)
3240 wWorkspaceChange(dock->screen_ptr, wapp->last_workspace);
3242 wUnhideApplication(wapp, event->xbutton.button == Button2, unhideHere);
3244 if (event->xbutton.state & MOD_MASK)
3245 wHideOtherApplications(btn->icon->owner);
3246 } else {
3247 if (event->xbutton.button == Button1) {
3248 if (event->xbutton.state & MOD_MASK) {
3249 /* raise/lower dock */
3250 toggleLowered(dock);
3251 } else if (btn == dock->screen_ptr->clip_icon) {
3252 if (getClipButton(event->xbutton.x, event->xbutton.y) == CLIP_IDLE)
3253 toggleCollapsed(dock);
3254 else
3255 handleClipChangeWorkspace(dock->screen_ptr, event);
3256 } else if (btn->command) {
3257 if (!btn->launching && (!btn->running || (event->xbutton.state & ControlMask)))
3258 launchDockedApplication(btn, False);
3264 static void handleDockMove(WDock *dock, WAppIcon *aicon, XEvent *event)
3266 WScreen *scr = dock->screen_ptr;
3267 int ofs_x = event->xbutton.x, ofs_y = event->xbutton.y;
3268 int x, y;
3269 XEvent ev;
3270 int grabbed = 0, swapped = 0, done;
3271 Pixmap ghost = None;
3272 int superfluous = wPreferences.superfluous; /* we catch it to avoid problems */
3274 if (XGrabPointer(dpy, aicon->icon->core->window, True, ButtonMotionMask
3275 | ButtonReleaseMask | ButtonPressMask, GrabModeAsync,
3276 GrabModeAsync, None, None, CurrentTime) != GrabSuccess)
3277 wwarning("pointer grab failed for dock move");
3279 y = 0;
3280 for (x = 0; x < dock->max_icons; x++) {
3281 if (dock->icon_array[x] != NULL && dock->icon_array[x]->yindex > y)
3282 y = dock->icon_array[x]->yindex;
3284 y++;
3285 XResizeWindow(dpy, scr->dock_shadow, ICON_SIZE, ICON_SIZE * y);
3287 done = 0;
3288 while (!done) {
3289 WMMaskEvent(dpy, PointerMotionMask | ButtonReleaseMask | ButtonPressMask
3290 | ButtonMotionMask | ExposureMask | EnterWindowMask, &ev);
3291 switch (ev.type) {
3292 case Expose:
3293 WMHandleEvent(&ev);
3294 break;
3296 case EnterNotify:
3297 /* It means the cursor moved so fast that it entered
3298 * something else (if moving slowly, it would have
3299 * stayed in the dock that is being moved. Ignore such
3300 * "spurious" EnterNotifiy's */
3301 break;
3303 case MotionNotify:
3304 if (!grabbed) {
3305 if (abs(ofs_x - ev.xmotion.x) >= MOVE_THRESHOLD
3306 || abs(ofs_y - ev.xmotion.y) >= MOVE_THRESHOLD) {
3307 XChangeActivePointerGrab(dpy, ButtonMotionMask
3308 | ButtonReleaseMask | ButtonPressMask,
3309 wCursor[WCUR_MOVE], CurrentTime);
3310 grabbed = 1;
3312 break;
3314 if (dock->type == WM_CLIP) {
3315 x = ev.xmotion.x_root - ofs_x;
3316 y = ev.xmotion.y_root - ofs_y;
3317 wScreenKeepInside(scr, &x, &y, ICON_SIZE, ICON_SIZE);
3319 moveDock(dock, x, y);
3320 } else {
3321 /* move vertically if pointer is inside the dock */
3322 if ((dock->on_right_side && ev.xmotion.x_root >= dock->x_pos - ICON_SIZE)
3323 || (!dock->on_right_side && ev.xmotion.x_root <= dock->x_pos + ICON_SIZE * 2)) {
3325 x = ev.xmotion.x_root - ofs_x;
3326 y = ev.xmotion.y_root - ofs_y;
3327 wScreenKeepInside(scr, &x, &y, ICON_SIZE, ICON_SIZE);
3328 moveDock(dock, dock->x_pos, y);
3330 /* move horizontally to change sides */
3331 x = ev.xmotion.x_root - ofs_x;
3332 if (!dock->on_right_side) {
3334 /* is on left */
3335 if (ev.xmotion.x_root > dock->x_pos + ICON_SIZE * 2) {
3336 XMoveWindow(dpy, scr->dock_shadow, scr->scr_width - ICON_SIZE
3337 - DOCK_EXTRA_SPACE - 1, dock->y_pos);
3338 if (superfluous && ghost == None) {
3339 ghost = MakeGhostDock(dock, dock->x_pos,
3340 scr->scr_width - ICON_SIZE
3341 - DOCK_EXTRA_SPACE - 1, dock->y_pos);
3342 XSetWindowBackgroundPixmap(dpy, scr->dock_shadow, ghost);
3343 XClearWindow(dpy, scr->dock_shadow);
3345 XMapRaised(dpy, scr->dock_shadow);
3346 swapped = 1;
3347 } else {
3348 if (superfluous && ghost != None) {
3349 XFreePixmap(dpy, ghost);
3350 ghost = None;
3352 XUnmapWindow(dpy, scr->dock_shadow);
3353 swapped = 0;
3355 } else {
3356 /* is on right */
3357 if (ev.xmotion.x_root < dock->x_pos - ICON_SIZE) {
3358 XMoveWindow(dpy, scr->dock_shadow, DOCK_EXTRA_SPACE, dock->y_pos);
3359 if (superfluous && ghost == None) {
3360 ghost = MakeGhostDock(dock, dock->x_pos,
3361 DOCK_EXTRA_SPACE, dock->y_pos);
3362 XSetWindowBackgroundPixmap(dpy, scr->dock_shadow, ghost);
3363 XClearWindow(dpy, scr->dock_shadow);
3365 XMapRaised(dpy, scr->dock_shadow);
3366 swapped = -1;
3367 } else {
3368 XUnmapWindow(dpy, scr->dock_shadow);
3369 swapped = 0;
3370 if (superfluous && ghost != None) {
3371 XFreePixmap(dpy, ghost);
3372 ghost = None;
3377 break;
3379 case ButtonPress:
3380 break;
3382 case ButtonRelease:
3383 if (ev.xbutton.button != event->xbutton.button)
3384 break;
3385 XUngrabPointer(dpy, CurrentTime);
3386 XUnmapWindow(dpy, scr->dock_shadow);
3387 XResizeWindow(dpy, scr->dock_shadow, ICON_SIZE, ICON_SIZE);
3388 if (dock->type == WM_DOCK) {
3389 if (swapped != 0) {
3390 if (swapped > 0)
3391 dock->on_right_side = 1;
3392 else
3393 dock->on_right_side = 0;
3394 swapDock(dock);
3395 wArrangeIcons(scr, False);
3398 done = 1;
3399 break;
3402 if (superfluous) {
3403 if (ghost != None)
3404 XFreePixmap(dpy, ghost);
3405 XSetWindowBackground(dpy, scr->dock_shadow, scr->white_pixel);
3409 static Bool handleIconMove(WDock *dock, WAppIcon *aicon, XEvent *event)
3411 WScreen *scr = dock->screen_ptr;
3412 Window wins[2];
3413 WIcon *icon = aicon->icon;
3414 WDock *dock2 = NULL, *last_dock = dock, *clip = NULL;
3415 int ondock, grabbed = 0, change_dock = 0, collapsed = 0;
3416 XEvent ev;
3417 int x = aicon->x_pos, y = aicon->y_pos;
3418 int ofs_x = event->xbutton.x, ofs_y = event->xbutton.y;
3419 int shad_x = x, shad_y = y;
3420 int ix = aicon->xindex, iy = aicon->yindex;
3421 int tmp;
3422 Pixmap ghost = None;
3423 Bool docked;
3424 int superfluous = wPreferences.superfluous; /* we catch it to avoid problems */
3425 int omnipresent = aicon->omnipresent; /* this must be cached!!! */
3426 Bool hasMoved = False;
3428 if (wPreferences.flags.noupdates)
3429 return hasMoved;
3431 if (XGrabPointer(dpy, icon->core->window, True, ButtonMotionMask
3432 | ButtonReleaseMask | ButtonPressMask, GrabModeAsync,
3433 GrabModeAsync, None, None, CurrentTime) != GrabSuccess) {
3436 if (!(event->xbutton.state & MOD_MASK))
3437 wRaiseFrame(icon->core);
3439 if (!wPreferences.flags.noclip)
3440 clip = scr->workspaces[scr->current_workspace]->clip;
3442 if (dock == scr->dock && !wPreferences.flags.noclip)
3443 dock2 = clip;
3444 else if (dock != scr->dock && !wPreferences.flags.nodock)
3445 dock2 = scr->dock;
3447 wins[0] = icon->core->window;
3448 wins[1] = scr->dock_shadow;
3449 XRestackWindows(dpy, wins, 2);
3450 XMoveResizeWindow(dpy, scr->dock_shadow, aicon->x_pos, aicon->y_pos, ICON_SIZE, ICON_SIZE);
3451 if (superfluous) {
3452 if (icon->pixmap != None)
3453 ghost = MakeGhostIcon(scr, icon->pixmap);
3454 else
3455 ghost = MakeGhostIcon(scr, icon->core->window);
3457 XSetWindowBackgroundPixmap(dpy, scr->dock_shadow, ghost);
3458 XClearWindow(dpy, scr->dock_shadow);
3460 XMapWindow(dpy, scr->dock_shadow);
3462 ondock = 1;
3464 while (1) {
3465 XMaskEvent(dpy, PointerMotionMask | ButtonReleaseMask | ButtonPressMask
3466 | ButtonMotionMask | ExposureMask, &ev);
3467 switch (ev.type) {
3468 case Expose:
3469 WMHandleEvent(&ev);
3470 break;
3472 case MotionNotify:
3473 hasMoved = True;
3474 if (!grabbed) {
3475 if (abs(ofs_x - ev.xmotion.x) >= MOVE_THRESHOLD
3476 || abs(ofs_y - ev.xmotion.y) >= MOVE_THRESHOLD) {
3477 XChangeActivePointerGrab(dpy, ButtonMotionMask
3478 | ButtonReleaseMask | ButtonPressMask,
3479 wCursor[WCUR_MOVE], CurrentTime);
3480 grabbed = 1;
3481 } else {
3482 break;
3486 if (omnipresent) {
3487 int i;
3488 for (i = 0; i < scr->workspace_count; i++) {
3489 if (i == scr->current_workspace)
3490 continue;
3491 wDockShowIcons(scr->workspaces[i]->clip);
3495 x = ev.xmotion.x_root - ofs_x;
3496 y = ev.xmotion.y_root - ofs_y;
3497 tmp = wDockSnapIcon(dock, aicon, x, y, &ix, &iy, True);
3498 if (tmp && dock2) {
3499 change_dock = 0;
3500 if (last_dock != dock && collapsed) {
3501 last_dock->collapsed = 1;
3502 wDockHideIcons(last_dock);
3503 collapsed = 0;
3505 if (!collapsed && (collapsed = dock->collapsed)) {
3506 dock->collapsed = 0;
3507 wDockShowIcons(dock);
3509 if (dock->auto_raise_lower)
3510 wDockRaise(dock);
3511 last_dock = dock;
3512 } else if (dock2) {
3513 tmp = wDockSnapIcon(dock2, aicon, x, y, &ix, &iy, False);
3514 if (tmp) {
3515 change_dock = 1;
3516 if (last_dock != dock2 && collapsed) {
3517 last_dock->collapsed = 1;
3518 wDockHideIcons(last_dock);
3519 collapsed = 0;
3521 if (!collapsed && (collapsed = dock2->collapsed)) {
3522 dock2->collapsed = 0;
3523 wDockShowIcons(dock2);
3525 if (dock2->auto_raise_lower)
3526 wDockRaise(dock2);
3527 last_dock = dock2;
3530 if (aicon->launching || aicon->lock || (aicon->running && !(ev.xmotion.state & MOD_MASK))
3531 || (!aicon->running && tmp)) {
3532 shad_x = last_dock->x_pos + ix * wPreferences.icon_size;
3533 shad_y = last_dock->y_pos + iy * wPreferences.icon_size;
3535 XMoveWindow(dpy, scr->dock_shadow, shad_x, shad_y);
3537 if (!ondock)
3538 XMapWindow(dpy, scr->dock_shadow);
3540 ondock = 1;
3541 } else {
3542 if (ondock)
3543 XUnmapWindow(dpy, scr->dock_shadow);
3545 ondock = 0;
3547 XMoveWindow(dpy, icon->core->window, x, y);
3548 break;
3550 case ButtonPress:
3551 break;
3553 case ButtonRelease:
3554 if (ev.xbutton.button != event->xbutton.button)
3555 break;
3556 XUngrabPointer(dpy, CurrentTime);
3557 if (ondock) {
3558 SlideWindow(icon->core->window, x, y, shad_x, shad_y);
3559 XUnmapWindow(dpy, scr->dock_shadow);
3560 if (!change_dock) {
3561 reattachIcon(dock, aicon, ix, iy);
3562 if (clip && dock != clip && clip->auto_raise_lower)
3563 wDockLower(clip);
3564 } else {
3565 docked = moveIconBetweenDocks(dock, dock2, aicon, ix, iy);
3566 if (!docked) {
3567 /* Slide it back if dock rejected it */
3568 SlideWindow(icon->core->window, x, y, aicon->x_pos, aicon->y_pos);
3569 reattachIcon(dock, aicon, aicon->xindex, aicon->yindex);
3571 if (last_dock->type == WM_CLIP && last_dock->auto_collapse)
3572 collapsed = 0;
3574 } else {
3575 aicon->x_pos = x;
3576 aicon->y_pos = y;
3577 if (superfluous) {
3578 if (!aicon->running && !wPreferences.no_animations) {
3579 /* We need to deselect it, even if is deselected in
3580 * wDockDetach(), because else DoKaboom() will fail.
3582 if (aicon->icon->selected)
3583 wIconSelect(aicon->icon);
3585 DoKaboom(scr, aicon->icon->core->window, x, y);
3588 if (clip && clip->auto_raise_lower)
3589 wDockLower(clip);
3590 wDockDetach(dock, aicon);
3592 if (collapsed) {
3593 last_dock->collapsed = 1;
3594 wDockHideIcons(last_dock);
3595 collapsed = 0;
3597 if (superfluous) {
3598 if (ghost != None)
3599 XFreePixmap(dpy, ghost);
3600 XSetWindowBackground(dpy, scr->dock_shadow, scr->white_pixel);
3602 if (omnipresent) {
3603 int i;
3604 for (i = 0; i < scr->workspace_count; i++) {
3605 if (i == scr->current_workspace)
3606 continue;
3607 wDockHideIcons(scr->workspaces[i]->clip);
3610 return hasMoved;;
3613 return False; /* never reached */
3616 static int getClipButton(int px, int py)
3618 int pt = (CLIP_BUTTON_SIZE + 2) * ICON_SIZE / 64;
3620 if (px < 0 || py < 0 || px >= ICON_SIZE || py >= ICON_SIZE)
3621 return CLIP_IDLE;
3623 if (py <= pt - ((int)ICON_SIZE - 1 - px))
3624 return CLIP_FORWARD;
3625 else if (px <= pt - ((int)ICON_SIZE - 1 - py))
3626 return CLIP_REWIND;
3628 return CLIP_IDLE;
3631 static void handleClipChangeWorkspace(WScreen *scr, XEvent *event)
3633 XEvent ev;
3634 int done, direction, new_ws;
3635 int new_dir;
3636 WDock *clip = scr->clip_icon->dock;
3638 direction = getClipButton(event->xbutton.x, event->xbutton.y);
3640 clip->lclip_button_pushed = direction == CLIP_REWIND;
3641 clip->rclip_button_pushed = direction == CLIP_FORWARD;
3643 wClipIconPaint(scr->clip_icon);
3644 done = 0;
3645 while (!done) {
3646 WMMaskEvent(dpy, ExposureMask | ButtonMotionMask | ButtonReleaseMask | ButtonPressMask, &ev);
3647 switch (ev.type) {
3648 case Expose:
3649 WMHandleEvent(&ev);
3650 break;
3652 case MotionNotify:
3653 new_dir = getClipButton(ev.xmotion.x, ev.xmotion.y);
3654 if (new_dir != direction) {
3655 direction = new_dir;
3656 clip->lclip_button_pushed = direction == CLIP_REWIND;
3657 clip->rclip_button_pushed = direction == CLIP_FORWARD;
3658 wClipIconPaint(scr->clip_icon);
3660 break;
3662 case ButtonPress:
3663 break;
3665 case ButtonRelease:
3666 if (ev.xbutton.button == event->xbutton.button)
3667 done = 1;
3671 clip->lclip_button_pushed = 0;
3672 clip->rclip_button_pushed = 0;
3674 new_ws = wPreferences.ws_advance || (event->xbutton.state & ControlMask);
3676 if (direction == CLIP_FORWARD) {
3677 if (scr->current_workspace < scr->workspace_count - 1)
3678 wWorkspaceChange(scr, scr->current_workspace + 1);
3679 else if (new_ws && scr->current_workspace < MAX_WORKSPACES - 1)
3680 wWorkspaceChange(scr, scr->current_workspace + 1);
3681 else if (wPreferences.ws_cycle)
3682 wWorkspaceChange(scr, 0);
3683 } else if (direction == CLIP_REWIND) {
3684 if (scr->current_workspace > 0)
3685 wWorkspaceChange(scr, scr->current_workspace - 1);
3686 else if (scr->current_workspace == 0 && wPreferences.ws_cycle)
3687 wWorkspaceChange(scr, scr->workspace_count - 1);
3690 wClipIconPaint(scr->clip_icon);
3693 static void iconMouseDown(WObjDescriptor *desc, XEvent *event)
3695 WAppIcon *aicon = desc->parent;
3696 WDock *dock = aicon->dock;
3697 WScreen *scr = aicon->icon->core->screen_ptr;
3699 if (aicon->editing || WCHECK_STATE(WSTATE_MODAL))
3700 return;
3702 scr->last_dock = dock;
3704 if (dock->menu->flags.mapped)
3705 wMenuUnmap(dock->menu);
3707 if (IsDoubleClick(scr, event)) {
3708 /* double-click was not in the main clip icon */
3709 if (dock->type != WM_CLIP || aicon->xindex != 0 || aicon->yindex != 0
3710 || getClipButton(event->xbutton.x, event->xbutton.y) == CLIP_IDLE) {
3711 iconDblClick(desc, event);
3712 return;
3716 if (event->xbutton.button == Button1) {
3717 if (event->xbutton.state & MOD_MASK)
3718 wDockLower(dock);
3719 else
3720 wDockRaise(dock);
3722 if ((event->xbutton.state & ShiftMask) && aicon != scr->clip_icon && dock->type != WM_DOCK) {
3723 wIconSelect(aicon->icon);
3724 return;
3727 if (aicon->yindex == 0 && aicon->xindex == 0) {
3728 if (getClipButton(event->xbutton.x, event->xbutton.y) != CLIP_IDLE
3729 && dock->type == WM_CLIP)
3730 handleClipChangeWorkspace(scr, event);
3731 else
3732 handleDockMove(dock, aicon, event);
3733 } else {
3734 Bool hasMoved = handleIconMove(dock, aicon, event);
3735 if (wPreferences.single_click && !hasMoved)
3736 iconDblClick(desc, event);
3738 } else if (event->xbutton.button == Button2 && dock->type == WM_CLIP && aicon == scr->clip_icon) {
3739 if (!scr->clip_ws_menu) {
3740 scr->clip_ws_menu = wWorkspaceMenuMake(scr, False);
3742 if (scr->clip_ws_menu) {
3743 WMenu *wsMenu = scr->clip_ws_menu;
3744 int xpos;
3746 wWorkspaceMenuUpdate(scr, wsMenu);
3748 xpos = event->xbutton.x_root - wsMenu->frame->core->width / 2 - 1;
3749 if (xpos < 0) {
3750 xpos = 0;
3751 } else if (xpos + wsMenu->frame->core->width > scr->scr_width - 2) {
3752 xpos = scr->scr_width - wsMenu->frame->core->width - 4;
3754 wMenuMapAt(wsMenu, xpos, event->xbutton.y_root + 2, False);
3756 desc = &wsMenu->menu->descriptor;
3757 event->xany.send_event = True;
3758 (*desc->handle_mousedown) (desc, event);
3760 } else if (event->xbutton.button == Button2 && dock->type == WM_CLIP &&
3761 (event->xbutton.state & ShiftMask) && aicon != scr->clip_icon) {
3762 wClipMakeIconOmnipresent(aicon, !aicon->omnipresent);
3763 } else if (event->xbutton.button == Button3) {
3764 if (event->xbutton.send_event &&
3765 XGrabPointer(dpy, aicon->icon->core->window, True, ButtonMotionMask
3766 | ButtonReleaseMask | ButtonPressMask, GrabModeAsync,
3767 GrabModeAsync, None, None, CurrentTime) != GrabSuccess) {
3768 wwarning("pointer grab failed for dockicon menu");
3769 return;
3772 openDockMenu(dock, aicon, event);
3773 } else if (event->xbutton.button == Button2) {
3774 WAppIcon *btn = desc->parent;
3776 if (!btn->launching && (!btn->running || (event->xbutton.state & ControlMask)))
3777 launchDockedApplication(btn, True);
3781 static void clipEnterNotify(WObjDescriptor *desc, XEvent *event)
3783 WAppIcon *btn = (WAppIcon *) desc->parent;
3784 WDock *dock;
3785 WScreen *scr;
3787 assert(event->type == EnterNotify);
3789 if (desc->parent_type != WCLASS_DOCK_ICON)
3790 return;
3792 scr = btn->icon->core->screen_ptr;
3793 if (!btn->omnipresent)
3794 dock = btn->dock;
3795 else
3796 dock = scr->workspaces[scr->current_workspace]->clip;
3798 if (!dock || dock->type != WM_CLIP)
3799 return;
3801 /* The auto raise/lower code */
3802 if (dock->auto_lower_magic) {
3803 WMDeleteTimerHandler(dock->auto_lower_magic);
3804 dock->auto_lower_magic = NULL;
3806 if (dock->auto_raise_lower && !dock->auto_raise_magic)
3807 dock->auto_raise_magic = WMAddTimerHandler(AUTO_RAISE_DELAY, clipAutoRaise, (void *)dock);
3809 /* The auto expand/collapse code */
3810 if (dock->auto_collapse_magic) {
3811 WMDeleteTimerHandler(dock->auto_collapse_magic);
3812 dock->auto_collapse_magic = NULL;
3814 if (dock->auto_collapse && !dock->auto_expand_magic)
3815 dock->auto_expand_magic = WMAddTimerHandler(AUTO_EXPAND_DELAY, clipAutoExpand, (void *)dock);
3818 static void clipLeave(WDock *dock)
3820 XEvent event;
3821 WObjDescriptor *desc = NULL;
3823 if (!dock || dock->type != WM_CLIP)
3824 return;
3826 if (XCheckTypedEvent(dpy, EnterNotify, &event) != False) {
3827 if (XFindContext(dpy, event.xcrossing.window, wWinContext,
3828 (XPointer *) & desc) != XCNOENT
3829 && desc && desc->parent_type == WCLASS_DOCK_ICON
3830 && ((WAppIcon *) desc->parent)->dock && ((WAppIcon *) desc->parent)->dock->type == WM_CLIP) {
3831 /* We didn't left the Clip yet */
3832 XPutBackEvent(dpy, &event);
3833 return;
3836 XPutBackEvent(dpy, &event);
3837 } else {
3838 /* We entered a withdrawn window, so we're still in Clip */
3839 return;
3842 if (dock->auto_raise_magic) {
3843 WMDeleteTimerHandler(dock->auto_raise_magic);
3844 dock->auto_raise_magic = NULL;
3846 if (dock->auto_raise_lower && !dock->auto_lower_magic)
3847 dock->auto_lower_magic = WMAddTimerHandler(AUTO_LOWER_DELAY, clipAutoLower, (void *)dock);
3849 if (dock->auto_expand_magic) {
3850 WMDeleteTimerHandler(dock->auto_expand_magic);
3851 dock->auto_expand_magic = NULL;
3853 if (dock->auto_collapse && !dock->auto_collapse_magic)
3854 dock->auto_collapse_magic = WMAddTimerHandler(AUTO_COLLAPSE_DELAY, clipAutoCollapse, (void *)dock);
3857 static void clipLeaveNotify(WObjDescriptor *desc, XEvent *event)
3859 WAppIcon *btn = (WAppIcon *) desc->parent;
3861 assert(event->type == LeaveNotify);
3863 if (desc->parent_type != WCLASS_DOCK_ICON)
3864 return;
3866 clipLeave(btn->dock);
3869 static void clipAutoCollapse(void *cdata)
3871 WDock *dock = (WDock *) cdata;
3873 if (dock->type != WM_CLIP)
3874 return;
3876 if (dock->auto_collapse) {
3877 dock->collapsed = 1;
3878 wDockHideIcons(dock);
3880 dock->auto_collapse_magic = NULL;
3883 static void clipAutoExpand(void *cdata)
3885 WDock *dock = (WDock *) cdata;
3887 if (dock->type != WM_CLIP)
3888 return;
3890 if (dock->auto_collapse) {
3891 dock->collapsed = 0;
3892 wDockShowIcons(dock);
3894 dock->auto_expand_magic = NULL;
3897 static void clipAutoLower(void *cdata)
3899 WDock *dock = (WDock *) cdata;
3901 if (dock->type != WM_CLIP)
3902 return;
3904 if (dock->auto_raise_lower)
3905 wDockLower(dock);
3907 dock->auto_lower_magic = NULL;
3910 static void clipAutoRaise(void *cdata)
3912 WDock *dock = (WDock *) cdata;
3914 if (dock->type != WM_CLIP)
3915 return;
3917 if (dock->auto_raise_lower)
3918 wDockRaise(dock);
3920 dock->auto_raise_magic = NULL;
3923 static Bool iconCanBeOmnipresent(WAppIcon *aicon)
3925 WScreen *scr = aicon->icon->core->screen_ptr;
3926 WDock *clip;
3927 WAppIcon *btn;
3928 int i, j;
3930 for (i = 0; i < scr->workspace_count; i++) {
3931 clip = scr->workspaces[i]->clip;
3933 if (clip == aicon->dock)
3934 continue;
3936 if (clip->icon_count + scr->global_icon_count >= clip->max_icons)
3937 return False; /* Clip is full in some workspace */
3939 for (j = 0; j < clip->max_icons; j++) {
3940 btn = clip->icon_array[j];
3941 if (btn && btn->xindex == aicon->xindex && btn->yindex == aicon->yindex)
3942 return False;
3946 return True;
3949 int wClipMakeIconOmnipresent(WAppIcon *aicon, int omnipresent)
3951 WScreen *scr = aicon->icon->core->screen_ptr;
3952 WAppIconChain *new_entry, *tmp, *tmp1;
3953 int status = WO_SUCCESS;
3955 if ((scr->dock && aicon->dock == scr->dock) || aicon == scr->clip_icon)
3956 return WO_NOT_APPLICABLE;
3958 if (aicon->omnipresent == omnipresent)
3959 return WO_SUCCESS;
3961 if (omnipresent) {
3962 if (iconCanBeOmnipresent(aicon)) {
3963 aicon->omnipresent = 1;
3964 new_entry = wmalloc(sizeof(WAppIconChain));
3965 new_entry->aicon = aicon;
3966 new_entry->next = scr->global_icons;
3967 scr->global_icons = new_entry;
3968 scr->global_icon_count++;
3969 } else {
3970 aicon->omnipresent = 0;
3971 status = WO_FAILED;
3973 } else {
3974 aicon->omnipresent = 0;
3975 if (aicon == scr->global_icons->aicon) {
3976 tmp = scr->global_icons->next;
3977 wfree(scr->global_icons);
3978 scr->global_icons = tmp;
3979 scr->global_icon_count--;
3980 } else {
3981 tmp = scr->global_icons;
3982 while (tmp->next) {
3983 if (tmp->next->aicon == aicon) {
3984 tmp1 = tmp->next->next;
3985 wfree(tmp->next);
3986 tmp->next = tmp1;
3987 scr->global_icon_count--;
3988 break;
3990 tmp = tmp->next;
3995 wAppIconPaint(aicon);
3997 return status;