Coding style cleanup in dock.c
[wmaker-crm.git] / src / dock.c
blob26d348ab27db65608b4f4d020526d007fb8b7cbe
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);
537 if (aicon && aicon->attracted && aicon->command) {
538 aicon->attracted = 0;
539 if (aicon->icon->shadowed) {
540 aicon->icon->shadowed = 0;
541 aicon->icon->force_paint = 1;
542 wAppIconPaint(aicon);
545 save_appicon(aicon, True);
547 WMFreeArray(selectedIcons);
550 static void toggleAutoAttractCallback(WMenu *menu, WMenuEntry *entry)
552 WDock *dock = (WDock *) entry->clientdata;
554 assert(entry->clientdata != NULL);
556 dock->attract_icons = !dock->attract_icons;
558 entry->flags.indicator_on = dock->attract_icons;
560 wMenuPaint(menu);
563 static void selectCallback(WMenu *menu, WMenuEntry *entry)
565 WAppIcon *icon = (WAppIcon *) entry->clientdata;
567 assert(icon != NULL);
569 wIconSelect(icon->icon);
571 wMenuPaint(menu);
574 static void colectIconsCallback(WMenu *menu, WMenuEntry *entry)
576 WAppIcon *clickedIcon = (WAppIcon *) entry->clientdata;
577 WDock *clip;
578 WAppIcon *aicon;
579 int x, y, x_pos, y_pos;
581 assert(entry->clientdata != NULL);
582 clip = clickedIcon->dock;
584 aicon = clip->screen_ptr->app_icon_list;
586 while (aicon) {
587 if (!aicon->docked && wDockFindFreeSlot(clip, &x, &y)) {
588 x_pos = clip->x_pos + x * ICON_SIZE;
589 y_pos = clip->y_pos + y * ICON_SIZE;
590 if (aicon->x_pos != x_pos || aicon->y_pos != y_pos)
591 move_window(aicon->icon->core->window, aicon->x_pos, aicon->y_pos, x_pos, y_pos);
593 aicon->attracted = 1;
594 if (!aicon->icon->shadowed) {
595 aicon->icon->shadowed = 1;
596 aicon->icon->force_paint = 1;
597 /* We don't do an wAppIconPaint() here because it's in
598 * wDockAttachIcon(). -Dan
601 wDockAttachIcon(clip, aicon, x, y);
602 if (clip->collapsed || !clip->mapped)
603 XUnmapWindow(dpy, aicon->icon->core->window);
605 aicon = aicon->next;
609 static void selectIconsCallback(WMenu *menu, WMenuEntry *entry)
611 WAppIcon *clickedIcon = (WAppIcon *) entry->clientdata;
612 WDock *dock;
613 WMArray *selectedIcons;
614 WMArrayIterator iter;
615 WAppIcon *btn;
616 int i;
618 assert(clickedIcon != NULL);
619 dock = clickedIcon->dock;
621 selectedIcons = getSelected(dock);
623 if (!WMGetArrayItemCount(selectedIcons)) {
624 for (i = 1; i < dock->max_icons; i++) {
625 btn = dock->icon_array[i];
626 if (btn && !btn->icon->selected)
627 wIconSelect(btn->icon);
629 } else {
630 WM_ITERATE_ARRAY(selectedIcons, btn, iter) {
631 wIconSelect(btn->icon);
634 WMFreeArray(selectedIcons);
636 wMenuPaint(menu);
639 static void toggleCollapsedCallback(WMenu *menu, WMenuEntry *entry)
641 assert(entry->clientdata != NULL);
643 toggleCollapsed(entry->clientdata);
645 entry->flags.indicator_on = ((WDock *) entry->clientdata)->collapsed;
647 wMenuPaint(menu);
650 static void toggleAutoCollapseCallback(WMenu *menu, WMenuEntry *entry)
652 WDock *dock;
653 assert(entry->clientdata != NULL);
655 dock = (WDock *) entry->clientdata;
657 dock->auto_collapse = !dock->auto_collapse;
659 entry->flags.indicator_on = ((WDock *) entry->clientdata)->auto_collapse;
661 wMenuPaint(menu);
664 static void toggleAutoRaiseLowerCallback(WMenu *menu, WMenuEntry *entry)
666 WDock *dock;
667 assert(entry->clientdata != NULL);
669 dock = (WDock *) entry->clientdata;
671 dock->auto_raise_lower = !dock->auto_raise_lower;
673 entry->flags.indicator_on = ((WDock *) entry->clientdata)->auto_raise_lower;
675 wMenuPaint(menu);
678 static void launchCallback(WMenu *menu, WMenuEntry *entry)
680 WAppIcon *btn = (WAppIcon *) entry->clientdata;
682 launchDockedApplication(btn, False);
685 static void settingsCallback(WMenu *menu, WMenuEntry *entry)
687 WAppIcon *btn = (WAppIcon *) entry->clientdata;
689 if (btn->editing)
690 return;
691 ShowDockAppSettingsPanel(btn);
694 static void hideCallback(WMenu *menu, WMenuEntry *entry)
696 WApplication *wapp;
697 WAppIcon *btn = (WAppIcon *) entry->clientdata;
699 wapp = wApplicationOf(btn->icon->owner->main_window);
701 if (wapp->flags.hidden) {
702 wWorkspaceChange(btn->icon->core->screen_ptr, wapp->last_workspace);
703 wUnhideApplication(wapp, False, False);
704 } else {
705 wHideApplication(wapp);
709 static void unhideHereCallback(WMenu *menu, WMenuEntry *entry)
711 WApplication *wapp;
712 WAppIcon *btn = (WAppIcon *) entry->clientdata;
714 wapp = wApplicationOf(btn->icon->owner->main_window);
716 wUnhideApplication(wapp, False, True);
719 static WAppIcon *mainIconCreate(WScreen *scr, int type)
721 WAppIcon *btn;
722 int x_pos;
724 if (type == WM_CLIP) {
725 if (scr->clip_icon)
726 return scr->clip_icon;
727 btn = wAppIconCreateForDock(scr, NULL, "Logo", "WMClip", TILE_CLIP);
728 btn->icon->core->descriptor.handle_expose = clipIconExpose;
729 btn->icon->core->descriptor.handle_enternotify = clipEnterNotify;
730 btn->icon->core->descriptor.handle_leavenotify = clipLeaveNotify;
731 x_pos = 0;
732 } else {
733 btn = wAppIconCreateForDock(scr, NULL, "Logo", "WMDock", TILE_NORMAL);
734 x_pos = scr->scr_width - ICON_SIZE - DOCK_EXTRA_SPACE;
737 btn->xindex = 0;
738 btn->yindex = 0;
740 btn->icon->core->descriptor.handle_mousedown = iconMouseDown;
741 btn->icon->core->descriptor.parent_type = WCLASS_DOCK_ICON;
742 btn->icon->core->descriptor.parent = btn;
743 XMapWindow(dpy, btn->icon->core->window);
744 btn->x_pos = x_pos;
745 btn->y_pos = 0;
746 btn->docked = 1;
747 if (type == WM_CLIP)
748 scr->clip_icon = btn;
750 return btn;
753 static void switchWSCommand(WMenu *menu, WMenuEntry *entry)
755 WAppIcon *btn, *icon = (WAppIcon *) entry->clientdata;
756 WScreen *scr = icon->icon->core->screen_ptr;
757 WDock *src, *dest;
758 WMArray *selectedIcons;
759 int x, y;
761 if (entry->order == scr->current_workspace)
762 return;
763 src = icon->dock;
764 dest = scr->workspaces[entry->order]->clip;
766 selectedIcons = getSelected(src);
768 if (WMGetArrayItemCount(selectedIcons)) {
769 WMArrayIterator iter;
771 WM_ITERATE_ARRAY(selectedIcons, btn, iter) {
772 if (wDockFindFreeSlot(dest, &x, &y)) {
773 moveIconBetweenDocks(src, dest, btn, x, y);
774 XUnmapWindow(dpy, btn->icon->core->window);
777 } else if (icon != scr->clip_icon) {
778 if (wDockFindFreeSlot(dest, &x, &y)) {
779 moveIconBetweenDocks(src, dest, icon, x, y);
780 XUnmapWindow(dpy, icon->icon->core->window);
783 WMFreeArray(selectedIcons);
786 static void launchDockedApplication(WAppIcon *btn, Bool withSelection)
788 WScreen *scr = btn->icon->core->screen_ptr;
790 if (!btn->launching &&
791 ((!withSelection && btn->command != NULL) || (withSelection && btn->paste_command != NULL))) {
792 if (!btn->forced_dock) {
793 btn->relaunching = btn->running;
794 btn->running = 1;
796 if (btn->wm_instance || btn->wm_class) {
797 WWindowAttributes attr;
798 memset(&attr, 0, sizeof(WWindowAttributes));
799 wDefaultFillAttributes(btn->wm_instance, btn->wm_class, &attr, NULL, True);
801 if (!attr.no_appicon && !btn->buggy_app)
802 btn->launching = 1;
803 else
804 btn->running = 0;
806 btn->drop_launch = 0;
807 btn->paste_launch = withSelection;
808 scr->last_dock = btn->dock;
809 btn->pid = execCommand(btn, (withSelection ? btn->paste_command : btn->command), NULL);
810 if (btn->pid > 0) {
811 if (btn->buggy_app) {
812 /* give feedback that the app was launched */
813 btn->launching = 1;
814 dockIconPaint(btn);
815 btn->launching = 0;
816 WMAddTimerHandler(200, (WMCallback *) dockIconPaint, btn);
817 } else {
818 dockIconPaint(btn);
820 } else {
821 wwarning(_("could not launch application %s"), btn->command);
822 btn->launching = 0;
823 if (!btn->relaunching)
824 btn->running = 0;
829 static void updateWorkspaceMenu(WMenu *menu, WAppIcon *icon)
831 WScreen *scr = menu->frame->screen_ptr;
832 char title[MAX_WORKSPACENAME_WIDTH + 1];
833 int i;
835 if (!menu || !icon)
836 return;
838 for (i = 0; i < scr->workspace_count; i++) {
839 if (i < menu->entry_no) {
840 if (strcmp(menu->entries[i]->text, scr->workspaces[i]->name) != 0) {
841 wfree(menu->entries[i]->text);
842 strcpy(title, scr->workspaces[i]->name);
843 menu->entries[i]->text = wstrdup(title);
844 menu->flags.realized = 0;
846 menu->entries[i]->clientdata = (void *)icon;
847 } else {
848 strcpy(title, scr->workspaces[i]->name);
850 wMenuAddCallback(menu, title, switchWSCommand, (void *)icon);
852 menu->flags.realized = 0;
855 if (i == scr->current_workspace)
856 wMenuSetEnabled(menu, i, False);
857 else
858 wMenuSetEnabled(menu, i, True);
861 if (!menu->flags.realized)
862 wMenuRealize(menu);
865 static WMenu *makeWorkspaceMenu(WScreen *scr)
867 WMenu *menu;
869 menu = wMenuCreate(scr, NULL, False);
870 if (!menu)
871 wwarning(_("could not create workspace submenu for Clip menu"));
873 wMenuAddCallback(menu, "", switchWSCommand, (void *)scr->clip_icon);
875 menu->flags.realized = 0;
876 wMenuRealize(menu);
878 return menu;
881 static void updateClipOptionsMenu(WMenu *menu, WDock *dock)
883 WMenuEntry *entry;
884 int index = 0;
886 if (!menu || !dock)
887 return;
889 /* keep on top */
890 entry = menu->entries[index];
891 entry->flags.indicator_on = !dock->lowered;
892 entry->clientdata = dock;
894 /* collapsed */
895 entry = menu->entries[++index];
896 entry->flags.indicator_on = dock->collapsed;
897 entry->clientdata = dock;
899 /* auto-collapse */
900 entry = menu->entries[++index];
901 entry->flags.indicator_on = dock->auto_collapse;
902 entry->clientdata = dock;
904 /* auto-raise/lower */
905 entry = menu->entries[++index];
906 entry->flags.indicator_on = dock->auto_raise_lower;
907 entry->clientdata = dock;
908 wMenuSetEnabled(menu, index, dock->lowered);
910 /* attract icons */
911 entry = menu->entries[++index];
912 entry->flags.indicator_on = dock->attract_icons;
913 entry->clientdata = dock;
915 menu->flags.realized = 0;
916 wMenuRealize(menu);
919 static WMenu *makeClipOptionsMenu(WScreen *scr)
921 WMenu *menu;
922 WMenuEntry *entry;
924 menu = wMenuCreate(scr, NULL, False);
925 if (!menu) {
926 wwarning(_("could not create options submenu for Clip menu"));
927 return NULL;
930 entry = wMenuAddCallback(menu, _("Keep on Top"), toggleLoweredCallback, NULL);
931 entry->flags.indicator = 1;
932 entry->flags.indicator_on = 1;
933 entry->flags.indicator_type = MI_CHECK;
935 entry = wMenuAddCallback(menu, _("Collapsed"), toggleCollapsedCallback, NULL);
936 entry->flags.indicator = 1;
937 entry->flags.indicator_on = 1;
938 entry->flags.indicator_type = MI_CHECK;
940 entry = wMenuAddCallback(menu, _("Autocollapse"), toggleAutoCollapseCallback, NULL);
941 entry->flags.indicator = 1;
942 entry->flags.indicator_on = 1;
943 entry->flags.indicator_type = MI_CHECK;
945 entry = wMenuAddCallback(menu, _("Autoraise"), toggleAutoRaiseLowerCallback, NULL);
946 entry->flags.indicator = 1;
947 entry->flags.indicator_on = 1;
948 entry->flags.indicator_type = MI_CHECK;
950 entry = wMenuAddCallback(menu, _("Autoattract Icons"), toggleAutoAttractCallback, NULL);
951 entry->flags.indicator = 1;
952 entry->flags.indicator_on = 1;
953 entry->flags.indicator_type = MI_CHECK;
955 menu->flags.realized = 0;
956 wMenuRealize(menu);
958 return menu;
961 static WMenu *dockMenuCreate(WScreen *scr, int type)
963 WMenu *menu;
964 WMenuEntry *entry;
966 if (type == WM_CLIP && scr->clip_menu)
967 return scr->clip_menu;
969 menu = wMenuCreate(scr, NULL, False);
970 if (type != WM_CLIP) {
971 entry = wMenuAddCallback(menu, _("Keep on Top"), toggleLoweredCallback, NULL);
972 entry->flags.indicator = 1;
973 entry->flags.indicator_on = 1;
974 entry->flags.indicator_type = MI_CHECK;
975 } else {
976 entry = wMenuAddCallback(menu, _("Clip Options"), NULL, NULL);
977 scr->clip_options = makeClipOptionsMenu(scr);
978 if (scr->clip_options)
979 wMenuEntrySetCascade(menu, entry, scr->clip_options);
981 entry = wMenuAddCallback(menu, _("Rename Workspace"), renameCallback, NULL);
982 wfree(entry->text);
983 entry->text = _("Rename Workspace");
985 entry = wMenuAddCallback(menu, _("Selected"), selectCallback, NULL);
986 entry->flags.indicator = 1;
987 entry->flags.indicator_on = 1;
988 entry->flags.indicator_type = MI_CHECK;
990 entry = wMenuAddCallback(menu, _("Select All Icons"), selectIconsCallback, NULL);
991 wfree(entry->text);
992 entry->text = _("Select All Icons");
994 entry = wMenuAddCallback(menu, _("Keep Icon"), keepIconsCallback, NULL);
995 wfree(entry->text);
996 entry->text = _("Keep Icon");
998 entry = wMenuAddCallback(menu, _("Move Icon To"), NULL, NULL);
999 wfree(entry->text);
1000 entry->text = _("Move Icon To");
1001 scr->clip_submenu = makeWorkspaceMenu(scr);
1002 if (scr->clip_submenu)
1003 wMenuEntrySetCascade(menu, entry, scr->clip_submenu);
1005 entry = wMenuAddCallback(menu, _("Remove Icon"), removeIconsCallback, NULL);
1006 wfree(entry->text);
1007 entry->text = _("Remove Icon");
1009 wMenuAddCallback(menu, _("Attract Icons"), colectIconsCallback, NULL);
1012 wMenuAddCallback(menu, _("Launch"), launchCallback, NULL);
1014 wMenuAddCallback(menu, _("Unhide Here"), unhideHereCallback, NULL);
1016 entry = wMenuAddCallback(menu, _("Hide"), hideCallback, NULL);
1017 wfree(entry->text);
1018 entry->text = _("Hide");
1020 wMenuAddCallback(menu, _("Settings..."), settingsCallback, NULL);
1022 wMenuAddCallback(menu, _("Kill"), killCallback, NULL);
1024 if (type == WM_CLIP)
1025 scr->clip_menu = menu;
1027 return menu;
1030 WDock *wDockCreate(WScreen *scr, int type)
1032 WDock *dock;
1033 WAppIcon *btn;
1035 make_keys();
1037 dock = wmalloc(sizeof(WDock));
1039 dock->max_icons = DOCK_MAX_ICONS;
1041 dock->icon_array = wmalloc(sizeof(WAppIcon *) * dock->max_icons);
1043 btn = mainIconCreate(scr, type);
1045 btn->dock = dock;
1047 dock->x_pos = btn->x_pos;
1048 dock->y_pos = btn->y_pos;
1049 dock->screen_ptr = scr;
1050 dock->type = type;
1051 dock->icon_count = 1;
1052 dock->on_right_side = 1;
1053 dock->collapsed = 0;
1054 dock->auto_collapse = 0;
1055 dock->auto_collapse_magic = NULL;
1056 dock->auto_raise_lower = 0;
1057 dock->auto_lower_magic = NULL;
1058 dock->auto_raise_magic = NULL;
1059 dock->attract_icons = 0;
1060 dock->lowered = 1;
1061 dock->icon_array[0] = btn;
1062 wRaiseFrame(btn->icon->core);
1063 XMoveWindow(dpy, btn->icon->core->window, btn->x_pos, btn->y_pos);
1065 /* create dock menu */
1066 dock->menu = dockMenuCreate(scr, type);
1068 return dock;
1071 void wDockDestroy(WDock *dock)
1073 int i;
1074 WAppIcon *aicon;
1076 for (i = (dock->type == WM_CLIP) ? 1 : 0; i < dock->max_icons; i++) {
1077 aicon = dock->icon_array[i];
1078 if (aicon) {
1079 int keepit = aicon->running && wApplicationOf(aicon->main_window);
1080 wDockDetach(dock, aicon);
1081 if (keepit) {
1082 /* XXX: can: aicon->icon == NULL ? */
1083 PlaceIcon(dock->screen_ptr, &aicon->x_pos, &aicon->y_pos,
1084 wGetHeadForWindow(aicon->icon->owner));
1085 XMoveWindow(dpy, aicon->icon->core->window, aicon->x_pos, aicon->y_pos);
1086 if (!dock->mapped || dock->collapsed)
1087 XMapWindow(dpy, aicon->icon->core->window);
1091 if (wPreferences.auto_arrange_icons)
1092 wArrangeIcons(dock->screen_ptr, True);
1093 wfree(dock->icon_array);
1094 if (dock->menu && dock->type != WM_CLIP)
1095 wMenuDestroy(dock->menu, True);
1096 if (dock->screen_ptr->last_dock == dock)
1097 dock->screen_ptr->last_dock = NULL;
1098 wfree(dock);
1101 void wClipIconPaint(WAppIcon *aicon)
1103 WScreen *scr = aicon->icon->core->screen_ptr;
1104 WWorkspace *workspace = scr->workspaces[scr->current_workspace];
1105 WMColor *color;
1106 Window win = aicon->icon->core->window;
1107 int length, nlength;
1108 char *ws_name, ws_number[10];
1109 int ty, tx;
1111 wIconPaint(aicon->icon);
1113 length = strlen(workspace->name);
1114 ws_name = wmalloc(length + 1);
1115 snprintf(ws_name, length + 1, "%s", workspace->name);
1116 snprintf(ws_number, sizeof(ws_number), "%i", scr->current_workspace + 1);
1117 nlength = strlen(ws_number);
1119 if (!workspace->clip->collapsed)
1120 color = scr->clip_title_color[CLIP_NORMAL];
1121 else
1122 color = scr->clip_title_color[CLIP_COLLAPSED];
1124 ty = ICON_SIZE - WMFontHeight(scr->clip_title_font) - 3;
1126 tx = CLIP_BUTTON_SIZE * ICON_SIZE / 64;
1128 if(wPreferences.show_clip_title)
1129 WMDrawString(scr->wmscreen, win, color, scr->clip_title_font, tx, ty, ws_name, length);
1131 tx = (ICON_SIZE / 2 - WMWidthOfString(scr->clip_title_font, ws_number, nlength)) / 2;
1133 WMDrawString(scr->wmscreen, win, color, scr->clip_title_font, tx, 2, ws_number, nlength);
1135 wfree(ws_name);
1137 if (aicon->launching)
1138 XFillRectangle(dpy, aicon->icon->core->window, scr->stipple_gc,
1139 0, 0, wPreferences.icon_size, wPreferences.icon_size);
1141 paintClipButtons(aicon, aicon->dock->lclip_button_pushed, aicon->dock->rclip_button_pushed);
1144 static void clipIconExpose(WObjDescriptor *desc, XEvent *event)
1146 wClipIconPaint(desc->parent);
1149 static void dockIconPaint(WAppIcon *btn)
1151 if (btn == btn->icon->core->screen_ptr->clip_icon) {
1152 wClipIconPaint(btn);
1153 } else {
1154 wAppIconPaint(btn);
1155 save_appicon(btn, True);
1159 static WMPropList *make_icon_state(WAppIcon *btn)
1161 WMPropList *node = NULL;
1162 WMPropList *command, *autolaunch, *lock, *name, *forced;
1163 WMPropList *position, *buggy, *omnipresent;
1164 char *tmp;
1165 char buffer[64];
1167 if (btn) {
1168 if (!btn->command)
1169 command = WMCreatePLString("-");
1170 else
1171 command = WMCreatePLString(btn->command);
1173 autolaunch = btn->auto_launch ? dYes : dNo;
1175 lock = btn->lock ? dYes : dNo;
1177 tmp = EscapeWM_CLASS(btn->wm_instance, btn->wm_class);
1179 name = WMCreatePLString(tmp);
1181 wfree(tmp);
1183 forced = btn->forced_dock ? dYes : dNo;
1185 buggy = btn->buggy_app ? dYes : dNo;
1187 if (btn == btn->icon->core->screen_ptr->clip_icon)
1188 snprintf(buffer, sizeof(buffer), "%i,%i", btn->x_pos, btn->y_pos);
1189 else
1190 snprintf(buffer, sizeof(buffer), "%hi,%hi", btn->xindex, btn->yindex);
1191 position = WMCreatePLString(buffer);
1193 node = WMCreatePLDictionary(dCommand, command,
1194 dName, name,
1195 dAutoLaunch, autolaunch,
1196 dLock, lock,
1197 dForced, forced, dBuggyApplication, buggy, dPosition, position, NULL);
1198 WMReleasePropList(command);
1199 WMReleasePropList(name);
1200 WMReleasePropList(position);
1202 omnipresent = btn->omnipresent ? dYes : dNo;
1203 if (btn->dock != btn->icon->core->screen_ptr->dock && (btn->xindex != 0 || btn->yindex != 0))
1204 WMPutInPLDictionary(node, dOmnipresent, omnipresent);
1206 #ifdef XDND /* was OFFIX */
1207 if (btn->dnd_command) {
1208 command = WMCreatePLString(btn->dnd_command);
1209 WMPutInPLDictionary(node, dDropCommand, command);
1210 WMReleasePropList(command);
1212 #endif /* XDND */
1214 if (btn->paste_command) {
1215 command = WMCreatePLString(btn->paste_command);
1216 WMPutInPLDictionary(node, dPasteCommand, command);
1217 WMReleasePropList(command);
1221 return node;
1224 static WMPropList *dockSaveState(WDock *dock)
1226 int i;
1227 WMPropList *icon_info;
1228 WMPropList *list = NULL, *dock_state = NULL;
1229 WMPropList *value, *key;
1230 char buffer[256];
1232 list = WMCreatePLArray(NULL);
1234 for (i = (dock->type == WM_DOCK ? 0 : 1); i < dock->max_icons; i++) {
1235 WAppIcon *btn = dock->icon_array[i];
1237 if (!btn || btn->attracted)
1238 continue;
1240 if ((icon_info = make_icon_state(dock->icon_array[i]))) {
1241 WMAddToPLArray(list, icon_info);
1242 WMReleasePropList(icon_info);
1246 dock_state = WMCreatePLDictionary(dApplications, list, NULL);
1248 if (dock->type == WM_DOCK) {
1249 snprintf(buffer, sizeof(buffer), "Applications%i", dock->screen_ptr->scr_height);
1250 key = WMCreatePLString(buffer);
1251 WMPutInPLDictionary(dock_state, key, list);
1252 WMReleasePropList(key);
1254 snprintf(buffer, sizeof(buffer), "%i,%i", (dock->on_right_side ? -ICON_SIZE : 0), dock->y_pos);
1255 value = WMCreatePLString(buffer);
1256 WMPutInPLDictionary(dock_state, dPosition, value);
1257 WMReleasePropList(value);
1259 WMReleasePropList(list);
1261 value = (dock->lowered ? dYes : dNo);
1262 WMPutInPLDictionary(dock_state, dLowered, value);
1264 if (dock->type == WM_CLIP) {
1265 value = (dock->collapsed ? dYes : dNo);
1266 WMPutInPLDictionary(dock_state, dCollapsed, value);
1268 value = (dock->auto_collapse ? dYes : dNo);
1269 WMPutInPLDictionary(dock_state, dAutoCollapse, value);
1271 value = (dock->auto_raise_lower ? dYes : dNo);
1272 WMPutInPLDictionary(dock_state, dAutoRaiseLower, value);
1274 value = (dock->attract_icons ? dYes : dNo);
1275 WMPutInPLDictionary(dock_state, dAutoAttractIcons, value);
1278 return dock_state;
1281 void wDockSaveState(WScreen *scr, WMPropList *old_state)
1283 WMPropList *dock_state;
1284 WMPropList *keys;
1286 dock_state = dockSaveState(scr->dock);
1289 * Copy saved states of docks with different sizes.
1291 if (old_state) {
1292 int i;
1293 WMPropList *tmp;
1295 keys = WMGetPLDictionaryKeys(old_state);
1296 for (i = 0; i < WMGetPropListItemCount(keys); i++) {
1297 tmp = WMGetFromPLArray(keys, i);
1299 if (strncasecmp(WMGetFromPLString(tmp), "applications", 12) == 0
1300 && !WMGetFromPLDictionary(dock_state, tmp)) {
1302 WMPutInPLDictionary(dock_state, tmp, WMGetFromPLDictionary(old_state, tmp));
1305 WMReleasePropList(keys);
1308 WMPutInPLDictionary(scr->session_state, dDock, dock_state);
1310 WMReleasePropList(dock_state);
1313 void wClipSaveState(WScreen *scr)
1315 WMPropList *clip_state;
1317 clip_state = make_icon_state(scr->clip_icon);
1319 WMPutInPLDictionary(scr->session_state, dClip, clip_state);
1321 WMReleasePropList(clip_state);
1324 WMPropList *wClipSaveWorkspaceState(WScreen *scr, int workspace)
1326 return dockSaveState(scr->workspaces[workspace]->clip);
1329 static Bool getBooleanDockValue(WMPropList *value, WMPropList *key)
1331 if (value) {
1332 if (WMIsPLString(value)) {
1333 if (strcasecmp(WMGetFromPLString(value), "YES") == 0)
1334 return True;
1335 } else {
1336 wwarning(_("bad value in docked icon state info %s"), WMGetFromPLString(key));
1339 return False;
1342 static WAppIcon *restore_icon_state(WScreen *scr, WMPropList *info, int type, int index)
1344 WAppIcon *aicon;
1345 WMPropList *cmd, *value;
1347 cmd = WMGetFromPLDictionary(info, dCommand);
1348 if (!cmd || !WMIsPLString(cmd))
1349 return NULL;
1351 /* parse window name */
1352 value = WMGetFromPLDictionary(info, dName);
1353 if (!value)
1354 return NULL;
1357 char *wclass, *winstance;
1358 char *command;
1360 ParseWindowName(value, &winstance, &wclass, "dock");
1362 if (!winstance && !wclass)
1363 return NULL;
1365 /* get commands */
1367 if (cmd)
1368 command = wstrdup(WMGetFromPLString(cmd));
1369 else
1370 command = NULL;
1372 if (!command || strcmp(command, "-") == 0) {
1373 if (command)
1374 wfree(command);
1375 if (wclass)
1376 wfree(wclass);
1377 if (winstance)
1378 wfree(winstance);
1380 return NULL;
1383 aicon = wAppIconCreateForDock(scr, command, winstance, wclass, TILE_NORMAL);
1384 if (wclass)
1385 wfree(wclass);
1386 if (winstance)
1387 wfree(winstance);
1388 if (command)
1389 wfree(command);
1392 aicon->icon->core->descriptor.handle_mousedown = iconMouseDown;
1393 if (type == WM_CLIP) {
1394 aicon->icon->core->descriptor.handle_enternotify = clipEnterNotify;
1395 aicon->icon->core->descriptor.handle_leavenotify = clipLeaveNotify;
1397 aicon->icon->core->descriptor.parent_type = WCLASS_DOCK_ICON;
1398 aicon->icon->core->descriptor.parent = aicon;
1400 #ifdef XDND /* was OFFIX */
1401 cmd = WMGetFromPLDictionary(info, dDropCommand);
1402 if (cmd)
1403 aicon->dnd_command = wstrdup(WMGetFromPLString(cmd));
1404 #endif
1406 cmd = WMGetFromPLDictionary(info, dPasteCommand);
1407 if (cmd)
1408 aicon->paste_command = wstrdup(WMGetFromPLString(cmd));
1410 /* check auto launch */
1411 value = WMGetFromPLDictionary(info, dAutoLaunch);
1413 aicon->auto_launch = getBooleanDockValue(value, dAutoLaunch);
1415 /* check lock */
1416 value = WMGetFromPLDictionary(info, dLock);
1418 aicon->lock = getBooleanDockValue(value, dLock);
1420 /* check if it wasn't normally docked */
1421 value = WMGetFromPLDictionary(info, dForced);
1423 aicon->forced_dock = getBooleanDockValue(value, dForced);
1425 /* check if we can rely on the stuff in the app */
1426 value = WMGetFromPLDictionary(info, dBuggyApplication);
1428 aicon->buggy_app = getBooleanDockValue(value, dBuggyApplication);
1430 /* get position in the dock */
1431 value = WMGetFromPLDictionary(info, dPosition);
1432 if (value && WMIsPLString(value)) {
1433 if (sscanf(WMGetFromPLString(value), "%hi,%hi", &aicon->xindex, &aicon->yindex) != 2)
1434 wwarning(_("bad value in docked icon state info %s"), WMGetFromPLString(dPosition));
1436 /* check position sanity */
1437 /* incomplete section! */
1438 if (type == WM_DOCK) {
1439 aicon->xindex = 0;
1440 if (aicon->yindex < 0)
1441 wwarning(_("bad value in docked icon position %i,%i"),
1442 aicon->xindex, aicon->yindex);
1444 } else {
1445 aicon->yindex = index;
1446 aicon->xindex = 0;
1449 /* check if icon is omnipresent */
1450 value = WMGetFromPLDictionary(info, dOmnipresent);
1452 aicon->omnipresent = getBooleanDockValue(value, dOmnipresent);
1454 aicon->running = 0;
1455 aicon->docked = 1;
1457 return aicon;
1460 #define COMPLAIN(key) wwarning(_("bad value in dock state info:%s"), key)
1462 WAppIcon *wClipRestoreState(WScreen *scr, WMPropList *clip_state)
1464 WAppIcon *icon;
1465 WMPropList *value;
1467 icon = mainIconCreate(scr, WM_CLIP);
1469 if (!clip_state)
1470 return icon;
1472 WMRetainPropList(clip_state);
1474 /* restore position */
1476 value = WMGetFromPLDictionary(clip_state, dPosition);
1478 if (value) {
1479 if (!WMIsPLString(value)) {
1480 COMPLAIN("Position");
1481 } else {
1482 WMRect rect;
1483 int flags;
1485 if (sscanf(WMGetFromPLString(value), "%i,%i", &icon->x_pos, &icon->y_pos) != 2)
1486 COMPLAIN("Position");
1488 /* check position sanity */
1489 rect.pos.x = icon->x_pos;
1490 rect.pos.y = icon->y_pos;
1491 rect.size.width = rect.size.height = ICON_SIZE;
1493 wGetRectPlacementInfo(scr, rect, &flags);
1494 if (flags & (XFLAG_DEAD | XFLAG_PARTIAL))
1495 wScreenKeepInside(scr, &icon->x_pos, &icon->y_pos, ICON_SIZE, ICON_SIZE);
1498 #ifdef XDND /* was OFFIX */
1499 value = WMGetFromPLDictionary(clip_state, dDropCommand);
1500 if (value && WMIsPLString(value))
1501 icon->dnd_command = wstrdup(WMGetFromPLString(value));
1502 #endif
1504 value = WMGetFromPLDictionary(clip_state, dPasteCommand);
1505 if (value && WMIsPLString(value))
1506 icon->paste_command = wstrdup(WMGetFromPLString(value));
1508 WMReleasePropList(clip_state);
1510 return icon;
1513 WDock *wDockRestoreState(WScreen *scr, WMPropList *dock_state, int type)
1515 WDock *dock;
1516 WMPropList *apps;
1517 WMPropList *value;
1518 WAppIcon *aicon, *old_top;
1519 int count, i;
1521 dock = wDockCreate(scr, type);
1523 if (!dock_state)
1524 return dock;
1526 WMRetainPropList(dock_state);
1528 /* restore position */
1529 value = WMGetFromPLDictionary(dock_state, dPosition);
1530 if (value) {
1531 if (!WMIsPLString(value)) {
1532 COMPLAIN("Position");
1533 } else {
1534 WMRect rect;
1535 int flags;
1537 if (sscanf(WMGetFromPLString(value), "%i,%i", &dock->x_pos, &dock->y_pos) != 2)
1538 COMPLAIN("Position");
1540 /* check position sanity */
1541 rect.pos.x = dock->x_pos;
1542 rect.pos.y = dock->y_pos;
1543 rect.size.width = rect.size.height = ICON_SIZE;
1545 wGetRectPlacementInfo(scr, rect, &flags);
1546 if (flags & (XFLAG_DEAD | XFLAG_PARTIAL)) {
1547 int x = dock->x_pos;
1548 wScreenKeepInside(scr, &x, &dock->y_pos, ICON_SIZE, ICON_SIZE);
1551 /* Is this needed any more? */
1552 if (type == WM_CLIP) {
1553 if (dock->x_pos < 0) {
1554 dock->x_pos = 0;
1555 } else if (dock->x_pos > scr->scr_width - ICON_SIZE) {
1556 dock->x_pos = scr->scr_width - ICON_SIZE;
1558 } else {
1559 if (dock->x_pos >= 0) {
1560 dock->x_pos = DOCK_EXTRA_SPACE;
1561 dock->on_right_side = 0;
1562 } else {
1563 dock->x_pos = scr->scr_width - DOCK_EXTRA_SPACE - ICON_SIZE;
1564 dock->on_right_side = 1;
1570 /* restore lowered/raised state */
1571 dock->lowered = 0;
1573 value = WMGetFromPLDictionary(dock_state, dLowered);
1574 if (value) {
1575 if (!WMIsPLString(value)) {
1576 COMPLAIN("Lowered");
1577 } else {
1578 if (strcasecmp(WMGetFromPLString(value), "YES") == 0)
1579 dock->lowered = 1;
1583 /* restore collapsed state */
1584 dock->collapsed = 0;
1586 value = WMGetFromPLDictionary(dock_state, dCollapsed);
1587 if (value) {
1588 if (!WMIsPLString(value)) {
1589 COMPLAIN("Collapsed");
1590 } else {
1591 if (strcasecmp(WMGetFromPLString(value), "YES") == 0)
1592 dock->collapsed = 1;
1596 /* restore auto-collapsed state */
1597 value = WMGetFromPLDictionary(dock_state, dAutoCollapse);
1598 if (value) {
1599 if (!WMIsPLString(value)) {
1600 COMPLAIN("AutoCollapse");
1601 } else {
1602 if (strcasecmp(WMGetFromPLString(value), "YES") == 0) {
1603 dock->auto_collapse = 1;
1604 dock->collapsed = 1;
1609 /* restore auto-raise/lower state */
1610 value = WMGetFromPLDictionary(dock_state, dAutoRaiseLower);
1611 if (value) {
1612 if (!WMIsPLString(value)) {
1613 COMPLAIN("AutoRaiseLower");
1614 } else {
1615 if (strcasecmp(WMGetFromPLString(value), "YES") == 0)
1616 dock->auto_raise_lower = 1;
1620 /* restore attract icons state */
1621 dock->attract_icons = 0;
1623 value = WMGetFromPLDictionary(dock_state, dAutoAttractIcons);
1624 if (value) {
1625 if (!WMIsPLString(value)) {
1626 COMPLAIN("AutoAttractIcons");
1627 } else {
1628 if (strcasecmp(WMGetFromPLString(value), "YES") == 0)
1629 dock->attract_icons = 1;
1633 /* application list */
1636 WMPropList *tmp;
1637 char buffer[64];
1640 * When saving, it saves the dock state in
1641 * Applications and Applicationsnnn
1643 * When loading, it will first try Applicationsnnn.
1644 * If it does not exist, use Applications as default.
1647 snprintf(buffer, sizeof(buffer), "Applications%i", scr->scr_height);
1649 tmp = WMCreatePLString(buffer);
1650 apps = WMGetFromPLDictionary(dock_state, tmp);
1651 WMReleasePropList(tmp);
1653 if (!apps)
1654 apps = WMGetFromPLDictionary(dock_state, dApplications);
1657 if (!apps)
1658 goto finish;
1660 count = WMGetPropListItemCount(apps);
1661 if (count == 0)
1662 goto finish;
1664 old_top = dock->icon_array[0];
1666 /* dock->icon_count is set to 1 when dock is created.
1667 * Since Clip is already restored, we want to keep it so for clip,
1668 * but for dock we may change the default top tile, so we set it to 0.
1670 if (type == WM_DOCK)
1671 dock->icon_count = 0;
1673 for (i = 0; i < count; i++) {
1674 if (dock->icon_count >= dock->max_icons) {
1675 wwarning(_("there are too many icons stored in dock. Ignoring what doesn't fit"));
1676 break;
1679 value = WMGetFromPLArray(apps, i);
1680 aicon = restore_icon_state(scr, value, type, dock->icon_count);
1682 dock->icon_array[dock->icon_count] = aicon;
1684 if (aicon) {
1685 aicon->dock = dock;
1686 aicon->x_pos = dock->x_pos + (aicon->xindex * ICON_SIZE);
1687 aicon->y_pos = dock->y_pos + (aicon->yindex * ICON_SIZE);
1689 if (dock->lowered)
1690 ChangeStackingLevel(aicon->icon->core, WMNormalLevel);
1691 else
1692 ChangeStackingLevel(aicon->icon->core, WMDockLevel);
1694 wCoreConfigure(aicon->icon->core, aicon->x_pos, aicon->y_pos, 0, 0);
1695 if (!dock->collapsed)
1696 XMapWindow(dpy, aicon->icon->core->window);
1698 wRaiseFrame(aicon->icon->core);
1700 dock->icon_count++;
1701 } else if (dock->icon_count == 0 && type == WM_DOCK) {
1702 dock->icon_count++;
1706 /* if the first icon is not defined, use the default */
1707 if (dock->icon_array[0] == NULL) {
1708 /* update default icon */
1709 old_top->x_pos = dock->x_pos;
1710 old_top->y_pos = dock->y_pos;
1711 if (dock->lowered)
1712 ChangeStackingLevel(old_top->icon->core, WMNormalLevel);
1713 else
1714 ChangeStackingLevel(old_top->icon->core, WMDockLevel);
1716 dock->icon_array[0] = old_top;
1717 XMoveWindow(dpy, old_top->icon->core->window, dock->x_pos, dock->y_pos);
1718 /* we don't need to increment dock->icon_count here because it was
1719 * incremented in the loop above.
1721 } else if (old_top != dock->icon_array[0]) {
1722 if (old_top == scr->clip_icon)
1723 scr->clip_icon = dock->icon_array[0];
1725 wAppIconDestroy(old_top);
1728 finish:
1729 WMReleasePropList(dock_state);
1731 return dock;
1734 void wDockLaunchWithState(WDock *dock, WAppIcon *btn, WSavedState *state)
1736 if (btn && btn->command && !btn->running && !btn->launching) {
1737 btn->drop_launch = 0;
1738 btn->paste_launch = 0;
1740 btn->pid = execCommand(btn, btn->command, state);
1742 if (btn->pid > 0) {
1743 if (!btn->forced_dock && !btn->buggy_app) {
1744 btn->launching = 1;
1745 dockIconPaint(btn);
1748 } else {
1749 wfree(state);
1753 void wDockDoAutoLaunch(WDock *dock, int workspace)
1755 WAppIcon *btn;
1756 WSavedState *state;
1757 int i;
1759 for (i = 0; i < dock->max_icons; i++) {
1760 btn = dock->icon_array[i];
1761 if (!btn || !btn->auto_launch)
1762 continue;
1764 state = wmalloc(sizeof(WSavedState));
1765 state->workspace = workspace;
1766 /* TODO: this is klugy and is very difficult to understand
1767 * what's going on. Try to clean up */
1768 wDockLaunchWithState(dock, btn, state);
1772 #ifdef XDND /* was OFFIX */
1773 static WDock *findDock(WScreen *scr, XEvent *event, int *icon_pos)
1775 WDock *dock;
1776 int i;
1778 *icon_pos = -1;
1779 if ((dock = scr->dock) != NULL) {
1780 for (i = 0; i < dock->max_icons; i++) {
1781 if (dock->icon_array[i]
1782 && dock->icon_array[i]->icon->core->window == event->xclient.window) {
1783 *icon_pos = i;
1784 break;
1788 if (*icon_pos < 0 && (dock = scr->workspaces[scr->current_workspace]->clip) != NULL) {
1789 for (i = 0; i < dock->max_icons; i++) {
1790 if (dock->icon_array[i]
1791 && dock->icon_array[i]->icon->core->window == event->xclient.window) {
1792 *icon_pos = i;
1793 break;
1797 if (*icon_pos >= 0)
1798 return dock;
1799 return NULL;
1802 int wDockReceiveDNDDrop(WScreen *scr, XEvent *event)
1804 WDock *dock;
1805 WAppIcon *btn;
1806 int icon_pos;
1808 dock = findDock(scr, event, &icon_pos);
1809 if (!dock)
1810 return False;
1813 * Return True if the drop was on an application icon window.
1814 * In this case, let the ClientMessage handler redirect the
1815 * message to the app.
1817 if (dock->icon_array[icon_pos]->icon->icon_win != None)
1818 return True;
1820 if (dock->icon_array[icon_pos]->dnd_command != NULL) {
1821 scr->flags.dnd_data_convertion_status = 0;
1823 btn = dock->icon_array[icon_pos];
1825 if (!btn->forced_dock) {
1826 btn->relaunching = btn->running;
1827 btn->running = 1;
1829 if (btn->wm_instance || btn->wm_class) {
1830 WWindowAttributes attr;
1831 memset(&attr, 0, sizeof(WWindowAttributes));
1832 wDefaultFillAttributes(btn->wm_instance, btn->wm_class, &attr, NULL, True);
1834 if (!attr.no_appicon)
1835 btn->launching = 1;
1836 else
1837 btn->running = 0;
1840 btn->paste_launch = 0;
1841 btn->drop_launch = 1;
1842 scr->last_dock = dock;
1843 btn->pid = execCommand(btn, btn->dnd_command, NULL);
1844 if (btn->pid > 0) {
1845 dockIconPaint(btn);
1846 } else {
1847 btn->launching = 0;
1848 if (!btn->relaunching)
1849 btn->running = 0;
1852 return False;
1854 #endif /* XDND */
1856 Bool wDockAttachIcon(WDock *dock, WAppIcon *icon, int x, int y)
1858 WWindow *wwin;
1859 int index;
1861 wwin = icon->icon->owner;
1862 if (icon->command == NULL) {
1863 char *command;
1865 icon->editing = 0;
1867 command = GetCommandForWindow(wwin->client_win);
1868 if (command) {
1869 icon->command = command;
1870 } else {
1871 /* icon->forced_dock = 1; */
1872 if (dock->type != WM_CLIP || !icon->attracted) {
1873 icon->editing = 1;
1874 if (wInputDialog(dock->screen_ptr, _("Dock Icon"),
1875 _("Type the command used to launch the application"), &command)) {
1876 if (command && (command[0] == 0 || (command[0] == '-' && command[1] == 0))) {
1877 wfree(command);
1878 command = NULL;
1880 icon->command = command;
1881 icon->editing = 0;
1882 } else {
1883 icon->editing = 0;
1884 if (command)
1885 wfree(command);
1886 /* If the target is the dock, reject the icon. If
1887 * the target is the clip, make it an attracted icon
1889 if (dock->type == WM_CLIP) {
1890 icon->attracted = 1;
1891 if (!icon->icon->shadowed) {
1892 icon->icon->shadowed = 1;
1893 icon->icon->force_paint = 1;
1895 } else {
1896 return False;
1901 } else {
1902 icon->editing = 0;
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);
1938 wAppIconPaint(icon);
1939 save_appicon(icon, True);
1941 if (wPreferences.auto_arrange_icons)
1942 wArrangeIcons(dock->screen_ptr, True);
1944 #ifdef XDND /* was OFFIX */
1945 if (icon->command && !icon->dnd_command) {
1946 int len = strlen(icon->command) + 8;
1947 icon->dnd_command = wmalloc(len);
1948 snprintf(icon->dnd_command, len, "%s %%d", icon->command);
1950 #endif
1952 if (icon->command && !icon->paste_command) {
1953 int len = strlen(icon->command) + 8;
1954 icon->paste_command = wmalloc(len);
1955 snprintf(icon->paste_command, len, "%s %%s", icon->command);
1958 return True;
1961 static void reattachIcon(WDock *dock, WAppIcon *icon, int x, int y)
1963 int index;
1965 for (index = 1; index < dock->max_icons; index++) {
1966 if (dock->icon_array[index] == icon)
1967 break;
1969 assert(index < dock->max_icons);
1971 icon->yindex = y;
1972 icon->xindex = x;
1974 icon->x_pos = dock->x_pos + x * ICON_SIZE;
1975 icon->y_pos = dock->y_pos + y * ICON_SIZE;
1978 static Bool moveIconBetweenDocks(WDock *src, WDock *dest, WAppIcon *icon, int x, int y)
1980 WWindow *wwin;
1981 char *command;
1982 int index;
1984 if (src == dest)
1985 return True; /* No move needed, we're already there */
1987 if (dest == NULL)
1988 return False;
1990 wwin = icon->icon->owner;
1993 * For the moment we can't do this if we move icons in Clip from one
1994 * workspace to other, because if we move two or more icons without
1995 * command, the dialog box will not be able to tell us to which of the
1996 * moved icons it applies. -Dan
1998 if ((dest->type == WM_DOCK /*|| dest->keep_attracted */ ) && icon->command == NULL) {
1999 command = GetCommandForWindow(wwin->client_win);
2000 if (command) {
2001 icon->command = command;
2002 } else {
2003 icon->editing = 1;
2004 /* icon->forced_dock = 1; */
2005 if (wInputDialog(src->screen_ptr, _("Dock Icon"),
2006 _("Type the command used to launch the application"), &command)) {
2007 if (command && (command[0] == 0 || (command[0] == '-' && command[1] == 0))) {
2008 wfree(command);
2009 command = NULL;
2011 icon->command = command;
2012 } else {
2013 icon->editing = 0;
2014 if (command)
2015 wfree(command);
2016 return False;
2018 icon->editing = 0;
2022 if (dest->type == WM_DOCK)
2023 wClipMakeIconOmnipresent(icon, False);
2025 for (index = 1; index < src->max_icons; index++) {
2026 if (src->icon_array[index] == icon)
2027 break;
2029 assert(index < src->max_icons);
2031 src->icon_array[index] = NULL;
2032 src->icon_count--;
2034 for (index = 1; index < dest->max_icons; index++) {
2035 if (dest->icon_array[index] == NULL)
2036 break;
2039 assert(index < dest->max_icons);
2041 dest->icon_array[index] = icon;
2042 icon->dock = dest;
2044 /* deselect the icon */
2045 if (icon->icon->selected)
2046 wIconSelect(icon->icon);
2048 if (dest->type == WM_DOCK) {
2049 icon->icon->core->descriptor.handle_enternotify = NULL;
2050 icon->icon->core->descriptor.handle_leavenotify = NULL;
2051 } else {
2052 icon->icon->core->descriptor.handle_enternotify = clipEnterNotify;
2053 icon->icon->core->descriptor.handle_leavenotify = clipLeaveNotify;
2056 /* set it to be kept when moving to dock.
2057 * Unless the icon does not have a command set
2059 if (icon->command && dest->type == WM_DOCK) {
2060 icon->attracted = 0;
2061 if (icon->icon->shadowed) {
2062 icon->icon->shadowed = 0;
2063 icon->icon->force_paint = 1;
2065 save_appicon(icon, True);
2068 if (src->auto_collapse || src->auto_raise_lower)
2069 clipLeave(src);
2071 icon->yindex = y;
2072 icon->xindex = x;
2074 icon->x_pos = dest->x_pos + x * ICON_SIZE;
2075 icon->y_pos = dest->y_pos + y * ICON_SIZE;
2077 dest->icon_count++;
2079 MoveInStackListUnder(dest->icon_array[index - 1]->icon->core, icon->icon->core);
2080 wAppIconPaint(icon);
2082 return True;
2085 void wDockDetach(WDock *dock, WAppIcon *icon)
2087 int index;
2089 /* make the settings panel be closed */
2090 if (icon->panel)
2091 DestroyDockAppSettingsPanel(icon->panel);
2093 /* This must be called before icon->dock is set to NULL.
2094 * Don't move it. -Dan
2096 wClipMakeIconOmnipresent(icon, False);
2098 icon->docked = 0;
2099 icon->dock = NULL;
2100 icon->attracted = 0;
2101 icon->auto_launch = 0;
2102 if (icon->icon->shadowed) {
2103 icon->icon->shadowed = 0;
2104 icon->icon->force_paint = 1;
2107 /* deselect the icon */
2108 if (icon->icon->selected)
2109 wIconSelect(icon->icon);
2111 if (icon->command) {
2112 wfree(icon->command);
2113 icon->command = NULL;
2115 #ifdef XDND /* was OFFIX */
2116 if (icon->dnd_command) {
2117 wfree(icon->dnd_command);
2118 icon->dnd_command = NULL;
2120 #endif
2121 if (icon->paste_command) {
2122 wfree(icon->paste_command);
2123 icon->paste_command = NULL;
2126 for (index = 1; index < dock->max_icons; index++)
2127 if (dock->icon_array[index] == icon)
2128 break;
2130 assert(index < dock->max_icons);
2131 dock->icon_array[index] = NULL;
2132 icon->yindex = -1;
2133 icon->xindex = -1;
2135 dock->icon_count--;
2137 /* if the dock is not attached to an application or
2138 * the application did not set the appropriate hints yet,
2139 * destroy the icon */
2140 if (!icon->running || !wApplicationOf(icon->main_window)) {
2141 wAppIconDestroy(icon);
2142 } else {
2143 icon->icon->core->descriptor.handle_mousedown = appIconMouseDown;
2144 icon->icon->core->descriptor.handle_enternotify = NULL;
2145 icon->icon->core->descriptor.handle_leavenotify = NULL;
2146 icon->icon->core->descriptor.parent_type = WCLASS_APPICON;
2147 icon->icon->core->descriptor.parent = icon;
2149 ChangeStackingLevel(icon->icon->core, NORMAL_ICON_LEVEL);
2151 wAppIconPaint(icon);
2152 if (wPreferences.auto_arrange_icons)
2153 wArrangeIcons(dock->screen_ptr, True);
2155 if (dock->auto_collapse || dock->auto_raise_lower)
2156 clipLeave(dock);
2160 * returns the closest Dock slot index for the passed
2161 * coordinates.
2163 * Returns False if icon can't be docked.
2165 * Note: this function should NEVER alter ret_x or ret_y, unless it will
2166 * return True. -Dan
2168 Bool wDockSnapIcon(WDock *dock, WAppIcon *icon, int req_x, int req_y, int *ret_x, int *ret_y, int redocking)
2170 WScreen *scr = dock->screen_ptr;
2171 int dx, dy;
2172 int ex_x, ex_y;
2173 int i, offset = ICON_SIZE / 2;
2174 WAppIcon *aicon = NULL;
2175 WAppIcon *nicon = NULL;
2176 int max_y_icons;
2178 /* TODO: XINERAMA, for these */
2179 max_y_icons = scr->scr_height / ICON_SIZE - 1;
2181 if (wPreferences.flags.noupdates)
2182 return False;
2184 dx = dock->x_pos;
2185 dy = dock->y_pos;
2187 /* if the dock is full */
2188 if (!redocking && (dock->icon_count >= dock->max_icons))
2189 return False;
2191 /* exact position */
2192 if (req_y < dy)
2193 ex_y = (req_y - offset - dy) / ICON_SIZE;
2194 else
2195 ex_y = (req_y + offset - dy) / ICON_SIZE;
2197 if (req_x < dx)
2198 ex_x = (req_x - offset - dx) / ICON_SIZE;
2199 else
2200 ex_x = (req_x + offset - dx) / ICON_SIZE;
2202 /* check if the icon is outside the screen boundaries */
2204 WMRect rect;
2205 int flags;
2207 rect.pos.x = dx + ex_x * ICON_SIZE;
2208 rect.pos.y = dy + ex_y * ICON_SIZE;
2209 rect.size.width = rect.size.height = ICON_SIZE;
2211 wGetRectPlacementInfo(scr, rect, &flags);
2212 if (flags & (XFLAG_DEAD | XFLAG_PARTIAL))
2213 return False;
2216 if (dock->type == WM_DOCK) {
2217 if (icon->dock != dock && ex_x != 0)
2218 return False;
2220 aicon = NULL;
2221 for (i = 0; i < dock->max_icons; i++) {
2222 nicon = dock->icon_array[i];
2223 if (nicon && nicon->yindex == ex_y) {
2224 aicon = nicon;
2225 break;
2229 if (redocking) {
2230 int sig, done, closest;
2232 /* Possible cases when redocking:
2234 * icon dragged out of range of any slot -> false
2235 * icon dragged to range of free slot
2236 * icon dragged to range of same slot
2237 * icon dragged to range of different icon
2239 if (abs(ex_x) > DOCK_DETTACH_THRESHOLD)
2240 return False;
2242 if (ex_y >= 0 && ex_y <= max_y_icons && (aicon == icon || !aicon)) {
2243 *ret_x = 0;
2244 *ret_y = ex_y;
2245 return True;
2248 /* start looking at the upper slot or lower? */
2249 if (ex_y * ICON_SIZE < (req_y + offset - dy))
2250 sig = 1;
2251 else
2252 sig = -1;
2254 closest = -1;
2255 done = 0;
2256 /* look for closest free slot */
2257 for (i = 0; i < (DOCK_DETTACH_THRESHOLD + 1) * 2 && !done; i++) {
2258 int j;
2260 done = 1;
2261 closest = sig * (i / 2) + ex_y;
2262 /* check if this slot is used */
2263 if (closest >= 0) {
2264 for (j = 0; j < dock->max_icons; j++) {
2265 if (dock->icon_array[j]
2266 && dock->icon_array[j]->yindex == closest) {
2267 /* slot is used by someone else */
2268 if (dock->icon_array[j] != icon)
2269 done = 0;
2270 break;
2274 sig = -sig;
2276 if (done && closest >= 0 && closest <= max_y_icons &&
2277 ((ex_y >= closest && ex_y - closest < DOCK_DETTACH_THRESHOLD + 1)
2278 || (ex_y < closest && closest - ex_y <= DOCK_DETTACH_THRESHOLD + 1))) {
2279 *ret_x = 0;
2280 *ret_y = closest;
2281 return True;
2283 } else { /* !redocking */
2285 /* if slot is free and the icon is close enough, return it */
2286 if (!aicon && ex_x == 0 && ex_y >= 0 && ex_y <= max_y_icons) {
2287 *ret_x = 0;
2288 *ret_y = ex_y;
2289 return True;
2292 } else { /* CLIP */
2293 int neighbours = 0;
2294 int start, stop, k;
2296 start = icon->omnipresent ? 0 : scr->current_workspace;
2297 stop = icon->omnipresent ? scr->workspace_count : start + 1;
2299 aicon = NULL;
2300 for (k = start; k < stop; k++) {
2301 WDock *tmp = scr->workspaces[k]->clip;
2302 if (!tmp)
2303 continue;
2304 for (i = 0; i < tmp->max_icons; i++) {
2305 nicon = tmp->icon_array[i];
2306 if (nicon && nicon->xindex == ex_x && nicon->yindex == ex_y) {
2307 aicon = nicon;
2308 break;
2311 if (aicon)
2312 break;
2314 for (k = start; k < stop; k++) {
2315 WDock *tmp = scr->workspaces[k]->clip;
2316 if (!tmp)
2317 continue;
2318 for (i = 0; i < tmp->max_icons; i++) {
2319 nicon = tmp->icon_array[i];
2320 if (nicon && nicon != icon && /* Icon can't be it's own neighbour */
2321 (abs(nicon->xindex - ex_x) <= CLIP_ATTACH_VICINITY &&
2322 abs(nicon->yindex - ex_y) <= CLIP_ATTACH_VICINITY)) {
2323 neighbours = 1;
2324 break;
2327 if (neighbours)
2328 break;
2331 if (neighbours && (aicon == NULL || (redocking && aicon == icon))) {
2332 *ret_x = ex_x;
2333 *ret_y = ex_y;
2334 return True;
2337 return False;
2340 static int onScreen(WScreen *scr, int x, int y, int sx, int ex, int sy, int ey)
2342 WMRect rect = wmkrect(x, y, ICON_SIZE, ICON_SIZE);
2343 int flags;
2345 wGetRectPlacementInfo(scr, rect, &flags);
2347 return !(flags & (XFLAG_DEAD | XFLAG_PARTIAL));
2351 * returns true if it can find a free slot in the dock,
2352 * in which case it changes x_pos and y_pos accordingly.
2353 * Else returns false.
2355 Bool wDockFindFreeSlot(WDock *dock, int *x_pos, int *y_pos)
2357 WScreen *scr = dock->screen_ptr;
2358 WAppIcon *btn;
2359 WAppIconChain *chain;
2360 unsigned char *slot_map;
2361 int mwidth;
2362 int r;
2363 int x, y;
2364 int i, done = False;
2365 int corner;
2366 int sx = 0, sy = 0, ex = scr->scr_width, ey = scr->scr_height;
2367 int extra_count = 0;
2369 if (dock->type == WM_CLIP && dock != scr->workspaces[scr->current_workspace]->clip)
2370 extra_count = scr->global_icon_count;
2372 /* if the dock is full */
2373 if (dock->icon_count + extra_count >= dock->max_icons)
2374 return False;
2376 if (!wPreferences.flags.nodock && scr->dock) {
2377 if (scr->dock->on_right_side)
2378 ex -= ICON_SIZE + DOCK_EXTRA_SPACE;
2379 else
2380 sx += ICON_SIZE + DOCK_EXTRA_SPACE;
2383 if (ex < dock->x_pos)
2384 ex = dock->x_pos;
2385 if (sx > dock->x_pos + ICON_SIZE)
2386 sx = dock->x_pos + ICON_SIZE;
2387 #define C_NONE 0
2388 #define C_NW 1
2389 #define C_NE 2
2390 #define C_SW 3
2391 #define C_SE 4
2393 /* check if clip is in a corner */
2394 if (dock->type == WM_CLIP) {
2395 if (dock->x_pos < 1 && dock->y_pos < 1)
2396 corner = C_NE;
2397 else if (dock->x_pos < 1 && dock->y_pos >= (ey - ICON_SIZE))
2398 corner = C_SE;
2399 else if (dock->x_pos >= (ex - ICON_SIZE) && dock->y_pos >= (ey - ICON_SIZE))
2400 corner = C_SW;
2401 else if (dock->x_pos >= (ex - ICON_SIZE) && dock->y_pos < 1)
2402 corner = C_NW;
2403 else
2404 corner = C_NONE;
2405 } else {
2406 corner = C_NONE;
2409 /* If the clip is in the corner, use only slots that are in the border
2410 * of the screen */
2411 if (corner != C_NONE) {
2412 char *hmap, *vmap;
2413 int hcount, vcount;
2415 hcount = WMIN(dock->max_icons, scr->scr_width / ICON_SIZE);
2416 vcount = WMIN(dock->max_icons, scr->scr_height / ICON_SIZE);
2417 hmap = wmalloc(hcount + 1);
2418 vmap = wmalloc(vcount + 1);
2420 /* mark used positions */
2421 switch (corner) {
2422 case C_NE:
2423 for (i = 0; i < dock->max_icons; i++) {
2424 btn = dock->icon_array[i];
2425 if (!btn)
2426 continue;
2428 if (btn->xindex == 0 && btn->yindex > 0 && btn->yindex < vcount)
2429 vmap[btn->yindex] = 1;
2430 else if (btn->yindex == 0 && btn->xindex > 0 && btn->xindex < hcount)
2431 hmap[btn->xindex] = 1;
2433 for (chain = scr->global_icons; chain != NULL; chain = chain->next) {
2434 btn = chain->aicon;
2435 if (btn->xindex == 0 && btn->yindex > 0 && btn->yindex < vcount)
2436 vmap[btn->yindex] = 1;
2437 else if (btn->yindex == 0 && btn->xindex > 0 && btn->xindex < hcount)
2438 hmap[btn->xindex] = 1;
2440 break;
2441 case C_NW:
2442 for (i = 0; i < dock->max_icons; i++) {
2443 btn = dock->icon_array[i];
2444 if (!btn)
2445 continue;
2447 if (btn->xindex == 0 && btn->yindex > 0 && btn->yindex < vcount)
2448 vmap[btn->yindex] = 1;
2449 else if (btn->yindex == 0 && btn->xindex < 0 && btn->xindex > -hcount)
2450 hmap[-btn->xindex] = 1;
2452 for (chain = scr->global_icons; chain != NULL; chain = chain->next) {
2453 btn = chain->aicon;
2454 if (btn->xindex == 0 && btn->yindex > 0 && btn->yindex < vcount)
2455 vmap[btn->yindex] = 1;
2456 else if (btn->yindex == 0 && btn->xindex < 0 && btn->xindex > -hcount)
2457 hmap[-btn->xindex] = 1;
2459 break;
2460 case C_SE:
2461 for (i = 0; i < dock->max_icons; i++) {
2462 btn = dock->icon_array[i];
2463 if (!btn)
2464 continue;
2466 if (btn->xindex == 0 && btn->yindex < 0 && btn->yindex > -vcount)
2467 vmap[-btn->yindex] = 1;
2468 else if (btn->yindex == 0 && btn->xindex > 0 && btn->xindex < hcount)
2469 hmap[btn->xindex] = 1;
2471 for (chain = scr->global_icons; chain != NULL; chain = chain->next) {
2472 btn = chain->aicon;
2473 if (btn->xindex == 0 && btn->yindex < 0 && btn->yindex > -vcount)
2474 vmap[-btn->yindex] = 1;
2475 else if (btn->yindex == 0 && btn->xindex > 0 && btn->xindex < hcount)
2476 hmap[btn->xindex] = 1;
2478 break;
2479 case C_SW:
2480 default:
2481 for (i = 0; i < dock->max_icons; i++) {
2482 btn = dock->icon_array[i];
2483 if (!btn)
2484 continue;
2486 if (btn->xindex == 0 && btn->yindex < 0 && btn->yindex > -vcount)
2487 vmap[-btn->yindex] = 1;
2488 else if (btn->yindex == 0 && btn->xindex < 0 && btn->xindex > -hcount)
2489 hmap[-btn->xindex] = 1;
2491 for (chain = scr->global_icons; chain != NULL; chain = chain->next) {
2492 btn = chain->aicon;
2493 if (btn->xindex == 0 && btn->yindex < 0 && btn->yindex > -vcount)
2494 vmap[-btn->yindex] = 1;
2495 else if (btn->yindex == 0 && btn->xindex < 0 && btn->xindex > -hcount)
2496 hmap[-btn->xindex] = 1;
2499 x = 0;
2500 y = 0;
2501 done = 0;
2502 /* search a vacant slot */
2503 for (i = 1; i < WMAX(vcount, hcount); i++) {
2504 if (i < vcount && vmap[i] == 0) {
2505 /* found a slot */
2506 x = 0;
2507 y = i;
2508 done = 1;
2509 break;
2510 } else if (i < hcount && hmap[i] == 0) {
2511 /* found a slot */
2512 x = i;
2513 y = 0;
2514 done = 1;
2515 break;
2518 wfree(vmap);
2519 wfree(hmap);
2520 /* If found a slot, translate and return */
2521 if (done) {
2522 if (corner == C_NW || corner == C_NE)
2523 *y_pos = y;
2524 else
2525 *y_pos = -y;
2527 if (corner == C_NE || corner == C_SE)
2528 *x_pos = x;
2529 else
2530 *x_pos = -x;
2532 return True;
2534 /* else, try to find a slot somewhere else */
2537 /* a map of mwidth x mwidth would be enough if we allowed icons to be
2538 * placed outside of screen */
2539 mwidth = (int)ceil(sqrt(dock->max_icons));
2541 /* In the worst case (the clip is in the corner of the screen),
2542 * the amount of icons that fit in the clip is smaller.
2543 * Double the map to get a safe value.
2545 mwidth += mwidth;
2547 r = (mwidth - 1) / 2;
2549 slot_map = wmalloc(mwidth * mwidth);
2551 #define XY2OFS(x,y) (WMAX(abs(x),abs(y)) > r) ? 0 : (((y)+r)*(mwidth)+(x)+r)
2553 /* mark used slots in the map. If the slot falls outside the map
2554 * (for example, when all icons are placed in line), ignore them. */
2555 for (i = 0; i < dock->max_icons; i++) {
2556 btn = dock->icon_array[i];
2557 if (btn)
2558 slot_map[XY2OFS(btn->xindex, btn->yindex)] = 1;
2561 for (chain = scr->global_icons; chain != NULL; chain = chain->next)
2562 slot_map[XY2OFS(chain->aicon->xindex, chain->aicon->yindex)] = 1;
2564 /* Find closest slot from the center that is free by scanning the
2565 * map from the center to outward in circular passes.
2566 * This will not result in a neat layout, but will be optimal
2567 * in the sense that there will not be holes left.
2569 done = 0;
2570 for (i = 1; i <= r && !done; i++) {
2571 int tx, ty;
2573 /* top and bottom parts of the ring */
2574 for (x = -i; x <= i && !done; x++) {
2575 tx = dock->x_pos + x * ICON_SIZE;
2576 y = -i;
2577 ty = dock->y_pos + y * ICON_SIZE;
2578 if (slot_map[XY2OFS(x, y)] == 0 && onScreen(scr, tx, ty, sx, ex, sy, ey)) {
2579 *x_pos = x;
2580 *y_pos = y;
2581 done = 1;
2582 break;
2584 y = i;
2585 ty = dock->y_pos + y * ICON_SIZE;
2586 if (slot_map[XY2OFS(x, y)] == 0 && onScreen(scr, tx, ty, sx, ex, sy, ey)) {
2587 *x_pos = x;
2588 *y_pos = y;
2589 done = 1;
2590 break;
2593 /* left and right parts of the ring */
2594 for (y = -i + 1; y <= i - 1; y++) {
2595 ty = dock->y_pos + y * ICON_SIZE;
2596 x = -i;
2597 tx = dock->x_pos + x * ICON_SIZE;
2598 if (slot_map[XY2OFS(x, y)] == 0 && onScreen(scr, tx, ty, sx, ex, sy, ey)) {
2599 *x_pos = x;
2600 *y_pos = y;
2601 done = 1;
2602 break;
2604 x = i;
2605 tx = dock->x_pos + x * ICON_SIZE;
2606 if (slot_map[XY2OFS(x, y)] == 0 && onScreen(scr, tx, ty, sx, ex, sy, ey)) {
2607 *x_pos = x;
2608 *y_pos = y;
2609 done = 1;
2610 break;
2614 wfree(slot_map);
2615 #undef XY2OFS
2616 return done;
2619 static void moveDock(WDock *dock, int new_x, int new_y)
2621 WAppIcon *btn;
2622 int i;
2624 dock->x_pos = new_x;
2625 dock->y_pos = new_y;
2626 for (i = 0; i < dock->max_icons; i++) {
2627 btn = dock->icon_array[i];
2628 if (btn) {
2629 btn->x_pos = new_x + btn->xindex * ICON_SIZE;
2630 btn->y_pos = new_y + btn->yindex * ICON_SIZE;
2631 XMoveWindow(dpy, btn->icon->core->window, btn->x_pos, btn->y_pos);
2636 static void swapDock(WDock *dock)
2638 WScreen *scr = dock->screen_ptr;
2639 WAppIcon *btn;
2640 int x, i;
2642 if (dock->on_right_side)
2643 x = dock->x_pos = scr->scr_width - ICON_SIZE - DOCK_EXTRA_SPACE;
2644 else
2645 x = dock->x_pos = DOCK_EXTRA_SPACE;
2647 for (i = 0; i < dock->max_icons; i++) {
2648 btn = dock->icon_array[i];
2649 if (btn) {
2650 btn->x_pos = x;
2651 XMoveWindow(dpy, btn->icon->core->window, btn->x_pos, btn->y_pos);
2655 wScreenUpdateUsableArea(scr);
2658 static pid_t execCommand(WAppIcon *btn, char *command, WSavedState *state)
2660 WScreen *scr = btn->icon->core->screen_ptr;
2661 pid_t pid;
2662 char **argv;
2663 int argc;
2664 char *cmdline;
2666 cmdline = ExpandOptions(scr, command);
2668 if (scr->flags.dnd_data_convertion_status || !cmdline) {
2669 if (cmdline)
2670 wfree(cmdline);
2671 if (state)
2672 wfree(state);
2673 return 0;
2676 wtokensplit(cmdline, &argv, &argc);
2678 if (!argc) {
2679 if (cmdline)
2680 wfree(cmdline);
2681 if (state)
2682 wfree(state);
2683 return 0;
2686 if ((pid = fork()) == 0) {
2687 char **args;
2688 int i;
2690 SetupEnvironment(scr);
2692 #ifdef HAVE_SETSID
2693 setsid();
2694 #endif
2696 args = malloc(sizeof(char *) * (argc + 1));
2697 if (!args)
2698 exit(111);
2700 for (i = 0; i < argc; i++)
2701 args[i] = argv[i];
2703 args[argc] = NULL;
2704 execvp(argv[0], args);
2705 exit(111);
2707 wtokenfree(argv, argc);
2709 if (pid > 0) {
2710 if (!state) {
2711 state = wmalloc(sizeof(WSavedState));
2712 state->hidden = -1;
2713 state->miniaturized = -1;
2714 state->shaded = -1;
2715 if (btn->dock == scr->dock || btn->omnipresent)
2716 state->workspace = -1;
2717 else
2718 state->workspace = scr->current_workspace;
2720 wWindowAddSavedState(btn->wm_instance, btn->wm_class, cmdline, pid, state);
2721 wAddDeathHandler(pid, (WDeathHandler *) trackDeadProcess, btn->dock);
2722 } else if (state) {
2723 wfree(state);
2725 wfree(cmdline);
2726 return pid;
2729 void wDockHideIcons(WDock *dock)
2731 int i;
2733 if (dock == NULL)
2734 return;
2736 for (i = 1; i < dock->max_icons; i++) {
2737 if (dock->icon_array[i])
2738 XUnmapWindow(dpy, dock->icon_array[i]->icon->core->window);
2740 dock->mapped = 0;
2742 dockIconPaint(dock->icon_array[0]);
2745 void wDockShowIcons(WDock *dock)
2747 int i, newlevel;
2748 WAppIcon *btn;
2750 if (dock == NULL)
2751 return;
2753 btn = dock->icon_array[0];
2754 moveDock(dock, btn->x_pos, btn->y_pos);
2756 newlevel = dock->lowered ? WMNormalLevel : WMDockLevel;
2757 ChangeStackingLevel(btn->icon->core, newlevel);
2759 for (i = 1; i < dock->max_icons; i++) {
2760 if (dock->icon_array[i]) {
2761 MoveInStackListAbove(dock->icon_array[i]->icon->core, btn->icon->core);
2762 break;
2766 if (!dock->collapsed) {
2767 for (i = 1; i < dock->max_icons; i++) {
2768 if (dock->icon_array[i])
2769 XMapWindow(dpy, dock->icon_array[i]->icon->core->window);
2772 dock->mapped = 1;
2774 dockIconPaint(btn);
2777 void wDockLower(WDock *dock)
2779 int i;
2781 for (i = 0; i < dock->max_icons; i++) {
2782 if (dock->icon_array[i])
2783 wLowerFrame(dock->icon_array[i]->icon->core);
2787 void wDockRaise(WDock *dock)
2789 int i;
2791 for (i = dock->max_icons - 1; i >= 0; i--) {
2792 if (dock->icon_array[i])
2793 wRaiseFrame(dock->icon_array[i]->icon->core);
2797 void wDockRaiseLower(WDock *dock)
2799 if (!dock->icon_array[0]->icon->core->stacking->above
2800 || (dock->icon_array[0]->icon->core->stacking->window_level
2801 != dock->icon_array[0]->icon->core->stacking->above->stacking->window_level))
2802 wDockLower(dock);
2803 else
2804 wDockRaise(dock);
2807 void wDockFinishLaunch(WDock *dock, WAppIcon *icon)
2809 icon->launching = 0;
2810 icon->relaunching = 0;
2811 dockIconPaint(icon);
2814 WAppIcon *wDockFindIconForWindow(WDock *dock, Window window)
2816 WAppIcon *icon;
2817 int i;
2819 for (i = 0; i < dock->max_icons; i++) {
2820 icon = dock->icon_array[i];
2821 if (icon && icon->main_window == window)
2822 return icon;
2824 return NULL;
2827 void wDockTrackWindowLaunch(WDock *dock, Window window)
2829 WAppIcon *icon;
2830 char *wm_class, *wm_instance;
2831 int i;
2832 Bool firstPass = True;
2833 Bool found = False;
2834 char *command = NULL;
2836 if (!PropGetWMClass(window, &wm_class, &wm_instance) || (!wm_class && !wm_instance))
2837 return;
2839 command = GetCommandForWindow(window);
2840 retry:
2841 for (i = 0; i < dock->max_icons; i++) {
2842 icon = dock->icon_array[i];
2843 if (!icon)
2844 continue;
2846 /* app is already attached to icon */
2847 if (icon->main_window == window) {
2848 found = True;
2849 break;
2852 if ((icon->wm_instance || icon->wm_class)
2853 && (icon->launching || !icon->running)) {
2855 if (icon->wm_instance && wm_instance && strcmp(icon->wm_instance, wm_instance) != 0)
2856 continue;
2858 if (icon->wm_class && wm_class && strcmp(icon->wm_class, wm_class) != 0)
2859 continue;
2861 if (firstPass && command && strcmp(icon->command, command) != 0)
2862 continue;
2864 if (!icon->relaunching) {
2865 WApplication *wapp;
2867 /* Possibly an application that was docked with dockit,
2868 * but the user did not update WMState to indicate that
2869 * it was docked by force */
2870 wapp = wApplicationOf(window);
2871 if (!wapp) {
2872 icon->forced_dock = 1;
2873 icon->running = 0;
2875 if (!icon->forced_dock)
2876 icon->main_window = window;
2878 found = True;
2879 if (!wPreferences.no_animations && !icon->launching &&
2880 !dock->screen_ptr->flags.startup && !dock->collapsed) {
2881 WAppIcon *aicon;
2882 int x0, y0;
2884 icon->launching = 1;
2885 dockIconPaint(icon);
2887 aicon = wAppIconCreateForDock(dock->screen_ptr, NULL,
2888 wm_instance, wm_class, TILE_NORMAL);
2889 /* XXX: can: aicon->icon == NULL ? */
2890 PlaceIcon(dock->screen_ptr, &x0, &y0, wGetHeadForWindow(aicon->icon->owner));
2891 wAppIconMove(aicon, x0, y0);
2892 /* Should this always be lowered? -Dan */
2893 if (dock->lowered)
2894 wLowerFrame(aicon->icon->core);
2895 XMapWindow(dpy, aicon->icon->core->window);
2896 aicon->launching = 1;
2897 wAppIconPaint(aicon);
2898 SlideWindow(aicon->icon->core->window, x0, y0, icon->x_pos, icon->y_pos);
2899 XUnmapWindow(dpy, aicon->icon->core->window);
2900 wAppIconDestroy(aicon);
2902 wDockFinishLaunch(dock, icon);
2903 break;
2907 if (firstPass && !found) {
2908 firstPass = False;
2909 goto retry;
2912 if (command)
2913 wfree(command);
2915 if (wm_class)
2916 free(wm_class);
2917 if (wm_instance)
2918 free(wm_instance);
2921 void wClipUpdateForWorkspaceChange(WScreen *scr, int workspace)
2923 if (!wPreferences.flags.noclip) {
2924 scr->clip_icon->dock = scr->workspaces[workspace]->clip;
2925 if (scr->current_workspace != workspace) {
2926 WDock *old_clip = scr->workspaces[scr->current_workspace]->clip;
2927 WAppIconChain *chain = scr->global_icons;
2929 while (chain) {
2930 moveIconBetweenDocks(chain->aicon->dock,
2931 scr->workspaces[workspace]->clip,
2932 chain->aicon, chain->aicon->xindex, chain->aicon->yindex);
2933 if (scr->workspaces[workspace]->clip->collapsed)
2934 XUnmapWindow(dpy, chain->aicon->icon->core->window);
2935 chain = chain->next;
2938 wDockHideIcons(old_clip);
2939 if (old_clip->auto_raise_lower) {
2940 if (old_clip->auto_raise_magic) {
2941 WMDeleteTimerHandler(old_clip->auto_raise_magic);
2942 old_clip->auto_raise_magic = NULL;
2944 wDockLower(old_clip);
2946 if (old_clip->auto_collapse) {
2947 if (old_clip->auto_expand_magic) {
2948 WMDeleteTimerHandler(old_clip->auto_expand_magic);
2949 old_clip->auto_expand_magic = NULL;
2951 old_clip->collapsed = 1;
2953 wDockShowIcons(scr->workspaces[workspace]->clip);
2958 static void trackDeadProcess(pid_t pid, unsigned char status, WDock *dock)
2960 WAppIcon *icon;
2961 int i;
2963 for (i = 0; i < dock->max_icons; i++) {
2964 icon = dock->icon_array[i];
2965 if (!icon)
2966 continue;
2968 if (icon->launching && icon->pid == pid) {
2969 if (!icon->relaunching) {
2970 icon->running = 0;
2971 icon->main_window = None;
2973 wDockFinishLaunch(dock, icon);
2974 icon->pid = 0;
2975 if (status == 111) {
2976 char msg[PATH_MAX];
2977 char *cmd;
2979 #ifdef XDND
2980 if (icon->drop_launch)
2981 cmd = icon->dnd_command;
2982 else
2983 #endif
2984 if (icon->paste_launch)
2985 cmd = icon->paste_command;
2986 else
2987 cmd = icon->command;
2989 snprintf(msg, sizeof(msg), _("Could not execute command \"%s\""), cmd);
2991 wMessageDialog(dock->screen_ptr, _("Error"), msg, _("OK"), NULL, NULL);
2993 break;
2998 static void toggleLowered(WDock *dock)
3000 WAppIcon *tmp;
3001 int newlevel, i;
3003 /* lower/raise Dock */
3004 if (!dock->lowered) {
3005 newlevel = WMNormalLevel;
3006 dock->lowered = 1;
3007 } else {
3008 newlevel = WMDockLevel;
3009 dock->lowered = 0;
3012 for (i = 0; i < dock->max_icons; i++) {
3013 tmp = dock->icon_array[i];
3014 if (!tmp)
3015 continue;
3017 ChangeStackingLevel(tmp->icon->core, newlevel);
3018 if (dock->lowered)
3019 wLowerFrame(tmp->icon->core);
3022 if (dock->type == WM_DOCK)
3023 wScreenUpdateUsableArea(dock->screen_ptr);
3026 static void toggleCollapsed(WDock *dock)
3028 if (dock->collapsed) {
3029 dock->collapsed = 0;
3030 wDockShowIcons(dock);
3031 } else {
3032 dock->collapsed = 1;
3033 wDockHideIcons(dock);
3037 static void openDockMenu(WDock *dock, WAppIcon *aicon, XEvent *event)
3039 WScreen *scr = dock->screen_ptr;
3040 WObjDescriptor *desc;
3041 WMenuEntry *entry;
3042 WApplication *wapp = NULL;
3043 int index = 0;
3044 int x_pos;
3045 int n_selected;
3046 int appIsRunning = aicon->running && aicon->icon && aicon->icon->owner;
3048 if (dock->type == WM_DOCK) {
3049 /* keep on top */
3050 entry = dock->menu->entries[index];
3051 entry->flags.indicator_on = !dock->lowered;
3052 entry->clientdata = dock;
3053 dock->menu->flags.realized = 0;
3054 } else {
3055 /* clip options */
3056 if (scr->clip_options)
3057 updateClipOptionsMenu(scr->clip_options, dock);
3059 n_selected = numberOfSelectedIcons(dock);
3061 /* Rename Workspace */
3062 entry = dock->menu->entries[++index];
3063 if (aicon == scr->clip_icon) {
3064 entry->callback = renameCallback;
3065 entry->clientdata = dock;
3066 entry->flags.indicator = 0;
3067 entry->text = _("Rename Workspace");
3068 } else {
3069 entry->callback = omnipresentCallback;
3070 entry->clientdata = aicon;
3071 if (n_selected > 0) {
3072 entry->flags.indicator = 0;
3073 entry->text = _("Toggle Omnipresent");
3074 } else {
3075 entry->flags.indicator = 1;
3076 entry->flags.indicator_on = aicon->omnipresent;
3077 entry->flags.indicator_type = MI_CHECK;
3078 entry->text = _("Omnipresent");
3082 /* select/unselect icon */
3083 entry = dock->menu->entries[++index];
3084 entry->clientdata = aicon;
3085 entry->flags.indicator_on = aicon->icon->selected;
3086 wMenuSetEnabled(dock->menu, index, aicon != scr->clip_icon);
3088 /* select/unselect all icons */
3089 entry = dock->menu->entries[++index];
3090 entry->clientdata = aicon;
3091 if (n_selected > 0)
3092 entry->text = _("Unselect All Icons");
3093 else
3094 entry->text = _("Select All Icons");
3096 wMenuSetEnabled(dock->menu, index, dock->icon_count > 1);
3098 /* keep icon(s) */
3099 entry = dock->menu->entries[++index];
3100 entry->clientdata = aicon;
3101 if (n_selected > 1)
3102 entry->text = _("Keep Icons");
3103 else
3104 entry->text = _("Keep Icon");
3106 wMenuSetEnabled(dock->menu, index, dock->icon_count > 1);
3108 /* this is the workspace submenu part */
3109 entry = dock->menu->entries[++index];
3110 if (n_selected > 1)
3111 entry->text = _("Move Icons To");
3112 else
3113 entry->text = _("Move Icon To");
3115 if (scr->clip_submenu)
3116 updateWorkspaceMenu(scr->clip_submenu, aicon);
3118 wMenuSetEnabled(dock->menu, index, !aicon->omnipresent);
3120 /* remove icon(s) */
3121 entry = dock->menu->entries[++index];
3122 entry->clientdata = aicon;
3123 if (n_selected > 1)
3124 entry->text = _("Remove Icons");
3125 else
3126 entry->text = _("Remove Icon");
3128 wMenuSetEnabled(dock->menu, index, dock->icon_count > 1);
3130 /* attract icon(s) */
3131 entry = dock->menu->entries[++index];
3132 entry->clientdata = aicon;
3134 dock->menu->flags.realized = 0;
3135 wMenuRealize(dock->menu);
3138 if (aicon->icon->owner)
3139 wapp = wApplicationOf(aicon->icon->owner->main_window);
3140 else
3141 wapp = NULL;
3143 /* launch */
3144 entry = dock->menu->entries[++index];
3145 entry->clientdata = aicon;
3146 wMenuSetEnabled(dock->menu, index, aicon->command != NULL);
3148 /* unhide here */
3149 entry = dock->menu->entries[++index];
3150 entry->clientdata = aicon;
3151 if (wapp && wapp->flags.hidden)
3152 entry->text = _("Unhide Here");
3153 else
3154 entry->text = _("Bring Here");
3156 wMenuSetEnabled(dock->menu, index, appIsRunning);
3158 /* hide */
3159 entry = dock->menu->entries[++index];
3160 entry->clientdata = aicon;
3161 if (wapp && wapp->flags.hidden)
3162 entry->text = _("Unhide");
3163 else
3164 entry->text = _("Hide");
3166 wMenuSetEnabled(dock->menu, index, appIsRunning);
3168 /* settings */
3169 entry = dock->menu->entries[++index];
3170 entry->clientdata = aicon;
3171 wMenuSetEnabled(dock->menu, index, !aicon->editing && !wPreferences.flags.noupdates);
3173 /* kill */
3174 entry = dock->menu->entries[++index];
3175 entry->clientdata = aicon;
3176 wMenuSetEnabled(dock->menu, index, appIsRunning);
3178 if (!dock->menu->flags.realized)
3179 wMenuRealize(dock->menu);
3181 if (dock->type == WM_CLIP) {
3182 /*x_pos = event->xbutton.x_root+2; */
3183 x_pos = event->xbutton.x_root - dock->menu->frame->core->width / 2 - 1;
3184 if (x_pos < 0) {
3185 x_pos = 0;
3186 } else if (x_pos + dock->menu->frame->core->width > scr->scr_width - 2) {
3187 x_pos = scr->scr_width - dock->menu->frame->core->width - 4;
3189 } else {
3190 x_pos = dock->on_right_side ? scr->scr_width - dock->menu->frame->core->width - 3 : 0;
3193 wMenuMapAt(dock->menu, x_pos, event->xbutton.y_root + 2, False);
3195 /* allow drag select */
3196 event->xany.send_event = True;
3197 desc = &dock->menu->menu->descriptor;
3198 (*desc->handle_mousedown) (desc, event);
3201 /******************************************************************/
3202 static void iconDblClick(WObjDescriptor *desc, XEvent *event)
3204 WAppIcon *btn = desc->parent;
3205 WDock *dock = btn->dock;
3206 WApplication *wapp = NULL;
3207 int unhideHere = 0;
3209 if (btn->icon->owner && !(event->xbutton.state & ControlMask)) {
3210 wapp = wApplicationOf(btn->icon->owner->main_window);
3212 assert(wapp != NULL);
3214 unhideHere = (event->xbutton.state & ShiftMask);
3216 /* go to the last workspace that the user worked on the app */
3217 if (wapp->last_workspace != dock->screen_ptr->current_workspace && !unhideHere)
3218 wWorkspaceChange(dock->screen_ptr, wapp->last_workspace);
3220 wUnhideApplication(wapp, event->xbutton.button == Button2, unhideHere);
3222 if (event->xbutton.state & MOD_MASK)
3223 wHideOtherApplications(btn->icon->owner);
3224 } else {
3225 if (event->xbutton.button == Button1) {
3226 if (event->xbutton.state & MOD_MASK) {
3227 /* raise/lower dock */
3228 toggleLowered(dock);
3229 } else if (btn == dock->screen_ptr->clip_icon) {
3230 if (getClipButton(event->xbutton.x, event->xbutton.y) == CLIP_IDLE)
3231 toggleCollapsed(dock);
3232 else
3233 handleClipChangeWorkspace(dock->screen_ptr, event);
3234 } else if (btn->command) {
3235 if (!btn->launching && (!btn->running || (event->xbutton.state & ControlMask)))
3236 launchDockedApplication(btn, False);
3242 static void handleDockMove(WDock *dock, WAppIcon *aicon, XEvent *event)
3244 WScreen *scr = dock->screen_ptr;
3245 int ofs_x = event->xbutton.x, ofs_y = event->xbutton.y;
3246 int x, y;
3247 XEvent ev;
3248 int grabbed = 0, swapped = 0, done;
3249 Pixmap ghost = None;
3250 int superfluous = wPreferences.superfluous; /* we catch it to avoid problems */
3252 if (XGrabPointer(dpy, aicon->icon->core->window, True, ButtonMotionMask
3253 | ButtonReleaseMask | ButtonPressMask, GrabModeAsync,
3254 GrabModeAsync, None, None, CurrentTime) != GrabSuccess)
3255 wwarning("pointer grab failed for dock move");
3257 y = 0;
3258 for (x = 0; x < dock->max_icons; x++) {
3259 if (dock->icon_array[x] != NULL && dock->icon_array[x]->yindex > y)
3260 y = dock->icon_array[x]->yindex;
3262 y++;
3263 XResizeWindow(dpy, scr->dock_shadow, ICON_SIZE, ICON_SIZE * y);
3265 done = 0;
3266 while (!done) {
3267 WMMaskEvent(dpy, PointerMotionMask | ButtonReleaseMask | ButtonPressMask
3268 | ButtonMotionMask | ExposureMask | EnterWindowMask, &ev);
3269 switch (ev.type) {
3270 case Expose:
3271 WMHandleEvent(&ev);
3272 break;
3274 case EnterNotify:
3275 /* It means the cursor moved so fast that it entered
3276 * something else (if moving slowly, it would have
3277 * stayed in the dock that is being moved. Ignore such
3278 * "spurious" EnterNotifiy's */
3279 break;
3281 case MotionNotify:
3282 if (!grabbed) {
3283 if (abs(ofs_x - ev.xmotion.x) >= MOVE_THRESHOLD
3284 || abs(ofs_y - ev.xmotion.y) >= MOVE_THRESHOLD) {
3285 XChangeActivePointerGrab(dpy, ButtonMotionMask
3286 | ButtonReleaseMask | ButtonPressMask,
3287 wCursor[WCUR_MOVE], CurrentTime);
3288 grabbed = 1;
3290 break;
3292 if (dock->type == WM_CLIP) {
3293 x = ev.xmotion.x_root - ofs_x;
3294 y = ev.xmotion.y_root - ofs_y;
3295 wScreenKeepInside(scr, &x, &y, ICON_SIZE, ICON_SIZE);
3297 moveDock(dock, x, y);
3298 } else {
3299 /* move vertically if pointer is inside the dock */
3300 if ((dock->on_right_side && ev.xmotion.x_root >= dock->x_pos - ICON_SIZE)
3301 || (!dock->on_right_side && ev.xmotion.x_root <= dock->x_pos + ICON_SIZE * 2)) {
3303 x = ev.xmotion.x_root - ofs_x;
3304 y = ev.xmotion.y_root - ofs_y;
3305 wScreenKeepInside(scr, &x, &y, ICON_SIZE, ICON_SIZE);
3306 moveDock(dock, dock->x_pos, y);
3308 /* move horizontally to change sides */
3309 x = ev.xmotion.x_root - ofs_x;
3310 if (!dock->on_right_side) {
3312 /* is on left */
3313 if (ev.xmotion.x_root > dock->x_pos + ICON_SIZE * 2) {
3314 XMoveWindow(dpy, scr->dock_shadow, scr->scr_width - ICON_SIZE
3315 - DOCK_EXTRA_SPACE - 1, dock->y_pos);
3316 if (superfluous && ghost == None) {
3317 ghost = MakeGhostDock(dock, dock->x_pos,
3318 scr->scr_width - ICON_SIZE
3319 - DOCK_EXTRA_SPACE - 1, dock->y_pos);
3320 XSetWindowBackgroundPixmap(dpy, scr->dock_shadow, ghost);
3321 XClearWindow(dpy, scr->dock_shadow);
3323 XMapRaised(dpy, scr->dock_shadow);
3324 swapped = 1;
3325 } else {
3326 if (superfluous && ghost != None) {
3327 XFreePixmap(dpy, ghost);
3328 ghost = None;
3330 XUnmapWindow(dpy, scr->dock_shadow);
3331 swapped = 0;
3333 } else {
3334 /* is on right */
3335 if (ev.xmotion.x_root < dock->x_pos - ICON_SIZE) {
3336 XMoveWindow(dpy, scr->dock_shadow, DOCK_EXTRA_SPACE, dock->y_pos);
3337 if (superfluous && ghost == None) {
3338 ghost = MakeGhostDock(dock, dock->x_pos,
3339 DOCK_EXTRA_SPACE, dock->y_pos);
3340 XSetWindowBackgroundPixmap(dpy, scr->dock_shadow, ghost);
3341 XClearWindow(dpy, scr->dock_shadow);
3343 XMapRaised(dpy, scr->dock_shadow);
3344 swapped = -1;
3345 } else {
3346 XUnmapWindow(dpy, scr->dock_shadow);
3347 swapped = 0;
3348 if (superfluous && ghost != None) {
3349 XFreePixmap(dpy, ghost);
3350 ghost = None;
3355 break;
3357 case ButtonPress:
3358 break;
3360 case ButtonRelease:
3361 if (ev.xbutton.button != event->xbutton.button)
3362 break;
3363 XUngrabPointer(dpy, CurrentTime);
3364 XUnmapWindow(dpy, scr->dock_shadow);
3365 XResizeWindow(dpy, scr->dock_shadow, ICON_SIZE, ICON_SIZE);
3366 if (dock->type == WM_DOCK) {
3367 if (swapped != 0) {
3368 if (swapped > 0)
3369 dock->on_right_side = 1;
3370 else
3371 dock->on_right_side = 0;
3372 swapDock(dock);
3373 wArrangeIcons(scr, False);
3376 done = 1;
3377 break;
3380 if (superfluous) {
3381 if (ghost != None)
3382 XFreePixmap(dpy, ghost);
3383 XSetWindowBackground(dpy, scr->dock_shadow, scr->white_pixel);
3387 static Bool handleIconMove(WDock *dock, WAppIcon *aicon, XEvent *event)
3389 WScreen *scr = dock->screen_ptr;
3390 Window wins[2];
3391 WIcon *icon = aicon->icon;
3392 WDock *dock2 = NULL, *last_dock = dock, *clip = NULL;
3393 int ondock, grabbed = 0, change_dock = 0, collapsed = 0;
3394 XEvent ev;
3395 int x = aicon->x_pos, y = aicon->y_pos;
3396 int ofs_x = event->xbutton.x, ofs_y = event->xbutton.y;
3397 int shad_x = x, shad_y = y;
3398 int ix = aicon->xindex, iy = aicon->yindex;
3399 int tmp;
3400 Pixmap ghost = None;
3401 Bool docked;
3402 int superfluous = wPreferences.superfluous; /* we catch it to avoid problems */
3403 int omnipresent = aicon->omnipresent; /* this must be cached!!! */
3404 Bool hasMoved = False;
3406 if (wPreferences.flags.noupdates)
3407 return hasMoved;
3409 if (XGrabPointer(dpy, icon->core->window, True, ButtonMotionMask
3410 | ButtonReleaseMask | ButtonPressMask, GrabModeAsync,
3411 GrabModeAsync, None, None, CurrentTime) != GrabSuccess) {
3414 if (!(event->xbutton.state & MOD_MASK))
3415 wRaiseFrame(icon->core);
3417 if (!wPreferences.flags.noclip)
3418 clip = scr->workspaces[scr->current_workspace]->clip;
3420 if (dock == scr->dock && !wPreferences.flags.noclip)
3421 dock2 = clip;
3422 else if (dock != scr->dock && !wPreferences.flags.nodock)
3423 dock2 = scr->dock;
3425 wins[0] = icon->core->window;
3426 wins[1] = scr->dock_shadow;
3427 XRestackWindows(dpy, wins, 2);
3428 XMoveResizeWindow(dpy, scr->dock_shadow, aicon->x_pos, aicon->y_pos, ICON_SIZE, ICON_SIZE);
3429 if (superfluous) {
3430 if (icon->pixmap != None)
3431 ghost = MakeGhostIcon(scr, icon->pixmap);
3432 else
3433 ghost = MakeGhostIcon(scr, icon->core->window);
3435 XSetWindowBackgroundPixmap(dpy, scr->dock_shadow, ghost);
3436 XClearWindow(dpy, scr->dock_shadow);
3438 XMapWindow(dpy, scr->dock_shadow);
3440 ondock = 1;
3442 while (1) {
3443 XMaskEvent(dpy, PointerMotionMask | ButtonReleaseMask | ButtonPressMask
3444 | ButtonMotionMask | ExposureMask, &ev);
3445 switch (ev.type) {
3446 case Expose:
3447 WMHandleEvent(&ev);
3448 break;
3450 case MotionNotify:
3451 hasMoved = True;
3452 if (!grabbed) {
3453 if (abs(ofs_x - ev.xmotion.x) >= MOVE_THRESHOLD
3454 || abs(ofs_y - ev.xmotion.y) >= MOVE_THRESHOLD) {
3455 XChangeActivePointerGrab(dpy, ButtonMotionMask
3456 | ButtonReleaseMask | ButtonPressMask,
3457 wCursor[WCUR_MOVE], CurrentTime);
3458 grabbed = 1;
3459 } else {
3460 break;
3464 if (omnipresent) {
3465 int i;
3466 for (i = 0; i < scr->workspace_count; i++) {
3467 if (i == scr->current_workspace)
3468 continue;
3469 wDockShowIcons(scr->workspaces[i]->clip);
3473 x = ev.xmotion.x_root - ofs_x;
3474 y = ev.xmotion.y_root - ofs_y;
3475 tmp = wDockSnapIcon(dock, aicon, x, y, &ix, &iy, True);
3476 if (tmp && dock2) {
3477 change_dock = 0;
3478 if (last_dock != dock && collapsed) {
3479 last_dock->collapsed = 1;
3480 wDockHideIcons(last_dock);
3481 collapsed = 0;
3483 if (!collapsed && (collapsed = dock->collapsed)) {
3484 dock->collapsed = 0;
3485 wDockShowIcons(dock);
3487 if (dock->auto_raise_lower)
3488 wDockRaise(dock);
3489 last_dock = dock;
3490 } else if (dock2) {
3491 tmp = wDockSnapIcon(dock2, aicon, x, y, &ix, &iy, False);
3492 if (tmp) {
3493 change_dock = 1;
3494 if (last_dock != dock2 && collapsed) {
3495 last_dock->collapsed = 1;
3496 wDockHideIcons(last_dock);
3497 collapsed = 0;
3499 if (!collapsed && (collapsed = dock2->collapsed)) {
3500 dock2->collapsed = 0;
3501 wDockShowIcons(dock2);
3503 if (dock2->auto_raise_lower)
3504 wDockRaise(dock2);
3505 last_dock = dock2;
3508 if (aicon->launching || aicon->lock || (aicon->running && !(ev.xmotion.state & MOD_MASK))
3509 || (!aicon->running && tmp)) {
3510 shad_x = last_dock->x_pos + ix * wPreferences.icon_size;
3511 shad_y = last_dock->y_pos + iy * wPreferences.icon_size;
3513 XMoveWindow(dpy, scr->dock_shadow, shad_x, shad_y);
3515 if (!ondock)
3516 XMapWindow(dpy, scr->dock_shadow);
3518 ondock = 1;
3519 } else {
3520 if (ondock)
3521 XUnmapWindow(dpy, scr->dock_shadow);
3523 ondock = 0;
3525 XMoveWindow(dpy, icon->core->window, x, y);
3526 break;
3528 case ButtonPress:
3529 break;
3531 case ButtonRelease:
3532 if (ev.xbutton.button != event->xbutton.button)
3533 break;
3534 XUngrabPointer(dpy, CurrentTime);
3535 if (ondock) {
3536 SlideWindow(icon->core->window, x, y, shad_x, shad_y);
3537 XUnmapWindow(dpy, scr->dock_shadow);
3538 if (!change_dock) {
3539 reattachIcon(dock, aicon, ix, iy);
3540 if (clip && dock != clip && clip->auto_raise_lower)
3541 wDockLower(clip);
3542 } else {
3543 docked = moveIconBetweenDocks(dock, dock2, aicon, ix, iy);
3544 if (!docked) {
3545 /* Slide it back if dock rejected it */
3546 SlideWindow(icon->core->window, x, y, aicon->x_pos, aicon->y_pos);
3547 reattachIcon(dock, aicon, aicon->xindex, aicon->yindex);
3549 if (last_dock->type == WM_CLIP && last_dock->auto_collapse)
3550 collapsed = 0;
3552 } else {
3553 aicon->x_pos = x;
3554 aicon->y_pos = y;
3555 if (superfluous) {
3556 if (!aicon->running && !wPreferences.no_animations) {
3557 /* We need to deselect it, even if is deselected in
3558 * wDockDetach(), because else DoKaboom() will fail.
3560 if (aicon->icon->selected)
3561 wIconSelect(aicon->icon);
3563 DoKaboom(scr, aicon->icon->core->window, x, y);
3566 if (clip && clip->auto_raise_lower)
3567 wDockLower(clip);
3568 wDockDetach(dock, aicon);
3570 if (collapsed) {
3571 last_dock->collapsed = 1;
3572 wDockHideIcons(last_dock);
3573 collapsed = 0;
3575 if (superfluous) {
3576 if (ghost != None)
3577 XFreePixmap(dpy, ghost);
3578 XSetWindowBackground(dpy, scr->dock_shadow, scr->white_pixel);
3580 if (omnipresent) {
3581 int i;
3582 for (i = 0; i < scr->workspace_count; i++) {
3583 if (i == scr->current_workspace)
3584 continue;
3585 wDockHideIcons(scr->workspaces[i]->clip);
3588 return hasMoved;;
3591 return False; /* never reached */
3594 static int getClipButton(int px, int py)
3596 int pt = (CLIP_BUTTON_SIZE + 2) * ICON_SIZE / 64;
3598 if (px < 0 || py < 0 || px >= ICON_SIZE || py >= ICON_SIZE)
3599 return CLIP_IDLE;
3601 if (py <= pt - ((int)ICON_SIZE - 1 - px))
3602 return CLIP_FORWARD;
3603 else if (px <= pt - ((int)ICON_SIZE - 1 - py))
3604 return CLIP_REWIND;
3606 return CLIP_IDLE;
3609 static void handleClipChangeWorkspace(WScreen *scr, XEvent *event)
3611 XEvent ev;
3612 int done, direction, new_ws;
3613 int new_dir;
3614 WDock *clip = scr->clip_icon->dock;
3616 direction = getClipButton(event->xbutton.x, event->xbutton.y);
3618 clip->lclip_button_pushed = direction == CLIP_REWIND;
3619 clip->rclip_button_pushed = direction == CLIP_FORWARD;
3621 wClipIconPaint(scr->clip_icon);
3622 done = 0;
3623 while (!done) {
3624 WMMaskEvent(dpy, ExposureMask | ButtonMotionMask | ButtonReleaseMask | ButtonPressMask, &ev);
3625 switch (ev.type) {
3626 case Expose:
3627 WMHandleEvent(&ev);
3628 break;
3630 case MotionNotify:
3631 new_dir = getClipButton(ev.xmotion.x, ev.xmotion.y);
3632 if (new_dir != direction) {
3633 direction = new_dir;
3634 clip->lclip_button_pushed = direction == CLIP_REWIND;
3635 clip->rclip_button_pushed = direction == CLIP_FORWARD;
3636 wClipIconPaint(scr->clip_icon);
3638 break;
3640 case ButtonPress:
3641 break;
3643 case ButtonRelease:
3644 if (ev.xbutton.button == event->xbutton.button)
3645 done = 1;
3649 clip->lclip_button_pushed = 0;
3650 clip->rclip_button_pushed = 0;
3652 new_ws = wPreferences.ws_advance || (event->xbutton.state & ControlMask);
3654 if (direction == CLIP_FORWARD) {
3655 if (scr->current_workspace < scr->workspace_count - 1)
3656 wWorkspaceChange(scr, scr->current_workspace + 1);
3657 else if (new_ws && scr->current_workspace < MAX_WORKSPACES - 1)
3658 wWorkspaceChange(scr, scr->current_workspace + 1);
3659 else if (wPreferences.ws_cycle)
3660 wWorkspaceChange(scr, 0);
3661 } else if (direction == CLIP_REWIND) {
3662 if (scr->current_workspace > 0)
3663 wWorkspaceChange(scr, scr->current_workspace - 1);
3664 else if (scr->current_workspace == 0 && wPreferences.ws_cycle)
3665 wWorkspaceChange(scr, scr->workspace_count - 1);
3668 wClipIconPaint(scr->clip_icon);
3671 static void iconMouseDown(WObjDescriptor *desc, XEvent *event)
3673 WAppIcon *aicon = desc->parent;
3674 WDock *dock = aicon->dock;
3675 WScreen *scr = aicon->icon->core->screen_ptr;
3677 if (aicon->editing || WCHECK_STATE(WSTATE_MODAL))
3678 return;
3680 scr->last_dock = dock;
3682 if (dock->menu->flags.mapped)
3683 wMenuUnmap(dock->menu);
3685 if (IsDoubleClick(scr, event)) {
3686 /* double-click was not in the main clip icon */
3687 if (dock->type != WM_CLIP || aicon->xindex != 0 || aicon->yindex != 0
3688 || getClipButton(event->xbutton.x, event->xbutton.y) == CLIP_IDLE) {
3689 iconDblClick(desc, event);
3690 return;
3694 if (event->xbutton.button == Button1) {
3695 if (event->xbutton.state & MOD_MASK)
3696 wDockLower(dock);
3697 else
3698 wDockRaise(dock);
3700 if ((event->xbutton.state & ShiftMask) && aicon != scr->clip_icon && dock->type != WM_DOCK) {
3701 wIconSelect(aicon->icon);
3702 return;
3705 if (aicon->yindex == 0 && aicon->xindex == 0) {
3706 if (getClipButton(event->xbutton.x, event->xbutton.y) != CLIP_IDLE
3707 && dock->type == WM_CLIP)
3708 handleClipChangeWorkspace(scr, event);
3709 else
3710 handleDockMove(dock, aicon, event);
3711 } else {
3712 Bool hasMoved = handleIconMove(dock, aicon, event);
3713 if (wPreferences.single_click && !hasMoved)
3714 iconDblClick(desc, event);
3716 } else if (event->xbutton.button == Button2 && dock->type == WM_CLIP && aicon == scr->clip_icon) {
3717 if (!scr->clip_ws_menu) {
3718 scr->clip_ws_menu = wWorkspaceMenuMake(scr, False);
3720 if (scr->clip_ws_menu) {
3721 WMenu *wsMenu = scr->clip_ws_menu;
3722 int xpos;
3724 wWorkspaceMenuUpdate(scr, wsMenu);
3726 xpos = event->xbutton.x_root - wsMenu->frame->core->width / 2 - 1;
3727 if (xpos < 0) {
3728 xpos = 0;
3729 } else if (xpos + wsMenu->frame->core->width > scr->scr_width - 2) {
3730 xpos = scr->scr_width - wsMenu->frame->core->width - 4;
3732 wMenuMapAt(wsMenu, xpos, event->xbutton.y_root + 2, False);
3734 desc = &wsMenu->menu->descriptor;
3735 event->xany.send_event = True;
3736 (*desc->handle_mousedown) (desc, event);
3738 } else if (event->xbutton.button == Button2 && dock->type == WM_CLIP &&
3739 (event->xbutton.state & ShiftMask) && aicon != scr->clip_icon) {
3740 wClipMakeIconOmnipresent(aicon, !aicon->omnipresent);
3741 } else if (event->xbutton.button == Button3) {
3742 if (event->xbutton.send_event &&
3743 XGrabPointer(dpy, aicon->icon->core->window, True, ButtonMotionMask
3744 | ButtonReleaseMask | ButtonPressMask, GrabModeAsync,
3745 GrabModeAsync, None, None, CurrentTime) != GrabSuccess) {
3746 wwarning("pointer grab failed for dockicon menu");
3747 return;
3750 openDockMenu(dock, aicon, event);
3751 } else if (event->xbutton.button == Button2) {
3752 WAppIcon *btn = desc->parent;
3754 if (!btn->launching && (!btn->running || (event->xbutton.state & ControlMask)))
3755 launchDockedApplication(btn, True);
3759 static void clipEnterNotify(WObjDescriptor *desc, XEvent *event)
3761 WAppIcon *btn = (WAppIcon *) desc->parent;
3762 WDock *dock;
3763 WScreen *scr;
3765 assert(event->type == EnterNotify);
3767 if (desc->parent_type != WCLASS_DOCK_ICON)
3768 return;
3770 scr = btn->icon->core->screen_ptr;
3771 if (!btn->omnipresent)
3772 dock = btn->dock;
3773 else
3774 dock = scr->workspaces[scr->current_workspace]->clip;
3776 if (!dock || dock->type != WM_CLIP)
3777 return;
3779 /* The auto raise/lower code */
3780 if (dock->auto_lower_magic) {
3781 WMDeleteTimerHandler(dock->auto_lower_magic);
3782 dock->auto_lower_magic = NULL;
3784 if (dock->auto_raise_lower && !dock->auto_raise_magic)
3785 dock->auto_raise_magic = WMAddTimerHandler(AUTO_RAISE_DELAY, clipAutoRaise, (void *)dock);
3787 /* The auto expand/collapse code */
3788 if (dock->auto_collapse_magic) {
3789 WMDeleteTimerHandler(dock->auto_collapse_magic);
3790 dock->auto_collapse_magic = NULL;
3792 if (dock->auto_collapse && !dock->auto_expand_magic)
3793 dock->auto_expand_magic = WMAddTimerHandler(AUTO_EXPAND_DELAY, clipAutoExpand, (void *)dock);
3796 static void clipLeave(WDock *dock)
3798 XEvent event;
3799 WObjDescriptor *desc = NULL;
3801 if (!dock || dock->type != WM_CLIP)
3802 return;
3804 if (XCheckTypedEvent(dpy, EnterNotify, &event) != False) {
3805 if (XFindContext(dpy, event.xcrossing.window, wWinContext,
3806 (XPointer *) & desc) != XCNOENT
3807 && desc && desc->parent_type == WCLASS_DOCK_ICON
3808 && ((WAppIcon *) desc->parent)->dock && ((WAppIcon *) desc->parent)->dock->type == WM_CLIP) {
3809 /* We didn't left the Clip yet */
3810 XPutBackEvent(dpy, &event);
3811 return;
3814 XPutBackEvent(dpy, &event);
3815 } else {
3816 /* We entered a withdrawn window, so we're still in Clip */
3817 return;
3820 if (dock->auto_raise_magic) {
3821 WMDeleteTimerHandler(dock->auto_raise_magic);
3822 dock->auto_raise_magic = NULL;
3824 if (dock->auto_raise_lower && !dock->auto_lower_magic)
3825 dock->auto_lower_magic = WMAddTimerHandler(AUTO_LOWER_DELAY, clipAutoLower, (void *)dock);
3827 if (dock->auto_expand_magic) {
3828 WMDeleteTimerHandler(dock->auto_expand_magic);
3829 dock->auto_expand_magic = NULL;
3831 if (dock->auto_collapse && !dock->auto_collapse_magic)
3832 dock->auto_collapse_magic = WMAddTimerHandler(AUTO_COLLAPSE_DELAY, clipAutoCollapse, (void *)dock);
3835 static void clipLeaveNotify(WObjDescriptor *desc, XEvent *event)
3837 WAppIcon *btn = (WAppIcon *) desc->parent;
3839 assert(event->type == LeaveNotify);
3841 if (desc->parent_type != WCLASS_DOCK_ICON)
3842 return;
3844 clipLeave(btn->dock);
3847 static void clipAutoCollapse(void *cdata)
3849 WDock *dock = (WDock *) cdata;
3851 if (dock->type != WM_CLIP)
3852 return;
3854 if (dock->auto_collapse) {
3855 dock->collapsed = 1;
3856 wDockHideIcons(dock);
3858 dock->auto_collapse_magic = NULL;
3861 static void clipAutoExpand(void *cdata)
3863 WDock *dock = (WDock *) cdata;
3865 if (dock->type != WM_CLIP)
3866 return;
3868 if (dock->auto_collapse) {
3869 dock->collapsed = 0;
3870 wDockShowIcons(dock);
3872 dock->auto_expand_magic = NULL;
3875 static void clipAutoLower(void *cdata)
3877 WDock *dock = (WDock *) cdata;
3879 if (dock->type != WM_CLIP)
3880 return;
3882 if (dock->auto_raise_lower)
3883 wDockLower(dock);
3885 dock->auto_lower_magic = NULL;
3888 static void clipAutoRaise(void *cdata)
3890 WDock *dock = (WDock *) cdata;
3892 if (dock->type != WM_CLIP)
3893 return;
3895 if (dock->auto_raise_lower)
3896 wDockRaise(dock);
3898 dock->auto_raise_magic = NULL;
3901 static Bool iconCanBeOmnipresent(WAppIcon *aicon)
3903 WScreen *scr = aicon->icon->core->screen_ptr;
3904 WDock *clip;
3905 WAppIcon *btn;
3906 int i, j;
3908 for (i = 0; i < scr->workspace_count; i++) {
3909 clip = scr->workspaces[i]->clip;
3911 if (clip == aicon->dock)
3912 continue;
3914 if (clip->icon_count + scr->global_icon_count >= clip->max_icons)
3915 return False; /* Clip is full in some workspace */
3917 for (j = 0; j < clip->max_icons; j++) {
3918 btn = clip->icon_array[j];
3919 if (btn && btn->xindex == aicon->xindex && btn->yindex == aicon->yindex)
3920 return False;
3924 return True;
3927 int wClipMakeIconOmnipresent(WAppIcon *aicon, int omnipresent)
3929 WScreen *scr = aicon->icon->core->screen_ptr;
3930 WAppIconChain *new_entry, *tmp, *tmp1;
3931 int status = WO_SUCCESS;
3933 if ((scr->dock && aicon->dock == scr->dock) || aicon == scr->clip_icon)
3934 return WO_NOT_APPLICABLE;
3936 if (aicon->omnipresent == omnipresent)
3937 return WO_SUCCESS;
3939 if (omnipresent) {
3940 if (iconCanBeOmnipresent(aicon)) {
3941 aicon->omnipresent = 1;
3942 new_entry = wmalloc(sizeof(WAppIconChain));
3943 new_entry->aicon = aicon;
3944 new_entry->next = scr->global_icons;
3945 scr->global_icons = new_entry;
3946 scr->global_icon_count++;
3947 } else {
3948 aicon->omnipresent = 0;
3949 status = WO_FAILED;
3951 } else {
3952 aicon->omnipresent = 0;
3953 if (aicon == scr->global_icons->aicon) {
3954 tmp = scr->global_icons->next;
3955 wfree(scr->global_icons);
3956 scr->global_icons = tmp;
3957 scr->global_icon_count--;
3958 } else {
3959 tmp = scr->global_icons;
3960 while (tmp->next) {
3961 if (tmp->next->aicon == aicon) {
3962 tmp1 = tmp->next->next;
3963 wfree(tmp->next);
3964 tmp->next = tmp1;
3965 scr->global_icon_count--;
3966 break;
3968 tmp = tmp->next;
3973 wAppIconPaint(aicon);
3975 return status;