Added ability to set omnipresent icons in Clip.
[wmaker-crm.git] / src / dock.c
blob90027205a313709e7f6b79973ac899359cc83a28
1 /* dock.c- built-in Dock module for WindowMaker
3 * Window Maker window manager
5 * Copyright (c) 1997, 1998 Alfredo K. Kojima
6 * Copyright (c) 1998, 1999 Dan Pascu
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
21 * USA.
25 #include "wconfig.h"
27 #include <X11/Xlib.h>
28 #include <X11/Xutil.h>
29 #include <stdlib.h>
30 #include <string.h>
31 #include <unistd.h>
32 #include <math.h>
33 #include <limits.h>
35 #ifndef PATH_MAX
36 #define PATH_MAX DEFAULT_PATH_MAX
37 #endif
39 #include "WindowMaker.h"
40 #include "wcore.h"
41 #include "window.h"
42 #include "icon.h"
43 #include "appicon.h"
44 #include "actions.h"
45 #include "stacking.h"
46 #include "dock.h"
47 #include "dialog.h"
48 #include "funcs.h"
49 #include "properties.h"
50 #include "menu.h"
51 #include "client.h"
52 #include "defaults.h"
53 #include "workspace.h"
54 #include "framewin.h"
55 #include "superfluous.h"
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, dOmnipresent;
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 dOmnipresent = PLMakeString("Omnipresent");
192 dDock = PLMakeString("Dock");
193 dClip = PLMakeString("Clip");
198 static void
199 renameCallback(WMenu *menu, WMenuEntry *entry)
201 WDock *dock = entry->clientdata;
202 char buffer[128];
203 int wspace;
204 char *name;
206 assert(entry->clientdata!=NULL);
208 wspace = dock->screen_ptr->current_workspace;
210 name = wstrdup(dock->screen_ptr->workspaces[wspace]->name);
212 sprintf(buffer, _("Type the name for workspace %i:"), wspace+1);
213 if (wInputDialog(dock->screen_ptr, _("Rename Workspace"), buffer,
214 &name)) {
215 wWorkspaceRename(dock->screen_ptr, wspace, name);
217 if (name) {
218 free(name);
223 static void
224 toggleLoweredCallback(WMenu *menu, WMenuEntry *entry)
226 assert(entry->clientdata!=NULL);
228 toggleLowered(entry->clientdata);
230 entry->flags.indicator_on = !((WDock*)entry->clientdata)->lowered;
232 wMenuPaint(menu);
237 static void
238 killCallback(WMenu *menu, WMenuEntry *entry)
240 WAppIcon *icon;
241 #ifdef REDUCE_APPICONS
242 WAppIconAppList *tapplist;
244 extern Atom _XA_WM_DELETE_WINDOW;
245 #else
246 char *buffer;
247 #endif
249 if (!WCHECK_STATE(WSTATE_NORMAL))
250 return;
252 assert(entry->clientdata!=NULL);
254 icon = (WAppIcon*)entry->clientdata;
256 icon->editing = 1;
258 WCHANGE_STATE(WSTATE_MODAL);
260 #ifdef REDUCE_APPICONS
261 /* Send a delete message to the main window of each application
262 * bound to this docked appicon. - cls
264 tapplist = icon->applist;
265 while (tapplist != NULL) {
266 if (tapplist->wapp->main_window_desc != NULL) {
267 if (tapplist->wapp->main_window_desc->protocols.DELETE_WINDOW) {
268 wClientSendProtocol(tapplist->wapp->main_window_desc,
269 _XA_WM_DELETE_WINDOW, CurrentTime);
270 } else {
271 wClientKill(tapplist->wapp->main_window_desc);
274 tapplist = tapplist->next;
276 #else
277 buffer = wstrappend(icon->wm_class,
278 _(" will be forcibly closed.\n"
279 "Any unsaved changes will be lost.\n"
280 "Please confirm."));
282 if (wPreferences.dont_confirm_kill
283 || wMessageDialog(menu->frame->screen_ptr, _("Kill Application"),
284 buffer, _("Yes"), _("No"), NULL)==WAPRDefault) {
285 if (icon->icon && icon->icon->owner) {
286 wClientKill(icon->icon->owner);
289 #endif /* !REDUCE_APPICONS */
291 icon->editing = 0;
293 WCHANGE_STATE(WSTATE_NORMAL);}
296 static LinkedList*
297 getSelected(WDock *dock)
299 LinkedList *ret=NULL;
300 WAppIcon *btn;
301 int i;
303 for (i=1; i<dock->max_icons; i++) {
304 btn = dock->icon_array[i];
305 if (btn && btn->icon->selected) {
306 ret = list_cons(btn, ret);
310 return ret;
314 static void
315 paintClipButtons(WAppIcon *clipIcon, Bool lpushed, Bool rpushed)
317 Window win = clipIcon->icon->core->window;
318 WScreen *scr = clipIcon->icon->core->screen_ptr;
319 XPoint p[4];
320 int pt = CLIP_BUTTON_SIZE*ICON_SIZE/64;
321 int tp = ICON_SIZE - pt;
322 int as = pt - 15; /* 15 = 5+5+5 */
323 GC gc = scr->clip_title_gc;
324 #ifdef GRADIENT_CLIP_ARROW
325 Bool collapsed = clipIcon->dock->collapsed;
326 #endif
328 if (rpushed) {
329 p[0].x = tp+1;
330 p[0].y = 1;
331 p[1].x = ICON_SIZE-2;
332 p[1].y = 1;
333 p[2].x = ICON_SIZE-2;
334 p[2].y = pt-1;
335 } else if (lpushed) {
336 p[0].x = 1;
337 p[0].y = tp;
338 p[1].x = pt;
339 p[1].y = ICON_SIZE-2;
340 p[2].x = 1;
341 p[2].y = ICON_SIZE-2;
343 if (lpushed || rpushed) {
344 XSetForeground(dpy, scr->draw_gc, scr->white_pixel);
345 XFillPolygon(dpy, win, scr->draw_gc, p, 3, Convex, CoordModeOrigin);
346 XSetForeground(dpy, scr->draw_gc, scr->black_pixel);
348 #ifdef GRADIENT_CLIP_ARROW
349 if (!collapsed) {
350 XSetFillStyle(dpy, scr->copy_gc, FillTiled);
351 XSetTile(dpy, scr->copy_gc, scr->clip_arrow_gradient);
352 XSetClipMask(dpy, scr->copy_gc, None);
353 gc = scr->copy_gc;
355 #endif /* GRADIENT_CLIP_ARROW */
357 /* top right arrow */
358 p[0].x = p[3].x = ICON_SIZE-5-as;
359 p[0].y = p[3].y = 5;
360 p[1].x = ICON_SIZE-6;
361 p[1].y = 5;
362 p[2].x = ICON_SIZE-6;
363 p[2].y = 4+as;
364 if (rpushed) {
365 XFillPolygon(dpy, win, scr->draw_gc, p, 3, Convex, CoordModeOrigin);
366 XDrawLines(dpy, win, scr->draw_gc, p, 4, CoordModeOrigin);
367 } else {
368 #ifdef GRADIENT_CLIP_ARROW
369 if (!collapsed)
370 XSetTSOrigin(dpy, gc, ICON_SIZE-6-as, 5);
371 #endif
372 XFillPolygon(dpy, win, gc, p,3,Convex,CoordModeOrigin);
373 XDrawLines(dpy, win, gc, p,4,CoordModeOrigin);
376 /* bottom left arrow */
377 p[0].x = p[3].x = 5;
378 p[0].y = p[3].y = ICON_SIZE-5-as;
379 p[1].x = 5;
380 p[1].y = ICON_SIZE-6;
381 p[2].x = 4+as;
382 p[2].y = ICON_SIZE-6;
383 if (lpushed) {
384 XFillPolygon(dpy, win, scr->draw_gc, p, 3, Convex, CoordModeOrigin);
385 XDrawLines(dpy, win, scr->draw_gc, p, 4, CoordModeOrigin);
386 } else {
387 #ifdef GRADIENT_CLIP_ARROW
388 if (!collapsed)
389 XSetTSOrigin(dpy, gc, 5, ICON_SIZE-6-as);
390 #endif
391 XFillPolygon(dpy, win, gc, p,3,Convex,CoordModeOrigin);
392 XDrawLines(dpy, win, gc, p,4,CoordModeOrigin);
394 #ifdef GRADIENT_CLIP_ARROW
395 if (!collapsed)
396 XSetFillStyle(dpy, scr->copy_gc, FillSolid);
397 #endif
401 RImage*
402 wClipMakeTile(WScreen *scr, RImage *normalTile)
404 RImage *tile = RCloneImage(normalTile);
405 RColor black;
406 RColor dark;
407 RColor light;
408 int pt, tp;
409 int as;
411 pt = CLIP_BUTTON_SIZE*wPreferences.icon_size/64;
412 tp = wPreferences.icon_size-1 - pt;
413 as = pt - 15;
415 black.alpha = 255;
416 black.red = black.green = black.blue = 0;
418 dark.alpha = 0;
419 dark.red = dark.green = dark.blue = 60;
421 light.alpha = 0;
422 light.red = light.green = light.blue = 80;
425 /* top right */
426 ROperateLine(tile, RSubtractOperation, tp, 0, wPreferences.icon_size-2,
427 pt-1, &dark);
428 RDrawLine(tile, tp-1, 0, wPreferences.icon_size-1, pt+1, &black);
429 ROperateLine(tile, RAddOperation, tp, 2, wPreferences.icon_size-3,
430 pt, &light);
432 /* arrow bevel */
433 ROperateLine(tile, RSubtractOperation, ICON_SIZE - 7 - as, 4,
434 ICON_SIZE - 5, 4, &dark);
435 ROperateLine(tile, RSubtractOperation, ICON_SIZE - 6 - as, 5,
436 ICON_SIZE - 5, 6 + as, &dark);
437 ROperateLine(tile, RAddOperation, ICON_SIZE - 5, 4, ICON_SIZE - 5, 6 + as,
438 &light);
440 /* bottom left */
441 ROperateLine(tile, RAddOperation, 2, tp+2, pt-2,
442 wPreferences.icon_size-3, &dark);
443 RDrawLine(tile, 0, tp-1, pt+1, wPreferences.icon_size-1, &black);
444 ROperateLine(tile, RSubtractOperation, 0, tp-2, pt+1,
445 wPreferences.icon_size-2, &light);
447 /* arrow bevel */
448 ROperateLine(tile, RSubtractOperation, 4, ICON_SIZE - 7 - as, 4,
449 ICON_SIZE - 5, &dark);
450 ROperateLine(tile, RSubtractOperation, 5, ICON_SIZE - 6 - as,
451 6 + as, ICON_SIZE - 5, &dark);
452 ROperateLine(tile, RAddOperation, 4, ICON_SIZE - 5, 6 + as, ICON_SIZE - 5,
453 &light);
455 return tile;
459 static void
460 removeIconsCallback(WMenu *menu, WMenuEntry *entry)
462 WAppIcon *clickedIcon = (WAppIcon*)entry->clientdata;
463 WDock *dock;
464 WAppIcon *aicon;
465 LinkedList *selectedIcons;
466 int keepit;
468 assert(clickedIcon!=NULL);
470 dock = clickedIcon->dock;
472 selectedIcons = getSelected(dock);
474 if (selectedIcons) {
475 if (wMessageDialog(dock->screen_ptr, _("Workspace Clip"),
476 _("All selected icons will be removed!"),
477 _("OK"), _("Cancel"), NULL)!=WAPRDefault) {
478 return;
480 } else {
481 if (clickedIcon->xindex==0 && clickedIcon->yindex==0)
482 return;
483 selectedIcons = list_cons(clickedIcon, NULL);
486 while (selectedIcons) {
487 aicon = selectedIcons->head;
488 keepit = aicon->running && wApplicationOf(aicon->main_window);
489 wDockDetach(dock, aicon);
490 if (keepit) {
491 PlaceIcon(dock->screen_ptr, &aicon->x_pos, &aicon->y_pos);
492 XMoveWindow(dpy, aicon->icon->core->window,
493 aicon->x_pos, aicon->y_pos);
494 if (!dock->mapped || dock->collapsed)
495 XMapWindow(dpy, aicon->icon->core->window);
497 list_remove_head(&selectedIcons);
500 if (wPreferences.auto_arrange_icons)
501 wArrangeIcons(dock->screen_ptr, True);
505 static void
506 keepIconsCallback(WMenu *menu, WMenuEntry *entry)
508 WAppIcon *clickedIcon = (WAppIcon*)entry->clientdata;
509 WDock *dock;
510 WAppIcon *aicon;
511 LinkedList *selectedIcons;
513 assert(clickedIcon!=NULL);
514 dock = clickedIcon->dock;
516 selectedIcons = getSelected(dock);
518 if (!selectedIcons && clickedIcon!=dock->screen_ptr->clip_icon) {
519 char *command = NULL;
521 if (!clickedIcon->command && !clickedIcon->editing) {
522 clickedIcon->editing = 1;
523 if (wInputDialog(dock->screen_ptr, _("Keep Icon"),
524 _("Type the command used to launch the application"),
525 &command)) {
526 if (command && (command[0]==0 ||
527 (command[0]=='-' && command[1]==0))) {
528 free(command);
529 command = NULL;
531 clickedIcon->command = command;
532 clickedIcon->editing = 0;
533 } else {
534 clickedIcon->editing = 0;
535 if (command)
536 free(command);
537 return;
541 selectedIcons = list_cons(clickedIcon, NULL);
544 while (selectedIcons) {
545 aicon = selectedIcons->head;
546 if (aicon->icon->selected)
547 wIconSelect(aicon->icon);
548 if (aicon && aicon->attracted && aicon->command) {
549 aicon->attracted = 0;
550 if (aicon->icon->shadowed) {
551 aicon->icon->shadowed = 0;
552 aicon->icon->force_paint = 1;
553 wAppIconPaint(aicon);
556 list_remove_head(&selectedIcons);
563 static void
564 toggleAutoAttractCallback(WMenu *menu, WMenuEntry *entry)
566 WDock *dock = (WDock*)entry->clientdata;
568 assert(entry->clientdata!=NULL);
570 dock->attract_icons = !dock->attract_icons;
571 /*if (!dock->attract_icons)
572 dock->keep_attracted = 0;*/
574 entry->flags.indicator_on = dock->attract_icons;
576 wMenuPaint(menu);
580 static void
581 toggleKeepCallback(WMenu *menu, WMenuEntry *entry)
583 WDock *dock = (WDock*)entry->clientdata;
584 WAppIcon *btn;
585 int i;
587 assert(entry->clientdata!=NULL);
589 dock->keep_attracted = !dock->keep_attracted;
591 if (dock->keep_attracted) {
592 for (i=0; i< dock->max_icons; i++) {
593 btn = dock->icon_array[i];
594 if (btn && btn->attracted && btn->command) {
595 btn->attracted = 0;
596 if (btn->icon->shadowed) {
597 btn->icon->shadowed = 0;
598 btn->icon->force_paint = 1;
599 wAppIconPaint(btn);
605 entry->flags.indicator_on = dock->keep_attracted;
607 wMenuPaint(menu);
611 static void
612 selectCallback(WMenu *menu, WMenuEntry *entry)
614 WAppIcon *icon = (WAppIcon*)entry->clientdata;
616 assert(icon!=NULL);
618 wIconSelect(icon->icon);
620 wMenuPaint(menu);
624 static void
625 colectIconsCallback(WMenu *menu, WMenuEntry *entry)
627 WAppIcon *clickedIcon = (WAppIcon*)entry->clientdata;
628 WDock *clip;
629 WAppIcon *aicon;
630 int x, y, x_pos, y_pos;
632 assert(entry->clientdata!=NULL);
633 clip = clickedIcon->dock;
635 aicon = clip->screen_ptr->app_icon_list;
637 while (aicon) {
638 if (!aicon->docked && wDockFindFreeSlot(clip, &x, &y)) {
639 x_pos = clip->x_pos + x*ICON_SIZE;
640 y_pos = clip->y_pos + y*ICON_SIZE;
641 if (aicon->x_pos != x_pos || aicon->y_pos != y_pos) {
642 #ifdef ANIMATIONS
643 if (wPreferences.no_animations) {
644 XMoveWindow(dpy, aicon->icon->core->window, x_pos, y_pos);
645 } else {
646 SlideWindow(aicon->icon->core->window,
647 aicon->x_pos, aicon->y_pos, x_pos, y_pos);
649 #else
650 XMoveWindow(dpy, aicon->icon->core->window, x_pos, y_pos);
651 #endif /* ANIMATIONS */
653 aicon->attracted = 1;
654 if (!clip->keep_attracted && !aicon->icon->shadowed) {
655 aicon->icon->shadowed = 1;
656 aicon->icon->force_paint = 1;
657 /* We don't do an wAppIconPaint() here because it's in
658 * wDockAttachIcon(). -Dan
661 wDockAttachIcon(clip, aicon, x, y);
662 if (clip->collapsed || !clip->mapped)
663 XUnmapWindow(dpy, aicon->icon->core->window);
665 aicon = aicon->next;
670 static void
671 selectIconsCallback(WMenu *menu, WMenuEntry *entry)
673 WAppIcon *clickedIcon = (WAppIcon*)entry->clientdata;
674 WDock *dock;
675 LinkedList *selectedIcons;
676 WAppIcon *btn;
677 int i;
679 assert(clickedIcon!=NULL);
680 dock = clickedIcon->dock;
682 selectedIcons = getSelected(dock);
684 if (!selectedIcons) {
685 for (i=1; i<dock->max_icons; i++) {
686 btn = dock->icon_array[i];
687 if (btn && !btn->icon->selected) {
688 wIconSelect(btn->icon);
691 } else {
692 while(selectedIcons) {
693 btn = selectedIcons->head;
694 wIconSelect(btn->icon);
695 list_remove_head(&selectedIcons);
699 wMenuPaint(menu);
703 static void
704 toggleCollapsedCallback(WMenu *menu, WMenuEntry *entry)
706 assert(entry->clientdata!=NULL);
708 toggleCollapsed(entry->clientdata);
710 entry->flags.indicator_on = ((WDock*)entry->clientdata)->collapsed;
712 wMenuPaint(menu);
716 static void
717 toggleAutoCollapseCallback(WMenu *menu, WMenuEntry *entry)
719 WDock *dock;
720 assert(entry->clientdata!=NULL);
722 dock = (WDock*) entry->clientdata;
724 dock->auto_collapse = !dock->auto_collapse;
726 entry->flags.indicator_on = ((WDock*)entry->clientdata)->auto_collapse;
728 wMenuPaint(menu);
732 static void
733 toggleAutoRaiseLowerCallback(WMenu *menu, WMenuEntry *entry)
735 WDock *dock;
736 assert(entry->clientdata!=NULL);
738 dock = (WDock*) entry->clientdata;
740 dock->auto_raise_lower = !dock->auto_raise_lower;
742 entry->flags.indicator_on = ((WDock*)entry->clientdata)->auto_raise_lower;
744 wMenuPaint(menu);
748 static void
749 launchCallback(WMenu *menu, WMenuEntry *entry)
751 WAppIcon *btn = (WAppIcon*)entry->clientdata;
753 launchDockedApplication(btn);
757 static void
758 settingsCallback(WMenu *menu, WMenuEntry *entry)
760 WAppIcon *btn = (WAppIcon*)entry->clientdata;
762 if (btn->editing)
763 return;
764 ShowDockAppSettingsPanel(btn);
768 static void
769 hideCallback(WMenu *menu, WMenuEntry *entry)
771 WApplication *wapp;
772 WAppIcon *btn = (WAppIcon*)entry->clientdata;
774 wapp = wApplicationOf(btn->icon->owner->main_window);
776 if (wapp->flags.hidden) {
777 wWorkspaceChange(btn->icon->core->screen_ptr,wapp->last_workspace);
778 wUnhideApplication(wapp, False, False);
779 } else {
780 wHideApplication(wapp);
785 static void
786 unhideHereCallback(WMenu *menu, WMenuEntry *entry)
788 WApplication *wapp;
789 WAppIcon *btn = (WAppIcon*)entry->clientdata;
791 wapp = wApplicationOf(btn->icon->owner->main_window);
793 wUnhideApplication(wapp, False, True);
797 WAppIcon*
798 mainIconCreate(WScreen *scr, int type)
800 WAppIcon *btn;
801 int x_pos;
803 if (type == WM_CLIP) {
804 if (scr->clip_icon)
805 return scr->clip_icon;
806 btn = wAppIconCreateForDock(scr, NULL, "Logo", "WMClip", TILE_CLIP);
807 btn->icon->core->descriptor.handle_expose = clipIconExpose;
808 btn->icon->core->descriptor.handle_enternotify = clipEnterNotify;
809 btn->icon->core->descriptor.handle_leavenotify = clipLeaveNotify;
810 /*x_pos = scr->scr_width - ICON_SIZE*2 - DOCK_EXTRA_SPACE;*/
811 x_pos = 0;
812 } else {
813 btn = wAppIconCreateForDock(scr, NULL, "Logo", "WMDock", TILE_NORMAL);
814 x_pos = scr->scr_width - ICON_SIZE - DOCK_EXTRA_SPACE;
817 btn->xindex = 0;
818 btn->yindex = 0;
820 btn->icon->core->descriptor.handle_mousedown = iconMouseDown;
821 btn->icon->core->descriptor.parent_type = WCLASS_DOCK_ICON;
822 btn->icon->core->descriptor.parent = btn;
823 /*ChangeStackingLevel(btn->icon->core, WMDockLevel);*/
824 XMapWindow(dpy, btn->icon->core->window);
825 btn->x_pos = x_pos;
826 btn->y_pos = 0;
827 btn->docked = 1;
828 if (type == WM_CLIP)
829 scr->clip_icon = btn;
831 return btn;
835 static void
836 switchWSCommand(WMenu *menu, WMenuEntry *entry)
838 WAppIcon *btn, *icon = (WAppIcon*) entry->clientdata;
839 WScreen *scr = icon->icon->core->screen_ptr;
840 WDock *src, *dest;
841 LinkedList *selectedIcons;
842 int x, y;
844 if (entry->order == scr->current_workspace)
845 return;
846 src = icon->dock;
847 dest = scr->workspaces[entry->order]->clip;
849 selectedIcons = getSelected(src);
851 if (selectedIcons) {
852 while(selectedIcons) {
853 btn = selectedIcons->head;
854 if (wDockFindFreeSlot(dest, &x, &y)) {
855 moveIconBetweenDocks(src, dest, btn, x, y);
856 XUnmapWindow(dpy, btn->icon->core->window);
858 list_remove_head(&selectedIcons);
860 } else if (icon != scr->clip_icon) {
861 if (wDockFindFreeSlot(dest, &x, &y)) {
862 moveIconBetweenDocks(src, dest, icon, x, y);
863 XUnmapWindow(dpy, icon->icon->core->window);
870 static void
871 launchDockedApplication(WAppIcon *btn)
873 WScreen *scr = btn->icon->core->screen_ptr;
875 if (!btn->launching && btn->command!=NULL) {
876 if (!btn->forced_dock) {
877 btn->relaunching = btn->running;
878 btn->running = 1;
880 if (btn->wm_instance || btn->wm_class) {
881 WWindowAttributes attr;
882 memset(&attr, 0, sizeof(WWindowAttributes));
883 wDefaultFillAttributes(scr, btn->wm_instance, btn->wm_class,
884 &attr, NULL, True);
886 if (!attr.no_appicon && !btn->buggy_app)
887 btn->launching = 1;
888 else
889 btn->running = 0;
891 btn->drop_launch = 0;
892 scr->last_dock = btn->dock;
893 btn->pid = execCommand(btn, btn->command, NULL);
894 if (btn->pid>0) {
895 if (btn->buggy_app) {
896 /* give feedback that the app was launched */
897 btn->launching = 1;
898 dockIconPaint(btn);
899 btn->launching = 0;
900 WMAddTimerHandler(200, (WMCallback*)dockIconPaint, btn);
901 } else {
902 dockIconPaint(btn);
904 } else {
905 wwarning(_("could not launch application %s\n"), btn->command);
906 btn->launching = 0;
907 if (!btn->relaunching)
908 btn->running = 0;
915 static void
916 updateWorkspaceMenu(WMenu *menu, WAppIcon *icon)
918 WScreen *scr = menu->frame->screen_ptr;
919 char title[MAX_WORKSPACENAME_WIDTH+1];
920 int i;
922 if (!menu || !icon)
923 return;
925 for (i=0; i<scr->workspace_count; i++) {
926 if (i < menu->entry_no) {
927 if (strcmp(menu->entries[i]->text,scr->workspaces[i]->name)!=0) {
928 free(menu->entries[i]->text);
929 strcpy(title, scr->workspaces[i]->name);
930 menu->entries[i]->text = wstrdup(title);
931 menu->flags.realized = 0;
933 menu->entries[i]->clientdata = (void*)icon;
934 } else {
935 strcpy(title, scr->workspaces[i]->name);
937 wMenuAddCallback(menu, title, switchWSCommand, (void*)icon);
939 menu->flags.realized = 0;
941 if (i == scr->current_workspace) {
942 wMenuSetEnabled(menu, i, False);
943 } else {
944 wMenuSetEnabled(menu, i, True);
948 if (!menu->flags.realized)
949 wMenuRealize(menu);
953 static WMenu*
954 makeWorkspaceMenu(WScreen *scr)
956 WMenu *menu;
958 menu = wMenuCreate(scr, NULL, False);
959 if (!menu)
960 wwarning(_("could not create workspace submenu for Clip menu"));
962 wMenuAddCallback(menu, "", switchWSCommand, (void*)scr->clip_icon);
964 menu->flags.realized = 0;
965 wMenuRealize(menu);
967 return menu;
971 static void
972 updateClipOptionsMenu(WMenu *menu, WDock *dock)
974 WMenuEntry *entry;
975 int index = 0;
977 if (!menu || !dock)
978 return;
980 /* keep on top */
981 entry = menu->entries[index];
982 entry->flags.indicator_on = !dock->lowered;
983 entry->clientdata = dock;
985 /* collapsed */
986 entry = menu->entries[++index];
987 entry->flags.indicator_on = dock->collapsed;
988 entry->clientdata = dock;
990 /* auto-collapse */
991 entry = menu->entries[++index];
992 entry->flags.indicator_on = dock->auto_collapse;
993 entry->clientdata = dock;
995 /* auto-raise/lower */
996 entry = menu->entries[++index];
997 entry->flags.indicator_on = dock->auto_raise_lower;
998 entry->clientdata = dock;
1000 /* attract icons */
1001 entry = menu->entries[++index];
1002 entry->flags.indicator_on = dock->attract_icons;
1003 entry->clientdata = dock;
1005 /* keep attracted icons */
1006 entry = menu->entries[++index];
1007 entry->flags.indicator_on = dock->keep_attracted;
1008 entry->clientdata = dock;
1010 menu->flags.realized = 0;
1011 wMenuRealize(menu);
1015 static WMenu*
1016 makeClipOptionsMenu(WScreen *scr)
1018 WMenu *menu;
1019 WMenuEntry *entry;
1021 menu = wMenuCreate(scr, NULL, False);
1022 if (!menu) {
1023 wwarning(_("could not create options submenu for Clip menu"));
1024 return NULL;
1027 entry = wMenuAddCallback(menu, _("Keep on Top"),
1028 toggleLoweredCallback, NULL);
1029 entry->flags.indicator = 1;
1030 entry->flags.indicator_on = 1;
1031 entry->flags.indicator_type = MI_CHECK;
1033 entry = wMenuAddCallback(menu, _("Collapsed"),
1034 toggleCollapsedCallback, NULL);
1035 entry->flags.indicator = 1;
1036 entry->flags.indicator_on = 1;
1037 entry->flags.indicator_type = MI_CHECK;
1039 entry = wMenuAddCallback(menu, _("AutoCollapse"),
1040 toggleAutoCollapseCallback, NULL);
1041 entry->flags.indicator = 1;
1042 entry->flags.indicator_on = 1;
1043 entry->flags.indicator_type = MI_CHECK;
1045 entry = wMenuAddCallback(menu, _("AutoRaiseLower"),
1046 toggleAutoRaiseLowerCallback, NULL);
1047 entry->flags.indicator = 1;
1048 entry->flags.indicator_on = 1;
1049 entry->flags.indicator_type = MI_CHECK;
1051 entry = wMenuAddCallback(menu, _("AutoAttract Icons"),
1052 toggleAutoAttractCallback, NULL);
1053 entry->flags.indicator = 1;
1054 entry->flags.indicator_on = 1;
1055 entry->flags.indicator_type = MI_CHECK;
1057 entry = wMenuAddCallback(menu, _("Keep Attracted Icons"),
1058 toggleKeepCallback, NULL);
1059 entry->flags.indicator = 1;
1060 entry->flags.indicator_on = 1;
1061 entry->flags.indicator_type = MI_CHECK;
1063 menu->flags.realized = 0;
1064 wMenuRealize(menu);
1066 return menu;
1070 static WMenu*
1071 dockMenuCreate(WScreen *scr, int type)
1073 WMenu *menu;
1074 WMenuEntry *entry;
1076 if (type == WM_CLIP && scr->clip_menu)
1077 return scr->clip_menu;
1079 menu = wMenuCreate(scr, NULL, False);
1080 if (type != WM_CLIP) {
1081 entry = wMenuAddCallback(menu, _("Keep on top"),
1082 toggleLoweredCallback, NULL);
1083 entry->flags.indicator = 1;
1084 entry->flags.indicator_on = 1;
1085 entry->flags.indicator_type = MI_CHECK;
1086 } else {
1087 entry = wMenuAddCallback(menu, _("Clip Options"), NULL, NULL);
1088 scr->clip_options = makeClipOptionsMenu(scr);
1089 if (scr->clip_options)
1090 wMenuEntrySetCascade(menu, entry, scr->clip_options);
1092 wMenuAddCallback(menu, _("Rename Workspace"), renameCallback, NULL);
1094 wMenuAddCallback(menu, _("(Un)Select Icon"), selectCallback, NULL);
1096 wMenuAddCallback(menu, _("(Un)Select All Icons"), selectIconsCallback,
1097 NULL);
1099 wMenuAddCallback(menu, _("Keep Icon(s)"), keepIconsCallback, NULL);
1101 entry = wMenuAddCallback(menu, _("Move Icon(s) To"), NULL, NULL);
1102 scr->clip_submenu = makeWorkspaceMenu(scr);
1103 if (scr->clip_submenu)
1104 wMenuEntrySetCascade(menu, entry, scr->clip_submenu);
1106 wMenuAddCallback(menu, _("Remove Icon(s)"), removeIconsCallback, NULL);
1108 wMenuAddCallback(menu, _("Attract Icons"), colectIconsCallback, NULL);
1111 wMenuAddCallback(menu, _("Launch"), launchCallback, NULL);
1113 wMenuAddCallback(menu, _("Unhide Here"), unhideHereCallback, NULL);
1115 entry = wMenuAddCallback(menu, _("Hide"), hideCallback, NULL);
1116 free(entry->text);
1117 entry->text = _("Hide");
1119 wMenuAddCallback(menu, _("Settings..."), settingsCallback, NULL);
1121 wMenuAddCallback(menu, _("Kill"), killCallback, NULL);
1123 if (type == WM_CLIP)
1124 scr->clip_menu = menu;
1126 return menu;
1130 WDock*
1131 wDockCreate(WScreen *scr, int type)
1133 WDock *dock;
1134 WAppIcon *btn;
1135 int icon_count;
1137 make_keys();
1139 dock = wmalloc(sizeof(WDock));
1140 memset(dock, 0, sizeof(WDock));
1142 if (type == WM_CLIP)
1143 icon_count = CLIP_MAX_ICONS;
1144 else
1145 icon_count = scr->scr_height/wPreferences.icon_size;
1147 dock->icon_array = wmalloc(sizeof(WAppIcon*)*icon_count);
1148 memset(dock->icon_array, 0, sizeof(WAppIcon*)*icon_count);
1150 dock->max_icons = icon_count;
1152 btn = mainIconCreate(scr, type);
1154 btn->dock = dock;
1156 dock->x_pos = btn->x_pos;
1157 dock->y_pos = btn->y_pos;
1158 dock->screen_ptr = scr;
1159 dock->type = type;
1160 dock->icon_count = 1;
1161 dock->on_right_side = 1;
1162 dock->collapsed = 0;
1163 dock->auto_collapse = 0;
1164 dock->auto_collapse_magic = NULL;
1165 dock->auto_raise_lower = 0;
1166 dock->auto_lower_magic = NULL;
1167 dock->auto_raise_magic = NULL;
1168 dock->attract_icons = 0;
1169 dock->keep_attracted = 0;
1170 dock->lowered = 1;
1171 dock->icon_array[0] = btn;
1172 wRaiseFrame(btn->icon->core);
1173 XMoveWindow(dpy, btn->icon->core->window, btn->x_pos, btn->y_pos);
1175 /* create dock menu */
1176 dock->menu = dockMenuCreate(scr, type);
1178 return dock;
1182 void
1183 wDockDestroy(WDock *dock)
1185 int i;
1186 WAppIcon *aicon;
1188 for (i=(dock->type == WM_CLIP) ? 1 : 0; i<dock->max_icons; i++) {
1189 aicon = dock->icon_array[i];
1190 if (aicon) {
1191 int keepit = aicon->running && wApplicationOf(aicon->main_window);
1192 wDockDetach(dock, aicon);
1193 if (keepit) {
1194 PlaceIcon(dock->screen_ptr, &aicon->x_pos, &aicon->y_pos);
1195 XMoveWindow(dpy, aicon->icon->core->window,
1196 aicon->x_pos, aicon->y_pos);
1197 if (!dock->mapped || dock->collapsed)
1198 XMapWindow(dpy, aicon->icon->core->window);
1202 if (wPreferences.auto_arrange_icons)
1203 wArrangeIcons(dock->screen_ptr, True);
1204 free(dock->icon_array);
1205 if (dock->menu && dock->type!=WM_CLIP)
1206 wMenuDestroy(dock->menu, True);
1207 free(dock);
1211 void
1212 wClipIconPaint(WAppIcon *aicon)
1214 WScreen *scr = aicon->icon->core->screen_ptr;
1215 WWorkspace *workspace = scr->workspaces[scr->current_workspace];
1216 GC gc;
1217 Window win = aicon->icon->core->window;
1218 int length, nlength;
1219 char *ws_name, ws_number[10];
1220 int ty, tx;
1222 wIconPaint(aicon->icon);
1224 length = strlen(workspace->name);
1225 ws_name = malloc(length + 1);
1226 sprintf(ws_name, "%s", workspace->name);
1227 sprintf(ws_number, "%i", scr->current_workspace + 1);
1228 nlength = strlen(ws_number);
1230 gc = scr->clip_title_gc;
1232 if (!workspace->clip->collapsed)
1233 XSetForeground(dpy, gc, scr->clip_title_pixel[CLIP_NORMAL]);
1234 else
1235 XSetForeground(dpy, gc, scr->clip_title_pixel[CLIP_COLLAPSED]);
1237 ty = ICON_SIZE - scr->clip_title_font->height - 3;
1239 tx = CLIP_BUTTON_SIZE*ICON_SIZE/64;
1241 wDrawString(win, scr->clip_title_font, gc, tx,
1242 ty + scr->clip_title_font->y, ws_name, length);
1244 tx = (ICON_SIZE/2 - wTextWidth(scr->clip_title_font->font, ws_number, nlength))/2;
1246 wDrawString(win, scr->clip_title_font, gc, tx,
1247 scr->clip_title_font->y + 2, ws_number, nlength);
1249 free(ws_name);
1251 if (aicon->launching) {
1252 XFillRectangle(dpy, aicon->icon->core->window, scr->stipple_gc,
1253 0, 0, wPreferences.icon_size, wPreferences.icon_size);
1255 paintClipButtons(aicon, aicon->dock->lclip_button_pushed,
1256 aicon->dock->rclip_button_pushed);
1260 static void
1261 clipIconExpose(WObjDescriptor *desc, XEvent *event)
1263 wClipIconPaint(desc->parent);
1267 static void
1268 dockIconPaint(WAppIcon *btn)
1270 if (btn == btn->icon->core->screen_ptr->clip_icon)
1271 wClipIconPaint(btn);
1272 else
1273 wAppIconPaint(btn);
1277 static proplist_t
1278 make_icon_state(WAppIcon *btn)
1280 proplist_t node = NULL;
1281 proplist_t command, autolaunch, name, forced, host, position, buggy;
1282 proplist_t omnipresent;
1283 char *tmp;
1284 char buffer[64];
1286 if (btn) {
1287 if (!btn->command)
1288 command = PLMakeString("-");
1289 else
1290 command = PLMakeString(btn->command);
1292 autolaunch = btn->auto_launch ? dYes : dNo;
1294 tmp = EscapeWM_CLASS(btn->wm_instance, btn->wm_class);
1296 name = PLMakeString(tmp);
1298 free(tmp);
1300 forced = btn->forced_dock ? dYes : dNo;
1302 buggy = btn->buggy_app ? dYes : dNo;
1304 if (btn == btn->icon->core->screen_ptr->clip_icon)
1305 sprintf(buffer, "%i,%i", btn->x_pos, btn->y_pos);
1306 else
1307 sprintf(buffer, "%hi,%hi", btn->xindex, btn->yindex);
1308 position = PLMakeString(buffer);
1310 node = PLMakeDictionaryFromEntries(dCommand, command,
1311 dName, name,
1312 dAutoLaunch, autolaunch,
1313 dForced, forced,
1314 dBuggyApplication, buggy,
1315 dPosition, position,
1316 NULL);
1317 PLRelease(command);
1318 PLRelease(name);
1319 PLRelease(position);
1321 omnipresent = btn->omnipresent ? dYes : dNo;
1322 if (btn->dock != btn->icon->core->screen_ptr->dock &&
1323 (btn->xindex != 0 || btn->yindex != 0))
1324 PLInsertDictionaryEntry(node, dOmnipresent, omnipresent);
1326 #ifdef OFFIX_DND
1327 if (btn->dnd_command) {
1328 command = PLMakeString(btn->dnd_command);
1329 PLInsertDictionaryEntry(node, dDropCommand, command);
1330 PLRelease(command);
1332 #endif /* OFFIX_DND */
1334 if (btn->client_machine && btn->remote_start) {
1335 host = PLMakeString(btn->client_machine);
1336 PLInsertDictionaryEntry(node, dHost, host);
1337 PLRelease(host);
1341 return node;
1345 static proplist_t
1346 dockSaveState(WDock *dock)
1348 int i;
1349 proplist_t icon_info;
1350 proplist_t list=NULL, dock_state=NULL;
1351 proplist_t value;
1352 char buffer[256];
1354 list = PLMakeArrayFromElements(NULL);
1356 for (i=(dock->type==WM_DOCK ? 0 : 1); i<dock->max_icons; i++) {
1357 WAppIcon *btn = dock->icon_array[i];
1359 if (!btn || (btn->attracted && !dock->keep_attracted))
1360 continue;
1362 if ((icon_info = make_icon_state(dock->icon_array[i]))) {
1363 list = PLAppendArrayElement(list, icon_info);
1364 PLRelease(icon_info);
1368 dock_state = PLMakeDictionaryFromEntries(dApplications, list, NULL);
1370 PLRelease(list);
1372 if (dock->type == WM_DOCK) {
1373 sprintf(buffer, "%i,%i", (dock->on_right_side ? -ICON_SIZE : 0),
1374 dock->y_pos);
1375 value = PLMakeString(buffer);
1376 PLInsertDictionaryEntry(dock_state, dPosition, value);
1377 PLRelease(value);
1380 value = (dock->lowered ? dYes : dNo);
1381 PLInsertDictionaryEntry(dock_state, dLowered, value);
1383 if (dock->type == WM_CLIP) {
1384 value = (dock->collapsed ? dYes : dNo);
1385 PLInsertDictionaryEntry(dock_state, dCollapsed, value);
1387 value = (dock->auto_collapse ? dYes : dNo);
1388 PLInsertDictionaryEntry(dock_state, dAutoCollapse, value);
1390 value = (dock->auto_raise_lower ? dYes : dNo);
1391 PLInsertDictionaryEntry(dock_state, dAutoRaiseLower, value);
1393 value = (dock->attract_icons ? dYes : dNo);
1394 PLInsertDictionaryEntry(dock_state, dAutoAttractIcons, value);
1396 value = (dock->keep_attracted ? dYes : dNo);
1397 PLInsertDictionaryEntry(dock_state, dKeepAttracted, value);
1400 return dock_state;
1404 void
1405 wDockSaveState(WScreen *scr)
1407 proplist_t dock_state;
1409 dock_state = dockSaveState(scr->dock);
1411 PLInsertDictionaryEntry(scr->session_state, dDock, dock_state);
1413 PLRelease(dock_state);
1417 void
1418 wClipSaveState(WScreen *scr)
1420 proplist_t clip_state;
1422 clip_state = make_icon_state(scr->clip_icon);
1424 PLInsertDictionaryEntry(scr->session_state, dClip, clip_state);
1426 PLRelease(clip_state);
1430 proplist_t
1431 wClipSaveWorkspaceState(WScreen *scr, int workspace)
1433 return dockSaveState(scr->workspaces[workspace]->clip);
1437 static WAppIcon*
1438 restore_icon_state(WScreen *scr, proplist_t info, int type, int index)
1440 WAppIcon *aicon;
1441 char *wclass, *winstance;
1442 proplist_t cmd, value;
1443 char *command;
1446 cmd = PLGetDictionaryEntry(info, dCommand);
1447 if (!cmd || !PLIsString(cmd)) {
1448 return NULL;
1451 /* parse window name */
1452 value = PLGetDictionaryEntry(info, dName);
1453 if (!value)
1454 return NULL;
1456 ParseWindowName(value, &winstance, &wclass, "dock");
1458 if (!winstance && !wclass) {
1459 return NULL;
1462 /* get commands */
1464 if (cmd)
1465 command = wstrdup(PLGetString(cmd));
1466 else
1467 command = NULL;
1469 if (!command || strcmp(command, "-")==0) {
1470 if (command)
1471 free(command);
1472 if (wclass)
1473 free(wclass);
1474 if (winstance)
1475 free(winstance);
1477 return NULL;
1480 aicon = wAppIconCreateForDock(scr, command, winstance, wclass,
1481 TILE_NORMAL);
1482 if (wclass)
1483 free(wclass);
1484 if (winstance)
1485 free(winstance);
1487 aicon->icon->core->descriptor.handle_mousedown = iconMouseDown;
1488 if (type == WM_CLIP) {
1489 aicon->icon->core->descriptor.handle_enternotify = clipEnterNotify;
1490 aicon->icon->core->descriptor.handle_leavenotify = clipLeaveNotify;
1492 aicon->icon->core->descriptor.parent_type = WCLASS_DOCK_ICON;
1493 aicon->icon->core->descriptor.parent = aicon;
1496 #ifdef OFFIX_DND
1497 cmd = PLGetDictionaryEntry(info, dDropCommand);
1498 if (cmd)
1499 aicon->dnd_command = wstrdup(PLGetString(cmd));
1500 #endif
1502 /* check auto launch */
1503 value = PLGetDictionaryEntry(info, dAutoLaunch);
1505 aicon->auto_launch = 0;
1506 if (value) {
1507 if (PLIsString(value)) {
1508 if (strcasecmp(PLGetString(value), "YES")==0)
1509 aicon->auto_launch = 1;
1510 } else {
1511 wwarning(_("bad value in docked icon state info %s"),
1512 PLGetString(dAutoLaunch));
1516 /* check if it wasn't normally docked */
1517 value = PLGetDictionaryEntry(info, dForced);
1519 aicon->forced_dock = 0;
1520 if (value) {
1521 if (PLIsString(value)) {
1522 if (strcasecmp(PLGetString(value), "YES")==0)
1523 aicon->forced_dock = 1;
1524 } else {
1525 wwarning(_("bad value in docked icon state info %s"),
1526 PLGetString(dForced));
1530 /* check if we can rely on the stuff in the app */
1531 value = PLGetDictionaryEntry(info, dBuggyApplication);
1533 aicon->buggy_app = 0;
1534 if (value) {
1535 if (PLIsString(value)) {
1536 if (strcasecmp(PLGetString(value), "YES")==0)
1537 aicon->buggy_app = 1;
1538 } else {
1539 wwarning(_("bad value in docked icon state info %s"),
1540 PLGetString(dBuggyApplication));
1544 /* get position in the dock */
1545 value = PLGetDictionaryEntry(info, dPosition);
1546 if (value && PLIsString(value)) {
1547 if (sscanf(PLGetString(value), "%hi,%hi", &aicon->xindex,
1548 &aicon->yindex)!=2)
1549 wwarning(_("bad value in docked icon state info %s"),
1550 PLGetString(dPosition));
1552 /* check position sanity */
1553 /* incomplete section! */
1554 if (type == WM_DOCK) {
1555 aicon->xindex = 0;
1556 if (aicon->yindex < 0)
1557 wwarning(_("bad value in docked icon position %i,%i"),
1558 aicon->xindex, aicon->yindex);
1560 } else {
1561 aicon->yindex = index;
1562 aicon->xindex = 0;
1565 /* check if icon is omnipresent */
1566 value = PLGetDictionaryEntry(info, dOmnipresent);
1568 aicon->omnipresent = 0;
1569 if (value) {
1570 if (PLIsString(value)) {
1571 if (strcasecmp(PLGetString(value), "YES")==0)
1572 aicon->omnipresent = 1;
1573 } else {
1574 wwarning(_("bad value in docked icon state info %s"),
1575 PLGetString(dOmnipresent));
1579 aicon->running = 0;
1580 aicon->docked = 1;
1582 return aicon;
1586 #define COMPLAIN(key) wwarning(_("bad value in dock state info:%s"), key)
1589 WAppIcon*
1590 wClipRestoreState(WScreen *scr, proplist_t clip_state)
1592 WAppIcon *icon;
1593 proplist_t value;
1596 icon = mainIconCreate(scr, WM_CLIP);
1598 if (!clip_state)
1599 return icon;
1600 else
1601 PLRetain(clip_state);
1603 /* restore position */
1605 value = PLGetDictionaryEntry(clip_state, dPosition);
1607 if (value) {
1608 if (!PLIsString(value))
1609 COMPLAIN("Position");
1610 else {
1611 if (sscanf(PLGetString(value), "%i,%i", &icon->x_pos,
1612 &icon->y_pos)!=2)
1613 COMPLAIN("Position");
1615 /* check position sanity */
1616 if (icon->y_pos < 0)
1617 icon->y_pos = 0;
1618 else if (icon->y_pos > scr->scr_height-ICON_SIZE)
1619 icon->y_pos = scr->scr_height-ICON_SIZE;
1621 if (icon->x_pos < 0)
1622 icon->x_pos = 0;
1623 else if (icon->x_pos > scr->scr_width-ICON_SIZE)
1624 icon->x_pos = scr->scr_width-ICON_SIZE;
1628 #ifdef OFFIX_DND
1629 value = PLGetDictionaryEntry(clip_state, dDropCommand);
1630 if (value && PLIsString(value))
1631 icon->dnd_command = wstrdup(PLGetString(value));
1632 #endif
1634 PLRelease(clip_state);
1636 return icon;
1640 WDock*
1641 wDockRestoreState(WScreen *scr, proplist_t dock_state, int type)
1643 WDock *dock;
1644 proplist_t apps;
1645 proplist_t value;
1646 WAppIcon *aicon, *old_top;
1647 int count, i;
1650 dock = wDockCreate(scr, type);
1652 if (!dock_state)
1653 return dock;
1655 if (dock_state)
1656 PLRetain(dock_state);
1659 /* restore position */
1661 value = PLGetDictionaryEntry(dock_state, dPosition);
1663 if (value) {
1664 if (!PLIsString(value))
1665 COMPLAIN("Position");
1666 else {
1667 if (sscanf(PLGetString(value), "%i,%i", &dock->x_pos,
1668 &dock->y_pos)!=2)
1669 COMPLAIN("Position");
1671 /* check position sanity */
1672 if (dock->y_pos < 0)
1673 dock->y_pos = 0;
1674 else if (dock->y_pos > scr->scr_height-ICON_SIZE)
1675 dock->y_pos = scr->scr_height - ICON_SIZE;
1677 /* This is no more needed. ??? */
1678 if (type == WM_CLIP) {
1679 if (dock->x_pos < 0)
1680 dock->x_pos = 0;
1681 else if (dock->x_pos > scr->scr_width-ICON_SIZE)
1682 dock->x_pos = scr->scr_width-ICON_SIZE;
1683 } else {
1684 if (dock->x_pos >= 0) {
1685 dock->x_pos = DOCK_EXTRA_SPACE;
1686 dock->on_right_side = 0;
1687 } else {
1688 dock->x_pos = scr->scr_width - DOCK_EXTRA_SPACE - ICON_SIZE;
1689 dock->on_right_side = 1;
1695 /* restore lowered/raised state */
1697 dock->lowered = 0;
1699 value = PLGetDictionaryEntry(dock_state, dLowered);
1701 if (value) {
1702 if (!PLIsString(value))
1703 COMPLAIN("Lowered");
1704 else {
1705 if (strcasecmp(PLGetString(value), "YES")==0)
1706 dock->lowered = 1;
1711 /* restore collapsed state */
1713 dock->collapsed = 0;
1715 value = PLGetDictionaryEntry(dock_state, dCollapsed);
1717 if (value) {
1718 if (!PLIsString(value))
1719 COMPLAIN("Collapsed");
1720 else {
1721 if (strcasecmp(PLGetString(value), "YES")==0)
1722 dock->collapsed = 1;
1727 /* restore auto-collapsed state */
1729 value = PLGetDictionaryEntry(dock_state, dAutoCollapse);
1731 if (value) {
1732 if (!PLIsString(value))
1733 COMPLAIN("AutoCollapse");
1734 else {
1735 if (strcasecmp(PLGetString(value), "YES")==0) {
1736 dock->auto_collapse = 1;
1737 dock->collapsed = 1;
1743 /* restore auto-raise/lower state */
1745 value = PLGetDictionaryEntry(dock_state, dAutoRaiseLower);
1747 if (value) {
1748 if (!PLIsString(value))
1749 COMPLAIN("AutoRaiseLower");
1750 else {
1751 if (strcasecmp(PLGetString(value), "YES")==0) {
1752 dock->auto_raise_lower = 1;
1757 /* restore attract icons state */
1759 dock->attract_icons = 0;
1761 value = PLGetDictionaryEntry(dock_state, dAutoAttractIcons);
1763 if (value) {
1764 if (!PLIsString(value))
1765 COMPLAIN("AutoAttractIcons");
1766 else {
1767 if (strcasecmp(PLGetString(value), "YES")==0)
1768 dock->attract_icons = 1;
1773 /* restore keep attracted icons state */
1775 dock->keep_attracted = 0;
1777 value = PLGetDictionaryEntry(dock_state, dKeepAttracted);
1779 if (value) {
1780 if (!PLIsString(value))
1781 COMPLAIN("KeepAttracted");
1782 else {
1783 if (strcasecmp(PLGetString(value), "YES")==0)
1784 dock->keep_attracted = 1;
1789 /* application list */
1791 apps = PLGetDictionaryEntry(dock_state, dApplications);
1793 if (!apps) {
1794 goto finish;
1797 count = PLGetNumberOfElements(apps);
1799 if (count==0)
1800 goto finish;
1802 old_top = dock->icon_array[0];
1804 /* dock->icon_count is set to 1 when dock is created.
1805 * Since Clip is already restored, we want to keep it so for clip,
1806 * but for dock we may change the default top tile, so we set it to 0.
1808 if (type == WM_DOCK)
1809 dock->icon_count = 0;
1811 for (i=0; i<count; i++) {
1812 if (dock->icon_count >= dock->max_icons) {
1813 wwarning(_("there are too many icons stored in dock. Ignoring what doesn't fit"));
1814 break;
1817 value = PLGetArrayElement(apps, i);
1818 aicon = restore_icon_state(scr, value, type, dock->icon_count);
1820 dock->icon_array[dock->icon_count] = aicon;
1822 if (aicon) {
1823 aicon->dock = dock;
1824 aicon->x_pos = dock->x_pos + (aicon->xindex*ICON_SIZE);
1825 aicon->y_pos = dock->y_pos + (aicon->yindex*ICON_SIZE);
1827 if (dock->lowered)
1828 ChangeStackingLevel(aicon->icon->core, WMNormalLevel);
1829 else
1830 ChangeStackingLevel(aicon->icon->core, WMDockLevel);
1832 wCoreConfigure(aicon->icon->core, aicon->x_pos, aicon->y_pos,
1833 0, 0);
1835 if (!dock->collapsed)
1836 XMapWindow(dpy, aicon->icon->core->window);
1837 wRaiseFrame(aicon->icon->core);
1839 dock->icon_count++;
1840 } else if (dock->icon_count==0 && type==WM_DOCK)
1841 dock->icon_count++;
1844 /* if the first icon is not defined, use the default */
1845 if (dock->icon_array[0]==NULL) {
1846 /* update default icon */
1847 old_top->x_pos = dock->x_pos;
1848 old_top->y_pos = dock->y_pos;
1849 if (dock->lowered)
1850 ChangeStackingLevel(old_top->icon->core, WMNormalLevel);
1851 else
1852 ChangeStackingLevel(old_top->icon->core, WMDockLevel);
1853 dock->icon_array[0] = old_top;
1854 XMoveWindow(dpy, old_top->icon->core->window, dock->x_pos, dock->y_pos);
1855 /* we don't need to increment dock->icon_count here because it was
1856 * incremented in the loop above.
1858 } else if (old_top!=dock->icon_array[0]) {
1859 if (old_top == scr->clip_icon)
1860 scr->clip_icon = dock->icon_array[0];
1861 wAppIconDestroy(old_top);
1864 finish:
1865 if (dock_state)
1866 PLRelease(dock_state);
1868 return dock;
1873 void
1874 wDockLaunchWithState(WDock *dock, WAppIcon *btn, WSavedState *state)
1876 if (btn && btn->command && !btn->running && !btn->launching) {
1878 btn->drop_launch = 0;
1880 btn->pid = execCommand(btn, btn->command, state);
1882 if (btn->pid>0) {
1883 if (!btn->forced_dock && !btn->buggy_app) {
1884 btn->launching = 1;
1885 dockIconPaint(btn);
1888 } else {
1889 free(state);
1894 void
1895 wDockDoAutoLaunch(WDock *dock, int workspace)
1897 WAppIcon *btn;
1898 WSavedState *state;
1899 int i;
1901 for (i = 0; i < dock->max_icons; i++) {
1902 btn = dock->icon_array[i];
1903 if (!btn || !btn->auto_launch)
1904 continue;
1906 state = wmalloc(sizeof(WSavedState));
1907 memset(state, 0, sizeof(WSavedState));
1908 state->workspace = workspace;
1909 /* TODO: this is klugy and is very difficult to understand
1910 * what's going on. Try to clean up */
1911 wDockLaunchWithState(dock, btn, state);
1915 #ifdef REDUCE_APPICONS
1916 void
1917 wDockSimulateLaunch(WDock *dock, WAppIcon *btn)
1919 if ((btn == NULL) || (dock == NULL))
1920 return;
1922 if (!btn->running) {
1923 if ((btn->icon->owner == NULL) && (btn->applist))
1924 btn->icon->owner = btn->applist->wapp->main_window_desc;
1925 if (!btn->forced_dock)
1926 btn->launching = 1;
1927 dockIconPaint(btn);
1928 wusleep(5000);
1931 #endif
1933 #ifdef OFFIX_DND
1934 static WDock*
1935 findDock(WScreen *scr, XEvent *event, int *icon_pos)
1937 WDock *dock;
1938 int i;
1940 *icon_pos = -1;
1941 if ((dock = scr->dock)!=NULL) {
1942 for (i=0; i<dock->max_icons; i++) {
1943 if (dock->icon_array[i]
1944 && dock->icon_array[i]->icon->core->window==event->xclient.window) {
1945 *icon_pos = i;
1946 break;
1950 if (*icon_pos<0 && (dock = scr->workspaces[scr->current_workspace]->clip)!=NULL) {
1951 for (i=0; i<dock->max_icons; i++) {
1952 if (dock->icon_array[i]
1953 && dock->icon_array[i]->icon->core->window==event->xclient.window) {
1954 *icon_pos = i;
1955 break;
1959 if(*icon_pos>=0)
1960 return dock;
1961 return NULL;
1966 wDockReceiveDNDDrop(WScreen *scr, XEvent *event)
1968 WDock *dock;
1969 WAppIcon *btn;
1970 int icon_pos;
1972 dock = findDock(scr, event, &icon_pos);
1973 if (!dock)
1974 return False;
1977 * Return True if the drop was on an application icon window.
1978 * In this case, let the ClientMessage handler redirect the
1979 * message to the app.
1981 if (dock->icon_array[icon_pos]->icon->icon_win!=None)
1982 return True;
1984 if (dock->icon_array[icon_pos]->dnd_command!=NULL) {
1985 scr->flags.dnd_data_convertion_status = 0;
1987 btn = dock->icon_array[icon_pos];
1989 if (!btn->forced_dock) {
1990 btn->relaunching = btn->running;
1991 btn->running = 1;
1993 if (btn->wm_instance || btn->wm_class) {
1994 WWindowAttributes attr;
1995 memset(&attr, 0, sizeof(WWindowAttributes));
1996 wDefaultFillAttributes(btn->icon->core->screen_ptr,
1997 btn->wm_instance,
1998 btn->wm_class, &attr, NULL, True);
2000 if (!attr.no_appicon)
2001 btn->launching = 1;
2002 else
2003 btn->running = 0;
2006 btn->drop_launch = 1;
2007 scr->last_dock = dock;
2008 btn->pid = execCommand(btn, btn->dnd_command, NULL);
2009 if (btn->pid>0) {
2010 dockIconPaint(btn);
2011 } else {
2012 btn->launching = 0;
2013 if (!btn->relaunching) {
2014 btn->running = 0;
2018 return False;
2020 #endif /* OFFIX_DND */
2024 Bool
2025 wDockAttachIcon(WDock *dock, WAppIcon *icon, int x, int y)
2027 WWindow *wwin;
2028 char **argv;
2029 int argc;
2030 int index;
2032 wwin = icon->icon->owner;
2033 if (icon->command==NULL) {
2034 icon->editing = 0;
2035 if (XGetCommand(dpy, wwin->client_win, &argv, &argc) && argc>0) {
2037 icon->command = FlattenStringList(argv, argc);
2038 XFreeStringList(argv);
2039 } else {
2040 char *command=NULL;
2042 /* icon->forced_dock = 1;*/
2043 if (!icon->attracted || dock->type!=WM_CLIP || dock->keep_attracted) {
2044 icon->editing = 1;
2045 if (wInputDialog(dock->screen_ptr, _("Dock Icon"),
2046 _("Type the command used to launch the application"),
2047 &command)) {
2048 if (command && (command[0]==0 ||
2049 (command[0]=='-' && command[1]==0))) {
2050 free(command);
2051 command = NULL;
2053 icon->command = command;
2054 icon->editing = 0;
2055 } else {
2056 icon->editing = 0;
2057 if (command)
2058 free(command);
2059 /* If the target is the dock, reject the icon. If
2060 * the target is the clip, make it an attracted icon
2062 if (dock->type==WM_CLIP) {
2063 icon->attracted = 1;
2064 if (!icon->icon->shadowed) {
2065 icon->icon->shadowed = 1;
2066 icon->icon->force_paint = 1;
2068 } else {
2069 return False;
2074 } else {
2075 icon->editing = 0;
2078 for (index=1; index<dock->max_icons; index++)
2079 if (dock->icon_array[index] == NULL)
2080 break;
2081 /* if (index == dock->max_icons)
2082 return; */
2084 assert(index < dock->max_icons);
2086 dock->icon_array[index] = icon;
2087 icon->yindex = y;
2088 icon->xindex = x;
2090 icon->omnipresent = 0;
2092 icon->x_pos = dock->x_pos + x*ICON_SIZE;
2093 icon->y_pos = dock->y_pos + y*ICON_SIZE;
2095 dock->icon_count++;
2097 icon->running = 1;
2098 icon->launching = 0;
2099 icon->docked = 1;
2100 icon->dock = dock;
2101 icon->icon->core->descriptor.handle_mousedown = iconMouseDown;
2102 if (dock->type == WM_CLIP) {
2103 icon->icon->core->descriptor.handle_enternotify = clipEnterNotify;
2104 icon->icon->core->descriptor.handle_leavenotify = clipLeaveNotify;
2106 icon->icon->core->descriptor.parent_type = WCLASS_DOCK_ICON;
2107 icon->icon->core->descriptor.parent = icon;
2109 MoveInStackListUnder(dock->icon_array[index-1]->icon->core,
2110 icon->icon->core);
2111 wAppIconMove(icon, icon->x_pos, icon->y_pos);
2112 wAppIconPaint(icon);
2114 if (wPreferences.auto_arrange_icons)
2115 wArrangeIcons(dock->screen_ptr, True);
2117 #ifdef OFFIX_DND
2118 if (icon->command && !icon->dnd_command) {
2119 icon->dnd_command = wmalloc(strlen(icon->command)+8);
2120 sprintf(icon->dnd_command, "%s %%d", icon->command);
2122 #endif
2124 return True;
2128 void
2129 reattachIcon(WDock *dock, WAppIcon *icon, int x, int y)
2131 int index;
2133 for(index=1; index<dock->max_icons; index++) {
2134 if(dock->icon_array[index] == icon)
2135 break;
2137 assert(index < dock->max_icons);
2139 icon->yindex = y;
2140 icon->xindex = x;
2142 icon->x_pos = dock->x_pos + x*ICON_SIZE;
2143 icon->y_pos = dock->y_pos + y*ICON_SIZE;
2147 Bool
2148 moveIconBetweenDocks(WDock *src, WDock *dest, WAppIcon *icon, int x, int y)
2150 WWindow *wwin;
2151 char **argv;
2152 int argc;
2153 int index;
2155 if (src == dest)
2156 return True; /* No move needed, we're already there */
2158 if (dest == NULL)
2159 return False;
2161 wwin = icon->icon->owner;
2164 * For the moment we can't do this if we move icons in Clip from one
2165 * workspace to other, because if we move two or more icons without
2166 * command, the dialog box will not be able to tell us to which of the
2167 * moved icons it applies. -Dan
2169 if ((dest->type==WM_DOCK /*|| dest->keep_attracted*/) && icon->command==NULL) {
2170 if (XGetCommand(dpy, wwin->client_win, &argv, &argc) && argc>0) {
2172 icon->command = FlattenStringList(argv, argc);
2173 XFreeStringList(argv);
2174 } else {
2175 char *command=NULL;
2177 icon->editing = 1;
2178 /* icon->forced_dock = 1;*/
2179 if (wInputDialog(src->screen_ptr, _("Dock Icon"),
2180 _("Type the command used to launch the application"),
2181 &command)) {
2182 if (command && (command[0]==0 ||
2183 (command[0]=='-' && command[1]==0))) {
2184 free(command);
2185 command = NULL;
2187 icon->command = command;
2188 } else {
2189 icon->editing = 0;
2190 if (command)
2191 free(command);
2192 return False;
2194 icon->editing = 0;
2198 if (dest->type == WM_DOCK)
2199 wClipMakeIconOmnipresent(icon, False);
2201 for(index=1; index<src->max_icons; index++) {
2202 if(src->icon_array[index] == icon)
2203 break;
2205 assert(index < src->max_icons);
2207 src->icon_array[index] = NULL;
2208 src->icon_count--;
2210 for(index=1; index<dest->max_icons; index++) {
2211 if(dest->icon_array[index] == NULL)
2212 break;
2214 /* if (index == dest->max_icons)
2215 return; */
2217 assert(index < dest->max_icons);
2219 dest->icon_array[index] = icon;
2220 icon->dock = dest;
2222 /* deselect the icon */
2223 if (icon->icon->selected)
2224 wIconSelect(icon->icon);
2226 if (dest->type == WM_DOCK) {
2227 icon->icon->core->descriptor.handle_enternotify = NULL;
2228 icon->icon->core->descriptor.handle_leavenotify = NULL;
2229 } else {
2230 icon->icon->core->descriptor.handle_enternotify = clipEnterNotify;
2231 icon->icon->core->descriptor.handle_leavenotify = clipLeaveNotify;
2234 /* set it to be kept when moving to dock, or to a clip that keep the
2235 * attracted icons.
2236 * Unless the icon does not have a command set
2238 if (icon->command && (dest->type==WM_DOCK || dest->keep_attracted)) {
2239 icon->attracted = 0;
2240 if (icon->icon->shadowed) {
2241 icon->icon->shadowed = 0;
2242 icon->icon->force_paint = 1;
2246 if (src->auto_collapse || src->auto_raise_lower)
2247 clipLeave(src);
2249 icon->yindex = y;
2250 icon->xindex = x;
2252 icon->x_pos = dest->x_pos + x*ICON_SIZE;
2253 icon->y_pos = dest->y_pos + y*ICON_SIZE;
2255 dest->icon_count++;
2257 MoveInStackListUnder(dest->icon_array[index-1]->icon->core,
2258 icon->icon->core);
2259 wAppIconPaint(icon);
2261 return True;
2265 void
2266 wDockDetach(WDock *dock, WAppIcon *icon)
2268 int index;
2270 /* make the settings panel be closed */
2271 if (icon->panel) {
2272 DestroyDockAppSettingsPanel(icon->panel);
2275 icon->docked = 0;
2276 icon->dock = NULL;
2277 icon->attracted = 0;
2278 if (icon->icon->shadowed) {
2279 icon->icon->shadowed = 0;
2280 icon->icon->force_paint = 1;
2283 /* This must be called before dock->icon_array[index] is set to NULL.
2284 * Don't move it. -Dan
2286 wClipMakeIconOmnipresent(icon, False);
2288 /* deselect the icon */
2289 if (icon->icon->selected)
2290 wIconSelect(icon->icon);
2292 if (icon->command) {
2293 free(icon->command);
2294 icon->command = NULL;
2296 #ifdef OFFIX_DND
2297 if (icon->dnd_command) {
2298 free(icon->dnd_command);
2299 icon->dnd_command = NULL;
2301 #endif
2303 for (index=1; index<dock->max_icons; index++)
2304 if (dock->icon_array[index] == icon)
2305 break;
2306 assert(index < dock->max_icons);
2307 dock->icon_array[index] = NULL;
2308 icon->yindex = -1;
2309 icon->xindex = -1;
2311 dock->icon_count--;
2313 /* if the dock is not attached to an application or
2314 * the the application did not set the approriate hints yet,
2315 * destroy the icon */
2316 #ifdef REDUCE_APPICONS
2317 if ((icon->num_apps == 0) && (!icon->running || !wApplicationOf(icon->main_window)) )
2318 #else
2319 if (!icon->running || !wApplicationOf(icon->main_window))
2320 #endif
2321 wAppIconDestroy(icon);
2322 else {
2323 icon->icon->core->descriptor.handle_mousedown = appIconMouseDown;
2324 icon->icon->core->descriptor.handle_enternotify = NULL;
2325 icon->icon->core->descriptor.handle_leavenotify = NULL;
2326 icon->icon->core->descriptor.parent_type = WCLASS_APPICON;
2327 icon->icon->core->descriptor.parent = icon;
2329 ChangeStackingLevel(icon->icon->core, NORMAL_ICON_LEVEL);
2331 wAppIconPaint(icon);
2332 if (wPreferences.auto_arrange_icons) {
2333 wArrangeIcons(dock->screen_ptr, True);
2336 if (dock->auto_collapse || dock->auto_raise_lower)
2337 clipLeave(dock);
2342 * returns the closest Dock slot index for the passed
2343 * coordinates.
2345 * Returns False if icon can't be docked.
2347 * Note: this function should NEVER alter ret_x or ret_y, unless it will
2348 * return True. -Dan
2350 Bool
2351 wDockSnapIcon(WDock *dock, WAppIcon *icon, int req_x, int req_y,
2352 int *ret_x, int *ret_y, int redocking)
2354 WScreen *scr = dock->screen_ptr;
2355 int dx, dy;
2356 int ex_x, ex_y;
2357 int i, offset = ICON_SIZE/2;
2358 WAppIcon *aicon = NULL;
2359 WAppIcon *nicon = NULL;
2360 int max_y_icons, max_x_icons;
2362 max_x_icons = scr->scr_width/ICON_SIZE;
2363 max_y_icons = scr->scr_height/ICON_SIZE-1;
2365 if (wPreferences.flags.noupdates)
2366 return False;
2368 dx = dock->x_pos;
2369 dy = dock->y_pos;
2371 /* if the dock is full */
2372 if (!redocking &&
2373 (dock->icon_count >= dock->max_icons)) {
2374 return False;
2377 /* exact position */
2378 if (req_y < dy)
2379 ex_y = (req_y - offset - dy)/ICON_SIZE;
2380 else
2381 ex_y = (req_y + offset - dy)/ICON_SIZE;
2383 if (req_x < dx)
2384 ex_x = (req_x - offset - dx)/ICON_SIZE;
2385 else
2386 ex_x = (req_x + offset - dx)/ICON_SIZE;
2388 /* check if the icon is outside the screen boundaries */
2389 if (dx + ex_x*ICON_SIZE < -ICON_SIZE+2 ||
2390 dx + ex_x*ICON_SIZE >= scr->scr_width-1 ||
2391 dy + ex_y*ICON_SIZE < -ICON_SIZE+2 ||
2392 dy + ex_y*ICON_SIZE >= scr->scr_height-1)
2393 return False;
2395 if (dock->type == WM_DOCK) {
2396 if (icon->dock != dock && ex_x != 0)
2397 return False;
2399 aicon = NULL;
2400 for (i=0; i<dock->max_icons; i++) {
2401 nicon = dock->icon_array[i];
2402 if (nicon && nicon->yindex == ex_y) {
2403 aicon = nicon;
2404 break;
2408 if (redocking) {
2409 int sig, done, closest;
2411 /* Possible cases when redocking:
2413 * icon dragged out of range of any slot -> false
2414 * icon dragged to range of free slot
2415 * icon dragged to range of same slot
2416 * icon dragged to range of different icon
2418 if (abs(ex_x) > DOCK_DETTACH_THRESHOLD)
2419 return False;
2421 if (ex_y>=0 && ex_y<=max_y_icons && (aicon==icon || !aicon)) {
2422 *ret_x = 0;
2423 *ret_y = ex_y;
2424 return True;
2427 /* start looking at the upper slot or lower? */
2428 if (ex_y*ICON_SIZE < (req_y + offset - dy))
2429 sig = 1;
2430 else
2431 sig = -1;
2433 closest = -1;
2434 done = 0;
2435 /* look for closest free slot */
2436 for (i=0; i<(DOCK_DETTACH_THRESHOLD+1)*2 && !done; i++) {
2437 int j;
2439 done = 1;
2440 closest = sig*(i/2) + ex_y;
2441 /* check if this slot is used */
2442 if (closest >= 0) {
2443 for (j = 0; j<dock->max_icons; j++) {
2444 if (dock->icon_array[j]
2445 && dock->icon_array[j]->yindex==closest) {
2446 /* slot is used by someone else */
2447 if (dock->icon_array[j]!=icon)
2448 done = 0;
2449 break;
2453 sig = -sig;
2455 if (done && closest >= 0 && closest <= max_y_icons &&
2456 ((ex_y >= closest && ex_y - closest < DOCK_DETTACH_THRESHOLD+1)
2458 (ex_y < closest && closest - ex_y <= DOCK_DETTACH_THRESHOLD+1))) {
2459 *ret_x = 0;
2460 *ret_y = closest;
2461 return True;
2463 } else { /* !redocking */
2465 /* if slot is free and the icon is close enough, return it */
2466 if (!aicon && ex_x == 0 && ex_y >= 0 && ex_y <= max_y_icons) {
2467 *ret_x = 0;
2468 *ret_y = ex_y;
2469 return True;
2472 } else { /* CLIP */
2473 int neighbours = 0;
2474 int start, stop, k;
2476 start = icon->omnipresent ? 0 : scr->current_workspace;
2477 stop = icon->omnipresent ? scr->workspace_count : start+1;
2479 aicon = NULL;
2480 for (k=start; k<stop; k++) {
2481 WDock *tmp = scr->workspaces[k]->clip;
2482 if (!tmp)
2483 continue;
2484 for (i=0; i<tmp->max_icons; i++) {
2485 nicon = tmp->icon_array[i];
2486 if (nicon && nicon->xindex == ex_x && nicon->yindex == ex_y) {
2487 aicon = nicon;
2488 break;
2491 if (aicon)
2492 break;
2494 for (k=start; k<stop; k++) {
2495 WDock *tmp = scr->workspaces[k]->clip;
2496 if (!tmp)
2497 continue;
2498 for (i=0; i<tmp->max_icons; i++) {
2499 nicon = tmp->icon_array[i];
2500 if (nicon && nicon != icon && /* Icon can't be it's own neighbour */
2501 (abs(nicon->xindex - ex_x) <= CLIP_ATTACH_VICINITY &&
2502 abs(nicon->yindex - ex_y) <= CLIP_ATTACH_VICINITY)) {
2503 neighbours = 1;
2504 break;
2507 if (neighbours)
2508 break;
2511 if (neighbours && (aicon==NULL || (redocking && aicon == icon))) {
2512 *ret_x = ex_x;
2513 *ret_y = ex_y;
2514 return True;
2517 return False;
2520 #define MIN(x, y) ((x) > (y) ? (y) : (x))
2521 #define MAX(x, y) ((x) < (y) ? (y) : (x))
2523 #define ON_SCREEN(x, y, sx, ex, sy, ey) \
2524 ((((x)+ICON_SIZE/2) >= (sx)) && (((y)+ICON_SIZE/2) >= (sy)) && \
2525 (((x) + (ICON_SIZE/2)) <= (ex)) && (((y) + (ICON_SIZE/2)) <= ey))
2529 * returns true if it can find a free slot in the dock,
2530 * in which case it changes x_pos and y_pos accordingly.
2531 * Else returns false.
2533 Bool
2534 wDockFindFreeSlot(WDock *dock, int *x_pos, int *y_pos)
2536 WScreen *scr = dock->screen_ptr;
2537 WAppIcon *btn;
2538 unsigned char *slot_map;
2539 int mwidth;
2540 int r;
2541 int x, y;
2542 int i, done = False;
2543 int corner;
2544 int sx=0, sy=0, ex=scr->scr_width, ey=scr->scr_height;
2547 /* if the dock is full */
2548 if (dock->icon_count >= dock->max_icons) {
2549 return False;
2552 if (!wPreferences.flags.nodock && scr->dock) {
2553 if (scr->dock->on_right_side)
2554 ex -= ICON_SIZE + DOCK_EXTRA_SPACE;
2555 else
2556 sx += ICON_SIZE + DOCK_EXTRA_SPACE;
2559 if (ex < dock->x_pos)
2560 ex = dock->x_pos;
2561 if (sx > dock->x_pos+ICON_SIZE)
2562 sx = dock->x_pos+ICON_SIZE;
2563 #define C_NONE 0
2564 #define C_NW 1
2565 #define C_NE 2
2566 #define C_SW 3
2567 #define C_SE 4
2569 /* check if clip is in a corner */
2570 if (dock->type==WM_CLIP) {
2571 if (dock->x_pos < 1 && dock->y_pos < 1)
2572 corner = C_NE;
2573 else if (dock->x_pos < 1 && dock->y_pos >= (ey-ICON_SIZE))
2574 corner = C_SE;
2575 else if (dock->x_pos >= (ex-ICON_SIZE)&& dock->y_pos >= (ey-ICON_SIZE))
2576 corner = C_SW;
2577 else if (dock->x_pos >= (ex-ICON_SIZE) && dock->y_pos < 1)
2578 corner = C_NW;
2579 else
2580 corner = C_NONE;
2581 } else
2582 corner = C_NONE;
2584 /* If the clip is in the corner, use only slots that are in the border
2585 * of the screen */
2586 if (corner!=C_NONE) {
2587 char *hmap, *vmap;
2588 int hcount, vcount;
2590 hcount = MIN(dock->max_icons, scr->scr_width/ICON_SIZE);
2591 vcount = MIN(dock->max_icons, scr->scr_height/ICON_SIZE);
2592 hmap = wmalloc(hcount+1);
2593 memset(hmap, 0, hcount+1);
2594 vmap = wmalloc(vcount+1);
2595 memset(vmap, 0, vcount+1);
2597 /* mark used positions */
2598 switch (corner) {
2599 case C_NE:
2600 for (i=0; i<dock->max_icons; i++) {
2601 btn = dock->icon_array[i];
2602 if (!btn)
2603 continue;
2605 if (btn->xindex==0 && btn->yindex > 0 && btn->yindex < vcount)
2606 vmap[btn->yindex] = 1;
2607 else if (btn->yindex==0 && btn->xindex>0 && btn->xindex<hcount)
2608 hmap[btn->xindex] = 1;
2610 case C_NW:
2611 for (i=0; i<dock->max_icons; i++) {
2612 btn = dock->icon_array[i];
2613 if (!btn)
2614 continue;
2616 if (btn->xindex==0 && btn->yindex > 0 && btn->yindex < vcount)
2617 vmap[btn->yindex] = 1;
2618 else if (btn->yindex==0 && btn->xindex<0 &&btn->xindex>-hcount)
2619 hmap[-btn->xindex] = 1;
2621 case C_SE:
2622 for (i=0; i<dock->max_icons; i++) {
2623 btn = dock->icon_array[i];
2624 if (!btn)
2625 continue;
2627 if (btn->xindex==0 && btn->yindex < 0 && btn->yindex > -vcount)
2628 vmap[-btn->yindex] = 1;
2629 else if (btn->yindex==0 && btn->xindex>0 && btn->xindex<hcount)
2630 hmap[btn->xindex] = 1;
2632 case C_SW:
2633 default:
2634 for (i=0; i<dock->max_icons; i++) {
2635 btn = dock->icon_array[i];
2636 if (!btn)
2637 continue;
2639 if (btn->xindex==0 && btn->yindex < 0 && btn->yindex > -vcount)
2640 vmap[-btn->yindex] = 1;
2641 else if (btn->yindex==0 && btn->xindex<0 &&btn->xindex>-hcount)
2642 hmap[-btn->xindex] = 1;
2645 x=0; y=0;
2646 done = 0;
2647 /* search a vacant slot */
2648 for (i=1; i<MAX(vcount, hcount); i++) {
2649 if (i < vcount && vmap[i]==0) {
2650 /* found a slot */
2651 x = 0;
2652 y = i;
2653 done = 1;
2654 break;
2655 } else if (i < hcount && hmap[i]==0) {
2656 /* found a slot */
2657 x = i;
2658 y = 0;
2659 done = 1;
2660 break;
2663 free(vmap);
2664 free(hmap);
2665 /* If found a slot, translate and return */
2666 if (done) {
2667 if (corner==C_NW || corner==C_NE) {
2668 *y_pos = y;
2669 } else {
2670 *y_pos = -y;
2672 if (corner==C_NE || corner==C_SE) {
2673 *x_pos = x;
2674 } else {
2675 *x_pos = -x;
2677 return True;
2679 /* else, try to find a slot somewhere else */
2682 /* a map of mwidth x mwidth would be enough if we allowed icons to be
2683 * placed outside of screen */
2684 mwidth = (int)ceil(sqrt(dock->max_icons));
2686 /* In the worst case (the clip is in the corner of the screen),
2687 * the amount of icons that fit in the clip is smaller.
2688 * Double the map to get a safe value.
2690 mwidth += mwidth;
2692 r = (mwidth-1)/2;
2694 slot_map = wmalloc(mwidth*mwidth);
2695 memset(slot_map, 0, mwidth*mwidth);
2697 #define XY2OFS(x,y) (MAX(abs(x),abs(y)) > r) ? 0 : (((y)+r)*(mwidth)+(x)+r)
2699 /* mark used slots in the map. If the slot falls outside the map
2700 * (for example, when all icons are placed in line), ignore them. */
2701 for (i=0; i<dock->max_icons; i++) {
2702 btn = dock->icon_array[i];
2703 if (btn)
2704 slot_map[XY2OFS(btn->xindex, btn->yindex)] = 1;
2706 /* Find closest slot from the center that is free by scanning the
2707 * map from the center to outward in circular passes.
2708 * This will not result in a neat layout, but will be optimal
2709 * in the sense that there will not be holes left.
2711 done = 0;
2712 for (i = 1; i <= r && !done; i++) {
2713 int tx, ty;
2715 /* top and bottom parts of the ring */
2716 for (x = -i; x <= i && !done; x++) {
2717 tx = dock->x_pos + x*ICON_SIZE;
2718 y = -i;
2719 ty = dock->y_pos + y*ICON_SIZE;
2720 if (slot_map[XY2OFS(x,y)]==0
2721 && ON_SCREEN(tx, ty, sx, ex, sy, ey)) {
2722 *x_pos = x;
2723 *y_pos = y;
2724 done = 1;
2725 break;
2727 y = i;
2728 ty = dock->y_pos + y*ICON_SIZE;
2729 if (slot_map[XY2OFS(x,y)]==0
2730 && ON_SCREEN(tx, ty, sx, ex, sy, ey)) {
2731 *x_pos = x;
2732 *y_pos = y;
2733 done = 1;
2734 break;
2737 /* left and right parts of the ring */
2738 for (y = -i+1; y <= i-1; y++) {
2739 ty = dock->y_pos + y*ICON_SIZE;
2740 x = -i;
2741 tx = dock->x_pos + x*ICON_SIZE;
2742 if (slot_map[XY2OFS(x,y)]==0
2743 && ON_SCREEN(tx, ty, sx, ex, sy, ey)) {
2744 *x_pos = x;
2745 *y_pos = y;
2746 done = 1;
2747 break;
2749 x = i;
2750 tx = dock->x_pos + x*ICON_SIZE;
2751 if (slot_map[XY2OFS(x,y)]==0
2752 && ON_SCREEN(tx, ty, sx, ex, sy, ey)) {
2753 *x_pos = x;
2754 *y_pos = y;
2755 done = 1;
2756 break;
2760 free(slot_map);
2761 #undef XY2OFS
2762 return done;
2766 static void
2767 moveDock(WDock *dock, int new_x, int new_y)
2769 WAppIcon *btn;
2770 int i;
2772 dock->x_pos = new_x;
2773 dock->y_pos = new_y;
2774 for (i=0; i<dock->max_icons; i++) {
2775 btn = dock->icon_array[i];
2776 if (btn) {
2777 btn->x_pos = new_x + btn->xindex*ICON_SIZE;
2778 btn->y_pos = new_y + btn->yindex*ICON_SIZE;
2779 XMoveWindow(dpy, btn->icon->core->window, btn->x_pos, btn->y_pos);
2785 static void
2786 swapDock(WDock *dock)
2788 WScreen *scr = dock->screen_ptr;
2789 WAppIcon *btn;
2790 int x, i;
2793 if (dock->on_right_side) {
2794 x = dock->x_pos = scr->scr_width - ICON_SIZE - DOCK_EXTRA_SPACE;
2795 } else {
2796 x = dock->x_pos = DOCK_EXTRA_SPACE;
2799 for (i=0; i<dock->max_icons; i++) {
2800 btn = dock->icon_array[i];
2801 if (btn) {
2802 btn->x_pos = x;
2803 XMoveWindow(dpy, btn->icon->core->window, btn->x_pos, btn->y_pos);
2807 wScreenUpdateUsableArea(scr);
2811 static pid_t
2812 execCommand(WAppIcon *btn, char *command, WSavedState *state)
2814 WScreen *scr = btn->icon->core->screen_ptr;
2815 pid_t pid;
2816 char **argv;
2817 int argc;
2818 char *cmdline;
2820 cmdline = ExpandOptions(scr, command);
2822 if (scr->flags.dnd_data_convertion_status || !cmdline) {
2823 if (cmdline)
2824 free(cmdline);
2825 if (state)
2826 free(state);
2827 return 0;
2830 ParseCommand(cmdline, &argv, &argc);
2832 if (argv==NULL) {
2833 if (cmdline)
2834 free(cmdline);
2835 if (state)
2836 free(state);
2837 return 0;
2840 if ((pid=fork())==0) {
2841 char **args;
2842 int i;
2844 SetupEnvironment(scr);
2846 #ifdef HAVE_SETPGID
2847 setpgid(0, 0);
2848 #endif
2850 args = malloc(sizeof(char*)*(argc+1));
2851 if (!args)
2852 exit(111);
2853 for (i=0; i<argc; i++) {
2854 args[i] = argv[i];
2856 args[argc] = NULL;
2857 execvp(argv[0], args);
2858 exit(111);
2860 while (argc > 0)
2861 free(argv[--argc]);
2862 free(argv);
2864 if (pid > 0) {
2865 if (!state) {
2866 state = wmalloc(sizeof(WSavedState));
2867 memset(state, 0, sizeof(WSavedState));
2868 state->hidden = -1;
2869 state->miniaturized = -1;
2870 state->shaded = -1;
2871 if (btn->dock == scr->dock)
2872 state->workspace = -1;
2873 else
2874 state->workspace = scr->current_workspace;
2876 wWindowAddSavedState(btn->wm_instance, btn->wm_class, cmdline, pid,
2877 state);
2878 wAddDeathHandler(pid, (WDeathHandler*)trackDeadProcess,
2879 btn->dock);
2880 } else if (state) {
2881 free(state);
2883 free(cmdline);
2884 return pid;
2888 void
2889 wDockHideIcons(WDock *dock)
2891 int i;
2893 if (dock==NULL)
2894 return;
2896 for (i=1; i<dock->max_icons; i++) {
2897 if (dock->icon_array[i])
2898 XUnmapWindow(dpy, dock->icon_array[i]->icon->core->window);
2900 dock->mapped = 0;
2902 dockIconPaint(dock->icon_array[0]);
2906 void
2907 wDockShowIcons(WDock *dock)
2909 int i, newlevel;
2910 WAppIcon *btn;
2912 if (dock==NULL)
2913 return;
2915 btn = dock->icon_array[0];
2916 moveDock(dock, btn->x_pos, btn->y_pos);
2918 newlevel = dock->lowered ? WMNormalLevel : WMDockLevel;
2919 ChangeStackingLevel(btn->icon->core, newlevel);
2921 for (i=1; i<dock->max_icons; i++) {
2922 if (dock->icon_array[i]) {
2923 MoveInStackListAbove(dock->icon_array[i]->icon->core,
2924 btn->icon->core);
2925 break;
2929 if (!dock->collapsed) {
2930 for (i=1; i<dock->max_icons; i++) {
2931 if (dock->icon_array[i]) {
2932 XMapWindow(dpy, dock->icon_array[i]->icon->core->window);
2936 dock->mapped = 1;
2938 dockIconPaint(btn);
2942 void
2943 wDockLower(WDock *dock)
2945 int i;
2947 for (i=0; i<dock->max_icons; i++) {
2948 if (dock->icon_array[i])
2949 wLowerFrame(dock->icon_array[i]->icon->core);
2954 void
2955 wDockRaise(WDock *dock)
2957 int i;
2959 for (i=dock->max_icons-1; i>=0; i--) {
2960 if (dock->icon_array[i])
2961 wRaiseFrame(dock->icon_array[i]->icon->core);
2966 void
2967 wDockRaiseLower(WDock *dock)
2969 if (!dock->icon_array[0]->icon->core->stacking->above
2970 ||(dock->icon_array[0]->icon->core->stacking->window_level
2971 !=dock->icon_array[0]->icon->core->stacking->above->stacking->window_level))
2972 wDockLower(dock);
2973 else
2974 wDockRaise(dock);
2978 void
2979 wDockFinishLaunch(WDock *dock, WAppIcon *icon)
2981 icon->launching = 0;
2982 icon->relaunching = 0;
2983 dockIconPaint(icon);
2987 WAppIcon*
2988 wDockFindIconFor(WDock *dock, Window window)
2990 WAppIcon *icon;
2991 int i;
2993 for (i=0; i<dock->max_icons; i++) {
2994 icon = dock->icon_array[i];
2995 if (icon && icon->main_window == window)
2996 return icon;
2998 return NULL;
3002 void
3003 wDockTrackWindowLaunch(WDock *dock, Window window)
3005 WAppIcon *icon;
3006 #ifdef REDUCE_APPICONS
3007 WAppIconAppList *tapplist;
3008 #endif
3009 char *wm_class, *wm_instance;
3010 int i;
3011 Bool firstPass = True;
3012 Bool found = False;
3013 char *command = NULL;
3016 int argc;
3017 char **argv;
3019 if (XGetCommand(dpy, window, &argv, &argc)) {
3020 if (argc > 0 && argv != NULL)
3021 command = FlattenStringList(argv,argc);
3022 if (argv) {
3023 XFreeStringList(argv);
3028 if (!PropGetWMClass(window, &wm_class, &wm_instance) ||
3029 (!wm_class && !wm_instance))
3030 return;
3032 retry:
3033 for (i=0; i<dock->max_icons; i++) {
3034 icon = dock->icon_array[i];
3035 if (!icon)
3036 continue;
3038 /* app is already attached to icon */
3039 if (icon->main_window == window) {
3040 found = True;
3041 break;
3044 if ((icon->wm_instance || icon->wm_class)
3045 && (icon->launching
3046 || (dock->screen_ptr->flags.startup && !icon->running))) {
3048 if (icon->wm_instance && wm_instance &&
3049 strcmp(icon->wm_instance, wm_instance)!=0) {
3050 continue;
3052 if (icon->wm_class && wm_class &&
3053 strcmp(icon->wm_class, wm_class)!=0) {
3054 continue;
3056 if (firstPass && command && strcmp(icon->command, command)!=0) {
3057 continue;
3060 if (!icon->relaunching) {
3061 WApplication *wapp;
3063 /* Possibly an application that was docked with dockit,
3064 * but the user did not update WMState to indicate that
3065 * it was docked by force */
3066 wapp = wApplicationOf(window);
3067 if (!wapp) {
3068 icon->forced_dock = 1;
3069 icon->running = 0;
3071 if (!icon->forced_dock)
3072 icon->main_window = window;
3074 #ifdef REDUCE_APPICONS
3075 tapplist = wmalloc(sizeof(WAppIconAppList));
3076 memset(tapplist, 0, sizeof(WAppIconAppList));
3077 tapplist->next = icon->applist;
3078 if (icon->applist)
3079 icon->applist->prev = tapplist;
3080 icon->applist = tapplist;
3081 tapplist->wapp = wApplicationOf(window);
3082 icon->num_apps++;
3083 #endif
3085 found = True;
3086 wDockFinishLaunch(dock, icon);
3087 break;
3091 if (firstPass && !found) {
3092 firstPass = False;
3093 goto retry;
3096 if (command)
3097 free(command);
3099 if (wm_class)
3100 XFree(wm_class);
3101 if (wm_instance)
3102 XFree(wm_instance);
3107 void
3108 wClipUpdateForWorkspaceChange(WScreen *scr, int workspace)
3110 if (!wPreferences.flags.noclip) {
3111 scr->clip_icon->dock = scr->workspaces[workspace]->clip;
3112 if (scr->current_workspace != workspace) {
3113 WDock *old_clip = scr->workspaces[scr->current_workspace]->clip;
3114 WAppIconChain *chain = scr->global_icons;
3116 while (chain) {
3117 moveIconBetweenDocks(chain->aicon->dock,
3118 scr->workspaces[workspace]->clip,
3119 chain->aicon, chain->aicon->xindex,
3120 chain->aicon->yindex);
3121 if (scr->workspaces[workspace]->clip->collapsed)
3122 XUnmapWindow(dpy, chain->aicon->icon->core->window);
3123 chain = chain->next;
3126 wDockHideIcons(old_clip);
3127 if (old_clip->auto_raise_lower) {
3128 if (old_clip->auto_raise_magic) {
3129 WMDeleteTimerHandler(old_clip->auto_raise_magic);
3130 old_clip->auto_raise_magic = NULL;
3132 wDockLower(old_clip);
3134 if (old_clip->auto_collapse) {
3135 if (old_clip->auto_expand_magic) {
3136 WMDeleteTimerHandler(old_clip->auto_expand_magic);
3137 old_clip->auto_expand_magic = NULL;
3139 old_clip->collapsed = 1;
3141 wDockShowIcons(scr->workspaces[workspace]->clip);
3143 if (scr->flags.clip_balloon_mapped)
3144 showClipBalloon(scr->clip_icon->dock, workspace);
3150 static void
3151 trackDeadProcess(pid_t pid, unsigned char status, WDock *dock)
3153 WAppIcon *icon;
3154 int i;
3156 for (i=0; i<dock->max_icons; i++) {
3157 icon = dock->icon_array[i];
3158 if (!icon)
3159 continue;
3161 if (icon->launching && icon->pid == pid) {
3162 if (!icon->relaunching) {
3163 icon->running = 0;
3164 icon->main_window = None;
3166 wDockFinishLaunch(dock, icon);
3167 icon->pid = 0;
3168 if (status==111) {
3169 char msg[PATH_MAX];
3170 #ifdef OFFIX_DND
3171 sprintf(msg, _("Could not execute command \"%s\""),
3172 icon->drop_launch && icon->dnd_command
3173 ? icon->dnd_command : icon->command);
3174 #else
3175 sprintf(msg, _("Could not execute command \"%s\""),
3176 icon->command);
3177 #endif
3178 wMessageDialog(dock->screen_ptr, _("Error"), msg,
3179 _("OK"), NULL, NULL);
3181 break;
3187 static void
3188 toggleLowered(WDock *dock)
3190 WAppIcon *tmp;
3191 int newlevel, i;
3193 /* lower/raise Dock */
3194 if (!dock->lowered) {
3195 newlevel = WMNormalLevel;
3196 dock->lowered = 1;
3197 } else {
3198 newlevel = WMDockLevel;
3199 dock->lowered = 0;
3202 for (i=0; i<dock->max_icons; i++) {
3203 tmp = dock->icon_array[i];
3204 if (!tmp)
3205 continue;
3207 ChangeStackingLevel(tmp->icon->core, newlevel);
3208 if (dock->lowered)
3209 wLowerFrame(tmp->icon->core);
3212 if (dock->type == WM_DOCK)
3213 wScreenUpdateUsableArea(dock->screen_ptr);
3217 static void
3218 toggleCollapsed(WDock *dock)
3220 if (dock->collapsed) {
3221 dock->collapsed = 0;
3222 wDockShowIcons(dock);
3224 else {
3225 dock->collapsed = 1;
3226 wDockHideIcons(dock);
3231 static void
3232 openDockMenu(WDock *dock, WAppIcon *aicon, XEvent *event)
3234 WScreen *scr = dock->screen_ptr;
3235 WObjDescriptor *desc;
3236 WMenuEntry *entry;
3237 WApplication *wapp = NULL;
3238 int index = 0;
3239 int x_pos;
3240 int appIsRunning = aicon->running && aicon->icon && aicon->icon->owner;
3242 if (dock->type == WM_DOCK) {
3243 /* keep on top */
3244 entry = dock->menu->entries[index];
3245 entry->flags.indicator_on = !dock->lowered;
3246 entry->clientdata = dock;
3247 } else {
3248 /* clip options */
3249 if (scr->clip_options)
3250 updateClipOptionsMenu(scr->clip_options, dock);
3252 /* Rename Workspace */
3253 entry = dock->menu->entries[++index];
3254 entry->clientdata = dock;
3256 /* select icon */
3257 entry = dock->menu->entries[++index];
3258 entry->clientdata = aicon;
3259 wMenuSetEnabled(dock->menu, index, aicon!=scr->clip_icon);
3261 /* (un)select all icons */
3262 entry = dock->menu->entries[++index];
3263 entry->clientdata = aicon;
3264 wMenuSetEnabled(dock->menu, index, dock->icon_count > 1);
3266 /* keep icon(s) */
3267 entry = dock->menu->entries[++index];
3268 entry->clientdata = aicon;
3269 wMenuSetEnabled(dock->menu, index, dock->icon_count > 1);
3271 /* this is the workspace submenu part */
3272 if (scr->clip_submenu)
3273 updateWorkspaceMenu(scr->clip_submenu, aicon);
3274 wMenuSetEnabled(dock->menu, ++index, !aicon->omnipresent);
3276 /* remove icon(s) */
3277 entry = dock->menu->entries[++index];
3278 entry->clientdata = aicon;
3279 wMenuSetEnabled(dock->menu, index, dock->icon_count > 1);
3281 /* attract icon(s) */
3282 entry = dock->menu->entries[++index];
3283 entry->clientdata = aicon;
3286 /* launch */
3287 entry = dock->menu->entries[++index];
3288 entry->clientdata = aicon;
3289 wMenuSetEnabled(dock->menu, index, aicon->command!=NULL);
3291 /* unhide here */
3292 entry = dock->menu->entries[++index];
3293 entry->clientdata = aicon;
3294 wMenuSetEnabled(dock->menu, index, appIsRunning);
3296 /* hide */
3297 entry = dock->menu->entries[++index];
3298 entry->clientdata = aicon;
3299 if (aicon->icon->owner) {
3300 wapp = wApplicationOf(aicon->icon->owner->main_window);
3301 if (wapp && wapp->flags.hidden)
3302 entry->text = _("Unhide");
3303 else
3304 entry->text = _("Hide");
3305 } else {
3306 entry->text = _("Hide");
3308 wMenuSetEnabled(dock->menu, index, appIsRunning);
3310 /* settings */
3311 entry = dock->menu->entries[++index];
3312 entry->clientdata = aicon;
3313 wMenuSetEnabled(dock->menu, index, !aicon->editing
3314 && !wPreferences.flags.noupdates);
3316 /* kill */
3317 entry = dock->menu->entries[++index];
3318 entry->clientdata = aicon;
3319 wMenuSetEnabled(dock->menu, index, appIsRunning);
3321 if (!dock->menu->flags.realized)
3322 wMenuRealize(dock->menu);
3324 if (dock->type == WM_CLIP) {
3325 x_pos = event->xbutton.x_root+2;
3326 } else {
3327 x_pos = dock->on_right_side ?
3328 scr->scr_width - dock->menu->frame->core->width - 2 : 0;
3331 wMenuMapAt(dock->menu, x_pos, event->xbutton.y_root+2, False);
3333 /* allow drag select */
3334 event->xany.send_event = True;
3335 desc = &dock->menu->menu->descriptor;
3336 (*desc->handle_mousedown)(desc, event);
3340 static void
3341 openClipWorkspaceMenu(WScreen *scr, int x, int y)
3343 if (!scr->clip_ws_menu) {
3344 scr->clip_ws_menu = wWorkspaceMenuMake(scr, False);
3346 wWorkspaceMenuUpdate(scr, scr->clip_ws_menu);
3347 wMenuMapAt(scr->clip_ws_menu, x, y, False);
3351 /******************************************************************/
3352 static void
3353 iconDblClick(WObjDescriptor *desc, XEvent *event)
3355 WAppIcon *btn = desc->parent;
3356 WDock *dock = btn->dock;
3357 WApplication *wapp = NULL;
3358 int unhideHere = 0;
3360 #ifdef REDUCE_APPICONS
3361 if ((btn->icon->owner && !(event->xbutton.state & ControlMask)) ||
3362 ((btn->icon->owner == NULL) && (btn->applist != NULL))) {
3363 if (btn->icon->owner == NULL)
3364 btn->icon->owner = btn->applist->wapp->main_window_desc;
3366 #else
3367 if (btn->icon->owner && !(event->xbutton.state & ControlMask)) {
3368 #endif
3369 wapp = wApplicationOf(btn->icon->owner->main_window);
3371 assert(wapp!=NULL);
3373 unhideHere = (event->xbutton.state & ShiftMask);
3375 /* go to the last workspace that the user worked on the app */
3376 if (wapp->last_workspace != dock->screen_ptr->current_workspace
3377 && !unhideHere) {
3378 wWorkspaceChange(dock->screen_ptr, wapp->last_workspace);
3381 wUnhideApplication(wapp, event->xbutton.button==Button2,
3382 unhideHere);
3384 if (event->xbutton.state & MOD_MASK) {
3385 wHideOtherApplications(btn->icon->owner);
3387 } else {
3388 if (event->xbutton.button==Button1) {
3390 if (event->xbutton.state & MOD_MASK) {
3391 /* raise/lower dock */
3392 toggleLowered(dock);
3393 } else if (btn == dock->screen_ptr->clip_icon) {
3394 if (getClipButton(event->xbutton.x, event->xbutton.y)==CLIP_IDLE)
3395 toggleCollapsed(dock);
3396 else
3397 handleClipChangeWorkspace(dock->screen_ptr, event);
3398 } else if (btn->command) {
3399 if (!btn->launching &&
3400 (!btn->running || (event->xbutton.state & ControlMask))) {
3401 launchDockedApplication(btn);
3403 } else if (btn->xindex == 0 && btn->yindex == 0
3404 && btn->dock->type == WM_DOCK) {
3406 wShowGNUstepPanel(dock->screen_ptr);
3413 static void
3414 handleDockMove(WDock *dock, WAppIcon *aicon, XEvent *event)
3416 WScreen *scr = dock->screen_ptr;
3417 int ofs_x=event->xbutton.x, ofs_y=event->xbutton.y;
3418 int x, y;
3419 XEvent ev;
3420 int grabbed = 0, swapped = 0, done;
3421 Pixmap ghost = None;
3422 int superfluous = wPreferences.superfluous; /* we catch it to avoid problems */
3424 #ifdef DEBUG
3425 puts("moving dock");
3426 #endif
3427 if (XGrabPointer(dpy, aicon->icon->core->window, True, ButtonMotionMask
3428 |ButtonReleaseMask|ButtonPressMask, GrabModeAsync,
3429 GrabModeAsync, None, None, CurrentTime) !=GrabSuccess) {
3430 wwarning("pointer grab failed for dock move");
3432 y = 0;
3433 for (x=0; x<dock->max_icons; x++) {
3434 if (dock->icon_array[x]!=NULL &&
3435 dock->icon_array[x]->yindex > y)
3436 y = dock->icon_array[x]->yindex;
3438 y++;
3439 XResizeWindow(dpy, scr->dock_shadow, ICON_SIZE, ICON_SIZE*y);
3441 done = 0;
3442 while (!done) {
3443 WMMaskEvent(dpy, PointerMotionMask|ButtonReleaseMask|ButtonPressMask
3444 |ButtonMotionMask|ExposureMask, &ev);
3445 switch (ev.type) {
3446 case Expose:
3447 WMHandleEvent(&ev);
3448 break;
3450 case MotionNotify:
3451 if (!grabbed) {
3452 if (abs(ofs_x-ev.xmotion.x)>=MOVE_THRESHOLD
3453 || abs(ofs_y-ev.xmotion.y)>=MOVE_THRESHOLD) {
3454 XChangeActivePointerGrab(dpy, ButtonMotionMask
3455 |ButtonReleaseMask|ButtonPressMask,
3456 wCursor[WCUR_MOVE], CurrentTime);
3457 grabbed=1;
3459 break;
3461 if (dock->type == WM_CLIP) {
3462 if (ev.xmotion.x_root - ofs_x < 0) {
3463 x = 0;
3464 } else if (ev.xmotion.x_root - ofs_x + ICON_SIZE >
3465 scr->scr_width) {
3466 x = scr->scr_width - ICON_SIZE;
3467 } else {
3468 x = ev.xmotion.x_root - ofs_x;
3470 if (ev.xmotion.y_root - ofs_y < 0) {
3471 y = 0;
3472 } else if (ev.xmotion.y_root - ofs_y + ICON_SIZE >
3473 scr->scr_height) {
3474 y = scr->scr_height - ICON_SIZE;
3475 } else {
3476 y = ev.xmotion.y_root - ofs_y;
3478 moveDock(dock, x, y);
3479 } else {
3480 /* move vertically if pointer is inside the dock*/
3481 if ((dock->on_right_side &&
3482 ev.xmotion.x_root >= dock->x_pos - ICON_SIZE)
3483 || (!dock->on_right_side &&
3484 ev.xmotion.x_root <= dock->x_pos + ICON_SIZE*2)) {
3486 if (ev.xmotion.y_root - ofs_y < 0) {
3487 y = 0;
3488 } else if (ev.xmotion.y_root - ofs_y + ICON_SIZE >
3489 scr->scr_height) {
3490 y = scr->scr_height - ICON_SIZE;
3491 } else {
3492 y = ev.xmotion.y_root - ofs_y;
3494 moveDock(dock, dock->x_pos, y);
3496 /* move horizontally to change sides */
3497 x = ev.xmotion.x_root - ofs_x;
3498 if (!dock->on_right_side) {
3500 /* is on left */
3502 if (ev.xmotion.x_root > dock->x_pos + ICON_SIZE*2) {
3503 XMoveWindow(dpy, scr->dock_shadow, scr->scr_width-ICON_SIZE
3504 -DOCK_EXTRA_SPACE-1, dock->y_pos);
3505 if (superfluous && ghost==None) {
3506 ghost = MakeGhostDock(dock, dock->x_pos,
3507 scr->scr_width-ICON_SIZE
3508 -DOCK_EXTRA_SPACE-1,
3509 dock->y_pos);
3510 XSetWindowBackgroundPixmap(dpy, scr->dock_shadow,
3511 ghost);
3512 XClearWindow(dpy, scr->dock_shadow);
3514 XMapRaised(dpy, scr->dock_shadow);
3515 swapped = 1;
3516 } else {
3517 if (superfluous && ghost!=None) {
3518 XFreePixmap(dpy, ghost);
3519 ghost = None;
3521 XUnmapWindow(dpy, scr->dock_shadow);
3522 swapped = 0;
3524 } else {
3525 /* is on right */
3526 if (ev.xmotion.x_root < dock->x_pos - ICON_SIZE) {
3527 XMoveWindow(dpy, scr->dock_shadow,
3528 DOCK_EXTRA_SPACE, dock->y_pos);
3529 if (superfluous && ghost==None) {
3530 ghost = MakeGhostDock(dock, dock->x_pos,
3531 DOCK_EXTRA_SPACE, dock->y_pos);
3532 XSetWindowBackgroundPixmap(dpy, scr->dock_shadow,
3533 ghost);
3534 XClearWindow(dpy, scr->dock_shadow);
3536 XMapRaised(dpy, scr->dock_shadow);
3537 swapped = -1;
3538 } else {
3539 XUnmapWindow(dpy, scr->dock_shadow);
3540 swapped = 0;
3541 if (superfluous && ghost!=None) {
3542 XFreePixmap(dpy, ghost);
3543 ghost = None;
3548 break;
3550 case ButtonPress:
3551 break;
3553 case ButtonRelease:
3554 if (ev.xbutton.button != event->xbutton.button)
3555 break;
3556 XUngrabPointer(dpy, CurrentTime);
3557 XUnmapWindow(dpy, scr->dock_shadow);
3558 XResizeWindow(dpy, scr->dock_shadow, ICON_SIZE, ICON_SIZE);
3559 if (dock->type == WM_DOCK) {
3560 if (swapped!=0) {
3561 if (swapped>0)
3562 dock->on_right_side = 1;
3563 else
3564 dock->on_right_side = 0;
3565 swapDock(dock);
3566 wArrangeIcons(scr, False);
3569 done = 1;
3570 break;
3573 if (superfluous) {
3574 if (ghost!=None)
3575 XFreePixmap(dpy, ghost);
3576 XSetWindowBackground(dpy, scr->dock_shadow, scr->white_pixel);
3578 #ifdef DEBUG
3579 puts("End dock move");
3580 #endif
3585 static void
3586 handleIconMove(WDock *dock, WAppIcon *aicon, XEvent *event)
3588 WScreen *scr = dock->screen_ptr;
3589 Window wins[2];
3590 WIcon *icon = aicon->icon;
3591 WDock *dock2 = NULL, *last_dock = dock, *clip = NULL;
3592 int ondock, grabbed = 0, change_dock = 0, collapsed = 0;
3593 XEvent ev;
3594 int x = aicon->x_pos, y = aicon->y_pos;
3595 int ofs_x = event->xbutton.x, ofs_y = event->xbutton.y;
3596 int shad_x = x, shad_y = y;
3597 int ix = aicon->xindex, iy = aicon->yindex;
3598 int tmp;
3599 Pixmap ghost = None;
3600 Bool docked;
3601 int superfluous = wPreferences.superfluous; /* we catch it to avoid problems */
3602 int omnipresent = aicon->omnipresent; /* this must be cached!!! */
3605 if (wPreferences.flags.noupdates)
3606 return;
3608 if (XGrabPointer(dpy, icon->core->window, True, ButtonMotionMask
3609 |ButtonReleaseMask|ButtonPressMask, GrabModeAsync,
3610 GrabModeAsync, None, None, CurrentTime) !=GrabSuccess) {
3611 #ifdef DEBUG0
3612 wwarning("pointer grab failed for icon move");
3613 #endif
3616 if (!(event->xbutton.state & MOD_MASK))
3617 wRaiseFrame(icon->core);
3619 if (!wPreferences.flags.noclip)
3620 clip = scr->workspaces[scr->current_workspace]->clip;
3622 if (dock == scr->dock && !wPreferences.flags.noclip)
3623 dock2 = clip;
3624 else if (dock != scr->dock && !wPreferences.flags.nodock)
3625 dock2 = scr->dock;
3627 wins[0] = icon->core->window;
3628 wins[1] = scr->dock_shadow;
3629 XRestackWindows(dpy, wins, 2);
3630 XMoveResizeWindow(dpy, scr->dock_shadow, aicon->x_pos, aicon->y_pos,
3631 ICON_SIZE, ICON_SIZE);
3632 if (superfluous) {
3633 if (icon->pixmap!=None)
3634 ghost = MakeGhostIcon(scr, icon->pixmap);
3635 else
3636 ghost = MakeGhostIcon(scr, icon->core->window);
3638 XSetWindowBackgroundPixmap(dpy, scr->dock_shadow, ghost);
3639 XClearWindow(dpy, scr->dock_shadow);
3641 XMapWindow(dpy, scr->dock_shadow);
3643 ondock = 1;
3646 while(1) {
3647 XMaskEvent(dpy, PointerMotionMask|ButtonReleaseMask|ButtonPressMask
3648 |ButtonMotionMask|ExposureMask, &ev);
3649 switch (ev.type) {
3650 case Expose:
3651 WMHandleEvent(&ev);
3652 break;
3654 case MotionNotify:
3655 if (!grabbed) {
3656 if (abs(ofs_x-ev.xmotion.x)>=MOVE_THRESHOLD
3657 || abs(ofs_y-ev.xmotion.y)>=MOVE_THRESHOLD) {
3658 XChangeActivePointerGrab(dpy, ButtonMotionMask
3659 |ButtonReleaseMask|ButtonPressMask,
3660 wCursor[WCUR_MOVE], CurrentTime);
3661 grabbed=1;
3662 } else {
3663 break;
3667 if (omnipresent) {
3668 int i;
3669 for (i=0; i<scr->workspace_count; i++) {
3670 if (i == scr->current_workspace)
3671 continue;
3672 wDockShowIcons(scr->workspaces[i]->clip);
3676 x = ev.xmotion.x_root - ofs_x;
3677 y = ev.xmotion.y_root - ofs_y;
3678 tmp = wDockSnapIcon(dock, aicon, x, y, &ix, &iy, True);
3679 if (tmp && dock2) {
3680 change_dock = 0;
3681 if (last_dock != dock && collapsed) {
3682 last_dock->collapsed = 1;
3683 wDockHideIcons(last_dock);
3684 collapsed = 0;
3686 if (!collapsed && (collapsed = dock->collapsed)) {
3687 dock->collapsed = 0;
3688 wDockShowIcons(dock);
3690 if (dock->auto_raise_lower)
3691 wDockRaise(dock);
3692 last_dock = dock;
3693 } else if (dock2) {
3694 tmp = wDockSnapIcon(dock2, aicon, x, y, &ix, &iy, False);
3695 if (tmp) {
3696 change_dock = 1;
3697 if (last_dock != dock2 && collapsed) {
3698 last_dock->collapsed = 1;
3699 wDockHideIcons(last_dock);
3700 collapsed = 0;
3702 if (!collapsed && (collapsed = dock2->collapsed)) {
3703 dock2->collapsed = 0;
3704 wDockShowIcons(dock2);
3706 if (dock2->auto_raise_lower)
3707 wDockRaise(dock2);
3708 last_dock = dock2;
3711 if (aicon->launching
3712 || (aicon->running && !(ev.xmotion.state & MOD_MASK))
3713 || (!aicon->running && tmp)) {
3714 shad_x = last_dock->x_pos + ix*wPreferences.icon_size;
3715 shad_y = last_dock->y_pos + iy*wPreferences.icon_size;
3717 XMoveWindow(dpy, scr->dock_shadow, shad_x, shad_y);
3719 if (!ondock) {
3720 XMapWindow(dpy, scr->dock_shadow);
3722 ondock = 1;
3723 } else {
3724 if (ondock) {
3725 XUnmapWindow(dpy, scr->dock_shadow);
3727 ondock = 0;
3729 XMoveWindow(dpy, icon->core->window, x, y);
3730 break;
3732 case ButtonPress:
3733 break;
3735 case ButtonRelease:
3736 if (ev.xbutton.button != event->xbutton.button)
3737 break;
3738 XUngrabPointer(dpy, CurrentTime);
3739 if (ondock) {
3740 SlideWindow(icon->core->window, x, y, shad_x, shad_y);
3741 XUnmapWindow(dpy, scr->dock_shadow);
3742 if (!change_dock) {
3743 reattachIcon(dock, aicon, ix, iy);
3744 if (clip && dock!=clip && clip->auto_raise_lower)
3745 wDockLower(clip);
3746 } else {
3747 docked = moveIconBetweenDocks(dock, dock2, aicon, ix, iy);
3748 if (!docked) {
3749 /* Slide it back if dock rejected it */
3750 SlideWindow(icon->core->window, x, y, aicon->x_pos,
3751 aicon->y_pos);
3752 reattachIcon(dock, aicon, aicon->xindex,aicon->yindex);
3754 if (last_dock->type==WM_CLIP && last_dock->auto_collapse) {
3755 collapsed = 0;
3758 } else {
3759 aicon->x_pos = x;
3760 aicon->y_pos = y;
3761 if (superfluous) {
3762 if (!aicon->running && !wPreferences.no_animations) {
3763 /* We need to deselect it, even if is deselected in
3764 * wDockDetach(), because else DoKaboom() will fail.
3766 if (aicon->icon->selected)
3767 wIconSelect(aicon->icon);
3768 DoKaboom(scr,aicon->icon->core->window, x, y);
3771 if (clip && clip->auto_raise_lower)
3772 wDockLower(clip);
3773 wDockDetach(dock, aicon);
3775 if (collapsed) {
3776 last_dock->collapsed = 1;
3777 wDockHideIcons(last_dock);
3778 collapsed = 0;
3780 if (superfluous) {
3781 if (ghost!=None)
3782 XFreePixmap(dpy, ghost);
3783 XSetWindowBackground(dpy, scr->dock_shadow, scr->white_pixel);
3785 if (omnipresent) {
3786 int i;
3787 for (i=0; i<scr->workspace_count; i++) {
3788 if (i == scr->current_workspace)
3789 continue;
3790 wDockHideIcons(scr->workspaces[i]->clip);
3794 #ifdef DEBUG
3795 puts("End icon move");
3796 #endif
3797 return;
3803 static int
3804 getClipButton(int px, int py)
3806 int pt = (CLIP_BUTTON_SIZE+2)*ICON_SIZE/64;
3808 if (px < 0 || py < 0 || px >= ICON_SIZE || py >= ICON_SIZE)
3809 return CLIP_IDLE;
3811 if (py <= pt-((int)ICON_SIZE-1-px))
3812 return CLIP_FORWARD;
3813 else if (px <= pt-((int)ICON_SIZE-1-py))
3814 return CLIP_REWIND;
3816 return CLIP_IDLE;
3820 static void
3821 handleClipChangeWorkspace(WScreen *scr, XEvent *event)
3823 XEvent ev;
3824 int done, direction, new_ws;
3825 int new_dir;
3826 WDock *clip = scr->clip_icon->dock;
3828 direction = getClipButton(event->xbutton.x, event->xbutton.y);
3830 clip->lclip_button_pushed = direction==CLIP_REWIND;
3831 clip->rclip_button_pushed = direction==CLIP_FORWARD;
3833 wClipIconPaint(scr->clip_icon);
3834 done = 0;
3835 while(!done) {
3836 WMMaskEvent(dpy, ExposureMask|ButtonMotionMask|ButtonReleaseMask
3837 |ButtonPressMask, &ev);
3838 switch (ev.type) {
3839 case Expose:
3840 WMHandleEvent(&ev);
3841 break;
3843 case MotionNotify:
3844 new_dir = getClipButton(ev.xmotion.x, ev.xmotion.y);
3845 if (new_dir != direction) {
3846 direction = new_dir;
3847 clip->lclip_button_pushed = direction==CLIP_REWIND;
3848 clip->rclip_button_pushed = direction==CLIP_FORWARD;
3849 wClipIconPaint(scr->clip_icon);
3851 break;
3853 case ButtonPress:
3854 break;
3856 case ButtonRelease:
3857 if (ev.xbutton.button == event->xbutton.button)
3858 done = 1;
3862 clip->lclip_button_pushed = 0;
3863 clip->rclip_button_pushed = 0;
3865 new_ws = wPreferences.ws_advance || (event->xbutton.state & ControlMask);
3867 if (direction == CLIP_FORWARD) {
3868 if (scr->current_workspace < scr->workspace_count-1)
3869 wWorkspaceChange(scr, scr->current_workspace+1);
3870 else if (new_ws && scr->current_workspace < MAX_WORKSPACES-1)
3871 wWorkspaceChange(scr, scr->current_workspace+1);
3872 else if (wPreferences.ws_cycle)
3873 wWorkspaceChange(scr, 0);
3875 else if (direction == CLIP_REWIND) {
3876 if (scr->current_workspace > 0)
3877 wWorkspaceChange(scr, scr->current_workspace-1);
3878 else if (scr->current_workspace==0 && wPreferences.ws_cycle)
3879 wWorkspaceChange(scr, scr->workspace_count-1);
3882 wClipIconPaint(scr->clip_icon);
3886 static void
3887 iconMouseDown(WObjDescriptor *desc, XEvent *event)
3889 WAppIcon *aicon = desc->parent;
3890 WDock *dock = aicon->dock;
3891 WScreen *scr = aicon->icon->core->screen_ptr;
3893 if (aicon->editing || WCHECK_STATE(WSTATE_MODAL))
3894 return;
3896 scr->last_dock = dock;
3898 if (dock->menu->flags.mapped)
3899 wMenuUnmap(dock->menu);
3901 if (IsDoubleClick(scr, event)) {
3902 /* double-click was not in the main clip icon */
3903 if (dock->type != WM_CLIP || aicon->xindex!=0 || aicon->yindex!=0
3904 || getClipButton(event->xbutton.x, event->xbutton.y)==CLIP_IDLE) {
3905 iconDblClick(desc, event);
3906 return;
3910 if (dock->type == WM_CLIP && scr->flags.clip_balloon_mapped) {
3911 XUnmapWindow(dpy, scr->clip_balloon);
3912 scr->flags.clip_balloon_mapped = 0;
3915 #ifdef DEBUG
3916 puts("handling dock");
3917 #endif
3918 if (event->xbutton.button == Button1) {
3919 if (event->xbutton.state & MOD_MASK)
3920 wDockLower(dock);
3921 else
3922 wDockRaise(dock);
3924 if ((event->xbutton.state & ShiftMask) && aicon!=scr->clip_icon &&
3925 dock->type!=WM_DOCK) {
3926 wIconSelect(aicon->icon);
3927 return;
3930 if (aicon->yindex==0 && aicon->xindex==0) {
3931 if (getClipButton(event->xbutton.x, event->xbutton.y)!=CLIP_IDLE
3932 && dock->type==WM_CLIP)
3933 handleClipChangeWorkspace(scr, event);
3934 else
3935 handleDockMove(dock, aicon, event);
3936 } else
3937 handleIconMove(dock, aicon, event);
3939 } else if (event->xbutton.button==Button2 && dock->type==WM_CLIP &&
3940 aicon==scr->clip_icon) {
3941 openClipWorkspaceMenu(scr, event->xbutton.x_root+2,
3942 event->xbutton.y_root+2);
3943 if (scr->clip_ws_menu) {
3944 WMenu *menu;
3945 menu = scr->clip_ws_menu;
3946 desc = &menu->menu->descriptor;
3948 event->xany.send_event = True;
3949 (*desc->handle_mousedown)(desc, event);
3951 #if 1
3952 } else if (event->xbutton.button==Button2 && dock->type==WM_CLIP &&
3953 (event->xbutton.state & ShiftMask) && aicon!=scr->clip_icon) {
3954 wClipMakeIconOmnipresent(aicon, !aicon->omnipresent);
3955 #endif
3956 } else if (event->xbutton.button == Button3) {
3957 openDockMenu(dock, aicon, event);
3962 static void
3963 showClipBalloon(WDock *dock, int workspace)
3965 int w, h;
3966 int x, y;
3967 WScreen *scr = dock->screen_ptr;
3968 char *text;
3969 Window stack[2];
3971 scr->flags.clip_balloon_mapped = 1;
3972 XMapWindow(dpy, scr->clip_balloon);
3974 text = scr->workspaces[workspace]->name;
3976 w = wTextWidth(scr->clip_title_font->font, text, strlen(text));
3978 h = scr->clip_title_font->height;
3979 XResizeWindow(dpy, scr->clip_balloon, w, h);
3981 x = dock->x_pos + CLIP_BUTTON_SIZE*ICON_SIZE/64;
3982 y = dock->y_pos + ICON_SIZE-scr->clip_title_font->height - 3;
3984 if (x+w > scr->scr_width) {
3985 x = scr->scr_width - w;
3986 if (dock->y_pos + ICON_SIZE + h > scr->scr_height)
3987 y = dock->y_pos - h - 1;
3988 else
3989 y = dock->y_pos + ICON_SIZE;
3990 XRaiseWindow(dpy, scr->clip_balloon);
3991 } else {
3992 stack[0] = scr->clip_icon->icon->core->window;
3993 stack[1] = scr->clip_balloon;
3994 XRestackWindows(dpy, stack, 2);
3996 XMoveWindow(dpy, scr->clip_balloon, x, y);
3997 XSetForeground(dpy, scr->clip_title_gc,
3998 scr->clip_title_pixel[CLIP_NORMAL]);
3999 XClearWindow(dpy, scr->clip_balloon);
4000 wDrawString(scr->clip_balloon, scr->clip_title_font, scr->clip_title_gc,
4001 0, scr->clip_title_font->y, text, strlen(text));
4005 static void
4006 clipEnterNotify(WObjDescriptor *desc, XEvent *event)
4008 WAppIcon *btn = (WAppIcon*)desc->parent;
4009 WDock *dock;
4010 WScreen *scr;
4012 assert(event->type==EnterNotify);
4014 if(desc->parent_type!=WCLASS_DOCK_ICON)
4015 return;
4017 scr = btn->icon->core->screen_ptr;
4018 if (!btn->omnipresent)
4019 dock = btn->dock;
4020 else
4021 dock = scr->workspaces[scr->current_workspace]->clip;
4023 if (!dock || dock->type!=WM_CLIP)
4024 return;
4026 /* The auto raise/lower code */
4027 if (dock->auto_lower_magic) {
4028 WMDeleteTimerHandler(dock->auto_lower_magic);
4029 dock->auto_lower_magic = NULL;
4031 if (dock->auto_raise_lower && !dock->auto_raise_magic) {
4032 dock->auto_raise_magic = WMAddTimerHandler(AUTO_RAISE_DELAY,
4033 clipAutoRaise,
4034 (void *)dock);
4037 /* The auto expand/collapse code */
4038 if (dock->auto_collapse_magic) {
4039 WMDeleteTimerHandler(dock->auto_collapse_magic);
4040 dock->auto_collapse_magic = NULL;
4042 if (dock->auto_collapse && !dock->auto_expand_magic) {
4043 dock->auto_expand_magic = WMAddTimerHandler(AUTO_EXPAND_DELAY,
4044 clipAutoExpand,
4045 (void *)dock);
4048 if (btn->xindex == 0 && btn->yindex == 0)
4049 showClipBalloon(dock, dock->screen_ptr->current_workspace);
4050 else {
4051 if (dock->screen_ptr->flags.clip_balloon_mapped) {
4052 XUnmapWindow(dpy, dock->screen_ptr->clip_balloon);
4053 dock->screen_ptr->flags.clip_balloon_mapped = 0;
4059 static void
4060 clipLeave(WDock *dock)
4062 XEvent event;
4063 WObjDescriptor *desc = NULL;
4065 if (!dock || dock->type!=WM_CLIP)
4066 return;
4068 if (XCheckTypedEvent(dpy, EnterNotify, &event)!=False) {
4069 if (XFindContext(dpy, event.xcrossing.window, wWinContext,
4070 (XPointer *)&desc)!=XCNOENT
4071 && desc && desc->parent_type==WCLASS_DOCK_ICON
4072 && ((WAppIcon*)desc->parent)->dock
4073 && ((WAppIcon*)desc->parent)->dock->type==WM_CLIP) {
4074 /* We didn't left the Clip yet */
4075 XPutBackEvent(dpy, &event);
4076 return;
4079 XPutBackEvent(dpy, &event);
4080 } else {
4081 /* We entered a withdrawn window, so we're still in Clip */
4082 return;
4085 if (dock->auto_raise_magic) {
4086 WMDeleteTimerHandler(dock->auto_raise_magic);
4087 dock->auto_raise_magic = NULL;
4089 if (dock->auto_raise_lower && !dock->auto_lower_magic) {
4090 dock->auto_lower_magic = WMAddTimerHandler(AUTO_LOWER_DELAY,
4091 clipAutoLower,
4092 (void *)dock);
4095 if (dock->auto_expand_magic) {
4096 WMDeleteTimerHandler(dock->auto_expand_magic);
4097 dock->auto_expand_magic = NULL;
4099 if (dock->auto_collapse && !dock->auto_collapse_magic) {
4100 dock->auto_collapse_magic = WMAddTimerHandler(AUTO_COLLAPSE_DELAY,
4101 clipAutoCollapse,
4102 (void *)dock);
4107 static void
4108 clipLeaveNotify(WObjDescriptor *desc, XEvent *event)
4110 WAppIcon *btn = (WAppIcon*)desc->parent;
4112 assert(event->type==LeaveNotify);
4114 if(desc->parent_type!=WCLASS_DOCK_ICON)
4115 return;
4117 clipLeave(btn->dock);
4121 static void
4122 clipAutoCollapse(void *cdata)
4124 WDock *dock = (WDock *)cdata;
4126 if (dock->type!=WM_CLIP)
4127 return;
4129 if (dock->auto_collapse) {
4130 dock->collapsed = 1;
4131 wDockHideIcons(dock);
4133 dock->auto_collapse_magic = NULL;
4137 static void
4138 clipAutoExpand(void *cdata)
4140 WDock *dock = (WDock *)cdata;
4142 if (dock->type!=WM_CLIP)
4143 return;
4145 if (dock->auto_collapse) {
4146 dock->collapsed = 0;
4147 wDockShowIcons(dock);
4149 dock->auto_expand_magic = NULL;
4153 static void
4154 clipAutoLower(void *cdata)
4156 WDock *dock = (WDock *)cdata;
4158 if (dock->type!=WM_CLIP)
4159 return;
4161 if (dock->auto_raise_lower)
4162 wDockLower(dock);
4164 dock->auto_lower_magic = NULL;
4168 static void
4169 clipAutoRaise(void *cdata)
4171 WDock *dock = (WDock *)cdata;
4173 if (dock->type!=WM_CLIP)
4174 return;
4176 if (dock->auto_raise_lower)
4177 wDockRaise(dock);
4179 if (dock->screen_ptr->flags.clip_balloon_mapped) {
4180 showClipBalloon(dock, dock->screen_ptr->current_workspace);
4183 dock->auto_raise_magic = NULL;
4187 static Bool
4188 iconCanBeOmnipresent(WAppIcon *aicon)
4190 WScreen *scr = aicon->icon->core->screen_ptr;
4191 WDock *clip;
4192 WAppIcon *btn;
4193 int i, j;
4194 Bool ocupied = False;
4196 for (i=0; i<scr->workspace_count; i++) {
4197 clip = scr->workspaces[i]->clip;
4199 if (clip == aicon->dock)
4200 continue;
4202 for (j=0; j<clip->max_icons; j++) {
4203 btn = clip->icon_array[j];
4204 if(btn && btn->xindex==aicon->xindex && btn->yindex==aicon->yindex) {
4205 ocupied = True;
4206 break;
4211 return !ocupied;
4216 wClipMakeIconOmnipresent(WAppIcon *aicon, int omnipresent)
4218 WScreen *scr = aicon->icon->core->screen_ptr;
4219 WAppIconChain *new_entry, *tmp, *tmp1;
4220 int status = WO_SUCCESS;
4222 if (aicon->dock == scr->dock)
4223 return WO_NOT_APPLICABLE;
4225 if (aicon->omnipresent == omnipresent)
4226 return WO_SUCCESS;
4228 if (omnipresent) {
4229 if (iconCanBeOmnipresent(aicon)) {
4230 aicon->omnipresent = 1;
4231 new_entry = wmalloc(sizeof(WAppIconChain));
4232 new_entry->aicon = aicon;
4233 new_entry->next = NULL;
4234 if (!scr->global_icons) {
4235 scr->global_icons = new_entry;
4236 } else {
4237 tmp = scr->global_icons;
4238 while (tmp->next)
4239 tmp = tmp->next;
4241 tmp->next = new_entry;
4243 } else {
4244 aicon->omnipresent = 0;
4245 status = WO_FAILED;
4247 } else {
4248 aicon->omnipresent = 0;
4249 if (aicon == scr->global_icons->aicon) {
4250 tmp = scr->global_icons->next;
4251 free(scr->global_icons);
4252 scr->global_icons = tmp;
4253 } else {
4254 tmp = scr->global_icons;
4255 while (tmp->next) {
4256 if (tmp->next->aicon == aicon) {
4257 tmp1 = tmp->next->next;
4258 free(tmp->next);
4259 tmp->next = tmp1;
4260 break;
4262 tmp = tmp->next;
4267 return status;