- renamed wstrappend() to wstrconcat().
[wmaker-crm.git] / src / dock.c
blob2bb519754d611d484286f8d3ce855f25cde40865
1 /* dock.c- built-in Dock module for WindowMaker
3 * Window Maker window manager
5 * Copyright (c) 1997, 1998 Alfredo K. Kojima
6 * Copyright (c) 1998, 1999 Dan Pascu
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
21 * USA.
25 #include "wconfig.h"
27 #include <X11/Xlib.h>
28 #include <X11/Xutil.h>
29 #include <stdlib.h>
30 #include <string.h>
31 #include <unistd.h>
32 #include <math.h>
33 #include <limits.h>
35 #ifndef PATH_MAX
36 #define PATH_MAX DEFAULT_PATH_MAX
37 #endif
39 #include "WindowMaker.h"
40 #include "wcore.h"
41 #include "window.h"
42 #include "icon.h"
43 #include "appicon.h"
44 #include "actions.h"
45 #include "stacking.h"
46 #include "dock.h"
47 #include "dialog.h"
48 #include "funcs.h"
49 #include "properties.h"
50 #include "menu.h"
51 #include "client.h"
52 #include "defaults.h"
53 #include "workspace.h"
54 #include "framewin.h"
55 #include "superfluous.h"
56 #include "wmsound.h"
59 #include <proplist.h>
64 /**** Local variables ****/
65 #define CLIP_REWIND 1
66 #define CLIP_IDLE 0
67 #define CLIP_FORWARD 2
70 /**** Global variables ****/
72 /* in dockedapp.c */
73 extern void DestroyDockAppSettingsPanel();
75 extern void ShowDockAppSettingsPanel(WAppIcon *aicon);
78 extern XContext wWinContext;
80 extern Cursor wCursor[WCUR_LAST];
82 extern WPreferences wPreferences;
84 extern XContext wWinContext;
86 #ifdef OFFIX_DND
87 extern Atom _XA_DND_PROTOCOL;
88 #endif
91 #define MOD_MASK wPreferences.modifier_mask
93 extern void appIconMouseDown(WObjDescriptor *desc, XEvent *event);
95 #define ICON_SIZE wPreferences.icon_size
98 /***** Local variables ****/
100 static proplist_t dCommand=NULL;
101 #ifdef OFFIX_DND
102 static proplist_t dDropCommand=NULL;
103 #endif
104 static proplist_t dAutoLaunch, dLock;
105 static proplist_t dName, dForced, dBuggyApplication, dYes, dNo;
106 static proplist_t dHost, dDock, dClip;
107 static proplist_t dAutoAttractIcons;
109 static proplist_t dPosition, dApplications, dLowered, dCollapsed, dAutoCollapse;
111 static proplist_t dAutoRaiseLower, dOmnipresent;
113 static void dockIconPaint(WAppIcon *btn);
115 static void iconMouseDown(WObjDescriptor *desc, XEvent *event);
117 static pid_t execCommand(WAppIcon *btn, char *command, WSavedState *state);
119 static void trackDeadProcess(pid_t pid, unsigned char status, WDock *dock);
121 static int getClipButton(int px, int py);
123 static void toggleLowered(WDock *dock);
125 static void toggleCollapsed(WDock *dock);
127 static void clipIconExpose(WObjDescriptor *desc, XEvent *event);
129 static void clipLeave(WDock *dock);
131 static void handleClipChangeWorkspace(WScreen *scr, XEvent *event);
133 Bool moveIconBetweenDocks(WDock *src, WDock *dest, WAppIcon *icon, int x, int y);
135 static void clipEnterNotify(WObjDescriptor *desc, XEvent *event);
136 static void clipLeaveNotify(WObjDescriptor *desc, XEvent *event);
137 static void clipAutoCollapse(void *cdata);
138 static void clipAutoExpand(void *cdata);
139 static void launchDockedApplication(WAppIcon *btn);
141 static void clipAutoLower(void *cdata);
142 static void clipAutoRaise(void *cdata);
144 static void showClipBalloon(WDock *dock, int workspace);
146 #ifdef OFFIX_DND
148 #define DndNotDnd -1
149 #define DndUnknown 0
150 #define DndRawData 1
151 #define DndFile 2
152 #define DndFiles 3
153 #define DndText 4
154 #define DndDir 5
155 #define DndLink 6
156 #define DndExe 7
158 #define DndEND 8
160 #endif /* OFFIX_DND */
164 static void
165 make_keys()
167 if (dCommand!=NULL)
168 return;
170 dCommand = PLRetain(PLMakeString("Command"));
171 #ifdef OFFIX_DND
172 dDropCommand = PLRetain(PLMakeString("DropCommand"));
173 #endif
174 dLock = PLRetain(PLMakeString("Lock"));
175 dAutoLaunch = PLRetain(PLMakeString("AutoLaunch"));
176 dName = PLRetain(PLMakeString("Name"));
177 dForced = PLRetain(PLMakeString("Forced"));
178 dBuggyApplication = PLRetain(PLMakeString("BuggyApplication"));
179 dYes = PLRetain(PLMakeString("Yes"));
180 dNo = PLRetain(PLMakeString("No"));
181 dHost = PLRetain(PLMakeString("Host"));
183 dPosition = PLMakeString("Position");
184 dApplications = PLMakeString("Applications");
185 dLowered = PLMakeString("Lowered");
186 dCollapsed = PLMakeString("Collapsed");
187 dAutoCollapse = PLMakeString("AutoCollapse");
188 dAutoRaiseLower = PLMakeString("AutoRaiseLower");
189 dAutoAttractIcons = PLMakeString("AutoAttractIcons");
191 dOmnipresent = PLMakeString("Omnipresent");
193 dDock = PLMakeString("Dock");
194 dClip = PLMakeString("Clip");
199 static void
200 renameCallback(WMenu *menu, WMenuEntry *entry)
202 WDock *dock = entry->clientdata;
203 char buffer[128];
204 int wspace;
205 char *name;
207 assert(entry->clientdata!=NULL);
209 wspace = dock->screen_ptr->current_workspace;
211 name = wstrdup(dock->screen_ptr->workspaces[wspace]->name);
213 sprintf(buffer, _("Type the name for workspace %i:"), wspace+1);
214 if (wInputDialog(dock->screen_ptr, _("Rename Workspace"), buffer,
215 &name)) {
216 wWorkspaceRename(dock->screen_ptr, wspace, name);
218 if (name) {
219 wfree(name);
224 static void
225 toggleLoweredCallback(WMenu *menu, WMenuEntry *entry)
227 assert(entry->clientdata!=NULL);
229 toggleLowered(entry->clientdata);
231 wMenuPaint(menu);
236 static void
237 killCallback(WMenu *menu, WMenuEntry *entry)
239 WAppIcon *icon;
240 #ifdef REDUCE_APPICONS
241 WAppIconAppList *tapplist;
243 extern Atom _XA_WM_DELETE_WINDOW;
244 #else
245 char *buffer;
246 #endif
248 if (!WCHECK_STATE(WSTATE_NORMAL))
249 return;
251 assert(entry->clientdata!=NULL);
253 icon = (WAppIcon*)entry->clientdata;
255 icon->editing = 1;
257 WCHANGE_STATE(WSTATE_MODAL);
259 #ifdef REDUCE_APPICONS
260 /* Send a delete message to the main window of each application
261 * bound to this docked appicon. - cls
263 tapplist = icon->applist;
264 while (tapplist != NULL) {
265 if (tapplist->wapp->main_window_desc != NULL) {
266 if (tapplist->wapp->main_window_desc->protocols.DELETE_WINDOW) {
267 wClientSendProtocol(tapplist->wapp->main_window_desc,
268 _XA_WM_DELETE_WINDOW, CurrentTime);
269 } else {
270 wClientKill(tapplist->wapp->main_window_desc);
273 tapplist = tapplist->next;
275 #else
276 buffer = wstrconcat(icon->wm_class,
277 _(" will be forcibly closed.\n"
278 "Any unsaved changes will be lost.\n"
279 "Please confirm."));
281 if (wPreferences.dont_confirm_kill
282 || wMessageDialog(menu->frame->screen_ptr, _("Kill Application"),
283 buffer, _("Yes"), _("No"), NULL)==WAPRDefault) {
284 if (icon->icon && icon->icon->owner) {
285 wClientKill(icon->icon->owner);
289 wfree(buffer);
290 #endif /* !REDUCE_APPICONS */
292 icon->editing = 0;
294 WCHANGE_STATE(WSTATE_NORMAL);
298 /* TODO: replace this function with a member of the dock struct */
299 static int
300 numberOfSelectedIcons(WDock *dock)
302 WAppIcon *aicon;
303 int i, n;
305 n = 0;
306 for (i=1; i<dock->max_icons; i++) {
307 aicon = dock->icon_array[i];
308 if (aicon && aicon->icon->selected) {
309 n++;
313 return n;
317 static WMBag*
318 getSelected(WDock *dock)
320 WMBag *ret = WMCreateBag(8);
321 WAppIcon *btn;
322 int i;
324 for (i=1; i<dock->max_icons; i++) {
325 btn = dock->icon_array[i];
326 if (btn && btn->icon->selected) {
327 WMPutInBag(ret, btn);
331 return ret;
335 static void
336 paintClipButtons(WAppIcon *clipIcon, Bool lpushed, Bool rpushed)
338 Window win = clipIcon->icon->core->window;
339 WScreen *scr = clipIcon->icon->core->screen_ptr;
340 XPoint p[4];
341 int pt = CLIP_BUTTON_SIZE*ICON_SIZE/64;
342 int tp = ICON_SIZE - pt;
343 int as = pt - 15; /* 15 = 5+5+5 */
344 GC gc = scr->clip_title_gc;
345 #ifdef GRADIENT_CLIP_ARROW
346 Bool collapsed = clipIcon->dock->collapsed;
347 #endif
349 if (rpushed) {
350 p[0].x = tp+1;
351 p[0].y = 1;
352 p[1].x = ICON_SIZE-2;
353 p[1].y = 1;
354 p[2].x = ICON_SIZE-2;
355 p[2].y = pt-1;
356 } else if (lpushed) {
357 p[0].x = 1;
358 p[0].y = tp;
359 p[1].x = pt;
360 p[1].y = ICON_SIZE-2;
361 p[2].x = 1;
362 p[2].y = ICON_SIZE-2;
364 if (lpushed || rpushed) {
365 XSetForeground(dpy, scr->draw_gc, scr->white_pixel);
366 XFillPolygon(dpy, win, scr->draw_gc, p, 3, Convex, CoordModeOrigin);
367 XSetForeground(dpy, scr->draw_gc, scr->black_pixel);
369 #ifdef GRADIENT_CLIP_ARROW
370 if (!collapsed) {
371 XSetFillStyle(dpy, scr->copy_gc, FillTiled);
372 XSetTile(dpy, scr->copy_gc, scr->clip_arrow_gradient);
373 XSetClipMask(dpy, scr->copy_gc, None);
374 gc = scr->copy_gc;
376 #endif /* GRADIENT_CLIP_ARROW */
378 /* top right arrow */
379 p[0].x = p[3].x = ICON_SIZE-5-as;
380 p[0].y = p[3].y = 5;
381 p[1].x = ICON_SIZE-6;
382 p[1].y = 5;
383 p[2].x = ICON_SIZE-6;
384 p[2].y = 4+as;
385 if (rpushed) {
386 XFillPolygon(dpy, win, scr->draw_gc, p, 3, Convex, CoordModeOrigin);
387 XDrawLines(dpy, win, scr->draw_gc, p, 4, CoordModeOrigin);
388 } else {
389 #ifdef GRADIENT_CLIP_ARROW
390 if (!collapsed)
391 XSetTSOrigin(dpy, gc, ICON_SIZE-6-as, 5);
392 #endif
393 XFillPolygon(dpy, win, gc, p,3,Convex,CoordModeOrigin);
394 XDrawLines(dpy, win, gc, p,4,CoordModeOrigin);
397 /* bottom left arrow */
398 p[0].x = p[3].x = 5;
399 p[0].y = p[3].y = ICON_SIZE-5-as;
400 p[1].x = 5;
401 p[1].y = ICON_SIZE-6;
402 p[2].x = 4+as;
403 p[2].y = ICON_SIZE-6;
404 if (lpushed) {
405 XFillPolygon(dpy, win, scr->draw_gc, p, 3, Convex, CoordModeOrigin);
406 XDrawLines(dpy, win, scr->draw_gc, p, 4, CoordModeOrigin);
407 } else {
408 #ifdef GRADIENT_CLIP_ARROW
409 if (!collapsed)
410 XSetTSOrigin(dpy, gc, 5, ICON_SIZE-6-as);
411 #endif
412 XFillPolygon(dpy, win, gc, p,3,Convex,CoordModeOrigin);
413 XDrawLines(dpy, win, gc, p,4,CoordModeOrigin);
415 #ifdef GRADIENT_CLIP_ARROW
416 if (!collapsed)
417 XSetFillStyle(dpy, scr->copy_gc, FillSolid);
418 #endif
422 RImage*
423 wClipMakeTile(WScreen *scr, RImage *normalTile)
425 RImage *tile = RCloneImage(normalTile);
426 RColor black;
427 RColor dark;
428 RColor light;
429 int pt, tp;
430 int as;
432 pt = CLIP_BUTTON_SIZE*wPreferences.icon_size/64;
433 tp = wPreferences.icon_size-1 - pt;
434 as = pt - 15;
436 black.alpha = 255;
437 black.red = black.green = black.blue = 0;
439 dark.alpha = 0;
440 dark.red = dark.green = dark.blue = 60;
442 light.alpha = 0;
443 light.red = light.green = light.blue = 80;
446 /* top right */
447 ROperateLine(tile, RSubtractOperation, tp, 0, wPreferences.icon_size-2,
448 pt-1, &dark);
449 RDrawLine(tile, tp-1, 0, wPreferences.icon_size-1, pt+1, &black);
450 ROperateLine(tile, RAddOperation, tp, 2, wPreferences.icon_size-3,
451 pt, &light);
453 /* arrow bevel */
454 ROperateLine(tile, RSubtractOperation, ICON_SIZE - 7 - as, 4,
455 ICON_SIZE - 5, 4, &dark);
456 ROperateLine(tile, RSubtractOperation, ICON_SIZE - 6 - as, 5,
457 ICON_SIZE - 5, 6 + as, &dark);
458 ROperateLine(tile, RAddOperation, ICON_SIZE - 5, 4, ICON_SIZE - 5, 6 + as,
459 &light);
461 /* bottom left */
462 ROperateLine(tile, RAddOperation, 2, tp+2, pt-2,
463 wPreferences.icon_size-3, &dark);
464 RDrawLine(tile, 0, tp-1, pt+1, wPreferences.icon_size-1, &black);
465 ROperateLine(tile, RSubtractOperation, 0, tp-2, pt+1,
466 wPreferences.icon_size-2, &light);
468 /* arrow bevel */
469 ROperateLine(tile, RSubtractOperation, 4, ICON_SIZE - 7 - as, 4,
470 ICON_SIZE - 5, &dark);
471 ROperateLine(tile, RSubtractOperation, 5, ICON_SIZE - 6 - as,
472 6 + as, ICON_SIZE - 5, &dark);
473 ROperateLine(tile, RAddOperation, 4, ICON_SIZE - 5, 6 + as, ICON_SIZE - 5,
474 &light);
476 return tile;
480 static void
481 omnipresentCallback(WMenu *menu, WMenuEntry *entry)
483 WAppIcon *clickedIcon = entry->clientdata;
484 WAppIcon *aicon;
485 WDock *dock;
486 WMBag *selectedIcons;
487 int failed;
488 int i;
490 assert(entry->clientdata!=NULL);
492 dock = clickedIcon->dock;
494 selectedIcons = getSelected(dock);
496 if (!WMGetBagItemCount(selectedIcons))
497 WMPutInBag(selectedIcons, clickedIcon);
499 failed = 0;
500 for (i = 0; i < WMGetBagItemCount(selectedIcons); i++) {
501 aicon = WMGetFromBag(selectedIcons, i);
503 if (wClipMakeIconOmnipresent(aicon, !aicon->omnipresent) == WO_FAILED)
504 failed++;
505 else if (aicon->icon->selected)
506 wIconSelect(aicon->icon);
508 WMFreeBag(selectedIcons);
510 if (failed > 1) {
511 wMessageDialog(dock->screen_ptr, _("Warning"),
512 _("Some icons cannot be made omnipresent. "
513 "Please make sure that no other icon is "
514 "docked in the same positions on the other "
515 "workspaces and the Clip is not full in "
516 "some workspace."),
517 _("OK"), NULL, NULL);
518 } else if (failed == 1) {
519 wMessageDialog(dock->screen_ptr, _("Warning"),
520 _("Icon cannot be made omnipresent. "
521 "Please make sure that no other icon is "
522 "docked in the same position on the other "
523 "workspaces and the Clip is not full in "
524 "some workspace."),
525 _("OK"), NULL, NULL);
530 static void
531 removeIconsCallback(WMenu *menu, WMenuEntry *entry)
533 WAppIcon *clickedIcon = (WAppIcon*)entry->clientdata;
534 WDock *dock;
535 WAppIcon *aicon;
536 WMBag *selectedIcons;
537 int keepit;
538 WMBagIterator it;
540 assert(clickedIcon!=NULL);
542 dock = clickedIcon->dock;
544 selectedIcons = getSelected(dock);
546 if (WMGetBagItemCount(selectedIcons)) {
547 if (wMessageDialog(dock->screen_ptr, _("Workspace Clip"),
548 _("All selected icons will be removed!"),
549 _("OK"), _("Cancel"), NULL)!=WAPRDefault) {
550 WMFreeBag(selectedIcons);
551 return;
553 } else {
554 if (clickedIcon->xindex==0 && clickedIcon->yindex==0) {
555 WMFreeBag(selectedIcons);
556 return;
558 WMPutInBag(selectedIcons, clickedIcon);
561 WM_ITERATE_BAG(selectedIcons, aicon, it) {
562 keepit = aicon->running && wApplicationOf(aicon->main_window);
563 wDockDetach(dock, aicon);
564 if (keepit) {
565 PlaceIcon(dock->screen_ptr, &aicon->x_pos, &aicon->y_pos);
566 XMoveWindow(dpy, aicon->icon->core->window,
567 aicon->x_pos, aicon->y_pos);
568 if (!dock->mapped || dock->collapsed)
569 XMapWindow(dpy, aicon->icon->core->window);
572 WMFreeBag(selectedIcons);
574 if (wPreferences.auto_arrange_icons)
575 wArrangeIcons(dock->screen_ptr, True);
579 static void
580 keepIconsCallback(WMenu *menu, WMenuEntry *entry)
582 WAppIcon *clickedIcon = (WAppIcon*)entry->clientdata;
583 WDock *dock;
584 WAppIcon *aicon;
585 WMBag *selectedIcons;
586 WMBagIterator it;
588 assert(clickedIcon!=NULL);
589 dock = clickedIcon->dock;
591 selectedIcons = getSelected(dock);
593 if (!WMGetBagItemCount(selectedIcons)
594 && clickedIcon!=dock->screen_ptr->clip_icon) {
595 char *command = NULL;
597 if (!clickedIcon->command && !clickedIcon->editing) {
598 clickedIcon->editing = 1;
599 if (wInputDialog(dock->screen_ptr, _("Keep Icon"),
600 _("Type the command used to launch the application"),
601 &command)) {
602 if (command && (command[0]==0 ||
603 (command[0]=='-' && command[1]==0))) {
604 wfree(command);
605 command = NULL;
607 clickedIcon->command = command;
608 clickedIcon->editing = 0;
609 } else {
610 clickedIcon->editing = 0;
611 if (command)
612 wfree(command);
613 WMFreeBag(selectedIcons);
614 return;
618 WMPutInBag(selectedIcons, clickedIcon);
621 WM_ITERATE_BAG(selectedIcons, aicon, it) {
622 if (aicon->icon->selected)
623 wIconSelect(aicon->icon);
624 if (aicon && aicon->attracted && aicon->command) {
625 aicon->attracted = 0;
626 if (aicon->icon->shadowed) {
627 aicon->icon->shadowed = 0;
628 aicon->icon->force_paint = 1;
629 wAppIconPaint(aicon);
633 WMFreeBag(selectedIcons);
639 static void
640 toggleAutoAttractCallback(WMenu *menu, WMenuEntry *entry)
642 WDock *dock = (WDock*)entry->clientdata;
644 assert(entry->clientdata!=NULL);
646 dock->attract_icons = !dock->attract_icons;
647 /*if (!dock->attract_icons)
648 dock->keep_attracted = 0;*/
650 entry->flags.indicator_on = dock->attract_icons;
652 wMenuPaint(menu);
656 static void
657 selectCallback(WMenu *menu, WMenuEntry *entry)
659 WAppIcon *icon = (WAppIcon*)entry->clientdata;
661 assert(icon!=NULL);
663 wIconSelect(icon->icon);
665 wMenuPaint(menu);
669 static void
670 colectIconsCallback(WMenu *menu, WMenuEntry *entry)
672 WAppIcon *clickedIcon = (WAppIcon*)entry->clientdata;
673 WDock *clip;
674 WAppIcon *aicon;
675 int x, y, x_pos, y_pos;
677 assert(entry->clientdata!=NULL);
678 clip = clickedIcon->dock;
680 aicon = clip->screen_ptr->app_icon_list;
682 while (aicon) {
683 if (!aicon->docked && wDockFindFreeSlot(clip, &x, &y)) {
684 x_pos = clip->x_pos + x*ICON_SIZE;
685 y_pos = clip->y_pos + y*ICON_SIZE;
686 if (aicon->x_pos != x_pos || aicon->y_pos != y_pos) {
687 #ifdef ANIMATIONS
688 if (wPreferences.no_animations) {
689 XMoveWindow(dpy, aicon->icon->core->window, x_pos, y_pos);
690 } else {
691 SlideWindow(aicon->icon->core->window,
692 aicon->x_pos, aicon->y_pos, x_pos, y_pos);
694 #else
695 XMoveWindow(dpy, aicon->icon->core->window, x_pos, y_pos);
696 #endif /* ANIMATIONS */
698 aicon->attracted = 1;
699 if (!aicon->icon->shadowed) {
700 aicon->icon->shadowed = 1;
701 aicon->icon->force_paint = 1;
702 /* We don't do an wAppIconPaint() here because it's in
703 * wDockAttachIcon(). -Dan
706 wDockAttachIcon(clip, aicon, x, y);
707 if (clip->collapsed || !clip->mapped)
708 XUnmapWindow(dpy, aicon->icon->core->window);
710 aicon = aicon->next;
715 static void
716 selectIconsCallback(WMenu *menu, WMenuEntry *entry)
718 WAppIcon *clickedIcon = (WAppIcon*)entry->clientdata;
719 WDock *dock;
720 WMBag *selectedIcons;
721 WAppIcon *btn;
722 int i;
724 assert(clickedIcon!=NULL);
725 dock = clickedIcon->dock;
727 selectedIcons = getSelected(dock);
729 if (!WMGetBagItemCount(selectedIcons)) {
730 for (i=1; i<dock->max_icons; i++) {
731 btn = dock->icon_array[i];
732 if (btn && !btn->icon->selected) {
733 wIconSelect(btn->icon);
736 } else {
737 for (i = 0; i < WMGetBagItemCount(selectedIcons); i++) {
738 btn = WMGetFromBag(selectedIcons, i);
739 wIconSelect(btn->icon);
742 WMFreeBag(selectedIcons);
744 wMenuPaint(menu);
748 static void
749 toggleCollapsedCallback(WMenu *menu, WMenuEntry *entry)
751 assert(entry->clientdata!=NULL);
753 toggleCollapsed(entry->clientdata);
755 entry->flags.indicator_on = ((WDock*)entry->clientdata)->collapsed;
757 wMenuPaint(menu);
761 static void
762 toggleAutoCollapseCallback(WMenu *menu, WMenuEntry *entry)
764 WDock *dock;
765 assert(entry->clientdata!=NULL);
767 dock = (WDock*) entry->clientdata;
769 dock->auto_collapse = !dock->auto_collapse;
771 entry->flags.indicator_on = ((WDock*)entry->clientdata)->auto_collapse;
773 wMenuPaint(menu);
777 static void
778 toggleAutoRaiseLowerCallback(WMenu *menu, WMenuEntry *entry)
780 WDock *dock;
781 assert(entry->clientdata!=NULL);
783 dock = (WDock*) entry->clientdata;
785 dock->auto_raise_lower = !dock->auto_raise_lower;
787 entry->flags.indicator_on = ((WDock*)entry->clientdata)->auto_raise_lower;
789 wMenuPaint(menu);
793 static void
794 launchCallback(WMenu *menu, WMenuEntry *entry)
796 WAppIcon *btn = (WAppIcon*)entry->clientdata;
798 launchDockedApplication(btn);
802 static void
803 settingsCallback(WMenu *menu, WMenuEntry *entry)
805 WAppIcon *btn = (WAppIcon*)entry->clientdata;
807 if (btn->editing)
808 return;
809 ShowDockAppSettingsPanel(btn);
813 static void
814 hideCallback(WMenu *menu, WMenuEntry *entry)
816 WApplication *wapp;
817 WAppIcon *btn = (WAppIcon*)entry->clientdata;
819 wapp = wApplicationOf(btn->icon->owner->main_window);
821 if (wapp->flags.hidden) {
822 wWorkspaceChange(btn->icon->core->screen_ptr,wapp->last_workspace);
823 wUnhideApplication(wapp, False, False);
824 } else {
825 wHideApplication(wapp);
830 static void
831 unhideHereCallback(WMenu *menu, WMenuEntry *entry)
833 WApplication *wapp;
834 WAppIcon *btn = (WAppIcon*)entry->clientdata;
836 wapp = wApplicationOf(btn->icon->owner->main_window);
838 wUnhideApplication(wapp, False, True);
842 WAppIcon*
843 mainIconCreate(WScreen *scr, int type)
845 WAppIcon *btn;
846 int x_pos;
848 if (type == WM_CLIP) {
849 if (scr->clip_icon)
850 return scr->clip_icon;
851 btn = wAppIconCreateForDock(scr, NULL, "Logo", "WMClip", TILE_CLIP);
852 btn->icon->core->descriptor.handle_expose = clipIconExpose;
853 btn->icon->core->descriptor.handle_enternotify = clipEnterNotify;
854 btn->icon->core->descriptor.handle_leavenotify = clipLeaveNotify;
855 /*x_pos = scr->scr_width - ICON_SIZE*2 - DOCK_EXTRA_SPACE;*/
856 x_pos = 0;
857 } else {
858 btn = wAppIconCreateForDock(scr, NULL, "Logo", "WMDock", TILE_NORMAL);
859 x_pos = scr->scr_width - ICON_SIZE - DOCK_EXTRA_SPACE;
862 btn->xindex = 0;
863 btn->yindex = 0;
865 btn->icon->core->descriptor.handle_mousedown = iconMouseDown;
866 btn->icon->core->descriptor.parent_type = WCLASS_DOCK_ICON;
867 btn->icon->core->descriptor.parent = btn;
868 /*ChangeStackingLevel(btn->icon->core, WMDockLevel);*/
869 XMapWindow(dpy, btn->icon->core->window);
870 btn->x_pos = x_pos;
871 btn->y_pos = 0;
872 btn->docked = 1;
873 if (type == WM_CLIP)
874 scr->clip_icon = btn;
876 return btn;
880 static void
881 switchWSCommand(WMenu *menu, WMenuEntry *entry)
883 WAppIcon *btn, *icon = (WAppIcon*) entry->clientdata;
884 WScreen *scr = icon->icon->core->screen_ptr;
885 WDock *src, *dest;
886 WMBag *selectedIcons;
887 int x, y;
889 if (entry->order == scr->current_workspace)
890 return;
891 src = icon->dock;
892 dest = scr->workspaces[entry->order]->clip;
894 selectedIcons = getSelected(src);
896 if (WMGetBagItemCount(selectedIcons)) {
897 int i;
898 for (i = 0; i < WMGetBagItemCount(selectedIcons); i++) {
899 btn = WMGetFromBag(selectedIcons, i);
900 if (wDockFindFreeSlot(dest, &x, &y)) {
901 moveIconBetweenDocks(src, dest, btn, x, y);
902 XUnmapWindow(dpy, btn->icon->core->window);
905 } else if (icon != scr->clip_icon) {
906 if (wDockFindFreeSlot(dest, &x, &y)) {
907 moveIconBetweenDocks(src, dest, icon, x, y);
908 XUnmapWindow(dpy, icon->icon->core->window);
911 WMFreeBag(selectedIcons);
916 static void
917 launchDockedApplication(WAppIcon *btn)
919 WScreen *scr = btn->icon->core->screen_ptr;
921 if (!btn->launching && btn->command!=NULL) {
922 if (!btn->forced_dock) {
923 btn->relaunching = btn->running;
924 btn->running = 1;
926 if (btn->wm_instance || btn->wm_class) {
927 WWindowAttributes attr;
928 memset(&attr, 0, sizeof(WWindowAttributes));
929 wDefaultFillAttributes(scr, btn->wm_instance, btn->wm_class,
930 &attr, NULL, True);
932 if (!attr.no_appicon && !btn->buggy_app)
933 btn->launching = 1;
934 else
935 btn->running = 0;
937 btn->drop_launch = 0;
938 scr->last_dock = btn->dock;
939 btn->pid = execCommand(btn, btn->command, NULL);
940 if (btn->pid>0) {
941 if (btn->buggy_app) {
942 /* give feedback that the app was launched */
943 btn->launching = 1;
944 dockIconPaint(btn);
945 btn->launching = 0;
946 WMAddTimerHandler(200, (WMCallback*)dockIconPaint, btn);
947 } else {
948 dockIconPaint(btn);
950 } else {
951 wwarning(_("could not launch application %s\n"), btn->command);
952 btn->launching = 0;
953 if (!btn->relaunching)
954 btn->running = 0;
961 static void
962 updateWorkspaceMenu(WMenu *menu, WAppIcon *icon)
964 WScreen *scr = menu->frame->screen_ptr;
965 char title[MAX_WORKSPACENAME_WIDTH+1];
966 int i;
968 if (!menu || !icon)
969 return;
971 for (i=0; i<scr->workspace_count; i++) {
972 if (i < menu->entry_no) {
973 if (strcmp(menu->entries[i]->text,scr->workspaces[i]->name)!=0) {
974 wfree(menu->entries[i]->text);
975 strcpy(title, scr->workspaces[i]->name);
976 menu->entries[i]->text = wstrdup(title);
977 menu->flags.realized = 0;
979 menu->entries[i]->clientdata = (void*)icon;
980 } else {
981 strcpy(title, scr->workspaces[i]->name);
983 wMenuAddCallback(menu, title, switchWSCommand, (void*)icon);
985 menu->flags.realized = 0;
987 if (i == scr->current_workspace) {
988 wMenuSetEnabled(menu, i, False);
989 } else {
990 wMenuSetEnabled(menu, i, True);
994 if (!menu->flags.realized)
995 wMenuRealize(menu);
999 static WMenu*
1000 makeWorkspaceMenu(WScreen *scr)
1002 WMenu *menu;
1004 menu = wMenuCreate(scr, NULL, False);
1005 if (!menu)
1006 wwarning(_("could not create workspace submenu for Clip menu"));
1008 wMenuAddCallback(menu, "", switchWSCommand, (void*)scr->clip_icon);
1010 menu->flags.realized = 0;
1011 wMenuRealize(menu);
1013 return menu;
1017 static void
1018 updateClipOptionsMenu(WMenu *menu, WDock *dock)
1020 WMenuEntry *entry;
1021 int index = 0;
1023 if (!menu || !dock)
1024 return;
1026 /* keep on top */
1027 entry = menu->entries[index];
1028 if (dock->lowered) {
1029 entry->text = _("Keep on Top");
1030 } else {
1031 entry->text = _("Allow Lowering");
1033 entry->clientdata = dock;
1035 /* collapsed */
1036 entry = menu->entries[++index];
1037 entry->flags.indicator_on = dock->collapsed;
1038 entry->clientdata = dock;
1040 /* auto-collapse */
1041 entry = menu->entries[++index];
1042 entry->flags.indicator_on = dock->auto_collapse;
1043 entry->clientdata = dock;
1045 /* auto-raise/lower */
1046 entry = menu->entries[++index];
1047 entry->flags.indicator_on = dock->auto_raise_lower;
1048 entry->clientdata = dock;
1050 /* attract icons */
1051 entry = menu->entries[++index];
1052 entry->flags.indicator_on = dock->attract_icons;
1053 entry->clientdata = dock;
1055 menu->flags.realized = 0;
1056 wMenuRealize(menu);
1060 static WMenu*
1061 makeClipOptionsMenu(WScreen *scr)
1063 WMenu *menu;
1064 WMenuEntry *entry;
1066 menu = wMenuCreate(scr, NULL, False);
1067 if (!menu) {
1068 wwarning(_("could not create options submenu for Clip menu"));
1069 return NULL;
1072 entry = wMenuAddCallback(menu, _("Keep on Top"),
1073 toggleLoweredCallback, NULL);
1075 entry = wMenuAddCallback(menu, _("Collapsed"),
1076 toggleCollapsedCallback, NULL);
1077 entry->flags.indicator = 1;
1078 entry->flags.indicator_on = 1;
1079 entry->flags.indicator_type = MI_CHECK;
1081 entry = wMenuAddCallback(menu, _("AutoCollapse"),
1082 toggleAutoCollapseCallback, NULL);
1083 entry->flags.indicator = 1;
1084 entry->flags.indicator_on = 1;
1085 entry->flags.indicator_type = MI_CHECK;
1087 entry = wMenuAddCallback(menu, _("AutoRaiseLower"),
1088 toggleAutoRaiseLowerCallback, NULL);
1089 entry->flags.indicator = 1;
1090 entry->flags.indicator_on = 1;
1091 entry->flags.indicator_type = MI_CHECK;
1093 entry = wMenuAddCallback(menu, _("AutoAttract Icons"),
1094 toggleAutoAttractCallback, NULL);
1095 entry->flags.indicator = 1;
1096 entry->flags.indicator_on = 1;
1097 entry->flags.indicator_type = MI_CHECK;
1099 menu->flags.realized = 0;
1100 wMenuRealize(menu);
1102 return menu;
1106 static WMenu*
1107 dockMenuCreate(WScreen *scr, int type)
1109 WMenu *menu;
1110 WMenuEntry *entry;
1112 if (type == WM_CLIP && scr->clip_menu)
1113 return scr->clip_menu;
1115 menu = wMenuCreate(scr, NULL, False);
1116 if (type != WM_CLIP) {
1117 entry = wMenuAddCallback(menu, _("Keep on Top"),
1118 toggleLoweredCallback, NULL);
1119 } else {
1120 entry = wMenuAddCallback(menu, _("Clip Options"), NULL, NULL);
1121 scr->clip_options = makeClipOptionsMenu(scr);
1122 if (scr->clip_options)
1123 wMenuEntrySetCascade(menu, entry, scr->clip_options);
1125 entry = wMenuAddCallback(menu, _("Rename Workspace"), renameCallback,
1126 NULL);
1127 wfree(entry->text);
1128 entry->text = _("Rename Workspace");
1130 entry = wMenuAddCallback(menu, _("Selected"), selectCallback, NULL);
1131 entry->flags.indicator = 1;
1132 entry->flags.indicator_on = 1;
1133 entry->flags.indicator_type = MI_CHECK;
1135 entry = wMenuAddCallback(menu, _("Select All Icons"),
1136 selectIconsCallback, NULL);
1137 wfree(entry->text);
1138 entry->text = _("Select All Icons");
1140 entry = wMenuAddCallback(menu, _("Keep Icon"), keepIconsCallback, NULL);
1141 wfree(entry->text);
1142 entry->text = _("Keep Icon");
1144 entry = wMenuAddCallback(menu, _("Move Icon To"), NULL, NULL);
1145 wfree(entry->text);
1146 entry->text = _("Move Icon To");
1147 scr->clip_submenu = makeWorkspaceMenu(scr);
1148 if (scr->clip_submenu)
1149 wMenuEntrySetCascade(menu, entry, scr->clip_submenu);
1151 entry = wMenuAddCallback(menu, _("Remove Icon"), removeIconsCallback,
1152 NULL);
1153 wfree(entry->text);
1154 entry->text = _("Remove Icon");
1156 wMenuAddCallback(menu, _("Attract Icons"), colectIconsCallback, NULL);
1159 wMenuAddCallback(menu, _("Launch"), launchCallback, NULL);
1161 wMenuAddCallback(menu, _("Unhide Here"), unhideHereCallback, NULL);
1163 entry = wMenuAddCallback(menu, _("Hide"), hideCallback, NULL);
1164 wfree(entry->text);
1165 entry->text = _("Hide");
1167 wMenuAddCallback(menu, _("Settings..."), settingsCallback, NULL);
1169 wMenuAddCallback(menu, _("Kill"), killCallback, NULL);
1171 if (type == WM_CLIP)
1172 scr->clip_menu = menu;
1174 return menu;
1178 WDock*
1179 wDockCreate(WScreen *scr, int type)
1181 WDock *dock;
1182 WAppIcon *btn;
1183 int icon_count;
1185 make_keys();
1187 dock = wmalloc(sizeof(WDock));
1188 memset(dock, 0, sizeof(WDock));
1190 if (type == WM_CLIP)
1191 icon_count = CLIP_MAX_ICONS;
1192 else
1193 icon_count = scr->scr_height/wPreferences.icon_size;
1195 dock->icon_array = wmalloc(sizeof(WAppIcon*)*icon_count);
1196 memset(dock->icon_array, 0, sizeof(WAppIcon*)*icon_count);
1198 dock->max_icons = icon_count;
1200 btn = mainIconCreate(scr, type);
1202 btn->dock = dock;
1204 dock->x_pos = btn->x_pos;
1205 dock->y_pos = btn->y_pos;
1206 dock->screen_ptr = scr;
1207 dock->type = type;
1208 dock->icon_count = 1;
1209 dock->on_right_side = 1;
1210 dock->collapsed = 0;
1211 dock->auto_collapse = 0;
1212 dock->auto_collapse_magic = NULL;
1213 dock->auto_raise_lower = 0;
1214 dock->auto_lower_magic = NULL;
1215 dock->auto_raise_magic = NULL;
1216 dock->attract_icons = 0;
1217 dock->lowered = 1;
1218 dock->icon_array[0] = btn;
1219 wRaiseFrame(btn->icon->core);
1220 XMoveWindow(dpy, btn->icon->core->window, btn->x_pos, btn->y_pos);
1222 /* create dock menu */
1223 dock->menu = dockMenuCreate(scr, type);
1225 return dock;
1229 void
1230 wDockDestroy(WDock *dock)
1232 int i;
1233 WAppIcon *aicon;
1235 for (i=(dock->type == WM_CLIP) ? 1 : 0; i<dock->max_icons; i++) {
1236 aicon = dock->icon_array[i];
1237 if (aicon) {
1238 int keepit = aicon->running && wApplicationOf(aicon->main_window);
1239 wDockDetach(dock, aicon);
1240 if (keepit) {
1241 PlaceIcon(dock->screen_ptr, &aicon->x_pos, &aicon->y_pos);
1242 XMoveWindow(dpy, aicon->icon->core->window,
1243 aicon->x_pos, aicon->y_pos);
1244 if (!dock->mapped || dock->collapsed)
1245 XMapWindow(dpy, aicon->icon->core->window);
1249 if (wPreferences.auto_arrange_icons)
1250 wArrangeIcons(dock->screen_ptr, True);
1251 wfree(dock->icon_array);
1252 if (dock->menu && dock->type!=WM_CLIP)
1253 wMenuDestroy(dock->menu, True);
1254 if (dock->screen_ptr->last_dock == dock)
1255 dock->screen_ptr->last_dock = NULL;
1256 wfree(dock);
1260 void
1261 wClipIconPaint(WAppIcon *aicon)
1263 WScreen *scr = aicon->icon->core->screen_ptr;
1264 WWorkspace *workspace = scr->workspaces[scr->current_workspace];
1265 GC gc;
1266 Window win = aicon->icon->core->window;
1267 int length, nlength;
1268 char *ws_name, ws_number[10];
1269 int ty, tx;
1271 wIconPaint(aicon->icon);
1273 length = strlen(workspace->name);
1274 ws_name = malloc(length + 1);
1275 sprintf(ws_name, "%s", workspace->name);
1276 sprintf(ws_number, "%i", scr->current_workspace + 1);
1277 nlength = strlen(ws_number);
1279 gc = scr->clip_title_gc;
1281 if (!workspace->clip->collapsed)
1282 XSetForeground(dpy, gc, scr->clip_title_pixel[CLIP_NORMAL]);
1283 else
1284 XSetForeground(dpy, gc, scr->clip_title_pixel[CLIP_COLLAPSED]);
1286 ty = ICON_SIZE - WMFontHeight(scr->clip_title_font) - 3;
1288 tx = CLIP_BUTTON_SIZE*ICON_SIZE/64;
1290 WMDrawString(scr->wmscreen, win, gc, scr->clip_title_font, tx,
1291 ty, ws_name, length);
1293 tx = (ICON_SIZE/2 - WMWidthOfString(scr->clip_title_font, ws_number,
1294 nlength))/2;
1296 WMDrawString(scr->wmscreen, win, gc, scr->clip_title_font, tx,
1297 2, ws_number, nlength);
1299 wfree(ws_name);
1301 if (aicon->launching) {
1302 XFillRectangle(dpy, aicon->icon->core->window, scr->stipple_gc,
1303 0, 0, wPreferences.icon_size, wPreferences.icon_size);
1305 paintClipButtons(aicon, aicon->dock->lclip_button_pushed,
1306 aicon->dock->rclip_button_pushed);
1310 static void
1311 clipIconExpose(WObjDescriptor *desc, XEvent *event)
1313 wClipIconPaint(desc->parent);
1317 static void
1318 dockIconPaint(WAppIcon *btn)
1320 if (btn == btn->icon->core->screen_ptr->clip_icon)
1321 wClipIconPaint(btn);
1322 else
1323 wAppIconPaint(btn);
1327 static proplist_t
1328 make_icon_state(WAppIcon *btn)
1330 proplist_t node = NULL;
1331 proplist_t command, autolaunch, lock, name, forced, host, position, buggy;
1332 proplist_t omnipresent;
1333 char *tmp;
1334 char buffer[64];
1336 if (btn) {
1337 if (!btn->command)
1338 command = PLMakeString("-");
1339 else
1340 command = PLMakeString(btn->command);
1342 autolaunch = btn->auto_launch ? dYes : dNo;
1344 lock = btn->lock ? dYes : dNo;
1346 tmp = EscapeWM_CLASS(btn->wm_instance, btn->wm_class);
1348 name = PLMakeString(tmp);
1350 wfree(tmp);
1352 forced = btn->forced_dock ? dYes : dNo;
1354 buggy = btn->buggy_app ? dYes : dNo;
1356 if (btn == btn->icon->core->screen_ptr->clip_icon)
1357 sprintf(buffer, "%i,%i", btn->x_pos, btn->y_pos);
1358 else
1359 sprintf(buffer, "%hi,%hi", btn->xindex, btn->yindex);
1360 position = PLMakeString(buffer);
1362 node = PLMakeDictionaryFromEntries(dCommand, command,
1363 dName, name,
1364 dAutoLaunch, autolaunch,
1365 dLock, lock,
1366 dForced, forced,
1367 dBuggyApplication, buggy,
1368 dPosition, position,
1369 NULL);
1370 PLRelease(command);
1371 PLRelease(name);
1372 PLRelease(position);
1374 omnipresent = btn->omnipresent ? dYes : dNo;
1375 if (btn->dock != btn->icon->core->screen_ptr->dock &&
1376 (btn->xindex != 0 || btn->yindex != 0))
1377 PLInsertDictionaryEntry(node, dOmnipresent, omnipresent);
1379 #ifdef OFFIX_DND
1380 if (btn->dnd_command) {
1381 command = PLMakeString(btn->dnd_command);
1382 PLInsertDictionaryEntry(node, dDropCommand, command);
1383 PLRelease(command);
1385 #endif /* OFFIX_DND */
1387 if (btn->client_machine && btn->remote_start) {
1388 host = PLMakeString(btn->client_machine);
1389 PLInsertDictionaryEntry(node, dHost, host);
1390 PLRelease(host);
1394 return node;
1398 static proplist_t
1399 dockSaveState(WDock *dock)
1401 int i;
1402 proplist_t icon_info;
1403 proplist_t list=NULL, dock_state=NULL;
1404 proplist_t value, key;
1405 char buffer[256];
1407 list = PLMakeArrayFromElements(NULL);
1409 for (i=(dock->type==WM_DOCK ? 0 : 1); i<dock->max_icons; i++) {
1410 WAppIcon *btn = dock->icon_array[i];
1412 if (!btn || btn->attracted)
1413 continue;
1415 if ((icon_info = make_icon_state(dock->icon_array[i]))) {
1416 list = PLAppendArrayElement(list, icon_info);
1417 PLRelease(icon_info);
1421 dock_state = PLMakeDictionaryFromEntries(dApplications, list,
1422 NULL);
1424 if (dock->type == WM_DOCK) {
1425 sprintf(buffer, "Applications%i", dock->screen_ptr->scr_height);
1426 key = PLMakeString(buffer);
1427 PLInsertDictionaryEntry(dock_state, key, list);
1428 PLRelease(key);
1431 sprintf(buffer, "%i,%i", (dock->on_right_side ? -ICON_SIZE : 0),
1432 dock->y_pos);
1433 value = PLMakeString(buffer);
1434 PLInsertDictionaryEntry(dock_state, dPosition, value);
1435 PLRelease(value);
1437 PLRelease(list);
1440 value = (dock->lowered ? dYes : dNo);
1441 PLInsertDictionaryEntry(dock_state, dLowered, value);
1443 if (dock->type == WM_CLIP) {
1444 value = (dock->collapsed ? dYes : dNo);
1445 PLInsertDictionaryEntry(dock_state, dCollapsed, value);
1447 value = (dock->auto_collapse ? dYes : dNo);
1448 PLInsertDictionaryEntry(dock_state, dAutoCollapse, value);
1450 value = (dock->auto_raise_lower ? dYes : dNo);
1451 PLInsertDictionaryEntry(dock_state, dAutoRaiseLower, value);
1453 value = (dock->attract_icons ? dYes : dNo);
1454 PLInsertDictionaryEntry(dock_state, dAutoAttractIcons, value);
1457 return dock_state;
1461 void
1462 wDockSaveState(WScreen *scr, proplist_t old_state)
1464 proplist_t dock_state;
1465 proplist_t keys;
1467 dock_state = dockSaveState(scr->dock);
1470 * Copy saved states of docks with different sizes.
1472 if (old_state) {
1473 int i;
1474 proplist_t tmp;
1476 keys = PLGetAllDictionaryKeys(old_state);
1477 for (i = 0; i < PLGetNumberOfElements(keys); i++) {
1478 tmp = PLGetArrayElement(keys, i);
1480 if (strncasecmp(PLGetString(tmp), "applications", 12) == 0
1481 && !PLGetDictionaryEntry(dock_state, tmp)) {
1483 PLInsertDictionaryEntry(dock_state,
1484 tmp,
1485 PLGetDictionaryEntry(old_state, tmp));
1488 PLRelease(keys);
1492 PLInsertDictionaryEntry(scr->session_state, dDock, dock_state);
1494 PLRelease(dock_state);
1498 void
1499 wClipSaveState(WScreen *scr)
1501 proplist_t clip_state;
1503 clip_state = make_icon_state(scr->clip_icon);
1505 PLInsertDictionaryEntry(scr->session_state, dClip, clip_state);
1507 PLRelease(clip_state);
1511 proplist_t
1512 wClipSaveWorkspaceState(WScreen *scr, int workspace)
1514 return dockSaveState(scr->workspaces[workspace]->clip);
1518 static Bool
1519 getBooleanDockValue(proplist_t value, proplist_t key)
1521 if (value) {
1522 if (PLIsString(value)) {
1523 if (strcasecmp(PLGetString(value), "YES")==0)
1524 return True;
1525 } else {
1526 wwarning(_("bad value in docked icon state info %s"),
1527 PLGetString(key));
1530 return False;
1534 static WAppIcon*
1535 restore_icon_state(WScreen *scr, proplist_t info, int type, int index)
1537 WAppIcon *aicon;
1538 proplist_t cmd, value;
1541 cmd = PLGetDictionaryEntry(info, dCommand);
1542 if (!cmd || !PLIsString(cmd)) {
1543 return NULL;
1546 /* parse window name */
1547 value = PLGetDictionaryEntry(info, dName);
1548 if (!value)
1549 return NULL;
1552 char *wclass, *winstance;
1553 char *command;
1555 ParseWindowName(value, &winstance, &wclass, "dock");
1557 if (!winstance && !wclass) {
1558 return NULL;
1561 /* get commands */
1563 if (cmd)
1564 command = wstrdup(PLGetString(cmd));
1565 else
1566 command = NULL;
1568 if (!command || strcmp(command, "-")==0) {
1569 if (command)
1570 wfree(command);
1571 if (wclass)
1572 wfree(wclass);
1573 if (winstance)
1574 wfree(winstance);
1576 return NULL;
1579 aicon = wAppIconCreateForDock(scr, command, winstance, wclass,
1580 TILE_NORMAL);
1581 if (wclass)
1582 wfree(wclass);
1583 if (winstance)
1584 wfree(winstance);
1585 if (command)
1586 wfree(command);
1589 aicon->icon->core->descriptor.handle_mousedown = iconMouseDown;
1590 if (type == WM_CLIP) {
1591 aicon->icon->core->descriptor.handle_enternotify = clipEnterNotify;
1592 aicon->icon->core->descriptor.handle_leavenotify = clipLeaveNotify;
1594 aicon->icon->core->descriptor.parent_type = WCLASS_DOCK_ICON;
1595 aicon->icon->core->descriptor.parent = aicon;
1598 #ifdef OFFIX_DND
1599 cmd = PLGetDictionaryEntry(info, dDropCommand);
1600 if (cmd)
1601 aicon->dnd_command = wstrdup(PLGetString(cmd));
1602 #endif
1604 /* check auto launch */
1605 value = PLGetDictionaryEntry(info, dAutoLaunch);
1607 aicon->auto_launch = getBooleanDockValue(value, dAutoLaunch);
1609 /* check lock */
1610 value = PLGetDictionaryEntry(info, dLock);
1612 aicon->lock = getBooleanDockValue(value, dLock);
1614 /* check if it wasn't normally docked */
1615 value = PLGetDictionaryEntry(info, dForced);
1617 aicon->forced_dock = getBooleanDockValue(value, dForced);
1619 /* check if we can rely on the stuff in the app */
1620 value = PLGetDictionaryEntry(info, dBuggyApplication);
1622 aicon->buggy_app = getBooleanDockValue(value, dBuggyApplication);
1624 /* get position in the dock */
1625 value = PLGetDictionaryEntry(info, dPosition);
1626 if (value && PLIsString(value)) {
1627 if (sscanf(PLGetString(value), "%hi,%hi", &aicon->xindex,
1628 &aicon->yindex)!=2)
1629 wwarning(_("bad value in docked icon state info %s"),
1630 PLGetString(dPosition));
1632 /* check position sanity */
1633 /* incomplete section! */
1634 if (type == WM_DOCK) {
1635 aicon->xindex = 0;
1636 if (aicon->yindex < 0)
1637 wwarning(_("bad value in docked icon position %i,%i"),
1638 aicon->xindex, aicon->yindex);
1640 } else {
1641 aicon->yindex = index;
1642 aicon->xindex = 0;
1645 /* check if icon is omnipresent */
1646 value = PLGetDictionaryEntry(info, dOmnipresent);
1648 aicon->omnipresent = getBooleanDockValue(value, dOmnipresent);
1650 aicon->running = 0;
1651 aicon->docked = 1;
1653 return aicon;
1657 #define COMPLAIN(key) wwarning(_("bad value in dock state info:%s"), key)
1660 WAppIcon*
1661 wClipRestoreState(WScreen *scr, proplist_t clip_state)
1663 WAppIcon *icon;
1664 proplist_t value;
1667 icon = mainIconCreate(scr, WM_CLIP);
1669 if (!clip_state)
1670 return icon;
1671 else
1672 PLRetain(clip_state);
1674 /* restore position */
1676 value = PLGetDictionaryEntry(clip_state, dPosition);
1678 if (value) {
1679 if (!PLIsString(value))
1680 COMPLAIN("Position");
1681 else {
1682 if (sscanf(PLGetString(value), "%i,%i", &icon->x_pos,
1683 &icon->y_pos)!=2)
1684 COMPLAIN("Position");
1686 /* check position sanity */
1687 if (icon->y_pos < 0)
1688 icon->y_pos = 0;
1689 else if (icon->y_pos > scr->scr_height-ICON_SIZE)
1690 icon->y_pos = scr->scr_height-ICON_SIZE;
1692 if (icon->x_pos < 0)
1693 icon->x_pos = 0;
1694 else if (icon->x_pos > scr->scr_width-ICON_SIZE)
1695 icon->x_pos = scr->scr_width-ICON_SIZE;
1699 #ifdef OFFIX_DND
1700 value = PLGetDictionaryEntry(clip_state, dDropCommand);
1701 if (value && PLIsString(value))
1702 icon->dnd_command = wstrdup(PLGetString(value));
1703 #endif
1705 PLRelease(clip_state);
1707 return icon;
1711 WDock*
1712 wDockRestoreState(WScreen *scr, proplist_t dock_state, int type)
1714 WDock *dock;
1715 proplist_t apps;
1716 proplist_t value;
1717 WAppIcon *aicon, *old_top;
1718 int count, i;
1721 dock = wDockCreate(scr, type);
1723 if (!dock_state)
1724 return dock;
1726 if (dock_state)
1727 PLRetain(dock_state);
1730 /* restore position */
1732 value = PLGetDictionaryEntry(dock_state, dPosition);
1734 if (value) {
1735 if (!PLIsString(value))
1736 COMPLAIN("Position");
1737 else {
1738 if (sscanf(PLGetString(value), "%i,%i", &dock->x_pos,
1739 &dock->y_pos)!=2)
1740 COMPLAIN("Position");
1742 /* check position sanity */
1743 if (dock->y_pos < 0)
1744 dock->y_pos = 0;
1745 else if (dock->y_pos > scr->scr_height-ICON_SIZE)
1746 dock->y_pos = scr->scr_height - ICON_SIZE;
1748 /* This is no more needed. ??? */
1749 if (type == WM_CLIP) {
1750 if (dock->x_pos < 0)
1751 dock->x_pos = 0;
1752 else if (dock->x_pos > scr->scr_width-ICON_SIZE)
1753 dock->x_pos = scr->scr_width-ICON_SIZE;
1754 } else {
1755 if (dock->x_pos >= 0) {
1756 dock->x_pos = DOCK_EXTRA_SPACE;
1757 dock->on_right_side = 0;
1758 } else {
1759 dock->x_pos = scr->scr_width - DOCK_EXTRA_SPACE - ICON_SIZE;
1760 dock->on_right_side = 1;
1766 /* restore lowered/raised state */
1768 dock->lowered = 0;
1770 value = PLGetDictionaryEntry(dock_state, dLowered);
1772 if (value) {
1773 if (!PLIsString(value))
1774 COMPLAIN("Lowered");
1775 else {
1776 if (strcasecmp(PLGetString(value), "YES")==0)
1777 dock->lowered = 1;
1782 /* restore collapsed state */
1784 dock->collapsed = 0;
1786 value = PLGetDictionaryEntry(dock_state, dCollapsed);
1788 if (value) {
1789 if (!PLIsString(value))
1790 COMPLAIN("Collapsed");
1791 else {
1792 if (strcasecmp(PLGetString(value), "YES")==0)
1793 dock->collapsed = 1;
1798 /* restore auto-collapsed state */
1800 value = PLGetDictionaryEntry(dock_state, dAutoCollapse);
1802 if (value) {
1803 if (!PLIsString(value))
1804 COMPLAIN("AutoCollapse");
1805 else {
1806 if (strcasecmp(PLGetString(value), "YES")==0) {
1807 dock->auto_collapse = 1;
1808 dock->collapsed = 1;
1814 /* restore auto-raise/lower state */
1816 value = PLGetDictionaryEntry(dock_state, dAutoRaiseLower);
1818 if (value) {
1819 if (!PLIsString(value))
1820 COMPLAIN("AutoRaiseLower");
1821 else {
1822 if (strcasecmp(PLGetString(value), "YES")==0) {
1823 dock->auto_raise_lower = 1;
1828 /* restore attract icons state */
1830 dock->attract_icons = 0;
1832 value = PLGetDictionaryEntry(dock_state, dAutoAttractIcons);
1834 if (value) {
1835 if (!PLIsString(value))
1836 COMPLAIN("AutoAttractIcons");
1837 else {
1838 if (strcasecmp(PLGetString(value), "YES")==0)
1839 dock->attract_icons = 1;
1844 /* application list */
1847 proplist_t tmp;
1848 char buffer[64];
1851 * When saving, it saves the dock state in
1852 * Applications and Applicationsnnn
1854 * When loading, it will first try Applicationsnnn.
1855 * If it does not exist, use Applications as default.
1858 sprintf(buffer, "Applications%i", scr->scr_height);
1860 tmp = PLMakeString(buffer);
1861 apps = PLGetDictionaryEntry(dock_state, tmp);
1862 PLRelease(tmp);
1864 if (!apps) {
1865 apps = PLGetDictionaryEntry(dock_state, dApplications);
1869 if (!apps) {
1870 goto finish;
1873 count = PLGetNumberOfElements(apps);
1875 if (count==0)
1876 goto finish;
1878 old_top = dock->icon_array[0];
1880 /* dock->icon_count is set to 1 when dock is created.
1881 * Since Clip is already restored, we want to keep it so for clip,
1882 * but for dock we may change the default top tile, so we set it to 0.
1884 if (type == WM_DOCK)
1885 dock->icon_count = 0;
1887 for (i=0; i<count; i++) {
1888 if (dock->icon_count >= dock->max_icons) {
1889 wwarning(_("there are too many icons stored in dock. Ignoring what doesn't fit"));
1890 break;
1893 value = PLGetArrayElement(apps, i);
1894 aicon = restore_icon_state(scr, value, type, dock->icon_count);
1896 dock->icon_array[dock->icon_count] = aicon;
1898 if (aicon) {
1899 aicon->dock = dock;
1900 aicon->x_pos = dock->x_pos + (aicon->xindex*ICON_SIZE);
1901 aicon->y_pos = dock->y_pos + (aicon->yindex*ICON_SIZE);
1903 if (dock->lowered)
1904 ChangeStackingLevel(aicon->icon->core, WMNormalLevel);
1905 else
1906 ChangeStackingLevel(aicon->icon->core, WMDockLevel);
1908 wCoreConfigure(aicon->icon->core, aicon->x_pos, aicon->y_pos,
1909 0, 0);
1911 if (!dock->collapsed)
1912 XMapWindow(dpy, aicon->icon->core->window);
1913 wRaiseFrame(aicon->icon->core);
1915 dock->icon_count++;
1916 } else if (dock->icon_count==0 && type==WM_DOCK)
1917 dock->icon_count++;
1920 /* if the first icon is not defined, use the default */
1921 if (dock->icon_array[0]==NULL) {
1922 /* update default icon */
1923 old_top->x_pos = dock->x_pos;
1924 old_top->y_pos = dock->y_pos;
1925 if (dock->lowered)
1926 ChangeStackingLevel(old_top->icon->core, WMNormalLevel);
1927 else
1928 ChangeStackingLevel(old_top->icon->core, WMDockLevel);
1929 dock->icon_array[0] = old_top;
1930 XMoveWindow(dpy, old_top->icon->core->window, dock->x_pos, dock->y_pos);
1931 /* we don't need to increment dock->icon_count here because it was
1932 * incremented in the loop above.
1934 } else if (old_top!=dock->icon_array[0]) {
1935 if (old_top == scr->clip_icon)
1936 scr->clip_icon = dock->icon_array[0];
1937 wAppIconDestroy(old_top);
1940 finish:
1941 if (dock_state)
1942 PLRelease(dock_state);
1944 return dock;
1949 void
1950 wDockLaunchWithState(WDock *dock, WAppIcon *btn, WSavedState *state)
1952 if (btn && btn->command && !btn->running && !btn->launching) {
1954 btn->drop_launch = 0;
1956 btn->pid = execCommand(btn, btn->command, state);
1958 if (btn->pid>0) {
1959 if (!btn->forced_dock && !btn->buggy_app) {
1960 btn->launching = 1;
1961 dockIconPaint(btn);
1964 } else {
1965 wfree(state);
1970 void
1971 wDockDoAutoLaunch(WDock *dock, int workspace)
1973 WAppIcon *btn;
1974 WSavedState *state;
1975 int i;
1977 for (i = 0; i < dock->max_icons; i++) {
1978 btn = dock->icon_array[i];
1979 if (!btn || !btn->auto_launch)
1980 continue;
1982 state = wmalloc(sizeof(WSavedState));
1983 memset(state, 0, sizeof(WSavedState));
1984 state->workspace = workspace;
1985 /* TODO: this is klugy and is very difficult to understand
1986 * what's going on. Try to clean up */
1987 wDockLaunchWithState(dock, btn, state);
1991 #ifdef REDUCE_APPICONS
1992 void
1993 wDockSimulateLaunch(WDock *dock, WAppIcon *btn)
1995 if ((btn == NULL) || (dock == NULL))
1996 return;
1998 if (!btn->running) {
1999 if ((btn->icon->owner == NULL) && (btn->applist))
2000 btn->icon->owner = btn->applist->wapp->main_window_desc;
2001 if (!btn->forced_dock)
2002 btn->launching = 1;
2003 dockIconPaint(btn);
2004 wusleep(5000);
2007 #endif
2009 #ifdef OFFIX_DND
2010 static WDock*
2011 findDock(WScreen *scr, XEvent *event, int *icon_pos)
2013 WDock *dock;
2014 int i;
2016 *icon_pos = -1;
2017 if ((dock = scr->dock)!=NULL) {
2018 for (i=0; i<dock->max_icons; i++) {
2019 if (dock->icon_array[i]
2020 && dock->icon_array[i]->icon->core->window==event->xclient.window) {
2021 *icon_pos = i;
2022 break;
2026 if (*icon_pos<0 && (dock = scr->workspaces[scr->current_workspace]->clip)!=NULL) {
2027 for (i=0; i<dock->max_icons; i++) {
2028 if (dock->icon_array[i]
2029 && dock->icon_array[i]->icon->core->window==event->xclient.window) {
2030 *icon_pos = i;
2031 break;
2035 if(*icon_pos>=0)
2036 return dock;
2037 return NULL;
2042 wDockReceiveDNDDrop(WScreen *scr, XEvent *event)
2044 WDock *dock;
2045 WAppIcon *btn;
2046 int icon_pos;
2048 dock = findDock(scr, event, &icon_pos);
2049 if (!dock)
2050 return False;
2053 * Return True if the drop was on an application icon window.
2054 * In this case, let the ClientMessage handler redirect the
2055 * message to the app.
2057 if (dock->icon_array[icon_pos]->icon->icon_win!=None)
2058 return True;
2060 if (dock->icon_array[icon_pos]->dnd_command!=NULL) {
2061 scr->flags.dnd_data_convertion_status = 0;
2063 btn = dock->icon_array[icon_pos];
2065 if (!btn->forced_dock) {
2066 btn->relaunching = btn->running;
2067 btn->running = 1;
2069 if (btn->wm_instance || btn->wm_class) {
2070 WWindowAttributes attr;
2071 memset(&attr, 0, sizeof(WWindowAttributes));
2072 wDefaultFillAttributes(btn->icon->core->screen_ptr,
2073 btn->wm_instance,
2074 btn->wm_class, &attr, NULL, True);
2076 if (!attr.no_appicon)
2077 btn->launching = 1;
2078 else
2079 btn->running = 0;
2082 btn->drop_launch = 1;
2083 scr->last_dock = dock;
2084 btn->pid = execCommand(btn, btn->dnd_command, NULL);
2085 if (btn->pid>0) {
2086 dockIconPaint(btn);
2087 } else {
2088 btn->launching = 0;
2089 if (!btn->relaunching) {
2090 btn->running = 0;
2094 return False;
2096 #endif /* OFFIX_DND */
2100 Bool
2101 wDockAttachIcon(WDock *dock, WAppIcon *icon, int x, int y)
2103 WWindow *wwin;
2104 char **argv;
2105 int argc;
2106 int index;
2108 wwin = icon->icon->owner;
2109 if (icon->command==NULL) {
2110 icon->editing = 0;
2111 if (XGetCommand(dpy, wwin->client_win, &argv, &argc) && argc>0) {
2113 icon->command = wtokenjoin(argv, argc);
2114 XFreeStringList(argv);
2115 } else {
2116 char *command=NULL;
2118 /* icon->forced_dock = 1;*/
2119 if (dock->type!=WM_CLIP || !icon->attracted) {
2120 icon->editing = 1;
2121 if (wInputDialog(dock->screen_ptr, _("Dock Icon"),
2122 _("Type the command used to launch the application"),
2123 &command)) {
2124 if (command && (command[0]==0 ||
2125 (command[0]=='-' && command[1]==0))) {
2126 wfree(command);
2127 command = NULL;
2129 icon->command = command;
2130 icon->editing = 0;
2131 } else {
2132 icon->editing = 0;
2133 if (command)
2134 wfree(command);
2135 /* If the target is the dock, reject the icon. If
2136 * the target is the clip, make it an attracted icon
2138 if (dock->type==WM_CLIP) {
2139 icon->attracted = 1;
2140 if (!icon->icon->shadowed) {
2141 icon->icon->shadowed = 1;
2142 icon->icon->force_paint = 1;
2144 } else {
2145 return False;
2150 } else {
2151 icon->editing = 0;
2154 for (index=1; index<dock->max_icons; index++)
2155 if (dock->icon_array[index] == NULL)
2156 break;
2157 /* if (index == dock->max_icons)
2158 return; */
2160 assert(index < dock->max_icons);
2162 dock->icon_array[index] = icon;
2163 icon->yindex = y;
2164 icon->xindex = x;
2166 icon->omnipresent = 0;
2168 icon->x_pos = dock->x_pos + x*ICON_SIZE;
2169 icon->y_pos = dock->y_pos + y*ICON_SIZE;
2171 dock->icon_count++;
2173 icon->running = 1;
2174 icon->launching = 0;
2175 icon->docked = 1;
2176 icon->dock = dock;
2177 icon->icon->core->descriptor.handle_mousedown = iconMouseDown;
2178 if (dock->type == WM_CLIP) {
2179 icon->icon->core->descriptor.handle_enternotify = clipEnterNotify;
2180 icon->icon->core->descriptor.handle_leavenotify = clipLeaveNotify;
2182 icon->icon->core->descriptor.parent_type = WCLASS_DOCK_ICON;
2183 icon->icon->core->descriptor.parent = icon;
2185 MoveInStackListUnder(dock->icon_array[index-1]->icon->core,
2186 icon->icon->core);
2187 wAppIconMove(icon, icon->x_pos, icon->y_pos);
2188 wAppIconPaint(icon);
2190 if (wPreferences.auto_arrange_icons)
2191 wArrangeIcons(dock->screen_ptr, True);
2193 #ifdef OFFIX_DND
2194 if (icon->command && !icon->dnd_command) {
2195 icon->dnd_command = wmalloc(strlen(icon->command)+8);
2196 sprintf(icon->dnd_command, "%s %%d", icon->command);
2198 #endif
2200 return True;
2204 void
2205 reattachIcon(WDock *dock, WAppIcon *icon, int x, int y)
2207 int index;
2209 for(index=1; index<dock->max_icons; index++) {
2210 if(dock->icon_array[index] == icon)
2211 break;
2213 assert(index < dock->max_icons);
2215 icon->yindex = y;
2216 icon->xindex = x;
2218 icon->x_pos = dock->x_pos + x*ICON_SIZE;
2219 icon->y_pos = dock->y_pos + y*ICON_SIZE;
2223 Bool
2224 moveIconBetweenDocks(WDock *src, WDock *dest, WAppIcon *icon, int x, int y)
2226 WWindow *wwin;
2227 char **argv;
2228 int argc;
2229 int index;
2231 if (src == dest)
2232 return True; /* No move needed, we're already there */
2234 if (dest == NULL)
2235 return False;
2237 wwin = icon->icon->owner;
2240 * For the moment we can't do this if we move icons in Clip from one
2241 * workspace to other, because if we move two or more icons without
2242 * command, the dialog box will not be able to tell us to which of the
2243 * moved icons it applies. -Dan
2245 if ((dest->type==WM_DOCK /*|| dest->keep_attracted*/) && icon->command==NULL) {
2246 if (XGetCommand(dpy, wwin->client_win, &argv, &argc) && argc>0) {
2248 icon->command = wtokenjoin(argv, argc);
2249 XFreeStringList(argv);
2250 } else {
2251 char *command=NULL;
2253 icon->editing = 1;
2254 /* icon->forced_dock = 1;*/
2255 if (wInputDialog(src->screen_ptr, _("Dock Icon"),
2256 _("Type the command used to launch the application"),
2257 &command)) {
2258 if (command && (command[0]==0 ||
2259 (command[0]=='-' && command[1]==0))) {
2260 wfree(command);
2261 command = NULL;
2263 icon->command = command;
2264 } else {
2265 icon->editing = 0;
2266 if (command)
2267 wfree(command);
2268 return False;
2270 icon->editing = 0;
2274 if (dest->type == WM_DOCK)
2275 wClipMakeIconOmnipresent(icon, False);
2277 for(index=1; index<src->max_icons; index++) {
2278 if(src->icon_array[index] == icon)
2279 break;
2281 assert(index < src->max_icons);
2283 src->icon_array[index] = NULL;
2284 src->icon_count--;
2286 for(index=1; index<dest->max_icons; index++) {
2287 if(dest->icon_array[index] == NULL)
2288 break;
2290 /* if (index == dest->max_icons)
2291 return; */
2293 assert(index < dest->max_icons);
2295 dest->icon_array[index] = icon;
2296 icon->dock = dest;
2298 /* deselect the icon */
2299 if (icon->icon->selected)
2300 wIconSelect(icon->icon);
2302 if (dest->type == WM_DOCK) {
2303 icon->icon->core->descriptor.handle_enternotify = NULL;
2304 icon->icon->core->descriptor.handle_leavenotify = NULL;
2305 } else {
2306 icon->icon->core->descriptor.handle_enternotify = clipEnterNotify;
2307 icon->icon->core->descriptor.handle_leavenotify = clipLeaveNotify;
2310 /* set it to be kept when moving to dock.
2311 * Unless the icon does not have a command set
2313 if (icon->command && dest->type==WM_DOCK) {
2314 icon->attracted = 0;
2315 if (icon->icon->shadowed) {
2316 icon->icon->shadowed = 0;
2317 icon->icon->force_paint = 1;
2321 if (src->auto_collapse || src->auto_raise_lower)
2322 clipLeave(src);
2324 icon->yindex = y;
2325 icon->xindex = x;
2327 icon->x_pos = dest->x_pos + x*ICON_SIZE;
2328 icon->y_pos = dest->y_pos + y*ICON_SIZE;
2330 dest->icon_count++;
2332 MoveInStackListUnder(dest->icon_array[index-1]->icon->core,
2333 icon->icon->core);
2334 wAppIconPaint(icon);
2336 return True;
2340 void
2341 wDockDetach(WDock *dock, WAppIcon *icon)
2343 int index;
2345 /* make the settings panel be closed */
2346 if (icon->panel) {
2347 DestroyDockAppSettingsPanel(icon->panel);
2350 /* This must be called before icon->dock is set to NULL.
2351 * Don't move it. -Dan
2353 wClipMakeIconOmnipresent(icon, False);
2355 icon->docked = 0;
2356 icon->dock = NULL;
2357 icon->attracted = 0;
2358 icon->auto_launch = 0;
2359 if (icon->icon->shadowed) {
2360 icon->icon->shadowed = 0;
2361 icon->icon->force_paint = 1;
2364 /* deselect the icon */
2365 if (icon->icon->selected)
2366 wIconSelect(icon->icon);
2368 if (icon->command) {
2369 wfree(icon->command);
2370 icon->command = NULL;
2372 #ifdef OFFIX_DND
2373 if (icon->dnd_command) {
2374 wfree(icon->dnd_command);
2375 icon->dnd_command = NULL;
2377 #endif
2379 for (index=1; index<dock->max_icons; index++)
2380 if (dock->icon_array[index] == icon)
2381 break;
2382 assert(index < dock->max_icons);
2383 dock->icon_array[index] = NULL;
2384 icon->yindex = -1;
2385 icon->xindex = -1;
2387 dock->icon_count--;
2389 /* if the dock is not attached to an application or
2390 * the the application did not set the approriate hints yet,
2391 * destroy the icon */
2392 #ifdef REDUCE_APPICONS
2393 if ((icon->num_apps == 0) && (!icon->running || !wApplicationOf(icon->main_window)) )
2394 #else
2395 if (!icon->running || !wApplicationOf(icon->main_window))
2396 #endif
2397 wAppIconDestroy(icon);
2398 else {
2399 icon->icon->core->descriptor.handle_mousedown = appIconMouseDown;
2400 icon->icon->core->descriptor.handle_enternotify = NULL;
2401 icon->icon->core->descriptor.handle_leavenotify = NULL;
2402 icon->icon->core->descriptor.parent_type = WCLASS_APPICON;
2403 icon->icon->core->descriptor.parent = icon;
2405 ChangeStackingLevel(icon->icon->core, NORMAL_ICON_LEVEL);
2407 wAppIconPaint(icon);
2408 if (wPreferences.auto_arrange_icons) {
2409 wArrangeIcons(dock->screen_ptr, True);
2412 if (dock->auto_collapse || dock->auto_raise_lower)
2413 clipLeave(dock);
2418 * returns the closest Dock slot index for the passed
2419 * coordinates.
2421 * Returns False if icon can't be docked.
2423 * Note: this function should NEVER alter ret_x or ret_y, unless it will
2424 * return True. -Dan
2426 Bool
2427 wDockSnapIcon(WDock *dock, WAppIcon *icon, int req_x, int req_y,
2428 int *ret_x, int *ret_y, int redocking)
2430 WScreen *scr = dock->screen_ptr;
2431 int dx, dy;
2432 int ex_x, ex_y;
2433 int i, offset = ICON_SIZE/2;
2434 WAppIcon *aicon = NULL;
2435 WAppIcon *nicon = NULL;
2436 int max_y_icons, max_x_icons;
2438 max_x_icons = scr->scr_width/ICON_SIZE;
2439 max_y_icons = scr->scr_height/ICON_SIZE-1;
2441 if (wPreferences.flags.noupdates)
2442 return False;
2444 dx = dock->x_pos;
2445 dy = dock->y_pos;
2447 /* if the dock is full */
2448 if (!redocking &&
2449 (dock->icon_count >= dock->max_icons)) {
2450 return False;
2453 /* exact position */
2454 if (req_y < dy)
2455 ex_y = (req_y - offset - dy)/ICON_SIZE;
2456 else
2457 ex_y = (req_y + offset - dy)/ICON_SIZE;
2459 if (req_x < dx)
2460 ex_x = (req_x - offset - dx)/ICON_SIZE;
2461 else
2462 ex_x = (req_x + offset - dx)/ICON_SIZE;
2464 /* check if the icon is outside the screen boundaries */
2465 if (dx + ex_x*ICON_SIZE < -ICON_SIZE+2 ||
2466 dx + ex_x*ICON_SIZE >= scr->scr_width-1 ||
2467 dy + ex_y*ICON_SIZE < -ICON_SIZE+2 ||
2468 dy + ex_y*ICON_SIZE >= scr->scr_height-1)
2469 return False;
2471 if (dock->type == WM_DOCK) {
2472 if (icon->dock != dock && ex_x != 0)
2473 return False;
2475 aicon = NULL;
2476 for (i=0; i<dock->max_icons; i++) {
2477 nicon = dock->icon_array[i];
2478 if (nicon && nicon->yindex == ex_y) {
2479 aicon = nicon;
2480 break;
2484 if (redocking) {
2485 int sig, done, closest;
2487 /* Possible cases when redocking:
2489 * icon dragged out of range of any slot -> false
2490 * icon dragged to range of free slot
2491 * icon dragged to range of same slot
2492 * icon dragged to range of different icon
2494 if (abs(ex_x) > DOCK_DETTACH_THRESHOLD)
2495 return False;
2497 if (ex_y>=0 && ex_y<=max_y_icons && (aicon==icon || !aicon)) {
2498 *ret_x = 0;
2499 *ret_y = ex_y;
2500 return True;
2503 /* start looking at the upper slot or lower? */
2504 if (ex_y*ICON_SIZE < (req_y + offset - dy))
2505 sig = 1;
2506 else
2507 sig = -1;
2509 closest = -1;
2510 done = 0;
2511 /* look for closest free slot */
2512 for (i=0; i<(DOCK_DETTACH_THRESHOLD+1)*2 && !done; i++) {
2513 int j;
2515 done = 1;
2516 closest = sig*(i/2) + ex_y;
2517 /* check if this slot is used */
2518 if (closest >= 0) {
2519 for (j = 0; j<dock->max_icons; j++) {
2520 if (dock->icon_array[j]
2521 && dock->icon_array[j]->yindex==closest) {
2522 /* slot is used by someone else */
2523 if (dock->icon_array[j]!=icon)
2524 done = 0;
2525 break;
2529 sig = -sig;
2531 if (done && closest >= 0 && closest <= max_y_icons &&
2532 ((ex_y >= closest && ex_y - closest < DOCK_DETTACH_THRESHOLD+1)
2534 (ex_y < closest && closest - ex_y <= DOCK_DETTACH_THRESHOLD+1))) {
2535 *ret_x = 0;
2536 *ret_y = closest;
2537 return True;
2539 } else { /* !redocking */
2541 /* if slot is free and the icon is close enough, return it */
2542 if (!aicon && ex_x == 0 && ex_y >= 0 && ex_y <= max_y_icons) {
2543 *ret_x = 0;
2544 *ret_y = ex_y;
2545 return True;
2548 } else { /* CLIP */
2549 int neighbours = 0;
2550 int start, stop, k;
2552 start = icon->omnipresent ? 0 : scr->current_workspace;
2553 stop = icon->omnipresent ? scr->workspace_count : start+1;
2555 aicon = NULL;
2556 for (k=start; k<stop; k++) {
2557 WDock *tmp = scr->workspaces[k]->clip;
2558 if (!tmp)
2559 continue;
2560 for (i=0; i<tmp->max_icons; i++) {
2561 nicon = tmp->icon_array[i];
2562 if (nicon && nicon->xindex == ex_x && nicon->yindex == ex_y) {
2563 aicon = nicon;
2564 break;
2567 if (aicon)
2568 break;
2570 for (k=start; k<stop; k++) {
2571 WDock *tmp = scr->workspaces[k]->clip;
2572 if (!tmp)
2573 continue;
2574 for (i=0; i<tmp->max_icons; i++) {
2575 nicon = tmp->icon_array[i];
2576 if (nicon && nicon != icon && /* Icon can't be it's own neighbour */
2577 (abs(nicon->xindex - ex_x) <= CLIP_ATTACH_VICINITY &&
2578 abs(nicon->yindex - ex_y) <= CLIP_ATTACH_VICINITY)) {
2579 neighbours = 1;
2580 break;
2583 if (neighbours)
2584 break;
2587 if (neighbours && (aicon==NULL || (redocking && aicon == icon))) {
2588 *ret_x = ex_x;
2589 *ret_y = ex_y;
2590 return True;
2593 return False;
2596 #define MIN(x, y) ((x) > (y) ? (y) : (x))
2597 #define MAX(x, y) ((x) < (y) ? (y) : (x))
2599 #define ON_SCREEN(x, y, sx, ex, sy, ey) \
2600 ((((x)+ICON_SIZE/2) >= (sx)) && (((y)+ICON_SIZE/2) >= (sy)) && \
2601 (((x) + (ICON_SIZE/2)) <= (ex)) && (((y) + (ICON_SIZE/2)) <= ey))
2605 * returns true if it can find a free slot in the dock,
2606 * in which case it changes x_pos and y_pos accordingly.
2607 * Else returns false.
2609 Bool
2610 wDockFindFreeSlot(WDock *dock, int *x_pos, int *y_pos)
2612 WScreen *scr = dock->screen_ptr;
2613 WAppIcon *btn;
2614 WAppIconChain *chain;
2615 unsigned char *slot_map;
2616 int mwidth;
2617 int r;
2618 int x, y;
2619 int i, done = False;
2620 int corner;
2621 int sx=0, sy=0, ex=scr->scr_width, ey=scr->scr_height;
2622 int extra_count=0;
2624 if (dock->type == WM_CLIP &&
2625 dock != scr->workspaces[scr->current_workspace]->clip)
2626 extra_count = scr->global_icon_count;
2628 /* if the dock is full */
2629 if (dock->icon_count+extra_count >= dock->max_icons) {
2630 return False;
2633 if (!wPreferences.flags.nodock && scr->dock) {
2634 if (scr->dock->on_right_side)
2635 ex -= ICON_SIZE + DOCK_EXTRA_SPACE;
2636 else
2637 sx += ICON_SIZE + DOCK_EXTRA_SPACE;
2640 if (ex < dock->x_pos)
2641 ex = dock->x_pos;
2642 if (sx > dock->x_pos+ICON_SIZE)
2643 sx = dock->x_pos+ICON_SIZE;
2644 #define C_NONE 0
2645 #define C_NW 1
2646 #define C_NE 2
2647 #define C_SW 3
2648 #define C_SE 4
2650 /* check if clip is in a corner */
2651 if (dock->type==WM_CLIP) {
2652 if (dock->x_pos < 1 && dock->y_pos < 1)
2653 corner = C_NE;
2654 else if (dock->x_pos < 1 && dock->y_pos >= (ey-ICON_SIZE))
2655 corner = C_SE;
2656 else if (dock->x_pos >= (ex-ICON_SIZE)&& dock->y_pos >= (ey-ICON_SIZE))
2657 corner = C_SW;
2658 else if (dock->x_pos >= (ex-ICON_SIZE) && dock->y_pos < 1)
2659 corner = C_NW;
2660 else
2661 corner = C_NONE;
2662 } else
2663 corner = C_NONE;
2665 /* If the clip is in the corner, use only slots that are in the border
2666 * of the screen */
2667 if (corner!=C_NONE) {
2668 char *hmap, *vmap;
2669 int hcount, vcount;
2671 hcount = MIN(dock->max_icons, scr->scr_width/ICON_SIZE);
2672 vcount = MIN(dock->max_icons, scr->scr_height/ICON_SIZE);
2673 hmap = wmalloc(hcount+1);
2674 memset(hmap, 0, hcount+1);
2675 vmap = wmalloc(vcount+1);
2676 memset(vmap, 0, vcount+1);
2678 /* mark used positions */
2679 switch (corner) {
2680 case C_NE:
2681 for (i=0; i<dock->max_icons; i++) {
2682 btn = dock->icon_array[i];
2683 if (!btn)
2684 continue;
2686 if (btn->xindex==0 && btn->yindex > 0 && btn->yindex < vcount)
2687 vmap[btn->yindex] = 1;
2688 else if (btn->yindex==0 && btn->xindex>0 && btn->xindex<hcount)
2689 hmap[btn->xindex] = 1;
2691 for (chain=scr->global_icons; chain!=NULL; chain=chain->next) {
2692 btn = chain->aicon;
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 break;
2699 case C_NW:
2700 for (i=0; i<dock->max_icons; i++) {
2701 btn = dock->icon_array[i];
2702 if (!btn)
2703 continue;
2705 if (btn->xindex==0 && btn->yindex > 0 && btn->yindex < vcount)
2706 vmap[btn->yindex] = 1;
2707 else if (btn->yindex==0 && btn->xindex<0 &&btn->xindex>-hcount)
2708 hmap[-btn->xindex] = 1;
2710 for (chain=scr->global_icons; chain!=NULL; chain=chain->next) {
2711 btn = chain->aicon;
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 break;
2718 case C_SE:
2719 for (i=0; i<dock->max_icons; i++) {
2720 btn = dock->icon_array[i];
2721 if (!btn)
2722 continue;
2724 if (btn->xindex==0 && btn->yindex < 0 && btn->yindex > -vcount)
2725 vmap[-btn->yindex] = 1;
2726 else if (btn->yindex==0 && btn->xindex>0 && btn->xindex<hcount)
2727 hmap[btn->xindex] = 1;
2729 for (chain=scr->global_icons; chain!=NULL; chain=chain->next) {
2730 btn = chain->aicon;
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 break;
2737 case C_SW:
2738 default:
2739 for (i=0; i<dock->max_icons; i++) {
2740 btn = dock->icon_array[i];
2741 if (!btn)
2742 continue;
2744 if (btn->xindex==0 && btn->yindex < 0 && btn->yindex > -vcount)
2745 vmap[-btn->yindex] = 1;
2746 else if (btn->yindex==0 && btn->xindex<0 &&btn->xindex>-hcount)
2747 hmap[-btn->xindex] = 1;
2749 for (chain=scr->global_icons; chain!=NULL; chain=chain->next) {
2750 btn = chain->aicon;
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;
2757 x=0; y=0;
2758 done = 0;
2759 /* search a vacant slot */
2760 for (i=1; i<MAX(vcount, hcount); i++) {
2761 if (i < vcount && vmap[i]==0) {
2762 /* found a slot */
2763 x = 0;
2764 y = i;
2765 done = 1;
2766 break;
2767 } else if (i < hcount && hmap[i]==0) {
2768 /* found a slot */
2769 x = i;
2770 y = 0;
2771 done = 1;
2772 break;
2775 wfree(vmap);
2776 wfree(hmap);
2777 /* If found a slot, translate and return */
2778 if (done) {
2779 if (corner==C_NW || corner==C_NE) {
2780 *y_pos = y;
2781 } else {
2782 *y_pos = -y;
2784 if (corner==C_NE || corner==C_SE) {
2785 *x_pos = x;
2786 } else {
2787 *x_pos = -x;
2789 return True;
2791 /* else, try to find a slot somewhere else */
2794 /* a map of mwidth x mwidth would be enough if we allowed icons to be
2795 * placed outside of screen */
2796 mwidth = (int)ceil(sqrt(dock->max_icons));
2798 /* In the worst case (the clip is in the corner of the screen),
2799 * the amount of icons that fit in the clip is smaller.
2800 * Double the map to get a safe value.
2802 mwidth += mwidth;
2804 r = (mwidth-1)/2;
2806 slot_map = wmalloc(mwidth*mwidth);
2807 memset(slot_map, 0, mwidth*mwidth);
2809 #define XY2OFS(x,y) (MAX(abs(x),abs(y)) > r) ? 0 : (((y)+r)*(mwidth)+(x)+r)
2811 /* mark used slots in the map. If the slot falls outside the map
2812 * (for example, when all icons are placed in line), ignore them. */
2813 for (i=0; i<dock->max_icons; i++) {
2814 btn = dock->icon_array[i];
2815 if (btn)
2816 slot_map[XY2OFS(btn->xindex, btn->yindex)] = 1;
2818 for (chain=scr->global_icons; chain!=NULL; chain=chain->next) {
2819 slot_map[XY2OFS(chain->aicon->xindex, chain->aicon->yindex)] = 1;
2821 /* Find closest slot from the center that is free by scanning the
2822 * map from the center to outward in circular passes.
2823 * This will not result in a neat layout, but will be optimal
2824 * in the sense that there will not be holes left.
2826 done = 0;
2827 for (i = 1; i <= r && !done; i++) {
2828 int tx, ty;
2830 /* top and bottom parts of the ring */
2831 for (x = -i; x <= i && !done; x++) {
2832 tx = dock->x_pos + x*ICON_SIZE;
2833 y = -i;
2834 ty = dock->y_pos + y*ICON_SIZE;
2835 if (slot_map[XY2OFS(x,y)]==0
2836 && ON_SCREEN(tx, ty, sx, ex, sy, ey)) {
2837 *x_pos = x;
2838 *y_pos = y;
2839 done = 1;
2840 break;
2842 y = i;
2843 ty = dock->y_pos + y*ICON_SIZE;
2844 if (slot_map[XY2OFS(x,y)]==0
2845 && ON_SCREEN(tx, ty, sx, ex, sy, ey)) {
2846 *x_pos = x;
2847 *y_pos = y;
2848 done = 1;
2849 break;
2852 /* left and right parts of the ring */
2853 for (y = -i+1; y <= i-1; y++) {
2854 ty = dock->y_pos + y*ICON_SIZE;
2855 x = -i;
2856 tx = dock->x_pos + x*ICON_SIZE;
2857 if (slot_map[XY2OFS(x,y)]==0
2858 && ON_SCREEN(tx, ty, sx, ex, sy, ey)) {
2859 *x_pos = x;
2860 *y_pos = y;
2861 done = 1;
2862 break;
2864 x = i;
2865 tx = dock->x_pos + x*ICON_SIZE;
2866 if (slot_map[XY2OFS(x,y)]==0
2867 && ON_SCREEN(tx, ty, sx, ex, sy, ey)) {
2868 *x_pos = x;
2869 *y_pos = y;
2870 done = 1;
2871 break;
2875 wfree(slot_map);
2876 #undef XY2OFS
2877 return done;
2881 static void
2882 moveDock(WDock *dock, int new_x, int new_y)
2884 WAppIcon *btn;
2885 int i;
2887 dock->x_pos = new_x;
2888 dock->y_pos = new_y;
2889 for (i=0; i<dock->max_icons; i++) {
2890 btn = dock->icon_array[i];
2891 if (btn) {
2892 btn->x_pos = new_x + btn->xindex*ICON_SIZE;
2893 btn->y_pos = new_y + btn->yindex*ICON_SIZE;
2894 XMoveWindow(dpy, btn->icon->core->window, btn->x_pos, btn->y_pos);
2900 static void
2901 swapDock(WDock *dock)
2903 WScreen *scr = dock->screen_ptr;
2904 WAppIcon *btn;
2905 int x, i;
2908 if (dock->on_right_side) {
2909 x = dock->x_pos = scr->scr_width - ICON_SIZE - DOCK_EXTRA_SPACE;
2910 } else {
2911 x = dock->x_pos = DOCK_EXTRA_SPACE;
2914 for (i=0; i<dock->max_icons; i++) {
2915 btn = dock->icon_array[i];
2916 if (btn) {
2917 btn->x_pos = x;
2918 XMoveWindow(dpy, btn->icon->core->window, btn->x_pos, btn->y_pos);
2922 wScreenUpdateUsableArea(scr);
2926 static pid_t
2927 execCommand(WAppIcon *btn, char *command, WSavedState *state)
2929 WScreen *scr = btn->icon->core->screen_ptr;
2930 pid_t pid;
2931 char **argv;
2932 int argc;
2933 char *cmdline;
2935 cmdline = ExpandOptions(scr, command);
2937 if (scr->flags.dnd_data_convertion_status || !cmdline) {
2938 if (cmdline)
2939 wfree(cmdline);
2940 if (state)
2941 wfree(state);
2942 return 0;
2945 wtokensplit(cmdline, &argv, &argc);
2947 if (argv==NULL) {
2948 if (cmdline)
2949 wfree(cmdline);
2950 if (state)
2951 wfree(state);
2952 return 0;
2955 if ((pid=fork())==0) {
2956 char **args;
2957 int i;
2959 SetupEnvironment(scr);
2961 #ifdef HAVE_SETPGID
2962 setpgid(0, 0);
2963 #endif
2965 args = malloc(sizeof(char*)*(argc+1));
2966 if (!args)
2967 exit(111);
2968 for (i=0; i<argc; i++) {
2969 args[i] = argv[i];
2971 args[argc] = NULL;
2972 execvp(argv[0], args);
2973 exit(111);
2975 while (argc > 0)
2976 wfree(argv[--argc]);
2977 wfree(argv);
2979 if (pid > 0) {
2980 if (!state) {
2981 state = wmalloc(sizeof(WSavedState));
2982 memset(state, 0, sizeof(WSavedState));
2983 state->hidden = -1;
2984 state->miniaturized = -1;
2985 state->shaded = -1;
2986 if (btn->dock==scr->dock || btn->omnipresent)
2987 state->workspace = -1;
2988 else
2989 state->workspace = scr->current_workspace;
2991 wWindowAddSavedState(btn->wm_instance, btn->wm_class, cmdline, pid,
2992 state);
2993 wAddDeathHandler(pid, (WDeathHandler*)trackDeadProcess,
2994 btn->dock);
2995 } else if (state) {
2996 wfree(state);
2998 wfree(cmdline);
2999 return pid;
3003 void
3004 wDockHideIcons(WDock *dock)
3006 int i;
3008 if (dock==NULL)
3009 return;
3011 for (i=1; i<dock->max_icons; i++) {
3012 if (dock->icon_array[i])
3013 XUnmapWindow(dpy, dock->icon_array[i]->icon->core->window);
3015 dock->mapped = 0;
3017 dockIconPaint(dock->icon_array[0]);
3021 void
3022 wDockShowIcons(WDock *dock)
3024 int i, newlevel;
3025 WAppIcon *btn;
3027 if (dock==NULL)
3028 return;
3030 btn = dock->icon_array[0];
3031 moveDock(dock, btn->x_pos, btn->y_pos);
3033 newlevel = dock->lowered ? WMNormalLevel : WMDockLevel;
3034 ChangeStackingLevel(btn->icon->core, newlevel);
3036 for (i=1; i<dock->max_icons; i++) {
3037 if (dock->icon_array[i]) {
3038 MoveInStackListAbove(dock->icon_array[i]->icon->core,
3039 btn->icon->core);
3040 break;
3044 if (!dock->collapsed) {
3045 for (i=1; i<dock->max_icons; i++) {
3046 if (dock->icon_array[i]) {
3047 XMapWindow(dpy, dock->icon_array[i]->icon->core->window);
3051 dock->mapped = 1;
3053 dockIconPaint(btn);
3057 void
3058 wDockLower(WDock *dock)
3060 int i;
3062 for (i=0; i<dock->max_icons; i++) {
3063 if (dock->icon_array[i])
3064 wLowerFrame(dock->icon_array[i]->icon->core);
3069 void
3070 wDockRaise(WDock *dock)
3072 int i;
3074 for (i=dock->max_icons-1; i>=0; i--) {
3075 if (dock->icon_array[i])
3076 wRaiseFrame(dock->icon_array[i]->icon->core);
3081 void
3082 wDockRaiseLower(WDock *dock)
3084 if (!dock->icon_array[0]->icon->core->stacking->above
3085 ||(dock->icon_array[0]->icon->core->stacking->window_level
3086 !=dock->icon_array[0]->icon->core->stacking->above->stacking->window_level))
3087 wDockLower(dock);
3088 else
3089 wDockRaise(dock);
3093 void
3094 wDockFinishLaunch(WDock *dock, WAppIcon *icon)
3096 icon->launching = 0;
3097 icon->relaunching = 0;
3098 dockIconPaint(icon);
3102 WAppIcon*
3103 wDockFindIconForWindow(WDock *dock, Window window)
3105 WAppIcon *icon;
3106 int i;
3108 for (i=0; i<dock->max_icons; i++) {
3109 icon = dock->icon_array[i];
3110 if (icon && icon->main_window == window)
3111 return icon;
3113 return NULL;
3117 void
3118 wDockTrackWindowLaunch(WDock *dock, Window window)
3120 WAppIcon *icon;
3121 #ifdef REDUCE_APPICONS
3122 WAppIconAppList *tapplist;
3123 #endif
3124 char *wm_class, *wm_instance;
3125 int i;
3126 Bool firstPass = True;
3127 Bool found = False;
3128 char *command = NULL;
3131 int argc;
3132 char **argv;
3134 if (XGetCommand(dpy, window, &argv, &argc)) {
3135 if (argc > 0 && argv != NULL)
3136 command = wtokenjoin(argv,argc);
3137 if (argv) {
3138 XFreeStringList(argv);
3143 if (!PropGetWMClass(window, &wm_class, &wm_instance) ||
3144 (!wm_class && !wm_instance))
3145 return;
3147 retry:
3148 for (i=0; i<dock->max_icons; i++) {
3149 icon = dock->icon_array[i];
3150 if (!icon)
3151 continue;
3153 /* app is already attached to icon */
3154 if (icon->main_window == window) {
3155 found = True;
3156 break;
3159 if ((icon->wm_instance || icon->wm_class)
3160 && (icon->launching
3161 || (dock->screen_ptr->flags.startup && !icon->running))) {
3163 if (icon->wm_instance && wm_instance &&
3164 strcmp(icon->wm_instance, wm_instance)!=0) {
3165 continue;
3167 if (icon->wm_class && wm_class &&
3168 strcmp(icon->wm_class, wm_class)!=0) {
3169 continue;
3171 if (firstPass && command && strcmp(icon->command, command)!=0) {
3172 continue;
3175 if (!icon->relaunching) {
3176 WApplication *wapp;
3178 /* Possibly an application that was docked with dockit,
3179 * but the user did not update WMState to indicate that
3180 * it was docked by force */
3181 wapp = wApplicationOf(window);
3182 if (!wapp) {
3183 icon->forced_dock = 1;
3184 icon->running = 0;
3186 if (!icon->forced_dock)
3187 icon->main_window = window;
3189 #ifdef REDUCE_APPICONS
3190 tapplist = wmalloc(sizeof(WAppIconAppList));
3191 memset(tapplist, 0, sizeof(WAppIconAppList));
3192 tapplist->next = icon->applist;
3193 if (icon->applist)
3194 icon->applist->prev = tapplist;
3195 icon->applist = tapplist;
3196 tapplist->wapp = wApplicationOf(window);
3197 icon->num_apps++;
3198 #endif
3200 found = True;
3201 wDockFinishLaunch(dock, icon);
3202 break;
3206 if (firstPass && !found) {
3207 firstPass = False;
3208 goto retry;
3211 if (command)
3212 wfree(command);
3214 if (wm_class)
3215 XFree(wm_class);
3216 if (wm_instance)
3217 XFree(wm_instance);
3222 void
3223 wClipUpdateForWorkspaceChange(WScreen *scr, int workspace)
3225 if (!wPreferences.flags.noclip) {
3226 scr->clip_icon->dock = scr->workspaces[workspace]->clip;
3227 if (scr->current_workspace != workspace) {
3228 WDock *old_clip = scr->workspaces[scr->current_workspace]->clip;
3229 WAppIconChain *chain = scr->global_icons;
3231 while (chain) {
3232 moveIconBetweenDocks(chain->aicon->dock,
3233 scr->workspaces[workspace]->clip,
3234 chain->aicon, chain->aicon->xindex,
3235 chain->aicon->yindex);
3236 if (scr->workspaces[workspace]->clip->collapsed)
3237 XUnmapWindow(dpy, chain->aicon->icon->core->window);
3238 chain = chain->next;
3241 wDockHideIcons(old_clip);
3242 if (old_clip->auto_raise_lower) {
3243 if (old_clip->auto_raise_magic) {
3244 WMDeleteTimerHandler(old_clip->auto_raise_magic);
3245 old_clip->auto_raise_magic = NULL;
3247 wDockLower(old_clip);
3249 if (old_clip->auto_collapse) {
3250 if (old_clip->auto_expand_magic) {
3251 WMDeleteTimerHandler(old_clip->auto_expand_magic);
3252 old_clip->auto_expand_magic = NULL;
3254 old_clip->collapsed = 1;
3256 wDockShowIcons(scr->workspaces[workspace]->clip);
3258 if (scr->flags.clip_balloon_mapped)
3259 showClipBalloon(scr->clip_icon->dock, workspace);
3265 static void
3266 trackDeadProcess(pid_t pid, unsigned char status, WDock *dock)
3268 WAppIcon *icon;
3269 int i;
3271 for (i=0; i<dock->max_icons; i++) {
3272 icon = dock->icon_array[i];
3273 if (!icon)
3274 continue;
3276 if (icon->launching && icon->pid == pid) {
3277 if (!icon->relaunching) {
3278 icon->running = 0;
3279 icon->main_window = None;
3281 wDockFinishLaunch(dock, icon);
3282 icon->pid = 0;
3283 if (status==111) {
3284 char msg[PATH_MAX];
3285 #ifdef OFFIX_DND
3286 sprintf(msg, _("Could not execute command \"%s\""),
3287 icon->drop_launch && icon->dnd_command
3288 ? icon->dnd_command : icon->command);
3289 #else
3290 sprintf(msg, _("Could not execute command \"%s\""),
3291 icon->command);
3292 #endif
3293 wMessageDialog(dock->screen_ptr, _("Error"), msg,
3294 _("OK"), NULL, NULL);
3296 break;
3302 static void
3303 toggleLowered(WDock *dock)
3305 WAppIcon *tmp;
3306 int newlevel, i;
3308 /* lower/raise Dock */
3309 if (!dock->lowered) {
3310 newlevel = WMNormalLevel;
3311 dock->lowered = 1;
3312 } else {
3313 newlevel = WMDockLevel;
3314 dock->lowered = 0;
3317 for (i=0; i<dock->max_icons; i++) {
3318 tmp = dock->icon_array[i];
3319 if (!tmp)
3320 continue;
3322 ChangeStackingLevel(tmp->icon->core, newlevel);
3323 if (dock->lowered)
3324 wLowerFrame(tmp->icon->core);
3327 if (dock->type == WM_DOCK)
3328 wScreenUpdateUsableArea(dock->screen_ptr);
3332 static void
3333 toggleCollapsed(WDock *dock)
3335 if (dock->collapsed) {
3336 dock->collapsed = 0;
3337 wDockShowIcons(dock);
3339 else {
3340 dock->collapsed = 1;
3341 wDockHideIcons(dock);
3346 static void
3347 openDockMenu(WDock *dock, WAppIcon *aicon, XEvent *event)
3349 WScreen *scr = dock->screen_ptr;
3350 WObjDescriptor *desc;
3351 WMenuEntry *entry;
3352 WApplication *wapp = NULL;
3353 int index = 0;
3354 int x_pos;
3355 int n_selected;
3356 int appIsRunning = aicon->running && aicon->icon && aicon->icon->owner;
3358 if (dock->type == WM_DOCK) {
3359 /* keep on top */
3360 entry = dock->menu->entries[index];
3362 if (dock->lowered) {
3363 entry->text = _("Keep on Top");
3364 } else {
3365 entry->text = _("Allow Lowering");
3367 entry->clientdata = dock;
3369 dock->menu->flags.realized = 0;
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 if (n_selected > 0) {
3388 entry->flags.indicator = 0;
3389 entry->text = _("Toggle Omnipresent");
3390 } else {
3391 entry->flags.indicator = 1;
3392 entry->flags.indicator_on = aicon->omnipresent;
3393 entry->flags.indicator_type = MI_CHECK;
3394 entry->text = _("Omnipresent");
3398 /* select/unselect icon */
3399 entry = dock->menu->entries[++index];
3400 entry->clientdata = aicon;
3401 entry->flags.indicator_on = aicon->icon->selected;
3402 wMenuSetEnabled(dock->menu, index, aicon!=scr->clip_icon);
3404 /* select/unselect all icons */
3405 entry = dock->menu->entries[++index];
3406 entry->clientdata = aicon;
3407 if (n_selected > 0)
3408 entry->text = _("Unselect All Icons");
3409 else
3410 entry->text = _("Select All Icons");
3411 wMenuSetEnabled(dock->menu, index, dock->icon_count > 1);
3413 /* keep icon(s) */
3414 entry = dock->menu->entries[++index];
3415 entry->clientdata = aicon;
3416 if (n_selected > 1)
3417 entry->text = _("Keep Icons");
3418 else
3419 entry->text = _("Keep Icon");
3420 wMenuSetEnabled(dock->menu, index, dock->icon_count > 1);
3422 /* this is the workspace submenu part */
3423 entry = dock->menu->entries[++index];
3424 if (n_selected > 1)
3425 entry->text = _("Move Icons To");
3426 else
3427 entry->text = _("Move Icon To");
3428 if (scr->clip_submenu)
3429 updateWorkspaceMenu(scr->clip_submenu, aicon);
3430 wMenuSetEnabled(dock->menu, index, !aicon->omnipresent);
3432 /* remove icon(s) */
3433 entry = dock->menu->entries[++index];
3434 entry->clientdata = aicon;
3435 if (n_selected > 1)
3436 entry->text = _("Remove Icons");
3437 else
3438 entry->text = _("Remove Icon");
3439 wMenuSetEnabled(dock->menu, index, dock->icon_count > 1);
3441 /* attract icon(s) */
3442 entry = dock->menu->entries[++index];
3443 entry->clientdata = aicon;
3445 dock->menu->flags.realized = 0;
3446 wMenuRealize(dock->menu);
3450 if (aicon->icon->owner) {
3451 wapp = wApplicationOf(aicon->icon->owner->main_window);
3452 } else {
3453 wapp = NULL;
3456 /* launch */
3457 entry = dock->menu->entries[++index];
3458 entry->clientdata = aicon;
3459 wMenuSetEnabled(dock->menu, index, aicon->command!=NULL);
3461 /* unhide here */
3462 entry = dock->menu->entries[++index];
3463 entry->clientdata = aicon;
3464 if (wapp && wapp->flags.hidden) {
3465 entry->text = _("Unhide Here");
3466 } else {
3467 entry->text = _("Bring Here");
3469 wMenuSetEnabled(dock->menu, index, appIsRunning);
3471 /* hide */
3472 entry = dock->menu->entries[++index];
3473 entry->clientdata = aicon;
3474 if (wapp && wapp->flags.hidden) {
3475 entry->text = _("Unhide");
3476 } else {
3477 entry->text = _("Hide");
3479 wMenuSetEnabled(dock->menu, index, appIsRunning);
3481 /* settings */
3482 entry = dock->menu->entries[++index];
3483 entry->clientdata = aicon;
3484 wMenuSetEnabled(dock->menu, index, !aicon->editing
3485 && !wPreferences.flags.noupdates);
3487 /* kill */
3488 entry = dock->menu->entries[++index];
3489 entry->clientdata = aicon;
3490 wMenuSetEnabled(dock->menu, index, appIsRunning);
3492 if (!dock->menu->flags.realized)
3493 wMenuRealize(dock->menu);
3495 if (dock->type == WM_CLIP) {
3496 x_pos = event->xbutton.x_root+2;
3497 } else {
3498 x_pos = dock->on_right_side ?
3499 scr->scr_width - dock->menu->frame->core->width - 2 : 0;
3502 wMenuMapAt(dock->menu, x_pos, event->xbutton.y_root+2, False);
3504 /* allow drag select */
3505 event->xany.send_event = True;
3506 desc = &dock->menu->menu->descriptor;
3507 (*desc->handle_mousedown)(desc, event);
3511 static void
3512 openClipWorkspaceMenu(WScreen *scr, int x, int y)
3514 if (!scr->clip_ws_menu) {
3515 scr->clip_ws_menu = wWorkspaceMenuMake(scr, False);
3517 wWorkspaceMenuUpdate(scr, scr->clip_ws_menu);
3518 wMenuMapAt(scr->clip_ws_menu, x, y, False);
3522 /******************************************************************/
3523 static void
3524 iconDblClick(WObjDescriptor *desc, XEvent *event)
3526 WAppIcon *btn = desc->parent;
3527 WDock *dock = btn->dock;
3528 WApplication *wapp = NULL;
3529 int unhideHere = 0;
3531 #ifdef REDUCE_APPICONS
3532 if ((btn->icon->owner && !(event->xbutton.state & ControlMask)) ||
3533 ((btn->icon->owner == NULL) && (btn->applist != NULL))) {
3534 if (btn->icon->owner == NULL)
3535 btn->icon->owner = btn->applist->wapp->main_window_desc;
3536 #else
3537 if (btn->icon->owner && !(event->xbutton.state & ControlMask)) {
3538 #endif
3539 wapp = wApplicationOf(btn->icon->owner->main_window);
3541 assert(wapp!=NULL);
3543 unhideHere = (event->xbutton.state & ShiftMask);
3545 /* go to the last workspace that the user worked on the app */
3546 if (wapp->last_workspace != dock->screen_ptr->current_workspace
3547 && !unhideHere) {
3548 wWorkspaceChange(dock->screen_ptr, wapp->last_workspace);
3551 wUnhideApplication(wapp, event->xbutton.button==Button2,
3552 unhideHere);
3554 if (event->xbutton.state & MOD_MASK) {
3555 wHideOtherApplications(btn->icon->owner);
3557 } else {
3558 if (event->xbutton.button==Button1) {
3560 if (event->xbutton.state & MOD_MASK) {
3561 /* raise/lower dock */
3562 toggleLowered(dock);
3563 } else if (btn == dock->screen_ptr->clip_icon) {
3564 if (getClipButton(event->xbutton.x, event->xbutton.y)==CLIP_IDLE)
3565 toggleCollapsed(dock);
3566 else
3567 handleClipChangeWorkspace(dock->screen_ptr, event);
3568 } else if (btn->command) {
3569 if (!btn->launching &&
3570 (!btn->running || (event->xbutton.state & ControlMask))) {
3571 launchDockedApplication(btn);
3573 } else if (btn->xindex == 0 && btn->yindex == 0
3574 && btn->dock->type == WM_DOCK) {
3576 wShowGNUstepPanel(dock->screen_ptr);
3583 static void
3584 handleDockMove(WDock *dock, WAppIcon *aicon, XEvent *event)
3586 WScreen *scr = dock->screen_ptr;
3587 int ofs_x=event->xbutton.x, ofs_y=event->xbutton.y;
3588 int x, y;
3589 XEvent ev;
3590 int grabbed = 0, swapped = 0, done;
3591 Pixmap ghost = None;
3592 int superfluous = wPreferences.superfluous; /* we catch it to avoid problems */
3594 #ifdef DEBUG
3595 puts("moving dock");
3596 #endif
3597 if (XGrabPointer(dpy, aicon->icon->core->window, True, ButtonMotionMask
3598 |ButtonReleaseMask|ButtonPressMask, GrabModeAsync,
3599 GrabModeAsync, None, None, CurrentTime) !=GrabSuccess) {
3600 wwarning("pointer grab failed for dock move");
3602 y = 0;
3603 for (x=0; x<dock->max_icons; x++) {
3604 if (dock->icon_array[x]!=NULL &&
3605 dock->icon_array[x]->yindex > y)
3606 y = dock->icon_array[x]->yindex;
3608 y++;
3609 XResizeWindow(dpy, scr->dock_shadow, ICON_SIZE, ICON_SIZE*y);
3611 done = 0;
3612 while (!done) {
3613 WMMaskEvent(dpy, PointerMotionMask|ButtonReleaseMask|ButtonPressMask
3614 |ButtonMotionMask|ExposureMask, &ev);
3615 switch (ev.type) {
3616 case Expose:
3617 WMHandleEvent(&ev);
3618 break;
3620 case MotionNotify:
3621 if (!grabbed) {
3622 if (abs(ofs_x-ev.xmotion.x)>=MOVE_THRESHOLD
3623 || abs(ofs_y-ev.xmotion.y)>=MOVE_THRESHOLD) {
3624 XChangeActivePointerGrab(dpy, ButtonMotionMask
3625 |ButtonReleaseMask|ButtonPressMask,
3626 wCursor[WCUR_MOVE], CurrentTime);
3627 grabbed=1;
3629 break;
3631 if (dock->type == WM_CLIP) {
3632 if (ev.xmotion.x_root - ofs_x < 0) {
3633 x = 0;
3634 } else if (ev.xmotion.x_root - ofs_x + ICON_SIZE >
3635 scr->scr_width) {
3636 x = scr->scr_width - ICON_SIZE;
3637 } else {
3638 x = ev.xmotion.x_root - ofs_x;
3640 if (ev.xmotion.y_root - ofs_y < 0) {
3641 y = 0;
3642 } else if (ev.xmotion.y_root - ofs_y + ICON_SIZE >
3643 scr->scr_height) {
3644 y = scr->scr_height - ICON_SIZE;
3645 } else {
3646 y = ev.xmotion.y_root - ofs_y;
3648 moveDock(dock, x, y);
3649 } else {
3650 /* move vertically if pointer is inside the dock*/
3651 if ((dock->on_right_side &&
3652 ev.xmotion.x_root >= dock->x_pos - ICON_SIZE)
3653 || (!dock->on_right_side &&
3654 ev.xmotion.x_root <= dock->x_pos + ICON_SIZE*2)) {
3656 if (ev.xmotion.y_root - ofs_y < 0) {
3657 y = 0;
3658 } else if (ev.xmotion.y_root - ofs_y + ICON_SIZE >
3659 scr->scr_height) {
3660 y = scr->scr_height - ICON_SIZE;
3661 } else {
3662 y = ev.xmotion.y_root - ofs_y;
3664 moveDock(dock, dock->x_pos, y);
3666 /* move horizontally to change sides */
3667 x = ev.xmotion.x_root - ofs_x;
3668 if (!dock->on_right_side) {
3670 /* is on left */
3672 if (ev.xmotion.x_root > dock->x_pos + ICON_SIZE*2) {
3673 XMoveWindow(dpy, scr->dock_shadow, scr->scr_width-ICON_SIZE
3674 -DOCK_EXTRA_SPACE-1, dock->y_pos);
3675 if (superfluous && ghost==None) {
3676 ghost = MakeGhostDock(dock, dock->x_pos,
3677 scr->scr_width-ICON_SIZE
3678 -DOCK_EXTRA_SPACE-1,
3679 dock->y_pos);
3680 XSetWindowBackgroundPixmap(dpy, scr->dock_shadow,
3681 ghost);
3682 XClearWindow(dpy, scr->dock_shadow);
3684 XMapRaised(dpy, scr->dock_shadow);
3685 swapped = 1;
3686 } else {
3687 if (superfluous && ghost!=None) {
3688 XFreePixmap(dpy, ghost);
3689 ghost = None;
3691 XUnmapWindow(dpy, scr->dock_shadow);
3692 swapped = 0;
3694 } else {
3695 /* is on right */
3696 if (ev.xmotion.x_root < dock->x_pos - ICON_SIZE) {
3697 XMoveWindow(dpy, scr->dock_shadow,
3698 DOCK_EXTRA_SPACE, dock->y_pos);
3699 if (superfluous && ghost==None) {
3700 ghost = MakeGhostDock(dock, dock->x_pos,
3701 DOCK_EXTRA_SPACE, dock->y_pos);
3702 XSetWindowBackgroundPixmap(dpy, scr->dock_shadow,
3703 ghost);
3704 XClearWindow(dpy, scr->dock_shadow);
3706 XMapRaised(dpy, scr->dock_shadow);
3707 swapped = -1;
3708 } else {
3709 XUnmapWindow(dpy, scr->dock_shadow);
3710 swapped = 0;
3711 if (superfluous && ghost!=None) {
3712 XFreePixmap(dpy, ghost);
3713 ghost = None;
3718 break;
3720 case ButtonPress:
3721 break;
3723 case ButtonRelease:
3724 if (ev.xbutton.button != event->xbutton.button)
3725 break;
3726 XUngrabPointer(dpy, CurrentTime);
3727 XUnmapWindow(dpy, scr->dock_shadow);
3728 XResizeWindow(dpy, scr->dock_shadow, ICON_SIZE, ICON_SIZE);
3729 if (dock->type == WM_DOCK) {
3730 if (swapped!=0) {
3731 if (swapped>0)
3732 dock->on_right_side = 1;
3733 else
3734 dock->on_right_side = 0;
3735 swapDock(dock);
3736 wArrangeIcons(scr, False);
3739 done = 1;
3740 break;
3743 if (superfluous) {
3744 if (ghost!=None)
3745 XFreePixmap(dpy, ghost);
3746 XSetWindowBackground(dpy, scr->dock_shadow, scr->white_pixel);
3748 #ifdef DEBUG
3749 puts("End dock move");
3750 #endif
3755 static void
3756 handleIconMove(WDock *dock, WAppIcon *aicon, XEvent *event)
3758 WScreen *scr = dock->screen_ptr;
3759 Window wins[2];
3760 WIcon *icon = aicon->icon;
3761 WDock *dock2 = NULL, *last_dock = dock, *clip = NULL;
3762 int ondock, grabbed = 0, change_dock = 0, collapsed = 0;
3763 XEvent ev;
3764 int x = aicon->x_pos, y = aicon->y_pos;
3765 int ofs_x = event->xbutton.x, ofs_y = event->xbutton.y;
3766 int shad_x = x, shad_y = y;
3767 int ix = aicon->xindex, iy = aicon->yindex;
3768 int tmp;
3769 Pixmap ghost = None;
3770 Bool docked;
3771 int superfluous = wPreferences.superfluous; /* we catch it to avoid problems */
3772 int omnipresent = aicon->omnipresent; /* this must be cached!!! */
3775 if (wPreferences.flags.noupdates)
3776 return;
3778 if (XGrabPointer(dpy, icon->core->window, True, ButtonMotionMask
3779 |ButtonReleaseMask|ButtonPressMask, GrabModeAsync,
3780 GrabModeAsync, None, None, CurrentTime) !=GrabSuccess) {
3781 #ifdef DEBUG0
3782 wwarning("pointer grab failed for icon move");
3783 #endif
3786 if (!(event->xbutton.state & MOD_MASK))
3787 wRaiseFrame(icon->core);
3789 if (!wPreferences.flags.noclip)
3790 clip = scr->workspaces[scr->current_workspace]->clip;
3792 if (dock == scr->dock && !wPreferences.flags.noclip)
3793 dock2 = clip;
3794 else if (dock != scr->dock && !wPreferences.flags.nodock)
3795 dock2 = scr->dock;
3797 wins[0] = icon->core->window;
3798 wins[1] = scr->dock_shadow;
3799 XRestackWindows(dpy, wins, 2);
3800 XMoveResizeWindow(dpy, scr->dock_shadow, aicon->x_pos, aicon->y_pos,
3801 ICON_SIZE, ICON_SIZE);
3802 if (superfluous) {
3803 if (icon->pixmap!=None)
3804 ghost = MakeGhostIcon(scr, icon->pixmap);
3805 else
3806 ghost = MakeGhostIcon(scr, icon->core->window);
3808 XSetWindowBackgroundPixmap(dpy, scr->dock_shadow, ghost);
3809 XClearWindow(dpy, scr->dock_shadow);
3811 XMapWindow(dpy, scr->dock_shadow);
3813 ondock = 1;
3816 while(1) {
3817 XMaskEvent(dpy, PointerMotionMask|ButtonReleaseMask|ButtonPressMask
3818 |ButtonMotionMask|ExposureMask, &ev);
3819 switch (ev.type) {
3820 case Expose:
3821 WMHandleEvent(&ev);
3822 break;
3824 case MotionNotify:
3825 if (!grabbed) {
3826 if (abs(ofs_x-ev.xmotion.x)>=MOVE_THRESHOLD
3827 || abs(ofs_y-ev.xmotion.y)>=MOVE_THRESHOLD) {
3828 XChangeActivePointerGrab(dpy, ButtonMotionMask
3829 |ButtonReleaseMask|ButtonPressMask,
3830 wCursor[WCUR_MOVE], CurrentTime);
3831 grabbed=1;
3832 } else {
3833 break;
3837 if (omnipresent) {
3838 int i;
3839 for (i=0; i<scr->workspace_count; i++) {
3840 if (i == scr->current_workspace)
3841 continue;
3842 wDockShowIcons(scr->workspaces[i]->clip);
3846 x = ev.xmotion.x_root - ofs_x;
3847 y = ev.xmotion.y_root - ofs_y;
3848 tmp = wDockSnapIcon(dock, aicon, x, y, &ix, &iy, True);
3849 if (tmp && dock2) {
3850 change_dock = 0;
3851 if (last_dock != dock && collapsed) {
3852 last_dock->collapsed = 1;
3853 wDockHideIcons(last_dock);
3854 collapsed = 0;
3856 if (!collapsed && (collapsed = dock->collapsed)) {
3857 dock->collapsed = 0;
3858 wDockShowIcons(dock);
3860 if (dock->auto_raise_lower)
3861 wDockRaise(dock);
3862 last_dock = dock;
3863 } else if (dock2) {
3864 tmp = wDockSnapIcon(dock2, aicon, x, y, &ix, &iy, False);
3865 if (tmp) {
3866 change_dock = 1;
3867 if (last_dock != dock2 && collapsed) {
3868 last_dock->collapsed = 1;
3869 wDockHideIcons(last_dock);
3870 collapsed = 0;
3872 if (!collapsed && (collapsed = dock2->collapsed)) {
3873 dock2->collapsed = 0;
3874 wDockShowIcons(dock2);
3876 if (dock2->auto_raise_lower)
3877 wDockRaise(dock2);
3878 last_dock = dock2;
3881 if (aicon->launching
3882 || aicon->lock
3883 || (aicon->running && !(ev.xmotion.state & MOD_MASK))
3884 || (!aicon->running && tmp)) {
3885 shad_x = last_dock->x_pos + ix*wPreferences.icon_size;
3886 shad_y = last_dock->y_pos + iy*wPreferences.icon_size;
3888 XMoveWindow(dpy, scr->dock_shadow, shad_x, shad_y);
3890 if (!ondock) {
3891 XMapWindow(dpy, scr->dock_shadow);
3893 ondock = 1;
3894 } else {
3895 if (ondock) {
3896 XUnmapWindow(dpy, scr->dock_shadow);
3898 ondock = 0;
3900 XMoveWindow(dpy, icon->core->window, x, y);
3901 break;
3903 case ButtonPress:
3904 break;
3906 case ButtonRelease:
3907 if (ev.xbutton.button != event->xbutton.button)
3908 break;
3909 XUngrabPointer(dpy, CurrentTime);
3910 if (ondock) {
3911 SlideWindow(icon->core->window, x, y, shad_x, shad_y);
3912 XUnmapWindow(dpy, scr->dock_shadow);
3913 if (!change_dock) {
3914 reattachIcon(dock, aicon, ix, iy);
3915 if (clip && dock!=clip && clip->auto_raise_lower)
3916 wDockLower(clip);
3917 } else {
3918 docked = moveIconBetweenDocks(dock, dock2, aicon, ix, iy);
3919 if (!docked) {
3920 /* Slide it back if dock rejected it */
3921 SlideWindow(icon->core->window, x, y, aicon->x_pos,
3922 aicon->y_pos);
3923 reattachIcon(dock, aicon, aicon->xindex,aicon->yindex);
3925 if (last_dock->type==WM_CLIP && last_dock->auto_collapse) {
3926 collapsed = 0;
3929 } else {
3930 aicon->x_pos = x;
3931 aicon->y_pos = y;
3932 if (superfluous) {
3933 if (!aicon->running && !wPreferences.no_animations) {
3934 /* We need to deselect it, even if is deselected in
3935 * wDockDetach(), because else DoKaboom() will fail.
3937 if (aicon->icon->selected)
3938 wIconSelect(aicon->icon);
3940 #ifdef WMSOUND
3941 wSoundPlay(WMSOUND_KABOOM);
3942 #endif
3943 DoKaboom(scr,aicon->icon->core->window, x, y);
3944 #ifdef WMSOUND
3945 } else {
3946 wSoundPlay(WMSOUND_UNDOCK);
3947 #endif
3949 #ifdef WMSOUND
3950 } else {
3951 wSoundPlay(WMSOUND_UNDOCK);
3952 #endif
3954 if (clip && clip->auto_raise_lower)
3955 wDockLower(clip);
3956 wDockDetach(dock, aicon);
3958 if (collapsed) {
3959 last_dock->collapsed = 1;
3960 wDockHideIcons(last_dock);
3961 collapsed = 0;
3963 if (superfluous) {
3964 if (ghost!=None)
3965 XFreePixmap(dpy, ghost);
3966 XSetWindowBackground(dpy, scr->dock_shadow, scr->white_pixel);
3968 if (omnipresent) {
3969 int i;
3970 for (i=0; i<scr->workspace_count; i++) {
3971 if (i == scr->current_workspace)
3972 continue;
3973 wDockHideIcons(scr->workspaces[i]->clip);
3977 #ifdef DEBUG
3978 puts("End icon move");
3979 #endif
3980 return;
3986 static int
3987 getClipButton(int px, int py)
3989 int pt = (CLIP_BUTTON_SIZE+2)*ICON_SIZE/64;
3991 if (px < 0 || py < 0 || px >= ICON_SIZE || py >= ICON_SIZE)
3992 return CLIP_IDLE;
3994 if (py <= pt-((int)ICON_SIZE-1-px))
3995 return CLIP_FORWARD;
3996 else if (px <= pt-((int)ICON_SIZE-1-py))
3997 return CLIP_REWIND;
3999 return CLIP_IDLE;
4003 static void
4004 handleClipChangeWorkspace(WScreen *scr, XEvent *event)
4006 XEvent ev;
4007 int done, direction, new_ws;
4008 int new_dir;
4009 WDock *clip = scr->clip_icon->dock;
4011 direction = getClipButton(event->xbutton.x, event->xbutton.y);
4013 clip->lclip_button_pushed = direction==CLIP_REWIND;
4014 clip->rclip_button_pushed = direction==CLIP_FORWARD;
4016 wClipIconPaint(scr->clip_icon);
4017 done = 0;
4018 while(!done) {
4019 WMMaskEvent(dpy, ExposureMask|ButtonMotionMask|ButtonReleaseMask
4020 |ButtonPressMask, &ev);
4021 switch (ev.type) {
4022 case Expose:
4023 WMHandleEvent(&ev);
4024 break;
4026 case MotionNotify:
4027 new_dir = getClipButton(ev.xmotion.x, ev.xmotion.y);
4028 if (new_dir != direction) {
4029 direction = new_dir;
4030 clip->lclip_button_pushed = direction==CLIP_REWIND;
4031 clip->rclip_button_pushed = direction==CLIP_FORWARD;
4032 wClipIconPaint(scr->clip_icon);
4034 break;
4036 case ButtonPress:
4037 break;
4039 case ButtonRelease:
4040 if (ev.xbutton.button == event->xbutton.button)
4041 done = 1;
4045 clip->lclip_button_pushed = 0;
4046 clip->rclip_button_pushed = 0;
4048 new_ws = wPreferences.ws_advance || (event->xbutton.state & ControlMask);
4050 if (direction == CLIP_FORWARD) {
4051 if (scr->current_workspace < scr->workspace_count-1)
4052 wWorkspaceChange(scr, scr->current_workspace+1);
4053 else if (new_ws && scr->current_workspace < MAX_WORKSPACES-1)
4054 wWorkspaceChange(scr, scr->current_workspace+1);
4055 else if (wPreferences.ws_cycle)
4056 wWorkspaceChange(scr, 0);
4058 else if (direction == CLIP_REWIND) {
4059 if (scr->current_workspace > 0)
4060 wWorkspaceChange(scr, scr->current_workspace-1);
4061 else if (scr->current_workspace==0 && wPreferences.ws_cycle)
4062 wWorkspaceChange(scr, scr->workspace_count-1);
4065 wClipIconPaint(scr->clip_icon);
4069 static void
4070 iconMouseDown(WObjDescriptor *desc, XEvent *event)
4072 WAppIcon *aicon = desc->parent;
4073 WDock *dock = aicon->dock;
4074 WScreen *scr = aicon->icon->core->screen_ptr;
4076 if (aicon->editing || WCHECK_STATE(WSTATE_MODAL))
4077 return;
4079 scr->last_dock = dock;
4081 if (dock->menu->flags.mapped)
4082 wMenuUnmap(dock->menu);
4084 if (IsDoubleClick(scr, event)) {
4085 /* double-click was not in the main clip icon */
4086 if (dock->type != WM_CLIP || aicon->xindex!=0 || aicon->yindex!=0
4087 || getClipButton(event->xbutton.x, event->xbutton.y)==CLIP_IDLE) {
4088 iconDblClick(desc, event);
4089 return;
4093 if (dock->type == WM_CLIP && scr->flags.clip_balloon_mapped) {
4094 XUnmapWindow(dpy, scr->clip_balloon);
4095 scr->flags.clip_balloon_mapped = 0;
4098 #ifdef DEBUG
4099 puts("handling dock");
4100 #endif
4101 if (event->xbutton.button == Button1) {
4102 if (event->xbutton.state & MOD_MASK)
4103 wDockLower(dock);
4104 else
4105 wDockRaise(dock);
4107 if ((event->xbutton.state & ShiftMask) && aicon!=scr->clip_icon &&
4108 dock->type!=WM_DOCK) {
4109 wIconSelect(aicon->icon);
4110 return;
4113 if (aicon->yindex==0 && aicon->xindex==0) {
4114 if (getClipButton(event->xbutton.x, event->xbutton.y)!=CLIP_IDLE
4115 && dock->type==WM_CLIP)
4116 handleClipChangeWorkspace(scr, event);
4117 else
4118 handleDockMove(dock, aicon, event);
4119 } else
4120 handleIconMove(dock, aicon, event);
4122 } else if (event->xbutton.button==Button2 && dock->type==WM_CLIP &&
4123 aicon==scr->clip_icon) {
4124 openClipWorkspaceMenu(scr, event->xbutton.x_root+2,
4125 event->xbutton.y_root+2);
4126 if (scr->clip_ws_menu) {
4127 WMenu *menu;
4128 menu = scr->clip_ws_menu;
4129 desc = &menu->menu->descriptor;
4131 event->xany.send_event = True;
4132 (*desc->handle_mousedown)(desc, event);
4134 } else if (event->xbutton.button==Button2 && dock->type==WM_CLIP &&
4135 (event->xbutton.state & ShiftMask) && aicon!=scr->clip_icon) {
4136 wClipMakeIconOmnipresent(aicon, !aicon->omnipresent);
4137 } else if (event->xbutton.button == Button3) {
4138 if (event->xbutton.send_event &&
4139 XGrabPointer(dpy, aicon->icon->core->window, True, ButtonMotionMask
4140 |ButtonReleaseMask|ButtonPressMask, GrabModeAsync,
4141 GrabModeAsync, None, None, CurrentTime) !=GrabSuccess) {
4142 wwarning("pointer grab failed for dockicon menu");
4143 return;
4146 openDockMenu(dock, aicon, event);
4151 static void
4152 showClipBalloon(WDock *dock, int workspace)
4154 int w, h;
4155 int x, y;
4156 WScreen *scr = dock->screen_ptr;
4157 char *text;
4158 Window stack[2];
4160 scr->flags.clip_balloon_mapped = 1;
4161 XMapWindow(dpy, scr->clip_balloon);
4163 text = scr->workspaces[workspace]->name;
4165 w = WMWidthOfString(scr->clip_title_font, text, strlen(text));
4167 h = WMFontHeight(scr->clip_title_font);
4168 XResizeWindow(dpy, scr->clip_balloon, w, h);
4170 x = dock->x_pos + CLIP_BUTTON_SIZE*ICON_SIZE/64;
4171 y = dock->y_pos + ICON_SIZE - WMFontHeight(scr->clip_title_font) - 3;
4173 if (x+w > scr->scr_width) {
4174 x = scr->scr_width - w;
4175 if (dock->y_pos + ICON_SIZE + h > scr->scr_height)
4176 y = dock->y_pos - h - 1;
4177 else
4178 y = dock->y_pos + ICON_SIZE;
4179 XRaiseWindow(dpy, scr->clip_balloon);
4180 } else {
4181 stack[0] = scr->clip_icon->icon->core->window;
4182 stack[1] = scr->clip_balloon;
4183 XRestackWindows(dpy, stack, 2);
4185 XMoveWindow(dpy, scr->clip_balloon, x, y);
4186 XSetForeground(dpy, scr->clip_title_gc,
4187 scr->clip_title_pixel[CLIP_NORMAL]);
4188 XClearWindow(dpy, scr->clip_balloon);
4189 WMDrawString(scr->wmscreen, scr->clip_balloon, scr->clip_title_gc,
4190 scr->clip_title_font, 0, 0, text, strlen(text));
4194 static void
4195 clipEnterNotify(WObjDescriptor *desc, XEvent *event)
4197 WAppIcon *btn = (WAppIcon*)desc->parent;
4198 WDock *dock;
4199 WScreen *scr;
4201 assert(event->type==EnterNotify);
4203 if(desc->parent_type!=WCLASS_DOCK_ICON)
4204 return;
4206 scr = btn->icon->core->screen_ptr;
4207 if (!btn->omnipresent)
4208 dock = btn->dock;
4209 else
4210 dock = scr->workspaces[scr->current_workspace]->clip;
4212 if (!dock || dock->type!=WM_CLIP)
4213 return;
4215 /* The auto raise/lower code */
4216 if (dock->auto_lower_magic) {
4217 WMDeleteTimerHandler(dock->auto_lower_magic);
4218 dock->auto_lower_magic = NULL;
4220 if (dock->auto_raise_lower && !dock->auto_raise_magic) {
4221 dock->auto_raise_magic = WMAddTimerHandler(AUTO_RAISE_DELAY,
4222 clipAutoRaise,
4223 (void *)dock);
4226 /* The auto expand/collapse code */
4227 if (dock->auto_collapse_magic) {
4228 WMDeleteTimerHandler(dock->auto_collapse_magic);
4229 dock->auto_collapse_magic = NULL;
4231 if (dock->auto_collapse && !dock->auto_expand_magic) {
4232 dock->auto_expand_magic = WMAddTimerHandler(AUTO_EXPAND_DELAY,
4233 clipAutoExpand,
4234 (void *)dock);
4237 if (btn->xindex == 0 && btn->yindex == 0)
4238 showClipBalloon(dock, dock->screen_ptr->current_workspace);
4239 else {
4240 if (dock->screen_ptr->flags.clip_balloon_mapped) {
4241 XUnmapWindow(dpy, dock->screen_ptr->clip_balloon);
4242 dock->screen_ptr->flags.clip_balloon_mapped = 0;
4248 static void
4249 clipLeave(WDock *dock)
4251 XEvent event;
4252 WObjDescriptor *desc = NULL;
4254 if (!dock || dock->type!=WM_CLIP)
4255 return;
4257 if (XCheckTypedEvent(dpy, EnterNotify, &event)!=False) {
4258 if (XFindContext(dpy, event.xcrossing.window, wWinContext,
4259 (XPointer *)&desc)!=XCNOENT
4260 && desc && desc->parent_type==WCLASS_DOCK_ICON
4261 && ((WAppIcon*)desc->parent)->dock
4262 && ((WAppIcon*)desc->parent)->dock->type==WM_CLIP) {
4263 /* We didn't left the Clip yet */
4264 XPutBackEvent(dpy, &event);
4265 return;
4268 XPutBackEvent(dpy, &event);
4269 } else {
4270 /* We entered a withdrawn window, so we're still in Clip */
4271 return;
4274 if (dock->auto_raise_magic) {
4275 WMDeleteTimerHandler(dock->auto_raise_magic);
4276 dock->auto_raise_magic = NULL;
4278 if (dock->auto_raise_lower && !dock->auto_lower_magic) {
4279 dock->auto_lower_magic = WMAddTimerHandler(AUTO_LOWER_DELAY,
4280 clipAutoLower,
4281 (void *)dock);
4284 if (dock->auto_expand_magic) {
4285 WMDeleteTimerHandler(dock->auto_expand_magic);
4286 dock->auto_expand_magic = NULL;
4288 if (dock->auto_collapse && !dock->auto_collapse_magic) {
4289 dock->auto_collapse_magic = WMAddTimerHandler(AUTO_COLLAPSE_DELAY,
4290 clipAutoCollapse,
4291 (void *)dock);
4296 static void
4297 clipLeaveNotify(WObjDescriptor *desc, XEvent *event)
4299 WAppIcon *btn = (WAppIcon*)desc->parent;
4301 assert(event->type==LeaveNotify);
4303 if(desc->parent_type!=WCLASS_DOCK_ICON)
4304 return;
4306 clipLeave(btn->dock);
4310 static void
4311 clipAutoCollapse(void *cdata)
4313 WDock *dock = (WDock *)cdata;
4315 if (dock->type!=WM_CLIP)
4316 return;
4318 if (dock->auto_collapse) {
4319 dock->collapsed = 1;
4320 wDockHideIcons(dock);
4322 dock->auto_collapse_magic = NULL;
4326 static void
4327 clipAutoExpand(void *cdata)
4329 WDock *dock = (WDock *)cdata;
4331 if (dock->type!=WM_CLIP)
4332 return;
4334 if (dock->auto_collapse) {
4335 dock->collapsed = 0;
4336 wDockShowIcons(dock);
4338 dock->auto_expand_magic = NULL;
4342 static void
4343 clipAutoLower(void *cdata)
4345 WDock *dock = (WDock *)cdata;
4347 if (dock->type!=WM_CLIP)
4348 return;
4350 if (dock->auto_raise_lower)
4351 wDockLower(dock);
4353 dock->auto_lower_magic = NULL;
4357 static void
4358 clipAutoRaise(void *cdata)
4360 WDock *dock = (WDock *)cdata;
4362 if (dock->type!=WM_CLIP)
4363 return;
4365 if (dock->auto_raise_lower)
4366 wDockRaise(dock);
4368 if (dock->screen_ptr->flags.clip_balloon_mapped) {
4369 showClipBalloon(dock, dock->screen_ptr->current_workspace);
4372 dock->auto_raise_magic = NULL;
4376 static Bool
4377 iconCanBeOmnipresent(WAppIcon *aicon)
4379 WScreen *scr = aicon->icon->core->screen_ptr;
4380 WDock *clip;
4381 WAppIcon *btn;
4382 int i, j;
4384 for (i=0; i<scr->workspace_count; i++) {
4385 clip = scr->workspaces[i]->clip;
4387 if (clip == aicon->dock)
4388 continue;
4390 if (clip->icon_count + scr->global_icon_count >= clip->max_icons)
4391 return False; /* Clip is full in some workspace */
4393 for (j=0; j<clip->max_icons; j++) {
4394 btn = clip->icon_array[j];
4395 if(btn && btn->xindex==aicon->xindex && btn->yindex==aicon->yindex)
4396 return False;
4400 return True;
4405 wClipMakeIconOmnipresent(WAppIcon *aicon, int omnipresent)
4407 WScreen *scr = aicon->icon->core->screen_ptr;
4408 WAppIconChain *new_entry, *tmp, *tmp1;
4409 int status = WO_SUCCESS;
4411 if ((scr->dock && aicon->dock==scr->dock) || aicon==scr->clip_icon) {
4412 return WO_NOT_APPLICABLE;
4415 if (aicon->omnipresent == omnipresent)
4416 return WO_SUCCESS;
4418 if (omnipresent) {
4419 if (iconCanBeOmnipresent(aicon)) {
4420 aicon->omnipresent = 1;
4421 new_entry = wmalloc(sizeof(WAppIconChain));
4422 new_entry->aicon = aicon;
4423 new_entry->next = scr->global_icons;
4424 scr->global_icons = new_entry;
4425 scr->global_icon_count++;
4426 } else {
4427 aicon->omnipresent = 0;
4428 status = WO_FAILED;
4430 } else {
4431 aicon->omnipresent = 0;
4432 if (aicon == scr->global_icons->aicon) {
4433 tmp = scr->global_icons->next;
4434 wfree(scr->global_icons);
4435 scr->global_icons = tmp;
4436 scr->global_icon_count--;
4437 } else {
4438 tmp = scr->global_icons;
4439 while (tmp->next) {
4440 if (tmp->next->aicon == aicon) {
4441 tmp1 = tmp->next->next;
4442 wfree(tmp->next);
4443 tmp->next = tmp1;
4444 scr->global_icon_count--;
4445 break;
4447 tmp = tmp->next;
4452 wAppIconPaint(aicon);
4454 return status;