- removed configure.in. use only autoconf 2.5x fom now
[wmaker-crm.git] / src / dock.c
bloba893c4ef20250fcc8ee2ef927c84da40ac1327dc
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);
1310 tx = (ICON_SIZE/2 - WMWidthOfString(scr->clip_title_font, ws_number,
1311 nlength))/2;
1313 WMDrawString(scr->wmscreen, win, gc, scr->clip_title_font, tx,
1314 2, ws_number, nlength);
1316 wfree(ws_name);
1318 if (aicon->launching) {
1319 XFillRectangle(dpy, aicon->icon->core->window, scr->stipple_gc,
1320 0, 0, wPreferences.icon_size, wPreferences.icon_size);
1322 paintClipButtons(aicon, aicon->dock->lclip_button_pushed,
1323 aicon->dock->rclip_button_pushed);
1327 static void
1328 clipIconExpose(WObjDescriptor *desc, XEvent *event)
1330 wClipIconPaint(desc->parent);
1334 static void
1335 dockIconPaint(WAppIcon *btn)
1337 if (btn == btn->icon->core->screen_ptr->clip_icon)
1338 wClipIconPaint(btn);
1339 else
1340 wAppIconPaint(btn);
1344 static WMPropList*
1345 make_icon_state(WAppIcon *btn)
1347 WMPropList *node = NULL;
1348 WMPropList *command, *autolaunch, *lock, *name, *forced, *host;
1349 WMPropList *position, *buggy, *omnipresent;
1350 char *tmp;
1351 char buffer[64];
1353 if (btn) {
1354 if (!btn->command)
1355 command = WMCreatePLString("-");
1356 else
1357 command = WMCreatePLString(btn->command);
1359 autolaunch = btn->auto_launch ? dYes : dNo;
1361 lock = btn->lock ? dYes : dNo;
1363 tmp = EscapeWM_CLASS(btn->wm_instance, btn->wm_class);
1365 name = WMCreatePLString(tmp);
1367 wfree(tmp);
1369 forced = btn->forced_dock ? dYes : dNo;
1371 buggy = btn->buggy_app ? dYes : dNo;
1373 if (btn == btn->icon->core->screen_ptr->clip_icon)
1374 snprintf(buffer, sizeof(buffer), "%i,%i", btn->x_pos, btn->y_pos);
1375 else
1376 snprintf(buffer, sizeof(buffer), "%hi,%hi", btn->xindex, btn->yindex);
1377 position = WMCreatePLString(buffer);
1379 node = WMCreatePLDictionary(dCommand, command,
1380 dName, name,
1381 dAutoLaunch, autolaunch,
1382 dLock, lock,
1383 dForced, forced,
1384 dBuggyApplication, buggy,
1385 dPosition, position,
1386 NULL);
1387 WMReleasePropList(command);
1388 WMReleasePropList(name);
1389 WMReleasePropList(position);
1391 omnipresent = btn->omnipresent ? dYes : dNo;
1392 if (btn->dock != btn->icon->core->screen_ptr->dock &&
1393 (btn->xindex != 0 || btn->yindex != 0))
1394 WMPutInPLDictionary(node, dOmnipresent, omnipresent);
1396 #ifdef OFFIX_DND
1397 if (btn->dnd_command) {
1398 command = WMCreatePLString(btn->dnd_command);
1399 WMPutInPLDictionary(node, dDropCommand, command);
1400 WMReleasePropList(command);
1402 #endif /* OFFIX_DND */
1404 if (btn->paste_command) {
1405 command = WMCreatePLString(btn->paste_command);
1406 WMPutInPLDictionary(node, dPasteCommand, command);
1407 WMReleasePropList(command);
1410 if (btn->client_machine && btn->remote_start) {
1411 host = WMCreatePLString(btn->client_machine);
1412 WMPutInPLDictionary(node, dHost, host);
1413 WMReleasePropList(host);
1417 return node;
1421 static WMPropList*
1422 dockSaveState(WDock *dock)
1424 int i;
1425 WMPropList *icon_info;
1426 WMPropList *list=NULL, *dock_state=NULL;
1427 WMPropList *value, *key;
1428 char buffer[256];
1430 list = WMCreatePLArray(NULL);
1432 for (i=(dock->type==WM_DOCK ? 0 : 1); i<dock->max_icons; i++) {
1433 WAppIcon *btn = dock->icon_array[i];
1435 if (!btn || btn->attracted)
1436 continue;
1438 if ((icon_info = make_icon_state(dock->icon_array[i]))) {
1439 WMAddToPLArray(list, icon_info);
1440 WMReleasePropList(icon_info);
1444 dock_state = WMCreatePLDictionary(dApplications, list,
1445 NULL);
1447 if (dock->type == WM_DOCK) {
1448 snprintf(buffer, sizeof(buffer), "Applications%i", dock->screen_ptr->scr_height);
1449 key = WMCreatePLString(buffer);
1450 WMPutInPLDictionary(dock_state, key, list);
1451 WMReleasePropList(key);
1454 snprintf(buffer, sizeof(buffer), "%i,%i", (dock->on_right_side ? -ICON_SIZE : 0),
1455 dock->y_pos);
1456 value = WMCreatePLString(buffer);
1457 WMPutInPLDictionary(dock_state, dPosition, value);
1458 WMReleasePropList(value);
1460 WMReleasePropList(list);
1463 value = (dock->lowered ? dYes : dNo);
1464 WMPutInPLDictionary(dock_state, dLowered, value);
1466 if (dock->type == WM_CLIP) {
1467 value = (dock->collapsed ? dYes : dNo);
1468 WMPutInPLDictionary(dock_state, dCollapsed, value);
1470 value = (dock->auto_collapse ? dYes : dNo);
1471 WMPutInPLDictionary(dock_state, dAutoCollapse, value);
1473 value = (dock->auto_raise_lower ? dYes : dNo);
1474 WMPutInPLDictionary(dock_state, dAutoRaiseLower, value);
1476 value = (dock->attract_icons ? dYes : dNo);
1477 WMPutInPLDictionary(dock_state, dAutoAttractIcons, value);
1480 return dock_state;
1484 void
1485 wDockSaveState(WScreen *scr, WMPropList *old_state)
1487 WMPropList *dock_state;
1488 WMPropList *keys;
1490 dock_state = dockSaveState(scr->dock);
1493 * Copy saved states of docks with different sizes.
1495 if (old_state) {
1496 int i;
1497 WMPropList *tmp;
1499 keys = WMGetPLDictionaryKeys(old_state);
1500 for (i = 0; i < WMGetPropListItemCount(keys); i++) {
1501 tmp = WMGetFromPLArray(keys, i);
1503 if (strncasecmp(WMGetFromPLString(tmp), "applications", 12) == 0
1504 && !WMGetFromPLDictionary(dock_state, tmp)) {
1506 WMPutInPLDictionary(dock_state,
1507 tmp,
1508 WMGetFromPLDictionary(old_state, tmp));
1511 WMReleasePropList(keys);
1515 WMPutInPLDictionary(scr->session_state, dDock, dock_state);
1517 WMReleasePropList(dock_state);
1521 void
1522 wClipSaveState(WScreen *scr)
1524 WMPropList *clip_state;
1526 clip_state = make_icon_state(scr->clip_icon);
1528 WMPutInPLDictionary(scr->session_state, dClip, clip_state);
1530 WMReleasePropList(clip_state);
1534 WMPropList*
1535 wClipSaveWorkspaceState(WScreen *scr, int workspace)
1537 return dockSaveState(scr->workspaces[workspace]->clip);
1541 static Bool
1542 getBooleanDockValue(WMPropList *value, WMPropList *key)
1544 if (value) {
1545 if (WMIsPLString(value)) {
1546 if (strcasecmp(WMGetFromPLString(value), "YES")==0)
1547 return True;
1548 } else {
1549 wwarning(_("bad value in docked icon state info %s"),
1550 WMGetFromPLString(key));
1553 return False;
1557 static WAppIcon*
1558 restore_icon_state(WScreen *scr, WMPropList *info, int type, int index)
1560 WAppIcon *aicon;
1561 WMPropList *cmd, *value;
1564 cmd = WMGetFromPLDictionary(info, dCommand);
1565 if (!cmd || !WMIsPLString(cmd)) {
1566 return NULL;
1569 /* parse window name */
1570 value = WMGetFromPLDictionary(info, dName);
1571 if (!value)
1572 return NULL;
1575 char *wclass, *winstance;
1576 char *command;
1578 ParseWindowName(value, &winstance, &wclass, "dock");
1580 if (!winstance && !wclass) {
1581 return NULL;
1584 /* get commands */
1586 if (cmd)
1587 command = wstrdup(WMGetFromPLString(cmd));
1588 else
1589 command = NULL;
1591 if (!command || strcmp(command, "-")==0) {
1592 if (command)
1593 wfree(command);
1594 if (wclass)
1595 wfree(wclass);
1596 if (winstance)
1597 wfree(winstance);
1599 return NULL;
1602 aicon = wAppIconCreateForDock(scr, command, winstance, wclass,
1603 TILE_NORMAL);
1604 if (wclass)
1605 wfree(wclass);
1606 if (winstance)
1607 wfree(winstance);
1608 if (command)
1609 wfree(command);
1612 aicon->icon->core->descriptor.handle_mousedown = iconMouseDown;
1613 if (type == WM_CLIP) {
1614 aicon->icon->core->descriptor.handle_enternotify = clipEnterNotify;
1615 aicon->icon->core->descriptor.handle_leavenotify = clipLeaveNotify;
1617 aicon->icon->core->descriptor.parent_type = WCLASS_DOCK_ICON;
1618 aicon->icon->core->descriptor.parent = aicon;
1621 #ifdef OFFIX_DND
1622 cmd = WMGetFromPLDictionary(info, dDropCommand);
1623 if (cmd)
1624 aicon->dnd_command = wstrdup(WMGetFromPLString(cmd));
1625 #endif
1627 cmd = WMGetFromPLDictionary(info, dPasteCommand);
1628 if (cmd)
1629 aicon->paste_command = wstrdup(WMGetFromPLString(cmd));
1631 /* check auto launch */
1632 value = WMGetFromPLDictionary(info, dAutoLaunch);
1634 aicon->auto_launch = getBooleanDockValue(value, dAutoLaunch);
1636 /* check lock */
1637 value = WMGetFromPLDictionary(info, dLock);
1639 aicon->lock = getBooleanDockValue(value, dLock);
1641 /* check if it wasn't normally docked */
1642 value = WMGetFromPLDictionary(info, dForced);
1644 aicon->forced_dock = getBooleanDockValue(value, dForced);
1646 /* check if we can rely on the stuff in the app */
1647 value = WMGetFromPLDictionary(info, dBuggyApplication);
1649 aicon->buggy_app = getBooleanDockValue(value, dBuggyApplication);
1651 /* get position in the dock */
1652 value = WMGetFromPLDictionary(info, dPosition);
1653 if (value && WMIsPLString(value)) {
1654 if (sscanf(WMGetFromPLString(value), "%hi,%hi", &aicon->xindex,
1655 &aicon->yindex)!=2)
1656 wwarning(_("bad value in docked icon state info %s"),
1657 WMGetFromPLString(dPosition));
1659 /* check position sanity */
1660 /* incomplete section! */
1661 if (type == WM_DOCK) {
1662 aicon->xindex = 0;
1663 if (aicon->yindex < 0)
1664 wwarning(_("bad value in docked icon position %i,%i"),
1665 aicon->xindex, aicon->yindex);
1667 } else {
1668 aicon->yindex = index;
1669 aicon->xindex = 0;
1672 /* check if icon is omnipresent */
1673 value = WMGetFromPLDictionary(info, dOmnipresent);
1675 aicon->omnipresent = getBooleanDockValue(value, dOmnipresent);
1677 aicon->running = 0;
1678 aicon->docked = 1;
1680 return aicon;
1684 #define COMPLAIN(key) wwarning(_("bad value in dock state info:%s"), key)
1687 WAppIcon*
1688 wClipRestoreState(WScreen *scr, WMPropList *clip_state)
1690 WAppIcon *icon;
1691 WMPropList *value;
1694 icon = mainIconCreate(scr, WM_CLIP);
1696 if (!clip_state)
1697 return icon;
1698 else
1699 WMRetainPropList(clip_state);
1701 /* restore position */
1703 value = WMGetFromPLDictionary(clip_state, dPosition);
1705 if (value) {
1706 if (!WMIsPLString(value))
1707 COMPLAIN("Position");
1708 else {
1709 if (sscanf(WMGetFromPLString(value), "%i,%i", &icon->x_pos,
1710 &icon->y_pos)!=2)
1711 COMPLAIN("Position");
1713 /* check position sanity */
1714 if (icon->y_pos < 0)
1715 icon->y_pos = 0;
1716 else if (icon->y_pos > scr->scr_height-ICON_SIZE)
1717 icon->y_pos = scr->scr_height-ICON_SIZE;
1719 if (icon->x_pos < 0)
1720 icon->x_pos = 0;
1721 else if (icon->x_pos > scr->scr_width-ICON_SIZE)
1722 icon->x_pos = scr->scr_width-ICON_SIZE;
1726 #ifdef OFFIX_DND
1727 value = WMGetFromPLDictionary(clip_state, dDropCommand);
1728 if (value && WMIsPLString(value))
1729 icon->dnd_command = wstrdup(WMGetFromPLString(value));
1730 #endif
1732 value = WMGetFromPLDictionary(clip_state, dPasteCommand);
1733 if (value && WMIsPLString(value))
1734 icon->paste_command = wstrdup(WMGetFromPLString(value));
1736 WMReleasePropList(clip_state);
1738 return icon;
1742 WDock*
1743 wDockRestoreState(WScreen *scr, WMPropList *dock_state, int type)
1745 WDock *dock;
1746 WMPropList *apps;
1747 WMPropList *value;
1748 WAppIcon *aicon, *old_top;
1749 int count, i;
1752 dock = wDockCreate(scr, type);
1754 if (!dock_state)
1755 return dock;
1757 if (dock_state)
1758 WMRetainPropList(dock_state);
1761 /* restore position */
1763 value = WMGetFromPLDictionary(dock_state, dPosition);
1765 if (value) {
1766 if (!WMIsPLString(value))
1767 COMPLAIN("Position");
1768 else {
1769 if (sscanf(WMGetFromPLString(value), "%i,%i", &dock->x_pos,
1770 &dock->y_pos)!=2)
1771 COMPLAIN("Position");
1773 /* check position sanity */
1774 if (dock->y_pos < 0)
1775 dock->y_pos = 0;
1776 else if (dock->y_pos > scr->scr_height-ICON_SIZE)
1777 dock->y_pos = scr->scr_height - ICON_SIZE;
1779 /* This is no more needed. ??? */
1780 if (type == WM_CLIP) {
1781 if (dock->x_pos < 0)
1782 dock->x_pos = 0;
1783 else if (dock->x_pos > scr->scr_width-ICON_SIZE)
1784 dock->x_pos = scr->scr_width-ICON_SIZE;
1785 } else {
1786 if (dock->x_pos >= 0) {
1787 dock->x_pos = DOCK_EXTRA_SPACE;
1788 dock->on_right_side = 0;
1789 } else {
1790 dock->x_pos = scr->scr_width - DOCK_EXTRA_SPACE - ICON_SIZE;
1791 dock->on_right_side = 1;
1797 /* restore lowered/raised state */
1799 dock->lowered = 0;
1801 value = WMGetFromPLDictionary(dock_state, dLowered);
1803 if (value) {
1804 if (!WMIsPLString(value))
1805 COMPLAIN("Lowered");
1806 else {
1807 if (strcasecmp(WMGetFromPLString(value), "YES")==0)
1808 dock->lowered = 1;
1813 /* restore collapsed state */
1815 dock->collapsed = 0;
1817 value = WMGetFromPLDictionary(dock_state, dCollapsed);
1819 if (value) {
1820 if (!WMIsPLString(value))
1821 COMPLAIN("Collapsed");
1822 else {
1823 if (strcasecmp(WMGetFromPLString(value), "YES")==0)
1824 dock->collapsed = 1;
1829 /* restore auto-collapsed state */
1831 value = WMGetFromPLDictionary(dock_state, dAutoCollapse);
1833 if (value) {
1834 if (!WMIsPLString(value))
1835 COMPLAIN("AutoCollapse");
1836 else {
1837 if (strcasecmp(WMGetFromPLString(value), "YES")==0) {
1838 dock->auto_collapse = 1;
1839 dock->collapsed = 1;
1845 /* restore auto-raise/lower state */
1847 value = WMGetFromPLDictionary(dock_state, dAutoRaiseLower);
1849 if (value) {
1850 if (!WMIsPLString(value))
1851 COMPLAIN("AutoRaiseLower");
1852 else {
1853 if (strcasecmp(WMGetFromPLString(value), "YES")==0) {
1854 dock->auto_raise_lower = 1;
1859 /* restore attract icons state */
1861 dock->attract_icons = 0;
1863 value = WMGetFromPLDictionary(dock_state, dAutoAttractIcons);
1865 if (value) {
1866 if (!WMIsPLString(value))
1867 COMPLAIN("AutoAttractIcons");
1868 else {
1869 if (strcasecmp(WMGetFromPLString(value), "YES")==0)
1870 dock->attract_icons = 1;
1875 /* application list */
1878 WMPropList *tmp;
1879 char buffer[64];
1882 * When saving, it saves the dock state in
1883 * Applications and Applicationsnnn
1885 * When loading, it will first try Applicationsnnn.
1886 * If it does not exist, use Applications as default.
1889 snprintf(buffer, sizeof(buffer), "Applications%i", scr->scr_height);
1891 tmp = WMCreatePLString(buffer);
1892 apps = WMGetFromPLDictionary(dock_state, tmp);
1893 WMReleasePropList(tmp);
1895 if (!apps) {
1896 apps = WMGetFromPLDictionary(dock_state, dApplications);
1900 if (!apps) {
1901 goto finish;
1904 count = WMGetPropListItemCount(apps);
1906 if (count==0)
1907 goto finish;
1909 old_top = dock->icon_array[0];
1911 /* dock->icon_count is set to 1 when dock is created.
1912 * Since Clip is already restored, we want to keep it so for clip,
1913 * but for dock we may change the default top tile, so we set it to 0.
1915 if (type == WM_DOCK)
1916 dock->icon_count = 0;
1918 for (i=0; i<count; i++) {
1919 if (dock->icon_count >= dock->max_icons) {
1920 wwarning(_("there are too many icons stored in dock. Ignoring what doesn't fit"));
1921 break;
1924 value = WMGetFromPLArray(apps, i);
1925 aicon = restore_icon_state(scr, value, type, dock->icon_count);
1927 dock->icon_array[dock->icon_count] = aicon;
1929 if (aicon) {
1930 aicon->dock = dock;
1931 aicon->x_pos = dock->x_pos + (aicon->xindex*ICON_SIZE);
1932 aicon->y_pos = dock->y_pos + (aicon->yindex*ICON_SIZE);
1934 if (dock->lowered)
1935 ChangeStackingLevel(aicon->icon->core, WMNormalLevel);
1936 else
1937 ChangeStackingLevel(aicon->icon->core, WMDockLevel);
1939 wCoreConfigure(aicon->icon->core, aicon->x_pos, aicon->y_pos,
1940 0, 0);
1942 if (!dock->collapsed)
1943 XMapWindow(dpy, aicon->icon->core->window);
1944 wRaiseFrame(aicon->icon->core);
1946 dock->icon_count++;
1947 } else if (dock->icon_count==0 && type==WM_DOCK)
1948 dock->icon_count++;
1951 /* if the first icon is not defined, use the default */
1952 if (dock->icon_array[0]==NULL) {
1953 /* update default icon */
1954 old_top->x_pos = dock->x_pos;
1955 old_top->y_pos = dock->y_pos;
1956 if (dock->lowered)
1957 ChangeStackingLevel(old_top->icon->core, WMNormalLevel);
1958 else
1959 ChangeStackingLevel(old_top->icon->core, WMDockLevel);
1960 dock->icon_array[0] = old_top;
1961 XMoveWindow(dpy, old_top->icon->core->window, dock->x_pos, dock->y_pos);
1962 /* we don't need to increment dock->icon_count here because it was
1963 * incremented in the loop above.
1965 } else if (old_top!=dock->icon_array[0]) {
1966 if (old_top == scr->clip_icon)
1967 scr->clip_icon = dock->icon_array[0];
1968 wAppIconDestroy(old_top);
1971 finish:
1972 if (dock_state)
1973 WMReleasePropList(dock_state);
1975 return dock;
1980 void
1981 wDockLaunchWithState(WDock *dock, WAppIcon *btn, WSavedState *state)
1983 if (btn && btn->command && !btn->running && !btn->launching) {
1985 btn->drop_launch = 0;
1986 btn->paste_launch = 0;
1988 btn->pid = execCommand(btn, btn->command, state);
1990 if (btn->pid>0) {
1991 if (!btn->forced_dock && !btn->buggy_app) {
1992 btn->launching = 1;
1993 dockIconPaint(btn);
1996 } else {
1997 wfree(state);
2002 void
2003 wDockDoAutoLaunch(WDock *dock, int workspace)
2005 WAppIcon *btn;
2006 WSavedState *state;
2007 int i;
2009 for (i = 0; i < dock->max_icons; i++) {
2010 btn = dock->icon_array[i];
2011 if (!btn || !btn->auto_launch)
2012 continue;
2014 state = wmalloc(sizeof(WSavedState));
2015 memset(state, 0, sizeof(WSavedState));
2016 state->workspace = workspace;
2017 /* TODO: this is klugy and is very difficult to understand
2018 * what's going on. Try to clean up */
2019 wDockLaunchWithState(dock, btn, state);
2024 #ifdef OFFIX_DND
2025 static WDock*
2026 findDock(WScreen *scr, XEvent *event, int *icon_pos)
2028 WDock *dock;
2029 int i;
2031 *icon_pos = -1;
2032 if ((dock = scr->dock)!=NULL) {
2033 for (i=0; i<dock->max_icons; i++) {
2034 if (dock->icon_array[i]
2035 && dock->icon_array[i]->icon->core->window==event->xclient.window) {
2036 *icon_pos = i;
2037 break;
2041 if (*icon_pos<0 && (dock = scr->workspaces[scr->current_workspace]->clip)!=NULL) {
2042 for (i=0; i<dock->max_icons; i++) {
2043 if (dock->icon_array[i]
2044 && dock->icon_array[i]->icon->core->window==event->xclient.window) {
2045 *icon_pos = i;
2046 break;
2050 if(*icon_pos>=0)
2051 return dock;
2052 return NULL;
2057 wDockReceiveDNDDrop(WScreen *scr, XEvent *event)
2059 WDock *dock;
2060 WAppIcon *btn;
2061 int icon_pos;
2063 dock = findDock(scr, event, &icon_pos);
2064 if (!dock)
2065 return False;
2068 * Return True if the drop was on an application icon window.
2069 * In this case, let the ClientMessage handler redirect the
2070 * message to the app.
2072 if (dock->icon_array[icon_pos]->icon->icon_win!=None)
2073 return True;
2075 if (dock->icon_array[icon_pos]->dnd_command!=NULL) {
2076 scr->flags.dnd_data_convertion_status = 0;
2078 btn = dock->icon_array[icon_pos];
2080 if (!btn->forced_dock) {
2081 btn->relaunching = btn->running;
2082 btn->running = 1;
2084 if (btn->wm_instance || btn->wm_class) {
2085 WWindowAttributes attr;
2086 memset(&attr, 0, sizeof(WWindowAttributes));
2087 wDefaultFillAttributes(btn->icon->core->screen_ptr,
2088 btn->wm_instance,
2089 btn->wm_class, &attr, NULL, True);
2091 if (!attr.no_appicon)
2092 btn->launching = 1;
2093 else
2094 btn->running = 0;
2097 btn->paste_launch = 0;
2098 btn->drop_launch = 1;
2099 scr->last_dock = dock;
2100 btn->pid = execCommand(btn, btn->dnd_command, NULL);
2101 if (btn->pid>0) {
2102 dockIconPaint(btn);
2103 } else {
2104 btn->launching = 0;
2105 if (!btn->relaunching) {
2106 btn->running = 0;
2110 return False;
2112 #endif /* OFFIX_DND */
2116 Bool
2117 wDockAttachIcon(WDock *dock, WAppIcon *icon, int x, int y)
2119 WWindow *wwin;
2120 char **argv;
2121 int argc;
2122 int index;
2124 wwin = icon->icon->owner;
2125 if (icon->command==NULL) {
2126 icon->editing = 0;
2127 if (XGetCommand(dpy, wwin->client_win, &argv, &argc) && argc>0) {
2129 icon->command = wtokenjoin(argv, argc);
2130 XFreeStringList(argv);
2131 } else {
2132 char *command=NULL;
2134 /* icon->forced_dock = 1;*/
2135 if (dock->type!=WM_CLIP || !icon->attracted) {
2136 icon->editing = 1;
2137 if (wInputDialog(dock->screen_ptr, _("Dock Icon"),
2138 _("Type the command used to launch the application"),
2139 &command)) {
2140 if (command && (command[0]==0 ||
2141 (command[0]=='-' && command[1]==0))) {
2142 wfree(command);
2143 command = NULL;
2145 icon->command = command;
2146 icon->editing = 0;
2147 } else {
2148 icon->editing = 0;
2149 if (command)
2150 wfree(command);
2151 /* If the target is the dock, reject the icon. If
2152 * the target is the clip, make it an attracted icon
2154 if (dock->type==WM_CLIP) {
2155 icon->attracted = 1;
2156 if (!icon->icon->shadowed) {
2157 icon->icon->shadowed = 1;
2158 icon->icon->force_paint = 1;
2160 } else {
2161 return False;
2166 } else {
2167 icon->editing = 0;
2170 for (index=1; index<dock->max_icons; index++)
2171 if (dock->icon_array[index] == NULL)
2172 break;
2173 /* if (index == dock->max_icons)
2174 return; */
2176 assert(index < dock->max_icons);
2178 dock->icon_array[index] = icon;
2179 icon->yindex = y;
2180 icon->xindex = x;
2182 icon->omnipresent = 0;
2184 icon->x_pos = dock->x_pos + x*ICON_SIZE;
2185 icon->y_pos = dock->y_pos + y*ICON_SIZE;
2187 dock->icon_count++;
2189 icon->running = 1;
2190 icon->launching = 0;
2191 icon->docked = 1;
2192 icon->dock = dock;
2193 icon->icon->core->descriptor.handle_mousedown = iconMouseDown;
2194 if (dock->type == WM_CLIP) {
2195 icon->icon->core->descriptor.handle_enternotify = clipEnterNotify;
2196 icon->icon->core->descriptor.handle_leavenotify = clipLeaveNotify;
2198 icon->icon->core->descriptor.parent_type = WCLASS_DOCK_ICON;
2199 icon->icon->core->descriptor.parent = icon;
2201 MoveInStackListUnder(dock->icon_array[index-1]->icon->core,
2202 icon->icon->core);
2203 wAppIconMove(icon, icon->x_pos, icon->y_pos);
2204 wAppIconPaint(icon);
2206 if (wPreferences.auto_arrange_icons)
2207 wArrangeIcons(dock->screen_ptr, True);
2209 #ifdef OFFIX_DND
2210 if (icon->command && !icon->dnd_command) {
2211 int len = strlen(icon->command)+8;
2212 icon->dnd_command = wmalloc(len);
2213 snprintf(icon->dnd_command, len, "%s %%d", icon->command);
2215 #endif
2217 if (icon->command && !icon->paste_command) {
2218 int len = strlen(icon->command)+8;
2219 icon->paste_command = wmalloc(len);
2220 snprintf(icon->paste_command, len, "%s %%s", icon->command);
2223 return True;
2227 void
2228 reattachIcon(WDock *dock, WAppIcon *icon, int x, int y)
2230 int index;
2232 for(index=1; index<dock->max_icons; index++) {
2233 if(dock->icon_array[index] == icon)
2234 break;
2236 assert(index < dock->max_icons);
2238 icon->yindex = y;
2239 icon->xindex = x;
2241 icon->x_pos = dock->x_pos + x*ICON_SIZE;
2242 icon->y_pos = dock->y_pos + y*ICON_SIZE;
2246 Bool
2247 moveIconBetweenDocks(WDock *src, WDock *dest, WAppIcon *icon, int x, int y)
2249 WWindow *wwin;
2250 char **argv;
2251 int argc;
2252 int index;
2254 if (src == dest)
2255 return True; /* No move needed, we're already there */
2257 if (dest == NULL)
2258 return False;
2260 wwin = icon->icon->owner;
2263 * For the moment we can't do this if we move icons in Clip from one
2264 * workspace to other, because if we move two or more icons without
2265 * command, the dialog box will not be able to tell us to which of the
2266 * moved icons it applies. -Dan
2268 if ((dest->type==WM_DOCK /*|| dest->keep_attracted*/) && icon->command==NULL) {
2269 if (XGetCommand(dpy, wwin->client_win, &argv, &argc) && argc>0) {
2271 icon->command = wtokenjoin(argv, argc);
2272 XFreeStringList(argv);
2273 } else {
2274 char *command=NULL;
2276 icon->editing = 1;
2277 /* icon->forced_dock = 1;*/
2278 if (wInputDialog(src->screen_ptr, _("Dock Icon"),
2279 _("Type the command used to launch the application"),
2280 &command)) {
2281 if (command && (command[0]==0 ||
2282 (command[0]=='-' && command[1]==0))) {
2283 wfree(command);
2284 command = NULL;
2286 icon->command = command;
2287 } else {
2288 icon->editing = 0;
2289 if (command)
2290 wfree(command);
2291 return False;
2293 icon->editing = 0;
2297 if (dest->type == WM_DOCK)
2298 wClipMakeIconOmnipresent(icon, False);
2300 for(index=1; index<src->max_icons; index++) {
2301 if(src->icon_array[index] == icon)
2302 break;
2304 assert(index < src->max_icons);
2306 src->icon_array[index] = NULL;
2307 src->icon_count--;
2309 for(index=1; index<dest->max_icons; index++) {
2310 if(dest->icon_array[index] == NULL)
2311 break;
2313 /* if (index == dest->max_icons)
2314 return; */
2316 assert(index < dest->max_icons);
2318 dest->icon_array[index] = icon;
2319 icon->dock = dest;
2321 /* deselect the icon */
2322 if (icon->icon->selected)
2323 wIconSelect(icon->icon);
2325 if (dest->type == WM_DOCK) {
2326 icon->icon->core->descriptor.handle_enternotify = NULL;
2327 icon->icon->core->descriptor.handle_leavenotify = NULL;
2328 } else {
2329 icon->icon->core->descriptor.handle_enternotify = clipEnterNotify;
2330 icon->icon->core->descriptor.handle_leavenotify = clipLeaveNotify;
2333 /* set it to be kept when moving to dock.
2334 * Unless the icon does not have a command set
2336 if (icon->command && dest->type==WM_DOCK) {
2337 icon->attracted = 0;
2338 if (icon->icon->shadowed) {
2339 icon->icon->shadowed = 0;
2340 icon->icon->force_paint = 1;
2344 if (src->auto_collapse || src->auto_raise_lower)
2345 clipLeave(src);
2347 icon->yindex = y;
2348 icon->xindex = x;
2350 icon->x_pos = dest->x_pos + x*ICON_SIZE;
2351 icon->y_pos = dest->y_pos + y*ICON_SIZE;
2353 dest->icon_count++;
2355 MoveInStackListUnder(dest->icon_array[index-1]->icon->core,
2356 icon->icon->core);
2357 wAppIconPaint(icon);
2359 return True;
2363 void
2364 wDockDetach(WDock *dock, WAppIcon *icon)
2366 int index;
2368 /* make the settings panel be closed */
2369 if (icon->panel) {
2370 DestroyDockAppSettingsPanel(icon->panel);
2373 /* This must be called before icon->dock is set to NULL.
2374 * Don't move it. -Dan
2376 wClipMakeIconOmnipresent(icon, False);
2378 icon->docked = 0;
2379 icon->dock = NULL;
2380 icon->attracted = 0;
2381 icon->auto_launch = 0;
2382 if (icon->icon->shadowed) {
2383 icon->icon->shadowed = 0;
2384 icon->icon->force_paint = 1;
2387 /* deselect the icon */
2388 if (icon->icon->selected)
2389 wIconSelect(icon->icon);
2391 if (icon->command) {
2392 wfree(icon->command);
2393 icon->command = NULL;
2395 #ifdef OFFIX_DND
2396 if (icon->dnd_command) {
2397 wfree(icon->dnd_command);
2398 icon->dnd_command = NULL;
2400 #endif
2401 if (icon->paste_command) {
2402 wfree(icon->paste_command);
2403 icon->paste_command = NULL;
2406 for (index=1; index<dock->max_icons; index++)
2407 if (dock->icon_array[index] == icon)
2408 break;
2409 assert(index < dock->max_icons);
2410 dock->icon_array[index] = NULL;
2411 icon->yindex = -1;
2412 icon->xindex = -1;
2414 dock->icon_count--;
2416 /* if the dock is not attached to an application or
2417 * the the application did not set the approriate hints yet,
2418 * destroy the icon */
2419 if (!icon->running || !wApplicationOf(icon->main_window))
2420 wAppIconDestroy(icon);
2421 else {
2422 icon->icon->core->descriptor.handle_mousedown = appIconMouseDown;
2423 icon->icon->core->descriptor.handle_enternotify = NULL;
2424 icon->icon->core->descriptor.handle_leavenotify = NULL;
2425 icon->icon->core->descriptor.parent_type = WCLASS_APPICON;
2426 icon->icon->core->descriptor.parent = icon;
2428 ChangeStackingLevel(icon->icon->core, NORMAL_ICON_LEVEL);
2430 wAppIconPaint(icon);
2431 if (wPreferences.auto_arrange_icons) {
2432 wArrangeIcons(dock->screen_ptr, True);
2435 if (dock->auto_collapse || dock->auto_raise_lower)
2436 clipLeave(dock);
2441 * returns the closest Dock slot index for the passed
2442 * coordinates.
2444 * Returns False if icon can't be docked.
2446 * Note: this function should NEVER alter ret_x or ret_y, unless it will
2447 * return True. -Dan
2449 Bool
2450 wDockSnapIcon(WDock *dock, WAppIcon *icon, int req_x, int req_y,
2451 int *ret_x, int *ret_y, int redocking)
2453 WScreen *scr = dock->screen_ptr;
2454 int dx, dy;
2455 int ex_x, ex_y;
2456 int i, offset = ICON_SIZE/2;
2457 WAppIcon *aicon = NULL;
2458 WAppIcon *nicon = NULL;
2459 int max_y_icons, max_x_icons;
2461 max_x_icons = scr->scr_width/ICON_SIZE;
2462 max_y_icons = scr->scr_height/ICON_SIZE-1;
2464 if (wPreferences.flags.noupdates)
2465 return False;
2467 dx = dock->x_pos;
2468 dy = dock->y_pos;
2470 /* if the dock is full */
2471 if (!redocking &&
2472 (dock->icon_count >= dock->max_icons)) {
2473 return False;
2476 /* exact position */
2477 if (req_y < dy)
2478 ex_y = (req_y - offset - dy)/ICON_SIZE;
2479 else
2480 ex_y = (req_y + offset - dy)/ICON_SIZE;
2482 if (req_x < dx)
2483 ex_x = (req_x - offset - dx)/ICON_SIZE;
2484 else
2485 ex_x = (req_x + offset - dx)/ICON_SIZE;
2487 /* check if the icon is outside the screen boundaries */
2488 if (dx + ex_x*ICON_SIZE < -ICON_SIZE+2 ||
2489 dx + ex_x*ICON_SIZE >= scr->scr_width-1 ||
2490 dy + ex_y*ICON_SIZE < -ICON_SIZE+2 ||
2491 dy + ex_y*ICON_SIZE >= scr->scr_height-1)
2492 return False;
2494 if (dock->type == WM_DOCK) {
2495 if (icon->dock != dock && ex_x != 0)
2496 return False;
2498 aicon = NULL;
2499 for (i=0; i<dock->max_icons; i++) {
2500 nicon = dock->icon_array[i];
2501 if (nicon && nicon->yindex == ex_y) {
2502 aicon = nicon;
2503 break;
2507 if (redocking) {
2508 int sig, done, closest;
2510 /* Possible cases when redocking:
2512 * icon dragged out of range of any slot -> false
2513 * icon dragged to range of free slot
2514 * icon dragged to range of same slot
2515 * icon dragged to range of different icon
2517 if (abs(ex_x) > DOCK_DETTACH_THRESHOLD)
2518 return False;
2520 if (ex_y>=0 && ex_y<=max_y_icons && (aicon==icon || !aicon)) {
2521 *ret_x = 0;
2522 *ret_y = ex_y;
2523 return True;
2526 /* start looking at the upper slot or lower? */
2527 if (ex_y*ICON_SIZE < (req_y + offset - dy))
2528 sig = 1;
2529 else
2530 sig = -1;
2532 closest = -1;
2533 done = 0;
2534 /* look for closest free slot */
2535 for (i=0; i<(DOCK_DETTACH_THRESHOLD+1)*2 && !done; i++) {
2536 int j;
2538 done = 1;
2539 closest = sig*(i/2) + ex_y;
2540 /* check if this slot is used */
2541 if (closest >= 0) {
2542 for (j = 0; j<dock->max_icons; j++) {
2543 if (dock->icon_array[j]
2544 && dock->icon_array[j]->yindex==closest) {
2545 /* slot is used by someone else */
2546 if (dock->icon_array[j]!=icon)
2547 done = 0;
2548 break;
2552 sig = -sig;
2554 if (done && closest >= 0 && closest <= max_y_icons &&
2555 ((ex_y >= closest && ex_y - closest < DOCK_DETTACH_THRESHOLD+1)
2557 (ex_y < closest && closest - ex_y <= DOCK_DETTACH_THRESHOLD+1))) {
2558 *ret_x = 0;
2559 *ret_y = closest;
2560 return True;
2562 } else { /* !redocking */
2564 /* if slot is free and the icon is close enough, return it */
2565 if (!aicon && ex_x == 0 && ex_y >= 0 && ex_y <= max_y_icons) {
2566 *ret_x = 0;
2567 *ret_y = ex_y;
2568 return True;
2571 } else { /* CLIP */
2572 int neighbours = 0;
2573 int start, stop, k;
2575 start = icon->omnipresent ? 0 : scr->current_workspace;
2576 stop = icon->omnipresent ? scr->workspace_count : start+1;
2578 aicon = NULL;
2579 for (k=start; k<stop; k++) {
2580 WDock *tmp = scr->workspaces[k]->clip;
2581 if (!tmp)
2582 continue;
2583 for (i=0; i<tmp->max_icons; i++) {
2584 nicon = tmp->icon_array[i];
2585 if (nicon && nicon->xindex == ex_x && nicon->yindex == ex_y) {
2586 aicon = nicon;
2587 break;
2590 if (aicon)
2591 break;
2593 for (k=start; k<stop; k++) {
2594 WDock *tmp = scr->workspaces[k]->clip;
2595 if (!tmp)
2596 continue;
2597 for (i=0; i<tmp->max_icons; i++) {
2598 nicon = tmp->icon_array[i];
2599 if (nicon && nicon != icon && /* Icon can't be it's own neighbour */
2600 (abs(nicon->xindex - ex_x) <= CLIP_ATTACH_VICINITY &&
2601 abs(nicon->yindex - ex_y) <= CLIP_ATTACH_VICINITY)) {
2602 neighbours = 1;
2603 break;
2606 if (neighbours)
2607 break;
2610 if (neighbours && (aicon==NULL || (redocking && aicon == icon))) {
2611 *ret_x = ex_x;
2612 *ret_y = ex_y;
2613 return True;
2616 return False;
2620 #define ON_SCREEN(x, y, sx, ex, sy, ey) \
2621 ((((x)+ICON_SIZE/2) >= (sx)) && (((y)+ICON_SIZE/2) >= (sy)) && \
2622 (((x) + (ICON_SIZE/2)) <= (ex)) && (((y) + (ICON_SIZE/2)) <= ey))
2626 * returns true if it can find a free slot in the dock,
2627 * in which case it changes x_pos and y_pos accordingly.
2628 * Else returns false.
2630 Bool
2631 wDockFindFreeSlot(WDock *dock, int *x_pos, int *y_pos)
2633 WScreen *scr = dock->screen_ptr;
2634 WAppIcon *btn;
2635 WAppIconChain *chain;
2636 unsigned char *slot_map;
2637 int mwidth;
2638 int r;
2639 int x, y;
2640 int i, done = False;
2641 int corner;
2642 int sx=0, sy=0, ex=scr->scr_width, ey=scr->scr_height;
2643 int extra_count=0;
2645 if (dock->type == WM_CLIP &&
2646 dock != scr->workspaces[scr->current_workspace]->clip)
2647 extra_count = scr->global_icon_count;
2649 /* if the dock is full */
2650 if (dock->icon_count+extra_count >= dock->max_icons) {
2651 return False;
2654 if (!wPreferences.flags.nodock && scr->dock) {
2655 if (scr->dock->on_right_side)
2656 ex -= ICON_SIZE + DOCK_EXTRA_SPACE;
2657 else
2658 sx += ICON_SIZE + DOCK_EXTRA_SPACE;
2661 if (ex < dock->x_pos)
2662 ex = dock->x_pos;
2663 if (sx > dock->x_pos+ICON_SIZE)
2664 sx = dock->x_pos+ICON_SIZE;
2665 #define C_NONE 0
2666 #define C_NW 1
2667 #define C_NE 2
2668 #define C_SW 3
2669 #define C_SE 4
2671 /* check if clip is in a corner */
2672 if (dock->type==WM_CLIP) {
2673 if (dock->x_pos < 1 && dock->y_pos < 1)
2674 corner = C_NE;
2675 else if (dock->x_pos < 1 && dock->y_pos >= (ey-ICON_SIZE))
2676 corner = C_SE;
2677 else if (dock->x_pos >= (ex-ICON_SIZE)&& dock->y_pos >= (ey-ICON_SIZE))
2678 corner = C_SW;
2679 else if (dock->x_pos >= (ex-ICON_SIZE) && dock->y_pos < 1)
2680 corner = C_NW;
2681 else
2682 corner = C_NONE;
2683 } else
2684 corner = C_NONE;
2686 /* If the clip is in the corner, use only slots that are in the border
2687 * of the screen */
2688 if (corner!=C_NONE) {
2689 char *hmap, *vmap;
2690 int hcount, vcount;
2692 hcount = WMIN(dock->max_icons, scr->scr_width/ICON_SIZE);
2693 vcount = WMIN(dock->max_icons, scr->scr_height/ICON_SIZE);
2694 hmap = wmalloc(hcount+1);
2695 memset(hmap, 0, hcount+1);
2696 vmap = wmalloc(vcount+1);
2697 memset(vmap, 0, vcount+1);
2699 /* mark used positions */
2700 switch (corner) {
2701 case C_NE:
2702 for (i=0; i<dock->max_icons; i++) {
2703 btn = dock->icon_array[i];
2704 if (!btn)
2705 continue;
2707 if (btn->xindex==0 && btn->yindex > 0 && btn->yindex < vcount)
2708 vmap[btn->yindex] = 1;
2709 else if (btn->yindex==0 && btn->xindex>0 && btn->xindex<hcount)
2710 hmap[btn->xindex] = 1;
2712 for (chain=scr->global_icons; chain!=NULL; chain=chain->next) {
2713 btn = chain->aicon;
2714 if (btn->xindex==0 && btn->yindex > 0 && btn->yindex < vcount)
2715 vmap[btn->yindex] = 1;
2716 else if (btn->yindex==0 && btn->xindex>0 && btn->xindex<hcount)
2717 hmap[btn->xindex] = 1;
2719 break;
2720 case C_NW:
2721 for (i=0; i<dock->max_icons; i++) {
2722 btn = dock->icon_array[i];
2723 if (!btn)
2724 continue;
2726 if (btn->xindex==0 && btn->yindex > 0 && btn->yindex < vcount)
2727 vmap[btn->yindex] = 1;
2728 else if (btn->yindex==0 && btn->xindex<0 &&btn->xindex>-hcount)
2729 hmap[-btn->xindex] = 1;
2731 for (chain=scr->global_icons; chain!=NULL; chain=chain->next) {
2732 btn = chain->aicon;
2733 if (btn->xindex==0 && btn->yindex > 0 && btn->yindex < vcount)
2734 vmap[btn->yindex] = 1;
2735 else if (btn->yindex==0 && btn->xindex<0 &&btn->xindex>-hcount)
2736 hmap[-btn->xindex] = 1;
2738 break;
2739 case C_SE:
2740 for (i=0; i<dock->max_icons; i++) {
2741 btn = dock->icon_array[i];
2742 if (!btn)
2743 continue;
2745 if (btn->xindex==0 && btn->yindex < 0 && btn->yindex > -vcount)
2746 vmap[-btn->yindex] = 1;
2747 else if (btn->yindex==0 && btn->xindex>0 && btn->xindex<hcount)
2748 hmap[btn->xindex] = 1;
2750 for (chain=scr->global_icons; chain!=NULL; chain=chain->next) {
2751 btn = chain->aicon;
2752 if (btn->xindex==0 && btn->yindex < 0 && btn->yindex > -vcount)
2753 vmap[-btn->yindex] = 1;
2754 else if (btn->yindex==0 && btn->xindex>0 && btn->xindex<hcount)
2755 hmap[btn->xindex] = 1;
2757 break;
2758 case C_SW:
2759 default:
2760 for (i=0; i<dock->max_icons; i++) {
2761 btn = dock->icon_array[i];
2762 if (!btn)
2763 continue;
2765 if (btn->xindex==0 && btn->yindex < 0 && btn->yindex > -vcount)
2766 vmap[-btn->yindex] = 1;
2767 else if (btn->yindex==0 && btn->xindex<0 &&btn->xindex>-hcount)
2768 hmap[-btn->xindex] = 1;
2770 for (chain=scr->global_icons; chain!=NULL; chain=chain->next) {
2771 btn = chain->aicon;
2772 if (btn->xindex==0 && btn->yindex < 0 && btn->yindex > -vcount)
2773 vmap[-btn->yindex] = 1;
2774 else if (btn->yindex==0 && btn->xindex<0 &&btn->xindex>-hcount)
2775 hmap[-btn->xindex] = 1;
2778 x=0; y=0;
2779 done = 0;
2780 /* search a vacant slot */
2781 for (i=1; i<WMAX(vcount, hcount); i++) {
2782 if (i < vcount && vmap[i]==0) {
2783 /* found a slot */
2784 x = 0;
2785 y = i;
2786 done = 1;
2787 break;
2788 } else if (i < hcount && hmap[i]==0) {
2789 /* found a slot */
2790 x = i;
2791 y = 0;
2792 done = 1;
2793 break;
2796 wfree(vmap);
2797 wfree(hmap);
2798 /* If found a slot, translate and return */
2799 if (done) {
2800 if (corner==C_NW || corner==C_NE) {
2801 *y_pos = y;
2802 } else {
2803 *y_pos = -y;
2805 if (corner==C_NE || corner==C_SE) {
2806 *x_pos = x;
2807 } else {
2808 *x_pos = -x;
2810 return True;
2812 /* else, try to find a slot somewhere else */
2815 /* a map of mwidth x mwidth would be enough if we allowed icons to be
2816 * placed outside of screen */
2817 mwidth = (int)ceil(sqrt(dock->max_icons));
2819 /* In the worst case (the clip is in the corner of the screen),
2820 * the amount of icons that fit in the clip is smaller.
2821 * Double the map to get a safe value.
2823 mwidth += mwidth;
2825 r = (mwidth-1)/2;
2827 slot_map = wmalloc(mwidth*mwidth);
2828 memset(slot_map, 0, mwidth*mwidth);
2830 #define XY2OFS(x,y) (WMAX(abs(x),abs(y)) > r) ? 0 : (((y)+r)*(mwidth)+(x)+r)
2832 /* mark used slots in the map. If the slot falls outside the map
2833 * (for example, when all icons are placed in line), ignore them. */
2834 for (i=0; i<dock->max_icons; i++) {
2835 btn = dock->icon_array[i];
2836 if (btn)
2837 slot_map[XY2OFS(btn->xindex, btn->yindex)] = 1;
2839 for (chain=scr->global_icons; chain!=NULL; chain=chain->next) {
2840 slot_map[XY2OFS(chain->aicon->xindex, chain->aicon->yindex)] = 1;
2842 /* Find closest slot from the center that is free by scanning the
2843 * map from the center to outward in circular passes.
2844 * This will not result in a neat layout, but will be optimal
2845 * in the sense that there will not be holes left.
2847 done = 0;
2848 for (i = 1; i <= r && !done; i++) {
2849 int tx, ty;
2851 /* top and bottom parts of the ring */
2852 for (x = -i; x <= i && !done; x++) {
2853 tx = dock->x_pos + x*ICON_SIZE;
2854 y = -i;
2855 ty = dock->y_pos + y*ICON_SIZE;
2856 if (slot_map[XY2OFS(x,y)]==0
2857 && ON_SCREEN(tx, ty, sx, ex, sy, ey)) {
2858 *x_pos = x;
2859 *y_pos = y;
2860 done = 1;
2861 break;
2863 y = i;
2864 ty = dock->y_pos + y*ICON_SIZE;
2865 if (slot_map[XY2OFS(x,y)]==0
2866 && ON_SCREEN(tx, ty, sx, ex, sy, ey)) {
2867 *x_pos = x;
2868 *y_pos = y;
2869 done = 1;
2870 break;
2873 /* left and right parts of the ring */
2874 for (y = -i+1; y <= i-1; y++) {
2875 ty = dock->y_pos + y*ICON_SIZE;
2876 x = -i;
2877 tx = dock->x_pos + x*ICON_SIZE;
2878 if (slot_map[XY2OFS(x,y)]==0
2879 && ON_SCREEN(tx, ty, sx, ex, sy, ey)) {
2880 *x_pos = x;
2881 *y_pos = y;
2882 done = 1;
2883 break;
2885 x = i;
2886 tx = dock->x_pos + x*ICON_SIZE;
2887 if (slot_map[XY2OFS(x,y)]==0
2888 && ON_SCREEN(tx, ty, sx, ex, sy, ey)) {
2889 *x_pos = x;
2890 *y_pos = y;
2891 done = 1;
2892 break;
2896 wfree(slot_map);
2897 #undef XY2OFS
2898 return done;
2902 static void
2903 moveDock(WDock *dock, int new_x, int new_y)
2905 WAppIcon *btn;
2906 int i;
2908 dock->x_pos = new_x;
2909 dock->y_pos = new_y;
2910 for (i=0; i<dock->max_icons; i++) {
2911 btn = dock->icon_array[i];
2912 if (btn) {
2913 btn->x_pos = new_x + btn->xindex*ICON_SIZE;
2914 btn->y_pos = new_y + btn->yindex*ICON_SIZE;
2915 XMoveWindow(dpy, btn->icon->core->window, btn->x_pos, btn->y_pos);
2921 static void
2922 swapDock(WDock *dock)
2924 WScreen *scr = dock->screen_ptr;
2925 WAppIcon *btn;
2926 int x, i;
2929 if (dock->on_right_side) {
2930 x = dock->x_pos = scr->scr_width - ICON_SIZE - DOCK_EXTRA_SPACE;
2931 } else {
2932 x = dock->x_pos = DOCK_EXTRA_SPACE;
2935 for (i=0; i<dock->max_icons; i++) {
2936 btn = dock->icon_array[i];
2937 if (btn) {
2938 btn->x_pos = x;
2939 XMoveWindow(dpy, btn->icon->core->window, btn->x_pos, btn->y_pos);
2943 wScreenUpdateUsableArea(scr);
2947 static pid_t
2948 execCommand(WAppIcon *btn, char *command, WSavedState *state)
2950 WScreen *scr = btn->icon->core->screen_ptr;
2951 pid_t pid;
2952 char **argv;
2953 int argc;
2954 char *cmdline;
2956 cmdline = ExpandOptions(scr, command);
2958 if (scr->flags.dnd_data_convertion_status || !cmdline) {
2959 if (cmdline)
2960 wfree(cmdline);
2961 if (state)
2962 wfree(state);
2963 return 0;
2966 wtokensplit(cmdline, &argv, &argc);
2968 if (argv==NULL) {
2969 if (cmdline)
2970 wfree(cmdline);
2971 if (state)
2972 wfree(state);
2973 return 0;
2976 if ((pid=fork())==0) {
2977 char **args;
2978 int i;
2980 SetupEnvironment(scr);
2982 #ifdef HAVE_SETPGID
2983 setpgid(0, 0);
2984 #endif
2986 args = malloc(sizeof(char*)*(argc+1));
2987 if (!args)
2988 exit(111);
2989 for (i=0; i<argc; i++) {
2990 args[i] = argv[i];
2992 args[argc] = NULL;
2993 execvp(argv[0], args);
2994 exit(111);
2996 wtokenfree(argv, argc);
2998 if (pid > 0) {
2999 if (!state) {
3000 state = wmalloc(sizeof(WSavedState));
3001 memset(state, 0, sizeof(WSavedState));
3002 state->hidden = -1;
3003 state->miniaturized = -1;
3004 state->shaded = -1;
3005 if (btn->dock==scr->dock || btn->omnipresent)
3006 state->workspace = -1;
3007 else
3008 state->workspace = scr->current_workspace;
3010 wWindowAddSavedState(btn->wm_instance, btn->wm_class, cmdline, pid,
3011 state);
3012 wAddDeathHandler(pid, (WDeathHandler*)trackDeadProcess,
3013 btn->dock);
3014 } else if (state) {
3015 wfree(state);
3017 wfree(cmdline);
3018 return pid;
3022 void
3023 wDockHideIcons(WDock *dock)
3025 int i;
3027 if (dock==NULL)
3028 return;
3030 for (i=1; i<dock->max_icons; i++) {
3031 if (dock->icon_array[i])
3032 XUnmapWindow(dpy, dock->icon_array[i]->icon->core->window);
3034 dock->mapped = 0;
3036 dockIconPaint(dock->icon_array[0]);
3040 void
3041 wDockShowIcons(WDock *dock)
3043 int i, newlevel;
3044 WAppIcon *btn;
3046 if (dock==NULL)
3047 return;
3049 btn = dock->icon_array[0];
3050 moveDock(dock, btn->x_pos, btn->y_pos);
3052 newlevel = dock->lowered ? WMNormalLevel : WMDockLevel;
3053 ChangeStackingLevel(btn->icon->core, newlevel);
3055 for (i=1; i<dock->max_icons; i++) {
3056 if (dock->icon_array[i]) {
3057 MoveInStackListAbove(dock->icon_array[i]->icon->core,
3058 btn->icon->core);
3059 break;
3063 if (!dock->collapsed) {
3064 for (i=1; i<dock->max_icons; i++) {
3065 if (dock->icon_array[i]) {
3066 XMapWindow(dpy, dock->icon_array[i]->icon->core->window);
3070 dock->mapped = 1;
3072 dockIconPaint(btn);
3076 void
3077 wDockLower(WDock *dock)
3079 int i;
3081 for (i=0; i<dock->max_icons; i++) {
3082 if (dock->icon_array[i])
3083 wLowerFrame(dock->icon_array[i]->icon->core);
3088 void
3089 wDockRaise(WDock *dock)
3091 int i;
3093 for (i=dock->max_icons-1; i>=0; i--) {
3094 if (dock->icon_array[i])
3095 wRaiseFrame(dock->icon_array[i]->icon->core);
3100 void
3101 wDockRaiseLower(WDock *dock)
3103 if (!dock->icon_array[0]->icon->core->stacking->above
3104 ||(dock->icon_array[0]->icon->core->stacking->window_level
3105 !=dock->icon_array[0]->icon->core->stacking->above->stacking->window_level))
3106 wDockLower(dock);
3107 else
3108 wDockRaise(dock);
3112 void
3113 wDockFinishLaunch(WDock *dock, WAppIcon *icon)
3115 icon->launching = 0;
3116 icon->relaunching = 0;
3117 dockIconPaint(icon);
3121 WAppIcon*
3122 wDockFindIconForWindow(WDock *dock, Window window)
3124 WAppIcon *icon;
3125 int i;
3127 for (i=0; i<dock->max_icons; i++) {
3128 icon = dock->icon_array[i];
3129 if (icon && icon->main_window == window)
3130 return icon;
3132 return NULL;
3136 void
3137 wDockTrackWindowLaunch(WDock *dock, Window window)
3139 WAppIcon *icon;
3140 char *wm_class, *wm_instance;
3141 int i;
3142 Bool firstPass = True;
3143 Bool found = False;
3144 char *command = NULL;
3147 int argc;
3148 char **argv;
3150 if (XGetCommand(dpy, window, &argv, &argc)) {
3151 if (argc > 0 && argv != NULL)
3152 command = wtokenjoin(argv, argc);
3153 if (argv) {
3154 XFreeStringList(argv);
3159 if (!PropGetWMClass(window, &wm_class, &wm_instance) ||
3160 (!wm_class && !wm_instance))
3161 return;
3163 retry:
3164 for (i=0; i<dock->max_icons; i++) {
3165 icon = dock->icon_array[i];
3166 if (!icon)
3167 continue;
3169 /* app is already attached to icon */
3170 if (icon->main_window == window) {
3171 found = True;
3172 break;
3175 if ((icon->wm_instance || icon->wm_class)
3176 && (icon->launching || !icon->running)) {
3178 if (icon->wm_instance && wm_instance &&
3179 strcmp(icon->wm_instance, wm_instance)!=0) {
3180 continue;
3182 if (icon->wm_class && wm_class &&
3183 strcmp(icon->wm_class, wm_class)!=0) {
3184 continue;
3186 if (firstPass && command && strcmp(icon->command, command)!=0) {
3187 continue;
3190 if (!icon->relaunching) {
3191 WApplication *wapp;
3193 /* Possibly an application that was docked with dockit,
3194 * but the user did not update WMState to indicate that
3195 * it was docked by force */
3196 wapp = wApplicationOf(window);
3197 if (!wapp) {
3198 icon->forced_dock = 1;
3199 icon->running = 0;
3201 if (!icon->forced_dock)
3202 icon->main_window = window;
3205 found = True;
3206 if (!wPreferences.no_animations && !icon->launching &&
3207 !dock->screen_ptr->flags.startup && !dock->collapsed) {
3208 WAppIcon *aicon;
3209 int x0, y0;
3211 icon->launching = 1;
3212 dockIconPaint(icon);
3214 aicon = wAppIconCreateForDock(dock->screen_ptr, NULL,
3215 wm_instance, wm_class,
3216 TILE_NORMAL);
3217 PlaceIcon(dock->screen_ptr, &x0, &y0);
3218 wAppIconMove(aicon, x0, y0);
3219 /* Should this always be lowered? -Dan */
3220 if (dock->lowered)
3221 wLowerFrame(aicon->icon->core);
3222 XMapWindow(dpy, aicon->icon->core->window);
3223 aicon->launching = 1;
3224 wAppIconPaint(aicon);
3225 SlideWindow(aicon->icon->core->window, x0, y0,
3226 icon->x_pos, icon->y_pos);
3227 XUnmapWindow(dpy, aicon->icon->core->window);
3228 wAppIconDestroy(aicon);
3230 wDockFinishLaunch(dock, icon);
3231 break;
3235 if (firstPass && !found) {
3236 firstPass = False;
3237 goto retry;
3240 if (command)
3241 wfree(command);
3243 if (wm_class)
3244 XFree(wm_class);
3245 if (wm_instance)
3246 XFree(wm_instance);
3251 void
3252 wClipUpdateForWorkspaceChange(WScreen *scr, int workspace)
3254 if (!wPreferences.flags.noclip) {
3255 scr->clip_icon->dock = scr->workspaces[workspace]->clip;
3256 if (scr->current_workspace != workspace) {
3257 WDock *old_clip = scr->workspaces[scr->current_workspace]->clip;
3258 WAppIconChain *chain = scr->global_icons;
3260 while (chain) {
3261 moveIconBetweenDocks(chain->aicon->dock,
3262 scr->workspaces[workspace]->clip,
3263 chain->aicon, chain->aicon->xindex,
3264 chain->aicon->yindex);
3265 if (scr->workspaces[workspace]->clip->collapsed)
3266 XUnmapWindow(dpy, chain->aicon->icon->core->window);
3267 chain = chain->next;
3270 wDockHideIcons(old_clip);
3271 if (old_clip->auto_raise_lower) {
3272 if (old_clip->auto_raise_magic) {
3273 WMDeleteTimerHandler(old_clip->auto_raise_magic);
3274 old_clip->auto_raise_magic = NULL;
3276 wDockLower(old_clip);
3278 if (old_clip->auto_collapse) {
3279 if (old_clip->auto_expand_magic) {
3280 WMDeleteTimerHandler(old_clip->auto_expand_magic);
3281 old_clip->auto_expand_magic = NULL;
3283 old_clip->collapsed = 1;
3285 wDockShowIcons(scr->workspaces[workspace]->clip);
3287 if (scr->flags.clip_balloon_mapped)
3288 showClipBalloon(scr->clip_icon->dock, workspace);
3294 static void
3295 trackDeadProcess(pid_t pid, unsigned char status, WDock *dock)
3297 WAppIcon *icon;
3298 int i;
3300 for (i=0; i<dock->max_icons; i++) {
3301 icon = dock->icon_array[i];
3302 if (!icon)
3303 continue;
3305 if (icon->launching && icon->pid == pid) {
3306 if (!icon->relaunching) {
3307 icon->running = 0;
3308 icon->main_window = None;
3310 wDockFinishLaunch(dock, icon);
3311 icon->pid = 0;
3312 if (status==111) {
3313 char msg[PATH_MAX];
3314 char *cmd;
3316 if (icon->drop_launch)
3317 cmd = icon->dnd_command;
3318 else if (icon->paste_launch)
3319 cmd = icon->paste_command;
3320 else
3321 cmd = icon->command;
3323 snprintf(msg, sizeof(msg),
3324 _("Could not execute command \"%s\""), cmd);
3326 wMessageDialog(dock->screen_ptr, _("Error"), msg,
3327 _("OK"), NULL, NULL);
3329 break;
3335 static void
3336 toggleLowered(WDock *dock)
3338 WAppIcon *tmp;
3339 int newlevel, i;
3341 /* lower/raise Dock */
3342 if (!dock->lowered) {
3343 newlevel = WMNormalLevel;
3344 dock->lowered = 1;
3345 } else {
3346 newlevel = WMDockLevel;
3347 dock->lowered = 0;
3350 for (i=0; i<dock->max_icons; i++) {
3351 tmp = dock->icon_array[i];
3352 if (!tmp)
3353 continue;
3355 ChangeStackingLevel(tmp->icon->core, newlevel);
3356 if (dock->lowered)
3357 wLowerFrame(tmp->icon->core);
3360 if (dock->type == WM_DOCK)
3361 wScreenUpdateUsableArea(dock->screen_ptr);
3365 static void
3366 toggleCollapsed(WDock *dock)
3368 if (dock->collapsed) {
3369 dock->collapsed = 0;
3370 wDockShowIcons(dock);
3372 else {
3373 dock->collapsed = 1;
3374 wDockHideIcons(dock);
3379 static void
3380 openDockMenu(WDock *dock, WAppIcon *aicon, XEvent *event)
3382 WScreen *scr = dock->screen_ptr;
3383 WObjDescriptor *desc;
3384 WMenuEntry *entry;
3385 WApplication *wapp = NULL;
3386 int index = 0;
3387 int x_pos;
3388 int n_selected;
3389 int appIsRunning = aicon->running && aicon->icon && aicon->icon->owner;
3391 if (dock->type == WM_DOCK) {
3392 /* keep on top */
3393 entry = dock->menu->entries[index];
3394 entry->flags.indicator_on = !dock->lowered;
3395 entry->clientdata = dock;
3396 dock->menu->flags.realized = 0;
3397 } else {
3398 /* clip options */
3399 if (scr->clip_options)
3400 updateClipOptionsMenu(scr->clip_options, dock);
3402 n_selected = numberOfSelectedIcons(dock);
3404 /* Rename Workspace */
3405 entry = dock->menu->entries[++index];
3406 if (aicon == scr->clip_icon) {
3407 entry->callback = renameCallback;
3408 entry->clientdata = dock;
3409 entry->flags.indicator = 0;
3410 entry->text = _("Rename Workspace");
3411 } else {
3412 entry->callback = omnipresentCallback;
3413 entry->clientdata = aicon;
3414 if (n_selected > 0) {
3415 entry->flags.indicator = 0;
3416 entry->text = _("Toggle Omnipresent");
3417 } else {
3418 entry->flags.indicator = 1;
3419 entry->flags.indicator_on = aicon->omnipresent;
3420 entry->flags.indicator_type = MI_CHECK;
3421 entry->text = _("Omnipresent");
3425 /* select/unselect icon */
3426 entry = dock->menu->entries[++index];
3427 entry->clientdata = aicon;
3428 entry->flags.indicator_on = aicon->icon->selected;
3429 wMenuSetEnabled(dock->menu, index, aicon!=scr->clip_icon);
3431 /* select/unselect all icons */
3432 entry = dock->menu->entries[++index];
3433 entry->clientdata = aicon;
3434 if (n_selected > 0)
3435 entry->text = _("Unselect All Icons");
3436 else
3437 entry->text = _("Select All Icons");
3438 wMenuSetEnabled(dock->menu, index, dock->icon_count > 1);
3440 /* keep icon(s) */
3441 entry = dock->menu->entries[++index];
3442 entry->clientdata = aicon;
3443 if (n_selected > 1)
3444 entry->text = _("Keep Icons");
3445 else
3446 entry->text = _("Keep Icon");
3447 wMenuSetEnabled(dock->menu, index, dock->icon_count > 1);
3449 /* this is the workspace submenu part */
3450 entry = dock->menu->entries[++index];
3451 if (n_selected > 1)
3452 entry->text = _("Move Icons To");
3453 else
3454 entry->text = _("Move Icon To");
3455 if (scr->clip_submenu)
3456 updateWorkspaceMenu(scr->clip_submenu, aicon);
3457 wMenuSetEnabled(dock->menu, index, !aicon->omnipresent);
3459 /* remove icon(s) */
3460 entry = dock->menu->entries[++index];
3461 entry->clientdata = aicon;
3462 if (n_selected > 1)
3463 entry->text = _("Remove Icons");
3464 else
3465 entry->text = _("Remove Icon");
3466 wMenuSetEnabled(dock->menu, index, dock->icon_count > 1);
3468 /* attract icon(s) */
3469 entry = dock->menu->entries[++index];
3470 entry->clientdata = aicon;
3472 dock->menu->flags.realized = 0;
3473 wMenuRealize(dock->menu);
3477 if (aicon->icon->owner) {
3478 wapp = wApplicationOf(aicon->icon->owner->main_window);
3479 } else {
3480 wapp = NULL;
3483 /* launch */
3484 entry = dock->menu->entries[++index];
3485 entry->clientdata = aicon;
3486 wMenuSetEnabled(dock->menu, index, aicon->command!=NULL);
3488 /* unhide here */
3489 entry = dock->menu->entries[++index];
3490 entry->clientdata = aicon;
3491 if (wapp && wapp->flags.hidden) {
3492 entry->text = _("Unhide Here");
3493 } else {
3494 entry->text = _("Bring Here");
3496 wMenuSetEnabled(dock->menu, index, appIsRunning);
3498 /* hide */
3499 entry = dock->menu->entries[++index];
3500 entry->clientdata = aicon;
3501 if (wapp && wapp->flags.hidden) {
3502 entry->text = _("Unhide");
3503 } else {
3504 entry->text = _("Hide");
3506 wMenuSetEnabled(dock->menu, index, appIsRunning);
3508 /* settings */
3509 entry = dock->menu->entries[++index];
3510 entry->clientdata = aicon;
3511 wMenuSetEnabled(dock->menu, index, !aicon->editing
3512 && !wPreferences.flags.noupdates);
3514 /* kill */
3515 entry = dock->menu->entries[++index];
3516 entry->clientdata = aicon;
3517 wMenuSetEnabled(dock->menu, index, appIsRunning);
3519 if (!dock->menu->flags.realized)
3520 wMenuRealize(dock->menu);
3522 if (dock->type == WM_CLIP) {
3523 x_pos = event->xbutton.x_root+2;
3524 } else {
3525 x_pos = dock->on_right_side ?
3526 scr->scr_width - dock->menu->frame->core->width - 2 : 0;
3529 wMenuMapAt(dock->menu, x_pos, event->xbutton.y_root+2, False);
3531 /* allow drag select */
3532 event->xany.send_event = True;
3533 desc = &dock->menu->menu->descriptor;
3534 (*desc->handle_mousedown)(desc, event);
3538 static void
3539 openClipWorkspaceMenu(WScreen *scr, int x, int y)
3541 if (!scr->clip_ws_menu) {
3542 scr->clip_ws_menu = wWorkspaceMenuMake(scr, False);
3544 wWorkspaceMenuUpdate(scr, scr->clip_ws_menu);
3545 wMenuMapAt(scr->clip_ws_menu, x, y, False);
3549 /******************************************************************/
3550 static void
3551 iconDblClick(WObjDescriptor *desc, XEvent *event)
3553 WAppIcon *btn = desc->parent;
3554 WDock *dock = btn->dock;
3555 WApplication *wapp = NULL;
3556 int unhideHere = 0;
3558 if (btn->icon->owner && !(event->xbutton.state & ControlMask)) {
3559 wapp = wApplicationOf(btn->icon->owner->main_window);
3561 assert(wapp!=NULL);
3563 unhideHere = (event->xbutton.state & ShiftMask);
3565 /* go to the last workspace that the user worked on the app */
3566 if (wapp->last_workspace != dock->screen_ptr->current_workspace
3567 && !unhideHere) {
3568 wWorkspaceChange(dock->screen_ptr, wapp->last_workspace);
3571 wUnhideApplication(wapp, event->xbutton.button==Button2,
3572 unhideHere);
3574 if (event->xbutton.state & MOD_MASK) {
3575 wHideOtherApplications(btn->icon->owner);
3577 } else {
3578 if (event->xbutton.button==Button1) {
3580 if (event->xbutton.state & MOD_MASK) {
3581 /* raise/lower dock */
3582 toggleLowered(dock);
3583 } else if (btn == dock->screen_ptr->clip_icon) {
3584 if (getClipButton(event->xbutton.x, event->xbutton.y)==CLIP_IDLE)
3585 toggleCollapsed(dock);
3586 else
3587 handleClipChangeWorkspace(dock->screen_ptr, event);
3588 } else if (btn->command) {
3589 if (!btn->launching &&
3590 (!btn->running || (event->xbutton.state & ControlMask))) {
3591 launchDockedApplication(btn, False);
3593 } else if (btn->xindex == 0 && btn->yindex == 0
3594 && btn->dock->type == WM_DOCK) {
3596 wShowGNUstepPanel(dock->screen_ptr);
3604 static void
3605 handleDockMove(WDock *dock, WAppIcon *aicon, XEvent *event)
3607 WScreen *scr = dock->screen_ptr;
3608 int ofs_x=event->xbutton.x, ofs_y=event->xbutton.y;
3609 int x, y;
3610 XEvent ev;
3611 int grabbed = 0, swapped = 0, done;
3612 Pixmap ghost = None;
3613 int superfluous = wPreferences.superfluous; /* we catch it to avoid problems */
3615 #ifdef DEBUG
3616 puts("moving dock");
3617 #endif
3618 if (XGrabPointer(dpy, aicon->icon->core->window, True, ButtonMotionMask
3619 |ButtonReleaseMask|ButtonPressMask, GrabModeAsync,
3620 GrabModeAsync, None, None, CurrentTime) !=GrabSuccess) {
3621 wwarning("pointer grab failed for dock move");
3623 y = 0;
3624 for (x=0; x<dock->max_icons; x++) {
3625 if (dock->icon_array[x]!=NULL &&
3626 dock->icon_array[x]->yindex > y)
3627 y = dock->icon_array[x]->yindex;
3629 y++;
3630 XResizeWindow(dpy, scr->dock_shadow, ICON_SIZE, ICON_SIZE*y);
3632 done = 0;
3633 while (!done) {
3634 WMMaskEvent(dpy, PointerMotionMask|ButtonReleaseMask|ButtonPressMask
3635 |ButtonMotionMask|ExposureMask, &ev);
3636 switch (ev.type) {
3637 case Expose:
3638 WMHandleEvent(&ev);
3639 break;
3641 case MotionNotify:
3642 if (!grabbed) {
3643 if (abs(ofs_x-ev.xmotion.x)>=MOVE_THRESHOLD
3644 || abs(ofs_y-ev.xmotion.y)>=MOVE_THRESHOLD) {
3645 XChangeActivePointerGrab(dpy, ButtonMotionMask
3646 |ButtonReleaseMask|ButtonPressMask,
3647 wCursor[WCUR_MOVE], CurrentTime);
3648 grabbed=1;
3650 break;
3652 if (dock->type == WM_CLIP) {
3653 if (ev.xmotion.x_root - ofs_x < 0) {
3654 x = 0;
3655 } else if (ev.xmotion.x_root - ofs_x + ICON_SIZE >
3656 scr->scr_width) {
3657 x = scr->scr_width - ICON_SIZE;
3658 } else {
3659 x = ev.xmotion.x_root - ofs_x;
3661 if (ev.xmotion.y_root - ofs_y < 0) {
3662 y = 0;
3663 } else if (ev.xmotion.y_root - ofs_y + ICON_SIZE >
3664 scr->scr_height) {
3665 y = scr->scr_height - ICON_SIZE;
3666 } else {
3667 y = ev.xmotion.y_root - ofs_y;
3669 moveDock(dock, x, y);
3670 } else {
3671 /* move vertically if pointer is inside the dock*/
3672 if ((dock->on_right_side &&
3673 ev.xmotion.x_root >= dock->x_pos - ICON_SIZE)
3674 || (!dock->on_right_side &&
3675 ev.xmotion.x_root <= dock->x_pos + ICON_SIZE*2)) {
3677 if (ev.xmotion.y_root - ofs_y < 0) {
3678 y = 0;
3679 } else if (ev.xmotion.y_root - ofs_y + ICON_SIZE >
3680 scr->scr_height) {
3681 y = scr->scr_height - ICON_SIZE;
3682 } else {
3683 y = ev.xmotion.y_root - ofs_y;
3685 moveDock(dock, dock->x_pos, y);
3687 /* move horizontally to change sides */
3688 x = ev.xmotion.x_root - ofs_x;
3689 if (!dock->on_right_side) {
3691 /* is on left */
3693 if (ev.xmotion.x_root > dock->x_pos + ICON_SIZE*2) {
3694 XMoveWindow(dpy, scr->dock_shadow, scr->scr_width-ICON_SIZE
3695 -DOCK_EXTRA_SPACE-1, dock->y_pos);
3696 if (superfluous && ghost==None) {
3697 ghost = MakeGhostDock(dock, dock->x_pos,
3698 scr->scr_width-ICON_SIZE
3699 -DOCK_EXTRA_SPACE-1,
3700 dock->y_pos);
3701 XSetWindowBackgroundPixmap(dpy, scr->dock_shadow,
3702 ghost);
3703 XClearWindow(dpy, scr->dock_shadow);
3705 XMapRaised(dpy, scr->dock_shadow);
3706 swapped = 1;
3707 } else {
3708 if (superfluous && ghost!=None) {
3709 XFreePixmap(dpy, ghost);
3710 ghost = None;
3712 XUnmapWindow(dpy, scr->dock_shadow);
3713 swapped = 0;
3715 } else {
3716 /* is on right */
3717 if (ev.xmotion.x_root < dock->x_pos - ICON_SIZE) {
3718 XMoveWindow(dpy, scr->dock_shadow,
3719 DOCK_EXTRA_SPACE, dock->y_pos);
3720 if (superfluous && ghost==None) {
3721 ghost = MakeGhostDock(dock, dock->x_pos,
3722 DOCK_EXTRA_SPACE, dock->y_pos);
3723 XSetWindowBackgroundPixmap(dpy, scr->dock_shadow,
3724 ghost);
3725 XClearWindow(dpy, scr->dock_shadow);
3727 XMapRaised(dpy, scr->dock_shadow);
3728 swapped = -1;
3729 } else {
3730 XUnmapWindow(dpy, scr->dock_shadow);
3731 swapped = 0;
3732 if (superfluous && ghost!=None) {
3733 XFreePixmap(dpy, ghost);
3734 ghost = None;
3739 break;
3741 case ButtonPress:
3742 break;
3744 case ButtonRelease:
3745 if (ev.xbutton.button != event->xbutton.button)
3746 break;
3747 XUngrabPointer(dpy, CurrentTime);
3748 XUnmapWindow(dpy, scr->dock_shadow);
3749 XResizeWindow(dpy, scr->dock_shadow, ICON_SIZE, ICON_SIZE);
3750 if (dock->type == WM_DOCK) {
3751 if (swapped!=0) {
3752 if (swapped>0)
3753 dock->on_right_side = 1;
3754 else
3755 dock->on_right_side = 0;
3756 swapDock(dock);
3757 wArrangeIcons(scr, False);
3760 done = 1;
3761 break;
3764 if (superfluous) {
3765 if (ghost!=None)
3766 XFreePixmap(dpy, ghost);
3767 XSetWindowBackground(dpy, scr->dock_shadow, scr->white_pixel);
3769 #ifdef DEBUG
3770 puts("End dock move");
3771 #endif
3776 static void
3777 handleIconMove(WDock *dock, WAppIcon *aicon, XEvent *event)
3779 WScreen *scr = dock->screen_ptr;
3780 Window wins[2];
3781 WIcon *icon = aicon->icon;
3782 WDock *dock2 = NULL, *last_dock = dock, *clip = NULL;
3783 int ondock, grabbed = 0, change_dock = 0, collapsed = 0;
3784 XEvent ev;
3785 int x = aicon->x_pos, y = aicon->y_pos;
3786 int ofs_x = event->xbutton.x, ofs_y = event->xbutton.y;
3787 int shad_x = x, shad_y = y;
3788 int ix = aicon->xindex, iy = aicon->yindex;
3789 int tmp;
3790 Pixmap ghost = None;
3791 Bool docked;
3792 int superfluous = wPreferences.superfluous; /* we catch it to avoid problems */
3793 int omnipresent = aicon->omnipresent; /* this must be cached!!! */
3796 if (wPreferences.flags.noupdates)
3797 return;
3799 if (XGrabPointer(dpy, icon->core->window, True, ButtonMotionMask
3800 |ButtonReleaseMask|ButtonPressMask, GrabModeAsync,
3801 GrabModeAsync, None, None, CurrentTime) !=GrabSuccess) {
3802 #ifdef DEBUG0
3803 wwarning("pointer grab failed for icon move");
3804 #endif
3807 if (!(event->xbutton.state & MOD_MASK))
3808 wRaiseFrame(icon->core);
3810 if (!wPreferences.flags.noclip)
3811 clip = scr->workspaces[scr->current_workspace]->clip;
3813 if (dock == scr->dock && !wPreferences.flags.noclip)
3814 dock2 = clip;
3815 else if (dock != scr->dock && !wPreferences.flags.nodock)
3816 dock2 = scr->dock;
3818 wins[0] = icon->core->window;
3819 wins[1] = scr->dock_shadow;
3820 XRestackWindows(dpy, wins, 2);
3821 XMoveResizeWindow(dpy, scr->dock_shadow, aicon->x_pos, aicon->y_pos,
3822 ICON_SIZE, ICON_SIZE);
3823 if (superfluous) {
3824 if (icon->pixmap!=None)
3825 ghost = MakeGhostIcon(scr, icon->pixmap);
3826 else
3827 ghost = MakeGhostIcon(scr, icon->core->window);
3829 XSetWindowBackgroundPixmap(dpy, scr->dock_shadow, ghost);
3830 XClearWindow(dpy, scr->dock_shadow);
3832 XMapWindow(dpy, scr->dock_shadow);
3834 ondock = 1;
3837 while(1) {
3838 XMaskEvent(dpy, PointerMotionMask|ButtonReleaseMask|ButtonPressMask
3839 |ButtonMotionMask|ExposureMask, &ev);
3840 switch (ev.type) {
3841 case Expose:
3842 WMHandleEvent(&ev);
3843 break;
3845 case MotionNotify:
3846 if (!grabbed) {
3847 if (abs(ofs_x-ev.xmotion.x)>=MOVE_THRESHOLD
3848 || abs(ofs_y-ev.xmotion.y)>=MOVE_THRESHOLD) {
3849 XChangeActivePointerGrab(dpy, ButtonMotionMask
3850 |ButtonReleaseMask|ButtonPressMask,
3851 wCursor[WCUR_MOVE], CurrentTime);
3852 grabbed=1;
3853 } else {
3854 break;
3858 if (omnipresent) {
3859 int i;
3860 for (i=0; i<scr->workspace_count; i++) {
3861 if (i == scr->current_workspace)
3862 continue;
3863 wDockShowIcons(scr->workspaces[i]->clip);
3867 x = ev.xmotion.x_root - ofs_x;
3868 y = ev.xmotion.y_root - ofs_y;
3869 tmp = wDockSnapIcon(dock, aicon, x, y, &ix, &iy, True);
3870 if (tmp && dock2) {
3871 change_dock = 0;
3872 if (last_dock != dock && collapsed) {
3873 last_dock->collapsed = 1;
3874 wDockHideIcons(last_dock);
3875 collapsed = 0;
3877 if (!collapsed && (collapsed = dock->collapsed)) {
3878 dock->collapsed = 0;
3879 wDockShowIcons(dock);
3881 if (dock->auto_raise_lower)
3882 wDockRaise(dock);
3883 last_dock = dock;
3884 } else if (dock2) {
3885 tmp = wDockSnapIcon(dock2, aicon, x, y, &ix, &iy, False);
3886 if (tmp) {
3887 change_dock = 1;
3888 if (last_dock != dock2 && collapsed) {
3889 last_dock->collapsed = 1;
3890 wDockHideIcons(last_dock);
3891 collapsed = 0;
3893 if (!collapsed && (collapsed = dock2->collapsed)) {
3894 dock2->collapsed = 0;
3895 wDockShowIcons(dock2);
3897 if (dock2->auto_raise_lower)
3898 wDockRaise(dock2);
3899 last_dock = dock2;
3902 if (aicon->launching
3903 || aicon->lock
3904 || (aicon->running && !(ev.xmotion.state & MOD_MASK))
3905 || (!aicon->running && tmp)) {
3906 shad_x = last_dock->x_pos + ix*wPreferences.icon_size;
3907 shad_y = last_dock->y_pos + iy*wPreferences.icon_size;
3909 XMoveWindow(dpy, scr->dock_shadow, shad_x, shad_y);
3911 if (!ondock) {
3912 XMapWindow(dpy, scr->dock_shadow);
3914 ondock = 1;
3915 } else {
3916 if (ondock) {
3917 XUnmapWindow(dpy, scr->dock_shadow);
3919 ondock = 0;
3921 XMoveWindow(dpy, icon->core->window, x, y);
3922 break;
3924 case ButtonPress:
3925 break;
3927 case ButtonRelease:
3928 if (ev.xbutton.button != event->xbutton.button)
3929 break;
3930 XUngrabPointer(dpy, CurrentTime);
3931 if (ondock) {
3932 SlideWindow(icon->core->window, x, y, shad_x, shad_y);
3933 XUnmapWindow(dpy, scr->dock_shadow);
3934 if (!change_dock) {
3935 reattachIcon(dock, aicon, ix, iy);
3936 if (clip && dock!=clip && clip->auto_raise_lower)
3937 wDockLower(clip);
3938 } else {
3939 docked = moveIconBetweenDocks(dock, dock2, aicon, ix, iy);
3940 if (!docked) {
3941 /* Slide it back if dock rejected it */
3942 SlideWindow(icon->core->window, x, y, aicon->x_pos,
3943 aicon->y_pos);
3944 reattachIcon(dock, aicon, aicon->xindex,aicon->yindex);
3946 if (last_dock->type==WM_CLIP && last_dock->auto_collapse) {
3947 collapsed = 0;
3950 } else {
3951 aicon->x_pos = x;
3952 aicon->y_pos = y;
3953 if (superfluous) {
3954 if (!aicon->running && !wPreferences.no_animations) {
3955 /* We need to deselect it, even if is deselected in
3956 * wDockDetach(), because else DoKaboom() will fail.
3958 if (aicon->icon->selected)
3959 wIconSelect(aicon->icon);
3961 wSoundPlay(WSOUND_KABOOM);
3962 DoKaboom(scr,aicon->icon->core->window, x, y);
3963 } else {
3964 wSoundPlay(WSOUND_UNDOCK);
3966 } else {
3967 wSoundPlay(WSOUND_UNDOCK);
3969 if (clip && clip->auto_raise_lower)
3970 wDockLower(clip);
3971 wDockDetach(dock, aicon);
3973 if (collapsed) {
3974 last_dock->collapsed = 1;
3975 wDockHideIcons(last_dock);
3976 collapsed = 0;
3978 if (superfluous) {
3979 if (ghost!=None)
3980 XFreePixmap(dpy, ghost);
3981 XSetWindowBackground(dpy, scr->dock_shadow, scr->white_pixel);
3983 if (omnipresent) {
3984 int i;
3985 for (i=0; i<scr->workspace_count; i++) {
3986 if (i == scr->current_workspace)
3987 continue;
3988 wDockHideIcons(scr->workspaces[i]->clip);
3992 #ifdef DEBUG
3993 puts("End icon move");
3994 #endif
3995 return;
4001 static int
4002 getClipButton(int px, int py)
4004 int pt = (CLIP_BUTTON_SIZE+2)*ICON_SIZE/64;
4006 if (px < 0 || py < 0 || px >= ICON_SIZE || py >= ICON_SIZE)
4007 return CLIP_IDLE;
4009 if (py <= pt-((int)ICON_SIZE-1-px))
4010 return CLIP_FORWARD;
4011 else if (px <= pt-((int)ICON_SIZE-1-py))
4012 return CLIP_REWIND;
4014 return CLIP_IDLE;
4018 static void
4019 handleClipChangeWorkspace(WScreen *scr, XEvent *event)
4021 XEvent ev;
4022 int done, direction, new_ws;
4023 int new_dir;
4024 WDock *clip = scr->clip_icon->dock;
4026 direction = getClipButton(event->xbutton.x, event->xbutton.y);
4028 clip->lclip_button_pushed = direction==CLIP_REWIND;
4029 clip->rclip_button_pushed = direction==CLIP_FORWARD;
4031 wClipIconPaint(scr->clip_icon);
4032 done = 0;
4033 while(!done) {
4034 WMMaskEvent(dpy, ExposureMask|ButtonMotionMask|ButtonReleaseMask
4035 |ButtonPressMask, &ev);
4036 switch (ev.type) {
4037 case Expose:
4038 WMHandleEvent(&ev);
4039 break;
4041 case MotionNotify:
4042 new_dir = getClipButton(ev.xmotion.x, ev.xmotion.y);
4043 if (new_dir != direction) {
4044 direction = new_dir;
4045 clip->lclip_button_pushed = direction==CLIP_REWIND;
4046 clip->rclip_button_pushed = direction==CLIP_FORWARD;
4047 wClipIconPaint(scr->clip_icon);
4049 break;
4051 case ButtonPress:
4052 break;
4054 case ButtonRelease:
4055 if (ev.xbutton.button == event->xbutton.button)
4056 done = 1;
4060 clip->lclip_button_pushed = 0;
4061 clip->rclip_button_pushed = 0;
4063 new_ws = wPreferences.ws_advance || (event->xbutton.state & ControlMask);
4065 if (direction == CLIP_FORWARD) {
4066 if (scr->current_workspace < scr->workspace_count-1)
4067 wWorkspaceChange(scr, scr->current_workspace+1);
4068 else if (new_ws && scr->current_workspace < MAX_WORKSPACES-1)
4069 wWorkspaceChange(scr, scr->current_workspace+1);
4070 else if (wPreferences.ws_cycle)
4071 wWorkspaceChange(scr, 0);
4073 else if (direction == CLIP_REWIND) {
4074 if (scr->current_workspace > 0)
4075 wWorkspaceChange(scr, scr->current_workspace-1);
4076 else if (scr->current_workspace==0 && wPreferences.ws_cycle)
4077 wWorkspaceChange(scr, scr->workspace_count-1);
4080 wClipIconPaint(scr->clip_icon);
4084 static void
4085 iconMouseDown(WObjDescriptor *desc, XEvent *event)
4087 WAppIcon *aicon = desc->parent;
4088 WDock *dock = aicon->dock;
4089 WScreen *scr = aicon->icon->core->screen_ptr;
4091 if (aicon->editing || WCHECK_STATE(WSTATE_MODAL))
4092 return;
4094 scr->last_dock = dock;
4096 if (dock->menu->flags.mapped)
4097 wMenuUnmap(dock->menu);
4099 if (IsDoubleClick(scr, event)) {
4100 /* double-click was not in the main clip icon */
4101 if (dock->type != WM_CLIP || aicon->xindex!=0 || aicon->yindex!=0
4102 || getClipButton(event->xbutton.x, event->xbutton.y)==CLIP_IDLE) {
4103 iconDblClick(desc, event);
4104 return;
4108 if (dock->type == WM_CLIP && scr->flags.clip_balloon_mapped) {
4109 XUnmapWindow(dpy, scr->clip_balloon);
4110 scr->flags.clip_balloon_mapped = 0;
4113 #ifdef DEBUG
4114 puts("handling dock");
4115 #endif
4116 if (event->xbutton.button == Button1) {
4117 if (event->xbutton.state & MOD_MASK)
4118 wDockLower(dock);
4119 else
4120 wDockRaise(dock);
4122 if ((event->xbutton.state & ShiftMask) && aicon!=scr->clip_icon &&
4123 dock->type!=WM_DOCK) {
4124 wIconSelect(aicon->icon);
4125 return;
4128 if (aicon->yindex==0 && aicon->xindex==0) {
4129 if (getClipButton(event->xbutton.x, event->xbutton.y)!=CLIP_IDLE
4130 && dock->type==WM_CLIP)
4131 handleClipChangeWorkspace(scr, event);
4132 else
4133 handleDockMove(dock, aicon, event);
4134 } else
4135 handleIconMove(dock, aicon, event);
4137 } else if (event->xbutton.button==Button2 && dock->type==WM_CLIP &&
4138 aicon==scr->clip_icon) {
4139 openClipWorkspaceMenu(scr, event->xbutton.x_root+2,
4140 event->xbutton.y_root+2);
4141 if (scr->clip_ws_menu) {
4142 WMenu *menu;
4143 menu = scr->clip_ws_menu;
4144 desc = &menu->menu->descriptor;
4146 event->xany.send_event = True;
4147 (*desc->handle_mousedown)(desc, event);
4149 } else if (event->xbutton.button==Button2 && dock->type==WM_CLIP &&
4150 (event->xbutton.state & ShiftMask) && aicon!=scr->clip_icon) {
4151 wClipMakeIconOmnipresent(aicon, !aicon->omnipresent);
4152 } else if (event->xbutton.button == Button3) {
4153 if (event->xbutton.send_event &&
4154 XGrabPointer(dpy, aicon->icon->core->window, True, ButtonMotionMask
4155 |ButtonReleaseMask|ButtonPressMask, GrabModeAsync,
4156 GrabModeAsync, None, None, CurrentTime) !=GrabSuccess) {
4157 wwarning("pointer grab failed for dockicon menu");
4158 return;
4161 openDockMenu(dock, aicon, event);
4162 } else if (event->xbutton.button == Button2) {
4163 WAppIcon *btn = desc->parent;
4165 if (!btn->launching &&
4166 (!btn->running || (event->xbutton.state & ControlMask))) {
4167 launchDockedApplication(btn, True);
4173 static void
4174 showClipBalloon(WDock *dock, int workspace)
4176 int w, h;
4177 int x, y;
4178 WScreen *scr = dock->screen_ptr;
4179 char *text;
4180 Window stack[2];
4182 scr->flags.clip_balloon_mapped = 1;
4183 XMapWindow(dpy, scr->clip_balloon);
4185 text = scr->workspaces[workspace]->name;
4187 w = WMWidthOfString(scr->clip_title_font, text, strlen(text));
4189 h = WMFontHeight(scr->clip_title_font);
4190 XResizeWindow(dpy, scr->clip_balloon, w, h);
4192 x = dock->x_pos + CLIP_BUTTON_SIZE*ICON_SIZE/64;
4193 y = dock->y_pos + ICON_SIZE - WMFontHeight(scr->clip_title_font) - 3;
4195 if (x+w > scr->scr_width) {
4196 x = scr->scr_width - w;
4197 if (dock->y_pos + ICON_SIZE + h > scr->scr_height)
4198 y = dock->y_pos - h - 1;
4199 else
4200 y = dock->y_pos + ICON_SIZE;
4201 XRaiseWindow(dpy, scr->clip_balloon);
4202 } else {
4203 stack[0] = scr->clip_icon->icon->core->window;
4204 stack[1] = scr->clip_balloon;
4205 XRestackWindows(dpy, stack, 2);
4207 XMoveWindow(dpy, scr->clip_balloon, x, y);
4208 XSetForeground(dpy, scr->clip_title_gc,
4209 scr->clip_title_pixel[CLIP_NORMAL]);
4210 XClearWindow(dpy, scr->clip_balloon);
4211 WMDrawString(scr->wmscreen, scr->clip_balloon, scr->clip_title_gc,
4212 scr->clip_title_font, 0, 0, text, strlen(text));
4216 static void
4217 clipEnterNotify(WObjDescriptor *desc, XEvent *event)
4219 WAppIcon *btn = (WAppIcon*)desc->parent;
4220 WDock *dock;
4221 WScreen *scr;
4223 assert(event->type==EnterNotify);
4225 if(desc->parent_type!=WCLASS_DOCK_ICON)
4226 return;
4228 scr = btn->icon->core->screen_ptr;
4229 if (!btn->omnipresent)
4230 dock = btn->dock;
4231 else
4232 dock = scr->workspaces[scr->current_workspace]->clip;
4234 if (!dock || dock->type!=WM_CLIP)
4235 return;
4237 /* The auto raise/lower code */
4238 if (dock->auto_lower_magic) {
4239 WMDeleteTimerHandler(dock->auto_lower_magic);
4240 dock->auto_lower_magic = NULL;
4242 if (dock->auto_raise_lower && !dock->auto_raise_magic) {
4243 dock->auto_raise_magic = WMAddTimerHandler(AUTO_RAISE_DELAY,
4244 clipAutoRaise,
4245 (void *)dock);
4248 /* The auto expand/collapse code */
4249 if (dock->auto_collapse_magic) {
4250 WMDeleteTimerHandler(dock->auto_collapse_magic);
4251 dock->auto_collapse_magic = NULL;
4253 if (dock->auto_collapse && !dock->auto_expand_magic) {
4254 dock->auto_expand_magic = WMAddTimerHandler(AUTO_EXPAND_DELAY,
4255 clipAutoExpand,
4256 (void *)dock);
4259 if (btn->xindex == 0 && btn->yindex == 0)
4260 showClipBalloon(dock, dock->screen_ptr->current_workspace);
4261 else {
4262 if (dock->screen_ptr->flags.clip_balloon_mapped) {
4263 XUnmapWindow(dpy, dock->screen_ptr->clip_balloon);
4264 dock->screen_ptr->flags.clip_balloon_mapped = 0;
4270 static void
4271 clipLeave(WDock *dock)
4273 XEvent event;
4274 WObjDescriptor *desc = NULL;
4276 if (!dock || dock->type!=WM_CLIP)
4277 return;
4279 if (XCheckTypedEvent(dpy, EnterNotify, &event)!=False) {
4280 if (XFindContext(dpy, event.xcrossing.window, wWinContext,
4281 (XPointer *)&desc)!=XCNOENT
4282 && desc && desc->parent_type==WCLASS_DOCK_ICON
4283 && ((WAppIcon*)desc->parent)->dock
4284 && ((WAppIcon*)desc->parent)->dock->type==WM_CLIP) {
4285 /* We didn't left the Clip yet */
4286 XPutBackEvent(dpy, &event);
4287 return;
4290 XPutBackEvent(dpy, &event);
4291 } else {
4292 /* We entered a withdrawn window, so we're still in Clip */
4293 return;
4296 if (dock->auto_raise_magic) {
4297 WMDeleteTimerHandler(dock->auto_raise_magic);
4298 dock->auto_raise_magic = NULL;
4300 if (dock->auto_raise_lower && !dock->auto_lower_magic) {
4301 dock->auto_lower_magic = WMAddTimerHandler(AUTO_LOWER_DELAY,
4302 clipAutoLower,
4303 (void *)dock);
4306 if (dock->auto_expand_magic) {
4307 WMDeleteTimerHandler(dock->auto_expand_magic);
4308 dock->auto_expand_magic = NULL;
4310 if (dock->auto_collapse && !dock->auto_collapse_magic) {
4311 dock->auto_collapse_magic = WMAddTimerHandler(AUTO_COLLAPSE_DELAY,
4312 clipAutoCollapse,
4313 (void *)dock);
4318 static void
4319 clipLeaveNotify(WObjDescriptor *desc, XEvent *event)
4321 WAppIcon *btn = (WAppIcon*)desc->parent;
4323 assert(event->type==LeaveNotify);
4325 if(desc->parent_type!=WCLASS_DOCK_ICON)
4326 return;
4328 clipLeave(btn->dock);
4332 static void
4333 clipAutoCollapse(void *cdata)
4335 WDock *dock = (WDock *)cdata;
4337 if (dock->type!=WM_CLIP)
4338 return;
4340 if (dock->auto_collapse) {
4341 dock->collapsed = 1;
4342 wDockHideIcons(dock);
4344 dock->auto_collapse_magic = NULL;
4348 static void
4349 clipAutoExpand(void *cdata)
4351 WDock *dock = (WDock *)cdata;
4353 if (dock->type!=WM_CLIP)
4354 return;
4356 if (dock->auto_collapse) {
4357 dock->collapsed = 0;
4358 wDockShowIcons(dock);
4360 dock->auto_expand_magic = NULL;
4364 static void
4365 clipAutoLower(void *cdata)
4367 WDock *dock = (WDock *)cdata;
4369 if (dock->type!=WM_CLIP)
4370 return;
4372 if (dock->auto_raise_lower)
4373 wDockLower(dock);
4375 dock->auto_lower_magic = NULL;
4379 static void
4380 clipAutoRaise(void *cdata)
4382 WDock *dock = (WDock *)cdata;
4384 if (dock->type!=WM_CLIP)
4385 return;
4387 if (dock->auto_raise_lower)
4388 wDockRaise(dock);
4390 if (dock->screen_ptr->flags.clip_balloon_mapped) {
4391 showClipBalloon(dock, dock->screen_ptr->current_workspace);
4394 dock->auto_raise_magic = NULL;
4398 static Bool
4399 iconCanBeOmnipresent(WAppIcon *aicon)
4401 WScreen *scr = aicon->icon->core->screen_ptr;
4402 WDock *clip;
4403 WAppIcon *btn;
4404 int i, j;
4406 for (i=0; i<scr->workspace_count; i++) {
4407 clip = scr->workspaces[i]->clip;
4409 if (clip == aicon->dock)
4410 continue;
4412 if (clip->icon_count + scr->global_icon_count >= clip->max_icons)
4413 return False; /* Clip is full in some workspace */
4415 for (j=0; j<clip->max_icons; j++) {
4416 btn = clip->icon_array[j];
4417 if(btn && btn->xindex==aicon->xindex && btn->yindex==aicon->yindex)
4418 return False;
4422 return True;
4427 wClipMakeIconOmnipresent(WAppIcon *aicon, int omnipresent)
4429 WScreen *scr = aicon->icon->core->screen_ptr;
4430 WAppIconChain *new_entry, *tmp, *tmp1;
4431 int status = WO_SUCCESS;
4433 if ((scr->dock && aicon->dock==scr->dock) || aicon==scr->clip_icon) {
4434 return WO_NOT_APPLICABLE;
4437 if (aicon->omnipresent == omnipresent)
4438 return WO_SUCCESS;
4440 if (omnipresent) {
4441 if (iconCanBeOmnipresent(aicon)) {
4442 aicon->omnipresent = 1;
4443 new_entry = wmalloc(sizeof(WAppIconChain));
4444 new_entry->aicon = aicon;
4445 new_entry->next = scr->global_icons;
4446 scr->global_icons = new_entry;
4447 scr->global_icon_count++;
4448 } else {
4449 aicon->omnipresent = 0;
4450 status = WO_FAILED;
4452 } else {
4453 aicon->omnipresent = 0;
4454 if (aicon == scr->global_icons->aicon) {
4455 tmp = scr->global_icons->next;
4456 wfree(scr->global_icons);
4457 scr->global_icons = tmp;
4458 scr->global_icon_count--;
4459 } else {
4460 tmp = scr->global_icons;
4461 while (tmp->next) {
4462 if (tmp->next->aicon == aicon) {
4463 tmp1 = tmp->next->next;
4464 wfree(tmp->next);
4465 tmp->next = tmp1;
4466 scr->global_icon_count--;
4467 break;
4469 tmp = tmp->next;
4474 wAppIconPaint(aicon);
4476 return status;