fixed cosmetic bug in geom. dpy window for 8bpp
[wmaker-crm.git] / src / dock.c
blobbd5b54a52c4b39b6bd2fb857418a0aafafde3b7a
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);
297 /* TODO: replace this function with a member of the dock struct */
298 static int
299 numberOfSelectedIcons(WDock *dock)
301 WAppIcon *aicon;
302 int i, n;
304 n = 0;
305 for (i=1; i<dock->max_icons; i++) {
306 aicon = dock->icon_array[i];
307 if (aicon && aicon->icon->selected) {
308 n++;
312 return n;
316 static LinkedList*
317 getSelected(WDock *dock)
319 LinkedList *ret=NULL;
320 WAppIcon *btn;
321 int i;
323 for (i=1; i<dock->max_icons; i++) {
324 btn = dock->icon_array[i];
325 if (btn && btn->icon->selected) {
326 ret = list_cons(btn, ret);
330 return ret;
334 static void
335 paintClipButtons(WAppIcon *clipIcon, Bool lpushed, Bool rpushed)
337 Window win = clipIcon->icon->core->window;
338 WScreen *scr = clipIcon->icon->core->screen_ptr;
339 XPoint p[4];
340 int pt = CLIP_BUTTON_SIZE*ICON_SIZE/64;
341 int tp = ICON_SIZE - pt;
342 int as = pt - 15; /* 15 = 5+5+5 */
343 GC gc = scr->clip_title_gc;
344 #ifdef GRADIENT_CLIP_ARROW
345 Bool collapsed = clipIcon->dock->collapsed;
346 #endif
348 if (rpushed) {
349 p[0].x = tp+1;
350 p[0].y = 1;
351 p[1].x = ICON_SIZE-2;
352 p[1].y = 1;
353 p[2].x = ICON_SIZE-2;
354 p[2].y = pt-1;
355 } else if (lpushed) {
356 p[0].x = 1;
357 p[0].y = tp;
358 p[1].x = pt;
359 p[1].y = ICON_SIZE-2;
360 p[2].x = 1;
361 p[2].y = ICON_SIZE-2;
363 if (lpushed || rpushed) {
364 XSetForeground(dpy, scr->draw_gc, scr->white_pixel);
365 XFillPolygon(dpy, win, scr->draw_gc, p, 3, Convex, CoordModeOrigin);
366 XSetForeground(dpy, scr->draw_gc, scr->black_pixel);
368 #ifdef GRADIENT_CLIP_ARROW
369 if (!collapsed) {
370 XSetFillStyle(dpy, scr->copy_gc, FillTiled);
371 XSetTile(dpy, scr->copy_gc, scr->clip_arrow_gradient);
372 XSetClipMask(dpy, scr->copy_gc, None);
373 gc = scr->copy_gc;
375 #endif /* GRADIENT_CLIP_ARROW */
377 /* top right arrow */
378 p[0].x = p[3].x = ICON_SIZE-5-as;
379 p[0].y = p[3].y = 5;
380 p[1].x = ICON_SIZE-6;
381 p[1].y = 5;
382 p[2].x = ICON_SIZE-6;
383 p[2].y = 4+as;
384 if (rpushed) {
385 XFillPolygon(dpy, win, scr->draw_gc, p, 3, Convex, CoordModeOrigin);
386 XDrawLines(dpy, win, scr->draw_gc, p, 4, CoordModeOrigin);
387 } else {
388 #ifdef GRADIENT_CLIP_ARROW
389 if (!collapsed)
390 XSetTSOrigin(dpy, gc, ICON_SIZE-6-as, 5);
391 #endif
392 XFillPolygon(dpy, win, gc, p,3,Convex,CoordModeOrigin);
393 XDrawLines(dpy, win, gc, p,4,CoordModeOrigin);
396 /* bottom left arrow */
397 p[0].x = p[3].x = 5;
398 p[0].y = p[3].y = ICON_SIZE-5-as;
399 p[1].x = 5;
400 p[1].y = ICON_SIZE-6;
401 p[2].x = 4+as;
402 p[2].y = ICON_SIZE-6;
403 if (lpushed) {
404 XFillPolygon(dpy, win, scr->draw_gc, p, 3, Convex, CoordModeOrigin);
405 XDrawLines(dpy, win, scr->draw_gc, p, 4, CoordModeOrigin);
406 } else {
407 #ifdef GRADIENT_CLIP_ARROW
408 if (!collapsed)
409 XSetTSOrigin(dpy, gc, 5, ICON_SIZE-6-as);
410 #endif
411 XFillPolygon(dpy, win, gc, p,3,Convex,CoordModeOrigin);
412 XDrawLines(dpy, win, gc, p,4,CoordModeOrigin);
414 #ifdef GRADIENT_CLIP_ARROW
415 if (!collapsed)
416 XSetFillStyle(dpy, scr->copy_gc, FillSolid);
417 #endif
421 RImage*
422 wClipMakeTile(WScreen *scr, RImage *normalTile)
424 RImage *tile = RCloneImage(normalTile);
425 RColor black;
426 RColor dark;
427 RColor light;
428 int pt, tp;
429 int as;
431 pt = CLIP_BUTTON_SIZE*wPreferences.icon_size/64;
432 tp = wPreferences.icon_size-1 - pt;
433 as = pt - 15;
435 black.alpha = 255;
436 black.red = black.green = black.blue = 0;
438 dark.alpha = 0;
439 dark.red = dark.green = dark.blue = 60;
441 light.alpha = 0;
442 light.red = light.green = light.blue = 80;
445 /* top right */
446 ROperateLine(tile, RSubtractOperation, tp, 0, wPreferences.icon_size-2,
447 pt-1, &dark);
448 RDrawLine(tile, tp-1, 0, wPreferences.icon_size-1, pt+1, &black);
449 ROperateLine(tile, RAddOperation, tp, 2, wPreferences.icon_size-3,
450 pt, &light);
452 /* arrow bevel */
453 ROperateLine(tile, RSubtractOperation, ICON_SIZE - 7 - as, 4,
454 ICON_SIZE - 5, 4, &dark);
455 ROperateLine(tile, RSubtractOperation, ICON_SIZE - 6 - as, 5,
456 ICON_SIZE - 5, 6 + as, &dark);
457 ROperateLine(tile, RAddOperation, ICON_SIZE - 5, 4, ICON_SIZE - 5, 6 + as,
458 &light);
460 /* bottom left */
461 ROperateLine(tile, RAddOperation, 2, tp+2, pt-2,
462 wPreferences.icon_size-3, &dark);
463 RDrawLine(tile, 0, tp-1, pt+1, wPreferences.icon_size-1, &black);
464 ROperateLine(tile, RSubtractOperation, 0, tp-2, pt+1,
465 wPreferences.icon_size-2, &light);
467 /* arrow bevel */
468 ROperateLine(tile, RSubtractOperation, 4, ICON_SIZE - 7 - as, 4,
469 ICON_SIZE - 5, &dark);
470 ROperateLine(tile, RSubtractOperation, 5, ICON_SIZE - 6 - as,
471 6 + as, ICON_SIZE - 5, &dark);
472 ROperateLine(tile, RAddOperation, 4, ICON_SIZE - 5, 6 + as, ICON_SIZE - 5,
473 &light);
475 return tile;
479 static void
480 removeIconsCallback(WMenu *menu, WMenuEntry *entry)
482 WAppIcon *clickedIcon = (WAppIcon*)entry->clientdata;
483 WDock *dock;
484 WAppIcon *aicon;
485 LinkedList *selectedIcons;
486 int keepit;
488 assert(clickedIcon!=NULL);
490 dock = clickedIcon->dock;
492 selectedIcons = getSelected(dock);
494 if (selectedIcons) {
495 if (wMessageDialog(dock->screen_ptr, _("Workspace Clip"),
496 _("All selected icons will be removed!"),
497 _("OK"), _("Cancel"), NULL)!=WAPRDefault) {
498 return;
500 } else {
501 if (clickedIcon->xindex==0 && clickedIcon->yindex==0)
502 return;
503 selectedIcons = list_cons(clickedIcon, NULL);
506 while (selectedIcons) {
507 aicon = selectedIcons->head;
508 keepit = aicon->running && wApplicationOf(aicon->main_window);
509 wDockDetach(dock, aicon);
510 if (keepit) {
511 PlaceIcon(dock->screen_ptr, &aicon->x_pos, &aicon->y_pos);
512 XMoveWindow(dpy, aicon->icon->core->window,
513 aicon->x_pos, aicon->y_pos);
514 if (!dock->mapped || dock->collapsed)
515 XMapWindow(dpy, aicon->icon->core->window);
517 list_remove_head(&selectedIcons);
520 if (wPreferences.auto_arrange_icons)
521 wArrangeIcons(dock->screen_ptr, True);
525 static void
526 keepIconsCallback(WMenu *menu, WMenuEntry *entry)
528 WAppIcon *clickedIcon = (WAppIcon*)entry->clientdata;
529 WDock *dock;
530 WAppIcon *aicon;
531 LinkedList *selectedIcons;
533 assert(clickedIcon!=NULL);
534 dock = clickedIcon->dock;
536 selectedIcons = getSelected(dock);
538 if (!selectedIcons && clickedIcon!=dock->screen_ptr->clip_icon) {
539 char *command = NULL;
541 if (!clickedIcon->command && !clickedIcon->editing) {
542 clickedIcon->editing = 1;
543 if (wInputDialog(dock->screen_ptr, _("Keep Icon"),
544 _("Type the command used to launch the application"),
545 &command)) {
546 if (command && (command[0]==0 ||
547 (command[0]=='-' && command[1]==0))) {
548 free(command);
549 command = NULL;
551 clickedIcon->command = command;
552 clickedIcon->editing = 0;
553 } else {
554 clickedIcon->editing = 0;
555 if (command)
556 free(command);
557 return;
561 selectedIcons = list_cons(clickedIcon, NULL);
564 while (selectedIcons) {
565 aicon = selectedIcons->head;
566 if (aicon->icon->selected)
567 wIconSelect(aicon->icon);
568 if (aicon && aicon->attracted && aicon->command) {
569 aicon->attracted = 0;
570 if (aicon->icon->shadowed) {
571 aicon->icon->shadowed = 0;
572 aicon->icon->force_paint = 1;
573 wAppIconPaint(aicon);
576 list_remove_head(&selectedIcons);
583 static void
584 toggleAutoAttractCallback(WMenu *menu, WMenuEntry *entry)
586 WDock *dock = (WDock*)entry->clientdata;
588 assert(entry->clientdata!=NULL);
590 dock->attract_icons = !dock->attract_icons;
591 /*if (!dock->attract_icons)
592 dock->keep_attracted = 0;*/
594 entry->flags.indicator_on = dock->attract_icons;
596 wMenuPaint(menu);
600 static void
601 toggleKeepCallback(WMenu *menu, WMenuEntry *entry)
603 WDock *dock = (WDock*)entry->clientdata;
604 WAppIcon *btn;
605 int i;
607 assert(entry->clientdata!=NULL);
609 dock->keep_attracted = !dock->keep_attracted;
611 if (dock->keep_attracted) {
612 for (i=0; i< dock->max_icons; i++) {
613 btn = dock->icon_array[i];
614 if (btn && btn->attracted && btn->command) {
615 btn->attracted = 0;
616 if (btn->icon->shadowed) {
617 btn->icon->shadowed = 0;
618 btn->icon->force_paint = 1;
619 wAppIconPaint(btn);
625 entry->flags.indicator_on = dock->keep_attracted;
627 wMenuPaint(menu);
631 static void
632 selectCallback(WMenu *menu, WMenuEntry *entry)
634 WAppIcon *icon = (WAppIcon*)entry->clientdata;
636 assert(icon!=NULL);
638 wIconSelect(icon->icon);
640 wMenuPaint(menu);
644 static void
645 colectIconsCallback(WMenu *menu, WMenuEntry *entry)
647 WAppIcon *clickedIcon = (WAppIcon*)entry->clientdata;
648 WDock *clip;
649 WAppIcon *aicon;
650 int x, y, x_pos, y_pos;
652 assert(entry->clientdata!=NULL);
653 clip = clickedIcon->dock;
655 aicon = clip->screen_ptr->app_icon_list;
657 while (aicon) {
658 if (!aicon->docked && wDockFindFreeSlot(clip, &x, &y)) {
659 x_pos = clip->x_pos + x*ICON_SIZE;
660 y_pos = clip->y_pos + y*ICON_SIZE;
661 if (aicon->x_pos != x_pos || aicon->y_pos != y_pos) {
662 #ifdef ANIMATIONS
663 if (wPreferences.no_animations) {
664 XMoveWindow(dpy, aicon->icon->core->window, x_pos, y_pos);
665 } else {
666 SlideWindow(aicon->icon->core->window,
667 aicon->x_pos, aicon->y_pos, x_pos, y_pos);
669 #else
670 XMoveWindow(dpy, aicon->icon->core->window, x_pos, y_pos);
671 #endif /* ANIMATIONS */
673 aicon->attracted = 1;
674 if (!clip->keep_attracted && !aicon->icon->shadowed) {
675 aicon->icon->shadowed = 1;
676 aicon->icon->force_paint = 1;
677 /* We don't do an wAppIconPaint() here because it's in
678 * wDockAttachIcon(). -Dan
681 wDockAttachIcon(clip, aicon, x, y);
682 if (clip->collapsed || !clip->mapped)
683 XUnmapWindow(dpy, aicon->icon->core->window);
685 aicon = aicon->next;
690 static void
691 selectIconsCallback(WMenu *menu, WMenuEntry *entry)
693 WAppIcon *clickedIcon = (WAppIcon*)entry->clientdata;
694 WDock *dock;
695 LinkedList *selectedIcons;
696 WAppIcon *btn;
697 int i;
699 assert(clickedIcon!=NULL);
700 dock = clickedIcon->dock;
702 selectedIcons = getSelected(dock);
704 if (!selectedIcons) {
705 for (i=1; i<dock->max_icons; i++) {
706 btn = dock->icon_array[i];
707 if (btn && !btn->icon->selected) {
708 wIconSelect(btn->icon);
711 } else {
712 while(selectedIcons) {
713 btn = selectedIcons->head;
714 wIconSelect(btn->icon);
715 list_remove_head(&selectedIcons);
719 wMenuPaint(menu);
723 static void
724 toggleCollapsedCallback(WMenu *menu, WMenuEntry *entry)
726 assert(entry->clientdata!=NULL);
728 toggleCollapsed(entry->clientdata);
730 entry->flags.indicator_on = ((WDock*)entry->clientdata)->collapsed;
732 wMenuPaint(menu);
736 static void
737 toggleAutoCollapseCallback(WMenu *menu, WMenuEntry *entry)
739 WDock *dock;
740 assert(entry->clientdata!=NULL);
742 dock = (WDock*) entry->clientdata;
744 dock->auto_collapse = !dock->auto_collapse;
746 entry->flags.indicator_on = ((WDock*)entry->clientdata)->auto_collapse;
748 wMenuPaint(menu);
752 static void
753 toggleAutoRaiseLowerCallback(WMenu *menu, WMenuEntry *entry)
755 WDock *dock;
756 assert(entry->clientdata!=NULL);
758 dock = (WDock*) entry->clientdata;
760 dock->auto_raise_lower = !dock->auto_raise_lower;
762 entry->flags.indicator_on = ((WDock*)entry->clientdata)->auto_raise_lower;
764 wMenuPaint(menu);
768 static void
769 launchCallback(WMenu *menu, WMenuEntry *entry)
771 WAppIcon *btn = (WAppIcon*)entry->clientdata;
773 launchDockedApplication(btn);
777 static void
778 settingsCallback(WMenu *menu, WMenuEntry *entry)
780 WAppIcon *btn = (WAppIcon*)entry->clientdata;
782 if (btn->editing)
783 return;
784 ShowDockAppSettingsPanel(btn);
788 static void
789 hideCallback(WMenu *menu, WMenuEntry *entry)
791 WApplication *wapp;
792 WAppIcon *btn = (WAppIcon*)entry->clientdata;
794 wapp = wApplicationOf(btn->icon->owner->main_window);
796 if (wapp->flags.hidden) {
797 wWorkspaceChange(btn->icon->core->screen_ptr,wapp->last_workspace);
798 wUnhideApplication(wapp, False, False);
799 } else {
800 wHideApplication(wapp);
805 static void
806 unhideHereCallback(WMenu *menu, WMenuEntry *entry)
808 WApplication *wapp;
809 WAppIcon *btn = (WAppIcon*)entry->clientdata;
811 wapp = wApplicationOf(btn->icon->owner->main_window);
813 wUnhideApplication(wapp, False, True);
817 WAppIcon*
818 mainIconCreate(WScreen *scr, int type)
820 WAppIcon *btn;
821 int x_pos;
823 if (type == WM_CLIP) {
824 if (scr->clip_icon)
825 return scr->clip_icon;
826 btn = wAppIconCreateForDock(scr, NULL, "Logo", "WMClip", TILE_CLIP);
827 btn->icon->core->descriptor.handle_expose = clipIconExpose;
828 btn->icon->core->descriptor.handle_enternotify = clipEnterNotify;
829 btn->icon->core->descriptor.handle_leavenotify = clipLeaveNotify;
830 /*x_pos = scr->scr_width - ICON_SIZE*2 - DOCK_EXTRA_SPACE;*/
831 x_pos = 0;
832 } else {
833 btn = wAppIconCreateForDock(scr, NULL, "Logo", "WMDock", TILE_NORMAL);
834 x_pos = scr->scr_width - ICON_SIZE - DOCK_EXTRA_SPACE;
837 btn->xindex = 0;
838 btn->yindex = 0;
840 btn->icon->core->descriptor.handle_mousedown = iconMouseDown;
841 btn->icon->core->descriptor.parent_type = WCLASS_DOCK_ICON;
842 btn->icon->core->descriptor.parent = btn;
843 /*ChangeStackingLevel(btn->icon->core, WMDockLevel);*/
844 XMapWindow(dpy, btn->icon->core->window);
845 btn->x_pos = x_pos;
846 btn->y_pos = 0;
847 btn->docked = 1;
848 if (type == WM_CLIP)
849 scr->clip_icon = btn;
851 return btn;
855 static void
856 switchWSCommand(WMenu *menu, WMenuEntry *entry)
858 WAppIcon *btn, *icon = (WAppIcon*) entry->clientdata;
859 WScreen *scr = icon->icon->core->screen_ptr;
860 WDock *src, *dest;
861 LinkedList *selectedIcons;
862 int x, y;
864 if (entry->order == scr->current_workspace)
865 return;
866 src = icon->dock;
867 dest = scr->workspaces[entry->order]->clip;
869 selectedIcons = getSelected(src);
871 if (selectedIcons) {
872 while(selectedIcons) {
873 btn = selectedIcons->head;
874 if (wDockFindFreeSlot(dest, &x, &y)) {
875 moveIconBetweenDocks(src, dest, btn, x, y);
876 XUnmapWindow(dpy, btn->icon->core->window);
878 list_remove_head(&selectedIcons);
880 } else if (icon != scr->clip_icon) {
881 if (wDockFindFreeSlot(dest, &x, &y)) {
882 moveIconBetweenDocks(src, dest, icon, x, y);
883 XUnmapWindow(dpy, icon->icon->core->window);
890 static void
891 launchDockedApplication(WAppIcon *btn)
893 WScreen *scr = btn->icon->core->screen_ptr;
895 if (!btn->launching && btn->command!=NULL) {
896 if (!btn->forced_dock) {
897 btn->relaunching = btn->running;
898 btn->running = 1;
900 if (btn->wm_instance || btn->wm_class) {
901 WWindowAttributes attr;
902 memset(&attr, 0, sizeof(WWindowAttributes));
903 wDefaultFillAttributes(scr, btn->wm_instance, btn->wm_class,
904 &attr, NULL, True);
906 if (!attr.no_appicon && !btn->buggy_app)
907 btn->launching = 1;
908 else
909 btn->running = 0;
911 btn->drop_launch = 0;
912 scr->last_dock = btn->dock;
913 btn->pid = execCommand(btn, btn->command, NULL);
914 if (btn->pid>0) {
915 if (btn->buggy_app) {
916 /* give feedback that the app was launched */
917 btn->launching = 1;
918 dockIconPaint(btn);
919 btn->launching = 0;
920 WMAddTimerHandler(200, (WMCallback*)dockIconPaint, btn);
921 } else {
922 dockIconPaint(btn);
924 } else {
925 wwarning(_("could not launch application %s\n"), btn->command);
926 btn->launching = 0;
927 if (!btn->relaunching)
928 btn->running = 0;
935 static void
936 updateWorkspaceMenu(WMenu *menu, WAppIcon *icon)
938 WScreen *scr = menu->frame->screen_ptr;
939 char title[MAX_WORKSPACENAME_WIDTH+1];
940 int i;
942 if (!menu || !icon)
943 return;
945 for (i=0; i<scr->workspace_count; i++) {
946 if (i < menu->entry_no) {
947 if (strcmp(menu->entries[i]->text,scr->workspaces[i]->name)!=0) {
948 free(menu->entries[i]->text);
949 strcpy(title, scr->workspaces[i]->name);
950 menu->entries[i]->text = wstrdup(title);
951 menu->flags.realized = 0;
953 menu->entries[i]->clientdata = (void*)icon;
954 } else {
955 strcpy(title, scr->workspaces[i]->name);
957 wMenuAddCallback(menu, title, switchWSCommand, (void*)icon);
959 menu->flags.realized = 0;
961 if (i == scr->current_workspace) {
962 wMenuSetEnabled(menu, i, False);
963 } else {
964 wMenuSetEnabled(menu, i, True);
968 if (!menu->flags.realized)
969 wMenuRealize(menu);
973 static WMenu*
974 makeWorkspaceMenu(WScreen *scr)
976 WMenu *menu;
978 menu = wMenuCreate(scr, NULL, False);
979 if (!menu)
980 wwarning(_("could not create workspace submenu for Clip menu"));
982 wMenuAddCallback(menu, "", switchWSCommand, (void*)scr->clip_icon);
984 menu->flags.realized = 0;
985 wMenuRealize(menu);
987 return menu;
991 static void
992 updateClipOptionsMenu(WMenu *menu, WDock *dock)
994 WMenuEntry *entry;
995 int index = 0;
997 if (!menu || !dock)
998 return;
1000 /* keep on top */
1001 entry = menu->entries[index];
1002 entry->flags.indicator_on = !dock->lowered;
1003 entry->clientdata = dock;
1005 /* collapsed */
1006 entry = menu->entries[++index];
1007 entry->flags.indicator_on = dock->collapsed;
1008 entry->clientdata = dock;
1010 /* auto-collapse */
1011 entry = menu->entries[++index];
1012 entry->flags.indicator_on = dock->auto_collapse;
1013 entry->clientdata = dock;
1015 /* auto-raise/lower */
1016 entry = menu->entries[++index];
1017 entry->flags.indicator_on = dock->auto_raise_lower;
1018 entry->clientdata = dock;
1020 /* attract icons */
1021 entry = menu->entries[++index];
1022 entry->flags.indicator_on = dock->attract_icons;
1023 entry->clientdata = dock;
1025 /* keep attracted icons */
1026 entry = menu->entries[++index];
1027 entry->flags.indicator_on = dock->keep_attracted;
1028 entry->clientdata = dock;
1030 menu->flags.realized = 0;
1031 wMenuRealize(menu);
1035 static WMenu*
1036 makeClipOptionsMenu(WScreen *scr)
1038 WMenu *menu;
1039 WMenuEntry *entry;
1041 menu = wMenuCreate(scr, NULL, False);
1042 if (!menu) {
1043 wwarning(_("could not create options submenu for Clip menu"));
1044 return NULL;
1047 entry = wMenuAddCallback(menu, _("Keep on Top"),
1048 toggleLoweredCallback, NULL);
1049 entry->flags.indicator = 1;
1050 entry->flags.indicator_on = 1;
1051 entry->flags.indicator_type = MI_CHECK;
1053 entry = wMenuAddCallback(menu, _("Collapsed"),
1054 toggleCollapsedCallback, NULL);
1055 entry->flags.indicator = 1;
1056 entry->flags.indicator_on = 1;
1057 entry->flags.indicator_type = MI_CHECK;
1059 entry = wMenuAddCallback(menu, _("AutoCollapse"),
1060 toggleAutoCollapseCallback, NULL);
1061 entry->flags.indicator = 1;
1062 entry->flags.indicator_on = 1;
1063 entry->flags.indicator_type = MI_CHECK;
1065 entry = wMenuAddCallback(menu, _("AutoRaiseLower"),
1066 toggleAutoRaiseLowerCallback, NULL);
1067 entry->flags.indicator = 1;
1068 entry->flags.indicator_on = 1;
1069 entry->flags.indicator_type = MI_CHECK;
1071 entry = wMenuAddCallback(menu, _("AutoAttract Icons"),
1072 toggleAutoAttractCallback, NULL);
1073 entry->flags.indicator = 1;
1074 entry->flags.indicator_on = 1;
1075 entry->flags.indicator_type = MI_CHECK;
1077 entry = wMenuAddCallback(menu, _("Keep Attracted Icons"),
1078 toggleKeepCallback, NULL);
1079 entry->flags.indicator = 1;
1080 entry->flags.indicator_on = 1;
1081 entry->flags.indicator_type = MI_CHECK;
1083 menu->flags.realized = 0;
1084 wMenuRealize(menu);
1086 return menu;
1090 static WMenu*
1091 dockMenuCreate(WScreen *scr, int type)
1093 WMenu *menu;
1094 WMenuEntry *entry;
1096 if (type == WM_CLIP && scr->clip_menu)
1097 return scr->clip_menu;
1099 menu = wMenuCreate(scr, NULL, False);
1100 if (type != WM_CLIP) {
1101 entry = wMenuAddCallback(menu, _("Keep on top"),
1102 toggleLoweredCallback, NULL);
1103 entry->flags.indicator = 1;
1104 entry->flags.indicator_on = 1;
1105 entry->flags.indicator_type = MI_CHECK;
1106 } else {
1107 entry = wMenuAddCallback(menu, _("Clip Options"), NULL, NULL);
1108 scr->clip_options = makeClipOptionsMenu(scr);
1109 if (scr->clip_options)
1110 wMenuEntrySetCascade(menu, entry, scr->clip_options);
1112 wMenuAddCallback(menu, _("Rename Workspace"), renameCallback, NULL);
1114 entry = wMenuAddCallback(menu, _("Select Icon"), selectCallback, NULL);
1115 free(entry->text);
1116 entry->text = _("Select Icon");
1118 entry = wMenuAddCallback(menu, _("Select All Icons"),
1119 selectIconsCallback, NULL);
1120 free(entry->text);
1121 entry->text = _("Select All Icons");
1123 entry = wMenuAddCallback(menu, _("Keep Icon"), keepIconsCallback, NULL);
1124 free(entry->text);
1125 entry->text = _("Keep Icon");
1127 entry = wMenuAddCallback(menu, _("Move Icon To"), NULL, NULL);
1128 free(entry->text);
1129 entry->text = _("Move Icon To");
1130 scr->clip_submenu = makeWorkspaceMenu(scr);
1131 if (scr->clip_submenu)
1132 wMenuEntrySetCascade(menu, entry, scr->clip_submenu);
1134 entry = wMenuAddCallback(menu, _("Remove Icon"), removeIconsCallback,
1135 NULL);
1136 free(entry->text);
1137 entry->text = _("Remove Icon");
1139 wMenuAddCallback(menu, _("Attract Icons"), colectIconsCallback, NULL);
1142 wMenuAddCallback(menu, _("Launch"), launchCallback, NULL);
1144 wMenuAddCallback(menu, _("Unhide Here"), unhideHereCallback, NULL);
1146 entry = wMenuAddCallback(menu, _("Hide"), hideCallback, NULL);
1147 free(entry->text);
1148 entry->text = _("Hide");
1150 wMenuAddCallback(menu, _("Settings..."), settingsCallback, NULL);
1152 wMenuAddCallback(menu, _("Kill"), killCallback, NULL);
1154 if (type == WM_CLIP)
1155 scr->clip_menu = menu;
1157 return menu;
1161 WDock*
1162 wDockCreate(WScreen *scr, int type)
1164 WDock *dock;
1165 WAppIcon *btn;
1166 int icon_count;
1168 make_keys();
1170 dock = wmalloc(sizeof(WDock));
1171 memset(dock, 0, sizeof(WDock));
1173 if (type == WM_CLIP)
1174 icon_count = CLIP_MAX_ICONS;
1175 else
1176 icon_count = scr->scr_height/wPreferences.icon_size;
1178 dock->icon_array = wmalloc(sizeof(WAppIcon*)*icon_count);
1179 memset(dock->icon_array, 0, sizeof(WAppIcon*)*icon_count);
1181 dock->max_icons = icon_count;
1183 btn = mainIconCreate(scr, type);
1185 btn->dock = dock;
1187 dock->x_pos = btn->x_pos;
1188 dock->y_pos = btn->y_pos;
1189 dock->screen_ptr = scr;
1190 dock->type = type;
1191 dock->icon_count = 1;
1192 dock->on_right_side = 1;
1193 dock->collapsed = 0;
1194 dock->auto_collapse = 0;
1195 dock->auto_collapse_magic = NULL;
1196 dock->auto_raise_lower = 0;
1197 dock->auto_lower_magic = NULL;
1198 dock->auto_raise_magic = NULL;
1199 dock->attract_icons = 0;
1200 dock->keep_attracted = 0;
1201 dock->lowered = 1;
1202 dock->icon_array[0] = btn;
1203 wRaiseFrame(btn->icon->core);
1204 XMoveWindow(dpy, btn->icon->core->window, btn->x_pos, btn->y_pos);
1206 /* create dock menu */
1207 dock->menu = dockMenuCreate(scr, type);
1209 return dock;
1213 void
1214 wDockDestroy(WDock *dock)
1216 int i;
1217 WAppIcon *aicon;
1219 for (i=(dock->type == WM_CLIP) ? 1 : 0; i<dock->max_icons; i++) {
1220 aicon = dock->icon_array[i];
1221 if (aicon) {
1222 int keepit = aicon->running && wApplicationOf(aicon->main_window);
1223 wDockDetach(dock, aicon);
1224 if (keepit) {
1225 PlaceIcon(dock->screen_ptr, &aicon->x_pos, &aicon->y_pos);
1226 XMoveWindow(dpy, aicon->icon->core->window,
1227 aicon->x_pos, aicon->y_pos);
1228 if (!dock->mapped || dock->collapsed)
1229 XMapWindow(dpy, aicon->icon->core->window);
1233 if (wPreferences.auto_arrange_icons)
1234 wArrangeIcons(dock->screen_ptr, True);
1235 free(dock->icon_array);
1236 if (dock->menu && dock->type!=WM_CLIP)
1237 wMenuDestroy(dock->menu, True);
1238 free(dock);
1242 void
1243 wClipIconPaint(WAppIcon *aicon)
1245 WScreen *scr = aicon->icon->core->screen_ptr;
1246 WWorkspace *workspace = scr->workspaces[scr->current_workspace];
1247 GC gc;
1248 Window win = aicon->icon->core->window;
1249 int length, nlength;
1250 char *ws_name, ws_number[10];
1251 int ty, tx;
1253 wIconPaint(aicon->icon);
1255 length = strlen(workspace->name);
1256 ws_name = malloc(length + 1);
1257 sprintf(ws_name, "%s", workspace->name);
1258 sprintf(ws_number, "%i", scr->current_workspace + 1);
1259 nlength = strlen(ws_number);
1261 gc = scr->clip_title_gc;
1263 if (!workspace->clip->collapsed)
1264 XSetForeground(dpy, gc, scr->clip_title_pixel[CLIP_NORMAL]);
1265 else
1266 XSetForeground(dpy, gc, scr->clip_title_pixel[CLIP_COLLAPSED]);
1268 ty = ICON_SIZE - WMFontHeight(scr->clip_title_font) - 3;
1270 tx = CLIP_BUTTON_SIZE*ICON_SIZE/64;
1272 WMDrawString(scr->wmscreen, win, gc, scr->clip_title_font, tx,
1273 ty, ws_name, length);
1275 tx = (ICON_SIZE/2 - WMWidthOfString(scr->clip_title_font, ws_number,
1276 nlength))/2;
1278 WMDrawString(scr->wmscreen, win, gc, scr->clip_title_font, tx,
1279 2, ws_number, nlength);
1281 free(ws_name);
1283 if (aicon->launching) {
1284 XFillRectangle(dpy, aicon->icon->core->window, scr->stipple_gc,
1285 0, 0, wPreferences.icon_size, wPreferences.icon_size);
1287 paintClipButtons(aicon, aicon->dock->lclip_button_pushed,
1288 aicon->dock->rclip_button_pushed);
1292 static void
1293 clipIconExpose(WObjDescriptor *desc, XEvent *event)
1295 wClipIconPaint(desc->parent);
1299 static void
1300 dockIconPaint(WAppIcon *btn)
1302 if (btn == btn->icon->core->screen_ptr->clip_icon)
1303 wClipIconPaint(btn);
1304 else
1305 wAppIconPaint(btn);
1309 static proplist_t
1310 make_icon_state(WAppIcon *btn)
1312 proplist_t node = NULL;
1313 proplist_t command, autolaunch, name, forced, host, position, buggy;
1314 proplist_t omnipresent;
1315 char *tmp;
1316 char buffer[64];
1318 if (btn) {
1319 if (!btn->command)
1320 command = PLMakeString("-");
1321 else
1322 command = PLMakeString(btn->command);
1324 autolaunch = btn->auto_launch ? dYes : dNo;
1326 tmp = EscapeWM_CLASS(btn->wm_instance, btn->wm_class);
1328 name = PLMakeString(tmp);
1330 free(tmp);
1332 forced = btn->forced_dock ? dYes : dNo;
1334 buggy = btn->buggy_app ? dYes : dNo;
1336 if (btn == btn->icon->core->screen_ptr->clip_icon)
1337 sprintf(buffer, "%i,%i", btn->x_pos, btn->y_pos);
1338 else
1339 sprintf(buffer, "%hi,%hi", btn->xindex, btn->yindex);
1340 position = PLMakeString(buffer);
1342 node = PLMakeDictionaryFromEntries(dCommand, command,
1343 dName, name,
1344 dAutoLaunch, autolaunch,
1345 dForced, forced,
1346 dBuggyApplication, buggy,
1347 dPosition, position,
1348 NULL);
1349 PLRelease(command);
1350 PLRelease(name);
1351 PLRelease(position);
1353 omnipresent = btn->omnipresent ? dYes : dNo;
1354 if (btn->dock != btn->icon->core->screen_ptr->dock &&
1355 (btn->xindex != 0 || btn->yindex != 0))
1356 PLInsertDictionaryEntry(node, dOmnipresent, omnipresent);
1358 #ifdef OFFIX_DND
1359 if (btn->dnd_command) {
1360 command = PLMakeString(btn->dnd_command);
1361 PLInsertDictionaryEntry(node, dDropCommand, command);
1362 PLRelease(command);
1364 #endif /* OFFIX_DND */
1366 if (btn->client_machine && btn->remote_start) {
1367 host = PLMakeString(btn->client_machine);
1368 PLInsertDictionaryEntry(node, dHost, host);
1369 PLRelease(host);
1373 return node;
1377 static proplist_t
1378 dockSaveState(WDock *dock)
1380 int i;
1381 proplist_t icon_info;
1382 proplist_t list=NULL, dock_state=NULL;
1383 proplist_t value;
1384 char buffer[256];
1386 list = PLMakeArrayFromElements(NULL);
1388 for (i=(dock->type==WM_DOCK ? 0 : 1); i<dock->max_icons; i++) {
1389 WAppIcon *btn = dock->icon_array[i];
1391 if (!btn || (btn->attracted && !dock->keep_attracted))
1392 continue;
1394 if ((icon_info = make_icon_state(dock->icon_array[i]))) {
1395 list = PLAppendArrayElement(list, icon_info);
1396 PLRelease(icon_info);
1400 dock_state = PLMakeDictionaryFromEntries(dApplications, list, NULL);
1402 PLRelease(list);
1404 if (dock->type == WM_DOCK) {
1405 sprintf(buffer, "%i,%i", (dock->on_right_side ? -ICON_SIZE : 0),
1406 dock->y_pos);
1407 value = PLMakeString(buffer);
1408 PLInsertDictionaryEntry(dock_state, dPosition, value);
1409 PLRelease(value);
1412 value = (dock->lowered ? dYes : dNo);
1413 PLInsertDictionaryEntry(dock_state, dLowered, value);
1415 if (dock->type == WM_CLIP) {
1416 value = (dock->collapsed ? dYes : dNo);
1417 PLInsertDictionaryEntry(dock_state, dCollapsed, value);
1419 value = (dock->auto_collapse ? dYes : dNo);
1420 PLInsertDictionaryEntry(dock_state, dAutoCollapse, value);
1422 value = (dock->auto_raise_lower ? dYes : dNo);
1423 PLInsertDictionaryEntry(dock_state, dAutoRaiseLower, value);
1425 value = (dock->attract_icons ? dYes : dNo);
1426 PLInsertDictionaryEntry(dock_state, dAutoAttractIcons, value);
1428 value = (dock->keep_attracted ? dYes : dNo);
1429 PLInsertDictionaryEntry(dock_state, dKeepAttracted, value);
1432 return dock_state;
1436 void
1437 wDockSaveState(WScreen *scr)
1439 proplist_t dock_state;
1441 dock_state = dockSaveState(scr->dock);
1443 PLInsertDictionaryEntry(scr->session_state, dDock, dock_state);
1445 PLRelease(dock_state);
1449 void
1450 wClipSaveState(WScreen *scr)
1452 proplist_t clip_state;
1454 clip_state = make_icon_state(scr->clip_icon);
1456 PLInsertDictionaryEntry(scr->session_state, dClip, clip_state);
1458 PLRelease(clip_state);
1462 proplist_t
1463 wClipSaveWorkspaceState(WScreen *scr, int workspace)
1465 return dockSaveState(scr->workspaces[workspace]->clip);
1469 static WAppIcon*
1470 restore_icon_state(WScreen *scr, proplist_t info, int type, int index)
1472 WAppIcon *aicon;
1473 char *wclass, *winstance;
1474 proplist_t cmd, value;
1475 char *command;
1478 cmd = PLGetDictionaryEntry(info, dCommand);
1479 if (!cmd || !PLIsString(cmd)) {
1480 return NULL;
1483 /* parse window name */
1484 value = PLGetDictionaryEntry(info, dName);
1485 if (!value)
1486 return NULL;
1488 ParseWindowName(value, &winstance, &wclass, "dock");
1490 if (!winstance && !wclass) {
1491 return NULL;
1494 /* get commands */
1496 if (cmd)
1497 command = wstrdup(PLGetString(cmd));
1498 else
1499 command = NULL;
1501 if (!command || strcmp(command, "-")==0) {
1502 if (command)
1503 free(command);
1504 if (wclass)
1505 free(wclass);
1506 if (winstance)
1507 free(winstance);
1509 return NULL;
1512 aicon = wAppIconCreateForDock(scr, command, winstance, wclass,
1513 TILE_NORMAL);
1514 if (wclass)
1515 free(wclass);
1516 if (winstance)
1517 free(winstance);
1519 aicon->icon->core->descriptor.handle_mousedown = iconMouseDown;
1520 if (type == WM_CLIP) {
1521 aicon->icon->core->descriptor.handle_enternotify = clipEnterNotify;
1522 aicon->icon->core->descriptor.handle_leavenotify = clipLeaveNotify;
1524 aicon->icon->core->descriptor.parent_type = WCLASS_DOCK_ICON;
1525 aicon->icon->core->descriptor.parent = aicon;
1528 #ifdef OFFIX_DND
1529 cmd = PLGetDictionaryEntry(info, dDropCommand);
1530 if (cmd)
1531 aicon->dnd_command = wstrdup(PLGetString(cmd));
1532 #endif
1534 /* check auto launch */
1535 value = PLGetDictionaryEntry(info, dAutoLaunch);
1537 aicon->auto_launch = 0;
1538 if (value) {
1539 if (PLIsString(value)) {
1540 if (strcasecmp(PLGetString(value), "YES")==0)
1541 aicon->auto_launch = 1;
1542 } else {
1543 wwarning(_("bad value in docked icon state info %s"),
1544 PLGetString(dAutoLaunch));
1548 /* check if it wasn't normally docked */
1549 value = PLGetDictionaryEntry(info, dForced);
1551 aicon->forced_dock = 0;
1552 if (value) {
1553 if (PLIsString(value)) {
1554 if (strcasecmp(PLGetString(value), "YES")==0)
1555 aicon->forced_dock = 1;
1556 } else {
1557 wwarning(_("bad value in docked icon state info %s"),
1558 PLGetString(dForced));
1562 /* check if we can rely on the stuff in the app */
1563 value = PLGetDictionaryEntry(info, dBuggyApplication);
1565 aicon->buggy_app = 0;
1566 if (value) {
1567 if (PLIsString(value)) {
1568 if (strcasecmp(PLGetString(value), "YES")==0)
1569 aicon->buggy_app = 1;
1570 } else {
1571 wwarning(_("bad value in docked icon state info %s"),
1572 PLGetString(dBuggyApplication));
1576 /* get position in the dock */
1577 value = PLGetDictionaryEntry(info, dPosition);
1578 if (value && PLIsString(value)) {
1579 if (sscanf(PLGetString(value), "%hi,%hi", &aicon->xindex,
1580 &aicon->yindex)!=2)
1581 wwarning(_("bad value in docked icon state info %s"),
1582 PLGetString(dPosition));
1584 /* check position sanity */
1585 /* incomplete section! */
1586 if (type == WM_DOCK) {
1587 aicon->xindex = 0;
1588 if (aicon->yindex < 0)
1589 wwarning(_("bad value in docked icon position %i,%i"),
1590 aicon->xindex, aicon->yindex);
1592 } else {
1593 aicon->yindex = index;
1594 aicon->xindex = 0;
1597 /* check if icon is omnipresent */
1598 value = PLGetDictionaryEntry(info, dOmnipresent);
1600 aicon->omnipresent = 0;
1601 if (value) {
1602 if (PLIsString(value)) {
1603 if (strcasecmp(PLGetString(value), "YES")==0)
1604 aicon->omnipresent = 1;
1605 } else {
1606 wwarning(_("bad value in docked icon state info %s"),
1607 PLGetString(dOmnipresent));
1611 aicon->running = 0;
1612 aicon->docked = 1;
1614 return aicon;
1618 #define COMPLAIN(key) wwarning(_("bad value in dock state info:%s"), key)
1621 WAppIcon*
1622 wClipRestoreState(WScreen *scr, proplist_t clip_state)
1624 WAppIcon *icon;
1625 proplist_t value;
1628 icon = mainIconCreate(scr, WM_CLIP);
1630 if (!clip_state)
1631 return icon;
1632 else
1633 PLRetain(clip_state);
1635 /* restore position */
1637 value = PLGetDictionaryEntry(clip_state, dPosition);
1639 if (value) {
1640 if (!PLIsString(value))
1641 COMPLAIN("Position");
1642 else {
1643 if (sscanf(PLGetString(value), "%i,%i", &icon->x_pos,
1644 &icon->y_pos)!=2)
1645 COMPLAIN("Position");
1647 /* check position sanity */
1648 if (icon->y_pos < 0)
1649 icon->y_pos = 0;
1650 else if (icon->y_pos > scr->scr_height-ICON_SIZE)
1651 icon->y_pos = scr->scr_height-ICON_SIZE;
1653 if (icon->x_pos < 0)
1654 icon->x_pos = 0;
1655 else if (icon->x_pos > scr->scr_width-ICON_SIZE)
1656 icon->x_pos = scr->scr_width-ICON_SIZE;
1660 #ifdef OFFIX_DND
1661 value = PLGetDictionaryEntry(clip_state, dDropCommand);
1662 if (value && PLIsString(value))
1663 icon->dnd_command = wstrdup(PLGetString(value));
1664 #endif
1666 PLRelease(clip_state);
1668 return icon;
1672 WDock*
1673 wDockRestoreState(WScreen *scr, proplist_t dock_state, int type)
1675 WDock *dock;
1676 proplist_t apps;
1677 proplist_t value;
1678 WAppIcon *aicon, *old_top;
1679 int count, i;
1682 dock = wDockCreate(scr, type);
1684 if (!dock_state)
1685 return dock;
1687 if (dock_state)
1688 PLRetain(dock_state);
1691 /* restore position */
1693 value = PLGetDictionaryEntry(dock_state, dPosition);
1695 if (value) {
1696 if (!PLIsString(value))
1697 COMPLAIN("Position");
1698 else {
1699 if (sscanf(PLGetString(value), "%i,%i", &dock->x_pos,
1700 &dock->y_pos)!=2)
1701 COMPLAIN("Position");
1703 /* check position sanity */
1704 if (dock->y_pos < 0)
1705 dock->y_pos = 0;
1706 else if (dock->y_pos > scr->scr_height-ICON_SIZE)
1707 dock->y_pos = scr->scr_height - ICON_SIZE;
1709 /* This is no more needed. ??? */
1710 if (type == WM_CLIP) {
1711 if (dock->x_pos < 0)
1712 dock->x_pos = 0;
1713 else if (dock->x_pos > scr->scr_width-ICON_SIZE)
1714 dock->x_pos = scr->scr_width-ICON_SIZE;
1715 } else {
1716 if (dock->x_pos >= 0) {
1717 dock->x_pos = DOCK_EXTRA_SPACE;
1718 dock->on_right_side = 0;
1719 } else {
1720 dock->x_pos = scr->scr_width - DOCK_EXTRA_SPACE - ICON_SIZE;
1721 dock->on_right_side = 1;
1727 /* restore lowered/raised state */
1729 dock->lowered = 0;
1731 value = PLGetDictionaryEntry(dock_state, dLowered);
1733 if (value) {
1734 if (!PLIsString(value))
1735 COMPLAIN("Lowered");
1736 else {
1737 if (strcasecmp(PLGetString(value), "YES")==0)
1738 dock->lowered = 1;
1743 /* restore collapsed state */
1745 dock->collapsed = 0;
1747 value = PLGetDictionaryEntry(dock_state, dCollapsed);
1749 if (value) {
1750 if (!PLIsString(value))
1751 COMPLAIN("Collapsed");
1752 else {
1753 if (strcasecmp(PLGetString(value), "YES")==0)
1754 dock->collapsed = 1;
1759 /* restore auto-collapsed state */
1761 value = PLGetDictionaryEntry(dock_state, dAutoCollapse);
1763 if (value) {
1764 if (!PLIsString(value))
1765 COMPLAIN("AutoCollapse");
1766 else {
1767 if (strcasecmp(PLGetString(value), "YES")==0) {
1768 dock->auto_collapse = 1;
1769 dock->collapsed = 1;
1775 /* restore auto-raise/lower state */
1777 value = PLGetDictionaryEntry(dock_state, dAutoRaiseLower);
1779 if (value) {
1780 if (!PLIsString(value))
1781 COMPLAIN("AutoRaiseLower");
1782 else {
1783 if (strcasecmp(PLGetString(value), "YES")==0) {
1784 dock->auto_raise_lower = 1;
1789 /* restore attract icons state */
1791 dock->attract_icons = 0;
1793 value = PLGetDictionaryEntry(dock_state, dAutoAttractIcons);
1795 if (value) {
1796 if (!PLIsString(value))
1797 COMPLAIN("AutoAttractIcons");
1798 else {
1799 if (strcasecmp(PLGetString(value), "YES")==0)
1800 dock->attract_icons = 1;
1805 /* restore keep attracted icons state */
1807 dock->keep_attracted = 0;
1809 value = PLGetDictionaryEntry(dock_state, dKeepAttracted);
1811 if (value) {
1812 if (!PLIsString(value))
1813 COMPLAIN("KeepAttracted");
1814 else {
1815 if (strcasecmp(PLGetString(value), "YES")==0)
1816 dock->keep_attracted = 1;
1821 /* application list */
1823 apps = PLGetDictionaryEntry(dock_state, dApplications);
1825 if (!apps) {
1826 goto finish;
1829 count = PLGetNumberOfElements(apps);
1831 if (count==0)
1832 goto finish;
1834 old_top = dock->icon_array[0];
1836 /* dock->icon_count is set to 1 when dock is created.
1837 * Since Clip is already restored, we want to keep it so for clip,
1838 * but for dock we may change the default top tile, so we set it to 0.
1840 if (type == WM_DOCK)
1841 dock->icon_count = 0;
1843 for (i=0; i<count; i++) {
1844 if (dock->icon_count >= dock->max_icons) {
1845 wwarning(_("there are too many icons stored in dock. Ignoring what doesn't fit"));
1846 break;
1849 value = PLGetArrayElement(apps, i);
1850 aicon = restore_icon_state(scr, value, type, dock->icon_count);
1852 dock->icon_array[dock->icon_count] = aicon;
1854 if (aicon) {
1855 aicon->dock = dock;
1856 aicon->x_pos = dock->x_pos + (aicon->xindex*ICON_SIZE);
1857 aicon->y_pos = dock->y_pos + (aicon->yindex*ICON_SIZE);
1859 if (dock->lowered)
1860 ChangeStackingLevel(aicon->icon->core, WMNormalLevel);
1861 else
1862 ChangeStackingLevel(aicon->icon->core, WMDockLevel);
1864 wCoreConfigure(aicon->icon->core, aicon->x_pos, aicon->y_pos,
1865 0, 0);
1867 if (!dock->collapsed)
1868 XMapWindow(dpy, aicon->icon->core->window);
1869 wRaiseFrame(aicon->icon->core);
1871 dock->icon_count++;
1872 } else if (dock->icon_count==0 && type==WM_DOCK)
1873 dock->icon_count++;
1876 /* if the first icon is not defined, use the default */
1877 if (dock->icon_array[0]==NULL) {
1878 /* update default icon */
1879 old_top->x_pos = dock->x_pos;
1880 old_top->y_pos = dock->y_pos;
1881 if (dock->lowered)
1882 ChangeStackingLevel(old_top->icon->core, WMNormalLevel);
1883 else
1884 ChangeStackingLevel(old_top->icon->core, WMDockLevel);
1885 dock->icon_array[0] = old_top;
1886 XMoveWindow(dpy, old_top->icon->core->window, dock->x_pos, dock->y_pos);
1887 /* we don't need to increment dock->icon_count here because it was
1888 * incremented in the loop above.
1890 } else if (old_top!=dock->icon_array[0]) {
1891 if (old_top == scr->clip_icon)
1892 scr->clip_icon = dock->icon_array[0];
1893 wAppIconDestroy(old_top);
1896 finish:
1897 if (dock_state)
1898 PLRelease(dock_state);
1900 return dock;
1905 void
1906 wDockLaunchWithState(WDock *dock, WAppIcon *btn, WSavedState *state)
1908 if (btn && btn->command && !btn->running && !btn->launching) {
1910 btn->drop_launch = 0;
1912 btn->pid = execCommand(btn, btn->command, state);
1914 if (btn->pid>0) {
1915 if (!btn->forced_dock && !btn->buggy_app) {
1916 btn->launching = 1;
1917 dockIconPaint(btn);
1920 } else {
1921 free(state);
1926 void
1927 wDockDoAutoLaunch(WDock *dock, int workspace)
1929 WAppIcon *btn;
1930 WSavedState *state;
1931 int i;
1933 for (i = 0; i < dock->max_icons; i++) {
1934 btn = dock->icon_array[i];
1935 if (!btn || !btn->auto_launch)
1936 continue;
1938 state = wmalloc(sizeof(WSavedState));
1939 memset(state, 0, sizeof(WSavedState));
1940 state->workspace = workspace;
1941 /* TODO: this is klugy and is very difficult to understand
1942 * what's going on. Try to clean up */
1943 wDockLaunchWithState(dock, btn, state);
1947 #ifdef REDUCE_APPICONS
1948 void
1949 wDockSimulateLaunch(WDock *dock, WAppIcon *btn)
1951 if ((btn == NULL) || (dock == NULL))
1952 return;
1954 if (!btn->running) {
1955 if ((btn->icon->owner == NULL) && (btn->applist))
1956 btn->icon->owner = btn->applist->wapp->main_window_desc;
1957 if (!btn->forced_dock)
1958 btn->launching = 1;
1959 dockIconPaint(btn);
1960 wusleep(5000);
1963 #endif
1965 #ifdef OFFIX_DND
1966 static WDock*
1967 findDock(WScreen *scr, XEvent *event, int *icon_pos)
1969 WDock *dock;
1970 int i;
1972 *icon_pos = -1;
1973 if ((dock = scr->dock)!=NULL) {
1974 for (i=0; i<dock->max_icons; i++) {
1975 if (dock->icon_array[i]
1976 && dock->icon_array[i]->icon->core->window==event->xclient.window) {
1977 *icon_pos = i;
1978 break;
1982 if (*icon_pos<0 && (dock = scr->workspaces[scr->current_workspace]->clip)!=NULL) {
1983 for (i=0; i<dock->max_icons; i++) {
1984 if (dock->icon_array[i]
1985 && dock->icon_array[i]->icon->core->window==event->xclient.window) {
1986 *icon_pos = i;
1987 break;
1991 if(*icon_pos>=0)
1992 return dock;
1993 return NULL;
1998 wDockReceiveDNDDrop(WScreen *scr, XEvent *event)
2000 WDock *dock;
2001 WAppIcon *btn;
2002 int icon_pos;
2004 dock = findDock(scr, event, &icon_pos);
2005 if (!dock)
2006 return False;
2009 * Return True if the drop was on an application icon window.
2010 * In this case, let the ClientMessage handler redirect the
2011 * message to the app.
2013 if (dock->icon_array[icon_pos]->icon->icon_win!=None)
2014 return True;
2016 if (dock->icon_array[icon_pos]->dnd_command!=NULL) {
2017 scr->flags.dnd_data_convertion_status = 0;
2019 btn = dock->icon_array[icon_pos];
2021 if (!btn->forced_dock) {
2022 btn->relaunching = btn->running;
2023 btn->running = 1;
2025 if (btn->wm_instance || btn->wm_class) {
2026 WWindowAttributes attr;
2027 memset(&attr, 0, sizeof(WWindowAttributes));
2028 wDefaultFillAttributes(btn->icon->core->screen_ptr,
2029 btn->wm_instance,
2030 btn->wm_class, &attr, NULL, True);
2032 if (!attr.no_appicon)
2033 btn->launching = 1;
2034 else
2035 btn->running = 0;
2038 btn->drop_launch = 1;
2039 scr->last_dock = dock;
2040 btn->pid = execCommand(btn, btn->dnd_command, NULL);
2041 if (btn->pid>0) {
2042 dockIconPaint(btn);
2043 } else {
2044 btn->launching = 0;
2045 if (!btn->relaunching) {
2046 btn->running = 0;
2050 return False;
2052 #endif /* OFFIX_DND */
2056 Bool
2057 wDockAttachIcon(WDock *dock, WAppIcon *icon, int x, int y)
2059 WWindow *wwin;
2060 char **argv;
2061 int argc;
2062 int index;
2064 wwin = icon->icon->owner;
2065 if (icon->command==NULL) {
2066 icon->editing = 0;
2067 if (XGetCommand(dpy, wwin->client_win, &argv, &argc) && argc>0) {
2069 icon->command = FlattenStringList(argv, argc);
2070 XFreeStringList(argv);
2071 } else {
2072 char *command=NULL;
2074 /* icon->forced_dock = 1;*/
2075 if (!icon->attracted || dock->type!=WM_CLIP || dock->keep_attracted) {
2076 icon->editing = 1;
2077 if (wInputDialog(dock->screen_ptr, _("Dock Icon"),
2078 _("Type the command used to launch the application"),
2079 &command)) {
2080 if (command && (command[0]==0 ||
2081 (command[0]=='-' && command[1]==0))) {
2082 free(command);
2083 command = NULL;
2085 icon->command = command;
2086 icon->editing = 0;
2087 } else {
2088 icon->editing = 0;
2089 if (command)
2090 free(command);
2091 /* If the target is the dock, reject the icon. If
2092 * the target is the clip, make it an attracted icon
2094 if (dock->type==WM_CLIP) {
2095 icon->attracted = 1;
2096 if (!icon->icon->shadowed) {
2097 icon->icon->shadowed = 1;
2098 icon->icon->force_paint = 1;
2100 } else {
2101 return False;
2106 } else {
2107 icon->editing = 0;
2110 for (index=1; index<dock->max_icons; index++)
2111 if (dock->icon_array[index] == NULL)
2112 break;
2113 /* if (index == dock->max_icons)
2114 return; */
2116 assert(index < dock->max_icons);
2118 dock->icon_array[index] = icon;
2119 icon->yindex = y;
2120 icon->xindex = x;
2122 icon->omnipresent = 0;
2124 icon->x_pos = dock->x_pos + x*ICON_SIZE;
2125 icon->y_pos = dock->y_pos + y*ICON_SIZE;
2127 dock->icon_count++;
2129 icon->running = 1;
2130 icon->launching = 0;
2131 icon->docked = 1;
2132 icon->dock = dock;
2133 icon->icon->core->descriptor.handle_mousedown = iconMouseDown;
2134 if (dock->type == WM_CLIP) {
2135 icon->icon->core->descriptor.handle_enternotify = clipEnterNotify;
2136 icon->icon->core->descriptor.handle_leavenotify = clipLeaveNotify;
2138 icon->icon->core->descriptor.parent_type = WCLASS_DOCK_ICON;
2139 icon->icon->core->descriptor.parent = icon;
2141 MoveInStackListUnder(dock->icon_array[index-1]->icon->core,
2142 icon->icon->core);
2143 wAppIconMove(icon, icon->x_pos, icon->y_pos);
2144 wAppIconPaint(icon);
2146 if (wPreferences.auto_arrange_icons)
2147 wArrangeIcons(dock->screen_ptr, True);
2149 #ifdef OFFIX_DND
2150 if (icon->command && !icon->dnd_command) {
2151 icon->dnd_command = wmalloc(strlen(icon->command)+8);
2152 sprintf(icon->dnd_command, "%s %%d", icon->command);
2154 #endif
2156 return True;
2160 void
2161 reattachIcon(WDock *dock, WAppIcon *icon, int x, int y)
2163 int index;
2165 for(index=1; index<dock->max_icons; index++) {
2166 if(dock->icon_array[index] == icon)
2167 break;
2169 assert(index < dock->max_icons);
2171 icon->yindex = y;
2172 icon->xindex = x;
2174 icon->x_pos = dock->x_pos + x*ICON_SIZE;
2175 icon->y_pos = dock->y_pos + y*ICON_SIZE;
2179 Bool
2180 moveIconBetweenDocks(WDock *src, WDock *dest, WAppIcon *icon, int x, int y)
2182 WWindow *wwin;
2183 char **argv;
2184 int argc;
2185 int index;
2187 if (src == dest)
2188 return True; /* No move needed, we're already there */
2190 if (dest == NULL)
2191 return False;
2193 wwin = icon->icon->owner;
2196 * For the moment we can't do this if we move icons in Clip from one
2197 * workspace to other, because if we move two or more icons without
2198 * command, the dialog box will not be able to tell us to which of the
2199 * moved icons it applies. -Dan
2201 if ((dest->type==WM_DOCK /*|| dest->keep_attracted*/) && icon->command==NULL) {
2202 if (XGetCommand(dpy, wwin->client_win, &argv, &argc) && argc>0) {
2204 icon->command = FlattenStringList(argv, argc);
2205 XFreeStringList(argv);
2206 } else {
2207 char *command=NULL;
2209 icon->editing = 1;
2210 /* icon->forced_dock = 1;*/
2211 if (wInputDialog(src->screen_ptr, _("Dock Icon"),
2212 _("Type the command used to launch the application"),
2213 &command)) {
2214 if (command && (command[0]==0 ||
2215 (command[0]=='-' && command[1]==0))) {
2216 free(command);
2217 command = NULL;
2219 icon->command = command;
2220 } else {
2221 icon->editing = 0;
2222 if (command)
2223 free(command);
2224 return False;
2226 icon->editing = 0;
2230 if (dest->type == WM_DOCK)
2231 wClipMakeIconOmnipresent(icon, False);
2233 for(index=1; index<src->max_icons; index++) {
2234 if(src->icon_array[index] == icon)
2235 break;
2237 assert(index < src->max_icons);
2239 src->icon_array[index] = NULL;
2240 src->icon_count--;
2242 for(index=1; index<dest->max_icons; index++) {
2243 if(dest->icon_array[index] == NULL)
2244 break;
2246 /* if (index == dest->max_icons)
2247 return; */
2249 assert(index < dest->max_icons);
2251 dest->icon_array[index] = icon;
2252 icon->dock = dest;
2254 /* deselect the icon */
2255 if (icon->icon->selected)
2256 wIconSelect(icon->icon);
2258 if (dest->type == WM_DOCK) {
2259 icon->icon->core->descriptor.handle_enternotify = NULL;
2260 icon->icon->core->descriptor.handle_leavenotify = NULL;
2261 } else {
2262 icon->icon->core->descriptor.handle_enternotify = clipEnterNotify;
2263 icon->icon->core->descriptor.handle_leavenotify = clipLeaveNotify;
2266 /* set it to be kept when moving to dock, or to a clip that keep the
2267 * attracted icons.
2268 * Unless the icon does not have a command set
2270 if (icon->command && (dest->type==WM_DOCK || dest->keep_attracted)) {
2271 icon->attracted = 0;
2272 if (icon->icon->shadowed) {
2273 icon->icon->shadowed = 0;
2274 icon->icon->force_paint = 1;
2278 if (src->auto_collapse || src->auto_raise_lower)
2279 clipLeave(src);
2281 icon->yindex = y;
2282 icon->xindex = x;
2284 icon->x_pos = dest->x_pos + x*ICON_SIZE;
2285 icon->y_pos = dest->y_pos + y*ICON_SIZE;
2287 dest->icon_count++;
2289 MoveInStackListUnder(dest->icon_array[index-1]->icon->core,
2290 icon->icon->core);
2291 wAppIconPaint(icon);
2293 return True;
2297 void
2298 wDockDetach(WDock *dock, WAppIcon *icon)
2300 int index;
2302 /* make the settings panel be closed */
2303 if (icon->panel) {
2304 DestroyDockAppSettingsPanel(icon->panel);
2307 icon->docked = 0;
2308 icon->dock = NULL;
2309 icon->attracted = 0;
2310 if (icon->icon->shadowed) {
2311 icon->icon->shadowed = 0;
2312 icon->icon->force_paint = 1;
2315 /* This must be called before dock->icon_array[index] is set to NULL.
2316 * Don't move it. -Dan
2318 wClipMakeIconOmnipresent(icon, False);
2320 /* deselect the icon */
2321 if (icon->icon->selected)
2322 wIconSelect(icon->icon);
2324 if (icon->command) {
2325 free(icon->command);
2326 icon->command = NULL;
2328 #ifdef OFFIX_DND
2329 if (icon->dnd_command) {
2330 free(icon->dnd_command);
2331 icon->dnd_command = NULL;
2333 #endif
2335 for (index=1; index<dock->max_icons; index++)
2336 if (dock->icon_array[index] == icon)
2337 break;
2338 assert(index < dock->max_icons);
2339 dock->icon_array[index] = NULL;
2340 icon->yindex = -1;
2341 icon->xindex = -1;
2343 dock->icon_count--;
2345 /* if the dock is not attached to an application or
2346 * the the application did not set the approriate hints yet,
2347 * destroy the icon */
2348 #ifdef REDUCE_APPICONS
2349 if ((icon->num_apps == 0) && (!icon->running || !wApplicationOf(icon->main_window)) )
2350 #else
2351 if (!icon->running || !wApplicationOf(icon->main_window))
2352 #endif
2353 wAppIconDestroy(icon);
2354 else {
2355 icon->icon->core->descriptor.handle_mousedown = appIconMouseDown;
2356 icon->icon->core->descriptor.handle_enternotify = NULL;
2357 icon->icon->core->descriptor.handle_leavenotify = NULL;
2358 icon->icon->core->descriptor.parent_type = WCLASS_APPICON;
2359 icon->icon->core->descriptor.parent = icon;
2361 ChangeStackingLevel(icon->icon->core, NORMAL_ICON_LEVEL);
2363 wAppIconPaint(icon);
2364 if (wPreferences.auto_arrange_icons) {
2365 wArrangeIcons(dock->screen_ptr, True);
2368 if (dock->auto_collapse || dock->auto_raise_lower)
2369 clipLeave(dock);
2374 * returns the closest Dock slot index for the passed
2375 * coordinates.
2377 * Returns False if icon can't be docked.
2379 * Note: this function should NEVER alter ret_x or ret_y, unless it will
2380 * return True. -Dan
2382 Bool
2383 wDockSnapIcon(WDock *dock, WAppIcon *icon, int req_x, int req_y,
2384 int *ret_x, int *ret_y, int redocking)
2386 WScreen *scr = dock->screen_ptr;
2387 int dx, dy;
2388 int ex_x, ex_y;
2389 int i, offset = ICON_SIZE/2;
2390 WAppIcon *aicon = NULL;
2391 WAppIcon *nicon = NULL;
2392 int max_y_icons, max_x_icons;
2394 max_x_icons = scr->scr_width/ICON_SIZE;
2395 max_y_icons = scr->scr_height/ICON_SIZE-1;
2397 if (wPreferences.flags.noupdates)
2398 return False;
2400 dx = dock->x_pos;
2401 dy = dock->y_pos;
2403 /* if the dock is full */
2404 if (!redocking &&
2405 (dock->icon_count >= dock->max_icons)) {
2406 return False;
2409 /* exact position */
2410 if (req_y < dy)
2411 ex_y = (req_y - offset - dy)/ICON_SIZE;
2412 else
2413 ex_y = (req_y + offset - dy)/ICON_SIZE;
2415 if (req_x < dx)
2416 ex_x = (req_x - offset - dx)/ICON_SIZE;
2417 else
2418 ex_x = (req_x + offset - dx)/ICON_SIZE;
2420 /* check if the icon is outside the screen boundaries */
2421 if (dx + ex_x*ICON_SIZE < -ICON_SIZE+2 ||
2422 dx + ex_x*ICON_SIZE >= scr->scr_width-1 ||
2423 dy + ex_y*ICON_SIZE < -ICON_SIZE+2 ||
2424 dy + ex_y*ICON_SIZE >= scr->scr_height-1)
2425 return False;
2427 if (dock->type == WM_DOCK) {
2428 if (icon->dock != dock && ex_x != 0)
2429 return False;
2431 aicon = NULL;
2432 for (i=0; i<dock->max_icons; i++) {
2433 nicon = dock->icon_array[i];
2434 if (nicon && nicon->yindex == ex_y) {
2435 aicon = nicon;
2436 break;
2440 if (redocking) {
2441 int sig, done, closest;
2443 /* Possible cases when redocking:
2445 * icon dragged out of range of any slot -> false
2446 * icon dragged to range of free slot
2447 * icon dragged to range of same slot
2448 * icon dragged to range of different icon
2450 if (abs(ex_x) > DOCK_DETTACH_THRESHOLD)
2451 return False;
2453 if (ex_y>=0 && ex_y<=max_y_icons && (aicon==icon || !aicon)) {
2454 *ret_x = 0;
2455 *ret_y = ex_y;
2456 return True;
2459 /* start looking at the upper slot or lower? */
2460 if (ex_y*ICON_SIZE < (req_y + offset - dy))
2461 sig = 1;
2462 else
2463 sig = -1;
2465 closest = -1;
2466 done = 0;
2467 /* look for closest free slot */
2468 for (i=0; i<(DOCK_DETTACH_THRESHOLD+1)*2 && !done; i++) {
2469 int j;
2471 done = 1;
2472 closest = sig*(i/2) + ex_y;
2473 /* check if this slot is used */
2474 if (closest >= 0) {
2475 for (j = 0; j<dock->max_icons; j++) {
2476 if (dock->icon_array[j]
2477 && dock->icon_array[j]->yindex==closest) {
2478 /* slot is used by someone else */
2479 if (dock->icon_array[j]!=icon)
2480 done = 0;
2481 break;
2485 sig = -sig;
2487 if (done && closest >= 0 && closest <= max_y_icons &&
2488 ((ex_y >= closest && ex_y - closest < DOCK_DETTACH_THRESHOLD+1)
2490 (ex_y < closest && closest - ex_y <= DOCK_DETTACH_THRESHOLD+1))) {
2491 *ret_x = 0;
2492 *ret_y = closest;
2493 return True;
2495 } else { /* !redocking */
2497 /* if slot is free and the icon is close enough, return it */
2498 if (!aicon && ex_x == 0 && ex_y >= 0 && ex_y <= max_y_icons) {
2499 *ret_x = 0;
2500 *ret_y = ex_y;
2501 return True;
2504 } else { /* CLIP */
2505 int neighbours = 0;
2506 int start, stop, k;
2508 start = icon->omnipresent ? 0 : scr->current_workspace;
2509 stop = icon->omnipresent ? scr->workspace_count : start+1;
2511 aicon = NULL;
2512 for (k=start; k<stop; k++) {
2513 WDock *tmp = scr->workspaces[k]->clip;
2514 if (!tmp)
2515 continue;
2516 for (i=0; i<tmp->max_icons; i++) {
2517 nicon = tmp->icon_array[i];
2518 if (nicon && nicon->xindex == ex_x && nicon->yindex == ex_y) {
2519 aicon = nicon;
2520 break;
2523 if (aicon)
2524 break;
2526 for (k=start; k<stop; k++) {
2527 WDock *tmp = scr->workspaces[k]->clip;
2528 if (!tmp)
2529 continue;
2530 for (i=0; i<tmp->max_icons; i++) {
2531 nicon = tmp->icon_array[i];
2532 if (nicon && nicon != icon && /* Icon can't be it's own neighbour */
2533 (abs(nicon->xindex - ex_x) <= CLIP_ATTACH_VICINITY &&
2534 abs(nicon->yindex - ex_y) <= CLIP_ATTACH_VICINITY)) {
2535 neighbours = 1;
2536 break;
2539 if (neighbours)
2540 break;
2543 if (neighbours && (aicon==NULL || (redocking && aicon == icon))) {
2544 *ret_x = ex_x;
2545 *ret_y = ex_y;
2546 return True;
2549 return False;
2552 #define MIN(x, y) ((x) > (y) ? (y) : (x))
2553 #define MAX(x, y) ((x) < (y) ? (y) : (x))
2555 #define ON_SCREEN(x, y, sx, ex, sy, ey) \
2556 ((((x)+ICON_SIZE/2) >= (sx)) && (((y)+ICON_SIZE/2) >= (sy)) && \
2557 (((x) + (ICON_SIZE/2)) <= (ex)) && (((y) + (ICON_SIZE/2)) <= ey))
2561 * returns true if it can find a free slot in the dock,
2562 * in which case it changes x_pos and y_pos accordingly.
2563 * Else returns false.
2565 Bool
2566 wDockFindFreeSlot(WDock *dock, int *x_pos, int *y_pos)
2568 WScreen *scr = dock->screen_ptr;
2569 WAppIcon *btn;
2570 WAppIconChain *chain;
2571 unsigned char *slot_map;
2572 int mwidth;
2573 int r;
2574 int x, y;
2575 int i, done = False;
2576 int corner;
2577 int sx=0, sy=0, ex=scr->scr_width, ey=scr->scr_height;
2578 int extra_count=0;
2580 if (dock->type == WM_CLIP &&
2581 dock != scr->workspaces[scr->current_workspace]->clip)
2582 extra_count = scr->global_icon_count;
2584 /* if the dock is full */
2585 if (dock->icon_count+extra_count >= dock->max_icons) {
2586 return False;
2589 if (!wPreferences.flags.nodock && scr->dock) {
2590 if (scr->dock->on_right_side)
2591 ex -= ICON_SIZE + DOCK_EXTRA_SPACE;
2592 else
2593 sx += ICON_SIZE + DOCK_EXTRA_SPACE;
2596 if (ex < dock->x_pos)
2597 ex = dock->x_pos;
2598 if (sx > dock->x_pos+ICON_SIZE)
2599 sx = dock->x_pos+ICON_SIZE;
2600 #define C_NONE 0
2601 #define C_NW 1
2602 #define C_NE 2
2603 #define C_SW 3
2604 #define C_SE 4
2606 /* check if clip is in a corner */
2607 if (dock->type==WM_CLIP) {
2608 if (dock->x_pos < 1 && dock->y_pos < 1)
2609 corner = C_NE;
2610 else if (dock->x_pos < 1 && dock->y_pos >= (ey-ICON_SIZE))
2611 corner = C_SE;
2612 else if (dock->x_pos >= (ex-ICON_SIZE)&& dock->y_pos >= (ey-ICON_SIZE))
2613 corner = C_SW;
2614 else if (dock->x_pos >= (ex-ICON_SIZE) && dock->y_pos < 1)
2615 corner = C_NW;
2616 else
2617 corner = C_NONE;
2618 } else
2619 corner = C_NONE;
2621 /* If the clip is in the corner, use only slots that are in the border
2622 * of the screen */
2623 if (corner!=C_NONE) {
2624 char *hmap, *vmap;
2625 int hcount, vcount;
2627 hcount = MIN(dock->max_icons, scr->scr_width/ICON_SIZE);
2628 vcount = MIN(dock->max_icons, scr->scr_height/ICON_SIZE);
2629 hmap = wmalloc(hcount+1);
2630 memset(hmap, 0, hcount+1);
2631 vmap = wmalloc(vcount+1);
2632 memset(vmap, 0, vcount+1);
2634 /* mark used positions */
2635 switch (corner) {
2636 case C_NE:
2637 for (i=0; i<dock->max_icons; i++) {
2638 btn = dock->icon_array[i];
2639 if (!btn)
2640 continue;
2642 if (btn->xindex==0 && btn->yindex > 0 && btn->yindex < vcount)
2643 vmap[btn->yindex] = 1;
2644 else if (btn->yindex==0 && btn->xindex>0 && btn->xindex<hcount)
2645 hmap[btn->xindex] = 1;
2647 for (chain=scr->global_icons; chain!=NULL; chain=chain->next) {
2648 btn = chain->aicon;
2649 if (btn->xindex==0 && btn->yindex > 0 && btn->yindex < vcount)
2650 vmap[btn->yindex] = 1;
2651 else if (btn->yindex==0 && btn->xindex>0 && btn->xindex<hcount)
2652 hmap[btn->xindex] = 1;
2654 break;
2655 case C_NW:
2656 for (i=0; i<dock->max_icons; i++) {
2657 btn = dock->icon_array[i];
2658 if (!btn)
2659 continue;
2661 if (btn->xindex==0 && btn->yindex > 0 && btn->yindex < vcount)
2662 vmap[btn->yindex] = 1;
2663 else if (btn->yindex==0 && btn->xindex<0 &&btn->xindex>-hcount)
2664 hmap[-btn->xindex] = 1;
2666 for (chain=scr->global_icons; chain!=NULL; chain=chain->next) {
2667 btn = chain->aicon;
2668 if (btn->xindex==0 && btn->yindex > 0 && btn->yindex < vcount)
2669 vmap[btn->yindex] = 1;
2670 else if (btn->yindex==0 && btn->xindex<0 &&btn->xindex>-hcount)
2671 hmap[-btn->xindex] = 1;
2673 break;
2674 case C_SE:
2675 for (i=0; i<dock->max_icons; i++) {
2676 btn = dock->icon_array[i];
2677 if (!btn)
2678 continue;
2680 if (btn->xindex==0 && btn->yindex < 0 && btn->yindex > -vcount)
2681 vmap[-btn->yindex] = 1;
2682 else if (btn->yindex==0 && btn->xindex>0 && btn->xindex<hcount)
2683 hmap[btn->xindex] = 1;
2685 for (chain=scr->global_icons; chain!=NULL; chain=chain->next) {
2686 btn = chain->aicon;
2687 if (btn->xindex==0 && btn->yindex < 0 && btn->yindex > -vcount)
2688 vmap[-btn->yindex] = 1;
2689 else if (btn->yindex==0 && btn->xindex>0 && btn->xindex<hcount)
2690 hmap[btn->xindex] = 1;
2692 break;
2693 case C_SW:
2694 default:
2695 for (i=0; i<dock->max_icons; i++) {
2696 btn = dock->icon_array[i];
2697 if (!btn)
2698 continue;
2700 if (btn->xindex==0 && btn->yindex < 0 && btn->yindex > -vcount)
2701 vmap[-btn->yindex] = 1;
2702 else if (btn->yindex==0 && btn->xindex<0 &&btn->xindex>-hcount)
2703 hmap[-btn->xindex] = 1;
2705 for (chain=scr->global_icons; chain!=NULL; chain=chain->next) {
2706 btn = chain->aicon;
2707 if (btn->xindex==0 && btn->yindex < 0 && btn->yindex > -vcount)
2708 vmap[-btn->yindex] = 1;
2709 else if (btn->yindex==0 && btn->xindex<0 &&btn->xindex>-hcount)
2710 hmap[-btn->xindex] = 1;
2713 x=0; y=0;
2714 done = 0;
2715 /* search a vacant slot */
2716 for (i=1; i<MAX(vcount, hcount); i++) {
2717 if (i < vcount && vmap[i]==0) {
2718 /* found a slot */
2719 x = 0;
2720 y = i;
2721 done = 1;
2722 break;
2723 } else if (i < hcount && hmap[i]==0) {
2724 /* found a slot */
2725 x = i;
2726 y = 0;
2727 done = 1;
2728 break;
2731 free(vmap);
2732 free(hmap);
2733 /* If found a slot, translate and return */
2734 if (done) {
2735 if (corner==C_NW || corner==C_NE) {
2736 *y_pos = y;
2737 } else {
2738 *y_pos = -y;
2740 if (corner==C_NE || corner==C_SE) {
2741 *x_pos = x;
2742 } else {
2743 *x_pos = -x;
2745 return True;
2747 /* else, try to find a slot somewhere else */
2750 /* a map of mwidth x mwidth would be enough if we allowed icons to be
2751 * placed outside of screen */
2752 mwidth = (int)ceil(sqrt(dock->max_icons));
2754 /* In the worst case (the clip is in the corner of the screen),
2755 * the amount of icons that fit in the clip is smaller.
2756 * Double the map to get a safe value.
2758 mwidth += mwidth;
2760 r = (mwidth-1)/2;
2762 slot_map = wmalloc(mwidth*mwidth);
2763 memset(slot_map, 0, mwidth*mwidth);
2765 #define XY2OFS(x,y) (MAX(abs(x),abs(y)) > r) ? 0 : (((y)+r)*(mwidth)+(x)+r)
2767 /* mark used slots in the map. If the slot falls outside the map
2768 * (for example, when all icons are placed in line), ignore them. */
2769 for (i=0; i<dock->max_icons; i++) {
2770 btn = dock->icon_array[i];
2771 if (btn)
2772 slot_map[XY2OFS(btn->xindex, btn->yindex)] = 1;
2774 for (chain=scr->global_icons; chain!=NULL; chain=chain->next) {
2775 slot_map[XY2OFS(chain->aicon->xindex, chain->aicon->yindex)] = 1;
2777 /* Find closest slot from the center that is free by scanning the
2778 * map from the center to outward in circular passes.
2779 * This will not result in a neat layout, but will be optimal
2780 * in the sense that there will not be holes left.
2782 done = 0;
2783 for (i = 1; i <= r && !done; i++) {
2784 int tx, ty;
2786 /* top and bottom parts of the ring */
2787 for (x = -i; x <= i && !done; x++) {
2788 tx = dock->x_pos + x*ICON_SIZE;
2789 y = -i;
2790 ty = dock->y_pos + y*ICON_SIZE;
2791 if (slot_map[XY2OFS(x,y)]==0
2792 && ON_SCREEN(tx, ty, sx, ex, sy, ey)) {
2793 *x_pos = x;
2794 *y_pos = y;
2795 done = 1;
2796 break;
2798 y = i;
2799 ty = dock->y_pos + y*ICON_SIZE;
2800 if (slot_map[XY2OFS(x,y)]==0
2801 && ON_SCREEN(tx, ty, sx, ex, sy, ey)) {
2802 *x_pos = x;
2803 *y_pos = y;
2804 done = 1;
2805 break;
2808 /* left and right parts of the ring */
2809 for (y = -i+1; y <= i-1; y++) {
2810 ty = dock->y_pos + y*ICON_SIZE;
2811 x = -i;
2812 tx = dock->x_pos + x*ICON_SIZE;
2813 if (slot_map[XY2OFS(x,y)]==0
2814 && ON_SCREEN(tx, ty, sx, ex, sy, ey)) {
2815 *x_pos = x;
2816 *y_pos = y;
2817 done = 1;
2818 break;
2820 x = i;
2821 tx = dock->x_pos + x*ICON_SIZE;
2822 if (slot_map[XY2OFS(x,y)]==0
2823 && ON_SCREEN(tx, ty, sx, ex, sy, ey)) {
2824 *x_pos = x;
2825 *y_pos = y;
2826 done = 1;
2827 break;
2831 free(slot_map);
2832 #undef XY2OFS
2833 return done;
2837 static void
2838 moveDock(WDock *dock, int new_x, int new_y)
2840 WAppIcon *btn;
2841 int i;
2843 dock->x_pos = new_x;
2844 dock->y_pos = new_y;
2845 for (i=0; i<dock->max_icons; i++) {
2846 btn = dock->icon_array[i];
2847 if (btn) {
2848 btn->x_pos = new_x + btn->xindex*ICON_SIZE;
2849 btn->y_pos = new_y + btn->yindex*ICON_SIZE;
2850 XMoveWindow(dpy, btn->icon->core->window, btn->x_pos, btn->y_pos);
2856 static void
2857 swapDock(WDock *dock)
2859 WScreen *scr = dock->screen_ptr;
2860 WAppIcon *btn;
2861 int x, i;
2864 if (dock->on_right_side) {
2865 x = dock->x_pos = scr->scr_width - ICON_SIZE - DOCK_EXTRA_SPACE;
2866 } else {
2867 x = dock->x_pos = DOCK_EXTRA_SPACE;
2870 for (i=0; i<dock->max_icons; i++) {
2871 btn = dock->icon_array[i];
2872 if (btn) {
2873 btn->x_pos = x;
2874 XMoveWindow(dpy, btn->icon->core->window, btn->x_pos, btn->y_pos);
2878 wScreenUpdateUsableArea(scr);
2882 static pid_t
2883 execCommand(WAppIcon *btn, char *command, WSavedState *state)
2885 WScreen *scr = btn->icon->core->screen_ptr;
2886 pid_t pid;
2887 char **argv;
2888 int argc;
2889 char *cmdline;
2891 cmdline = ExpandOptions(scr, command);
2893 if (scr->flags.dnd_data_convertion_status || !cmdline) {
2894 if (cmdline)
2895 free(cmdline);
2896 if (state)
2897 free(state);
2898 return 0;
2901 ParseCommand(cmdline, &argv, &argc);
2903 if (argv==NULL) {
2904 if (cmdline)
2905 free(cmdline);
2906 if (state)
2907 free(state);
2908 return 0;
2911 if ((pid=fork())==0) {
2912 char **args;
2913 int i;
2915 SetupEnvironment(scr);
2917 #ifdef HAVE_SETPGID
2918 setpgid(0, 0);
2919 #endif
2921 args = malloc(sizeof(char*)*(argc+1));
2922 if (!args)
2923 exit(111);
2924 for (i=0; i<argc; i++) {
2925 args[i] = argv[i];
2927 args[argc] = NULL;
2928 execvp(argv[0], args);
2929 exit(111);
2931 while (argc > 0)
2932 free(argv[--argc]);
2933 free(argv);
2935 if (pid > 0) {
2936 if (!state) {
2937 state = wmalloc(sizeof(WSavedState));
2938 memset(state, 0, sizeof(WSavedState));
2939 state->hidden = -1;
2940 state->miniaturized = -1;
2941 state->shaded = -1;
2942 if (btn->dock==scr->dock || btn->omnipresent)
2943 state->workspace = -1;
2944 else
2945 state->workspace = scr->current_workspace;
2947 wWindowAddSavedState(btn->wm_instance, btn->wm_class, cmdline, pid,
2948 state);
2949 wAddDeathHandler(pid, (WDeathHandler*)trackDeadProcess,
2950 btn->dock);
2951 } else if (state) {
2952 free(state);
2954 free(cmdline);
2955 return pid;
2959 void
2960 wDockHideIcons(WDock *dock)
2962 int i;
2964 if (dock==NULL)
2965 return;
2967 for (i=1; i<dock->max_icons; i++) {
2968 if (dock->icon_array[i])
2969 XUnmapWindow(dpy, dock->icon_array[i]->icon->core->window);
2971 dock->mapped = 0;
2973 dockIconPaint(dock->icon_array[0]);
2977 void
2978 wDockShowIcons(WDock *dock)
2980 int i, newlevel;
2981 WAppIcon *btn;
2983 if (dock==NULL)
2984 return;
2986 btn = dock->icon_array[0];
2987 moveDock(dock, btn->x_pos, btn->y_pos);
2989 newlevel = dock->lowered ? WMNormalLevel : WMDockLevel;
2990 ChangeStackingLevel(btn->icon->core, newlevel);
2992 for (i=1; i<dock->max_icons; i++) {
2993 if (dock->icon_array[i]) {
2994 MoveInStackListAbove(dock->icon_array[i]->icon->core,
2995 btn->icon->core);
2996 break;
3000 if (!dock->collapsed) {
3001 for (i=1; i<dock->max_icons; i++) {
3002 if (dock->icon_array[i]) {
3003 XMapWindow(dpy, dock->icon_array[i]->icon->core->window);
3007 dock->mapped = 1;
3009 dockIconPaint(btn);
3013 void
3014 wDockLower(WDock *dock)
3016 int i;
3018 for (i=0; i<dock->max_icons; i++) {
3019 if (dock->icon_array[i])
3020 wLowerFrame(dock->icon_array[i]->icon->core);
3025 void
3026 wDockRaise(WDock *dock)
3028 int i;
3030 for (i=dock->max_icons-1; i>=0; i--) {
3031 if (dock->icon_array[i])
3032 wRaiseFrame(dock->icon_array[i]->icon->core);
3037 void
3038 wDockRaiseLower(WDock *dock)
3040 if (!dock->icon_array[0]->icon->core->stacking->above
3041 ||(dock->icon_array[0]->icon->core->stacking->window_level
3042 !=dock->icon_array[0]->icon->core->stacking->above->stacking->window_level))
3043 wDockLower(dock);
3044 else
3045 wDockRaise(dock);
3049 void
3050 wDockFinishLaunch(WDock *dock, WAppIcon *icon)
3052 icon->launching = 0;
3053 icon->relaunching = 0;
3054 dockIconPaint(icon);
3058 WAppIcon*
3059 wDockFindIconFor(WDock *dock, Window window)
3061 WAppIcon *icon;
3062 int i;
3064 for (i=0; i<dock->max_icons; i++) {
3065 icon = dock->icon_array[i];
3066 if (icon && icon->main_window == window)
3067 return icon;
3069 return NULL;
3073 void
3074 wDockTrackWindowLaunch(WDock *dock, Window window)
3076 WAppIcon *icon;
3077 #ifdef REDUCE_APPICONS
3078 WAppIconAppList *tapplist;
3079 #endif
3080 char *wm_class, *wm_instance;
3081 int i;
3082 Bool firstPass = True;
3083 Bool found = False;
3084 char *command = NULL;
3087 int argc;
3088 char **argv;
3090 if (XGetCommand(dpy, window, &argv, &argc)) {
3091 if (argc > 0 && argv != NULL)
3092 command = FlattenStringList(argv,argc);
3093 if (argv) {
3094 XFreeStringList(argv);
3099 if (!PropGetWMClass(window, &wm_class, &wm_instance) ||
3100 (!wm_class && !wm_instance))
3101 return;
3103 retry:
3104 for (i=0; i<dock->max_icons; i++) {
3105 icon = dock->icon_array[i];
3106 if (!icon)
3107 continue;
3109 /* app is already attached to icon */
3110 if (icon->main_window == window) {
3111 found = True;
3112 break;
3115 if ((icon->wm_instance || icon->wm_class)
3116 && (icon->launching
3117 || (dock->screen_ptr->flags.startup && !icon->running))) {
3119 if (icon->wm_instance && wm_instance &&
3120 strcmp(icon->wm_instance, wm_instance)!=0) {
3121 continue;
3123 if (icon->wm_class && wm_class &&
3124 strcmp(icon->wm_class, wm_class)!=0) {
3125 continue;
3127 if (firstPass && command && strcmp(icon->command, command)!=0) {
3128 continue;
3131 if (!icon->relaunching) {
3132 WApplication *wapp;
3134 /* Possibly an application that was docked with dockit,
3135 * but the user did not update WMState to indicate that
3136 * it was docked by force */
3137 wapp = wApplicationOf(window);
3138 if (!wapp) {
3139 icon->forced_dock = 1;
3140 icon->running = 0;
3142 if (!icon->forced_dock)
3143 icon->main_window = window;
3145 #ifdef REDUCE_APPICONS
3146 tapplist = wmalloc(sizeof(WAppIconAppList));
3147 memset(tapplist, 0, sizeof(WAppIconAppList));
3148 tapplist->next = icon->applist;
3149 if (icon->applist)
3150 icon->applist->prev = tapplist;
3151 icon->applist = tapplist;
3152 tapplist->wapp = wApplicationOf(window);
3153 icon->num_apps++;
3154 #endif
3156 found = True;
3157 wDockFinishLaunch(dock, icon);
3158 break;
3162 if (firstPass && !found) {
3163 firstPass = False;
3164 goto retry;
3167 if (command)
3168 free(command);
3170 if (wm_class)
3171 XFree(wm_class);
3172 if (wm_instance)
3173 XFree(wm_instance);
3178 void
3179 wClipUpdateForWorkspaceChange(WScreen *scr, int workspace)
3181 if (!wPreferences.flags.noclip) {
3182 scr->clip_icon->dock = scr->workspaces[workspace]->clip;
3183 if (scr->current_workspace != workspace) {
3184 WDock *old_clip = scr->workspaces[scr->current_workspace]->clip;
3185 WAppIconChain *chain = scr->global_icons;
3187 while (chain) {
3188 moveIconBetweenDocks(chain->aicon->dock,
3189 scr->workspaces[workspace]->clip,
3190 chain->aicon, chain->aicon->xindex,
3191 chain->aicon->yindex);
3192 if (scr->workspaces[workspace]->clip->collapsed)
3193 XUnmapWindow(dpy, chain->aicon->icon->core->window);
3194 chain = chain->next;
3197 wDockHideIcons(old_clip);
3198 if (old_clip->auto_raise_lower) {
3199 if (old_clip->auto_raise_magic) {
3200 WMDeleteTimerHandler(old_clip->auto_raise_magic);
3201 old_clip->auto_raise_magic = NULL;
3203 wDockLower(old_clip);
3205 if (old_clip->auto_collapse) {
3206 if (old_clip->auto_expand_magic) {
3207 WMDeleteTimerHandler(old_clip->auto_expand_magic);
3208 old_clip->auto_expand_magic = NULL;
3210 old_clip->collapsed = 1;
3212 wDockShowIcons(scr->workspaces[workspace]->clip);
3214 if (scr->flags.clip_balloon_mapped)
3215 showClipBalloon(scr->clip_icon->dock, workspace);
3221 static void
3222 trackDeadProcess(pid_t pid, unsigned char status, WDock *dock)
3224 WAppIcon *icon;
3225 int i;
3227 for (i=0; i<dock->max_icons; i++) {
3228 icon = dock->icon_array[i];
3229 if (!icon)
3230 continue;
3232 if (icon->launching && icon->pid == pid) {
3233 if (!icon->relaunching) {
3234 icon->running = 0;
3235 icon->main_window = None;
3237 wDockFinishLaunch(dock, icon);
3238 icon->pid = 0;
3239 if (status==111) {
3240 char msg[PATH_MAX];
3241 #ifdef OFFIX_DND
3242 sprintf(msg, _("Could not execute command \"%s\""),
3243 icon->drop_launch && icon->dnd_command
3244 ? icon->dnd_command : icon->command);
3245 #else
3246 sprintf(msg, _("Could not execute command \"%s\""),
3247 icon->command);
3248 #endif
3249 wMessageDialog(dock->screen_ptr, _("Error"), msg,
3250 _("OK"), NULL, NULL);
3252 break;
3258 static void
3259 toggleLowered(WDock *dock)
3261 WAppIcon *tmp;
3262 int newlevel, i;
3264 /* lower/raise Dock */
3265 if (!dock->lowered) {
3266 newlevel = WMNormalLevel;
3267 dock->lowered = 1;
3268 } else {
3269 newlevel = WMDockLevel;
3270 dock->lowered = 0;
3273 for (i=0; i<dock->max_icons; i++) {
3274 tmp = dock->icon_array[i];
3275 if (!tmp)
3276 continue;
3278 ChangeStackingLevel(tmp->icon->core, newlevel);
3279 if (dock->lowered)
3280 wLowerFrame(tmp->icon->core);
3283 if (dock->type == WM_DOCK)
3284 wScreenUpdateUsableArea(dock->screen_ptr);
3288 static void
3289 toggleCollapsed(WDock *dock)
3291 if (dock->collapsed) {
3292 dock->collapsed = 0;
3293 wDockShowIcons(dock);
3295 else {
3296 dock->collapsed = 1;
3297 wDockHideIcons(dock);
3302 static void
3303 openDockMenu(WDock *dock, WAppIcon *aicon, XEvent *event)
3305 WScreen *scr = dock->screen_ptr;
3306 WObjDescriptor *desc;
3307 WMenuEntry *entry;
3308 WApplication *wapp = NULL;
3309 int index = 0;
3310 int x_pos;
3311 int n_selected;
3312 int appIsRunning = aicon->running && aicon->icon && aicon->icon->owner;
3314 if (dock->type == WM_DOCK) {
3315 /* keep on top */
3316 entry = dock->menu->entries[index];
3317 entry->flags.indicator_on = !dock->lowered;
3318 entry->clientdata = dock;
3319 } else {
3320 /* clip options */
3321 if (scr->clip_options)
3322 updateClipOptionsMenu(scr->clip_options, dock);
3324 /* Rename Workspace */
3325 entry = dock->menu->entries[++index];
3326 entry->clientdata = dock;
3328 /* select/unselect icon */
3329 entry = dock->menu->entries[++index];
3330 entry->clientdata = aicon;
3331 if (aicon->icon->selected)
3332 entry->text = _("Unselect Icon");
3333 else
3334 entry->text = _("Select Icon");
3335 wMenuSetEnabled(dock->menu, index, aicon!=scr->clip_icon);
3337 n_selected = numberOfSelectedIcons(dock);
3338 /* select/unselect all icons */
3339 entry = dock->menu->entries[++index];
3340 entry->clientdata = aicon;
3341 if (n_selected > 0)
3342 entry->text = _("Unselect All Icons");
3343 else
3344 entry->text = _("Select All Icons");
3345 wMenuSetEnabled(dock->menu, index, dock->icon_count > 1);
3347 /* keep icon(s) */
3348 entry = dock->menu->entries[++index];
3349 entry->clientdata = aicon;
3350 if (n_selected > 1)
3351 entry->text = _("Keep Icons");
3352 else
3353 entry->text = _("Keep Icon");
3354 wMenuSetEnabled(dock->menu, index, dock->icon_count > 1);
3356 /* this is the workspace submenu part */
3357 entry = dock->menu->entries[++index];
3358 if (n_selected > 1)
3359 entry->text = _("Move Icons To");
3360 else
3361 entry->text = _("Move Icon To");
3362 if (scr->clip_submenu)
3363 updateWorkspaceMenu(scr->clip_submenu, aicon);
3364 wMenuSetEnabled(dock->menu, index, !aicon->omnipresent);
3366 /* remove icon(s) */
3367 entry = dock->menu->entries[++index];
3368 entry->clientdata = aicon;
3369 if (n_selected > 1)
3370 entry->text = _("Remove Icons");
3371 else
3372 entry->text = _("Remove Icon");
3373 wMenuSetEnabled(dock->menu, index, dock->icon_count > 1);
3375 /* attract icon(s) */
3376 entry = dock->menu->entries[++index];
3377 entry->clientdata = aicon;
3380 /* launch */
3381 entry = dock->menu->entries[++index];
3382 entry->clientdata = aicon;
3383 wMenuSetEnabled(dock->menu, index, aicon->command!=NULL);
3385 /* unhide here */
3386 entry = dock->menu->entries[++index];
3387 entry->clientdata = aicon;
3388 wMenuSetEnabled(dock->menu, index, appIsRunning);
3390 /* hide */
3391 entry = dock->menu->entries[++index];
3392 entry->clientdata = aicon;
3393 if (aicon->icon->owner) {
3394 wapp = wApplicationOf(aicon->icon->owner->main_window);
3395 if (wapp && wapp->flags.hidden)
3396 entry->text = _("Unhide");
3397 else
3398 entry->text = _("Hide");
3399 } else {
3400 entry->text = _("Hide");
3402 wMenuSetEnabled(dock->menu, index, appIsRunning);
3404 /* settings */
3405 entry = dock->menu->entries[++index];
3406 entry->clientdata = aicon;
3407 wMenuSetEnabled(dock->menu, index, !aicon->editing
3408 && !wPreferences.flags.noupdates);
3410 /* kill */
3411 entry = dock->menu->entries[++index];
3412 entry->clientdata = aicon;
3413 wMenuSetEnabled(dock->menu, index, appIsRunning);
3415 if (!dock->menu->flags.realized)
3416 wMenuRealize(dock->menu);
3418 if (dock->type == WM_CLIP) {
3419 x_pos = event->xbutton.x_root+2;
3420 } else {
3421 x_pos = dock->on_right_side ?
3422 scr->scr_width - dock->menu->frame->core->width - 2 : 0;
3425 wMenuMapAt(dock->menu, x_pos, event->xbutton.y_root+2, False);
3427 /* allow drag select */
3428 event->xany.send_event = True;
3429 desc = &dock->menu->menu->descriptor;
3430 (*desc->handle_mousedown)(desc, event);
3434 static void
3435 openClipWorkspaceMenu(WScreen *scr, int x, int y)
3437 if (!scr->clip_ws_menu) {
3438 scr->clip_ws_menu = wWorkspaceMenuMake(scr, False);
3440 wWorkspaceMenuUpdate(scr, scr->clip_ws_menu);
3441 wMenuMapAt(scr->clip_ws_menu, x, y, False);
3445 /******************************************************************/
3446 static void
3447 iconDblClick(WObjDescriptor *desc, XEvent *event)
3449 WAppIcon *btn = desc->parent;
3450 WDock *dock = btn->dock;
3451 WApplication *wapp = NULL;
3452 int unhideHere = 0;
3454 #ifdef REDUCE_APPICONS
3455 if ((btn->icon->owner && !(event->xbutton.state & ControlMask)) ||
3456 ((btn->icon->owner == NULL) && (btn->applist != NULL))) {
3457 if (btn->icon->owner == NULL)
3458 btn->icon->owner = btn->applist->wapp->main_window_desc;
3460 #else
3461 if (btn->icon->owner && !(event->xbutton.state & ControlMask)) {
3462 #endif
3463 wapp = wApplicationOf(btn->icon->owner->main_window);
3465 assert(wapp!=NULL);
3467 unhideHere = (event->xbutton.state & ShiftMask);
3469 /* go to the last workspace that the user worked on the app */
3470 if (wapp->last_workspace != dock->screen_ptr->current_workspace
3471 && !unhideHere) {
3472 wWorkspaceChange(dock->screen_ptr, wapp->last_workspace);
3475 wUnhideApplication(wapp, event->xbutton.button==Button2,
3476 unhideHere);
3478 if (event->xbutton.state & MOD_MASK) {
3479 wHideOtherApplications(btn->icon->owner);
3481 } else {
3482 if (event->xbutton.button==Button1) {
3484 if (event->xbutton.state & MOD_MASK) {
3485 /* raise/lower dock */
3486 toggleLowered(dock);
3487 } else if (btn == dock->screen_ptr->clip_icon) {
3488 if (getClipButton(event->xbutton.x, event->xbutton.y)==CLIP_IDLE)
3489 toggleCollapsed(dock);
3490 else
3491 handleClipChangeWorkspace(dock->screen_ptr, event);
3492 } else if (btn->command) {
3493 if (!btn->launching &&
3494 (!btn->running || (event->xbutton.state & ControlMask))) {
3495 launchDockedApplication(btn);
3497 } else if (btn->xindex == 0 && btn->yindex == 0
3498 && btn->dock->type == WM_DOCK) {
3500 wShowGNUstepPanel(dock->screen_ptr);
3507 static void
3508 handleDockMove(WDock *dock, WAppIcon *aicon, XEvent *event)
3510 WScreen *scr = dock->screen_ptr;
3511 int ofs_x=event->xbutton.x, ofs_y=event->xbutton.y;
3512 int x, y;
3513 XEvent ev;
3514 int grabbed = 0, swapped = 0, done;
3515 Pixmap ghost = None;
3516 int superfluous = wPreferences.superfluous; /* we catch it to avoid problems */
3518 #ifdef DEBUG
3519 puts("moving dock");
3520 #endif
3521 if (XGrabPointer(dpy, aicon->icon->core->window, True, ButtonMotionMask
3522 |ButtonReleaseMask|ButtonPressMask, GrabModeAsync,
3523 GrabModeAsync, None, None, CurrentTime) !=GrabSuccess) {
3524 wwarning("pointer grab failed for dock move");
3526 y = 0;
3527 for (x=0; x<dock->max_icons; x++) {
3528 if (dock->icon_array[x]!=NULL &&
3529 dock->icon_array[x]->yindex > y)
3530 y = dock->icon_array[x]->yindex;
3532 y++;
3533 XResizeWindow(dpy, scr->dock_shadow, ICON_SIZE, ICON_SIZE*y);
3535 done = 0;
3536 while (!done) {
3537 WMMaskEvent(dpy, PointerMotionMask|ButtonReleaseMask|ButtonPressMask
3538 |ButtonMotionMask|ExposureMask, &ev);
3539 switch (ev.type) {
3540 case Expose:
3541 WMHandleEvent(&ev);
3542 break;
3544 case MotionNotify:
3545 if (!grabbed) {
3546 if (abs(ofs_x-ev.xmotion.x)>=MOVE_THRESHOLD
3547 || abs(ofs_y-ev.xmotion.y)>=MOVE_THRESHOLD) {
3548 XChangeActivePointerGrab(dpy, ButtonMotionMask
3549 |ButtonReleaseMask|ButtonPressMask,
3550 wCursor[WCUR_MOVE], CurrentTime);
3551 grabbed=1;
3553 break;
3555 if (dock->type == WM_CLIP) {
3556 if (ev.xmotion.x_root - ofs_x < 0) {
3557 x = 0;
3558 } else if (ev.xmotion.x_root - ofs_x + ICON_SIZE >
3559 scr->scr_width) {
3560 x = scr->scr_width - ICON_SIZE;
3561 } else {
3562 x = ev.xmotion.x_root - ofs_x;
3564 if (ev.xmotion.y_root - ofs_y < 0) {
3565 y = 0;
3566 } else if (ev.xmotion.y_root - ofs_y + ICON_SIZE >
3567 scr->scr_height) {
3568 y = scr->scr_height - ICON_SIZE;
3569 } else {
3570 y = ev.xmotion.y_root - ofs_y;
3572 moveDock(dock, x, y);
3573 } else {
3574 /* move vertically if pointer is inside the dock*/
3575 if ((dock->on_right_side &&
3576 ev.xmotion.x_root >= dock->x_pos - ICON_SIZE)
3577 || (!dock->on_right_side &&
3578 ev.xmotion.x_root <= dock->x_pos + ICON_SIZE*2)) {
3580 if (ev.xmotion.y_root - ofs_y < 0) {
3581 y = 0;
3582 } else if (ev.xmotion.y_root - ofs_y + ICON_SIZE >
3583 scr->scr_height) {
3584 y = scr->scr_height - ICON_SIZE;
3585 } else {
3586 y = ev.xmotion.y_root - ofs_y;
3588 moveDock(dock, dock->x_pos, y);
3590 /* move horizontally to change sides */
3591 x = ev.xmotion.x_root - ofs_x;
3592 if (!dock->on_right_side) {
3594 /* is on left */
3596 if (ev.xmotion.x_root > dock->x_pos + ICON_SIZE*2) {
3597 XMoveWindow(dpy, scr->dock_shadow, scr->scr_width-ICON_SIZE
3598 -DOCK_EXTRA_SPACE-1, dock->y_pos);
3599 if (superfluous && ghost==None) {
3600 ghost = MakeGhostDock(dock, dock->x_pos,
3601 scr->scr_width-ICON_SIZE
3602 -DOCK_EXTRA_SPACE-1,
3603 dock->y_pos);
3604 XSetWindowBackgroundPixmap(dpy, scr->dock_shadow,
3605 ghost);
3606 XClearWindow(dpy, scr->dock_shadow);
3608 XMapRaised(dpy, scr->dock_shadow);
3609 swapped = 1;
3610 } else {
3611 if (superfluous && ghost!=None) {
3612 XFreePixmap(dpy, ghost);
3613 ghost = None;
3615 XUnmapWindow(dpy, scr->dock_shadow);
3616 swapped = 0;
3618 } else {
3619 /* is on right */
3620 if (ev.xmotion.x_root < dock->x_pos - ICON_SIZE) {
3621 XMoveWindow(dpy, scr->dock_shadow,
3622 DOCK_EXTRA_SPACE, dock->y_pos);
3623 if (superfluous && ghost==None) {
3624 ghost = MakeGhostDock(dock, dock->x_pos,
3625 DOCK_EXTRA_SPACE, dock->y_pos);
3626 XSetWindowBackgroundPixmap(dpy, scr->dock_shadow,
3627 ghost);
3628 XClearWindow(dpy, scr->dock_shadow);
3630 XMapRaised(dpy, scr->dock_shadow);
3631 swapped = -1;
3632 } else {
3633 XUnmapWindow(dpy, scr->dock_shadow);
3634 swapped = 0;
3635 if (superfluous && ghost!=None) {
3636 XFreePixmap(dpy, ghost);
3637 ghost = None;
3642 break;
3644 case ButtonPress:
3645 break;
3647 case ButtonRelease:
3648 if (ev.xbutton.button != event->xbutton.button)
3649 break;
3650 XUngrabPointer(dpy, CurrentTime);
3651 XUnmapWindow(dpy, scr->dock_shadow);
3652 XResizeWindow(dpy, scr->dock_shadow, ICON_SIZE, ICON_SIZE);
3653 if (dock->type == WM_DOCK) {
3654 if (swapped!=0) {
3655 if (swapped>0)
3656 dock->on_right_side = 1;
3657 else
3658 dock->on_right_side = 0;
3659 swapDock(dock);
3660 wArrangeIcons(scr, False);
3663 done = 1;
3664 break;
3667 if (superfluous) {
3668 if (ghost!=None)
3669 XFreePixmap(dpy, ghost);
3670 XSetWindowBackground(dpy, scr->dock_shadow, scr->white_pixel);
3672 #ifdef DEBUG
3673 puts("End dock move");
3674 #endif
3679 static void
3680 handleIconMove(WDock *dock, WAppIcon *aicon, XEvent *event)
3682 WScreen *scr = dock->screen_ptr;
3683 Window wins[2];
3684 WIcon *icon = aicon->icon;
3685 WDock *dock2 = NULL, *last_dock = dock, *clip = NULL;
3686 int ondock, grabbed = 0, change_dock = 0, collapsed = 0;
3687 XEvent ev;
3688 int x = aicon->x_pos, y = aicon->y_pos;
3689 int ofs_x = event->xbutton.x, ofs_y = event->xbutton.y;
3690 int shad_x = x, shad_y = y;
3691 int ix = aicon->xindex, iy = aicon->yindex;
3692 int tmp;
3693 Pixmap ghost = None;
3694 Bool docked;
3695 int superfluous = wPreferences.superfluous; /* we catch it to avoid problems */
3696 int omnipresent = aicon->omnipresent; /* this must be cached!!! */
3699 if (wPreferences.flags.noupdates)
3700 return;
3702 if (XGrabPointer(dpy, icon->core->window, True, ButtonMotionMask
3703 |ButtonReleaseMask|ButtonPressMask, GrabModeAsync,
3704 GrabModeAsync, None, None, CurrentTime) !=GrabSuccess) {
3705 #ifdef DEBUG0
3706 wwarning("pointer grab failed for icon move");
3707 #endif
3710 if (!(event->xbutton.state & MOD_MASK))
3711 wRaiseFrame(icon->core);
3713 if (!wPreferences.flags.noclip)
3714 clip = scr->workspaces[scr->current_workspace]->clip;
3716 if (dock == scr->dock && !wPreferences.flags.noclip)
3717 dock2 = clip;
3718 else if (dock != scr->dock && !wPreferences.flags.nodock)
3719 dock2 = scr->dock;
3721 wins[0] = icon->core->window;
3722 wins[1] = scr->dock_shadow;
3723 XRestackWindows(dpy, wins, 2);
3724 XMoveResizeWindow(dpy, scr->dock_shadow, aicon->x_pos, aicon->y_pos,
3725 ICON_SIZE, ICON_SIZE);
3726 if (superfluous) {
3727 if (icon->pixmap!=None)
3728 ghost = MakeGhostIcon(scr, icon->pixmap);
3729 else
3730 ghost = MakeGhostIcon(scr, icon->core->window);
3732 XSetWindowBackgroundPixmap(dpy, scr->dock_shadow, ghost);
3733 XClearWindow(dpy, scr->dock_shadow);
3735 XMapWindow(dpy, scr->dock_shadow);
3737 ondock = 1;
3740 while(1) {
3741 XMaskEvent(dpy, PointerMotionMask|ButtonReleaseMask|ButtonPressMask
3742 |ButtonMotionMask|ExposureMask, &ev);
3743 switch (ev.type) {
3744 case Expose:
3745 WMHandleEvent(&ev);
3746 break;
3748 case MotionNotify:
3749 if (!grabbed) {
3750 if (abs(ofs_x-ev.xmotion.x)>=MOVE_THRESHOLD
3751 || abs(ofs_y-ev.xmotion.y)>=MOVE_THRESHOLD) {
3752 XChangeActivePointerGrab(dpy, ButtonMotionMask
3753 |ButtonReleaseMask|ButtonPressMask,
3754 wCursor[WCUR_MOVE], CurrentTime);
3755 grabbed=1;
3756 } else {
3757 break;
3761 if (omnipresent) {
3762 int i;
3763 for (i=0; i<scr->workspace_count; i++) {
3764 if (i == scr->current_workspace)
3765 continue;
3766 wDockShowIcons(scr->workspaces[i]->clip);
3770 x = ev.xmotion.x_root - ofs_x;
3771 y = ev.xmotion.y_root - ofs_y;
3772 tmp = wDockSnapIcon(dock, aicon, x, y, &ix, &iy, True);
3773 if (tmp && dock2) {
3774 change_dock = 0;
3775 if (last_dock != dock && collapsed) {
3776 last_dock->collapsed = 1;
3777 wDockHideIcons(last_dock);
3778 collapsed = 0;
3780 if (!collapsed && (collapsed = dock->collapsed)) {
3781 dock->collapsed = 0;
3782 wDockShowIcons(dock);
3784 if (dock->auto_raise_lower)
3785 wDockRaise(dock);
3786 last_dock = dock;
3787 } else if (dock2) {
3788 tmp = wDockSnapIcon(dock2, aicon, x, y, &ix, &iy, False);
3789 if (tmp) {
3790 change_dock = 1;
3791 if (last_dock != dock2 && collapsed) {
3792 last_dock->collapsed = 1;
3793 wDockHideIcons(last_dock);
3794 collapsed = 0;
3796 if (!collapsed && (collapsed = dock2->collapsed)) {
3797 dock2->collapsed = 0;
3798 wDockShowIcons(dock2);
3800 if (dock2->auto_raise_lower)
3801 wDockRaise(dock2);
3802 last_dock = dock2;
3805 if (aicon->launching
3806 || (aicon->running && !(ev.xmotion.state & MOD_MASK))
3807 || (!aicon->running && tmp)) {
3808 shad_x = last_dock->x_pos + ix*wPreferences.icon_size;
3809 shad_y = last_dock->y_pos + iy*wPreferences.icon_size;
3811 XMoveWindow(dpy, scr->dock_shadow, shad_x, shad_y);
3813 if (!ondock) {
3814 XMapWindow(dpy, scr->dock_shadow);
3816 ondock = 1;
3817 } else {
3818 if (ondock) {
3819 XUnmapWindow(dpy, scr->dock_shadow);
3821 ondock = 0;
3823 XMoveWindow(dpy, icon->core->window, x, y);
3824 break;
3826 case ButtonPress:
3827 break;
3829 case ButtonRelease:
3830 if (ev.xbutton.button != event->xbutton.button)
3831 break;
3832 XUngrabPointer(dpy, CurrentTime);
3833 if (ondock) {
3834 SlideWindow(icon->core->window, x, y, shad_x, shad_y);
3835 XUnmapWindow(dpy, scr->dock_shadow);
3836 if (!change_dock) {
3837 reattachIcon(dock, aicon, ix, iy);
3838 if (clip && dock!=clip && clip->auto_raise_lower)
3839 wDockLower(clip);
3840 } else {
3841 docked = moveIconBetweenDocks(dock, dock2, aicon, ix, iy);
3842 if (!docked) {
3843 /* Slide it back if dock rejected it */
3844 SlideWindow(icon->core->window, x, y, aicon->x_pos,
3845 aicon->y_pos);
3846 reattachIcon(dock, aicon, aicon->xindex,aicon->yindex);
3848 if (last_dock->type==WM_CLIP && last_dock->auto_collapse) {
3849 collapsed = 0;
3852 } else {
3853 aicon->x_pos = x;
3854 aicon->y_pos = y;
3855 if (superfluous) {
3856 if (!aicon->running && !wPreferences.no_animations) {
3857 /* We need to deselect it, even if is deselected in
3858 * wDockDetach(), because else DoKaboom() will fail.
3860 if (aicon->icon->selected)
3861 wIconSelect(aicon->icon);
3862 DoKaboom(scr,aicon->icon->core->window, x, y);
3865 if (clip && clip->auto_raise_lower)
3866 wDockLower(clip);
3867 wDockDetach(dock, aicon);
3869 if (collapsed) {
3870 last_dock->collapsed = 1;
3871 wDockHideIcons(last_dock);
3872 collapsed = 0;
3874 if (superfluous) {
3875 if (ghost!=None)
3876 XFreePixmap(dpy, ghost);
3877 XSetWindowBackground(dpy, scr->dock_shadow, scr->white_pixel);
3879 if (omnipresent) {
3880 int i;
3881 for (i=0; i<scr->workspace_count; i++) {
3882 if (i == scr->current_workspace)
3883 continue;
3884 wDockHideIcons(scr->workspaces[i]->clip);
3888 #ifdef DEBUG
3889 puts("End icon move");
3890 #endif
3891 return;
3897 static int
3898 getClipButton(int px, int py)
3900 int pt = (CLIP_BUTTON_SIZE+2)*ICON_SIZE/64;
3902 if (px < 0 || py < 0 || px >= ICON_SIZE || py >= ICON_SIZE)
3903 return CLIP_IDLE;
3905 if (py <= pt-((int)ICON_SIZE-1-px))
3906 return CLIP_FORWARD;
3907 else if (px <= pt-((int)ICON_SIZE-1-py))
3908 return CLIP_REWIND;
3910 return CLIP_IDLE;
3914 static void
3915 handleClipChangeWorkspace(WScreen *scr, XEvent *event)
3917 XEvent ev;
3918 int done, direction, new_ws;
3919 int new_dir;
3920 WDock *clip = scr->clip_icon->dock;
3922 direction = getClipButton(event->xbutton.x, event->xbutton.y);
3924 clip->lclip_button_pushed = direction==CLIP_REWIND;
3925 clip->rclip_button_pushed = direction==CLIP_FORWARD;
3927 wClipIconPaint(scr->clip_icon);
3928 done = 0;
3929 while(!done) {
3930 WMMaskEvent(dpy, ExposureMask|ButtonMotionMask|ButtonReleaseMask
3931 |ButtonPressMask, &ev);
3932 switch (ev.type) {
3933 case Expose:
3934 WMHandleEvent(&ev);
3935 break;
3937 case MotionNotify:
3938 new_dir = getClipButton(ev.xmotion.x, ev.xmotion.y);
3939 if (new_dir != direction) {
3940 direction = new_dir;
3941 clip->lclip_button_pushed = direction==CLIP_REWIND;
3942 clip->rclip_button_pushed = direction==CLIP_FORWARD;
3943 wClipIconPaint(scr->clip_icon);
3945 break;
3947 case ButtonPress:
3948 break;
3950 case ButtonRelease:
3951 if (ev.xbutton.button == event->xbutton.button)
3952 done = 1;
3956 clip->lclip_button_pushed = 0;
3957 clip->rclip_button_pushed = 0;
3959 new_ws = wPreferences.ws_advance || (event->xbutton.state & ControlMask);
3961 if (direction == CLIP_FORWARD) {
3962 if (scr->current_workspace < scr->workspace_count-1)
3963 wWorkspaceChange(scr, scr->current_workspace+1);
3964 else if (new_ws && scr->current_workspace < MAX_WORKSPACES-1)
3965 wWorkspaceChange(scr, scr->current_workspace+1);
3966 else if (wPreferences.ws_cycle)
3967 wWorkspaceChange(scr, 0);
3969 else if (direction == CLIP_REWIND) {
3970 if (scr->current_workspace > 0)
3971 wWorkspaceChange(scr, scr->current_workspace-1);
3972 else if (scr->current_workspace==0 && wPreferences.ws_cycle)
3973 wWorkspaceChange(scr, scr->workspace_count-1);
3976 wClipIconPaint(scr->clip_icon);
3980 static void
3981 iconMouseDown(WObjDescriptor *desc, XEvent *event)
3983 WAppIcon *aicon = desc->parent;
3984 WDock *dock = aicon->dock;
3985 WScreen *scr = aicon->icon->core->screen_ptr;
3987 if (aicon->editing || WCHECK_STATE(WSTATE_MODAL))
3988 return;
3990 scr->last_dock = dock;
3992 if (dock->menu->flags.mapped)
3993 wMenuUnmap(dock->menu);
3995 if (IsDoubleClick(scr, event)) {
3996 /* double-click was not in the main clip icon */
3997 if (dock->type != WM_CLIP || aicon->xindex!=0 || aicon->yindex!=0
3998 || getClipButton(event->xbutton.x, event->xbutton.y)==CLIP_IDLE) {
3999 iconDblClick(desc, event);
4000 return;
4004 if (dock->type == WM_CLIP && scr->flags.clip_balloon_mapped) {
4005 XUnmapWindow(dpy, scr->clip_balloon);
4006 scr->flags.clip_balloon_mapped = 0;
4009 #ifdef DEBUG
4010 puts("handling dock");
4011 #endif
4012 if (event->xbutton.button == Button1) {
4013 if (event->xbutton.state & MOD_MASK)
4014 wDockLower(dock);
4015 else
4016 wDockRaise(dock);
4018 if ((event->xbutton.state & ShiftMask) && aicon!=scr->clip_icon &&
4019 dock->type!=WM_DOCK) {
4020 wIconSelect(aicon->icon);
4021 return;
4024 if (aicon->yindex==0 && aicon->xindex==0) {
4025 if (getClipButton(event->xbutton.x, event->xbutton.y)!=CLIP_IDLE
4026 && dock->type==WM_CLIP)
4027 handleClipChangeWorkspace(scr, event);
4028 else
4029 handleDockMove(dock, aicon, event);
4030 } else
4031 handleIconMove(dock, aicon, event);
4033 } else if (event->xbutton.button==Button2 && dock->type==WM_CLIP &&
4034 aicon==scr->clip_icon) {
4035 openClipWorkspaceMenu(scr, event->xbutton.x_root+2,
4036 event->xbutton.y_root+2);
4037 if (scr->clip_ws_menu) {
4038 WMenu *menu;
4039 menu = scr->clip_ws_menu;
4040 desc = &menu->menu->descriptor;
4042 event->xany.send_event = True;
4043 (*desc->handle_mousedown)(desc, event);
4045 #if 1
4046 } else if (event->xbutton.button==Button2 && dock->type==WM_CLIP &&
4047 (event->xbutton.state & ShiftMask) && aicon!=scr->clip_icon) {
4048 wClipMakeIconOmnipresent(aicon, !aicon->omnipresent);
4049 #endif
4050 } else if (event->xbutton.button == Button3) {
4051 openDockMenu(dock, aicon, event);
4056 static void
4057 showClipBalloon(WDock *dock, int workspace)
4059 int w, h;
4060 int x, y;
4061 WScreen *scr = dock->screen_ptr;
4062 char *text;
4063 Window stack[2];
4065 scr->flags.clip_balloon_mapped = 1;
4066 XMapWindow(dpy, scr->clip_balloon);
4068 text = scr->workspaces[workspace]->name;
4070 w = WMWidthOfString(scr->clip_title_font, text, strlen(text));
4072 h = WMFontHeight(scr->clip_title_font);
4073 XResizeWindow(dpy, scr->clip_balloon, w, h);
4075 x = dock->x_pos + CLIP_BUTTON_SIZE*ICON_SIZE/64;
4076 y = dock->y_pos + ICON_SIZE - WMFontHeight(scr->clip_title_font) - 3;
4078 if (x+w > scr->scr_width) {
4079 x = scr->scr_width - w;
4080 if (dock->y_pos + ICON_SIZE + h > scr->scr_height)
4081 y = dock->y_pos - h - 1;
4082 else
4083 y = dock->y_pos + ICON_SIZE;
4084 XRaiseWindow(dpy, scr->clip_balloon);
4085 } else {
4086 stack[0] = scr->clip_icon->icon->core->window;
4087 stack[1] = scr->clip_balloon;
4088 XRestackWindows(dpy, stack, 2);
4090 XMoveWindow(dpy, scr->clip_balloon, x, y);
4091 XSetForeground(dpy, scr->clip_title_gc,
4092 scr->clip_title_pixel[CLIP_NORMAL]);
4093 XClearWindow(dpy, scr->clip_balloon);
4094 WMDrawString(scr->wmscreen, scr->clip_balloon, scr->clip_title_gc,
4095 scr->clip_title_font, 0, 0, text, strlen(text));
4099 static void
4100 clipEnterNotify(WObjDescriptor *desc, XEvent *event)
4102 WAppIcon *btn = (WAppIcon*)desc->parent;
4103 WDock *dock;
4104 WScreen *scr;
4106 assert(event->type==EnterNotify);
4108 if(desc->parent_type!=WCLASS_DOCK_ICON)
4109 return;
4111 scr = btn->icon->core->screen_ptr;
4112 if (!btn->omnipresent)
4113 dock = btn->dock;
4114 else
4115 dock = scr->workspaces[scr->current_workspace]->clip;
4117 if (!dock || dock->type!=WM_CLIP)
4118 return;
4120 /* The auto raise/lower code */
4121 if (dock->auto_lower_magic) {
4122 WMDeleteTimerHandler(dock->auto_lower_magic);
4123 dock->auto_lower_magic = NULL;
4125 if (dock->auto_raise_lower && !dock->auto_raise_magic) {
4126 dock->auto_raise_magic = WMAddTimerHandler(AUTO_RAISE_DELAY,
4127 clipAutoRaise,
4128 (void *)dock);
4131 /* The auto expand/collapse code */
4132 if (dock->auto_collapse_magic) {
4133 WMDeleteTimerHandler(dock->auto_collapse_magic);
4134 dock->auto_collapse_magic = NULL;
4136 if (dock->auto_collapse && !dock->auto_expand_magic) {
4137 dock->auto_expand_magic = WMAddTimerHandler(AUTO_EXPAND_DELAY,
4138 clipAutoExpand,
4139 (void *)dock);
4142 if (btn->xindex == 0 && btn->yindex == 0)
4143 showClipBalloon(dock, dock->screen_ptr->current_workspace);
4144 else {
4145 if (dock->screen_ptr->flags.clip_balloon_mapped) {
4146 XUnmapWindow(dpy, dock->screen_ptr->clip_balloon);
4147 dock->screen_ptr->flags.clip_balloon_mapped = 0;
4153 static void
4154 clipLeave(WDock *dock)
4156 XEvent event;
4157 WObjDescriptor *desc = NULL;
4159 if (!dock || dock->type!=WM_CLIP)
4160 return;
4162 if (XCheckTypedEvent(dpy, EnterNotify, &event)!=False) {
4163 if (XFindContext(dpy, event.xcrossing.window, wWinContext,
4164 (XPointer *)&desc)!=XCNOENT
4165 && desc && desc->parent_type==WCLASS_DOCK_ICON
4166 && ((WAppIcon*)desc->parent)->dock
4167 && ((WAppIcon*)desc->parent)->dock->type==WM_CLIP) {
4168 /* We didn't left the Clip yet */
4169 XPutBackEvent(dpy, &event);
4170 return;
4173 XPutBackEvent(dpy, &event);
4174 } else {
4175 /* We entered a withdrawn window, so we're still in Clip */
4176 return;
4179 if (dock->auto_raise_magic) {
4180 WMDeleteTimerHandler(dock->auto_raise_magic);
4181 dock->auto_raise_magic = NULL;
4183 if (dock->auto_raise_lower && !dock->auto_lower_magic) {
4184 dock->auto_lower_magic = WMAddTimerHandler(AUTO_LOWER_DELAY,
4185 clipAutoLower,
4186 (void *)dock);
4189 if (dock->auto_expand_magic) {
4190 WMDeleteTimerHandler(dock->auto_expand_magic);
4191 dock->auto_expand_magic = NULL;
4193 if (dock->auto_collapse && !dock->auto_collapse_magic) {
4194 dock->auto_collapse_magic = WMAddTimerHandler(AUTO_COLLAPSE_DELAY,
4195 clipAutoCollapse,
4196 (void *)dock);
4201 static void
4202 clipLeaveNotify(WObjDescriptor *desc, XEvent *event)
4204 WAppIcon *btn = (WAppIcon*)desc->parent;
4206 assert(event->type==LeaveNotify);
4208 if(desc->parent_type!=WCLASS_DOCK_ICON)
4209 return;
4211 clipLeave(btn->dock);
4215 static void
4216 clipAutoCollapse(void *cdata)
4218 WDock *dock = (WDock *)cdata;
4220 if (dock->type!=WM_CLIP)
4221 return;
4223 if (dock->auto_collapse) {
4224 dock->collapsed = 1;
4225 wDockHideIcons(dock);
4227 dock->auto_collapse_magic = NULL;
4231 static void
4232 clipAutoExpand(void *cdata)
4234 WDock *dock = (WDock *)cdata;
4236 if (dock->type!=WM_CLIP)
4237 return;
4239 if (dock->auto_collapse) {
4240 dock->collapsed = 0;
4241 wDockShowIcons(dock);
4243 dock->auto_expand_magic = NULL;
4247 static void
4248 clipAutoLower(void *cdata)
4250 WDock *dock = (WDock *)cdata;
4252 if (dock->type!=WM_CLIP)
4253 return;
4255 if (dock->auto_raise_lower)
4256 wDockLower(dock);
4258 dock->auto_lower_magic = NULL;
4262 static void
4263 clipAutoRaise(void *cdata)
4265 WDock *dock = (WDock *)cdata;
4267 if (dock->type!=WM_CLIP)
4268 return;
4270 if (dock->auto_raise_lower)
4271 wDockRaise(dock);
4273 if (dock->screen_ptr->flags.clip_balloon_mapped) {
4274 showClipBalloon(dock, dock->screen_ptr->current_workspace);
4277 dock->auto_raise_magic = NULL;
4281 static Bool
4282 iconCanBeOmnipresent(WAppIcon *aicon)
4284 WScreen *scr = aicon->icon->core->screen_ptr;
4285 WDock *clip;
4286 WAppIcon *btn;
4287 int i, j;
4289 for (i=0; i<scr->workspace_count; i++) {
4290 clip = scr->workspaces[i]->clip;
4292 if (clip == aicon->dock)
4293 continue;
4295 if (clip->icon_count + scr->global_icon_count >= clip->max_icons)
4296 return False; /* Clip is full in some workspace */
4298 for (j=0; j<clip->max_icons; j++) {
4299 btn = clip->icon_array[j];
4300 if(btn && btn->xindex==aicon->xindex && btn->yindex==aicon->yindex)
4301 return False;
4305 return True;
4310 wClipMakeIconOmnipresent(WAppIcon *aicon, int omnipresent)
4312 WScreen *scr = aicon->icon->core->screen_ptr;
4313 WAppIconChain *new_entry, *tmp, *tmp1;
4314 int status = WO_SUCCESS;
4316 if (aicon->dock == scr->dock || aicon == scr->clip_icon)
4317 return WO_NOT_APPLICABLE;
4319 if (aicon->omnipresent == omnipresent)
4320 return WO_SUCCESS;
4322 if (omnipresent) {
4323 if (iconCanBeOmnipresent(aicon)) {
4324 aicon->omnipresent = 1;
4325 new_entry = wmalloc(sizeof(WAppIconChain));
4326 new_entry->aicon = aicon;
4327 new_entry->next = NULL;
4328 if (!scr->global_icons) {
4329 scr->global_icons = new_entry;
4330 } else {
4331 tmp = scr->global_icons;
4332 while (tmp->next)
4333 tmp = tmp->next;
4335 tmp->next = new_entry;
4337 scr->global_icon_count++;
4338 } else {
4339 aicon->omnipresent = 0;
4340 status = WO_FAILED;
4342 } else {
4343 aicon->omnipresent = 0;
4344 if (aicon == scr->global_icons->aicon) {
4345 tmp = scr->global_icons->next;
4346 free(scr->global_icons);
4347 scr->global_icons = tmp;
4348 scr->global_icon_count--;
4349 } else {
4350 tmp = scr->global_icons;
4351 while (tmp->next) {
4352 if (tmp->next->aicon == aicon) {
4353 tmp1 = tmp->next->next;
4354 free(tmp->next);
4355 tmp->next = tmp1;
4356 scr->global_icon_count--;
4357 break;
4359 tmp = tmp->next;
4364 return status;