Ungeneralize wrlib DEBUG
[wmaker-crm.git] / src / dock.c
blob3df2759d8c4e5e0521328c0e44d8ddff08fa8b55
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
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
21 * USA.
24 #include "wconfig.h"
26 #include <X11/Xlib.h>
27 #include <X11/Xutil.h>
28 #include <stdlib.h>
29 #include <string.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);
580 WMFreeArray(selectedIcons);
583 static void toggleAutoAttractCallback(WMenu * menu, WMenuEntry * entry)
585 WDock *dock = (WDock *) entry->clientdata;
587 assert(entry->clientdata != NULL);
589 dock->attract_icons = !dock->attract_icons;
590 /*if (!dock->attract_icons)
591 dock->keep_attracted = 0; */
593 entry->flags.indicator_on = dock->attract_icons;
595 wMenuPaint(menu);
598 static void selectCallback(WMenu * menu, WMenuEntry * entry)
600 WAppIcon *icon = (WAppIcon *) entry->clientdata;
602 assert(icon != NULL);
604 wIconSelect(icon->icon);
606 wMenuPaint(menu);
609 static void colectIconsCallback(WMenu * menu, WMenuEntry * entry)
611 WAppIcon *clickedIcon = (WAppIcon *) entry->clientdata;
612 WDock *clip;
613 WAppIcon *aicon;
614 int x, y, x_pos, y_pos;
616 assert(entry->clientdata != NULL);
617 clip = clickedIcon->dock;
619 aicon = clip->screen_ptr->app_icon_list;
621 while (aicon) {
622 if (!aicon->docked && wDockFindFreeSlot(clip, &x, &y)) {
623 x_pos = clip->x_pos + x * ICON_SIZE;
624 y_pos = clip->y_pos + y * ICON_SIZE;
625 if (aicon->x_pos != x_pos || aicon->y_pos != y_pos) {
626 #ifdef ANIMATIONS
627 if (wPreferences.no_animations) {
628 XMoveWindow(dpy, aicon->icon->core->window, x_pos, y_pos);
629 } else {
630 SlideWindow(aicon->icon->core->window,
631 aicon->x_pos, aicon->y_pos, x_pos, y_pos);
633 #else
634 XMoveWindow(dpy, aicon->icon->core->window, x_pos, y_pos);
635 #endif /* ANIMATIONS */
637 aicon->attracted = 1;
638 if (!aicon->icon->shadowed) {
639 aicon->icon->shadowed = 1;
640 aicon->icon->force_paint = 1;
641 /* We don't do an wAppIconPaint() here because it's in
642 * wDockAttachIcon(). -Dan
645 wDockAttachIcon(clip, aicon, x, y);
646 if (clip->collapsed || !clip->mapped)
647 XUnmapWindow(dpy, aicon->icon->core->window);
649 aicon = aicon->next;
653 static void selectIconsCallback(WMenu * menu, WMenuEntry * entry)
655 WAppIcon *clickedIcon = (WAppIcon *) entry->clientdata;
656 WDock *dock;
657 WMArray *selectedIcons;
658 WMArrayIterator iter;
659 WAppIcon *btn;
660 int i;
662 assert(clickedIcon != NULL);
663 dock = clickedIcon->dock;
665 selectedIcons = getSelected(dock);
667 if (!WMGetArrayItemCount(selectedIcons)) {
668 for (i = 1; i < dock->max_icons; i++) {
669 btn = dock->icon_array[i];
670 if (btn && !btn->icon->selected) {
671 wIconSelect(btn->icon);
674 } else {
675 WM_ITERATE_ARRAY(selectedIcons, btn, iter) {
676 wIconSelect(btn->icon);
679 WMFreeArray(selectedIcons);
681 wMenuPaint(menu);
684 static void toggleCollapsedCallback(WMenu * menu, WMenuEntry * entry)
686 assert(entry->clientdata != NULL);
688 toggleCollapsed(entry->clientdata);
690 entry->flags.indicator_on = ((WDock *) entry->clientdata)->collapsed;
692 wMenuPaint(menu);
695 static void toggleAutoCollapseCallback(WMenu * menu, WMenuEntry * entry)
697 WDock *dock;
698 assert(entry->clientdata != NULL);
700 dock = (WDock *) entry->clientdata;
702 dock->auto_collapse = !dock->auto_collapse;
704 entry->flags.indicator_on = ((WDock *) entry->clientdata)->auto_collapse;
706 wMenuPaint(menu);
709 static void toggleAutoRaiseLowerCallback(WMenu * menu, WMenuEntry * entry)
711 WDock *dock;
712 assert(entry->clientdata != NULL);
714 dock = (WDock *) entry->clientdata;
716 dock->auto_raise_lower = !dock->auto_raise_lower;
718 entry->flags.indicator_on = ((WDock *) entry->clientdata)->auto_raise_lower;
720 wMenuPaint(menu);
723 static void launchCallback(WMenu * menu, WMenuEntry * entry)
725 WAppIcon *btn = (WAppIcon *) entry->clientdata;
727 launchDockedApplication(btn, False);
730 static void settingsCallback(WMenu * menu, WMenuEntry * entry)
732 WAppIcon *btn = (WAppIcon *) entry->clientdata;
734 if (btn->editing)
735 return;
736 ShowDockAppSettingsPanel(btn);
739 static void hideCallback(WMenu * menu, WMenuEntry * entry)
741 WApplication *wapp;
742 WAppIcon *btn = (WAppIcon *) entry->clientdata;
744 wapp = wApplicationOf(btn->icon->owner->main_window);
746 if (wapp->flags.hidden) {
747 wWorkspaceChange(btn->icon->core->screen_ptr, wapp->last_workspace);
748 wUnhideApplication(wapp, False, False);
749 } else {
750 wHideApplication(wapp);
754 static void unhideHereCallback(WMenu * menu, WMenuEntry * entry)
756 WApplication *wapp;
757 WAppIcon *btn = (WAppIcon *) entry->clientdata;
759 wapp = wApplicationOf(btn->icon->owner->main_window);
761 wUnhideApplication(wapp, False, True);
764 WAppIcon *mainIconCreate(WScreen * scr, int type)
766 WAppIcon *btn;
767 int x_pos;
769 if (type == WM_CLIP) {
770 if (scr->clip_icon)
771 return scr->clip_icon;
772 btn = wAppIconCreateForDock(scr, NULL, "Logo", "WMClip", TILE_CLIP);
773 btn->icon->core->descriptor.handle_expose = clipIconExpose;
774 btn->icon->core->descriptor.handle_enternotify = clipEnterNotify;
775 btn->icon->core->descriptor.handle_leavenotify = clipLeaveNotify;
776 /*x_pos = scr->scr_width - ICON_SIZE*2 - DOCK_EXTRA_SPACE; */
777 x_pos = 0;
778 } else {
779 btn = wAppIconCreateForDock(scr, NULL, "Logo", "WMDock", TILE_NORMAL);
780 x_pos = scr->scr_width - ICON_SIZE - DOCK_EXTRA_SPACE;
783 btn->xindex = 0;
784 btn->yindex = 0;
786 btn->icon->core->descriptor.handle_mousedown = iconMouseDown;
787 btn->icon->core->descriptor.parent_type = WCLASS_DOCK_ICON;
788 btn->icon->core->descriptor.parent = btn;
789 /*ChangeStackingLevel(btn->icon->core, WMDockLevel); */
790 XMapWindow(dpy, btn->icon->core->window);
791 btn->x_pos = x_pos;
792 btn->y_pos = 0;
793 btn->docked = 1;
794 if (type == WM_CLIP)
795 scr->clip_icon = btn;
797 return btn;
800 static void switchWSCommand(WMenu * menu, WMenuEntry * entry)
802 WAppIcon *btn, *icon = (WAppIcon *) entry->clientdata;
803 WScreen *scr = icon->icon->core->screen_ptr;
804 WDock *src, *dest;
805 WMArray *selectedIcons;
806 int x, y;
808 if (entry->order == scr->current_workspace)
809 return;
810 src = icon->dock;
811 dest = scr->workspaces[entry->order]->clip;
813 selectedIcons = getSelected(src);
815 if (WMGetArrayItemCount(selectedIcons)) {
816 WMArrayIterator iter;
818 WM_ITERATE_ARRAY(selectedIcons, btn, iter) {
819 if (wDockFindFreeSlot(dest, &x, &y)) {
820 moveIconBetweenDocks(src, dest, btn, x, y);
821 XUnmapWindow(dpy, btn->icon->core->window);
824 } else if (icon != scr->clip_icon) {
825 if (wDockFindFreeSlot(dest, &x, &y)) {
826 moveIconBetweenDocks(src, dest, icon, x, y);
827 XUnmapWindow(dpy, icon->icon->core->window);
830 WMFreeArray(selectedIcons);
833 static void launchDockedApplication(WAppIcon * btn, Bool withSelection)
835 WScreen *scr = btn->icon->core->screen_ptr;
837 if (!btn->launching &&
838 ((!withSelection && btn->command != NULL) || (withSelection && btn->paste_command != NULL))) {
839 if (!btn->forced_dock) {
840 btn->relaunching = btn->running;
841 btn->running = 1;
843 if (btn->wm_instance || btn->wm_class) {
844 WWindowAttributes attr;
845 memset(&attr, 0, sizeof(WWindowAttributes));
846 wDefaultFillAttributes(scr, btn->wm_instance, btn->wm_class, &attr, NULL, True);
848 if (!attr.no_appicon && !btn->buggy_app)
849 btn->launching = 1;
850 else
851 btn->running = 0;
853 btn->drop_launch = 0;
854 btn->paste_launch = withSelection;
855 scr->last_dock = btn->dock;
856 btn->pid = execCommand(btn, (withSelection ? btn->paste_command : btn->command), NULL);
857 if (btn->pid > 0) {
858 if (btn->buggy_app) {
859 /* give feedback that the app was launched */
860 btn->launching = 1;
861 dockIconPaint(btn);
862 btn->launching = 0;
863 WMAddTimerHandler(200, (WMCallback *) dockIconPaint, btn);
864 } else {
865 dockIconPaint(btn);
867 } else {
868 wwarning(_("could not launch application %s\n"), btn->command);
869 btn->launching = 0;
870 if (!btn->relaunching) {
871 btn->running = 0;
877 static void updateWorkspaceMenu(WMenu * menu, WAppIcon * icon)
879 WScreen *scr = menu->frame->screen_ptr;
880 char title[MAX_WORKSPACENAME_WIDTH + 1];
881 int i;
883 if (!menu || !icon)
884 return;
886 for (i = 0; i < scr->workspace_count; i++) {
887 if (i < menu->entry_no) {
888 if (strcmp(menu->entries[i]->text, scr->workspaces[i]->name) != 0) {
889 wfree(menu->entries[i]->text);
890 strcpy(title, scr->workspaces[i]->name);
891 menu->entries[i]->text = wstrdup(title);
892 menu->flags.realized = 0;
894 menu->entries[i]->clientdata = (void *)icon;
895 } else {
896 strcpy(title, scr->workspaces[i]->name);
898 wMenuAddCallback(menu, title, switchWSCommand, (void *)icon);
900 menu->flags.realized = 0;
902 if (i == scr->current_workspace) {
903 wMenuSetEnabled(menu, i, False);
904 } else {
905 wMenuSetEnabled(menu, i, True);
909 if (!menu->flags.realized)
910 wMenuRealize(menu);
913 static WMenu *makeWorkspaceMenu(WScreen * scr)
915 WMenu *menu;
917 menu = wMenuCreate(scr, NULL, False);
918 if (!menu)
919 wwarning(_("could not create workspace submenu for Clip menu"));
921 wMenuAddCallback(menu, "", switchWSCommand, (void *)scr->clip_icon);
923 menu->flags.realized = 0;
924 wMenuRealize(menu);
926 return menu;
929 static void updateClipOptionsMenu(WMenu * menu, WDock * dock)
931 WMenuEntry *entry;
932 int index = 0;
934 if (!menu || !dock)
935 return;
937 /* keep on top */
938 entry = menu->entries[index];
939 entry->flags.indicator_on = !dock->lowered;
940 entry->clientdata = dock;
942 /* collapsed */
943 entry = menu->entries[++index];
944 entry->flags.indicator_on = dock->collapsed;
945 entry->clientdata = dock;
947 /* auto-collapse */
948 entry = menu->entries[++index];
949 entry->flags.indicator_on = dock->auto_collapse;
950 entry->clientdata = dock;
952 /* auto-raise/lower */
953 entry = menu->entries[++index];
954 entry->flags.indicator_on = dock->auto_raise_lower;
955 entry->clientdata = dock;
956 wMenuSetEnabled(menu, index, dock->lowered);
958 /* attract icons */
959 entry = menu->entries[++index];
960 entry->flags.indicator_on = dock->attract_icons;
961 entry->clientdata = dock;
963 menu->flags.realized = 0;
964 wMenuRealize(menu);
967 static WMenu *makeClipOptionsMenu(WScreen * scr)
969 WMenu *menu;
970 WMenuEntry *entry;
972 menu = wMenuCreate(scr, NULL, False);
973 if (!menu) {
974 wwarning(_("could not create options submenu for Clip menu"));
975 return NULL;
978 entry = wMenuAddCallback(menu, _("Keep on Top"), toggleLoweredCallback, NULL);
979 entry->flags.indicator = 1;
980 entry->flags.indicator_on = 1;
981 entry->flags.indicator_type = MI_CHECK;
983 entry = wMenuAddCallback(menu, _("Collapsed"), toggleCollapsedCallback, NULL);
984 entry->flags.indicator = 1;
985 entry->flags.indicator_on = 1;
986 entry->flags.indicator_type = MI_CHECK;
988 entry = wMenuAddCallback(menu, _("Autocollapse"), toggleAutoCollapseCallback, NULL);
989 entry->flags.indicator = 1;
990 entry->flags.indicator_on = 1;
991 entry->flags.indicator_type = MI_CHECK;
993 entry = wMenuAddCallback(menu, _("Autoraise"), toggleAutoRaiseLowerCallback, NULL);
994 entry->flags.indicator = 1;
995 entry->flags.indicator_on = 1;
996 entry->flags.indicator_type = MI_CHECK;
998 entry = wMenuAddCallback(menu, _("Autoattract Icons"), toggleAutoAttractCallback, NULL);
999 entry->flags.indicator = 1;
1000 entry->flags.indicator_on = 1;
1001 entry->flags.indicator_type = MI_CHECK;
1003 menu->flags.realized = 0;
1004 wMenuRealize(menu);
1006 return menu;
1009 static WMenu *dockMenuCreate(WScreen * scr, int type)
1011 WMenu *menu;
1012 WMenuEntry *entry;
1014 if (type == WM_CLIP && scr->clip_menu)
1015 return scr->clip_menu;
1017 menu = wMenuCreate(scr, NULL, False);
1018 if (type != WM_CLIP) {
1019 entry = wMenuAddCallback(menu, _("Keep on Top"), toggleLoweredCallback, NULL);
1020 entry->flags.indicator = 1;
1021 entry->flags.indicator_on = 1;
1022 entry->flags.indicator_type = MI_CHECK;
1023 } else {
1024 entry = wMenuAddCallback(menu, _("Clip Options"), NULL, NULL);
1025 scr->clip_options = makeClipOptionsMenu(scr);
1026 if (scr->clip_options)
1027 wMenuEntrySetCascade(menu, entry, scr->clip_options);
1029 entry = wMenuAddCallback(menu, _("Rename Workspace"), renameCallback, NULL);
1030 wfree(entry->text);
1031 entry->text = _("Rename Workspace");
1033 entry = wMenuAddCallback(menu, _("Selected"), selectCallback, NULL);
1034 entry->flags.indicator = 1;
1035 entry->flags.indicator_on = 1;
1036 entry->flags.indicator_type = MI_CHECK;
1038 entry = wMenuAddCallback(menu, _("Select All Icons"), selectIconsCallback, NULL);
1039 wfree(entry->text);
1040 entry->text = _("Select All Icons");
1042 entry = wMenuAddCallback(menu, _("Keep Icon"), keepIconsCallback, NULL);
1043 wfree(entry->text);
1044 entry->text = _("Keep Icon");
1046 entry = wMenuAddCallback(menu, _("Move Icon To"), NULL, NULL);
1047 wfree(entry->text);
1048 entry->text = _("Move Icon To");
1049 scr->clip_submenu = makeWorkspaceMenu(scr);
1050 if (scr->clip_submenu)
1051 wMenuEntrySetCascade(menu, entry, scr->clip_submenu);
1053 entry = wMenuAddCallback(menu, _("Remove Icon"), removeIconsCallback, NULL);
1054 wfree(entry->text);
1055 entry->text = _("Remove Icon");
1057 wMenuAddCallback(menu, _("Attract Icons"), colectIconsCallback, NULL);
1060 wMenuAddCallback(menu, _("Launch"), launchCallback, NULL);
1062 wMenuAddCallback(menu, _("Unhide Here"), unhideHereCallback, NULL);
1064 entry = wMenuAddCallback(menu, _("Hide"), hideCallback, NULL);
1065 wfree(entry->text);
1066 entry->text = _("Hide");
1068 wMenuAddCallback(menu, _("Settings..."), settingsCallback, NULL);
1070 wMenuAddCallback(menu, _("Kill"), killCallback, NULL);
1072 if (type == WM_CLIP)
1073 scr->clip_menu = menu;
1075 return menu;
1078 WDock *wDockCreate(WScreen * scr, int type)
1080 WDock *dock;
1081 WAppIcon *btn;
1082 int icon_count;
1084 make_keys();
1086 dock = wmalloc(sizeof(WDock));
1087 memset(dock, 0, sizeof(WDock));
1089 if (type == WM_CLIP)
1090 icon_count = CLIP_MAX_ICONS;
1091 else
1092 icon_count = scr->scr_height / wPreferences.icon_size;
1094 dock->icon_array = wmalloc(sizeof(WAppIcon *) * icon_count);
1095 memset(dock->icon_array, 0, sizeof(WAppIcon *) * icon_count);
1097 dock->max_icons = icon_count;
1099 btn = mainIconCreate(scr, type);
1101 btn->dock = dock;
1103 dock->x_pos = btn->x_pos;
1104 dock->y_pos = btn->y_pos;
1105 dock->screen_ptr = scr;
1106 dock->type = type;
1107 dock->icon_count = 1;
1108 dock->on_right_side = 1;
1109 dock->collapsed = 0;
1110 dock->auto_collapse = 0;
1111 dock->auto_collapse_magic = NULL;
1112 dock->auto_raise_lower = 0;
1113 dock->auto_lower_magic = NULL;
1114 dock->auto_raise_magic = NULL;
1115 dock->attract_icons = 0;
1116 dock->lowered = 1;
1117 dock->icon_array[0] = btn;
1118 wRaiseFrame(btn->icon->core);
1119 XMoveWindow(dpy, btn->icon->core->window, btn->x_pos, btn->y_pos);
1121 /* create dock menu */
1122 dock->menu = dockMenuCreate(scr, type);
1124 return dock;
1127 void wDockDestroy(WDock * dock)
1129 int i;
1130 WAppIcon *aicon;
1132 for (i = (dock->type == WM_CLIP) ? 1 : 0; i < dock->max_icons; i++) {
1133 aicon = dock->icon_array[i];
1134 if (aicon) {
1135 int keepit = aicon->running && wApplicationOf(aicon->main_window);
1136 wDockDetach(dock, aicon);
1137 if (keepit) {
1138 /* XXX: can: aicon->icon == NULL ? */
1139 PlaceIcon(dock->screen_ptr, &aicon->x_pos, &aicon->y_pos,
1140 wGetHeadForWindow(aicon->icon->owner));
1141 XMoveWindow(dpy, aicon->icon->core->window, aicon->x_pos, aicon->y_pos);
1142 if (!dock->mapped || dock->collapsed)
1143 XMapWindow(dpy, aicon->icon->core->window);
1147 if (wPreferences.auto_arrange_icons)
1148 wArrangeIcons(dock->screen_ptr, True);
1149 wfree(dock->icon_array);
1150 if (dock->menu && dock->type != WM_CLIP)
1151 wMenuDestroy(dock->menu, True);
1152 if (dock->screen_ptr->last_dock == dock)
1153 dock->screen_ptr->last_dock = NULL;
1154 wfree(dock);
1157 void wClipIconPaint(WAppIcon * aicon)
1159 WScreen *scr = aicon->icon->core->screen_ptr;
1160 WWorkspace *workspace = scr->workspaces[scr->current_workspace];
1161 WMColor *color;
1162 Window win = aicon->icon->core->window;
1163 int length, nlength;
1164 char *ws_name, ws_number[10];
1165 int ty, tx;
1167 wIconPaint(aicon->icon);
1169 length = strlen(workspace->name);
1170 ws_name = wmalloc(length + 1);
1171 snprintf(ws_name, length + 1, "%s", workspace->name);
1172 snprintf(ws_number, sizeof(ws_number), "%i", scr->current_workspace + 1);
1173 nlength = strlen(ws_number);
1175 if (!workspace->clip->collapsed)
1176 color = scr->clip_title_color[CLIP_NORMAL];
1177 else
1178 color = scr->clip_title_color[CLIP_COLLAPSED];
1180 ty = ICON_SIZE - WMFontHeight(scr->clip_title_font) - 3;
1182 tx = CLIP_BUTTON_SIZE * ICON_SIZE / 64;
1184 if(wPreferences.show_clip_title)
1185 WMDrawString(scr->wmscreen, win, color, scr->clip_title_font, tx, ty, ws_name, length);
1186 /*WMDrawString(scr->wmscreen, win, color, scr->clip_title_font, 4,
1187 2, ws_name, length); */
1189 tx = (ICON_SIZE / 2 - WMWidthOfString(scr->clip_title_font, ws_number, nlength)) / 2;
1191 WMDrawString(scr->wmscreen, win, color, scr->clip_title_font, tx, 2, ws_number, nlength);
1193 wfree(ws_name);
1195 if (aicon->launching) {
1196 XFillRectangle(dpy, aicon->icon->core->window, scr->stipple_gc,
1197 0, 0, wPreferences.icon_size, wPreferences.icon_size);
1199 paintClipButtons(aicon, aicon->dock->lclip_button_pushed, aicon->dock->rclip_button_pushed);
1202 static void clipIconExpose(WObjDescriptor * desc, XEvent * event)
1204 wClipIconPaint(desc->parent);
1207 static void dockIconPaint(WAppIcon * btn)
1209 if (btn == btn->icon->core->screen_ptr->clip_icon)
1210 wClipIconPaint(btn);
1211 else
1212 wAppIconPaint(btn);
1215 static WMPropList *make_icon_state(WAppIcon * btn)
1217 WMPropList *node = NULL;
1218 WMPropList *command, *autolaunch, *lock, *name, *forced, *host;
1219 WMPropList *position, *buggy, *omnipresent;
1220 char *tmp;
1221 char buffer[64];
1223 if (btn) {
1224 if (!btn->command)
1225 command = WMCreatePLString("-");
1226 else
1227 command = WMCreatePLString(btn->command);
1229 autolaunch = btn->auto_launch ? dYes : dNo;
1231 lock = btn->lock ? dYes : dNo;
1233 tmp = EscapeWM_CLASS(btn->wm_instance, btn->wm_class);
1235 name = WMCreatePLString(tmp);
1237 wfree(tmp);
1239 forced = btn->forced_dock ? dYes : dNo;
1241 buggy = btn->buggy_app ? dYes : dNo;
1243 if (btn == btn->icon->core->screen_ptr->clip_icon)
1244 snprintf(buffer, sizeof(buffer), "%i,%i", btn->x_pos, btn->y_pos);
1245 else
1246 snprintf(buffer, sizeof(buffer), "%hi,%hi", btn->xindex, btn->yindex);
1247 position = WMCreatePLString(buffer);
1249 node = WMCreatePLDictionary(dCommand, command,
1250 dName, name,
1251 dAutoLaunch, autolaunch,
1252 dLock, lock,
1253 dForced, forced, dBuggyApplication, buggy, dPosition, position, NULL);
1254 WMReleasePropList(command);
1255 WMReleasePropList(name);
1256 WMReleasePropList(position);
1258 omnipresent = btn->omnipresent ? dYes : dNo;
1259 if (btn->dock != btn->icon->core->screen_ptr->dock && (btn->xindex != 0 || btn->yindex != 0))
1260 WMPutInPLDictionary(node, dOmnipresent, omnipresent);
1262 #ifdef XDND /* was OFFIX */
1263 if (btn->dnd_command) {
1264 command = WMCreatePLString(btn->dnd_command);
1265 WMPutInPLDictionary(node, dDropCommand, command);
1266 WMReleasePropList(command);
1268 #endif /* XDND */
1270 if (btn->paste_command) {
1271 command = WMCreatePLString(btn->paste_command);
1272 WMPutInPLDictionary(node, dPasteCommand, command);
1273 WMReleasePropList(command);
1276 if (btn->client_machine && btn->remote_start) {
1277 host = WMCreatePLString(btn->client_machine);
1278 WMPutInPLDictionary(node, dHost, host);
1279 WMReleasePropList(host);
1283 return node;
1286 static WMPropList *dockSaveState(WDock * dock)
1288 int i;
1289 WMPropList *icon_info;
1290 WMPropList *list = NULL, *dock_state = NULL;
1291 WMPropList *value, *key;
1292 char buffer[256];
1294 list = WMCreatePLArray(NULL);
1296 for (i = (dock->type == WM_DOCK ? 0 : 1); i < dock->max_icons; i++) {
1297 WAppIcon *btn = dock->icon_array[i];
1299 if (!btn || btn->attracted)
1300 continue;
1302 if ((icon_info = make_icon_state(dock->icon_array[i]))) {
1303 WMAddToPLArray(list, icon_info);
1304 WMReleasePropList(icon_info);
1308 dock_state = WMCreatePLDictionary(dApplications, list, NULL);
1310 if (dock->type == WM_DOCK) {
1311 snprintf(buffer, sizeof(buffer), "Applications%i", dock->screen_ptr->scr_height);
1312 key = WMCreatePLString(buffer);
1313 WMPutInPLDictionary(dock_state, key, list);
1314 WMReleasePropList(key);
1316 snprintf(buffer, sizeof(buffer), "%i,%i", (dock->on_right_side ? -ICON_SIZE : 0), dock->y_pos);
1317 value = WMCreatePLString(buffer);
1318 WMPutInPLDictionary(dock_state, dPosition, value);
1319 WMReleasePropList(value);
1321 WMReleasePropList(list);
1323 value = (dock->lowered ? dYes : dNo);
1324 WMPutInPLDictionary(dock_state, dLowered, value);
1326 if (dock->type == WM_CLIP) {
1327 value = (dock->collapsed ? dYes : dNo);
1328 WMPutInPLDictionary(dock_state, dCollapsed, value);
1330 value = (dock->auto_collapse ? dYes : dNo);
1331 WMPutInPLDictionary(dock_state, dAutoCollapse, value);
1333 value = (dock->auto_raise_lower ? dYes : dNo);
1334 WMPutInPLDictionary(dock_state, dAutoRaiseLower, value);
1336 value = (dock->attract_icons ? dYes : dNo);
1337 WMPutInPLDictionary(dock_state, dAutoAttractIcons, value);
1340 return dock_state;
1343 void wDockSaveState(WScreen * scr, WMPropList * old_state)
1345 WMPropList *dock_state;
1346 WMPropList *keys;
1348 dock_state = dockSaveState(scr->dock);
1351 * Copy saved states of docks with different sizes.
1353 if (old_state) {
1354 int i;
1355 WMPropList *tmp;
1357 keys = WMGetPLDictionaryKeys(old_state);
1358 for (i = 0; i < WMGetPropListItemCount(keys); i++) {
1359 tmp = WMGetFromPLArray(keys, i);
1361 if (strncasecmp(WMGetFromPLString(tmp), "applications", 12) == 0
1362 && !WMGetFromPLDictionary(dock_state, tmp)) {
1364 WMPutInPLDictionary(dock_state, tmp, WMGetFromPLDictionary(old_state, tmp));
1367 WMReleasePropList(keys);
1370 WMPutInPLDictionary(scr->session_state, dDock, dock_state);
1372 WMReleasePropList(dock_state);
1375 void wClipSaveState(WScreen * scr)
1377 WMPropList *clip_state;
1379 clip_state = make_icon_state(scr->clip_icon);
1381 WMPutInPLDictionary(scr->session_state, dClip, clip_state);
1383 WMReleasePropList(clip_state);
1386 WMPropList *wClipSaveWorkspaceState(WScreen * scr, int workspace)
1388 return dockSaveState(scr->workspaces[workspace]->clip);
1391 static Bool getBooleanDockValue(WMPropList * value, WMPropList * key)
1393 if (value) {
1394 if (WMIsPLString(value)) {
1395 if (strcasecmp(WMGetFromPLString(value), "YES") == 0)
1396 return True;
1397 } else {
1398 wwarning(_("bad value in docked icon state info %s"), WMGetFromPLString(key));
1401 return False;
1404 static WAppIcon *restore_icon_state(WScreen * scr, WMPropList * info, int type, int index)
1406 WAppIcon *aicon;
1407 WMPropList *cmd, *value;
1409 cmd = WMGetFromPLDictionary(info, dCommand);
1410 if (!cmd || !WMIsPLString(cmd)) {
1411 return NULL;
1414 /* parse window name */
1415 value = WMGetFromPLDictionary(info, dName);
1416 if (!value)
1417 return NULL;
1420 char *wclass, *winstance;
1421 char *command;
1423 ParseWindowName(value, &winstance, &wclass, "dock");
1425 if (!winstance && !wclass) {
1426 return NULL;
1429 /* get commands */
1431 if (cmd)
1432 command = wstrdup(WMGetFromPLString(cmd));
1433 else
1434 command = NULL;
1436 if (!command || strcmp(command, "-") == 0) {
1437 if (command)
1438 wfree(command);
1439 if (wclass)
1440 wfree(wclass);
1441 if (winstance)
1442 wfree(winstance);
1444 return NULL;
1447 aicon = wAppIconCreateForDock(scr, command, winstance, wclass, TILE_NORMAL);
1448 if (wclass)
1449 wfree(wclass);
1450 if (winstance)
1451 wfree(winstance);
1452 if (command)
1453 wfree(command);
1456 aicon->icon->core->descriptor.handle_mousedown = iconMouseDown;
1457 if (type == WM_CLIP) {
1458 aicon->icon->core->descriptor.handle_enternotify = clipEnterNotify;
1459 aicon->icon->core->descriptor.handle_leavenotify = clipLeaveNotify;
1461 aicon->icon->core->descriptor.parent_type = WCLASS_DOCK_ICON;
1462 aicon->icon->core->descriptor.parent = aicon;
1464 #ifdef XDND /* was OFFIX */
1465 cmd = WMGetFromPLDictionary(info, dDropCommand);
1466 if (cmd)
1467 aicon->dnd_command = wstrdup(WMGetFromPLString(cmd));
1468 #endif
1470 cmd = WMGetFromPLDictionary(info, dPasteCommand);
1471 if (cmd)
1472 aicon->paste_command = wstrdup(WMGetFromPLString(cmd));
1474 /* check auto launch */
1475 value = WMGetFromPLDictionary(info, dAutoLaunch);
1477 aicon->auto_launch = getBooleanDockValue(value, dAutoLaunch);
1479 /* check lock */
1480 value = WMGetFromPLDictionary(info, dLock);
1482 aicon->lock = getBooleanDockValue(value, dLock);
1484 /* check if it wasn't normally docked */
1485 value = WMGetFromPLDictionary(info, dForced);
1487 aicon->forced_dock = getBooleanDockValue(value, dForced);
1489 /* check if we can rely on the stuff in the app */
1490 value = WMGetFromPLDictionary(info, dBuggyApplication);
1492 aicon->buggy_app = getBooleanDockValue(value, dBuggyApplication);
1494 /* get position in the dock */
1495 value = WMGetFromPLDictionary(info, dPosition);
1496 if (value && WMIsPLString(value)) {
1497 if (sscanf(WMGetFromPLString(value), "%hi,%hi", &aicon->xindex, &aicon->yindex) != 2)
1498 wwarning(_("bad value in docked icon state info %s"), WMGetFromPLString(dPosition));
1500 /* check position sanity */
1501 /* incomplete section! */
1502 if (type == WM_DOCK) {
1503 aicon->xindex = 0;
1504 if (aicon->yindex < 0)
1505 wwarning(_("bad value in docked icon position %i,%i"),
1506 aicon->xindex, aicon->yindex);
1508 } else {
1509 aicon->yindex = index;
1510 aicon->xindex = 0;
1513 /* check if icon is omnipresent */
1514 value = WMGetFromPLDictionary(info, dOmnipresent);
1516 aicon->omnipresent = getBooleanDockValue(value, dOmnipresent);
1518 aicon->running = 0;
1519 aicon->docked = 1;
1521 return aicon;
1524 #define COMPLAIN(key) wwarning(_("bad value in dock state info:%s"), key)
1526 WAppIcon *wClipRestoreState(WScreen * scr, WMPropList * clip_state)
1528 WAppIcon *icon;
1529 WMPropList *value;
1531 icon = mainIconCreate(scr, WM_CLIP);
1533 if (!clip_state)
1534 return icon;
1536 WMRetainPropList(clip_state);
1538 /* restore position */
1540 value = WMGetFromPLDictionary(clip_state, dPosition);
1542 if (value) {
1543 if (!WMIsPLString(value))
1544 COMPLAIN("Position");
1545 else {
1546 WMRect rect;
1547 int flags;
1549 if (sscanf(WMGetFromPLString(value), "%i,%i", &icon->x_pos, &icon->y_pos) != 2)
1550 COMPLAIN("Position");
1552 /* check position sanity */
1553 rect.pos.x = icon->x_pos;
1554 rect.pos.y = icon->y_pos;
1555 rect.size.width = rect.size.height = ICON_SIZE;
1557 wGetRectPlacementInfo(scr, rect, &flags);
1558 if (flags & (XFLAG_DEAD | XFLAG_PARTIAL))
1559 wScreenKeepInside(scr, &icon->x_pos, &icon->y_pos, ICON_SIZE, ICON_SIZE);
1562 #ifdef XDND /* was OFFIX */
1563 value = WMGetFromPLDictionary(clip_state, dDropCommand);
1564 if (value && WMIsPLString(value))
1565 icon->dnd_command = wstrdup(WMGetFromPLString(value));
1566 #endif
1568 value = WMGetFromPLDictionary(clip_state, dPasteCommand);
1569 if (value && WMIsPLString(value))
1570 icon->paste_command = wstrdup(WMGetFromPLString(value));
1572 WMReleasePropList(clip_state);
1574 return icon;
1577 WDock *wDockRestoreState(WScreen * scr, WMPropList * dock_state, int type)
1579 WDock *dock;
1580 WMPropList *apps;
1581 WMPropList *value;
1582 WAppIcon *aicon, *old_top;
1583 int count, i;
1585 dock = wDockCreate(scr, type);
1587 if (!dock_state)
1588 return dock;
1590 WMRetainPropList(dock_state);
1592 /* restore position */
1594 value = WMGetFromPLDictionary(dock_state, dPosition);
1596 if (value) {
1597 if (!WMIsPLString(value)) {
1598 COMPLAIN("Position");
1599 } else {
1600 WMRect rect;
1601 int flags;
1603 if (sscanf(WMGetFromPLString(value), "%i,%i", &dock->x_pos, &dock->y_pos) != 2)
1604 COMPLAIN("Position");
1606 /* check position sanity */
1607 rect.pos.x = dock->x_pos;
1608 rect.pos.y = dock->y_pos;
1609 rect.size.width = rect.size.height = ICON_SIZE;
1611 wGetRectPlacementInfo(scr, rect, &flags);
1612 if (flags & (XFLAG_DEAD | XFLAG_PARTIAL)) {
1613 int x = dock->x_pos;
1614 wScreenKeepInside(scr, &x, &dock->y_pos, ICON_SIZE, ICON_SIZE);
1617 /* Is this needed any more? */
1618 if (type == WM_CLIP) {
1619 if (dock->x_pos < 0) {
1620 dock->x_pos = 0;
1621 } else if (dock->x_pos > scr->scr_width - ICON_SIZE) {
1622 dock->x_pos = scr->scr_width - ICON_SIZE;
1624 } else {
1625 if (dock->x_pos >= 0) {
1626 dock->x_pos = DOCK_EXTRA_SPACE;
1627 dock->on_right_side = 0;
1628 } else {
1629 dock->x_pos = scr->scr_width - DOCK_EXTRA_SPACE - ICON_SIZE;
1630 dock->on_right_side = 1;
1636 /* restore lowered/raised state */
1638 dock->lowered = 0;
1640 value = WMGetFromPLDictionary(dock_state, dLowered);
1642 if (value) {
1643 if (!WMIsPLString(value)) {
1644 COMPLAIN("Lowered");
1645 } else {
1646 if (strcasecmp(WMGetFromPLString(value), "YES") == 0) {
1647 dock->lowered = 1;
1652 /* restore collapsed state */
1654 dock->collapsed = 0;
1656 value = WMGetFromPLDictionary(dock_state, dCollapsed);
1658 if (value) {
1659 if (!WMIsPLString(value)) {
1660 COMPLAIN("Collapsed");
1661 } else {
1662 if (strcasecmp(WMGetFromPLString(value), "YES") == 0) {
1663 dock->collapsed = 1;
1668 /* restore auto-collapsed state */
1670 value = WMGetFromPLDictionary(dock_state, dAutoCollapse);
1672 if (value) {
1673 if (!WMIsPLString(value)) {
1674 COMPLAIN("AutoCollapse");
1675 } else {
1676 if (strcasecmp(WMGetFromPLString(value), "YES") == 0) {
1677 dock->auto_collapse = 1;
1678 dock->collapsed = 1;
1683 /* restore auto-raise/lower state */
1685 value = WMGetFromPLDictionary(dock_state, dAutoRaiseLower);
1687 if (value) {
1688 if (!WMIsPLString(value)) {
1689 COMPLAIN("AutoRaiseLower");
1690 } else {
1691 if (strcasecmp(WMGetFromPLString(value), "YES") == 0) {
1692 dock->auto_raise_lower = 1;
1697 /* restore attract icons state */
1699 dock->attract_icons = 0;
1701 value = WMGetFromPLDictionary(dock_state, dAutoAttractIcons);
1703 if (value) {
1704 if (!WMIsPLString(value)) {
1705 COMPLAIN("AutoAttractIcons");
1706 } else {
1707 if (strcasecmp(WMGetFromPLString(value), "YES") == 0) {
1708 dock->attract_icons = 1;
1713 /* application list */
1716 WMPropList *tmp;
1717 char buffer[64];
1720 * When saving, it saves the dock state in
1721 * Applications and Applicationsnnn
1723 * When loading, it will first try Applicationsnnn.
1724 * If it does not exist, use Applications as default.
1727 snprintf(buffer, sizeof(buffer), "Applications%i", scr->scr_height);
1729 tmp = WMCreatePLString(buffer);
1730 apps = WMGetFromPLDictionary(dock_state, tmp);
1731 WMReleasePropList(tmp);
1733 if (!apps) {
1734 apps = WMGetFromPLDictionary(dock_state, dApplications);
1738 if (!apps) {
1739 goto finish;
1742 count = WMGetPropListItemCount(apps);
1744 if (count == 0)
1745 goto finish;
1747 old_top = dock->icon_array[0];
1749 /* dock->icon_count is set to 1 when dock is created.
1750 * Since Clip is already restored, we want to keep it so for clip,
1751 * but for dock we may change the default top tile, so we set it to 0.
1753 if (type == WM_DOCK)
1754 dock->icon_count = 0;
1756 for (i = 0; i < count; i++) {
1757 if (dock->icon_count >= dock->max_icons) {
1758 wwarning(_("there are too many icons stored in dock. Ignoring what doesn't fit"));
1759 break;
1762 value = WMGetFromPLArray(apps, i);
1763 aicon = restore_icon_state(scr, value, type, dock->icon_count);
1765 dock->icon_array[dock->icon_count] = aicon;
1767 if (aicon) {
1768 aicon->dock = dock;
1769 aicon->x_pos = dock->x_pos + (aicon->xindex * ICON_SIZE);
1770 aicon->y_pos = dock->y_pos + (aicon->yindex * ICON_SIZE);
1772 if (dock->lowered)
1773 ChangeStackingLevel(aicon->icon->core, WMNormalLevel);
1774 else
1775 ChangeStackingLevel(aicon->icon->core, WMDockLevel);
1777 wCoreConfigure(aicon->icon->core, aicon->x_pos, aicon->y_pos, 0, 0);
1779 if (!dock->collapsed)
1780 XMapWindow(dpy, aicon->icon->core->window);
1781 wRaiseFrame(aicon->icon->core);
1783 dock->icon_count++;
1784 } else if (dock->icon_count == 0 && type == WM_DOCK)
1785 dock->icon_count++;
1788 /* if the first icon is not defined, use the default */
1789 if (dock->icon_array[0] == NULL) {
1790 /* update default icon */
1791 old_top->x_pos = dock->x_pos;
1792 old_top->y_pos = dock->y_pos;
1793 if (dock->lowered)
1794 ChangeStackingLevel(old_top->icon->core, WMNormalLevel);
1795 else
1796 ChangeStackingLevel(old_top->icon->core, WMDockLevel);
1797 dock->icon_array[0] = old_top;
1798 XMoveWindow(dpy, old_top->icon->core->window, dock->x_pos, dock->y_pos);
1799 /* we don't need to increment dock->icon_count here because it was
1800 * incremented in the loop above.
1802 } else if (old_top != dock->icon_array[0]) {
1803 if (old_top == scr->clip_icon)
1804 scr->clip_icon = dock->icon_array[0];
1805 wAppIconDestroy(old_top);
1808 finish:
1809 WMReleasePropList(dock_state);
1811 return dock;
1814 void wDockLaunchWithState(WDock * dock, WAppIcon * btn, WSavedState * state)
1816 if (btn && btn->command && !btn->running && !btn->launching) {
1818 btn->drop_launch = 0;
1819 btn->paste_launch = 0;
1821 btn->pid = execCommand(btn, btn->command, state);
1823 if (btn->pid > 0) {
1824 if (!btn->forced_dock && !btn->buggy_app) {
1825 btn->launching = 1;
1826 dockIconPaint(btn);
1829 } else {
1830 wfree(state);
1834 void wDockDoAutoLaunch(WDock * dock, int workspace)
1836 WAppIcon *btn;
1837 WSavedState *state;
1838 int i;
1840 for (i = 0; i < dock->max_icons; i++) {
1841 btn = dock->icon_array[i];
1842 if (!btn || !btn->auto_launch)
1843 continue;
1845 state = wmalloc(sizeof(WSavedState));
1846 memset(state, 0, sizeof(WSavedState));
1847 state->workspace = workspace;
1848 /* TODO: this is klugy and is very difficult to understand
1849 * what's going on. Try to clean up */
1850 wDockLaunchWithState(dock, btn, state);
1854 #ifdef XDND /* was OFFIX */
1855 static WDock *findDock(WScreen * scr, XEvent * event, int *icon_pos)
1857 WDock *dock;
1858 int i;
1860 *icon_pos = -1;
1861 if ((dock = scr->dock) != NULL) {
1862 for (i = 0; i < dock->max_icons; i++) {
1863 if (dock->icon_array[i]
1864 && dock->icon_array[i]->icon->core->window == event->xclient.window) {
1865 *icon_pos = i;
1866 break;
1870 if (*icon_pos < 0 && (dock = scr->workspaces[scr->current_workspace]->clip) != NULL) {
1871 for (i = 0; i < dock->max_icons; i++) {
1872 if (dock->icon_array[i]
1873 && dock->icon_array[i]->icon->core->window == event->xclient.window) {
1874 *icon_pos = i;
1875 break;
1879 if (*icon_pos >= 0)
1880 return dock;
1881 return NULL;
1884 int wDockReceiveDNDDrop(WScreen * scr, XEvent * event)
1886 WDock *dock;
1887 WAppIcon *btn;
1888 int icon_pos;
1890 dock = findDock(scr, event, &icon_pos);
1891 if (!dock)
1892 return False;
1895 * Return True if the drop was on an application icon window.
1896 * In this case, let the ClientMessage handler redirect the
1897 * message to the app.
1899 if (dock->icon_array[icon_pos]->icon->icon_win != None)
1900 return True;
1902 if (dock->icon_array[icon_pos]->dnd_command != NULL) {
1903 scr->flags.dnd_data_convertion_status = 0;
1905 btn = dock->icon_array[icon_pos];
1907 if (!btn->forced_dock) {
1908 btn->relaunching = btn->running;
1909 btn->running = 1;
1911 if (btn->wm_instance || btn->wm_class) {
1912 WWindowAttributes attr;
1913 memset(&attr, 0, sizeof(WWindowAttributes));
1914 wDefaultFillAttributes(btn->icon->core->screen_ptr,
1915 btn->wm_instance, btn->wm_class, &attr, NULL, True);
1917 if (!attr.no_appicon)
1918 btn->launching = 1;
1919 else
1920 btn->running = 0;
1923 btn->paste_launch = 0;
1924 btn->drop_launch = 1;
1925 scr->last_dock = dock;
1926 btn->pid = execCommand(btn, btn->dnd_command, NULL);
1927 if (btn->pid > 0) {
1928 dockIconPaint(btn);
1929 } else {
1930 btn->launching = 0;
1931 if (!btn->relaunching) {
1932 btn->running = 0;
1936 return False;
1938 #endif /* XDND */
1940 Bool wDockAttachIcon(WDock * dock, WAppIcon * icon, int x, int y)
1942 WWindow *wwin;
1943 int index;
1945 wwin = icon->icon->owner;
1946 if (icon->command == NULL) {
1947 char *command;
1949 icon->editing = 0;
1951 command = GetCommandForWindow(wwin->client_win);
1952 if (command) {
1953 icon->command = command;
1954 } else {
1955 /* icon->forced_dock = 1; */
1956 if (dock->type != WM_CLIP || !icon->attracted) {
1957 icon->editing = 1;
1958 if (wInputDialog(dock->screen_ptr, _("Dock Icon"),
1959 _("Type the command used to launch the application"), &command)) {
1960 if (command && (command[0] == 0 || (command[0] == '-' && command[1] == 0))) {
1961 wfree(command);
1962 command = NULL;
1964 icon->command = command;
1965 icon->editing = 0;
1966 } else {
1967 icon->editing = 0;
1968 if (command)
1969 wfree(command);
1970 /* If the target is the dock, reject the icon. If
1971 * the target is the clip, make it an attracted icon
1973 if (dock->type == WM_CLIP) {
1974 icon->attracted = 1;
1975 if (!icon->icon->shadowed) {
1976 icon->icon->shadowed = 1;
1977 icon->icon->force_paint = 1;
1979 } else {
1980 return False;
1985 } else {
1986 icon->editing = 0;
1989 for (index = 1; index < dock->max_icons; index++)
1990 if (dock->icon_array[index] == NULL)
1991 break;
1992 /* if (index == dock->max_icons)
1993 return; */
1995 assert(index < dock->max_icons);
1997 dock->icon_array[index] = icon;
1998 icon->yindex = y;
1999 icon->xindex = x;
2001 icon->omnipresent = 0;
2003 icon->x_pos = dock->x_pos + x * ICON_SIZE;
2004 icon->y_pos = dock->y_pos + y * ICON_SIZE;
2006 dock->icon_count++;
2008 icon->running = 1;
2009 icon->launching = 0;
2010 icon->docked = 1;
2011 icon->dock = dock;
2012 icon->icon->core->descriptor.handle_mousedown = iconMouseDown;
2013 if (dock->type == WM_CLIP) {
2014 icon->icon->core->descriptor.handle_enternotify = clipEnterNotify;
2015 icon->icon->core->descriptor.handle_leavenotify = clipLeaveNotify;
2017 icon->icon->core->descriptor.parent_type = WCLASS_DOCK_ICON;
2018 icon->icon->core->descriptor.parent = icon;
2020 MoveInStackListUnder(dock->icon_array[index - 1]->icon->core, icon->icon->core);
2021 wAppIconMove(icon, icon->x_pos, icon->y_pos);
2022 wAppIconPaint(icon);
2024 if (wPreferences.auto_arrange_icons)
2025 wArrangeIcons(dock->screen_ptr, True);
2027 #ifdef XDND /* was OFFIX */
2028 if (icon->command && !icon->dnd_command) {
2029 int len = strlen(icon->command) + 8;
2030 icon->dnd_command = wmalloc(len);
2031 snprintf(icon->dnd_command, len, "%s %%d", icon->command);
2033 #endif
2035 if (icon->command && !icon->paste_command) {
2036 int len = strlen(icon->command) + 8;
2037 icon->paste_command = wmalloc(len);
2038 snprintf(icon->paste_command, len, "%s %%s", icon->command);
2041 return True;
2044 void reattachIcon(WDock * dock, WAppIcon * icon, int x, int y)
2046 int index;
2048 for (index = 1; index < dock->max_icons; index++) {
2049 if (dock->icon_array[index] == icon)
2050 break;
2052 assert(index < dock->max_icons);
2054 icon->yindex = y;
2055 icon->xindex = x;
2057 icon->x_pos = dock->x_pos + x * ICON_SIZE;
2058 icon->y_pos = dock->y_pos + y * ICON_SIZE;
2061 Bool moveIconBetweenDocks(WDock * src, WDock * dest, WAppIcon * icon, int x, int y)
2063 WWindow *wwin;
2064 char *command;
2065 int index;
2067 if (src == dest)
2068 return True; /* No move needed, we're already there */
2070 if (dest == NULL)
2071 return False;
2073 wwin = icon->icon->owner;
2076 * For the moment we can't do this if we move icons in Clip from one
2077 * workspace to other, because if we move two or more icons without
2078 * command, the dialog box will not be able to tell us to which of the
2079 * moved icons it applies. -Dan
2081 if ((dest->type == WM_DOCK /*|| dest->keep_attracted */ ) && icon->command == NULL) {
2082 command = GetCommandForWindow(wwin->client_win);
2083 if (command) {
2084 icon->command = command;
2085 } else {
2086 icon->editing = 1;
2087 /* icon->forced_dock = 1; */
2088 if (wInputDialog(src->screen_ptr, _("Dock Icon"),
2089 _("Type the command used to launch the application"), &command)) {
2090 if (command && (command[0] == 0 || (command[0] == '-' && command[1] == 0))) {
2091 wfree(command);
2092 command = NULL;
2094 icon->command = command;
2095 } else {
2096 icon->editing = 0;
2097 if (command)
2098 wfree(command);
2099 return False;
2101 icon->editing = 0;
2105 if (dest->type == WM_DOCK)
2106 wClipMakeIconOmnipresent(icon, False);
2108 for (index = 1; index < src->max_icons; index++) {
2109 if (src->icon_array[index] == icon)
2110 break;
2112 assert(index < src->max_icons);
2114 src->icon_array[index] = NULL;
2115 src->icon_count--;
2117 for (index = 1; index < dest->max_icons; index++) {
2118 if (dest->icon_array[index] == NULL)
2119 break;
2121 /* if (index == dest->max_icons)
2122 return; */
2124 assert(index < dest->max_icons);
2126 dest->icon_array[index] = icon;
2127 icon->dock = dest;
2129 /* deselect the icon */
2130 if (icon->icon->selected)
2131 wIconSelect(icon->icon);
2133 if (dest->type == WM_DOCK) {
2134 icon->icon->core->descriptor.handle_enternotify = NULL;
2135 icon->icon->core->descriptor.handle_leavenotify = NULL;
2136 } else {
2137 icon->icon->core->descriptor.handle_enternotify = clipEnterNotify;
2138 icon->icon->core->descriptor.handle_leavenotify = clipLeaveNotify;
2141 /* set it to be kept when moving to dock.
2142 * Unless the icon does not have a command set
2144 if (icon->command && dest->type == WM_DOCK) {
2145 icon->attracted = 0;
2146 if (icon->icon->shadowed) {
2147 icon->icon->shadowed = 0;
2148 icon->icon->force_paint = 1;
2152 if (src->auto_collapse || src->auto_raise_lower)
2153 clipLeave(src);
2155 icon->yindex = y;
2156 icon->xindex = x;
2158 icon->x_pos = dest->x_pos + x * ICON_SIZE;
2159 icon->y_pos = dest->y_pos + y * ICON_SIZE;
2161 dest->icon_count++;
2163 MoveInStackListUnder(dest->icon_array[index - 1]->icon->core, icon->icon->core);
2164 wAppIconPaint(icon);
2166 return True;
2169 void wDockDetach(WDock * dock, WAppIcon * icon)
2171 int index;
2173 /* make the settings panel be closed */
2174 if (icon->panel) {
2175 DestroyDockAppSettingsPanel(icon->panel);
2178 /* This must be called before icon->dock is set to NULL.
2179 * Don't move it. -Dan
2181 wClipMakeIconOmnipresent(icon, False);
2183 icon->docked = 0;
2184 icon->dock = NULL;
2185 icon->attracted = 0;
2186 icon->auto_launch = 0;
2187 if (icon->icon->shadowed) {
2188 icon->icon->shadowed = 0;
2189 icon->icon->force_paint = 1;
2192 /* deselect the icon */
2193 if (icon->icon->selected)
2194 wIconSelect(icon->icon);
2196 if (icon->command) {
2197 wfree(icon->command);
2198 icon->command = NULL;
2200 #ifdef XDND /* was OFFIX */
2201 if (icon->dnd_command) {
2202 wfree(icon->dnd_command);
2203 icon->dnd_command = NULL;
2205 #endif
2206 if (icon->paste_command) {
2207 wfree(icon->paste_command);
2208 icon->paste_command = NULL;
2211 for (index = 1; index < dock->max_icons; index++)
2212 if (dock->icon_array[index] == icon)
2213 break;
2214 assert(index < dock->max_icons);
2215 dock->icon_array[index] = NULL;
2216 icon->yindex = -1;
2217 icon->xindex = -1;
2219 dock->icon_count--;
2221 /* if the dock is not attached to an application or
2222 * the the application did not set the approriate hints yet,
2223 * destroy the icon */
2224 if (!icon->running || !wApplicationOf(icon->main_window))
2225 wAppIconDestroy(icon);
2226 else {
2227 icon->icon->core->descriptor.handle_mousedown = appIconMouseDown;
2228 icon->icon->core->descriptor.handle_enternotify = NULL;
2229 icon->icon->core->descriptor.handle_leavenotify = NULL;
2230 icon->icon->core->descriptor.parent_type = WCLASS_APPICON;
2231 icon->icon->core->descriptor.parent = icon;
2233 ChangeStackingLevel(icon->icon->core, NORMAL_ICON_LEVEL);
2235 wAppIconPaint(icon);
2236 if (wPreferences.auto_arrange_icons) {
2237 wArrangeIcons(dock->screen_ptr, True);
2240 if (dock->auto_collapse || dock->auto_raise_lower)
2241 clipLeave(dock);
2245 * returns the closest Dock slot index for the passed
2246 * coordinates.
2248 * Returns False if icon can't be docked.
2250 * Note: this function should NEVER alter ret_x or ret_y, unless it will
2251 * return True. -Dan
2253 Bool wDockSnapIcon(WDock * dock, WAppIcon * icon, int req_x, int req_y, int *ret_x, int *ret_y, int redocking)
2255 WScreen *scr = dock->screen_ptr;
2256 int dx, dy;
2257 int ex_x, ex_y;
2258 int i, offset = ICON_SIZE / 2;
2259 WAppIcon *aicon = NULL;
2260 WAppIcon *nicon = NULL;
2261 int max_y_icons, max_x_icons;
2263 /* TODO: XINERAMA, for these */
2264 max_x_icons = scr->scr_width / ICON_SIZE;
2265 max_y_icons = scr->scr_height / ICON_SIZE - 1;
2267 if (wPreferences.flags.noupdates)
2268 return False;
2270 dx = dock->x_pos;
2271 dy = dock->y_pos;
2273 /* if the dock is full */
2274 if (!redocking && (dock->icon_count >= dock->max_icons)) {
2275 return False;
2278 /* exact position */
2279 if (req_y < dy)
2280 ex_y = (req_y - offset - dy) / ICON_SIZE;
2281 else
2282 ex_y = (req_y + offset - dy) / ICON_SIZE;
2284 if (req_x < dx)
2285 ex_x = (req_x - offset - dx) / ICON_SIZE;
2286 else
2287 ex_x = (req_x + offset - dx) / ICON_SIZE;
2289 /* check if the icon is outside the screen boundaries */
2291 WMRect rect;
2292 int flags;
2294 rect.pos.x = dx + ex_x * ICON_SIZE;
2295 rect.pos.y = dy + ex_y * ICON_SIZE;
2296 rect.size.width = rect.size.height = ICON_SIZE;
2298 wGetRectPlacementInfo(scr, rect, &flags);
2299 if (flags & (XFLAG_DEAD | XFLAG_PARTIAL))
2300 return False;
2303 if (dock->type == WM_DOCK) {
2304 if (icon->dock != dock && ex_x != 0)
2305 return False;
2307 aicon = NULL;
2308 for (i = 0; i < dock->max_icons; i++) {
2309 nicon = dock->icon_array[i];
2310 if (nicon && nicon->yindex == ex_y) {
2311 aicon = nicon;
2312 break;
2316 if (redocking) {
2317 int sig, done, closest;
2319 /* Possible cases when redocking:
2321 * icon dragged out of range of any slot -> false
2322 * icon dragged to range of free slot
2323 * icon dragged to range of same slot
2324 * icon dragged to range of different icon
2326 if (abs(ex_x) > DOCK_DETTACH_THRESHOLD)
2327 return False;
2329 if (ex_y >= 0 && ex_y <= max_y_icons && (aicon == icon || !aicon)) {
2330 *ret_x = 0;
2331 *ret_y = ex_y;
2332 return True;
2335 /* start looking at the upper slot or lower? */
2336 if (ex_y * ICON_SIZE < (req_y + offset - dy))
2337 sig = 1;
2338 else
2339 sig = -1;
2341 closest = -1;
2342 done = 0;
2343 /* look for closest free slot */
2344 for (i = 0; i < (DOCK_DETTACH_THRESHOLD + 1) * 2 && !done; i++) {
2345 int j;
2347 done = 1;
2348 closest = sig * (i / 2) + ex_y;
2349 /* check if this slot is used */
2350 if (closest >= 0) {
2351 for (j = 0; j < dock->max_icons; j++) {
2352 if (dock->icon_array[j]
2353 && dock->icon_array[j]->yindex == closest) {
2354 /* slot is used by someone else */
2355 if (dock->icon_array[j] != icon)
2356 done = 0;
2357 break;
2361 sig = -sig;
2363 if (done && closest >= 0 && closest <= max_y_icons &&
2364 ((ex_y >= closest && ex_y - closest < DOCK_DETTACH_THRESHOLD + 1)
2365 || (ex_y < closest && closest - ex_y <= DOCK_DETTACH_THRESHOLD + 1))) {
2366 *ret_x = 0;
2367 *ret_y = closest;
2368 return True;
2370 } else { /* !redocking */
2372 /* if slot is free and the icon is close enough, return it */
2373 if (!aicon && ex_x == 0 && ex_y >= 0 && ex_y <= max_y_icons) {
2374 *ret_x = 0;
2375 *ret_y = ex_y;
2376 return True;
2379 } else { /* CLIP */
2380 int neighbours = 0;
2381 int start, stop, k;
2383 start = icon->omnipresent ? 0 : scr->current_workspace;
2384 stop = icon->omnipresent ? scr->workspace_count : start + 1;
2386 aicon = NULL;
2387 for (k = start; k < stop; k++) {
2388 WDock *tmp = scr->workspaces[k]->clip;
2389 if (!tmp)
2390 continue;
2391 for (i = 0; i < tmp->max_icons; i++) {
2392 nicon = tmp->icon_array[i];
2393 if (nicon && nicon->xindex == ex_x && nicon->yindex == ex_y) {
2394 aicon = nicon;
2395 break;
2398 if (aicon)
2399 break;
2401 for (k = start; k < stop; k++) {
2402 WDock *tmp = scr->workspaces[k]->clip;
2403 if (!tmp)
2404 continue;
2405 for (i = 0; i < tmp->max_icons; i++) {
2406 nicon = tmp->icon_array[i];
2407 if (nicon && nicon != icon && /* Icon can't be it's own neighbour */
2408 (abs(nicon->xindex - ex_x) <= CLIP_ATTACH_VICINITY &&
2409 abs(nicon->yindex - ex_y) <= CLIP_ATTACH_VICINITY)) {
2410 neighbours = 1;
2411 break;
2414 if (neighbours)
2415 break;
2418 if (neighbours && (aicon == NULL || (redocking && aicon == icon))) {
2419 *ret_x = ex_x;
2420 *ret_y = ex_y;
2421 return True;
2424 return False;
2427 static int onScreen(WScreen * scr, int x, int y, int sx, int ex, int sy, int ey)
2429 WMRect rect = wmkrect(x, y, ICON_SIZE, ICON_SIZE);
2430 int flags;
2432 wGetRectPlacementInfo(scr, rect, &flags);
2434 return !(flags & (XFLAG_DEAD | XFLAG_PARTIAL));
2438 * returns true if it can find a free slot in the dock,
2439 * in which case it changes x_pos and y_pos accordingly.
2440 * Else returns false.
2442 Bool wDockFindFreeSlot(WDock * dock, int *x_pos, int *y_pos)
2444 WScreen *scr = dock->screen_ptr;
2445 WAppIcon *btn;
2446 WAppIconChain *chain;
2447 unsigned char *slot_map;
2448 int mwidth;
2449 int r;
2450 int x, y;
2451 int i, done = False;
2452 int corner;
2453 int sx = 0, sy = 0, ex = scr->scr_width, ey = scr->scr_height;
2454 int extra_count = 0;
2456 if (dock->type == WM_CLIP && dock != scr->workspaces[scr->current_workspace]->clip)
2457 extra_count = scr->global_icon_count;
2459 /* if the dock is full */
2460 if (dock->icon_count + extra_count >= dock->max_icons) {
2461 return False;
2464 if (!wPreferences.flags.nodock && scr->dock) {
2465 if (scr->dock->on_right_side)
2466 ex -= ICON_SIZE + DOCK_EXTRA_SPACE;
2467 else
2468 sx += ICON_SIZE + DOCK_EXTRA_SPACE;
2471 if (ex < dock->x_pos)
2472 ex = dock->x_pos;
2473 if (sx > dock->x_pos + ICON_SIZE)
2474 sx = dock->x_pos + ICON_SIZE;
2475 #define C_NONE 0
2476 #define C_NW 1
2477 #define C_NE 2
2478 #define C_SW 3
2479 #define C_SE 4
2481 /* check if clip is in a corner */
2482 if (dock->type == WM_CLIP) {
2483 if (dock->x_pos < 1 && dock->y_pos < 1)
2484 corner = C_NE;
2485 else if (dock->x_pos < 1 && dock->y_pos >= (ey - ICON_SIZE))
2486 corner = C_SE;
2487 else if (dock->x_pos >= (ex - ICON_SIZE) && dock->y_pos >= (ey - ICON_SIZE))
2488 corner = C_SW;
2489 else if (dock->x_pos >= (ex - ICON_SIZE) && dock->y_pos < 1)
2490 corner = C_NW;
2491 else
2492 corner = C_NONE;
2493 } else
2494 corner = C_NONE;
2496 /* If the clip is in the corner, use only slots that are in the border
2497 * of the screen */
2498 if (corner != C_NONE) {
2499 char *hmap, *vmap;
2500 int hcount, vcount;
2502 hcount = WMIN(dock->max_icons, scr->scr_width / ICON_SIZE);
2503 vcount = WMIN(dock->max_icons, scr->scr_height / ICON_SIZE);
2504 hmap = wmalloc(hcount + 1);
2505 memset(hmap, 0, hcount + 1);
2506 vmap = wmalloc(vcount + 1);
2507 memset(vmap, 0, vcount + 1);
2509 /* mark used positions */
2510 switch (corner) {
2511 case C_NE:
2512 for (i = 0; i < dock->max_icons; i++) {
2513 btn = dock->icon_array[i];
2514 if (!btn)
2515 continue;
2517 if (btn->xindex == 0 && btn->yindex > 0 && btn->yindex < vcount)
2518 vmap[btn->yindex] = 1;
2519 else if (btn->yindex == 0 && btn->xindex > 0 && btn->xindex < hcount)
2520 hmap[btn->xindex] = 1;
2522 for (chain = scr->global_icons; chain != NULL; chain = chain->next) {
2523 btn = chain->aicon;
2524 if (btn->xindex == 0 && btn->yindex > 0 && btn->yindex < vcount)
2525 vmap[btn->yindex] = 1;
2526 else if (btn->yindex == 0 && btn->xindex > 0 && btn->xindex < hcount)
2527 hmap[btn->xindex] = 1;
2529 break;
2530 case C_NW:
2531 for (i = 0; i < dock->max_icons; i++) {
2532 btn = dock->icon_array[i];
2533 if (!btn)
2534 continue;
2536 if (btn->xindex == 0 && btn->yindex > 0 && btn->yindex < vcount)
2537 vmap[btn->yindex] = 1;
2538 else if (btn->yindex == 0 && btn->xindex < 0 && btn->xindex > -hcount)
2539 hmap[-btn->xindex] = 1;
2541 for (chain = scr->global_icons; chain != NULL; chain = chain->next) {
2542 btn = chain->aicon;
2543 if (btn->xindex == 0 && btn->yindex > 0 && btn->yindex < vcount)
2544 vmap[btn->yindex] = 1;
2545 else if (btn->yindex == 0 && btn->xindex < 0 && btn->xindex > -hcount)
2546 hmap[-btn->xindex] = 1;
2548 break;
2549 case C_SE:
2550 for (i = 0; i < dock->max_icons; i++) {
2551 btn = dock->icon_array[i];
2552 if (!btn)
2553 continue;
2555 if (btn->xindex == 0 && btn->yindex < 0 && btn->yindex > -vcount)
2556 vmap[-btn->yindex] = 1;
2557 else if (btn->yindex == 0 && btn->xindex > 0 && btn->xindex < hcount)
2558 hmap[btn->xindex] = 1;
2560 for (chain = scr->global_icons; chain != NULL; chain = chain->next) {
2561 btn = chain->aicon;
2562 if (btn->xindex == 0 && btn->yindex < 0 && btn->yindex > -vcount)
2563 vmap[-btn->yindex] = 1;
2564 else if (btn->yindex == 0 && btn->xindex > 0 && btn->xindex < hcount)
2565 hmap[btn->xindex] = 1;
2567 break;
2568 case C_SW:
2569 default:
2570 for (i = 0; i < dock->max_icons; i++) {
2571 btn = dock->icon_array[i];
2572 if (!btn)
2573 continue;
2575 if (btn->xindex == 0 && btn->yindex < 0 && btn->yindex > -vcount)
2576 vmap[-btn->yindex] = 1;
2577 else if (btn->yindex == 0 && btn->xindex < 0 && btn->xindex > -hcount)
2578 hmap[-btn->xindex] = 1;
2580 for (chain = scr->global_icons; chain != NULL; chain = chain->next) {
2581 btn = chain->aicon;
2582 if (btn->xindex == 0 && btn->yindex < 0 && btn->yindex > -vcount)
2583 vmap[-btn->yindex] = 1;
2584 else if (btn->yindex == 0 && btn->xindex < 0 && btn->xindex > -hcount)
2585 hmap[-btn->xindex] = 1;
2588 x = 0;
2589 y = 0;
2590 done = 0;
2591 /* search a vacant slot */
2592 for (i = 1; i < WMAX(vcount, hcount); i++) {
2593 if (i < vcount && vmap[i] == 0) {
2594 /* found a slot */
2595 x = 0;
2596 y = i;
2597 done = 1;
2598 break;
2599 } else if (i < hcount && hmap[i] == 0) {
2600 /* found a slot */
2601 x = i;
2602 y = 0;
2603 done = 1;
2604 break;
2607 wfree(vmap);
2608 wfree(hmap);
2609 /* If found a slot, translate and return */
2610 if (done) {
2611 if (corner == C_NW || corner == C_NE) {
2612 *y_pos = y;
2613 } else {
2614 *y_pos = -y;
2616 if (corner == C_NE || corner == C_SE) {
2617 *x_pos = x;
2618 } else {
2619 *x_pos = -x;
2621 return True;
2623 /* else, try to find a slot somewhere else */
2626 /* a map of mwidth x mwidth would be enough if we allowed icons to be
2627 * placed outside of screen */
2628 mwidth = (int)ceil(sqrt(dock->max_icons));
2630 /* In the worst case (the clip is in the corner of the screen),
2631 * the amount of icons that fit in the clip is smaller.
2632 * Double the map to get a safe value.
2634 mwidth += mwidth;
2636 r = (mwidth - 1) / 2;
2638 slot_map = wmalloc(mwidth * mwidth);
2639 memset(slot_map, 0, mwidth * mwidth);
2641 #define XY2OFS(x,y) (WMAX(abs(x),abs(y)) > r) ? 0 : (((y)+r)*(mwidth)+(x)+r)
2643 /* mark used slots in the map. If the slot falls outside the map
2644 * (for example, when all icons are placed in line), ignore them. */
2645 for (i = 0; i < dock->max_icons; i++) {
2646 btn = dock->icon_array[i];
2647 if (btn)
2648 slot_map[XY2OFS(btn->xindex, btn->yindex)] = 1;
2650 for (chain = scr->global_icons; chain != NULL; chain = chain->next) {
2651 slot_map[XY2OFS(chain->aicon->xindex, chain->aicon->yindex)] = 1;
2653 /* Find closest slot from the center that is free by scanning the
2654 * map from the center to outward in circular passes.
2655 * This will not result in a neat layout, but will be optimal
2656 * in the sense that there will not be holes left.
2658 done = 0;
2659 for (i = 1; i <= r && !done; i++) {
2660 int tx, ty;
2662 /* top and bottom parts of the ring */
2663 for (x = -i; x <= i && !done; x++) {
2664 tx = dock->x_pos + x * ICON_SIZE;
2665 y = -i;
2666 ty = dock->y_pos + y * ICON_SIZE;
2667 if (slot_map[XY2OFS(x, y)] == 0 && onScreen(scr, tx, ty, sx, ex, sy, ey)) {
2668 *x_pos = x;
2669 *y_pos = y;
2670 done = 1;
2671 break;
2673 y = i;
2674 ty = dock->y_pos + y * ICON_SIZE;
2675 if (slot_map[XY2OFS(x, y)] == 0 && onScreen(scr, tx, ty, sx, ex, sy, ey)) {
2676 *x_pos = x;
2677 *y_pos = y;
2678 done = 1;
2679 break;
2682 /* left and right parts of the ring */
2683 for (y = -i + 1; y <= i - 1; y++) {
2684 ty = dock->y_pos + y * ICON_SIZE;
2685 x = -i;
2686 tx = dock->x_pos + x * ICON_SIZE;
2687 if (slot_map[XY2OFS(x, y)] == 0 && onScreen(scr, tx, ty, sx, ex, sy, ey)) {
2688 *x_pos = x;
2689 *y_pos = y;
2690 done = 1;
2691 break;
2693 x = i;
2694 tx = dock->x_pos + x * ICON_SIZE;
2695 if (slot_map[XY2OFS(x, y)] == 0 && onScreen(scr, tx, ty, sx, ex, sy, ey)) {
2696 *x_pos = x;
2697 *y_pos = y;
2698 done = 1;
2699 break;
2703 wfree(slot_map);
2704 #undef XY2OFS
2705 return done;
2708 static void moveDock(WDock * dock, int new_x, int new_y)
2710 WAppIcon *btn;
2711 int i;
2713 dock->x_pos = new_x;
2714 dock->y_pos = new_y;
2715 for (i = 0; i < dock->max_icons; i++) {
2716 btn = dock->icon_array[i];
2717 if (btn) {
2718 btn->x_pos = new_x + btn->xindex * ICON_SIZE;
2719 btn->y_pos = new_y + btn->yindex * ICON_SIZE;
2720 XMoveWindow(dpy, btn->icon->core->window, btn->x_pos, btn->y_pos);
2725 static void swapDock(WDock * dock)
2727 WScreen *scr = dock->screen_ptr;
2728 WAppIcon *btn;
2729 int x, i;
2731 if (dock->on_right_side) {
2732 x = dock->x_pos = scr->scr_width - ICON_SIZE - DOCK_EXTRA_SPACE;
2733 } else {
2734 x = dock->x_pos = DOCK_EXTRA_SPACE;
2737 for (i = 0; i < dock->max_icons; i++) {
2738 btn = dock->icon_array[i];
2739 if (btn) {
2740 btn->x_pos = x;
2741 XMoveWindow(dpy, btn->icon->core->window, btn->x_pos, btn->y_pos);
2745 wScreenUpdateUsableArea(scr);
2748 static pid_t execCommand(WAppIcon * btn, char *command, WSavedState * state)
2750 WScreen *scr = btn->icon->core->screen_ptr;
2751 pid_t pid;
2752 char **argv;
2753 int argc;
2754 char *cmdline;
2756 cmdline = ExpandOptions(scr, command);
2758 if (scr->flags.dnd_data_convertion_status || !cmdline) {
2759 if (cmdline)
2760 wfree(cmdline);
2761 if (state)
2762 wfree(state);
2763 return 0;
2766 wtokensplit(cmdline, &argv, &argc);
2768 if (!argc) {
2769 if (cmdline)
2770 wfree(cmdline);
2771 if (state)
2772 wfree(state);
2773 return 0;
2776 if ((pid = fork()) == 0) {
2777 char **args;
2778 int i;
2780 SetupEnvironment(scr);
2782 #ifdef HAVE_SETSID
2783 setsid();
2784 #endif
2786 args = malloc(sizeof(char *) * (argc + 1));
2787 if (!args)
2788 exit(111);
2789 for (i = 0; i < argc; i++) {
2790 args[i] = argv[i];
2792 args[argc] = NULL;
2793 execvp(argv[0], args);
2794 exit(111);
2796 wtokenfree(argv, argc);
2798 if (pid > 0) {
2799 if (!state) {
2800 state = wmalloc(sizeof(WSavedState));
2801 memset(state, 0, sizeof(WSavedState));
2802 state->hidden = -1;
2803 state->miniaturized = -1;
2804 state->shaded = -1;
2805 if (btn->dock == scr->dock || btn->omnipresent)
2806 state->workspace = -1;
2807 else
2808 state->workspace = scr->current_workspace;
2810 wWindowAddSavedState(btn->wm_instance, btn->wm_class, cmdline, pid, state);
2811 wAddDeathHandler(pid, (WDeathHandler *) trackDeadProcess, btn->dock);
2812 } else if (state) {
2813 wfree(state);
2815 wfree(cmdline);
2816 return pid;
2819 void wDockHideIcons(WDock * dock)
2821 int i;
2823 if (dock == NULL)
2824 return;
2826 for (i = 1; i < dock->max_icons; i++) {
2827 if (dock->icon_array[i])
2828 XUnmapWindow(dpy, dock->icon_array[i]->icon->core->window);
2830 dock->mapped = 0;
2832 dockIconPaint(dock->icon_array[0]);
2835 void wDockShowIcons(WDock * dock)
2837 int i, newlevel;
2838 WAppIcon *btn;
2840 if (dock == NULL)
2841 return;
2843 btn = dock->icon_array[0];
2844 moveDock(dock, btn->x_pos, btn->y_pos);
2846 newlevel = dock->lowered ? WMNormalLevel : WMDockLevel;
2847 ChangeStackingLevel(btn->icon->core, newlevel);
2849 for (i = 1; i < dock->max_icons; i++) {
2850 if (dock->icon_array[i]) {
2851 MoveInStackListAbove(dock->icon_array[i]->icon->core, btn->icon->core);
2852 break;
2856 if (!dock->collapsed) {
2857 for (i = 1; i < dock->max_icons; i++) {
2858 if (dock->icon_array[i]) {
2859 XMapWindow(dpy, dock->icon_array[i]->icon->core->window);
2863 dock->mapped = 1;
2865 dockIconPaint(btn);
2868 void wDockLower(WDock * dock)
2870 int i;
2872 for (i = 0; i < dock->max_icons; i++) {
2873 if (dock->icon_array[i])
2874 wLowerFrame(dock->icon_array[i]->icon->core);
2878 void wDockRaise(WDock * dock)
2880 int i;
2882 for (i = dock->max_icons - 1; i >= 0; i--) {
2883 if (dock->icon_array[i])
2884 wRaiseFrame(dock->icon_array[i]->icon->core);
2888 void wDockRaiseLower(WDock * dock)
2890 if (!dock->icon_array[0]->icon->core->stacking->above
2891 || (dock->icon_array[0]->icon->core->stacking->window_level
2892 != dock->icon_array[0]->icon->core->stacking->above->stacking->window_level))
2893 wDockLower(dock);
2894 else
2895 wDockRaise(dock);
2898 void wDockFinishLaunch(WDock * dock, WAppIcon * icon)
2900 icon->launching = 0;
2901 icon->relaunching = 0;
2902 dockIconPaint(icon);
2905 WAppIcon *wDockFindIconForWindow(WDock * dock, Window window)
2907 WAppIcon *icon;
2908 int i;
2910 for (i = 0; i < dock->max_icons; i++) {
2911 icon = dock->icon_array[i];
2912 if (icon && icon->main_window == window)
2913 return icon;
2915 return NULL;
2918 void wDockTrackWindowLaunch(WDock * dock, Window window)
2920 WAppIcon *icon;
2921 char *wm_class, *wm_instance;
2922 int i;
2923 Bool firstPass = True;
2924 Bool found = False;
2925 char *command = NULL;
2927 command = GetCommandForWindow(window);
2929 if (!PropGetWMClass(window, &wm_class, &wm_instance) || (!wm_class && !wm_instance)) {
2931 if (command)
2932 wfree(command);
2933 return;
2936 retry:
2937 for (i = 0; i < dock->max_icons; i++) {
2938 icon = dock->icon_array[i];
2939 if (!icon)
2940 continue;
2942 /* app is already attached to icon */
2943 if (icon->main_window == window) {
2944 found = True;
2945 break;
2948 if ((icon->wm_instance || icon->wm_class)
2949 && (icon->launching || !icon->running)) {
2951 if (icon->wm_instance && wm_instance && strcmp(icon->wm_instance, wm_instance) != 0) {
2952 continue;
2954 if (icon->wm_class && wm_class && strcmp(icon->wm_class, wm_class) != 0) {
2955 continue;
2957 if (firstPass && command && strcmp(icon->command, command) != 0) {
2958 continue;
2961 if (!icon->relaunching) {
2962 WApplication *wapp;
2964 /* Possibly an application that was docked with dockit,
2965 * but the user did not update WMState to indicate that
2966 * it was docked by force */
2967 wapp = wApplicationOf(window);
2968 if (!wapp) {
2969 icon->forced_dock = 1;
2970 icon->running = 0;
2972 if (!icon->forced_dock) {
2973 icon->main_window = window;
2976 found = True;
2977 if (!wPreferences.no_animations && !icon->launching &&
2978 !dock->screen_ptr->flags.startup && !dock->collapsed) {
2979 WAppIcon *aicon;
2980 int x0, y0;
2982 icon->launching = 1;
2983 dockIconPaint(icon);
2985 aicon = wAppIconCreateForDock(dock->screen_ptr, NULL,
2986 wm_instance, wm_class, TILE_NORMAL);
2987 /* XXX: can: aicon->icon == NULL ? */
2988 PlaceIcon(dock->screen_ptr, &x0, &y0, wGetHeadForWindow(aicon->icon->owner));
2989 wAppIconMove(aicon, x0, y0);
2990 /* Should this always be lowered? -Dan */
2991 if (dock->lowered)
2992 wLowerFrame(aicon->icon->core);
2993 XMapWindow(dpy, aicon->icon->core->window);
2994 aicon->launching = 1;
2995 wAppIconPaint(aicon);
2996 SlideWindow(aicon->icon->core->window, x0, y0, icon->x_pos, icon->y_pos);
2997 XUnmapWindow(dpy, aicon->icon->core->window);
2998 wAppIconDestroy(aicon);
3000 wDockFinishLaunch(dock, icon);
3001 break;
3005 if (firstPass && !found) {
3006 firstPass = False;
3007 goto retry;
3010 if (command)
3011 wfree(command);
3013 if (wm_class)
3014 XFree(wm_class);
3015 if (wm_instance)
3016 XFree(wm_instance);
3019 void wClipUpdateForWorkspaceChange(WScreen * scr, int workspace)
3021 if (!wPreferences.flags.noclip) {
3022 scr->clip_icon->dock = scr->workspaces[workspace]->clip;
3023 if (scr->current_workspace != workspace) {
3024 WDock *old_clip = scr->workspaces[scr->current_workspace]->clip;
3025 WAppIconChain *chain = scr->global_icons;
3027 while (chain) {
3028 moveIconBetweenDocks(chain->aicon->dock,
3029 scr->workspaces[workspace]->clip,
3030 chain->aicon, chain->aicon->xindex, chain->aicon->yindex);
3031 if (scr->workspaces[workspace]->clip->collapsed)
3032 XUnmapWindow(dpy, chain->aicon->icon->core->window);
3033 chain = chain->next;
3036 wDockHideIcons(old_clip);
3037 if (old_clip->auto_raise_lower) {
3038 if (old_clip->auto_raise_magic) {
3039 WMDeleteTimerHandler(old_clip->auto_raise_magic);
3040 old_clip->auto_raise_magic = NULL;
3042 wDockLower(old_clip);
3044 if (old_clip->auto_collapse) {
3045 if (old_clip->auto_expand_magic) {
3046 WMDeleteTimerHandler(old_clip->auto_expand_magic);
3047 old_clip->auto_expand_magic = NULL;
3049 old_clip->collapsed = 1;
3051 wDockShowIcons(scr->workspaces[workspace]->clip);
3053 if (scr->flags.clip_balloon_mapped)
3054 showClipBalloon(scr->clip_icon->dock, workspace);
3058 static void trackDeadProcess(pid_t pid, unsigned char status, WDock * dock)
3060 WAppIcon *icon;
3061 int i;
3063 for (i = 0; i < dock->max_icons; i++) {
3064 icon = dock->icon_array[i];
3065 if (!icon)
3066 continue;
3068 if (icon->launching && icon->pid == pid) {
3069 if (!icon->relaunching) {
3070 icon->running = 0;
3071 icon->main_window = None;
3073 wDockFinishLaunch(dock, icon);
3074 icon->pid = 0;
3075 if (status == 111) {
3076 char msg[PATH_MAX];
3077 char *cmd;
3079 #ifdef XDND
3080 if (icon->drop_launch)
3081 cmd = icon->dnd_command;
3082 else
3083 #endif
3084 if (icon->paste_launch)
3085 cmd = icon->paste_command;
3086 else
3087 cmd = icon->command;
3089 snprintf(msg, sizeof(msg), _("Could not execute command \"%s\""), cmd);
3091 wMessageDialog(dock->screen_ptr, _("Error"), msg, _("OK"), NULL, NULL);
3093 break;
3098 static void toggleLowered(WDock * dock)
3100 WAppIcon *tmp;
3101 int newlevel, i;
3103 /* lower/raise Dock */
3104 if (!dock->lowered) {
3105 newlevel = WMNormalLevel;
3106 dock->lowered = 1;
3107 } else {
3108 newlevel = WMDockLevel;
3109 dock->lowered = 0;
3112 for (i = 0; i < dock->max_icons; i++) {
3113 tmp = dock->icon_array[i];
3114 if (!tmp)
3115 continue;
3117 ChangeStackingLevel(tmp->icon->core, newlevel);
3118 if (dock->lowered)
3119 wLowerFrame(tmp->icon->core);
3122 if (dock->type == WM_DOCK)
3123 wScreenUpdateUsableArea(dock->screen_ptr);
3126 static void toggleCollapsed(WDock * dock)
3128 if (dock->collapsed) {
3129 dock->collapsed = 0;
3130 wDockShowIcons(dock);
3131 } else {
3132 dock->collapsed = 1;
3133 wDockHideIcons(dock);
3137 static void openDockMenu(WDock * dock, WAppIcon * aicon, XEvent * event)
3139 WScreen *scr = dock->screen_ptr;
3140 WObjDescriptor *desc;
3141 WMenuEntry *entry;
3142 WApplication *wapp = NULL;
3143 int index = 0;
3144 int x_pos;
3145 int n_selected;
3146 int appIsRunning = aicon->running && aicon->icon && aicon->icon->owner;
3148 if (dock->type == WM_DOCK) {
3149 /* keep on top */
3150 entry = dock->menu->entries[index];
3151 entry->flags.indicator_on = !dock->lowered;
3152 entry->clientdata = dock;
3153 dock->menu->flags.realized = 0;
3154 } else {
3155 /* clip options */
3156 if (scr->clip_options)
3157 updateClipOptionsMenu(scr->clip_options, dock);
3159 n_selected = numberOfSelectedIcons(dock);
3161 /* Rename Workspace */
3162 entry = dock->menu->entries[++index];
3163 if (aicon == scr->clip_icon) {
3164 entry->callback = renameCallback;
3165 entry->clientdata = dock;
3166 entry->flags.indicator = 0;
3167 entry->text = _("Rename Workspace");
3168 } else {
3169 entry->callback = omnipresentCallback;
3170 entry->clientdata = aicon;
3171 if (n_selected > 0) {
3172 entry->flags.indicator = 0;
3173 entry->text = _("Toggle Omnipresent");
3174 } else {
3175 entry->flags.indicator = 1;
3176 entry->flags.indicator_on = aicon->omnipresent;
3177 entry->flags.indicator_type = MI_CHECK;
3178 entry->text = _("Omnipresent");
3182 /* select/unselect icon */
3183 entry = dock->menu->entries[++index];
3184 entry->clientdata = aicon;
3185 entry->flags.indicator_on = aicon->icon->selected;
3186 wMenuSetEnabled(dock->menu, index, aicon != scr->clip_icon);
3188 /* select/unselect all icons */
3189 entry = dock->menu->entries[++index];
3190 entry->clientdata = aicon;
3191 if (n_selected > 0)
3192 entry->text = _("Unselect All Icons");
3193 else
3194 entry->text = _("Select All Icons");
3195 wMenuSetEnabled(dock->menu, index, dock->icon_count > 1);
3197 /* keep icon(s) */
3198 entry = dock->menu->entries[++index];
3199 entry->clientdata = aicon;
3200 if (n_selected > 1)
3201 entry->text = _("Keep Icons");
3202 else
3203 entry->text = _("Keep Icon");
3204 wMenuSetEnabled(dock->menu, index, dock->icon_count > 1);
3206 /* this is the workspace submenu part */
3207 entry = dock->menu->entries[++index];
3208 if (n_selected > 1)
3209 entry->text = _("Move Icons To");
3210 else
3211 entry->text = _("Move Icon To");
3212 if (scr->clip_submenu)
3213 updateWorkspaceMenu(scr->clip_submenu, aicon);
3214 wMenuSetEnabled(dock->menu, index, !aicon->omnipresent);
3216 /* remove icon(s) */
3217 entry = dock->menu->entries[++index];
3218 entry->clientdata = aicon;
3219 if (n_selected > 1)
3220 entry->text = _("Remove Icons");
3221 else
3222 entry->text = _("Remove Icon");
3223 wMenuSetEnabled(dock->menu, index, dock->icon_count > 1);
3225 /* attract icon(s) */
3226 entry = dock->menu->entries[++index];
3227 entry->clientdata = aicon;
3229 dock->menu->flags.realized = 0;
3230 wMenuRealize(dock->menu);
3233 if (aicon->icon->owner) {
3234 wapp = wApplicationOf(aicon->icon->owner->main_window);
3235 } else {
3236 wapp = NULL;
3239 /* launch */
3240 entry = dock->menu->entries[++index];
3241 entry->clientdata = aicon;
3242 wMenuSetEnabled(dock->menu, index, aicon->command != NULL);
3244 /* unhide here */
3245 entry = dock->menu->entries[++index];
3246 entry->clientdata = aicon;
3247 if (wapp && wapp->flags.hidden) {
3248 entry->text = _("Unhide Here");
3249 } else {
3250 entry->text = _("Bring Here");
3252 wMenuSetEnabled(dock->menu, index, appIsRunning);
3254 /* hide */
3255 entry = dock->menu->entries[++index];
3256 entry->clientdata = aicon;
3257 if (wapp && wapp->flags.hidden) {
3258 entry->text = _("Unhide");
3259 } else {
3260 entry->text = _("Hide");
3262 wMenuSetEnabled(dock->menu, index, appIsRunning);
3264 /* settings */
3265 entry = dock->menu->entries[++index];
3266 entry->clientdata = aicon;
3267 wMenuSetEnabled(dock->menu, index, !aicon->editing && !wPreferences.flags.noupdates);
3269 /* kill */
3270 entry = dock->menu->entries[++index];
3271 entry->clientdata = aicon;
3272 wMenuSetEnabled(dock->menu, index, appIsRunning);
3274 if (!dock->menu->flags.realized)
3275 wMenuRealize(dock->menu);
3277 if (dock->type == WM_CLIP) {
3278 /*x_pos = event->xbutton.x_root+2; */
3279 x_pos = event->xbutton.x_root - dock->menu->frame->core->width / 2 - 1;
3280 if (x_pos < 0) {
3281 x_pos = 0;
3282 } else if (x_pos + dock->menu->frame->core->width > scr->scr_width - 2) {
3283 x_pos = scr->scr_width - dock->menu->frame->core->width - 4;
3285 } else {
3286 x_pos = dock->on_right_side ? scr->scr_width - dock->menu->frame->core->width - 3 : 0;
3289 wMenuMapAt(dock->menu, x_pos, event->xbutton.y_root + 2, False);
3291 /* allow drag select */
3292 event->xany.send_event = True;
3293 desc = &dock->menu->menu->descriptor;
3294 (*desc->handle_mousedown) (desc, event);
3297 /******************************************************************/
3298 static void iconDblClick(WObjDescriptor * desc, XEvent * event)
3300 WAppIcon *btn = desc->parent;
3301 WDock *dock = btn->dock;
3302 WApplication *wapp = NULL;
3303 int unhideHere = 0;
3305 if (btn->icon->owner && !(event->xbutton.state & ControlMask)) {
3306 wapp = wApplicationOf(btn->icon->owner->main_window);
3308 assert(wapp != NULL);
3310 unhideHere = (event->xbutton.state & ShiftMask);
3312 /* go to the last workspace that the user worked on the app */
3313 if (wapp->last_workspace != dock->screen_ptr->current_workspace && !unhideHere) {
3314 wWorkspaceChange(dock->screen_ptr, wapp->last_workspace);
3317 wUnhideApplication(wapp, event->xbutton.button == Button2, unhideHere);
3319 if (event->xbutton.state & MOD_MASK) {
3320 wHideOtherApplications(btn->icon->owner);
3322 } else {
3323 if (event->xbutton.button == Button1) {
3325 if (event->xbutton.state & MOD_MASK) {
3326 /* raise/lower dock */
3327 toggleLowered(dock);
3328 } else if (btn == dock->screen_ptr->clip_icon) {
3329 if (getClipButton(event->xbutton.x, event->xbutton.y) == CLIP_IDLE)
3330 toggleCollapsed(dock);
3331 else
3332 handleClipChangeWorkspace(dock->screen_ptr, event);
3333 } else if (btn->command) {
3334 if (!btn->launching && (!btn->running || (event->xbutton.state & ControlMask))) {
3335 launchDockedApplication(btn, False);
3337 } else if (btn->xindex == 0 && btn->yindex == 0 && btn->dock->type == WM_DOCK) {
3338 wShowGNUstepPanel(dock->screen_ptr);
3344 static void handleDockMove(WDock * dock, WAppIcon * aicon, XEvent * event)
3346 WScreen *scr = dock->screen_ptr;
3347 int ofs_x = event->xbutton.x, ofs_y = event->xbutton.y;
3348 int x, y;
3349 XEvent ev;
3350 int grabbed = 0, swapped = 0, done;
3351 Pixmap ghost = None;
3352 int superfluous = wPreferences.superfluous; /* we catch it to avoid problems */
3354 if (XGrabPointer(dpy, aicon->icon->core->window, True, ButtonMotionMask
3355 | ButtonReleaseMask | ButtonPressMask, GrabModeAsync,
3356 GrabModeAsync, None, None, CurrentTime) != GrabSuccess) {
3357 wwarning("pointer grab failed for dock move");
3359 y = 0;
3360 for (x = 0; x < dock->max_icons; x++) {
3361 if (dock->icon_array[x] != NULL && dock->icon_array[x]->yindex > y)
3362 y = dock->icon_array[x]->yindex;
3364 y++;
3365 XResizeWindow(dpy, scr->dock_shadow, ICON_SIZE, ICON_SIZE * y);
3367 done = 0;
3368 while (!done) {
3369 WMMaskEvent(dpy, PointerMotionMask | ButtonReleaseMask | ButtonPressMask
3370 | ButtonMotionMask | ExposureMask | EnterWindowMask, &ev);
3371 switch (ev.type) {
3372 case Expose:
3373 WMHandleEvent(&ev);
3374 break;
3376 case EnterNotify:
3377 /* It means the cursor moved so fast that it entered
3378 * something else (if moving slowly, it would have
3379 * stayed in the dock that is being moved. Ignore such
3380 * "spurious" EnterNotifiy's */
3381 break;
3383 case MotionNotify:
3384 if (!grabbed) {
3385 if (abs(ofs_x - ev.xmotion.x) >= MOVE_THRESHOLD
3386 || abs(ofs_y - ev.xmotion.y) >= MOVE_THRESHOLD) {
3387 XChangeActivePointerGrab(dpy, ButtonMotionMask
3388 | ButtonReleaseMask | ButtonPressMask,
3389 wCursor[WCUR_MOVE], CurrentTime);
3390 grabbed = 1;
3392 break;
3394 if (dock->type == WM_CLIP) {
3395 x = ev.xmotion.x_root - ofs_x;
3396 y = ev.xmotion.y_root - ofs_y;
3397 wScreenKeepInside(scr, &x, &y, ICON_SIZE, ICON_SIZE);
3399 moveDock(dock, x, y);
3400 } else {
3401 /* move vertically if pointer is inside the dock */
3402 if ((dock->on_right_side && ev.xmotion.x_root >= dock->x_pos - ICON_SIZE)
3403 || (!dock->on_right_side && ev.xmotion.x_root <= dock->x_pos + ICON_SIZE * 2)) {
3405 x = ev.xmotion.x_root - ofs_x;
3406 y = ev.xmotion.y_root - ofs_y;
3407 wScreenKeepInside(scr, &x, &y, ICON_SIZE, ICON_SIZE);
3408 moveDock(dock, dock->x_pos, y);
3410 /* move horizontally to change sides */
3411 x = ev.xmotion.x_root - ofs_x;
3412 if (!dock->on_right_side) {
3414 /* is on left */
3416 if (ev.xmotion.x_root > dock->x_pos + ICON_SIZE * 2) {
3417 XMoveWindow(dpy, scr->dock_shadow, scr->scr_width - ICON_SIZE
3418 - DOCK_EXTRA_SPACE - 1, dock->y_pos);
3419 if (superfluous && ghost == None) {
3420 ghost = MakeGhostDock(dock, dock->x_pos,
3421 scr->scr_width - ICON_SIZE
3422 - DOCK_EXTRA_SPACE - 1, dock->y_pos);
3423 XSetWindowBackgroundPixmap(dpy, scr->dock_shadow, ghost);
3424 XClearWindow(dpy, scr->dock_shadow);
3426 XMapRaised(dpy, scr->dock_shadow);
3427 swapped = 1;
3428 } else {
3429 if (superfluous && ghost != None) {
3430 XFreePixmap(dpy, ghost);
3431 ghost = None;
3433 XUnmapWindow(dpy, scr->dock_shadow);
3434 swapped = 0;
3436 } else {
3437 /* is on right */
3438 if (ev.xmotion.x_root < dock->x_pos - ICON_SIZE) {
3439 XMoveWindow(dpy, scr->dock_shadow, DOCK_EXTRA_SPACE, dock->y_pos);
3440 if (superfluous && ghost == None) {
3441 ghost = MakeGhostDock(dock, dock->x_pos,
3442 DOCK_EXTRA_SPACE, dock->y_pos);
3443 XSetWindowBackgroundPixmap(dpy, scr->dock_shadow, ghost);
3444 XClearWindow(dpy, scr->dock_shadow);
3446 XMapRaised(dpy, scr->dock_shadow);
3447 swapped = -1;
3448 } else {
3449 XUnmapWindow(dpy, scr->dock_shadow);
3450 swapped = 0;
3451 if (superfluous && ghost != None) {
3452 XFreePixmap(dpy, ghost);
3453 ghost = None;
3458 break;
3460 case ButtonPress:
3461 break;
3463 case ButtonRelease:
3464 if (ev.xbutton.button != event->xbutton.button)
3465 break;
3466 XUngrabPointer(dpy, CurrentTime);
3467 XUnmapWindow(dpy, scr->dock_shadow);
3468 XResizeWindow(dpy, scr->dock_shadow, ICON_SIZE, ICON_SIZE);
3469 if (dock->type == WM_DOCK) {
3470 if (swapped != 0) {
3471 if (swapped > 0)
3472 dock->on_right_side = 1;
3473 else
3474 dock->on_right_side = 0;
3475 swapDock(dock);
3476 wArrangeIcons(scr, False);
3479 done = 1;
3480 break;
3483 if (superfluous) {
3484 if (ghost != None)
3485 XFreePixmap(dpy, ghost);
3486 XSetWindowBackground(dpy, scr->dock_shadow, scr->white_pixel);
3490 static Bool handleIconMove(WDock *dock, WAppIcon *aicon, XEvent *event)
3492 WScreen *scr = dock->screen_ptr;
3493 Window wins[2];
3494 WIcon *icon = aicon->icon;
3495 WDock *dock2 = NULL, *last_dock = dock, *clip = NULL;
3496 int ondock, grabbed = 0, change_dock = 0, collapsed = 0;
3497 XEvent ev;
3498 int x = aicon->x_pos, y = aicon->y_pos;
3499 int ofs_x = event->xbutton.x, ofs_y = event->xbutton.y;
3500 int shad_x = x, shad_y = y;
3501 int ix = aicon->xindex, iy = aicon->yindex;
3502 int tmp;
3503 Pixmap ghost = None;
3504 Bool docked;
3505 int superfluous = wPreferences.superfluous; /* we catch it to avoid problems */
3506 int omnipresent = aicon->omnipresent; /* this must be cached!!! */
3507 Bool hasMoved = False;
3509 if (wPreferences.flags.noupdates)
3510 return hasMoved;
3512 if (XGrabPointer(dpy, icon->core->window, True, ButtonMotionMask
3513 | ButtonReleaseMask | ButtonPressMask, GrabModeAsync,
3514 GrabModeAsync, None, None, CurrentTime) != GrabSuccess) {
3517 if (!(event->xbutton.state & MOD_MASK))
3518 wRaiseFrame(icon->core);
3520 if (!wPreferences.flags.noclip)
3521 clip = scr->workspaces[scr->current_workspace]->clip;
3523 if (dock == scr->dock && !wPreferences.flags.noclip)
3524 dock2 = clip;
3525 else if (dock != scr->dock && !wPreferences.flags.nodock)
3526 dock2 = scr->dock;
3528 wins[0] = icon->core->window;
3529 wins[1] = scr->dock_shadow;
3530 XRestackWindows(dpy, wins, 2);
3531 XMoveResizeWindow(dpy, scr->dock_shadow, aicon->x_pos, aicon->y_pos, ICON_SIZE, ICON_SIZE);
3532 if (superfluous) {
3533 if (icon->pixmap != None)
3534 ghost = MakeGhostIcon(scr, icon->pixmap);
3535 else
3536 ghost = MakeGhostIcon(scr, icon->core->window);
3538 XSetWindowBackgroundPixmap(dpy, scr->dock_shadow, ghost);
3539 XClearWindow(dpy, scr->dock_shadow);
3541 XMapWindow(dpy, scr->dock_shadow);
3543 ondock = 1;
3545 while (1) {
3546 XMaskEvent(dpy, PointerMotionMask | ButtonReleaseMask | ButtonPressMask
3547 | ButtonMotionMask | ExposureMask, &ev);
3548 switch (ev.type) {
3549 case Expose:
3550 WMHandleEvent(&ev);
3551 break;
3553 case MotionNotify:
3554 hasMoved = True;
3555 if (!grabbed) {
3556 if (abs(ofs_x - ev.xmotion.x) >= MOVE_THRESHOLD
3557 || abs(ofs_y - ev.xmotion.y) >= MOVE_THRESHOLD) {
3558 XChangeActivePointerGrab(dpy, ButtonMotionMask
3559 | ButtonReleaseMask | ButtonPressMask,
3560 wCursor[WCUR_MOVE], CurrentTime);
3561 grabbed = 1;
3562 } else {
3563 break;
3567 if (omnipresent) {
3568 int i;
3569 for (i = 0; i < scr->workspace_count; i++) {
3570 if (i == scr->current_workspace)
3571 continue;
3572 wDockShowIcons(scr->workspaces[i]->clip);
3576 x = ev.xmotion.x_root - ofs_x;
3577 y = ev.xmotion.y_root - ofs_y;
3578 tmp = wDockSnapIcon(dock, aicon, x, y, &ix, &iy, True);
3579 if (tmp && dock2) {
3580 change_dock = 0;
3581 if (last_dock != dock && collapsed) {
3582 last_dock->collapsed = 1;
3583 wDockHideIcons(last_dock);
3584 collapsed = 0;
3586 if (!collapsed && (collapsed = dock->collapsed)) {
3587 dock->collapsed = 0;
3588 wDockShowIcons(dock);
3590 if (dock->auto_raise_lower)
3591 wDockRaise(dock);
3592 last_dock = dock;
3593 } else if (dock2) {
3594 tmp = wDockSnapIcon(dock2, aicon, x, y, &ix, &iy, False);
3595 if (tmp) {
3596 change_dock = 1;
3597 if (last_dock != dock2 && collapsed) {
3598 last_dock->collapsed = 1;
3599 wDockHideIcons(last_dock);
3600 collapsed = 0;
3602 if (!collapsed && (collapsed = dock2->collapsed)) {
3603 dock2->collapsed = 0;
3604 wDockShowIcons(dock2);
3606 if (dock2->auto_raise_lower)
3607 wDockRaise(dock2);
3608 last_dock = dock2;
3611 if (aicon->launching || aicon->lock || (aicon->running && !(ev.xmotion.state & MOD_MASK))
3612 || (!aicon->running && tmp)) {
3613 shad_x = last_dock->x_pos + ix * wPreferences.icon_size;
3614 shad_y = last_dock->y_pos + iy * wPreferences.icon_size;
3616 XMoveWindow(dpy, scr->dock_shadow, shad_x, shad_y);
3618 if (!ondock) {
3619 XMapWindow(dpy, scr->dock_shadow);
3621 ondock = 1;
3622 } else {
3623 if (ondock) {
3624 XUnmapWindow(dpy, scr->dock_shadow);
3626 ondock = 0;
3628 XMoveWindow(dpy, icon->core->window, x, y);
3629 break;
3631 case ButtonPress:
3632 break;
3634 case ButtonRelease:
3635 if (ev.xbutton.button != event->xbutton.button)
3636 break;
3637 XUngrabPointer(dpy, CurrentTime);
3638 if (ondock) {
3639 SlideWindow(icon->core->window, x, y, shad_x, shad_y);
3640 XUnmapWindow(dpy, scr->dock_shadow);
3641 if (!change_dock) {
3642 reattachIcon(dock, aicon, ix, iy);
3643 if (clip && dock != clip && clip->auto_raise_lower)
3644 wDockLower(clip);
3645 } else {
3646 docked = moveIconBetweenDocks(dock, dock2, aicon, ix, iy);
3647 if (!docked) {
3648 /* Slide it back if dock rejected it */
3649 SlideWindow(icon->core->window, x, y, aicon->x_pos, aicon->y_pos);
3650 reattachIcon(dock, aicon, aicon->xindex, aicon->yindex);
3652 if (last_dock->type == WM_CLIP && last_dock->auto_collapse) {
3653 collapsed = 0;
3656 } else {
3657 aicon->x_pos = x;
3658 aicon->y_pos = y;
3659 if (superfluous) {
3660 if (!aicon->running && !wPreferences.no_animations) {
3661 /* We need to deselect it, even if is deselected in
3662 * wDockDetach(), because else DoKaboom() will fail.
3664 if (aicon->icon->selected)
3665 wIconSelect(aicon->icon);
3667 DoKaboom(scr, aicon->icon->core->window, x, y);
3670 if (clip && clip->auto_raise_lower)
3671 wDockLower(clip);
3672 wDockDetach(dock, aicon);
3674 if (collapsed) {
3675 last_dock->collapsed = 1;
3676 wDockHideIcons(last_dock);
3677 collapsed = 0;
3679 if (superfluous) {
3680 if (ghost != None)
3681 XFreePixmap(dpy, ghost);
3682 XSetWindowBackground(dpy, scr->dock_shadow, scr->white_pixel);
3684 if (omnipresent) {
3685 int i;
3686 for (i = 0; i < scr->workspace_count; i++) {
3687 if (i == scr->current_workspace)
3688 continue;
3689 wDockHideIcons(scr->workspaces[i]->clip);
3692 return hasMoved;;
3695 return False; /* never reached */
3698 static int getClipButton(int px, int py)
3700 int pt = (CLIP_BUTTON_SIZE + 2) * ICON_SIZE / 64;
3702 if (px < 0 || py < 0 || px >= ICON_SIZE || py >= ICON_SIZE)
3703 return CLIP_IDLE;
3705 if (py <= pt - ((int)ICON_SIZE - 1 - px))
3706 return CLIP_FORWARD;
3707 else if (px <= pt - ((int)ICON_SIZE - 1 - py))
3708 return CLIP_REWIND;
3710 return CLIP_IDLE;
3713 static void handleClipChangeWorkspace(WScreen * scr, XEvent * event)
3715 XEvent ev;
3716 int done, direction, new_ws;
3717 int new_dir;
3718 WDock *clip = scr->clip_icon->dock;
3720 direction = getClipButton(event->xbutton.x, event->xbutton.y);
3722 clip->lclip_button_pushed = direction == CLIP_REWIND;
3723 clip->rclip_button_pushed = direction == CLIP_FORWARD;
3725 wClipIconPaint(scr->clip_icon);
3726 done = 0;
3727 while (!done) {
3728 WMMaskEvent(dpy, ExposureMask | ButtonMotionMask | ButtonReleaseMask | ButtonPressMask, &ev);
3729 switch (ev.type) {
3730 case Expose:
3731 WMHandleEvent(&ev);
3732 break;
3734 case MotionNotify:
3735 new_dir = getClipButton(ev.xmotion.x, ev.xmotion.y);
3736 if (new_dir != direction) {
3737 direction = new_dir;
3738 clip->lclip_button_pushed = direction == CLIP_REWIND;
3739 clip->rclip_button_pushed = direction == CLIP_FORWARD;
3740 wClipIconPaint(scr->clip_icon);
3742 break;
3744 case ButtonPress:
3745 break;
3747 case ButtonRelease:
3748 if (ev.xbutton.button == event->xbutton.button)
3749 done = 1;
3753 clip->lclip_button_pushed = 0;
3754 clip->rclip_button_pushed = 0;
3756 new_ws = wPreferences.ws_advance || (event->xbutton.state & ControlMask);
3758 if (direction == CLIP_FORWARD) {
3759 if (scr->current_workspace < scr->workspace_count - 1)
3760 wWorkspaceChange(scr, scr->current_workspace + 1);
3761 else if (new_ws && scr->current_workspace < MAX_WORKSPACES - 1)
3762 wWorkspaceChange(scr, scr->current_workspace + 1);
3763 else if (wPreferences.ws_cycle)
3764 wWorkspaceChange(scr, 0);
3765 } else if (direction == CLIP_REWIND) {
3766 if (scr->current_workspace > 0)
3767 wWorkspaceChange(scr, scr->current_workspace - 1);
3768 else if (scr->current_workspace == 0 && wPreferences.ws_cycle)
3769 wWorkspaceChange(scr, scr->workspace_count - 1);
3772 wClipIconPaint(scr->clip_icon);
3775 static void iconMouseDown(WObjDescriptor * desc, XEvent * event)
3777 WAppIcon *aicon = desc->parent;
3778 WDock *dock = aicon->dock;
3779 WScreen *scr = aicon->icon->core->screen_ptr;
3781 if (aicon->editing || WCHECK_STATE(WSTATE_MODAL))
3782 return;
3784 scr->last_dock = dock;
3786 if (dock->menu->flags.mapped)
3787 wMenuUnmap(dock->menu);
3789 if (IsDoubleClick(scr, event)) {
3790 /* double-click was not in the main clip icon */
3791 if (dock->type != WM_CLIP || aicon->xindex != 0 || aicon->yindex != 0
3792 || getClipButton(event->xbutton.x, event->xbutton.y) == CLIP_IDLE) {
3793 iconDblClick(desc, event);
3794 return;
3798 if (dock->type == WM_CLIP && scr->flags.clip_balloon_mapped) {
3799 XUnmapWindow(dpy, scr->clip_balloon);
3800 scr->flags.clip_balloon_mapped = 0;
3802 if (event->xbutton.button == Button1) {
3803 if (event->xbutton.state & MOD_MASK)
3804 wDockLower(dock);
3805 else
3806 wDockRaise(dock);
3808 if ((event->xbutton.state & ShiftMask) && aicon != scr->clip_icon && dock->type != WM_DOCK) {
3809 wIconSelect(aicon->icon);
3810 return;
3813 if (aicon->yindex == 0 && aicon->xindex == 0) {
3814 if (getClipButton(event->xbutton.x, event->xbutton.y) != CLIP_IDLE
3815 && dock->type == WM_CLIP)
3816 handleClipChangeWorkspace(scr, event);
3817 else
3818 handleDockMove(dock, aicon, event);
3819 } else {
3820 Bool hasMoved = handleIconMove(dock, aicon, event);
3821 if (wPreferences.single_click && !hasMoved)
3822 iconDblClick(desc, event);
3824 } else if (event->xbutton.button == Button2 && dock->type == WM_CLIP && aicon == scr->clip_icon) {
3825 if (!scr->clip_ws_menu) {
3826 scr->clip_ws_menu = wWorkspaceMenuMake(scr, False);
3828 if (scr->clip_ws_menu) {
3829 WMenu *wsMenu = scr->clip_ws_menu;
3830 int xpos;
3832 wWorkspaceMenuUpdate(scr, wsMenu);
3834 xpos = event->xbutton.x_root - wsMenu->frame->core->width / 2 - 1;
3835 if (xpos < 0) {
3836 xpos = 0;
3837 } else if (xpos + wsMenu->frame->core->width > scr->scr_width - 2) {
3838 xpos = scr->scr_width - wsMenu->frame->core->width - 4;
3840 wMenuMapAt(wsMenu, xpos, event->xbutton.y_root + 2, False);
3842 desc = &wsMenu->menu->descriptor;
3843 event->xany.send_event = True;
3844 (*desc->handle_mousedown) (desc, event);
3846 } else if (event->xbutton.button == Button2 && dock->type == WM_CLIP &&
3847 (event->xbutton.state & ShiftMask) && aicon != scr->clip_icon) {
3848 wClipMakeIconOmnipresent(aicon, !aicon->omnipresent);
3849 } else if (event->xbutton.button == Button3) {
3850 if (event->xbutton.send_event &&
3851 XGrabPointer(dpy, aicon->icon->core->window, True, ButtonMotionMask
3852 | ButtonReleaseMask | ButtonPressMask, GrabModeAsync,
3853 GrabModeAsync, None, None, CurrentTime) != GrabSuccess) {
3854 wwarning("pointer grab failed for dockicon menu");
3855 return;
3858 openDockMenu(dock, aicon, event);
3859 } else if (event->xbutton.button == Button2) {
3860 WAppIcon *btn = desc->parent;
3862 if (!btn->launching && (!btn->running || (event->xbutton.state & ControlMask))) {
3863 launchDockedApplication(btn, True);
3868 static void showClipBalloon(WDock * dock, int workspace)
3870 int w, h;
3871 int x, y;
3872 WScreen *scr = dock->screen_ptr;
3873 char *text;
3874 Window stack[2];
3876 scr->flags.clip_balloon_mapped = 1;
3877 XMapWindow(dpy, scr->clip_balloon);
3879 text = scr->workspaces[workspace]->name;
3881 w = WMWidthOfString(scr->clip_title_font, text, strlen(text));
3883 h = WMFontHeight(scr->clip_title_font);
3884 XResizeWindow(dpy, scr->clip_balloon, w, h);
3886 x = dock->x_pos + CLIP_BUTTON_SIZE * ICON_SIZE / 64;
3887 y = dock->y_pos + ICON_SIZE - WMFontHeight(scr->clip_title_font) - 3;
3889 if (x + w > scr->scr_width) {
3890 x = scr->scr_width - w;
3891 if (dock->y_pos + ICON_SIZE + h > scr->scr_height)
3892 y = dock->y_pos - h - 1;
3893 else
3894 y = dock->y_pos + ICON_SIZE;
3895 XRaiseWindow(dpy, scr->clip_balloon);
3896 } else {
3897 stack[0] = scr->clip_icon->icon->core->window;
3898 stack[1] = scr->clip_balloon;
3899 XRestackWindows(dpy, stack, 2);
3901 XMoveWindow(dpy, scr->clip_balloon, x, y);
3902 XClearWindow(dpy, scr->clip_balloon);
3903 WMDrawString(scr->wmscreen, scr->clip_balloon,
3904 scr->clip_title_color[CLIP_NORMAL], scr->clip_title_font, 0, 0, text, strlen(text));
3907 static void clipEnterNotify(WObjDescriptor * desc, XEvent * event)
3909 WAppIcon *btn = (WAppIcon *) desc->parent;
3910 WDock *dock;
3911 WScreen *scr;
3913 assert(event->type == EnterNotify);
3915 if (desc->parent_type != WCLASS_DOCK_ICON)
3916 return;
3918 scr = btn->icon->core->screen_ptr;
3919 if (!btn->omnipresent)
3920 dock = btn->dock;
3921 else
3922 dock = scr->workspaces[scr->current_workspace]->clip;
3924 if (!dock || dock->type != WM_CLIP)
3925 return;
3927 /* The auto raise/lower code */
3928 if (dock->auto_lower_magic) {
3929 WMDeleteTimerHandler(dock->auto_lower_magic);
3930 dock->auto_lower_magic = NULL;
3932 if (dock->auto_raise_lower && !dock->auto_raise_magic) {
3933 dock->auto_raise_magic = WMAddTimerHandler(AUTO_RAISE_DELAY, clipAutoRaise, (void *)dock);
3936 /* The auto expand/collapse code */
3937 if (dock->auto_collapse_magic) {
3938 WMDeleteTimerHandler(dock->auto_collapse_magic);
3939 dock->auto_collapse_magic = NULL;
3941 if (dock->auto_collapse && !dock->auto_expand_magic) {
3942 dock->auto_expand_magic = WMAddTimerHandler(AUTO_EXPAND_DELAY, clipAutoExpand, (void *)dock);
3945 if (btn->xindex == 0 && btn->yindex == 0)
3946 showClipBalloon(dock, dock->screen_ptr->current_workspace);
3947 else {
3948 if (dock->screen_ptr->flags.clip_balloon_mapped) {
3949 XUnmapWindow(dpy, dock->screen_ptr->clip_balloon);
3950 dock->screen_ptr->flags.clip_balloon_mapped = 0;
3955 static void clipLeave(WDock * dock)
3957 XEvent event;
3958 WObjDescriptor *desc = NULL;
3960 if (!dock || dock->type != WM_CLIP)
3961 return;
3963 if (XCheckTypedEvent(dpy, EnterNotify, &event) != False) {
3964 if (XFindContext(dpy, event.xcrossing.window, wWinContext,
3965 (XPointer *) & desc) != XCNOENT
3966 && desc && desc->parent_type == WCLASS_DOCK_ICON
3967 && ((WAppIcon *) desc->parent)->dock && ((WAppIcon *) desc->parent)->dock->type == WM_CLIP) {
3968 /* We didn't left the Clip yet */
3969 XPutBackEvent(dpy, &event);
3970 return;
3973 XPutBackEvent(dpy, &event);
3974 } else {
3975 /* We entered a withdrawn window, so we're still in Clip */
3976 return;
3979 if (dock->auto_raise_magic) {
3980 WMDeleteTimerHandler(dock->auto_raise_magic);
3981 dock->auto_raise_magic = NULL;
3983 if (dock->auto_raise_lower && !dock->auto_lower_magic) {
3984 dock->auto_lower_magic = WMAddTimerHandler(AUTO_LOWER_DELAY, clipAutoLower, (void *)dock);
3987 if (dock->auto_expand_magic) {
3988 WMDeleteTimerHandler(dock->auto_expand_magic);
3989 dock->auto_expand_magic = NULL;
3991 if (dock->auto_collapse && !dock->auto_collapse_magic) {
3992 dock->auto_collapse_magic = WMAddTimerHandler(AUTO_COLLAPSE_DELAY, clipAutoCollapse, (void *)dock);
3996 static void clipLeaveNotify(WObjDescriptor * desc, XEvent * event)
3998 WAppIcon *btn = (WAppIcon *) desc->parent;
4000 assert(event->type == LeaveNotify);
4002 if (desc->parent_type != WCLASS_DOCK_ICON)
4003 return;
4005 clipLeave(btn->dock);
4008 static void clipAutoCollapse(void *cdata)
4010 WDock *dock = (WDock *) cdata;
4012 if (dock->type != WM_CLIP)
4013 return;
4015 if (dock->auto_collapse) {
4016 dock->collapsed = 1;
4017 wDockHideIcons(dock);
4019 dock->auto_collapse_magic = NULL;
4022 static void clipAutoExpand(void *cdata)
4024 WDock *dock = (WDock *) cdata;
4026 if (dock->type != WM_CLIP)
4027 return;
4029 if (dock->auto_collapse) {
4030 dock->collapsed = 0;
4031 wDockShowIcons(dock);
4033 dock->auto_expand_magic = NULL;
4036 static void clipAutoLower(void *cdata)
4038 WDock *dock = (WDock *) cdata;
4040 if (dock->type != WM_CLIP)
4041 return;
4043 if (dock->auto_raise_lower)
4044 wDockLower(dock);
4046 dock->auto_lower_magic = NULL;
4049 static void clipAutoRaise(void *cdata)
4051 WDock *dock = (WDock *) cdata;
4053 if (dock->type != WM_CLIP)
4054 return;
4056 if (dock->auto_raise_lower)
4057 wDockRaise(dock);
4059 if (dock->screen_ptr->flags.clip_balloon_mapped) {
4060 showClipBalloon(dock, dock->screen_ptr->current_workspace);
4063 dock->auto_raise_magic = NULL;
4066 static Bool iconCanBeOmnipresent(WAppIcon * aicon)
4068 WScreen *scr = aicon->icon->core->screen_ptr;
4069 WDock *clip;
4070 WAppIcon *btn;
4071 int i, j;
4073 for (i = 0; i < scr->workspace_count; i++) {
4074 clip = scr->workspaces[i]->clip;
4076 if (clip == aicon->dock)
4077 continue;
4079 if (clip->icon_count + scr->global_icon_count >= clip->max_icons)
4080 return False; /* Clip is full in some workspace */
4082 for (j = 0; j < clip->max_icons; j++) {
4083 btn = clip->icon_array[j];
4084 if (btn && btn->xindex == aicon->xindex && btn->yindex == aicon->yindex)
4085 return False;
4089 return True;
4092 int wClipMakeIconOmnipresent(WAppIcon * aicon, int omnipresent)
4094 WScreen *scr = aicon->icon->core->screen_ptr;
4095 WAppIconChain *new_entry, *tmp, *tmp1;
4096 int status = WO_SUCCESS;
4098 if ((scr->dock && aicon->dock == scr->dock) || aicon == scr->clip_icon) {
4099 return WO_NOT_APPLICABLE;
4102 if (aicon->omnipresent == omnipresent)
4103 return WO_SUCCESS;
4105 if (omnipresent) {
4106 if (iconCanBeOmnipresent(aicon)) {
4107 aicon->omnipresent = 1;
4108 new_entry = wmalloc(sizeof(WAppIconChain));
4109 new_entry->aicon = aicon;
4110 new_entry->next = scr->global_icons;
4111 scr->global_icons = new_entry;
4112 scr->global_icon_count++;
4113 } else {
4114 aicon->omnipresent = 0;
4115 status = WO_FAILED;
4117 } else {
4118 aicon->omnipresent = 0;
4119 if (aicon == scr->global_icons->aicon) {
4120 tmp = scr->global_icons->next;
4121 wfree(scr->global_icons);
4122 scr->global_icons = tmp;
4123 scr->global_icon_count--;
4124 } else {
4125 tmp = scr->global_icons;
4126 while (tmp->next) {
4127 if (tmp->next->aicon == aicon) {
4128 tmp1 = tmp->next->next;
4129 wfree(tmp->next);
4130 tmp->next = tmp1;
4131 scr->global_icon_count--;
4132 break;
4134 tmp = tmp->next;
4139 wAppIconPaint(aicon);
4141 return status;