Fixed a function name.
[wmaker-crm.git] / src / dock.c
blobdd31ccaa3f820c220a9c66d20dfcc9a35269b3c8
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"
56 #include "wmsound.h"
59 #include <proplist.h>
64 /**** Local variables ****/
65 #define CLIP_REWIND 1
66 #define CLIP_IDLE 0
67 #define CLIP_FORWARD 2
70 /**** Global variables ****/
72 /* in dockedapp.c */
73 extern void DestroyDockAppSettingsPanel();
75 extern void ShowDockAppSettingsPanel(WAppIcon *aicon);
78 extern XContext wWinContext;
80 extern Cursor wCursor[WCUR_LAST];
82 extern WPreferences wPreferences;
84 extern XContext wWinContext;
86 #ifdef OFFIX_DND
87 extern Atom _XA_DND_PROTOCOL;
88 #endif
91 #define MOD_MASK wPreferences.modifier_mask
93 extern void appIconMouseDown(WObjDescriptor *desc, XEvent *event);
95 #define ICON_SIZE wPreferences.icon_size
98 /***** Local variables ****/
100 static proplist_t dCommand=NULL;
101 #ifdef OFFIX_DND
102 static proplist_t dDropCommand=NULL;
103 #endif
104 static proplist_t dAutoLaunch, dLock;
105 static proplist_t dName, dForced, dBuggyApplication, dYes, dNo;
106 static proplist_t dHost, dDock, dClip;
107 static proplist_t dAutoAttractIcons;
109 static proplist_t dPosition, dApplications, dLowered, dCollapsed, dAutoCollapse;
111 static proplist_t dAutoRaiseLower, dOmnipresent;
113 static void dockIconPaint(WAppIcon *btn);
115 static void iconMouseDown(WObjDescriptor *desc, XEvent *event);
117 static pid_t execCommand(WAppIcon *btn, char *command, WSavedState *state);
119 static void trackDeadProcess(pid_t pid, unsigned char status, WDock *dock);
121 static int getClipButton(int px, int py);
123 static void toggleLowered(WDock *dock);
125 static void toggleCollapsed(WDock *dock);
127 static void clipIconExpose(WObjDescriptor *desc, XEvent *event);
129 static void clipLeave(WDock *dock);
131 static void handleClipChangeWorkspace(WScreen *scr, XEvent *event);
133 Bool moveIconBetweenDocks(WDock *src, WDock *dest, WAppIcon *icon, int x, int y);
135 static void clipEnterNotify(WObjDescriptor *desc, XEvent *event);
136 static void clipLeaveNotify(WObjDescriptor *desc, XEvent *event);
137 static void clipAutoCollapse(void *cdata);
138 static void clipAutoExpand(void *cdata);
139 static void launchDockedApplication(WAppIcon *btn);
141 static void clipAutoLower(void *cdata);
142 static void clipAutoRaise(void *cdata);
144 static void showClipBalloon(WDock *dock, int workspace);
146 #ifdef OFFIX_DND
148 #define DndNotDnd -1
149 #define DndUnknown 0
150 #define DndRawData 1
151 #define DndFile 2
152 #define DndFiles 3
153 #define DndText 4
154 #define DndDir 5
155 #define DndLink 6
156 #define DndExe 7
158 #define DndEND 8
160 #endif /* OFFIX_DND */
164 static void
165 make_keys()
167 if (dCommand!=NULL)
168 return;
170 dCommand = PLRetain(PLMakeString("Command"));
171 #ifdef OFFIX_DND
172 dDropCommand = PLRetain(PLMakeString("DropCommand"));
173 #endif
174 dLock = PLRetain(PLMakeString("Lock"));
175 dAutoLaunch = PLRetain(PLMakeString("AutoLaunch"));
176 dName = PLRetain(PLMakeString("Name"));
177 dForced = PLRetain(PLMakeString("Forced"));
178 dBuggyApplication = PLRetain(PLMakeString("BuggyApplication"));
179 dYes = PLRetain(PLMakeString("Yes"));
180 dNo = PLRetain(PLMakeString("No"));
181 dHost = PLRetain(PLMakeString("Host"));
183 dPosition = PLMakeString("Position");
184 dApplications = PLMakeString("Applications");
185 dLowered = PLMakeString("Lowered");
186 dCollapsed = PLMakeString("Collapsed");
187 dAutoCollapse = PLMakeString("AutoCollapse");
188 dAutoRaiseLower = PLMakeString("AutoRaiseLower");
189 dAutoAttractIcons = PLMakeString("AutoAttractIcons");
191 dOmnipresent = PLMakeString("Omnipresent");
193 dDock = PLMakeString("Dock");
194 dClip = PLMakeString("Clip");
199 static void
200 renameCallback(WMenu *menu, WMenuEntry *entry)
202 WDock *dock = entry->clientdata;
203 char buffer[128];
204 int wspace;
205 char *name;
207 assert(entry->clientdata!=NULL);
209 wspace = dock->screen_ptr->current_workspace;
211 name = wstrdup(dock->screen_ptr->workspaces[wspace]->name);
213 sprintf(buffer, _("Type the name for workspace %i:"), wspace+1);
214 if (wInputDialog(dock->screen_ptr, _("Rename Workspace"), buffer,
215 &name)) {
216 wWorkspaceRename(dock->screen_ptr, wspace, name);
218 if (name) {
219 free(name);
224 static void
225 toggleLoweredCallback(WMenu *menu, WMenuEntry *entry)
227 assert(entry->clientdata!=NULL);
229 toggleLowered(entry->clientdata);
231 entry->flags.indicator_on = !((WDock*)entry->clientdata)->lowered;
233 wMenuPaint(menu);
238 static void
239 killCallback(WMenu *menu, WMenuEntry *entry)
241 WAppIcon *icon;
242 #ifdef REDUCE_APPICONS
243 WAppIconAppList *tapplist;
245 extern Atom _XA_WM_DELETE_WINDOW;
246 #else
247 char *buffer;
248 #endif
250 if (!WCHECK_STATE(WSTATE_NORMAL))
251 return;
253 assert(entry->clientdata!=NULL);
255 icon = (WAppIcon*)entry->clientdata;
257 icon->editing = 1;
259 WCHANGE_STATE(WSTATE_MODAL);
261 #ifdef REDUCE_APPICONS
262 /* Send a delete message to the main window of each application
263 * bound to this docked appicon. - cls
265 tapplist = icon->applist;
266 while (tapplist != NULL) {
267 if (tapplist->wapp->main_window_desc != NULL) {
268 if (tapplist->wapp->main_window_desc->protocols.DELETE_WINDOW) {
269 wClientSendProtocol(tapplist->wapp->main_window_desc,
270 _XA_WM_DELETE_WINDOW, CurrentTime);
271 } else {
272 wClientKill(tapplist->wapp->main_window_desc);
275 tapplist = tapplist->next;
277 #else
278 buffer = wstrappend(icon->wm_class,
279 _(" will be forcibly closed.\n"
280 "Any unsaved changes will be lost.\n"
281 "Please confirm."));
283 if (wPreferences.dont_confirm_kill
284 || wMessageDialog(menu->frame->screen_ptr, _("Kill Application"),
285 buffer, _("Yes"), _("No"), NULL)==WAPRDefault) {
286 if (icon->icon && icon->icon->owner) {
287 wClientKill(icon->icon->owner);
290 #endif /* !REDUCE_APPICONS */
292 icon->editing = 0;
294 WCHANGE_STATE(WSTATE_NORMAL);
298 /* TODO: replace this function with a member of the dock struct */
299 static int
300 numberOfSelectedIcons(WDock *dock)
302 WAppIcon *aicon;
303 int i, n;
305 n = 0;
306 for (i=1; i<dock->max_icons; i++) {
307 aicon = dock->icon_array[i];
308 if (aicon && aicon->icon->selected) {
309 n++;
313 return n;
317 static WMBag*
318 getSelected(WDock *dock)
320 WMBag *ret = WMCreateBag(8);
321 WAppIcon *btn;
322 int i;
324 for (i=1; i<dock->max_icons; i++) {
325 btn = dock->icon_array[i];
326 if (btn && btn->icon->selected) {
327 WMPutInBag(ret, btn);
331 return ret;
335 static void
336 paintClipButtons(WAppIcon *clipIcon, Bool lpushed, Bool rpushed)
338 Window win = clipIcon->icon->core->window;
339 WScreen *scr = clipIcon->icon->core->screen_ptr;
340 XPoint p[4];
341 int pt = CLIP_BUTTON_SIZE*ICON_SIZE/64;
342 int tp = ICON_SIZE - pt;
343 int as = pt - 15; /* 15 = 5+5+5 */
344 GC gc = scr->clip_title_gc;
345 #ifdef GRADIENT_CLIP_ARROW
346 Bool collapsed = clipIcon->dock->collapsed;
347 #endif
349 if (rpushed) {
350 p[0].x = tp+1;
351 p[0].y = 1;
352 p[1].x = ICON_SIZE-2;
353 p[1].y = 1;
354 p[2].x = ICON_SIZE-2;
355 p[2].y = pt-1;
356 } else if (lpushed) {
357 p[0].x = 1;
358 p[0].y = tp;
359 p[1].x = pt;
360 p[1].y = ICON_SIZE-2;
361 p[2].x = 1;
362 p[2].y = ICON_SIZE-2;
364 if (lpushed || rpushed) {
365 XSetForeground(dpy, scr->draw_gc, scr->white_pixel);
366 XFillPolygon(dpy, win, scr->draw_gc, p, 3, Convex, CoordModeOrigin);
367 XSetForeground(dpy, scr->draw_gc, scr->black_pixel);
369 #ifdef GRADIENT_CLIP_ARROW
370 if (!collapsed) {
371 XSetFillStyle(dpy, scr->copy_gc, FillTiled);
372 XSetTile(dpy, scr->copy_gc, scr->clip_arrow_gradient);
373 XSetClipMask(dpy, scr->copy_gc, None);
374 gc = scr->copy_gc;
376 #endif /* GRADIENT_CLIP_ARROW */
378 /* top right arrow */
379 p[0].x = p[3].x = ICON_SIZE-5-as;
380 p[0].y = p[3].y = 5;
381 p[1].x = ICON_SIZE-6;
382 p[1].y = 5;
383 p[2].x = ICON_SIZE-6;
384 p[2].y = 4+as;
385 if (rpushed) {
386 XFillPolygon(dpy, win, scr->draw_gc, p, 3, Convex, CoordModeOrigin);
387 XDrawLines(dpy, win, scr->draw_gc, p, 4, CoordModeOrigin);
388 } else {
389 #ifdef GRADIENT_CLIP_ARROW
390 if (!collapsed)
391 XSetTSOrigin(dpy, gc, ICON_SIZE-6-as, 5);
392 #endif
393 XFillPolygon(dpy, win, gc, p,3,Convex,CoordModeOrigin);
394 XDrawLines(dpy, win, gc, p,4,CoordModeOrigin);
397 /* bottom left arrow */
398 p[0].x = p[3].x = 5;
399 p[0].y = p[3].y = ICON_SIZE-5-as;
400 p[1].x = 5;
401 p[1].y = ICON_SIZE-6;
402 p[2].x = 4+as;
403 p[2].y = ICON_SIZE-6;
404 if (lpushed) {
405 XFillPolygon(dpy, win, scr->draw_gc, p, 3, Convex, CoordModeOrigin);
406 XDrawLines(dpy, win, scr->draw_gc, p, 4, CoordModeOrigin);
407 } else {
408 #ifdef GRADIENT_CLIP_ARROW
409 if (!collapsed)
410 XSetTSOrigin(dpy, gc, 5, ICON_SIZE-6-as);
411 #endif
412 XFillPolygon(dpy, win, gc, p,3,Convex,CoordModeOrigin);
413 XDrawLines(dpy, win, gc, p,4,CoordModeOrigin);
415 #ifdef GRADIENT_CLIP_ARROW
416 if (!collapsed)
417 XSetFillStyle(dpy, scr->copy_gc, FillSolid);
418 #endif
422 RImage*
423 wClipMakeTile(WScreen *scr, RImage *normalTile)
425 RImage *tile = RCloneImage(normalTile);
426 RColor black;
427 RColor dark;
428 RColor light;
429 int pt, tp;
430 int as;
432 pt = CLIP_BUTTON_SIZE*wPreferences.icon_size/64;
433 tp = wPreferences.icon_size-1 - pt;
434 as = pt - 15;
436 black.alpha = 255;
437 black.red = black.green = black.blue = 0;
439 dark.alpha = 0;
440 dark.red = dark.green = dark.blue = 60;
442 light.alpha = 0;
443 light.red = light.green = light.blue = 80;
446 /* top right */
447 ROperateLine(tile, RSubtractOperation, tp, 0, wPreferences.icon_size-2,
448 pt-1, &dark);
449 RDrawLine(tile, tp-1, 0, wPreferences.icon_size-1, pt+1, &black);
450 ROperateLine(tile, RAddOperation, tp, 2, wPreferences.icon_size-3,
451 pt, &light);
453 /* arrow bevel */
454 ROperateLine(tile, RSubtractOperation, ICON_SIZE - 7 - as, 4,
455 ICON_SIZE - 5, 4, &dark);
456 ROperateLine(tile, RSubtractOperation, ICON_SIZE - 6 - as, 5,
457 ICON_SIZE - 5, 6 + as, &dark);
458 ROperateLine(tile, RAddOperation, ICON_SIZE - 5, 4, ICON_SIZE - 5, 6 + as,
459 &light);
461 /* bottom left */
462 ROperateLine(tile, RAddOperation, 2, tp+2, pt-2,
463 wPreferences.icon_size-3, &dark);
464 RDrawLine(tile, 0, tp-1, pt+1, wPreferences.icon_size-1, &black);
465 ROperateLine(tile, RSubtractOperation, 0, tp-2, pt+1,
466 wPreferences.icon_size-2, &light);
468 /* arrow bevel */
469 ROperateLine(tile, RSubtractOperation, 4, ICON_SIZE - 7 - as, 4,
470 ICON_SIZE - 5, &dark);
471 ROperateLine(tile, RSubtractOperation, 5, ICON_SIZE - 6 - as,
472 6 + as, ICON_SIZE - 5, &dark);
473 ROperateLine(tile, RAddOperation, 4, ICON_SIZE - 5, 6 + as, ICON_SIZE - 5,
474 &light);
476 return tile;
480 static void
481 omnipresentCallback(WMenu *menu, WMenuEntry *entry)
483 WAppIcon *clickedIcon = entry->clientdata;
484 WAppIcon *aicon;
485 WDock *dock;
486 WMBag *selectedIcons;
487 int failed;
488 int i;
490 assert(entry->clientdata!=NULL);
492 dock = clickedIcon->dock;
494 selectedIcons = getSelected(dock);
496 if (!WMGetBagItemCount(selectedIcons))
497 WMPutInBag(selectedIcons, clickedIcon);
499 failed = 0;
500 for (i = 0; i < WMGetBagItemCount(selectedIcons); i++) {
501 aicon = WMGetFromBag(selectedIcons, i);
503 if (wClipMakeIconOmnipresent(aicon, !aicon->omnipresent) == WO_FAILED)
504 failed++;
505 else if (aicon->icon->selected)
506 wIconSelect(aicon->icon);
508 WMFreeBag(selectedIcons);
510 if (failed > 1) {
511 wMessageDialog(dock->screen_ptr, _("Warning"),
512 _("Some icons cannot be made omnipresent. "
513 "Please make sure that no other icon is "
514 "docked in the same positions on the other "
515 "workspaces and the Clip is not full in "
516 "some workspace."),
517 _("OK"), NULL, NULL);
518 } else if (failed == 1) {
519 wMessageDialog(dock->screen_ptr, _("Warning"),
520 _("Icon cannot be made omnipresent. "
521 "Please make sure that no other icon is "
522 "docked in the same position on the other "
523 "workspaces and the Clip is not full in "
524 "some workspace."),
525 _("OK"), NULL, NULL);
530 static void
531 removeIconsCallback(WMenu *menu, WMenuEntry *entry)
533 WAppIcon *clickedIcon = (WAppIcon*)entry->clientdata;
534 WDock *dock;
535 WAppIcon *aicon;
536 WMBag *selectedIcons;
537 int keepit;
538 int i;
540 assert(clickedIcon!=NULL);
542 dock = clickedIcon->dock;
544 selectedIcons = getSelected(dock);
546 if (WMGetBagItemCount(selectedIcons)) {
547 if (wMessageDialog(dock->screen_ptr, _("Workspace Clip"),
548 _("All selected icons will be removed!"),
549 _("OK"), _("Cancel"), NULL)!=WAPRDefault) {
550 WMFreeBag(selectedIcons);
551 return;
553 } else {
554 if (clickedIcon->xindex==0 && clickedIcon->yindex==0) {
555 WMFreeBag(selectedIcons);
556 return;
558 WMPutInBag(selectedIcons, clickedIcon);
561 for (i = 0; i < WMGetBagItemCount(selectedIcons); i++) {
562 aicon = WMGetFromBag(selectedIcons, i);
563 keepit = aicon->running && wApplicationOf(aicon->main_window);
564 wDockDetach(dock, aicon);
565 if (keepit) {
566 PlaceIcon(dock->screen_ptr, &aicon->x_pos, &aicon->y_pos);
567 XMoveWindow(dpy, aicon->icon->core->window,
568 aicon->x_pos, aicon->y_pos);
569 if (!dock->mapped || dock->collapsed)
570 XMapWindow(dpy, aicon->icon->core->window);
573 WMFreeBag(selectedIcons);
575 if (wPreferences.auto_arrange_icons)
576 wArrangeIcons(dock->screen_ptr, True);
580 static void
581 keepIconsCallback(WMenu *menu, WMenuEntry *entry)
583 WAppIcon *clickedIcon = (WAppIcon*)entry->clientdata;
584 WDock *dock;
585 WAppIcon *aicon;
586 WMBag *selectedIcons;
587 int i;
589 assert(clickedIcon!=NULL);
590 dock = clickedIcon->dock;
592 selectedIcons = getSelected(dock);
594 if (!WMGetBagItemCount(selectedIcons)
595 && clickedIcon!=dock->screen_ptr->clip_icon) {
596 char *command = NULL;
598 if (!clickedIcon->command && !clickedIcon->editing) {
599 clickedIcon->editing = 1;
600 if (wInputDialog(dock->screen_ptr, _("Keep Icon"),
601 _("Type the command used to launch the application"),
602 &command)) {
603 if (command && (command[0]==0 ||
604 (command[0]=='-' && command[1]==0))) {
605 free(command);
606 command = NULL;
608 clickedIcon->command = command;
609 clickedIcon->editing = 0;
610 } else {
611 clickedIcon->editing = 0;
612 if (command)
613 free(command);
614 WMFreeBag(selectedIcons);
615 return;
619 WMPutInBag(selectedIcons, clickedIcon);
622 for (i = 0; i < WMGetBagItemCount(selectedIcons); i++) {
623 aicon = WMGetFromBag(selectedIcons, i);
624 if (aicon->icon->selected)
625 wIconSelect(aicon->icon);
626 if (aicon && aicon->attracted && aicon->command) {
627 aicon->attracted = 0;
628 if (aicon->icon->shadowed) {
629 aicon->icon->shadowed = 0;
630 aicon->icon->force_paint = 1;
631 wAppIconPaint(aicon);
635 WMFreeBag(selectedIcons);
641 static void
642 toggleAutoAttractCallback(WMenu *menu, WMenuEntry *entry)
644 WDock *dock = (WDock*)entry->clientdata;
646 assert(entry->clientdata!=NULL);
648 dock->attract_icons = !dock->attract_icons;
649 /*if (!dock->attract_icons)
650 dock->keep_attracted = 0;*/
652 entry->flags.indicator_on = dock->attract_icons;
654 wMenuPaint(menu);
658 static void
659 selectCallback(WMenu *menu, WMenuEntry *entry)
661 WAppIcon *icon = (WAppIcon*)entry->clientdata;
663 assert(icon!=NULL);
665 wIconSelect(icon->icon);
667 wMenuPaint(menu);
671 static void
672 colectIconsCallback(WMenu *menu, WMenuEntry *entry)
674 WAppIcon *clickedIcon = (WAppIcon*)entry->clientdata;
675 WDock *clip;
676 WAppIcon *aicon;
677 int x, y, x_pos, y_pos;
679 assert(entry->clientdata!=NULL);
680 clip = clickedIcon->dock;
682 aicon = clip->screen_ptr->app_icon_list;
684 while (aicon) {
685 if (!aicon->docked && wDockFindFreeSlot(clip, &x, &y)) {
686 x_pos = clip->x_pos + x*ICON_SIZE;
687 y_pos = clip->y_pos + y*ICON_SIZE;
688 if (aicon->x_pos != x_pos || aicon->y_pos != y_pos) {
689 #ifdef ANIMATIONS
690 if (wPreferences.no_animations) {
691 XMoveWindow(dpy, aicon->icon->core->window, x_pos, y_pos);
692 } else {
693 SlideWindow(aicon->icon->core->window,
694 aicon->x_pos, aicon->y_pos, x_pos, y_pos);
696 #else
697 XMoveWindow(dpy, aicon->icon->core->window, x_pos, y_pos);
698 #endif /* ANIMATIONS */
700 aicon->attracted = 1;
701 if (!aicon->icon->shadowed) {
702 aicon->icon->shadowed = 1;
703 aicon->icon->force_paint = 1;
704 /* We don't do an wAppIconPaint() here because it's in
705 * wDockAttachIcon(). -Dan
708 wDockAttachIcon(clip, aicon, x, y);
709 if (clip->collapsed || !clip->mapped)
710 XUnmapWindow(dpy, aicon->icon->core->window);
712 aicon = aicon->next;
717 static void
718 selectIconsCallback(WMenu *menu, WMenuEntry *entry)
720 WAppIcon *clickedIcon = (WAppIcon*)entry->clientdata;
721 WDock *dock;
722 WMBag *selectedIcons;
723 WAppIcon *btn;
724 int i;
726 assert(clickedIcon!=NULL);
727 dock = clickedIcon->dock;
729 selectedIcons = getSelected(dock);
731 if (!WMGetBagItemCount(selectedIcons)) {
732 for (i=1; i<dock->max_icons; i++) {
733 btn = dock->icon_array[i];
734 if (btn && !btn->icon->selected) {
735 wIconSelect(btn->icon);
738 } else {
739 for (i = 0; i < WMGetBagItemCount(selectedIcons); i++) {
740 btn = WMGetFromBag(selectedIcons, i);
741 wIconSelect(btn->icon);
744 WMFreeBag(selectedIcons);
746 wMenuPaint(menu);
750 static void
751 toggleCollapsedCallback(WMenu *menu, WMenuEntry *entry)
753 assert(entry->clientdata!=NULL);
755 toggleCollapsed(entry->clientdata);
757 entry->flags.indicator_on = ((WDock*)entry->clientdata)->collapsed;
759 wMenuPaint(menu);
763 static void
764 toggleAutoCollapseCallback(WMenu *menu, WMenuEntry *entry)
766 WDock *dock;
767 assert(entry->clientdata!=NULL);
769 dock = (WDock*) entry->clientdata;
771 dock->auto_collapse = !dock->auto_collapse;
773 entry->flags.indicator_on = ((WDock*)entry->clientdata)->auto_collapse;
775 wMenuPaint(menu);
779 static void
780 toggleAutoRaiseLowerCallback(WMenu *menu, WMenuEntry *entry)
782 WDock *dock;
783 assert(entry->clientdata!=NULL);
785 dock = (WDock*) entry->clientdata;
787 dock->auto_raise_lower = !dock->auto_raise_lower;
789 entry->flags.indicator_on = ((WDock*)entry->clientdata)->auto_raise_lower;
791 wMenuPaint(menu);
795 static void
796 launchCallback(WMenu *menu, WMenuEntry *entry)
798 WAppIcon *btn = (WAppIcon*)entry->clientdata;
800 launchDockedApplication(btn);
804 static void
805 settingsCallback(WMenu *menu, WMenuEntry *entry)
807 WAppIcon *btn = (WAppIcon*)entry->clientdata;
809 if (btn->editing)
810 return;
811 ShowDockAppSettingsPanel(btn);
815 static void
816 hideCallback(WMenu *menu, WMenuEntry *entry)
818 WApplication *wapp;
819 WAppIcon *btn = (WAppIcon*)entry->clientdata;
821 wapp = wApplicationOf(btn->icon->owner->main_window);
823 if (wapp->flags.hidden) {
824 wWorkspaceChange(btn->icon->core->screen_ptr,wapp->last_workspace);
825 wUnhideApplication(wapp, False, False);
826 } else {
827 wHideApplication(wapp);
832 static void
833 unhideHereCallback(WMenu *menu, WMenuEntry *entry)
835 WApplication *wapp;
836 WAppIcon *btn = (WAppIcon*)entry->clientdata;
838 wapp = wApplicationOf(btn->icon->owner->main_window);
840 wUnhideApplication(wapp, False, True);
844 WAppIcon*
845 mainIconCreate(WScreen *scr, int type)
847 WAppIcon *btn;
848 int x_pos;
850 if (type == WM_CLIP) {
851 if (scr->clip_icon)
852 return scr->clip_icon;
853 btn = wAppIconCreateForDock(scr, NULL, "Logo", "WMClip", TILE_CLIP);
854 btn->icon->core->descriptor.handle_expose = clipIconExpose;
855 btn->icon->core->descriptor.handle_enternotify = clipEnterNotify;
856 btn->icon->core->descriptor.handle_leavenotify = clipLeaveNotify;
857 /*x_pos = scr->scr_width - ICON_SIZE*2 - DOCK_EXTRA_SPACE;*/
858 x_pos = 0;
859 } else {
860 btn = wAppIconCreateForDock(scr, NULL, "Logo", "WMDock", TILE_NORMAL);
861 x_pos = scr->scr_width - ICON_SIZE - DOCK_EXTRA_SPACE;
864 btn->xindex = 0;
865 btn->yindex = 0;
867 btn->icon->core->descriptor.handle_mousedown = iconMouseDown;
868 btn->icon->core->descriptor.parent_type = WCLASS_DOCK_ICON;
869 btn->icon->core->descriptor.parent = btn;
870 /*ChangeStackingLevel(btn->icon->core, WMDockLevel);*/
871 XMapWindow(dpy, btn->icon->core->window);
872 btn->x_pos = x_pos;
873 btn->y_pos = 0;
874 btn->docked = 1;
875 if (type == WM_CLIP)
876 scr->clip_icon = btn;
878 return btn;
882 static void
883 switchWSCommand(WMenu *menu, WMenuEntry *entry)
885 WAppIcon *btn, *icon = (WAppIcon*) entry->clientdata;
886 WScreen *scr = icon->icon->core->screen_ptr;
887 WDock *src, *dest;
888 WMBag *selectedIcons;
889 int x, y;
891 if (entry->order == scr->current_workspace)
892 return;
893 src = icon->dock;
894 dest = scr->workspaces[entry->order]->clip;
896 selectedIcons = getSelected(src);
898 if (WMGetBagItemCount(selectedIcons)) {
899 int i;
900 for (i = 0; i < WMGetBagItemCount(selectedIcons); i++) {
901 btn = WMGetFromBag(selectedIcons, i);
902 if (wDockFindFreeSlot(dest, &x, &y)) {
903 moveIconBetweenDocks(src, dest, btn, x, y);
904 XUnmapWindow(dpy, btn->icon->core->window);
907 } else if (icon != scr->clip_icon) {
908 if (wDockFindFreeSlot(dest, &x, &y)) {
909 moveIconBetweenDocks(src, dest, icon, x, y);
910 XUnmapWindow(dpy, icon->icon->core->window);
913 WMFreeBag(selectedIcons);
918 static void
919 launchDockedApplication(WAppIcon *btn)
921 WScreen *scr = btn->icon->core->screen_ptr;
923 if (!btn->launching && btn->command!=NULL) {
924 if (!btn->forced_dock) {
925 btn->relaunching = btn->running;
926 btn->running = 1;
928 if (btn->wm_instance || btn->wm_class) {
929 WWindowAttributes attr;
930 memset(&attr, 0, sizeof(WWindowAttributes));
931 wDefaultFillAttributes(scr, btn->wm_instance, btn->wm_class,
932 &attr, NULL, True);
934 if (!attr.no_appicon && !btn->buggy_app)
935 btn->launching = 1;
936 else
937 btn->running = 0;
939 btn->drop_launch = 0;
940 scr->last_dock = btn->dock;
941 btn->pid = execCommand(btn, btn->command, NULL);
942 if (btn->pid>0) {
943 if (btn->buggy_app) {
944 /* give feedback that the app was launched */
945 btn->launching = 1;
946 dockIconPaint(btn);
947 btn->launching = 0;
948 WMAddTimerHandler(200, (WMCallback*)dockIconPaint, btn);
949 } else {
950 dockIconPaint(btn);
952 } else {
953 wwarning(_("could not launch application %s\n"), btn->command);
954 btn->launching = 0;
955 if (!btn->relaunching)
956 btn->running = 0;
963 static void
964 updateWorkspaceMenu(WMenu *menu, WAppIcon *icon)
966 WScreen *scr = menu->frame->screen_ptr;
967 char title[MAX_WORKSPACENAME_WIDTH+1];
968 int i;
970 if (!menu || !icon)
971 return;
973 for (i=0; i<scr->workspace_count; i++) {
974 if (i < menu->entry_no) {
975 if (strcmp(menu->entries[i]->text,scr->workspaces[i]->name)!=0) {
976 free(menu->entries[i]->text);
977 strcpy(title, scr->workspaces[i]->name);
978 menu->entries[i]->text = wstrdup(title);
979 menu->flags.realized = 0;
981 menu->entries[i]->clientdata = (void*)icon;
982 } else {
983 strcpy(title, scr->workspaces[i]->name);
985 wMenuAddCallback(menu, title, switchWSCommand, (void*)icon);
987 menu->flags.realized = 0;
989 if (i == scr->current_workspace) {
990 wMenuSetEnabled(menu, i, False);
991 } else {
992 wMenuSetEnabled(menu, i, True);
996 if (!menu->flags.realized)
997 wMenuRealize(menu);
1001 static WMenu*
1002 makeWorkspaceMenu(WScreen *scr)
1004 WMenu *menu;
1006 menu = wMenuCreate(scr, NULL, False);
1007 if (!menu)
1008 wwarning(_("could not create workspace submenu for Clip menu"));
1010 wMenuAddCallback(menu, "", switchWSCommand, (void*)scr->clip_icon);
1012 menu->flags.realized = 0;
1013 wMenuRealize(menu);
1015 return menu;
1019 static void
1020 updateClipOptionsMenu(WMenu *menu, WDock *dock)
1022 WMenuEntry *entry;
1023 int index = 0;
1025 if (!menu || !dock)
1026 return;
1028 /* keep on top */
1029 entry = menu->entries[index];
1030 entry->flags.indicator_on = !dock->lowered;
1031 entry->clientdata = dock;
1033 /* collapsed */
1034 entry = menu->entries[++index];
1035 entry->flags.indicator_on = dock->collapsed;
1036 entry->clientdata = dock;
1038 /* auto-collapse */
1039 entry = menu->entries[++index];
1040 entry->flags.indicator_on = dock->auto_collapse;
1041 entry->clientdata = dock;
1043 /* auto-raise/lower */
1044 entry = menu->entries[++index];
1045 entry->flags.indicator_on = dock->auto_raise_lower;
1046 entry->clientdata = dock;
1048 /* attract icons */
1049 entry = menu->entries[++index];
1050 entry->flags.indicator_on = dock->attract_icons;
1051 entry->clientdata = dock;
1053 menu->flags.realized = 0;
1054 wMenuRealize(menu);
1058 static WMenu*
1059 makeClipOptionsMenu(WScreen *scr)
1061 WMenu *menu;
1062 WMenuEntry *entry;
1064 menu = wMenuCreate(scr, NULL, False);
1065 if (!menu) {
1066 wwarning(_("could not create options submenu for Clip menu"));
1067 return NULL;
1070 entry = wMenuAddCallback(menu, _("Keep on Top"),
1071 toggleLoweredCallback, NULL);
1072 entry->flags.indicator = 1;
1073 entry->flags.indicator_on = 1;
1074 entry->flags.indicator_type = MI_CHECK;
1076 entry = wMenuAddCallback(menu, _("Collapsed"),
1077 toggleCollapsedCallback, NULL);
1078 entry->flags.indicator = 1;
1079 entry->flags.indicator_on = 1;
1080 entry->flags.indicator_type = MI_CHECK;
1082 entry = wMenuAddCallback(menu, _("AutoCollapse"),
1083 toggleAutoCollapseCallback, NULL);
1084 entry->flags.indicator = 1;
1085 entry->flags.indicator_on = 1;
1086 entry->flags.indicator_type = MI_CHECK;
1088 entry = wMenuAddCallback(menu, _("AutoRaiseLower"),
1089 toggleAutoRaiseLowerCallback, NULL);
1090 entry->flags.indicator = 1;
1091 entry->flags.indicator_on = 1;
1092 entry->flags.indicator_type = MI_CHECK;
1094 entry = wMenuAddCallback(menu, _("AutoAttract Icons"),
1095 toggleAutoAttractCallback, NULL);
1096 entry->flags.indicator = 1;
1097 entry->flags.indicator_on = 1;
1098 entry->flags.indicator_type = MI_CHECK;
1100 menu->flags.realized = 0;
1101 wMenuRealize(menu);
1103 return menu;
1107 static WMenu*
1108 dockMenuCreate(WScreen *scr, int type)
1110 WMenu *menu;
1111 WMenuEntry *entry;
1113 if (type == WM_CLIP && scr->clip_menu)
1114 return scr->clip_menu;
1116 menu = wMenuCreate(scr, NULL, False);
1117 if (type != WM_CLIP) {
1118 entry = wMenuAddCallback(menu, _("Keep on top"),
1119 toggleLoweredCallback, NULL);
1120 entry->flags.indicator = 1;
1121 entry->flags.indicator_on = 1;
1122 entry->flags.indicator_type = MI_CHECK;
1123 } else {
1124 entry = wMenuAddCallback(menu, _("Clip Options"), NULL, NULL);
1125 scr->clip_options = makeClipOptionsMenu(scr);
1126 if (scr->clip_options)
1127 wMenuEntrySetCascade(menu, entry, scr->clip_options);
1129 entry = wMenuAddCallback(menu, _("Rename Workspace"), renameCallback,
1130 NULL);
1131 free(entry->text);
1132 entry->text = _("Rename Workspace");
1134 entry = wMenuAddCallback(menu, _("Selected"), selectCallback, NULL);
1135 entry->flags.indicator = 1;
1136 entry->flags.indicator_on = 1;
1137 entry->flags.indicator_type = MI_CHECK;
1139 entry = wMenuAddCallback(menu, _("Select All Icons"),
1140 selectIconsCallback, NULL);
1141 free(entry->text);
1142 entry->text = _("Select All Icons");
1144 entry = wMenuAddCallback(menu, _("Keep Icon"), keepIconsCallback, NULL);
1145 free(entry->text);
1146 entry->text = _("Keep Icon");
1148 entry = wMenuAddCallback(menu, _("Move Icon To"), NULL, NULL);
1149 free(entry->text);
1150 entry->text = _("Move Icon To");
1151 scr->clip_submenu = makeWorkspaceMenu(scr);
1152 if (scr->clip_submenu)
1153 wMenuEntrySetCascade(menu, entry, scr->clip_submenu);
1155 entry = wMenuAddCallback(menu, _("Remove Icon"), removeIconsCallback,
1156 NULL);
1157 free(entry->text);
1158 entry->text = _("Remove Icon");
1160 wMenuAddCallback(menu, _("Attract Icons"), colectIconsCallback, NULL);
1163 wMenuAddCallback(menu, _("Launch"), launchCallback, NULL);
1165 wMenuAddCallback(menu, _("Unhide Here"), unhideHereCallback, NULL);
1167 entry = wMenuAddCallback(menu, _("Hide"), hideCallback, NULL);
1168 free(entry->text);
1169 entry->text = _("Hide");
1171 wMenuAddCallback(menu, _("Settings..."), settingsCallback, NULL);
1173 wMenuAddCallback(menu, _("Kill"), killCallback, NULL);
1175 if (type == WM_CLIP)
1176 scr->clip_menu = menu;
1178 return menu;
1182 WDock*
1183 wDockCreate(WScreen *scr, int type)
1185 WDock *dock;
1186 WAppIcon *btn;
1187 int icon_count;
1189 make_keys();
1191 dock = wmalloc(sizeof(WDock));
1192 memset(dock, 0, sizeof(WDock));
1194 if (type == WM_CLIP)
1195 icon_count = CLIP_MAX_ICONS;
1196 else
1197 icon_count = scr->scr_height/wPreferences.icon_size;
1199 dock->icon_array = wmalloc(sizeof(WAppIcon*)*icon_count);
1200 memset(dock->icon_array, 0, sizeof(WAppIcon*)*icon_count);
1202 dock->max_icons = icon_count;
1204 btn = mainIconCreate(scr, type);
1206 btn->dock = dock;
1208 dock->x_pos = btn->x_pos;
1209 dock->y_pos = btn->y_pos;
1210 dock->screen_ptr = scr;
1211 dock->type = type;
1212 dock->icon_count = 1;
1213 dock->on_right_side = 1;
1214 dock->collapsed = 0;
1215 dock->auto_collapse = 0;
1216 dock->auto_collapse_magic = NULL;
1217 dock->auto_raise_lower = 0;
1218 dock->auto_lower_magic = NULL;
1219 dock->auto_raise_magic = NULL;
1220 dock->attract_icons = 0;
1221 dock->lowered = 1;
1222 dock->icon_array[0] = btn;
1223 wRaiseFrame(btn->icon->core);
1224 XMoveWindow(dpy, btn->icon->core->window, btn->x_pos, btn->y_pos);
1226 /* create dock menu */
1227 dock->menu = dockMenuCreate(scr, type);
1229 return dock;
1233 void
1234 wDockDestroy(WDock *dock)
1236 int i;
1237 WAppIcon *aicon;
1239 for (i=(dock->type == WM_CLIP) ? 1 : 0; i<dock->max_icons; i++) {
1240 aicon = dock->icon_array[i];
1241 if (aicon) {
1242 int keepit = aicon->running && wApplicationOf(aicon->main_window);
1243 wDockDetach(dock, aicon);
1244 if (keepit) {
1245 PlaceIcon(dock->screen_ptr, &aicon->x_pos, &aicon->y_pos);
1246 XMoveWindow(dpy, aicon->icon->core->window,
1247 aicon->x_pos, aicon->y_pos);
1248 if (!dock->mapped || dock->collapsed)
1249 XMapWindow(dpy, aicon->icon->core->window);
1253 if (wPreferences.auto_arrange_icons)
1254 wArrangeIcons(dock->screen_ptr, True);
1255 free(dock->icon_array);
1256 if (dock->menu && dock->type!=WM_CLIP)
1257 wMenuDestroy(dock->menu, True);
1258 free(dock);
1262 void
1263 wClipIconPaint(WAppIcon *aicon)
1265 WScreen *scr = aicon->icon->core->screen_ptr;
1266 WWorkspace *workspace = scr->workspaces[scr->current_workspace];
1267 GC gc;
1268 Window win = aicon->icon->core->window;
1269 int length, nlength;
1270 char *ws_name, ws_number[10];
1271 int ty, tx;
1273 wIconPaint(aicon->icon);
1275 length = strlen(workspace->name);
1276 ws_name = malloc(length + 1);
1277 sprintf(ws_name, "%s", workspace->name);
1278 sprintf(ws_number, "%i", scr->current_workspace + 1);
1279 nlength = strlen(ws_number);
1281 gc = scr->clip_title_gc;
1283 if (!workspace->clip->collapsed)
1284 XSetForeground(dpy, gc, scr->clip_title_pixel[CLIP_NORMAL]);
1285 else
1286 XSetForeground(dpy, gc, scr->clip_title_pixel[CLIP_COLLAPSED]);
1288 ty = ICON_SIZE - WMFontHeight(scr->clip_title_font) - 3;
1290 tx = CLIP_BUTTON_SIZE*ICON_SIZE/64;
1292 WMDrawString(scr->wmscreen, win, gc, scr->clip_title_font, tx,
1293 ty, ws_name, length);
1295 tx = (ICON_SIZE/2 - WMWidthOfString(scr->clip_title_font, ws_number,
1296 nlength))/2;
1298 WMDrawString(scr->wmscreen, win, gc, scr->clip_title_font, tx,
1299 2, ws_number, nlength);
1301 free(ws_name);
1303 if (aicon->launching) {
1304 XFillRectangle(dpy, aicon->icon->core->window, scr->stipple_gc,
1305 0, 0, wPreferences.icon_size, wPreferences.icon_size);
1307 paintClipButtons(aicon, aicon->dock->lclip_button_pushed,
1308 aicon->dock->rclip_button_pushed);
1312 static void
1313 clipIconExpose(WObjDescriptor *desc, XEvent *event)
1315 wClipIconPaint(desc->parent);
1319 static void
1320 dockIconPaint(WAppIcon *btn)
1322 if (btn == btn->icon->core->screen_ptr->clip_icon)
1323 wClipIconPaint(btn);
1324 else
1325 wAppIconPaint(btn);
1329 static proplist_t
1330 make_icon_state(WAppIcon *btn)
1332 proplist_t node = NULL;
1333 proplist_t command, autolaunch, lock, name, forced, host, position, buggy;
1334 proplist_t omnipresent;
1335 char *tmp;
1336 char buffer[64];
1338 if (btn) {
1339 if (!btn->command)
1340 command = PLMakeString("-");
1341 else
1342 command = PLMakeString(btn->command);
1344 autolaunch = btn->auto_launch ? dYes : dNo;
1346 lock = btn->lock ? dYes : dNo;
1348 tmp = EscapeWM_CLASS(btn->wm_instance, btn->wm_class);
1350 name = PLMakeString(tmp);
1352 free(tmp);
1354 forced = btn->forced_dock ? dYes : dNo;
1356 buggy = btn->buggy_app ? dYes : dNo;
1358 if (btn == btn->icon->core->screen_ptr->clip_icon)
1359 sprintf(buffer, "%i,%i", btn->x_pos, btn->y_pos);
1360 else
1361 sprintf(buffer, "%hi,%hi", btn->xindex, btn->yindex);
1362 position = PLMakeString(buffer);
1364 node = PLMakeDictionaryFromEntries(dCommand, command,
1365 dName, name,
1366 dAutoLaunch, autolaunch,
1367 dLock, lock,
1368 dForced, forced,
1369 dBuggyApplication, buggy,
1370 dPosition, position,
1371 NULL);
1372 PLRelease(command);
1373 PLRelease(name);
1374 PLRelease(position);
1376 omnipresent = btn->omnipresent ? dYes : dNo;
1377 if (btn->dock != btn->icon->core->screen_ptr->dock &&
1378 (btn->xindex != 0 || btn->yindex != 0))
1379 PLInsertDictionaryEntry(node, dOmnipresent, omnipresent);
1381 #ifdef OFFIX_DND
1382 if (btn->dnd_command) {
1383 command = PLMakeString(btn->dnd_command);
1384 PLInsertDictionaryEntry(node, dDropCommand, command);
1385 PLRelease(command);
1387 #endif /* OFFIX_DND */
1389 if (btn->client_machine && btn->remote_start) {
1390 host = PLMakeString(btn->client_machine);
1391 PLInsertDictionaryEntry(node, dHost, host);
1392 PLRelease(host);
1396 return node;
1400 static proplist_t
1401 dockSaveState(WDock *dock)
1403 int i;
1404 proplist_t icon_info;
1405 proplist_t list=NULL, dock_state=NULL;
1406 proplist_t value, key;
1407 char buffer[256];
1409 list = PLMakeArrayFromElements(NULL);
1411 for (i=(dock->type==WM_DOCK ? 0 : 1); i<dock->max_icons; i++) {
1412 WAppIcon *btn = dock->icon_array[i];
1414 if (!btn || btn->attracted)
1415 continue;
1417 if ((icon_info = make_icon_state(dock->icon_array[i]))) {
1418 list = PLAppendArrayElement(list, icon_info);
1419 PLRelease(icon_info);
1423 dock_state = PLMakeDictionaryFromEntries(dApplications, list,
1424 NULL);
1426 if (dock->type == WM_DOCK) {
1427 sprintf(buffer, "Applications%i", dock->screen_ptr->scr_height);
1428 key = PLMakeString(buffer);
1429 PLInsertDictionaryEntry(dock_state, key, list);
1430 PLRelease(key);
1433 sprintf(buffer, "%i,%i", (dock->on_right_side ? -ICON_SIZE : 0),
1434 dock->y_pos);
1435 value = PLMakeString(buffer);
1436 PLInsertDictionaryEntry(dock_state, dPosition, value);
1437 PLRelease(value);
1439 PLRelease(list);
1442 value = (dock->lowered ? dYes : dNo);
1443 PLInsertDictionaryEntry(dock_state, dLowered, value);
1445 if (dock->type == WM_CLIP) {
1446 value = (dock->collapsed ? dYes : dNo);
1447 PLInsertDictionaryEntry(dock_state, dCollapsed, value);
1449 value = (dock->auto_collapse ? dYes : dNo);
1450 PLInsertDictionaryEntry(dock_state, dAutoCollapse, value);
1452 value = (dock->auto_raise_lower ? dYes : dNo);
1453 PLInsertDictionaryEntry(dock_state, dAutoRaiseLower, value);
1455 value = (dock->attract_icons ? dYes : dNo);
1456 PLInsertDictionaryEntry(dock_state, dAutoAttractIcons, value);
1459 return dock_state;
1463 void
1464 wDockSaveState(WScreen *scr, proplist_t old_state)
1466 proplist_t dock_state;
1467 proplist_t keys;
1469 dock_state = dockSaveState(scr->dock);
1472 * Copy saved states of docks with different sizes.
1474 if (old_state) {
1475 int i;
1476 proplist_t tmp;
1478 keys = PLGetAllDictionaryKeys(old_state);
1479 for (i = 0; i < PLGetNumberOfElements(keys); i++) {
1480 tmp = PLGetArrayElement(keys, i);
1482 if (strncasecmp(PLGetString(tmp), "applications", 12) == 0
1483 && !PLGetDictionaryEntry(dock_state, tmp)) {
1485 PLInsertDictionaryEntry(dock_state,
1486 tmp,
1487 PLGetDictionaryEntry(old_state, tmp));
1490 PLRelease(keys);
1494 PLInsertDictionaryEntry(scr->session_state, dDock, dock_state);
1496 PLRelease(dock_state);
1500 void
1501 wClipSaveState(WScreen *scr)
1503 proplist_t clip_state;
1505 clip_state = make_icon_state(scr->clip_icon);
1507 PLInsertDictionaryEntry(scr->session_state, dClip, clip_state);
1509 PLRelease(clip_state);
1513 proplist_t
1514 wClipSaveWorkspaceState(WScreen *scr, int workspace)
1516 return dockSaveState(scr->workspaces[workspace]->clip);
1520 static Bool
1521 getBooleanDockValue(proplist_t value, proplist_t key)
1523 if (value) {
1524 if (PLIsString(value)) {
1525 if (strcasecmp(PLGetString(value), "YES")==0)
1526 return True;
1527 } else {
1528 wwarning(_("bad value in docked icon state info %s"),
1529 PLGetString(key));
1532 return False;
1536 static WAppIcon*
1537 restore_icon_state(WScreen *scr, proplist_t info, int type, int index)
1539 WAppIcon *aicon;
1540 char *wclass, *winstance;
1541 proplist_t cmd, value;
1542 char *command;
1545 cmd = PLGetDictionaryEntry(info, dCommand);
1546 if (!cmd || !PLIsString(cmd)) {
1547 return NULL;
1550 /* parse window name */
1551 value = PLGetDictionaryEntry(info, dName);
1552 if (!value)
1553 return NULL;
1555 ParseWindowName(value, &winstance, &wclass, "dock");
1557 if (!winstance && !wclass) {
1558 return NULL;
1561 /* get commands */
1563 if (cmd)
1564 command = wstrdup(PLGetString(cmd));
1565 else
1566 command = NULL;
1568 if (!command || strcmp(command, "-")==0) {
1569 if (command)
1570 free(command);
1571 if (wclass)
1572 free(wclass);
1573 if (winstance)
1574 free(winstance);
1576 return NULL;
1579 aicon = wAppIconCreateForDock(scr, command, winstance, wclass,
1580 TILE_NORMAL);
1581 if (wclass)
1582 free(wclass);
1583 if (winstance)
1584 free(winstance);
1586 aicon->icon->core->descriptor.handle_mousedown = iconMouseDown;
1587 if (type == WM_CLIP) {
1588 aicon->icon->core->descriptor.handle_enternotify = clipEnterNotify;
1589 aicon->icon->core->descriptor.handle_leavenotify = clipLeaveNotify;
1591 aicon->icon->core->descriptor.parent_type = WCLASS_DOCK_ICON;
1592 aicon->icon->core->descriptor.parent = aicon;
1595 #ifdef OFFIX_DND
1596 cmd = PLGetDictionaryEntry(info, dDropCommand);
1597 if (cmd)
1598 aicon->dnd_command = wstrdup(PLGetString(cmd));
1599 #endif
1601 /* check auto launch */
1602 value = PLGetDictionaryEntry(info, dAutoLaunch);
1604 aicon->auto_launch = getBooleanDockValue(value, dAutoLaunch);
1606 /* check lock */
1607 value = PLGetDictionaryEntry(info, dLock);
1609 aicon->lock = getBooleanDockValue(value, dLock);
1611 /* check if it wasn't normally docked */
1612 value = PLGetDictionaryEntry(info, dForced);
1614 aicon->forced_dock = getBooleanDockValue(value, dForced);
1616 /* check if we can rely on the stuff in the app */
1617 value = PLGetDictionaryEntry(info, dBuggyApplication);
1619 aicon->buggy_app = getBooleanDockValue(value, dBuggyApplication);
1621 /* get position in the dock */
1622 value = PLGetDictionaryEntry(info, dPosition);
1623 if (value && PLIsString(value)) {
1624 if (sscanf(PLGetString(value), "%hi,%hi", &aicon->xindex,
1625 &aicon->yindex)!=2)
1626 wwarning(_("bad value in docked icon state info %s"),
1627 PLGetString(dPosition));
1629 /* check position sanity */
1630 /* incomplete section! */
1631 if (type == WM_DOCK) {
1632 aicon->xindex = 0;
1633 if (aicon->yindex < 0)
1634 wwarning(_("bad value in docked icon position %i,%i"),
1635 aicon->xindex, aicon->yindex);
1637 } else {
1638 aicon->yindex = index;
1639 aicon->xindex = 0;
1642 /* check if icon is omnipresent */
1643 value = PLGetDictionaryEntry(info, dOmnipresent);
1645 aicon->omnipresent = getBooleanDockValue(value, dOmnipresent);
1647 aicon->running = 0;
1648 aicon->docked = 1;
1650 return aicon;
1654 #define COMPLAIN(key) wwarning(_("bad value in dock state info:%s"), key)
1657 WAppIcon*
1658 wClipRestoreState(WScreen *scr, proplist_t clip_state)
1660 WAppIcon *icon;
1661 proplist_t value;
1664 icon = mainIconCreate(scr, WM_CLIP);
1666 if (!clip_state)
1667 return icon;
1668 else
1669 PLRetain(clip_state);
1671 /* restore position */
1673 value = PLGetDictionaryEntry(clip_state, dPosition);
1675 if (value) {
1676 if (!PLIsString(value))
1677 COMPLAIN("Position");
1678 else {
1679 if (sscanf(PLGetString(value), "%i,%i", &icon->x_pos,
1680 &icon->y_pos)!=2)
1681 COMPLAIN("Position");
1683 /* check position sanity */
1684 if (icon->y_pos < 0)
1685 icon->y_pos = 0;
1686 else if (icon->y_pos > scr->scr_height-ICON_SIZE)
1687 icon->y_pos = scr->scr_height-ICON_SIZE;
1689 if (icon->x_pos < 0)
1690 icon->x_pos = 0;
1691 else if (icon->x_pos > scr->scr_width-ICON_SIZE)
1692 icon->x_pos = scr->scr_width-ICON_SIZE;
1696 #ifdef OFFIX_DND
1697 value = PLGetDictionaryEntry(clip_state, dDropCommand);
1698 if (value && PLIsString(value))
1699 icon->dnd_command = wstrdup(PLGetString(value));
1700 #endif
1702 PLRelease(clip_state);
1704 return icon;
1708 WDock*
1709 wDockRestoreState(WScreen *scr, proplist_t dock_state, int type)
1711 WDock *dock;
1712 proplist_t apps;
1713 proplist_t value;
1714 WAppIcon *aicon, *old_top;
1715 int count, i;
1718 dock = wDockCreate(scr, type);
1720 if (!dock_state)
1721 return dock;
1723 if (dock_state)
1724 PLRetain(dock_state);
1727 /* restore position */
1729 value = PLGetDictionaryEntry(dock_state, dPosition);
1731 if (value) {
1732 if (!PLIsString(value))
1733 COMPLAIN("Position");
1734 else {
1735 if (sscanf(PLGetString(value), "%i,%i", &dock->x_pos,
1736 &dock->y_pos)!=2)
1737 COMPLAIN("Position");
1739 /* check position sanity */
1740 if (dock->y_pos < 0)
1741 dock->y_pos = 0;
1742 else if (dock->y_pos > scr->scr_height-ICON_SIZE)
1743 dock->y_pos = scr->scr_height - ICON_SIZE;
1745 /* This is no more needed. ??? */
1746 if (type == WM_CLIP) {
1747 if (dock->x_pos < 0)
1748 dock->x_pos = 0;
1749 else if (dock->x_pos > scr->scr_width-ICON_SIZE)
1750 dock->x_pos = scr->scr_width-ICON_SIZE;
1751 } else {
1752 if (dock->x_pos >= 0) {
1753 dock->x_pos = DOCK_EXTRA_SPACE;
1754 dock->on_right_side = 0;
1755 } else {
1756 dock->x_pos = scr->scr_width - DOCK_EXTRA_SPACE - ICON_SIZE;
1757 dock->on_right_side = 1;
1763 /* restore lowered/raised state */
1765 dock->lowered = 0;
1767 value = PLGetDictionaryEntry(dock_state, dLowered);
1769 if (value) {
1770 if (!PLIsString(value))
1771 COMPLAIN("Lowered");
1772 else {
1773 if (strcasecmp(PLGetString(value), "YES")==0)
1774 dock->lowered = 1;
1779 /* restore collapsed state */
1781 dock->collapsed = 0;
1783 value = PLGetDictionaryEntry(dock_state, dCollapsed);
1785 if (value) {
1786 if (!PLIsString(value))
1787 COMPLAIN("Collapsed");
1788 else {
1789 if (strcasecmp(PLGetString(value), "YES")==0)
1790 dock->collapsed = 1;
1795 /* restore auto-collapsed state */
1797 value = PLGetDictionaryEntry(dock_state, dAutoCollapse);
1799 if (value) {
1800 if (!PLIsString(value))
1801 COMPLAIN("AutoCollapse");
1802 else {
1803 if (strcasecmp(PLGetString(value), "YES")==0) {
1804 dock->auto_collapse = 1;
1805 dock->collapsed = 1;
1811 /* restore auto-raise/lower state */
1813 value = PLGetDictionaryEntry(dock_state, dAutoRaiseLower);
1815 if (value) {
1816 if (!PLIsString(value))
1817 COMPLAIN("AutoRaiseLower");
1818 else {
1819 if (strcasecmp(PLGetString(value), "YES")==0) {
1820 dock->auto_raise_lower = 1;
1825 /* restore attract icons state */
1827 dock->attract_icons = 0;
1829 value = PLGetDictionaryEntry(dock_state, dAutoAttractIcons);
1831 if (value) {
1832 if (!PLIsString(value))
1833 COMPLAIN("AutoAttractIcons");
1834 else {
1835 if (strcasecmp(PLGetString(value), "YES")==0)
1836 dock->attract_icons = 1;
1841 /* application list */
1844 proplist_t tmp;
1845 char buffer[64];
1848 * When saving, it saves the dock state in
1849 * Applications and Applicationsnnn
1851 * When loading, it will first try Applicationsnnn.
1852 * If it does not exist, use Applications as default.
1855 sprintf(buffer, "Applications%i", scr->scr_height);
1857 tmp = PLMakeString(buffer);
1858 apps = PLGetDictionaryEntry(dock_state, tmp);
1859 PLRelease(tmp);
1861 if (!apps) {
1862 apps = PLGetDictionaryEntry(dock_state, dApplications);
1866 if (!apps) {
1867 goto finish;
1870 count = PLGetNumberOfElements(apps);
1872 if (count==0)
1873 goto finish;
1875 old_top = dock->icon_array[0];
1877 /* dock->icon_count is set to 1 when dock is created.
1878 * Since Clip is already restored, we want to keep it so for clip,
1879 * but for dock we may change the default top tile, so we set it to 0.
1881 if (type == WM_DOCK)
1882 dock->icon_count = 0;
1884 for (i=0; i<count; i++) {
1885 if (dock->icon_count >= dock->max_icons) {
1886 wwarning(_("there are too many icons stored in dock. Ignoring what doesn't fit"));
1887 break;
1890 value = PLGetArrayElement(apps, i);
1891 aicon = restore_icon_state(scr, value, type, dock->icon_count);
1893 dock->icon_array[dock->icon_count] = aicon;
1895 if (aicon) {
1896 aicon->dock = dock;
1897 aicon->x_pos = dock->x_pos + (aicon->xindex*ICON_SIZE);
1898 aicon->y_pos = dock->y_pos + (aicon->yindex*ICON_SIZE);
1900 if (dock->lowered)
1901 ChangeStackingLevel(aicon->icon->core, WMNormalLevel);
1902 else
1903 ChangeStackingLevel(aicon->icon->core, WMDockLevel);
1905 wCoreConfigure(aicon->icon->core, aicon->x_pos, aicon->y_pos,
1906 0, 0);
1908 if (!dock->collapsed)
1909 XMapWindow(dpy, aicon->icon->core->window);
1910 wRaiseFrame(aicon->icon->core);
1912 dock->icon_count++;
1913 } else if (dock->icon_count==0 && type==WM_DOCK)
1914 dock->icon_count++;
1917 /* if the first icon is not defined, use the default */
1918 if (dock->icon_array[0]==NULL) {
1919 /* update default icon */
1920 old_top->x_pos = dock->x_pos;
1921 old_top->y_pos = dock->y_pos;
1922 if (dock->lowered)
1923 ChangeStackingLevel(old_top->icon->core, WMNormalLevel);
1924 else
1925 ChangeStackingLevel(old_top->icon->core, WMDockLevel);
1926 dock->icon_array[0] = old_top;
1927 XMoveWindow(dpy, old_top->icon->core->window, dock->x_pos, dock->y_pos);
1928 /* we don't need to increment dock->icon_count here because it was
1929 * incremented in the loop above.
1931 } else if (old_top!=dock->icon_array[0]) {
1932 if (old_top == scr->clip_icon)
1933 scr->clip_icon = dock->icon_array[0];
1934 wAppIconDestroy(old_top);
1937 finish:
1938 if (dock_state)
1939 PLRelease(dock_state);
1941 return dock;
1946 void
1947 wDockLaunchWithState(WDock *dock, WAppIcon *btn, WSavedState *state)
1949 if (btn && btn->command && !btn->running && !btn->launching) {
1951 btn->drop_launch = 0;
1953 btn->pid = execCommand(btn, btn->command, state);
1955 if (btn->pid>0) {
1956 if (!btn->forced_dock && !btn->buggy_app) {
1957 btn->launching = 1;
1958 dockIconPaint(btn);
1961 } else {
1962 free(state);
1967 void
1968 wDockDoAutoLaunch(WDock *dock, int workspace)
1970 WAppIcon *btn;
1971 WSavedState *state;
1972 int i;
1974 for (i = 0; i < dock->max_icons; i++) {
1975 btn = dock->icon_array[i];
1976 if (!btn || !btn->auto_launch)
1977 continue;
1979 state = wmalloc(sizeof(WSavedState));
1980 memset(state, 0, sizeof(WSavedState));
1981 state->workspace = workspace;
1982 /* TODO: this is klugy and is very difficult to understand
1983 * what's going on. Try to clean up */
1984 wDockLaunchWithState(dock, btn, state);
1988 #ifdef REDUCE_APPICONS
1989 void
1990 wDockSimulateLaunch(WDock *dock, WAppIcon *btn)
1992 if ((btn == NULL) || (dock == NULL))
1993 return;
1995 if (!btn->running) {
1996 if ((btn->icon->owner == NULL) && (btn->applist))
1997 btn->icon->owner = btn->applist->wapp->main_window_desc;
1998 if (!btn->forced_dock)
1999 btn->launching = 1;
2000 dockIconPaint(btn);
2001 wusleep(5000);
2004 #endif
2006 #ifdef OFFIX_DND
2007 static WDock*
2008 findDock(WScreen *scr, XEvent *event, int *icon_pos)
2010 WDock *dock;
2011 int i;
2013 *icon_pos = -1;
2014 if ((dock = scr->dock)!=NULL) {
2015 for (i=0; i<dock->max_icons; i++) {
2016 if (dock->icon_array[i]
2017 && dock->icon_array[i]->icon->core->window==event->xclient.window) {
2018 *icon_pos = i;
2019 break;
2023 if (*icon_pos<0 && (dock = scr->workspaces[scr->current_workspace]->clip)!=NULL) {
2024 for (i=0; i<dock->max_icons; i++) {
2025 if (dock->icon_array[i]
2026 && dock->icon_array[i]->icon->core->window==event->xclient.window) {
2027 *icon_pos = i;
2028 break;
2032 if(*icon_pos>=0)
2033 return dock;
2034 return NULL;
2039 wDockReceiveDNDDrop(WScreen *scr, XEvent *event)
2041 WDock *dock;
2042 WAppIcon *btn;
2043 int icon_pos;
2045 dock = findDock(scr, event, &icon_pos);
2046 if (!dock)
2047 return False;
2050 * Return True if the drop was on an application icon window.
2051 * In this case, let the ClientMessage handler redirect the
2052 * message to the app.
2054 if (dock->icon_array[icon_pos]->icon->icon_win!=None)
2055 return True;
2057 if (dock->icon_array[icon_pos]->dnd_command!=NULL) {
2058 scr->flags.dnd_data_convertion_status = 0;
2060 btn = dock->icon_array[icon_pos];
2062 if (!btn->forced_dock) {
2063 btn->relaunching = btn->running;
2064 btn->running = 1;
2066 if (btn->wm_instance || btn->wm_class) {
2067 WWindowAttributes attr;
2068 memset(&attr, 0, sizeof(WWindowAttributes));
2069 wDefaultFillAttributes(btn->icon->core->screen_ptr,
2070 btn->wm_instance,
2071 btn->wm_class, &attr, NULL, True);
2073 if (!attr.no_appicon)
2074 btn->launching = 1;
2075 else
2076 btn->running = 0;
2079 btn->drop_launch = 1;
2080 scr->last_dock = dock;
2081 btn->pid = execCommand(btn, btn->dnd_command, NULL);
2082 if (btn->pid>0) {
2083 dockIconPaint(btn);
2084 } else {
2085 btn->launching = 0;
2086 if (!btn->relaunching) {
2087 btn->running = 0;
2091 return False;
2093 #endif /* OFFIX_DND */
2097 Bool
2098 wDockAttachIcon(WDock *dock, WAppIcon *icon, int x, int y)
2100 WWindow *wwin;
2101 char **argv;
2102 int argc;
2103 int index;
2105 wwin = icon->icon->owner;
2106 if (icon->command==NULL) {
2107 icon->editing = 0;
2108 if (XGetCommand(dpy, wwin->client_win, &argv, &argc) && argc>0) {
2110 icon->command = FlattenStringList(argv, argc);
2111 XFreeStringList(argv);
2112 } else {
2113 char *command=NULL;
2115 /* icon->forced_dock = 1;*/
2116 if (dock->type!=WM_CLIP || !icon->attracted) {
2117 icon->editing = 1;
2118 if (wInputDialog(dock->screen_ptr, _("Dock Icon"),
2119 _("Type the command used to launch the application"),
2120 &command)) {
2121 if (command && (command[0]==0 ||
2122 (command[0]=='-' && command[1]==0))) {
2123 free(command);
2124 command = NULL;
2126 icon->command = command;
2127 icon->editing = 0;
2128 } else {
2129 icon->editing = 0;
2130 if (command)
2131 free(command);
2132 /* If the target is the dock, reject the icon. If
2133 * the target is the clip, make it an attracted icon
2135 if (dock->type==WM_CLIP) {
2136 icon->attracted = 1;
2137 if (!icon->icon->shadowed) {
2138 icon->icon->shadowed = 1;
2139 icon->icon->force_paint = 1;
2141 } else {
2142 return False;
2147 } else {
2148 icon->editing = 0;
2151 for (index=1; index<dock->max_icons; index++)
2152 if (dock->icon_array[index] == NULL)
2153 break;
2154 /* if (index == dock->max_icons)
2155 return; */
2157 assert(index < dock->max_icons);
2159 dock->icon_array[index] = icon;
2160 icon->yindex = y;
2161 icon->xindex = x;
2163 icon->omnipresent = 0;
2165 icon->x_pos = dock->x_pos + x*ICON_SIZE;
2166 icon->y_pos = dock->y_pos + y*ICON_SIZE;
2168 dock->icon_count++;
2170 icon->running = 1;
2171 icon->launching = 0;
2172 icon->docked = 1;
2173 icon->dock = dock;
2174 icon->icon->core->descriptor.handle_mousedown = iconMouseDown;
2175 if (dock->type == WM_CLIP) {
2176 icon->icon->core->descriptor.handle_enternotify = clipEnterNotify;
2177 icon->icon->core->descriptor.handle_leavenotify = clipLeaveNotify;
2179 icon->icon->core->descriptor.parent_type = WCLASS_DOCK_ICON;
2180 icon->icon->core->descriptor.parent = icon;
2182 MoveInStackListUnder(dock->icon_array[index-1]->icon->core,
2183 icon->icon->core);
2184 wAppIconMove(icon, icon->x_pos, icon->y_pos);
2185 wAppIconPaint(icon);
2187 if (wPreferences.auto_arrange_icons)
2188 wArrangeIcons(dock->screen_ptr, True);
2190 #ifdef OFFIX_DND
2191 if (icon->command && !icon->dnd_command) {
2192 icon->dnd_command = wmalloc(strlen(icon->command)+8);
2193 sprintf(icon->dnd_command, "%s %%d", icon->command);
2195 #endif
2197 return True;
2201 void
2202 reattachIcon(WDock *dock, WAppIcon *icon, int x, int y)
2204 int index;
2206 for(index=1; index<dock->max_icons; index++) {
2207 if(dock->icon_array[index] == icon)
2208 break;
2210 assert(index < dock->max_icons);
2212 icon->yindex = y;
2213 icon->xindex = x;
2215 icon->x_pos = dock->x_pos + x*ICON_SIZE;
2216 icon->y_pos = dock->y_pos + y*ICON_SIZE;
2220 Bool
2221 moveIconBetweenDocks(WDock *src, WDock *dest, WAppIcon *icon, int x, int y)
2223 WWindow *wwin;
2224 char **argv;
2225 int argc;
2226 int index;
2228 if (src == dest)
2229 return True; /* No move needed, we're already there */
2231 if (dest == NULL)
2232 return False;
2234 wwin = icon->icon->owner;
2237 * For the moment we can't do this if we move icons in Clip from one
2238 * workspace to other, because if we move two or more icons without
2239 * command, the dialog box will not be able to tell us to which of the
2240 * moved icons it applies. -Dan
2242 if ((dest->type==WM_DOCK /*|| dest->keep_attracted*/) && icon->command==NULL) {
2243 if (XGetCommand(dpy, wwin->client_win, &argv, &argc) && argc>0) {
2245 icon->command = FlattenStringList(argv, argc);
2246 XFreeStringList(argv);
2247 } else {
2248 char *command=NULL;
2250 icon->editing = 1;
2251 /* icon->forced_dock = 1;*/
2252 if (wInputDialog(src->screen_ptr, _("Dock Icon"),
2253 _("Type the command used to launch the application"),
2254 &command)) {
2255 if (command && (command[0]==0 ||
2256 (command[0]=='-' && command[1]==0))) {
2257 free(command);
2258 command = NULL;
2260 icon->command = command;
2261 } else {
2262 icon->editing = 0;
2263 if (command)
2264 free(command);
2265 return False;
2267 icon->editing = 0;
2271 if (dest->type == WM_DOCK)
2272 wClipMakeIconOmnipresent(icon, False);
2274 for(index=1; index<src->max_icons; index++) {
2275 if(src->icon_array[index] == icon)
2276 break;
2278 assert(index < src->max_icons);
2280 src->icon_array[index] = NULL;
2281 src->icon_count--;
2283 for(index=1; index<dest->max_icons; index++) {
2284 if(dest->icon_array[index] == NULL)
2285 break;
2287 /* if (index == dest->max_icons)
2288 return; */
2290 assert(index < dest->max_icons);
2292 dest->icon_array[index] = icon;
2293 icon->dock = dest;
2295 /* deselect the icon */
2296 if (icon->icon->selected)
2297 wIconSelect(icon->icon);
2299 if (dest->type == WM_DOCK) {
2300 icon->icon->core->descriptor.handle_enternotify = NULL;
2301 icon->icon->core->descriptor.handle_leavenotify = NULL;
2302 } else {
2303 icon->icon->core->descriptor.handle_enternotify = clipEnterNotify;
2304 icon->icon->core->descriptor.handle_leavenotify = clipLeaveNotify;
2307 /* set it to be kept when moving to dock.
2308 * Unless the icon does not have a command set
2310 if (icon->command && dest->type==WM_DOCK) {
2311 icon->attracted = 0;
2312 if (icon->icon->shadowed) {
2313 icon->icon->shadowed = 0;
2314 icon->icon->force_paint = 1;
2318 if (src->auto_collapse || src->auto_raise_lower)
2319 clipLeave(src);
2321 icon->yindex = y;
2322 icon->xindex = x;
2324 icon->x_pos = dest->x_pos + x*ICON_SIZE;
2325 icon->y_pos = dest->y_pos + y*ICON_SIZE;
2327 dest->icon_count++;
2329 MoveInStackListUnder(dest->icon_array[index-1]->icon->core,
2330 icon->icon->core);
2331 wAppIconPaint(icon);
2333 return True;
2337 void
2338 wDockDetach(WDock *dock, WAppIcon *icon)
2340 int index;
2342 /* make the settings panel be closed */
2343 if (icon->panel) {
2344 DestroyDockAppSettingsPanel(icon->panel);
2347 /* This must be called before icon->dock is set to NULL.
2348 * Don't move it. -Dan
2350 wClipMakeIconOmnipresent(icon, False);
2352 icon->docked = 0;
2353 icon->dock = NULL;
2354 icon->attracted = 0;
2355 icon->auto_launch = 0;
2356 if (icon->icon->shadowed) {
2357 icon->icon->shadowed = 0;
2358 icon->icon->force_paint = 1;
2361 /* deselect the icon */
2362 if (icon->icon->selected)
2363 wIconSelect(icon->icon);
2365 if (icon->command) {
2366 free(icon->command);
2367 icon->command = NULL;
2369 #ifdef OFFIX_DND
2370 if (icon->dnd_command) {
2371 free(icon->dnd_command);
2372 icon->dnd_command = NULL;
2374 #endif
2376 for (index=1; index<dock->max_icons; index++)
2377 if (dock->icon_array[index] == icon)
2378 break;
2379 assert(index < dock->max_icons);
2380 dock->icon_array[index] = NULL;
2381 icon->yindex = -1;
2382 icon->xindex = -1;
2384 dock->icon_count--;
2386 /* if the dock is not attached to an application or
2387 * the the application did not set the approriate hints yet,
2388 * destroy the icon */
2389 #ifdef REDUCE_APPICONS
2390 if ((icon->num_apps == 0) && (!icon->running || !wApplicationOf(icon->main_window)) )
2391 #else
2392 if (!icon->running || !wApplicationOf(icon->main_window))
2393 #endif
2394 wAppIconDestroy(icon);
2395 else {
2396 icon->icon->core->descriptor.handle_mousedown = appIconMouseDown;
2397 icon->icon->core->descriptor.handle_enternotify = NULL;
2398 icon->icon->core->descriptor.handle_leavenotify = NULL;
2399 icon->icon->core->descriptor.parent_type = WCLASS_APPICON;
2400 icon->icon->core->descriptor.parent = icon;
2402 ChangeStackingLevel(icon->icon->core, NORMAL_ICON_LEVEL);
2404 wAppIconPaint(icon);
2405 if (wPreferences.auto_arrange_icons) {
2406 wArrangeIcons(dock->screen_ptr, True);
2409 if (dock->auto_collapse || dock->auto_raise_lower)
2410 clipLeave(dock);
2415 * returns the closest Dock slot index for the passed
2416 * coordinates.
2418 * Returns False if icon can't be docked.
2420 * Note: this function should NEVER alter ret_x or ret_y, unless it will
2421 * return True. -Dan
2423 Bool
2424 wDockSnapIcon(WDock *dock, WAppIcon *icon, int req_x, int req_y,
2425 int *ret_x, int *ret_y, int redocking)
2427 WScreen *scr = dock->screen_ptr;
2428 int dx, dy;
2429 int ex_x, ex_y;
2430 int i, offset = ICON_SIZE/2;
2431 WAppIcon *aicon = NULL;
2432 WAppIcon *nicon = NULL;
2433 int max_y_icons, max_x_icons;
2435 max_x_icons = scr->scr_width/ICON_SIZE;
2436 max_y_icons = scr->scr_height/ICON_SIZE-1;
2438 if (wPreferences.flags.noupdates)
2439 return False;
2441 dx = dock->x_pos;
2442 dy = dock->y_pos;
2444 /* if the dock is full */
2445 if (!redocking &&
2446 (dock->icon_count >= dock->max_icons)) {
2447 return False;
2450 /* exact position */
2451 if (req_y < dy)
2452 ex_y = (req_y - offset - dy)/ICON_SIZE;
2453 else
2454 ex_y = (req_y + offset - dy)/ICON_SIZE;
2456 if (req_x < dx)
2457 ex_x = (req_x - offset - dx)/ICON_SIZE;
2458 else
2459 ex_x = (req_x + offset - dx)/ICON_SIZE;
2461 /* check if the icon is outside the screen boundaries */
2462 if (dx + ex_x*ICON_SIZE < -ICON_SIZE+2 ||
2463 dx + ex_x*ICON_SIZE >= scr->scr_width-1 ||
2464 dy + ex_y*ICON_SIZE < -ICON_SIZE+2 ||
2465 dy + ex_y*ICON_SIZE >= scr->scr_height-1)
2466 return False;
2468 if (dock->type == WM_DOCK) {
2469 if (icon->dock != dock && ex_x != 0)
2470 return False;
2472 aicon = NULL;
2473 for (i=0; i<dock->max_icons; i++) {
2474 nicon = dock->icon_array[i];
2475 if (nicon && nicon->yindex == ex_y) {
2476 aicon = nicon;
2477 break;
2481 if (redocking) {
2482 int sig, done, closest;
2484 /* Possible cases when redocking:
2486 * icon dragged out of range of any slot -> false
2487 * icon dragged to range of free slot
2488 * icon dragged to range of same slot
2489 * icon dragged to range of different icon
2491 if (abs(ex_x) > DOCK_DETTACH_THRESHOLD)
2492 return False;
2494 if (ex_y>=0 && ex_y<=max_y_icons && (aicon==icon || !aicon)) {
2495 *ret_x = 0;
2496 *ret_y = ex_y;
2497 return True;
2500 /* start looking at the upper slot or lower? */
2501 if (ex_y*ICON_SIZE < (req_y + offset - dy))
2502 sig = 1;
2503 else
2504 sig = -1;
2506 closest = -1;
2507 done = 0;
2508 /* look for closest free slot */
2509 for (i=0; i<(DOCK_DETTACH_THRESHOLD+1)*2 && !done; i++) {
2510 int j;
2512 done = 1;
2513 closest = sig*(i/2) + ex_y;
2514 /* check if this slot is used */
2515 if (closest >= 0) {
2516 for (j = 0; j<dock->max_icons; j++) {
2517 if (dock->icon_array[j]
2518 && dock->icon_array[j]->yindex==closest) {
2519 /* slot is used by someone else */
2520 if (dock->icon_array[j]!=icon)
2521 done = 0;
2522 break;
2526 sig = -sig;
2528 if (done && closest >= 0 && closest <= max_y_icons &&
2529 ((ex_y >= closest && ex_y - closest < DOCK_DETTACH_THRESHOLD+1)
2531 (ex_y < closest && closest - ex_y <= DOCK_DETTACH_THRESHOLD+1))) {
2532 *ret_x = 0;
2533 *ret_y = closest;
2534 return True;
2536 } else { /* !redocking */
2538 /* if slot is free and the icon is close enough, return it */
2539 if (!aicon && ex_x == 0 && ex_y >= 0 && ex_y <= max_y_icons) {
2540 *ret_x = 0;
2541 *ret_y = ex_y;
2542 return True;
2545 } else { /* CLIP */
2546 int neighbours = 0;
2547 int start, stop, k;
2549 start = icon->omnipresent ? 0 : scr->current_workspace;
2550 stop = icon->omnipresent ? scr->workspace_count : start+1;
2552 aicon = NULL;
2553 for (k=start; k<stop; k++) {
2554 WDock *tmp = scr->workspaces[k]->clip;
2555 if (!tmp)
2556 continue;
2557 for (i=0; i<tmp->max_icons; i++) {
2558 nicon = tmp->icon_array[i];
2559 if (nicon && nicon->xindex == ex_x && nicon->yindex == ex_y) {
2560 aicon = nicon;
2561 break;
2564 if (aicon)
2565 break;
2567 for (k=start; k<stop; k++) {
2568 WDock *tmp = scr->workspaces[k]->clip;
2569 if (!tmp)
2570 continue;
2571 for (i=0; i<tmp->max_icons; i++) {
2572 nicon = tmp->icon_array[i];
2573 if (nicon && nicon != icon && /* Icon can't be it's own neighbour */
2574 (abs(nicon->xindex - ex_x) <= CLIP_ATTACH_VICINITY &&
2575 abs(nicon->yindex - ex_y) <= CLIP_ATTACH_VICINITY)) {
2576 neighbours = 1;
2577 break;
2580 if (neighbours)
2581 break;
2584 if (neighbours && (aicon==NULL || (redocking && aicon == icon))) {
2585 *ret_x = ex_x;
2586 *ret_y = ex_y;
2587 return True;
2590 return False;
2593 #define MIN(x, y) ((x) > (y) ? (y) : (x))
2594 #define MAX(x, y) ((x) < (y) ? (y) : (x))
2596 #define ON_SCREEN(x, y, sx, ex, sy, ey) \
2597 ((((x)+ICON_SIZE/2) >= (sx)) && (((y)+ICON_SIZE/2) >= (sy)) && \
2598 (((x) + (ICON_SIZE/2)) <= (ex)) && (((y) + (ICON_SIZE/2)) <= ey))
2602 * returns true if it can find a free slot in the dock,
2603 * in which case it changes x_pos and y_pos accordingly.
2604 * Else returns false.
2606 Bool
2607 wDockFindFreeSlot(WDock *dock, int *x_pos, int *y_pos)
2609 WScreen *scr = dock->screen_ptr;
2610 WAppIcon *btn;
2611 WAppIconChain *chain;
2612 unsigned char *slot_map;
2613 int mwidth;
2614 int r;
2615 int x, y;
2616 int i, done = False;
2617 int corner;
2618 int sx=0, sy=0, ex=scr->scr_width, ey=scr->scr_height;
2619 int extra_count=0;
2621 if (dock->type == WM_CLIP &&
2622 dock != scr->workspaces[scr->current_workspace]->clip)
2623 extra_count = scr->global_icon_count;
2625 /* if the dock is full */
2626 if (dock->icon_count+extra_count >= dock->max_icons) {
2627 return False;
2630 if (!wPreferences.flags.nodock && scr->dock) {
2631 if (scr->dock->on_right_side)
2632 ex -= ICON_SIZE + DOCK_EXTRA_SPACE;
2633 else
2634 sx += ICON_SIZE + DOCK_EXTRA_SPACE;
2637 if (ex < dock->x_pos)
2638 ex = dock->x_pos;
2639 if (sx > dock->x_pos+ICON_SIZE)
2640 sx = dock->x_pos+ICON_SIZE;
2641 #define C_NONE 0
2642 #define C_NW 1
2643 #define C_NE 2
2644 #define C_SW 3
2645 #define C_SE 4
2647 /* check if clip is in a corner */
2648 if (dock->type==WM_CLIP) {
2649 if (dock->x_pos < 1 && dock->y_pos < 1)
2650 corner = C_NE;
2651 else if (dock->x_pos < 1 && dock->y_pos >= (ey-ICON_SIZE))
2652 corner = C_SE;
2653 else if (dock->x_pos >= (ex-ICON_SIZE)&& dock->y_pos >= (ey-ICON_SIZE))
2654 corner = C_SW;
2655 else if (dock->x_pos >= (ex-ICON_SIZE) && dock->y_pos < 1)
2656 corner = C_NW;
2657 else
2658 corner = C_NONE;
2659 } else
2660 corner = C_NONE;
2662 /* If the clip is in the corner, use only slots that are in the border
2663 * of the screen */
2664 if (corner!=C_NONE) {
2665 char *hmap, *vmap;
2666 int hcount, vcount;
2668 hcount = MIN(dock->max_icons, scr->scr_width/ICON_SIZE);
2669 vcount = MIN(dock->max_icons, scr->scr_height/ICON_SIZE);
2670 hmap = wmalloc(hcount+1);
2671 memset(hmap, 0, hcount+1);
2672 vmap = wmalloc(vcount+1);
2673 memset(vmap, 0, vcount+1);
2675 /* mark used positions */
2676 switch (corner) {
2677 case C_NE:
2678 for (i=0; i<dock->max_icons; i++) {
2679 btn = dock->icon_array[i];
2680 if (!btn)
2681 continue;
2683 if (btn->xindex==0 && btn->yindex > 0 && btn->yindex < vcount)
2684 vmap[btn->yindex] = 1;
2685 else if (btn->yindex==0 && btn->xindex>0 && btn->xindex<hcount)
2686 hmap[btn->xindex] = 1;
2688 for (chain=scr->global_icons; chain!=NULL; chain=chain->next) {
2689 btn = chain->aicon;
2690 if (btn->xindex==0 && btn->yindex > 0 && btn->yindex < vcount)
2691 vmap[btn->yindex] = 1;
2692 else if (btn->yindex==0 && btn->xindex>0 && btn->xindex<hcount)
2693 hmap[btn->xindex] = 1;
2695 break;
2696 case C_NW:
2697 for (i=0; i<dock->max_icons; i++) {
2698 btn = dock->icon_array[i];
2699 if (!btn)
2700 continue;
2702 if (btn->xindex==0 && btn->yindex > 0 && btn->yindex < vcount)
2703 vmap[btn->yindex] = 1;
2704 else if (btn->yindex==0 && btn->xindex<0 &&btn->xindex>-hcount)
2705 hmap[-btn->xindex] = 1;
2707 for (chain=scr->global_icons; chain!=NULL; chain=chain->next) {
2708 btn = chain->aicon;
2709 if (btn->xindex==0 && btn->yindex > 0 && btn->yindex < vcount)
2710 vmap[btn->yindex] = 1;
2711 else if (btn->yindex==0 && btn->xindex<0 &&btn->xindex>-hcount)
2712 hmap[-btn->xindex] = 1;
2714 break;
2715 case C_SE:
2716 for (i=0; i<dock->max_icons; i++) {
2717 btn = dock->icon_array[i];
2718 if (!btn)
2719 continue;
2721 if (btn->xindex==0 && btn->yindex < 0 && btn->yindex > -vcount)
2722 vmap[-btn->yindex] = 1;
2723 else if (btn->yindex==0 && btn->xindex>0 && btn->xindex<hcount)
2724 hmap[btn->xindex] = 1;
2726 for (chain=scr->global_icons; chain!=NULL; chain=chain->next) {
2727 btn = chain->aicon;
2728 if (btn->xindex==0 && btn->yindex < 0 && btn->yindex > -vcount)
2729 vmap[-btn->yindex] = 1;
2730 else if (btn->yindex==0 && btn->xindex>0 && btn->xindex<hcount)
2731 hmap[btn->xindex] = 1;
2733 break;
2734 case C_SW:
2735 default:
2736 for (i=0; i<dock->max_icons; i++) {
2737 btn = dock->icon_array[i];
2738 if (!btn)
2739 continue;
2741 if (btn->xindex==0 && btn->yindex < 0 && btn->yindex > -vcount)
2742 vmap[-btn->yindex] = 1;
2743 else if (btn->yindex==0 && btn->xindex<0 &&btn->xindex>-hcount)
2744 hmap[-btn->xindex] = 1;
2746 for (chain=scr->global_icons; chain!=NULL; chain=chain->next) {
2747 btn = chain->aicon;
2748 if (btn->xindex==0 && btn->yindex < 0 && btn->yindex > -vcount)
2749 vmap[-btn->yindex] = 1;
2750 else if (btn->yindex==0 && btn->xindex<0 &&btn->xindex>-hcount)
2751 hmap[-btn->xindex] = 1;
2754 x=0; y=0;
2755 done = 0;
2756 /* search a vacant slot */
2757 for (i=1; i<MAX(vcount, hcount); i++) {
2758 if (i < vcount && vmap[i]==0) {
2759 /* found a slot */
2760 x = 0;
2761 y = i;
2762 done = 1;
2763 break;
2764 } else if (i < hcount && hmap[i]==0) {
2765 /* found a slot */
2766 x = i;
2767 y = 0;
2768 done = 1;
2769 break;
2772 free(vmap);
2773 free(hmap);
2774 /* If found a slot, translate and return */
2775 if (done) {
2776 if (corner==C_NW || corner==C_NE) {
2777 *y_pos = y;
2778 } else {
2779 *y_pos = -y;
2781 if (corner==C_NE || corner==C_SE) {
2782 *x_pos = x;
2783 } else {
2784 *x_pos = -x;
2786 return True;
2788 /* else, try to find a slot somewhere else */
2791 /* a map of mwidth x mwidth would be enough if we allowed icons to be
2792 * placed outside of screen */
2793 mwidth = (int)ceil(sqrt(dock->max_icons));
2795 /* In the worst case (the clip is in the corner of the screen),
2796 * the amount of icons that fit in the clip is smaller.
2797 * Double the map to get a safe value.
2799 mwidth += mwidth;
2801 r = (mwidth-1)/2;
2803 slot_map = wmalloc(mwidth*mwidth);
2804 memset(slot_map, 0, mwidth*mwidth);
2806 #define XY2OFS(x,y) (MAX(abs(x),abs(y)) > r) ? 0 : (((y)+r)*(mwidth)+(x)+r)
2808 /* mark used slots in the map. If the slot falls outside the map
2809 * (for example, when all icons are placed in line), ignore them. */
2810 for (i=0; i<dock->max_icons; i++) {
2811 btn = dock->icon_array[i];
2812 if (btn)
2813 slot_map[XY2OFS(btn->xindex, btn->yindex)] = 1;
2815 for (chain=scr->global_icons; chain!=NULL; chain=chain->next) {
2816 slot_map[XY2OFS(chain->aicon->xindex, chain->aicon->yindex)] = 1;
2818 /* Find closest slot from the center that is free by scanning the
2819 * map from the center to outward in circular passes.
2820 * This will not result in a neat layout, but will be optimal
2821 * in the sense that there will not be holes left.
2823 done = 0;
2824 for (i = 1; i <= r && !done; i++) {
2825 int tx, ty;
2827 /* top and bottom parts of the ring */
2828 for (x = -i; x <= i && !done; x++) {
2829 tx = dock->x_pos + x*ICON_SIZE;
2830 y = -i;
2831 ty = dock->y_pos + y*ICON_SIZE;
2832 if (slot_map[XY2OFS(x,y)]==0
2833 && ON_SCREEN(tx, ty, sx, ex, sy, ey)) {
2834 *x_pos = x;
2835 *y_pos = y;
2836 done = 1;
2837 break;
2839 y = i;
2840 ty = dock->y_pos + y*ICON_SIZE;
2841 if (slot_map[XY2OFS(x,y)]==0
2842 && ON_SCREEN(tx, ty, sx, ex, sy, ey)) {
2843 *x_pos = x;
2844 *y_pos = y;
2845 done = 1;
2846 break;
2849 /* left and right parts of the ring */
2850 for (y = -i+1; y <= i-1; y++) {
2851 ty = dock->y_pos + y*ICON_SIZE;
2852 x = -i;
2853 tx = dock->x_pos + x*ICON_SIZE;
2854 if (slot_map[XY2OFS(x,y)]==0
2855 && ON_SCREEN(tx, ty, sx, ex, sy, ey)) {
2856 *x_pos = x;
2857 *y_pos = y;
2858 done = 1;
2859 break;
2861 x = i;
2862 tx = dock->x_pos + x*ICON_SIZE;
2863 if (slot_map[XY2OFS(x,y)]==0
2864 && ON_SCREEN(tx, ty, sx, ex, sy, ey)) {
2865 *x_pos = x;
2866 *y_pos = y;
2867 done = 1;
2868 break;
2872 free(slot_map);
2873 #undef XY2OFS
2874 return done;
2878 static void
2879 moveDock(WDock *dock, int new_x, int new_y)
2881 WAppIcon *btn;
2882 int i;
2884 dock->x_pos = new_x;
2885 dock->y_pos = new_y;
2886 for (i=0; i<dock->max_icons; i++) {
2887 btn = dock->icon_array[i];
2888 if (btn) {
2889 btn->x_pos = new_x + btn->xindex*ICON_SIZE;
2890 btn->y_pos = new_y + btn->yindex*ICON_SIZE;
2891 XMoveWindow(dpy, btn->icon->core->window, btn->x_pos, btn->y_pos);
2897 static void
2898 swapDock(WDock *dock)
2900 WScreen *scr = dock->screen_ptr;
2901 WAppIcon *btn;
2902 int x, i;
2905 if (dock->on_right_side) {
2906 x = dock->x_pos = scr->scr_width - ICON_SIZE - DOCK_EXTRA_SPACE;
2907 } else {
2908 x = dock->x_pos = DOCK_EXTRA_SPACE;
2911 for (i=0; i<dock->max_icons; i++) {
2912 btn = dock->icon_array[i];
2913 if (btn) {
2914 btn->x_pos = x;
2915 XMoveWindow(dpy, btn->icon->core->window, btn->x_pos, btn->y_pos);
2919 wScreenUpdateUsableArea(scr);
2923 static pid_t
2924 execCommand(WAppIcon *btn, char *command, WSavedState *state)
2926 WScreen *scr = btn->icon->core->screen_ptr;
2927 pid_t pid;
2928 char **argv;
2929 int argc;
2930 char *cmdline;
2932 cmdline = ExpandOptions(scr, command);
2934 if (scr->flags.dnd_data_convertion_status || !cmdline) {
2935 if (cmdline)
2936 free(cmdline);
2937 if (state)
2938 free(state);
2939 return 0;
2942 ParseCommand(cmdline, &argv, &argc);
2944 if (argv==NULL) {
2945 if (cmdline)
2946 free(cmdline);
2947 if (state)
2948 free(state);
2949 return 0;
2952 if ((pid=fork())==0) {
2953 char **args;
2954 int i;
2956 SetupEnvironment(scr);
2958 #ifdef HAVE_SETPGID
2959 setpgid(0, 0);
2960 #endif
2962 args = malloc(sizeof(char*)*(argc+1));
2963 if (!args)
2964 exit(111);
2965 for (i=0; i<argc; i++) {
2966 args[i] = argv[i];
2968 args[argc] = NULL;
2969 execvp(argv[0], args);
2970 exit(111);
2972 while (argc > 0)
2973 free(argv[--argc]);
2974 free(argv);
2976 if (pid > 0) {
2977 if (!state) {
2978 state = wmalloc(sizeof(WSavedState));
2979 memset(state, 0, sizeof(WSavedState));
2980 state->hidden = -1;
2981 state->miniaturized = -1;
2982 state->shaded = -1;
2983 if (btn->dock==scr->dock || btn->omnipresent)
2984 state->workspace = -1;
2985 else
2986 state->workspace = scr->current_workspace;
2988 wWindowAddSavedState(btn->wm_instance, btn->wm_class, cmdline, pid,
2989 state);
2990 wAddDeathHandler(pid, (WDeathHandler*)trackDeadProcess,
2991 btn->dock);
2992 } else if (state) {
2993 free(state);
2995 free(cmdline);
2996 return pid;
3000 void
3001 wDockHideIcons(WDock *dock)
3003 int i;
3005 if (dock==NULL)
3006 return;
3008 for (i=1; i<dock->max_icons; i++) {
3009 if (dock->icon_array[i])
3010 XUnmapWindow(dpy, dock->icon_array[i]->icon->core->window);
3012 dock->mapped = 0;
3014 dockIconPaint(dock->icon_array[0]);
3018 void
3019 wDockShowIcons(WDock *dock)
3021 int i, newlevel;
3022 WAppIcon *btn;
3024 if (dock==NULL)
3025 return;
3027 btn = dock->icon_array[0];
3028 moveDock(dock, btn->x_pos, btn->y_pos);
3030 newlevel = dock->lowered ? WMNormalLevel : WMDockLevel;
3031 ChangeStackingLevel(btn->icon->core, newlevel);
3033 for (i=1; i<dock->max_icons; i++) {
3034 if (dock->icon_array[i]) {
3035 MoveInStackListAbove(dock->icon_array[i]->icon->core,
3036 btn->icon->core);
3037 break;
3041 if (!dock->collapsed) {
3042 for (i=1; i<dock->max_icons; i++) {
3043 if (dock->icon_array[i]) {
3044 XMapWindow(dpy, dock->icon_array[i]->icon->core->window);
3048 dock->mapped = 1;
3050 dockIconPaint(btn);
3054 void
3055 wDockLower(WDock *dock)
3057 int i;
3059 for (i=0; i<dock->max_icons; i++) {
3060 if (dock->icon_array[i])
3061 wLowerFrame(dock->icon_array[i]->icon->core);
3066 void
3067 wDockRaise(WDock *dock)
3069 int i;
3071 for (i=dock->max_icons-1; i>=0; i--) {
3072 if (dock->icon_array[i])
3073 wRaiseFrame(dock->icon_array[i]->icon->core);
3078 void
3079 wDockRaiseLower(WDock *dock)
3081 if (!dock->icon_array[0]->icon->core->stacking->above
3082 ||(dock->icon_array[0]->icon->core->stacking->window_level
3083 !=dock->icon_array[0]->icon->core->stacking->above->stacking->window_level))
3084 wDockLower(dock);
3085 else
3086 wDockRaise(dock);
3090 void
3091 wDockFinishLaunch(WDock *dock, WAppIcon *icon)
3093 icon->launching = 0;
3094 icon->relaunching = 0;
3095 dockIconPaint(icon);
3099 WAppIcon*
3100 wDockFindIconForWindow(WDock *dock, Window window)
3102 WAppIcon *icon;
3103 int i;
3105 for (i=0; i<dock->max_icons; i++) {
3106 icon = dock->icon_array[i];
3107 if (icon && icon->main_window == window)
3108 return icon;
3110 return NULL;
3114 void
3115 wDockTrackWindowLaunch(WDock *dock, Window window)
3117 WAppIcon *icon;
3118 #ifdef REDUCE_APPICONS
3119 WAppIconAppList *tapplist;
3120 #endif
3121 char *wm_class, *wm_instance;
3122 int i;
3123 Bool firstPass = True;
3124 Bool found = False;
3125 char *command = NULL;
3128 int argc;
3129 char **argv;
3131 if (XGetCommand(dpy, window, &argv, &argc)) {
3132 if (argc > 0 && argv != NULL)
3133 command = FlattenStringList(argv,argc);
3134 if (argv) {
3135 XFreeStringList(argv);
3140 if (!PropGetWMClass(window, &wm_class, &wm_instance) ||
3141 (!wm_class && !wm_instance))
3142 return;
3144 retry:
3145 for (i=0; i<dock->max_icons; i++) {
3146 icon = dock->icon_array[i];
3147 if (!icon)
3148 continue;
3150 /* app is already attached to icon */
3151 if (icon->main_window == window) {
3152 found = True;
3153 break;
3156 if ((icon->wm_instance || icon->wm_class)
3157 && (icon->launching
3158 || (dock->screen_ptr->flags.startup && !icon->running))) {
3160 if (icon->wm_instance && wm_instance &&
3161 strcmp(icon->wm_instance, wm_instance)!=0) {
3162 continue;
3164 if (icon->wm_class && wm_class &&
3165 strcmp(icon->wm_class, wm_class)!=0) {
3166 continue;
3168 if (firstPass && command && strcmp(icon->command, command)!=0) {
3169 continue;
3172 if (!icon->relaunching) {
3173 WApplication *wapp;
3175 /* Possibly an application that was docked with dockit,
3176 * but the user did not update WMState to indicate that
3177 * it was docked by force */
3178 wapp = wApplicationOf(window);
3179 if (!wapp) {
3180 icon->forced_dock = 1;
3181 icon->running = 0;
3183 if (!icon->forced_dock)
3184 icon->main_window = window;
3186 #ifdef REDUCE_APPICONS
3187 tapplist = wmalloc(sizeof(WAppIconAppList));
3188 memset(tapplist, 0, sizeof(WAppIconAppList));
3189 tapplist->next = icon->applist;
3190 if (icon->applist)
3191 icon->applist->prev = tapplist;
3192 icon->applist = tapplist;
3193 tapplist->wapp = wApplicationOf(window);
3194 icon->num_apps++;
3195 #endif
3197 found = True;
3198 wDockFinishLaunch(dock, icon);
3199 break;
3203 if (firstPass && !found) {
3204 firstPass = False;
3205 goto retry;
3208 if (command)
3209 free(command);
3211 if (wm_class)
3212 XFree(wm_class);
3213 if (wm_instance)
3214 XFree(wm_instance);
3219 void
3220 wClipUpdateForWorkspaceChange(WScreen *scr, int workspace)
3222 if (!wPreferences.flags.noclip) {
3223 scr->clip_icon->dock = scr->workspaces[workspace]->clip;
3224 if (scr->current_workspace != workspace) {
3225 WDock *old_clip = scr->workspaces[scr->current_workspace]->clip;
3226 WAppIconChain *chain = scr->global_icons;
3228 while (chain) {
3229 moveIconBetweenDocks(chain->aicon->dock,
3230 scr->workspaces[workspace]->clip,
3231 chain->aicon, chain->aicon->xindex,
3232 chain->aicon->yindex);
3233 if (scr->workspaces[workspace]->clip->collapsed)
3234 XUnmapWindow(dpy, chain->aicon->icon->core->window);
3235 chain = chain->next;
3238 wDockHideIcons(old_clip);
3239 if (old_clip->auto_raise_lower) {
3240 if (old_clip->auto_raise_magic) {
3241 WMDeleteTimerHandler(old_clip->auto_raise_magic);
3242 old_clip->auto_raise_magic = NULL;
3244 wDockLower(old_clip);
3246 if (old_clip->auto_collapse) {
3247 if (old_clip->auto_expand_magic) {
3248 WMDeleteTimerHandler(old_clip->auto_expand_magic);
3249 old_clip->auto_expand_magic = NULL;
3251 old_clip->collapsed = 1;
3253 wDockShowIcons(scr->workspaces[workspace]->clip);
3255 if (scr->flags.clip_balloon_mapped)
3256 showClipBalloon(scr->clip_icon->dock, workspace);
3262 static void
3263 trackDeadProcess(pid_t pid, unsigned char status, WDock *dock)
3265 WAppIcon *icon;
3266 int i;
3268 for (i=0; i<dock->max_icons; i++) {
3269 icon = dock->icon_array[i];
3270 if (!icon)
3271 continue;
3273 if (icon->launching && icon->pid == pid) {
3274 if (!icon->relaunching) {
3275 icon->running = 0;
3276 icon->main_window = None;
3278 wDockFinishLaunch(dock, icon);
3279 icon->pid = 0;
3280 if (status==111) {
3281 char msg[PATH_MAX];
3282 #ifdef OFFIX_DND
3283 sprintf(msg, _("Could not execute command \"%s\""),
3284 icon->drop_launch && icon->dnd_command
3285 ? icon->dnd_command : icon->command);
3286 #else
3287 sprintf(msg, _("Could not execute command \"%s\""),
3288 icon->command);
3289 #endif
3290 wMessageDialog(dock->screen_ptr, _("Error"), msg,
3291 _("OK"), NULL, NULL);
3293 break;
3299 static void
3300 toggleLowered(WDock *dock)
3302 WAppIcon *tmp;
3303 int newlevel, i;
3305 /* lower/raise Dock */
3306 if (!dock->lowered) {
3307 newlevel = WMNormalLevel;
3308 dock->lowered = 1;
3309 } else {
3310 newlevel = WMDockLevel;
3311 dock->lowered = 0;
3314 for (i=0; i<dock->max_icons; i++) {
3315 tmp = dock->icon_array[i];
3316 if (!tmp)
3317 continue;
3319 ChangeStackingLevel(tmp->icon->core, newlevel);
3320 if (dock->lowered)
3321 wLowerFrame(tmp->icon->core);
3324 if (dock->type == WM_DOCK)
3325 wScreenUpdateUsableArea(dock->screen_ptr);
3329 static void
3330 toggleCollapsed(WDock *dock)
3332 if (dock->collapsed) {
3333 dock->collapsed = 0;
3334 wDockShowIcons(dock);
3336 else {
3337 dock->collapsed = 1;
3338 wDockHideIcons(dock);
3343 static void
3344 openDockMenu(WDock *dock, WAppIcon *aicon, XEvent *event)
3346 WScreen *scr = dock->screen_ptr;
3347 WObjDescriptor *desc;
3348 WMenuEntry *entry;
3349 WApplication *wapp = NULL;
3350 int index = 0;
3351 int x_pos;
3352 int n_selected;
3353 int appIsRunning = aicon->running && aicon->icon && aicon->icon->owner;
3355 if (dock->type == WM_DOCK) {
3356 /* keep on top */
3357 entry = dock->menu->entries[index];
3358 entry->flags.indicator_on = !dock->lowered;
3359 entry->clientdata = dock;
3360 } else {
3361 /* clip options */
3362 if (scr->clip_options)
3363 updateClipOptionsMenu(scr->clip_options, dock);
3365 n_selected = numberOfSelectedIcons(dock);
3367 /* Rename Workspace */
3368 entry = dock->menu->entries[++index];
3369 if (aicon == scr->clip_icon) {
3370 entry->callback = renameCallback;
3371 entry->clientdata = dock;
3372 entry->flags.indicator = 0;
3373 entry->text = _("Rename Workspace");
3374 } else {
3375 entry->callback = omnipresentCallback;
3376 entry->clientdata = aicon;
3377 if (n_selected > 0) {
3378 entry->flags.indicator = 0;
3379 entry->text = _("Toggle Omnipresent");
3380 } else {
3381 entry->flags.indicator = 1;
3382 entry->flags.indicator_on = aicon->omnipresent;
3383 entry->flags.indicator_type = MI_CHECK;
3384 entry->text = _("Omnipresent");
3388 /* select/unselect icon */
3389 entry = dock->menu->entries[++index];
3390 entry->clientdata = aicon;
3391 entry->flags.indicator_on = aicon->icon->selected;
3392 wMenuSetEnabled(dock->menu, index, aicon!=scr->clip_icon);
3394 /* select/unselect all icons */
3395 entry = dock->menu->entries[++index];
3396 entry->clientdata = aicon;
3397 if (n_selected > 0)
3398 entry->text = _("Unselect All Icons");
3399 else
3400 entry->text = _("Select All Icons");
3401 wMenuSetEnabled(dock->menu, index, dock->icon_count > 1);
3403 /* keep icon(s) */
3404 entry = dock->menu->entries[++index];
3405 entry->clientdata = aicon;
3406 if (n_selected > 1)
3407 entry->text = _("Keep Icons");
3408 else
3409 entry->text = _("Keep Icon");
3410 wMenuSetEnabled(dock->menu, index, dock->icon_count > 1);
3412 /* this is the workspace submenu part */
3413 entry = dock->menu->entries[++index];
3414 if (n_selected > 1)
3415 entry->text = _("Move Icons To");
3416 else
3417 entry->text = _("Move Icon To");
3418 if (scr->clip_submenu)
3419 updateWorkspaceMenu(scr->clip_submenu, aicon);
3420 wMenuSetEnabled(dock->menu, index, !aicon->omnipresent);
3422 /* remove icon(s) */
3423 entry = dock->menu->entries[++index];
3424 entry->clientdata = aicon;
3425 if (n_selected > 1)
3426 entry->text = _("Remove Icons");
3427 else
3428 entry->text = _("Remove Icon");
3429 wMenuSetEnabled(dock->menu, index, dock->icon_count > 1);
3431 /* attract icon(s) */
3432 entry = dock->menu->entries[++index];
3433 entry->clientdata = aicon;
3435 dock->menu->flags.realized = 0;
3436 wMenuRealize(dock->menu);
3439 /* launch */
3440 entry = dock->menu->entries[++index];
3441 entry->clientdata = aicon;
3442 wMenuSetEnabled(dock->menu, index, aicon->command!=NULL);
3444 /* unhide here */
3445 entry = dock->menu->entries[++index];
3446 entry->clientdata = aicon;
3447 wMenuSetEnabled(dock->menu, index, appIsRunning);
3449 /* hide */
3450 entry = dock->menu->entries[++index];
3451 entry->clientdata = aicon;
3452 if (aicon->icon->owner) {
3453 wapp = wApplicationOf(aicon->icon->owner->main_window);
3454 if (wapp && wapp->flags.hidden)
3455 entry->text = _("Unhide");
3456 else
3457 entry->text = _("Hide");
3458 } else {
3459 entry->text = _("Hide");
3461 wMenuSetEnabled(dock->menu, index, appIsRunning);
3463 /* settings */
3464 entry = dock->menu->entries[++index];
3465 entry->clientdata = aicon;
3466 wMenuSetEnabled(dock->menu, index, !aicon->editing
3467 && !wPreferences.flags.noupdates);
3469 /* kill */
3470 entry = dock->menu->entries[++index];
3471 entry->clientdata = aicon;
3472 wMenuSetEnabled(dock->menu, index, appIsRunning);
3474 if (!dock->menu->flags.realized)
3475 wMenuRealize(dock->menu);
3477 if (dock->type == WM_CLIP) {
3478 x_pos = event->xbutton.x_root+2;
3479 } else {
3480 x_pos = dock->on_right_side ?
3481 scr->scr_width - dock->menu->frame->core->width - 2 : 0;
3484 wMenuMapAt(dock->menu, x_pos, event->xbutton.y_root+2, False);
3486 /* allow drag select */
3487 event->xany.send_event = True;
3488 desc = &dock->menu->menu->descriptor;
3489 (*desc->handle_mousedown)(desc, event);
3493 static void
3494 openClipWorkspaceMenu(WScreen *scr, int x, int y)
3496 if (!scr->clip_ws_menu) {
3497 scr->clip_ws_menu = wWorkspaceMenuMake(scr, False);
3499 wWorkspaceMenuUpdate(scr, scr->clip_ws_menu);
3500 wMenuMapAt(scr->clip_ws_menu, x, y, False);
3504 /******************************************************************/
3505 static void
3506 iconDblClick(WObjDescriptor *desc, XEvent *event)
3508 WAppIcon *btn = desc->parent;
3509 WDock *dock = btn->dock;
3510 WApplication *wapp = NULL;
3511 int unhideHere = 0;
3513 #ifdef REDUCE_APPICONS
3514 if ((btn->icon->owner && !(event->xbutton.state & ControlMask)) ||
3515 ((btn->icon->owner == NULL) && (btn->applist != NULL))) {
3516 if (btn->icon->owner == NULL)
3517 btn->icon->owner = btn->applist->wapp->main_window_desc;
3518 #else
3519 if (btn->icon->owner && !(event->xbutton.state & ControlMask)) {
3520 #endif
3521 wapp = wApplicationOf(btn->icon->owner->main_window);
3523 assert(wapp!=NULL);
3525 unhideHere = (event->xbutton.state & ShiftMask);
3527 /* go to the last workspace that the user worked on the app */
3528 if (wapp->last_workspace != dock->screen_ptr->current_workspace
3529 && !unhideHere) {
3530 wWorkspaceChange(dock->screen_ptr, wapp->last_workspace);
3533 wUnhideApplication(wapp, event->xbutton.button==Button2,
3534 unhideHere);
3536 if (event->xbutton.state & MOD_MASK) {
3537 wHideOtherApplications(btn->icon->owner);
3539 } else {
3540 if (event->xbutton.button==Button1) {
3542 if (event->xbutton.state & MOD_MASK) {
3543 /* raise/lower dock */
3544 toggleLowered(dock);
3545 } else if (btn == dock->screen_ptr->clip_icon) {
3546 if (getClipButton(event->xbutton.x, event->xbutton.y)==CLIP_IDLE)
3547 toggleCollapsed(dock);
3548 else
3549 handleClipChangeWorkspace(dock->screen_ptr, event);
3550 } else if (btn->command) {
3551 if (!btn->launching &&
3552 (!btn->running || (event->xbutton.state & ControlMask))) {
3553 launchDockedApplication(btn);
3555 } else if (btn->xindex == 0 && btn->yindex == 0
3556 && btn->dock->type == WM_DOCK) {
3558 wShowGNUstepPanel(dock->screen_ptr);
3565 static void
3566 handleDockMove(WDock *dock, WAppIcon *aicon, XEvent *event)
3568 WScreen *scr = dock->screen_ptr;
3569 int ofs_x=event->xbutton.x, ofs_y=event->xbutton.y;
3570 int x, y;
3571 XEvent ev;
3572 int grabbed = 0, swapped = 0, done;
3573 Pixmap ghost = None;
3574 int superfluous = wPreferences.superfluous; /* we catch it to avoid problems */
3576 #ifdef DEBUG
3577 puts("moving dock");
3578 #endif
3579 if (XGrabPointer(dpy, aicon->icon->core->window, True, ButtonMotionMask
3580 |ButtonReleaseMask|ButtonPressMask, GrabModeAsync,
3581 GrabModeAsync, None, None, CurrentTime) !=GrabSuccess) {
3582 wwarning("pointer grab failed for dock move");
3584 y = 0;
3585 for (x=0; x<dock->max_icons; x++) {
3586 if (dock->icon_array[x]!=NULL &&
3587 dock->icon_array[x]->yindex > y)
3588 y = dock->icon_array[x]->yindex;
3590 y++;
3591 XResizeWindow(dpy, scr->dock_shadow, ICON_SIZE, ICON_SIZE*y);
3593 done = 0;
3594 while (!done) {
3595 WMMaskEvent(dpy, PointerMotionMask|ButtonReleaseMask|ButtonPressMask
3596 |ButtonMotionMask|ExposureMask, &ev);
3597 switch (ev.type) {
3598 case Expose:
3599 WMHandleEvent(&ev);
3600 break;
3602 case MotionNotify:
3603 if (!grabbed) {
3604 if (abs(ofs_x-ev.xmotion.x)>=MOVE_THRESHOLD
3605 || abs(ofs_y-ev.xmotion.y)>=MOVE_THRESHOLD) {
3606 XChangeActivePointerGrab(dpy, ButtonMotionMask
3607 |ButtonReleaseMask|ButtonPressMask,
3608 wCursor[WCUR_MOVE], CurrentTime);
3609 grabbed=1;
3611 break;
3613 if (dock->type == WM_CLIP) {
3614 if (ev.xmotion.x_root - ofs_x < 0) {
3615 x = 0;
3616 } else if (ev.xmotion.x_root - ofs_x + ICON_SIZE >
3617 scr->scr_width) {
3618 x = scr->scr_width - ICON_SIZE;
3619 } else {
3620 x = ev.xmotion.x_root - ofs_x;
3622 if (ev.xmotion.y_root - ofs_y < 0) {
3623 y = 0;
3624 } else if (ev.xmotion.y_root - ofs_y + ICON_SIZE >
3625 scr->scr_height) {
3626 y = scr->scr_height - ICON_SIZE;
3627 } else {
3628 y = ev.xmotion.y_root - ofs_y;
3630 moveDock(dock, x, y);
3631 } else {
3632 /* move vertically if pointer is inside the dock*/
3633 if ((dock->on_right_side &&
3634 ev.xmotion.x_root >= dock->x_pos - ICON_SIZE)
3635 || (!dock->on_right_side &&
3636 ev.xmotion.x_root <= dock->x_pos + ICON_SIZE*2)) {
3638 if (ev.xmotion.y_root - ofs_y < 0) {
3639 y = 0;
3640 } else if (ev.xmotion.y_root - ofs_y + ICON_SIZE >
3641 scr->scr_height) {
3642 y = scr->scr_height - ICON_SIZE;
3643 } else {
3644 y = ev.xmotion.y_root - ofs_y;
3646 moveDock(dock, dock->x_pos, y);
3648 /* move horizontally to change sides */
3649 x = ev.xmotion.x_root - ofs_x;
3650 if (!dock->on_right_side) {
3652 /* is on left */
3654 if (ev.xmotion.x_root > dock->x_pos + ICON_SIZE*2) {
3655 XMoveWindow(dpy, scr->dock_shadow, scr->scr_width-ICON_SIZE
3656 -DOCK_EXTRA_SPACE-1, dock->y_pos);
3657 if (superfluous && ghost==None) {
3658 ghost = MakeGhostDock(dock, dock->x_pos,
3659 scr->scr_width-ICON_SIZE
3660 -DOCK_EXTRA_SPACE-1,
3661 dock->y_pos);
3662 XSetWindowBackgroundPixmap(dpy, scr->dock_shadow,
3663 ghost);
3664 XClearWindow(dpy, scr->dock_shadow);
3666 XMapRaised(dpy, scr->dock_shadow);
3667 swapped = 1;
3668 } else {
3669 if (superfluous && ghost!=None) {
3670 XFreePixmap(dpy, ghost);
3671 ghost = None;
3673 XUnmapWindow(dpy, scr->dock_shadow);
3674 swapped = 0;
3676 } else {
3677 /* is on right */
3678 if (ev.xmotion.x_root < dock->x_pos - ICON_SIZE) {
3679 XMoveWindow(dpy, scr->dock_shadow,
3680 DOCK_EXTRA_SPACE, dock->y_pos);
3681 if (superfluous && ghost==None) {
3682 ghost = MakeGhostDock(dock, dock->x_pos,
3683 DOCK_EXTRA_SPACE, dock->y_pos);
3684 XSetWindowBackgroundPixmap(dpy, scr->dock_shadow,
3685 ghost);
3686 XClearWindow(dpy, scr->dock_shadow);
3688 XMapRaised(dpy, scr->dock_shadow);
3689 swapped = -1;
3690 } else {
3691 XUnmapWindow(dpy, scr->dock_shadow);
3692 swapped = 0;
3693 if (superfluous && ghost!=None) {
3694 XFreePixmap(dpy, ghost);
3695 ghost = None;
3700 break;
3702 case ButtonPress:
3703 break;
3705 case ButtonRelease:
3706 if (ev.xbutton.button != event->xbutton.button)
3707 break;
3708 XUngrabPointer(dpy, CurrentTime);
3709 XUnmapWindow(dpy, scr->dock_shadow);
3710 XResizeWindow(dpy, scr->dock_shadow, ICON_SIZE, ICON_SIZE);
3711 if (dock->type == WM_DOCK) {
3712 if (swapped!=0) {
3713 if (swapped>0)
3714 dock->on_right_side = 1;
3715 else
3716 dock->on_right_side = 0;
3717 swapDock(dock);
3718 wArrangeIcons(scr, False);
3721 done = 1;
3722 break;
3725 if (superfluous) {
3726 if (ghost!=None)
3727 XFreePixmap(dpy, ghost);
3728 XSetWindowBackground(dpy, scr->dock_shadow, scr->white_pixel);
3730 #ifdef DEBUG
3731 puts("End dock move");
3732 #endif
3737 static void
3738 handleIconMove(WDock *dock, WAppIcon *aicon, XEvent *event)
3740 WScreen *scr = dock->screen_ptr;
3741 Window wins[2];
3742 WIcon *icon = aicon->icon;
3743 WDock *dock2 = NULL, *last_dock = dock, *clip = NULL;
3744 int ondock, grabbed = 0, change_dock = 0, collapsed = 0;
3745 XEvent ev;
3746 int x = aicon->x_pos, y = aicon->y_pos;
3747 int ofs_x = event->xbutton.x, ofs_y = event->xbutton.y;
3748 int shad_x = x, shad_y = y;
3749 int ix = aicon->xindex, iy = aicon->yindex;
3750 int tmp;
3751 Pixmap ghost = None;
3752 Bool docked;
3753 int superfluous = wPreferences.superfluous; /* we catch it to avoid problems */
3754 int omnipresent = aicon->omnipresent; /* this must be cached!!! */
3757 if (wPreferences.flags.noupdates)
3758 return;
3760 if (XGrabPointer(dpy, icon->core->window, True, ButtonMotionMask
3761 |ButtonReleaseMask|ButtonPressMask, GrabModeAsync,
3762 GrabModeAsync, None, None, CurrentTime) !=GrabSuccess) {
3763 #ifdef DEBUG0
3764 wwarning("pointer grab failed for icon move");
3765 #endif
3768 if (!(event->xbutton.state & MOD_MASK))
3769 wRaiseFrame(icon->core);
3771 if (!wPreferences.flags.noclip)
3772 clip = scr->workspaces[scr->current_workspace]->clip;
3774 if (dock == scr->dock && !wPreferences.flags.noclip)
3775 dock2 = clip;
3776 else if (dock != scr->dock && !wPreferences.flags.nodock)
3777 dock2 = scr->dock;
3779 wins[0] = icon->core->window;
3780 wins[1] = scr->dock_shadow;
3781 XRestackWindows(dpy, wins, 2);
3782 XMoveResizeWindow(dpy, scr->dock_shadow, aicon->x_pos, aicon->y_pos,
3783 ICON_SIZE, ICON_SIZE);
3784 if (superfluous) {
3785 if (icon->pixmap!=None)
3786 ghost = MakeGhostIcon(scr, icon->pixmap);
3787 else
3788 ghost = MakeGhostIcon(scr, icon->core->window);
3790 XSetWindowBackgroundPixmap(dpy, scr->dock_shadow, ghost);
3791 XClearWindow(dpy, scr->dock_shadow);
3793 XMapWindow(dpy, scr->dock_shadow);
3795 ondock = 1;
3798 while(1) {
3799 XMaskEvent(dpy, PointerMotionMask|ButtonReleaseMask|ButtonPressMask
3800 |ButtonMotionMask|ExposureMask, &ev);
3801 switch (ev.type) {
3802 case Expose:
3803 WMHandleEvent(&ev);
3804 break;
3806 case MotionNotify:
3807 if (!grabbed) {
3808 if (abs(ofs_x-ev.xmotion.x)>=MOVE_THRESHOLD
3809 || abs(ofs_y-ev.xmotion.y)>=MOVE_THRESHOLD) {
3810 XChangeActivePointerGrab(dpy, ButtonMotionMask
3811 |ButtonReleaseMask|ButtonPressMask,
3812 wCursor[WCUR_MOVE], CurrentTime);
3813 grabbed=1;
3814 } else {
3815 break;
3819 if (omnipresent) {
3820 int i;
3821 for (i=0; i<scr->workspace_count; i++) {
3822 if (i == scr->current_workspace)
3823 continue;
3824 wDockShowIcons(scr->workspaces[i]->clip);
3828 x = ev.xmotion.x_root - ofs_x;
3829 y = ev.xmotion.y_root - ofs_y;
3830 tmp = wDockSnapIcon(dock, aicon, x, y, &ix, &iy, True);
3831 if (tmp && dock2) {
3832 change_dock = 0;
3833 if (last_dock != dock && collapsed) {
3834 last_dock->collapsed = 1;
3835 wDockHideIcons(last_dock);
3836 collapsed = 0;
3838 if (!collapsed && (collapsed = dock->collapsed)) {
3839 dock->collapsed = 0;
3840 wDockShowIcons(dock);
3842 if (dock->auto_raise_lower)
3843 wDockRaise(dock);
3844 last_dock = dock;
3845 } else if (dock2) {
3846 tmp = wDockSnapIcon(dock2, aicon, x, y, &ix, &iy, False);
3847 if (tmp) {
3848 change_dock = 1;
3849 if (last_dock != dock2 && collapsed) {
3850 last_dock->collapsed = 1;
3851 wDockHideIcons(last_dock);
3852 collapsed = 0;
3854 if (!collapsed && (collapsed = dock2->collapsed)) {
3855 dock2->collapsed = 0;
3856 wDockShowIcons(dock2);
3858 if (dock2->auto_raise_lower)
3859 wDockRaise(dock2);
3860 last_dock = dock2;
3863 if (aicon->launching
3864 || aicon->lock
3865 || (aicon->running && !(ev.xmotion.state & MOD_MASK))
3866 || (!aicon->running && tmp)) {
3867 shad_x = last_dock->x_pos + ix*wPreferences.icon_size;
3868 shad_y = last_dock->y_pos + iy*wPreferences.icon_size;
3870 XMoveWindow(dpy, scr->dock_shadow, shad_x, shad_y);
3872 if (!ondock) {
3873 XMapWindow(dpy, scr->dock_shadow);
3875 ondock = 1;
3876 } else {
3877 if (ondock) {
3878 XUnmapWindow(dpy, scr->dock_shadow);
3880 ondock = 0;
3882 XMoveWindow(dpy, icon->core->window, x, y);
3883 break;
3885 case ButtonPress:
3886 break;
3888 case ButtonRelease:
3889 if (ev.xbutton.button != event->xbutton.button)
3890 break;
3891 XUngrabPointer(dpy, CurrentTime);
3892 if (ondock) {
3893 SlideWindow(icon->core->window, x, y, shad_x, shad_y);
3894 XUnmapWindow(dpy, scr->dock_shadow);
3895 if (!change_dock) {
3896 reattachIcon(dock, aicon, ix, iy);
3897 if (clip && dock!=clip && clip->auto_raise_lower)
3898 wDockLower(clip);
3899 } else {
3900 docked = moveIconBetweenDocks(dock, dock2, aicon, ix, iy);
3901 if (!docked) {
3902 /* Slide it back if dock rejected it */
3903 SlideWindow(icon->core->window, x, y, aicon->x_pos,
3904 aicon->y_pos);
3905 reattachIcon(dock, aicon, aicon->xindex,aicon->yindex);
3907 if (last_dock->type==WM_CLIP && last_dock->auto_collapse) {
3908 collapsed = 0;
3911 } else {
3912 aicon->x_pos = x;
3913 aicon->y_pos = y;
3914 if (superfluous) {
3915 if (!aicon->running && !wPreferences.no_animations) {
3916 /* We need to deselect it, even if is deselected in
3917 * wDockDetach(), because else DoKaboom() will fail.
3919 if (aicon->icon->selected)
3920 wIconSelect(aicon->icon);
3922 #ifdef WMSOUND
3923 wSoundPlay(WMSOUND_KABOOM);
3924 #endif
3925 DoKaboom(scr,aicon->icon->core->window, x, y);
3926 #ifdef WMSOUND
3927 } else {
3928 wSoundPlay(WMSOUND_UNDOCK);
3929 #endif
3931 #ifdef WMSOUND
3932 } else {
3933 wSoundPlay(WMSOUND_UNDOCK);
3934 #endif
3936 if (clip && clip->auto_raise_lower)
3937 wDockLower(clip);
3938 wDockDetach(dock, aicon);
3940 if (collapsed) {
3941 last_dock->collapsed = 1;
3942 wDockHideIcons(last_dock);
3943 collapsed = 0;
3945 if (superfluous) {
3946 if (ghost!=None)
3947 XFreePixmap(dpy, ghost);
3948 XSetWindowBackground(dpy, scr->dock_shadow, scr->white_pixel);
3950 if (omnipresent) {
3951 int i;
3952 for (i=0; i<scr->workspace_count; i++) {
3953 if (i == scr->current_workspace)
3954 continue;
3955 wDockHideIcons(scr->workspaces[i]->clip);
3959 #ifdef DEBUG
3960 puts("End icon move");
3961 #endif
3962 return;
3968 static int
3969 getClipButton(int px, int py)
3971 int pt = (CLIP_BUTTON_SIZE+2)*ICON_SIZE/64;
3973 if (px < 0 || py < 0 || px >= ICON_SIZE || py >= ICON_SIZE)
3974 return CLIP_IDLE;
3976 if (py <= pt-((int)ICON_SIZE-1-px))
3977 return CLIP_FORWARD;
3978 else if (px <= pt-((int)ICON_SIZE-1-py))
3979 return CLIP_REWIND;
3981 return CLIP_IDLE;
3985 static void
3986 handleClipChangeWorkspace(WScreen *scr, XEvent *event)
3988 XEvent ev;
3989 int done, direction, new_ws;
3990 int new_dir;
3991 WDock *clip = scr->clip_icon->dock;
3993 direction = getClipButton(event->xbutton.x, event->xbutton.y);
3995 clip->lclip_button_pushed = direction==CLIP_REWIND;
3996 clip->rclip_button_pushed = direction==CLIP_FORWARD;
3998 wClipIconPaint(scr->clip_icon);
3999 done = 0;
4000 while(!done) {
4001 WMMaskEvent(dpy, ExposureMask|ButtonMotionMask|ButtonReleaseMask
4002 |ButtonPressMask, &ev);
4003 switch (ev.type) {
4004 case Expose:
4005 WMHandleEvent(&ev);
4006 break;
4008 case MotionNotify:
4009 new_dir = getClipButton(ev.xmotion.x, ev.xmotion.y);
4010 if (new_dir != direction) {
4011 direction = new_dir;
4012 clip->lclip_button_pushed = direction==CLIP_REWIND;
4013 clip->rclip_button_pushed = direction==CLIP_FORWARD;
4014 wClipIconPaint(scr->clip_icon);
4016 break;
4018 case ButtonPress:
4019 break;
4021 case ButtonRelease:
4022 if (ev.xbutton.button == event->xbutton.button)
4023 done = 1;
4027 clip->lclip_button_pushed = 0;
4028 clip->rclip_button_pushed = 0;
4030 new_ws = wPreferences.ws_advance || (event->xbutton.state & ControlMask);
4032 if (direction == CLIP_FORWARD) {
4033 if (scr->current_workspace < scr->workspace_count-1)
4034 wWorkspaceChange(scr, scr->current_workspace+1);
4035 else if (new_ws && scr->current_workspace < MAX_WORKSPACES-1)
4036 wWorkspaceChange(scr, scr->current_workspace+1);
4037 else if (wPreferences.ws_cycle)
4038 wWorkspaceChange(scr, 0);
4040 else if (direction == CLIP_REWIND) {
4041 if (scr->current_workspace > 0)
4042 wWorkspaceChange(scr, scr->current_workspace-1);
4043 else if (scr->current_workspace==0 && wPreferences.ws_cycle)
4044 wWorkspaceChange(scr, scr->workspace_count-1);
4047 wClipIconPaint(scr->clip_icon);
4051 static void
4052 iconMouseDown(WObjDescriptor *desc, XEvent *event)
4054 WAppIcon *aicon = desc->parent;
4055 WDock *dock = aicon->dock;
4056 WScreen *scr = aicon->icon->core->screen_ptr;
4058 if (aicon->editing || WCHECK_STATE(WSTATE_MODAL))
4059 return;
4061 scr->last_dock = dock;
4063 if (dock->menu->flags.mapped)
4064 wMenuUnmap(dock->menu);
4066 if (IsDoubleClick(scr, event)) {
4067 /* double-click was not in the main clip icon */
4068 if (dock->type != WM_CLIP || aicon->xindex!=0 || aicon->yindex!=0
4069 || getClipButton(event->xbutton.x, event->xbutton.y)==CLIP_IDLE) {
4070 iconDblClick(desc, event);
4071 return;
4075 if (dock->type == WM_CLIP && scr->flags.clip_balloon_mapped) {
4076 XUnmapWindow(dpy, scr->clip_balloon);
4077 scr->flags.clip_balloon_mapped = 0;
4080 #ifdef DEBUG
4081 puts("handling dock");
4082 #endif
4083 if (event->xbutton.button == Button1) {
4084 if (event->xbutton.state & MOD_MASK)
4085 wDockLower(dock);
4086 else
4087 wDockRaise(dock);
4089 if ((event->xbutton.state & ShiftMask) && aicon!=scr->clip_icon &&
4090 dock->type!=WM_DOCK) {
4091 wIconSelect(aicon->icon);
4092 return;
4095 if (aicon->yindex==0 && aicon->xindex==0) {
4096 if (getClipButton(event->xbutton.x, event->xbutton.y)!=CLIP_IDLE
4097 && dock->type==WM_CLIP)
4098 handleClipChangeWorkspace(scr, event);
4099 else
4100 handleDockMove(dock, aicon, event);
4101 } else
4102 handleIconMove(dock, aicon, event);
4104 } else if (event->xbutton.button==Button2 && dock->type==WM_CLIP &&
4105 aicon==scr->clip_icon) {
4106 openClipWorkspaceMenu(scr, event->xbutton.x_root+2,
4107 event->xbutton.y_root+2);
4108 if (scr->clip_ws_menu) {
4109 WMenu *menu;
4110 menu = scr->clip_ws_menu;
4111 desc = &menu->menu->descriptor;
4113 event->xany.send_event = True;
4114 (*desc->handle_mousedown)(desc, event);
4116 } else if (event->xbutton.button==Button2 && dock->type==WM_CLIP &&
4117 (event->xbutton.state & ShiftMask) && aicon!=scr->clip_icon) {
4118 wClipMakeIconOmnipresent(aicon, !aicon->omnipresent);
4119 } else if (event->xbutton.button == Button3) {
4120 openDockMenu(dock, aicon, event);
4125 static void
4126 showClipBalloon(WDock *dock, int workspace)
4128 int w, h;
4129 int x, y;
4130 WScreen *scr = dock->screen_ptr;
4131 char *text;
4132 Window stack[2];
4134 scr->flags.clip_balloon_mapped = 1;
4135 XMapWindow(dpy, scr->clip_balloon);
4137 text = scr->workspaces[workspace]->name;
4139 w = WMWidthOfString(scr->clip_title_font, text, strlen(text));
4141 h = WMFontHeight(scr->clip_title_font);
4142 XResizeWindow(dpy, scr->clip_balloon, w, h);
4144 x = dock->x_pos + CLIP_BUTTON_SIZE*ICON_SIZE/64;
4145 y = dock->y_pos + ICON_SIZE - WMFontHeight(scr->clip_title_font) - 3;
4147 if (x+w > scr->scr_width) {
4148 x = scr->scr_width - w;
4149 if (dock->y_pos + ICON_SIZE + h > scr->scr_height)
4150 y = dock->y_pos - h - 1;
4151 else
4152 y = dock->y_pos + ICON_SIZE;
4153 XRaiseWindow(dpy, scr->clip_balloon);
4154 } else {
4155 stack[0] = scr->clip_icon->icon->core->window;
4156 stack[1] = scr->clip_balloon;
4157 XRestackWindows(dpy, stack, 2);
4159 XMoveWindow(dpy, scr->clip_balloon, x, y);
4160 XSetForeground(dpy, scr->clip_title_gc,
4161 scr->clip_title_pixel[CLIP_NORMAL]);
4162 XClearWindow(dpy, scr->clip_balloon);
4163 WMDrawString(scr->wmscreen, scr->clip_balloon, scr->clip_title_gc,
4164 scr->clip_title_font, 0, 0, text, strlen(text));
4168 static void
4169 clipEnterNotify(WObjDescriptor *desc, XEvent *event)
4171 WAppIcon *btn = (WAppIcon*)desc->parent;
4172 WDock *dock;
4173 WScreen *scr;
4175 assert(event->type==EnterNotify);
4177 if(desc->parent_type!=WCLASS_DOCK_ICON)
4178 return;
4180 scr = btn->icon->core->screen_ptr;
4181 if (!btn->omnipresent)
4182 dock = btn->dock;
4183 else
4184 dock = scr->workspaces[scr->current_workspace]->clip;
4186 if (!dock || dock->type!=WM_CLIP)
4187 return;
4189 /* The auto raise/lower code */
4190 if (dock->auto_lower_magic) {
4191 WMDeleteTimerHandler(dock->auto_lower_magic);
4192 dock->auto_lower_magic = NULL;
4194 if (dock->auto_raise_lower && !dock->auto_raise_magic) {
4195 dock->auto_raise_magic = WMAddTimerHandler(AUTO_RAISE_DELAY,
4196 clipAutoRaise,
4197 (void *)dock);
4200 /* The auto expand/collapse code */
4201 if (dock->auto_collapse_magic) {
4202 WMDeleteTimerHandler(dock->auto_collapse_magic);
4203 dock->auto_collapse_magic = NULL;
4205 if (dock->auto_collapse && !dock->auto_expand_magic) {
4206 dock->auto_expand_magic = WMAddTimerHandler(AUTO_EXPAND_DELAY,
4207 clipAutoExpand,
4208 (void *)dock);
4211 if (btn->xindex == 0 && btn->yindex == 0)
4212 showClipBalloon(dock, dock->screen_ptr->current_workspace);
4213 else {
4214 if (dock->screen_ptr->flags.clip_balloon_mapped) {
4215 XUnmapWindow(dpy, dock->screen_ptr->clip_balloon);
4216 dock->screen_ptr->flags.clip_balloon_mapped = 0;
4222 static void
4223 clipLeave(WDock *dock)
4225 XEvent event;
4226 WObjDescriptor *desc = NULL;
4228 if (!dock || dock->type!=WM_CLIP)
4229 return;
4231 if (XCheckTypedEvent(dpy, EnterNotify, &event)!=False) {
4232 if (XFindContext(dpy, event.xcrossing.window, wWinContext,
4233 (XPointer *)&desc)!=XCNOENT
4234 && desc && desc->parent_type==WCLASS_DOCK_ICON
4235 && ((WAppIcon*)desc->parent)->dock
4236 && ((WAppIcon*)desc->parent)->dock->type==WM_CLIP) {
4237 /* We didn't left the Clip yet */
4238 XPutBackEvent(dpy, &event);
4239 return;
4242 XPutBackEvent(dpy, &event);
4243 } else {
4244 /* We entered a withdrawn window, so we're still in Clip */
4245 return;
4248 if (dock->auto_raise_magic) {
4249 WMDeleteTimerHandler(dock->auto_raise_magic);
4250 dock->auto_raise_magic = NULL;
4252 if (dock->auto_raise_lower && !dock->auto_lower_magic) {
4253 dock->auto_lower_magic = WMAddTimerHandler(AUTO_LOWER_DELAY,
4254 clipAutoLower,
4255 (void *)dock);
4258 if (dock->auto_expand_magic) {
4259 WMDeleteTimerHandler(dock->auto_expand_magic);
4260 dock->auto_expand_magic = NULL;
4262 if (dock->auto_collapse && !dock->auto_collapse_magic) {
4263 dock->auto_collapse_magic = WMAddTimerHandler(AUTO_COLLAPSE_DELAY,
4264 clipAutoCollapse,
4265 (void *)dock);
4270 static void
4271 clipLeaveNotify(WObjDescriptor *desc, XEvent *event)
4273 WAppIcon *btn = (WAppIcon*)desc->parent;
4275 assert(event->type==LeaveNotify);
4277 if(desc->parent_type!=WCLASS_DOCK_ICON)
4278 return;
4280 clipLeave(btn->dock);
4284 static void
4285 clipAutoCollapse(void *cdata)
4287 WDock *dock = (WDock *)cdata;
4289 if (dock->type!=WM_CLIP)
4290 return;
4292 if (dock->auto_collapse) {
4293 dock->collapsed = 1;
4294 wDockHideIcons(dock);
4296 dock->auto_collapse_magic = NULL;
4300 static void
4301 clipAutoExpand(void *cdata)
4303 WDock *dock = (WDock *)cdata;
4305 if (dock->type!=WM_CLIP)
4306 return;
4308 if (dock->auto_collapse) {
4309 dock->collapsed = 0;
4310 wDockShowIcons(dock);
4312 dock->auto_expand_magic = NULL;
4316 static void
4317 clipAutoLower(void *cdata)
4319 WDock *dock = (WDock *)cdata;
4321 if (dock->type!=WM_CLIP)
4322 return;
4324 if (dock->auto_raise_lower)
4325 wDockLower(dock);
4327 dock->auto_lower_magic = NULL;
4331 static void
4332 clipAutoRaise(void *cdata)
4334 WDock *dock = (WDock *)cdata;
4336 if (dock->type!=WM_CLIP)
4337 return;
4339 if (dock->auto_raise_lower)
4340 wDockRaise(dock);
4342 if (dock->screen_ptr->flags.clip_balloon_mapped) {
4343 showClipBalloon(dock, dock->screen_ptr->current_workspace);
4346 dock->auto_raise_magic = NULL;
4350 static Bool
4351 iconCanBeOmnipresent(WAppIcon *aicon)
4353 WScreen *scr = aicon->icon->core->screen_ptr;
4354 WDock *clip;
4355 WAppIcon *btn;
4356 int i, j;
4358 for (i=0; i<scr->workspace_count; i++) {
4359 clip = scr->workspaces[i]->clip;
4361 if (clip == aicon->dock)
4362 continue;
4364 if (clip->icon_count + scr->global_icon_count >= clip->max_icons)
4365 return False; /* Clip is full in some workspace */
4367 for (j=0; j<clip->max_icons; j++) {
4368 btn = clip->icon_array[j];
4369 if(btn && btn->xindex==aicon->xindex && btn->yindex==aicon->yindex)
4370 return False;
4374 return True;
4379 wClipMakeIconOmnipresent(WAppIcon *aicon, int omnipresent)
4381 WScreen *scr = aicon->icon->core->screen_ptr;
4382 WAppIconChain *new_entry, *tmp, *tmp1;
4383 int status = WO_SUCCESS;
4385 if ((scr->dock && aicon->dock==scr->dock) || aicon==scr->clip_icon) {
4386 return WO_NOT_APPLICABLE;
4389 if (aicon->omnipresent == omnipresent)
4390 return WO_SUCCESS;
4392 if (omnipresent) {
4393 if (iconCanBeOmnipresent(aicon)) {
4394 aicon->omnipresent = 1;
4395 new_entry = wmalloc(sizeof(WAppIconChain));
4396 new_entry->aicon = aicon;
4397 new_entry->next = scr->global_icons;
4398 scr->global_icons = new_entry;
4399 scr->global_icon_count++;
4400 } else {
4401 aicon->omnipresent = 0;
4402 status = WO_FAILED;
4404 } else {
4405 aicon->omnipresent = 0;
4406 if (aicon == scr->global_icons->aicon) {
4407 tmp = scr->global_icons->next;
4408 free(scr->global_icons);
4409 scr->global_icons = tmp;
4410 scr->global_icon_count--;
4411 } else {
4412 tmp = scr->global_icons;
4413 while (tmp->next) {
4414 if (tmp->next->aicon == aicon) {
4415 tmp1 = tmp->next->next;
4416 free(tmp->next);
4417 tmp->next = tmp1;
4418 scr->global_icon_count--;
4419 break;
4421 tmp = tmp->next;
4426 wAppIconPaint(aicon);
4428 return status;