bug fixes
[wmaker-crm.git] / src / dock.c
blob115a402f2623477f287f3bf0d7aaa11ce5f00cd3
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 /* top right arrow */
356 p[0].x = p[3].x = ICON_SIZE-5-as;
357 p[0].y = p[3].y = 5;
358 p[1].x = ICON_SIZE-6;
359 p[1].y = 5;
360 p[2].x = ICON_SIZE-6;
361 p[2].y = 4+as;
362 if (rpushed) {
363 XFillPolygon(dpy, win, scr->draw_gc, p, 3, Convex, CoordModeOrigin);
364 XDrawLines(dpy, win, scr->draw_gc, p, 4, CoordModeOrigin);
365 } else {
366 #ifdef GRADIENT_CLIP_ARROW
367 if (!collapsed)
368 XSetTSOrigin(dpy, gc, ICON_SIZE-6-as, 5);
369 #endif
370 XFillPolygon(dpy, win, gc, p,3,Convex,CoordModeOrigin);
371 XDrawLines(dpy, win, gc, p,4,CoordModeOrigin);
374 /* bottom left arrow */
375 p[0].x = p[3].x = 5;
376 p[0].y = p[3].y = ICON_SIZE-5-as;
377 p[1].x = 5;
378 p[1].y = ICON_SIZE-6;
379 p[2].x = 4+as;
380 p[2].y = ICON_SIZE-6;
381 if (lpushed) {
382 XFillPolygon(dpy, win, scr->draw_gc, p, 3, Convex, CoordModeOrigin);
383 XDrawLines(dpy, win, scr->draw_gc, p, 4, CoordModeOrigin);
384 } else {
385 #ifdef GRADIENT_CLIP_ARROW
386 if (!collapsed)
387 XSetTSOrigin(dpy, gc, 5, ICON_SIZE-6-as);
388 #endif
389 XFillPolygon(dpy, win, gc, p,3,Convex,CoordModeOrigin);
390 XDrawLines(dpy, win, gc, p,4,CoordModeOrigin);
392 #ifdef GRADIENT_CLIP_ARROW
393 if (!collapsed)
394 XSetFillStyle(dpy, scr->copy_gc, FillSolid);
395 #endif
399 RImage*
400 wClipMakeTile(WScreen *scr, RImage *normalTile)
402 RImage *tile = RCloneImage(normalTile);
403 RColor black;
404 RColor dark;
405 RColor light;
406 int pt, tp;
407 int as;
409 pt = CLIP_BUTTON_SIZE*wPreferences.icon_size/64;
410 tp = wPreferences.icon_size-1 - pt;
411 as = pt - 15;
413 black.alpha = 255;
414 black.red = black.green = black.blue = 0;
416 dark.alpha = 0;
417 dark.red = dark.green = dark.blue = 60;
419 light.alpha = 0;
420 light.red = light.green = light.blue = 80;
423 /* top right */
424 ROperateLine(tile, RSubtractOperation, tp, 0, wPreferences.icon_size-2,
425 pt-1, &dark);
426 RDrawLine(tile, tp-1, 0, wPreferences.icon_size-1, pt+1, &black);
427 ROperateLine(tile, RAddOperation, tp, 2, wPreferences.icon_size-3,
428 pt, &light);
430 /* arrow bevel */
431 ROperateLine(tile, RSubtractOperation, ICON_SIZE - 7 - as, 4,
432 ICON_SIZE - 5, 4, &dark);
433 ROperateLine(tile, RSubtractOperation, ICON_SIZE - 6 - as, 5,
434 ICON_SIZE - 5, 6 + as, &dark);
435 ROperateLine(tile, RAddOperation, ICON_SIZE - 5, 4, ICON_SIZE - 5, 6 + as,
436 &light);
438 /* bottom left */
439 ROperateLine(tile, RAddOperation, 2, tp+2, pt-2,
440 wPreferences.icon_size-3, &dark);
441 RDrawLine(tile, 0, tp-1, pt+1, wPreferences.icon_size-1, &black);
442 ROperateLine(tile, RSubtractOperation, 0, tp-2, pt+1,
443 wPreferences.icon_size-2, &light);
445 /* arrow bevel */
446 ROperateLine(tile, RSubtractOperation, 4, ICON_SIZE - 7 - as, 4,
447 ICON_SIZE - 5, &dark);
448 ROperateLine(tile, RSubtractOperation, 5, ICON_SIZE - 6 - as,
449 6 + as, ICON_SIZE - 5, &dark);
450 ROperateLine(tile, RAddOperation, 4, ICON_SIZE - 5, 6 + as, ICON_SIZE - 5,
451 &light);
453 return tile;
457 static void
458 removeIconsCallback(WMenu *menu, WMenuEntry *entry)
460 WAppIcon *clickedIcon = (WAppIcon*)entry->clientdata;
461 WDock *dock;
462 WAppIcon *aicon;
463 LinkedList *selectedIcons;
464 int keepit;
466 assert(clickedIcon!=NULL);
468 dock = clickedIcon->dock;
470 selectedIcons = getSelected(dock);
472 if (selectedIcons) {
473 if (wMessageDialog(dock->screen_ptr, _("Workspace Clip"),
474 _("All selected icons will be removed!"),
475 _("OK"), _("Cancel"), NULL)!=WAPRDefault) {
476 return;
478 } else {
479 if (clickedIcon->xindex==0 && clickedIcon->yindex==0)
480 return;
481 selectedIcons = list_cons(clickedIcon, NULL);
484 while (selectedIcons) {
485 aicon = selectedIcons->head;
486 keepit = aicon->running && wApplicationOf(aicon->main_window);
487 wDockDetach(dock, aicon);
488 if (keepit) {
489 PlaceIcon(dock->screen_ptr, &aicon->x_pos, &aicon->y_pos);
490 XMoveWindow(dpy, aicon->icon->core->window,
491 aicon->x_pos, aicon->y_pos);
492 if (!dock->mapped || dock->collapsed)
493 XMapWindow(dpy, aicon->icon->core->window);
495 list_remove_head(&selectedIcons);
498 if (wPreferences.auto_arrange_icons)
499 wArrangeIcons(dock->screen_ptr, True);
503 static void
504 keepIconsCallback(WMenu *menu, WMenuEntry *entry)
506 WAppIcon *clickedIcon = (WAppIcon*)entry->clientdata;
507 WDock *dock;
508 WAppIcon *aicon;
509 LinkedList *selectedIcons;
511 assert(clickedIcon!=NULL);
512 dock = clickedIcon->dock;
514 selectedIcons = getSelected(dock);
516 if (!selectedIcons && clickedIcon!=dock->screen_ptr->clip_icon) {
517 char *command = NULL;
519 if (!clickedIcon->command && !clickedIcon->editing) {
520 clickedIcon->editing = 1;
521 if (wInputDialog(dock->screen_ptr, _("Keep Icon"),
522 _("Type the command used to launch the application"),
523 &command)) {
524 if (command && (command[0]==0 ||
525 (command[0]=='-' && command[1]==0))) {
526 free(command);
527 command = NULL;
529 clickedIcon->command = command;
530 clickedIcon->editing = 0;
531 } else {
532 clickedIcon->editing = 0;
533 if (command)
534 free(command);
535 return;
539 selectedIcons = list_cons(clickedIcon, NULL);
542 while (selectedIcons) {
543 aicon = selectedIcons->head;
544 if (aicon->icon->selected)
545 wIconSelect(aicon->icon);
546 if (aicon && aicon->attracted && aicon->command) {
547 aicon->attracted = 0;
548 if (aicon->icon->shadowed) {
549 aicon->icon->shadowed = 0;
550 aicon->icon->force_paint = 1;
551 wAppIconPaint(aicon);
554 list_remove_head(&selectedIcons);
561 static void
562 toggleAutoAttractCallback(WMenu *menu, WMenuEntry *entry)
564 WDock *dock = (WDock*)entry->clientdata;
566 assert(entry->clientdata!=NULL);
568 dock->attract_icons = !dock->attract_icons;
569 /*if (!dock->attract_icons)
570 dock->keep_attracted = 0;*/
572 entry->flags.indicator_on = dock->attract_icons;
574 wMenuPaint(menu);
578 static void
579 toggleKeepCallback(WMenu *menu, WMenuEntry *entry)
581 WDock *dock = (WDock*)entry->clientdata;
582 WAppIcon *btn;
583 int i;
585 assert(entry->clientdata!=NULL);
587 dock->keep_attracted = !dock->keep_attracted;
589 if (dock->keep_attracted) {
590 for (i=0; i< dock->max_icons; i++) {
591 btn = dock->icon_array[i];
592 if (btn && btn->attracted && btn->command) {
593 btn->attracted = 0;
594 if (btn->icon->shadowed) {
595 btn->icon->shadowed = 0;
596 btn->icon->force_paint = 1;
597 wAppIconPaint(btn);
603 entry->flags.indicator_on = dock->keep_attracted;
605 wMenuPaint(menu);
609 static void
610 selectCallback(WMenu *menu, WMenuEntry *entry)
612 WAppIcon *icon = (WAppIcon*)entry->clientdata;
614 assert(icon!=NULL);
616 wIconSelect(icon->icon);
618 wMenuPaint(menu);
622 static void
623 colectIconsCallback(WMenu *menu, WMenuEntry *entry)
625 WAppIcon *clickedIcon = (WAppIcon*)entry->clientdata;
626 WDock *clip;
627 WAppIcon *aicon;
628 int x, y, x_pos, y_pos;
630 assert(entry->clientdata!=NULL);
631 clip = clickedIcon->dock;
633 aicon = clip->screen_ptr->app_icon_list;
635 while (aicon) {
636 if (!aicon->docked && wDockFindFreeSlot(clip, &x, &y)) {
637 x_pos = clip->x_pos + x*ICON_SIZE;
638 y_pos = clip->y_pos + y*ICON_SIZE;
639 if (aicon->x_pos != x_pos || aicon->y_pos != y_pos) {
640 #ifdef ANIMATIONS
641 if (wPreferences.no_animations) {
642 XMoveWindow(dpy, aicon->icon->core->window, x_pos, y_pos);
643 } else {
644 SlideWindow(aicon->icon->core->window,
645 aicon->x_pos, aicon->y_pos, x_pos, y_pos);
647 #else
648 XMoveWindow(dpy, aicon->icon->core->window, x_pos, y_pos);
649 #endif /* ANIMATIONS */
651 aicon->attracted = 1;
652 if (!clip->keep_attracted && !aicon->icon->shadowed) {
653 aicon->icon->shadowed = 1;
654 aicon->icon->force_paint = 1;
655 /* We don't do an wAppIconPaint() here because it's in
656 * wDockAttachIcon(). -Dan
659 wDockAttachIcon(clip, aicon, x, y);
660 if (clip->collapsed || !clip->mapped)
661 XUnmapWindow(dpy, aicon->icon->core->window);
663 aicon = aicon->next;
668 static void
669 selectIconsCallback(WMenu *menu, WMenuEntry *entry)
671 WAppIcon *clickedIcon = (WAppIcon*)entry->clientdata;
672 WDock *dock;
673 LinkedList *selectedIcons;
674 WAppIcon *btn;
675 int i;
677 assert(clickedIcon!=NULL);
678 dock = clickedIcon->dock;
680 selectedIcons = getSelected(dock);
682 if (!selectedIcons) {
683 for (i=1; i<dock->max_icons; i++) {
684 btn = dock->icon_array[i];
685 if (btn && !btn->icon->selected) {
686 wIconSelect(btn->icon);
689 } else {
690 while(selectedIcons) {
691 btn = selectedIcons->head;
692 wIconSelect(btn->icon);
693 list_remove_head(&selectedIcons);
697 wMenuPaint(menu);
701 static void
702 toggleCollapsedCallback(WMenu *menu, WMenuEntry *entry)
704 assert(entry->clientdata!=NULL);
706 toggleCollapsed(entry->clientdata);
708 entry->flags.indicator_on = ((WDock*)entry->clientdata)->collapsed;
710 wMenuPaint(menu);
714 static void
715 toggleAutoCollapseCallback(WMenu *menu, WMenuEntry *entry)
717 WDock *dock;
718 assert(entry->clientdata!=NULL);
720 dock = (WDock*) entry->clientdata;
722 dock->auto_collapse = !dock->auto_collapse;
724 entry->flags.indicator_on = ((WDock*)entry->clientdata)->auto_collapse;
726 wMenuPaint(menu);
730 static void
731 toggleAutoRaiseLowerCallback(WMenu *menu, WMenuEntry *entry)
733 WDock *dock;
734 assert(entry->clientdata!=NULL);
736 dock = (WDock*) entry->clientdata;
738 dock->auto_raise_lower = !dock->auto_raise_lower;
740 entry->flags.indicator_on = ((WDock*)entry->clientdata)->auto_raise_lower;
742 wMenuPaint(menu);
746 static void
747 launchCallback(WMenu *menu, WMenuEntry *entry)
749 WAppIcon *btn = (WAppIcon*)entry->clientdata;
751 launchDockedApplication(btn);
755 static void
756 settingsCallback(WMenu *menu, WMenuEntry *entry)
758 WAppIcon *btn = (WAppIcon*)entry->clientdata;
760 if (btn->editing)
761 return;
762 ShowDockAppSettingsPanel(btn);
766 static void
767 hideCallback(WMenu *menu, WMenuEntry *entry)
769 WApplication *wapp;
770 WAppIcon *btn = (WAppIcon*)entry->clientdata;
772 wapp = wApplicationOf(btn->icon->owner->main_window);
774 if (wapp->flags.hidden) {
775 wWorkspaceChange(btn->icon->core->screen_ptr,wapp->last_workspace);
776 wUnhideApplication(wapp, False, False);
777 } else {
778 wHideApplication(wapp);
783 static void
784 unhideHereCallback(WMenu *menu, WMenuEntry *entry)
786 WApplication *wapp;
787 WAppIcon *btn = (WAppIcon*)entry->clientdata;
789 wapp = wApplicationOf(btn->icon->owner->main_window);
791 wUnhideApplication(wapp, False, True);
795 WAppIcon*
796 mainIconCreate(WScreen *scr, int type)
798 WAppIcon *btn;
799 int x_pos;
801 if (type == WM_CLIP) {
802 if (scr->clip_icon)
803 return scr->clip_icon;
804 btn = wAppIconCreateForDock(scr, NULL, "Logo", "WMClip", TILE_CLIP);
805 btn->icon->core->descriptor.handle_expose = clipIconExpose;
806 btn->icon->core->descriptor.handle_enternotify = clipEnterNotify;
807 btn->icon->core->descriptor.handle_leavenotify = clipLeaveNotify;
808 /*x_pos = scr->scr_width - ICON_SIZE*2 - DOCK_EXTRA_SPACE;*/
809 x_pos = 0;
810 } else {
811 btn = wAppIconCreateForDock(scr, NULL, "Logo", "WMDock", TILE_NORMAL);
812 x_pos = scr->scr_width - ICON_SIZE - DOCK_EXTRA_SPACE;
815 btn->xindex = 0;
816 btn->yindex = 0;
818 btn->icon->core->descriptor.handle_mousedown = iconMouseDown;
819 btn->icon->core->descriptor.parent_type = WCLASS_DOCK_ICON;
820 btn->icon->core->descriptor.parent = btn;
821 /*ChangeStackingLevel(btn->icon->core, WMDockLevel);*/
822 XMapWindow(dpy, btn->icon->core->window);
823 btn->x_pos = x_pos;
824 btn->y_pos = 0;
825 btn->docked = 1;
826 if (type == WM_CLIP)
827 scr->clip_icon = btn;
829 return btn;
833 static void
834 switchWSCommand(WMenu *menu, WMenuEntry *entry)
836 WAppIcon *btn, *icon = (WAppIcon*) entry->clientdata;
837 WScreen *scr = icon->icon->core->screen_ptr;
838 WDock *src, *dest;
839 LinkedList *selectedIcons;
840 int x, y;
842 if (entry->order == scr->current_workspace)
843 return;
844 src = icon->dock;
845 dest = scr->workspaces[entry->order]->clip;
847 selectedIcons = getSelected(src);
849 if (selectedIcons) {
850 while(selectedIcons) {
851 btn = selectedIcons->head;
852 if (wDockFindFreeSlot(dest, &x, &y)) {
853 moveIconBetweenDocks(src, dest, btn, x, y);
854 XUnmapWindow(dpy, btn->icon->core->window);
856 list_remove_head(&selectedIcons);
858 } else if (icon != scr->clip_icon) {
859 if (wDockFindFreeSlot(dest, &x, &y)) {
860 moveIconBetweenDocks(src, dest, icon, x, y);
861 XUnmapWindow(dpy, icon->icon->core->window);
868 static void
869 launchDockedApplication(WAppIcon *btn)
871 WScreen *scr = btn->icon->core->screen_ptr;
873 if (!btn->launching && btn->command!=NULL) {
874 if (!btn->forced_dock) {
875 btn->relaunching = btn->running;
876 btn->running = 1;
878 if (btn->wm_instance || btn->wm_class) {
879 WWindowAttributes attr;
880 memset(&attr, 0, sizeof(WWindowAttributes));
881 wDefaultFillAttributes(scr, btn->wm_instance, btn->wm_class,
882 &attr, NULL, True);
884 if (!attr.no_appicon && !btn->buggy_app)
885 btn->launching = 1;
886 else
887 btn->running = 0;
889 btn->drop_launch = 0;
890 scr->last_dock = btn->dock;
891 btn->pid = execCommand(btn, btn->command, NULL);
892 if (btn->pid>0) {
893 if (btn->buggy_app) {
894 /* give feedback that the app was launched */
895 btn->launching = 1;
896 dockIconPaint(btn);
897 btn->launching = 0;
898 WMAddTimerHandler(200, (WMCallback*)dockIconPaint, btn);
899 } else {
900 dockIconPaint(btn);
902 } else {
903 wwarning(_("could not launch application %s\n"), btn->command);
904 btn->launching = 0;
905 if (!btn->relaunching)
906 btn->running = 0;
913 static void
914 updateWorkspaceMenu(WMenu *menu, WAppIcon *icon)
916 WScreen *scr = menu->frame->screen_ptr;
917 char title[MAX_WORKSPACENAME_WIDTH+1];
918 int i;
920 if (!menu || !icon)
921 return;
923 for (i=0; i<scr->workspace_count; i++) {
924 if (i < menu->entry_no) {
925 if (strcmp(menu->entries[i]->text,scr->workspaces[i]->name)!=0) {
926 free(menu->entries[i]->text);
927 strcpy(title, scr->workspaces[i]->name);
928 menu->entries[i]->text = wstrdup(title);
929 menu->flags.realized = 0;
931 menu->entries[i]->clientdata = (void*)icon;
932 } else {
933 strcpy(title, scr->workspaces[i]->name);
935 wMenuAddCallback(menu, title, switchWSCommand, (void*)icon);
937 menu->flags.realized = 0;
939 if (i == scr->current_workspace) {
940 wMenuSetEnabled(menu, i, False);
941 } else {
942 wMenuSetEnabled(menu, i, True);
946 if (!menu->flags.realized)
947 wMenuRealize(menu);
951 static WMenu*
952 makeWorkspaceMenu(WScreen *scr)
954 WMenu *menu;
956 menu = wMenuCreate(scr, NULL, False);
957 if (!menu)
958 wwarning(_("could not create workspace submenu for Clip menu"));
960 wMenuAddCallback(menu, "", switchWSCommand, (void*)scr->clip_icon);
962 menu->flags.realized = 0;
963 wMenuRealize(menu);
965 return menu;
969 static void
970 updateClipOptionsMenu(WMenu *menu, WDock *dock)
972 WMenuEntry *entry;
973 int index = 0;
975 if (!menu || !dock)
976 return;
978 /* keep on top */
979 entry = menu->entries[index];
980 entry->flags.indicator_on = !dock->lowered;
981 entry->clientdata = dock;
983 /* collapsed */
984 entry = menu->entries[++index];
985 entry->flags.indicator_on = dock->collapsed;
986 entry->clientdata = dock;
988 /* auto-collapse */
989 entry = menu->entries[++index];
990 entry->flags.indicator_on = dock->auto_collapse;
991 entry->clientdata = dock;
993 /* auto-raise/lower */
994 entry = menu->entries[++index];
995 entry->flags.indicator_on = dock->auto_raise_lower;
996 entry->clientdata = dock;
998 /* attract icons */
999 entry = menu->entries[++index];
1000 entry->flags.indicator_on = dock->attract_icons;
1001 entry->clientdata = dock;
1003 /* keep attracted icons */
1004 entry = menu->entries[++index];
1005 entry->flags.indicator_on = dock->keep_attracted;
1006 entry->clientdata = dock;
1008 menu->flags.realized = 0;
1009 wMenuRealize(menu);
1013 static WMenu*
1014 makeClipOptionsMenu(WScreen *scr)
1016 WMenu *menu;
1017 WMenuEntry *entry;
1019 menu = wMenuCreate(scr, NULL, False);
1020 if (!menu) {
1021 wwarning(_("could not create options submenu for Clip menu"));
1022 return NULL;
1025 entry = wMenuAddCallback(menu, _("Keep on Top"),
1026 toggleLoweredCallback, NULL);
1027 entry->flags.indicator = 1;
1028 entry->flags.indicator_on = 1;
1029 entry->flags.indicator_type = MI_CHECK;
1031 entry = wMenuAddCallback(menu, _("Collapsed"),
1032 toggleCollapsedCallback, NULL);
1033 entry->flags.indicator = 1;
1034 entry->flags.indicator_on = 1;
1035 entry->flags.indicator_type = MI_CHECK;
1037 entry = wMenuAddCallback(menu, _("AutoCollapse"),
1038 toggleAutoCollapseCallback, NULL);
1039 entry->flags.indicator = 1;
1040 entry->flags.indicator_on = 1;
1041 entry->flags.indicator_type = MI_CHECK;
1043 entry = wMenuAddCallback(menu, _("AutoRaiseLower"),
1044 toggleAutoRaiseLowerCallback, NULL);
1045 entry->flags.indicator = 1;
1046 entry->flags.indicator_on = 1;
1047 entry->flags.indicator_type = MI_CHECK;
1049 entry = wMenuAddCallback(menu, _("AutoAttract Icons"),
1050 toggleAutoAttractCallback, NULL);
1051 entry->flags.indicator = 1;
1052 entry->flags.indicator_on = 1;
1053 entry->flags.indicator_type = MI_CHECK;
1055 entry = wMenuAddCallback(menu, _("Keep Attracted Icons"),
1056 toggleKeepCallback, NULL);
1057 entry->flags.indicator = 1;
1058 entry->flags.indicator_on = 1;
1059 entry->flags.indicator_type = MI_CHECK;
1061 menu->flags.realized = 0;
1062 wMenuRealize(menu);
1064 return menu;
1068 static WMenu*
1069 dockMenuCreate(WScreen *scr, int type)
1071 WMenu *menu;
1072 WMenuEntry *entry;
1074 if (type == WM_CLIP && scr->clip_menu)
1075 return scr->clip_menu;
1077 menu = wMenuCreate(scr, NULL, False);
1078 if (type != WM_CLIP) {
1079 entry = wMenuAddCallback(menu, _("Keep on top"),
1080 toggleLoweredCallback, NULL);
1081 entry->flags.indicator = 1;
1082 entry->flags.indicator_on = 1;
1083 entry->flags.indicator_type = MI_CHECK;
1084 } else {
1085 entry = wMenuAddCallback(menu, _("Clip Options"), NULL, NULL);
1086 scr->clip_options = makeClipOptionsMenu(scr);
1087 if (scr->clip_options)
1088 wMenuEntrySetCascade(menu, entry, scr->clip_options);
1090 wMenuAddCallback(menu, _("Rename Workspace"), renameCallback, NULL);
1092 wMenuAddCallback(menu, _("(Un)Select Icon"), selectCallback, NULL);
1094 wMenuAddCallback(menu, _("(Un)Select All Icons"), selectIconsCallback,
1095 NULL);
1097 wMenuAddCallback(menu, _("Keep Icon(s)"), keepIconsCallback, NULL);
1099 entry = wMenuAddCallback(menu, _("Move Icon(s) To"), NULL, NULL);
1100 scr->clip_submenu = makeWorkspaceMenu(scr);
1101 if (scr->clip_submenu)
1102 wMenuEntrySetCascade(menu, entry, scr->clip_submenu);
1104 wMenuAddCallback(menu, _("Remove Icon(s)"), removeIconsCallback, NULL);
1106 wMenuAddCallback(menu, _("Attract Icons"), colectIconsCallback, NULL);
1109 wMenuAddCallback(menu, _("Launch"), launchCallback, NULL);
1111 wMenuAddCallback(menu, _("Unhide Here"), unhideHereCallback, NULL);
1113 entry = wMenuAddCallback(menu, _("Hide"), hideCallback, NULL);
1114 free(entry->text);
1115 entry->text = _("Hide");
1117 wMenuAddCallback(menu, _("Settings..."), settingsCallback, NULL);
1119 wMenuAddCallback(menu, _("Kill"), killCallback, NULL);
1121 if (type == WM_CLIP)
1122 scr->clip_menu = menu;
1124 return menu;
1128 WDock*
1129 wDockCreate(WScreen *scr, int type)
1131 WDock *dock;
1132 WAppIcon *btn;
1133 int icon_count;
1135 make_keys();
1137 dock = wmalloc(sizeof(WDock));
1138 memset(dock, 0, sizeof(WDock));
1140 if (type == WM_CLIP)
1141 icon_count = CLIP_MAX_ICONS;
1142 else
1143 icon_count = scr->scr_height/wPreferences.icon_size;
1145 dock->icon_array = wmalloc(sizeof(WAppIcon*)*icon_count);
1146 memset(dock->icon_array, 0, sizeof(WAppIcon*)*icon_count);
1148 dock->max_icons = icon_count;
1150 btn = mainIconCreate(scr, type);
1152 btn->dock = dock;
1154 dock->x_pos = btn->x_pos;
1155 dock->y_pos = btn->y_pos;
1156 dock->screen_ptr = scr;
1157 dock->type = type;
1158 dock->icon_count = 1;
1159 dock->on_right_side = 1;
1160 dock->collapsed = 0;
1161 dock->auto_collapse = 0;
1162 dock->auto_collapse_magic = NULL;
1163 dock->auto_raise_lower = 0;
1164 dock->auto_lower_magic = NULL;
1165 dock->auto_raise_magic = NULL;
1166 dock->attract_icons = 0;
1167 dock->keep_attracted = 0;
1168 dock->lowered = 1;
1169 dock->icon_array[0] = btn;
1170 wRaiseFrame(btn->icon->core);
1171 XMoveWindow(dpy, btn->icon->core->window, btn->x_pos, btn->y_pos);
1173 /* create dock menu */
1174 dock->menu = dockMenuCreate(scr, type);
1176 return dock;
1180 void
1181 wDockDestroy(WDock *dock)
1183 int i;
1184 WAppIcon *aicon;
1186 for (i=(dock->type == WM_CLIP) ? 1 : 0; i<dock->max_icons; i++) {
1187 aicon = dock->icon_array[i];
1188 if (aicon) {
1189 int keepit = aicon->running && wApplicationOf(aicon->main_window);
1190 wDockDetach(dock, aicon);
1191 if (keepit) {
1192 PlaceIcon(dock->screen_ptr, &aicon->x_pos, &aicon->y_pos);
1193 XMoveWindow(dpy, aicon->icon->core->window,
1194 aicon->x_pos, aicon->y_pos);
1195 if (!dock->mapped || dock->collapsed)
1196 XMapWindow(dpy, aicon->icon->core->window);
1200 if (wPreferences.auto_arrange_icons)
1201 wArrangeIcons(dock->screen_ptr, True);
1202 free(dock->icon_array);
1203 if (dock->menu && dock->type!=WM_CLIP)
1204 wMenuDestroy(dock->menu, True);
1205 free(dock);
1209 void
1210 wClipIconPaint(WAppIcon *aicon)
1212 WScreen *scr = aicon->icon->core->screen_ptr;
1213 WWorkspace *workspace = scr->workspaces[scr->current_workspace];
1214 GC gc;
1215 Window win = aicon->icon->core->window;
1216 int length, nlength;
1217 char *ws_name, ws_number[10];
1218 int ty, tx;
1220 wIconPaint(aicon->icon);
1222 length = strlen(workspace->name);
1223 ws_name = malloc(length + 1);
1224 sprintf(ws_name, "%s", workspace->name);
1225 sprintf(ws_number, "%i", scr->current_workspace + 1);
1226 nlength = strlen(ws_number);
1228 gc = scr->clip_title_gc;
1230 if (!workspace->clip->collapsed)
1231 XSetForeground(dpy, gc, scr->clip_title_pixel[CLIP_NORMAL]);
1232 else
1233 XSetForeground(dpy, gc, scr->clip_title_pixel[CLIP_COLLAPSED]);
1235 ty = ICON_SIZE - scr->clip_title_font->height - 3;
1237 tx = CLIP_BUTTON_SIZE*ICON_SIZE/64;
1239 wDrawString(win, scr->clip_title_font, gc, tx,
1240 ty + scr->clip_title_font->y, ws_name, length);
1242 tx = (ICON_SIZE/2 - wTextWidth(scr->clip_title_font->font, ws_number, nlength))/2;
1244 wDrawString(win, scr->clip_title_font, gc, tx,
1245 scr->clip_title_font->y + 2, ws_number, nlength);
1247 free(ws_name);
1249 if (aicon->launching) {
1250 XFillRectangle(dpy, aicon->icon->core->window, scr->stipple_gc,
1251 0, 0, wPreferences.icon_size, wPreferences.icon_size);
1253 paintClipButtons(aicon, aicon->dock->lclip_button_pushed,
1254 aicon->dock->rclip_button_pushed);
1258 static void
1259 clipIconExpose(WObjDescriptor *desc, XEvent *event)
1261 wClipIconPaint(desc->parent);
1265 static void
1266 dockIconPaint(WAppIcon *btn)
1268 if (btn == btn->icon->core->screen_ptr->clip_icon)
1269 wClipIconPaint(btn);
1270 else
1271 wAppIconPaint(btn);
1275 static proplist_t
1276 make_icon_state(WAppIcon *btn)
1278 proplist_t node = NULL;
1279 proplist_t command, autolaunch, name, forced, host, position, buggy;
1280 char *tmp;
1281 char buffer[64];
1283 if (btn) {
1284 if (!btn->command)
1285 command = PLMakeString("-");
1286 else
1287 command = PLMakeString(btn->command);
1289 autolaunch = btn->auto_launch ? dYes : dNo;
1291 tmp = EscapeWM_CLASS(btn->wm_instance, btn->wm_class);
1293 name = PLMakeString(tmp);
1295 free(tmp);
1297 forced = btn->forced_dock ? dYes : dNo;
1299 buggy = btn->buggy_app ? dYes: dNo;
1301 if (btn == btn->icon->core->screen_ptr->clip_icon)
1302 sprintf(buffer, "%i,%i", btn->x_pos, btn->y_pos);
1303 else
1304 sprintf(buffer, "%hi,%hi", btn->xindex, btn->yindex);
1305 position = PLMakeString(buffer);
1307 node = PLMakeDictionaryFromEntries(dCommand, command,
1308 dName, name,
1309 dAutoLaunch, autolaunch,
1310 dForced, forced,
1311 dBuggyApplication, buggy,
1312 dPosition, position,
1313 NULL);
1314 PLRelease(command);
1315 PLRelease(name);
1316 PLRelease(position);
1317 #ifdef OFFIX_DND
1318 if (btn->dnd_command) {
1319 command = PLMakeString(btn->dnd_command);
1320 PLInsertDictionaryEntry(node, dDropCommand, command);
1321 PLRelease(command);
1323 #endif /* OFFIX_DND */
1325 if (btn->client_machine && btn->remote_start) {
1326 host = PLMakeString(btn->client_machine);
1327 PLInsertDictionaryEntry(node, dHost, host);
1328 PLRelease(host);
1332 return node;
1336 static proplist_t
1337 dockSaveState(WDock *dock)
1339 int i;
1340 proplist_t icon_info;
1341 proplist_t list=NULL, dock_state=NULL;
1342 proplist_t value;
1343 char buffer[256];
1345 list = PLMakeArrayFromElements(NULL);
1347 for (i=(dock->type==WM_DOCK ? 0 : 1); i<dock->max_icons; i++) {
1348 WAppIcon *btn = dock->icon_array[i];
1350 if (!btn || (btn->attracted && !dock->keep_attracted))
1351 continue;
1353 if ((icon_info = make_icon_state(dock->icon_array[i]))) {
1354 list = PLAppendArrayElement(list, icon_info);
1355 PLRelease(icon_info);
1359 dock_state = PLMakeDictionaryFromEntries(dApplications, list, NULL);
1361 PLRelease(list);
1363 if (dock->type == WM_DOCK) {
1364 sprintf(buffer, "%i,%i", (dock->on_right_side ? -ICON_SIZE : 0),
1365 dock->y_pos);
1366 value = PLMakeString(buffer);
1367 PLInsertDictionaryEntry(dock_state, dPosition, value);
1368 PLRelease(value);
1371 value = (dock->lowered ? dYes : dNo);
1372 PLInsertDictionaryEntry(dock_state, dLowered, value);
1374 if (dock->type == WM_CLIP) {
1375 value = (dock->collapsed ? dYes : dNo);
1376 PLInsertDictionaryEntry(dock_state, dCollapsed, value);
1378 value = (dock->auto_collapse ? dYes : dNo);
1379 PLInsertDictionaryEntry(dock_state, dAutoCollapse, value);
1381 value = (dock->auto_raise_lower ? dYes : dNo);
1382 PLInsertDictionaryEntry(dock_state, dAutoRaiseLower, value);
1384 value = (dock->attract_icons ? dYes : dNo);
1385 PLInsertDictionaryEntry(dock_state, dAutoAttractIcons, value);
1387 value = (dock->keep_attracted ? dYes : dNo);
1388 PLInsertDictionaryEntry(dock_state, dKeepAttracted, value);
1391 return dock_state;
1395 void
1396 wDockSaveState(WScreen *scr)
1398 proplist_t dock_state;
1400 dock_state = dockSaveState(scr->dock);
1402 PLInsertDictionaryEntry(scr->session_state, dDock, dock_state);
1404 PLRelease(dock_state);
1408 void
1409 wClipSaveState(WScreen *scr)
1411 proplist_t clip_state;
1413 clip_state = make_icon_state(scr->clip_icon);
1415 PLInsertDictionaryEntry(scr->session_state, dClip, clip_state);
1417 PLRelease(clip_state);
1421 proplist_t
1422 wClipSaveWorkspaceState(WScreen *scr, int workspace)
1424 return dockSaveState(scr->workspaces[workspace]->clip);
1428 static WAppIcon*
1429 restore_icon_state(WScreen *scr, proplist_t info, int type, int index)
1431 WAppIcon *aicon;
1432 char *wclass, *winstance;
1433 proplist_t cmd, value;
1434 char *command;
1437 cmd = PLGetDictionaryEntry(info, dCommand);
1438 if (!cmd || !PLIsString(cmd)) {
1439 return NULL;
1442 /* parse window name */
1443 value = PLGetDictionaryEntry(info, dName);
1444 if (!value)
1445 return NULL;
1447 ParseWindowName(value, &winstance, &wclass, "dock");
1449 if (!winstance && !wclass) {
1450 return NULL;
1453 /* get commands */
1455 if (cmd)
1456 command = wstrdup(PLGetString(cmd));
1457 else
1458 command = NULL;
1460 if (!command || strcmp(command, "-")==0) {
1461 if (command)
1462 free(command);
1463 if (wclass)
1464 free(wclass);
1465 if (winstance)
1466 free(winstance);
1468 return NULL;
1471 aicon = wAppIconCreateForDock(scr, command, winstance, wclass,
1472 TILE_NORMAL);
1473 if (wclass)
1474 free(wclass);
1475 if (winstance)
1476 free(winstance);
1478 aicon->icon->core->descriptor.handle_mousedown = iconMouseDown;
1479 if (type == WM_CLIP) {
1480 aicon->icon->core->descriptor.handle_enternotify = clipEnterNotify;
1481 aicon->icon->core->descriptor.handle_leavenotify = clipLeaveNotify;
1483 aicon->icon->core->descriptor.parent_type = WCLASS_DOCK_ICON;
1484 aicon->icon->core->descriptor.parent = aicon;
1487 #ifdef OFFIX_DND
1488 cmd = PLGetDictionaryEntry(info, dDropCommand);
1489 if (cmd)
1490 aicon->dnd_command = wstrdup(PLGetString(cmd));
1491 #endif
1493 /* check auto launch */
1494 value = PLGetDictionaryEntry(info, dAutoLaunch);
1496 aicon->auto_launch = 0;
1497 if (value) {
1498 if (PLIsString(value)) {
1499 if (strcasecmp(PLGetString(value), "YES")==0)
1500 aicon->auto_launch = 1;
1501 } else {
1502 wwarning(_("bad value in docked icon state info %s"),
1503 PLGetString(dAutoLaunch));
1507 /* check if it wasn't normally docked */
1508 value = PLGetDictionaryEntry(info, dForced);
1510 aicon->forced_dock = 0;
1511 if (value) {
1512 if (PLIsString(value)) {
1513 if (strcasecmp(PLGetString(value), "YES")==0)
1514 aicon->forced_dock = 1;
1515 } else {
1516 wwarning(_("bad value in docked icon state info %s"),
1517 PLGetString(dForced));
1521 /* check if we can rely on the stuff in the app */
1522 value = PLGetDictionaryEntry(info, dBuggyApplication);
1524 aicon->buggy_app = 0;
1525 if (value) {
1526 if (PLIsString(value)) {
1527 if (strcasecmp(PLGetString(value), "YES")==0)
1528 aicon->buggy_app = 1;
1529 } else {
1530 wwarning(_("bad value in docked icon state info %s"),
1531 PLGetString(dBuggyApplication));
1535 /* get position in the dock */
1536 value = PLGetDictionaryEntry(info, dPosition);
1537 if (value && PLIsString(value)) {
1538 if (sscanf(PLGetString(value), "%hi,%hi", &aicon->xindex,
1539 &aicon->yindex)!=2)
1540 wwarning(_("bad value in docked icon state info %s"),
1541 PLGetString(dPosition));
1543 /* check position sanity */
1544 /* incomplete section! */
1545 if (type == WM_DOCK) {
1546 aicon->xindex = 0;
1547 if (aicon->yindex < 0)
1548 wwarning(_("bad value in docked icon position %i,%i"),
1549 aicon->xindex, aicon->yindex);
1551 } else {
1552 aicon->yindex = index;
1553 aicon->xindex = 0;
1556 aicon->running = 0;
1557 aicon->docked = 1;
1559 return aicon;
1563 #define COMPLAIN(key) wwarning(_("bad value in dock state info:%s"), key)
1566 WAppIcon*
1567 wClipRestoreState(WScreen *scr, proplist_t clip_state)
1569 WAppIcon *icon;
1570 proplist_t value;
1573 icon = mainIconCreate(scr, WM_CLIP);
1575 if (!clip_state)
1576 return icon;
1577 else
1578 PLRetain(clip_state);
1580 /* restore position */
1582 value = PLGetDictionaryEntry(clip_state, dPosition);
1584 if (value) {
1585 if (!PLIsString(value))
1586 COMPLAIN("Position");
1587 else {
1588 if (sscanf(PLGetString(value), "%i,%i", &icon->x_pos,
1589 &icon->y_pos)!=2)
1590 COMPLAIN("Position");
1592 /* check position sanity */
1593 if (icon->y_pos < 0)
1594 icon->y_pos = 0;
1595 else if (icon->y_pos > scr->scr_height-ICON_SIZE)
1596 icon->y_pos = scr->scr_height-ICON_SIZE;
1598 if (icon->x_pos < 0)
1599 icon->x_pos = 0;
1600 else if (icon->x_pos > scr->scr_width-ICON_SIZE)
1601 icon->x_pos = scr->scr_width-ICON_SIZE;
1605 #ifdef OFFIX_DND
1606 value = PLGetDictionaryEntry(clip_state, dDropCommand);
1607 if (value && PLIsString(value))
1608 icon->dnd_command = wstrdup(PLGetString(value));
1609 #endif
1611 PLRelease(clip_state);
1613 return icon;
1617 WDock*
1618 wDockRestoreState(WScreen *scr, proplist_t dock_state, int type)
1620 WDock *dock;
1621 proplist_t apps;
1622 proplist_t value;
1623 WAppIcon *aicon, *old_top;
1624 int count, i;
1627 dock = wDockCreate(scr, type);
1629 if (!dock_state)
1630 return dock;
1632 if (dock_state)
1633 PLRetain(dock_state);
1636 /* restore position */
1638 value = PLGetDictionaryEntry(dock_state, dPosition);
1640 if (value) {
1641 if (!PLIsString(value))
1642 COMPLAIN("Position");
1643 else {
1644 if (sscanf(PLGetString(value), "%i,%i", &dock->x_pos,
1645 &dock->y_pos)!=2)
1646 COMPLAIN("Position");
1648 /* check position sanity */
1649 if (dock->y_pos < 0)
1650 dock->y_pos = 0;
1651 else if (dock->y_pos > scr->scr_height-ICON_SIZE)
1652 dock->y_pos = scr->scr_height - ICON_SIZE;
1654 /* This is no more needed. ??? */
1655 if (type == WM_CLIP) {
1656 if (dock->x_pos < 0)
1657 dock->x_pos = 0;
1658 else if (dock->x_pos > scr->scr_width-ICON_SIZE)
1659 dock->x_pos = scr->scr_width-ICON_SIZE;
1660 } else {
1661 if (dock->x_pos >= 0) {
1662 dock->x_pos = DOCK_EXTRA_SPACE;
1663 dock->on_right_side = 0;
1664 } else {
1665 dock->x_pos = scr->scr_width - DOCK_EXTRA_SPACE - ICON_SIZE;
1666 dock->on_right_side = 1;
1672 /* restore lowered/raised state */
1674 dock->lowered = 0;
1676 value = PLGetDictionaryEntry(dock_state, dLowered);
1678 if (value) {
1679 if (!PLIsString(value))
1680 COMPLAIN("Lowered");
1681 else {
1682 if (strcasecmp(PLGetString(value), "YES")==0)
1683 dock->lowered = 1;
1688 /* restore collapsed state */
1690 dock->collapsed = 0;
1692 value = PLGetDictionaryEntry(dock_state, dCollapsed);
1694 if (value) {
1695 if (!PLIsString(value))
1696 COMPLAIN("Collapsed");
1697 else {
1698 if (strcasecmp(PLGetString(value), "YES")==0)
1699 dock->collapsed = 1;
1704 /* restore auto-collapsed state */
1706 value = PLGetDictionaryEntry(dock_state, dAutoCollapse);
1708 if (value) {
1709 if (!PLIsString(value))
1710 COMPLAIN("AutoCollapse");
1711 else {
1712 if (strcasecmp(PLGetString(value), "YES")==0) {
1713 dock->auto_collapse = 1;
1714 dock->collapsed = 1;
1720 /* restore auto-raise/lower state */
1722 value = PLGetDictionaryEntry(dock_state, dAutoRaiseLower);
1724 if (value) {
1725 if (!PLIsString(value))
1726 COMPLAIN("AutoRaiseLower");
1727 else {
1728 if (strcasecmp(PLGetString(value), "YES")==0) {
1729 dock->auto_raise_lower = 1;
1734 /* restore attract icons state */
1736 dock->attract_icons = 0;
1738 value = PLGetDictionaryEntry(dock_state, dAutoAttractIcons);
1740 if (value) {
1741 if (!PLIsString(value))
1742 COMPLAIN("AutoAttractIcons");
1743 else {
1744 if (strcasecmp(PLGetString(value), "YES")==0)
1745 dock->attract_icons = 1;
1750 /* restore keep attracted icons state */
1752 dock->keep_attracted = 0;
1754 value = PLGetDictionaryEntry(dock_state, dKeepAttracted);
1756 if (value) {
1757 if (!PLIsString(value))
1758 COMPLAIN("KeepAttracted");
1759 else {
1760 if (strcasecmp(PLGetString(value), "YES")==0)
1761 dock->keep_attracted = 1;
1766 /* application list */
1768 apps = PLGetDictionaryEntry(dock_state, dApplications);
1770 if (!apps) {
1771 goto finish;
1774 count = PLGetNumberOfElements(apps);
1776 if (count==0)
1777 goto finish;
1779 old_top = dock->icon_array[0];
1781 /* dock->icon_count is set to 1 when dock is created.
1782 * Since Clip is already restored, we want to keep it so for clip,
1783 * but for dock we may change the default top tile, so we set it to 0.
1785 if (type == WM_DOCK)
1786 dock->icon_count = 0;
1788 for (i=0; i<count; i++) {
1789 if (dock->icon_count >= dock->max_icons) {
1790 wwarning(_("there are too many icons stored in dock. Ignoring what doesn't fit"));
1791 break;
1794 value = PLGetArrayElement(apps, i);
1795 aicon = restore_icon_state(scr, value, type, dock->icon_count);
1797 dock->icon_array[dock->icon_count] = aicon;
1799 if (aicon) {
1800 aicon->dock = dock;
1801 aicon->x_pos = dock->x_pos + (aicon->xindex*ICON_SIZE);
1802 aicon->y_pos = dock->y_pos + (aicon->yindex*ICON_SIZE);
1804 if (dock->lowered)
1805 ChangeStackingLevel(aicon->icon->core, WMNormalLevel);
1806 else
1807 ChangeStackingLevel(aicon->icon->core, WMDockLevel);
1809 wCoreConfigure(aicon->icon->core, aicon->x_pos, aicon->y_pos,
1810 0, 0);
1812 if (!dock->collapsed)
1813 XMapWindow(dpy, aicon->icon->core->window);
1814 wRaiseFrame(aicon->icon->core);
1816 dock->icon_count++;
1817 } else if (dock->icon_count==0 && type==WM_DOCK)
1818 dock->icon_count++;
1821 /* if the first icon is not defined, use the default */
1822 if (dock->icon_array[0]==NULL) {
1823 /* update default icon */
1824 old_top->x_pos = dock->x_pos;
1825 old_top->y_pos = dock->y_pos;
1826 if (dock->lowered)
1827 ChangeStackingLevel(old_top->icon->core, WMNormalLevel);
1828 else
1829 ChangeStackingLevel(old_top->icon->core, WMDockLevel);
1830 dock->icon_array[0] = old_top;
1831 XMoveWindow(dpy, old_top->icon->core->window, dock->x_pos, dock->y_pos);
1832 /* we don't need to increment dock->icon_count here because it was
1833 * incremented in the loop above.
1835 } else if (old_top!=dock->icon_array[0]) {
1836 if (old_top == scr->clip_icon)
1837 scr->clip_icon = dock->icon_array[0];
1838 wAppIconDestroy(old_top);
1841 finish:
1842 if (dock_state)
1843 PLRelease(dock_state);
1845 return dock;
1850 void
1851 wDockLaunchWithState(WDock *dock, WAppIcon *btn, WSavedState *state)
1853 if (btn && btn->command && !btn->running && !btn->launching) {
1855 btn->drop_launch = 0;
1857 btn->pid = execCommand(btn, btn->command, state);
1859 if (btn->pid>0) {
1860 if (!btn->forced_dock && !btn->buggy_app) {
1861 btn->launching = 1;
1862 dockIconPaint(btn);
1865 } else {
1866 free(state);
1871 void
1872 wDockDoAutoLaunch(WDock *dock, int workspace)
1874 WAppIcon *btn;
1875 WSavedState *state;
1876 int i;
1878 for (i = 0; i < dock->max_icons; i++) {
1879 btn = dock->icon_array[i];
1880 if (!btn || !btn->auto_launch)
1881 continue;
1883 state = wmalloc(sizeof(WSavedState));
1884 memset(state, 0, sizeof(WSavedState));
1885 state->workspace = workspace;
1886 /* TODO: this is klugy and is very difficult to understand
1887 * what's going on. Try to clean up */
1888 wDockLaunchWithState(dock, btn, state);
1892 #ifdef REDUCE_APPICONS
1893 void
1894 wDockSimulateLaunch(WDock *dock, WAppIcon *btn)
1896 if ((btn == NULL) || (dock == NULL))
1897 return;
1899 if (!btn->running) {
1900 if ((btn->icon->owner == NULL) && (btn->applist))
1901 btn->icon->owner = btn->applist->wapp->main_window_desc;
1902 if (!btn->forced_dock)
1903 btn->launching = 1;
1904 dockIconPaint(btn);
1905 wusleep(5000);
1908 #endif
1910 #ifdef OFFIX_DND
1911 static WDock*
1912 findDock(WScreen *scr, XEvent *event, int *icon_pos)
1914 WDock *dock;
1915 int i;
1917 *icon_pos = -1;
1918 if ((dock = scr->dock)!=NULL) {
1919 for (i=0; i<dock->max_icons; i++) {
1920 if (dock->icon_array[i]
1921 && dock->icon_array[i]->icon->core->window==event->xclient.window) {
1922 *icon_pos = i;
1923 break;
1927 if (*icon_pos<0 && (dock = scr->workspaces[scr->current_workspace]->clip)!=NULL) {
1928 for (i=0; i<dock->max_icons; i++) {
1929 if (dock->icon_array[i]
1930 && dock->icon_array[i]->icon->core->window==event->xclient.window) {
1931 *icon_pos = i;
1932 break;
1936 if(*icon_pos>=0)
1937 return dock;
1938 return NULL;
1943 wDockReceiveDNDDrop(WScreen *scr, XEvent *event)
1945 WDock *dock;
1946 WAppIcon *btn;
1947 int icon_pos;
1949 dock = findDock(scr, event, &icon_pos);
1950 if (!dock)
1951 return False;
1954 * Return True if the drop was on an application icon window.
1955 * In this case, let the ClientMessage handler redirect the
1956 * message to the app.
1958 if (dock->icon_array[icon_pos]->icon->icon_win!=None)
1959 return True;
1961 if (dock->icon_array[icon_pos]->dnd_command!=NULL) {
1962 scr->flags.dnd_data_convertion_status = 0;
1964 btn = dock->icon_array[icon_pos];
1966 if (!btn->forced_dock) {
1967 btn->relaunching = btn->running;
1968 btn->running = 1;
1970 if (btn->wm_instance || btn->wm_class) {
1971 WWindowAttributes attr;
1972 memset(&attr, 0, sizeof(WWindowAttributes));
1973 wDefaultFillAttributes(btn->icon->core->screen_ptr,
1974 btn->wm_instance,
1975 btn->wm_class, &attr, NULL, True);
1977 if (!attr.no_appicon)
1978 btn->launching = 1;
1979 else
1980 btn->running = 0;
1983 btn->drop_launch = 1;
1984 scr->last_dock = dock;
1985 btn->pid = execCommand(btn, btn->dnd_command, NULL);
1986 if (btn->pid>0) {
1987 dockIconPaint(btn);
1988 } else {
1989 btn->launching = 0;
1990 if (!btn->relaunching) {
1991 btn->running = 0;
1995 return False;
1997 #endif /* OFFIX_DND */
2001 Bool
2002 wDockAttachIcon(WDock *dock, WAppIcon *icon, int x, int y)
2004 WWindow *wwin;
2005 char **argv;
2006 int argc;
2007 int index;
2009 wwin = icon->icon->owner;
2010 if (icon->command==NULL) {
2011 icon->editing = 0;
2012 if (XGetCommand(dpy, wwin->client_win, &argv, &argc) && argc>0) {
2014 icon->command = FlattenStringList(argv, argc);
2015 XFreeStringList(argv);
2016 } else {
2017 char *command=NULL;
2019 /* icon->forced_dock = 1;*/
2020 if (!icon->attracted || dock->type!=WM_CLIP || dock->keep_attracted) {
2021 icon->editing = 1;
2022 if (wInputDialog(dock->screen_ptr, _("Dock Icon"),
2023 _("Type the command used to launch the application"),
2024 &command)) {
2025 if (command && (command[0]==0 ||
2026 (command[0]=='-' && command[1]==0))) {
2027 free(command);
2028 command = NULL;
2030 icon->command = command;
2031 icon->editing = 0;
2032 } else {
2033 icon->editing = 0;
2034 if (command)
2035 free(command);
2036 /* If the target is the dock, reject the icon. If
2037 * the target is the clip, make it an attracted icon
2039 if (dock->type==WM_CLIP) {
2040 icon->attracted = 1;
2041 if (!icon->icon->shadowed) {
2042 icon->icon->shadowed = 1;
2043 icon->icon->force_paint = 1;
2045 } else {
2046 return False;
2051 } else {
2052 icon->editing = 0;
2055 for (index=1; index<dock->max_icons; index++)
2056 if (dock->icon_array[index] == NULL)
2057 break;
2058 /* if (index == dock->max_icons)
2059 return; */
2061 assert(index < dock->max_icons);
2063 dock->icon_array[index] = icon;
2064 icon->yindex = y;
2065 icon->xindex = x;
2067 icon->x_pos = dock->x_pos + x*ICON_SIZE;
2068 icon->y_pos = dock->y_pos + y*ICON_SIZE;
2070 dock->icon_count++;
2072 icon->running = 1;
2073 icon->launching = 0;
2074 icon->docked = 1;
2075 icon->dock = dock;
2076 icon->icon->core->descriptor.handle_mousedown = iconMouseDown;
2077 if (dock->type == WM_CLIP) {
2078 icon->icon->core->descriptor.handle_enternotify = clipEnterNotify;
2079 icon->icon->core->descriptor.handle_leavenotify = clipLeaveNotify;
2081 icon->icon->core->descriptor.parent_type = WCLASS_DOCK_ICON;
2082 icon->icon->core->descriptor.parent = icon;
2084 MoveInStackListUnder(dock->icon_array[index-1]->icon->core,
2085 icon->icon->core);
2086 wAppIconMove(icon, icon->x_pos, icon->y_pos);
2087 wAppIconPaint(icon);
2089 if (wPreferences.auto_arrange_icons)
2090 wArrangeIcons(dock->screen_ptr, True);
2092 #ifdef OFFIX_DND
2093 if (icon->command && !icon->dnd_command) {
2094 icon->dnd_command = wmalloc(strlen(icon->command)+8);
2095 sprintf(icon->dnd_command, "%s %%d", icon->command);
2097 #endif
2099 return True;
2103 void
2104 reattachIcon(WDock *dock, WAppIcon *icon, int x, int y)
2106 int index;
2108 for(index=1; index<dock->max_icons; index++) {
2109 if(dock->icon_array[index] == icon)
2110 break;
2112 assert(index < dock->max_icons);
2114 icon->yindex = y;
2115 icon->xindex = x;
2117 icon->x_pos = dock->x_pos + x*ICON_SIZE;
2118 icon->y_pos = dock->y_pos + y*ICON_SIZE;
2122 Bool
2123 moveIconBetweenDocks(WDock *src, WDock *dest, WAppIcon *icon, int x, int y)
2125 WWindow *wwin;
2126 char **argv;
2127 int argc;
2128 int index;
2130 if (dest == NULL)
2131 return False;
2133 wwin = icon->icon->owner;
2136 * For the moment we can't do this if we move icons in Clip from one
2137 * workspace to other, because if we move two or more icons without
2138 * command, the dialog box will not be able to tell us to which of the
2139 * moved icons it applies. -Dan
2141 if ((dest->type==WM_DOCK /*|| dest->keep_attracted*/) && icon->command==NULL) {
2142 if (XGetCommand(dpy, wwin->client_win, &argv, &argc) && argc>0) {
2144 icon->command = FlattenStringList(argv, argc);
2145 XFreeStringList(argv);
2146 } else {
2147 char *command=NULL;
2149 icon->editing = 1;
2150 /* icon->forced_dock = 1;*/
2151 if (wInputDialog(src->screen_ptr, _("Dock Icon"),
2152 _("Type the command used to launch the application"),
2153 &command)) {
2154 if (command && (command[0]==0 ||
2155 (command[0]=='-' && command[1]==0))) {
2156 free(command);
2157 command = NULL;
2159 icon->command = command;
2160 } else {
2161 icon->editing = 0;
2162 if (command)
2163 free(command);
2164 return False;
2166 icon->editing = 0;
2170 for(index=1; index<src->max_icons; index++) {
2171 if(src->icon_array[index] == icon)
2172 break;
2174 assert(index < src->max_icons);
2176 src->icon_array[index] = NULL;
2177 src->icon_count--;
2179 for(index=1; index<dest->max_icons; index++) {
2180 if(dest->icon_array[index] == NULL)
2181 break;
2183 /* if (index == dest->max_icons)
2184 return; */
2186 assert(index < dest->max_icons);
2188 dest->icon_array[index] = icon;
2189 icon->dock = dest;
2191 /* deselect the icon */
2192 if (icon->icon->selected)
2193 wIconSelect(icon->icon);
2195 if (dest->type == WM_DOCK) {
2196 icon->icon->core->descriptor.handle_enternotify = NULL;
2197 icon->icon->core->descriptor.handle_leavenotify = NULL;
2198 } else {
2199 icon->icon->core->descriptor.handle_enternotify = clipEnterNotify;
2200 icon->icon->core->descriptor.handle_leavenotify = clipLeaveNotify;
2203 /* set it to be kept when moving to dock, or to a clip that keep the
2204 * attracted icons.
2205 * Unless the icon does not have a command set
2207 if (icon->command && (dest->type==WM_DOCK || dest->keep_attracted)) {
2208 icon->attracted = 0;
2209 if (icon->icon->shadowed) {
2210 icon->icon->shadowed = 0;
2211 icon->icon->force_paint = 1;
2215 if (src->auto_collapse || src->auto_raise_lower)
2216 clipLeave(src);
2218 icon->yindex = y;
2219 icon->xindex = x;
2221 icon->x_pos = dest->x_pos + x*ICON_SIZE;
2222 icon->y_pos = dest->y_pos + y*ICON_SIZE;
2224 dest->icon_count++;
2226 MoveInStackListUnder(dest->icon_array[index-1]->icon->core,
2227 icon->icon->core);
2228 wAppIconPaint(icon);
2230 return True;
2234 void
2235 wDockDetach(WDock *dock, WAppIcon *icon)
2237 int index;
2239 /* make the settings panel be closed */
2240 if (icon->panel) {
2241 DestroyDockAppSettingsPanel(icon->panel);
2244 icon->docked = 0;
2245 icon->dock = NULL;
2246 icon->attracted = 0;
2247 if (icon->icon->shadowed) {
2248 icon->icon->shadowed = 0;
2249 icon->icon->force_paint = 1;
2252 /* deselect the icon */
2253 if (icon->icon->selected)
2254 wIconSelect(icon->icon);
2256 if (icon->command) {
2257 free(icon->command);
2258 icon->command = NULL;
2260 #ifdef OFFIX_DND
2261 if (icon->dnd_command) {
2262 free(icon->dnd_command);
2263 icon->dnd_command = NULL;
2265 #endif
2267 for (index=1; index<dock->max_icons; index++)
2268 if (dock->icon_array[index] == icon)
2269 break;
2270 assert(index < dock->max_icons);
2271 dock->icon_array[index] = NULL;
2272 icon->yindex = -1;
2273 icon->xindex = -1;
2274 dock->icon_count--;
2276 /* if the dock is not attached to an application or
2277 * the the application did not set the approriate hints yet,
2278 * destroy the icon */
2279 #ifdef REDUCE_APPICONS
2280 if ((icon->num_apps == 0) && (!icon->running || !wApplicationOf(icon->main_window)) )
2281 #else
2282 if (!icon->running || !wApplicationOf(icon->main_window))
2283 #endif
2284 wAppIconDestroy(icon);
2285 else {
2286 icon->icon->core->descriptor.handle_mousedown = appIconMouseDown;
2287 icon->icon->core->descriptor.handle_enternotify = NULL;
2288 icon->icon->core->descriptor.handle_leavenotify = NULL;
2289 icon->icon->core->descriptor.parent_type = WCLASS_APPICON;
2290 icon->icon->core->descriptor.parent = icon;
2292 ChangeStackingLevel(icon->icon->core, NORMAL_ICON_LEVEL);
2294 wAppIconPaint(icon);
2295 if (wPreferences.auto_arrange_icons) {
2296 wArrangeIcons(dock->screen_ptr, True);
2299 if (dock->auto_collapse || dock->auto_raise_lower)
2300 clipLeave(dock);
2305 * returns the closest Dock slot index for the passed
2306 * coordinates.
2308 * Returns False if icon can't be docked.
2310 * Note: this function should NEVER alter ret_x or ret_y, unless it will
2311 * return True. -Dan
2313 Bool
2314 wDockSnapIcon(WDock *dock, WAppIcon *icon, int req_x, int req_y,
2315 int *ret_x, int *ret_y, int redocking)
2317 WScreen *scr = dock->screen_ptr;
2318 int dx, dy;
2319 int ex_x, ex_y;
2320 int i, offset = ICON_SIZE/2;
2321 WAppIcon *aicon = NULL;
2322 WAppIcon *nicon = NULL;
2323 int max_y_icons, max_x_icons;
2325 max_x_icons = scr->scr_width/ICON_SIZE;
2326 max_y_icons = scr->scr_height/ICON_SIZE-1;
2328 if (wPreferences.flags.noupdates)
2329 return False;
2331 dx = dock->x_pos;
2332 dy = dock->y_pos;
2334 /* if the dock is full */
2335 if (!redocking &&
2336 (dock->icon_count >= dock->max_icons)) {
2337 return False;
2340 /* exact position */
2341 if (req_y < dy)
2342 ex_y = (req_y - offset - dy)/ICON_SIZE;
2343 else
2344 ex_y = (req_y + offset - dy)/ICON_SIZE;
2346 if (req_x < dx)
2347 ex_x = (req_x - offset - dx)/ICON_SIZE;
2348 else
2349 ex_x = (req_x + offset - dx)/ICON_SIZE;
2351 /* check if the icon is outside the screen boundaries */
2352 if (dx + ex_x*ICON_SIZE < -ICON_SIZE+2 ||
2353 dx + ex_x*ICON_SIZE >= scr->scr_width-1 ||
2354 dy + ex_y*ICON_SIZE < -ICON_SIZE+2 ||
2355 dy + ex_y*ICON_SIZE >= scr->scr_height-1)
2356 return False;
2358 if (dock->type == WM_DOCK) {
2359 if (icon->dock != dock && ex_x != 0)
2360 return False;
2362 for (i=0; i<dock->max_icons; i++) {
2363 nicon = dock->icon_array[i];
2364 if (nicon && nicon->yindex == ex_y) {
2365 aicon = nicon;
2366 break;
2370 if (redocking) {
2371 int sig, done, closest;
2373 /* Possible cases when redocking:
2375 * icon dragged out of range of any slot -> false
2376 * icon dragged to range of free slot
2377 * icon dragged to range of same slot
2378 * icon dragged to range of different icon
2380 if (abs(ex_x) > DOCK_DETTACH_THRESHOLD)
2381 return False;
2383 if (ex_y>=0 && ex_y<=max_y_icons && (aicon==icon || !aicon)) {
2384 *ret_x = 0;
2385 *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))) {
2421 *ret_x = 0;
2422 *ret_y = closest;
2423 return True;
2425 } else { /* !redocking */
2427 /* if slot is free and the icon is close enough, return it */
2428 if (!aicon && ex_x == 0 && ex_y >= 0 && ex_y <= max_y_icons) {
2429 *ret_x = 0;
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;
3302 #else
3303 if (btn->icon->owner && !(event->xbutton.state & ControlMask)) {
3304 #endif
3305 wapp = wApplicationOf(btn->icon->owner->main_window);
3307 assert(wapp!=NULL);
3309 unhideHere = (event->xbutton.state & ShiftMask);
3311 /* go to the last workspace that the user worked on the app */
3312 if (wapp->last_workspace != dock->screen_ptr->current_workspace
3313 && !unhideHere) {
3314 wWorkspaceChange(dock->screen_ptr, wapp->last_workspace);
3317 wUnhideApplication(wapp, event->xbutton.button==Button2,
3318 unhideHere);
3320 if (event->xbutton.state & MOD_MASK) {
3321 wHideOtherApplications(btn->icon->owner);
3323 } else {
3324 if (event->xbutton.button==Button1) {
3326 if (event->xbutton.state & MOD_MASK) {
3327 /* raise/lower dock */
3328 toggleLowered(dock);
3329 } else if (btn == dock->screen_ptr->clip_icon) {
3330 if (getClipButton(event->xbutton.x, event->xbutton.y)==CLIP_IDLE)
3331 toggleCollapsed(dock);
3332 else
3333 handleClipChangeWorkspace(dock->screen_ptr, event);
3334 } else if (btn->command) {
3335 if (!btn->launching &&
3336 (!btn->running || (event->xbutton.state & ControlMask))) {
3337 launchDockedApplication(btn);
3339 } else if (btn->xindex == 0 && btn->yindex == 0
3340 && btn->dock->type == WM_DOCK) {
3342 wShowGNUstepPanel(dock->screen_ptr);
3349 static void
3350 handleDockMove(WDock *dock, WAppIcon *aicon, XEvent *event)
3352 WScreen *scr = dock->screen_ptr;
3353 int ofs_x=event->xbutton.x, ofs_y=event->xbutton.y;
3354 int x, y;
3355 XEvent ev;
3356 int grabbed = 0, swapped = 0, done;
3357 Pixmap ghost = None;
3358 int superfluous = wPreferences.superfluous; /* we catch it to avoid problems */
3360 #ifdef DEBUG
3361 puts("moving dock");
3362 #endif
3363 if (XGrabPointer(dpy, aicon->icon->core->window, True, ButtonMotionMask
3364 |ButtonReleaseMask|ButtonPressMask, GrabModeAsync,
3365 GrabModeAsync, None, None, CurrentTime) !=GrabSuccess) {
3366 wwarning("pointer grab failed for dock move");
3368 y = 0;
3369 for (x=0; x<dock->max_icons; x++) {
3370 if (dock->icon_array[x]!=NULL &&
3371 dock->icon_array[x]->yindex > y)
3372 y = dock->icon_array[x]->yindex;
3374 y++;
3375 XResizeWindow(dpy, scr->dock_shadow, ICON_SIZE, ICON_SIZE*y);
3377 done = 0;
3378 while (!done) {
3379 WMMaskEvent(dpy, PointerMotionMask|ButtonReleaseMask|ButtonPressMask
3380 |ButtonMotionMask|ExposureMask, &ev);
3381 switch (ev.type) {
3382 case Expose:
3383 WMHandleEvent(&ev);
3384 break;
3386 case MotionNotify:
3387 if (!grabbed) {
3388 if (abs(ofs_x-ev.xmotion.x)>=MOVE_THRESHOLD
3389 || abs(ofs_y-ev.xmotion.y)>=MOVE_THRESHOLD) {
3390 XChangeActivePointerGrab(dpy, ButtonMotionMask
3391 |ButtonReleaseMask|ButtonPressMask,
3392 wCursor[WCUR_MOVE], CurrentTime);
3393 grabbed=1;
3395 break;
3397 if (dock->type == WM_CLIP) {
3398 if (ev.xmotion.x_root - ofs_x < 0) {
3399 x = 0;
3400 } else if (ev.xmotion.x_root - ofs_x + ICON_SIZE >
3401 scr->scr_width) {
3402 x = scr->scr_width - ICON_SIZE;
3403 } else {
3404 x = ev.xmotion.x_root - ofs_x;
3406 if (ev.xmotion.y_root - ofs_y < 0) {
3407 y = 0;
3408 } else if (ev.xmotion.y_root - ofs_y + ICON_SIZE >
3409 scr->scr_height) {
3410 y = scr->scr_height - ICON_SIZE;
3411 } else {
3412 y = ev.xmotion.y_root - ofs_y;
3414 moveDock(dock, x, y);
3415 } else {
3416 /* move vertically if pointer is inside the dock*/
3417 if ((dock->on_right_side &&
3418 ev.xmotion.x_root >= dock->x_pos - ICON_SIZE)
3419 || (!dock->on_right_side &&
3420 ev.xmotion.x_root <= dock->x_pos + ICON_SIZE*2)) {
3422 if (ev.xmotion.y_root - ofs_y < 0) {
3423 y = 0;
3424 } else if (ev.xmotion.y_root - ofs_y + ICON_SIZE >
3425 scr->scr_height) {
3426 y = scr->scr_height - ICON_SIZE;
3427 } else {
3428 y = ev.xmotion.y_root - ofs_y;
3430 moveDock(dock, dock->x_pos, y);
3432 /* move horizontally to change sides */
3433 x = ev.xmotion.x_root - ofs_x;
3434 if (!dock->on_right_side) {
3436 /* is on left */
3438 if (ev.xmotion.x_root > dock->x_pos + ICON_SIZE*2) {
3439 XMoveWindow(dpy, scr->dock_shadow, scr->scr_width-ICON_SIZE
3440 -DOCK_EXTRA_SPACE-1, dock->y_pos);
3441 if (superfluous) {
3442 if (ghost==None) {
3443 ghost = MakeGhostDock(dock, dock->x_pos,
3444 scr->scr_width-ICON_SIZE
3445 -DOCK_EXTRA_SPACE-1,
3446 dock->y_pos);
3447 XSetWindowBackgroundPixmap(dpy, scr->dock_shadow,
3448 ghost);
3449 XClearWindow(dpy, scr->dock_shadow);
3452 XMapRaised(dpy, scr->dock_shadow);
3453 swapped = 1;
3454 } else {
3455 if (superfluous && ghost!=None) {
3456 XFreePixmap(dpy, ghost);
3457 ghost = None;
3459 XUnmapWindow(dpy, scr->dock_shadow);
3460 swapped = 0;
3462 } else {
3463 /* is on right */
3464 if (ev.xmotion.x_root < dock->x_pos - ICON_SIZE) {
3465 XMoveWindow(dpy, scr->dock_shadow,
3466 DOCK_EXTRA_SPACE, dock->y_pos);
3467 if (superfluous) {
3468 if (ghost==None) {
3469 ghost = MakeGhostDock(dock, dock->x_pos,
3470 DOCK_EXTRA_SPACE, dock->y_pos);
3471 XSetWindowBackgroundPixmap(dpy, scr->dock_shadow,
3472 ghost);
3473 XClearWindow(dpy, scr->dock_shadow);
3476 XMapRaised(dpy, scr->dock_shadow);
3477 swapped = -1;
3478 } else {
3479 XUnmapWindow(dpy, scr->dock_shadow);
3480 swapped = 0;
3481 if (superfluous && ghost!=None) {
3482 XFreePixmap(dpy, ghost);
3483 ghost = None;
3488 break;
3490 case ButtonPress:
3491 break;
3493 case ButtonRelease:
3494 if (ev.xbutton.button != event->xbutton.button)
3495 break;
3496 XUngrabPointer(dpy, CurrentTime);
3497 XUnmapWindow(dpy, scr->dock_shadow);
3498 XResizeWindow(dpy, scr->dock_shadow, ICON_SIZE, ICON_SIZE);
3499 if (dock->type == WM_DOCK) {
3500 if (swapped!=0) {
3501 if (swapped>0)
3502 dock->on_right_side = 1;
3503 else
3504 dock->on_right_side = 0;
3505 swapDock(dock);
3506 wArrangeIcons(scr, False);
3509 done = 1;
3510 break;
3513 if (superfluous) {
3514 if (ghost!=None)
3515 XFreePixmap(dpy, ghost);
3516 XSetWindowBackground(dpy, scr->dock_shadow, scr->white_pixel);
3518 #ifdef DEBUG
3519 puts("End dock move");
3520 #endif
3525 static void
3526 handleIconMove(WDock *dock, WAppIcon *aicon, XEvent *event)
3528 WScreen *scr = dock->screen_ptr;
3529 Window wins[2];
3530 WIcon *icon = aicon->icon;
3531 WDock *dock2 = NULL, *last_dock = dock, *clip = NULL;
3532 int ondock, grabbed = 0, change_dock = 0, collapsed = 0;
3533 XEvent ev;
3534 int x = aicon->x_pos, y = aicon->y_pos;
3535 int ofs_x = event->xbutton.x, ofs_y = event->xbutton.y;
3536 int shad_x = x, shad_y = y;
3537 int ix = aicon->xindex, iy = aicon->yindex;
3538 int tmp;
3539 Pixmap ghost = None;
3540 Bool docked;
3541 int superfluous = wPreferences.superfluous; /* we catch it to avoid problems */
3543 if (wPreferences.flags.noupdates)
3544 return;
3546 if (XGrabPointer(dpy, icon->core->window, True, ButtonMotionMask
3547 |ButtonReleaseMask|ButtonPressMask, GrabModeAsync,
3548 GrabModeAsync, None, None, CurrentTime) !=GrabSuccess) {
3549 #ifdef DEBUG0
3550 wwarning("pointer grab failed for icon move");
3551 #endif
3554 if (!(event->xbutton.state & MOD_MASK))
3555 wRaiseFrame(icon->core);
3557 if (!wPreferences.flags.noclip)
3558 clip = scr->workspaces[scr->current_workspace]->clip;
3560 if (dock == scr->dock && !wPreferences.flags.noclip)
3561 dock2 = clip;
3562 else if (dock != scr->dock && !wPreferences.flags.nodock)
3563 dock2 = scr->dock;
3565 wins[0] = icon->core->window;
3566 wins[1] = scr->dock_shadow;
3567 XRestackWindows(dpy, wins, 2);
3568 XMoveResizeWindow(dpy, scr->dock_shadow, aicon->x_pos, aicon->y_pos,
3569 ICON_SIZE, ICON_SIZE);
3570 if (superfluous) {
3571 if (icon->pixmap!=None)
3572 ghost = MakeGhostIcon(scr, icon->pixmap);
3573 else
3574 ghost = MakeGhostIcon(scr, icon->core->window);
3576 XSetWindowBackgroundPixmap(dpy, scr->dock_shadow, ghost);
3577 XClearWindow(dpy, scr->dock_shadow);
3579 XMapWindow(dpy, scr->dock_shadow);
3581 ondock = 1;
3584 while(1) {
3585 XMaskEvent(dpy, PointerMotionMask|ButtonReleaseMask|ButtonPressMask
3586 |ButtonMotionMask|ExposureMask, &ev);
3587 switch (ev.type) {
3588 case Expose:
3589 WMHandleEvent(&ev);
3590 break;
3592 case MotionNotify:
3593 if (!grabbed) {
3594 if (abs(ofs_x-ev.xmotion.x)>=MOVE_THRESHOLD
3595 || abs(ofs_y-ev.xmotion.y)>=MOVE_THRESHOLD) {
3596 XChangeActivePointerGrab(dpy, ButtonMotionMask
3597 |ButtonReleaseMask|ButtonPressMask,
3598 wCursor[WCUR_MOVE], CurrentTime);
3599 grabbed=1;
3600 } else {
3601 break;
3605 x = ev.xmotion.x_root - ofs_x;
3606 y = ev.xmotion.y_root - ofs_y;
3607 tmp = wDockSnapIcon(dock, aicon, x, y, &ix, &iy, True);
3608 if (tmp && dock2) {
3609 change_dock = 0;
3610 if (last_dock != dock && collapsed) {
3611 last_dock->collapsed = 1;
3612 wDockHideIcons(last_dock);
3613 collapsed = 0;
3615 if (!collapsed && (collapsed = dock->collapsed)) {
3616 dock->collapsed = 0;
3617 wDockShowIcons(dock);
3619 if (dock->auto_raise_lower)
3620 wDockRaise(dock);
3621 last_dock = dock;
3622 } else if (dock2) {
3623 tmp = wDockSnapIcon(dock2, aicon, x, y, &ix, &iy, False);
3624 if (tmp) {
3625 change_dock = 1;
3626 if (last_dock != dock2 && collapsed) {
3627 last_dock->collapsed = 1;
3628 wDockHideIcons(last_dock);
3629 collapsed = 0;
3631 if (!collapsed && (collapsed = dock2->collapsed)) {
3632 dock2->collapsed = 0;
3633 wDockShowIcons(dock2);
3635 if (dock2->auto_raise_lower)
3636 wDockRaise(dock2);
3637 last_dock = dock2;
3640 if (aicon->launching
3641 || (aicon->running && !(ev.xmotion.state & MOD_MASK))
3642 || (!aicon->running && tmp)) {
3643 shad_x = last_dock->x_pos + ix*wPreferences.icon_size;
3644 shad_y = last_dock->y_pos + iy*wPreferences.icon_size;
3646 XMoveWindow(dpy, scr->dock_shadow, shad_x, shad_y);
3648 if (!ondock) {
3649 XMapWindow(dpy, scr->dock_shadow);
3651 ondock = 1;
3652 } else {
3653 if (ondock) {
3654 XUnmapWindow(dpy, scr->dock_shadow);
3656 ondock = 0;
3658 XMoveWindow(dpy, icon->core->window, x, y);
3659 break;
3661 case ButtonPress:
3662 break;
3664 case ButtonRelease:
3665 if (ev.xbutton.button != event->xbutton.button)
3666 break;
3667 XUngrabPointer(dpy, CurrentTime);
3668 if (ondock) {
3669 SlideWindow(icon->core->window, x, y, shad_x, shad_y);
3670 XUnmapWindow(dpy, scr->dock_shadow);
3671 if (!change_dock) {
3672 reattachIcon(dock, aicon, ix, iy);
3673 if (clip && dock!=clip && clip->auto_raise_lower)
3674 wDockLower(clip);
3675 } else {
3676 docked = moveIconBetweenDocks(dock, dock2, aicon, ix, iy);
3677 if (!docked) {
3678 /* Slide it back if dock rejected it */
3679 SlideWindow(icon->core->window, x, y, aicon->x_pos,
3680 aicon->y_pos);
3681 reattachIcon(dock, aicon, aicon->xindex,aicon->yindex);
3683 if (last_dock->type==WM_CLIP && last_dock->auto_collapse) {
3684 collapsed = 0;
3687 } else {
3688 aicon->x_pos = x;
3689 aicon->y_pos = y;
3690 if (superfluous) {
3691 if (!aicon->running && !wPreferences.no_animations) {
3692 /* We need to deselect it, even if is deselected in
3693 * wDockDetach(), because else DoKaboom() will fail.
3695 if (aicon->icon->selected)
3696 wIconSelect(aicon->icon);
3697 DoKaboom(scr,aicon->icon->core->window, x, y);
3700 if (clip && clip->auto_raise_lower)
3701 wDockLower(clip);
3702 wDockDetach(dock, aicon);
3704 if (collapsed) {
3705 last_dock->collapsed = 1;
3706 wDockHideIcons(last_dock);
3707 collapsed = 0;
3709 if (superfluous) {
3710 if (ghost!=None)
3711 XFreePixmap(dpy, ghost);
3712 XSetWindowBackground(dpy, scr->dock_shadow, scr->white_pixel);
3714 #ifdef DEBUG
3715 puts("End icon move");
3716 #endif
3717 return;
3723 static int
3724 getClipButton(int px, int py)
3726 int pt = (CLIP_BUTTON_SIZE+2)*ICON_SIZE/64;
3728 if (px < 0 || py < 0 || px >= ICON_SIZE || py >= ICON_SIZE)
3729 return CLIP_IDLE;
3731 if (py <= pt-((int)ICON_SIZE-1-px))
3732 return CLIP_FORWARD;
3733 else if (px <= pt-((int)ICON_SIZE-1-py))
3734 return CLIP_REWIND;
3736 return CLIP_IDLE;
3740 static void
3741 handleClipChangeWorkspace(WScreen *scr, XEvent *event)
3743 XEvent ev;
3744 int done, direction, new_ws;
3745 int new_dir;
3746 WDock *clip = scr->clip_icon->dock;
3748 direction = getClipButton(event->xbutton.x, event->xbutton.y);
3750 clip->lclip_button_pushed = direction==CLIP_REWIND;
3751 clip->rclip_button_pushed = direction==CLIP_FORWARD;
3753 wClipIconPaint(scr->clip_icon);
3754 done = 0;
3755 while(!done) {
3756 WMMaskEvent(dpy, ExposureMask|ButtonMotionMask|ButtonReleaseMask
3757 |ButtonPressMask, &ev);
3758 switch (ev.type) {
3759 case Expose:
3760 WMHandleEvent(&ev);
3761 break;
3763 case MotionNotify:
3764 new_dir = getClipButton(ev.xmotion.x, ev.xmotion.y);
3765 if (new_dir != direction) {
3766 direction = new_dir;
3767 clip->lclip_button_pushed = direction==CLIP_REWIND;
3768 clip->rclip_button_pushed = direction==CLIP_FORWARD;
3769 wClipIconPaint(scr->clip_icon);
3771 break;
3773 case ButtonPress:
3774 break;
3776 case ButtonRelease:
3777 if (ev.xbutton.button == event->xbutton.button)
3778 done = 1;
3782 clip->lclip_button_pushed = 0;
3783 clip->rclip_button_pushed = 0;
3785 new_ws = wPreferences.ws_advance || (event->xbutton.state & ControlMask);
3787 if (direction == CLIP_FORWARD) {
3788 if (scr->current_workspace < scr->workspace_count-1)
3789 wWorkspaceChange(scr, scr->current_workspace+1);
3790 else if (new_ws && scr->current_workspace < MAX_WORKSPACES-1)
3791 wWorkspaceChange(scr, scr->current_workspace+1);
3792 else if (wPreferences.ws_cycle)
3793 wWorkspaceChange(scr, 0);
3795 else if (direction == CLIP_REWIND) {
3796 if (scr->current_workspace > 0)
3797 wWorkspaceChange(scr, scr->current_workspace-1);
3798 else if (scr->current_workspace==0 && wPreferences.ws_cycle)
3799 wWorkspaceChange(scr, scr->workspace_count-1);
3802 wClipIconPaint(scr->clip_icon);
3806 static void
3807 iconMouseDown(WObjDescriptor *desc, XEvent *event)
3809 WAppIcon *aicon = desc->parent;
3810 WDock *dock = aicon->dock;
3811 WScreen *scr = aicon->icon->core->screen_ptr;
3813 if (aicon->editing || WCHECK_STATE(WSTATE_MODAL))
3814 return;
3816 scr->last_dock = dock;
3818 if (dock->menu->flags.mapped)
3819 wMenuUnmap(dock->menu);
3821 if (IsDoubleClick(scr, event)) {
3822 /* double-click was not in the main clip icon */
3823 if (dock->type != WM_CLIP || aicon->xindex!=0 || aicon->yindex!=0
3824 || getClipButton(event->xbutton.x, event->xbutton.y)==CLIP_IDLE) {
3825 iconDblClick(desc, event);
3826 return;
3830 if (dock->type == WM_CLIP && scr->flags.clip_balloon_mapped) {
3831 XUnmapWindow(dpy, scr->clip_balloon);
3832 scr->flags.clip_balloon_mapped = 0;
3835 #ifdef DEBUG
3836 puts("handling dock");
3837 #endif
3838 if (event->xbutton.button == Button1) {
3839 if (event->xbutton.state & MOD_MASK)
3840 wDockLower(dock);
3841 else
3842 wDockRaise(dock);
3844 if ((event->xbutton.state & ShiftMask) && aicon!=scr->clip_icon &&
3845 dock->type!=WM_DOCK) {
3846 wIconSelect(aicon->icon);
3847 return;
3850 if (aicon->yindex==0 && aicon->xindex==0) {
3851 if (getClipButton(event->xbutton.x, event->xbutton.y)!=CLIP_IDLE
3852 && dock->type==WM_CLIP)
3853 handleClipChangeWorkspace(scr, event);
3854 else
3855 handleDockMove(dock, aicon, event);
3856 } else
3857 handleIconMove(dock, aicon, event);
3859 } else if (event->xbutton.button==Button2 && dock->type==WM_CLIP &&
3860 aicon->xindex==0 && aicon->yindex==0) {
3861 openClipWorkspaceMenu(scr, event->xbutton.x_root+2,
3862 event->xbutton.y_root+2);
3863 if (scr->clip_ws_menu) {
3864 WMenu *menu;
3865 menu = scr->clip_ws_menu;
3866 desc = &menu->menu->descriptor;
3868 event->xany.send_event = True;
3869 (*desc->handle_mousedown)(desc, event);
3871 } else if (event->xbutton.button == Button3) {
3872 openDockMenu(dock, aicon, event);
3877 static void
3878 showClipBalloon(WDock *dock, int workspace)
3880 int w, h;
3881 int x, y;
3882 WScreen *scr = dock->screen_ptr;
3883 char *text;
3884 Window stack[2];
3886 scr->flags.clip_balloon_mapped = 1;
3887 XMapWindow(dpy, scr->clip_balloon);
3889 text = scr->workspaces[workspace]->name;
3891 w = wTextWidth(scr->clip_title_font->font, text, strlen(text));
3893 h = scr->clip_title_font->height;
3894 XResizeWindow(dpy, scr->clip_balloon, w, h);
3896 x = dock->x_pos + CLIP_BUTTON_SIZE*ICON_SIZE/64;
3897 y = dock->y_pos + ICON_SIZE-scr->clip_title_font->height - 3;
3899 if (x+w > scr->scr_width) {
3900 x = scr->scr_width - w;
3901 if (dock->y_pos + ICON_SIZE + h > scr->scr_height)
3902 y = dock->y_pos - h - 1;
3903 else
3904 y = dock->y_pos + ICON_SIZE;
3905 XRaiseWindow(dpy, scr->clip_balloon);
3906 } else {
3907 stack[0] = scr->clip_icon->icon->core->window;
3908 stack[1] = scr->clip_balloon;
3909 XRestackWindows(dpy, stack, 2);
3911 XMoveWindow(dpy, scr->clip_balloon, x, y);
3912 XSetForeground(dpy, scr->clip_title_gc,
3913 scr->clip_title_pixel[CLIP_NORMAL]);
3914 XClearWindow(dpy, scr->clip_balloon);
3915 wDrawString(scr->clip_balloon, scr->clip_title_font, scr->clip_title_gc,
3916 0, scr->clip_title_font->y, text, strlen(text));
3920 static void
3921 clipEnterNotify(WObjDescriptor *desc, XEvent *event)
3923 WAppIcon *btn = (WAppIcon*)desc->parent;
3924 WDock *dock;
3926 assert(event->type==EnterNotify);
3928 if(desc->parent_type!=WCLASS_DOCK_ICON)
3929 return;
3931 dock = btn->dock;
3932 if (!dock || dock->type!=WM_CLIP)
3933 return;
3935 /* The auto raise/lower code */
3936 if (dock->auto_lower_magic) {
3937 WMDeleteTimerHandler(dock->auto_lower_magic);
3938 dock->auto_lower_magic = NULL;
3940 if (dock->auto_raise_lower && !dock->auto_raise_magic) {
3941 dock->auto_raise_magic = WMAddTimerHandler(AUTO_RAISE_DELAY,
3942 clipAutoRaise,
3943 (void *)dock);
3946 /* The auto expand/collapse code */
3947 if (dock->auto_collapse_magic) {
3948 WMDeleteTimerHandler(dock->auto_collapse_magic);
3949 dock->auto_collapse_magic = NULL;
3951 if (dock->auto_collapse && !dock->auto_expand_magic) {
3952 dock->auto_expand_magic = WMAddTimerHandler(AUTO_EXPAND_DELAY,
3953 clipAutoExpand,
3954 (void *)dock);
3957 if (btn->xindex == 0 && btn->yindex == 0)
3958 showClipBalloon(dock, dock->screen_ptr->current_workspace);
3959 else {
3960 if (dock->screen_ptr->flags.clip_balloon_mapped) {
3961 XUnmapWindow(dpy, dock->screen_ptr->clip_balloon);
3962 dock->screen_ptr->flags.clip_balloon_mapped = 0;
3968 static void
3969 clipLeave(WDock *dock)
3971 XEvent event;
3972 WObjDescriptor *desc = NULL;
3974 if (!dock || dock->type!=WM_CLIP)
3975 return;
3977 if (XCheckTypedEvent(dpy, EnterNotify, &event)!=False) {
3978 if (XFindContext(dpy, event.xcrossing.window, wWinContext,
3979 (XPointer *)&desc)!=XCNOENT
3980 && desc && desc->parent_type==WCLASS_DOCK_ICON
3981 && ((WAppIcon*)desc->parent)->dock
3982 && ((WAppIcon*)desc->parent)->dock->type==WM_CLIP) {
3983 /* We didn't left the Clip yet */
3984 XPutBackEvent(dpy, &event);
3985 return;
3988 XPutBackEvent(dpy, &event);
3989 } else {
3990 /* We entered a withdrawn window, so we're still in Clip */
3991 return;
3994 if (dock->auto_raise_magic) {
3995 WMDeleteTimerHandler(dock->auto_raise_magic);
3996 dock->auto_raise_magic = NULL;
3998 if (dock->auto_raise_lower && !dock->auto_lower_magic) {
3999 dock->auto_lower_magic = WMAddTimerHandler(AUTO_LOWER_DELAY,
4000 clipAutoLower,
4001 (void *)dock);
4004 if (dock->auto_expand_magic) {
4005 WMDeleteTimerHandler(dock->auto_expand_magic);
4006 dock->auto_expand_magic = NULL;
4008 if (dock->auto_collapse && !dock->auto_collapse_magic) {
4009 dock->auto_collapse_magic = WMAddTimerHandler(AUTO_COLLAPSE_DELAY,
4010 clipAutoCollapse,
4011 (void *)dock);
4016 static void
4017 clipLeaveNotify(WObjDescriptor *desc, XEvent *event)
4019 WAppIcon *btn = (WAppIcon*)desc->parent;
4021 assert(event->type==LeaveNotify);
4023 if(desc->parent_type!=WCLASS_DOCK_ICON)
4024 return;
4026 clipLeave(btn->dock);
4030 static void
4031 clipAutoCollapse(void *cdata)
4033 WDock *dock = (WDock *)cdata;
4035 if (dock->type!=WM_CLIP)
4036 return;
4038 if (dock->auto_collapse) {
4039 dock->collapsed = 1;
4040 wDockHideIcons(dock);
4042 dock->auto_collapse_magic = NULL;
4046 static void
4047 clipAutoExpand(void *cdata)
4049 WDock *dock = (WDock *)cdata;
4051 if (dock->type!=WM_CLIP)
4052 return;
4054 if (dock->auto_collapse) {
4055 dock->collapsed = 0;
4056 wDockShowIcons(dock);
4058 dock->auto_expand_magic = NULL;
4062 static void
4063 clipAutoLower(void *cdata)
4065 WDock *dock = (WDock *)cdata;
4067 if (dock->type!=WM_CLIP)
4068 return;
4070 if (dock->auto_raise_lower)
4071 wDockLower(dock);
4073 dock->auto_lower_magic = NULL;
4077 static void
4078 clipAutoRaise(void *cdata)
4080 WDock *dock = (WDock *)cdata;
4082 if (dock->type!=WM_CLIP)
4083 return;
4085 if (dock->auto_raise_lower)
4086 wDockRaise(dock);
4088 if (dock->screen_ptr->flags.clip_balloon_mapped) {
4089 showClipBalloon(dock, dock->screen_ptr->current_workspace);
4092 dock->auto_raise_magic = NULL;