Changes for omnipresent icons in clip (menu entries redone).
[wmaker-crm.git] / src / dock.c
blobe7b93ea7173b2aa815d27777829da564b6029e02
1 /* dock.c- built-in Dock module for WindowMaker
3 * Window Maker window manager
5 * Copyright (c) 1997, 1998 Alfredo K. Kojima
6 * Copyright (c) 1998, 1999 Dan Pascu
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
21 * USA.
25 #include "wconfig.h"
27 #include <X11/Xlib.h>
28 #include <X11/Xutil.h>
29 #include <stdlib.h>
30 #include <string.h>
31 #include <unistd.h>
32 #include <math.h>
33 #include <limits.h>
35 #ifndef PATH_MAX
36 #define PATH_MAX DEFAULT_PATH_MAX
37 #endif
39 #include "WindowMaker.h"
40 #include "wcore.h"
41 #include "window.h"
42 #include "icon.h"
43 #include "appicon.h"
44 #include "actions.h"
45 #include "stacking.h"
46 #include "dock.h"
47 #include "dialog.h"
48 #include "funcs.h"
49 #include "properties.h"
50 #include "menu.h"
51 #include "client.h"
52 #include "defaults.h"
53 #include "workspace.h"
54 #include "framewin.h"
55 #include "superfluous.h"
57 #include "list.h"
59 #include <proplist.h>
64 /**** Local variables ****/
65 #define CLIP_REWIND 1
66 #define CLIP_IDLE 0
67 #define CLIP_FORWARD 2
70 /**** Global variables ****/
72 /* in dockedapp.c */
73 extern void DestroyDockAppSettingsPanel();
75 extern void ShowDockAppSettingsPanel(WAppIcon *aicon);
78 extern XContext wWinContext;
80 extern Cursor wCursor[WCUR_LAST];
82 extern WPreferences wPreferences;
84 extern XContext wWinContext;
86 #ifdef OFFIX_DND
87 extern Atom _XA_DND_PROTOCOL;
88 #endif
91 #define MOD_MASK wPreferences.modifier_mask
93 extern void appIconMouseDown(WObjDescriptor *desc, XEvent *event);
95 #define ICON_SIZE wPreferences.icon_size
98 /***** Local variables ****/
100 static proplist_t dCommand=NULL;
101 #ifdef OFFIX_DND
102 static proplist_t dDropCommand=NULL;
103 #endif
104 static proplist_t dAutoLaunch, dName, dForced, dBuggyApplication, dYes, dNo;
105 static proplist_t dHost, dDock, dClip;
106 static proplist_t dAutoAttractIcons, dKeepAttracted;
108 static proplist_t dPosition, dApplications, dLowered, dCollapsed, dAutoCollapse;
110 static proplist_t dAutoRaiseLower, dOmnipresent;
112 static void dockIconPaint(WAppIcon *btn);
114 static void iconMouseDown(WObjDescriptor *desc, XEvent *event);
116 static pid_t execCommand(WAppIcon *btn, char *command, WSavedState *state);
118 static void trackDeadProcess(pid_t pid, unsigned char status, WDock *dock);
120 static int getClipButton(int px, int py);
122 static void toggleLowered(WDock *dock);
124 static void toggleCollapsed(WDock *dock);
126 static void clipIconExpose(WObjDescriptor *desc, XEvent *event);
128 static void clipLeave(WDock *dock);
130 static void handleClipChangeWorkspace(WScreen *scr, XEvent *event);
132 Bool moveIconBetweenDocks(WDock *src, WDock *dest, WAppIcon *icon, int x, int y);
134 static void clipEnterNotify(WObjDescriptor *desc, XEvent *event);
135 static void clipLeaveNotify(WObjDescriptor *desc, XEvent *event);
136 static void clipAutoCollapse(void *cdata);
137 static void clipAutoExpand(void *cdata);
138 static void launchDockedApplication(WAppIcon *btn);
140 static void clipAutoLower(void *cdata);
141 static void clipAutoRaise(void *cdata);
143 static void showClipBalloon(WDock *dock, int workspace);
145 #ifdef OFFIX_DND
147 #define DndNotDnd -1
148 #define DndUnknown 0
149 #define DndRawData 1
150 #define DndFile 2
151 #define DndFiles 3
152 #define DndText 4
153 #define DndDir 5
154 #define DndLink 6
155 #define DndExe 7
157 #define DndEND 8
159 #endif /* OFFIX_DND */
163 static void
164 make_keys()
166 if (dCommand!=NULL)
167 return;
169 dCommand = PLRetain(PLMakeString("Command"));
170 #ifdef OFFIX_DND
171 dDropCommand = PLRetain(PLMakeString("DropCommand"));
172 #endif
173 dAutoLaunch = PLRetain(PLMakeString("AutoLaunch"));
174 dName = PLRetain(PLMakeString("Name"));
175 dForced = PLRetain(PLMakeString("Forced"));
176 dBuggyApplication = PLRetain(PLMakeString("BuggyApplication"));
177 dYes = PLRetain(PLMakeString("Yes"));
178 dNo = PLRetain(PLMakeString("No"));
179 dHost = PLRetain(PLMakeString("Host"));
181 dPosition = PLMakeString("Position");
182 dApplications = PLMakeString("Applications");
183 dLowered = PLMakeString("Lowered");
184 dCollapsed = PLMakeString("Collapsed");
185 dAutoCollapse = PLMakeString("AutoCollapse");
186 dAutoRaiseLower = PLMakeString("AutoRaiseLower");
187 dAutoAttractIcons = PLMakeString("AutoAttractIcons");
188 dKeepAttracted = PLMakeString("KeepAttracted");
190 dOmnipresent = PLMakeString("Omnipresent");
192 dDock = PLMakeString("Dock");
193 dClip = PLMakeString("Clip");
198 static void
199 renameCallback(WMenu *menu, WMenuEntry *entry)
201 WDock *dock = entry->clientdata;
202 char buffer[128];
203 int wspace;
204 char *name;
206 assert(entry->clientdata!=NULL);
208 wspace = dock->screen_ptr->current_workspace;
210 name = wstrdup(dock->screen_ptr->workspaces[wspace]->name);
212 sprintf(buffer, _("Type the name for workspace %i:"), wspace+1);
213 if (wInputDialog(dock->screen_ptr, _("Rename Workspace"), buffer,
214 &name)) {
215 wWorkspaceRename(dock->screen_ptr, wspace, name);
217 if (name) {
218 free(name);
223 static void
224 toggleLoweredCallback(WMenu *menu, WMenuEntry *entry)
226 assert(entry->clientdata!=NULL);
228 toggleLowered(entry->clientdata);
230 entry->flags.indicator_on = !((WDock*)entry->clientdata)->lowered;
232 wMenuPaint(menu);
237 static void
238 killCallback(WMenu *menu, WMenuEntry *entry)
240 WAppIcon *icon;
241 #ifdef REDUCE_APPICONS
242 WAppIconAppList *tapplist;
244 extern Atom _XA_WM_DELETE_WINDOW;
245 #else
246 char *buffer;
247 #endif
249 if (!WCHECK_STATE(WSTATE_NORMAL))
250 return;
252 assert(entry->clientdata!=NULL);
254 icon = (WAppIcon*)entry->clientdata;
256 icon->editing = 1;
258 WCHANGE_STATE(WSTATE_MODAL);
260 #ifdef REDUCE_APPICONS
261 /* Send a delete message to the main window of each application
262 * bound to this docked appicon. - cls
264 tapplist = icon->applist;
265 while (tapplist != NULL) {
266 if (tapplist->wapp->main_window_desc != NULL) {
267 if (tapplist->wapp->main_window_desc->protocols.DELETE_WINDOW) {
268 wClientSendProtocol(tapplist->wapp->main_window_desc,
269 _XA_WM_DELETE_WINDOW, CurrentTime);
270 } else {
271 wClientKill(tapplist->wapp->main_window_desc);
274 tapplist = tapplist->next;
276 #else
277 buffer = wstrappend(icon->wm_class,
278 _(" will be forcibly closed.\n"
279 "Any unsaved changes will be lost.\n"
280 "Please confirm."));
282 if (wPreferences.dont_confirm_kill
283 || wMessageDialog(menu->frame->screen_ptr, _("Kill Application"),
284 buffer, _("Yes"), _("No"), NULL)==WAPRDefault) {
285 if (icon->icon && icon->icon->owner) {
286 wClientKill(icon->icon->owner);
289 #endif /* !REDUCE_APPICONS */
291 icon->editing = 0;
293 WCHANGE_STATE(WSTATE_NORMAL);
297 /* TODO: replace this function with a member of the dock struct */
298 static int
299 numberOfSelectedIcons(WDock *dock)
301 WAppIcon *aicon;
302 int i, n;
304 n = 0;
305 for (i=1; i<dock->max_icons; i++) {
306 aicon = dock->icon_array[i];
307 if (aicon && aicon->icon->selected) {
308 n++;
312 return n;
316 static LinkedList*
317 getSelected(WDock *dock)
319 LinkedList *ret=NULL;
320 WAppIcon *btn;
321 int i;
323 for (i=1; i<dock->max_icons; i++) {
324 btn = dock->icon_array[i];
325 if (btn && btn->icon->selected) {
326 ret = list_cons(btn, ret);
330 return ret;
334 static void
335 paintClipButtons(WAppIcon *clipIcon, Bool lpushed, Bool rpushed)
337 Window win = clipIcon->icon->core->window;
338 WScreen *scr = clipIcon->icon->core->screen_ptr;
339 XPoint p[4];
340 int pt = CLIP_BUTTON_SIZE*ICON_SIZE/64;
341 int tp = ICON_SIZE - pt;
342 int as = pt - 15; /* 15 = 5+5+5 */
343 GC gc = scr->clip_title_gc;
344 #ifdef GRADIENT_CLIP_ARROW
345 Bool collapsed = clipIcon->dock->collapsed;
346 #endif
348 if (rpushed) {
349 p[0].x = tp+1;
350 p[0].y = 1;
351 p[1].x = ICON_SIZE-2;
352 p[1].y = 1;
353 p[2].x = ICON_SIZE-2;
354 p[2].y = pt-1;
355 } else if (lpushed) {
356 p[0].x = 1;
357 p[0].y = tp;
358 p[1].x = pt;
359 p[1].y = ICON_SIZE-2;
360 p[2].x = 1;
361 p[2].y = ICON_SIZE-2;
363 if (lpushed || rpushed) {
364 XSetForeground(dpy, scr->draw_gc, scr->white_pixel);
365 XFillPolygon(dpy, win, scr->draw_gc, p, 3, Convex, CoordModeOrigin);
366 XSetForeground(dpy, scr->draw_gc, scr->black_pixel);
368 #ifdef GRADIENT_CLIP_ARROW
369 if (!collapsed) {
370 XSetFillStyle(dpy, scr->copy_gc, FillTiled);
371 XSetTile(dpy, scr->copy_gc, scr->clip_arrow_gradient);
372 XSetClipMask(dpy, scr->copy_gc, None);
373 gc = scr->copy_gc;
375 #endif /* GRADIENT_CLIP_ARROW */
377 /* top right arrow */
378 p[0].x = p[3].x = ICON_SIZE-5-as;
379 p[0].y = p[3].y = 5;
380 p[1].x = ICON_SIZE-6;
381 p[1].y = 5;
382 p[2].x = ICON_SIZE-6;
383 p[2].y = 4+as;
384 if (rpushed) {
385 XFillPolygon(dpy, win, scr->draw_gc, p, 3, Convex, CoordModeOrigin);
386 XDrawLines(dpy, win, scr->draw_gc, p, 4, CoordModeOrigin);
387 } else {
388 #ifdef GRADIENT_CLIP_ARROW
389 if (!collapsed)
390 XSetTSOrigin(dpy, gc, ICON_SIZE-6-as, 5);
391 #endif
392 XFillPolygon(dpy, win, gc, p,3,Convex,CoordModeOrigin);
393 XDrawLines(dpy, win, gc, p,4,CoordModeOrigin);
396 /* bottom left arrow */
397 p[0].x = p[3].x = 5;
398 p[0].y = p[3].y = ICON_SIZE-5-as;
399 p[1].x = 5;
400 p[1].y = ICON_SIZE-6;
401 p[2].x = 4+as;
402 p[2].y = ICON_SIZE-6;
403 if (lpushed) {
404 XFillPolygon(dpy, win, scr->draw_gc, p, 3, Convex, CoordModeOrigin);
405 XDrawLines(dpy, win, scr->draw_gc, p, 4, CoordModeOrigin);
406 } else {
407 #ifdef GRADIENT_CLIP_ARROW
408 if (!collapsed)
409 XSetTSOrigin(dpy, gc, 5, ICON_SIZE-6-as);
410 #endif
411 XFillPolygon(dpy, win, gc, p,3,Convex,CoordModeOrigin);
412 XDrawLines(dpy, win, gc, p,4,CoordModeOrigin);
414 #ifdef GRADIENT_CLIP_ARROW
415 if (!collapsed)
416 XSetFillStyle(dpy, scr->copy_gc, FillSolid);
417 #endif
421 RImage*
422 wClipMakeTile(WScreen *scr, RImage *normalTile)
424 RImage *tile = RCloneImage(normalTile);
425 RColor black;
426 RColor dark;
427 RColor light;
428 int pt, tp;
429 int as;
431 pt = CLIP_BUTTON_SIZE*wPreferences.icon_size/64;
432 tp = wPreferences.icon_size-1 - pt;
433 as = pt - 15;
435 black.alpha = 255;
436 black.red = black.green = black.blue = 0;
438 dark.alpha = 0;
439 dark.red = dark.green = dark.blue = 60;
441 light.alpha = 0;
442 light.red = light.green = light.blue = 80;
445 /* top right */
446 ROperateLine(tile, RSubtractOperation, tp, 0, wPreferences.icon_size-2,
447 pt-1, &dark);
448 RDrawLine(tile, tp-1, 0, wPreferences.icon_size-1, pt+1, &black);
449 ROperateLine(tile, RAddOperation, tp, 2, wPreferences.icon_size-3,
450 pt, &light);
452 /* arrow bevel */
453 ROperateLine(tile, RSubtractOperation, ICON_SIZE - 7 - as, 4,
454 ICON_SIZE - 5, 4, &dark);
455 ROperateLine(tile, RSubtractOperation, ICON_SIZE - 6 - as, 5,
456 ICON_SIZE - 5, 6 + as, &dark);
457 ROperateLine(tile, RAddOperation, ICON_SIZE - 5, 4, ICON_SIZE - 5, 6 + as,
458 &light);
460 /* bottom left */
461 ROperateLine(tile, RAddOperation, 2, tp+2, pt-2,
462 wPreferences.icon_size-3, &dark);
463 RDrawLine(tile, 0, tp-1, pt+1, wPreferences.icon_size-1, &black);
464 ROperateLine(tile, RSubtractOperation, 0, tp-2, pt+1,
465 wPreferences.icon_size-2, &light);
467 /* arrow bevel */
468 ROperateLine(tile, RSubtractOperation, 4, ICON_SIZE - 7 - as, 4,
469 ICON_SIZE - 5, &dark);
470 ROperateLine(tile, RSubtractOperation, 5, ICON_SIZE - 6 - as,
471 6 + as, ICON_SIZE - 5, &dark);
472 ROperateLine(tile, RAddOperation, 4, ICON_SIZE - 5, 6 + as, ICON_SIZE - 5,
473 &light);
475 return tile;
479 static void
480 omnipresentCallback(WMenu *menu, WMenuEntry *entry)
482 WAppIcon *clickedIcon = entry->clientdata;
483 WAppIcon *aicon;
484 WDock *dock;
485 LinkedList *selectedIcons;
486 int failed;
488 assert(entry->clientdata!=NULL);
490 dock = clickedIcon->dock;
492 selectedIcons = getSelected(dock);
494 if (!selectedIcons)
495 selectedIcons = list_cons(clickedIcon, NULL);
497 failed = 0;
498 while (selectedIcons) {
499 aicon = selectedIcons->head;
500 if (wClipMakeIconOmnipresent(aicon, !aicon->omnipresent) == WO_FAILED)
501 failed++;
502 else if (aicon->icon->selected)
503 wIconSelect(aicon->icon);
504 list_remove_head(&selectedIcons);
507 if (failed > 1) {
508 wMessageDialog(aicon->icon->core->screen_ptr, _("Warning"),
509 _("Some icons cannot be made omnipresent. "
510 "Please make sure that no other icon is "
511 "docked in the same positions on the other "
512 "workspaces and the Clip is not full in "
513 "some workspace."),
514 _("OK"), NULL, NULL);
515 } else if (failed == 1) {
516 wMessageDialog(aicon->icon->core->screen_ptr, _("Warning"),
517 _("Icon cannot be made omnipresent. "
518 "Please make sure that no other icon is "
519 "docked in the same position on the other "
520 "workspaces and the Clip is not full in "
521 "some workspace."),
522 _("OK"), NULL, NULL);
527 static void
528 removeIconsCallback(WMenu *menu, WMenuEntry *entry)
530 WAppIcon *clickedIcon = (WAppIcon*)entry->clientdata;
531 WDock *dock;
532 WAppIcon *aicon;
533 LinkedList *selectedIcons;
534 int keepit;
536 assert(clickedIcon!=NULL);
538 dock = clickedIcon->dock;
540 selectedIcons = getSelected(dock);
542 if (selectedIcons) {
543 if (wMessageDialog(dock->screen_ptr, _("Workspace Clip"),
544 _("All selected icons will be removed!"),
545 _("OK"), _("Cancel"), NULL)!=WAPRDefault) {
546 return;
548 } else {
549 if (clickedIcon->xindex==0 && clickedIcon->yindex==0)
550 return;
551 selectedIcons = list_cons(clickedIcon, NULL);
554 while (selectedIcons) {
555 aicon = selectedIcons->head;
556 keepit = aicon->running && wApplicationOf(aicon->main_window);
557 wDockDetach(dock, aicon);
558 if (keepit) {
559 PlaceIcon(dock->screen_ptr, &aicon->x_pos, &aicon->y_pos);
560 XMoveWindow(dpy, aicon->icon->core->window,
561 aicon->x_pos, aicon->y_pos);
562 if (!dock->mapped || dock->collapsed)
563 XMapWindow(dpy, aicon->icon->core->window);
565 list_remove_head(&selectedIcons);
568 if (wPreferences.auto_arrange_icons)
569 wArrangeIcons(dock->screen_ptr, True);
573 static void
574 keepIconsCallback(WMenu *menu, WMenuEntry *entry)
576 WAppIcon *clickedIcon = (WAppIcon*)entry->clientdata;
577 WDock *dock;
578 WAppIcon *aicon;
579 LinkedList *selectedIcons;
581 assert(clickedIcon!=NULL);
582 dock = clickedIcon->dock;
584 selectedIcons = getSelected(dock);
586 if (!selectedIcons && clickedIcon!=dock->screen_ptr->clip_icon) {
587 char *command = NULL;
589 if (!clickedIcon->command && !clickedIcon->editing) {
590 clickedIcon->editing = 1;
591 if (wInputDialog(dock->screen_ptr, _("Keep Icon"),
592 _("Type the command used to launch the application"),
593 &command)) {
594 if (command && (command[0]==0 ||
595 (command[0]=='-' && command[1]==0))) {
596 free(command);
597 command = NULL;
599 clickedIcon->command = command;
600 clickedIcon->editing = 0;
601 } else {
602 clickedIcon->editing = 0;
603 if (command)
604 free(command);
605 return;
609 selectedIcons = list_cons(clickedIcon, NULL);
612 while (selectedIcons) {
613 aicon = selectedIcons->head;
614 if (aicon->icon->selected)
615 wIconSelect(aicon->icon);
616 if (aicon && aicon->attracted && aicon->command) {
617 aicon->attracted = 0;
618 if (aicon->icon->shadowed) {
619 aicon->icon->shadowed = 0;
620 aicon->icon->force_paint = 1;
621 wAppIconPaint(aicon);
624 list_remove_head(&selectedIcons);
631 static void
632 toggleAutoAttractCallback(WMenu *menu, WMenuEntry *entry)
634 WDock *dock = (WDock*)entry->clientdata;
636 assert(entry->clientdata!=NULL);
638 dock->attract_icons = !dock->attract_icons;
639 /*if (!dock->attract_icons)
640 dock->keep_attracted = 0;*/
642 entry->flags.indicator_on = dock->attract_icons;
644 wMenuPaint(menu);
648 static void
649 toggleKeepCallback(WMenu *menu, WMenuEntry *entry)
651 WDock *dock = (WDock*)entry->clientdata;
652 WAppIcon *btn;
653 int i;
655 assert(entry->clientdata!=NULL);
657 dock->keep_attracted = !dock->keep_attracted;
659 if (dock->keep_attracted) {
660 for (i=0; i< dock->max_icons; i++) {
661 btn = dock->icon_array[i];
662 if (btn && btn->attracted && btn->command) {
663 btn->attracted = 0;
664 if (btn->icon->shadowed) {
665 btn->icon->shadowed = 0;
666 btn->icon->force_paint = 1;
667 wAppIconPaint(btn);
673 entry->flags.indicator_on = dock->keep_attracted;
675 wMenuPaint(menu);
679 static void
680 selectCallback(WMenu *menu, WMenuEntry *entry)
682 WAppIcon *icon = (WAppIcon*)entry->clientdata;
684 assert(icon!=NULL);
686 wIconSelect(icon->icon);
688 wMenuPaint(menu);
692 static void
693 colectIconsCallback(WMenu *menu, WMenuEntry *entry)
695 WAppIcon *clickedIcon = (WAppIcon*)entry->clientdata;
696 WDock *clip;
697 WAppIcon *aicon;
698 int x, y, x_pos, y_pos;
700 assert(entry->clientdata!=NULL);
701 clip = clickedIcon->dock;
703 aicon = clip->screen_ptr->app_icon_list;
705 while (aicon) {
706 if (!aicon->docked && wDockFindFreeSlot(clip, &x, &y)) {
707 x_pos = clip->x_pos + x*ICON_SIZE;
708 y_pos = clip->y_pos + y*ICON_SIZE;
709 if (aicon->x_pos != x_pos || aicon->y_pos != y_pos) {
710 #ifdef ANIMATIONS
711 if (wPreferences.no_animations) {
712 XMoveWindow(dpy, aicon->icon->core->window, x_pos, y_pos);
713 } else {
714 SlideWindow(aicon->icon->core->window,
715 aicon->x_pos, aicon->y_pos, x_pos, y_pos);
717 #else
718 XMoveWindow(dpy, aicon->icon->core->window, x_pos, y_pos);
719 #endif /* ANIMATIONS */
721 aicon->attracted = 1;
722 if (!clip->keep_attracted && !aicon->icon->shadowed) {
723 aicon->icon->shadowed = 1;
724 aicon->icon->force_paint = 1;
725 /* We don't do an wAppIconPaint() here because it's in
726 * wDockAttachIcon(). -Dan
729 wDockAttachIcon(clip, aicon, x, y);
730 if (clip->collapsed || !clip->mapped)
731 XUnmapWindow(dpy, aicon->icon->core->window);
733 aicon = aicon->next;
738 static void
739 selectIconsCallback(WMenu *menu, WMenuEntry *entry)
741 WAppIcon *clickedIcon = (WAppIcon*)entry->clientdata;
742 WDock *dock;
743 LinkedList *selectedIcons;
744 WAppIcon *btn;
745 int i;
747 assert(clickedIcon!=NULL);
748 dock = clickedIcon->dock;
750 selectedIcons = getSelected(dock);
752 if (!selectedIcons) {
753 for (i=1; i<dock->max_icons; i++) {
754 btn = dock->icon_array[i];
755 if (btn && !btn->icon->selected) {
756 wIconSelect(btn->icon);
759 } else {
760 while(selectedIcons) {
761 btn = selectedIcons->head;
762 wIconSelect(btn->icon);
763 list_remove_head(&selectedIcons);
767 wMenuPaint(menu);
771 static void
772 toggleCollapsedCallback(WMenu *menu, WMenuEntry *entry)
774 assert(entry->clientdata!=NULL);
776 toggleCollapsed(entry->clientdata);
778 entry->flags.indicator_on = ((WDock*)entry->clientdata)->collapsed;
780 wMenuPaint(menu);
784 static void
785 toggleAutoCollapseCallback(WMenu *menu, WMenuEntry *entry)
787 WDock *dock;
788 assert(entry->clientdata!=NULL);
790 dock = (WDock*) entry->clientdata;
792 dock->auto_collapse = !dock->auto_collapse;
794 entry->flags.indicator_on = ((WDock*)entry->clientdata)->auto_collapse;
796 wMenuPaint(menu);
800 static void
801 toggleAutoRaiseLowerCallback(WMenu *menu, WMenuEntry *entry)
803 WDock *dock;
804 assert(entry->clientdata!=NULL);
806 dock = (WDock*) entry->clientdata;
808 dock->auto_raise_lower = !dock->auto_raise_lower;
810 entry->flags.indicator_on = ((WDock*)entry->clientdata)->auto_raise_lower;
812 wMenuPaint(menu);
816 static void
817 launchCallback(WMenu *menu, WMenuEntry *entry)
819 WAppIcon *btn = (WAppIcon*)entry->clientdata;
821 launchDockedApplication(btn);
825 static void
826 settingsCallback(WMenu *menu, WMenuEntry *entry)
828 WAppIcon *btn = (WAppIcon*)entry->clientdata;
830 if (btn->editing)
831 return;
832 ShowDockAppSettingsPanel(btn);
836 static void
837 hideCallback(WMenu *menu, WMenuEntry *entry)
839 WApplication *wapp;
840 WAppIcon *btn = (WAppIcon*)entry->clientdata;
842 wapp = wApplicationOf(btn->icon->owner->main_window);
844 if (wapp->flags.hidden) {
845 wWorkspaceChange(btn->icon->core->screen_ptr,wapp->last_workspace);
846 wUnhideApplication(wapp, False, False);
847 } else {
848 wHideApplication(wapp);
853 static void
854 unhideHereCallback(WMenu *menu, WMenuEntry *entry)
856 WApplication *wapp;
857 WAppIcon *btn = (WAppIcon*)entry->clientdata;
859 wapp = wApplicationOf(btn->icon->owner->main_window);
861 wUnhideApplication(wapp, False, True);
865 WAppIcon*
866 mainIconCreate(WScreen *scr, int type)
868 WAppIcon *btn;
869 int x_pos;
871 if (type == WM_CLIP) {
872 if (scr->clip_icon)
873 return scr->clip_icon;
874 btn = wAppIconCreateForDock(scr, NULL, "Logo", "WMClip", TILE_CLIP);
875 btn->icon->core->descriptor.handle_expose = clipIconExpose;
876 btn->icon->core->descriptor.handle_enternotify = clipEnterNotify;
877 btn->icon->core->descriptor.handle_leavenotify = clipLeaveNotify;
878 /*x_pos = scr->scr_width - ICON_SIZE*2 - DOCK_EXTRA_SPACE;*/
879 x_pos = 0;
880 } else {
881 btn = wAppIconCreateForDock(scr, NULL, "Logo", "WMDock", TILE_NORMAL);
882 x_pos = scr->scr_width - ICON_SIZE - DOCK_EXTRA_SPACE;
885 btn->xindex = 0;
886 btn->yindex = 0;
888 btn->icon->core->descriptor.handle_mousedown = iconMouseDown;
889 btn->icon->core->descriptor.parent_type = WCLASS_DOCK_ICON;
890 btn->icon->core->descriptor.parent = btn;
891 /*ChangeStackingLevel(btn->icon->core, WMDockLevel);*/
892 XMapWindow(dpy, btn->icon->core->window);
893 btn->x_pos = x_pos;
894 btn->y_pos = 0;
895 btn->docked = 1;
896 if (type == WM_CLIP)
897 scr->clip_icon = btn;
899 return btn;
903 static void
904 switchWSCommand(WMenu *menu, WMenuEntry *entry)
906 WAppIcon *btn, *icon = (WAppIcon*) entry->clientdata;
907 WScreen *scr = icon->icon->core->screen_ptr;
908 WDock *src, *dest;
909 LinkedList *selectedIcons;
910 int x, y;
912 if (entry->order == scr->current_workspace)
913 return;
914 src = icon->dock;
915 dest = scr->workspaces[entry->order]->clip;
917 selectedIcons = getSelected(src);
919 if (selectedIcons) {
920 while(selectedIcons) {
921 btn = selectedIcons->head;
922 if (wDockFindFreeSlot(dest, &x, &y)) {
923 moveIconBetweenDocks(src, dest, btn, x, y);
924 XUnmapWindow(dpy, btn->icon->core->window);
926 list_remove_head(&selectedIcons);
928 } else if (icon != scr->clip_icon) {
929 if (wDockFindFreeSlot(dest, &x, &y)) {
930 moveIconBetweenDocks(src, dest, icon, x, y);
931 XUnmapWindow(dpy, icon->icon->core->window);
938 static void
939 launchDockedApplication(WAppIcon *btn)
941 WScreen *scr = btn->icon->core->screen_ptr;
943 if (!btn->launching && btn->command!=NULL) {
944 if (!btn->forced_dock) {
945 btn->relaunching = btn->running;
946 btn->running = 1;
948 if (btn->wm_instance || btn->wm_class) {
949 WWindowAttributes attr;
950 memset(&attr, 0, sizeof(WWindowAttributes));
951 wDefaultFillAttributes(scr, btn->wm_instance, btn->wm_class,
952 &attr, NULL, True);
954 if (!attr.no_appicon && !btn->buggy_app)
955 btn->launching = 1;
956 else
957 btn->running = 0;
959 btn->drop_launch = 0;
960 scr->last_dock = btn->dock;
961 btn->pid = execCommand(btn, btn->command, NULL);
962 if (btn->pid>0) {
963 if (btn->buggy_app) {
964 /* give feedback that the app was launched */
965 btn->launching = 1;
966 dockIconPaint(btn);
967 btn->launching = 0;
968 WMAddTimerHandler(200, (WMCallback*)dockIconPaint, btn);
969 } else {
970 dockIconPaint(btn);
972 } else {
973 wwarning(_("could not launch application %s\n"), btn->command);
974 btn->launching = 0;
975 if (!btn->relaunching)
976 btn->running = 0;
983 static void
984 updateWorkspaceMenu(WMenu *menu, WAppIcon *icon)
986 WScreen *scr = menu->frame->screen_ptr;
987 char title[MAX_WORKSPACENAME_WIDTH+1];
988 int i;
990 if (!menu || !icon)
991 return;
993 for (i=0; i<scr->workspace_count; i++) {
994 if (i < menu->entry_no) {
995 if (strcmp(menu->entries[i]->text,scr->workspaces[i]->name)!=0) {
996 free(menu->entries[i]->text);
997 strcpy(title, scr->workspaces[i]->name);
998 menu->entries[i]->text = wstrdup(title);
999 menu->flags.realized = 0;
1001 menu->entries[i]->clientdata = (void*)icon;
1002 } else {
1003 strcpy(title, scr->workspaces[i]->name);
1005 wMenuAddCallback(menu, title, switchWSCommand, (void*)icon);
1007 menu->flags.realized = 0;
1009 if (i == scr->current_workspace) {
1010 wMenuSetEnabled(menu, i, False);
1011 } else {
1012 wMenuSetEnabled(menu, i, True);
1016 if (!menu->flags.realized)
1017 wMenuRealize(menu);
1021 static WMenu*
1022 makeWorkspaceMenu(WScreen *scr)
1024 WMenu *menu;
1026 menu = wMenuCreate(scr, NULL, False);
1027 if (!menu)
1028 wwarning(_("could not create workspace submenu for Clip menu"));
1030 wMenuAddCallback(menu, "", switchWSCommand, (void*)scr->clip_icon);
1032 menu->flags.realized = 0;
1033 wMenuRealize(menu);
1035 return menu;
1039 static void
1040 updateClipOptionsMenu(WMenu *menu, WDock *dock)
1042 WMenuEntry *entry;
1043 int index = 0;
1045 if (!menu || !dock)
1046 return;
1048 /* keep on top */
1049 entry = menu->entries[index];
1050 entry->flags.indicator_on = !dock->lowered;
1051 entry->clientdata = dock;
1053 /* collapsed */
1054 entry = menu->entries[++index];
1055 entry->flags.indicator_on = dock->collapsed;
1056 entry->clientdata = dock;
1058 /* auto-collapse */
1059 entry = menu->entries[++index];
1060 entry->flags.indicator_on = dock->auto_collapse;
1061 entry->clientdata = dock;
1063 /* auto-raise/lower */
1064 entry = menu->entries[++index];
1065 entry->flags.indicator_on = dock->auto_raise_lower;
1066 entry->clientdata = dock;
1068 /* attract icons */
1069 entry = menu->entries[++index];
1070 entry->flags.indicator_on = dock->attract_icons;
1071 entry->clientdata = dock;
1073 /* keep attracted icons */
1074 entry = menu->entries[++index];
1075 entry->flags.indicator_on = dock->keep_attracted;
1076 entry->clientdata = dock;
1078 menu->flags.realized = 0;
1079 wMenuRealize(menu);
1083 static WMenu*
1084 makeClipOptionsMenu(WScreen *scr)
1086 WMenu *menu;
1087 WMenuEntry *entry;
1089 menu = wMenuCreate(scr, NULL, False);
1090 if (!menu) {
1091 wwarning(_("could not create options submenu for Clip menu"));
1092 return NULL;
1095 entry = wMenuAddCallback(menu, _("Keep on Top"),
1096 toggleLoweredCallback, NULL);
1097 entry->flags.indicator = 1;
1098 entry->flags.indicator_on = 1;
1099 entry->flags.indicator_type = MI_CHECK;
1101 entry = wMenuAddCallback(menu, _("Collapsed"),
1102 toggleCollapsedCallback, NULL);
1103 entry->flags.indicator = 1;
1104 entry->flags.indicator_on = 1;
1105 entry->flags.indicator_type = MI_CHECK;
1107 entry = wMenuAddCallback(menu, _("AutoCollapse"),
1108 toggleAutoCollapseCallback, NULL);
1109 entry->flags.indicator = 1;
1110 entry->flags.indicator_on = 1;
1111 entry->flags.indicator_type = MI_CHECK;
1113 entry = wMenuAddCallback(menu, _("AutoRaiseLower"),
1114 toggleAutoRaiseLowerCallback, NULL);
1115 entry->flags.indicator = 1;
1116 entry->flags.indicator_on = 1;
1117 entry->flags.indicator_type = MI_CHECK;
1119 entry = wMenuAddCallback(menu, _("AutoAttract Icons"),
1120 toggleAutoAttractCallback, NULL);
1121 entry->flags.indicator = 1;
1122 entry->flags.indicator_on = 1;
1123 entry->flags.indicator_type = MI_CHECK;
1125 entry = wMenuAddCallback(menu, _("Keep Attracted Icons"),
1126 toggleKeepCallback, NULL);
1127 entry->flags.indicator = 1;
1128 entry->flags.indicator_on = 1;
1129 entry->flags.indicator_type = MI_CHECK;
1131 menu->flags.realized = 0;
1132 wMenuRealize(menu);
1134 return menu;
1138 static WMenu*
1139 dockMenuCreate(WScreen *scr, int type)
1141 WMenu *menu;
1142 WMenuEntry *entry;
1144 if (type == WM_CLIP && scr->clip_menu)
1145 return scr->clip_menu;
1147 menu = wMenuCreate(scr, NULL, False);
1148 if (type != WM_CLIP) {
1149 entry = wMenuAddCallback(menu, _("Keep on top"),
1150 toggleLoweredCallback, NULL);
1151 entry->flags.indicator = 1;
1152 entry->flags.indicator_on = 1;
1153 entry->flags.indicator_type = MI_CHECK;
1154 } else {
1155 entry = wMenuAddCallback(menu, _("Clip Options"), NULL, NULL);
1156 scr->clip_options = makeClipOptionsMenu(scr);
1157 if (scr->clip_options)
1158 wMenuEntrySetCascade(menu, entry, scr->clip_options);
1160 entry = wMenuAddCallback(menu, _("Rename Workspace"), renameCallback,
1161 NULL);
1162 free(entry->text);
1163 entry->text = _("Rename Workspace");
1165 entry = wMenuAddCallback(menu, _("Selected"), selectCallback, NULL);
1166 entry->flags.indicator = 1;
1167 entry->flags.indicator_on = 1;
1168 entry->flags.indicator_type = MI_CHECK;
1170 entry = wMenuAddCallback(menu, _("Select All Icons"),
1171 selectIconsCallback, NULL);
1172 free(entry->text);
1173 entry->text = _("Select All Icons");
1175 entry = wMenuAddCallback(menu, _("Keep Icon"), keepIconsCallback, NULL);
1176 free(entry->text);
1177 entry->text = _("Keep Icon");
1179 entry = wMenuAddCallback(menu, _("Move Icon To"), NULL, NULL);
1180 free(entry->text);
1181 entry->text = _("Move Icon To");
1182 scr->clip_submenu = makeWorkspaceMenu(scr);
1183 if (scr->clip_submenu)
1184 wMenuEntrySetCascade(menu, entry, scr->clip_submenu);
1186 entry = wMenuAddCallback(menu, _("Remove Icon"), removeIconsCallback,
1187 NULL);
1188 free(entry->text);
1189 entry->text = _("Remove Icon");
1191 wMenuAddCallback(menu, _("Attract Icons"), colectIconsCallback, NULL);
1194 wMenuAddCallback(menu, _("Launch"), launchCallback, NULL);
1196 wMenuAddCallback(menu, _("Unhide Here"), unhideHereCallback, NULL);
1198 entry = wMenuAddCallback(menu, _("Hide"), hideCallback, NULL);
1199 free(entry->text);
1200 entry->text = _("Hide");
1202 wMenuAddCallback(menu, _("Settings..."), settingsCallback, NULL);
1204 wMenuAddCallback(menu, _("Kill"), killCallback, NULL);
1206 if (type == WM_CLIP)
1207 scr->clip_menu = menu;
1209 return menu;
1213 WDock*
1214 wDockCreate(WScreen *scr, int type)
1216 WDock *dock;
1217 WAppIcon *btn;
1218 int icon_count;
1220 make_keys();
1222 dock = wmalloc(sizeof(WDock));
1223 memset(dock, 0, sizeof(WDock));
1225 if (type == WM_CLIP)
1226 icon_count = CLIP_MAX_ICONS;
1227 else
1228 icon_count = scr->scr_height/wPreferences.icon_size;
1230 dock->icon_array = wmalloc(sizeof(WAppIcon*)*icon_count);
1231 memset(dock->icon_array, 0, sizeof(WAppIcon*)*icon_count);
1233 dock->max_icons = icon_count;
1235 btn = mainIconCreate(scr, type);
1237 btn->dock = dock;
1239 dock->x_pos = btn->x_pos;
1240 dock->y_pos = btn->y_pos;
1241 dock->screen_ptr = scr;
1242 dock->type = type;
1243 dock->icon_count = 1;
1244 dock->on_right_side = 1;
1245 dock->collapsed = 0;
1246 dock->auto_collapse = 0;
1247 dock->auto_collapse_magic = NULL;
1248 dock->auto_raise_lower = 0;
1249 dock->auto_lower_magic = NULL;
1250 dock->auto_raise_magic = NULL;
1251 dock->attract_icons = 0;
1252 dock->keep_attracted = 0;
1253 dock->lowered = 1;
1254 dock->icon_array[0] = btn;
1255 wRaiseFrame(btn->icon->core);
1256 XMoveWindow(dpy, btn->icon->core->window, btn->x_pos, btn->y_pos);
1258 /* create dock menu */
1259 dock->menu = dockMenuCreate(scr, type);
1261 return dock;
1265 void
1266 wDockDestroy(WDock *dock)
1268 int i;
1269 WAppIcon *aicon;
1271 for (i=(dock->type == WM_CLIP) ? 1 : 0; i<dock->max_icons; i++) {
1272 aicon = dock->icon_array[i];
1273 if (aicon) {
1274 int keepit = aicon->running && wApplicationOf(aicon->main_window);
1275 wDockDetach(dock, aicon);
1276 if (keepit) {
1277 PlaceIcon(dock->screen_ptr, &aicon->x_pos, &aicon->y_pos);
1278 XMoveWindow(dpy, aicon->icon->core->window,
1279 aicon->x_pos, aicon->y_pos);
1280 if (!dock->mapped || dock->collapsed)
1281 XMapWindow(dpy, aicon->icon->core->window);
1285 if (wPreferences.auto_arrange_icons)
1286 wArrangeIcons(dock->screen_ptr, True);
1287 free(dock->icon_array);
1288 if (dock->menu && dock->type!=WM_CLIP)
1289 wMenuDestroy(dock->menu, True);
1290 free(dock);
1294 void
1295 wClipIconPaint(WAppIcon *aicon)
1297 WScreen *scr = aicon->icon->core->screen_ptr;
1298 WWorkspace *workspace = scr->workspaces[scr->current_workspace];
1299 GC gc;
1300 Window win = aicon->icon->core->window;
1301 int length, nlength;
1302 char *ws_name, ws_number[10];
1303 int ty, tx;
1305 wIconPaint(aicon->icon);
1307 length = strlen(workspace->name);
1308 ws_name = malloc(length + 1);
1309 sprintf(ws_name, "%s", workspace->name);
1310 sprintf(ws_number, "%i", scr->current_workspace + 1);
1311 nlength = strlen(ws_number);
1313 gc = scr->clip_title_gc;
1315 if (!workspace->clip->collapsed)
1316 XSetForeground(dpy, gc, scr->clip_title_pixel[CLIP_NORMAL]);
1317 else
1318 XSetForeground(dpy, gc, scr->clip_title_pixel[CLIP_COLLAPSED]);
1320 ty = ICON_SIZE - WMFontHeight(scr->clip_title_font) - 3;
1322 tx = CLIP_BUTTON_SIZE*ICON_SIZE/64;
1324 WMDrawString(scr->wmscreen, win, gc, scr->clip_title_font, tx,
1325 ty, ws_name, length);
1327 tx = (ICON_SIZE/2 - WMWidthOfString(scr->clip_title_font, ws_number,
1328 nlength))/2;
1330 WMDrawString(scr->wmscreen, win, gc, scr->clip_title_font, tx,
1331 2, ws_number, nlength);
1333 free(ws_name);
1335 if (aicon->launching) {
1336 XFillRectangle(dpy, aicon->icon->core->window, scr->stipple_gc,
1337 0, 0, wPreferences.icon_size, wPreferences.icon_size);
1339 paintClipButtons(aicon, aicon->dock->lclip_button_pushed,
1340 aicon->dock->rclip_button_pushed);
1344 static void
1345 clipIconExpose(WObjDescriptor *desc, XEvent *event)
1347 wClipIconPaint(desc->parent);
1351 static void
1352 dockIconPaint(WAppIcon *btn)
1354 if (btn == btn->icon->core->screen_ptr->clip_icon)
1355 wClipIconPaint(btn);
1356 else
1357 wAppIconPaint(btn);
1361 static proplist_t
1362 make_icon_state(WAppIcon *btn)
1364 proplist_t node = NULL;
1365 proplist_t command, autolaunch, name, forced, host, position, buggy;
1366 proplist_t omnipresent;
1367 char *tmp;
1368 char buffer[64];
1370 if (btn) {
1371 if (!btn->command)
1372 command = PLMakeString("-");
1373 else
1374 command = PLMakeString(btn->command);
1376 autolaunch = btn->auto_launch ? dYes : dNo;
1378 tmp = EscapeWM_CLASS(btn->wm_instance, btn->wm_class);
1380 name = PLMakeString(tmp);
1382 free(tmp);
1384 forced = btn->forced_dock ? dYes : dNo;
1386 buggy = btn->buggy_app ? dYes : dNo;
1388 if (btn == btn->icon->core->screen_ptr->clip_icon)
1389 sprintf(buffer, "%i,%i", btn->x_pos, btn->y_pos);
1390 else
1391 sprintf(buffer, "%hi,%hi", btn->xindex, btn->yindex);
1392 position = PLMakeString(buffer);
1394 node = PLMakeDictionaryFromEntries(dCommand, command,
1395 dName, name,
1396 dAutoLaunch, autolaunch,
1397 dForced, forced,
1398 dBuggyApplication, buggy,
1399 dPosition, position,
1400 NULL);
1401 PLRelease(command);
1402 PLRelease(name);
1403 PLRelease(position);
1405 omnipresent = btn->omnipresent ? dYes : dNo;
1406 if (btn->dock != btn->icon->core->screen_ptr->dock &&
1407 (btn->xindex != 0 || btn->yindex != 0))
1408 PLInsertDictionaryEntry(node, dOmnipresent, omnipresent);
1410 #ifdef OFFIX_DND
1411 if (btn->dnd_command) {
1412 command = PLMakeString(btn->dnd_command);
1413 PLInsertDictionaryEntry(node, dDropCommand, command);
1414 PLRelease(command);
1416 #endif /* OFFIX_DND */
1418 if (btn->client_machine && btn->remote_start) {
1419 host = PLMakeString(btn->client_machine);
1420 PLInsertDictionaryEntry(node, dHost, host);
1421 PLRelease(host);
1425 return node;
1429 static proplist_t
1430 dockSaveState(WDock *dock)
1432 int i;
1433 proplist_t icon_info;
1434 proplist_t list=NULL, dock_state=NULL;
1435 proplist_t value;
1436 char buffer[256];
1438 list = PLMakeArrayFromElements(NULL);
1440 for (i=(dock->type==WM_DOCK ? 0 : 1); i<dock->max_icons; i++) {
1441 WAppIcon *btn = dock->icon_array[i];
1443 if (!btn || (btn->attracted && !dock->keep_attracted))
1444 continue;
1446 if ((icon_info = make_icon_state(dock->icon_array[i]))) {
1447 list = PLAppendArrayElement(list, icon_info);
1448 PLRelease(icon_info);
1452 dock_state = PLMakeDictionaryFromEntries(dApplications, list, NULL);
1454 PLRelease(list);
1456 if (dock->type == WM_DOCK) {
1457 sprintf(buffer, "%i,%i", (dock->on_right_side ? -ICON_SIZE : 0),
1458 dock->y_pos);
1459 value = PLMakeString(buffer);
1460 PLInsertDictionaryEntry(dock_state, dPosition, value);
1461 PLRelease(value);
1464 value = (dock->lowered ? dYes : dNo);
1465 PLInsertDictionaryEntry(dock_state, dLowered, value);
1467 if (dock->type == WM_CLIP) {
1468 value = (dock->collapsed ? dYes : dNo);
1469 PLInsertDictionaryEntry(dock_state, dCollapsed, value);
1471 value = (dock->auto_collapse ? dYes : dNo);
1472 PLInsertDictionaryEntry(dock_state, dAutoCollapse, value);
1474 value = (dock->auto_raise_lower ? dYes : dNo);
1475 PLInsertDictionaryEntry(dock_state, dAutoRaiseLower, value);
1477 value = (dock->attract_icons ? dYes : dNo);
1478 PLInsertDictionaryEntry(dock_state, dAutoAttractIcons, value);
1480 value = (dock->keep_attracted ? dYes : dNo);
1481 PLInsertDictionaryEntry(dock_state, dKeepAttracted, value);
1484 return dock_state;
1488 void
1489 wDockSaveState(WScreen *scr)
1491 proplist_t dock_state;
1493 dock_state = dockSaveState(scr->dock);
1495 PLInsertDictionaryEntry(scr->session_state, dDock, dock_state);
1497 PLRelease(dock_state);
1501 void
1502 wClipSaveState(WScreen *scr)
1504 proplist_t clip_state;
1506 clip_state = make_icon_state(scr->clip_icon);
1508 PLInsertDictionaryEntry(scr->session_state, dClip, clip_state);
1510 PLRelease(clip_state);
1514 proplist_t
1515 wClipSaveWorkspaceState(WScreen *scr, int workspace)
1517 return dockSaveState(scr->workspaces[workspace]->clip);
1521 static WAppIcon*
1522 restore_icon_state(WScreen *scr, proplist_t info, int type, int index)
1524 WAppIcon *aicon;
1525 char *wclass, *winstance;
1526 proplist_t cmd, value;
1527 char *command;
1530 cmd = PLGetDictionaryEntry(info, dCommand);
1531 if (!cmd || !PLIsString(cmd)) {
1532 return NULL;
1535 /* parse window name */
1536 value = PLGetDictionaryEntry(info, dName);
1537 if (!value)
1538 return NULL;
1540 ParseWindowName(value, &winstance, &wclass, "dock");
1542 if (!winstance && !wclass) {
1543 return NULL;
1546 /* get commands */
1548 if (cmd)
1549 command = wstrdup(PLGetString(cmd));
1550 else
1551 command = NULL;
1553 if (!command || strcmp(command, "-")==0) {
1554 if (command)
1555 free(command);
1556 if (wclass)
1557 free(wclass);
1558 if (winstance)
1559 free(winstance);
1561 return NULL;
1564 aicon = wAppIconCreateForDock(scr, command, winstance, wclass,
1565 TILE_NORMAL);
1566 if (wclass)
1567 free(wclass);
1568 if (winstance)
1569 free(winstance);
1571 aicon->icon->core->descriptor.handle_mousedown = iconMouseDown;
1572 if (type == WM_CLIP) {
1573 aicon->icon->core->descriptor.handle_enternotify = clipEnterNotify;
1574 aicon->icon->core->descriptor.handle_leavenotify = clipLeaveNotify;
1576 aicon->icon->core->descriptor.parent_type = WCLASS_DOCK_ICON;
1577 aicon->icon->core->descriptor.parent = aicon;
1580 #ifdef OFFIX_DND
1581 cmd = PLGetDictionaryEntry(info, dDropCommand);
1582 if (cmd)
1583 aicon->dnd_command = wstrdup(PLGetString(cmd));
1584 #endif
1586 /* check auto launch */
1587 value = PLGetDictionaryEntry(info, dAutoLaunch);
1589 aicon->auto_launch = 0;
1590 if (value) {
1591 if (PLIsString(value)) {
1592 if (strcasecmp(PLGetString(value), "YES")==0)
1593 aicon->auto_launch = 1;
1594 } else {
1595 wwarning(_("bad value in docked icon state info %s"),
1596 PLGetString(dAutoLaunch));
1600 /* check if it wasn't normally docked */
1601 value = PLGetDictionaryEntry(info, dForced);
1603 aicon->forced_dock = 0;
1604 if (value) {
1605 if (PLIsString(value)) {
1606 if (strcasecmp(PLGetString(value), "YES")==0)
1607 aicon->forced_dock = 1;
1608 } else {
1609 wwarning(_("bad value in docked icon state info %s"),
1610 PLGetString(dForced));
1614 /* check if we can rely on the stuff in the app */
1615 value = PLGetDictionaryEntry(info, dBuggyApplication);
1617 aicon->buggy_app = 0;
1618 if (value) {
1619 if (PLIsString(value)) {
1620 if (strcasecmp(PLGetString(value), "YES")==0)
1621 aicon->buggy_app = 1;
1622 } else {
1623 wwarning(_("bad value in docked icon state info %s"),
1624 PLGetString(dBuggyApplication));
1628 /* get position in the dock */
1629 value = PLGetDictionaryEntry(info, dPosition);
1630 if (value && PLIsString(value)) {
1631 if (sscanf(PLGetString(value), "%hi,%hi", &aicon->xindex,
1632 &aicon->yindex)!=2)
1633 wwarning(_("bad value in docked icon state info %s"),
1634 PLGetString(dPosition));
1636 /* check position sanity */
1637 /* incomplete section! */
1638 if (type == WM_DOCK) {
1639 aicon->xindex = 0;
1640 if (aicon->yindex < 0)
1641 wwarning(_("bad value in docked icon position %i,%i"),
1642 aicon->xindex, aicon->yindex);
1644 } else {
1645 aicon->yindex = index;
1646 aicon->xindex = 0;
1649 /* check if icon is omnipresent */
1650 value = PLGetDictionaryEntry(info, dOmnipresent);
1652 aicon->omnipresent = 0;
1653 if (value) {
1654 if (PLIsString(value)) {
1655 if (strcasecmp(PLGetString(value), "YES")==0)
1656 aicon->omnipresent = 1;
1657 } else {
1658 wwarning(_("bad value in docked icon state info %s"),
1659 PLGetString(dOmnipresent));
1663 aicon->running = 0;
1664 aicon->docked = 1;
1666 return aicon;
1670 #define COMPLAIN(key) wwarning(_("bad value in dock state info:%s"), key)
1673 WAppIcon*
1674 wClipRestoreState(WScreen *scr, proplist_t clip_state)
1676 WAppIcon *icon;
1677 proplist_t value;
1680 icon = mainIconCreate(scr, WM_CLIP);
1682 if (!clip_state)
1683 return icon;
1684 else
1685 PLRetain(clip_state);
1687 /* restore position */
1689 value = PLGetDictionaryEntry(clip_state, dPosition);
1691 if (value) {
1692 if (!PLIsString(value))
1693 COMPLAIN("Position");
1694 else {
1695 if (sscanf(PLGetString(value), "%i,%i", &icon->x_pos,
1696 &icon->y_pos)!=2)
1697 COMPLAIN("Position");
1699 /* check position sanity */
1700 if (icon->y_pos < 0)
1701 icon->y_pos = 0;
1702 else if (icon->y_pos > scr->scr_height-ICON_SIZE)
1703 icon->y_pos = scr->scr_height-ICON_SIZE;
1705 if (icon->x_pos < 0)
1706 icon->x_pos = 0;
1707 else if (icon->x_pos > scr->scr_width-ICON_SIZE)
1708 icon->x_pos = scr->scr_width-ICON_SIZE;
1712 #ifdef OFFIX_DND
1713 value = PLGetDictionaryEntry(clip_state, dDropCommand);
1714 if (value && PLIsString(value))
1715 icon->dnd_command = wstrdup(PLGetString(value));
1716 #endif
1718 PLRelease(clip_state);
1720 return icon;
1724 WDock*
1725 wDockRestoreState(WScreen *scr, proplist_t dock_state, int type)
1727 WDock *dock;
1728 proplist_t apps;
1729 proplist_t value;
1730 WAppIcon *aicon, *old_top;
1731 int count, i;
1734 dock = wDockCreate(scr, type);
1736 if (!dock_state)
1737 return dock;
1739 if (dock_state)
1740 PLRetain(dock_state);
1743 /* restore position */
1745 value = PLGetDictionaryEntry(dock_state, dPosition);
1747 if (value) {
1748 if (!PLIsString(value))
1749 COMPLAIN("Position");
1750 else {
1751 if (sscanf(PLGetString(value), "%i,%i", &dock->x_pos,
1752 &dock->y_pos)!=2)
1753 COMPLAIN("Position");
1755 /* check position sanity */
1756 if (dock->y_pos < 0)
1757 dock->y_pos = 0;
1758 else if (dock->y_pos > scr->scr_height-ICON_SIZE)
1759 dock->y_pos = scr->scr_height - ICON_SIZE;
1761 /* This is no more needed. ??? */
1762 if (type == WM_CLIP) {
1763 if (dock->x_pos < 0)
1764 dock->x_pos = 0;
1765 else if (dock->x_pos > scr->scr_width-ICON_SIZE)
1766 dock->x_pos = scr->scr_width-ICON_SIZE;
1767 } else {
1768 if (dock->x_pos >= 0) {
1769 dock->x_pos = DOCK_EXTRA_SPACE;
1770 dock->on_right_side = 0;
1771 } else {
1772 dock->x_pos = scr->scr_width - DOCK_EXTRA_SPACE - ICON_SIZE;
1773 dock->on_right_side = 1;
1779 /* restore lowered/raised state */
1781 dock->lowered = 0;
1783 value = PLGetDictionaryEntry(dock_state, dLowered);
1785 if (value) {
1786 if (!PLIsString(value))
1787 COMPLAIN("Lowered");
1788 else {
1789 if (strcasecmp(PLGetString(value), "YES")==0)
1790 dock->lowered = 1;
1795 /* restore collapsed state */
1797 dock->collapsed = 0;
1799 value = PLGetDictionaryEntry(dock_state, dCollapsed);
1801 if (value) {
1802 if (!PLIsString(value))
1803 COMPLAIN("Collapsed");
1804 else {
1805 if (strcasecmp(PLGetString(value), "YES")==0)
1806 dock->collapsed = 1;
1811 /* restore auto-collapsed state */
1813 value = PLGetDictionaryEntry(dock_state, dAutoCollapse);
1815 if (value) {
1816 if (!PLIsString(value))
1817 COMPLAIN("AutoCollapse");
1818 else {
1819 if (strcasecmp(PLGetString(value), "YES")==0) {
1820 dock->auto_collapse = 1;
1821 dock->collapsed = 1;
1827 /* restore auto-raise/lower state */
1829 value = PLGetDictionaryEntry(dock_state, dAutoRaiseLower);
1831 if (value) {
1832 if (!PLIsString(value))
1833 COMPLAIN("AutoRaiseLower");
1834 else {
1835 if (strcasecmp(PLGetString(value), "YES")==0) {
1836 dock->auto_raise_lower = 1;
1841 /* restore attract icons state */
1843 dock->attract_icons = 0;
1845 value = PLGetDictionaryEntry(dock_state, dAutoAttractIcons);
1847 if (value) {
1848 if (!PLIsString(value))
1849 COMPLAIN("AutoAttractIcons");
1850 else {
1851 if (strcasecmp(PLGetString(value), "YES")==0)
1852 dock->attract_icons = 1;
1857 /* restore keep attracted icons state */
1859 dock->keep_attracted = 0;
1861 value = PLGetDictionaryEntry(dock_state, dKeepAttracted);
1863 if (value) {
1864 if (!PLIsString(value))
1865 COMPLAIN("KeepAttracted");
1866 else {
1867 if (strcasecmp(PLGetString(value), "YES")==0)
1868 dock->keep_attracted = 1;
1873 /* application list */
1875 apps = PLGetDictionaryEntry(dock_state, dApplications);
1877 if (!apps) {
1878 goto finish;
1881 count = PLGetNumberOfElements(apps);
1883 if (count==0)
1884 goto finish;
1886 old_top = dock->icon_array[0];
1888 /* dock->icon_count is set to 1 when dock is created.
1889 * Since Clip is already restored, we want to keep it so for clip,
1890 * but for dock we may change the default top tile, so we set it to 0.
1892 if (type == WM_DOCK)
1893 dock->icon_count = 0;
1895 for (i=0; i<count; i++) {
1896 if (dock->icon_count >= dock->max_icons) {
1897 wwarning(_("there are too many icons stored in dock. Ignoring what doesn't fit"));
1898 break;
1901 value = PLGetArrayElement(apps, i);
1902 aicon = restore_icon_state(scr, value, type, dock->icon_count);
1904 dock->icon_array[dock->icon_count] = aicon;
1906 if (aicon) {
1907 aicon->dock = dock;
1908 aicon->x_pos = dock->x_pos + (aicon->xindex*ICON_SIZE);
1909 aicon->y_pos = dock->y_pos + (aicon->yindex*ICON_SIZE);
1911 if (dock->lowered)
1912 ChangeStackingLevel(aicon->icon->core, WMNormalLevel);
1913 else
1914 ChangeStackingLevel(aicon->icon->core, WMDockLevel);
1916 wCoreConfigure(aicon->icon->core, aicon->x_pos, aicon->y_pos,
1917 0, 0);
1919 if (!dock->collapsed)
1920 XMapWindow(dpy, aicon->icon->core->window);
1921 wRaiseFrame(aicon->icon->core);
1923 dock->icon_count++;
1924 } else if (dock->icon_count==0 && type==WM_DOCK)
1925 dock->icon_count++;
1928 /* if the first icon is not defined, use the default */
1929 if (dock->icon_array[0]==NULL) {
1930 /* update default icon */
1931 old_top->x_pos = dock->x_pos;
1932 old_top->y_pos = dock->y_pos;
1933 if (dock->lowered)
1934 ChangeStackingLevel(old_top->icon->core, WMNormalLevel);
1935 else
1936 ChangeStackingLevel(old_top->icon->core, WMDockLevel);
1937 dock->icon_array[0] = old_top;
1938 XMoveWindow(dpy, old_top->icon->core->window, dock->x_pos, dock->y_pos);
1939 /* we don't need to increment dock->icon_count here because it was
1940 * incremented in the loop above.
1942 } else if (old_top!=dock->icon_array[0]) {
1943 if (old_top == scr->clip_icon)
1944 scr->clip_icon = dock->icon_array[0];
1945 wAppIconDestroy(old_top);
1948 finish:
1949 if (dock_state)
1950 PLRelease(dock_state);
1952 return dock;
1957 void
1958 wDockLaunchWithState(WDock *dock, WAppIcon *btn, WSavedState *state)
1960 if (btn && btn->command && !btn->running && !btn->launching) {
1962 btn->drop_launch = 0;
1964 btn->pid = execCommand(btn, btn->command, state);
1966 if (btn->pid>0) {
1967 if (!btn->forced_dock && !btn->buggy_app) {
1968 btn->launching = 1;
1969 dockIconPaint(btn);
1972 } else {
1973 free(state);
1978 void
1979 wDockDoAutoLaunch(WDock *dock, int workspace)
1981 WAppIcon *btn;
1982 WSavedState *state;
1983 int i;
1985 for (i = 0; i < dock->max_icons; i++) {
1986 btn = dock->icon_array[i];
1987 if (!btn || !btn->auto_launch)
1988 continue;
1990 state = wmalloc(sizeof(WSavedState));
1991 memset(state, 0, sizeof(WSavedState));
1992 state->workspace = workspace;
1993 /* TODO: this is klugy and is very difficult to understand
1994 * what's going on. Try to clean up */
1995 wDockLaunchWithState(dock, btn, state);
1999 #ifdef REDUCE_APPICONS
2000 void
2001 wDockSimulateLaunch(WDock *dock, WAppIcon *btn)
2003 if ((btn == NULL) || (dock == NULL))
2004 return;
2006 if (!btn->running) {
2007 if ((btn->icon->owner == NULL) && (btn->applist))
2008 btn->icon->owner = btn->applist->wapp->main_window_desc;
2009 if (!btn->forced_dock)
2010 btn->launching = 1;
2011 dockIconPaint(btn);
2012 wusleep(5000);
2015 #endif
2017 #ifdef OFFIX_DND
2018 static WDock*
2019 findDock(WScreen *scr, XEvent *event, int *icon_pos)
2021 WDock *dock;
2022 int i;
2024 *icon_pos = -1;
2025 if ((dock = scr->dock)!=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 && (dock = scr->workspaces[scr->current_workspace]->clip)!=NULL) {
2035 for (i=0; i<dock->max_icons; i++) {
2036 if (dock->icon_array[i]
2037 && dock->icon_array[i]->icon->core->window==event->xclient.window) {
2038 *icon_pos = i;
2039 break;
2043 if(*icon_pos>=0)
2044 return dock;
2045 return NULL;
2050 wDockReceiveDNDDrop(WScreen *scr, XEvent *event)
2052 WDock *dock;
2053 WAppIcon *btn;
2054 int icon_pos;
2056 dock = findDock(scr, event, &icon_pos);
2057 if (!dock)
2058 return False;
2061 * Return True if the drop was on an application icon window.
2062 * In this case, let the ClientMessage handler redirect the
2063 * message to the app.
2065 if (dock->icon_array[icon_pos]->icon->icon_win!=None)
2066 return True;
2068 if (dock->icon_array[icon_pos]->dnd_command!=NULL) {
2069 scr->flags.dnd_data_convertion_status = 0;
2071 btn = dock->icon_array[icon_pos];
2073 if (!btn->forced_dock) {
2074 btn->relaunching = btn->running;
2075 btn->running = 1;
2077 if (btn->wm_instance || btn->wm_class) {
2078 WWindowAttributes attr;
2079 memset(&attr, 0, sizeof(WWindowAttributes));
2080 wDefaultFillAttributes(btn->icon->core->screen_ptr,
2081 btn->wm_instance,
2082 btn->wm_class, &attr, NULL, True);
2084 if (!attr.no_appicon)
2085 btn->launching = 1;
2086 else
2087 btn->running = 0;
2090 btn->drop_launch = 1;
2091 scr->last_dock = dock;
2092 btn->pid = execCommand(btn, btn->dnd_command, NULL);
2093 if (btn->pid>0) {
2094 dockIconPaint(btn);
2095 } else {
2096 btn->launching = 0;
2097 if (!btn->relaunching) {
2098 btn->running = 0;
2102 return False;
2104 #endif /* OFFIX_DND */
2108 Bool
2109 wDockAttachIcon(WDock *dock, WAppIcon *icon, int x, int y)
2111 WWindow *wwin;
2112 char **argv;
2113 int argc;
2114 int index;
2116 wwin = icon->icon->owner;
2117 if (icon->command==NULL) {
2118 icon->editing = 0;
2119 if (XGetCommand(dpy, wwin->client_win, &argv, &argc) && argc>0) {
2121 icon->command = FlattenStringList(argv, argc);
2122 XFreeStringList(argv);
2123 } else {
2124 char *command=NULL;
2126 /* icon->forced_dock = 1;*/
2127 if (!icon->attracted || dock->type!=WM_CLIP || dock->keep_attracted) {
2128 icon->editing = 1;
2129 if (wInputDialog(dock->screen_ptr, _("Dock Icon"),
2130 _("Type the command used to launch the application"),
2131 &command)) {
2132 if (command && (command[0]==0 ||
2133 (command[0]=='-' && command[1]==0))) {
2134 free(command);
2135 command = NULL;
2137 icon->command = command;
2138 icon->editing = 0;
2139 } else {
2140 icon->editing = 0;
2141 if (command)
2142 free(command);
2143 /* If the target is the dock, reject the icon. If
2144 * the target is the clip, make it an attracted icon
2146 if (dock->type==WM_CLIP) {
2147 icon->attracted = 1;
2148 if (!icon->icon->shadowed) {
2149 icon->icon->shadowed = 1;
2150 icon->icon->force_paint = 1;
2152 } else {
2153 return False;
2158 } else {
2159 icon->editing = 0;
2162 for (index=1; index<dock->max_icons; index++)
2163 if (dock->icon_array[index] == NULL)
2164 break;
2165 /* if (index == dock->max_icons)
2166 return; */
2168 assert(index < dock->max_icons);
2170 dock->icon_array[index] = icon;
2171 icon->yindex = y;
2172 icon->xindex = x;
2174 icon->omnipresent = 0;
2176 icon->x_pos = dock->x_pos + x*ICON_SIZE;
2177 icon->y_pos = dock->y_pos + y*ICON_SIZE;
2179 dock->icon_count++;
2181 icon->running = 1;
2182 icon->launching = 0;
2183 icon->docked = 1;
2184 icon->dock = dock;
2185 icon->icon->core->descriptor.handle_mousedown = iconMouseDown;
2186 if (dock->type == WM_CLIP) {
2187 icon->icon->core->descriptor.handle_enternotify = clipEnterNotify;
2188 icon->icon->core->descriptor.handle_leavenotify = clipLeaveNotify;
2190 icon->icon->core->descriptor.parent_type = WCLASS_DOCK_ICON;
2191 icon->icon->core->descriptor.parent = icon;
2193 MoveInStackListUnder(dock->icon_array[index-1]->icon->core,
2194 icon->icon->core);
2195 wAppIconMove(icon, icon->x_pos, icon->y_pos);
2196 wAppIconPaint(icon);
2198 if (wPreferences.auto_arrange_icons)
2199 wArrangeIcons(dock->screen_ptr, True);
2201 #ifdef OFFIX_DND
2202 if (icon->command && !icon->dnd_command) {
2203 icon->dnd_command = wmalloc(strlen(icon->command)+8);
2204 sprintf(icon->dnd_command, "%s %%d", icon->command);
2206 #endif
2208 return True;
2212 void
2213 reattachIcon(WDock *dock, WAppIcon *icon, int x, int y)
2215 int index;
2217 for(index=1; index<dock->max_icons; index++) {
2218 if(dock->icon_array[index] == icon)
2219 break;
2221 assert(index < dock->max_icons);
2223 icon->yindex = y;
2224 icon->xindex = x;
2226 icon->x_pos = dock->x_pos + x*ICON_SIZE;
2227 icon->y_pos = dock->y_pos + y*ICON_SIZE;
2231 Bool
2232 moveIconBetweenDocks(WDock *src, WDock *dest, WAppIcon *icon, int x, int y)
2234 WWindow *wwin;
2235 char **argv;
2236 int argc;
2237 int index;
2239 if (src == dest)
2240 return True; /* No move needed, we're already there */
2242 if (dest == NULL)
2243 return False;
2245 wwin = icon->icon->owner;
2248 * For the moment we can't do this if we move icons in Clip from one
2249 * workspace to other, because if we move two or more icons without
2250 * command, the dialog box will not be able to tell us to which of the
2251 * moved icons it applies. -Dan
2253 if ((dest->type==WM_DOCK /*|| dest->keep_attracted*/) && icon->command==NULL) {
2254 if (XGetCommand(dpy, wwin->client_win, &argv, &argc) && argc>0) {
2256 icon->command = FlattenStringList(argv, argc);
2257 XFreeStringList(argv);
2258 } else {
2259 char *command=NULL;
2261 icon->editing = 1;
2262 /* icon->forced_dock = 1;*/
2263 if (wInputDialog(src->screen_ptr, _("Dock Icon"),
2264 _("Type the command used to launch the application"),
2265 &command)) {
2266 if (command && (command[0]==0 ||
2267 (command[0]=='-' && command[1]==0))) {
2268 free(command);
2269 command = NULL;
2271 icon->command = command;
2272 } else {
2273 icon->editing = 0;
2274 if (command)
2275 free(command);
2276 return False;
2278 icon->editing = 0;
2282 if (dest->type == WM_DOCK)
2283 wClipMakeIconOmnipresent(icon, False);
2285 for(index=1; index<src->max_icons; index++) {
2286 if(src->icon_array[index] == icon)
2287 break;
2289 assert(index < src->max_icons);
2291 src->icon_array[index] = NULL;
2292 src->icon_count--;
2294 for(index=1; index<dest->max_icons; index++) {
2295 if(dest->icon_array[index] == NULL)
2296 break;
2298 /* if (index == dest->max_icons)
2299 return; */
2301 assert(index < dest->max_icons);
2303 dest->icon_array[index] = icon;
2304 icon->dock = dest;
2306 /* deselect the icon */
2307 if (icon->icon->selected)
2308 wIconSelect(icon->icon);
2310 if (dest->type == WM_DOCK) {
2311 icon->icon->core->descriptor.handle_enternotify = NULL;
2312 icon->icon->core->descriptor.handle_leavenotify = NULL;
2313 } else {
2314 icon->icon->core->descriptor.handle_enternotify = clipEnterNotify;
2315 icon->icon->core->descriptor.handle_leavenotify = clipLeaveNotify;
2318 /* set it to be kept when moving to dock, or to a clip that keep the
2319 * attracted icons.
2320 * Unless the icon does not have a command set
2322 if (icon->command && (dest->type==WM_DOCK || dest->keep_attracted)) {
2323 icon->attracted = 0;
2324 if (icon->icon->shadowed) {
2325 icon->icon->shadowed = 0;
2326 icon->icon->force_paint = 1;
2330 if (src->auto_collapse || src->auto_raise_lower)
2331 clipLeave(src);
2333 icon->yindex = y;
2334 icon->xindex = x;
2336 icon->x_pos = dest->x_pos + x*ICON_SIZE;
2337 icon->y_pos = dest->y_pos + y*ICON_SIZE;
2339 dest->icon_count++;
2341 MoveInStackListUnder(dest->icon_array[index-1]->icon->core,
2342 icon->icon->core);
2343 wAppIconPaint(icon);
2345 return True;
2349 void
2350 wDockDetach(WDock *dock, WAppIcon *icon)
2352 int index;
2354 /* make the settings panel be closed */
2355 if (icon->panel) {
2356 DestroyDockAppSettingsPanel(icon->panel);
2359 icon->docked = 0;
2360 icon->dock = NULL;
2361 icon->attracted = 0;
2362 if (icon->icon->shadowed) {
2363 icon->icon->shadowed = 0;
2364 icon->icon->force_paint = 1;
2367 /* This must be called before dock->icon_array[index] is set to NULL.
2368 * Don't move it. -Dan
2370 wClipMakeIconOmnipresent(icon, False);
2372 /* deselect the icon */
2373 if (icon->icon->selected)
2374 wIconSelect(icon->icon);
2376 if (icon->command) {
2377 free(icon->command);
2378 icon->command = NULL;
2380 #ifdef OFFIX_DND
2381 if (icon->dnd_command) {
2382 free(icon->dnd_command);
2383 icon->dnd_command = NULL;
2385 #endif
2387 for (index=1; index<dock->max_icons; index++)
2388 if (dock->icon_array[index] == icon)
2389 break;
2390 assert(index < dock->max_icons);
2391 dock->icon_array[index] = NULL;
2392 icon->yindex = -1;
2393 icon->xindex = -1;
2395 dock->icon_count--;
2397 /* if the dock is not attached to an application or
2398 * the the application did not set the approriate hints yet,
2399 * destroy the icon */
2400 #ifdef REDUCE_APPICONS
2401 if ((icon->num_apps == 0) && (!icon->running || !wApplicationOf(icon->main_window)) )
2402 #else
2403 if (!icon->running || !wApplicationOf(icon->main_window))
2404 #endif
2405 wAppIconDestroy(icon);
2406 else {
2407 icon->icon->core->descriptor.handle_mousedown = appIconMouseDown;
2408 icon->icon->core->descriptor.handle_enternotify = NULL;
2409 icon->icon->core->descriptor.handle_leavenotify = NULL;
2410 icon->icon->core->descriptor.parent_type = WCLASS_APPICON;
2411 icon->icon->core->descriptor.parent = icon;
2413 ChangeStackingLevel(icon->icon->core, NORMAL_ICON_LEVEL);
2415 wAppIconPaint(icon);
2416 if (wPreferences.auto_arrange_icons) {
2417 wArrangeIcons(dock->screen_ptr, True);
2420 if (dock->auto_collapse || dock->auto_raise_lower)
2421 clipLeave(dock);
2426 * returns the closest Dock slot index for the passed
2427 * coordinates.
2429 * Returns False if icon can't be docked.
2431 * Note: this function should NEVER alter ret_x or ret_y, unless it will
2432 * return True. -Dan
2434 Bool
2435 wDockSnapIcon(WDock *dock, WAppIcon *icon, int req_x, int req_y,
2436 int *ret_x, int *ret_y, int redocking)
2438 WScreen *scr = dock->screen_ptr;
2439 int dx, dy;
2440 int ex_x, ex_y;
2441 int i, offset = ICON_SIZE/2;
2442 WAppIcon *aicon = NULL;
2443 WAppIcon *nicon = NULL;
2444 int max_y_icons, max_x_icons;
2446 max_x_icons = scr->scr_width/ICON_SIZE;
2447 max_y_icons = scr->scr_height/ICON_SIZE-1;
2449 if (wPreferences.flags.noupdates)
2450 return False;
2452 dx = dock->x_pos;
2453 dy = dock->y_pos;
2455 /* if the dock is full */
2456 if (!redocking &&
2457 (dock->icon_count >= dock->max_icons)) {
2458 return False;
2461 /* exact position */
2462 if (req_y < dy)
2463 ex_y = (req_y - offset - dy)/ICON_SIZE;
2464 else
2465 ex_y = (req_y + offset - dy)/ICON_SIZE;
2467 if (req_x < dx)
2468 ex_x = (req_x - offset - dx)/ICON_SIZE;
2469 else
2470 ex_x = (req_x + offset - dx)/ICON_SIZE;
2472 /* check if the icon is outside the screen boundaries */
2473 if (dx + ex_x*ICON_SIZE < -ICON_SIZE+2 ||
2474 dx + ex_x*ICON_SIZE >= scr->scr_width-1 ||
2475 dy + ex_y*ICON_SIZE < -ICON_SIZE+2 ||
2476 dy + ex_y*ICON_SIZE >= scr->scr_height-1)
2477 return False;
2479 if (dock->type == WM_DOCK) {
2480 if (icon->dock != dock && ex_x != 0)
2481 return False;
2483 aicon = NULL;
2484 for (i=0; i<dock->max_icons; i++) {
2485 nicon = dock->icon_array[i];
2486 if (nicon && nicon->yindex == ex_y) {
2487 aicon = nicon;
2488 break;
2492 if (redocking) {
2493 int sig, done, closest;
2495 /* Possible cases when redocking:
2497 * icon dragged out of range of any slot -> false
2498 * icon dragged to range of free slot
2499 * icon dragged to range of same slot
2500 * icon dragged to range of different icon
2502 if (abs(ex_x) > DOCK_DETTACH_THRESHOLD)
2503 return False;
2505 if (ex_y>=0 && ex_y<=max_y_icons && (aicon==icon || !aicon)) {
2506 *ret_x = 0;
2507 *ret_y = ex_y;
2508 return True;
2511 /* start looking at the upper slot or lower? */
2512 if (ex_y*ICON_SIZE < (req_y + offset - dy))
2513 sig = 1;
2514 else
2515 sig = -1;
2517 closest = -1;
2518 done = 0;
2519 /* look for closest free slot */
2520 for (i=0; i<(DOCK_DETTACH_THRESHOLD+1)*2 && !done; i++) {
2521 int j;
2523 done = 1;
2524 closest = sig*(i/2) + ex_y;
2525 /* check if this slot is used */
2526 if (closest >= 0) {
2527 for (j = 0; j<dock->max_icons; j++) {
2528 if (dock->icon_array[j]
2529 && dock->icon_array[j]->yindex==closest) {
2530 /* slot is used by someone else */
2531 if (dock->icon_array[j]!=icon)
2532 done = 0;
2533 break;
2537 sig = -sig;
2539 if (done && closest >= 0 && closest <= max_y_icons &&
2540 ((ex_y >= closest && ex_y - closest < DOCK_DETTACH_THRESHOLD+1)
2542 (ex_y < closest && closest - ex_y <= DOCK_DETTACH_THRESHOLD+1))) {
2543 *ret_x = 0;
2544 *ret_y = closest;
2545 return True;
2547 } else { /* !redocking */
2549 /* if slot is free and the icon is close enough, return it */
2550 if (!aicon && ex_x == 0 && ex_y >= 0 && ex_y <= max_y_icons) {
2551 *ret_x = 0;
2552 *ret_y = ex_y;
2553 return True;
2556 } else { /* CLIP */
2557 int neighbours = 0;
2558 int start, stop, k;
2560 start = icon->omnipresent ? 0 : scr->current_workspace;
2561 stop = icon->omnipresent ? scr->workspace_count : start+1;
2563 aicon = NULL;
2564 for (k=start; k<stop; k++) {
2565 WDock *tmp = scr->workspaces[k]->clip;
2566 if (!tmp)
2567 continue;
2568 for (i=0; i<tmp->max_icons; i++) {
2569 nicon = tmp->icon_array[i];
2570 if (nicon && nicon->xindex == ex_x && nicon->yindex == ex_y) {
2571 aicon = nicon;
2572 break;
2575 if (aicon)
2576 break;
2578 for (k=start; k<stop; k++) {
2579 WDock *tmp = scr->workspaces[k]->clip;
2580 if (!tmp)
2581 continue;
2582 for (i=0; i<tmp->max_icons; i++) {
2583 nicon = tmp->icon_array[i];
2584 if (nicon && nicon != icon && /* Icon can't be it's own neighbour */
2585 (abs(nicon->xindex - ex_x) <= CLIP_ATTACH_VICINITY &&
2586 abs(nicon->yindex - ex_y) <= CLIP_ATTACH_VICINITY)) {
2587 neighbours = 1;
2588 break;
2591 if (neighbours)
2592 break;
2595 if (neighbours && (aicon==NULL || (redocking && aicon == icon))) {
2596 *ret_x = ex_x;
2597 *ret_y = ex_y;
2598 return True;
2601 return False;
2604 #define MIN(x, y) ((x) > (y) ? (y) : (x))
2605 #define MAX(x, y) ((x) < (y) ? (y) : (x))
2607 #define ON_SCREEN(x, y, sx, ex, sy, ey) \
2608 ((((x)+ICON_SIZE/2) >= (sx)) && (((y)+ICON_SIZE/2) >= (sy)) && \
2609 (((x) + (ICON_SIZE/2)) <= (ex)) && (((y) + (ICON_SIZE/2)) <= ey))
2613 * returns true if it can find a free slot in the dock,
2614 * in which case it changes x_pos and y_pos accordingly.
2615 * Else returns false.
2617 Bool
2618 wDockFindFreeSlot(WDock *dock, int *x_pos, int *y_pos)
2620 WScreen *scr = dock->screen_ptr;
2621 WAppIcon *btn;
2622 WAppIconChain *chain;
2623 unsigned char *slot_map;
2624 int mwidth;
2625 int r;
2626 int x, y;
2627 int i, done = False;
2628 int corner;
2629 int sx=0, sy=0, ex=scr->scr_width, ey=scr->scr_height;
2630 int extra_count=0;
2632 if (dock->type == WM_CLIP &&
2633 dock != scr->workspaces[scr->current_workspace]->clip)
2634 extra_count = scr->global_icon_count;
2636 /* if the dock is full */
2637 if (dock->icon_count+extra_count >= dock->max_icons) {
2638 return False;
2641 if (!wPreferences.flags.nodock && scr->dock) {
2642 if (scr->dock->on_right_side)
2643 ex -= ICON_SIZE + DOCK_EXTRA_SPACE;
2644 else
2645 sx += ICON_SIZE + DOCK_EXTRA_SPACE;
2648 if (ex < dock->x_pos)
2649 ex = dock->x_pos;
2650 if (sx > dock->x_pos+ICON_SIZE)
2651 sx = dock->x_pos+ICON_SIZE;
2652 #define C_NONE 0
2653 #define C_NW 1
2654 #define C_NE 2
2655 #define C_SW 3
2656 #define C_SE 4
2658 /* check if clip is in a corner */
2659 if (dock->type==WM_CLIP) {
2660 if (dock->x_pos < 1 && dock->y_pos < 1)
2661 corner = C_NE;
2662 else if (dock->x_pos < 1 && dock->y_pos >= (ey-ICON_SIZE))
2663 corner = C_SE;
2664 else if (dock->x_pos >= (ex-ICON_SIZE)&& dock->y_pos >= (ey-ICON_SIZE))
2665 corner = C_SW;
2666 else if (dock->x_pos >= (ex-ICON_SIZE) && dock->y_pos < 1)
2667 corner = C_NW;
2668 else
2669 corner = C_NONE;
2670 } else
2671 corner = C_NONE;
2673 /* If the clip is in the corner, use only slots that are in the border
2674 * of the screen */
2675 if (corner!=C_NONE) {
2676 char *hmap, *vmap;
2677 int hcount, vcount;
2679 hcount = MIN(dock->max_icons, scr->scr_width/ICON_SIZE);
2680 vcount = MIN(dock->max_icons, scr->scr_height/ICON_SIZE);
2681 hmap = wmalloc(hcount+1);
2682 memset(hmap, 0, hcount+1);
2683 vmap = wmalloc(vcount+1);
2684 memset(vmap, 0, vcount+1);
2686 /* mark used positions */
2687 switch (corner) {
2688 case C_NE:
2689 for (i=0; i<dock->max_icons; i++) {
2690 btn = dock->icon_array[i];
2691 if (!btn)
2692 continue;
2694 if (btn->xindex==0 && btn->yindex > 0 && btn->yindex < vcount)
2695 vmap[btn->yindex] = 1;
2696 else if (btn->yindex==0 && btn->xindex>0 && btn->xindex<hcount)
2697 hmap[btn->xindex] = 1;
2699 for (chain=scr->global_icons; chain!=NULL; chain=chain->next) {
2700 btn = chain->aicon;
2701 if (btn->xindex==0 && btn->yindex > 0 && btn->yindex < vcount)
2702 vmap[btn->yindex] = 1;
2703 else if (btn->yindex==0 && btn->xindex>0 && btn->xindex<hcount)
2704 hmap[btn->xindex] = 1;
2706 break;
2707 case C_NW:
2708 for (i=0; i<dock->max_icons; i++) {
2709 btn = dock->icon_array[i];
2710 if (!btn)
2711 continue;
2713 if (btn->xindex==0 && btn->yindex > 0 && btn->yindex < vcount)
2714 vmap[btn->yindex] = 1;
2715 else if (btn->yindex==0 && btn->xindex<0 &&btn->xindex>-hcount)
2716 hmap[-btn->xindex] = 1;
2718 for (chain=scr->global_icons; chain!=NULL; chain=chain->next) {
2719 btn = chain->aicon;
2720 if (btn->xindex==0 && btn->yindex > 0 && btn->yindex < vcount)
2721 vmap[btn->yindex] = 1;
2722 else if (btn->yindex==0 && btn->xindex<0 &&btn->xindex>-hcount)
2723 hmap[-btn->xindex] = 1;
2725 break;
2726 case C_SE:
2727 for (i=0; i<dock->max_icons; i++) {
2728 btn = dock->icon_array[i];
2729 if (!btn)
2730 continue;
2732 if (btn->xindex==0 && btn->yindex < 0 && btn->yindex > -vcount)
2733 vmap[-btn->yindex] = 1;
2734 else if (btn->yindex==0 && btn->xindex>0 && btn->xindex<hcount)
2735 hmap[btn->xindex] = 1;
2737 for (chain=scr->global_icons; chain!=NULL; chain=chain->next) {
2738 btn = chain->aicon;
2739 if (btn->xindex==0 && btn->yindex < 0 && btn->yindex > -vcount)
2740 vmap[-btn->yindex] = 1;
2741 else if (btn->yindex==0 && btn->xindex>0 && btn->xindex<hcount)
2742 hmap[btn->xindex] = 1;
2744 break;
2745 case C_SW:
2746 default:
2747 for (i=0; i<dock->max_icons; i++) {
2748 btn = dock->icon_array[i];
2749 if (!btn)
2750 continue;
2752 if (btn->xindex==0 && btn->yindex < 0 && btn->yindex > -vcount)
2753 vmap[-btn->yindex] = 1;
2754 else if (btn->yindex==0 && btn->xindex<0 &&btn->xindex>-hcount)
2755 hmap[-btn->xindex] = 1;
2757 for (chain=scr->global_icons; chain!=NULL; chain=chain->next) {
2758 btn = chain->aicon;
2759 if (btn->xindex==0 && btn->yindex < 0 && btn->yindex > -vcount)
2760 vmap[-btn->yindex] = 1;
2761 else if (btn->yindex==0 && btn->xindex<0 &&btn->xindex>-hcount)
2762 hmap[-btn->xindex] = 1;
2765 x=0; y=0;
2766 done = 0;
2767 /* search a vacant slot */
2768 for (i=1; i<MAX(vcount, hcount); i++) {
2769 if (i < vcount && vmap[i]==0) {
2770 /* found a slot */
2771 x = 0;
2772 y = i;
2773 done = 1;
2774 break;
2775 } else if (i < hcount && hmap[i]==0) {
2776 /* found a slot */
2777 x = i;
2778 y = 0;
2779 done = 1;
2780 break;
2783 free(vmap);
2784 free(hmap);
2785 /* If found a slot, translate and return */
2786 if (done) {
2787 if (corner==C_NW || corner==C_NE) {
2788 *y_pos = y;
2789 } else {
2790 *y_pos = -y;
2792 if (corner==C_NE || corner==C_SE) {
2793 *x_pos = x;
2794 } else {
2795 *x_pos = -x;
2797 return True;
2799 /* else, try to find a slot somewhere else */
2802 /* a map of mwidth x mwidth would be enough if we allowed icons to be
2803 * placed outside of screen */
2804 mwidth = (int)ceil(sqrt(dock->max_icons));
2806 /* In the worst case (the clip is in the corner of the screen),
2807 * the amount of icons that fit in the clip is smaller.
2808 * Double the map to get a safe value.
2810 mwidth += mwidth;
2812 r = (mwidth-1)/2;
2814 slot_map = wmalloc(mwidth*mwidth);
2815 memset(slot_map, 0, mwidth*mwidth);
2817 #define XY2OFS(x,y) (MAX(abs(x),abs(y)) > r) ? 0 : (((y)+r)*(mwidth)+(x)+r)
2819 /* mark used slots in the map. If the slot falls outside the map
2820 * (for example, when all icons are placed in line), ignore them. */
2821 for (i=0; i<dock->max_icons; i++) {
2822 btn = dock->icon_array[i];
2823 if (btn)
2824 slot_map[XY2OFS(btn->xindex, btn->yindex)] = 1;
2826 for (chain=scr->global_icons; chain!=NULL; chain=chain->next) {
2827 slot_map[XY2OFS(chain->aicon->xindex, chain->aicon->yindex)] = 1;
2829 /* Find closest slot from the center that is free by scanning the
2830 * map from the center to outward in circular passes.
2831 * This will not result in a neat layout, but will be optimal
2832 * in the sense that there will not be holes left.
2834 done = 0;
2835 for (i = 1; i <= r && !done; i++) {
2836 int tx, ty;
2838 /* top and bottom parts of the ring */
2839 for (x = -i; x <= i && !done; x++) {
2840 tx = dock->x_pos + x*ICON_SIZE;
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;
2850 y = i;
2851 ty = dock->y_pos + y*ICON_SIZE;
2852 if (slot_map[XY2OFS(x,y)]==0
2853 && ON_SCREEN(tx, ty, sx, ex, sy, ey)) {
2854 *x_pos = x;
2855 *y_pos = y;
2856 done = 1;
2857 break;
2860 /* left and right parts of the ring */
2861 for (y = -i+1; y <= i-1; y++) {
2862 ty = dock->y_pos + y*ICON_SIZE;
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;
2872 x = i;
2873 tx = dock->x_pos + x*ICON_SIZE;
2874 if (slot_map[XY2OFS(x,y)]==0
2875 && ON_SCREEN(tx, ty, sx, ex, sy, ey)) {
2876 *x_pos = x;
2877 *y_pos = y;
2878 done = 1;
2879 break;
2883 free(slot_map);
2884 #undef XY2OFS
2885 return done;
2889 static void
2890 moveDock(WDock *dock, int new_x, int new_y)
2892 WAppIcon *btn;
2893 int i;
2895 dock->x_pos = new_x;
2896 dock->y_pos = new_y;
2897 for (i=0; i<dock->max_icons; i++) {
2898 btn = dock->icon_array[i];
2899 if (btn) {
2900 btn->x_pos = new_x + btn->xindex*ICON_SIZE;
2901 btn->y_pos = new_y + btn->yindex*ICON_SIZE;
2902 XMoveWindow(dpy, btn->icon->core->window, btn->x_pos, btn->y_pos);
2908 static void
2909 swapDock(WDock *dock)
2911 WScreen *scr = dock->screen_ptr;
2912 WAppIcon *btn;
2913 int x, i;
2916 if (dock->on_right_side) {
2917 x = dock->x_pos = scr->scr_width - ICON_SIZE - DOCK_EXTRA_SPACE;
2918 } else {
2919 x = dock->x_pos = DOCK_EXTRA_SPACE;
2922 for (i=0; i<dock->max_icons; i++) {
2923 btn = dock->icon_array[i];
2924 if (btn) {
2925 btn->x_pos = x;
2926 XMoveWindow(dpy, btn->icon->core->window, btn->x_pos, btn->y_pos);
2930 wScreenUpdateUsableArea(scr);
2934 static pid_t
2935 execCommand(WAppIcon *btn, char *command, WSavedState *state)
2937 WScreen *scr = btn->icon->core->screen_ptr;
2938 pid_t pid;
2939 char **argv;
2940 int argc;
2941 char *cmdline;
2943 cmdline = ExpandOptions(scr, command);
2945 if (scr->flags.dnd_data_convertion_status || !cmdline) {
2946 if (cmdline)
2947 free(cmdline);
2948 if (state)
2949 free(state);
2950 return 0;
2953 ParseCommand(cmdline, &argv, &argc);
2955 if (argv==NULL) {
2956 if (cmdline)
2957 free(cmdline);
2958 if (state)
2959 free(state);
2960 return 0;
2963 if ((pid=fork())==0) {
2964 char **args;
2965 int i;
2967 SetupEnvironment(scr);
2969 #ifdef HAVE_SETPGID
2970 setpgid(0, 0);
2971 #endif
2973 args = malloc(sizeof(char*)*(argc+1));
2974 if (!args)
2975 exit(111);
2976 for (i=0; i<argc; i++) {
2977 args[i] = argv[i];
2979 args[argc] = NULL;
2980 execvp(argv[0], args);
2981 exit(111);
2983 while (argc > 0)
2984 free(argv[--argc]);
2985 free(argv);
2987 if (pid > 0) {
2988 if (!state) {
2989 state = wmalloc(sizeof(WSavedState));
2990 memset(state, 0, sizeof(WSavedState));
2991 state->hidden = -1;
2992 state->miniaturized = -1;
2993 state->shaded = -1;
2994 if (btn->dock==scr->dock || btn->omnipresent)
2995 state->workspace = -1;
2996 else
2997 state->workspace = scr->current_workspace;
2999 wWindowAddSavedState(btn->wm_instance, btn->wm_class, cmdline, pid,
3000 state);
3001 wAddDeathHandler(pid, (WDeathHandler*)trackDeadProcess,
3002 btn->dock);
3003 } else if (state) {
3004 free(state);
3006 free(cmdline);
3007 return pid;
3011 void
3012 wDockHideIcons(WDock *dock)
3014 int i;
3016 if (dock==NULL)
3017 return;
3019 for (i=1; i<dock->max_icons; i++) {
3020 if (dock->icon_array[i])
3021 XUnmapWindow(dpy, dock->icon_array[i]->icon->core->window);
3023 dock->mapped = 0;
3025 dockIconPaint(dock->icon_array[0]);
3029 void
3030 wDockShowIcons(WDock *dock)
3032 int i, newlevel;
3033 WAppIcon *btn;
3035 if (dock==NULL)
3036 return;
3038 btn = dock->icon_array[0];
3039 moveDock(dock, btn->x_pos, btn->y_pos);
3041 newlevel = dock->lowered ? WMNormalLevel : WMDockLevel;
3042 ChangeStackingLevel(btn->icon->core, newlevel);
3044 for (i=1; i<dock->max_icons; i++) {
3045 if (dock->icon_array[i]) {
3046 MoveInStackListAbove(dock->icon_array[i]->icon->core,
3047 btn->icon->core);
3048 break;
3052 if (!dock->collapsed) {
3053 for (i=1; i<dock->max_icons; i++) {
3054 if (dock->icon_array[i]) {
3055 XMapWindow(dpy, dock->icon_array[i]->icon->core->window);
3059 dock->mapped = 1;
3061 dockIconPaint(btn);
3065 void
3066 wDockLower(WDock *dock)
3068 int i;
3070 for (i=0; i<dock->max_icons; i++) {
3071 if (dock->icon_array[i])
3072 wLowerFrame(dock->icon_array[i]->icon->core);
3077 void
3078 wDockRaise(WDock *dock)
3080 int i;
3082 for (i=dock->max_icons-1; i>=0; i--) {
3083 if (dock->icon_array[i])
3084 wRaiseFrame(dock->icon_array[i]->icon->core);
3089 void
3090 wDockRaiseLower(WDock *dock)
3092 if (!dock->icon_array[0]->icon->core->stacking->above
3093 ||(dock->icon_array[0]->icon->core->stacking->window_level
3094 !=dock->icon_array[0]->icon->core->stacking->above->stacking->window_level))
3095 wDockLower(dock);
3096 else
3097 wDockRaise(dock);
3101 void
3102 wDockFinishLaunch(WDock *dock, WAppIcon *icon)
3104 icon->launching = 0;
3105 icon->relaunching = 0;
3106 dockIconPaint(icon);
3110 WAppIcon*
3111 wDockFindIconFor(WDock *dock, Window window)
3113 WAppIcon *icon;
3114 int i;
3116 for (i=0; i<dock->max_icons; i++) {
3117 icon = dock->icon_array[i];
3118 if (icon && icon->main_window == window)
3119 return icon;
3121 return NULL;
3125 void
3126 wDockTrackWindowLaunch(WDock *dock, Window window)
3128 WAppIcon *icon;
3129 #ifdef REDUCE_APPICONS
3130 WAppIconAppList *tapplist;
3131 #endif
3132 char *wm_class, *wm_instance;
3133 int i;
3134 Bool firstPass = True;
3135 Bool found = False;
3136 char *command = NULL;
3139 int argc;
3140 char **argv;
3142 if (XGetCommand(dpy, window, &argv, &argc)) {
3143 if (argc > 0 && argv != NULL)
3144 command = FlattenStringList(argv,argc);
3145 if (argv) {
3146 XFreeStringList(argv);
3151 if (!PropGetWMClass(window, &wm_class, &wm_instance) ||
3152 (!wm_class && !wm_instance))
3153 return;
3155 retry:
3156 for (i=0; i<dock->max_icons; i++) {
3157 icon = dock->icon_array[i];
3158 if (!icon)
3159 continue;
3161 /* app is already attached to icon */
3162 if (icon->main_window == window) {
3163 found = True;
3164 break;
3167 if ((icon->wm_instance || icon->wm_class)
3168 && (icon->launching
3169 || (dock->screen_ptr->flags.startup && !icon->running))) {
3171 if (icon->wm_instance && wm_instance &&
3172 strcmp(icon->wm_instance, wm_instance)!=0) {
3173 continue;
3175 if (icon->wm_class && wm_class &&
3176 strcmp(icon->wm_class, wm_class)!=0) {
3177 continue;
3179 if (firstPass && command && strcmp(icon->command, command)!=0) {
3180 continue;
3183 if (!icon->relaunching) {
3184 WApplication *wapp;
3186 /* Possibly an application that was docked with dockit,
3187 * but the user did not update WMState to indicate that
3188 * it was docked by force */
3189 wapp = wApplicationOf(window);
3190 if (!wapp) {
3191 icon->forced_dock = 1;
3192 icon->running = 0;
3194 if (!icon->forced_dock)
3195 icon->main_window = window;
3197 #ifdef REDUCE_APPICONS
3198 tapplist = wmalloc(sizeof(WAppIconAppList));
3199 memset(tapplist, 0, sizeof(WAppIconAppList));
3200 tapplist->next = icon->applist;
3201 if (icon->applist)
3202 icon->applist->prev = tapplist;
3203 icon->applist = tapplist;
3204 tapplist->wapp = wApplicationOf(window);
3205 icon->num_apps++;
3206 #endif
3208 found = True;
3209 wDockFinishLaunch(dock, icon);
3210 break;
3214 if (firstPass && !found) {
3215 firstPass = False;
3216 goto retry;
3219 if (command)
3220 free(command);
3222 if (wm_class)
3223 XFree(wm_class);
3224 if (wm_instance)
3225 XFree(wm_instance);
3230 void
3231 wClipUpdateForWorkspaceChange(WScreen *scr, int workspace)
3233 if (!wPreferences.flags.noclip) {
3234 scr->clip_icon->dock = scr->workspaces[workspace]->clip;
3235 if (scr->current_workspace != workspace) {
3236 WDock *old_clip = scr->workspaces[scr->current_workspace]->clip;
3237 WAppIconChain *chain = scr->global_icons;
3239 while (chain) {
3240 moveIconBetweenDocks(chain->aicon->dock,
3241 scr->workspaces[workspace]->clip,
3242 chain->aicon, chain->aicon->xindex,
3243 chain->aicon->yindex);
3244 if (scr->workspaces[workspace]->clip->collapsed)
3245 XUnmapWindow(dpy, chain->aicon->icon->core->window);
3246 chain = chain->next;
3249 wDockHideIcons(old_clip);
3250 if (old_clip->auto_raise_lower) {
3251 if (old_clip->auto_raise_magic) {
3252 WMDeleteTimerHandler(old_clip->auto_raise_magic);
3253 old_clip->auto_raise_magic = NULL;
3255 wDockLower(old_clip);
3257 if (old_clip->auto_collapse) {
3258 if (old_clip->auto_expand_magic) {
3259 WMDeleteTimerHandler(old_clip->auto_expand_magic);
3260 old_clip->auto_expand_magic = NULL;
3262 old_clip->collapsed = 1;
3264 wDockShowIcons(scr->workspaces[workspace]->clip);
3266 if (scr->flags.clip_balloon_mapped)
3267 showClipBalloon(scr->clip_icon->dock, workspace);
3273 static void
3274 trackDeadProcess(pid_t pid, unsigned char status, WDock *dock)
3276 WAppIcon *icon;
3277 int i;
3279 for (i=0; i<dock->max_icons; i++) {
3280 icon = dock->icon_array[i];
3281 if (!icon)
3282 continue;
3284 if (icon->launching && icon->pid == pid) {
3285 if (!icon->relaunching) {
3286 icon->running = 0;
3287 icon->main_window = None;
3289 wDockFinishLaunch(dock, icon);
3290 icon->pid = 0;
3291 if (status==111) {
3292 char msg[PATH_MAX];
3293 #ifdef OFFIX_DND
3294 sprintf(msg, _("Could not execute command \"%s\""),
3295 icon->drop_launch && icon->dnd_command
3296 ? icon->dnd_command : icon->command);
3297 #else
3298 sprintf(msg, _("Could not execute command \"%s\""),
3299 icon->command);
3300 #endif
3301 wMessageDialog(dock->screen_ptr, _("Error"), msg,
3302 _("OK"), NULL, NULL);
3304 break;
3310 static void
3311 toggleLowered(WDock *dock)
3313 WAppIcon *tmp;
3314 int newlevel, i;
3316 /* lower/raise Dock */
3317 if (!dock->lowered) {
3318 newlevel = WMNormalLevel;
3319 dock->lowered = 1;
3320 } else {
3321 newlevel = WMDockLevel;
3322 dock->lowered = 0;
3325 for (i=0; i<dock->max_icons; i++) {
3326 tmp = dock->icon_array[i];
3327 if (!tmp)
3328 continue;
3330 ChangeStackingLevel(tmp->icon->core, newlevel);
3331 if (dock->lowered)
3332 wLowerFrame(tmp->icon->core);
3335 if (dock->type == WM_DOCK)
3336 wScreenUpdateUsableArea(dock->screen_ptr);
3340 static void
3341 toggleCollapsed(WDock *dock)
3343 if (dock->collapsed) {
3344 dock->collapsed = 0;
3345 wDockShowIcons(dock);
3347 else {
3348 dock->collapsed = 1;
3349 wDockHideIcons(dock);
3354 static void
3355 openDockMenu(WDock *dock, WAppIcon *aicon, XEvent *event)
3357 WScreen *scr = dock->screen_ptr;
3358 WObjDescriptor *desc;
3359 WMenuEntry *entry;
3360 WApplication *wapp = NULL;
3361 int index = 0;
3362 int x_pos;
3363 int n_selected;
3364 int appIsRunning = aicon->running && aicon->icon && aicon->icon->owner;
3366 if (dock->type == WM_DOCK) {
3367 /* keep on top */
3368 entry = dock->menu->entries[index];
3369 entry->flags.indicator_on = !dock->lowered;
3370 entry->clientdata = dock;
3371 } else {
3372 /* clip options */
3373 if (scr->clip_options)
3374 updateClipOptionsMenu(scr->clip_options, dock);
3376 n_selected = numberOfSelectedIcons(dock);
3378 /* Rename Workspace */
3379 entry = dock->menu->entries[++index];
3380 if (aicon == scr->clip_icon) {
3381 entry->callback = renameCallback;
3382 entry->clientdata = dock;
3383 entry->flags.indicator = 0;
3384 entry->text = _("Rename Workspace");
3385 } else {
3386 entry->callback = omnipresentCallback;
3387 entry->clientdata = aicon;
3388 if (n_selected > 0) {
3389 entry->flags.indicator = 0;
3390 entry->text = _("Toggle Omnipresent");
3391 } else {
3392 entry->flags.indicator = 1;
3393 entry->flags.indicator_on = aicon->omnipresent;
3394 entry->flags.indicator_type = MI_CHECK;
3395 entry->text = _("Omnipresent");
3399 /* select/unselect icon */
3400 entry = dock->menu->entries[++index];
3401 entry->clientdata = aicon;
3402 entry->flags.indicator_on = aicon->icon->selected;
3403 wMenuSetEnabled(dock->menu, index, aicon!=scr->clip_icon);
3405 /* select/unselect all icons */
3406 entry = dock->menu->entries[++index];
3407 entry->clientdata = aicon;
3408 if (n_selected > 0)
3409 entry->text = _("Unselect All Icons");
3410 else
3411 entry->text = _("Select All Icons");
3412 wMenuSetEnabled(dock->menu, index, dock->icon_count > 1);
3414 /* keep icon(s) */
3415 entry = dock->menu->entries[++index];
3416 entry->clientdata = aicon;
3417 if (n_selected > 1)
3418 entry->text = _("Keep Icons");
3419 else
3420 entry->text = _("Keep Icon");
3421 wMenuSetEnabled(dock->menu, index, dock->icon_count > 1);
3423 /* this is the workspace submenu part */
3424 entry = dock->menu->entries[++index];
3425 if (n_selected > 1)
3426 entry->text = _("Move Icons To");
3427 else
3428 entry->text = _("Move Icon To");
3429 if (scr->clip_submenu)
3430 updateWorkspaceMenu(scr->clip_submenu, aicon);
3431 wMenuSetEnabled(dock->menu, index, !aicon->omnipresent);
3433 /* remove icon(s) */
3434 entry = dock->menu->entries[++index];
3435 entry->clientdata = aicon;
3436 if (n_selected > 1)
3437 entry->text = _("Remove Icons");
3438 else
3439 entry->text = _("Remove Icon");
3440 wMenuSetEnabled(dock->menu, index, dock->icon_count > 1);
3442 /* attract icon(s) */
3443 entry = dock->menu->entries[++index];
3444 entry->clientdata = aicon;
3446 dock->menu->flags.realized = 0;
3447 wMenuRealize(dock->menu);
3450 /* launch */
3451 entry = dock->menu->entries[++index];
3452 entry->clientdata = aicon;
3453 wMenuSetEnabled(dock->menu, index, aicon->command!=NULL);
3455 /* unhide here */
3456 entry = dock->menu->entries[++index];
3457 entry->clientdata = aicon;
3458 wMenuSetEnabled(dock->menu, index, appIsRunning);
3460 /* hide */
3461 entry = dock->menu->entries[++index];
3462 entry->clientdata = aicon;
3463 if (aicon->icon->owner) {
3464 wapp = wApplicationOf(aicon->icon->owner->main_window);
3465 if (wapp && wapp->flags.hidden)
3466 entry->text = _("Unhide");
3467 else
3468 entry->text = _("Hide");
3469 } else {
3470 entry->text = _("Hide");
3472 wMenuSetEnabled(dock->menu, index, appIsRunning);
3474 /* settings */
3475 entry = dock->menu->entries[++index];
3476 entry->clientdata = aicon;
3477 wMenuSetEnabled(dock->menu, index, !aicon->editing
3478 && !wPreferences.flags.noupdates);
3480 /* kill */
3481 entry = dock->menu->entries[++index];
3482 entry->clientdata = aicon;
3483 wMenuSetEnabled(dock->menu, index, appIsRunning);
3485 if (!dock->menu->flags.realized)
3486 wMenuRealize(dock->menu);
3488 if (dock->type == WM_CLIP) {
3489 x_pos = event->xbutton.x_root+2;
3490 } else {
3491 x_pos = dock->on_right_side ?
3492 scr->scr_width - dock->menu->frame->core->width - 2 : 0;
3495 wMenuMapAt(dock->menu, x_pos, event->xbutton.y_root+2, False);
3497 /* allow drag select */
3498 event->xany.send_event = True;
3499 desc = &dock->menu->menu->descriptor;
3500 (*desc->handle_mousedown)(desc, event);
3504 static void
3505 openClipWorkspaceMenu(WScreen *scr, int x, int y)
3507 if (!scr->clip_ws_menu) {
3508 scr->clip_ws_menu = wWorkspaceMenuMake(scr, False);
3510 wWorkspaceMenuUpdate(scr, scr->clip_ws_menu);
3511 wMenuMapAt(scr->clip_ws_menu, x, y, False);
3515 /******************************************************************/
3516 static void
3517 iconDblClick(WObjDescriptor *desc, XEvent *event)
3519 WAppIcon *btn = desc->parent;
3520 WDock *dock = btn->dock;
3521 WApplication *wapp = NULL;
3522 int unhideHere = 0;
3524 #ifdef REDUCE_APPICONS
3525 if ((btn->icon->owner && !(event->xbutton.state & ControlMask)) ||
3526 ((btn->icon->owner == NULL) && (btn->applist != NULL))) {
3527 if (btn->icon->owner == NULL)
3528 btn->icon->owner = btn->applist->wapp->main_window_desc;
3530 #else
3531 if (btn->icon->owner && !(event->xbutton.state & ControlMask)) {
3532 #endif
3533 wapp = wApplicationOf(btn->icon->owner->main_window);
3535 assert(wapp!=NULL);
3537 unhideHere = (event->xbutton.state & ShiftMask);
3539 /* go to the last workspace that the user worked on the app */
3540 if (wapp->last_workspace != dock->screen_ptr->current_workspace
3541 && !unhideHere) {
3542 wWorkspaceChange(dock->screen_ptr, wapp->last_workspace);
3545 wUnhideApplication(wapp, event->xbutton.button==Button2,
3546 unhideHere);
3548 if (event->xbutton.state & MOD_MASK) {
3549 wHideOtherApplications(btn->icon->owner);
3551 } else {
3552 if (event->xbutton.button==Button1) {
3554 if (event->xbutton.state & MOD_MASK) {
3555 /* raise/lower dock */
3556 toggleLowered(dock);
3557 } else if (btn == dock->screen_ptr->clip_icon) {
3558 if (getClipButton(event->xbutton.x, event->xbutton.y)==CLIP_IDLE)
3559 toggleCollapsed(dock);
3560 else
3561 handleClipChangeWorkspace(dock->screen_ptr, event);
3562 } else if (btn->command) {
3563 if (!btn->launching &&
3564 (!btn->running || (event->xbutton.state & ControlMask))) {
3565 launchDockedApplication(btn);
3567 } else if (btn->xindex == 0 && btn->yindex == 0
3568 && btn->dock->type == WM_DOCK) {
3570 wShowGNUstepPanel(dock->screen_ptr);
3577 static void
3578 handleDockMove(WDock *dock, WAppIcon *aicon, XEvent *event)
3580 WScreen *scr = dock->screen_ptr;
3581 int ofs_x=event->xbutton.x, ofs_y=event->xbutton.y;
3582 int x, y;
3583 XEvent ev;
3584 int grabbed = 0, swapped = 0, done;
3585 Pixmap ghost = None;
3586 int superfluous = wPreferences.superfluous; /* we catch it to avoid problems */
3588 #ifdef DEBUG
3589 puts("moving dock");
3590 #endif
3591 if (XGrabPointer(dpy, aicon->icon->core->window, True, ButtonMotionMask
3592 |ButtonReleaseMask|ButtonPressMask, GrabModeAsync,
3593 GrabModeAsync, None, None, CurrentTime) !=GrabSuccess) {
3594 wwarning("pointer grab failed for dock move");
3596 y = 0;
3597 for (x=0; x<dock->max_icons; x++) {
3598 if (dock->icon_array[x]!=NULL &&
3599 dock->icon_array[x]->yindex > y)
3600 y = dock->icon_array[x]->yindex;
3602 y++;
3603 XResizeWindow(dpy, scr->dock_shadow, ICON_SIZE, ICON_SIZE*y);
3605 done = 0;
3606 while (!done) {
3607 WMMaskEvent(dpy, PointerMotionMask|ButtonReleaseMask|ButtonPressMask
3608 |ButtonMotionMask|ExposureMask, &ev);
3609 switch (ev.type) {
3610 case Expose:
3611 WMHandleEvent(&ev);
3612 break;
3614 case MotionNotify:
3615 if (!grabbed) {
3616 if (abs(ofs_x-ev.xmotion.x)>=MOVE_THRESHOLD
3617 || abs(ofs_y-ev.xmotion.y)>=MOVE_THRESHOLD) {
3618 XChangeActivePointerGrab(dpy, ButtonMotionMask
3619 |ButtonReleaseMask|ButtonPressMask,
3620 wCursor[WCUR_MOVE], CurrentTime);
3621 grabbed=1;
3623 break;
3625 if (dock->type == WM_CLIP) {
3626 if (ev.xmotion.x_root - ofs_x < 0) {
3627 x = 0;
3628 } else if (ev.xmotion.x_root - ofs_x + ICON_SIZE >
3629 scr->scr_width) {
3630 x = scr->scr_width - ICON_SIZE;
3631 } else {
3632 x = ev.xmotion.x_root - ofs_x;
3634 if (ev.xmotion.y_root - ofs_y < 0) {
3635 y = 0;
3636 } else if (ev.xmotion.y_root - ofs_y + ICON_SIZE >
3637 scr->scr_height) {
3638 y = scr->scr_height - ICON_SIZE;
3639 } else {
3640 y = ev.xmotion.y_root - ofs_y;
3642 moveDock(dock, x, y);
3643 } else {
3644 /* move vertically if pointer is inside the dock*/
3645 if ((dock->on_right_side &&
3646 ev.xmotion.x_root >= dock->x_pos - ICON_SIZE)
3647 || (!dock->on_right_side &&
3648 ev.xmotion.x_root <= dock->x_pos + ICON_SIZE*2)) {
3650 if (ev.xmotion.y_root - ofs_y < 0) {
3651 y = 0;
3652 } else if (ev.xmotion.y_root - ofs_y + ICON_SIZE >
3653 scr->scr_height) {
3654 y = scr->scr_height - ICON_SIZE;
3655 } else {
3656 y = ev.xmotion.y_root - ofs_y;
3658 moveDock(dock, dock->x_pos, y);
3660 /* move horizontally to change sides */
3661 x = ev.xmotion.x_root - ofs_x;
3662 if (!dock->on_right_side) {
3664 /* is on left */
3666 if (ev.xmotion.x_root > dock->x_pos + ICON_SIZE*2) {
3667 XMoveWindow(dpy, scr->dock_shadow, scr->scr_width-ICON_SIZE
3668 -DOCK_EXTRA_SPACE-1, dock->y_pos);
3669 if (superfluous && ghost==None) {
3670 ghost = MakeGhostDock(dock, dock->x_pos,
3671 scr->scr_width-ICON_SIZE
3672 -DOCK_EXTRA_SPACE-1,
3673 dock->y_pos);
3674 XSetWindowBackgroundPixmap(dpy, scr->dock_shadow,
3675 ghost);
3676 XClearWindow(dpy, scr->dock_shadow);
3678 XMapRaised(dpy, scr->dock_shadow);
3679 swapped = 1;
3680 } else {
3681 if (superfluous && ghost!=None) {
3682 XFreePixmap(dpy, ghost);
3683 ghost = None;
3685 XUnmapWindow(dpy, scr->dock_shadow);
3686 swapped = 0;
3688 } else {
3689 /* is on right */
3690 if (ev.xmotion.x_root < dock->x_pos - ICON_SIZE) {
3691 XMoveWindow(dpy, scr->dock_shadow,
3692 DOCK_EXTRA_SPACE, dock->y_pos);
3693 if (superfluous && ghost==None) {
3694 ghost = MakeGhostDock(dock, dock->x_pos,
3695 DOCK_EXTRA_SPACE, dock->y_pos);
3696 XSetWindowBackgroundPixmap(dpy, scr->dock_shadow,
3697 ghost);
3698 XClearWindow(dpy, scr->dock_shadow);
3700 XMapRaised(dpy, scr->dock_shadow);
3701 swapped = -1;
3702 } else {
3703 XUnmapWindow(dpy, scr->dock_shadow);
3704 swapped = 0;
3705 if (superfluous && ghost!=None) {
3706 XFreePixmap(dpy, ghost);
3707 ghost = None;
3712 break;
3714 case ButtonPress:
3715 break;
3717 case ButtonRelease:
3718 if (ev.xbutton.button != event->xbutton.button)
3719 break;
3720 XUngrabPointer(dpy, CurrentTime);
3721 XUnmapWindow(dpy, scr->dock_shadow);
3722 XResizeWindow(dpy, scr->dock_shadow, ICON_SIZE, ICON_SIZE);
3723 if (dock->type == WM_DOCK) {
3724 if (swapped!=0) {
3725 if (swapped>0)
3726 dock->on_right_side = 1;
3727 else
3728 dock->on_right_side = 0;
3729 swapDock(dock);
3730 wArrangeIcons(scr, False);
3733 done = 1;
3734 break;
3737 if (superfluous) {
3738 if (ghost!=None)
3739 XFreePixmap(dpy, ghost);
3740 XSetWindowBackground(dpy, scr->dock_shadow, scr->white_pixel);
3742 #ifdef DEBUG
3743 puts("End dock move");
3744 #endif
3749 static void
3750 handleIconMove(WDock *dock, WAppIcon *aicon, XEvent *event)
3752 WScreen *scr = dock->screen_ptr;
3753 Window wins[2];
3754 WIcon *icon = aicon->icon;
3755 WDock *dock2 = NULL, *last_dock = dock, *clip = NULL;
3756 int ondock, grabbed = 0, change_dock = 0, collapsed = 0;
3757 XEvent ev;
3758 int x = aicon->x_pos, y = aicon->y_pos;
3759 int ofs_x = event->xbutton.x, ofs_y = event->xbutton.y;
3760 int shad_x = x, shad_y = y;
3761 int ix = aicon->xindex, iy = aicon->yindex;
3762 int tmp;
3763 Pixmap ghost = None;
3764 Bool docked;
3765 int superfluous = wPreferences.superfluous; /* we catch it to avoid problems */
3766 int omnipresent = aicon->omnipresent; /* this must be cached!!! */
3769 if (wPreferences.flags.noupdates)
3770 return;
3772 if (XGrabPointer(dpy, icon->core->window, True, ButtonMotionMask
3773 |ButtonReleaseMask|ButtonPressMask, GrabModeAsync,
3774 GrabModeAsync, None, None, CurrentTime) !=GrabSuccess) {
3775 #ifdef DEBUG0
3776 wwarning("pointer grab failed for icon move");
3777 #endif
3780 if (!(event->xbutton.state & MOD_MASK))
3781 wRaiseFrame(icon->core);
3783 if (!wPreferences.flags.noclip)
3784 clip = scr->workspaces[scr->current_workspace]->clip;
3786 if (dock == scr->dock && !wPreferences.flags.noclip)
3787 dock2 = clip;
3788 else if (dock != scr->dock && !wPreferences.flags.nodock)
3789 dock2 = scr->dock;
3791 wins[0] = icon->core->window;
3792 wins[1] = scr->dock_shadow;
3793 XRestackWindows(dpy, wins, 2);
3794 XMoveResizeWindow(dpy, scr->dock_shadow, aicon->x_pos, aicon->y_pos,
3795 ICON_SIZE, ICON_SIZE);
3796 if (superfluous) {
3797 if (icon->pixmap!=None)
3798 ghost = MakeGhostIcon(scr, icon->pixmap);
3799 else
3800 ghost = MakeGhostIcon(scr, icon->core->window);
3802 XSetWindowBackgroundPixmap(dpy, scr->dock_shadow, ghost);
3803 XClearWindow(dpy, scr->dock_shadow);
3805 XMapWindow(dpy, scr->dock_shadow);
3807 ondock = 1;
3810 while(1) {
3811 XMaskEvent(dpy, PointerMotionMask|ButtonReleaseMask|ButtonPressMask
3812 |ButtonMotionMask|ExposureMask, &ev);
3813 switch (ev.type) {
3814 case Expose:
3815 WMHandleEvent(&ev);
3816 break;
3818 case MotionNotify:
3819 if (!grabbed) {
3820 if (abs(ofs_x-ev.xmotion.x)>=MOVE_THRESHOLD
3821 || abs(ofs_y-ev.xmotion.y)>=MOVE_THRESHOLD) {
3822 XChangeActivePointerGrab(dpy, ButtonMotionMask
3823 |ButtonReleaseMask|ButtonPressMask,
3824 wCursor[WCUR_MOVE], CurrentTime);
3825 grabbed=1;
3826 } else {
3827 break;
3831 if (omnipresent) {
3832 int i;
3833 for (i=0; i<scr->workspace_count; i++) {
3834 if (i == scr->current_workspace)
3835 continue;
3836 wDockShowIcons(scr->workspaces[i]->clip);
3840 x = ev.xmotion.x_root - ofs_x;
3841 y = ev.xmotion.y_root - ofs_y;
3842 tmp = wDockSnapIcon(dock, aicon, x, y, &ix, &iy, True);
3843 if (tmp && dock2) {
3844 change_dock = 0;
3845 if (last_dock != dock && collapsed) {
3846 last_dock->collapsed = 1;
3847 wDockHideIcons(last_dock);
3848 collapsed = 0;
3850 if (!collapsed && (collapsed = dock->collapsed)) {
3851 dock->collapsed = 0;
3852 wDockShowIcons(dock);
3854 if (dock->auto_raise_lower)
3855 wDockRaise(dock);
3856 last_dock = dock;
3857 } else if (dock2) {
3858 tmp = wDockSnapIcon(dock2, aicon, x, y, &ix, &iy, False);
3859 if (tmp) {
3860 change_dock = 1;
3861 if (last_dock != dock2 && collapsed) {
3862 last_dock->collapsed = 1;
3863 wDockHideIcons(last_dock);
3864 collapsed = 0;
3866 if (!collapsed && (collapsed = dock2->collapsed)) {
3867 dock2->collapsed = 0;
3868 wDockShowIcons(dock2);
3870 if (dock2->auto_raise_lower)
3871 wDockRaise(dock2);
3872 last_dock = dock2;
3875 if (aicon->launching
3876 || (aicon->running && !(ev.xmotion.state & MOD_MASK))
3877 || (!aicon->running && tmp)) {
3878 shad_x = last_dock->x_pos + ix*wPreferences.icon_size;
3879 shad_y = last_dock->y_pos + iy*wPreferences.icon_size;
3881 XMoveWindow(dpy, scr->dock_shadow, shad_x, shad_y);
3883 if (!ondock) {
3884 XMapWindow(dpy, scr->dock_shadow);
3886 ondock = 1;
3887 } else {
3888 if (ondock) {
3889 XUnmapWindow(dpy, scr->dock_shadow);
3891 ondock = 0;
3893 XMoveWindow(dpy, icon->core->window, x, y);
3894 break;
3896 case ButtonPress:
3897 break;
3899 case ButtonRelease:
3900 if (ev.xbutton.button != event->xbutton.button)
3901 break;
3902 XUngrabPointer(dpy, CurrentTime);
3903 if (ondock) {
3904 SlideWindow(icon->core->window, x, y, shad_x, shad_y);
3905 XUnmapWindow(dpy, scr->dock_shadow);
3906 if (!change_dock) {
3907 reattachIcon(dock, aicon, ix, iy);
3908 if (clip && dock!=clip && clip->auto_raise_lower)
3909 wDockLower(clip);
3910 } else {
3911 docked = moveIconBetweenDocks(dock, dock2, aicon, ix, iy);
3912 if (!docked) {
3913 /* Slide it back if dock rejected it */
3914 SlideWindow(icon->core->window, x, y, aicon->x_pos,
3915 aicon->y_pos);
3916 reattachIcon(dock, aicon, aicon->xindex,aicon->yindex);
3918 if (last_dock->type==WM_CLIP && last_dock->auto_collapse) {
3919 collapsed = 0;
3922 } else {
3923 aicon->x_pos = x;
3924 aicon->y_pos = y;
3925 if (superfluous) {
3926 if (!aicon->running && !wPreferences.no_animations) {
3927 /* We need to deselect it, even if is deselected in
3928 * wDockDetach(), because else DoKaboom() will fail.
3930 if (aicon->icon->selected)
3931 wIconSelect(aicon->icon);
3932 DoKaboom(scr,aicon->icon->core->window, x, y);
3935 if (clip && clip->auto_raise_lower)
3936 wDockLower(clip);
3937 wDockDetach(dock, aicon);
3939 if (collapsed) {
3940 last_dock->collapsed = 1;
3941 wDockHideIcons(last_dock);
3942 collapsed = 0;
3944 if (superfluous) {
3945 if (ghost!=None)
3946 XFreePixmap(dpy, ghost);
3947 XSetWindowBackground(dpy, scr->dock_shadow, scr->white_pixel);
3949 if (omnipresent) {
3950 int i;
3951 for (i=0; i<scr->workspace_count; i++) {
3952 if (i == scr->current_workspace)
3953 continue;
3954 wDockHideIcons(scr->workspaces[i]->clip);
3958 #ifdef DEBUG
3959 puts("End icon move");
3960 #endif
3961 return;
3967 static int
3968 getClipButton(int px, int py)
3970 int pt = (CLIP_BUTTON_SIZE+2)*ICON_SIZE/64;
3972 if (px < 0 || py < 0 || px >= ICON_SIZE || py >= ICON_SIZE)
3973 return CLIP_IDLE;
3975 if (py <= pt-((int)ICON_SIZE-1-px))
3976 return CLIP_FORWARD;
3977 else if (px <= pt-((int)ICON_SIZE-1-py))
3978 return CLIP_REWIND;
3980 return CLIP_IDLE;
3984 static void
3985 handleClipChangeWorkspace(WScreen *scr, XEvent *event)
3987 XEvent ev;
3988 int done, direction, new_ws;
3989 int new_dir;
3990 WDock *clip = scr->clip_icon->dock;
3992 direction = getClipButton(event->xbutton.x, event->xbutton.y);
3994 clip->lclip_button_pushed = direction==CLIP_REWIND;
3995 clip->rclip_button_pushed = direction==CLIP_FORWARD;
3997 wClipIconPaint(scr->clip_icon);
3998 done = 0;
3999 while(!done) {
4000 WMMaskEvent(dpy, ExposureMask|ButtonMotionMask|ButtonReleaseMask
4001 |ButtonPressMask, &ev);
4002 switch (ev.type) {
4003 case Expose:
4004 WMHandleEvent(&ev);
4005 break;
4007 case MotionNotify:
4008 new_dir = getClipButton(ev.xmotion.x, ev.xmotion.y);
4009 if (new_dir != direction) {
4010 direction = new_dir;
4011 clip->lclip_button_pushed = direction==CLIP_REWIND;
4012 clip->rclip_button_pushed = direction==CLIP_FORWARD;
4013 wClipIconPaint(scr->clip_icon);
4015 break;
4017 case ButtonPress:
4018 break;
4020 case ButtonRelease:
4021 if (ev.xbutton.button == event->xbutton.button)
4022 done = 1;
4026 clip->lclip_button_pushed = 0;
4027 clip->rclip_button_pushed = 0;
4029 new_ws = wPreferences.ws_advance || (event->xbutton.state & ControlMask);
4031 if (direction == CLIP_FORWARD) {
4032 if (scr->current_workspace < scr->workspace_count-1)
4033 wWorkspaceChange(scr, scr->current_workspace+1);
4034 else if (new_ws && scr->current_workspace < MAX_WORKSPACES-1)
4035 wWorkspaceChange(scr, scr->current_workspace+1);
4036 else if (wPreferences.ws_cycle)
4037 wWorkspaceChange(scr, 0);
4039 else if (direction == CLIP_REWIND) {
4040 if (scr->current_workspace > 0)
4041 wWorkspaceChange(scr, scr->current_workspace-1);
4042 else if (scr->current_workspace==0 && wPreferences.ws_cycle)
4043 wWorkspaceChange(scr, scr->workspace_count-1);
4046 wClipIconPaint(scr->clip_icon);
4050 static void
4051 iconMouseDown(WObjDescriptor *desc, XEvent *event)
4053 WAppIcon *aicon = desc->parent;
4054 WDock *dock = aicon->dock;
4055 WScreen *scr = aicon->icon->core->screen_ptr;
4057 if (aicon->editing || WCHECK_STATE(WSTATE_MODAL))
4058 return;
4060 scr->last_dock = dock;
4062 if (dock->menu->flags.mapped)
4063 wMenuUnmap(dock->menu);
4065 if (IsDoubleClick(scr, event)) {
4066 /* double-click was not in the main clip icon */
4067 if (dock->type != WM_CLIP || aicon->xindex!=0 || aicon->yindex!=0
4068 || getClipButton(event->xbutton.x, event->xbutton.y)==CLIP_IDLE) {
4069 iconDblClick(desc, event);
4070 return;
4074 if (dock->type == WM_CLIP && scr->flags.clip_balloon_mapped) {
4075 XUnmapWindow(dpy, scr->clip_balloon);
4076 scr->flags.clip_balloon_mapped = 0;
4079 #ifdef DEBUG
4080 puts("handling dock");
4081 #endif
4082 if (event->xbutton.button == Button1) {
4083 if (event->xbutton.state & MOD_MASK)
4084 wDockLower(dock);
4085 else
4086 wDockRaise(dock);
4088 if ((event->xbutton.state & ShiftMask) && aicon!=scr->clip_icon &&
4089 dock->type!=WM_DOCK) {
4090 wIconSelect(aicon->icon);
4091 return;
4094 if (aicon->yindex==0 && aicon->xindex==0) {
4095 if (getClipButton(event->xbutton.x, event->xbutton.y)!=CLIP_IDLE
4096 && dock->type==WM_CLIP)
4097 handleClipChangeWorkspace(scr, event);
4098 else
4099 handleDockMove(dock, aicon, event);
4100 } else
4101 handleIconMove(dock, aicon, event);
4103 } else if (event->xbutton.button==Button2 && dock->type==WM_CLIP &&
4104 aicon==scr->clip_icon) {
4105 openClipWorkspaceMenu(scr, event->xbutton.x_root+2,
4106 event->xbutton.y_root+2);
4107 if (scr->clip_ws_menu) {
4108 WMenu *menu;
4109 menu = scr->clip_ws_menu;
4110 desc = &menu->menu->descriptor;
4112 event->xany.send_event = True;
4113 (*desc->handle_mousedown)(desc, event);
4115 } else if (event->xbutton.button==Button2 && dock->type==WM_CLIP &&
4116 (event->xbutton.state & ShiftMask) && aicon!=scr->clip_icon) {
4117 wClipMakeIconOmnipresent(aicon, !aicon->omnipresent);
4118 } else if (event->xbutton.button == Button3) {
4119 openDockMenu(dock, aicon, event);
4124 static void
4125 showClipBalloon(WDock *dock, int workspace)
4127 int w, h;
4128 int x, y;
4129 WScreen *scr = dock->screen_ptr;
4130 char *text;
4131 Window stack[2];
4133 scr->flags.clip_balloon_mapped = 1;
4134 XMapWindow(dpy, scr->clip_balloon);
4136 text = scr->workspaces[workspace]->name;
4138 w = WMWidthOfString(scr->clip_title_font, text, strlen(text));
4140 h = WMFontHeight(scr->clip_title_font);
4141 XResizeWindow(dpy, scr->clip_balloon, w, h);
4143 x = dock->x_pos + CLIP_BUTTON_SIZE*ICON_SIZE/64;
4144 y = dock->y_pos + ICON_SIZE - WMFontHeight(scr->clip_title_font) - 3;
4146 if (x+w > scr->scr_width) {
4147 x = scr->scr_width - w;
4148 if (dock->y_pos + ICON_SIZE + h > scr->scr_height)
4149 y = dock->y_pos - h - 1;
4150 else
4151 y = dock->y_pos + ICON_SIZE;
4152 XRaiseWindow(dpy, scr->clip_balloon);
4153 } else {
4154 stack[0] = scr->clip_icon->icon->core->window;
4155 stack[1] = scr->clip_balloon;
4156 XRestackWindows(dpy, stack, 2);
4158 XMoveWindow(dpy, scr->clip_balloon, x, y);
4159 XSetForeground(dpy, scr->clip_title_gc,
4160 scr->clip_title_pixel[CLIP_NORMAL]);
4161 XClearWindow(dpy, scr->clip_balloon);
4162 WMDrawString(scr->wmscreen, scr->clip_balloon, scr->clip_title_gc,
4163 scr->clip_title_font, 0, 0, text, strlen(text));
4167 static void
4168 clipEnterNotify(WObjDescriptor *desc, XEvent *event)
4170 WAppIcon *btn = (WAppIcon*)desc->parent;
4171 WDock *dock;
4172 WScreen *scr;
4174 assert(event->type==EnterNotify);
4176 if(desc->parent_type!=WCLASS_DOCK_ICON)
4177 return;
4179 scr = btn->icon->core->screen_ptr;
4180 if (!btn->omnipresent)
4181 dock = btn->dock;
4182 else
4183 dock = scr->workspaces[scr->current_workspace]->clip;
4185 if (!dock || dock->type!=WM_CLIP)
4186 return;
4188 /* The auto raise/lower code */
4189 if (dock->auto_lower_magic) {
4190 WMDeleteTimerHandler(dock->auto_lower_magic);
4191 dock->auto_lower_magic = NULL;
4193 if (dock->auto_raise_lower && !dock->auto_raise_magic) {
4194 dock->auto_raise_magic = WMAddTimerHandler(AUTO_RAISE_DELAY,
4195 clipAutoRaise,
4196 (void *)dock);
4199 /* The auto expand/collapse code */
4200 if (dock->auto_collapse_magic) {
4201 WMDeleteTimerHandler(dock->auto_collapse_magic);
4202 dock->auto_collapse_magic = NULL;
4204 if (dock->auto_collapse && !dock->auto_expand_magic) {
4205 dock->auto_expand_magic = WMAddTimerHandler(AUTO_EXPAND_DELAY,
4206 clipAutoExpand,
4207 (void *)dock);
4210 if (btn->xindex == 0 && btn->yindex == 0)
4211 showClipBalloon(dock, dock->screen_ptr->current_workspace);
4212 else {
4213 if (dock->screen_ptr->flags.clip_balloon_mapped) {
4214 XUnmapWindow(dpy, dock->screen_ptr->clip_balloon);
4215 dock->screen_ptr->flags.clip_balloon_mapped = 0;
4221 static void
4222 clipLeave(WDock *dock)
4224 XEvent event;
4225 WObjDescriptor *desc = NULL;
4227 if (!dock || dock->type!=WM_CLIP)
4228 return;
4230 if (XCheckTypedEvent(dpy, EnterNotify, &event)!=False) {
4231 if (XFindContext(dpy, event.xcrossing.window, wWinContext,
4232 (XPointer *)&desc)!=XCNOENT
4233 && desc && desc->parent_type==WCLASS_DOCK_ICON
4234 && ((WAppIcon*)desc->parent)->dock
4235 && ((WAppIcon*)desc->parent)->dock->type==WM_CLIP) {
4236 /* We didn't left the Clip yet */
4237 XPutBackEvent(dpy, &event);
4238 return;
4241 XPutBackEvent(dpy, &event);
4242 } else {
4243 /* We entered a withdrawn window, so we're still in Clip */
4244 return;
4247 if (dock->auto_raise_magic) {
4248 WMDeleteTimerHandler(dock->auto_raise_magic);
4249 dock->auto_raise_magic = NULL;
4251 if (dock->auto_raise_lower && !dock->auto_lower_magic) {
4252 dock->auto_lower_magic = WMAddTimerHandler(AUTO_LOWER_DELAY,
4253 clipAutoLower,
4254 (void *)dock);
4257 if (dock->auto_expand_magic) {
4258 WMDeleteTimerHandler(dock->auto_expand_magic);
4259 dock->auto_expand_magic = NULL;
4261 if (dock->auto_collapse && !dock->auto_collapse_magic) {
4262 dock->auto_collapse_magic = WMAddTimerHandler(AUTO_COLLAPSE_DELAY,
4263 clipAutoCollapse,
4264 (void *)dock);
4269 static void
4270 clipLeaveNotify(WObjDescriptor *desc, XEvent *event)
4272 WAppIcon *btn = (WAppIcon*)desc->parent;
4274 assert(event->type==LeaveNotify);
4276 if(desc->parent_type!=WCLASS_DOCK_ICON)
4277 return;
4279 clipLeave(btn->dock);
4283 static void
4284 clipAutoCollapse(void *cdata)
4286 WDock *dock = (WDock *)cdata;
4288 if (dock->type!=WM_CLIP)
4289 return;
4291 if (dock->auto_collapse) {
4292 dock->collapsed = 1;
4293 wDockHideIcons(dock);
4295 dock->auto_collapse_magic = NULL;
4299 static void
4300 clipAutoExpand(void *cdata)
4302 WDock *dock = (WDock *)cdata;
4304 if (dock->type!=WM_CLIP)
4305 return;
4307 if (dock->auto_collapse) {
4308 dock->collapsed = 0;
4309 wDockShowIcons(dock);
4311 dock->auto_expand_magic = NULL;
4315 static void
4316 clipAutoLower(void *cdata)
4318 WDock *dock = (WDock *)cdata;
4320 if (dock->type!=WM_CLIP)
4321 return;
4323 if (dock->auto_raise_lower)
4324 wDockLower(dock);
4326 dock->auto_lower_magic = NULL;
4330 static void
4331 clipAutoRaise(void *cdata)
4333 WDock *dock = (WDock *)cdata;
4335 if (dock->type!=WM_CLIP)
4336 return;
4338 if (dock->auto_raise_lower)
4339 wDockRaise(dock);
4341 if (dock->screen_ptr->flags.clip_balloon_mapped) {
4342 showClipBalloon(dock, dock->screen_ptr->current_workspace);
4345 dock->auto_raise_magic = NULL;
4349 static Bool
4350 iconCanBeOmnipresent(WAppIcon *aicon)
4352 WScreen *scr = aicon->icon->core->screen_ptr;
4353 WDock *clip;
4354 WAppIcon *btn;
4355 int i, j;
4357 for (i=0; i<scr->workspace_count; i++) {
4358 clip = scr->workspaces[i]->clip;
4360 if (clip == aicon->dock)
4361 continue;
4363 if (clip->icon_count + scr->global_icon_count >= clip->max_icons)
4364 return False; /* Clip is full in some workspace */
4366 for (j=0; j<clip->max_icons; j++) {
4367 btn = clip->icon_array[j];
4368 if(btn && btn->xindex==aicon->xindex && btn->yindex==aicon->yindex)
4369 return False;
4373 return True;
4378 wClipMakeIconOmnipresent(WAppIcon *aicon, int omnipresent)
4380 WScreen *scr = aicon->icon->core->screen_ptr;
4381 WAppIconChain *new_entry, *tmp, *tmp1;
4382 int status = WO_SUCCESS;
4384 if (aicon->dock == scr->dock || aicon == scr->clip_icon)
4385 return WO_NOT_APPLICABLE;
4387 if (aicon->omnipresent == omnipresent)
4388 return WO_SUCCESS;
4390 if (omnipresent) {
4391 if (iconCanBeOmnipresent(aicon)) {
4392 aicon->omnipresent = 1;
4393 new_entry = wmalloc(sizeof(WAppIconChain));
4394 new_entry->aicon = aicon;
4395 new_entry->next = NULL;
4396 if (!scr->global_icons) {
4397 scr->global_icons = new_entry;
4398 } else {
4399 tmp = scr->global_icons;
4400 while (tmp->next)
4401 tmp = tmp->next;
4403 tmp->next = new_entry;
4405 scr->global_icon_count++;
4406 } else {
4407 aicon->omnipresent = 0;
4408 status = WO_FAILED;
4410 } else {
4411 aicon->omnipresent = 0;
4412 if (aicon == scr->global_icons->aicon) {
4413 tmp = scr->global_icons->next;
4414 free(scr->global_icons);
4415 scr->global_icons = tmp;
4416 scr->global_icon_count--;
4417 } else {
4418 tmp = scr->global_icons;
4419 while (tmp->next) {
4420 if (tmp->next->aicon == aicon) {
4421 tmp1 = tmp->next->next;
4422 free(tmp->next);
4423 tmp->next = tmp1;
4424 scr->global_icon_count--;
4425 break;
4427 tmp = tmp->next;
4432 wAppIconPaint(aicon);
4434 return status;