This update includes the 0.20.3pre3 code
[wmaker-crm.git] / src / dock.c
blobfde4e2aea3341224b8616321d958f559a357fae6
1 /* dock.c- built-in Dock module for WindowMaker
2 *
3 * Window Maker window manager
4 *
5 * Copyright (c) 1997, 1998 Alfredo K. Kojima
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
20 * USA.
24 #include "wconfig.h"
26 #include <X11/Xlib.h>
27 #include <X11/Xutil.h>
28 #include <stdlib.h>
29 #include <string.h>
30 #include <unistd.h>
31 #include <math.h>
32 #include <limits.h>
34 #ifndef PATH_MAX
35 #define PATH_MAX DEFAULT_PATH_MAX
36 #endif
38 #include "WindowMaker.h"
39 #include "wcore.h"
40 #include "window.h"
41 #include "icon.h"
42 #include "appicon.h"
43 #include "actions.h"
44 #include "stacking.h"
45 #include "dock.h"
46 #include "dialog.h"
47 #include "funcs.h"
48 #include "properties.h"
49 #include "menu.h"
50 #include "client.h"
51 #include "defaults.h"
52 #include "workspace.h"
53 #include "framewin.h"
54 #include "superfluous.h"
56 #include "list.h"
58 #ifdef WMSOUND
59 #include "wmsound.h"
60 #endif
62 #include <proplist.h>
67 /**** Local variables ****/
68 #define CLIP_REWIND 1
69 #define CLIP_IDLE 0
70 #define CLIP_FORWARD 2
72 #define CLIP_BUTTON_SIZE 23
75 /**** Global variables ****/
77 /* in dockedapp.c */
78 extern void DestroyDockAppSettingsPanel();
80 extern void ShowDockAppSettingsPanel(WAppIcon *aicon);
84 extern Cursor wCursor[WCUR_LAST];
86 extern WPreferences wPreferences;
88 extern XContext wWinContext;
90 #ifdef OFFIX_DND
91 extern Atom _XA_DND_PROTOCOL;
92 #endif
95 #define MOD_MASK wPreferences.modifier_mask
97 extern void appIconMouseDown(WObjDescriptor *desc, XEvent *event);
99 #define ICON_SIZE wPreferences.icon_size
102 /***** Local variables ****/
104 static proplist_t dCommand=NULL;
105 #ifdef OFFIX_DND
106 static proplist_t dDropCommand=NULL;
107 #endif
108 static proplist_t dAutoLaunch, dName, dForced, dBuggyApplication, dYes, dNo;
109 static proplist_t dHost, dDock, dClip;
110 static proplist_t dAutoAttractIcons, dKeepAttracted;
112 static proplist_t dPosition, dApplications, dLowered, dCollapsed, dAutoCollapse;
114 static void dockIconPaint(WAppIcon *btn);
116 static void iconMouseDown(WObjDescriptor *desc, XEvent *event);
118 static pid_t execCommand(WAppIcon *btn, char *command, WSavedState *state);
120 static void trackDeadProcess(pid_t pid, unsigned char status, WDock *dock);
122 static int getClipButton(int px, int py);
124 static void toggleLowered(WDock *dock);
126 static void toggleCollapsed(WDock *dock);
128 static void toggleAutoCollapse(WDock *dock);
130 static void clipIconExpose(WObjDescriptor *desc, XEvent *event);
132 static void clipLeave(WDock *dock);
134 static void handleClipChangeWorkspace(WScreen *scr, XEvent *event);
136 Bool moveIconBetweenDocks(WDock *src, WDock *dest, WAppIcon *icon, int x, int y);
138 static void clipEnterNotify(WObjDescriptor *desc, XEvent *event);
139 static void clipLeaveNotify(WObjDescriptor *desc, XEvent *event);
140 static void clipAutoCollapse(void *cdata);
141 static void launchDockedApplication(WAppIcon *btn);
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 dAutoAttractIcons = PLMakeString("AutoAttractIcons");
187 dKeepAttracted = PLMakeString("KeepAttracted");
189 dDock = PLMakeString("Dock");
190 dClip = PLMakeString("Clip");
195 static void
196 renameCallback(WMenu *menu, WMenuEntry *entry)
198 WDock *dock = entry->clientdata;
199 char buffer[128];
200 int wspace;
201 char *name;
203 assert(entry->clientdata!=NULL);
205 wspace = dock->screen_ptr->current_workspace;
207 name = wstrdup(dock->screen_ptr->workspaces[wspace]->name);
209 sprintf(buffer, _("Type the name for workspace %i:"), wspace+1);
210 if (wInputDialog(dock->screen_ptr, _("Rename Workspace"), buffer,
211 &name)) {
212 wWorkspaceRename(dock->screen_ptr, wspace, name);
214 if (name) {
215 free(name);
220 static void
221 toggleLoweredCallback(WMenu *menu, WMenuEntry *entry)
223 assert(entry->clientdata!=NULL);
225 toggleLowered(entry->clientdata);
227 entry->flags.indicator_on = !((WDock*)entry->clientdata)->lowered;
229 wMenuPaint(menu);
234 static void
235 killCallback(WMenu *menu, WMenuEntry *entry)
237 WAppIcon *icon;
238 #ifdef REDUCE_APPICONS
239 WAppIconAppList *tapplist;
241 extern Atom _XA_WM_DELETE_WINDOW;
242 #endif
244 assert(entry->clientdata!=NULL);
246 icon = (WAppIcon*)entry->clientdata;
248 icon->editing = 1;
250 #ifdef REDUCE_APPICONS
251 /* Send a delete message to the main window of each application
252 * bound to this docked appicon. - cls
254 tapplist = icon->applist;
255 while (tapplist != NULL) {
256 if (tapplist->wapp->main_window_desc != NULL) {
257 if (tapplist->wapp->main_window_desc->protocols.DELETE_WINDOW) {
258 wClientSendProtocol(tapplist->wapp->main_window_desc,
259 _XA_WM_DELETE_WINDOW, CurrentTime);
260 } else {
261 wClientKill(tapplist->wapp->main_window_desc);
264 tapplist = tapplist->next;
266 #else
267 if (wPreferences.dont_confirm_kill
268 || wMessageDialog(menu->frame->screen_ptr, _("Kill Application"),
269 _("This will kill the application.\nAny unsaved changes will be lost.\nPlease confirm."),
270 _("Yes"), _("No"), NULL)==WAPRDefault) {
271 if (icon->icon && icon->icon->owner) {
272 wClientKill(icon->icon->owner);
275 #endif /* !REDUCE_APPICONS */
277 icon->editing = 0;
281 static LinkedList*
282 getSelected(WDock *dock)
284 LinkedList *ret=NULL;
285 WAppIcon *btn;
286 int i;
288 for (i=1; i<dock->max_icons; i++) {
289 btn = dock->icon_array[i];
290 if (btn && btn->icon->selected) {
291 ret = list_cons(btn, ret);
295 return ret;
299 static void
300 paintClipButtons(WAppIcon *clipIcon, Bool lpushed, Bool rpushed)
302 Window win = clipIcon->icon->core->window;
303 WScreen *scr = clipIcon->icon->core->screen_ptr;
304 XPoint p[4];
305 int pt = CLIP_BUTTON_SIZE*ICON_SIZE/64;
306 int tp = ICON_SIZE - pt;
307 int as = pt - 15; /* 15 = 5+5+5 */
310 if (rpushed) {
311 p[0].x = tp+1;
312 p[0].y = 1;
313 p[1].x = ICON_SIZE-2;
314 p[1].y = 1;
315 p[2].x = ICON_SIZE-2;
316 p[2].y = pt-1;
317 } else if (lpushed) {
318 p[0].x = 1;
319 p[0].y = tp;
320 p[1].x = pt;
321 p[1].y = ICON_SIZE-2;
322 p[2].x = 1;
323 p[2].y = ICON_SIZE-2;
325 if (lpushed || rpushed) {
326 XSetForeground(dpy, scr->draw_gc, scr->white_pixel);
327 XFillPolygon(dpy, win, scr->draw_gc, p, 3, Convex, CoordModeOrigin);
328 XSetForeground(dpy, scr->draw_gc, scr->black_pixel);
331 p[0].x = p[3].x = ICON_SIZE-6-as;
332 p[0].y = p[3].y = 5;
333 p[1].x = ICON_SIZE-6;
334 p[1].y = 5;
335 p[2].x = ICON_SIZE-6;
336 p[2].y = 5+as;
337 if (rpushed) {
338 XFillPolygon(dpy, win, scr->draw_gc, p, 3, Convex, CoordModeOrigin);
339 XDrawLines(dpy, win, scr->draw_gc, p, 4, CoordModeOrigin);
340 } else {
341 XFillPolygon(dpy, win, scr->clip_title_gc, p,3,Convex,CoordModeOrigin);
342 XDrawLines(dpy, win, scr->clip_title_gc, p,4,CoordModeOrigin);
345 p[0].x = p[3].x = 5;
346 p[0].y = p[3].y = ICON_SIZE-6-as;
347 p[1].x = 5;
348 p[1].y = ICON_SIZE-6;
349 p[2].x = 5+as;
350 p[2].y = ICON_SIZE-6;
351 if (lpushed) {
352 XFillPolygon(dpy, win, scr->draw_gc, p, 3, Convex, CoordModeOrigin);
353 XDrawLines(dpy, win, scr->draw_gc, p, 4, CoordModeOrigin);
354 } else {
355 XFillPolygon(dpy, win, scr->clip_title_gc, p,3,Convex,CoordModeOrigin);
356 XDrawLines(dpy, win, scr->clip_title_gc, p,4,CoordModeOrigin);
361 RImage*
362 wClipMakeTile(WScreen *scr, RImage *normalTile)
364 RImage *tile = RCloneImage(normalTile);
365 RColor black;
366 RColor dark;
367 RColor light;
368 int pt, tp;
370 pt = CLIP_BUTTON_SIZE*wPreferences.icon_size/64;
371 tp = wPreferences.icon_size-1 - pt;
373 black.alpha = 255;
374 black.red = black.green = black.blue = 0;
376 dark.alpha = 0;
377 dark.red = dark.green = dark.blue = 80;
379 light.alpha = 0;
380 light.red = light.green = light.blue = 80;
383 /* top right */
384 ROperateLine(tile, RSubtractOperation, tp, 0, wPreferences.icon_size-2,
385 pt-1, &dark);
386 RDrawLine(tile, tp-1, 0, wPreferences.icon_size-1, pt+1, &black);
387 ROperateLine(tile, RAddOperation, tp, 2, wPreferences.icon_size-3,
388 pt, &light);
391 /* bottom left */
392 ROperateLine(tile, RAddOperation, 2, tp+2, pt-2,
393 wPreferences.icon_size-3, &dark);
394 RDrawLine(tile, 0, tp-1, pt+1, wPreferences.icon_size-1, &black);
395 ROperateLine(tile, RSubtractOperation, 0, tp-2, pt+1,
396 wPreferences.icon_size-2, &light);
398 return tile;
402 static void
403 removeIconsCallback(WMenu *menu, WMenuEntry *entry)
405 WAppIcon *clickedIcon = (WAppIcon*)entry->clientdata;
406 WDock *dock;
407 WAppIcon *aicon;
408 LinkedList *selectedIcons;
409 int keepit;
411 assert(clickedIcon!=NULL);
413 dock = clickedIcon->dock;
415 selectedIcons = getSelected(dock);
417 if (selectedIcons) {
418 if (wMessageDialog(dock->screen_ptr, _("Workspace Clip"),
419 _("All selected icons will be removed!"),
420 _("OK"), _("Cancel"), NULL)!=WAPRDefault) {
421 return;
423 } else {
424 if (clickedIcon->xindex==0 && clickedIcon->yindex==0)
425 return;
426 selectedIcons = list_cons(clickedIcon, NULL);
429 while (selectedIcons) {
430 aicon = selectedIcons->head;
431 keepit = aicon->running && wApplicationOf(aicon->main_window);
432 wDockDetach(dock, aicon);
433 if (keepit) {
434 PlaceIcon(dock->screen_ptr, &aicon->x_pos, &aicon->y_pos);
435 XMoveWindow(dpy, aicon->icon->core->window,
436 aicon->x_pos, aicon->y_pos);
437 if (!dock->mapped || dock->collapsed)
438 XMapWindow(dpy, aicon->icon->core->window);
440 list_remove_head(&selectedIcons);
443 if (wPreferences.auto_arrange_icons)
444 wArrangeIcons(dock->screen_ptr, True);
448 static void
449 keepIconsCallback(WMenu *menu, WMenuEntry *entry)
451 WAppIcon *clickedIcon = (WAppIcon*)entry->clientdata;
452 WDock *dock;
453 WAppIcon *aicon;
454 LinkedList *selectedIcons;
456 assert(clickedIcon!=NULL);
457 dock = clickedIcon->dock;
459 selectedIcons = getSelected(dock);
461 if (!selectedIcons && clickedIcon!=dock->screen_ptr->clip_icon) {
462 char *command = NULL;
464 if (!clickedIcon->command && !clickedIcon->editing) {
465 clickedIcon->editing = 1;
466 if (wInputDialog(dock->screen_ptr, _("Keep Icon"),
467 _("Type the command used to launch the application"),
468 &command)) {
469 if (command && (command[0]==0 ||
470 (command[0]=='-' && command[1]==0))) {
471 free(command);
472 command = NULL;
474 clickedIcon->command = command;
475 clickedIcon->editing = 0;
476 } else {
477 clickedIcon->editing = 0;
478 if (command)
479 free(command);
480 return;
484 selectedIcons = list_cons(clickedIcon, NULL);
487 while (selectedIcons) {
488 aicon = selectedIcons->head;
489 if (aicon->icon->selected)
490 wIconSelect(aicon->icon);
491 if (aicon && aicon->attracted && aicon->command) {
492 aicon->attracted = 0;
493 if (aicon->icon->shadowed) {
494 aicon->icon->shadowed = 0;
495 aicon->icon->force_paint = 1;
496 wAppIconPaint(aicon);
499 list_remove_head(&selectedIcons);
506 static void
507 toggleAutoAttractCallback(WMenu *menu, WMenuEntry *entry)
509 WDock *dock = (WDock*)entry->clientdata;
511 assert(entry->clientdata!=NULL);
513 dock->attract_icons = !dock->attract_icons;
514 /*if (!dock->attract_icons)
515 dock->keep_attracted = 0;*/
517 entry->flags.indicator_on = dock->attract_icons;
519 wMenuPaint(menu);
523 static void
524 toggleKeepCallback(WMenu *menu, WMenuEntry *entry)
526 WDock *dock = (WDock*)entry->clientdata;
527 WAppIcon *btn;
528 int i;
530 assert(entry->clientdata!=NULL);
532 dock->keep_attracted = !dock->keep_attracted;
534 if (dock->keep_attracted) {
535 for (i=0; i< dock->max_icons; i++) {
536 btn = dock->icon_array[i];
537 if (btn && btn->attracted && btn->command) {
538 btn->attracted = 0;
539 if (btn->icon->shadowed) {
540 btn->icon->shadowed = 0;
541 btn->icon->force_paint = 1;
542 wAppIconPaint(btn);
548 entry->flags.indicator_on = dock->keep_attracted;
550 wMenuPaint(menu);
554 static void
555 selectCallback(WMenu *menu, WMenuEntry *entry)
557 WAppIcon *icon = (WAppIcon*)entry->clientdata;
559 assert(icon!=NULL);
561 wIconSelect(icon->icon);
563 wMenuPaint(menu);
567 static void
568 colectIconsCallback(WMenu *menu, WMenuEntry *entry)
570 WAppIcon *clickedIcon = (WAppIcon*)entry->clientdata;
571 WDock *clip;
572 WAppIcon *aicon;
573 int x, y, x_pos, y_pos;
575 assert(entry->clientdata!=NULL);
576 clip = clickedIcon->dock;
578 aicon = clip->screen_ptr->app_icon_list;
580 while (aicon) {
581 if (!aicon->docked && wDockFindFreeSlot(clip, &x, &y)) {
582 x_pos = clip->x_pos + x*ICON_SIZE;
583 y_pos = clip->y_pos + y*ICON_SIZE;
584 if (aicon->x_pos != x_pos || aicon->y_pos != y_pos) {
585 #ifdef ANIMATIONS
586 if (wPreferences.no_animations) {
587 XMoveWindow(dpy, aicon->icon->core->window, x_pos, y_pos);
588 } else {
589 SlideWindow(aicon->icon->core->window,
590 aicon->x_pos, aicon->y_pos, x_pos, y_pos);
592 #else
593 XMoveWindow(dpy, aicon->icon->core->window, x_pos, y_pos);
594 #endif /* ANIMATIONS */
596 aicon->attracted = 1;
597 if (!clip->keep_attracted && !aicon->icon->shadowed) {
598 aicon->icon->shadowed = 1;
599 aicon->icon->force_paint = 1;
600 /* We don't do an wAppIconPaint() here because it's in
601 * wDockAttachIcon(). -Dan
604 wDockAttachIcon(clip, aicon, x, y);
605 if (clip->collapsed || !clip->mapped)
606 XUnmapWindow(dpy, aicon->icon->core->window);
608 aicon = aicon->next;
613 static void
614 selectIconsCallback(WMenu *menu, WMenuEntry *entry)
616 WAppIcon *clickedIcon = (WAppIcon*)entry->clientdata;
617 WDock *dock;
618 LinkedList *selectedIcons;
619 WAppIcon *btn;
620 int i;
622 assert(clickedIcon!=NULL);
623 dock = clickedIcon->dock;
625 selectedIcons = getSelected(dock);
627 if (!selectedIcons) {
628 for (i=1; i<dock->max_icons; i++) {
629 btn = dock->icon_array[i];
630 if (btn && !btn->icon->selected) {
631 wIconSelect(btn->icon);
634 } else {
635 while(selectedIcons) {
636 btn = selectedIcons->head;
637 wIconSelect(btn->icon);
638 list_remove_head(&selectedIcons);
642 wMenuPaint(menu);
646 static void
647 toggleCollapsedCallback(WMenu *menu, WMenuEntry *entry)
649 assert(entry->clientdata!=NULL);
651 toggleCollapsed(entry->clientdata);
653 entry->flags.indicator_on = ((WDock*)entry->clientdata)->collapsed;
655 wMenuPaint(menu);
659 static void
660 toggleAutoCollapseCallback(WMenu *menu, WMenuEntry *entry)
662 assert(entry->clientdata!=NULL);
664 toggleAutoCollapse(entry->clientdata);
666 entry->flags.indicator_on = ((WDock*)entry->clientdata)->auto_collapse;
668 wMenuPaint(menu);
672 static void
673 launchCallback(WMenu *menu, WMenuEntry *entry)
675 WAppIcon *btn = (WAppIcon*)entry->clientdata;
677 launchDockedApplication(btn);
681 static void
682 settingsCallback(WMenu *menu, WMenuEntry *entry)
684 WAppIcon *btn = (WAppIcon*)entry->clientdata;
686 if (btn->editing)
687 return;
688 ShowDockAppSettingsPanel(btn);
692 static void
693 hideCallback(WMenu *menu, WMenuEntry *entry)
695 WApplication *wapp;
696 WAppIcon *btn = (WAppIcon*)entry->clientdata;
698 wapp = wApplicationOf(btn->icon->owner->main_window);
700 if (wapp->flags.hidden) {
701 wWorkspaceChange(btn->icon->core->screen_ptr,wapp->last_workspace);
702 wUnhideApplication(wapp, False, False);
703 } else {
704 wHideApplication(wapp);
709 static void
710 unhideHereCallback(WMenu *menu, WMenuEntry *entry)
712 WApplication *wapp;
713 WAppIcon *btn = (WAppIcon*)entry->clientdata;
715 wapp = wApplicationOf(btn->icon->owner->main_window);
717 wUnhideApplication(wapp, False, True);
721 WAppIcon*
722 mainIconCreate(WScreen *scr, int type)
724 WAppIcon *btn;
725 int x_pos;
727 if (type == WM_CLIP) {
728 if (scr->clip_icon)
729 return scr->clip_icon;
730 btn = wAppIconCreateForDock(scr, NULL, "Logo", "WMClip", TILE_CLIP);
731 btn->icon->core->descriptor.handle_expose = clipIconExpose;
732 btn->icon->core->descriptor.handle_enternotify = clipEnterNotify;
733 btn->icon->core->descriptor.handle_leavenotify = clipLeaveNotify;
734 /*x_pos = scr->scr_width - ICON_SIZE*2 - DOCK_EXTRA_SPACE;*/
735 x_pos = 0;
737 else {
738 btn = wAppIconCreateForDock(scr, NULL, "Logo", "WMDock", TILE_NORMAL);
739 x_pos = scr->scr_width - ICON_SIZE - DOCK_EXTRA_SPACE;
742 btn->xindex = 0;
743 btn->yindex = 0;
745 btn->icon->core->descriptor.handle_mousedown = iconMouseDown;
746 btn->icon->core->descriptor.parent_type = WCLASS_DOCK_ICON;
747 btn->icon->core->descriptor.parent = btn;
748 /*ChangeStackingLevel(btn->icon->core, WMDockLevel);*/
749 XMapWindow(dpy, btn->icon->core->window);
750 btn->x_pos = x_pos;
751 btn->y_pos = 0;
752 btn->docked = 1;
753 if (type == WM_CLIP)
754 scr->clip_icon = btn;
756 return btn;
760 static void
761 switchWSCommand(WMenu *menu, WMenuEntry *entry)
763 WAppIcon *btn, *icon = (WAppIcon*) entry->clientdata;
764 WScreen *scr = icon->icon->core->screen_ptr;
765 WDock *src, *dest;
766 LinkedList *selectedIcons;
767 int x, y;
769 if (entry->order == scr->current_workspace)
770 return;
771 src = icon->dock;
772 dest = scr->workspaces[entry->order]->clip;
774 selectedIcons = getSelected(src);
776 if (selectedIcons) {
777 while(selectedIcons) {
778 btn = selectedIcons->head;
779 if (wDockFindFreeSlot(dest, &x, &y)) {
780 moveIconBetweenDocks(src, dest, btn, x, y);
781 XUnmapWindow(dpy, btn->icon->core->window);
783 list_remove_head(&selectedIcons);
785 } else if (icon != scr->clip_icon) {
786 if (wDockFindFreeSlot(dest, &x, &y)) {
787 moveIconBetweenDocks(src, dest, icon, x, y);
788 XUnmapWindow(dpy, icon->icon->core->window);
795 static void
796 launchDockedApplication(WAppIcon *btn)
798 WScreen *scr = btn->icon->core->screen_ptr;
800 if (!btn->launching && btn->command!=NULL) {
801 if (!btn->forced_dock) {
802 btn->relaunching = btn->running;
803 btn->running = 1;
805 if (btn->wm_instance || btn->wm_class) {
806 WWindowAttributes attr;
807 memset(&attr, 0, sizeof(WWindowAttributes));
808 wDefaultFillAttributes(scr, btn->wm_instance, btn->wm_class, &attr,
809 True);
811 if (!attr.no_appicon && !btn->buggy_app)
812 btn->launching = 1;
813 else
814 btn->running = 0;
816 btn->drop_launch = 0;
817 scr->last_dock = btn->dock;
818 btn->pid = execCommand(btn, btn->command, NULL);
819 if (btn->pid>0) {
820 if (btn->buggy_app) {
821 /* give feedback that the app was launched */
822 btn->launching = 1;
823 dockIconPaint(btn);
824 btn->launching = 0;
825 WMAddTimerHandler(200, (WMCallback*)dockIconPaint, btn);
826 } else {
827 dockIconPaint(btn);
829 } else {
830 wwarning(_("could not launch application %s\n"), btn->command);
831 btn->launching = 0;
832 if (!btn->relaunching)
833 btn->running = 0;
840 static void
841 updateWorkspaceMenu(WMenu *menu, WAppIcon *icon)
843 WScreen *scr = menu->frame->screen_ptr;
844 char title[MAX_WORKSPACENAME_WIDTH+1];
845 int i;
847 if (!menu || !icon)
848 return;
850 for (i=0; i<scr->workspace_count; i++) {
851 if (i < menu->entry_no) {
852 if (strcmp(menu->entries[i]->text,scr->workspaces[i]->name)!=0) {
853 free(menu->entries[i]->text);
854 strcpy(title, scr->workspaces[i]->name);
855 menu->entries[i]->text = wstrdup(title);
856 menu->flags.realized = 0;
858 menu->entries[i]->clientdata = (void*)icon;
859 } else {
860 strcpy(title, scr->workspaces[i]->name);
862 wMenuAddCallback(menu, title, switchWSCommand, (void*)icon);
864 menu->flags.realized = 0;
866 if (i == scr->current_workspace) {
867 wMenuSetEnabled(menu, i, False);
868 } else {
869 wMenuSetEnabled(menu, i, True);
873 if (!menu->flags.realized)
874 wMenuRealize(menu);
878 static WMenu*
879 makeWorkspaceMenu(WScreen *scr)
881 WMenu *menu;
883 menu = wMenuCreate(scr, NULL, False);
884 if (!menu)
885 wwarning(_("could not create workspace submenu for Clip menu"));
887 wMenuAddCallback(menu, "", switchWSCommand, (void*)scr->clip_icon);
889 menu->flags.realized = 0;
890 wMenuRealize(menu);
892 return menu;
896 static void
897 updateClipOptionsMenu(WMenu *menu, WDock *dock)
899 WMenuEntry *entry;
900 int index = 0;
902 if (!menu || !dock)
903 return;
905 /* keep on top */
906 entry = menu->entries[index];
907 entry->flags.indicator_on = !dock->lowered;
908 entry->clientdata = dock;
910 /* collapsed */
911 entry = menu->entries[++index];
912 entry->flags.indicator_on = dock->collapsed;
913 entry->clientdata = dock;
915 /* auto-collapse */
916 entry = menu->entries[++index];
917 entry->flags.indicator_on = dock->auto_collapse;
918 entry->clientdata = dock;
920 /* attract icons */
921 entry = menu->entries[++index];
922 entry->flags.indicator_on = dock->attract_icons;
923 entry->clientdata = dock;
925 /* keep attracted icons */
926 entry = menu->entries[++index];
927 entry->flags.indicator_on = dock->keep_attracted;
928 entry->clientdata = dock;
930 menu->flags.realized = 0;
931 wMenuRealize(menu);
935 static WMenu*
936 makeClipOptionsMenu(WScreen *scr)
938 WMenu *menu;
939 WMenuEntry *entry;
941 menu = wMenuCreate(scr, NULL, False);
942 if (!menu) {
943 wwarning(_("could not create options submenu for Clip menu"));
944 return NULL;
947 entry = wMenuAddCallback(menu, _("Keep on top"),
948 toggleLoweredCallback, NULL);
949 entry->flags.indicator = 1;
950 entry->flags.indicator_on = 1;
951 entry->flags.indicator_type = MI_CHECK;
953 entry = wMenuAddCallback(menu, _("Collapsed"),
954 toggleCollapsedCallback, NULL);
955 entry->flags.indicator = 1;
956 entry->flags.indicator_on = 1;
957 entry->flags.indicator_type = MI_CHECK;
959 entry = wMenuAddCallback(menu, _("AutoCollapse"),
960 toggleAutoCollapseCallback, NULL);
961 entry->flags.indicator = 1;
962 entry->flags.indicator_on = 1;
963 entry->flags.indicator_type = MI_CHECK;
965 entry = wMenuAddCallback(menu, _("AutoAttract Icons"),
966 toggleAutoAttractCallback, NULL);
967 entry->flags.indicator = 1;
968 entry->flags.indicator_on = 1;
969 entry->flags.indicator_type = MI_CHECK;
971 entry = wMenuAddCallback(menu, _("Keep Attracted Icons"),
972 toggleKeepCallback, NULL);
973 entry->flags.indicator = 1;
974 entry->flags.indicator_on = 1;
975 entry->flags.indicator_type = MI_CHECK;
977 menu->flags.realized = 0;
978 wMenuRealize(menu);
980 return menu;
984 static WMenu*
985 dockMenuCreate(WScreen *scr, int type)
987 WMenu *menu;
988 WMenuEntry *entry;
990 if (type == WM_CLIP && scr->clip_menu)
991 return scr->clip_menu;
993 menu = wMenuCreate(scr, NULL, False);
994 if (type != WM_CLIP) {
995 entry = wMenuAddCallback(menu, _("Keep on top"),
996 toggleLoweredCallback, NULL);
997 entry->flags.indicator = 1;
998 entry->flags.indicator_on = 1;
999 entry->flags.indicator_type = MI_CHECK;
1000 } else {
1001 entry = wMenuAddCallback(menu, _("Clip Options"), NULL, NULL);
1002 scr->clip_options = makeClipOptionsMenu(scr);
1003 if (scr->clip_options)
1004 wMenuEntrySetCascade(menu, entry, scr->clip_options);
1006 wMenuAddCallback(menu, _("Rename Workspace"), renameCallback, NULL);
1008 wMenuAddCallback(menu, _("(Un)Select Icon"), selectCallback, NULL);
1010 wMenuAddCallback(menu, _("(Un)Select All Icons"), selectIconsCallback,
1011 NULL);
1013 wMenuAddCallback(menu, _("Keep Icon(s)"), keepIconsCallback, NULL);
1015 entry = wMenuAddCallback(menu, _("Move Icon(s) To"), NULL, NULL);
1016 scr->clip_submenu = makeWorkspaceMenu(scr);
1017 if (scr->clip_submenu)
1018 wMenuEntrySetCascade(menu, entry, scr->clip_submenu);
1020 wMenuAddCallback(menu, _("Remove Icon(s)"), removeIconsCallback, NULL);
1022 wMenuAddCallback(menu, _("Attract Icons"), colectIconsCallback, NULL);
1025 wMenuAddCallback(menu, _("Launch"), launchCallback, NULL);
1027 wMenuAddCallback(menu, _("Unhide Here"), unhideHereCallback, NULL);
1029 wMenuAddCallback(menu, _("(Un)Hide"), hideCallback, NULL);
1031 wMenuAddCallback(menu, _("Settings..."), settingsCallback, NULL);
1033 wMenuAddCallback(menu, _("Kill"), killCallback, NULL);
1035 if (type == WM_CLIP)
1036 scr->clip_menu = menu;
1038 return menu;
1042 WDock*
1043 wDockCreate(WScreen *scr, int type)
1045 WDock *dock;
1046 WAppIcon *btn;
1047 int icon_count;
1049 make_keys();
1051 dock = wmalloc(sizeof(WDock));
1052 memset(dock, 0, sizeof(WDock));
1054 if (type == WM_CLIP)
1055 icon_count = CLIP_MAX_ICONS;
1056 else
1057 icon_count = scr->scr_height/wPreferences.icon_size;
1059 dock->icon_array = wmalloc(sizeof(WAppIcon*)*icon_count);
1060 memset(dock->icon_array, 0, sizeof(WAppIcon*)*icon_count);
1062 dock->max_icons = icon_count;
1064 btn = mainIconCreate(scr, type);
1066 btn->dock = dock;
1068 dock->x_pos = btn->x_pos;
1069 dock->y_pos = btn->y_pos;
1070 dock->screen_ptr = scr;
1071 dock->type = type;
1072 dock->icon_count = 1;
1073 dock->on_right_side = 1;
1074 dock->collapsed = 0;
1075 dock->auto_collapse = 0;
1076 dock->auto_collapse_magic = NULL;
1077 dock->attract_icons = 0;
1078 dock->keep_attracted = 0;
1079 dock->lowered = 1;
1080 dock->icon_array[0] = btn;
1081 wRaiseFrame(btn->icon->core);
1082 XMoveWindow(dpy, btn->icon->core->window, btn->x_pos, btn->y_pos);
1084 /* create dock menu */
1085 dock->menu = dockMenuCreate(scr, type);
1087 return dock;
1091 void
1092 wDockDestroy(WDock *dock)
1094 int i;
1095 WAppIcon *aicon;
1097 for (i=(dock->type == WM_CLIP) ? 1 : 0; i<dock->max_icons; i++) {
1098 aicon = dock->icon_array[i];
1099 if (aicon) {
1100 int keepit = aicon->running && wApplicationOf(aicon->main_window);
1101 wDockDetach(dock, aicon);
1102 if (keepit) {
1103 PlaceIcon(dock->screen_ptr, &aicon->x_pos, &aicon->y_pos);
1104 XMoveWindow(dpy, aicon->icon->core->window,
1105 aicon->x_pos, aicon->y_pos);
1106 if (!dock->mapped || dock->collapsed)
1107 XMapWindow(dpy, aicon->icon->core->window);
1111 if (wPreferences.auto_arrange_icons)
1112 wArrangeIcons(dock->screen_ptr, True);
1113 free(dock->icon_array);
1114 if (dock->menu && dock->type!=WM_CLIP)
1115 wMenuDestroy(dock->menu, True);
1116 free(dock);
1120 void
1121 wClipIconPaint(WAppIcon *aicon)
1123 WScreen *scr = aicon->icon->core->screen_ptr;
1124 WWorkspace *workspace = scr->workspaces[scr->current_workspace];
1125 GC gc;
1126 Window win = aicon->icon->core->window;
1127 int length, nlength;
1128 char *ws_name, ws_number[10];
1129 int ty, tx;
1131 wIconPaint(aicon->icon);
1133 length = strlen(workspace->name);
1134 ws_name = malloc(length + 1);
1135 sprintf(ws_name, "%s", workspace->name);
1136 sprintf(ws_number, "%i", scr->current_workspace + 1);
1137 nlength = strlen(ws_number);
1139 gc = scr->clip_title_gc;
1141 if (!workspace->clip->collapsed)
1142 XSetForeground(dpy, gc, scr->clip_title_pixel[CLIP_NORMAL]);
1143 else
1144 XSetForeground(dpy, gc, scr->clip_title_pixel[CLIP_COLLAPSED]);
1146 ty = ICON_SIZE - scr->clip_title_font->height - 3;
1148 tx = CLIP_BUTTON_SIZE*ICON_SIZE/64;
1150 wDrawString(win, scr->clip_title_font, gc, tx,
1151 ty + scr->clip_title_font->y, ws_name, length);
1153 tx = (ICON_SIZE/2 - wTextWidth(scr->clip_title_font->font, ws_number, nlength))/2;
1155 wDrawString(win, scr->clip_title_font, gc, tx,
1156 scr->clip_title_font->y + 2, ws_number, nlength);
1158 free(ws_name);
1160 if (aicon->launching) {
1161 XFillRectangle(dpy, aicon->icon->core->window, scr->stipple_gc,
1162 0, 0, wPreferences.icon_size, wPreferences.icon_size);
1164 paintClipButtons(aicon, aicon->dock->lclip_button_pushed,
1165 aicon->dock->rclip_button_pushed);
1169 static void
1170 clipIconExpose(WObjDescriptor *desc, XEvent *event)
1172 wClipIconPaint(desc->parent);
1176 static void
1177 dockIconPaint(WAppIcon *btn)
1179 if (btn == btn->icon->core->screen_ptr->clip_icon)
1180 wClipIconPaint(btn);
1181 else
1182 wAppIconPaint(btn);
1186 static proplist_t
1187 make_icon_state(WAppIcon *btn)
1189 proplist_t node = NULL;
1190 proplist_t command, autolaunch, name, forced, host, position, buggy;
1191 char *tmp;
1192 char buffer[64];
1194 if (btn) {
1195 if (!btn->command)
1196 command = PLMakeString("-");
1197 else
1198 command = PLMakeString(btn->command);
1200 autolaunch = btn->auto_launch ? dYes : dNo;
1202 tmp = EscapeWM_CLASS(btn->wm_instance, btn->wm_class);
1204 name = PLMakeString(tmp);
1206 free(tmp);
1208 forced = btn->forced_dock ? dYes : dNo;
1210 buggy = btn->buggy_app ? dYes: dNo;
1212 if (btn == btn->icon->core->screen_ptr->clip_icon)
1213 sprintf(buffer, "%i,%i", btn->x_pos, btn->y_pos);
1214 else
1215 sprintf(buffer, "%hi,%hi", btn->xindex, btn->yindex);
1216 position = PLMakeString(buffer);
1218 node = PLMakeDictionaryFromEntries(dCommand, command,
1219 dName, name,
1220 dAutoLaunch, autolaunch,
1221 dForced, forced,
1222 dBuggyApplication, buggy,
1223 dPosition, position,
1224 NULL);
1225 PLRelease(command);
1226 PLRelease(name);
1227 PLRelease(position);
1228 #ifdef OFFIX_DND
1229 if (btn->dnd_command) {
1230 command = PLMakeString(btn->dnd_command);
1231 PLInsertDictionaryEntry(node, dDropCommand, command);
1232 PLRelease(command);
1234 #endif /* OFFIX_DND */
1236 if (btn->client_machine && btn->remote_start) {
1237 host = PLMakeString(btn->client_machine);
1238 PLInsertDictionaryEntry(node, dHost, host);
1239 PLRelease(host);
1243 return node;
1247 static proplist_t
1248 dockSaveState(WDock *dock)
1250 int i;
1251 proplist_t icon_info;
1252 proplist_t list=NULL, dock_state=NULL;
1253 proplist_t value;
1254 char buffer[256];
1256 list = PLMakeArrayFromElements(NULL);
1258 for (i=(dock->type==WM_DOCK ? 0 : 1); i<dock->max_icons; i++) {
1259 WAppIcon *btn = dock->icon_array[i];
1261 if (!btn || (btn->attracted && !dock->keep_attracted))
1262 continue;
1264 if ((icon_info = make_icon_state(dock->icon_array[i]))) {
1265 list = PLAppendArrayElement(list, icon_info);
1266 PLRelease(icon_info);
1270 dock_state = PLMakeDictionaryFromEntries(dApplications, list, NULL);
1272 PLRelease(list);
1274 if (dock->type == WM_DOCK) {
1275 sprintf(buffer, "%i,%i", (dock->on_right_side ? -ICON_SIZE : 0),
1276 dock->y_pos);
1277 value = PLMakeString(buffer);
1278 PLInsertDictionaryEntry(dock_state, dPosition, value);
1279 PLRelease(value);
1282 value = (dock->lowered ? dYes : dNo);
1283 PLInsertDictionaryEntry(dock_state, dLowered, value);
1285 if (dock->type == WM_CLIP) {
1286 value = (dock->collapsed ? dYes : dNo);
1287 PLInsertDictionaryEntry(dock_state, dCollapsed, value);
1289 value = (dock->auto_collapse ? dYes : dNo);
1290 PLInsertDictionaryEntry(dock_state, dAutoCollapse, value);
1292 value = (dock->attract_icons ? dYes : dNo);
1293 PLInsertDictionaryEntry(dock_state, dAutoAttractIcons, value);
1295 value = (dock->keep_attracted ? dYes : dNo);
1296 PLInsertDictionaryEntry(dock_state, dKeepAttracted, value);
1299 return dock_state;
1303 void
1304 wDockSaveState(WScreen *scr)
1306 proplist_t dock_state;
1308 dock_state = dockSaveState(scr->dock);
1310 PLInsertDictionaryEntry(scr->session_state, dDock, dock_state);
1312 PLRelease(dock_state);
1316 void
1317 wClipSaveState(WScreen *scr)
1319 proplist_t clip_state;
1321 clip_state = make_icon_state(scr->clip_icon);
1323 PLInsertDictionaryEntry(scr->session_state, dClip, clip_state);
1325 PLRelease(clip_state);
1329 proplist_t
1330 wClipSaveWorkspaceState(WScreen *scr, int workspace)
1332 return dockSaveState(scr->workspaces[workspace]->clip);
1336 static WAppIcon*
1337 restore_icon_state(WScreen *scr, proplist_t info, int type, int index)
1339 WAppIcon *aicon;
1340 char *wclass, *winstance;
1341 proplist_t cmd, value;
1342 char *command;
1345 cmd = PLGetDictionaryEntry(info, dCommand);
1346 if (!cmd || !PLIsString(cmd)) {
1347 return NULL;
1350 /* parse window name */
1351 value = PLGetDictionaryEntry(info, dName);
1352 if (!value)
1353 return NULL;
1355 ParseWindowName(value, &winstance, &wclass, "dock");
1357 if (!winstance && !wclass) {
1358 return NULL;
1361 /* get commands */
1363 if (cmd)
1364 command = wstrdup(PLGetString(cmd));
1365 else
1366 command = NULL;
1368 if (!command || strcmp(command, "-")==0) {
1369 if (command)
1370 free(command);
1371 if (wclass)
1372 free(wclass);
1373 if (winstance)
1374 free(winstance);
1376 return NULL;
1379 aicon = wAppIconCreateForDock(scr, command, winstance, wclass,
1380 TILE_NORMAL);
1381 if (wclass)
1382 free(wclass);
1383 if (winstance)
1384 free(winstance);
1386 aicon->icon->core->descriptor.handle_mousedown = iconMouseDown;
1387 if (type == WM_CLIP) {
1388 aicon->icon->core->descriptor.handle_enternotify = clipEnterNotify;
1389 aicon->icon->core->descriptor.handle_leavenotify = clipLeaveNotify;
1391 aicon->icon->core->descriptor.parent_type = WCLASS_DOCK_ICON;
1392 aicon->icon->core->descriptor.parent = aicon;
1395 #ifdef OFFIX_DND
1396 cmd = PLGetDictionaryEntry(info, dDropCommand);
1397 if (cmd)
1398 aicon->dnd_command = wstrdup(PLGetString(cmd));
1399 #endif
1401 /* check auto launch */
1402 value = PLGetDictionaryEntry(info, dAutoLaunch);
1404 aicon->auto_launch = 0;
1405 if (value) {
1406 if (PLIsString(value)) {
1407 if (strcasecmp(PLGetString(value), "YES")==0)
1408 aicon->auto_launch = 1;
1409 } else {
1410 wwarning(_("bad value in docked icon state info %s"),
1411 PLGetString(dAutoLaunch));
1415 /* check if it wasn't normally docked */
1416 value = PLGetDictionaryEntry(info, dForced);
1418 aicon->forced_dock = 0;
1419 if (value) {
1420 if (PLIsString(value)) {
1421 if (strcasecmp(PLGetString(value), "YES")==0)
1422 aicon->forced_dock = 1;
1423 } else {
1424 wwarning(_("bad value in docked icon state info %s"),
1425 PLGetString(dForced));
1429 /* check if we can rely on the stuff in the app */
1430 value = PLGetDictionaryEntry(info, dBuggyApplication);
1432 aicon->buggy_app = 0;
1433 if (value) {
1434 if (PLIsString(value)) {
1435 if (strcasecmp(PLGetString(value), "YES")==0)
1436 aicon->buggy_app = 1;
1437 } else {
1438 wwarning(_("bad value in docked icon state info %s"),
1439 PLGetString(dBuggyApplication));
1443 /* get position in the dock */
1444 value = PLGetDictionaryEntry(info, dPosition);
1445 if (value && PLIsString(value)) {
1446 if (sscanf(PLGetString(value), "%hi,%hi", &aicon->xindex,
1447 &aicon->yindex)!=2)
1448 wwarning(_("bad value in docked icon state info %s"),
1449 PLGetString(dPosition));
1451 /* check position sanity */
1452 /* incomplete section! */
1453 if (type == WM_DOCK) {
1454 aicon->xindex = 0;
1455 if (aicon->yindex < 0)
1456 wwarning(_("bad value in docked icon position %i,%i"),
1457 aicon->xindex, aicon->yindex);
1459 } else {
1460 aicon->yindex = index;
1461 aicon->xindex = 0;
1464 aicon->running = 0;
1465 aicon->docked = 1;
1467 return aicon;
1471 #define COMPLAIN(key) wwarning(_("bad value in dock state info:%s"), key)
1474 WAppIcon*
1475 wClipRestoreState(WScreen *scr, proplist_t clip_state)
1477 WAppIcon *icon;
1478 proplist_t value;
1481 icon = mainIconCreate(scr, WM_CLIP);
1483 if (!clip_state)
1484 return icon;
1485 else
1486 PLRetain(clip_state);
1488 /* restore position */
1490 value = PLGetDictionaryEntry(clip_state, dPosition);
1492 if (value) {
1493 if (!PLIsString(value))
1494 COMPLAIN("Position");
1495 else {
1496 if (sscanf(PLGetString(value), "%i,%i", &icon->x_pos,
1497 &icon->y_pos)!=2)
1498 COMPLAIN("Position");
1500 /* check position sanity */
1501 if (icon->y_pos < 0)
1502 icon->y_pos = 0;
1503 else if (icon->y_pos > scr->scr_height-ICON_SIZE)
1504 icon->y_pos = scr->scr_height-ICON_SIZE;
1506 if (icon->x_pos < 0)
1507 icon->x_pos = 0;
1508 else if (icon->x_pos > scr->scr_width-ICON_SIZE)
1509 icon->x_pos = scr->scr_width-ICON_SIZE;
1513 #ifdef OFFIX_DND
1514 value = PLGetDictionaryEntry(clip_state, dDropCommand);
1515 if (value && PLIsString(value))
1516 icon->dnd_command = wstrdup(PLGetString(value));
1517 #endif
1519 PLRelease(clip_state);
1521 return icon;
1525 WDock*
1526 wDockRestoreState(WScreen *scr, proplist_t dock_state, int type)
1528 WDock *dock;
1529 proplist_t apps;
1530 proplist_t value;
1531 WAppIcon *aicon, *old_top;
1532 int count, i;
1535 dock = wDockCreate(scr, type);
1537 if (!dock_state)
1538 return dock;
1540 if (dock_state)
1541 PLRetain(dock_state);
1544 /* restore position */
1546 value = PLGetDictionaryEntry(dock_state, dPosition);
1548 if (value) {
1549 if (!PLIsString(value))
1550 COMPLAIN("Position");
1551 else {
1552 if (sscanf(PLGetString(value), "%i,%i", &dock->x_pos,
1553 &dock->y_pos)!=2)
1554 COMPLAIN("Position");
1556 /* check position sanity */
1557 if (dock->y_pos < 0)
1558 dock->y_pos = 0;
1559 else if (dock->y_pos > scr->scr_height-ICON_SIZE)
1560 dock->y_pos = scr->scr_height-ICON_SIZE;
1562 /* This is no more needed. ??? */
1563 if (type == WM_CLIP) {
1564 if (dock->x_pos < 0)
1565 dock->x_pos = 0;
1566 else if (dock->x_pos > scr->scr_width-ICON_SIZE)
1567 dock->x_pos = scr->scr_width-ICON_SIZE;
1569 else {
1570 if (dock->x_pos >= 0) {
1571 dock->x_pos = DOCK_EXTRA_SPACE;
1572 dock->on_right_side = 0;
1573 } else {
1574 dock->x_pos = scr->scr_width - DOCK_EXTRA_SPACE - ICON_SIZE;
1575 dock->on_right_side = 1;
1581 /* restore lowered/raised state */
1583 dock->lowered = 0;
1585 value = PLGetDictionaryEntry(dock_state, dLowered);
1587 if (value) {
1588 if (!PLIsString(value))
1589 COMPLAIN("Lowered");
1590 else {
1591 if (strcasecmp(PLGetString(value), "YES")==0)
1592 dock->lowered = 1;
1597 /* restore collapsed state */
1599 dock->collapsed = 0;
1601 value = PLGetDictionaryEntry(dock_state, dCollapsed);
1603 if (value) {
1604 if (!PLIsString(value))
1605 COMPLAIN("Collapsed");
1606 else {
1607 if (strcasecmp(PLGetString(value), "YES")==0)
1608 dock->collapsed = 1;
1613 /* restore auto-collapsed state */
1615 value = PLGetDictionaryEntry(dock_state, dAutoCollapse);
1617 if (value) {
1618 if (!PLIsString(value))
1619 COMPLAIN("AutoCollapse");
1620 else {
1621 if (strcasecmp(PLGetString(value), "YES")==0) {
1622 dock->auto_collapse = 1;
1623 dock->collapsed = 1;
1629 /* restore attract icons state */
1631 dock->attract_icons = 0;
1633 value = PLGetDictionaryEntry(dock_state, dAutoAttractIcons);
1635 if (value) {
1636 if (!PLIsString(value))
1637 COMPLAIN("AutoAttractIcons");
1638 else {
1639 if (strcasecmp(PLGetString(value), "YES")==0)
1640 dock->attract_icons = 1;
1645 /* restore keep attracted icons state */
1647 dock->keep_attracted = 0;
1649 value = PLGetDictionaryEntry(dock_state, dKeepAttracted);
1651 if (value) {
1652 if (!PLIsString(value))
1653 COMPLAIN("KeepAttracted");
1654 else {
1655 if (strcasecmp(PLGetString(value), "YES")==0)
1656 dock->keep_attracted = 1;
1661 /* application list */
1663 apps = PLGetDictionaryEntry(dock_state, dApplications);
1665 if (!apps) {
1666 goto finish;
1669 count = PLGetNumberOfElements(apps);
1671 if (count==0)
1672 goto finish;
1674 old_top = dock->icon_array[0];
1676 /* dock->icon_count is set to 1 when dock is created.
1677 * Since Clip is already restored, we want to keep it so for clip,
1678 * but for dock we may change the default top tile, so we set it to 0.
1680 if (type == WM_DOCK)
1681 dock->icon_count = 0;
1683 for (i=0; i<count; i++) {
1684 if (dock->icon_count >= dock->max_icons) {
1685 wwarning(_("there are too many icons stored in dock. Ignoring what doesn't fit"));
1686 break;
1689 value = PLGetArrayElement(apps, i);
1690 aicon = restore_icon_state(scr, value, type, dock->icon_count);
1692 dock->icon_array[dock->icon_count] = aicon;
1694 if (aicon) {
1695 aicon->dock = dock;
1696 aicon->x_pos = dock->x_pos + (aicon->xindex*ICON_SIZE);
1697 aicon->y_pos = dock->y_pos + (aicon->yindex*ICON_SIZE);
1699 if (dock->lowered)
1700 ChangeStackingLevel(aicon->icon->core, WMNormalLevel);
1701 else
1702 ChangeStackingLevel(aicon->icon->core, WMDockLevel);
1704 wCoreConfigure(aicon->icon->core, aicon->x_pos, aicon->y_pos,
1705 0, 0);
1707 if (!dock->collapsed)
1708 XMapWindow(dpy, aicon->icon->core->window);
1709 wRaiseFrame(aicon->icon->core);
1711 dock->icon_count++;
1712 } else if (dock->icon_count==0 && type==WM_DOCK)
1713 dock->icon_count++;
1716 /* if the first icon is not defined, use the default */
1717 if (dock->icon_array[0]==NULL) {
1718 /* update default icon */
1719 old_top->x_pos = dock->x_pos;
1720 old_top->y_pos = dock->y_pos;
1721 if (dock->lowered)
1722 ChangeStackingLevel(old_top->icon->core, WMNormalLevel);
1723 else
1724 ChangeStackingLevel(old_top->icon->core, WMDockLevel);
1725 dock->icon_array[0] = old_top;
1726 XMoveWindow(dpy, old_top->icon->core->window, dock->x_pos, dock->y_pos);
1727 /* we don't need to increment dock->icon_count here because it was
1728 * incremented in the loop above.
1730 } else if (old_top!=dock->icon_array[0]) {
1731 if (old_top == scr->clip_icon)
1732 scr->clip_icon = dock->icon_array[0];
1733 wAppIconDestroy(old_top);
1736 finish:
1737 if (dock_state)
1738 PLRelease(dock_state);
1740 return dock;
1745 void
1746 wDockLaunchWithState(WDock *dock, WAppIcon *btn, WSavedState *state)
1748 if (btn && btn->command && !btn->running && !btn->launching) {
1750 btn->drop_launch = 0;
1752 btn->pid = execCommand(btn, btn->command, state);
1754 if (btn->pid>0) {
1755 if (!btn->forced_dock && !btn->buggy_app) {
1756 btn->launching = 1;
1757 dockIconPaint(btn);
1760 } else {
1761 free(state);
1766 void
1767 wDockDoAutoLaunch(WDock *dock, int workspace)
1769 WAppIcon *btn;
1770 WSavedState *state;
1771 int i;
1773 for (i=0; i < dock->max_icons; i++) {
1774 btn = dock->icon_array[i];
1775 if (!btn || !btn->auto_launch)
1776 continue;
1778 state = wmalloc(sizeof(WSavedState));
1779 memset(state, 0, sizeof(WSavedState));
1780 state->workspace = workspace;
1781 /* TODO: this is klugy and is very difficult to understand
1782 * what's going on. Try to clean up */
1783 wDockLaunchWithState(dock, btn, state);
1787 #ifdef REDUCE_APPICONS
1788 void
1789 wDockSimulateLaunch(WDock *dock, WAppIcon *btn)
1791 if ((btn == NULL) || (dock == NULL))
1792 return;
1794 if (!btn->running) {
1795 if ((btn->icon->owner == NULL) && (btn->applist))
1796 btn->icon->owner = btn->applist->wapp->main_window_desc;
1797 if (!btn->forced_dock)
1798 btn->launching = 1;
1799 dockIconPaint(btn);
1800 wusleep(5000);
1803 #endif
1805 #ifdef OFFIX_DND
1806 static WDock*
1807 findDock(WScreen *scr, XEvent *event, int *icon_pos)
1809 WDock *dock;
1810 int i;
1812 *icon_pos = -1;
1813 if ((dock = scr->dock)!=NULL) {
1814 for (i=0; i<dock->max_icons; i++) {
1815 if (dock->icon_array[i]
1816 && dock->icon_array[i]->icon->core->window==event->xclient.window) {
1817 *icon_pos = i;
1818 break;
1822 if (*icon_pos<0 && (dock = scr->workspaces[scr->current_workspace]->clip)!=NULL) {
1823 for (i=0; i<dock->max_icons; i++) {
1824 if (dock->icon_array[i]
1825 && dock->icon_array[i]->icon->core->window==event->xclient.window) {
1826 *icon_pos = i;
1827 break;
1831 if(*icon_pos>=0)
1832 return dock;
1833 return NULL;
1838 wDockReceiveDNDDrop(WScreen *scr, XEvent *event)
1840 WDock *dock;
1841 WAppIcon *btn;
1842 int icon_pos;
1844 dock = findDock(scr, event, &icon_pos);
1845 if (!dock)
1846 return False;
1849 * Return True if the drop was on an application icon window.
1850 * In this case, let the ClientMessage handler redirect the
1851 * message to the app.
1853 if (dock->icon_array[icon_pos]->icon->icon_win!=None)
1854 return True;
1856 if (dock->icon_array[icon_pos]->dnd_command!=NULL) {
1857 scr->flags.dnd_data_convertion_status = 0;
1859 btn = dock->icon_array[icon_pos];
1861 if (!btn->forced_dock) {
1862 btn->relaunching = btn->running;
1863 btn->running = 1;
1865 if (btn->wm_instance || btn->wm_class) {
1866 WWindowAttributes attr;
1867 memset(&attr, 0, sizeof(WWindowAttributes));
1868 wDefaultFillAttributes(btn->icon->core->screen_ptr,
1869 btn->wm_instance,
1870 btn->wm_class, &attr, True);
1872 if (!attr.no_appicon)
1873 btn->launching = 1;
1874 else
1875 btn->running = 0;
1878 btn->drop_launch = 1;
1879 scr->last_dock = dock;
1880 btn->pid = execCommand(btn, btn->dnd_command, NULL);
1881 if (btn->pid>0) {
1882 dockIconPaint(btn);
1883 } else {
1884 btn->launching = 0;
1885 if (!btn->relaunching) {
1886 btn->running = 0;
1890 return False;
1892 #endif /* OFFIX_DND */
1896 Bool
1897 wDockAttachIcon(WDock *dock, WAppIcon *icon, int x, int y)
1899 WWindow *wwin;
1900 char **argv;
1901 int argc;
1902 int index;
1904 wwin = icon->icon->owner;
1905 if (icon->command==NULL) {
1906 icon->editing = 0;
1907 if (XGetCommand(dpy, wwin->client_win, &argv, &argc) && argc>0) {
1909 icon->command = FlattenStringList(argv, argc);
1910 XFreeStringList(argv);
1911 } else {
1912 char *command=NULL;
1914 /* icon->forced_dock = 1;*/
1915 if (!icon->attracted || dock->type!=WM_CLIP || dock->keep_attracted) {
1916 icon->editing = 1;
1917 if (wInputDialog(dock->screen_ptr, _("Dock Icon"),
1918 _("Type the command used to launch the application"),
1919 &command)) {
1920 if (command && (command[0]==0 ||
1921 (command[0]=='-' && command[1]==0))) {
1922 free(command);
1923 command = NULL;
1925 icon->command = command;
1926 icon->editing = 0;
1927 } else {
1928 icon->editing = 0;
1929 if (command)
1930 free(command);
1931 /* If the target is the dock, reject the icon. If
1932 * the target is the clip, make it an attracted icon
1934 if (dock->type==WM_CLIP) {
1935 icon->attracted = 1;
1936 if (!icon->icon->shadowed) {
1937 icon->icon->shadowed = 1;
1938 icon->icon->force_paint = 1;
1940 } else {
1941 return False;
1946 } else {
1947 icon->editing = 0;
1950 for (index=1; index<dock->max_icons; index++)
1951 if (dock->icon_array[index] == NULL)
1952 break;
1953 /* if (index == dock->max_icons)
1954 return; */
1956 assert(index < dock->max_icons);
1958 dock->icon_array[index] = icon;
1959 icon->yindex = y;
1960 icon->xindex = x;
1962 icon->x_pos = dock->x_pos + x*ICON_SIZE;
1963 icon->y_pos = dock->y_pos + y*ICON_SIZE;
1965 dock->icon_count++;
1967 icon->running = 1;
1968 icon->launching = 0;
1969 icon->docked = 1;
1970 icon->dock = dock;
1971 icon->icon->core->descriptor.handle_mousedown = iconMouseDown;
1972 if (dock->type == WM_CLIP) {
1973 icon->icon->core->descriptor.handle_enternotify = clipEnterNotify;
1974 icon->icon->core->descriptor.handle_leavenotify = clipLeaveNotify;
1976 icon->icon->core->descriptor.parent_type = WCLASS_DOCK_ICON;
1977 icon->icon->core->descriptor.parent = icon;
1979 MoveInStackListUnder(dock->icon_array[index-1]->icon->core,
1980 icon->icon->core);
1981 wAppIconMove(icon, icon->x_pos, icon->y_pos);
1982 wAppIconPaint(icon);
1984 if (wPreferences.auto_arrange_icons)
1985 wArrangeIcons(dock->screen_ptr, True);
1987 #ifdef OFFIX_DND
1988 if (icon->command && !icon->dnd_command) {
1989 icon->dnd_command = wmalloc(strlen(icon->command)+8);
1990 sprintf(icon->dnd_command, "%s %%d", icon->command);
1992 #endif
1994 return True;
1998 void
1999 reattachIcon(WDock *dock, WAppIcon *icon, int x, int y)
2001 int index;
2003 for(index=1; index<dock->max_icons; index++) {
2004 if(dock->icon_array[index] == icon)
2005 break;
2007 assert(index < dock->max_icons);
2009 icon->yindex = y;
2010 icon->xindex = x;
2012 icon->x_pos = dock->x_pos + x*ICON_SIZE;
2013 icon->y_pos = dock->y_pos + y*ICON_SIZE;
2017 Bool
2018 moveIconBetweenDocks(WDock *src, WDock *dest, WAppIcon *icon, int x, int y)
2020 WWindow *wwin;
2021 char **argv;
2022 int argc;
2023 int index;
2025 if (dest == NULL)
2026 return False;
2028 wwin = icon->icon->owner;
2031 * For the moment we can't do this if we move icons in Clip from one
2032 * workspace to other, because if we move two or more icons without
2033 * command, the dialog box will not be able to tell us to which of the
2034 * moved icons it applies. -Dan
2036 if ((dest->type==WM_DOCK /*|| dest->keep_attracted*/) && icon->command==NULL) {
2037 if (XGetCommand(dpy, wwin->client_win, &argv, &argc) && argc>0) {
2039 icon->command = FlattenStringList(argv, argc);
2040 XFreeStringList(argv);
2041 } else {
2042 char *command=NULL;
2044 icon->editing = 1;
2045 /* icon->forced_dock = 1;*/
2046 if (wInputDialog(src->screen_ptr, _("Dock Icon"),
2047 _("Type the command used to launch the application"),
2048 &command)) {
2049 if (command && (command[0]==0 ||
2050 (command[0]=='-' && command[1]==0))) {
2051 free(command);
2052 command = NULL;
2054 icon->command = command;
2055 } else {
2056 icon->editing = 0;
2057 if (command)
2058 free(command);
2059 return False;
2061 icon->editing = 0;
2065 for(index=1; index<src->max_icons; index++) {
2066 if(src->icon_array[index] == icon)
2067 break;
2069 assert(index < src->max_icons);
2071 src->icon_array[index] = NULL;
2072 src->icon_count--;
2074 for(index=1; index<dest->max_icons; index++) {
2075 if(dest->icon_array[index] == NULL)
2076 break;
2078 /* if (index == dest->max_icons)
2079 return; */
2081 assert(index < dest->max_icons);
2083 dest->icon_array[index] = icon;
2084 icon->dock = dest;
2086 /* deselect the icon */
2087 if (icon->icon->selected)
2088 wIconSelect(icon->icon);
2090 if (dest->type == WM_DOCK) {
2091 icon->icon->core->descriptor.handle_enternotify = NULL;
2092 icon->icon->core->descriptor.handle_leavenotify = NULL;
2093 } else {
2094 icon->icon->core->descriptor.handle_enternotify = clipEnterNotify;
2095 icon->icon->core->descriptor.handle_leavenotify = clipLeaveNotify;
2098 /* set it to be kept when moving to dock, or to a clip that keep the
2099 * attracted icons.
2100 * Unless the icon does not have a command set
2102 if (icon->command && (dest->type==WM_DOCK || dest->keep_attracted)) {
2103 icon->attracted = 0;
2104 if (icon->icon->shadowed) {
2105 icon->icon->shadowed = 0;
2106 icon->icon->force_paint = 1;
2110 if (src->auto_collapse)
2111 clipLeave(src);
2113 icon->yindex = y;
2114 icon->xindex = x;
2116 icon->x_pos = dest->x_pos + x*ICON_SIZE;
2117 icon->y_pos = dest->y_pos + y*ICON_SIZE;
2119 dest->icon_count++;
2121 MoveInStackListUnder(dest->icon_array[index-1]->icon->core,
2122 icon->icon->core);
2123 wAppIconPaint(icon);
2125 return True;
2128 void
2129 wDockDetach(WDock *dock, WAppIcon *icon)
2131 int index;
2133 /* make the settings panel be closed */
2134 if (icon->panel) {
2135 DestroyDockAppSettingsPanel(icon->panel);
2138 icon->docked = 0;
2139 icon->dock = NULL;
2140 icon->attracted = 0;
2141 if (icon->icon->shadowed) {
2142 icon->icon->shadowed = 0;
2143 icon->icon->force_paint = 1;
2146 /* deselect the icon */
2147 if (icon->icon->selected)
2148 wIconSelect(icon->icon);
2150 if (icon->command) {
2151 free(icon->command);
2152 icon->command = NULL;
2154 #ifdef OFFIX_DND
2155 if (icon->dnd_command) {
2156 free(icon->dnd_command);
2157 icon->dnd_command = NULL;
2159 #endif
2161 for (index=1; index<dock->max_icons; index++)
2162 if (dock->icon_array[index] == icon)
2163 break;
2164 assert(index < dock->max_icons);
2165 dock->icon_array[index] = NULL;
2166 icon->yindex = -1;
2167 icon->xindex = -1;
2168 dock->icon_count--;
2170 /* if the dock is not attached to an application or
2171 * the the application did not set the approriate hints yet,
2172 * destroy the icon */
2173 #ifdef REDUCE_APPICONS
2174 if ((icon->num_apps == 0) && (!icon->running || !wApplicationOf(icon->main_window)) )
2175 #else
2176 if (!icon->running || !wApplicationOf(icon->main_window))
2177 #endif
2178 wAppIconDestroy(icon);
2179 else {
2180 icon->icon->core->descriptor.handle_mousedown = appIconMouseDown;
2181 icon->icon->core->descriptor.handle_enternotify = NULL;
2182 icon->icon->core->descriptor.handle_leavenotify = NULL;
2183 icon->icon->core->descriptor.parent_type = WCLASS_APPICON;
2184 icon->icon->core->descriptor.parent = icon;
2186 ChangeStackingLevel(icon->icon->core, NORMAL_ICON_LEVEL);
2188 wAppIconPaint(icon);
2189 if (wPreferences.auto_arrange_icons) {
2190 wArrangeIcons(dock->screen_ptr, True);
2193 if (dock->auto_collapse)
2194 clipLeave(dock);
2199 * returns the closest Dock slot index for the passed
2200 * coordinates.
2202 * Returns False if icon can't be docked.
2204 Bool
2205 wDockSnapIcon(WDock *dock, WAppIcon *icon, int req_x, int req_y,
2206 int *ret_x, int *ret_y, int redocking)
2208 WScreen *scr = dock->screen_ptr;
2209 int dx, dy;
2210 int ex_x, ex_y;
2211 int i, offset = ICON_SIZE/2;
2212 WAppIcon *aicon = NULL;
2213 WAppIcon *nicon = NULL;
2216 if (wPreferences.flags.noupdates)
2217 return False;
2219 dx = dock->x_pos;
2220 dy = dock->y_pos;
2222 /* if the dock is full */
2223 if (!redocking &&
2224 (dock->icon_count >= dock->max_icons)) {
2225 return False;
2228 /* exact position */
2229 if (req_y < dy)
2230 ex_y = (req_y - offset - dy)/ICON_SIZE;
2231 else
2232 ex_y = (req_y + offset - dy)/ICON_SIZE;
2234 if (req_x < dx)
2235 ex_x = (req_x - offset - dx)/ICON_SIZE;
2236 else
2237 ex_x = (req_x + offset - dx)/ICON_SIZE;
2239 /* check if the icon is outside the screen boundaries */
2240 if (dx + ex_x*ICON_SIZE < -ICON_SIZE+2 ||
2241 dx + ex_x*ICON_SIZE > scr->scr_width-1 ||
2242 dy + ex_y*ICON_SIZE < -ICON_SIZE+2 ||
2243 dy + ex_y*ICON_SIZE > scr->scr_height-1)
2244 return False;
2246 if (dock->type == WM_DOCK) {
2247 if (icon->dock != dock && ex_x != 0)
2248 return False;
2250 for (i=0; i<dock->max_icons; i++) {
2251 nicon = dock->icon_array[i];
2252 if (nicon && nicon->yindex == ex_y) {
2253 aicon = nicon;
2254 break;
2258 *ret_x = 0;
2260 if (redocking) {
2261 int sig, done, closest;
2263 /* Possible cases when redocking:
2265 * icon dragged out of range of any slot -> false
2266 * icon dragged to range of free slot
2267 * icon dragged to range of same slot
2268 * icon dragged to range of different icon
2270 if (abs(ex_x) > DOCK_DETTACH_THRESHOLD)
2271 return False;
2273 if (ex_y >=0 && (aicon == icon || !aicon)) {
2275 *ret_y = ex_y;
2277 return True;
2280 /* start looking at the upper slot or lower? */
2281 if (ex_y*ICON_SIZE < (req_y + offset - dy))
2282 sig = 1;
2283 else
2284 sig = -1;
2286 closest = -1;
2287 done = 0;
2288 /* look for closest free slot */
2289 for (i=0; i<(DOCK_DETTACH_THRESHOLD+1)*2 && !done; i++) {
2290 int j;
2292 done = 1;
2293 closest = sig*(i/2) + ex_y;
2294 /* check if this slot is used */
2295 if (closest >= 0) {
2296 for (j = 0; j<dock->max_icons; j++) {
2297 if (dock->icon_array[j]
2298 && dock->icon_array[j]->yindex==closest) {
2299 /* slot is used by someone else */
2300 if (dock->icon_array[j]!=icon)
2301 done = 0;
2302 break;
2306 sig = -sig;
2308 if (done && closest >= 0 &&
2309 ((ex_y >= closest && ex_y - closest < DOCK_DETTACH_THRESHOLD+1)
2311 (ex_y < closest && closest - ex_y <= DOCK_DETTACH_THRESHOLD+1))) {
2313 *ret_y = closest;
2315 return True;
2317 } else { /* !redocking */
2319 /* if slot is free and the icon is close enough, return it */
2320 if (!aicon && ex_x==0 && ex_y>=0) {
2321 *ret_y = ex_y;
2322 return True;
2325 } else { /* CLIP */
2326 int neighbours = 0;
2328 for (i=0; i<dock->max_icons; i++) {
2329 nicon = dock->icon_array[i];
2330 if (nicon && nicon->xindex == ex_x && nicon->yindex == ex_y) {
2331 aicon = nicon;
2332 break;
2336 for (i=0; i<dock->max_icons; i++) {
2337 nicon = dock->icon_array[i];
2338 if (nicon && nicon != icon && /* Icon can't be it's own neighbour */
2339 (abs(nicon->xindex - ex_x) <= CLIP_ATTACH_VICINITY &&
2340 abs(nicon->yindex - ex_y) <= CLIP_ATTACH_VICINITY)) {
2341 neighbours = 1;
2342 break;
2346 if (neighbours && (aicon==NULL || (redocking && aicon == icon))) {
2347 *ret_x = ex_x;
2348 *ret_y = ex_y;
2349 return True;
2352 return False;
2355 #define MIN(x, y) ((x) > (y) ? (y) : (x))
2356 #define MAX(x, y) ((x) < (y) ? (y) : (x))
2358 #define ON_SCREEN(x, y, sx, ex, sy, ey) \
2359 ((((x)+ICON_SIZE/2) >= (sx)) && (((y)+ICON_SIZE/2) >= (sy)) && \
2360 (((x) + (ICON_SIZE/2)) <= (ex)) && (((y) + (ICON_SIZE/2)) <= ey))
2364 * returns true if it can find a free slot in the dock,
2365 * in which case it changes x_pos and y_pos accordingly.
2366 * Else returns false.
2368 Bool
2369 wDockFindFreeSlot(WDock *dock, int *x_pos, int *y_pos)
2371 WScreen *scr = dock->screen_ptr;
2372 WAppIcon *btn;
2373 unsigned char *slot_map;
2374 int mwidth;
2375 int r;
2376 int x, y;
2377 int i, done = False;
2378 int corner;
2379 int sx=0, sy=0, ex=scr->scr_width, ey=scr->scr_height;
2382 /* if the dock is full */
2383 if (dock->icon_count >= dock->max_icons) {
2384 return False;
2387 if (!wPreferences.flags.nodock && scr->dock) {
2388 if (scr->dock->on_right_side)
2389 ex -= ICON_SIZE + DOCK_EXTRA_SPACE;
2390 else
2391 sx += ICON_SIZE + DOCK_EXTRA_SPACE;
2394 if (ex < dock->x_pos)
2395 ex = dock->x_pos;
2396 if (sx > dock->x_pos+ICON_SIZE)
2397 sx = dock->x_pos+ICON_SIZE;
2398 #define C_NONE 0
2399 #define C_NW 1
2400 #define C_NE 2
2401 #define C_SW 3
2402 #define C_SE 4
2404 /* check if clip is in a corner */
2405 if (dock->type==WM_CLIP) {
2406 if (dock->x_pos < 1 && dock->y_pos < 1)
2407 corner = C_NE;
2408 else if (dock->x_pos < 1 && dock->y_pos >= (ey-ICON_SIZE))
2409 corner = C_SE;
2410 else if (dock->x_pos >= (ex-ICON_SIZE)&& dock->y_pos >= (ey-ICON_SIZE))
2411 corner = C_SW;
2412 else if (dock->x_pos >= (ex-ICON_SIZE) && dock->y_pos < 1)
2413 corner = C_NW;
2414 else
2415 corner = C_NONE;
2416 } else
2417 corner = C_NONE;
2419 /* If the clip is in the corner, use only slots that are in the border
2420 * of the screen */
2421 if (corner!=C_NONE) {
2422 char *hmap, *vmap;
2423 int hcount, vcount;
2425 hcount = MIN(dock->max_icons, scr->scr_width/ICON_SIZE);
2426 vcount = MIN(dock->max_icons, scr->scr_height/ICON_SIZE);
2427 hmap = wmalloc(hcount+1);
2428 memset(hmap, 0, hcount+1);
2429 vmap = wmalloc(vcount+1);
2430 memset(vmap, 0, vcount+1);
2432 /* mark used positions */
2433 switch (corner) {
2434 case C_NE:
2435 for (i=0; i<dock->max_icons; i++) {
2436 btn = dock->icon_array[i];
2437 if (!btn)
2438 continue;
2440 if (btn->xindex==0 && btn->yindex > 0 && btn->yindex < vcount)
2441 vmap[btn->yindex] = 1;
2442 else if (btn->yindex==0 && btn->xindex>0 && btn->xindex<hcount)
2443 hmap[btn->xindex] = 1;
2445 case C_NW:
2446 for (i=0; i<dock->max_icons; i++) {
2447 btn = dock->icon_array[i];
2448 if (!btn)
2449 continue;
2451 if (btn->xindex==0 && btn->yindex > 0 && btn->yindex < vcount)
2452 vmap[btn->yindex] = 1;
2453 else if (btn->yindex==0 && btn->xindex<0 &&btn->xindex>-hcount)
2454 hmap[-btn->xindex] = 1;
2456 case C_SE:
2457 for (i=0; i<dock->max_icons; i++) {
2458 btn = dock->icon_array[i];
2459 if (!btn)
2460 continue;
2462 if (btn->xindex==0 && btn->yindex < 0 && btn->yindex > -vcount)
2463 vmap[-btn->yindex] = 1;
2464 else if (btn->yindex==0 && btn->xindex>0 && btn->xindex<hcount)
2465 hmap[btn->xindex] = 1;
2467 case C_SW:
2468 default:
2469 for (i=0; i<dock->max_icons; i++) {
2470 btn = dock->icon_array[i];
2471 if (!btn)
2472 continue;
2474 if (btn->xindex==0 && btn->yindex < 0 && btn->yindex > -vcount)
2475 vmap[-btn->yindex] = 1;
2476 else if (btn->yindex==0 && btn->xindex<0 &&btn->xindex>-hcount)
2477 hmap[-btn->xindex] = 1;
2480 x=0; y=0;
2481 done = 0;
2482 /* search a vacant slot */
2483 for (i=1; i<MAX(vcount, hcount); i++) {
2484 if (i < vcount && vmap[i]==0) {
2485 /* found a slot */
2486 x = 0;
2487 y = i;
2488 done = 1;
2489 break;
2490 } else if (i < hcount && hmap[i]==0) {
2491 /* found a slot */
2492 x = i;
2493 y = 0;
2494 done = 1;
2495 break;
2498 free(vmap);
2499 free(hmap);
2500 /* If found a slot, translate and return */
2501 if (done) {
2502 if (corner==C_NW || corner==C_NE) {
2503 *y_pos = y;
2504 } else {
2505 *y_pos = -y;
2507 if (corner==C_NE || corner==C_SE) {
2508 *x_pos = x;
2509 } else {
2510 *x_pos = -x;
2512 return True;
2514 /* else, try to find a slot somewhere else */
2517 /* a map of mwidth x mwidth would be enough if we allowed icons to be
2518 * placed outside of screen */
2519 mwidth = (int)ceil(sqrt(dock->max_icons));
2521 /* In the worst case (the clip is in the corner of the screen),
2522 * the amount of icons that fit in the clip is smaller.
2523 * Double the map to get a safe value.
2525 mwidth += mwidth;
2527 r = (mwidth-1)/2;
2529 slot_map = wmalloc(mwidth*mwidth);
2530 memset(slot_map, 0, mwidth*mwidth);
2532 #define XY2OFS(x,y) (MAX(abs(x),abs(y)) > r) ? 0 : (((y)+r)*(mwidth)+(x)+r)
2534 /* mark used slots in the map. If the slot falls outside the map
2535 * (for example, when all icons are placed in line), ignore them. */
2536 for (i=0; i<dock->max_icons; i++) {
2537 btn = dock->icon_array[i];
2538 if (btn)
2539 slot_map[XY2OFS(btn->xindex, btn->yindex)] = 1;
2541 /* Find closest slot from the center that is free by scanning the
2542 * map from the center to outward in circular passes.
2543 * This will not result in a neat layout, but will be optimal
2544 * in the sense that there will not be holes left.
2546 done = 0;
2547 for (i = 1; i <= r && !done; i++) {
2548 int tx, ty;
2550 /* top and bottom parts of the ring */
2551 for (x = -i; x <= i && !done; x++) {
2552 tx = dock->x_pos + x*ICON_SIZE;
2553 y = -i;
2554 ty = dock->y_pos + y*ICON_SIZE;
2555 if (slot_map[XY2OFS(x,y)]==0
2556 && ON_SCREEN(tx, ty, sx, ex, sy, ey)) {
2557 *x_pos = x;
2558 *y_pos = y;
2559 done = 1;
2560 break;
2562 y = i;
2563 ty = dock->y_pos + y*ICON_SIZE;
2564 if (slot_map[XY2OFS(x,y)]==0
2565 && ON_SCREEN(tx, ty, sx, ex, sy, ey)) {
2566 *x_pos = x;
2567 *y_pos = y;
2568 done = 1;
2569 break;
2572 /* left and right parts of the ring */
2573 for (y = -i+1; y <= i-1; y++) {
2574 ty = dock->y_pos + y*ICON_SIZE;
2575 x = -i;
2576 tx = dock->x_pos + x*ICON_SIZE;
2577 if (slot_map[XY2OFS(x,y)]==0
2578 && ON_SCREEN(tx, ty, sx, ex, sy, ey)) {
2579 *x_pos = x;
2580 *y_pos = y;
2581 done = 1;
2582 break;
2584 x = i;
2585 tx = dock->x_pos + x*ICON_SIZE;
2586 if (slot_map[XY2OFS(x,y)]==0
2587 && ON_SCREEN(tx, ty, sx, ex, sy, ey)) {
2588 *x_pos = x;
2589 *y_pos = y;
2590 done = 1;
2591 break;
2595 free(slot_map);
2596 #undef XY2OFS
2597 return done;
2601 static void
2602 moveDock(WDock *dock, int new_x, int new_y)
2604 WAppIcon *btn;
2605 int i;
2607 dock->x_pos = new_x;
2608 dock->y_pos = new_y;
2609 for (i=0; i<dock->max_icons; i++) {
2610 btn = dock->icon_array[i];
2611 if (btn) {
2612 btn->x_pos = new_x + btn->xindex*ICON_SIZE;
2613 btn->y_pos = new_y + btn->yindex*ICON_SIZE;
2614 XMoveWindow(dpy, btn->icon->core->window, btn->x_pos, btn->y_pos);
2620 static void
2621 swapDock(WDock *dock)
2623 WScreen *scr = dock->screen_ptr;
2624 WAppIcon *btn;
2625 int x, i;
2628 if (dock->on_right_side) {
2629 x = dock->x_pos = scr->scr_width - ICON_SIZE - DOCK_EXTRA_SPACE;
2630 } else {
2631 x = dock->x_pos = DOCK_EXTRA_SPACE;
2634 for (i=0; i<dock->max_icons; i++) {
2635 btn = dock->icon_array[i];
2636 if (btn) {
2637 btn->x_pos = x;
2638 XMoveWindow(dpy, btn->icon->core->window, btn->x_pos, btn->y_pos);
2644 static pid_t
2645 execCommand(WAppIcon *btn, char *command, WSavedState *state)
2647 WScreen *scr = btn->icon->core->screen_ptr;
2648 pid_t pid;
2649 char **argv;
2650 int argc;
2651 char *cmdline;
2653 cmdline = ExpandOptions(scr, command);
2655 if (scr->flags.dnd_data_convertion_status || !cmdline) {
2656 if (cmdline)
2657 free(cmdline);
2658 if (state)
2659 free(state);
2660 return 0;
2663 ParseCommand(cmdline, &argv, &argc);
2665 if (argv==NULL) {
2666 if (cmdline)
2667 free(cmdline);
2668 if (state)
2669 free(state);
2670 return 0;
2673 if ((pid=fork())==0) {
2674 char **args;
2675 int i;
2677 SetupEnvironment(scr);
2679 CloseDescriptors();
2681 #ifdef HAVE_SETPGID
2682 setpgid(0, 0);
2683 #endif
2685 args = malloc(sizeof(char*)*(argc+1));
2686 if (!args)
2687 exit(111);
2688 for (i=0; i<argc; i++) {
2689 args[i] = argv[i];
2691 args[argc] = NULL;
2692 execvp(argv[0], args);
2693 exit(111);
2695 while (argc > 0)
2696 free(argv[--argc]);
2697 free(argv);
2699 if (pid > 0) {
2700 if (!state) {
2701 state = wmalloc(sizeof(WSavedState));
2702 memset(state, 0, sizeof(WSavedState));
2703 state->hidden = -1;
2704 state->miniaturized = -1;
2705 state->shaded = -1;
2706 if (btn->dock == scr->dock)
2707 state->workspace = -1;
2708 else
2709 state->workspace = scr->current_workspace;
2711 wWindowAddSavedState(btn->wm_instance, btn->wm_class, cmdline, pid,
2712 state);
2713 wAddDeathHandler(pid, (WDeathHandler*)trackDeadProcess,
2714 btn->dock);
2715 } else if (state) {
2716 free(state);
2718 free(cmdline);
2719 return pid;
2723 void
2724 wDockHideIcons(WDock *dock)
2726 int i;
2727 WAppIcon *btn;
2729 if (dock==NULL)
2730 return;
2732 btn = dock->icon_array[0];
2734 for (i=1; i<dock->max_icons; i++) {
2735 if (dock->icon_array[i])
2736 XUnmapWindow(dpy, dock->icon_array[i]->icon->core->window);
2738 dock->mapped = 0;
2740 dockIconPaint(btn);
2744 void
2745 wDockShowIcons(WDock *dock)
2747 int i, newlevel;
2748 WAppIcon *btn;
2750 if (dock==NULL)
2751 return;
2753 btn = dock->icon_array[0];
2754 moveDock(dock, btn->x_pos, btn->y_pos);
2756 newlevel = dock->lowered ? WMNormalLevel : WMDockLevel;
2757 ChangeStackingLevel(btn->icon->core, newlevel);
2759 for (i=1; i<dock->max_icons; i++) {
2760 if (dock->icon_array[i]) {
2761 MoveInStackListAbove(dock->icon_array[i]->icon->core,
2762 btn->icon->core);
2763 break;
2767 if (!dock->collapsed) {
2768 for (i=1; i<dock->max_icons; i++) {
2769 if (dock->icon_array[i]) {
2770 XMapWindow(dpy, dock->icon_array[i]->icon->core->window);
2774 dock->mapped = 1;
2776 dockIconPaint(btn);
2780 void
2781 wDockLower(WDock *dock)
2783 int i;
2785 for (i=0; i<dock->max_icons; i++) {
2786 if (dock->icon_array[i])
2787 wLowerFrame(dock->icon_array[i]->icon->core);
2792 void
2793 wDockRaise(WDock *dock)
2795 int i;
2797 for (i=dock->max_icons-1; i>=0; i--) {
2798 if (dock->icon_array[i])
2799 wRaiseFrame(dock->icon_array[i]->icon->core);
2804 void
2805 wDockRaiseLower(WDock *dock)
2807 if (!dock->icon_array[0]->icon->core->stacking->above
2808 ||(dock->icon_array[0]->icon->core->stacking->window_level
2809 !=dock->icon_array[0]->icon->core->stacking->above->stacking->window_level))
2810 wDockLower(dock);
2811 else
2812 wDockRaise(dock);
2816 void
2817 wDockFinishLaunch(WDock *dock, WAppIcon *icon)
2819 icon->launching = 0;
2820 icon->relaunching = 0;
2821 dockIconPaint(icon);
2825 WAppIcon*
2826 wDockFindIconFor(WDock *dock, Window window)
2828 WAppIcon *icon;
2829 int i;
2831 for (i=0; i<dock->max_icons; i++) {
2832 icon = dock->icon_array[i];
2833 if (icon && icon->main_window == window)
2834 return icon;
2836 return NULL;
2840 void
2841 wDockTrackWindowLaunch(WDock *dock, Window window)
2843 WAppIcon *icon;
2844 #ifdef REDUCE_APPICONS
2845 WAppIconAppList *tapplist;
2846 #endif
2847 char *wm_class, *wm_instance;
2848 int i;
2851 if (!PropGetWMClass(window, &wm_class, &wm_instance) ||
2852 (!wm_class && !wm_instance))
2853 return;
2855 for (i=0; i<dock->max_icons; i++) {
2856 icon = dock->icon_array[i];
2857 if (!icon)
2858 continue;
2860 /* kluge. If this does not exist, some windows attach themselves
2861 * to more than one icon. Find out why */
2862 if (icon->main_window == window) {
2863 break;
2865 if ((icon->wm_instance || icon->wm_class)
2866 && (icon->launching
2867 || (dock->screen_ptr->flags.startup && !icon->running))) {
2869 if (icon->wm_instance && wm_instance &&
2870 strcmp(icon->wm_instance, wm_instance)!=0) {
2871 continue;
2873 if (icon->wm_class && wm_class &&
2874 strcmp(icon->wm_class, wm_class)!=0) {
2875 continue;
2878 if (!icon->relaunching) {
2879 WApplication *wapp;
2881 /* Possibly an application that was docked with dockit,
2882 * but the user did not update WMState to indicate that
2883 * it was docked by force */
2884 wapp = wApplicationOf(window);
2885 if (!wapp) {
2886 icon->forced_dock = 1;
2887 icon->running = 0;
2889 if (!icon->forced_dock)
2890 icon->main_window = window;
2891 #ifdef REDUCE_APPICONS
2892 tapplist = wmalloc(sizeof(WAppIconAppList));
2893 memset(tapplist, 0, sizeof(WAppIconAppList));
2894 tapplist->next = icon->applist;
2895 if (icon->applist)
2896 icon->applist->prev = tapplist;
2897 icon->applist = tapplist;
2898 tapplist->wapp = wApplicationOf(window);
2899 icon->num_apps++;
2900 #endif
2902 wDockFinishLaunch(dock, icon);
2903 break;
2907 if (wm_class)
2908 XFree(wm_class);
2909 if (wm_instance)
2910 XFree(wm_instance);
2915 void
2916 wClipUpdateForWorkspaceChange(WScreen *scr, int workspace)
2918 if (!wPreferences.flags.noclip) {
2919 scr->clip_icon->dock = scr->workspaces[workspace]->clip;
2920 if (scr->current_workspace != workspace) {
2921 WDock *old_clip = scr->workspaces[scr->current_workspace]->clip;
2923 wDockHideIcons(old_clip);
2924 if (old_clip->auto_collapse && !old_clip->collapsed)
2925 old_clip->collapsed = 1;
2926 wDockShowIcons(scr->workspaces[workspace]->clip);
2928 if (scr->flags.clip_balloon_mapped)
2929 showClipBalloon(scr->clip_icon->dock, workspace);
2935 static void
2936 trackDeadProcess(pid_t pid, unsigned char status, WDock *dock)
2938 WAppIcon *icon;
2939 int i;
2941 for (i=0; i<dock->max_icons; i++) {
2942 icon = dock->icon_array[i];
2943 if (!icon)
2944 continue;
2946 if (icon->launching && icon->pid == pid) {
2947 if (!icon->relaunching) {
2948 icon->running = 0;
2949 icon->main_window = None;
2951 wDockFinishLaunch(dock, icon);
2952 icon->pid = 0;
2953 if (status==111) {
2954 char msg[PATH_MAX];
2955 #ifdef OFFIX_DND
2956 sprintf(msg, _("Could not execute command \"%s\""),
2957 icon->drop_launch && icon->dnd_command
2958 ? icon->dnd_command : icon->command);
2959 #else
2960 sprintf(msg, _("Could not execute command \"%s\""),
2961 icon->command);
2962 #endif
2963 wMessageDialog(dock->screen_ptr, _("Error"), msg,
2964 _("OK"), NULL, NULL);
2966 break;
2972 static void
2973 toggleLowered(WDock *dock)
2975 WAppIcon *tmp;
2976 int newlevel, i;
2978 /* lower/raise Dock */
2979 if (!dock->lowered) {
2980 newlevel = WMNormalLevel;
2981 dock->lowered = 1;
2982 } else {
2983 newlevel = WMDockLevel;
2984 dock->lowered = 0;
2987 for (i=0; i<dock->max_icons; i++) {
2988 tmp = dock->icon_array[i];
2989 if (!tmp)
2990 continue;
2992 ChangeStackingLevel(tmp->icon->core, newlevel);
2993 if (dock->lowered)
2994 wLowerFrame(tmp->icon->core);
2999 static void
3000 toggleCollapsed(WDock *dock)
3002 if (dock->collapsed) {
3003 dock->collapsed = 0;
3004 wDockShowIcons(dock);
3006 else {
3007 dock->collapsed = 1;
3008 wDockHideIcons(dock);
3013 static void
3014 toggleAutoCollapse(WDock *dock)
3016 dock->auto_collapse = !dock->auto_collapse;
3017 if (dock->auto_collapse_magic) {
3018 WMDeleteTimerHandler(dock->auto_collapse_magic);
3019 dock->auto_collapse_magic = NULL;
3024 static void
3025 openDockMenu(WDock *dock, WAppIcon *aicon, XEvent *event)
3027 WScreen *scr = dock->screen_ptr;
3028 WObjDescriptor *desc;
3029 WMenuEntry *entry;
3030 int index = 0;
3031 int x_pos;
3032 int appIsRunning = aicon->running && aicon->icon && aicon->icon->owner;
3034 if (dock->type == WM_DOCK) {
3035 /* keep on top */
3036 entry = dock->menu->entries[index];
3037 entry->flags.indicator_on = !dock->lowered;
3038 entry->clientdata = dock;
3039 } else {
3040 /* clip options */
3041 if (scr->clip_options)
3042 updateClipOptionsMenu(scr->clip_options, dock);
3044 /* Rename Workspace */
3045 entry = dock->menu->entries[++index];
3046 entry->clientdata = dock;
3048 /* select icon */
3049 entry = dock->menu->entries[++index];
3050 entry->clientdata = aicon;
3051 wMenuSetEnabled(dock->menu, index, aicon!=scr->clip_icon);
3053 /* (un)select all icons */
3054 entry = dock->menu->entries[++index];
3055 entry->clientdata = aicon;
3056 wMenuSetEnabled(dock->menu, index, dock->icon_count > 1);
3058 /* keep icon(s) */
3059 entry = dock->menu->entries[++index];
3060 entry->clientdata = aicon;
3061 wMenuSetEnabled(dock->menu, index, dock->icon_count > 1);
3063 /* this is the workspace submenu part */
3064 if (scr->clip_submenu)
3065 updateWorkspaceMenu(scr->clip_submenu, aicon);
3066 index++;
3068 /* remove icon(s) */
3069 entry = dock->menu->entries[++index];
3070 entry->clientdata = aicon;
3071 wMenuSetEnabled(dock->menu, index, dock->icon_count > 1);
3073 /* attract icon(s) */
3074 entry = dock->menu->entries[++index];
3075 entry->clientdata = aicon;
3078 /* launch */
3079 entry = dock->menu->entries[++index];
3080 entry->clientdata = aicon;
3081 wMenuSetEnabled(dock->menu, index, aicon->command!=NULL);
3083 /* unhide here */
3084 entry = dock->menu->entries[++index];
3085 entry->clientdata = aicon;
3086 wMenuSetEnabled(dock->menu, index, appIsRunning);
3088 /* hide */
3089 entry = dock->menu->entries[++index];
3090 entry->clientdata = aicon;
3091 wMenuSetEnabled(dock->menu, index, appIsRunning);
3093 /* settings */
3094 entry = dock->menu->entries[++index];
3095 entry->clientdata = aicon;
3096 wMenuSetEnabled(dock->menu, index, !aicon->editing
3097 && !wPreferences.flags.noupdates);
3099 /* kill */
3100 entry = dock->menu->entries[++index];
3101 entry->clientdata = aicon;
3102 wMenuSetEnabled(dock->menu, index, appIsRunning);
3104 if (!dock->menu->flags.realized)
3105 wMenuRealize(dock->menu);
3107 if (dock->type == WM_CLIP) {
3108 x_pos = event->xbutton.x_root+2;
3109 } else {
3110 x_pos = dock->on_right_side ?
3111 scr->scr_width - dock->menu->frame->core->width - 2 : 0;
3114 wMenuMapAt(dock->menu, x_pos, event->xbutton.y_root+2, False);
3116 /* allow drag select */
3117 event->xany.send_event = True;
3118 desc = &dock->menu->menu->descriptor;
3119 (*desc->handle_mousedown)(desc, event);
3123 static void
3124 openClipWorkspaceMenu(WScreen *scr, int x, int y)
3126 if (!scr->clip_ws_menu) {
3127 scr->clip_ws_menu = wWorkspaceMenuMake(scr, False);
3129 wWorkspaceMenuUpdate(scr, scr->clip_ws_menu);
3130 wMenuMapAt(scr->clip_ws_menu, x, y, False);
3134 /******************************************************************/
3135 static void
3136 iconDblClick(WObjDescriptor *desc, XEvent *event)
3138 WAppIcon *btn = desc->parent;
3139 WDock *dock = btn->dock;
3140 WApplication *wapp = NULL;
3141 int unhideHere = 0;
3143 #ifdef REDUCE_APPICONS
3144 if ((btn->icon->owner && !(event->xbutton.state & ControlMask)) ||
3145 ((btn->icon->owner == NULL) && (btn->applist != NULL))) {
3146 if (btn->icon->owner == NULL)
3147 btn->icon->owner = btn->applist->wapp->main_window_desc;
3148 #ifdef I_HATE_THIS
3150 #endif
3151 #else
3152 if (btn->icon->owner && !(event->xbutton.state & ControlMask)) {
3153 #endif
3154 wapp = wApplicationOf(btn->icon->owner->main_window);
3156 assert(wapp!=NULL);
3158 unhideHere = (event->xbutton.state & ShiftMask);
3160 /* go to the last workspace that the user worked on the app */
3161 if (!unhideHere) {
3162 wWorkspaceChange(dock->screen_ptr, wapp->last_workspace);
3165 wUnhideApplication(wapp, event->xbutton.button==Button2,
3166 unhideHere);
3168 if (event->xbutton.state & MOD_MASK) {
3169 wHideOtherApplications(btn->icon->owner);
3171 } else {
3172 if (event->xbutton.button==Button1) {
3174 if (event->xbutton.state & MOD_MASK) {
3175 /* raise/lower dock */
3176 toggleLowered(dock);
3177 } else if (btn == dock->screen_ptr->clip_icon) {
3178 if (getClipButton(event->xbutton.x, event->xbutton.y)==CLIP_IDLE)
3179 toggleCollapsed(dock);
3180 else
3181 handleClipChangeWorkspace(dock->screen_ptr, event);
3182 } else if (btn->command) {
3183 if (!btn->launching &&
3184 (!btn->running || (event->xbutton.state & ControlMask))) {
3185 launchDockedApplication(btn);
3193 static void
3194 handleDockMove(WDock *dock, WAppIcon *aicon, XEvent *event)
3196 WScreen *scr = dock->screen_ptr;
3197 int ofs_x=event->xbutton.x, ofs_y=event->xbutton.y;
3198 int x, y;
3199 XEvent ev;
3200 int grabbed = 0, swapped = 0, done;
3201 Pixmap ghost = None;
3202 int superfluous = wPreferences.superfluous; /* we catch it to avoid problems */
3204 #ifdef DEBUG
3205 puts("moving dock");
3206 #endif
3207 if (XGrabPointer(dpy, aicon->icon->core->window, True, ButtonMotionMask
3208 |ButtonReleaseMask|ButtonPressMask, GrabModeAsync,
3209 GrabModeAsync, None, None, CurrentTime) !=GrabSuccess) {
3210 wwarning("pointer grab failed for dock move");
3212 y = 0;
3213 for (x=0; x<dock->max_icons; x++) {
3214 if (dock->icon_array[x]!=NULL &&
3215 dock->icon_array[x]->yindex > y)
3216 y = dock->icon_array[x]->yindex;
3218 y++;
3219 XResizeWindow(dpy, scr->dock_shadow, ICON_SIZE, ICON_SIZE*y);
3221 done = 0;
3222 while (!done) {
3223 WMMaskEvent(dpy, PointerMotionMask|ButtonReleaseMask|ButtonPressMask
3224 |ButtonMotionMask|ExposureMask, &ev);
3225 switch (ev.type) {
3226 case Expose:
3227 WMHandleEvent(&ev);
3228 break;
3230 case MotionNotify:
3231 if (!grabbed) {
3232 if (abs(ofs_x-ev.xmotion.x)>=MOVE_THRESHOLD
3233 || abs(ofs_y-ev.xmotion.y)>=MOVE_THRESHOLD) {
3234 XChangeActivePointerGrab(dpy, ButtonMotionMask
3235 |ButtonReleaseMask|ButtonPressMask,
3236 wCursor[WCUR_MOVE], CurrentTime);
3237 grabbed=1;
3239 break;
3241 if (dock->type == WM_CLIP) {
3242 if (ev.xmotion.x_root - ofs_x < 0) {
3243 x = 0;
3244 } else if (ev.xmotion.x_root - ofs_x + ICON_SIZE >
3245 scr->scr_width) {
3246 x = scr->scr_width - ICON_SIZE;
3247 } else {
3248 x = ev.xmotion.x_root - ofs_x;
3250 if (ev.xmotion.y_root - ofs_y < 0) {
3251 y = 0;
3252 } else if (ev.xmotion.y_root - ofs_y + ICON_SIZE >
3253 scr->scr_height) {
3254 y = scr->scr_height - ICON_SIZE;
3255 } else {
3256 y = ev.xmotion.y_root - ofs_y;
3258 moveDock(dock, x, y);
3260 else {
3261 /* move vertically if pointer is inside the dock*/
3262 if ((dock->on_right_side &&
3263 ev.xmotion.x_root >= dock->x_pos - ICON_SIZE)
3264 || (!dock->on_right_side &&
3265 ev.xmotion.x_root <= dock->x_pos + ICON_SIZE*2)) {
3267 if (ev.xmotion.y_root - ofs_y < 0) {
3268 y = 0;
3269 } else if (ev.xmotion.y_root - ofs_y + ICON_SIZE >
3270 scr->scr_height) {
3271 y = scr->scr_height - ICON_SIZE;
3272 } else {
3273 y = ev.xmotion.y_root - ofs_y;
3275 moveDock(dock, dock->x_pos, y);
3277 /* move horizontally to change sides */
3278 x = ev.xmotion.x_root - ofs_x;
3279 if (!dock->on_right_side) {
3281 /* is on left */
3283 if (ev.xmotion.x_root > dock->x_pos + ICON_SIZE*2) {
3284 XMoveWindow(dpy, scr->dock_shadow, scr->scr_width-ICON_SIZE
3285 -DOCK_EXTRA_SPACE, dock->y_pos);
3286 if (superfluous) {
3287 if (ghost==None) {
3288 ghost = MakeGhostDock(dock, dock->x_pos,
3289 scr->scr_width-ICON_SIZE
3290 -DOCK_EXTRA_SPACE,
3291 dock->y_pos);
3292 XSetWindowBackgroundPixmap(dpy, scr->dock_shadow,
3293 ghost);
3294 XClearWindow(dpy, scr->dock_shadow);
3297 XMapRaised(dpy, scr->dock_shadow);
3298 swapped = 1;
3299 } else {
3300 if (superfluous && ghost!=None) {
3301 XFreePixmap(dpy, ghost);
3302 ghost = None;
3304 XUnmapWindow(dpy, scr->dock_shadow);
3305 swapped = 0;
3307 } else {
3308 /* is on right */
3309 if (ev.xmotion.x_root < dock->x_pos - ICON_SIZE) {
3310 XMoveWindow(dpy, scr->dock_shadow,
3311 DOCK_EXTRA_SPACE, dock->y_pos);
3312 if (superfluous) {
3313 if (ghost==None) {
3314 ghost = MakeGhostDock(dock, dock->x_pos,
3315 DOCK_EXTRA_SPACE, dock->y_pos);
3316 XSetWindowBackgroundPixmap(dpy, scr->dock_shadow,
3317 ghost);
3318 XClearWindow(dpy, scr->dock_shadow);
3321 XMapRaised(dpy, scr->dock_shadow);
3322 swapped = -1;
3323 } else {
3324 XUnmapWindow(dpy, scr->dock_shadow);
3325 swapped = 0;
3326 if (superfluous && ghost!=None) {
3327 XFreePixmap(dpy, ghost);
3328 ghost = None;
3333 break;
3335 case ButtonPress:
3336 break;
3338 case ButtonRelease:
3339 if (ev.xbutton.button != event->xbutton.button)
3340 break;
3341 XUngrabPointer(dpy, CurrentTime);
3342 XUnmapWindow(dpy, scr->dock_shadow);
3343 XResizeWindow(dpy, scr->dock_shadow, ICON_SIZE, ICON_SIZE);
3344 if (dock->type == WM_DOCK) {
3345 if (swapped!=0) {
3346 if (swapped>0)
3347 dock->on_right_side = 1;
3348 else
3349 dock->on_right_side = 0;
3350 swapDock(dock);
3351 wArrangeIcons(scr, False);
3354 done = 1;
3355 break;
3358 if (superfluous) {
3359 if (ghost!=None)
3360 XFreePixmap(dpy, ghost);
3361 XSetWindowBackground(dpy, scr->dock_shadow, scr->white_pixel);
3363 #ifdef DEBUG
3364 puts("End dock move");
3365 #endif
3370 static void
3371 handleIconMove(WDock *dock, WAppIcon *aicon, XEvent *event)
3373 WScreen *scr = dock->screen_ptr;
3374 Window wins[2];
3375 WIcon *icon = aicon->icon;
3376 WDock *dock2 = NULL, *last_dock = dock;
3377 int ondock, grabbed = 0, change_dock = 0, collapsed = 0;
3378 XEvent ev;
3379 int x = aicon->x_pos, y = aicon->y_pos;
3380 int ofs_x = event->xbutton.x, ofs_y = event->xbutton.y;
3381 int shad_x = x, shad_y = y;
3382 int ix = aicon->xindex, iy = aicon->yindex;
3383 int tmp;
3384 Pixmap ghost = None;
3385 Bool docked;
3386 int superfluous = wPreferences.superfluous; /* we catch it to avoid problems */
3388 if (wPreferences.flags.noupdates)
3389 return;
3391 if (XGrabPointer(dpy, icon->core->window, True, ButtonMotionMask
3392 |ButtonReleaseMask|ButtonPressMask, GrabModeAsync,
3393 GrabModeAsync, None, None, CurrentTime) !=GrabSuccess) {
3394 #ifdef DEBUG0
3395 wwarning("pointer grab failed for icon move");
3396 #endif
3399 wRaiseFrame(icon->core);
3401 if (dock == scr->dock && !wPreferences.flags.noclip)
3402 dock2 = scr->workspaces[scr->current_workspace]->clip;
3403 else if (dock != scr->dock && !wPreferences.flags.nodock)
3404 dock2 = scr->dock;
3406 wins[0] = icon->core->window;
3407 wins[1] = scr->dock_shadow;
3408 XRestackWindows(dpy, wins, 2);
3409 XMoveResizeWindow(dpy, scr->dock_shadow, aicon->x_pos, aicon->y_pos,
3410 ICON_SIZE, ICON_SIZE);
3411 if (superfluous) {
3412 if (icon->pixmap!=None)
3413 ghost = MakeGhostIcon(scr, icon->pixmap);
3414 else
3415 ghost = MakeGhostIcon(scr, icon->core->window);
3417 XSetWindowBackgroundPixmap(dpy, scr->dock_shadow, ghost);
3418 XClearWindow(dpy, scr->dock_shadow);
3420 XMapWindow(dpy, scr->dock_shadow);
3422 ondock = 1;
3425 while(1) {
3426 XMaskEvent(dpy, PointerMotionMask|ButtonReleaseMask|ButtonPressMask
3427 |ButtonMotionMask|ExposureMask, &ev);
3428 switch (ev.type) {
3429 case Expose:
3430 WMHandleEvent(&ev);
3431 break;
3433 case MotionNotify:
3434 if (!grabbed) {
3435 if (abs(ofs_x-ev.xmotion.x)>=MOVE_THRESHOLD
3436 || abs(ofs_y-ev.xmotion.y)>=MOVE_THRESHOLD) {
3437 XChangeActivePointerGrab(dpy, ButtonMotionMask
3438 |ButtonReleaseMask|ButtonPressMask,
3439 wCursor[WCUR_MOVE], CurrentTime);
3440 grabbed=1;
3441 } else {
3442 break;
3446 x = ev.xmotion.x_root - ofs_x;
3447 y = ev.xmotion.y_root - ofs_y;
3448 tmp = wDockSnapIcon(dock, aicon, x, y, &ix, &iy, True);
3449 if (tmp && dock2) {
3450 change_dock = 0;
3451 if (last_dock != dock && collapsed) {
3452 last_dock->collapsed = 1;
3453 wDockHideIcons(last_dock);
3454 collapsed = 0;
3456 if (!collapsed && (collapsed = dock->collapsed)) {
3457 dock->collapsed = 0;
3458 wDockShowIcons(dock);
3460 last_dock = dock;
3462 else if (dock2) {
3463 tmp = wDockSnapIcon(dock2, aicon, x, y, &ix, &iy, False);
3464 if (tmp) {
3465 change_dock = 1;
3466 if (last_dock != dock2 && collapsed) {
3467 last_dock->collapsed = 1;
3468 wDockHideIcons(last_dock);
3469 collapsed = 0;
3471 if (!collapsed && (collapsed = dock2->collapsed)) {
3472 dock2->collapsed = 0;
3473 wDockShowIcons(dock2);
3475 last_dock = dock2;
3478 if (aicon->launching
3479 || (aicon->running && !(ev.xmotion.state & MOD_MASK))
3480 || (!aicon->running && tmp)) {
3481 shad_x = last_dock->x_pos + ix*wPreferences.icon_size;
3482 shad_y = last_dock->y_pos + iy*wPreferences.icon_size;
3484 XMoveWindow(dpy, scr->dock_shadow, shad_x, shad_y);
3486 if (!ondock) {
3487 XMapWindow(dpy, scr->dock_shadow);
3488 #if 0
3489 if (!collapsed && (collapsed = last_dock->collapsed)) {
3490 last_dock->collapsed = 0;
3491 wDockShowIcons(last_dock);
3493 #endif
3495 ondock = 1;
3496 } else {
3497 if (ondock) {
3498 XUnmapWindow(dpy, scr->dock_shadow);
3499 #if 0
3500 if (last_dock && collapsed &&
3501 aicon->running && (ev.xmotion.state & MOD_MASK)) {
3502 last_dock->collapsed = 1;
3503 wDockHideIcons(last_dock);
3504 collapsed = 0;
3506 #endif
3508 ondock = 0;
3510 XMoveWindow(dpy, icon->core->window, x, y);
3511 break;
3513 case ButtonPress:
3514 break;
3516 case ButtonRelease:
3517 if (ev.xbutton.button != event->xbutton.button)
3518 break;
3519 XUngrabPointer(dpy, CurrentTime);
3520 if (ondock) {
3521 SlideWindow(icon->core->window, x, y, shad_x, shad_y);
3522 XUnmapWindow(dpy, scr->dock_shadow);
3523 if (!change_dock)
3524 reattachIcon(dock, aicon, ix, iy);
3525 else {
3526 docked = moveIconBetweenDocks(dock, dock2, aicon, ix, iy);
3527 if (!docked) {
3528 /* Slide it back if dock rejected it */
3529 SlideWindow(icon->core->window, x, y, aicon->x_pos,
3530 aicon->y_pos);
3531 reattachIcon(dock, aicon, aicon->xindex,aicon->yindex);
3533 if (last_dock->type==WM_CLIP && last_dock->auto_collapse) {
3534 collapsed = 0;
3537 } else {
3538 aicon->x_pos = x;
3539 aicon->y_pos = y;
3540 if (superfluous) {
3541 if (!aicon->running && !wPreferences.no_animations) {
3542 /* We need to deselect it, even if is deselected in
3543 * wDockDetach(), because else DoKaboom() will fail.
3545 if (aicon->icon->selected)
3546 wIconSelect(aicon->icon);
3547 DoKaboom(scr,aicon->icon->core->window, x, y);
3550 wDockDetach(dock, aicon);
3552 if (collapsed) {
3553 last_dock->collapsed = 1;
3554 wDockHideIcons(last_dock);
3555 collapsed = 0;
3557 if (superfluous) {
3558 if (ghost!=None)
3559 XFreePixmap(dpy, ghost);
3560 XSetWindowBackground(dpy, scr->dock_shadow, scr->white_pixel);
3562 #ifdef DEBUG
3563 puts("End icon move");
3564 #endif
3565 return;
3571 static int
3572 getClipButton(int px, int py)
3574 int pt = (CLIP_BUTTON_SIZE+2)*ICON_SIZE/64;
3576 if (px < 0 || py < 0 || px >= ICON_SIZE || py >= ICON_SIZE)
3577 return CLIP_IDLE;
3579 if (py <= pt-((int)ICON_SIZE-1-px))
3580 return CLIP_FORWARD;
3581 else if (px <= pt-((int)ICON_SIZE-1-py))
3582 return CLIP_REWIND;
3584 return CLIP_IDLE;
3588 static void
3589 handleClipChangeWorkspace(WScreen *scr, XEvent *event)
3591 XEvent ev;
3592 int done, direction, new_ws;
3593 int new_dir;
3594 WDock *clip = scr->clip_icon->dock;
3596 direction = getClipButton(event->xbutton.x, event->xbutton.y);
3598 clip->lclip_button_pushed = direction==CLIP_REWIND;
3599 clip->rclip_button_pushed = direction==CLIP_FORWARD;
3601 wClipIconPaint(scr->clip_icon);
3602 done = 0;
3603 while(!done) {
3604 WMMaskEvent(dpy, ExposureMask|ButtonMotionMask|ButtonReleaseMask
3605 |ButtonPressMask, &ev);
3606 switch (ev.type) {
3607 case Expose:
3608 WMHandleEvent(&ev);
3609 break;
3611 case MotionNotify:
3612 new_dir = getClipButton(ev.xmotion.x, ev.xmotion.y);
3613 if (new_dir != direction) {
3614 direction = new_dir;
3615 clip->lclip_button_pushed = direction==CLIP_REWIND;
3616 clip->rclip_button_pushed = direction==CLIP_FORWARD;
3617 wClipIconPaint(scr->clip_icon);
3619 break;
3621 case ButtonPress:
3622 break;
3624 case ButtonRelease:
3625 if (ev.xbutton.button == event->xbutton.button)
3626 done = 1;
3630 clip->lclip_button_pushed = 0;
3631 clip->rclip_button_pushed = 0;
3633 new_ws = wPreferences.ws_advance || (event->xbutton.state & ControlMask);
3635 if (direction == CLIP_FORWARD) {
3636 if (scr->current_workspace < scr->workspace_count-1)
3637 wWorkspaceChange(scr, scr->current_workspace+1);
3638 else if (new_ws && scr->current_workspace < MAX_WORKSPACES-1)
3639 wWorkspaceChange(scr, scr->current_workspace+1);
3640 else if (wPreferences.ws_cycle)
3641 wWorkspaceChange(scr, 0);
3643 else if (direction == CLIP_REWIND) {
3644 if (scr->current_workspace > 0)
3645 wWorkspaceChange(scr, scr->current_workspace-1);
3646 else if (scr->current_workspace==0 && wPreferences.ws_cycle)
3647 wWorkspaceChange(scr, scr->workspace_count-1);
3650 wClipIconPaint(scr->clip_icon);
3654 static void
3655 iconMouseDown(WObjDescriptor *desc, XEvent *event)
3657 WAppIcon *aicon = desc->parent;
3658 WDock *dock = aicon->dock;
3659 WScreen *scr = aicon->icon->core->screen_ptr;
3661 if (aicon->editing)
3662 return;
3664 scr->last_dock = dock;
3666 if (dock->menu->flags.mapped)
3667 wMenuUnmap(dock->menu);
3669 if (IsDoubleClick(scr, event)) {
3670 /* double-click was not in the main clip icon */
3671 if (dock->type != WM_CLIP || aicon->xindex!=0 || aicon->yindex!=0
3672 || getClipButton(event->xbutton.x, event->xbutton.y)==CLIP_IDLE) {
3673 iconDblClick(desc, event);
3674 return;
3678 if (dock->type == WM_CLIP && scr->flags.clip_balloon_mapped) {
3679 XUnmapWindow(dpy, scr->clip_balloon);
3680 scr->flags.clip_balloon_mapped = 0;
3683 #ifdef DEBUG
3684 puts("handling dock");
3685 #endif
3686 if (event->xbutton.button == Button1) {
3687 if (event->xbutton.state & MOD_MASK)
3688 wDockLower(dock);
3689 else
3690 wDockRaise(dock);
3692 if ((event->xbutton.state & ShiftMask) && aicon!=scr->clip_icon &&
3693 dock->type!=WM_DOCK) {
3694 wIconSelect(aicon->icon);
3695 return;
3698 if (aicon->yindex==0 && aicon->xindex==0) {
3699 if (getClipButton(event->xbutton.x, event->xbutton.y)!=CLIP_IDLE
3700 && dock->type==WM_CLIP)
3701 handleClipChangeWorkspace(scr, event);
3702 else
3703 handleDockMove(dock, aicon, event);
3704 } else
3705 handleIconMove(dock, aicon, event);
3707 } else if (event->xbutton.button==Button2 && dock->type==WM_CLIP &&
3708 aicon->xindex==0 && aicon->yindex==0) {
3709 openClipWorkspaceMenu(scr, event->xbutton.x_root+2,
3710 event->xbutton.y_root+2);
3711 if (scr->clip_ws_menu) {
3712 WMenu *menu;
3713 menu = scr->clip_ws_menu;
3714 desc = &menu->menu->descriptor;
3716 event->xany.send_event = True;
3717 (*desc->handle_mousedown)(desc, event);
3719 } else if (event->xbutton.button == Button3) {
3720 openDockMenu(dock, aicon, event);
3725 static void
3726 showClipBalloon(WDock *dock, int workspace)
3728 int w, h;
3729 int x, y;
3730 WScreen *scr = dock->screen_ptr;
3731 char *text;
3732 Window stack[2];
3734 scr->flags.clip_balloon_mapped = 1;
3735 XMapWindow(dpy, scr->clip_balloon);
3737 text = scr->workspaces[workspace]->name;
3739 w = wTextWidth(scr->clip_title_font->font, text, strlen(text));
3741 h = scr->clip_title_font->height;
3742 XResizeWindow(dpy, scr->clip_balloon, w, h);
3744 x = dock->x_pos + CLIP_BUTTON_SIZE*ICON_SIZE/64;
3745 y = dock->y_pos + ICON_SIZE-scr->clip_title_font->height - 3;
3747 if (x+w > scr->scr_width) {
3748 x = scr->scr_width - w;
3749 if (dock->y_pos + ICON_SIZE + h > scr->scr_height)
3750 y = dock->y_pos - h - 1;
3751 else
3752 y = dock->y_pos + ICON_SIZE;
3753 XRaiseWindow(dpy, scr->clip_balloon);
3754 } else {
3755 stack[0] = scr->clip_icon->icon->core->window;
3756 stack[1] = scr->clip_balloon;
3757 XRestackWindows(dpy, stack, 2);
3759 XMoveWindow(dpy, scr->clip_balloon, x, y);
3760 XSetForeground(dpy, scr->clip_title_gc,
3761 scr->clip_title_pixel[CLIP_NORMAL]);
3762 XClearWindow(dpy, scr->clip_balloon);
3763 wDrawString(scr->clip_balloon, scr->clip_title_font, scr->clip_title_gc,
3764 0, scr->clip_title_font->y, text, strlen(text));
3768 static void
3769 clipEnterNotify(WObjDescriptor *desc, XEvent *event)
3771 WAppIcon *btn = (WAppIcon*)desc->parent;
3772 WDock *dock;
3774 assert(event->type==EnterNotify);
3776 if(desc->parent_type!=WCLASS_DOCK_ICON)
3777 return;
3779 dock = btn->dock;
3780 if (!dock || dock->type!=WM_CLIP)
3781 return;
3783 if (btn->xindex == 0 && btn->yindex == 0)
3784 showClipBalloon(dock, dock->screen_ptr->current_workspace);
3785 else {
3786 if (dock->screen_ptr->flags.clip_balloon_mapped) {
3787 XUnmapWindow(dpy, dock->screen_ptr->clip_balloon);
3788 dock->screen_ptr->flags.clip_balloon_mapped = 0;
3791 if (!dock->auto_collapse)
3792 return;
3794 if (dock->auto_collapse_magic) {
3795 WMDeleteTimerHandler(dock->auto_collapse_magic);
3796 dock->auto_collapse_magic = NULL;
3799 if (dock->collapsed)
3800 toggleCollapsed(dock);
3804 static void
3805 clipLeave(WDock *dock)
3807 if (!dock || dock->type!=WM_CLIP || !dock->auto_collapse)
3808 return;
3810 if (dock->auto_collapse_magic) {
3811 WMDeleteTimerHandler(dock->auto_collapse_magic);
3812 dock->auto_collapse_magic = NULL;
3814 if (!dock->collapsed) {
3815 dock->auto_collapse_magic = WMAddTimerHandler(AUTO_COLLAPSE_DELAY,
3816 clipAutoCollapse,
3817 (void *)dock);
3822 static void
3823 clipLeaveNotify(WObjDescriptor *desc, XEvent *event)
3825 WAppIcon *btn = (WAppIcon*)desc->parent;
3827 assert(event->type==LeaveNotify);
3829 if(desc->parent_type!=WCLASS_DOCK_ICON)
3830 return;
3832 clipLeave(btn->dock);
3836 static void
3837 clipAutoCollapse(void *cdata)
3839 WDock *dock = (WDock *)cdata;
3841 if (dock->type!=WM_CLIP || !dock->auto_collapse)
3842 return;
3844 if (!dock->collapsed && dock->auto_collapse_magic!=NULL) {
3845 toggleCollapsed(dock);
3847 dock->auto_collapse_magic = NULL;