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