small fix in wtest
[wmaker-crm.git] / src / dock.c
blob453a1c9e1a2db51bb9cbb6c29b59b7e314615c57
1 /* dock.c- built-in Dock module for WindowMaker
3 * Window Maker window manager
5 * Copyright (c) 1997-2002 Alfredo K. Kojima
6 * Copyright (c) 1998-2002 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.
25 #include "wconfig.h"
27 #include <X11/Xlib.h>
28 #include <X11/Xutil.h>
29 #include <stdlib.h>
30 #include <string.h>
31 #include <unistd.h>
32 #include <math.h>
33 #include <limits.h>
35 #ifndef PATH_MAX
36 #define PATH_MAX DEFAULT_PATH_MAX
37 #endif
39 #include "WindowMaker.h"
40 #include "wcore.h"
41 #include "window.h"
42 #include "icon.h"
43 #include "appicon.h"
44 #include "actions.h"
45 #include "stacking.h"
46 #include "dock.h"
47 #include "dialog.h"
48 #include "funcs.h"
49 #include "properties.h"
50 #include "menu.h"
51 #include "client.h"
52 #include "defaults.h"
53 #include "workspace.h"
54 #include "framewin.h"
55 #include "superfluous.h"
56 #include "wsound.h"
59 #include <WINGs/WUtil.h>
64 /**** Local variables ****/
65 #define CLIP_REWIND 1
66 #define CLIP_IDLE 0
67 #define CLIP_FORWARD 2
70 /**** Global variables ****/
72 /* in dockedapp.c */
73 extern void DestroyDockAppSettingsPanel();
75 extern void ShowDockAppSettingsPanel(WAppIcon *aicon);
78 extern XContext wWinContext;
80 extern Cursor wCursor[WCUR_LAST];
82 extern WPreferences wPreferences;
84 extern XContext wWinContext;
86 #ifdef OFFIX_DND
87 extern Atom _XA_DND_PROTOCOL;
88 #endif
91 #define MOD_MASK wPreferences.modifier_mask
93 extern void appIconMouseDown(WObjDescriptor *desc, XEvent *event);
95 #define ICON_SIZE wPreferences.icon_size
98 /***** Local variables ****/
100 static WMPropList *dCommand=NULL;
101 static WMPropList *dPasteCommand=NULL;
102 #ifdef OFFIX_DND
103 static WMPropList *dDropCommand=NULL;
104 #endif
105 static WMPropList *dAutoLaunch, *dLock;
106 static WMPropList *dName, *dForced, *dBuggyApplication, *dYes, *dNo;
107 static WMPropList *dHost, *dDock, *dClip;
108 static WMPropList *dAutoAttractIcons;
110 static WMPropList *dPosition, *dApplications, *dLowered, *dCollapsed;
112 static WMPropList *dAutoCollapse, *dAutoRaiseLower, *dOmnipresent;
114 static void dockIconPaint(WAppIcon *btn);
116 static void iconMouseDown(WObjDescriptor *desc, XEvent *event);
118 static pid_t execCommand(WAppIcon *btn, char *command, WSavedState *state);
120 static void trackDeadProcess(pid_t pid, unsigned char status, WDock *dock);
122 static int getClipButton(int px, int py);
124 static void toggleLowered(WDock *dock);
126 static void toggleCollapsed(WDock *dock);
128 static void clipIconExpose(WObjDescriptor *desc, XEvent *event);
130 static void clipLeave(WDock *dock);
132 static void handleClipChangeWorkspace(WScreen *scr, XEvent *event);
134 Bool moveIconBetweenDocks(WDock *src, WDock *dest, WAppIcon *icon, int x, int y);
136 static void clipEnterNotify(WObjDescriptor *desc, XEvent *event);
137 static void clipLeaveNotify(WObjDescriptor *desc, XEvent *event);
138 static void clipAutoCollapse(void *cdata);
139 static void clipAutoExpand(void *cdata);
140 static void launchDockedApplication(WAppIcon *btn, Bool withSelection);
142 static void clipAutoLower(void *cdata);
143 static void clipAutoRaise(void *cdata);
145 static void showClipBalloon(WDock *dock, int workspace);
147 #ifdef OFFIX_DND
149 #define DndNotDnd -1
150 #define DndUnknown 0
151 #define DndRawData 1
152 #define DndFile 2
153 #define DndFiles 3
154 #define DndText 4
155 #define DndDir 5
156 #define DndLink 6
157 #define DndExe 7
159 #define DndEND 8
161 #endif /* OFFIX_DND */
165 static void
166 make_keys()
168 if (dCommand!=NULL)
169 return;
171 dCommand = WMRetainPropList(WMCreatePLString("Command"));
172 dPasteCommand = WMRetainPropList(WMCreatePLString("PasteCommand"));
173 dDropCommand = WMRetainPropList(WMCreatePLString("DropCommand"));
174 dLock = WMRetainPropList(WMCreatePLString("Lock"));
175 dAutoLaunch = WMRetainPropList(WMCreatePLString("AutoLaunch"));
176 dName = WMRetainPropList(WMCreatePLString("Name"));
177 dForced = WMRetainPropList(WMCreatePLString("Forced"));
178 dBuggyApplication = WMRetainPropList(WMCreatePLString("BuggyApplication"));
179 dYes = WMRetainPropList(WMCreatePLString("Yes"));
180 dNo = WMRetainPropList(WMCreatePLString("No"));
181 dHost = WMRetainPropList(WMCreatePLString("Host"));
183 dPosition = WMCreatePLString("Position");
184 dApplications = WMCreatePLString("Applications");
185 dLowered = WMCreatePLString("Lowered");
186 dCollapsed = WMCreatePLString("Collapsed");
187 dAutoCollapse = WMCreatePLString("AutoCollapse");
188 dAutoRaiseLower = WMCreatePLString("AutoRaiseLower");
189 dAutoAttractIcons = WMCreatePLString("AutoAttractIcons");
191 dOmnipresent = WMCreatePLString("Omnipresent");
193 dDock = WMCreatePLString("Dock");
194 dClip = WMCreatePLString("Clip");
199 static void
200 renameCallback(WMenu *menu, WMenuEntry *entry)
202 WDock *dock = entry->clientdata;
203 char buffer[128];
204 int wspace;
205 char *name;
207 assert(entry->clientdata!=NULL);
209 wspace = dock->screen_ptr->current_workspace;
211 name = wstrdup(dock->screen_ptr->workspaces[wspace]->name);
213 snprintf(buffer, sizeof(buffer), _("Type the name for workspace %i:"), wspace+1);
214 if (wInputDialog(dock->screen_ptr, _("Rename Workspace"), buffer,
215 &name)) {
216 wWorkspaceRename(dock->screen_ptr, wspace, name);
218 if (name) {
219 wfree(name);
224 static void
225 toggleLoweredCallback(WMenu *menu, WMenuEntry *entry)
227 assert(entry->clientdata!=NULL);
229 toggleLowered(entry->clientdata);
231 entry->flags.indicator_on = !(((WDock*)entry->clientdata)->lowered);
233 wMenuPaint(menu);
237 static int
238 matchWindow(void *item, void *cdata)
240 return (((WFakeGroupLeader*)item)->leader == (Window)cdata);
244 static void
245 killCallback(WMenu *menu, WMenuEntry *entry)
247 WScreen *scr = menu->menu->screen_ptr;
248 WAppIcon *icon;
249 WFakeGroupLeader *fPtr;
250 char *buffer;
252 if (!WCHECK_STATE(WSTATE_NORMAL))
253 return;
255 assert(entry->clientdata!=NULL);
257 icon = (WAppIcon*)entry->clientdata;
259 icon->editing = 1;
261 WCHANGE_STATE(WSTATE_MODAL);
263 buffer = wstrconcat(icon->wm_class,
264 _(" will be forcibly closed.\n"
265 "Any unsaved changes will be lost.\n"
266 "Please confirm."));
268 if (icon->icon && icon->icon->owner) {
269 fPtr = icon->icon->owner->fake_group;
270 } else {
271 /* is this really necessary? can we kill a dock icon not running? */
272 Window win = icon->main_window;
273 int index;
275 index = WMFindInArray(scr->fakeGroupLeaders, matchWindow, (void*)win);
276 if (index != WANotFound)
277 fPtr = WMGetFromArray(scr->fakeGroupLeaders, index);
278 else
279 fPtr = NULL;
282 if (wPreferences.dont_confirm_kill
283 || wMessageDialog(menu->frame->screen_ptr, _("Kill Application"),
284 buffer, _("Yes"), _("No"), NULL)==WAPRDefault) {
285 if (fPtr!=NULL) {
286 WWindow *wwin, *twin;
288 wwin = scr->focused_window;
289 while (wwin) {
290 twin = wwin->prev;
291 if (wwin->fake_group == fPtr) {
292 wClientKill(wwin);
294 wwin = twin;
296 } else if (icon->icon && icon->icon->owner) {
297 wClientKill(icon->icon->owner);
301 wfree(buffer);
303 icon->editing = 0;
305 WCHANGE_STATE(WSTATE_NORMAL);
309 /* TODO: replace this function with a member of the dock struct */
310 static int
311 numberOfSelectedIcons(WDock *dock)
313 WAppIcon *aicon;
314 int i, n;
316 n = 0;
317 for (i=1; i<dock->max_icons; i++) {
318 aicon = dock->icon_array[i];
319 if (aicon && aicon->icon->selected) {
320 n++;
324 return n;
328 static WMArray*
329 getSelected(WDock *dock)
331 WMArray *ret = WMCreateArray(8);
332 WAppIcon *btn;
333 int i;
335 for (i=1; i<dock->max_icons; i++) {
336 btn = dock->icon_array[i];
337 if (btn && btn->icon->selected) {
338 WMAddToArray(ret, btn);
342 return ret;
346 static void
347 paintClipButtons(WAppIcon *clipIcon, Bool lpushed, Bool rpushed)
349 Window win = clipIcon->icon->core->window;
350 WScreen *scr = clipIcon->icon->core->screen_ptr;
351 XPoint p[4];
352 int pt = CLIP_BUTTON_SIZE*ICON_SIZE/64;
353 int tp = ICON_SIZE - pt;
354 int as = pt - 15; /* 15 = 5+5+5 */
355 GC gc = scr->clip_title_gc;
356 #ifdef GRADIENT_CLIP_ARROW
357 Bool collapsed = clipIcon->dock->collapsed;
358 #endif
360 if (rpushed) {
361 p[0].x = tp+1;
362 p[0].y = 1;
363 p[1].x = ICON_SIZE-2;
364 p[1].y = 1;
365 p[2].x = ICON_SIZE-2;
366 p[2].y = pt-1;
367 } else if (lpushed) {
368 p[0].x = 1;
369 p[0].y = tp;
370 p[1].x = pt;
371 p[1].y = ICON_SIZE-2;
372 p[2].x = 1;
373 p[2].y = ICON_SIZE-2;
375 if (lpushed || rpushed) {
376 XSetForeground(dpy, scr->draw_gc, scr->white_pixel);
377 XFillPolygon(dpy, win, scr->draw_gc, p, 3, Convex, CoordModeOrigin);
378 XSetForeground(dpy, scr->draw_gc, scr->black_pixel);
380 #ifdef GRADIENT_CLIP_ARROW
381 if (!collapsed) {
382 XSetFillStyle(dpy, scr->copy_gc, FillTiled);
383 XSetTile(dpy, scr->copy_gc, scr->clip_arrow_gradient);
384 XSetClipMask(dpy, scr->copy_gc, None);
385 gc = scr->copy_gc;
387 #endif /* GRADIENT_CLIP_ARROW */
389 /* top right arrow */
390 p[0].x = p[3].x = ICON_SIZE-5-as;
391 p[0].y = p[3].y = 5;
392 p[1].x = ICON_SIZE-6;
393 p[1].y = 5;
394 p[2].x = ICON_SIZE-6;
395 p[2].y = 4+as;
396 if (rpushed) {
397 XFillPolygon(dpy, win, scr->draw_gc, p, 3, Convex, CoordModeOrigin);
398 XDrawLines(dpy, win, scr->draw_gc, p, 4, CoordModeOrigin);
399 } else {
400 #ifdef GRADIENT_CLIP_ARROW
401 if (!collapsed)
402 XSetTSOrigin(dpy, gc, ICON_SIZE-6-as, 5);
403 #endif
404 XFillPolygon(dpy, win, gc, p,3,Convex,CoordModeOrigin);
405 XDrawLines(dpy, win, gc, p,4,CoordModeOrigin);
408 /* bottom left arrow */
409 p[0].x = p[3].x = 5;
410 p[0].y = p[3].y = ICON_SIZE-5-as;
411 p[1].x = 5;
412 p[1].y = ICON_SIZE-6;
413 p[2].x = 4+as;
414 p[2].y = ICON_SIZE-6;
415 if (lpushed) {
416 XFillPolygon(dpy, win, scr->draw_gc, p, 3, Convex, CoordModeOrigin);
417 XDrawLines(dpy, win, scr->draw_gc, p, 4, CoordModeOrigin);
418 } else {
419 #ifdef GRADIENT_CLIP_ARROW
420 if (!collapsed)
421 XSetTSOrigin(dpy, gc, 5, ICON_SIZE-6-as);
422 #endif
423 XFillPolygon(dpy, win, gc, p,3,Convex,CoordModeOrigin);
424 XDrawLines(dpy, win, gc, p,4,CoordModeOrigin);
426 #ifdef GRADIENT_CLIP_ARROW
427 if (!collapsed)
428 XSetFillStyle(dpy, scr->copy_gc, FillSolid);
429 #endif
433 RImage*
434 wClipMakeTile(WScreen *scr, RImage *normalTile)
436 RImage *tile = RCloneImage(normalTile);
437 RColor black;
438 RColor dark;
439 RColor light;
440 int pt, tp;
441 int as;
443 pt = CLIP_BUTTON_SIZE*wPreferences.icon_size/64;
444 tp = wPreferences.icon_size-1 - pt;
445 as = pt - 15;
447 black.alpha = 255;
448 black.red = black.green = black.blue = 0;
450 dark.alpha = 0;
451 dark.red = dark.green = dark.blue = 60;
453 light.alpha = 0;
454 light.red = light.green = light.blue = 80;
457 /* top right */
458 ROperateLine(tile, RSubtractOperation, tp, 0, wPreferences.icon_size-2,
459 pt-1, &dark);
460 RDrawLine(tile, tp-1, 0, wPreferences.icon_size-1, pt+1, &black);
461 ROperateLine(tile, RAddOperation, tp, 2, wPreferences.icon_size-3,
462 pt, &light);
464 /* arrow bevel */
465 ROperateLine(tile, RSubtractOperation, ICON_SIZE - 7 - as, 4,
466 ICON_SIZE - 5, 4, &dark);
467 ROperateLine(tile, RSubtractOperation, ICON_SIZE - 6 - as, 5,
468 ICON_SIZE - 5, 6 + as, &dark);
469 ROperateLine(tile, RAddOperation, ICON_SIZE - 5, 4, ICON_SIZE - 5, 6 + as,
470 &light);
472 /* bottom left */
473 ROperateLine(tile, RAddOperation, 2, tp+2, pt-2,
474 wPreferences.icon_size-3, &dark);
475 RDrawLine(tile, 0, tp-1, pt+1, wPreferences.icon_size-1, &black);
476 ROperateLine(tile, RSubtractOperation, 0, tp-2, pt+1,
477 wPreferences.icon_size-2, &light);
479 /* arrow bevel */
480 ROperateLine(tile, RSubtractOperation, 4, ICON_SIZE - 7 - as, 4,
481 ICON_SIZE - 5, &dark);
482 ROperateLine(tile, RSubtractOperation, 5, ICON_SIZE - 6 - as,
483 6 + as, ICON_SIZE - 5, &dark);
484 ROperateLine(tile, RAddOperation, 4, ICON_SIZE - 5, 6 + as, ICON_SIZE - 5,
485 &light);
487 return tile;
491 static void
492 omnipresentCallback(WMenu *menu, WMenuEntry *entry)
494 WAppIcon *clickedIcon = entry->clientdata;
495 WAppIcon *aicon;
496 WDock *dock;
497 WMArray *selectedIcons;
498 WMArrayIterator iter;
499 int failed;
501 assert(entry->clientdata!=NULL);
503 dock = clickedIcon->dock;
505 selectedIcons = getSelected(dock);
507 if (!WMGetArrayItemCount(selectedIcons))
508 WMAddToArray(selectedIcons, clickedIcon);
510 failed = 0;
511 WM_ITERATE_ARRAY(selectedIcons, aicon, iter) {
512 if (wClipMakeIconOmnipresent(aicon, !aicon->omnipresent) == WO_FAILED)
513 failed++;
514 else if (aicon->icon->selected)
515 wIconSelect(aicon->icon);
517 WMFreeArray(selectedIcons);
519 if (failed > 1) {
520 wMessageDialog(dock->screen_ptr, _("Warning"),
521 _("Some icons cannot be made omnipresent. "
522 "Please make sure that no other icon is "
523 "docked in the same positions on the other "
524 "workspaces and the Clip is not full in "
525 "some workspace."),
526 _("OK"), NULL, NULL);
527 } else if (failed == 1) {
528 wMessageDialog(dock->screen_ptr, _("Warning"),
529 _("Icon cannot be made omnipresent. "
530 "Please make sure that no other icon is "
531 "docked in the same position on the other "
532 "workspaces and the Clip is not full in "
533 "some workspace."),
534 _("OK"), NULL, NULL);
539 static void
540 removeIconsCallback(WMenu *menu, WMenuEntry *entry)
542 WAppIcon *clickedIcon = (WAppIcon*)entry->clientdata;
543 WDock *dock;
544 WAppIcon *aicon;
545 WMArray *selectedIcons;
546 int keepit;
547 WMArrayIterator it;
549 assert(clickedIcon!=NULL);
551 dock = clickedIcon->dock;
553 selectedIcons = getSelected(dock);
555 if (WMGetArrayItemCount(selectedIcons)) {
556 if (wMessageDialog(dock->screen_ptr, _("Workspace Clip"),
557 _("All selected icons will be removed!"),
558 _("OK"), _("Cancel"), NULL)!=WAPRDefault) {
559 WMFreeArray(selectedIcons);
560 return;
562 } else {
563 if (clickedIcon->xindex==0 && clickedIcon->yindex==0) {
564 WMFreeArray(selectedIcons);
565 return;
567 WMAddToArray(selectedIcons, clickedIcon);
570 WM_ITERATE_ARRAY(selectedIcons, aicon, it) {
571 keepit = aicon->running && wApplicationOf(aicon->main_window);
572 wDockDetach(dock, aicon);
573 if (keepit) {
574 PlaceIcon(dock->screen_ptr, &aicon->x_pos, &aicon->y_pos);
575 XMoveWindow(dpy, aicon->icon->core->window,
576 aicon->x_pos, aicon->y_pos);
577 if (!dock->mapped || dock->collapsed)
578 XMapWindow(dpy, aicon->icon->core->window);
581 WMFreeArray(selectedIcons);
583 if (wPreferences.auto_arrange_icons)
584 wArrangeIcons(dock->screen_ptr, True);
588 static void
589 keepIconsCallback(WMenu *menu, WMenuEntry *entry)
591 WAppIcon *clickedIcon = (WAppIcon*)entry->clientdata;
592 WDock *dock;
593 WAppIcon *aicon;
594 WMArray *selectedIcons;
595 WMArrayIterator it;
597 assert(clickedIcon!=NULL);
598 dock = clickedIcon->dock;
600 selectedIcons = getSelected(dock);
602 if (!WMGetArrayItemCount(selectedIcons)
603 && clickedIcon!=dock->screen_ptr->clip_icon) {
604 char *command = NULL;
606 if (!clickedIcon->command && !clickedIcon->editing) {
607 clickedIcon->editing = 1;
608 if (wInputDialog(dock->screen_ptr, _("Keep Icon"),
609 _("Type the command used to launch the application"),
610 &command)) {
611 if (command && (command[0]==0 ||
612 (command[0]=='-' && command[1]==0))) {
613 wfree(command);
614 command = NULL;
616 clickedIcon->command = command;
617 clickedIcon->editing = 0;
618 } else {
619 clickedIcon->editing = 0;
620 if (command)
621 wfree(command);
622 WMFreeArray(selectedIcons);
623 return;
627 WMAddToArray(selectedIcons, clickedIcon);
630 WM_ITERATE_ARRAY(selectedIcons, aicon, it) {
631 if (aicon->icon->selected)
632 wIconSelect(aicon->icon);
633 if (aicon && aicon->attracted && aicon->command) {
634 aicon->attracted = 0;
635 if (aicon->icon->shadowed) {
636 aicon->icon->shadowed = 0;
637 aicon->icon->force_paint = 1;
638 wAppIconPaint(aicon);
642 WMFreeArray(selectedIcons);
648 static void
649 toggleAutoAttractCallback(WMenu *menu, WMenuEntry *entry)
651 WDock *dock = (WDock*)entry->clientdata;
653 assert(entry->clientdata!=NULL);
655 dock->attract_icons = !dock->attract_icons;
656 /*if (!dock->attract_icons)
657 dock->keep_attracted = 0;*/
659 entry->flags.indicator_on = dock->attract_icons;
661 wMenuPaint(menu);
665 static void
666 selectCallback(WMenu *menu, WMenuEntry *entry)
668 WAppIcon *icon = (WAppIcon*)entry->clientdata;
670 assert(icon!=NULL);
672 wIconSelect(icon->icon);
674 wMenuPaint(menu);
678 static void
679 colectIconsCallback(WMenu *menu, WMenuEntry *entry)
681 WAppIcon *clickedIcon = (WAppIcon*)entry->clientdata;
682 WDock *clip;
683 WAppIcon *aicon;
684 int x, y, x_pos, y_pos;
686 assert(entry->clientdata!=NULL);
687 clip = clickedIcon->dock;
689 aicon = clip->screen_ptr->app_icon_list;
691 while (aicon) {
692 if (!aicon->docked && wDockFindFreeSlot(clip, &x, &y)) {
693 x_pos = clip->x_pos + x*ICON_SIZE;
694 y_pos = clip->y_pos + y*ICON_SIZE;
695 if (aicon->x_pos != x_pos || aicon->y_pos != y_pos) {
696 #ifdef ANIMATIONS
697 if (wPreferences.no_animations) {
698 XMoveWindow(dpy, aicon->icon->core->window, x_pos, y_pos);
699 } else {
700 SlideWindow(aicon->icon->core->window,
701 aicon->x_pos, aicon->y_pos, x_pos, y_pos);
703 #else
704 XMoveWindow(dpy, aicon->icon->core->window, x_pos, y_pos);
705 #endif /* ANIMATIONS */
707 aicon->attracted = 1;
708 if (!aicon->icon->shadowed) {
709 aicon->icon->shadowed = 1;
710 aicon->icon->force_paint = 1;
711 /* We don't do an wAppIconPaint() here because it's in
712 * wDockAttachIcon(). -Dan
715 wDockAttachIcon(clip, aicon, x, y);
716 if (clip->collapsed || !clip->mapped)
717 XUnmapWindow(dpy, aicon->icon->core->window);
719 aicon = aicon->next;
724 static void
725 selectIconsCallback(WMenu *menu, WMenuEntry *entry)
727 WAppIcon *clickedIcon = (WAppIcon*)entry->clientdata;
728 WDock *dock;
729 WMArray *selectedIcons;
730 WMArrayIterator iter;
731 WAppIcon *btn;
732 int i;
734 assert(clickedIcon!=NULL);
735 dock = clickedIcon->dock;
737 selectedIcons = getSelected(dock);
739 if (!WMGetArrayItemCount(selectedIcons)) {
740 for (i=1; i<dock->max_icons; i++) {
741 btn = dock->icon_array[i];
742 if (btn && !btn->icon->selected) {
743 wIconSelect(btn->icon);
746 } else {
747 WM_ITERATE_ARRAY(selectedIcons, btn, iter) {
748 wIconSelect(btn->icon);
751 WMFreeArray(selectedIcons);
753 wMenuPaint(menu);
757 static void
758 toggleCollapsedCallback(WMenu *menu, WMenuEntry *entry)
760 assert(entry->clientdata!=NULL);
762 toggleCollapsed(entry->clientdata);
764 entry->flags.indicator_on = ((WDock*)entry->clientdata)->collapsed;
766 wMenuPaint(menu);
770 static void
771 toggleAutoCollapseCallback(WMenu *menu, WMenuEntry *entry)
773 WDock *dock;
774 assert(entry->clientdata!=NULL);
776 dock = (WDock*) entry->clientdata;
778 dock->auto_collapse = !dock->auto_collapse;
780 entry->flags.indicator_on = ((WDock*)entry->clientdata)->auto_collapse;
782 wMenuPaint(menu);
786 static void
787 toggleAutoRaiseLowerCallback(WMenu *menu, WMenuEntry *entry)
789 WDock *dock;
790 assert(entry->clientdata!=NULL);
792 dock = (WDock*) entry->clientdata;
794 dock->auto_raise_lower = !dock->auto_raise_lower;
796 entry->flags.indicator_on = ((WDock*)entry->clientdata)->auto_raise_lower;
798 wMenuPaint(menu);
802 static void
803 launchCallback(WMenu *menu, WMenuEntry *entry)
805 WAppIcon *btn = (WAppIcon*)entry->clientdata;
807 launchDockedApplication(btn, False);
811 static void
812 settingsCallback(WMenu *menu, WMenuEntry *entry)
814 WAppIcon *btn = (WAppIcon*)entry->clientdata;
816 if (btn->editing)
817 return;
818 ShowDockAppSettingsPanel(btn);
822 static void
823 hideCallback(WMenu *menu, WMenuEntry *entry)
825 WApplication *wapp;
826 WAppIcon *btn = (WAppIcon*)entry->clientdata;
828 wapp = wApplicationOf(btn->icon->owner->main_window);
830 if (wapp->flags.hidden) {
831 wWorkspaceChange(btn->icon->core->screen_ptr, wapp->last_workspace);
832 wUnhideApplication(wapp, False, False);
833 } else {
834 wHideApplication(wapp);
839 static void
840 unhideHereCallback(WMenu *menu, WMenuEntry *entry)
842 WApplication *wapp;
843 WAppIcon *btn = (WAppIcon*)entry->clientdata;
845 wapp = wApplicationOf(btn->icon->owner->main_window);
847 wUnhideApplication(wapp, False, True);
851 WAppIcon*
852 mainIconCreate(WScreen *scr, int type)
854 WAppIcon *btn;
855 int x_pos;
857 if (type == WM_CLIP) {
858 if (scr->clip_icon)
859 return scr->clip_icon;
860 btn = wAppIconCreateForDock(scr, NULL, "Logo", "WMClip", TILE_CLIP);
861 btn->icon->core->descriptor.handle_expose = clipIconExpose;
862 btn->icon->core->descriptor.handle_enternotify = clipEnterNotify;
863 btn->icon->core->descriptor.handle_leavenotify = clipLeaveNotify;
864 /*x_pos = scr->scr_width - ICON_SIZE*2 - DOCK_EXTRA_SPACE;*/
865 x_pos = 0;
866 } else {
867 btn = wAppIconCreateForDock(scr, NULL, "Logo", "WMDock", TILE_NORMAL);
868 x_pos = scr->scr_width - ICON_SIZE - DOCK_EXTRA_SPACE;
871 btn->xindex = 0;
872 btn->yindex = 0;
874 btn->icon->core->descriptor.handle_mousedown = iconMouseDown;
875 btn->icon->core->descriptor.parent_type = WCLASS_DOCK_ICON;
876 btn->icon->core->descriptor.parent = btn;
877 /*ChangeStackingLevel(btn->icon->core, WMDockLevel);*/
878 XMapWindow(dpy, btn->icon->core->window);
879 btn->x_pos = x_pos;
880 btn->y_pos = 0;
881 btn->docked = 1;
882 if (type == WM_CLIP)
883 scr->clip_icon = btn;
885 return btn;
889 static void
890 switchWSCommand(WMenu *menu, WMenuEntry *entry)
892 WAppIcon *btn, *icon = (WAppIcon*) entry->clientdata;
893 WScreen *scr = icon->icon->core->screen_ptr;
894 WDock *src, *dest;
895 WMArray *selectedIcons;
896 int x, y;
898 if (entry->order == scr->current_workspace)
899 return;
900 src = icon->dock;
901 dest = scr->workspaces[entry->order]->clip;
903 selectedIcons = getSelected(src);
905 if (WMGetArrayItemCount(selectedIcons)) {
906 WMArrayIterator iter;
908 WM_ITERATE_ARRAY(selectedIcons, btn, iter) {
909 if (wDockFindFreeSlot(dest, &x, &y)) {
910 moveIconBetweenDocks(src, dest, btn, x, y);
911 XUnmapWindow(dpy, btn->icon->core->window);
914 } else if (icon != scr->clip_icon) {
915 if (wDockFindFreeSlot(dest, &x, &y)) {
916 moveIconBetweenDocks(src, dest, icon, x, y);
917 XUnmapWindow(dpy, icon->icon->core->window);
920 WMFreeArray(selectedIcons);
925 static void
926 launchDockedApplication(WAppIcon *btn, Bool withSelection)
928 WScreen *scr = btn->icon->core->screen_ptr;
930 if (!btn->launching &&
931 ((!withSelection && btn->command!=NULL) ||
932 (withSelection && btn->paste_command!=NULL))) {
933 if (!btn->forced_dock) {
934 btn->relaunching = btn->running;
935 btn->running = 1;
937 if (btn->wm_instance || btn->wm_class) {
938 WWindowAttributes attr;
939 memset(&attr, 0, sizeof(WWindowAttributes));
940 wDefaultFillAttributes(scr, btn->wm_instance, btn->wm_class,
941 &attr, NULL, True);
943 if (!attr.no_appicon && !btn->buggy_app)
944 btn->launching = 1;
945 else
946 btn->running = 0;
948 btn->drop_launch = 0;
949 btn->paste_launch = withSelection;
950 scr->last_dock = btn->dock;
951 btn->pid = execCommand(btn,
952 withSelection ? btn->paste_command : btn->command,
953 NULL);
954 if (btn->pid>0) {
955 if (btn->buggy_app) {
956 /* give feedback that the app was launched */
957 btn->launching = 1;
958 dockIconPaint(btn);
959 btn->launching = 0;
960 WMAddTimerHandler(200, (WMCallback*)dockIconPaint, btn);
961 } else {
962 dockIconPaint(btn);
964 } else {
965 wwarning(_("could not launch application %s\n"), btn->command);
966 btn->launching = 0;
967 if (!btn->relaunching)
968 btn->running = 0;
975 static void
976 updateWorkspaceMenu(WMenu *menu, WAppIcon *icon)
978 WScreen *scr = menu->frame->screen_ptr;
979 char title[MAX_WORKSPACENAME_WIDTH+1];
980 int i;
982 if (!menu || !icon)
983 return;
985 for (i=0; i<scr->workspace_count; i++) {
986 if (i < menu->entry_no) {
987 if (strcmp(menu->entries[i]->text,scr->workspaces[i]->name)!=0) {
988 wfree(menu->entries[i]->text);
989 strcpy(title, scr->workspaces[i]->name);
990 menu->entries[i]->text = wstrdup(title);
991 menu->flags.realized = 0;
993 menu->entries[i]->clientdata = (void*)icon;
994 } else {
995 strcpy(title, scr->workspaces[i]->name);
997 wMenuAddCallback(menu, title, switchWSCommand, (void*)icon);
999 menu->flags.realized = 0;
1001 if (i == scr->current_workspace) {
1002 wMenuSetEnabled(menu, i, False);
1003 } else {
1004 wMenuSetEnabled(menu, i, True);
1008 if (!menu->flags.realized)
1009 wMenuRealize(menu);
1013 static WMenu*
1014 makeWorkspaceMenu(WScreen *scr)
1016 WMenu *menu;
1018 menu = wMenuCreate(scr, NULL, False);
1019 if (!menu)
1020 wwarning(_("could not create workspace submenu for Clip menu"));
1022 wMenuAddCallback(menu, "", switchWSCommand, (void*)scr->clip_icon);
1024 menu->flags.realized = 0;
1025 wMenuRealize(menu);
1027 return menu;
1031 static void
1032 updateClipOptionsMenu(WMenu *menu, WDock *dock)
1034 WMenuEntry *entry;
1035 int index = 0;
1037 if (!menu || !dock)
1038 return;
1040 /* keep on top */
1041 entry = menu->entries[index];
1042 entry->flags.indicator_on = !dock->lowered;
1043 entry->clientdata = dock;
1045 /* collapsed */
1046 entry = menu->entries[++index];
1047 entry->flags.indicator_on = dock->collapsed;
1048 entry->clientdata = dock;
1050 /* auto-collapse */
1051 entry = menu->entries[++index];
1052 entry->flags.indicator_on = dock->auto_collapse;
1053 entry->clientdata = dock;
1055 /* auto-raise/lower */
1056 entry = menu->entries[++index];
1057 entry->flags.indicator_on = dock->auto_raise_lower;
1058 entry->clientdata = dock;
1059 wMenuSetEnabled(menu, index, dock->lowered);
1061 /* attract icons */
1062 entry = menu->entries[++index];
1063 entry->flags.indicator_on = dock->attract_icons;
1064 entry->clientdata = dock;
1066 menu->flags.realized = 0;
1067 wMenuRealize(menu);
1071 static WMenu*
1072 makeClipOptionsMenu(WScreen *scr)
1074 WMenu *menu;
1075 WMenuEntry *entry;
1077 menu = wMenuCreate(scr, NULL, False);
1078 if (!menu) {
1079 wwarning(_("could not create options submenu for Clip menu"));
1080 return NULL;
1083 entry = wMenuAddCallback(menu, _("Keep on Top"),
1084 toggleLoweredCallback, NULL);
1085 entry->flags.indicator = 1;
1086 entry->flags.indicator_on = 1;
1087 entry->flags.indicator_type = MI_CHECK;
1089 entry = wMenuAddCallback(menu, _("Collapsed"),
1090 toggleCollapsedCallback, NULL);
1091 entry->flags.indicator = 1;
1092 entry->flags.indicator_on = 1;
1093 entry->flags.indicator_type = MI_CHECK;
1095 entry = wMenuAddCallback(menu, _("Autocollapse"),
1096 toggleAutoCollapseCallback, NULL);
1097 entry->flags.indicator = 1;
1098 entry->flags.indicator_on = 1;
1099 entry->flags.indicator_type = MI_CHECK;
1101 entry = wMenuAddCallback(menu, _("Autoraise"),
1102 toggleAutoRaiseLowerCallback, NULL);
1103 entry->flags.indicator = 1;
1104 entry->flags.indicator_on = 1;
1105 entry->flags.indicator_type = MI_CHECK;
1107 entry = wMenuAddCallback(menu, _("Autoattract Icons"),
1108 toggleAutoAttractCallback, NULL);
1109 entry->flags.indicator = 1;
1110 entry->flags.indicator_on = 1;
1111 entry->flags.indicator_type = MI_CHECK;
1113 menu->flags.realized = 0;
1114 wMenuRealize(menu);
1116 return menu;
1120 static WMenu*
1121 dockMenuCreate(WScreen *scr, int type)
1123 WMenu *menu;
1124 WMenuEntry *entry;
1126 if (type == WM_CLIP && scr->clip_menu)
1127 return scr->clip_menu;
1129 menu = wMenuCreate(scr, NULL, False);
1130 if (type != WM_CLIP) {
1131 entry = wMenuAddCallback(menu, _("Keep on Top"),
1132 toggleLoweredCallback, NULL);
1133 entry->flags.indicator = 1;
1134 entry->flags.indicator_on = 1;
1135 entry->flags.indicator_type = MI_CHECK;
1136 } else {
1137 entry = wMenuAddCallback(menu, _("Clip Options"), NULL, NULL);
1138 scr->clip_options = makeClipOptionsMenu(scr);
1139 if (scr->clip_options)
1140 wMenuEntrySetCascade(menu, entry, scr->clip_options);
1142 entry = wMenuAddCallback(menu, _("Rename Workspace"), renameCallback,
1143 NULL);
1144 wfree(entry->text);
1145 entry->text = _("Rename Workspace");
1147 entry = wMenuAddCallback(menu, _("Selected"), selectCallback, NULL);
1148 entry->flags.indicator = 1;
1149 entry->flags.indicator_on = 1;
1150 entry->flags.indicator_type = MI_CHECK;
1152 entry = wMenuAddCallback(menu, _("Select All Icons"),
1153 selectIconsCallback, NULL);
1154 wfree(entry->text);
1155 entry->text = _("Select All Icons");
1157 entry = wMenuAddCallback(menu, _("Keep Icon"), keepIconsCallback, NULL);
1158 wfree(entry->text);
1159 entry->text = _("Keep Icon");
1161 entry = wMenuAddCallback(menu, _("Move Icon To"), NULL, NULL);
1162 wfree(entry->text);
1163 entry->text = _("Move Icon To");
1164 scr->clip_submenu = makeWorkspaceMenu(scr);
1165 if (scr->clip_submenu)
1166 wMenuEntrySetCascade(menu, entry, scr->clip_submenu);
1168 entry = wMenuAddCallback(menu, _("Remove Icon"), removeIconsCallback,
1169 NULL);
1170 wfree(entry->text);
1171 entry->text = _("Remove Icon");
1173 wMenuAddCallback(menu, _("Attract Icons"), colectIconsCallback, NULL);
1176 wMenuAddCallback(menu, _("Launch"), launchCallback, NULL);
1178 wMenuAddCallback(menu, _("Unhide Here"), unhideHereCallback, NULL);
1180 entry = wMenuAddCallback(menu, _("Hide"), hideCallback, NULL);
1181 wfree(entry->text);
1182 entry->text = _("Hide");
1184 wMenuAddCallback(menu, _("Settings..."), settingsCallback, NULL);
1186 wMenuAddCallback(menu, _("Kill"), killCallback, NULL);
1188 if (type == WM_CLIP)
1189 scr->clip_menu = menu;
1191 return menu;
1195 WDock*
1196 wDockCreate(WScreen *scr, int type)
1198 WDock *dock;
1199 WAppIcon *btn;
1200 int icon_count;
1202 make_keys();
1204 dock = wmalloc(sizeof(WDock));
1205 memset(dock, 0, sizeof(WDock));
1207 if (type == WM_CLIP)
1208 icon_count = CLIP_MAX_ICONS;
1209 else
1210 icon_count = scr->scr_height/wPreferences.icon_size;
1212 dock->icon_array = wmalloc(sizeof(WAppIcon*)*icon_count);
1213 memset(dock->icon_array, 0, sizeof(WAppIcon*)*icon_count);
1215 dock->max_icons = icon_count;
1217 btn = mainIconCreate(scr, type);
1219 btn->dock = dock;
1221 dock->x_pos = btn->x_pos;
1222 dock->y_pos = btn->y_pos;
1223 dock->screen_ptr = scr;
1224 dock->type = type;
1225 dock->icon_count = 1;
1226 dock->on_right_side = 1;
1227 dock->collapsed = 0;
1228 dock->auto_collapse = 0;
1229 dock->auto_collapse_magic = NULL;
1230 dock->auto_raise_lower = 0;
1231 dock->auto_lower_magic = NULL;
1232 dock->auto_raise_magic = NULL;
1233 dock->attract_icons = 0;
1234 dock->lowered = 1;
1235 dock->icon_array[0] = btn;
1236 wRaiseFrame(btn->icon->core);
1237 XMoveWindow(dpy, btn->icon->core->window, btn->x_pos, btn->y_pos);
1239 /* create dock menu */
1240 dock->menu = dockMenuCreate(scr, type);
1242 return dock;
1246 void
1247 wDockDestroy(WDock *dock)
1249 int i;
1250 WAppIcon *aicon;
1252 for (i=(dock->type == WM_CLIP) ? 1 : 0; i<dock->max_icons; i++) {
1253 aicon = dock->icon_array[i];
1254 if (aicon) {
1255 int keepit = aicon->running && wApplicationOf(aicon->main_window);
1256 wDockDetach(dock, aicon);
1257 if (keepit) {
1258 PlaceIcon(dock->screen_ptr, &aicon->x_pos, &aicon->y_pos);
1259 XMoveWindow(dpy, aicon->icon->core->window,
1260 aicon->x_pos, aicon->y_pos);
1261 if (!dock->mapped || dock->collapsed)
1262 XMapWindow(dpy, aicon->icon->core->window);
1266 if (wPreferences.auto_arrange_icons)
1267 wArrangeIcons(dock->screen_ptr, True);
1268 wfree(dock->icon_array);
1269 if (dock->menu && dock->type!=WM_CLIP)
1270 wMenuDestroy(dock->menu, True);
1271 if (dock->screen_ptr->last_dock == dock)
1272 dock->screen_ptr->last_dock = NULL;
1273 wfree(dock);
1277 void
1278 wClipIconPaint(WAppIcon *aicon)
1280 WScreen *scr = aicon->icon->core->screen_ptr;
1281 WWorkspace *workspace = scr->workspaces[scr->current_workspace];
1282 GC gc;
1283 Window win = aicon->icon->core->window;
1284 int length, nlength;
1285 char *ws_name, ws_number[10];
1286 int ty, tx;
1288 wIconPaint(aicon->icon);
1290 length = strlen(workspace->name);
1291 ws_name = wmalloc(length + 1);
1292 snprintf(ws_name, length+1, "%s", workspace->name);
1293 snprintf(ws_number, sizeof(ws_number), "%i", scr->current_workspace + 1);
1294 nlength = strlen(ws_number);
1296 gc = scr->clip_title_gc;
1298 if (!workspace->clip->collapsed)
1299 XSetForeground(dpy, gc, scr->clip_title_pixel[CLIP_NORMAL]);
1300 else
1301 XSetForeground(dpy, gc, scr->clip_title_pixel[CLIP_COLLAPSED]);
1303 ty = ICON_SIZE - WMFontHeight(scr->clip_title_font) - 3;
1305 tx = CLIP_BUTTON_SIZE*ICON_SIZE/64;
1307 WMDrawString(scr->wmscreen, win, gc, scr->clip_title_font, tx,
1308 ty, ws_name, length);
1309 /*WMDrawString(scr->wmscreen, win, gc, scr->clip_title_font, 4,
1310 2, ws_name, length);*/
1312 tx = (ICON_SIZE/2 - WMWidthOfString(scr->clip_title_font, ws_number,
1313 nlength))/2;
1315 WMDrawString(scr->wmscreen, win, gc, scr->clip_title_font, tx,
1316 2, ws_number, nlength);
1318 wfree(ws_name);
1320 if (aicon->launching) {
1321 XFillRectangle(dpy, aicon->icon->core->window, scr->stipple_gc,
1322 0, 0, wPreferences.icon_size, wPreferences.icon_size);
1324 paintClipButtons(aicon, aicon->dock->lclip_button_pushed,
1325 aicon->dock->rclip_button_pushed);
1329 static void
1330 clipIconExpose(WObjDescriptor *desc, XEvent *event)
1332 wClipIconPaint(desc->parent);
1336 static void
1337 dockIconPaint(WAppIcon *btn)
1339 if (btn == btn->icon->core->screen_ptr->clip_icon)
1340 wClipIconPaint(btn);
1341 else
1342 wAppIconPaint(btn);
1346 static WMPropList*
1347 make_icon_state(WAppIcon *btn)
1349 WMPropList *node = NULL;
1350 WMPropList *command, *autolaunch, *lock, *name, *forced, *host;
1351 WMPropList *position, *buggy, *omnipresent;
1352 char *tmp;
1353 char buffer[64];
1355 if (btn) {
1356 if (!btn->command)
1357 command = WMCreatePLString("-");
1358 else
1359 command = WMCreatePLString(btn->command);
1361 autolaunch = btn->auto_launch ? dYes : dNo;
1363 lock = btn->lock ? dYes : dNo;
1365 tmp = EscapeWM_CLASS(btn->wm_instance, btn->wm_class);
1367 name = WMCreatePLString(tmp);
1369 wfree(tmp);
1371 forced = btn->forced_dock ? dYes : dNo;
1373 buggy = btn->buggy_app ? dYes : dNo;
1375 if (btn == btn->icon->core->screen_ptr->clip_icon)
1376 snprintf(buffer, sizeof(buffer), "%i,%i", btn->x_pos, btn->y_pos);
1377 else
1378 snprintf(buffer, sizeof(buffer), "%hi,%hi", btn->xindex, btn->yindex);
1379 position = WMCreatePLString(buffer);
1381 node = WMCreatePLDictionary(dCommand, command,
1382 dName, name,
1383 dAutoLaunch, autolaunch,
1384 dLock, lock,
1385 dForced, forced,
1386 dBuggyApplication, buggy,
1387 dPosition, position,
1388 NULL);
1389 WMReleasePropList(command);
1390 WMReleasePropList(name);
1391 WMReleasePropList(position);
1393 omnipresent = btn->omnipresent ? dYes : dNo;
1394 if (btn->dock != btn->icon->core->screen_ptr->dock &&
1395 (btn->xindex != 0 || btn->yindex != 0))
1396 WMPutInPLDictionary(node, dOmnipresent, omnipresent);
1398 #ifdef OFFIX_DND
1399 if (btn->dnd_command) {
1400 command = WMCreatePLString(btn->dnd_command);
1401 WMPutInPLDictionary(node, dDropCommand, command);
1402 WMReleasePropList(command);
1404 #endif /* OFFIX_DND */
1406 if (btn->paste_command) {
1407 command = WMCreatePLString(btn->paste_command);
1408 WMPutInPLDictionary(node, dPasteCommand, command);
1409 WMReleasePropList(command);
1412 if (btn->client_machine && btn->remote_start) {
1413 host = WMCreatePLString(btn->client_machine);
1414 WMPutInPLDictionary(node, dHost, host);
1415 WMReleasePropList(host);
1419 return node;
1423 static WMPropList*
1424 dockSaveState(WDock *dock)
1426 int i;
1427 WMPropList *icon_info;
1428 WMPropList *list=NULL, *dock_state=NULL;
1429 WMPropList *value, *key;
1430 char buffer[256];
1432 list = WMCreatePLArray(NULL);
1434 for (i=(dock->type==WM_DOCK ? 0 : 1); i<dock->max_icons; i++) {
1435 WAppIcon *btn = dock->icon_array[i];
1437 if (!btn || btn->attracted)
1438 continue;
1440 if ((icon_info = make_icon_state(dock->icon_array[i]))) {
1441 WMAddToPLArray(list, icon_info);
1442 WMReleasePropList(icon_info);
1446 dock_state = WMCreatePLDictionary(dApplications, list,
1447 NULL);
1449 if (dock->type == WM_DOCK) {
1450 snprintf(buffer, sizeof(buffer), "Applications%i", dock->screen_ptr->scr_height);
1451 key = WMCreatePLString(buffer);
1452 WMPutInPLDictionary(dock_state, key, list);
1453 WMReleasePropList(key);
1456 snprintf(buffer, sizeof(buffer), "%i,%i", (dock->on_right_side ? -ICON_SIZE : 0),
1457 dock->y_pos);
1458 value = WMCreatePLString(buffer);
1459 WMPutInPLDictionary(dock_state, dPosition, value);
1460 WMReleasePropList(value);
1462 WMReleasePropList(list);
1465 value = (dock->lowered ? dYes : dNo);
1466 WMPutInPLDictionary(dock_state, dLowered, value);
1468 if (dock->type == WM_CLIP) {
1469 value = (dock->collapsed ? dYes : dNo);
1470 WMPutInPLDictionary(dock_state, dCollapsed, value);
1472 value = (dock->auto_collapse ? dYes : dNo);
1473 WMPutInPLDictionary(dock_state, dAutoCollapse, value);
1475 value = (dock->auto_raise_lower ? dYes : dNo);
1476 WMPutInPLDictionary(dock_state, dAutoRaiseLower, value);
1478 value = (dock->attract_icons ? dYes : dNo);
1479 WMPutInPLDictionary(dock_state, dAutoAttractIcons, value);
1482 return dock_state;
1486 void
1487 wDockSaveState(WScreen *scr, WMPropList *old_state)
1489 WMPropList *dock_state;
1490 WMPropList *keys;
1492 dock_state = dockSaveState(scr->dock);
1495 * Copy saved states of docks with different sizes.
1497 if (old_state) {
1498 int i;
1499 WMPropList *tmp;
1501 keys = WMGetPLDictionaryKeys(old_state);
1502 for (i = 0; i < WMGetPropListItemCount(keys); i++) {
1503 tmp = WMGetFromPLArray(keys, i);
1505 if (strncasecmp(WMGetFromPLString(tmp), "applications", 12) == 0
1506 && !WMGetFromPLDictionary(dock_state, tmp)) {
1508 WMPutInPLDictionary(dock_state,
1509 tmp,
1510 WMGetFromPLDictionary(old_state, tmp));
1513 WMReleasePropList(keys);
1517 WMPutInPLDictionary(scr->session_state, dDock, dock_state);
1519 WMReleasePropList(dock_state);
1523 void
1524 wClipSaveState(WScreen *scr)
1526 WMPropList *clip_state;
1528 clip_state = make_icon_state(scr->clip_icon);
1530 WMPutInPLDictionary(scr->session_state, dClip, clip_state);
1532 WMReleasePropList(clip_state);
1536 WMPropList*
1537 wClipSaveWorkspaceState(WScreen *scr, int workspace)
1539 return dockSaveState(scr->workspaces[workspace]->clip);
1543 static Bool
1544 getBooleanDockValue(WMPropList *value, WMPropList *key)
1546 if (value) {
1547 if (WMIsPLString(value)) {
1548 if (strcasecmp(WMGetFromPLString(value), "YES")==0)
1549 return True;
1550 } else {
1551 wwarning(_("bad value in docked icon state info %s"),
1552 WMGetFromPLString(key));
1555 return False;
1559 static WAppIcon*
1560 restore_icon_state(WScreen *scr, WMPropList *info, int type, int index)
1562 WAppIcon *aicon;
1563 WMPropList *cmd, *value;
1566 cmd = WMGetFromPLDictionary(info, dCommand);
1567 if (!cmd || !WMIsPLString(cmd)) {
1568 return NULL;
1571 /* parse window name */
1572 value = WMGetFromPLDictionary(info, dName);
1573 if (!value)
1574 return NULL;
1577 char *wclass, *winstance;
1578 char *command;
1580 ParseWindowName(value, &winstance, &wclass, "dock");
1582 if (!winstance && !wclass) {
1583 return NULL;
1586 /* get commands */
1588 if (cmd)
1589 command = wstrdup(WMGetFromPLString(cmd));
1590 else
1591 command = NULL;
1593 if (!command || strcmp(command, "-")==0) {
1594 if (command)
1595 wfree(command);
1596 if (wclass)
1597 wfree(wclass);
1598 if (winstance)
1599 wfree(winstance);
1601 return NULL;
1604 aicon = wAppIconCreateForDock(scr, command, winstance, wclass,
1605 TILE_NORMAL);
1606 if (wclass)
1607 wfree(wclass);
1608 if (winstance)
1609 wfree(winstance);
1610 if (command)
1611 wfree(command);
1614 aicon->icon->core->descriptor.handle_mousedown = iconMouseDown;
1615 if (type == WM_CLIP) {
1616 aicon->icon->core->descriptor.handle_enternotify = clipEnterNotify;
1617 aicon->icon->core->descriptor.handle_leavenotify = clipLeaveNotify;
1619 aicon->icon->core->descriptor.parent_type = WCLASS_DOCK_ICON;
1620 aicon->icon->core->descriptor.parent = aicon;
1623 #ifdef OFFIX_DND
1624 cmd = WMGetFromPLDictionary(info, dDropCommand);
1625 if (cmd)
1626 aicon->dnd_command = wstrdup(WMGetFromPLString(cmd));
1627 #endif
1629 cmd = WMGetFromPLDictionary(info, dPasteCommand);
1630 if (cmd)
1631 aicon->paste_command = wstrdup(WMGetFromPLString(cmd));
1633 /* check auto launch */
1634 value = WMGetFromPLDictionary(info, dAutoLaunch);
1636 aicon->auto_launch = getBooleanDockValue(value, dAutoLaunch);
1638 /* check lock */
1639 value = WMGetFromPLDictionary(info, dLock);
1641 aicon->lock = getBooleanDockValue(value, dLock);
1643 /* check if it wasn't normally docked */
1644 value = WMGetFromPLDictionary(info, dForced);
1646 aicon->forced_dock = getBooleanDockValue(value, dForced);
1648 /* check if we can rely on the stuff in the app */
1649 value = WMGetFromPLDictionary(info, dBuggyApplication);
1651 aicon->buggy_app = getBooleanDockValue(value, dBuggyApplication);
1653 /* get position in the dock */
1654 value = WMGetFromPLDictionary(info, dPosition);
1655 if (value && WMIsPLString(value)) {
1656 if (sscanf(WMGetFromPLString(value), "%hi,%hi", &aicon->xindex,
1657 &aicon->yindex)!=2)
1658 wwarning(_("bad value in docked icon state info %s"),
1659 WMGetFromPLString(dPosition));
1661 /* check position sanity */
1662 /* incomplete section! */
1663 if (type == WM_DOCK) {
1664 aicon->xindex = 0;
1665 if (aicon->yindex < 0)
1666 wwarning(_("bad value in docked icon position %i,%i"),
1667 aicon->xindex, aicon->yindex);
1669 } else {
1670 aicon->yindex = index;
1671 aicon->xindex = 0;
1674 /* check if icon is omnipresent */
1675 value = WMGetFromPLDictionary(info, dOmnipresent);
1677 aicon->omnipresent = getBooleanDockValue(value, dOmnipresent);
1679 aicon->running = 0;
1680 aicon->docked = 1;
1682 return aicon;
1686 #define COMPLAIN(key) wwarning(_("bad value in dock state info:%s"), key)
1689 WAppIcon*
1690 wClipRestoreState(WScreen *scr, WMPropList *clip_state)
1692 WAppIcon *icon;
1693 WMPropList *value;
1696 icon = mainIconCreate(scr, WM_CLIP);
1698 if (!clip_state)
1699 return icon;
1700 else
1701 WMRetainPropList(clip_state);
1703 /* restore position */
1705 value = WMGetFromPLDictionary(clip_state, dPosition);
1707 if (value) {
1708 if (!WMIsPLString(value))
1709 COMPLAIN("Position");
1710 else {
1711 if (sscanf(WMGetFromPLString(value), "%i,%i", &icon->x_pos,
1712 &icon->y_pos)!=2)
1713 COMPLAIN("Position");
1715 /* check position sanity */
1716 if (icon->y_pos < 0)
1717 icon->y_pos = 0;
1718 else if (icon->y_pos > scr->scr_height-ICON_SIZE)
1719 icon->y_pos = scr->scr_height-ICON_SIZE;
1721 if (icon->x_pos < 0)
1722 icon->x_pos = 0;
1723 else if (icon->x_pos > scr->scr_width-ICON_SIZE)
1724 icon->x_pos = scr->scr_width-ICON_SIZE;
1728 #ifdef OFFIX_DND
1729 value = WMGetFromPLDictionary(clip_state, dDropCommand);
1730 if (value && WMIsPLString(value))
1731 icon->dnd_command = wstrdup(WMGetFromPLString(value));
1732 #endif
1734 value = WMGetFromPLDictionary(clip_state, dPasteCommand);
1735 if (value && WMIsPLString(value))
1736 icon->paste_command = wstrdup(WMGetFromPLString(value));
1738 WMReleasePropList(clip_state);
1740 return icon;
1744 WDock*
1745 wDockRestoreState(WScreen *scr, WMPropList *dock_state, int type)
1747 WDock *dock;
1748 WMPropList *apps;
1749 WMPropList *value;
1750 WAppIcon *aicon, *old_top;
1751 int count, i;
1754 dock = wDockCreate(scr, type);
1756 if (!dock_state)
1757 return dock;
1759 if (dock_state)
1760 WMRetainPropList(dock_state);
1763 /* restore position */
1765 value = WMGetFromPLDictionary(dock_state, dPosition);
1767 if (value) {
1768 if (!WMIsPLString(value))
1769 COMPLAIN("Position");
1770 else {
1771 if (sscanf(WMGetFromPLString(value), "%i,%i", &dock->x_pos,
1772 &dock->y_pos)!=2)
1773 COMPLAIN("Position");
1775 /* check position sanity */
1776 if (dock->y_pos < 0)
1777 dock->y_pos = 0;
1778 else if (dock->y_pos > scr->scr_height-ICON_SIZE)
1779 dock->y_pos = scr->scr_height - ICON_SIZE;
1781 /* This is no more needed. ??? */
1782 if (type == WM_CLIP) {
1783 if (dock->x_pos < 0)
1784 dock->x_pos = 0;
1785 else if (dock->x_pos > scr->scr_width-ICON_SIZE)
1786 dock->x_pos = scr->scr_width-ICON_SIZE;
1787 } else {
1788 if (dock->x_pos >= 0) {
1789 dock->x_pos = DOCK_EXTRA_SPACE;
1790 dock->on_right_side = 0;
1791 } else {
1792 dock->x_pos = scr->scr_width - DOCK_EXTRA_SPACE - ICON_SIZE;
1793 dock->on_right_side = 1;
1799 /* restore lowered/raised state */
1801 dock->lowered = 0;
1803 value = WMGetFromPLDictionary(dock_state, dLowered);
1805 if (value) {
1806 if (!WMIsPLString(value))
1807 COMPLAIN("Lowered");
1808 else {
1809 if (strcasecmp(WMGetFromPLString(value), "YES")==0)
1810 dock->lowered = 1;
1815 /* restore collapsed state */
1817 dock->collapsed = 0;
1819 value = WMGetFromPLDictionary(dock_state, dCollapsed);
1821 if (value) {
1822 if (!WMIsPLString(value))
1823 COMPLAIN("Collapsed");
1824 else {
1825 if (strcasecmp(WMGetFromPLString(value), "YES")==0)
1826 dock->collapsed = 1;
1831 /* restore auto-collapsed state */
1833 value = WMGetFromPLDictionary(dock_state, dAutoCollapse);
1835 if (value) {
1836 if (!WMIsPLString(value))
1837 COMPLAIN("AutoCollapse");
1838 else {
1839 if (strcasecmp(WMGetFromPLString(value), "YES")==0) {
1840 dock->auto_collapse = 1;
1841 dock->collapsed = 1;
1847 /* restore auto-raise/lower state */
1849 value = WMGetFromPLDictionary(dock_state, dAutoRaiseLower);
1851 if (value) {
1852 if (!WMIsPLString(value))
1853 COMPLAIN("AutoRaiseLower");
1854 else {
1855 if (strcasecmp(WMGetFromPLString(value), "YES")==0) {
1856 dock->auto_raise_lower = 1;
1861 /* restore attract icons state */
1863 dock->attract_icons = 0;
1865 value = WMGetFromPLDictionary(dock_state, dAutoAttractIcons);
1867 if (value) {
1868 if (!WMIsPLString(value))
1869 COMPLAIN("AutoAttractIcons");
1870 else {
1871 if (strcasecmp(WMGetFromPLString(value), "YES")==0)
1872 dock->attract_icons = 1;
1877 /* application list */
1880 WMPropList *tmp;
1881 char buffer[64];
1884 * When saving, it saves the dock state in
1885 * Applications and Applicationsnnn
1887 * When loading, it will first try Applicationsnnn.
1888 * If it does not exist, use Applications as default.
1891 snprintf(buffer, sizeof(buffer), "Applications%i", scr->scr_height);
1893 tmp = WMCreatePLString(buffer);
1894 apps = WMGetFromPLDictionary(dock_state, tmp);
1895 WMReleasePropList(tmp);
1897 if (!apps) {
1898 apps = WMGetFromPLDictionary(dock_state, dApplications);
1902 if (!apps) {
1903 goto finish;
1906 count = WMGetPropListItemCount(apps);
1908 if (count==0)
1909 goto finish;
1911 old_top = dock->icon_array[0];
1913 /* dock->icon_count is set to 1 when dock is created.
1914 * Since Clip is already restored, we want to keep it so for clip,
1915 * but for dock we may change the default top tile, so we set it to 0.
1917 if (type == WM_DOCK)
1918 dock->icon_count = 0;
1920 for (i=0; i<count; i++) {
1921 if (dock->icon_count >= dock->max_icons) {
1922 wwarning(_("there are too many icons stored in dock. Ignoring what doesn't fit"));
1923 break;
1926 value = WMGetFromPLArray(apps, i);
1927 aicon = restore_icon_state(scr, value, type, dock->icon_count);
1929 dock->icon_array[dock->icon_count] = aicon;
1931 if (aicon) {
1932 aicon->dock = dock;
1933 aicon->x_pos = dock->x_pos + (aicon->xindex*ICON_SIZE);
1934 aicon->y_pos = dock->y_pos + (aicon->yindex*ICON_SIZE);
1936 if (dock->lowered)
1937 ChangeStackingLevel(aicon->icon->core, WMNormalLevel);
1938 else
1939 ChangeStackingLevel(aicon->icon->core, WMDockLevel);
1941 wCoreConfigure(aicon->icon->core, aicon->x_pos, aicon->y_pos,
1942 0, 0);
1944 if (!dock->collapsed)
1945 XMapWindow(dpy, aicon->icon->core->window);
1946 wRaiseFrame(aicon->icon->core);
1948 dock->icon_count++;
1949 } else if (dock->icon_count==0 && type==WM_DOCK)
1950 dock->icon_count++;
1953 /* if the first icon is not defined, use the default */
1954 if (dock->icon_array[0]==NULL) {
1955 /* update default icon */
1956 old_top->x_pos = dock->x_pos;
1957 old_top->y_pos = dock->y_pos;
1958 if (dock->lowered)
1959 ChangeStackingLevel(old_top->icon->core, WMNormalLevel);
1960 else
1961 ChangeStackingLevel(old_top->icon->core, WMDockLevel);
1962 dock->icon_array[0] = old_top;
1963 XMoveWindow(dpy, old_top->icon->core->window, dock->x_pos, dock->y_pos);
1964 /* we don't need to increment dock->icon_count here because it was
1965 * incremented in the loop above.
1967 } else if (old_top!=dock->icon_array[0]) {
1968 if (old_top == scr->clip_icon)
1969 scr->clip_icon = dock->icon_array[0];
1970 wAppIconDestroy(old_top);
1973 finish:
1974 if (dock_state)
1975 WMReleasePropList(dock_state);
1977 return dock;
1982 void
1983 wDockLaunchWithState(WDock *dock, WAppIcon *btn, WSavedState *state)
1985 if (btn && btn->command && !btn->running && !btn->launching) {
1987 btn->drop_launch = 0;
1988 btn->paste_launch = 0;
1990 btn->pid = execCommand(btn, btn->command, state);
1992 if (btn->pid>0) {
1993 if (!btn->forced_dock && !btn->buggy_app) {
1994 btn->launching = 1;
1995 dockIconPaint(btn);
1998 } else {
1999 wfree(state);
2004 void
2005 wDockDoAutoLaunch(WDock *dock, int workspace)
2007 WAppIcon *btn;
2008 WSavedState *state;
2009 int i;
2011 for (i = 0; i < dock->max_icons; i++) {
2012 btn = dock->icon_array[i];
2013 if (!btn || !btn->auto_launch)
2014 continue;
2016 state = wmalloc(sizeof(WSavedState));
2017 memset(state, 0, sizeof(WSavedState));
2018 state->workspace = workspace;
2019 /* TODO: this is klugy and is very difficult to understand
2020 * what's going on. Try to clean up */
2021 wDockLaunchWithState(dock, btn, state);
2026 #ifdef OFFIX_DND
2027 static WDock*
2028 findDock(WScreen *scr, XEvent *event, int *icon_pos)
2030 WDock *dock;
2031 int i;
2033 *icon_pos = -1;
2034 if ((dock = scr->dock)!=NULL) {
2035 for (i=0; i<dock->max_icons; i++) {
2036 if (dock->icon_array[i]
2037 && dock->icon_array[i]->icon->core->window==event->xclient.window) {
2038 *icon_pos = i;
2039 break;
2043 if (*icon_pos<0 && (dock = scr->workspaces[scr->current_workspace]->clip)!=NULL) {
2044 for (i=0; i<dock->max_icons; i++) {
2045 if (dock->icon_array[i]
2046 && dock->icon_array[i]->icon->core->window==event->xclient.window) {
2047 *icon_pos = i;
2048 break;
2052 if(*icon_pos>=0)
2053 return dock;
2054 return NULL;
2059 wDockReceiveDNDDrop(WScreen *scr, XEvent *event)
2061 WDock *dock;
2062 WAppIcon *btn;
2063 int icon_pos;
2065 dock = findDock(scr, event, &icon_pos);
2066 if (!dock)
2067 return False;
2070 * Return True if the drop was on an application icon window.
2071 * In this case, let the ClientMessage handler redirect the
2072 * message to the app.
2074 if (dock->icon_array[icon_pos]->icon->icon_win!=None)
2075 return True;
2077 if (dock->icon_array[icon_pos]->dnd_command!=NULL) {
2078 scr->flags.dnd_data_convertion_status = 0;
2080 btn = dock->icon_array[icon_pos];
2082 if (!btn->forced_dock) {
2083 btn->relaunching = btn->running;
2084 btn->running = 1;
2086 if (btn->wm_instance || btn->wm_class) {
2087 WWindowAttributes attr;
2088 memset(&attr, 0, sizeof(WWindowAttributes));
2089 wDefaultFillAttributes(btn->icon->core->screen_ptr,
2090 btn->wm_instance,
2091 btn->wm_class, &attr, NULL, True);
2093 if (!attr.no_appicon)
2094 btn->launching = 1;
2095 else
2096 btn->running = 0;
2099 btn->paste_launch = 0;
2100 btn->drop_launch = 1;
2101 scr->last_dock = dock;
2102 btn->pid = execCommand(btn, btn->dnd_command, NULL);
2103 if (btn->pid>0) {
2104 dockIconPaint(btn);
2105 } else {
2106 btn->launching = 0;
2107 if (!btn->relaunching) {
2108 btn->running = 0;
2112 return False;
2114 #endif /* OFFIX_DND */
2118 Bool
2119 wDockAttachIcon(WDock *dock, WAppIcon *icon, int x, int y)
2121 WWindow *wwin;
2122 char **argv;
2123 int argc;
2124 int index;
2126 wwin = icon->icon->owner;
2127 if (icon->command==NULL) {
2128 icon->editing = 0;
2129 if (XGetCommand(dpy, wwin->client_win, &argv, &argc) && argc>0) {
2131 icon->command = wtokenjoin(argv, argc);
2132 XFreeStringList(argv);
2133 } else {
2134 char *command=NULL;
2136 /* icon->forced_dock = 1;*/
2137 if (dock->type!=WM_CLIP || !icon->attracted) {
2138 icon->editing = 1;
2139 if (wInputDialog(dock->screen_ptr, _("Dock Icon"),
2140 _("Type the command used to launch the application"),
2141 &command)) {
2142 if (command && (command[0]==0 ||
2143 (command[0]=='-' && command[1]==0))) {
2144 wfree(command);
2145 command = NULL;
2147 icon->command = command;
2148 icon->editing = 0;
2149 } else {
2150 icon->editing = 0;
2151 if (command)
2152 wfree(command);
2153 /* If the target is the dock, reject the icon. If
2154 * the target is the clip, make it an attracted icon
2156 if (dock->type==WM_CLIP) {
2157 icon->attracted = 1;
2158 if (!icon->icon->shadowed) {
2159 icon->icon->shadowed = 1;
2160 icon->icon->force_paint = 1;
2162 } else {
2163 return False;
2168 } else {
2169 icon->editing = 0;
2172 for (index=1; index<dock->max_icons; index++)
2173 if (dock->icon_array[index] == NULL)
2174 break;
2175 /* if (index == dock->max_icons)
2176 return; */
2178 assert(index < dock->max_icons);
2180 dock->icon_array[index] = icon;
2181 icon->yindex = y;
2182 icon->xindex = x;
2184 icon->omnipresent = 0;
2186 icon->x_pos = dock->x_pos + x*ICON_SIZE;
2187 icon->y_pos = dock->y_pos + y*ICON_SIZE;
2189 dock->icon_count++;
2191 icon->running = 1;
2192 icon->launching = 0;
2193 icon->docked = 1;
2194 icon->dock = dock;
2195 icon->icon->core->descriptor.handle_mousedown = iconMouseDown;
2196 if (dock->type == WM_CLIP) {
2197 icon->icon->core->descriptor.handle_enternotify = clipEnterNotify;
2198 icon->icon->core->descriptor.handle_leavenotify = clipLeaveNotify;
2200 icon->icon->core->descriptor.parent_type = WCLASS_DOCK_ICON;
2201 icon->icon->core->descriptor.parent = icon;
2203 MoveInStackListUnder(dock->icon_array[index-1]->icon->core,
2204 icon->icon->core);
2205 wAppIconMove(icon, icon->x_pos, icon->y_pos);
2206 wAppIconPaint(icon);
2208 if (wPreferences.auto_arrange_icons)
2209 wArrangeIcons(dock->screen_ptr, True);
2211 #ifdef OFFIX_DND
2212 if (icon->command && !icon->dnd_command) {
2213 int len = strlen(icon->command)+8;
2214 icon->dnd_command = wmalloc(len);
2215 snprintf(icon->dnd_command, len, "%s %%d", icon->command);
2217 #endif
2219 if (icon->command && !icon->paste_command) {
2220 int len = strlen(icon->command)+8;
2221 icon->paste_command = wmalloc(len);
2222 snprintf(icon->paste_command, len, "%s %%s", icon->command);
2225 return True;
2229 void
2230 reattachIcon(WDock *dock, WAppIcon *icon, int x, int y)
2232 int index;
2234 for(index=1; index<dock->max_icons; index++) {
2235 if(dock->icon_array[index] == icon)
2236 break;
2238 assert(index < dock->max_icons);
2240 icon->yindex = y;
2241 icon->xindex = x;
2243 icon->x_pos = dock->x_pos + x*ICON_SIZE;
2244 icon->y_pos = dock->y_pos + y*ICON_SIZE;
2248 Bool
2249 moveIconBetweenDocks(WDock *src, WDock *dest, WAppIcon *icon, int x, int y)
2251 WWindow *wwin;
2252 char **argv;
2253 int argc;
2254 int index;
2256 if (src == dest)
2257 return True; /* No move needed, we're already there */
2259 if (dest == NULL)
2260 return False;
2262 wwin = icon->icon->owner;
2265 * For the moment we can't do this if we move icons in Clip from one
2266 * workspace to other, because if we move two or more icons without
2267 * command, the dialog box will not be able to tell us to which of the
2268 * moved icons it applies. -Dan
2270 if ((dest->type==WM_DOCK /*|| dest->keep_attracted*/) && icon->command==NULL) {
2271 if (XGetCommand(dpy, wwin->client_win, &argv, &argc) && argc>0) {
2273 icon->command = wtokenjoin(argv, argc);
2274 XFreeStringList(argv);
2275 } else {
2276 char *command=NULL;
2278 icon->editing = 1;
2279 /* icon->forced_dock = 1;*/
2280 if (wInputDialog(src->screen_ptr, _("Dock Icon"),
2281 _("Type the command used to launch the application"),
2282 &command)) {
2283 if (command && (command[0]==0 ||
2284 (command[0]=='-' && command[1]==0))) {
2285 wfree(command);
2286 command = NULL;
2288 icon->command = command;
2289 } else {
2290 icon->editing = 0;
2291 if (command)
2292 wfree(command);
2293 return False;
2295 icon->editing = 0;
2299 if (dest->type == WM_DOCK)
2300 wClipMakeIconOmnipresent(icon, False);
2302 for(index=1; index<src->max_icons; index++) {
2303 if(src->icon_array[index] == icon)
2304 break;
2306 assert(index < src->max_icons);
2308 src->icon_array[index] = NULL;
2309 src->icon_count--;
2311 for(index=1; index<dest->max_icons; index++) {
2312 if(dest->icon_array[index] == NULL)
2313 break;
2315 /* if (index == dest->max_icons)
2316 return; */
2318 assert(index < dest->max_icons);
2320 dest->icon_array[index] = icon;
2321 icon->dock = dest;
2323 /* deselect the icon */
2324 if (icon->icon->selected)
2325 wIconSelect(icon->icon);
2327 if (dest->type == WM_DOCK) {
2328 icon->icon->core->descriptor.handle_enternotify = NULL;
2329 icon->icon->core->descriptor.handle_leavenotify = NULL;
2330 } else {
2331 icon->icon->core->descriptor.handle_enternotify = clipEnterNotify;
2332 icon->icon->core->descriptor.handle_leavenotify = clipLeaveNotify;
2335 /* set it to be kept when moving to dock.
2336 * Unless the icon does not have a command set
2338 if (icon->command && dest->type==WM_DOCK) {
2339 icon->attracted = 0;
2340 if (icon->icon->shadowed) {
2341 icon->icon->shadowed = 0;
2342 icon->icon->force_paint = 1;
2346 if (src->auto_collapse || src->auto_raise_lower)
2347 clipLeave(src);
2349 icon->yindex = y;
2350 icon->xindex = x;
2352 icon->x_pos = dest->x_pos + x*ICON_SIZE;
2353 icon->y_pos = dest->y_pos + y*ICON_SIZE;
2355 dest->icon_count++;
2357 MoveInStackListUnder(dest->icon_array[index-1]->icon->core,
2358 icon->icon->core);
2359 wAppIconPaint(icon);
2361 return True;
2365 void
2366 wDockDetach(WDock *dock, WAppIcon *icon)
2368 int index;
2370 /* make the settings panel be closed */
2371 if (icon->panel) {
2372 DestroyDockAppSettingsPanel(icon->panel);
2375 /* This must be called before icon->dock is set to NULL.
2376 * Don't move it. -Dan
2378 wClipMakeIconOmnipresent(icon, False);
2380 icon->docked = 0;
2381 icon->dock = NULL;
2382 icon->attracted = 0;
2383 icon->auto_launch = 0;
2384 if (icon->icon->shadowed) {
2385 icon->icon->shadowed = 0;
2386 icon->icon->force_paint = 1;
2389 /* deselect the icon */
2390 if (icon->icon->selected)
2391 wIconSelect(icon->icon);
2393 if (icon->command) {
2394 wfree(icon->command);
2395 icon->command = NULL;
2397 #ifdef OFFIX_DND
2398 if (icon->dnd_command) {
2399 wfree(icon->dnd_command);
2400 icon->dnd_command = NULL;
2402 #endif
2403 if (icon->paste_command) {
2404 wfree(icon->paste_command);
2405 icon->paste_command = NULL;
2408 for (index=1; index<dock->max_icons; index++)
2409 if (dock->icon_array[index] == icon)
2410 break;
2411 assert(index < dock->max_icons);
2412 dock->icon_array[index] = NULL;
2413 icon->yindex = -1;
2414 icon->xindex = -1;
2416 dock->icon_count--;
2418 /* if the dock is not attached to an application or
2419 * the the application did not set the approriate hints yet,
2420 * destroy the icon */
2421 if (!icon->running || !wApplicationOf(icon->main_window))
2422 wAppIconDestroy(icon);
2423 else {
2424 icon->icon->core->descriptor.handle_mousedown = appIconMouseDown;
2425 icon->icon->core->descriptor.handle_enternotify = NULL;
2426 icon->icon->core->descriptor.handle_leavenotify = NULL;
2427 icon->icon->core->descriptor.parent_type = WCLASS_APPICON;
2428 icon->icon->core->descriptor.parent = icon;
2430 ChangeStackingLevel(icon->icon->core, NORMAL_ICON_LEVEL);
2432 wAppIconPaint(icon);
2433 if (wPreferences.auto_arrange_icons) {
2434 wArrangeIcons(dock->screen_ptr, True);
2437 if (dock->auto_collapse || dock->auto_raise_lower)
2438 clipLeave(dock);
2443 * returns the closest Dock slot index for the passed
2444 * coordinates.
2446 * Returns False if icon can't be docked.
2448 * Note: this function should NEVER alter ret_x or ret_y, unless it will
2449 * return True. -Dan
2451 Bool
2452 wDockSnapIcon(WDock *dock, WAppIcon *icon, int req_x, int req_y,
2453 int *ret_x, int *ret_y, int redocking)
2455 WScreen *scr = dock->screen_ptr;
2456 int dx, dy;
2457 int ex_x, ex_y;
2458 int i, offset = ICON_SIZE/2;
2459 WAppIcon *aicon = NULL;
2460 WAppIcon *nicon = NULL;
2461 int max_y_icons, max_x_icons;
2463 max_x_icons = scr->scr_width/ICON_SIZE;
2464 max_y_icons = scr->scr_height/ICON_SIZE-1;
2466 if (wPreferences.flags.noupdates)
2467 return False;
2469 dx = dock->x_pos;
2470 dy = dock->y_pos;
2472 /* if the dock is full */
2473 if (!redocking &&
2474 (dock->icon_count >= dock->max_icons)) {
2475 return False;
2478 /* exact position */
2479 if (req_y < dy)
2480 ex_y = (req_y - offset - dy)/ICON_SIZE;
2481 else
2482 ex_y = (req_y + offset - dy)/ICON_SIZE;
2484 if (req_x < dx)
2485 ex_x = (req_x - offset - dx)/ICON_SIZE;
2486 else
2487 ex_x = (req_x + offset - dx)/ICON_SIZE;
2489 /* check if the icon is outside the screen boundaries */
2490 if (dx + ex_x*ICON_SIZE < -ICON_SIZE+2 ||
2491 dx + ex_x*ICON_SIZE >= scr->scr_width-1 ||
2492 dy + ex_y*ICON_SIZE < -ICON_SIZE+2 ||
2493 dy + ex_y*ICON_SIZE >= scr->scr_height-1)
2494 return False;
2496 if (dock->type == WM_DOCK) {
2497 if (icon->dock != dock && ex_x != 0)
2498 return False;
2500 aicon = NULL;
2501 for (i=0; i<dock->max_icons; i++) {
2502 nicon = dock->icon_array[i];
2503 if (nicon && nicon->yindex == ex_y) {
2504 aicon = nicon;
2505 break;
2509 if (redocking) {
2510 int sig, done, closest;
2512 /* Possible cases when redocking:
2514 * icon dragged out of range of any slot -> false
2515 * icon dragged to range of free slot
2516 * icon dragged to range of same slot
2517 * icon dragged to range of different icon
2519 if (abs(ex_x) > DOCK_DETTACH_THRESHOLD)
2520 return False;
2522 if (ex_y>=0 && ex_y<=max_y_icons && (aicon==icon || !aicon)) {
2523 *ret_x = 0;
2524 *ret_y = ex_y;
2525 return True;
2528 /* start looking at the upper slot or lower? */
2529 if (ex_y*ICON_SIZE < (req_y + offset - dy))
2530 sig = 1;
2531 else
2532 sig = -1;
2534 closest = -1;
2535 done = 0;
2536 /* look for closest free slot */
2537 for (i=0; i<(DOCK_DETTACH_THRESHOLD+1)*2 && !done; i++) {
2538 int j;
2540 done = 1;
2541 closest = sig*(i/2) + ex_y;
2542 /* check if this slot is used */
2543 if (closest >= 0) {
2544 for (j = 0; j<dock->max_icons; j++) {
2545 if (dock->icon_array[j]
2546 && dock->icon_array[j]->yindex==closest) {
2547 /* slot is used by someone else */
2548 if (dock->icon_array[j]!=icon)
2549 done = 0;
2550 break;
2554 sig = -sig;
2556 if (done && closest >= 0 && closest <= max_y_icons &&
2557 ((ex_y >= closest && ex_y - closest < DOCK_DETTACH_THRESHOLD+1)
2559 (ex_y < closest && closest - ex_y <= DOCK_DETTACH_THRESHOLD+1))) {
2560 *ret_x = 0;
2561 *ret_y = closest;
2562 return True;
2564 } else { /* !redocking */
2566 /* if slot is free and the icon is close enough, return it */
2567 if (!aicon && ex_x == 0 && ex_y >= 0 && ex_y <= max_y_icons) {
2568 *ret_x = 0;
2569 *ret_y = ex_y;
2570 return True;
2573 } else { /* CLIP */
2574 int neighbours = 0;
2575 int start, stop, k;
2577 start = icon->omnipresent ? 0 : scr->current_workspace;
2578 stop = icon->omnipresent ? scr->workspace_count : start+1;
2580 aicon = NULL;
2581 for (k=start; k<stop; k++) {
2582 WDock *tmp = scr->workspaces[k]->clip;
2583 if (!tmp)
2584 continue;
2585 for (i=0; i<tmp->max_icons; i++) {
2586 nicon = tmp->icon_array[i];
2587 if (nicon && nicon->xindex == ex_x && nicon->yindex == ex_y) {
2588 aicon = nicon;
2589 break;
2592 if (aicon)
2593 break;
2595 for (k=start; k<stop; k++) {
2596 WDock *tmp = scr->workspaces[k]->clip;
2597 if (!tmp)
2598 continue;
2599 for (i=0; i<tmp->max_icons; i++) {
2600 nicon = tmp->icon_array[i];
2601 if (nicon && nicon != icon && /* Icon can't be it's own neighbour */
2602 (abs(nicon->xindex - ex_x) <= CLIP_ATTACH_VICINITY &&
2603 abs(nicon->yindex - ex_y) <= CLIP_ATTACH_VICINITY)) {
2604 neighbours = 1;
2605 break;
2608 if (neighbours)
2609 break;
2612 if (neighbours && (aicon==NULL || (redocking && aicon == icon))) {
2613 *ret_x = ex_x;
2614 *ret_y = ex_y;
2615 return True;
2618 return False;
2622 #define ON_SCREEN(x, y, sx, ex, sy, ey) \
2623 ((((x)+ICON_SIZE/2) >= (sx)) && (((y)+ICON_SIZE/2) >= (sy)) && \
2624 (((x) + (ICON_SIZE/2)) <= (ex)) && (((y) + (ICON_SIZE/2)) <= ey))
2628 * returns true if it can find a free slot in the dock,
2629 * in which case it changes x_pos and y_pos accordingly.
2630 * Else returns false.
2632 Bool
2633 wDockFindFreeSlot(WDock *dock, int *x_pos, int *y_pos)
2635 WScreen *scr = dock->screen_ptr;
2636 WAppIcon *btn;
2637 WAppIconChain *chain;
2638 unsigned char *slot_map;
2639 int mwidth;
2640 int r;
2641 int x, y;
2642 int i, done = False;
2643 int corner;
2644 int sx=0, sy=0, ex=scr->scr_width, ey=scr->scr_height;
2645 int extra_count=0;
2647 if (dock->type == WM_CLIP &&
2648 dock != scr->workspaces[scr->current_workspace]->clip)
2649 extra_count = scr->global_icon_count;
2651 /* if the dock is full */
2652 if (dock->icon_count+extra_count >= dock->max_icons) {
2653 return False;
2656 if (!wPreferences.flags.nodock && scr->dock) {
2657 if (scr->dock->on_right_side)
2658 ex -= ICON_SIZE + DOCK_EXTRA_SPACE;
2659 else
2660 sx += ICON_SIZE + DOCK_EXTRA_SPACE;
2663 if (ex < dock->x_pos)
2664 ex = dock->x_pos;
2665 if (sx > dock->x_pos+ICON_SIZE)
2666 sx = dock->x_pos+ICON_SIZE;
2667 #define C_NONE 0
2668 #define C_NW 1
2669 #define C_NE 2
2670 #define C_SW 3
2671 #define C_SE 4
2673 /* check if clip is in a corner */
2674 if (dock->type==WM_CLIP) {
2675 if (dock->x_pos < 1 && dock->y_pos < 1)
2676 corner = C_NE;
2677 else if (dock->x_pos < 1 && dock->y_pos >= (ey-ICON_SIZE))
2678 corner = C_SE;
2679 else if (dock->x_pos >= (ex-ICON_SIZE)&& dock->y_pos >= (ey-ICON_SIZE))
2680 corner = C_SW;
2681 else if (dock->x_pos >= (ex-ICON_SIZE) && dock->y_pos < 1)
2682 corner = C_NW;
2683 else
2684 corner = C_NONE;
2685 } else
2686 corner = C_NONE;
2688 /* If the clip is in the corner, use only slots that are in the border
2689 * of the screen */
2690 if (corner!=C_NONE) {
2691 char *hmap, *vmap;
2692 int hcount, vcount;
2694 hcount = WMIN(dock->max_icons, scr->scr_width/ICON_SIZE);
2695 vcount = WMIN(dock->max_icons, scr->scr_height/ICON_SIZE);
2696 hmap = wmalloc(hcount+1);
2697 memset(hmap, 0, hcount+1);
2698 vmap = wmalloc(vcount+1);
2699 memset(vmap, 0, vcount+1);
2701 /* mark used positions */
2702 switch (corner) {
2703 case C_NE:
2704 for (i=0; i<dock->max_icons; i++) {
2705 btn = dock->icon_array[i];
2706 if (!btn)
2707 continue;
2709 if (btn->xindex==0 && btn->yindex > 0 && btn->yindex < vcount)
2710 vmap[btn->yindex] = 1;
2711 else if (btn->yindex==0 && btn->xindex>0 && btn->xindex<hcount)
2712 hmap[btn->xindex] = 1;
2714 for (chain=scr->global_icons; chain!=NULL; chain=chain->next) {
2715 btn = chain->aicon;
2716 if (btn->xindex==0 && btn->yindex > 0 && btn->yindex < vcount)
2717 vmap[btn->yindex] = 1;
2718 else if (btn->yindex==0 && btn->xindex>0 && btn->xindex<hcount)
2719 hmap[btn->xindex] = 1;
2721 break;
2722 case C_NW:
2723 for (i=0; i<dock->max_icons; i++) {
2724 btn = dock->icon_array[i];
2725 if (!btn)
2726 continue;
2728 if (btn->xindex==0 && btn->yindex > 0 && btn->yindex < vcount)
2729 vmap[btn->yindex] = 1;
2730 else if (btn->yindex==0 && btn->xindex<0 &&btn->xindex>-hcount)
2731 hmap[-btn->xindex] = 1;
2733 for (chain=scr->global_icons; chain!=NULL; chain=chain->next) {
2734 btn = chain->aicon;
2735 if (btn->xindex==0 && btn->yindex > 0 && btn->yindex < vcount)
2736 vmap[btn->yindex] = 1;
2737 else if (btn->yindex==0 && btn->xindex<0 &&btn->xindex>-hcount)
2738 hmap[-btn->xindex] = 1;
2740 break;
2741 case C_SE:
2742 for (i=0; i<dock->max_icons; i++) {
2743 btn = dock->icon_array[i];
2744 if (!btn)
2745 continue;
2747 if (btn->xindex==0 && btn->yindex < 0 && btn->yindex > -vcount)
2748 vmap[-btn->yindex] = 1;
2749 else if (btn->yindex==0 && btn->xindex>0 && btn->xindex<hcount)
2750 hmap[btn->xindex] = 1;
2752 for (chain=scr->global_icons; chain!=NULL; chain=chain->next) {
2753 btn = chain->aicon;
2754 if (btn->xindex==0 && btn->yindex < 0 && btn->yindex > -vcount)
2755 vmap[-btn->yindex] = 1;
2756 else if (btn->yindex==0 && btn->xindex>0 && btn->xindex<hcount)
2757 hmap[btn->xindex] = 1;
2759 break;
2760 case C_SW:
2761 default:
2762 for (i=0; i<dock->max_icons; i++) {
2763 btn = dock->icon_array[i];
2764 if (!btn)
2765 continue;
2767 if (btn->xindex==0 && btn->yindex < 0 && btn->yindex > -vcount)
2768 vmap[-btn->yindex] = 1;
2769 else if (btn->yindex==0 && btn->xindex<0 &&btn->xindex>-hcount)
2770 hmap[-btn->xindex] = 1;
2772 for (chain=scr->global_icons; chain!=NULL; chain=chain->next) {
2773 btn = chain->aicon;
2774 if (btn->xindex==0 && btn->yindex < 0 && btn->yindex > -vcount)
2775 vmap[-btn->yindex] = 1;
2776 else if (btn->yindex==0 && btn->xindex<0 &&btn->xindex>-hcount)
2777 hmap[-btn->xindex] = 1;
2780 x=0; y=0;
2781 done = 0;
2782 /* search a vacant slot */
2783 for (i=1; i<WMAX(vcount, hcount); i++) {
2784 if (i < vcount && vmap[i]==0) {
2785 /* found a slot */
2786 x = 0;
2787 y = i;
2788 done = 1;
2789 break;
2790 } else if (i < hcount && hmap[i]==0) {
2791 /* found a slot */
2792 x = i;
2793 y = 0;
2794 done = 1;
2795 break;
2798 wfree(vmap);
2799 wfree(hmap);
2800 /* If found a slot, translate and return */
2801 if (done) {
2802 if (corner==C_NW || corner==C_NE) {
2803 *y_pos = y;
2804 } else {
2805 *y_pos = -y;
2807 if (corner==C_NE || corner==C_SE) {
2808 *x_pos = x;
2809 } else {
2810 *x_pos = -x;
2812 return True;
2814 /* else, try to find a slot somewhere else */
2817 /* a map of mwidth x mwidth would be enough if we allowed icons to be
2818 * placed outside of screen */
2819 mwidth = (int)ceil(sqrt(dock->max_icons));
2821 /* In the worst case (the clip is in the corner of the screen),
2822 * the amount of icons that fit in the clip is smaller.
2823 * Double the map to get a safe value.
2825 mwidth += mwidth;
2827 r = (mwidth-1)/2;
2829 slot_map = wmalloc(mwidth*mwidth);
2830 memset(slot_map, 0, mwidth*mwidth);
2832 #define XY2OFS(x,y) (WMAX(abs(x),abs(y)) > r) ? 0 : (((y)+r)*(mwidth)+(x)+r)
2834 /* mark used slots in the map. If the slot falls outside the map
2835 * (for example, when all icons are placed in line), ignore them. */
2836 for (i=0; i<dock->max_icons; i++) {
2837 btn = dock->icon_array[i];
2838 if (btn)
2839 slot_map[XY2OFS(btn->xindex, btn->yindex)] = 1;
2841 for (chain=scr->global_icons; chain!=NULL; chain=chain->next) {
2842 slot_map[XY2OFS(chain->aicon->xindex, chain->aicon->yindex)] = 1;
2844 /* Find closest slot from the center that is free by scanning the
2845 * map from the center to outward in circular passes.
2846 * This will not result in a neat layout, but will be optimal
2847 * in the sense that there will not be holes left.
2849 done = 0;
2850 for (i = 1; i <= r && !done; i++) {
2851 int tx, ty;
2853 /* top and bottom parts of the ring */
2854 for (x = -i; x <= i && !done; x++) {
2855 tx = dock->x_pos + x*ICON_SIZE;
2856 y = -i;
2857 ty = dock->y_pos + y*ICON_SIZE;
2858 if (slot_map[XY2OFS(x,y)]==0
2859 && ON_SCREEN(tx, ty, sx, ex, sy, ey)) {
2860 *x_pos = x;
2861 *y_pos = y;
2862 done = 1;
2863 break;
2865 y = i;
2866 ty = dock->y_pos + y*ICON_SIZE;
2867 if (slot_map[XY2OFS(x,y)]==0
2868 && ON_SCREEN(tx, ty, sx, ex, sy, ey)) {
2869 *x_pos = x;
2870 *y_pos = y;
2871 done = 1;
2872 break;
2875 /* left and right parts of the ring */
2876 for (y = -i+1; y <= i-1; y++) {
2877 ty = dock->y_pos + y*ICON_SIZE;
2878 x = -i;
2879 tx = dock->x_pos + x*ICON_SIZE;
2880 if (slot_map[XY2OFS(x,y)]==0
2881 && ON_SCREEN(tx, ty, sx, ex, sy, ey)) {
2882 *x_pos = x;
2883 *y_pos = y;
2884 done = 1;
2885 break;
2887 x = i;
2888 tx = dock->x_pos + x*ICON_SIZE;
2889 if (slot_map[XY2OFS(x,y)]==0
2890 && ON_SCREEN(tx, ty, sx, ex, sy, ey)) {
2891 *x_pos = x;
2892 *y_pos = y;
2893 done = 1;
2894 break;
2898 wfree(slot_map);
2899 #undef XY2OFS
2900 return done;
2904 static void
2905 moveDock(WDock *dock, int new_x, int new_y)
2907 WAppIcon *btn;
2908 int i;
2910 dock->x_pos = new_x;
2911 dock->y_pos = new_y;
2912 for (i=0; i<dock->max_icons; i++) {
2913 btn = dock->icon_array[i];
2914 if (btn) {
2915 btn->x_pos = new_x + btn->xindex*ICON_SIZE;
2916 btn->y_pos = new_y + btn->yindex*ICON_SIZE;
2917 XMoveWindow(dpy, btn->icon->core->window, btn->x_pos, btn->y_pos);
2923 static void
2924 swapDock(WDock *dock)
2926 WScreen *scr = dock->screen_ptr;
2927 WAppIcon *btn;
2928 int x, i;
2931 if (dock->on_right_side) {
2932 x = dock->x_pos = scr->scr_width - ICON_SIZE - DOCK_EXTRA_SPACE;
2933 } else {
2934 x = dock->x_pos = DOCK_EXTRA_SPACE;
2937 for (i=0; i<dock->max_icons; i++) {
2938 btn = dock->icon_array[i];
2939 if (btn) {
2940 btn->x_pos = x;
2941 XMoveWindow(dpy, btn->icon->core->window, btn->x_pos, btn->y_pos);
2945 wScreenUpdateUsableArea(scr);
2949 static pid_t
2950 execCommand(WAppIcon *btn, char *command, WSavedState *state)
2952 WScreen *scr = btn->icon->core->screen_ptr;
2953 pid_t pid;
2954 char **argv;
2955 int argc;
2956 char *cmdline;
2958 cmdline = ExpandOptions(scr, command);
2960 if (scr->flags.dnd_data_convertion_status || !cmdline) {
2961 if (cmdline)
2962 wfree(cmdline);
2963 if (state)
2964 wfree(state);
2965 return 0;
2968 wtokensplit(cmdline, &argv, &argc);
2970 if (argv==NULL) {
2971 if (cmdline)
2972 wfree(cmdline);
2973 if (state)
2974 wfree(state);
2975 return 0;
2978 if ((pid=fork())==0) {
2979 char **args;
2980 int i;
2982 SetupEnvironment(scr);
2984 #ifdef HAVE_SETSID
2985 setsid();
2986 #endif
2988 args = malloc(sizeof(char*)*(argc+1));
2989 if (!args)
2990 exit(111);
2991 for (i=0; i<argc; i++) {
2992 args[i] = argv[i];
2994 args[argc] = NULL;
2995 execvp(argv[0], args);
2996 exit(111);
2998 wtokenfree(argv, argc);
3000 if (pid > 0) {
3001 if (!state) {
3002 state = wmalloc(sizeof(WSavedState));
3003 memset(state, 0, sizeof(WSavedState));
3004 state->hidden = -1;
3005 state->miniaturized = -1;
3006 state->shaded = -1;
3007 if (btn->dock==scr->dock || btn->omnipresent)
3008 state->workspace = -1;
3009 else
3010 state->workspace = scr->current_workspace;
3012 wWindowAddSavedState(btn->wm_instance, btn->wm_class, cmdline, pid,
3013 state);
3014 wAddDeathHandler(pid, (WDeathHandler*)trackDeadProcess,
3015 btn->dock);
3016 } else if (state) {
3017 wfree(state);
3019 wfree(cmdline);
3020 return pid;
3024 void
3025 wDockHideIcons(WDock *dock)
3027 int i;
3029 if (dock==NULL)
3030 return;
3032 for (i=1; i<dock->max_icons; i++) {
3033 if (dock->icon_array[i])
3034 XUnmapWindow(dpy, dock->icon_array[i]->icon->core->window);
3036 dock->mapped = 0;
3038 dockIconPaint(dock->icon_array[0]);
3042 void
3043 wDockShowIcons(WDock *dock)
3045 int i, newlevel;
3046 WAppIcon *btn;
3048 if (dock==NULL)
3049 return;
3051 btn = dock->icon_array[0];
3052 moveDock(dock, btn->x_pos, btn->y_pos);
3054 newlevel = dock->lowered ? WMNormalLevel : WMDockLevel;
3055 ChangeStackingLevel(btn->icon->core, newlevel);
3057 for (i=1; i<dock->max_icons; i++) {
3058 if (dock->icon_array[i]) {
3059 MoveInStackListAbove(dock->icon_array[i]->icon->core,
3060 btn->icon->core);
3061 break;
3065 if (!dock->collapsed) {
3066 for (i=1; i<dock->max_icons; i++) {
3067 if (dock->icon_array[i]) {
3068 XMapWindow(dpy, dock->icon_array[i]->icon->core->window);
3072 dock->mapped = 1;
3074 dockIconPaint(btn);
3078 void
3079 wDockLower(WDock *dock)
3081 int i;
3083 for (i=0; i<dock->max_icons; i++) {
3084 if (dock->icon_array[i])
3085 wLowerFrame(dock->icon_array[i]->icon->core);
3090 void
3091 wDockRaise(WDock *dock)
3093 int i;
3095 for (i=dock->max_icons-1; i>=0; i--) {
3096 if (dock->icon_array[i])
3097 wRaiseFrame(dock->icon_array[i]->icon->core);
3102 void
3103 wDockRaiseLower(WDock *dock)
3105 if (!dock->icon_array[0]->icon->core->stacking->above
3106 ||(dock->icon_array[0]->icon->core->stacking->window_level
3107 !=dock->icon_array[0]->icon->core->stacking->above->stacking->window_level))
3108 wDockLower(dock);
3109 else
3110 wDockRaise(dock);
3114 void
3115 wDockFinishLaunch(WDock *dock, WAppIcon *icon)
3117 icon->launching = 0;
3118 icon->relaunching = 0;
3119 dockIconPaint(icon);
3123 WAppIcon*
3124 wDockFindIconForWindow(WDock *dock, Window window)
3126 WAppIcon *icon;
3127 int i;
3129 for (i=0; i<dock->max_icons; i++) {
3130 icon = dock->icon_array[i];
3131 if (icon && icon->main_window == window)
3132 return icon;
3134 return NULL;
3138 void
3139 wDockTrackWindowLaunch(WDock *dock, Window window)
3141 WAppIcon *icon;
3142 char *wm_class, *wm_instance;
3143 int i;
3144 Bool firstPass = True;
3145 Bool found = False;
3146 char *command = NULL;
3149 int argc;
3150 char **argv;
3152 if (XGetCommand(dpy, window, &argv, &argc)) {
3153 if (argc > 0 && argv != NULL)
3154 command = wtokenjoin(argv, argc);
3155 if (argv) {
3156 XFreeStringList(argv);
3161 if (!PropGetWMClass(window, &wm_class, &wm_instance) ||
3162 (!wm_class && !wm_instance))
3163 return;
3165 retry:
3166 for (i=0; i<dock->max_icons; i++) {
3167 icon = dock->icon_array[i];
3168 if (!icon)
3169 continue;
3171 /* app is already attached to icon */
3172 if (icon->main_window == window) {
3173 found = True;
3174 break;
3177 if ((icon->wm_instance || icon->wm_class)
3178 && (icon->launching || !icon->running)) {
3180 if (icon->wm_instance && wm_instance &&
3181 strcmp(icon->wm_instance, wm_instance)!=0) {
3182 continue;
3184 if (icon->wm_class && wm_class &&
3185 strcmp(icon->wm_class, wm_class)!=0) {
3186 continue;
3188 if (firstPass && command && strcmp(icon->command, command)!=0) {
3189 continue;
3192 if (!icon->relaunching) {
3193 WApplication *wapp;
3195 /* Possibly an application that was docked with dockit,
3196 * but the user did not update WMState to indicate that
3197 * it was docked by force */
3198 wapp = wApplicationOf(window);
3199 if (!wapp) {
3200 icon->forced_dock = 1;
3201 icon->running = 0;
3203 if (!icon->forced_dock)
3204 icon->main_window = window;
3207 found = True;
3208 if (!wPreferences.no_animations && !icon->launching &&
3209 !dock->screen_ptr->flags.startup && !dock->collapsed) {
3210 WAppIcon *aicon;
3211 int x0, y0;
3213 icon->launching = 1;
3214 dockIconPaint(icon);
3216 aicon = wAppIconCreateForDock(dock->screen_ptr, NULL,
3217 wm_instance, wm_class,
3218 TILE_NORMAL);
3219 PlaceIcon(dock->screen_ptr, &x0, &y0);
3220 wAppIconMove(aicon, x0, y0);
3221 /* Should this always be lowered? -Dan */
3222 if (dock->lowered)
3223 wLowerFrame(aicon->icon->core);
3224 XMapWindow(dpy, aicon->icon->core->window);
3225 aicon->launching = 1;
3226 wAppIconPaint(aicon);
3227 SlideWindow(aicon->icon->core->window, x0, y0,
3228 icon->x_pos, icon->y_pos);
3229 XUnmapWindow(dpy, aicon->icon->core->window);
3230 wAppIconDestroy(aicon);
3232 wDockFinishLaunch(dock, icon);
3233 break;
3237 if (firstPass && !found) {
3238 firstPass = False;
3239 goto retry;
3242 if (command)
3243 wfree(command);
3245 if (wm_class)
3246 XFree(wm_class);
3247 if (wm_instance)
3248 XFree(wm_instance);
3253 void
3254 wClipUpdateForWorkspaceChange(WScreen *scr, int workspace)
3256 if (!wPreferences.flags.noclip) {
3257 scr->clip_icon->dock = scr->workspaces[workspace]->clip;
3258 if (scr->current_workspace != workspace) {
3259 WDock *old_clip = scr->workspaces[scr->current_workspace]->clip;
3260 WAppIconChain *chain = scr->global_icons;
3262 while (chain) {
3263 moveIconBetweenDocks(chain->aicon->dock,
3264 scr->workspaces[workspace]->clip,
3265 chain->aicon, chain->aicon->xindex,
3266 chain->aicon->yindex);
3267 if (scr->workspaces[workspace]->clip->collapsed)
3268 XUnmapWindow(dpy, chain->aicon->icon->core->window);
3269 chain = chain->next;
3272 wDockHideIcons(old_clip);
3273 if (old_clip->auto_raise_lower) {
3274 if (old_clip->auto_raise_magic) {
3275 WMDeleteTimerHandler(old_clip->auto_raise_magic);
3276 old_clip->auto_raise_magic = NULL;
3278 wDockLower(old_clip);
3280 if (old_clip->auto_collapse) {
3281 if (old_clip->auto_expand_magic) {
3282 WMDeleteTimerHandler(old_clip->auto_expand_magic);
3283 old_clip->auto_expand_magic = NULL;
3285 old_clip->collapsed = 1;
3287 wDockShowIcons(scr->workspaces[workspace]->clip);
3289 if (scr->flags.clip_balloon_mapped)
3290 showClipBalloon(scr->clip_icon->dock, workspace);
3296 static void
3297 trackDeadProcess(pid_t pid, unsigned char status, WDock *dock)
3299 WAppIcon *icon;
3300 int i;
3302 for (i=0; i<dock->max_icons; i++) {
3303 icon = dock->icon_array[i];
3304 if (!icon)
3305 continue;
3307 if (icon->launching && icon->pid == pid) {
3308 if (!icon->relaunching) {
3309 icon->running = 0;
3310 icon->main_window = None;
3312 wDockFinishLaunch(dock, icon);
3313 icon->pid = 0;
3314 if (status==111) {
3315 char msg[PATH_MAX];
3316 char *cmd;
3318 if (icon->drop_launch)
3319 cmd = icon->dnd_command;
3320 else if (icon->paste_launch)
3321 cmd = icon->paste_command;
3322 else
3323 cmd = icon->command;
3325 snprintf(msg, sizeof(msg),
3326 _("Could not execute command \"%s\""), cmd);
3328 wMessageDialog(dock->screen_ptr, _("Error"), msg,
3329 _("OK"), NULL, NULL);
3331 break;
3337 static void
3338 toggleLowered(WDock *dock)
3340 WAppIcon *tmp;
3341 int newlevel, i;
3343 /* lower/raise Dock */
3344 if (!dock->lowered) {
3345 newlevel = WMNormalLevel;
3346 dock->lowered = 1;
3347 } else {
3348 newlevel = WMDockLevel;
3349 dock->lowered = 0;
3352 for (i=0; i<dock->max_icons; i++) {
3353 tmp = dock->icon_array[i];
3354 if (!tmp)
3355 continue;
3357 ChangeStackingLevel(tmp->icon->core, newlevel);
3358 if (dock->lowered)
3359 wLowerFrame(tmp->icon->core);
3362 if (dock->type == WM_DOCK)
3363 wScreenUpdateUsableArea(dock->screen_ptr);
3367 static void
3368 toggleCollapsed(WDock *dock)
3370 if (dock->collapsed) {
3371 dock->collapsed = 0;
3372 wDockShowIcons(dock);
3374 else {
3375 dock->collapsed = 1;
3376 wDockHideIcons(dock);
3381 static void
3382 openDockMenu(WDock *dock, WAppIcon *aicon, XEvent *event)
3384 WScreen *scr = dock->screen_ptr;
3385 WObjDescriptor *desc;
3386 WMenuEntry *entry;
3387 WApplication *wapp = NULL;
3388 int index = 0;
3389 int x_pos;
3390 int n_selected;
3391 int appIsRunning = aicon->running && aicon->icon && aicon->icon->owner;
3393 if (dock->type == WM_DOCK) {
3394 /* keep on top */
3395 entry = dock->menu->entries[index];
3396 entry->flags.indicator_on = !dock->lowered;
3397 entry->clientdata = dock;
3398 dock->menu->flags.realized = 0;
3399 } else {
3400 /* clip options */
3401 if (scr->clip_options)
3402 updateClipOptionsMenu(scr->clip_options, dock);
3404 n_selected = numberOfSelectedIcons(dock);
3406 /* Rename Workspace */
3407 entry = dock->menu->entries[++index];
3408 if (aicon == scr->clip_icon) {
3409 entry->callback = renameCallback;
3410 entry->clientdata = dock;
3411 entry->flags.indicator = 0;
3412 entry->text = _("Rename Workspace");
3413 } else {
3414 entry->callback = omnipresentCallback;
3415 entry->clientdata = aicon;
3416 if (n_selected > 0) {
3417 entry->flags.indicator = 0;
3418 entry->text = _("Toggle Omnipresent");
3419 } else {
3420 entry->flags.indicator = 1;
3421 entry->flags.indicator_on = aicon->omnipresent;
3422 entry->flags.indicator_type = MI_CHECK;
3423 entry->text = _("Omnipresent");
3427 /* select/unselect icon */
3428 entry = dock->menu->entries[++index];
3429 entry->clientdata = aicon;
3430 entry->flags.indicator_on = aicon->icon->selected;
3431 wMenuSetEnabled(dock->menu, index, aicon!=scr->clip_icon);
3433 /* select/unselect all icons */
3434 entry = dock->menu->entries[++index];
3435 entry->clientdata = aicon;
3436 if (n_selected > 0)
3437 entry->text = _("Unselect All Icons");
3438 else
3439 entry->text = _("Select All Icons");
3440 wMenuSetEnabled(dock->menu, index, dock->icon_count > 1);
3442 /* keep icon(s) */
3443 entry = dock->menu->entries[++index];
3444 entry->clientdata = aicon;
3445 if (n_selected > 1)
3446 entry->text = _("Keep Icons");
3447 else
3448 entry->text = _("Keep Icon");
3449 wMenuSetEnabled(dock->menu, index, dock->icon_count > 1);
3451 /* this is the workspace submenu part */
3452 entry = dock->menu->entries[++index];
3453 if (n_selected > 1)
3454 entry->text = _("Move Icons To");
3455 else
3456 entry->text = _("Move Icon To");
3457 if (scr->clip_submenu)
3458 updateWorkspaceMenu(scr->clip_submenu, aicon);
3459 wMenuSetEnabled(dock->menu, index, !aicon->omnipresent);
3461 /* remove icon(s) */
3462 entry = dock->menu->entries[++index];
3463 entry->clientdata = aicon;
3464 if (n_selected > 1)
3465 entry->text = _("Remove Icons");
3466 else
3467 entry->text = _("Remove Icon");
3468 wMenuSetEnabled(dock->menu, index, dock->icon_count > 1);
3470 /* attract icon(s) */
3471 entry = dock->menu->entries[++index];
3472 entry->clientdata = aicon;
3474 dock->menu->flags.realized = 0;
3475 wMenuRealize(dock->menu);
3479 if (aicon->icon->owner) {
3480 wapp = wApplicationOf(aicon->icon->owner->main_window);
3481 } else {
3482 wapp = NULL;
3485 /* launch */
3486 entry = dock->menu->entries[++index];
3487 entry->clientdata = aicon;
3488 wMenuSetEnabled(dock->menu, index, aicon->command!=NULL);
3490 /* unhide here */
3491 entry = dock->menu->entries[++index];
3492 entry->clientdata = aicon;
3493 if (wapp && wapp->flags.hidden) {
3494 entry->text = _("Unhide Here");
3495 } else {
3496 entry->text = _("Bring Here");
3498 wMenuSetEnabled(dock->menu, index, appIsRunning);
3500 /* hide */
3501 entry = dock->menu->entries[++index];
3502 entry->clientdata = aicon;
3503 if (wapp && wapp->flags.hidden) {
3504 entry->text = _("Unhide");
3505 } else {
3506 entry->text = _("Hide");
3508 wMenuSetEnabled(dock->menu, index, appIsRunning);
3510 /* settings */
3511 entry = dock->menu->entries[++index];
3512 entry->clientdata = aicon;
3513 wMenuSetEnabled(dock->menu, index, !aicon->editing
3514 && !wPreferences.flags.noupdates);
3516 /* kill */
3517 entry = dock->menu->entries[++index];
3518 entry->clientdata = aicon;
3519 wMenuSetEnabled(dock->menu, index, appIsRunning);
3521 if (!dock->menu->flags.realized)
3522 wMenuRealize(dock->menu);
3524 if (dock->type == WM_CLIP) {
3525 /*x_pos = event->xbutton.x_root+2;*/
3526 x_pos = event->xbutton.x_root - dock->menu->frame->core->width/2 - 1;
3527 if (x_pos < 0) {
3528 x_pos = 0;
3529 } else if (x_pos + dock->menu->frame->core->width > scr->scr_width-2) {
3530 x_pos = scr->scr_width - dock->menu->frame->core->width - 4;
3532 } else {
3533 x_pos = dock->on_right_side ?
3534 scr->scr_width - dock->menu->frame->core->width - 3 : 0;
3537 wMenuMapAt(dock->menu, x_pos, event->xbutton.y_root+2, False);
3539 /* allow drag select */
3540 event->xany.send_event = True;
3541 desc = &dock->menu->menu->descriptor;
3542 (*desc->handle_mousedown)(desc, event);
3546 static void
3547 openClipWorkspaceMenu(WScreen *scr, int x, int y)
3549 if (!scr->clip_ws_menu) {
3550 scr->clip_ws_menu = wWorkspaceMenuMake(scr, False);
3552 wWorkspaceMenuUpdate(scr, scr->clip_ws_menu);
3553 wMenuMapAt(scr->clip_ws_menu, x, y, False);
3557 /******************************************************************/
3558 static void
3559 iconDblClick(WObjDescriptor *desc, XEvent *event)
3561 WAppIcon *btn = desc->parent;
3562 WDock *dock = btn->dock;
3563 WApplication *wapp = NULL;
3564 int unhideHere = 0;
3566 if (btn->icon->owner && !(event->xbutton.state & ControlMask)) {
3567 wapp = wApplicationOf(btn->icon->owner->main_window);
3569 assert(wapp!=NULL);
3571 unhideHere = (event->xbutton.state & ShiftMask);
3573 /* go to the last workspace that the user worked on the app */
3574 if (wapp->last_workspace != dock->screen_ptr->current_workspace
3575 && !unhideHere) {
3576 wWorkspaceChange(dock->screen_ptr, wapp->last_workspace);
3579 wUnhideApplication(wapp, event->xbutton.button==Button2,
3580 unhideHere);
3582 if (event->xbutton.state & MOD_MASK) {
3583 wHideOtherApplications(btn->icon->owner);
3585 } else {
3586 if (event->xbutton.button==Button1) {
3588 if (event->xbutton.state & MOD_MASK) {
3589 /* raise/lower dock */
3590 toggleLowered(dock);
3591 } else if (btn == dock->screen_ptr->clip_icon) {
3592 if (getClipButton(event->xbutton.x, event->xbutton.y)==CLIP_IDLE)
3593 toggleCollapsed(dock);
3594 else
3595 handleClipChangeWorkspace(dock->screen_ptr, event);
3596 } else if (btn->command) {
3597 if (!btn->launching &&
3598 (!btn->running || (event->xbutton.state & ControlMask))) {
3599 launchDockedApplication(btn, False);
3601 } else if (btn->xindex == 0 && btn->yindex == 0
3602 && btn->dock->type == WM_DOCK) {
3604 wShowGNUstepPanel(dock->screen_ptr);
3612 static void
3613 handleDockMove(WDock *dock, WAppIcon *aicon, XEvent *event)
3615 WScreen *scr = dock->screen_ptr;
3616 int ofs_x=event->xbutton.x, ofs_y=event->xbutton.y;
3617 int x, y;
3618 XEvent ev;
3619 int grabbed = 0, swapped = 0, done;
3620 Pixmap ghost = None;
3621 int superfluous = wPreferences.superfluous; /* we catch it to avoid problems */
3623 #ifdef DEBUG
3624 puts("moving dock");
3625 #endif
3626 if (XGrabPointer(dpy, aicon->icon->core->window, True, ButtonMotionMask
3627 |ButtonReleaseMask|ButtonPressMask, GrabModeAsync,
3628 GrabModeAsync, None, None, CurrentTime) !=GrabSuccess) {
3629 wwarning("pointer grab failed for dock move");
3631 y = 0;
3632 for (x=0; x<dock->max_icons; x++) {
3633 if (dock->icon_array[x]!=NULL &&
3634 dock->icon_array[x]->yindex > y)
3635 y = dock->icon_array[x]->yindex;
3637 y++;
3638 XResizeWindow(dpy, scr->dock_shadow, ICON_SIZE, ICON_SIZE*y);
3640 done = 0;
3641 while (!done) {
3642 WMMaskEvent(dpy, PointerMotionMask|ButtonReleaseMask|ButtonPressMask
3643 |ButtonMotionMask|ExposureMask, &ev);
3644 switch (ev.type) {
3645 case Expose:
3646 WMHandleEvent(&ev);
3647 break;
3649 case MotionNotify:
3650 if (!grabbed) {
3651 if (abs(ofs_x-ev.xmotion.x)>=MOVE_THRESHOLD
3652 || abs(ofs_y-ev.xmotion.y)>=MOVE_THRESHOLD) {
3653 XChangeActivePointerGrab(dpy, ButtonMotionMask
3654 |ButtonReleaseMask|ButtonPressMask,
3655 wCursor[WCUR_MOVE], CurrentTime);
3656 grabbed=1;
3658 break;
3660 if (dock->type == WM_CLIP) {
3661 if (ev.xmotion.x_root - ofs_x < 0) {
3662 x = 0;
3663 } else if (ev.xmotion.x_root - ofs_x + ICON_SIZE >
3664 scr->scr_width) {
3665 x = scr->scr_width - ICON_SIZE;
3666 } else {
3667 x = ev.xmotion.x_root - ofs_x;
3669 if (ev.xmotion.y_root - ofs_y < 0) {
3670 y = 0;
3671 } else if (ev.xmotion.y_root - ofs_y + ICON_SIZE >
3672 scr->scr_height) {
3673 y = scr->scr_height - ICON_SIZE;
3674 } else {
3675 y = ev.xmotion.y_root - ofs_y;
3677 moveDock(dock, x, y);
3678 } else {
3679 /* move vertically if pointer is inside the dock*/
3680 if ((dock->on_right_side &&
3681 ev.xmotion.x_root >= dock->x_pos - ICON_SIZE)
3682 || (!dock->on_right_side &&
3683 ev.xmotion.x_root <= dock->x_pos + ICON_SIZE*2)) {
3685 if (ev.xmotion.y_root - ofs_y < 0) {
3686 y = 0;
3687 } else if (ev.xmotion.y_root - ofs_y + ICON_SIZE >
3688 scr->scr_height) {
3689 y = scr->scr_height - ICON_SIZE;
3690 } else {
3691 y = ev.xmotion.y_root - ofs_y;
3693 moveDock(dock, dock->x_pos, y);
3695 /* move horizontally to change sides */
3696 x = ev.xmotion.x_root - ofs_x;
3697 if (!dock->on_right_side) {
3699 /* is on left */
3701 if (ev.xmotion.x_root > dock->x_pos + ICON_SIZE*2) {
3702 XMoveWindow(dpy, scr->dock_shadow, scr->scr_width-ICON_SIZE
3703 -DOCK_EXTRA_SPACE-1, dock->y_pos);
3704 if (superfluous && ghost==None) {
3705 ghost = MakeGhostDock(dock, dock->x_pos,
3706 scr->scr_width-ICON_SIZE
3707 -DOCK_EXTRA_SPACE-1,
3708 dock->y_pos);
3709 XSetWindowBackgroundPixmap(dpy, scr->dock_shadow,
3710 ghost);
3711 XClearWindow(dpy, scr->dock_shadow);
3713 XMapRaised(dpy, scr->dock_shadow);
3714 swapped = 1;
3715 } else {
3716 if (superfluous && ghost!=None) {
3717 XFreePixmap(dpy, ghost);
3718 ghost = None;
3720 XUnmapWindow(dpy, scr->dock_shadow);
3721 swapped = 0;
3723 } else {
3724 /* is on right */
3725 if (ev.xmotion.x_root < dock->x_pos - ICON_SIZE) {
3726 XMoveWindow(dpy, scr->dock_shadow,
3727 DOCK_EXTRA_SPACE, dock->y_pos);
3728 if (superfluous && ghost==None) {
3729 ghost = MakeGhostDock(dock, dock->x_pos,
3730 DOCK_EXTRA_SPACE, dock->y_pos);
3731 XSetWindowBackgroundPixmap(dpy, scr->dock_shadow,
3732 ghost);
3733 XClearWindow(dpy, scr->dock_shadow);
3735 XMapRaised(dpy, scr->dock_shadow);
3736 swapped = -1;
3737 } else {
3738 XUnmapWindow(dpy, scr->dock_shadow);
3739 swapped = 0;
3740 if (superfluous && ghost!=None) {
3741 XFreePixmap(dpy, ghost);
3742 ghost = None;
3747 break;
3749 case ButtonPress:
3750 break;
3752 case ButtonRelease:
3753 if (ev.xbutton.button != event->xbutton.button)
3754 break;
3755 XUngrabPointer(dpy, CurrentTime);
3756 XUnmapWindow(dpy, scr->dock_shadow);
3757 XResizeWindow(dpy, scr->dock_shadow, ICON_SIZE, ICON_SIZE);
3758 if (dock->type == WM_DOCK) {
3759 if (swapped!=0) {
3760 if (swapped>0)
3761 dock->on_right_side = 1;
3762 else
3763 dock->on_right_side = 0;
3764 swapDock(dock);
3765 wArrangeIcons(scr, False);
3768 done = 1;
3769 break;
3772 if (superfluous) {
3773 if (ghost!=None)
3774 XFreePixmap(dpy, ghost);
3775 XSetWindowBackground(dpy, scr->dock_shadow, scr->white_pixel);
3777 #ifdef DEBUG
3778 puts("End dock move");
3779 #endif
3784 static void
3785 handleIconMove(WDock *dock, WAppIcon *aicon, XEvent *event)
3787 WScreen *scr = dock->screen_ptr;
3788 Window wins[2];
3789 WIcon *icon = aicon->icon;
3790 WDock *dock2 = NULL, *last_dock = dock, *clip = NULL;
3791 int ondock, grabbed = 0, change_dock = 0, collapsed = 0;
3792 XEvent ev;
3793 int x = aicon->x_pos, y = aicon->y_pos;
3794 int ofs_x = event->xbutton.x, ofs_y = event->xbutton.y;
3795 int shad_x = x, shad_y = y;
3796 int ix = aicon->xindex, iy = aicon->yindex;
3797 int tmp;
3798 Pixmap ghost = None;
3799 Bool docked;
3800 int superfluous = wPreferences.superfluous; /* we catch it to avoid problems */
3801 int omnipresent = aicon->omnipresent; /* this must be cached!!! */
3804 if (wPreferences.flags.noupdates)
3805 return;
3807 if (XGrabPointer(dpy, icon->core->window, True, ButtonMotionMask
3808 |ButtonReleaseMask|ButtonPressMask, GrabModeAsync,
3809 GrabModeAsync, None, None, CurrentTime) !=GrabSuccess) {
3810 #ifdef DEBUG0
3811 wwarning("pointer grab failed for icon move");
3812 #endif
3815 if (!(event->xbutton.state & MOD_MASK))
3816 wRaiseFrame(icon->core);
3818 if (!wPreferences.flags.noclip)
3819 clip = scr->workspaces[scr->current_workspace]->clip;
3821 if (dock == scr->dock && !wPreferences.flags.noclip)
3822 dock2 = clip;
3823 else if (dock != scr->dock && !wPreferences.flags.nodock)
3824 dock2 = scr->dock;
3826 wins[0] = icon->core->window;
3827 wins[1] = scr->dock_shadow;
3828 XRestackWindows(dpy, wins, 2);
3829 XMoveResizeWindow(dpy, scr->dock_shadow, aicon->x_pos, aicon->y_pos,
3830 ICON_SIZE, ICON_SIZE);
3831 if (superfluous) {
3832 if (icon->pixmap!=None)
3833 ghost = MakeGhostIcon(scr, icon->pixmap);
3834 else
3835 ghost = MakeGhostIcon(scr, icon->core->window);
3837 XSetWindowBackgroundPixmap(dpy, scr->dock_shadow, ghost);
3838 XClearWindow(dpy, scr->dock_shadow);
3840 XMapWindow(dpy, scr->dock_shadow);
3842 ondock = 1;
3845 while(1) {
3846 XMaskEvent(dpy, PointerMotionMask|ButtonReleaseMask|ButtonPressMask
3847 |ButtonMotionMask|ExposureMask, &ev);
3848 switch (ev.type) {
3849 case Expose:
3850 WMHandleEvent(&ev);
3851 break;
3853 case MotionNotify:
3854 if (!grabbed) {
3855 if (abs(ofs_x-ev.xmotion.x)>=MOVE_THRESHOLD
3856 || abs(ofs_y-ev.xmotion.y)>=MOVE_THRESHOLD) {
3857 XChangeActivePointerGrab(dpy, ButtonMotionMask
3858 |ButtonReleaseMask|ButtonPressMask,
3859 wCursor[WCUR_MOVE], CurrentTime);
3860 grabbed=1;
3861 } else {
3862 break;
3866 if (omnipresent) {
3867 int i;
3868 for (i=0; i<scr->workspace_count; i++) {
3869 if (i == scr->current_workspace)
3870 continue;
3871 wDockShowIcons(scr->workspaces[i]->clip);
3875 x = ev.xmotion.x_root - ofs_x;
3876 y = ev.xmotion.y_root - ofs_y;
3877 tmp = wDockSnapIcon(dock, aicon, x, y, &ix, &iy, True);
3878 if (tmp && dock2) {
3879 change_dock = 0;
3880 if (last_dock != dock && collapsed) {
3881 last_dock->collapsed = 1;
3882 wDockHideIcons(last_dock);
3883 collapsed = 0;
3885 if (!collapsed && (collapsed = dock->collapsed)) {
3886 dock->collapsed = 0;
3887 wDockShowIcons(dock);
3889 if (dock->auto_raise_lower)
3890 wDockRaise(dock);
3891 last_dock = dock;
3892 } else if (dock2) {
3893 tmp = wDockSnapIcon(dock2, aicon, x, y, &ix, &iy, False);
3894 if (tmp) {
3895 change_dock = 1;
3896 if (last_dock != dock2 && collapsed) {
3897 last_dock->collapsed = 1;
3898 wDockHideIcons(last_dock);
3899 collapsed = 0;
3901 if (!collapsed && (collapsed = dock2->collapsed)) {
3902 dock2->collapsed = 0;
3903 wDockShowIcons(dock2);
3905 if (dock2->auto_raise_lower)
3906 wDockRaise(dock2);
3907 last_dock = dock2;
3910 if (aicon->launching
3911 || aicon->lock
3912 || (aicon->running && !(ev.xmotion.state & MOD_MASK))
3913 || (!aicon->running && tmp)) {
3914 shad_x = last_dock->x_pos + ix*wPreferences.icon_size;
3915 shad_y = last_dock->y_pos + iy*wPreferences.icon_size;
3917 XMoveWindow(dpy, scr->dock_shadow, shad_x, shad_y);
3919 if (!ondock) {
3920 XMapWindow(dpy, scr->dock_shadow);
3922 ondock = 1;
3923 } else {
3924 if (ondock) {
3925 XUnmapWindow(dpy, scr->dock_shadow);
3927 ondock = 0;
3929 XMoveWindow(dpy, icon->core->window, x, y);
3930 break;
3932 case ButtonPress:
3933 break;
3935 case ButtonRelease:
3936 if (ev.xbutton.button != event->xbutton.button)
3937 break;
3938 XUngrabPointer(dpy, CurrentTime);
3939 if (ondock) {
3940 SlideWindow(icon->core->window, x, y, shad_x, shad_y);
3941 XUnmapWindow(dpy, scr->dock_shadow);
3942 if (!change_dock) {
3943 reattachIcon(dock, aicon, ix, iy);
3944 if (clip && dock!=clip && clip->auto_raise_lower)
3945 wDockLower(clip);
3946 } else {
3947 docked = moveIconBetweenDocks(dock, dock2, aicon, ix, iy);
3948 if (!docked) {
3949 /* Slide it back if dock rejected it */
3950 SlideWindow(icon->core->window, x, y, aicon->x_pos,
3951 aicon->y_pos);
3952 reattachIcon(dock, aicon, aicon->xindex,aicon->yindex);
3954 if (last_dock->type==WM_CLIP && last_dock->auto_collapse) {
3955 collapsed = 0;
3958 } else {
3959 aicon->x_pos = x;
3960 aicon->y_pos = y;
3961 if (superfluous) {
3962 if (!aicon->running && !wPreferences.no_animations) {
3963 /* We need to deselect it, even if is deselected in
3964 * wDockDetach(), because else DoKaboom() will fail.
3966 if (aicon->icon->selected)
3967 wIconSelect(aicon->icon);
3969 wSoundPlay(WSOUND_KABOOM);
3970 DoKaboom(scr,aicon->icon->core->window, x, y);
3971 } else {
3972 wSoundPlay(WSOUND_UNDOCK);
3974 } else {
3975 wSoundPlay(WSOUND_UNDOCK);
3977 if (clip && clip->auto_raise_lower)
3978 wDockLower(clip);
3979 wDockDetach(dock, aicon);
3981 if (collapsed) {
3982 last_dock->collapsed = 1;
3983 wDockHideIcons(last_dock);
3984 collapsed = 0;
3986 if (superfluous) {
3987 if (ghost!=None)
3988 XFreePixmap(dpy, ghost);
3989 XSetWindowBackground(dpy, scr->dock_shadow, scr->white_pixel);
3991 if (omnipresent) {
3992 int i;
3993 for (i=0; i<scr->workspace_count; i++) {
3994 if (i == scr->current_workspace)
3995 continue;
3996 wDockHideIcons(scr->workspaces[i]->clip);
4000 #ifdef DEBUG
4001 puts("End icon move");
4002 #endif
4003 return;
4009 static int
4010 getClipButton(int px, int py)
4012 int pt = (CLIP_BUTTON_SIZE+2)*ICON_SIZE/64;
4014 if (px < 0 || py < 0 || px >= ICON_SIZE || py >= ICON_SIZE)
4015 return CLIP_IDLE;
4017 if (py <= pt-((int)ICON_SIZE-1-px))
4018 return CLIP_FORWARD;
4019 else if (px <= pt-((int)ICON_SIZE-1-py))
4020 return CLIP_REWIND;
4022 return CLIP_IDLE;
4026 static void
4027 handleClipChangeWorkspace(WScreen *scr, XEvent *event)
4029 XEvent ev;
4030 int done, direction, new_ws;
4031 int new_dir;
4032 WDock *clip = scr->clip_icon->dock;
4034 direction = getClipButton(event->xbutton.x, event->xbutton.y);
4036 clip->lclip_button_pushed = direction==CLIP_REWIND;
4037 clip->rclip_button_pushed = direction==CLIP_FORWARD;
4039 wClipIconPaint(scr->clip_icon);
4040 done = 0;
4041 while(!done) {
4042 WMMaskEvent(dpy, ExposureMask|ButtonMotionMask|ButtonReleaseMask
4043 |ButtonPressMask, &ev);
4044 switch (ev.type) {
4045 case Expose:
4046 WMHandleEvent(&ev);
4047 break;
4049 case MotionNotify:
4050 new_dir = getClipButton(ev.xmotion.x, ev.xmotion.y);
4051 if (new_dir != direction) {
4052 direction = new_dir;
4053 clip->lclip_button_pushed = direction==CLIP_REWIND;
4054 clip->rclip_button_pushed = direction==CLIP_FORWARD;
4055 wClipIconPaint(scr->clip_icon);
4057 break;
4059 case ButtonPress:
4060 break;
4062 case ButtonRelease:
4063 if (ev.xbutton.button == event->xbutton.button)
4064 done = 1;
4068 clip->lclip_button_pushed = 0;
4069 clip->rclip_button_pushed = 0;
4071 new_ws = wPreferences.ws_advance || (event->xbutton.state & ControlMask);
4073 if (direction == CLIP_FORWARD) {
4074 if (scr->current_workspace < scr->workspace_count-1)
4075 wWorkspaceChange(scr, scr->current_workspace+1);
4076 else if (new_ws && scr->current_workspace < MAX_WORKSPACES-1)
4077 wWorkspaceChange(scr, scr->current_workspace+1);
4078 else if (wPreferences.ws_cycle)
4079 wWorkspaceChange(scr, 0);
4081 else if (direction == CLIP_REWIND) {
4082 if (scr->current_workspace > 0)
4083 wWorkspaceChange(scr, scr->current_workspace-1);
4084 else if (scr->current_workspace==0 && wPreferences.ws_cycle)
4085 wWorkspaceChange(scr, scr->workspace_count-1);
4088 wClipIconPaint(scr->clip_icon);
4092 static void
4093 iconMouseDown(WObjDescriptor *desc, XEvent *event)
4095 WAppIcon *aicon = desc->parent;
4096 WDock *dock = aicon->dock;
4097 WScreen *scr = aicon->icon->core->screen_ptr;
4099 if (aicon->editing || WCHECK_STATE(WSTATE_MODAL))
4100 return;
4102 scr->last_dock = dock;
4104 if (dock->menu->flags.mapped)
4105 wMenuUnmap(dock->menu);
4107 if (IsDoubleClick(scr, event)) {
4108 /* double-click was not in the main clip icon */
4109 if (dock->type != WM_CLIP || aicon->xindex!=0 || aicon->yindex!=0
4110 || getClipButton(event->xbutton.x, event->xbutton.y)==CLIP_IDLE) {
4111 iconDblClick(desc, event);
4112 return;
4116 if (dock->type == WM_CLIP && scr->flags.clip_balloon_mapped) {
4117 XUnmapWindow(dpy, scr->clip_balloon);
4118 scr->flags.clip_balloon_mapped = 0;
4121 #ifdef DEBUG
4122 puts("handling dock");
4123 #endif
4124 if (event->xbutton.button == Button1) {
4125 if (event->xbutton.state & MOD_MASK)
4126 wDockLower(dock);
4127 else
4128 wDockRaise(dock);
4130 if ((event->xbutton.state & ShiftMask) && aicon!=scr->clip_icon &&
4131 dock->type!=WM_DOCK) {
4132 wIconSelect(aicon->icon);
4133 return;
4136 if (aicon->yindex==0 && aicon->xindex==0) {
4137 if (getClipButton(event->xbutton.x, event->xbutton.y)!=CLIP_IDLE
4138 && dock->type==WM_CLIP)
4139 handleClipChangeWorkspace(scr, event);
4140 else
4141 handleDockMove(dock, aicon, event);
4142 } else
4143 handleIconMove(dock, aicon, event);
4145 } else if (event->xbutton.button==Button2 && dock->type==WM_CLIP &&
4146 aicon==scr->clip_icon) {
4147 openClipWorkspaceMenu(scr, event->xbutton.x_root+2,
4148 event->xbutton.y_root+2);
4149 if (scr->clip_ws_menu) {
4150 WMenu *menu;
4151 menu = scr->clip_ws_menu;
4152 desc = &menu->menu->descriptor;
4154 event->xany.send_event = True;
4155 (*desc->handle_mousedown)(desc, event);
4157 } else if (event->xbutton.button==Button2 && dock->type==WM_CLIP &&
4158 (event->xbutton.state & ShiftMask) && aicon!=scr->clip_icon) {
4159 wClipMakeIconOmnipresent(aicon, !aicon->omnipresent);
4160 } else if (event->xbutton.button == Button3) {
4161 if (event->xbutton.send_event &&
4162 XGrabPointer(dpy, aicon->icon->core->window, True, ButtonMotionMask
4163 |ButtonReleaseMask|ButtonPressMask, GrabModeAsync,
4164 GrabModeAsync, None, None, CurrentTime) !=GrabSuccess) {
4165 wwarning("pointer grab failed for dockicon menu");
4166 return;
4169 openDockMenu(dock, aicon, event);
4170 } else if (event->xbutton.button == Button2) {
4171 WAppIcon *btn = desc->parent;
4173 if (!btn->launching &&
4174 (!btn->running || (event->xbutton.state & ControlMask))) {
4175 launchDockedApplication(btn, True);
4181 static void
4182 showClipBalloon(WDock *dock, int workspace)
4184 int w, h;
4185 int x, y;
4186 WScreen *scr = dock->screen_ptr;
4187 char *text;
4188 Window stack[2];
4190 scr->flags.clip_balloon_mapped = 1;
4191 XMapWindow(dpy, scr->clip_balloon);
4193 text = scr->workspaces[workspace]->name;
4195 w = WMWidthOfString(scr->clip_title_font, text, strlen(text));
4197 h = WMFontHeight(scr->clip_title_font);
4198 XResizeWindow(dpy, scr->clip_balloon, w, h);
4200 x = dock->x_pos + CLIP_BUTTON_SIZE*ICON_SIZE/64;
4201 y = dock->y_pos + ICON_SIZE - WMFontHeight(scr->clip_title_font) - 3;
4203 if (x+w > scr->scr_width) {
4204 x = scr->scr_width - w;
4205 if (dock->y_pos + ICON_SIZE + h > scr->scr_height)
4206 y = dock->y_pos - h - 1;
4207 else
4208 y = dock->y_pos + ICON_SIZE;
4209 XRaiseWindow(dpy, scr->clip_balloon);
4210 } else {
4211 stack[0] = scr->clip_icon->icon->core->window;
4212 stack[1] = scr->clip_balloon;
4213 XRestackWindows(dpy, stack, 2);
4215 XMoveWindow(dpy, scr->clip_balloon, x, y);
4216 XSetForeground(dpy, scr->clip_title_gc,
4217 scr->clip_title_pixel[CLIP_NORMAL]);
4218 XClearWindow(dpy, scr->clip_balloon);
4219 WMDrawString(scr->wmscreen, scr->clip_balloon, scr->clip_title_gc,
4220 scr->clip_title_font, 0, 0, text, strlen(text));
4224 static void
4225 clipEnterNotify(WObjDescriptor *desc, XEvent *event)
4227 WAppIcon *btn = (WAppIcon*)desc->parent;
4228 WDock *dock;
4229 WScreen *scr;
4231 assert(event->type==EnterNotify);
4233 if(desc->parent_type!=WCLASS_DOCK_ICON)
4234 return;
4236 scr = btn->icon->core->screen_ptr;
4237 if (!btn->omnipresent)
4238 dock = btn->dock;
4239 else
4240 dock = scr->workspaces[scr->current_workspace]->clip;
4242 if (!dock || dock->type!=WM_CLIP)
4243 return;
4245 /* The auto raise/lower code */
4246 if (dock->auto_lower_magic) {
4247 WMDeleteTimerHandler(dock->auto_lower_magic);
4248 dock->auto_lower_magic = NULL;
4250 if (dock->auto_raise_lower && !dock->auto_raise_magic) {
4251 dock->auto_raise_magic = WMAddTimerHandler(AUTO_RAISE_DELAY,
4252 clipAutoRaise,
4253 (void *)dock);
4256 /* The auto expand/collapse code */
4257 if (dock->auto_collapse_magic) {
4258 WMDeleteTimerHandler(dock->auto_collapse_magic);
4259 dock->auto_collapse_magic = NULL;
4261 if (dock->auto_collapse && !dock->auto_expand_magic) {
4262 dock->auto_expand_magic = WMAddTimerHandler(AUTO_EXPAND_DELAY,
4263 clipAutoExpand,
4264 (void *)dock);
4267 if (btn->xindex == 0 && btn->yindex == 0)
4268 showClipBalloon(dock, dock->screen_ptr->current_workspace);
4269 else {
4270 if (dock->screen_ptr->flags.clip_balloon_mapped) {
4271 XUnmapWindow(dpy, dock->screen_ptr->clip_balloon);
4272 dock->screen_ptr->flags.clip_balloon_mapped = 0;
4278 static void
4279 clipLeave(WDock *dock)
4281 XEvent event;
4282 WObjDescriptor *desc = NULL;
4284 if (!dock || dock->type!=WM_CLIP)
4285 return;
4287 if (XCheckTypedEvent(dpy, EnterNotify, &event)!=False) {
4288 if (XFindContext(dpy, event.xcrossing.window, wWinContext,
4289 (XPointer *)&desc)!=XCNOENT
4290 && desc && desc->parent_type==WCLASS_DOCK_ICON
4291 && ((WAppIcon*)desc->parent)->dock
4292 && ((WAppIcon*)desc->parent)->dock->type==WM_CLIP) {
4293 /* We didn't left the Clip yet */
4294 XPutBackEvent(dpy, &event);
4295 return;
4298 XPutBackEvent(dpy, &event);
4299 } else {
4300 /* We entered a withdrawn window, so we're still in Clip */
4301 return;
4304 if (dock->auto_raise_magic) {
4305 WMDeleteTimerHandler(dock->auto_raise_magic);
4306 dock->auto_raise_magic = NULL;
4308 if (dock->auto_raise_lower && !dock->auto_lower_magic) {
4309 dock->auto_lower_magic = WMAddTimerHandler(AUTO_LOWER_DELAY,
4310 clipAutoLower,
4311 (void *)dock);
4314 if (dock->auto_expand_magic) {
4315 WMDeleteTimerHandler(dock->auto_expand_magic);
4316 dock->auto_expand_magic = NULL;
4318 if (dock->auto_collapse && !dock->auto_collapse_magic) {
4319 dock->auto_collapse_magic = WMAddTimerHandler(AUTO_COLLAPSE_DELAY,
4320 clipAutoCollapse,
4321 (void *)dock);
4326 static void
4327 clipLeaveNotify(WObjDescriptor *desc, XEvent *event)
4329 WAppIcon *btn = (WAppIcon*)desc->parent;
4331 assert(event->type==LeaveNotify);
4333 if(desc->parent_type!=WCLASS_DOCK_ICON)
4334 return;
4336 clipLeave(btn->dock);
4340 static void
4341 clipAutoCollapse(void *cdata)
4343 WDock *dock = (WDock *)cdata;
4345 if (dock->type!=WM_CLIP)
4346 return;
4348 if (dock->auto_collapse) {
4349 dock->collapsed = 1;
4350 wDockHideIcons(dock);
4352 dock->auto_collapse_magic = NULL;
4356 static void
4357 clipAutoExpand(void *cdata)
4359 WDock *dock = (WDock *)cdata;
4361 if (dock->type!=WM_CLIP)
4362 return;
4364 if (dock->auto_collapse) {
4365 dock->collapsed = 0;
4366 wDockShowIcons(dock);
4368 dock->auto_expand_magic = NULL;
4372 static void
4373 clipAutoLower(void *cdata)
4375 WDock *dock = (WDock *)cdata;
4377 if (dock->type!=WM_CLIP)
4378 return;
4380 if (dock->auto_raise_lower)
4381 wDockLower(dock);
4383 dock->auto_lower_magic = NULL;
4387 static void
4388 clipAutoRaise(void *cdata)
4390 WDock *dock = (WDock *)cdata;
4392 if (dock->type!=WM_CLIP)
4393 return;
4395 if (dock->auto_raise_lower)
4396 wDockRaise(dock);
4398 if (dock->screen_ptr->flags.clip_balloon_mapped) {
4399 showClipBalloon(dock, dock->screen_ptr->current_workspace);
4402 dock->auto_raise_magic = NULL;
4406 static Bool
4407 iconCanBeOmnipresent(WAppIcon *aicon)
4409 WScreen *scr = aicon->icon->core->screen_ptr;
4410 WDock *clip;
4411 WAppIcon *btn;
4412 int i, j;
4414 for (i=0; i<scr->workspace_count; i++) {
4415 clip = scr->workspaces[i]->clip;
4417 if (clip == aicon->dock)
4418 continue;
4420 if (clip->icon_count + scr->global_icon_count >= clip->max_icons)
4421 return False; /* Clip is full in some workspace */
4423 for (j=0; j<clip->max_icons; j++) {
4424 btn = clip->icon_array[j];
4425 if(btn && btn->xindex==aicon->xindex && btn->yindex==aicon->yindex)
4426 return False;
4430 return True;
4435 wClipMakeIconOmnipresent(WAppIcon *aicon, int omnipresent)
4437 WScreen *scr = aicon->icon->core->screen_ptr;
4438 WAppIconChain *new_entry, *tmp, *tmp1;
4439 int status = WO_SUCCESS;
4441 if ((scr->dock && aicon->dock==scr->dock) || aicon==scr->clip_icon) {
4442 return WO_NOT_APPLICABLE;
4445 if (aicon->omnipresent == omnipresent)
4446 return WO_SUCCESS;
4448 if (omnipresent) {
4449 if (iconCanBeOmnipresent(aicon)) {
4450 aicon->omnipresent = 1;
4451 new_entry = wmalloc(sizeof(WAppIconChain));
4452 new_entry->aicon = aicon;
4453 new_entry->next = scr->global_icons;
4454 scr->global_icons = new_entry;
4455 scr->global_icon_count++;
4456 } else {
4457 aicon->omnipresent = 0;
4458 status = WO_FAILED;
4460 } else {
4461 aicon->omnipresent = 0;
4462 if (aicon == scr->global_icons->aicon) {
4463 tmp = scr->global_icons->next;
4464 wfree(scr->global_icons);
4465 scr->global_icons = tmp;
4466 scr->global_icon_count--;
4467 } else {
4468 tmp = scr->global_icons;
4469 while (tmp->next) {
4470 if (tmp->next->aicon == aicon) {
4471 tmp1 = tmp->next->next;
4472 wfree(tmp->next);
4473 tmp->next = tmp1;
4474 scr->global_icon_count--;
4475 break;
4477 tmp = tmp->next;
4482 wAppIconPaint(aicon);
4484 return status;