Fixed some problems with WMGetDefaultsFromPath().
[wmaker-crm.git] / src / dock.c
blob5afc9ba37f6ab3dad1c8a682f48202dfd8f5f3b7
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 /* arrow bevel */
429 ROperateLine(tile, RSubtractOperation, ICON_SIZE - 5 - as, 5,
430 ICON_SIZE - 6, 5, &dark);
431 ROperateLine(tile, RSubtractOperation, ICON_SIZE - 6 - as, 5,
432 ICON_SIZE - 6, 5 + as, &dark);
433 ROperateLine(tile, RAddOperation, ICON_SIZE - 6, 5, ICON_SIZE - 6, 5 + as,
434 &light);
436 /* bottom left */
437 ROperateLine(tile, RAddOperation, 2, tp+2, pt-2,
438 wPreferences.icon_size-3, &dark);
439 RDrawLine(tile, 0, tp-1, pt+1, wPreferences.icon_size-1, &black);
440 ROperateLine(tile, RSubtractOperation, 0, tp-2, pt+1,
441 wPreferences.icon_size-2, &light);
443 /* arrow bevel */
444 ROperateLine(tile, RSubtractOperation, 5, ICON_SIZE - 6 - as,
445 5 + as, ICON_SIZE - 6, &dark);
446 ROperateLine(tile, RSubtractOperation, 5, ICON_SIZE - 5 - as, 5,
447 ICON_SIZE - 6, &dark);
448 ROperateLine(tile, RAddOperation, 5, ICON_SIZE - 6, 5 + as, ICON_SIZE - 6,
449 &light);
451 return tile;
455 static void
456 removeIconsCallback(WMenu *menu, WMenuEntry *entry)
458 WAppIcon *clickedIcon = (WAppIcon*)entry->clientdata;
459 WDock *dock;
460 WAppIcon *aicon;
461 LinkedList *selectedIcons;
462 int keepit;
464 assert(clickedIcon!=NULL);
466 dock = clickedIcon->dock;
468 selectedIcons = getSelected(dock);
470 if (selectedIcons) {
471 if (wMessageDialog(dock->screen_ptr, _("Workspace Clip"),
472 _("All selected icons will be removed!"),
473 _("OK"), _("Cancel"), NULL)!=WAPRDefault) {
474 return;
476 } else {
477 if (clickedIcon->xindex==0 && clickedIcon->yindex==0)
478 return;
479 selectedIcons = list_cons(clickedIcon, NULL);
482 while (selectedIcons) {
483 aicon = selectedIcons->head;
484 keepit = aicon->running && wApplicationOf(aicon->main_window);
485 wDockDetach(dock, aicon);
486 if (keepit) {
487 PlaceIcon(dock->screen_ptr, &aicon->x_pos, &aicon->y_pos);
488 XMoveWindow(dpy, aicon->icon->core->window,
489 aicon->x_pos, aicon->y_pos);
490 if (!dock->mapped || dock->collapsed)
491 XMapWindow(dpy, aicon->icon->core->window);
493 list_remove_head(&selectedIcons);
496 if (wPreferences.auto_arrange_icons)
497 wArrangeIcons(dock->screen_ptr, True);
501 static void
502 keepIconsCallback(WMenu *menu, WMenuEntry *entry)
504 WAppIcon *clickedIcon = (WAppIcon*)entry->clientdata;
505 WDock *dock;
506 WAppIcon *aicon;
507 LinkedList *selectedIcons;
509 assert(clickedIcon!=NULL);
510 dock = clickedIcon->dock;
512 selectedIcons = getSelected(dock);
514 if (!selectedIcons && clickedIcon!=dock->screen_ptr->clip_icon) {
515 char *command = NULL;
517 if (!clickedIcon->command && !clickedIcon->editing) {
518 clickedIcon->editing = 1;
519 if (wInputDialog(dock->screen_ptr, _("Keep Icon"),
520 _("Type the command used to launch the application"),
521 &command)) {
522 if (command && (command[0]==0 ||
523 (command[0]=='-' && command[1]==0))) {
524 free(command);
525 command = NULL;
527 clickedIcon->command = command;
528 clickedIcon->editing = 0;
529 } else {
530 clickedIcon->editing = 0;
531 if (command)
532 free(command);
533 return;
537 selectedIcons = list_cons(clickedIcon, NULL);
540 while (selectedIcons) {
541 aicon = selectedIcons->head;
542 if (aicon->icon->selected)
543 wIconSelect(aicon->icon);
544 if (aicon && aicon->attracted && aicon->command) {
545 aicon->attracted = 0;
546 if (aicon->icon->shadowed) {
547 aicon->icon->shadowed = 0;
548 aicon->icon->force_paint = 1;
549 wAppIconPaint(aicon);
552 list_remove_head(&selectedIcons);
559 static void
560 toggleAutoAttractCallback(WMenu *menu, WMenuEntry *entry)
562 WDock *dock = (WDock*)entry->clientdata;
564 assert(entry->clientdata!=NULL);
566 dock->attract_icons = !dock->attract_icons;
567 /*if (!dock->attract_icons)
568 dock->keep_attracted = 0;*/
570 entry->flags.indicator_on = dock->attract_icons;
572 wMenuPaint(menu);
576 static void
577 toggleKeepCallback(WMenu *menu, WMenuEntry *entry)
579 WDock *dock = (WDock*)entry->clientdata;
580 WAppIcon *btn;
581 int i;
583 assert(entry->clientdata!=NULL);
585 dock->keep_attracted = !dock->keep_attracted;
587 if (dock->keep_attracted) {
588 for (i=0; i< dock->max_icons; i++) {
589 btn = dock->icon_array[i];
590 if (btn && btn->attracted && btn->command) {
591 btn->attracted = 0;
592 if (btn->icon->shadowed) {
593 btn->icon->shadowed = 0;
594 btn->icon->force_paint = 1;
595 wAppIconPaint(btn);
601 entry->flags.indicator_on = dock->keep_attracted;
603 wMenuPaint(menu);
607 static void
608 selectCallback(WMenu *menu, WMenuEntry *entry)
610 WAppIcon *icon = (WAppIcon*)entry->clientdata;
612 assert(icon!=NULL);
614 wIconSelect(icon->icon);
616 wMenuPaint(menu);
620 static void
621 colectIconsCallback(WMenu *menu, WMenuEntry *entry)
623 WAppIcon *clickedIcon = (WAppIcon*)entry->clientdata;
624 WDock *clip;
625 WAppIcon *aicon;
626 int x, y, x_pos, y_pos;
628 assert(entry->clientdata!=NULL);
629 clip = clickedIcon->dock;
631 aicon = clip->screen_ptr->app_icon_list;
633 while (aicon) {
634 if (!aicon->docked && wDockFindFreeSlot(clip, &x, &y)) {
635 x_pos = clip->x_pos + x*ICON_SIZE;
636 y_pos = clip->y_pos + y*ICON_SIZE;
637 if (aicon->x_pos != x_pos || aicon->y_pos != y_pos) {
638 #ifdef ANIMATIONS
639 if (wPreferences.no_animations) {
640 XMoveWindow(dpy, aicon->icon->core->window, x_pos, y_pos);
641 } else {
642 SlideWindow(aicon->icon->core->window,
643 aicon->x_pos, aicon->y_pos, x_pos, y_pos);
645 #else
646 XMoveWindow(dpy, aicon->icon->core->window, x_pos, y_pos);
647 #endif /* ANIMATIONS */
649 aicon->attracted = 1;
650 if (!clip->keep_attracted && !aicon->icon->shadowed) {
651 aicon->icon->shadowed = 1;
652 aicon->icon->force_paint = 1;
653 /* We don't do an wAppIconPaint() here because it's in
654 * wDockAttachIcon(). -Dan
657 wDockAttachIcon(clip, aicon, x, y);
658 if (clip->collapsed || !clip->mapped)
659 XUnmapWindow(dpy, aicon->icon->core->window);
661 aicon = aicon->next;
666 static void
667 selectIconsCallback(WMenu *menu, WMenuEntry *entry)
669 WAppIcon *clickedIcon = (WAppIcon*)entry->clientdata;
670 WDock *dock;
671 LinkedList *selectedIcons;
672 WAppIcon *btn;
673 int i;
675 assert(clickedIcon!=NULL);
676 dock = clickedIcon->dock;
678 selectedIcons = getSelected(dock);
680 if (!selectedIcons) {
681 for (i=1; i<dock->max_icons; i++) {
682 btn = dock->icon_array[i];
683 if (btn && !btn->icon->selected) {
684 wIconSelect(btn->icon);
687 } else {
688 while(selectedIcons) {
689 btn = selectedIcons->head;
690 wIconSelect(btn->icon);
691 list_remove_head(&selectedIcons);
695 wMenuPaint(menu);
699 static void
700 toggleCollapsedCallback(WMenu *menu, WMenuEntry *entry)
702 assert(entry->clientdata!=NULL);
704 toggleCollapsed(entry->clientdata);
706 entry->flags.indicator_on = ((WDock*)entry->clientdata)->collapsed;
708 wMenuPaint(menu);
712 static void
713 toggleAutoCollapseCallback(WMenu *menu, WMenuEntry *entry)
715 WDock *dock;
716 assert(entry->clientdata!=NULL);
718 dock = (WDock*) entry->clientdata;
720 dock->auto_collapse = !dock->auto_collapse;
722 entry->flags.indicator_on = ((WDock*)entry->clientdata)->auto_collapse;
724 wMenuPaint(menu);
728 static void
729 toggleAutoRaiseLowerCallback(WMenu *menu, WMenuEntry *entry)
731 WDock *dock;
732 assert(entry->clientdata!=NULL);
734 dock = (WDock*) entry->clientdata;
736 dock->auto_raise_lower = !dock->auto_raise_lower;
738 entry->flags.indicator_on = ((WDock*)entry->clientdata)->auto_raise_lower;
740 wMenuPaint(menu);
744 static void
745 launchCallback(WMenu *menu, WMenuEntry *entry)
747 WAppIcon *btn = (WAppIcon*)entry->clientdata;
749 launchDockedApplication(btn);
753 static void
754 settingsCallback(WMenu *menu, WMenuEntry *entry)
756 WAppIcon *btn = (WAppIcon*)entry->clientdata;
758 if (btn->editing)
759 return;
760 ShowDockAppSettingsPanel(btn);
764 static void
765 hideCallback(WMenu *menu, WMenuEntry *entry)
767 WApplication *wapp;
768 WAppIcon *btn = (WAppIcon*)entry->clientdata;
770 wapp = wApplicationOf(btn->icon->owner->main_window);
772 if (wapp->flags.hidden) {
773 wWorkspaceChange(btn->icon->core->screen_ptr,wapp->last_workspace);
774 wUnhideApplication(wapp, False, False);
775 } else {
776 wHideApplication(wapp);
781 static void
782 unhideHereCallback(WMenu *menu, WMenuEntry *entry)
784 WApplication *wapp;
785 WAppIcon *btn = (WAppIcon*)entry->clientdata;
787 wapp = wApplicationOf(btn->icon->owner->main_window);
789 wUnhideApplication(wapp, False, True);
793 WAppIcon*
794 mainIconCreate(WScreen *scr, int type)
796 WAppIcon *btn;
797 int x_pos;
799 if (type == WM_CLIP) {
800 if (scr->clip_icon)
801 return scr->clip_icon;
802 btn = wAppIconCreateForDock(scr, NULL, "Logo", "WMClip", TILE_CLIP);
803 btn->icon->core->descriptor.handle_expose = clipIconExpose;
804 btn->icon->core->descriptor.handle_enternotify = clipEnterNotify;
805 btn->icon->core->descriptor.handle_leavenotify = clipLeaveNotify;
806 /*x_pos = scr->scr_width - ICON_SIZE*2 - DOCK_EXTRA_SPACE;*/
807 x_pos = 0;
808 } else {
809 btn = wAppIconCreateForDock(scr, NULL, "Logo", "WMDock", TILE_NORMAL);
810 x_pos = scr->scr_width - ICON_SIZE - DOCK_EXTRA_SPACE;
813 btn->xindex = 0;
814 btn->yindex = 0;
816 btn->icon->core->descriptor.handle_mousedown = iconMouseDown;
817 btn->icon->core->descriptor.parent_type = WCLASS_DOCK_ICON;
818 btn->icon->core->descriptor.parent = btn;
819 /*ChangeStackingLevel(btn->icon->core, WMDockLevel);*/
820 XMapWindow(dpy, btn->icon->core->window);
821 btn->x_pos = x_pos;
822 btn->y_pos = 0;
823 btn->docked = 1;
824 if (type == WM_CLIP)
825 scr->clip_icon = btn;
827 return btn;
831 static void
832 switchWSCommand(WMenu *menu, WMenuEntry *entry)
834 WAppIcon *btn, *icon = (WAppIcon*) entry->clientdata;
835 WScreen *scr = icon->icon->core->screen_ptr;
836 WDock *src, *dest;
837 LinkedList *selectedIcons;
838 int x, y;
840 if (entry->order == scr->current_workspace)
841 return;
842 src = icon->dock;
843 dest = scr->workspaces[entry->order]->clip;
845 selectedIcons = getSelected(src);
847 if (selectedIcons) {
848 while(selectedIcons) {
849 btn = selectedIcons->head;
850 if (wDockFindFreeSlot(dest, &x, &y)) {
851 moveIconBetweenDocks(src, dest, btn, x, y);
852 XUnmapWindow(dpy, btn->icon->core->window);
854 list_remove_head(&selectedIcons);
856 } else if (icon != scr->clip_icon) {
857 if (wDockFindFreeSlot(dest, &x, &y)) {
858 moveIconBetweenDocks(src, dest, icon, x, y);
859 XUnmapWindow(dpy, icon->icon->core->window);
866 static void
867 launchDockedApplication(WAppIcon *btn)
869 WScreen *scr = btn->icon->core->screen_ptr;
871 if (!btn->launching && btn->command!=NULL) {
872 if (!btn->forced_dock) {
873 btn->relaunching = btn->running;
874 btn->running = 1;
876 if (btn->wm_instance || btn->wm_class) {
877 WWindowAttributes attr;
878 memset(&attr, 0, sizeof(WWindowAttributes));
879 wDefaultFillAttributes(scr, btn->wm_instance, btn->wm_class,
880 &attr, NULL, True);
882 if (!attr.no_appicon && !btn->buggy_app)
883 btn->launching = 1;
884 else
885 btn->running = 0;
887 btn->drop_launch = 0;
888 scr->last_dock = btn->dock;
889 btn->pid = execCommand(btn, btn->command, NULL);
890 if (btn->pid>0) {
891 if (btn->buggy_app) {
892 /* give feedback that the app was launched */
893 btn->launching = 1;
894 dockIconPaint(btn);
895 btn->launching = 0;
896 WMAddTimerHandler(200, (WMCallback*)dockIconPaint, btn);
897 } else {
898 dockIconPaint(btn);
900 } else {
901 wwarning(_("could not launch application %s\n"), btn->command);
902 btn->launching = 0;
903 if (!btn->relaunching)
904 btn->running = 0;
911 static void
912 updateWorkspaceMenu(WMenu *menu, WAppIcon *icon)
914 WScreen *scr = menu->frame->screen_ptr;
915 char title[MAX_WORKSPACENAME_WIDTH+1];
916 int i;
918 if (!menu || !icon)
919 return;
921 for (i=0; i<scr->workspace_count; i++) {
922 if (i < menu->entry_no) {
923 if (strcmp(menu->entries[i]->text,scr->workspaces[i]->name)!=0) {
924 free(menu->entries[i]->text);
925 strcpy(title, scr->workspaces[i]->name);
926 menu->entries[i]->text = wstrdup(title);
927 menu->flags.realized = 0;
929 menu->entries[i]->clientdata = (void*)icon;
930 } else {
931 strcpy(title, scr->workspaces[i]->name);
933 wMenuAddCallback(menu, title, switchWSCommand, (void*)icon);
935 menu->flags.realized = 0;
937 if (i == scr->current_workspace) {
938 wMenuSetEnabled(menu, i, False);
939 } else {
940 wMenuSetEnabled(menu, i, True);
944 if (!menu->flags.realized)
945 wMenuRealize(menu);
949 static WMenu*
950 makeWorkspaceMenu(WScreen *scr)
952 WMenu *menu;
954 menu = wMenuCreate(scr, NULL, False);
955 if (!menu)
956 wwarning(_("could not create workspace submenu for Clip menu"));
958 wMenuAddCallback(menu, "", switchWSCommand, (void*)scr->clip_icon);
960 menu->flags.realized = 0;
961 wMenuRealize(menu);
963 return menu;
967 static void
968 updateClipOptionsMenu(WMenu *menu, WDock *dock)
970 WMenuEntry *entry;
971 int index = 0;
973 if (!menu || !dock)
974 return;
976 /* keep on top */
977 entry = menu->entries[index];
978 entry->flags.indicator_on = !dock->lowered;
979 entry->clientdata = dock;
981 /* collapsed */
982 entry = menu->entries[++index];
983 entry->flags.indicator_on = dock->collapsed;
984 entry->clientdata = dock;
986 /* auto-collapse */
987 entry = menu->entries[++index];
988 entry->flags.indicator_on = dock->auto_collapse;
989 entry->clientdata = dock;
991 /* auto-raise/lower */
992 entry = menu->entries[++index];
993 entry->flags.indicator_on = dock->auto_raise_lower;
994 entry->clientdata = dock;
996 /* attract icons */
997 entry = menu->entries[++index];
998 entry->flags.indicator_on = dock->attract_icons;
999 entry->clientdata = dock;
1001 /* keep attracted icons */
1002 entry = menu->entries[++index];
1003 entry->flags.indicator_on = dock->keep_attracted;
1004 entry->clientdata = dock;
1006 menu->flags.realized = 0;
1007 wMenuRealize(menu);
1011 static WMenu*
1012 makeClipOptionsMenu(WScreen *scr)
1014 WMenu *menu;
1015 WMenuEntry *entry;
1017 menu = wMenuCreate(scr, NULL, False);
1018 if (!menu) {
1019 wwarning(_("could not create options submenu for Clip menu"));
1020 return NULL;
1023 entry = wMenuAddCallback(menu, _("Keep on top"),
1024 toggleLoweredCallback, NULL);
1025 entry->flags.indicator = 1;
1026 entry->flags.indicator_on = 1;
1027 entry->flags.indicator_type = MI_CHECK;
1029 entry = wMenuAddCallback(menu, _("Collapsed"),
1030 toggleCollapsedCallback, NULL);
1031 entry->flags.indicator = 1;
1032 entry->flags.indicator_on = 1;
1033 entry->flags.indicator_type = MI_CHECK;
1035 entry = wMenuAddCallback(menu, _("AutoCollapse"),
1036 toggleAutoCollapseCallback, NULL);
1037 entry->flags.indicator = 1;
1038 entry->flags.indicator_on = 1;
1039 entry->flags.indicator_type = MI_CHECK;
1041 entry = wMenuAddCallback(menu, _("AutoRaiseLower"),
1042 toggleAutoRaiseLowerCallback, NULL);
1043 entry->flags.indicator = 1;
1044 entry->flags.indicator_on = 1;
1045 entry->flags.indicator_type = MI_CHECK;
1047 entry = wMenuAddCallback(menu, _("AutoAttract Icons"),
1048 toggleAutoAttractCallback, NULL);
1049 entry->flags.indicator = 1;
1050 entry->flags.indicator_on = 1;
1051 entry->flags.indicator_type = MI_CHECK;
1053 entry = wMenuAddCallback(menu, _("Keep Attracted Icons"),
1054 toggleKeepCallback, NULL);
1055 entry->flags.indicator = 1;
1056 entry->flags.indicator_on = 1;
1057 entry->flags.indicator_type = MI_CHECK;
1059 menu->flags.realized = 0;
1060 wMenuRealize(menu);
1062 return menu;
1066 static WMenu*
1067 dockMenuCreate(WScreen *scr, int type)
1069 WMenu *menu;
1070 WMenuEntry *entry;
1072 if (type == WM_CLIP && scr->clip_menu)
1073 return scr->clip_menu;
1075 menu = wMenuCreate(scr, NULL, False);
1076 if (type != WM_CLIP) {
1077 entry = wMenuAddCallback(menu, _("Keep on top"),
1078 toggleLoweredCallback, NULL);
1079 entry->flags.indicator = 1;
1080 entry->flags.indicator_on = 1;
1081 entry->flags.indicator_type = MI_CHECK;
1082 } else {
1083 entry = wMenuAddCallback(menu, _("Clip Options"), NULL, NULL);
1084 scr->clip_options = makeClipOptionsMenu(scr);
1085 if (scr->clip_options)
1086 wMenuEntrySetCascade(menu, entry, scr->clip_options);
1088 wMenuAddCallback(menu, _("Rename Workspace"), renameCallback, NULL);
1090 wMenuAddCallback(menu, _("(Un)Select Icon"), selectCallback, NULL);
1092 wMenuAddCallback(menu, _("(Un)Select All Icons"), selectIconsCallback,
1093 NULL);
1095 wMenuAddCallback(menu, _("Keep Icon(s)"), keepIconsCallback, NULL);
1097 entry = wMenuAddCallback(menu, _("Move Icon(s) To"), NULL, NULL);
1098 scr->clip_submenu = makeWorkspaceMenu(scr);
1099 if (scr->clip_submenu)
1100 wMenuEntrySetCascade(menu, entry, scr->clip_submenu);
1102 wMenuAddCallback(menu, _("Remove Icon(s)"), removeIconsCallback, NULL);
1104 wMenuAddCallback(menu, _("Attract Icons"), colectIconsCallback, NULL);
1107 wMenuAddCallback(menu, _("Launch"), launchCallback, NULL);
1109 wMenuAddCallback(menu, _("Unhide Here"), unhideHereCallback, NULL);
1111 entry = wMenuAddCallback(menu, _("Hide"), hideCallback, NULL);
1112 free(entry->text);
1113 entry->text = _("Hide");
1115 wMenuAddCallback(menu, _("Settings..."), settingsCallback, NULL);
1117 wMenuAddCallback(menu, _("Kill"), killCallback, NULL);
1119 if (type == WM_CLIP)
1120 scr->clip_menu = menu;
1122 return menu;
1126 WDock*
1127 wDockCreate(WScreen *scr, int type)
1129 WDock *dock;
1130 WAppIcon *btn;
1131 int icon_count;
1133 make_keys();
1135 dock = wmalloc(sizeof(WDock));
1136 memset(dock, 0, sizeof(WDock));
1138 if (type == WM_CLIP)
1139 icon_count = CLIP_MAX_ICONS;
1140 else
1141 icon_count = scr->scr_height/wPreferences.icon_size;
1143 dock->icon_array = wmalloc(sizeof(WAppIcon*)*icon_count);
1144 memset(dock->icon_array, 0, sizeof(WAppIcon*)*icon_count);
1146 dock->max_icons = icon_count;
1148 btn = mainIconCreate(scr, type);
1150 btn->dock = dock;
1152 dock->x_pos = btn->x_pos;
1153 dock->y_pos = btn->y_pos;
1154 dock->screen_ptr = scr;
1155 dock->type = type;
1156 dock->icon_count = 1;
1157 dock->on_right_side = 1;
1158 dock->collapsed = 0;
1159 dock->auto_collapse = 0;
1160 dock->auto_collapse_magic = NULL;
1161 dock->auto_raise_lower = 0;
1162 dock->auto_lower_magic = NULL;
1163 dock->auto_raise_magic = NULL;
1164 dock->attract_icons = 0;
1165 dock->keep_attracted = 0;
1166 dock->lowered = 1;
1167 dock->icon_array[0] = btn;
1168 wRaiseFrame(btn->icon->core);
1169 XMoveWindow(dpy, btn->icon->core->window, btn->x_pos, btn->y_pos);
1171 /* create dock menu */
1172 dock->menu = dockMenuCreate(scr, type);
1174 return dock;
1178 void
1179 wDockDestroy(WDock *dock)
1181 int i;
1182 WAppIcon *aicon;
1184 for (i=(dock->type == WM_CLIP) ? 1 : 0; i<dock->max_icons; i++) {
1185 aicon = dock->icon_array[i];
1186 if (aicon) {
1187 int keepit = aicon->running && wApplicationOf(aicon->main_window);
1188 wDockDetach(dock, aicon);
1189 if (keepit) {
1190 PlaceIcon(dock->screen_ptr, &aicon->x_pos, &aicon->y_pos);
1191 XMoveWindow(dpy, aicon->icon->core->window,
1192 aicon->x_pos, aicon->y_pos);
1193 if (!dock->mapped || dock->collapsed)
1194 XMapWindow(dpy, aicon->icon->core->window);
1198 if (wPreferences.auto_arrange_icons)
1199 wArrangeIcons(dock->screen_ptr, True);
1200 free(dock->icon_array);
1201 if (dock->menu && dock->type!=WM_CLIP)
1202 wMenuDestroy(dock->menu, True);
1203 free(dock);
1207 void
1208 wClipIconPaint(WAppIcon *aicon)
1210 WScreen *scr = aicon->icon->core->screen_ptr;
1211 WWorkspace *workspace = scr->workspaces[scr->current_workspace];
1212 GC gc;
1213 Window win = aicon->icon->core->window;
1214 int length, nlength;
1215 char *ws_name, ws_number[10];
1216 int ty, tx;
1218 wIconPaint(aicon->icon);
1220 length = strlen(workspace->name);
1221 ws_name = malloc(length + 1);
1222 sprintf(ws_name, "%s", workspace->name);
1223 sprintf(ws_number, "%i", scr->current_workspace + 1);
1224 nlength = strlen(ws_number);
1226 gc = scr->clip_title_gc;
1228 if (!workspace->clip->collapsed)
1229 XSetForeground(dpy, gc, scr->clip_title_pixel[CLIP_NORMAL]);
1230 else
1231 XSetForeground(dpy, gc, scr->clip_title_pixel[CLIP_COLLAPSED]);
1233 ty = ICON_SIZE - scr->clip_title_font->height - 3;
1235 tx = CLIP_BUTTON_SIZE*ICON_SIZE/64;
1237 wDrawString(win, scr->clip_title_font, gc, tx,
1238 ty + scr->clip_title_font->y, ws_name, length);
1240 tx = (ICON_SIZE/2 - wTextWidth(scr->clip_title_font->font, ws_number, nlength))/2;
1242 wDrawString(win, scr->clip_title_font, gc, tx,
1243 scr->clip_title_font->y + 2, ws_number, nlength);
1245 free(ws_name);
1247 if (aicon->launching) {
1248 XFillRectangle(dpy, aicon->icon->core->window, scr->stipple_gc,
1249 0, 0, wPreferences.icon_size, wPreferences.icon_size);
1251 paintClipButtons(aicon, aicon->dock->lclip_button_pushed,
1252 aicon->dock->rclip_button_pushed);
1256 static void
1257 clipIconExpose(WObjDescriptor *desc, XEvent *event)
1259 wClipIconPaint(desc->parent);
1263 static void
1264 dockIconPaint(WAppIcon *btn)
1266 if (btn == btn->icon->core->screen_ptr->clip_icon)
1267 wClipIconPaint(btn);
1268 else
1269 wAppIconPaint(btn);
1273 static proplist_t
1274 make_icon_state(WAppIcon *btn)
1276 proplist_t node = NULL;
1277 proplist_t command, autolaunch, name, forced, host, position, buggy;
1278 char *tmp;
1279 char buffer[64];
1281 if (btn) {
1282 if (!btn->command)
1283 command = PLMakeString("-");
1284 else
1285 command = PLMakeString(btn->command);
1287 autolaunch = btn->auto_launch ? dYes : dNo;
1289 tmp = EscapeWM_CLASS(btn->wm_instance, btn->wm_class);
1291 name = PLMakeString(tmp);
1293 free(tmp);
1295 forced = btn->forced_dock ? dYes : dNo;
1297 buggy = btn->buggy_app ? dYes: dNo;
1299 if (btn == btn->icon->core->screen_ptr->clip_icon)
1300 sprintf(buffer, "%i,%i", btn->x_pos, btn->y_pos);
1301 else
1302 sprintf(buffer, "%hi,%hi", btn->xindex, btn->yindex);
1303 position = PLMakeString(buffer);
1305 node = PLMakeDictionaryFromEntries(dCommand, command,
1306 dName, name,
1307 dAutoLaunch, autolaunch,
1308 dForced, forced,
1309 dBuggyApplication, buggy,
1310 dPosition, position,
1311 NULL);
1312 PLRelease(command);
1313 PLRelease(name);
1314 PLRelease(position);
1315 #ifdef OFFIX_DND
1316 if (btn->dnd_command) {
1317 command = PLMakeString(btn->dnd_command);
1318 PLInsertDictionaryEntry(node, dDropCommand, command);
1319 PLRelease(command);
1321 #endif /* OFFIX_DND */
1323 if (btn->client_machine && btn->remote_start) {
1324 host = PLMakeString(btn->client_machine);
1325 PLInsertDictionaryEntry(node, dHost, host);
1326 PLRelease(host);
1330 return node;
1334 static proplist_t
1335 dockSaveState(WDock *dock)
1337 int i;
1338 proplist_t icon_info;
1339 proplist_t list=NULL, dock_state=NULL;
1340 proplist_t value;
1341 char buffer[256];
1343 list = PLMakeArrayFromElements(NULL);
1345 for (i=(dock->type==WM_DOCK ? 0 : 1); i<dock->max_icons; i++) {
1346 WAppIcon *btn = dock->icon_array[i];
1348 if (!btn || (btn->attracted && !dock->keep_attracted))
1349 continue;
1351 if ((icon_info = make_icon_state(dock->icon_array[i]))) {
1352 list = PLAppendArrayElement(list, icon_info);
1353 PLRelease(icon_info);
1357 dock_state = PLMakeDictionaryFromEntries(dApplications, list, NULL);
1359 PLRelease(list);
1361 if (dock->type == WM_DOCK) {
1362 sprintf(buffer, "%i,%i", (dock->on_right_side ? -ICON_SIZE : 0),
1363 dock->y_pos);
1364 value = PLMakeString(buffer);
1365 PLInsertDictionaryEntry(dock_state, dPosition, value);
1366 PLRelease(value);
1369 value = (dock->lowered ? dYes : dNo);
1370 PLInsertDictionaryEntry(dock_state, dLowered, value);
1372 if (dock->type == WM_CLIP) {
1373 value = (dock->collapsed ? dYes : dNo);
1374 PLInsertDictionaryEntry(dock_state, dCollapsed, value);
1376 value = (dock->auto_collapse ? dYes : dNo);
1377 PLInsertDictionaryEntry(dock_state, dAutoCollapse, value);
1379 value = (dock->auto_raise_lower ? dYes : dNo);
1380 PLInsertDictionaryEntry(dock_state, dAutoRaiseLower, value);
1382 value = (dock->attract_icons ? dYes : dNo);
1383 PLInsertDictionaryEntry(dock_state, dAutoAttractIcons, value);
1385 value = (dock->keep_attracted ? dYes : dNo);
1386 PLInsertDictionaryEntry(dock_state, dKeepAttracted, value);
1389 return dock_state;
1393 void
1394 wDockSaveState(WScreen *scr)
1396 proplist_t dock_state;
1398 dock_state = dockSaveState(scr->dock);
1400 PLInsertDictionaryEntry(scr->session_state, dDock, dock_state);
1402 PLRelease(dock_state);
1406 void
1407 wClipSaveState(WScreen *scr)
1409 proplist_t clip_state;
1411 clip_state = make_icon_state(scr->clip_icon);
1413 PLInsertDictionaryEntry(scr->session_state, dClip, clip_state);
1415 PLRelease(clip_state);
1419 proplist_t
1420 wClipSaveWorkspaceState(WScreen *scr, int workspace)
1422 return dockSaveState(scr->workspaces[workspace]->clip);
1426 static WAppIcon*
1427 restore_icon_state(WScreen *scr, proplist_t info, int type, int index)
1429 WAppIcon *aicon;
1430 char *wclass, *winstance;
1431 proplist_t cmd, value;
1432 char *command;
1435 cmd = PLGetDictionaryEntry(info, dCommand);
1436 if (!cmd || !PLIsString(cmd)) {
1437 return NULL;
1440 /* parse window name */
1441 value = PLGetDictionaryEntry(info, dName);
1442 if (!value)
1443 return NULL;
1445 ParseWindowName(value, &winstance, &wclass, "dock");
1447 if (!winstance && !wclass) {
1448 return NULL;
1451 /* get commands */
1453 if (cmd)
1454 command = wstrdup(PLGetString(cmd));
1455 else
1456 command = NULL;
1458 if (!command || strcmp(command, "-")==0) {
1459 if (command)
1460 free(command);
1461 if (wclass)
1462 free(wclass);
1463 if (winstance)
1464 free(winstance);
1466 return NULL;
1469 aicon = wAppIconCreateForDock(scr, command, winstance, wclass,
1470 TILE_NORMAL);
1471 if (wclass)
1472 free(wclass);
1473 if (winstance)
1474 free(winstance);
1476 aicon->icon->core->descriptor.handle_mousedown = iconMouseDown;
1477 if (type == WM_CLIP) {
1478 aicon->icon->core->descriptor.handle_enternotify = clipEnterNotify;
1479 aicon->icon->core->descriptor.handle_leavenotify = clipLeaveNotify;
1481 aicon->icon->core->descriptor.parent_type = WCLASS_DOCK_ICON;
1482 aicon->icon->core->descriptor.parent = aicon;
1485 #ifdef OFFIX_DND
1486 cmd = PLGetDictionaryEntry(info, dDropCommand);
1487 if (cmd)
1488 aicon->dnd_command = wstrdup(PLGetString(cmd));
1489 #endif
1491 /* check auto launch */
1492 value = PLGetDictionaryEntry(info, dAutoLaunch);
1494 aicon->auto_launch = 0;
1495 if (value) {
1496 if (PLIsString(value)) {
1497 if (strcasecmp(PLGetString(value), "YES")==0)
1498 aicon->auto_launch = 1;
1499 } else {
1500 wwarning(_("bad value in docked icon state info %s"),
1501 PLGetString(dAutoLaunch));
1505 /* check if it wasn't normally docked */
1506 value = PLGetDictionaryEntry(info, dForced);
1508 aicon->forced_dock = 0;
1509 if (value) {
1510 if (PLIsString(value)) {
1511 if (strcasecmp(PLGetString(value), "YES")==0)
1512 aicon->forced_dock = 1;
1513 } else {
1514 wwarning(_("bad value in docked icon state info %s"),
1515 PLGetString(dForced));
1519 /* check if we can rely on the stuff in the app */
1520 value = PLGetDictionaryEntry(info, dBuggyApplication);
1522 aicon->buggy_app = 0;
1523 if (value) {
1524 if (PLIsString(value)) {
1525 if (strcasecmp(PLGetString(value), "YES")==0)
1526 aicon->buggy_app = 1;
1527 } else {
1528 wwarning(_("bad value in docked icon state info %s"),
1529 PLGetString(dBuggyApplication));
1533 /* get position in the dock */
1534 value = PLGetDictionaryEntry(info, dPosition);
1535 if (value && PLIsString(value)) {
1536 if (sscanf(PLGetString(value), "%hi,%hi", &aicon->xindex,
1537 &aicon->yindex)!=2)
1538 wwarning(_("bad value in docked icon state info %s"),
1539 PLGetString(dPosition));
1541 /* check position sanity */
1542 /* incomplete section! */
1543 if (type == WM_DOCK) {
1544 aicon->xindex = 0;
1545 if (aicon->yindex < 0)
1546 wwarning(_("bad value in docked icon position %i,%i"),
1547 aicon->xindex, aicon->yindex);
1549 } else {
1550 aicon->yindex = index;
1551 aicon->xindex = 0;
1554 aicon->running = 0;
1555 aicon->docked = 1;
1557 return aicon;
1561 #define COMPLAIN(key) wwarning(_("bad value in dock state info:%s"), key)
1564 WAppIcon*
1565 wClipRestoreState(WScreen *scr, proplist_t clip_state)
1567 WAppIcon *icon;
1568 proplist_t value;
1571 icon = mainIconCreate(scr, WM_CLIP);
1573 if (!clip_state)
1574 return icon;
1575 else
1576 PLRetain(clip_state);
1578 /* restore position */
1580 value = PLGetDictionaryEntry(clip_state, dPosition);
1582 if (value) {
1583 if (!PLIsString(value))
1584 COMPLAIN("Position");
1585 else {
1586 if (sscanf(PLGetString(value), "%i,%i", &icon->x_pos,
1587 &icon->y_pos)!=2)
1588 COMPLAIN("Position");
1590 /* check position sanity */
1591 if (icon->y_pos < 0)
1592 icon->y_pos = 0;
1593 else if (icon->y_pos > scr->scr_height-ICON_SIZE)
1594 icon->y_pos = scr->scr_height-ICON_SIZE;
1596 if (icon->x_pos < 0)
1597 icon->x_pos = 0;
1598 else if (icon->x_pos > scr->scr_width-ICON_SIZE)
1599 icon->x_pos = scr->scr_width-ICON_SIZE;
1603 #ifdef OFFIX_DND
1604 value = PLGetDictionaryEntry(clip_state, dDropCommand);
1605 if (value && PLIsString(value))
1606 icon->dnd_command = wstrdup(PLGetString(value));
1607 #endif
1609 PLRelease(clip_state);
1611 return icon;
1615 WDock*
1616 wDockRestoreState(WScreen *scr, proplist_t dock_state, int type)
1618 WDock *dock;
1619 proplist_t apps;
1620 proplist_t value;
1621 WAppIcon *aicon, *old_top;
1622 int count, i;
1625 dock = wDockCreate(scr, type);
1627 if (!dock_state)
1628 return dock;
1630 if (dock_state)
1631 PLRetain(dock_state);
1634 /* restore position */
1636 value = PLGetDictionaryEntry(dock_state, dPosition);
1638 if (value) {
1639 if (!PLIsString(value))
1640 COMPLAIN("Position");
1641 else {
1642 if (sscanf(PLGetString(value), "%i,%i", &dock->x_pos,
1643 &dock->y_pos)!=2)
1644 COMPLAIN("Position");
1646 /* check position sanity */
1647 if (dock->y_pos < 0)
1648 dock->y_pos = 0;
1649 else if (dock->y_pos > scr->scr_height-ICON_SIZE)
1650 dock->y_pos = scr->scr_height - ICON_SIZE;
1652 /* This is no more needed. ??? */
1653 if (type == WM_CLIP) {
1654 if (dock->x_pos < 0)
1655 dock->x_pos = 0;
1656 else if (dock->x_pos > scr->scr_width-ICON_SIZE)
1657 dock->x_pos = scr->scr_width-ICON_SIZE;
1658 } else {
1659 if (dock->x_pos >= 0) {
1660 dock->x_pos = DOCK_EXTRA_SPACE;
1661 dock->on_right_side = 0;
1662 } else {
1663 dock->x_pos = scr->scr_width - DOCK_EXTRA_SPACE - ICON_SIZE;
1664 dock->on_right_side = 1;
1670 /* restore lowered/raised state */
1672 dock->lowered = 0;
1674 value = PLGetDictionaryEntry(dock_state, dLowered);
1676 if (value) {
1677 if (!PLIsString(value))
1678 COMPLAIN("Lowered");
1679 else {
1680 if (strcasecmp(PLGetString(value), "YES")==0)
1681 dock->lowered = 1;
1686 /* restore collapsed state */
1688 dock->collapsed = 0;
1690 value = PLGetDictionaryEntry(dock_state, dCollapsed);
1692 if (value) {
1693 if (!PLIsString(value))
1694 COMPLAIN("Collapsed");
1695 else {
1696 if (strcasecmp(PLGetString(value), "YES")==0)
1697 dock->collapsed = 1;
1702 /* restore auto-collapsed state */
1704 value = PLGetDictionaryEntry(dock_state, dAutoCollapse);
1706 if (value) {
1707 if (!PLIsString(value))
1708 COMPLAIN("AutoCollapse");
1709 else {
1710 if (strcasecmp(PLGetString(value), "YES")==0) {
1711 dock->auto_collapse = 1;
1712 dock->collapsed = 1;
1718 /* restore auto-raise/lower state */
1720 value = PLGetDictionaryEntry(dock_state, dAutoRaiseLower);
1722 if (value) {
1723 if (!PLIsString(value))
1724 COMPLAIN("AutoRaiseLower");
1725 else {
1726 if (strcasecmp(PLGetString(value), "YES")==0) {
1727 dock->auto_raise_lower = 1;
1733 /* restore attract icons state */
1735 dock->attract_icons = 0;
1737 value = PLGetDictionaryEntry(dock_state, dAutoAttractIcons);
1739 if (value) {
1740 if (!PLIsString(value))
1741 COMPLAIN("AutoAttractIcons");
1742 else {
1743 if (strcasecmp(PLGetString(value), "YES")==0)
1744 dock->attract_icons = 1;
1749 /* restore keep attracted icons state */
1751 dock->keep_attracted = 0;
1753 value = PLGetDictionaryEntry(dock_state, dKeepAttracted);
1755 if (value) {
1756 if (!PLIsString(value))
1757 COMPLAIN("KeepAttracted");
1758 else {
1759 if (strcasecmp(PLGetString(value), "YES")==0)
1760 dock->keep_attracted = 1;
1765 /* application list */
1767 apps = PLGetDictionaryEntry(dock_state, dApplications);
1769 if (!apps) {
1770 goto finish;
1773 count = PLGetNumberOfElements(apps);
1775 if (count==0)
1776 goto finish;
1778 old_top = dock->icon_array[0];
1780 /* dock->icon_count is set to 1 when dock is created.
1781 * Since Clip is already restored, we want to keep it so for clip,
1782 * but for dock we may change the default top tile, so we set it to 0.
1784 if (type == WM_DOCK)
1785 dock->icon_count = 0;
1787 for (i=0; i<count; i++) {
1788 if (dock->icon_count >= dock->max_icons) {
1789 wwarning(_("there are too many icons stored in dock. Ignoring what doesn't fit"));
1790 break;
1793 value = PLGetArrayElement(apps, i);
1794 aicon = restore_icon_state(scr, value, type, dock->icon_count);
1796 dock->icon_array[dock->icon_count] = aicon;
1798 if (aicon) {
1799 aicon->dock = dock;
1800 aicon->x_pos = dock->x_pos + (aicon->xindex*ICON_SIZE);
1801 aicon->y_pos = dock->y_pos + (aicon->yindex*ICON_SIZE);
1803 if (dock->lowered)
1804 ChangeStackingLevel(aicon->icon->core, WMNormalLevel);
1805 else
1806 ChangeStackingLevel(aicon->icon->core, WMDockLevel);
1808 wCoreConfigure(aicon->icon->core, aicon->x_pos, aicon->y_pos,
1809 0, 0);
1811 if (!dock->collapsed)
1812 XMapWindow(dpy, aicon->icon->core->window);
1813 wRaiseFrame(aicon->icon->core);
1815 dock->icon_count++;
1816 } else if (dock->icon_count==0 && type==WM_DOCK)
1817 dock->icon_count++;
1820 /* if the first icon is not defined, use the default */
1821 if (dock->icon_array[0]==NULL) {
1822 /* update default icon */
1823 old_top->x_pos = dock->x_pos;
1824 old_top->y_pos = dock->y_pos;
1825 if (dock->lowered)
1826 ChangeStackingLevel(old_top->icon->core, WMNormalLevel);
1827 else
1828 ChangeStackingLevel(old_top->icon->core, WMDockLevel);
1829 dock->icon_array[0] = old_top;
1830 XMoveWindow(dpy, old_top->icon->core->window, dock->x_pos, dock->y_pos);
1831 /* we don't need to increment dock->icon_count here because it was
1832 * incremented in the loop above.
1834 } else if (old_top!=dock->icon_array[0]) {
1835 if (old_top == scr->clip_icon)
1836 scr->clip_icon = dock->icon_array[0];
1837 wAppIconDestroy(old_top);
1840 finish:
1841 if (dock_state)
1842 PLRelease(dock_state);
1844 return dock;
1849 void
1850 wDockLaunchWithState(WDock *dock, WAppIcon *btn, WSavedState *state)
1852 if (btn && btn->command && !btn->running && !btn->launching) {
1854 btn->drop_launch = 0;
1856 btn->pid = execCommand(btn, btn->command, state);
1858 if (btn->pid>0) {
1859 if (!btn->forced_dock && !btn->buggy_app) {
1860 btn->launching = 1;
1861 dockIconPaint(btn);
1864 } else {
1865 free(state);
1870 void
1871 wDockDoAutoLaunch(WDock *dock, int workspace)
1873 WAppIcon *btn;
1874 WSavedState *state;
1875 int i;
1877 for (i = 0; i < dock->max_icons; i++) {
1878 btn = dock->icon_array[i];
1879 if (!btn || !btn->auto_launch)
1880 continue;
1882 state = wmalloc(sizeof(WSavedState));
1883 memset(state, 0, sizeof(WSavedState));
1884 state->workspace = workspace;
1885 /* TODO: this is klugy and is very difficult to understand
1886 * what's going on. Try to clean up */
1887 wDockLaunchWithState(dock, btn, state);
1891 #ifdef REDUCE_APPICONS
1892 void
1893 wDockSimulateLaunch(WDock *dock, WAppIcon *btn)
1895 if ((btn == NULL) || (dock == NULL))
1896 return;
1898 if (!btn->running) {
1899 if ((btn->icon->owner == NULL) && (btn->applist))
1900 btn->icon->owner = btn->applist->wapp->main_window_desc;
1901 if (!btn->forced_dock)
1902 btn->launching = 1;
1903 dockIconPaint(btn);
1904 wusleep(5000);
1907 #endif
1909 #ifdef OFFIX_DND
1910 static WDock*
1911 findDock(WScreen *scr, XEvent *event, int *icon_pos)
1913 WDock *dock;
1914 int i;
1916 *icon_pos = -1;
1917 if ((dock = scr->dock)!=NULL) {
1918 for (i=0; i<dock->max_icons; i++) {
1919 if (dock->icon_array[i]
1920 && dock->icon_array[i]->icon->core->window==event->xclient.window) {
1921 *icon_pos = i;
1922 break;
1926 if (*icon_pos<0 && (dock = scr->workspaces[scr->current_workspace]->clip)!=NULL) {
1927 for (i=0; i<dock->max_icons; i++) {
1928 if (dock->icon_array[i]
1929 && dock->icon_array[i]->icon->core->window==event->xclient.window) {
1930 *icon_pos = i;
1931 break;
1935 if(*icon_pos>=0)
1936 return dock;
1937 return NULL;
1942 wDockReceiveDNDDrop(WScreen *scr, XEvent *event)
1944 WDock *dock;
1945 WAppIcon *btn;
1946 int icon_pos;
1948 dock = findDock(scr, event, &icon_pos);
1949 if (!dock)
1950 return False;
1953 * Return True if the drop was on an application icon window.
1954 * In this case, let the ClientMessage handler redirect the
1955 * message to the app.
1957 if (dock->icon_array[icon_pos]->icon->icon_win!=None)
1958 return True;
1960 if (dock->icon_array[icon_pos]->dnd_command!=NULL) {
1961 scr->flags.dnd_data_convertion_status = 0;
1963 btn = dock->icon_array[icon_pos];
1965 if (!btn->forced_dock) {
1966 btn->relaunching = btn->running;
1967 btn->running = 1;
1969 if (btn->wm_instance || btn->wm_class) {
1970 WWindowAttributes attr;
1971 memset(&attr, 0, sizeof(WWindowAttributes));
1972 wDefaultFillAttributes(btn->icon->core->screen_ptr,
1973 btn->wm_instance,
1974 btn->wm_class, &attr, NULL, True);
1976 if (!attr.no_appicon)
1977 btn->launching = 1;
1978 else
1979 btn->running = 0;
1982 btn->drop_launch = 1;
1983 scr->last_dock = dock;
1984 btn->pid = execCommand(btn, btn->dnd_command, NULL);
1985 if (btn->pid>0) {
1986 dockIconPaint(btn);
1987 } else {
1988 btn->launching = 0;
1989 if (!btn->relaunching) {
1990 btn->running = 0;
1994 return False;
1996 #endif /* OFFIX_DND */
2000 Bool
2001 wDockAttachIcon(WDock *dock, WAppIcon *icon, int x, int y)
2003 WWindow *wwin;
2004 char **argv;
2005 int argc;
2006 int index;
2008 wwin = icon->icon->owner;
2009 if (icon->command==NULL) {
2010 icon->editing = 0;
2011 if (XGetCommand(dpy, wwin->client_win, &argv, &argc) && argc>0) {
2013 icon->command = FlattenStringList(argv, argc);
2014 XFreeStringList(argv);
2015 } else {
2016 char *command=NULL;
2018 /* icon->forced_dock = 1;*/
2019 if (!icon->attracted || dock->type!=WM_CLIP || dock->keep_attracted) {
2020 icon->editing = 1;
2021 if (wInputDialog(dock->screen_ptr, _("Dock Icon"),
2022 _("Type the command used to launch the application"),
2023 &command)) {
2024 if (command && (command[0]==0 ||
2025 (command[0]=='-' && command[1]==0))) {
2026 free(command);
2027 command = NULL;
2029 icon->command = command;
2030 icon->editing = 0;
2031 } else {
2032 icon->editing = 0;
2033 if (command)
2034 free(command);
2035 /* If the target is the dock, reject the icon. If
2036 * the target is the clip, make it an attracted icon
2038 if (dock->type==WM_CLIP) {
2039 icon->attracted = 1;
2040 if (!icon->icon->shadowed) {
2041 icon->icon->shadowed = 1;
2042 icon->icon->force_paint = 1;
2044 } else {
2045 return False;
2050 } else {
2051 icon->editing = 0;
2054 for (index=1; index<dock->max_icons; index++)
2055 if (dock->icon_array[index] == NULL)
2056 break;
2057 /* if (index == dock->max_icons)
2058 return; */
2060 assert(index < dock->max_icons);
2062 dock->icon_array[index] = icon;
2063 icon->yindex = y;
2064 icon->xindex = x;
2066 icon->x_pos = dock->x_pos + x*ICON_SIZE;
2067 icon->y_pos = dock->y_pos + y*ICON_SIZE;
2069 dock->icon_count++;
2071 icon->running = 1;
2072 icon->launching = 0;
2073 icon->docked = 1;
2074 icon->dock = dock;
2075 icon->icon->core->descriptor.handle_mousedown = iconMouseDown;
2076 if (dock->type == WM_CLIP) {
2077 icon->icon->core->descriptor.handle_enternotify = clipEnterNotify;
2078 icon->icon->core->descriptor.handle_leavenotify = clipLeaveNotify;
2080 icon->icon->core->descriptor.parent_type = WCLASS_DOCK_ICON;
2081 icon->icon->core->descriptor.parent = icon;
2083 MoveInStackListUnder(dock->icon_array[index-1]->icon->core,
2084 icon->icon->core);
2085 wAppIconMove(icon, icon->x_pos, icon->y_pos);
2086 wAppIconPaint(icon);
2088 if (wPreferences.auto_arrange_icons)
2089 wArrangeIcons(dock->screen_ptr, True);
2091 #ifdef OFFIX_DND
2092 if (icon->command && !icon->dnd_command) {
2093 icon->dnd_command = wmalloc(strlen(icon->command)+8);
2094 sprintf(icon->dnd_command, "%s %%d", icon->command);
2096 #endif
2098 return True;
2102 void
2103 reattachIcon(WDock *dock, WAppIcon *icon, int x, int y)
2105 int index;
2107 for(index=1; index<dock->max_icons; index++) {
2108 if(dock->icon_array[index] == icon)
2109 break;
2111 assert(index < dock->max_icons);
2113 icon->yindex = y;
2114 icon->xindex = x;
2116 icon->x_pos = dock->x_pos + x*ICON_SIZE;
2117 icon->y_pos = dock->y_pos + y*ICON_SIZE;
2121 Bool
2122 moveIconBetweenDocks(WDock *src, WDock *dest, WAppIcon *icon, int x, int y)
2124 WWindow *wwin;
2125 char **argv;
2126 int argc;
2127 int index;
2129 if (dest == NULL)
2130 return False;
2132 wwin = icon->icon->owner;
2135 * For the moment we can't do this if we move icons in Clip from one
2136 * workspace to other, because if we move two or more icons without
2137 * command, the dialog box will not be able to tell us to which of the
2138 * moved icons it applies. -Dan
2140 if ((dest->type==WM_DOCK /*|| dest->keep_attracted*/) && icon->command==NULL) {
2141 if (XGetCommand(dpy, wwin->client_win, &argv, &argc) && argc>0) {
2143 icon->command = FlattenStringList(argv, argc);
2144 XFreeStringList(argv);
2145 } else {
2146 char *command=NULL;
2148 icon->editing = 1;
2149 /* icon->forced_dock = 1;*/
2150 if (wInputDialog(src->screen_ptr, _("Dock Icon"),
2151 _("Type the command used to launch the application"),
2152 &command)) {
2153 if (command && (command[0]==0 ||
2154 (command[0]=='-' && command[1]==0))) {
2155 free(command);
2156 command = NULL;
2158 icon->command = command;
2159 } else {
2160 icon->editing = 0;
2161 if (command)
2162 free(command);
2163 return False;
2165 icon->editing = 0;
2169 for(index=1; index<src->max_icons; index++) {
2170 if(src->icon_array[index] == icon)
2171 break;
2173 assert(index < src->max_icons);
2175 src->icon_array[index] = NULL;
2176 src->icon_count--;
2178 for(index=1; index<dest->max_icons; index++) {
2179 if(dest->icon_array[index] == NULL)
2180 break;
2182 /* if (index == dest->max_icons)
2183 return; */
2185 assert(index < dest->max_icons);
2187 dest->icon_array[index] = icon;
2188 icon->dock = dest;
2190 /* deselect the icon */
2191 if (icon->icon->selected)
2192 wIconSelect(icon->icon);
2194 if (dest->type == WM_DOCK) {
2195 icon->icon->core->descriptor.handle_enternotify = NULL;
2196 icon->icon->core->descriptor.handle_leavenotify = NULL;
2197 } else {
2198 icon->icon->core->descriptor.handle_enternotify = clipEnterNotify;
2199 icon->icon->core->descriptor.handle_leavenotify = clipLeaveNotify;
2202 /* set it to be kept when moving to dock, or to a clip that keep the
2203 * attracted icons.
2204 * Unless the icon does not have a command set
2206 if (icon->command && (dest->type==WM_DOCK || dest->keep_attracted)) {
2207 icon->attracted = 0;
2208 if (icon->icon->shadowed) {
2209 icon->icon->shadowed = 0;
2210 icon->icon->force_paint = 1;
2214 if (src->auto_collapse || src->auto_raise_lower)
2215 clipLeave(src);
2217 icon->yindex = y;
2218 icon->xindex = x;
2220 icon->x_pos = dest->x_pos + x*ICON_SIZE;
2221 icon->y_pos = dest->y_pos + y*ICON_SIZE;
2223 dest->icon_count++;
2225 MoveInStackListUnder(dest->icon_array[index-1]->icon->core,
2226 icon->icon->core);
2227 wAppIconPaint(icon);
2229 return True;
2233 void
2234 wDockDetach(WDock *dock, WAppIcon *icon)
2236 int index;
2238 /* make the settings panel be closed */
2239 if (icon->panel) {
2240 DestroyDockAppSettingsPanel(icon->panel);
2243 icon->docked = 0;
2244 icon->dock = NULL;
2245 icon->attracted = 0;
2246 if (icon->icon->shadowed) {
2247 icon->icon->shadowed = 0;
2248 icon->icon->force_paint = 1;
2251 /* deselect the icon */
2252 if (icon->icon->selected)
2253 wIconSelect(icon->icon);
2255 if (icon->command) {
2256 free(icon->command);
2257 icon->command = NULL;
2259 #ifdef OFFIX_DND
2260 if (icon->dnd_command) {
2261 free(icon->dnd_command);
2262 icon->dnd_command = NULL;
2264 #endif
2266 for (index=1; index<dock->max_icons; index++)
2267 if (dock->icon_array[index] == icon)
2268 break;
2269 assert(index < dock->max_icons);
2270 dock->icon_array[index] = NULL;
2271 icon->yindex = -1;
2272 icon->xindex = -1;
2273 dock->icon_count--;
2275 /* if the dock is not attached to an application or
2276 * the the application did not set the approriate hints yet,
2277 * destroy the icon */
2278 #ifdef REDUCE_APPICONS
2279 if ((icon->num_apps == 0) && (!icon->running || !wApplicationOf(icon->main_window)) )
2280 #else
2281 if (!icon->running || !wApplicationOf(icon->main_window))
2282 #endif
2283 wAppIconDestroy(icon);
2284 else {
2285 icon->icon->core->descriptor.handle_mousedown = appIconMouseDown;
2286 icon->icon->core->descriptor.handle_enternotify = NULL;
2287 icon->icon->core->descriptor.handle_leavenotify = NULL;
2288 icon->icon->core->descriptor.parent_type = WCLASS_APPICON;
2289 icon->icon->core->descriptor.parent = icon;
2291 ChangeStackingLevel(icon->icon->core, NORMAL_ICON_LEVEL);
2293 wAppIconPaint(icon);
2294 if (wPreferences.auto_arrange_icons) {
2295 wArrangeIcons(dock->screen_ptr, True);
2298 if (dock->auto_collapse || dock->auto_raise_lower)
2299 clipLeave(dock);
2304 * returns the closest Dock slot index for the passed
2305 * coordinates.
2307 * Returns False if icon can't be docked.
2309 Bool
2310 wDockSnapIcon(WDock *dock, WAppIcon *icon, int req_x, int req_y,
2311 int *ret_x, int *ret_y, int redocking)
2313 WScreen *scr = dock->screen_ptr;
2314 int dx, dy;
2315 int ex_x, ex_y;
2316 int i, offset = ICON_SIZE/2;
2317 WAppIcon *aicon = NULL;
2318 WAppIcon *nicon = NULL;
2319 int max_y_icons, max_x_icons;
2321 max_x_icons = scr->scr_width/ICON_SIZE;
2322 max_y_icons = scr->scr_height/ICON_SIZE-1;
2324 if (wPreferences.flags.noupdates)
2325 return False;
2327 dx = dock->x_pos;
2328 dy = dock->y_pos;
2330 /* if the dock is full */
2331 if (!redocking &&
2332 (dock->icon_count >= dock->max_icons)) {
2333 return False;
2336 /* exact position */
2337 if (req_y < dy)
2338 ex_y = (req_y - offset - dy)/ICON_SIZE;
2339 else
2340 ex_y = (req_y + offset - dy)/ICON_SIZE;
2342 if (req_x < dx)
2343 ex_x = (req_x - offset - dx)/ICON_SIZE;
2344 else
2345 ex_x = (req_x + offset - dx)/ICON_SIZE;
2347 /* check if the icon is outside the screen boundaries */
2348 if (dx + ex_x*ICON_SIZE < -ICON_SIZE+2 ||
2349 dx + ex_x*ICON_SIZE >= scr->scr_width-1 ||
2350 dy + ex_y*ICON_SIZE < -ICON_SIZE+2 ||
2351 dy + ex_y*ICON_SIZE >= scr->scr_height-1)
2352 return False;
2354 if (dock->type == WM_DOCK) {
2355 if (icon->dock != dock && ex_x != 0)
2356 return False;
2358 for (i=0; i<dock->max_icons; i++) {
2359 nicon = dock->icon_array[i];
2360 if (nicon && nicon->yindex == ex_y) {
2361 aicon = nicon;
2362 break;
2366 *ret_x = 0;
2368 if (redocking) {
2369 int sig, done, closest;
2371 /* Possible cases when redocking:
2373 * icon dragged out of range of any slot -> false
2374 * icon dragged to range of free slot
2375 * icon dragged to range of same slot
2376 * icon dragged to range of different icon
2378 if (abs(ex_x) > DOCK_DETTACH_THRESHOLD)
2379 return False;
2381 if (ex_y >= 0 && ex_y <= max_y_icons && (aicon == icon || !aicon)) {
2383 *ret_y = ex_y;
2386 return True;
2389 /* start looking at the upper slot or lower? */
2390 if (ex_y*ICON_SIZE < (req_y + offset - dy))
2391 sig = 1;
2392 else
2393 sig = -1;
2395 closest = -1;
2396 done = 0;
2397 /* look for closest free slot */
2398 for (i=0; i<(DOCK_DETTACH_THRESHOLD+1)*2 && !done; i++) {
2399 int j;
2401 done = 1;
2402 closest = sig*(i/2) + ex_y;
2403 /* check if this slot is used */
2404 if (closest >= 0) {
2405 for (j = 0; j<dock->max_icons; j++) {
2406 if (dock->icon_array[j]
2407 && dock->icon_array[j]->yindex==closest) {
2408 /* slot is used by someone else */
2409 if (dock->icon_array[j]!=icon)
2410 done = 0;
2411 break;
2415 sig = -sig;
2417 if (done && closest >= 0 && closest <= max_y_icons &&
2418 ((ex_y >= closest && ex_y - closest < DOCK_DETTACH_THRESHOLD+1)
2420 (ex_y < closest && closest - ex_y <= DOCK_DETTACH_THRESHOLD+1))) {
2422 *ret_y = closest;
2424 return True;
2426 } else { /* !redocking */
2428 /* if slot is free and the icon is close enough, return it */
2429 if (!aicon && ex_x == 0 && ex_y >= 0 && ex_y <= max_y_icons) {
2430 *ret_y = ex_y;
2431 return True;
2434 } else { /* CLIP */
2435 int neighbours = 0;
2437 for (i=0; i<dock->max_icons; i++) {
2438 nicon = dock->icon_array[i];
2439 if (nicon && nicon->xindex == ex_x && nicon->yindex == ex_y) {
2440 aicon = nicon;
2441 break;
2445 for (i=0; i<dock->max_icons; i++) {
2446 nicon = dock->icon_array[i];
2447 if (nicon && nicon != icon && /* Icon can't be it's own neighbour */
2448 (abs(nicon->xindex - ex_x) <= CLIP_ATTACH_VICINITY &&
2449 abs(nicon->yindex - ex_y) <= CLIP_ATTACH_VICINITY)) {
2450 neighbours = 1;
2451 break;
2455 if (neighbours && (aicon==NULL || (redocking && aicon == icon))) {
2456 *ret_x = ex_x;
2457 *ret_y = ex_y;
2458 return True;
2461 return False;
2464 #define MIN(x, y) ((x) > (y) ? (y) : (x))
2465 #define MAX(x, y) ((x) < (y) ? (y) : (x))
2467 #define ON_SCREEN(x, y, sx, ex, sy, ey) \
2468 ((((x)+ICON_SIZE/2) >= (sx)) && (((y)+ICON_SIZE/2) >= (sy)) && \
2469 (((x) + (ICON_SIZE/2)) <= (ex)) && (((y) + (ICON_SIZE/2)) <= ey))
2473 * returns true if it can find a free slot in the dock,
2474 * in which case it changes x_pos and y_pos accordingly.
2475 * Else returns false.
2477 Bool
2478 wDockFindFreeSlot(WDock *dock, int *x_pos, int *y_pos)
2480 WScreen *scr = dock->screen_ptr;
2481 WAppIcon *btn;
2482 unsigned char *slot_map;
2483 int mwidth;
2484 int r;
2485 int x, y;
2486 int i, done = False;
2487 int corner;
2488 int sx=0, sy=0, ex=scr->scr_width, ey=scr->scr_height;
2491 /* if the dock is full */
2492 if (dock->icon_count >= dock->max_icons) {
2493 return False;
2496 if (!wPreferences.flags.nodock && scr->dock) {
2497 if (scr->dock->on_right_side)
2498 ex -= ICON_SIZE + DOCK_EXTRA_SPACE;
2499 else
2500 sx += ICON_SIZE + DOCK_EXTRA_SPACE;
2503 if (ex < dock->x_pos)
2504 ex = dock->x_pos;
2505 if (sx > dock->x_pos+ICON_SIZE)
2506 sx = dock->x_pos+ICON_SIZE;
2507 #define C_NONE 0
2508 #define C_NW 1
2509 #define C_NE 2
2510 #define C_SW 3
2511 #define C_SE 4
2513 /* check if clip is in a corner */
2514 if (dock->type==WM_CLIP) {
2515 if (dock->x_pos < 1 && dock->y_pos < 1)
2516 corner = C_NE;
2517 else if (dock->x_pos < 1 && dock->y_pos >= (ey-ICON_SIZE))
2518 corner = C_SE;
2519 else if (dock->x_pos >= (ex-ICON_SIZE)&& dock->y_pos >= (ey-ICON_SIZE))
2520 corner = C_SW;
2521 else if (dock->x_pos >= (ex-ICON_SIZE) && dock->y_pos < 1)
2522 corner = C_NW;
2523 else
2524 corner = C_NONE;
2525 } else
2526 corner = C_NONE;
2528 /* If the clip is in the corner, use only slots that are in the border
2529 * of the screen */
2530 if (corner!=C_NONE) {
2531 char *hmap, *vmap;
2532 int hcount, vcount;
2534 hcount = MIN(dock->max_icons, scr->scr_width/ICON_SIZE);
2535 vcount = MIN(dock->max_icons, scr->scr_height/ICON_SIZE);
2536 hmap = wmalloc(hcount+1);
2537 memset(hmap, 0, hcount+1);
2538 vmap = wmalloc(vcount+1);
2539 memset(vmap, 0, vcount+1);
2541 /* mark used positions */
2542 switch (corner) {
2543 case C_NE:
2544 for (i=0; i<dock->max_icons; i++) {
2545 btn = dock->icon_array[i];
2546 if (!btn)
2547 continue;
2549 if (btn->xindex==0 && btn->yindex > 0 && btn->yindex < vcount)
2550 vmap[btn->yindex] = 1;
2551 else if (btn->yindex==0 && btn->xindex>0 && btn->xindex<hcount)
2552 hmap[btn->xindex] = 1;
2554 case C_NW:
2555 for (i=0; i<dock->max_icons; i++) {
2556 btn = dock->icon_array[i];
2557 if (!btn)
2558 continue;
2560 if (btn->xindex==0 && btn->yindex > 0 && btn->yindex < vcount)
2561 vmap[btn->yindex] = 1;
2562 else if (btn->yindex==0 && btn->xindex<0 &&btn->xindex>-hcount)
2563 hmap[-btn->xindex] = 1;
2565 case C_SE:
2566 for (i=0; i<dock->max_icons; i++) {
2567 btn = dock->icon_array[i];
2568 if (!btn)
2569 continue;
2571 if (btn->xindex==0 && btn->yindex < 0 && btn->yindex > -vcount)
2572 vmap[-btn->yindex] = 1;
2573 else if (btn->yindex==0 && btn->xindex>0 && btn->xindex<hcount)
2574 hmap[btn->xindex] = 1;
2576 case C_SW:
2577 default:
2578 for (i=0; i<dock->max_icons; i++) {
2579 btn = dock->icon_array[i];
2580 if (!btn)
2581 continue;
2583 if (btn->xindex==0 && btn->yindex < 0 && btn->yindex > -vcount)
2584 vmap[-btn->yindex] = 1;
2585 else if (btn->yindex==0 && btn->xindex<0 &&btn->xindex>-hcount)
2586 hmap[-btn->xindex] = 1;
2589 x=0; y=0;
2590 done = 0;
2591 /* search a vacant slot */
2592 for (i=1; i<MAX(vcount, hcount); i++) {
2593 if (i < vcount && vmap[i]==0) {
2594 /* found a slot */
2595 x = 0;
2596 y = i;
2597 done = 1;
2598 break;
2599 } else if (i < hcount && hmap[i]==0) {
2600 /* found a slot */
2601 x = i;
2602 y = 0;
2603 done = 1;
2604 break;
2607 free(vmap);
2608 free(hmap);
2609 /* If found a slot, translate and return */
2610 if (done) {
2611 if (corner==C_NW || corner==C_NE) {
2612 *y_pos = y;
2613 } else {
2614 *y_pos = -y;
2616 if (corner==C_NE || corner==C_SE) {
2617 *x_pos = x;
2618 } else {
2619 *x_pos = -x;
2621 return True;
2623 /* else, try to find a slot somewhere else */
2626 /* a map of mwidth x mwidth would be enough if we allowed icons to be
2627 * placed outside of screen */
2628 mwidth = (int)ceil(sqrt(dock->max_icons));
2630 /* In the worst case (the clip is in the corner of the screen),
2631 * the amount of icons that fit in the clip is smaller.
2632 * Double the map to get a safe value.
2634 mwidth += mwidth;
2636 r = (mwidth-1)/2;
2638 slot_map = wmalloc(mwidth*mwidth);
2639 memset(slot_map, 0, mwidth*mwidth);
2641 #define XY2OFS(x,y) (MAX(abs(x),abs(y)) > r) ? 0 : (((y)+r)*(mwidth)+(x)+r)
2643 /* mark used slots in the map. If the slot falls outside the map
2644 * (for example, when all icons are placed in line), ignore them. */
2645 for (i=0; i<dock->max_icons; i++) {
2646 btn = dock->icon_array[i];
2647 if (btn)
2648 slot_map[XY2OFS(btn->xindex, btn->yindex)] = 1;
2650 /* Find closest slot from the center that is free by scanning the
2651 * map from the center to outward in circular passes.
2652 * This will not result in a neat layout, but will be optimal
2653 * in the sense that there will not be holes left.
2655 done = 0;
2656 for (i = 1; i <= r && !done; i++) {
2657 int tx, ty;
2659 /* top and bottom parts of the ring */
2660 for (x = -i; x <= i && !done; x++) {
2661 tx = dock->x_pos + x*ICON_SIZE;
2662 y = -i;
2663 ty = dock->y_pos + y*ICON_SIZE;
2664 if (slot_map[XY2OFS(x,y)]==0
2665 && ON_SCREEN(tx, ty, sx, ex, sy, ey)) {
2666 *x_pos = x;
2667 *y_pos = y;
2668 done = 1;
2669 break;
2671 y = i;
2672 ty = dock->y_pos + y*ICON_SIZE;
2673 if (slot_map[XY2OFS(x,y)]==0
2674 && ON_SCREEN(tx, ty, sx, ex, sy, ey)) {
2675 *x_pos = x;
2676 *y_pos = y;
2677 done = 1;
2678 break;
2681 /* left and right parts of the ring */
2682 for (y = -i+1; y <= i-1; y++) {
2683 ty = dock->y_pos + y*ICON_SIZE;
2684 x = -i;
2685 tx = dock->x_pos + x*ICON_SIZE;
2686 if (slot_map[XY2OFS(x,y)]==0
2687 && ON_SCREEN(tx, ty, sx, ex, sy, ey)) {
2688 *x_pos = x;
2689 *y_pos = y;
2690 done = 1;
2691 break;
2693 x = i;
2694 tx = dock->x_pos + x*ICON_SIZE;
2695 if (slot_map[XY2OFS(x,y)]==0
2696 && ON_SCREEN(tx, ty, sx, ex, sy, ey)) {
2697 *x_pos = x;
2698 *y_pos = y;
2699 done = 1;
2700 break;
2704 free(slot_map);
2705 #undef XY2OFS
2706 return done;
2710 static void
2711 moveDock(WDock *dock, int new_x, int new_y)
2713 WAppIcon *btn;
2714 int i;
2716 dock->x_pos = new_x;
2717 dock->y_pos = new_y;
2718 for (i=0; i<dock->max_icons; i++) {
2719 btn = dock->icon_array[i];
2720 if (btn) {
2721 btn->x_pos = new_x + btn->xindex*ICON_SIZE;
2722 btn->y_pos = new_y + btn->yindex*ICON_SIZE;
2723 XMoveWindow(dpy, btn->icon->core->window, btn->x_pos, btn->y_pos);
2729 static void
2730 swapDock(WDock *dock)
2732 WScreen *scr = dock->screen_ptr;
2733 WAppIcon *btn;
2734 int x, i;
2737 if (dock->on_right_side) {
2738 x = dock->x_pos = scr->scr_width - ICON_SIZE - DOCK_EXTRA_SPACE;
2739 } else {
2740 x = dock->x_pos = DOCK_EXTRA_SPACE;
2743 for (i=0; i<dock->max_icons; i++) {
2744 btn = dock->icon_array[i];
2745 if (btn) {
2746 btn->x_pos = x;
2747 XMoveWindow(dpy, btn->icon->core->window, btn->x_pos, btn->y_pos);
2751 wScreenUpdateUsableArea(scr);
2755 static pid_t
2756 execCommand(WAppIcon *btn, char *command, WSavedState *state)
2758 WScreen *scr = btn->icon->core->screen_ptr;
2759 pid_t pid;
2760 char **argv;
2761 int argc;
2762 char *cmdline;
2764 cmdline = ExpandOptions(scr, command);
2766 if (scr->flags.dnd_data_convertion_status || !cmdline) {
2767 if (cmdline)
2768 free(cmdline);
2769 if (state)
2770 free(state);
2771 return 0;
2774 ParseCommand(cmdline, &argv, &argc);
2776 if (argv==NULL) {
2777 if (cmdline)
2778 free(cmdline);
2779 if (state)
2780 free(state);
2781 return 0;
2784 if ((pid=fork())==0) {
2785 char **args;
2786 int i;
2788 SetupEnvironment(scr);
2790 #ifdef HAVE_SETPGID
2791 setpgid(0, 0);
2792 #endif
2794 args = malloc(sizeof(char*)*(argc+1));
2795 if (!args)
2796 exit(111);
2797 for (i=0; i<argc; i++) {
2798 args[i] = argv[i];
2800 args[argc] = NULL;
2801 execvp(argv[0], args);
2802 exit(111);
2804 while (argc > 0)
2805 free(argv[--argc]);
2806 free(argv);
2808 if (pid > 0) {
2809 if (!state) {
2810 state = wmalloc(sizeof(WSavedState));
2811 memset(state, 0, sizeof(WSavedState));
2812 state->hidden = -1;
2813 state->miniaturized = -1;
2814 state->shaded = -1;
2815 if (btn->dock == scr->dock)
2816 state->workspace = -1;
2817 else
2818 state->workspace = scr->current_workspace;
2820 wWindowAddSavedState(btn->wm_instance, btn->wm_class, cmdline, pid,
2821 state);
2822 wAddDeathHandler(pid, (WDeathHandler*)trackDeadProcess,
2823 btn->dock);
2824 } else if (state) {
2825 free(state);
2827 free(cmdline);
2828 return pid;
2832 void
2833 wDockHideIcons(WDock *dock)
2835 int i;
2836 WAppIcon *btn;
2838 if (dock==NULL)
2839 return;
2841 btn = dock->icon_array[0];
2843 for (i=1; i<dock->max_icons; i++) {
2844 if (dock->icon_array[i])
2845 XUnmapWindow(dpy, dock->icon_array[i]->icon->core->window);
2847 dock->mapped = 0;
2849 dockIconPaint(btn);
2853 void
2854 wDockShowIcons(WDock *dock)
2856 int i, newlevel;
2857 WAppIcon *btn;
2859 if (dock==NULL)
2860 return;
2862 btn = dock->icon_array[0];
2863 moveDock(dock, btn->x_pos, btn->y_pos);
2865 newlevel = dock->lowered ? WMNormalLevel : WMDockLevel;
2866 ChangeStackingLevel(btn->icon->core, newlevel);
2868 for (i=1; i<dock->max_icons; i++) {
2869 if (dock->icon_array[i]) {
2870 MoveInStackListAbove(dock->icon_array[i]->icon->core,
2871 btn->icon->core);
2872 break;
2876 if (!dock->collapsed) {
2877 for (i=1; i<dock->max_icons; i++) {
2878 if (dock->icon_array[i]) {
2879 XMapWindow(dpy, dock->icon_array[i]->icon->core->window);
2883 dock->mapped = 1;
2885 dockIconPaint(btn);
2889 void
2890 wDockLower(WDock *dock)
2892 int i;
2894 for (i=0; i<dock->max_icons; i++) {
2895 if (dock->icon_array[i])
2896 wLowerFrame(dock->icon_array[i]->icon->core);
2901 void
2902 wDockRaise(WDock *dock)
2904 int i;
2906 for (i=dock->max_icons-1; i>=0; i--) {
2907 if (dock->icon_array[i])
2908 wRaiseFrame(dock->icon_array[i]->icon->core);
2913 void
2914 wDockRaiseLower(WDock *dock)
2916 if (!dock->icon_array[0]->icon->core->stacking->above
2917 ||(dock->icon_array[0]->icon->core->stacking->window_level
2918 !=dock->icon_array[0]->icon->core->stacking->above->stacking->window_level))
2919 wDockLower(dock);
2920 else
2921 wDockRaise(dock);
2925 void
2926 wDockFinishLaunch(WDock *dock, WAppIcon *icon)
2928 icon->launching = 0;
2929 icon->relaunching = 0;
2930 dockIconPaint(icon);
2934 WAppIcon*
2935 wDockFindIconFor(WDock *dock, Window window)
2937 WAppIcon *icon;
2938 int i;
2940 for (i=0; i<dock->max_icons; i++) {
2941 icon = dock->icon_array[i];
2942 if (icon && icon->main_window == window)
2943 return icon;
2945 return NULL;
2949 void
2950 wDockTrackWindowLaunch(WDock *dock, Window window)
2952 WAppIcon *icon;
2953 #ifdef REDUCE_APPICONS
2954 WAppIconAppList *tapplist;
2955 #endif
2956 char *wm_class, *wm_instance;
2957 int i;
2958 Bool firstPass = True;
2959 Bool found = False;
2960 char *command = NULL;
2963 int argc;
2964 char **argv;
2966 if (XGetCommand(dpy, window, &argv, &argc)) {
2967 if (argc > 0 && argv != NULL)
2968 command = FlattenStringList(argv,argc);
2969 if (argv) {
2970 XFreeStringList(argv);
2975 if (!PropGetWMClass(window, &wm_class, &wm_instance) ||
2976 (!wm_class && !wm_instance))
2977 return;
2979 retry:
2980 for (i=0; i<dock->max_icons; i++) {
2981 icon = dock->icon_array[i];
2982 if (!icon)
2983 continue;
2985 /* app is already attached to icon */
2986 if (icon->main_window == window) {
2987 found = True;
2988 break;
2991 if ((icon->wm_instance || icon->wm_class)
2992 && (icon->launching
2993 || (dock->screen_ptr->flags.startup && !icon->running))) {
2995 if (icon->wm_instance && wm_instance &&
2996 strcmp(icon->wm_instance, wm_instance)!=0) {
2997 continue;
2999 if (icon->wm_class && wm_class &&
3000 strcmp(icon->wm_class, wm_class)!=0) {
3001 continue;
3003 if (firstPass && command && strcmp(icon->command, command)!=0) {
3004 continue;
3007 if (!icon->relaunching) {
3008 WApplication *wapp;
3010 /* Possibly an application that was docked with dockit,
3011 * but the user did not update WMState to indicate that
3012 * it was docked by force */
3013 wapp = wApplicationOf(window);
3014 if (!wapp) {
3015 icon->forced_dock = 1;
3016 icon->running = 0;
3018 if (!icon->forced_dock)
3019 icon->main_window = window;
3021 #ifdef REDUCE_APPICONS
3022 tapplist = wmalloc(sizeof(WAppIconAppList));
3023 memset(tapplist, 0, sizeof(WAppIconAppList));
3024 tapplist->next = icon->applist;
3025 if (icon->applist)
3026 icon->applist->prev = tapplist;
3027 icon->applist = tapplist;
3028 tapplist->wapp = wApplicationOf(window);
3029 icon->num_apps++;
3030 #endif
3032 found = True;
3033 wDockFinishLaunch(dock, icon);
3034 break;
3038 if (firstPass && !found) {
3039 firstPass = False;
3040 goto retry;
3043 if (command)
3044 free(command);
3046 if (wm_class)
3047 XFree(wm_class);
3048 if (wm_instance)
3049 XFree(wm_instance);
3054 void
3055 wClipUpdateForWorkspaceChange(WScreen *scr, int workspace)
3057 if (!wPreferences.flags.noclip) {
3058 scr->clip_icon->dock = scr->workspaces[workspace]->clip;
3059 if (scr->current_workspace != workspace) {
3060 WDock *old_clip = scr->workspaces[scr->current_workspace]->clip;
3062 wDockHideIcons(old_clip);
3063 if (old_clip->auto_raise_lower) {
3064 if (old_clip->auto_raise_magic) {
3065 WMDeleteTimerHandler(old_clip->auto_raise_magic);
3066 old_clip->auto_raise_magic = NULL;
3068 wDockLower(old_clip);
3070 if (old_clip->auto_collapse) {
3071 if (old_clip->auto_expand_magic) {
3072 WMDeleteTimerHandler(old_clip->auto_expand_magic);
3073 old_clip->auto_expand_magic = NULL;
3075 old_clip->collapsed = 1;
3077 wDockShowIcons(scr->workspaces[workspace]->clip);
3079 if (scr->flags.clip_balloon_mapped)
3080 showClipBalloon(scr->clip_icon->dock, workspace);
3086 static void
3087 trackDeadProcess(pid_t pid, unsigned char status, WDock *dock)
3089 WAppIcon *icon;
3090 int i;
3092 for (i=0; i<dock->max_icons; i++) {
3093 icon = dock->icon_array[i];
3094 if (!icon)
3095 continue;
3097 if (icon->launching && icon->pid == pid) {
3098 if (!icon->relaunching) {
3099 icon->running = 0;
3100 icon->main_window = None;
3102 wDockFinishLaunch(dock, icon);
3103 icon->pid = 0;
3104 if (status==111) {
3105 char msg[PATH_MAX];
3106 #ifdef OFFIX_DND
3107 sprintf(msg, _("Could not execute command \"%s\""),
3108 icon->drop_launch && icon->dnd_command
3109 ? icon->dnd_command : icon->command);
3110 #else
3111 sprintf(msg, _("Could not execute command \"%s\""),
3112 icon->command);
3113 #endif
3114 wMessageDialog(dock->screen_ptr, _("Error"), msg,
3115 _("OK"), NULL, NULL);
3117 break;
3123 static void
3124 toggleLowered(WDock *dock)
3126 WAppIcon *tmp;
3127 int newlevel, i;
3129 /* lower/raise Dock */
3130 if (!dock->lowered) {
3131 newlevel = WMNormalLevel;
3132 dock->lowered = 1;
3133 } else {
3134 newlevel = WMDockLevel;
3135 dock->lowered = 0;
3138 for (i=0; i<dock->max_icons; i++) {
3139 tmp = dock->icon_array[i];
3140 if (!tmp)
3141 continue;
3143 ChangeStackingLevel(tmp->icon->core, newlevel);
3144 if (dock->lowered)
3145 wLowerFrame(tmp->icon->core);
3148 if (dock->type == WM_DOCK)
3149 wScreenUpdateUsableArea(dock->screen_ptr);
3153 static void
3154 toggleCollapsed(WDock *dock)
3156 if (dock->collapsed) {
3157 dock->collapsed = 0;
3158 wDockShowIcons(dock);
3160 else {
3161 dock->collapsed = 1;
3162 wDockHideIcons(dock);
3167 static void
3168 openDockMenu(WDock *dock, WAppIcon *aicon, XEvent *event)
3170 WScreen *scr = dock->screen_ptr;
3171 WObjDescriptor *desc;
3172 WMenuEntry *entry;
3173 WApplication *wapp = NULL;
3174 int index = 0;
3175 int x_pos;
3176 int appIsRunning = aicon->running && aicon->icon && aicon->icon->owner;
3178 if (dock->type == WM_DOCK) {
3179 /* keep on top */
3180 entry = dock->menu->entries[index];
3181 entry->flags.indicator_on = !dock->lowered;
3182 entry->clientdata = dock;
3183 } else {
3184 /* clip options */
3185 if (scr->clip_options)
3186 updateClipOptionsMenu(scr->clip_options, dock);
3188 /* Rename Workspace */
3189 entry = dock->menu->entries[++index];
3190 entry->clientdata = dock;
3192 /* select icon */
3193 entry = dock->menu->entries[++index];
3194 entry->clientdata = aicon;
3195 wMenuSetEnabled(dock->menu, index, aicon!=scr->clip_icon);
3197 /* (un)select all icons */
3198 entry = dock->menu->entries[++index];
3199 entry->clientdata = aicon;
3200 wMenuSetEnabled(dock->menu, index, dock->icon_count > 1);
3202 /* keep icon(s) */
3203 entry = dock->menu->entries[++index];
3204 entry->clientdata = aicon;
3205 wMenuSetEnabled(dock->menu, index, dock->icon_count > 1);
3207 /* this is the workspace submenu part */
3208 if (scr->clip_submenu)
3209 updateWorkspaceMenu(scr->clip_submenu, aicon);
3210 index++;
3212 /* remove icon(s) */
3213 entry = dock->menu->entries[++index];
3214 entry->clientdata = aicon;
3215 wMenuSetEnabled(dock->menu, index, dock->icon_count > 1);
3217 /* attract icon(s) */
3218 entry = dock->menu->entries[++index];
3219 entry->clientdata = aicon;
3222 /* launch */
3223 entry = dock->menu->entries[++index];
3224 entry->clientdata = aicon;
3225 wMenuSetEnabled(dock->menu, index, aicon->command!=NULL);
3227 /* unhide here */
3228 entry = dock->menu->entries[++index];
3229 entry->clientdata = aicon;
3230 wMenuSetEnabled(dock->menu, index, appIsRunning);
3232 /* hide */
3233 entry = dock->menu->entries[++index];
3234 entry->clientdata = aicon;
3235 if (aicon->icon->owner) {
3236 wapp = wApplicationOf(aicon->icon->owner->main_window);
3237 if (wapp && wapp->flags.hidden)
3238 entry->text = _("Unhide");
3239 else
3240 entry->text = _("Hide");
3241 } else {
3242 entry->text = _("Hide");
3244 wMenuSetEnabled(dock->menu, index, appIsRunning);
3246 /* settings */
3247 entry = dock->menu->entries[++index];
3248 entry->clientdata = aicon;
3249 wMenuSetEnabled(dock->menu, index, !aicon->editing
3250 && !wPreferences.flags.noupdates);
3252 /* kill */
3253 entry = dock->menu->entries[++index];
3254 entry->clientdata = aicon;
3255 wMenuSetEnabled(dock->menu, index, appIsRunning);
3257 if (!dock->menu->flags.realized)
3258 wMenuRealize(dock->menu);
3260 if (dock->type == WM_CLIP) {
3261 x_pos = event->xbutton.x_root+2;
3262 } else {
3263 x_pos = dock->on_right_side ?
3264 scr->scr_width - dock->menu->frame->core->width - 2 : 0;
3267 wMenuMapAt(dock->menu, x_pos, event->xbutton.y_root+2, False);
3269 /* allow drag select */
3270 event->xany.send_event = True;
3271 desc = &dock->menu->menu->descriptor;
3272 (*desc->handle_mousedown)(desc, event);
3276 static void
3277 openClipWorkspaceMenu(WScreen *scr, int x, int y)
3279 if (!scr->clip_ws_menu) {
3280 scr->clip_ws_menu = wWorkspaceMenuMake(scr, False);
3282 wWorkspaceMenuUpdate(scr, scr->clip_ws_menu);
3283 wMenuMapAt(scr->clip_ws_menu, x, y, False);
3287 /******************************************************************/
3288 static void
3289 iconDblClick(WObjDescriptor *desc, XEvent *event)
3291 WAppIcon *btn = desc->parent;
3292 WDock *dock = btn->dock;
3293 WApplication *wapp = NULL;
3294 int unhideHere = 0;
3296 #ifdef REDUCE_APPICONS
3297 if ((btn->icon->owner && !(event->xbutton.state & ControlMask)) ||
3298 ((btn->icon->owner == NULL) && (btn->applist != NULL))) {
3299 if (btn->icon->owner == NULL)
3300 btn->icon->owner = btn->applist->wapp->main_window_desc;
3301 #ifdef I_HATE_THIS
3303 #endif
3304 #else
3305 if (btn->icon->owner && !(event->xbutton.state & ControlMask)) {
3306 #endif
3307 wapp = wApplicationOf(btn->icon->owner->main_window);
3309 assert(wapp!=NULL);
3311 unhideHere = (event->xbutton.state & ShiftMask);
3313 /* go to the last workspace that the user worked on the app */
3314 if (wapp->last_workspace != dock->screen_ptr->current_workspace
3315 && !unhideHere) {
3316 wWorkspaceChange(dock->screen_ptr, wapp->last_workspace);
3319 wUnhideApplication(wapp, event->xbutton.button==Button2,
3320 unhideHere);
3322 if (event->xbutton.state & MOD_MASK) {
3323 wHideOtherApplications(btn->icon->owner);
3325 } else {
3326 if (event->xbutton.button==Button1) {
3328 if (event->xbutton.state & MOD_MASK) {
3329 /* raise/lower dock */
3330 toggleLowered(dock);
3331 } else if (btn == dock->screen_ptr->clip_icon) {
3332 if (getClipButton(event->xbutton.x, event->xbutton.y)==CLIP_IDLE)
3333 toggleCollapsed(dock);
3334 else
3335 handleClipChangeWorkspace(dock->screen_ptr, event);
3336 } else if (btn->command) {
3337 if (!btn->launching &&
3338 (!btn->running || (event->xbutton.state & ControlMask))) {
3339 launchDockedApplication(btn);
3347 static void
3348 handleDockMove(WDock *dock, WAppIcon *aicon, XEvent *event)
3350 WScreen *scr = dock->screen_ptr;
3351 int ofs_x=event->xbutton.x, ofs_y=event->xbutton.y;
3352 int x, y;
3353 XEvent ev;
3354 int grabbed = 0, swapped = 0, done;
3355 Pixmap ghost = None;
3356 int superfluous = wPreferences.superfluous; /* we catch it to avoid problems */
3358 #ifdef DEBUG
3359 puts("moving dock");
3360 #endif
3361 if (XGrabPointer(dpy, aicon->icon->core->window, True, ButtonMotionMask
3362 |ButtonReleaseMask|ButtonPressMask, GrabModeAsync,
3363 GrabModeAsync, None, None, CurrentTime) !=GrabSuccess) {
3364 wwarning("pointer grab failed for dock move");
3366 y = 0;
3367 for (x=0; x<dock->max_icons; x++) {
3368 if (dock->icon_array[x]!=NULL &&
3369 dock->icon_array[x]->yindex > y)
3370 y = dock->icon_array[x]->yindex;
3372 y++;
3373 XResizeWindow(dpy, scr->dock_shadow, ICON_SIZE, ICON_SIZE*y);
3375 done = 0;
3376 while (!done) {
3377 WMMaskEvent(dpy, PointerMotionMask|ButtonReleaseMask|ButtonPressMask
3378 |ButtonMotionMask|ExposureMask, &ev);
3379 switch (ev.type) {
3380 case Expose:
3381 WMHandleEvent(&ev);
3382 break;
3384 case MotionNotify:
3385 if (!grabbed) {
3386 if (abs(ofs_x-ev.xmotion.x)>=MOVE_THRESHOLD
3387 || abs(ofs_y-ev.xmotion.y)>=MOVE_THRESHOLD) {
3388 XChangeActivePointerGrab(dpy, ButtonMotionMask
3389 |ButtonReleaseMask|ButtonPressMask,
3390 wCursor[WCUR_MOVE], CurrentTime);
3391 grabbed=1;
3393 break;
3395 if (dock->type == WM_CLIP) {
3396 if (ev.xmotion.x_root - ofs_x < 0) {
3397 x = 0;
3398 } else if (ev.xmotion.x_root - ofs_x + ICON_SIZE >
3399 scr->scr_width) {
3400 x = scr->scr_width - ICON_SIZE;
3401 } else {
3402 x = ev.xmotion.x_root - ofs_x;
3404 if (ev.xmotion.y_root - ofs_y < 0) {
3405 y = 0;
3406 } else if (ev.xmotion.y_root - ofs_y + ICON_SIZE >
3407 scr->scr_height) {
3408 y = scr->scr_height - ICON_SIZE;
3409 } else {
3410 y = ev.xmotion.y_root - ofs_y;
3412 moveDock(dock, x, y);
3413 } else {
3414 /* move vertically if pointer is inside the dock*/
3415 if ((dock->on_right_side &&
3416 ev.xmotion.x_root >= dock->x_pos - ICON_SIZE)
3417 || (!dock->on_right_side &&
3418 ev.xmotion.x_root <= dock->x_pos + ICON_SIZE*2)) {
3420 if (ev.xmotion.y_root - ofs_y < 0) {
3421 y = 0;
3422 } else if (ev.xmotion.y_root - ofs_y + ICON_SIZE >
3423 scr->scr_height) {
3424 y = scr->scr_height - ICON_SIZE;
3425 } else {
3426 y = ev.xmotion.y_root - ofs_y;
3428 moveDock(dock, dock->x_pos, y);
3430 /* move horizontally to change sides */
3431 x = ev.xmotion.x_root - ofs_x;
3432 if (!dock->on_right_side) {
3434 /* is on left */
3436 if (ev.xmotion.x_root > dock->x_pos + ICON_SIZE*2) {
3437 XMoveWindow(dpy, scr->dock_shadow, scr->scr_width-ICON_SIZE
3438 -DOCK_EXTRA_SPACE-1, dock->y_pos);
3439 if (superfluous) {
3440 if (ghost==None) {
3441 ghost = MakeGhostDock(dock, dock->x_pos,
3442 scr->scr_width-ICON_SIZE
3443 -DOCK_EXTRA_SPACE-1,
3444 dock->y_pos);
3445 XSetWindowBackgroundPixmap(dpy, scr->dock_shadow,
3446 ghost);
3447 XClearWindow(dpy, scr->dock_shadow);
3450 XMapRaised(dpy, scr->dock_shadow);
3451 swapped = 1;
3452 } else {
3453 if (superfluous && ghost!=None) {
3454 XFreePixmap(dpy, ghost);
3455 ghost = None;
3457 XUnmapWindow(dpy, scr->dock_shadow);
3458 swapped = 0;
3460 } else {
3461 /* is on right */
3462 if (ev.xmotion.x_root < dock->x_pos - ICON_SIZE) {
3463 XMoveWindow(dpy, scr->dock_shadow,
3464 DOCK_EXTRA_SPACE, dock->y_pos);
3465 if (superfluous) {
3466 if (ghost==None) {
3467 ghost = MakeGhostDock(dock, dock->x_pos,
3468 DOCK_EXTRA_SPACE, dock->y_pos);
3469 XSetWindowBackgroundPixmap(dpy, scr->dock_shadow,
3470 ghost);
3471 XClearWindow(dpy, scr->dock_shadow);
3474 XMapRaised(dpy, scr->dock_shadow);
3475 swapped = -1;
3476 } else {
3477 XUnmapWindow(dpy, scr->dock_shadow);
3478 swapped = 0;
3479 if (superfluous && ghost!=None) {
3480 XFreePixmap(dpy, ghost);
3481 ghost = None;
3486 break;
3488 case ButtonPress:
3489 break;
3491 case ButtonRelease:
3492 if (ev.xbutton.button != event->xbutton.button)
3493 break;
3494 XUngrabPointer(dpy, CurrentTime);
3495 XUnmapWindow(dpy, scr->dock_shadow);
3496 XResizeWindow(dpy, scr->dock_shadow, ICON_SIZE, ICON_SIZE);
3497 if (dock->type == WM_DOCK) {
3498 if (swapped!=0) {
3499 if (swapped>0)
3500 dock->on_right_side = 1;
3501 else
3502 dock->on_right_side = 0;
3503 swapDock(dock);
3504 wArrangeIcons(scr, False);
3507 done = 1;
3508 break;
3511 if (superfluous) {
3512 if (ghost!=None)
3513 XFreePixmap(dpy, ghost);
3514 XSetWindowBackground(dpy, scr->dock_shadow, scr->white_pixel);
3516 #ifdef DEBUG
3517 puts("End dock move");
3518 #endif
3523 static void
3524 handleIconMove(WDock *dock, WAppIcon *aicon, XEvent *event)
3526 WScreen *scr = dock->screen_ptr;
3527 Window wins[2];
3528 WIcon *icon = aicon->icon;
3529 WDock *dock2 = NULL, *last_dock = dock, *clip = NULL;
3530 int ondock, grabbed = 0, change_dock = 0, collapsed = 0;
3531 XEvent ev;
3532 int x = aicon->x_pos, y = aicon->y_pos;
3533 int ofs_x = event->xbutton.x, ofs_y = event->xbutton.y;
3534 int shad_x = x, shad_y = y;
3535 int ix = aicon->xindex, iy = aicon->yindex;
3536 int tmp;
3537 Pixmap ghost = None;
3538 Bool docked;
3539 int superfluous = wPreferences.superfluous; /* we catch it to avoid problems */
3541 if (wPreferences.flags.noupdates)
3542 return;
3544 if (XGrabPointer(dpy, icon->core->window, True, ButtonMotionMask
3545 |ButtonReleaseMask|ButtonPressMask, GrabModeAsync,
3546 GrabModeAsync, None, None, CurrentTime) !=GrabSuccess) {
3547 #ifdef DEBUG0
3548 wwarning("pointer grab failed for icon move");
3549 #endif
3552 wRaiseFrame(icon->core);
3554 if (!wPreferences.flags.noclip)
3555 clip = scr->workspaces[scr->current_workspace]->clip;
3557 if (dock == scr->dock && !wPreferences.flags.noclip)
3558 dock2 = clip;
3559 else if (dock != scr->dock && !wPreferences.flags.nodock)
3560 dock2 = scr->dock;
3562 wins[0] = icon->core->window;
3563 wins[1] = scr->dock_shadow;
3564 XRestackWindows(dpy, wins, 2);
3565 XMoveResizeWindow(dpy, scr->dock_shadow, aicon->x_pos, aicon->y_pos,
3566 ICON_SIZE, ICON_SIZE);
3567 if (superfluous) {
3568 if (icon->pixmap!=None)
3569 ghost = MakeGhostIcon(scr, icon->pixmap);
3570 else
3571 ghost = MakeGhostIcon(scr, icon->core->window);
3573 XSetWindowBackgroundPixmap(dpy, scr->dock_shadow, ghost);
3574 XClearWindow(dpy, scr->dock_shadow);
3576 XMapWindow(dpy, scr->dock_shadow);
3578 ondock = 1;
3581 while(1) {
3582 XMaskEvent(dpy, PointerMotionMask|ButtonReleaseMask|ButtonPressMask
3583 |ButtonMotionMask|ExposureMask, &ev);
3584 switch (ev.type) {
3585 case Expose:
3586 WMHandleEvent(&ev);
3587 break;
3589 case MotionNotify:
3590 if (!grabbed) {
3591 if (abs(ofs_x-ev.xmotion.x)>=MOVE_THRESHOLD
3592 || abs(ofs_y-ev.xmotion.y)>=MOVE_THRESHOLD) {
3593 XChangeActivePointerGrab(dpy, ButtonMotionMask
3594 |ButtonReleaseMask|ButtonPressMask,
3595 wCursor[WCUR_MOVE], CurrentTime);
3596 grabbed=1;
3597 } else {
3598 break;
3602 x = ev.xmotion.x_root - ofs_x;
3603 y = ev.xmotion.y_root - ofs_y;
3604 tmp = wDockSnapIcon(dock, aicon, x, y, &ix, &iy, True);
3605 if (tmp && dock2) {
3606 change_dock = 0;
3607 if (last_dock != dock && collapsed) {
3608 last_dock->collapsed = 1;
3609 wDockHideIcons(last_dock);
3610 collapsed = 0;
3612 if (!collapsed && (collapsed = dock->collapsed)) {
3613 dock->collapsed = 0;
3614 wDockShowIcons(dock);
3616 if (dock->auto_raise_lower)
3617 wDockRaise(dock);
3618 last_dock = dock;
3620 else if (dock2) {
3621 tmp = wDockSnapIcon(dock2, aicon, x, y, &ix, &iy, False);
3622 if (tmp) {
3623 change_dock = 1;
3624 if (last_dock != dock2 && collapsed) {
3625 last_dock->collapsed = 1;
3626 wDockHideIcons(last_dock);
3627 collapsed = 0;
3629 if (!collapsed && (collapsed = dock2->collapsed)) {
3630 dock2->collapsed = 0;
3631 wDockShowIcons(dock2);
3633 if (dock2->auto_raise_lower)
3634 wDockRaise(dock2);
3635 last_dock = dock2;
3638 if (aicon->launching
3639 || (aicon->running && !(ev.xmotion.state & MOD_MASK))
3640 || (!aicon->running && tmp)) {
3641 shad_x = last_dock->x_pos + ix*wPreferences.icon_size;
3642 shad_y = last_dock->y_pos + iy*wPreferences.icon_size;
3644 XMoveWindow(dpy, scr->dock_shadow, shad_x, shad_y);
3646 if (!ondock) {
3647 XMapWindow(dpy, scr->dock_shadow);
3648 #if 0
3649 if (!collapsed && (collapsed = last_dock->collapsed)) {
3650 last_dock->collapsed = 0;
3651 wDockShowIcons(last_dock);
3653 #endif
3655 ondock = 1;
3656 } else {
3657 if (ondock) {
3658 XUnmapWindow(dpy, scr->dock_shadow);
3659 #if 0
3660 if (last_dock && collapsed &&
3661 aicon->running && (ev.xmotion.state & MOD_MASK)) {
3662 last_dock->collapsed = 1;
3663 wDockHideIcons(last_dock);
3664 collapsed = 0;
3666 #endif
3668 ondock = 0;
3670 XMoveWindow(dpy, icon->core->window, x, y);
3671 break;
3673 case ButtonPress:
3674 break;
3676 case ButtonRelease:
3677 if (ev.xbutton.button != event->xbutton.button)
3678 break;
3679 XUngrabPointer(dpy, CurrentTime);
3680 if (ondock) {
3681 SlideWindow(icon->core->window, x, y, shad_x, shad_y);
3682 XUnmapWindow(dpy, scr->dock_shadow);
3683 if (!change_dock) {
3684 reattachIcon(dock, aicon, ix, iy);
3685 if (clip && dock!=clip && clip->auto_raise_lower)
3686 wDockLower(clip);
3687 } else {
3688 docked = moveIconBetweenDocks(dock, dock2, aicon, ix, iy);
3689 if (!docked) {
3690 /* Slide it back if dock rejected it */
3691 SlideWindow(icon->core->window, x, y, aicon->x_pos,
3692 aicon->y_pos);
3693 reattachIcon(dock, aicon, aicon->xindex,aicon->yindex);
3695 if (last_dock->type==WM_CLIP && last_dock->auto_collapse) {
3696 collapsed = 0;
3699 } else {
3700 aicon->x_pos = x;
3701 aicon->y_pos = y;
3702 if (superfluous) {
3703 if (!aicon->running && !wPreferences.no_animations) {
3704 /* We need to deselect it, even if is deselected in
3705 * wDockDetach(), because else DoKaboom() will fail.
3707 if (aicon->icon->selected)
3708 wIconSelect(aicon->icon);
3709 DoKaboom(scr,aicon->icon->core->window, x, y);
3712 if (clip && clip->auto_raise_lower)
3713 wDockLower(clip);
3714 wDockDetach(dock, aicon);
3716 if (collapsed) {
3717 last_dock->collapsed = 1;
3718 wDockHideIcons(last_dock);
3719 collapsed = 0;
3721 if (superfluous) {
3722 if (ghost!=None)
3723 XFreePixmap(dpy, ghost);
3724 XSetWindowBackground(dpy, scr->dock_shadow, scr->white_pixel);
3726 #ifdef DEBUG
3727 puts("End icon move");
3728 #endif
3729 return;
3735 static int
3736 getClipButton(int px, int py)
3738 int pt = (CLIP_BUTTON_SIZE+2)*ICON_SIZE/64;
3740 if (px < 0 || py < 0 || px >= ICON_SIZE || py >= ICON_SIZE)
3741 return CLIP_IDLE;
3743 if (py <= pt-((int)ICON_SIZE-1-px))
3744 return CLIP_FORWARD;
3745 else if (px <= pt-((int)ICON_SIZE-1-py))
3746 return CLIP_REWIND;
3748 return CLIP_IDLE;
3752 static void
3753 handleClipChangeWorkspace(WScreen *scr, XEvent *event)
3755 XEvent ev;
3756 int done, direction, new_ws;
3757 int new_dir;
3758 WDock *clip = scr->clip_icon->dock;
3760 direction = getClipButton(event->xbutton.x, event->xbutton.y);
3762 clip->lclip_button_pushed = direction==CLIP_REWIND;
3763 clip->rclip_button_pushed = direction==CLIP_FORWARD;
3765 wClipIconPaint(scr->clip_icon);
3766 done = 0;
3767 while(!done) {
3768 WMMaskEvent(dpy, ExposureMask|ButtonMotionMask|ButtonReleaseMask
3769 |ButtonPressMask, &ev);
3770 switch (ev.type) {
3771 case Expose:
3772 WMHandleEvent(&ev);
3773 break;
3775 case MotionNotify:
3776 new_dir = getClipButton(ev.xmotion.x, ev.xmotion.y);
3777 if (new_dir != direction) {
3778 direction = new_dir;
3779 clip->lclip_button_pushed = direction==CLIP_REWIND;
3780 clip->rclip_button_pushed = direction==CLIP_FORWARD;
3781 wClipIconPaint(scr->clip_icon);
3783 break;
3785 case ButtonPress:
3786 break;
3788 case ButtonRelease:
3789 if (ev.xbutton.button == event->xbutton.button)
3790 done = 1;
3794 clip->lclip_button_pushed = 0;
3795 clip->rclip_button_pushed = 0;
3797 new_ws = wPreferences.ws_advance || (event->xbutton.state & ControlMask);
3799 if (direction == CLIP_FORWARD) {
3800 if (scr->current_workspace < scr->workspace_count-1)
3801 wWorkspaceChange(scr, scr->current_workspace+1);
3802 else if (new_ws && scr->current_workspace < MAX_WORKSPACES-1)
3803 wWorkspaceChange(scr, scr->current_workspace+1);
3804 else if (wPreferences.ws_cycle)
3805 wWorkspaceChange(scr, 0);
3807 else if (direction == CLIP_REWIND) {
3808 if (scr->current_workspace > 0)
3809 wWorkspaceChange(scr, scr->current_workspace-1);
3810 else if (scr->current_workspace==0 && wPreferences.ws_cycle)
3811 wWorkspaceChange(scr, scr->workspace_count-1);
3814 wClipIconPaint(scr->clip_icon);
3818 static void
3819 iconMouseDown(WObjDescriptor *desc, XEvent *event)
3821 WAppIcon *aicon = desc->parent;
3822 WDock *dock = aicon->dock;
3823 WScreen *scr = aicon->icon->core->screen_ptr;
3825 if (aicon->editing || WCHECK_STATE(WSTATE_MODAL))
3826 return;
3828 scr->last_dock = dock;
3830 if (dock->menu->flags.mapped)
3831 wMenuUnmap(dock->menu);
3833 if (IsDoubleClick(scr, event)) {
3834 /* double-click was not in the main clip icon */
3835 if (dock->type != WM_CLIP || aicon->xindex!=0 || aicon->yindex!=0
3836 || getClipButton(event->xbutton.x, event->xbutton.y)==CLIP_IDLE) {
3837 iconDblClick(desc, event);
3838 return;
3842 if (dock->type == WM_CLIP && scr->flags.clip_balloon_mapped) {
3843 XUnmapWindow(dpy, scr->clip_balloon);
3844 scr->flags.clip_balloon_mapped = 0;
3847 #ifdef DEBUG
3848 puts("handling dock");
3849 #endif
3850 if (event->xbutton.button == Button1) {
3851 if (event->xbutton.state & MOD_MASK)
3852 wDockLower(dock);
3853 else
3854 wDockRaise(dock);
3856 if ((event->xbutton.state & ShiftMask) && aicon!=scr->clip_icon &&
3857 dock->type!=WM_DOCK) {
3858 wIconSelect(aicon->icon);
3859 return;
3862 if (aicon->yindex==0 && aicon->xindex==0) {
3863 if (getClipButton(event->xbutton.x, event->xbutton.y)!=CLIP_IDLE
3864 && dock->type==WM_CLIP)
3865 handleClipChangeWorkspace(scr, event);
3866 else
3867 handleDockMove(dock, aicon, event);
3868 } else
3869 handleIconMove(dock, aicon, event);
3871 } else if (event->xbutton.button==Button2 && dock->type==WM_CLIP &&
3872 aicon->xindex==0 && aicon->yindex==0) {
3873 openClipWorkspaceMenu(scr, event->xbutton.x_root+2,
3874 event->xbutton.y_root+2);
3875 if (scr->clip_ws_menu) {
3876 WMenu *menu;
3877 menu = scr->clip_ws_menu;
3878 desc = &menu->menu->descriptor;
3880 event->xany.send_event = True;
3881 (*desc->handle_mousedown)(desc, event);
3883 } else if (event->xbutton.button == Button3) {
3884 openDockMenu(dock, aicon, event);
3889 static void
3890 showClipBalloon(WDock *dock, int workspace)
3892 int w, h;
3893 int x, y;
3894 WScreen *scr = dock->screen_ptr;
3895 char *text;
3896 Window stack[2];
3898 scr->flags.clip_balloon_mapped = 1;
3899 XMapWindow(dpy, scr->clip_balloon);
3901 text = scr->workspaces[workspace]->name;
3903 w = wTextWidth(scr->clip_title_font->font, text, strlen(text));
3905 h = scr->clip_title_font->height;
3906 XResizeWindow(dpy, scr->clip_balloon, w, h);
3908 x = dock->x_pos + CLIP_BUTTON_SIZE*ICON_SIZE/64;
3909 y = dock->y_pos + ICON_SIZE-scr->clip_title_font->height - 3;
3911 if (x+w > scr->scr_width) {
3912 x = scr->scr_width - w;
3913 if (dock->y_pos + ICON_SIZE + h > scr->scr_height)
3914 y = dock->y_pos - h - 1;
3915 else
3916 y = dock->y_pos + ICON_SIZE;
3917 XRaiseWindow(dpy, scr->clip_balloon);
3918 } else {
3919 stack[0] = scr->clip_icon->icon->core->window;
3920 stack[1] = scr->clip_balloon;
3921 XRestackWindows(dpy, stack, 2);
3923 XMoveWindow(dpy, scr->clip_balloon, x, y);
3924 XSetForeground(dpy, scr->clip_title_gc,
3925 scr->clip_title_pixel[CLIP_NORMAL]);
3926 XClearWindow(dpy, scr->clip_balloon);
3927 wDrawString(scr->clip_balloon, scr->clip_title_font, scr->clip_title_gc,
3928 0, scr->clip_title_font->y, text, strlen(text));
3932 static void
3933 clipEnterNotify(WObjDescriptor *desc, XEvent *event)
3935 WAppIcon *btn = (WAppIcon*)desc->parent;
3936 WDock *dock;
3938 assert(event->type==EnterNotify);
3940 if(desc->parent_type!=WCLASS_DOCK_ICON)
3941 return;
3943 dock = btn->dock;
3944 if (!dock || dock->type!=WM_CLIP)
3945 return;
3947 /* The auto raise/lower code */
3948 if (dock->auto_lower_magic) {
3949 WMDeleteTimerHandler(dock->auto_lower_magic);
3950 dock->auto_lower_magic = NULL;
3952 if (dock->auto_raise_lower && !dock->auto_raise_magic) {
3953 dock->auto_raise_magic = WMAddTimerHandler(AUTO_RAISE_DELAY,
3954 clipAutoRaise,
3955 (void *)dock);
3958 /* The auto expand/collapse code */
3959 if (dock->auto_collapse_magic) {
3960 WMDeleteTimerHandler(dock->auto_collapse_magic);
3961 dock->auto_collapse_magic = NULL;
3963 if (dock->auto_collapse && !dock->auto_expand_magic) {
3964 dock->auto_expand_magic = WMAddTimerHandler(AUTO_EXPAND_DELAY,
3965 clipAutoExpand,
3966 (void *)dock);
3969 if (btn->xindex == 0 && btn->yindex == 0)
3970 showClipBalloon(dock, dock->screen_ptr->current_workspace);
3971 else {
3972 if (dock->screen_ptr->flags.clip_balloon_mapped) {
3973 XUnmapWindow(dpy, dock->screen_ptr->clip_balloon);
3974 dock->screen_ptr->flags.clip_balloon_mapped = 0;
3980 static void
3981 clipLeave(WDock *dock)
3983 XEvent event;
3984 WObjDescriptor *desc = NULL;
3986 if (!dock || dock->type!=WM_CLIP)
3987 return;
3989 if (XCheckTypedEvent(dpy, EnterNotify, &event)!=False) {
3990 if (XFindContext(dpy, event.xcrossing.window, wWinContext,
3991 (XPointer *)&desc)!=XCNOENT
3992 && desc && desc->parent_type==WCLASS_DOCK_ICON
3993 && ((WAppIcon*)desc->parent)->dock
3994 && ((WAppIcon*)desc->parent)->dock->type==WM_CLIP) {
3995 /* We didn't left the Clip yet */
3996 XPutBackEvent(dpy, &event);
3997 return;
4000 XPutBackEvent(dpy, &event);
4001 } else {
4002 /* We entered a withdrawn window, so we're still in Clip */
4003 return;
4006 if (dock->auto_raise_magic) {
4007 WMDeleteTimerHandler(dock->auto_raise_magic);
4008 dock->auto_raise_magic = NULL;
4010 if (dock->auto_raise_lower && !dock->auto_lower_magic) {
4011 dock->auto_lower_magic = WMAddTimerHandler(AUTO_LOWER_DELAY,
4012 clipAutoLower,
4013 (void *)dock);
4016 if (dock->auto_expand_magic) {
4017 WMDeleteTimerHandler(dock->auto_expand_magic);
4018 dock->auto_expand_magic = NULL;
4020 if (dock->auto_collapse && !dock->auto_collapse_magic) {
4021 dock->auto_collapse_magic = WMAddTimerHandler(AUTO_COLLAPSE_DELAY,
4022 clipAutoCollapse,
4023 (void *)dock);
4028 static void
4029 clipLeaveNotify(WObjDescriptor *desc, XEvent *event)
4031 WAppIcon *btn = (WAppIcon*)desc->parent;
4033 assert(event->type==LeaveNotify);
4035 if(desc->parent_type!=WCLASS_DOCK_ICON)
4036 return;
4038 clipLeave(btn->dock);
4042 static void
4043 clipAutoCollapse(void *cdata)
4045 WDock *dock = (WDock *)cdata;
4047 if (dock->type!=WM_CLIP)
4048 return;
4050 if (dock->auto_collapse) {
4051 dock->collapsed = 1;
4052 wDockHideIcons(dock);
4054 dock->auto_collapse_magic = NULL;
4058 static void
4059 clipAutoExpand(void *cdata)
4061 WDock *dock = (WDock *)cdata;
4063 if (dock->type!=WM_CLIP)
4064 return;
4066 if (dock->auto_collapse) {
4067 dock->collapsed = 0;
4068 wDockShowIcons(dock);
4070 dock->auto_expand_magic = NULL;
4074 static void
4075 clipAutoLower(void *cdata)
4077 WDock *dock = (WDock *)cdata;
4079 if (dock->type!=WM_CLIP)
4080 return;
4082 if (dock->auto_raise_lower)
4083 wDockLower(dock);
4085 dock->auto_lower_magic = NULL;
4089 static void
4090 clipAutoRaise(void *cdata)
4092 WDock *dock = (WDock *)cdata;
4094 if (dock->type!=WM_CLIP)
4095 return;
4097 if (dock->auto_raise_lower)
4098 wDockRaise(dock);
4100 if (dock->screen_ptr->flags.clip_balloon_mapped) {
4101 showClipBalloon(dock, dock->screen_ptr->current_workspace);
4104 dock->auto_raise_magic = NULL;