Bug fixes for 0.20.3 pre-release 2
[wmaker-crm.git] / src / dock.c
blob50c0c2f72d546cc05116de3232d5938d37af882d
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)==WDB_OK) {
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 #ifndef STRICTNS
436 wLowerFrame(aicon->icon->core);
437 #endif
438 XMoveWindow(dpy, aicon->icon->core->window,
439 aicon->x_pos, aicon->y_pos);
440 if (!dock->mapped || dock->collapsed)
441 XMapWindow(dpy, aicon->icon->core->window);
443 list_remove_head(&selectedIcons);
446 if (wPreferences.auto_arrange_icons)
447 wArrangeIcons(dock->screen_ptr, True);
451 static void
452 keepIconsCallback(WMenu *menu, WMenuEntry *entry)
454 WAppIcon *clickedIcon = (WAppIcon*)entry->clientdata;
455 WDock *dock;
456 WAppIcon *aicon;
457 LinkedList *selectedIcons;
459 assert(clickedIcon!=NULL);
460 dock = clickedIcon->dock;
462 selectedIcons = getSelected(dock);
464 if (!selectedIcons && clickedIcon!=dock->screen_ptr->clip_icon) {
465 char *command = NULL;
467 if (!clickedIcon->command && !clickedIcon->editing) {
468 clickedIcon->editing = 1;
469 if (wInputDialog(dock->screen_ptr, _("Keep Icon"),
470 _("Type the command used to launch the application"),
471 &command)==WDB_OK) {
472 if (command && (command[0]==0 ||
473 (command[0]=='-' && command[1]==0))) {
474 free(command);
475 command = NULL;
477 clickedIcon->command = command;
478 clickedIcon->editing = 0;
479 } else {
480 clickedIcon->editing = 0;
481 if (command)
482 free(command);
483 return;
487 selectedIcons = list_cons(clickedIcon, NULL);
490 while (selectedIcons) {
491 aicon = selectedIcons->head;
492 if (aicon->icon->selected)
493 wIconSelect(aicon->icon);
494 if (aicon && aicon->attracted && aicon->command) {
495 aicon->attracted = 0;
496 if (aicon->icon->shadowed) {
497 aicon->icon->shadowed = 0;
498 aicon->icon->force_paint = 1;
499 wAppIconPaint(aicon);
502 list_remove_head(&selectedIcons);
509 static void
510 toggleAutoAttractCallback(WMenu *menu, WMenuEntry *entry)
512 WDock *dock = (WDock*)entry->clientdata;
514 assert(entry->clientdata!=NULL);
516 dock->attract_icons = !dock->attract_icons;
517 /*if (!dock->attract_icons)
518 dock->keep_attracted = 0;*/
520 entry->flags.indicator_on = dock->attract_icons;
522 wMenuPaint(menu);
526 static void
527 toggleKeepCallback(WMenu *menu, WMenuEntry *entry)
529 WDock *dock = (WDock*)entry->clientdata;
530 WAppIcon *btn;
531 int i;
533 assert(entry->clientdata!=NULL);
535 dock->keep_attracted = !dock->keep_attracted;
537 if (dock->keep_attracted) {
538 for (i=0; i< dock->max_icons; i++) {
539 btn = dock->icon_array[i];
540 if (btn && btn->attracted && btn->command) {
541 btn->attracted = 0;
542 if (btn->icon->shadowed) {
543 btn->icon->shadowed = 0;
544 btn->icon->force_paint = 1;
545 wAppIconPaint(btn);
551 entry->flags.indicator_on = dock->keep_attracted;
553 wMenuPaint(menu);
557 static void
558 selectCallback(WMenu *menu, WMenuEntry *entry)
560 WAppIcon *icon = (WAppIcon*)entry->clientdata;
562 assert(icon!=NULL);
564 wIconSelect(icon->icon);
566 wMenuPaint(menu);
570 static void
571 colectIconsCallback(WMenu *menu, WMenuEntry *entry)
573 WAppIcon *clickedIcon = (WAppIcon*)entry->clientdata;
574 WDock *clip;
575 WAppIcon *aicon;
576 int x, y, x_pos, y_pos;
578 assert(entry->clientdata!=NULL);
579 clip = clickedIcon->dock;
581 aicon = clip->screen_ptr->app_icon_list;
583 while (aicon) {
584 if (!aicon->docked && wDockFindFreeSlot(clip, &x, &y)) {
585 x_pos = clip->x_pos + x*ICON_SIZE;
586 y_pos = clip->y_pos + y*ICON_SIZE;
587 if (aicon->x_pos != x_pos || aicon->y_pos != y_pos) {
588 #ifdef ANIMATIONS
589 if (wPreferences.no_animations) {
590 XMoveWindow(dpy, aicon->icon->core->window, x_pos, y_pos);
591 } else {
592 SlideWindow(aicon->icon->core->window,
593 aicon->x_pos, aicon->y_pos, x_pos, y_pos);
595 #else
596 XMoveWindow(dpy, aicon->icon->core->window, x_pos, y_pos);
597 #endif /* ANIMATIONS */
599 aicon->attracted = 1;
600 if (!clip->keep_attracted && !aicon->icon->shadowed) {
601 aicon->icon->shadowed = 1;
602 aicon->icon->force_paint = 1;
603 /* We don't do an wAppIconPaint() here because it's in
604 * wDockAttachIcon(). -Dan
607 wDockAttachIcon(clip, aicon, x, y);
608 if (clip->collapsed || !clip->mapped)
609 XUnmapWindow(dpy, aicon->icon->core->window);
611 aicon = aicon->next;
616 static void
617 selectIconsCallback(WMenu *menu, WMenuEntry *entry)
619 WAppIcon *clickedIcon = (WAppIcon*)entry->clientdata;
620 WDock *dock;
621 LinkedList *selectedIcons;
622 WAppIcon *btn;
623 int i;
625 assert(clickedIcon!=NULL);
626 dock = clickedIcon->dock;
628 selectedIcons = getSelected(dock);
630 if (!selectedIcons) {
631 for (i=1; i<dock->max_icons; i++) {
632 btn = dock->icon_array[i];
633 if (btn && !btn->icon->selected) {
634 wIconSelect(btn->icon);
637 } else {
638 while(selectedIcons) {
639 btn = selectedIcons->head;
640 wIconSelect(btn->icon);
641 list_remove_head(&selectedIcons);
645 wMenuPaint(menu);
649 static void
650 toggleCollapsedCallback(WMenu *menu, WMenuEntry *entry)
652 assert(entry->clientdata!=NULL);
654 toggleCollapsed(entry->clientdata);
656 entry->flags.indicator_on = ((WDock*)entry->clientdata)->collapsed;
658 wMenuPaint(menu);
662 static void
663 toggleAutoCollapseCallback(WMenu *menu, WMenuEntry *entry)
665 assert(entry->clientdata!=NULL);
667 toggleAutoCollapse(entry->clientdata);
669 entry->flags.indicator_on = ((WDock*)entry->clientdata)->auto_collapse;
671 wMenuPaint(menu);
675 static void
676 launchCallback(WMenu *menu, WMenuEntry *entry)
678 WAppIcon *btn = (WAppIcon*)entry->clientdata;
680 launchDockedApplication(btn);
684 static void
685 settingsCallback(WMenu *menu, WMenuEntry *entry)
687 WAppIcon *btn = (WAppIcon*)entry->clientdata;
689 if (btn->editing)
690 return;
691 ShowDockAppSettingsPanel(btn);
695 static void
696 hideCallback(WMenu *menu, WMenuEntry *entry)
698 WApplication *wapp;
699 WAppIcon *btn = (WAppIcon*)entry->clientdata;
701 wapp = wApplicationOf(btn->icon->owner->main_window);
703 if (wapp->flags.hidden) {
704 wWorkspaceChange(btn->icon->core->screen_ptr,wapp->last_workspace);
705 wUnhideApplication(wapp, False, False);
706 } else {
707 wHideApplication(wapp);
712 static void
713 unhideHereCallback(WMenu *menu, WMenuEntry *entry)
715 WApplication *wapp;
716 WAppIcon *btn = (WAppIcon*)entry->clientdata;
718 wapp = wApplicationOf(btn->icon->owner->main_window);
720 wUnhideApplication(wapp, False, True);
724 WAppIcon*
725 mainIconCreate(WScreen *scr, int type)
727 WAppIcon *btn;
728 int x_pos;
730 if (type == WM_CLIP) {
731 if (scr->clip_icon)
732 return scr->clip_icon;
733 btn = wAppIconCreateForDock(scr, NULL, "Logo", "WMClip", TILE_CLIP);
734 btn->icon->core->descriptor.handle_expose = clipIconExpose;
735 btn->icon->core->descriptor.handle_enternotify = clipEnterNotify;
736 btn->icon->core->descriptor.handle_leavenotify = clipLeaveNotify;
737 /*x_pos = scr->scr_width - ICON_SIZE*2 - DOCK_EXTRA_SPACE;*/
738 x_pos = 0;
740 else {
741 btn = wAppIconCreateForDock(scr, NULL, "Logo", "WMDock", TILE_NORMAL);
742 x_pos = scr->scr_width - ICON_SIZE - DOCK_EXTRA_SPACE;
745 btn->xindex = 0;
746 btn->yindex = 0;
748 btn->icon->core->descriptor.handle_mousedown = iconMouseDown;
749 btn->icon->core->descriptor.parent_type = WCLASS_DOCK_ICON;
750 btn->icon->core->descriptor.parent = btn;
751 /*ChangeStackingLevel(btn->icon->core, NSDockWindowLevel);*/
752 XMapWindow(dpy, btn->icon->core->window);
753 btn->x_pos = x_pos;
754 btn->y_pos = 0;
755 btn->docked = 1;
756 if (type == WM_CLIP)
757 scr->clip_icon = btn;
759 return btn;
763 static void
764 switchWSCommand(WMenu *menu, WMenuEntry *entry)
766 WAppIcon *btn, *icon = (WAppIcon*) entry->clientdata;
767 WScreen *scr = icon->icon->core->screen_ptr;
768 WDock *src, *dest;
769 LinkedList *selectedIcons;
770 int x, y;
772 if (entry->order == scr->current_workspace)
773 return;
774 src = icon->dock;
775 dest = scr->workspaces[entry->order]->clip;
777 selectedIcons = getSelected(src);
779 if (selectedIcons) {
780 while(selectedIcons) {
781 btn = selectedIcons->head;
782 if (wDockFindFreeSlot(dest, &x, &y)) {
783 moveIconBetweenDocks(src, dest, btn, x, y);
784 XUnmapWindow(dpy, btn->icon->core->window);
786 list_remove_head(&selectedIcons);
788 } else if (icon != scr->clip_icon) {
789 if (wDockFindFreeSlot(dest, &x, &y)) {
790 moveIconBetweenDocks(src, dest, icon, x, y);
791 XUnmapWindow(dpy, icon->icon->core->window);
798 static void
799 launchDockedApplication(WAppIcon *btn)
801 WScreen *scr = btn->icon->core->screen_ptr;
803 if (!btn->launching && btn->command!=NULL) {
804 if (!btn->forced_dock) {
805 btn->relaunching = btn->running;
806 btn->running = 1;
808 if (btn->wm_instance || btn->wm_class) {
809 WWindowAttributes attr;
810 memset(&attr, 0, sizeof(WWindowAttributes));
811 wDefaultFillAttributes(scr, btn->wm_instance, btn->wm_class, &attr,
812 True);
814 if (!attr.no_appicon && !btn->buggy_app)
815 btn->launching = 1;
816 else
817 btn->running = 0;
819 btn->drop_launch = 0;
820 scr->last_dock = btn->dock;
821 btn->pid = execCommand(btn, btn->command, NULL);
822 if (btn->pid>0) {
823 if (btn->buggy_app) {
824 /* give feedback that the app was launched */
825 btn->launching = 1;
826 dockIconPaint(btn);
827 btn->launching = 0;
828 WMAddTimerHandler(200, (WMCallback*)dockIconPaint, btn);
829 } else {
830 dockIconPaint(btn);
832 } else {
833 wwarning(_("could not launch application %s\n"), btn->command);
834 btn->launching = 0;
835 if (!btn->relaunching)
836 btn->running = 0;
843 static void
844 updateWorkspaceMenu(WMenu *menu, WAppIcon *icon)
846 WScreen *scr = menu->frame->screen_ptr;
847 char title[MAX_WORKSPACENAME_WIDTH+1];
848 int i;
850 if (!menu || !icon)
851 return;
853 for (i=0; i<scr->workspace_count; i++) {
854 if (i < menu->entry_no) {
855 if (strcmp(menu->entries[i]->text,scr->workspaces[i]->name)!=0) {
856 free(menu->entries[i]->text);
857 strcpy(title, scr->workspaces[i]->name);
858 menu->entries[i]->text = wstrdup(title);
859 menu->flags.realized = 0;
861 menu->entries[i]->clientdata = (void*)icon;
862 } else {
863 strcpy(title, scr->workspaces[i]->name);
865 wMenuAddCallback(menu, title, switchWSCommand, (void*)icon);
867 menu->flags.realized = 0;
869 if (i == scr->current_workspace) {
870 wMenuSetEnabled(menu, i, False);
871 } else {
872 wMenuSetEnabled(menu, i, True);
876 if (!menu->flags.realized)
877 wMenuRealize(menu);
881 static WMenu*
882 makeWorkspaceMenu(WScreen *scr)
884 WMenu *menu;
886 menu = wMenuCreate(scr, NULL, False);
887 if (!menu)
888 wwarning(_("could not create workspace submenu for Clip menu"));
890 wMenuAddCallback(menu, "", switchWSCommand, (void*)scr->clip_icon);
892 menu->flags.realized = 0;
893 wMenuRealize(menu);
895 return menu;
899 static void
900 updateClipOptionsMenu(WMenu *menu, WDock *dock)
902 WMenuEntry *entry;
903 int index = 0;
905 if (!menu || !dock)
906 return;
908 /* keep on top */
909 entry = menu->entries[index];
910 entry->flags.indicator_on = !dock->lowered;
911 entry->clientdata = dock;
913 /* collapsed */
914 entry = menu->entries[++index];
915 entry->flags.indicator_on = dock->collapsed;
916 entry->clientdata = dock;
918 /* auto-collapse */
919 entry = menu->entries[++index];
920 entry->flags.indicator_on = dock->auto_collapse;
921 entry->clientdata = dock;
923 /* attract icons */
924 entry = menu->entries[++index];
925 entry->flags.indicator_on = dock->attract_icons;
926 entry->clientdata = dock;
928 /* keep attracted icons */
929 entry = menu->entries[++index];
930 entry->flags.indicator_on = dock->keep_attracted;
931 entry->clientdata = dock;
933 menu->flags.realized = 0;
934 wMenuRealize(menu);
938 static WMenu*
939 makeClipOptionsMenu(WScreen *scr)
941 WMenu *menu;
942 WMenuEntry *entry;
944 menu = wMenuCreate(scr, NULL, False);
945 if (!menu) {
946 wwarning(_("could not create options submenu for Clip menu"));
947 return NULL;
950 entry = wMenuAddCallback(menu, _("Keep on top"),
951 toggleLoweredCallback, NULL);
952 entry->flags.indicator = 1;
953 entry->flags.indicator_on = 1;
954 entry->flags.indicator_type = MI_CHECK;
956 entry = wMenuAddCallback(menu, _("Collapsed"),
957 toggleCollapsedCallback, NULL);
958 entry->flags.indicator = 1;
959 entry->flags.indicator_on = 1;
960 entry->flags.indicator_type = MI_CHECK;
962 entry = wMenuAddCallback(menu, _("AutoCollapse"),
963 toggleAutoCollapseCallback, NULL);
964 entry->flags.indicator = 1;
965 entry->flags.indicator_on = 1;
966 entry->flags.indicator_type = MI_CHECK;
968 entry = wMenuAddCallback(menu, _("AutoAttract Icons"),
969 toggleAutoAttractCallback, NULL);
970 entry->flags.indicator = 1;
971 entry->flags.indicator_on = 1;
972 entry->flags.indicator_type = MI_CHECK;
974 entry = wMenuAddCallback(menu, _("Keep Attracted Icons"),
975 toggleKeepCallback, NULL);
976 entry->flags.indicator = 1;
977 entry->flags.indicator_on = 1;
978 entry->flags.indicator_type = MI_CHECK;
980 menu->flags.realized = 0;
981 wMenuRealize(menu);
983 return menu;
987 static WMenu*
988 dockMenuCreate(WScreen *scr, int type)
990 WMenu *menu;
991 WMenuEntry *entry;
993 if (type == WM_CLIP && scr->clip_menu)
994 return scr->clip_menu;
996 menu = wMenuCreate(scr, NULL, False);
997 if (type != WM_CLIP) {
998 entry = wMenuAddCallback(menu, _("Keep on top"),
999 toggleLoweredCallback, NULL);
1000 entry->flags.indicator = 1;
1001 entry->flags.indicator_on = 1;
1002 entry->flags.indicator_type = MI_CHECK;
1003 } else {
1004 entry = wMenuAddCallback(menu, _("Clip Options"), NULL, NULL);
1005 scr->clip_options = makeClipOptionsMenu(scr);
1006 if (scr->clip_options)
1007 wMenuEntrySetCascade(menu, entry, scr->clip_options);
1009 wMenuAddCallback(menu, _("Rename Workspace"), renameCallback, NULL);
1011 wMenuAddCallback(menu, _("(Un)Select Icon"), selectCallback, NULL);
1013 wMenuAddCallback(menu, _("(Un)Select All Icons"), selectIconsCallback,
1014 NULL);
1016 wMenuAddCallback(menu, _("Keep Icon(s)"), keepIconsCallback, NULL);
1018 entry = wMenuAddCallback(menu, _("Move Icon(s) To"), NULL, NULL);
1019 scr->clip_submenu = makeWorkspaceMenu(scr);
1020 if (scr->clip_submenu)
1021 wMenuEntrySetCascade(menu, entry, scr->clip_submenu);
1023 wMenuAddCallback(menu, _("Remove Icon(s)"), removeIconsCallback, NULL);
1025 wMenuAddCallback(menu, _("Attract Icons"), colectIconsCallback, NULL);
1028 wMenuAddCallback(menu, _("Launch"), launchCallback, NULL);
1030 wMenuAddCallback(menu, _("Unhide Here"), unhideHereCallback, NULL);
1032 wMenuAddCallback(menu, _("(Un)Hide"), hideCallback, NULL);
1034 wMenuAddCallback(menu, _("Settings..."), settingsCallback, NULL);
1036 wMenuAddCallback(menu, _("Kill"), killCallback, NULL);
1038 if (type == WM_CLIP)
1039 scr->clip_menu = menu;
1041 return menu;
1045 WDock*
1046 wDockCreate(WScreen *scr, int type)
1048 WDock *dock;
1049 WAppIcon *btn;
1050 int icon_count;
1052 make_keys();
1054 dock = wmalloc(sizeof(WDock));
1055 memset(dock, 0, sizeof(WDock));
1057 if (type == WM_CLIP)
1058 icon_count = CLIP_MAX_ICONS;
1059 else
1060 icon_count = scr->scr_height/wPreferences.icon_size;
1062 dock->icon_array = wmalloc(sizeof(WAppIcon*)*icon_count);
1063 memset(dock->icon_array, 0, sizeof(WAppIcon*)*icon_count);
1065 dock->max_icons = icon_count;
1067 btn = mainIconCreate(scr, type);
1069 btn->dock = dock;
1071 dock->x_pos = btn->x_pos;
1072 dock->y_pos = btn->y_pos;
1073 dock->screen_ptr = scr;
1074 dock->type = type;
1075 dock->icon_count = 1;
1076 dock->on_right_side = 1;
1077 dock->collapsed = 0;
1078 dock->auto_collapse = 0;
1079 dock->auto_collapse_magic = NULL;
1080 dock->attract_icons = 0;
1081 dock->keep_attracted = 0;
1082 dock->lowered = 1;
1083 dock->icon_array[0] = btn;
1084 wRaiseFrame(btn->icon->core);
1085 XMoveWindow(dpy, btn->icon->core->window, btn->x_pos, btn->y_pos);
1087 /* create dock menu */
1088 dock->menu = dockMenuCreate(scr, type);
1090 return dock;
1094 void
1095 wDockDestroy(WDock *dock)
1097 int i;
1098 WAppIcon *aicon;
1100 for (i=(dock->type == WM_CLIP) ? 1 : 0; i<dock->max_icons; i++) {
1101 aicon = dock->icon_array[i];
1102 if (aicon) {
1103 int keepit = aicon->running && wApplicationOf(aicon->main_window);
1104 wDockDetach(dock, aicon);
1105 if (keepit) {
1106 PlaceIcon(dock->screen_ptr, &aicon->x_pos, &aicon->y_pos);
1107 #ifndef STRICTNS
1108 wLowerFrame(aicon->icon->core);
1109 #endif
1110 XMoveWindow(dpy, aicon->icon->core->window,
1111 aicon->x_pos, aicon->y_pos);
1112 if (!dock->mapped || dock->collapsed)
1113 XMapWindow(dpy, aicon->icon->core->window);
1117 if (wPreferences.auto_arrange_icons)
1118 wArrangeIcons(dock->screen_ptr, True);
1119 free(dock->icon_array);
1120 if (dock->menu && dock->type!=WM_CLIP)
1121 wMenuDestroy(dock->menu, True);
1122 free(dock);
1126 void
1127 wClipIconPaint(WAppIcon *aicon)
1129 WScreen *scr = aicon->icon->core->screen_ptr;
1130 WWorkspace *workspace = scr->workspaces[scr->current_workspace];
1131 GC gc;
1132 Window win = aicon->icon->core->window;
1133 int length, nlength;
1134 char *ws_name, ws_number[10];
1135 int ty, tx;
1137 wIconPaint(aicon->icon);
1139 length = strlen(workspace->name);
1140 ws_name = malloc(length + 1);
1141 sprintf(ws_name, "%s", workspace->name);
1142 sprintf(ws_number, "%i", scr->current_workspace + 1);
1143 nlength = strlen(ws_number);
1145 gc = scr->clip_title_gc;
1147 if (!workspace->clip->collapsed)
1148 XSetForeground(dpy, gc, scr->clip_title_pixel[CLIP_NORMAL]);
1149 else
1150 XSetForeground(dpy, gc, scr->clip_title_pixel[CLIP_COLLAPSED]);
1152 ty = ICON_SIZE - scr->clip_title_font->height - 3;
1154 tx = CLIP_BUTTON_SIZE*ICON_SIZE/64;
1156 wDrawString(win, scr->clip_title_font, gc, tx,
1157 ty + scr->clip_title_font->y, ws_name, length);
1159 tx = (ICON_SIZE/2 - wTextWidth(scr->clip_title_font->font, ws_number, nlength))/2;
1161 wDrawString(win, scr->clip_title_font, gc, tx,
1162 scr->clip_title_font->y + 2, ws_number, nlength);
1164 free(ws_name);
1166 if (aicon->launching) {
1167 XFillRectangle(dpy, aicon->icon->core->window, scr->stipple_gc,
1168 0, 0, wPreferences.icon_size, wPreferences.icon_size);
1170 paintClipButtons(aicon, aicon->dock->lclip_button_pushed,
1171 aicon->dock->rclip_button_pushed);
1175 static void
1176 clipIconExpose(WObjDescriptor *desc, XEvent *event)
1178 wClipIconPaint(desc->parent);
1182 static void
1183 dockIconPaint(WAppIcon *btn)
1185 if (btn == btn->icon->core->screen_ptr->clip_icon)
1186 wClipIconPaint(btn);
1187 else
1188 wAppIconPaint(btn);
1192 static proplist_t
1193 make_icon_state(WAppIcon *btn)
1195 proplist_t node = NULL;
1196 proplist_t command, autolaunch, name, forced, host, position, buggy;
1197 char *tmp;
1198 char buffer[64];
1200 if (btn) {
1201 if (!btn->command)
1202 command = PLMakeString("-");
1203 else
1204 command = PLMakeString(btn->command);
1206 autolaunch = btn->auto_launch ? dYes : dNo;
1208 tmp = EscapeWM_CLASS(btn->wm_instance, btn->wm_class);
1210 name = PLMakeString(tmp);
1212 free(tmp);
1214 forced = btn->forced_dock ? dYes : dNo;
1216 buggy = btn->buggy_app ? dYes: dNo;
1218 if (btn == btn->icon->core->screen_ptr->clip_icon)
1219 sprintf(buffer, "%i,%i", btn->x_pos, btn->y_pos);
1220 else
1221 sprintf(buffer, "%hi,%hi", btn->xindex, btn->yindex);
1222 position = PLMakeString(buffer);
1224 node = PLMakeDictionaryFromEntries(dCommand, command,
1225 dName, name,
1226 dAutoLaunch, autolaunch,
1227 dForced, forced,
1228 dBuggyApplication, buggy,
1229 dPosition, position,
1230 NULL);
1231 PLRelease(command);
1232 PLRelease(name);
1233 PLRelease(position);
1234 #ifdef OFFIX_DND
1235 if (btn->dnd_command) {
1236 command = PLMakeString(btn->dnd_command);
1237 PLInsertDictionaryEntry(node, dDropCommand, command);
1238 PLRelease(command);
1240 #endif /* OFFIX_DND */
1242 if (btn->client_machine && btn->remote_start) {
1243 host = PLMakeString(btn->client_machine);
1244 PLInsertDictionaryEntry(node, dHost, host);
1245 PLRelease(host);
1249 return node;
1253 static proplist_t
1254 dockSaveState(WDock *dock)
1256 int i;
1257 proplist_t icon_info;
1258 proplist_t list=NULL, dock_state=NULL;
1259 proplist_t value;
1260 char buffer[256];
1262 list = PLMakeArrayFromElements(NULL);
1264 for (i=(dock->type==WM_DOCK ? 0 : 1); i<dock->max_icons; i++) {
1265 WAppIcon *btn = dock->icon_array[i];
1267 if (!btn || (btn->attracted && !dock->keep_attracted))
1268 continue;
1270 if ((icon_info = make_icon_state(dock->icon_array[i]))) {
1271 list = PLAppendArrayElement(list, icon_info);
1272 PLRelease(icon_info);
1276 dock_state = PLMakeDictionaryFromEntries(dApplications, list, NULL);
1278 PLRelease(list);
1280 if (dock->type == WM_DOCK) {
1281 sprintf(buffer, "%i,%i", (dock->on_right_side ? -ICON_SIZE : 0),
1282 dock->y_pos);
1283 value = PLMakeString(buffer);
1284 PLInsertDictionaryEntry(dock_state, dPosition, value);
1285 PLRelease(value);
1288 value = (dock->lowered ? dYes : dNo);
1289 PLInsertDictionaryEntry(dock_state, dLowered, value);
1291 if (dock->type == WM_CLIP) {
1292 value = (dock->collapsed ? dYes : dNo);
1293 PLInsertDictionaryEntry(dock_state, dCollapsed, value);
1295 value = (dock->auto_collapse ? dYes : dNo);
1296 PLInsertDictionaryEntry(dock_state, dAutoCollapse, value);
1298 value = (dock->attract_icons ? dYes : dNo);
1299 PLInsertDictionaryEntry(dock_state, dAutoAttractIcons, value);
1301 value = (dock->keep_attracted ? dYes : dNo);
1302 PLInsertDictionaryEntry(dock_state, dKeepAttracted, value);
1305 return dock_state;
1309 void
1310 wDockSaveState(WScreen *scr)
1312 proplist_t dock_state;
1314 dock_state = dockSaveState(scr->dock);
1316 PLInsertDictionaryEntry(scr->session_state, dDock, dock_state);
1318 PLRelease(dock_state);
1322 void
1323 wClipSaveState(WScreen *scr)
1325 proplist_t clip_state;
1327 clip_state = make_icon_state(scr->clip_icon);
1329 PLInsertDictionaryEntry(scr->session_state, dClip, clip_state);
1331 PLRelease(clip_state);
1335 proplist_t
1336 wClipSaveWorkspaceState(WScreen *scr, int workspace)
1338 return dockSaveState(scr->workspaces[workspace]->clip);
1342 static WAppIcon*
1343 restore_icon_state(WScreen *scr, proplist_t info, int type, int index)
1345 WAppIcon *aicon;
1346 char *wclass, *winstance;
1347 proplist_t cmd, value;
1348 char *command;
1351 cmd = PLGetDictionaryEntry(info, dCommand);
1352 if (!cmd || !PLIsString(cmd)) {
1353 return NULL;
1356 /* parse window name */
1357 value = PLGetDictionaryEntry(info, dName);
1358 if (!value)
1359 return NULL;
1361 ParseWindowName(value, &winstance, &wclass, "dock");
1363 if (!winstance && !wclass) {
1364 return NULL;
1367 /* get commands */
1369 if (cmd)
1370 command = wstrdup(PLGetString(cmd));
1371 else
1372 command = NULL;
1374 if (!command || strcmp(command, "-")==0) {
1375 if (command)
1376 free(command);
1377 if (wclass)
1378 free(wclass);
1379 if (winstance)
1380 free(winstance);
1382 return NULL;
1385 aicon = wAppIconCreateForDock(scr, command, winstance, wclass,
1386 TILE_NORMAL);
1387 if (wclass)
1388 free(wclass);
1389 if (winstance)
1390 free(winstance);
1392 aicon->icon->core->descriptor.handle_mousedown = iconMouseDown;
1393 if (type == WM_CLIP) {
1394 aicon->icon->core->descriptor.handle_enternotify = clipEnterNotify;
1395 aicon->icon->core->descriptor.handle_leavenotify = clipLeaveNotify;
1397 aicon->icon->core->descriptor.parent_type = WCLASS_DOCK_ICON;
1398 aicon->icon->core->descriptor.parent = aicon;
1401 #ifdef OFFIX_DND
1402 cmd = PLGetDictionaryEntry(info, dDropCommand);
1403 if (cmd)
1404 aicon->dnd_command = wstrdup(PLGetString(cmd));
1405 #endif
1407 /* check auto launch */
1408 value = PLGetDictionaryEntry(info, dAutoLaunch);
1410 aicon->auto_launch = 0;
1411 if (value) {
1412 if (PLIsString(value)) {
1413 if (strcasecmp(PLGetString(value), "YES")==0)
1414 aicon->auto_launch = 1;
1415 } else {
1416 wwarning(_("bad value in docked icon state info %s"),
1417 PLGetString(dAutoLaunch));
1421 /* check if it wasn't normally docked */
1422 value = PLGetDictionaryEntry(info, dForced);
1424 aicon->forced_dock = 0;
1425 if (value) {
1426 if (PLIsString(value)) {
1427 if (strcasecmp(PLGetString(value), "YES")==0)
1428 aicon->forced_dock = 1;
1429 } else {
1430 wwarning(_("bad value in docked icon state info %s"),
1431 PLGetString(dForced));
1435 /* check if we can rely on the stuff in the app */
1436 value = PLGetDictionaryEntry(info, dBuggyApplication);
1438 aicon->buggy_app = 0;
1439 if (value) {
1440 if (PLIsString(value)) {
1441 if (strcasecmp(PLGetString(value), "YES")==0)
1442 aicon->buggy_app = 1;
1443 } else {
1444 wwarning(_("bad value in docked icon state info %s"),
1445 PLGetString(dBuggyApplication));
1449 /* get position in the dock */
1450 value = PLGetDictionaryEntry(info, dPosition);
1451 if (value && PLIsString(value)) {
1452 if (sscanf(PLGetString(value), "%hi,%hi", &aicon->xindex,
1453 &aicon->yindex)!=2)
1454 wwarning(_("bad value in docked icon state info %s"),
1455 PLGetString(dPosition));
1457 /* check position sanity */
1458 /* incomplete section! */
1459 if (type == WM_DOCK) {
1460 aicon->xindex = 0;
1461 if (aicon->yindex < 0)
1462 wwarning(_("bad value in docked icon position %i,%i"),
1463 aicon->xindex, aicon->yindex);
1465 } else {
1466 aicon->yindex = index;
1467 aicon->xindex = 0;
1470 aicon->running = 0;
1471 aicon->docked = 1;
1473 return aicon;
1477 #define COMPLAIN(key) wwarning(_("bad value in dock state info:%s"), key)
1480 WAppIcon*
1481 wClipRestoreState(WScreen *scr, proplist_t clip_state)
1483 WAppIcon *icon;
1484 proplist_t value;
1487 icon = mainIconCreate(scr, WM_CLIP);
1489 if (!clip_state)
1490 return icon;
1491 else
1492 PLRetain(clip_state);
1494 /* restore position */
1496 value = PLGetDictionaryEntry(clip_state, dPosition);
1498 if (value) {
1499 if (!PLIsString(value))
1500 COMPLAIN("Position");
1501 else {
1502 if (sscanf(PLGetString(value), "%i,%i", &icon->x_pos,
1503 &icon->y_pos)!=2)
1504 COMPLAIN("Position");
1506 /* check position sanity */
1507 if (icon->y_pos < 0)
1508 icon->y_pos = 0;
1509 else if (icon->y_pos > scr->scr_height-ICON_SIZE)
1510 icon->y_pos = scr->scr_height-ICON_SIZE;
1512 if (icon->x_pos < 0)
1513 icon->x_pos = 0;
1514 else if (icon->x_pos > scr->scr_width-ICON_SIZE)
1515 icon->x_pos = scr->scr_width-ICON_SIZE;
1519 #ifdef OFFIX_DND
1520 value = PLGetDictionaryEntry(clip_state, dDropCommand);
1521 if (value && PLIsString(value))
1522 icon->dnd_command = wstrdup(PLGetString(value));
1523 #endif
1525 PLRelease(clip_state);
1527 return icon;
1531 WDock*
1532 wDockRestoreState(WScreen *scr, proplist_t dock_state, int type)
1534 WDock *dock;
1535 proplist_t apps;
1536 proplist_t value;
1537 WAppIcon *aicon, *old_top;
1538 int count, i;
1541 dock = wDockCreate(scr, type);
1543 if (!dock_state)
1544 return dock;
1546 if (dock_state)
1547 PLRetain(dock_state);
1550 /* restore position */
1552 value = PLGetDictionaryEntry(dock_state, dPosition);
1554 if (value) {
1555 if (!PLIsString(value))
1556 COMPLAIN("Position");
1557 else {
1558 if (sscanf(PLGetString(value), "%i,%i", &dock->x_pos,
1559 &dock->y_pos)!=2)
1560 COMPLAIN("Position");
1562 /* check position sanity */
1563 if (dock->y_pos < 0)
1564 dock->y_pos = 0;
1565 else if (dock->y_pos > scr->scr_height-ICON_SIZE)
1566 dock->y_pos = scr->scr_height-ICON_SIZE;
1568 /* This is no more needed. ??? */
1569 if (type == WM_CLIP) {
1570 if (dock->x_pos < 0)
1571 dock->x_pos = 0;
1572 else if (dock->x_pos > scr->scr_width-ICON_SIZE)
1573 dock->x_pos = scr->scr_width-ICON_SIZE;
1575 else {
1576 if (dock->x_pos >= 0) {
1577 dock->x_pos = DOCK_EXTRA_SPACE;
1578 dock->on_right_side = 0;
1579 } else {
1580 dock->x_pos = scr->scr_width - DOCK_EXTRA_SPACE - ICON_SIZE;
1581 dock->on_right_side = 1;
1587 /* restore lowered/raised state */
1589 dock->lowered = 0;
1591 value = PLGetDictionaryEntry(dock_state, dLowered);
1593 if (value) {
1594 if (!PLIsString(value))
1595 COMPLAIN("Lowered");
1596 else {
1597 if (strcasecmp(PLGetString(value), "YES")==0)
1598 dock->lowered = 1;
1603 /* restore collapsed state */
1605 dock->collapsed = 0;
1607 value = PLGetDictionaryEntry(dock_state, dCollapsed);
1609 if (value) {
1610 if (!PLIsString(value))
1611 COMPLAIN("Collapsed");
1612 else {
1613 if (strcasecmp(PLGetString(value), "YES")==0)
1614 dock->collapsed = 1;
1619 /* restore auto-collapsed state */
1621 value = PLGetDictionaryEntry(dock_state, dAutoCollapse);
1623 if (value) {
1624 if (!PLIsString(value))
1625 COMPLAIN("AutoCollapse");
1626 else {
1627 if (strcasecmp(PLGetString(value), "YES")==0) {
1628 dock->auto_collapse = 1;
1629 dock->collapsed = 1;
1635 /* restore attract icons state */
1637 dock->attract_icons = 0;
1639 value = PLGetDictionaryEntry(dock_state, dAutoAttractIcons);
1641 if (value) {
1642 if (!PLIsString(value))
1643 COMPLAIN("AutoAttractIcons");
1644 else {
1645 if (strcasecmp(PLGetString(value), "YES")==0)
1646 dock->attract_icons = 1;
1651 /* restore keep attracted icons state */
1653 dock->keep_attracted = 0;
1655 value = PLGetDictionaryEntry(dock_state, dKeepAttracted);
1657 if (value) {
1658 if (!PLIsString(value))
1659 COMPLAIN("KeepAttracted");
1660 else {
1661 if (strcasecmp(PLGetString(value), "YES")==0)
1662 dock->keep_attracted = 1;
1667 /* application list */
1669 apps = PLGetDictionaryEntry(dock_state, dApplications);
1671 if (!apps) {
1672 goto finish;
1675 count = PLGetNumberOfElements(apps);
1677 if (count==0)
1678 goto finish;
1680 old_top = dock->icon_array[0];
1682 /* dock->icon_count is set to 1 when dock is created.
1683 * Since Clip is already restored, we want to keep it so for clip,
1684 * but for dock we may change the default top tile, so we set it to 0.
1686 if (type == WM_DOCK)
1687 dock->icon_count = 0;
1689 for (i=0; i<count; i++) {
1690 if (dock->icon_count >= dock->max_icons) {
1691 wwarning(_("there are too many icons stored in dock. Ignoring what doesn't fit"));
1692 break;
1695 value = PLGetArrayElement(apps, i);
1696 aicon = restore_icon_state(scr, value, type, dock->icon_count);
1698 dock->icon_array[dock->icon_count] = aicon;
1700 if (aicon) {
1701 aicon->dock = dock;
1702 aicon->x_pos = dock->x_pos + (aicon->xindex*ICON_SIZE);
1703 aicon->y_pos = dock->y_pos + (aicon->yindex*ICON_SIZE);
1705 if (dock->lowered)
1706 ChangeStackingLevel(aicon->icon->core, WMNormalWindowLevel);
1707 else
1708 ChangeStackingLevel(aicon->icon->core, WMDockWindowLevel);
1710 wCoreConfigure(aicon->icon->core, aicon->x_pos, aicon->y_pos,
1711 0, 0);
1713 if (!dock->collapsed)
1714 XMapWindow(dpy, aicon->icon->core->window);
1715 wRaiseFrame(aicon->icon->core);
1717 dock->icon_count++;
1718 } else if (dock->icon_count==0 && type==WM_DOCK)
1719 dock->icon_count++;
1722 /* if the first icon is not defined, use the default */
1723 if (dock->icon_array[0]==NULL) {
1724 /* update default icon */
1725 old_top->x_pos = dock->x_pos;
1726 old_top->y_pos = dock->y_pos;
1727 if (dock->lowered)
1728 ChangeStackingLevel(old_top->icon->core, WMNormalWindowLevel);
1729 else
1730 ChangeStackingLevel(old_top->icon->core, WMDockWindowLevel);
1731 dock->icon_array[0] = old_top;
1732 XMoveWindow(dpy, old_top->icon->core->window, dock->x_pos, dock->y_pos);
1733 /* we don't need to increment dock->icon_count here because it was
1734 * incremented in the loop above.
1736 } else if (old_top!=dock->icon_array[0]) {
1737 if (old_top == scr->clip_icon)
1738 scr->clip_icon = dock->icon_array[0];
1739 wAppIconDestroy(old_top);
1742 finish:
1743 if (dock_state)
1744 PLRelease(dock_state);
1746 return dock;
1751 void
1752 wDockLaunchWithState(WDock *dock, WAppIcon *btn, WSavedState *state)
1754 if (btn && btn->command && !btn->running && !btn->launching) {
1756 btn->drop_launch = 0;
1758 btn->pid = execCommand(btn, btn->command, state);
1760 if (btn->pid>0) {
1761 if (!btn->forced_dock && !btn->buggy_app) {
1762 btn->launching = 1;
1763 dockIconPaint(btn);
1766 } else {
1767 free(state);
1772 void
1773 wDockDoAutoLaunch(WDock *dock, int workspace)
1775 WAppIcon *btn;
1776 WSavedState *state;
1777 int i;
1779 for (i=0; i < dock->max_icons; i++) {
1780 btn = dock->icon_array[i];
1781 if (!btn || !btn->auto_launch)
1782 continue;
1784 state = wmalloc(sizeof(WSavedState));
1785 memset(state, 0, sizeof(WSavedState));
1786 state->workspace = workspace;
1787 /* TODO: this is klugy and is very difficult to understand
1788 * what's going on. Try to clean up */
1789 wDockLaunchWithState(dock, btn, state);
1793 #ifdef REDUCE_APPICONS
1794 void
1795 wDockSimulateLaunch(WDock *dock, WAppIcon *btn)
1797 if ((btn == NULL) || (dock == NULL))
1798 return;
1800 if (!btn->running) {
1801 if ((btn->icon->owner == NULL) && (btn->applist))
1802 btn->icon->owner = btn->applist->wapp->main_window_desc;
1803 if (!btn->forced_dock)
1804 btn->launching = 1;
1805 dockIconPaint(btn);
1806 wusleep(5000);
1809 #endif
1811 #ifdef OFFIX_DND
1812 static WDock*
1813 findDock(WScreen *scr, XEvent *event, int *icon_pos)
1815 WDock *dock;
1816 int i;
1818 *icon_pos = -1;
1819 if ((dock = scr->dock)!=NULL) {
1820 for (i=0; i<dock->max_icons; i++) {
1821 if (dock->icon_array[i]
1822 && dock->icon_array[i]->icon->core->window==event->xclient.window) {
1823 *icon_pos = i;
1824 break;
1828 if (*icon_pos<0 && (dock = scr->workspaces[scr->current_workspace]->clip)!=NULL) {
1829 for (i=0; i<dock->max_icons; i++) {
1830 if (dock->icon_array[i]
1831 && dock->icon_array[i]->icon->core->window==event->xclient.window) {
1832 *icon_pos = i;
1833 break;
1837 if(*icon_pos>=0)
1838 return dock;
1839 return NULL;
1844 wDockReceiveDNDDrop(WScreen *scr, XEvent *event)
1846 WDock *dock;
1847 WAppIcon *btn;
1848 int icon_pos;
1850 dock = findDock(scr, event, &icon_pos);
1851 if (!dock)
1852 return False;
1855 * Return True if the drop was on an application icon window.
1856 * In this case, let the ClientMessage handler redirect the
1857 * message to the app.
1859 if (dock->icon_array[icon_pos]->icon->icon_win!=None)
1860 return True;
1862 if (dock->icon_array[icon_pos]->dnd_command!=NULL) {
1863 scr->flags.dnd_data_convertion_status = 0;
1865 btn = dock->icon_array[icon_pos];
1867 if (!btn->forced_dock) {
1868 btn->relaunching = btn->running;
1869 btn->running = 1;
1871 if (btn->wm_instance || btn->wm_class) {
1872 WWindowAttributes attr;
1873 memset(&attr, 0, sizeof(WWindowAttributes));
1874 wDefaultFillAttributes(btn->icon->core->screen_ptr,
1875 btn->wm_instance,
1876 btn->wm_class, &attr, True);
1878 if (!attr.no_appicon)
1879 btn->launching = 1;
1880 else
1881 btn->running = 0;
1884 btn->drop_launch = 1;
1885 scr->last_dock = dock;
1886 btn->pid = execCommand(btn, btn->dnd_command, NULL);
1887 if (btn->pid>0) {
1888 dockIconPaint(btn);
1889 } else {
1890 btn->launching = 0;
1891 if (!btn->relaunching) {
1892 btn->running = 0;
1896 return False;
1898 #endif /* OFFIX_DND */
1902 Bool
1903 wDockAttachIcon(WDock *dock, WAppIcon *icon, int x, int y)
1905 WWindow *wwin;
1906 char **argv;
1907 int argc;
1908 int index;
1910 wwin = icon->icon->owner;
1911 if (icon->command==NULL) {
1912 icon->editing = 0;
1913 if (XGetCommand(dpy, wwin->client_win, &argv, &argc) && argc>0) {
1915 icon->command = FlattenStringList(argv, argc);
1916 XFreeStringList(argv);
1917 } else {
1918 char *command=NULL;
1920 /* icon->forced_dock = 1;*/
1921 if (!icon->attracted || dock->type!=WM_CLIP || dock->keep_attracted) {
1922 icon->editing = 1;
1923 if (wInputDialog(dock->screen_ptr, _("Dock Icon"),
1924 _("Type the command used to launch the application"),
1925 &command)==WDB_OK) {
1926 if (command && (command[0]==0 ||
1927 (command[0]=='-' && command[1]==0))) {
1928 free(command);
1929 command = NULL;
1931 icon->command = command;
1932 icon->editing = 0;
1933 } else {
1934 icon->editing = 0;
1935 if (command)
1936 free(command);
1937 /* If the target is the dock, reject the icon. If
1938 * the target is the clip, make it an attracted icon
1940 if (dock->type==WM_CLIP) {
1941 icon->attracted = 1;
1942 if (!icon->icon->shadowed) {
1943 icon->icon->shadowed = 1;
1944 icon->icon->force_paint = 1;
1946 } else {
1947 return False;
1952 } else {
1953 icon->editing = 0;
1956 for (index=1; index<dock->max_icons; index++)
1957 if (dock->icon_array[index] == NULL)
1958 break;
1959 /* if (index == dock->max_icons)
1960 return; */
1962 assert(index < dock->max_icons);
1964 dock->icon_array[index] = icon;
1965 icon->yindex = y;
1966 icon->xindex = x;
1968 icon->x_pos = dock->x_pos + x*ICON_SIZE;
1969 icon->y_pos = dock->y_pos + y*ICON_SIZE;
1971 dock->icon_count++;
1973 icon->running = 1;
1974 icon->launching = 0;
1975 icon->docked = 1;
1976 icon->dock = dock;
1977 icon->icon->core->descriptor.handle_mousedown = iconMouseDown;
1978 if (dock->type == WM_CLIP) {
1979 icon->icon->core->descriptor.handle_enternotify = clipEnterNotify;
1980 icon->icon->core->descriptor.handle_leavenotify = clipLeaveNotify;
1982 icon->icon->core->descriptor.parent_type = WCLASS_DOCK_ICON;
1983 icon->icon->core->descriptor.parent = icon;
1985 MoveInStackListUnder(dock->icon_array[index-1]->icon->core,
1986 icon->icon->core);
1987 wAppIconMove(icon, icon->x_pos, icon->y_pos);
1988 wAppIconPaint(icon);
1990 if (wPreferences.auto_arrange_icons)
1991 wArrangeIcons(dock->screen_ptr, True);
1993 return True;
1997 void
1998 reattachIcon(WDock *dock, WAppIcon *icon, int x, int y)
2000 int index;
2002 for(index=1; index<dock->max_icons; index++) {
2003 if(dock->icon_array[index] == icon)
2004 break;
2006 assert(index < dock->max_icons);
2008 icon->yindex = y;
2009 icon->xindex = x;
2011 icon->x_pos = dock->x_pos + x*ICON_SIZE;
2012 icon->y_pos = dock->y_pos + y*ICON_SIZE;
2016 Bool
2017 moveIconBetweenDocks(WDock *src, WDock *dest, WAppIcon *icon, int x, int y)
2019 WWindow *wwin;
2020 char **argv;
2021 int argc;
2022 int index;
2024 if (dest == NULL)
2025 return False;
2027 wwin = icon->icon->owner;
2030 * For the moment we can't do this if we move icons in Clip from one
2031 * workspace to other, because if we move two or more icons without
2032 * command, the dialog box will not be able to tell us to which of the
2033 * moved icons it applies. -Dan
2035 if ((dest->type==WM_DOCK /*|| dest->keep_attracted*/) && icon->command==NULL) {
2036 if (XGetCommand(dpy, wwin->client_win, &argv, &argc) && argc>0) {
2038 icon->command = FlattenStringList(argv, argc);
2039 XFreeStringList(argv);
2040 } else {
2041 char *command=NULL;
2043 icon->editing = 1;
2044 /* icon->forced_dock = 1;*/
2045 if (wInputDialog(src->screen_ptr, _("Dock Icon"),
2046 _("Type the command used to launch the application"),
2047 &command)==WDB_OK) {
2048 if (command && (command[0]==0 ||
2049 (command[0]=='-' && command[1]==0))) {
2050 free(command);
2051 command = NULL;
2053 icon->command = command;
2054 } else {
2055 icon->editing = 0;
2056 if (command)
2057 free(command);
2058 return False;
2060 icon->editing = 0;
2064 for(index=1; index<src->max_icons; index++) {
2065 if(src->icon_array[index] == icon)
2066 break;
2068 assert(index < src->max_icons);
2070 src->icon_array[index] = NULL;
2071 src->icon_count--;
2073 for(index=1; index<dest->max_icons; index++) {
2074 if(dest->icon_array[index] == NULL)
2075 break;
2077 /* if (index == dest->max_icons)
2078 return; */
2080 assert(index < dest->max_icons);
2082 dest->icon_array[index] = icon;
2083 icon->dock = dest;
2085 /* deselect the icon */
2086 if (icon->icon->selected)
2087 wIconSelect(icon->icon);
2089 if (dest->type == WM_DOCK) {
2090 icon->icon->core->descriptor.handle_enternotify = NULL;
2091 icon->icon->core->descriptor.handle_leavenotify = NULL;
2092 } else {
2093 icon->icon->core->descriptor.handle_enternotify = clipEnterNotify;
2094 icon->icon->core->descriptor.handle_leavenotify = clipLeaveNotify;
2097 /* set it to be kept when moving to dock, or to a clip that keep the
2098 * attracted icons.
2099 * Unless the icon does not have a command set
2101 if (icon->command && (dest->type==WM_DOCK || dest->keep_attracted)) {
2102 icon->attracted = 0;
2103 if (icon->icon->shadowed) {
2104 icon->icon->shadowed = 0;
2105 icon->icon->force_paint = 1;
2109 if (src->auto_collapse)
2110 clipLeave(src);
2112 icon->yindex = y;
2113 icon->xindex = x;
2115 icon->x_pos = dest->x_pos + x*ICON_SIZE;
2116 icon->y_pos = dest->y_pos + y*ICON_SIZE;
2118 dest->icon_count++;
2120 MoveInStackListUnder(dest->icon_array[index-1]->icon->core,
2121 icon->icon->core);
2122 wAppIconPaint(icon);
2124 return True;
2127 void
2128 wDockDetach(WDock *dock, WAppIcon *icon)
2130 int index;
2132 /* make the settings panel be closed */
2133 if (icon->panel) {
2134 DestroyDockAppSettingsPanel(icon->panel);
2137 icon->docked = 0;
2138 icon->dock = NULL;
2139 icon->attracted = 0;
2140 if (icon->icon->shadowed) {
2141 icon->icon->shadowed = 0;
2142 icon->icon->force_paint = 1;
2145 /* deselect the icon */
2146 if (icon->icon->selected)
2147 wIconSelect(icon->icon);
2149 if (icon->command) {
2150 free(icon->command);
2151 icon->command = NULL;
2153 #ifdef OFFIX_DND
2154 if (icon->dnd_command) {
2155 free(icon->dnd_command);
2156 icon->dnd_command = NULL;
2158 #endif
2160 for (index=1; index<dock->max_icons; index++)
2161 if (dock->icon_array[index] == icon)
2162 break;
2163 assert(index < dock->max_icons);
2164 dock->icon_array[index] = NULL;
2165 icon->yindex = -1;
2166 icon->xindex = -1;
2167 dock->icon_count--;
2169 /* if the dock is not attached to an application or
2170 * the the application did not set the approriate hints yet,
2171 * destroy the icon */
2172 #ifdef REDUCE_APPICONS
2173 if ((icon->num_apps == 0) && (!icon->running || !wApplicationOf(icon->main_window)) )
2174 #else
2175 if (!icon->running || !wApplicationOf(icon->main_window))
2176 #endif
2177 wAppIconDestroy(icon);
2178 else {
2179 icon->icon->core->descriptor.handle_mousedown = appIconMouseDown;
2180 icon->icon->core->descriptor.handle_enternotify = NULL;
2181 icon->icon->core->descriptor.handle_leavenotify = NULL;
2182 icon->icon->core->descriptor.parent_type = WCLASS_APPICON;
2183 icon->icon->core->descriptor.parent = icon;
2184 ChangeStackingLevel(icon->icon->core, WMNormalWindowLevel);
2185 wAppIconPaint(icon);
2186 if (wPreferences.auto_arrange_icons) {
2187 wArrangeIcons(dock->screen_ptr, True);
2190 if (dock->auto_collapse)
2191 clipLeave(dock);
2196 * returns the closest Dock slot index for the passed
2197 * coordinates.
2199 * Returns False if icon can't be docked.
2202 wDockSnapIcon(WDock *dock, WAppIcon *icon, int req_x, int req_y,
2203 int *ret_x, int *ret_y, int redocking)
2205 WScreen *scr = dock->screen_ptr;
2206 int dx, dy;
2207 int ex_x, ex_y;
2208 int i, offset = ICON_SIZE/2;
2209 WAppIcon *aicon = NULL;
2210 WAppIcon *nicon = NULL;
2213 dx = dock->x_pos;
2214 dy = dock->y_pos;
2216 /* if the dock is full */
2217 if (!redocking &&
2218 (dock->icon_count >= dock->max_icons)) {
2219 return False;
2222 /* exact position */
2223 if (req_y < dy)
2224 ex_y = (req_y - offset - dy)/ICON_SIZE;
2225 else
2226 ex_y = (req_y + offset - dy)/ICON_SIZE;
2228 if (req_x < dx)
2229 ex_x = (req_x - offset - dx)/ICON_SIZE;
2230 else
2231 ex_x = (req_x + offset - dx)/ICON_SIZE;
2233 /* check if the icon is outside the screen boundaries */
2234 if (dx + ex_x*ICON_SIZE < -ICON_SIZE+2 ||
2235 dx + ex_x*ICON_SIZE > scr->scr_width-1 ||
2236 dy + ex_y*ICON_SIZE < -ICON_SIZE+2 ||
2237 dy + ex_y*ICON_SIZE > scr->scr_height-1)
2238 return False;
2240 if (dock->type == WM_DOCK) {
2241 for (i=0; i<dock->max_icons; i++) {
2242 nicon = dock->icon_array[i];
2243 if (nicon && nicon->yindex == ex_y) {
2244 aicon = nicon;
2245 break;
2249 *ret_x = 0;
2251 if (redocking) {
2252 int sig, done, closest;
2254 /* Possible cases when redocking:
2256 * icon dragged out of range of any slot -> false
2257 * icon dragged to range of free slot
2258 * icon dragged to range of same slot
2259 * icon dragged to range of different icon
2261 if (abs(ex_x) > DOCK_DETTACH_THRESHOLD)
2262 return False;
2264 if (ex_y >=0 && (aicon == icon || !aicon)) {
2266 *ret_y = ex_y;
2268 return True;
2271 /* start looking at the upper slot or lower? */
2272 if (ex_y*ICON_SIZE < (req_y + offset - dy))
2273 sig = 1;
2274 else
2275 sig = -1;
2277 closest = -1;
2278 done = 0;
2279 /* look for closest free slot */
2280 for (i=0; i<(DOCK_DETTACH_THRESHOLD+1)*2 && !done; i++) {
2281 int j;
2283 done = 1;
2284 closest = sig*(i/2) + ex_y;
2285 /* check if this slot is used */
2286 if (closest >= 0) {
2287 for (j = 0; j<dock->max_icons; j++) {
2288 if (dock->icon_array[j]
2289 && dock->icon_array[j]->yindex==closest) {
2290 /* slot is used by someone else */
2291 if (dock->icon_array[j]!=icon)
2292 done = 0;
2293 break;
2297 sig = -sig;
2299 if (done && closest >= 0 &&
2300 ((ex_y >= closest && ex_y - closest < DOCK_DETTACH_THRESHOLD+1)
2302 (ex_y < closest && closest - ex_y <= DOCK_DETTACH_THRESHOLD+1))) {
2304 *ret_y = closest;
2306 return True;
2308 } else { /* !redocking */
2310 /* if slot is free and the icon is close enough, return it */
2311 if (!aicon && ex_x==0 && ex_y>=0) {
2312 *ret_y = ex_y;
2313 return True;
2316 } else { /* CLIP */
2317 int neighbours = 0;
2319 for (i=0; i<dock->max_icons; i++) {
2320 nicon = dock->icon_array[i];
2321 if (nicon && nicon->xindex == ex_x && nicon->yindex == ex_y) {
2322 aicon = nicon;
2323 break;
2327 for (i=0; i<dock->max_icons; i++) {
2328 nicon = dock->icon_array[i];
2329 if (nicon && nicon != icon && /* Icon can't be it's own neighbour */
2330 (abs(nicon->xindex - ex_x) <= CLIP_ATTACH_VICINITY &&
2331 abs(nicon->yindex - ex_y) <= CLIP_ATTACH_VICINITY)) {
2332 neighbours = 1;
2333 break;
2336 if ((!redocking && neighbours && !aicon) ||
2337 (redocking && neighbours && (aicon == icon || !aicon))) {
2338 *ret_x = ex_x;
2339 *ret_y = ex_y;
2340 return True;
2343 return False;
2346 #define MIN(x, y) ((x) > (y) ? (y) : (x))
2347 #define MAX(x, y) ((x) < (y) ? (y) : (x))
2349 #define ON_SCREEN(x, y, sx, ex, sy, ey) \
2350 ((((x)+ICON_SIZE/2) >= (sx)) && (((y)+ICON_SIZE/2) >= (sy)) && \
2351 (((x) + (ICON_SIZE/2)) <= (ex)) && (((y) + (ICON_SIZE/2)) <= ey))
2355 * returns true if it can find a free slot in the dock,
2356 * in which case it changes x_pos and y_pos accordingly.
2357 * Else returns false.
2360 wDockFindFreeSlot(WDock *dock, int *x_pos, int *y_pos)
2362 WScreen *scr = dock->screen_ptr;
2363 WAppIcon *btn;
2364 unsigned char *slot_map;
2365 int mwidth;
2366 int r;
2367 int x, y;
2368 int i, done = False;
2369 int corner;
2370 int sx=0, sy=0, ex=scr->scr_width, ey=scr->scr_height;
2372 /* if the dock is full */
2373 if (dock->icon_count >= dock->max_icons) {
2374 return False;
2377 if (!wPreferences.flags.nodock && scr->dock) {
2378 if (scr->dock->on_right_side)
2379 ex -= ICON_SIZE + DOCK_EXTRA_SPACE;
2380 else
2381 sx += ICON_SIZE + DOCK_EXTRA_SPACE;
2384 if (ex < dock->x_pos)
2385 ex = dock->x_pos;
2386 if (sx > dock->x_pos+ICON_SIZE)
2387 sx = dock->x_pos+ICON_SIZE;
2388 #define C_NONE 0
2389 #define C_NW 1
2390 #define C_NE 2
2391 #define C_SW 3
2392 #define C_SE 4
2394 /* check if clip is in a corner */
2395 if (dock->type==WM_CLIP) {
2396 if (dock->x_pos < 1 && dock->y_pos < 1)
2397 corner = C_NE;
2398 else if (dock->x_pos < 1 && dock->y_pos >= (ey-ICON_SIZE))
2399 corner = C_SE;
2400 else if (dock->x_pos >= (ex-ICON_SIZE)&& dock->y_pos >= (ey-ICON_SIZE))
2401 corner = C_SW;
2402 else if (dock->x_pos >= (ex-ICON_SIZE) && dock->y_pos < 1)
2403 corner = C_NW;
2404 else
2405 corner = C_NONE;
2406 } else
2407 corner = C_NONE;
2409 /* If the clip is in the corner, use only slots that are in the border
2410 * of the screen */
2411 if (corner!=C_NONE) {
2412 char *hmap, *vmap;
2413 int hcount, vcount;
2415 hcount = MIN(dock->max_icons, scr->scr_width/ICON_SIZE);
2416 vcount = MIN(dock->max_icons, scr->scr_height/ICON_SIZE);
2417 hmap = wmalloc(hcount+1);
2418 memset(hmap, 0, hcount+1);
2419 vmap = wmalloc(vcount+1);
2420 memset(vmap, 0, vcount+1);
2422 /* mark used positions */
2423 switch (corner) {
2424 case C_NE:
2425 for (i=0; i<dock->max_icons; i++) {
2426 btn = dock->icon_array[i];
2427 if (!btn)
2428 continue;
2430 if (btn->xindex==0 && btn->yindex > 0 && btn->yindex < vcount)
2431 vmap[btn->yindex] = 1;
2432 else if (btn->yindex==0 && btn->xindex>0 && btn->xindex<hcount)
2433 hmap[btn->xindex] = 1;
2435 case C_NW:
2436 for (i=0; i<dock->max_icons; i++) {
2437 btn = dock->icon_array[i];
2438 if (!btn)
2439 continue;
2441 if (btn->xindex==0 && btn->yindex > 0 && btn->yindex < vcount)
2442 vmap[btn->yindex] = 1;
2443 else if (btn->yindex==0 && btn->xindex<0 &&btn->xindex>-hcount)
2444 hmap[-btn->xindex] = 1;
2446 case C_SE:
2447 for (i=0; i<dock->max_icons; i++) {
2448 btn = dock->icon_array[i];
2449 if (!btn)
2450 continue;
2452 if (btn->xindex==0 && btn->yindex < 0 && btn->yindex > -vcount)
2453 vmap[-btn->yindex] = 1;
2454 else if (btn->yindex==0 && btn->xindex>0 && btn->xindex<hcount)
2455 hmap[btn->xindex] = 1;
2457 case C_SW:
2458 default:
2459 for (i=0; i<dock->max_icons; i++) {
2460 btn = dock->icon_array[i];
2461 if (!btn)
2462 continue;
2464 if (btn->xindex==0 && btn->yindex < 0 && btn->yindex > -vcount)
2465 vmap[-btn->yindex] = 1;
2466 else if (btn->yindex==0 && btn->xindex<0 &&btn->xindex>-hcount)
2467 hmap[-btn->xindex] = 1;
2470 x=0; y=0;
2471 done = 0;
2472 /* search a vacant slot */
2473 for (i=1; i<MAX(vcount, hcount); i++) {
2474 if (i < vcount && vmap[i]==0) {
2475 /* found a slot */
2476 x = 0;
2477 y = i;
2478 done = 1;
2479 break;
2480 } else if (i < hcount && hmap[i]==0) {
2481 /* found a slot */
2482 x = i;
2483 y = 0;
2484 done = 1;
2485 break;
2488 free(vmap);
2489 free(hmap);
2490 /* If found a slot, translate and return */
2491 if (done) {
2492 if (corner==C_NW || corner==C_NE) {
2493 *y_pos = y;
2494 } else {
2495 *y_pos = -y;
2497 if (corner==C_NE || corner==C_SE) {
2498 *x_pos = x;
2499 } else {
2500 *x_pos = -x;
2502 return True;
2504 /* else, try to find a slot somewhere else */
2507 /* a map of mwidth x mwidth would be enough if we allowed icons to be
2508 * placed outside of screen */
2509 mwidth = (int)ceil(sqrt(dock->max_icons));
2511 /* In the worst case (the clip is in the corner of the screen),
2512 * the amount of icons that fit in the clip is smaller.
2513 * Double the map to get a safe value.
2515 mwidth += mwidth;
2517 r = (mwidth-1)/2;
2519 slot_map = wmalloc(mwidth*mwidth);
2520 memset(slot_map, 0, mwidth*mwidth);
2522 #define XY2OFS(x,y) (MAX(abs(x),abs(y)) > r) ? 0 : (((y)+r)*(mwidth)+(x)+r)
2524 /* mark used slots in the map. If the slot falls outside the map
2525 * (for example, when all icons are placed in line), ignore them. */
2526 for (i=0; i<dock->max_icons; i++) {
2527 btn = dock->icon_array[i];
2528 if (btn)
2529 slot_map[XY2OFS(btn->xindex, btn->yindex)] = 1;
2531 /* Find closest slot from the center that is free by scanning the
2532 * map from the center to outward in circular passes.
2533 * This will not result in a neat layout, but will be optimal
2534 * in the sense that there will not be holes left.
2536 done = 0;
2537 for (i = 1; i <= r && !done; i++) {
2538 int tx, ty;
2540 /* top and bottom parts of the ring */
2541 for (x = -i; x <= i && !done; x++) {
2542 tx = dock->x_pos + x*ICON_SIZE;
2543 y = -i;
2544 ty = dock->y_pos + y*ICON_SIZE;
2545 if (slot_map[XY2OFS(x,y)]==0
2546 && ON_SCREEN(tx, ty, sx, ex, sy, ey)) {
2547 *x_pos = x;
2548 *y_pos = y;
2549 done = 1;
2550 break;
2552 y = i;
2553 ty = dock->y_pos + y*ICON_SIZE;
2554 if (slot_map[XY2OFS(x,y)]==0
2555 && ON_SCREEN(tx, ty, sx, ex, sy, ey)) {
2556 *x_pos = x;
2557 *y_pos = y;
2558 done = 1;
2559 break;
2562 /* left and right parts of the ring */
2563 for (y = -i+1; y <= i-1; y++) {
2564 ty = dock->y_pos + y*ICON_SIZE;
2565 x = -i;
2566 tx = dock->x_pos + x*ICON_SIZE;
2567 if (slot_map[XY2OFS(x,y)]==0
2568 && ON_SCREEN(tx, ty, sx, ex, sy, ey)) {
2569 *x_pos = x;
2570 *y_pos = y;
2571 done = 1;
2572 break;
2574 x = i;
2575 tx = dock->x_pos + x*ICON_SIZE;
2576 if (slot_map[XY2OFS(x,y)]==0
2577 && ON_SCREEN(tx, ty, sx, ex, sy, ey)) {
2578 *x_pos = x;
2579 *y_pos = y;
2580 done = 1;
2581 break;
2585 free(slot_map);
2586 #undef XY2OFS
2587 return done;
2591 static void
2592 moveDock(WDock *dock, int new_x, int new_y)
2594 WAppIcon *btn;
2595 int i;
2597 dock->x_pos = new_x;
2598 dock->y_pos = new_y;
2599 for (i=0; i<dock->max_icons; i++) {
2600 btn = dock->icon_array[i];
2601 if (btn) {
2602 btn->x_pos = new_x + btn->xindex*ICON_SIZE;
2603 btn->y_pos = new_y + btn->yindex*ICON_SIZE;
2604 XMoveWindow(dpy, btn->icon->core->window, btn->x_pos, btn->y_pos);
2610 static void
2611 swapDock(WDock *dock)
2613 WScreen *scr = dock->screen_ptr;
2614 WAppIcon *btn;
2615 int x, i;
2618 if (dock->on_right_side) {
2619 x = dock->x_pos = scr->scr_width - ICON_SIZE - DOCK_EXTRA_SPACE;
2620 } else {
2621 x = dock->x_pos = DOCK_EXTRA_SPACE;
2624 for (i=0; i<dock->max_icons; i++) {
2625 btn = dock->icon_array[i];
2626 if (btn) {
2627 btn->x_pos = x;
2628 XMoveWindow(dpy, btn->icon->core->window, btn->x_pos, btn->y_pos);
2634 static pid_t
2635 execCommand(WAppIcon *btn, char *command, WSavedState *state)
2637 WScreen *scr = btn->icon->core->screen_ptr;
2638 pid_t pid;
2639 char **argv;
2640 int argc;
2641 char *cmdline;
2643 cmdline = ExpandOptions(scr, command);
2645 if (scr->flags.dnd_data_convertion_status || !cmdline) {
2646 if (cmdline)
2647 free(cmdline);
2648 if (state)
2649 free(state);
2650 return 0;
2653 ParseCommand(cmdline, &argv, &argc);
2655 if (argv==NULL) {
2656 if (cmdline)
2657 free(cmdline);
2658 if (state)
2659 free(state);
2660 return 0;
2663 if ((pid=fork())==0) {
2664 char **args;
2665 int i;
2667 SetupEnvironment(scr);
2669 close(ConnectionNumber(dpy));
2671 #ifdef HAVE_SETPGID
2672 setpgid(0, 0);
2673 #endif
2675 args = malloc(sizeof(char*)*(argc+1));
2676 if (!args)
2677 exit(111);
2678 for (i=0; i<argc; i++) {
2679 args[i] = argv[i];
2681 args[argc] = NULL;
2682 execvp(argv[0], args);
2683 exit(111);
2685 while (argc > 0)
2686 free(argv[--argc]);
2687 free(argv);
2689 if (pid > 0) {
2690 if (!state) {
2691 state = wmalloc(sizeof(WSavedState));
2692 memset(state, 0, sizeof(WSavedState));
2693 state->hidden = -1;
2694 state->miniaturized = -1;
2695 state->shaded = -1;
2696 if (btn->dock == scr->dock)
2697 state->workspace = -1;
2698 else
2699 state->workspace = scr->current_workspace;
2701 wWindowAddSavedState(btn->wm_instance, btn->wm_class, cmdline, pid,
2702 state);
2703 wAddDeathHandler(pid, (WDeathHandler*)trackDeadProcess,
2704 btn->dock);
2705 } else if (state) {
2706 free(state);
2708 free(cmdline);
2709 return pid;
2713 void
2714 wDockHideIcons(WDock *dock)
2716 int i;
2717 WAppIcon *btn;
2719 if (dock==NULL)
2720 return;
2722 btn = dock->icon_array[0];
2724 for (i=1; i<dock->max_icons; i++) {
2725 if (dock->icon_array[i])
2726 XUnmapWindow(dpy, dock->icon_array[i]->icon->core->window);
2728 dock->mapped = 0;
2730 dockIconPaint(btn);
2734 void
2735 wDockShowIcons(WDock *dock)
2737 int i, newlevel;
2738 WAppIcon *btn;
2740 if (dock==NULL)
2741 return;
2743 btn = dock->icon_array[0];
2744 moveDock(dock, btn->x_pos, btn->y_pos);
2746 newlevel = dock->lowered ? WMNormalWindowLevel : WMDockWindowLevel;
2747 ChangeStackingLevel(btn->icon->core, newlevel);
2749 for (i=1; i<dock->max_icons; i++) {
2750 if (dock->icon_array[i]) {
2751 MoveInStackListAbove(dock->icon_array[i]->icon->core,
2752 btn->icon->core);
2753 break;
2757 if (!dock->collapsed) {
2758 for (i=1; i<dock->max_icons; i++) {
2759 if (dock->icon_array[i]) {
2760 XMapWindow(dpy, dock->icon_array[i]->icon->core->window);
2764 dock->mapped = 1;
2766 dockIconPaint(btn);
2770 void
2771 wDockLower(WDock *dock)
2773 int i;
2775 for (i=0; i<dock->max_icons; i++) {
2776 if (dock->icon_array[i])
2777 wLowerFrame(dock->icon_array[i]->icon->core);
2782 void
2783 wDockRaise(WDock *dock)
2785 int i;
2787 for (i=dock->max_icons-1; i>=0; i--) {
2788 if (dock->icon_array[i])
2789 wRaiseFrame(dock->icon_array[i]->icon->core);
2794 void
2795 wDockRaiseLower(WDock *dock)
2797 if (!dock->icon_array[0]->icon->core->stacking->above
2798 ||(dock->icon_array[0]->icon->core->stacking->window_level
2799 !=dock->icon_array[0]->icon->core->stacking->above->stacking->window_level))
2800 wDockLower(dock);
2801 else
2802 wDockRaise(dock);
2806 void
2807 wDockFinishLaunch(WDock *dock, WAppIcon *icon)
2809 icon->launching = 0;
2810 icon->relaunching = 0;
2811 dockIconPaint(icon);
2815 WAppIcon*
2816 wDockFindIconFor(WDock *dock, Window window)
2818 WAppIcon *icon;
2819 int i;
2821 for (i=0; i<dock->max_icons; i++) {
2822 icon = dock->icon_array[i];
2823 if (icon && icon->main_window == window)
2824 return icon;
2826 return NULL;
2830 void
2831 wDockTrackWindowLaunch(WDock *dock, Window window)
2833 WAppIcon *icon;
2834 #ifdef REDUCE_APPICONS
2835 WAppIconAppList *tapplist;
2836 #endif
2837 char *wm_class, *wm_instance;
2838 int i;
2841 if (!PropGetWMClass(window, &wm_class, &wm_instance) ||
2842 (!wm_class && !wm_instance))
2843 return;
2845 for (i=0; i<dock->max_icons; i++) {
2846 icon = dock->icon_array[i];
2847 if (!icon)
2848 continue;
2850 /* kluge. If this does not exist, some windows attach themselves
2851 * to more than one icon. Find out why */
2852 if (icon->main_window == window) {
2853 break;
2855 if ((icon->wm_instance || icon->wm_class)
2856 && (icon->launching
2857 || (dock->screen_ptr->flags.startup && !icon->running))) {
2859 if (icon->wm_instance && wm_instance &&
2860 strcmp(icon->wm_instance, wm_instance)!=0) {
2861 continue;
2863 if (icon->wm_class && wm_class &&
2864 strcmp(icon->wm_class, wm_class)!=0) {
2865 continue;
2868 if (!icon->relaunching) {
2869 WApplication *wapp;
2871 /* Possibly an application that was docked with dockit,
2872 * but the user did not update WMState to indicate that
2873 * it was docked by force */
2874 wapp = wApplicationOf(window);
2875 if (!wapp) {
2876 icon->forced_dock = 1;
2877 icon->running = 0;
2879 if (!icon->forced_dock)
2880 icon->main_window = window;
2881 #ifdef REDUCE_APPICONS
2882 tapplist = wmalloc(sizeof(WAppIconAppList));
2883 memset(tapplist, 0, sizeof(WAppIconAppList));
2884 tapplist->next = icon->applist;
2885 if (icon->applist)
2886 icon->applist->prev = tapplist;
2887 icon->applist = tapplist;
2888 tapplist->wapp = wApplicationOf(window);
2889 icon->num_apps++;
2890 #endif
2892 wDockFinishLaunch(dock, icon);
2893 break;
2897 if (wm_class)
2898 XFree(wm_class);
2899 if (wm_instance)
2900 XFree(wm_instance);
2905 void
2906 wClipUpdateForWorkspaceChange(WScreen *scr, int workspace)
2908 if (!wPreferences.flags.noclip) {
2909 scr->clip_icon->dock = scr->workspaces[workspace]->clip;
2910 if (scr->current_workspace != workspace) {
2911 WDock *old_clip = scr->workspaces[scr->current_workspace]->clip;
2913 wDockHideIcons(old_clip);
2914 if (old_clip->auto_collapse && !old_clip->collapsed)
2915 old_clip->collapsed = 1;
2916 wDockShowIcons(scr->workspaces[workspace]->clip);
2918 if (scr->flags.clip_balloon_mapped)
2919 showClipBalloon(scr->clip_icon->dock, workspace);
2925 static void
2926 trackDeadProcess(pid_t pid, unsigned char status, WDock *dock)
2928 WAppIcon *icon;
2929 int i;
2931 for (i=0; i<dock->max_icons; i++) {
2932 icon = dock->icon_array[i];
2933 if (!icon)
2934 continue;
2936 if (icon->launching && icon->pid == pid) {
2937 if (!icon->relaunching) {
2938 icon->running = 0;
2939 icon->main_window = None;
2941 wDockFinishLaunch(dock, icon);
2942 icon->pid = 0;
2943 if (status==111) {
2944 char msg[PATH_MAX];
2945 #ifdef OFFIX_DND
2946 sprintf(msg, _("Could not execute command \"%s\""),
2947 icon->drop_launch && icon->dnd_command
2948 ? icon->dnd_command : icon->command);
2949 #else
2950 sprintf(msg, _("Could not execute command \"%s\""),
2951 icon->command);
2952 #endif
2953 wMessageDialog(dock->screen_ptr, _("Error"), msg,
2954 _("OK"), NULL, NULL);
2956 break;
2962 static void
2963 toggleLowered(WDock *dock)
2965 WAppIcon *tmp;
2966 int newlevel, i;
2968 /* lower/raise Dock */
2969 if (!dock->lowered) {
2970 newlevel = WMNormalWindowLevel;
2971 dock->lowered = 1;
2972 } else {
2973 newlevel = WMDockWindowLevel;
2974 dock->lowered = 0;
2977 for (i=0; i<dock->max_icons; i++) {
2978 tmp = dock->icon_array[i];
2979 if (!tmp)
2980 continue;
2982 ChangeStackingLevel(tmp->icon->core, newlevel);
2983 if (dock->lowered)
2984 wLowerFrame(tmp->icon->core);
2989 static void
2990 toggleCollapsed(WDock *dock)
2992 if (dock->collapsed) {
2993 dock->collapsed = 0;
2994 wDockShowIcons(dock);
2996 else {
2997 dock->collapsed = 1;
2998 wDockHideIcons(dock);
3003 static void
3004 toggleAutoCollapse(WDock *dock)
3006 dock->auto_collapse = !dock->auto_collapse;
3007 if (dock->auto_collapse_magic) {
3008 WMDeleteTimerHandler(dock->auto_collapse_magic);
3009 dock->auto_collapse_magic = NULL;
3014 static void
3015 openDockMenu(WDock *dock, WAppIcon *aicon, XEvent *event)
3017 WScreen *scr = dock->screen_ptr;
3018 WObjDescriptor *desc;
3019 WMenuEntry *entry;
3020 int index = 0;
3021 int x_pos;
3022 int appIsRunning = aicon->running && aicon->icon && aicon->icon->owner;
3024 if (dock->type == WM_DOCK) {
3025 /* keep on top */
3026 entry = dock->menu->entries[index];
3027 entry->flags.indicator_on = !dock->lowered;
3028 entry->clientdata = dock;
3029 } else {
3030 /* clip options */
3031 if (scr->clip_options)
3032 updateClipOptionsMenu(scr->clip_options, dock);
3034 /* Rename Workspace */
3035 entry = dock->menu->entries[++index];
3036 entry->clientdata = dock;
3038 /* select icon */
3039 entry = dock->menu->entries[++index];
3040 entry->clientdata = aicon;
3041 wMenuSetEnabled(dock->menu, index, aicon!=scr->clip_icon);
3043 /* (un)select all icons */
3044 entry = dock->menu->entries[++index];
3045 entry->clientdata = aicon;
3046 wMenuSetEnabled(dock->menu, index, dock->icon_count > 1);
3048 /* keep icon(s) */
3049 entry = dock->menu->entries[++index];
3050 entry->clientdata = aicon;
3051 wMenuSetEnabled(dock->menu, index, dock->icon_count > 1);
3053 /* this is the workspace submenu part */
3054 if (scr->clip_submenu)
3055 updateWorkspaceMenu(scr->clip_submenu, aicon);
3056 index++;
3058 /* remove icon(s) */
3059 entry = dock->menu->entries[++index];
3060 entry->clientdata = aicon;
3061 wMenuSetEnabled(dock->menu, index, dock->icon_count > 1);
3063 /* attract icon(s) */
3064 entry = dock->menu->entries[++index];
3065 entry->clientdata = aicon;
3068 /* launch */
3069 entry = dock->menu->entries[++index];
3070 entry->clientdata = aicon;
3071 wMenuSetEnabled(dock->menu, index, aicon->command!=NULL);
3073 /* unhide here */
3074 entry = dock->menu->entries[++index];
3075 entry->clientdata = aicon;
3076 wMenuSetEnabled(dock->menu, index, appIsRunning);
3078 /* hide */
3079 entry = dock->menu->entries[++index];
3080 entry->clientdata = aicon;
3081 wMenuSetEnabled(dock->menu, index, appIsRunning);
3083 /* settings */
3084 entry = dock->menu->entries[++index];
3085 entry->clientdata = aicon;
3086 wMenuSetEnabled(dock->menu, index, !aicon->editing);
3088 /* kill */
3089 entry = dock->menu->entries[++index];
3090 entry->clientdata = aicon;
3091 wMenuSetEnabled(dock->menu, index, appIsRunning);
3093 if (!dock->menu->flags.realized)
3094 wMenuRealize(dock->menu);
3096 if (dock->type == WM_CLIP) {
3097 x_pos = event->xbutton.x_root+2;
3098 } else {
3099 x_pos = dock->on_right_side ?
3100 scr->scr_width - dock->menu->frame->core->width - 2 : 0;
3103 wMenuMapAt(dock->menu, x_pos, event->xbutton.y_root+2, False);
3105 /* allow drag select */
3106 event->xany.send_event = True;
3107 desc = &dock->menu->menu->descriptor;
3108 (*desc->handle_mousedown)(desc, event);
3112 static void
3113 openClipWorkspaceMenu(WScreen *scr, int x, int y)
3115 if (!scr->clip_ws_menu) {
3116 scr->clip_ws_menu = wWorkspaceMenuMake(scr, False);
3118 wWorkspaceMenuUpdate(scr, scr->clip_ws_menu);
3119 wMenuMapAt(scr->clip_ws_menu, x, y, False);
3123 /******************************************************************/
3124 static void
3125 iconDblClick(WObjDescriptor *desc, XEvent *event)
3127 WAppIcon *btn = desc->parent;
3128 WDock *dock = btn->dock;
3129 WApplication *wapp = NULL;
3130 int unhideHere = 0;
3132 #ifdef REDUCE_APPICONS
3133 if ((btn->icon->owner && !(event->xbutton.state & ControlMask)) ||
3134 ((btn->icon->owner == NULL) && (btn->applist != NULL))) {
3135 if (btn->icon->owner == NULL)
3136 btn->icon->owner = btn->applist->wapp->main_window_desc;
3137 #ifdef I_HATE_THIS
3139 #endif
3140 #else
3141 if (btn->icon->owner && !(event->xbutton.state & ControlMask)) {
3142 #endif
3143 wapp = wApplicationOf(btn->icon->owner->main_window);
3145 assert(wapp!=NULL);
3147 unhideHere = (event->xbutton.state & ShiftMask);
3149 /* go to the last workspace that the user worked on the app */
3150 if (!unhideHere) {
3151 wWorkspaceChange(dock->screen_ptr, wapp->last_workspace);
3154 wUnhideApplication(wapp, event->xbutton.button==Button2,
3155 unhideHere);
3157 if (event->xbutton.state & MOD_MASK) {
3158 wHideOtherApplications(btn->icon->owner);
3160 } else {
3161 if (event->xbutton.button==Button1) {
3163 if (event->xbutton.state & MOD_MASK) {
3164 /* raise/lower dock */
3165 toggleLowered(dock);
3166 } else if (btn == dock->screen_ptr->clip_icon) {
3167 if (getClipButton(event->xbutton.x, event->xbutton.y)==CLIP_IDLE)
3168 toggleCollapsed(dock);
3169 else
3170 handleClipChangeWorkspace(dock->screen_ptr, event);
3171 } else if (btn->command) {
3172 if (!btn->launching &&
3173 (!btn->running || (event->xbutton.state & ControlMask))) {
3174 launchDockedApplication(btn);
3182 static void
3183 handleDockMove(WDock *dock, WAppIcon *aicon, XEvent *event)
3185 WScreen *scr = dock->screen_ptr;
3186 int ofs_x=event->xbutton.x, ofs_y=event->xbutton.y;
3187 int x, y;
3188 XEvent ev;
3189 int grabbed = 0, swapped = 0, done;
3190 Pixmap ghost = None;
3191 int superfluous = wPreferences.superfluous; /* we catch it to avoid problems */
3193 #ifdef DEBUG
3194 puts("moving dock");
3195 #endif
3196 if (XGrabPointer(dpy, aicon->icon->core->window, True, ButtonMotionMask
3197 |ButtonReleaseMask|ButtonPressMask, GrabModeAsync,
3198 GrabModeAsync, None, None, CurrentTime) !=GrabSuccess) {
3199 wwarning("pointer grab failed for dock move");
3201 y = 0;
3202 for (x=0; x<dock->max_icons; x++) {
3203 if (dock->icon_array[x]!=NULL &&
3204 dock->icon_array[x]->yindex > y)
3205 y = dock->icon_array[x]->yindex;
3207 y++;
3208 XResizeWindow(dpy, scr->dock_shadow, ICON_SIZE, ICON_SIZE*y);
3210 done = 0;
3211 while (!done) {
3212 WMMaskEvent(dpy, PointerMotionMask|ButtonReleaseMask|ButtonPressMask
3213 |ButtonMotionMask|ExposureMask, &ev);
3214 switch (ev.type) {
3215 case Expose:
3216 WMHandleEvent(&ev);
3217 break;
3219 case MotionNotify:
3220 if (!grabbed) {
3221 if (abs(ofs_x-ev.xmotion.x)>=MOVE_THRESHOLD
3222 || abs(ofs_y-ev.xmotion.y)>=MOVE_THRESHOLD) {
3223 XChangeActivePointerGrab(dpy, ButtonMotionMask
3224 |ButtonReleaseMask|ButtonPressMask,
3225 wCursor[WCUR_MOVE], CurrentTime);
3226 grabbed=1;
3228 break;
3230 if (dock->type == WM_CLIP) {
3231 if (ev.xmotion.x_root - ofs_x < 0) {
3232 x = 0;
3233 } else if (ev.xmotion.x_root - ofs_x + ICON_SIZE >
3234 scr->scr_width) {
3235 x = scr->scr_width - ICON_SIZE;
3236 } else {
3237 x = ev.xmotion.x_root - ofs_x;
3239 if (ev.xmotion.y_root - ofs_y < 0) {
3240 y = 0;
3241 } else if (ev.xmotion.y_root - ofs_y + ICON_SIZE >
3242 scr->scr_height) {
3243 y = scr->scr_height - ICON_SIZE;
3244 } else {
3245 y = ev.xmotion.y_root - ofs_y;
3247 moveDock(dock, x, y);
3249 else {
3250 /* move vertically if pointer is inside the dock*/
3251 if ((dock->on_right_side &&
3252 ev.xmotion.x_root >= dock->x_pos - ICON_SIZE)
3253 || (!dock->on_right_side &&
3254 ev.xmotion.x_root <= dock->x_pos + ICON_SIZE*2)) {
3256 if (ev.xmotion.y_root - ofs_y < 0) {
3257 y = 0;
3258 } else if (ev.xmotion.y_root - ofs_y + ICON_SIZE >
3259 scr->scr_height) {
3260 y = scr->scr_height - ICON_SIZE;
3261 } else {
3262 y = ev.xmotion.y_root - ofs_y;
3264 moveDock(dock, dock->x_pos, y);
3266 /* move horizontally to change sides */
3267 x = ev.xmotion.x_root - ofs_x;
3268 if (!dock->on_right_side) {
3270 /* is on left */
3272 if (ev.xmotion.x_root > dock->x_pos + ICON_SIZE*2) {
3273 XMoveWindow(dpy, scr->dock_shadow, scr->scr_width-ICON_SIZE
3274 -DOCK_EXTRA_SPACE, dock->y_pos);
3275 if (superfluous) {
3276 if (ghost==None) {
3277 ghost = MakeGhostDock(dock, dock->x_pos,
3278 scr->scr_width-ICON_SIZE
3279 -DOCK_EXTRA_SPACE,
3280 dock->y_pos);
3281 XSetWindowBackgroundPixmap(dpy, scr->dock_shadow,
3282 ghost);
3283 XClearWindow(dpy, scr->dock_shadow);
3286 XMapRaised(dpy, scr->dock_shadow);
3287 swapped = 1;
3288 } else {
3289 if (superfluous && ghost!=None) {
3290 XFreePixmap(dpy, ghost);
3291 ghost = None;
3293 XUnmapWindow(dpy, scr->dock_shadow);
3294 swapped = 0;
3296 } else {
3297 /* is on right */
3298 if (ev.xmotion.x_root < dock->x_pos - ICON_SIZE) {
3299 XMoveWindow(dpy, scr->dock_shadow,
3300 DOCK_EXTRA_SPACE, dock->y_pos);
3301 if (superfluous) {
3302 if (ghost==None) {
3303 ghost = MakeGhostDock(dock, dock->x_pos,
3304 DOCK_EXTRA_SPACE, dock->y_pos);
3305 XSetWindowBackgroundPixmap(dpy, scr->dock_shadow,
3306 ghost);
3307 XClearWindow(dpy, scr->dock_shadow);
3310 XMapRaised(dpy, scr->dock_shadow);
3311 swapped = -1;
3312 } else {
3313 XUnmapWindow(dpy, scr->dock_shadow);
3314 swapped = 0;
3315 if (superfluous && ghost!=None) {
3316 XFreePixmap(dpy, ghost);
3317 ghost = None;
3322 break;
3324 case ButtonPress:
3325 break;
3327 case ButtonRelease:
3328 if (ev.xbutton.button != event->xbutton.button)
3329 break;
3330 XUngrabPointer(dpy, CurrentTime);
3331 XUnmapWindow(dpy, scr->dock_shadow);
3332 XResizeWindow(dpy, scr->dock_shadow, ICON_SIZE, ICON_SIZE);
3333 if (dock->type == WM_DOCK) {
3334 if (swapped!=0) {
3335 if (swapped>0)
3336 dock->on_right_side = 1;
3337 else
3338 dock->on_right_side = 0;
3339 swapDock(dock);
3340 wArrangeIcons(scr, False);
3343 done = 1;
3344 break;
3347 if (superfluous) {
3348 if (ghost!=None)
3349 XFreePixmap(dpy, ghost);
3350 XSetWindowBackground(dpy, scr->dock_shadow, scr->white_pixel);
3352 #ifdef DEBUG
3353 puts("End dock move");
3354 #endif
3359 static void
3360 handleIconMove(WDock *dock, WAppIcon *aicon, XEvent *event)
3362 WScreen *scr = dock->screen_ptr;
3363 Window wins[2];
3364 WIcon *icon = aicon->icon;
3365 WDock *dock2 = NULL, *last_dock = dock;
3366 int ondock, grabbed = 0, change_dock = 0, collapsed = 0;
3367 XEvent ev;
3368 int x = aicon->x_pos, y = aicon->y_pos;
3369 int ofs_x = event->xbutton.x, ofs_y = event->xbutton.y;
3370 int shad_x = x, shad_y = y;
3371 int ix = aicon->xindex, iy = aicon->yindex;
3372 int tmp;
3373 Pixmap ghost = None;
3374 Bool docked;
3375 int superfluous = wPreferences.superfluous; /* we catch it to avoid problems */
3377 if (XGrabPointer(dpy, icon->core->window, True, ButtonMotionMask
3378 |ButtonReleaseMask|ButtonPressMask, GrabModeAsync,
3379 GrabModeAsync, None, None, CurrentTime) !=GrabSuccess) {
3380 #ifdef DEBUG0
3381 wwarning("pointer grab failed for icon move");
3382 #endif
3385 wRaiseFrame(icon->core);
3387 if (dock == scr->dock && !wPreferences.flags.noclip)
3388 dock2 = scr->workspaces[scr->current_workspace]->clip;
3389 else if (dock != scr->dock && !wPreferences.flags.nodock)
3390 dock2 = scr->dock;
3392 wins[0] = icon->core->window;
3393 wins[1] = scr->dock_shadow;
3394 XRestackWindows(dpy, wins, 2);
3395 XMoveResizeWindow(dpy, scr->dock_shadow, aicon->x_pos, aicon->y_pos,
3396 ICON_SIZE, ICON_SIZE);
3397 if (superfluous) {
3398 if (icon->pixmap!=None)
3399 ghost = MakeGhostIcon(scr, icon->pixmap);
3400 else
3401 ghost = MakeGhostIcon(scr, icon->core->window);
3403 XSetWindowBackgroundPixmap(dpy, scr->dock_shadow, ghost);
3404 XClearWindow(dpy, scr->dock_shadow);
3406 XMapWindow(dpy, scr->dock_shadow);
3408 ondock = 1;
3411 while(1) {
3412 XMaskEvent(dpy, PointerMotionMask|ButtonReleaseMask|ButtonPressMask
3413 |ButtonMotionMask|ExposureMask, &ev);
3414 switch (ev.type) {
3415 case Expose:
3416 WMHandleEvent(&ev);
3417 break;
3419 case MotionNotify:
3420 if (!grabbed) {
3421 if (abs(ofs_x-ev.xmotion.x)>=MOVE_THRESHOLD
3422 || abs(ofs_y-ev.xmotion.y)>=MOVE_THRESHOLD) {
3423 XChangeActivePointerGrab(dpy, ButtonMotionMask
3424 |ButtonReleaseMask|ButtonPressMask,
3425 wCursor[WCUR_MOVE], CurrentTime);
3426 grabbed=1;
3427 } else {
3428 break;
3432 x = ev.xmotion.x_root - ofs_x;
3433 y = ev.xmotion.y_root - ofs_y;
3434 tmp = wDockSnapIcon(dock, aicon, x, y, &ix, &iy, True);
3435 if (tmp && dock2) {
3436 change_dock = 0;
3437 if (last_dock != dock && collapsed) {
3438 last_dock->collapsed = 1;
3439 wDockHideIcons(last_dock);
3440 collapsed = 0;
3442 if (!collapsed && (collapsed = dock->collapsed)) {
3443 dock->collapsed = 0;
3444 wDockShowIcons(dock);
3446 last_dock = dock;
3448 else if (dock2) {
3449 tmp = wDockSnapIcon(dock2, aicon, x, y, &ix, &iy, False);
3450 if (tmp) {
3451 change_dock = 1;
3452 if (last_dock != dock2 && collapsed) {
3453 last_dock->collapsed = 1;
3454 wDockHideIcons(last_dock);
3455 collapsed = 0;
3457 if (!collapsed && (collapsed = dock2->collapsed)) {
3458 dock2->collapsed = 0;
3459 wDockShowIcons(dock2);
3461 last_dock = dock2;
3464 if (aicon->launching
3465 || (aicon->running && !(ev.xmotion.state & MOD_MASK))
3466 || (!aicon->running && tmp)) {
3467 shad_x = last_dock->x_pos + ix*wPreferences.icon_size;
3468 shad_y = last_dock->y_pos + iy*wPreferences.icon_size;
3470 XMoveWindow(dpy, scr->dock_shadow, shad_x, shad_y);
3472 if (!ondock) {
3473 XMapWindow(dpy, scr->dock_shadow);
3474 #if 0
3475 if (!collapsed && (collapsed = last_dock->collapsed)) {
3476 last_dock->collapsed = 0;
3477 wDockShowIcons(last_dock);
3479 #endif
3481 ondock = 1;
3482 } else {
3483 if (ondock) {
3484 XUnmapWindow(dpy, scr->dock_shadow);
3485 #if 0
3486 if (last_dock && collapsed &&
3487 aicon->running && (ev.xmotion.state & MOD_MASK)) {
3488 last_dock->collapsed = 1;
3489 wDockHideIcons(last_dock);
3490 collapsed = 0;
3492 #endif
3494 ondock = 0;
3496 XMoveWindow(dpy, icon->core->window, x, y);
3497 break;
3499 case ButtonPress:
3500 break;
3502 case ButtonRelease:
3503 if (ev.xbutton.button != event->xbutton.button)
3504 break;
3505 XUngrabPointer(dpy, CurrentTime);
3506 if (ondock) {
3507 SlideWindow(icon->core->window, x, y, shad_x, shad_y);
3508 XUnmapWindow(dpy, scr->dock_shadow);
3509 if (!change_dock)
3510 reattachIcon(dock, aicon, ix, iy);
3511 else {
3512 docked = moveIconBetweenDocks(dock, dock2, aicon, ix, iy);
3513 if (!docked) {
3514 /* Slide it back if dock rejected it */
3515 SlideWindow(icon->core->window, x, y, aicon->x_pos,
3516 aicon->y_pos);
3517 reattachIcon(dock, aicon, aicon->xindex,aicon->yindex);
3519 if (last_dock->type==WM_CLIP && last_dock->auto_collapse) {
3520 collapsed = 0;
3523 } else {
3524 aicon->x_pos = x;
3525 aicon->y_pos = y;
3526 if (superfluous) {
3527 if (!aicon->running && !wPreferences.no_animations) {
3528 /* We need to deselect it, even if is deselected in
3529 * wDockDetach(), because else DoKaboom() will fail.
3531 if (aicon->icon->selected)
3532 wIconSelect(aicon->icon);
3533 DoKaboom(scr,aicon->icon->core->window, x, y);
3536 wDockDetach(dock, aicon);
3538 if (collapsed) {
3539 last_dock->collapsed = 1;
3540 wDockHideIcons(last_dock);
3541 collapsed = 0;
3543 if (superfluous) {
3544 if (ghost!=None)
3545 XFreePixmap(dpy, ghost);
3546 XSetWindowBackground(dpy, scr->dock_shadow, scr->white_pixel);
3548 #ifdef DEBUG
3549 puts("End icon move");
3550 #endif
3551 return;
3557 static int
3558 getClipButton(int px, int py)
3560 int pt = (CLIP_BUTTON_SIZE+2)*ICON_SIZE/64;
3562 if (px < 0 || py < 0 || px >= ICON_SIZE || py >= ICON_SIZE)
3563 return CLIP_IDLE;
3565 if (py <= pt-((int)ICON_SIZE-1-px))
3566 return CLIP_FORWARD;
3567 else if (px <= pt-((int)ICON_SIZE-1-py))
3568 return CLIP_REWIND;
3570 return CLIP_IDLE;
3574 static void
3575 handleClipChangeWorkspace(WScreen *scr, XEvent *event)
3577 XEvent ev;
3578 int done, direction, new_ws;
3579 int new_dir;
3580 WDock *clip = scr->clip_icon->dock;
3582 direction = getClipButton(event->xbutton.x, event->xbutton.y);
3584 clip->lclip_button_pushed = direction==CLIP_REWIND;
3585 clip->rclip_button_pushed = direction==CLIP_FORWARD;
3587 wClipIconPaint(scr->clip_icon);
3588 done = 0;
3589 while(!done) {
3590 WMMaskEvent(dpy, ExposureMask|ButtonMotionMask|ButtonReleaseMask
3591 |ButtonPressMask, &ev);
3592 switch (ev.type) {
3593 case Expose:
3594 WMHandleEvent(&ev);
3595 break;
3597 case MotionNotify:
3598 new_dir = getClipButton(ev.xmotion.x, ev.xmotion.y);
3599 if (new_dir != direction) {
3600 direction = new_dir;
3601 clip->lclip_button_pushed = direction==CLIP_REWIND;
3602 clip->rclip_button_pushed = direction==CLIP_FORWARD;
3603 wClipIconPaint(scr->clip_icon);
3605 break;
3607 case ButtonPress:
3608 break;
3610 case ButtonRelease:
3611 if (ev.xbutton.button == event->xbutton.button)
3612 done = 1;
3616 clip->lclip_button_pushed = 0;
3617 clip->rclip_button_pushed = 0;
3619 new_ws = wPreferences.ws_advance || (event->xbutton.state & ControlMask);
3621 if (direction == CLIP_FORWARD) {
3622 if (scr->current_workspace < scr->workspace_count-1)
3623 wWorkspaceChange(scr, scr->current_workspace+1);
3624 else if (new_ws && scr->current_workspace < MAX_WORKSPACES-1)
3625 wWorkspaceChange(scr, scr->current_workspace+1);
3626 else if (wPreferences.ws_cycle)
3627 wWorkspaceChange(scr, 0);
3629 else if (direction == CLIP_REWIND) {
3630 if (scr->current_workspace > 0)
3631 wWorkspaceChange(scr, scr->current_workspace-1);
3632 else if (scr->current_workspace==0 && wPreferences.ws_cycle)
3633 wWorkspaceChange(scr, scr->workspace_count-1);
3636 wClipIconPaint(scr->clip_icon);
3640 static void
3641 iconMouseDown(WObjDescriptor *desc, XEvent *event)
3643 WAppIcon *aicon = desc->parent;
3644 WDock *dock = aicon->dock;
3645 WScreen *scr = aicon->icon->core->screen_ptr;
3647 if (aicon->editing)
3648 return;
3650 scr->last_dock = dock;
3652 if (dock->menu->flags.mapped)
3653 wMenuUnmap(dock->menu);
3655 if (IsDoubleClick(scr, event)) {
3656 /* double-click was not in the main clip icon */
3657 if (dock->type != WM_CLIP || aicon->xindex!=0 || aicon->yindex!=0
3658 || getClipButton(event->xbutton.x, event->xbutton.y)==CLIP_IDLE) {
3659 iconDblClick(desc, event);
3660 return;
3664 if (dock->type == WM_CLIP && scr->flags.clip_balloon_mapped) {
3665 XUnmapWindow(dpy, scr->clip_balloon);
3666 scr->flags.clip_balloon_mapped = 0;
3669 #ifdef DEBUG
3670 puts("handling dock");
3671 #endif
3672 if (event->xbutton.button == Button1) {
3673 if (event->xbutton.state & MOD_MASK)
3674 wDockLower(dock);
3675 else
3676 wDockRaise(dock);
3678 if ((event->xbutton.state & ShiftMask) && aicon!=scr->clip_icon &&
3679 dock->type!=WM_DOCK) {
3680 wIconSelect(aicon->icon);
3681 return;
3684 if (aicon->yindex==0 && aicon->xindex==0) {
3685 if (getClipButton(event->xbutton.x, event->xbutton.y)!=CLIP_IDLE
3686 && dock->type==WM_CLIP)
3687 handleClipChangeWorkspace(scr, event);
3688 else
3689 handleDockMove(dock, aicon, event);
3690 } else
3691 handleIconMove(dock, aicon, event);
3693 } else if (event->xbutton.button==Button2 && dock->type==WM_CLIP &&
3694 aicon->xindex==0 && aicon->yindex==0) {
3695 openClipWorkspaceMenu(scr, event->xbutton.x_root+2,
3696 event->xbutton.y_root+2);
3697 if (scr->clip_ws_menu) {
3698 WMenu *menu;
3699 menu = scr->clip_ws_menu;
3700 desc = &menu->menu->descriptor;
3702 event->xany.send_event = True;
3703 (*desc->handle_mousedown)(desc, event);
3705 } else if (event->xbutton.button == Button3) {
3706 openDockMenu(dock, aicon, event);
3711 static void
3712 showClipBalloon(WDock *dock, int workspace)
3714 int w, h;
3715 int x, y;
3716 WScreen *scr = dock->screen_ptr;
3717 char *text;
3718 Window stack[2];
3720 scr->flags.clip_balloon_mapped = 1;
3721 XMapWindow(dpy, scr->clip_balloon);
3723 text = scr->workspaces[workspace]->name;
3725 w = wTextWidth(scr->clip_title_font->font, text, strlen(text));
3727 h = scr->clip_title_font->height;
3728 XResizeWindow(dpy, scr->clip_balloon, w, h);
3730 x = dock->x_pos + CLIP_BUTTON_SIZE*ICON_SIZE/64;
3731 y = dock->y_pos + ICON_SIZE-scr->clip_title_font->height - 3;
3733 if (x+w > scr->scr_width) {
3734 x = scr->scr_width - w;
3735 if (dock->y_pos + ICON_SIZE + h > scr->scr_height)
3736 y = dock->y_pos - h - 1;
3737 else
3738 y = dock->y_pos + ICON_SIZE;
3739 XRaiseWindow(dpy, scr->clip_balloon);
3740 } else {
3741 stack[0] = scr->clip_icon->icon->core->window;
3742 stack[1] = scr->clip_balloon;
3743 XRestackWindows(dpy, stack, 2);
3745 XMoveWindow(dpy, scr->clip_balloon, x, y);
3746 XSetForeground(dpy, scr->clip_title_gc,
3747 scr->clip_title_pixel[CLIP_NORMAL]);
3748 XClearWindow(dpy, scr->clip_balloon);
3749 wDrawString(scr->clip_balloon, scr->clip_title_font, scr->clip_title_gc,
3750 0, scr->clip_title_font->y, text, strlen(text));
3754 static void
3755 clipEnterNotify(WObjDescriptor *desc, XEvent *event)
3757 WAppIcon *btn = (WAppIcon*)desc->parent;
3758 WDock *dock;
3760 assert(event->type==EnterNotify);
3762 if(desc->parent_type!=WCLASS_DOCK_ICON)
3763 return;
3765 dock = btn->dock;
3766 if (!dock || dock->type!=WM_CLIP)
3767 return;
3769 if (btn->xindex == 0 && btn->yindex == 0)
3770 showClipBalloon(dock, dock->screen_ptr->current_workspace);
3771 else {
3772 if (dock->screen_ptr->flags.clip_balloon_mapped) {
3773 XUnmapWindow(dpy, dock->screen_ptr->clip_balloon);
3774 dock->screen_ptr->flags.clip_balloon_mapped = 0;
3777 if (!dock->auto_collapse)
3778 return;
3780 if (dock->auto_collapse_magic) {
3781 WMDeleteTimerHandler(dock->auto_collapse_magic);
3782 dock->auto_collapse_magic = NULL;
3785 if (dock->collapsed)
3786 toggleCollapsed(dock);
3790 static void
3791 clipLeave(WDock *dock)
3793 if (!dock || dock->type!=WM_CLIP || !dock->auto_collapse)
3794 return;
3796 if (dock->auto_collapse_magic) {
3797 WMDeleteTimerHandler(dock->auto_collapse_magic);
3798 dock->auto_collapse_magic = NULL;
3800 if (!dock->collapsed) {
3801 dock->auto_collapse_magic = WMAddTimerHandler(AUTO_COLLAPSE_DELAY,
3802 clipAutoCollapse,
3803 (void *)dock);
3808 static void
3809 clipLeaveNotify(WObjDescriptor *desc, XEvent *event)
3811 WAppIcon *btn = (WAppIcon*)desc->parent;
3813 assert(event->type==LeaveNotify);
3815 if(desc->parent_type!=WCLASS_DOCK_ICON)
3816 return;
3818 clipLeave(btn->dock);
3822 static void
3823 clipAutoCollapse(void *cdata)
3825 WDock *dock = (WDock *)cdata;
3827 if (dock->type!=WM_CLIP || !dock->auto_collapse)
3828 return;
3830 if (!dock->collapsed && dock->auto_collapse_magic!=NULL) {
3831 toggleCollapsed(dock);
3833 dock->auto_collapse_magic = NULL;