clip arrow bevel
[wmaker-crm.git] / src / dock.c
blob8facc8bda6781a27e6585de13316d7c30161783a
1 /* dock.c- built-in Dock module for WindowMaker
2 *
3 * Window Maker window manager
4 *
5 * Copyright (c) 1997, 1998 Alfredo K. Kojima
6 * Copyright (c) 1998, 1999 Dan Pascu
7 *
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"
57 #include "list.h"
59 #include <proplist.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 proplist_t dCommand=NULL;
101 #ifdef OFFIX_DND
102 static proplist_t dDropCommand=NULL;
103 #endif
104 static proplist_t dAutoLaunch, dName, dForced, dBuggyApplication, dYes, dNo;
105 static proplist_t dHost, dDock, dClip;
106 static proplist_t dAutoAttractIcons, dKeepAttracted;
108 static proplist_t dPosition, dApplications, dLowered, dCollapsed, dAutoCollapse;
110 static proplist_t dAutoRaiseLower;
112 static void dockIconPaint(WAppIcon *btn);
114 static void iconMouseDown(WObjDescriptor *desc, XEvent *event);
116 static pid_t execCommand(WAppIcon *btn, char *command, WSavedState *state);
118 static void trackDeadProcess(pid_t pid, unsigned char status, WDock *dock);
120 static int getClipButton(int px, int py);
122 static void toggleLowered(WDock *dock);
124 static void toggleCollapsed(WDock *dock);
126 static void clipIconExpose(WObjDescriptor *desc, XEvent *event);
128 static void clipLeave(WDock *dock);
130 static void handleClipChangeWorkspace(WScreen *scr, XEvent *event);
132 Bool moveIconBetweenDocks(WDock *src, WDock *dest, WAppIcon *icon, int x, int y);
134 static void clipEnterNotify(WObjDescriptor *desc, XEvent *event);
135 static void clipLeaveNotify(WObjDescriptor *desc, XEvent *event);
136 static void clipAutoCollapse(void *cdata);
137 static void clipAutoExpand(void *cdata);
138 static void launchDockedApplication(WAppIcon *btn);
140 static void clipAutoLower(void *cdata);
141 static void clipAutoRaise(void *cdata);
143 static void showClipBalloon(WDock *dock, int workspace);
145 #ifdef OFFIX_DND
147 #define DndNotDnd -1
148 #define DndUnknown 0
149 #define DndRawData 1
150 #define DndFile 2
151 #define DndFiles 3
152 #define DndText 4
153 #define DndDir 5
154 #define DndLink 6
155 #define DndExe 7
157 #define DndEND 8
159 #endif /* OFFIX_DND */
163 static void
164 make_keys()
166 if (dCommand!=NULL)
167 return;
169 dCommand = PLRetain(PLMakeString("Command"));
170 #ifdef OFFIX_DND
171 dDropCommand = PLRetain(PLMakeString("DropCommand"));
172 #endif
173 dAutoLaunch = PLRetain(PLMakeString("AutoLaunch"));
174 dName = PLRetain(PLMakeString("Name"));
175 dForced = PLRetain(PLMakeString("Forced"));
176 dBuggyApplication = PLRetain(PLMakeString("BuggyApplication"));
177 dYes = PLRetain(PLMakeString("Yes"));
178 dNo = PLRetain(PLMakeString("No"));
179 dHost = PLRetain(PLMakeString("Host"));
181 dPosition = PLMakeString("Position");
182 dApplications = PLMakeString("Applications");
183 dLowered = PLMakeString("Lowered");
184 dCollapsed = PLMakeString("Collapsed");
185 dAutoCollapse = PLMakeString("AutoCollapse");
186 dAutoRaiseLower = PLMakeString("AutoRaiseLower");
187 dAutoAttractIcons = PLMakeString("AutoAttractIcons");
188 dKeepAttracted = PLMakeString("KeepAttracted");
190 dDock = PLMakeString("Dock");
191 dClip = PLMakeString("Clip");
196 static void
197 renameCallback(WMenu *menu, WMenuEntry *entry)
199 WDock *dock = entry->clientdata;
200 char buffer[128];
201 int wspace;
202 char *name;
204 assert(entry->clientdata!=NULL);
206 wspace = dock->screen_ptr->current_workspace;
208 name = wstrdup(dock->screen_ptr->workspaces[wspace]->name);
210 sprintf(buffer, _("Type the name for workspace %i:"), wspace+1);
211 if (wInputDialog(dock->screen_ptr, _("Rename Workspace"), buffer,
212 &name)) {
213 wWorkspaceRename(dock->screen_ptr, wspace, name);
215 if (name) {
216 free(name);
221 static void
222 toggleLoweredCallback(WMenu *menu, WMenuEntry *entry)
224 assert(entry->clientdata!=NULL);
226 toggleLowered(entry->clientdata);
228 entry->flags.indicator_on = !((WDock*)entry->clientdata)->lowered;
230 wMenuPaint(menu);
235 static void
236 killCallback(WMenu *menu, WMenuEntry *entry)
238 WAppIcon *icon;
239 #ifdef REDUCE_APPICONS
240 WAppIconAppList *tapplist;
242 extern Atom _XA_WM_DELETE_WINDOW;
243 #else
244 char *buffer;
245 #endif
247 if (!WCHECK_STATE(WSTATE_NORMAL))
248 return;
250 assert(entry->clientdata!=NULL);
252 icon = (WAppIcon*)entry->clientdata;
254 icon->editing = 1;
256 WCHANGE_STATE(WSTATE_MODAL);
258 #ifdef REDUCE_APPICONS
259 /* Send a delete message to the main window of each application
260 * bound to this docked appicon. - cls
262 tapplist = icon->applist;
263 while (tapplist != NULL) {
264 if (tapplist->wapp->main_window_desc != NULL) {
265 if (tapplist->wapp->main_window_desc->protocols.DELETE_WINDOW) {
266 wClientSendProtocol(tapplist->wapp->main_window_desc,
267 _XA_WM_DELETE_WINDOW, CurrentTime);
268 } else {
269 wClientKill(tapplist->wapp->main_window_desc);
272 tapplist = tapplist->next;
274 #else
275 buffer = wstrappend(icon->wm_class,
276 _(" will be forcibly closed.\n"
277 "Any unsaved changes will be lost.\n"
278 "Please confirm."));
280 if (wPreferences.dont_confirm_kill
281 || wMessageDialog(menu->frame->screen_ptr, _("Kill Application"),
282 buffer, _("Yes"), _("No"), NULL)==WAPRDefault) {
283 if (icon->icon && icon->icon->owner) {
284 wClientKill(icon->icon->owner);
287 #endif /* !REDUCE_APPICONS */
289 icon->editing = 0;
291 WCHANGE_STATE(WSTATE_NORMAL);}
294 static LinkedList*
295 getSelected(WDock *dock)
297 LinkedList *ret=NULL;
298 WAppIcon *btn;
299 int i;
301 for (i=1; i<dock->max_icons; i++) {
302 btn = dock->icon_array[i];
303 if (btn && btn->icon->selected) {
304 ret = list_cons(btn, ret);
308 return ret;
312 static void
313 paintClipButtons(WAppIcon *clipIcon, Bool lpushed, Bool rpushed)
315 Window win = clipIcon->icon->core->window;
316 WScreen *scr = clipIcon->icon->core->screen_ptr;
317 XPoint p[4];
318 int pt = CLIP_BUTTON_SIZE*ICON_SIZE/64;
319 int tp = ICON_SIZE - pt;
320 int as = pt - 15; /* 15 = 5+5+5 */
321 GC gc = scr->clip_title_gc;
322 #ifdef GRADIENT_CLIP_ARROW
323 Bool collapsed = clipIcon->dock->collapsed;
324 #endif
326 if (rpushed) {
327 p[0].x = tp+1;
328 p[0].y = 1;
329 p[1].x = ICON_SIZE-2;
330 p[1].y = 1;
331 p[2].x = ICON_SIZE-2;
332 p[2].y = pt-1;
333 } else if (lpushed) {
334 p[0].x = 1;
335 p[0].y = tp;
336 p[1].x = pt;
337 p[1].y = ICON_SIZE-2;
338 p[2].x = 1;
339 p[2].y = ICON_SIZE-2;
341 if (lpushed || rpushed) {
342 XSetForeground(dpy, scr->draw_gc, scr->white_pixel);
343 XFillPolygon(dpy, win, scr->draw_gc, p, 3, Convex, CoordModeOrigin);
344 XSetForeground(dpy, scr->draw_gc, scr->black_pixel);
346 #ifdef GRADIENT_CLIP_ARROW
347 if (!collapsed) {
348 XSetFillStyle(dpy, scr->copy_gc, FillTiled);
349 XSetTile(dpy, scr->copy_gc, scr->clip_arrow_gradient);
350 XSetClipMask(dpy, scr->copy_gc, None);
351 gc = scr->copy_gc;
353 #endif /* GRADIENT_CLIP_ARROW */
355 p[0].x = p[3].x = ICON_SIZE-4-as;
356 p[0].y = p[3].y = 6;
357 p[1].x = ICON_SIZE-7;
358 p[1].y = 6;
359 p[2].x = ICON_SIZE-7;
360 p[2].y = 3+as;
361 if (rpushed) {
362 XFillPolygon(dpy, win, scr->draw_gc, p, 3, Convex, CoordModeOrigin);
363 XDrawLines(dpy, win, scr->draw_gc, p, 4, CoordModeOrigin);
364 } else {
365 #ifdef GRADIENT_CLIP_ARROW
366 if (!collapsed)
367 XSetTSOrigin(dpy, gc, ICON_SIZE-6-as, 5);
368 #endif
369 XFillPolygon(dpy, win, gc, p,3,Convex,CoordModeOrigin);
370 XDrawLines(dpy, win, gc, p,4,CoordModeOrigin);
373 p[0].x = p[3].x = 6;
374 p[0].y = p[3].y = ICON_SIZE-4-as;
375 p[1].x = 6;
376 p[1].y = ICON_SIZE-7;
377 p[2].x = 3+as;
378 p[2].y = ICON_SIZE-7;
379 if (lpushed) {
380 XFillPolygon(dpy, win, scr->draw_gc, p, 3, Convex, CoordModeOrigin);
381 XDrawLines(dpy, win, scr->draw_gc, p, 4, CoordModeOrigin);
382 } else {
383 #ifdef GRADIENT_CLIP_ARROW
384 if (!collapsed)
385 XSetTSOrigin(dpy, gc, 5, ICON_SIZE-6-as);
386 #endif
387 XFillPolygon(dpy, win, gc, p,3,Convex,CoordModeOrigin);
388 XDrawLines(dpy, win, gc, p,4,CoordModeOrigin);
390 #ifdef GRADIENT_CLIP_ARROW
391 if (!collapsed)
392 XSetFillStyle(dpy, scr->copy_gc, FillSolid);
393 #endif
397 RImage*
398 wClipMakeTile(WScreen *scr, RImage *normalTile)
400 RImage *tile = RCloneImage(normalTile);
401 RColor black;
402 RColor dark;
403 RColor light;
404 int pt, tp;
405 int as;
407 pt = CLIP_BUTTON_SIZE*wPreferences.icon_size/64;
408 tp = wPreferences.icon_size-1 - pt;
409 as = pt - 15;
411 black.alpha = 255;
412 black.red = black.green = black.blue = 0;
414 dark.alpha = 0;
415 dark.red = dark.green = dark.blue = 60;
417 light.alpha = 0;
418 light.red = light.green = light.blue = 80;
421 /* top right */
422 ROperateLine(tile, RSubtractOperation, tp, 0, wPreferences.icon_size-2,
423 pt-1, &dark);
424 RDrawLine(tile, tp-1, 0, wPreferences.icon_size-1, pt+1, &black);
425 ROperateLine(tile, RAddOperation, tp, 2, wPreferences.icon_size-3,
426 pt, &light);
428 RDrawLine(tile, ICON_SIZE - 6 - as, 5, ICON_SIZE - 6, 5, &black);
429 ROperateLine(tile, RSubtractOperation, ICON_SIZE - 6 - as, 5,
430 ICON_SIZE - 7, 4 + as, &dark);
431 ROperateLine(tile, RAddOperation, ICON_SIZE - 6, 6, ICON_SIZE - 6, 5 + as,
432 &light);
435 /* bottom left */
436 ROperateLine(tile, RAddOperation, 2, tp+2, pt-2,
437 wPreferences.icon_size-3, &dark);
438 RDrawLine(tile, 0, tp-1, pt+1, wPreferences.icon_size-1, &black);
439 ROperateLine(tile, RSubtractOperation, 0, tp-2, pt+1,
440 wPreferences.icon_size-2, &light);
442 ROperateLine(tile, RSubtractOperation, 5, ICON_SIZE - 6 - as,
443 4 + as, ICON_SIZE - 7, &dark);
444 RDrawLine(tile, 5, ICON_SIZE - 6 - as, 5, ICON_SIZE - 6, &black);
446 ROperateLine(tile, RAddOperation, 6, ICON_SIZE - 6, 5 + as, ICON_SIZE - 6,
447 &light);
450 return tile;
454 static void
455 removeIconsCallback(WMenu *menu, WMenuEntry *entry)
457 WAppIcon *clickedIcon = (WAppIcon*)entry->clientdata;
458 WDock *dock;
459 WAppIcon *aicon;
460 LinkedList *selectedIcons;
461 int keepit;
463 assert(clickedIcon!=NULL);
465 dock = clickedIcon->dock;
467 selectedIcons = getSelected(dock);
469 if (selectedIcons) {
470 if (wMessageDialog(dock->screen_ptr, _("Workspace Clip"),
471 _("All selected icons will be removed!"),
472 _("OK"), _("Cancel"), NULL)!=WAPRDefault) {
473 return;
475 } else {
476 if (clickedIcon->xindex==0 && clickedIcon->yindex==0)
477 return;
478 selectedIcons = list_cons(clickedIcon, NULL);
481 while (selectedIcons) {
482 aicon = selectedIcons->head;
483 keepit = aicon->running && wApplicationOf(aicon->main_window);
484 wDockDetach(dock, aicon);
485 if (keepit) {
486 PlaceIcon(dock->screen_ptr, &aicon->x_pos, &aicon->y_pos);
487 XMoveWindow(dpy, aicon->icon->core->window,
488 aicon->x_pos, aicon->y_pos);
489 if (!dock->mapped || dock->collapsed)
490 XMapWindow(dpy, aicon->icon->core->window);
492 list_remove_head(&selectedIcons);
495 if (wPreferences.auto_arrange_icons)
496 wArrangeIcons(dock->screen_ptr, True);
500 static void
501 keepIconsCallback(WMenu *menu, WMenuEntry *entry)
503 WAppIcon *clickedIcon = (WAppIcon*)entry->clientdata;
504 WDock *dock;
505 WAppIcon *aicon;
506 LinkedList *selectedIcons;
508 assert(clickedIcon!=NULL);
509 dock = clickedIcon->dock;
511 selectedIcons = getSelected(dock);
513 if (!selectedIcons && clickedIcon!=dock->screen_ptr->clip_icon) {
514 char *command = NULL;
516 if (!clickedIcon->command && !clickedIcon->editing) {
517 clickedIcon->editing = 1;
518 if (wInputDialog(dock->screen_ptr, _("Keep Icon"),
519 _("Type the command used to launch the application"),
520 &command)) {
521 if (command && (command[0]==0 ||
522 (command[0]=='-' && command[1]==0))) {
523 free(command);
524 command = NULL;
526 clickedIcon->command = command;
527 clickedIcon->editing = 0;
528 } else {
529 clickedIcon->editing = 0;
530 if (command)
531 free(command);
532 return;
536 selectedIcons = list_cons(clickedIcon, NULL);
539 while (selectedIcons) {
540 aicon = selectedIcons->head;
541 if (aicon->icon->selected)
542 wIconSelect(aicon->icon);
543 if (aicon && aicon->attracted && aicon->command) {
544 aicon->attracted = 0;
545 if (aicon->icon->shadowed) {
546 aicon->icon->shadowed = 0;
547 aicon->icon->force_paint = 1;
548 wAppIconPaint(aicon);
551 list_remove_head(&selectedIcons);
558 static void
559 toggleAutoAttractCallback(WMenu *menu, WMenuEntry *entry)
561 WDock *dock = (WDock*)entry->clientdata;
563 assert(entry->clientdata!=NULL);
565 dock->attract_icons = !dock->attract_icons;
566 /*if (!dock->attract_icons)
567 dock->keep_attracted = 0;*/
569 entry->flags.indicator_on = dock->attract_icons;
571 wMenuPaint(menu);
575 static void
576 toggleKeepCallback(WMenu *menu, WMenuEntry *entry)
578 WDock *dock = (WDock*)entry->clientdata;
579 WAppIcon *btn;
580 int i;
582 assert(entry->clientdata!=NULL);
584 dock->keep_attracted = !dock->keep_attracted;
586 if (dock->keep_attracted) {
587 for (i=0; i< dock->max_icons; i++) {
588 btn = dock->icon_array[i];
589 if (btn && btn->attracted && btn->command) {
590 btn->attracted = 0;
591 if (btn->icon->shadowed) {
592 btn->icon->shadowed = 0;
593 btn->icon->force_paint = 1;
594 wAppIconPaint(btn);
600 entry->flags.indicator_on = dock->keep_attracted;
602 wMenuPaint(menu);
606 static void
607 selectCallback(WMenu *menu, WMenuEntry *entry)
609 WAppIcon *icon = (WAppIcon*)entry->clientdata;
611 assert(icon!=NULL);
613 wIconSelect(icon->icon);
615 wMenuPaint(menu);
619 static void
620 colectIconsCallback(WMenu *menu, WMenuEntry *entry)
622 WAppIcon *clickedIcon = (WAppIcon*)entry->clientdata;
623 WDock *clip;
624 WAppIcon *aicon;
625 int x, y, x_pos, y_pos;
627 assert(entry->clientdata!=NULL);
628 clip = clickedIcon->dock;
630 aicon = clip->screen_ptr->app_icon_list;
632 while (aicon) {
633 if (!aicon->docked && wDockFindFreeSlot(clip, &x, &y)) {
634 x_pos = clip->x_pos + x*ICON_SIZE;
635 y_pos = clip->y_pos + y*ICON_SIZE;
636 if (aicon->x_pos != x_pos || aicon->y_pos != y_pos) {
637 #ifdef ANIMATIONS
638 if (wPreferences.no_animations) {
639 XMoveWindow(dpy, aicon->icon->core->window, x_pos, y_pos);
640 } else {
641 SlideWindow(aicon->icon->core->window,
642 aicon->x_pos, aicon->y_pos, x_pos, y_pos);
644 #else
645 XMoveWindow(dpy, aicon->icon->core->window, x_pos, y_pos);
646 #endif /* ANIMATIONS */
648 aicon->attracted = 1;
649 if (!clip->keep_attracted && !aicon->icon->shadowed) {
650 aicon->icon->shadowed = 1;
651 aicon->icon->force_paint = 1;
652 /* We don't do an wAppIconPaint() here because it's in
653 * wDockAttachIcon(). -Dan
656 wDockAttachIcon(clip, aicon, x, y);
657 if (clip->collapsed || !clip->mapped)
658 XUnmapWindow(dpy, aicon->icon->core->window);
660 aicon = aicon->next;
665 static void
666 selectIconsCallback(WMenu *menu, WMenuEntry *entry)
668 WAppIcon *clickedIcon = (WAppIcon*)entry->clientdata;
669 WDock *dock;
670 LinkedList *selectedIcons;
671 WAppIcon *btn;
672 int i;
674 assert(clickedIcon!=NULL);
675 dock = clickedIcon->dock;
677 selectedIcons = getSelected(dock);
679 if (!selectedIcons) {
680 for (i=1; i<dock->max_icons; i++) {
681 btn = dock->icon_array[i];
682 if (btn && !btn->icon->selected) {
683 wIconSelect(btn->icon);
686 } else {
687 while(selectedIcons) {
688 btn = selectedIcons->head;
689 wIconSelect(btn->icon);
690 list_remove_head(&selectedIcons);
694 wMenuPaint(menu);
698 static void
699 toggleCollapsedCallback(WMenu *menu, WMenuEntry *entry)
701 assert(entry->clientdata!=NULL);
703 toggleCollapsed(entry->clientdata);
705 entry->flags.indicator_on = ((WDock*)entry->clientdata)->collapsed;
707 wMenuPaint(menu);
711 static void
712 toggleAutoCollapseCallback(WMenu *menu, WMenuEntry *entry)
714 WDock *dock;
715 assert(entry->clientdata!=NULL);
717 dock = (WDock*) entry->clientdata;
719 dock->auto_collapse = !dock->auto_collapse;
721 entry->flags.indicator_on = ((WDock*)entry->clientdata)->auto_collapse;
723 wMenuPaint(menu);
727 static void
728 toggleAutoRaiseLowerCallback(WMenu *menu, WMenuEntry *entry)
730 WDock *dock;
731 assert(entry->clientdata!=NULL);
733 dock = (WDock*) entry->clientdata;
735 dock->auto_raise_lower = !dock->auto_raise_lower;
737 entry->flags.indicator_on = ((WDock*)entry->clientdata)->auto_raise_lower;
739 wMenuPaint(menu);
743 static void
744 launchCallback(WMenu *menu, WMenuEntry *entry)
746 WAppIcon *btn = (WAppIcon*)entry->clientdata;
748 launchDockedApplication(btn);
752 static void
753 settingsCallback(WMenu *menu, WMenuEntry *entry)
755 WAppIcon *btn = (WAppIcon*)entry->clientdata;
757 if (btn->editing)
758 return;
759 ShowDockAppSettingsPanel(btn);
763 static void
764 hideCallback(WMenu *menu, WMenuEntry *entry)
766 WApplication *wapp;
767 WAppIcon *btn = (WAppIcon*)entry->clientdata;
769 wapp = wApplicationOf(btn->icon->owner->main_window);
771 if (wapp->flags.hidden) {
772 wWorkspaceChange(btn->icon->core->screen_ptr,wapp->last_workspace);
773 wUnhideApplication(wapp, False, False);
774 } else {
775 wHideApplication(wapp);
780 static void
781 unhideHereCallback(WMenu *menu, WMenuEntry *entry)
783 WApplication *wapp;
784 WAppIcon *btn = (WAppIcon*)entry->clientdata;
786 wapp = wApplicationOf(btn->icon->owner->main_window);
788 wUnhideApplication(wapp, False, True);
792 WAppIcon*
793 mainIconCreate(WScreen *scr, int type)
795 WAppIcon *btn;
796 int x_pos;
798 if (type == WM_CLIP) {
799 if (scr->clip_icon)
800 return scr->clip_icon;
801 btn = wAppIconCreateForDock(scr, NULL, "Logo", "WMClip", TILE_CLIP);
802 btn->icon->core->descriptor.handle_expose = clipIconExpose;
803 btn->icon->core->descriptor.handle_enternotify = clipEnterNotify;
804 btn->icon->core->descriptor.handle_leavenotify = clipLeaveNotify;
805 /*x_pos = scr->scr_width - ICON_SIZE*2 - DOCK_EXTRA_SPACE;*/
806 x_pos = 0;
807 } else {
808 btn = wAppIconCreateForDock(scr, NULL, "Logo", "WMDock", TILE_NORMAL);
809 x_pos = scr->scr_width - ICON_SIZE - DOCK_EXTRA_SPACE;
812 btn->xindex = 0;
813 btn->yindex = 0;
815 btn->icon->core->descriptor.handle_mousedown = iconMouseDown;
816 btn->icon->core->descriptor.parent_type = WCLASS_DOCK_ICON;
817 btn->icon->core->descriptor.parent = btn;
818 /*ChangeStackingLevel(btn->icon->core, WMDockLevel);*/
819 XMapWindow(dpy, btn->icon->core->window);
820 btn->x_pos = x_pos;
821 btn->y_pos = 0;
822 btn->docked = 1;
823 if (type == WM_CLIP)
824 scr->clip_icon = btn;
826 return btn;
830 static void
831 switchWSCommand(WMenu *menu, WMenuEntry *entry)
833 WAppIcon *btn, *icon = (WAppIcon*) entry->clientdata;
834 WScreen *scr = icon->icon->core->screen_ptr;
835 WDock *src, *dest;
836 LinkedList *selectedIcons;
837 int x, y;
839 if (entry->order == scr->current_workspace)
840 return;
841 src = icon->dock;
842 dest = scr->workspaces[entry->order]->clip;
844 selectedIcons = getSelected(src);
846 if (selectedIcons) {
847 while(selectedIcons) {
848 btn = selectedIcons->head;
849 if (wDockFindFreeSlot(dest, &x, &y)) {
850 moveIconBetweenDocks(src, dest, btn, x, y);
851 XUnmapWindow(dpy, btn->icon->core->window);
853 list_remove_head(&selectedIcons);
855 } else if (icon != scr->clip_icon) {
856 if (wDockFindFreeSlot(dest, &x, &y)) {
857 moveIconBetweenDocks(src, dest, icon, x, y);
858 XUnmapWindow(dpy, icon->icon->core->window);
865 static void
866 launchDockedApplication(WAppIcon *btn)
868 WScreen *scr = btn->icon->core->screen_ptr;
870 if (!btn->launching && btn->command!=NULL) {
871 if (!btn->forced_dock) {
872 btn->relaunching = btn->running;
873 btn->running = 1;
875 if (btn->wm_instance || btn->wm_class) {
876 WWindowAttributes attr;
877 memset(&attr, 0, sizeof(WWindowAttributes));
878 wDefaultFillAttributes(scr, btn->wm_instance, btn->wm_class,
879 &attr, NULL, True);
881 if (!attr.no_appicon && !btn->buggy_app)
882 btn->launching = 1;
883 else
884 btn->running = 0;
886 btn->drop_launch = 0;
887 scr->last_dock = btn->dock;
888 btn->pid = execCommand(btn, btn->command, NULL);
889 if (btn->pid>0) {
890 if (btn->buggy_app) {
891 /* give feedback that the app was launched */
892 btn->launching = 1;
893 dockIconPaint(btn);
894 btn->launching = 0;
895 WMAddTimerHandler(200, (WMCallback*)dockIconPaint, btn);
896 } else {
897 dockIconPaint(btn);
899 } else {
900 wwarning(_("could not launch application %s\n"), btn->command);
901 btn->launching = 0;
902 if (!btn->relaunching)
903 btn->running = 0;
910 static void
911 updateWorkspaceMenu(WMenu *menu, WAppIcon *icon)
913 WScreen *scr = menu->frame->screen_ptr;
914 char title[MAX_WORKSPACENAME_WIDTH+1];
915 int i;
917 if (!menu || !icon)
918 return;
920 for (i=0; i<scr->workspace_count; i++) {
921 if (i < menu->entry_no) {
922 if (strcmp(menu->entries[i]->text,scr->workspaces[i]->name)!=0) {
923 free(menu->entries[i]->text);
924 strcpy(title, scr->workspaces[i]->name);
925 menu->entries[i]->text = wstrdup(title);
926 menu->flags.realized = 0;
928 menu->entries[i]->clientdata = (void*)icon;
929 } else {
930 strcpy(title, scr->workspaces[i]->name);
932 wMenuAddCallback(menu, title, switchWSCommand, (void*)icon);
934 menu->flags.realized = 0;
936 if (i == scr->current_workspace) {
937 wMenuSetEnabled(menu, i, False);
938 } else {
939 wMenuSetEnabled(menu, i, True);
943 if (!menu->flags.realized)
944 wMenuRealize(menu);
948 static WMenu*
949 makeWorkspaceMenu(WScreen *scr)
951 WMenu *menu;
953 menu = wMenuCreate(scr, NULL, False);
954 if (!menu)
955 wwarning(_("could not create workspace submenu for Clip menu"));
957 wMenuAddCallback(menu, "", switchWSCommand, (void*)scr->clip_icon);
959 menu->flags.realized = 0;
960 wMenuRealize(menu);
962 return menu;
966 static void
967 updateClipOptionsMenu(WMenu *menu, WDock *dock)
969 WMenuEntry *entry;
970 int index = 0;
972 if (!menu || !dock)
973 return;
975 /* keep on top */
976 entry = menu->entries[index];
977 entry->flags.indicator_on = !dock->lowered;
978 entry->clientdata = dock;
980 /* collapsed */
981 entry = menu->entries[++index];
982 entry->flags.indicator_on = dock->collapsed;
983 entry->clientdata = dock;
985 /* auto-collapse */
986 entry = menu->entries[++index];
987 entry->flags.indicator_on = dock->auto_collapse;
988 entry->clientdata = dock;
990 /* auto-raise/lower */
991 entry = menu->entries[++index];
992 entry->flags.indicator_on = dock->auto_raise_lower;
993 entry->clientdata = dock;
995 /* attract icons */
996 entry = menu->entries[++index];
997 entry->flags.indicator_on = dock->attract_icons;
998 entry->clientdata = dock;
1000 /* keep attracted icons */
1001 entry = menu->entries[++index];
1002 entry->flags.indicator_on = dock->keep_attracted;
1003 entry->clientdata = dock;
1005 menu->flags.realized = 0;
1006 wMenuRealize(menu);
1010 static WMenu*
1011 makeClipOptionsMenu(WScreen *scr)
1013 WMenu *menu;
1014 WMenuEntry *entry;
1016 menu = wMenuCreate(scr, NULL, False);
1017 if (!menu) {
1018 wwarning(_("could not create options submenu for Clip menu"));
1019 return NULL;
1022 entry = wMenuAddCallback(menu, _("Keep on top"),
1023 toggleLoweredCallback, NULL);
1024 entry->flags.indicator = 1;
1025 entry->flags.indicator_on = 1;
1026 entry->flags.indicator_type = MI_CHECK;
1028 entry = wMenuAddCallback(menu, _("Collapsed"),
1029 toggleCollapsedCallback, NULL);
1030 entry->flags.indicator = 1;
1031 entry->flags.indicator_on = 1;
1032 entry->flags.indicator_type = MI_CHECK;
1034 entry = wMenuAddCallback(menu, _("AutoCollapse"),
1035 toggleAutoCollapseCallback, NULL);
1036 entry->flags.indicator = 1;
1037 entry->flags.indicator_on = 1;
1038 entry->flags.indicator_type = MI_CHECK;
1040 entry = wMenuAddCallback(menu, _("AutoRaiseLower"),
1041 toggleAutoRaiseLowerCallback, NULL);
1042 entry->flags.indicator = 1;
1043 entry->flags.indicator_on = 1;
1044 entry->flags.indicator_type = MI_CHECK;
1046 entry = wMenuAddCallback(menu, _("AutoAttract Icons"),
1047 toggleAutoAttractCallback, NULL);
1048 entry->flags.indicator = 1;
1049 entry->flags.indicator_on = 1;
1050 entry->flags.indicator_type = MI_CHECK;
1052 entry = wMenuAddCallback(menu, _("Keep Attracted Icons"),
1053 toggleKeepCallback, NULL);
1054 entry->flags.indicator = 1;
1055 entry->flags.indicator_on = 1;
1056 entry->flags.indicator_type = MI_CHECK;
1058 menu->flags.realized = 0;
1059 wMenuRealize(menu);
1061 return menu;
1065 static WMenu*
1066 dockMenuCreate(WScreen *scr, int type)
1068 WMenu *menu;
1069 WMenuEntry *entry;
1071 if (type == WM_CLIP && scr->clip_menu)
1072 return scr->clip_menu;
1074 menu = wMenuCreate(scr, NULL, False);
1075 if (type != WM_CLIP) {
1076 entry = wMenuAddCallback(menu, _("Keep on top"),
1077 toggleLoweredCallback, NULL);
1078 entry->flags.indicator = 1;
1079 entry->flags.indicator_on = 1;
1080 entry->flags.indicator_type = MI_CHECK;
1081 } else {
1082 entry = wMenuAddCallback(menu, _("Clip Options"), NULL, NULL);
1083 scr->clip_options = makeClipOptionsMenu(scr);
1084 if (scr->clip_options)
1085 wMenuEntrySetCascade(menu, entry, scr->clip_options);
1087 wMenuAddCallback(menu, _("Rename Workspace"), renameCallback, NULL);
1089 wMenuAddCallback(menu, _("(Un)Select Icon"), selectCallback, NULL);
1091 wMenuAddCallback(menu, _("(Un)Select All Icons"), selectIconsCallback,
1092 NULL);
1094 wMenuAddCallback(menu, _("Keep Icon(s)"), keepIconsCallback, NULL);
1096 entry = wMenuAddCallback(menu, _("Move Icon(s) To"), NULL, NULL);
1097 scr->clip_submenu = makeWorkspaceMenu(scr);
1098 if (scr->clip_submenu)
1099 wMenuEntrySetCascade(menu, entry, scr->clip_submenu);
1101 wMenuAddCallback(menu, _("Remove Icon(s)"), removeIconsCallback, NULL);
1103 wMenuAddCallback(menu, _("Attract Icons"), colectIconsCallback, NULL);
1106 wMenuAddCallback(menu, _("Launch"), launchCallback, NULL);
1108 wMenuAddCallback(menu, _("Unhide Here"), unhideHereCallback, NULL);
1110 entry = wMenuAddCallback(menu, _("Hide"), hideCallback, NULL);
1111 free(entry->text);
1112 entry->text = _("Hide");
1114 wMenuAddCallback(menu, _("Settings..."), settingsCallback, NULL);
1116 wMenuAddCallback(menu, _("Kill"), killCallback, NULL);
1118 if (type == WM_CLIP)
1119 scr->clip_menu = menu;
1121 return menu;
1125 WDock*
1126 wDockCreate(WScreen *scr, int type)
1128 WDock *dock;
1129 WAppIcon *btn;
1130 int icon_count;
1132 make_keys();
1134 dock = wmalloc(sizeof(WDock));
1135 memset(dock, 0, sizeof(WDock));
1137 if (type == WM_CLIP)
1138 icon_count = CLIP_MAX_ICONS;
1139 else
1140 icon_count = scr->scr_height/wPreferences.icon_size;
1142 dock->icon_array = wmalloc(sizeof(WAppIcon*)*icon_count);
1143 memset(dock->icon_array, 0, sizeof(WAppIcon*)*icon_count);
1145 dock->max_icons = icon_count;
1147 btn = mainIconCreate(scr, type);
1149 btn->dock = dock;
1151 dock->x_pos = btn->x_pos;
1152 dock->y_pos = btn->y_pos;
1153 dock->screen_ptr = scr;
1154 dock->type = type;
1155 dock->icon_count = 1;
1156 dock->on_right_side = 1;
1157 dock->collapsed = 0;
1158 dock->auto_collapse = 0;
1159 dock->auto_collapse_magic = NULL;
1160 dock->auto_raise_lower = 0;
1161 dock->auto_lower_magic = NULL;
1162 dock->auto_raise_magic = NULL;
1163 dock->attract_icons = 0;
1164 dock->keep_attracted = 0;
1165 dock->lowered = 1;
1166 dock->icon_array[0] = btn;
1167 wRaiseFrame(btn->icon->core);
1168 XMoveWindow(dpy, btn->icon->core->window, btn->x_pos, btn->y_pos);
1170 /* create dock menu */
1171 dock->menu = dockMenuCreate(scr, type);
1173 return dock;
1177 void
1178 wDockDestroy(WDock *dock)
1180 int i;
1181 WAppIcon *aicon;
1183 for (i=(dock->type == WM_CLIP) ? 1 : 0; i<dock->max_icons; i++) {
1184 aicon = dock->icon_array[i];
1185 if (aicon) {
1186 int keepit = aicon->running && wApplicationOf(aicon->main_window);
1187 wDockDetach(dock, aicon);
1188 if (keepit) {
1189 PlaceIcon(dock->screen_ptr, &aicon->x_pos, &aicon->y_pos);
1190 XMoveWindow(dpy, aicon->icon->core->window,
1191 aicon->x_pos, aicon->y_pos);
1192 if (!dock->mapped || dock->collapsed)
1193 XMapWindow(dpy, aicon->icon->core->window);
1197 if (wPreferences.auto_arrange_icons)
1198 wArrangeIcons(dock->screen_ptr, True);
1199 free(dock->icon_array);
1200 if (dock->menu && dock->type!=WM_CLIP)
1201 wMenuDestroy(dock->menu, True);
1202 free(dock);
1206 void
1207 wClipIconPaint(WAppIcon *aicon)
1209 WScreen *scr = aicon->icon->core->screen_ptr;
1210 WWorkspace *workspace = scr->workspaces[scr->current_workspace];
1211 GC gc;
1212 Window win = aicon->icon->core->window;
1213 int length, nlength;
1214 char *ws_name, ws_number[10];
1215 int ty, tx;
1217 wIconPaint(aicon->icon);
1219 length = strlen(workspace->name);
1220 ws_name = malloc(length + 1);
1221 sprintf(ws_name, "%s", workspace->name);
1222 sprintf(ws_number, "%i", scr->current_workspace + 1);
1223 nlength = strlen(ws_number);
1225 gc = scr->clip_title_gc;
1227 if (!workspace->clip->collapsed)
1228 XSetForeground(dpy, gc, scr->clip_title_pixel[CLIP_NORMAL]);
1229 else
1230 XSetForeground(dpy, gc, scr->clip_title_pixel[CLIP_COLLAPSED]);
1232 ty = ICON_SIZE - scr->clip_title_font->height - 3;
1234 tx = CLIP_BUTTON_SIZE*ICON_SIZE/64;
1236 wDrawString(win, scr->clip_title_font, gc, tx,
1237 ty + scr->clip_title_font->y, ws_name, length);
1239 tx = (ICON_SIZE/2 - wTextWidth(scr->clip_title_font->font, ws_number, nlength))/2;
1241 wDrawString(win, scr->clip_title_font, gc, tx,
1242 scr->clip_title_font->y + 2, ws_number, nlength);
1244 free(ws_name);
1246 if (aicon->launching) {
1247 XFillRectangle(dpy, aicon->icon->core->window, scr->stipple_gc,
1248 0, 0, wPreferences.icon_size, wPreferences.icon_size);
1250 paintClipButtons(aicon, aicon->dock->lclip_button_pushed,
1251 aicon->dock->rclip_button_pushed);
1255 static void
1256 clipIconExpose(WObjDescriptor *desc, XEvent *event)
1258 wClipIconPaint(desc->parent);
1262 static void
1263 dockIconPaint(WAppIcon *btn)
1265 if (btn == btn->icon->core->screen_ptr->clip_icon)
1266 wClipIconPaint(btn);
1267 else
1268 wAppIconPaint(btn);
1272 static proplist_t
1273 make_icon_state(WAppIcon *btn)
1275 proplist_t node = NULL;
1276 proplist_t command, autolaunch, name, forced, host, position, buggy;
1277 char *tmp;
1278 char buffer[64];
1280 if (btn) {
1281 if (!btn->command)
1282 command = PLMakeString("-");
1283 else
1284 command = PLMakeString(btn->command);
1286 autolaunch = btn->auto_launch ? dYes : dNo;
1288 tmp = EscapeWM_CLASS(btn->wm_instance, btn->wm_class);
1290 name = PLMakeString(tmp);
1292 free(tmp);
1294 forced = btn->forced_dock ? dYes : dNo;
1296 buggy = btn->buggy_app ? dYes: dNo;
1298 if (btn == btn->icon->core->screen_ptr->clip_icon)
1299 sprintf(buffer, "%i,%i", btn->x_pos, btn->y_pos);
1300 else
1301 sprintf(buffer, "%hi,%hi", btn->xindex, btn->yindex);
1302 position = PLMakeString(buffer);
1304 node = PLMakeDictionaryFromEntries(dCommand, command,
1305 dName, name,
1306 dAutoLaunch, autolaunch,
1307 dForced, forced,
1308 dBuggyApplication, buggy,
1309 dPosition, position,
1310 NULL);
1311 PLRelease(command);
1312 PLRelease(name);
1313 PLRelease(position);
1314 #ifdef OFFIX_DND
1315 if (btn->dnd_command) {
1316 command = PLMakeString(btn->dnd_command);
1317 PLInsertDictionaryEntry(node, dDropCommand, command);
1318 PLRelease(command);
1320 #endif /* OFFIX_DND */
1322 if (btn->client_machine && btn->remote_start) {
1323 host = PLMakeString(btn->client_machine);
1324 PLInsertDictionaryEntry(node, dHost, host);
1325 PLRelease(host);
1329 return node;
1333 static proplist_t
1334 dockSaveState(WDock *dock)
1336 int i;
1337 proplist_t icon_info;
1338 proplist_t list=NULL, dock_state=NULL;
1339 proplist_t value;
1340 char buffer[256];
1342 list = PLMakeArrayFromElements(NULL);
1344 for (i=(dock->type==WM_DOCK ? 0 : 1); i<dock->max_icons; i++) {
1345 WAppIcon *btn = dock->icon_array[i];
1347 if (!btn || (btn->attracted && !dock->keep_attracted))
1348 continue;
1350 if ((icon_info = make_icon_state(dock->icon_array[i]))) {
1351 list = PLAppendArrayElement(list, icon_info);
1352 PLRelease(icon_info);
1356 dock_state = PLMakeDictionaryFromEntries(dApplications, list, NULL);
1358 PLRelease(list);
1360 if (dock->type == WM_DOCK) {
1361 sprintf(buffer, "%i,%i", (dock->on_right_side ? -ICON_SIZE : 0),
1362 dock->y_pos);
1363 value = PLMakeString(buffer);
1364 PLInsertDictionaryEntry(dock_state, dPosition, value);
1365 PLRelease(value);
1368 value = (dock->lowered ? dYes : dNo);
1369 PLInsertDictionaryEntry(dock_state, dLowered, value);
1371 if (dock->type == WM_CLIP) {
1372 value = (dock->collapsed ? dYes : dNo);
1373 PLInsertDictionaryEntry(dock_state, dCollapsed, value);
1375 value = (dock->auto_collapse ? dYes : dNo);
1376 PLInsertDictionaryEntry(dock_state, dAutoCollapse, value);
1378 value = (dock->auto_raise_lower ? dYes : dNo);
1379 PLInsertDictionaryEntry(dock_state, dAutoRaiseLower, value);
1381 value = (dock->attract_icons ? dYes : dNo);
1382 PLInsertDictionaryEntry(dock_state, dAutoAttractIcons, value);
1384 value = (dock->keep_attracted ? dYes : dNo);
1385 PLInsertDictionaryEntry(dock_state, dKeepAttracted, value);
1388 return dock_state;
1392 void
1393 wDockSaveState(WScreen *scr)
1395 proplist_t dock_state;
1397 dock_state = dockSaveState(scr->dock);
1399 PLInsertDictionaryEntry(scr->session_state, dDock, dock_state);
1401 PLRelease(dock_state);
1405 void
1406 wClipSaveState(WScreen *scr)
1408 proplist_t clip_state;
1410 clip_state = make_icon_state(scr->clip_icon);
1412 PLInsertDictionaryEntry(scr->session_state, dClip, clip_state);
1414 PLRelease(clip_state);
1418 proplist_t
1419 wClipSaveWorkspaceState(WScreen *scr, int workspace)
1421 return dockSaveState(scr->workspaces[workspace]->clip);
1425 static WAppIcon*
1426 restore_icon_state(WScreen *scr, proplist_t info, int type, int index)
1428 WAppIcon *aicon;
1429 char *wclass, *winstance;
1430 proplist_t cmd, value;
1431 char *command;
1434 cmd = PLGetDictionaryEntry(info, dCommand);
1435 if (!cmd || !PLIsString(cmd)) {
1436 return NULL;
1439 /* parse window name */
1440 value = PLGetDictionaryEntry(info, dName);
1441 if (!value)
1442 return NULL;
1444 ParseWindowName(value, &winstance, &wclass, "dock");
1446 if (!winstance && !wclass) {
1447 return NULL;
1450 /* get commands */
1452 if (cmd)
1453 command = wstrdup(PLGetString(cmd));
1454 else
1455 command = NULL;
1457 if (!command || strcmp(command, "-")==0) {
1458 if (command)
1459 free(command);
1460 if (wclass)
1461 free(wclass);
1462 if (winstance)
1463 free(winstance);
1465 return NULL;
1468 aicon = wAppIconCreateForDock(scr, command, winstance, wclass,
1469 TILE_NORMAL);
1470 if (wclass)
1471 free(wclass);
1472 if (winstance)
1473 free(winstance);
1475 aicon->icon->core->descriptor.handle_mousedown = iconMouseDown;
1476 if (type == WM_CLIP) {
1477 aicon->icon->core->descriptor.handle_enternotify = clipEnterNotify;
1478 aicon->icon->core->descriptor.handle_leavenotify = clipLeaveNotify;
1480 aicon->icon->core->descriptor.parent_type = WCLASS_DOCK_ICON;
1481 aicon->icon->core->descriptor.parent = aicon;
1484 #ifdef OFFIX_DND
1485 cmd = PLGetDictionaryEntry(info, dDropCommand);
1486 if (cmd)
1487 aicon->dnd_command = wstrdup(PLGetString(cmd));
1488 #endif
1490 /* check auto launch */
1491 value = PLGetDictionaryEntry(info, dAutoLaunch);
1493 aicon->auto_launch = 0;
1494 if (value) {
1495 if (PLIsString(value)) {
1496 if (strcasecmp(PLGetString(value), "YES")==0)
1497 aicon->auto_launch = 1;
1498 } else {
1499 wwarning(_("bad value in docked icon state info %s"),
1500 PLGetString(dAutoLaunch));
1504 /* check if it wasn't normally docked */
1505 value = PLGetDictionaryEntry(info, dForced);
1507 aicon->forced_dock = 0;
1508 if (value) {
1509 if (PLIsString(value)) {
1510 if (strcasecmp(PLGetString(value), "YES")==0)
1511 aicon->forced_dock = 1;
1512 } else {
1513 wwarning(_("bad value in docked icon state info %s"),
1514 PLGetString(dForced));
1518 /* check if we can rely on the stuff in the app */
1519 value = PLGetDictionaryEntry(info, dBuggyApplication);
1521 aicon->buggy_app = 0;
1522 if (value) {
1523 if (PLIsString(value)) {
1524 if (strcasecmp(PLGetString(value), "YES")==0)
1525 aicon->buggy_app = 1;
1526 } else {
1527 wwarning(_("bad value in docked icon state info %s"),
1528 PLGetString(dBuggyApplication));
1532 /* get position in the dock */
1533 value = PLGetDictionaryEntry(info, dPosition);
1534 if (value && PLIsString(value)) {
1535 if (sscanf(PLGetString(value), "%hi,%hi", &aicon->xindex,
1536 &aicon->yindex)!=2)
1537 wwarning(_("bad value in docked icon state info %s"),
1538 PLGetString(dPosition));
1540 /* check position sanity */
1541 /* incomplete section! */
1542 if (type == WM_DOCK) {
1543 aicon->xindex = 0;
1544 if (aicon->yindex < 0)
1545 wwarning(_("bad value in docked icon position %i,%i"),
1546 aicon->xindex, aicon->yindex);
1548 } else {
1549 aicon->yindex = index;
1550 aicon->xindex = 0;
1553 aicon->running = 0;
1554 aicon->docked = 1;
1556 return aicon;
1560 #define COMPLAIN(key) wwarning(_("bad value in dock state info:%s"), key)
1563 WAppIcon*
1564 wClipRestoreState(WScreen *scr, proplist_t clip_state)
1566 WAppIcon *icon;
1567 proplist_t value;
1570 icon = mainIconCreate(scr, WM_CLIP);
1572 if (!clip_state)
1573 return icon;
1574 else
1575 PLRetain(clip_state);
1577 /* restore position */
1579 value = PLGetDictionaryEntry(clip_state, dPosition);
1581 if (value) {
1582 if (!PLIsString(value))
1583 COMPLAIN("Position");
1584 else {
1585 if (sscanf(PLGetString(value), "%i,%i", &icon->x_pos,
1586 &icon->y_pos)!=2)
1587 COMPLAIN("Position");
1589 /* check position sanity */
1590 if (icon->y_pos < 0)
1591 icon->y_pos = 0;
1592 else if (icon->y_pos > scr->scr_height-ICON_SIZE)
1593 icon->y_pos = scr->scr_height-ICON_SIZE;
1595 if (icon->x_pos < 0)
1596 icon->x_pos = 0;
1597 else if (icon->x_pos > scr->scr_width-ICON_SIZE)
1598 icon->x_pos = scr->scr_width-ICON_SIZE;
1602 #ifdef OFFIX_DND
1603 value = PLGetDictionaryEntry(clip_state, dDropCommand);
1604 if (value && PLIsString(value))
1605 icon->dnd_command = wstrdup(PLGetString(value));
1606 #endif
1608 PLRelease(clip_state);
1610 return icon;
1614 WDock*
1615 wDockRestoreState(WScreen *scr, proplist_t dock_state, int type)
1617 WDock *dock;
1618 proplist_t apps;
1619 proplist_t value;
1620 WAppIcon *aicon, *old_top;
1621 int count, i;
1624 dock = wDockCreate(scr, type);
1626 if (!dock_state)
1627 return dock;
1629 if (dock_state)
1630 PLRetain(dock_state);
1633 /* restore position */
1635 value = PLGetDictionaryEntry(dock_state, dPosition);
1637 if (value) {
1638 if (!PLIsString(value))
1639 COMPLAIN("Position");
1640 else {
1641 if (sscanf(PLGetString(value), "%i,%i", &dock->x_pos,
1642 &dock->y_pos)!=2)
1643 COMPLAIN("Position");
1645 /* check position sanity */
1646 if (dock->y_pos < 0)
1647 dock->y_pos = 0;
1648 else if (dock->y_pos > scr->scr_height-ICON_SIZE)
1649 dock->y_pos = scr->scr_height - ICON_SIZE;
1651 /* This is no more needed. ??? */
1652 if (type == WM_CLIP) {
1653 if (dock->x_pos < 0)
1654 dock->x_pos = 0;
1655 else if (dock->x_pos > scr->scr_width-ICON_SIZE)
1656 dock->x_pos = scr->scr_width-ICON_SIZE;
1657 } else {
1658 if (dock->x_pos >= 0) {
1659 dock->x_pos = DOCK_EXTRA_SPACE;
1660 dock->on_right_side = 0;
1661 } else {
1662 dock->x_pos = scr->scr_width - DOCK_EXTRA_SPACE - ICON_SIZE;
1663 dock->on_right_side = 1;
1669 /* restore lowered/raised state */
1671 dock->lowered = 0;
1673 value = PLGetDictionaryEntry(dock_state, dLowered);
1675 if (value) {
1676 if (!PLIsString(value))
1677 COMPLAIN("Lowered");
1678 else {
1679 if (strcasecmp(PLGetString(value), "YES")==0)
1680 dock->lowered = 1;
1685 /* restore collapsed state */
1687 dock->collapsed = 0;
1689 value = PLGetDictionaryEntry(dock_state, dCollapsed);
1691 if (value) {
1692 if (!PLIsString(value))
1693 COMPLAIN("Collapsed");
1694 else {
1695 if (strcasecmp(PLGetString(value), "YES")==0)
1696 dock->collapsed = 1;
1701 /* restore auto-collapsed state */
1703 value = PLGetDictionaryEntry(dock_state, dAutoCollapse);
1705 if (value) {
1706 if (!PLIsString(value))
1707 COMPLAIN("AutoCollapse");
1708 else {
1709 if (strcasecmp(PLGetString(value), "YES")==0) {
1710 dock->auto_collapse = 1;
1711 dock->collapsed = 1;
1717 /* restore auto-raise/lower state */
1719 value = PLGetDictionaryEntry(dock_state, dAutoRaiseLower);
1721 if (value) {
1722 if (!PLIsString(value))
1723 COMPLAIN("AutoRaiseLower");
1724 else {
1725 if (strcasecmp(PLGetString(value), "YES")==0) {
1726 dock->auto_raise_lower = 1;
1732 /* restore attract icons state */
1734 dock->attract_icons = 0;
1736 value = PLGetDictionaryEntry(dock_state, dAutoAttractIcons);
1738 if (value) {
1739 if (!PLIsString(value))
1740 COMPLAIN("AutoAttractIcons");
1741 else {
1742 if (strcasecmp(PLGetString(value), "YES")==0)
1743 dock->attract_icons = 1;
1748 /* restore keep attracted icons state */
1750 dock->keep_attracted = 0;
1752 value = PLGetDictionaryEntry(dock_state, dKeepAttracted);
1754 if (value) {
1755 if (!PLIsString(value))
1756 COMPLAIN("KeepAttracted");
1757 else {
1758 if (strcasecmp(PLGetString(value), "YES")==0)
1759 dock->keep_attracted = 1;
1764 /* application list */
1766 apps = PLGetDictionaryEntry(dock_state, dApplications);
1768 if (!apps) {
1769 goto finish;
1772 count = PLGetNumberOfElements(apps);
1774 if (count==0)
1775 goto finish;
1777 old_top = dock->icon_array[0];
1779 /* dock->icon_count is set to 1 when dock is created.
1780 * Since Clip is already restored, we want to keep it so for clip,
1781 * but for dock we may change the default top tile, so we set it to 0.
1783 if (type == WM_DOCK)
1784 dock->icon_count = 0;
1786 for (i=0; i<count; i++) {
1787 if (dock->icon_count >= dock->max_icons) {
1788 wwarning(_("there are too many icons stored in dock. Ignoring what doesn't fit"));
1789 break;
1792 value = PLGetArrayElement(apps, i);
1793 aicon = restore_icon_state(scr, value, type, dock->icon_count);
1795 dock->icon_array[dock->icon_count] = aicon;
1797 if (aicon) {
1798 aicon->dock = dock;
1799 aicon->x_pos = dock->x_pos + (aicon->xindex*ICON_SIZE);
1800 aicon->y_pos = dock->y_pos + (aicon->yindex*ICON_SIZE);
1802 if (dock->lowered)
1803 ChangeStackingLevel(aicon->icon->core, WMNormalLevel);
1804 else
1805 ChangeStackingLevel(aicon->icon->core, WMDockLevel);
1807 wCoreConfigure(aicon->icon->core, aicon->x_pos, aicon->y_pos,
1808 0, 0);
1810 if (!dock->collapsed)
1811 XMapWindow(dpy, aicon->icon->core->window);
1812 wRaiseFrame(aicon->icon->core);
1814 dock->icon_count++;
1815 } else if (dock->icon_count==0 && type==WM_DOCK)
1816 dock->icon_count++;
1819 /* if the first icon is not defined, use the default */
1820 if (dock->icon_array[0]==NULL) {
1821 /* update default icon */
1822 old_top->x_pos = dock->x_pos;
1823 old_top->y_pos = dock->y_pos;
1824 if (dock->lowered)
1825 ChangeStackingLevel(old_top->icon->core, WMNormalLevel);
1826 else
1827 ChangeStackingLevel(old_top->icon->core, WMDockLevel);
1828 dock->icon_array[0] = old_top;
1829 XMoveWindow(dpy, old_top->icon->core->window, dock->x_pos, dock->y_pos);
1830 /* we don't need to increment dock->icon_count here because it was
1831 * incremented in the loop above.
1833 } else if (old_top!=dock->icon_array[0]) {
1834 if (old_top == scr->clip_icon)
1835 scr->clip_icon = dock->icon_array[0];
1836 wAppIconDestroy(old_top);
1839 finish:
1840 if (dock_state)
1841 PLRelease(dock_state);
1843 return dock;
1848 void
1849 wDockLaunchWithState(WDock *dock, WAppIcon *btn, WSavedState *state)
1851 if (btn && btn->command && !btn->running && !btn->launching) {
1853 btn->drop_launch = 0;
1855 btn->pid = execCommand(btn, btn->command, state);
1857 if (btn->pid>0) {
1858 if (!btn->forced_dock && !btn->buggy_app) {
1859 btn->launching = 1;
1860 dockIconPaint(btn);
1863 } else {
1864 free(state);
1869 void
1870 wDockDoAutoLaunch(WDock *dock, int workspace)
1872 WAppIcon *btn;
1873 WSavedState *state;
1874 int i;
1876 for (i = 0; i < dock->max_icons; i++) {
1877 btn = dock->icon_array[i];
1878 if (!btn || !btn->auto_launch)
1879 continue;
1881 state = wmalloc(sizeof(WSavedState));
1882 memset(state, 0, sizeof(WSavedState));
1883 state->workspace = workspace;
1884 /* TODO: this is klugy and is very difficult to understand
1885 * what's going on. Try to clean up */
1886 wDockLaunchWithState(dock, btn, state);
1890 #ifdef REDUCE_APPICONS
1891 void
1892 wDockSimulateLaunch(WDock *dock, WAppIcon *btn)
1894 if ((btn == NULL) || (dock == NULL))
1895 return;
1897 if (!btn->running) {
1898 if ((btn->icon->owner == NULL) && (btn->applist))
1899 btn->icon->owner = btn->applist->wapp->main_window_desc;
1900 if (!btn->forced_dock)
1901 btn->launching = 1;
1902 dockIconPaint(btn);
1903 wusleep(5000);
1906 #endif
1908 #ifdef OFFIX_DND
1909 static WDock*
1910 findDock(WScreen *scr, XEvent *event, int *icon_pos)
1912 WDock *dock;
1913 int i;
1915 *icon_pos = -1;
1916 if ((dock = scr->dock)!=NULL) {
1917 for (i=0; i<dock->max_icons; i++) {
1918 if (dock->icon_array[i]
1919 && dock->icon_array[i]->icon->core->window==event->xclient.window) {
1920 *icon_pos = i;
1921 break;
1925 if (*icon_pos<0 && (dock = scr->workspaces[scr->current_workspace]->clip)!=NULL) {
1926 for (i=0; i<dock->max_icons; i++) {
1927 if (dock->icon_array[i]
1928 && dock->icon_array[i]->icon->core->window==event->xclient.window) {
1929 *icon_pos = i;
1930 break;
1934 if(*icon_pos>=0)
1935 return dock;
1936 return NULL;
1941 wDockReceiveDNDDrop(WScreen *scr, XEvent *event)
1943 WDock *dock;
1944 WAppIcon *btn;
1945 int icon_pos;
1947 dock = findDock(scr, event, &icon_pos);
1948 if (!dock)
1949 return False;
1952 * Return True if the drop was on an application icon window.
1953 * In this case, let the ClientMessage handler redirect the
1954 * message to the app.
1956 if (dock->icon_array[icon_pos]->icon->icon_win!=None)
1957 return True;
1959 if (dock->icon_array[icon_pos]->dnd_command!=NULL) {
1960 scr->flags.dnd_data_convertion_status = 0;
1962 btn = dock->icon_array[icon_pos];
1964 if (!btn->forced_dock) {
1965 btn->relaunching = btn->running;
1966 btn->running = 1;
1968 if (btn->wm_instance || btn->wm_class) {
1969 WWindowAttributes attr;
1970 memset(&attr, 0, sizeof(WWindowAttributes));
1971 wDefaultFillAttributes(btn->icon->core->screen_ptr,
1972 btn->wm_instance,
1973 btn->wm_class, &attr, NULL, True);
1975 if (!attr.no_appicon)
1976 btn->launching = 1;
1977 else
1978 btn->running = 0;
1981 btn->drop_launch = 1;
1982 scr->last_dock = dock;
1983 btn->pid = execCommand(btn, btn->dnd_command, NULL);
1984 if (btn->pid>0) {
1985 dockIconPaint(btn);
1986 } else {
1987 btn->launching = 0;
1988 if (!btn->relaunching) {
1989 btn->running = 0;
1993 return False;
1995 #endif /* OFFIX_DND */
1999 Bool
2000 wDockAttachIcon(WDock *dock, WAppIcon *icon, int x, int y)
2002 WWindow *wwin;
2003 char **argv;
2004 int argc;
2005 int index;
2007 wwin = icon->icon->owner;
2008 if (icon->command==NULL) {
2009 icon->editing = 0;
2010 if (XGetCommand(dpy, wwin->client_win, &argv, &argc) && argc>0) {
2012 icon->command = FlattenStringList(argv, argc);
2013 XFreeStringList(argv);
2014 } else {
2015 char *command=NULL;
2017 /* icon->forced_dock = 1;*/
2018 if (!icon->attracted || dock->type!=WM_CLIP || dock->keep_attracted) {
2019 icon->editing = 1;
2020 if (wInputDialog(dock->screen_ptr, _("Dock Icon"),
2021 _("Type the command used to launch the application"),
2022 &command)) {
2023 if (command && (command[0]==0 ||
2024 (command[0]=='-' && command[1]==0))) {
2025 free(command);
2026 command = NULL;
2028 icon->command = command;
2029 icon->editing = 0;
2030 } else {
2031 icon->editing = 0;
2032 if (command)
2033 free(command);
2034 /* If the target is the dock, reject the icon. If
2035 * the target is the clip, make it an attracted icon
2037 if (dock->type==WM_CLIP) {
2038 icon->attracted = 1;
2039 if (!icon->icon->shadowed) {
2040 icon->icon->shadowed = 1;
2041 icon->icon->force_paint = 1;
2043 } else {
2044 return False;
2049 } else {
2050 icon->editing = 0;
2053 for (index=1; index<dock->max_icons; index++)
2054 if (dock->icon_array[index] == NULL)
2055 break;
2056 /* if (index == dock->max_icons)
2057 return; */
2059 assert(index < dock->max_icons);
2061 dock->icon_array[index] = icon;
2062 icon->yindex = y;
2063 icon->xindex = x;
2065 icon->x_pos = dock->x_pos + x*ICON_SIZE;
2066 icon->y_pos = dock->y_pos + y*ICON_SIZE;
2068 dock->icon_count++;
2070 icon->running = 1;
2071 icon->launching = 0;
2072 icon->docked = 1;
2073 icon->dock = dock;
2074 icon->icon->core->descriptor.handle_mousedown = iconMouseDown;
2075 if (dock->type == WM_CLIP) {
2076 icon->icon->core->descriptor.handle_enternotify = clipEnterNotify;
2077 icon->icon->core->descriptor.handle_leavenotify = clipLeaveNotify;
2079 icon->icon->core->descriptor.parent_type = WCLASS_DOCK_ICON;
2080 icon->icon->core->descriptor.parent = icon;
2082 MoveInStackListUnder(dock->icon_array[index-1]->icon->core,
2083 icon->icon->core);
2084 wAppIconMove(icon, icon->x_pos, icon->y_pos);
2085 wAppIconPaint(icon);
2087 if (wPreferences.auto_arrange_icons)
2088 wArrangeIcons(dock->screen_ptr, True);
2090 #ifdef OFFIX_DND
2091 if (icon->command && !icon->dnd_command) {
2092 icon->dnd_command = wmalloc(strlen(icon->command)+8);
2093 sprintf(icon->dnd_command, "%s %%d", icon->command);
2095 #endif
2097 return True;
2101 void
2102 reattachIcon(WDock *dock, WAppIcon *icon, int x, int y)
2104 int index;
2106 for(index=1; index<dock->max_icons; index++) {
2107 if(dock->icon_array[index] == icon)
2108 break;
2110 assert(index < dock->max_icons);
2112 icon->yindex = y;
2113 icon->xindex = x;
2115 icon->x_pos = dock->x_pos + x*ICON_SIZE;
2116 icon->y_pos = dock->y_pos + y*ICON_SIZE;
2120 Bool
2121 moveIconBetweenDocks(WDock *src, WDock *dest, WAppIcon *icon, int x, int y)
2123 WWindow *wwin;
2124 char **argv;
2125 int argc;
2126 int index;
2128 if (dest == NULL)
2129 return False;
2131 wwin = icon->icon->owner;
2134 * For the moment we can't do this if we move icons in Clip from one
2135 * workspace to other, because if we move two or more icons without
2136 * command, the dialog box will not be able to tell us to which of the
2137 * moved icons it applies. -Dan
2139 if ((dest->type==WM_DOCK /*|| dest->keep_attracted*/) && icon->command==NULL) {
2140 if (XGetCommand(dpy, wwin->client_win, &argv, &argc) && argc>0) {
2142 icon->command = FlattenStringList(argv, argc);
2143 XFreeStringList(argv);
2144 } else {
2145 char *command=NULL;
2147 icon->editing = 1;
2148 /* icon->forced_dock = 1;*/
2149 if (wInputDialog(src->screen_ptr, _("Dock Icon"),
2150 _("Type the command used to launch the application"),
2151 &command)) {
2152 if (command && (command[0]==0 ||
2153 (command[0]=='-' && command[1]==0))) {
2154 free(command);
2155 command = NULL;
2157 icon->command = command;
2158 } else {
2159 icon->editing = 0;
2160 if (command)
2161 free(command);
2162 return False;
2164 icon->editing = 0;
2168 for(index=1; index<src->max_icons; index++) {
2169 if(src->icon_array[index] == icon)
2170 break;
2172 assert(index < src->max_icons);
2174 src->icon_array[index] = NULL;
2175 src->icon_count--;
2177 for(index=1; index<dest->max_icons; index++) {
2178 if(dest->icon_array[index] == NULL)
2179 break;
2181 /* if (index == dest->max_icons)
2182 return; */
2184 assert(index < dest->max_icons);
2186 dest->icon_array[index] = icon;
2187 icon->dock = dest;
2189 /* deselect the icon */
2190 if (icon->icon->selected)
2191 wIconSelect(icon->icon);
2193 if (dest->type == WM_DOCK) {
2194 icon->icon->core->descriptor.handle_enternotify = NULL;
2195 icon->icon->core->descriptor.handle_leavenotify = NULL;
2196 } else {
2197 icon->icon->core->descriptor.handle_enternotify = clipEnterNotify;
2198 icon->icon->core->descriptor.handle_leavenotify = clipLeaveNotify;
2201 /* set it to be kept when moving to dock, or to a clip that keep the
2202 * attracted icons.
2203 * Unless the icon does not have a command set
2205 if (icon->command && (dest->type==WM_DOCK || dest->keep_attracted)) {
2206 icon->attracted = 0;
2207 if (icon->icon->shadowed) {
2208 icon->icon->shadowed = 0;
2209 icon->icon->force_paint = 1;
2213 if (src->auto_collapse || src->auto_raise_lower)
2214 clipLeave(src);
2216 icon->yindex = y;
2217 icon->xindex = x;
2219 icon->x_pos = dest->x_pos + x*ICON_SIZE;
2220 icon->y_pos = dest->y_pos + y*ICON_SIZE;
2222 dest->icon_count++;
2224 MoveInStackListUnder(dest->icon_array[index-1]->icon->core,
2225 icon->icon->core);
2226 wAppIconPaint(icon);
2228 return True;
2232 void
2233 wDockDetach(WDock *dock, WAppIcon *icon)
2235 int index;
2237 /* make the settings panel be closed */
2238 if (icon->panel) {
2239 DestroyDockAppSettingsPanel(icon->panel);
2242 icon->docked = 0;
2243 icon->dock = NULL;
2244 icon->attracted = 0;
2245 if (icon->icon->shadowed) {
2246 icon->icon->shadowed = 0;
2247 icon->icon->force_paint = 1;
2250 /* deselect the icon */
2251 if (icon->icon->selected)
2252 wIconSelect(icon->icon);
2254 if (icon->command) {
2255 free(icon->command);
2256 icon->command = NULL;
2258 #ifdef OFFIX_DND
2259 if (icon->dnd_command) {
2260 free(icon->dnd_command);
2261 icon->dnd_command = NULL;
2263 #endif
2265 for (index=1; index<dock->max_icons; index++)
2266 if (dock->icon_array[index] == icon)
2267 break;
2268 assert(index < dock->max_icons);
2269 dock->icon_array[index] = NULL;
2270 icon->yindex = -1;
2271 icon->xindex = -1;
2272 dock->icon_count--;
2274 /* if the dock is not attached to an application or
2275 * the the application did not set the approriate hints yet,
2276 * destroy the icon */
2277 #ifdef REDUCE_APPICONS
2278 if ((icon->num_apps == 0) && (!icon->running || !wApplicationOf(icon->main_window)) )
2279 #else
2280 if (!icon->running || !wApplicationOf(icon->main_window))
2281 #endif
2282 wAppIconDestroy(icon);
2283 else {
2284 icon->icon->core->descriptor.handle_mousedown = appIconMouseDown;
2285 icon->icon->core->descriptor.handle_enternotify = NULL;
2286 icon->icon->core->descriptor.handle_leavenotify = NULL;
2287 icon->icon->core->descriptor.parent_type = WCLASS_APPICON;
2288 icon->icon->core->descriptor.parent = icon;
2290 ChangeStackingLevel(icon->icon->core, NORMAL_ICON_LEVEL);
2292 wAppIconPaint(icon);
2293 if (wPreferences.auto_arrange_icons) {
2294 wArrangeIcons(dock->screen_ptr, True);
2297 if (dock->auto_collapse || dock->auto_raise_lower)
2298 clipLeave(dock);
2303 * returns the closest Dock slot index for the passed
2304 * coordinates.
2306 * Returns False if icon can't be docked.
2308 Bool
2309 wDockSnapIcon(WDock *dock, WAppIcon *icon, int req_x, int req_y,
2310 int *ret_x, int *ret_y, int redocking)
2312 WScreen *scr = dock->screen_ptr;
2313 int dx, dy;
2314 int ex_x, ex_y;
2315 int i, offset = ICON_SIZE/2;
2316 WAppIcon *aicon = NULL;
2317 WAppIcon *nicon = NULL;
2318 int max_y_icons, max_x_icons;
2320 max_x_icons = scr->scr_width/ICON_SIZE;
2321 max_y_icons = scr->scr_height/ICON_SIZE-1;
2323 if (wPreferences.flags.noupdates)
2324 return False;
2326 dx = dock->x_pos;
2327 dy = dock->y_pos;
2329 /* if the dock is full */
2330 if (!redocking &&
2331 (dock->icon_count >= dock->max_icons)) {
2332 return False;
2335 /* exact position */
2336 if (req_y < dy)
2337 ex_y = (req_y - offset - dy)/ICON_SIZE;
2338 else
2339 ex_y = (req_y + offset - dy)/ICON_SIZE;
2341 if (req_x < dx)
2342 ex_x = (req_x - offset - dx)/ICON_SIZE;
2343 else
2344 ex_x = (req_x + offset - dx)/ICON_SIZE;
2346 /* check if the icon is outside the screen boundaries */
2347 if (dx + ex_x*ICON_SIZE < -ICON_SIZE+2 ||
2348 dx + ex_x*ICON_SIZE >= scr->scr_width-1 ||
2349 dy + ex_y*ICON_SIZE < -ICON_SIZE+2 ||
2350 dy + ex_y*ICON_SIZE >= scr->scr_height-1)
2351 return False;
2353 if (dock->type == WM_DOCK) {
2354 if (icon->dock != dock && ex_x != 0)
2355 return False;
2357 for (i=0; i<dock->max_icons; i++) {
2358 nicon = dock->icon_array[i];
2359 if (nicon && nicon->yindex == ex_y) {
2360 aicon = nicon;
2361 break;
2365 *ret_x = 0;
2367 if (redocking) {
2368 int sig, done, closest;
2370 /* Possible cases when redocking:
2372 * icon dragged out of range of any slot -> false
2373 * icon dragged to range of free slot
2374 * icon dragged to range of same slot
2375 * icon dragged to range of different icon
2377 if (abs(ex_x) > DOCK_DETTACH_THRESHOLD)
2378 return False;
2380 if (ex_y >= 0 && ex_y <= max_y_icons && (aicon == icon || !aicon)) {
2382 *ret_y = ex_y;
2385 return True;
2388 /* start looking at the upper slot or lower? */
2389 if (ex_y*ICON_SIZE < (req_y + offset - dy))
2390 sig = 1;
2391 else
2392 sig = -1;
2394 closest = -1;
2395 done = 0;
2396 /* look for closest free slot */
2397 for (i=0; i<(DOCK_DETTACH_THRESHOLD+1)*2 && !done; i++) {
2398 int j;
2400 done = 1;
2401 closest = sig*(i/2) + ex_y;
2402 /* check if this slot is used */
2403 if (closest >= 0) {
2404 for (j = 0; j<dock->max_icons; j++) {
2405 if (dock->icon_array[j]
2406 && dock->icon_array[j]->yindex==closest) {
2407 /* slot is used by someone else */
2408 if (dock->icon_array[j]!=icon)
2409 done = 0;
2410 break;
2414 sig = -sig;
2416 if (done && closest >= 0 && closest <= max_y_icons &&
2417 ((ex_y >= closest && ex_y - closest < DOCK_DETTACH_THRESHOLD+1)
2419 (ex_y < closest && closest - ex_y <= DOCK_DETTACH_THRESHOLD+1))) {
2421 *ret_y = closest;
2423 return True;
2425 } else { /* !redocking */
2427 /* if slot is free and the icon is close enough, return it */
2428 if (!aicon && ex_x == 0 && ex_y >= 0 && ex_y <= max_y_icons) {
2429 *ret_y = ex_y;
2430 return True;
2433 } else { /* CLIP */
2434 int neighbours = 0;
2436 for (i=0; i<dock->max_icons; i++) {
2437 nicon = dock->icon_array[i];
2438 if (nicon && nicon->xindex == ex_x && nicon->yindex == ex_y) {
2439 aicon = nicon;
2440 break;
2444 for (i=0; i<dock->max_icons; i++) {
2445 nicon = dock->icon_array[i];
2446 if (nicon && nicon != icon && /* Icon can't be it's own neighbour */
2447 (abs(nicon->xindex - ex_x) <= CLIP_ATTACH_VICINITY &&
2448 abs(nicon->yindex - ex_y) <= CLIP_ATTACH_VICINITY)) {
2449 neighbours = 1;
2450 break;
2454 if (neighbours && (aicon==NULL || (redocking && aicon == icon))) {
2455 *ret_x = ex_x;
2456 *ret_y = ex_y;
2457 return True;
2460 return False;
2463 #define MIN(x, y) ((x) > (y) ? (y) : (x))
2464 #define MAX(x, y) ((x) < (y) ? (y) : (x))
2466 #define ON_SCREEN(x, y, sx, ex, sy, ey) \
2467 ((((x)+ICON_SIZE/2) >= (sx)) && (((y)+ICON_SIZE/2) >= (sy)) && \
2468 (((x) + (ICON_SIZE/2)) <= (ex)) && (((y) + (ICON_SIZE/2)) <= ey))
2472 * returns true if it can find a free slot in the dock,
2473 * in which case it changes x_pos and y_pos accordingly.
2474 * Else returns false.
2476 Bool
2477 wDockFindFreeSlot(WDock *dock, int *x_pos, int *y_pos)
2479 WScreen *scr = dock->screen_ptr;
2480 WAppIcon *btn;
2481 unsigned char *slot_map;
2482 int mwidth;
2483 int r;
2484 int x, y;
2485 int i, done = False;
2486 int corner;
2487 int sx=0, sy=0, ex=scr->scr_width, ey=scr->scr_height;
2490 /* if the dock is full */
2491 if (dock->icon_count >= dock->max_icons) {
2492 return False;
2495 if (!wPreferences.flags.nodock && scr->dock) {
2496 if (scr->dock->on_right_side)
2497 ex -= ICON_SIZE + DOCK_EXTRA_SPACE;
2498 else
2499 sx += ICON_SIZE + DOCK_EXTRA_SPACE;
2502 if (ex < dock->x_pos)
2503 ex = dock->x_pos;
2504 if (sx > dock->x_pos+ICON_SIZE)
2505 sx = dock->x_pos+ICON_SIZE;
2506 #define C_NONE 0
2507 #define C_NW 1
2508 #define C_NE 2
2509 #define C_SW 3
2510 #define C_SE 4
2512 /* check if clip is in a corner */
2513 if (dock->type==WM_CLIP) {
2514 if (dock->x_pos < 1 && dock->y_pos < 1)
2515 corner = C_NE;
2516 else if (dock->x_pos < 1 && dock->y_pos >= (ey-ICON_SIZE))
2517 corner = C_SE;
2518 else if (dock->x_pos >= (ex-ICON_SIZE)&& dock->y_pos >= (ey-ICON_SIZE))
2519 corner = C_SW;
2520 else if (dock->x_pos >= (ex-ICON_SIZE) && dock->y_pos < 1)
2521 corner = C_NW;
2522 else
2523 corner = C_NONE;
2524 } else
2525 corner = C_NONE;
2527 /* If the clip is in the corner, use only slots that are in the border
2528 * of the screen */
2529 if (corner!=C_NONE) {
2530 char *hmap, *vmap;
2531 int hcount, vcount;
2533 hcount = MIN(dock->max_icons, scr->scr_width/ICON_SIZE);
2534 vcount = MIN(dock->max_icons, scr->scr_height/ICON_SIZE);
2535 hmap = wmalloc(hcount+1);
2536 memset(hmap, 0, hcount+1);
2537 vmap = wmalloc(vcount+1);
2538 memset(vmap, 0, vcount+1);
2540 /* mark used positions */
2541 switch (corner) {
2542 case C_NE:
2543 for (i=0; i<dock->max_icons; i++) {
2544 btn = dock->icon_array[i];
2545 if (!btn)
2546 continue;
2548 if (btn->xindex==0 && btn->yindex > 0 && btn->yindex < vcount)
2549 vmap[btn->yindex] = 1;
2550 else if (btn->yindex==0 && btn->xindex>0 && btn->xindex<hcount)
2551 hmap[btn->xindex] = 1;
2553 case C_NW:
2554 for (i=0; i<dock->max_icons; i++) {
2555 btn = dock->icon_array[i];
2556 if (!btn)
2557 continue;
2559 if (btn->xindex==0 && btn->yindex > 0 && btn->yindex < vcount)
2560 vmap[btn->yindex] = 1;
2561 else if (btn->yindex==0 && btn->xindex<0 &&btn->xindex>-hcount)
2562 hmap[-btn->xindex] = 1;
2564 case C_SE:
2565 for (i=0; i<dock->max_icons; i++) {
2566 btn = dock->icon_array[i];
2567 if (!btn)
2568 continue;
2570 if (btn->xindex==0 && btn->yindex < 0 && btn->yindex > -vcount)
2571 vmap[-btn->yindex] = 1;
2572 else if (btn->yindex==0 && btn->xindex>0 && btn->xindex<hcount)
2573 hmap[btn->xindex] = 1;
2575 case C_SW:
2576 default:
2577 for (i=0; i<dock->max_icons; i++) {
2578 btn = dock->icon_array[i];
2579 if (!btn)
2580 continue;
2582 if (btn->xindex==0 && btn->yindex < 0 && btn->yindex > -vcount)
2583 vmap[-btn->yindex] = 1;
2584 else if (btn->yindex==0 && btn->xindex<0 &&btn->xindex>-hcount)
2585 hmap[-btn->xindex] = 1;
2588 x=0; y=0;
2589 done = 0;
2590 /* search a vacant slot */
2591 for (i=1; i<MAX(vcount, hcount); i++) {
2592 if (i < vcount && vmap[i]==0) {
2593 /* found a slot */
2594 x = 0;
2595 y = i;
2596 done = 1;
2597 break;
2598 } else if (i < hcount && hmap[i]==0) {
2599 /* found a slot */
2600 x = i;
2601 y = 0;
2602 done = 1;
2603 break;
2606 free(vmap);
2607 free(hmap);
2608 /* If found a slot, translate and return */
2609 if (done) {
2610 if (corner==C_NW || corner==C_NE) {
2611 *y_pos = y;
2612 } else {
2613 *y_pos = -y;
2615 if (corner==C_NE || corner==C_SE) {
2616 *x_pos = x;
2617 } else {
2618 *x_pos = -x;
2620 return True;
2622 /* else, try to find a slot somewhere else */
2625 /* a map of mwidth x mwidth would be enough if we allowed icons to be
2626 * placed outside of screen */
2627 mwidth = (int)ceil(sqrt(dock->max_icons));
2629 /* In the worst case (the clip is in the corner of the screen),
2630 * the amount of icons that fit in the clip is smaller.
2631 * Double the map to get a safe value.
2633 mwidth += mwidth;
2635 r = (mwidth-1)/2;
2637 slot_map = wmalloc(mwidth*mwidth);
2638 memset(slot_map, 0, mwidth*mwidth);
2640 #define XY2OFS(x,y) (MAX(abs(x),abs(y)) > r) ? 0 : (((y)+r)*(mwidth)+(x)+r)
2642 /* mark used slots in the map. If the slot falls outside the map
2643 * (for example, when all icons are placed in line), ignore them. */
2644 for (i=0; i<dock->max_icons; i++) {
2645 btn = dock->icon_array[i];
2646 if (btn)
2647 slot_map[XY2OFS(btn->xindex, btn->yindex)] = 1;
2649 /* Find closest slot from the center that is free by scanning the
2650 * map from the center to outward in circular passes.
2651 * This will not result in a neat layout, but will be optimal
2652 * in the sense that there will not be holes left.
2654 done = 0;
2655 for (i = 1; i <= r && !done; i++) {
2656 int tx, ty;
2658 /* top and bottom parts of the ring */
2659 for (x = -i; x <= i && !done; x++) {
2660 tx = dock->x_pos + x*ICON_SIZE;
2661 y = -i;
2662 ty = dock->y_pos + y*ICON_SIZE;
2663 if (slot_map[XY2OFS(x,y)]==0
2664 && ON_SCREEN(tx, ty, sx, ex, sy, ey)) {
2665 *x_pos = x;
2666 *y_pos = y;
2667 done = 1;
2668 break;
2670 y = i;
2671 ty = dock->y_pos + y*ICON_SIZE;
2672 if (slot_map[XY2OFS(x,y)]==0
2673 && ON_SCREEN(tx, ty, sx, ex, sy, ey)) {
2674 *x_pos = x;
2675 *y_pos = y;
2676 done = 1;
2677 break;
2680 /* left and right parts of the ring */
2681 for (y = -i+1; y <= i-1; y++) {
2682 ty = dock->y_pos + y*ICON_SIZE;
2683 x = -i;
2684 tx = dock->x_pos + x*ICON_SIZE;
2685 if (slot_map[XY2OFS(x,y)]==0
2686 && ON_SCREEN(tx, ty, sx, ex, sy, ey)) {
2687 *x_pos = x;
2688 *y_pos = y;
2689 done = 1;
2690 break;
2692 x = i;
2693 tx = dock->x_pos + x*ICON_SIZE;
2694 if (slot_map[XY2OFS(x,y)]==0
2695 && ON_SCREEN(tx, ty, sx, ex, sy, ey)) {
2696 *x_pos = x;
2697 *y_pos = y;
2698 done = 1;
2699 break;
2703 free(slot_map);
2704 #undef XY2OFS
2705 return done;
2709 static void
2710 moveDock(WDock *dock, int new_x, int new_y)
2712 WAppIcon *btn;
2713 int i;
2715 dock->x_pos = new_x;
2716 dock->y_pos = new_y;
2717 for (i=0; i<dock->max_icons; i++) {
2718 btn = dock->icon_array[i];
2719 if (btn) {
2720 btn->x_pos = new_x + btn->xindex*ICON_SIZE;
2721 btn->y_pos = new_y + btn->yindex*ICON_SIZE;
2722 XMoveWindow(dpy, btn->icon->core->window, btn->x_pos, btn->y_pos);
2728 static void
2729 swapDock(WDock *dock)
2731 WScreen *scr = dock->screen_ptr;
2732 WAppIcon *btn;
2733 int x, i;
2736 if (dock->on_right_side) {
2737 x = dock->x_pos = scr->scr_width - ICON_SIZE - DOCK_EXTRA_SPACE;
2738 } else {
2739 x = dock->x_pos = DOCK_EXTRA_SPACE;
2742 for (i=0; i<dock->max_icons; i++) {
2743 btn = dock->icon_array[i];
2744 if (btn) {
2745 btn->x_pos = x;
2746 XMoveWindow(dpy, btn->icon->core->window, btn->x_pos, btn->y_pos);
2750 wScreenUpdateUsableArea(scr);
2754 static pid_t
2755 execCommand(WAppIcon *btn, char *command, WSavedState *state)
2757 WScreen *scr = btn->icon->core->screen_ptr;
2758 pid_t pid;
2759 char **argv;
2760 int argc;
2761 char *cmdline;
2763 cmdline = ExpandOptions(scr, command);
2765 if (scr->flags.dnd_data_convertion_status || !cmdline) {
2766 if (cmdline)
2767 free(cmdline);
2768 if (state)
2769 free(state);
2770 return 0;
2773 ParseCommand(cmdline, &argv, &argc);
2775 if (argv==NULL) {
2776 if (cmdline)
2777 free(cmdline);
2778 if (state)
2779 free(state);
2780 return 0;
2783 if ((pid=fork())==0) {
2784 char **args;
2785 int i;
2787 SetupEnvironment(scr);
2789 #ifdef HAVE_SETPGID
2790 setpgid(0, 0);
2791 #endif
2793 args = malloc(sizeof(char*)*(argc+1));
2794 if (!args)
2795 exit(111);
2796 for (i=0; i<argc; i++) {
2797 args[i] = argv[i];
2799 args[argc] = NULL;
2800 execvp(argv[0], args);
2801 exit(111);
2803 while (argc > 0)
2804 free(argv[--argc]);
2805 free(argv);
2807 if (pid > 0) {
2808 if (!state) {
2809 state = wmalloc(sizeof(WSavedState));
2810 memset(state, 0, sizeof(WSavedState));
2811 state->hidden = -1;
2812 state->miniaturized = -1;
2813 state->shaded = -1;
2814 if (btn->dock == scr->dock)
2815 state->workspace = -1;
2816 else
2817 state->workspace = scr->current_workspace;
2819 wWindowAddSavedState(btn->wm_instance, btn->wm_class, cmdline, pid,
2820 state);
2821 wAddDeathHandler(pid, (WDeathHandler*)trackDeadProcess,
2822 btn->dock);
2823 } else if (state) {
2824 free(state);
2826 free(cmdline);
2827 return pid;
2831 void
2832 wDockHideIcons(WDock *dock)
2834 int i;
2835 WAppIcon *btn;
2837 if (dock==NULL)
2838 return;
2840 btn = dock->icon_array[0];
2842 for (i=1; i<dock->max_icons; i++) {
2843 if (dock->icon_array[i])
2844 XUnmapWindow(dpy, dock->icon_array[i]->icon->core->window);
2846 dock->mapped = 0;
2848 dockIconPaint(btn);
2852 void
2853 wDockShowIcons(WDock *dock)
2855 int i, newlevel;
2856 WAppIcon *btn;
2858 if (dock==NULL)
2859 return;
2861 btn = dock->icon_array[0];
2862 moveDock(dock, btn->x_pos, btn->y_pos);
2864 newlevel = dock->lowered ? WMNormalLevel : WMDockLevel;
2865 ChangeStackingLevel(btn->icon->core, newlevel);
2867 for (i=1; i<dock->max_icons; i++) {
2868 if (dock->icon_array[i]) {
2869 MoveInStackListAbove(dock->icon_array[i]->icon->core,
2870 btn->icon->core);
2871 break;
2875 if (!dock->collapsed) {
2876 for (i=1; i<dock->max_icons; i++) {
2877 if (dock->icon_array[i]) {
2878 XMapWindow(dpy, dock->icon_array[i]->icon->core->window);
2882 dock->mapped = 1;
2884 dockIconPaint(btn);
2888 void
2889 wDockLower(WDock *dock)
2891 int i;
2893 for (i=0; i<dock->max_icons; i++) {
2894 if (dock->icon_array[i])
2895 wLowerFrame(dock->icon_array[i]->icon->core);
2900 void
2901 wDockRaise(WDock *dock)
2903 int i;
2905 for (i=dock->max_icons-1; i>=0; i--) {
2906 if (dock->icon_array[i])
2907 wRaiseFrame(dock->icon_array[i]->icon->core);
2912 void
2913 wDockRaiseLower(WDock *dock)
2915 if (!dock->icon_array[0]->icon->core->stacking->above
2916 ||(dock->icon_array[0]->icon->core->stacking->window_level
2917 !=dock->icon_array[0]->icon->core->stacking->above->stacking->window_level))
2918 wDockLower(dock);
2919 else
2920 wDockRaise(dock);
2924 void
2925 wDockFinishLaunch(WDock *dock, WAppIcon *icon)
2927 icon->launching = 0;
2928 icon->relaunching = 0;
2929 dockIconPaint(icon);
2933 WAppIcon*
2934 wDockFindIconFor(WDock *dock, Window window)
2936 WAppIcon *icon;
2937 int i;
2939 for (i=0; i<dock->max_icons; i++) {
2940 icon = dock->icon_array[i];
2941 if (icon && icon->main_window == window)
2942 return icon;
2944 return NULL;
2948 void
2949 wDockTrackWindowLaunch(WDock *dock, Window window)
2951 WAppIcon *icon;
2952 #ifdef REDUCE_APPICONS
2953 WAppIconAppList *tapplist;
2954 #endif
2955 char *wm_class, *wm_instance;
2956 int i;
2957 Bool firstPass = True;
2958 Bool found = False;
2959 char *command = NULL;
2962 int argc;
2963 char **argv;
2965 if (XGetCommand(dpy, window, &argv, &argc)) {
2966 if (argc > 0 && argv != NULL)
2967 command = FlattenStringList(argv,argc);
2968 if (argv) {
2969 XFreeStringList(argv);
2974 if (!PropGetWMClass(window, &wm_class, &wm_instance) ||
2975 (!wm_class && !wm_instance))
2976 return;
2978 retry:
2979 for (i=0; i<dock->max_icons; i++) {
2980 icon = dock->icon_array[i];
2981 if (!icon)
2982 continue;
2984 /* app is already attached to icon */
2985 if (icon->main_window == window) {
2986 found = True;
2987 break;
2990 if ((icon->wm_instance || icon->wm_class)
2991 && (icon->launching
2992 || (dock->screen_ptr->flags.startup && !icon->running))) {
2994 if (icon->wm_instance && wm_instance &&
2995 strcmp(icon->wm_instance, wm_instance)!=0) {
2996 continue;
2998 if (icon->wm_class && wm_class &&
2999 strcmp(icon->wm_class, wm_class)!=0) {
3000 continue;
3002 if (firstPass && command && strcmp(icon->command, command)!=0) {
3003 continue;
3006 if (!icon->relaunching) {
3007 WApplication *wapp;
3009 /* Possibly an application that was docked with dockit,
3010 * but the user did not update WMState to indicate that
3011 * it was docked by force */
3012 wapp = wApplicationOf(window);
3013 if (!wapp) {
3014 icon->forced_dock = 1;
3015 icon->running = 0;
3017 if (!icon->forced_dock)
3018 icon->main_window = window;
3020 #ifdef REDUCE_APPICONS
3021 tapplist = wmalloc(sizeof(WAppIconAppList));
3022 memset(tapplist, 0, sizeof(WAppIconAppList));
3023 tapplist->next = icon->applist;
3024 if (icon->applist)
3025 icon->applist->prev = tapplist;
3026 icon->applist = tapplist;
3027 tapplist->wapp = wApplicationOf(window);
3028 icon->num_apps++;
3029 #endif
3031 found = True;
3032 wDockFinishLaunch(dock, icon);
3033 break;
3037 if (firstPass && !found) {
3038 firstPass = False;
3039 goto retry;
3042 if (command)
3043 free(command);
3045 if (wm_class)
3046 XFree(wm_class);
3047 if (wm_instance)
3048 XFree(wm_instance);
3053 void
3054 wClipUpdateForWorkspaceChange(WScreen *scr, int workspace)
3056 if (!wPreferences.flags.noclip) {
3057 scr->clip_icon->dock = scr->workspaces[workspace]->clip;
3058 if (scr->current_workspace != workspace) {
3059 WDock *old_clip = scr->workspaces[scr->current_workspace]->clip;
3061 wDockHideIcons(old_clip);
3062 if (old_clip->auto_raise_lower) {
3063 if (old_clip->auto_raise_magic) {
3064 WMDeleteTimerHandler(old_clip->auto_raise_magic);
3065 old_clip->auto_raise_magic = NULL;
3067 wDockLower(old_clip);
3069 if (old_clip->auto_collapse) {
3070 if (old_clip->auto_expand_magic) {
3071 WMDeleteTimerHandler(old_clip->auto_expand_magic);
3072 old_clip->auto_expand_magic = NULL;
3074 old_clip->collapsed = 1;
3076 wDockShowIcons(scr->workspaces[workspace]->clip);
3078 if (scr->flags.clip_balloon_mapped)
3079 showClipBalloon(scr->clip_icon->dock, workspace);
3085 static void
3086 trackDeadProcess(pid_t pid, unsigned char status, WDock *dock)
3088 WAppIcon *icon;
3089 int i;
3091 for (i=0; i<dock->max_icons; i++) {
3092 icon = dock->icon_array[i];
3093 if (!icon)
3094 continue;
3096 if (icon->launching && icon->pid == pid) {
3097 if (!icon->relaunching) {
3098 icon->running = 0;
3099 icon->main_window = None;
3101 wDockFinishLaunch(dock, icon);
3102 icon->pid = 0;
3103 if (status==111) {
3104 char msg[PATH_MAX];
3105 #ifdef OFFIX_DND
3106 sprintf(msg, _("Could not execute command \"%s\""),
3107 icon->drop_launch && icon->dnd_command
3108 ? icon->dnd_command : icon->command);
3109 #else
3110 sprintf(msg, _("Could not execute command \"%s\""),
3111 icon->command);
3112 #endif
3113 wMessageDialog(dock->screen_ptr, _("Error"), msg,
3114 _("OK"), NULL, NULL);
3116 break;
3122 static void
3123 toggleLowered(WDock *dock)
3125 WAppIcon *tmp;
3126 int newlevel, i;
3128 /* lower/raise Dock */
3129 if (!dock->lowered) {
3130 newlevel = WMNormalLevel;
3131 dock->lowered = 1;
3132 } else {
3133 newlevel = WMDockLevel;
3134 dock->lowered = 0;
3137 for (i=0; i<dock->max_icons; i++) {
3138 tmp = dock->icon_array[i];
3139 if (!tmp)
3140 continue;
3142 ChangeStackingLevel(tmp->icon->core, newlevel);
3143 if (dock->lowered)
3144 wLowerFrame(tmp->icon->core);
3147 if (dock->type == WM_DOCK)
3148 wScreenUpdateUsableArea(dock->screen_ptr);
3152 static void
3153 toggleCollapsed(WDock *dock)
3155 if (dock->collapsed) {
3156 dock->collapsed = 0;
3157 wDockShowIcons(dock);
3159 else {
3160 dock->collapsed = 1;
3161 wDockHideIcons(dock);
3166 static void
3167 openDockMenu(WDock *dock, WAppIcon *aicon, XEvent *event)
3169 WScreen *scr = dock->screen_ptr;
3170 WObjDescriptor *desc;
3171 WMenuEntry *entry;
3172 WApplication *wapp = NULL;
3173 int index = 0;
3174 int x_pos;
3175 int appIsRunning = aicon->running && aicon->icon && aicon->icon->owner;
3177 if (dock->type == WM_DOCK) {
3178 /* keep on top */
3179 entry = dock->menu->entries[index];
3180 entry->flags.indicator_on = !dock->lowered;
3181 entry->clientdata = dock;
3182 } else {
3183 /* clip options */
3184 if (scr->clip_options)
3185 updateClipOptionsMenu(scr->clip_options, dock);
3187 /* Rename Workspace */
3188 entry = dock->menu->entries[++index];
3189 entry->clientdata = dock;
3191 /* select icon */
3192 entry = dock->menu->entries[++index];
3193 entry->clientdata = aicon;
3194 wMenuSetEnabled(dock->menu, index, aicon!=scr->clip_icon);
3196 /* (un)select all icons */
3197 entry = dock->menu->entries[++index];
3198 entry->clientdata = aicon;
3199 wMenuSetEnabled(dock->menu, index, dock->icon_count > 1);
3201 /* keep icon(s) */
3202 entry = dock->menu->entries[++index];
3203 entry->clientdata = aicon;
3204 wMenuSetEnabled(dock->menu, index, dock->icon_count > 1);
3206 /* this is the workspace submenu part */
3207 if (scr->clip_submenu)
3208 updateWorkspaceMenu(scr->clip_submenu, aicon);
3209 index++;
3211 /* remove icon(s) */
3212 entry = dock->menu->entries[++index];
3213 entry->clientdata = aicon;
3214 wMenuSetEnabled(dock->menu, index, dock->icon_count > 1);
3216 /* attract icon(s) */
3217 entry = dock->menu->entries[++index];
3218 entry->clientdata = aicon;
3221 /* launch */
3222 entry = dock->menu->entries[++index];
3223 entry->clientdata = aicon;
3224 wMenuSetEnabled(dock->menu, index, aicon->command!=NULL);
3226 /* unhide here */
3227 entry = dock->menu->entries[++index];
3228 entry->clientdata = aicon;
3229 wMenuSetEnabled(dock->menu, index, appIsRunning);
3231 /* hide */
3232 entry = dock->menu->entries[++index];
3233 entry->clientdata = aicon;
3234 if (aicon->icon->owner) {
3235 wapp = wApplicationOf(aicon->icon->owner->main_window);
3236 if (wapp && wapp->flags.hidden)
3237 entry->text = _("Unhide");
3238 else
3239 entry->text = _("Hide");
3240 } else {
3241 entry->text = _("Hide");
3243 wMenuSetEnabled(dock->menu, index, appIsRunning);
3245 /* settings */
3246 entry = dock->menu->entries[++index];
3247 entry->clientdata = aicon;
3248 wMenuSetEnabled(dock->menu, index, !aicon->editing
3249 && !wPreferences.flags.noupdates);
3251 /* kill */
3252 entry = dock->menu->entries[++index];
3253 entry->clientdata = aicon;
3254 wMenuSetEnabled(dock->menu, index, appIsRunning);
3256 if (!dock->menu->flags.realized)
3257 wMenuRealize(dock->menu);
3259 if (dock->type == WM_CLIP) {
3260 x_pos = event->xbutton.x_root+2;
3261 } else {
3262 x_pos = dock->on_right_side ?
3263 scr->scr_width - dock->menu->frame->core->width - 2 : 0;
3266 wMenuMapAt(dock->menu, x_pos, event->xbutton.y_root+2, False);
3268 /* allow drag select */
3269 event->xany.send_event = True;
3270 desc = &dock->menu->menu->descriptor;
3271 (*desc->handle_mousedown)(desc, event);
3275 static void
3276 openClipWorkspaceMenu(WScreen *scr, int x, int y)
3278 if (!scr->clip_ws_menu) {
3279 scr->clip_ws_menu = wWorkspaceMenuMake(scr, False);
3281 wWorkspaceMenuUpdate(scr, scr->clip_ws_menu);
3282 wMenuMapAt(scr->clip_ws_menu, x, y, False);
3286 /******************************************************************/
3287 static void
3288 iconDblClick(WObjDescriptor *desc, XEvent *event)
3290 WAppIcon *btn = desc->parent;
3291 WDock *dock = btn->dock;
3292 WApplication *wapp = NULL;
3293 int unhideHere = 0;
3295 #ifdef REDUCE_APPICONS
3296 if ((btn->icon->owner && !(event->xbutton.state & ControlMask)) ||
3297 ((btn->icon->owner == NULL) && (btn->applist != NULL))) {
3298 if (btn->icon->owner == NULL)
3299 btn->icon->owner = btn->applist->wapp->main_window_desc;
3300 #ifdef I_HATE_THIS
3302 #endif
3303 #else
3304 if (btn->icon->owner && !(event->xbutton.state & ControlMask)) {
3305 #endif
3306 wapp = wApplicationOf(btn->icon->owner->main_window);
3308 assert(wapp!=NULL);
3310 unhideHere = (event->xbutton.state & ShiftMask);
3312 /* go to the last workspace that the user worked on the app */
3313 if (!unhideHere) {
3314 wWorkspaceChange(dock->screen_ptr, wapp->last_workspace);
3317 wUnhideApplication(wapp, event->xbutton.button==Button2,
3318 unhideHere);
3320 if (event->xbutton.state & MOD_MASK) {
3321 wHideOtherApplications(btn->icon->owner);
3323 } else {
3324 if (event->xbutton.button==Button1) {
3326 if (event->xbutton.state & MOD_MASK) {
3327 /* raise/lower dock */
3328 toggleLowered(dock);
3329 } else if (btn == dock->screen_ptr->clip_icon) {
3330 if (getClipButton(event->xbutton.x, event->xbutton.y)==CLIP_IDLE)
3331 toggleCollapsed(dock);
3332 else
3333 handleClipChangeWorkspace(dock->screen_ptr, event);
3334 } else if (btn->command) {
3335 if (!btn->launching &&
3336 (!btn->running || (event->xbutton.state & ControlMask))) {
3337 launchDockedApplication(btn);
3345 static void
3346 handleDockMove(WDock *dock, WAppIcon *aicon, XEvent *event)
3348 WScreen *scr = dock->screen_ptr;
3349 int ofs_x=event->xbutton.x, ofs_y=event->xbutton.y;
3350 int x, y;
3351 XEvent ev;
3352 int grabbed = 0, swapped = 0, done;
3353 Pixmap ghost = None;
3354 int superfluous = wPreferences.superfluous; /* we catch it to avoid problems */
3356 #ifdef DEBUG
3357 puts("moving dock");
3358 #endif
3359 if (XGrabPointer(dpy, aicon->icon->core->window, True, ButtonMotionMask
3360 |ButtonReleaseMask|ButtonPressMask, GrabModeAsync,
3361 GrabModeAsync, None, None, CurrentTime) !=GrabSuccess) {
3362 wwarning("pointer grab failed for dock move");
3364 y = 0;
3365 for (x=0; x<dock->max_icons; x++) {
3366 if (dock->icon_array[x]!=NULL &&
3367 dock->icon_array[x]->yindex > y)
3368 y = dock->icon_array[x]->yindex;
3370 y++;
3371 XResizeWindow(dpy, scr->dock_shadow, ICON_SIZE, ICON_SIZE*y);
3373 done = 0;
3374 while (!done) {
3375 WMMaskEvent(dpy, PointerMotionMask|ButtonReleaseMask|ButtonPressMask
3376 |ButtonMotionMask|ExposureMask, &ev);
3377 switch (ev.type) {
3378 case Expose:
3379 WMHandleEvent(&ev);
3380 break;
3382 case MotionNotify:
3383 if (!grabbed) {
3384 if (abs(ofs_x-ev.xmotion.x)>=MOVE_THRESHOLD
3385 || abs(ofs_y-ev.xmotion.y)>=MOVE_THRESHOLD) {
3386 XChangeActivePointerGrab(dpy, ButtonMotionMask
3387 |ButtonReleaseMask|ButtonPressMask,
3388 wCursor[WCUR_MOVE], CurrentTime);
3389 grabbed=1;
3391 break;
3393 if (dock->type == WM_CLIP) {
3394 if (ev.xmotion.x_root - ofs_x < 0) {
3395 x = 0;
3396 } else if (ev.xmotion.x_root - ofs_x + ICON_SIZE >
3397 scr->scr_width) {
3398 x = scr->scr_width - ICON_SIZE;
3399 } else {
3400 x = ev.xmotion.x_root - ofs_x;
3402 if (ev.xmotion.y_root - ofs_y < 0) {
3403 y = 0;
3404 } else if (ev.xmotion.y_root - ofs_y + ICON_SIZE >
3405 scr->scr_height) {
3406 y = scr->scr_height - ICON_SIZE;
3407 } else {
3408 y = ev.xmotion.y_root - ofs_y;
3410 moveDock(dock, x, y);
3411 } else {
3412 /* move vertically if pointer is inside the dock*/
3413 if ((dock->on_right_side &&
3414 ev.xmotion.x_root >= dock->x_pos - ICON_SIZE)
3415 || (!dock->on_right_side &&
3416 ev.xmotion.x_root <= dock->x_pos + ICON_SIZE*2)) {
3418 if (ev.xmotion.y_root - ofs_y < 0) {
3419 y = 0;
3420 } else if (ev.xmotion.y_root - ofs_y + ICON_SIZE >
3421 scr->scr_height) {
3422 y = scr->scr_height - ICON_SIZE;
3423 } else {
3424 y = ev.xmotion.y_root - ofs_y;
3426 moveDock(dock, dock->x_pos, y);
3428 /* move horizontally to change sides */
3429 x = ev.xmotion.x_root - ofs_x;
3430 if (!dock->on_right_side) {
3432 /* is on left */
3434 if (ev.xmotion.x_root > dock->x_pos + ICON_SIZE*2) {
3435 XMoveWindow(dpy, scr->dock_shadow, scr->scr_width-ICON_SIZE
3436 -DOCK_EXTRA_SPACE-1, dock->y_pos);
3437 if (superfluous) {
3438 if (ghost==None) {
3439 ghost = MakeGhostDock(dock, dock->x_pos,
3440 scr->scr_width-ICON_SIZE
3441 -DOCK_EXTRA_SPACE-1,
3442 dock->y_pos);
3443 XSetWindowBackgroundPixmap(dpy, scr->dock_shadow,
3444 ghost);
3445 XClearWindow(dpy, scr->dock_shadow);
3448 XMapRaised(dpy, scr->dock_shadow);
3449 swapped = 1;
3450 } else {
3451 if (superfluous && ghost!=None) {
3452 XFreePixmap(dpy, ghost);
3453 ghost = None;
3455 XUnmapWindow(dpy, scr->dock_shadow);
3456 swapped = 0;
3458 } else {
3459 /* is on right */
3460 if (ev.xmotion.x_root < dock->x_pos - ICON_SIZE) {
3461 XMoveWindow(dpy, scr->dock_shadow,
3462 DOCK_EXTRA_SPACE, dock->y_pos);
3463 if (superfluous) {
3464 if (ghost==None) {
3465 ghost = MakeGhostDock(dock, dock->x_pos,
3466 DOCK_EXTRA_SPACE, dock->y_pos);
3467 XSetWindowBackgroundPixmap(dpy, scr->dock_shadow,
3468 ghost);
3469 XClearWindow(dpy, scr->dock_shadow);
3472 XMapRaised(dpy, scr->dock_shadow);
3473 swapped = -1;
3474 } else {
3475 XUnmapWindow(dpy, scr->dock_shadow);
3476 swapped = 0;
3477 if (superfluous && ghost!=None) {
3478 XFreePixmap(dpy, ghost);
3479 ghost = None;
3484 break;
3486 case ButtonPress:
3487 break;
3489 case ButtonRelease:
3490 if (ev.xbutton.button != event->xbutton.button)
3491 break;
3492 XUngrabPointer(dpy, CurrentTime);
3493 XUnmapWindow(dpy, scr->dock_shadow);
3494 XResizeWindow(dpy, scr->dock_shadow, ICON_SIZE, ICON_SIZE);
3495 if (dock->type == WM_DOCK) {
3496 if (swapped!=0) {
3497 if (swapped>0)
3498 dock->on_right_side = 1;
3499 else
3500 dock->on_right_side = 0;
3501 swapDock(dock);
3502 wArrangeIcons(scr, False);
3505 done = 1;
3506 break;
3509 if (superfluous) {
3510 if (ghost!=None)
3511 XFreePixmap(dpy, ghost);
3512 XSetWindowBackground(dpy, scr->dock_shadow, scr->white_pixel);
3514 #ifdef DEBUG
3515 puts("End dock move");
3516 #endif
3521 static void
3522 handleIconMove(WDock *dock, WAppIcon *aicon, XEvent *event)
3524 WScreen *scr = dock->screen_ptr;
3525 Window wins[2];
3526 WIcon *icon = aicon->icon;
3527 WDock *dock2 = NULL, *last_dock = dock, *clip = NULL;
3528 int ondock, grabbed = 0, change_dock = 0, collapsed = 0;
3529 XEvent ev;
3530 int x = aicon->x_pos, y = aicon->y_pos;
3531 int ofs_x = event->xbutton.x, ofs_y = event->xbutton.y;
3532 int shad_x = x, shad_y = y;
3533 int ix = aicon->xindex, iy = aicon->yindex;
3534 int tmp;
3535 Pixmap ghost = None;
3536 Bool docked;
3537 int superfluous = wPreferences.superfluous; /* we catch it to avoid problems */
3539 if (wPreferences.flags.noupdates)
3540 return;
3542 if (XGrabPointer(dpy, icon->core->window, True, ButtonMotionMask
3543 |ButtonReleaseMask|ButtonPressMask, GrabModeAsync,
3544 GrabModeAsync, None, None, CurrentTime) !=GrabSuccess) {
3545 #ifdef DEBUG0
3546 wwarning("pointer grab failed for icon move");
3547 #endif
3550 wRaiseFrame(icon->core);
3552 if (!wPreferences.flags.noclip)
3553 clip = scr->workspaces[scr->current_workspace]->clip;
3555 if (dock == scr->dock && !wPreferences.flags.noclip)
3556 dock2 = clip;
3557 else if (dock != scr->dock && !wPreferences.flags.nodock)
3558 dock2 = scr->dock;
3560 wins[0] = icon->core->window;
3561 wins[1] = scr->dock_shadow;
3562 XRestackWindows(dpy, wins, 2);
3563 XMoveResizeWindow(dpy, scr->dock_shadow, aicon->x_pos, aicon->y_pos,
3564 ICON_SIZE, ICON_SIZE);
3565 if (superfluous) {
3566 if (icon->pixmap!=None)
3567 ghost = MakeGhostIcon(scr, icon->pixmap);
3568 else
3569 ghost = MakeGhostIcon(scr, icon->core->window);
3571 XSetWindowBackgroundPixmap(dpy, scr->dock_shadow, ghost);
3572 XClearWindow(dpy, scr->dock_shadow);
3574 XMapWindow(dpy, scr->dock_shadow);
3576 ondock = 1;
3579 while(1) {
3580 XMaskEvent(dpy, PointerMotionMask|ButtonReleaseMask|ButtonPressMask
3581 |ButtonMotionMask|ExposureMask, &ev);
3582 switch (ev.type) {
3583 case Expose:
3584 WMHandleEvent(&ev);
3585 break;
3587 case MotionNotify:
3588 if (!grabbed) {
3589 if (abs(ofs_x-ev.xmotion.x)>=MOVE_THRESHOLD
3590 || abs(ofs_y-ev.xmotion.y)>=MOVE_THRESHOLD) {
3591 XChangeActivePointerGrab(dpy, ButtonMotionMask
3592 |ButtonReleaseMask|ButtonPressMask,
3593 wCursor[WCUR_MOVE], CurrentTime);
3594 grabbed=1;
3595 } else {
3596 break;
3600 x = ev.xmotion.x_root - ofs_x;
3601 y = ev.xmotion.y_root - ofs_y;
3602 tmp = wDockSnapIcon(dock, aicon, x, y, &ix, &iy, True);
3603 if (tmp && dock2) {
3604 change_dock = 0;
3605 if (last_dock != dock && collapsed) {
3606 last_dock->collapsed = 1;
3607 wDockHideIcons(last_dock);
3608 collapsed = 0;
3610 if (!collapsed && (collapsed = dock->collapsed)) {
3611 dock->collapsed = 0;
3612 wDockShowIcons(dock);
3614 if (dock->auto_raise_lower)
3615 wDockRaise(dock);
3616 last_dock = dock;
3618 else if (dock2) {
3619 tmp = wDockSnapIcon(dock2, aicon, x, y, &ix, &iy, False);
3620 if (tmp) {
3621 change_dock = 1;
3622 if (last_dock != dock2 && collapsed) {
3623 last_dock->collapsed = 1;
3624 wDockHideIcons(last_dock);
3625 collapsed = 0;
3627 if (!collapsed && (collapsed = dock2->collapsed)) {
3628 dock2->collapsed = 0;
3629 wDockShowIcons(dock2);
3631 if (dock2->auto_raise_lower)
3632 wDockRaise(dock2);
3633 last_dock = dock2;
3636 if (aicon->launching
3637 || (aicon->running && !(ev.xmotion.state & MOD_MASK))
3638 || (!aicon->running && tmp)) {
3639 shad_x = last_dock->x_pos + ix*wPreferences.icon_size;
3640 shad_y = last_dock->y_pos + iy*wPreferences.icon_size;
3642 XMoveWindow(dpy, scr->dock_shadow, shad_x, shad_y);
3644 if (!ondock) {
3645 XMapWindow(dpy, scr->dock_shadow);
3646 #if 0
3647 if (!collapsed && (collapsed = last_dock->collapsed)) {
3648 last_dock->collapsed = 0;
3649 wDockShowIcons(last_dock);
3651 #endif
3653 ondock = 1;
3654 } else {
3655 if (ondock) {
3656 XUnmapWindow(dpy, scr->dock_shadow);
3657 #if 0
3658 if (last_dock && collapsed &&
3659 aicon->running && (ev.xmotion.state & MOD_MASK)) {
3660 last_dock->collapsed = 1;
3661 wDockHideIcons(last_dock);
3662 collapsed = 0;
3664 #endif
3666 ondock = 0;
3668 XMoveWindow(dpy, icon->core->window, x, y);
3669 break;
3671 case ButtonPress:
3672 break;
3674 case ButtonRelease:
3675 if (ev.xbutton.button != event->xbutton.button)
3676 break;
3677 XUngrabPointer(dpy, CurrentTime);
3678 if (ondock) {
3679 SlideWindow(icon->core->window, x, y, shad_x, shad_y);
3680 XUnmapWindow(dpy, scr->dock_shadow);
3681 if (!change_dock) {
3682 reattachIcon(dock, aicon, ix, iy);
3683 if (clip && dock!=clip && clip->auto_raise_lower)
3684 wDockLower(clip);
3685 } else {
3686 docked = moveIconBetweenDocks(dock, dock2, aicon, ix, iy);
3687 if (!docked) {
3688 /* Slide it back if dock rejected it */
3689 SlideWindow(icon->core->window, x, y, aicon->x_pos,
3690 aicon->y_pos);
3691 reattachIcon(dock, aicon, aicon->xindex,aicon->yindex);
3693 if (last_dock->type==WM_CLIP && last_dock->auto_collapse) {
3694 collapsed = 0;
3697 } else {
3698 aicon->x_pos = x;
3699 aicon->y_pos = y;
3700 if (superfluous) {
3701 if (!aicon->running && !wPreferences.no_animations) {
3702 /* We need to deselect it, even if is deselected in
3703 * wDockDetach(), because else DoKaboom() will fail.
3705 if (aicon->icon->selected)
3706 wIconSelect(aicon->icon);
3707 DoKaboom(scr,aicon->icon->core->window, x, y);
3710 if (clip && clip->auto_raise_lower)
3711 wDockLower(clip);
3712 wDockDetach(dock, aicon);
3714 if (collapsed) {
3715 last_dock->collapsed = 1;
3716 wDockHideIcons(last_dock);
3717 collapsed = 0;
3719 if (superfluous) {
3720 if (ghost!=None)
3721 XFreePixmap(dpy, ghost);
3722 XSetWindowBackground(dpy, scr->dock_shadow, scr->white_pixel);
3724 #ifdef DEBUG
3725 puts("End icon move");
3726 #endif
3727 return;
3733 static int
3734 getClipButton(int px, int py)
3736 int pt = (CLIP_BUTTON_SIZE+2)*ICON_SIZE/64;
3738 if (px < 0 || py < 0 || px >= ICON_SIZE || py >= ICON_SIZE)
3739 return CLIP_IDLE;
3741 if (py <= pt-((int)ICON_SIZE-1-px))
3742 return CLIP_FORWARD;
3743 else if (px <= pt-((int)ICON_SIZE-1-py))
3744 return CLIP_REWIND;
3746 return CLIP_IDLE;
3750 static void
3751 handleClipChangeWorkspace(WScreen *scr, XEvent *event)
3753 XEvent ev;
3754 int done, direction, new_ws;
3755 int new_dir;
3756 WDock *clip = scr->clip_icon->dock;
3758 direction = getClipButton(event->xbutton.x, event->xbutton.y);
3760 clip->lclip_button_pushed = direction==CLIP_REWIND;
3761 clip->rclip_button_pushed = direction==CLIP_FORWARD;
3763 wClipIconPaint(scr->clip_icon);
3764 done = 0;
3765 while(!done) {
3766 WMMaskEvent(dpy, ExposureMask|ButtonMotionMask|ButtonReleaseMask
3767 |ButtonPressMask, &ev);
3768 switch (ev.type) {
3769 case Expose:
3770 WMHandleEvent(&ev);
3771 break;
3773 case MotionNotify:
3774 new_dir = getClipButton(ev.xmotion.x, ev.xmotion.y);
3775 if (new_dir != direction) {
3776 direction = new_dir;
3777 clip->lclip_button_pushed = direction==CLIP_REWIND;
3778 clip->rclip_button_pushed = direction==CLIP_FORWARD;
3779 wClipIconPaint(scr->clip_icon);
3781 break;
3783 case ButtonPress:
3784 break;
3786 case ButtonRelease:
3787 if (ev.xbutton.button == event->xbutton.button)
3788 done = 1;
3792 clip->lclip_button_pushed = 0;
3793 clip->rclip_button_pushed = 0;
3795 new_ws = wPreferences.ws_advance || (event->xbutton.state & ControlMask);
3797 if (direction == CLIP_FORWARD) {
3798 if (scr->current_workspace < scr->workspace_count-1)
3799 wWorkspaceChange(scr, scr->current_workspace+1);
3800 else if (new_ws && scr->current_workspace < MAX_WORKSPACES-1)
3801 wWorkspaceChange(scr, scr->current_workspace+1);
3802 else if (wPreferences.ws_cycle)
3803 wWorkspaceChange(scr, 0);
3805 else if (direction == CLIP_REWIND) {
3806 if (scr->current_workspace > 0)
3807 wWorkspaceChange(scr, scr->current_workspace-1);
3808 else if (scr->current_workspace==0 && wPreferences.ws_cycle)
3809 wWorkspaceChange(scr, scr->workspace_count-1);
3812 wClipIconPaint(scr->clip_icon);
3816 static void
3817 iconMouseDown(WObjDescriptor *desc, XEvent *event)
3819 WAppIcon *aicon = desc->parent;
3820 WDock *dock = aicon->dock;
3821 WScreen *scr = aicon->icon->core->screen_ptr;
3823 if (aicon->editing || WCHECK_STATE(WSTATE_MODAL))
3824 return;
3826 scr->last_dock = dock;
3828 if (dock->menu->flags.mapped)
3829 wMenuUnmap(dock->menu);
3831 if (IsDoubleClick(scr, event)) {
3832 /* double-click was not in the main clip icon */
3833 if (dock->type != WM_CLIP || aicon->xindex!=0 || aicon->yindex!=0
3834 || getClipButton(event->xbutton.x, event->xbutton.y)==CLIP_IDLE) {
3835 iconDblClick(desc, event);
3836 return;
3840 if (dock->type == WM_CLIP && scr->flags.clip_balloon_mapped) {
3841 XUnmapWindow(dpy, scr->clip_balloon);
3842 scr->flags.clip_balloon_mapped = 0;
3845 #ifdef DEBUG
3846 puts("handling dock");
3847 #endif
3848 if (event->xbutton.button == Button1) {
3849 if (event->xbutton.state & MOD_MASK)
3850 wDockLower(dock);
3851 else
3852 wDockRaise(dock);
3854 if ((event->xbutton.state & ShiftMask) && aicon!=scr->clip_icon &&
3855 dock->type!=WM_DOCK) {
3856 wIconSelect(aicon->icon);
3857 return;
3860 if (aicon->yindex==0 && aicon->xindex==0) {
3861 if (getClipButton(event->xbutton.x, event->xbutton.y)!=CLIP_IDLE
3862 && dock->type==WM_CLIP)
3863 handleClipChangeWorkspace(scr, event);
3864 else
3865 handleDockMove(dock, aicon, event);
3866 } else
3867 handleIconMove(dock, aicon, event);
3869 } else if (event->xbutton.button==Button2 && dock->type==WM_CLIP &&
3870 aicon->xindex==0 && aicon->yindex==0) {
3871 openClipWorkspaceMenu(scr, event->xbutton.x_root+2,
3872 event->xbutton.y_root+2);
3873 if (scr->clip_ws_menu) {
3874 WMenu *menu;
3875 menu = scr->clip_ws_menu;
3876 desc = &menu->menu->descriptor;
3878 event->xany.send_event = True;
3879 (*desc->handle_mousedown)(desc, event);
3881 } else if (event->xbutton.button == Button3) {
3882 openDockMenu(dock, aicon, event);
3887 static void
3888 showClipBalloon(WDock *dock, int workspace)
3890 int w, h;
3891 int x, y;
3892 WScreen *scr = dock->screen_ptr;
3893 char *text;
3894 Window stack[2];
3896 scr->flags.clip_balloon_mapped = 1;
3897 XMapWindow(dpy, scr->clip_balloon);
3899 text = scr->workspaces[workspace]->name;
3901 w = wTextWidth(scr->clip_title_font->font, text, strlen(text));
3903 h = scr->clip_title_font->height;
3904 XResizeWindow(dpy, scr->clip_balloon, w, h);
3906 x = dock->x_pos + CLIP_BUTTON_SIZE*ICON_SIZE/64;
3907 y = dock->y_pos + ICON_SIZE-scr->clip_title_font->height - 3;
3909 if (x+w > scr->scr_width) {
3910 x = scr->scr_width - w;
3911 if (dock->y_pos + ICON_SIZE + h > scr->scr_height)
3912 y = dock->y_pos - h - 1;
3913 else
3914 y = dock->y_pos + ICON_SIZE;
3915 XRaiseWindow(dpy, scr->clip_balloon);
3916 } else {
3917 stack[0] = scr->clip_icon->icon->core->window;
3918 stack[1] = scr->clip_balloon;
3919 XRestackWindows(dpy, stack, 2);
3921 XMoveWindow(dpy, scr->clip_balloon, x, y);
3922 XSetForeground(dpy, scr->clip_title_gc,
3923 scr->clip_title_pixel[CLIP_NORMAL]);
3924 XClearWindow(dpy, scr->clip_balloon);
3925 wDrawString(scr->clip_balloon, scr->clip_title_font, scr->clip_title_gc,
3926 0, scr->clip_title_font->y, text, strlen(text));
3930 static void
3931 clipEnterNotify(WObjDescriptor *desc, XEvent *event)
3933 WAppIcon *btn = (WAppIcon*)desc->parent;
3934 WDock *dock;
3936 assert(event->type==EnterNotify);
3938 if(desc->parent_type!=WCLASS_DOCK_ICON)
3939 return;
3941 dock = btn->dock;
3942 if (!dock || dock->type!=WM_CLIP)
3943 return;
3945 /* The auto raise/lower code */
3946 if (dock->auto_lower_magic) {
3947 WMDeleteTimerHandler(dock->auto_lower_magic);
3948 dock->auto_lower_magic = NULL;
3950 if (dock->auto_raise_lower && !dock->auto_raise_magic) {
3951 dock->auto_raise_magic = WMAddTimerHandler(AUTO_RAISE_DELAY,
3952 clipAutoRaise,
3953 (void *)dock);
3956 /* The auto expand/collapse code */
3957 if (dock->auto_collapse_magic) {
3958 WMDeleteTimerHandler(dock->auto_collapse_magic);
3959 dock->auto_collapse_magic = NULL;
3961 if (dock->auto_collapse && !dock->auto_expand_magic) {
3962 dock->auto_expand_magic = WMAddTimerHandler(AUTO_EXPAND_DELAY,
3963 clipAutoExpand,
3964 (void *)dock);
3967 if (btn->xindex == 0 && btn->yindex == 0)
3968 showClipBalloon(dock, dock->screen_ptr->current_workspace);
3969 else {
3970 if (dock->screen_ptr->flags.clip_balloon_mapped) {
3971 XUnmapWindow(dpy, dock->screen_ptr->clip_balloon);
3972 dock->screen_ptr->flags.clip_balloon_mapped = 0;
3978 static void
3979 clipLeave(WDock *dock)
3981 XEvent event;
3982 WObjDescriptor *desc = NULL;
3984 if (!dock || dock->type!=WM_CLIP)
3985 return;
3987 if (XCheckTypedEvent(dpy, EnterNotify, &event)!=False) {
3988 if (XFindContext(dpy, event.xcrossing.window, wWinContext,
3989 (XPointer *)&desc)!=XCNOENT
3990 && desc && desc->parent_type==WCLASS_DOCK_ICON
3991 && ((WAppIcon*)desc->parent)->dock
3992 && ((WAppIcon*)desc->parent)->dock->type==WM_CLIP) {
3993 /* We didn't left the Clip yet */
3994 XPutBackEvent(dpy, &event);
3995 return;
3998 XPutBackEvent(dpy, &event);
3999 } else {
4000 /* We entered a withdrawn window, so we're still in Clip */
4001 return;
4004 if (dock->auto_raise_magic) {
4005 WMDeleteTimerHandler(dock->auto_raise_magic);
4006 dock->auto_raise_magic = NULL;
4008 if (dock->auto_raise_lower && !dock->auto_lower_magic) {
4009 dock->auto_lower_magic = WMAddTimerHandler(AUTO_LOWER_DELAY,
4010 clipAutoLower,
4011 (void *)dock);
4014 if (dock->auto_expand_magic) {
4015 WMDeleteTimerHandler(dock->auto_expand_magic);
4016 dock->auto_expand_magic = NULL;
4018 if (dock->auto_collapse && !dock->auto_collapse_magic) {
4019 dock->auto_collapse_magic = WMAddTimerHandler(AUTO_COLLAPSE_DELAY,
4020 clipAutoCollapse,
4021 (void *)dock);
4026 static void
4027 clipLeaveNotify(WObjDescriptor *desc, XEvent *event)
4029 WAppIcon *btn = (WAppIcon*)desc->parent;
4031 assert(event->type==LeaveNotify);
4033 if(desc->parent_type!=WCLASS_DOCK_ICON)
4034 return;
4036 clipLeave(btn->dock);
4040 static void
4041 clipAutoCollapse(void *cdata)
4043 WDock *dock = (WDock *)cdata;
4045 if (dock->type!=WM_CLIP)
4046 return;
4048 if (dock->auto_collapse) {
4049 dock->collapsed = 1;
4050 wDockHideIcons(dock);
4052 dock->auto_collapse_magic = NULL;
4056 static void
4057 clipAutoExpand(void *cdata)
4059 WDock *dock = (WDock *)cdata;
4061 if (dock->type!=WM_CLIP)
4062 return;
4064 if (dock->auto_collapse) {
4065 dock->collapsed = 0;
4066 wDockShowIcons(dock);
4068 dock->auto_expand_magic = NULL;
4072 static void
4073 clipAutoLower(void *cdata)
4075 WDock *dock = (WDock *)cdata;
4077 if (dock->type!=WM_CLIP)
4078 return;
4080 if (dock->auto_raise_lower)
4081 wDockLower(dock);
4083 dock->auto_lower_magic = NULL;
4087 static void
4088 clipAutoRaise(void *cdata)
4090 WDock *dock = (WDock *)cdata;
4092 if (dock->type!=WM_CLIP)
4093 return;
4095 if (dock->auto_raise_lower)
4096 wDockRaise(dock);
4098 if (dock->screen_ptr->flags.clip_balloon_mapped) {
4099 showClipBalloon(dock, dock->screen_ptr->current_workspace);
4102 dock->auto_raise_magic = NULL;