fixed many bugs, removed linked list
[wmaker-crm.git] / src / dock.c
bloba559e4fc8f01e7fcffa33ef0b58b60100b10decb
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"
58 #include <proplist.h>
63 /**** Local variables ****/
64 #define CLIP_REWIND 1
65 #define CLIP_IDLE 0
66 #define CLIP_FORWARD 2
69 /**** Global variables ****/
71 /* in dockedapp.c */
72 extern void DestroyDockAppSettingsPanel();
74 extern void ShowDockAppSettingsPanel(WAppIcon *aicon);
77 extern XContext wWinContext;
79 extern Cursor wCursor[WCUR_LAST];
81 extern WPreferences wPreferences;
83 extern XContext wWinContext;
85 #ifdef OFFIX_DND
86 extern Atom _XA_DND_PROTOCOL;
87 #endif
90 #define MOD_MASK wPreferences.modifier_mask
92 extern void appIconMouseDown(WObjDescriptor *desc, XEvent *event);
94 #define ICON_SIZE wPreferences.icon_size
97 /***** Local variables ****/
99 static proplist_t dCommand=NULL;
100 #ifdef OFFIX_DND
101 static proplist_t dDropCommand=NULL;
102 #endif
103 static proplist_t dAutoLaunch, dName, dForced, dBuggyApplication, dYes, dNo;
104 static proplist_t dHost, dDock, dClip;
105 static proplist_t dAutoAttractIcons, dKeepAttracted;
107 static proplist_t dPosition, dApplications, dLowered, dCollapsed, dAutoCollapse;
109 static proplist_t dAutoRaiseLower, dOmnipresent;
111 static void dockIconPaint(WAppIcon *btn);
113 static void iconMouseDown(WObjDescriptor *desc, XEvent *event);
115 static pid_t execCommand(WAppIcon *btn, char *command, WSavedState *state);
117 static void trackDeadProcess(pid_t pid, unsigned char status, WDock *dock);
119 static int getClipButton(int px, int py);
121 static void toggleLowered(WDock *dock);
123 static void toggleCollapsed(WDock *dock);
125 static void clipIconExpose(WObjDescriptor *desc, XEvent *event);
127 static void clipLeave(WDock *dock);
129 static void handleClipChangeWorkspace(WScreen *scr, XEvent *event);
131 Bool moveIconBetweenDocks(WDock *src, WDock *dest, WAppIcon *icon, int x, int y);
133 static void clipEnterNotify(WObjDescriptor *desc, XEvent *event);
134 static void clipLeaveNotify(WObjDescriptor *desc, XEvent *event);
135 static void clipAutoCollapse(void *cdata);
136 static void clipAutoExpand(void *cdata);
137 static void launchDockedApplication(WAppIcon *btn);
139 static void clipAutoLower(void *cdata);
140 static void clipAutoRaise(void *cdata);
142 static void showClipBalloon(WDock *dock, int workspace);
144 #ifdef OFFIX_DND
146 #define DndNotDnd -1
147 #define DndUnknown 0
148 #define DndRawData 1
149 #define DndFile 2
150 #define DndFiles 3
151 #define DndText 4
152 #define DndDir 5
153 #define DndLink 6
154 #define DndExe 7
156 #define DndEND 8
158 #endif /* OFFIX_DND */
162 static void
163 make_keys()
165 if (dCommand!=NULL)
166 return;
168 dCommand = PLRetain(PLMakeString("Command"));
169 #ifdef OFFIX_DND
170 dDropCommand = PLRetain(PLMakeString("DropCommand"));
171 #endif
172 dAutoLaunch = PLRetain(PLMakeString("AutoLaunch"));
173 dName = PLRetain(PLMakeString("Name"));
174 dForced = PLRetain(PLMakeString("Forced"));
175 dBuggyApplication = PLRetain(PLMakeString("BuggyApplication"));
176 dYes = PLRetain(PLMakeString("Yes"));
177 dNo = PLRetain(PLMakeString("No"));
178 dHost = PLRetain(PLMakeString("Host"));
180 dPosition = PLMakeString("Position");
181 dApplications = PLMakeString("Applications");
182 dLowered = PLMakeString("Lowered");
183 dCollapsed = PLMakeString("Collapsed");
184 dAutoCollapse = PLMakeString("AutoCollapse");
185 dAutoRaiseLower = PLMakeString("AutoRaiseLower");
186 dAutoAttractIcons = PLMakeString("AutoAttractIcons");
187 dKeepAttracted = PLMakeString("KeepAttracted");
189 dOmnipresent = PLMakeString("Omnipresent");
191 dDock = PLMakeString("Dock");
192 dClip = PLMakeString("Clip");
197 static void
198 renameCallback(WMenu *menu, WMenuEntry *entry)
200 WDock *dock = entry->clientdata;
201 char buffer[128];
202 int wspace;
203 char *name;
205 assert(entry->clientdata!=NULL);
207 wspace = dock->screen_ptr->current_workspace;
209 name = wstrdup(dock->screen_ptr->workspaces[wspace]->name);
211 sprintf(buffer, _("Type the name for workspace %i:"), wspace+1);
212 if (wInputDialog(dock->screen_ptr, _("Rename Workspace"), buffer,
213 &name)) {
214 wWorkspaceRename(dock->screen_ptr, wspace, name);
216 if (name) {
217 free(name);
222 static void
223 toggleLoweredCallback(WMenu *menu, WMenuEntry *entry)
225 assert(entry->clientdata!=NULL);
227 toggleLowered(entry->clientdata);
229 entry->flags.indicator_on = !((WDock*)entry->clientdata)->lowered;
231 wMenuPaint(menu);
236 static void
237 killCallback(WMenu *menu, WMenuEntry *entry)
239 WAppIcon *icon;
240 #ifdef REDUCE_APPICONS
241 WAppIconAppList *tapplist;
243 extern Atom _XA_WM_DELETE_WINDOW;
244 #else
245 char *buffer;
246 #endif
248 if (!WCHECK_STATE(WSTATE_NORMAL))
249 return;
251 assert(entry->clientdata!=NULL);
253 icon = (WAppIcon*)entry->clientdata;
255 icon->editing = 1;
257 WCHANGE_STATE(WSTATE_MODAL);
259 #ifdef REDUCE_APPICONS
260 /* Send a delete message to the main window of each application
261 * bound to this docked appicon. - cls
263 tapplist = icon->applist;
264 while (tapplist != NULL) {
265 if (tapplist->wapp->main_window_desc != NULL) {
266 if (tapplist->wapp->main_window_desc->protocols.DELETE_WINDOW) {
267 wClientSendProtocol(tapplist->wapp->main_window_desc,
268 _XA_WM_DELETE_WINDOW, CurrentTime);
269 } else {
270 wClientKill(tapplist->wapp->main_window_desc);
273 tapplist = tapplist->next;
275 #else
276 buffer = wstrappend(icon->wm_class,
277 _(" will be forcibly closed.\n"
278 "Any unsaved changes will be lost.\n"
279 "Please confirm."));
281 if (wPreferences.dont_confirm_kill
282 || wMessageDialog(menu->frame->screen_ptr, _("Kill Application"),
283 buffer, _("Yes"), _("No"), NULL)==WAPRDefault) {
284 if (icon->icon && icon->icon->owner) {
285 wClientKill(icon->icon->owner);
288 #endif /* !REDUCE_APPICONS */
290 icon->editing = 0;
292 WCHANGE_STATE(WSTATE_NORMAL);
296 /* TODO: replace this function with a member of the dock struct */
297 static int
298 numberOfSelectedIcons(WDock *dock)
300 WAppIcon *aicon;
301 int i, n;
303 n = 0;
304 for (i=1; i<dock->max_icons; i++) {
305 aicon = dock->icon_array[i];
306 if (aicon && aicon->icon->selected) {
307 n++;
311 return n;
315 static WMBag*
316 getSelected(WDock *dock)
318 WMBag *ret = WMCreateBag(8);
319 WAppIcon *btn;
320 int i;
322 for (i=1; i<dock->max_icons; i++) {
323 btn = dock->icon_array[i];
324 if (btn && btn->icon->selected) {
325 WMPutInBag(ret, btn);
329 return ret;
333 static void
334 paintClipButtons(WAppIcon *clipIcon, Bool lpushed, Bool rpushed)
336 Window win = clipIcon->icon->core->window;
337 WScreen *scr = clipIcon->icon->core->screen_ptr;
338 XPoint p[4];
339 int pt = CLIP_BUTTON_SIZE*ICON_SIZE/64;
340 int tp = ICON_SIZE - pt;
341 int as = pt - 15; /* 15 = 5+5+5 */
342 GC gc = scr->clip_title_gc;
343 #ifdef GRADIENT_CLIP_ARROW
344 Bool collapsed = clipIcon->dock->collapsed;
345 #endif
347 if (rpushed) {
348 p[0].x = tp+1;
349 p[0].y = 1;
350 p[1].x = ICON_SIZE-2;
351 p[1].y = 1;
352 p[2].x = ICON_SIZE-2;
353 p[2].y = pt-1;
354 } else if (lpushed) {
355 p[0].x = 1;
356 p[0].y = tp;
357 p[1].x = pt;
358 p[1].y = ICON_SIZE-2;
359 p[2].x = 1;
360 p[2].y = ICON_SIZE-2;
362 if (lpushed || rpushed) {
363 XSetForeground(dpy, scr->draw_gc, scr->white_pixel);
364 XFillPolygon(dpy, win, scr->draw_gc, p, 3, Convex, CoordModeOrigin);
365 XSetForeground(dpy, scr->draw_gc, scr->black_pixel);
367 #ifdef GRADIENT_CLIP_ARROW
368 if (!collapsed) {
369 XSetFillStyle(dpy, scr->copy_gc, FillTiled);
370 XSetTile(dpy, scr->copy_gc, scr->clip_arrow_gradient);
371 XSetClipMask(dpy, scr->copy_gc, None);
372 gc = scr->copy_gc;
374 #endif /* GRADIENT_CLIP_ARROW */
376 /* top right arrow */
377 p[0].x = p[3].x = ICON_SIZE-5-as;
378 p[0].y = p[3].y = 5;
379 p[1].x = ICON_SIZE-6;
380 p[1].y = 5;
381 p[2].x = ICON_SIZE-6;
382 p[2].y = 4+as;
383 if (rpushed) {
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, ICON_SIZE-6-as, 5);
390 #endif
391 XFillPolygon(dpy, win, gc, p,3,Convex,CoordModeOrigin);
392 XDrawLines(dpy, win, gc, p,4,CoordModeOrigin);
395 /* bottom left arrow */
396 p[0].x = p[3].x = 5;
397 p[0].y = p[3].y = ICON_SIZE-5-as;
398 p[1].x = 5;
399 p[1].y = ICON_SIZE-6;
400 p[2].x = 4+as;
401 p[2].y = ICON_SIZE-6;
402 if (lpushed) {
403 XFillPolygon(dpy, win, scr->draw_gc, p, 3, Convex, CoordModeOrigin);
404 XDrawLines(dpy, win, scr->draw_gc, p, 4, CoordModeOrigin);
405 } else {
406 #ifdef GRADIENT_CLIP_ARROW
407 if (!collapsed)
408 XSetTSOrigin(dpy, gc, 5, ICON_SIZE-6-as);
409 #endif
410 XFillPolygon(dpy, win, gc, p,3,Convex,CoordModeOrigin);
411 XDrawLines(dpy, win, gc, p,4,CoordModeOrigin);
413 #ifdef GRADIENT_CLIP_ARROW
414 if (!collapsed)
415 XSetFillStyle(dpy, scr->copy_gc, FillSolid);
416 #endif
420 RImage*
421 wClipMakeTile(WScreen *scr, RImage *normalTile)
423 RImage *tile = RCloneImage(normalTile);
424 RColor black;
425 RColor dark;
426 RColor light;
427 int pt, tp;
428 int as;
430 pt = CLIP_BUTTON_SIZE*wPreferences.icon_size/64;
431 tp = wPreferences.icon_size-1 - pt;
432 as = pt - 15;
434 black.alpha = 255;
435 black.red = black.green = black.blue = 0;
437 dark.alpha = 0;
438 dark.red = dark.green = dark.blue = 60;
440 light.alpha = 0;
441 light.red = light.green = light.blue = 80;
444 /* top right */
445 ROperateLine(tile, RSubtractOperation, tp, 0, wPreferences.icon_size-2,
446 pt-1, &dark);
447 RDrawLine(tile, tp-1, 0, wPreferences.icon_size-1, pt+1, &black);
448 ROperateLine(tile, RAddOperation, tp, 2, wPreferences.icon_size-3,
449 pt, &light);
451 /* arrow bevel */
452 ROperateLine(tile, RSubtractOperation, ICON_SIZE - 7 - as, 4,
453 ICON_SIZE - 5, 4, &dark);
454 ROperateLine(tile, RSubtractOperation, ICON_SIZE - 6 - as, 5,
455 ICON_SIZE - 5, 6 + as, &dark);
456 ROperateLine(tile, RAddOperation, ICON_SIZE - 5, 4, ICON_SIZE - 5, 6 + as,
457 &light);
459 /* bottom left */
460 ROperateLine(tile, RAddOperation, 2, tp+2, pt-2,
461 wPreferences.icon_size-3, &dark);
462 RDrawLine(tile, 0, tp-1, pt+1, wPreferences.icon_size-1, &black);
463 ROperateLine(tile, RSubtractOperation, 0, tp-2, pt+1,
464 wPreferences.icon_size-2, &light);
466 /* arrow bevel */
467 ROperateLine(tile, RSubtractOperation, 4, ICON_SIZE - 7 - as, 4,
468 ICON_SIZE - 5, &dark);
469 ROperateLine(tile, RSubtractOperation, 5, ICON_SIZE - 6 - as,
470 6 + as, ICON_SIZE - 5, &dark);
471 ROperateLine(tile, RAddOperation, 4, ICON_SIZE - 5, 6 + as, ICON_SIZE - 5,
472 &light);
474 return tile;
478 static void
479 omnipresentCallback(WMenu *menu, WMenuEntry *entry)
481 WAppIcon *clickedIcon = entry->clientdata;
482 WAppIcon *aicon;
483 WDock *dock;
484 WMBag *selectedIcons;
485 int failed;
486 int i;
488 assert(entry->clientdata!=NULL);
490 dock = clickedIcon->dock;
492 selectedIcons = getSelected(dock);
494 if (!WMGetBagItemCount(selectedIcons))
495 WMPutInBag(selectedIcons, clickedIcon);
497 failed = 0;
498 for (i = 0; i < WMGetBagItemCount(selectedIcons); i++) {
499 aicon = WMGetFromBag(selectedIcons, i);
501 if (wClipMakeIconOmnipresent(aicon, !aicon->omnipresent) == WO_FAILED)
502 failed++;
503 else if (aicon->icon->selected)
504 wIconSelect(aicon->icon);
506 WMFreeBag(selectedIcons);
508 if (failed > 1) {
509 wMessageDialog(dock->screen_ptr, _("Warning"),
510 _("Some icons cannot be made omnipresent. "
511 "Please make sure that no other icon is "
512 "docked in the same positions on the other "
513 "workspaces and the Clip is not full in "
514 "some workspace."),
515 _("OK"), NULL, NULL);
516 } else if (failed == 1) {
517 wMessageDialog(dock->screen_ptr, _("Warning"),
518 _("Icon cannot be made omnipresent. "
519 "Please make sure that no other icon is "
520 "docked in the same position on the other "
521 "workspaces and the Clip is not full in "
522 "some workspace."),
523 _("OK"), NULL, NULL);
528 static void
529 removeIconsCallback(WMenu *menu, WMenuEntry *entry)
531 WAppIcon *clickedIcon = (WAppIcon*)entry->clientdata;
532 WDock *dock;
533 WAppIcon *aicon;
534 WMBag *selectedIcons;
535 int keepit;
536 int i;
538 assert(clickedIcon!=NULL);
540 dock = clickedIcon->dock;
542 selectedIcons = getSelected(dock);
544 if (WMGetBagItemCount(selectedIcons)) {
545 if (wMessageDialog(dock->screen_ptr, _("Workspace Clip"),
546 _("All selected icons will be removed!"),
547 _("OK"), _("Cancel"), NULL)!=WAPRDefault) {
548 WMFreeBag(selectedIcons);
549 return;
551 } else {
552 if (clickedIcon->xindex==0 && clickedIcon->yindex==0) {
553 WMFreeBag(selectedIcons);
554 return;
556 WMPutInBag(selectedIcons, clickedIcon);
559 for (i = 0; i < WMGetBagItemCount(selectedIcons); i++) {
560 aicon = WMGetFromBag(selectedIcons, i);
561 keepit = aicon->running && wApplicationOf(aicon->main_window);
562 wDockDetach(dock, aicon);
563 if (keepit) {
564 PlaceIcon(dock->screen_ptr, &aicon->x_pos, &aicon->y_pos);
565 XMoveWindow(dpy, aicon->icon->core->window,
566 aicon->x_pos, aicon->y_pos);
567 if (!dock->mapped || dock->collapsed)
568 XMapWindow(dpy, aicon->icon->core->window);
571 WMFreeBag(selectedIcons);
573 if (wPreferences.auto_arrange_icons)
574 wArrangeIcons(dock->screen_ptr, True);
578 static void
579 keepIconsCallback(WMenu *menu, WMenuEntry *entry)
581 WAppIcon *clickedIcon = (WAppIcon*)entry->clientdata;
582 WDock *dock;
583 WAppIcon *aicon;
584 WMBag *selectedIcons;
585 int i;
587 assert(clickedIcon!=NULL);
588 dock = clickedIcon->dock;
590 selectedIcons = getSelected(dock);
592 if (!WMGetBagItemCount(selectedIcons)
593 && clickedIcon!=dock->screen_ptr->clip_icon) {
594 char *command = NULL;
596 if (!clickedIcon->command && !clickedIcon->editing) {
597 clickedIcon->editing = 1;
598 if (wInputDialog(dock->screen_ptr, _("Keep Icon"),
599 _("Type the command used to launch the application"),
600 &command)) {
601 if (command && (command[0]==0 ||
602 (command[0]=='-' && command[1]==0))) {
603 free(command);
604 command = NULL;
606 clickedIcon->command = command;
607 clickedIcon->editing = 0;
608 } else {
609 clickedIcon->editing = 0;
610 if (command)
611 free(command);
612 WMFreeBag(selectedIcons);
613 return;
617 WMPutInBag(selectedIcons, clickedIcon);
620 for (i = 0; i < WMGetBagItemCount(selectedIcons); i++) {
621 aicon = WMGetFromBag(selectedIcons, i);
622 if (aicon->icon->selected)
623 wIconSelect(aicon->icon);
624 if (aicon && aicon->attracted && aicon->command) {
625 aicon->attracted = 0;
626 if (aicon->icon->shadowed) {
627 aicon->icon->shadowed = 0;
628 aicon->icon->force_paint = 1;
629 wAppIconPaint(aicon);
633 WMFreeBag(selectedIcons);
639 static void
640 toggleAutoAttractCallback(WMenu *menu, WMenuEntry *entry)
642 WDock *dock = (WDock*)entry->clientdata;
644 assert(entry->clientdata!=NULL);
646 dock->attract_icons = !dock->attract_icons;
647 /*if (!dock->attract_icons)
648 dock->keep_attracted = 0;*/
650 entry->flags.indicator_on = dock->attract_icons;
652 wMenuPaint(menu);
656 static void
657 toggleKeepCallback(WMenu *menu, WMenuEntry *entry)
659 WDock *dock = (WDock*)entry->clientdata;
660 WAppIcon *btn;
661 int i;
663 assert(entry->clientdata!=NULL);
665 dock->keep_attracted = !dock->keep_attracted;
667 if (dock->keep_attracted) {
668 for (i=0; i< dock->max_icons; i++) {
669 btn = dock->icon_array[i];
670 if (btn && btn->attracted && btn->command) {
671 btn->attracted = 0;
672 if (btn->icon->shadowed) {
673 btn->icon->shadowed = 0;
674 btn->icon->force_paint = 1;
675 wAppIconPaint(btn);
681 entry->flags.indicator_on = dock->keep_attracted;
683 wMenuPaint(menu);
687 static void
688 selectCallback(WMenu *menu, WMenuEntry *entry)
690 WAppIcon *icon = (WAppIcon*)entry->clientdata;
692 assert(icon!=NULL);
694 wIconSelect(icon->icon);
696 wMenuPaint(menu);
700 static void
701 colectIconsCallback(WMenu *menu, WMenuEntry *entry)
703 WAppIcon *clickedIcon = (WAppIcon*)entry->clientdata;
704 WDock *clip;
705 WAppIcon *aicon;
706 int x, y, x_pos, y_pos;
708 assert(entry->clientdata!=NULL);
709 clip = clickedIcon->dock;
711 aicon = clip->screen_ptr->app_icon_list;
713 while (aicon) {
714 if (!aicon->docked && wDockFindFreeSlot(clip, &x, &y)) {
715 x_pos = clip->x_pos + x*ICON_SIZE;
716 y_pos = clip->y_pos + y*ICON_SIZE;
717 if (aicon->x_pos != x_pos || aicon->y_pos != y_pos) {
718 #ifdef ANIMATIONS
719 if (wPreferences.no_animations) {
720 XMoveWindow(dpy, aicon->icon->core->window, x_pos, y_pos);
721 } else {
722 SlideWindow(aicon->icon->core->window,
723 aicon->x_pos, aicon->y_pos, x_pos, y_pos);
725 #else
726 XMoveWindow(dpy, aicon->icon->core->window, x_pos, y_pos);
727 #endif /* ANIMATIONS */
729 aicon->attracted = 1;
730 if (!clip->keep_attracted && !aicon->icon->shadowed) {
731 aicon->icon->shadowed = 1;
732 aicon->icon->force_paint = 1;
733 /* We don't do an wAppIconPaint() here because it's in
734 * wDockAttachIcon(). -Dan
737 wDockAttachIcon(clip, aicon, x, y);
738 if (clip->collapsed || !clip->mapped)
739 XUnmapWindow(dpy, aicon->icon->core->window);
741 aicon = aicon->next;
746 static void
747 selectIconsCallback(WMenu *menu, WMenuEntry *entry)
749 WAppIcon *clickedIcon = (WAppIcon*)entry->clientdata;
750 WDock *dock;
751 WMBag *selectedIcons;
752 WAppIcon *btn;
753 int i;
755 assert(clickedIcon!=NULL);
756 dock = clickedIcon->dock;
758 selectedIcons = getSelected(dock);
760 if (!WMGetBagItemCount(selectedIcons)) {
761 for (i=1; i<dock->max_icons; i++) {
762 btn = dock->icon_array[i];
763 if (btn && !btn->icon->selected) {
764 wIconSelect(btn->icon);
767 } else {
768 for (i = 0; i < WMGetBagItemCount(selectedIcons); i++) {
769 btn = WMGetFromBag(selectedIcons, i);
770 wIconSelect(btn->icon);
773 WMFreeBag(selectedIcons);
775 wMenuPaint(menu);
779 static void
780 toggleCollapsedCallback(WMenu *menu, WMenuEntry *entry)
782 assert(entry->clientdata!=NULL);
784 toggleCollapsed(entry->clientdata);
786 entry->flags.indicator_on = ((WDock*)entry->clientdata)->collapsed;
788 wMenuPaint(menu);
792 static void
793 toggleAutoCollapseCallback(WMenu *menu, WMenuEntry *entry)
795 WDock *dock;
796 assert(entry->clientdata!=NULL);
798 dock = (WDock*) entry->clientdata;
800 dock->auto_collapse = !dock->auto_collapse;
802 entry->flags.indicator_on = ((WDock*)entry->clientdata)->auto_collapse;
804 wMenuPaint(menu);
808 static void
809 toggleAutoRaiseLowerCallback(WMenu *menu, WMenuEntry *entry)
811 WDock *dock;
812 assert(entry->clientdata!=NULL);
814 dock = (WDock*) entry->clientdata;
816 dock->auto_raise_lower = !dock->auto_raise_lower;
818 entry->flags.indicator_on = ((WDock*)entry->clientdata)->auto_raise_lower;
820 wMenuPaint(menu);
824 static void
825 launchCallback(WMenu *menu, WMenuEntry *entry)
827 WAppIcon *btn = (WAppIcon*)entry->clientdata;
829 launchDockedApplication(btn);
833 static void
834 settingsCallback(WMenu *menu, WMenuEntry *entry)
836 WAppIcon *btn = (WAppIcon*)entry->clientdata;
838 if (btn->editing)
839 return;
840 ShowDockAppSettingsPanel(btn);
844 static void
845 hideCallback(WMenu *menu, WMenuEntry *entry)
847 WApplication *wapp;
848 WAppIcon *btn = (WAppIcon*)entry->clientdata;
850 wapp = wApplicationOf(btn->icon->owner->main_window);
852 if (wapp->flags.hidden) {
853 wWorkspaceChange(btn->icon->core->screen_ptr,wapp->last_workspace);
854 wUnhideApplication(wapp, False, False);
855 } else {
856 wHideApplication(wapp);
861 static void
862 unhideHereCallback(WMenu *menu, WMenuEntry *entry)
864 WApplication *wapp;
865 WAppIcon *btn = (WAppIcon*)entry->clientdata;
867 wapp = wApplicationOf(btn->icon->owner->main_window);
869 wUnhideApplication(wapp, False, True);
873 WAppIcon*
874 mainIconCreate(WScreen *scr, int type)
876 WAppIcon *btn;
877 int x_pos;
879 if (type == WM_CLIP) {
880 if (scr->clip_icon)
881 return scr->clip_icon;
882 btn = wAppIconCreateForDock(scr, NULL, "Logo", "WMClip", TILE_CLIP);
883 btn->icon->core->descriptor.handle_expose = clipIconExpose;
884 btn->icon->core->descriptor.handle_enternotify = clipEnterNotify;
885 btn->icon->core->descriptor.handle_leavenotify = clipLeaveNotify;
886 /*x_pos = scr->scr_width - ICON_SIZE*2 - DOCK_EXTRA_SPACE;*/
887 x_pos = 0;
888 } else {
889 btn = wAppIconCreateForDock(scr, NULL, "Logo", "WMDock", TILE_NORMAL);
890 x_pos = scr->scr_width - ICON_SIZE - DOCK_EXTRA_SPACE;
893 btn->xindex = 0;
894 btn->yindex = 0;
896 btn->icon->core->descriptor.handle_mousedown = iconMouseDown;
897 btn->icon->core->descriptor.parent_type = WCLASS_DOCK_ICON;
898 btn->icon->core->descriptor.parent = btn;
899 /*ChangeStackingLevel(btn->icon->core, WMDockLevel);*/
900 XMapWindow(dpy, btn->icon->core->window);
901 btn->x_pos = x_pos;
902 btn->y_pos = 0;
903 btn->docked = 1;
904 if (type == WM_CLIP)
905 scr->clip_icon = btn;
907 return btn;
911 static void
912 switchWSCommand(WMenu *menu, WMenuEntry *entry)
914 WAppIcon *btn, *icon = (WAppIcon*) entry->clientdata;
915 WScreen *scr = icon->icon->core->screen_ptr;
916 WDock *src, *dest;
917 WMBag *selectedIcons;
918 int x, y;
920 if (entry->order == scr->current_workspace)
921 return;
922 src = icon->dock;
923 dest = scr->workspaces[entry->order]->clip;
925 selectedIcons = getSelected(src);
927 if (WMGetBagItemCount(selectedIcons)) {
928 int i;
929 for (i = 0; i < WMGetBagItemCount(selectedIcons); i++) {
930 btn = WMGetFromBag(selectedIcons, i);
931 if (wDockFindFreeSlot(dest, &x, &y)) {
932 moveIconBetweenDocks(src, dest, btn, x, y);
933 XUnmapWindow(dpy, btn->icon->core->window);
936 } else if (icon != scr->clip_icon) {
937 if (wDockFindFreeSlot(dest, &x, &y)) {
938 moveIconBetweenDocks(src, dest, icon, x, y);
939 XUnmapWindow(dpy, icon->icon->core->window);
942 WMFreeBag(selectedIcons);
947 static void
948 launchDockedApplication(WAppIcon *btn)
950 WScreen *scr = btn->icon->core->screen_ptr;
952 if (!btn->launching && btn->command!=NULL) {
953 if (!btn->forced_dock) {
954 btn->relaunching = btn->running;
955 btn->running = 1;
957 if (btn->wm_instance || btn->wm_class) {
958 WWindowAttributes attr;
959 memset(&attr, 0, sizeof(WWindowAttributes));
960 wDefaultFillAttributes(scr, btn->wm_instance, btn->wm_class,
961 &attr, NULL, True);
963 if (!attr.no_appicon && !btn->buggy_app)
964 btn->launching = 1;
965 else
966 btn->running = 0;
968 btn->drop_launch = 0;
969 scr->last_dock = btn->dock;
970 btn->pid = execCommand(btn, btn->command, NULL);
971 if (btn->pid>0) {
972 if (btn->buggy_app) {
973 /* give feedback that the app was launched */
974 btn->launching = 1;
975 dockIconPaint(btn);
976 btn->launching = 0;
977 WMAddTimerHandler(200, (WMCallback*)dockIconPaint, btn);
978 } else {
979 dockIconPaint(btn);
981 } else {
982 wwarning(_("could not launch application %s\n"), btn->command);
983 btn->launching = 0;
984 if (!btn->relaunching)
985 btn->running = 0;
992 static void
993 updateWorkspaceMenu(WMenu *menu, WAppIcon *icon)
995 WScreen *scr = menu->frame->screen_ptr;
996 char title[MAX_WORKSPACENAME_WIDTH+1];
997 int i;
999 if (!menu || !icon)
1000 return;
1002 for (i=0; i<scr->workspace_count; i++) {
1003 if (i < menu->entry_no) {
1004 if (strcmp(menu->entries[i]->text,scr->workspaces[i]->name)!=0) {
1005 free(menu->entries[i]->text);
1006 strcpy(title, scr->workspaces[i]->name);
1007 menu->entries[i]->text = wstrdup(title);
1008 menu->flags.realized = 0;
1010 menu->entries[i]->clientdata = (void*)icon;
1011 } else {
1012 strcpy(title, scr->workspaces[i]->name);
1014 wMenuAddCallback(menu, title, switchWSCommand, (void*)icon);
1016 menu->flags.realized = 0;
1018 if (i == scr->current_workspace) {
1019 wMenuSetEnabled(menu, i, False);
1020 } else {
1021 wMenuSetEnabled(menu, i, True);
1025 if (!menu->flags.realized)
1026 wMenuRealize(menu);
1030 static WMenu*
1031 makeWorkspaceMenu(WScreen *scr)
1033 WMenu *menu;
1035 menu = wMenuCreate(scr, NULL, False);
1036 if (!menu)
1037 wwarning(_("could not create workspace submenu for Clip menu"));
1039 wMenuAddCallback(menu, "", switchWSCommand, (void*)scr->clip_icon);
1041 menu->flags.realized = 0;
1042 wMenuRealize(menu);
1044 return menu;
1048 static void
1049 updateClipOptionsMenu(WMenu *menu, WDock *dock)
1051 WMenuEntry *entry;
1052 int index = 0;
1054 if (!menu || !dock)
1055 return;
1057 /* keep on top */
1058 entry = menu->entries[index];
1059 entry->flags.indicator_on = !dock->lowered;
1060 entry->clientdata = dock;
1062 /* collapsed */
1063 entry = menu->entries[++index];
1064 entry->flags.indicator_on = dock->collapsed;
1065 entry->clientdata = dock;
1067 /* auto-collapse */
1068 entry = menu->entries[++index];
1069 entry->flags.indicator_on = dock->auto_collapse;
1070 entry->clientdata = dock;
1072 /* auto-raise/lower */
1073 entry = menu->entries[++index];
1074 entry->flags.indicator_on = dock->auto_raise_lower;
1075 entry->clientdata = dock;
1077 /* attract icons */
1078 entry = menu->entries[++index];
1079 entry->flags.indicator_on = dock->attract_icons;
1080 entry->clientdata = dock;
1082 /* keep attracted icons */
1083 entry = menu->entries[++index];
1084 entry->flags.indicator_on = dock->keep_attracted;
1085 entry->clientdata = dock;
1087 menu->flags.realized = 0;
1088 wMenuRealize(menu);
1092 static WMenu*
1093 makeClipOptionsMenu(WScreen *scr)
1095 WMenu *menu;
1096 WMenuEntry *entry;
1098 menu = wMenuCreate(scr, NULL, False);
1099 if (!menu) {
1100 wwarning(_("could not create options submenu for Clip menu"));
1101 return NULL;
1104 entry = wMenuAddCallback(menu, _("Keep on Top"),
1105 toggleLoweredCallback, NULL);
1106 entry->flags.indicator = 1;
1107 entry->flags.indicator_on = 1;
1108 entry->flags.indicator_type = MI_CHECK;
1110 entry = wMenuAddCallback(menu, _("Collapsed"),
1111 toggleCollapsedCallback, NULL);
1112 entry->flags.indicator = 1;
1113 entry->flags.indicator_on = 1;
1114 entry->flags.indicator_type = MI_CHECK;
1116 entry = wMenuAddCallback(menu, _("AutoCollapse"),
1117 toggleAutoCollapseCallback, NULL);
1118 entry->flags.indicator = 1;
1119 entry->flags.indicator_on = 1;
1120 entry->flags.indicator_type = MI_CHECK;
1122 entry = wMenuAddCallback(menu, _("AutoRaiseLower"),
1123 toggleAutoRaiseLowerCallback, NULL);
1124 entry->flags.indicator = 1;
1125 entry->flags.indicator_on = 1;
1126 entry->flags.indicator_type = MI_CHECK;
1128 entry = wMenuAddCallback(menu, _("AutoAttract Icons"),
1129 toggleAutoAttractCallback, NULL);
1130 entry->flags.indicator = 1;
1131 entry->flags.indicator_on = 1;
1132 entry->flags.indicator_type = MI_CHECK;
1134 entry = wMenuAddCallback(menu, _("Keep Attracted Icons"),
1135 toggleKeepCallback, NULL);
1136 entry->flags.indicator = 1;
1137 entry->flags.indicator_on = 1;
1138 entry->flags.indicator_type = MI_CHECK;
1140 menu->flags.realized = 0;
1141 wMenuRealize(menu);
1143 return menu;
1147 static WMenu*
1148 dockMenuCreate(WScreen *scr, int type)
1150 WMenu *menu;
1151 WMenuEntry *entry;
1153 if (type == WM_CLIP && scr->clip_menu)
1154 return scr->clip_menu;
1156 menu = wMenuCreate(scr, NULL, False);
1157 if (type != WM_CLIP) {
1158 entry = wMenuAddCallback(menu, _("Keep on top"),
1159 toggleLoweredCallback, NULL);
1160 entry->flags.indicator = 1;
1161 entry->flags.indicator_on = 1;
1162 entry->flags.indicator_type = MI_CHECK;
1163 } else {
1164 entry = wMenuAddCallback(menu, _("Clip Options"), NULL, NULL);
1165 scr->clip_options = makeClipOptionsMenu(scr);
1166 if (scr->clip_options)
1167 wMenuEntrySetCascade(menu, entry, scr->clip_options);
1169 entry = wMenuAddCallback(menu, _("Rename Workspace"), renameCallback,
1170 NULL);
1171 free(entry->text);
1172 entry->text = _("Rename Workspace");
1174 entry = wMenuAddCallback(menu, _("Selected"), selectCallback, NULL);
1175 entry->flags.indicator = 1;
1176 entry->flags.indicator_on = 1;
1177 entry->flags.indicator_type = MI_CHECK;
1179 entry = wMenuAddCallback(menu, _("Select All Icons"),
1180 selectIconsCallback, NULL);
1181 free(entry->text);
1182 entry->text = _("Select All Icons");
1184 entry = wMenuAddCallback(menu, _("Keep Icon"), keepIconsCallback, NULL);
1185 free(entry->text);
1186 entry->text = _("Keep Icon");
1188 entry = wMenuAddCallback(menu, _("Move Icon To"), NULL, NULL);
1189 free(entry->text);
1190 entry->text = _("Move Icon To");
1191 scr->clip_submenu = makeWorkspaceMenu(scr);
1192 if (scr->clip_submenu)
1193 wMenuEntrySetCascade(menu, entry, scr->clip_submenu);
1195 entry = wMenuAddCallback(menu, _("Remove Icon"), removeIconsCallback,
1196 NULL);
1197 free(entry->text);
1198 entry->text = _("Remove Icon");
1200 wMenuAddCallback(menu, _("Attract Icons"), colectIconsCallback, NULL);
1203 wMenuAddCallback(menu, _("Launch"), launchCallback, NULL);
1205 wMenuAddCallback(menu, _("Unhide Here"), unhideHereCallback, NULL);
1207 entry = wMenuAddCallback(menu, _("Hide"), hideCallback, NULL);
1208 free(entry->text);
1209 entry->text = _("Hide");
1211 wMenuAddCallback(menu, _("Settings..."), settingsCallback, NULL);
1213 wMenuAddCallback(menu, _("Kill"), killCallback, NULL);
1215 if (type == WM_CLIP)
1216 scr->clip_menu = menu;
1218 return menu;
1222 WDock*
1223 wDockCreate(WScreen *scr, int type)
1225 WDock *dock;
1226 WAppIcon *btn;
1227 int icon_count;
1229 make_keys();
1231 dock = wmalloc(sizeof(WDock));
1232 memset(dock, 0, sizeof(WDock));
1234 if (type == WM_CLIP)
1235 icon_count = CLIP_MAX_ICONS;
1236 else
1237 icon_count = scr->scr_height/wPreferences.icon_size;
1239 dock->icon_array = wmalloc(sizeof(WAppIcon*)*icon_count);
1240 memset(dock->icon_array, 0, sizeof(WAppIcon*)*icon_count);
1242 dock->max_icons = icon_count;
1244 btn = mainIconCreate(scr, type);
1246 btn->dock = dock;
1248 dock->x_pos = btn->x_pos;
1249 dock->y_pos = btn->y_pos;
1250 dock->screen_ptr = scr;
1251 dock->type = type;
1252 dock->icon_count = 1;
1253 dock->on_right_side = 1;
1254 dock->collapsed = 0;
1255 dock->auto_collapse = 0;
1256 dock->auto_collapse_magic = NULL;
1257 dock->auto_raise_lower = 0;
1258 dock->auto_lower_magic = NULL;
1259 dock->auto_raise_magic = NULL;
1260 dock->attract_icons = 0;
1261 dock->keep_attracted = 0;
1262 dock->lowered = 1;
1263 dock->icon_array[0] = btn;
1264 wRaiseFrame(btn->icon->core);
1265 XMoveWindow(dpy, btn->icon->core->window, btn->x_pos, btn->y_pos);
1267 /* create dock menu */
1268 dock->menu = dockMenuCreate(scr, type);
1270 return dock;
1274 void
1275 wDockDestroy(WDock *dock)
1277 int i;
1278 WAppIcon *aicon;
1280 for (i=(dock->type == WM_CLIP) ? 1 : 0; i<dock->max_icons; i++) {
1281 aicon = dock->icon_array[i];
1282 if (aicon) {
1283 int keepit = aicon->running && wApplicationOf(aicon->main_window);
1284 wDockDetach(dock, aicon);
1285 if (keepit) {
1286 PlaceIcon(dock->screen_ptr, &aicon->x_pos, &aicon->y_pos);
1287 XMoveWindow(dpy, aicon->icon->core->window,
1288 aicon->x_pos, aicon->y_pos);
1289 if (!dock->mapped || dock->collapsed)
1290 XMapWindow(dpy, aicon->icon->core->window);
1294 if (wPreferences.auto_arrange_icons)
1295 wArrangeIcons(dock->screen_ptr, True);
1296 free(dock->icon_array);
1297 if (dock->menu && dock->type!=WM_CLIP)
1298 wMenuDestroy(dock->menu, True);
1299 free(dock);
1303 void
1304 wClipIconPaint(WAppIcon *aicon)
1306 WScreen *scr = aicon->icon->core->screen_ptr;
1307 WWorkspace *workspace = scr->workspaces[scr->current_workspace];
1308 GC gc;
1309 Window win = aicon->icon->core->window;
1310 int length, nlength;
1311 char *ws_name, ws_number[10];
1312 int ty, tx;
1314 wIconPaint(aicon->icon);
1316 length = strlen(workspace->name);
1317 ws_name = malloc(length + 1);
1318 sprintf(ws_name, "%s", workspace->name);
1319 sprintf(ws_number, "%i", scr->current_workspace + 1);
1320 nlength = strlen(ws_number);
1322 gc = scr->clip_title_gc;
1324 if (!workspace->clip->collapsed)
1325 XSetForeground(dpy, gc, scr->clip_title_pixel[CLIP_NORMAL]);
1326 else
1327 XSetForeground(dpy, gc, scr->clip_title_pixel[CLIP_COLLAPSED]);
1329 ty = ICON_SIZE - WMFontHeight(scr->clip_title_font) - 3;
1331 tx = CLIP_BUTTON_SIZE*ICON_SIZE/64;
1333 WMDrawString(scr->wmscreen, win, gc, scr->clip_title_font, tx,
1334 ty, ws_name, length);
1336 tx = (ICON_SIZE/2 - WMWidthOfString(scr->clip_title_font, ws_number,
1337 nlength))/2;
1339 WMDrawString(scr->wmscreen, win, gc, scr->clip_title_font, tx,
1340 2, ws_number, nlength);
1342 free(ws_name);
1344 if (aicon->launching) {
1345 XFillRectangle(dpy, aicon->icon->core->window, scr->stipple_gc,
1346 0, 0, wPreferences.icon_size, wPreferences.icon_size);
1348 paintClipButtons(aicon, aicon->dock->lclip_button_pushed,
1349 aicon->dock->rclip_button_pushed);
1353 static void
1354 clipIconExpose(WObjDescriptor *desc, XEvent *event)
1356 wClipIconPaint(desc->parent);
1360 static void
1361 dockIconPaint(WAppIcon *btn)
1363 if (btn == btn->icon->core->screen_ptr->clip_icon)
1364 wClipIconPaint(btn);
1365 else
1366 wAppIconPaint(btn);
1370 static proplist_t
1371 make_icon_state(WAppIcon *btn)
1373 proplist_t node = NULL;
1374 proplist_t command, autolaunch, name, forced, host, position, buggy;
1375 proplist_t omnipresent;
1376 char *tmp;
1377 char buffer[64];
1379 if (btn) {
1380 if (!btn->command)
1381 command = PLMakeString("-");
1382 else
1383 command = PLMakeString(btn->command);
1385 autolaunch = btn->auto_launch ? dYes : dNo;
1387 tmp = EscapeWM_CLASS(btn->wm_instance, btn->wm_class);
1389 name = PLMakeString(tmp);
1391 free(tmp);
1393 forced = btn->forced_dock ? dYes : dNo;
1395 buggy = btn->buggy_app ? dYes : dNo;
1397 if (btn == btn->icon->core->screen_ptr->clip_icon)
1398 sprintf(buffer, "%i,%i", btn->x_pos, btn->y_pos);
1399 else
1400 sprintf(buffer, "%hi,%hi", btn->xindex, btn->yindex);
1401 position = PLMakeString(buffer);
1403 node = PLMakeDictionaryFromEntries(dCommand, command,
1404 dName, name,
1405 dAutoLaunch, autolaunch,
1406 dForced, forced,
1407 dBuggyApplication, buggy,
1408 dPosition, position,
1409 NULL);
1410 PLRelease(command);
1411 PLRelease(name);
1412 PLRelease(position);
1414 omnipresent = btn->omnipresent ? dYes : dNo;
1415 if (btn->dock != btn->icon->core->screen_ptr->dock &&
1416 (btn->xindex != 0 || btn->yindex != 0))
1417 PLInsertDictionaryEntry(node, dOmnipresent, omnipresent);
1419 #ifdef OFFIX_DND
1420 if (btn->dnd_command) {
1421 command = PLMakeString(btn->dnd_command);
1422 PLInsertDictionaryEntry(node, dDropCommand, command);
1423 PLRelease(command);
1425 #endif /* OFFIX_DND */
1427 if (btn->client_machine && btn->remote_start) {
1428 host = PLMakeString(btn->client_machine);
1429 PLInsertDictionaryEntry(node, dHost, host);
1430 PLRelease(host);
1434 return node;
1438 static proplist_t
1439 dockSaveState(WDock *dock)
1441 int i;
1442 proplist_t icon_info;
1443 proplist_t list=NULL, dock_state=NULL;
1444 proplist_t value, key;
1445 char buffer[256];
1447 list = PLMakeArrayFromElements(NULL);
1449 for (i=(dock->type==WM_DOCK ? 0 : 1); i<dock->max_icons; i++) {
1450 WAppIcon *btn = dock->icon_array[i];
1452 if (!btn || (btn->attracted && !dock->keep_attracted))
1453 continue;
1455 if ((icon_info = make_icon_state(dock->icon_array[i]))) {
1456 list = PLAppendArrayElement(list, icon_info);
1457 PLRelease(icon_info);
1461 dock_state = PLMakeDictionaryFromEntries(dApplications, list,
1462 NULL);
1464 if (dock->type == WM_DOCK) {
1465 sprintf(buffer, "Applications%i", dock->screen_ptr->scr_height);
1466 key = PLMakeString(buffer);
1467 PLInsertDictionaryEntry(dock_state, key, list);
1468 PLRelease(key);
1471 sprintf(buffer, "%i,%i", (dock->on_right_side ? -ICON_SIZE : 0),
1472 dock->y_pos);
1473 value = PLMakeString(buffer);
1474 PLInsertDictionaryEntry(dock_state, dPosition, value);
1475 PLRelease(value);
1477 PLRelease(list);
1480 value = (dock->lowered ? dYes : dNo);
1481 PLInsertDictionaryEntry(dock_state, dLowered, value);
1483 if (dock->type == WM_CLIP) {
1484 value = (dock->collapsed ? dYes : dNo);
1485 PLInsertDictionaryEntry(dock_state, dCollapsed, value);
1487 value = (dock->auto_collapse ? dYes : dNo);
1488 PLInsertDictionaryEntry(dock_state, dAutoCollapse, value);
1490 value = (dock->auto_raise_lower ? dYes : dNo);
1491 PLInsertDictionaryEntry(dock_state, dAutoRaiseLower, value);
1493 value = (dock->attract_icons ? dYes : dNo);
1494 PLInsertDictionaryEntry(dock_state, dAutoAttractIcons, value);
1496 value = (dock->keep_attracted ? dYes : dNo);
1497 PLInsertDictionaryEntry(dock_state, dKeepAttracted, value);
1500 return dock_state;
1504 void
1505 wDockSaveState(WScreen *scr, proplist_t old_state)
1507 proplist_t dock_state;
1508 proplist_t keys;
1510 dock_state = dockSaveState(scr->dock);
1513 * Copy saved states of docks with different sizes.
1515 if (old_state) {
1516 int i;
1517 proplist_t tmp;
1519 keys = PLGetAllDictionaryKeys(old_state);
1520 for (i = 0; i < PLGetNumberOfElements(keys); i++) {
1521 tmp = PLGetArrayElement(keys, i);
1523 if (strncasecmp(PLGetString(tmp), "applications", 12) == 0
1524 && !PLGetDictionaryEntry(dock_state, tmp)) {
1526 PLInsertDictionaryEntry(dock_state,
1527 tmp,
1528 PLGetDictionaryEntry(old_state, tmp));
1531 PLRelease(keys);
1535 PLInsertDictionaryEntry(scr->session_state, dDock, dock_state);
1537 PLRelease(dock_state);
1541 void
1542 wClipSaveState(WScreen *scr)
1544 proplist_t clip_state;
1546 clip_state = make_icon_state(scr->clip_icon);
1548 PLInsertDictionaryEntry(scr->session_state, dClip, clip_state);
1550 PLRelease(clip_state);
1554 proplist_t
1555 wClipSaveWorkspaceState(WScreen *scr, int workspace)
1557 return dockSaveState(scr->workspaces[workspace]->clip);
1561 static WAppIcon*
1562 restore_icon_state(WScreen *scr, proplist_t info, int type, int index)
1564 WAppIcon *aicon;
1565 char *wclass, *winstance;
1566 proplist_t cmd, value;
1567 char *command;
1570 cmd = PLGetDictionaryEntry(info, dCommand);
1571 if (!cmd || !PLIsString(cmd)) {
1572 return NULL;
1575 /* parse window name */
1576 value = PLGetDictionaryEntry(info, dName);
1577 if (!value)
1578 return NULL;
1580 ParseWindowName(value, &winstance, &wclass, "dock");
1582 if (!winstance && !wclass) {
1583 return NULL;
1586 /* get commands */
1588 if (cmd)
1589 command = wstrdup(PLGetString(cmd));
1590 else
1591 command = NULL;
1593 if (!command || strcmp(command, "-")==0) {
1594 if (command)
1595 free(command);
1596 if (wclass)
1597 free(wclass);
1598 if (winstance)
1599 free(winstance);
1601 return NULL;
1604 aicon = wAppIconCreateForDock(scr, command, winstance, wclass,
1605 TILE_NORMAL);
1606 if (wclass)
1607 free(wclass);
1608 if (winstance)
1609 free(winstance);
1611 aicon->icon->core->descriptor.handle_mousedown = iconMouseDown;
1612 if (type == WM_CLIP) {
1613 aicon->icon->core->descriptor.handle_enternotify = clipEnterNotify;
1614 aicon->icon->core->descriptor.handle_leavenotify = clipLeaveNotify;
1616 aicon->icon->core->descriptor.parent_type = WCLASS_DOCK_ICON;
1617 aicon->icon->core->descriptor.parent = aicon;
1620 #ifdef OFFIX_DND
1621 cmd = PLGetDictionaryEntry(info, dDropCommand);
1622 if (cmd)
1623 aicon->dnd_command = wstrdup(PLGetString(cmd));
1624 #endif
1626 /* check auto launch */
1627 value = PLGetDictionaryEntry(info, dAutoLaunch);
1629 aicon->auto_launch = 0;
1630 if (value) {
1631 if (PLIsString(value)) {
1632 if (strcasecmp(PLGetString(value), "YES")==0)
1633 aicon->auto_launch = 1;
1634 } else {
1635 wwarning(_("bad value in docked icon state info %s"),
1636 PLGetString(dAutoLaunch));
1640 /* check if it wasn't normally docked */
1641 value = PLGetDictionaryEntry(info, dForced);
1643 aicon->forced_dock = 0;
1644 if (value) {
1645 if (PLIsString(value)) {
1646 if (strcasecmp(PLGetString(value), "YES")==0)
1647 aicon->forced_dock = 1;
1648 } else {
1649 wwarning(_("bad value in docked icon state info %s"),
1650 PLGetString(dForced));
1654 /* check if we can rely on the stuff in the app */
1655 value = PLGetDictionaryEntry(info, dBuggyApplication);
1657 aicon->buggy_app = 0;
1658 if (value) {
1659 if (PLIsString(value)) {
1660 if (strcasecmp(PLGetString(value), "YES")==0)
1661 aicon->buggy_app = 1;
1662 } else {
1663 wwarning(_("bad value in docked icon state info %s"),
1664 PLGetString(dBuggyApplication));
1668 /* get position in the dock */
1669 value = PLGetDictionaryEntry(info, dPosition);
1670 if (value && PLIsString(value)) {
1671 if (sscanf(PLGetString(value), "%hi,%hi", &aicon->xindex,
1672 &aicon->yindex)!=2)
1673 wwarning(_("bad value in docked icon state info %s"),
1674 PLGetString(dPosition));
1676 /* check position sanity */
1677 /* incomplete section! */
1678 if (type == WM_DOCK) {
1679 aicon->xindex = 0;
1680 if (aicon->yindex < 0)
1681 wwarning(_("bad value in docked icon position %i,%i"),
1682 aicon->xindex, aicon->yindex);
1684 } else {
1685 aicon->yindex = index;
1686 aicon->xindex = 0;
1689 /* check if icon is omnipresent */
1690 value = PLGetDictionaryEntry(info, dOmnipresent);
1692 aicon->omnipresent = 0;
1693 if (value) {
1694 if (PLIsString(value)) {
1695 if (strcasecmp(PLGetString(value), "YES")==0)
1696 aicon->omnipresent = 1;
1697 } else {
1698 wwarning(_("bad value in docked icon state info %s"),
1699 PLGetString(dOmnipresent));
1703 aicon->running = 0;
1704 aicon->docked = 1;
1706 return aicon;
1710 #define COMPLAIN(key) wwarning(_("bad value in dock state info:%s"), key)
1713 WAppIcon*
1714 wClipRestoreState(WScreen *scr, proplist_t clip_state)
1716 WAppIcon *icon;
1717 proplist_t value;
1720 icon = mainIconCreate(scr, WM_CLIP);
1722 if (!clip_state)
1723 return icon;
1724 else
1725 PLRetain(clip_state);
1727 /* restore position */
1729 value = PLGetDictionaryEntry(clip_state, dPosition);
1731 if (value) {
1732 if (!PLIsString(value))
1733 COMPLAIN("Position");
1734 else {
1735 if (sscanf(PLGetString(value), "%i,%i", &icon->x_pos,
1736 &icon->y_pos)!=2)
1737 COMPLAIN("Position");
1739 /* check position sanity */
1740 if (icon->y_pos < 0)
1741 icon->y_pos = 0;
1742 else if (icon->y_pos > scr->scr_height-ICON_SIZE)
1743 icon->y_pos = scr->scr_height-ICON_SIZE;
1745 if (icon->x_pos < 0)
1746 icon->x_pos = 0;
1747 else if (icon->x_pos > scr->scr_width-ICON_SIZE)
1748 icon->x_pos = scr->scr_width-ICON_SIZE;
1752 #ifdef OFFIX_DND
1753 value = PLGetDictionaryEntry(clip_state, dDropCommand);
1754 if (value && PLIsString(value))
1755 icon->dnd_command = wstrdup(PLGetString(value));
1756 #endif
1758 PLRelease(clip_state);
1760 return icon;
1764 WDock*
1765 wDockRestoreState(WScreen *scr, proplist_t dock_state, int type)
1767 WDock *dock;
1768 proplist_t apps;
1769 proplist_t value;
1770 WAppIcon *aicon, *old_top;
1771 int count, i;
1774 dock = wDockCreate(scr, type);
1776 if (!dock_state)
1777 return dock;
1779 if (dock_state)
1780 PLRetain(dock_state);
1783 /* restore position */
1785 value = PLGetDictionaryEntry(dock_state, dPosition);
1787 if (value) {
1788 if (!PLIsString(value))
1789 COMPLAIN("Position");
1790 else {
1791 if (sscanf(PLGetString(value), "%i,%i", &dock->x_pos,
1792 &dock->y_pos)!=2)
1793 COMPLAIN("Position");
1795 /* check position sanity */
1796 if (dock->y_pos < 0)
1797 dock->y_pos = 0;
1798 else if (dock->y_pos > scr->scr_height-ICON_SIZE)
1799 dock->y_pos = scr->scr_height - ICON_SIZE;
1801 /* This is no more needed. ??? */
1802 if (type == WM_CLIP) {
1803 if (dock->x_pos < 0)
1804 dock->x_pos = 0;
1805 else if (dock->x_pos > scr->scr_width-ICON_SIZE)
1806 dock->x_pos = scr->scr_width-ICON_SIZE;
1807 } else {
1808 if (dock->x_pos >= 0) {
1809 dock->x_pos = DOCK_EXTRA_SPACE;
1810 dock->on_right_side = 0;
1811 } else {
1812 dock->x_pos = scr->scr_width - DOCK_EXTRA_SPACE - ICON_SIZE;
1813 dock->on_right_side = 1;
1819 /* restore lowered/raised state */
1821 dock->lowered = 0;
1823 value = PLGetDictionaryEntry(dock_state, dLowered);
1825 if (value) {
1826 if (!PLIsString(value))
1827 COMPLAIN("Lowered");
1828 else {
1829 if (strcasecmp(PLGetString(value), "YES")==0)
1830 dock->lowered = 1;
1835 /* restore collapsed state */
1837 dock->collapsed = 0;
1839 value = PLGetDictionaryEntry(dock_state, dCollapsed);
1841 if (value) {
1842 if (!PLIsString(value))
1843 COMPLAIN("Collapsed");
1844 else {
1845 if (strcasecmp(PLGetString(value), "YES")==0)
1846 dock->collapsed = 1;
1851 /* restore auto-collapsed state */
1853 value = PLGetDictionaryEntry(dock_state, dAutoCollapse);
1855 if (value) {
1856 if (!PLIsString(value))
1857 COMPLAIN("AutoCollapse");
1858 else {
1859 if (strcasecmp(PLGetString(value), "YES")==0) {
1860 dock->auto_collapse = 1;
1861 dock->collapsed = 1;
1867 /* restore auto-raise/lower state */
1869 value = PLGetDictionaryEntry(dock_state, dAutoRaiseLower);
1871 if (value) {
1872 if (!PLIsString(value))
1873 COMPLAIN("AutoRaiseLower");
1874 else {
1875 if (strcasecmp(PLGetString(value), "YES")==0) {
1876 dock->auto_raise_lower = 1;
1881 /* restore attract icons state */
1883 dock->attract_icons = 0;
1885 value = PLGetDictionaryEntry(dock_state, dAutoAttractIcons);
1887 if (value) {
1888 if (!PLIsString(value))
1889 COMPLAIN("AutoAttractIcons");
1890 else {
1891 if (strcasecmp(PLGetString(value), "YES")==0)
1892 dock->attract_icons = 1;
1897 /* restore keep attracted icons state */
1899 dock->keep_attracted = 0;
1901 value = PLGetDictionaryEntry(dock_state, dKeepAttracted);
1903 if (value) {
1904 if (!PLIsString(value))
1905 COMPLAIN("KeepAttracted");
1906 else {
1907 if (strcasecmp(PLGetString(value), "YES")==0)
1908 dock->keep_attracted = 1;
1913 /* application list */
1916 proplist_t tmp;
1917 char buffer[64];
1920 * When saving, it saves the dock state in
1921 * Applications and Applicationsnnn
1923 * When loading, it will first try Applicationsnnn.
1924 * If it does not exist, use Applications as default.
1927 sprintf(buffer, "Applications%i", scr->scr_height);
1929 tmp = PLMakeString(buffer);
1930 apps = PLGetDictionaryEntry(dock_state, tmp);
1931 PLRelease(tmp);
1933 if (!apps) {
1934 apps = PLGetDictionaryEntry(dock_state, dApplications);
1938 if (!apps) {
1939 goto finish;
1942 count = PLGetNumberOfElements(apps);
1944 if (count==0)
1945 goto finish;
1947 old_top = dock->icon_array[0];
1949 /* dock->icon_count is set to 1 when dock is created.
1950 * Since Clip is already restored, we want to keep it so for clip,
1951 * but for dock we may change the default top tile, so we set it to 0.
1953 if (type == WM_DOCK)
1954 dock->icon_count = 0;
1956 for (i=0; i<count; i++) {
1957 if (dock->icon_count >= dock->max_icons) {
1958 wwarning(_("there are too many icons stored in dock. Ignoring what doesn't fit"));
1959 break;
1962 value = PLGetArrayElement(apps, i);
1963 aicon = restore_icon_state(scr, value, type, dock->icon_count);
1965 dock->icon_array[dock->icon_count] = aicon;
1967 if (aicon) {
1968 aicon->dock = dock;
1969 aicon->x_pos = dock->x_pos + (aicon->xindex*ICON_SIZE);
1970 aicon->y_pos = dock->y_pos + (aicon->yindex*ICON_SIZE);
1972 if (dock->lowered)
1973 ChangeStackingLevel(aicon->icon->core, WMNormalLevel);
1974 else
1975 ChangeStackingLevel(aicon->icon->core, WMDockLevel);
1977 wCoreConfigure(aicon->icon->core, aicon->x_pos, aicon->y_pos,
1978 0, 0);
1980 if (!dock->collapsed)
1981 XMapWindow(dpy, aicon->icon->core->window);
1982 wRaiseFrame(aicon->icon->core);
1984 dock->icon_count++;
1985 } else if (dock->icon_count==0 && type==WM_DOCK)
1986 dock->icon_count++;
1989 /* if the first icon is not defined, use the default */
1990 if (dock->icon_array[0]==NULL) {
1991 /* update default icon */
1992 old_top->x_pos = dock->x_pos;
1993 old_top->y_pos = dock->y_pos;
1994 if (dock->lowered)
1995 ChangeStackingLevel(old_top->icon->core, WMNormalLevel);
1996 else
1997 ChangeStackingLevel(old_top->icon->core, WMDockLevel);
1998 dock->icon_array[0] = old_top;
1999 XMoveWindow(dpy, old_top->icon->core->window, dock->x_pos, dock->y_pos);
2000 /* we don't need to increment dock->icon_count here because it was
2001 * incremented in the loop above.
2003 } else if (old_top!=dock->icon_array[0]) {
2004 if (old_top == scr->clip_icon)
2005 scr->clip_icon = dock->icon_array[0];
2006 wAppIconDestroy(old_top);
2009 finish:
2010 if (dock_state)
2011 PLRelease(dock_state);
2013 return dock;
2018 void
2019 wDockLaunchWithState(WDock *dock, WAppIcon *btn, WSavedState *state)
2021 if (btn && btn->command && !btn->running && !btn->launching) {
2023 btn->drop_launch = 0;
2025 btn->pid = execCommand(btn, btn->command, state);
2027 if (btn->pid>0) {
2028 if (!btn->forced_dock && !btn->buggy_app) {
2029 btn->launching = 1;
2030 dockIconPaint(btn);
2033 } else {
2034 free(state);
2039 void
2040 wDockDoAutoLaunch(WDock *dock, int workspace)
2042 WAppIcon *btn;
2043 WSavedState *state;
2044 int i;
2046 for (i = 0; i < dock->max_icons; i++) {
2047 btn = dock->icon_array[i];
2048 if (!btn || !btn->auto_launch)
2049 continue;
2051 state = wmalloc(sizeof(WSavedState));
2052 memset(state, 0, sizeof(WSavedState));
2053 state->workspace = workspace;
2054 /* TODO: this is klugy and is very difficult to understand
2055 * what's going on. Try to clean up */
2056 wDockLaunchWithState(dock, btn, state);
2060 #ifdef REDUCE_APPICONS
2061 void
2062 wDockSimulateLaunch(WDock *dock, WAppIcon *btn)
2064 if ((btn == NULL) || (dock == NULL))
2065 return;
2067 if (!btn->running) {
2068 if ((btn->icon->owner == NULL) && (btn->applist))
2069 btn->icon->owner = btn->applist->wapp->main_window_desc;
2070 if (!btn->forced_dock)
2071 btn->launching = 1;
2072 dockIconPaint(btn);
2073 wusleep(5000);
2076 #endif
2078 #ifdef OFFIX_DND
2079 static WDock*
2080 findDock(WScreen *scr, XEvent *event, int *icon_pos)
2082 WDock *dock;
2083 int i;
2085 *icon_pos = -1;
2086 if ((dock = scr->dock)!=NULL) {
2087 for (i=0; i<dock->max_icons; i++) {
2088 if (dock->icon_array[i]
2089 && dock->icon_array[i]->icon->core->window==event->xclient.window) {
2090 *icon_pos = i;
2091 break;
2095 if (*icon_pos<0 && (dock = scr->workspaces[scr->current_workspace]->clip)!=NULL) {
2096 for (i=0; i<dock->max_icons; i++) {
2097 if (dock->icon_array[i]
2098 && dock->icon_array[i]->icon->core->window==event->xclient.window) {
2099 *icon_pos = i;
2100 break;
2104 if(*icon_pos>=0)
2105 return dock;
2106 return NULL;
2111 wDockReceiveDNDDrop(WScreen *scr, XEvent *event)
2113 WDock *dock;
2114 WAppIcon *btn;
2115 int icon_pos;
2117 dock = findDock(scr, event, &icon_pos);
2118 if (!dock)
2119 return False;
2122 * Return True if the drop was on an application icon window.
2123 * In this case, let the ClientMessage handler redirect the
2124 * message to the app.
2126 if (dock->icon_array[icon_pos]->icon->icon_win!=None)
2127 return True;
2129 if (dock->icon_array[icon_pos]->dnd_command!=NULL) {
2130 scr->flags.dnd_data_convertion_status = 0;
2132 btn = dock->icon_array[icon_pos];
2134 if (!btn->forced_dock) {
2135 btn->relaunching = btn->running;
2136 btn->running = 1;
2138 if (btn->wm_instance || btn->wm_class) {
2139 WWindowAttributes attr;
2140 memset(&attr, 0, sizeof(WWindowAttributes));
2141 wDefaultFillAttributes(btn->icon->core->screen_ptr,
2142 btn->wm_instance,
2143 btn->wm_class, &attr, NULL, True);
2145 if (!attr.no_appicon)
2146 btn->launching = 1;
2147 else
2148 btn->running = 0;
2151 btn->drop_launch = 1;
2152 scr->last_dock = dock;
2153 btn->pid = execCommand(btn, btn->dnd_command, NULL);
2154 if (btn->pid>0) {
2155 dockIconPaint(btn);
2156 } else {
2157 btn->launching = 0;
2158 if (!btn->relaunching) {
2159 btn->running = 0;
2163 return False;
2165 #endif /* OFFIX_DND */
2169 Bool
2170 wDockAttachIcon(WDock *dock, WAppIcon *icon, int x, int y)
2172 WWindow *wwin;
2173 char **argv;
2174 int argc;
2175 int index;
2177 wwin = icon->icon->owner;
2178 if (icon->command==NULL) {
2179 icon->editing = 0;
2180 if (XGetCommand(dpy, wwin->client_win, &argv, &argc) && argc>0) {
2182 icon->command = FlattenStringList(argv, argc);
2183 XFreeStringList(argv);
2184 } else {
2185 char *command=NULL;
2187 /* icon->forced_dock = 1;*/
2188 if (!icon->attracted || dock->type!=WM_CLIP || dock->keep_attracted) {
2189 icon->editing = 1;
2190 if (wInputDialog(dock->screen_ptr, _("Dock Icon"),
2191 _("Type the command used to launch the application"),
2192 &command)) {
2193 if (command && (command[0]==0 ||
2194 (command[0]=='-' && command[1]==0))) {
2195 free(command);
2196 command = NULL;
2198 icon->command = command;
2199 icon->editing = 0;
2200 } else {
2201 icon->editing = 0;
2202 if (command)
2203 free(command);
2204 /* If the target is the dock, reject the icon. If
2205 * the target is the clip, make it an attracted icon
2207 if (dock->type==WM_CLIP) {
2208 icon->attracted = 1;
2209 if (!icon->icon->shadowed) {
2210 icon->icon->shadowed = 1;
2211 icon->icon->force_paint = 1;
2213 } else {
2214 return False;
2219 } else {
2220 icon->editing = 0;
2223 for (index=1; index<dock->max_icons; index++)
2224 if (dock->icon_array[index] == NULL)
2225 break;
2226 /* if (index == dock->max_icons)
2227 return; */
2229 assert(index < dock->max_icons);
2231 dock->icon_array[index] = icon;
2232 icon->yindex = y;
2233 icon->xindex = x;
2235 icon->omnipresent = 0;
2237 icon->x_pos = dock->x_pos + x*ICON_SIZE;
2238 icon->y_pos = dock->y_pos + y*ICON_SIZE;
2240 dock->icon_count++;
2242 icon->running = 1;
2243 icon->launching = 0;
2244 icon->docked = 1;
2245 icon->dock = dock;
2246 icon->icon->core->descriptor.handle_mousedown = iconMouseDown;
2247 if (dock->type == WM_CLIP) {
2248 icon->icon->core->descriptor.handle_enternotify = clipEnterNotify;
2249 icon->icon->core->descriptor.handle_leavenotify = clipLeaveNotify;
2251 icon->icon->core->descriptor.parent_type = WCLASS_DOCK_ICON;
2252 icon->icon->core->descriptor.parent = icon;
2254 MoveInStackListUnder(dock->icon_array[index-1]->icon->core,
2255 icon->icon->core);
2256 wAppIconMove(icon, icon->x_pos, icon->y_pos);
2257 wAppIconPaint(icon);
2259 if (wPreferences.auto_arrange_icons)
2260 wArrangeIcons(dock->screen_ptr, True);
2262 #ifdef OFFIX_DND
2263 if (icon->command && !icon->dnd_command) {
2264 icon->dnd_command = wmalloc(strlen(icon->command)+8);
2265 sprintf(icon->dnd_command, "%s %%d", icon->command);
2267 #endif
2269 return True;
2273 void
2274 reattachIcon(WDock *dock, WAppIcon *icon, int x, int y)
2276 int index;
2278 for(index=1; index<dock->max_icons; index++) {
2279 if(dock->icon_array[index] == icon)
2280 break;
2282 assert(index < dock->max_icons);
2284 icon->yindex = y;
2285 icon->xindex = x;
2287 icon->x_pos = dock->x_pos + x*ICON_SIZE;
2288 icon->y_pos = dock->y_pos + y*ICON_SIZE;
2292 Bool
2293 moveIconBetweenDocks(WDock *src, WDock *dest, WAppIcon *icon, int x, int y)
2295 WWindow *wwin;
2296 char **argv;
2297 int argc;
2298 int index;
2300 if (src == dest)
2301 return True; /* No move needed, we're already there */
2303 if (dest == NULL)
2304 return False;
2306 wwin = icon->icon->owner;
2309 * For the moment we can't do this if we move icons in Clip from one
2310 * workspace to other, because if we move two or more icons without
2311 * command, the dialog box will not be able to tell us to which of the
2312 * moved icons it applies. -Dan
2314 if ((dest->type==WM_DOCK /*|| dest->keep_attracted*/) && icon->command==NULL) {
2315 if (XGetCommand(dpy, wwin->client_win, &argv, &argc) && argc>0) {
2317 icon->command = FlattenStringList(argv, argc);
2318 XFreeStringList(argv);
2319 } else {
2320 char *command=NULL;
2322 icon->editing = 1;
2323 /* icon->forced_dock = 1;*/
2324 if (wInputDialog(src->screen_ptr, _("Dock Icon"),
2325 _("Type the command used to launch the application"),
2326 &command)) {
2327 if (command && (command[0]==0 ||
2328 (command[0]=='-' && command[1]==0))) {
2329 free(command);
2330 command = NULL;
2332 icon->command = command;
2333 } else {
2334 icon->editing = 0;
2335 if (command)
2336 free(command);
2337 return False;
2339 icon->editing = 0;
2343 if (dest->type == WM_DOCK)
2344 wClipMakeIconOmnipresent(icon, False);
2346 for(index=1; index<src->max_icons; index++) {
2347 if(src->icon_array[index] == icon)
2348 break;
2350 assert(index < src->max_icons);
2352 src->icon_array[index] = NULL;
2353 src->icon_count--;
2355 for(index=1; index<dest->max_icons; index++) {
2356 if(dest->icon_array[index] == NULL)
2357 break;
2359 /* if (index == dest->max_icons)
2360 return; */
2362 assert(index < dest->max_icons);
2364 dest->icon_array[index] = icon;
2365 icon->dock = dest;
2367 /* deselect the icon */
2368 if (icon->icon->selected)
2369 wIconSelect(icon->icon);
2371 if (dest->type == WM_DOCK) {
2372 icon->icon->core->descriptor.handle_enternotify = NULL;
2373 icon->icon->core->descriptor.handle_leavenotify = NULL;
2374 } else {
2375 icon->icon->core->descriptor.handle_enternotify = clipEnterNotify;
2376 icon->icon->core->descriptor.handle_leavenotify = clipLeaveNotify;
2379 /* set it to be kept when moving to dock, or to a clip that keep the
2380 * attracted icons.
2381 * Unless the icon does not have a command set
2383 if (icon->command && (dest->type==WM_DOCK || dest->keep_attracted)) {
2384 icon->attracted = 0;
2385 if (icon->icon->shadowed) {
2386 icon->icon->shadowed = 0;
2387 icon->icon->force_paint = 1;
2391 if (src->auto_collapse || src->auto_raise_lower)
2392 clipLeave(src);
2394 icon->yindex = y;
2395 icon->xindex = x;
2397 icon->x_pos = dest->x_pos + x*ICON_SIZE;
2398 icon->y_pos = dest->y_pos + y*ICON_SIZE;
2400 dest->icon_count++;
2402 MoveInStackListUnder(dest->icon_array[index-1]->icon->core,
2403 icon->icon->core);
2404 wAppIconPaint(icon);
2406 return True;
2410 void
2411 wDockDetach(WDock *dock, WAppIcon *icon)
2413 int index;
2415 /* make the settings panel be closed */
2416 if (icon->panel) {
2417 DestroyDockAppSettingsPanel(icon->panel);
2420 icon->docked = 0;
2421 icon->dock = NULL;
2422 icon->attracted = 0;
2423 if (icon->icon->shadowed) {
2424 icon->icon->shadowed = 0;
2425 icon->icon->force_paint = 1;
2428 /* This must be called before dock->icon_array[index] is set to NULL.
2429 * Don't move it. -Dan
2431 wClipMakeIconOmnipresent(icon, False);
2433 /* deselect the icon */
2434 if (icon->icon->selected)
2435 wIconSelect(icon->icon);
2437 if (icon->command) {
2438 free(icon->command);
2439 icon->command = NULL;
2441 #ifdef OFFIX_DND
2442 if (icon->dnd_command) {
2443 free(icon->dnd_command);
2444 icon->dnd_command = NULL;
2446 #endif
2448 for (index=1; index<dock->max_icons; index++)
2449 if (dock->icon_array[index] == icon)
2450 break;
2451 assert(index < dock->max_icons);
2452 dock->icon_array[index] = NULL;
2453 icon->yindex = -1;
2454 icon->xindex = -1;
2456 dock->icon_count--;
2458 /* if the dock is not attached to an application or
2459 * the the application did not set the approriate hints yet,
2460 * destroy the icon */
2461 #ifdef REDUCE_APPICONS
2462 if ((icon->num_apps == 0) && (!icon->running || !wApplicationOf(icon->main_window)) )
2463 #else
2464 if (!icon->running || !wApplicationOf(icon->main_window))
2465 #endif
2466 wAppIconDestroy(icon);
2467 else {
2468 icon->icon->core->descriptor.handle_mousedown = appIconMouseDown;
2469 icon->icon->core->descriptor.handle_enternotify = NULL;
2470 icon->icon->core->descriptor.handle_leavenotify = NULL;
2471 icon->icon->core->descriptor.parent_type = WCLASS_APPICON;
2472 icon->icon->core->descriptor.parent = icon;
2474 ChangeStackingLevel(icon->icon->core, NORMAL_ICON_LEVEL);
2476 wAppIconPaint(icon);
2477 if (wPreferences.auto_arrange_icons) {
2478 wArrangeIcons(dock->screen_ptr, True);
2481 if (dock->auto_collapse || dock->auto_raise_lower)
2482 clipLeave(dock);
2487 * returns the closest Dock slot index for the passed
2488 * coordinates.
2490 * Returns False if icon can't be docked.
2492 * Note: this function should NEVER alter ret_x or ret_y, unless it will
2493 * return True. -Dan
2495 Bool
2496 wDockSnapIcon(WDock *dock, WAppIcon *icon, int req_x, int req_y,
2497 int *ret_x, int *ret_y, int redocking)
2499 WScreen *scr = dock->screen_ptr;
2500 int dx, dy;
2501 int ex_x, ex_y;
2502 int i, offset = ICON_SIZE/2;
2503 WAppIcon *aicon = NULL;
2504 WAppIcon *nicon = NULL;
2505 int max_y_icons, max_x_icons;
2507 max_x_icons = scr->scr_width/ICON_SIZE;
2508 max_y_icons = scr->scr_height/ICON_SIZE-1;
2510 if (wPreferences.flags.noupdates)
2511 return False;
2513 dx = dock->x_pos;
2514 dy = dock->y_pos;
2516 /* if the dock is full */
2517 if (!redocking &&
2518 (dock->icon_count >= dock->max_icons)) {
2519 return False;
2522 /* exact position */
2523 if (req_y < dy)
2524 ex_y = (req_y - offset - dy)/ICON_SIZE;
2525 else
2526 ex_y = (req_y + offset - dy)/ICON_SIZE;
2528 if (req_x < dx)
2529 ex_x = (req_x - offset - dx)/ICON_SIZE;
2530 else
2531 ex_x = (req_x + offset - dx)/ICON_SIZE;
2533 /* check if the icon is outside the screen boundaries */
2534 if (dx + ex_x*ICON_SIZE < -ICON_SIZE+2 ||
2535 dx + ex_x*ICON_SIZE >= scr->scr_width-1 ||
2536 dy + ex_y*ICON_SIZE < -ICON_SIZE+2 ||
2537 dy + ex_y*ICON_SIZE >= scr->scr_height-1)
2538 return False;
2540 if (dock->type == WM_DOCK) {
2541 if (icon->dock != dock && ex_x != 0)
2542 return False;
2544 aicon = NULL;
2545 for (i=0; i<dock->max_icons; i++) {
2546 nicon = dock->icon_array[i];
2547 if (nicon && nicon->yindex == ex_y) {
2548 aicon = nicon;
2549 break;
2553 if (redocking) {
2554 int sig, done, closest;
2556 /* Possible cases when redocking:
2558 * icon dragged out of range of any slot -> false
2559 * icon dragged to range of free slot
2560 * icon dragged to range of same slot
2561 * icon dragged to range of different icon
2563 if (abs(ex_x) > DOCK_DETTACH_THRESHOLD)
2564 return False;
2566 if (ex_y>=0 && ex_y<=max_y_icons && (aicon==icon || !aicon)) {
2567 *ret_x = 0;
2568 *ret_y = ex_y;
2569 return True;
2572 /* start looking at the upper slot or lower? */
2573 if (ex_y*ICON_SIZE < (req_y + offset - dy))
2574 sig = 1;
2575 else
2576 sig = -1;
2578 closest = -1;
2579 done = 0;
2580 /* look for closest free slot */
2581 for (i=0; i<(DOCK_DETTACH_THRESHOLD+1)*2 && !done; i++) {
2582 int j;
2584 done = 1;
2585 closest = sig*(i/2) + ex_y;
2586 /* check if this slot is used */
2587 if (closest >= 0) {
2588 for (j = 0; j<dock->max_icons; j++) {
2589 if (dock->icon_array[j]
2590 && dock->icon_array[j]->yindex==closest) {
2591 /* slot is used by someone else */
2592 if (dock->icon_array[j]!=icon)
2593 done = 0;
2594 break;
2598 sig = -sig;
2600 if (done && closest >= 0 && closest <= max_y_icons &&
2601 ((ex_y >= closest && ex_y - closest < DOCK_DETTACH_THRESHOLD+1)
2603 (ex_y < closest && closest - ex_y <= DOCK_DETTACH_THRESHOLD+1))) {
2604 *ret_x = 0;
2605 *ret_y = closest;
2606 return True;
2608 } else { /* !redocking */
2610 /* if slot is free and the icon is close enough, return it */
2611 if (!aicon && ex_x == 0 && ex_y >= 0 && ex_y <= max_y_icons) {
2612 *ret_x = 0;
2613 *ret_y = ex_y;
2614 return True;
2617 } else { /* CLIP */
2618 int neighbours = 0;
2619 int start, stop, k;
2621 start = icon->omnipresent ? 0 : scr->current_workspace;
2622 stop = icon->omnipresent ? scr->workspace_count : start+1;
2624 aicon = NULL;
2625 for (k=start; k<stop; k++) {
2626 WDock *tmp = scr->workspaces[k]->clip;
2627 if (!tmp)
2628 continue;
2629 for (i=0; i<tmp->max_icons; i++) {
2630 nicon = tmp->icon_array[i];
2631 if (nicon && nicon->xindex == ex_x && nicon->yindex == ex_y) {
2632 aicon = nicon;
2633 break;
2636 if (aicon)
2637 break;
2639 for (k=start; k<stop; k++) {
2640 WDock *tmp = scr->workspaces[k]->clip;
2641 if (!tmp)
2642 continue;
2643 for (i=0; i<tmp->max_icons; i++) {
2644 nicon = tmp->icon_array[i];
2645 if (nicon && nicon != icon && /* Icon can't be it's own neighbour */
2646 (abs(nicon->xindex - ex_x) <= CLIP_ATTACH_VICINITY &&
2647 abs(nicon->yindex - ex_y) <= CLIP_ATTACH_VICINITY)) {
2648 neighbours = 1;
2649 break;
2652 if (neighbours)
2653 break;
2656 if (neighbours && (aicon==NULL || (redocking && aicon == icon))) {
2657 *ret_x = ex_x;
2658 *ret_y = ex_y;
2659 return True;
2662 return False;
2665 #define MIN(x, y) ((x) > (y) ? (y) : (x))
2666 #define MAX(x, y) ((x) < (y) ? (y) : (x))
2668 #define ON_SCREEN(x, y, sx, ex, sy, ey) \
2669 ((((x)+ICON_SIZE/2) >= (sx)) && (((y)+ICON_SIZE/2) >= (sy)) && \
2670 (((x) + (ICON_SIZE/2)) <= (ex)) && (((y) + (ICON_SIZE/2)) <= ey))
2674 * returns true if it can find a free slot in the dock,
2675 * in which case it changes x_pos and y_pos accordingly.
2676 * Else returns false.
2678 Bool
2679 wDockFindFreeSlot(WDock *dock, int *x_pos, int *y_pos)
2681 WScreen *scr = dock->screen_ptr;
2682 WAppIcon *btn;
2683 WAppIconChain *chain;
2684 unsigned char *slot_map;
2685 int mwidth;
2686 int r;
2687 int x, y;
2688 int i, done = False;
2689 int corner;
2690 int sx=0, sy=0, ex=scr->scr_width, ey=scr->scr_height;
2691 int extra_count=0;
2693 if (dock->type == WM_CLIP &&
2694 dock != scr->workspaces[scr->current_workspace]->clip)
2695 extra_count = scr->global_icon_count;
2697 /* if the dock is full */
2698 if (dock->icon_count+extra_count >= dock->max_icons) {
2699 return False;
2702 if (!wPreferences.flags.nodock && scr->dock) {
2703 if (scr->dock->on_right_side)
2704 ex -= ICON_SIZE + DOCK_EXTRA_SPACE;
2705 else
2706 sx += ICON_SIZE + DOCK_EXTRA_SPACE;
2709 if (ex < dock->x_pos)
2710 ex = dock->x_pos;
2711 if (sx > dock->x_pos+ICON_SIZE)
2712 sx = dock->x_pos+ICON_SIZE;
2713 #define C_NONE 0
2714 #define C_NW 1
2715 #define C_NE 2
2716 #define C_SW 3
2717 #define C_SE 4
2719 /* check if clip is in a corner */
2720 if (dock->type==WM_CLIP) {
2721 if (dock->x_pos < 1 && dock->y_pos < 1)
2722 corner = C_NE;
2723 else if (dock->x_pos < 1 && dock->y_pos >= (ey-ICON_SIZE))
2724 corner = C_SE;
2725 else if (dock->x_pos >= (ex-ICON_SIZE)&& dock->y_pos >= (ey-ICON_SIZE))
2726 corner = C_SW;
2727 else if (dock->x_pos >= (ex-ICON_SIZE) && dock->y_pos < 1)
2728 corner = C_NW;
2729 else
2730 corner = C_NONE;
2731 } else
2732 corner = C_NONE;
2734 /* If the clip is in the corner, use only slots that are in the border
2735 * of the screen */
2736 if (corner!=C_NONE) {
2737 char *hmap, *vmap;
2738 int hcount, vcount;
2740 hcount = MIN(dock->max_icons, scr->scr_width/ICON_SIZE);
2741 vcount = MIN(dock->max_icons, scr->scr_height/ICON_SIZE);
2742 hmap = wmalloc(hcount+1);
2743 memset(hmap, 0, hcount+1);
2744 vmap = wmalloc(vcount+1);
2745 memset(vmap, 0, vcount+1);
2747 /* mark used positions */
2748 switch (corner) {
2749 case C_NE:
2750 for (i=0; i<dock->max_icons; i++) {
2751 btn = dock->icon_array[i];
2752 if (!btn)
2753 continue;
2755 if (btn->xindex==0 && btn->yindex > 0 && btn->yindex < vcount)
2756 vmap[btn->yindex] = 1;
2757 else if (btn->yindex==0 && btn->xindex>0 && btn->xindex<hcount)
2758 hmap[btn->xindex] = 1;
2760 for (chain=scr->global_icons; chain!=NULL; chain=chain->next) {
2761 btn = chain->aicon;
2762 if (btn->xindex==0 && btn->yindex > 0 && btn->yindex < vcount)
2763 vmap[btn->yindex] = 1;
2764 else if (btn->yindex==0 && btn->xindex>0 && btn->xindex<hcount)
2765 hmap[btn->xindex] = 1;
2767 break;
2768 case C_NW:
2769 for (i=0; i<dock->max_icons; i++) {
2770 btn = dock->icon_array[i];
2771 if (!btn)
2772 continue;
2774 if (btn->xindex==0 && btn->yindex > 0 && btn->yindex < vcount)
2775 vmap[btn->yindex] = 1;
2776 else if (btn->yindex==0 && btn->xindex<0 &&btn->xindex>-hcount)
2777 hmap[-btn->xindex] = 1;
2779 for (chain=scr->global_icons; chain!=NULL; chain=chain->next) {
2780 btn = chain->aicon;
2781 if (btn->xindex==0 && btn->yindex > 0 && btn->yindex < vcount)
2782 vmap[btn->yindex] = 1;
2783 else if (btn->yindex==0 && btn->xindex<0 &&btn->xindex>-hcount)
2784 hmap[-btn->xindex] = 1;
2786 break;
2787 case C_SE:
2788 for (i=0; i<dock->max_icons; i++) {
2789 btn = dock->icon_array[i];
2790 if (!btn)
2791 continue;
2793 if (btn->xindex==0 && btn->yindex < 0 && btn->yindex > -vcount)
2794 vmap[-btn->yindex] = 1;
2795 else if (btn->yindex==0 && btn->xindex>0 && btn->xindex<hcount)
2796 hmap[btn->xindex] = 1;
2798 for (chain=scr->global_icons; chain!=NULL; chain=chain->next) {
2799 btn = chain->aicon;
2800 if (btn->xindex==0 && btn->yindex < 0 && btn->yindex > -vcount)
2801 vmap[-btn->yindex] = 1;
2802 else if (btn->yindex==0 && btn->xindex>0 && btn->xindex<hcount)
2803 hmap[btn->xindex] = 1;
2805 break;
2806 case C_SW:
2807 default:
2808 for (i=0; i<dock->max_icons; i++) {
2809 btn = dock->icon_array[i];
2810 if (!btn)
2811 continue;
2813 if (btn->xindex==0 && btn->yindex < 0 && btn->yindex > -vcount)
2814 vmap[-btn->yindex] = 1;
2815 else if (btn->yindex==0 && btn->xindex<0 &&btn->xindex>-hcount)
2816 hmap[-btn->xindex] = 1;
2818 for (chain=scr->global_icons; chain!=NULL; chain=chain->next) {
2819 btn = chain->aicon;
2820 if (btn->xindex==0 && btn->yindex < 0 && btn->yindex > -vcount)
2821 vmap[-btn->yindex] = 1;
2822 else if (btn->yindex==0 && btn->xindex<0 &&btn->xindex>-hcount)
2823 hmap[-btn->xindex] = 1;
2826 x=0; y=0;
2827 done = 0;
2828 /* search a vacant slot */
2829 for (i=1; i<MAX(vcount, hcount); i++) {
2830 if (i < vcount && vmap[i]==0) {
2831 /* found a slot */
2832 x = 0;
2833 y = i;
2834 done = 1;
2835 break;
2836 } else if (i < hcount && hmap[i]==0) {
2837 /* found a slot */
2838 x = i;
2839 y = 0;
2840 done = 1;
2841 break;
2844 free(vmap);
2845 free(hmap);
2846 /* If found a slot, translate and return */
2847 if (done) {
2848 if (corner==C_NW || corner==C_NE) {
2849 *y_pos = y;
2850 } else {
2851 *y_pos = -y;
2853 if (corner==C_NE || corner==C_SE) {
2854 *x_pos = x;
2855 } else {
2856 *x_pos = -x;
2858 return True;
2860 /* else, try to find a slot somewhere else */
2863 /* a map of mwidth x mwidth would be enough if we allowed icons to be
2864 * placed outside of screen */
2865 mwidth = (int)ceil(sqrt(dock->max_icons));
2867 /* In the worst case (the clip is in the corner of the screen),
2868 * the amount of icons that fit in the clip is smaller.
2869 * Double the map to get a safe value.
2871 mwidth += mwidth;
2873 r = (mwidth-1)/2;
2875 slot_map = wmalloc(mwidth*mwidth);
2876 memset(slot_map, 0, mwidth*mwidth);
2878 #define XY2OFS(x,y) (MAX(abs(x),abs(y)) > r) ? 0 : (((y)+r)*(mwidth)+(x)+r)
2880 /* mark used slots in the map. If the slot falls outside the map
2881 * (for example, when all icons are placed in line), ignore them. */
2882 for (i=0; i<dock->max_icons; i++) {
2883 btn = dock->icon_array[i];
2884 if (btn)
2885 slot_map[XY2OFS(btn->xindex, btn->yindex)] = 1;
2887 for (chain=scr->global_icons; chain!=NULL; chain=chain->next) {
2888 slot_map[XY2OFS(chain->aicon->xindex, chain->aicon->yindex)] = 1;
2890 /* Find closest slot from the center that is free by scanning the
2891 * map from the center to outward in circular passes.
2892 * This will not result in a neat layout, but will be optimal
2893 * in the sense that there will not be holes left.
2895 done = 0;
2896 for (i = 1; i <= r && !done; i++) {
2897 int tx, ty;
2899 /* top and bottom parts of the ring */
2900 for (x = -i; x <= i && !done; x++) {
2901 tx = dock->x_pos + x*ICON_SIZE;
2902 y = -i;
2903 ty = dock->y_pos + y*ICON_SIZE;
2904 if (slot_map[XY2OFS(x,y)]==0
2905 && ON_SCREEN(tx, ty, sx, ex, sy, ey)) {
2906 *x_pos = x;
2907 *y_pos = y;
2908 done = 1;
2909 break;
2911 y = i;
2912 ty = dock->y_pos + y*ICON_SIZE;
2913 if (slot_map[XY2OFS(x,y)]==0
2914 && ON_SCREEN(tx, ty, sx, ex, sy, ey)) {
2915 *x_pos = x;
2916 *y_pos = y;
2917 done = 1;
2918 break;
2921 /* left and right parts of the ring */
2922 for (y = -i+1; y <= i-1; y++) {
2923 ty = dock->y_pos + y*ICON_SIZE;
2924 x = -i;
2925 tx = dock->x_pos + x*ICON_SIZE;
2926 if (slot_map[XY2OFS(x,y)]==0
2927 && ON_SCREEN(tx, ty, sx, ex, sy, ey)) {
2928 *x_pos = x;
2929 *y_pos = y;
2930 done = 1;
2931 break;
2933 x = i;
2934 tx = dock->x_pos + x*ICON_SIZE;
2935 if (slot_map[XY2OFS(x,y)]==0
2936 && ON_SCREEN(tx, ty, sx, ex, sy, ey)) {
2937 *x_pos = x;
2938 *y_pos = y;
2939 done = 1;
2940 break;
2944 free(slot_map);
2945 #undef XY2OFS
2946 return done;
2950 static void
2951 moveDock(WDock *dock, int new_x, int new_y)
2953 WAppIcon *btn;
2954 int i;
2956 dock->x_pos = new_x;
2957 dock->y_pos = new_y;
2958 for (i=0; i<dock->max_icons; i++) {
2959 btn = dock->icon_array[i];
2960 if (btn) {
2961 btn->x_pos = new_x + btn->xindex*ICON_SIZE;
2962 btn->y_pos = new_y + btn->yindex*ICON_SIZE;
2963 XMoveWindow(dpy, btn->icon->core->window, btn->x_pos, btn->y_pos);
2969 static void
2970 swapDock(WDock *dock)
2972 WScreen *scr = dock->screen_ptr;
2973 WAppIcon *btn;
2974 int x, i;
2977 if (dock->on_right_side) {
2978 x = dock->x_pos = scr->scr_width - ICON_SIZE - DOCK_EXTRA_SPACE;
2979 } else {
2980 x = dock->x_pos = DOCK_EXTRA_SPACE;
2983 for (i=0; i<dock->max_icons; i++) {
2984 btn = dock->icon_array[i];
2985 if (btn) {
2986 btn->x_pos = x;
2987 XMoveWindow(dpy, btn->icon->core->window, btn->x_pos, btn->y_pos);
2991 wScreenUpdateUsableArea(scr);
2995 static pid_t
2996 execCommand(WAppIcon *btn, char *command, WSavedState *state)
2998 WScreen *scr = btn->icon->core->screen_ptr;
2999 pid_t pid;
3000 char **argv;
3001 int argc;
3002 char *cmdline;
3004 cmdline = ExpandOptions(scr, command);
3006 if (scr->flags.dnd_data_convertion_status || !cmdline) {
3007 if (cmdline)
3008 free(cmdline);
3009 if (state)
3010 free(state);
3011 return 0;
3014 ParseCommand(cmdline, &argv, &argc);
3016 if (argv==NULL) {
3017 if (cmdline)
3018 free(cmdline);
3019 if (state)
3020 free(state);
3021 return 0;
3024 if ((pid=fork())==0) {
3025 char **args;
3026 int i;
3028 SetupEnvironment(scr);
3030 #ifdef HAVE_SETPGID
3031 setpgid(0, 0);
3032 #endif
3034 args = malloc(sizeof(char*)*(argc+1));
3035 if (!args)
3036 exit(111);
3037 for (i=0; i<argc; i++) {
3038 args[i] = argv[i];
3040 args[argc] = NULL;
3041 execvp(argv[0], args);
3042 exit(111);
3044 while (argc > 0)
3045 free(argv[--argc]);
3046 free(argv);
3048 if (pid > 0) {
3049 if (!state) {
3050 state = wmalloc(sizeof(WSavedState));
3051 memset(state, 0, sizeof(WSavedState));
3052 state->hidden = -1;
3053 state->miniaturized = -1;
3054 state->shaded = -1;
3055 if (btn->dock==scr->dock || btn->omnipresent)
3056 state->workspace = -1;
3057 else
3058 state->workspace = scr->current_workspace;
3060 wWindowAddSavedState(btn->wm_instance, btn->wm_class, cmdline, pid,
3061 state);
3062 wAddDeathHandler(pid, (WDeathHandler*)trackDeadProcess,
3063 btn->dock);
3064 } else if (state) {
3065 free(state);
3067 free(cmdline);
3068 return pid;
3072 void
3073 wDockHideIcons(WDock *dock)
3075 int i;
3077 if (dock==NULL)
3078 return;
3080 for (i=1; i<dock->max_icons; i++) {
3081 if (dock->icon_array[i])
3082 XUnmapWindow(dpy, dock->icon_array[i]->icon->core->window);
3084 dock->mapped = 0;
3086 dockIconPaint(dock->icon_array[0]);
3090 void
3091 wDockShowIcons(WDock *dock)
3093 int i, newlevel;
3094 WAppIcon *btn;
3096 if (dock==NULL)
3097 return;
3099 btn = dock->icon_array[0];
3100 moveDock(dock, btn->x_pos, btn->y_pos);
3102 newlevel = dock->lowered ? WMNormalLevel : WMDockLevel;
3103 ChangeStackingLevel(btn->icon->core, newlevel);
3105 for (i=1; i<dock->max_icons; i++) {
3106 if (dock->icon_array[i]) {
3107 MoveInStackListAbove(dock->icon_array[i]->icon->core,
3108 btn->icon->core);
3109 break;
3113 if (!dock->collapsed) {
3114 for (i=1; i<dock->max_icons; i++) {
3115 if (dock->icon_array[i]) {
3116 XMapWindow(dpy, dock->icon_array[i]->icon->core->window);
3120 dock->mapped = 1;
3122 dockIconPaint(btn);
3126 void
3127 wDockLower(WDock *dock)
3129 int i;
3131 for (i=0; i<dock->max_icons; i++) {
3132 if (dock->icon_array[i])
3133 wLowerFrame(dock->icon_array[i]->icon->core);
3138 void
3139 wDockRaise(WDock *dock)
3141 int i;
3143 for (i=dock->max_icons-1; i>=0; i--) {
3144 if (dock->icon_array[i])
3145 wRaiseFrame(dock->icon_array[i]->icon->core);
3150 void
3151 wDockRaiseLower(WDock *dock)
3153 if (!dock->icon_array[0]->icon->core->stacking->above
3154 ||(dock->icon_array[0]->icon->core->stacking->window_level
3155 !=dock->icon_array[0]->icon->core->stacking->above->stacking->window_level))
3156 wDockLower(dock);
3157 else
3158 wDockRaise(dock);
3162 void
3163 wDockFinishLaunch(WDock *dock, WAppIcon *icon)
3165 icon->launching = 0;
3166 icon->relaunching = 0;
3167 dockIconPaint(icon);
3171 WAppIcon*
3172 wDockFindIconFor(WDock *dock, Window window)
3174 WAppIcon *icon;
3175 int i;
3177 for (i=0; i<dock->max_icons; i++) {
3178 icon = dock->icon_array[i];
3179 if (icon && icon->main_window == window)
3180 return icon;
3182 return NULL;
3186 void
3187 wDockTrackWindowLaunch(WDock *dock, Window window)
3189 WAppIcon *icon;
3190 #ifdef REDUCE_APPICONS
3191 WAppIconAppList *tapplist;
3192 #endif
3193 char *wm_class, *wm_instance;
3194 int i;
3195 Bool firstPass = True;
3196 Bool found = False;
3197 char *command = NULL;
3200 int argc;
3201 char **argv;
3203 if (XGetCommand(dpy, window, &argv, &argc)) {
3204 if (argc > 0 && argv != NULL)
3205 command = FlattenStringList(argv,argc);
3206 if (argv) {
3207 XFreeStringList(argv);
3212 if (!PropGetWMClass(window, &wm_class, &wm_instance) ||
3213 (!wm_class && !wm_instance))
3214 return;
3216 retry:
3217 for (i=0; i<dock->max_icons; i++) {
3218 icon = dock->icon_array[i];
3219 if (!icon)
3220 continue;
3222 /* app is already attached to icon */
3223 if (icon->main_window == window) {
3224 found = True;
3225 break;
3228 if ((icon->wm_instance || icon->wm_class)
3229 && (icon->launching
3230 || (dock->screen_ptr->flags.startup && !icon->running))) {
3232 if (icon->wm_instance && wm_instance &&
3233 strcmp(icon->wm_instance, wm_instance)!=0) {
3234 continue;
3236 if (icon->wm_class && wm_class &&
3237 strcmp(icon->wm_class, wm_class)!=0) {
3238 continue;
3240 if (firstPass && command && strcmp(icon->command, command)!=0) {
3241 continue;
3244 if (!icon->relaunching) {
3245 WApplication *wapp;
3247 /* Possibly an application that was docked with dockit,
3248 * but the user did not update WMState to indicate that
3249 * it was docked by force */
3250 wapp = wApplicationOf(window);
3251 if (!wapp) {
3252 icon->forced_dock = 1;
3253 icon->running = 0;
3255 if (!icon->forced_dock)
3256 icon->main_window = window;
3258 #ifdef REDUCE_APPICONS
3259 tapplist = wmalloc(sizeof(WAppIconAppList));
3260 memset(tapplist, 0, sizeof(WAppIconAppList));
3261 tapplist->next = icon->applist;
3262 if (icon->applist)
3263 icon->applist->prev = tapplist;
3264 icon->applist = tapplist;
3265 tapplist->wapp = wApplicationOf(window);
3266 icon->num_apps++;
3267 #endif
3269 found = True;
3270 wDockFinishLaunch(dock, icon);
3271 break;
3275 if (firstPass && !found) {
3276 firstPass = False;
3277 goto retry;
3280 if (command)
3281 free(command);
3283 if (wm_class)
3284 XFree(wm_class);
3285 if (wm_instance)
3286 XFree(wm_instance);
3291 void
3292 wClipUpdateForWorkspaceChange(WScreen *scr, int workspace)
3294 if (!wPreferences.flags.noclip) {
3295 scr->clip_icon->dock = scr->workspaces[workspace]->clip;
3296 if (scr->current_workspace != workspace) {
3297 WDock *old_clip = scr->workspaces[scr->current_workspace]->clip;
3298 WAppIconChain *chain = scr->global_icons;
3300 while (chain) {
3301 moveIconBetweenDocks(chain->aicon->dock,
3302 scr->workspaces[workspace]->clip,
3303 chain->aicon, chain->aicon->xindex,
3304 chain->aicon->yindex);
3305 if (scr->workspaces[workspace]->clip->collapsed)
3306 XUnmapWindow(dpy, chain->aicon->icon->core->window);
3307 chain = chain->next;
3310 wDockHideIcons(old_clip);
3311 if (old_clip->auto_raise_lower) {
3312 if (old_clip->auto_raise_magic) {
3313 WMDeleteTimerHandler(old_clip->auto_raise_magic);
3314 old_clip->auto_raise_magic = NULL;
3316 wDockLower(old_clip);
3318 if (old_clip->auto_collapse) {
3319 if (old_clip->auto_expand_magic) {
3320 WMDeleteTimerHandler(old_clip->auto_expand_magic);
3321 old_clip->auto_expand_magic = NULL;
3323 old_clip->collapsed = 1;
3325 wDockShowIcons(scr->workspaces[workspace]->clip);
3327 if (scr->flags.clip_balloon_mapped)
3328 showClipBalloon(scr->clip_icon->dock, workspace);
3334 static void
3335 trackDeadProcess(pid_t pid, unsigned char status, WDock *dock)
3337 WAppIcon *icon;
3338 int i;
3340 for (i=0; i<dock->max_icons; i++) {
3341 icon = dock->icon_array[i];
3342 if (!icon)
3343 continue;
3345 if (icon->launching && icon->pid == pid) {
3346 if (!icon->relaunching) {
3347 icon->running = 0;
3348 icon->main_window = None;
3350 wDockFinishLaunch(dock, icon);
3351 icon->pid = 0;
3352 if (status==111) {
3353 char msg[PATH_MAX];
3354 #ifdef OFFIX_DND
3355 sprintf(msg, _("Could not execute command \"%s\""),
3356 icon->drop_launch && icon->dnd_command
3357 ? icon->dnd_command : icon->command);
3358 #else
3359 sprintf(msg, _("Could not execute command \"%s\""),
3360 icon->command);
3361 #endif
3362 wMessageDialog(dock->screen_ptr, _("Error"), msg,
3363 _("OK"), NULL, NULL);
3365 break;
3371 static void
3372 toggleLowered(WDock *dock)
3374 WAppIcon *tmp;
3375 int newlevel, i;
3377 /* lower/raise Dock */
3378 if (!dock->lowered) {
3379 newlevel = WMNormalLevel;
3380 dock->lowered = 1;
3381 } else {
3382 newlevel = WMDockLevel;
3383 dock->lowered = 0;
3386 for (i=0; i<dock->max_icons; i++) {
3387 tmp = dock->icon_array[i];
3388 if (!tmp)
3389 continue;
3391 ChangeStackingLevel(tmp->icon->core, newlevel);
3392 if (dock->lowered)
3393 wLowerFrame(tmp->icon->core);
3396 if (dock->type == WM_DOCK)
3397 wScreenUpdateUsableArea(dock->screen_ptr);
3401 static void
3402 toggleCollapsed(WDock *dock)
3404 if (dock->collapsed) {
3405 dock->collapsed = 0;
3406 wDockShowIcons(dock);
3408 else {
3409 dock->collapsed = 1;
3410 wDockHideIcons(dock);
3415 static void
3416 openDockMenu(WDock *dock, WAppIcon *aicon, XEvent *event)
3418 WScreen *scr = dock->screen_ptr;
3419 WObjDescriptor *desc;
3420 WMenuEntry *entry;
3421 WApplication *wapp = NULL;
3422 int index = 0;
3423 int x_pos;
3424 int n_selected;
3425 int appIsRunning = aicon->running && aicon->icon && aicon->icon->owner;
3427 if (dock->type == WM_DOCK) {
3428 /* keep on top */
3429 entry = dock->menu->entries[index];
3430 entry->flags.indicator_on = !dock->lowered;
3431 entry->clientdata = dock;
3432 } else {
3433 /* clip options */
3434 if (scr->clip_options)
3435 updateClipOptionsMenu(scr->clip_options, dock);
3437 n_selected = numberOfSelectedIcons(dock);
3439 /* Rename Workspace */
3440 entry = dock->menu->entries[++index];
3441 if (aicon == scr->clip_icon) {
3442 entry->callback = renameCallback;
3443 entry->clientdata = dock;
3444 entry->flags.indicator = 0;
3445 entry->text = _("Rename Workspace");
3446 } else {
3447 entry->callback = omnipresentCallback;
3448 entry->clientdata = aicon;
3449 if (n_selected > 0) {
3450 entry->flags.indicator = 0;
3451 entry->text = _("Toggle Omnipresent");
3452 } else {
3453 entry->flags.indicator = 1;
3454 entry->flags.indicator_on = aicon->omnipresent;
3455 entry->flags.indicator_type = MI_CHECK;
3456 entry->text = _("Omnipresent");
3460 /* select/unselect icon */
3461 entry = dock->menu->entries[++index];
3462 entry->clientdata = aicon;
3463 entry->flags.indicator_on = aicon->icon->selected;
3464 wMenuSetEnabled(dock->menu, index, aicon!=scr->clip_icon);
3466 /* select/unselect all icons */
3467 entry = dock->menu->entries[++index];
3468 entry->clientdata = aicon;
3469 if (n_selected > 0)
3470 entry->text = _("Unselect All Icons");
3471 else
3472 entry->text = _("Select All Icons");
3473 wMenuSetEnabled(dock->menu, index, dock->icon_count > 1);
3475 /* keep icon(s) */
3476 entry = dock->menu->entries[++index];
3477 entry->clientdata = aicon;
3478 if (n_selected > 1)
3479 entry->text = _("Keep Icons");
3480 else
3481 entry->text = _("Keep Icon");
3482 wMenuSetEnabled(dock->menu, index, dock->icon_count > 1);
3484 /* this is the workspace submenu part */
3485 entry = dock->menu->entries[++index];
3486 if (n_selected > 1)
3487 entry->text = _("Move Icons To");
3488 else
3489 entry->text = _("Move Icon To");
3490 if (scr->clip_submenu)
3491 updateWorkspaceMenu(scr->clip_submenu, aicon);
3492 wMenuSetEnabled(dock->menu, index, !aicon->omnipresent);
3494 /* remove icon(s) */
3495 entry = dock->menu->entries[++index];
3496 entry->clientdata = aicon;
3497 if (n_selected > 1)
3498 entry->text = _("Remove Icons");
3499 else
3500 entry->text = _("Remove Icon");
3501 wMenuSetEnabled(dock->menu, index, dock->icon_count > 1);
3503 /* attract icon(s) */
3504 entry = dock->menu->entries[++index];
3505 entry->clientdata = aicon;
3507 dock->menu->flags.realized = 0;
3508 wMenuRealize(dock->menu);
3511 /* launch */
3512 entry = dock->menu->entries[++index];
3513 entry->clientdata = aicon;
3514 wMenuSetEnabled(dock->menu, index, aicon->command!=NULL);
3516 /* unhide here */
3517 entry = dock->menu->entries[++index];
3518 entry->clientdata = aicon;
3519 wMenuSetEnabled(dock->menu, index, appIsRunning);
3521 /* hide */
3522 entry = dock->menu->entries[++index];
3523 entry->clientdata = aicon;
3524 if (aicon->icon->owner) {
3525 wapp = wApplicationOf(aicon->icon->owner->main_window);
3526 if (wapp && wapp->flags.hidden)
3527 entry->text = _("Unhide");
3528 else
3529 entry->text = _("Hide");
3530 } else {
3531 entry->text = _("Hide");
3533 wMenuSetEnabled(dock->menu, index, appIsRunning);
3535 /* settings */
3536 entry = dock->menu->entries[++index];
3537 entry->clientdata = aicon;
3538 wMenuSetEnabled(dock->menu, index, !aicon->editing
3539 && !wPreferences.flags.noupdates);
3541 /* kill */
3542 entry = dock->menu->entries[++index];
3543 entry->clientdata = aicon;
3544 wMenuSetEnabled(dock->menu, index, appIsRunning);
3546 if (!dock->menu->flags.realized)
3547 wMenuRealize(dock->menu);
3549 if (dock->type == WM_CLIP) {
3550 x_pos = event->xbutton.x_root+2;
3551 } else {
3552 x_pos = dock->on_right_side ?
3553 scr->scr_width - dock->menu->frame->core->width - 2 : 0;
3556 wMenuMapAt(dock->menu, x_pos, event->xbutton.y_root+2, False);
3558 /* allow drag select */
3559 event->xany.send_event = True;
3560 desc = &dock->menu->menu->descriptor;
3561 (*desc->handle_mousedown)(desc, event);
3565 static void
3566 openClipWorkspaceMenu(WScreen *scr, int x, int y)
3568 if (!scr->clip_ws_menu) {
3569 scr->clip_ws_menu = wWorkspaceMenuMake(scr, False);
3571 wWorkspaceMenuUpdate(scr, scr->clip_ws_menu);
3572 wMenuMapAt(scr->clip_ws_menu, x, y, False);
3576 /******************************************************************/
3577 static void
3578 iconDblClick(WObjDescriptor *desc, XEvent *event)
3580 WAppIcon *btn = desc->parent;
3581 WDock *dock = btn->dock;
3582 WApplication *wapp = NULL;
3583 int unhideHere = 0;
3585 #ifdef REDUCE_APPICONS
3586 if ((btn->icon->owner && !(event->xbutton.state & ControlMask)) ||
3587 ((btn->icon->owner == NULL) && (btn->applist != NULL))) {
3588 if (btn->icon->owner == NULL)
3589 btn->icon->owner = btn->applist->wapp->main_window_desc;
3590 #else
3591 if (btn->icon->owner && !(event->xbutton.state & ControlMask)) {
3592 #endif
3593 wapp = wApplicationOf(btn->icon->owner->main_window);
3595 assert(wapp!=NULL);
3597 unhideHere = (event->xbutton.state & ShiftMask);
3599 /* go to the last workspace that the user worked on the app */
3600 if (wapp->last_workspace != dock->screen_ptr->current_workspace
3601 && !unhideHere) {
3602 wWorkspaceChange(dock->screen_ptr, wapp->last_workspace);
3605 wUnhideApplication(wapp, event->xbutton.button==Button2,
3606 unhideHere);
3608 if (event->xbutton.state & MOD_MASK) {
3609 wHideOtherApplications(btn->icon->owner);
3611 } else {
3612 if (event->xbutton.button==Button1) {
3614 if (event->xbutton.state & MOD_MASK) {
3615 /* raise/lower dock */
3616 toggleLowered(dock);
3617 } else if (btn == dock->screen_ptr->clip_icon) {
3618 if (getClipButton(event->xbutton.x, event->xbutton.y)==CLIP_IDLE)
3619 toggleCollapsed(dock);
3620 else
3621 handleClipChangeWorkspace(dock->screen_ptr, event);
3622 } else if (btn->command) {
3623 if (!btn->launching &&
3624 (!btn->running || (event->xbutton.state & ControlMask))) {
3625 launchDockedApplication(btn);
3627 } else if (btn->xindex == 0 && btn->yindex == 0
3628 && btn->dock->type == WM_DOCK) {
3630 wShowGNUstepPanel(dock->screen_ptr);
3637 static void
3638 handleDockMove(WDock *dock, WAppIcon *aicon, XEvent *event)
3640 WScreen *scr = dock->screen_ptr;
3641 int ofs_x=event->xbutton.x, ofs_y=event->xbutton.y;
3642 int x, y;
3643 XEvent ev;
3644 int grabbed = 0, swapped = 0, done;
3645 Pixmap ghost = None;
3646 int superfluous = wPreferences.superfluous; /* we catch it to avoid problems */
3648 #ifdef DEBUG
3649 puts("moving dock");
3650 #endif
3651 if (XGrabPointer(dpy, aicon->icon->core->window, True, ButtonMotionMask
3652 |ButtonReleaseMask|ButtonPressMask, GrabModeAsync,
3653 GrabModeAsync, None, None, CurrentTime) !=GrabSuccess) {
3654 wwarning("pointer grab failed for dock move");
3656 y = 0;
3657 for (x=0; x<dock->max_icons; x++) {
3658 if (dock->icon_array[x]!=NULL &&
3659 dock->icon_array[x]->yindex > y)
3660 y = dock->icon_array[x]->yindex;
3662 y++;
3663 XResizeWindow(dpy, scr->dock_shadow, ICON_SIZE, ICON_SIZE*y);
3665 done = 0;
3666 while (!done) {
3667 WMMaskEvent(dpy, PointerMotionMask|ButtonReleaseMask|ButtonPressMask
3668 |ButtonMotionMask|ExposureMask, &ev);
3669 switch (ev.type) {
3670 case Expose:
3671 WMHandleEvent(&ev);
3672 break;
3674 case MotionNotify:
3675 if (!grabbed) {
3676 if (abs(ofs_x-ev.xmotion.x)>=MOVE_THRESHOLD
3677 || abs(ofs_y-ev.xmotion.y)>=MOVE_THRESHOLD) {
3678 XChangeActivePointerGrab(dpy, ButtonMotionMask
3679 |ButtonReleaseMask|ButtonPressMask,
3680 wCursor[WCUR_MOVE], CurrentTime);
3681 grabbed=1;
3683 break;
3685 if (dock->type == WM_CLIP) {
3686 if (ev.xmotion.x_root - ofs_x < 0) {
3687 x = 0;
3688 } else if (ev.xmotion.x_root - ofs_x + ICON_SIZE >
3689 scr->scr_width) {
3690 x = scr->scr_width - ICON_SIZE;
3691 } else {
3692 x = ev.xmotion.x_root - ofs_x;
3694 if (ev.xmotion.y_root - ofs_y < 0) {
3695 y = 0;
3696 } else if (ev.xmotion.y_root - ofs_y + ICON_SIZE >
3697 scr->scr_height) {
3698 y = scr->scr_height - ICON_SIZE;
3699 } else {
3700 y = ev.xmotion.y_root - ofs_y;
3702 moveDock(dock, x, y);
3703 } else {
3704 /* move vertically if pointer is inside the dock*/
3705 if ((dock->on_right_side &&
3706 ev.xmotion.x_root >= dock->x_pos - ICON_SIZE)
3707 || (!dock->on_right_side &&
3708 ev.xmotion.x_root <= dock->x_pos + ICON_SIZE*2)) {
3710 if (ev.xmotion.y_root - ofs_y < 0) {
3711 y = 0;
3712 } else if (ev.xmotion.y_root - ofs_y + ICON_SIZE >
3713 scr->scr_height) {
3714 y = scr->scr_height - ICON_SIZE;
3715 } else {
3716 y = ev.xmotion.y_root - ofs_y;
3718 moveDock(dock, dock->x_pos, y);
3720 /* move horizontally to change sides */
3721 x = ev.xmotion.x_root - ofs_x;
3722 if (!dock->on_right_side) {
3724 /* is on left */
3726 if (ev.xmotion.x_root > dock->x_pos + ICON_SIZE*2) {
3727 XMoveWindow(dpy, scr->dock_shadow, scr->scr_width-ICON_SIZE
3728 -DOCK_EXTRA_SPACE-1, dock->y_pos);
3729 if (superfluous && ghost==None) {
3730 ghost = MakeGhostDock(dock, dock->x_pos,
3731 scr->scr_width-ICON_SIZE
3732 -DOCK_EXTRA_SPACE-1,
3733 dock->y_pos);
3734 XSetWindowBackgroundPixmap(dpy, scr->dock_shadow,
3735 ghost);
3736 XClearWindow(dpy, scr->dock_shadow);
3738 XMapRaised(dpy, scr->dock_shadow);
3739 swapped = 1;
3740 } else {
3741 if (superfluous && ghost!=None) {
3742 XFreePixmap(dpy, ghost);
3743 ghost = None;
3745 XUnmapWindow(dpy, scr->dock_shadow);
3746 swapped = 0;
3748 } else {
3749 /* is on right */
3750 if (ev.xmotion.x_root < dock->x_pos - ICON_SIZE) {
3751 XMoveWindow(dpy, scr->dock_shadow,
3752 DOCK_EXTRA_SPACE, dock->y_pos);
3753 if (superfluous && ghost==None) {
3754 ghost = MakeGhostDock(dock, dock->x_pos,
3755 DOCK_EXTRA_SPACE, dock->y_pos);
3756 XSetWindowBackgroundPixmap(dpy, scr->dock_shadow,
3757 ghost);
3758 XClearWindow(dpy, scr->dock_shadow);
3760 XMapRaised(dpy, scr->dock_shadow);
3761 swapped = -1;
3762 } else {
3763 XUnmapWindow(dpy, scr->dock_shadow);
3764 swapped = 0;
3765 if (superfluous && ghost!=None) {
3766 XFreePixmap(dpy, ghost);
3767 ghost = None;
3772 break;
3774 case ButtonPress:
3775 break;
3777 case ButtonRelease:
3778 if (ev.xbutton.button != event->xbutton.button)
3779 break;
3780 XUngrabPointer(dpy, CurrentTime);
3781 XUnmapWindow(dpy, scr->dock_shadow);
3782 XResizeWindow(dpy, scr->dock_shadow, ICON_SIZE, ICON_SIZE);
3783 if (dock->type == WM_DOCK) {
3784 if (swapped!=0) {
3785 if (swapped>0)
3786 dock->on_right_side = 1;
3787 else
3788 dock->on_right_side = 0;
3789 swapDock(dock);
3790 wArrangeIcons(scr, False);
3793 done = 1;
3794 break;
3797 if (superfluous) {
3798 if (ghost!=None)
3799 XFreePixmap(dpy, ghost);
3800 XSetWindowBackground(dpy, scr->dock_shadow, scr->white_pixel);
3802 #ifdef DEBUG
3803 puts("End dock move");
3804 #endif
3809 static void
3810 handleIconMove(WDock *dock, WAppIcon *aicon, XEvent *event)
3812 WScreen *scr = dock->screen_ptr;
3813 Window wins[2];
3814 WIcon *icon = aicon->icon;
3815 WDock *dock2 = NULL, *last_dock = dock, *clip = NULL;
3816 int ondock, grabbed = 0, change_dock = 0, collapsed = 0;
3817 XEvent ev;
3818 int x = aicon->x_pos, y = aicon->y_pos;
3819 int ofs_x = event->xbutton.x, ofs_y = event->xbutton.y;
3820 int shad_x = x, shad_y = y;
3821 int ix = aicon->xindex, iy = aicon->yindex;
3822 int tmp;
3823 Pixmap ghost = None;
3824 Bool docked;
3825 int superfluous = wPreferences.superfluous; /* we catch it to avoid problems */
3826 int omnipresent = aicon->omnipresent; /* this must be cached!!! */
3829 if (wPreferences.flags.noupdates)
3830 return;
3832 if (XGrabPointer(dpy, icon->core->window, True, ButtonMotionMask
3833 |ButtonReleaseMask|ButtonPressMask, GrabModeAsync,
3834 GrabModeAsync, None, None, CurrentTime) !=GrabSuccess) {
3835 #ifdef DEBUG0
3836 wwarning("pointer grab failed for icon move");
3837 #endif
3840 if (!(event->xbutton.state & MOD_MASK))
3841 wRaiseFrame(icon->core);
3843 if (!wPreferences.flags.noclip)
3844 clip = scr->workspaces[scr->current_workspace]->clip;
3846 if (dock == scr->dock && !wPreferences.flags.noclip)
3847 dock2 = clip;
3848 else if (dock != scr->dock && !wPreferences.flags.nodock)
3849 dock2 = scr->dock;
3851 wins[0] = icon->core->window;
3852 wins[1] = scr->dock_shadow;
3853 XRestackWindows(dpy, wins, 2);
3854 XMoveResizeWindow(dpy, scr->dock_shadow, aicon->x_pos, aicon->y_pos,
3855 ICON_SIZE, ICON_SIZE);
3856 if (superfluous) {
3857 if (icon->pixmap!=None)
3858 ghost = MakeGhostIcon(scr, icon->pixmap);
3859 else
3860 ghost = MakeGhostIcon(scr, icon->core->window);
3862 XSetWindowBackgroundPixmap(dpy, scr->dock_shadow, ghost);
3863 XClearWindow(dpy, scr->dock_shadow);
3865 XMapWindow(dpy, scr->dock_shadow);
3867 ondock = 1;
3870 while(1) {
3871 XMaskEvent(dpy, PointerMotionMask|ButtonReleaseMask|ButtonPressMask
3872 |ButtonMotionMask|ExposureMask, &ev);
3873 switch (ev.type) {
3874 case Expose:
3875 WMHandleEvent(&ev);
3876 break;
3878 case MotionNotify:
3879 if (!grabbed) {
3880 if (abs(ofs_x-ev.xmotion.x)>=MOVE_THRESHOLD
3881 || abs(ofs_y-ev.xmotion.y)>=MOVE_THRESHOLD) {
3882 XChangeActivePointerGrab(dpy, ButtonMotionMask
3883 |ButtonReleaseMask|ButtonPressMask,
3884 wCursor[WCUR_MOVE], CurrentTime);
3885 grabbed=1;
3886 } else {
3887 break;
3891 if (omnipresent) {
3892 int i;
3893 for (i=0; i<scr->workspace_count; i++) {
3894 if (i == scr->current_workspace)
3895 continue;
3896 wDockShowIcons(scr->workspaces[i]->clip);
3900 x = ev.xmotion.x_root - ofs_x;
3901 y = ev.xmotion.y_root - ofs_y;
3902 tmp = wDockSnapIcon(dock, aicon, x, y, &ix, &iy, True);
3903 if (tmp && dock2) {
3904 change_dock = 0;
3905 if (last_dock != dock && collapsed) {
3906 last_dock->collapsed = 1;
3907 wDockHideIcons(last_dock);
3908 collapsed = 0;
3910 if (!collapsed && (collapsed = dock->collapsed)) {
3911 dock->collapsed = 0;
3912 wDockShowIcons(dock);
3914 if (dock->auto_raise_lower)
3915 wDockRaise(dock);
3916 last_dock = dock;
3917 } else if (dock2) {
3918 tmp = wDockSnapIcon(dock2, aicon, x, y, &ix, &iy, False);
3919 if (tmp) {
3920 change_dock = 1;
3921 if (last_dock != dock2 && collapsed) {
3922 last_dock->collapsed = 1;
3923 wDockHideIcons(last_dock);
3924 collapsed = 0;
3926 if (!collapsed && (collapsed = dock2->collapsed)) {
3927 dock2->collapsed = 0;
3928 wDockShowIcons(dock2);
3930 if (dock2->auto_raise_lower)
3931 wDockRaise(dock2);
3932 last_dock = dock2;
3935 if (aicon->launching
3936 || (aicon->running && !(ev.xmotion.state & MOD_MASK))
3937 || (!aicon->running && tmp)) {
3938 shad_x = last_dock->x_pos + ix*wPreferences.icon_size;
3939 shad_y = last_dock->y_pos + iy*wPreferences.icon_size;
3941 XMoveWindow(dpy, scr->dock_shadow, shad_x, shad_y);
3943 if (!ondock) {
3944 XMapWindow(dpy, scr->dock_shadow);
3946 ondock = 1;
3947 } else {
3948 if (ondock) {
3949 XUnmapWindow(dpy, scr->dock_shadow);
3951 ondock = 0;
3953 XMoveWindow(dpy, icon->core->window, x, y);
3954 break;
3956 case ButtonPress:
3957 break;
3959 case ButtonRelease:
3960 if (ev.xbutton.button != event->xbutton.button)
3961 break;
3962 XUngrabPointer(dpy, CurrentTime);
3963 if (ondock) {
3964 SlideWindow(icon->core->window, x, y, shad_x, shad_y);
3965 XUnmapWindow(dpy, scr->dock_shadow);
3966 if (!change_dock) {
3967 reattachIcon(dock, aicon, ix, iy);
3968 if (clip && dock!=clip && clip->auto_raise_lower)
3969 wDockLower(clip);
3970 } else {
3971 docked = moveIconBetweenDocks(dock, dock2, aicon, ix, iy);
3972 if (!docked) {
3973 /* Slide it back if dock rejected it */
3974 SlideWindow(icon->core->window, x, y, aicon->x_pos,
3975 aicon->y_pos);
3976 reattachIcon(dock, aicon, aicon->xindex,aicon->yindex);
3978 if (last_dock->type==WM_CLIP && last_dock->auto_collapse) {
3979 collapsed = 0;
3982 } else {
3983 aicon->x_pos = x;
3984 aicon->y_pos = y;
3985 if (superfluous) {
3986 if (!aicon->running && !wPreferences.no_animations) {
3987 /* We need to deselect it, even if is deselected in
3988 * wDockDetach(), because else DoKaboom() will fail.
3990 if (aicon->icon->selected)
3991 wIconSelect(aicon->icon);
3992 DoKaboom(scr,aicon->icon->core->window, x, y);
3995 if (clip && clip->auto_raise_lower)
3996 wDockLower(clip);
3997 wDockDetach(dock, aicon);
3999 if (collapsed) {
4000 last_dock->collapsed = 1;
4001 wDockHideIcons(last_dock);
4002 collapsed = 0;
4004 if (superfluous) {
4005 if (ghost!=None)
4006 XFreePixmap(dpy, ghost);
4007 XSetWindowBackground(dpy, scr->dock_shadow, scr->white_pixel);
4009 if (omnipresent) {
4010 int i;
4011 for (i=0; i<scr->workspace_count; i++) {
4012 if (i == scr->current_workspace)
4013 continue;
4014 wDockHideIcons(scr->workspaces[i]->clip);
4018 #ifdef DEBUG
4019 puts("End icon move");
4020 #endif
4021 return;
4027 static int
4028 getClipButton(int px, int py)
4030 int pt = (CLIP_BUTTON_SIZE+2)*ICON_SIZE/64;
4032 if (px < 0 || py < 0 || px >= ICON_SIZE || py >= ICON_SIZE)
4033 return CLIP_IDLE;
4035 if (py <= pt-((int)ICON_SIZE-1-px))
4036 return CLIP_FORWARD;
4037 else if (px <= pt-((int)ICON_SIZE-1-py))
4038 return CLIP_REWIND;
4040 return CLIP_IDLE;
4044 static void
4045 handleClipChangeWorkspace(WScreen *scr, XEvent *event)
4047 XEvent ev;
4048 int done, direction, new_ws;
4049 int new_dir;
4050 WDock *clip = scr->clip_icon->dock;
4052 direction = getClipButton(event->xbutton.x, event->xbutton.y);
4054 clip->lclip_button_pushed = direction==CLIP_REWIND;
4055 clip->rclip_button_pushed = direction==CLIP_FORWARD;
4057 wClipIconPaint(scr->clip_icon);
4058 done = 0;
4059 while(!done) {
4060 WMMaskEvent(dpy, ExposureMask|ButtonMotionMask|ButtonReleaseMask
4061 |ButtonPressMask, &ev);
4062 switch (ev.type) {
4063 case Expose:
4064 WMHandleEvent(&ev);
4065 break;
4067 case MotionNotify:
4068 new_dir = getClipButton(ev.xmotion.x, ev.xmotion.y);
4069 if (new_dir != direction) {
4070 direction = new_dir;
4071 clip->lclip_button_pushed = direction==CLIP_REWIND;
4072 clip->rclip_button_pushed = direction==CLIP_FORWARD;
4073 wClipIconPaint(scr->clip_icon);
4075 break;
4077 case ButtonPress:
4078 break;
4080 case ButtonRelease:
4081 if (ev.xbutton.button == event->xbutton.button)
4082 done = 1;
4086 clip->lclip_button_pushed = 0;
4087 clip->rclip_button_pushed = 0;
4089 new_ws = wPreferences.ws_advance || (event->xbutton.state & ControlMask);
4091 if (direction == CLIP_FORWARD) {
4092 if (scr->current_workspace < scr->workspace_count-1)
4093 wWorkspaceChange(scr, scr->current_workspace+1);
4094 else if (new_ws && scr->current_workspace < MAX_WORKSPACES-1)
4095 wWorkspaceChange(scr, scr->current_workspace+1);
4096 else if (wPreferences.ws_cycle)
4097 wWorkspaceChange(scr, 0);
4099 else if (direction == CLIP_REWIND) {
4100 if (scr->current_workspace > 0)
4101 wWorkspaceChange(scr, scr->current_workspace-1);
4102 else if (scr->current_workspace==0 && wPreferences.ws_cycle)
4103 wWorkspaceChange(scr, scr->workspace_count-1);
4106 wClipIconPaint(scr->clip_icon);
4110 static void
4111 iconMouseDown(WObjDescriptor *desc, XEvent *event)
4113 WAppIcon *aicon = desc->parent;
4114 WDock *dock = aicon->dock;
4115 WScreen *scr = aicon->icon->core->screen_ptr;
4117 if (aicon->editing || WCHECK_STATE(WSTATE_MODAL))
4118 return;
4120 scr->last_dock = dock;
4122 if (dock->menu->flags.mapped)
4123 wMenuUnmap(dock->menu);
4125 if (IsDoubleClick(scr, event)) {
4126 /* double-click was not in the main clip icon */
4127 if (dock->type != WM_CLIP || aicon->xindex!=0 || aicon->yindex!=0
4128 || getClipButton(event->xbutton.x, event->xbutton.y)==CLIP_IDLE) {
4129 iconDblClick(desc, event);
4130 return;
4134 if (dock->type == WM_CLIP && scr->flags.clip_balloon_mapped) {
4135 XUnmapWindow(dpy, scr->clip_balloon);
4136 scr->flags.clip_balloon_mapped = 0;
4139 #ifdef DEBUG
4140 puts("handling dock");
4141 #endif
4142 if (event->xbutton.button == Button1) {
4143 if (event->xbutton.state & MOD_MASK)
4144 wDockLower(dock);
4145 else
4146 wDockRaise(dock);
4148 if ((event->xbutton.state & ShiftMask) && aicon!=scr->clip_icon &&
4149 dock->type!=WM_DOCK) {
4150 wIconSelect(aicon->icon);
4151 return;
4154 if (aicon->yindex==0 && aicon->xindex==0) {
4155 if (getClipButton(event->xbutton.x, event->xbutton.y)!=CLIP_IDLE
4156 && dock->type==WM_CLIP)
4157 handleClipChangeWorkspace(scr, event);
4158 else
4159 handleDockMove(dock, aicon, event);
4160 } else
4161 handleIconMove(dock, aicon, event);
4163 } else if (event->xbutton.button==Button2 && dock->type==WM_CLIP &&
4164 aicon==scr->clip_icon) {
4165 openClipWorkspaceMenu(scr, event->xbutton.x_root+2,
4166 event->xbutton.y_root+2);
4167 if (scr->clip_ws_menu) {
4168 WMenu *menu;
4169 menu = scr->clip_ws_menu;
4170 desc = &menu->menu->descriptor;
4172 event->xany.send_event = True;
4173 (*desc->handle_mousedown)(desc, event);
4175 } else if (event->xbutton.button==Button2 && dock->type==WM_CLIP &&
4176 (event->xbutton.state & ShiftMask) && aicon!=scr->clip_icon) {
4177 wClipMakeIconOmnipresent(aicon, !aicon->omnipresent);
4178 } else if (event->xbutton.button == Button3) {
4179 openDockMenu(dock, aicon, event);
4184 static void
4185 showClipBalloon(WDock *dock, int workspace)
4187 int w, h;
4188 int x, y;
4189 WScreen *scr = dock->screen_ptr;
4190 char *text;
4191 Window stack[2];
4193 scr->flags.clip_balloon_mapped = 1;
4194 XMapWindow(dpy, scr->clip_balloon);
4196 text = scr->workspaces[workspace]->name;
4198 w = WMWidthOfString(scr->clip_title_font, text, strlen(text));
4200 h = WMFontHeight(scr->clip_title_font);
4201 XResizeWindow(dpy, scr->clip_balloon, w, h);
4203 x = dock->x_pos + CLIP_BUTTON_SIZE*ICON_SIZE/64;
4204 y = dock->y_pos + ICON_SIZE - WMFontHeight(scr->clip_title_font) - 3;
4206 if (x+w > scr->scr_width) {
4207 x = scr->scr_width - w;
4208 if (dock->y_pos + ICON_SIZE + h > scr->scr_height)
4209 y = dock->y_pos - h - 1;
4210 else
4211 y = dock->y_pos + ICON_SIZE;
4212 XRaiseWindow(dpy, scr->clip_balloon);
4213 } else {
4214 stack[0] = scr->clip_icon->icon->core->window;
4215 stack[1] = scr->clip_balloon;
4216 XRestackWindows(dpy, stack, 2);
4218 XMoveWindow(dpy, scr->clip_balloon, x, y);
4219 XSetForeground(dpy, scr->clip_title_gc,
4220 scr->clip_title_pixel[CLIP_NORMAL]);
4221 XClearWindow(dpy, scr->clip_balloon);
4222 WMDrawString(scr->wmscreen, scr->clip_balloon, scr->clip_title_gc,
4223 scr->clip_title_font, 0, 0, text, strlen(text));
4227 static void
4228 clipEnterNotify(WObjDescriptor *desc, XEvent *event)
4230 WAppIcon *btn = (WAppIcon*)desc->parent;
4231 WDock *dock;
4232 WScreen *scr;
4234 assert(event->type==EnterNotify);
4236 if(desc->parent_type!=WCLASS_DOCK_ICON)
4237 return;
4239 scr = btn->icon->core->screen_ptr;
4240 if (!btn->omnipresent)
4241 dock = btn->dock;
4242 else
4243 dock = scr->workspaces[scr->current_workspace]->clip;
4245 if (!dock || dock->type!=WM_CLIP)
4246 return;
4248 /* The auto raise/lower code */
4249 if (dock->auto_lower_magic) {
4250 WMDeleteTimerHandler(dock->auto_lower_magic);
4251 dock->auto_lower_magic = NULL;
4253 if (dock->auto_raise_lower && !dock->auto_raise_magic) {
4254 dock->auto_raise_magic = WMAddTimerHandler(AUTO_RAISE_DELAY,
4255 clipAutoRaise,
4256 (void *)dock);
4259 /* The auto expand/collapse code */
4260 if (dock->auto_collapse_magic) {
4261 WMDeleteTimerHandler(dock->auto_collapse_magic);
4262 dock->auto_collapse_magic = NULL;
4264 if (dock->auto_collapse && !dock->auto_expand_magic) {
4265 dock->auto_expand_magic = WMAddTimerHandler(AUTO_EXPAND_DELAY,
4266 clipAutoExpand,
4267 (void *)dock);
4270 if (btn->xindex == 0 && btn->yindex == 0)
4271 showClipBalloon(dock, dock->screen_ptr->current_workspace);
4272 else {
4273 if (dock->screen_ptr->flags.clip_balloon_mapped) {
4274 XUnmapWindow(dpy, dock->screen_ptr->clip_balloon);
4275 dock->screen_ptr->flags.clip_balloon_mapped = 0;
4281 static void
4282 clipLeave(WDock *dock)
4284 XEvent event;
4285 WObjDescriptor *desc = NULL;
4287 if (!dock || dock->type!=WM_CLIP)
4288 return;
4290 if (XCheckTypedEvent(dpy, EnterNotify, &event)!=False) {
4291 if (XFindContext(dpy, event.xcrossing.window, wWinContext,
4292 (XPointer *)&desc)!=XCNOENT
4293 && desc && desc->parent_type==WCLASS_DOCK_ICON
4294 && ((WAppIcon*)desc->parent)->dock
4295 && ((WAppIcon*)desc->parent)->dock->type==WM_CLIP) {
4296 /* We didn't left the Clip yet */
4297 XPutBackEvent(dpy, &event);
4298 return;
4301 XPutBackEvent(dpy, &event);
4302 } else {
4303 /* We entered a withdrawn window, so we're still in Clip */
4304 return;
4307 if (dock->auto_raise_magic) {
4308 WMDeleteTimerHandler(dock->auto_raise_magic);
4309 dock->auto_raise_magic = NULL;
4311 if (dock->auto_raise_lower && !dock->auto_lower_magic) {
4312 dock->auto_lower_magic = WMAddTimerHandler(AUTO_LOWER_DELAY,
4313 clipAutoLower,
4314 (void *)dock);
4317 if (dock->auto_expand_magic) {
4318 WMDeleteTimerHandler(dock->auto_expand_magic);
4319 dock->auto_expand_magic = NULL;
4321 if (dock->auto_collapse && !dock->auto_collapse_magic) {
4322 dock->auto_collapse_magic = WMAddTimerHandler(AUTO_COLLAPSE_DELAY,
4323 clipAutoCollapse,
4324 (void *)dock);
4329 static void
4330 clipLeaveNotify(WObjDescriptor *desc, XEvent *event)
4332 WAppIcon *btn = (WAppIcon*)desc->parent;
4334 assert(event->type==LeaveNotify);
4336 if(desc->parent_type!=WCLASS_DOCK_ICON)
4337 return;
4339 clipLeave(btn->dock);
4343 static void
4344 clipAutoCollapse(void *cdata)
4346 WDock *dock = (WDock *)cdata;
4348 if (dock->type!=WM_CLIP)
4349 return;
4351 if (dock->auto_collapse) {
4352 dock->collapsed = 1;
4353 wDockHideIcons(dock);
4355 dock->auto_collapse_magic = NULL;
4359 static void
4360 clipAutoExpand(void *cdata)
4362 WDock *dock = (WDock *)cdata;
4364 if (dock->type!=WM_CLIP)
4365 return;
4367 if (dock->auto_collapse) {
4368 dock->collapsed = 0;
4369 wDockShowIcons(dock);
4371 dock->auto_expand_magic = NULL;
4375 static void
4376 clipAutoLower(void *cdata)
4378 WDock *dock = (WDock *)cdata;
4380 if (dock->type!=WM_CLIP)
4381 return;
4383 if (dock->auto_raise_lower)
4384 wDockLower(dock);
4386 dock->auto_lower_magic = NULL;
4390 static void
4391 clipAutoRaise(void *cdata)
4393 WDock *dock = (WDock *)cdata;
4395 if (dock->type!=WM_CLIP)
4396 return;
4398 if (dock->auto_raise_lower)
4399 wDockRaise(dock);
4401 if (dock->screen_ptr->flags.clip_balloon_mapped) {
4402 showClipBalloon(dock, dock->screen_ptr->current_workspace);
4405 dock->auto_raise_magic = NULL;
4409 static Bool
4410 iconCanBeOmnipresent(WAppIcon *aicon)
4412 WScreen *scr = aicon->icon->core->screen_ptr;
4413 WDock *clip;
4414 WAppIcon *btn;
4415 int i, j;
4417 for (i=0; i<scr->workspace_count; i++) {
4418 clip = scr->workspaces[i]->clip;
4420 if (clip == aicon->dock)
4421 continue;
4423 if (clip->icon_count + scr->global_icon_count >= clip->max_icons)
4424 return False; /* Clip is full in some workspace */
4426 for (j=0; j<clip->max_icons; j++) {
4427 btn = clip->icon_array[j];
4428 if(btn && btn->xindex==aicon->xindex && btn->yindex==aicon->yindex)
4429 return False;
4433 return True;
4438 wClipMakeIconOmnipresent(WAppIcon *aicon, int omnipresent)
4440 WScreen *scr = aicon->icon->core->screen_ptr;
4441 WAppIconChain *new_entry, *tmp, *tmp1;
4442 int status = WO_SUCCESS;
4444 if (aicon->dock == scr->dock || aicon == scr->clip_icon)
4445 return WO_NOT_APPLICABLE;
4447 if (aicon->omnipresent == omnipresent)
4448 return WO_SUCCESS;
4450 if (omnipresent) {
4451 if (iconCanBeOmnipresent(aicon)) {
4452 aicon->omnipresent = 1;
4453 new_entry = wmalloc(sizeof(WAppIconChain));
4454 new_entry->aicon = aicon;
4455 new_entry->next = NULL;
4456 if (!scr->global_icons) {
4457 scr->global_icons = new_entry;
4458 } else {
4459 tmp = scr->global_icons;
4460 while (tmp->next)
4461 tmp = tmp->next;
4463 tmp->next = new_entry;
4465 scr->global_icon_count++;
4466 } else {
4467 aicon->omnipresent = 0;
4468 status = WO_FAILED;
4470 } else {
4471 aicon->omnipresent = 0;
4472 if (aicon == scr->global_icons->aicon) {
4473 tmp = scr->global_icons->next;
4474 free(scr->global_icons);
4475 scr->global_icons = tmp;
4476 scr->global_icon_count--;
4477 } else {
4478 tmp = scr->global_icons;
4479 while (tmp->next) {
4480 if (tmp->next->aicon == aicon) {
4481 tmp1 = tmp->next->next;
4482 free(tmp->next);
4483 tmp->next = tmp1;
4484 scr->global_icon_count--;
4485 break;
4487 tmp = tmp->next;
4492 wAppIconPaint(aicon);
4494 return status;