wIconChangeTitle rewrited
[wmaker-crm.git] / src / dock.c
blob7b5d08d8ed79adb3c3dd8b2313d2b76f0d312b4a
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"
57 #include "placement.h"
59 /**** Local variables ****/
60 #define CLIP_REWIND 1
61 #define CLIP_IDLE 0
62 #define CLIP_FORWARD 2
64 /**** Global variables ****/
65 extern Cursor wCursor[WCUR_LAST];
66 extern WPreferences wPreferences;
67 extern XContext wWinContext;
68 extern void appIconMouseDown(WObjDescriptor *desc, XEvent *event);
70 #define MOD_MASK wPreferences.modifier_mask
71 #define ICON_SIZE wPreferences.icon_size
73 /***** Local variables ****/
75 static WMPropList *dCommand = NULL;
76 static WMPropList *dPasteCommand = NULL;
77 #ifdef XDND /* XXX was OFFIX */
78 static WMPropList *dDropCommand = NULL;
79 #endif
80 static WMPropList *dAutoLaunch, *dLock;
81 static WMPropList *dName, *dForced, *dBuggyApplication, *dYes, *dNo;
82 static WMPropList *dHost, *dDock, *dClip;
83 static WMPropList *dAutoAttractIcons;
85 static WMPropList *dPosition, *dApplications, *dLowered, *dCollapsed;
87 static WMPropList *dAutoCollapse, *dAutoRaiseLower, *dOmnipresent;
89 static void dockIconPaint(WAppIcon *btn);
91 static void iconMouseDown(WObjDescriptor *desc, XEvent *event);
93 static pid_t execCommand(WAppIcon *btn, char *command, WSavedState *state);
95 static void trackDeadProcess(pid_t pid, unsigned char status, WDock *dock);
97 static int getClipButton(int px, int py);
99 static void toggleLowered(WDock *dock);
101 static void toggleCollapsed(WDock *dock);
103 static void clipIconExpose(WObjDescriptor *desc, XEvent *event);
105 static void clipLeave(WDock *dock);
107 static void handleClipChangeWorkspace(WScreen *scr, XEvent *event);
109 static Bool moveIconBetweenDocks(WDock *src, WDock *dest, WAppIcon *icon, int x, int y);
111 static void clipEnterNotify(WObjDescriptor *desc, XEvent *event);
112 static void clipLeaveNotify(WObjDescriptor *desc, XEvent *event);
113 static void clipAutoCollapse(void *cdata);
114 static void clipAutoExpand(void *cdata);
115 static void launchDockedApplication(WAppIcon *btn, Bool withSelection);
117 static void clipAutoLower(void *cdata);
118 static void clipAutoRaise(void *cdata);
119 static void reattachIcon(WDock *dock, WAppIcon *icon, int x, int y);
120 static WAppIcon *mainIconCreate(WScreen *scr, int type);
122 static void make_keys(void)
124 if (dCommand != NULL)
125 return;
127 dCommand = WMRetainPropList(WMCreatePLString("Command"));
128 dPasteCommand = WMRetainPropList(WMCreatePLString("PasteCommand"));
129 #ifdef XDND
130 dDropCommand = WMRetainPropList(WMCreatePLString("DropCommand"));
131 #endif
132 dLock = WMRetainPropList(WMCreatePLString("Lock"));
133 dAutoLaunch = WMRetainPropList(WMCreatePLString("AutoLaunch"));
134 dName = WMRetainPropList(WMCreatePLString("Name"));
135 dForced = WMRetainPropList(WMCreatePLString("Forced"));
136 dBuggyApplication = WMRetainPropList(WMCreatePLString("BuggyApplication"));
137 dYes = WMRetainPropList(WMCreatePLString("Yes"));
138 dNo = WMRetainPropList(WMCreatePLString("No"));
139 dHost = WMRetainPropList(WMCreatePLString("Host"));
141 dPosition = WMCreatePLString("Position");
142 dApplications = WMCreatePLString("Applications");
143 dLowered = WMCreatePLString("Lowered");
144 dCollapsed = WMCreatePLString("Collapsed");
145 dAutoCollapse = WMCreatePLString("AutoCollapse");
146 dAutoRaiseLower = WMCreatePLString("AutoRaiseLower");
147 dAutoAttractIcons = WMCreatePLString("AutoAttractIcons");
149 dOmnipresent = WMCreatePLString("Omnipresent");
151 dDock = WMCreatePLString("Dock");
152 dClip = WMCreatePLString("Clip");
155 static void renameCallback(WMenu *menu, WMenuEntry *entry)
157 WDock *dock = entry->clientdata;
158 char buffer[128];
159 int wspace;
160 char *name;
162 assert(entry->clientdata != NULL);
164 wspace = dock->screen_ptr->current_workspace;
166 name = wstrdup(dock->screen_ptr->workspaces[wspace]->name);
168 snprintf(buffer, sizeof(buffer), _("Type the name for workspace %i:"), wspace + 1);
169 if (wInputDialog(dock->screen_ptr, _("Rename Workspace"), buffer, &name))
170 wWorkspaceRename(dock->screen_ptr, wspace, name);
172 wfree(name);
175 static void toggleLoweredCallback(WMenu *menu, WMenuEntry *entry)
177 assert(entry->clientdata != NULL);
179 toggleLowered(entry->clientdata);
181 entry->flags.indicator_on = !(((WDock *) entry->clientdata)->lowered);
183 wMenuPaint(menu);
186 static int matchWindow(const void *item, const void *cdata)
188 return (((WFakeGroupLeader *) item)->leader == (Window) cdata);
191 static void killCallback(WMenu *menu, WMenuEntry *entry)
193 WScreen *scr = menu->menu->screen_ptr;
194 WAppIcon *icon;
195 WFakeGroupLeader *fPtr;
196 char *buffer, *shortname, **argv;
197 char *basename(const char *shortname);
198 int argc;
200 if (!WCHECK_STATE(WSTATE_NORMAL))
201 return;
203 assert(entry->clientdata != NULL);
205 icon = (WAppIcon *) entry->clientdata;
207 icon->editing = 1;
209 WCHANGE_STATE(WSTATE_MODAL);
211 /* strip away dir names */
212 shortname = basename(icon->command);
213 /* separate out command options */
214 wtokensplit(shortname, &argv, &argc);
216 buffer = wstrconcat(argv[0],
217 _(" will be forcibly closed.\n"
218 "Any unsaved changes will be lost.\n" "Please confirm."));
220 if (icon->icon && icon->icon->owner) {
221 fPtr = icon->icon->owner->fake_group;
222 } else {
223 /* is this really necessary? can we kill a non-running dock icon? */
224 Window win = icon->main_window;
225 int index;
227 index = WMFindInArray(scr->fakeGroupLeaders, matchWindow, (void *)win);
228 if (index != WANotFound)
229 fPtr = WMGetFromArray(scr->fakeGroupLeaders, index);
230 else
231 fPtr = NULL;
234 if (wPreferences.dont_confirm_kill
235 || wMessageDialog(menu->frame->screen_ptr, _("Kill Application"),
236 buffer, _("Yes"), _("No"), NULL) == WAPRDefault) {
237 if (fPtr != NULL) {
238 WWindow *wwin, *twin;
240 wwin = scr->focused_window;
241 while (wwin) {
242 twin = wwin->prev;
243 if (wwin->fake_group == fPtr)
244 wClientKill(wwin);
246 wwin = twin;
248 } else if (icon->icon && icon->icon->owner) {
249 wClientKill(icon->icon->owner);
253 wfree(buffer);
254 wtokenfree(argv, argc);
256 icon->editing = 0;
258 WCHANGE_STATE(WSTATE_NORMAL);
261 /* TODO: replace this function with a member of the dock struct */
262 static int numberOfSelectedIcons(WDock *dock)
264 WAppIcon *aicon;
265 int i, n;
267 n = 0;
268 for (i = 1; i < dock->max_icons; i++) {
269 aicon = dock->icon_array[i];
270 if (aicon && aicon->icon->selected)
271 n++;
274 return n;
277 static WMArray *getSelected(WDock *dock)
279 WMArray *ret = WMCreateArray(8);
280 WAppIcon *btn;
281 int i;
283 for (i = 1; i < dock->max_icons; i++) {
284 btn = dock->icon_array[i];
285 if (btn && btn->icon->selected)
286 WMAddToArray(ret, btn);
289 return ret;
292 static void paintClipButtons(WAppIcon *clipIcon, Bool lpushed, Bool rpushed)
294 Window win = clipIcon->icon->core->window;
295 WScreen *scr = clipIcon->icon->core->screen_ptr;
296 XPoint p[4];
297 int pt = CLIP_BUTTON_SIZE * ICON_SIZE / 64;
298 int tp = ICON_SIZE - pt;
299 int as = pt - 15; /* 15 = 5+5+5 */
300 GC gc = scr->draw_gc; /* maybe use WMColorGC() instead here? */
301 WMColor *color;
303 color = scr->clip_title_color[CLIP_NORMAL];
305 XSetForeground(dpy, gc, WMColorPixel(color));
307 if (rpushed) {
308 p[0].x = tp + 1;
309 p[0].y = 1;
310 p[1].x = ICON_SIZE - 2;
311 p[1].y = 1;
312 p[2].x = ICON_SIZE - 2;
313 p[2].y = pt - 1;
314 } else if (lpushed) {
315 p[0].x = 1;
316 p[0].y = tp;
317 p[1].x = pt;
318 p[1].y = ICON_SIZE - 2;
319 p[2].x = 1;
320 p[2].y = ICON_SIZE - 2;
322 if (lpushed || rpushed) {
323 XSetForeground(dpy, scr->draw_gc, scr->white_pixel);
324 XFillPolygon(dpy, win, scr->draw_gc, p, 3, Convex, CoordModeOrigin);
325 XSetForeground(dpy, scr->draw_gc, scr->black_pixel);
328 /* top right arrow */
329 p[0].x = p[3].x = ICON_SIZE - 5 - as;
330 p[0].y = p[3].y = 5;
331 p[1].x = ICON_SIZE - 6;
332 p[1].y = 5;
333 p[2].x = ICON_SIZE - 6;
334 p[2].y = 4 + as;
335 if (rpushed) {
336 XFillPolygon(dpy, win, scr->draw_gc, p, 3, Convex, CoordModeOrigin);
337 XDrawLines(dpy, win, scr->draw_gc, p, 4, CoordModeOrigin);
338 } else {
339 XFillPolygon(dpy, win, gc, p, 3, Convex, CoordModeOrigin);
340 XDrawLines(dpy, win, gc, p, 4, CoordModeOrigin);
343 /* bottom left arrow */
344 p[0].x = p[3].x = 5;
345 p[0].y = p[3].y = ICON_SIZE - 5 - as;
346 p[1].x = 5;
347 p[1].y = ICON_SIZE - 6;
348 p[2].x = 4 + as;
349 p[2].y = ICON_SIZE - 6;
350 if (lpushed) {
351 XFillPolygon(dpy, win, scr->draw_gc, p, 3, Convex, CoordModeOrigin);
352 XDrawLines(dpy, win, scr->draw_gc, p, 4, CoordModeOrigin);
353 } else {
354 XFillPolygon(dpy, win, gc, p, 3, Convex, CoordModeOrigin);
355 XDrawLines(dpy, win, gc, p, 4, CoordModeOrigin);
359 RImage *wClipMakeTile(WScreen *scr, RImage *normalTile)
361 RImage *tile = RCloneImage(normalTile);
362 RColor black;
363 RColor dark;
364 RColor light;
365 int pt, tp;
366 int as;
368 pt = CLIP_BUTTON_SIZE * wPreferences.icon_size / 64;
369 tp = wPreferences.icon_size - 1 - pt;
370 as = pt - 15;
372 black.alpha = 255;
373 black.red = black.green = black.blue = 0;
375 dark.alpha = 0;
376 dark.red = dark.green = dark.blue = 60;
378 light.alpha = 0;
379 light.red = light.green = light.blue = 80;
381 /* top right */
382 ROperateLine(tile, RSubtractOperation, tp, 0, wPreferences.icon_size - 2, pt - 1, &dark);
383 RDrawLine(tile, tp - 1, 0, wPreferences.icon_size - 1, pt + 1, &black);
384 ROperateLine(tile, RAddOperation, tp, 2, wPreferences.icon_size - 3, pt, &light);
386 /* arrow bevel */
387 ROperateLine(tile, RSubtractOperation, ICON_SIZE - 7 - as, 4, ICON_SIZE - 5, 4, &dark);
388 ROperateLine(tile, RSubtractOperation, ICON_SIZE - 6 - as, 5, ICON_SIZE - 5, 6 + as, &dark);
389 ROperateLine(tile, RAddOperation, ICON_SIZE - 5, 4, ICON_SIZE - 5, 6 + as, &light);
391 /* bottom left */
392 ROperateLine(tile, RAddOperation, 2, tp + 2, pt - 2, wPreferences.icon_size - 3, &dark);
393 RDrawLine(tile, 0, tp - 1, pt + 1, wPreferences.icon_size - 1, &black);
394 ROperateLine(tile, RSubtractOperation, 0, tp - 2, pt + 1, wPreferences.icon_size - 2, &light);
396 /* arrow bevel */
397 ROperateLine(tile, RSubtractOperation, 4, ICON_SIZE - 7 - as, 4, ICON_SIZE - 5, &dark);
398 ROperateLine(tile, RSubtractOperation, 5, ICON_SIZE - 6 - as, 6 + as, ICON_SIZE - 5, &dark);
399 ROperateLine(tile, RAddOperation, 4, ICON_SIZE - 5, 6 + as, ICON_SIZE - 5, &light);
401 return tile;
404 static void omnipresentCallback(WMenu *menu, WMenuEntry *entry)
406 WAppIcon *clickedIcon = entry->clientdata;
407 WAppIcon *aicon;
408 WDock *dock;
409 WMArray *selectedIcons;
410 WMArrayIterator iter;
411 int failed;
413 assert(entry->clientdata != NULL);
415 dock = clickedIcon->dock;
417 selectedIcons = getSelected(dock);
419 if (!WMGetArrayItemCount(selectedIcons))
420 WMAddToArray(selectedIcons, clickedIcon);
422 failed = 0;
423 WM_ITERATE_ARRAY(selectedIcons, aicon, iter) {
424 if (wClipMakeIconOmnipresent(aicon, !aicon->omnipresent) == WO_FAILED)
425 failed++;
426 else if (aicon->icon->selected)
427 wIconSelect(aicon->icon);
429 WMFreeArray(selectedIcons);
431 if (failed > 1) {
432 wMessageDialog(dock->screen_ptr, _("Warning"),
433 _("Some icons cannot be made omnipresent. "
434 "Please make sure that no other icon is "
435 "docked in the same positions on the other "
436 "workspaces and the Clip is not full in "
437 "some workspace."), _("OK"), NULL, NULL);
438 } else if (failed == 1) {
439 wMessageDialog(dock->screen_ptr, _("Warning"),
440 _("Icon cannot be made omnipresent. "
441 "Please make sure that no other icon is "
442 "docked in the same position on the other "
443 "workspaces and the Clip is not full in "
444 "some workspace."), _("OK"), NULL, NULL);
448 static void removeIconsCallback(WMenu *menu, WMenuEntry *entry)
450 WAppIcon *clickedIcon = (WAppIcon *) entry->clientdata;
451 WDock *dock;
452 WAppIcon *aicon;
453 WMArray *selectedIcons;
454 int keepit;
455 WMArrayIterator it;
457 assert(clickedIcon != NULL);
459 dock = clickedIcon->dock;
461 selectedIcons = getSelected(dock);
463 if (WMGetArrayItemCount(selectedIcons)) {
464 if (wMessageDialog(dock->screen_ptr, _("Workspace Clip"),
465 _("All selected icons will be removed!"),
466 _("OK"), _("Cancel"), NULL) != WAPRDefault) {
467 WMFreeArray(selectedIcons);
468 return;
470 } else {
471 if (clickedIcon->xindex == 0 && clickedIcon->yindex == 0) {
472 WMFreeArray(selectedIcons);
473 return;
475 WMAddToArray(selectedIcons, clickedIcon);
478 WM_ITERATE_ARRAY(selectedIcons, aicon, it) {
479 keepit = aicon->running && wApplicationOf(aicon->main_window);
480 wDockDetach(dock, aicon);
481 if (keepit) {
482 /* XXX: can: aicon->icon == NULL ? */
483 PlaceIcon(dock->screen_ptr, &aicon->x_pos, &aicon->y_pos,
484 wGetHeadForWindow(aicon->icon->owner));
485 XMoveWindow(dpy, aicon->icon->core->window, aicon->x_pos, aicon->y_pos);
486 if (!dock->mapped || dock->collapsed)
487 XMapWindow(dpy, aicon->icon->core->window);
490 WMFreeArray(selectedIcons);
492 if (wPreferences.auto_arrange_icons)
493 wArrangeIcons(dock->screen_ptr, True);
496 static void keepIconsCallback(WMenu *menu, WMenuEntry *entry)
498 WAppIcon *clickedIcon = (WAppIcon *) entry->clientdata;
499 WDock *dock;
500 WAppIcon *aicon;
501 WMArray *selectedIcons;
502 WMArrayIterator it;
504 assert(clickedIcon != NULL);
505 dock = clickedIcon->dock;
507 selectedIcons = getSelected(dock);
509 if (!WMGetArrayItemCount(selectedIcons)
510 && clickedIcon != dock->screen_ptr->clip_icon) {
511 char *command = NULL;
513 if (!clickedIcon->command && !clickedIcon->editing) {
514 clickedIcon->editing = 1;
515 if (wInputDialog(dock->screen_ptr, _("Keep Icon"),
516 _("Type the command used to launch the application"), &command)) {
517 if (command && (command[0] == 0 || (command[0] == '-' && command[1] == 0))) {
518 wfree(command);
519 command = NULL;
521 clickedIcon->command = command;
522 clickedIcon->editing = 0;
523 } else {
524 clickedIcon->editing = 0;
525 if (command)
526 wfree(command);
527 WMFreeArray(selectedIcons);
528 return;
532 WMAddToArray(selectedIcons, clickedIcon);
535 WM_ITERATE_ARRAY(selectedIcons, aicon, it) {
536 if (aicon->icon->selected)
537 wIconSelect(aicon->icon);
539 if (aicon && aicon->attracted && aicon->command) {
540 aicon->attracted = 0;
541 if (aicon->icon->shadowed) {
542 aicon->icon->shadowed = 0;
544 /* Update the icon images */
545 wIconUpdate(aicon->icon);
547 /* Paint it */
548 wAppIconPaint(aicon);
551 save_appicon(aicon, True);
553 WMFreeArray(selectedIcons);
556 static void toggleAutoAttractCallback(WMenu *menu, WMenuEntry *entry)
558 WDock *dock = (WDock *) entry->clientdata;
560 assert(entry->clientdata != NULL);
562 dock->attract_icons = !dock->attract_icons;
564 entry->flags.indicator_on = dock->attract_icons;
566 wMenuPaint(menu);
569 static void selectCallback(WMenu *menu, WMenuEntry *entry)
571 WAppIcon *icon = (WAppIcon *) entry->clientdata;
573 assert(icon != NULL);
575 wIconSelect(icon->icon);
577 wMenuPaint(menu);
580 static void colectIconsCallback(WMenu *menu, WMenuEntry *entry)
582 WAppIcon *clickedIcon = (WAppIcon *) entry->clientdata;
583 WDock *clip;
584 WAppIcon *aicon;
585 int x, y, x_pos, y_pos;
586 Bool update_icon = False;
588 assert(entry->clientdata != NULL);
589 clip = clickedIcon->dock;
591 aicon = clip->screen_ptr->app_icon_list;
593 while (aicon) {
594 if (!aicon->docked && wDockFindFreeSlot(clip, &x, &y)) {
595 x_pos = clip->x_pos + x * ICON_SIZE;
596 y_pos = clip->y_pos + y * ICON_SIZE;
597 if (aicon->x_pos != x_pos || aicon->y_pos != y_pos)
598 move_window(aicon->icon->core->window, aicon->x_pos, aicon->y_pos, x_pos, y_pos);
600 aicon->attracted = 1;
601 if (!aicon->icon->shadowed) {
602 aicon->icon->shadowed = 1;
603 update_icon = True;
605 wDockAttachIcon(clip, aicon, x, y, update_icon);
606 if (clip->collapsed || !clip->mapped)
607 XUnmapWindow(dpy, aicon->icon->core->window);
609 aicon = aicon->next;
613 static void selectIconsCallback(WMenu *menu, WMenuEntry *entry)
615 WAppIcon *clickedIcon = (WAppIcon *) entry->clientdata;
616 WDock *dock;
617 WMArray *selectedIcons;
618 WMArrayIterator iter;
619 WAppIcon *btn;
620 int i;
622 assert(clickedIcon != NULL);
623 dock = clickedIcon->dock;
625 selectedIcons = getSelected(dock);
627 if (!WMGetArrayItemCount(selectedIcons)) {
628 for (i = 1; i < dock->max_icons; i++) {
629 btn = dock->icon_array[i];
630 if (btn && !btn->icon->selected)
631 wIconSelect(btn->icon);
633 } else {
634 WM_ITERATE_ARRAY(selectedIcons, btn, iter) {
635 wIconSelect(btn->icon);
638 WMFreeArray(selectedIcons);
640 wMenuPaint(menu);
643 static void toggleCollapsedCallback(WMenu *menu, WMenuEntry *entry)
645 assert(entry->clientdata != NULL);
647 toggleCollapsed(entry->clientdata);
649 entry->flags.indicator_on = ((WDock *) entry->clientdata)->collapsed;
651 wMenuPaint(menu);
654 static void toggleAutoCollapseCallback(WMenu *menu, WMenuEntry *entry)
656 WDock *dock;
657 assert(entry->clientdata != NULL);
659 dock = (WDock *) entry->clientdata;
661 dock->auto_collapse = !dock->auto_collapse;
663 entry->flags.indicator_on = ((WDock *) entry->clientdata)->auto_collapse;
665 wMenuPaint(menu);
668 static void toggleAutoRaiseLowerCallback(WMenu *menu, WMenuEntry *entry)
670 WDock *dock;
671 assert(entry->clientdata != NULL);
673 dock = (WDock *) entry->clientdata;
675 dock->auto_raise_lower = !dock->auto_raise_lower;
677 entry->flags.indicator_on = ((WDock *) entry->clientdata)->auto_raise_lower;
679 wMenuPaint(menu);
682 static void launchCallback(WMenu *menu, WMenuEntry *entry)
684 WAppIcon *btn = (WAppIcon *) entry->clientdata;
686 launchDockedApplication(btn, False);
689 static void settingsCallback(WMenu *menu, WMenuEntry *entry)
691 WAppIcon *btn = (WAppIcon *) entry->clientdata;
693 if (btn->editing)
694 return;
695 ShowDockAppSettingsPanel(btn);
698 static void hideCallback(WMenu *menu, WMenuEntry *entry)
700 WApplication *wapp;
701 WAppIcon *btn = (WAppIcon *) entry->clientdata;
703 wapp = wApplicationOf(btn->icon->owner->main_window);
705 if (wapp->flags.hidden) {
706 wWorkspaceChange(btn->icon->core->screen_ptr, wapp->last_workspace);
707 wUnhideApplication(wapp, False, False);
708 } else {
709 wHideApplication(wapp);
713 static void unhideHereCallback(WMenu *menu, WMenuEntry *entry)
715 WApplication *wapp;
716 WAppIcon *btn = (WAppIcon *) entry->clientdata;
718 wapp = wApplicationOf(btn->icon->owner->main_window);
720 wUnhideApplication(wapp, False, True);
723 static WAppIcon *mainIconCreate(WScreen *scr, int type)
725 WAppIcon *btn;
726 int x_pos;
728 if (type == WM_CLIP) {
729 if (scr->clip_icon)
730 return scr->clip_icon;
731 btn = wAppIconCreateForDock(scr, NULL, "Logo", "WMClip", TILE_CLIP);
732 btn->icon->core->descriptor.handle_expose = clipIconExpose;
733 btn->icon->core->descriptor.handle_enternotify = clipEnterNotify;
734 btn->icon->core->descriptor.handle_leavenotify = clipLeaveNotify;
735 x_pos = 0;
736 } else {
737 btn = wAppIconCreateForDock(scr, NULL, "Logo", "WMDock", TILE_NORMAL);
738 x_pos = scr->scr_width - ICON_SIZE - DOCK_EXTRA_SPACE;
741 btn->xindex = 0;
742 btn->yindex = 0;
744 btn->icon->core->descriptor.handle_mousedown = iconMouseDown;
745 btn->icon->core->descriptor.parent_type = WCLASS_DOCK_ICON;
746 btn->icon->core->descriptor.parent = btn;
747 XMapWindow(dpy, btn->icon->core->window);
748 btn->x_pos = x_pos;
749 btn->y_pos = 0;
750 btn->docked = 1;
751 if (type == WM_CLIP)
752 scr->clip_icon = btn;
754 return btn;
757 static void switchWSCommand(WMenu *menu, WMenuEntry *entry)
759 WAppIcon *btn, *icon = (WAppIcon *) entry->clientdata;
760 WScreen *scr = icon->icon->core->screen_ptr;
761 WDock *src, *dest;
762 WMArray *selectedIcons;
763 int x, y;
765 if (entry->order == scr->current_workspace)
766 return;
767 src = icon->dock;
768 dest = scr->workspaces[entry->order]->clip;
770 selectedIcons = getSelected(src);
772 if (WMGetArrayItemCount(selectedIcons)) {
773 WMArrayIterator iter;
775 WM_ITERATE_ARRAY(selectedIcons, btn, iter) {
776 if (wDockFindFreeSlot(dest, &x, &y)) {
777 moveIconBetweenDocks(src, dest, btn, x, y);
778 XUnmapWindow(dpy, btn->icon->core->window);
781 } else if (icon != scr->clip_icon) {
782 if (wDockFindFreeSlot(dest, &x, &y)) {
783 moveIconBetweenDocks(src, dest, icon, x, y);
784 XUnmapWindow(dpy, icon->icon->core->window);
787 WMFreeArray(selectedIcons);
790 static void launchDockedApplication(WAppIcon *btn, Bool withSelection)
792 WScreen *scr = btn->icon->core->screen_ptr;
794 if (!btn->launching &&
795 ((!withSelection && btn->command != NULL) || (withSelection && btn->paste_command != NULL))) {
796 if (!btn->forced_dock) {
797 btn->relaunching = btn->running;
798 btn->running = 1;
800 if (btn->wm_instance || btn->wm_class) {
801 WWindowAttributes attr;
802 memset(&attr, 0, sizeof(WWindowAttributes));
803 wDefaultFillAttributes(btn->wm_instance, btn->wm_class, &attr, NULL, True);
805 if (!attr.no_appicon && !btn->buggy_app)
806 btn->launching = 1;
807 else
808 btn->running = 0;
810 btn->drop_launch = 0;
811 btn->paste_launch = withSelection;
812 scr->last_dock = btn->dock;
813 btn->pid = execCommand(btn, (withSelection ? btn->paste_command : btn->command), NULL);
814 if (btn->pid > 0) {
815 if (btn->buggy_app) {
816 /* give feedback that the app was launched */
817 btn->launching = 1;
818 dockIconPaint(btn);
819 btn->launching = 0;
820 WMAddTimerHandler(200, (WMCallback *) dockIconPaint, btn);
821 } else {
822 dockIconPaint(btn);
824 } else {
825 wwarning(_("could not launch application %s"), btn->command);
826 btn->launching = 0;
827 if (!btn->relaunching)
828 btn->running = 0;
833 static void updateWorkspaceMenu(WMenu *menu, WAppIcon *icon)
835 WScreen *scr = menu->frame->screen_ptr;
836 char title[MAX_WORKSPACENAME_WIDTH + 1];
837 int i;
839 if (!menu || !icon)
840 return;
842 for (i = 0; i < scr->workspace_count; i++) {
843 if (i < menu->entry_no) {
844 if (strcmp(menu->entries[i]->text, scr->workspaces[i]->name) != 0) {
845 wfree(menu->entries[i]->text);
846 strcpy(title, scr->workspaces[i]->name);
847 menu->entries[i]->text = wstrdup(title);
848 menu->flags.realized = 0;
850 menu->entries[i]->clientdata = (void *)icon;
851 } else {
852 strcpy(title, scr->workspaces[i]->name);
854 wMenuAddCallback(menu, title, switchWSCommand, (void *)icon);
856 menu->flags.realized = 0;
859 if (i == scr->current_workspace)
860 wMenuSetEnabled(menu, i, False);
861 else
862 wMenuSetEnabled(menu, i, True);
865 if (!menu->flags.realized)
866 wMenuRealize(menu);
869 static WMenu *makeWorkspaceMenu(WScreen *scr)
871 WMenu *menu;
873 menu = wMenuCreate(scr, NULL, False);
874 if (!menu)
875 wwarning(_("could not create workspace submenu for Clip menu"));
877 wMenuAddCallback(menu, "", switchWSCommand, (void *)scr->clip_icon);
879 menu->flags.realized = 0;
880 wMenuRealize(menu);
882 return menu;
885 static void updateClipOptionsMenu(WMenu *menu, WDock *dock)
887 WMenuEntry *entry;
888 int index = 0;
890 if (!menu || !dock)
891 return;
893 /* keep on top */
894 entry = menu->entries[index];
895 entry->flags.indicator_on = !dock->lowered;
896 entry->clientdata = dock;
898 /* collapsed */
899 entry = menu->entries[++index];
900 entry->flags.indicator_on = dock->collapsed;
901 entry->clientdata = dock;
903 /* auto-collapse */
904 entry = menu->entries[++index];
905 entry->flags.indicator_on = dock->auto_collapse;
906 entry->clientdata = dock;
908 /* auto-raise/lower */
909 entry = menu->entries[++index];
910 entry->flags.indicator_on = dock->auto_raise_lower;
911 entry->clientdata = dock;
912 wMenuSetEnabled(menu, index, dock->lowered);
914 /* attract icons */
915 entry = menu->entries[++index];
916 entry->flags.indicator_on = dock->attract_icons;
917 entry->clientdata = dock;
919 menu->flags.realized = 0;
920 wMenuRealize(menu);
923 static WMenu *makeClipOptionsMenu(WScreen *scr)
925 WMenu *menu;
926 WMenuEntry *entry;
928 menu = wMenuCreate(scr, NULL, False);
929 if (!menu) {
930 wwarning(_("could not create options submenu for Clip menu"));
931 return NULL;
934 entry = wMenuAddCallback(menu, _("Keep on Top"), toggleLoweredCallback, NULL);
935 entry->flags.indicator = 1;
936 entry->flags.indicator_on = 1;
937 entry->flags.indicator_type = MI_CHECK;
939 entry = wMenuAddCallback(menu, _("Collapsed"), toggleCollapsedCallback, NULL);
940 entry->flags.indicator = 1;
941 entry->flags.indicator_on = 1;
942 entry->flags.indicator_type = MI_CHECK;
944 entry = wMenuAddCallback(menu, _("Autocollapse"), toggleAutoCollapseCallback, NULL);
945 entry->flags.indicator = 1;
946 entry->flags.indicator_on = 1;
947 entry->flags.indicator_type = MI_CHECK;
949 entry = wMenuAddCallback(menu, _("Autoraise"), toggleAutoRaiseLowerCallback, NULL);
950 entry->flags.indicator = 1;
951 entry->flags.indicator_on = 1;
952 entry->flags.indicator_type = MI_CHECK;
954 entry = wMenuAddCallback(menu, _("Autoattract Icons"), toggleAutoAttractCallback, NULL);
955 entry->flags.indicator = 1;
956 entry->flags.indicator_on = 1;
957 entry->flags.indicator_type = MI_CHECK;
959 menu->flags.realized = 0;
960 wMenuRealize(menu);
962 return menu;
965 static WMenu *dockMenuCreate(WScreen *scr, int type)
967 WMenu *menu;
968 WMenuEntry *entry;
970 if (type == WM_CLIP && scr->clip_menu)
971 return scr->clip_menu;
973 menu = wMenuCreate(scr, NULL, False);
974 if (type != WM_CLIP) {
975 entry = wMenuAddCallback(menu, _("Keep on Top"), toggleLoweredCallback, NULL);
976 entry->flags.indicator = 1;
977 entry->flags.indicator_on = 1;
978 entry->flags.indicator_type = MI_CHECK;
979 } else {
980 entry = wMenuAddCallback(menu, _("Clip Options"), NULL, NULL);
981 scr->clip_options = makeClipOptionsMenu(scr);
982 if (scr->clip_options)
983 wMenuEntrySetCascade(menu, entry, scr->clip_options);
985 entry = wMenuAddCallback(menu, _("Rename Workspace"), renameCallback, NULL);
986 wfree(entry->text);
987 entry->text = _("Rename Workspace");
989 entry = wMenuAddCallback(menu, _("Selected"), selectCallback, NULL);
990 entry->flags.indicator = 1;
991 entry->flags.indicator_on = 1;
992 entry->flags.indicator_type = MI_CHECK;
994 entry = wMenuAddCallback(menu, _("Select All Icons"), selectIconsCallback, NULL);
995 wfree(entry->text);
996 entry->text = _("Select All Icons");
998 entry = wMenuAddCallback(menu, _("Keep Icon"), keepIconsCallback, NULL);
999 wfree(entry->text);
1000 entry->text = _("Keep Icon");
1002 entry = wMenuAddCallback(menu, _("Move Icon To"), NULL, NULL);
1003 wfree(entry->text);
1004 entry->text = _("Move Icon To");
1005 scr->clip_submenu = makeWorkspaceMenu(scr);
1006 if (scr->clip_submenu)
1007 wMenuEntrySetCascade(menu, entry, scr->clip_submenu);
1009 entry = wMenuAddCallback(menu, _("Remove Icon"), removeIconsCallback, NULL);
1010 wfree(entry->text);
1011 entry->text = _("Remove Icon");
1013 wMenuAddCallback(menu, _("Attract Icons"), colectIconsCallback, NULL);
1016 wMenuAddCallback(menu, _("Launch"), launchCallback, NULL);
1018 wMenuAddCallback(menu, _("Unhide Here"), unhideHereCallback, NULL);
1020 entry = wMenuAddCallback(menu, _("Hide"), hideCallback, NULL);
1021 wfree(entry->text);
1022 entry->text = _("Hide");
1024 wMenuAddCallback(menu, _("Settings..."), settingsCallback, NULL);
1026 wMenuAddCallback(menu, _("Kill"), killCallback, NULL);
1028 if (type == WM_CLIP)
1029 scr->clip_menu = menu;
1031 return menu;
1034 WDock *wDockCreate(WScreen *scr, int type)
1036 WDock *dock;
1037 WAppIcon *btn;
1039 make_keys();
1041 dock = wmalloc(sizeof(WDock));
1043 dock->max_icons = DOCK_MAX_ICONS;
1045 dock->icon_array = wmalloc(sizeof(WAppIcon *) * dock->max_icons);
1047 btn = mainIconCreate(scr, type);
1049 btn->dock = dock;
1051 dock->x_pos = btn->x_pos;
1052 dock->y_pos = btn->y_pos;
1053 dock->screen_ptr = scr;
1054 dock->type = type;
1055 dock->icon_count = 1;
1056 dock->on_right_side = 1;
1057 dock->collapsed = 0;
1058 dock->auto_collapse = 0;
1059 dock->auto_collapse_magic = NULL;
1060 dock->auto_raise_lower = 0;
1061 dock->auto_lower_magic = NULL;
1062 dock->auto_raise_magic = NULL;
1063 dock->attract_icons = 0;
1064 dock->lowered = 1;
1065 dock->icon_array[0] = btn;
1066 wRaiseFrame(btn->icon->core);
1067 XMoveWindow(dpy, btn->icon->core->window, btn->x_pos, btn->y_pos);
1069 /* create dock menu */
1070 dock->menu = dockMenuCreate(scr, type);
1072 return dock;
1075 void wDockDestroy(WDock *dock)
1077 int i;
1078 WAppIcon *aicon;
1080 for (i = (dock->type == WM_CLIP) ? 1 : 0; i < dock->max_icons; i++) {
1081 aicon = dock->icon_array[i];
1082 if (aicon) {
1083 int keepit = aicon->running && wApplicationOf(aicon->main_window);
1084 wDockDetach(dock, aicon);
1085 if (keepit) {
1086 /* XXX: can: aicon->icon == NULL ? */
1087 PlaceIcon(dock->screen_ptr, &aicon->x_pos, &aicon->y_pos,
1088 wGetHeadForWindow(aicon->icon->owner));
1089 XMoveWindow(dpy, aicon->icon->core->window, aicon->x_pos, aicon->y_pos);
1090 if (!dock->mapped || dock->collapsed)
1091 XMapWindow(dpy, aicon->icon->core->window);
1095 if (wPreferences.auto_arrange_icons)
1096 wArrangeIcons(dock->screen_ptr, True);
1097 wfree(dock->icon_array);
1098 if (dock->menu && dock->type != WM_CLIP)
1099 wMenuDestroy(dock->menu, True);
1100 if (dock->screen_ptr->last_dock == dock)
1101 dock->screen_ptr->last_dock = NULL;
1102 wfree(dock);
1105 void wClipIconPaint(WAppIcon *aicon)
1107 WScreen *scr = aicon->icon->core->screen_ptr;
1108 WWorkspace *workspace = scr->workspaces[scr->current_workspace];
1109 WMColor *color;
1110 Window win = aicon->icon->core->window;
1111 int length, nlength;
1112 char *ws_name, ws_number[10];
1113 int ty, tx;
1115 wIconPaint(aicon->icon);
1117 length = strlen(workspace->name);
1118 ws_name = wmalloc(length + 1);
1119 snprintf(ws_name, length + 1, "%s", workspace->name);
1120 snprintf(ws_number, sizeof(ws_number), "%i", scr->current_workspace + 1);
1121 nlength = strlen(ws_number);
1123 if (!workspace->clip->collapsed)
1124 color = scr->clip_title_color[CLIP_NORMAL];
1125 else
1126 color = scr->clip_title_color[CLIP_COLLAPSED];
1128 ty = ICON_SIZE - WMFontHeight(scr->clip_title_font) - 3;
1130 tx = CLIP_BUTTON_SIZE * ICON_SIZE / 64;
1132 if(wPreferences.show_clip_title)
1133 WMDrawString(scr->wmscreen, win, color, scr->clip_title_font, tx, ty, ws_name, length);
1135 tx = (ICON_SIZE / 2 - WMWidthOfString(scr->clip_title_font, ws_number, nlength)) / 2;
1137 WMDrawString(scr->wmscreen, win, color, scr->clip_title_font, tx, 2, ws_number, nlength);
1139 wfree(ws_name);
1141 if (aicon->launching)
1142 XFillRectangle(dpy, aicon->icon->core->window, scr->stipple_gc,
1143 0, 0, wPreferences.icon_size, wPreferences.icon_size);
1145 paintClipButtons(aicon, aicon->dock->lclip_button_pushed, aicon->dock->rclip_button_pushed);
1148 static void clipIconExpose(WObjDescriptor *desc, XEvent *event)
1150 wClipIconPaint(desc->parent);
1153 static void dockIconPaint(WAppIcon *btn)
1155 if (btn == btn->icon->core->screen_ptr->clip_icon) {
1156 wClipIconPaint(btn);
1157 } else {
1158 wAppIconPaint(btn);
1159 save_appicon(btn, True);
1163 static WMPropList *make_icon_state(WAppIcon *btn)
1165 WMPropList *node = NULL;
1166 WMPropList *command, *autolaunch, *lock, *name, *forced;
1167 WMPropList *position, *buggy, *omnipresent;
1168 char *tmp;
1169 char buffer[64];
1171 if (btn) {
1172 if (!btn->command)
1173 command = WMCreatePLString("-");
1174 else
1175 command = WMCreatePLString(btn->command);
1177 autolaunch = btn->auto_launch ? dYes : dNo;
1179 lock = btn->lock ? dYes : dNo;
1181 tmp = EscapeWM_CLASS(btn->wm_instance, btn->wm_class);
1183 name = WMCreatePLString(tmp);
1185 wfree(tmp);
1187 forced = btn->forced_dock ? dYes : dNo;
1189 buggy = btn->buggy_app ? dYes : dNo;
1191 if (btn == btn->icon->core->screen_ptr->clip_icon)
1192 snprintf(buffer, sizeof(buffer), "%i,%i", btn->x_pos, btn->y_pos);
1193 else
1194 snprintf(buffer, sizeof(buffer), "%hi,%hi", btn->xindex, btn->yindex);
1195 position = WMCreatePLString(buffer);
1197 node = WMCreatePLDictionary(dCommand, command,
1198 dName, name,
1199 dAutoLaunch, autolaunch,
1200 dLock, lock,
1201 dForced, forced, dBuggyApplication, buggy, dPosition, position, NULL);
1202 WMReleasePropList(command);
1203 WMReleasePropList(name);
1204 WMReleasePropList(position);
1206 omnipresent = btn->omnipresent ? dYes : dNo;
1207 if (btn->dock != btn->icon->core->screen_ptr->dock && (btn->xindex != 0 || btn->yindex != 0))
1208 WMPutInPLDictionary(node, dOmnipresent, omnipresent);
1210 #ifdef XDND /* was OFFIX */
1211 if (btn->dnd_command) {
1212 command = WMCreatePLString(btn->dnd_command);
1213 WMPutInPLDictionary(node, dDropCommand, command);
1214 WMReleasePropList(command);
1216 #endif /* XDND */
1218 if (btn->paste_command) {
1219 command = WMCreatePLString(btn->paste_command);
1220 WMPutInPLDictionary(node, dPasteCommand, command);
1221 WMReleasePropList(command);
1225 return node;
1228 static WMPropList *dockSaveState(WDock *dock)
1230 int i;
1231 WMPropList *icon_info;
1232 WMPropList *list = NULL, *dock_state = NULL;
1233 WMPropList *value, *key;
1234 char buffer[256];
1236 list = WMCreatePLArray(NULL);
1238 for (i = (dock->type == WM_DOCK ? 0 : 1); i < dock->max_icons; i++) {
1239 WAppIcon *btn = dock->icon_array[i];
1241 if (!btn || btn->attracted)
1242 continue;
1244 if ((icon_info = make_icon_state(dock->icon_array[i]))) {
1245 WMAddToPLArray(list, icon_info);
1246 WMReleasePropList(icon_info);
1250 dock_state = WMCreatePLDictionary(dApplications, list, NULL);
1252 if (dock->type == WM_DOCK) {
1253 snprintf(buffer, sizeof(buffer), "Applications%i", dock->screen_ptr->scr_height);
1254 key = WMCreatePLString(buffer);
1255 WMPutInPLDictionary(dock_state, key, list);
1256 WMReleasePropList(key);
1258 snprintf(buffer, sizeof(buffer), "%i,%i", (dock->on_right_side ? -ICON_SIZE : 0), dock->y_pos);
1259 value = WMCreatePLString(buffer);
1260 WMPutInPLDictionary(dock_state, dPosition, value);
1261 WMReleasePropList(value);
1263 WMReleasePropList(list);
1265 value = (dock->lowered ? dYes : dNo);
1266 WMPutInPLDictionary(dock_state, dLowered, value);
1268 if (dock->type == WM_CLIP) {
1269 value = (dock->collapsed ? dYes : dNo);
1270 WMPutInPLDictionary(dock_state, dCollapsed, value);
1272 value = (dock->auto_collapse ? dYes : dNo);
1273 WMPutInPLDictionary(dock_state, dAutoCollapse, value);
1275 value = (dock->auto_raise_lower ? dYes : dNo);
1276 WMPutInPLDictionary(dock_state, dAutoRaiseLower, value);
1278 value = (dock->attract_icons ? dYes : dNo);
1279 WMPutInPLDictionary(dock_state, dAutoAttractIcons, value);
1282 return dock_state;
1285 void wDockSaveState(WScreen *scr, WMPropList *old_state)
1287 WMPropList *dock_state;
1288 WMPropList *keys;
1290 dock_state = dockSaveState(scr->dock);
1293 * Copy saved states of docks with different sizes.
1295 if (old_state) {
1296 int i;
1297 WMPropList *tmp;
1299 keys = WMGetPLDictionaryKeys(old_state);
1300 for (i = 0; i < WMGetPropListItemCount(keys); i++) {
1301 tmp = WMGetFromPLArray(keys, i);
1303 if (strncasecmp(WMGetFromPLString(tmp), "applications", 12) == 0
1304 && !WMGetFromPLDictionary(dock_state, tmp)) {
1306 WMPutInPLDictionary(dock_state, tmp, WMGetFromPLDictionary(old_state, tmp));
1309 WMReleasePropList(keys);
1312 WMPutInPLDictionary(scr->session_state, dDock, dock_state);
1314 WMReleasePropList(dock_state);
1317 void wClipSaveState(WScreen *scr)
1319 WMPropList *clip_state;
1321 clip_state = make_icon_state(scr->clip_icon);
1323 WMPutInPLDictionary(scr->session_state, dClip, clip_state);
1325 WMReleasePropList(clip_state);
1328 WMPropList *wClipSaveWorkspaceState(WScreen *scr, int workspace)
1330 return dockSaveState(scr->workspaces[workspace]->clip);
1333 static Bool getBooleanDockValue(WMPropList *value, WMPropList *key)
1335 if (value) {
1336 if (WMIsPLString(value)) {
1337 if (strcasecmp(WMGetFromPLString(value), "YES") == 0)
1338 return True;
1339 } else {
1340 wwarning(_("bad value in docked icon state info %s"), WMGetFromPLString(key));
1343 return False;
1346 static WAppIcon *restore_icon_state(WScreen *scr, WMPropList *info, int type, int index)
1348 WAppIcon *aicon;
1349 WMPropList *cmd, *value;
1350 char *wclass, *winstance, *command;
1352 cmd = WMGetFromPLDictionary(info, dCommand);
1353 if (!cmd || !WMIsPLString(cmd))
1354 return NULL;
1356 /* parse window name */
1357 value = WMGetFromPLDictionary(info, dName);
1358 if (!value)
1359 return NULL;
1361 ParseWindowName(value, &winstance, &wclass, "dock");
1363 if (!winstance && !wclass)
1364 return NULL;
1366 /* 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);
1391 aicon->icon->core->descriptor.handle_mousedown = iconMouseDown;
1392 if (type == WM_CLIP) {
1393 aicon->icon->core->descriptor.handle_enternotify = clipEnterNotify;
1394 aicon->icon->core->descriptor.handle_leavenotify = clipLeaveNotify;
1396 aicon->icon->core->descriptor.parent_type = WCLASS_DOCK_ICON;
1397 aicon->icon->core->descriptor.parent = aicon;
1399 #ifdef XDND /* was OFFIX */
1400 cmd = WMGetFromPLDictionary(info, dDropCommand);
1401 if (cmd)
1402 aicon->dnd_command = wstrdup(WMGetFromPLString(cmd));
1403 #endif
1405 cmd = WMGetFromPLDictionary(info, dPasteCommand);
1406 if (cmd)
1407 aicon->paste_command = wstrdup(WMGetFromPLString(cmd));
1409 /* check auto launch */
1410 value = WMGetFromPLDictionary(info, dAutoLaunch);
1412 aicon->auto_launch = getBooleanDockValue(value, dAutoLaunch);
1414 /* check lock */
1415 value = WMGetFromPLDictionary(info, dLock);
1417 aicon->lock = getBooleanDockValue(value, dLock);
1419 /* check if it wasn't normally docked */
1420 value = WMGetFromPLDictionary(info, dForced);
1422 aicon->forced_dock = getBooleanDockValue(value, dForced);
1424 /* check if we can rely on the stuff in the app */
1425 value = WMGetFromPLDictionary(info, dBuggyApplication);
1427 aicon->buggy_app = getBooleanDockValue(value, dBuggyApplication);
1429 /* get position in the dock */
1430 value = WMGetFromPLDictionary(info, dPosition);
1431 if (value && WMIsPLString(value)) {
1432 if (sscanf(WMGetFromPLString(value), "%hi,%hi", &aicon->xindex, &aicon->yindex) != 2)
1433 wwarning(_("bad value in docked icon state info %s"), WMGetFromPLString(dPosition));
1435 /* check position sanity */
1436 /* incomplete section! */
1437 if (type == WM_DOCK) {
1438 aicon->xindex = 0;
1439 if (aicon->yindex < 0)
1440 wwarning(_("bad value in docked icon position %i,%i"),
1441 aicon->xindex, aicon->yindex);
1443 } else {
1444 aicon->yindex = index;
1445 aicon->xindex = 0;
1448 /* check if icon is omnipresent */
1449 value = WMGetFromPLDictionary(info, dOmnipresent);
1451 aicon->omnipresent = getBooleanDockValue(value, dOmnipresent);
1453 aicon->running = 0;
1454 aicon->docked = 1;
1456 return aicon;
1459 #define COMPLAIN(key) wwarning(_("bad value in dock state info:%s"), key)
1461 WAppIcon *wClipRestoreState(WScreen *scr, WMPropList *clip_state)
1463 WAppIcon *icon;
1464 WMPropList *value;
1466 icon = mainIconCreate(scr, WM_CLIP);
1468 if (!clip_state)
1469 return icon;
1471 WMRetainPropList(clip_state);
1473 /* restore position */
1475 value = WMGetFromPLDictionary(clip_state, dPosition);
1477 if (value) {
1478 if (!WMIsPLString(value)) {
1479 COMPLAIN("Position");
1480 } else {
1481 WMRect rect;
1482 int flags;
1484 if (sscanf(WMGetFromPLString(value), "%i,%i", &icon->x_pos, &icon->y_pos) != 2)
1485 COMPLAIN("Position");
1487 /* check position sanity */
1488 rect.pos.x = icon->x_pos;
1489 rect.pos.y = icon->y_pos;
1490 rect.size.width = rect.size.height = ICON_SIZE;
1492 wGetRectPlacementInfo(scr, rect, &flags);
1493 if (flags & (XFLAG_DEAD | XFLAG_PARTIAL))
1494 wScreenKeepInside(scr, &icon->x_pos, &icon->y_pos, ICON_SIZE, ICON_SIZE);
1497 #ifdef XDND /* was OFFIX */
1498 value = WMGetFromPLDictionary(clip_state, dDropCommand);
1499 if (value && WMIsPLString(value))
1500 icon->dnd_command = wstrdup(WMGetFromPLString(value));
1501 #endif
1503 value = WMGetFromPLDictionary(clip_state, dPasteCommand);
1504 if (value && WMIsPLString(value))
1505 icon->paste_command = wstrdup(WMGetFromPLString(value));
1507 WMReleasePropList(clip_state);
1509 return icon;
1512 WDock *wDockRestoreState(WScreen *scr, WMPropList *dock_state, int type)
1514 WDock *dock;
1515 WMPropList *apps;
1516 WMPropList *value;
1517 WAppIcon *aicon, *old_top;
1518 int count, i;
1520 dock = wDockCreate(scr, type);
1522 if (!dock_state)
1523 return dock;
1525 WMRetainPropList(dock_state);
1527 /* restore position */
1528 value = WMGetFromPLDictionary(dock_state, dPosition);
1529 if (value) {
1530 if (!WMIsPLString(value)) {
1531 COMPLAIN("Position");
1532 } else {
1533 WMRect rect;
1534 int flags;
1536 if (sscanf(WMGetFromPLString(value), "%i,%i", &dock->x_pos, &dock->y_pos) != 2)
1537 COMPLAIN("Position");
1539 /* check position sanity */
1540 rect.pos.x = dock->x_pos;
1541 rect.pos.y = dock->y_pos;
1542 rect.size.width = rect.size.height = ICON_SIZE;
1544 wGetRectPlacementInfo(scr, rect, &flags);
1545 if (flags & (XFLAG_DEAD | XFLAG_PARTIAL)) {
1546 int x = dock->x_pos;
1547 wScreenKeepInside(scr, &x, &dock->y_pos, ICON_SIZE, ICON_SIZE);
1550 /* Is this needed any more? */
1551 if (type == WM_CLIP) {
1552 if (dock->x_pos < 0) {
1553 dock->x_pos = 0;
1554 } else if (dock->x_pos > scr->scr_width - ICON_SIZE) {
1555 dock->x_pos = scr->scr_width - ICON_SIZE;
1557 } else {
1558 if (dock->x_pos >= 0) {
1559 dock->x_pos = DOCK_EXTRA_SPACE;
1560 dock->on_right_side = 0;
1561 } else {
1562 dock->x_pos = scr->scr_width - DOCK_EXTRA_SPACE - ICON_SIZE;
1563 dock->on_right_side = 1;
1569 /* restore lowered/raised state */
1570 dock->lowered = 0;
1572 value = WMGetFromPLDictionary(dock_state, dLowered);
1573 if (value) {
1574 if (!WMIsPLString(value)) {
1575 COMPLAIN("Lowered");
1576 } else {
1577 if (strcasecmp(WMGetFromPLString(value), "YES") == 0)
1578 dock->lowered = 1;
1582 /* restore collapsed state */
1583 dock->collapsed = 0;
1585 value = WMGetFromPLDictionary(dock_state, dCollapsed);
1586 if (value) {
1587 if (!WMIsPLString(value)) {
1588 COMPLAIN("Collapsed");
1589 } else {
1590 if (strcasecmp(WMGetFromPLString(value), "YES") == 0)
1591 dock->collapsed = 1;
1595 /* restore auto-collapsed state */
1596 value = WMGetFromPLDictionary(dock_state, dAutoCollapse);
1597 if (value) {
1598 if (!WMIsPLString(value)) {
1599 COMPLAIN("AutoCollapse");
1600 } else {
1601 if (strcasecmp(WMGetFromPLString(value), "YES") == 0) {
1602 dock->auto_collapse = 1;
1603 dock->collapsed = 1;
1608 /* restore auto-raise/lower state */
1609 value = WMGetFromPLDictionary(dock_state, dAutoRaiseLower);
1610 if (value) {
1611 if (!WMIsPLString(value)) {
1612 COMPLAIN("AutoRaiseLower");
1613 } else {
1614 if (strcasecmp(WMGetFromPLString(value), "YES") == 0)
1615 dock->auto_raise_lower = 1;
1619 /* restore attract icons state */
1620 dock->attract_icons = 0;
1622 value = WMGetFromPLDictionary(dock_state, dAutoAttractIcons);
1623 if (value) {
1624 if (!WMIsPLString(value)) {
1625 COMPLAIN("AutoAttractIcons");
1626 } else {
1627 if (strcasecmp(WMGetFromPLString(value), "YES") == 0)
1628 dock->attract_icons = 1;
1632 /* application list */
1635 WMPropList *tmp;
1636 char buffer[64];
1639 * When saving, it saves the dock state in
1640 * Applications and Applicationsnnn
1642 * When loading, it will first try Applicationsnnn.
1643 * If it does not exist, use Applications as default.
1646 snprintf(buffer, sizeof(buffer), "Applications%i", scr->scr_height);
1648 tmp = WMCreatePLString(buffer);
1649 apps = WMGetFromPLDictionary(dock_state, tmp);
1650 WMReleasePropList(tmp);
1652 if (!apps)
1653 apps = WMGetFromPLDictionary(dock_state, dApplications);
1656 if (!apps)
1657 goto finish;
1659 count = WMGetPropListItemCount(apps);
1660 if (count == 0)
1661 goto finish;
1663 old_top = dock->icon_array[0];
1665 /* dock->icon_count is set to 1 when dock is created.
1666 * Since Clip is already restored, we want to keep it so for clip,
1667 * but for dock we may change the default top tile, so we set it to 0.
1669 if (type == WM_DOCK)
1670 dock->icon_count = 0;
1672 for (i = 0; i < count; i++) {
1673 if (dock->icon_count >= dock->max_icons) {
1674 wwarning(_("there are too many icons stored in dock. Ignoring what doesn't fit"));
1675 break;
1678 value = WMGetFromPLArray(apps, i);
1679 aicon = restore_icon_state(scr, value, type, dock->icon_count);
1681 dock->icon_array[dock->icon_count] = aicon;
1683 if (aicon) {
1684 aicon->dock = dock;
1685 aicon->x_pos = dock->x_pos + (aicon->xindex * ICON_SIZE);
1686 aicon->y_pos = dock->y_pos + (aicon->yindex * ICON_SIZE);
1688 if (dock->lowered)
1689 ChangeStackingLevel(aicon->icon->core, WMNormalLevel);
1690 else
1691 ChangeStackingLevel(aicon->icon->core, WMDockLevel);
1693 wCoreConfigure(aicon->icon->core, aicon->x_pos, aicon->y_pos, 0, 0);
1694 if (!dock->collapsed)
1695 XMapWindow(dpy, aicon->icon->core->window);
1697 wRaiseFrame(aicon->icon->core);
1699 dock->icon_count++;
1700 } else if (dock->icon_count == 0 && type == WM_DOCK) {
1701 dock->icon_count++;
1705 /* if the first icon is not defined, use the default */
1706 if (dock->icon_array[0] == NULL) {
1707 /* update default icon */
1708 old_top->x_pos = dock->x_pos;
1709 old_top->y_pos = dock->y_pos;
1710 if (dock->lowered)
1711 ChangeStackingLevel(old_top->icon->core, WMNormalLevel);
1712 else
1713 ChangeStackingLevel(old_top->icon->core, WMDockLevel);
1715 dock->icon_array[0] = old_top;
1716 XMoveWindow(dpy, old_top->icon->core->window, dock->x_pos, dock->y_pos);
1717 /* we don't need to increment dock->icon_count here because it was
1718 * incremented in the loop above.
1720 } else if (old_top != dock->icon_array[0]) {
1721 if (old_top == scr->clip_icon)
1722 scr->clip_icon = dock->icon_array[0];
1724 wAppIconDestroy(old_top);
1727 finish:
1728 WMReleasePropList(dock_state);
1730 return dock;
1733 void wDockLaunchWithState(WDock *dock, WAppIcon *btn, WSavedState *state)
1735 if (btn && btn->command && !btn->running && !btn->launching) {
1736 btn->drop_launch = 0;
1737 btn->paste_launch = 0;
1739 btn->pid = execCommand(btn, btn->command, state);
1741 if (btn->pid > 0) {
1742 if (!btn->forced_dock && !btn->buggy_app) {
1743 btn->launching = 1;
1744 dockIconPaint(btn);
1747 } else {
1748 wfree(state);
1752 void wDockDoAutoLaunch(WDock *dock, int workspace)
1754 WAppIcon *btn;
1755 WSavedState *state;
1756 int i;
1758 for (i = 0; i < dock->max_icons; i++) {
1759 btn = dock->icon_array[i];
1760 if (!btn || !btn->auto_launch)
1761 continue;
1763 state = wmalloc(sizeof(WSavedState));
1764 state->workspace = workspace;
1765 /* TODO: this is klugy and is very difficult to understand
1766 * what's going on. Try to clean up */
1767 wDockLaunchWithState(dock, btn, state);
1771 #ifdef XDND /* was OFFIX */
1772 static WDock *findDock(WScreen *scr, XEvent *event, int *icon_pos)
1774 WDock *dock;
1775 int i;
1777 *icon_pos = -1;
1778 if ((dock = scr->dock) != NULL) {
1779 for (i = 0; i < dock->max_icons; i++) {
1780 if (dock->icon_array[i]
1781 && dock->icon_array[i]->icon->core->window == event->xclient.window) {
1782 *icon_pos = i;
1783 break;
1787 if (*icon_pos < 0 && (dock = scr->workspaces[scr->current_workspace]->clip) != NULL) {
1788 for (i = 0; i < dock->max_icons; i++) {
1789 if (dock->icon_array[i]
1790 && dock->icon_array[i]->icon->core->window == event->xclient.window) {
1791 *icon_pos = i;
1792 break;
1796 if (*icon_pos >= 0)
1797 return dock;
1798 return NULL;
1801 int wDockReceiveDNDDrop(WScreen *scr, XEvent *event)
1803 WDock *dock;
1804 WAppIcon *btn;
1805 int icon_pos;
1807 dock = findDock(scr, event, &icon_pos);
1808 if (!dock)
1809 return False;
1812 * Return True if the drop was on an application icon window.
1813 * In this case, let the ClientMessage handler redirect the
1814 * message to the app.
1816 if (dock->icon_array[icon_pos]->icon->icon_win != None)
1817 return True;
1819 if (dock->icon_array[icon_pos]->dnd_command != NULL) {
1820 scr->flags.dnd_data_convertion_status = 0;
1822 btn = dock->icon_array[icon_pos];
1824 if (!btn->forced_dock) {
1825 btn->relaunching = btn->running;
1826 btn->running = 1;
1828 if (btn->wm_instance || btn->wm_class) {
1829 WWindowAttributes attr;
1830 memset(&attr, 0, sizeof(WWindowAttributes));
1831 wDefaultFillAttributes(btn->wm_instance, btn->wm_class, &attr, NULL, True);
1833 if (!attr.no_appicon)
1834 btn->launching = 1;
1835 else
1836 btn->running = 0;
1839 btn->paste_launch = 0;
1840 btn->drop_launch = 1;
1841 scr->last_dock = dock;
1842 btn->pid = execCommand(btn, btn->dnd_command, NULL);
1843 if (btn->pid > 0) {
1844 dockIconPaint(btn);
1845 } else {
1846 btn->launching = 0;
1847 if (!btn->relaunching)
1848 btn->running = 0;
1851 return False;
1853 #endif /* XDND */
1855 Bool wDockAttachIcon(WDock *dock, WAppIcon *icon, int x, int y, Bool update_icon)
1857 WWindow *wwin;
1858 Bool lupdate_icon = False;
1859 char *command = NULL;
1860 int index;
1862 icon->editing = 0;
1864 if (update_icon)
1865 lupdate_icon = True;
1867 if (icon->command == NULL) {
1868 /* If icon->owner exists, it means the application is running */
1869 if (icon->icon->owner) {
1870 wwin = icon->icon->owner;
1871 command = GetCommandForWindow(wwin->client_win);
1874 if (command) {
1875 icon->command = command;
1876 } else {
1877 /* icon->forced_dock = 1; */
1878 if (dock->type != WM_CLIP || !icon->attracted) {
1879 icon->editing = 1;
1880 if (wInputDialog(dock->screen_ptr, _("Dock Icon"),
1881 _("Type the command used to launch the application"), &command)) {
1882 if (command && (command[0] == 0 || (command[0] == '-' && command[1] == 0))) {
1883 wfree(command);
1884 command = NULL;
1886 icon->command = command;
1887 icon->editing = 0;
1888 } else {
1889 icon->editing = 0;
1890 if (command)
1891 wfree(command);
1892 /* If the target is the dock, reject the icon. If
1893 * the target is the clip, make it an attracted icon
1895 if (dock->type == WM_CLIP) {
1896 icon->attracted = 1;
1897 if (!icon->icon->shadowed) {
1898 icon->icon->shadowed = 1;
1899 lupdate_icon = True;
1901 } else {
1902 return False;
1909 for (index = 1; index < dock->max_icons; index++)
1910 if (dock->icon_array[index] == NULL)
1911 break;
1912 /* if (index == dock->max_icons)
1913 return; */
1915 assert(index < dock->max_icons);
1917 dock->icon_array[index] = icon;
1918 icon->yindex = y;
1919 icon->xindex = x;
1921 icon->omnipresent = 0;
1923 icon->x_pos = dock->x_pos + x * ICON_SIZE;
1924 icon->y_pos = dock->y_pos + y * ICON_SIZE;
1926 dock->icon_count++;
1928 icon->running = 1;
1929 icon->launching = 0;
1930 icon->docked = 1;
1931 icon->dock = dock;
1932 icon->icon->core->descriptor.handle_mousedown = iconMouseDown;
1933 if (dock->type == WM_CLIP) {
1934 icon->icon->core->descriptor.handle_enternotify = clipEnterNotify;
1935 icon->icon->core->descriptor.handle_leavenotify = clipLeaveNotify;
1937 icon->icon->core->descriptor.parent_type = WCLASS_DOCK_ICON;
1938 icon->icon->core->descriptor.parent = icon;
1940 MoveInStackListUnder(dock->icon_array[index - 1]->icon->core, icon->icon->core);
1941 wAppIconMove(icon, icon->x_pos, icon->y_pos);
1943 /* Update the icon images */
1944 if (lupdate_icon)
1945 wIconUpdate(icon->icon);
1947 /* Paint it */
1948 wAppIconPaint(icon);
1950 /* Save it */
1951 save_appicon(icon, True);
1953 if (wPreferences.auto_arrange_icons)
1954 wArrangeIcons(dock->screen_ptr, True);
1956 #ifdef XDND /* was OFFIX */
1957 if (icon->command && !icon->dnd_command) {
1958 int len = strlen(icon->command) + 8;
1959 icon->dnd_command = wmalloc(len);
1960 snprintf(icon->dnd_command, len, "%s %%d", icon->command);
1962 #endif
1964 if (icon->command && !icon->paste_command) {
1965 int len = strlen(icon->command) + 8;
1966 icon->paste_command = wmalloc(len);
1967 snprintf(icon->paste_command, len, "%s %%s", icon->command);
1970 return True;
1973 static void reattachIcon(WDock *dock, WAppIcon *icon, int x, int y)
1975 int index;
1977 for (index = 1; index < dock->max_icons; index++) {
1978 if (dock->icon_array[index] == icon)
1979 break;
1981 assert(index < dock->max_icons);
1983 icon->yindex = y;
1984 icon->xindex = x;
1986 icon->x_pos = dock->x_pos + x * ICON_SIZE;
1987 icon->y_pos = dock->y_pos + y * ICON_SIZE;
1990 static Bool moveIconBetweenDocks(WDock *src, WDock *dest, WAppIcon *icon, int x, int y)
1992 WWindow *wwin;
1993 char *command = NULL;
1994 int index;
1995 Bool update_icon = False;
1997 if (src == dest)
1998 return True; /* No move needed, we're already there */
2000 if (dest == NULL)
2001 return False;
2004 * For the moment we can't do this if we move icons in Clip from one
2005 * workspace to other, because if we move two or more icons without
2006 * command, the dialog box will not be able to tell us to which of the
2007 * moved icons it applies. -Dan
2009 if ((dest->type == WM_DOCK /*|| dest->keep_attracted */ ) && icon->command == NULL) {
2010 /* If icon->owner exists, it means the application is running */
2011 if (icon->icon->owner) {
2012 wwin = icon->icon->owner;
2013 command = GetCommandForWindow(wwin->client_win);
2016 if (command) {
2017 icon->command = command;
2018 } else {
2019 icon->editing = 1;
2020 /* icon->forced_dock = 1; */
2021 if (wInputDialog(src->screen_ptr, _("Dock Icon"),
2022 _("Type the command used to launch the application"), &command)) {
2023 if (command && (command[0] == 0 || (command[0] == '-' && command[1] == 0))) {
2024 wfree(command);
2025 command = NULL;
2027 icon->command = command;
2028 } else {
2029 icon->editing = 0;
2030 if (command)
2031 wfree(command);
2032 return False;
2034 icon->editing = 0;
2038 if (dest->type == WM_DOCK)
2039 wClipMakeIconOmnipresent(icon, False);
2041 for (index = 1; index < src->max_icons; index++) {
2042 if (src->icon_array[index] == icon)
2043 break;
2045 assert(index < src->max_icons);
2047 src->icon_array[index] = NULL;
2048 src->icon_count--;
2050 for (index = 1; index < dest->max_icons; index++) {
2051 if (dest->icon_array[index] == NULL)
2052 break;
2055 assert(index < dest->max_icons);
2057 dest->icon_array[index] = icon;
2058 icon->dock = dest;
2060 /* deselect the icon */
2061 if (icon->icon->selected)
2062 wIconSelect(icon->icon);
2064 if (dest->type == WM_DOCK) {
2065 icon->icon->core->descriptor.handle_enternotify = NULL;
2066 icon->icon->core->descriptor.handle_leavenotify = NULL;
2067 } else {
2068 icon->icon->core->descriptor.handle_enternotify = clipEnterNotify;
2069 icon->icon->core->descriptor.handle_leavenotify = clipLeaveNotify;
2072 /* set it to be kept when moving to dock.
2073 * Unless the icon does not have a command set
2075 if (icon->command && dest->type == WM_DOCK) {
2076 icon->attracted = 0;
2077 if (icon->icon->shadowed) {
2078 icon->icon->shadowed = 0;
2079 update_icon = True;
2081 save_appicon(icon, True);
2084 if (src->auto_collapse || src->auto_raise_lower)
2085 clipLeave(src);
2087 icon->yindex = y;
2088 icon->xindex = x;
2090 icon->x_pos = dest->x_pos + x * ICON_SIZE;
2091 icon->y_pos = dest->y_pos + y * ICON_SIZE;
2093 dest->icon_count++;
2095 MoveInStackListUnder(dest->icon_array[index - 1]->icon->core, icon->icon->core);
2097 /* Update the icon images */
2098 if (update_icon)
2099 wIconUpdate(icon->icon);
2101 /* Paint it */
2102 wAppIconPaint(icon);
2104 return True;
2107 void wDockDetach(WDock *dock, WAppIcon *icon)
2109 int index;
2110 Bool update_icon = False;
2112 /* make the settings panel be closed */
2113 if (icon->panel)
2114 DestroyDockAppSettingsPanel(icon->panel);
2116 /* This must be called before icon->dock is set to NULL.
2117 * Don't move it. -Dan
2119 wClipMakeIconOmnipresent(icon, False);
2121 icon->docked = 0;
2122 icon->dock = NULL;
2123 icon->attracted = 0;
2124 icon->auto_launch = 0;
2125 if (icon->icon->shadowed) {
2126 icon->icon->shadowed = 0;
2127 update_icon = True;
2130 /* deselect the icon */
2131 if (icon->icon->selected)
2132 wIconSelect(icon->icon);
2134 if (icon->command) {
2135 wfree(icon->command);
2136 icon->command = NULL;
2138 #ifdef XDND /* was OFFIX */
2139 if (icon->dnd_command) {
2140 wfree(icon->dnd_command);
2141 icon->dnd_command = NULL;
2143 #endif
2144 if (icon->paste_command) {
2145 wfree(icon->paste_command);
2146 icon->paste_command = NULL;
2149 for (index = 1; index < dock->max_icons; index++)
2150 if (dock->icon_array[index] == icon)
2151 break;
2153 assert(index < dock->max_icons);
2154 dock->icon_array[index] = NULL;
2155 icon->yindex = -1;
2156 icon->xindex = -1;
2158 dock->icon_count--;
2160 /* if the dock is not attached to an application or
2161 * the application did not set the appropriate hints yet,
2162 * destroy the icon */
2163 if (!icon->running || !wApplicationOf(icon->main_window)) {
2164 wAppIconDestroy(icon);
2165 } else {
2166 icon->icon->core->descriptor.handle_mousedown = appIconMouseDown;
2167 icon->icon->core->descriptor.handle_enternotify = NULL;
2168 icon->icon->core->descriptor.handle_leavenotify = NULL;
2169 icon->icon->core->descriptor.parent_type = WCLASS_APPICON;
2170 icon->icon->core->descriptor.parent = icon;
2172 ChangeStackingLevel(icon->icon->core, NORMAL_ICON_LEVEL);
2174 /* Update the icon images */
2175 if (update_icon)
2176 wIconUpdate(icon->icon);
2178 /* Paint it */
2179 wAppIconPaint(icon);
2181 if (wPreferences.auto_arrange_icons)
2182 wArrangeIcons(dock->screen_ptr, True);
2184 if (dock->auto_collapse || dock->auto_raise_lower)
2185 clipLeave(dock);
2189 * returns the closest Dock slot index for the passed
2190 * coordinates.
2192 * Returns False if icon can't be docked.
2194 * Note: this function should NEVER alter ret_x or ret_y, unless it will
2195 * return True. -Dan
2197 Bool wDockSnapIcon(WDock *dock, WAppIcon *icon, int req_x, int req_y, int *ret_x, int *ret_y, int redocking)
2199 WScreen *scr = dock->screen_ptr;
2200 int dx, dy;
2201 int ex_x, ex_y;
2202 int i, offset = ICON_SIZE / 2;
2203 WAppIcon *aicon = NULL;
2204 WAppIcon *nicon = NULL;
2205 int max_y_icons;
2207 /* TODO: XINERAMA, for these */
2208 max_y_icons = scr->scr_height / ICON_SIZE - 1;
2210 if (wPreferences.flags.noupdates)
2211 return False;
2213 dx = dock->x_pos;
2214 dy = dock->y_pos;
2216 /* if the dock is full */
2217 if (!redocking && (dock->icon_count >= dock->max_icons))
2218 return False;
2220 /* exact position */
2221 if (req_y < dy)
2222 ex_y = (req_y - offset - dy) / ICON_SIZE;
2223 else
2224 ex_y = (req_y + offset - dy) / ICON_SIZE;
2226 if (req_x < dx)
2227 ex_x = (req_x - offset - dx) / ICON_SIZE;
2228 else
2229 ex_x = (req_x + offset - dx) / ICON_SIZE;
2231 /* check if the icon is outside the screen boundaries */
2233 WMRect rect;
2234 int flags;
2236 rect.pos.x = dx + ex_x * ICON_SIZE;
2237 rect.pos.y = dy + ex_y * ICON_SIZE;
2238 rect.size.width = rect.size.height = ICON_SIZE;
2240 wGetRectPlacementInfo(scr, rect, &flags);
2241 if (flags & (XFLAG_DEAD | XFLAG_PARTIAL))
2242 return False;
2245 if (dock->type == WM_DOCK) {
2246 if (icon->dock != dock && ex_x != 0)
2247 return False;
2249 aicon = NULL;
2250 for (i = 0; i < dock->max_icons; i++) {
2251 nicon = dock->icon_array[i];
2252 if (nicon && nicon->yindex == ex_y) {
2253 aicon = nicon;
2254 break;
2258 if (redocking) {
2259 int sig, done, closest;
2261 /* Possible cases when redocking:
2263 * icon dragged out of range of any slot -> false
2264 * icon dragged to range of free slot
2265 * icon dragged to range of same slot
2266 * icon dragged to range of different icon
2268 if (abs(ex_x) > DOCK_DETTACH_THRESHOLD)
2269 return False;
2271 if (ex_y >= 0 && ex_y <= max_y_icons && (aicon == icon || !aicon)) {
2272 *ret_x = 0;
2273 *ret_y = ex_y;
2274 return True;
2277 /* start looking at the upper slot or lower? */
2278 if (ex_y * ICON_SIZE < (req_y + offset - dy))
2279 sig = 1;
2280 else
2281 sig = -1;
2283 closest = -1;
2284 done = 0;
2285 /* look for closest free slot */
2286 for (i = 0; i < (DOCK_DETTACH_THRESHOLD + 1) * 2 && !done; i++) {
2287 int j;
2289 done = 1;
2290 closest = sig * (i / 2) + ex_y;
2291 /* check if this slot is used */
2292 if (closest >= 0) {
2293 for (j = 0; j < dock->max_icons; j++) {
2294 if (dock->icon_array[j]
2295 && dock->icon_array[j]->yindex == closest) {
2296 /* slot is used by someone else */
2297 if (dock->icon_array[j] != icon)
2298 done = 0;
2299 break;
2303 sig = -sig;
2305 if (done && closest >= 0 && closest <= max_y_icons &&
2306 ((ex_y >= closest && ex_y - closest < DOCK_DETTACH_THRESHOLD + 1)
2307 || (ex_y < closest && closest - ex_y <= DOCK_DETTACH_THRESHOLD + 1))) {
2308 *ret_x = 0;
2309 *ret_y = closest;
2310 return True;
2312 } else { /* !redocking */
2314 /* if slot is free and the icon is close enough, return it */
2315 if (!aicon && ex_x == 0 && ex_y >= 0 && ex_y <= max_y_icons) {
2316 *ret_x = 0;
2317 *ret_y = ex_y;
2318 return True;
2321 } else { /* CLIP */
2322 int neighbours = 0;
2323 int start, stop, k;
2325 start = icon->omnipresent ? 0 : scr->current_workspace;
2326 stop = icon->omnipresent ? scr->workspace_count : start + 1;
2328 aicon = NULL;
2329 for (k = start; k < stop; k++) {
2330 WDock *tmp = scr->workspaces[k]->clip;
2331 if (!tmp)
2332 continue;
2333 for (i = 0; i < tmp->max_icons; i++) {
2334 nicon = tmp->icon_array[i];
2335 if (nicon && nicon->xindex == ex_x && nicon->yindex == ex_y) {
2336 aicon = nicon;
2337 break;
2340 if (aicon)
2341 break;
2343 for (k = start; k < stop; k++) {
2344 WDock *tmp = scr->workspaces[k]->clip;
2345 if (!tmp)
2346 continue;
2347 for (i = 0; i < tmp->max_icons; i++) {
2348 nicon = tmp->icon_array[i];
2349 if (nicon && nicon != icon && /* Icon can't be it's own neighbour */
2350 (abs(nicon->xindex - ex_x) <= CLIP_ATTACH_VICINITY &&
2351 abs(nicon->yindex - ex_y) <= CLIP_ATTACH_VICINITY)) {
2352 neighbours = 1;
2353 break;
2356 if (neighbours)
2357 break;
2360 if (neighbours && (aicon == NULL || (redocking && aicon == icon))) {
2361 *ret_x = ex_x;
2362 *ret_y = ex_y;
2363 return True;
2366 return False;
2369 static int onScreen(WScreen *scr, int x, int y, int sx, int ex, int sy, int ey)
2371 WMRect rect = wmkrect(x, y, ICON_SIZE, ICON_SIZE);
2372 int flags;
2374 wGetRectPlacementInfo(scr, rect, &flags);
2376 return !(flags & (XFLAG_DEAD | XFLAG_PARTIAL));
2380 * returns true if it can find a free slot in the dock,
2381 * in which case it changes x_pos and y_pos accordingly.
2382 * Else returns false.
2384 Bool wDockFindFreeSlot(WDock *dock, int *x_pos, int *y_pos)
2386 WScreen *scr = dock->screen_ptr;
2387 WAppIcon *btn;
2388 WAppIconChain *chain;
2389 unsigned char *slot_map;
2390 int mwidth;
2391 int r;
2392 int x, y;
2393 int i, done = False;
2394 int corner;
2395 int sx = 0, sy = 0, ex = scr->scr_width, ey = scr->scr_height;
2396 int extra_count = 0;
2398 if (dock->type == WM_CLIP && dock != scr->workspaces[scr->current_workspace]->clip)
2399 extra_count = scr->global_icon_count;
2401 /* if the dock is full */
2402 if (dock->icon_count + extra_count >= dock->max_icons)
2403 return False;
2405 if (!wPreferences.flags.nodock && scr->dock) {
2406 if (scr->dock->on_right_side)
2407 ex -= ICON_SIZE + DOCK_EXTRA_SPACE;
2408 else
2409 sx += ICON_SIZE + DOCK_EXTRA_SPACE;
2412 if (ex < dock->x_pos)
2413 ex = dock->x_pos;
2414 if (sx > dock->x_pos + ICON_SIZE)
2415 sx = dock->x_pos + ICON_SIZE;
2416 #define C_NONE 0
2417 #define C_NW 1
2418 #define C_NE 2
2419 #define C_SW 3
2420 #define C_SE 4
2422 /* check if clip is in a corner */
2423 if (dock->type == WM_CLIP) {
2424 if (dock->x_pos < 1 && dock->y_pos < 1)
2425 corner = C_NE;
2426 else if (dock->x_pos < 1 && dock->y_pos >= (ey - ICON_SIZE))
2427 corner = C_SE;
2428 else if (dock->x_pos >= (ex - ICON_SIZE) && dock->y_pos >= (ey - ICON_SIZE))
2429 corner = C_SW;
2430 else if (dock->x_pos >= (ex - ICON_SIZE) && dock->y_pos < 1)
2431 corner = C_NW;
2432 else
2433 corner = C_NONE;
2434 } else {
2435 corner = C_NONE;
2438 /* If the clip is in the corner, use only slots that are in the border
2439 * of the screen */
2440 if (corner != C_NONE) {
2441 char *hmap, *vmap;
2442 int hcount, vcount;
2444 hcount = WMIN(dock->max_icons, scr->scr_width / ICON_SIZE);
2445 vcount = WMIN(dock->max_icons, scr->scr_height / ICON_SIZE);
2446 hmap = wmalloc(hcount + 1);
2447 vmap = wmalloc(vcount + 1);
2449 /* mark used positions */
2450 switch (corner) {
2451 case C_NE:
2452 for (i = 0; i < dock->max_icons; i++) {
2453 btn = dock->icon_array[i];
2454 if (!btn)
2455 continue;
2457 if (btn->xindex == 0 && btn->yindex > 0 && btn->yindex < vcount)
2458 vmap[btn->yindex] = 1;
2459 else if (btn->yindex == 0 && btn->xindex > 0 && btn->xindex < hcount)
2460 hmap[btn->xindex] = 1;
2462 for (chain = scr->global_icons; chain != NULL; chain = chain->next) {
2463 btn = chain->aicon;
2464 if (btn->xindex == 0 && btn->yindex > 0 && btn->yindex < vcount)
2465 vmap[btn->yindex] = 1;
2466 else if (btn->yindex == 0 && btn->xindex > 0 && btn->xindex < hcount)
2467 hmap[btn->xindex] = 1;
2469 break;
2470 case C_NW:
2471 for (i = 0; i < dock->max_icons; i++) {
2472 btn = dock->icon_array[i];
2473 if (!btn)
2474 continue;
2476 if (btn->xindex == 0 && btn->yindex > 0 && btn->yindex < vcount)
2477 vmap[btn->yindex] = 1;
2478 else if (btn->yindex == 0 && btn->xindex < 0 && btn->xindex > -hcount)
2479 hmap[-btn->xindex] = 1;
2481 for (chain = scr->global_icons; chain != NULL; chain = chain->next) {
2482 btn = chain->aicon;
2483 if (btn->xindex == 0 && btn->yindex > 0 && btn->yindex < vcount)
2484 vmap[btn->yindex] = 1;
2485 else if (btn->yindex == 0 && btn->xindex < 0 && btn->xindex > -hcount)
2486 hmap[-btn->xindex] = 1;
2488 break;
2489 case C_SE:
2490 for (i = 0; i < dock->max_icons; i++) {
2491 btn = dock->icon_array[i];
2492 if (!btn)
2493 continue;
2495 if (btn->xindex == 0 && btn->yindex < 0 && btn->yindex > -vcount)
2496 vmap[-btn->yindex] = 1;
2497 else if (btn->yindex == 0 && btn->xindex > 0 && btn->xindex < hcount)
2498 hmap[btn->xindex] = 1;
2500 for (chain = scr->global_icons; chain != NULL; chain = chain->next) {
2501 btn = chain->aicon;
2502 if (btn->xindex == 0 && btn->yindex < 0 && btn->yindex > -vcount)
2503 vmap[-btn->yindex] = 1;
2504 else if (btn->yindex == 0 && btn->xindex > 0 && btn->xindex < hcount)
2505 hmap[btn->xindex] = 1;
2507 break;
2508 case C_SW:
2509 default:
2510 for (i = 0; i < dock->max_icons; i++) {
2511 btn = dock->icon_array[i];
2512 if (!btn)
2513 continue;
2515 if (btn->xindex == 0 && btn->yindex < 0 && btn->yindex > -vcount)
2516 vmap[-btn->yindex] = 1;
2517 else if (btn->yindex == 0 && btn->xindex < 0 && btn->xindex > -hcount)
2518 hmap[-btn->xindex] = 1;
2520 for (chain = scr->global_icons; chain != NULL; chain = chain->next) {
2521 btn = chain->aicon;
2522 if (btn->xindex == 0 && btn->yindex < 0 && btn->yindex > -vcount)
2523 vmap[-btn->yindex] = 1;
2524 else if (btn->yindex == 0 && btn->xindex < 0 && btn->xindex > -hcount)
2525 hmap[-btn->xindex] = 1;
2528 x = 0;
2529 y = 0;
2530 done = 0;
2531 /* search a vacant slot */
2532 for (i = 1; i < WMAX(vcount, hcount); i++) {
2533 if (i < vcount && vmap[i] == 0) {
2534 /* found a slot */
2535 x = 0;
2536 y = i;
2537 done = 1;
2538 break;
2539 } else if (i < hcount && hmap[i] == 0) {
2540 /* found a slot */
2541 x = i;
2542 y = 0;
2543 done = 1;
2544 break;
2547 wfree(vmap);
2548 wfree(hmap);
2549 /* If found a slot, translate and return */
2550 if (done) {
2551 if (corner == C_NW || corner == C_NE)
2552 *y_pos = y;
2553 else
2554 *y_pos = -y;
2556 if (corner == C_NE || corner == C_SE)
2557 *x_pos = x;
2558 else
2559 *x_pos = -x;
2561 return True;
2563 /* else, try to find a slot somewhere else */
2566 /* a map of mwidth x mwidth would be enough if we allowed icons to be
2567 * placed outside of screen */
2568 mwidth = (int)ceil(sqrt(dock->max_icons));
2570 /* In the worst case (the clip is in the corner of the screen),
2571 * the amount of icons that fit in the clip is smaller.
2572 * Double the map to get a safe value.
2574 mwidth += mwidth;
2576 r = (mwidth - 1) / 2;
2578 slot_map = wmalloc(mwidth * mwidth);
2580 #define XY2OFS(x,y) (WMAX(abs(x),abs(y)) > r) ? 0 : (((y)+r)*(mwidth)+(x)+r)
2582 /* mark used slots in the map. If the slot falls outside the map
2583 * (for example, when all icons are placed in line), ignore them. */
2584 for (i = 0; i < dock->max_icons; i++) {
2585 btn = dock->icon_array[i];
2586 if (btn)
2587 slot_map[XY2OFS(btn->xindex, btn->yindex)] = 1;
2590 for (chain = scr->global_icons; chain != NULL; chain = chain->next)
2591 slot_map[XY2OFS(chain->aicon->xindex, chain->aicon->yindex)] = 1;
2593 /* Find closest slot from the center that is free by scanning the
2594 * map from the center to outward in circular passes.
2595 * This will not result in a neat layout, but will be optimal
2596 * in the sense that there will not be holes left.
2598 done = 0;
2599 for (i = 1; i <= r && !done; i++) {
2600 int tx, ty;
2602 /* top and bottom parts of the ring */
2603 for (x = -i; x <= i && !done; x++) {
2604 tx = dock->x_pos + x * ICON_SIZE;
2605 y = -i;
2606 ty = dock->y_pos + y * ICON_SIZE;
2607 if (slot_map[XY2OFS(x, y)] == 0 && onScreen(scr, tx, ty, sx, ex, sy, ey)) {
2608 *x_pos = x;
2609 *y_pos = y;
2610 done = 1;
2611 break;
2613 y = i;
2614 ty = dock->y_pos + y * ICON_SIZE;
2615 if (slot_map[XY2OFS(x, y)] == 0 && onScreen(scr, tx, ty, sx, ex, sy, ey)) {
2616 *x_pos = x;
2617 *y_pos = y;
2618 done = 1;
2619 break;
2622 /* left and right parts of the ring */
2623 for (y = -i + 1; y <= i - 1; y++) {
2624 ty = dock->y_pos + y * ICON_SIZE;
2625 x = -i;
2626 tx = dock->x_pos + x * ICON_SIZE;
2627 if (slot_map[XY2OFS(x, y)] == 0 && onScreen(scr, tx, ty, sx, ex, sy, ey)) {
2628 *x_pos = x;
2629 *y_pos = y;
2630 done = 1;
2631 break;
2633 x = i;
2634 tx = dock->x_pos + x * ICON_SIZE;
2635 if (slot_map[XY2OFS(x, y)] == 0 && onScreen(scr, tx, ty, sx, ex, sy, ey)) {
2636 *x_pos = x;
2637 *y_pos = y;
2638 done = 1;
2639 break;
2643 wfree(slot_map);
2644 #undef XY2OFS
2645 return done;
2648 static void moveDock(WDock *dock, int new_x, int new_y)
2650 WAppIcon *btn;
2651 int i;
2653 dock->x_pos = new_x;
2654 dock->y_pos = new_y;
2655 for (i = 0; i < dock->max_icons; i++) {
2656 btn = dock->icon_array[i];
2657 if (btn) {
2658 btn->x_pos = new_x + btn->xindex * ICON_SIZE;
2659 btn->y_pos = new_y + btn->yindex * ICON_SIZE;
2660 XMoveWindow(dpy, btn->icon->core->window, btn->x_pos, btn->y_pos);
2665 static void swapDock(WDock *dock)
2667 WScreen *scr = dock->screen_ptr;
2668 WAppIcon *btn;
2669 int x, i;
2671 if (dock->on_right_side)
2672 x = dock->x_pos = scr->scr_width - ICON_SIZE - DOCK_EXTRA_SPACE;
2673 else
2674 x = dock->x_pos = DOCK_EXTRA_SPACE;
2676 for (i = 0; i < dock->max_icons; i++) {
2677 btn = dock->icon_array[i];
2678 if (btn) {
2679 btn->x_pos = x;
2680 XMoveWindow(dpy, btn->icon->core->window, btn->x_pos, btn->y_pos);
2684 wScreenUpdateUsableArea(scr);
2687 static pid_t execCommand(WAppIcon *btn, char *command, WSavedState *state)
2689 WScreen *scr = btn->icon->core->screen_ptr;
2690 pid_t pid;
2691 char **argv;
2692 int argc;
2693 char *cmdline;
2695 cmdline = ExpandOptions(scr, command);
2697 if (scr->flags.dnd_data_convertion_status || !cmdline) {
2698 if (cmdline)
2699 wfree(cmdline);
2700 if (state)
2701 wfree(state);
2702 return 0;
2705 wtokensplit(cmdline, &argv, &argc);
2707 if (!argc) {
2708 if (cmdline)
2709 wfree(cmdline);
2710 if (state)
2711 wfree(state);
2712 return 0;
2715 if ((pid = fork()) == 0) {
2716 char **args;
2717 int i;
2719 SetupEnvironment(scr);
2721 #ifdef HAVE_SETSID
2722 setsid();
2723 #endif
2725 args = malloc(sizeof(char *) * (argc + 1));
2726 if (!args)
2727 exit(111);
2729 for (i = 0; i < argc; i++)
2730 args[i] = argv[i];
2732 args[argc] = NULL;
2733 execvp(argv[0], args);
2734 exit(111);
2736 wtokenfree(argv, argc);
2738 if (pid > 0) {
2739 if (!state) {
2740 state = wmalloc(sizeof(WSavedState));
2741 state->hidden = -1;
2742 state->miniaturized = -1;
2743 state->shaded = -1;
2744 if (btn->dock == scr->dock || btn->omnipresent)
2745 state->workspace = -1;
2746 else
2747 state->workspace = scr->current_workspace;
2749 wWindowAddSavedState(btn->wm_instance, btn->wm_class, cmdline, pid, state);
2750 wAddDeathHandler(pid, (WDeathHandler *) trackDeadProcess, btn->dock);
2751 } else if (state) {
2752 wfree(state);
2754 wfree(cmdline);
2755 return pid;
2758 void wDockHideIcons(WDock *dock)
2760 int i;
2762 if (dock == NULL)
2763 return;
2765 for (i = 1; i < dock->max_icons; i++) {
2766 if (dock->icon_array[i])
2767 XUnmapWindow(dpy, dock->icon_array[i]->icon->core->window);
2769 dock->mapped = 0;
2771 dockIconPaint(dock->icon_array[0]);
2774 void wDockShowIcons(WDock *dock)
2776 int i, newlevel;
2777 WAppIcon *btn;
2779 if (dock == NULL)
2780 return;
2782 btn = dock->icon_array[0];
2783 moveDock(dock, btn->x_pos, btn->y_pos);
2785 newlevel = dock->lowered ? WMNormalLevel : WMDockLevel;
2786 ChangeStackingLevel(btn->icon->core, newlevel);
2788 for (i = 1; i < dock->max_icons; i++) {
2789 if (dock->icon_array[i]) {
2790 MoveInStackListAbove(dock->icon_array[i]->icon->core, btn->icon->core);
2791 break;
2795 if (!dock->collapsed) {
2796 for (i = 1; i < dock->max_icons; i++) {
2797 if (dock->icon_array[i])
2798 XMapWindow(dpy, dock->icon_array[i]->icon->core->window);
2801 dock->mapped = 1;
2803 dockIconPaint(btn);
2806 void wDockLower(WDock *dock)
2808 int i;
2810 for (i = 0; i < dock->max_icons; i++) {
2811 if (dock->icon_array[i])
2812 wLowerFrame(dock->icon_array[i]->icon->core);
2816 void wDockRaise(WDock *dock)
2818 int i;
2820 for (i = dock->max_icons - 1; i >= 0; i--) {
2821 if (dock->icon_array[i])
2822 wRaiseFrame(dock->icon_array[i]->icon->core);
2826 void wDockRaiseLower(WDock *dock)
2828 if (!dock->icon_array[0]->icon->core->stacking->above
2829 || (dock->icon_array[0]->icon->core->stacking->window_level
2830 != dock->icon_array[0]->icon->core->stacking->above->stacking->window_level))
2831 wDockLower(dock);
2832 else
2833 wDockRaise(dock);
2836 void wDockFinishLaunch(WDock *dock, WAppIcon *icon)
2838 icon->launching = 0;
2839 icon->relaunching = 0;
2840 dockIconPaint(icon);
2843 WAppIcon *wDockFindIconForWindow(WDock *dock, Window window)
2845 WAppIcon *icon;
2846 int i;
2848 for (i = 0; i < dock->max_icons; i++) {
2849 icon = dock->icon_array[i];
2850 if (icon && icon->main_window == window)
2851 return icon;
2853 return NULL;
2856 void wDockTrackWindowLaunch(WDock *dock, Window window)
2858 WAppIcon *icon;
2859 char *wm_class, *wm_instance;
2860 int i;
2861 Bool firstPass = True;
2862 Bool found = False;
2863 char *command = NULL;
2865 if (!PropGetWMClass(window, &wm_class, &wm_instance) || (!wm_class && !wm_instance))
2866 return;
2868 command = GetCommandForWindow(window);
2869 retry:
2870 for (i = 0; i < dock->max_icons; i++) {
2871 icon = dock->icon_array[i];
2872 if (!icon)
2873 continue;
2875 /* app is already attached to icon */
2876 if (icon->main_window == window) {
2877 found = True;
2878 break;
2881 if ((icon->wm_instance || icon->wm_class)
2882 && (icon->launching || !icon->running)) {
2884 if (icon->wm_instance && wm_instance && strcmp(icon->wm_instance, wm_instance) != 0)
2885 continue;
2887 if (icon->wm_class && wm_class && strcmp(icon->wm_class, wm_class) != 0)
2888 continue;
2890 if (firstPass && command && strcmp(icon->command, command) != 0)
2891 continue;
2893 if (!icon->relaunching) {
2894 WApplication *wapp;
2896 /* Possibly an application that was docked with dockit,
2897 * but the user did not update WMState to indicate that
2898 * it was docked by force */
2899 wapp = wApplicationOf(window);
2900 if (!wapp) {
2901 icon->forced_dock = 1;
2902 icon->running = 0;
2904 if (!icon->forced_dock)
2905 icon->main_window = window;
2907 found = True;
2908 if (!wPreferences.no_animations && !icon->launching &&
2909 !dock->screen_ptr->flags.startup && !dock->collapsed) {
2910 WAppIcon *aicon;
2911 int x0, y0;
2913 icon->launching = 1;
2914 dockIconPaint(icon);
2916 aicon = wAppIconCreateForDock(dock->screen_ptr, NULL,
2917 wm_instance, wm_class, TILE_NORMAL);
2918 /* XXX: can: aicon->icon == NULL ? */
2919 PlaceIcon(dock->screen_ptr, &x0, &y0, wGetHeadForWindow(aicon->icon->owner));
2920 wAppIconMove(aicon, x0, y0);
2921 /* Should this always be lowered? -Dan */
2922 if (dock->lowered)
2923 wLowerFrame(aicon->icon->core);
2924 XMapWindow(dpy, aicon->icon->core->window);
2925 aicon->launching = 1;
2926 wAppIconPaint(aicon);
2927 SlideWindow(aicon->icon->core->window, x0, y0, icon->x_pos, icon->y_pos);
2928 XUnmapWindow(dpy, aicon->icon->core->window);
2929 wAppIconDestroy(aicon);
2931 wDockFinishLaunch(dock, icon);
2932 break;
2936 if (firstPass && !found) {
2937 firstPass = False;
2938 goto retry;
2941 if (command)
2942 wfree(command);
2944 if (wm_class)
2945 free(wm_class);
2946 if (wm_instance)
2947 free(wm_instance);
2950 void wClipUpdateForWorkspaceChange(WScreen *scr, int workspace)
2952 if (!wPreferences.flags.noclip) {
2953 scr->clip_icon->dock = scr->workspaces[workspace]->clip;
2954 if (scr->current_workspace != workspace) {
2955 WDock *old_clip = scr->workspaces[scr->current_workspace]->clip;
2956 WAppIconChain *chain = scr->global_icons;
2958 while (chain) {
2959 moveIconBetweenDocks(chain->aicon->dock,
2960 scr->workspaces[workspace]->clip,
2961 chain->aicon, chain->aicon->xindex, chain->aicon->yindex);
2962 if (scr->workspaces[workspace]->clip->collapsed)
2963 XUnmapWindow(dpy, chain->aicon->icon->core->window);
2964 chain = chain->next;
2967 wDockHideIcons(old_clip);
2968 if (old_clip->auto_raise_lower) {
2969 if (old_clip->auto_raise_magic) {
2970 WMDeleteTimerHandler(old_clip->auto_raise_magic);
2971 old_clip->auto_raise_magic = NULL;
2973 wDockLower(old_clip);
2975 if (old_clip->auto_collapse) {
2976 if (old_clip->auto_expand_magic) {
2977 WMDeleteTimerHandler(old_clip->auto_expand_magic);
2978 old_clip->auto_expand_magic = NULL;
2980 old_clip->collapsed = 1;
2982 wDockShowIcons(scr->workspaces[workspace]->clip);
2987 static void trackDeadProcess(pid_t pid, unsigned char status, WDock *dock)
2989 WAppIcon *icon;
2990 int i;
2992 for (i = 0; i < dock->max_icons; i++) {
2993 icon = dock->icon_array[i];
2994 if (!icon)
2995 continue;
2997 if (icon->launching && icon->pid == pid) {
2998 if (!icon->relaunching) {
2999 icon->running = 0;
3000 icon->main_window = None;
3002 wDockFinishLaunch(dock, icon);
3003 icon->pid = 0;
3004 if (status == 111) {
3005 char msg[PATH_MAX];
3006 char *cmd;
3008 #ifdef XDND
3009 if (icon->drop_launch)
3010 cmd = icon->dnd_command;
3011 else
3012 #endif
3013 if (icon->paste_launch)
3014 cmd = icon->paste_command;
3015 else
3016 cmd = icon->command;
3018 snprintf(msg, sizeof(msg), _("Could not execute command \"%s\""), cmd);
3020 wMessageDialog(dock->screen_ptr, _("Error"), msg, _("OK"), NULL, NULL);
3022 break;
3027 static void toggleLowered(WDock *dock)
3029 WAppIcon *tmp;
3030 int newlevel, i;
3032 /* lower/raise Dock */
3033 if (!dock->lowered) {
3034 newlevel = WMNormalLevel;
3035 dock->lowered = 1;
3036 } else {
3037 newlevel = WMDockLevel;
3038 dock->lowered = 0;
3041 for (i = 0; i < dock->max_icons; i++) {
3042 tmp = dock->icon_array[i];
3043 if (!tmp)
3044 continue;
3046 ChangeStackingLevel(tmp->icon->core, newlevel);
3047 if (dock->lowered)
3048 wLowerFrame(tmp->icon->core);
3051 if (dock->type == WM_DOCK)
3052 wScreenUpdateUsableArea(dock->screen_ptr);
3055 static void toggleCollapsed(WDock *dock)
3057 if (dock->collapsed) {
3058 dock->collapsed = 0;
3059 wDockShowIcons(dock);
3060 } else {
3061 dock->collapsed = 1;
3062 wDockHideIcons(dock);
3066 static void openDockMenu(WDock *dock, WAppIcon *aicon, XEvent *event)
3068 WScreen *scr = dock->screen_ptr;
3069 WObjDescriptor *desc;
3070 WMenuEntry *entry;
3071 WApplication *wapp = NULL;
3072 int index = 0;
3073 int x_pos;
3074 int n_selected;
3075 int appIsRunning = aicon->running && aicon->icon && aicon->icon->owner;
3077 if (dock->type == WM_DOCK) {
3078 /* keep on top */
3079 entry = dock->menu->entries[index];
3080 entry->flags.indicator_on = !dock->lowered;
3081 entry->clientdata = dock;
3082 dock->menu->flags.realized = 0;
3083 } else {
3084 /* clip options */
3085 if (scr->clip_options)
3086 updateClipOptionsMenu(scr->clip_options, dock);
3088 n_selected = numberOfSelectedIcons(dock);
3090 /* Rename Workspace */
3091 entry = dock->menu->entries[++index];
3092 if (aicon == scr->clip_icon) {
3093 entry->callback = renameCallback;
3094 entry->clientdata = dock;
3095 entry->flags.indicator = 0;
3096 entry->text = _("Rename Workspace");
3097 } else {
3098 entry->callback = omnipresentCallback;
3099 entry->clientdata = aicon;
3100 if (n_selected > 0) {
3101 entry->flags.indicator = 0;
3102 entry->text = _("Toggle Omnipresent");
3103 } else {
3104 entry->flags.indicator = 1;
3105 entry->flags.indicator_on = aicon->omnipresent;
3106 entry->flags.indicator_type = MI_CHECK;
3107 entry->text = _("Omnipresent");
3111 /* select/unselect icon */
3112 entry = dock->menu->entries[++index];
3113 entry->clientdata = aicon;
3114 entry->flags.indicator_on = aicon->icon->selected;
3115 wMenuSetEnabled(dock->menu, index, aicon != scr->clip_icon);
3117 /* select/unselect all icons */
3118 entry = dock->menu->entries[++index];
3119 entry->clientdata = aicon;
3120 if (n_selected > 0)
3121 entry->text = _("Unselect All Icons");
3122 else
3123 entry->text = _("Select All Icons");
3125 wMenuSetEnabled(dock->menu, index, dock->icon_count > 1);
3127 /* keep icon(s) */
3128 entry = dock->menu->entries[++index];
3129 entry->clientdata = aicon;
3130 if (n_selected > 1)
3131 entry->text = _("Keep Icons");
3132 else
3133 entry->text = _("Keep Icon");
3135 wMenuSetEnabled(dock->menu, index, dock->icon_count > 1);
3137 /* this is the workspace submenu part */
3138 entry = dock->menu->entries[++index];
3139 if (n_selected > 1)
3140 entry->text = _("Move Icons To");
3141 else
3142 entry->text = _("Move Icon To");
3144 if (scr->clip_submenu)
3145 updateWorkspaceMenu(scr->clip_submenu, aicon);
3147 wMenuSetEnabled(dock->menu, index, !aicon->omnipresent);
3149 /* remove icon(s) */
3150 entry = dock->menu->entries[++index];
3151 entry->clientdata = aicon;
3152 if (n_selected > 1)
3153 entry->text = _("Remove Icons");
3154 else
3155 entry->text = _("Remove Icon");
3157 wMenuSetEnabled(dock->menu, index, dock->icon_count > 1);
3159 /* attract icon(s) */
3160 entry = dock->menu->entries[++index];
3161 entry->clientdata = aicon;
3163 dock->menu->flags.realized = 0;
3164 wMenuRealize(dock->menu);
3167 if (aicon->icon->owner)
3168 wapp = wApplicationOf(aicon->icon->owner->main_window);
3169 else
3170 wapp = NULL;
3172 /* launch */
3173 entry = dock->menu->entries[++index];
3174 entry->clientdata = aicon;
3175 wMenuSetEnabled(dock->menu, index, aicon->command != NULL);
3177 /* unhide here */
3178 entry = dock->menu->entries[++index];
3179 entry->clientdata = aicon;
3180 if (wapp && wapp->flags.hidden)
3181 entry->text = _("Unhide Here");
3182 else
3183 entry->text = _("Bring Here");
3185 wMenuSetEnabled(dock->menu, index, appIsRunning);
3187 /* hide */
3188 entry = dock->menu->entries[++index];
3189 entry->clientdata = aicon;
3190 if (wapp && wapp->flags.hidden)
3191 entry->text = _("Unhide");
3192 else
3193 entry->text = _("Hide");
3195 wMenuSetEnabled(dock->menu, index, appIsRunning);
3197 /* settings */
3198 entry = dock->menu->entries[++index];
3199 entry->clientdata = aicon;
3200 wMenuSetEnabled(dock->menu, index, !aicon->editing && !wPreferences.flags.noupdates);
3202 /* kill */
3203 entry = dock->menu->entries[++index];
3204 entry->clientdata = aicon;
3205 wMenuSetEnabled(dock->menu, index, appIsRunning);
3207 if (!dock->menu->flags.realized)
3208 wMenuRealize(dock->menu);
3210 if (dock->type == WM_CLIP) {
3211 /*x_pos = event->xbutton.x_root+2; */
3212 x_pos = event->xbutton.x_root - dock->menu->frame->core->width / 2 - 1;
3213 if (x_pos < 0) {
3214 x_pos = 0;
3215 } else if (x_pos + dock->menu->frame->core->width > scr->scr_width - 2) {
3216 x_pos = scr->scr_width - dock->menu->frame->core->width - 4;
3218 } else {
3219 x_pos = dock->on_right_side ? scr->scr_width - dock->menu->frame->core->width - 3 : 0;
3222 wMenuMapAt(dock->menu, x_pos, event->xbutton.y_root + 2, False);
3224 /* allow drag select */
3225 event->xany.send_event = True;
3226 desc = &dock->menu->menu->descriptor;
3227 (*desc->handle_mousedown) (desc, event);
3230 /******************************************************************/
3231 static void iconDblClick(WObjDescriptor *desc, XEvent *event)
3233 WAppIcon *btn = desc->parent;
3234 WDock *dock = btn->dock;
3235 WApplication *wapp = NULL;
3236 int unhideHere = 0;
3238 if (btn->icon->owner && !(event->xbutton.state & ControlMask)) {
3239 wapp = wApplicationOf(btn->icon->owner->main_window);
3241 assert(wapp != NULL);
3243 unhideHere = (event->xbutton.state & ShiftMask);
3245 /* go to the last workspace that the user worked on the app */
3246 if (wapp->last_workspace != dock->screen_ptr->current_workspace && !unhideHere)
3247 wWorkspaceChange(dock->screen_ptr, wapp->last_workspace);
3249 wUnhideApplication(wapp, event->xbutton.button == Button2, unhideHere);
3251 if (event->xbutton.state & MOD_MASK)
3252 wHideOtherApplications(btn->icon->owner);
3253 } else {
3254 if (event->xbutton.button == Button1) {
3255 if (event->xbutton.state & MOD_MASK) {
3256 /* raise/lower dock */
3257 toggleLowered(dock);
3258 } else if (btn == dock->screen_ptr->clip_icon) {
3259 if (getClipButton(event->xbutton.x, event->xbutton.y) == CLIP_IDLE)
3260 toggleCollapsed(dock);
3261 else
3262 handleClipChangeWorkspace(dock->screen_ptr, event);
3263 } else if (btn->command) {
3264 if (!btn->launching && (!btn->running || (event->xbutton.state & ControlMask)))
3265 launchDockedApplication(btn, False);
3271 static void handleDockMove(WDock *dock, WAppIcon *aicon, XEvent *event)
3273 WScreen *scr = dock->screen_ptr;
3274 int ofs_x = event->xbutton.x, ofs_y = event->xbutton.y;
3275 int x, y;
3276 XEvent ev;
3277 int grabbed = 0, swapped = 0, done;
3278 Pixmap ghost = None;
3279 int superfluous = wPreferences.superfluous; /* we catch it to avoid problems */
3281 if (XGrabPointer(dpy, aicon->icon->core->window, True, ButtonMotionMask
3282 | ButtonReleaseMask | ButtonPressMask, GrabModeAsync,
3283 GrabModeAsync, None, None, CurrentTime) != GrabSuccess)
3284 wwarning("pointer grab failed for dock move");
3286 y = 0;
3287 for (x = 0; x < dock->max_icons; x++) {
3288 if (dock->icon_array[x] != NULL && dock->icon_array[x]->yindex > y)
3289 y = dock->icon_array[x]->yindex;
3291 y++;
3292 XResizeWindow(dpy, scr->dock_shadow, ICON_SIZE, ICON_SIZE * y);
3294 done = 0;
3295 while (!done) {
3296 WMMaskEvent(dpy, PointerMotionMask | ButtonReleaseMask | ButtonPressMask
3297 | ButtonMotionMask | ExposureMask | EnterWindowMask, &ev);
3298 switch (ev.type) {
3299 case Expose:
3300 WMHandleEvent(&ev);
3301 break;
3303 case EnterNotify:
3304 /* It means the cursor moved so fast that it entered
3305 * something else (if moving slowly, it would have
3306 * stayed in the dock that is being moved. Ignore such
3307 * "spurious" EnterNotifiy's */
3308 break;
3310 case MotionNotify:
3311 if (!grabbed) {
3312 if (abs(ofs_x - ev.xmotion.x) >= MOVE_THRESHOLD
3313 || abs(ofs_y - ev.xmotion.y) >= MOVE_THRESHOLD) {
3314 XChangeActivePointerGrab(dpy, ButtonMotionMask
3315 | ButtonReleaseMask | ButtonPressMask,
3316 wCursor[WCUR_MOVE], CurrentTime);
3317 grabbed = 1;
3319 break;
3321 if (dock->type == WM_CLIP) {
3322 x = ev.xmotion.x_root - ofs_x;
3323 y = ev.xmotion.y_root - ofs_y;
3324 wScreenKeepInside(scr, &x, &y, ICON_SIZE, ICON_SIZE);
3326 moveDock(dock, x, y);
3327 } else {
3328 /* move vertically if pointer is inside the dock */
3329 if ((dock->on_right_side && ev.xmotion.x_root >= dock->x_pos - ICON_SIZE)
3330 || (!dock->on_right_side && ev.xmotion.x_root <= dock->x_pos + ICON_SIZE * 2)) {
3332 x = ev.xmotion.x_root - ofs_x;
3333 y = ev.xmotion.y_root - ofs_y;
3334 wScreenKeepInside(scr, &x, &y, ICON_SIZE, ICON_SIZE);
3335 moveDock(dock, dock->x_pos, y);
3337 /* move horizontally to change sides */
3338 x = ev.xmotion.x_root - ofs_x;
3339 if (!dock->on_right_side) {
3341 /* is on left */
3342 if (ev.xmotion.x_root > dock->x_pos + ICON_SIZE * 2) {
3343 XMoveWindow(dpy, scr->dock_shadow, scr->scr_width - ICON_SIZE
3344 - DOCK_EXTRA_SPACE - 1, dock->y_pos);
3345 if (superfluous && ghost == None) {
3346 ghost = MakeGhostDock(dock, dock->x_pos,
3347 scr->scr_width - ICON_SIZE
3348 - DOCK_EXTRA_SPACE - 1, dock->y_pos);
3349 XSetWindowBackgroundPixmap(dpy, scr->dock_shadow, ghost);
3350 XClearWindow(dpy, scr->dock_shadow);
3352 XMapRaised(dpy, scr->dock_shadow);
3353 swapped = 1;
3354 } else {
3355 if (superfluous && ghost != None) {
3356 XFreePixmap(dpy, ghost);
3357 ghost = None;
3359 XUnmapWindow(dpy, scr->dock_shadow);
3360 swapped = 0;
3362 } else {
3363 /* is on right */
3364 if (ev.xmotion.x_root < dock->x_pos - ICON_SIZE) {
3365 XMoveWindow(dpy, scr->dock_shadow, DOCK_EXTRA_SPACE, dock->y_pos);
3366 if (superfluous && ghost == None) {
3367 ghost = MakeGhostDock(dock, dock->x_pos,
3368 DOCK_EXTRA_SPACE, dock->y_pos);
3369 XSetWindowBackgroundPixmap(dpy, scr->dock_shadow, ghost);
3370 XClearWindow(dpy, scr->dock_shadow);
3372 XMapRaised(dpy, scr->dock_shadow);
3373 swapped = -1;
3374 } else {
3375 XUnmapWindow(dpy, scr->dock_shadow);
3376 swapped = 0;
3377 if (superfluous && ghost != None) {
3378 XFreePixmap(dpy, ghost);
3379 ghost = None;
3384 break;
3386 case ButtonPress:
3387 break;
3389 case ButtonRelease:
3390 if (ev.xbutton.button != event->xbutton.button)
3391 break;
3392 XUngrabPointer(dpy, CurrentTime);
3393 XUnmapWindow(dpy, scr->dock_shadow);
3394 XResizeWindow(dpy, scr->dock_shadow, ICON_SIZE, ICON_SIZE);
3395 if (dock->type == WM_DOCK) {
3396 if (swapped != 0) {
3397 if (swapped > 0)
3398 dock->on_right_side = 1;
3399 else
3400 dock->on_right_side = 0;
3401 swapDock(dock);
3402 wArrangeIcons(scr, False);
3405 done = 1;
3406 break;
3409 if (superfluous) {
3410 if (ghost != None)
3411 XFreePixmap(dpy, ghost);
3412 XSetWindowBackground(dpy, scr->dock_shadow, scr->white_pixel);
3416 static Bool handleIconMove(WDock *dock, WAppIcon *aicon, XEvent *event)
3418 WScreen *scr = dock->screen_ptr;
3419 Window wins[2];
3420 WIcon *icon = aicon->icon;
3421 WDock *dock2 = NULL, *last_dock = dock, *clip = NULL;
3422 int ondock, grabbed = 0, change_dock = 0, collapsed = 0;
3423 XEvent ev;
3424 int x = aicon->x_pos, y = aicon->y_pos;
3425 int ofs_x = event->xbutton.x, ofs_y = event->xbutton.y;
3426 int shad_x = x, shad_y = y;
3427 int ix = aicon->xindex, iy = aicon->yindex;
3428 int tmp;
3429 Pixmap ghost = None;
3430 Bool docked;
3431 int superfluous = wPreferences.superfluous; /* we catch it to avoid problems */
3432 int omnipresent = aicon->omnipresent; /* this must be cached!!! */
3433 Bool hasMoved = False;
3435 if (wPreferences.flags.noupdates)
3436 return hasMoved;
3438 if (XGrabPointer(dpy, icon->core->window, True, ButtonMotionMask
3439 | ButtonReleaseMask | ButtonPressMask, GrabModeAsync,
3440 GrabModeAsync, None, None, CurrentTime) != GrabSuccess) {
3443 if (!(event->xbutton.state & MOD_MASK))
3444 wRaiseFrame(icon->core);
3446 if (!wPreferences.flags.noclip)
3447 clip = scr->workspaces[scr->current_workspace]->clip;
3449 if (dock == scr->dock && !wPreferences.flags.noclip)
3450 dock2 = clip;
3451 else if (dock != scr->dock && !wPreferences.flags.nodock)
3452 dock2 = scr->dock;
3454 wins[0] = icon->core->window;
3455 wins[1] = scr->dock_shadow;
3456 XRestackWindows(dpy, wins, 2);
3457 XMoveResizeWindow(dpy, scr->dock_shadow, aicon->x_pos, aicon->y_pos, ICON_SIZE, ICON_SIZE);
3458 if (superfluous) {
3459 if (icon->pixmap != None)
3460 ghost = MakeGhostIcon(scr, icon->pixmap);
3461 else
3462 ghost = MakeGhostIcon(scr, icon->core->window);
3464 XSetWindowBackgroundPixmap(dpy, scr->dock_shadow, ghost);
3465 XClearWindow(dpy, scr->dock_shadow);
3467 XMapWindow(dpy, scr->dock_shadow);
3469 ondock = 1;
3471 while (1) {
3472 XMaskEvent(dpy, PointerMotionMask | ButtonReleaseMask | ButtonPressMask
3473 | ButtonMotionMask | ExposureMask, &ev);
3474 switch (ev.type) {
3475 case Expose:
3476 WMHandleEvent(&ev);
3477 break;
3479 case MotionNotify:
3480 hasMoved = True;
3481 if (!grabbed) {
3482 if (abs(ofs_x - ev.xmotion.x) >= MOVE_THRESHOLD
3483 || abs(ofs_y - ev.xmotion.y) >= MOVE_THRESHOLD) {
3484 XChangeActivePointerGrab(dpy, ButtonMotionMask
3485 | ButtonReleaseMask | ButtonPressMask,
3486 wCursor[WCUR_MOVE], CurrentTime);
3487 grabbed = 1;
3488 } else {
3489 break;
3493 if (omnipresent) {
3494 int i;
3495 for (i = 0; i < scr->workspace_count; i++) {
3496 if (i == scr->current_workspace)
3497 continue;
3498 wDockShowIcons(scr->workspaces[i]->clip);
3502 x = ev.xmotion.x_root - ofs_x;
3503 y = ev.xmotion.y_root - ofs_y;
3504 tmp = wDockSnapIcon(dock, aicon, x, y, &ix, &iy, True);
3505 if (tmp && dock2) {
3506 change_dock = 0;
3507 if (last_dock != dock && collapsed) {
3508 last_dock->collapsed = 1;
3509 wDockHideIcons(last_dock);
3510 collapsed = 0;
3512 if (!collapsed && (collapsed = dock->collapsed)) {
3513 dock->collapsed = 0;
3514 wDockShowIcons(dock);
3516 if (dock->auto_raise_lower)
3517 wDockRaise(dock);
3518 last_dock = dock;
3519 } else if (dock2) {
3520 tmp = wDockSnapIcon(dock2, aicon, x, y, &ix, &iy, False);
3521 if (tmp) {
3522 change_dock = 1;
3523 if (last_dock != dock2 && collapsed) {
3524 last_dock->collapsed = 1;
3525 wDockHideIcons(last_dock);
3526 collapsed = 0;
3528 if (!collapsed && (collapsed = dock2->collapsed)) {
3529 dock2->collapsed = 0;
3530 wDockShowIcons(dock2);
3532 if (dock2->auto_raise_lower)
3533 wDockRaise(dock2);
3534 last_dock = dock2;
3537 if (aicon->launching || aicon->lock || (aicon->running && !(ev.xmotion.state & MOD_MASK))
3538 || (!aicon->running && tmp)) {
3539 shad_x = last_dock->x_pos + ix * wPreferences.icon_size;
3540 shad_y = last_dock->y_pos + iy * wPreferences.icon_size;
3542 XMoveWindow(dpy, scr->dock_shadow, shad_x, shad_y);
3544 if (!ondock)
3545 XMapWindow(dpy, scr->dock_shadow);
3547 ondock = 1;
3548 } else {
3549 if (ondock)
3550 XUnmapWindow(dpy, scr->dock_shadow);
3552 ondock = 0;
3554 XMoveWindow(dpy, icon->core->window, x, y);
3555 break;
3557 case ButtonPress:
3558 break;
3560 case ButtonRelease:
3561 if (ev.xbutton.button != event->xbutton.button)
3562 break;
3563 XUngrabPointer(dpy, CurrentTime);
3564 if (ondock) {
3565 SlideWindow(icon->core->window, x, y, shad_x, shad_y);
3566 XUnmapWindow(dpy, scr->dock_shadow);
3567 if (!change_dock) {
3568 reattachIcon(dock, aicon, ix, iy);
3569 if (clip && dock != clip && clip->auto_raise_lower)
3570 wDockLower(clip);
3571 } else {
3572 docked = moveIconBetweenDocks(dock, dock2, aicon, ix, iy);
3573 if (!docked) {
3574 /* Slide it back if dock rejected it */
3575 SlideWindow(icon->core->window, x, y, aicon->x_pos, aicon->y_pos);
3576 reattachIcon(dock, aicon, aicon->xindex, aicon->yindex);
3578 if (last_dock->type == WM_CLIP && last_dock->auto_collapse)
3579 collapsed = 0;
3581 } else {
3582 aicon->x_pos = x;
3583 aicon->y_pos = y;
3584 if (superfluous) {
3585 if (!aicon->running && !wPreferences.no_animations) {
3586 /* We need to deselect it, even if is deselected in
3587 * wDockDetach(), because else DoKaboom() will fail.
3589 if (aicon->icon->selected)
3590 wIconSelect(aicon->icon);
3592 DoKaboom(scr, aicon->icon->core->window, x, y);
3595 if (clip && clip->auto_raise_lower)
3596 wDockLower(clip);
3597 wDockDetach(dock, aicon);
3599 if (collapsed) {
3600 last_dock->collapsed = 1;
3601 wDockHideIcons(last_dock);
3602 collapsed = 0;
3604 if (superfluous) {
3605 if (ghost != None)
3606 XFreePixmap(dpy, ghost);
3607 XSetWindowBackground(dpy, scr->dock_shadow, scr->white_pixel);
3609 if (omnipresent) {
3610 int i;
3611 for (i = 0; i < scr->workspace_count; i++) {
3612 if (i == scr->current_workspace)
3613 continue;
3614 wDockHideIcons(scr->workspaces[i]->clip);
3617 return hasMoved;;
3620 return False; /* never reached */
3623 static int getClipButton(int px, int py)
3625 int pt = (CLIP_BUTTON_SIZE + 2) * ICON_SIZE / 64;
3627 if (px < 0 || py < 0 || px >= ICON_SIZE || py >= ICON_SIZE)
3628 return CLIP_IDLE;
3630 if (py <= pt - ((int)ICON_SIZE - 1 - px))
3631 return CLIP_FORWARD;
3632 else if (px <= pt - ((int)ICON_SIZE - 1 - py))
3633 return CLIP_REWIND;
3635 return CLIP_IDLE;
3638 static void handleClipChangeWorkspace(WScreen *scr, XEvent *event)
3640 XEvent ev;
3641 int done, direction, new_ws;
3642 int new_dir;
3643 WDock *clip = scr->clip_icon->dock;
3645 direction = getClipButton(event->xbutton.x, event->xbutton.y);
3647 clip->lclip_button_pushed = direction == CLIP_REWIND;
3648 clip->rclip_button_pushed = direction == CLIP_FORWARD;
3650 wClipIconPaint(scr->clip_icon);
3651 done = 0;
3652 while (!done) {
3653 WMMaskEvent(dpy, ExposureMask | ButtonMotionMask | ButtonReleaseMask | ButtonPressMask, &ev);
3654 switch (ev.type) {
3655 case Expose:
3656 WMHandleEvent(&ev);
3657 break;
3659 case MotionNotify:
3660 new_dir = getClipButton(ev.xmotion.x, ev.xmotion.y);
3661 if (new_dir != direction) {
3662 direction = new_dir;
3663 clip->lclip_button_pushed = direction == CLIP_REWIND;
3664 clip->rclip_button_pushed = direction == CLIP_FORWARD;
3665 wClipIconPaint(scr->clip_icon);
3667 break;
3669 case ButtonPress:
3670 break;
3672 case ButtonRelease:
3673 if (ev.xbutton.button == event->xbutton.button)
3674 done = 1;
3678 clip->lclip_button_pushed = 0;
3679 clip->rclip_button_pushed = 0;
3681 new_ws = wPreferences.ws_advance || (event->xbutton.state & ControlMask);
3683 if (direction == CLIP_FORWARD) {
3684 if (scr->current_workspace < scr->workspace_count - 1)
3685 wWorkspaceChange(scr, scr->current_workspace + 1);
3686 else if (new_ws && scr->current_workspace < MAX_WORKSPACES - 1)
3687 wWorkspaceChange(scr, scr->current_workspace + 1);
3688 else if (wPreferences.ws_cycle)
3689 wWorkspaceChange(scr, 0);
3690 } else if (direction == CLIP_REWIND) {
3691 if (scr->current_workspace > 0)
3692 wWorkspaceChange(scr, scr->current_workspace - 1);
3693 else if (scr->current_workspace == 0 && wPreferences.ws_cycle)
3694 wWorkspaceChange(scr, scr->workspace_count - 1);
3697 wClipIconPaint(scr->clip_icon);
3700 static void iconMouseDown(WObjDescriptor *desc, XEvent *event)
3702 WAppIcon *aicon = desc->parent;
3703 WDock *dock = aicon->dock;
3704 WScreen *scr = aicon->icon->core->screen_ptr;
3706 if (aicon->editing || WCHECK_STATE(WSTATE_MODAL))
3707 return;
3709 scr->last_dock = dock;
3711 if (dock->menu->flags.mapped)
3712 wMenuUnmap(dock->menu);
3714 if (IsDoubleClick(scr, event)) {
3715 /* double-click was not in the main clip icon */
3716 if (dock->type != WM_CLIP || aicon->xindex != 0 || aicon->yindex != 0
3717 || getClipButton(event->xbutton.x, event->xbutton.y) == CLIP_IDLE) {
3718 iconDblClick(desc, event);
3719 return;
3723 if (event->xbutton.button == Button1) {
3724 if (event->xbutton.state & MOD_MASK)
3725 wDockLower(dock);
3726 else
3727 wDockRaise(dock);
3729 if ((event->xbutton.state & ShiftMask) && aicon != scr->clip_icon && dock->type != WM_DOCK) {
3730 wIconSelect(aicon->icon);
3731 return;
3734 if (aicon->yindex == 0 && aicon->xindex == 0) {
3735 if (getClipButton(event->xbutton.x, event->xbutton.y) != CLIP_IDLE
3736 && dock->type == WM_CLIP)
3737 handleClipChangeWorkspace(scr, event);
3738 else
3739 handleDockMove(dock, aicon, event);
3740 } else {
3741 Bool hasMoved = handleIconMove(dock, aicon, event);
3742 if (wPreferences.single_click && !hasMoved)
3743 iconDblClick(desc, event);
3745 } else if (event->xbutton.button == Button2 && dock->type == WM_CLIP && aicon == scr->clip_icon) {
3746 if (!scr->clip_ws_menu) {
3747 scr->clip_ws_menu = wWorkspaceMenuMake(scr, False);
3749 if (scr->clip_ws_menu) {
3750 WMenu *wsMenu = scr->clip_ws_menu;
3751 int xpos;
3753 wWorkspaceMenuUpdate(scr, wsMenu);
3755 xpos = event->xbutton.x_root - wsMenu->frame->core->width / 2 - 1;
3756 if (xpos < 0) {
3757 xpos = 0;
3758 } else if (xpos + wsMenu->frame->core->width > scr->scr_width - 2) {
3759 xpos = scr->scr_width - wsMenu->frame->core->width - 4;
3761 wMenuMapAt(wsMenu, xpos, event->xbutton.y_root + 2, False);
3763 desc = &wsMenu->menu->descriptor;
3764 event->xany.send_event = True;
3765 (*desc->handle_mousedown) (desc, event);
3767 } else if (event->xbutton.button == Button2 && dock->type == WM_CLIP &&
3768 (event->xbutton.state & ShiftMask) && aicon != scr->clip_icon) {
3769 wClipMakeIconOmnipresent(aicon, !aicon->omnipresent);
3770 } else if (event->xbutton.button == Button3) {
3771 if (event->xbutton.send_event &&
3772 XGrabPointer(dpy, aicon->icon->core->window, True, ButtonMotionMask
3773 | ButtonReleaseMask | ButtonPressMask, GrabModeAsync,
3774 GrabModeAsync, None, None, CurrentTime) != GrabSuccess) {
3775 wwarning("pointer grab failed for dockicon menu");
3776 return;
3779 openDockMenu(dock, aicon, event);
3780 } else if (event->xbutton.button == Button2) {
3781 WAppIcon *btn = desc->parent;
3783 if (!btn->launching && (!btn->running || (event->xbutton.state & ControlMask)))
3784 launchDockedApplication(btn, True);
3788 static void clipEnterNotify(WObjDescriptor *desc, XEvent *event)
3790 WAppIcon *btn = (WAppIcon *) desc->parent;
3791 WDock *dock;
3792 WScreen *scr;
3794 assert(event->type == EnterNotify);
3796 if (desc->parent_type != WCLASS_DOCK_ICON)
3797 return;
3799 scr = btn->icon->core->screen_ptr;
3800 if (!btn->omnipresent)
3801 dock = btn->dock;
3802 else
3803 dock = scr->workspaces[scr->current_workspace]->clip;
3805 if (!dock || dock->type != WM_CLIP)
3806 return;
3808 /* The auto raise/lower code */
3809 if (dock->auto_lower_magic) {
3810 WMDeleteTimerHandler(dock->auto_lower_magic);
3811 dock->auto_lower_magic = NULL;
3813 if (dock->auto_raise_lower && !dock->auto_raise_magic)
3814 dock->auto_raise_magic = WMAddTimerHandler(AUTO_RAISE_DELAY, clipAutoRaise, (void *)dock);
3816 /* The auto expand/collapse code */
3817 if (dock->auto_collapse_magic) {
3818 WMDeleteTimerHandler(dock->auto_collapse_magic);
3819 dock->auto_collapse_magic = NULL;
3821 if (dock->auto_collapse && !dock->auto_expand_magic)
3822 dock->auto_expand_magic = WMAddTimerHandler(AUTO_EXPAND_DELAY, clipAutoExpand, (void *)dock);
3825 static void clipLeave(WDock *dock)
3827 XEvent event;
3828 WObjDescriptor *desc = NULL;
3830 if (!dock || dock->type != WM_CLIP)
3831 return;
3833 if (XCheckTypedEvent(dpy, EnterNotify, &event) != False) {
3834 if (XFindContext(dpy, event.xcrossing.window, wWinContext,
3835 (XPointer *) & desc) != XCNOENT
3836 && desc && desc->parent_type == WCLASS_DOCK_ICON
3837 && ((WAppIcon *) desc->parent)->dock && ((WAppIcon *) desc->parent)->dock->type == WM_CLIP) {
3838 /* We didn't left the Clip yet */
3839 XPutBackEvent(dpy, &event);
3840 return;
3843 XPutBackEvent(dpy, &event);
3844 } else {
3845 /* We entered a withdrawn window, so we're still in Clip */
3846 return;
3849 if (dock->auto_raise_magic) {
3850 WMDeleteTimerHandler(dock->auto_raise_magic);
3851 dock->auto_raise_magic = NULL;
3853 if (dock->auto_raise_lower && !dock->auto_lower_magic)
3854 dock->auto_lower_magic = WMAddTimerHandler(AUTO_LOWER_DELAY, clipAutoLower, (void *)dock);
3856 if (dock->auto_expand_magic) {
3857 WMDeleteTimerHandler(dock->auto_expand_magic);
3858 dock->auto_expand_magic = NULL;
3860 if (dock->auto_collapse && !dock->auto_collapse_magic)
3861 dock->auto_collapse_magic = WMAddTimerHandler(AUTO_COLLAPSE_DELAY, clipAutoCollapse, (void *)dock);
3864 static void clipLeaveNotify(WObjDescriptor *desc, XEvent *event)
3866 WAppIcon *btn = (WAppIcon *) desc->parent;
3868 assert(event->type == LeaveNotify);
3870 if (desc->parent_type != WCLASS_DOCK_ICON)
3871 return;
3873 clipLeave(btn->dock);
3876 static void clipAutoCollapse(void *cdata)
3878 WDock *dock = (WDock *) cdata;
3880 if (dock->type != WM_CLIP)
3881 return;
3883 if (dock->auto_collapse) {
3884 dock->collapsed = 1;
3885 wDockHideIcons(dock);
3887 dock->auto_collapse_magic = NULL;
3890 static void clipAutoExpand(void *cdata)
3892 WDock *dock = (WDock *) cdata;
3894 if (dock->type != WM_CLIP)
3895 return;
3897 if (dock->auto_collapse) {
3898 dock->collapsed = 0;
3899 wDockShowIcons(dock);
3901 dock->auto_expand_magic = NULL;
3904 static void clipAutoLower(void *cdata)
3906 WDock *dock = (WDock *) cdata;
3908 if (dock->type != WM_CLIP)
3909 return;
3911 if (dock->auto_raise_lower)
3912 wDockLower(dock);
3914 dock->auto_lower_magic = NULL;
3917 static void clipAutoRaise(void *cdata)
3919 WDock *dock = (WDock *) cdata;
3921 if (dock->type != WM_CLIP)
3922 return;
3924 if (dock->auto_raise_lower)
3925 wDockRaise(dock);
3927 dock->auto_raise_magic = NULL;
3930 static Bool iconCanBeOmnipresent(WAppIcon *aicon)
3932 WScreen *scr = aicon->icon->core->screen_ptr;
3933 WDock *clip;
3934 WAppIcon *btn;
3935 int i, j;
3937 for (i = 0; i < scr->workspace_count; i++) {
3938 clip = scr->workspaces[i]->clip;
3940 if (clip == aicon->dock)
3941 continue;
3943 if (clip->icon_count + scr->global_icon_count >= clip->max_icons)
3944 return False; /* Clip is full in some workspace */
3946 for (j = 0; j < clip->max_icons; j++) {
3947 btn = clip->icon_array[j];
3948 if (btn && btn->xindex == aicon->xindex && btn->yindex == aicon->yindex)
3949 return False;
3953 return True;
3956 int wClipMakeIconOmnipresent(WAppIcon *aicon, int omnipresent)
3958 WScreen *scr = aicon->icon->core->screen_ptr;
3959 WAppIconChain *new_entry, *tmp, *tmp1;
3960 int status = WO_SUCCESS;
3962 if ((scr->dock && aicon->dock == scr->dock) || aicon == scr->clip_icon)
3963 return WO_NOT_APPLICABLE;
3965 if (aicon->omnipresent == omnipresent)
3966 return WO_SUCCESS;
3968 if (omnipresent) {
3969 if (iconCanBeOmnipresent(aicon)) {
3970 aicon->omnipresent = 1;
3971 new_entry = wmalloc(sizeof(WAppIconChain));
3972 new_entry->aicon = aicon;
3973 new_entry->next = scr->global_icons;
3974 scr->global_icons = new_entry;
3975 scr->global_icon_count++;
3976 } else {
3977 aicon->omnipresent = 0;
3978 status = WO_FAILED;
3980 } else {
3981 aicon->omnipresent = 0;
3982 if (aicon == scr->global_icons->aicon) {
3983 tmp = scr->global_icons->next;
3984 wfree(scr->global_icons);
3985 scr->global_icons = tmp;
3986 scr->global_icon_count--;
3987 } else {
3988 tmp = scr->global_icons;
3989 while (tmp->next) {
3990 if (tmp->next->aicon == aicon) {
3991 tmp1 = tmp->next->next;
3992 wfree(tmp->next);
3993 tmp->next = tmp1;
3994 scr->global_icon_count--;
3995 break;
3997 tmp = tmp->next;
4002 wAppIconPaint(aicon);
4004 return status;