Fixed a crashing bug
[wmaker-crm.git] / src / dock.c
blob102a598026cfc9648a6e14f192a918702797a414
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 if (dock->screen_ptr->last_dock == dock)
1259 dock->screen_ptr->last_dock = NULL;
1260 free(dock);
1264 void
1265 wClipIconPaint(WAppIcon *aicon)
1267 WScreen *scr = aicon->icon->core->screen_ptr;
1268 WWorkspace *workspace = scr->workspaces[scr->current_workspace];
1269 GC gc;
1270 Window win = aicon->icon->core->window;
1271 int length, nlength;
1272 char *ws_name, ws_number[10];
1273 int ty, tx;
1275 wIconPaint(aicon->icon);
1277 length = strlen(workspace->name);
1278 ws_name = malloc(length + 1);
1279 sprintf(ws_name, "%s", workspace->name);
1280 sprintf(ws_number, "%i", scr->current_workspace + 1);
1281 nlength = strlen(ws_number);
1283 gc = scr->clip_title_gc;
1285 if (!workspace->clip->collapsed)
1286 XSetForeground(dpy, gc, scr->clip_title_pixel[CLIP_NORMAL]);
1287 else
1288 XSetForeground(dpy, gc, scr->clip_title_pixel[CLIP_COLLAPSED]);
1290 ty = ICON_SIZE - WMFontHeight(scr->clip_title_font) - 3;
1292 tx = CLIP_BUTTON_SIZE*ICON_SIZE/64;
1294 WMDrawString(scr->wmscreen, win, gc, scr->clip_title_font, tx,
1295 ty, ws_name, length);
1297 tx = (ICON_SIZE/2 - WMWidthOfString(scr->clip_title_font, ws_number,
1298 nlength))/2;
1300 WMDrawString(scr->wmscreen, win, gc, scr->clip_title_font, tx,
1301 2, ws_number, nlength);
1303 free(ws_name);
1305 if (aicon->launching) {
1306 XFillRectangle(dpy, aicon->icon->core->window, scr->stipple_gc,
1307 0, 0, wPreferences.icon_size, wPreferences.icon_size);
1309 paintClipButtons(aicon, aicon->dock->lclip_button_pushed,
1310 aicon->dock->rclip_button_pushed);
1314 static void
1315 clipIconExpose(WObjDescriptor *desc, XEvent *event)
1317 wClipIconPaint(desc->parent);
1321 static void
1322 dockIconPaint(WAppIcon *btn)
1324 if (btn == btn->icon->core->screen_ptr->clip_icon)
1325 wClipIconPaint(btn);
1326 else
1327 wAppIconPaint(btn);
1331 static proplist_t
1332 make_icon_state(WAppIcon *btn)
1334 proplist_t node = NULL;
1335 proplist_t command, autolaunch, lock, name, forced, host, position, buggy;
1336 proplist_t omnipresent;
1337 char *tmp;
1338 char buffer[64];
1340 if (btn) {
1341 if (!btn->command)
1342 command = PLMakeString("-");
1343 else
1344 command = PLMakeString(btn->command);
1346 autolaunch = btn->auto_launch ? dYes : dNo;
1348 lock = btn->lock ? dYes : dNo;
1350 tmp = EscapeWM_CLASS(btn->wm_instance, btn->wm_class);
1352 name = PLMakeString(tmp);
1354 free(tmp);
1356 forced = btn->forced_dock ? dYes : dNo;
1358 buggy = btn->buggy_app ? dYes : dNo;
1360 if (btn == btn->icon->core->screen_ptr->clip_icon)
1361 sprintf(buffer, "%i,%i", btn->x_pos, btn->y_pos);
1362 else
1363 sprintf(buffer, "%hi,%hi", btn->xindex, btn->yindex);
1364 position = PLMakeString(buffer);
1366 node = PLMakeDictionaryFromEntries(dCommand, command,
1367 dName, name,
1368 dAutoLaunch, autolaunch,
1369 dLock, lock,
1370 dForced, forced,
1371 dBuggyApplication, buggy,
1372 dPosition, position,
1373 NULL);
1374 PLRelease(command);
1375 PLRelease(name);
1376 PLRelease(position);
1378 omnipresent = btn->omnipresent ? dYes : dNo;
1379 if (btn->dock != btn->icon->core->screen_ptr->dock &&
1380 (btn->xindex != 0 || btn->yindex != 0))
1381 PLInsertDictionaryEntry(node, dOmnipresent, omnipresent);
1383 #ifdef OFFIX_DND
1384 if (btn->dnd_command) {
1385 command = PLMakeString(btn->dnd_command);
1386 PLInsertDictionaryEntry(node, dDropCommand, command);
1387 PLRelease(command);
1389 #endif /* OFFIX_DND */
1391 if (btn->client_machine && btn->remote_start) {
1392 host = PLMakeString(btn->client_machine);
1393 PLInsertDictionaryEntry(node, dHost, host);
1394 PLRelease(host);
1398 return node;
1402 static proplist_t
1403 dockSaveState(WDock *dock)
1405 int i;
1406 proplist_t icon_info;
1407 proplist_t list=NULL, dock_state=NULL;
1408 proplist_t value, key;
1409 char buffer[256];
1411 list = PLMakeArrayFromElements(NULL);
1413 for (i=(dock->type==WM_DOCK ? 0 : 1); i<dock->max_icons; i++) {
1414 WAppIcon *btn = dock->icon_array[i];
1416 if (!btn || btn->attracted)
1417 continue;
1419 if ((icon_info = make_icon_state(dock->icon_array[i]))) {
1420 list = PLAppendArrayElement(list, icon_info);
1421 PLRelease(icon_info);
1425 dock_state = PLMakeDictionaryFromEntries(dApplications, list,
1426 NULL);
1428 if (dock->type == WM_DOCK) {
1429 sprintf(buffer, "Applications%i", dock->screen_ptr->scr_height);
1430 key = PLMakeString(buffer);
1431 PLInsertDictionaryEntry(dock_state, key, list);
1432 PLRelease(key);
1435 sprintf(buffer, "%i,%i", (dock->on_right_side ? -ICON_SIZE : 0),
1436 dock->y_pos);
1437 value = PLMakeString(buffer);
1438 PLInsertDictionaryEntry(dock_state, dPosition, value);
1439 PLRelease(value);
1441 PLRelease(list);
1444 value = (dock->lowered ? dYes : dNo);
1445 PLInsertDictionaryEntry(dock_state, dLowered, value);
1447 if (dock->type == WM_CLIP) {
1448 value = (dock->collapsed ? dYes : dNo);
1449 PLInsertDictionaryEntry(dock_state, dCollapsed, value);
1451 value = (dock->auto_collapse ? dYes : dNo);
1452 PLInsertDictionaryEntry(dock_state, dAutoCollapse, value);
1454 value = (dock->auto_raise_lower ? dYes : dNo);
1455 PLInsertDictionaryEntry(dock_state, dAutoRaiseLower, value);
1457 value = (dock->attract_icons ? dYes : dNo);
1458 PLInsertDictionaryEntry(dock_state, dAutoAttractIcons, value);
1461 return dock_state;
1465 void
1466 wDockSaveState(WScreen *scr, proplist_t old_state)
1468 proplist_t dock_state;
1469 proplist_t keys;
1471 dock_state = dockSaveState(scr->dock);
1474 * Copy saved states of docks with different sizes.
1476 if (old_state) {
1477 int i;
1478 proplist_t tmp;
1480 keys = PLGetAllDictionaryKeys(old_state);
1481 for (i = 0; i < PLGetNumberOfElements(keys); i++) {
1482 tmp = PLGetArrayElement(keys, i);
1484 if (strncasecmp(PLGetString(tmp), "applications", 12) == 0
1485 && !PLGetDictionaryEntry(dock_state, tmp)) {
1487 PLInsertDictionaryEntry(dock_state,
1488 tmp,
1489 PLGetDictionaryEntry(old_state, tmp));
1492 PLRelease(keys);
1496 PLInsertDictionaryEntry(scr->session_state, dDock, dock_state);
1498 PLRelease(dock_state);
1502 void
1503 wClipSaveState(WScreen *scr)
1505 proplist_t clip_state;
1507 clip_state = make_icon_state(scr->clip_icon);
1509 PLInsertDictionaryEntry(scr->session_state, dClip, clip_state);
1511 PLRelease(clip_state);
1515 proplist_t
1516 wClipSaveWorkspaceState(WScreen *scr, int workspace)
1518 return dockSaveState(scr->workspaces[workspace]->clip);
1522 static Bool
1523 getBooleanDockValue(proplist_t value, proplist_t key)
1525 if (value) {
1526 if (PLIsString(value)) {
1527 if (strcasecmp(PLGetString(value), "YES")==0)
1528 return True;
1529 } else {
1530 wwarning(_("bad value in docked icon state info %s"),
1531 PLGetString(key));
1534 return False;
1538 static WAppIcon*
1539 restore_icon_state(WScreen *scr, proplist_t info, int type, int index)
1541 WAppIcon *aicon;
1542 char *wclass, *winstance;
1543 proplist_t cmd, value;
1544 char *command;
1547 cmd = PLGetDictionaryEntry(info, dCommand);
1548 if (!cmd || !PLIsString(cmd)) {
1549 return NULL;
1552 /* parse window name */
1553 value = PLGetDictionaryEntry(info, dName);
1554 if (!value)
1555 return NULL;
1557 ParseWindowName(value, &winstance, &wclass, "dock");
1559 if (!winstance && !wclass) {
1560 return NULL;
1563 /* get commands */
1565 if (cmd)
1566 command = wstrdup(PLGetString(cmd));
1567 else
1568 command = NULL;
1570 if (!command || strcmp(command, "-")==0) {
1571 if (command)
1572 free(command);
1573 if (wclass)
1574 free(wclass);
1575 if (winstance)
1576 free(winstance);
1578 return NULL;
1581 aicon = wAppIconCreateForDock(scr, command, winstance, wclass,
1582 TILE_NORMAL);
1583 if (wclass)
1584 free(wclass);
1585 if (winstance)
1586 free(winstance);
1588 aicon->icon->core->descriptor.handle_mousedown = iconMouseDown;
1589 if (type == WM_CLIP) {
1590 aicon->icon->core->descriptor.handle_enternotify = clipEnterNotify;
1591 aicon->icon->core->descriptor.handle_leavenotify = clipLeaveNotify;
1593 aicon->icon->core->descriptor.parent_type = WCLASS_DOCK_ICON;
1594 aicon->icon->core->descriptor.parent = aicon;
1597 #ifdef OFFIX_DND
1598 cmd = PLGetDictionaryEntry(info, dDropCommand);
1599 if (cmd)
1600 aicon->dnd_command = wstrdup(PLGetString(cmd));
1601 #endif
1603 /* check auto launch */
1604 value = PLGetDictionaryEntry(info, dAutoLaunch);
1606 aicon->auto_launch = getBooleanDockValue(value, dAutoLaunch);
1608 /* check lock */
1609 value = PLGetDictionaryEntry(info, dLock);
1611 aicon->lock = getBooleanDockValue(value, dLock);
1613 /* check if it wasn't normally docked */
1614 value = PLGetDictionaryEntry(info, dForced);
1616 aicon->forced_dock = getBooleanDockValue(value, dForced);
1618 /* check if we can rely on the stuff in the app */
1619 value = PLGetDictionaryEntry(info, dBuggyApplication);
1621 aicon->buggy_app = getBooleanDockValue(value, dBuggyApplication);
1623 /* get position in the dock */
1624 value = PLGetDictionaryEntry(info, dPosition);
1625 if (value && PLIsString(value)) {
1626 if (sscanf(PLGetString(value), "%hi,%hi", &aicon->xindex,
1627 &aicon->yindex)!=2)
1628 wwarning(_("bad value in docked icon state info %s"),
1629 PLGetString(dPosition));
1631 /* check position sanity */
1632 /* incomplete section! */
1633 if (type == WM_DOCK) {
1634 aicon->xindex = 0;
1635 if (aicon->yindex < 0)
1636 wwarning(_("bad value in docked icon position %i,%i"),
1637 aicon->xindex, aicon->yindex);
1639 } else {
1640 aicon->yindex = index;
1641 aicon->xindex = 0;
1644 /* check if icon is omnipresent */
1645 value = PLGetDictionaryEntry(info, dOmnipresent);
1647 aicon->omnipresent = getBooleanDockValue(value, dOmnipresent);
1649 aicon->running = 0;
1650 aicon->docked = 1;
1652 return aicon;
1656 #define COMPLAIN(key) wwarning(_("bad value in dock state info:%s"), key)
1659 WAppIcon*
1660 wClipRestoreState(WScreen *scr, proplist_t clip_state)
1662 WAppIcon *icon;
1663 proplist_t value;
1666 icon = mainIconCreate(scr, WM_CLIP);
1668 if (!clip_state)
1669 return icon;
1670 else
1671 PLRetain(clip_state);
1673 /* restore position */
1675 value = PLGetDictionaryEntry(clip_state, dPosition);
1677 if (value) {
1678 if (!PLIsString(value))
1679 COMPLAIN("Position");
1680 else {
1681 if (sscanf(PLGetString(value), "%i,%i", &icon->x_pos,
1682 &icon->y_pos)!=2)
1683 COMPLAIN("Position");
1685 /* check position sanity */
1686 if (icon->y_pos < 0)
1687 icon->y_pos = 0;
1688 else if (icon->y_pos > scr->scr_height-ICON_SIZE)
1689 icon->y_pos = scr->scr_height-ICON_SIZE;
1691 if (icon->x_pos < 0)
1692 icon->x_pos = 0;
1693 else if (icon->x_pos > scr->scr_width-ICON_SIZE)
1694 icon->x_pos = scr->scr_width-ICON_SIZE;
1698 #ifdef OFFIX_DND
1699 value = PLGetDictionaryEntry(clip_state, dDropCommand);
1700 if (value && PLIsString(value))
1701 icon->dnd_command = wstrdup(PLGetString(value));
1702 #endif
1704 PLRelease(clip_state);
1706 return icon;
1710 WDock*
1711 wDockRestoreState(WScreen *scr, proplist_t dock_state, int type)
1713 WDock *dock;
1714 proplist_t apps;
1715 proplist_t value;
1716 WAppIcon *aicon, *old_top;
1717 int count, i;
1720 dock = wDockCreate(scr, type);
1722 if (!dock_state)
1723 return dock;
1725 if (dock_state)
1726 PLRetain(dock_state);
1729 /* restore position */
1731 value = PLGetDictionaryEntry(dock_state, dPosition);
1733 if (value) {
1734 if (!PLIsString(value))
1735 COMPLAIN("Position");
1736 else {
1737 if (sscanf(PLGetString(value), "%i,%i", &dock->x_pos,
1738 &dock->y_pos)!=2)
1739 COMPLAIN("Position");
1741 /* check position sanity */
1742 if (dock->y_pos < 0)
1743 dock->y_pos = 0;
1744 else if (dock->y_pos > scr->scr_height-ICON_SIZE)
1745 dock->y_pos = scr->scr_height - ICON_SIZE;
1747 /* This is no more needed. ??? */
1748 if (type == WM_CLIP) {
1749 if (dock->x_pos < 0)
1750 dock->x_pos = 0;
1751 else if (dock->x_pos > scr->scr_width-ICON_SIZE)
1752 dock->x_pos = scr->scr_width-ICON_SIZE;
1753 } else {
1754 if (dock->x_pos >= 0) {
1755 dock->x_pos = DOCK_EXTRA_SPACE;
1756 dock->on_right_side = 0;
1757 } else {
1758 dock->x_pos = scr->scr_width - DOCK_EXTRA_SPACE - ICON_SIZE;
1759 dock->on_right_side = 1;
1765 /* restore lowered/raised state */
1767 dock->lowered = 0;
1769 value = PLGetDictionaryEntry(dock_state, dLowered);
1771 if (value) {
1772 if (!PLIsString(value))
1773 COMPLAIN("Lowered");
1774 else {
1775 if (strcasecmp(PLGetString(value), "YES")==0)
1776 dock->lowered = 1;
1781 /* restore collapsed state */
1783 dock->collapsed = 0;
1785 value = PLGetDictionaryEntry(dock_state, dCollapsed);
1787 if (value) {
1788 if (!PLIsString(value))
1789 COMPLAIN("Collapsed");
1790 else {
1791 if (strcasecmp(PLGetString(value), "YES")==0)
1792 dock->collapsed = 1;
1797 /* restore auto-collapsed state */
1799 value = PLGetDictionaryEntry(dock_state, dAutoCollapse);
1801 if (value) {
1802 if (!PLIsString(value))
1803 COMPLAIN("AutoCollapse");
1804 else {
1805 if (strcasecmp(PLGetString(value), "YES")==0) {
1806 dock->auto_collapse = 1;
1807 dock->collapsed = 1;
1813 /* restore auto-raise/lower state */
1815 value = PLGetDictionaryEntry(dock_state, dAutoRaiseLower);
1817 if (value) {
1818 if (!PLIsString(value))
1819 COMPLAIN("AutoRaiseLower");
1820 else {
1821 if (strcasecmp(PLGetString(value), "YES")==0) {
1822 dock->auto_raise_lower = 1;
1827 /* restore attract icons state */
1829 dock->attract_icons = 0;
1831 value = PLGetDictionaryEntry(dock_state, dAutoAttractIcons);
1833 if (value) {
1834 if (!PLIsString(value))
1835 COMPLAIN("AutoAttractIcons");
1836 else {
1837 if (strcasecmp(PLGetString(value), "YES")==0)
1838 dock->attract_icons = 1;
1843 /* application list */
1846 proplist_t tmp;
1847 char buffer[64];
1850 * When saving, it saves the dock state in
1851 * Applications and Applicationsnnn
1853 * When loading, it will first try Applicationsnnn.
1854 * If it does not exist, use Applications as default.
1857 sprintf(buffer, "Applications%i", scr->scr_height);
1859 tmp = PLMakeString(buffer);
1860 apps = PLGetDictionaryEntry(dock_state, tmp);
1861 PLRelease(tmp);
1863 if (!apps) {
1864 apps = PLGetDictionaryEntry(dock_state, dApplications);
1868 if (!apps) {
1869 goto finish;
1872 count = PLGetNumberOfElements(apps);
1874 if (count==0)
1875 goto finish;
1877 old_top = dock->icon_array[0];
1879 /* dock->icon_count is set to 1 when dock is created.
1880 * Since Clip is already restored, we want to keep it so for clip,
1881 * but for dock we may change the default top tile, so we set it to 0.
1883 if (type == WM_DOCK)
1884 dock->icon_count = 0;
1886 for (i=0; i<count; i++) {
1887 if (dock->icon_count >= dock->max_icons) {
1888 wwarning(_("there are too many icons stored in dock. Ignoring what doesn't fit"));
1889 break;
1892 value = PLGetArrayElement(apps, i);
1893 aicon = restore_icon_state(scr, value, type, dock->icon_count);
1895 dock->icon_array[dock->icon_count] = aicon;
1897 if (aicon) {
1898 aicon->dock = dock;
1899 aicon->x_pos = dock->x_pos + (aicon->xindex*ICON_SIZE);
1900 aicon->y_pos = dock->y_pos + (aicon->yindex*ICON_SIZE);
1902 if (dock->lowered)
1903 ChangeStackingLevel(aicon->icon->core, WMNormalLevel);
1904 else
1905 ChangeStackingLevel(aicon->icon->core, WMDockLevel);
1907 wCoreConfigure(aicon->icon->core, aicon->x_pos, aicon->y_pos,
1908 0, 0);
1910 if (!dock->collapsed)
1911 XMapWindow(dpy, aicon->icon->core->window);
1912 wRaiseFrame(aicon->icon->core);
1914 dock->icon_count++;
1915 } else if (dock->icon_count==0 && type==WM_DOCK)
1916 dock->icon_count++;
1919 /* if the first icon is not defined, use the default */
1920 if (dock->icon_array[0]==NULL) {
1921 /* update default icon */
1922 old_top->x_pos = dock->x_pos;
1923 old_top->y_pos = dock->y_pos;
1924 if (dock->lowered)
1925 ChangeStackingLevel(old_top->icon->core, WMNormalLevel);
1926 else
1927 ChangeStackingLevel(old_top->icon->core, WMDockLevel);
1928 dock->icon_array[0] = old_top;
1929 XMoveWindow(dpy, old_top->icon->core->window, dock->x_pos, dock->y_pos);
1930 /* we don't need to increment dock->icon_count here because it was
1931 * incremented in the loop above.
1933 } else if (old_top!=dock->icon_array[0]) {
1934 if (old_top == scr->clip_icon)
1935 scr->clip_icon = dock->icon_array[0];
1936 wAppIconDestroy(old_top);
1939 finish:
1940 if (dock_state)
1941 PLRelease(dock_state);
1943 return dock;
1948 void
1949 wDockLaunchWithState(WDock *dock, WAppIcon *btn, WSavedState *state)
1951 if (btn && btn->command && !btn->running && !btn->launching) {
1953 btn->drop_launch = 0;
1955 btn->pid = execCommand(btn, btn->command, state);
1957 if (btn->pid>0) {
1958 if (!btn->forced_dock && !btn->buggy_app) {
1959 btn->launching = 1;
1960 dockIconPaint(btn);
1963 } else {
1964 free(state);
1969 void
1970 wDockDoAutoLaunch(WDock *dock, int workspace)
1972 WAppIcon *btn;
1973 WSavedState *state;
1974 int i;
1976 for (i = 0; i < dock->max_icons; i++) {
1977 btn = dock->icon_array[i];
1978 if (!btn || !btn->auto_launch)
1979 continue;
1981 state = wmalloc(sizeof(WSavedState));
1982 memset(state, 0, sizeof(WSavedState));
1983 state->workspace = workspace;
1984 /* TODO: this is klugy and is very difficult to understand
1985 * what's going on. Try to clean up */
1986 wDockLaunchWithState(dock, btn, state);
1990 #ifdef REDUCE_APPICONS
1991 void
1992 wDockSimulateLaunch(WDock *dock, WAppIcon *btn)
1994 if ((btn == NULL) || (dock == NULL))
1995 return;
1997 if (!btn->running) {
1998 if ((btn->icon->owner == NULL) && (btn->applist))
1999 btn->icon->owner = btn->applist->wapp->main_window_desc;
2000 if (!btn->forced_dock)
2001 btn->launching = 1;
2002 dockIconPaint(btn);
2003 wusleep(5000);
2006 #endif
2008 #ifdef OFFIX_DND
2009 static WDock*
2010 findDock(WScreen *scr, XEvent *event, int *icon_pos)
2012 WDock *dock;
2013 int i;
2015 *icon_pos = -1;
2016 if ((dock = scr->dock)!=NULL) {
2017 for (i=0; i<dock->max_icons; i++) {
2018 if (dock->icon_array[i]
2019 && dock->icon_array[i]->icon->core->window==event->xclient.window) {
2020 *icon_pos = i;
2021 break;
2025 if (*icon_pos<0 && (dock = scr->workspaces[scr->current_workspace]->clip)!=NULL) {
2026 for (i=0; i<dock->max_icons; i++) {
2027 if (dock->icon_array[i]
2028 && dock->icon_array[i]->icon->core->window==event->xclient.window) {
2029 *icon_pos = i;
2030 break;
2034 if(*icon_pos>=0)
2035 return dock;
2036 return NULL;
2041 wDockReceiveDNDDrop(WScreen *scr, XEvent *event)
2043 WDock *dock;
2044 WAppIcon *btn;
2045 int icon_pos;
2047 dock = findDock(scr, event, &icon_pos);
2048 if (!dock)
2049 return False;
2052 * Return True if the drop was on an application icon window.
2053 * In this case, let the ClientMessage handler redirect the
2054 * message to the app.
2056 if (dock->icon_array[icon_pos]->icon->icon_win!=None)
2057 return True;
2059 if (dock->icon_array[icon_pos]->dnd_command!=NULL) {
2060 scr->flags.dnd_data_convertion_status = 0;
2062 btn = dock->icon_array[icon_pos];
2064 if (!btn->forced_dock) {
2065 btn->relaunching = btn->running;
2066 btn->running = 1;
2068 if (btn->wm_instance || btn->wm_class) {
2069 WWindowAttributes attr;
2070 memset(&attr, 0, sizeof(WWindowAttributes));
2071 wDefaultFillAttributes(btn->icon->core->screen_ptr,
2072 btn->wm_instance,
2073 btn->wm_class, &attr, NULL, True);
2075 if (!attr.no_appicon)
2076 btn->launching = 1;
2077 else
2078 btn->running = 0;
2081 btn->drop_launch = 1;
2082 scr->last_dock = dock;
2083 btn->pid = execCommand(btn, btn->dnd_command, NULL);
2084 if (btn->pid>0) {
2085 dockIconPaint(btn);
2086 } else {
2087 btn->launching = 0;
2088 if (!btn->relaunching) {
2089 btn->running = 0;
2093 return False;
2095 #endif /* OFFIX_DND */
2099 Bool
2100 wDockAttachIcon(WDock *dock, WAppIcon *icon, int x, int y)
2102 WWindow *wwin;
2103 char **argv;
2104 int argc;
2105 int index;
2107 wwin = icon->icon->owner;
2108 if (icon->command==NULL) {
2109 icon->editing = 0;
2110 if (XGetCommand(dpy, wwin->client_win, &argv, &argc) && argc>0) {
2112 icon->command = FlattenStringList(argv, argc);
2113 XFreeStringList(argv);
2114 } else {
2115 char *command=NULL;
2117 /* icon->forced_dock = 1;*/
2118 if (dock->type!=WM_CLIP || !icon->attracted) {
2119 icon->editing = 1;
2120 if (wInputDialog(dock->screen_ptr, _("Dock Icon"),
2121 _("Type the command used to launch the application"),
2122 &command)) {
2123 if (command && (command[0]==0 ||
2124 (command[0]=='-' && command[1]==0))) {
2125 free(command);
2126 command = NULL;
2128 icon->command = command;
2129 icon->editing = 0;
2130 } else {
2131 icon->editing = 0;
2132 if (command)
2133 free(command);
2134 /* If the target is the dock, reject the icon. If
2135 * the target is the clip, make it an attracted icon
2137 if (dock->type==WM_CLIP) {
2138 icon->attracted = 1;
2139 if (!icon->icon->shadowed) {
2140 icon->icon->shadowed = 1;
2141 icon->icon->force_paint = 1;
2143 } else {
2144 return False;
2149 } else {
2150 icon->editing = 0;
2153 for (index=1; index<dock->max_icons; index++)
2154 if (dock->icon_array[index] == NULL)
2155 break;
2156 /* if (index == dock->max_icons)
2157 return; */
2159 assert(index < dock->max_icons);
2161 dock->icon_array[index] = icon;
2162 icon->yindex = y;
2163 icon->xindex = x;
2165 icon->omnipresent = 0;
2167 icon->x_pos = dock->x_pos + x*ICON_SIZE;
2168 icon->y_pos = dock->y_pos + y*ICON_SIZE;
2170 dock->icon_count++;
2172 icon->running = 1;
2173 icon->launching = 0;
2174 icon->docked = 1;
2175 icon->dock = dock;
2176 icon->icon->core->descriptor.handle_mousedown = iconMouseDown;
2177 if (dock->type == WM_CLIP) {
2178 icon->icon->core->descriptor.handle_enternotify = clipEnterNotify;
2179 icon->icon->core->descriptor.handle_leavenotify = clipLeaveNotify;
2181 icon->icon->core->descriptor.parent_type = WCLASS_DOCK_ICON;
2182 icon->icon->core->descriptor.parent = icon;
2184 MoveInStackListUnder(dock->icon_array[index-1]->icon->core,
2185 icon->icon->core);
2186 wAppIconMove(icon, icon->x_pos, icon->y_pos);
2187 wAppIconPaint(icon);
2189 if (wPreferences.auto_arrange_icons)
2190 wArrangeIcons(dock->screen_ptr, True);
2192 #ifdef OFFIX_DND
2193 if (icon->command && !icon->dnd_command) {
2194 icon->dnd_command = wmalloc(strlen(icon->command)+8);
2195 sprintf(icon->dnd_command, "%s %%d", icon->command);
2197 #endif
2199 return True;
2203 void
2204 reattachIcon(WDock *dock, WAppIcon *icon, int x, int y)
2206 int index;
2208 for(index=1; index<dock->max_icons; index++) {
2209 if(dock->icon_array[index] == icon)
2210 break;
2212 assert(index < dock->max_icons);
2214 icon->yindex = y;
2215 icon->xindex = x;
2217 icon->x_pos = dock->x_pos + x*ICON_SIZE;
2218 icon->y_pos = dock->y_pos + y*ICON_SIZE;
2222 Bool
2223 moveIconBetweenDocks(WDock *src, WDock *dest, WAppIcon *icon, int x, int y)
2225 WWindow *wwin;
2226 char **argv;
2227 int argc;
2228 int index;
2230 if (src == dest)
2231 return True; /* No move needed, we're already there */
2233 if (dest == NULL)
2234 return False;
2236 wwin = icon->icon->owner;
2239 * For the moment we can't do this if we move icons in Clip from one
2240 * workspace to other, because if we move two or more icons without
2241 * command, the dialog box will not be able to tell us to which of the
2242 * moved icons it applies. -Dan
2244 if ((dest->type==WM_DOCK /*|| dest->keep_attracted*/) && icon->command==NULL) {
2245 if (XGetCommand(dpy, wwin->client_win, &argv, &argc) && argc>0) {
2247 icon->command = FlattenStringList(argv, argc);
2248 XFreeStringList(argv);
2249 } else {
2250 char *command=NULL;
2252 icon->editing = 1;
2253 /* icon->forced_dock = 1;*/
2254 if (wInputDialog(src->screen_ptr, _("Dock Icon"),
2255 _("Type the command used to launch the application"),
2256 &command)) {
2257 if (command && (command[0]==0 ||
2258 (command[0]=='-' && command[1]==0))) {
2259 free(command);
2260 command = NULL;
2262 icon->command = command;
2263 } else {
2264 icon->editing = 0;
2265 if (command)
2266 free(command);
2267 return False;
2269 icon->editing = 0;
2273 if (dest->type == WM_DOCK)
2274 wClipMakeIconOmnipresent(icon, False);
2276 for(index=1; index<src->max_icons; index++) {
2277 if(src->icon_array[index] == icon)
2278 break;
2280 assert(index < src->max_icons);
2282 src->icon_array[index] = NULL;
2283 src->icon_count--;
2285 for(index=1; index<dest->max_icons; index++) {
2286 if(dest->icon_array[index] == NULL)
2287 break;
2289 /* if (index == dest->max_icons)
2290 return; */
2292 assert(index < dest->max_icons);
2294 dest->icon_array[index] = icon;
2295 icon->dock = dest;
2297 /* deselect the icon */
2298 if (icon->icon->selected)
2299 wIconSelect(icon->icon);
2301 if (dest->type == WM_DOCK) {
2302 icon->icon->core->descriptor.handle_enternotify = NULL;
2303 icon->icon->core->descriptor.handle_leavenotify = NULL;
2304 } else {
2305 icon->icon->core->descriptor.handle_enternotify = clipEnterNotify;
2306 icon->icon->core->descriptor.handle_leavenotify = clipLeaveNotify;
2309 /* set it to be kept when moving to dock.
2310 * Unless the icon does not have a command set
2312 if (icon->command && dest->type==WM_DOCK) {
2313 icon->attracted = 0;
2314 if (icon->icon->shadowed) {
2315 icon->icon->shadowed = 0;
2316 icon->icon->force_paint = 1;
2320 if (src->auto_collapse || src->auto_raise_lower)
2321 clipLeave(src);
2323 icon->yindex = y;
2324 icon->xindex = x;
2326 icon->x_pos = dest->x_pos + x*ICON_SIZE;
2327 icon->y_pos = dest->y_pos + y*ICON_SIZE;
2329 dest->icon_count++;
2331 MoveInStackListUnder(dest->icon_array[index-1]->icon->core,
2332 icon->icon->core);
2333 wAppIconPaint(icon);
2335 return True;
2339 void
2340 wDockDetach(WDock *dock, WAppIcon *icon)
2342 int index;
2344 /* make the settings panel be closed */
2345 if (icon->panel) {
2346 DestroyDockAppSettingsPanel(icon->panel);
2349 /* This must be called before icon->dock is set to NULL.
2350 * Don't move it. -Dan
2352 wClipMakeIconOmnipresent(icon, False);
2354 icon->docked = 0;
2355 icon->dock = NULL;
2356 icon->attracted = 0;
2357 icon->auto_launch = 0;
2358 if (icon->icon->shadowed) {
2359 icon->icon->shadowed = 0;
2360 icon->icon->force_paint = 1;
2363 /* deselect the icon */
2364 if (icon->icon->selected)
2365 wIconSelect(icon->icon);
2367 if (icon->command) {
2368 free(icon->command);
2369 icon->command = NULL;
2371 #ifdef OFFIX_DND
2372 if (icon->dnd_command) {
2373 free(icon->dnd_command);
2374 icon->dnd_command = NULL;
2376 #endif
2378 for (index=1; index<dock->max_icons; index++)
2379 if (dock->icon_array[index] == icon)
2380 break;
2381 assert(index < dock->max_icons);
2382 dock->icon_array[index] = NULL;
2383 icon->yindex = -1;
2384 icon->xindex = -1;
2386 dock->icon_count--;
2388 /* if the dock is not attached to an application or
2389 * the the application did not set the approriate hints yet,
2390 * destroy the icon */
2391 #ifdef REDUCE_APPICONS
2392 if ((icon->num_apps == 0) && (!icon->running || !wApplicationOf(icon->main_window)) )
2393 #else
2394 if (!icon->running || !wApplicationOf(icon->main_window))
2395 #endif
2396 wAppIconDestroy(icon);
2397 else {
2398 icon->icon->core->descriptor.handle_mousedown = appIconMouseDown;
2399 icon->icon->core->descriptor.handle_enternotify = NULL;
2400 icon->icon->core->descriptor.handle_leavenotify = NULL;
2401 icon->icon->core->descriptor.parent_type = WCLASS_APPICON;
2402 icon->icon->core->descriptor.parent = icon;
2404 ChangeStackingLevel(icon->icon->core, NORMAL_ICON_LEVEL);
2406 wAppIconPaint(icon);
2407 if (wPreferences.auto_arrange_icons) {
2408 wArrangeIcons(dock->screen_ptr, True);
2411 if (dock->auto_collapse || dock->auto_raise_lower)
2412 clipLeave(dock);
2417 * returns the closest Dock slot index for the passed
2418 * coordinates.
2420 * Returns False if icon can't be docked.
2422 * Note: this function should NEVER alter ret_x or ret_y, unless it will
2423 * return True. -Dan
2425 Bool
2426 wDockSnapIcon(WDock *dock, WAppIcon *icon, int req_x, int req_y,
2427 int *ret_x, int *ret_y, int redocking)
2429 WScreen *scr = dock->screen_ptr;
2430 int dx, dy;
2431 int ex_x, ex_y;
2432 int i, offset = ICON_SIZE/2;
2433 WAppIcon *aicon = NULL;
2434 WAppIcon *nicon = NULL;
2435 int max_y_icons, max_x_icons;
2437 max_x_icons = scr->scr_width/ICON_SIZE;
2438 max_y_icons = scr->scr_height/ICON_SIZE-1;
2440 if (wPreferences.flags.noupdates)
2441 return False;
2443 dx = dock->x_pos;
2444 dy = dock->y_pos;
2446 /* if the dock is full */
2447 if (!redocking &&
2448 (dock->icon_count >= dock->max_icons)) {
2449 return False;
2452 /* exact position */
2453 if (req_y < dy)
2454 ex_y = (req_y - offset - dy)/ICON_SIZE;
2455 else
2456 ex_y = (req_y + offset - dy)/ICON_SIZE;
2458 if (req_x < dx)
2459 ex_x = (req_x - offset - dx)/ICON_SIZE;
2460 else
2461 ex_x = (req_x + offset - dx)/ICON_SIZE;
2463 /* check if the icon is outside the screen boundaries */
2464 if (dx + ex_x*ICON_SIZE < -ICON_SIZE+2 ||
2465 dx + ex_x*ICON_SIZE >= scr->scr_width-1 ||
2466 dy + ex_y*ICON_SIZE < -ICON_SIZE+2 ||
2467 dy + ex_y*ICON_SIZE >= scr->scr_height-1)
2468 return False;
2470 if (dock->type == WM_DOCK) {
2471 if (icon->dock != dock && ex_x != 0)
2472 return False;
2474 aicon = NULL;
2475 for (i=0; i<dock->max_icons; i++) {
2476 nicon = dock->icon_array[i];
2477 if (nicon && nicon->yindex == ex_y) {
2478 aicon = nicon;
2479 break;
2483 if (redocking) {
2484 int sig, done, closest;
2486 /* Possible cases when redocking:
2488 * icon dragged out of range of any slot -> false
2489 * icon dragged to range of free slot
2490 * icon dragged to range of same slot
2491 * icon dragged to range of different icon
2493 if (abs(ex_x) > DOCK_DETTACH_THRESHOLD)
2494 return False;
2496 if (ex_y>=0 && ex_y<=max_y_icons && (aicon==icon || !aicon)) {
2497 *ret_x = 0;
2498 *ret_y = ex_y;
2499 return True;
2502 /* start looking at the upper slot or lower? */
2503 if (ex_y*ICON_SIZE < (req_y + offset - dy))
2504 sig = 1;
2505 else
2506 sig = -1;
2508 closest = -1;
2509 done = 0;
2510 /* look for closest free slot */
2511 for (i=0; i<(DOCK_DETTACH_THRESHOLD+1)*2 && !done; i++) {
2512 int j;
2514 done = 1;
2515 closest = sig*(i/2) + ex_y;
2516 /* check if this slot is used */
2517 if (closest >= 0) {
2518 for (j = 0; j<dock->max_icons; j++) {
2519 if (dock->icon_array[j]
2520 && dock->icon_array[j]->yindex==closest) {
2521 /* slot is used by someone else */
2522 if (dock->icon_array[j]!=icon)
2523 done = 0;
2524 break;
2528 sig = -sig;
2530 if (done && closest >= 0 && closest <= max_y_icons &&
2531 ((ex_y >= closest && ex_y - closest < DOCK_DETTACH_THRESHOLD+1)
2533 (ex_y < closest && closest - ex_y <= DOCK_DETTACH_THRESHOLD+1))) {
2534 *ret_x = 0;
2535 *ret_y = closest;
2536 return True;
2538 } else { /* !redocking */
2540 /* if slot is free and the icon is close enough, return it */
2541 if (!aicon && ex_x == 0 && ex_y >= 0 && ex_y <= max_y_icons) {
2542 *ret_x = 0;
2543 *ret_y = ex_y;
2544 return True;
2547 } else { /* CLIP */
2548 int neighbours = 0;
2549 int start, stop, k;
2551 start = icon->omnipresent ? 0 : scr->current_workspace;
2552 stop = icon->omnipresent ? scr->workspace_count : start+1;
2554 aicon = NULL;
2555 for (k=start; k<stop; k++) {
2556 WDock *tmp = scr->workspaces[k]->clip;
2557 if (!tmp)
2558 continue;
2559 for (i=0; i<tmp->max_icons; i++) {
2560 nicon = tmp->icon_array[i];
2561 if (nicon && nicon->xindex == ex_x && nicon->yindex == ex_y) {
2562 aicon = nicon;
2563 break;
2566 if (aicon)
2567 break;
2569 for (k=start; k<stop; k++) {
2570 WDock *tmp = scr->workspaces[k]->clip;
2571 if (!tmp)
2572 continue;
2573 for (i=0; i<tmp->max_icons; i++) {
2574 nicon = tmp->icon_array[i];
2575 if (nicon && nicon != icon && /* Icon can't be it's own neighbour */
2576 (abs(nicon->xindex - ex_x) <= CLIP_ATTACH_VICINITY &&
2577 abs(nicon->yindex - ex_y) <= CLIP_ATTACH_VICINITY)) {
2578 neighbours = 1;
2579 break;
2582 if (neighbours)
2583 break;
2586 if (neighbours && (aicon==NULL || (redocking && aicon == icon))) {
2587 *ret_x = ex_x;
2588 *ret_y = ex_y;
2589 return True;
2592 return False;
2595 #define MIN(x, y) ((x) > (y) ? (y) : (x))
2596 #define MAX(x, y) ((x) < (y) ? (y) : (x))
2598 #define ON_SCREEN(x, y, sx, ex, sy, ey) \
2599 ((((x)+ICON_SIZE/2) >= (sx)) && (((y)+ICON_SIZE/2) >= (sy)) && \
2600 (((x) + (ICON_SIZE/2)) <= (ex)) && (((y) + (ICON_SIZE/2)) <= ey))
2604 * returns true if it can find a free slot in the dock,
2605 * in which case it changes x_pos and y_pos accordingly.
2606 * Else returns false.
2608 Bool
2609 wDockFindFreeSlot(WDock *dock, int *x_pos, int *y_pos)
2611 WScreen *scr = dock->screen_ptr;
2612 WAppIcon *btn;
2613 WAppIconChain *chain;
2614 unsigned char *slot_map;
2615 int mwidth;
2616 int r;
2617 int x, y;
2618 int i, done = False;
2619 int corner;
2620 int sx=0, sy=0, ex=scr->scr_width, ey=scr->scr_height;
2621 int extra_count=0;
2623 if (dock->type == WM_CLIP &&
2624 dock != scr->workspaces[scr->current_workspace]->clip)
2625 extra_count = scr->global_icon_count;
2627 /* if the dock is full */
2628 if (dock->icon_count+extra_count >= dock->max_icons) {
2629 return False;
2632 if (!wPreferences.flags.nodock && scr->dock) {
2633 if (scr->dock->on_right_side)
2634 ex -= ICON_SIZE + DOCK_EXTRA_SPACE;
2635 else
2636 sx += ICON_SIZE + DOCK_EXTRA_SPACE;
2639 if (ex < dock->x_pos)
2640 ex = dock->x_pos;
2641 if (sx > dock->x_pos+ICON_SIZE)
2642 sx = dock->x_pos+ICON_SIZE;
2643 #define C_NONE 0
2644 #define C_NW 1
2645 #define C_NE 2
2646 #define C_SW 3
2647 #define C_SE 4
2649 /* check if clip is in a corner */
2650 if (dock->type==WM_CLIP) {
2651 if (dock->x_pos < 1 && dock->y_pos < 1)
2652 corner = C_NE;
2653 else if (dock->x_pos < 1 && dock->y_pos >= (ey-ICON_SIZE))
2654 corner = C_SE;
2655 else if (dock->x_pos >= (ex-ICON_SIZE)&& dock->y_pos >= (ey-ICON_SIZE))
2656 corner = C_SW;
2657 else if (dock->x_pos >= (ex-ICON_SIZE) && dock->y_pos < 1)
2658 corner = C_NW;
2659 else
2660 corner = C_NONE;
2661 } else
2662 corner = C_NONE;
2664 /* If the clip is in the corner, use only slots that are in the border
2665 * of the screen */
2666 if (corner!=C_NONE) {
2667 char *hmap, *vmap;
2668 int hcount, vcount;
2670 hcount = MIN(dock->max_icons, scr->scr_width/ICON_SIZE);
2671 vcount = MIN(dock->max_icons, scr->scr_height/ICON_SIZE);
2672 hmap = wmalloc(hcount+1);
2673 memset(hmap, 0, hcount+1);
2674 vmap = wmalloc(vcount+1);
2675 memset(vmap, 0, vcount+1);
2677 /* mark used positions */
2678 switch (corner) {
2679 case C_NE:
2680 for (i=0; i<dock->max_icons; i++) {
2681 btn = dock->icon_array[i];
2682 if (!btn)
2683 continue;
2685 if (btn->xindex==0 && btn->yindex > 0 && btn->yindex < vcount)
2686 vmap[btn->yindex] = 1;
2687 else if (btn->yindex==0 && btn->xindex>0 && btn->xindex<hcount)
2688 hmap[btn->xindex] = 1;
2690 for (chain=scr->global_icons; chain!=NULL; chain=chain->next) {
2691 btn = chain->aicon;
2692 if (btn->xindex==0 && btn->yindex > 0 && btn->yindex < vcount)
2693 vmap[btn->yindex] = 1;
2694 else if (btn->yindex==0 && btn->xindex>0 && btn->xindex<hcount)
2695 hmap[btn->xindex] = 1;
2697 break;
2698 case C_NW:
2699 for (i=0; i<dock->max_icons; i++) {
2700 btn = dock->icon_array[i];
2701 if (!btn)
2702 continue;
2704 if (btn->xindex==0 && btn->yindex > 0 && btn->yindex < vcount)
2705 vmap[btn->yindex] = 1;
2706 else if (btn->yindex==0 && btn->xindex<0 &&btn->xindex>-hcount)
2707 hmap[-btn->xindex] = 1;
2709 for (chain=scr->global_icons; chain!=NULL; chain=chain->next) {
2710 btn = chain->aicon;
2711 if (btn->xindex==0 && btn->yindex > 0 && btn->yindex < vcount)
2712 vmap[btn->yindex] = 1;
2713 else if (btn->yindex==0 && btn->xindex<0 &&btn->xindex>-hcount)
2714 hmap[-btn->xindex] = 1;
2716 break;
2717 case C_SE:
2718 for (i=0; i<dock->max_icons; i++) {
2719 btn = dock->icon_array[i];
2720 if (!btn)
2721 continue;
2723 if (btn->xindex==0 && btn->yindex < 0 && btn->yindex > -vcount)
2724 vmap[-btn->yindex] = 1;
2725 else if (btn->yindex==0 && btn->xindex>0 && btn->xindex<hcount)
2726 hmap[btn->xindex] = 1;
2728 for (chain=scr->global_icons; chain!=NULL; chain=chain->next) {
2729 btn = chain->aicon;
2730 if (btn->xindex==0 && btn->yindex < 0 && btn->yindex > -vcount)
2731 vmap[-btn->yindex] = 1;
2732 else if (btn->yindex==0 && btn->xindex>0 && btn->xindex<hcount)
2733 hmap[btn->xindex] = 1;
2735 break;
2736 case C_SW:
2737 default:
2738 for (i=0; i<dock->max_icons; i++) {
2739 btn = dock->icon_array[i];
2740 if (!btn)
2741 continue;
2743 if (btn->xindex==0 && btn->yindex < 0 && btn->yindex > -vcount)
2744 vmap[-btn->yindex] = 1;
2745 else if (btn->yindex==0 && btn->xindex<0 &&btn->xindex>-hcount)
2746 hmap[-btn->xindex] = 1;
2748 for (chain=scr->global_icons; chain!=NULL; chain=chain->next) {
2749 btn = chain->aicon;
2750 if (btn->xindex==0 && btn->yindex < 0 && btn->yindex > -vcount)
2751 vmap[-btn->yindex] = 1;
2752 else if (btn->yindex==0 && btn->xindex<0 &&btn->xindex>-hcount)
2753 hmap[-btn->xindex] = 1;
2756 x=0; y=0;
2757 done = 0;
2758 /* search a vacant slot */
2759 for (i=1; i<MAX(vcount, hcount); i++) {
2760 if (i < vcount && vmap[i]==0) {
2761 /* found a slot */
2762 x = 0;
2763 y = i;
2764 done = 1;
2765 break;
2766 } else if (i < hcount && hmap[i]==0) {
2767 /* found a slot */
2768 x = i;
2769 y = 0;
2770 done = 1;
2771 break;
2774 free(vmap);
2775 free(hmap);
2776 /* If found a slot, translate and return */
2777 if (done) {
2778 if (corner==C_NW || corner==C_NE) {
2779 *y_pos = y;
2780 } else {
2781 *y_pos = -y;
2783 if (corner==C_NE || corner==C_SE) {
2784 *x_pos = x;
2785 } else {
2786 *x_pos = -x;
2788 return True;
2790 /* else, try to find a slot somewhere else */
2793 /* a map of mwidth x mwidth would be enough if we allowed icons to be
2794 * placed outside of screen */
2795 mwidth = (int)ceil(sqrt(dock->max_icons));
2797 /* In the worst case (the clip is in the corner of the screen),
2798 * the amount of icons that fit in the clip is smaller.
2799 * Double the map to get a safe value.
2801 mwidth += mwidth;
2803 r = (mwidth-1)/2;
2805 slot_map = wmalloc(mwidth*mwidth);
2806 memset(slot_map, 0, mwidth*mwidth);
2808 #define XY2OFS(x,y) (MAX(abs(x),abs(y)) > r) ? 0 : (((y)+r)*(mwidth)+(x)+r)
2810 /* mark used slots in the map. If the slot falls outside the map
2811 * (for example, when all icons are placed in line), ignore them. */
2812 for (i=0; i<dock->max_icons; i++) {
2813 btn = dock->icon_array[i];
2814 if (btn)
2815 slot_map[XY2OFS(btn->xindex, btn->yindex)] = 1;
2817 for (chain=scr->global_icons; chain!=NULL; chain=chain->next) {
2818 slot_map[XY2OFS(chain->aicon->xindex, chain->aicon->yindex)] = 1;
2820 /* Find closest slot from the center that is free by scanning the
2821 * map from the center to outward in circular passes.
2822 * This will not result in a neat layout, but will be optimal
2823 * in the sense that there will not be holes left.
2825 done = 0;
2826 for (i = 1; i <= r && !done; i++) {
2827 int tx, ty;
2829 /* top and bottom parts of the ring */
2830 for (x = -i; x <= i && !done; x++) {
2831 tx = dock->x_pos + x*ICON_SIZE;
2832 y = -i;
2833 ty = dock->y_pos + y*ICON_SIZE;
2834 if (slot_map[XY2OFS(x,y)]==0
2835 && ON_SCREEN(tx, ty, sx, ex, sy, ey)) {
2836 *x_pos = x;
2837 *y_pos = y;
2838 done = 1;
2839 break;
2841 y = i;
2842 ty = dock->y_pos + y*ICON_SIZE;
2843 if (slot_map[XY2OFS(x,y)]==0
2844 && ON_SCREEN(tx, ty, sx, ex, sy, ey)) {
2845 *x_pos = x;
2846 *y_pos = y;
2847 done = 1;
2848 break;
2851 /* left and right parts of the ring */
2852 for (y = -i+1; y <= i-1; y++) {
2853 ty = dock->y_pos + y*ICON_SIZE;
2854 x = -i;
2855 tx = dock->x_pos + x*ICON_SIZE;
2856 if (slot_map[XY2OFS(x,y)]==0
2857 && ON_SCREEN(tx, ty, sx, ex, sy, ey)) {
2858 *x_pos = x;
2859 *y_pos = y;
2860 done = 1;
2861 break;
2863 x = i;
2864 tx = dock->x_pos + x*ICON_SIZE;
2865 if (slot_map[XY2OFS(x,y)]==0
2866 && ON_SCREEN(tx, ty, sx, ex, sy, ey)) {
2867 *x_pos = x;
2868 *y_pos = y;
2869 done = 1;
2870 break;
2874 free(slot_map);
2875 #undef XY2OFS
2876 return done;
2880 static void
2881 moveDock(WDock *dock, int new_x, int new_y)
2883 WAppIcon *btn;
2884 int i;
2886 dock->x_pos = new_x;
2887 dock->y_pos = new_y;
2888 for (i=0; i<dock->max_icons; i++) {
2889 btn = dock->icon_array[i];
2890 if (btn) {
2891 btn->x_pos = new_x + btn->xindex*ICON_SIZE;
2892 btn->y_pos = new_y + btn->yindex*ICON_SIZE;
2893 XMoveWindow(dpy, btn->icon->core->window, btn->x_pos, btn->y_pos);
2899 static void
2900 swapDock(WDock *dock)
2902 WScreen *scr = dock->screen_ptr;
2903 WAppIcon *btn;
2904 int x, i;
2907 if (dock->on_right_side) {
2908 x = dock->x_pos = scr->scr_width - ICON_SIZE - DOCK_EXTRA_SPACE;
2909 } else {
2910 x = dock->x_pos = DOCK_EXTRA_SPACE;
2913 for (i=0; i<dock->max_icons; i++) {
2914 btn = dock->icon_array[i];
2915 if (btn) {
2916 btn->x_pos = x;
2917 XMoveWindow(dpy, btn->icon->core->window, btn->x_pos, btn->y_pos);
2921 wScreenUpdateUsableArea(scr);
2925 static pid_t
2926 execCommand(WAppIcon *btn, char *command, WSavedState *state)
2928 WScreen *scr = btn->icon->core->screen_ptr;
2929 pid_t pid;
2930 char **argv;
2931 int argc;
2932 char *cmdline;
2934 cmdline = ExpandOptions(scr, command);
2936 if (scr->flags.dnd_data_convertion_status || !cmdline) {
2937 if (cmdline)
2938 free(cmdline);
2939 if (state)
2940 free(state);
2941 return 0;
2944 ParseCommand(cmdline, &argv, &argc);
2946 if (argv==NULL) {
2947 if (cmdline)
2948 free(cmdline);
2949 if (state)
2950 free(state);
2951 return 0;
2954 if ((pid=fork())==0) {
2955 char **args;
2956 int i;
2958 SetupEnvironment(scr);
2960 #ifdef HAVE_SETPGID
2961 setpgid(0, 0);
2962 #endif
2964 args = malloc(sizeof(char*)*(argc+1));
2965 if (!args)
2966 exit(111);
2967 for (i=0; i<argc; i++) {
2968 args[i] = argv[i];
2970 args[argc] = NULL;
2971 execvp(argv[0], args);
2972 exit(111);
2974 while (argc > 0)
2975 free(argv[--argc]);
2976 free(argv);
2978 if (pid > 0) {
2979 if (!state) {
2980 state = wmalloc(sizeof(WSavedState));
2981 memset(state, 0, sizeof(WSavedState));
2982 state->hidden = -1;
2983 state->miniaturized = -1;
2984 state->shaded = -1;
2985 if (btn->dock==scr->dock || btn->omnipresent)
2986 state->workspace = -1;
2987 else
2988 state->workspace = scr->current_workspace;
2990 wWindowAddSavedState(btn->wm_instance, btn->wm_class, cmdline, pid,
2991 state);
2992 wAddDeathHandler(pid, (WDeathHandler*)trackDeadProcess,
2993 btn->dock);
2994 } else if (state) {
2995 free(state);
2997 free(cmdline);
2998 return pid;
3002 void
3003 wDockHideIcons(WDock *dock)
3005 int i;
3007 if (dock==NULL)
3008 return;
3010 for (i=1; i<dock->max_icons; i++) {
3011 if (dock->icon_array[i])
3012 XUnmapWindow(dpy, dock->icon_array[i]->icon->core->window);
3014 dock->mapped = 0;
3016 dockIconPaint(dock->icon_array[0]);
3020 void
3021 wDockShowIcons(WDock *dock)
3023 int i, newlevel;
3024 WAppIcon *btn;
3026 if (dock==NULL)
3027 return;
3029 btn = dock->icon_array[0];
3030 moveDock(dock, btn->x_pos, btn->y_pos);
3032 newlevel = dock->lowered ? WMNormalLevel : WMDockLevel;
3033 ChangeStackingLevel(btn->icon->core, newlevel);
3035 for (i=1; i<dock->max_icons; i++) {
3036 if (dock->icon_array[i]) {
3037 MoveInStackListAbove(dock->icon_array[i]->icon->core,
3038 btn->icon->core);
3039 break;
3043 if (!dock->collapsed) {
3044 for (i=1; i<dock->max_icons; i++) {
3045 if (dock->icon_array[i]) {
3046 XMapWindow(dpy, dock->icon_array[i]->icon->core->window);
3050 dock->mapped = 1;
3052 dockIconPaint(btn);
3056 void
3057 wDockLower(WDock *dock)
3059 int i;
3061 for (i=0; i<dock->max_icons; i++) {
3062 if (dock->icon_array[i])
3063 wLowerFrame(dock->icon_array[i]->icon->core);
3068 void
3069 wDockRaise(WDock *dock)
3071 int i;
3073 for (i=dock->max_icons-1; i>=0; i--) {
3074 if (dock->icon_array[i])
3075 wRaiseFrame(dock->icon_array[i]->icon->core);
3080 void
3081 wDockRaiseLower(WDock *dock)
3083 if (!dock->icon_array[0]->icon->core->stacking->above
3084 ||(dock->icon_array[0]->icon->core->stacking->window_level
3085 !=dock->icon_array[0]->icon->core->stacking->above->stacking->window_level))
3086 wDockLower(dock);
3087 else
3088 wDockRaise(dock);
3092 void
3093 wDockFinishLaunch(WDock *dock, WAppIcon *icon)
3095 icon->launching = 0;
3096 icon->relaunching = 0;
3097 dockIconPaint(icon);
3101 WAppIcon*
3102 wDockFindIconForWindow(WDock *dock, Window window)
3104 WAppIcon *icon;
3105 int i;
3107 for (i=0; i<dock->max_icons; i++) {
3108 icon = dock->icon_array[i];
3109 if (icon && icon->main_window == window)
3110 return icon;
3112 return NULL;
3116 void
3117 wDockTrackWindowLaunch(WDock *dock, Window window)
3119 WAppIcon *icon;
3120 #ifdef REDUCE_APPICONS
3121 WAppIconAppList *tapplist;
3122 #endif
3123 char *wm_class, *wm_instance;
3124 int i;
3125 Bool firstPass = True;
3126 Bool found = False;
3127 char *command = NULL;
3130 int argc;
3131 char **argv;
3133 if (XGetCommand(dpy, window, &argv, &argc)) {
3134 if (argc > 0 && argv != NULL)
3135 command = FlattenStringList(argv,argc);
3136 if (argv) {
3137 XFreeStringList(argv);
3142 if (!PropGetWMClass(window, &wm_class, &wm_instance) ||
3143 (!wm_class && !wm_instance))
3144 return;
3146 retry:
3147 for (i=0; i<dock->max_icons; i++) {
3148 icon = dock->icon_array[i];
3149 if (!icon)
3150 continue;
3152 /* app is already attached to icon */
3153 if (icon->main_window == window) {
3154 found = True;
3155 break;
3158 if ((icon->wm_instance || icon->wm_class)
3159 && (icon->launching
3160 || (dock->screen_ptr->flags.startup && !icon->running))) {
3162 if (icon->wm_instance && wm_instance &&
3163 strcmp(icon->wm_instance, wm_instance)!=0) {
3164 continue;
3166 if (icon->wm_class && wm_class &&
3167 strcmp(icon->wm_class, wm_class)!=0) {
3168 continue;
3170 if (firstPass && command && strcmp(icon->command, command)!=0) {
3171 continue;
3174 if (!icon->relaunching) {
3175 WApplication *wapp;
3177 /* Possibly an application that was docked with dockit,
3178 * but the user did not update WMState to indicate that
3179 * it was docked by force */
3180 wapp = wApplicationOf(window);
3181 if (!wapp) {
3182 icon->forced_dock = 1;
3183 icon->running = 0;
3185 if (!icon->forced_dock)
3186 icon->main_window = window;
3188 #ifdef REDUCE_APPICONS
3189 tapplist = wmalloc(sizeof(WAppIconAppList));
3190 memset(tapplist, 0, sizeof(WAppIconAppList));
3191 tapplist->next = icon->applist;
3192 if (icon->applist)
3193 icon->applist->prev = tapplist;
3194 icon->applist = tapplist;
3195 tapplist->wapp = wApplicationOf(window);
3196 icon->num_apps++;
3197 #endif
3199 found = True;
3200 wDockFinishLaunch(dock, icon);
3201 break;
3205 if (firstPass && !found) {
3206 firstPass = False;
3207 goto retry;
3210 if (command)
3211 free(command);
3213 if (wm_class)
3214 XFree(wm_class);
3215 if (wm_instance)
3216 XFree(wm_instance);
3221 void
3222 wClipUpdateForWorkspaceChange(WScreen *scr, int workspace)
3224 if (!wPreferences.flags.noclip) {
3225 scr->clip_icon->dock = scr->workspaces[workspace]->clip;
3226 if (scr->current_workspace != workspace) {
3227 WDock *old_clip = scr->workspaces[scr->current_workspace]->clip;
3228 WAppIconChain *chain = scr->global_icons;
3230 while (chain) {
3231 moveIconBetweenDocks(chain->aicon->dock,
3232 scr->workspaces[workspace]->clip,
3233 chain->aicon, chain->aicon->xindex,
3234 chain->aicon->yindex);
3235 if (scr->workspaces[workspace]->clip->collapsed)
3236 XUnmapWindow(dpy, chain->aicon->icon->core->window);
3237 chain = chain->next;
3240 wDockHideIcons(old_clip);
3241 if (old_clip->auto_raise_lower) {
3242 if (old_clip->auto_raise_magic) {
3243 WMDeleteTimerHandler(old_clip->auto_raise_magic);
3244 old_clip->auto_raise_magic = NULL;
3246 wDockLower(old_clip);
3248 if (old_clip->auto_collapse) {
3249 if (old_clip->auto_expand_magic) {
3250 WMDeleteTimerHandler(old_clip->auto_expand_magic);
3251 old_clip->auto_expand_magic = NULL;
3253 old_clip->collapsed = 1;
3255 wDockShowIcons(scr->workspaces[workspace]->clip);
3257 if (scr->flags.clip_balloon_mapped)
3258 showClipBalloon(scr->clip_icon->dock, workspace);
3264 static void
3265 trackDeadProcess(pid_t pid, unsigned char status, WDock *dock)
3267 WAppIcon *icon;
3268 int i;
3270 for (i=0; i<dock->max_icons; i++) {
3271 icon = dock->icon_array[i];
3272 if (!icon)
3273 continue;
3275 if (icon->launching && icon->pid == pid) {
3276 if (!icon->relaunching) {
3277 icon->running = 0;
3278 icon->main_window = None;
3280 wDockFinishLaunch(dock, icon);
3281 icon->pid = 0;
3282 if (status==111) {
3283 char msg[PATH_MAX];
3284 #ifdef OFFIX_DND
3285 sprintf(msg, _("Could not execute command \"%s\""),
3286 icon->drop_launch && icon->dnd_command
3287 ? icon->dnd_command : icon->command);
3288 #else
3289 sprintf(msg, _("Could not execute command \"%s\""),
3290 icon->command);
3291 #endif
3292 wMessageDialog(dock->screen_ptr, _("Error"), msg,
3293 _("OK"), NULL, NULL);
3295 break;
3301 static void
3302 toggleLowered(WDock *dock)
3304 WAppIcon *tmp;
3305 int newlevel, i;
3307 /* lower/raise Dock */
3308 if (!dock->lowered) {
3309 newlevel = WMNormalLevel;
3310 dock->lowered = 1;
3311 } else {
3312 newlevel = WMDockLevel;
3313 dock->lowered = 0;
3316 for (i=0; i<dock->max_icons; i++) {
3317 tmp = dock->icon_array[i];
3318 if (!tmp)
3319 continue;
3321 ChangeStackingLevel(tmp->icon->core, newlevel);
3322 if (dock->lowered)
3323 wLowerFrame(tmp->icon->core);
3326 if (dock->type == WM_DOCK)
3327 wScreenUpdateUsableArea(dock->screen_ptr);
3331 static void
3332 toggleCollapsed(WDock *dock)
3334 if (dock->collapsed) {
3335 dock->collapsed = 0;
3336 wDockShowIcons(dock);
3338 else {
3339 dock->collapsed = 1;
3340 wDockHideIcons(dock);
3345 static void
3346 openDockMenu(WDock *dock, WAppIcon *aicon, XEvent *event)
3348 WScreen *scr = dock->screen_ptr;
3349 WObjDescriptor *desc;
3350 WMenuEntry *entry;
3351 WApplication *wapp = NULL;
3352 int index = 0;
3353 int x_pos;
3354 int n_selected;
3355 int appIsRunning = aicon->running && aicon->icon && aicon->icon->owner;
3357 if (dock->type == WM_DOCK) {
3358 /* keep on top */
3359 entry = dock->menu->entries[index];
3360 entry->flags.indicator_on = !dock->lowered;
3361 entry->clientdata = dock;
3362 } else {
3363 /* clip options */
3364 if (scr->clip_options)
3365 updateClipOptionsMenu(scr->clip_options, dock);
3367 n_selected = numberOfSelectedIcons(dock);
3369 /* Rename Workspace */
3370 entry = dock->menu->entries[++index];
3371 if (aicon == scr->clip_icon) {
3372 entry->callback = renameCallback;
3373 entry->clientdata = dock;
3374 entry->flags.indicator = 0;
3375 entry->text = _("Rename Workspace");
3376 } else {
3377 entry->callback = omnipresentCallback;
3378 entry->clientdata = aicon;
3379 if (n_selected > 0) {
3380 entry->flags.indicator = 0;
3381 entry->text = _("Toggle Omnipresent");
3382 } else {
3383 entry->flags.indicator = 1;
3384 entry->flags.indicator_on = aicon->omnipresent;
3385 entry->flags.indicator_type = MI_CHECK;
3386 entry->text = _("Omnipresent");
3390 /* select/unselect icon */
3391 entry = dock->menu->entries[++index];
3392 entry->clientdata = aicon;
3393 entry->flags.indicator_on = aicon->icon->selected;
3394 wMenuSetEnabled(dock->menu, index, aicon!=scr->clip_icon);
3396 /* select/unselect all icons */
3397 entry = dock->menu->entries[++index];
3398 entry->clientdata = aicon;
3399 if (n_selected > 0)
3400 entry->text = _("Unselect All Icons");
3401 else
3402 entry->text = _("Select All Icons");
3403 wMenuSetEnabled(dock->menu, index, dock->icon_count > 1);
3405 /* keep icon(s) */
3406 entry = dock->menu->entries[++index];
3407 entry->clientdata = aicon;
3408 if (n_selected > 1)
3409 entry->text = _("Keep Icons");
3410 else
3411 entry->text = _("Keep Icon");
3412 wMenuSetEnabled(dock->menu, index, dock->icon_count > 1);
3414 /* this is the workspace submenu part */
3415 entry = dock->menu->entries[++index];
3416 if (n_selected > 1)
3417 entry->text = _("Move Icons To");
3418 else
3419 entry->text = _("Move Icon To");
3420 if (scr->clip_submenu)
3421 updateWorkspaceMenu(scr->clip_submenu, aicon);
3422 wMenuSetEnabled(dock->menu, index, !aicon->omnipresent);
3424 /* remove icon(s) */
3425 entry = dock->menu->entries[++index];
3426 entry->clientdata = aicon;
3427 if (n_selected > 1)
3428 entry->text = _("Remove Icons");
3429 else
3430 entry->text = _("Remove Icon");
3431 wMenuSetEnabled(dock->menu, index, dock->icon_count > 1);
3433 /* attract icon(s) */
3434 entry = dock->menu->entries[++index];
3435 entry->clientdata = aicon;
3437 dock->menu->flags.realized = 0;
3438 wMenuRealize(dock->menu);
3441 /* launch */
3442 entry = dock->menu->entries[++index];
3443 entry->clientdata = aicon;
3444 wMenuSetEnabled(dock->menu, index, aicon->command!=NULL);
3446 /* unhide here */
3447 entry = dock->menu->entries[++index];
3448 entry->clientdata = aicon;
3449 wMenuSetEnabled(dock->menu, index, appIsRunning);
3451 /* hide */
3452 entry = dock->menu->entries[++index];
3453 entry->clientdata = aicon;
3454 if (aicon->icon->owner) {
3455 wapp = wApplicationOf(aicon->icon->owner->main_window);
3456 if (wapp && wapp->flags.hidden)
3457 entry->text = _("Unhide");
3458 else
3459 entry->text = _("Hide");
3460 } else {
3461 entry->text = _("Hide");
3463 wMenuSetEnabled(dock->menu, index, appIsRunning);
3465 /* settings */
3466 entry = dock->menu->entries[++index];
3467 entry->clientdata = aicon;
3468 wMenuSetEnabled(dock->menu, index, !aicon->editing
3469 && !wPreferences.flags.noupdates);
3471 /* kill */
3472 entry = dock->menu->entries[++index];
3473 entry->clientdata = aicon;
3474 wMenuSetEnabled(dock->menu, index, appIsRunning);
3476 if (!dock->menu->flags.realized)
3477 wMenuRealize(dock->menu);
3479 if (dock->type == WM_CLIP) {
3480 x_pos = event->xbutton.x_root+2;
3481 } else {
3482 x_pos = dock->on_right_side ?
3483 scr->scr_width - dock->menu->frame->core->width - 2 : 0;
3486 wMenuMapAt(dock->menu, x_pos, event->xbutton.y_root+2, False);
3488 /* allow drag select */
3489 event->xany.send_event = True;
3490 desc = &dock->menu->menu->descriptor;
3491 (*desc->handle_mousedown)(desc, event);
3495 static void
3496 openClipWorkspaceMenu(WScreen *scr, int x, int y)
3498 if (!scr->clip_ws_menu) {
3499 scr->clip_ws_menu = wWorkspaceMenuMake(scr, False);
3501 wWorkspaceMenuUpdate(scr, scr->clip_ws_menu);
3502 wMenuMapAt(scr->clip_ws_menu, x, y, False);
3506 /******************************************************************/
3507 static void
3508 iconDblClick(WObjDescriptor *desc, XEvent *event)
3510 WAppIcon *btn = desc->parent;
3511 WDock *dock = btn->dock;
3512 WApplication *wapp = NULL;
3513 int unhideHere = 0;
3515 #ifdef REDUCE_APPICONS
3516 if ((btn->icon->owner && !(event->xbutton.state & ControlMask)) ||
3517 ((btn->icon->owner == NULL) && (btn->applist != NULL))) {
3518 if (btn->icon->owner == NULL)
3519 btn->icon->owner = btn->applist->wapp->main_window_desc;
3520 #else
3521 if (btn->icon->owner && !(event->xbutton.state & ControlMask)) {
3522 #endif
3523 wapp = wApplicationOf(btn->icon->owner->main_window);
3525 assert(wapp!=NULL);
3527 unhideHere = (event->xbutton.state & ShiftMask);
3529 /* go to the last workspace that the user worked on the app */
3530 if (wapp->last_workspace != dock->screen_ptr->current_workspace
3531 && !unhideHere) {
3532 wWorkspaceChange(dock->screen_ptr, wapp->last_workspace);
3535 wUnhideApplication(wapp, event->xbutton.button==Button2,
3536 unhideHere);
3538 if (event->xbutton.state & MOD_MASK) {
3539 wHideOtherApplications(btn->icon->owner);
3541 } else {
3542 if (event->xbutton.button==Button1) {
3544 if (event->xbutton.state & MOD_MASK) {
3545 /* raise/lower dock */
3546 toggleLowered(dock);
3547 } else if (btn == dock->screen_ptr->clip_icon) {
3548 if (getClipButton(event->xbutton.x, event->xbutton.y)==CLIP_IDLE)
3549 toggleCollapsed(dock);
3550 else
3551 handleClipChangeWorkspace(dock->screen_ptr, event);
3552 } else if (btn->command) {
3553 if (!btn->launching &&
3554 (!btn->running || (event->xbutton.state & ControlMask))) {
3555 launchDockedApplication(btn);
3557 } else if (btn->xindex == 0 && btn->yindex == 0
3558 && btn->dock->type == WM_DOCK) {
3560 wShowGNUstepPanel(dock->screen_ptr);
3567 static void
3568 handleDockMove(WDock *dock, WAppIcon *aicon, XEvent *event)
3570 WScreen *scr = dock->screen_ptr;
3571 int ofs_x=event->xbutton.x, ofs_y=event->xbutton.y;
3572 int x, y;
3573 XEvent ev;
3574 int grabbed = 0, swapped = 0, done;
3575 Pixmap ghost = None;
3576 int superfluous = wPreferences.superfluous; /* we catch it to avoid problems */
3578 #ifdef DEBUG
3579 puts("moving dock");
3580 #endif
3581 if (XGrabPointer(dpy, aicon->icon->core->window, True, ButtonMotionMask
3582 |ButtonReleaseMask|ButtonPressMask, GrabModeAsync,
3583 GrabModeAsync, None, None, CurrentTime) !=GrabSuccess) {
3584 wwarning("pointer grab failed for dock move");
3586 y = 0;
3587 for (x=0; x<dock->max_icons; x++) {
3588 if (dock->icon_array[x]!=NULL &&
3589 dock->icon_array[x]->yindex > y)
3590 y = dock->icon_array[x]->yindex;
3592 y++;
3593 XResizeWindow(dpy, scr->dock_shadow, ICON_SIZE, ICON_SIZE*y);
3595 done = 0;
3596 while (!done) {
3597 WMMaskEvent(dpy, PointerMotionMask|ButtonReleaseMask|ButtonPressMask
3598 |ButtonMotionMask|ExposureMask, &ev);
3599 switch (ev.type) {
3600 case Expose:
3601 WMHandleEvent(&ev);
3602 break;
3604 case MotionNotify:
3605 if (!grabbed) {
3606 if (abs(ofs_x-ev.xmotion.x)>=MOVE_THRESHOLD
3607 || abs(ofs_y-ev.xmotion.y)>=MOVE_THRESHOLD) {
3608 XChangeActivePointerGrab(dpy, ButtonMotionMask
3609 |ButtonReleaseMask|ButtonPressMask,
3610 wCursor[WCUR_MOVE], CurrentTime);
3611 grabbed=1;
3613 break;
3615 if (dock->type == WM_CLIP) {
3616 if (ev.xmotion.x_root - ofs_x < 0) {
3617 x = 0;
3618 } else if (ev.xmotion.x_root - ofs_x + ICON_SIZE >
3619 scr->scr_width) {
3620 x = scr->scr_width - ICON_SIZE;
3621 } else {
3622 x = ev.xmotion.x_root - ofs_x;
3624 if (ev.xmotion.y_root - ofs_y < 0) {
3625 y = 0;
3626 } else if (ev.xmotion.y_root - ofs_y + ICON_SIZE >
3627 scr->scr_height) {
3628 y = scr->scr_height - ICON_SIZE;
3629 } else {
3630 y = ev.xmotion.y_root - ofs_y;
3632 moveDock(dock, x, y);
3633 } else {
3634 /* move vertically if pointer is inside the dock*/
3635 if ((dock->on_right_side &&
3636 ev.xmotion.x_root >= dock->x_pos - ICON_SIZE)
3637 || (!dock->on_right_side &&
3638 ev.xmotion.x_root <= dock->x_pos + ICON_SIZE*2)) {
3640 if (ev.xmotion.y_root - ofs_y < 0) {
3641 y = 0;
3642 } else if (ev.xmotion.y_root - ofs_y + ICON_SIZE >
3643 scr->scr_height) {
3644 y = scr->scr_height - ICON_SIZE;
3645 } else {
3646 y = ev.xmotion.y_root - ofs_y;
3648 moveDock(dock, dock->x_pos, y);
3650 /* move horizontally to change sides */
3651 x = ev.xmotion.x_root - ofs_x;
3652 if (!dock->on_right_side) {
3654 /* is on left */
3656 if (ev.xmotion.x_root > dock->x_pos + ICON_SIZE*2) {
3657 XMoveWindow(dpy, scr->dock_shadow, scr->scr_width-ICON_SIZE
3658 -DOCK_EXTRA_SPACE-1, dock->y_pos);
3659 if (superfluous && ghost==None) {
3660 ghost = MakeGhostDock(dock, dock->x_pos,
3661 scr->scr_width-ICON_SIZE
3662 -DOCK_EXTRA_SPACE-1,
3663 dock->y_pos);
3664 XSetWindowBackgroundPixmap(dpy, scr->dock_shadow,
3665 ghost);
3666 XClearWindow(dpy, scr->dock_shadow);
3668 XMapRaised(dpy, scr->dock_shadow);
3669 swapped = 1;
3670 } else {
3671 if (superfluous && ghost!=None) {
3672 XFreePixmap(dpy, ghost);
3673 ghost = None;
3675 XUnmapWindow(dpy, scr->dock_shadow);
3676 swapped = 0;
3678 } else {
3679 /* is on right */
3680 if (ev.xmotion.x_root < dock->x_pos - ICON_SIZE) {
3681 XMoveWindow(dpy, scr->dock_shadow,
3682 DOCK_EXTRA_SPACE, dock->y_pos);
3683 if (superfluous && ghost==None) {
3684 ghost = MakeGhostDock(dock, dock->x_pos,
3685 DOCK_EXTRA_SPACE, dock->y_pos);
3686 XSetWindowBackgroundPixmap(dpy, scr->dock_shadow,
3687 ghost);
3688 XClearWindow(dpy, scr->dock_shadow);
3690 XMapRaised(dpy, scr->dock_shadow);
3691 swapped = -1;
3692 } else {
3693 XUnmapWindow(dpy, scr->dock_shadow);
3694 swapped = 0;
3695 if (superfluous && ghost!=None) {
3696 XFreePixmap(dpy, ghost);
3697 ghost = None;
3702 break;
3704 case ButtonPress:
3705 break;
3707 case ButtonRelease:
3708 if (ev.xbutton.button != event->xbutton.button)
3709 break;
3710 XUngrabPointer(dpy, CurrentTime);
3711 XUnmapWindow(dpy, scr->dock_shadow);
3712 XResizeWindow(dpy, scr->dock_shadow, ICON_SIZE, ICON_SIZE);
3713 if (dock->type == WM_DOCK) {
3714 if (swapped!=0) {
3715 if (swapped>0)
3716 dock->on_right_side = 1;
3717 else
3718 dock->on_right_side = 0;
3719 swapDock(dock);
3720 wArrangeIcons(scr, False);
3723 done = 1;
3724 break;
3727 if (superfluous) {
3728 if (ghost!=None)
3729 XFreePixmap(dpy, ghost);
3730 XSetWindowBackground(dpy, scr->dock_shadow, scr->white_pixel);
3732 #ifdef DEBUG
3733 puts("End dock move");
3734 #endif
3739 static void
3740 handleIconMove(WDock *dock, WAppIcon *aicon, XEvent *event)
3742 WScreen *scr = dock->screen_ptr;
3743 Window wins[2];
3744 WIcon *icon = aicon->icon;
3745 WDock *dock2 = NULL, *last_dock = dock, *clip = NULL;
3746 int ondock, grabbed = 0, change_dock = 0, collapsed = 0;
3747 XEvent ev;
3748 int x = aicon->x_pos, y = aicon->y_pos;
3749 int ofs_x = event->xbutton.x, ofs_y = event->xbutton.y;
3750 int shad_x = x, shad_y = y;
3751 int ix = aicon->xindex, iy = aicon->yindex;
3752 int tmp;
3753 Pixmap ghost = None;
3754 Bool docked;
3755 int superfluous = wPreferences.superfluous; /* we catch it to avoid problems */
3756 int omnipresent = aicon->omnipresent; /* this must be cached!!! */
3759 if (wPreferences.flags.noupdates)
3760 return;
3762 if (XGrabPointer(dpy, icon->core->window, True, ButtonMotionMask
3763 |ButtonReleaseMask|ButtonPressMask, GrabModeAsync,
3764 GrabModeAsync, None, None, CurrentTime) !=GrabSuccess) {
3765 #ifdef DEBUG0
3766 wwarning("pointer grab failed for icon move");
3767 #endif
3770 if (!(event->xbutton.state & MOD_MASK))
3771 wRaiseFrame(icon->core);
3773 if (!wPreferences.flags.noclip)
3774 clip = scr->workspaces[scr->current_workspace]->clip;
3776 if (dock == scr->dock && !wPreferences.flags.noclip)
3777 dock2 = clip;
3778 else if (dock != scr->dock && !wPreferences.flags.nodock)
3779 dock2 = scr->dock;
3781 wins[0] = icon->core->window;
3782 wins[1] = scr->dock_shadow;
3783 XRestackWindows(dpy, wins, 2);
3784 XMoveResizeWindow(dpy, scr->dock_shadow, aicon->x_pos, aicon->y_pos,
3785 ICON_SIZE, ICON_SIZE);
3786 if (superfluous) {
3787 if (icon->pixmap!=None)
3788 ghost = MakeGhostIcon(scr, icon->pixmap);
3789 else
3790 ghost = MakeGhostIcon(scr, icon->core->window);
3792 XSetWindowBackgroundPixmap(dpy, scr->dock_shadow, ghost);
3793 XClearWindow(dpy, scr->dock_shadow);
3795 XMapWindow(dpy, scr->dock_shadow);
3797 ondock = 1;
3800 while(1) {
3801 XMaskEvent(dpy, PointerMotionMask|ButtonReleaseMask|ButtonPressMask
3802 |ButtonMotionMask|ExposureMask, &ev);
3803 switch (ev.type) {
3804 case Expose:
3805 WMHandleEvent(&ev);
3806 break;
3808 case MotionNotify:
3809 if (!grabbed) {
3810 if (abs(ofs_x-ev.xmotion.x)>=MOVE_THRESHOLD
3811 || abs(ofs_y-ev.xmotion.y)>=MOVE_THRESHOLD) {
3812 XChangeActivePointerGrab(dpy, ButtonMotionMask
3813 |ButtonReleaseMask|ButtonPressMask,
3814 wCursor[WCUR_MOVE], CurrentTime);
3815 grabbed=1;
3816 } else {
3817 break;
3821 if (omnipresent) {
3822 int i;
3823 for (i=0; i<scr->workspace_count; i++) {
3824 if (i == scr->current_workspace)
3825 continue;
3826 wDockShowIcons(scr->workspaces[i]->clip);
3830 x = ev.xmotion.x_root - ofs_x;
3831 y = ev.xmotion.y_root - ofs_y;
3832 tmp = wDockSnapIcon(dock, aicon, x, y, &ix, &iy, True);
3833 if (tmp && dock2) {
3834 change_dock = 0;
3835 if (last_dock != dock && collapsed) {
3836 last_dock->collapsed = 1;
3837 wDockHideIcons(last_dock);
3838 collapsed = 0;
3840 if (!collapsed && (collapsed = dock->collapsed)) {
3841 dock->collapsed = 0;
3842 wDockShowIcons(dock);
3844 if (dock->auto_raise_lower)
3845 wDockRaise(dock);
3846 last_dock = dock;
3847 } else if (dock2) {
3848 tmp = wDockSnapIcon(dock2, aicon, x, y, &ix, &iy, False);
3849 if (tmp) {
3850 change_dock = 1;
3851 if (last_dock != dock2 && collapsed) {
3852 last_dock->collapsed = 1;
3853 wDockHideIcons(last_dock);
3854 collapsed = 0;
3856 if (!collapsed && (collapsed = dock2->collapsed)) {
3857 dock2->collapsed = 0;
3858 wDockShowIcons(dock2);
3860 if (dock2->auto_raise_lower)
3861 wDockRaise(dock2);
3862 last_dock = dock2;
3865 if (aicon->launching
3866 || aicon->lock
3867 || (aicon->running && !(ev.xmotion.state & MOD_MASK))
3868 || (!aicon->running && tmp)) {
3869 shad_x = last_dock->x_pos + ix*wPreferences.icon_size;
3870 shad_y = last_dock->y_pos + iy*wPreferences.icon_size;
3872 XMoveWindow(dpy, scr->dock_shadow, shad_x, shad_y);
3874 if (!ondock) {
3875 XMapWindow(dpy, scr->dock_shadow);
3877 ondock = 1;
3878 } else {
3879 if (ondock) {
3880 XUnmapWindow(dpy, scr->dock_shadow);
3882 ondock = 0;
3884 XMoveWindow(dpy, icon->core->window, x, y);
3885 break;
3887 case ButtonPress:
3888 break;
3890 case ButtonRelease:
3891 if (ev.xbutton.button != event->xbutton.button)
3892 break;
3893 XUngrabPointer(dpy, CurrentTime);
3894 if (ondock) {
3895 SlideWindow(icon->core->window, x, y, shad_x, shad_y);
3896 XUnmapWindow(dpy, scr->dock_shadow);
3897 if (!change_dock) {
3898 reattachIcon(dock, aicon, ix, iy);
3899 if (clip && dock!=clip && clip->auto_raise_lower)
3900 wDockLower(clip);
3901 } else {
3902 docked = moveIconBetweenDocks(dock, dock2, aicon, ix, iy);
3903 if (!docked) {
3904 /* Slide it back if dock rejected it */
3905 SlideWindow(icon->core->window, x, y, aicon->x_pos,
3906 aicon->y_pos);
3907 reattachIcon(dock, aicon, aicon->xindex,aicon->yindex);
3909 if (last_dock->type==WM_CLIP && last_dock->auto_collapse) {
3910 collapsed = 0;
3913 } else {
3914 aicon->x_pos = x;
3915 aicon->y_pos = y;
3916 if (superfluous) {
3917 if (!aicon->running && !wPreferences.no_animations) {
3918 /* We need to deselect it, even if is deselected in
3919 * wDockDetach(), because else DoKaboom() will fail.
3921 if (aicon->icon->selected)
3922 wIconSelect(aicon->icon);
3924 #ifdef WMSOUND
3925 wSoundPlay(WMSOUND_KABOOM);
3926 #endif
3927 DoKaboom(scr,aicon->icon->core->window, x, y);
3928 #ifdef WMSOUND
3929 } else {
3930 wSoundPlay(WMSOUND_UNDOCK);
3931 #endif
3933 #ifdef WMSOUND
3934 } else {
3935 wSoundPlay(WMSOUND_UNDOCK);
3936 #endif
3938 if (clip && clip->auto_raise_lower)
3939 wDockLower(clip);
3940 wDockDetach(dock, aicon);
3942 if (collapsed) {
3943 last_dock->collapsed = 1;
3944 wDockHideIcons(last_dock);
3945 collapsed = 0;
3947 if (superfluous) {
3948 if (ghost!=None)
3949 XFreePixmap(dpy, ghost);
3950 XSetWindowBackground(dpy, scr->dock_shadow, scr->white_pixel);
3952 if (omnipresent) {
3953 int i;
3954 for (i=0; i<scr->workspace_count; i++) {
3955 if (i == scr->current_workspace)
3956 continue;
3957 wDockHideIcons(scr->workspaces[i]->clip);
3961 #ifdef DEBUG
3962 puts("End icon move");
3963 #endif
3964 return;
3970 static int
3971 getClipButton(int px, int py)
3973 int pt = (CLIP_BUTTON_SIZE+2)*ICON_SIZE/64;
3975 if (px < 0 || py < 0 || px >= ICON_SIZE || py >= ICON_SIZE)
3976 return CLIP_IDLE;
3978 if (py <= pt-((int)ICON_SIZE-1-px))
3979 return CLIP_FORWARD;
3980 else if (px <= pt-((int)ICON_SIZE-1-py))
3981 return CLIP_REWIND;
3983 return CLIP_IDLE;
3987 static void
3988 handleClipChangeWorkspace(WScreen *scr, XEvent *event)
3990 XEvent ev;
3991 int done, direction, new_ws;
3992 int new_dir;
3993 WDock *clip = scr->clip_icon->dock;
3995 direction = getClipButton(event->xbutton.x, event->xbutton.y);
3997 clip->lclip_button_pushed = direction==CLIP_REWIND;
3998 clip->rclip_button_pushed = direction==CLIP_FORWARD;
4000 wClipIconPaint(scr->clip_icon);
4001 done = 0;
4002 while(!done) {
4003 WMMaskEvent(dpy, ExposureMask|ButtonMotionMask|ButtonReleaseMask
4004 |ButtonPressMask, &ev);
4005 switch (ev.type) {
4006 case Expose:
4007 WMHandleEvent(&ev);
4008 break;
4010 case MotionNotify:
4011 new_dir = getClipButton(ev.xmotion.x, ev.xmotion.y);
4012 if (new_dir != direction) {
4013 direction = new_dir;
4014 clip->lclip_button_pushed = direction==CLIP_REWIND;
4015 clip->rclip_button_pushed = direction==CLIP_FORWARD;
4016 wClipIconPaint(scr->clip_icon);
4018 break;
4020 case ButtonPress:
4021 break;
4023 case ButtonRelease:
4024 if (ev.xbutton.button == event->xbutton.button)
4025 done = 1;
4029 clip->lclip_button_pushed = 0;
4030 clip->rclip_button_pushed = 0;
4032 new_ws = wPreferences.ws_advance || (event->xbutton.state & ControlMask);
4034 if (direction == CLIP_FORWARD) {
4035 if (scr->current_workspace < scr->workspace_count-1)
4036 wWorkspaceChange(scr, scr->current_workspace+1);
4037 else if (new_ws && scr->current_workspace < MAX_WORKSPACES-1)
4038 wWorkspaceChange(scr, scr->current_workspace+1);
4039 else if (wPreferences.ws_cycle)
4040 wWorkspaceChange(scr, 0);
4042 else if (direction == CLIP_REWIND) {
4043 if (scr->current_workspace > 0)
4044 wWorkspaceChange(scr, scr->current_workspace-1);
4045 else if (scr->current_workspace==0 && wPreferences.ws_cycle)
4046 wWorkspaceChange(scr, scr->workspace_count-1);
4049 wClipIconPaint(scr->clip_icon);
4053 static void
4054 iconMouseDown(WObjDescriptor *desc, XEvent *event)
4056 WAppIcon *aicon = desc->parent;
4057 WDock *dock = aicon->dock;
4058 WScreen *scr = aicon->icon->core->screen_ptr;
4060 if (aicon->editing || WCHECK_STATE(WSTATE_MODAL))
4061 return;
4063 scr->last_dock = dock;
4065 if (dock->menu->flags.mapped)
4066 wMenuUnmap(dock->menu);
4068 if (IsDoubleClick(scr, event)) {
4069 /* double-click was not in the main clip icon */
4070 if (dock->type != WM_CLIP || aicon->xindex!=0 || aicon->yindex!=0
4071 || getClipButton(event->xbutton.x, event->xbutton.y)==CLIP_IDLE) {
4072 iconDblClick(desc, event);
4073 return;
4077 if (dock->type == WM_CLIP && scr->flags.clip_balloon_mapped) {
4078 XUnmapWindow(dpy, scr->clip_balloon);
4079 scr->flags.clip_balloon_mapped = 0;
4082 #ifdef DEBUG
4083 puts("handling dock");
4084 #endif
4085 if (event->xbutton.button == Button1) {
4086 if (event->xbutton.state & MOD_MASK)
4087 wDockLower(dock);
4088 else
4089 wDockRaise(dock);
4091 if ((event->xbutton.state & ShiftMask) && aicon!=scr->clip_icon &&
4092 dock->type!=WM_DOCK) {
4093 wIconSelect(aicon->icon);
4094 return;
4097 if (aicon->yindex==0 && aicon->xindex==0) {
4098 if (getClipButton(event->xbutton.x, event->xbutton.y)!=CLIP_IDLE
4099 && dock->type==WM_CLIP)
4100 handleClipChangeWorkspace(scr, event);
4101 else
4102 handleDockMove(dock, aicon, event);
4103 } else
4104 handleIconMove(dock, aicon, event);
4106 } else if (event->xbutton.button==Button2 && dock->type==WM_CLIP &&
4107 aicon==scr->clip_icon) {
4108 openClipWorkspaceMenu(scr, event->xbutton.x_root+2,
4109 event->xbutton.y_root+2);
4110 if (scr->clip_ws_menu) {
4111 WMenu *menu;
4112 menu = scr->clip_ws_menu;
4113 desc = &menu->menu->descriptor;
4115 event->xany.send_event = True;
4116 (*desc->handle_mousedown)(desc, event);
4118 } else if (event->xbutton.button==Button2 && dock->type==WM_CLIP &&
4119 (event->xbutton.state & ShiftMask) && aicon!=scr->clip_icon) {
4120 wClipMakeIconOmnipresent(aicon, !aicon->omnipresent);
4121 } else if (event->xbutton.button == Button3) {
4122 openDockMenu(dock, aicon, event);
4127 static void
4128 showClipBalloon(WDock *dock, int workspace)
4130 int w, h;
4131 int x, y;
4132 WScreen *scr = dock->screen_ptr;
4133 char *text;
4134 Window stack[2];
4136 scr->flags.clip_balloon_mapped = 1;
4137 XMapWindow(dpy, scr->clip_balloon);
4139 text = scr->workspaces[workspace]->name;
4141 w = WMWidthOfString(scr->clip_title_font, text, strlen(text));
4143 h = WMFontHeight(scr->clip_title_font);
4144 XResizeWindow(dpy, scr->clip_balloon, w, h);
4146 x = dock->x_pos + CLIP_BUTTON_SIZE*ICON_SIZE/64;
4147 y = dock->y_pos + ICON_SIZE - WMFontHeight(scr->clip_title_font) - 3;
4149 if (x+w > scr->scr_width) {
4150 x = scr->scr_width - w;
4151 if (dock->y_pos + ICON_SIZE + h > scr->scr_height)
4152 y = dock->y_pos - h - 1;
4153 else
4154 y = dock->y_pos + ICON_SIZE;
4155 XRaiseWindow(dpy, scr->clip_balloon);
4156 } else {
4157 stack[0] = scr->clip_icon->icon->core->window;
4158 stack[1] = scr->clip_balloon;
4159 XRestackWindows(dpy, stack, 2);
4161 XMoveWindow(dpy, scr->clip_balloon, x, y);
4162 XSetForeground(dpy, scr->clip_title_gc,
4163 scr->clip_title_pixel[CLIP_NORMAL]);
4164 XClearWindow(dpy, scr->clip_balloon);
4165 WMDrawString(scr->wmscreen, scr->clip_balloon, scr->clip_title_gc,
4166 scr->clip_title_font, 0, 0, text, strlen(text));
4170 static void
4171 clipEnterNotify(WObjDescriptor *desc, XEvent *event)
4173 WAppIcon *btn = (WAppIcon*)desc->parent;
4174 WDock *dock;
4175 WScreen *scr;
4177 assert(event->type==EnterNotify);
4179 if(desc->parent_type!=WCLASS_DOCK_ICON)
4180 return;
4182 scr = btn->icon->core->screen_ptr;
4183 if (!btn->omnipresent)
4184 dock = btn->dock;
4185 else
4186 dock = scr->workspaces[scr->current_workspace]->clip;
4188 if (!dock || dock->type!=WM_CLIP)
4189 return;
4191 /* The auto raise/lower code */
4192 if (dock->auto_lower_magic) {
4193 WMDeleteTimerHandler(dock->auto_lower_magic);
4194 dock->auto_lower_magic = NULL;
4196 if (dock->auto_raise_lower && !dock->auto_raise_magic) {
4197 dock->auto_raise_magic = WMAddTimerHandler(AUTO_RAISE_DELAY,
4198 clipAutoRaise,
4199 (void *)dock);
4202 /* The auto expand/collapse code */
4203 if (dock->auto_collapse_magic) {
4204 WMDeleteTimerHandler(dock->auto_collapse_magic);
4205 dock->auto_collapse_magic = NULL;
4207 if (dock->auto_collapse && !dock->auto_expand_magic) {
4208 dock->auto_expand_magic = WMAddTimerHandler(AUTO_EXPAND_DELAY,
4209 clipAutoExpand,
4210 (void *)dock);
4213 if (btn->xindex == 0 && btn->yindex == 0)
4214 showClipBalloon(dock, dock->screen_ptr->current_workspace);
4215 else {
4216 if (dock->screen_ptr->flags.clip_balloon_mapped) {
4217 XUnmapWindow(dpy, dock->screen_ptr->clip_balloon);
4218 dock->screen_ptr->flags.clip_balloon_mapped = 0;
4224 static void
4225 clipLeave(WDock *dock)
4227 XEvent event;
4228 WObjDescriptor *desc = NULL;
4230 if (!dock || dock->type!=WM_CLIP)
4231 return;
4233 if (XCheckTypedEvent(dpy, EnterNotify, &event)!=False) {
4234 if (XFindContext(dpy, event.xcrossing.window, wWinContext,
4235 (XPointer *)&desc)!=XCNOENT
4236 && desc && desc->parent_type==WCLASS_DOCK_ICON
4237 && ((WAppIcon*)desc->parent)->dock
4238 && ((WAppIcon*)desc->parent)->dock->type==WM_CLIP) {
4239 /* We didn't left the Clip yet */
4240 XPutBackEvent(dpy, &event);
4241 return;
4244 XPutBackEvent(dpy, &event);
4245 } else {
4246 /* We entered a withdrawn window, so we're still in Clip */
4247 return;
4250 if (dock->auto_raise_magic) {
4251 WMDeleteTimerHandler(dock->auto_raise_magic);
4252 dock->auto_raise_magic = NULL;
4254 if (dock->auto_raise_lower && !dock->auto_lower_magic) {
4255 dock->auto_lower_magic = WMAddTimerHandler(AUTO_LOWER_DELAY,
4256 clipAutoLower,
4257 (void *)dock);
4260 if (dock->auto_expand_magic) {
4261 WMDeleteTimerHandler(dock->auto_expand_magic);
4262 dock->auto_expand_magic = NULL;
4264 if (dock->auto_collapse && !dock->auto_collapse_magic) {
4265 dock->auto_collapse_magic = WMAddTimerHandler(AUTO_COLLAPSE_DELAY,
4266 clipAutoCollapse,
4267 (void *)dock);
4272 static void
4273 clipLeaveNotify(WObjDescriptor *desc, XEvent *event)
4275 WAppIcon *btn = (WAppIcon*)desc->parent;
4277 assert(event->type==LeaveNotify);
4279 if(desc->parent_type!=WCLASS_DOCK_ICON)
4280 return;
4282 clipLeave(btn->dock);
4286 static void
4287 clipAutoCollapse(void *cdata)
4289 WDock *dock = (WDock *)cdata;
4291 if (dock->type!=WM_CLIP)
4292 return;
4294 if (dock->auto_collapse) {
4295 dock->collapsed = 1;
4296 wDockHideIcons(dock);
4298 dock->auto_collapse_magic = NULL;
4302 static void
4303 clipAutoExpand(void *cdata)
4305 WDock *dock = (WDock *)cdata;
4307 if (dock->type!=WM_CLIP)
4308 return;
4310 if (dock->auto_collapse) {
4311 dock->collapsed = 0;
4312 wDockShowIcons(dock);
4314 dock->auto_expand_magic = NULL;
4318 static void
4319 clipAutoLower(void *cdata)
4321 WDock *dock = (WDock *)cdata;
4323 if (dock->type!=WM_CLIP)
4324 return;
4326 if (dock->auto_raise_lower)
4327 wDockLower(dock);
4329 dock->auto_lower_magic = NULL;
4333 static void
4334 clipAutoRaise(void *cdata)
4336 WDock *dock = (WDock *)cdata;
4338 if (dock->type!=WM_CLIP)
4339 return;
4341 if (dock->auto_raise_lower)
4342 wDockRaise(dock);
4344 if (dock->screen_ptr->flags.clip_balloon_mapped) {
4345 showClipBalloon(dock, dock->screen_ptr->current_workspace);
4348 dock->auto_raise_magic = NULL;
4352 static Bool
4353 iconCanBeOmnipresent(WAppIcon *aicon)
4355 WScreen *scr = aicon->icon->core->screen_ptr;
4356 WDock *clip;
4357 WAppIcon *btn;
4358 int i, j;
4360 for (i=0; i<scr->workspace_count; i++) {
4361 clip = scr->workspaces[i]->clip;
4363 if (clip == aicon->dock)
4364 continue;
4366 if (clip->icon_count + scr->global_icon_count >= clip->max_icons)
4367 return False; /* Clip is full in some workspace */
4369 for (j=0; j<clip->max_icons; j++) {
4370 btn = clip->icon_array[j];
4371 if(btn && btn->xindex==aicon->xindex && btn->yindex==aicon->yindex)
4372 return False;
4376 return True;
4381 wClipMakeIconOmnipresent(WAppIcon *aicon, int omnipresent)
4383 WScreen *scr = aicon->icon->core->screen_ptr;
4384 WAppIconChain *new_entry, *tmp, *tmp1;
4385 int status = WO_SUCCESS;
4387 if ((scr->dock && aicon->dock==scr->dock) || aicon==scr->clip_icon) {
4388 return WO_NOT_APPLICABLE;
4391 if (aicon->omnipresent == omnipresent)
4392 return WO_SUCCESS;
4394 if (omnipresent) {
4395 if (iconCanBeOmnipresent(aicon)) {
4396 aicon->omnipresent = 1;
4397 new_entry = wmalloc(sizeof(WAppIconChain));
4398 new_entry->aicon = aicon;
4399 new_entry->next = scr->global_icons;
4400 scr->global_icons = new_entry;
4401 scr->global_icon_count++;
4402 } else {
4403 aicon->omnipresent = 0;
4404 status = WO_FAILED;
4406 } else {
4407 aicon->omnipresent = 0;
4408 if (aicon == scr->global_icons->aicon) {
4409 tmp = scr->global_icons->next;
4410 free(scr->global_icons);
4411 scr->global_icons = tmp;
4412 scr->global_icon_count--;
4413 } else {
4414 tmp = scr->global_icons;
4415 while (tmp->next) {
4416 if (tmp->next->aicon == aicon) {
4417 tmp1 = tmp->next->next;
4418 free(tmp->next);
4419 tmp->next = tmp1;
4420 scr->global_icon_count--;
4421 break;
4423 tmp = tmp->next;
4428 wAppIconPaint(aicon);
4430 return status;