window.h: Remove unused variables from WWindow struct
[wmaker-crm.git] / src / dock.c
blob05824d4781b434f8d3107acbac62e988dcec1197
1 /* dock.c- built-in Dock module for WindowMaker
3 * Window Maker window manager
5 * Copyright (c) 1997-2003 Alfredo K. Kojima
6 * Copyright (c) 1998-2003 Dan Pascu
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
23 #include "wconfig.h"
25 #include <X11/Xlib.h>
26 #include <X11/Xutil.h>
27 #include <stdlib.h>
28 #include <string.h>
29 #include <strings.h>
30 #include <unistd.h>
31 #include <math.h>
32 #include <limits.h>
34 #ifndef PATH_MAX
35 #define PATH_MAX DEFAULT_PATH_MAX
36 #endif
38 #include "WindowMaker.h"
39 #include "wcore.h"
40 #include "window.h"
41 #include "icon.h"
42 #include "appicon.h"
43 #include "actions.h"
44 #include "stacking.h"
45 #include "dock.h"
46 #include "dockedapp.h"
47 #include "dialog.h"
48 #include "main.h"
49 #include "properties.h"
50 #include "menu.h"
51 #include "client.h"
52 #include "defaults.h"
53 #include "workspace.h"
54 #include "framewin.h"
55 #include "superfluous.h"
56 #include "xinerama.h"
58 /**** Local variables ****/
59 #define CLIP_REWIND 1
60 #define CLIP_IDLE 0
61 #define CLIP_FORWARD 2
63 /**** Global variables ****/
64 extern Cursor wCursor[WCUR_LAST];
65 extern WPreferences wPreferences;
66 extern XContext wWinContext;
67 extern void appIconMouseDown(WObjDescriptor *desc, XEvent *event);
69 #define MOD_MASK wPreferences.modifier_mask
70 #define ICON_SIZE wPreferences.icon_size
72 /***** Local variables ****/
74 static WMPropList *dCommand = NULL;
75 static WMPropList *dPasteCommand = NULL;
76 #ifdef XDND /* XXX was OFFIX */
77 static WMPropList *dDropCommand = NULL;
78 #endif
79 static WMPropList *dAutoLaunch, *dLock;
80 static WMPropList *dName, *dForced, *dBuggyApplication, *dYes, *dNo;
81 static WMPropList *dHost, *dDock, *dClip;
82 static WMPropList *dAutoAttractIcons;
84 static WMPropList *dPosition, *dApplications, *dLowered, *dCollapsed;
86 static WMPropList *dAutoCollapse, *dAutoRaiseLower, *dOmnipresent;
88 static void dockIconPaint(WAppIcon *btn);
90 static void iconMouseDown(WObjDescriptor *desc, XEvent *event);
92 static pid_t execCommand(WAppIcon *btn, char *command, WSavedState *state);
94 static void trackDeadProcess(pid_t pid, unsigned char status, WDock *dock);
96 static int getClipButton(int px, int py);
98 static void toggleLowered(WDock *dock);
100 static void toggleCollapsed(WDock *dock);
102 static void clipIconExpose(WObjDescriptor *desc, XEvent *event);
104 static void clipLeave(WDock *dock);
106 static void handleClipChangeWorkspace(WScreen *scr, XEvent *event);
108 static Bool moveIconBetweenDocks(WDock *src, WDock *dest, WAppIcon *icon, int x, int y);
110 static void clipEnterNotify(WObjDescriptor *desc, XEvent *event);
111 static void clipLeaveNotify(WObjDescriptor *desc, XEvent *event);
112 static void clipAutoCollapse(void *cdata);
113 static void clipAutoExpand(void *cdata);
114 static void launchDockedApplication(WAppIcon *btn, Bool withSelection);
116 static void clipAutoLower(void *cdata);
117 static void clipAutoRaise(void *cdata);
118 static void reattachIcon(WDock *dock, WAppIcon *icon, int x, int y);
119 static WAppIcon *mainIconCreate(WScreen *scr, int type);
121 static void make_keys(void)
123 if (dCommand != NULL)
124 return;
126 dCommand = WMRetainPropList(WMCreatePLString("Command"));
127 dPasteCommand = WMRetainPropList(WMCreatePLString("PasteCommand"));
128 #ifdef XDND
129 dDropCommand = WMRetainPropList(WMCreatePLString("DropCommand"));
130 #endif
131 dLock = WMRetainPropList(WMCreatePLString("Lock"));
132 dAutoLaunch = WMRetainPropList(WMCreatePLString("AutoLaunch"));
133 dName = WMRetainPropList(WMCreatePLString("Name"));
134 dForced = WMRetainPropList(WMCreatePLString("Forced"));
135 dBuggyApplication = WMRetainPropList(WMCreatePLString("BuggyApplication"));
136 dYes = WMRetainPropList(WMCreatePLString("Yes"));
137 dNo = WMRetainPropList(WMCreatePLString("No"));
138 dHost = WMRetainPropList(WMCreatePLString("Host"));
140 dPosition = WMCreatePLString("Position");
141 dApplications = WMCreatePLString("Applications");
142 dLowered = WMCreatePLString("Lowered");
143 dCollapsed = WMCreatePLString("Collapsed");
144 dAutoCollapse = WMCreatePLString("AutoCollapse");
145 dAutoRaiseLower = WMCreatePLString("AutoRaiseLower");
146 dAutoAttractIcons = WMCreatePLString("AutoAttractIcons");
148 dOmnipresent = WMCreatePLString("Omnipresent");
150 dDock = WMCreatePLString("Dock");
151 dClip = WMCreatePLString("Clip");
154 static void renameCallback(WMenu *menu, WMenuEntry *entry)
156 WDock *dock = entry->clientdata;
157 char buffer[128];
158 int wspace;
159 char *name;
161 assert(entry->clientdata != NULL);
163 wspace = dock->screen_ptr->current_workspace;
165 name = wstrdup(dock->screen_ptr->workspaces[wspace]->name);
167 snprintf(buffer, sizeof(buffer), _("Type the name for workspace %i:"), wspace + 1);
168 if (wInputDialog(dock->screen_ptr, _("Rename Workspace"), buffer, &name)) {
169 wWorkspaceRename(dock->screen_ptr, wspace, name);
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++;
275 return n;
278 static WMArray *getSelected(WDock *dock)
280 WMArray *ret = WMCreateArray(8);
281 WAppIcon *btn;
282 int i;
284 for (i = 1; i < dock->max_icons; i++) {
285 btn = dock->icon_array[i];
286 if (btn && btn->icon->selected) {
287 WMAddToArray(ret, btn);
291 return ret;
294 static void paintClipButtons(WAppIcon *clipIcon, Bool lpushed, Bool rpushed)
296 Window win = clipIcon->icon->core->window;
297 WScreen *scr = clipIcon->icon->core->screen_ptr;
298 XPoint p[4];
299 int pt = CLIP_BUTTON_SIZE * ICON_SIZE / 64;
300 int tp = ICON_SIZE - pt;
301 int as = pt - 15; /* 15 = 5+5+5 */
302 GC gc = scr->draw_gc; /* maybe use WMColorGC() instead here? */
303 WMColor *color;
305 color = scr->clip_title_color[CLIP_NORMAL];
307 XSetForeground(dpy, gc, WMColorPixel(color));
309 if (rpushed) {
310 p[0].x = tp + 1;
311 p[0].y = 1;
312 p[1].x = ICON_SIZE - 2;
313 p[1].y = 1;
314 p[2].x = ICON_SIZE - 2;
315 p[2].y = pt - 1;
316 } else if (lpushed) {
317 p[0].x = 1;
318 p[0].y = tp;
319 p[1].x = pt;
320 p[1].y = ICON_SIZE - 2;
321 p[2].x = 1;
322 p[2].y = ICON_SIZE - 2;
324 if (lpushed || rpushed) {
325 XSetForeground(dpy, scr->draw_gc, scr->white_pixel);
326 XFillPolygon(dpy, win, scr->draw_gc, p, 3, Convex, CoordModeOrigin);
327 XSetForeground(dpy, scr->draw_gc, scr->black_pixel);
330 /* top right arrow */
331 p[0].x = p[3].x = ICON_SIZE - 5 - as;
332 p[0].y = p[3].y = 5;
333 p[1].x = ICON_SIZE - 6;
334 p[1].y = 5;
335 p[2].x = ICON_SIZE - 6;
336 p[2].y = 4 + as;
337 if (rpushed) {
338 XFillPolygon(dpy, win, scr->draw_gc, p, 3, Convex, CoordModeOrigin);
339 XDrawLines(dpy, win, scr->draw_gc, p, 4, CoordModeOrigin);
340 } else {
341 XFillPolygon(dpy, win, gc, p, 3, Convex, CoordModeOrigin);
342 XDrawLines(dpy, win, gc, p, 4, CoordModeOrigin);
345 /* bottom left arrow */
346 p[0].x = p[3].x = 5;
347 p[0].y = p[3].y = ICON_SIZE - 5 - as;
348 p[1].x = 5;
349 p[1].y = ICON_SIZE - 6;
350 p[2].x = 4 + as;
351 p[2].y = ICON_SIZE - 6;
352 if (lpushed) {
353 XFillPolygon(dpy, win, scr->draw_gc, p, 3, Convex, CoordModeOrigin);
354 XDrawLines(dpy, win, scr->draw_gc, p, 4, CoordModeOrigin);
355 } else {
356 XFillPolygon(dpy, win, gc, p, 3, Convex, CoordModeOrigin);
357 XDrawLines(dpy, win, gc, p, 4, CoordModeOrigin);
361 RImage *wClipMakeTile(WScreen *scr, RImage *normalTile)
363 RImage *tile = RCloneImage(normalTile);
364 RColor black;
365 RColor dark;
366 RColor light;
367 int pt, tp;
368 int as;
370 pt = CLIP_BUTTON_SIZE * wPreferences.icon_size / 64;
371 tp = wPreferences.icon_size - 1 - pt;
372 as = pt - 15;
374 black.alpha = 255;
375 black.red = black.green = black.blue = 0;
377 dark.alpha = 0;
378 dark.red = dark.green = dark.blue = 60;
380 light.alpha = 0;
381 light.red = light.green = light.blue = 80;
383 /* top right */
384 ROperateLine(tile, RSubtractOperation, tp, 0, wPreferences.icon_size - 2, pt - 1, &dark);
385 RDrawLine(tile, tp - 1, 0, wPreferences.icon_size - 1, pt + 1, &black);
386 ROperateLine(tile, RAddOperation, tp, 2, wPreferences.icon_size - 3, pt, &light);
388 /* arrow bevel */
389 ROperateLine(tile, RSubtractOperation, ICON_SIZE - 7 - as, 4, ICON_SIZE - 5, 4, &dark);
390 ROperateLine(tile, RSubtractOperation, ICON_SIZE - 6 - as, 5, ICON_SIZE - 5, 6 + as, &dark);
391 ROperateLine(tile, RAddOperation, ICON_SIZE - 5, 4, ICON_SIZE - 5, 6 + as, &light);
393 /* bottom left */
394 ROperateLine(tile, RAddOperation, 2, tp + 2, pt - 2, wPreferences.icon_size - 3, &dark);
395 RDrawLine(tile, 0, tp - 1, pt + 1, wPreferences.icon_size - 1, &black);
396 ROperateLine(tile, RSubtractOperation, 0, tp - 2, pt + 1, wPreferences.icon_size - 2, &light);
398 /* arrow bevel */
399 ROperateLine(tile, RSubtractOperation, 4, ICON_SIZE - 7 - as, 4, ICON_SIZE - 5, &dark);
400 ROperateLine(tile, RSubtractOperation, 5, ICON_SIZE - 6 - as, 6 + as, ICON_SIZE - 5, &dark);
401 ROperateLine(tile, RAddOperation, 4, ICON_SIZE - 5, 6 + as, ICON_SIZE - 5, &light);
403 return tile;
406 static void omnipresentCallback(WMenu *menu, WMenuEntry *entry)
408 WAppIcon *clickedIcon = entry->clientdata;
409 WAppIcon *aicon;
410 WDock *dock;
411 WMArray *selectedIcons;
412 WMArrayIterator iter;
413 int failed;
415 assert(entry->clientdata != NULL);
417 dock = clickedIcon->dock;
419 selectedIcons = getSelected(dock);
421 if (!WMGetArrayItemCount(selectedIcons))
422 WMAddToArray(selectedIcons, clickedIcon);
424 failed = 0;
425 WM_ITERATE_ARRAY(selectedIcons, aicon, iter) {
426 if (wClipMakeIconOmnipresent(aicon, !aicon->omnipresent) == WO_FAILED)
427 failed++;
428 else if (aicon->icon->selected)
429 wIconSelect(aicon->icon);
431 WMFreeArray(selectedIcons);
433 if (failed > 1) {
434 wMessageDialog(dock->screen_ptr, _("Warning"),
435 _("Some icons cannot be made omnipresent. "
436 "Please make sure that no other icon is "
437 "docked in the same positions on the other "
438 "workspaces and the Clip is not full in "
439 "some workspace."), _("OK"), NULL, NULL);
440 } else if (failed == 1) {
441 wMessageDialog(dock->screen_ptr, _("Warning"),
442 _("Icon cannot be made omnipresent. "
443 "Please make sure that no other icon is "
444 "docked in the same position on the other "
445 "workspaces and the Clip is not full in "
446 "some workspace."), _("OK"), NULL, NULL);
450 static void removeIconsCallback(WMenu *menu, WMenuEntry *entry)
452 WAppIcon *clickedIcon = (WAppIcon *) entry->clientdata;
453 WDock *dock;
454 WAppIcon *aicon;
455 WMArray *selectedIcons;
456 int keepit;
457 WMArrayIterator it;
459 assert(clickedIcon != NULL);
461 dock = clickedIcon->dock;
463 selectedIcons = getSelected(dock);
465 if (WMGetArrayItemCount(selectedIcons)) {
466 if (wMessageDialog(dock->screen_ptr, _("Workspace Clip"),
467 _("All selected icons will be removed!"),
468 _("OK"), _("Cancel"), NULL) != WAPRDefault) {
469 WMFreeArray(selectedIcons);
470 return;
472 } else {
473 if (clickedIcon->xindex == 0 && clickedIcon->yindex == 0) {
474 WMFreeArray(selectedIcons);
475 return;
477 WMAddToArray(selectedIcons, clickedIcon);
480 WM_ITERATE_ARRAY(selectedIcons, aicon, it) {
481 keepit = aicon->running && wApplicationOf(aicon->main_window);
482 wDockDetach(dock, aicon);
483 if (keepit) {
484 /* XXX: can: aicon->icon == NULL ? */
485 PlaceIcon(dock->screen_ptr, &aicon->x_pos, &aicon->y_pos,
486 wGetHeadForWindow(aicon->icon->owner));
487 XMoveWindow(dpy, aicon->icon->core->window, aicon->x_pos, aicon->y_pos);
488 if (!dock->mapped || dock->collapsed)
489 XMapWindow(dpy, aicon->icon->core->window);
492 WMFreeArray(selectedIcons);
494 if (wPreferences.auto_arrange_icons)
495 wArrangeIcons(dock->screen_ptr, True);
498 static void keepIconsCallback(WMenu *menu, WMenuEntry *entry)
500 WAppIcon *clickedIcon = (WAppIcon *) entry->clientdata;
501 WDock *dock;
502 WAppIcon *aicon;
503 WMArray *selectedIcons;
504 WMArrayIterator it;
506 assert(clickedIcon != NULL);
507 dock = clickedIcon->dock;
509 selectedIcons = getSelected(dock);
511 if (!WMGetArrayItemCount(selectedIcons)
512 && clickedIcon != dock->screen_ptr->clip_icon) {
513 char *command = NULL;
515 if (!clickedIcon->command && !clickedIcon->editing) {
516 clickedIcon->editing = 1;
517 if (wInputDialog(dock->screen_ptr, _("Keep Icon"),
518 _("Type the command used to launch the application"), &command)) {
519 if (command && (command[0] == 0 || (command[0] == '-' && command[1] == 0))) {
520 wfree(command);
521 command = NULL;
523 clickedIcon->command = command;
524 clickedIcon->editing = 0;
525 } else {
526 clickedIcon->editing = 0;
527 if (command)
528 wfree(command);
529 WMFreeArray(selectedIcons);
530 return;
534 WMAddToArray(selectedIcons, clickedIcon);
537 WM_ITERATE_ARRAY(selectedIcons, aicon, it) {
538 if (aicon->icon->selected)
539 wIconSelect(aicon->icon);
540 if (aicon && aicon->attracted && aicon->command) {
541 aicon->attracted = 0;
542 if (aicon->icon->shadowed) {
543 aicon->icon->shadowed = 0;
544 aicon->icon->force_paint = 1;
545 wAppIconPaint(aicon);
548 save_appicon(aicon, True);
550 WMFreeArray(selectedIcons);
553 static void toggleAutoAttractCallback(WMenu *menu, WMenuEntry *entry)
555 WDock *dock = (WDock *) entry->clientdata;
557 assert(entry->clientdata != NULL);
559 dock->attract_icons = !dock->attract_icons;
560 /*if (!dock->attract_icons)
561 dock->keep_attracted = 0; */
563 entry->flags.indicator_on = dock->attract_icons;
565 wMenuPaint(menu);
568 static void selectCallback(WMenu *menu, WMenuEntry *entry)
570 WAppIcon *icon = (WAppIcon *) entry->clientdata;
572 assert(icon != NULL);
574 wIconSelect(icon->icon);
576 wMenuPaint(menu);
579 static void colectIconsCallback(WMenu *menu, WMenuEntry *entry)
581 WAppIcon *clickedIcon = (WAppIcon *) entry->clientdata;
582 WDock *clip;
583 WAppIcon *aicon;
584 int x, y, x_pos, y_pos;
586 assert(entry->clientdata != NULL);
587 clip = clickedIcon->dock;
589 aicon = clip->screen_ptr->app_icon_list;
591 while (aicon) {
592 if (!aicon->docked && wDockFindFreeSlot(clip, &x, &y)) {
593 x_pos = clip->x_pos + x * ICON_SIZE;
594 y_pos = clip->y_pos + y * ICON_SIZE;
595 if (aicon->x_pos != x_pos || aicon->y_pos != y_pos)
596 move_window(aicon->icon->core->window, aicon->x_pos, aicon->y_pos, x_pos, y_pos);
598 aicon->attracted = 1;
599 if (!aicon->icon->shadowed) {
600 aicon->icon->shadowed = 1;
601 aicon->icon->force_paint = 1;
602 /* We don't do an wAppIconPaint() here because it's in
603 * wDockAttachIcon(). -Dan
606 wDockAttachIcon(clip, aicon, x, y);
607 if (clip->collapsed || !clip->mapped)
608 XUnmapWindow(dpy, aicon->icon->core->window);
610 aicon = aicon->next;
614 static void selectIconsCallback(WMenu *menu, WMenuEntry *entry)
616 WAppIcon *clickedIcon = (WAppIcon *) entry->clientdata;
617 WDock *dock;
618 WMArray *selectedIcons;
619 WMArrayIterator iter;
620 WAppIcon *btn;
621 int i;
623 assert(clickedIcon != NULL);
624 dock = clickedIcon->dock;
626 selectedIcons = getSelected(dock);
628 if (!WMGetArrayItemCount(selectedIcons)) {
629 for (i = 1; i < dock->max_icons; i++) {
630 btn = dock->icon_array[i];
631 if (btn && !btn->icon->selected) {
632 wIconSelect(btn->icon);
635 } else {
636 WM_ITERATE_ARRAY(selectedIcons, btn, iter) {
637 wIconSelect(btn->icon);
640 WMFreeArray(selectedIcons);
642 wMenuPaint(menu);
645 static void toggleCollapsedCallback(WMenu *menu, WMenuEntry *entry)
647 assert(entry->clientdata != NULL);
649 toggleCollapsed(entry->clientdata);
651 entry->flags.indicator_on = ((WDock *) entry->clientdata)->collapsed;
653 wMenuPaint(menu);
656 static void toggleAutoCollapseCallback(WMenu *menu, WMenuEntry *entry)
658 WDock *dock;
659 assert(entry->clientdata != NULL);
661 dock = (WDock *) entry->clientdata;
663 dock->auto_collapse = !dock->auto_collapse;
665 entry->flags.indicator_on = ((WDock *) entry->clientdata)->auto_collapse;
667 wMenuPaint(menu);
670 static void toggleAutoRaiseLowerCallback(WMenu *menu, WMenuEntry *entry)
672 WDock *dock;
673 assert(entry->clientdata != NULL);
675 dock = (WDock *) entry->clientdata;
677 dock->auto_raise_lower = !dock->auto_raise_lower;
679 entry->flags.indicator_on = ((WDock *) entry->clientdata)->auto_raise_lower;
681 wMenuPaint(menu);
684 static void launchCallback(WMenu *menu, WMenuEntry *entry)
686 WAppIcon *btn = (WAppIcon *) entry->clientdata;
688 launchDockedApplication(btn, False);
691 static void settingsCallback(WMenu *menu, WMenuEntry *entry)
693 WAppIcon *btn = (WAppIcon *) entry->clientdata;
695 if (btn->editing)
696 return;
697 ShowDockAppSettingsPanel(btn);
700 static void hideCallback(WMenu *menu, WMenuEntry *entry)
702 WApplication *wapp;
703 WAppIcon *btn = (WAppIcon *) entry->clientdata;
705 wapp = wApplicationOf(btn->icon->owner->main_window);
707 if (wapp->flags.hidden) {
708 wWorkspaceChange(btn->icon->core->screen_ptr, wapp->last_workspace);
709 wUnhideApplication(wapp, False, False);
710 } else {
711 wHideApplication(wapp);
715 static void unhideHereCallback(WMenu *menu, WMenuEntry *entry)
717 WApplication *wapp;
718 WAppIcon *btn = (WAppIcon *) entry->clientdata;
720 wapp = wApplicationOf(btn->icon->owner->main_window);
722 wUnhideApplication(wapp, False, True);
725 static WAppIcon *mainIconCreate(WScreen *scr, int type)
727 WAppIcon *btn;
728 int x_pos;
730 if (type == WM_CLIP) {
731 if (scr->clip_icon)
732 return scr->clip_icon;
733 btn = wAppIconCreateForDock(scr, NULL, "Logo", "WMClip", TILE_CLIP);
734 btn->icon->core->descriptor.handle_expose = clipIconExpose;
735 btn->icon->core->descriptor.handle_enternotify = clipEnterNotify;
736 btn->icon->core->descriptor.handle_leavenotify = clipLeaveNotify;
737 /*x_pos = scr->scr_width - ICON_SIZE*2 - DOCK_EXTRA_SPACE; */
738 x_pos = 0;
739 } else {
740 btn = wAppIconCreateForDock(scr, NULL, "Logo", "WMDock", TILE_NORMAL);
741 x_pos = scr->scr_width - ICON_SIZE - DOCK_EXTRA_SPACE;
744 btn->xindex = 0;
745 btn->yindex = 0;
747 btn->icon->core->descriptor.handle_mousedown = iconMouseDown;
748 btn->icon->core->descriptor.parent_type = WCLASS_DOCK_ICON;
749 btn->icon->core->descriptor.parent = btn;
750 /*ChangeStackingLevel(btn->icon->core, WMDockLevel); */
751 XMapWindow(dpy, btn->icon->core->window);
752 btn->x_pos = x_pos;
753 btn->y_pos = 0;
754 btn->docked = 1;
755 if (type == WM_CLIP)
756 scr->clip_icon = btn;
758 return btn;
761 static void switchWSCommand(WMenu *menu, WMenuEntry *entry)
763 WAppIcon *btn, *icon = (WAppIcon *) entry->clientdata;
764 WScreen *scr = icon->icon->core->screen_ptr;
765 WDock *src, *dest;
766 WMArray *selectedIcons;
767 int x, y;
769 if (entry->order == scr->current_workspace)
770 return;
771 src = icon->dock;
772 dest = scr->workspaces[entry->order]->clip;
774 selectedIcons = getSelected(src);
776 if (WMGetArrayItemCount(selectedIcons)) {
777 WMArrayIterator iter;
779 WM_ITERATE_ARRAY(selectedIcons, btn, iter) {
780 if (wDockFindFreeSlot(dest, &x, &y)) {
781 moveIconBetweenDocks(src, dest, btn, x, y);
782 XUnmapWindow(dpy, btn->icon->core->window);
785 } else if (icon != scr->clip_icon) {
786 if (wDockFindFreeSlot(dest, &x, &y)) {
787 moveIconBetweenDocks(src, dest, icon, x, y);
788 XUnmapWindow(dpy, icon->icon->core->window);
791 WMFreeArray(selectedIcons);
794 static void launchDockedApplication(WAppIcon *btn, Bool withSelection)
796 WScreen *scr = btn->icon->core->screen_ptr;
798 if (!btn->launching &&
799 ((!withSelection && btn->command != NULL) || (withSelection && btn->paste_command != NULL))) {
800 if (!btn->forced_dock) {
801 btn->relaunching = btn->running;
802 btn->running = 1;
804 if (btn->wm_instance || btn->wm_class) {
805 WWindowAttributes attr;
806 memset(&attr, 0, sizeof(WWindowAttributes));
807 wDefaultFillAttributes(scr, btn->wm_instance, btn->wm_class, &attr, NULL, True);
809 if (!attr.no_appicon && !btn->buggy_app)
810 btn->launching = 1;
811 else
812 btn->running = 0;
814 btn->drop_launch = 0;
815 btn->paste_launch = withSelection;
816 scr->last_dock = btn->dock;
817 btn->pid = execCommand(btn, (withSelection ? btn->paste_command : btn->command), NULL);
818 if (btn->pid > 0) {
819 if (btn->buggy_app) {
820 /* give feedback that the app was launched */
821 btn->launching = 1;
822 dockIconPaint(btn);
823 btn->launching = 0;
824 WMAddTimerHandler(200, (WMCallback *) dockIconPaint, btn);
825 } else {
826 dockIconPaint(btn);
828 } else {
829 wwarning(_("could not launch application %s"), btn->command);
830 btn->launching = 0;
831 if (!btn->relaunching) {
832 btn->running = 0;
838 static void updateWorkspaceMenu(WMenu *menu, WAppIcon *icon)
840 WScreen *scr = menu->frame->screen_ptr;
841 char title[MAX_WORKSPACENAME_WIDTH + 1];
842 int i;
844 if (!menu || !icon)
845 return;
847 for (i = 0; i < scr->workspace_count; i++) {
848 if (i < menu->entry_no) {
849 if (strcmp(menu->entries[i]->text, scr->workspaces[i]->name) != 0) {
850 wfree(menu->entries[i]->text);
851 strcpy(title, scr->workspaces[i]->name);
852 menu->entries[i]->text = wstrdup(title);
853 menu->flags.realized = 0;
855 menu->entries[i]->clientdata = (void *)icon;
856 } else {
857 strcpy(title, scr->workspaces[i]->name);
859 wMenuAddCallback(menu, title, switchWSCommand, (void *)icon);
861 menu->flags.realized = 0;
863 if (i == scr->current_workspace) {
864 wMenuSetEnabled(menu, i, False);
865 } else {
866 wMenuSetEnabled(menu, i, True);
870 if (!menu->flags.realized)
871 wMenuRealize(menu);
874 static WMenu *makeWorkspaceMenu(WScreen *scr)
876 WMenu *menu;
878 menu = wMenuCreate(scr, NULL, False);
879 if (!menu)
880 wwarning(_("could not create workspace submenu for Clip menu"));
882 wMenuAddCallback(menu, "", switchWSCommand, (void *)scr->clip_icon);
884 menu->flags.realized = 0;
885 wMenuRealize(menu);
887 return menu;
890 static void updateClipOptionsMenu(WMenu *menu, WDock *dock)
892 WMenuEntry *entry;
893 int index = 0;
895 if (!menu || !dock)
896 return;
898 /* keep on top */
899 entry = menu->entries[index];
900 entry->flags.indicator_on = !dock->lowered;
901 entry->clientdata = dock;
903 /* collapsed */
904 entry = menu->entries[++index];
905 entry->flags.indicator_on = dock->collapsed;
906 entry->clientdata = dock;
908 /* auto-collapse */
909 entry = menu->entries[++index];
910 entry->flags.indicator_on = dock->auto_collapse;
911 entry->clientdata = dock;
913 /* auto-raise/lower */
914 entry = menu->entries[++index];
915 entry->flags.indicator_on = dock->auto_raise_lower;
916 entry->clientdata = dock;
917 wMenuSetEnabled(menu, index, dock->lowered);
919 /* attract icons */
920 entry = menu->entries[++index];
921 entry->flags.indicator_on = dock->attract_icons;
922 entry->clientdata = dock;
924 menu->flags.realized = 0;
925 wMenuRealize(menu);
928 static WMenu *makeClipOptionsMenu(WScreen *scr)
930 WMenu *menu;
931 WMenuEntry *entry;
933 menu = wMenuCreate(scr, NULL, False);
934 if (!menu) {
935 wwarning(_("could not create options submenu for Clip menu"));
936 return NULL;
939 entry = wMenuAddCallback(menu, _("Keep on Top"), toggleLoweredCallback, NULL);
940 entry->flags.indicator = 1;
941 entry->flags.indicator_on = 1;
942 entry->flags.indicator_type = MI_CHECK;
944 entry = wMenuAddCallback(menu, _("Collapsed"), toggleCollapsedCallback, NULL);
945 entry->flags.indicator = 1;
946 entry->flags.indicator_on = 1;
947 entry->flags.indicator_type = MI_CHECK;
949 entry = wMenuAddCallback(menu, _("Autocollapse"), toggleAutoCollapseCallback, NULL);
950 entry->flags.indicator = 1;
951 entry->flags.indicator_on = 1;
952 entry->flags.indicator_type = MI_CHECK;
954 entry = wMenuAddCallback(menu, _("Autoraise"), toggleAutoRaiseLowerCallback, NULL);
955 entry->flags.indicator = 1;
956 entry->flags.indicator_on = 1;
957 entry->flags.indicator_type = MI_CHECK;
959 entry = wMenuAddCallback(menu, _("Autoattract Icons"), toggleAutoAttractCallback, NULL);
960 entry->flags.indicator = 1;
961 entry->flags.indicator_on = 1;
962 entry->flags.indicator_type = MI_CHECK;
964 menu->flags.realized = 0;
965 wMenuRealize(menu);
967 return menu;
970 static WMenu *dockMenuCreate(WScreen *scr, int type)
972 WMenu *menu;
973 WMenuEntry *entry;
975 if (type == WM_CLIP && scr->clip_menu)
976 return scr->clip_menu;
978 menu = wMenuCreate(scr, NULL, False);
979 if (type != WM_CLIP) {
980 entry = wMenuAddCallback(menu, _("Keep on Top"), toggleLoweredCallback, NULL);
981 entry->flags.indicator = 1;
982 entry->flags.indicator_on = 1;
983 entry->flags.indicator_type = MI_CHECK;
984 } else {
985 entry = wMenuAddCallback(menu, _("Clip Options"), NULL, NULL);
986 scr->clip_options = makeClipOptionsMenu(scr);
987 if (scr->clip_options)
988 wMenuEntrySetCascade(menu, entry, scr->clip_options);
990 entry = wMenuAddCallback(menu, _("Rename Workspace"), renameCallback, NULL);
991 wfree(entry->text);
992 entry->text = _("Rename Workspace");
994 entry = wMenuAddCallback(menu, _("Selected"), selectCallback, NULL);
995 entry->flags.indicator = 1;
996 entry->flags.indicator_on = 1;
997 entry->flags.indicator_type = MI_CHECK;
999 entry = wMenuAddCallback(menu, _("Select All Icons"), selectIconsCallback, NULL);
1000 wfree(entry->text);
1001 entry->text = _("Select All Icons");
1003 entry = wMenuAddCallback(menu, _("Keep Icon"), keepIconsCallback, NULL);
1004 wfree(entry->text);
1005 entry->text = _("Keep Icon");
1007 entry = wMenuAddCallback(menu, _("Move Icon To"), NULL, NULL);
1008 wfree(entry->text);
1009 entry->text = _("Move Icon To");
1010 scr->clip_submenu = makeWorkspaceMenu(scr);
1011 if (scr->clip_submenu)
1012 wMenuEntrySetCascade(menu, entry, scr->clip_submenu);
1014 entry = wMenuAddCallback(menu, _("Remove Icon"), removeIconsCallback, NULL);
1015 wfree(entry->text);
1016 entry->text = _("Remove Icon");
1018 wMenuAddCallback(menu, _("Attract Icons"), colectIconsCallback, NULL);
1021 wMenuAddCallback(menu, _("Launch"), launchCallback, NULL);
1023 wMenuAddCallback(menu, _("Unhide Here"), unhideHereCallback, NULL);
1025 entry = wMenuAddCallback(menu, _("Hide"), hideCallback, NULL);
1026 wfree(entry->text);
1027 entry->text = _("Hide");
1029 wMenuAddCallback(menu, _("Settings..."), settingsCallback, NULL);
1031 wMenuAddCallback(menu, _("Kill"), killCallback, NULL);
1033 if (type == WM_CLIP)
1034 scr->clip_menu = menu;
1036 return menu;
1039 WDock *wDockCreate(WScreen *scr, int type)
1041 WDock *dock;
1042 WAppIcon *btn;
1044 make_keys();
1046 dock = wmalloc(sizeof(WDock));
1048 dock->max_icons = DOCK_MAX_ICONS;
1050 dock->icon_array = wmalloc(sizeof(WAppIcon *) * dock->max_icons);
1052 btn = mainIconCreate(scr, type);
1054 btn->dock = dock;
1056 dock->x_pos = btn->x_pos;
1057 dock->y_pos = btn->y_pos;
1058 dock->screen_ptr = scr;
1059 dock->type = type;
1060 dock->icon_count = 1;
1061 dock->on_right_side = 1;
1062 dock->collapsed = 0;
1063 dock->auto_collapse = 0;
1064 dock->auto_collapse_magic = NULL;
1065 dock->auto_raise_lower = 0;
1066 dock->auto_lower_magic = NULL;
1067 dock->auto_raise_magic = NULL;
1068 dock->attract_icons = 0;
1069 dock->lowered = 1;
1070 dock->icon_array[0] = btn;
1071 wRaiseFrame(btn->icon->core);
1072 XMoveWindow(dpy, btn->icon->core->window, btn->x_pos, btn->y_pos);
1074 /* create dock menu */
1075 dock->menu = dockMenuCreate(scr, type);
1077 return dock;
1080 void wDockDestroy(WDock *dock)
1082 int i;
1083 WAppIcon *aicon;
1085 for (i = (dock->type == WM_CLIP) ? 1 : 0; i < dock->max_icons; i++) {
1086 aicon = dock->icon_array[i];
1087 if (aicon) {
1088 int keepit = aicon->running && wApplicationOf(aicon->main_window);
1089 wDockDetach(dock, aicon);
1090 if (keepit) {
1091 /* XXX: can: aicon->icon == NULL ? */
1092 PlaceIcon(dock->screen_ptr, &aicon->x_pos, &aicon->y_pos,
1093 wGetHeadForWindow(aicon->icon->owner));
1094 XMoveWindow(dpy, aicon->icon->core->window, aicon->x_pos, aicon->y_pos);
1095 if (!dock->mapped || dock->collapsed)
1096 XMapWindow(dpy, aicon->icon->core->window);
1100 if (wPreferences.auto_arrange_icons)
1101 wArrangeIcons(dock->screen_ptr, True);
1102 wfree(dock->icon_array);
1103 if (dock->menu && dock->type != WM_CLIP)
1104 wMenuDestroy(dock->menu, True);
1105 if (dock->screen_ptr->last_dock == dock)
1106 dock->screen_ptr->last_dock = NULL;
1107 wfree(dock);
1110 void wClipIconPaint(WAppIcon *aicon)
1112 WScreen *scr = aicon->icon->core->screen_ptr;
1113 WWorkspace *workspace = scr->workspaces[scr->current_workspace];
1114 WMColor *color;
1115 Window win = aicon->icon->core->window;
1116 int length, nlength;
1117 char *ws_name, ws_number[10];
1118 int ty, tx;
1120 wIconPaint(aicon->icon);
1122 length = strlen(workspace->name);
1123 ws_name = wmalloc(length + 1);
1124 snprintf(ws_name, length + 1, "%s", workspace->name);
1125 snprintf(ws_number, sizeof(ws_number), "%i", scr->current_workspace + 1);
1126 nlength = strlen(ws_number);
1128 if (!workspace->clip->collapsed)
1129 color = scr->clip_title_color[CLIP_NORMAL];
1130 else
1131 color = scr->clip_title_color[CLIP_COLLAPSED];
1133 ty = ICON_SIZE - WMFontHeight(scr->clip_title_font) - 3;
1135 tx = CLIP_BUTTON_SIZE * ICON_SIZE / 64;
1137 if(wPreferences.show_clip_title)
1138 WMDrawString(scr->wmscreen, win, color, scr->clip_title_font, tx, ty, ws_name, length);
1140 tx = (ICON_SIZE / 2 - WMWidthOfString(scr->clip_title_font, ws_number, nlength)) / 2;
1142 WMDrawString(scr->wmscreen, win, color, scr->clip_title_font, tx, 2, ws_number, nlength);
1144 wfree(ws_name);
1146 if (aicon->launching) {
1147 XFillRectangle(dpy, aicon->icon->core->window, scr->stipple_gc,
1148 0, 0, wPreferences.icon_size, wPreferences.icon_size);
1150 paintClipButtons(aicon, aicon->dock->lclip_button_pushed, aicon->dock->rclip_button_pushed);
1153 static void clipIconExpose(WObjDescriptor *desc, XEvent *event)
1155 wClipIconPaint(desc->parent);
1158 static void dockIconPaint(WAppIcon *btn)
1160 if (btn == btn->icon->core->screen_ptr->clip_icon)
1161 wClipIconPaint(btn);
1162 else {
1163 wAppIconPaint(btn);
1164 save_appicon(btn, True);
1168 static WMPropList *make_icon_state(WAppIcon *btn)
1170 WMPropList *node = NULL;
1171 WMPropList *command, *autolaunch, *lock, *name, *forced;
1172 WMPropList *position, *buggy, *omnipresent;
1173 char *tmp;
1174 char buffer[64];
1176 if (btn) {
1177 if (!btn->command)
1178 command = WMCreatePLString("-");
1179 else
1180 command = WMCreatePLString(btn->command);
1182 autolaunch = btn->auto_launch ? dYes : dNo;
1184 lock = btn->lock ? dYes : dNo;
1186 tmp = EscapeWM_CLASS(btn->wm_instance, btn->wm_class);
1188 name = WMCreatePLString(tmp);
1190 wfree(tmp);
1192 forced = btn->forced_dock ? dYes : dNo;
1194 buggy = btn->buggy_app ? dYes : dNo;
1196 if (btn == btn->icon->core->screen_ptr->clip_icon)
1197 snprintf(buffer, sizeof(buffer), "%i,%i", btn->x_pos, btn->y_pos);
1198 else
1199 snprintf(buffer, sizeof(buffer), "%hi,%hi", btn->xindex, btn->yindex);
1200 position = WMCreatePLString(buffer);
1202 node = WMCreatePLDictionary(dCommand, command,
1203 dName, name,
1204 dAutoLaunch, autolaunch,
1205 dLock, lock,
1206 dForced, forced, dBuggyApplication, buggy, dPosition, position, NULL);
1207 WMReleasePropList(command);
1208 WMReleasePropList(name);
1209 WMReleasePropList(position);
1211 omnipresent = btn->omnipresent ? dYes : dNo;
1212 if (btn->dock != btn->icon->core->screen_ptr->dock && (btn->xindex != 0 || btn->yindex != 0))
1213 WMPutInPLDictionary(node, dOmnipresent, omnipresent);
1215 #ifdef XDND /* was OFFIX */
1216 if (btn->dnd_command) {
1217 command = WMCreatePLString(btn->dnd_command);
1218 WMPutInPLDictionary(node, dDropCommand, command);
1219 WMReleasePropList(command);
1221 #endif /* XDND */
1223 if (btn->paste_command) {
1224 command = WMCreatePLString(btn->paste_command);
1225 WMPutInPLDictionary(node, dPasteCommand, command);
1226 WMReleasePropList(command);
1230 return node;
1233 static WMPropList *dockSaveState(WDock *dock)
1235 int i;
1236 WMPropList *icon_info;
1237 WMPropList *list = NULL, *dock_state = NULL;
1238 WMPropList *value, *key;
1239 char buffer[256];
1241 list = WMCreatePLArray(NULL);
1243 for (i = (dock->type == WM_DOCK ? 0 : 1); i < dock->max_icons; i++) {
1244 WAppIcon *btn = dock->icon_array[i];
1246 if (!btn || btn->attracted)
1247 continue;
1249 if ((icon_info = make_icon_state(dock->icon_array[i]))) {
1250 WMAddToPLArray(list, icon_info);
1251 WMReleasePropList(icon_info);
1255 dock_state = WMCreatePLDictionary(dApplications, list, NULL);
1257 if (dock->type == WM_DOCK) {
1258 snprintf(buffer, sizeof(buffer), "Applications%i", dock->screen_ptr->scr_height);
1259 key = WMCreatePLString(buffer);
1260 WMPutInPLDictionary(dock_state, key, list);
1261 WMReleasePropList(key);
1263 snprintf(buffer, sizeof(buffer), "%i,%i", (dock->on_right_side ? -ICON_SIZE : 0), dock->y_pos);
1264 value = WMCreatePLString(buffer);
1265 WMPutInPLDictionary(dock_state, dPosition, value);
1266 WMReleasePropList(value);
1268 WMReleasePropList(list);
1270 value = (dock->lowered ? dYes : dNo);
1271 WMPutInPLDictionary(dock_state, dLowered, value);
1273 if (dock->type == WM_CLIP) {
1274 value = (dock->collapsed ? dYes : dNo);
1275 WMPutInPLDictionary(dock_state, dCollapsed, value);
1277 value = (dock->auto_collapse ? dYes : dNo);
1278 WMPutInPLDictionary(dock_state, dAutoCollapse, value);
1280 value = (dock->auto_raise_lower ? dYes : dNo);
1281 WMPutInPLDictionary(dock_state, dAutoRaiseLower, value);
1283 value = (dock->attract_icons ? dYes : dNo);
1284 WMPutInPLDictionary(dock_state, dAutoAttractIcons, value);
1287 return dock_state;
1290 void wDockSaveState(WScreen *scr, WMPropList *old_state)
1292 WMPropList *dock_state;
1293 WMPropList *keys;
1295 dock_state = dockSaveState(scr->dock);
1298 * Copy saved states of docks with different sizes.
1300 if (old_state) {
1301 int i;
1302 WMPropList *tmp;
1304 keys = WMGetPLDictionaryKeys(old_state);
1305 for (i = 0; i < WMGetPropListItemCount(keys); i++) {
1306 tmp = WMGetFromPLArray(keys, i);
1308 if (strncasecmp(WMGetFromPLString(tmp), "applications", 12) == 0
1309 && !WMGetFromPLDictionary(dock_state, tmp)) {
1311 WMPutInPLDictionary(dock_state, tmp, WMGetFromPLDictionary(old_state, tmp));
1314 WMReleasePropList(keys);
1317 WMPutInPLDictionary(scr->session_state, dDock, dock_state);
1319 WMReleasePropList(dock_state);
1322 void wClipSaveState(WScreen *scr)
1324 WMPropList *clip_state;
1326 clip_state = make_icon_state(scr->clip_icon);
1328 WMPutInPLDictionary(scr->session_state, dClip, clip_state);
1330 WMReleasePropList(clip_state);
1333 WMPropList *wClipSaveWorkspaceState(WScreen *scr, int workspace)
1335 return dockSaveState(scr->workspaces[workspace]->clip);
1338 static Bool getBooleanDockValue(WMPropList *value, WMPropList *key)
1340 if (value) {
1341 if (WMIsPLString(value)) {
1342 if (strcasecmp(WMGetFromPLString(value), "YES") == 0)
1343 return True;
1344 } else {
1345 wwarning(_("bad value in docked icon state info %s"), WMGetFromPLString(key));
1348 return False;
1351 static WAppIcon *restore_icon_state(WScreen *scr, WMPropList *info, int type, int index)
1353 WAppIcon *aicon;
1354 WMPropList *cmd, *value;
1356 cmd = WMGetFromPLDictionary(info, dCommand);
1357 if (!cmd || !WMIsPLString(cmd)) {
1358 return NULL;
1361 /* parse window name */
1362 value = WMGetFromPLDictionary(info, dName);
1363 if (!value)
1364 return NULL;
1367 char *wclass, *winstance;
1368 char *command;
1370 ParseWindowName(value, &winstance, &wclass, "dock");
1372 if (!winstance && !wclass) {
1373 return NULL;
1376 /* get commands */
1378 if (cmd)
1379 command = wstrdup(WMGetFromPLString(cmd));
1380 else
1381 command = NULL;
1383 if (!command || strcmp(command, "-") == 0) {
1384 if (command)
1385 wfree(command);
1386 if (wclass)
1387 wfree(wclass);
1388 if (winstance)
1389 wfree(winstance);
1391 return NULL;
1394 aicon = wAppIconCreateForDock(scr, command, winstance, wclass, TILE_NORMAL);
1395 if (wclass)
1396 wfree(wclass);
1397 if (winstance)
1398 wfree(winstance);
1399 if (command)
1400 wfree(command);
1403 aicon->icon->core->descriptor.handle_mousedown = iconMouseDown;
1404 if (type == WM_CLIP) {
1405 aicon->icon->core->descriptor.handle_enternotify = clipEnterNotify;
1406 aicon->icon->core->descriptor.handle_leavenotify = clipLeaveNotify;
1408 aicon->icon->core->descriptor.parent_type = WCLASS_DOCK_ICON;
1409 aicon->icon->core->descriptor.parent = aicon;
1411 #ifdef XDND /* was OFFIX */
1412 cmd = WMGetFromPLDictionary(info, dDropCommand);
1413 if (cmd)
1414 aicon->dnd_command = wstrdup(WMGetFromPLString(cmd));
1415 #endif
1417 cmd = WMGetFromPLDictionary(info, dPasteCommand);
1418 if (cmd)
1419 aicon->paste_command = wstrdup(WMGetFromPLString(cmd));
1421 /* check auto launch */
1422 value = WMGetFromPLDictionary(info, dAutoLaunch);
1424 aicon->auto_launch = getBooleanDockValue(value, dAutoLaunch);
1426 /* check lock */
1427 value = WMGetFromPLDictionary(info, dLock);
1429 aicon->lock = getBooleanDockValue(value, dLock);
1431 /* check if it wasn't normally docked */
1432 value = WMGetFromPLDictionary(info, dForced);
1434 aicon->forced_dock = getBooleanDockValue(value, dForced);
1436 /* check if we can rely on the stuff in the app */
1437 value = WMGetFromPLDictionary(info, dBuggyApplication);
1439 aicon->buggy_app = getBooleanDockValue(value, dBuggyApplication);
1441 /* get position in the dock */
1442 value = WMGetFromPLDictionary(info, dPosition);
1443 if (value && WMIsPLString(value)) {
1444 if (sscanf(WMGetFromPLString(value), "%hi,%hi", &aicon->xindex, &aicon->yindex) != 2)
1445 wwarning(_("bad value in docked icon state info %s"), WMGetFromPLString(dPosition));
1447 /* check position sanity */
1448 /* incomplete section! */
1449 if (type == WM_DOCK) {
1450 aicon->xindex = 0;
1451 if (aicon->yindex < 0)
1452 wwarning(_("bad value in docked icon position %i,%i"),
1453 aicon->xindex, aicon->yindex);
1455 } else {
1456 aicon->yindex = index;
1457 aicon->xindex = 0;
1460 /* check if icon is omnipresent */
1461 value = WMGetFromPLDictionary(info, dOmnipresent);
1463 aicon->omnipresent = getBooleanDockValue(value, dOmnipresent);
1465 aicon->running = 0;
1466 aicon->docked = 1;
1468 return aicon;
1471 #define COMPLAIN(key) wwarning(_("bad value in dock state info:%s"), key)
1473 WAppIcon *wClipRestoreState(WScreen *scr, WMPropList *clip_state)
1475 WAppIcon *icon;
1476 WMPropList *value;
1478 icon = mainIconCreate(scr, WM_CLIP);
1480 if (!clip_state)
1481 return icon;
1483 WMRetainPropList(clip_state);
1485 /* restore position */
1487 value = WMGetFromPLDictionary(clip_state, dPosition);
1489 if (value) {
1490 if (!WMIsPLString(value))
1491 COMPLAIN("Position");
1492 else {
1493 WMRect rect;
1494 int flags;
1496 if (sscanf(WMGetFromPLString(value), "%i,%i", &icon->x_pos, &icon->y_pos) != 2)
1497 COMPLAIN("Position");
1499 /* check position sanity */
1500 rect.pos.x = icon->x_pos;
1501 rect.pos.y = icon->y_pos;
1502 rect.size.width = rect.size.height = ICON_SIZE;
1504 wGetRectPlacementInfo(scr, rect, &flags);
1505 if (flags & (XFLAG_DEAD | XFLAG_PARTIAL))
1506 wScreenKeepInside(scr, &icon->x_pos, &icon->y_pos, ICON_SIZE, ICON_SIZE);
1509 #ifdef XDND /* was OFFIX */
1510 value = WMGetFromPLDictionary(clip_state, dDropCommand);
1511 if (value && WMIsPLString(value))
1512 icon->dnd_command = wstrdup(WMGetFromPLString(value));
1513 #endif
1515 value = WMGetFromPLDictionary(clip_state, dPasteCommand);
1516 if (value && WMIsPLString(value))
1517 icon->paste_command = wstrdup(WMGetFromPLString(value));
1519 WMReleasePropList(clip_state);
1521 return icon;
1524 WDock *wDockRestoreState(WScreen *scr, WMPropList *dock_state, int type)
1526 WDock *dock;
1527 WMPropList *apps;
1528 WMPropList *value;
1529 WAppIcon *aicon, *old_top;
1530 int count, i;
1532 dock = wDockCreate(scr, type);
1534 if (!dock_state)
1535 return dock;
1537 WMRetainPropList(dock_state);
1539 /* restore position */
1540 value = WMGetFromPLDictionary(dock_state, dPosition);
1541 if (value) {
1542 if (!WMIsPLString(value)) {
1543 COMPLAIN("Position");
1544 } else {
1545 WMRect rect;
1546 int flags;
1548 if (sscanf(WMGetFromPLString(value), "%i,%i", &dock->x_pos, &dock->y_pos) != 2)
1549 COMPLAIN("Position");
1551 /* check position sanity */
1552 rect.pos.x = dock->x_pos;
1553 rect.pos.y = dock->y_pos;
1554 rect.size.width = rect.size.height = ICON_SIZE;
1556 wGetRectPlacementInfo(scr, rect, &flags);
1557 if (flags & (XFLAG_DEAD | XFLAG_PARTIAL)) {
1558 int x = dock->x_pos;
1559 wScreenKeepInside(scr, &x, &dock->y_pos, ICON_SIZE, ICON_SIZE);
1562 /* Is this needed any more? */
1563 if (type == WM_CLIP) {
1564 if (dock->x_pos < 0) {
1565 dock->x_pos = 0;
1566 } else if (dock->x_pos > scr->scr_width - ICON_SIZE) {
1567 dock->x_pos = scr->scr_width - ICON_SIZE;
1569 } else {
1570 if (dock->x_pos >= 0) {
1571 dock->x_pos = DOCK_EXTRA_SPACE;
1572 dock->on_right_side = 0;
1573 } else {
1574 dock->x_pos = scr->scr_width - DOCK_EXTRA_SPACE - ICON_SIZE;
1575 dock->on_right_side = 1;
1581 /* restore lowered/raised state */
1582 dock->lowered = 0;
1584 value = WMGetFromPLDictionary(dock_state, dLowered);
1585 if (value) {
1586 if (!WMIsPLString(value)) {
1587 COMPLAIN("Lowered");
1588 } else {
1589 if (strcasecmp(WMGetFromPLString(value), "YES") == 0) {
1590 dock->lowered = 1;
1595 /* restore collapsed state */
1596 dock->collapsed = 0;
1598 value = WMGetFromPLDictionary(dock_state, dCollapsed);
1599 if (value) {
1600 if (!WMIsPLString(value)) {
1601 COMPLAIN("Collapsed");
1602 } else {
1603 if (strcasecmp(WMGetFromPLString(value), "YES") == 0) {
1604 dock->collapsed = 1;
1609 /* restore auto-collapsed state */
1610 value = WMGetFromPLDictionary(dock_state, dAutoCollapse);
1611 if (value) {
1612 if (!WMIsPLString(value)) {
1613 COMPLAIN("AutoCollapse");
1614 } else {
1615 if (strcasecmp(WMGetFromPLString(value), "YES") == 0) {
1616 dock->auto_collapse = 1;
1617 dock->collapsed = 1;
1622 /* restore auto-raise/lower state */
1623 value = WMGetFromPLDictionary(dock_state, dAutoRaiseLower);
1624 if (value) {
1625 if (!WMIsPLString(value)) {
1626 COMPLAIN("AutoRaiseLower");
1627 } else {
1628 if (strcasecmp(WMGetFromPLString(value), "YES") == 0)
1629 dock->auto_raise_lower = 1;
1633 /* restore attract icons state */
1634 dock->attract_icons = 0;
1636 value = WMGetFromPLDictionary(dock_state, dAutoAttractIcons);
1637 if (value) {
1638 if (!WMIsPLString(value)) {
1639 COMPLAIN("AutoAttractIcons");
1640 } else {
1641 if (strcasecmp(WMGetFromPLString(value), "YES") == 0)
1642 dock->attract_icons = 1;
1646 /* application list */
1649 WMPropList *tmp;
1650 char buffer[64];
1653 * When saving, it saves the dock state in
1654 * Applications and Applicationsnnn
1656 * When loading, it will first try Applicationsnnn.
1657 * If it does not exist, use Applications as default.
1660 snprintf(buffer, sizeof(buffer), "Applications%i", scr->scr_height);
1662 tmp = WMCreatePLString(buffer);
1663 apps = WMGetFromPLDictionary(dock_state, tmp);
1664 WMReleasePropList(tmp);
1666 if (!apps)
1667 apps = WMGetFromPLDictionary(dock_state, dApplications);
1670 if (!apps)
1671 goto finish;
1673 count = WMGetPropListItemCount(apps);
1674 if (count == 0)
1675 goto finish;
1677 old_top = dock->icon_array[0];
1679 /* dock->icon_count is set to 1 when dock is created.
1680 * Since Clip is already restored, we want to keep it so for clip,
1681 * but for dock we may change the default top tile, so we set it to 0.
1683 if (type == WM_DOCK)
1684 dock->icon_count = 0;
1686 for (i = 0; i < count; i++) {
1687 if (dock->icon_count >= dock->max_icons) {
1688 wwarning(_("there are too many icons stored in dock. Ignoring what doesn't fit"));
1689 break;
1692 value = WMGetFromPLArray(apps, i);
1693 aicon = restore_icon_state(scr, value, type, dock->icon_count);
1695 dock->icon_array[dock->icon_count] = aicon;
1697 if (aicon) {
1698 aicon->dock = dock;
1699 aicon->x_pos = dock->x_pos + (aicon->xindex * ICON_SIZE);
1700 aicon->y_pos = dock->y_pos + (aicon->yindex * ICON_SIZE);
1702 if (dock->lowered)
1703 ChangeStackingLevel(aicon->icon->core, WMNormalLevel);
1704 else
1705 ChangeStackingLevel(aicon->icon->core, WMDockLevel);
1707 wCoreConfigure(aicon->icon->core, aicon->x_pos, aicon->y_pos, 0, 0);
1709 if (!dock->collapsed)
1710 XMapWindow(dpy, aicon->icon->core->window);
1711 wRaiseFrame(aicon->icon->core);
1713 dock->icon_count++;
1714 } else if (dock->icon_count == 0 && type == WM_DOCK)
1715 dock->icon_count++;
1718 /* if the first icon is not defined, use the default */
1719 if (dock->icon_array[0] == NULL) {
1720 /* update default icon */
1721 old_top->x_pos = dock->x_pos;
1722 old_top->y_pos = dock->y_pos;
1723 if (dock->lowered)
1724 ChangeStackingLevel(old_top->icon->core, WMNormalLevel);
1725 else
1726 ChangeStackingLevel(old_top->icon->core, WMDockLevel);
1727 dock->icon_array[0] = old_top;
1728 XMoveWindow(dpy, old_top->icon->core->window, dock->x_pos, dock->y_pos);
1729 /* we don't need to increment dock->icon_count here because it was
1730 * incremented in the loop above.
1732 } else if (old_top != dock->icon_array[0]) {
1733 if (old_top == scr->clip_icon)
1734 scr->clip_icon = dock->icon_array[0];
1735 wAppIconDestroy(old_top);
1738 finish:
1739 WMReleasePropList(dock_state);
1741 return dock;
1744 void wDockLaunchWithState(WDock *dock, WAppIcon *btn, WSavedState *state)
1746 if (btn && btn->command && !btn->running && !btn->launching) {
1748 btn->drop_launch = 0;
1749 btn->paste_launch = 0;
1751 btn->pid = execCommand(btn, btn->command, state);
1753 if (btn->pid > 0) {
1754 if (!btn->forced_dock && !btn->buggy_app) {
1755 btn->launching = 1;
1756 dockIconPaint(btn);
1759 } else {
1760 wfree(state);
1764 void wDockDoAutoLaunch(WDock *dock, int workspace)
1766 WAppIcon *btn;
1767 WSavedState *state;
1768 int i;
1770 for (i = 0; i < dock->max_icons; i++) {
1771 btn = dock->icon_array[i];
1772 if (!btn || !btn->auto_launch)
1773 continue;
1775 state = wmalloc(sizeof(WSavedState));
1776 state->workspace = workspace;
1777 /* TODO: this is klugy and is very difficult to understand
1778 * what's going on. Try to clean up */
1779 wDockLaunchWithState(dock, btn, state);
1783 #ifdef XDND /* was OFFIX */
1784 static WDock *findDock(WScreen *scr, XEvent *event, int *icon_pos)
1786 WDock *dock;
1787 int i;
1789 *icon_pos = -1;
1790 if ((dock = scr->dock) != NULL) {
1791 for (i = 0; i < dock->max_icons; i++) {
1792 if (dock->icon_array[i]
1793 && dock->icon_array[i]->icon->core->window == event->xclient.window) {
1794 *icon_pos = i;
1795 break;
1799 if (*icon_pos < 0 && (dock = scr->workspaces[scr->current_workspace]->clip) != NULL) {
1800 for (i = 0; i < dock->max_icons; i++) {
1801 if (dock->icon_array[i]
1802 && dock->icon_array[i]->icon->core->window == event->xclient.window) {
1803 *icon_pos = i;
1804 break;
1808 if (*icon_pos >= 0)
1809 return dock;
1810 return NULL;
1813 int wDockReceiveDNDDrop(WScreen *scr, XEvent *event)
1815 WDock *dock;
1816 WAppIcon *btn;
1817 int icon_pos;
1819 dock = findDock(scr, event, &icon_pos);
1820 if (!dock)
1821 return False;
1824 * Return True if the drop was on an application icon window.
1825 * In this case, let the ClientMessage handler redirect the
1826 * message to the app.
1828 if (dock->icon_array[icon_pos]->icon->icon_win != None)
1829 return True;
1831 if (dock->icon_array[icon_pos]->dnd_command != NULL) {
1832 scr->flags.dnd_data_convertion_status = 0;
1834 btn = dock->icon_array[icon_pos];
1836 if (!btn->forced_dock) {
1837 btn->relaunching = btn->running;
1838 btn->running = 1;
1840 if (btn->wm_instance || btn->wm_class) {
1841 WWindowAttributes attr;
1842 memset(&attr, 0, sizeof(WWindowAttributes));
1843 wDefaultFillAttributes(btn->icon->core->screen_ptr,
1844 btn->wm_instance, btn->wm_class, &attr, NULL, True);
1846 if (!attr.no_appicon)
1847 btn->launching = 1;
1848 else
1849 btn->running = 0;
1852 btn->paste_launch = 0;
1853 btn->drop_launch = 1;
1854 scr->last_dock = dock;
1855 btn->pid = execCommand(btn, btn->dnd_command, NULL);
1856 if (btn->pid > 0) {
1857 dockIconPaint(btn);
1858 } else {
1859 btn->launching = 0;
1860 if (!btn->relaunching) {
1861 btn->running = 0;
1865 return False;
1867 #endif /* XDND */
1869 Bool wDockAttachIcon(WDock *dock, WAppIcon *icon, int x, int y)
1871 WWindow *wwin;
1872 int index;
1874 wwin = icon->icon->owner;
1875 if (icon->command == NULL) {
1876 char *command;
1878 icon->editing = 0;
1880 command = GetCommandForWindow(wwin->client_win);
1881 if (command) {
1882 icon->command = command;
1883 } else {
1884 /* icon->forced_dock = 1; */
1885 if (dock->type != WM_CLIP || !icon->attracted) {
1886 icon->editing = 1;
1887 if (wInputDialog(dock->screen_ptr, _("Dock Icon"),
1888 _("Type the command used to launch the application"), &command)) {
1889 if (command && (command[0] == 0 || (command[0] == '-' && command[1] == 0))) {
1890 wfree(command);
1891 command = NULL;
1893 icon->command = command;
1894 icon->editing = 0;
1895 } else {
1896 icon->editing = 0;
1897 if (command)
1898 wfree(command);
1899 /* If the target is the dock, reject the icon. If
1900 * the target is the clip, make it an attracted icon
1902 if (dock->type == WM_CLIP) {
1903 icon->attracted = 1;
1904 if (!icon->icon->shadowed) {
1905 icon->icon->shadowed = 1;
1906 icon->icon->force_paint = 1;
1908 } else {
1909 return False;
1914 } else {
1915 icon->editing = 0;
1918 for (index = 1; index < dock->max_icons; index++)
1919 if (dock->icon_array[index] == NULL)
1920 break;
1921 /* if (index == dock->max_icons)
1922 return; */
1924 assert(index < dock->max_icons);
1926 dock->icon_array[index] = icon;
1927 icon->yindex = y;
1928 icon->xindex = x;
1930 icon->omnipresent = 0;
1932 icon->x_pos = dock->x_pos + x * ICON_SIZE;
1933 icon->y_pos = dock->y_pos + y * ICON_SIZE;
1935 dock->icon_count++;
1937 icon->running = 1;
1938 icon->launching = 0;
1939 icon->docked = 1;
1940 icon->dock = dock;
1941 icon->icon->core->descriptor.handle_mousedown = iconMouseDown;
1942 if (dock->type == WM_CLIP) {
1943 icon->icon->core->descriptor.handle_enternotify = clipEnterNotify;
1944 icon->icon->core->descriptor.handle_leavenotify = clipLeaveNotify;
1946 icon->icon->core->descriptor.parent_type = WCLASS_DOCK_ICON;
1947 icon->icon->core->descriptor.parent = icon;
1949 MoveInStackListUnder(dock->icon_array[index - 1]->icon->core, icon->icon->core);
1950 wAppIconMove(icon, icon->x_pos, icon->y_pos);
1951 wAppIconPaint(icon);
1952 save_appicon(icon, True);
1954 if (wPreferences.auto_arrange_icons)
1955 wArrangeIcons(dock->screen_ptr, True);
1957 #ifdef XDND /* was OFFIX */
1958 if (icon->command && !icon->dnd_command) {
1959 int len = strlen(icon->command) + 8;
1960 icon->dnd_command = wmalloc(len);
1961 snprintf(icon->dnd_command, len, "%s %%d", icon->command);
1963 #endif
1965 if (icon->command && !icon->paste_command) {
1966 int len = strlen(icon->command) + 8;
1967 icon->paste_command = wmalloc(len);
1968 snprintf(icon->paste_command, len, "%s %%s", icon->command);
1971 return True;
1974 static void reattachIcon(WDock *dock, WAppIcon *icon, int x, int y)
1976 int index;
1978 for (index = 1; index < dock->max_icons; index++) {
1979 if (dock->icon_array[index] == icon)
1980 break;
1982 assert(index < dock->max_icons);
1984 icon->yindex = y;
1985 icon->xindex = x;
1987 icon->x_pos = dock->x_pos + x * ICON_SIZE;
1988 icon->y_pos = dock->y_pos + y * ICON_SIZE;
1991 static Bool moveIconBetweenDocks(WDock *src, WDock *dest, WAppIcon *icon, int x, int y)
1993 WWindow *wwin;
1994 char *command;
1995 int index;
1997 if (src == dest)
1998 return True; /* No move needed, we're already there */
2000 if (dest == NULL)
2001 return False;
2003 wwin = icon->icon->owner;
2006 * For the moment we can't do this if we move icons in Clip from one
2007 * workspace to other, because if we move two or more icons without
2008 * command, the dialog box will not be able to tell us to which of the
2009 * moved icons it applies. -Dan
2011 if ((dest->type == WM_DOCK /*|| dest->keep_attracted */ ) && icon->command == NULL) {
2012 command = GetCommandForWindow(wwin->client_win);
2013 if (command) {
2014 icon->command = command;
2015 } else {
2016 icon->editing = 1;
2017 /* icon->forced_dock = 1; */
2018 if (wInputDialog(src->screen_ptr, _("Dock Icon"),
2019 _("Type the command used to launch the application"), &command)) {
2020 if (command && (command[0] == 0 || (command[0] == '-' && command[1] == 0))) {
2021 wfree(command);
2022 command = NULL;
2024 icon->command = command;
2025 } else {
2026 icon->editing = 0;
2027 if (command)
2028 wfree(command);
2029 return False;
2031 icon->editing = 0;
2035 if (dest->type == WM_DOCK)
2036 wClipMakeIconOmnipresent(icon, False);
2038 for (index = 1; index < src->max_icons; index++) {
2039 if (src->icon_array[index] == icon)
2040 break;
2042 assert(index < src->max_icons);
2044 src->icon_array[index] = NULL;
2045 src->icon_count--;
2047 for (index = 1; index < dest->max_icons; index++) {
2048 if (dest->icon_array[index] == NULL)
2049 break;
2051 /* if (index == dest->max_icons)
2052 return; */
2054 assert(index < dest->max_icons);
2056 dest->icon_array[index] = icon;
2057 icon->dock = dest;
2059 /* deselect the icon */
2060 if (icon->icon->selected)
2061 wIconSelect(icon->icon);
2063 if (dest->type == WM_DOCK) {
2064 icon->icon->core->descriptor.handle_enternotify = NULL;
2065 icon->icon->core->descriptor.handle_leavenotify = NULL;
2066 } else {
2067 icon->icon->core->descriptor.handle_enternotify = clipEnterNotify;
2068 icon->icon->core->descriptor.handle_leavenotify = clipLeaveNotify;
2071 /* set it to be kept when moving to dock.
2072 * Unless the icon does not have a command set
2074 if (icon->command && dest->type == WM_DOCK) {
2075 icon->attracted = 0;
2076 if (icon->icon->shadowed) {
2077 icon->icon->shadowed = 0;
2078 icon->icon->force_paint = 1;
2080 save_appicon(icon, True);
2083 if (src->auto_collapse || src->auto_raise_lower)
2084 clipLeave(src);
2086 icon->yindex = y;
2087 icon->xindex = x;
2089 icon->x_pos = dest->x_pos + x * ICON_SIZE;
2090 icon->y_pos = dest->y_pos + y * ICON_SIZE;
2092 dest->icon_count++;
2094 MoveInStackListUnder(dest->icon_array[index - 1]->icon->core, icon->icon->core);
2095 wAppIconPaint(icon);
2097 return True;
2100 void wDockDetach(WDock *dock, WAppIcon *icon)
2102 int index;
2104 /* make the settings panel be closed */
2105 if (icon->panel) {
2106 DestroyDockAppSettingsPanel(icon->panel);
2109 /* This must be called before icon->dock is set to NULL.
2110 * Don't move it. -Dan
2112 wClipMakeIconOmnipresent(icon, False);
2114 icon->docked = 0;
2115 icon->dock = NULL;
2116 icon->attracted = 0;
2117 icon->auto_launch = 0;
2118 if (icon->icon->shadowed) {
2119 icon->icon->shadowed = 0;
2120 icon->icon->force_paint = 1;
2123 /* deselect the icon */
2124 if (icon->icon->selected)
2125 wIconSelect(icon->icon);
2127 if (icon->command) {
2128 wfree(icon->command);
2129 icon->command = NULL;
2131 #ifdef XDND /* was OFFIX */
2132 if (icon->dnd_command) {
2133 wfree(icon->dnd_command);
2134 icon->dnd_command = NULL;
2136 #endif
2137 if (icon->paste_command) {
2138 wfree(icon->paste_command);
2139 icon->paste_command = NULL;
2142 for (index = 1; index < dock->max_icons; index++)
2143 if (dock->icon_array[index] == icon)
2144 break;
2145 assert(index < dock->max_icons);
2146 dock->icon_array[index] = NULL;
2147 icon->yindex = -1;
2148 icon->xindex = -1;
2150 dock->icon_count--;
2152 /* if the dock is not attached to an application or
2153 * the application did not set the appropriate hints yet,
2154 * destroy the icon */
2155 if (!icon->running || !wApplicationOf(icon->main_window))
2156 wAppIconDestroy(icon);
2157 else {
2158 icon->icon->core->descriptor.handle_mousedown = appIconMouseDown;
2159 icon->icon->core->descriptor.handle_enternotify = NULL;
2160 icon->icon->core->descriptor.handle_leavenotify = NULL;
2161 icon->icon->core->descriptor.parent_type = WCLASS_APPICON;
2162 icon->icon->core->descriptor.parent = icon;
2164 ChangeStackingLevel(icon->icon->core, NORMAL_ICON_LEVEL);
2166 wAppIconPaint(icon);
2167 if (wPreferences.auto_arrange_icons) {
2168 wArrangeIcons(dock->screen_ptr, True);
2171 if (dock->auto_collapse || dock->auto_raise_lower)
2172 clipLeave(dock);
2176 * returns the closest Dock slot index for the passed
2177 * coordinates.
2179 * Returns False if icon can't be docked.
2181 * Note: this function should NEVER alter ret_x or ret_y, unless it will
2182 * return True. -Dan
2184 Bool wDockSnapIcon(WDock *dock, WAppIcon *icon, int req_x, int req_y, int *ret_x, int *ret_y, int redocking)
2186 WScreen *scr = dock->screen_ptr;
2187 int dx, dy;
2188 int ex_x, ex_y;
2189 int i, offset = ICON_SIZE / 2;
2190 WAppIcon *aicon = NULL;
2191 WAppIcon *nicon = NULL;
2192 int max_y_icons;
2194 /* TODO: XINERAMA, for these */
2195 max_y_icons = scr->scr_height / ICON_SIZE - 1;
2197 if (wPreferences.flags.noupdates)
2198 return False;
2200 dx = dock->x_pos;
2201 dy = dock->y_pos;
2203 /* if the dock is full */
2204 if (!redocking && (dock->icon_count >= dock->max_icons)) {
2205 return False;
2208 /* exact position */
2209 if (req_y < dy)
2210 ex_y = (req_y - offset - dy) / ICON_SIZE;
2211 else
2212 ex_y = (req_y + offset - dy) / ICON_SIZE;
2214 if (req_x < dx)
2215 ex_x = (req_x - offset - dx) / ICON_SIZE;
2216 else
2217 ex_x = (req_x + offset - dx) / ICON_SIZE;
2219 /* check if the icon is outside the screen boundaries */
2221 WMRect rect;
2222 int flags;
2224 rect.pos.x = dx + ex_x * ICON_SIZE;
2225 rect.pos.y = dy + ex_y * ICON_SIZE;
2226 rect.size.width = rect.size.height = ICON_SIZE;
2228 wGetRectPlacementInfo(scr, rect, &flags);
2229 if (flags & (XFLAG_DEAD | XFLAG_PARTIAL))
2230 return False;
2233 if (dock->type == WM_DOCK) {
2234 if (icon->dock != dock && ex_x != 0)
2235 return False;
2237 aicon = NULL;
2238 for (i = 0; i < dock->max_icons; i++) {
2239 nicon = dock->icon_array[i];
2240 if (nicon && nicon->yindex == ex_y) {
2241 aicon = nicon;
2242 break;
2246 if (redocking) {
2247 int sig, done, closest;
2249 /* Possible cases when redocking:
2251 * icon dragged out of range of any slot -> false
2252 * icon dragged to range of free slot
2253 * icon dragged to range of same slot
2254 * icon dragged to range of different icon
2256 if (abs(ex_x) > DOCK_DETTACH_THRESHOLD)
2257 return False;
2259 if (ex_y >= 0 && ex_y <= max_y_icons && (aicon == icon || !aicon)) {
2260 *ret_x = 0;
2261 *ret_y = ex_y;
2262 return True;
2265 /* start looking at the upper slot or lower? */
2266 if (ex_y * ICON_SIZE < (req_y + offset - dy))
2267 sig = 1;
2268 else
2269 sig = -1;
2271 closest = -1;
2272 done = 0;
2273 /* look for closest free slot */
2274 for (i = 0; i < (DOCK_DETTACH_THRESHOLD + 1) * 2 && !done; i++) {
2275 int j;
2277 done = 1;
2278 closest = sig * (i / 2) + ex_y;
2279 /* check if this slot is used */
2280 if (closest >= 0) {
2281 for (j = 0; j < dock->max_icons; j++) {
2282 if (dock->icon_array[j]
2283 && dock->icon_array[j]->yindex == closest) {
2284 /* slot is used by someone else */
2285 if (dock->icon_array[j] != icon)
2286 done = 0;
2287 break;
2291 sig = -sig;
2293 if (done && closest >= 0 && closest <= max_y_icons &&
2294 ((ex_y >= closest && ex_y - closest < DOCK_DETTACH_THRESHOLD + 1)
2295 || (ex_y < closest && closest - ex_y <= DOCK_DETTACH_THRESHOLD + 1))) {
2296 *ret_x = 0;
2297 *ret_y = closest;
2298 return True;
2300 } else { /* !redocking */
2302 /* if slot is free and the icon is close enough, return it */
2303 if (!aicon && ex_x == 0 && ex_y >= 0 && ex_y <= max_y_icons) {
2304 *ret_x = 0;
2305 *ret_y = ex_y;
2306 return True;
2309 } else { /* CLIP */
2310 int neighbours = 0;
2311 int start, stop, k;
2313 start = icon->omnipresent ? 0 : scr->current_workspace;
2314 stop = icon->omnipresent ? scr->workspace_count : start + 1;
2316 aicon = NULL;
2317 for (k = start; k < stop; k++) {
2318 WDock *tmp = scr->workspaces[k]->clip;
2319 if (!tmp)
2320 continue;
2321 for (i = 0; i < tmp->max_icons; i++) {
2322 nicon = tmp->icon_array[i];
2323 if (nicon && nicon->xindex == ex_x && nicon->yindex == ex_y) {
2324 aicon = nicon;
2325 break;
2328 if (aicon)
2329 break;
2331 for (k = start; k < stop; k++) {
2332 WDock *tmp = scr->workspaces[k]->clip;
2333 if (!tmp)
2334 continue;
2335 for (i = 0; i < tmp->max_icons; i++) {
2336 nicon = tmp->icon_array[i];
2337 if (nicon && nicon != icon && /* Icon can't be it's own neighbour */
2338 (abs(nicon->xindex - ex_x) <= CLIP_ATTACH_VICINITY &&
2339 abs(nicon->yindex - ex_y) <= CLIP_ATTACH_VICINITY)) {
2340 neighbours = 1;
2341 break;
2344 if (neighbours)
2345 break;
2348 if (neighbours && (aicon == NULL || (redocking && aicon == icon))) {
2349 *ret_x = ex_x;
2350 *ret_y = ex_y;
2351 return True;
2354 return False;
2357 static int onScreen(WScreen *scr, int x, int y, int sx, int ex, int sy, int ey)
2359 WMRect rect = wmkrect(x, y, ICON_SIZE, ICON_SIZE);
2360 int flags;
2362 wGetRectPlacementInfo(scr, rect, &flags);
2364 return !(flags & (XFLAG_DEAD | XFLAG_PARTIAL));
2368 * returns true if it can find a free slot in the dock,
2369 * in which case it changes x_pos and y_pos accordingly.
2370 * Else returns false.
2372 Bool wDockFindFreeSlot(WDock *dock, int *x_pos, int *y_pos)
2374 WScreen *scr = dock->screen_ptr;
2375 WAppIcon *btn;
2376 WAppIconChain *chain;
2377 unsigned char *slot_map;
2378 int mwidth;
2379 int r;
2380 int x, y;
2381 int i, done = False;
2382 int corner;
2383 int sx = 0, sy = 0, ex = scr->scr_width, ey = scr->scr_height;
2384 int extra_count = 0;
2386 if (dock->type == WM_CLIP && dock != scr->workspaces[scr->current_workspace]->clip)
2387 extra_count = scr->global_icon_count;
2389 /* if the dock is full */
2390 if (dock->icon_count + extra_count >= dock->max_icons) {
2391 return False;
2394 if (!wPreferences.flags.nodock && scr->dock) {
2395 if (scr->dock->on_right_side)
2396 ex -= ICON_SIZE + DOCK_EXTRA_SPACE;
2397 else
2398 sx += ICON_SIZE + DOCK_EXTRA_SPACE;
2401 if (ex < dock->x_pos)
2402 ex = dock->x_pos;
2403 if (sx > dock->x_pos + ICON_SIZE)
2404 sx = dock->x_pos + ICON_SIZE;
2405 #define C_NONE 0
2406 #define C_NW 1
2407 #define C_NE 2
2408 #define C_SW 3
2409 #define C_SE 4
2411 /* check if clip is in a corner */
2412 if (dock->type == WM_CLIP) {
2413 if (dock->x_pos < 1 && dock->y_pos < 1)
2414 corner = C_NE;
2415 else if (dock->x_pos < 1 && dock->y_pos >= (ey - ICON_SIZE))
2416 corner = C_SE;
2417 else if (dock->x_pos >= (ex - ICON_SIZE) && dock->y_pos >= (ey - ICON_SIZE))
2418 corner = C_SW;
2419 else if (dock->x_pos >= (ex - ICON_SIZE) && dock->y_pos < 1)
2420 corner = C_NW;
2421 else
2422 corner = C_NONE;
2423 } else
2424 corner = C_NONE;
2426 /* If the clip is in the corner, use only slots that are in the border
2427 * of the screen */
2428 if (corner != C_NONE) {
2429 char *hmap, *vmap;
2430 int hcount, vcount;
2432 hcount = WMIN(dock->max_icons, scr->scr_width / ICON_SIZE);
2433 vcount = WMIN(dock->max_icons, scr->scr_height / ICON_SIZE);
2434 hmap = wmalloc(hcount + 1);
2435 vmap = wmalloc(vcount + 1);
2437 /* mark used positions */
2438 switch (corner) {
2439 case C_NE:
2440 for (i = 0; i < dock->max_icons; i++) {
2441 btn = dock->icon_array[i];
2442 if (!btn)
2443 continue;
2445 if (btn->xindex == 0 && btn->yindex > 0 && btn->yindex < vcount)
2446 vmap[btn->yindex] = 1;
2447 else if (btn->yindex == 0 && btn->xindex > 0 && btn->xindex < hcount)
2448 hmap[btn->xindex] = 1;
2450 for (chain = scr->global_icons; chain != NULL; chain = chain->next) {
2451 btn = chain->aicon;
2452 if (btn->xindex == 0 && btn->yindex > 0 && btn->yindex < vcount)
2453 vmap[btn->yindex] = 1;
2454 else if (btn->yindex == 0 && btn->xindex > 0 && btn->xindex < hcount)
2455 hmap[btn->xindex] = 1;
2457 break;
2458 case C_NW:
2459 for (i = 0; i < dock->max_icons; i++) {
2460 btn = dock->icon_array[i];
2461 if (!btn)
2462 continue;
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 for (chain = scr->global_icons; chain != NULL; chain = chain->next) {
2470 btn = chain->aicon;
2471 if (btn->xindex == 0 && btn->yindex > 0 && btn->yindex < vcount)
2472 vmap[btn->yindex] = 1;
2473 else if (btn->yindex == 0 && btn->xindex < 0 && btn->xindex > -hcount)
2474 hmap[-btn->xindex] = 1;
2476 break;
2477 case C_SE:
2478 for (i = 0; i < dock->max_icons; i++) {
2479 btn = dock->icon_array[i];
2480 if (!btn)
2481 continue;
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 for (chain = scr->global_icons; chain != NULL; chain = chain->next) {
2489 btn = chain->aicon;
2490 if (btn->xindex == 0 && btn->yindex < 0 && btn->yindex > -vcount)
2491 vmap[-btn->yindex] = 1;
2492 else if (btn->yindex == 0 && btn->xindex > 0 && btn->xindex < hcount)
2493 hmap[btn->xindex] = 1;
2495 break;
2496 case C_SW:
2497 default:
2498 for (i = 0; i < dock->max_icons; i++) {
2499 btn = dock->icon_array[i];
2500 if (!btn)
2501 continue;
2503 if (btn->xindex == 0 && btn->yindex < 0 && btn->yindex > -vcount)
2504 vmap[-btn->yindex] = 1;
2505 else if (btn->yindex == 0 && btn->xindex < 0 && btn->xindex > -hcount)
2506 hmap[-btn->xindex] = 1;
2508 for (chain = scr->global_icons; chain != NULL; chain = chain->next) {
2509 btn = chain->aicon;
2510 if (btn->xindex == 0 && btn->yindex < 0 && btn->yindex > -vcount)
2511 vmap[-btn->yindex] = 1;
2512 else if (btn->yindex == 0 && btn->xindex < 0 && btn->xindex > -hcount)
2513 hmap[-btn->xindex] = 1;
2516 x = 0;
2517 y = 0;
2518 done = 0;
2519 /* search a vacant slot */
2520 for (i = 1; i < WMAX(vcount, hcount); i++) {
2521 if (i < vcount && vmap[i] == 0) {
2522 /* found a slot */
2523 x = 0;
2524 y = i;
2525 done = 1;
2526 break;
2527 } else if (i < hcount && hmap[i] == 0) {
2528 /* found a slot */
2529 x = i;
2530 y = 0;
2531 done = 1;
2532 break;
2535 wfree(vmap);
2536 wfree(hmap);
2537 /* If found a slot, translate and return */
2538 if (done) {
2539 if (corner == C_NW || corner == C_NE) {
2540 *y_pos = y;
2541 } else {
2542 *y_pos = -y;
2544 if (corner == C_NE || corner == C_SE) {
2545 *x_pos = x;
2546 } else {
2547 *x_pos = -x;
2549 return True;
2551 /* else, try to find a slot somewhere else */
2554 /* a map of mwidth x mwidth would be enough if we allowed icons to be
2555 * placed outside of screen */
2556 mwidth = (int)ceil(sqrt(dock->max_icons));
2558 /* In the worst case (the clip is in the corner of the screen),
2559 * the amount of icons that fit in the clip is smaller.
2560 * Double the map to get a safe value.
2562 mwidth += mwidth;
2564 r = (mwidth - 1) / 2;
2566 slot_map = wmalloc(mwidth * mwidth);
2568 #define XY2OFS(x,y) (WMAX(abs(x),abs(y)) > r) ? 0 : (((y)+r)*(mwidth)+(x)+r)
2570 /* mark used slots in the map. If the slot falls outside the map
2571 * (for example, when all icons are placed in line), ignore them. */
2572 for (i = 0; i < dock->max_icons; i++) {
2573 btn = dock->icon_array[i];
2574 if (btn)
2575 slot_map[XY2OFS(btn->xindex, btn->yindex)] = 1;
2577 for (chain = scr->global_icons; chain != NULL; chain = chain->next) {
2578 slot_map[XY2OFS(chain->aicon->xindex, chain->aicon->yindex)] = 1;
2580 /* Find closest slot from the center that is free by scanning the
2581 * map from the center to outward in circular passes.
2582 * This will not result in a neat layout, but will be optimal
2583 * in the sense that there will not be holes left.
2585 done = 0;
2586 for (i = 1; i <= r && !done; i++) {
2587 int tx, ty;
2589 /* top and bottom parts of the ring */
2590 for (x = -i; x <= i && !done; x++) {
2591 tx = dock->x_pos + x * ICON_SIZE;
2592 y = -i;
2593 ty = dock->y_pos + y * ICON_SIZE;
2594 if (slot_map[XY2OFS(x, y)] == 0 && onScreen(scr, tx, ty, sx, ex, sy, ey)) {
2595 *x_pos = x;
2596 *y_pos = y;
2597 done = 1;
2598 break;
2600 y = i;
2601 ty = dock->y_pos + y * ICON_SIZE;
2602 if (slot_map[XY2OFS(x, y)] == 0 && onScreen(scr, tx, ty, sx, ex, sy, ey)) {
2603 *x_pos = x;
2604 *y_pos = y;
2605 done = 1;
2606 break;
2609 /* left and right parts of the ring */
2610 for (y = -i + 1; y <= i - 1; y++) {
2611 ty = dock->y_pos + y * ICON_SIZE;
2612 x = -i;
2613 tx = dock->x_pos + x * ICON_SIZE;
2614 if (slot_map[XY2OFS(x, y)] == 0 && onScreen(scr, tx, ty, sx, ex, sy, ey)) {
2615 *x_pos = x;
2616 *y_pos = y;
2617 done = 1;
2618 break;
2620 x = i;
2621 tx = dock->x_pos + x * ICON_SIZE;
2622 if (slot_map[XY2OFS(x, y)] == 0 && onScreen(scr, tx, ty, sx, ex, sy, ey)) {
2623 *x_pos = x;
2624 *y_pos = y;
2625 done = 1;
2626 break;
2630 wfree(slot_map);
2631 #undef XY2OFS
2632 return done;
2635 static void moveDock(WDock *dock, int new_x, int new_y)
2637 WAppIcon *btn;
2638 int i;
2640 dock->x_pos = new_x;
2641 dock->y_pos = new_y;
2642 for (i = 0; i < dock->max_icons; i++) {
2643 btn = dock->icon_array[i];
2644 if (btn) {
2645 btn->x_pos = new_x + btn->xindex * ICON_SIZE;
2646 btn->y_pos = new_y + btn->yindex * ICON_SIZE;
2647 XMoveWindow(dpy, btn->icon->core->window, btn->x_pos, btn->y_pos);
2652 static void swapDock(WDock *dock)
2654 WScreen *scr = dock->screen_ptr;
2655 WAppIcon *btn;
2656 int x, i;
2658 if (dock->on_right_side) {
2659 x = dock->x_pos = scr->scr_width - ICON_SIZE - DOCK_EXTRA_SPACE;
2660 } else {
2661 x = dock->x_pos = DOCK_EXTRA_SPACE;
2664 for (i = 0; i < dock->max_icons; i++) {
2665 btn = dock->icon_array[i];
2666 if (btn) {
2667 btn->x_pos = x;
2668 XMoveWindow(dpy, btn->icon->core->window, btn->x_pos, btn->y_pos);
2672 wScreenUpdateUsableArea(scr);
2675 static pid_t execCommand(WAppIcon *btn, char *command, WSavedState *state)
2677 WScreen *scr = btn->icon->core->screen_ptr;
2678 pid_t pid;
2679 char **argv;
2680 int argc;
2681 char *cmdline;
2683 cmdline = ExpandOptions(scr, command);
2685 if (scr->flags.dnd_data_convertion_status || !cmdline) {
2686 if (cmdline)
2687 wfree(cmdline);
2688 if (state)
2689 wfree(state);
2690 return 0;
2693 wtokensplit(cmdline, &argv, &argc);
2695 if (!argc) {
2696 if (cmdline)
2697 wfree(cmdline);
2698 if (state)
2699 wfree(state);
2700 return 0;
2703 if ((pid = fork()) == 0) {
2704 char **args;
2705 int i;
2707 SetupEnvironment(scr);
2709 #ifdef HAVE_SETSID
2710 setsid();
2711 #endif
2713 args = malloc(sizeof(char *) * (argc + 1));
2714 if (!args)
2715 exit(111);
2716 for (i = 0; i < argc; i++) {
2717 args[i] = argv[i];
2719 args[argc] = NULL;
2720 execvp(argv[0], args);
2721 exit(111);
2723 wtokenfree(argv, argc);
2725 if (pid > 0) {
2726 if (!state) {
2727 state = wmalloc(sizeof(WSavedState));
2728 state->hidden = -1;
2729 state->miniaturized = -1;
2730 state->shaded = -1;
2731 if (btn->dock == scr->dock || btn->omnipresent)
2732 state->workspace = -1;
2733 else
2734 state->workspace = scr->current_workspace;
2736 wWindowAddSavedState(btn->wm_instance, btn->wm_class, cmdline, pid, state);
2737 wAddDeathHandler(pid, (WDeathHandler *) trackDeadProcess, btn->dock);
2738 } else if (state) {
2739 wfree(state);
2741 wfree(cmdline);
2742 return pid;
2745 void wDockHideIcons(WDock *dock)
2747 int i;
2749 if (dock == NULL)
2750 return;
2752 for (i = 1; i < dock->max_icons; i++) {
2753 if (dock->icon_array[i])
2754 XUnmapWindow(dpy, dock->icon_array[i]->icon->core->window);
2756 dock->mapped = 0;
2758 dockIconPaint(dock->icon_array[0]);
2761 void wDockShowIcons(WDock *dock)
2763 int i, newlevel;
2764 WAppIcon *btn;
2766 if (dock == NULL)
2767 return;
2769 btn = dock->icon_array[0];
2770 moveDock(dock, btn->x_pos, btn->y_pos);
2772 newlevel = dock->lowered ? WMNormalLevel : WMDockLevel;
2773 ChangeStackingLevel(btn->icon->core, newlevel);
2775 for (i = 1; i < dock->max_icons; i++) {
2776 if (dock->icon_array[i]) {
2777 MoveInStackListAbove(dock->icon_array[i]->icon->core, btn->icon->core);
2778 break;
2782 if (!dock->collapsed) {
2783 for (i = 1; i < dock->max_icons; i++) {
2784 if (dock->icon_array[i]) {
2785 XMapWindow(dpy, dock->icon_array[i]->icon->core->window);
2789 dock->mapped = 1;
2791 dockIconPaint(btn);
2794 void wDockLower(WDock *dock)
2796 int i;
2798 for (i = 0; i < dock->max_icons; i++) {
2799 if (dock->icon_array[i])
2800 wLowerFrame(dock->icon_array[i]->icon->core);
2804 void wDockRaise(WDock *dock)
2806 int i;
2808 for (i = dock->max_icons - 1; i >= 0; i--) {
2809 if (dock->icon_array[i])
2810 wRaiseFrame(dock->icon_array[i]->icon->core);
2814 void wDockRaiseLower(WDock *dock)
2816 if (!dock->icon_array[0]->icon->core->stacking->above
2817 || (dock->icon_array[0]->icon->core->stacking->window_level
2818 != dock->icon_array[0]->icon->core->stacking->above->stacking->window_level))
2819 wDockLower(dock);
2820 else
2821 wDockRaise(dock);
2824 void wDockFinishLaunch(WDock *dock, WAppIcon *icon)
2826 icon->launching = 0;
2827 icon->relaunching = 0;
2828 dockIconPaint(icon);
2831 WAppIcon *wDockFindIconForWindow(WDock *dock, Window window)
2833 WAppIcon *icon;
2834 int i;
2836 for (i = 0; i < dock->max_icons; i++) {
2837 icon = dock->icon_array[i];
2838 if (icon && icon->main_window == window)
2839 return icon;
2841 return NULL;
2844 void wDockTrackWindowLaunch(WDock *dock, Window window)
2846 WAppIcon *icon;
2847 char *wm_class, *wm_instance;
2848 int i;
2849 Bool firstPass = True;
2850 Bool found = False;
2851 char *command = NULL;
2853 if (!PropGetWMClass(window, &wm_class, &wm_instance) || (!wm_class && !wm_instance)) {
2854 return;
2857 command = GetCommandForWindow(window);
2858 retry:
2859 for (i = 0; i < dock->max_icons; i++) {
2860 icon = dock->icon_array[i];
2861 if (!icon)
2862 continue;
2864 /* app is already attached to icon */
2865 if (icon->main_window == window) {
2866 found = True;
2867 break;
2870 if ((icon->wm_instance || icon->wm_class)
2871 && (icon->launching || !icon->running)) {
2873 if (icon->wm_instance && wm_instance && strcmp(icon->wm_instance, wm_instance) != 0) {
2874 continue;
2876 if (icon->wm_class && wm_class && strcmp(icon->wm_class, wm_class) != 0) {
2877 continue;
2879 if (firstPass && command && strcmp(icon->command, command) != 0) {
2880 continue;
2883 if (!icon->relaunching) {
2884 WApplication *wapp;
2886 /* Possibly an application that was docked with dockit,
2887 * but the user did not update WMState to indicate that
2888 * it was docked by force */
2889 wapp = wApplicationOf(window);
2890 if (!wapp) {
2891 icon->forced_dock = 1;
2892 icon->running = 0;
2894 if (!icon->forced_dock) {
2895 icon->main_window = window;
2898 found = True;
2899 if (!wPreferences.no_animations && !icon->launching &&
2900 !dock->screen_ptr->flags.startup && !dock->collapsed) {
2901 WAppIcon *aicon;
2902 int x0, y0;
2904 icon->launching = 1;
2905 dockIconPaint(icon);
2907 aicon = wAppIconCreateForDock(dock->screen_ptr, NULL,
2908 wm_instance, wm_class, TILE_NORMAL);
2909 /* XXX: can: aicon->icon == NULL ? */
2910 PlaceIcon(dock->screen_ptr, &x0, &y0, wGetHeadForWindow(aicon->icon->owner));
2911 wAppIconMove(aicon, x0, y0);
2912 /* Should this always be lowered? -Dan */
2913 if (dock->lowered)
2914 wLowerFrame(aicon->icon->core);
2915 XMapWindow(dpy, aicon->icon->core->window);
2916 aicon->launching = 1;
2917 wAppIconPaint(aicon);
2918 SlideWindow(aicon->icon->core->window, x0, y0, icon->x_pos, icon->y_pos);
2919 XUnmapWindow(dpy, aicon->icon->core->window);
2920 wAppIconDestroy(aicon);
2922 wDockFinishLaunch(dock, icon);
2923 break;
2927 if (firstPass && !found) {
2928 firstPass = False;
2929 goto retry;
2932 if (command)
2933 wfree(command);
2935 if (wm_class)
2936 free(wm_class);
2937 if (wm_instance)
2938 free(wm_instance);
2941 void wClipUpdateForWorkspaceChange(WScreen *scr, int workspace)
2943 if (!wPreferences.flags.noclip) {
2944 scr->clip_icon->dock = scr->workspaces[workspace]->clip;
2945 if (scr->current_workspace != workspace) {
2946 WDock *old_clip = scr->workspaces[scr->current_workspace]->clip;
2947 WAppIconChain *chain = scr->global_icons;
2949 while (chain) {
2950 moveIconBetweenDocks(chain->aicon->dock,
2951 scr->workspaces[workspace]->clip,
2952 chain->aicon, chain->aicon->xindex, chain->aicon->yindex);
2953 if (scr->workspaces[workspace]->clip->collapsed)
2954 XUnmapWindow(dpy, chain->aicon->icon->core->window);
2955 chain = chain->next;
2958 wDockHideIcons(old_clip);
2959 if (old_clip->auto_raise_lower) {
2960 if (old_clip->auto_raise_magic) {
2961 WMDeleteTimerHandler(old_clip->auto_raise_magic);
2962 old_clip->auto_raise_magic = NULL;
2964 wDockLower(old_clip);
2966 if (old_clip->auto_collapse) {
2967 if (old_clip->auto_expand_magic) {
2968 WMDeleteTimerHandler(old_clip->auto_expand_magic);
2969 old_clip->auto_expand_magic = NULL;
2971 old_clip->collapsed = 1;
2973 wDockShowIcons(scr->workspaces[workspace]->clip);
2978 static void trackDeadProcess(pid_t pid, unsigned char status, WDock *dock)
2980 WAppIcon *icon;
2981 int i;
2983 for (i = 0; i < dock->max_icons; i++) {
2984 icon = dock->icon_array[i];
2985 if (!icon)
2986 continue;
2988 if (icon->launching && icon->pid == pid) {
2989 if (!icon->relaunching) {
2990 icon->running = 0;
2991 icon->main_window = None;
2993 wDockFinishLaunch(dock, icon);
2994 icon->pid = 0;
2995 if (status == 111) {
2996 char msg[PATH_MAX];
2997 char *cmd;
2999 #ifdef XDND
3000 if (icon->drop_launch)
3001 cmd = icon->dnd_command;
3002 else
3003 #endif
3004 if (icon->paste_launch)
3005 cmd = icon->paste_command;
3006 else
3007 cmd = icon->command;
3009 snprintf(msg, sizeof(msg), _("Could not execute command \"%s\""), cmd);
3011 wMessageDialog(dock->screen_ptr, _("Error"), msg, _("OK"), NULL, NULL);
3013 break;
3018 static void toggleLowered(WDock *dock)
3020 WAppIcon *tmp;
3021 int newlevel, i;
3023 /* lower/raise Dock */
3024 if (!dock->lowered) {
3025 newlevel = WMNormalLevel;
3026 dock->lowered = 1;
3027 } else {
3028 newlevel = WMDockLevel;
3029 dock->lowered = 0;
3032 for (i = 0; i < dock->max_icons; i++) {
3033 tmp = dock->icon_array[i];
3034 if (!tmp)
3035 continue;
3037 ChangeStackingLevel(tmp->icon->core, newlevel);
3038 if (dock->lowered)
3039 wLowerFrame(tmp->icon->core);
3042 if (dock->type == WM_DOCK)
3043 wScreenUpdateUsableArea(dock->screen_ptr);
3046 static void toggleCollapsed(WDock *dock)
3048 if (dock->collapsed) {
3049 dock->collapsed = 0;
3050 wDockShowIcons(dock);
3051 } else {
3052 dock->collapsed = 1;
3053 wDockHideIcons(dock);
3057 static void openDockMenu(WDock *dock, WAppIcon *aicon, XEvent *event)
3059 WScreen *scr = dock->screen_ptr;
3060 WObjDescriptor *desc;
3061 WMenuEntry *entry;
3062 WApplication *wapp = NULL;
3063 int index = 0;
3064 int x_pos;
3065 int n_selected;
3066 int appIsRunning = aicon->running && aicon->icon && aicon->icon->owner;
3068 if (dock->type == WM_DOCK) {
3069 /* keep on top */
3070 entry = dock->menu->entries[index];
3071 entry->flags.indicator_on = !dock->lowered;
3072 entry->clientdata = dock;
3073 dock->menu->flags.realized = 0;
3074 } else {
3075 /* clip options */
3076 if (scr->clip_options)
3077 updateClipOptionsMenu(scr->clip_options, dock);
3079 n_selected = numberOfSelectedIcons(dock);
3081 /* Rename Workspace */
3082 entry = dock->menu->entries[++index];
3083 if (aicon == scr->clip_icon) {
3084 entry->callback = renameCallback;
3085 entry->clientdata = dock;
3086 entry->flags.indicator = 0;
3087 entry->text = _("Rename Workspace");
3088 } else {
3089 entry->callback = omnipresentCallback;
3090 entry->clientdata = aicon;
3091 if (n_selected > 0) {
3092 entry->flags.indicator = 0;
3093 entry->text = _("Toggle Omnipresent");
3094 } else {
3095 entry->flags.indicator = 1;
3096 entry->flags.indicator_on = aicon->omnipresent;
3097 entry->flags.indicator_type = MI_CHECK;
3098 entry->text = _("Omnipresent");
3102 /* select/unselect icon */
3103 entry = dock->menu->entries[++index];
3104 entry->clientdata = aicon;
3105 entry->flags.indicator_on = aicon->icon->selected;
3106 wMenuSetEnabled(dock->menu, index, aicon != scr->clip_icon);
3108 /* select/unselect all icons */
3109 entry = dock->menu->entries[++index];
3110 entry->clientdata = aicon;
3111 if (n_selected > 0)
3112 entry->text = _("Unselect All Icons");
3113 else
3114 entry->text = _("Select All Icons");
3115 wMenuSetEnabled(dock->menu, index, dock->icon_count > 1);
3117 /* keep icon(s) */
3118 entry = dock->menu->entries[++index];
3119 entry->clientdata = aicon;
3120 if (n_selected > 1)
3121 entry->text = _("Keep Icons");
3122 else
3123 entry->text = _("Keep Icon");
3124 wMenuSetEnabled(dock->menu, index, dock->icon_count > 1);
3126 /* this is the workspace submenu part */
3127 entry = dock->menu->entries[++index];
3128 if (n_selected > 1)
3129 entry->text = _("Move Icons To");
3130 else
3131 entry->text = _("Move Icon To");
3132 if (scr->clip_submenu)
3133 updateWorkspaceMenu(scr->clip_submenu, aicon);
3134 wMenuSetEnabled(dock->menu, index, !aicon->omnipresent);
3136 /* remove icon(s) */
3137 entry = dock->menu->entries[++index];
3138 entry->clientdata = aicon;
3139 if (n_selected > 1)
3140 entry->text = _("Remove Icons");
3141 else
3142 entry->text = _("Remove Icon");
3143 wMenuSetEnabled(dock->menu, index, dock->icon_count > 1);
3145 /* attract icon(s) */
3146 entry = dock->menu->entries[++index];
3147 entry->clientdata = aicon;
3149 dock->menu->flags.realized = 0;
3150 wMenuRealize(dock->menu);
3153 if (aicon->icon->owner) {
3154 wapp = wApplicationOf(aicon->icon->owner->main_window);
3155 } else {
3156 wapp = NULL;
3159 /* launch */
3160 entry = dock->menu->entries[++index];
3161 entry->clientdata = aicon;
3162 wMenuSetEnabled(dock->menu, index, aicon->command != NULL);
3164 /* unhide here */
3165 entry = dock->menu->entries[++index];
3166 entry->clientdata = aicon;
3167 if (wapp && wapp->flags.hidden) {
3168 entry->text = _("Unhide Here");
3169 } else {
3170 entry->text = _("Bring Here");
3172 wMenuSetEnabled(dock->menu, index, appIsRunning);
3174 /* hide */
3175 entry = dock->menu->entries[++index];
3176 entry->clientdata = aicon;
3177 if (wapp && wapp->flags.hidden) {
3178 entry->text = _("Unhide");
3179 } else {
3180 entry->text = _("Hide");
3182 wMenuSetEnabled(dock->menu, index, appIsRunning);
3184 /* settings */
3185 entry = dock->menu->entries[++index];
3186 entry->clientdata = aicon;
3187 wMenuSetEnabled(dock->menu, index, !aicon->editing && !wPreferences.flags.noupdates);
3189 /* kill */
3190 entry = dock->menu->entries[++index];
3191 entry->clientdata = aicon;
3192 wMenuSetEnabled(dock->menu, index, appIsRunning);
3194 if (!dock->menu->flags.realized)
3195 wMenuRealize(dock->menu);
3197 if (dock->type == WM_CLIP) {
3198 /*x_pos = event->xbutton.x_root+2; */
3199 x_pos = event->xbutton.x_root - dock->menu->frame->core->width / 2 - 1;
3200 if (x_pos < 0) {
3201 x_pos = 0;
3202 } else if (x_pos + dock->menu->frame->core->width > scr->scr_width - 2) {
3203 x_pos = scr->scr_width - dock->menu->frame->core->width - 4;
3205 } else {
3206 x_pos = dock->on_right_side ? scr->scr_width - dock->menu->frame->core->width - 3 : 0;
3209 wMenuMapAt(dock->menu, x_pos, event->xbutton.y_root + 2, False);
3211 /* allow drag select */
3212 event->xany.send_event = True;
3213 desc = &dock->menu->menu->descriptor;
3214 (*desc->handle_mousedown) (desc, event);
3217 /******************************************************************/
3218 static void iconDblClick(WObjDescriptor *desc, XEvent *event)
3220 WAppIcon *btn = desc->parent;
3221 WDock *dock = btn->dock;
3222 WApplication *wapp = NULL;
3223 int unhideHere = 0;
3225 if (btn->icon->owner && !(event->xbutton.state & ControlMask)) {
3226 wapp = wApplicationOf(btn->icon->owner->main_window);
3228 assert(wapp != NULL);
3230 unhideHere = (event->xbutton.state & ShiftMask);
3232 /* go to the last workspace that the user worked on the app */
3233 if (wapp->last_workspace != dock->screen_ptr->current_workspace && !unhideHere) {
3234 wWorkspaceChange(dock->screen_ptr, wapp->last_workspace);
3237 wUnhideApplication(wapp, event->xbutton.button == Button2, unhideHere);
3239 if (event->xbutton.state & MOD_MASK) {
3240 wHideOtherApplications(btn->icon->owner);
3242 } else {
3243 if (event->xbutton.button == Button1) {
3245 if (event->xbutton.state & MOD_MASK) {
3246 /* raise/lower dock */
3247 toggleLowered(dock);
3248 } else if (btn == dock->screen_ptr->clip_icon) {
3249 if (getClipButton(event->xbutton.x, event->xbutton.y) == CLIP_IDLE)
3250 toggleCollapsed(dock);
3251 else
3252 handleClipChangeWorkspace(dock->screen_ptr, event);
3253 } else if (btn->command) {
3254 if (!btn->launching && (!btn->running || (event->xbutton.state & ControlMask)))
3255 launchDockedApplication(btn, False);
3261 static void handleDockMove(WDock *dock, WAppIcon *aicon, XEvent *event)
3263 WScreen *scr = dock->screen_ptr;
3264 int ofs_x = event->xbutton.x, ofs_y = event->xbutton.y;
3265 int x, y;
3266 XEvent ev;
3267 int grabbed = 0, swapped = 0, done;
3268 Pixmap ghost = None;
3269 int superfluous = wPreferences.superfluous; /* we catch it to avoid problems */
3271 if (XGrabPointer(dpy, aicon->icon->core->window, True, ButtonMotionMask
3272 | ButtonReleaseMask | ButtonPressMask, GrabModeAsync,
3273 GrabModeAsync, None, None, CurrentTime) != GrabSuccess) {
3274 wwarning("pointer grab failed for dock move");
3276 y = 0;
3277 for (x = 0; x < dock->max_icons; x++) {
3278 if (dock->icon_array[x] != NULL && dock->icon_array[x]->yindex > y)
3279 y = dock->icon_array[x]->yindex;
3281 y++;
3282 XResizeWindow(dpy, scr->dock_shadow, ICON_SIZE, ICON_SIZE * y);
3284 done = 0;
3285 while (!done) {
3286 WMMaskEvent(dpy, PointerMotionMask | ButtonReleaseMask | ButtonPressMask
3287 | ButtonMotionMask | ExposureMask | EnterWindowMask, &ev);
3288 switch (ev.type) {
3289 case Expose:
3290 WMHandleEvent(&ev);
3291 break;
3293 case EnterNotify:
3294 /* It means the cursor moved so fast that it entered
3295 * something else (if moving slowly, it would have
3296 * stayed in the dock that is being moved. Ignore such
3297 * "spurious" EnterNotifiy's */
3298 break;
3300 case MotionNotify:
3301 if (!grabbed) {
3302 if (abs(ofs_x - ev.xmotion.x) >= MOVE_THRESHOLD
3303 || abs(ofs_y - ev.xmotion.y) >= MOVE_THRESHOLD) {
3304 XChangeActivePointerGrab(dpy, ButtonMotionMask
3305 | ButtonReleaseMask | ButtonPressMask,
3306 wCursor[WCUR_MOVE], CurrentTime);
3307 grabbed = 1;
3309 break;
3311 if (dock->type == WM_CLIP) {
3312 x = ev.xmotion.x_root - ofs_x;
3313 y = ev.xmotion.y_root - ofs_y;
3314 wScreenKeepInside(scr, &x, &y, ICON_SIZE, ICON_SIZE);
3316 moveDock(dock, x, y);
3317 } else {
3318 /* move vertically if pointer is inside the dock */
3319 if ((dock->on_right_side && ev.xmotion.x_root >= dock->x_pos - ICON_SIZE)
3320 || (!dock->on_right_side && ev.xmotion.x_root <= dock->x_pos + ICON_SIZE * 2)) {
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);
3325 moveDock(dock, dock->x_pos, y);
3327 /* move horizontally to change sides */
3328 x = ev.xmotion.x_root - ofs_x;
3329 if (!dock->on_right_side) {
3331 /* is on left */
3333 if (ev.xmotion.x_root > dock->x_pos + ICON_SIZE * 2) {
3334 XMoveWindow(dpy, scr->dock_shadow, scr->scr_width - ICON_SIZE
3335 - DOCK_EXTRA_SPACE - 1, dock->y_pos);
3336 if (superfluous && ghost == None) {
3337 ghost = MakeGhostDock(dock, dock->x_pos,
3338 scr->scr_width - ICON_SIZE
3339 - DOCK_EXTRA_SPACE - 1, dock->y_pos);
3340 XSetWindowBackgroundPixmap(dpy, scr->dock_shadow, ghost);
3341 XClearWindow(dpy, scr->dock_shadow);
3343 XMapRaised(dpy, scr->dock_shadow);
3344 swapped = 1;
3345 } else {
3346 if (superfluous && ghost != None) {
3347 XFreePixmap(dpy, ghost);
3348 ghost = None;
3350 XUnmapWindow(dpy, scr->dock_shadow);
3351 swapped = 0;
3353 } else {
3354 /* is on right */
3355 if (ev.xmotion.x_root < dock->x_pos - ICON_SIZE) {
3356 XMoveWindow(dpy, scr->dock_shadow, DOCK_EXTRA_SPACE, dock->y_pos);
3357 if (superfluous && ghost == None) {
3358 ghost = MakeGhostDock(dock, dock->x_pos,
3359 DOCK_EXTRA_SPACE, dock->y_pos);
3360 XSetWindowBackgroundPixmap(dpy, scr->dock_shadow, ghost);
3361 XClearWindow(dpy, scr->dock_shadow);
3363 XMapRaised(dpy, scr->dock_shadow);
3364 swapped = -1;
3365 } else {
3366 XUnmapWindow(dpy, scr->dock_shadow);
3367 swapped = 0;
3368 if (superfluous && ghost != None) {
3369 XFreePixmap(dpy, ghost);
3370 ghost = None;
3375 break;
3377 case ButtonPress:
3378 break;
3380 case ButtonRelease:
3381 if (ev.xbutton.button != event->xbutton.button)
3382 break;
3383 XUngrabPointer(dpy, CurrentTime);
3384 XUnmapWindow(dpy, scr->dock_shadow);
3385 XResizeWindow(dpy, scr->dock_shadow, ICON_SIZE, ICON_SIZE);
3386 if (dock->type == WM_DOCK) {
3387 if (swapped != 0) {
3388 if (swapped > 0)
3389 dock->on_right_side = 1;
3390 else
3391 dock->on_right_side = 0;
3392 swapDock(dock);
3393 wArrangeIcons(scr, False);
3396 done = 1;
3397 break;
3400 if (superfluous) {
3401 if (ghost != None)
3402 XFreePixmap(dpy, ghost);
3403 XSetWindowBackground(dpy, scr->dock_shadow, scr->white_pixel);
3407 static Bool handleIconMove(WDock *dock, WAppIcon *aicon, XEvent *event)
3409 WScreen *scr = dock->screen_ptr;
3410 Window wins[2];
3411 WIcon *icon = aicon->icon;
3412 WDock *dock2 = NULL, *last_dock = dock, *clip = NULL;
3413 int ondock, grabbed = 0, change_dock = 0, collapsed = 0;
3414 XEvent ev;
3415 int x = aicon->x_pos, y = aicon->y_pos;
3416 int ofs_x = event->xbutton.x, ofs_y = event->xbutton.y;
3417 int shad_x = x, shad_y = y;
3418 int ix = aicon->xindex, iy = aicon->yindex;
3419 int tmp;
3420 Pixmap ghost = None;
3421 Bool docked;
3422 int superfluous = wPreferences.superfluous; /* we catch it to avoid problems */
3423 int omnipresent = aicon->omnipresent; /* this must be cached!!! */
3424 Bool hasMoved = False;
3426 if (wPreferences.flags.noupdates)
3427 return hasMoved;
3429 if (XGrabPointer(dpy, icon->core->window, True, ButtonMotionMask
3430 | ButtonReleaseMask | ButtonPressMask, GrabModeAsync,
3431 GrabModeAsync, None, None, CurrentTime) != GrabSuccess) {
3434 if (!(event->xbutton.state & MOD_MASK))
3435 wRaiseFrame(icon->core);
3437 if (!wPreferences.flags.noclip)
3438 clip = scr->workspaces[scr->current_workspace]->clip;
3440 if (dock == scr->dock && !wPreferences.flags.noclip)
3441 dock2 = clip;
3442 else if (dock != scr->dock && !wPreferences.flags.nodock)
3443 dock2 = scr->dock;
3445 wins[0] = icon->core->window;
3446 wins[1] = scr->dock_shadow;
3447 XRestackWindows(dpy, wins, 2);
3448 XMoveResizeWindow(dpy, scr->dock_shadow, aicon->x_pos, aicon->y_pos, ICON_SIZE, ICON_SIZE);
3449 if (superfluous) {
3450 if (icon->pixmap != None)
3451 ghost = MakeGhostIcon(scr, icon->pixmap);
3452 else
3453 ghost = MakeGhostIcon(scr, icon->core->window);
3455 XSetWindowBackgroundPixmap(dpy, scr->dock_shadow, ghost);
3456 XClearWindow(dpy, scr->dock_shadow);
3458 XMapWindow(dpy, scr->dock_shadow);
3460 ondock = 1;
3462 while (1) {
3463 XMaskEvent(dpy, PointerMotionMask | ButtonReleaseMask | ButtonPressMask
3464 | ButtonMotionMask | ExposureMask, &ev);
3465 switch (ev.type) {
3466 case Expose:
3467 WMHandleEvent(&ev);
3468 break;
3470 case MotionNotify:
3471 hasMoved = True;
3472 if (!grabbed) {
3473 if (abs(ofs_x - ev.xmotion.x) >= MOVE_THRESHOLD
3474 || abs(ofs_y - ev.xmotion.y) >= MOVE_THRESHOLD) {
3475 XChangeActivePointerGrab(dpy, ButtonMotionMask
3476 | ButtonReleaseMask | ButtonPressMask,
3477 wCursor[WCUR_MOVE], CurrentTime);
3478 grabbed = 1;
3479 } else {
3480 break;
3484 if (omnipresent) {
3485 int i;
3486 for (i = 0; i < scr->workspace_count; i++) {
3487 if (i == scr->current_workspace)
3488 continue;
3489 wDockShowIcons(scr->workspaces[i]->clip);
3493 x = ev.xmotion.x_root - ofs_x;
3494 y = ev.xmotion.y_root - ofs_y;
3495 tmp = wDockSnapIcon(dock, aicon, x, y, &ix, &iy, True);
3496 if (tmp && dock2) {
3497 change_dock = 0;
3498 if (last_dock != dock && collapsed) {
3499 last_dock->collapsed = 1;
3500 wDockHideIcons(last_dock);
3501 collapsed = 0;
3503 if (!collapsed && (collapsed = dock->collapsed)) {
3504 dock->collapsed = 0;
3505 wDockShowIcons(dock);
3507 if (dock->auto_raise_lower)
3508 wDockRaise(dock);
3509 last_dock = dock;
3510 } else if (dock2) {
3511 tmp = wDockSnapIcon(dock2, aicon, x, y, &ix, &iy, False);
3512 if (tmp) {
3513 change_dock = 1;
3514 if (last_dock != dock2 && collapsed) {
3515 last_dock->collapsed = 1;
3516 wDockHideIcons(last_dock);
3517 collapsed = 0;
3519 if (!collapsed && (collapsed = dock2->collapsed)) {
3520 dock2->collapsed = 0;
3521 wDockShowIcons(dock2);
3523 if (dock2->auto_raise_lower)
3524 wDockRaise(dock2);
3525 last_dock = dock2;
3528 if (aicon->launching || aicon->lock || (aicon->running && !(ev.xmotion.state & MOD_MASK))
3529 || (!aicon->running && tmp)) {
3530 shad_x = last_dock->x_pos + ix * wPreferences.icon_size;
3531 shad_y = last_dock->y_pos + iy * wPreferences.icon_size;
3533 XMoveWindow(dpy, scr->dock_shadow, shad_x, shad_y);
3535 if (!ondock) {
3536 XMapWindow(dpy, scr->dock_shadow);
3538 ondock = 1;
3539 } else {
3540 if (ondock) {
3541 XUnmapWindow(dpy, scr->dock_shadow);
3543 ondock = 0;
3545 XMoveWindow(dpy, icon->core->window, x, y);
3546 break;
3548 case ButtonPress:
3549 break;
3551 case ButtonRelease:
3552 if (ev.xbutton.button != event->xbutton.button)
3553 break;
3554 XUngrabPointer(dpy, CurrentTime);
3555 if (ondock) {
3556 SlideWindow(icon->core->window, x, y, shad_x, shad_y);
3557 XUnmapWindow(dpy, scr->dock_shadow);
3558 if (!change_dock) {
3559 reattachIcon(dock, aicon, ix, iy);
3560 if (clip && dock != clip && clip->auto_raise_lower)
3561 wDockLower(clip);
3562 } else {
3563 docked = moveIconBetweenDocks(dock, dock2, aicon, ix, iy);
3564 if (!docked) {
3565 /* Slide it back if dock rejected it */
3566 SlideWindow(icon->core->window, x, y, aicon->x_pos, aicon->y_pos);
3567 reattachIcon(dock, aicon, aicon->xindex, aicon->yindex);
3569 if (last_dock->type == WM_CLIP && last_dock->auto_collapse) {
3570 collapsed = 0;
3573 } else {
3574 aicon->x_pos = x;
3575 aicon->y_pos = y;
3576 if (superfluous) {
3577 if (!aicon->running && !wPreferences.no_animations) {
3578 /* We need to deselect it, even if is deselected in
3579 * wDockDetach(), because else DoKaboom() will fail.
3581 if (aicon->icon->selected)
3582 wIconSelect(aicon->icon);
3584 DoKaboom(scr, aicon->icon->core->window, x, y);
3587 if (clip && clip->auto_raise_lower)
3588 wDockLower(clip);
3589 wDockDetach(dock, aicon);
3591 if (collapsed) {
3592 last_dock->collapsed = 1;
3593 wDockHideIcons(last_dock);
3594 collapsed = 0;
3596 if (superfluous) {
3597 if (ghost != None)
3598 XFreePixmap(dpy, ghost);
3599 XSetWindowBackground(dpy, scr->dock_shadow, scr->white_pixel);
3601 if (omnipresent) {
3602 int i;
3603 for (i = 0; i < scr->workspace_count; i++) {
3604 if (i == scr->current_workspace)
3605 continue;
3606 wDockHideIcons(scr->workspaces[i]->clip);
3609 return hasMoved;;
3612 return False; /* never reached */
3615 static int getClipButton(int px, int py)
3617 int pt = (CLIP_BUTTON_SIZE + 2) * ICON_SIZE / 64;
3619 if (px < 0 || py < 0 || px >= ICON_SIZE || py >= ICON_SIZE)
3620 return CLIP_IDLE;
3622 if (py <= pt - ((int)ICON_SIZE - 1 - px))
3623 return CLIP_FORWARD;
3624 else if (px <= pt - ((int)ICON_SIZE - 1 - py))
3625 return CLIP_REWIND;
3627 return CLIP_IDLE;
3630 static void handleClipChangeWorkspace(WScreen *scr, XEvent *event)
3632 XEvent ev;
3633 int done, direction, new_ws;
3634 int new_dir;
3635 WDock *clip = scr->clip_icon->dock;
3637 direction = getClipButton(event->xbutton.x, event->xbutton.y);
3639 clip->lclip_button_pushed = direction == CLIP_REWIND;
3640 clip->rclip_button_pushed = direction == CLIP_FORWARD;
3642 wClipIconPaint(scr->clip_icon);
3643 done = 0;
3644 while (!done) {
3645 WMMaskEvent(dpy, ExposureMask | ButtonMotionMask | ButtonReleaseMask | ButtonPressMask, &ev);
3646 switch (ev.type) {
3647 case Expose:
3648 WMHandleEvent(&ev);
3649 break;
3651 case MotionNotify:
3652 new_dir = getClipButton(ev.xmotion.x, ev.xmotion.y);
3653 if (new_dir != direction) {
3654 direction = new_dir;
3655 clip->lclip_button_pushed = direction == CLIP_REWIND;
3656 clip->rclip_button_pushed = direction == CLIP_FORWARD;
3657 wClipIconPaint(scr->clip_icon);
3659 break;
3661 case ButtonPress:
3662 break;
3664 case ButtonRelease:
3665 if (ev.xbutton.button == event->xbutton.button)
3666 done = 1;
3670 clip->lclip_button_pushed = 0;
3671 clip->rclip_button_pushed = 0;
3673 new_ws = wPreferences.ws_advance || (event->xbutton.state & ControlMask);
3675 if (direction == CLIP_FORWARD) {
3676 if (scr->current_workspace < scr->workspace_count - 1)
3677 wWorkspaceChange(scr, scr->current_workspace + 1);
3678 else if (new_ws && scr->current_workspace < MAX_WORKSPACES - 1)
3679 wWorkspaceChange(scr, scr->current_workspace + 1);
3680 else if (wPreferences.ws_cycle)
3681 wWorkspaceChange(scr, 0);
3682 } else if (direction == CLIP_REWIND) {
3683 if (scr->current_workspace > 0)
3684 wWorkspaceChange(scr, scr->current_workspace - 1);
3685 else if (scr->current_workspace == 0 && wPreferences.ws_cycle)
3686 wWorkspaceChange(scr, scr->workspace_count - 1);
3689 wClipIconPaint(scr->clip_icon);
3692 static void iconMouseDown(WObjDescriptor *desc, XEvent *event)
3694 WAppIcon *aicon = desc->parent;
3695 WDock *dock = aicon->dock;
3696 WScreen *scr = aicon->icon->core->screen_ptr;
3698 if (aicon->editing || WCHECK_STATE(WSTATE_MODAL))
3699 return;
3701 scr->last_dock = dock;
3703 if (dock->menu->flags.mapped)
3704 wMenuUnmap(dock->menu);
3706 if (IsDoubleClick(scr, event)) {
3707 /* double-click was not in the main clip icon */
3708 if (dock->type != WM_CLIP || aicon->xindex != 0 || aicon->yindex != 0
3709 || getClipButton(event->xbutton.x, event->xbutton.y) == CLIP_IDLE) {
3710 iconDblClick(desc, event);
3711 return;
3715 if (event->xbutton.button == Button1) {
3716 if (event->xbutton.state & MOD_MASK)
3717 wDockLower(dock);
3718 else
3719 wDockRaise(dock);
3721 if ((event->xbutton.state & ShiftMask) && aicon != scr->clip_icon && dock->type != WM_DOCK) {
3722 wIconSelect(aicon->icon);
3723 return;
3726 if (aicon->yindex == 0 && aicon->xindex == 0) {
3727 if (getClipButton(event->xbutton.x, event->xbutton.y) != CLIP_IDLE
3728 && dock->type == WM_CLIP)
3729 handleClipChangeWorkspace(scr, event);
3730 else
3731 handleDockMove(dock, aicon, event);
3732 } else {
3733 Bool hasMoved = handleIconMove(dock, aicon, event);
3734 if (wPreferences.single_click && !hasMoved)
3735 iconDblClick(desc, event);
3737 } else if (event->xbutton.button == Button2 && dock->type == WM_CLIP && aicon == scr->clip_icon) {
3738 if (!scr->clip_ws_menu) {
3739 scr->clip_ws_menu = wWorkspaceMenuMake(scr, False);
3741 if (scr->clip_ws_menu) {
3742 WMenu *wsMenu = scr->clip_ws_menu;
3743 int xpos;
3745 wWorkspaceMenuUpdate(scr, wsMenu);
3747 xpos = event->xbutton.x_root - wsMenu->frame->core->width / 2 - 1;
3748 if (xpos < 0) {
3749 xpos = 0;
3750 } else if (xpos + wsMenu->frame->core->width > scr->scr_width - 2) {
3751 xpos = scr->scr_width - wsMenu->frame->core->width - 4;
3753 wMenuMapAt(wsMenu, xpos, event->xbutton.y_root + 2, False);
3755 desc = &wsMenu->menu->descriptor;
3756 event->xany.send_event = True;
3757 (*desc->handle_mousedown) (desc, event);
3759 } else if (event->xbutton.button == Button2 && dock->type == WM_CLIP &&
3760 (event->xbutton.state & ShiftMask) && aicon != scr->clip_icon) {
3761 wClipMakeIconOmnipresent(aicon, !aicon->omnipresent);
3762 } else if (event->xbutton.button == Button3) {
3763 if (event->xbutton.send_event &&
3764 XGrabPointer(dpy, aicon->icon->core->window, True, ButtonMotionMask
3765 | ButtonReleaseMask | ButtonPressMask, GrabModeAsync,
3766 GrabModeAsync, None, None, CurrentTime) != GrabSuccess) {
3767 wwarning("pointer grab failed for dockicon menu");
3768 return;
3771 openDockMenu(dock, aicon, event);
3772 } else if (event->xbutton.button == Button2) {
3773 WAppIcon *btn = desc->parent;
3775 if (!btn->launching && (!btn->running || (event->xbutton.state & ControlMask))) {
3776 launchDockedApplication(btn, True);
3781 static void clipEnterNotify(WObjDescriptor *desc, XEvent *event)
3783 WAppIcon *btn = (WAppIcon *) desc->parent;
3784 WDock *dock;
3785 WScreen *scr;
3787 assert(event->type == EnterNotify);
3789 if (desc->parent_type != WCLASS_DOCK_ICON)
3790 return;
3792 scr = btn->icon->core->screen_ptr;
3793 if (!btn->omnipresent)
3794 dock = btn->dock;
3795 else
3796 dock = scr->workspaces[scr->current_workspace]->clip;
3798 if (!dock || dock->type != WM_CLIP)
3799 return;
3801 /* The auto raise/lower code */
3802 if (dock->auto_lower_magic) {
3803 WMDeleteTimerHandler(dock->auto_lower_magic);
3804 dock->auto_lower_magic = NULL;
3806 if (dock->auto_raise_lower && !dock->auto_raise_magic) {
3807 dock->auto_raise_magic = WMAddTimerHandler(AUTO_RAISE_DELAY, clipAutoRaise, (void *)dock);
3810 /* The auto expand/collapse code */
3811 if (dock->auto_collapse_magic) {
3812 WMDeleteTimerHandler(dock->auto_collapse_magic);
3813 dock->auto_collapse_magic = NULL;
3815 if (dock->auto_collapse && !dock->auto_expand_magic) {
3816 dock->auto_expand_magic = WMAddTimerHandler(AUTO_EXPAND_DELAY, clipAutoExpand, (void *)dock);
3820 static void clipLeave(WDock *dock)
3822 XEvent event;
3823 WObjDescriptor *desc = NULL;
3825 if (!dock || dock->type != WM_CLIP)
3826 return;
3828 if (XCheckTypedEvent(dpy, EnterNotify, &event) != False) {
3829 if (XFindContext(dpy, event.xcrossing.window, wWinContext,
3830 (XPointer *) & desc) != XCNOENT
3831 && desc && desc->parent_type == WCLASS_DOCK_ICON
3832 && ((WAppIcon *) desc->parent)->dock && ((WAppIcon *) desc->parent)->dock->type == WM_CLIP) {
3833 /* We didn't left the Clip yet */
3834 XPutBackEvent(dpy, &event);
3835 return;
3838 XPutBackEvent(dpy, &event);
3839 } else {
3840 /* We entered a withdrawn window, so we're still in Clip */
3841 return;
3844 if (dock->auto_raise_magic) {
3845 WMDeleteTimerHandler(dock->auto_raise_magic);
3846 dock->auto_raise_magic = NULL;
3848 if (dock->auto_raise_lower && !dock->auto_lower_magic) {
3849 dock->auto_lower_magic = WMAddTimerHandler(AUTO_LOWER_DELAY, clipAutoLower, (void *)dock);
3852 if (dock->auto_expand_magic) {
3853 WMDeleteTimerHandler(dock->auto_expand_magic);
3854 dock->auto_expand_magic = NULL;
3856 if (dock->auto_collapse && !dock->auto_collapse_magic) {
3857 dock->auto_collapse_magic = WMAddTimerHandler(AUTO_COLLAPSE_DELAY, clipAutoCollapse, (void *)dock);
3861 static void clipLeaveNotify(WObjDescriptor *desc, XEvent *event)
3863 WAppIcon *btn = (WAppIcon *) desc->parent;
3865 assert(event->type == LeaveNotify);
3867 if (desc->parent_type != WCLASS_DOCK_ICON)
3868 return;
3870 clipLeave(btn->dock);
3873 static void clipAutoCollapse(void *cdata)
3875 WDock *dock = (WDock *) cdata;
3877 if (dock->type != WM_CLIP)
3878 return;
3880 if (dock->auto_collapse) {
3881 dock->collapsed = 1;
3882 wDockHideIcons(dock);
3884 dock->auto_collapse_magic = NULL;
3887 static void clipAutoExpand(void *cdata)
3889 WDock *dock = (WDock *) cdata;
3891 if (dock->type != WM_CLIP)
3892 return;
3894 if (dock->auto_collapse) {
3895 dock->collapsed = 0;
3896 wDockShowIcons(dock);
3898 dock->auto_expand_magic = NULL;
3901 static void clipAutoLower(void *cdata)
3903 WDock *dock = (WDock *) cdata;
3905 if (dock->type != WM_CLIP)
3906 return;
3908 if (dock->auto_raise_lower)
3909 wDockLower(dock);
3911 dock->auto_lower_magic = NULL;
3914 static void clipAutoRaise(void *cdata)
3916 WDock *dock = (WDock *) cdata;
3918 if (dock->type != WM_CLIP)
3919 return;
3921 if (dock->auto_raise_lower)
3922 wDockRaise(dock);
3924 dock->auto_raise_magic = NULL;
3927 static Bool iconCanBeOmnipresent(WAppIcon *aicon)
3929 WScreen *scr = aicon->icon->core->screen_ptr;
3930 WDock *clip;
3931 WAppIcon *btn;
3932 int i, j;
3934 for (i = 0; i < scr->workspace_count; i++) {
3935 clip = scr->workspaces[i]->clip;
3937 if (clip == aicon->dock)
3938 continue;
3940 if (clip->icon_count + scr->global_icon_count >= clip->max_icons)
3941 return False; /* Clip is full in some workspace */
3943 for (j = 0; j < clip->max_icons; j++) {
3944 btn = clip->icon_array[j];
3945 if (btn && btn->xindex == aicon->xindex && btn->yindex == aicon->yindex)
3946 return False;
3950 return True;
3953 int wClipMakeIconOmnipresent(WAppIcon *aicon, int omnipresent)
3955 WScreen *scr = aicon->icon->core->screen_ptr;
3956 WAppIconChain *new_entry, *tmp, *tmp1;
3957 int status = WO_SUCCESS;
3959 if ((scr->dock && aicon->dock == scr->dock) || aicon == scr->clip_icon) {
3960 return WO_NOT_APPLICABLE;
3963 if (aicon->omnipresent == omnipresent)
3964 return WO_SUCCESS;
3966 if (omnipresent) {
3967 if (iconCanBeOmnipresent(aicon)) {
3968 aicon->omnipresent = 1;
3969 new_entry = wmalloc(sizeof(WAppIconChain));
3970 new_entry->aicon = aicon;
3971 new_entry->next = scr->global_icons;
3972 scr->global_icons = new_entry;
3973 scr->global_icon_count++;
3974 } else {
3975 aicon->omnipresent = 0;
3976 status = WO_FAILED;
3978 } else {
3979 aicon->omnipresent = 0;
3980 if (aicon == scr->global_icons->aicon) {
3981 tmp = scr->global_icons->next;
3982 wfree(scr->global_icons);
3983 scr->global_icons = tmp;
3984 scr->global_icon_count--;
3985 } else {
3986 tmp = scr->global_icons;
3987 while (tmp->next) {
3988 if (tmp->next->aicon == aicon) {
3989 tmp1 = tmp->next->next;
3990 wfree(tmp->next);
3991 tmp->next = tmp1;
3992 scr->global_icon_count--;
3993 break;
3995 tmp = tmp->next;
4000 wAppIconPaint(aicon);
4002 return status;