Moved the omnipresent setting from settings panel to clip's menu.
[wmaker-crm.git] / src / dock.c
blob622dbb18936bd30b19ac59713dd42d2a4478838c
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, _("Select Icon"), selectCallback, NULL);
1166 free(entry->text);
1167 entry->text = _("Select Icon");
1169 entry = wMenuAddCallback(menu, _("Select All Icons"),
1170 selectIconsCallback, NULL);
1171 free(entry->text);
1172 entry->text = _("Select All Icons");
1174 entry = wMenuAddCallback(menu, _("Keep Icon"), keepIconsCallback, NULL);
1175 free(entry->text);
1176 entry->text = _("Keep Icon");
1178 entry = wMenuAddCallback(menu, _("Move Icon To"), NULL, NULL);
1179 free(entry->text);
1180 entry->text = _("Move Icon To");
1181 scr->clip_submenu = makeWorkspaceMenu(scr);
1182 if (scr->clip_submenu)
1183 wMenuEntrySetCascade(menu, entry, scr->clip_submenu);
1185 entry = wMenuAddCallback(menu, _("Remove Icon"), removeIconsCallback,
1186 NULL);
1187 free(entry->text);
1188 entry->text = _("Remove Icon");
1190 wMenuAddCallback(menu, _("Attract Icons"), colectIconsCallback, NULL);
1193 wMenuAddCallback(menu, _("Launch"), launchCallback, NULL);
1195 wMenuAddCallback(menu, _("Unhide Here"), unhideHereCallback, NULL);
1197 entry = wMenuAddCallback(menu, _("Hide"), hideCallback, NULL);
1198 free(entry->text);
1199 entry->text = _("Hide");
1201 wMenuAddCallback(menu, _("Settings..."), settingsCallback, NULL);
1203 wMenuAddCallback(menu, _("Kill"), killCallback, NULL);
1205 if (type == WM_CLIP)
1206 scr->clip_menu = menu;
1208 return menu;
1212 WDock*
1213 wDockCreate(WScreen *scr, int type)
1215 WDock *dock;
1216 WAppIcon *btn;
1217 int icon_count;
1219 make_keys();
1221 dock = wmalloc(sizeof(WDock));
1222 memset(dock, 0, sizeof(WDock));
1224 if (type == WM_CLIP)
1225 icon_count = CLIP_MAX_ICONS;
1226 else
1227 icon_count = scr->scr_height/wPreferences.icon_size;
1229 dock->icon_array = wmalloc(sizeof(WAppIcon*)*icon_count);
1230 memset(dock->icon_array, 0, sizeof(WAppIcon*)*icon_count);
1232 dock->max_icons = icon_count;
1234 btn = mainIconCreate(scr, type);
1236 btn->dock = dock;
1238 dock->x_pos = btn->x_pos;
1239 dock->y_pos = btn->y_pos;
1240 dock->screen_ptr = scr;
1241 dock->type = type;
1242 dock->icon_count = 1;
1243 dock->on_right_side = 1;
1244 dock->collapsed = 0;
1245 dock->auto_collapse = 0;
1246 dock->auto_collapse_magic = NULL;
1247 dock->auto_raise_lower = 0;
1248 dock->auto_lower_magic = NULL;
1249 dock->auto_raise_magic = NULL;
1250 dock->attract_icons = 0;
1251 dock->keep_attracted = 0;
1252 dock->lowered = 1;
1253 dock->icon_array[0] = btn;
1254 wRaiseFrame(btn->icon->core);
1255 XMoveWindow(dpy, btn->icon->core->window, btn->x_pos, btn->y_pos);
1257 /* create dock menu */
1258 dock->menu = dockMenuCreate(scr, type);
1260 return dock;
1264 void
1265 wDockDestroy(WDock *dock)
1267 int i;
1268 WAppIcon *aicon;
1270 for (i=(dock->type == WM_CLIP) ? 1 : 0; i<dock->max_icons; i++) {
1271 aicon = dock->icon_array[i];
1272 if (aicon) {
1273 int keepit = aicon->running && wApplicationOf(aicon->main_window);
1274 wDockDetach(dock, aicon);
1275 if (keepit) {
1276 PlaceIcon(dock->screen_ptr, &aicon->x_pos, &aicon->y_pos);
1277 XMoveWindow(dpy, aicon->icon->core->window,
1278 aicon->x_pos, aicon->y_pos);
1279 if (!dock->mapped || dock->collapsed)
1280 XMapWindow(dpy, aicon->icon->core->window);
1284 if (wPreferences.auto_arrange_icons)
1285 wArrangeIcons(dock->screen_ptr, True);
1286 free(dock->icon_array);
1287 if (dock->menu && dock->type!=WM_CLIP)
1288 wMenuDestroy(dock->menu, True);
1289 free(dock);
1293 void
1294 wClipIconPaint(WAppIcon *aicon)
1296 WScreen *scr = aicon->icon->core->screen_ptr;
1297 WWorkspace *workspace = scr->workspaces[scr->current_workspace];
1298 GC gc;
1299 Window win = aicon->icon->core->window;
1300 int length, nlength;
1301 char *ws_name, ws_number[10];
1302 int ty, tx;
1304 wIconPaint(aicon->icon);
1306 length = strlen(workspace->name);
1307 ws_name = malloc(length + 1);
1308 sprintf(ws_name, "%s", workspace->name);
1309 sprintf(ws_number, "%i", scr->current_workspace + 1);
1310 nlength = strlen(ws_number);
1312 gc = scr->clip_title_gc;
1314 if (!workspace->clip->collapsed)
1315 XSetForeground(dpy, gc, scr->clip_title_pixel[CLIP_NORMAL]);
1316 else
1317 XSetForeground(dpy, gc, scr->clip_title_pixel[CLIP_COLLAPSED]);
1319 ty = ICON_SIZE - WMFontHeight(scr->clip_title_font) - 3;
1321 tx = CLIP_BUTTON_SIZE*ICON_SIZE/64;
1323 WMDrawString(scr->wmscreen, win, gc, scr->clip_title_font, tx,
1324 ty, ws_name, length);
1326 tx = (ICON_SIZE/2 - WMWidthOfString(scr->clip_title_font, ws_number,
1327 nlength))/2;
1329 WMDrawString(scr->wmscreen, win, gc, scr->clip_title_font, tx,
1330 2, ws_number, nlength);
1332 free(ws_name);
1334 if (aicon->launching) {
1335 XFillRectangle(dpy, aicon->icon->core->window, scr->stipple_gc,
1336 0, 0, wPreferences.icon_size, wPreferences.icon_size);
1338 paintClipButtons(aicon, aicon->dock->lclip_button_pushed,
1339 aicon->dock->rclip_button_pushed);
1343 static void
1344 clipIconExpose(WObjDescriptor *desc, XEvent *event)
1346 wClipIconPaint(desc->parent);
1350 static void
1351 dockIconPaint(WAppIcon *btn)
1353 if (btn == btn->icon->core->screen_ptr->clip_icon)
1354 wClipIconPaint(btn);
1355 else
1356 wAppIconPaint(btn);
1360 static proplist_t
1361 make_icon_state(WAppIcon *btn)
1363 proplist_t node = NULL;
1364 proplist_t command, autolaunch, name, forced, host, position, buggy;
1365 proplist_t omnipresent;
1366 char *tmp;
1367 char buffer[64];
1369 if (btn) {
1370 if (!btn->command)
1371 command = PLMakeString("-");
1372 else
1373 command = PLMakeString(btn->command);
1375 autolaunch = btn->auto_launch ? dYes : dNo;
1377 tmp = EscapeWM_CLASS(btn->wm_instance, btn->wm_class);
1379 name = PLMakeString(tmp);
1381 free(tmp);
1383 forced = btn->forced_dock ? dYes : dNo;
1385 buggy = btn->buggy_app ? dYes : dNo;
1387 if (btn == btn->icon->core->screen_ptr->clip_icon)
1388 sprintf(buffer, "%i,%i", btn->x_pos, btn->y_pos);
1389 else
1390 sprintf(buffer, "%hi,%hi", btn->xindex, btn->yindex);
1391 position = PLMakeString(buffer);
1393 node = PLMakeDictionaryFromEntries(dCommand, command,
1394 dName, name,
1395 dAutoLaunch, autolaunch,
1396 dForced, forced,
1397 dBuggyApplication, buggy,
1398 dPosition, position,
1399 NULL);
1400 PLRelease(command);
1401 PLRelease(name);
1402 PLRelease(position);
1404 omnipresent = btn->omnipresent ? dYes : dNo;
1405 if (btn->dock != btn->icon->core->screen_ptr->dock &&
1406 (btn->xindex != 0 || btn->yindex != 0))
1407 PLInsertDictionaryEntry(node, dOmnipresent, omnipresent);
1409 #ifdef OFFIX_DND
1410 if (btn->dnd_command) {
1411 command = PLMakeString(btn->dnd_command);
1412 PLInsertDictionaryEntry(node, dDropCommand, command);
1413 PLRelease(command);
1415 #endif /* OFFIX_DND */
1417 if (btn->client_machine && btn->remote_start) {
1418 host = PLMakeString(btn->client_machine);
1419 PLInsertDictionaryEntry(node, dHost, host);
1420 PLRelease(host);
1424 return node;
1428 static proplist_t
1429 dockSaveState(WDock *dock)
1431 int i;
1432 proplist_t icon_info;
1433 proplist_t list=NULL, dock_state=NULL;
1434 proplist_t value;
1435 char buffer[256];
1437 list = PLMakeArrayFromElements(NULL);
1439 for (i=(dock->type==WM_DOCK ? 0 : 1); i<dock->max_icons; i++) {
1440 WAppIcon *btn = dock->icon_array[i];
1442 if (!btn || (btn->attracted && !dock->keep_attracted))
1443 continue;
1445 if ((icon_info = make_icon_state(dock->icon_array[i]))) {
1446 list = PLAppendArrayElement(list, icon_info);
1447 PLRelease(icon_info);
1451 dock_state = PLMakeDictionaryFromEntries(dApplications, list, NULL);
1453 PLRelease(list);
1455 if (dock->type == WM_DOCK) {
1456 sprintf(buffer, "%i,%i", (dock->on_right_side ? -ICON_SIZE : 0),
1457 dock->y_pos);
1458 value = PLMakeString(buffer);
1459 PLInsertDictionaryEntry(dock_state, dPosition, value);
1460 PLRelease(value);
1463 value = (dock->lowered ? dYes : dNo);
1464 PLInsertDictionaryEntry(dock_state, dLowered, value);
1466 if (dock->type == WM_CLIP) {
1467 value = (dock->collapsed ? dYes : dNo);
1468 PLInsertDictionaryEntry(dock_state, dCollapsed, value);
1470 value = (dock->auto_collapse ? dYes : dNo);
1471 PLInsertDictionaryEntry(dock_state, dAutoCollapse, value);
1473 value = (dock->auto_raise_lower ? dYes : dNo);
1474 PLInsertDictionaryEntry(dock_state, dAutoRaiseLower, value);
1476 value = (dock->attract_icons ? dYes : dNo);
1477 PLInsertDictionaryEntry(dock_state, dAutoAttractIcons, value);
1479 value = (dock->keep_attracted ? dYes : dNo);
1480 PLInsertDictionaryEntry(dock_state, dKeepAttracted, value);
1483 return dock_state;
1487 void
1488 wDockSaveState(WScreen *scr)
1490 proplist_t dock_state;
1492 dock_state = dockSaveState(scr->dock);
1494 PLInsertDictionaryEntry(scr->session_state, dDock, dock_state);
1496 PLRelease(dock_state);
1500 void
1501 wClipSaveState(WScreen *scr)
1503 proplist_t clip_state;
1505 clip_state = make_icon_state(scr->clip_icon);
1507 PLInsertDictionaryEntry(scr->session_state, dClip, clip_state);
1509 PLRelease(clip_state);
1513 proplist_t
1514 wClipSaveWorkspaceState(WScreen *scr, int workspace)
1516 return dockSaveState(scr->workspaces[workspace]->clip);
1520 static WAppIcon*
1521 restore_icon_state(WScreen *scr, proplist_t info, int type, int index)
1523 WAppIcon *aicon;
1524 char *wclass, *winstance;
1525 proplist_t cmd, value;
1526 char *command;
1529 cmd = PLGetDictionaryEntry(info, dCommand);
1530 if (!cmd || !PLIsString(cmd)) {
1531 return NULL;
1534 /* parse window name */
1535 value = PLGetDictionaryEntry(info, dName);
1536 if (!value)
1537 return NULL;
1539 ParseWindowName(value, &winstance, &wclass, "dock");
1541 if (!winstance && !wclass) {
1542 return NULL;
1545 /* get commands */
1547 if (cmd)
1548 command = wstrdup(PLGetString(cmd));
1549 else
1550 command = NULL;
1552 if (!command || strcmp(command, "-")==0) {
1553 if (command)
1554 free(command);
1555 if (wclass)
1556 free(wclass);
1557 if (winstance)
1558 free(winstance);
1560 return NULL;
1563 aicon = wAppIconCreateForDock(scr, command, winstance, wclass,
1564 TILE_NORMAL);
1565 if (wclass)
1566 free(wclass);
1567 if (winstance)
1568 free(winstance);
1570 aicon->icon->core->descriptor.handle_mousedown = iconMouseDown;
1571 if (type == WM_CLIP) {
1572 aicon->icon->core->descriptor.handle_enternotify = clipEnterNotify;
1573 aicon->icon->core->descriptor.handle_leavenotify = clipLeaveNotify;
1575 aicon->icon->core->descriptor.parent_type = WCLASS_DOCK_ICON;
1576 aicon->icon->core->descriptor.parent = aicon;
1579 #ifdef OFFIX_DND
1580 cmd = PLGetDictionaryEntry(info, dDropCommand);
1581 if (cmd)
1582 aicon->dnd_command = wstrdup(PLGetString(cmd));
1583 #endif
1585 /* check auto launch */
1586 value = PLGetDictionaryEntry(info, dAutoLaunch);
1588 aicon->auto_launch = 0;
1589 if (value) {
1590 if (PLIsString(value)) {
1591 if (strcasecmp(PLGetString(value), "YES")==0)
1592 aicon->auto_launch = 1;
1593 } else {
1594 wwarning(_("bad value in docked icon state info %s"),
1595 PLGetString(dAutoLaunch));
1599 /* check if it wasn't normally docked */
1600 value = PLGetDictionaryEntry(info, dForced);
1602 aicon->forced_dock = 0;
1603 if (value) {
1604 if (PLIsString(value)) {
1605 if (strcasecmp(PLGetString(value), "YES")==0)
1606 aicon->forced_dock = 1;
1607 } else {
1608 wwarning(_("bad value in docked icon state info %s"),
1609 PLGetString(dForced));
1613 /* check if we can rely on the stuff in the app */
1614 value = PLGetDictionaryEntry(info, dBuggyApplication);
1616 aicon->buggy_app = 0;
1617 if (value) {
1618 if (PLIsString(value)) {
1619 if (strcasecmp(PLGetString(value), "YES")==0)
1620 aicon->buggy_app = 1;
1621 } else {
1622 wwarning(_("bad value in docked icon state info %s"),
1623 PLGetString(dBuggyApplication));
1627 /* get position in the dock */
1628 value = PLGetDictionaryEntry(info, dPosition);
1629 if (value && PLIsString(value)) {
1630 if (sscanf(PLGetString(value), "%hi,%hi", &aicon->xindex,
1631 &aicon->yindex)!=2)
1632 wwarning(_("bad value in docked icon state info %s"),
1633 PLGetString(dPosition));
1635 /* check position sanity */
1636 /* incomplete section! */
1637 if (type == WM_DOCK) {
1638 aicon->xindex = 0;
1639 if (aicon->yindex < 0)
1640 wwarning(_("bad value in docked icon position %i,%i"),
1641 aicon->xindex, aicon->yindex);
1643 } else {
1644 aicon->yindex = index;
1645 aicon->xindex = 0;
1648 /* check if icon is omnipresent */
1649 value = PLGetDictionaryEntry(info, dOmnipresent);
1651 aicon->omnipresent = 0;
1652 if (value) {
1653 if (PLIsString(value)) {
1654 if (strcasecmp(PLGetString(value), "YES")==0)
1655 aicon->omnipresent = 1;
1656 } else {
1657 wwarning(_("bad value in docked icon state info %s"),
1658 PLGetString(dOmnipresent));
1662 aicon->running = 0;
1663 aicon->docked = 1;
1665 return aicon;
1669 #define COMPLAIN(key) wwarning(_("bad value in dock state info:%s"), key)
1672 WAppIcon*
1673 wClipRestoreState(WScreen *scr, proplist_t clip_state)
1675 WAppIcon *icon;
1676 proplist_t value;
1679 icon = mainIconCreate(scr, WM_CLIP);
1681 if (!clip_state)
1682 return icon;
1683 else
1684 PLRetain(clip_state);
1686 /* restore position */
1688 value = PLGetDictionaryEntry(clip_state, dPosition);
1690 if (value) {
1691 if (!PLIsString(value))
1692 COMPLAIN("Position");
1693 else {
1694 if (sscanf(PLGetString(value), "%i,%i", &icon->x_pos,
1695 &icon->y_pos)!=2)
1696 COMPLAIN("Position");
1698 /* check position sanity */
1699 if (icon->y_pos < 0)
1700 icon->y_pos = 0;
1701 else if (icon->y_pos > scr->scr_height-ICON_SIZE)
1702 icon->y_pos = scr->scr_height-ICON_SIZE;
1704 if (icon->x_pos < 0)
1705 icon->x_pos = 0;
1706 else if (icon->x_pos > scr->scr_width-ICON_SIZE)
1707 icon->x_pos = scr->scr_width-ICON_SIZE;
1711 #ifdef OFFIX_DND
1712 value = PLGetDictionaryEntry(clip_state, dDropCommand);
1713 if (value && PLIsString(value))
1714 icon->dnd_command = wstrdup(PLGetString(value));
1715 #endif
1717 PLRelease(clip_state);
1719 return icon;
1723 WDock*
1724 wDockRestoreState(WScreen *scr, proplist_t dock_state, int type)
1726 WDock *dock;
1727 proplist_t apps;
1728 proplist_t value;
1729 WAppIcon *aicon, *old_top;
1730 int count, i;
1733 dock = wDockCreate(scr, type);
1735 if (!dock_state)
1736 return dock;
1738 if (dock_state)
1739 PLRetain(dock_state);
1742 /* restore position */
1744 value = PLGetDictionaryEntry(dock_state, dPosition);
1746 if (value) {
1747 if (!PLIsString(value))
1748 COMPLAIN("Position");
1749 else {
1750 if (sscanf(PLGetString(value), "%i,%i", &dock->x_pos,
1751 &dock->y_pos)!=2)
1752 COMPLAIN("Position");
1754 /* check position sanity */
1755 if (dock->y_pos < 0)
1756 dock->y_pos = 0;
1757 else if (dock->y_pos > scr->scr_height-ICON_SIZE)
1758 dock->y_pos = scr->scr_height - ICON_SIZE;
1760 /* This is no more needed. ??? */
1761 if (type == WM_CLIP) {
1762 if (dock->x_pos < 0)
1763 dock->x_pos = 0;
1764 else if (dock->x_pos > scr->scr_width-ICON_SIZE)
1765 dock->x_pos = scr->scr_width-ICON_SIZE;
1766 } else {
1767 if (dock->x_pos >= 0) {
1768 dock->x_pos = DOCK_EXTRA_SPACE;
1769 dock->on_right_side = 0;
1770 } else {
1771 dock->x_pos = scr->scr_width - DOCK_EXTRA_SPACE - ICON_SIZE;
1772 dock->on_right_side = 1;
1778 /* restore lowered/raised state */
1780 dock->lowered = 0;
1782 value = PLGetDictionaryEntry(dock_state, dLowered);
1784 if (value) {
1785 if (!PLIsString(value))
1786 COMPLAIN("Lowered");
1787 else {
1788 if (strcasecmp(PLGetString(value), "YES")==0)
1789 dock->lowered = 1;
1794 /* restore collapsed state */
1796 dock->collapsed = 0;
1798 value = PLGetDictionaryEntry(dock_state, dCollapsed);
1800 if (value) {
1801 if (!PLIsString(value))
1802 COMPLAIN("Collapsed");
1803 else {
1804 if (strcasecmp(PLGetString(value), "YES")==0)
1805 dock->collapsed = 1;
1810 /* restore auto-collapsed state */
1812 value = PLGetDictionaryEntry(dock_state, dAutoCollapse);
1814 if (value) {
1815 if (!PLIsString(value))
1816 COMPLAIN("AutoCollapse");
1817 else {
1818 if (strcasecmp(PLGetString(value), "YES")==0) {
1819 dock->auto_collapse = 1;
1820 dock->collapsed = 1;
1826 /* restore auto-raise/lower state */
1828 value = PLGetDictionaryEntry(dock_state, dAutoRaiseLower);
1830 if (value) {
1831 if (!PLIsString(value))
1832 COMPLAIN("AutoRaiseLower");
1833 else {
1834 if (strcasecmp(PLGetString(value), "YES")==0) {
1835 dock->auto_raise_lower = 1;
1840 /* restore attract icons state */
1842 dock->attract_icons = 0;
1844 value = PLGetDictionaryEntry(dock_state, dAutoAttractIcons);
1846 if (value) {
1847 if (!PLIsString(value))
1848 COMPLAIN("AutoAttractIcons");
1849 else {
1850 if (strcasecmp(PLGetString(value), "YES")==0)
1851 dock->attract_icons = 1;
1856 /* restore keep attracted icons state */
1858 dock->keep_attracted = 0;
1860 value = PLGetDictionaryEntry(dock_state, dKeepAttracted);
1862 if (value) {
1863 if (!PLIsString(value))
1864 COMPLAIN("KeepAttracted");
1865 else {
1866 if (strcasecmp(PLGetString(value), "YES")==0)
1867 dock->keep_attracted = 1;
1872 /* application list */
1874 apps = PLGetDictionaryEntry(dock_state, dApplications);
1876 if (!apps) {
1877 goto finish;
1880 count = PLGetNumberOfElements(apps);
1882 if (count==0)
1883 goto finish;
1885 old_top = dock->icon_array[0];
1887 /* dock->icon_count is set to 1 when dock is created.
1888 * Since Clip is already restored, we want to keep it so for clip,
1889 * but for dock we may change the default top tile, so we set it to 0.
1891 if (type == WM_DOCK)
1892 dock->icon_count = 0;
1894 for (i=0; i<count; i++) {
1895 if (dock->icon_count >= dock->max_icons) {
1896 wwarning(_("there are too many icons stored in dock. Ignoring what doesn't fit"));
1897 break;
1900 value = PLGetArrayElement(apps, i);
1901 aicon = restore_icon_state(scr, value, type, dock->icon_count);
1903 dock->icon_array[dock->icon_count] = aicon;
1905 if (aicon) {
1906 aicon->dock = dock;
1907 aicon->x_pos = dock->x_pos + (aicon->xindex*ICON_SIZE);
1908 aicon->y_pos = dock->y_pos + (aicon->yindex*ICON_SIZE);
1910 if (dock->lowered)
1911 ChangeStackingLevel(aicon->icon->core, WMNormalLevel);
1912 else
1913 ChangeStackingLevel(aicon->icon->core, WMDockLevel);
1915 wCoreConfigure(aicon->icon->core, aicon->x_pos, aicon->y_pos,
1916 0, 0);
1918 if (!dock->collapsed)
1919 XMapWindow(dpy, aicon->icon->core->window);
1920 wRaiseFrame(aicon->icon->core);
1922 dock->icon_count++;
1923 } else if (dock->icon_count==0 && type==WM_DOCK)
1924 dock->icon_count++;
1927 /* if the first icon is not defined, use the default */
1928 if (dock->icon_array[0]==NULL) {
1929 /* update default icon */
1930 old_top->x_pos = dock->x_pos;
1931 old_top->y_pos = dock->y_pos;
1932 if (dock->lowered)
1933 ChangeStackingLevel(old_top->icon->core, WMNormalLevel);
1934 else
1935 ChangeStackingLevel(old_top->icon->core, WMDockLevel);
1936 dock->icon_array[0] = old_top;
1937 XMoveWindow(dpy, old_top->icon->core->window, dock->x_pos, dock->y_pos);
1938 /* we don't need to increment dock->icon_count here because it was
1939 * incremented in the loop above.
1941 } else if (old_top!=dock->icon_array[0]) {
1942 if (old_top == scr->clip_icon)
1943 scr->clip_icon = dock->icon_array[0];
1944 wAppIconDestroy(old_top);
1947 finish:
1948 if (dock_state)
1949 PLRelease(dock_state);
1951 return dock;
1956 void
1957 wDockLaunchWithState(WDock *dock, WAppIcon *btn, WSavedState *state)
1959 if (btn && btn->command && !btn->running && !btn->launching) {
1961 btn->drop_launch = 0;
1963 btn->pid = execCommand(btn, btn->command, state);
1965 if (btn->pid>0) {
1966 if (!btn->forced_dock && !btn->buggy_app) {
1967 btn->launching = 1;
1968 dockIconPaint(btn);
1971 } else {
1972 free(state);
1977 void
1978 wDockDoAutoLaunch(WDock *dock, int workspace)
1980 WAppIcon *btn;
1981 WSavedState *state;
1982 int i;
1984 for (i = 0; i < dock->max_icons; i++) {
1985 btn = dock->icon_array[i];
1986 if (!btn || !btn->auto_launch)
1987 continue;
1989 state = wmalloc(sizeof(WSavedState));
1990 memset(state, 0, sizeof(WSavedState));
1991 state->workspace = workspace;
1992 /* TODO: this is klugy and is very difficult to understand
1993 * what's going on. Try to clean up */
1994 wDockLaunchWithState(dock, btn, state);
1998 #ifdef REDUCE_APPICONS
1999 void
2000 wDockSimulateLaunch(WDock *dock, WAppIcon *btn)
2002 if ((btn == NULL) || (dock == NULL))
2003 return;
2005 if (!btn->running) {
2006 if ((btn->icon->owner == NULL) && (btn->applist))
2007 btn->icon->owner = btn->applist->wapp->main_window_desc;
2008 if (!btn->forced_dock)
2009 btn->launching = 1;
2010 dockIconPaint(btn);
2011 wusleep(5000);
2014 #endif
2016 #ifdef OFFIX_DND
2017 static WDock*
2018 findDock(WScreen *scr, XEvent *event, int *icon_pos)
2020 WDock *dock;
2021 int i;
2023 *icon_pos = -1;
2024 if ((dock = scr->dock)!=NULL) {
2025 for (i=0; i<dock->max_icons; i++) {
2026 if (dock->icon_array[i]
2027 && dock->icon_array[i]->icon->core->window==event->xclient.window) {
2028 *icon_pos = i;
2029 break;
2033 if (*icon_pos<0 && (dock = scr->workspaces[scr->current_workspace]->clip)!=NULL) {
2034 for (i=0; i<dock->max_icons; i++) {
2035 if (dock->icon_array[i]
2036 && dock->icon_array[i]->icon->core->window==event->xclient.window) {
2037 *icon_pos = i;
2038 break;
2042 if(*icon_pos>=0)
2043 return dock;
2044 return NULL;
2049 wDockReceiveDNDDrop(WScreen *scr, XEvent *event)
2051 WDock *dock;
2052 WAppIcon *btn;
2053 int icon_pos;
2055 dock = findDock(scr, event, &icon_pos);
2056 if (!dock)
2057 return False;
2060 * Return True if the drop was on an application icon window.
2061 * In this case, let the ClientMessage handler redirect the
2062 * message to the app.
2064 if (dock->icon_array[icon_pos]->icon->icon_win!=None)
2065 return True;
2067 if (dock->icon_array[icon_pos]->dnd_command!=NULL) {
2068 scr->flags.dnd_data_convertion_status = 0;
2070 btn = dock->icon_array[icon_pos];
2072 if (!btn->forced_dock) {
2073 btn->relaunching = btn->running;
2074 btn->running = 1;
2076 if (btn->wm_instance || btn->wm_class) {
2077 WWindowAttributes attr;
2078 memset(&attr, 0, sizeof(WWindowAttributes));
2079 wDefaultFillAttributes(btn->icon->core->screen_ptr,
2080 btn->wm_instance,
2081 btn->wm_class, &attr, NULL, True);
2083 if (!attr.no_appicon)
2084 btn->launching = 1;
2085 else
2086 btn->running = 0;
2089 btn->drop_launch = 1;
2090 scr->last_dock = dock;
2091 btn->pid = execCommand(btn, btn->dnd_command, NULL);
2092 if (btn->pid>0) {
2093 dockIconPaint(btn);
2094 } else {
2095 btn->launching = 0;
2096 if (!btn->relaunching) {
2097 btn->running = 0;
2101 return False;
2103 #endif /* OFFIX_DND */
2107 Bool
2108 wDockAttachIcon(WDock *dock, WAppIcon *icon, int x, int y)
2110 WWindow *wwin;
2111 char **argv;
2112 int argc;
2113 int index;
2115 wwin = icon->icon->owner;
2116 if (icon->command==NULL) {
2117 icon->editing = 0;
2118 if (XGetCommand(dpy, wwin->client_win, &argv, &argc) && argc>0) {
2120 icon->command = FlattenStringList(argv, argc);
2121 XFreeStringList(argv);
2122 } else {
2123 char *command=NULL;
2125 /* icon->forced_dock = 1;*/
2126 if (!icon->attracted || dock->type!=WM_CLIP || dock->keep_attracted) {
2127 icon->editing = 1;
2128 if (wInputDialog(dock->screen_ptr, _("Dock Icon"),
2129 _("Type the command used to launch the application"),
2130 &command)) {
2131 if (command && (command[0]==0 ||
2132 (command[0]=='-' && command[1]==0))) {
2133 free(command);
2134 command = NULL;
2136 icon->command = command;
2137 icon->editing = 0;
2138 } else {
2139 icon->editing = 0;
2140 if (command)
2141 free(command);
2142 /* If the target is the dock, reject the icon. If
2143 * the target is the clip, make it an attracted icon
2145 if (dock->type==WM_CLIP) {
2146 icon->attracted = 1;
2147 if (!icon->icon->shadowed) {
2148 icon->icon->shadowed = 1;
2149 icon->icon->force_paint = 1;
2151 } else {
2152 return False;
2157 } else {
2158 icon->editing = 0;
2161 for (index=1; index<dock->max_icons; index++)
2162 if (dock->icon_array[index] == NULL)
2163 break;
2164 /* if (index == dock->max_icons)
2165 return; */
2167 assert(index < dock->max_icons);
2169 dock->icon_array[index] = icon;
2170 icon->yindex = y;
2171 icon->xindex = x;
2173 icon->omnipresent = 0;
2175 icon->x_pos = dock->x_pos + x*ICON_SIZE;
2176 icon->y_pos = dock->y_pos + y*ICON_SIZE;
2178 dock->icon_count++;
2180 icon->running = 1;
2181 icon->launching = 0;
2182 icon->docked = 1;
2183 icon->dock = dock;
2184 icon->icon->core->descriptor.handle_mousedown = iconMouseDown;
2185 if (dock->type == WM_CLIP) {
2186 icon->icon->core->descriptor.handle_enternotify = clipEnterNotify;
2187 icon->icon->core->descriptor.handle_leavenotify = clipLeaveNotify;
2189 icon->icon->core->descriptor.parent_type = WCLASS_DOCK_ICON;
2190 icon->icon->core->descriptor.parent = icon;
2192 MoveInStackListUnder(dock->icon_array[index-1]->icon->core,
2193 icon->icon->core);
2194 wAppIconMove(icon, icon->x_pos, icon->y_pos);
2195 wAppIconPaint(icon);
2197 if (wPreferences.auto_arrange_icons)
2198 wArrangeIcons(dock->screen_ptr, True);
2200 #ifdef OFFIX_DND
2201 if (icon->command && !icon->dnd_command) {
2202 icon->dnd_command = wmalloc(strlen(icon->command)+8);
2203 sprintf(icon->dnd_command, "%s %%d", icon->command);
2205 #endif
2207 return True;
2211 void
2212 reattachIcon(WDock *dock, WAppIcon *icon, int x, int y)
2214 int index;
2216 for(index=1; index<dock->max_icons; index++) {
2217 if(dock->icon_array[index] == icon)
2218 break;
2220 assert(index < dock->max_icons);
2222 icon->yindex = y;
2223 icon->xindex = x;
2225 icon->x_pos = dock->x_pos + x*ICON_SIZE;
2226 icon->y_pos = dock->y_pos + y*ICON_SIZE;
2230 Bool
2231 moveIconBetweenDocks(WDock *src, WDock *dest, WAppIcon *icon, int x, int y)
2233 WWindow *wwin;
2234 char **argv;
2235 int argc;
2236 int index;
2238 if (src == dest)
2239 return True; /* No move needed, we're already there */
2241 if (dest == NULL)
2242 return False;
2244 wwin = icon->icon->owner;
2247 * For the moment we can't do this if we move icons in Clip from one
2248 * workspace to other, because if we move two or more icons without
2249 * command, the dialog box will not be able to tell us to which of the
2250 * moved icons it applies. -Dan
2252 if ((dest->type==WM_DOCK /*|| dest->keep_attracted*/) && icon->command==NULL) {
2253 if (XGetCommand(dpy, wwin->client_win, &argv, &argc) && argc>0) {
2255 icon->command = FlattenStringList(argv, argc);
2256 XFreeStringList(argv);
2257 } else {
2258 char *command=NULL;
2260 icon->editing = 1;
2261 /* icon->forced_dock = 1;*/
2262 if (wInputDialog(src->screen_ptr, _("Dock Icon"),
2263 _("Type the command used to launch the application"),
2264 &command)) {
2265 if (command && (command[0]==0 ||
2266 (command[0]=='-' && command[1]==0))) {
2267 free(command);
2268 command = NULL;
2270 icon->command = command;
2271 } else {
2272 icon->editing = 0;
2273 if (command)
2274 free(command);
2275 return False;
2277 icon->editing = 0;
2281 if (dest->type == WM_DOCK)
2282 wClipMakeIconOmnipresent(icon, False);
2284 for(index=1; index<src->max_icons; index++) {
2285 if(src->icon_array[index] == icon)
2286 break;
2288 assert(index < src->max_icons);
2290 src->icon_array[index] = NULL;
2291 src->icon_count--;
2293 for(index=1; index<dest->max_icons; index++) {
2294 if(dest->icon_array[index] == NULL)
2295 break;
2297 /* if (index == dest->max_icons)
2298 return; */
2300 assert(index < dest->max_icons);
2302 dest->icon_array[index] = icon;
2303 icon->dock = dest;
2305 /* deselect the icon */
2306 if (icon->icon->selected)
2307 wIconSelect(icon->icon);
2309 if (dest->type == WM_DOCK) {
2310 icon->icon->core->descriptor.handle_enternotify = NULL;
2311 icon->icon->core->descriptor.handle_leavenotify = NULL;
2312 } else {
2313 icon->icon->core->descriptor.handle_enternotify = clipEnterNotify;
2314 icon->icon->core->descriptor.handle_leavenotify = clipLeaveNotify;
2317 /* set it to be kept when moving to dock, or to a clip that keep the
2318 * attracted icons.
2319 * Unless the icon does not have a command set
2321 if (icon->command && (dest->type==WM_DOCK || dest->keep_attracted)) {
2322 icon->attracted = 0;
2323 if (icon->icon->shadowed) {
2324 icon->icon->shadowed = 0;
2325 icon->icon->force_paint = 1;
2329 if (src->auto_collapse || src->auto_raise_lower)
2330 clipLeave(src);
2332 icon->yindex = y;
2333 icon->xindex = x;
2335 icon->x_pos = dest->x_pos + x*ICON_SIZE;
2336 icon->y_pos = dest->y_pos + y*ICON_SIZE;
2338 dest->icon_count++;
2340 MoveInStackListUnder(dest->icon_array[index-1]->icon->core,
2341 icon->icon->core);
2342 wAppIconPaint(icon);
2344 return True;
2348 void
2349 wDockDetach(WDock *dock, WAppIcon *icon)
2351 int index;
2353 /* make the settings panel be closed */
2354 if (icon->panel) {
2355 DestroyDockAppSettingsPanel(icon->panel);
2358 icon->docked = 0;
2359 icon->dock = NULL;
2360 icon->attracted = 0;
2361 if (icon->icon->shadowed) {
2362 icon->icon->shadowed = 0;
2363 icon->icon->force_paint = 1;
2366 /* This must be called before dock->icon_array[index] is set to NULL.
2367 * Don't move it. -Dan
2369 wClipMakeIconOmnipresent(icon, False);
2371 /* deselect the icon */
2372 if (icon->icon->selected)
2373 wIconSelect(icon->icon);
2375 if (icon->command) {
2376 free(icon->command);
2377 icon->command = NULL;
2379 #ifdef OFFIX_DND
2380 if (icon->dnd_command) {
2381 free(icon->dnd_command);
2382 icon->dnd_command = NULL;
2384 #endif
2386 for (index=1; index<dock->max_icons; index++)
2387 if (dock->icon_array[index] == icon)
2388 break;
2389 assert(index < dock->max_icons);
2390 dock->icon_array[index] = NULL;
2391 icon->yindex = -1;
2392 icon->xindex = -1;
2394 dock->icon_count--;
2396 /* if the dock is not attached to an application or
2397 * the the application did not set the approriate hints yet,
2398 * destroy the icon */
2399 #ifdef REDUCE_APPICONS
2400 if ((icon->num_apps == 0) && (!icon->running || !wApplicationOf(icon->main_window)) )
2401 #else
2402 if (!icon->running || !wApplicationOf(icon->main_window))
2403 #endif
2404 wAppIconDestroy(icon);
2405 else {
2406 icon->icon->core->descriptor.handle_mousedown = appIconMouseDown;
2407 icon->icon->core->descriptor.handle_enternotify = NULL;
2408 icon->icon->core->descriptor.handle_leavenotify = NULL;
2409 icon->icon->core->descriptor.parent_type = WCLASS_APPICON;
2410 icon->icon->core->descriptor.parent = icon;
2412 ChangeStackingLevel(icon->icon->core, NORMAL_ICON_LEVEL);
2414 wAppIconPaint(icon);
2415 if (wPreferences.auto_arrange_icons) {
2416 wArrangeIcons(dock->screen_ptr, True);
2419 if (dock->auto_collapse || dock->auto_raise_lower)
2420 clipLeave(dock);
2425 * returns the closest Dock slot index for the passed
2426 * coordinates.
2428 * Returns False if icon can't be docked.
2430 * Note: this function should NEVER alter ret_x or ret_y, unless it will
2431 * return True. -Dan
2433 Bool
2434 wDockSnapIcon(WDock *dock, WAppIcon *icon, int req_x, int req_y,
2435 int *ret_x, int *ret_y, int redocking)
2437 WScreen *scr = dock->screen_ptr;
2438 int dx, dy;
2439 int ex_x, ex_y;
2440 int i, offset = ICON_SIZE/2;
2441 WAppIcon *aicon = NULL;
2442 WAppIcon *nicon = NULL;
2443 int max_y_icons, max_x_icons;
2445 max_x_icons = scr->scr_width/ICON_SIZE;
2446 max_y_icons = scr->scr_height/ICON_SIZE-1;
2448 if (wPreferences.flags.noupdates)
2449 return False;
2451 dx = dock->x_pos;
2452 dy = dock->y_pos;
2454 /* if the dock is full */
2455 if (!redocking &&
2456 (dock->icon_count >= dock->max_icons)) {
2457 return False;
2460 /* exact position */
2461 if (req_y < dy)
2462 ex_y = (req_y - offset - dy)/ICON_SIZE;
2463 else
2464 ex_y = (req_y + offset - dy)/ICON_SIZE;
2466 if (req_x < dx)
2467 ex_x = (req_x - offset - dx)/ICON_SIZE;
2468 else
2469 ex_x = (req_x + offset - dx)/ICON_SIZE;
2471 /* check if the icon is outside the screen boundaries */
2472 if (dx + ex_x*ICON_SIZE < -ICON_SIZE+2 ||
2473 dx + ex_x*ICON_SIZE >= scr->scr_width-1 ||
2474 dy + ex_y*ICON_SIZE < -ICON_SIZE+2 ||
2475 dy + ex_y*ICON_SIZE >= scr->scr_height-1)
2476 return False;
2478 if (dock->type == WM_DOCK) {
2479 if (icon->dock != dock && ex_x != 0)
2480 return False;
2482 aicon = NULL;
2483 for (i=0; i<dock->max_icons; i++) {
2484 nicon = dock->icon_array[i];
2485 if (nicon && nicon->yindex == ex_y) {
2486 aicon = nicon;
2487 break;
2491 if (redocking) {
2492 int sig, done, closest;
2494 /* Possible cases when redocking:
2496 * icon dragged out of range of any slot -> false
2497 * icon dragged to range of free slot
2498 * icon dragged to range of same slot
2499 * icon dragged to range of different icon
2501 if (abs(ex_x) > DOCK_DETTACH_THRESHOLD)
2502 return False;
2504 if (ex_y>=0 && ex_y<=max_y_icons && (aicon==icon || !aicon)) {
2505 *ret_x = 0;
2506 *ret_y = ex_y;
2507 return True;
2510 /* start looking at the upper slot or lower? */
2511 if (ex_y*ICON_SIZE < (req_y + offset - dy))
2512 sig = 1;
2513 else
2514 sig = -1;
2516 closest = -1;
2517 done = 0;
2518 /* look for closest free slot */
2519 for (i=0; i<(DOCK_DETTACH_THRESHOLD+1)*2 && !done; i++) {
2520 int j;
2522 done = 1;
2523 closest = sig*(i/2) + ex_y;
2524 /* check if this slot is used */
2525 if (closest >= 0) {
2526 for (j = 0; j<dock->max_icons; j++) {
2527 if (dock->icon_array[j]
2528 && dock->icon_array[j]->yindex==closest) {
2529 /* slot is used by someone else */
2530 if (dock->icon_array[j]!=icon)
2531 done = 0;
2532 break;
2536 sig = -sig;
2538 if (done && closest >= 0 && closest <= max_y_icons &&
2539 ((ex_y >= closest && ex_y - closest < DOCK_DETTACH_THRESHOLD+1)
2541 (ex_y < closest && closest - ex_y <= DOCK_DETTACH_THRESHOLD+1))) {
2542 *ret_x = 0;
2543 *ret_y = closest;
2544 return True;
2546 } else { /* !redocking */
2548 /* if slot is free and the icon is close enough, return it */
2549 if (!aicon && ex_x == 0 && ex_y >= 0 && ex_y <= max_y_icons) {
2550 *ret_x = 0;
2551 *ret_y = ex_y;
2552 return True;
2555 } else { /* CLIP */
2556 int neighbours = 0;
2557 int start, stop, k;
2559 start = icon->omnipresent ? 0 : scr->current_workspace;
2560 stop = icon->omnipresent ? scr->workspace_count : start+1;
2562 aicon = NULL;
2563 for (k=start; k<stop; k++) {
2564 WDock *tmp = scr->workspaces[k]->clip;
2565 if (!tmp)
2566 continue;
2567 for (i=0; i<tmp->max_icons; i++) {
2568 nicon = tmp->icon_array[i];
2569 if (nicon && nicon->xindex == ex_x && nicon->yindex == ex_y) {
2570 aicon = nicon;
2571 break;
2574 if (aicon)
2575 break;
2577 for (k=start; k<stop; k++) {
2578 WDock *tmp = scr->workspaces[k]->clip;
2579 if (!tmp)
2580 continue;
2581 for (i=0; i<tmp->max_icons; i++) {
2582 nicon = tmp->icon_array[i];
2583 if (nicon && nicon != icon && /* Icon can't be it's own neighbour */
2584 (abs(nicon->xindex - ex_x) <= CLIP_ATTACH_VICINITY &&
2585 abs(nicon->yindex - ex_y) <= CLIP_ATTACH_VICINITY)) {
2586 neighbours = 1;
2587 break;
2590 if (neighbours)
2591 break;
2594 if (neighbours && (aicon==NULL || (redocking && aicon == icon))) {
2595 *ret_x = ex_x;
2596 *ret_y = ex_y;
2597 return True;
2600 return False;
2603 #define MIN(x, y) ((x) > (y) ? (y) : (x))
2604 #define MAX(x, y) ((x) < (y) ? (y) : (x))
2606 #define ON_SCREEN(x, y, sx, ex, sy, ey) \
2607 ((((x)+ICON_SIZE/2) >= (sx)) && (((y)+ICON_SIZE/2) >= (sy)) && \
2608 (((x) + (ICON_SIZE/2)) <= (ex)) && (((y) + (ICON_SIZE/2)) <= ey))
2612 * returns true if it can find a free slot in the dock,
2613 * in which case it changes x_pos and y_pos accordingly.
2614 * Else returns false.
2616 Bool
2617 wDockFindFreeSlot(WDock *dock, int *x_pos, int *y_pos)
2619 WScreen *scr = dock->screen_ptr;
2620 WAppIcon *btn;
2621 WAppIconChain *chain;
2622 unsigned char *slot_map;
2623 int mwidth;
2624 int r;
2625 int x, y;
2626 int i, done = False;
2627 int corner;
2628 int sx=0, sy=0, ex=scr->scr_width, ey=scr->scr_height;
2629 int extra_count=0;
2631 if (dock->type == WM_CLIP &&
2632 dock != scr->workspaces[scr->current_workspace]->clip)
2633 extra_count = scr->global_icon_count;
2635 /* if the dock is full */
2636 if (dock->icon_count+extra_count >= dock->max_icons) {
2637 return False;
2640 if (!wPreferences.flags.nodock && scr->dock) {
2641 if (scr->dock->on_right_side)
2642 ex -= ICON_SIZE + DOCK_EXTRA_SPACE;
2643 else
2644 sx += ICON_SIZE + DOCK_EXTRA_SPACE;
2647 if (ex < dock->x_pos)
2648 ex = dock->x_pos;
2649 if (sx > dock->x_pos+ICON_SIZE)
2650 sx = dock->x_pos+ICON_SIZE;
2651 #define C_NONE 0
2652 #define C_NW 1
2653 #define C_NE 2
2654 #define C_SW 3
2655 #define C_SE 4
2657 /* check if clip is in a corner */
2658 if (dock->type==WM_CLIP) {
2659 if (dock->x_pos < 1 && dock->y_pos < 1)
2660 corner = C_NE;
2661 else if (dock->x_pos < 1 && dock->y_pos >= (ey-ICON_SIZE))
2662 corner = C_SE;
2663 else if (dock->x_pos >= (ex-ICON_SIZE)&& dock->y_pos >= (ey-ICON_SIZE))
2664 corner = C_SW;
2665 else if (dock->x_pos >= (ex-ICON_SIZE) && dock->y_pos < 1)
2666 corner = C_NW;
2667 else
2668 corner = C_NONE;
2669 } else
2670 corner = C_NONE;
2672 /* If the clip is in the corner, use only slots that are in the border
2673 * of the screen */
2674 if (corner!=C_NONE) {
2675 char *hmap, *vmap;
2676 int hcount, vcount;
2678 hcount = MIN(dock->max_icons, scr->scr_width/ICON_SIZE);
2679 vcount = MIN(dock->max_icons, scr->scr_height/ICON_SIZE);
2680 hmap = wmalloc(hcount+1);
2681 memset(hmap, 0, hcount+1);
2682 vmap = wmalloc(vcount+1);
2683 memset(vmap, 0, vcount+1);
2685 /* mark used positions */
2686 switch (corner) {
2687 case C_NE:
2688 for (i=0; i<dock->max_icons; i++) {
2689 btn = dock->icon_array[i];
2690 if (!btn)
2691 continue;
2693 if (btn->xindex==0 && btn->yindex > 0 && btn->yindex < vcount)
2694 vmap[btn->yindex] = 1;
2695 else if (btn->yindex==0 && btn->xindex>0 && btn->xindex<hcount)
2696 hmap[btn->xindex] = 1;
2698 for (chain=scr->global_icons; chain!=NULL; chain=chain->next) {
2699 btn = chain->aicon;
2700 if (btn->xindex==0 && btn->yindex > 0 && btn->yindex < vcount)
2701 vmap[btn->yindex] = 1;
2702 else if (btn->yindex==0 && btn->xindex>0 && btn->xindex<hcount)
2703 hmap[btn->xindex] = 1;
2705 break;
2706 case C_NW:
2707 for (i=0; i<dock->max_icons; i++) {
2708 btn = dock->icon_array[i];
2709 if (!btn)
2710 continue;
2712 if (btn->xindex==0 && btn->yindex > 0 && btn->yindex < vcount)
2713 vmap[btn->yindex] = 1;
2714 else if (btn->yindex==0 && btn->xindex<0 &&btn->xindex>-hcount)
2715 hmap[-btn->xindex] = 1;
2717 for (chain=scr->global_icons; chain!=NULL; chain=chain->next) {
2718 btn = chain->aicon;
2719 if (btn->xindex==0 && btn->yindex > 0 && btn->yindex < vcount)
2720 vmap[btn->yindex] = 1;
2721 else if (btn->yindex==0 && btn->xindex<0 &&btn->xindex>-hcount)
2722 hmap[-btn->xindex] = 1;
2724 break;
2725 case C_SE:
2726 for (i=0; i<dock->max_icons; i++) {
2727 btn = dock->icon_array[i];
2728 if (!btn)
2729 continue;
2731 if (btn->xindex==0 && btn->yindex < 0 && btn->yindex > -vcount)
2732 vmap[-btn->yindex] = 1;
2733 else if (btn->yindex==0 && btn->xindex>0 && btn->xindex<hcount)
2734 hmap[btn->xindex] = 1;
2736 for (chain=scr->global_icons; chain!=NULL; chain=chain->next) {
2737 btn = chain->aicon;
2738 if (btn->xindex==0 && btn->yindex < 0 && btn->yindex > -vcount)
2739 vmap[-btn->yindex] = 1;
2740 else if (btn->yindex==0 && btn->xindex>0 && btn->xindex<hcount)
2741 hmap[btn->xindex] = 1;
2743 break;
2744 case C_SW:
2745 default:
2746 for (i=0; i<dock->max_icons; i++) {
2747 btn = dock->icon_array[i];
2748 if (!btn)
2749 continue;
2751 if (btn->xindex==0 && btn->yindex < 0 && btn->yindex > -vcount)
2752 vmap[-btn->yindex] = 1;
2753 else if (btn->yindex==0 && btn->xindex<0 &&btn->xindex>-hcount)
2754 hmap[-btn->xindex] = 1;
2756 for (chain=scr->global_icons; chain!=NULL; chain=chain->next) {
2757 btn = chain->aicon;
2758 if (btn->xindex==0 && btn->yindex < 0 && btn->yindex > -vcount)
2759 vmap[-btn->yindex] = 1;
2760 else if (btn->yindex==0 && btn->xindex<0 &&btn->xindex>-hcount)
2761 hmap[-btn->xindex] = 1;
2764 x=0; y=0;
2765 done = 0;
2766 /* search a vacant slot */
2767 for (i=1; i<MAX(vcount, hcount); i++) {
2768 if (i < vcount && vmap[i]==0) {
2769 /* found a slot */
2770 x = 0;
2771 y = i;
2772 done = 1;
2773 break;
2774 } else if (i < hcount && hmap[i]==0) {
2775 /* found a slot */
2776 x = i;
2777 y = 0;
2778 done = 1;
2779 break;
2782 free(vmap);
2783 free(hmap);
2784 /* If found a slot, translate and return */
2785 if (done) {
2786 if (corner==C_NW || corner==C_NE) {
2787 *y_pos = y;
2788 } else {
2789 *y_pos = -y;
2791 if (corner==C_NE || corner==C_SE) {
2792 *x_pos = x;
2793 } else {
2794 *x_pos = -x;
2796 return True;
2798 /* else, try to find a slot somewhere else */
2801 /* a map of mwidth x mwidth would be enough if we allowed icons to be
2802 * placed outside of screen */
2803 mwidth = (int)ceil(sqrt(dock->max_icons));
2805 /* In the worst case (the clip is in the corner of the screen),
2806 * the amount of icons that fit in the clip is smaller.
2807 * Double the map to get a safe value.
2809 mwidth += mwidth;
2811 r = (mwidth-1)/2;
2813 slot_map = wmalloc(mwidth*mwidth);
2814 memset(slot_map, 0, mwidth*mwidth);
2816 #define XY2OFS(x,y) (MAX(abs(x),abs(y)) > r) ? 0 : (((y)+r)*(mwidth)+(x)+r)
2818 /* mark used slots in the map. If the slot falls outside the map
2819 * (for example, when all icons are placed in line), ignore them. */
2820 for (i=0; i<dock->max_icons; i++) {
2821 btn = dock->icon_array[i];
2822 if (btn)
2823 slot_map[XY2OFS(btn->xindex, btn->yindex)] = 1;
2825 for (chain=scr->global_icons; chain!=NULL; chain=chain->next) {
2826 slot_map[XY2OFS(chain->aicon->xindex, chain->aicon->yindex)] = 1;
2828 /* Find closest slot from the center that is free by scanning the
2829 * map from the center to outward in circular passes.
2830 * This will not result in a neat layout, but will be optimal
2831 * in the sense that there will not be holes left.
2833 done = 0;
2834 for (i = 1; i <= r && !done; i++) {
2835 int tx, ty;
2837 /* top and bottom parts of the ring */
2838 for (x = -i; x <= i && !done; x++) {
2839 tx = dock->x_pos + x*ICON_SIZE;
2840 y = -i;
2841 ty = dock->y_pos + y*ICON_SIZE;
2842 if (slot_map[XY2OFS(x,y)]==0
2843 && ON_SCREEN(tx, ty, sx, ex, sy, ey)) {
2844 *x_pos = x;
2845 *y_pos = y;
2846 done = 1;
2847 break;
2849 y = i;
2850 ty = dock->y_pos + y*ICON_SIZE;
2851 if (slot_map[XY2OFS(x,y)]==0
2852 && ON_SCREEN(tx, ty, sx, ex, sy, ey)) {
2853 *x_pos = x;
2854 *y_pos = y;
2855 done = 1;
2856 break;
2859 /* left and right parts of the ring */
2860 for (y = -i+1; y <= i-1; y++) {
2861 ty = dock->y_pos + y*ICON_SIZE;
2862 x = -i;
2863 tx = dock->x_pos + x*ICON_SIZE;
2864 if (slot_map[XY2OFS(x,y)]==0
2865 && ON_SCREEN(tx, ty, sx, ex, sy, ey)) {
2866 *x_pos = x;
2867 *y_pos = y;
2868 done = 1;
2869 break;
2871 x = i;
2872 tx = dock->x_pos + x*ICON_SIZE;
2873 if (slot_map[XY2OFS(x,y)]==0
2874 && ON_SCREEN(tx, ty, sx, ex, sy, ey)) {
2875 *x_pos = x;
2876 *y_pos = y;
2877 done = 1;
2878 break;
2882 free(slot_map);
2883 #undef XY2OFS
2884 return done;
2888 static void
2889 moveDock(WDock *dock, int new_x, int new_y)
2891 WAppIcon *btn;
2892 int i;
2894 dock->x_pos = new_x;
2895 dock->y_pos = new_y;
2896 for (i=0; i<dock->max_icons; i++) {
2897 btn = dock->icon_array[i];
2898 if (btn) {
2899 btn->x_pos = new_x + btn->xindex*ICON_SIZE;
2900 btn->y_pos = new_y + btn->yindex*ICON_SIZE;
2901 XMoveWindow(dpy, btn->icon->core->window, btn->x_pos, btn->y_pos);
2907 static void
2908 swapDock(WDock *dock)
2910 WScreen *scr = dock->screen_ptr;
2911 WAppIcon *btn;
2912 int x, i;
2915 if (dock->on_right_side) {
2916 x = dock->x_pos = scr->scr_width - ICON_SIZE - DOCK_EXTRA_SPACE;
2917 } else {
2918 x = dock->x_pos = DOCK_EXTRA_SPACE;
2921 for (i=0; i<dock->max_icons; i++) {
2922 btn = dock->icon_array[i];
2923 if (btn) {
2924 btn->x_pos = x;
2925 XMoveWindow(dpy, btn->icon->core->window, btn->x_pos, btn->y_pos);
2929 wScreenUpdateUsableArea(scr);
2933 static pid_t
2934 execCommand(WAppIcon *btn, char *command, WSavedState *state)
2936 WScreen *scr = btn->icon->core->screen_ptr;
2937 pid_t pid;
2938 char **argv;
2939 int argc;
2940 char *cmdline;
2942 cmdline = ExpandOptions(scr, command);
2944 if (scr->flags.dnd_data_convertion_status || !cmdline) {
2945 if (cmdline)
2946 free(cmdline);
2947 if (state)
2948 free(state);
2949 return 0;
2952 ParseCommand(cmdline, &argv, &argc);
2954 if (argv==NULL) {
2955 if (cmdline)
2956 free(cmdline);
2957 if (state)
2958 free(state);
2959 return 0;
2962 if ((pid=fork())==0) {
2963 char **args;
2964 int i;
2966 SetupEnvironment(scr);
2968 #ifdef HAVE_SETPGID
2969 setpgid(0, 0);
2970 #endif
2972 args = malloc(sizeof(char*)*(argc+1));
2973 if (!args)
2974 exit(111);
2975 for (i=0; i<argc; i++) {
2976 args[i] = argv[i];
2978 args[argc] = NULL;
2979 execvp(argv[0], args);
2980 exit(111);
2982 while (argc > 0)
2983 free(argv[--argc]);
2984 free(argv);
2986 if (pid > 0) {
2987 if (!state) {
2988 state = wmalloc(sizeof(WSavedState));
2989 memset(state, 0, sizeof(WSavedState));
2990 state->hidden = -1;
2991 state->miniaturized = -1;
2992 state->shaded = -1;
2993 if (btn->dock==scr->dock || btn->omnipresent)
2994 state->workspace = -1;
2995 else
2996 state->workspace = scr->current_workspace;
2998 wWindowAddSavedState(btn->wm_instance, btn->wm_class, cmdline, pid,
2999 state);
3000 wAddDeathHandler(pid, (WDeathHandler*)trackDeadProcess,
3001 btn->dock);
3002 } else if (state) {
3003 free(state);
3005 free(cmdline);
3006 return pid;
3010 void
3011 wDockHideIcons(WDock *dock)
3013 int i;
3015 if (dock==NULL)
3016 return;
3018 for (i=1; i<dock->max_icons; i++) {
3019 if (dock->icon_array[i])
3020 XUnmapWindow(dpy, dock->icon_array[i]->icon->core->window);
3022 dock->mapped = 0;
3024 dockIconPaint(dock->icon_array[0]);
3028 void
3029 wDockShowIcons(WDock *dock)
3031 int i, newlevel;
3032 WAppIcon *btn;
3034 if (dock==NULL)
3035 return;
3037 btn = dock->icon_array[0];
3038 moveDock(dock, btn->x_pos, btn->y_pos);
3040 newlevel = dock->lowered ? WMNormalLevel : WMDockLevel;
3041 ChangeStackingLevel(btn->icon->core, newlevel);
3043 for (i=1; i<dock->max_icons; i++) {
3044 if (dock->icon_array[i]) {
3045 MoveInStackListAbove(dock->icon_array[i]->icon->core,
3046 btn->icon->core);
3047 break;
3051 if (!dock->collapsed) {
3052 for (i=1; i<dock->max_icons; i++) {
3053 if (dock->icon_array[i]) {
3054 XMapWindow(dpy, dock->icon_array[i]->icon->core->window);
3058 dock->mapped = 1;
3060 dockIconPaint(btn);
3064 void
3065 wDockLower(WDock *dock)
3067 int i;
3069 for (i=0; i<dock->max_icons; i++) {
3070 if (dock->icon_array[i])
3071 wLowerFrame(dock->icon_array[i]->icon->core);
3076 void
3077 wDockRaise(WDock *dock)
3079 int i;
3081 for (i=dock->max_icons-1; i>=0; i--) {
3082 if (dock->icon_array[i])
3083 wRaiseFrame(dock->icon_array[i]->icon->core);
3088 void
3089 wDockRaiseLower(WDock *dock)
3091 if (!dock->icon_array[0]->icon->core->stacking->above
3092 ||(dock->icon_array[0]->icon->core->stacking->window_level
3093 !=dock->icon_array[0]->icon->core->stacking->above->stacking->window_level))
3094 wDockLower(dock);
3095 else
3096 wDockRaise(dock);
3100 void
3101 wDockFinishLaunch(WDock *dock, WAppIcon *icon)
3103 icon->launching = 0;
3104 icon->relaunching = 0;
3105 dockIconPaint(icon);
3109 WAppIcon*
3110 wDockFindIconFor(WDock *dock, Window window)
3112 WAppIcon *icon;
3113 int i;
3115 for (i=0; i<dock->max_icons; i++) {
3116 icon = dock->icon_array[i];
3117 if (icon && icon->main_window == window)
3118 return icon;
3120 return NULL;
3124 void
3125 wDockTrackWindowLaunch(WDock *dock, Window window)
3127 WAppIcon *icon;
3128 #ifdef REDUCE_APPICONS
3129 WAppIconAppList *tapplist;
3130 #endif
3131 char *wm_class, *wm_instance;
3132 int i;
3133 Bool firstPass = True;
3134 Bool found = False;
3135 char *command = NULL;
3138 int argc;
3139 char **argv;
3141 if (XGetCommand(dpy, window, &argv, &argc)) {
3142 if (argc > 0 && argv != NULL)
3143 command = FlattenStringList(argv,argc);
3144 if (argv) {
3145 XFreeStringList(argv);
3150 if (!PropGetWMClass(window, &wm_class, &wm_instance) ||
3151 (!wm_class && !wm_instance))
3152 return;
3154 retry:
3155 for (i=0; i<dock->max_icons; i++) {
3156 icon = dock->icon_array[i];
3157 if (!icon)
3158 continue;
3160 /* app is already attached to icon */
3161 if (icon->main_window == window) {
3162 found = True;
3163 break;
3166 if ((icon->wm_instance || icon->wm_class)
3167 && (icon->launching
3168 || (dock->screen_ptr->flags.startup && !icon->running))) {
3170 if (icon->wm_instance && wm_instance &&
3171 strcmp(icon->wm_instance, wm_instance)!=0) {
3172 continue;
3174 if (icon->wm_class && wm_class &&
3175 strcmp(icon->wm_class, wm_class)!=0) {
3176 continue;
3178 if (firstPass && command && strcmp(icon->command, command)!=0) {
3179 continue;
3182 if (!icon->relaunching) {
3183 WApplication *wapp;
3185 /* Possibly an application that was docked with dockit,
3186 * but the user did not update WMState to indicate that
3187 * it was docked by force */
3188 wapp = wApplicationOf(window);
3189 if (!wapp) {
3190 icon->forced_dock = 1;
3191 icon->running = 0;
3193 if (!icon->forced_dock)
3194 icon->main_window = window;
3196 #ifdef REDUCE_APPICONS
3197 tapplist = wmalloc(sizeof(WAppIconAppList));
3198 memset(tapplist, 0, sizeof(WAppIconAppList));
3199 tapplist->next = icon->applist;
3200 if (icon->applist)
3201 icon->applist->prev = tapplist;
3202 icon->applist = tapplist;
3203 tapplist->wapp = wApplicationOf(window);
3204 icon->num_apps++;
3205 #endif
3207 found = True;
3208 wDockFinishLaunch(dock, icon);
3209 break;
3213 if (firstPass && !found) {
3214 firstPass = False;
3215 goto retry;
3218 if (command)
3219 free(command);
3221 if (wm_class)
3222 XFree(wm_class);
3223 if (wm_instance)
3224 XFree(wm_instance);
3229 void
3230 wClipUpdateForWorkspaceChange(WScreen *scr, int workspace)
3232 if (!wPreferences.flags.noclip) {
3233 scr->clip_icon->dock = scr->workspaces[workspace]->clip;
3234 if (scr->current_workspace != workspace) {
3235 WDock *old_clip = scr->workspaces[scr->current_workspace]->clip;
3236 WAppIconChain *chain = scr->global_icons;
3238 while (chain) {
3239 moveIconBetweenDocks(chain->aicon->dock,
3240 scr->workspaces[workspace]->clip,
3241 chain->aicon, chain->aicon->xindex,
3242 chain->aicon->yindex);
3243 if (scr->workspaces[workspace]->clip->collapsed)
3244 XUnmapWindow(dpy, chain->aicon->icon->core->window);
3245 chain = chain->next;
3248 wDockHideIcons(old_clip);
3249 if (old_clip->auto_raise_lower) {
3250 if (old_clip->auto_raise_magic) {
3251 WMDeleteTimerHandler(old_clip->auto_raise_magic);
3252 old_clip->auto_raise_magic = NULL;
3254 wDockLower(old_clip);
3256 if (old_clip->auto_collapse) {
3257 if (old_clip->auto_expand_magic) {
3258 WMDeleteTimerHandler(old_clip->auto_expand_magic);
3259 old_clip->auto_expand_magic = NULL;
3261 old_clip->collapsed = 1;
3263 wDockShowIcons(scr->workspaces[workspace]->clip);
3265 if (scr->flags.clip_balloon_mapped)
3266 showClipBalloon(scr->clip_icon->dock, workspace);
3272 static void
3273 trackDeadProcess(pid_t pid, unsigned char status, WDock *dock)
3275 WAppIcon *icon;
3276 int i;
3278 for (i=0; i<dock->max_icons; i++) {
3279 icon = dock->icon_array[i];
3280 if (!icon)
3281 continue;
3283 if (icon->launching && icon->pid == pid) {
3284 if (!icon->relaunching) {
3285 icon->running = 0;
3286 icon->main_window = None;
3288 wDockFinishLaunch(dock, icon);
3289 icon->pid = 0;
3290 if (status==111) {
3291 char msg[PATH_MAX];
3292 #ifdef OFFIX_DND
3293 sprintf(msg, _("Could not execute command \"%s\""),
3294 icon->drop_launch && icon->dnd_command
3295 ? icon->dnd_command : icon->command);
3296 #else
3297 sprintf(msg, _("Could not execute command \"%s\""),
3298 icon->command);
3299 #endif
3300 wMessageDialog(dock->screen_ptr, _("Error"), msg,
3301 _("OK"), NULL, NULL);
3303 break;
3309 static void
3310 toggleLowered(WDock *dock)
3312 WAppIcon *tmp;
3313 int newlevel, i;
3315 /* lower/raise Dock */
3316 if (!dock->lowered) {
3317 newlevel = WMNormalLevel;
3318 dock->lowered = 1;
3319 } else {
3320 newlevel = WMDockLevel;
3321 dock->lowered = 0;
3324 for (i=0; i<dock->max_icons; i++) {
3325 tmp = dock->icon_array[i];
3326 if (!tmp)
3327 continue;
3329 ChangeStackingLevel(tmp->icon->core, newlevel);
3330 if (dock->lowered)
3331 wLowerFrame(tmp->icon->core);
3334 if (dock->type == WM_DOCK)
3335 wScreenUpdateUsableArea(dock->screen_ptr);
3339 static void
3340 toggleCollapsed(WDock *dock)
3342 if (dock->collapsed) {
3343 dock->collapsed = 0;
3344 wDockShowIcons(dock);
3346 else {
3347 dock->collapsed = 1;
3348 wDockHideIcons(dock);
3353 static void
3354 openDockMenu(WDock *dock, WAppIcon *aicon, XEvent *event)
3356 WScreen *scr = dock->screen_ptr;
3357 WObjDescriptor *desc;
3358 WMenuEntry *entry;
3359 WApplication *wapp = NULL;
3360 int index = 0;
3361 int x_pos;
3362 int n_selected;
3363 int appIsRunning = aicon->running && aicon->icon && aicon->icon->owner;
3365 if (dock->type == WM_DOCK) {
3366 /* keep on top */
3367 entry = dock->menu->entries[index];
3368 entry->flags.indicator_on = !dock->lowered;
3369 entry->clientdata = dock;
3370 } else {
3371 /* clip options */
3372 if (scr->clip_options)
3373 updateClipOptionsMenu(scr->clip_options, dock);
3375 n_selected = numberOfSelectedIcons(dock);
3377 /* Rename Workspace */
3378 entry = dock->menu->entries[++index];
3379 if (aicon == scr->clip_icon) {
3380 entry->callback = renameCallback;
3381 entry->clientdata = dock;
3382 //entry->flags.indicator = 0;
3383 entry->text = _("Rename Workspace");
3384 } else {
3385 entry->callback = omnipresentCallback;
3386 entry->clientdata = aicon;
3387 //entry->flags.indicator = 1;
3388 //entry->flags.indicator_on = aicon->omnipresent;
3389 //entry->flags.indicator_type = MI_CHECK;
3390 //entry->text = _("Omnipresent");
3391 if (n_selected > 0) {
3392 entry->text = _("Toggle Omnipresent");
3393 } else {
3394 if (aicon->omnipresent)
3395 entry->text = _("Unset Omnipresent");
3396 else
3397 entry->text = _("Set Omnipresent");
3401 /* select/unselect icon */
3402 entry = dock->menu->entries[++index];
3403 entry->clientdata = aicon;
3404 if (aicon->icon->selected)
3405 entry->text = _("Unselect Icon");
3406 else
3407 entry->text = _("Select Icon");
3408 wMenuSetEnabled(dock->menu, index, aicon!=scr->clip_icon);
3410 /* select/unselect all icons */
3411 entry = dock->menu->entries[++index];
3412 entry->clientdata = aicon;
3413 if (n_selected > 0)
3414 entry->text = _("Unselect All Icons");
3415 else
3416 entry->text = _("Select All Icons");
3417 wMenuSetEnabled(dock->menu, index, dock->icon_count > 1);
3419 /* keep icon(s) */
3420 entry = dock->menu->entries[++index];
3421 entry->clientdata = aicon;
3422 if (n_selected > 1)
3423 entry->text = _("Keep Icons");
3424 else
3425 entry->text = _("Keep Icon");
3426 wMenuSetEnabled(dock->menu, index, dock->icon_count > 1);
3428 /* this is the workspace submenu part */
3429 entry = dock->menu->entries[++index];
3430 if (n_selected > 1)
3431 entry->text = _("Move Icons To");
3432 else
3433 entry->text = _("Move Icon To");
3434 if (scr->clip_submenu)
3435 updateWorkspaceMenu(scr->clip_submenu, aicon);
3436 wMenuSetEnabled(dock->menu, index, !aicon->omnipresent);
3438 /* remove icon(s) */
3439 entry = dock->menu->entries[++index];
3440 entry->clientdata = aicon;
3441 if (n_selected > 1)
3442 entry->text = _("Remove Icons");
3443 else
3444 entry->text = _("Remove Icon");
3445 wMenuSetEnabled(dock->menu, index, dock->icon_count > 1);
3447 /* attract icon(s) */
3448 entry = dock->menu->entries[++index];
3449 entry->clientdata = aicon;
3451 dock->menu->flags.realized = 0;
3452 wMenuRealize(dock->menu);
3455 /* launch */
3456 entry = dock->menu->entries[++index];
3457 entry->clientdata = aicon;
3458 wMenuSetEnabled(dock->menu, index, aicon->command!=NULL);
3460 /* unhide here */
3461 entry = dock->menu->entries[++index];
3462 entry->clientdata = aicon;
3463 wMenuSetEnabled(dock->menu, index, appIsRunning);
3465 /* hide */
3466 entry = dock->menu->entries[++index];
3467 entry->clientdata = aicon;
3468 if (aicon->icon->owner) {
3469 wapp = wApplicationOf(aicon->icon->owner->main_window);
3470 if (wapp && wapp->flags.hidden)
3471 entry->text = _("Unhide");
3472 else
3473 entry->text = _("Hide");
3474 } else {
3475 entry->text = _("Hide");
3477 wMenuSetEnabled(dock->menu, index, appIsRunning);
3479 /* settings */
3480 entry = dock->menu->entries[++index];
3481 entry->clientdata = aicon;
3482 wMenuSetEnabled(dock->menu, index, !aicon->editing
3483 && !wPreferences.flags.noupdates);
3485 /* kill */
3486 entry = dock->menu->entries[++index];
3487 entry->clientdata = aicon;
3488 wMenuSetEnabled(dock->menu, index, appIsRunning);
3490 if (!dock->menu->flags.realized)
3491 wMenuRealize(dock->menu);
3493 if (dock->type == WM_CLIP) {
3494 x_pos = event->xbutton.x_root+2;
3495 } else {
3496 x_pos = dock->on_right_side ?
3497 scr->scr_width - dock->menu->frame->core->width - 2 : 0;
3500 wMenuMapAt(dock->menu, x_pos, event->xbutton.y_root+2, False);
3502 /* allow drag select */
3503 event->xany.send_event = True;
3504 desc = &dock->menu->menu->descriptor;
3505 (*desc->handle_mousedown)(desc, event);
3509 static void
3510 openClipWorkspaceMenu(WScreen *scr, int x, int y)
3512 if (!scr->clip_ws_menu) {
3513 scr->clip_ws_menu = wWorkspaceMenuMake(scr, False);
3515 wWorkspaceMenuUpdate(scr, scr->clip_ws_menu);
3516 wMenuMapAt(scr->clip_ws_menu, x, y, False);
3520 /******************************************************************/
3521 static void
3522 iconDblClick(WObjDescriptor *desc, XEvent *event)
3524 WAppIcon *btn = desc->parent;
3525 WDock *dock = btn->dock;
3526 WApplication *wapp = NULL;
3527 int unhideHere = 0;
3529 #ifdef REDUCE_APPICONS
3530 if ((btn->icon->owner && !(event->xbutton.state & ControlMask)) ||
3531 ((btn->icon->owner == NULL) && (btn->applist != NULL))) {
3532 if (btn->icon->owner == NULL)
3533 btn->icon->owner = btn->applist->wapp->main_window_desc;
3535 #else
3536 if (btn->icon->owner && !(event->xbutton.state & ControlMask)) {
3537 #endif
3538 wapp = wApplicationOf(btn->icon->owner->main_window);
3540 assert(wapp!=NULL);
3542 unhideHere = (event->xbutton.state & ShiftMask);
3544 /* go to the last workspace that the user worked on the app */
3545 if (wapp->last_workspace != dock->screen_ptr->current_workspace
3546 && !unhideHere) {
3547 wWorkspaceChange(dock->screen_ptr, wapp->last_workspace);
3550 wUnhideApplication(wapp, event->xbutton.button==Button2,
3551 unhideHere);
3553 if (event->xbutton.state & MOD_MASK) {
3554 wHideOtherApplications(btn->icon->owner);
3556 } else {
3557 if (event->xbutton.button==Button1) {
3559 if (event->xbutton.state & MOD_MASK) {
3560 /* raise/lower dock */
3561 toggleLowered(dock);
3562 } else if (btn == dock->screen_ptr->clip_icon) {
3563 if (getClipButton(event->xbutton.x, event->xbutton.y)==CLIP_IDLE)
3564 toggleCollapsed(dock);
3565 else
3566 handleClipChangeWorkspace(dock->screen_ptr, event);
3567 } else if (btn->command) {
3568 if (!btn->launching &&
3569 (!btn->running || (event->xbutton.state & ControlMask))) {
3570 launchDockedApplication(btn);
3572 } else if (btn->xindex == 0 && btn->yindex == 0
3573 && btn->dock->type == WM_DOCK) {
3575 wShowGNUstepPanel(dock->screen_ptr);
3582 static void
3583 handleDockMove(WDock *dock, WAppIcon *aicon, XEvent *event)
3585 WScreen *scr = dock->screen_ptr;
3586 int ofs_x=event->xbutton.x, ofs_y=event->xbutton.y;
3587 int x, y;
3588 XEvent ev;
3589 int grabbed = 0, swapped = 0, done;
3590 Pixmap ghost = None;
3591 int superfluous = wPreferences.superfluous; /* we catch it to avoid problems */
3593 #ifdef DEBUG
3594 puts("moving dock");
3595 #endif
3596 if (XGrabPointer(dpy, aicon->icon->core->window, True, ButtonMotionMask
3597 |ButtonReleaseMask|ButtonPressMask, GrabModeAsync,
3598 GrabModeAsync, None, None, CurrentTime) !=GrabSuccess) {
3599 wwarning("pointer grab failed for dock move");
3601 y = 0;
3602 for (x=0; x<dock->max_icons; x++) {
3603 if (dock->icon_array[x]!=NULL &&
3604 dock->icon_array[x]->yindex > y)
3605 y = dock->icon_array[x]->yindex;
3607 y++;
3608 XResizeWindow(dpy, scr->dock_shadow, ICON_SIZE, ICON_SIZE*y);
3610 done = 0;
3611 while (!done) {
3612 WMMaskEvent(dpy, PointerMotionMask|ButtonReleaseMask|ButtonPressMask
3613 |ButtonMotionMask|ExposureMask, &ev);
3614 switch (ev.type) {
3615 case Expose:
3616 WMHandleEvent(&ev);
3617 break;
3619 case MotionNotify:
3620 if (!grabbed) {
3621 if (abs(ofs_x-ev.xmotion.x)>=MOVE_THRESHOLD
3622 || abs(ofs_y-ev.xmotion.y)>=MOVE_THRESHOLD) {
3623 XChangeActivePointerGrab(dpy, ButtonMotionMask
3624 |ButtonReleaseMask|ButtonPressMask,
3625 wCursor[WCUR_MOVE], CurrentTime);
3626 grabbed=1;
3628 break;
3630 if (dock->type == WM_CLIP) {
3631 if (ev.xmotion.x_root - ofs_x < 0) {
3632 x = 0;
3633 } else if (ev.xmotion.x_root - ofs_x + ICON_SIZE >
3634 scr->scr_width) {
3635 x = scr->scr_width - ICON_SIZE;
3636 } else {
3637 x = ev.xmotion.x_root - ofs_x;
3639 if (ev.xmotion.y_root - ofs_y < 0) {
3640 y = 0;
3641 } else if (ev.xmotion.y_root - ofs_y + ICON_SIZE >
3642 scr->scr_height) {
3643 y = scr->scr_height - ICON_SIZE;
3644 } else {
3645 y = ev.xmotion.y_root - ofs_y;
3647 moveDock(dock, x, y);
3648 } else {
3649 /* move vertically if pointer is inside the dock*/
3650 if ((dock->on_right_side &&
3651 ev.xmotion.x_root >= dock->x_pos - ICON_SIZE)
3652 || (!dock->on_right_side &&
3653 ev.xmotion.x_root <= dock->x_pos + ICON_SIZE*2)) {
3655 if (ev.xmotion.y_root - ofs_y < 0) {
3656 y = 0;
3657 } else if (ev.xmotion.y_root - ofs_y + ICON_SIZE >
3658 scr->scr_height) {
3659 y = scr->scr_height - ICON_SIZE;
3660 } else {
3661 y = ev.xmotion.y_root - ofs_y;
3663 moveDock(dock, dock->x_pos, y);
3665 /* move horizontally to change sides */
3666 x = ev.xmotion.x_root - ofs_x;
3667 if (!dock->on_right_side) {
3669 /* is on left */
3671 if (ev.xmotion.x_root > dock->x_pos + ICON_SIZE*2) {
3672 XMoveWindow(dpy, scr->dock_shadow, scr->scr_width-ICON_SIZE
3673 -DOCK_EXTRA_SPACE-1, dock->y_pos);
3674 if (superfluous && ghost==None) {
3675 ghost = MakeGhostDock(dock, dock->x_pos,
3676 scr->scr_width-ICON_SIZE
3677 -DOCK_EXTRA_SPACE-1,
3678 dock->y_pos);
3679 XSetWindowBackgroundPixmap(dpy, scr->dock_shadow,
3680 ghost);
3681 XClearWindow(dpy, scr->dock_shadow);
3683 XMapRaised(dpy, scr->dock_shadow);
3684 swapped = 1;
3685 } else {
3686 if (superfluous && ghost!=None) {
3687 XFreePixmap(dpy, ghost);
3688 ghost = None;
3690 XUnmapWindow(dpy, scr->dock_shadow);
3691 swapped = 0;
3693 } else {
3694 /* is on right */
3695 if (ev.xmotion.x_root < dock->x_pos - ICON_SIZE) {
3696 XMoveWindow(dpy, scr->dock_shadow,
3697 DOCK_EXTRA_SPACE, dock->y_pos);
3698 if (superfluous && ghost==None) {
3699 ghost = MakeGhostDock(dock, dock->x_pos,
3700 DOCK_EXTRA_SPACE, dock->y_pos);
3701 XSetWindowBackgroundPixmap(dpy, scr->dock_shadow,
3702 ghost);
3703 XClearWindow(dpy, scr->dock_shadow);
3705 XMapRaised(dpy, scr->dock_shadow);
3706 swapped = -1;
3707 } else {
3708 XUnmapWindow(dpy, scr->dock_shadow);
3709 swapped = 0;
3710 if (superfluous && ghost!=None) {
3711 XFreePixmap(dpy, ghost);
3712 ghost = None;
3717 break;
3719 case ButtonPress:
3720 break;
3722 case ButtonRelease:
3723 if (ev.xbutton.button != event->xbutton.button)
3724 break;
3725 XUngrabPointer(dpy, CurrentTime);
3726 XUnmapWindow(dpy, scr->dock_shadow);
3727 XResizeWindow(dpy, scr->dock_shadow, ICON_SIZE, ICON_SIZE);
3728 if (dock->type == WM_DOCK) {
3729 if (swapped!=0) {
3730 if (swapped>0)
3731 dock->on_right_side = 1;
3732 else
3733 dock->on_right_side = 0;
3734 swapDock(dock);
3735 wArrangeIcons(scr, False);
3738 done = 1;
3739 break;
3742 if (superfluous) {
3743 if (ghost!=None)
3744 XFreePixmap(dpy, ghost);
3745 XSetWindowBackground(dpy, scr->dock_shadow, scr->white_pixel);
3747 #ifdef DEBUG
3748 puts("End dock move");
3749 #endif
3754 static void
3755 handleIconMove(WDock *dock, WAppIcon *aicon, XEvent *event)
3757 WScreen *scr = dock->screen_ptr;
3758 Window wins[2];
3759 WIcon *icon = aicon->icon;
3760 WDock *dock2 = NULL, *last_dock = dock, *clip = NULL;
3761 int ondock, grabbed = 0, change_dock = 0, collapsed = 0;
3762 XEvent ev;
3763 int x = aicon->x_pos, y = aicon->y_pos;
3764 int ofs_x = event->xbutton.x, ofs_y = event->xbutton.y;
3765 int shad_x = x, shad_y = y;
3766 int ix = aicon->xindex, iy = aicon->yindex;
3767 int tmp;
3768 Pixmap ghost = None;
3769 Bool docked;
3770 int superfluous = wPreferences.superfluous; /* we catch it to avoid problems */
3771 int omnipresent = aicon->omnipresent; /* this must be cached!!! */
3774 if (wPreferences.flags.noupdates)
3775 return;
3777 if (XGrabPointer(dpy, icon->core->window, True, ButtonMotionMask
3778 |ButtonReleaseMask|ButtonPressMask, GrabModeAsync,
3779 GrabModeAsync, None, None, CurrentTime) !=GrabSuccess) {
3780 #ifdef DEBUG0
3781 wwarning("pointer grab failed for icon move");
3782 #endif
3785 if (!(event->xbutton.state & MOD_MASK))
3786 wRaiseFrame(icon->core);
3788 if (!wPreferences.flags.noclip)
3789 clip = scr->workspaces[scr->current_workspace]->clip;
3791 if (dock == scr->dock && !wPreferences.flags.noclip)
3792 dock2 = clip;
3793 else if (dock != scr->dock && !wPreferences.flags.nodock)
3794 dock2 = scr->dock;
3796 wins[0] = icon->core->window;
3797 wins[1] = scr->dock_shadow;
3798 XRestackWindows(dpy, wins, 2);
3799 XMoveResizeWindow(dpy, scr->dock_shadow, aicon->x_pos, aicon->y_pos,
3800 ICON_SIZE, ICON_SIZE);
3801 if (superfluous) {
3802 if (icon->pixmap!=None)
3803 ghost = MakeGhostIcon(scr, icon->pixmap);
3804 else
3805 ghost = MakeGhostIcon(scr, icon->core->window);
3807 XSetWindowBackgroundPixmap(dpy, scr->dock_shadow, ghost);
3808 XClearWindow(dpy, scr->dock_shadow);
3810 XMapWindow(dpy, scr->dock_shadow);
3812 ondock = 1;
3815 while(1) {
3816 XMaskEvent(dpy, PointerMotionMask|ButtonReleaseMask|ButtonPressMask
3817 |ButtonMotionMask|ExposureMask, &ev);
3818 switch (ev.type) {
3819 case Expose:
3820 WMHandleEvent(&ev);
3821 break;
3823 case MotionNotify:
3824 if (!grabbed) {
3825 if (abs(ofs_x-ev.xmotion.x)>=MOVE_THRESHOLD
3826 || abs(ofs_y-ev.xmotion.y)>=MOVE_THRESHOLD) {
3827 XChangeActivePointerGrab(dpy, ButtonMotionMask
3828 |ButtonReleaseMask|ButtonPressMask,
3829 wCursor[WCUR_MOVE], CurrentTime);
3830 grabbed=1;
3831 } else {
3832 break;
3836 if (omnipresent) {
3837 int i;
3838 for (i=0; i<scr->workspace_count; i++) {
3839 if (i == scr->current_workspace)
3840 continue;
3841 wDockShowIcons(scr->workspaces[i]->clip);
3845 x = ev.xmotion.x_root - ofs_x;
3846 y = ev.xmotion.y_root - ofs_y;
3847 tmp = wDockSnapIcon(dock, aicon, x, y, &ix, &iy, True);
3848 if (tmp && dock2) {
3849 change_dock = 0;
3850 if (last_dock != dock && collapsed) {
3851 last_dock->collapsed = 1;
3852 wDockHideIcons(last_dock);
3853 collapsed = 0;
3855 if (!collapsed && (collapsed = dock->collapsed)) {
3856 dock->collapsed = 0;
3857 wDockShowIcons(dock);
3859 if (dock->auto_raise_lower)
3860 wDockRaise(dock);
3861 last_dock = dock;
3862 } else if (dock2) {
3863 tmp = wDockSnapIcon(dock2, aicon, x, y, &ix, &iy, False);
3864 if (tmp) {
3865 change_dock = 1;
3866 if (last_dock != dock2 && collapsed) {
3867 last_dock->collapsed = 1;
3868 wDockHideIcons(last_dock);
3869 collapsed = 0;
3871 if (!collapsed && (collapsed = dock2->collapsed)) {
3872 dock2->collapsed = 0;
3873 wDockShowIcons(dock2);
3875 if (dock2->auto_raise_lower)
3876 wDockRaise(dock2);
3877 last_dock = dock2;
3880 if (aicon->launching
3881 || (aicon->running && !(ev.xmotion.state & MOD_MASK))
3882 || (!aicon->running && tmp)) {
3883 shad_x = last_dock->x_pos + ix*wPreferences.icon_size;
3884 shad_y = last_dock->y_pos + iy*wPreferences.icon_size;
3886 XMoveWindow(dpy, scr->dock_shadow, shad_x, shad_y);
3888 if (!ondock) {
3889 XMapWindow(dpy, scr->dock_shadow);
3891 ondock = 1;
3892 } else {
3893 if (ondock) {
3894 XUnmapWindow(dpy, scr->dock_shadow);
3896 ondock = 0;
3898 XMoveWindow(dpy, icon->core->window, x, y);
3899 break;
3901 case ButtonPress:
3902 break;
3904 case ButtonRelease:
3905 if (ev.xbutton.button != event->xbutton.button)
3906 break;
3907 XUngrabPointer(dpy, CurrentTime);
3908 if (ondock) {
3909 SlideWindow(icon->core->window, x, y, shad_x, shad_y);
3910 XUnmapWindow(dpy, scr->dock_shadow);
3911 if (!change_dock) {
3912 reattachIcon(dock, aicon, ix, iy);
3913 if (clip && dock!=clip && clip->auto_raise_lower)
3914 wDockLower(clip);
3915 } else {
3916 docked = moveIconBetweenDocks(dock, dock2, aicon, ix, iy);
3917 if (!docked) {
3918 /* Slide it back if dock rejected it */
3919 SlideWindow(icon->core->window, x, y, aicon->x_pos,
3920 aicon->y_pos);
3921 reattachIcon(dock, aicon, aicon->xindex,aicon->yindex);
3923 if (last_dock->type==WM_CLIP && last_dock->auto_collapse) {
3924 collapsed = 0;
3927 } else {
3928 aicon->x_pos = x;
3929 aicon->y_pos = y;
3930 if (superfluous) {
3931 if (!aicon->running && !wPreferences.no_animations) {
3932 /* We need to deselect it, even if is deselected in
3933 * wDockDetach(), because else DoKaboom() will fail.
3935 if (aicon->icon->selected)
3936 wIconSelect(aicon->icon);
3937 DoKaboom(scr,aicon->icon->core->window, x, y);
3940 if (clip && clip->auto_raise_lower)
3941 wDockLower(clip);
3942 wDockDetach(dock, aicon);
3944 if (collapsed) {
3945 last_dock->collapsed = 1;
3946 wDockHideIcons(last_dock);
3947 collapsed = 0;
3949 if (superfluous) {
3950 if (ghost!=None)
3951 XFreePixmap(dpy, ghost);
3952 XSetWindowBackground(dpy, scr->dock_shadow, scr->white_pixel);
3954 if (omnipresent) {
3955 int i;
3956 for (i=0; i<scr->workspace_count; i++) {
3957 if (i == scr->current_workspace)
3958 continue;
3959 wDockHideIcons(scr->workspaces[i]->clip);
3963 #ifdef DEBUG
3964 puts("End icon move");
3965 #endif
3966 return;
3972 static int
3973 getClipButton(int px, int py)
3975 int pt = (CLIP_BUTTON_SIZE+2)*ICON_SIZE/64;
3977 if (px < 0 || py < 0 || px >= ICON_SIZE || py >= ICON_SIZE)
3978 return CLIP_IDLE;
3980 if (py <= pt-((int)ICON_SIZE-1-px))
3981 return CLIP_FORWARD;
3982 else if (px <= pt-((int)ICON_SIZE-1-py))
3983 return CLIP_REWIND;
3985 return CLIP_IDLE;
3989 static void
3990 handleClipChangeWorkspace(WScreen *scr, XEvent *event)
3992 XEvent ev;
3993 int done, direction, new_ws;
3994 int new_dir;
3995 WDock *clip = scr->clip_icon->dock;
3997 direction = getClipButton(event->xbutton.x, event->xbutton.y);
3999 clip->lclip_button_pushed = direction==CLIP_REWIND;
4000 clip->rclip_button_pushed = direction==CLIP_FORWARD;
4002 wClipIconPaint(scr->clip_icon);
4003 done = 0;
4004 while(!done) {
4005 WMMaskEvent(dpy, ExposureMask|ButtonMotionMask|ButtonReleaseMask
4006 |ButtonPressMask, &ev);
4007 switch (ev.type) {
4008 case Expose:
4009 WMHandleEvent(&ev);
4010 break;
4012 case MotionNotify:
4013 new_dir = getClipButton(ev.xmotion.x, ev.xmotion.y);
4014 if (new_dir != direction) {
4015 direction = new_dir;
4016 clip->lclip_button_pushed = direction==CLIP_REWIND;
4017 clip->rclip_button_pushed = direction==CLIP_FORWARD;
4018 wClipIconPaint(scr->clip_icon);
4020 break;
4022 case ButtonPress:
4023 break;
4025 case ButtonRelease:
4026 if (ev.xbutton.button == event->xbutton.button)
4027 done = 1;
4031 clip->lclip_button_pushed = 0;
4032 clip->rclip_button_pushed = 0;
4034 new_ws = wPreferences.ws_advance || (event->xbutton.state & ControlMask);
4036 if (direction == CLIP_FORWARD) {
4037 if (scr->current_workspace < scr->workspace_count-1)
4038 wWorkspaceChange(scr, scr->current_workspace+1);
4039 else if (new_ws && scr->current_workspace < MAX_WORKSPACES-1)
4040 wWorkspaceChange(scr, scr->current_workspace+1);
4041 else if (wPreferences.ws_cycle)
4042 wWorkspaceChange(scr, 0);
4044 else if (direction == CLIP_REWIND) {
4045 if (scr->current_workspace > 0)
4046 wWorkspaceChange(scr, scr->current_workspace-1);
4047 else if (scr->current_workspace==0 && wPreferences.ws_cycle)
4048 wWorkspaceChange(scr, scr->workspace_count-1);
4051 wClipIconPaint(scr->clip_icon);
4055 static void
4056 iconMouseDown(WObjDescriptor *desc, XEvent *event)
4058 WAppIcon *aicon = desc->parent;
4059 WDock *dock = aicon->dock;
4060 WScreen *scr = aicon->icon->core->screen_ptr;
4062 if (aicon->editing || WCHECK_STATE(WSTATE_MODAL))
4063 return;
4065 scr->last_dock = dock;
4067 if (dock->menu->flags.mapped)
4068 wMenuUnmap(dock->menu);
4070 if (IsDoubleClick(scr, event)) {
4071 /* double-click was not in the main clip icon */
4072 if (dock->type != WM_CLIP || aicon->xindex!=0 || aicon->yindex!=0
4073 || getClipButton(event->xbutton.x, event->xbutton.y)==CLIP_IDLE) {
4074 iconDblClick(desc, event);
4075 return;
4079 if (dock->type == WM_CLIP && scr->flags.clip_balloon_mapped) {
4080 XUnmapWindow(dpy, scr->clip_balloon);
4081 scr->flags.clip_balloon_mapped = 0;
4084 #ifdef DEBUG
4085 puts("handling dock");
4086 #endif
4087 if (event->xbutton.button == Button1) {
4088 if (event->xbutton.state & MOD_MASK)
4089 wDockLower(dock);
4090 else
4091 wDockRaise(dock);
4093 if ((event->xbutton.state & ShiftMask) && aicon!=scr->clip_icon &&
4094 dock->type!=WM_DOCK) {
4095 wIconSelect(aicon->icon);
4096 return;
4099 if (aicon->yindex==0 && aicon->xindex==0) {
4100 if (getClipButton(event->xbutton.x, event->xbutton.y)!=CLIP_IDLE
4101 && dock->type==WM_CLIP)
4102 handleClipChangeWorkspace(scr, event);
4103 else
4104 handleDockMove(dock, aicon, event);
4105 } else
4106 handleIconMove(dock, aicon, event);
4108 } else if (event->xbutton.button==Button2 && dock->type==WM_CLIP &&
4109 aicon==scr->clip_icon) {
4110 openClipWorkspaceMenu(scr, event->xbutton.x_root+2,
4111 event->xbutton.y_root+2);
4112 if (scr->clip_ws_menu) {
4113 WMenu *menu;
4114 menu = scr->clip_ws_menu;
4115 desc = &menu->menu->descriptor;
4117 event->xany.send_event = True;
4118 (*desc->handle_mousedown)(desc, event);
4120 } else if (event->xbutton.button==Button2 && dock->type==WM_CLIP &&
4121 (event->xbutton.state & ShiftMask) && aicon!=scr->clip_icon) {
4122 wClipMakeIconOmnipresent(aicon, !aicon->omnipresent);
4123 } else if (event->xbutton.button == Button3) {
4124 openDockMenu(dock, aicon, event);
4129 static void
4130 showClipBalloon(WDock *dock, int workspace)
4132 int w, h;
4133 int x, y;
4134 WScreen *scr = dock->screen_ptr;
4135 char *text;
4136 Window stack[2];
4138 scr->flags.clip_balloon_mapped = 1;
4139 XMapWindow(dpy, scr->clip_balloon);
4141 text = scr->workspaces[workspace]->name;
4143 w = WMWidthOfString(scr->clip_title_font, text, strlen(text));
4145 h = WMFontHeight(scr->clip_title_font);
4146 XResizeWindow(dpy, scr->clip_balloon, w, h);
4148 x = dock->x_pos + CLIP_BUTTON_SIZE*ICON_SIZE/64;
4149 y = dock->y_pos + ICON_SIZE - WMFontHeight(scr->clip_title_font) - 3;
4151 if (x+w > scr->scr_width) {
4152 x = scr->scr_width - w;
4153 if (dock->y_pos + ICON_SIZE + h > scr->scr_height)
4154 y = dock->y_pos - h - 1;
4155 else
4156 y = dock->y_pos + ICON_SIZE;
4157 XRaiseWindow(dpy, scr->clip_balloon);
4158 } else {
4159 stack[0] = scr->clip_icon->icon->core->window;
4160 stack[1] = scr->clip_balloon;
4161 XRestackWindows(dpy, stack, 2);
4163 XMoveWindow(dpy, scr->clip_balloon, x, y);
4164 XSetForeground(dpy, scr->clip_title_gc,
4165 scr->clip_title_pixel[CLIP_NORMAL]);
4166 XClearWindow(dpy, scr->clip_balloon);
4167 WMDrawString(scr->wmscreen, scr->clip_balloon, scr->clip_title_gc,
4168 scr->clip_title_font, 0, 0, text, strlen(text));
4172 static void
4173 clipEnterNotify(WObjDescriptor *desc, XEvent *event)
4175 WAppIcon *btn = (WAppIcon*)desc->parent;
4176 WDock *dock;
4177 WScreen *scr;
4179 assert(event->type==EnterNotify);
4181 if(desc->parent_type!=WCLASS_DOCK_ICON)
4182 return;
4184 scr = btn->icon->core->screen_ptr;
4185 if (!btn->omnipresent)
4186 dock = btn->dock;
4187 else
4188 dock = scr->workspaces[scr->current_workspace]->clip;
4190 if (!dock || dock->type!=WM_CLIP)
4191 return;
4193 /* The auto raise/lower code */
4194 if (dock->auto_lower_magic) {
4195 WMDeleteTimerHandler(dock->auto_lower_magic);
4196 dock->auto_lower_magic = NULL;
4198 if (dock->auto_raise_lower && !dock->auto_raise_magic) {
4199 dock->auto_raise_magic = WMAddTimerHandler(AUTO_RAISE_DELAY,
4200 clipAutoRaise,
4201 (void *)dock);
4204 /* The auto expand/collapse code */
4205 if (dock->auto_collapse_magic) {
4206 WMDeleteTimerHandler(dock->auto_collapse_magic);
4207 dock->auto_collapse_magic = NULL;
4209 if (dock->auto_collapse && !dock->auto_expand_magic) {
4210 dock->auto_expand_magic = WMAddTimerHandler(AUTO_EXPAND_DELAY,
4211 clipAutoExpand,
4212 (void *)dock);
4215 if (btn->xindex == 0 && btn->yindex == 0)
4216 showClipBalloon(dock, dock->screen_ptr->current_workspace);
4217 else {
4218 if (dock->screen_ptr->flags.clip_balloon_mapped) {
4219 XUnmapWindow(dpy, dock->screen_ptr->clip_balloon);
4220 dock->screen_ptr->flags.clip_balloon_mapped = 0;
4226 static void
4227 clipLeave(WDock *dock)
4229 XEvent event;
4230 WObjDescriptor *desc = NULL;
4232 if (!dock || dock->type!=WM_CLIP)
4233 return;
4235 if (XCheckTypedEvent(dpy, EnterNotify, &event)!=False) {
4236 if (XFindContext(dpy, event.xcrossing.window, wWinContext,
4237 (XPointer *)&desc)!=XCNOENT
4238 && desc && desc->parent_type==WCLASS_DOCK_ICON
4239 && ((WAppIcon*)desc->parent)->dock
4240 && ((WAppIcon*)desc->parent)->dock->type==WM_CLIP) {
4241 /* We didn't left the Clip yet */
4242 XPutBackEvent(dpy, &event);
4243 return;
4246 XPutBackEvent(dpy, &event);
4247 } else {
4248 /* We entered a withdrawn window, so we're still in Clip */
4249 return;
4252 if (dock->auto_raise_magic) {
4253 WMDeleteTimerHandler(dock->auto_raise_magic);
4254 dock->auto_raise_magic = NULL;
4256 if (dock->auto_raise_lower && !dock->auto_lower_magic) {
4257 dock->auto_lower_magic = WMAddTimerHandler(AUTO_LOWER_DELAY,
4258 clipAutoLower,
4259 (void *)dock);
4262 if (dock->auto_expand_magic) {
4263 WMDeleteTimerHandler(dock->auto_expand_magic);
4264 dock->auto_expand_magic = NULL;
4266 if (dock->auto_collapse && !dock->auto_collapse_magic) {
4267 dock->auto_collapse_magic = WMAddTimerHandler(AUTO_COLLAPSE_DELAY,
4268 clipAutoCollapse,
4269 (void *)dock);
4274 static void
4275 clipLeaveNotify(WObjDescriptor *desc, XEvent *event)
4277 WAppIcon *btn = (WAppIcon*)desc->parent;
4279 assert(event->type==LeaveNotify);
4281 if(desc->parent_type!=WCLASS_DOCK_ICON)
4282 return;
4284 clipLeave(btn->dock);
4288 static void
4289 clipAutoCollapse(void *cdata)
4291 WDock *dock = (WDock *)cdata;
4293 if (dock->type!=WM_CLIP)
4294 return;
4296 if (dock->auto_collapse) {
4297 dock->collapsed = 1;
4298 wDockHideIcons(dock);
4300 dock->auto_collapse_magic = NULL;
4304 static void
4305 clipAutoExpand(void *cdata)
4307 WDock *dock = (WDock *)cdata;
4309 if (dock->type!=WM_CLIP)
4310 return;
4312 if (dock->auto_collapse) {
4313 dock->collapsed = 0;
4314 wDockShowIcons(dock);
4316 dock->auto_expand_magic = NULL;
4320 static void
4321 clipAutoLower(void *cdata)
4323 WDock *dock = (WDock *)cdata;
4325 if (dock->type!=WM_CLIP)
4326 return;
4328 if (dock->auto_raise_lower)
4329 wDockLower(dock);
4331 dock->auto_lower_magic = NULL;
4335 static void
4336 clipAutoRaise(void *cdata)
4338 WDock *dock = (WDock *)cdata;
4340 if (dock->type!=WM_CLIP)
4341 return;
4343 if (dock->auto_raise_lower)
4344 wDockRaise(dock);
4346 if (dock->screen_ptr->flags.clip_balloon_mapped) {
4347 showClipBalloon(dock, dock->screen_ptr->current_workspace);
4350 dock->auto_raise_magic = NULL;
4354 static Bool
4355 iconCanBeOmnipresent(WAppIcon *aicon)
4357 WScreen *scr = aicon->icon->core->screen_ptr;
4358 WDock *clip;
4359 WAppIcon *btn;
4360 int i, j;
4362 for (i=0; i<scr->workspace_count; i++) {
4363 clip = scr->workspaces[i]->clip;
4365 if (clip == aicon->dock)
4366 continue;
4368 if (clip->icon_count + scr->global_icon_count >= clip->max_icons)
4369 return False; /* Clip is full in some workspace */
4371 for (j=0; j<clip->max_icons; j++) {
4372 btn = clip->icon_array[j];
4373 if(btn && btn->xindex==aicon->xindex && btn->yindex==aicon->yindex)
4374 return False;
4378 return True;
4383 wClipMakeIconOmnipresent(WAppIcon *aicon, int omnipresent)
4385 WScreen *scr = aicon->icon->core->screen_ptr;
4386 WAppIconChain *new_entry, *tmp, *tmp1;
4387 int status = WO_SUCCESS;
4389 if (aicon->dock == scr->dock || aicon == scr->clip_icon)
4390 return WO_NOT_APPLICABLE;
4392 if (aicon->omnipresent == omnipresent)
4393 return WO_SUCCESS;
4395 if (omnipresent) {
4396 if (iconCanBeOmnipresent(aicon)) {
4397 aicon->omnipresent = 1;
4398 new_entry = wmalloc(sizeof(WAppIconChain));
4399 new_entry->aicon = aicon;
4400 new_entry->next = NULL;
4401 if (!scr->global_icons) {
4402 scr->global_icons = new_entry;
4403 } else {
4404 tmp = scr->global_icons;
4405 while (tmp->next)
4406 tmp = tmp->next;
4408 tmp->next = new_entry;
4410 scr->global_icon_count++;
4411 } else {
4412 aicon->omnipresent = 0;
4413 status = WO_FAILED;
4415 } else {
4416 aicon->omnipresent = 0;
4417 if (aicon == scr->global_icons->aicon) {
4418 tmp = scr->global_icons->next;
4419 free(scr->global_icons);
4420 scr->global_icons = tmp;
4421 scr->global_icon_count--;
4422 } else {
4423 tmp = scr->global_icons;
4424 while (tmp->next) {
4425 if (tmp->next->aicon == aicon) {
4426 tmp1 = tmp->next->next;
4427 free(tmp->next);
4428 tmp->next = tmp1;
4429 scr->global_icon_count--;
4430 break;
4432 tmp = tmp->next;
4437 wAppIconPaint(aicon);
4439 return status;