- Fixed problem with unhiding taht mapped windows from other workspaces
[wmaker-crm.git] / src / dock.c
blobb563aa7f58ffb5dcf395eaa19eccb8b8429b478d
1 /* dock.c- built-in Dock module for WindowMaker
3 * Window Maker window manager
5 * Copyright (c) 1997, 1998 Alfredo K. Kojima
6 * Copyright (c) 1998, 1999 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;
1060 /* attract icons */
1061 entry = menu->entries[++index];
1062 entry->flags.indicator_on = dock->attract_icons;
1063 entry->clientdata = dock;
1065 menu->flags.realized = 0;
1066 wMenuRealize(menu);
1070 static WMenu*
1071 makeClipOptionsMenu(WScreen *scr)
1073 WMenu *menu;
1074 WMenuEntry *entry;
1076 menu = wMenuCreate(scr, NULL, False);
1077 if (!menu) {
1078 wwarning(_("could not create options submenu for Clip menu"));
1079 return NULL;
1082 entry = wMenuAddCallback(menu, _("Keep on Top"),
1083 toggleLoweredCallback, NULL);
1084 entry->flags.indicator = 1;
1085 entry->flags.indicator_on = 1;
1086 entry->flags.indicator_type = MI_CHECK;
1088 entry = wMenuAddCallback(menu, _("Collapsed"),
1089 toggleCollapsedCallback, NULL);
1090 entry->flags.indicator = 1;
1091 entry->flags.indicator_on = 1;
1092 entry->flags.indicator_type = MI_CHECK;
1094 entry = wMenuAddCallback(menu, _("Autocollapse"),
1095 toggleAutoCollapseCallback, NULL);
1096 entry->flags.indicator = 1;
1097 entry->flags.indicator_on = 1;
1098 entry->flags.indicator_type = MI_CHECK;
1100 entry = wMenuAddCallback(menu, _("Autoraise"),
1101 toggleAutoRaiseLowerCallback, NULL);
1102 entry->flags.indicator = 1;
1103 entry->flags.indicator_on = 1;
1104 entry->flags.indicator_type = MI_CHECK;
1106 entry = wMenuAddCallback(menu, _("Autoattract Icons"),
1107 toggleAutoAttractCallback, NULL);
1108 entry->flags.indicator = 1;
1109 entry->flags.indicator_on = 1;
1110 entry->flags.indicator_type = MI_CHECK;
1112 menu->flags.realized = 0;
1113 wMenuRealize(menu);
1115 return menu;
1119 static WMenu*
1120 dockMenuCreate(WScreen *scr, int type)
1122 WMenu *menu;
1123 WMenuEntry *entry;
1125 if (type == WM_CLIP && scr->clip_menu)
1126 return scr->clip_menu;
1128 menu = wMenuCreate(scr, NULL, False);
1129 if (type != WM_CLIP) {
1130 entry = wMenuAddCallback(menu, _("Keep on Top"),
1131 toggleLoweredCallback, NULL);
1132 entry->flags.indicator = 1;
1133 entry->flags.indicator_on = 1;
1134 entry->flags.indicator_type = MI_CHECK;
1135 } else {
1136 entry = wMenuAddCallback(menu, _("Clip Options"), NULL, NULL);
1137 scr->clip_options = makeClipOptionsMenu(scr);
1138 if (scr->clip_options)
1139 wMenuEntrySetCascade(menu, entry, scr->clip_options);
1141 entry = wMenuAddCallback(menu, _("Rename Workspace"), renameCallback,
1142 NULL);
1143 wfree(entry->text);
1144 entry->text = _("Rename Workspace");
1146 entry = wMenuAddCallback(menu, _("Selected"), selectCallback, NULL);
1147 entry->flags.indicator = 1;
1148 entry->flags.indicator_on = 1;
1149 entry->flags.indicator_type = MI_CHECK;
1151 entry = wMenuAddCallback(menu, _("Select All Icons"),
1152 selectIconsCallback, NULL);
1153 wfree(entry->text);
1154 entry->text = _("Select All Icons");
1156 entry = wMenuAddCallback(menu, _("Keep Icon"), keepIconsCallback, NULL);
1157 wfree(entry->text);
1158 entry->text = _("Keep Icon");
1160 entry = wMenuAddCallback(menu, _("Move Icon To"), NULL, NULL);
1161 wfree(entry->text);
1162 entry->text = _("Move Icon To");
1163 scr->clip_submenu = makeWorkspaceMenu(scr);
1164 if (scr->clip_submenu)
1165 wMenuEntrySetCascade(menu, entry, scr->clip_submenu);
1167 entry = wMenuAddCallback(menu, _("Remove Icon"), removeIconsCallback,
1168 NULL);
1169 wfree(entry->text);
1170 entry->text = _("Remove Icon");
1172 wMenuAddCallback(menu, _("Attract Icons"), colectIconsCallback, NULL);
1175 wMenuAddCallback(menu, _("Launch"), launchCallback, NULL);
1177 wMenuAddCallback(menu, _("Unhide Here"), unhideHereCallback, NULL);
1179 entry = wMenuAddCallback(menu, _("Hide"), hideCallback, NULL);
1180 wfree(entry->text);
1181 entry->text = _("Hide");
1183 wMenuAddCallback(menu, _("Settings..."), settingsCallback, NULL);
1185 wMenuAddCallback(menu, _("Kill"), killCallback, NULL);
1187 if (type == WM_CLIP)
1188 scr->clip_menu = menu;
1190 return menu;
1194 WDock*
1195 wDockCreate(WScreen *scr, int type)
1197 WDock *dock;
1198 WAppIcon *btn;
1199 int icon_count;
1201 make_keys();
1203 dock = wmalloc(sizeof(WDock));
1204 memset(dock, 0, sizeof(WDock));
1206 if (type == WM_CLIP)
1207 icon_count = CLIP_MAX_ICONS;
1208 else
1209 icon_count = scr->scr_height/wPreferences.icon_size;
1211 dock->icon_array = wmalloc(sizeof(WAppIcon*)*icon_count);
1212 memset(dock->icon_array, 0, sizeof(WAppIcon*)*icon_count);
1214 dock->max_icons = icon_count;
1216 btn = mainIconCreate(scr, type);
1218 btn->dock = dock;
1220 dock->x_pos = btn->x_pos;
1221 dock->y_pos = btn->y_pos;
1222 dock->screen_ptr = scr;
1223 dock->type = type;
1224 dock->icon_count = 1;
1225 dock->on_right_side = 1;
1226 dock->collapsed = 0;
1227 dock->auto_collapse = 0;
1228 dock->auto_collapse_magic = NULL;
1229 dock->auto_raise_lower = 0;
1230 dock->auto_lower_magic = NULL;
1231 dock->auto_raise_magic = NULL;
1232 dock->attract_icons = 0;
1233 dock->lowered = 1;
1234 dock->icon_array[0] = btn;
1235 wRaiseFrame(btn->icon->core);
1236 XMoveWindow(dpy, btn->icon->core->window, btn->x_pos, btn->y_pos);
1238 /* create dock menu */
1239 dock->menu = dockMenuCreate(scr, type);
1241 return dock;
1245 void
1246 wDockDestroy(WDock *dock)
1248 int i;
1249 WAppIcon *aicon;
1251 for (i=(dock->type == WM_CLIP) ? 1 : 0; i<dock->max_icons; i++) {
1252 aicon = dock->icon_array[i];
1253 if (aicon) {
1254 int keepit = aicon->running && wApplicationOf(aicon->main_window);
1255 wDockDetach(dock, aicon);
1256 if (keepit) {
1257 PlaceIcon(dock->screen_ptr, &aicon->x_pos, &aicon->y_pos);
1258 XMoveWindow(dpy, aicon->icon->core->window,
1259 aicon->x_pos, aicon->y_pos);
1260 if (!dock->mapped || dock->collapsed)
1261 XMapWindow(dpy, aicon->icon->core->window);
1265 if (wPreferences.auto_arrange_icons)
1266 wArrangeIcons(dock->screen_ptr, True);
1267 wfree(dock->icon_array);
1268 if (dock->menu && dock->type!=WM_CLIP)
1269 wMenuDestroy(dock->menu, True);
1270 if (dock->screen_ptr->last_dock == dock)
1271 dock->screen_ptr->last_dock = NULL;
1272 wfree(dock);
1276 void
1277 wClipIconPaint(WAppIcon *aicon)
1279 WScreen *scr = aicon->icon->core->screen_ptr;
1280 WWorkspace *workspace = scr->workspaces[scr->current_workspace];
1281 GC gc;
1282 Window win = aicon->icon->core->window;
1283 int length, nlength;
1284 char *ws_name, ws_number[10];
1285 int ty, tx;
1287 wIconPaint(aicon->icon);
1289 length = strlen(workspace->name);
1290 ws_name = wmalloc(length + 1);
1291 snprintf(ws_name, length+1, "%s", workspace->name);
1292 snprintf(ws_number, sizeof(ws_number), "%i", scr->current_workspace + 1);
1293 nlength = strlen(ws_number);
1295 gc = scr->clip_title_gc;
1297 if (!workspace->clip->collapsed)
1298 XSetForeground(dpy, gc, scr->clip_title_pixel[CLIP_NORMAL]);
1299 else
1300 XSetForeground(dpy, gc, scr->clip_title_pixel[CLIP_COLLAPSED]);
1302 ty = ICON_SIZE - WMFontHeight(scr->clip_title_font) - 3;
1304 tx = CLIP_BUTTON_SIZE*ICON_SIZE/64;
1306 WMDrawString(scr->wmscreen, win, gc, scr->clip_title_font, tx,
1307 ty, ws_name, length);
1309 tx = (ICON_SIZE/2 - WMWidthOfString(scr->clip_title_font, ws_number,
1310 nlength))/2;
1312 WMDrawString(scr->wmscreen, win, gc, scr->clip_title_font, tx,
1313 2, ws_number, nlength);
1315 wfree(ws_name);
1317 if (aicon->launching) {
1318 XFillRectangle(dpy, aicon->icon->core->window, scr->stipple_gc,
1319 0, 0, wPreferences.icon_size, wPreferences.icon_size);
1321 paintClipButtons(aicon, aicon->dock->lclip_button_pushed,
1322 aicon->dock->rclip_button_pushed);
1326 static void
1327 clipIconExpose(WObjDescriptor *desc, XEvent *event)
1329 wClipIconPaint(desc->parent);
1333 static void
1334 dockIconPaint(WAppIcon *btn)
1336 if (btn == btn->icon->core->screen_ptr->clip_icon)
1337 wClipIconPaint(btn);
1338 else
1339 wAppIconPaint(btn);
1343 static WMPropList*
1344 make_icon_state(WAppIcon *btn)
1346 WMPropList *node = NULL;
1347 WMPropList *command, *autolaunch, *lock, *name, *forced, *host;
1348 WMPropList *position, *buggy, *omnipresent;
1349 char *tmp;
1350 char buffer[64];
1352 if (btn) {
1353 if (!btn->command)
1354 command = WMCreatePLString("-");
1355 else
1356 command = WMCreatePLString(btn->command);
1358 autolaunch = btn->auto_launch ? dYes : dNo;
1360 lock = btn->lock ? dYes : dNo;
1362 tmp = EscapeWM_CLASS(btn->wm_instance, btn->wm_class);
1364 name = WMCreatePLString(tmp);
1366 wfree(tmp);
1368 forced = btn->forced_dock ? dYes : dNo;
1370 buggy = btn->buggy_app ? dYes : dNo;
1372 if (btn == btn->icon->core->screen_ptr->clip_icon)
1373 snprintf(buffer, sizeof(buffer), "%i,%i", btn->x_pos, btn->y_pos);
1374 else
1375 snprintf(buffer, sizeof(buffer), "%hi,%hi", btn->xindex, btn->yindex);
1376 position = WMCreatePLString(buffer);
1378 node = WMCreatePLDictionary(dCommand, command,
1379 dName, name,
1380 dAutoLaunch, autolaunch,
1381 dLock, lock,
1382 dForced, forced,
1383 dBuggyApplication, buggy,
1384 dPosition, position,
1385 NULL);
1386 WMReleasePropList(command);
1387 WMReleasePropList(name);
1388 WMReleasePropList(position);
1390 omnipresent = btn->omnipresent ? dYes : dNo;
1391 if (btn->dock != btn->icon->core->screen_ptr->dock &&
1392 (btn->xindex != 0 || btn->yindex != 0))
1393 WMPutInPLDictionary(node, dOmnipresent, omnipresent);
1395 #ifdef OFFIX_DND
1396 if (btn->dnd_command) {
1397 command = WMCreatePLString(btn->dnd_command);
1398 WMPutInPLDictionary(node, dDropCommand, command);
1399 WMReleasePropList(command);
1401 #endif /* OFFIX_DND */
1403 if (btn->paste_command) {
1404 command = WMCreatePLString(btn->paste_command);
1405 WMPutInPLDictionary(node, dPasteCommand, command);
1406 WMReleasePropList(command);
1409 if (btn->client_machine && btn->remote_start) {
1410 host = WMCreatePLString(btn->client_machine);
1411 WMPutInPLDictionary(node, dHost, host);
1412 WMReleasePropList(host);
1416 return node;
1420 static WMPropList*
1421 dockSaveState(WDock *dock)
1423 int i;
1424 WMPropList *icon_info;
1425 WMPropList *list=NULL, *dock_state=NULL;
1426 WMPropList *value, *key;
1427 char buffer[256];
1429 list = WMCreatePLArray(NULL);
1431 for (i=(dock->type==WM_DOCK ? 0 : 1); i<dock->max_icons; i++) {
1432 WAppIcon *btn = dock->icon_array[i];
1434 if (!btn || btn->attracted)
1435 continue;
1437 if ((icon_info = make_icon_state(dock->icon_array[i]))) {
1438 WMAddToPLArray(list, icon_info);
1439 WMReleasePropList(icon_info);
1443 dock_state = WMCreatePLDictionary(dApplications, list,
1444 NULL);
1446 if (dock->type == WM_DOCK) {
1447 snprintf(buffer, sizeof(buffer), "Applications%i", dock->screen_ptr->scr_height);
1448 key = WMCreatePLString(buffer);
1449 WMPutInPLDictionary(dock_state, key, list);
1450 WMReleasePropList(key);
1453 snprintf(buffer, sizeof(buffer), "%i,%i", (dock->on_right_side ? -ICON_SIZE : 0),
1454 dock->y_pos);
1455 value = WMCreatePLString(buffer);
1456 WMPutInPLDictionary(dock_state, dPosition, value);
1457 WMReleasePropList(value);
1459 WMReleasePropList(list);
1462 value = (dock->lowered ? dYes : dNo);
1463 WMPutInPLDictionary(dock_state, dLowered, value);
1465 if (dock->type == WM_CLIP) {
1466 value = (dock->collapsed ? dYes : dNo);
1467 WMPutInPLDictionary(dock_state, dCollapsed, value);
1469 value = (dock->auto_collapse ? dYes : dNo);
1470 WMPutInPLDictionary(dock_state, dAutoCollapse, value);
1472 value = (dock->auto_raise_lower ? dYes : dNo);
1473 WMPutInPLDictionary(dock_state, dAutoRaiseLower, value);
1475 value = (dock->attract_icons ? dYes : dNo);
1476 WMPutInPLDictionary(dock_state, dAutoAttractIcons, value);
1479 return dock_state;
1483 void
1484 wDockSaveState(WScreen *scr, WMPropList *old_state)
1486 WMPropList *dock_state;
1487 WMPropList *keys;
1489 dock_state = dockSaveState(scr->dock);
1492 * Copy saved states of docks with different sizes.
1494 if (old_state) {
1495 int i;
1496 WMPropList *tmp;
1498 keys = WMGetPLDictionaryKeys(old_state);
1499 for (i = 0; i < WMGetPropListItemCount(keys); i++) {
1500 tmp = WMGetFromPLArray(keys, i);
1502 if (strncasecmp(WMGetFromPLString(tmp), "applications", 12) == 0
1503 && !WMGetFromPLDictionary(dock_state, tmp)) {
1505 WMPutInPLDictionary(dock_state,
1506 tmp,
1507 WMGetFromPLDictionary(old_state, tmp));
1510 WMReleasePropList(keys);
1514 WMPutInPLDictionary(scr->session_state, dDock, dock_state);
1516 WMReleasePropList(dock_state);
1520 void
1521 wClipSaveState(WScreen *scr)
1523 WMPropList *clip_state;
1525 clip_state = make_icon_state(scr->clip_icon);
1527 WMPutInPLDictionary(scr->session_state, dClip, clip_state);
1529 WMReleasePropList(clip_state);
1533 WMPropList*
1534 wClipSaveWorkspaceState(WScreen *scr, int workspace)
1536 return dockSaveState(scr->workspaces[workspace]->clip);
1540 static Bool
1541 getBooleanDockValue(WMPropList *value, WMPropList *key)
1543 if (value) {
1544 if (WMIsPLString(value)) {
1545 if (strcasecmp(WMGetFromPLString(value), "YES")==0)
1546 return True;
1547 } else {
1548 wwarning(_("bad value in docked icon state info %s"),
1549 WMGetFromPLString(key));
1552 return False;
1556 static WAppIcon*
1557 restore_icon_state(WScreen *scr, WMPropList *info, int type, int index)
1559 WAppIcon *aicon;
1560 WMPropList *cmd, *value;
1563 cmd = WMGetFromPLDictionary(info, dCommand);
1564 if (!cmd || !WMIsPLString(cmd)) {
1565 return NULL;
1568 /* parse window name */
1569 value = WMGetFromPLDictionary(info, dName);
1570 if (!value)
1571 return NULL;
1574 char *wclass, *winstance;
1575 char *command;
1577 ParseWindowName(value, &winstance, &wclass, "dock");
1579 if (!winstance && !wclass) {
1580 return NULL;
1583 /* get commands */
1585 if (cmd)
1586 command = wstrdup(WMGetFromPLString(cmd));
1587 else
1588 command = NULL;
1590 if (!command || strcmp(command, "-")==0) {
1591 if (command)
1592 wfree(command);
1593 if (wclass)
1594 wfree(wclass);
1595 if (winstance)
1596 wfree(winstance);
1598 return NULL;
1601 aicon = wAppIconCreateForDock(scr, command, winstance, wclass,
1602 TILE_NORMAL);
1603 if (wclass)
1604 wfree(wclass);
1605 if (winstance)
1606 wfree(winstance);
1607 if (command)
1608 wfree(command);
1611 aicon->icon->core->descriptor.handle_mousedown = iconMouseDown;
1612 if (type == WM_CLIP) {
1613 aicon->icon->core->descriptor.handle_enternotify = clipEnterNotify;
1614 aicon->icon->core->descriptor.handle_leavenotify = clipLeaveNotify;
1616 aicon->icon->core->descriptor.parent_type = WCLASS_DOCK_ICON;
1617 aicon->icon->core->descriptor.parent = aicon;
1620 #ifdef OFFIX_DND
1621 cmd = WMGetFromPLDictionary(info, dDropCommand);
1622 if (cmd)
1623 aicon->dnd_command = wstrdup(WMGetFromPLString(cmd));
1624 #endif
1626 cmd = WMGetFromPLDictionary(info, dPasteCommand);
1627 if (cmd)
1628 aicon->paste_command = wstrdup(WMGetFromPLString(cmd));
1630 /* check auto launch */
1631 value = WMGetFromPLDictionary(info, dAutoLaunch);
1633 aicon->auto_launch = getBooleanDockValue(value, dAutoLaunch);
1635 /* check lock */
1636 value = WMGetFromPLDictionary(info, dLock);
1638 aicon->lock = getBooleanDockValue(value, dLock);
1640 /* check if it wasn't normally docked */
1641 value = WMGetFromPLDictionary(info, dForced);
1643 aicon->forced_dock = getBooleanDockValue(value, dForced);
1645 /* check if we can rely on the stuff in the app */
1646 value = WMGetFromPLDictionary(info, dBuggyApplication);
1648 aicon->buggy_app = getBooleanDockValue(value, dBuggyApplication);
1650 /* get position in the dock */
1651 value = WMGetFromPLDictionary(info, dPosition);
1652 if (value && WMIsPLString(value)) {
1653 if (sscanf(WMGetFromPLString(value), "%hi,%hi", &aicon->xindex,
1654 &aicon->yindex)!=2)
1655 wwarning(_("bad value in docked icon state info %s"),
1656 WMGetFromPLString(dPosition));
1658 /* check position sanity */
1659 /* incomplete section! */
1660 if (type == WM_DOCK) {
1661 aicon->xindex = 0;
1662 if (aicon->yindex < 0)
1663 wwarning(_("bad value in docked icon position %i,%i"),
1664 aicon->xindex, aicon->yindex);
1666 } else {
1667 aicon->yindex = index;
1668 aicon->xindex = 0;
1671 /* check if icon is omnipresent */
1672 value = WMGetFromPLDictionary(info, dOmnipresent);
1674 aicon->omnipresent = getBooleanDockValue(value, dOmnipresent);
1676 aicon->running = 0;
1677 aicon->docked = 1;
1679 return aicon;
1683 #define COMPLAIN(key) wwarning(_("bad value in dock state info:%s"), key)
1686 WAppIcon*
1687 wClipRestoreState(WScreen *scr, WMPropList *clip_state)
1689 WAppIcon *icon;
1690 WMPropList *value;
1693 icon = mainIconCreate(scr, WM_CLIP);
1695 if (!clip_state)
1696 return icon;
1697 else
1698 WMRetainPropList(clip_state);
1700 /* restore position */
1702 value = WMGetFromPLDictionary(clip_state, dPosition);
1704 if (value) {
1705 if (!WMIsPLString(value))
1706 COMPLAIN("Position");
1707 else {
1708 if (sscanf(WMGetFromPLString(value), "%i,%i", &icon->x_pos,
1709 &icon->y_pos)!=2)
1710 COMPLAIN("Position");
1712 /* check position sanity */
1713 if (icon->y_pos < 0)
1714 icon->y_pos = 0;
1715 else if (icon->y_pos > scr->scr_height-ICON_SIZE)
1716 icon->y_pos = scr->scr_height-ICON_SIZE;
1718 if (icon->x_pos < 0)
1719 icon->x_pos = 0;
1720 else if (icon->x_pos > scr->scr_width-ICON_SIZE)
1721 icon->x_pos = scr->scr_width-ICON_SIZE;
1725 #ifdef OFFIX_DND
1726 value = WMGetFromPLDictionary(clip_state, dDropCommand);
1727 if (value && WMIsPLString(value))
1728 icon->dnd_command = wstrdup(WMGetFromPLString(value));
1729 #endif
1731 value = WMGetFromPLDictionary(clip_state, dPasteCommand);
1732 if (value && WMIsPLString(value))
1733 icon->paste_command = wstrdup(WMGetFromPLString(value));
1735 WMReleasePropList(clip_state);
1737 return icon;
1741 WDock*
1742 wDockRestoreState(WScreen *scr, WMPropList *dock_state, int type)
1744 WDock *dock;
1745 WMPropList *apps;
1746 WMPropList *value;
1747 WAppIcon *aicon, *old_top;
1748 int count, i;
1751 dock = wDockCreate(scr, type);
1753 if (!dock_state)
1754 return dock;
1756 if (dock_state)
1757 WMRetainPropList(dock_state);
1760 /* restore position */
1762 value = WMGetFromPLDictionary(dock_state, dPosition);
1764 if (value) {
1765 if (!WMIsPLString(value))
1766 COMPLAIN("Position");
1767 else {
1768 if (sscanf(WMGetFromPLString(value), "%i,%i", &dock->x_pos,
1769 &dock->y_pos)!=2)
1770 COMPLAIN("Position");
1772 /* check position sanity */
1773 if (dock->y_pos < 0)
1774 dock->y_pos = 0;
1775 else if (dock->y_pos > scr->scr_height-ICON_SIZE)
1776 dock->y_pos = scr->scr_height - ICON_SIZE;
1778 /* This is no more needed. ??? */
1779 if (type == WM_CLIP) {
1780 if (dock->x_pos < 0)
1781 dock->x_pos = 0;
1782 else if (dock->x_pos > scr->scr_width-ICON_SIZE)
1783 dock->x_pos = scr->scr_width-ICON_SIZE;
1784 } else {
1785 if (dock->x_pos >= 0) {
1786 dock->x_pos = DOCK_EXTRA_SPACE;
1787 dock->on_right_side = 0;
1788 } else {
1789 dock->x_pos = scr->scr_width - DOCK_EXTRA_SPACE - ICON_SIZE;
1790 dock->on_right_side = 1;
1796 /* restore lowered/raised state */
1798 dock->lowered = 0;
1800 value = WMGetFromPLDictionary(dock_state, dLowered);
1802 if (value) {
1803 if (!WMIsPLString(value))
1804 COMPLAIN("Lowered");
1805 else {
1806 if (strcasecmp(WMGetFromPLString(value), "YES")==0)
1807 dock->lowered = 1;
1812 /* restore collapsed state */
1814 dock->collapsed = 0;
1816 value = WMGetFromPLDictionary(dock_state, dCollapsed);
1818 if (value) {
1819 if (!WMIsPLString(value))
1820 COMPLAIN("Collapsed");
1821 else {
1822 if (strcasecmp(WMGetFromPLString(value), "YES")==0)
1823 dock->collapsed = 1;
1828 /* restore auto-collapsed state */
1830 value = WMGetFromPLDictionary(dock_state, dAutoCollapse);
1832 if (value) {
1833 if (!WMIsPLString(value))
1834 COMPLAIN("AutoCollapse");
1835 else {
1836 if (strcasecmp(WMGetFromPLString(value), "YES")==0) {
1837 dock->auto_collapse = 1;
1838 dock->collapsed = 1;
1844 /* restore auto-raise/lower state */
1846 value = WMGetFromPLDictionary(dock_state, dAutoRaiseLower);
1848 if (value) {
1849 if (!WMIsPLString(value))
1850 COMPLAIN("AutoRaiseLower");
1851 else {
1852 if (strcasecmp(WMGetFromPLString(value), "YES")==0) {
1853 dock->auto_raise_lower = 1;
1858 /* restore attract icons state */
1860 dock->attract_icons = 0;
1862 value = WMGetFromPLDictionary(dock_state, dAutoAttractIcons);
1864 if (value) {
1865 if (!WMIsPLString(value))
1866 COMPLAIN("AutoAttractIcons");
1867 else {
1868 if (strcasecmp(WMGetFromPLString(value), "YES")==0)
1869 dock->attract_icons = 1;
1874 /* application list */
1877 WMPropList *tmp;
1878 char buffer[64];
1881 * When saving, it saves the dock state in
1882 * Applications and Applicationsnnn
1884 * When loading, it will first try Applicationsnnn.
1885 * If it does not exist, use Applications as default.
1888 snprintf(buffer, sizeof(buffer), "Applications%i", scr->scr_height);
1890 tmp = WMCreatePLString(buffer);
1891 apps = WMGetFromPLDictionary(dock_state, tmp);
1892 WMReleasePropList(tmp);
1894 if (!apps) {
1895 apps = WMGetFromPLDictionary(dock_state, dApplications);
1899 if (!apps) {
1900 goto finish;
1903 count = WMGetPropListItemCount(apps);
1905 if (count==0)
1906 goto finish;
1908 old_top = dock->icon_array[0];
1910 /* dock->icon_count is set to 1 when dock is created.
1911 * Since Clip is already restored, we want to keep it so for clip,
1912 * but for dock we may change the default top tile, so we set it to 0.
1914 if (type == WM_DOCK)
1915 dock->icon_count = 0;
1917 for (i=0; i<count; i++) {
1918 if (dock->icon_count >= dock->max_icons) {
1919 wwarning(_("there are too many icons stored in dock. Ignoring what doesn't fit"));
1920 break;
1923 value = WMGetFromPLArray(apps, i);
1924 aicon = restore_icon_state(scr, value, type, dock->icon_count);
1926 dock->icon_array[dock->icon_count] = aicon;
1928 if (aicon) {
1929 aicon->dock = dock;
1930 aicon->x_pos = dock->x_pos + (aicon->xindex*ICON_SIZE);
1931 aicon->y_pos = dock->y_pos + (aicon->yindex*ICON_SIZE);
1933 if (dock->lowered)
1934 ChangeStackingLevel(aicon->icon->core, WMNormalLevel);
1935 else
1936 ChangeStackingLevel(aicon->icon->core, WMDockLevel);
1938 wCoreConfigure(aicon->icon->core, aicon->x_pos, aicon->y_pos,
1939 0, 0);
1941 if (!dock->collapsed)
1942 XMapWindow(dpy, aicon->icon->core->window);
1943 wRaiseFrame(aicon->icon->core);
1945 dock->icon_count++;
1946 } else if (dock->icon_count==0 && type==WM_DOCK)
1947 dock->icon_count++;
1950 /* if the first icon is not defined, use the default */
1951 if (dock->icon_array[0]==NULL) {
1952 /* update default icon */
1953 old_top->x_pos = dock->x_pos;
1954 old_top->y_pos = dock->y_pos;
1955 if (dock->lowered)
1956 ChangeStackingLevel(old_top->icon->core, WMNormalLevel);
1957 else
1958 ChangeStackingLevel(old_top->icon->core, WMDockLevel);
1959 dock->icon_array[0] = old_top;
1960 XMoveWindow(dpy, old_top->icon->core->window, dock->x_pos, dock->y_pos);
1961 /* we don't need to increment dock->icon_count here because it was
1962 * incremented in the loop above.
1964 } else if (old_top!=dock->icon_array[0]) {
1965 if (old_top == scr->clip_icon)
1966 scr->clip_icon = dock->icon_array[0];
1967 wAppIconDestroy(old_top);
1970 finish:
1971 if (dock_state)
1972 WMReleasePropList(dock_state);
1974 return dock;
1979 void
1980 wDockLaunchWithState(WDock *dock, WAppIcon *btn, WSavedState *state)
1982 if (btn && btn->command && !btn->running && !btn->launching) {
1984 btn->drop_launch = 0;
1985 btn->paste_launch = 0;
1987 btn->pid = execCommand(btn, btn->command, state);
1989 if (btn->pid>0) {
1990 if (!btn->forced_dock && !btn->buggy_app) {
1991 btn->launching = 1;
1992 dockIconPaint(btn);
1995 } else {
1996 wfree(state);
2001 void
2002 wDockDoAutoLaunch(WDock *dock, int workspace)
2004 WAppIcon *btn;
2005 WSavedState *state;
2006 int i;
2008 for (i = 0; i < dock->max_icons; i++) {
2009 btn = dock->icon_array[i];
2010 if (!btn || !btn->auto_launch)
2011 continue;
2013 state = wmalloc(sizeof(WSavedState));
2014 memset(state, 0, sizeof(WSavedState));
2015 state->workspace = workspace;
2016 /* TODO: this is klugy and is very difficult to understand
2017 * what's going on. Try to clean up */
2018 wDockLaunchWithState(dock, btn, state);
2023 #ifdef OFFIX_DND
2024 static WDock*
2025 findDock(WScreen *scr, XEvent *event, int *icon_pos)
2027 WDock *dock;
2028 int i;
2030 *icon_pos = -1;
2031 if ((dock = scr->dock)!=NULL) {
2032 for (i=0; i<dock->max_icons; i++) {
2033 if (dock->icon_array[i]
2034 && dock->icon_array[i]->icon->core->window==event->xclient.window) {
2035 *icon_pos = i;
2036 break;
2040 if (*icon_pos<0 && (dock = scr->workspaces[scr->current_workspace]->clip)!=NULL) {
2041 for (i=0; i<dock->max_icons; i++) {
2042 if (dock->icon_array[i]
2043 && dock->icon_array[i]->icon->core->window==event->xclient.window) {
2044 *icon_pos = i;
2045 break;
2049 if(*icon_pos>=0)
2050 return dock;
2051 return NULL;
2056 wDockReceiveDNDDrop(WScreen *scr, XEvent *event)
2058 WDock *dock;
2059 WAppIcon *btn;
2060 int icon_pos;
2062 dock = findDock(scr, event, &icon_pos);
2063 if (!dock)
2064 return False;
2067 * Return True if the drop was on an application icon window.
2068 * In this case, let the ClientMessage handler redirect the
2069 * message to the app.
2071 if (dock->icon_array[icon_pos]->icon->icon_win!=None)
2072 return True;
2074 if (dock->icon_array[icon_pos]->dnd_command!=NULL) {
2075 scr->flags.dnd_data_convertion_status = 0;
2077 btn = dock->icon_array[icon_pos];
2079 if (!btn->forced_dock) {
2080 btn->relaunching = btn->running;
2081 btn->running = 1;
2083 if (btn->wm_instance || btn->wm_class) {
2084 WWindowAttributes attr;
2085 memset(&attr, 0, sizeof(WWindowAttributes));
2086 wDefaultFillAttributes(btn->icon->core->screen_ptr,
2087 btn->wm_instance,
2088 btn->wm_class, &attr, NULL, True);
2090 if (!attr.no_appicon)
2091 btn->launching = 1;
2092 else
2093 btn->running = 0;
2096 btn->paste_launch = 0;
2097 btn->drop_launch = 1;
2098 scr->last_dock = dock;
2099 btn->pid = execCommand(btn, btn->dnd_command, NULL);
2100 if (btn->pid>0) {
2101 dockIconPaint(btn);
2102 } else {
2103 btn->launching = 0;
2104 if (!btn->relaunching) {
2105 btn->running = 0;
2109 return False;
2111 #endif /* OFFIX_DND */
2115 Bool
2116 wDockAttachIcon(WDock *dock, WAppIcon *icon, int x, int y)
2118 WWindow *wwin;
2119 char **argv;
2120 int argc;
2121 int index;
2123 wwin = icon->icon->owner;
2124 if (icon->command==NULL) {
2125 icon->editing = 0;
2126 if (XGetCommand(dpy, wwin->client_win, &argv, &argc) && argc>0) {
2128 icon->command = wtokenjoin(argv, argc);
2129 XFreeStringList(argv);
2130 } else {
2131 char *command=NULL;
2133 /* icon->forced_dock = 1;*/
2134 if (dock->type!=WM_CLIP || !icon->attracted) {
2135 icon->editing = 1;
2136 if (wInputDialog(dock->screen_ptr, _("Dock Icon"),
2137 _("Type the command used to launch the application"),
2138 &command)) {
2139 if (command && (command[0]==0 ||
2140 (command[0]=='-' && command[1]==0))) {
2141 wfree(command);
2142 command = NULL;
2144 icon->command = command;
2145 icon->editing = 0;
2146 } else {
2147 icon->editing = 0;
2148 if (command)
2149 wfree(command);
2150 /* If the target is the dock, reject the icon. If
2151 * the target is the clip, make it an attracted icon
2153 if (dock->type==WM_CLIP) {
2154 icon->attracted = 1;
2155 if (!icon->icon->shadowed) {
2156 icon->icon->shadowed = 1;
2157 icon->icon->force_paint = 1;
2159 } else {
2160 return False;
2165 } else {
2166 icon->editing = 0;
2169 for (index=1; index<dock->max_icons; index++)
2170 if (dock->icon_array[index] == NULL)
2171 break;
2172 /* if (index == dock->max_icons)
2173 return; */
2175 assert(index < dock->max_icons);
2177 dock->icon_array[index] = icon;
2178 icon->yindex = y;
2179 icon->xindex = x;
2181 icon->omnipresent = 0;
2183 icon->x_pos = dock->x_pos + x*ICON_SIZE;
2184 icon->y_pos = dock->y_pos + y*ICON_SIZE;
2186 dock->icon_count++;
2188 icon->running = 1;
2189 icon->launching = 0;
2190 icon->docked = 1;
2191 icon->dock = dock;
2192 icon->icon->core->descriptor.handle_mousedown = iconMouseDown;
2193 if (dock->type == WM_CLIP) {
2194 icon->icon->core->descriptor.handle_enternotify = clipEnterNotify;
2195 icon->icon->core->descriptor.handle_leavenotify = clipLeaveNotify;
2197 icon->icon->core->descriptor.parent_type = WCLASS_DOCK_ICON;
2198 icon->icon->core->descriptor.parent = icon;
2200 MoveInStackListUnder(dock->icon_array[index-1]->icon->core,
2201 icon->icon->core);
2202 wAppIconMove(icon, icon->x_pos, icon->y_pos);
2203 wAppIconPaint(icon);
2205 if (wPreferences.auto_arrange_icons)
2206 wArrangeIcons(dock->screen_ptr, True);
2208 #ifdef OFFIX_DND
2209 if (icon->command && !icon->dnd_command) {
2210 int len = strlen(icon->command)+8;
2211 icon->dnd_command = wmalloc(len);
2212 snprintf(icon->dnd_command, len, "%s %%d", icon->command);
2214 #endif
2216 if (icon->command && !icon->paste_command) {
2217 int len = strlen(icon->command)+8;
2218 icon->paste_command = wmalloc(len);
2219 snprintf(icon->paste_command, len, "%s %%s", icon->command);
2222 return True;
2226 void
2227 reattachIcon(WDock *dock, WAppIcon *icon, int x, int y)
2229 int index;
2231 for(index=1; index<dock->max_icons; index++) {
2232 if(dock->icon_array[index] == icon)
2233 break;
2235 assert(index < dock->max_icons);
2237 icon->yindex = y;
2238 icon->xindex = x;
2240 icon->x_pos = dock->x_pos + x*ICON_SIZE;
2241 icon->y_pos = dock->y_pos + y*ICON_SIZE;
2245 Bool
2246 moveIconBetweenDocks(WDock *src, WDock *dest, WAppIcon *icon, int x, int y)
2248 WWindow *wwin;
2249 char **argv;
2250 int argc;
2251 int index;
2253 if (src == dest)
2254 return True; /* No move needed, we're already there */
2256 if (dest == NULL)
2257 return False;
2259 wwin = icon->icon->owner;
2262 * For the moment we can't do this if we move icons in Clip from one
2263 * workspace to other, because if we move two or more icons without
2264 * command, the dialog box will not be able to tell us to which of the
2265 * moved icons it applies. -Dan
2267 if ((dest->type==WM_DOCK /*|| dest->keep_attracted*/) && icon->command==NULL) {
2268 if (XGetCommand(dpy, wwin->client_win, &argv, &argc) && argc>0) {
2270 icon->command = wtokenjoin(argv, argc);
2271 XFreeStringList(argv);
2272 } else {
2273 char *command=NULL;
2275 icon->editing = 1;
2276 /* icon->forced_dock = 1;*/
2277 if (wInputDialog(src->screen_ptr, _("Dock Icon"),
2278 _("Type the command used to launch the application"),
2279 &command)) {
2280 if (command && (command[0]==0 ||
2281 (command[0]=='-' && command[1]==0))) {
2282 wfree(command);
2283 command = NULL;
2285 icon->command = command;
2286 } else {
2287 icon->editing = 0;
2288 if (command)
2289 wfree(command);
2290 return False;
2292 icon->editing = 0;
2296 if (dest->type == WM_DOCK)
2297 wClipMakeIconOmnipresent(icon, False);
2299 for(index=1; index<src->max_icons; index++) {
2300 if(src->icon_array[index] == icon)
2301 break;
2303 assert(index < src->max_icons);
2305 src->icon_array[index] = NULL;
2306 src->icon_count--;
2308 for(index=1; index<dest->max_icons; index++) {
2309 if(dest->icon_array[index] == NULL)
2310 break;
2312 /* if (index == dest->max_icons)
2313 return; */
2315 assert(index < dest->max_icons);
2317 dest->icon_array[index] = icon;
2318 icon->dock = dest;
2320 /* deselect the icon */
2321 if (icon->icon->selected)
2322 wIconSelect(icon->icon);
2324 if (dest->type == WM_DOCK) {
2325 icon->icon->core->descriptor.handle_enternotify = NULL;
2326 icon->icon->core->descriptor.handle_leavenotify = NULL;
2327 } else {
2328 icon->icon->core->descriptor.handle_enternotify = clipEnterNotify;
2329 icon->icon->core->descriptor.handle_leavenotify = clipLeaveNotify;
2332 /* set it to be kept when moving to dock.
2333 * Unless the icon does not have a command set
2335 if (icon->command && dest->type==WM_DOCK) {
2336 icon->attracted = 0;
2337 if (icon->icon->shadowed) {
2338 icon->icon->shadowed = 0;
2339 icon->icon->force_paint = 1;
2343 if (src->auto_collapse || src->auto_raise_lower)
2344 clipLeave(src);
2346 icon->yindex = y;
2347 icon->xindex = x;
2349 icon->x_pos = dest->x_pos + x*ICON_SIZE;
2350 icon->y_pos = dest->y_pos + y*ICON_SIZE;
2352 dest->icon_count++;
2354 MoveInStackListUnder(dest->icon_array[index-1]->icon->core,
2355 icon->icon->core);
2356 wAppIconPaint(icon);
2358 return True;
2362 void
2363 wDockDetach(WDock *dock, WAppIcon *icon)
2365 int index;
2367 /* make the settings panel be closed */
2368 if (icon->panel) {
2369 DestroyDockAppSettingsPanel(icon->panel);
2372 /* This must be called before icon->dock is set to NULL.
2373 * Don't move it. -Dan
2375 wClipMakeIconOmnipresent(icon, False);
2377 icon->docked = 0;
2378 icon->dock = NULL;
2379 icon->attracted = 0;
2380 icon->auto_launch = 0;
2381 if (icon->icon->shadowed) {
2382 icon->icon->shadowed = 0;
2383 icon->icon->force_paint = 1;
2386 /* deselect the icon */
2387 if (icon->icon->selected)
2388 wIconSelect(icon->icon);
2390 if (icon->command) {
2391 wfree(icon->command);
2392 icon->command = NULL;
2394 #ifdef OFFIX_DND
2395 if (icon->dnd_command) {
2396 wfree(icon->dnd_command);
2397 icon->dnd_command = NULL;
2399 #endif
2400 if (icon->paste_command) {
2401 wfree(icon->paste_command);
2402 icon->paste_command = NULL;
2405 for (index=1; index<dock->max_icons; index++)
2406 if (dock->icon_array[index] == icon)
2407 break;
2408 assert(index < dock->max_icons);
2409 dock->icon_array[index] = NULL;
2410 icon->yindex = -1;
2411 icon->xindex = -1;
2413 dock->icon_count--;
2415 /* if the dock is not attached to an application or
2416 * the the application did not set the approriate hints yet,
2417 * destroy the icon */
2418 if (!icon->running || !wApplicationOf(icon->main_window))
2419 wAppIconDestroy(icon);
2420 else {
2421 icon->icon->core->descriptor.handle_mousedown = appIconMouseDown;
2422 icon->icon->core->descriptor.handle_enternotify = NULL;
2423 icon->icon->core->descriptor.handle_leavenotify = NULL;
2424 icon->icon->core->descriptor.parent_type = WCLASS_APPICON;
2425 icon->icon->core->descriptor.parent = icon;
2427 ChangeStackingLevel(icon->icon->core, NORMAL_ICON_LEVEL);
2429 wAppIconPaint(icon);
2430 if (wPreferences.auto_arrange_icons) {
2431 wArrangeIcons(dock->screen_ptr, True);
2434 if (dock->auto_collapse || dock->auto_raise_lower)
2435 clipLeave(dock);
2440 * returns the closest Dock slot index for the passed
2441 * coordinates.
2443 * Returns False if icon can't be docked.
2445 * Note: this function should NEVER alter ret_x or ret_y, unless it will
2446 * return True. -Dan
2448 Bool
2449 wDockSnapIcon(WDock *dock, WAppIcon *icon, int req_x, int req_y,
2450 int *ret_x, int *ret_y, int redocking)
2452 WScreen *scr = dock->screen_ptr;
2453 int dx, dy;
2454 int ex_x, ex_y;
2455 int i, offset = ICON_SIZE/2;
2456 WAppIcon *aicon = NULL;
2457 WAppIcon *nicon = NULL;
2458 int max_y_icons, max_x_icons;
2460 max_x_icons = scr->scr_width/ICON_SIZE;
2461 max_y_icons = scr->scr_height/ICON_SIZE-1;
2463 if (wPreferences.flags.noupdates)
2464 return False;
2466 dx = dock->x_pos;
2467 dy = dock->y_pos;
2469 /* if the dock is full */
2470 if (!redocking &&
2471 (dock->icon_count >= dock->max_icons)) {
2472 return False;
2475 /* exact position */
2476 if (req_y < dy)
2477 ex_y = (req_y - offset - dy)/ICON_SIZE;
2478 else
2479 ex_y = (req_y + offset - dy)/ICON_SIZE;
2481 if (req_x < dx)
2482 ex_x = (req_x - offset - dx)/ICON_SIZE;
2483 else
2484 ex_x = (req_x + offset - dx)/ICON_SIZE;
2486 /* check if the icon is outside the screen boundaries */
2487 if (dx + ex_x*ICON_SIZE < -ICON_SIZE+2 ||
2488 dx + ex_x*ICON_SIZE >= scr->scr_width-1 ||
2489 dy + ex_y*ICON_SIZE < -ICON_SIZE+2 ||
2490 dy + ex_y*ICON_SIZE >= scr->scr_height-1)
2491 return False;
2493 if (dock->type == WM_DOCK) {
2494 if (icon->dock != dock && ex_x != 0)
2495 return False;
2497 aicon = NULL;
2498 for (i=0; i<dock->max_icons; i++) {
2499 nicon = dock->icon_array[i];
2500 if (nicon && nicon->yindex == ex_y) {
2501 aicon = nicon;
2502 break;
2506 if (redocking) {
2507 int sig, done, closest;
2509 /* Possible cases when redocking:
2511 * icon dragged out of range of any slot -> false
2512 * icon dragged to range of free slot
2513 * icon dragged to range of same slot
2514 * icon dragged to range of different icon
2516 if (abs(ex_x) > DOCK_DETTACH_THRESHOLD)
2517 return False;
2519 if (ex_y>=0 && ex_y<=max_y_icons && (aicon==icon || !aicon)) {
2520 *ret_x = 0;
2521 *ret_y = ex_y;
2522 return True;
2525 /* start looking at the upper slot or lower? */
2526 if (ex_y*ICON_SIZE < (req_y + offset - dy))
2527 sig = 1;
2528 else
2529 sig = -1;
2531 closest = -1;
2532 done = 0;
2533 /* look for closest free slot */
2534 for (i=0; i<(DOCK_DETTACH_THRESHOLD+1)*2 && !done; i++) {
2535 int j;
2537 done = 1;
2538 closest = sig*(i/2) + ex_y;
2539 /* check if this slot is used */
2540 if (closest >= 0) {
2541 for (j = 0; j<dock->max_icons; j++) {
2542 if (dock->icon_array[j]
2543 && dock->icon_array[j]->yindex==closest) {
2544 /* slot is used by someone else */
2545 if (dock->icon_array[j]!=icon)
2546 done = 0;
2547 break;
2551 sig = -sig;
2553 if (done && closest >= 0 && closest <= max_y_icons &&
2554 ((ex_y >= closest && ex_y - closest < DOCK_DETTACH_THRESHOLD+1)
2556 (ex_y < closest && closest - ex_y <= DOCK_DETTACH_THRESHOLD+1))) {
2557 *ret_x = 0;
2558 *ret_y = closest;
2559 return True;
2561 } else { /* !redocking */
2563 /* if slot is free and the icon is close enough, return it */
2564 if (!aicon && ex_x == 0 && ex_y >= 0 && ex_y <= max_y_icons) {
2565 *ret_x = 0;
2566 *ret_y = ex_y;
2567 return True;
2570 } else { /* CLIP */
2571 int neighbours = 0;
2572 int start, stop, k;
2574 start = icon->omnipresent ? 0 : scr->current_workspace;
2575 stop = icon->omnipresent ? scr->workspace_count : start+1;
2577 aicon = NULL;
2578 for (k=start; k<stop; k++) {
2579 WDock *tmp = scr->workspaces[k]->clip;
2580 if (!tmp)
2581 continue;
2582 for (i=0; i<tmp->max_icons; i++) {
2583 nicon = tmp->icon_array[i];
2584 if (nicon && nicon->xindex == ex_x && nicon->yindex == ex_y) {
2585 aicon = nicon;
2586 break;
2589 if (aicon)
2590 break;
2592 for (k=start; k<stop; k++) {
2593 WDock *tmp = scr->workspaces[k]->clip;
2594 if (!tmp)
2595 continue;
2596 for (i=0; i<tmp->max_icons; i++) {
2597 nicon = tmp->icon_array[i];
2598 if (nicon && nicon != icon && /* Icon can't be it's own neighbour */
2599 (abs(nicon->xindex - ex_x) <= CLIP_ATTACH_VICINITY &&
2600 abs(nicon->yindex - ex_y) <= CLIP_ATTACH_VICINITY)) {
2601 neighbours = 1;
2602 break;
2605 if (neighbours)
2606 break;
2609 if (neighbours && (aicon==NULL || (redocking && aicon == icon))) {
2610 *ret_x = ex_x;
2611 *ret_y = ex_y;
2612 return True;
2615 return False;
2619 #define ON_SCREEN(x, y, sx, ex, sy, ey) \
2620 ((((x)+ICON_SIZE/2) >= (sx)) && (((y)+ICON_SIZE/2) >= (sy)) && \
2621 (((x) + (ICON_SIZE/2)) <= (ex)) && (((y) + (ICON_SIZE/2)) <= ey))
2625 * returns true if it can find a free slot in the dock,
2626 * in which case it changes x_pos and y_pos accordingly.
2627 * Else returns false.
2629 Bool
2630 wDockFindFreeSlot(WDock *dock, int *x_pos, int *y_pos)
2632 WScreen *scr = dock->screen_ptr;
2633 WAppIcon *btn;
2634 WAppIconChain *chain;
2635 unsigned char *slot_map;
2636 int mwidth;
2637 int r;
2638 int x, y;
2639 int i, done = False;
2640 int corner;
2641 int sx=0, sy=0, ex=scr->scr_width, ey=scr->scr_height;
2642 int extra_count=0;
2644 if (dock->type == WM_CLIP &&
2645 dock != scr->workspaces[scr->current_workspace]->clip)
2646 extra_count = scr->global_icon_count;
2648 /* if the dock is full */
2649 if (dock->icon_count+extra_count >= dock->max_icons) {
2650 return False;
2653 if (!wPreferences.flags.nodock && scr->dock) {
2654 if (scr->dock->on_right_side)
2655 ex -= ICON_SIZE + DOCK_EXTRA_SPACE;
2656 else
2657 sx += ICON_SIZE + DOCK_EXTRA_SPACE;
2660 if (ex < dock->x_pos)
2661 ex = dock->x_pos;
2662 if (sx > dock->x_pos+ICON_SIZE)
2663 sx = dock->x_pos+ICON_SIZE;
2664 #define C_NONE 0
2665 #define C_NW 1
2666 #define C_NE 2
2667 #define C_SW 3
2668 #define C_SE 4
2670 /* check if clip is in a corner */
2671 if (dock->type==WM_CLIP) {
2672 if (dock->x_pos < 1 && dock->y_pos < 1)
2673 corner = C_NE;
2674 else if (dock->x_pos < 1 && dock->y_pos >= (ey-ICON_SIZE))
2675 corner = C_SE;
2676 else if (dock->x_pos >= (ex-ICON_SIZE)&& dock->y_pos >= (ey-ICON_SIZE))
2677 corner = C_SW;
2678 else if (dock->x_pos >= (ex-ICON_SIZE) && dock->y_pos < 1)
2679 corner = C_NW;
2680 else
2681 corner = C_NONE;
2682 } else
2683 corner = C_NONE;
2685 /* If the clip is in the corner, use only slots that are in the border
2686 * of the screen */
2687 if (corner!=C_NONE) {
2688 char *hmap, *vmap;
2689 int hcount, vcount;
2691 hcount = WMIN(dock->max_icons, scr->scr_width/ICON_SIZE);
2692 vcount = WMIN(dock->max_icons, scr->scr_height/ICON_SIZE);
2693 hmap = wmalloc(hcount+1);
2694 memset(hmap, 0, hcount+1);
2695 vmap = wmalloc(vcount+1);
2696 memset(vmap, 0, vcount+1);
2698 /* mark used positions */
2699 switch (corner) {
2700 case C_NE:
2701 for (i=0; i<dock->max_icons; i++) {
2702 btn = dock->icon_array[i];
2703 if (!btn)
2704 continue;
2706 if (btn->xindex==0 && btn->yindex > 0 && btn->yindex < vcount)
2707 vmap[btn->yindex] = 1;
2708 else if (btn->yindex==0 && btn->xindex>0 && btn->xindex<hcount)
2709 hmap[btn->xindex] = 1;
2711 for (chain=scr->global_icons; chain!=NULL; chain=chain->next) {
2712 btn = chain->aicon;
2713 if (btn->xindex==0 && btn->yindex > 0 && btn->yindex < vcount)
2714 vmap[btn->yindex] = 1;
2715 else if (btn->yindex==0 && btn->xindex>0 && btn->xindex<hcount)
2716 hmap[btn->xindex] = 1;
2718 break;
2719 case C_NW:
2720 for (i=0; i<dock->max_icons; i++) {
2721 btn = dock->icon_array[i];
2722 if (!btn)
2723 continue;
2725 if (btn->xindex==0 && btn->yindex > 0 && btn->yindex < vcount)
2726 vmap[btn->yindex] = 1;
2727 else if (btn->yindex==0 && btn->xindex<0 &&btn->xindex>-hcount)
2728 hmap[-btn->xindex] = 1;
2730 for (chain=scr->global_icons; chain!=NULL; chain=chain->next) {
2731 btn = chain->aicon;
2732 if (btn->xindex==0 && btn->yindex > 0 && btn->yindex < vcount)
2733 vmap[btn->yindex] = 1;
2734 else if (btn->yindex==0 && btn->xindex<0 &&btn->xindex>-hcount)
2735 hmap[-btn->xindex] = 1;
2737 break;
2738 case C_SE:
2739 for (i=0; i<dock->max_icons; i++) {
2740 btn = dock->icon_array[i];
2741 if (!btn)
2742 continue;
2744 if (btn->xindex==0 && btn->yindex < 0 && btn->yindex > -vcount)
2745 vmap[-btn->yindex] = 1;
2746 else if (btn->yindex==0 && btn->xindex>0 && btn->xindex<hcount)
2747 hmap[btn->xindex] = 1;
2749 for (chain=scr->global_icons; chain!=NULL; chain=chain->next) {
2750 btn = chain->aicon;
2751 if (btn->xindex==0 && btn->yindex < 0 && btn->yindex > -vcount)
2752 vmap[-btn->yindex] = 1;
2753 else if (btn->yindex==0 && btn->xindex>0 && btn->xindex<hcount)
2754 hmap[btn->xindex] = 1;
2756 break;
2757 case C_SW:
2758 default:
2759 for (i=0; i<dock->max_icons; i++) {
2760 btn = dock->icon_array[i];
2761 if (!btn)
2762 continue;
2764 if (btn->xindex==0 && btn->yindex < 0 && btn->yindex > -vcount)
2765 vmap[-btn->yindex] = 1;
2766 else if (btn->yindex==0 && btn->xindex<0 &&btn->xindex>-hcount)
2767 hmap[-btn->xindex] = 1;
2769 for (chain=scr->global_icons; chain!=NULL; chain=chain->next) {
2770 btn = chain->aicon;
2771 if (btn->xindex==0 && btn->yindex < 0 && btn->yindex > -vcount)
2772 vmap[-btn->yindex] = 1;
2773 else if (btn->yindex==0 && btn->xindex<0 &&btn->xindex>-hcount)
2774 hmap[-btn->xindex] = 1;
2777 x=0; y=0;
2778 done = 0;
2779 /* search a vacant slot */
2780 for (i=1; i<WMAX(vcount, hcount); i++) {
2781 if (i < vcount && vmap[i]==0) {
2782 /* found a slot */
2783 x = 0;
2784 y = i;
2785 done = 1;
2786 break;
2787 } else if (i < hcount && hmap[i]==0) {
2788 /* found a slot */
2789 x = i;
2790 y = 0;
2791 done = 1;
2792 break;
2795 wfree(vmap);
2796 wfree(hmap);
2797 /* If found a slot, translate and return */
2798 if (done) {
2799 if (corner==C_NW || corner==C_NE) {
2800 *y_pos = y;
2801 } else {
2802 *y_pos = -y;
2804 if (corner==C_NE || corner==C_SE) {
2805 *x_pos = x;
2806 } else {
2807 *x_pos = -x;
2809 return True;
2811 /* else, try to find a slot somewhere else */
2814 /* a map of mwidth x mwidth would be enough if we allowed icons to be
2815 * placed outside of screen */
2816 mwidth = (int)ceil(sqrt(dock->max_icons));
2818 /* In the worst case (the clip is in the corner of the screen),
2819 * the amount of icons that fit in the clip is smaller.
2820 * Double the map to get a safe value.
2822 mwidth += mwidth;
2824 r = (mwidth-1)/2;
2826 slot_map = wmalloc(mwidth*mwidth);
2827 memset(slot_map, 0, mwidth*mwidth);
2829 #define XY2OFS(x,y) (WMAX(abs(x),abs(y)) > r) ? 0 : (((y)+r)*(mwidth)+(x)+r)
2831 /* mark used slots in the map. If the slot falls outside the map
2832 * (for example, when all icons are placed in line), ignore them. */
2833 for (i=0; i<dock->max_icons; i++) {
2834 btn = dock->icon_array[i];
2835 if (btn)
2836 slot_map[XY2OFS(btn->xindex, btn->yindex)] = 1;
2838 for (chain=scr->global_icons; chain!=NULL; chain=chain->next) {
2839 slot_map[XY2OFS(chain->aicon->xindex, chain->aicon->yindex)] = 1;
2841 /* Find closest slot from the center that is free by scanning the
2842 * map from the center to outward in circular passes.
2843 * This will not result in a neat layout, but will be optimal
2844 * in the sense that there will not be holes left.
2846 done = 0;
2847 for (i = 1; i <= r && !done; i++) {
2848 int tx, ty;
2850 /* top and bottom parts of the ring */
2851 for (x = -i; x <= i && !done; x++) {
2852 tx = dock->x_pos + x*ICON_SIZE;
2853 y = -i;
2854 ty = dock->y_pos + y*ICON_SIZE;
2855 if (slot_map[XY2OFS(x,y)]==0
2856 && ON_SCREEN(tx, ty, sx, ex, sy, ey)) {
2857 *x_pos = x;
2858 *y_pos = y;
2859 done = 1;
2860 break;
2862 y = i;
2863 ty = dock->y_pos + y*ICON_SIZE;
2864 if (slot_map[XY2OFS(x,y)]==0
2865 && ON_SCREEN(tx, ty, sx, ex, sy, ey)) {
2866 *x_pos = x;
2867 *y_pos = y;
2868 done = 1;
2869 break;
2872 /* left and right parts of the ring */
2873 for (y = -i+1; y <= i-1; y++) {
2874 ty = dock->y_pos + y*ICON_SIZE;
2875 x = -i;
2876 tx = dock->x_pos + x*ICON_SIZE;
2877 if (slot_map[XY2OFS(x,y)]==0
2878 && ON_SCREEN(tx, ty, sx, ex, sy, ey)) {
2879 *x_pos = x;
2880 *y_pos = y;
2881 done = 1;
2882 break;
2884 x = i;
2885 tx = dock->x_pos + x*ICON_SIZE;
2886 if (slot_map[XY2OFS(x,y)]==0
2887 && ON_SCREEN(tx, ty, sx, ex, sy, ey)) {
2888 *x_pos = x;
2889 *y_pos = y;
2890 done = 1;
2891 break;
2895 wfree(slot_map);
2896 #undef XY2OFS
2897 return done;
2901 static void
2902 moveDock(WDock *dock, int new_x, int new_y)
2904 WAppIcon *btn;
2905 int i;
2907 dock->x_pos = new_x;
2908 dock->y_pos = new_y;
2909 for (i=0; i<dock->max_icons; i++) {
2910 btn = dock->icon_array[i];
2911 if (btn) {
2912 btn->x_pos = new_x + btn->xindex*ICON_SIZE;
2913 btn->y_pos = new_y + btn->yindex*ICON_SIZE;
2914 XMoveWindow(dpy, btn->icon->core->window, btn->x_pos, btn->y_pos);
2920 static void
2921 swapDock(WDock *dock)
2923 WScreen *scr = dock->screen_ptr;
2924 WAppIcon *btn;
2925 int x, i;
2928 if (dock->on_right_side) {
2929 x = dock->x_pos = scr->scr_width - ICON_SIZE - DOCK_EXTRA_SPACE;
2930 } else {
2931 x = dock->x_pos = DOCK_EXTRA_SPACE;
2934 for (i=0; i<dock->max_icons; i++) {
2935 btn = dock->icon_array[i];
2936 if (btn) {
2937 btn->x_pos = x;
2938 XMoveWindow(dpy, btn->icon->core->window, btn->x_pos, btn->y_pos);
2942 wScreenUpdateUsableArea(scr);
2946 static pid_t
2947 execCommand(WAppIcon *btn, char *command, WSavedState *state)
2949 WScreen *scr = btn->icon->core->screen_ptr;
2950 pid_t pid;
2951 char **argv;
2952 int argc;
2953 char *cmdline;
2955 cmdline = ExpandOptions(scr, command);
2957 if (scr->flags.dnd_data_convertion_status || !cmdline) {
2958 if (cmdline)
2959 wfree(cmdline);
2960 if (state)
2961 wfree(state);
2962 return 0;
2965 wtokensplit(cmdline, &argv, &argc);
2967 if (argv==NULL) {
2968 if (cmdline)
2969 wfree(cmdline);
2970 if (state)
2971 wfree(state);
2972 return 0;
2975 if ((pid=fork())==0) {
2976 char **args;
2977 int i;
2979 SetupEnvironment(scr);
2981 #ifdef HAVE_SETPGID
2982 setpgid(0, 0);
2983 #endif
2985 args = malloc(sizeof(char*)*(argc+1));
2986 if (!args)
2987 exit(111);
2988 for (i=0; i<argc; i++) {
2989 args[i] = argv[i];
2991 args[argc] = NULL;
2992 execvp(argv[0], args);
2993 exit(111);
2995 wtokenfree(argv, argc);
2997 if (pid > 0) {
2998 if (!state) {
2999 state = wmalloc(sizeof(WSavedState));
3000 memset(state, 0, sizeof(WSavedState));
3001 state->hidden = -1;
3002 state->miniaturized = -1;
3003 state->shaded = -1;
3004 if (btn->dock==scr->dock || btn->omnipresent)
3005 state->workspace = -1;
3006 else
3007 state->workspace = scr->current_workspace;
3009 wWindowAddSavedState(btn->wm_instance, btn->wm_class, cmdline, pid,
3010 state);
3011 wAddDeathHandler(pid, (WDeathHandler*)trackDeadProcess,
3012 btn->dock);
3013 } else if (state) {
3014 wfree(state);
3016 wfree(cmdline);
3017 return pid;
3021 void
3022 wDockHideIcons(WDock *dock)
3024 int i;
3026 if (dock==NULL)
3027 return;
3029 for (i=1; i<dock->max_icons; i++) {
3030 if (dock->icon_array[i])
3031 XUnmapWindow(dpy, dock->icon_array[i]->icon->core->window);
3033 dock->mapped = 0;
3035 dockIconPaint(dock->icon_array[0]);
3039 void
3040 wDockShowIcons(WDock *dock)
3042 int i, newlevel;
3043 WAppIcon *btn;
3045 if (dock==NULL)
3046 return;
3048 btn = dock->icon_array[0];
3049 moveDock(dock, btn->x_pos, btn->y_pos);
3051 newlevel = dock->lowered ? WMNormalLevel : WMDockLevel;
3052 ChangeStackingLevel(btn->icon->core, newlevel);
3054 for (i=1; i<dock->max_icons; i++) {
3055 if (dock->icon_array[i]) {
3056 MoveInStackListAbove(dock->icon_array[i]->icon->core,
3057 btn->icon->core);
3058 break;
3062 if (!dock->collapsed) {
3063 for (i=1; i<dock->max_icons; i++) {
3064 if (dock->icon_array[i]) {
3065 XMapWindow(dpy, dock->icon_array[i]->icon->core->window);
3069 dock->mapped = 1;
3071 dockIconPaint(btn);
3075 void
3076 wDockLower(WDock *dock)
3078 int i;
3080 for (i=0; i<dock->max_icons; i++) {
3081 if (dock->icon_array[i])
3082 wLowerFrame(dock->icon_array[i]->icon->core);
3087 void
3088 wDockRaise(WDock *dock)
3090 int i;
3092 for (i=dock->max_icons-1; i>=0; i--) {
3093 if (dock->icon_array[i])
3094 wRaiseFrame(dock->icon_array[i]->icon->core);
3099 void
3100 wDockRaiseLower(WDock *dock)
3102 if (!dock->icon_array[0]->icon->core->stacking->above
3103 ||(dock->icon_array[0]->icon->core->stacking->window_level
3104 !=dock->icon_array[0]->icon->core->stacking->above->stacking->window_level))
3105 wDockLower(dock);
3106 else
3107 wDockRaise(dock);
3111 void
3112 wDockFinishLaunch(WDock *dock, WAppIcon *icon)
3114 icon->launching = 0;
3115 icon->relaunching = 0;
3116 dockIconPaint(icon);
3120 WAppIcon*
3121 wDockFindIconForWindow(WDock *dock, Window window)
3123 WAppIcon *icon;
3124 int i;
3126 for (i=0; i<dock->max_icons; i++) {
3127 icon = dock->icon_array[i];
3128 if (icon && icon->main_window == window)
3129 return icon;
3131 return NULL;
3135 void
3136 wDockTrackWindowLaunch(WDock *dock, Window window)
3138 WAppIcon *icon;
3139 char *wm_class, *wm_instance;
3140 int i;
3141 Bool firstPass = True;
3142 Bool found = False;
3143 char *command = NULL;
3146 int argc;
3147 char **argv;
3149 if (XGetCommand(dpy, window, &argv, &argc)) {
3150 if (argc > 0 && argv != NULL)
3151 command = wtokenjoin(argv, argc);
3152 if (argv) {
3153 XFreeStringList(argv);
3158 if (!PropGetWMClass(window, &wm_class, &wm_instance) ||
3159 (!wm_class && !wm_instance))
3160 return;
3162 retry:
3163 for (i=0; i<dock->max_icons; i++) {
3164 icon = dock->icon_array[i];
3165 if (!icon)
3166 continue;
3168 /* app is already attached to icon */
3169 if (icon->main_window == window) {
3170 found = True;
3171 break;
3174 if ((icon->wm_instance || icon->wm_class)
3175 && (icon->launching || !icon->running)) {
3177 if (icon->wm_instance && wm_instance &&
3178 strcmp(icon->wm_instance, wm_instance)!=0) {
3179 continue;
3181 if (icon->wm_class && wm_class &&
3182 strcmp(icon->wm_class, wm_class)!=0) {
3183 continue;
3185 if (firstPass && command && strcmp(icon->command, command)!=0) {
3186 continue;
3189 if (!icon->relaunching) {
3190 WApplication *wapp;
3192 /* Possibly an application that was docked with dockit,
3193 * but the user did not update WMState to indicate that
3194 * it was docked by force */
3195 wapp = wApplicationOf(window);
3196 if (!wapp) {
3197 icon->forced_dock = 1;
3198 icon->running = 0;
3200 if (!icon->forced_dock)
3201 icon->main_window = window;
3204 found = True;
3205 if (!wPreferences.no_animations && !icon->launching &&
3206 !dock->screen_ptr->flags.startup && !dock->collapsed) {
3207 WAppIcon *aicon;
3208 int x0, y0;
3210 icon->launching = 1;
3211 dockIconPaint(icon);
3213 aicon = wAppIconCreateForDock(dock->screen_ptr, NULL,
3214 wm_instance, wm_class,
3215 TILE_NORMAL);
3216 PlaceIcon(dock->screen_ptr, &x0, &y0);
3217 wAppIconMove(aicon, x0, y0);
3218 /* Should this always be lowered? -Dan */
3219 if (dock->lowered)
3220 wLowerFrame(aicon->icon->core);
3221 XMapWindow(dpy, aicon->icon->core->window);
3222 aicon->launching = 1;
3223 wAppIconPaint(aicon);
3224 SlideWindow(aicon->icon->core->window, x0, y0,
3225 icon->x_pos, icon->y_pos);
3226 XUnmapWindow(dpy, aicon->icon->core->window);
3227 wAppIconDestroy(aicon);
3229 wDockFinishLaunch(dock, icon);
3230 break;
3234 if (firstPass && !found) {
3235 firstPass = False;
3236 goto retry;
3239 if (command)
3240 wfree(command);
3242 if (wm_class)
3243 XFree(wm_class);
3244 if (wm_instance)
3245 XFree(wm_instance);
3250 void
3251 wClipUpdateForWorkspaceChange(WScreen *scr, int workspace)
3253 if (!wPreferences.flags.noclip) {
3254 scr->clip_icon->dock = scr->workspaces[workspace]->clip;
3255 if (scr->current_workspace != workspace) {
3256 WDock *old_clip = scr->workspaces[scr->current_workspace]->clip;
3257 WAppIconChain *chain = scr->global_icons;
3259 while (chain) {
3260 moveIconBetweenDocks(chain->aicon->dock,
3261 scr->workspaces[workspace]->clip,
3262 chain->aicon, chain->aicon->xindex,
3263 chain->aicon->yindex);
3264 if (scr->workspaces[workspace]->clip->collapsed)
3265 XUnmapWindow(dpy, chain->aicon->icon->core->window);
3266 chain = chain->next;
3269 wDockHideIcons(old_clip);
3270 if (old_clip->auto_raise_lower) {
3271 if (old_clip->auto_raise_magic) {
3272 WMDeleteTimerHandler(old_clip->auto_raise_magic);
3273 old_clip->auto_raise_magic = NULL;
3275 wDockLower(old_clip);
3277 if (old_clip->auto_collapse) {
3278 if (old_clip->auto_expand_magic) {
3279 WMDeleteTimerHandler(old_clip->auto_expand_magic);
3280 old_clip->auto_expand_magic = NULL;
3282 old_clip->collapsed = 1;
3284 wDockShowIcons(scr->workspaces[workspace]->clip);
3286 if (scr->flags.clip_balloon_mapped)
3287 showClipBalloon(scr->clip_icon->dock, workspace);
3293 static void
3294 trackDeadProcess(pid_t pid, unsigned char status, WDock *dock)
3296 WAppIcon *icon;
3297 int i;
3299 for (i=0; i<dock->max_icons; i++) {
3300 icon = dock->icon_array[i];
3301 if (!icon)
3302 continue;
3304 if (icon->launching && icon->pid == pid) {
3305 if (!icon->relaunching) {
3306 icon->running = 0;
3307 icon->main_window = None;
3309 wDockFinishLaunch(dock, icon);
3310 icon->pid = 0;
3311 if (status==111) {
3312 char msg[PATH_MAX];
3313 char *cmd;
3315 if (icon->drop_launch)
3316 cmd = icon->dnd_command;
3317 else if (icon->paste_launch)
3318 cmd = icon->paste_command;
3319 else
3320 cmd = icon->command;
3322 snprintf(msg, sizeof(msg),
3323 _("Could not execute command \"%s\""), cmd);
3325 wMessageDialog(dock->screen_ptr, _("Error"), msg,
3326 _("OK"), NULL, NULL);
3328 break;
3334 static void
3335 toggleLowered(WDock *dock)
3337 WAppIcon *tmp;
3338 int newlevel, i;
3340 /* lower/raise Dock */
3341 if (!dock->lowered) {
3342 newlevel = WMNormalLevel;
3343 dock->lowered = 1;
3344 } else {
3345 newlevel = WMDockLevel;
3346 dock->lowered = 0;
3349 for (i=0; i<dock->max_icons; i++) {
3350 tmp = dock->icon_array[i];
3351 if (!tmp)
3352 continue;
3354 ChangeStackingLevel(tmp->icon->core, newlevel);
3355 if (dock->lowered)
3356 wLowerFrame(tmp->icon->core);
3359 if (dock->type == WM_DOCK)
3360 wScreenUpdateUsableArea(dock->screen_ptr);
3364 static void
3365 toggleCollapsed(WDock *dock)
3367 if (dock->collapsed) {
3368 dock->collapsed = 0;
3369 wDockShowIcons(dock);
3371 else {
3372 dock->collapsed = 1;
3373 wDockHideIcons(dock);
3378 static void
3379 openDockMenu(WDock *dock, WAppIcon *aicon, XEvent *event)
3381 WScreen *scr = dock->screen_ptr;
3382 WObjDescriptor *desc;
3383 WMenuEntry *entry;
3384 WApplication *wapp = NULL;
3385 int index = 0;
3386 int x_pos;
3387 int n_selected;
3388 int appIsRunning = aicon->running && aicon->icon && aicon->icon->owner;
3390 if (dock->type == WM_DOCK) {
3391 /* keep on top */
3392 entry = dock->menu->entries[index];
3393 entry->flags.indicator_on = !dock->lowered;
3394 entry->clientdata = dock;
3395 dock->menu->flags.realized = 0;
3396 } else {
3397 /* clip options */
3398 if (scr->clip_options)
3399 updateClipOptionsMenu(scr->clip_options, dock);
3401 n_selected = numberOfSelectedIcons(dock);
3403 /* Rename Workspace */
3404 entry = dock->menu->entries[++index];
3405 if (aicon == scr->clip_icon) {
3406 entry->callback = renameCallback;
3407 entry->clientdata = dock;
3408 entry->flags.indicator = 0;
3409 entry->text = _("Rename Workspace");
3410 } else {
3411 entry->callback = omnipresentCallback;
3412 entry->clientdata = aicon;
3413 if (n_selected > 0) {
3414 entry->flags.indicator = 0;
3415 entry->text = _("Toggle Omnipresent");
3416 } else {
3417 entry->flags.indicator = 1;
3418 entry->flags.indicator_on = aicon->omnipresent;
3419 entry->flags.indicator_type = MI_CHECK;
3420 entry->text = _("Omnipresent");
3424 /* select/unselect icon */
3425 entry = dock->menu->entries[++index];
3426 entry->clientdata = aicon;
3427 entry->flags.indicator_on = aicon->icon->selected;
3428 wMenuSetEnabled(dock->menu, index, aicon!=scr->clip_icon);
3430 /* select/unselect all icons */
3431 entry = dock->menu->entries[++index];
3432 entry->clientdata = aicon;
3433 if (n_selected > 0)
3434 entry->text = _("Unselect All Icons");
3435 else
3436 entry->text = _("Select All Icons");
3437 wMenuSetEnabled(dock->menu, index, dock->icon_count > 1);
3439 /* keep icon(s) */
3440 entry = dock->menu->entries[++index];
3441 entry->clientdata = aicon;
3442 if (n_selected > 1)
3443 entry->text = _("Keep Icons");
3444 else
3445 entry->text = _("Keep Icon");
3446 wMenuSetEnabled(dock->menu, index, dock->icon_count > 1);
3448 /* this is the workspace submenu part */
3449 entry = dock->menu->entries[++index];
3450 if (n_selected > 1)
3451 entry->text = _("Move Icons To");
3452 else
3453 entry->text = _("Move Icon To");
3454 if (scr->clip_submenu)
3455 updateWorkspaceMenu(scr->clip_submenu, aicon);
3456 wMenuSetEnabled(dock->menu, index, !aicon->omnipresent);
3458 /* remove icon(s) */
3459 entry = dock->menu->entries[++index];
3460 entry->clientdata = aicon;
3461 if (n_selected > 1)
3462 entry->text = _("Remove Icons");
3463 else
3464 entry->text = _("Remove Icon");
3465 wMenuSetEnabled(dock->menu, index, dock->icon_count > 1);
3467 /* attract icon(s) */
3468 entry = dock->menu->entries[++index];
3469 entry->clientdata = aicon;
3471 dock->menu->flags.realized = 0;
3472 wMenuRealize(dock->menu);
3476 if (aicon->icon->owner) {
3477 wapp = wApplicationOf(aicon->icon->owner->main_window);
3478 } else {
3479 wapp = NULL;
3482 /* launch */
3483 entry = dock->menu->entries[++index];
3484 entry->clientdata = aicon;
3485 wMenuSetEnabled(dock->menu, index, aicon->command!=NULL);
3487 /* unhide here */
3488 entry = dock->menu->entries[++index];
3489 entry->clientdata = aicon;
3490 if (wapp && wapp->flags.hidden) {
3491 entry->text = _("Unhide Here");
3492 } else {
3493 entry->text = _("Bring Here");
3495 wMenuSetEnabled(dock->menu, index, appIsRunning);
3497 /* hide */
3498 entry = dock->menu->entries[++index];
3499 entry->clientdata = aicon;
3500 if (wapp && wapp->flags.hidden) {
3501 entry->text = _("Unhide");
3502 } else {
3503 entry->text = _("Hide");
3505 wMenuSetEnabled(dock->menu, index, appIsRunning);
3507 /* settings */
3508 entry = dock->menu->entries[++index];
3509 entry->clientdata = aicon;
3510 wMenuSetEnabled(dock->menu, index, !aicon->editing
3511 && !wPreferences.flags.noupdates);
3513 /* kill */
3514 entry = dock->menu->entries[++index];
3515 entry->clientdata = aicon;
3516 wMenuSetEnabled(dock->menu, index, appIsRunning);
3518 if (!dock->menu->flags.realized)
3519 wMenuRealize(dock->menu);
3521 if (dock->type == WM_CLIP) {
3522 x_pos = event->xbutton.x_root+2;
3523 } else {
3524 x_pos = dock->on_right_side ?
3525 scr->scr_width - dock->menu->frame->core->width - 2 : 0;
3528 wMenuMapAt(dock->menu, x_pos, event->xbutton.y_root+2, False);
3530 /* allow drag select */
3531 event->xany.send_event = True;
3532 desc = &dock->menu->menu->descriptor;
3533 (*desc->handle_mousedown)(desc, event);
3537 static void
3538 openClipWorkspaceMenu(WScreen *scr, int x, int y)
3540 if (!scr->clip_ws_menu) {
3541 scr->clip_ws_menu = wWorkspaceMenuMake(scr, False);
3543 wWorkspaceMenuUpdate(scr, scr->clip_ws_menu);
3544 wMenuMapAt(scr->clip_ws_menu, x, y, False);
3548 /******************************************************************/
3549 static void
3550 iconDblClick(WObjDescriptor *desc, XEvent *event)
3552 WAppIcon *btn = desc->parent;
3553 WDock *dock = btn->dock;
3554 WApplication *wapp = NULL;
3555 int unhideHere = 0;
3557 if (btn->icon->owner && !(event->xbutton.state & ControlMask)) {
3558 wapp = wApplicationOf(btn->icon->owner->main_window);
3560 assert(wapp!=NULL);
3562 unhideHere = (event->xbutton.state & ShiftMask);
3564 /* go to the last workspace that the user worked on the app */
3565 if (wapp->last_workspace != dock->screen_ptr->current_workspace
3566 && !unhideHere) {
3567 wWorkspaceChange(dock->screen_ptr, wapp->last_workspace);
3570 wUnhideApplication(wapp, event->xbutton.button==Button2,
3571 unhideHere);
3573 if (event->xbutton.state & MOD_MASK) {
3574 wHideOtherApplications(btn->icon->owner);
3576 } else {
3577 if (event->xbutton.button==Button1) {
3579 if (event->xbutton.state & MOD_MASK) {
3580 /* raise/lower dock */
3581 toggleLowered(dock);
3582 } else if (btn == dock->screen_ptr->clip_icon) {
3583 if (getClipButton(event->xbutton.x, event->xbutton.y)==CLIP_IDLE)
3584 toggleCollapsed(dock);
3585 else
3586 handleClipChangeWorkspace(dock->screen_ptr, event);
3587 } else if (btn->command) {
3588 if (!btn->launching &&
3589 (!btn->running || (event->xbutton.state & ControlMask))) {
3590 launchDockedApplication(btn, False);
3592 } else if (btn->xindex == 0 && btn->yindex == 0
3593 && btn->dock->type == WM_DOCK) {
3595 wShowGNUstepPanel(dock->screen_ptr);
3603 static void
3604 handleDockMove(WDock *dock, WAppIcon *aicon, XEvent *event)
3606 WScreen *scr = dock->screen_ptr;
3607 int ofs_x=event->xbutton.x, ofs_y=event->xbutton.y;
3608 int x, y;
3609 XEvent ev;
3610 int grabbed = 0, swapped = 0, done;
3611 Pixmap ghost = None;
3612 int superfluous = wPreferences.superfluous; /* we catch it to avoid problems */
3614 #ifdef DEBUG
3615 puts("moving dock");
3616 #endif
3617 if (XGrabPointer(dpy, aicon->icon->core->window, True, ButtonMotionMask
3618 |ButtonReleaseMask|ButtonPressMask, GrabModeAsync,
3619 GrabModeAsync, None, None, CurrentTime) !=GrabSuccess) {
3620 wwarning("pointer grab failed for dock move");
3622 y = 0;
3623 for (x=0; x<dock->max_icons; x++) {
3624 if (dock->icon_array[x]!=NULL &&
3625 dock->icon_array[x]->yindex > y)
3626 y = dock->icon_array[x]->yindex;
3628 y++;
3629 XResizeWindow(dpy, scr->dock_shadow, ICON_SIZE, ICON_SIZE*y);
3631 done = 0;
3632 while (!done) {
3633 WMMaskEvent(dpy, PointerMotionMask|ButtonReleaseMask|ButtonPressMask
3634 |ButtonMotionMask|ExposureMask, &ev);
3635 switch (ev.type) {
3636 case Expose:
3637 WMHandleEvent(&ev);
3638 break;
3640 case MotionNotify:
3641 if (!grabbed) {
3642 if (abs(ofs_x-ev.xmotion.x)>=MOVE_THRESHOLD
3643 || abs(ofs_y-ev.xmotion.y)>=MOVE_THRESHOLD) {
3644 XChangeActivePointerGrab(dpy, ButtonMotionMask
3645 |ButtonReleaseMask|ButtonPressMask,
3646 wCursor[WCUR_MOVE], CurrentTime);
3647 grabbed=1;
3649 break;
3651 if (dock->type == WM_CLIP) {
3652 if (ev.xmotion.x_root - ofs_x < 0) {
3653 x = 0;
3654 } else if (ev.xmotion.x_root - ofs_x + ICON_SIZE >
3655 scr->scr_width) {
3656 x = scr->scr_width - ICON_SIZE;
3657 } else {
3658 x = ev.xmotion.x_root - ofs_x;
3660 if (ev.xmotion.y_root - ofs_y < 0) {
3661 y = 0;
3662 } else if (ev.xmotion.y_root - ofs_y + ICON_SIZE >
3663 scr->scr_height) {
3664 y = scr->scr_height - ICON_SIZE;
3665 } else {
3666 y = ev.xmotion.y_root - ofs_y;
3668 moveDock(dock, x, y);
3669 } else {
3670 /* move vertically if pointer is inside the dock*/
3671 if ((dock->on_right_side &&
3672 ev.xmotion.x_root >= dock->x_pos - ICON_SIZE)
3673 || (!dock->on_right_side &&
3674 ev.xmotion.x_root <= dock->x_pos + ICON_SIZE*2)) {
3676 if (ev.xmotion.y_root - ofs_y < 0) {
3677 y = 0;
3678 } else if (ev.xmotion.y_root - ofs_y + ICON_SIZE >
3679 scr->scr_height) {
3680 y = scr->scr_height - ICON_SIZE;
3681 } else {
3682 y = ev.xmotion.y_root - ofs_y;
3684 moveDock(dock, dock->x_pos, y);
3686 /* move horizontally to change sides */
3687 x = ev.xmotion.x_root - ofs_x;
3688 if (!dock->on_right_side) {
3690 /* is on left */
3692 if (ev.xmotion.x_root > dock->x_pos + ICON_SIZE*2) {
3693 XMoveWindow(dpy, scr->dock_shadow, scr->scr_width-ICON_SIZE
3694 -DOCK_EXTRA_SPACE-1, dock->y_pos);
3695 if (superfluous && ghost==None) {
3696 ghost = MakeGhostDock(dock, dock->x_pos,
3697 scr->scr_width-ICON_SIZE
3698 -DOCK_EXTRA_SPACE-1,
3699 dock->y_pos);
3700 XSetWindowBackgroundPixmap(dpy, scr->dock_shadow,
3701 ghost);
3702 XClearWindow(dpy, scr->dock_shadow);
3704 XMapRaised(dpy, scr->dock_shadow);
3705 swapped = 1;
3706 } else {
3707 if (superfluous && ghost!=None) {
3708 XFreePixmap(dpy, ghost);
3709 ghost = None;
3711 XUnmapWindow(dpy, scr->dock_shadow);
3712 swapped = 0;
3714 } else {
3715 /* is on right */
3716 if (ev.xmotion.x_root < dock->x_pos - ICON_SIZE) {
3717 XMoveWindow(dpy, scr->dock_shadow,
3718 DOCK_EXTRA_SPACE, dock->y_pos);
3719 if (superfluous && ghost==None) {
3720 ghost = MakeGhostDock(dock, dock->x_pos,
3721 DOCK_EXTRA_SPACE, dock->y_pos);
3722 XSetWindowBackgroundPixmap(dpy, scr->dock_shadow,
3723 ghost);
3724 XClearWindow(dpy, scr->dock_shadow);
3726 XMapRaised(dpy, scr->dock_shadow);
3727 swapped = -1;
3728 } else {
3729 XUnmapWindow(dpy, scr->dock_shadow);
3730 swapped = 0;
3731 if (superfluous && ghost!=None) {
3732 XFreePixmap(dpy, ghost);
3733 ghost = None;
3738 break;
3740 case ButtonPress:
3741 break;
3743 case ButtonRelease:
3744 if (ev.xbutton.button != event->xbutton.button)
3745 break;
3746 XUngrabPointer(dpy, CurrentTime);
3747 XUnmapWindow(dpy, scr->dock_shadow);
3748 XResizeWindow(dpy, scr->dock_shadow, ICON_SIZE, ICON_SIZE);
3749 if (dock->type == WM_DOCK) {
3750 if (swapped!=0) {
3751 if (swapped>0)
3752 dock->on_right_side = 1;
3753 else
3754 dock->on_right_side = 0;
3755 swapDock(dock);
3756 wArrangeIcons(scr, False);
3759 done = 1;
3760 break;
3763 if (superfluous) {
3764 if (ghost!=None)
3765 XFreePixmap(dpy, ghost);
3766 XSetWindowBackground(dpy, scr->dock_shadow, scr->white_pixel);
3768 #ifdef DEBUG
3769 puts("End dock move");
3770 #endif
3775 static void
3776 handleIconMove(WDock *dock, WAppIcon *aicon, XEvent *event)
3778 WScreen *scr = dock->screen_ptr;
3779 Window wins[2];
3780 WIcon *icon = aicon->icon;
3781 WDock *dock2 = NULL, *last_dock = dock, *clip = NULL;
3782 int ondock, grabbed = 0, change_dock = 0, collapsed = 0;
3783 XEvent ev;
3784 int x = aicon->x_pos, y = aicon->y_pos;
3785 int ofs_x = event->xbutton.x, ofs_y = event->xbutton.y;
3786 int shad_x = x, shad_y = y;
3787 int ix = aicon->xindex, iy = aicon->yindex;
3788 int tmp;
3789 Pixmap ghost = None;
3790 Bool docked;
3791 int superfluous = wPreferences.superfluous; /* we catch it to avoid problems */
3792 int omnipresent = aicon->omnipresent; /* this must be cached!!! */
3795 if (wPreferences.flags.noupdates)
3796 return;
3798 if (XGrabPointer(dpy, icon->core->window, True, ButtonMotionMask
3799 |ButtonReleaseMask|ButtonPressMask, GrabModeAsync,
3800 GrabModeAsync, None, None, CurrentTime) !=GrabSuccess) {
3801 #ifdef DEBUG0
3802 wwarning("pointer grab failed for icon move");
3803 #endif
3806 if (!(event->xbutton.state & MOD_MASK))
3807 wRaiseFrame(icon->core);
3809 if (!wPreferences.flags.noclip)
3810 clip = scr->workspaces[scr->current_workspace]->clip;
3812 if (dock == scr->dock && !wPreferences.flags.noclip)
3813 dock2 = clip;
3814 else if (dock != scr->dock && !wPreferences.flags.nodock)
3815 dock2 = scr->dock;
3817 wins[0] = icon->core->window;
3818 wins[1] = scr->dock_shadow;
3819 XRestackWindows(dpy, wins, 2);
3820 XMoveResizeWindow(dpy, scr->dock_shadow, aicon->x_pos, aicon->y_pos,
3821 ICON_SIZE, ICON_SIZE);
3822 if (superfluous) {
3823 if (icon->pixmap!=None)
3824 ghost = MakeGhostIcon(scr, icon->pixmap);
3825 else
3826 ghost = MakeGhostIcon(scr, icon->core->window);
3828 XSetWindowBackgroundPixmap(dpy, scr->dock_shadow, ghost);
3829 XClearWindow(dpy, scr->dock_shadow);
3831 XMapWindow(dpy, scr->dock_shadow);
3833 ondock = 1;
3836 while(1) {
3837 XMaskEvent(dpy, PointerMotionMask|ButtonReleaseMask|ButtonPressMask
3838 |ButtonMotionMask|ExposureMask, &ev);
3839 switch (ev.type) {
3840 case Expose:
3841 WMHandleEvent(&ev);
3842 break;
3844 case MotionNotify:
3845 if (!grabbed) {
3846 if (abs(ofs_x-ev.xmotion.x)>=MOVE_THRESHOLD
3847 || abs(ofs_y-ev.xmotion.y)>=MOVE_THRESHOLD) {
3848 XChangeActivePointerGrab(dpy, ButtonMotionMask
3849 |ButtonReleaseMask|ButtonPressMask,
3850 wCursor[WCUR_MOVE], CurrentTime);
3851 grabbed=1;
3852 } else {
3853 break;
3857 if (omnipresent) {
3858 int i;
3859 for (i=0; i<scr->workspace_count; i++) {
3860 if (i == scr->current_workspace)
3861 continue;
3862 wDockShowIcons(scr->workspaces[i]->clip);
3866 x = ev.xmotion.x_root - ofs_x;
3867 y = ev.xmotion.y_root - ofs_y;
3868 tmp = wDockSnapIcon(dock, aicon, x, y, &ix, &iy, True);
3869 if (tmp && dock2) {
3870 change_dock = 0;
3871 if (last_dock != dock && collapsed) {
3872 last_dock->collapsed = 1;
3873 wDockHideIcons(last_dock);
3874 collapsed = 0;
3876 if (!collapsed && (collapsed = dock->collapsed)) {
3877 dock->collapsed = 0;
3878 wDockShowIcons(dock);
3880 if (dock->auto_raise_lower)
3881 wDockRaise(dock);
3882 last_dock = dock;
3883 } else if (dock2) {
3884 tmp = wDockSnapIcon(dock2, aicon, x, y, &ix, &iy, False);
3885 if (tmp) {
3886 change_dock = 1;
3887 if (last_dock != dock2 && collapsed) {
3888 last_dock->collapsed = 1;
3889 wDockHideIcons(last_dock);
3890 collapsed = 0;
3892 if (!collapsed && (collapsed = dock2->collapsed)) {
3893 dock2->collapsed = 0;
3894 wDockShowIcons(dock2);
3896 if (dock2->auto_raise_lower)
3897 wDockRaise(dock2);
3898 last_dock = dock2;
3901 if (aicon->launching
3902 || aicon->lock
3903 || (aicon->running && !(ev.xmotion.state & MOD_MASK))
3904 || (!aicon->running && tmp)) {
3905 shad_x = last_dock->x_pos + ix*wPreferences.icon_size;
3906 shad_y = last_dock->y_pos + iy*wPreferences.icon_size;
3908 XMoveWindow(dpy, scr->dock_shadow, shad_x, shad_y);
3910 if (!ondock) {
3911 XMapWindow(dpy, scr->dock_shadow);
3913 ondock = 1;
3914 } else {
3915 if (ondock) {
3916 XUnmapWindow(dpy, scr->dock_shadow);
3918 ondock = 0;
3920 XMoveWindow(dpy, icon->core->window, x, y);
3921 break;
3923 case ButtonPress:
3924 break;
3926 case ButtonRelease:
3927 if (ev.xbutton.button != event->xbutton.button)
3928 break;
3929 XUngrabPointer(dpy, CurrentTime);
3930 if (ondock) {
3931 SlideWindow(icon->core->window, x, y, shad_x, shad_y);
3932 XUnmapWindow(dpy, scr->dock_shadow);
3933 if (!change_dock) {
3934 reattachIcon(dock, aicon, ix, iy);
3935 if (clip && dock!=clip && clip->auto_raise_lower)
3936 wDockLower(clip);
3937 } else {
3938 docked = moveIconBetweenDocks(dock, dock2, aicon, ix, iy);
3939 if (!docked) {
3940 /* Slide it back if dock rejected it */
3941 SlideWindow(icon->core->window, x, y, aicon->x_pos,
3942 aicon->y_pos);
3943 reattachIcon(dock, aicon, aicon->xindex,aicon->yindex);
3945 if (last_dock->type==WM_CLIP && last_dock->auto_collapse) {
3946 collapsed = 0;
3949 } else {
3950 aicon->x_pos = x;
3951 aicon->y_pos = y;
3952 if (superfluous) {
3953 if (!aicon->running && !wPreferences.no_animations) {
3954 /* We need to deselect it, even if is deselected in
3955 * wDockDetach(), because else DoKaboom() will fail.
3957 if (aicon->icon->selected)
3958 wIconSelect(aicon->icon);
3960 wSoundPlay(WSOUND_KABOOM);
3961 DoKaboom(scr,aicon->icon->core->window, x, y);
3962 } else {
3963 wSoundPlay(WSOUND_UNDOCK);
3965 } else {
3966 wSoundPlay(WSOUND_UNDOCK);
3968 if (clip && clip->auto_raise_lower)
3969 wDockLower(clip);
3970 wDockDetach(dock, aicon);
3972 if (collapsed) {
3973 last_dock->collapsed = 1;
3974 wDockHideIcons(last_dock);
3975 collapsed = 0;
3977 if (superfluous) {
3978 if (ghost!=None)
3979 XFreePixmap(dpy, ghost);
3980 XSetWindowBackground(dpy, scr->dock_shadow, scr->white_pixel);
3982 if (omnipresent) {
3983 int i;
3984 for (i=0; i<scr->workspace_count; i++) {
3985 if (i == scr->current_workspace)
3986 continue;
3987 wDockHideIcons(scr->workspaces[i]->clip);
3991 #ifdef DEBUG
3992 puts("End icon move");
3993 #endif
3994 return;
4000 static int
4001 getClipButton(int px, int py)
4003 int pt = (CLIP_BUTTON_SIZE+2)*ICON_SIZE/64;
4005 if (px < 0 || py < 0 || px >= ICON_SIZE || py >= ICON_SIZE)
4006 return CLIP_IDLE;
4008 if (py <= pt-((int)ICON_SIZE-1-px))
4009 return CLIP_FORWARD;
4010 else if (px <= pt-((int)ICON_SIZE-1-py))
4011 return CLIP_REWIND;
4013 return CLIP_IDLE;
4017 static void
4018 handleClipChangeWorkspace(WScreen *scr, XEvent *event)
4020 XEvent ev;
4021 int done, direction, new_ws;
4022 int new_dir;
4023 WDock *clip = scr->clip_icon->dock;
4025 direction = getClipButton(event->xbutton.x, event->xbutton.y);
4027 clip->lclip_button_pushed = direction==CLIP_REWIND;
4028 clip->rclip_button_pushed = direction==CLIP_FORWARD;
4030 wClipIconPaint(scr->clip_icon);
4031 done = 0;
4032 while(!done) {
4033 WMMaskEvent(dpy, ExposureMask|ButtonMotionMask|ButtonReleaseMask
4034 |ButtonPressMask, &ev);
4035 switch (ev.type) {
4036 case Expose:
4037 WMHandleEvent(&ev);
4038 break;
4040 case MotionNotify:
4041 new_dir = getClipButton(ev.xmotion.x, ev.xmotion.y);
4042 if (new_dir != direction) {
4043 direction = new_dir;
4044 clip->lclip_button_pushed = direction==CLIP_REWIND;
4045 clip->rclip_button_pushed = direction==CLIP_FORWARD;
4046 wClipIconPaint(scr->clip_icon);
4048 break;
4050 case ButtonPress:
4051 break;
4053 case ButtonRelease:
4054 if (ev.xbutton.button == event->xbutton.button)
4055 done = 1;
4059 clip->lclip_button_pushed = 0;
4060 clip->rclip_button_pushed = 0;
4062 new_ws = wPreferences.ws_advance || (event->xbutton.state & ControlMask);
4064 if (direction == CLIP_FORWARD) {
4065 if (scr->current_workspace < scr->workspace_count-1)
4066 wWorkspaceChange(scr, scr->current_workspace+1);
4067 else if (new_ws && scr->current_workspace < MAX_WORKSPACES-1)
4068 wWorkspaceChange(scr, scr->current_workspace+1);
4069 else if (wPreferences.ws_cycle)
4070 wWorkspaceChange(scr, 0);
4072 else if (direction == CLIP_REWIND) {
4073 if (scr->current_workspace > 0)
4074 wWorkspaceChange(scr, scr->current_workspace-1);
4075 else if (scr->current_workspace==0 && wPreferences.ws_cycle)
4076 wWorkspaceChange(scr, scr->workspace_count-1);
4079 wClipIconPaint(scr->clip_icon);
4083 static void
4084 iconMouseDown(WObjDescriptor *desc, XEvent *event)
4086 WAppIcon *aicon = desc->parent;
4087 WDock *dock = aicon->dock;
4088 WScreen *scr = aicon->icon->core->screen_ptr;
4090 if (aicon->editing || WCHECK_STATE(WSTATE_MODAL))
4091 return;
4093 scr->last_dock = dock;
4095 if (dock->menu->flags.mapped)
4096 wMenuUnmap(dock->menu);
4098 if (IsDoubleClick(scr, event)) {
4099 /* double-click was not in the main clip icon */
4100 if (dock->type != WM_CLIP || aicon->xindex!=0 || aicon->yindex!=0
4101 || getClipButton(event->xbutton.x, event->xbutton.y)==CLIP_IDLE) {
4102 iconDblClick(desc, event);
4103 return;
4107 if (dock->type == WM_CLIP && scr->flags.clip_balloon_mapped) {
4108 XUnmapWindow(dpy, scr->clip_balloon);
4109 scr->flags.clip_balloon_mapped = 0;
4112 #ifdef DEBUG
4113 puts("handling dock");
4114 #endif
4115 if (event->xbutton.button == Button1) {
4116 if (event->xbutton.state & MOD_MASK)
4117 wDockLower(dock);
4118 else
4119 wDockRaise(dock);
4121 if ((event->xbutton.state & ShiftMask) && aicon!=scr->clip_icon &&
4122 dock->type!=WM_DOCK) {
4123 wIconSelect(aicon->icon);
4124 return;
4127 if (aicon->yindex==0 && aicon->xindex==0) {
4128 if (getClipButton(event->xbutton.x, event->xbutton.y)!=CLIP_IDLE
4129 && dock->type==WM_CLIP)
4130 handleClipChangeWorkspace(scr, event);
4131 else
4132 handleDockMove(dock, aicon, event);
4133 } else
4134 handleIconMove(dock, aicon, event);
4136 } else if (event->xbutton.button==Button2 && dock->type==WM_CLIP &&
4137 aicon==scr->clip_icon) {
4138 openClipWorkspaceMenu(scr, event->xbutton.x_root+2,
4139 event->xbutton.y_root+2);
4140 if (scr->clip_ws_menu) {
4141 WMenu *menu;
4142 menu = scr->clip_ws_menu;
4143 desc = &menu->menu->descriptor;
4145 event->xany.send_event = True;
4146 (*desc->handle_mousedown)(desc, event);
4148 } else if (event->xbutton.button==Button2 && dock->type==WM_CLIP &&
4149 (event->xbutton.state & ShiftMask) && aicon!=scr->clip_icon) {
4150 wClipMakeIconOmnipresent(aicon, !aicon->omnipresent);
4151 } else if (event->xbutton.button == Button3) {
4152 if (event->xbutton.send_event &&
4153 XGrabPointer(dpy, aicon->icon->core->window, True, ButtonMotionMask
4154 |ButtonReleaseMask|ButtonPressMask, GrabModeAsync,
4155 GrabModeAsync, None, None, CurrentTime) !=GrabSuccess) {
4156 wwarning("pointer grab failed for dockicon menu");
4157 return;
4160 openDockMenu(dock, aicon, event);
4161 } else if (event->xbutton.button == Button2) {
4162 WAppIcon *btn = desc->parent;
4164 if (!btn->launching &&
4165 (!btn->running || (event->xbutton.state & ControlMask))) {
4166 launchDockedApplication(btn, True);
4172 static void
4173 showClipBalloon(WDock *dock, int workspace)
4175 int w, h;
4176 int x, y;
4177 WScreen *scr = dock->screen_ptr;
4178 char *text;
4179 Window stack[2];
4181 scr->flags.clip_balloon_mapped = 1;
4182 XMapWindow(dpy, scr->clip_balloon);
4184 text = scr->workspaces[workspace]->name;
4186 w = WMWidthOfString(scr->clip_title_font, text, strlen(text));
4188 h = WMFontHeight(scr->clip_title_font);
4189 XResizeWindow(dpy, scr->clip_balloon, w, h);
4191 x = dock->x_pos + CLIP_BUTTON_SIZE*ICON_SIZE/64;
4192 y = dock->y_pos + ICON_SIZE - WMFontHeight(scr->clip_title_font) - 3;
4194 if (x+w > scr->scr_width) {
4195 x = scr->scr_width - w;
4196 if (dock->y_pos + ICON_SIZE + h > scr->scr_height)
4197 y = dock->y_pos - h - 1;
4198 else
4199 y = dock->y_pos + ICON_SIZE;
4200 XRaiseWindow(dpy, scr->clip_balloon);
4201 } else {
4202 stack[0] = scr->clip_icon->icon->core->window;
4203 stack[1] = scr->clip_balloon;
4204 XRestackWindows(dpy, stack, 2);
4206 XMoveWindow(dpy, scr->clip_balloon, x, y);
4207 XSetForeground(dpy, scr->clip_title_gc,
4208 scr->clip_title_pixel[CLIP_NORMAL]);
4209 XClearWindow(dpy, scr->clip_balloon);
4210 WMDrawString(scr->wmscreen, scr->clip_balloon, scr->clip_title_gc,
4211 scr->clip_title_font, 0, 0, text, strlen(text));
4215 static void
4216 clipEnterNotify(WObjDescriptor *desc, XEvent *event)
4218 WAppIcon *btn = (WAppIcon*)desc->parent;
4219 WDock *dock;
4220 WScreen *scr;
4222 assert(event->type==EnterNotify);
4224 if(desc->parent_type!=WCLASS_DOCK_ICON)
4225 return;
4227 scr = btn->icon->core->screen_ptr;
4228 if (!btn->omnipresent)
4229 dock = btn->dock;
4230 else
4231 dock = scr->workspaces[scr->current_workspace]->clip;
4233 if (!dock || dock->type!=WM_CLIP)
4234 return;
4236 /* The auto raise/lower code */
4237 if (dock->auto_lower_magic) {
4238 WMDeleteTimerHandler(dock->auto_lower_magic);
4239 dock->auto_lower_magic = NULL;
4241 if (dock->auto_raise_lower && !dock->auto_raise_magic) {
4242 dock->auto_raise_magic = WMAddTimerHandler(AUTO_RAISE_DELAY,
4243 clipAutoRaise,
4244 (void *)dock);
4247 /* The auto expand/collapse code */
4248 if (dock->auto_collapse_magic) {
4249 WMDeleteTimerHandler(dock->auto_collapse_magic);
4250 dock->auto_collapse_magic = NULL;
4252 if (dock->auto_collapse && !dock->auto_expand_magic) {
4253 dock->auto_expand_magic = WMAddTimerHandler(AUTO_EXPAND_DELAY,
4254 clipAutoExpand,
4255 (void *)dock);
4258 if (btn->xindex == 0 && btn->yindex == 0)
4259 showClipBalloon(dock, dock->screen_ptr->current_workspace);
4260 else {
4261 if (dock->screen_ptr->flags.clip_balloon_mapped) {
4262 XUnmapWindow(dpy, dock->screen_ptr->clip_balloon);
4263 dock->screen_ptr->flags.clip_balloon_mapped = 0;
4269 static void
4270 clipLeave(WDock *dock)
4272 XEvent event;
4273 WObjDescriptor *desc = NULL;
4275 if (!dock || dock->type!=WM_CLIP)
4276 return;
4278 if (XCheckTypedEvent(dpy, EnterNotify, &event)!=False) {
4279 if (XFindContext(dpy, event.xcrossing.window, wWinContext,
4280 (XPointer *)&desc)!=XCNOENT
4281 && desc && desc->parent_type==WCLASS_DOCK_ICON
4282 && ((WAppIcon*)desc->parent)->dock
4283 && ((WAppIcon*)desc->parent)->dock->type==WM_CLIP) {
4284 /* We didn't left the Clip yet */
4285 XPutBackEvent(dpy, &event);
4286 return;
4289 XPutBackEvent(dpy, &event);
4290 } else {
4291 /* We entered a withdrawn window, so we're still in Clip */
4292 return;
4295 if (dock->auto_raise_magic) {
4296 WMDeleteTimerHandler(dock->auto_raise_magic);
4297 dock->auto_raise_magic = NULL;
4299 if (dock->auto_raise_lower && !dock->auto_lower_magic) {
4300 dock->auto_lower_magic = WMAddTimerHandler(AUTO_LOWER_DELAY,
4301 clipAutoLower,
4302 (void *)dock);
4305 if (dock->auto_expand_magic) {
4306 WMDeleteTimerHandler(dock->auto_expand_magic);
4307 dock->auto_expand_magic = NULL;
4309 if (dock->auto_collapse && !dock->auto_collapse_magic) {
4310 dock->auto_collapse_magic = WMAddTimerHandler(AUTO_COLLAPSE_DELAY,
4311 clipAutoCollapse,
4312 (void *)dock);
4317 static void
4318 clipLeaveNotify(WObjDescriptor *desc, XEvent *event)
4320 WAppIcon *btn = (WAppIcon*)desc->parent;
4322 assert(event->type==LeaveNotify);
4324 if(desc->parent_type!=WCLASS_DOCK_ICON)
4325 return;
4327 clipLeave(btn->dock);
4331 static void
4332 clipAutoCollapse(void *cdata)
4334 WDock *dock = (WDock *)cdata;
4336 if (dock->type!=WM_CLIP)
4337 return;
4339 if (dock->auto_collapse) {
4340 dock->collapsed = 1;
4341 wDockHideIcons(dock);
4343 dock->auto_collapse_magic = NULL;
4347 static void
4348 clipAutoExpand(void *cdata)
4350 WDock *dock = (WDock *)cdata;
4352 if (dock->type!=WM_CLIP)
4353 return;
4355 if (dock->auto_collapse) {
4356 dock->collapsed = 0;
4357 wDockShowIcons(dock);
4359 dock->auto_expand_magic = NULL;
4363 static void
4364 clipAutoLower(void *cdata)
4366 WDock *dock = (WDock *)cdata;
4368 if (dock->type!=WM_CLIP)
4369 return;
4371 if (dock->auto_raise_lower)
4372 wDockLower(dock);
4374 dock->auto_lower_magic = NULL;
4378 static void
4379 clipAutoRaise(void *cdata)
4381 WDock *dock = (WDock *)cdata;
4383 if (dock->type!=WM_CLIP)
4384 return;
4386 if (dock->auto_raise_lower)
4387 wDockRaise(dock);
4389 if (dock->screen_ptr->flags.clip_balloon_mapped) {
4390 showClipBalloon(dock, dock->screen_ptr->current_workspace);
4393 dock->auto_raise_magic = NULL;
4397 static Bool
4398 iconCanBeOmnipresent(WAppIcon *aicon)
4400 WScreen *scr = aicon->icon->core->screen_ptr;
4401 WDock *clip;
4402 WAppIcon *btn;
4403 int i, j;
4405 for (i=0; i<scr->workspace_count; i++) {
4406 clip = scr->workspaces[i]->clip;
4408 if (clip == aicon->dock)
4409 continue;
4411 if (clip->icon_count + scr->global_icon_count >= clip->max_icons)
4412 return False; /* Clip is full in some workspace */
4414 for (j=0; j<clip->max_icons; j++) {
4415 btn = clip->icon_array[j];
4416 if(btn && btn->xindex==aicon->xindex && btn->yindex==aicon->yindex)
4417 return False;
4421 return True;
4426 wClipMakeIconOmnipresent(WAppIcon *aicon, int omnipresent)
4428 WScreen *scr = aicon->icon->core->screen_ptr;
4429 WAppIconChain *new_entry, *tmp, *tmp1;
4430 int status = WO_SUCCESS;
4432 if ((scr->dock && aicon->dock==scr->dock) || aicon==scr->clip_icon) {
4433 return WO_NOT_APPLICABLE;
4436 if (aicon->omnipresent == omnipresent)
4437 return WO_SUCCESS;
4439 if (omnipresent) {
4440 if (iconCanBeOmnipresent(aicon)) {
4441 aicon->omnipresent = 1;
4442 new_entry = wmalloc(sizeof(WAppIconChain));
4443 new_entry->aicon = aicon;
4444 new_entry->next = scr->global_icons;
4445 scr->global_icons = new_entry;
4446 scr->global_icon_count++;
4447 } else {
4448 aicon->omnipresent = 0;
4449 status = WO_FAILED;
4451 } else {
4452 aicon->omnipresent = 0;
4453 if (aicon == scr->global_icons->aicon) {
4454 tmp = scr->global_icons->next;
4455 wfree(scr->global_icons);
4456 scr->global_icons = tmp;
4457 scr->global_icon_count--;
4458 } else {
4459 tmp = scr->global_icons;
4460 while (tmp->next) {
4461 if (tmp->next->aicon == aicon) {
4462 tmp1 = tmp->next->next;
4463 wfree(tmp->next);
4464 tmp->next = tmp1;
4465 scr->global_icon_count--;
4466 break;
4468 tmp = tmp->next;
4473 wAppIconPaint(aicon);
4475 return status;