Prevent removal of dock icons due to resolution changes
[wmaker-crm.git] / src / dock.c
blob96d8c61d00977e7c10d694b1d85fb6bedd015370
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 "dialog.h"
47 #include "funcs.h"
48 #include "properties.h"
49 #include "menu.h"
50 #include "client.h"
51 #include "defaults.h"
52 #include "workspace.h"
53 #include "framewin.h"
54 #include "superfluous.h"
55 #include "xinerama.h"
57 /**** Local variables ****/
58 #define CLIP_REWIND 1
59 #define CLIP_IDLE 0
60 #define CLIP_FORWARD 2
62 /**** Global variables ****/
64 /* in dockedapp.c */
65 extern void DestroyDockAppSettingsPanel();
66 extern void ShowDockAppSettingsPanel(WAppIcon * aicon);
67 extern Cursor wCursor[WCUR_LAST];
68 extern WPreferences wPreferences;
69 extern XContext wWinContext;
70 extern void appIconMouseDown(WObjDescriptor *desc, XEvent *event);
72 #define MOD_MASK wPreferences.modifier_mask
73 #define ICON_SIZE wPreferences.icon_size
75 /***** Local variables ****/
77 static WMPropList *dCommand = NULL;
78 static WMPropList *dPasteCommand = NULL;
79 #ifdef XDND /* XXX was OFFIX */
80 static WMPropList *dDropCommand = NULL;
81 #endif
82 static WMPropList *dAutoLaunch, *dLock;
83 static WMPropList *dName, *dForced, *dBuggyApplication, *dYes, *dNo;
84 static WMPropList *dHost, *dDock, *dClip;
85 static WMPropList *dAutoAttractIcons;
87 static WMPropList *dPosition, *dApplications, *dLowered, *dCollapsed;
89 static WMPropList *dAutoCollapse, *dAutoRaiseLower, *dOmnipresent;
91 static void dockIconPaint(WAppIcon * btn);
93 static void iconMouseDown(WObjDescriptor * desc, XEvent * event);
95 static pid_t execCommand(WAppIcon * btn, char *command, WSavedState * state);
97 static void trackDeadProcess(pid_t pid, unsigned char status, WDock * dock);
99 static int getClipButton(int px, int py);
101 static void toggleLowered(WDock * dock);
103 static void toggleCollapsed(WDock * dock);
105 static void clipIconExpose(WObjDescriptor * desc, XEvent * event);
107 static void clipLeave(WDock * dock);
109 static void handleClipChangeWorkspace(WScreen * scr, XEvent * event);
111 Bool moveIconBetweenDocks(WDock * src, WDock * dest, WAppIcon * icon, int x, int y);
113 static void clipEnterNotify(WObjDescriptor * desc, XEvent * event);
114 static void clipLeaveNotify(WObjDescriptor * desc, XEvent * event);
115 static void clipAutoCollapse(void *cdata);
116 static void clipAutoExpand(void *cdata);
117 static void launchDockedApplication(WAppIcon * btn, Bool withSelection);
119 static void clipAutoLower(void *cdata);
120 static void clipAutoRaise(void *cdata);
122 static void showClipBalloon(WDock * dock, int workspace);
124 static void make_keys(void)
126 if (dCommand != NULL)
127 return;
129 dCommand = WMRetainPropList(WMCreatePLString("Command"));
130 dPasteCommand = WMRetainPropList(WMCreatePLString("PasteCommand"));
131 #ifdef XDND
132 dDropCommand = WMRetainPropList(WMCreatePLString("DropCommand"));
133 #endif
134 dLock = WMRetainPropList(WMCreatePLString("Lock"));
135 dAutoLaunch = WMRetainPropList(WMCreatePLString("AutoLaunch"));
136 dName = WMRetainPropList(WMCreatePLString("Name"));
137 dForced = WMRetainPropList(WMCreatePLString("Forced"));
138 dBuggyApplication = WMRetainPropList(WMCreatePLString("BuggyApplication"));
139 dYes = WMRetainPropList(WMCreatePLString("Yes"));
140 dNo = WMRetainPropList(WMCreatePLString("No"));
141 dHost = WMRetainPropList(WMCreatePLString("Host"));
143 dPosition = WMCreatePLString("Position");
144 dApplications = WMCreatePLString("Applications");
145 dLowered = WMCreatePLString("Lowered");
146 dCollapsed = WMCreatePLString("Collapsed");
147 dAutoCollapse = WMCreatePLString("AutoCollapse");
148 dAutoRaiseLower = WMCreatePLString("AutoRaiseLower");
149 dAutoAttractIcons = WMCreatePLString("AutoAttractIcons");
151 dOmnipresent = WMCreatePLString("Omnipresent");
153 dDock = WMCreatePLString("Dock");
154 dClip = WMCreatePLString("Clip");
157 static void renameCallback(WMenu * menu, WMenuEntry * entry)
159 WDock *dock = entry->clientdata;
160 char buffer[128];
161 int wspace;
162 char *name;
164 assert(entry->clientdata != NULL);
166 wspace = dock->screen_ptr->current_workspace;
168 name = wstrdup(dock->screen_ptr->workspaces[wspace]->name);
170 snprintf(buffer, sizeof(buffer), _("Type the name for workspace %i:"), wspace + 1);
171 if (wInputDialog(dock->screen_ptr, _("Rename Workspace"), buffer, &name)) {
172 wWorkspaceRename(dock->screen_ptr, wspace, name);
174 if (name) {
175 wfree(name);
179 static void toggleLoweredCallback(WMenu * menu, WMenuEntry * entry)
181 assert(entry->clientdata != NULL);
183 toggleLowered(entry->clientdata);
185 entry->flags.indicator_on = !(((WDock *) entry->clientdata)->lowered);
187 wMenuPaint(menu);
190 static int matchWindow(const void *item, const void *cdata)
192 return (((WFakeGroupLeader *) item)->leader == (Window) cdata);
195 static void killCallback(WMenu * menu, WMenuEntry * entry)
197 WScreen *scr = menu->menu->screen_ptr;
198 WAppIcon *icon;
199 WFakeGroupLeader *fPtr;
200 char *buffer, *shortname, **argv;
201 char *basename(const char *shortname);
202 int argc;
204 if (!WCHECK_STATE(WSTATE_NORMAL))
205 return;
207 assert(entry->clientdata != NULL);
209 icon = (WAppIcon *) entry->clientdata;
211 icon->editing = 1;
213 WCHANGE_STATE(WSTATE_MODAL);
215 /* strip away dir names */
216 shortname = basename(icon->command);
217 /* separate out command options */
218 wtokensplit(shortname, &argv, &argc);
220 buffer = wstrconcat(argv[0],
221 _(" will be forcibly closed.\n"
222 "Any unsaved changes will be lost.\n" "Please confirm."));
224 if (icon->icon && icon->icon->owner) {
225 fPtr = icon->icon->owner->fake_group;
226 } else {
227 /* is this really necessary? can we kill a non-running dock icon? */
228 Window win = icon->main_window;
229 int index;
231 index = WMFindInArray(scr->fakeGroupLeaders, matchWindow, (void *)win);
232 if (index != WANotFound)
233 fPtr = WMGetFromArray(scr->fakeGroupLeaders, index);
234 else
235 fPtr = NULL;
238 if (wPreferences.dont_confirm_kill
239 || wMessageDialog(menu->frame->screen_ptr, _("Kill Application"),
240 buffer, _("Yes"), _("No"), NULL) == WAPRDefault) {
241 if (fPtr != NULL) {
242 WWindow *wwin, *twin;
244 wwin = scr->focused_window;
245 while (wwin) {
246 twin = wwin->prev;
247 if (wwin->fake_group == fPtr) {
248 wClientKill(wwin);
250 wwin = twin;
252 } else if (icon->icon && icon->icon->owner) {
253 wClientKill(icon->icon->owner);
257 wfree(buffer);
258 wtokenfree(argv, argc);
260 icon->editing = 0;
262 WCHANGE_STATE(WSTATE_NORMAL);
265 /* TODO: replace this function with a member of the dock struct */
266 static int numberOfSelectedIcons(WDock * dock)
268 WAppIcon *aicon;
269 int i, n;
271 n = 0;
272 for (i = 1; i < dock->max_icons; i++) {
273 aicon = dock->icon_array[i];
274 if (aicon && aicon->icon->selected) {
275 n++;
279 return n;
282 static WMArray *getSelected(WDock * dock)
284 WMArray *ret = WMCreateArray(8);
285 WAppIcon *btn;
286 int i;
288 for (i = 1; i < dock->max_icons; i++) {
289 btn = dock->icon_array[i];
290 if (btn && btn->icon->selected) {
291 WMAddToArray(ret, btn);
295 return ret;
298 static void paintClipButtons(WAppIcon * clipIcon, Bool lpushed, Bool rpushed)
300 Window win = clipIcon->icon->core->window;
301 WScreen *scr = clipIcon->icon->core->screen_ptr;
302 XPoint p[4];
303 int pt = CLIP_BUTTON_SIZE * ICON_SIZE / 64;
304 int tp = ICON_SIZE - pt;
305 int as = pt - 15; /* 15 = 5+5+5 */
306 GC gc = scr->draw_gc; /* maybe use WMColorGC() instead here? */
307 WMColor *color;
308 #ifdef GRADIENT_CLIP_ARROW
309 Bool collapsed = clipIcon->dock->collapsed;
310 #endif
312 /*if (!clipIcon->dock->collapsed)
313 color = scr->clip_title_color[CLIP_NORMAL];
314 else
315 color = scr->clip_title_color[CLIP_COLLAPSED]; */
316 color = scr->clip_title_color[CLIP_NORMAL];
318 XSetForeground(dpy, gc, WMColorPixel(color));
320 if (rpushed) {
321 p[0].x = tp + 1;
322 p[0].y = 1;
323 p[1].x = ICON_SIZE - 2;
324 p[1].y = 1;
325 p[2].x = ICON_SIZE - 2;
326 p[2].y = pt - 1;
327 } else if (lpushed) {
328 p[0].x = 1;
329 p[0].y = tp;
330 p[1].x = pt;
331 p[1].y = ICON_SIZE - 2;
332 p[2].x = 1;
333 p[2].y = ICON_SIZE - 2;
335 if (lpushed || rpushed) {
336 XSetForeground(dpy, scr->draw_gc, scr->white_pixel);
337 XFillPolygon(dpy, win, scr->draw_gc, p, 3, Convex, CoordModeOrigin);
338 XSetForeground(dpy, scr->draw_gc, scr->black_pixel);
340 #ifdef GRADIENT_CLIP_ARROW
341 if (!collapsed) {
342 XSetFillStyle(dpy, scr->copy_gc, FillTiled);
343 XSetTile(dpy, scr->copy_gc, scr->clip_arrow_gradient);
344 XSetClipMask(dpy, scr->copy_gc, None);
345 gc = scr->copy_gc;
347 #endif /* GRADIENT_CLIP_ARROW */
349 /* top right arrow */
350 p[0].x = p[3].x = ICON_SIZE - 5 - as;
351 p[0].y = p[3].y = 5;
352 p[1].x = ICON_SIZE - 6;
353 p[1].y = 5;
354 p[2].x = ICON_SIZE - 6;
355 p[2].y = 4 + as;
356 if (rpushed) {
357 XFillPolygon(dpy, win, scr->draw_gc, p, 3, Convex, CoordModeOrigin);
358 XDrawLines(dpy, win, scr->draw_gc, p, 4, CoordModeOrigin);
359 } else {
360 #ifdef GRADIENT_CLIP_ARROW
361 if (!collapsed)
362 XSetTSOrigin(dpy, gc, ICON_SIZE - 6 - as, 5);
363 #endif
364 XFillPolygon(dpy, win, gc, p, 3, Convex, CoordModeOrigin);
365 XDrawLines(dpy, win, gc, p, 4, CoordModeOrigin);
368 /* bottom left arrow */
369 p[0].x = p[3].x = 5;
370 p[0].y = p[3].y = ICON_SIZE - 5 - as;
371 p[1].x = 5;
372 p[1].y = ICON_SIZE - 6;
373 p[2].x = 4 + as;
374 p[2].y = ICON_SIZE - 6;
375 if (lpushed) {
376 XFillPolygon(dpy, win, scr->draw_gc, p, 3, Convex, CoordModeOrigin);
377 XDrawLines(dpy, win, scr->draw_gc, p, 4, CoordModeOrigin);
378 } else {
379 #ifdef GRADIENT_CLIP_ARROW
380 if (!collapsed)
381 XSetTSOrigin(dpy, gc, 5, ICON_SIZE - 6 - as);
382 #endif
383 XFillPolygon(dpy, win, gc, p, 3, Convex, CoordModeOrigin);
384 XDrawLines(dpy, win, gc, p, 4, CoordModeOrigin);
386 #ifdef GRADIENT_CLIP_ARROW
387 if (!collapsed)
388 XSetFillStyle(dpy, scr->copy_gc, FillSolid);
389 #endif
392 RImage *wClipMakeTile(WScreen * scr, RImage * normalTile)
394 RImage *tile = RCloneImage(normalTile);
395 RColor black;
396 RColor dark;
397 RColor light;
398 int pt, tp;
399 int as;
401 pt = CLIP_BUTTON_SIZE * wPreferences.icon_size / 64;
402 tp = wPreferences.icon_size - 1 - pt;
403 as = pt - 15;
405 black.alpha = 255;
406 black.red = black.green = black.blue = 0;
408 dark.alpha = 0;
409 dark.red = dark.green = dark.blue = 60;
411 light.alpha = 0;
412 light.red = light.green = light.blue = 80;
414 /* top right */
415 ROperateLine(tile, RSubtractOperation, tp, 0, wPreferences.icon_size - 2, pt - 1, &dark);
416 RDrawLine(tile, tp - 1, 0, wPreferences.icon_size - 1, pt + 1, &black);
417 ROperateLine(tile, RAddOperation, tp, 2, wPreferences.icon_size - 3, pt, &light);
419 /* arrow bevel */
420 ROperateLine(tile, RSubtractOperation, ICON_SIZE - 7 - as, 4, ICON_SIZE - 5, 4, &dark);
421 ROperateLine(tile, RSubtractOperation, ICON_SIZE - 6 - as, 5, ICON_SIZE - 5, 6 + as, &dark);
422 ROperateLine(tile, RAddOperation, ICON_SIZE - 5, 4, ICON_SIZE - 5, 6 + as, &light);
424 /* bottom left */
425 ROperateLine(tile, RAddOperation, 2, tp + 2, pt - 2, wPreferences.icon_size - 3, &dark);
426 RDrawLine(tile, 0, tp - 1, pt + 1, wPreferences.icon_size - 1, &black);
427 ROperateLine(tile, RSubtractOperation, 0, tp - 2, pt + 1, wPreferences.icon_size - 2, &light);
429 /* arrow bevel */
430 ROperateLine(tile, RSubtractOperation, 4, ICON_SIZE - 7 - as, 4, ICON_SIZE - 5, &dark);
431 ROperateLine(tile, RSubtractOperation, 5, ICON_SIZE - 6 - as, 6 + as, ICON_SIZE - 5, &dark);
432 ROperateLine(tile, RAddOperation, 4, ICON_SIZE - 5, 6 + as, ICON_SIZE - 5, &light);
434 return tile;
437 static void omnipresentCallback(WMenu * menu, WMenuEntry * entry)
439 WAppIcon *clickedIcon = entry->clientdata;
440 WAppIcon *aicon;
441 WDock *dock;
442 WMArray *selectedIcons;
443 WMArrayIterator iter;
444 int failed;
446 assert(entry->clientdata != NULL);
448 dock = clickedIcon->dock;
450 selectedIcons = getSelected(dock);
452 if (!WMGetArrayItemCount(selectedIcons))
453 WMAddToArray(selectedIcons, clickedIcon);
455 failed = 0;
456 WM_ITERATE_ARRAY(selectedIcons, aicon, iter) {
457 if (wClipMakeIconOmnipresent(aicon, !aicon->omnipresent) == WO_FAILED)
458 failed++;
459 else if (aicon->icon->selected)
460 wIconSelect(aicon->icon);
462 WMFreeArray(selectedIcons);
464 if (failed > 1) {
465 wMessageDialog(dock->screen_ptr, _("Warning"),
466 _("Some icons cannot be made omnipresent. "
467 "Please make sure that no other icon is "
468 "docked in the same positions on the other "
469 "workspaces and the Clip is not full in "
470 "some workspace."), _("OK"), NULL, NULL);
471 } else if (failed == 1) {
472 wMessageDialog(dock->screen_ptr, _("Warning"),
473 _("Icon cannot be made omnipresent. "
474 "Please make sure that no other icon is "
475 "docked in the same position on the other "
476 "workspaces and the Clip is not full in "
477 "some workspace."), _("OK"), NULL, NULL);
481 static void removeIconsCallback(WMenu * menu, WMenuEntry * entry)
483 WAppIcon *clickedIcon = (WAppIcon *) entry->clientdata;
484 WDock *dock;
485 WAppIcon *aicon;
486 WMArray *selectedIcons;
487 int keepit;
488 WMArrayIterator it;
490 assert(clickedIcon != NULL);
492 dock = clickedIcon->dock;
494 selectedIcons = getSelected(dock);
496 if (WMGetArrayItemCount(selectedIcons)) {
497 if (wMessageDialog(dock->screen_ptr, _("Workspace Clip"),
498 _("All selected icons will be removed!"),
499 _("OK"), _("Cancel"), NULL) != WAPRDefault) {
500 WMFreeArray(selectedIcons);
501 return;
503 } else {
504 if (clickedIcon->xindex == 0 && clickedIcon->yindex == 0) {
505 WMFreeArray(selectedIcons);
506 return;
508 WMAddToArray(selectedIcons, clickedIcon);
511 WM_ITERATE_ARRAY(selectedIcons, aicon, it) {
512 keepit = aicon->running && wApplicationOf(aicon->main_window);
513 wDockDetach(dock, aicon);
514 if (keepit) {
515 /* XXX: can: aicon->icon == NULL ? */
516 PlaceIcon(dock->screen_ptr, &aicon->x_pos, &aicon->y_pos,
517 wGetHeadForWindow(aicon->icon->owner));
518 XMoveWindow(dpy, aicon->icon->core->window, aicon->x_pos, aicon->y_pos);
519 if (!dock->mapped || dock->collapsed)
520 XMapWindow(dpy, aicon->icon->core->window);
523 WMFreeArray(selectedIcons);
525 if (wPreferences.auto_arrange_icons)
526 wArrangeIcons(dock->screen_ptr, True);
529 static void keepIconsCallback(WMenu * menu, WMenuEntry * entry)
531 WAppIcon *clickedIcon = (WAppIcon *) entry->clientdata;
532 WDock *dock;
533 WAppIcon *aicon;
534 WMArray *selectedIcons;
535 WMArrayIterator it;
537 assert(clickedIcon != NULL);
538 dock = clickedIcon->dock;
540 selectedIcons = getSelected(dock);
542 if (!WMGetArrayItemCount(selectedIcons)
543 && clickedIcon != dock->screen_ptr->clip_icon) {
544 char *command = NULL;
546 if (!clickedIcon->command && !clickedIcon->editing) {
547 clickedIcon->editing = 1;
548 if (wInputDialog(dock->screen_ptr, _("Keep Icon"),
549 _("Type the command used to launch the application"), &command)) {
550 if (command && (command[0] == 0 || (command[0] == '-' && command[1] == 0))) {
551 wfree(command);
552 command = NULL;
554 clickedIcon->command = command;
555 clickedIcon->editing = 0;
556 } else {
557 clickedIcon->editing = 0;
558 if (command)
559 wfree(command);
560 WMFreeArray(selectedIcons);
561 return;
565 WMAddToArray(selectedIcons, clickedIcon);
568 WM_ITERATE_ARRAY(selectedIcons, aicon, it) {
569 if (aicon->icon->selected)
570 wIconSelect(aicon->icon);
571 if (aicon && aicon->attracted && aicon->command) {
572 aicon->attracted = 0;
573 if (aicon->icon->shadowed) {
574 aicon->icon->shadowed = 0;
575 aicon->icon->force_paint = 1;
576 wAppIconPaint(aicon);
579 wAppIconSave(aicon);
581 WMFreeArray(selectedIcons);
584 static void toggleAutoAttractCallback(WMenu * menu, WMenuEntry * entry)
586 WDock *dock = (WDock *) entry->clientdata;
588 assert(entry->clientdata != NULL);
590 dock->attract_icons = !dock->attract_icons;
591 /*if (!dock->attract_icons)
592 dock->keep_attracted = 0; */
594 entry->flags.indicator_on = dock->attract_icons;
596 wMenuPaint(menu);
599 static void selectCallback(WMenu * menu, WMenuEntry * entry)
601 WAppIcon *icon = (WAppIcon *) entry->clientdata;
603 assert(icon != NULL);
605 wIconSelect(icon->icon);
607 wMenuPaint(menu);
610 static void colectIconsCallback(WMenu * menu, WMenuEntry * entry)
612 WAppIcon *clickedIcon = (WAppIcon *) entry->clientdata;
613 WDock *clip;
614 WAppIcon *aicon;
615 int x, y, x_pos, y_pos;
617 assert(entry->clientdata != NULL);
618 clip = clickedIcon->dock;
620 aicon = clip->screen_ptr->app_icon_list;
622 while (aicon) {
623 if (!aicon->docked && wDockFindFreeSlot(clip, &x, &y)) {
624 x_pos = clip->x_pos + x * ICON_SIZE;
625 y_pos = clip->y_pos + y * ICON_SIZE;
626 if (aicon->x_pos != x_pos || aicon->y_pos != y_pos) {
627 #ifdef ANIMATIONS
628 if (wPreferences.no_animations) {
629 XMoveWindow(dpy, aicon->icon->core->window, x_pos, y_pos);
630 } else {
631 SlideWindow(aicon->icon->core->window,
632 aicon->x_pos, aicon->y_pos, x_pos, y_pos);
634 #else
635 XMoveWindow(dpy, aicon->icon->core->window, x_pos, y_pos);
636 #endif /* ANIMATIONS */
638 aicon->attracted = 1;
639 if (!aicon->icon->shadowed) {
640 aicon->icon->shadowed = 1;
641 aicon->icon->force_paint = 1;
642 /* We don't do an wAppIconPaint() here because it's in
643 * wDockAttachIcon(). -Dan
646 wDockAttachIcon(clip, aicon, x, y);
647 if (clip->collapsed || !clip->mapped)
648 XUnmapWindow(dpy, aicon->icon->core->window);
650 aicon = aicon->next;
654 static void selectIconsCallback(WMenu * menu, WMenuEntry * entry)
656 WAppIcon *clickedIcon = (WAppIcon *) entry->clientdata;
657 WDock *dock;
658 WMArray *selectedIcons;
659 WMArrayIterator iter;
660 WAppIcon *btn;
661 int i;
663 assert(clickedIcon != NULL);
664 dock = clickedIcon->dock;
666 selectedIcons = getSelected(dock);
668 if (!WMGetArrayItemCount(selectedIcons)) {
669 for (i = 1; i < dock->max_icons; i++) {
670 btn = dock->icon_array[i];
671 if (btn && !btn->icon->selected) {
672 wIconSelect(btn->icon);
675 } else {
676 WM_ITERATE_ARRAY(selectedIcons, btn, iter) {
677 wIconSelect(btn->icon);
680 WMFreeArray(selectedIcons);
682 wMenuPaint(menu);
685 static void toggleCollapsedCallback(WMenu * menu, WMenuEntry * entry)
687 assert(entry->clientdata != NULL);
689 toggleCollapsed(entry->clientdata);
691 entry->flags.indicator_on = ((WDock *) entry->clientdata)->collapsed;
693 wMenuPaint(menu);
696 static void toggleAutoCollapseCallback(WMenu * menu, WMenuEntry * entry)
698 WDock *dock;
699 assert(entry->clientdata != NULL);
701 dock = (WDock *) entry->clientdata;
703 dock->auto_collapse = !dock->auto_collapse;
705 entry->flags.indicator_on = ((WDock *) entry->clientdata)->auto_collapse;
707 wMenuPaint(menu);
710 static void toggleAutoRaiseLowerCallback(WMenu * menu, WMenuEntry * entry)
712 WDock *dock;
713 assert(entry->clientdata != NULL);
715 dock = (WDock *) entry->clientdata;
717 dock->auto_raise_lower = !dock->auto_raise_lower;
719 entry->flags.indicator_on = ((WDock *) entry->clientdata)->auto_raise_lower;
721 wMenuPaint(menu);
724 static void launchCallback(WMenu * menu, WMenuEntry * entry)
726 WAppIcon *btn = (WAppIcon *) entry->clientdata;
728 launchDockedApplication(btn, False);
731 static void settingsCallback(WMenu * menu, WMenuEntry * entry)
733 WAppIcon *btn = (WAppIcon *) entry->clientdata;
735 if (btn->editing)
736 return;
737 ShowDockAppSettingsPanel(btn);
740 static void hideCallback(WMenu * menu, WMenuEntry * entry)
742 WApplication *wapp;
743 WAppIcon *btn = (WAppIcon *) entry->clientdata;
745 wapp = wApplicationOf(btn->icon->owner->main_window);
747 if (wapp->flags.hidden) {
748 wWorkspaceChange(btn->icon->core->screen_ptr, wapp->last_workspace);
749 wUnhideApplication(wapp, False, False);
750 } else {
751 wHideApplication(wapp);
755 static void unhideHereCallback(WMenu * menu, WMenuEntry * entry)
757 WApplication *wapp;
758 WAppIcon *btn = (WAppIcon *) entry->clientdata;
760 wapp = wApplicationOf(btn->icon->owner->main_window);
762 wUnhideApplication(wapp, False, True);
765 WAppIcon *mainIconCreate(WScreen * scr, int type)
767 WAppIcon *btn;
768 int x_pos;
770 if (type == WM_CLIP) {
771 if (scr->clip_icon)
772 return scr->clip_icon;
773 btn = wAppIconCreateForDock(scr, NULL, "Logo", "WMClip", TILE_CLIP);
774 btn->icon->core->descriptor.handle_expose = clipIconExpose;
775 btn->icon->core->descriptor.handle_enternotify = clipEnterNotify;
776 btn->icon->core->descriptor.handle_leavenotify = clipLeaveNotify;
777 /*x_pos = scr->scr_width - ICON_SIZE*2 - DOCK_EXTRA_SPACE; */
778 x_pos = 0;
779 } else {
780 btn = wAppIconCreateForDock(scr, NULL, "Logo", "WMDock", TILE_NORMAL);
781 x_pos = scr->scr_width - ICON_SIZE - DOCK_EXTRA_SPACE;
784 btn->xindex = 0;
785 btn->yindex = 0;
787 btn->icon->core->descriptor.handle_mousedown = iconMouseDown;
788 btn->icon->core->descriptor.parent_type = WCLASS_DOCK_ICON;
789 btn->icon->core->descriptor.parent = btn;
790 /*ChangeStackingLevel(btn->icon->core, WMDockLevel); */
791 XMapWindow(dpy, btn->icon->core->window);
792 btn->x_pos = x_pos;
793 btn->y_pos = 0;
794 btn->docked = 1;
795 if (type == WM_CLIP)
796 scr->clip_icon = btn;
798 return btn;
801 static void switchWSCommand(WMenu * menu, WMenuEntry * entry)
803 WAppIcon *btn, *icon = (WAppIcon *) entry->clientdata;
804 WScreen *scr = icon->icon->core->screen_ptr;
805 WDock *src, *dest;
806 WMArray *selectedIcons;
807 int x, y;
809 if (entry->order == scr->current_workspace)
810 return;
811 src = icon->dock;
812 dest = scr->workspaces[entry->order]->clip;
814 selectedIcons = getSelected(src);
816 if (WMGetArrayItemCount(selectedIcons)) {
817 WMArrayIterator iter;
819 WM_ITERATE_ARRAY(selectedIcons, btn, iter) {
820 if (wDockFindFreeSlot(dest, &x, &y)) {
821 moveIconBetweenDocks(src, dest, btn, x, y);
822 XUnmapWindow(dpy, btn->icon->core->window);
825 } else if (icon != scr->clip_icon) {
826 if (wDockFindFreeSlot(dest, &x, &y)) {
827 moveIconBetweenDocks(src, dest, icon, x, y);
828 XUnmapWindow(dpy, icon->icon->core->window);
831 WMFreeArray(selectedIcons);
834 static void launchDockedApplication(WAppIcon * btn, Bool withSelection)
836 WScreen *scr = btn->icon->core->screen_ptr;
838 if (!btn->launching &&
839 ((!withSelection && btn->command != NULL) || (withSelection && btn->paste_command != NULL))) {
840 if (!btn->forced_dock) {
841 btn->relaunching = btn->running;
842 btn->running = 1;
844 if (btn->wm_instance || btn->wm_class) {
845 WWindowAttributes attr;
846 memset(&attr, 0, sizeof(WWindowAttributes));
847 wDefaultFillAttributes(scr, btn->wm_instance, btn->wm_class, &attr, NULL, True);
849 if (!attr.no_appicon && !btn->buggy_app)
850 btn->launching = 1;
851 else
852 btn->running = 0;
854 btn->drop_launch = 0;
855 btn->paste_launch = withSelection;
856 scr->last_dock = btn->dock;
857 btn->pid = execCommand(btn, (withSelection ? btn->paste_command : btn->command), NULL);
858 if (btn->pid > 0) {
859 if (btn->buggy_app) {
860 /* give feedback that the app was launched */
861 btn->launching = 1;
862 dockIconPaint(btn);
863 btn->launching = 0;
864 WMAddTimerHandler(200, (WMCallback *) dockIconPaint, btn);
865 } else {
866 dockIconPaint(btn);
868 } else {
869 wwarning(_("could not launch application %s"), btn->command);
870 btn->launching = 0;
871 if (!btn->relaunching) {
872 btn->running = 0;
878 static void updateWorkspaceMenu(WMenu * menu, WAppIcon * icon)
880 WScreen *scr = menu->frame->screen_ptr;
881 char title[MAX_WORKSPACENAME_WIDTH + 1];
882 int i;
884 if (!menu || !icon)
885 return;
887 for (i = 0; i < scr->workspace_count; i++) {
888 if (i < menu->entry_no) {
889 if (strcmp(menu->entries[i]->text, scr->workspaces[i]->name) != 0) {
890 wfree(menu->entries[i]->text);
891 strcpy(title, scr->workspaces[i]->name);
892 menu->entries[i]->text = wstrdup(title);
893 menu->flags.realized = 0;
895 menu->entries[i]->clientdata = (void *)icon;
896 } else {
897 strcpy(title, scr->workspaces[i]->name);
899 wMenuAddCallback(menu, title, switchWSCommand, (void *)icon);
901 menu->flags.realized = 0;
903 if (i == scr->current_workspace) {
904 wMenuSetEnabled(menu, i, False);
905 } else {
906 wMenuSetEnabled(menu, i, True);
910 if (!menu->flags.realized)
911 wMenuRealize(menu);
914 static WMenu *makeWorkspaceMenu(WScreen * scr)
916 WMenu *menu;
918 menu = wMenuCreate(scr, NULL, False);
919 if (!menu)
920 wwarning(_("could not create workspace submenu for Clip menu"));
922 wMenuAddCallback(menu, "", switchWSCommand, (void *)scr->clip_icon);
924 menu->flags.realized = 0;
925 wMenuRealize(menu);
927 return menu;
930 static void updateClipOptionsMenu(WMenu * menu, WDock * dock)
932 WMenuEntry *entry;
933 int index = 0;
935 if (!menu || !dock)
936 return;
938 /* keep on top */
939 entry = menu->entries[index];
940 entry->flags.indicator_on = !dock->lowered;
941 entry->clientdata = dock;
943 /* collapsed */
944 entry = menu->entries[++index];
945 entry->flags.indicator_on = dock->collapsed;
946 entry->clientdata = dock;
948 /* auto-collapse */
949 entry = menu->entries[++index];
950 entry->flags.indicator_on = dock->auto_collapse;
951 entry->clientdata = dock;
953 /* auto-raise/lower */
954 entry = menu->entries[++index];
955 entry->flags.indicator_on = dock->auto_raise_lower;
956 entry->clientdata = dock;
957 wMenuSetEnabled(menu, index, dock->lowered);
959 /* attract icons */
960 entry = menu->entries[++index];
961 entry->flags.indicator_on = dock->attract_icons;
962 entry->clientdata = dock;
964 menu->flags.realized = 0;
965 wMenuRealize(menu);
968 static WMenu *makeClipOptionsMenu(WScreen * scr)
970 WMenu *menu;
971 WMenuEntry *entry;
973 menu = wMenuCreate(scr, NULL, False);
974 if (!menu) {
975 wwarning(_("could not create options submenu for Clip menu"));
976 return NULL;
979 entry = wMenuAddCallback(menu, _("Keep on Top"), toggleLoweredCallback, NULL);
980 entry->flags.indicator = 1;
981 entry->flags.indicator_on = 1;
982 entry->flags.indicator_type = MI_CHECK;
984 entry = wMenuAddCallback(menu, _("Collapsed"), toggleCollapsedCallback, NULL);
985 entry->flags.indicator = 1;
986 entry->flags.indicator_on = 1;
987 entry->flags.indicator_type = MI_CHECK;
989 entry = wMenuAddCallback(menu, _("Autocollapse"), toggleAutoCollapseCallback, NULL);
990 entry->flags.indicator = 1;
991 entry->flags.indicator_on = 1;
992 entry->flags.indicator_type = MI_CHECK;
994 entry = wMenuAddCallback(menu, _("Autoraise"), toggleAutoRaiseLowerCallback, NULL);
995 entry->flags.indicator = 1;
996 entry->flags.indicator_on = 1;
997 entry->flags.indicator_type = MI_CHECK;
999 entry = wMenuAddCallback(menu, _("Autoattract Icons"), toggleAutoAttractCallback, NULL);
1000 entry->flags.indicator = 1;
1001 entry->flags.indicator_on = 1;
1002 entry->flags.indicator_type = MI_CHECK;
1004 menu->flags.realized = 0;
1005 wMenuRealize(menu);
1007 return menu;
1010 static WMenu *dockMenuCreate(WScreen * scr, int type)
1012 WMenu *menu;
1013 WMenuEntry *entry;
1015 if (type == WM_CLIP && scr->clip_menu)
1016 return scr->clip_menu;
1018 menu = wMenuCreate(scr, NULL, False);
1019 if (type != WM_CLIP) {
1020 entry = wMenuAddCallback(menu, _("Keep on Top"), toggleLoweredCallback, NULL);
1021 entry->flags.indicator = 1;
1022 entry->flags.indicator_on = 1;
1023 entry->flags.indicator_type = MI_CHECK;
1024 } else {
1025 entry = wMenuAddCallback(menu, _("Clip Options"), NULL, NULL);
1026 scr->clip_options = makeClipOptionsMenu(scr);
1027 if (scr->clip_options)
1028 wMenuEntrySetCascade(menu, entry, scr->clip_options);
1030 entry = wMenuAddCallback(menu, _("Rename Workspace"), renameCallback, NULL);
1031 wfree(entry->text);
1032 entry->text = _("Rename Workspace");
1034 entry = wMenuAddCallback(menu, _("Selected"), selectCallback, NULL);
1035 entry->flags.indicator = 1;
1036 entry->flags.indicator_on = 1;
1037 entry->flags.indicator_type = MI_CHECK;
1039 entry = wMenuAddCallback(menu, _("Select All Icons"), selectIconsCallback, NULL);
1040 wfree(entry->text);
1041 entry->text = _("Select All Icons");
1043 entry = wMenuAddCallback(menu, _("Keep Icon"), keepIconsCallback, NULL);
1044 wfree(entry->text);
1045 entry->text = _("Keep Icon");
1047 entry = wMenuAddCallback(menu, _("Move Icon To"), NULL, NULL);
1048 wfree(entry->text);
1049 entry->text = _("Move Icon To");
1050 scr->clip_submenu = makeWorkspaceMenu(scr);
1051 if (scr->clip_submenu)
1052 wMenuEntrySetCascade(menu, entry, scr->clip_submenu);
1054 entry = wMenuAddCallback(menu, _("Remove Icon"), removeIconsCallback, NULL);
1055 wfree(entry->text);
1056 entry->text = _("Remove Icon");
1058 wMenuAddCallback(menu, _("Attract Icons"), colectIconsCallback, NULL);
1061 wMenuAddCallback(menu, _("Launch"), launchCallback, NULL);
1063 wMenuAddCallback(menu, _("Unhide Here"), unhideHereCallback, NULL);
1065 entry = wMenuAddCallback(menu, _("Hide"), hideCallback, NULL);
1066 wfree(entry->text);
1067 entry->text = _("Hide");
1069 wMenuAddCallback(menu, _("Settings..."), settingsCallback, NULL);
1071 wMenuAddCallback(menu, _("Kill"), killCallback, NULL);
1073 if (type == WM_CLIP)
1074 scr->clip_menu = menu;
1076 return menu;
1079 WDock *wDockCreate(WScreen * scr, int type)
1081 WDock *dock;
1082 WAppIcon *btn;
1084 make_keys();
1086 dock = wmalloc(sizeof(WDock));
1087 memset(dock, 0, sizeof(WDock));
1089 dock->max_icons = DOCK_MAX_ICONS;
1091 dock->icon_array = wmalloc(sizeof(WAppIcon *) * dock->max_icons);
1092 memset(dock->icon_array, 0, sizeof(WAppIcon *) * dock->max_icons);
1094 btn = mainIconCreate(scr, type);
1096 btn->dock = dock;
1098 dock->x_pos = btn->x_pos;
1099 dock->y_pos = btn->y_pos;
1100 dock->screen_ptr = scr;
1101 dock->type = type;
1102 dock->icon_count = 1;
1103 dock->on_right_side = 1;
1104 dock->collapsed = 0;
1105 dock->auto_collapse = 0;
1106 dock->auto_collapse_magic = NULL;
1107 dock->auto_raise_lower = 0;
1108 dock->auto_lower_magic = NULL;
1109 dock->auto_raise_magic = NULL;
1110 dock->attract_icons = 0;
1111 dock->lowered = 1;
1112 dock->icon_array[0] = btn;
1113 wRaiseFrame(btn->icon->core);
1114 XMoveWindow(dpy, btn->icon->core->window, btn->x_pos, btn->y_pos);
1116 /* create dock menu */
1117 dock->menu = dockMenuCreate(scr, type);
1119 return dock;
1122 void wDockDestroy(WDock * dock)
1124 int i;
1125 WAppIcon *aicon;
1127 for (i = (dock->type == WM_CLIP) ? 1 : 0; i < dock->max_icons; i++) {
1128 aicon = dock->icon_array[i];
1129 if (aicon) {
1130 int keepit = aicon->running && wApplicationOf(aicon->main_window);
1131 wDockDetach(dock, aicon);
1132 if (keepit) {
1133 /* XXX: can: aicon->icon == NULL ? */
1134 PlaceIcon(dock->screen_ptr, &aicon->x_pos, &aicon->y_pos,
1135 wGetHeadForWindow(aicon->icon->owner));
1136 XMoveWindow(dpy, aicon->icon->core->window, aicon->x_pos, aicon->y_pos);
1137 if (!dock->mapped || dock->collapsed)
1138 XMapWindow(dpy, aicon->icon->core->window);
1142 if (wPreferences.auto_arrange_icons)
1143 wArrangeIcons(dock->screen_ptr, True);
1144 wfree(dock->icon_array);
1145 if (dock->menu && dock->type != WM_CLIP)
1146 wMenuDestroy(dock->menu, True);
1147 if (dock->screen_ptr->last_dock == dock)
1148 dock->screen_ptr->last_dock = NULL;
1149 wfree(dock);
1152 void wClipIconPaint(WAppIcon * aicon)
1154 WScreen *scr = aicon->icon->core->screen_ptr;
1155 WWorkspace *workspace = scr->workspaces[scr->current_workspace];
1156 WMColor *color;
1157 Window win = aicon->icon->core->window;
1158 int length, nlength;
1159 char *ws_name, ws_number[10];
1160 int ty, tx;
1162 wIconPaint(aicon->icon);
1164 length = strlen(workspace->name);
1165 ws_name = wmalloc(length + 1);
1166 snprintf(ws_name, length + 1, "%s", workspace->name);
1167 snprintf(ws_number, sizeof(ws_number), "%i", scr->current_workspace + 1);
1168 nlength = strlen(ws_number);
1170 if (!workspace->clip->collapsed)
1171 color = scr->clip_title_color[CLIP_NORMAL];
1172 else
1173 color = scr->clip_title_color[CLIP_COLLAPSED];
1175 ty = ICON_SIZE - WMFontHeight(scr->clip_title_font) - 3;
1177 tx = CLIP_BUTTON_SIZE * ICON_SIZE / 64;
1179 if(wPreferences.show_clip_title)
1180 WMDrawString(scr->wmscreen, win, color, scr->clip_title_font, tx, ty, ws_name, length);
1181 /*WMDrawString(scr->wmscreen, win, color, scr->clip_title_font, 4,
1182 2, ws_name, length); */
1184 tx = (ICON_SIZE / 2 - WMWidthOfString(scr->clip_title_font, ws_number, nlength)) / 2;
1186 WMDrawString(scr->wmscreen, win, color, scr->clip_title_font, tx, 2, ws_number, nlength);
1188 wfree(ws_name);
1190 if (aicon->launching) {
1191 XFillRectangle(dpy, aicon->icon->core->window, scr->stipple_gc,
1192 0, 0, wPreferences.icon_size, wPreferences.icon_size);
1194 paintClipButtons(aicon, aicon->dock->lclip_button_pushed, aicon->dock->rclip_button_pushed);
1197 static void clipIconExpose(WObjDescriptor * desc, XEvent * event)
1199 wClipIconPaint(desc->parent);
1202 static void dockIconPaint(WAppIcon * btn)
1204 if (btn == btn->icon->core->screen_ptr->clip_icon)
1205 wClipIconPaint(btn);
1206 else {
1207 wAppIconPaint(btn);
1208 wAppIconSave(btn);
1212 static WMPropList *make_icon_state(WAppIcon * btn)
1214 WMPropList *node = NULL;
1215 WMPropList *command, *autolaunch, *lock, *name, *forced, *host;
1216 WMPropList *position, *buggy, *omnipresent;
1217 char *tmp;
1218 char buffer[64];
1220 if (btn) {
1221 if (!btn->command)
1222 command = WMCreatePLString("-");
1223 else
1224 command = WMCreatePLString(btn->command);
1226 autolaunch = btn->auto_launch ? dYes : dNo;
1228 lock = btn->lock ? dYes : dNo;
1230 tmp = EscapeWM_CLASS(btn->wm_instance, btn->wm_class);
1232 name = WMCreatePLString(tmp);
1234 wfree(tmp);
1236 forced = btn->forced_dock ? dYes : dNo;
1238 buggy = btn->buggy_app ? dYes : dNo;
1240 if (btn == btn->icon->core->screen_ptr->clip_icon)
1241 snprintf(buffer, sizeof(buffer), "%i,%i", btn->x_pos, btn->y_pos);
1242 else
1243 snprintf(buffer, sizeof(buffer), "%hi,%hi", btn->xindex, btn->yindex);
1244 position = WMCreatePLString(buffer);
1246 node = WMCreatePLDictionary(dCommand, command,
1247 dName, name,
1248 dAutoLaunch, autolaunch,
1249 dLock, lock,
1250 dForced, forced, dBuggyApplication, buggy, dPosition, position, NULL);
1251 WMReleasePropList(command);
1252 WMReleasePropList(name);
1253 WMReleasePropList(position);
1255 omnipresent = btn->omnipresent ? dYes : dNo;
1256 if (btn->dock != btn->icon->core->screen_ptr->dock && (btn->xindex != 0 || btn->yindex != 0))
1257 WMPutInPLDictionary(node, dOmnipresent, omnipresent);
1259 #ifdef XDND /* was OFFIX */
1260 if (btn->dnd_command) {
1261 command = WMCreatePLString(btn->dnd_command);
1262 WMPutInPLDictionary(node, dDropCommand, command);
1263 WMReleasePropList(command);
1265 #endif /* XDND */
1267 if (btn->paste_command) {
1268 command = WMCreatePLString(btn->paste_command);
1269 WMPutInPLDictionary(node, dPasteCommand, command);
1270 WMReleasePropList(command);
1273 if (btn->client_machine && btn->remote_start) {
1274 host = WMCreatePLString(btn->client_machine);
1275 WMPutInPLDictionary(node, dHost, host);
1276 WMReleasePropList(host);
1280 return node;
1283 static WMPropList *dockSaveState(WDock * dock)
1285 int i;
1286 WMPropList *icon_info;
1287 WMPropList *list = NULL, *dock_state = NULL;
1288 WMPropList *value, *key;
1289 char buffer[256];
1291 list = WMCreatePLArray(NULL);
1293 for (i = (dock->type == WM_DOCK ? 0 : 1); i < dock->max_icons; i++) {
1294 WAppIcon *btn = dock->icon_array[i];
1296 if (!btn || btn->attracted)
1297 continue;
1299 if ((icon_info = make_icon_state(dock->icon_array[i]))) {
1300 WMAddToPLArray(list, icon_info);
1301 WMReleasePropList(icon_info);
1305 dock_state = WMCreatePLDictionary(dApplications, list, NULL);
1307 if (dock->type == WM_DOCK) {
1308 snprintf(buffer, sizeof(buffer), "Applications%i", dock->screen_ptr->scr_height);
1309 key = WMCreatePLString(buffer);
1310 WMPutInPLDictionary(dock_state, key, list);
1311 WMReleasePropList(key);
1313 snprintf(buffer, sizeof(buffer), "%i,%i", (dock->on_right_side ? -ICON_SIZE : 0), dock->y_pos);
1314 value = WMCreatePLString(buffer);
1315 WMPutInPLDictionary(dock_state, dPosition, value);
1316 WMReleasePropList(value);
1318 WMReleasePropList(list);
1320 value = (dock->lowered ? dYes : dNo);
1321 WMPutInPLDictionary(dock_state, dLowered, value);
1323 if (dock->type == WM_CLIP) {
1324 value = (dock->collapsed ? dYes : dNo);
1325 WMPutInPLDictionary(dock_state, dCollapsed, value);
1327 value = (dock->auto_collapse ? dYes : dNo);
1328 WMPutInPLDictionary(dock_state, dAutoCollapse, value);
1330 value = (dock->auto_raise_lower ? dYes : dNo);
1331 WMPutInPLDictionary(dock_state, dAutoRaiseLower, value);
1333 value = (dock->attract_icons ? dYes : dNo);
1334 WMPutInPLDictionary(dock_state, dAutoAttractIcons, value);
1337 return dock_state;
1340 void wDockSaveState(WScreen * scr, WMPropList * old_state)
1342 WMPropList *dock_state;
1343 WMPropList *keys;
1345 dock_state = dockSaveState(scr->dock);
1348 * Copy saved states of docks with different sizes.
1350 if (old_state) {
1351 int i;
1352 WMPropList *tmp;
1354 keys = WMGetPLDictionaryKeys(old_state);
1355 for (i = 0; i < WMGetPropListItemCount(keys); i++) {
1356 tmp = WMGetFromPLArray(keys, i);
1358 if (strncasecmp(WMGetFromPLString(tmp), "applications", 12) == 0
1359 && !WMGetFromPLDictionary(dock_state, tmp)) {
1361 WMPutInPLDictionary(dock_state, tmp, WMGetFromPLDictionary(old_state, tmp));
1364 WMReleasePropList(keys);
1367 WMPutInPLDictionary(scr->session_state, dDock, dock_state);
1369 WMReleasePropList(dock_state);
1372 void wClipSaveState(WScreen * scr)
1374 WMPropList *clip_state;
1376 clip_state = make_icon_state(scr->clip_icon);
1378 WMPutInPLDictionary(scr->session_state, dClip, clip_state);
1380 WMReleasePropList(clip_state);
1383 WMPropList *wClipSaveWorkspaceState(WScreen * scr, int workspace)
1385 return dockSaveState(scr->workspaces[workspace]->clip);
1388 static Bool getBooleanDockValue(WMPropList * value, WMPropList * key)
1390 if (value) {
1391 if (WMIsPLString(value)) {
1392 if (strcasecmp(WMGetFromPLString(value), "YES") == 0)
1393 return True;
1394 } else {
1395 wwarning(_("bad value in docked icon state info %s"), WMGetFromPLString(key));
1398 return False;
1401 static WAppIcon *restore_icon_state(WScreen * scr, WMPropList * info, int type, int index)
1403 WAppIcon *aicon;
1404 WMPropList *cmd, *value;
1406 cmd = WMGetFromPLDictionary(info, dCommand);
1407 if (!cmd || !WMIsPLString(cmd)) {
1408 return NULL;
1411 /* parse window name */
1412 value = WMGetFromPLDictionary(info, dName);
1413 if (!value)
1414 return NULL;
1417 char *wclass, *winstance;
1418 char *command;
1420 ParseWindowName(value, &winstance, &wclass, "dock");
1422 if (!winstance && !wclass) {
1423 return NULL;
1426 /* get commands */
1428 if (cmd)
1429 command = wstrdup(WMGetFromPLString(cmd));
1430 else
1431 command = NULL;
1433 if (!command || strcmp(command, "-") == 0) {
1434 if (command)
1435 wfree(command);
1436 if (wclass)
1437 wfree(wclass);
1438 if (winstance)
1439 wfree(winstance);
1441 return NULL;
1444 aicon = wAppIconCreateForDock(scr, command, winstance, wclass, TILE_NORMAL);
1445 if (wclass)
1446 wfree(wclass);
1447 if (winstance)
1448 wfree(winstance);
1449 if (command)
1450 wfree(command);
1453 aicon->icon->core->descriptor.handle_mousedown = iconMouseDown;
1454 if (type == WM_CLIP) {
1455 aicon->icon->core->descriptor.handle_enternotify = clipEnterNotify;
1456 aicon->icon->core->descriptor.handle_leavenotify = clipLeaveNotify;
1458 aicon->icon->core->descriptor.parent_type = WCLASS_DOCK_ICON;
1459 aicon->icon->core->descriptor.parent = aicon;
1461 #ifdef XDND /* was OFFIX */
1462 cmd = WMGetFromPLDictionary(info, dDropCommand);
1463 if (cmd)
1464 aicon->dnd_command = wstrdup(WMGetFromPLString(cmd));
1465 #endif
1467 cmd = WMGetFromPLDictionary(info, dPasteCommand);
1468 if (cmd)
1469 aicon->paste_command = wstrdup(WMGetFromPLString(cmd));
1471 /* check auto launch */
1472 value = WMGetFromPLDictionary(info, dAutoLaunch);
1474 aicon->auto_launch = getBooleanDockValue(value, dAutoLaunch);
1476 /* check lock */
1477 value = WMGetFromPLDictionary(info, dLock);
1479 aicon->lock = getBooleanDockValue(value, dLock);
1481 /* check if it wasn't normally docked */
1482 value = WMGetFromPLDictionary(info, dForced);
1484 aicon->forced_dock = getBooleanDockValue(value, dForced);
1486 /* check if we can rely on the stuff in the app */
1487 value = WMGetFromPLDictionary(info, dBuggyApplication);
1489 aicon->buggy_app = getBooleanDockValue(value, dBuggyApplication);
1491 /* get position in the dock */
1492 value = WMGetFromPLDictionary(info, dPosition);
1493 if (value && WMIsPLString(value)) {
1494 if (sscanf(WMGetFromPLString(value), "%hi,%hi", &aicon->xindex, &aicon->yindex) != 2)
1495 wwarning(_("bad value in docked icon state info %s"), WMGetFromPLString(dPosition));
1497 /* check position sanity */
1498 /* incomplete section! */
1499 if (type == WM_DOCK) {
1500 aicon->xindex = 0;
1501 if (aicon->yindex < 0)
1502 wwarning(_("bad value in docked icon position %i,%i"),
1503 aicon->xindex, aicon->yindex);
1505 } else {
1506 aicon->yindex = index;
1507 aicon->xindex = 0;
1510 /* check if icon is omnipresent */
1511 value = WMGetFromPLDictionary(info, dOmnipresent);
1513 aicon->omnipresent = getBooleanDockValue(value, dOmnipresent);
1515 aicon->running = 0;
1516 aicon->docked = 1;
1518 return aicon;
1521 #define COMPLAIN(key) wwarning(_("bad value in dock state info:%s"), key)
1523 WAppIcon *wClipRestoreState(WScreen * scr, WMPropList * clip_state)
1525 WAppIcon *icon;
1526 WMPropList *value;
1528 icon = mainIconCreate(scr, WM_CLIP);
1530 if (!clip_state)
1531 return icon;
1533 WMRetainPropList(clip_state);
1535 /* restore position */
1537 value = WMGetFromPLDictionary(clip_state, dPosition);
1539 if (value) {
1540 if (!WMIsPLString(value))
1541 COMPLAIN("Position");
1542 else {
1543 WMRect rect;
1544 int flags;
1546 if (sscanf(WMGetFromPLString(value), "%i,%i", &icon->x_pos, &icon->y_pos) != 2)
1547 COMPLAIN("Position");
1549 /* check position sanity */
1550 rect.pos.x = icon->x_pos;
1551 rect.pos.y = icon->y_pos;
1552 rect.size.width = rect.size.height = ICON_SIZE;
1554 wGetRectPlacementInfo(scr, rect, &flags);
1555 if (flags & (XFLAG_DEAD | XFLAG_PARTIAL))
1556 wScreenKeepInside(scr, &icon->x_pos, &icon->y_pos, ICON_SIZE, ICON_SIZE);
1559 #ifdef XDND /* was OFFIX */
1560 value = WMGetFromPLDictionary(clip_state, dDropCommand);
1561 if (value && WMIsPLString(value))
1562 icon->dnd_command = wstrdup(WMGetFromPLString(value));
1563 #endif
1565 value = WMGetFromPLDictionary(clip_state, dPasteCommand);
1566 if (value && WMIsPLString(value))
1567 icon->paste_command = wstrdup(WMGetFromPLString(value));
1569 WMReleasePropList(clip_state);
1571 return icon;
1574 WDock *wDockRestoreState(WScreen * scr, WMPropList * dock_state, int type)
1576 WDock *dock;
1577 WMPropList *apps;
1578 WMPropList *value;
1579 WAppIcon *aicon, *old_top;
1580 int count, i;
1582 dock = wDockCreate(scr, type);
1584 if (!dock_state)
1585 return dock;
1587 WMRetainPropList(dock_state);
1589 /* restore position */
1591 value = WMGetFromPLDictionary(dock_state, dPosition);
1593 if (value) {
1594 if (!WMIsPLString(value)) {
1595 COMPLAIN("Position");
1596 } else {
1597 WMRect rect;
1598 int flags;
1600 if (sscanf(WMGetFromPLString(value), "%i,%i", &dock->x_pos, &dock->y_pos) != 2)
1601 COMPLAIN("Position");
1603 /* check position sanity */
1604 rect.pos.x = dock->x_pos;
1605 rect.pos.y = dock->y_pos;
1606 rect.size.width = rect.size.height = ICON_SIZE;
1608 wGetRectPlacementInfo(scr, rect, &flags);
1609 if (flags & (XFLAG_DEAD | XFLAG_PARTIAL)) {
1610 int x = dock->x_pos;
1611 wScreenKeepInside(scr, &x, &dock->y_pos, ICON_SIZE, ICON_SIZE);
1614 /* Is this needed any more? */
1615 if (type == WM_CLIP) {
1616 if (dock->x_pos < 0) {
1617 dock->x_pos = 0;
1618 } else if (dock->x_pos > scr->scr_width - ICON_SIZE) {
1619 dock->x_pos = scr->scr_width - ICON_SIZE;
1621 } else {
1622 if (dock->x_pos >= 0) {
1623 dock->x_pos = DOCK_EXTRA_SPACE;
1624 dock->on_right_side = 0;
1625 } else {
1626 dock->x_pos = scr->scr_width - DOCK_EXTRA_SPACE - ICON_SIZE;
1627 dock->on_right_side = 1;
1633 /* restore lowered/raised state */
1635 dock->lowered = 0;
1637 value = WMGetFromPLDictionary(dock_state, dLowered);
1639 if (value) {
1640 if (!WMIsPLString(value)) {
1641 COMPLAIN("Lowered");
1642 } else {
1643 if (strcasecmp(WMGetFromPLString(value), "YES") == 0) {
1644 dock->lowered = 1;
1649 /* restore collapsed state */
1651 dock->collapsed = 0;
1653 value = WMGetFromPLDictionary(dock_state, dCollapsed);
1655 if (value) {
1656 if (!WMIsPLString(value)) {
1657 COMPLAIN("Collapsed");
1658 } else {
1659 if (strcasecmp(WMGetFromPLString(value), "YES") == 0) {
1660 dock->collapsed = 1;
1665 /* restore auto-collapsed state */
1667 value = WMGetFromPLDictionary(dock_state, dAutoCollapse);
1669 if (value) {
1670 if (!WMIsPLString(value)) {
1671 COMPLAIN("AutoCollapse");
1672 } else {
1673 if (strcasecmp(WMGetFromPLString(value), "YES") == 0) {
1674 dock->auto_collapse = 1;
1675 dock->collapsed = 1;
1680 /* restore auto-raise/lower state */
1682 value = WMGetFromPLDictionary(dock_state, dAutoRaiseLower);
1684 if (value) {
1685 if (!WMIsPLString(value)) {
1686 COMPLAIN("AutoRaiseLower");
1687 } else {
1688 if (strcasecmp(WMGetFromPLString(value), "YES") == 0) {
1689 dock->auto_raise_lower = 1;
1694 /* restore attract icons state */
1696 dock->attract_icons = 0;
1698 value = WMGetFromPLDictionary(dock_state, dAutoAttractIcons);
1700 if (value) {
1701 if (!WMIsPLString(value)) {
1702 COMPLAIN("AutoAttractIcons");
1703 } else {
1704 if (strcasecmp(WMGetFromPLString(value), "YES") == 0) {
1705 dock->attract_icons = 1;
1710 /* application list */
1713 WMPropList *tmp;
1714 char buffer[64];
1717 * When saving, it saves the dock state in
1718 * Applications and Applicationsnnn
1720 * When loading, it will first try Applicationsnnn.
1721 * If it does not exist, use Applications as default.
1724 snprintf(buffer, sizeof(buffer), "Applications%i", scr->scr_height);
1726 tmp = WMCreatePLString(buffer);
1727 apps = WMGetFromPLDictionary(dock_state, tmp);
1728 WMReleasePropList(tmp);
1730 if (!apps) {
1731 apps = WMGetFromPLDictionary(dock_state, dApplications);
1735 if (!apps) {
1736 goto finish;
1739 count = WMGetPropListItemCount(apps);
1741 if (count == 0)
1742 goto finish;
1744 old_top = dock->icon_array[0];
1746 /* dock->icon_count is set to 1 when dock is created.
1747 * Since Clip is already restored, we want to keep it so for clip,
1748 * but for dock we may change the default top tile, so we set it to 0.
1750 if (type == WM_DOCK)
1751 dock->icon_count = 0;
1753 for (i = 0; i < count; i++) {
1754 if (dock->icon_count >= dock->max_icons) {
1755 wwarning(_("there are too many icons stored in dock. Ignoring what doesn't fit"));
1756 break;
1759 value = WMGetFromPLArray(apps, i);
1760 aicon = restore_icon_state(scr, value, type, dock->icon_count);
1762 dock->icon_array[dock->icon_count] = aicon;
1764 if (aicon) {
1765 aicon->dock = dock;
1766 aicon->x_pos = dock->x_pos + (aicon->xindex * ICON_SIZE);
1767 aicon->y_pos = dock->y_pos + (aicon->yindex * ICON_SIZE);
1769 if (dock->lowered)
1770 ChangeStackingLevel(aicon->icon->core, WMNormalLevel);
1771 else
1772 ChangeStackingLevel(aicon->icon->core, WMDockLevel);
1774 wCoreConfigure(aicon->icon->core, aicon->x_pos, aicon->y_pos, 0, 0);
1776 if (!dock->collapsed)
1777 XMapWindow(dpy, aicon->icon->core->window);
1778 wRaiseFrame(aicon->icon->core);
1780 dock->icon_count++;
1781 } else if (dock->icon_count == 0 && type == WM_DOCK)
1782 dock->icon_count++;
1785 /* if the first icon is not defined, use the default */
1786 if (dock->icon_array[0] == NULL) {
1787 /* update default icon */
1788 old_top->x_pos = dock->x_pos;
1789 old_top->y_pos = dock->y_pos;
1790 if (dock->lowered)
1791 ChangeStackingLevel(old_top->icon->core, WMNormalLevel);
1792 else
1793 ChangeStackingLevel(old_top->icon->core, WMDockLevel);
1794 dock->icon_array[0] = old_top;
1795 XMoveWindow(dpy, old_top->icon->core->window, dock->x_pos, dock->y_pos);
1796 /* we don't need to increment dock->icon_count here because it was
1797 * incremented in the loop above.
1799 } else if (old_top != dock->icon_array[0]) {
1800 if (old_top == scr->clip_icon)
1801 scr->clip_icon = dock->icon_array[0];
1802 wAppIconDestroy(old_top);
1805 finish:
1806 WMReleasePropList(dock_state);
1808 return dock;
1811 void wDockLaunchWithState(WDock * dock, WAppIcon * btn, WSavedState * state)
1813 if (btn && btn->command && !btn->running && !btn->launching) {
1815 btn->drop_launch = 0;
1816 btn->paste_launch = 0;
1818 btn->pid = execCommand(btn, btn->command, state);
1820 if (btn->pid > 0) {
1821 if (!btn->forced_dock && !btn->buggy_app) {
1822 btn->launching = 1;
1823 dockIconPaint(btn);
1826 } else {
1827 wfree(state);
1831 void wDockDoAutoLaunch(WDock * dock, int workspace)
1833 WAppIcon *btn;
1834 WSavedState *state;
1835 int i;
1837 for (i = 0; i < dock->max_icons; i++) {
1838 btn = dock->icon_array[i];
1839 if (!btn || !btn->auto_launch)
1840 continue;
1842 state = wmalloc(sizeof(WSavedState));
1843 memset(state, 0, sizeof(WSavedState));
1844 state->workspace = workspace;
1845 /* TODO: this is klugy and is very difficult to understand
1846 * what's going on. Try to clean up */
1847 wDockLaunchWithState(dock, btn, state);
1851 #ifdef XDND /* was OFFIX */
1852 static WDock *findDock(WScreen * scr, XEvent * event, int *icon_pos)
1854 WDock *dock;
1855 int i;
1857 *icon_pos = -1;
1858 if ((dock = scr->dock) != NULL) {
1859 for (i = 0; i < dock->max_icons; i++) {
1860 if (dock->icon_array[i]
1861 && dock->icon_array[i]->icon->core->window == event->xclient.window) {
1862 *icon_pos = i;
1863 break;
1867 if (*icon_pos < 0 && (dock = scr->workspaces[scr->current_workspace]->clip) != NULL) {
1868 for (i = 0; i < dock->max_icons; i++) {
1869 if (dock->icon_array[i]
1870 && dock->icon_array[i]->icon->core->window == event->xclient.window) {
1871 *icon_pos = i;
1872 break;
1876 if (*icon_pos >= 0)
1877 return dock;
1878 return NULL;
1881 int wDockReceiveDNDDrop(WScreen * scr, XEvent * event)
1883 WDock *dock;
1884 WAppIcon *btn;
1885 int icon_pos;
1887 dock = findDock(scr, event, &icon_pos);
1888 if (!dock)
1889 return False;
1892 * Return True if the drop was on an application icon window.
1893 * In this case, let the ClientMessage handler redirect the
1894 * message to the app.
1896 if (dock->icon_array[icon_pos]->icon->icon_win != None)
1897 return True;
1899 if (dock->icon_array[icon_pos]->dnd_command != NULL) {
1900 scr->flags.dnd_data_convertion_status = 0;
1902 btn = dock->icon_array[icon_pos];
1904 if (!btn->forced_dock) {
1905 btn->relaunching = btn->running;
1906 btn->running = 1;
1908 if (btn->wm_instance || btn->wm_class) {
1909 WWindowAttributes attr;
1910 memset(&attr, 0, sizeof(WWindowAttributes));
1911 wDefaultFillAttributes(btn->icon->core->screen_ptr,
1912 btn->wm_instance, btn->wm_class, &attr, NULL, True);
1914 if (!attr.no_appicon)
1915 btn->launching = 1;
1916 else
1917 btn->running = 0;
1920 btn->paste_launch = 0;
1921 btn->drop_launch = 1;
1922 scr->last_dock = dock;
1923 btn->pid = execCommand(btn, btn->dnd_command, NULL);
1924 if (btn->pid > 0) {
1925 dockIconPaint(btn);
1926 } else {
1927 btn->launching = 0;
1928 if (!btn->relaunching) {
1929 btn->running = 0;
1933 return False;
1935 #endif /* XDND */
1937 Bool wDockAttachIcon(WDock * dock, WAppIcon * icon, int x, int y)
1939 WWindow *wwin;
1940 int index;
1942 wwin = icon->icon->owner;
1943 if (icon->command == NULL) {
1944 char *command;
1946 icon->editing = 0;
1948 command = GetCommandForWindow(wwin->client_win);
1949 if (command) {
1950 icon->command = command;
1951 } else {
1952 /* icon->forced_dock = 1; */
1953 if (dock->type != WM_CLIP || !icon->attracted) {
1954 icon->editing = 1;
1955 if (wInputDialog(dock->screen_ptr, _("Dock Icon"),
1956 _("Type the command used to launch the application"), &command)) {
1957 if (command && (command[0] == 0 || (command[0] == '-' && command[1] == 0))) {
1958 wfree(command);
1959 command = NULL;
1961 icon->command = command;
1962 icon->editing = 0;
1963 } else {
1964 icon->editing = 0;
1965 if (command)
1966 wfree(command);
1967 /* If the target is the dock, reject the icon. If
1968 * the target is the clip, make it an attracted icon
1970 if (dock->type == WM_CLIP) {
1971 icon->attracted = 1;
1972 if (!icon->icon->shadowed) {
1973 icon->icon->shadowed = 1;
1974 icon->icon->force_paint = 1;
1976 } else {
1977 return False;
1982 } else {
1983 icon->editing = 0;
1986 for (index = 1; index < dock->max_icons; index++)
1987 if (dock->icon_array[index] == NULL)
1988 break;
1989 /* if (index == dock->max_icons)
1990 return; */
1992 assert(index < dock->max_icons);
1994 dock->icon_array[index] = icon;
1995 icon->yindex = y;
1996 icon->xindex = x;
1998 icon->omnipresent = 0;
2000 icon->x_pos = dock->x_pos + x * ICON_SIZE;
2001 icon->y_pos = dock->y_pos + y * ICON_SIZE;
2003 dock->icon_count++;
2005 icon->running = 1;
2006 icon->launching = 0;
2007 icon->docked = 1;
2008 icon->dock = dock;
2009 icon->icon->core->descriptor.handle_mousedown = iconMouseDown;
2010 if (dock->type == WM_CLIP) {
2011 icon->icon->core->descriptor.handle_enternotify = clipEnterNotify;
2012 icon->icon->core->descriptor.handle_leavenotify = clipLeaveNotify;
2014 icon->icon->core->descriptor.parent_type = WCLASS_DOCK_ICON;
2015 icon->icon->core->descriptor.parent = icon;
2017 MoveInStackListUnder(dock->icon_array[index - 1]->icon->core, icon->icon->core);
2018 wAppIconMove(icon, icon->x_pos, icon->y_pos);
2019 wAppIconPaint(icon);
2020 wAppIconSave(icon);
2022 if (wPreferences.auto_arrange_icons)
2023 wArrangeIcons(dock->screen_ptr, True);
2025 #ifdef XDND /* was OFFIX */
2026 if (icon->command && !icon->dnd_command) {
2027 int len = strlen(icon->command) + 8;
2028 icon->dnd_command = wmalloc(len);
2029 snprintf(icon->dnd_command, len, "%s %%d", icon->command);
2031 #endif
2033 if (icon->command && !icon->paste_command) {
2034 int len = strlen(icon->command) + 8;
2035 icon->paste_command = wmalloc(len);
2036 snprintf(icon->paste_command, len, "%s %%s", icon->command);
2039 return True;
2042 void reattachIcon(WDock * dock, WAppIcon * icon, int x, int y)
2044 int index;
2046 for (index = 1; index < dock->max_icons; index++) {
2047 if (dock->icon_array[index] == icon)
2048 break;
2050 assert(index < dock->max_icons);
2052 icon->yindex = y;
2053 icon->xindex = x;
2055 icon->x_pos = dock->x_pos + x * ICON_SIZE;
2056 icon->y_pos = dock->y_pos + y * ICON_SIZE;
2059 Bool moveIconBetweenDocks(WDock * src, WDock * dest, WAppIcon * icon, int x, int y)
2061 WWindow *wwin;
2062 char *command;
2063 int index;
2065 if (src == dest)
2066 return True; /* No move needed, we're already there */
2068 if (dest == NULL)
2069 return False;
2071 wwin = icon->icon->owner;
2074 * For the moment we can't do this if we move icons in Clip from one
2075 * workspace to other, because if we move two or more icons without
2076 * command, the dialog box will not be able to tell us to which of the
2077 * moved icons it applies. -Dan
2079 if ((dest->type == WM_DOCK /*|| dest->keep_attracted */ ) && icon->command == NULL) {
2080 command = GetCommandForWindow(wwin->client_win);
2081 if (command) {
2082 icon->command = command;
2083 } else {
2084 icon->editing = 1;
2085 /* icon->forced_dock = 1; */
2086 if (wInputDialog(src->screen_ptr, _("Dock Icon"),
2087 _("Type the command used to launch the application"), &command)) {
2088 if (command && (command[0] == 0 || (command[0] == '-' && command[1] == 0))) {
2089 wfree(command);
2090 command = NULL;
2092 icon->command = command;
2093 } else {
2094 icon->editing = 0;
2095 if (command)
2096 wfree(command);
2097 return False;
2099 icon->editing = 0;
2103 if (dest->type == WM_DOCK)
2104 wClipMakeIconOmnipresent(icon, False);
2106 for (index = 1; index < src->max_icons; index++) {
2107 if (src->icon_array[index] == icon)
2108 break;
2110 assert(index < src->max_icons);
2112 src->icon_array[index] = NULL;
2113 src->icon_count--;
2115 for (index = 1; index < dest->max_icons; index++) {
2116 if (dest->icon_array[index] == NULL)
2117 break;
2119 /* if (index == dest->max_icons)
2120 return; */
2122 assert(index < dest->max_icons);
2124 dest->icon_array[index] = icon;
2125 icon->dock = dest;
2127 /* deselect the icon */
2128 if (icon->icon->selected)
2129 wIconSelect(icon->icon);
2131 if (dest->type == WM_DOCK) {
2132 icon->icon->core->descriptor.handle_enternotify = NULL;
2133 icon->icon->core->descriptor.handle_leavenotify = NULL;
2134 } else {
2135 icon->icon->core->descriptor.handle_enternotify = clipEnterNotify;
2136 icon->icon->core->descriptor.handle_leavenotify = clipLeaveNotify;
2139 /* set it to be kept when moving to dock.
2140 * Unless the icon does not have a command set
2142 if (icon->command && dest->type == WM_DOCK) {
2143 icon->attracted = 0;
2144 if (icon->icon->shadowed) {
2145 icon->icon->shadowed = 0;
2146 icon->icon->force_paint = 1;
2148 wAppIconSave(icon);
2151 if (src->auto_collapse || src->auto_raise_lower)
2152 clipLeave(src);
2154 icon->yindex = y;
2155 icon->xindex = x;
2157 icon->x_pos = dest->x_pos + x * ICON_SIZE;
2158 icon->y_pos = dest->y_pos + y * ICON_SIZE;
2160 dest->icon_count++;
2162 MoveInStackListUnder(dest->icon_array[index - 1]->icon->core, icon->icon->core);
2163 wAppIconPaint(icon);
2165 return True;
2168 void wDockDetach(WDock * dock, WAppIcon * icon)
2170 int index;
2172 /* make the settings panel be closed */
2173 if (icon->panel) {
2174 DestroyDockAppSettingsPanel(icon->panel);
2177 /* This must be called before icon->dock is set to NULL.
2178 * Don't move it. -Dan
2180 wClipMakeIconOmnipresent(icon, False);
2182 icon->docked = 0;
2183 icon->dock = NULL;
2184 icon->attracted = 0;
2185 icon->auto_launch = 0;
2186 if (icon->icon->shadowed) {
2187 icon->icon->shadowed = 0;
2188 icon->icon->force_paint = 1;
2191 /* deselect the icon */
2192 if (icon->icon->selected)
2193 wIconSelect(icon->icon);
2195 if (icon->command) {
2196 wfree(icon->command);
2197 icon->command = NULL;
2199 #ifdef XDND /* was OFFIX */
2200 if (icon->dnd_command) {
2201 wfree(icon->dnd_command);
2202 icon->dnd_command = NULL;
2204 #endif
2205 if (icon->paste_command) {
2206 wfree(icon->paste_command);
2207 icon->paste_command = NULL;
2210 for (index = 1; index < dock->max_icons; index++)
2211 if (dock->icon_array[index] == icon)
2212 break;
2213 assert(index < dock->max_icons);
2214 dock->icon_array[index] = NULL;
2215 icon->yindex = -1;
2216 icon->xindex = -1;
2218 dock->icon_count--;
2220 /* if the dock is not attached to an application or
2221 * the the application did not set the approriate hints yet,
2222 * destroy the icon */
2223 if (!icon->running || !wApplicationOf(icon->main_window))
2224 wAppIconDestroy(icon);
2225 else {
2226 icon->icon->core->descriptor.handle_mousedown = appIconMouseDown;
2227 icon->icon->core->descriptor.handle_enternotify = NULL;
2228 icon->icon->core->descriptor.handle_leavenotify = NULL;
2229 icon->icon->core->descriptor.parent_type = WCLASS_APPICON;
2230 icon->icon->core->descriptor.parent = icon;
2232 ChangeStackingLevel(icon->icon->core, NORMAL_ICON_LEVEL);
2234 wAppIconPaint(icon);
2235 if (wPreferences.auto_arrange_icons) {
2236 wArrangeIcons(dock->screen_ptr, True);
2239 if (dock->auto_collapse || dock->auto_raise_lower)
2240 clipLeave(dock);
2244 * returns the closest Dock slot index for the passed
2245 * coordinates.
2247 * Returns False if icon can't be docked.
2249 * Note: this function should NEVER alter ret_x or ret_y, unless it will
2250 * return True. -Dan
2252 Bool wDockSnapIcon(WDock * dock, WAppIcon * icon, int req_x, int req_y, int *ret_x, int *ret_y, int redocking)
2254 WScreen *scr = dock->screen_ptr;
2255 int dx, dy;
2256 int ex_x, ex_y;
2257 int i, offset = ICON_SIZE / 2;
2258 WAppIcon *aicon = NULL;
2259 WAppIcon *nicon = NULL;
2260 int max_y_icons;
2262 /* TODO: XINERAMA, for these */
2263 max_y_icons = scr->scr_height / ICON_SIZE - 1;
2265 if (wPreferences.flags.noupdates)
2266 return False;
2268 dx = dock->x_pos;
2269 dy = dock->y_pos;
2271 /* if the dock is full */
2272 if (!redocking && (dock->icon_count >= dock->max_icons)) {
2273 return False;
2276 /* exact position */
2277 if (req_y < dy)
2278 ex_y = (req_y - offset - dy) / ICON_SIZE;
2279 else
2280 ex_y = (req_y + offset - dy) / ICON_SIZE;
2282 if (req_x < dx)
2283 ex_x = (req_x - offset - dx) / ICON_SIZE;
2284 else
2285 ex_x = (req_x + offset - dx) / ICON_SIZE;
2287 /* check if the icon is outside the screen boundaries */
2289 WMRect rect;
2290 int flags;
2292 rect.pos.x = dx + ex_x * ICON_SIZE;
2293 rect.pos.y = dy + ex_y * ICON_SIZE;
2294 rect.size.width = rect.size.height = ICON_SIZE;
2296 wGetRectPlacementInfo(scr, rect, &flags);
2297 if (flags & (XFLAG_DEAD | XFLAG_PARTIAL))
2298 return False;
2301 if (dock->type == WM_DOCK) {
2302 if (icon->dock != dock && ex_x != 0)
2303 return False;
2305 aicon = NULL;
2306 for (i = 0; i < dock->max_icons; i++) {
2307 nicon = dock->icon_array[i];
2308 if (nicon && nicon->yindex == ex_y) {
2309 aicon = nicon;
2310 break;
2314 if (redocking) {
2315 int sig, done, closest;
2317 /* Possible cases when redocking:
2319 * icon dragged out of range of any slot -> false
2320 * icon dragged to range of free slot
2321 * icon dragged to range of same slot
2322 * icon dragged to range of different icon
2324 if (abs(ex_x) > DOCK_DETTACH_THRESHOLD)
2325 return False;
2327 if (ex_y >= 0 && ex_y <= max_y_icons && (aicon == icon || !aicon)) {
2328 *ret_x = 0;
2329 *ret_y = ex_y;
2330 return True;
2333 /* start looking at the upper slot or lower? */
2334 if (ex_y * ICON_SIZE < (req_y + offset - dy))
2335 sig = 1;
2336 else
2337 sig = -1;
2339 closest = -1;
2340 done = 0;
2341 /* look for closest free slot */
2342 for (i = 0; i < (DOCK_DETTACH_THRESHOLD + 1) * 2 && !done; i++) {
2343 int j;
2345 done = 1;
2346 closest = sig * (i / 2) + ex_y;
2347 /* check if this slot is used */
2348 if (closest >= 0) {
2349 for (j = 0; j < dock->max_icons; j++) {
2350 if (dock->icon_array[j]
2351 && dock->icon_array[j]->yindex == closest) {
2352 /* slot is used by someone else */
2353 if (dock->icon_array[j] != icon)
2354 done = 0;
2355 break;
2359 sig = -sig;
2361 if (done && closest >= 0 && closest <= max_y_icons &&
2362 ((ex_y >= closest && ex_y - closest < DOCK_DETTACH_THRESHOLD + 1)
2363 || (ex_y < closest && closest - ex_y <= DOCK_DETTACH_THRESHOLD + 1))) {
2364 *ret_x = 0;
2365 *ret_y = closest;
2366 return True;
2368 } else { /* !redocking */
2370 /* if slot is free and the icon is close enough, return it */
2371 if (!aicon && ex_x == 0 && ex_y >= 0 && ex_y <= max_y_icons) {
2372 *ret_x = 0;
2373 *ret_y = ex_y;
2374 return True;
2377 } else { /* CLIP */
2378 int neighbours = 0;
2379 int start, stop, k;
2381 start = icon->omnipresent ? 0 : scr->current_workspace;
2382 stop = icon->omnipresent ? scr->workspace_count : start + 1;
2384 aicon = NULL;
2385 for (k = start; k < stop; k++) {
2386 WDock *tmp = scr->workspaces[k]->clip;
2387 if (!tmp)
2388 continue;
2389 for (i = 0; i < tmp->max_icons; i++) {
2390 nicon = tmp->icon_array[i];
2391 if (nicon && nicon->xindex == ex_x && nicon->yindex == ex_y) {
2392 aicon = nicon;
2393 break;
2396 if (aicon)
2397 break;
2399 for (k = start; k < stop; k++) {
2400 WDock *tmp = scr->workspaces[k]->clip;
2401 if (!tmp)
2402 continue;
2403 for (i = 0; i < tmp->max_icons; i++) {
2404 nicon = tmp->icon_array[i];
2405 if (nicon && nicon != icon && /* Icon can't be it's own neighbour */
2406 (abs(nicon->xindex - ex_x) <= CLIP_ATTACH_VICINITY &&
2407 abs(nicon->yindex - ex_y) <= CLIP_ATTACH_VICINITY)) {
2408 neighbours = 1;
2409 break;
2412 if (neighbours)
2413 break;
2416 if (neighbours && (aicon == NULL || (redocking && aicon == icon))) {
2417 *ret_x = ex_x;
2418 *ret_y = ex_y;
2419 return True;
2422 return False;
2425 static int onScreen(WScreen * scr, int x, int y, int sx, int ex, int sy, int ey)
2427 WMRect rect = wmkrect(x, y, ICON_SIZE, ICON_SIZE);
2428 int flags;
2430 wGetRectPlacementInfo(scr, rect, &flags);
2432 return !(flags & (XFLAG_DEAD | XFLAG_PARTIAL));
2436 * returns true if it can find a free slot in the dock,
2437 * in which case it changes x_pos and y_pos accordingly.
2438 * Else returns false.
2440 Bool wDockFindFreeSlot(WDock * dock, int *x_pos, int *y_pos)
2442 WScreen *scr = dock->screen_ptr;
2443 WAppIcon *btn;
2444 WAppIconChain *chain;
2445 unsigned char *slot_map;
2446 int mwidth;
2447 int r;
2448 int x, y;
2449 int i, done = False;
2450 int corner;
2451 int sx = 0, sy = 0, ex = scr->scr_width, ey = scr->scr_height;
2452 int extra_count = 0;
2454 if (dock->type == WM_CLIP && dock != scr->workspaces[scr->current_workspace]->clip)
2455 extra_count = scr->global_icon_count;
2457 /* if the dock is full */
2458 if (dock->icon_count + extra_count >= dock->max_icons) {
2459 return False;
2462 if (!wPreferences.flags.nodock && scr->dock) {
2463 if (scr->dock->on_right_side)
2464 ex -= ICON_SIZE + DOCK_EXTRA_SPACE;
2465 else
2466 sx += ICON_SIZE + DOCK_EXTRA_SPACE;
2469 if (ex < dock->x_pos)
2470 ex = dock->x_pos;
2471 if (sx > dock->x_pos + ICON_SIZE)
2472 sx = dock->x_pos + ICON_SIZE;
2473 #define C_NONE 0
2474 #define C_NW 1
2475 #define C_NE 2
2476 #define C_SW 3
2477 #define C_SE 4
2479 /* check if clip is in a corner */
2480 if (dock->type == WM_CLIP) {
2481 if (dock->x_pos < 1 && dock->y_pos < 1)
2482 corner = C_NE;
2483 else if (dock->x_pos < 1 && dock->y_pos >= (ey - ICON_SIZE))
2484 corner = C_SE;
2485 else if (dock->x_pos >= (ex - ICON_SIZE) && dock->y_pos >= (ey - ICON_SIZE))
2486 corner = C_SW;
2487 else if (dock->x_pos >= (ex - ICON_SIZE) && dock->y_pos < 1)
2488 corner = C_NW;
2489 else
2490 corner = C_NONE;
2491 } else
2492 corner = C_NONE;
2494 /* If the clip is in the corner, use only slots that are in the border
2495 * of the screen */
2496 if (corner != C_NONE) {
2497 char *hmap, *vmap;
2498 int hcount, vcount;
2500 hcount = WMIN(dock->max_icons, scr->scr_width / ICON_SIZE);
2501 vcount = WMIN(dock->max_icons, scr->scr_height / ICON_SIZE);
2502 hmap = wmalloc(hcount + 1);
2503 memset(hmap, 0, hcount + 1);
2504 vmap = wmalloc(vcount + 1);
2505 memset(vmap, 0, vcount + 1);
2507 /* mark used positions */
2508 switch (corner) {
2509 case C_NE:
2510 for (i = 0; i < dock->max_icons; i++) {
2511 btn = dock->icon_array[i];
2512 if (!btn)
2513 continue;
2515 if (btn->xindex == 0 && btn->yindex > 0 && btn->yindex < vcount)
2516 vmap[btn->yindex] = 1;
2517 else if (btn->yindex == 0 && btn->xindex > 0 && btn->xindex < hcount)
2518 hmap[btn->xindex] = 1;
2520 for (chain = scr->global_icons; chain != NULL; chain = chain->next) {
2521 btn = chain->aicon;
2522 if (btn->xindex == 0 && btn->yindex > 0 && btn->yindex < vcount)
2523 vmap[btn->yindex] = 1;
2524 else if (btn->yindex == 0 && btn->xindex > 0 && btn->xindex < hcount)
2525 hmap[btn->xindex] = 1;
2527 break;
2528 case C_NW:
2529 for (i = 0; i < dock->max_icons; i++) {
2530 btn = dock->icon_array[i];
2531 if (!btn)
2532 continue;
2534 if (btn->xindex == 0 && btn->yindex > 0 && btn->yindex < vcount)
2535 vmap[btn->yindex] = 1;
2536 else if (btn->yindex == 0 && btn->xindex < 0 && btn->xindex > -hcount)
2537 hmap[-btn->xindex] = 1;
2539 for (chain = scr->global_icons; chain != NULL; chain = chain->next) {
2540 btn = chain->aicon;
2541 if (btn->xindex == 0 && btn->yindex > 0 && btn->yindex < vcount)
2542 vmap[btn->yindex] = 1;
2543 else if (btn->yindex == 0 && btn->xindex < 0 && btn->xindex > -hcount)
2544 hmap[-btn->xindex] = 1;
2546 break;
2547 case C_SE:
2548 for (i = 0; i < dock->max_icons; i++) {
2549 btn = dock->icon_array[i];
2550 if (!btn)
2551 continue;
2553 if (btn->xindex == 0 && btn->yindex < 0 && btn->yindex > -vcount)
2554 vmap[-btn->yindex] = 1;
2555 else if (btn->yindex == 0 && btn->xindex > 0 && btn->xindex < hcount)
2556 hmap[btn->xindex] = 1;
2558 for (chain = scr->global_icons; chain != NULL; chain = chain->next) {
2559 btn = chain->aicon;
2560 if (btn->xindex == 0 && btn->yindex < 0 && btn->yindex > -vcount)
2561 vmap[-btn->yindex] = 1;
2562 else if (btn->yindex == 0 && btn->xindex > 0 && btn->xindex < hcount)
2563 hmap[btn->xindex] = 1;
2565 break;
2566 case C_SW:
2567 default:
2568 for (i = 0; i < dock->max_icons; i++) {
2569 btn = dock->icon_array[i];
2570 if (!btn)
2571 continue;
2573 if (btn->xindex == 0 && btn->yindex < 0 && btn->yindex > -vcount)
2574 vmap[-btn->yindex] = 1;
2575 else if (btn->yindex == 0 && btn->xindex < 0 && btn->xindex > -hcount)
2576 hmap[-btn->xindex] = 1;
2578 for (chain = scr->global_icons; chain != NULL; chain = chain->next) {
2579 btn = chain->aicon;
2580 if (btn->xindex == 0 && btn->yindex < 0 && btn->yindex > -vcount)
2581 vmap[-btn->yindex] = 1;
2582 else if (btn->yindex == 0 && btn->xindex < 0 && btn->xindex > -hcount)
2583 hmap[-btn->xindex] = 1;
2586 x = 0;
2587 y = 0;
2588 done = 0;
2589 /* search a vacant slot */
2590 for (i = 1; i < WMAX(vcount, hcount); i++) {
2591 if (i < vcount && vmap[i] == 0) {
2592 /* found a slot */
2593 x = 0;
2594 y = i;
2595 done = 1;
2596 break;
2597 } else if (i < hcount && hmap[i] == 0) {
2598 /* found a slot */
2599 x = i;
2600 y = 0;
2601 done = 1;
2602 break;
2605 wfree(vmap);
2606 wfree(hmap);
2607 /* If found a slot, translate and return */
2608 if (done) {
2609 if (corner == C_NW || corner == C_NE) {
2610 *y_pos = y;
2611 } else {
2612 *y_pos = -y;
2614 if (corner == C_NE || corner == C_SE) {
2615 *x_pos = x;
2616 } else {
2617 *x_pos = -x;
2619 return True;
2621 /* else, try to find a slot somewhere else */
2624 /* a map of mwidth x mwidth would be enough if we allowed icons to be
2625 * placed outside of screen */
2626 mwidth = (int)ceil(sqrt(dock->max_icons));
2628 /* In the worst case (the clip is in the corner of the screen),
2629 * the amount of icons that fit in the clip is smaller.
2630 * Double the map to get a safe value.
2632 mwidth += mwidth;
2634 r = (mwidth - 1) / 2;
2636 slot_map = wmalloc(mwidth * mwidth);
2637 memset(slot_map, 0, mwidth * mwidth);
2639 #define XY2OFS(x,y) (WMAX(abs(x),abs(y)) > r) ? 0 : (((y)+r)*(mwidth)+(x)+r)
2641 /* mark used slots in the map. If the slot falls outside the map
2642 * (for example, when all icons are placed in line), ignore them. */
2643 for (i = 0; i < dock->max_icons; i++) {
2644 btn = dock->icon_array[i];
2645 if (btn)
2646 slot_map[XY2OFS(btn->xindex, btn->yindex)] = 1;
2648 for (chain = scr->global_icons; chain != NULL; chain = chain->next) {
2649 slot_map[XY2OFS(chain->aicon->xindex, chain->aicon->yindex)] = 1;
2651 /* Find closest slot from the center that is free by scanning the
2652 * map from the center to outward in circular passes.
2653 * This will not result in a neat layout, but will be optimal
2654 * in the sense that there will not be holes left.
2656 done = 0;
2657 for (i = 1; i <= r && !done; i++) {
2658 int tx, ty;
2660 /* top and bottom parts of the ring */
2661 for (x = -i; x <= i && !done; x++) {
2662 tx = dock->x_pos + x * ICON_SIZE;
2663 y = -i;
2664 ty = dock->y_pos + y * ICON_SIZE;
2665 if (slot_map[XY2OFS(x, y)] == 0 && onScreen(scr, tx, ty, sx, ex, sy, ey)) {
2666 *x_pos = x;
2667 *y_pos = y;
2668 done = 1;
2669 break;
2671 y = i;
2672 ty = dock->y_pos + y * ICON_SIZE;
2673 if (slot_map[XY2OFS(x, y)] == 0 && onScreen(scr, tx, ty, sx, ex, sy, ey)) {
2674 *x_pos = x;
2675 *y_pos = y;
2676 done = 1;
2677 break;
2680 /* left and right parts of the ring */
2681 for (y = -i + 1; y <= i - 1; y++) {
2682 ty = dock->y_pos + y * ICON_SIZE;
2683 x = -i;
2684 tx = dock->x_pos + x * ICON_SIZE;
2685 if (slot_map[XY2OFS(x, y)] == 0 && onScreen(scr, tx, ty, sx, ex, sy, ey)) {
2686 *x_pos = x;
2687 *y_pos = y;
2688 done = 1;
2689 break;
2691 x = i;
2692 tx = dock->x_pos + x * ICON_SIZE;
2693 if (slot_map[XY2OFS(x, y)] == 0 && onScreen(scr, tx, ty, sx, ex, sy, ey)) {
2694 *x_pos = x;
2695 *y_pos = y;
2696 done = 1;
2697 break;
2701 wfree(slot_map);
2702 #undef XY2OFS
2703 return done;
2706 static void moveDock(WDock * dock, int new_x, int new_y)
2708 WAppIcon *btn;
2709 int i;
2711 dock->x_pos = new_x;
2712 dock->y_pos = new_y;
2713 for (i = 0; i < dock->max_icons; i++) {
2714 btn = dock->icon_array[i];
2715 if (btn) {
2716 btn->x_pos = new_x + btn->xindex * ICON_SIZE;
2717 btn->y_pos = new_y + btn->yindex * ICON_SIZE;
2718 XMoveWindow(dpy, btn->icon->core->window, btn->x_pos, btn->y_pos);
2723 static void swapDock(WDock * dock)
2725 WScreen *scr = dock->screen_ptr;
2726 WAppIcon *btn;
2727 int x, i;
2729 if (dock->on_right_side) {
2730 x = dock->x_pos = scr->scr_width - ICON_SIZE - DOCK_EXTRA_SPACE;
2731 } else {
2732 x = dock->x_pos = DOCK_EXTRA_SPACE;
2735 for (i = 0; i < dock->max_icons; i++) {
2736 btn = dock->icon_array[i];
2737 if (btn) {
2738 btn->x_pos = x;
2739 XMoveWindow(dpy, btn->icon->core->window, btn->x_pos, btn->y_pos);
2743 wScreenUpdateUsableArea(scr);
2746 static pid_t execCommand(WAppIcon * btn, char *command, WSavedState * state)
2748 WScreen *scr = btn->icon->core->screen_ptr;
2749 pid_t pid;
2750 char **argv;
2751 int argc;
2752 char *cmdline;
2754 cmdline = ExpandOptions(scr, command);
2756 if (scr->flags.dnd_data_convertion_status || !cmdline) {
2757 if (cmdline)
2758 wfree(cmdline);
2759 if (state)
2760 wfree(state);
2761 return 0;
2764 wtokensplit(cmdline, &argv, &argc);
2766 if (!argc) {
2767 if (cmdline)
2768 wfree(cmdline);
2769 if (state)
2770 wfree(state);
2771 return 0;
2774 if ((pid = fork()) == 0) {
2775 char **args;
2776 int i;
2778 SetupEnvironment(scr);
2780 #ifdef HAVE_SETSID
2781 setsid();
2782 #endif
2784 args = malloc(sizeof(char *) * (argc + 1));
2785 if (!args)
2786 exit(111);
2787 for (i = 0; i < argc; i++) {
2788 args[i] = argv[i];
2790 args[argc] = NULL;
2791 execvp(argv[0], args);
2792 exit(111);
2794 wtokenfree(argv, argc);
2796 if (pid > 0) {
2797 if (!state) {
2798 state = wmalloc(sizeof(WSavedState));
2799 memset(state, 0, sizeof(WSavedState));
2800 state->hidden = -1;
2801 state->miniaturized = -1;
2802 state->shaded = -1;
2803 if (btn->dock == scr->dock || btn->omnipresent)
2804 state->workspace = -1;
2805 else
2806 state->workspace = scr->current_workspace;
2808 wWindowAddSavedState(btn->wm_instance, btn->wm_class, cmdline, pid, state);
2809 wAddDeathHandler(pid, (WDeathHandler *) trackDeadProcess, btn->dock);
2810 } else if (state) {
2811 wfree(state);
2813 wfree(cmdline);
2814 return pid;
2817 void wDockHideIcons(WDock * dock)
2819 int i;
2821 if (dock == NULL)
2822 return;
2824 for (i = 1; i < dock->max_icons; i++) {
2825 if (dock->icon_array[i])
2826 XUnmapWindow(dpy, dock->icon_array[i]->icon->core->window);
2828 dock->mapped = 0;
2830 dockIconPaint(dock->icon_array[0]);
2833 void wDockShowIcons(WDock * dock)
2835 int i, newlevel;
2836 WAppIcon *btn;
2838 if (dock == NULL)
2839 return;
2841 btn = dock->icon_array[0];
2842 moveDock(dock, btn->x_pos, btn->y_pos);
2844 newlevel = dock->lowered ? WMNormalLevel : WMDockLevel;
2845 ChangeStackingLevel(btn->icon->core, newlevel);
2847 for (i = 1; i < dock->max_icons; i++) {
2848 if (dock->icon_array[i]) {
2849 MoveInStackListAbove(dock->icon_array[i]->icon->core, btn->icon->core);
2850 break;
2854 if (!dock->collapsed) {
2855 for (i = 1; i < dock->max_icons; i++) {
2856 if (dock->icon_array[i]) {
2857 XMapWindow(dpy, dock->icon_array[i]->icon->core->window);
2861 dock->mapped = 1;
2863 dockIconPaint(btn);
2866 void wDockLower(WDock * dock)
2868 int i;
2870 for (i = 0; i < dock->max_icons; i++) {
2871 if (dock->icon_array[i])
2872 wLowerFrame(dock->icon_array[i]->icon->core);
2876 void wDockRaise(WDock * dock)
2878 int i;
2880 for (i = dock->max_icons - 1; i >= 0; i--) {
2881 if (dock->icon_array[i])
2882 wRaiseFrame(dock->icon_array[i]->icon->core);
2886 void wDockRaiseLower(WDock * dock)
2888 if (!dock->icon_array[0]->icon->core->stacking->above
2889 || (dock->icon_array[0]->icon->core->stacking->window_level
2890 != dock->icon_array[0]->icon->core->stacking->above->stacking->window_level))
2891 wDockLower(dock);
2892 else
2893 wDockRaise(dock);
2896 void wDockFinishLaunch(WDock * dock, WAppIcon * icon)
2898 icon->launching = 0;
2899 icon->relaunching = 0;
2900 dockIconPaint(icon);
2903 WAppIcon *wDockFindIconForWindow(WDock * dock, Window window)
2905 WAppIcon *icon;
2906 int i;
2908 for (i = 0; i < dock->max_icons; i++) {
2909 icon = dock->icon_array[i];
2910 if (icon && icon->main_window == window)
2911 return icon;
2913 return NULL;
2916 void wDockTrackWindowLaunch(WDock * dock, Window window)
2918 WAppIcon *icon;
2919 char *wm_class, *wm_instance;
2920 int i;
2921 Bool firstPass = True;
2922 Bool found = False;
2923 char *command = NULL;
2925 command = GetCommandForWindow(window);
2927 if (!PropGetWMClass(window, &wm_class, &wm_instance) || (!wm_class && !wm_instance)) {
2929 if (command)
2930 wfree(command);
2931 return;
2934 retry:
2935 for (i = 0; i < dock->max_icons; i++) {
2936 icon = dock->icon_array[i];
2937 if (!icon)
2938 continue;
2940 /* app is already attached to icon */
2941 if (icon->main_window == window) {
2942 found = True;
2943 break;
2946 if ((icon->wm_instance || icon->wm_class)
2947 && (icon->launching || !icon->running)) {
2949 if (icon->wm_instance && wm_instance && strcmp(icon->wm_instance, wm_instance) != 0) {
2950 continue;
2952 if (icon->wm_class && wm_class && strcmp(icon->wm_class, wm_class) != 0) {
2953 continue;
2955 if (firstPass && command && strcmp(icon->command, command) != 0) {
2956 continue;
2959 if (!icon->relaunching) {
2960 WApplication *wapp;
2962 /* Possibly an application that was docked with dockit,
2963 * but the user did not update WMState to indicate that
2964 * it was docked by force */
2965 wapp = wApplicationOf(window);
2966 if (!wapp) {
2967 icon->forced_dock = 1;
2968 icon->running = 0;
2970 if (!icon->forced_dock) {
2971 icon->main_window = window;
2974 found = True;
2975 if (!wPreferences.no_animations && !icon->launching &&
2976 !dock->screen_ptr->flags.startup && !dock->collapsed) {
2977 WAppIcon *aicon;
2978 int x0, y0;
2980 icon->launching = 1;
2981 dockIconPaint(icon);
2983 aicon = wAppIconCreateForDock(dock->screen_ptr, NULL,
2984 wm_instance, wm_class, TILE_NORMAL);
2985 /* XXX: can: aicon->icon == NULL ? */
2986 PlaceIcon(dock->screen_ptr, &x0, &y0, wGetHeadForWindow(aicon->icon->owner));
2987 wAppIconMove(aicon, x0, y0);
2988 /* Should this always be lowered? -Dan */
2989 if (dock->lowered)
2990 wLowerFrame(aicon->icon->core);
2991 XMapWindow(dpy, aicon->icon->core->window);
2992 aicon->launching = 1;
2993 wAppIconPaint(aicon);
2994 SlideWindow(aicon->icon->core->window, x0, y0, icon->x_pos, icon->y_pos);
2995 XUnmapWindow(dpy, aicon->icon->core->window);
2996 wAppIconDestroy(aicon);
2998 wDockFinishLaunch(dock, icon);
2999 break;
3003 if (firstPass && !found) {
3004 firstPass = False;
3005 goto retry;
3008 if (command)
3009 wfree(command);
3011 if (wm_class)
3012 free(wm_class);
3013 if (wm_instance)
3014 free(wm_instance);
3017 void wClipUpdateForWorkspaceChange(WScreen * scr, int workspace)
3019 if (!wPreferences.flags.noclip) {
3020 scr->clip_icon->dock = scr->workspaces[workspace]->clip;
3021 if (scr->current_workspace != workspace) {
3022 WDock *old_clip = scr->workspaces[scr->current_workspace]->clip;
3023 WAppIconChain *chain = scr->global_icons;
3025 while (chain) {
3026 moveIconBetweenDocks(chain->aicon->dock,
3027 scr->workspaces[workspace]->clip,
3028 chain->aicon, chain->aicon->xindex, chain->aicon->yindex);
3029 if (scr->workspaces[workspace]->clip->collapsed)
3030 XUnmapWindow(dpy, chain->aicon->icon->core->window);
3031 chain = chain->next;
3034 wDockHideIcons(old_clip);
3035 if (old_clip->auto_raise_lower) {
3036 if (old_clip->auto_raise_magic) {
3037 WMDeleteTimerHandler(old_clip->auto_raise_magic);
3038 old_clip->auto_raise_magic = NULL;
3040 wDockLower(old_clip);
3042 if (old_clip->auto_collapse) {
3043 if (old_clip->auto_expand_magic) {
3044 WMDeleteTimerHandler(old_clip->auto_expand_magic);
3045 old_clip->auto_expand_magic = NULL;
3047 old_clip->collapsed = 1;
3049 wDockShowIcons(scr->workspaces[workspace]->clip);
3051 if (scr->flags.clip_balloon_mapped)
3052 showClipBalloon(scr->clip_icon->dock, workspace);
3056 static void trackDeadProcess(pid_t pid, unsigned char status, WDock * dock)
3058 WAppIcon *icon;
3059 int i;
3061 for (i = 0; i < dock->max_icons; i++) {
3062 icon = dock->icon_array[i];
3063 if (!icon)
3064 continue;
3066 if (icon->launching && icon->pid == pid) {
3067 if (!icon->relaunching) {
3068 icon->running = 0;
3069 icon->main_window = None;
3071 wDockFinishLaunch(dock, icon);
3072 icon->pid = 0;
3073 if (status == 111) {
3074 char msg[PATH_MAX];
3075 char *cmd;
3077 #ifdef XDND
3078 if (icon->drop_launch)
3079 cmd = icon->dnd_command;
3080 else
3081 #endif
3082 if (icon->paste_launch)
3083 cmd = icon->paste_command;
3084 else
3085 cmd = icon->command;
3087 snprintf(msg, sizeof(msg), _("Could not execute command \"%s\""), cmd);
3089 wMessageDialog(dock->screen_ptr, _("Error"), msg, _("OK"), NULL, NULL);
3091 break;
3096 static void toggleLowered(WDock * dock)
3098 WAppIcon *tmp;
3099 int newlevel, i;
3101 /* lower/raise Dock */
3102 if (!dock->lowered) {
3103 newlevel = WMNormalLevel;
3104 dock->lowered = 1;
3105 } else {
3106 newlevel = WMDockLevel;
3107 dock->lowered = 0;
3110 for (i = 0; i < dock->max_icons; i++) {
3111 tmp = dock->icon_array[i];
3112 if (!tmp)
3113 continue;
3115 ChangeStackingLevel(tmp->icon->core, newlevel);
3116 if (dock->lowered)
3117 wLowerFrame(tmp->icon->core);
3120 if (dock->type == WM_DOCK)
3121 wScreenUpdateUsableArea(dock->screen_ptr);
3124 static void toggleCollapsed(WDock * dock)
3126 if (dock->collapsed) {
3127 dock->collapsed = 0;
3128 wDockShowIcons(dock);
3129 } else {
3130 dock->collapsed = 1;
3131 wDockHideIcons(dock);
3135 static void openDockMenu(WDock * dock, WAppIcon * aicon, XEvent * event)
3137 WScreen *scr = dock->screen_ptr;
3138 WObjDescriptor *desc;
3139 WMenuEntry *entry;
3140 WApplication *wapp = NULL;
3141 int index = 0;
3142 int x_pos;
3143 int n_selected;
3144 int appIsRunning = aicon->running && aicon->icon && aicon->icon->owner;
3146 if (dock->type == WM_DOCK) {
3147 /* keep on top */
3148 entry = dock->menu->entries[index];
3149 entry->flags.indicator_on = !dock->lowered;
3150 entry->clientdata = dock;
3151 dock->menu->flags.realized = 0;
3152 } else {
3153 /* clip options */
3154 if (scr->clip_options)
3155 updateClipOptionsMenu(scr->clip_options, dock);
3157 n_selected = numberOfSelectedIcons(dock);
3159 /* Rename Workspace */
3160 entry = dock->menu->entries[++index];
3161 if (aicon == scr->clip_icon) {
3162 entry->callback = renameCallback;
3163 entry->clientdata = dock;
3164 entry->flags.indicator = 0;
3165 entry->text = _("Rename Workspace");
3166 } else {
3167 entry->callback = omnipresentCallback;
3168 entry->clientdata = aicon;
3169 if (n_selected > 0) {
3170 entry->flags.indicator = 0;
3171 entry->text = _("Toggle Omnipresent");
3172 } else {
3173 entry->flags.indicator = 1;
3174 entry->flags.indicator_on = aicon->omnipresent;
3175 entry->flags.indicator_type = MI_CHECK;
3176 entry->text = _("Omnipresent");
3180 /* select/unselect icon */
3181 entry = dock->menu->entries[++index];
3182 entry->clientdata = aicon;
3183 entry->flags.indicator_on = aicon->icon->selected;
3184 wMenuSetEnabled(dock->menu, index, aicon != scr->clip_icon);
3186 /* select/unselect all icons */
3187 entry = dock->menu->entries[++index];
3188 entry->clientdata = aicon;
3189 if (n_selected > 0)
3190 entry->text = _("Unselect All Icons");
3191 else
3192 entry->text = _("Select All Icons");
3193 wMenuSetEnabled(dock->menu, index, dock->icon_count > 1);
3195 /* keep icon(s) */
3196 entry = dock->menu->entries[++index];
3197 entry->clientdata = aicon;
3198 if (n_selected > 1)
3199 entry->text = _("Keep Icons");
3200 else
3201 entry->text = _("Keep Icon");
3202 wMenuSetEnabled(dock->menu, index, dock->icon_count > 1);
3204 /* this is the workspace submenu part */
3205 entry = dock->menu->entries[++index];
3206 if (n_selected > 1)
3207 entry->text = _("Move Icons To");
3208 else
3209 entry->text = _("Move Icon To");
3210 if (scr->clip_submenu)
3211 updateWorkspaceMenu(scr->clip_submenu, aicon);
3212 wMenuSetEnabled(dock->menu, index, !aicon->omnipresent);
3214 /* remove icon(s) */
3215 entry = dock->menu->entries[++index];
3216 entry->clientdata = aicon;
3217 if (n_selected > 1)
3218 entry->text = _("Remove Icons");
3219 else
3220 entry->text = _("Remove Icon");
3221 wMenuSetEnabled(dock->menu, index, dock->icon_count > 1);
3223 /* attract icon(s) */
3224 entry = dock->menu->entries[++index];
3225 entry->clientdata = aicon;
3227 dock->menu->flags.realized = 0;
3228 wMenuRealize(dock->menu);
3231 if (aicon->icon->owner) {
3232 wapp = wApplicationOf(aicon->icon->owner->main_window);
3233 } else {
3234 wapp = NULL;
3237 /* launch */
3238 entry = dock->menu->entries[++index];
3239 entry->clientdata = aicon;
3240 wMenuSetEnabled(dock->menu, index, aicon->command != NULL);
3242 /* unhide here */
3243 entry = dock->menu->entries[++index];
3244 entry->clientdata = aicon;
3245 if (wapp && wapp->flags.hidden) {
3246 entry->text = _("Unhide Here");
3247 } else {
3248 entry->text = _("Bring Here");
3250 wMenuSetEnabled(dock->menu, index, appIsRunning);
3252 /* hide */
3253 entry = dock->menu->entries[++index];
3254 entry->clientdata = aicon;
3255 if (wapp && wapp->flags.hidden) {
3256 entry->text = _("Unhide");
3257 } else {
3258 entry->text = _("Hide");
3260 wMenuSetEnabled(dock->menu, index, appIsRunning);
3262 /* settings */
3263 entry = dock->menu->entries[++index];
3264 entry->clientdata = aicon;
3265 wMenuSetEnabled(dock->menu, index, !aicon->editing && !wPreferences.flags.noupdates);
3267 /* kill */
3268 entry = dock->menu->entries[++index];
3269 entry->clientdata = aicon;
3270 wMenuSetEnabled(dock->menu, index, appIsRunning);
3272 if (!dock->menu->flags.realized)
3273 wMenuRealize(dock->menu);
3275 if (dock->type == WM_CLIP) {
3276 /*x_pos = event->xbutton.x_root+2; */
3277 x_pos = event->xbutton.x_root - dock->menu->frame->core->width / 2 - 1;
3278 if (x_pos < 0) {
3279 x_pos = 0;
3280 } else if (x_pos + dock->menu->frame->core->width > scr->scr_width - 2) {
3281 x_pos = scr->scr_width - dock->menu->frame->core->width - 4;
3283 } else {
3284 x_pos = dock->on_right_side ? scr->scr_width - dock->menu->frame->core->width - 3 : 0;
3287 wMenuMapAt(dock->menu, x_pos, event->xbutton.y_root + 2, False);
3289 /* allow drag select */
3290 event->xany.send_event = True;
3291 desc = &dock->menu->menu->descriptor;
3292 (*desc->handle_mousedown) (desc, event);
3295 /******************************************************************/
3296 static void iconDblClick(WObjDescriptor * desc, XEvent * event)
3298 WAppIcon *btn = desc->parent;
3299 WDock *dock = btn->dock;
3300 WApplication *wapp = NULL;
3301 int unhideHere = 0;
3303 if (btn->icon->owner && !(event->xbutton.state & ControlMask)) {
3304 wapp = wApplicationOf(btn->icon->owner->main_window);
3306 assert(wapp != NULL);
3308 unhideHere = (event->xbutton.state & ShiftMask);
3310 /* go to the last workspace that the user worked on the app */
3311 if (wapp->last_workspace != dock->screen_ptr->current_workspace && !unhideHere) {
3312 wWorkspaceChange(dock->screen_ptr, wapp->last_workspace);
3315 wUnhideApplication(wapp, event->xbutton.button == Button2, unhideHere);
3317 if (event->xbutton.state & MOD_MASK) {
3318 wHideOtherApplications(btn->icon->owner);
3320 } else {
3321 if (event->xbutton.button == Button1) {
3323 if (event->xbutton.state & MOD_MASK) {
3324 /* raise/lower dock */
3325 toggleLowered(dock);
3326 } else if (btn == dock->screen_ptr->clip_icon) {
3327 if (getClipButton(event->xbutton.x, event->xbutton.y) == CLIP_IDLE)
3328 toggleCollapsed(dock);
3329 else
3330 handleClipChangeWorkspace(dock->screen_ptr, event);
3331 } else if (btn->command) {
3332 if (!btn->launching && (!btn->running || (event->xbutton.state & ControlMask))) {
3333 launchDockedApplication(btn, False);
3335 } else if (btn->xindex == 0 && btn->yindex == 0 && btn->dock->type == WM_DOCK) {
3336 wShowGNUstepPanel(dock->screen_ptr);
3342 static void handleDockMove(WDock * dock, WAppIcon * aicon, XEvent * event)
3344 WScreen *scr = dock->screen_ptr;
3345 int ofs_x = event->xbutton.x, ofs_y = event->xbutton.y;
3346 int x, y;
3347 XEvent ev;
3348 int grabbed = 0, swapped = 0, done;
3349 Pixmap ghost = None;
3350 int superfluous = wPreferences.superfluous; /* we catch it to avoid problems */
3352 if (XGrabPointer(dpy, aicon->icon->core->window, True, ButtonMotionMask
3353 | ButtonReleaseMask | ButtonPressMask, GrabModeAsync,
3354 GrabModeAsync, None, None, CurrentTime) != GrabSuccess) {
3355 wwarning("pointer grab failed for dock move");
3357 y = 0;
3358 for (x = 0; x < dock->max_icons; x++) {
3359 if (dock->icon_array[x] != NULL && dock->icon_array[x]->yindex > y)
3360 y = dock->icon_array[x]->yindex;
3362 y++;
3363 XResizeWindow(dpy, scr->dock_shadow, ICON_SIZE, ICON_SIZE * y);
3365 done = 0;
3366 while (!done) {
3367 WMMaskEvent(dpy, PointerMotionMask | ButtonReleaseMask | ButtonPressMask
3368 | ButtonMotionMask | ExposureMask | EnterWindowMask, &ev);
3369 switch (ev.type) {
3370 case Expose:
3371 WMHandleEvent(&ev);
3372 break;
3374 case EnterNotify:
3375 /* It means the cursor moved so fast that it entered
3376 * something else (if moving slowly, it would have
3377 * stayed in the dock that is being moved. Ignore such
3378 * "spurious" EnterNotifiy's */
3379 break;
3381 case MotionNotify:
3382 if (!grabbed) {
3383 if (abs(ofs_x - ev.xmotion.x) >= MOVE_THRESHOLD
3384 || abs(ofs_y - ev.xmotion.y) >= MOVE_THRESHOLD) {
3385 XChangeActivePointerGrab(dpy, ButtonMotionMask
3386 | ButtonReleaseMask | ButtonPressMask,
3387 wCursor[WCUR_MOVE], CurrentTime);
3388 grabbed = 1;
3390 break;
3392 if (dock->type == WM_CLIP) {
3393 x = ev.xmotion.x_root - ofs_x;
3394 y = ev.xmotion.y_root - ofs_y;
3395 wScreenKeepInside(scr, &x, &y, ICON_SIZE, ICON_SIZE);
3397 moveDock(dock, x, y);
3398 } else {
3399 /* move vertically if pointer is inside the dock */
3400 if ((dock->on_right_side && ev.xmotion.x_root >= dock->x_pos - ICON_SIZE)
3401 || (!dock->on_right_side && ev.xmotion.x_root <= dock->x_pos + ICON_SIZE * 2)) {
3403 x = ev.xmotion.x_root - ofs_x;
3404 y = ev.xmotion.y_root - ofs_y;
3405 wScreenKeepInside(scr, &x, &y, ICON_SIZE, ICON_SIZE);
3406 moveDock(dock, dock->x_pos, y);
3408 /* move horizontally to change sides */
3409 x = ev.xmotion.x_root - ofs_x;
3410 if (!dock->on_right_side) {
3412 /* is on left */
3414 if (ev.xmotion.x_root > dock->x_pos + ICON_SIZE * 2) {
3415 XMoveWindow(dpy, scr->dock_shadow, scr->scr_width - ICON_SIZE
3416 - DOCK_EXTRA_SPACE - 1, dock->y_pos);
3417 if (superfluous && ghost == None) {
3418 ghost = MakeGhostDock(dock, dock->x_pos,
3419 scr->scr_width - ICON_SIZE
3420 - DOCK_EXTRA_SPACE - 1, dock->y_pos);
3421 XSetWindowBackgroundPixmap(dpy, scr->dock_shadow, ghost);
3422 XClearWindow(dpy, scr->dock_shadow);
3424 XMapRaised(dpy, scr->dock_shadow);
3425 swapped = 1;
3426 } else {
3427 if (superfluous && ghost != None) {
3428 XFreePixmap(dpy, ghost);
3429 ghost = None;
3431 XUnmapWindow(dpy, scr->dock_shadow);
3432 swapped = 0;
3434 } else {
3435 /* is on right */
3436 if (ev.xmotion.x_root < dock->x_pos - ICON_SIZE) {
3437 XMoveWindow(dpy, scr->dock_shadow, DOCK_EXTRA_SPACE, dock->y_pos);
3438 if (superfluous && ghost == None) {
3439 ghost = MakeGhostDock(dock, dock->x_pos,
3440 DOCK_EXTRA_SPACE, dock->y_pos);
3441 XSetWindowBackgroundPixmap(dpy, scr->dock_shadow, ghost);
3442 XClearWindow(dpy, scr->dock_shadow);
3444 XMapRaised(dpy, scr->dock_shadow);
3445 swapped = -1;
3446 } else {
3447 XUnmapWindow(dpy, scr->dock_shadow);
3448 swapped = 0;
3449 if (superfluous && ghost != None) {
3450 XFreePixmap(dpy, ghost);
3451 ghost = None;
3456 break;
3458 case ButtonPress:
3459 break;
3461 case ButtonRelease:
3462 if (ev.xbutton.button != event->xbutton.button)
3463 break;
3464 XUngrabPointer(dpy, CurrentTime);
3465 XUnmapWindow(dpy, scr->dock_shadow);
3466 XResizeWindow(dpy, scr->dock_shadow, ICON_SIZE, ICON_SIZE);
3467 if (dock->type == WM_DOCK) {
3468 if (swapped != 0) {
3469 if (swapped > 0)
3470 dock->on_right_side = 1;
3471 else
3472 dock->on_right_side = 0;
3473 swapDock(dock);
3474 wArrangeIcons(scr, False);
3477 done = 1;
3478 break;
3481 if (superfluous) {
3482 if (ghost != None)
3483 XFreePixmap(dpy, ghost);
3484 XSetWindowBackground(dpy, scr->dock_shadow, scr->white_pixel);
3488 static Bool handleIconMove(WDock *dock, WAppIcon *aicon, XEvent *event)
3490 WScreen *scr = dock->screen_ptr;
3491 Window wins[2];
3492 WIcon *icon = aicon->icon;
3493 WDock *dock2 = NULL, *last_dock = dock, *clip = NULL;
3494 int ondock, grabbed = 0, change_dock = 0, collapsed = 0;
3495 XEvent ev;
3496 int x = aicon->x_pos, y = aicon->y_pos;
3497 int ofs_x = event->xbutton.x, ofs_y = event->xbutton.y;
3498 int shad_x = x, shad_y = y;
3499 int ix = aicon->xindex, iy = aicon->yindex;
3500 int tmp;
3501 Pixmap ghost = None;
3502 Bool docked;
3503 int superfluous = wPreferences.superfluous; /* we catch it to avoid problems */
3504 int omnipresent = aicon->omnipresent; /* this must be cached!!! */
3505 Bool hasMoved = False;
3507 if (wPreferences.flags.noupdates)
3508 return hasMoved;
3510 if (XGrabPointer(dpy, icon->core->window, True, ButtonMotionMask
3511 | ButtonReleaseMask | ButtonPressMask, GrabModeAsync,
3512 GrabModeAsync, None, None, CurrentTime) != GrabSuccess) {
3515 if (!(event->xbutton.state & MOD_MASK))
3516 wRaiseFrame(icon->core);
3518 if (!wPreferences.flags.noclip)
3519 clip = scr->workspaces[scr->current_workspace]->clip;
3521 if (dock == scr->dock && !wPreferences.flags.noclip)
3522 dock2 = clip;
3523 else if (dock != scr->dock && !wPreferences.flags.nodock)
3524 dock2 = scr->dock;
3526 wins[0] = icon->core->window;
3527 wins[1] = scr->dock_shadow;
3528 XRestackWindows(dpy, wins, 2);
3529 XMoveResizeWindow(dpy, scr->dock_shadow, aicon->x_pos, aicon->y_pos, ICON_SIZE, ICON_SIZE);
3530 if (superfluous) {
3531 if (icon->pixmap != None)
3532 ghost = MakeGhostIcon(scr, icon->pixmap);
3533 else
3534 ghost = MakeGhostIcon(scr, icon->core->window);
3536 XSetWindowBackgroundPixmap(dpy, scr->dock_shadow, ghost);
3537 XClearWindow(dpy, scr->dock_shadow);
3539 XMapWindow(dpy, scr->dock_shadow);
3541 ondock = 1;
3543 while (1) {
3544 XMaskEvent(dpy, PointerMotionMask | ButtonReleaseMask | ButtonPressMask
3545 | ButtonMotionMask | ExposureMask, &ev);
3546 switch (ev.type) {
3547 case Expose:
3548 WMHandleEvent(&ev);
3549 break;
3551 case MotionNotify:
3552 hasMoved = True;
3553 if (!grabbed) {
3554 if (abs(ofs_x - ev.xmotion.x) >= MOVE_THRESHOLD
3555 || abs(ofs_y - ev.xmotion.y) >= MOVE_THRESHOLD) {
3556 XChangeActivePointerGrab(dpy, ButtonMotionMask
3557 | ButtonReleaseMask | ButtonPressMask,
3558 wCursor[WCUR_MOVE], CurrentTime);
3559 grabbed = 1;
3560 } else {
3561 break;
3565 if (omnipresent) {
3566 int i;
3567 for (i = 0; i < scr->workspace_count; i++) {
3568 if (i == scr->current_workspace)
3569 continue;
3570 wDockShowIcons(scr->workspaces[i]->clip);
3574 x = ev.xmotion.x_root - ofs_x;
3575 y = ev.xmotion.y_root - ofs_y;
3576 tmp = wDockSnapIcon(dock, aicon, x, y, &ix, &iy, True);
3577 if (tmp && dock2) {
3578 change_dock = 0;
3579 if (last_dock != dock && collapsed) {
3580 last_dock->collapsed = 1;
3581 wDockHideIcons(last_dock);
3582 collapsed = 0;
3584 if (!collapsed && (collapsed = dock->collapsed)) {
3585 dock->collapsed = 0;
3586 wDockShowIcons(dock);
3588 if (dock->auto_raise_lower)
3589 wDockRaise(dock);
3590 last_dock = dock;
3591 } else if (dock2) {
3592 tmp = wDockSnapIcon(dock2, aicon, x, y, &ix, &iy, False);
3593 if (tmp) {
3594 change_dock = 1;
3595 if (last_dock != dock2 && collapsed) {
3596 last_dock->collapsed = 1;
3597 wDockHideIcons(last_dock);
3598 collapsed = 0;
3600 if (!collapsed && (collapsed = dock2->collapsed)) {
3601 dock2->collapsed = 0;
3602 wDockShowIcons(dock2);
3604 if (dock2->auto_raise_lower)
3605 wDockRaise(dock2);
3606 last_dock = dock2;
3609 if (aicon->launching || aicon->lock || (aicon->running && !(ev.xmotion.state & MOD_MASK))
3610 || (!aicon->running && tmp)) {
3611 shad_x = last_dock->x_pos + ix * wPreferences.icon_size;
3612 shad_y = last_dock->y_pos + iy * wPreferences.icon_size;
3614 XMoveWindow(dpy, scr->dock_shadow, shad_x, shad_y);
3616 if (!ondock) {
3617 XMapWindow(dpy, scr->dock_shadow);
3619 ondock = 1;
3620 } else {
3621 if (ondock) {
3622 XUnmapWindow(dpy, scr->dock_shadow);
3624 ondock = 0;
3626 XMoveWindow(dpy, icon->core->window, x, y);
3627 break;
3629 case ButtonPress:
3630 break;
3632 case ButtonRelease:
3633 if (ev.xbutton.button != event->xbutton.button)
3634 break;
3635 XUngrabPointer(dpy, CurrentTime);
3636 if (ondock) {
3637 SlideWindow(icon->core->window, x, y, shad_x, shad_y);
3638 XUnmapWindow(dpy, scr->dock_shadow);
3639 if (!change_dock) {
3640 reattachIcon(dock, aicon, ix, iy);
3641 if (clip && dock != clip && clip->auto_raise_lower)
3642 wDockLower(clip);
3643 } else {
3644 docked = moveIconBetweenDocks(dock, dock2, aicon, ix, iy);
3645 if (!docked) {
3646 /* Slide it back if dock rejected it */
3647 SlideWindow(icon->core->window, x, y, aicon->x_pos, aicon->y_pos);
3648 reattachIcon(dock, aicon, aicon->xindex, aicon->yindex);
3650 if (last_dock->type == WM_CLIP && last_dock->auto_collapse) {
3651 collapsed = 0;
3654 } else {
3655 aicon->x_pos = x;
3656 aicon->y_pos = y;
3657 if (superfluous) {
3658 if (!aicon->running && !wPreferences.no_animations) {
3659 /* We need to deselect it, even if is deselected in
3660 * wDockDetach(), because else DoKaboom() will fail.
3662 if (aicon->icon->selected)
3663 wIconSelect(aicon->icon);
3665 DoKaboom(scr, aicon->icon->core->window, x, y);
3668 if (clip && clip->auto_raise_lower)
3669 wDockLower(clip);
3670 wDockDetach(dock, aicon);
3672 if (collapsed) {
3673 last_dock->collapsed = 1;
3674 wDockHideIcons(last_dock);
3675 collapsed = 0;
3677 if (superfluous) {
3678 if (ghost != None)
3679 XFreePixmap(dpy, ghost);
3680 XSetWindowBackground(dpy, scr->dock_shadow, scr->white_pixel);
3682 if (omnipresent) {
3683 int i;
3684 for (i = 0; i < scr->workspace_count; i++) {
3685 if (i == scr->current_workspace)
3686 continue;
3687 wDockHideIcons(scr->workspaces[i]->clip);
3690 return hasMoved;;
3693 return False; /* never reached */
3696 static int getClipButton(int px, int py)
3698 int pt = (CLIP_BUTTON_SIZE + 2) * ICON_SIZE / 64;
3700 if (px < 0 || py < 0 || px >= ICON_SIZE || py >= ICON_SIZE)
3701 return CLIP_IDLE;
3703 if (py <= pt - ((int)ICON_SIZE - 1 - px))
3704 return CLIP_FORWARD;
3705 else if (px <= pt - ((int)ICON_SIZE - 1 - py))
3706 return CLIP_REWIND;
3708 return CLIP_IDLE;
3711 static void handleClipChangeWorkspace(WScreen * scr, XEvent * event)
3713 XEvent ev;
3714 int done, direction, new_ws;
3715 int new_dir;
3716 WDock *clip = scr->clip_icon->dock;
3718 direction = getClipButton(event->xbutton.x, event->xbutton.y);
3720 clip->lclip_button_pushed = direction == CLIP_REWIND;
3721 clip->rclip_button_pushed = direction == CLIP_FORWARD;
3723 wClipIconPaint(scr->clip_icon);
3724 done = 0;
3725 while (!done) {
3726 WMMaskEvent(dpy, ExposureMask | ButtonMotionMask | ButtonReleaseMask | ButtonPressMask, &ev);
3727 switch (ev.type) {
3728 case Expose:
3729 WMHandleEvent(&ev);
3730 break;
3732 case MotionNotify:
3733 new_dir = getClipButton(ev.xmotion.x, ev.xmotion.y);
3734 if (new_dir != direction) {
3735 direction = new_dir;
3736 clip->lclip_button_pushed = direction == CLIP_REWIND;
3737 clip->rclip_button_pushed = direction == CLIP_FORWARD;
3738 wClipIconPaint(scr->clip_icon);
3740 break;
3742 case ButtonPress:
3743 break;
3745 case ButtonRelease:
3746 if (ev.xbutton.button == event->xbutton.button)
3747 done = 1;
3751 clip->lclip_button_pushed = 0;
3752 clip->rclip_button_pushed = 0;
3754 new_ws = wPreferences.ws_advance || (event->xbutton.state & ControlMask);
3756 if (direction == CLIP_FORWARD) {
3757 if (scr->current_workspace < scr->workspace_count - 1)
3758 wWorkspaceChange(scr, scr->current_workspace + 1);
3759 else if (new_ws && scr->current_workspace < MAX_WORKSPACES - 1)
3760 wWorkspaceChange(scr, scr->current_workspace + 1);
3761 else if (wPreferences.ws_cycle)
3762 wWorkspaceChange(scr, 0);
3763 } else if (direction == CLIP_REWIND) {
3764 if (scr->current_workspace > 0)
3765 wWorkspaceChange(scr, scr->current_workspace - 1);
3766 else if (scr->current_workspace == 0 && wPreferences.ws_cycle)
3767 wWorkspaceChange(scr, scr->workspace_count - 1);
3770 wClipIconPaint(scr->clip_icon);
3773 static void iconMouseDown(WObjDescriptor * desc, XEvent * event)
3775 WAppIcon *aicon = desc->parent;
3776 WDock *dock = aicon->dock;
3777 WScreen *scr = aicon->icon->core->screen_ptr;
3779 if (aicon->editing || WCHECK_STATE(WSTATE_MODAL))
3780 return;
3782 scr->last_dock = dock;
3784 if (dock->menu->flags.mapped)
3785 wMenuUnmap(dock->menu);
3787 if (IsDoubleClick(scr, event)) {
3788 /* double-click was not in the main clip icon */
3789 if (dock->type != WM_CLIP || aicon->xindex != 0 || aicon->yindex != 0
3790 || getClipButton(event->xbutton.x, event->xbutton.y) == CLIP_IDLE) {
3791 iconDblClick(desc, event);
3792 return;
3796 if (dock->type == WM_CLIP && scr->flags.clip_balloon_mapped) {
3797 XUnmapWindow(dpy, scr->clip_balloon);
3798 scr->flags.clip_balloon_mapped = 0;
3800 if (event->xbutton.button == Button1) {
3801 if (event->xbutton.state & MOD_MASK)
3802 wDockLower(dock);
3803 else
3804 wDockRaise(dock);
3806 if ((event->xbutton.state & ShiftMask) && aicon != scr->clip_icon && dock->type != WM_DOCK) {
3807 wIconSelect(aicon->icon);
3808 return;
3811 if (aicon->yindex == 0 && aicon->xindex == 0) {
3812 if (getClipButton(event->xbutton.x, event->xbutton.y) != CLIP_IDLE
3813 && dock->type == WM_CLIP)
3814 handleClipChangeWorkspace(scr, event);
3815 else
3816 handleDockMove(dock, aicon, event);
3817 } else {
3818 Bool hasMoved = handleIconMove(dock, aicon, event);
3819 if (wPreferences.single_click && !hasMoved)
3820 iconDblClick(desc, event);
3822 } else if (event->xbutton.button == Button2 && dock->type == WM_CLIP && aicon == scr->clip_icon) {
3823 if (!scr->clip_ws_menu) {
3824 scr->clip_ws_menu = wWorkspaceMenuMake(scr, False);
3826 if (scr->clip_ws_menu) {
3827 WMenu *wsMenu = scr->clip_ws_menu;
3828 int xpos;
3830 wWorkspaceMenuUpdate(scr, wsMenu);
3832 xpos = event->xbutton.x_root - wsMenu->frame->core->width / 2 - 1;
3833 if (xpos < 0) {
3834 xpos = 0;
3835 } else if (xpos + wsMenu->frame->core->width > scr->scr_width - 2) {
3836 xpos = scr->scr_width - wsMenu->frame->core->width - 4;
3838 wMenuMapAt(wsMenu, xpos, event->xbutton.y_root + 2, False);
3840 desc = &wsMenu->menu->descriptor;
3841 event->xany.send_event = True;
3842 (*desc->handle_mousedown) (desc, event);
3844 } else if (event->xbutton.button == Button2 && dock->type == WM_CLIP &&
3845 (event->xbutton.state & ShiftMask) && aicon != scr->clip_icon) {
3846 wClipMakeIconOmnipresent(aicon, !aicon->omnipresent);
3847 } else if (event->xbutton.button == Button3) {
3848 if (event->xbutton.send_event &&
3849 XGrabPointer(dpy, aicon->icon->core->window, True, ButtonMotionMask
3850 | ButtonReleaseMask | ButtonPressMask, GrabModeAsync,
3851 GrabModeAsync, None, None, CurrentTime) != GrabSuccess) {
3852 wwarning("pointer grab failed for dockicon menu");
3853 return;
3856 openDockMenu(dock, aicon, event);
3857 } else if (event->xbutton.button == Button2) {
3858 WAppIcon *btn = desc->parent;
3860 if (!btn->launching && (!btn->running || (event->xbutton.state & ControlMask))) {
3861 launchDockedApplication(btn, True);
3866 static void showClipBalloon(WDock * dock, int workspace)
3868 int w, h;
3869 int x, y;
3870 WScreen *scr = dock->screen_ptr;
3871 char *text;
3872 Window stack[2];
3874 scr->flags.clip_balloon_mapped = 1;
3875 XMapWindow(dpy, scr->clip_balloon);
3877 text = scr->workspaces[workspace]->name;
3879 w = WMWidthOfString(scr->clip_title_font, text, strlen(text));
3881 h = WMFontHeight(scr->clip_title_font);
3882 XResizeWindow(dpy, scr->clip_balloon, w, h);
3884 x = dock->x_pos + CLIP_BUTTON_SIZE * ICON_SIZE / 64;
3885 y = dock->y_pos + ICON_SIZE - WMFontHeight(scr->clip_title_font) - 3;
3887 if (x + w > scr->scr_width) {
3888 x = scr->scr_width - w;
3889 if (dock->y_pos + ICON_SIZE + h > scr->scr_height)
3890 y = dock->y_pos - h - 1;
3891 else
3892 y = dock->y_pos + ICON_SIZE;
3893 XRaiseWindow(dpy, scr->clip_balloon);
3894 } else {
3895 stack[0] = scr->clip_icon->icon->core->window;
3896 stack[1] = scr->clip_balloon;
3897 XRestackWindows(dpy, stack, 2);
3899 XMoveWindow(dpy, scr->clip_balloon, x, y);
3900 XClearWindow(dpy, scr->clip_balloon);
3901 WMDrawString(scr->wmscreen, scr->clip_balloon,
3902 scr->clip_title_color[CLIP_NORMAL], scr->clip_title_font, 0, 0, text, strlen(text));
3905 static void clipEnterNotify(WObjDescriptor * desc, XEvent * event)
3907 WAppIcon *btn = (WAppIcon *) desc->parent;
3908 WDock *dock;
3909 WScreen *scr;
3911 assert(event->type == EnterNotify);
3913 if (desc->parent_type != WCLASS_DOCK_ICON)
3914 return;
3916 scr = btn->icon->core->screen_ptr;
3917 if (!btn->omnipresent)
3918 dock = btn->dock;
3919 else
3920 dock = scr->workspaces[scr->current_workspace]->clip;
3922 if (!dock || dock->type != WM_CLIP)
3923 return;
3925 /* The auto raise/lower code */
3926 if (dock->auto_lower_magic) {
3927 WMDeleteTimerHandler(dock->auto_lower_magic);
3928 dock->auto_lower_magic = NULL;
3930 if (dock->auto_raise_lower && !dock->auto_raise_magic) {
3931 dock->auto_raise_magic = WMAddTimerHandler(AUTO_RAISE_DELAY, clipAutoRaise, (void *)dock);
3934 /* The auto expand/collapse code */
3935 if (dock->auto_collapse_magic) {
3936 WMDeleteTimerHandler(dock->auto_collapse_magic);
3937 dock->auto_collapse_magic = NULL;
3939 if (dock->auto_collapse && !dock->auto_expand_magic) {
3940 dock->auto_expand_magic = WMAddTimerHandler(AUTO_EXPAND_DELAY, clipAutoExpand, (void *)dock);
3943 if (btn->xindex == 0 && btn->yindex == 0)
3944 showClipBalloon(dock, dock->screen_ptr->current_workspace);
3945 else {
3946 if (dock->screen_ptr->flags.clip_balloon_mapped) {
3947 XUnmapWindow(dpy, dock->screen_ptr->clip_balloon);
3948 dock->screen_ptr->flags.clip_balloon_mapped = 0;
3953 static void clipLeave(WDock * dock)
3955 XEvent event;
3956 WObjDescriptor *desc = NULL;
3958 if (!dock || dock->type != WM_CLIP)
3959 return;
3961 if (XCheckTypedEvent(dpy, EnterNotify, &event) != False) {
3962 if (XFindContext(dpy, event.xcrossing.window, wWinContext,
3963 (XPointer *) & desc) != XCNOENT
3964 && desc && desc->parent_type == WCLASS_DOCK_ICON
3965 && ((WAppIcon *) desc->parent)->dock && ((WAppIcon *) desc->parent)->dock->type == WM_CLIP) {
3966 /* We didn't left the Clip yet */
3967 XPutBackEvent(dpy, &event);
3968 return;
3971 XPutBackEvent(dpy, &event);
3972 } else {
3973 /* We entered a withdrawn window, so we're still in Clip */
3974 return;
3977 if (dock->auto_raise_magic) {
3978 WMDeleteTimerHandler(dock->auto_raise_magic);
3979 dock->auto_raise_magic = NULL;
3981 if (dock->auto_raise_lower && !dock->auto_lower_magic) {
3982 dock->auto_lower_magic = WMAddTimerHandler(AUTO_LOWER_DELAY, clipAutoLower, (void *)dock);
3985 if (dock->auto_expand_magic) {
3986 WMDeleteTimerHandler(dock->auto_expand_magic);
3987 dock->auto_expand_magic = NULL;
3989 if (dock->auto_collapse && !dock->auto_collapse_magic) {
3990 dock->auto_collapse_magic = WMAddTimerHandler(AUTO_COLLAPSE_DELAY, clipAutoCollapse, (void *)dock);
3994 static void clipLeaveNotify(WObjDescriptor * desc, XEvent * event)
3996 WAppIcon *btn = (WAppIcon *) desc->parent;
3998 assert(event->type == LeaveNotify);
4000 if (desc->parent_type != WCLASS_DOCK_ICON)
4001 return;
4003 clipLeave(btn->dock);
4006 static void clipAutoCollapse(void *cdata)
4008 WDock *dock = (WDock *) cdata;
4010 if (dock->type != WM_CLIP)
4011 return;
4013 if (dock->auto_collapse) {
4014 dock->collapsed = 1;
4015 wDockHideIcons(dock);
4017 dock->auto_collapse_magic = NULL;
4020 static void clipAutoExpand(void *cdata)
4022 WDock *dock = (WDock *) cdata;
4024 if (dock->type != WM_CLIP)
4025 return;
4027 if (dock->auto_collapse) {
4028 dock->collapsed = 0;
4029 wDockShowIcons(dock);
4031 dock->auto_expand_magic = NULL;
4034 static void clipAutoLower(void *cdata)
4036 WDock *dock = (WDock *) cdata;
4038 if (dock->type != WM_CLIP)
4039 return;
4041 if (dock->auto_raise_lower)
4042 wDockLower(dock);
4044 dock->auto_lower_magic = NULL;
4047 static void clipAutoRaise(void *cdata)
4049 WDock *dock = (WDock *) cdata;
4051 if (dock->type != WM_CLIP)
4052 return;
4054 if (dock->auto_raise_lower)
4055 wDockRaise(dock);
4057 if (dock->screen_ptr->flags.clip_balloon_mapped) {
4058 showClipBalloon(dock, dock->screen_ptr->current_workspace);
4061 dock->auto_raise_magic = NULL;
4064 static Bool iconCanBeOmnipresent(WAppIcon * aicon)
4066 WScreen *scr = aicon->icon->core->screen_ptr;
4067 WDock *clip;
4068 WAppIcon *btn;
4069 int i, j;
4071 for (i = 0; i < scr->workspace_count; i++) {
4072 clip = scr->workspaces[i]->clip;
4074 if (clip == aicon->dock)
4075 continue;
4077 if (clip->icon_count + scr->global_icon_count >= clip->max_icons)
4078 return False; /* Clip is full in some workspace */
4080 for (j = 0; j < clip->max_icons; j++) {
4081 btn = clip->icon_array[j];
4082 if (btn && btn->xindex == aicon->xindex && btn->yindex == aicon->yindex)
4083 return False;
4087 return True;
4090 int wClipMakeIconOmnipresent(WAppIcon * aicon, int omnipresent)
4092 WScreen *scr = aicon->icon->core->screen_ptr;
4093 WAppIconChain *new_entry, *tmp, *tmp1;
4094 int status = WO_SUCCESS;
4096 if ((scr->dock && aicon->dock == scr->dock) || aicon == scr->clip_icon) {
4097 return WO_NOT_APPLICABLE;
4100 if (aicon->omnipresent == omnipresent)
4101 return WO_SUCCESS;
4103 if (omnipresent) {
4104 if (iconCanBeOmnipresent(aicon)) {
4105 aicon->omnipresent = 1;
4106 new_entry = wmalloc(sizeof(WAppIconChain));
4107 new_entry->aicon = aicon;
4108 new_entry->next = scr->global_icons;
4109 scr->global_icons = new_entry;
4110 scr->global_icon_count++;
4111 } else {
4112 aicon->omnipresent = 0;
4113 status = WO_FAILED;
4115 } else {
4116 aicon->omnipresent = 0;
4117 if (aicon == scr->global_icons->aicon) {
4118 tmp = scr->global_icons->next;
4119 wfree(scr->global_icons);
4120 scr->global_icons = tmp;
4121 scr->global_icon_count--;
4122 } else {
4123 tmp = scr->global_icons;
4124 while (tmp->next) {
4125 if (tmp->next->aicon == aicon) {
4126 tmp1 = tmp->next->next;
4127 wfree(tmp->next);
4128 tmp->next = tmp1;
4129 scr->global_icon_count--;
4130 break;
4132 tmp = tmp->next;
4137 wAppIconPaint(aicon);
4139 return status;