Added some comments and spaces in Git's ignore file
[wmaker-crm.git] / src / dock.c
blob0db0fe574b4e47409baae3eb457e750b12b521f4
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(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->wm_instance, btn->wm_class, &attr, NULL, True);
1845 if (!attr.no_appicon)
1846 btn->launching = 1;
1847 else
1848 btn->running = 0;
1851 btn->paste_launch = 0;
1852 btn->drop_launch = 1;
1853 scr->last_dock = dock;
1854 btn->pid = execCommand(btn, btn->dnd_command, NULL);
1855 if (btn->pid > 0) {
1856 dockIconPaint(btn);
1857 } else {
1858 btn->launching = 0;
1859 if (!btn->relaunching) {
1860 btn->running = 0;
1864 return False;
1866 #endif /* XDND */
1868 Bool wDockAttachIcon(WDock *dock, WAppIcon *icon, int x, int y)
1870 WWindow *wwin;
1871 int index;
1873 wwin = icon->icon->owner;
1874 if (icon->command == NULL) {
1875 char *command;
1877 icon->editing = 0;
1879 command = GetCommandForWindow(wwin->client_win);
1880 if (command) {
1881 icon->command = command;
1882 } else {
1883 /* icon->forced_dock = 1; */
1884 if (dock->type != WM_CLIP || !icon->attracted) {
1885 icon->editing = 1;
1886 if (wInputDialog(dock->screen_ptr, _("Dock Icon"),
1887 _("Type the command used to launch the application"), &command)) {
1888 if (command && (command[0] == 0 || (command[0] == '-' && command[1] == 0))) {
1889 wfree(command);
1890 command = NULL;
1892 icon->command = command;
1893 icon->editing = 0;
1894 } else {
1895 icon->editing = 0;
1896 if (command)
1897 wfree(command);
1898 /* If the target is the dock, reject the icon. If
1899 * the target is the clip, make it an attracted icon
1901 if (dock->type == WM_CLIP) {
1902 icon->attracted = 1;
1903 if (!icon->icon->shadowed) {
1904 icon->icon->shadowed = 1;
1905 icon->icon->force_paint = 1;
1907 } else {
1908 return False;
1913 } else {
1914 icon->editing = 0;
1917 for (index = 1; index < dock->max_icons; index++)
1918 if (dock->icon_array[index] == NULL)
1919 break;
1920 /* if (index == dock->max_icons)
1921 return; */
1923 assert(index < dock->max_icons);
1925 dock->icon_array[index] = icon;
1926 icon->yindex = y;
1927 icon->xindex = x;
1929 icon->omnipresent = 0;
1931 icon->x_pos = dock->x_pos + x * ICON_SIZE;
1932 icon->y_pos = dock->y_pos + y * ICON_SIZE;
1934 dock->icon_count++;
1936 icon->running = 1;
1937 icon->launching = 0;
1938 icon->docked = 1;
1939 icon->dock = dock;
1940 icon->icon->core->descriptor.handle_mousedown = iconMouseDown;
1941 if (dock->type == WM_CLIP) {
1942 icon->icon->core->descriptor.handle_enternotify = clipEnterNotify;
1943 icon->icon->core->descriptor.handle_leavenotify = clipLeaveNotify;
1945 icon->icon->core->descriptor.parent_type = WCLASS_DOCK_ICON;
1946 icon->icon->core->descriptor.parent = icon;
1948 MoveInStackListUnder(dock->icon_array[index - 1]->icon->core, icon->icon->core);
1949 wAppIconMove(icon, icon->x_pos, icon->y_pos);
1950 wAppIconPaint(icon);
1951 save_appicon(icon, True);
1953 if (wPreferences.auto_arrange_icons)
1954 wArrangeIcons(dock->screen_ptr, True);
1956 #ifdef XDND /* was OFFIX */
1957 if (icon->command && !icon->dnd_command) {
1958 int len = strlen(icon->command) + 8;
1959 icon->dnd_command = wmalloc(len);
1960 snprintf(icon->dnd_command, len, "%s %%d", icon->command);
1962 #endif
1964 if (icon->command && !icon->paste_command) {
1965 int len = strlen(icon->command) + 8;
1966 icon->paste_command = wmalloc(len);
1967 snprintf(icon->paste_command, len, "%s %%s", icon->command);
1970 return True;
1973 static void reattachIcon(WDock *dock, WAppIcon *icon, int x, int y)
1975 int index;
1977 for (index = 1; index < dock->max_icons; index++) {
1978 if (dock->icon_array[index] == icon)
1979 break;
1981 assert(index < dock->max_icons);
1983 icon->yindex = y;
1984 icon->xindex = x;
1986 icon->x_pos = dock->x_pos + x * ICON_SIZE;
1987 icon->y_pos = dock->y_pos + y * ICON_SIZE;
1990 static Bool moveIconBetweenDocks(WDock *src, WDock *dest, WAppIcon *icon, int x, int y)
1992 WWindow *wwin;
1993 char *command;
1994 int index;
1996 if (src == dest)
1997 return True; /* No move needed, we're already there */
1999 if (dest == NULL)
2000 return False;
2002 wwin = icon->icon->owner;
2005 * For the moment we can't do this if we move icons in Clip from one
2006 * workspace to other, because if we move two or more icons without
2007 * command, the dialog box will not be able to tell us to which of the
2008 * moved icons it applies. -Dan
2010 if ((dest->type == WM_DOCK /*|| dest->keep_attracted */ ) && icon->command == NULL) {
2011 command = GetCommandForWindow(wwin->client_win);
2012 if (command) {
2013 icon->command = command;
2014 } else {
2015 icon->editing = 1;
2016 /* icon->forced_dock = 1; */
2017 if (wInputDialog(src->screen_ptr, _("Dock Icon"),
2018 _("Type the command used to launch the application"), &command)) {
2019 if (command && (command[0] == 0 || (command[0] == '-' && command[1] == 0))) {
2020 wfree(command);
2021 command = NULL;
2023 icon->command = command;
2024 } else {
2025 icon->editing = 0;
2026 if (command)
2027 wfree(command);
2028 return False;
2030 icon->editing = 0;
2034 if (dest->type == WM_DOCK)
2035 wClipMakeIconOmnipresent(icon, False);
2037 for (index = 1; index < src->max_icons; index++) {
2038 if (src->icon_array[index] == icon)
2039 break;
2041 assert(index < src->max_icons);
2043 src->icon_array[index] = NULL;
2044 src->icon_count--;
2046 for (index = 1; index < dest->max_icons; index++) {
2047 if (dest->icon_array[index] == NULL)
2048 break;
2050 /* if (index == dest->max_icons)
2051 return; */
2053 assert(index < dest->max_icons);
2055 dest->icon_array[index] = icon;
2056 icon->dock = dest;
2058 /* deselect the icon */
2059 if (icon->icon->selected)
2060 wIconSelect(icon->icon);
2062 if (dest->type == WM_DOCK) {
2063 icon->icon->core->descriptor.handle_enternotify = NULL;
2064 icon->icon->core->descriptor.handle_leavenotify = NULL;
2065 } else {
2066 icon->icon->core->descriptor.handle_enternotify = clipEnterNotify;
2067 icon->icon->core->descriptor.handle_leavenotify = clipLeaveNotify;
2070 /* set it to be kept when moving to dock.
2071 * Unless the icon does not have a command set
2073 if (icon->command && dest->type == WM_DOCK) {
2074 icon->attracted = 0;
2075 if (icon->icon->shadowed) {
2076 icon->icon->shadowed = 0;
2077 icon->icon->force_paint = 1;
2079 save_appicon(icon, True);
2082 if (src->auto_collapse || src->auto_raise_lower)
2083 clipLeave(src);
2085 icon->yindex = y;
2086 icon->xindex = x;
2088 icon->x_pos = dest->x_pos + x * ICON_SIZE;
2089 icon->y_pos = dest->y_pos + y * ICON_SIZE;
2091 dest->icon_count++;
2093 MoveInStackListUnder(dest->icon_array[index - 1]->icon->core, icon->icon->core);
2094 wAppIconPaint(icon);
2096 return True;
2099 void wDockDetach(WDock *dock, WAppIcon *icon)
2101 int index;
2103 /* make the settings panel be closed */
2104 if (icon->panel) {
2105 DestroyDockAppSettingsPanel(icon->panel);
2108 /* This must be called before icon->dock is set to NULL.
2109 * Don't move it. -Dan
2111 wClipMakeIconOmnipresent(icon, False);
2113 icon->docked = 0;
2114 icon->dock = NULL;
2115 icon->attracted = 0;
2116 icon->auto_launch = 0;
2117 if (icon->icon->shadowed) {
2118 icon->icon->shadowed = 0;
2119 icon->icon->force_paint = 1;
2122 /* deselect the icon */
2123 if (icon->icon->selected)
2124 wIconSelect(icon->icon);
2126 if (icon->command) {
2127 wfree(icon->command);
2128 icon->command = NULL;
2130 #ifdef XDND /* was OFFIX */
2131 if (icon->dnd_command) {
2132 wfree(icon->dnd_command);
2133 icon->dnd_command = NULL;
2135 #endif
2136 if (icon->paste_command) {
2137 wfree(icon->paste_command);
2138 icon->paste_command = NULL;
2141 for (index = 1; index < dock->max_icons; index++)
2142 if (dock->icon_array[index] == icon)
2143 break;
2144 assert(index < dock->max_icons);
2145 dock->icon_array[index] = NULL;
2146 icon->yindex = -1;
2147 icon->xindex = -1;
2149 dock->icon_count--;
2151 /* if the dock is not attached to an application or
2152 * the application did not set the appropriate hints yet,
2153 * destroy the icon */
2154 if (!icon->running || !wApplicationOf(icon->main_window))
2155 wAppIconDestroy(icon);
2156 else {
2157 icon->icon->core->descriptor.handle_mousedown = appIconMouseDown;
2158 icon->icon->core->descriptor.handle_enternotify = NULL;
2159 icon->icon->core->descriptor.handle_leavenotify = NULL;
2160 icon->icon->core->descriptor.parent_type = WCLASS_APPICON;
2161 icon->icon->core->descriptor.parent = icon;
2163 ChangeStackingLevel(icon->icon->core, NORMAL_ICON_LEVEL);
2165 wAppIconPaint(icon);
2166 if (wPreferences.auto_arrange_icons) {
2167 wArrangeIcons(dock->screen_ptr, True);
2170 if (dock->auto_collapse || dock->auto_raise_lower)
2171 clipLeave(dock);
2175 * returns the closest Dock slot index for the passed
2176 * coordinates.
2178 * Returns False if icon can't be docked.
2180 * Note: this function should NEVER alter ret_x or ret_y, unless it will
2181 * return True. -Dan
2183 Bool wDockSnapIcon(WDock *dock, WAppIcon *icon, int req_x, int req_y, int *ret_x, int *ret_y, int redocking)
2185 WScreen *scr = dock->screen_ptr;
2186 int dx, dy;
2187 int ex_x, ex_y;
2188 int i, offset = ICON_SIZE / 2;
2189 WAppIcon *aicon = NULL;
2190 WAppIcon *nicon = NULL;
2191 int max_y_icons;
2193 /* TODO: XINERAMA, for these */
2194 max_y_icons = scr->scr_height / ICON_SIZE - 1;
2196 if (wPreferences.flags.noupdates)
2197 return False;
2199 dx = dock->x_pos;
2200 dy = dock->y_pos;
2202 /* if the dock is full */
2203 if (!redocking && (dock->icon_count >= dock->max_icons)) {
2204 return False;
2207 /* exact position */
2208 if (req_y < dy)
2209 ex_y = (req_y - offset - dy) / ICON_SIZE;
2210 else
2211 ex_y = (req_y + offset - dy) / ICON_SIZE;
2213 if (req_x < dx)
2214 ex_x = (req_x - offset - dx) / ICON_SIZE;
2215 else
2216 ex_x = (req_x + offset - dx) / ICON_SIZE;
2218 /* check if the icon is outside the screen boundaries */
2220 WMRect rect;
2221 int flags;
2223 rect.pos.x = dx + ex_x * ICON_SIZE;
2224 rect.pos.y = dy + ex_y * ICON_SIZE;
2225 rect.size.width = rect.size.height = ICON_SIZE;
2227 wGetRectPlacementInfo(scr, rect, &flags);
2228 if (flags & (XFLAG_DEAD | XFLAG_PARTIAL))
2229 return False;
2232 if (dock->type == WM_DOCK) {
2233 if (icon->dock != dock && ex_x != 0)
2234 return False;
2236 aicon = NULL;
2237 for (i = 0; i < dock->max_icons; i++) {
2238 nicon = dock->icon_array[i];
2239 if (nicon && nicon->yindex == ex_y) {
2240 aicon = nicon;
2241 break;
2245 if (redocking) {
2246 int sig, done, closest;
2248 /* Possible cases when redocking:
2250 * icon dragged out of range of any slot -> false
2251 * icon dragged to range of free slot
2252 * icon dragged to range of same slot
2253 * icon dragged to range of different icon
2255 if (abs(ex_x) > DOCK_DETTACH_THRESHOLD)
2256 return False;
2258 if (ex_y >= 0 && ex_y <= max_y_icons && (aicon == icon || !aicon)) {
2259 *ret_x = 0;
2260 *ret_y = ex_y;
2261 return True;
2264 /* start looking at the upper slot or lower? */
2265 if (ex_y * ICON_SIZE < (req_y + offset - dy))
2266 sig = 1;
2267 else
2268 sig = -1;
2270 closest = -1;
2271 done = 0;
2272 /* look for closest free slot */
2273 for (i = 0; i < (DOCK_DETTACH_THRESHOLD + 1) * 2 && !done; i++) {
2274 int j;
2276 done = 1;
2277 closest = sig * (i / 2) + ex_y;
2278 /* check if this slot is used */
2279 if (closest >= 0) {
2280 for (j = 0; j < dock->max_icons; j++) {
2281 if (dock->icon_array[j]
2282 && dock->icon_array[j]->yindex == closest) {
2283 /* slot is used by someone else */
2284 if (dock->icon_array[j] != icon)
2285 done = 0;
2286 break;
2290 sig = -sig;
2292 if (done && closest >= 0 && closest <= max_y_icons &&
2293 ((ex_y >= closest && ex_y - closest < DOCK_DETTACH_THRESHOLD + 1)
2294 || (ex_y < closest && closest - ex_y <= DOCK_DETTACH_THRESHOLD + 1))) {
2295 *ret_x = 0;
2296 *ret_y = closest;
2297 return True;
2299 } else { /* !redocking */
2301 /* if slot is free and the icon is close enough, return it */
2302 if (!aicon && ex_x == 0 && ex_y >= 0 && ex_y <= max_y_icons) {
2303 *ret_x = 0;
2304 *ret_y = ex_y;
2305 return True;
2308 } else { /* CLIP */
2309 int neighbours = 0;
2310 int start, stop, k;
2312 start = icon->omnipresent ? 0 : scr->current_workspace;
2313 stop = icon->omnipresent ? scr->workspace_count : start + 1;
2315 aicon = NULL;
2316 for (k = start; k < stop; k++) {
2317 WDock *tmp = scr->workspaces[k]->clip;
2318 if (!tmp)
2319 continue;
2320 for (i = 0; i < tmp->max_icons; i++) {
2321 nicon = tmp->icon_array[i];
2322 if (nicon && nicon->xindex == ex_x && nicon->yindex == ex_y) {
2323 aicon = nicon;
2324 break;
2327 if (aicon)
2328 break;
2330 for (k = start; k < stop; k++) {
2331 WDock *tmp = scr->workspaces[k]->clip;
2332 if (!tmp)
2333 continue;
2334 for (i = 0; i < tmp->max_icons; i++) {
2335 nicon = tmp->icon_array[i];
2336 if (nicon && nicon != icon && /* Icon can't be it's own neighbour */
2337 (abs(nicon->xindex - ex_x) <= CLIP_ATTACH_VICINITY &&
2338 abs(nicon->yindex - ex_y) <= CLIP_ATTACH_VICINITY)) {
2339 neighbours = 1;
2340 break;
2343 if (neighbours)
2344 break;
2347 if (neighbours && (aicon == NULL || (redocking && aicon == icon))) {
2348 *ret_x = ex_x;
2349 *ret_y = ex_y;
2350 return True;
2353 return False;
2356 static int onScreen(WScreen *scr, int x, int y, int sx, int ex, int sy, int ey)
2358 WMRect rect = wmkrect(x, y, ICON_SIZE, ICON_SIZE);
2359 int flags;
2361 wGetRectPlacementInfo(scr, rect, &flags);
2363 return !(flags & (XFLAG_DEAD | XFLAG_PARTIAL));
2367 * returns true if it can find a free slot in the dock,
2368 * in which case it changes x_pos and y_pos accordingly.
2369 * Else returns false.
2371 Bool wDockFindFreeSlot(WDock *dock, int *x_pos, int *y_pos)
2373 WScreen *scr = dock->screen_ptr;
2374 WAppIcon *btn;
2375 WAppIconChain *chain;
2376 unsigned char *slot_map;
2377 int mwidth;
2378 int r;
2379 int x, y;
2380 int i, done = False;
2381 int corner;
2382 int sx = 0, sy = 0, ex = scr->scr_width, ey = scr->scr_height;
2383 int extra_count = 0;
2385 if (dock->type == WM_CLIP && dock != scr->workspaces[scr->current_workspace]->clip)
2386 extra_count = scr->global_icon_count;
2388 /* if the dock is full */
2389 if (dock->icon_count + extra_count >= dock->max_icons) {
2390 return False;
2393 if (!wPreferences.flags.nodock && scr->dock) {
2394 if (scr->dock->on_right_side)
2395 ex -= ICON_SIZE + DOCK_EXTRA_SPACE;
2396 else
2397 sx += ICON_SIZE + DOCK_EXTRA_SPACE;
2400 if (ex < dock->x_pos)
2401 ex = dock->x_pos;
2402 if (sx > dock->x_pos + ICON_SIZE)
2403 sx = dock->x_pos + ICON_SIZE;
2404 #define C_NONE 0
2405 #define C_NW 1
2406 #define C_NE 2
2407 #define C_SW 3
2408 #define C_SE 4
2410 /* check if clip is in a corner */
2411 if (dock->type == WM_CLIP) {
2412 if (dock->x_pos < 1 && dock->y_pos < 1)
2413 corner = C_NE;
2414 else if (dock->x_pos < 1 && dock->y_pos >= (ey - ICON_SIZE))
2415 corner = C_SE;
2416 else if (dock->x_pos >= (ex - ICON_SIZE) && dock->y_pos >= (ey - ICON_SIZE))
2417 corner = C_SW;
2418 else if (dock->x_pos >= (ex - ICON_SIZE) && dock->y_pos < 1)
2419 corner = C_NW;
2420 else
2421 corner = C_NONE;
2422 } else
2423 corner = C_NONE;
2425 /* If the clip is in the corner, use only slots that are in the border
2426 * of the screen */
2427 if (corner != C_NONE) {
2428 char *hmap, *vmap;
2429 int hcount, vcount;
2431 hcount = WMIN(dock->max_icons, scr->scr_width / ICON_SIZE);
2432 vcount = WMIN(dock->max_icons, scr->scr_height / ICON_SIZE);
2433 hmap = wmalloc(hcount + 1);
2434 vmap = wmalloc(vcount + 1);
2436 /* mark used positions */
2437 switch (corner) {
2438 case C_NE:
2439 for (i = 0; i < dock->max_icons; i++) {
2440 btn = dock->icon_array[i];
2441 if (!btn)
2442 continue;
2444 if (btn->xindex == 0 && btn->yindex > 0 && btn->yindex < vcount)
2445 vmap[btn->yindex] = 1;
2446 else if (btn->yindex == 0 && btn->xindex > 0 && btn->xindex < hcount)
2447 hmap[btn->xindex] = 1;
2449 for (chain = scr->global_icons; chain != NULL; chain = chain->next) {
2450 btn = chain->aicon;
2451 if (btn->xindex == 0 && btn->yindex > 0 && btn->yindex < vcount)
2452 vmap[btn->yindex] = 1;
2453 else if (btn->yindex == 0 && btn->xindex > 0 && btn->xindex < hcount)
2454 hmap[btn->xindex] = 1;
2456 break;
2457 case C_NW:
2458 for (i = 0; i < dock->max_icons; i++) {
2459 btn = dock->icon_array[i];
2460 if (!btn)
2461 continue;
2463 if (btn->xindex == 0 && btn->yindex > 0 && btn->yindex < vcount)
2464 vmap[btn->yindex] = 1;
2465 else if (btn->yindex == 0 && btn->xindex < 0 && btn->xindex > -hcount)
2466 hmap[-btn->xindex] = 1;
2468 for (chain = scr->global_icons; chain != NULL; chain = chain->next) {
2469 btn = chain->aicon;
2470 if (btn->xindex == 0 && btn->yindex > 0 && btn->yindex < vcount)
2471 vmap[btn->yindex] = 1;
2472 else if (btn->yindex == 0 && btn->xindex < 0 && btn->xindex > -hcount)
2473 hmap[-btn->xindex] = 1;
2475 break;
2476 case C_SE:
2477 for (i = 0; i < dock->max_icons; i++) {
2478 btn = dock->icon_array[i];
2479 if (!btn)
2480 continue;
2482 if (btn->xindex == 0 && btn->yindex < 0 && btn->yindex > -vcount)
2483 vmap[-btn->yindex] = 1;
2484 else if (btn->yindex == 0 && btn->xindex > 0 && btn->xindex < hcount)
2485 hmap[btn->xindex] = 1;
2487 for (chain = scr->global_icons; chain != NULL; chain = chain->next) {
2488 btn = chain->aicon;
2489 if (btn->xindex == 0 && btn->yindex < 0 && btn->yindex > -vcount)
2490 vmap[-btn->yindex] = 1;
2491 else if (btn->yindex == 0 && btn->xindex > 0 && btn->xindex < hcount)
2492 hmap[btn->xindex] = 1;
2494 break;
2495 case C_SW:
2496 default:
2497 for (i = 0; i < dock->max_icons; i++) {
2498 btn = dock->icon_array[i];
2499 if (!btn)
2500 continue;
2502 if (btn->xindex == 0 && btn->yindex < 0 && btn->yindex > -vcount)
2503 vmap[-btn->yindex] = 1;
2504 else if (btn->yindex == 0 && btn->xindex < 0 && btn->xindex > -hcount)
2505 hmap[-btn->xindex] = 1;
2507 for (chain = scr->global_icons; chain != NULL; chain = chain->next) {
2508 btn = chain->aicon;
2509 if (btn->xindex == 0 && btn->yindex < 0 && btn->yindex > -vcount)
2510 vmap[-btn->yindex] = 1;
2511 else if (btn->yindex == 0 && btn->xindex < 0 && btn->xindex > -hcount)
2512 hmap[-btn->xindex] = 1;
2515 x = 0;
2516 y = 0;
2517 done = 0;
2518 /* search a vacant slot */
2519 for (i = 1; i < WMAX(vcount, hcount); i++) {
2520 if (i < vcount && vmap[i] == 0) {
2521 /* found a slot */
2522 x = 0;
2523 y = i;
2524 done = 1;
2525 break;
2526 } else if (i < hcount && hmap[i] == 0) {
2527 /* found a slot */
2528 x = i;
2529 y = 0;
2530 done = 1;
2531 break;
2534 wfree(vmap);
2535 wfree(hmap);
2536 /* If found a slot, translate and return */
2537 if (done) {
2538 if (corner == C_NW || corner == C_NE) {
2539 *y_pos = y;
2540 } else {
2541 *y_pos = -y;
2543 if (corner == C_NE || corner == C_SE) {
2544 *x_pos = x;
2545 } else {
2546 *x_pos = -x;
2548 return True;
2550 /* else, try to find a slot somewhere else */
2553 /* a map of mwidth x mwidth would be enough if we allowed icons to be
2554 * placed outside of screen */
2555 mwidth = (int)ceil(sqrt(dock->max_icons));
2557 /* In the worst case (the clip is in the corner of the screen),
2558 * the amount of icons that fit in the clip is smaller.
2559 * Double the map to get a safe value.
2561 mwidth += mwidth;
2563 r = (mwidth - 1) / 2;
2565 slot_map = wmalloc(mwidth * mwidth);
2567 #define XY2OFS(x,y) (WMAX(abs(x),abs(y)) > r) ? 0 : (((y)+r)*(mwidth)+(x)+r)
2569 /* mark used slots in the map. If the slot falls outside the map
2570 * (for example, when all icons are placed in line), ignore them. */
2571 for (i = 0; i < dock->max_icons; i++) {
2572 btn = dock->icon_array[i];
2573 if (btn)
2574 slot_map[XY2OFS(btn->xindex, btn->yindex)] = 1;
2576 for (chain = scr->global_icons; chain != NULL; chain = chain->next) {
2577 slot_map[XY2OFS(chain->aicon->xindex, chain->aicon->yindex)] = 1;
2579 /* Find closest slot from the center that is free by scanning the
2580 * map from the center to outward in circular passes.
2581 * This will not result in a neat layout, but will be optimal
2582 * in the sense that there will not be holes left.
2584 done = 0;
2585 for (i = 1; i <= r && !done; i++) {
2586 int tx, ty;
2588 /* top and bottom parts of the ring */
2589 for (x = -i; x <= i && !done; x++) {
2590 tx = dock->x_pos + x * ICON_SIZE;
2591 y = -i;
2592 ty = dock->y_pos + y * ICON_SIZE;
2593 if (slot_map[XY2OFS(x, y)] == 0 && onScreen(scr, tx, ty, sx, ex, sy, ey)) {
2594 *x_pos = x;
2595 *y_pos = y;
2596 done = 1;
2597 break;
2599 y = i;
2600 ty = dock->y_pos + y * ICON_SIZE;
2601 if (slot_map[XY2OFS(x, y)] == 0 && onScreen(scr, tx, ty, sx, ex, sy, ey)) {
2602 *x_pos = x;
2603 *y_pos = y;
2604 done = 1;
2605 break;
2608 /* left and right parts of the ring */
2609 for (y = -i + 1; y <= i - 1; y++) {
2610 ty = dock->y_pos + y * ICON_SIZE;
2611 x = -i;
2612 tx = dock->x_pos + x * ICON_SIZE;
2613 if (slot_map[XY2OFS(x, y)] == 0 && onScreen(scr, tx, ty, sx, ex, sy, ey)) {
2614 *x_pos = x;
2615 *y_pos = y;
2616 done = 1;
2617 break;
2619 x = i;
2620 tx = dock->x_pos + x * ICON_SIZE;
2621 if (slot_map[XY2OFS(x, y)] == 0 && onScreen(scr, tx, ty, sx, ex, sy, ey)) {
2622 *x_pos = x;
2623 *y_pos = y;
2624 done = 1;
2625 break;
2629 wfree(slot_map);
2630 #undef XY2OFS
2631 return done;
2634 static void moveDock(WDock *dock, int new_x, int new_y)
2636 WAppIcon *btn;
2637 int i;
2639 dock->x_pos = new_x;
2640 dock->y_pos = new_y;
2641 for (i = 0; i < dock->max_icons; i++) {
2642 btn = dock->icon_array[i];
2643 if (btn) {
2644 btn->x_pos = new_x + btn->xindex * ICON_SIZE;
2645 btn->y_pos = new_y + btn->yindex * ICON_SIZE;
2646 XMoveWindow(dpy, btn->icon->core->window, btn->x_pos, btn->y_pos);
2651 static void swapDock(WDock *dock)
2653 WScreen *scr = dock->screen_ptr;
2654 WAppIcon *btn;
2655 int x, i;
2657 if (dock->on_right_side) {
2658 x = dock->x_pos = scr->scr_width - ICON_SIZE - DOCK_EXTRA_SPACE;
2659 } else {
2660 x = dock->x_pos = DOCK_EXTRA_SPACE;
2663 for (i = 0; i < dock->max_icons; i++) {
2664 btn = dock->icon_array[i];
2665 if (btn) {
2666 btn->x_pos = x;
2667 XMoveWindow(dpy, btn->icon->core->window, btn->x_pos, btn->y_pos);
2671 wScreenUpdateUsableArea(scr);
2674 static pid_t execCommand(WAppIcon *btn, char *command, WSavedState *state)
2676 WScreen *scr = btn->icon->core->screen_ptr;
2677 pid_t pid;
2678 char **argv;
2679 int argc;
2680 char *cmdline;
2682 cmdline = ExpandOptions(scr, command);
2684 if (scr->flags.dnd_data_convertion_status || !cmdline) {
2685 if (cmdline)
2686 wfree(cmdline);
2687 if (state)
2688 wfree(state);
2689 return 0;
2692 wtokensplit(cmdline, &argv, &argc);
2694 if (!argc) {
2695 if (cmdline)
2696 wfree(cmdline);
2697 if (state)
2698 wfree(state);
2699 return 0;
2702 if ((pid = fork()) == 0) {
2703 char **args;
2704 int i;
2706 SetupEnvironment(scr);
2708 #ifdef HAVE_SETSID
2709 setsid();
2710 #endif
2712 args = malloc(sizeof(char *) * (argc + 1));
2713 if (!args)
2714 exit(111);
2715 for (i = 0; i < argc; i++) {
2716 args[i] = argv[i];
2718 args[argc] = NULL;
2719 execvp(argv[0], args);
2720 exit(111);
2722 wtokenfree(argv, argc);
2724 if (pid > 0) {
2725 if (!state) {
2726 state = wmalloc(sizeof(WSavedState));
2727 state->hidden = -1;
2728 state->miniaturized = -1;
2729 state->shaded = -1;
2730 if (btn->dock == scr->dock || btn->omnipresent)
2731 state->workspace = -1;
2732 else
2733 state->workspace = scr->current_workspace;
2735 wWindowAddSavedState(btn->wm_instance, btn->wm_class, cmdline, pid, state);
2736 wAddDeathHandler(pid, (WDeathHandler *) trackDeadProcess, btn->dock);
2737 } else if (state) {
2738 wfree(state);
2740 wfree(cmdline);
2741 return pid;
2744 void wDockHideIcons(WDock *dock)
2746 int i;
2748 if (dock == NULL)
2749 return;
2751 for (i = 1; i < dock->max_icons; i++) {
2752 if (dock->icon_array[i])
2753 XUnmapWindow(dpy, dock->icon_array[i]->icon->core->window);
2755 dock->mapped = 0;
2757 dockIconPaint(dock->icon_array[0]);
2760 void wDockShowIcons(WDock *dock)
2762 int i, newlevel;
2763 WAppIcon *btn;
2765 if (dock == NULL)
2766 return;
2768 btn = dock->icon_array[0];
2769 moveDock(dock, btn->x_pos, btn->y_pos);
2771 newlevel = dock->lowered ? WMNormalLevel : WMDockLevel;
2772 ChangeStackingLevel(btn->icon->core, newlevel);
2774 for (i = 1; i < dock->max_icons; i++) {
2775 if (dock->icon_array[i]) {
2776 MoveInStackListAbove(dock->icon_array[i]->icon->core, btn->icon->core);
2777 break;
2781 if (!dock->collapsed) {
2782 for (i = 1; i < dock->max_icons; i++) {
2783 if (dock->icon_array[i]) {
2784 XMapWindow(dpy, dock->icon_array[i]->icon->core->window);
2788 dock->mapped = 1;
2790 dockIconPaint(btn);
2793 void wDockLower(WDock *dock)
2795 int i;
2797 for (i = 0; i < dock->max_icons; i++) {
2798 if (dock->icon_array[i])
2799 wLowerFrame(dock->icon_array[i]->icon->core);
2803 void wDockRaise(WDock *dock)
2805 int i;
2807 for (i = dock->max_icons - 1; i >= 0; i--) {
2808 if (dock->icon_array[i])
2809 wRaiseFrame(dock->icon_array[i]->icon->core);
2813 void wDockRaiseLower(WDock *dock)
2815 if (!dock->icon_array[0]->icon->core->stacking->above
2816 || (dock->icon_array[0]->icon->core->stacking->window_level
2817 != dock->icon_array[0]->icon->core->stacking->above->stacking->window_level))
2818 wDockLower(dock);
2819 else
2820 wDockRaise(dock);
2823 void wDockFinishLaunch(WDock *dock, WAppIcon *icon)
2825 icon->launching = 0;
2826 icon->relaunching = 0;
2827 dockIconPaint(icon);
2830 WAppIcon *wDockFindIconForWindow(WDock *dock, Window window)
2832 WAppIcon *icon;
2833 int i;
2835 for (i = 0; i < dock->max_icons; i++) {
2836 icon = dock->icon_array[i];
2837 if (icon && icon->main_window == window)
2838 return icon;
2840 return NULL;
2843 void wDockTrackWindowLaunch(WDock *dock, Window window)
2845 WAppIcon *icon;
2846 char *wm_class, *wm_instance;
2847 int i;
2848 Bool firstPass = True;
2849 Bool found = False;
2850 char *command = NULL;
2852 if (!PropGetWMClass(window, &wm_class, &wm_instance) || (!wm_class && !wm_instance)) {
2853 return;
2856 command = GetCommandForWindow(window);
2857 retry:
2858 for (i = 0; i < dock->max_icons; i++) {
2859 icon = dock->icon_array[i];
2860 if (!icon)
2861 continue;
2863 /* app is already attached to icon */
2864 if (icon->main_window == window) {
2865 found = True;
2866 break;
2869 if ((icon->wm_instance || icon->wm_class)
2870 && (icon->launching || !icon->running)) {
2872 if (icon->wm_instance && wm_instance && strcmp(icon->wm_instance, wm_instance) != 0) {
2873 continue;
2875 if (icon->wm_class && wm_class && strcmp(icon->wm_class, wm_class) != 0) {
2876 continue;
2878 if (firstPass && command && strcmp(icon->command, command) != 0) {
2879 continue;
2882 if (!icon->relaunching) {
2883 WApplication *wapp;
2885 /* Possibly an application that was docked with dockit,
2886 * but the user did not update WMState to indicate that
2887 * it was docked by force */
2888 wapp = wApplicationOf(window);
2889 if (!wapp) {
2890 icon->forced_dock = 1;
2891 icon->running = 0;
2893 if (!icon->forced_dock) {
2894 icon->main_window = window;
2897 found = True;
2898 if (!wPreferences.no_animations && !icon->launching &&
2899 !dock->screen_ptr->flags.startup && !dock->collapsed) {
2900 WAppIcon *aicon;
2901 int x0, y0;
2903 icon->launching = 1;
2904 dockIconPaint(icon);
2906 aicon = wAppIconCreateForDock(dock->screen_ptr, NULL,
2907 wm_instance, wm_class, TILE_NORMAL);
2908 /* XXX: can: aicon->icon == NULL ? */
2909 PlaceIcon(dock->screen_ptr, &x0, &y0, wGetHeadForWindow(aicon->icon->owner));
2910 wAppIconMove(aicon, x0, y0);
2911 /* Should this always be lowered? -Dan */
2912 if (dock->lowered)
2913 wLowerFrame(aicon->icon->core);
2914 XMapWindow(dpy, aicon->icon->core->window);
2915 aicon->launching = 1;
2916 wAppIconPaint(aicon);
2917 SlideWindow(aicon->icon->core->window, x0, y0, icon->x_pos, icon->y_pos);
2918 XUnmapWindow(dpy, aicon->icon->core->window);
2919 wAppIconDestroy(aicon);
2921 wDockFinishLaunch(dock, icon);
2922 break;
2926 if (firstPass && !found) {
2927 firstPass = False;
2928 goto retry;
2931 if (command)
2932 wfree(command);
2934 if (wm_class)
2935 free(wm_class);
2936 if (wm_instance)
2937 free(wm_instance);
2940 void wClipUpdateForWorkspaceChange(WScreen *scr, int workspace)
2942 if (!wPreferences.flags.noclip) {
2943 scr->clip_icon->dock = scr->workspaces[workspace]->clip;
2944 if (scr->current_workspace != workspace) {
2945 WDock *old_clip = scr->workspaces[scr->current_workspace]->clip;
2946 WAppIconChain *chain = scr->global_icons;
2948 while (chain) {
2949 moveIconBetweenDocks(chain->aicon->dock,
2950 scr->workspaces[workspace]->clip,
2951 chain->aicon, chain->aicon->xindex, chain->aicon->yindex);
2952 if (scr->workspaces[workspace]->clip->collapsed)
2953 XUnmapWindow(dpy, chain->aicon->icon->core->window);
2954 chain = chain->next;
2957 wDockHideIcons(old_clip);
2958 if (old_clip->auto_raise_lower) {
2959 if (old_clip->auto_raise_magic) {
2960 WMDeleteTimerHandler(old_clip->auto_raise_magic);
2961 old_clip->auto_raise_magic = NULL;
2963 wDockLower(old_clip);
2965 if (old_clip->auto_collapse) {
2966 if (old_clip->auto_expand_magic) {
2967 WMDeleteTimerHandler(old_clip->auto_expand_magic);
2968 old_clip->auto_expand_magic = NULL;
2970 old_clip->collapsed = 1;
2972 wDockShowIcons(scr->workspaces[workspace]->clip);
2977 static void trackDeadProcess(pid_t pid, unsigned char status, WDock *dock)
2979 WAppIcon *icon;
2980 int i;
2982 for (i = 0; i < dock->max_icons; i++) {
2983 icon = dock->icon_array[i];
2984 if (!icon)
2985 continue;
2987 if (icon->launching && icon->pid == pid) {
2988 if (!icon->relaunching) {
2989 icon->running = 0;
2990 icon->main_window = None;
2992 wDockFinishLaunch(dock, icon);
2993 icon->pid = 0;
2994 if (status == 111) {
2995 char msg[PATH_MAX];
2996 char *cmd;
2998 #ifdef XDND
2999 if (icon->drop_launch)
3000 cmd = icon->dnd_command;
3001 else
3002 #endif
3003 if (icon->paste_launch)
3004 cmd = icon->paste_command;
3005 else
3006 cmd = icon->command;
3008 snprintf(msg, sizeof(msg), _("Could not execute command \"%s\""), cmd);
3010 wMessageDialog(dock->screen_ptr, _("Error"), msg, _("OK"), NULL, NULL);
3012 break;
3017 static void toggleLowered(WDock *dock)
3019 WAppIcon *tmp;
3020 int newlevel, i;
3022 /* lower/raise Dock */
3023 if (!dock->lowered) {
3024 newlevel = WMNormalLevel;
3025 dock->lowered = 1;
3026 } else {
3027 newlevel = WMDockLevel;
3028 dock->lowered = 0;
3031 for (i = 0; i < dock->max_icons; i++) {
3032 tmp = dock->icon_array[i];
3033 if (!tmp)
3034 continue;
3036 ChangeStackingLevel(tmp->icon->core, newlevel);
3037 if (dock->lowered)
3038 wLowerFrame(tmp->icon->core);
3041 if (dock->type == WM_DOCK)
3042 wScreenUpdateUsableArea(dock->screen_ptr);
3045 static void toggleCollapsed(WDock *dock)
3047 if (dock->collapsed) {
3048 dock->collapsed = 0;
3049 wDockShowIcons(dock);
3050 } else {
3051 dock->collapsed = 1;
3052 wDockHideIcons(dock);
3056 static void openDockMenu(WDock *dock, WAppIcon *aicon, XEvent *event)
3058 WScreen *scr = dock->screen_ptr;
3059 WObjDescriptor *desc;
3060 WMenuEntry *entry;
3061 WApplication *wapp = NULL;
3062 int index = 0;
3063 int x_pos;
3064 int n_selected;
3065 int appIsRunning = aicon->running && aicon->icon && aicon->icon->owner;
3067 if (dock->type == WM_DOCK) {
3068 /* keep on top */
3069 entry = dock->menu->entries[index];
3070 entry->flags.indicator_on = !dock->lowered;
3071 entry->clientdata = dock;
3072 dock->menu->flags.realized = 0;
3073 } else {
3074 /* clip options */
3075 if (scr->clip_options)
3076 updateClipOptionsMenu(scr->clip_options, dock);
3078 n_selected = numberOfSelectedIcons(dock);
3080 /* Rename Workspace */
3081 entry = dock->menu->entries[++index];
3082 if (aicon == scr->clip_icon) {
3083 entry->callback = renameCallback;
3084 entry->clientdata = dock;
3085 entry->flags.indicator = 0;
3086 entry->text = _("Rename Workspace");
3087 } else {
3088 entry->callback = omnipresentCallback;
3089 entry->clientdata = aicon;
3090 if (n_selected > 0) {
3091 entry->flags.indicator = 0;
3092 entry->text = _("Toggle Omnipresent");
3093 } else {
3094 entry->flags.indicator = 1;
3095 entry->flags.indicator_on = aicon->omnipresent;
3096 entry->flags.indicator_type = MI_CHECK;
3097 entry->text = _("Omnipresent");
3101 /* select/unselect icon */
3102 entry = dock->menu->entries[++index];
3103 entry->clientdata = aicon;
3104 entry->flags.indicator_on = aicon->icon->selected;
3105 wMenuSetEnabled(dock->menu, index, aicon != scr->clip_icon);
3107 /* select/unselect all icons */
3108 entry = dock->menu->entries[++index];
3109 entry->clientdata = aicon;
3110 if (n_selected > 0)
3111 entry->text = _("Unselect All Icons");
3112 else
3113 entry->text = _("Select All Icons");
3114 wMenuSetEnabled(dock->menu, index, dock->icon_count > 1);
3116 /* keep icon(s) */
3117 entry = dock->menu->entries[++index];
3118 entry->clientdata = aicon;
3119 if (n_selected > 1)
3120 entry->text = _("Keep Icons");
3121 else
3122 entry->text = _("Keep Icon");
3123 wMenuSetEnabled(dock->menu, index, dock->icon_count > 1);
3125 /* this is the workspace submenu part */
3126 entry = dock->menu->entries[++index];
3127 if (n_selected > 1)
3128 entry->text = _("Move Icons To");
3129 else
3130 entry->text = _("Move Icon To");
3131 if (scr->clip_submenu)
3132 updateWorkspaceMenu(scr->clip_submenu, aicon);
3133 wMenuSetEnabled(dock->menu, index, !aicon->omnipresent);
3135 /* remove icon(s) */
3136 entry = dock->menu->entries[++index];
3137 entry->clientdata = aicon;
3138 if (n_selected > 1)
3139 entry->text = _("Remove Icons");
3140 else
3141 entry->text = _("Remove Icon");
3142 wMenuSetEnabled(dock->menu, index, dock->icon_count > 1);
3144 /* attract icon(s) */
3145 entry = dock->menu->entries[++index];
3146 entry->clientdata = aicon;
3148 dock->menu->flags.realized = 0;
3149 wMenuRealize(dock->menu);
3152 if (aicon->icon->owner) {
3153 wapp = wApplicationOf(aicon->icon->owner->main_window);
3154 } else {
3155 wapp = NULL;
3158 /* launch */
3159 entry = dock->menu->entries[++index];
3160 entry->clientdata = aicon;
3161 wMenuSetEnabled(dock->menu, index, aicon->command != NULL);
3163 /* unhide here */
3164 entry = dock->menu->entries[++index];
3165 entry->clientdata = aicon;
3166 if (wapp && wapp->flags.hidden) {
3167 entry->text = _("Unhide Here");
3168 } else {
3169 entry->text = _("Bring Here");
3171 wMenuSetEnabled(dock->menu, index, appIsRunning);
3173 /* hide */
3174 entry = dock->menu->entries[++index];
3175 entry->clientdata = aicon;
3176 if (wapp && wapp->flags.hidden) {
3177 entry->text = _("Unhide");
3178 } else {
3179 entry->text = _("Hide");
3181 wMenuSetEnabled(dock->menu, index, appIsRunning);
3183 /* settings */
3184 entry = dock->menu->entries[++index];
3185 entry->clientdata = aicon;
3186 wMenuSetEnabled(dock->menu, index, !aicon->editing && !wPreferences.flags.noupdates);
3188 /* kill */
3189 entry = dock->menu->entries[++index];
3190 entry->clientdata = aicon;
3191 wMenuSetEnabled(dock->menu, index, appIsRunning);
3193 if (!dock->menu->flags.realized)
3194 wMenuRealize(dock->menu);
3196 if (dock->type == WM_CLIP) {
3197 /*x_pos = event->xbutton.x_root+2; */
3198 x_pos = event->xbutton.x_root - dock->menu->frame->core->width / 2 - 1;
3199 if (x_pos < 0) {
3200 x_pos = 0;
3201 } else if (x_pos + dock->menu->frame->core->width > scr->scr_width - 2) {
3202 x_pos = scr->scr_width - dock->menu->frame->core->width - 4;
3204 } else {
3205 x_pos = dock->on_right_side ? scr->scr_width - dock->menu->frame->core->width - 3 : 0;
3208 wMenuMapAt(dock->menu, x_pos, event->xbutton.y_root + 2, False);
3210 /* allow drag select */
3211 event->xany.send_event = True;
3212 desc = &dock->menu->menu->descriptor;
3213 (*desc->handle_mousedown) (desc, event);
3216 /******************************************************************/
3217 static void iconDblClick(WObjDescriptor *desc, XEvent *event)
3219 WAppIcon *btn = desc->parent;
3220 WDock *dock = btn->dock;
3221 WApplication *wapp = NULL;
3222 int unhideHere = 0;
3224 if (btn->icon->owner && !(event->xbutton.state & ControlMask)) {
3225 wapp = wApplicationOf(btn->icon->owner->main_window);
3227 assert(wapp != NULL);
3229 unhideHere = (event->xbutton.state & ShiftMask);
3231 /* go to the last workspace that the user worked on the app */
3232 if (wapp->last_workspace != dock->screen_ptr->current_workspace && !unhideHere) {
3233 wWorkspaceChange(dock->screen_ptr, wapp->last_workspace);
3236 wUnhideApplication(wapp, event->xbutton.button == Button2, unhideHere);
3238 if (event->xbutton.state & MOD_MASK) {
3239 wHideOtherApplications(btn->icon->owner);
3241 } else {
3242 if (event->xbutton.button == Button1) {
3244 if (event->xbutton.state & MOD_MASK) {
3245 /* raise/lower dock */
3246 toggleLowered(dock);
3247 } else if (btn == dock->screen_ptr->clip_icon) {
3248 if (getClipButton(event->xbutton.x, event->xbutton.y) == CLIP_IDLE)
3249 toggleCollapsed(dock);
3250 else
3251 handleClipChangeWorkspace(dock->screen_ptr, event);
3252 } else if (btn->command) {
3253 if (!btn->launching && (!btn->running || (event->xbutton.state & ControlMask)))
3254 launchDockedApplication(btn, False);
3260 static void handleDockMove(WDock *dock, WAppIcon *aicon, XEvent *event)
3262 WScreen *scr = dock->screen_ptr;
3263 int ofs_x = event->xbutton.x, ofs_y = event->xbutton.y;
3264 int x, y;
3265 XEvent ev;
3266 int grabbed = 0, swapped = 0, done;
3267 Pixmap ghost = None;
3268 int superfluous = wPreferences.superfluous; /* we catch it to avoid problems */
3270 if (XGrabPointer(dpy, aicon->icon->core->window, True, ButtonMotionMask
3271 | ButtonReleaseMask | ButtonPressMask, GrabModeAsync,
3272 GrabModeAsync, None, None, CurrentTime) != GrabSuccess) {
3273 wwarning("pointer grab failed for dock move");
3275 y = 0;
3276 for (x = 0; x < dock->max_icons; x++) {
3277 if (dock->icon_array[x] != NULL && dock->icon_array[x]->yindex > y)
3278 y = dock->icon_array[x]->yindex;
3280 y++;
3281 XResizeWindow(dpy, scr->dock_shadow, ICON_SIZE, ICON_SIZE * y);
3283 done = 0;
3284 while (!done) {
3285 WMMaskEvent(dpy, PointerMotionMask | ButtonReleaseMask | ButtonPressMask
3286 | ButtonMotionMask | ExposureMask | EnterWindowMask, &ev);
3287 switch (ev.type) {
3288 case Expose:
3289 WMHandleEvent(&ev);
3290 break;
3292 case EnterNotify:
3293 /* It means the cursor moved so fast that it entered
3294 * something else (if moving slowly, it would have
3295 * stayed in the dock that is being moved. Ignore such
3296 * "spurious" EnterNotifiy's */
3297 break;
3299 case MotionNotify:
3300 if (!grabbed) {
3301 if (abs(ofs_x - ev.xmotion.x) >= MOVE_THRESHOLD
3302 || abs(ofs_y - ev.xmotion.y) >= MOVE_THRESHOLD) {
3303 XChangeActivePointerGrab(dpy, ButtonMotionMask
3304 | ButtonReleaseMask | ButtonPressMask,
3305 wCursor[WCUR_MOVE], CurrentTime);
3306 grabbed = 1;
3308 break;
3310 if (dock->type == WM_CLIP) {
3311 x = ev.xmotion.x_root - ofs_x;
3312 y = ev.xmotion.y_root - ofs_y;
3313 wScreenKeepInside(scr, &x, &y, ICON_SIZE, ICON_SIZE);
3315 moveDock(dock, x, y);
3316 } else {
3317 /* move vertically if pointer is inside the dock */
3318 if ((dock->on_right_side && ev.xmotion.x_root >= dock->x_pos - ICON_SIZE)
3319 || (!dock->on_right_side && ev.xmotion.x_root <= dock->x_pos + ICON_SIZE * 2)) {
3321 x = ev.xmotion.x_root - ofs_x;
3322 y = ev.xmotion.y_root - ofs_y;
3323 wScreenKeepInside(scr, &x, &y, ICON_SIZE, ICON_SIZE);
3324 moveDock(dock, dock->x_pos, y);
3326 /* move horizontally to change sides */
3327 x = ev.xmotion.x_root - ofs_x;
3328 if (!dock->on_right_side) {
3330 /* is on left */
3332 if (ev.xmotion.x_root > dock->x_pos + ICON_SIZE * 2) {
3333 XMoveWindow(dpy, scr->dock_shadow, scr->scr_width - ICON_SIZE
3334 - DOCK_EXTRA_SPACE - 1, dock->y_pos);
3335 if (superfluous && ghost == None) {
3336 ghost = MakeGhostDock(dock, dock->x_pos,
3337 scr->scr_width - ICON_SIZE
3338 - DOCK_EXTRA_SPACE - 1, dock->y_pos);
3339 XSetWindowBackgroundPixmap(dpy, scr->dock_shadow, ghost);
3340 XClearWindow(dpy, scr->dock_shadow);
3342 XMapRaised(dpy, scr->dock_shadow);
3343 swapped = 1;
3344 } else {
3345 if (superfluous && ghost != None) {
3346 XFreePixmap(dpy, ghost);
3347 ghost = None;
3349 XUnmapWindow(dpy, scr->dock_shadow);
3350 swapped = 0;
3352 } else {
3353 /* is on right */
3354 if (ev.xmotion.x_root < dock->x_pos - ICON_SIZE) {
3355 XMoveWindow(dpy, scr->dock_shadow, DOCK_EXTRA_SPACE, dock->y_pos);
3356 if (superfluous && ghost == None) {
3357 ghost = MakeGhostDock(dock, dock->x_pos,
3358 DOCK_EXTRA_SPACE, dock->y_pos);
3359 XSetWindowBackgroundPixmap(dpy, scr->dock_shadow, ghost);
3360 XClearWindow(dpy, scr->dock_shadow);
3362 XMapRaised(dpy, scr->dock_shadow);
3363 swapped = -1;
3364 } else {
3365 XUnmapWindow(dpy, scr->dock_shadow);
3366 swapped = 0;
3367 if (superfluous && ghost != None) {
3368 XFreePixmap(dpy, ghost);
3369 ghost = None;
3374 break;
3376 case ButtonPress:
3377 break;
3379 case ButtonRelease:
3380 if (ev.xbutton.button != event->xbutton.button)
3381 break;
3382 XUngrabPointer(dpy, CurrentTime);
3383 XUnmapWindow(dpy, scr->dock_shadow);
3384 XResizeWindow(dpy, scr->dock_shadow, ICON_SIZE, ICON_SIZE);
3385 if (dock->type == WM_DOCK) {
3386 if (swapped != 0) {
3387 if (swapped > 0)
3388 dock->on_right_side = 1;
3389 else
3390 dock->on_right_side = 0;
3391 swapDock(dock);
3392 wArrangeIcons(scr, False);
3395 done = 1;
3396 break;
3399 if (superfluous) {
3400 if (ghost != None)
3401 XFreePixmap(dpy, ghost);
3402 XSetWindowBackground(dpy, scr->dock_shadow, scr->white_pixel);
3406 static Bool handleIconMove(WDock *dock, WAppIcon *aicon, XEvent *event)
3408 WScreen *scr = dock->screen_ptr;
3409 Window wins[2];
3410 WIcon *icon = aicon->icon;
3411 WDock *dock2 = NULL, *last_dock = dock, *clip = NULL;
3412 int ondock, grabbed = 0, change_dock = 0, collapsed = 0;
3413 XEvent ev;
3414 int x = aicon->x_pos, y = aicon->y_pos;
3415 int ofs_x = event->xbutton.x, ofs_y = event->xbutton.y;
3416 int shad_x = x, shad_y = y;
3417 int ix = aicon->xindex, iy = aicon->yindex;
3418 int tmp;
3419 Pixmap ghost = None;
3420 Bool docked;
3421 int superfluous = wPreferences.superfluous; /* we catch it to avoid problems */
3422 int omnipresent = aicon->omnipresent; /* this must be cached!!! */
3423 Bool hasMoved = False;
3425 if (wPreferences.flags.noupdates)
3426 return hasMoved;
3428 if (XGrabPointer(dpy, icon->core->window, True, ButtonMotionMask
3429 | ButtonReleaseMask | ButtonPressMask, GrabModeAsync,
3430 GrabModeAsync, None, None, CurrentTime) != GrabSuccess) {
3433 if (!(event->xbutton.state & MOD_MASK))
3434 wRaiseFrame(icon->core);
3436 if (!wPreferences.flags.noclip)
3437 clip = scr->workspaces[scr->current_workspace]->clip;
3439 if (dock == scr->dock && !wPreferences.flags.noclip)
3440 dock2 = clip;
3441 else if (dock != scr->dock && !wPreferences.flags.nodock)
3442 dock2 = scr->dock;
3444 wins[0] = icon->core->window;
3445 wins[1] = scr->dock_shadow;
3446 XRestackWindows(dpy, wins, 2);
3447 XMoveResizeWindow(dpy, scr->dock_shadow, aicon->x_pos, aicon->y_pos, ICON_SIZE, ICON_SIZE);
3448 if (superfluous) {
3449 if (icon->pixmap != None)
3450 ghost = MakeGhostIcon(scr, icon->pixmap);
3451 else
3452 ghost = MakeGhostIcon(scr, icon->core->window);
3454 XSetWindowBackgroundPixmap(dpy, scr->dock_shadow, ghost);
3455 XClearWindow(dpy, scr->dock_shadow);
3457 XMapWindow(dpy, scr->dock_shadow);
3459 ondock = 1;
3461 while (1) {
3462 XMaskEvent(dpy, PointerMotionMask | ButtonReleaseMask | ButtonPressMask
3463 | ButtonMotionMask | ExposureMask, &ev);
3464 switch (ev.type) {
3465 case Expose:
3466 WMHandleEvent(&ev);
3467 break;
3469 case MotionNotify:
3470 hasMoved = True;
3471 if (!grabbed) {
3472 if (abs(ofs_x - ev.xmotion.x) >= MOVE_THRESHOLD
3473 || abs(ofs_y - ev.xmotion.y) >= MOVE_THRESHOLD) {
3474 XChangeActivePointerGrab(dpy, ButtonMotionMask
3475 | ButtonReleaseMask | ButtonPressMask,
3476 wCursor[WCUR_MOVE], CurrentTime);
3477 grabbed = 1;
3478 } else {
3479 break;
3483 if (omnipresent) {
3484 int i;
3485 for (i = 0; i < scr->workspace_count; i++) {
3486 if (i == scr->current_workspace)
3487 continue;
3488 wDockShowIcons(scr->workspaces[i]->clip);
3492 x = ev.xmotion.x_root - ofs_x;
3493 y = ev.xmotion.y_root - ofs_y;
3494 tmp = wDockSnapIcon(dock, aicon, x, y, &ix, &iy, True);
3495 if (tmp && dock2) {
3496 change_dock = 0;
3497 if (last_dock != dock && collapsed) {
3498 last_dock->collapsed = 1;
3499 wDockHideIcons(last_dock);
3500 collapsed = 0;
3502 if (!collapsed && (collapsed = dock->collapsed)) {
3503 dock->collapsed = 0;
3504 wDockShowIcons(dock);
3506 if (dock->auto_raise_lower)
3507 wDockRaise(dock);
3508 last_dock = dock;
3509 } else if (dock2) {
3510 tmp = wDockSnapIcon(dock2, aicon, x, y, &ix, &iy, False);
3511 if (tmp) {
3512 change_dock = 1;
3513 if (last_dock != dock2 && collapsed) {
3514 last_dock->collapsed = 1;
3515 wDockHideIcons(last_dock);
3516 collapsed = 0;
3518 if (!collapsed && (collapsed = dock2->collapsed)) {
3519 dock2->collapsed = 0;
3520 wDockShowIcons(dock2);
3522 if (dock2->auto_raise_lower)
3523 wDockRaise(dock2);
3524 last_dock = dock2;
3527 if (aicon->launching || aicon->lock || (aicon->running && !(ev.xmotion.state & MOD_MASK))
3528 || (!aicon->running && tmp)) {
3529 shad_x = last_dock->x_pos + ix * wPreferences.icon_size;
3530 shad_y = last_dock->y_pos + iy * wPreferences.icon_size;
3532 XMoveWindow(dpy, scr->dock_shadow, shad_x, shad_y);
3534 if (!ondock) {
3535 XMapWindow(dpy, scr->dock_shadow);
3537 ondock = 1;
3538 } else {
3539 if (ondock) {
3540 XUnmapWindow(dpy, scr->dock_shadow);
3542 ondock = 0;
3544 XMoveWindow(dpy, icon->core->window, x, y);
3545 break;
3547 case ButtonPress:
3548 break;
3550 case ButtonRelease:
3551 if (ev.xbutton.button != event->xbutton.button)
3552 break;
3553 XUngrabPointer(dpy, CurrentTime);
3554 if (ondock) {
3555 SlideWindow(icon->core->window, x, y, shad_x, shad_y);
3556 XUnmapWindow(dpy, scr->dock_shadow);
3557 if (!change_dock) {
3558 reattachIcon(dock, aicon, ix, iy);
3559 if (clip && dock != clip && clip->auto_raise_lower)
3560 wDockLower(clip);
3561 } else {
3562 docked = moveIconBetweenDocks(dock, dock2, aicon, ix, iy);
3563 if (!docked) {
3564 /* Slide it back if dock rejected it */
3565 SlideWindow(icon->core->window, x, y, aicon->x_pos, aicon->y_pos);
3566 reattachIcon(dock, aicon, aicon->xindex, aicon->yindex);
3568 if (last_dock->type == WM_CLIP && last_dock->auto_collapse) {
3569 collapsed = 0;
3572 } else {
3573 aicon->x_pos = x;
3574 aicon->y_pos = y;
3575 if (superfluous) {
3576 if (!aicon->running && !wPreferences.no_animations) {
3577 /* We need to deselect it, even if is deselected in
3578 * wDockDetach(), because else DoKaboom() will fail.
3580 if (aicon->icon->selected)
3581 wIconSelect(aicon->icon);
3583 DoKaboom(scr, aicon->icon->core->window, x, y);
3586 if (clip && clip->auto_raise_lower)
3587 wDockLower(clip);
3588 wDockDetach(dock, aicon);
3590 if (collapsed) {
3591 last_dock->collapsed = 1;
3592 wDockHideIcons(last_dock);
3593 collapsed = 0;
3595 if (superfluous) {
3596 if (ghost != None)
3597 XFreePixmap(dpy, ghost);
3598 XSetWindowBackground(dpy, scr->dock_shadow, scr->white_pixel);
3600 if (omnipresent) {
3601 int i;
3602 for (i = 0; i < scr->workspace_count; i++) {
3603 if (i == scr->current_workspace)
3604 continue;
3605 wDockHideIcons(scr->workspaces[i]->clip);
3608 return hasMoved;;
3611 return False; /* never reached */
3614 static int getClipButton(int px, int py)
3616 int pt = (CLIP_BUTTON_SIZE + 2) * ICON_SIZE / 64;
3618 if (px < 0 || py < 0 || px >= ICON_SIZE || py >= ICON_SIZE)
3619 return CLIP_IDLE;
3621 if (py <= pt - ((int)ICON_SIZE - 1 - px))
3622 return CLIP_FORWARD;
3623 else if (px <= pt - ((int)ICON_SIZE - 1 - py))
3624 return CLIP_REWIND;
3626 return CLIP_IDLE;
3629 static void handleClipChangeWorkspace(WScreen *scr, XEvent *event)
3631 XEvent ev;
3632 int done, direction, new_ws;
3633 int new_dir;
3634 WDock *clip = scr->clip_icon->dock;
3636 direction = getClipButton(event->xbutton.x, event->xbutton.y);
3638 clip->lclip_button_pushed = direction == CLIP_REWIND;
3639 clip->rclip_button_pushed = direction == CLIP_FORWARD;
3641 wClipIconPaint(scr->clip_icon);
3642 done = 0;
3643 while (!done) {
3644 WMMaskEvent(dpy, ExposureMask | ButtonMotionMask | ButtonReleaseMask | ButtonPressMask, &ev);
3645 switch (ev.type) {
3646 case Expose:
3647 WMHandleEvent(&ev);
3648 break;
3650 case MotionNotify:
3651 new_dir = getClipButton(ev.xmotion.x, ev.xmotion.y);
3652 if (new_dir != direction) {
3653 direction = new_dir;
3654 clip->lclip_button_pushed = direction == CLIP_REWIND;
3655 clip->rclip_button_pushed = direction == CLIP_FORWARD;
3656 wClipIconPaint(scr->clip_icon);
3658 break;
3660 case ButtonPress:
3661 break;
3663 case ButtonRelease:
3664 if (ev.xbutton.button == event->xbutton.button)
3665 done = 1;
3669 clip->lclip_button_pushed = 0;
3670 clip->rclip_button_pushed = 0;
3672 new_ws = wPreferences.ws_advance || (event->xbutton.state & ControlMask);
3674 if (direction == CLIP_FORWARD) {
3675 if (scr->current_workspace < scr->workspace_count - 1)
3676 wWorkspaceChange(scr, scr->current_workspace + 1);
3677 else if (new_ws && scr->current_workspace < MAX_WORKSPACES - 1)
3678 wWorkspaceChange(scr, scr->current_workspace + 1);
3679 else if (wPreferences.ws_cycle)
3680 wWorkspaceChange(scr, 0);
3681 } else if (direction == CLIP_REWIND) {
3682 if (scr->current_workspace > 0)
3683 wWorkspaceChange(scr, scr->current_workspace - 1);
3684 else if (scr->current_workspace == 0 && wPreferences.ws_cycle)
3685 wWorkspaceChange(scr, scr->workspace_count - 1);
3688 wClipIconPaint(scr->clip_icon);
3691 static void iconMouseDown(WObjDescriptor *desc, XEvent *event)
3693 WAppIcon *aicon = desc->parent;
3694 WDock *dock = aicon->dock;
3695 WScreen *scr = aicon->icon->core->screen_ptr;
3697 if (aicon->editing || WCHECK_STATE(WSTATE_MODAL))
3698 return;
3700 scr->last_dock = dock;
3702 if (dock->menu->flags.mapped)
3703 wMenuUnmap(dock->menu);
3705 if (IsDoubleClick(scr, event)) {
3706 /* double-click was not in the main clip icon */
3707 if (dock->type != WM_CLIP || aicon->xindex != 0 || aicon->yindex != 0
3708 || getClipButton(event->xbutton.x, event->xbutton.y) == CLIP_IDLE) {
3709 iconDblClick(desc, event);
3710 return;
3714 if (event->xbutton.button == Button1) {
3715 if (event->xbutton.state & MOD_MASK)
3716 wDockLower(dock);
3717 else
3718 wDockRaise(dock);
3720 if ((event->xbutton.state & ShiftMask) && aicon != scr->clip_icon && dock->type != WM_DOCK) {
3721 wIconSelect(aicon->icon);
3722 return;
3725 if (aicon->yindex == 0 && aicon->xindex == 0) {
3726 if (getClipButton(event->xbutton.x, event->xbutton.y) != CLIP_IDLE
3727 && dock->type == WM_CLIP)
3728 handleClipChangeWorkspace(scr, event);
3729 else
3730 handleDockMove(dock, aicon, event);
3731 } else {
3732 Bool hasMoved = handleIconMove(dock, aicon, event);
3733 if (wPreferences.single_click && !hasMoved)
3734 iconDblClick(desc, event);
3736 } else if (event->xbutton.button == Button2 && dock->type == WM_CLIP && aicon == scr->clip_icon) {
3737 if (!scr->clip_ws_menu) {
3738 scr->clip_ws_menu = wWorkspaceMenuMake(scr, False);
3740 if (scr->clip_ws_menu) {
3741 WMenu *wsMenu = scr->clip_ws_menu;
3742 int xpos;
3744 wWorkspaceMenuUpdate(scr, wsMenu);
3746 xpos = event->xbutton.x_root - wsMenu->frame->core->width / 2 - 1;
3747 if (xpos < 0) {
3748 xpos = 0;
3749 } else if (xpos + wsMenu->frame->core->width > scr->scr_width - 2) {
3750 xpos = scr->scr_width - wsMenu->frame->core->width - 4;
3752 wMenuMapAt(wsMenu, xpos, event->xbutton.y_root + 2, False);
3754 desc = &wsMenu->menu->descriptor;
3755 event->xany.send_event = True;
3756 (*desc->handle_mousedown) (desc, event);
3758 } else if (event->xbutton.button == Button2 && dock->type == WM_CLIP &&
3759 (event->xbutton.state & ShiftMask) && aicon != scr->clip_icon) {
3760 wClipMakeIconOmnipresent(aicon, !aicon->omnipresent);
3761 } else if (event->xbutton.button == Button3) {
3762 if (event->xbutton.send_event &&
3763 XGrabPointer(dpy, aicon->icon->core->window, True, ButtonMotionMask
3764 | ButtonReleaseMask | ButtonPressMask, GrabModeAsync,
3765 GrabModeAsync, None, None, CurrentTime) != GrabSuccess) {
3766 wwarning("pointer grab failed for dockicon menu");
3767 return;
3770 openDockMenu(dock, aicon, event);
3771 } else if (event->xbutton.button == Button2) {
3772 WAppIcon *btn = desc->parent;
3774 if (!btn->launching && (!btn->running || (event->xbutton.state & ControlMask))) {
3775 launchDockedApplication(btn, True);
3780 static void clipEnterNotify(WObjDescriptor *desc, XEvent *event)
3782 WAppIcon *btn = (WAppIcon *) desc->parent;
3783 WDock *dock;
3784 WScreen *scr;
3786 assert(event->type == EnterNotify);
3788 if (desc->parent_type != WCLASS_DOCK_ICON)
3789 return;
3791 scr = btn->icon->core->screen_ptr;
3792 if (!btn->omnipresent)
3793 dock = btn->dock;
3794 else
3795 dock = scr->workspaces[scr->current_workspace]->clip;
3797 if (!dock || dock->type != WM_CLIP)
3798 return;
3800 /* The auto raise/lower code */
3801 if (dock->auto_lower_magic) {
3802 WMDeleteTimerHandler(dock->auto_lower_magic);
3803 dock->auto_lower_magic = NULL;
3805 if (dock->auto_raise_lower && !dock->auto_raise_magic) {
3806 dock->auto_raise_magic = WMAddTimerHandler(AUTO_RAISE_DELAY, clipAutoRaise, (void *)dock);
3809 /* The auto expand/collapse code */
3810 if (dock->auto_collapse_magic) {
3811 WMDeleteTimerHandler(dock->auto_collapse_magic);
3812 dock->auto_collapse_magic = NULL;
3814 if (dock->auto_collapse && !dock->auto_expand_magic) {
3815 dock->auto_expand_magic = WMAddTimerHandler(AUTO_EXPAND_DELAY, clipAutoExpand, (void *)dock);
3819 static void clipLeave(WDock *dock)
3821 XEvent event;
3822 WObjDescriptor *desc = NULL;
3824 if (!dock || dock->type != WM_CLIP)
3825 return;
3827 if (XCheckTypedEvent(dpy, EnterNotify, &event) != False) {
3828 if (XFindContext(dpy, event.xcrossing.window, wWinContext,
3829 (XPointer *) & desc) != XCNOENT
3830 && desc && desc->parent_type == WCLASS_DOCK_ICON
3831 && ((WAppIcon *) desc->parent)->dock && ((WAppIcon *) desc->parent)->dock->type == WM_CLIP) {
3832 /* We didn't left the Clip yet */
3833 XPutBackEvent(dpy, &event);
3834 return;
3837 XPutBackEvent(dpy, &event);
3838 } else {
3839 /* We entered a withdrawn window, so we're still in Clip */
3840 return;
3843 if (dock->auto_raise_magic) {
3844 WMDeleteTimerHandler(dock->auto_raise_magic);
3845 dock->auto_raise_magic = NULL;
3847 if (dock->auto_raise_lower && !dock->auto_lower_magic) {
3848 dock->auto_lower_magic = WMAddTimerHandler(AUTO_LOWER_DELAY, clipAutoLower, (void *)dock);
3851 if (dock->auto_expand_magic) {
3852 WMDeleteTimerHandler(dock->auto_expand_magic);
3853 dock->auto_expand_magic = NULL;
3855 if (dock->auto_collapse && !dock->auto_collapse_magic) {
3856 dock->auto_collapse_magic = WMAddTimerHandler(AUTO_COLLAPSE_DELAY, clipAutoCollapse, (void *)dock);
3860 static void clipLeaveNotify(WObjDescriptor *desc, XEvent *event)
3862 WAppIcon *btn = (WAppIcon *) desc->parent;
3864 assert(event->type == LeaveNotify);
3866 if (desc->parent_type != WCLASS_DOCK_ICON)
3867 return;
3869 clipLeave(btn->dock);
3872 static void clipAutoCollapse(void *cdata)
3874 WDock *dock = (WDock *) cdata;
3876 if (dock->type != WM_CLIP)
3877 return;
3879 if (dock->auto_collapse) {
3880 dock->collapsed = 1;
3881 wDockHideIcons(dock);
3883 dock->auto_collapse_magic = NULL;
3886 static void clipAutoExpand(void *cdata)
3888 WDock *dock = (WDock *) cdata;
3890 if (dock->type != WM_CLIP)
3891 return;
3893 if (dock->auto_collapse) {
3894 dock->collapsed = 0;
3895 wDockShowIcons(dock);
3897 dock->auto_expand_magic = NULL;
3900 static void clipAutoLower(void *cdata)
3902 WDock *dock = (WDock *) cdata;
3904 if (dock->type != WM_CLIP)
3905 return;
3907 if (dock->auto_raise_lower)
3908 wDockLower(dock);
3910 dock->auto_lower_magic = NULL;
3913 static void clipAutoRaise(void *cdata)
3915 WDock *dock = (WDock *) cdata;
3917 if (dock->type != WM_CLIP)
3918 return;
3920 if (dock->auto_raise_lower)
3921 wDockRaise(dock);
3923 dock->auto_raise_magic = NULL;
3926 static Bool iconCanBeOmnipresent(WAppIcon *aicon)
3928 WScreen *scr = aicon->icon->core->screen_ptr;
3929 WDock *clip;
3930 WAppIcon *btn;
3931 int i, j;
3933 for (i = 0; i < scr->workspace_count; i++) {
3934 clip = scr->workspaces[i]->clip;
3936 if (clip == aicon->dock)
3937 continue;
3939 if (clip->icon_count + scr->global_icon_count >= clip->max_icons)
3940 return False; /* Clip is full in some workspace */
3942 for (j = 0; j < clip->max_icons; j++) {
3943 btn = clip->icon_array[j];
3944 if (btn && btn->xindex == aicon->xindex && btn->yindex == aicon->yindex)
3945 return False;
3949 return True;
3952 int wClipMakeIconOmnipresent(WAppIcon *aicon, int omnipresent)
3954 WScreen *scr = aicon->icon->core->screen_ptr;
3955 WAppIconChain *new_entry, *tmp, *tmp1;
3956 int status = WO_SUCCESS;
3958 if ((scr->dock && aicon->dock == scr->dock) || aicon == scr->clip_icon) {
3959 return WO_NOT_APPLICABLE;
3962 if (aicon->omnipresent == omnipresent)
3963 return WO_SUCCESS;
3965 if (omnipresent) {
3966 if (iconCanBeOmnipresent(aicon)) {
3967 aicon->omnipresent = 1;
3968 new_entry = wmalloc(sizeof(WAppIconChain));
3969 new_entry->aicon = aicon;
3970 new_entry->next = scr->global_icons;
3971 scr->global_icons = new_entry;
3972 scr->global_icon_count++;
3973 } else {
3974 aicon->omnipresent = 0;
3975 status = WO_FAILED;
3977 } else {
3978 aicon->omnipresent = 0;
3979 if (aicon == scr->global_icons->aicon) {
3980 tmp = scr->global_icons->next;
3981 wfree(scr->global_icons);
3982 scr->global_icons = tmp;
3983 scr->global_icon_count--;
3984 } else {
3985 tmp = scr->global_icons;
3986 while (tmp->next) {
3987 if (tmp->next->aicon == aicon) {
3988 tmp1 = tmp->next->next;
3989 wfree(tmp->next);
3990 tmp->next = tmp1;
3991 scr->global_icon_count--;
3992 break;
3994 tmp = tmp->next;
3999 wAppIconPaint(aicon);
4001 return status;