Updating to version 0.20.2
[wmaker-crm.git] / src / dock.c
blob6ac9cd436b8fba0cec073689885b541c71729fd1
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 Clip 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 Dock 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 buffer[256];
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 if (btn->wm_class && btn->wm_instance)
1209 sprintf(buffer, "%s.%s", btn->wm_instance, btn->wm_class);
1210 else if (btn->wm_instance)
1211 sprintf(buffer, "%s", btn->wm_instance);
1212 else if (btn->wm_class)
1213 sprintf(buffer, ".%s", btn->wm_class);
1214 else
1215 sprintf(buffer, ".");
1217 name = PLMakeString(buffer);
1219 forced = btn->forced_dock ? dYes : dNo;
1221 buggy = btn->buggy_app ? dYes: dNo;
1223 if (btn == btn->icon->core->screen_ptr->clip_icon)
1224 sprintf(buffer, "%i,%i", btn->x_pos, btn->y_pos);
1225 else
1226 sprintf(buffer, "%hi,%hi", btn->xindex, btn->yindex);
1227 position = PLMakeString(buffer);
1229 node = PLMakeDictionaryFromEntries(dCommand, command,
1230 dName, name,
1231 dAutoLaunch, autolaunch,
1232 dForced, forced,
1233 dBuggyApplication, buggy,
1234 dPosition, position,
1235 NULL);
1236 PLRelease(command);
1237 PLRelease(name);
1238 PLRelease(position);
1239 #ifdef OFFIX_DND
1240 if (btn->dnd_command) {
1241 command = PLMakeString(btn->dnd_command);
1242 PLInsertDictionaryEntry(node, dDropCommand, command);
1243 PLRelease(command);
1245 #endif /* OFFIX_DND */
1247 if (btn->client_machine && btn->remote_start) {
1248 host = PLMakeString(btn->client_machine);
1249 PLInsertDictionaryEntry(node, dHost, host);
1250 PLRelease(host);
1254 return node;
1258 static proplist_t
1259 dockSaveState(WDock *dock)
1261 int i;
1262 proplist_t icon_info;
1263 proplist_t list=NULL, dock_state=NULL;
1264 proplist_t value;
1265 char buffer[256];
1267 list = PLMakeArrayFromElements(NULL);
1269 for (i=(dock->type==WM_DOCK ? 0 : 1); i<dock->max_icons; i++) {
1270 WAppIcon *btn = dock->icon_array[i];
1272 if (!btn || (btn->attracted && !dock->keep_attracted))
1273 continue;
1275 if ((icon_info = make_icon_state(dock->icon_array[i]))) {
1276 list = PLAppendArrayElement(list, icon_info);
1277 PLRelease(icon_info);
1281 dock_state = PLMakeDictionaryFromEntries(dApplications, list, NULL);
1283 PLRelease(list);
1285 if (dock->type == WM_DOCK) {
1286 sprintf(buffer, "%i,%i", (dock->on_right_side ? -ICON_SIZE : 0),
1287 dock->y_pos);
1288 value = PLMakeString(buffer);
1289 PLInsertDictionaryEntry(dock_state, dPosition, value);
1290 PLRelease(value);
1293 value = (dock->lowered ? dYes : dNo);
1294 PLInsertDictionaryEntry(dock_state, dLowered, value);
1296 if (dock->type == WM_CLIP) {
1297 value = (dock->collapsed ? dYes : dNo);
1298 PLInsertDictionaryEntry(dock_state, dCollapsed, value);
1300 value = (dock->auto_collapse ? dYes : dNo);
1301 PLInsertDictionaryEntry(dock_state, dAutoCollapse, value);
1303 value = (dock->attract_icons ? dYes : dNo);
1304 PLInsertDictionaryEntry(dock_state, dAutoAttractIcons, value);
1306 value = (dock->keep_attracted ? dYes : dNo);
1307 PLInsertDictionaryEntry(dock_state, dKeepAttracted, value);
1310 return dock_state;
1314 void
1315 wDockSaveState(WScreen *scr)
1317 proplist_t dock_state;
1319 dock_state = dockSaveState(scr->dock);
1321 PLInsertDictionaryEntry(scr->session_state, dDock, dock_state);
1323 PLRelease(dock_state);
1327 void
1328 wClipSaveState(WScreen *scr)
1330 proplist_t clip_state;
1332 clip_state = make_icon_state(scr->clip_icon);
1334 PLInsertDictionaryEntry(scr->session_state, dClip, clip_state);
1336 PLRelease(clip_state);
1340 proplist_t
1341 wClipSaveWorkspaceState(WScreen *scr, int workspace)
1343 return dockSaveState(scr->workspaces[workspace]->clip);
1347 static WAppIcon*
1348 restore_icon_state(WScreen *scr, proplist_t info, int type, int index)
1350 WAppIcon *aicon;
1351 char *wclass, *winstance;
1352 proplist_t cmd, value;
1353 char *command;
1356 cmd = PLGetDictionaryEntry(info, dCommand);
1357 if (!cmd || !PLIsString(cmd)) {
1358 return NULL;
1361 /* parse window name */
1362 value = PLGetDictionaryEntry(info, dName);
1363 if (!value)
1364 return NULL;
1366 ParseWindowName(value, &winstance, &wclass, "dock");
1368 if (!winstance && !wclass) {
1369 return NULL;
1372 /* get commands */
1374 command = wstrdup(PLGetString(cmd));
1376 if (!command || strcmp(command, "-")==0) {
1377 if (command)
1378 free(command);
1379 if (wclass)
1380 free(wclass);
1381 if (winstance)
1382 free(winstance);
1384 return NULL;
1387 aicon = wAppIconCreateForDock(scr, command, winstance, wclass,
1388 TILE_NORMAL);
1389 if (wclass)
1390 free(wclass);
1391 if (winstance)
1392 free(winstance);
1394 aicon->icon->core->descriptor.handle_mousedown = iconMouseDown;
1395 if (type == WM_CLIP) {
1396 aicon->icon->core->descriptor.handle_enternotify = clipEnterNotify;
1397 aicon->icon->core->descriptor.handle_leavenotify = clipLeaveNotify;
1399 aicon->icon->core->descriptor.parent_type = WCLASS_DOCK_ICON;
1400 aicon->icon->core->descriptor.parent = aicon;
1403 #ifdef OFFIX_DND
1404 cmd = PLGetDictionaryEntry(info, dDropCommand);
1405 if (cmd)
1406 aicon->dnd_command = wstrdup(PLGetString(cmd));
1407 #endif
1409 /* check auto launch */
1410 value = PLGetDictionaryEntry(info, dAutoLaunch);
1412 aicon->auto_launch = 0;
1413 if (value) {
1414 if (PLIsString(value)) {
1415 if (strcasecmp(PLGetString(value), "YES")==0)
1416 aicon->auto_launch = 1;
1417 } else {
1418 wwarning(_("bad value in docked icon state info %s"),
1419 PLGetString(dAutoLaunch));
1423 /* check if it wasn't normally docked */
1424 value = PLGetDictionaryEntry(info, dForced);
1426 aicon->forced_dock = 0;
1427 if (value) {
1428 if (PLIsString(value)) {
1429 if (strcasecmp(PLGetString(value), "YES")==0)
1430 aicon->forced_dock = 1;
1431 } else {
1432 wwarning(_("bad value in docked icon state info %s"),
1433 PLGetString(dForced));
1437 /* check if we can rely on the stuff in the app */
1438 value = PLGetDictionaryEntry(info, dBuggyApplication);
1440 aicon->buggy_app = 0;
1441 if (value) {
1442 if (PLIsString(value)) {
1443 if (strcasecmp(PLGetString(value), "YES")==0)
1444 aicon->buggy_app = 1;
1445 } else {
1446 wwarning(_("bad value in docked icon state info %s"),
1447 PLGetString(dBuggyApplication));
1451 /* get position in the dock */
1452 value = PLGetDictionaryEntry(info, dPosition);
1453 if (value && PLIsString(value)) {
1454 if (sscanf(PLGetString(value), "%hi,%hi", &aicon->xindex,
1455 &aicon->yindex)!=2)
1456 wwarning(_("bad value in docked icon state info %s"),
1457 PLGetString(dPosition));
1459 /* check position sanity */
1460 /* incomplete section! */
1461 if (type == WM_DOCK) {
1462 aicon->xindex = 0;
1463 if (aicon->yindex < 0)
1464 wwarning(_("bad value in docked icon position %i,%i"),
1465 aicon->xindex, aicon->yindex);
1467 } else {
1468 aicon->yindex = index;
1469 aicon->xindex = 0;
1472 aicon->running = 0;
1473 aicon->docked = 1;
1475 return aicon;
1479 #define COMPLAIN(key) wwarning(_("bad value in dock state info:%s"), key)
1482 WAppIcon*
1483 wClipRestoreState(WScreen *scr, proplist_t clip_state)
1485 WAppIcon *icon;
1486 proplist_t value;
1489 icon = mainIconCreate(scr, WM_CLIP);
1491 if (!clip_state)
1492 return icon;
1493 else
1494 PLRetain(clip_state);
1496 /* restore position */
1498 value = PLGetDictionaryEntry(clip_state, dPosition);
1500 if (value) {
1501 if (!PLIsString(value))
1502 COMPLAIN("Position");
1503 else {
1504 if (sscanf(PLGetString(value), "%i,%i", &icon->x_pos,
1505 &icon->y_pos)!=2)
1506 COMPLAIN("Position");
1508 /* check position sanity */
1509 if (icon->y_pos < 0)
1510 icon->y_pos = 0;
1511 else if (icon->y_pos > scr->scr_height-ICON_SIZE)
1512 icon->y_pos = scr->scr_height-ICON_SIZE;
1514 if (icon->x_pos < 0)
1515 icon->x_pos = 0;
1516 else if (icon->x_pos > scr->scr_width-ICON_SIZE)
1517 icon->x_pos = scr->scr_width-ICON_SIZE;
1521 #ifdef OFFIX_DND
1522 value = PLGetDictionaryEntry(clip_state, dDropCommand);
1523 if (value && PLIsString(value))
1524 icon->dnd_command = wstrdup(PLGetString(value));
1525 #endif
1527 PLRelease(clip_state);
1529 return icon;
1533 WDock*
1534 wDockRestoreState(WScreen *scr, proplist_t dock_state, int type)
1536 WDock *dock;
1537 proplist_t apps;
1538 proplist_t value;
1539 WAppIcon *aicon, *old_top;
1540 int count, i;
1543 dock = wDockCreate(scr, type);
1545 if (!dock_state)
1546 return dock;
1548 if (dock_state)
1549 PLRetain(dock_state);
1552 /* restore position */
1554 value = PLGetDictionaryEntry(dock_state, dPosition);
1556 if (value) {
1557 if (!PLIsString(value))
1558 COMPLAIN("Position");
1559 else {
1560 if (sscanf(PLGetString(value), "%i,%i", &dock->x_pos,
1561 &dock->y_pos)!=2)
1562 COMPLAIN("Position");
1564 /* check position sanity */
1565 if (dock->y_pos < 0)
1566 dock->y_pos = 0;
1567 else if (dock->y_pos > scr->scr_height-ICON_SIZE)
1568 dock->y_pos = scr->scr_height-ICON_SIZE;
1570 /* This is no more needed. ??? */
1571 if (type == WM_CLIP) {
1572 if (dock->x_pos < 0)
1573 dock->x_pos = 0;
1574 else if (dock->x_pos > scr->scr_width-ICON_SIZE)
1575 dock->x_pos = scr->scr_width-ICON_SIZE;
1577 else {
1578 if (dock->x_pos >= 0) {
1579 dock->x_pos = DOCK_EXTRA_SPACE;
1580 dock->on_right_side = 0;
1581 } else {
1582 dock->x_pos = scr->scr_width - DOCK_EXTRA_SPACE - ICON_SIZE;
1583 dock->on_right_side = 1;
1589 /* restore lowered/raised state */
1591 dock->lowered = 0;
1593 value = PLGetDictionaryEntry(dock_state, dLowered);
1595 if (value) {
1596 if (!PLIsString(value))
1597 COMPLAIN("Lowered");
1598 else {
1599 if (strcasecmp(PLGetString(value), "YES")==0)
1600 dock->lowered = 1;
1605 /* restore collapsed state */
1607 dock->collapsed = 0;
1609 value = PLGetDictionaryEntry(dock_state, dCollapsed);
1611 if (value) {
1612 if (!PLIsString(value))
1613 COMPLAIN("Collapsed");
1614 else {
1615 if (strcasecmp(PLGetString(value), "YES")==0)
1616 dock->collapsed = 1;
1621 /* restore auto-collapsed state */
1623 value = PLGetDictionaryEntry(dock_state, dAutoCollapse);
1625 if (value) {
1626 if (!PLIsString(value))
1627 COMPLAIN("AutoCollapse");
1628 else {
1629 if (strcasecmp(PLGetString(value), "YES")==0) {
1630 dock->auto_collapse = 1;
1631 dock->collapsed = 1;
1637 /* restore attract icons state */
1639 dock->attract_icons = 0;
1641 value = PLGetDictionaryEntry(dock_state, dAutoAttractIcons);
1643 if (value) {
1644 if (!PLIsString(value))
1645 COMPLAIN("AutoAttractIcons");
1646 else {
1647 if (strcasecmp(PLGetString(value), "YES")==0)
1648 dock->attract_icons = 1;
1653 /* restore keep attracted icons state */
1655 dock->keep_attracted = 0;
1657 value = PLGetDictionaryEntry(dock_state, dKeepAttracted);
1659 if (value) {
1660 if (!PLIsString(value))
1661 COMPLAIN("KeepAttracted");
1662 else {
1663 if (strcasecmp(PLGetString(value), "YES")==0)
1664 dock->keep_attracted = 1;
1669 /* application list */
1671 apps = PLGetDictionaryEntry(dock_state, dApplications);
1673 if (!apps) {
1674 goto finish;
1677 count = PLGetNumberOfElements(apps);
1679 if (count==0)
1680 goto finish;
1682 old_top = dock->icon_array[0];
1684 /* dock->icon_count is set to 1 when dock is created.
1685 * Since Clip is already restored, we want to keep it so for clip,
1686 * but for dock we may change the default top tile, so we set it to 0.
1688 if (type == WM_DOCK)
1689 dock->icon_count = 0;
1691 for (i=0; i<count; i++) {
1692 if (dock->icon_count >= dock->max_icons) {
1693 wwarning(_("there are too many icons stored in dock. Ignoring what doesn't fit"));
1694 break;
1697 value = PLGetArrayElement(apps, i);
1698 aicon = restore_icon_state(scr, value, type, dock->icon_count);
1700 dock->icon_array[dock->icon_count] = aicon;
1702 if (aicon) {
1703 aicon->dock = dock;
1704 aicon->x_pos = dock->x_pos + (aicon->xindex*ICON_SIZE);
1705 aicon->y_pos = dock->y_pos + (aicon->yindex*ICON_SIZE);
1707 if (dock->lowered)
1708 ChangeStackingLevel(aicon->icon->core, WMNormalWindowLevel);
1709 else
1710 ChangeStackingLevel(aicon->icon->core, WMDockWindowLevel);
1712 wCoreConfigure(aicon->icon->core, aicon->x_pos, aicon->y_pos,
1713 0, 0);
1715 if (!dock->collapsed)
1716 XMapWindow(dpy, aicon->icon->core->window);
1717 wRaiseFrame(aicon->icon->core);
1719 dock->icon_count++;
1720 } else if (dock->icon_count==0 && type==WM_DOCK)
1721 dock->icon_count++;
1724 /* if the first icon is not defined, use the default */
1725 if (dock->icon_array[0]==NULL) {
1726 /* update default icon */
1727 old_top->x_pos = dock->x_pos;
1728 old_top->y_pos = dock->y_pos;
1729 if (dock->lowered)
1730 ChangeStackingLevel(old_top->icon->core, WMNormalWindowLevel);
1731 else
1732 ChangeStackingLevel(old_top->icon->core, WMDockWindowLevel);
1733 dock->icon_array[0] = old_top;
1734 XMoveWindow(dpy, old_top->icon->core->window, dock->x_pos, dock->y_pos);
1735 /* we don't need to increment dock->icon_count here because it was
1736 * incremented in the loop above.
1738 } else if (old_top!=dock->icon_array[0]) {
1739 if (old_top == scr->clip_icon)
1740 scr->clip_icon = dock->icon_array[0];
1741 wAppIconDestroy(old_top);
1744 finish:
1745 if (dock_state)
1746 PLRelease(dock_state);
1748 return dock;
1753 void
1754 wDockLaunchWithState(WDock *dock, WAppIcon *btn, WSavedState *state)
1756 if (btn && btn->command && !btn->running && !btn->launching) {
1758 btn->drop_launch = 0;
1760 btn->pid = execCommand(btn, btn->command, state);
1762 if (btn->pid>0) {
1763 if (!btn->forced_dock && !btn->buggy_app) {
1764 btn->launching = 1;
1765 dockIconPaint(btn);
1768 } else {
1769 free(state);
1774 void
1775 wDockDoAutoLaunch(WDock *dock, int workspace)
1777 WAppIcon *btn;
1778 WSavedState *state;
1779 int i;
1781 for (i=0; i < dock->max_icons; i++) {
1782 btn = dock->icon_array[i];
1783 if (!btn || !btn->auto_launch)
1784 continue;
1786 state = wmalloc(sizeof(WSavedState));
1787 memset(state, 0, sizeof(WSavedState));
1788 state->workspace = workspace;
1789 /* TODO: this is klugy and is very difficult to understand
1790 * what's going on. Try to clean up */
1791 wDockLaunchWithState(dock, btn, state);
1795 #ifdef REDUCE_APPICONS
1796 void
1797 wDockSimulateLaunch(WDock *dock, WAppIcon *btn)
1799 if ((btn == NULL) || (dock == NULL))
1800 return;
1802 if (!btn->running) {
1803 if ((btn->icon->owner == NULL) && (btn->applist))
1804 btn->icon->owner = btn->applist->wapp->main_window_desc;
1805 if (!btn->forced_dock)
1806 btn->launching = 1;
1807 dockIconPaint(btn);
1808 wusleep(5000);
1811 #endif
1813 #ifdef OFFIX_DND
1814 static WDock*
1815 findDock(WScreen *scr, XEvent *event, int *icon_pos)
1817 WDock *dock;
1818 int i;
1820 *icon_pos = -1;
1821 if ((dock = scr->dock)!=NULL) {
1822 for (i=0; i<dock->max_icons; i++) {
1823 if (dock->icon_array[i]
1824 && dock->icon_array[i]->icon->core->window==event->xclient.window) {
1825 *icon_pos = i;
1826 break;
1830 if (*icon_pos<0 && (dock = scr->workspaces[scr->current_workspace]->clip)!=NULL) {
1831 for (i=0; i<dock->max_icons; i++) {
1832 if (dock->icon_array[i]
1833 && dock->icon_array[i]->icon->core->window==event->xclient.window) {
1834 *icon_pos = i;
1835 break;
1839 if(*icon_pos>=0)
1840 return dock;
1841 return NULL;
1846 wDockReceiveDNDDrop(WScreen *scr, XEvent *event)
1848 WDock *dock;
1849 WAppIcon *btn;
1850 int icon_pos;
1852 dock = findDock(scr, event, &icon_pos);
1853 if (!dock)
1854 return False;
1857 * Return True if the drop was on an application icon window.
1858 * In this case, let the ClientMessage handler redirect the
1859 * message to the app.
1861 if (dock->icon_array[icon_pos]->icon->icon_win!=None)
1862 return True;
1864 if (dock->icon_array[icon_pos]->dnd_command!=NULL) {
1865 scr->flags.dnd_data_convertion_status = 0;
1867 btn = dock->icon_array[icon_pos];
1869 if (!btn->forced_dock) {
1870 btn->relaunching = btn->running;
1871 btn->running = 1;
1873 if (btn->wm_instance || btn->wm_class) {
1874 WWindowAttributes attr;
1875 memset(&attr, 0, sizeof(WWindowAttributes));
1876 wDefaultFillAttributes(btn->icon->core->screen_ptr,
1877 btn->wm_instance,
1878 btn->wm_class, &attr, True);
1880 if (!attr.no_appicon)
1881 btn->launching = 1;
1882 else
1883 btn->running = 0;
1886 btn->drop_launch = 1;
1887 scr->last_dock = dock;
1888 btn->pid = execCommand(btn, btn->dnd_command, NULL);
1889 if (btn->pid>0) {
1890 dockIconPaint(btn);
1891 } else {
1892 btn->launching = 0;
1893 if (!btn->relaunching) {
1894 btn->running = 0;
1898 return False;
1900 #endif /* OFFIX_DND */
1904 Bool
1905 wDockAttachIcon(WDock *dock, WAppIcon *icon, int x, int y)
1907 WWindow *wwin;
1908 char **argv;
1909 int argc;
1910 int index;
1912 wwin = icon->icon->owner;
1913 if (icon->command==NULL) {
1914 icon->editing = 0;
1915 if (XGetCommand(dpy, wwin->client_win, &argv, &argc) && argc>0) {
1917 icon->command = FlattenStringList(argv, argc);
1918 XFreeStringList(argv);
1919 } else {
1920 char *command=NULL;
1922 /* icon->forced_dock = 1;*/
1923 if (!icon->attracted || dock->type!=WM_CLIP || dock->keep_attracted) {
1924 icon->editing = 1;
1925 if (wInputDialog(dock->screen_ptr, _("Dock Icon"),
1926 _("Type the command used to launch the application"),
1927 &command)==WDB_OK) {
1928 if (command && (command[0]==0 ||
1929 (command[0]=='-' && command[1]==0))) {
1930 free(command);
1931 command = NULL;
1933 icon->command = command;
1934 icon->editing = 0;
1935 } else {
1936 icon->editing = 0;
1937 if (command)
1938 free(command);
1939 /* If the target is the dock, reject the icon. If
1940 * the target is the clip, make it an attracted icon
1942 if (dock->type==WM_CLIP) {
1943 icon->attracted = 1;
1944 if (!icon->icon->shadowed) {
1945 icon->icon->shadowed = 1;
1946 icon->icon->force_paint = 1;
1948 } else {
1949 return False;
1954 } else {
1955 icon->editing = 0;
1958 for (index=1; index<dock->max_icons; index++)
1959 if (dock->icon_array[index] == NULL)
1960 break;
1961 /* if (index == dock->max_icons)
1962 return; */
1964 assert(index < dock->max_icons);
1966 dock->icon_array[index] = icon;
1967 icon->yindex = y;
1968 icon->xindex = x;
1970 icon->x_pos = dock->x_pos + x*ICON_SIZE;
1971 icon->y_pos = dock->y_pos + y*ICON_SIZE;
1973 dock->icon_count++;
1975 icon->running = 1;
1976 icon->launching = 0;
1977 icon->docked = 1;
1978 icon->dock = dock;
1979 icon->icon->core->descriptor.handle_mousedown = iconMouseDown;
1980 if (dock->type == WM_CLIP) {
1981 icon->icon->core->descriptor.handle_enternotify = clipEnterNotify;
1982 icon->icon->core->descriptor.handle_leavenotify = clipLeaveNotify;
1984 icon->icon->core->descriptor.parent_type = WCLASS_DOCK_ICON;
1985 icon->icon->core->descriptor.parent = icon;
1987 MoveInStackListUnder(dock->icon_array[index-1]->icon->core,
1988 icon->icon->core);
1989 wAppIconMove(icon, icon->x_pos, icon->y_pos);
1990 wAppIconPaint(icon);
1992 if (wPreferences.auto_arrange_icons)
1993 wArrangeIcons(dock->screen_ptr, True);
1995 return True;
1999 void
2000 reattachIcon(WDock *dock, WAppIcon *icon, int x, int y)
2002 int index;
2004 for(index=1; index<dock->max_icons; index++) {
2005 if(dock->icon_array[index] == icon)
2006 break;
2008 assert(index < dock->max_icons);
2010 icon->yindex = y;
2011 icon->xindex = x;
2013 icon->x_pos = dock->x_pos + x*ICON_SIZE;
2014 icon->y_pos = dock->y_pos + y*ICON_SIZE;
2018 Bool
2019 moveIconBetweenDocks(WDock *src, WDock *dest, WAppIcon *icon, int x, int y)
2021 WWindow *wwin;
2022 char **argv;
2023 int argc;
2024 int index;
2026 if (dest == NULL)
2027 return False;
2029 wwin = icon->icon->owner;
2032 * For the moment we can't do this if we move icons in Clip from one
2033 * workspace to other, because if we move two or more icons without
2034 * command, the dialog box will not be able to tell us to which of the
2035 * moved icons it applies. -Dan
2037 if ((dest->type==WM_DOCK /*|| dest->keep_attracted*/) && icon->command==NULL) {
2038 if (XGetCommand(dpy, wwin->client_win, &argv, &argc) && argc>0) {
2040 icon->command = FlattenStringList(argv, argc);
2041 XFreeStringList(argv);
2042 } else {
2043 char *command=NULL;
2045 icon->editing = 1;
2046 /* icon->forced_dock = 1;*/
2047 if (wInputDialog(src->screen_ptr, _("Dock Icon"),
2048 _("Type the command used to launch the application"),
2049 &command)==WDB_OK) {
2050 if (command && (command[0]==0 ||
2051 (command[0]=='-' && command[1]==0))) {
2052 free(command);
2053 command = NULL;
2055 icon->command = command;
2056 } else {
2057 icon->editing = 0;
2058 if (command)
2059 free(command);
2060 return False;
2062 icon->editing = 0;
2066 for(index=1; index<src->max_icons; index++) {
2067 if(src->icon_array[index] == icon)
2068 break;
2070 assert(index < src->max_icons);
2072 src->icon_array[index] = NULL;
2073 src->icon_count--;
2075 for(index=1; index<dest->max_icons; index++) {
2076 if(dest->icon_array[index] == NULL)
2077 break;
2079 /* if (index == dest->max_icons)
2080 return; */
2082 assert(index < dest->max_icons);
2084 dest->icon_array[index] = icon;
2085 icon->dock = dest;
2087 /* deselect the icon */
2088 if (icon->icon->selected)
2089 wIconSelect(icon->icon);
2091 if (dest->type == WM_DOCK) {
2092 icon->icon->core->descriptor.handle_enternotify = NULL;
2093 icon->icon->core->descriptor.handle_leavenotify = NULL;
2094 } else {
2095 icon->icon->core->descriptor.handle_enternotify = clipEnterNotify;
2096 icon->icon->core->descriptor.handle_leavenotify = clipLeaveNotify;
2099 /* set it to be kept when moving to dock, or to a clip that keep the
2100 * attracted icons.
2101 * Unless the icon does not have a command set
2103 if (icon->command && (dest->type==WM_DOCK || dest->keep_attracted)) {
2104 icon->attracted = 0;
2105 if (icon->icon->shadowed) {
2106 icon->icon->shadowed = 0;
2107 icon->icon->force_paint = 1;
2111 if (src->auto_collapse)
2112 clipLeave(src);
2114 icon->yindex = y;
2115 icon->xindex = x;
2117 icon->x_pos = dest->x_pos + x*ICON_SIZE;
2118 icon->y_pos = dest->y_pos + y*ICON_SIZE;
2120 dest->icon_count++;
2122 MoveInStackListUnder(dest->icon_array[index-1]->icon->core,
2123 icon->icon->core);
2124 wAppIconPaint(icon);
2126 return True;
2129 void
2130 wDockDetach(WDock *dock, WAppIcon *icon)
2132 int index;
2134 /* make the settings panel be closed */
2135 if (icon->panel) {
2136 DestroyDockAppSettingsPanel(icon->panel);
2139 icon->docked = 0;
2140 icon->dock = NULL;
2141 icon->attracted = 0;
2142 if (icon->icon->shadowed) {
2143 icon->icon->shadowed = 0;
2144 icon->icon->force_paint = 1;
2147 /* deselect the icon */
2148 if (icon->icon->selected)
2149 wIconSelect(icon->icon);
2151 if (icon->command) {
2152 free(icon->command);
2153 icon->command = NULL;
2155 #ifdef OFFIX_DND
2156 if (icon->dnd_command) {
2157 free(icon->dnd_command);
2158 icon->dnd_command = NULL;
2160 #endif
2162 for (index=1; index<dock->max_icons; index++)
2163 if (dock->icon_array[index] == icon)
2164 break;
2165 assert(index < dock->max_icons);
2166 dock->icon_array[index] = NULL;
2167 icon->yindex = -1;
2168 icon->xindex = -1;
2169 dock->icon_count--;
2171 /* if the dock is not attached to an application or
2172 * the the application did not set the approriate hints yet,
2173 * destroy the icon */
2174 #ifdef REDUCE_APPICONS
2175 if ((icon->num_apps == 0) && (!icon->running || !wApplicationOf(icon->main_window)) )
2176 #else
2177 if (!icon->running || !wApplicationOf(icon->main_window))
2178 #endif
2179 wAppIconDestroy(icon);
2180 else {
2181 icon->icon->core->descriptor.handle_mousedown = appIconMouseDown;
2182 icon->icon->core->descriptor.handle_enternotify = NULL;
2183 icon->icon->core->descriptor.handle_leavenotify = NULL;
2184 icon->icon->core->descriptor.parent_type = WCLASS_APPICON;
2185 icon->icon->core->descriptor.parent = icon;
2186 ChangeStackingLevel(icon->icon->core, WMNormalWindowLevel);
2187 wAppIconPaint(icon);
2188 if (wPreferences.auto_arrange_icons) {
2189 wArrangeIcons(dock->screen_ptr, True);
2192 if (dock->auto_collapse)
2193 clipLeave(dock);
2198 * returns the closest Dock slot index for the passed
2199 * coordinates.
2201 * Returns False if icon can't be docked.
2204 wDockSnapIcon(WDock *dock, WAppIcon *icon, int req_x, int req_y,
2205 int *ret_x, int *ret_y, int redocking)
2207 WScreen *scr = dock->screen_ptr;
2208 int dx, dy;
2209 int ex_x, ex_y;
2210 int i, offset = ICON_SIZE/2;
2211 WAppIcon *aicon = NULL;
2212 WAppIcon *nicon = NULL;
2215 dx = dock->x_pos;
2216 dy = dock->y_pos;
2218 /* if the dock is full */
2219 if (!redocking &&
2220 (dock->icon_count >= dock->max_icons)) {
2221 return False;
2224 /* exact position */
2225 if (req_y < dy)
2226 ex_y = (req_y - offset - dy)/ICON_SIZE;
2227 else
2228 ex_y = (req_y + offset - dy)/ICON_SIZE;
2230 if (req_x < dx)
2231 ex_x = (req_x - offset - dx)/ICON_SIZE;
2232 else
2233 ex_x = (req_x + offset - dx)/ICON_SIZE;
2235 /* check if the icon is outside the screen boundaries */
2236 if (dx + ex_x*ICON_SIZE < -ICON_SIZE+2 ||
2237 dx + ex_x*ICON_SIZE > scr->scr_width-1 ||
2238 dy + ex_y*ICON_SIZE < -ICON_SIZE+2 ||
2239 dy + ex_y*ICON_SIZE > scr->scr_height-1)
2240 return False;
2242 if (dock->type == WM_DOCK) {
2243 for (i=0; i<dock->max_icons; i++) {
2244 nicon = dock->icon_array[i];
2245 if (nicon && nicon->yindex == ex_y) {
2246 aicon = nicon;
2247 break;
2251 *ret_x = 0;
2253 if (redocking) {
2254 int sig, done, closest;
2256 /* Possible cases when redocking:
2258 * icon dragged out of range of any slot -> false
2259 * icon dragged to range of free slot
2260 * icon dragged to range of same slot
2261 * icon dragged to range of different icon
2263 if (abs(ex_x) > DOCK_DETTACH_THRESHOLD)
2264 return False;
2266 if (ex_y >=0 && (aicon == icon || !aicon)) {
2268 *ret_y = ex_y;
2270 return True;
2273 /* start looking at the upper slot or lower? */
2274 if (ex_y*ICON_SIZE < (req_y + offset - dy))
2275 sig = 1;
2276 else
2277 sig = -1;
2279 closest = -1;
2280 done = 0;
2281 /* look for closest free slot */
2282 for (i=0; i<(DOCK_DETTACH_THRESHOLD+1)*2 && !done; i++) {
2283 int j;
2285 done = 1;
2286 closest = sig*(i/2) + ex_y;
2287 /* check if this slot is used */
2288 if (closest >= 0) {
2289 for (j = 0; j<dock->max_icons; j++) {
2290 if (dock->icon_array[j]
2291 && dock->icon_array[j]->yindex==closest) {
2292 /* slot is used by someone else */
2293 if (dock->icon_array[j]!=icon)
2294 done = 0;
2295 break;
2299 sig = -sig;
2301 if (done && closest >= 0 &&
2302 ((ex_y >= closest && ex_y - closest < DOCK_DETTACH_THRESHOLD+1)
2304 (ex_y < closest && closest - ex_y <= DOCK_DETTACH_THRESHOLD+1))) {
2306 *ret_y = closest;
2308 return True;
2310 } else { /* !redocking */
2312 /* if slot is free and the icon is close enough, return it */
2313 if (!aicon && ex_x==0 && ex_y>=0) {
2314 *ret_y = ex_y;
2315 return True;
2318 } else { /* CLIP */
2319 int neighbours = 0;
2321 for (i=0; i<dock->max_icons; i++) {
2322 nicon = dock->icon_array[i];
2323 if (nicon && nicon->xindex == ex_x && nicon->yindex == ex_y) {
2324 aicon = nicon;
2325 break;
2329 for (i=0; i<dock->max_icons; i++) {
2330 nicon = dock->icon_array[i];
2331 if (nicon && nicon != icon && /* Icon can't be it's own neighbour */
2332 (abs(nicon->xindex - ex_x) <= CLIP_ATTACH_VICINITY &&
2333 abs(nicon->yindex - ex_y) <= CLIP_ATTACH_VICINITY)) {
2334 neighbours = 1;
2335 break;
2338 if ((!redocking && neighbours && !aicon) ||
2339 (redocking && neighbours && (aicon == icon || !aicon))) {
2340 *ret_x = ex_x;
2341 *ret_y = ex_y;
2342 return True;
2345 return False;
2348 #define MIN(x, y) ((x) > (y) ? (y) : (x))
2349 #define MAX(x, y) ((x) < (y) ? (y) : (x))
2351 #define ON_SCREEN(x, y, sx, ex, sy, ey) \
2352 ((((x)+ICON_SIZE/2) >= (sx)) && (((y)+ICON_SIZE/2) >= (sy)) && \
2353 (((x) + (ICON_SIZE/2)) <= (ex)) && (((y) + (ICON_SIZE/2)) <= ey))
2357 * returns true if it can find a free slot in the dock,
2358 * in which case it changes x_pos and y_pos accordingly.
2359 * Else returns false.
2362 wDockFindFreeSlot(WDock *dock, int *x_pos, int *y_pos)
2364 WScreen *scr = dock->screen_ptr;
2365 WAppIcon *btn;
2366 unsigned char *slot_map;
2367 int mwidth;
2368 int r;
2369 int x, y;
2370 int i, done = False;
2371 int corner;
2372 int sx=0, sy=0, ex=scr->scr_width, ey=scr->scr_height;
2374 /* if the dock is full */
2375 if (dock->icon_count >= dock->max_icons) {
2376 return False;
2379 if (!wPreferences.flags.nodock && scr->dock) {
2380 if (scr->dock->on_right_side)
2381 ex -= ICON_SIZE + DOCK_EXTRA_SPACE;
2382 else
2383 sx += ICON_SIZE + DOCK_EXTRA_SPACE;
2386 if (ex < dock->x_pos)
2387 ex = dock->x_pos;
2388 if (sx > dock->x_pos+ICON_SIZE)
2389 sx = dock->x_pos+ICON_SIZE;
2390 #define C_NONE 0
2391 #define C_NW 1
2392 #define C_NE 2
2393 #define C_SW 3
2394 #define C_SE 4
2396 /* check if clip is in a corner */
2397 if (dock->type==WM_CLIP) {
2398 if (dock->x_pos < 1 && dock->y_pos < 1)
2399 corner = C_NE;
2400 else if (dock->x_pos < 1 && dock->y_pos >= (ey-ICON_SIZE))
2401 corner = C_SE;
2402 else if (dock->x_pos >= (ex-ICON_SIZE)&& dock->y_pos >= (ey-ICON_SIZE))
2403 corner = C_SW;
2404 else if (dock->x_pos >= (ex-ICON_SIZE) && dock->y_pos < 1)
2405 corner = C_NW;
2406 else
2407 corner = C_NONE;
2408 } else
2409 corner = C_NONE;
2411 /* If the clip is in the corner, use only slots that are in the border
2412 * of the screen */
2413 if (corner!=C_NONE) {
2414 char *hmap, *vmap;
2415 int hcount, vcount;
2417 hcount = MIN(dock->max_icons, scr->scr_width/ICON_SIZE);
2418 vcount = MIN(dock->max_icons, scr->scr_height/ICON_SIZE);
2419 hmap = wmalloc(hcount+1);
2420 memset(hmap, 0, hcount+1);
2421 vmap = wmalloc(vcount+1);
2422 memset(vmap, 0, vcount+1);
2424 /* mark used positions */
2425 switch (corner) {
2426 case C_NE:
2427 for (i=0; i<dock->max_icons; i++) {
2428 btn = dock->icon_array[i];
2429 if (!btn)
2430 continue;
2432 if (btn->xindex==0 && btn->yindex > 0 && btn->yindex < vcount)
2433 vmap[btn->yindex] = 1;
2434 else if (btn->yindex==0 && btn->xindex>0 && btn->xindex<hcount)
2435 hmap[btn->xindex] = 1;
2437 case C_NW:
2438 for (i=0; i<dock->max_icons; i++) {
2439 btn = dock->icon_array[i];
2440 if (!btn)
2441 continue;
2443 if (btn->xindex==0 && btn->yindex > 0 && btn->yindex < vcount)
2444 vmap[btn->yindex] = 1;
2445 else if (btn->yindex==0 && btn->xindex<0 &&btn->xindex>-hcount)
2446 hmap[-btn->xindex] = 1;
2448 case C_SE:
2449 for (i=0; i<dock->max_icons; i++) {
2450 btn = dock->icon_array[i];
2451 if (!btn)
2452 continue;
2454 if (btn->xindex==0 && btn->yindex < 0 && btn->yindex > -vcount)
2455 vmap[-btn->yindex] = 1;
2456 else if (btn->yindex==0 && btn->xindex>0 && btn->xindex<hcount)
2457 hmap[btn->xindex] = 1;
2459 case C_SW:
2460 default:
2461 for (i=0; i<dock->max_icons; i++) {
2462 btn = dock->icon_array[i];
2463 if (!btn)
2464 continue;
2466 if (btn->xindex==0 && btn->yindex < 0 && btn->yindex > -vcount)
2467 vmap[-btn->yindex] = 1;
2468 else if (btn->yindex==0 && btn->xindex<0 &&btn->xindex>-hcount)
2469 hmap[-btn->xindex] = 1;
2472 x=0; y=0;
2473 done = 0;
2474 /* search a vacant slot */
2475 for (i=1; i<MAX(vcount, hcount); i++) {
2476 if (i < vcount && vmap[i]==0) {
2477 /* found a slot */
2478 x = 0;
2479 y = i;
2480 done = 1;
2481 break;
2482 } else if (i < hcount && hmap[i]==0) {
2483 /* found a slot */
2484 x = i;
2485 y = 0;
2486 done = 1;
2487 break;
2490 free(vmap);
2491 free(hmap);
2492 /* If found a slot, translate and return */
2493 if (done) {
2494 if (corner==C_NW || corner==C_NE) {
2495 *y_pos = y;
2496 } else {
2497 *y_pos = -y;
2499 if (corner==C_NE || corner==C_SE) {
2500 *x_pos = x;
2501 } else {
2502 *x_pos = -x;
2504 return True;
2506 /* else, try to find a slot somewhere else */
2509 /* a map of mwidth x mwidth would be enough if we allowed icons to be
2510 * placed outside of screen */
2511 mwidth = (int)ceil(sqrt(dock->max_icons));
2513 /* In the worst case (the clip is in the corner of the screen),
2514 * the amount of icons that fit in the clip is smaller.
2515 * Double the map to get a safe value.
2517 mwidth += mwidth;
2519 r = (mwidth-1)/2;
2521 slot_map = wmalloc(mwidth*mwidth);
2522 memset(slot_map, 0, mwidth*mwidth);
2524 #define XY2OFS(x,y) (MAX(abs(x),abs(y)) > r) ? 0 : (((y)+r)*(mwidth)+(x)+r)
2526 /* mark used slots in the map. If the slot falls outside the map
2527 * (for example, when all icons are placed in line), ignore them. */
2528 for (i=0; i<dock->max_icons; i++) {
2529 btn = dock->icon_array[i];
2530 if (btn)
2531 slot_map[XY2OFS(btn->xindex, btn->yindex)] = 1;
2533 /* Find closest slot from the center that is free by scanning the
2534 * map from the center to outward in circular passes.
2535 * This will not result in a neat layout, but will be optimal
2536 * in the sense that there will not be holes left.
2538 done = 0;
2539 for (i = 1; i <= r && !done; i++) {
2540 int tx, ty;
2542 /* top and bottom parts of the ring */
2543 for (x = -i; x <= i && !done; x++) {
2544 tx = dock->x_pos + x*ICON_SIZE;
2545 y = -i;
2546 ty = dock->y_pos + y*ICON_SIZE;
2547 if (slot_map[XY2OFS(x,y)]==0
2548 && ON_SCREEN(tx, ty, sx, ex, sy, ey)) {
2549 *x_pos = x;
2550 *y_pos = y;
2551 done = 1;
2552 break;
2554 y = i;
2555 ty = dock->y_pos + y*ICON_SIZE;
2556 if (slot_map[XY2OFS(x,y)]==0
2557 && ON_SCREEN(tx, ty, sx, ex, sy, ey)) {
2558 *x_pos = x;
2559 *y_pos = y;
2560 done = 1;
2561 break;
2564 /* left and right parts of the ring */
2565 for (y = -i+1; y <= i-1; y++) {
2566 ty = dock->y_pos + y*ICON_SIZE;
2567 x = -i;
2568 tx = dock->x_pos + x*ICON_SIZE;
2569 if (slot_map[XY2OFS(x,y)]==0
2570 && ON_SCREEN(tx, ty, sx, ex, sy, ey)) {
2571 *x_pos = x;
2572 *y_pos = y;
2573 done = 1;
2574 break;
2576 x = i;
2577 tx = dock->x_pos + x*ICON_SIZE;
2578 if (slot_map[XY2OFS(x,y)]==0
2579 && ON_SCREEN(tx, ty, sx, ex, sy, ey)) {
2580 *x_pos = x;
2581 *y_pos = y;
2582 done = 1;
2583 break;
2587 free(slot_map);
2588 #undef XY2OFS
2589 return done;
2593 static void
2594 moveDock(WDock *dock, int new_x, int new_y)
2596 WAppIcon *btn;
2597 int i;
2599 dock->x_pos = new_x;
2600 dock->y_pos = new_y;
2601 for (i=0; i<dock->max_icons; i++) {
2602 btn = dock->icon_array[i];
2603 if (btn) {
2604 btn->x_pos = new_x + btn->xindex*ICON_SIZE;
2605 btn->y_pos = new_y + btn->yindex*ICON_SIZE;
2606 XMoveWindow(dpy, btn->icon->core->window, btn->x_pos, btn->y_pos);
2612 static void
2613 swapDock(WDock *dock)
2615 WScreen *scr = dock->screen_ptr;
2616 WAppIcon *btn;
2617 int x, i;
2620 if (dock->on_right_side) {
2621 x = dock->x_pos = scr->scr_width - ICON_SIZE - DOCK_EXTRA_SPACE;
2622 } else {
2623 x = dock->x_pos = DOCK_EXTRA_SPACE;
2626 for (i=0; i<dock->max_icons; i++) {
2627 btn = dock->icon_array[i];
2628 if (btn) {
2629 btn->x_pos = x;
2630 XMoveWindow(dpy, btn->icon->core->window, btn->x_pos, btn->y_pos);
2636 static pid_t
2637 execCommand(WAppIcon *btn, char *command, WSavedState *state)
2639 WScreen *scr = btn->icon->core->screen_ptr;
2640 pid_t pid;
2641 char **argv;
2642 int argc;
2643 char *cmdline;
2645 cmdline = ExpandOptions(scr, command);
2647 if (scr->flags.dnd_data_convertion_status || !cmdline) {
2648 if (cmdline)
2649 free(cmdline);
2650 if (state)
2651 free(state);
2652 return 0;
2655 ParseCommand(cmdline, &argv, &argc);
2657 if (argv==NULL) {
2658 if (cmdline)
2659 free(cmdline);
2660 if (state)
2661 free(state);
2662 return 0;
2665 if ((pid=fork())==0) {
2666 char **args;
2667 int i;
2669 SetupEnvironment(scr);
2671 close(ConnectionNumber(dpy));
2673 #ifdef HAVE_SETPGID
2674 setpgid(0, 0);
2675 #endif
2677 args = malloc(sizeof(char*)*(argc+1));
2678 if (!args)
2679 exit(111);
2680 for (i=0; i<argc; i++) {
2681 args[i] = argv[i];
2683 args[argc] = NULL;
2684 execvp(argv[0], args);
2685 exit(111);
2687 while (argc > 0)
2688 free(argv[--argc]);
2689 free(argv);
2691 if (pid > 0) {
2692 if (!state) {
2693 state = wmalloc(sizeof(WSavedState));
2694 memset(state, 0, sizeof(WSavedState));
2695 state->hidden = -1;
2696 state->miniaturized = -1;
2697 state->shaded = -1;
2698 if (btn->dock == scr->dock)
2699 state->workspace = -1;
2700 else
2701 state->workspace = scr->current_workspace;
2703 wWindowAddSavedState(btn->wm_instance, btn->wm_class, cmdline, pid,
2704 state);
2705 wAddDeathHandler(pid, (WDeathHandler*)trackDeadProcess,
2706 btn->dock);
2707 } else if (state) {
2708 free(state);
2710 free(cmdline);
2711 return pid;
2715 void
2716 wDockHideIcons(WDock *dock)
2718 int i;
2719 WAppIcon *btn;
2721 if (dock==NULL)
2722 return;
2724 btn = dock->icon_array[0];
2726 for (i=1; i<dock->max_icons; i++) {
2727 if (dock->icon_array[i])
2728 XUnmapWindow(dpy, dock->icon_array[i]->icon->core->window);
2730 dock->mapped = 0;
2732 dockIconPaint(btn);
2736 void
2737 wDockShowIcons(WDock *dock)
2739 int i, newlevel;
2740 WAppIcon *btn;
2742 if (dock==NULL)
2743 return;
2745 btn = dock->icon_array[0];
2746 moveDock(dock, btn->x_pos, btn->y_pos);
2748 newlevel = dock->lowered ? WMNormalWindowLevel : WMDockWindowLevel;
2749 ChangeStackingLevel(btn->icon->core, newlevel);
2751 for (i=1; i<dock->max_icons; i++) {
2752 if (dock->icon_array[i]) {
2753 MoveInStackListAbove(dock->icon_array[i]->icon->core,
2754 btn->icon->core);
2755 break;
2759 if (!dock->collapsed) {
2760 for (i=1; i<dock->max_icons; i++) {
2761 if (dock->icon_array[i]) {
2762 XMapWindow(dpy, dock->icon_array[i]->icon->core->window);
2766 dock->mapped = 1;
2768 dockIconPaint(btn);
2772 void
2773 wDockLower(WDock *dock)
2775 int i;
2777 for (i=0; i<dock->max_icons; i++) {
2778 if (dock->icon_array[i])
2779 wLowerFrame(dock->icon_array[i]->icon->core);
2784 void
2785 wDockRaise(WDock *dock)
2787 int i;
2789 for (i=dock->max_icons-1; i>=0; i--) {
2790 if (dock->icon_array[i])
2791 wRaiseFrame(dock->icon_array[i]->icon->core);
2796 void
2797 wDockRaiseLower(WDock *dock)
2799 if (!dock->icon_array[0]->icon->core->stacking->above
2800 ||(dock->icon_array[0]->icon->core->stacking->window_level
2801 !=dock->icon_array[0]->icon->core->stacking->above->stacking->window_level))
2802 wDockLower(dock);
2803 else
2804 wDockRaise(dock);
2808 void
2809 wDockFinishLaunch(WDock *dock, WAppIcon *icon)
2811 icon->launching = 0;
2812 icon->relaunching = 0;
2813 dockIconPaint(icon);
2817 WAppIcon*
2818 wDockFindIconFor(WDock *dock, Window window)
2820 WAppIcon *icon;
2821 int i;
2823 for (i=0; i<dock->max_icons; i++) {
2824 icon = dock->icon_array[i];
2825 if (icon && icon->main_window == window)
2826 return icon;
2828 return NULL;
2832 void
2833 wDockTrackWindowLaunch(WDock *dock, Window window)
2835 WAppIcon *icon;
2836 #ifdef REDUCE_APPICONS
2837 WAppIconAppList *tapplist;
2838 #endif
2839 char *wm_class, *wm_instance;
2840 int i;
2843 if (!PropGetWMClass(window, &wm_class, &wm_instance) ||
2844 (!wm_class && !wm_instance))
2845 return;
2847 for (i=0; i<dock->max_icons; i++) {
2848 icon = dock->icon_array[i];
2849 if (!icon)
2850 continue;
2852 /* kluge. If this does not exist, some windows attach themselves
2853 * to more than one icon. Find out why */
2854 if (icon->main_window == window) {
2855 break;
2857 if ((icon->wm_instance || icon->wm_class)
2858 && (icon->launching
2859 || (dock->screen_ptr->flags.startup && !icon->running))) {
2861 if (icon->wm_instance && wm_instance &&
2862 strcmp(icon->wm_instance, wm_instance)!=0) {
2863 continue;
2865 if (icon->wm_class && wm_class &&
2866 strcmp(icon->wm_class, wm_class)!=0) {
2867 continue;
2870 if (!icon->relaunching) {
2871 WApplication *wapp;
2873 /* Possibly an application that was docked with dockit,
2874 * but the user did not update WMState to indicate that
2875 * it was docked by force */
2876 wapp = wApplicationOf(window);
2877 if (!wapp) {
2878 icon->forced_dock = 1;
2879 icon->running = 0;
2881 if (!icon->forced_dock)
2882 icon->main_window = window;
2883 #ifdef REDUCE_APPICONS
2884 tapplist = wmalloc(sizeof(WAppIconAppList));
2885 memset(tapplist, 0, sizeof(WAppIconAppList));
2886 tapplist->next = icon->applist;
2887 if (icon->applist)
2888 icon->applist->prev = tapplist;
2889 icon->applist = tapplist;
2890 tapplist->wapp = wApplicationOf(window);
2891 icon->num_apps++;
2892 #endif
2894 wDockFinishLaunch(dock, icon);
2895 break;
2899 if (wm_class)
2900 XFree(wm_class);
2901 if (wm_instance)
2902 XFree(wm_instance);
2907 void
2908 wClipUpdateForWorkspaceChange(WScreen *scr, int workspace)
2910 if (!wPreferences.flags.noclip) {
2911 scr->clip_icon->dock = scr->workspaces[workspace]->clip;
2912 if (scr->current_workspace != workspace) {
2913 WDock *old_clip = scr->workspaces[scr->current_workspace]->clip;
2915 wDockHideIcons(old_clip);
2916 if (old_clip->auto_collapse && !old_clip->collapsed)
2917 old_clip->collapsed = 1;
2918 wDockShowIcons(scr->workspaces[workspace]->clip);
2920 if (scr->flags.clip_balloon_mapped)
2921 showClipBalloon(scr->clip_icon->dock, workspace);
2927 static void
2928 trackDeadProcess(pid_t pid, unsigned char status, WDock *dock)
2930 WAppIcon *icon;
2931 int i;
2933 for (i=0; i<dock->max_icons; i++) {
2934 icon = dock->icon_array[i];
2935 if (!icon)
2936 continue;
2938 if (icon->launching && icon->pid == pid) {
2939 if (!icon->relaunching) {
2940 icon->running = 0;
2941 icon->main_window = None;
2943 wDockFinishLaunch(dock, icon);
2944 icon->pid = 0;
2945 if (status==111) {
2946 char msg[PATH_MAX];
2947 #ifdef OFFIX_DND
2948 sprintf(msg, _("Could not execute command \"%s\""),
2949 icon->drop_launch && icon->dnd_command
2950 ? icon->dnd_command : icon->command);
2951 #else
2952 sprintf(msg, _("Could not execute command \"%s\""),
2953 icon->command);
2954 #endif
2955 wMessageDialog(dock->screen_ptr, _("Error"), msg,
2956 _("OK"), NULL, NULL);
2958 break;
2964 static void
2965 toggleLowered(WDock *dock)
2967 WAppIcon *tmp;
2968 int newlevel, i;
2970 /* lower/raise Dock */
2971 if (!dock->lowered) {
2972 newlevel = WMNormalWindowLevel;
2973 dock->lowered = 1;
2974 } else {
2975 newlevel = WMDockWindowLevel;
2976 dock->lowered = 0;
2979 for (i=0; i<dock->max_icons; i++) {
2980 tmp = dock->icon_array[i];
2981 if (!tmp)
2982 continue;
2984 ChangeStackingLevel(tmp->icon->core, newlevel);
2985 if (dock->lowered)
2986 wLowerFrame(tmp->icon->core);
2991 static void
2992 toggleCollapsed(WDock *dock)
2994 if (dock->collapsed) {
2995 dock->collapsed = 0;
2996 wDockShowIcons(dock);
2998 else {
2999 dock->collapsed = 1;
3000 wDockHideIcons(dock);
3005 static void
3006 toggleAutoCollapse(WDock *dock)
3008 dock->auto_collapse = !dock->auto_collapse;
3009 if (dock->auto_collapse_magic) {
3010 WMDeleteTimerHandler(dock->auto_collapse_magic);
3011 dock->auto_collapse_magic = NULL;
3016 static void
3017 openDockMenu(WDock *dock, WAppIcon *aicon, XEvent *event)
3019 WScreen *scr = dock->screen_ptr;
3020 WObjDescriptor *desc;
3021 WMenuEntry *entry;
3022 int index = 0;
3023 int x_pos;
3024 int appIsRunning = aicon->running && aicon->icon && aicon->icon->owner;
3026 if (dock->type == WM_DOCK) {
3027 /* keep on top */
3028 entry = dock->menu->entries[index];
3029 entry->flags.indicator_on = !dock->lowered;
3030 entry->clientdata = dock;
3031 } else {
3032 /* clip options */
3033 if (scr->clip_options)
3034 updateClipOptionsMenu(scr->clip_options, dock);
3036 /* Rename Workspace */
3037 entry = dock->menu->entries[++index];
3038 entry->clientdata = dock;
3040 /* select icon */
3041 entry = dock->menu->entries[++index];
3042 entry->clientdata = aicon;
3043 wMenuSetEnabled(dock->menu, index, aicon!=scr->clip_icon);
3045 /* (un)select all icons */
3046 entry = dock->menu->entries[++index];
3047 entry->clientdata = aicon;
3048 wMenuSetEnabled(dock->menu, index, dock->icon_count > 1);
3050 /* keep icon(s) */
3051 entry = dock->menu->entries[++index];
3052 entry->clientdata = aicon;
3053 wMenuSetEnabled(dock->menu, index, dock->icon_count > 1);
3055 /* this is the workspace submenu part */
3056 if (scr->clip_submenu)
3057 updateWorkspaceMenu(scr->clip_submenu, aicon);
3058 index++;
3060 /* remove icon(s) */
3061 entry = dock->menu->entries[++index];
3062 entry->clientdata = aicon;
3063 wMenuSetEnabled(dock->menu, index, dock->icon_count > 1);
3065 /* attract icon(s) */
3066 entry = dock->menu->entries[++index];
3067 entry->clientdata = aicon;
3070 /* launch */
3071 entry = dock->menu->entries[++index];
3072 entry->clientdata = aicon;
3073 wMenuSetEnabled(dock->menu, index, aicon->command!=NULL);
3075 /* unhide here */
3076 entry = dock->menu->entries[++index];
3077 entry->clientdata = aicon;
3078 wMenuSetEnabled(dock->menu, index, appIsRunning);
3080 /* hide */
3081 entry = dock->menu->entries[++index];
3082 entry->clientdata = aicon;
3083 wMenuSetEnabled(dock->menu, index, appIsRunning);
3085 /* settings */
3086 entry = dock->menu->entries[++index];
3087 entry->clientdata = aicon;
3088 wMenuSetEnabled(dock->menu, index, !aicon->editing);
3090 /* kill */
3091 entry = dock->menu->entries[++index];
3092 entry->clientdata = aicon;
3093 wMenuSetEnabled(dock->menu, index, appIsRunning);
3095 if (!dock->menu->flags.realized)
3096 wMenuRealize(dock->menu);
3098 if (dock->type == WM_CLIP) {
3099 x_pos = event->xbutton.x_root+2;
3100 } else {
3101 x_pos = dock->on_right_side ?
3102 scr->scr_width - dock->menu->frame->core->width - 2 : 0;
3105 wMenuMapAt(dock->menu, x_pos, event->xbutton.y_root+2, False);
3107 /* allow drag select */
3108 event->xany.send_event = True;
3109 desc = &dock->menu->menu->descriptor;
3110 (*desc->handle_mousedown)(desc, event);
3114 static void
3115 openClipWorkspaceMenu(WScreen *scr, int x, int y)
3117 if (!scr->clip_ws_menu) {
3118 scr->clip_ws_menu = wWorkspaceMenuMake(scr, False);
3120 wWorkspaceMenuUpdate(scr, scr->clip_ws_menu);
3121 wMenuMapAt(scr->clip_ws_menu, x, y, False);
3125 /******************************************************************/
3126 static void
3127 iconDblClick(WObjDescriptor *desc, XEvent *event)
3129 WAppIcon *btn = desc->parent;
3130 WDock *dock = btn->dock;
3131 WApplication *wapp = NULL;
3132 int unhideHere = 0;
3134 #ifdef REDUCE_APPICONS
3135 if ((btn->icon->owner && !(event->xbutton.state & ControlMask)) ||
3136 ((btn->icon->owner == NULL) && (btn->applist != NULL))) {
3137 if (btn->icon->owner == NULL)
3138 btn->icon->owner = btn->applist->wapp->main_window_desc;
3139 #ifdef I_HATE_THIS
3141 #endif
3142 #else
3143 if (btn->icon->owner && !(event->xbutton.state & ControlMask)) {
3144 #endif
3145 wapp = wApplicationOf(btn->icon->owner->main_window);
3147 assert(wapp!=NULL);
3149 unhideHere = (event->xbutton.state & ShiftMask);
3151 /* go to the last workspace that the user worked on the app */
3152 if (!unhideHere) {
3153 wWorkspaceChange(dock->screen_ptr, wapp->last_workspace);
3156 wUnhideApplication(wapp, event->xbutton.button==Button2,
3157 unhideHere);
3159 if (event->xbutton.state & MOD_MASK) {
3160 wHideOtherApplications(btn->icon->owner);
3162 } else {
3163 if (event->xbutton.button==Button1) {
3165 if (event->xbutton.state & MOD_MASK) {
3166 /* raise/lower dock */
3167 toggleLowered(dock);
3168 } else if (btn == dock->screen_ptr->clip_icon) {
3169 if (getClipButton(event->xbutton.x, event->xbutton.y)==CLIP_IDLE)
3170 toggleCollapsed(dock);
3171 else
3172 handleClipChangeWorkspace(dock->screen_ptr, event);
3173 } else if (btn->command) {
3174 if (!btn->launching &&
3175 (!btn->running || (event->xbutton.state & ControlMask))) {
3176 launchDockedApplication(btn);
3184 static void
3185 handleDockMove(WDock *dock, WAppIcon *aicon, XEvent *event)
3187 WScreen *scr = dock->screen_ptr;
3188 int ofs_x=event->xbutton.x, ofs_y=event->xbutton.y;
3189 int x, y;
3190 XEvent ev;
3191 int grabbed = 0, swapped = 0, done;
3192 Pixmap ghost = None;
3193 int superfluous = wPreferences.superfluous; /* we catch it to avoid problems */
3195 #ifdef DEBUG
3196 puts("moving dock");
3197 #endif
3198 if (XGrabPointer(dpy, aicon->icon->core->window, True, ButtonMotionMask
3199 |ButtonReleaseMask|ButtonPressMask, GrabModeAsync,
3200 GrabModeAsync, None, None, CurrentTime) !=GrabSuccess) {
3201 wwarning("pointer grab failed for dock move");
3203 y = 0;
3204 for (x=0; x<dock->max_icons; x++) {
3205 if (dock->icon_array[x]!=NULL &&
3206 dock->icon_array[x]->yindex > y)
3207 y = dock->icon_array[x]->yindex;
3209 y++;
3210 XResizeWindow(dpy, scr->dock_shadow, ICON_SIZE, ICON_SIZE*y);
3212 done = 0;
3213 while (!done) {
3214 WMMaskEvent(dpy, PointerMotionMask|ButtonReleaseMask|ButtonPressMask
3215 |ButtonMotionMask|ExposureMask, &ev);
3216 switch (ev.type) {
3217 case Expose:
3218 WMHandleEvent(&ev);
3219 break;
3221 case MotionNotify:
3222 if (!grabbed) {
3223 if (abs(ofs_x-ev.xmotion.x)>=MOVE_THRESHOLD
3224 || abs(ofs_y-ev.xmotion.y)>=MOVE_THRESHOLD) {
3225 XChangeActivePointerGrab(dpy, ButtonMotionMask
3226 |ButtonReleaseMask|ButtonPressMask,
3227 wCursor[WCUR_MOVE], CurrentTime);
3228 grabbed=1;
3230 break;
3232 if (dock->type == WM_CLIP) {
3233 if (ev.xmotion.x_root - ofs_x < 0) {
3234 x = 0;
3235 } else if (ev.xmotion.x_root - ofs_x + ICON_SIZE >
3236 scr->scr_width) {
3237 x = scr->scr_width - ICON_SIZE;
3238 } else {
3239 x = ev.xmotion.x_root - ofs_x;
3241 if (ev.xmotion.y_root - ofs_y < 0) {
3242 y = 0;
3243 } else if (ev.xmotion.y_root - ofs_y + ICON_SIZE >
3244 scr->scr_height) {
3245 y = scr->scr_height - ICON_SIZE;
3246 } else {
3247 y = ev.xmotion.y_root - ofs_y;
3249 moveDock(dock, x, y);
3251 else {
3252 /* move vertically if pointer is inside the dock*/
3253 if ((dock->on_right_side &&
3254 ev.xmotion.x_root >= dock->x_pos - ICON_SIZE)
3255 || (!dock->on_right_side &&
3256 ev.xmotion.x_root <= dock->x_pos + ICON_SIZE*2)) {
3258 if (ev.xmotion.y_root - ofs_y < 0) {
3259 y = 0;
3260 } else if (ev.xmotion.y_root - ofs_y + ICON_SIZE >
3261 scr->scr_height) {
3262 y = scr->scr_height - ICON_SIZE;
3263 } else {
3264 y = ev.xmotion.y_root - ofs_y;
3266 moveDock(dock, dock->x_pos, y);
3268 /* move horizontally to change sides */
3269 x = ev.xmotion.x_root - ofs_x;
3270 if (!dock->on_right_side) {
3272 /* is on left */
3274 if (ev.xmotion.x_root > dock->x_pos + ICON_SIZE*2) {
3275 XMoveWindow(dpy, scr->dock_shadow, scr->scr_width-ICON_SIZE
3276 -DOCK_EXTRA_SPACE, dock->y_pos);
3277 if (superfluous) {
3278 if (ghost==None) {
3279 ghost = MakeGhostDock(dock, dock->x_pos,
3280 scr->scr_width-ICON_SIZE
3281 -DOCK_EXTRA_SPACE,
3282 dock->y_pos);
3283 XSetWindowBackgroundPixmap(dpy, scr->dock_shadow,
3284 ghost);
3285 XClearWindow(dpy, scr->dock_shadow);
3288 XMapRaised(dpy, scr->dock_shadow);
3289 swapped = 1;
3290 } else {
3291 if (superfluous && ghost!=None) {
3292 XFreePixmap(dpy, ghost);
3293 ghost = None;
3295 XUnmapWindow(dpy, scr->dock_shadow);
3296 swapped = 0;
3298 } else {
3299 /* is on right */
3300 if (ev.xmotion.x_root < dock->x_pos - ICON_SIZE) {
3301 XMoveWindow(dpy, scr->dock_shadow,
3302 DOCK_EXTRA_SPACE, dock->y_pos);
3303 if (superfluous) {
3304 if (ghost==None) {
3305 ghost = MakeGhostDock(dock, dock->x_pos,
3306 DOCK_EXTRA_SPACE, dock->y_pos);
3307 XSetWindowBackgroundPixmap(dpy, scr->dock_shadow,
3308 ghost);
3309 XClearWindow(dpy, scr->dock_shadow);
3312 XMapRaised(dpy, scr->dock_shadow);
3313 swapped = -1;
3314 } else {
3315 XUnmapWindow(dpy, scr->dock_shadow);
3316 swapped = 0;
3317 if (superfluous && ghost!=None) {
3318 XFreePixmap(dpy, ghost);
3319 ghost = None;
3324 break;
3326 case ButtonPress:
3327 break;
3329 case ButtonRelease:
3330 if (ev.xbutton.button != event->xbutton.button)
3331 break;
3332 XUngrabPointer(dpy, CurrentTime);
3333 XUnmapWindow(dpy, scr->dock_shadow);
3334 XResizeWindow(dpy, scr->dock_shadow, ICON_SIZE, ICON_SIZE);
3335 if (dock->type == WM_DOCK) {
3336 if (swapped!=0) {
3337 if (swapped>0)
3338 dock->on_right_side = 1;
3339 else
3340 dock->on_right_side = 0;
3341 swapDock(dock);
3342 wArrangeIcons(scr, False);
3345 done = 1;
3346 break;
3349 if (superfluous) {
3350 if (ghost!=None)
3351 XFreePixmap(dpy, ghost);
3352 XSetWindowBackground(dpy, scr->dock_shadow, scr->white_pixel);
3354 #ifdef DEBUG
3355 puts("End dock move");
3356 #endif
3361 static void
3362 handleIconMove(WDock *dock, WAppIcon *aicon, XEvent *event)
3364 WScreen *scr = dock->screen_ptr;
3365 Window wins[2];
3366 WIcon *icon = aicon->icon;
3367 WDock *dock2 = NULL, *last_dock = dock;
3368 int ondock, grabbed = 0, change_dock = 0, collapsed = 0;
3369 XEvent ev;
3370 int x = aicon->x_pos, y = aicon->y_pos;
3371 int ofs_x = event->xbutton.x, ofs_y = event->xbutton.y;
3372 int shad_x = x, shad_y = y;
3373 int ix = aicon->xindex, iy = aicon->yindex;
3374 int tmp;
3375 Pixmap ghost = None;
3376 Bool docked;
3377 int superfluous = wPreferences.superfluous; /* we catch it to avoid problems */
3379 if (XGrabPointer(dpy, icon->core->window, True, ButtonMotionMask
3380 |ButtonReleaseMask|ButtonPressMask, GrabModeAsync,
3381 GrabModeAsync, None, None, CurrentTime) !=GrabSuccess) {
3382 #ifdef DEBUG0
3383 wwarning("pointer grab failed for icon move");
3384 #endif
3387 wRaiseFrame(icon->core);
3389 if (dock == scr->dock && !wPreferences.flags.noclip)
3390 dock2 = scr->workspaces[scr->current_workspace]->clip;
3391 else if (dock != scr->dock && !wPreferences.flags.nodock)
3392 dock2 = scr->dock;
3394 wins[0] = icon->core->window;
3395 wins[1] = scr->dock_shadow;
3396 XRestackWindows(dpy, wins, 2);
3397 XMoveResizeWindow(dpy, scr->dock_shadow, aicon->x_pos, aicon->y_pos,
3398 ICON_SIZE, ICON_SIZE);
3399 if (superfluous) {
3400 if (icon->pixmap!=None)
3401 ghost = MakeGhostIcon(scr, icon->pixmap);
3402 else
3403 ghost = MakeGhostIcon(scr, icon->core->window);
3405 XSetWindowBackgroundPixmap(dpy, scr->dock_shadow, ghost);
3406 XClearWindow(dpy, scr->dock_shadow);
3408 XMapWindow(dpy, scr->dock_shadow);
3410 ondock = 1;
3413 while(1) {
3414 XMaskEvent(dpy, PointerMotionMask|ButtonReleaseMask|ButtonPressMask
3415 |ButtonMotionMask|ExposureMask, &ev);
3416 switch (ev.type) {
3417 case Expose:
3418 WMHandleEvent(&ev);
3419 break;
3421 case MotionNotify:
3422 if (!grabbed) {
3423 if (abs(ofs_x-ev.xmotion.x)>=MOVE_THRESHOLD
3424 || abs(ofs_y-ev.xmotion.y)>=MOVE_THRESHOLD) {
3425 XChangeActivePointerGrab(dpy, ButtonMotionMask
3426 |ButtonReleaseMask|ButtonPressMask,
3427 wCursor[WCUR_MOVE], CurrentTime);
3428 grabbed=1;
3429 } else {
3430 break;
3434 x = ev.xmotion.x_root - ofs_x;
3435 y = ev.xmotion.y_root - ofs_y;
3436 tmp = wDockSnapIcon(dock, aicon, x, y, &ix, &iy, True);
3437 if (tmp && dock2) {
3438 change_dock = 0;
3439 if (last_dock != dock && collapsed) {
3440 last_dock->collapsed = 1;
3441 wDockHideIcons(last_dock);
3442 collapsed = 0;
3444 if (!collapsed && (collapsed = dock->collapsed)) {
3445 dock->collapsed = 0;
3446 wDockShowIcons(dock);
3448 last_dock = dock;
3450 else if (dock2) {
3451 tmp = wDockSnapIcon(dock2, aicon, x, y, &ix, &iy, False);
3452 if (tmp) {
3453 change_dock = 1;
3454 if (last_dock != dock2 && collapsed) {
3455 last_dock->collapsed = 1;
3456 wDockHideIcons(last_dock);
3457 collapsed = 0;
3459 if (!collapsed && (collapsed = dock2->collapsed)) {
3460 dock2->collapsed = 0;
3461 wDockShowIcons(dock2);
3463 last_dock = dock2;
3466 if (aicon->launching
3467 || (aicon->running && !(ev.xmotion.state & MOD_MASK))
3468 || (!aicon->running && tmp)) {
3469 shad_x = last_dock->x_pos + ix*wPreferences.icon_size;
3470 shad_y = last_dock->y_pos + iy*wPreferences.icon_size;
3472 XMoveWindow(dpy, scr->dock_shadow, shad_x, shad_y);
3474 if (!ondock) {
3475 XMapWindow(dpy, scr->dock_shadow);
3476 #if 0
3477 if (!collapsed && (collapsed = last_dock->collapsed)) {
3478 last_dock->collapsed = 0;
3479 wDockShowIcons(last_dock);
3481 #endif
3483 ondock = 1;
3484 } else {
3485 if (ondock) {
3486 XUnmapWindow(dpy, scr->dock_shadow);
3487 #if 0
3488 if (last_dock && collapsed &&
3489 aicon->running && (ev.xmotion.state & MOD_MASK)) {
3490 last_dock->collapsed = 1;
3491 wDockHideIcons(last_dock);
3492 collapsed = 0;
3494 #endif
3496 ondock = 0;
3498 XMoveWindow(dpy, icon->core->window, x, y);
3499 break;
3501 case ButtonPress:
3502 break;
3504 case ButtonRelease:
3505 if (ev.xbutton.button != event->xbutton.button)
3506 break;
3507 XUngrabPointer(dpy, CurrentTime);
3508 if (ondock) {
3509 SlideWindow(icon->core->window, x, y, shad_x, shad_y);
3510 XUnmapWindow(dpy, scr->dock_shadow);
3511 if (!change_dock)
3512 reattachIcon(dock, aicon, ix, iy);
3513 else {
3514 docked = moveIconBetweenDocks(dock, dock2, aicon, ix, iy);
3515 if (!docked) {
3516 /* Slide it back if dock rejected it */
3517 SlideWindow(icon->core->window, x, y, aicon->x_pos,
3518 aicon->y_pos);
3519 reattachIcon(dock, aicon, aicon->xindex,aicon->yindex);
3521 if (last_dock->type==WM_CLIP && last_dock->auto_collapse) {
3522 collapsed = 0;
3525 } else {
3526 aicon->x_pos = x;
3527 aicon->y_pos = y;
3528 if (superfluous) {
3529 if (!aicon->running && !wPreferences.no_animations) {
3530 /* We need to deselect it, even if is deselected in
3531 * wDockDetach(), because else DoKaboom() will fail.
3533 if (aicon->icon->selected)
3534 wIconSelect(aicon->icon);
3535 DoKaboom(scr,aicon->icon->core->window, x, y);
3538 wDockDetach(dock, aicon);
3540 if (collapsed) {
3541 last_dock->collapsed = 1;
3542 wDockHideIcons(last_dock);
3543 collapsed = 0;
3545 if (superfluous) {
3546 if (ghost!=None)
3547 XFreePixmap(dpy, ghost);
3548 XSetWindowBackground(dpy, scr->dock_shadow, scr->white_pixel);
3550 #ifdef DEBUG
3551 puts("End icon move");
3552 #endif
3553 return;
3559 static int
3560 getClipButton(int px, int py)
3562 int pt = (CLIP_BUTTON_SIZE+2)*ICON_SIZE/64;
3564 if (px < 0 || py < 0 || px >= ICON_SIZE || py >= ICON_SIZE)
3565 return CLIP_IDLE;
3567 if (py <= pt-((int)ICON_SIZE-1-px))
3568 return CLIP_FORWARD;
3569 else if (px <= pt-((int)ICON_SIZE-1-py))
3570 return CLIP_REWIND;
3572 return CLIP_IDLE;
3576 static void
3577 handleClipChangeWorkspace(WScreen *scr, XEvent *event)
3579 XEvent ev;
3580 int done, direction, new_ws;
3581 int new_dir;
3582 WDock *clip = scr->clip_icon->dock;
3584 direction = getClipButton(event->xbutton.x, event->xbutton.y);
3586 clip->lclip_button_pushed = direction==CLIP_REWIND;
3587 clip->rclip_button_pushed = direction==CLIP_FORWARD;
3589 wClipIconPaint(scr->clip_icon);
3590 done = 0;
3591 while(!done) {
3592 WMMaskEvent(dpy, ExposureMask|ButtonMotionMask|ButtonReleaseMask
3593 |ButtonPressMask, &ev);
3594 switch (ev.type) {
3595 case Expose:
3596 WMHandleEvent(&ev);
3597 break;
3599 case MotionNotify:
3600 new_dir = getClipButton(ev.xmotion.x, ev.xmotion.y);
3601 if (new_dir != direction) {
3602 direction = new_dir;
3603 clip->lclip_button_pushed = direction==CLIP_REWIND;
3604 clip->rclip_button_pushed = direction==CLIP_FORWARD;
3605 wClipIconPaint(scr->clip_icon);
3607 break;
3609 case ButtonPress:
3610 break;
3612 case ButtonRelease:
3613 if (ev.xbutton.button == event->xbutton.button)
3614 done = 1;
3618 clip->lclip_button_pushed = 0;
3619 clip->rclip_button_pushed = 0;
3621 new_ws = wPreferences.ws_advance || (event->xbutton.state & ControlMask);
3623 if (direction == CLIP_FORWARD) {
3624 if (scr->current_workspace < scr->workspace_count-1)
3625 wWorkspaceChange(scr, scr->current_workspace+1);
3626 else if (new_ws && scr->current_workspace < MAX_WORKSPACES-1)
3627 wWorkspaceChange(scr, scr->current_workspace+1);
3628 else if (wPreferences.ws_cycle)
3629 wWorkspaceChange(scr, 0);
3631 else if (direction == CLIP_REWIND) {
3632 if (scr->current_workspace > 0)
3633 wWorkspaceChange(scr, scr->current_workspace-1);
3634 else if (scr->current_workspace==0 && wPreferences.ws_cycle)
3635 wWorkspaceChange(scr, scr->workspace_count-1);
3638 wClipIconPaint(scr->clip_icon);
3642 static void
3643 iconMouseDown(WObjDescriptor *desc, XEvent *event)
3645 WAppIcon *aicon = desc->parent;
3646 WDock *dock = aicon->dock;
3647 WScreen *scr = aicon->icon->core->screen_ptr;
3649 if (aicon->editing)
3650 return;
3652 scr->last_dock = dock;
3654 if (dock->menu->flags.mapped)
3655 wMenuUnmap(dock->menu);
3657 if (IsDoubleClick(scr, event)) {
3658 /* double-click was not in the main clip icon */
3659 if (dock->type != WM_CLIP || aicon->xindex!=0 || aicon->yindex!=0
3660 || getClipButton(event->xbutton.x, event->xbutton.y)==CLIP_IDLE) {
3661 iconDblClick(desc, event);
3662 return;
3666 if (dock->type == WM_CLIP && scr->flags.clip_balloon_mapped) {
3667 XUnmapWindow(dpy, scr->clip_balloon);
3668 scr->flags.clip_balloon_mapped = 0;
3671 #ifdef DEBUG
3672 puts("handling dock");
3673 #endif
3674 if (event->xbutton.button == Button1) {
3675 if (event->xbutton.state & MOD_MASK)
3676 wDockLower(dock);
3677 else
3678 wDockRaise(dock);
3680 if ((event->xbutton.state & ShiftMask) && aicon!=scr->clip_icon &&
3681 dock->type!=WM_DOCK) {
3682 wIconSelect(aicon->icon);
3683 return;
3686 if (aicon->yindex==0 && aicon->xindex==0) {
3687 if (getClipButton(event->xbutton.x, event->xbutton.y)!=CLIP_IDLE
3688 && dock->type==WM_CLIP)
3689 handleClipChangeWorkspace(scr, event);
3690 else
3691 handleDockMove(dock, aicon, event);
3692 } else
3693 handleIconMove(dock, aicon, event);
3695 } else if (event->xbutton.button==Button2 && dock->type==WM_CLIP &&
3696 aicon->xindex==0 && aicon->yindex==0) {
3697 openClipWorkspaceMenu(scr, event->xbutton.x_root+2,
3698 event->xbutton.y_root+2);
3699 if (scr->clip_ws_menu) {
3700 WMenu *menu;
3701 menu = scr->clip_ws_menu;
3702 desc = &menu->menu->descriptor;
3704 event->xany.send_event = True;
3705 (*desc->handle_mousedown)(desc, event);
3707 } else if (event->xbutton.button == Button3) {
3708 openDockMenu(dock, aicon, event);
3713 static void
3714 showClipBalloon(WDock *dock, int workspace)
3716 int w, h;
3717 int x, y;
3718 WScreen *scr = dock->screen_ptr;
3719 char *text;
3720 Window stack[2];
3722 scr->flags.clip_balloon_mapped = 1;
3723 XMapWindow(dpy, scr->clip_balloon);
3725 text = scr->workspaces[workspace]->name;
3727 w = wTextWidth(scr->clip_title_font->font, text, strlen(text)) + 4;
3729 h = scr->clip_title_font->height;
3730 XResizeWindow(dpy, scr->clip_balloon, w, h);
3732 x = dock->x_pos + CLIP_BUTTON_SIZE*ICON_SIZE/64;
3733 y = dock->y_pos + ICON_SIZE-scr->clip_title_font->height - 3;
3735 if (x+w > scr->scr_width) {
3736 x = scr->scr_width - w;
3737 if (dock->y_pos + ICON_SIZE + h > scr->scr_height)
3738 y = dock->y_pos - h - 1;
3739 else
3740 y = dock->y_pos + ICON_SIZE;
3741 XRaiseWindow(dpy, scr->clip_balloon);
3742 } else {
3743 stack[0] = scr->clip_icon->icon->core->window;
3744 stack[1] = scr->clip_balloon;
3745 XRestackWindows(dpy, stack, 2);
3747 XMoveWindow(dpy, scr->clip_balloon, x, y);
3748 XSetForeground(dpy, scr->clip_title_gc,
3749 scr->clip_title_pixel[CLIP_NORMAL]);
3750 XClearWindow(dpy, scr->clip_balloon);
3751 wDrawString(scr->clip_balloon, scr->clip_title_font, scr->clip_title_gc,
3752 0, scr->clip_title_font->y, text, strlen(text));
3756 static void
3757 clipEnterNotify(WObjDescriptor *desc, XEvent *event)
3759 WAppIcon *btn = (WAppIcon*)desc->parent;
3760 WDock *dock;
3762 assert(event->type==EnterNotify);
3764 if(desc->parent_type!=WCLASS_DOCK_ICON)
3765 return;
3767 dock = btn->dock;
3768 if (!dock || dock->type!=WM_CLIP)
3769 return;
3771 if (btn->xindex == 0 && btn->yindex == 0)
3772 showClipBalloon(dock, dock->screen_ptr->current_workspace);
3773 else {
3774 if (dock->screen_ptr->flags.clip_balloon_mapped) {
3775 XUnmapWindow(dpy, dock->screen_ptr->clip_balloon);
3776 dock->screen_ptr->flags.clip_balloon_mapped = 0;
3779 if (!dock->auto_collapse)
3780 return;
3782 if (dock->auto_collapse_magic) {
3783 WMDeleteTimerHandler(dock->auto_collapse_magic);
3784 dock->auto_collapse_magic = NULL;
3787 if (dock->collapsed)
3788 toggleCollapsed(dock);
3792 static void
3793 clipLeave(WDock *dock)
3795 if (!dock || dock->type!=WM_CLIP || !dock->auto_collapse)
3796 return;
3798 if (dock->auto_collapse_magic) {
3799 WMDeleteTimerHandler(dock->auto_collapse_magic);
3800 dock->auto_collapse_magic = NULL;
3802 if (!dock->collapsed) {
3803 dock->auto_collapse_magic = WMAddTimerHandler(AUTO_COLLAPSE_DELAY,
3804 clipAutoCollapse,
3805 (void *)dock);
3810 static void
3811 clipLeaveNotify(WObjDescriptor *desc, XEvent *event)
3813 WAppIcon *btn = (WAppIcon*)desc->parent;
3815 assert(event->type==LeaveNotify);
3817 if(desc->parent_type!=WCLASS_DOCK_ICON)
3818 return;
3820 clipLeave(btn->dock);
3824 static void
3825 clipAutoCollapse(void *cdata)
3827 WDock *dock = (WDock *)cdata;
3829 if (dock->type!=WM_CLIP || !dock->auto_collapse)
3830 return;
3832 if (!dock->collapsed && dock->auto_collapse_magic!=NULL) {
3833 toggleCollapsed(dock);
3835 dock->auto_collapse_magic = NULL;