Updated korean po files.
[wmaker-crm.git] / src / dock.c
blobe12ab2f1ec5b5186b1369826c22b6122db0a035d
1 /* dock.c- built-in Dock module for WindowMaker
3 * Window Maker window manager
5 * Copyright (c) 1997, 1998 Alfredo K. Kojima
6 * Copyright (c) 1998, 1999 Dan Pascu
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
21 * USA.
25 #include "wconfig.h"
27 #include <X11/Xlib.h>
28 #include <X11/Xutil.h>
29 #include <stdlib.h>
30 #include <string.h>
31 #include <unistd.h>
32 #include <math.h>
33 #include <limits.h>
35 #ifndef PATH_MAX
36 #define PATH_MAX DEFAULT_PATH_MAX
37 #endif
39 #include "WindowMaker.h"
40 #include "wcore.h"
41 #include "window.h"
42 #include "icon.h"
43 #include "appicon.h"
44 #include "actions.h"
45 #include "stacking.h"
46 #include "dock.h"
47 #include "dialog.h"
48 #include "funcs.h"
49 #include "properties.h"
50 #include "menu.h"
51 #include "client.h"
52 #include "defaults.h"
53 #include "workspace.h"
54 #include "framewin.h"
55 #include "superfluous.h"
56 #include "wmsound.h"
59 #include <proplist.h>
64 /**** Local variables ****/
65 #define CLIP_REWIND 1
66 #define CLIP_IDLE 0
67 #define CLIP_FORWARD 2
70 /**** Global variables ****/
72 /* in dockedapp.c */
73 extern void DestroyDockAppSettingsPanel();
75 extern void ShowDockAppSettingsPanel(WAppIcon *aicon);
78 extern XContext wWinContext;
80 extern Cursor wCursor[WCUR_LAST];
82 extern WPreferences wPreferences;
84 extern XContext wWinContext;
86 #ifdef OFFIX_DND
87 extern Atom _XA_DND_PROTOCOL;
88 #endif
91 #define MOD_MASK wPreferences.modifier_mask
93 extern void appIconMouseDown(WObjDescriptor *desc, XEvent *event);
95 #define ICON_SIZE wPreferences.icon_size
98 /***** Local variables ****/
100 static proplist_t dCommand=NULL;
101 #ifdef OFFIX_DND
102 static proplist_t dDropCommand=NULL;
103 #endif
104 static proplist_t dAutoLaunch, dLock;
105 static proplist_t dName, dForced, dBuggyApplication, dYes, dNo;
106 static proplist_t dHost, dDock, dClip;
107 static proplist_t dAutoAttractIcons;
109 static proplist_t dPosition, dApplications, dLowered, dCollapsed, dAutoCollapse;
111 static proplist_t dAutoRaiseLower, dOmnipresent;
113 static void dockIconPaint(WAppIcon *btn);
115 static void iconMouseDown(WObjDescriptor *desc, XEvent *event);
117 static pid_t execCommand(WAppIcon *btn, char *command, WSavedState *state);
119 static void trackDeadProcess(pid_t pid, unsigned char status, WDock *dock);
121 static int getClipButton(int px, int py);
123 static void toggleLowered(WDock *dock);
125 static void toggleCollapsed(WDock *dock);
127 static void clipIconExpose(WObjDescriptor *desc, XEvent *event);
129 static void clipLeave(WDock *dock);
131 static void handleClipChangeWorkspace(WScreen *scr, XEvent *event);
133 Bool moveIconBetweenDocks(WDock *src, WDock *dest, WAppIcon *icon, int x, int y);
135 static void clipEnterNotify(WObjDescriptor *desc, XEvent *event);
136 static void clipLeaveNotify(WObjDescriptor *desc, XEvent *event);
137 static void clipAutoCollapse(void *cdata);
138 static void clipAutoExpand(void *cdata);
139 static void launchDockedApplication(WAppIcon *btn);
141 static void clipAutoLower(void *cdata);
142 static void clipAutoRaise(void *cdata);
144 static void showClipBalloon(WDock *dock, int workspace);
146 #ifdef OFFIX_DND
148 #define DndNotDnd -1
149 #define DndUnknown 0
150 #define DndRawData 1
151 #define DndFile 2
152 #define DndFiles 3
153 #define DndText 4
154 #define DndDir 5
155 #define DndLink 6
156 #define DndExe 7
158 #define DndEND 8
160 #endif /* OFFIX_DND */
164 static void
165 make_keys()
167 if (dCommand!=NULL)
168 return;
170 dCommand = PLRetain(PLMakeString("Command"));
171 #ifdef OFFIX_DND
172 dDropCommand = PLRetain(PLMakeString("DropCommand"));
173 #endif
174 dLock = PLRetain(PLMakeString("Lock"));
175 dAutoLaunch = PLRetain(PLMakeString("AutoLaunch"));
176 dName = PLRetain(PLMakeString("Name"));
177 dForced = PLRetain(PLMakeString("Forced"));
178 dBuggyApplication = PLRetain(PLMakeString("BuggyApplication"));
179 dYes = PLRetain(PLMakeString("Yes"));
180 dNo = PLRetain(PLMakeString("No"));
181 dHost = PLRetain(PLMakeString("Host"));
183 dPosition = PLMakeString("Position");
184 dApplications = PLMakeString("Applications");
185 dLowered = PLMakeString("Lowered");
186 dCollapsed = PLMakeString("Collapsed");
187 dAutoCollapse = PLMakeString("AutoCollapse");
188 dAutoRaiseLower = PLMakeString("AutoRaiseLower");
189 dAutoAttractIcons = PLMakeString("AutoAttractIcons");
191 dOmnipresent = PLMakeString("Omnipresent");
193 dDock = PLMakeString("Dock");
194 dClip = PLMakeString("Clip");
199 static void
200 renameCallback(WMenu *menu, WMenuEntry *entry)
202 WDock *dock = entry->clientdata;
203 char buffer[128];
204 int wspace;
205 char *name;
207 assert(entry->clientdata!=NULL);
209 wspace = dock->screen_ptr->current_workspace;
211 name = wstrdup(dock->screen_ptr->workspaces[wspace]->name);
213 sprintf(buffer, _("Type the name for workspace %i:"), wspace+1);
214 if (wInputDialog(dock->screen_ptr, _("Rename Workspace"), buffer,
215 &name)) {
216 wWorkspaceRename(dock->screen_ptr, wspace, name);
218 if (name) {
219 free(name);
224 static void
225 toggleLoweredCallback(WMenu *menu, WMenuEntry *entry)
227 assert(entry->clientdata!=NULL);
229 toggleLowered(entry->clientdata);
231 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 = wstrappend(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);
288 #endif /* !REDUCE_APPICONS */
290 icon->editing = 0;
292 WCHANGE_STATE(WSTATE_NORMAL);
296 /* TODO: replace this function with a member of the dock struct */
297 static int
298 numberOfSelectedIcons(WDock *dock)
300 WAppIcon *aicon;
301 int i, n;
303 n = 0;
304 for (i=1; i<dock->max_icons; i++) {
305 aicon = dock->icon_array[i];
306 if (aicon && aicon->icon->selected) {
307 n++;
311 return n;
315 static WMBag*
316 getSelected(WDock *dock)
318 WMBag *ret = WMCreateBag(8);
319 WAppIcon *btn;
320 int i;
322 for (i=1; i<dock->max_icons; i++) {
323 btn = dock->icon_array[i];
324 if (btn && btn->icon->selected) {
325 WMPutInBag(ret, btn);
329 return ret;
333 static void
334 paintClipButtons(WAppIcon *clipIcon, Bool lpushed, Bool rpushed)
336 Window win = clipIcon->icon->core->window;
337 WScreen *scr = clipIcon->icon->core->screen_ptr;
338 XPoint p[4];
339 int pt = CLIP_BUTTON_SIZE*ICON_SIZE/64;
340 int tp = ICON_SIZE - pt;
341 int as = pt - 15; /* 15 = 5+5+5 */
342 GC gc = scr->clip_title_gc;
343 #ifdef GRADIENT_CLIP_ARROW
344 Bool collapsed = clipIcon->dock->collapsed;
345 #endif
347 if (rpushed) {
348 p[0].x = tp+1;
349 p[0].y = 1;
350 p[1].x = ICON_SIZE-2;
351 p[1].y = 1;
352 p[2].x = ICON_SIZE-2;
353 p[2].y = pt-1;
354 } else if (lpushed) {
355 p[0].x = 1;
356 p[0].y = tp;
357 p[1].x = pt;
358 p[1].y = ICON_SIZE-2;
359 p[2].x = 1;
360 p[2].y = ICON_SIZE-2;
362 if (lpushed || rpushed) {
363 XSetForeground(dpy, scr->draw_gc, scr->white_pixel);
364 XFillPolygon(dpy, win, scr->draw_gc, p, 3, Convex, CoordModeOrigin);
365 XSetForeground(dpy, scr->draw_gc, scr->black_pixel);
367 #ifdef GRADIENT_CLIP_ARROW
368 if (!collapsed) {
369 XSetFillStyle(dpy, scr->copy_gc, FillTiled);
370 XSetTile(dpy, scr->copy_gc, scr->clip_arrow_gradient);
371 XSetClipMask(dpy, scr->copy_gc, None);
372 gc = scr->copy_gc;
374 #endif /* GRADIENT_CLIP_ARROW */
376 /* top right arrow */
377 p[0].x = p[3].x = ICON_SIZE-5-as;
378 p[0].y = p[3].y = 5;
379 p[1].x = ICON_SIZE-6;
380 p[1].y = 5;
381 p[2].x = ICON_SIZE-6;
382 p[2].y = 4+as;
383 if (rpushed) {
384 XFillPolygon(dpy, win, scr->draw_gc, p, 3, Convex, CoordModeOrigin);
385 XDrawLines(dpy, win, scr->draw_gc, p, 4, CoordModeOrigin);
386 } else {
387 #ifdef GRADIENT_CLIP_ARROW
388 if (!collapsed)
389 XSetTSOrigin(dpy, gc, ICON_SIZE-6-as, 5);
390 #endif
391 XFillPolygon(dpy, win, gc, p,3,Convex,CoordModeOrigin);
392 XDrawLines(dpy, win, gc, p,4,CoordModeOrigin);
395 /* bottom left arrow */
396 p[0].x = p[3].x = 5;
397 p[0].y = p[3].y = ICON_SIZE-5-as;
398 p[1].x = 5;
399 p[1].y = ICON_SIZE-6;
400 p[2].x = 4+as;
401 p[2].y = ICON_SIZE-6;
402 if (lpushed) {
403 XFillPolygon(dpy, win, scr->draw_gc, p, 3, Convex, CoordModeOrigin);
404 XDrawLines(dpy, win, scr->draw_gc, p, 4, CoordModeOrigin);
405 } else {
406 #ifdef GRADIENT_CLIP_ARROW
407 if (!collapsed)
408 XSetTSOrigin(dpy, gc, 5, ICON_SIZE-6-as);
409 #endif
410 XFillPolygon(dpy, win, gc, p,3,Convex,CoordModeOrigin);
411 XDrawLines(dpy, win, gc, p,4,CoordModeOrigin);
413 #ifdef GRADIENT_CLIP_ARROW
414 if (!collapsed)
415 XSetFillStyle(dpy, scr->copy_gc, FillSolid);
416 #endif
420 RImage*
421 wClipMakeTile(WScreen *scr, RImage *normalTile)
423 RImage *tile = RCloneImage(normalTile);
424 RColor black;
425 RColor dark;
426 RColor light;
427 int pt, tp;
428 int as;
430 pt = CLIP_BUTTON_SIZE*wPreferences.icon_size/64;
431 tp = wPreferences.icon_size-1 - pt;
432 as = pt - 15;
434 black.alpha = 255;
435 black.red = black.green = black.blue = 0;
437 dark.alpha = 0;
438 dark.red = dark.green = dark.blue = 60;
440 light.alpha = 0;
441 light.red = light.green = light.blue = 80;
444 /* top right */
445 ROperateLine(tile, RSubtractOperation, tp, 0, wPreferences.icon_size-2,
446 pt-1, &dark);
447 RDrawLine(tile, tp-1, 0, wPreferences.icon_size-1, pt+1, &black);
448 ROperateLine(tile, RAddOperation, tp, 2, wPreferences.icon_size-3,
449 pt, &light);
451 /* arrow bevel */
452 ROperateLine(tile, RSubtractOperation, ICON_SIZE - 7 - as, 4,
453 ICON_SIZE - 5, 4, &dark);
454 ROperateLine(tile, RSubtractOperation, ICON_SIZE - 6 - as, 5,
455 ICON_SIZE - 5, 6 + as, &dark);
456 ROperateLine(tile, RAddOperation, ICON_SIZE - 5, 4, ICON_SIZE - 5, 6 + as,
457 &light);
459 /* bottom left */
460 ROperateLine(tile, RAddOperation, 2, tp+2, pt-2,
461 wPreferences.icon_size-3, &dark);
462 RDrawLine(tile, 0, tp-1, pt+1, wPreferences.icon_size-1, &black);
463 ROperateLine(tile, RSubtractOperation, 0, tp-2, pt+1,
464 wPreferences.icon_size-2, &light);
466 /* arrow bevel */
467 ROperateLine(tile, RSubtractOperation, 4, ICON_SIZE - 7 - as, 4,
468 ICON_SIZE - 5, &dark);
469 ROperateLine(tile, RSubtractOperation, 5, ICON_SIZE - 6 - as,
470 6 + as, ICON_SIZE - 5, &dark);
471 ROperateLine(tile, RAddOperation, 4, ICON_SIZE - 5, 6 + as, ICON_SIZE - 5,
472 &light);
474 return tile;
478 static void
479 omnipresentCallback(WMenu *menu, WMenuEntry *entry)
481 WAppIcon *clickedIcon = entry->clientdata;
482 WAppIcon *aicon;
483 WDock *dock;
484 WMBag *selectedIcons;
485 int failed;
486 int i;
488 assert(entry->clientdata!=NULL);
490 dock = clickedIcon->dock;
492 selectedIcons = getSelected(dock);
494 if (!WMGetBagItemCount(selectedIcons))
495 WMPutInBag(selectedIcons, clickedIcon);
497 failed = 0;
498 for (i = 0; i < WMGetBagItemCount(selectedIcons); i++) {
499 aicon = WMGetFromBag(selectedIcons, i);
501 if (wClipMakeIconOmnipresent(aicon, !aicon->omnipresent) == WO_FAILED)
502 failed++;
503 else if (aicon->icon->selected)
504 wIconSelect(aicon->icon);
506 WMFreeBag(selectedIcons);
508 if (failed > 1) {
509 wMessageDialog(dock->screen_ptr, _("Warning"),
510 _("Some icons cannot be made omnipresent. "
511 "Please make sure that no other icon is "
512 "docked in the same positions on the other "
513 "workspaces and the Clip is not full in "
514 "some workspace."),
515 _("OK"), NULL, NULL);
516 } else if (failed == 1) {
517 wMessageDialog(dock->screen_ptr, _("Warning"),
518 _("Icon cannot be made omnipresent. "
519 "Please make sure that no other icon is "
520 "docked in the same position on the other "
521 "workspaces and the Clip is not full in "
522 "some workspace."),
523 _("OK"), NULL, NULL);
528 static void
529 removeIconsCallback(WMenu *menu, WMenuEntry *entry)
531 WAppIcon *clickedIcon = (WAppIcon*)entry->clientdata;
532 WDock *dock;
533 WAppIcon *aicon;
534 WMBag *selectedIcons;
535 int keepit;
536 int i;
538 assert(clickedIcon!=NULL);
540 dock = clickedIcon->dock;
542 selectedIcons = getSelected(dock);
544 if (WMGetBagItemCount(selectedIcons)) {
545 if (wMessageDialog(dock->screen_ptr, _("Workspace Clip"),
546 _("All selected icons will be removed!"),
547 _("OK"), _("Cancel"), NULL)!=WAPRDefault) {
548 WMFreeBag(selectedIcons);
549 return;
551 } else {
552 if (clickedIcon->xindex==0 && clickedIcon->yindex==0) {
553 WMFreeBag(selectedIcons);
554 return;
556 WMPutInBag(selectedIcons, clickedIcon);
559 for (i = 0; i < WMGetBagItemCount(selectedIcons); i++) {
560 aicon = WMGetFromBag(selectedIcons, i);
561 keepit = aicon->running && wApplicationOf(aicon->main_window);
562 wDockDetach(dock, aicon);
563 if (keepit) {
564 PlaceIcon(dock->screen_ptr, &aicon->x_pos, &aicon->y_pos);
565 XMoveWindow(dpy, aicon->icon->core->window,
566 aicon->x_pos, aicon->y_pos);
567 if (!dock->mapped || dock->collapsed)
568 XMapWindow(dpy, aicon->icon->core->window);
571 WMFreeBag(selectedIcons);
573 if (wPreferences.auto_arrange_icons)
574 wArrangeIcons(dock->screen_ptr, True);
578 static void
579 keepIconsCallback(WMenu *menu, WMenuEntry *entry)
581 WAppIcon *clickedIcon = (WAppIcon*)entry->clientdata;
582 WDock *dock;
583 WAppIcon *aicon;
584 WMBag *selectedIcons;
585 int i;
587 assert(clickedIcon!=NULL);
588 dock = clickedIcon->dock;
590 selectedIcons = getSelected(dock);
592 if (!WMGetBagItemCount(selectedIcons)
593 && clickedIcon!=dock->screen_ptr->clip_icon) {
594 char *command = NULL;
596 if (!clickedIcon->command && !clickedIcon->editing) {
597 clickedIcon->editing = 1;
598 if (wInputDialog(dock->screen_ptr, _("Keep Icon"),
599 _("Type the command used to launch the application"),
600 &command)) {
601 if (command && (command[0]==0 ||
602 (command[0]=='-' && command[1]==0))) {
603 free(command);
604 command = NULL;
606 clickedIcon->command = command;
607 clickedIcon->editing = 0;
608 } else {
609 clickedIcon->editing = 0;
610 if (command)
611 free(command);
612 WMFreeBag(selectedIcons);
613 return;
617 WMPutInBag(selectedIcons, clickedIcon);
620 for (i = 0; i < WMGetBagItemCount(selectedIcons); i++) {
621 aicon = WMGetFromBag(selectedIcons, i);
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 free(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 free(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 free(entry->text);
1138 entry->text = _("Select All Icons");
1140 entry = wMenuAddCallback(menu, _("Keep Icon"), keepIconsCallback, NULL);
1141 free(entry->text);
1142 entry->text = _("Keep Icon");
1144 entry = wMenuAddCallback(menu, _("Move Icon To"), NULL, NULL);
1145 free(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 free(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 free(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 free(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 free(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 free(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 free(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 char *wclass, *winstance;
1539 proplist_t cmd, value;
1540 char *command;
1543 cmd = PLGetDictionaryEntry(info, dCommand);
1544 if (!cmd || !PLIsString(cmd)) {
1545 return NULL;
1548 /* parse window name */
1549 value = PLGetDictionaryEntry(info, dName);
1550 if (!value)
1551 return NULL;
1553 ParseWindowName(value, &winstance, &wclass, "dock");
1555 if (!winstance && !wclass) {
1556 return NULL;
1559 /* get commands */
1561 if (cmd)
1562 command = wstrdup(PLGetString(cmd));
1563 else
1564 command = NULL;
1566 if (!command || strcmp(command, "-")==0) {
1567 if (command)
1568 free(command);
1569 if (wclass)
1570 free(wclass);
1571 if (winstance)
1572 free(winstance);
1574 return NULL;
1577 aicon = wAppIconCreateForDock(scr, command, winstance, wclass,
1578 TILE_NORMAL);
1579 if (wclass)
1580 free(wclass);
1581 if (winstance)
1582 free(winstance);
1584 aicon->icon->core->descriptor.handle_mousedown = iconMouseDown;
1585 if (type == WM_CLIP) {
1586 aicon->icon->core->descriptor.handle_enternotify = clipEnterNotify;
1587 aicon->icon->core->descriptor.handle_leavenotify = clipLeaveNotify;
1589 aicon->icon->core->descriptor.parent_type = WCLASS_DOCK_ICON;
1590 aicon->icon->core->descriptor.parent = aicon;
1593 #ifdef OFFIX_DND
1594 cmd = PLGetDictionaryEntry(info, dDropCommand);
1595 if (cmd)
1596 aicon->dnd_command = wstrdup(PLGetString(cmd));
1597 #endif
1599 /* check auto launch */
1600 value = PLGetDictionaryEntry(info, dAutoLaunch);
1602 aicon->auto_launch = getBooleanDockValue(value, dAutoLaunch);
1604 /* check lock */
1605 value = PLGetDictionaryEntry(info, dLock);
1607 aicon->lock = getBooleanDockValue(value, dLock);
1609 /* check if it wasn't normally docked */
1610 value = PLGetDictionaryEntry(info, dForced);
1612 aicon->forced_dock = getBooleanDockValue(value, dForced);
1614 /* check if we can rely on the stuff in the app */
1615 value = PLGetDictionaryEntry(info, dBuggyApplication);
1617 aicon->buggy_app = getBooleanDockValue(value, dBuggyApplication);
1619 /* get position in the dock */
1620 value = PLGetDictionaryEntry(info, dPosition);
1621 if (value && PLIsString(value)) {
1622 if (sscanf(PLGetString(value), "%hi,%hi", &aicon->xindex,
1623 &aicon->yindex)!=2)
1624 wwarning(_("bad value in docked icon state info %s"),
1625 PLGetString(dPosition));
1627 /* check position sanity */
1628 /* incomplete section! */
1629 if (type == WM_DOCK) {
1630 aicon->xindex = 0;
1631 if (aicon->yindex < 0)
1632 wwarning(_("bad value in docked icon position %i,%i"),
1633 aicon->xindex, aicon->yindex);
1635 } else {
1636 aicon->yindex = index;
1637 aicon->xindex = 0;
1640 /* check if icon is omnipresent */
1641 value = PLGetDictionaryEntry(info, dOmnipresent);
1643 aicon->omnipresent = getBooleanDockValue(value, dOmnipresent);
1645 aicon->running = 0;
1646 aicon->docked = 1;
1648 return aicon;
1652 #define COMPLAIN(key) wwarning(_("bad value in dock state info:%s"), key)
1655 WAppIcon*
1656 wClipRestoreState(WScreen *scr, proplist_t clip_state)
1658 WAppIcon *icon;
1659 proplist_t value;
1662 icon = mainIconCreate(scr, WM_CLIP);
1664 if (!clip_state)
1665 return icon;
1666 else
1667 PLRetain(clip_state);
1669 /* restore position */
1671 value = PLGetDictionaryEntry(clip_state, dPosition);
1673 if (value) {
1674 if (!PLIsString(value))
1675 COMPLAIN("Position");
1676 else {
1677 if (sscanf(PLGetString(value), "%i,%i", &icon->x_pos,
1678 &icon->y_pos)!=2)
1679 COMPLAIN("Position");
1681 /* check position sanity */
1682 if (icon->y_pos < 0)
1683 icon->y_pos = 0;
1684 else if (icon->y_pos > scr->scr_height-ICON_SIZE)
1685 icon->y_pos = scr->scr_height-ICON_SIZE;
1687 if (icon->x_pos < 0)
1688 icon->x_pos = 0;
1689 else if (icon->x_pos > scr->scr_width-ICON_SIZE)
1690 icon->x_pos = scr->scr_width-ICON_SIZE;
1694 #ifdef OFFIX_DND
1695 value = PLGetDictionaryEntry(clip_state, dDropCommand);
1696 if (value && PLIsString(value))
1697 icon->dnd_command = wstrdup(PLGetString(value));
1698 #endif
1700 PLRelease(clip_state);
1702 return icon;
1706 WDock*
1707 wDockRestoreState(WScreen *scr, proplist_t dock_state, int type)
1709 WDock *dock;
1710 proplist_t apps;
1711 proplist_t value;
1712 WAppIcon *aicon, *old_top;
1713 int count, i;
1716 dock = wDockCreate(scr, type);
1718 if (!dock_state)
1719 return dock;
1721 if (dock_state)
1722 PLRetain(dock_state);
1725 /* restore position */
1727 value = PLGetDictionaryEntry(dock_state, dPosition);
1729 if (value) {
1730 if (!PLIsString(value))
1731 COMPLAIN("Position");
1732 else {
1733 if (sscanf(PLGetString(value), "%i,%i", &dock->x_pos,
1734 &dock->y_pos)!=2)
1735 COMPLAIN("Position");
1737 /* check position sanity */
1738 if (dock->y_pos < 0)
1739 dock->y_pos = 0;
1740 else if (dock->y_pos > scr->scr_height-ICON_SIZE)
1741 dock->y_pos = scr->scr_height - ICON_SIZE;
1743 /* This is no more needed. ??? */
1744 if (type == WM_CLIP) {
1745 if (dock->x_pos < 0)
1746 dock->x_pos = 0;
1747 else if (dock->x_pos > scr->scr_width-ICON_SIZE)
1748 dock->x_pos = scr->scr_width-ICON_SIZE;
1749 } else {
1750 if (dock->x_pos >= 0) {
1751 dock->x_pos = DOCK_EXTRA_SPACE;
1752 dock->on_right_side = 0;
1753 } else {
1754 dock->x_pos = scr->scr_width - DOCK_EXTRA_SPACE - ICON_SIZE;
1755 dock->on_right_side = 1;
1761 /* restore lowered/raised state */
1763 dock->lowered = 0;
1765 value = PLGetDictionaryEntry(dock_state, dLowered);
1767 if (value) {
1768 if (!PLIsString(value))
1769 COMPLAIN("Lowered");
1770 else {
1771 if (strcasecmp(PLGetString(value), "YES")==0)
1772 dock->lowered = 1;
1777 /* restore collapsed state */
1779 dock->collapsed = 0;
1781 value = PLGetDictionaryEntry(dock_state, dCollapsed);
1783 if (value) {
1784 if (!PLIsString(value))
1785 COMPLAIN("Collapsed");
1786 else {
1787 if (strcasecmp(PLGetString(value), "YES")==0)
1788 dock->collapsed = 1;
1793 /* restore auto-collapsed state */
1795 value = PLGetDictionaryEntry(dock_state, dAutoCollapse);
1797 if (value) {
1798 if (!PLIsString(value))
1799 COMPLAIN("AutoCollapse");
1800 else {
1801 if (strcasecmp(PLGetString(value), "YES")==0) {
1802 dock->auto_collapse = 1;
1803 dock->collapsed = 1;
1809 /* restore auto-raise/lower state */
1811 value = PLGetDictionaryEntry(dock_state, dAutoRaiseLower);
1813 if (value) {
1814 if (!PLIsString(value))
1815 COMPLAIN("AutoRaiseLower");
1816 else {
1817 if (strcasecmp(PLGetString(value), "YES")==0) {
1818 dock->auto_raise_lower = 1;
1823 /* restore attract icons state */
1825 dock->attract_icons = 0;
1827 value = PLGetDictionaryEntry(dock_state, dAutoAttractIcons);
1829 if (value) {
1830 if (!PLIsString(value))
1831 COMPLAIN("AutoAttractIcons");
1832 else {
1833 if (strcasecmp(PLGetString(value), "YES")==0)
1834 dock->attract_icons = 1;
1839 /* application list */
1842 proplist_t tmp;
1843 char buffer[64];
1846 * When saving, it saves the dock state in
1847 * Applications and Applicationsnnn
1849 * When loading, it will first try Applicationsnnn.
1850 * If it does not exist, use Applications as default.
1853 sprintf(buffer, "Applications%i", scr->scr_height);
1855 tmp = PLMakeString(buffer);
1856 apps = PLGetDictionaryEntry(dock_state, tmp);
1857 PLRelease(tmp);
1859 if (!apps) {
1860 apps = PLGetDictionaryEntry(dock_state, dApplications);
1864 if (!apps) {
1865 goto finish;
1868 count = PLGetNumberOfElements(apps);
1870 if (count==0)
1871 goto finish;
1873 old_top = dock->icon_array[0];
1875 /* dock->icon_count is set to 1 when dock is created.
1876 * Since Clip is already restored, we want to keep it so for clip,
1877 * but for dock we may change the default top tile, so we set it to 0.
1879 if (type == WM_DOCK)
1880 dock->icon_count = 0;
1882 for (i=0; i<count; i++) {
1883 if (dock->icon_count >= dock->max_icons) {
1884 wwarning(_("there are too many icons stored in dock. Ignoring what doesn't fit"));
1885 break;
1888 value = PLGetArrayElement(apps, i);
1889 aicon = restore_icon_state(scr, value, type, dock->icon_count);
1891 dock->icon_array[dock->icon_count] = aicon;
1893 if (aicon) {
1894 aicon->dock = dock;
1895 aicon->x_pos = dock->x_pos + (aicon->xindex*ICON_SIZE);
1896 aicon->y_pos = dock->y_pos + (aicon->yindex*ICON_SIZE);
1898 if (dock->lowered)
1899 ChangeStackingLevel(aicon->icon->core, WMNormalLevel);
1900 else
1901 ChangeStackingLevel(aicon->icon->core, WMDockLevel);
1903 wCoreConfigure(aicon->icon->core, aicon->x_pos, aicon->y_pos,
1904 0, 0);
1906 if (!dock->collapsed)
1907 XMapWindow(dpy, aicon->icon->core->window);
1908 wRaiseFrame(aicon->icon->core);
1910 dock->icon_count++;
1911 } else if (dock->icon_count==0 && type==WM_DOCK)
1912 dock->icon_count++;
1915 /* if the first icon is not defined, use the default */
1916 if (dock->icon_array[0]==NULL) {
1917 /* update default icon */
1918 old_top->x_pos = dock->x_pos;
1919 old_top->y_pos = dock->y_pos;
1920 if (dock->lowered)
1921 ChangeStackingLevel(old_top->icon->core, WMNormalLevel);
1922 else
1923 ChangeStackingLevel(old_top->icon->core, WMDockLevel);
1924 dock->icon_array[0] = old_top;
1925 XMoveWindow(dpy, old_top->icon->core->window, dock->x_pos, dock->y_pos);
1926 /* we don't need to increment dock->icon_count here because it was
1927 * incremented in the loop above.
1929 } else if (old_top!=dock->icon_array[0]) {
1930 if (old_top == scr->clip_icon)
1931 scr->clip_icon = dock->icon_array[0];
1932 wAppIconDestroy(old_top);
1935 finish:
1936 if (dock_state)
1937 PLRelease(dock_state);
1939 return dock;
1944 void
1945 wDockLaunchWithState(WDock *dock, WAppIcon *btn, WSavedState *state)
1947 if (btn && btn->command && !btn->running && !btn->launching) {
1949 btn->drop_launch = 0;
1951 btn->pid = execCommand(btn, btn->command, state);
1953 if (btn->pid>0) {
1954 if (!btn->forced_dock && !btn->buggy_app) {
1955 btn->launching = 1;
1956 dockIconPaint(btn);
1959 } else {
1960 free(state);
1965 void
1966 wDockDoAutoLaunch(WDock *dock, int workspace)
1968 WAppIcon *btn;
1969 WSavedState *state;
1970 int i;
1972 for (i = 0; i < dock->max_icons; i++) {
1973 btn = dock->icon_array[i];
1974 if (!btn || !btn->auto_launch)
1975 continue;
1977 state = wmalloc(sizeof(WSavedState));
1978 memset(state, 0, sizeof(WSavedState));
1979 state->workspace = workspace;
1980 /* TODO: this is klugy and is very difficult to understand
1981 * what's going on. Try to clean up */
1982 wDockLaunchWithState(dock, btn, state);
1986 #ifdef REDUCE_APPICONS
1987 void
1988 wDockSimulateLaunch(WDock *dock, WAppIcon *btn)
1990 if ((btn == NULL) || (dock == NULL))
1991 return;
1993 if (!btn->running) {
1994 if ((btn->icon->owner == NULL) && (btn->applist))
1995 btn->icon->owner = btn->applist->wapp->main_window_desc;
1996 if (!btn->forced_dock)
1997 btn->launching = 1;
1998 dockIconPaint(btn);
1999 wusleep(5000);
2002 #endif
2004 #ifdef OFFIX_DND
2005 static WDock*
2006 findDock(WScreen *scr, XEvent *event, int *icon_pos)
2008 WDock *dock;
2009 int i;
2011 *icon_pos = -1;
2012 if ((dock = scr->dock)!=NULL) {
2013 for (i=0; i<dock->max_icons; i++) {
2014 if (dock->icon_array[i]
2015 && dock->icon_array[i]->icon->core->window==event->xclient.window) {
2016 *icon_pos = i;
2017 break;
2021 if (*icon_pos<0 && (dock = scr->workspaces[scr->current_workspace]->clip)!=NULL) {
2022 for (i=0; i<dock->max_icons; i++) {
2023 if (dock->icon_array[i]
2024 && dock->icon_array[i]->icon->core->window==event->xclient.window) {
2025 *icon_pos = i;
2026 break;
2030 if(*icon_pos>=0)
2031 return dock;
2032 return NULL;
2037 wDockReceiveDNDDrop(WScreen *scr, XEvent *event)
2039 WDock *dock;
2040 WAppIcon *btn;
2041 int icon_pos;
2043 dock = findDock(scr, event, &icon_pos);
2044 if (!dock)
2045 return False;
2048 * Return True if the drop was on an application icon window.
2049 * In this case, let the ClientMessage handler redirect the
2050 * message to the app.
2052 if (dock->icon_array[icon_pos]->icon->icon_win!=None)
2053 return True;
2055 if (dock->icon_array[icon_pos]->dnd_command!=NULL) {
2056 scr->flags.dnd_data_convertion_status = 0;
2058 btn = dock->icon_array[icon_pos];
2060 if (!btn->forced_dock) {
2061 btn->relaunching = btn->running;
2062 btn->running = 1;
2064 if (btn->wm_instance || btn->wm_class) {
2065 WWindowAttributes attr;
2066 memset(&attr, 0, sizeof(WWindowAttributes));
2067 wDefaultFillAttributes(btn->icon->core->screen_ptr,
2068 btn->wm_instance,
2069 btn->wm_class, &attr, NULL, True);
2071 if (!attr.no_appicon)
2072 btn->launching = 1;
2073 else
2074 btn->running = 0;
2077 btn->drop_launch = 1;
2078 scr->last_dock = dock;
2079 btn->pid = execCommand(btn, btn->dnd_command, NULL);
2080 if (btn->pid>0) {
2081 dockIconPaint(btn);
2082 } else {
2083 btn->launching = 0;
2084 if (!btn->relaunching) {
2085 btn->running = 0;
2089 return False;
2091 #endif /* OFFIX_DND */
2095 Bool
2096 wDockAttachIcon(WDock *dock, WAppIcon *icon, int x, int y)
2098 WWindow *wwin;
2099 char **argv;
2100 int argc;
2101 int index;
2103 wwin = icon->icon->owner;
2104 if (icon->command==NULL) {
2105 icon->editing = 0;
2106 if (XGetCommand(dpy, wwin->client_win, &argv, &argc) && argc>0) {
2108 icon->command = FlattenStringList(argv, argc);
2109 XFreeStringList(argv);
2110 } else {
2111 char *command=NULL;
2113 /* icon->forced_dock = 1;*/
2114 if (dock->type!=WM_CLIP || !icon->attracted) {
2115 icon->editing = 1;
2116 if (wInputDialog(dock->screen_ptr, _("Dock Icon"),
2117 _("Type the command used to launch the application"),
2118 &command)) {
2119 if (command && (command[0]==0 ||
2120 (command[0]=='-' && command[1]==0))) {
2121 free(command);
2122 command = NULL;
2124 icon->command = command;
2125 icon->editing = 0;
2126 } else {
2127 icon->editing = 0;
2128 if (command)
2129 free(command);
2130 /* If the target is the dock, reject the icon. If
2131 * the target is the clip, make it an attracted icon
2133 if (dock->type==WM_CLIP) {
2134 icon->attracted = 1;
2135 if (!icon->icon->shadowed) {
2136 icon->icon->shadowed = 1;
2137 icon->icon->force_paint = 1;
2139 } else {
2140 return False;
2145 } else {
2146 icon->editing = 0;
2149 for (index=1; index<dock->max_icons; index++)
2150 if (dock->icon_array[index] == NULL)
2151 break;
2152 /* if (index == dock->max_icons)
2153 return; */
2155 assert(index < dock->max_icons);
2157 dock->icon_array[index] = icon;
2158 icon->yindex = y;
2159 icon->xindex = x;
2161 icon->omnipresent = 0;
2163 icon->x_pos = dock->x_pos + x*ICON_SIZE;
2164 icon->y_pos = dock->y_pos + y*ICON_SIZE;
2166 dock->icon_count++;
2168 icon->running = 1;
2169 icon->launching = 0;
2170 icon->docked = 1;
2171 icon->dock = dock;
2172 icon->icon->core->descriptor.handle_mousedown = iconMouseDown;
2173 if (dock->type == WM_CLIP) {
2174 icon->icon->core->descriptor.handle_enternotify = clipEnterNotify;
2175 icon->icon->core->descriptor.handle_leavenotify = clipLeaveNotify;
2177 icon->icon->core->descriptor.parent_type = WCLASS_DOCK_ICON;
2178 icon->icon->core->descriptor.parent = icon;
2180 MoveInStackListUnder(dock->icon_array[index-1]->icon->core,
2181 icon->icon->core);
2182 wAppIconMove(icon, icon->x_pos, icon->y_pos);
2183 wAppIconPaint(icon);
2185 if (wPreferences.auto_arrange_icons)
2186 wArrangeIcons(dock->screen_ptr, True);
2188 #ifdef OFFIX_DND
2189 if (icon->command && !icon->dnd_command) {
2190 icon->dnd_command = wmalloc(strlen(icon->command)+8);
2191 sprintf(icon->dnd_command, "%s %%d", icon->command);
2193 #endif
2195 return True;
2199 void
2200 reattachIcon(WDock *dock, WAppIcon *icon, int x, int y)
2202 int index;
2204 for(index=1; index<dock->max_icons; index++) {
2205 if(dock->icon_array[index] == icon)
2206 break;
2208 assert(index < dock->max_icons);
2210 icon->yindex = y;
2211 icon->xindex = x;
2213 icon->x_pos = dock->x_pos + x*ICON_SIZE;
2214 icon->y_pos = dock->y_pos + y*ICON_SIZE;
2218 Bool
2219 moveIconBetweenDocks(WDock *src, WDock *dest, WAppIcon *icon, int x, int y)
2221 WWindow *wwin;
2222 char **argv;
2223 int argc;
2224 int index;
2226 if (src == dest)
2227 return True; /* No move needed, we're already there */
2229 if (dest == NULL)
2230 return False;
2232 wwin = icon->icon->owner;
2235 * For the moment we can't do this if we move icons in Clip from one
2236 * workspace to other, because if we move two or more icons without
2237 * command, the dialog box will not be able to tell us to which of the
2238 * moved icons it applies. -Dan
2240 if ((dest->type==WM_DOCK /*|| dest->keep_attracted*/) && icon->command==NULL) {
2241 if (XGetCommand(dpy, wwin->client_win, &argv, &argc) && argc>0) {
2243 icon->command = FlattenStringList(argv, argc);
2244 XFreeStringList(argv);
2245 } else {
2246 char *command=NULL;
2248 icon->editing = 1;
2249 /* icon->forced_dock = 1;*/
2250 if (wInputDialog(src->screen_ptr, _("Dock Icon"),
2251 _("Type the command used to launch the application"),
2252 &command)) {
2253 if (command && (command[0]==0 ||
2254 (command[0]=='-' && command[1]==0))) {
2255 free(command);
2256 command = NULL;
2258 icon->command = command;
2259 } else {
2260 icon->editing = 0;
2261 if (command)
2262 free(command);
2263 return False;
2265 icon->editing = 0;
2269 if (dest->type == WM_DOCK)
2270 wClipMakeIconOmnipresent(icon, False);
2272 for(index=1; index<src->max_icons; index++) {
2273 if(src->icon_array[index] == icon)
2274 break;
2276 assert(index < src->max_icons);
2278 src->icon_array[index] = NULL;
2279 src->icon_count--;
2281 for(index=1; index<dest->max_icons; index++) {
2282 if(dest->icon_array[index] == NULL)
2283 break;
2285 /* if (index == dest->max_icons)
2286 return; */
2288 assert(index < dest->max_icons);
2290 dest->icon_array[index] = icon;
2291 icon->dock = dest;
2293 /* deselect the icon */
2294 if (icon->icon->selected)
2295 wIconSelect(icon->icon);
2297 if (dest->type == WM_DOCK) {
2298 icon->icon->core->descriptor.handle_enternotify = NULL;
2299 icon->icon->core->descriptor.handle_leavenotify = NULL;
2300 } else {
2301 icon->icon->core->descriptor.handle_enternotify = clipEnterNotify;
2302 icon->icon->core->descriptor.handle_leavenotify = clipLeaveNotify;
2305 /* set it to be kept when moving to dock.
2306 * Unless the icon does not have a command set
2308 if (icon->command && dest->type==WM_DOCK) {
2309 icon->attracted = 0;
2310 if (icon->icon->shadowed) {
2311 icon->icon->shadowed = 0;
2312 icon->icon->force_paint = 1;
2316 if (src->auto_collapse || src->auto_raise_lower)
2317 clipLeave(src);
2319 icon->yindex = y;
2320 icon->xindex = x;
2322 icon->x_pos = dest->x_pos + x*ICON_SIZE;
2323 icon->y_pos = dest->y_pos + y*ICON_SIZE;
2325 dest->icon_count++;
2327 MoveInStackListUnder(dest->icon_array[index-1]->icon->core,
2328 icon->icon->core);
2329 wAppIconPaint(icon);
2331 return True;
2335 void
2336 wDockDetach(WDock *dock, WAppIcon *icon)
2338 int index;
2340 /* make the settings panel be closed */
2341 if (icon->panel) {
2342 DestroyDockAppSettingsPanel(icon->panel);
2345 /* This must be called before icon->dock is set to NULL.
2346 * Don't move it. -Dan
2348 wClipMakeIconOmnipresent(icon, False);
2350 icon->docked = 0;
2351 icon->dock = NULL;
2352 icon->attracted = 0;
2353 icon->auto_launch = 0;
2354 if (icon->icon->shadowed) {
2355 icon->icon->shadowed = 0;
2356 icon->icon->force_paint = 1;
2359 /* deselect the icon */
2360 if (icon->icon->selected)
2361 wIconSelect(icon->icon);
2363 if (icon->command) {
2364 free(icon->command);
2365 icon->command = NULL;
2367 #ifdef OFFIX_DND
2368 if (icon->dnd_command) {
2369 free(icon->dnd_command);
2370 icon->dnd_command = NULL;
2372 #endif
2374 for (index=1; index<dock->max_icons; index++)
2375 if (dock->icon_array[index] == icon)
2376 break;
2377 assert(index < dock->max_icons);
2378 dock->icon_array[index] = NULL;
2379 icon->yindex = -1;
2380 icon->xindex = -1;
2382 dock->icon_count--;
2384 /* if the dock is not attached to an application or
2385 * the the application did not set the approriate hints yet,
2386 * destroy the icon */
2387 #ifdef REDUCE_APPICONS
2388 if ((icon->num_apps == 0) && (!icon->running || !wApplicationOf(icon->main_window)) )
2389 #else
2390 if (!icon->running || !wApplicationOf(icon->main_window))
2391 #endif
2392 wAppIconDestroy(icon);
2393 else {
2394 icon->icon->core->descriptor.handle_mousedown = appIconMouseDown;
2395 icon->icon->core->descriptor.handle_enternotify = NULL;
2396 icon->icon->core->descriptor.handle_leavenotify = NULL;
2397 icon->icon->core->descriptor.parent_type = WCLASS_APPICON;
2398 icon->icon->core->descriptor.parent = icon;
2400 ChangeStackingLevel(icon->icon->core, NORMAL_ICON_LEVEL);
2402 wAppIconPaint(icon);
2403 if (wPreferences.auto_arrange_icons) {
2404 wArrangeIcons(dock->screen_ptr, True);
2407 if (dock->auto_collapse || dock->auto_raise_lower)
2408 clipLeave(dock);
2413 * returns the closest Dock slot index for the passed
2414 * coordinates.
2416 * Returns False if icon can't be docked.
2418 * Note: this function should NEVER alter ret_x or ret_y, unless it will
2419 * return True. -Dan
2421 Bool
2422 wDockSnapIcon(WDock *dock, WAppIcon *icon, int req_x, int req_y,
2423 int *ret_x, int *ret_y, int redocking)
2425 WScreen *scr = dock->screen_ptr;
2426 int dx, dy;
2427 int ex_x, ex_y;
2428 int i, offset = ICON_SIZE/2;
2429 WAppIcon *aicon = NULL;
2430 WAppIcon *nicon = NULL;
2431 int max_y_icons, max_x_icons;
2433 max_x_icons = scr->scr_width/ICON_SIZE;
2434 max_y_icons = scr->scr_height/ICON_SIZE-1;
2436 if (wPreferences.flags.noupdates)
2437 return False;
2439 dx = dock->x_pos;
2440 dy = dock->y_pos;
2442 /* if the dock is full */
2443 if (!redocking &&
2444 (dock->icon_count >= dock->max_icons)) {
2445 return False;
2448 /* exact position */
2449 if (req_y < dy)
2450 ex_y = (req_y - offset - dy)/ICON_SIZE;
2451 else
2452 ex_y = (req_y + offset - dy)/ICON_SIZE;
2454 if (req_x < dx)
2455 ex_x = (req_x - offset - dx)/ICON_SIZE;
2456 else
2457 ex_x = (req_x + offset - dx)/ICON_SIZE;
2459 /* check if the icon is outside the screen boundaries */
2460 if (dx + ex_x*ICON_SIZE < -ICON_SIZE+2 ||
2461 dx + ex_x*ICON_SIZE >= scr->scr_width-1 ||
2462 dy + ex_y*ICON_SIZE < -ICON_SIZE+2 ||
2463 dy + ex_y*ICON_SIZE >= scr->scr_height-1)
2464 return False;
2466 if (dock->type == WM_DOCK) {
2467 if (icon->dock != dock && ex_x != 0)
2468 return False;
2470 aicon = NULL;
2471 for (i=0; i<dock->max_icons; i++) {
2472 nicon = dock->icon_array[i];
2473 if (nicon && nicon->yindex == ex_y) {
2474 aicon = nicon;
2475 break;
2479 if (redocking) {
2480 int sig, done, closest;
2482 /* Possible cases when redocking:
2484 * icon dragged out of range of any slot -> false
2485 * icon dragged to range of free slot
2486 * icon dragged to range of same slot
2487 * icon dragged to range of different icon
2489 if (abs(ex_x) > DOCK_DETTACH_THRESHOLD)
2490 return False;
2492 if (ex_y>=0 && ex_y<=max_y_icons && (aicon==icon || !aicon)) {
2493 *ret_x = 0;
2494 *ret_y = ex_y;
2495 return True;
2498 /* start looking at the upper slot or lower? */
2499 if (ex_y*ICON_SIZE < (req_y + offset - dy))
2500 sig = 1;
2501 else
2502 sig = -1;
2504 closest = -1;
2505 done = 0;
2506 /* look for closest free slot */
2507 for (i=0; i<(DOCK_DETTACH_THRESHOLD+1)*2 && !done; i++) {
2508 int j;
2510 done = 1;
2511 closest = sig*(i/2) + ex_y;
2512 /* check if this slot is used */
2513 if (closest >= 0) {
2514 for (j = 0; j<dock->max_icons; j++) {
2515 if (dock->icon_array[j]
2516 && dock->icon_array[j]->yindex==closest) {
2517 /* slot is used by someone else */
2518 if (dock->icon_array[j]!=icon)
2519 done = 0;
2520 break;
2524 sig = -sig;
2526 if (done && closest >= 0 && closest <= max_y_icons &&
2527 ((ex_y >= closest && ex_y - closest < DOCK_DETTACH_THRESHOLD+1)
2529 (ex_y < closest && closest - ex_y <= DOCK_DETTACH_THRESHOLD+1))) {
2530 *ret_x = 0;
2531 *ret_y = closest;
2532 return True;
2534 } else { /* !redocking */
2536 /* if slot is free and the icon is close enough, return it */
2537 if (!aicon && ex_x == 0 && ex_y >= 0 && ex_y <= max_y_icons) {
2538 *ret_x = 0;
2539 *ret_y = ex_y;
2540 return True;
2543 } else { /* CLIP */
2544 int neighbours = 0;
2545 int start, stop, k;
2547 start = icon->omnipresent ? 0 : scr->current_workspace;
2548 stop = icon->omnipresent ? scr->workspace_count : start+1;
2550 aicon = NULL;
2551 for (k=start; k<stop; k++) {
2552 WDock *tmp = scr->workspaces[k]->clip;
2553 if (!tmp)
2554 continue;
2555 for (i=0; i<tmp->max_icons; i++) {
2556 nicon = tmp->icon_array[i];
2557 if (nicon && nicon->xindex == ex_x && nicon->yindex == ex_y) {
2558 aicon = nicon;
2559 break;
2562 if (aicon)
2563 break;
2565 for (k=start; k<stop; k++) {
2566 WDock *tmp = scr->workspaces[k]->clip;
2567 if (!tmp)
2568 continue;
2569 for (i=0; i<tmp->max_icons; i++) {
2570 nicon = tmp->icon_array[i];
2571 if (nicon && nicon != icon && /* Icon can't be it's own neighbour */
2572 (abs(nicon->xindex - ex_x) <= CLIP_ATTACH_VICINITY &&
2573 abs(nicon->yindex - ex_y) <= CLIP_ATTACH_VICINITY)) {
2574 neighbours = 1;
2575 break;
2578 if (neighbours)
2579 break;
2582 if (neighbours && (aicon==NULL || (redocking && aicon == icon))) {
2583 *ret_x = ex_x;
2584 *ret_y = ex_y;
2585 return True;
2588 return False;
2591 #define MIN(x, y) ((x) > (y) ? (y) : (x))
2592 #define MAX(x, y) ((x) < (y) ? (y) : (x))
2594 #define ON_SCREEN(x, y, sx, ex, sy, ey) \
2595 ((((x)+ICON_SIZE/2) >= (sx)) && (((y)+ICON_SIZE/2) >= (sy)) && \
2596 (((x) + (ICON_SIZE/2)) <= (ex)) && (((y) + (ICON_SIZE/2)) <= ey))
2600 * returns true if it can find a free slot in the dock,
2601 * in which case it changes x_pos and y_pos accordingly.
2602 * Else returns false.
2604 Bool
2605 wDockFindFreeSlot(WDock *dock, int *x_pos, int *y_pos)
2607 WScreen *scr = dock->screen_ptr;
2608 WAppIcon *btn;
2609 WAppIconChain *chain;
2610 unsigned char *slot_map;
2611 int mwidth;
2612 int r;
2613 int x, y;
2614 int i, done = False;
2615 int corner;
2616 int sx=0, sy=0, ex=scr->scr_width, ey=scr->scr_height;
2617 int extra_count=0;
2619 if (dock->type == WM_CLIP &&
2620 dock != scr->workspaces[scr->current_workspace]->clip)
2621 extra_count = scr->global_icon_count;
2623 /* if the dock is full */
2624 if (dock->icon_count+extra_count >= dock->max_icons) {
2625 return False;
2628 if (!wPreferences.flags.nodock && scr->dock) {
2629 if (scr->dock->on_right_side)
2630 ex -= ICON_SIZE + DOCK_EXTRA_SPACE;
2631 else
2632 sx += ICON_SIZE + DOCK_EXTRA_SPACE;
2635 if (ex < dock->x_pos)
2636 ex = dock->x_pos;
2637 if (sx > dock->x_pos+ICON_SIZE)
2638 sx = dock->x_pos+ICON_SIZE;
2639 #define C_NONE 0
2640 #define C_NW 1
2641 #define C_NE 2
2642 #define C_SW 3
2643 #define C_SE 4
2645 /* check if clip is in a corner */
2646 if (dock->type==WM_CLIP) {
2647 if (dock->x_pos < 1 && dock->y_pos < 1)
2648 corner = C_NE;
2649 else if (dock->x_pos < 1 && dock->y_pos >= (ey-ICON_SIZE))
2650 corner = C_SE;
2651 else if (dock->x_pos >= (ex-ICON_SIZE)&& dock->y_pos >= (ey-ICON_SIZE))
2652 corner = C_SW;
2653 else if (dock->x_pos >= (ex-ICON_SIZE) && dock->y_pos < 1)
2654 corner = C_NW;
2655 else
2656 corner = C_NONE;
2657 } else
2658 corner = C_NONE;
2660 /* If the clip is in the corner, use only slots that are in the border
2661 * of the screen */
2662 if (corner!=C_NONE) {
2663 char *hmap, *vmap;
2664 int hcount, vcount;
2666 hcount = MIN(dock->max_icons, scr->scr_width/ICON_SIZE);
2667 vcount = MIN(dock->max_icons, scr->scr_height/ICON_SIZE);
2668 hmap = wmalloc(hcount+1);
2669 memset(hmap, 0, hcount+1);
2670 vmap = wmalloc(vcount+1);
2671 memset(vmap, 0, vcount+1);
2673 /* mark used positions */
2674 switch (corner) {
2675 case C_NE:
2676 for (i=0; i<dock->max_icons; i++) {
2677 btn = dock->icon_array[i];
2678 if (!btn)
2679 continue;
2681 if (btn->xindex==0 && btn->yindex > 0 && btn->yindex < vcount)
2682 vmap[btn->yindex] = 1;
2683 else if (btn->yindex==0 && btn->xindex>0 && btn->xindex<hcount)
2684 hmap[btn->xindex] = 1;
2686 for (chain=scr->global_icons; chain!=NULL; chain=chain->next) {
2687 btn = chain->aicon;
2688 if (btn->xindex==0 && btn->yindex > 0 && btn->yindex < vcount)
2689 vmap[btn->yindex] = 1;
2690 else if (btn->yindex==0 && btn->xindex>0 && btn->xindex<hcount)
2691 hmap[btn->xindex] = 1;
2693 break;
2694 case C_NW:
2695 for (i=0; i<dock->max_icons; i++) {
2696 btn = dock->icon_array[i];
2697 if (!btn)
2698 continue;
2700 if (btn->xindex==0 && btn->yindex > 0 && btn->yindex < vcount)
2701 vmap[btn->yindex] = 1;
2702 else if (btn->yindex==0 && btn->xindex<0 &&btn->xindex>-hcount)
2703 hmap[-btn->xindex] = 1;
2705 for (chain=scr->global_icons; chain!=NULL; chain=chain->next) {
2706 btn = chain->aicon;
2707 if (btn->xindex==0 && btn->yindex > 0 && btn->yindex < vcount)
2708 vmap[btn->yindex] = 1;
2709 else if (btn->yindex==0 && btn->xindex<0 &&btn->xindex>-hcount)
2710 hmap[-btn->xindex] = 1;
2712 break;
2713 case C_SE:
2714 for (i=0; i<dock->max_icons; i++) {
2715 btn = dock->icon_array[i];
2716 if (!btn)
2717 continue;
2719 if (btn->xindex==0 && btn->yindex < 0 && btn->yindex > -vcount)
2720 vmap[-btn->yindex] = 1;
2721 else if (btn->yindex==0 && btn->xindex>0 && btn->xindex<hcount)
2722 hmap[btn->xindex] = 1;
2724 for (chain=scr->global_icons; chain!=NULL; chain=chain->next) {
2725 btn = chain->aicon;
2726 if (btn->xindex==0 && btn->yindex < 0 && btn->yindex > -vcount)
2727 vmap[-btn->yindex] = 1;
2728 else if (btn->yindex==0 && btn->xindex>0 && btn->xindex<hcount)
2729 hmap[btn->xindex] = 1;
2731 break;
2732 case C_SW:
2733 default:
2734 for (i=0; i<dock->max_icons; i++) {
2735 btn = dock->icon_array[i];
2736 if (!btn)
2737 continue;
2739 if (btn->xindex==0 && btn->yindex < 0 && btn->yindex > -vcount)
2740 vmap[-btn->yindex] = 1;
2741 else if (btn->yindex==0 && btn->xindex<0 &&btn->xindex>-hcount)
2742 hmap[-btn->xindex] = 1;
2744 for (chain=scr->global_icons; chain!=NULL; chain=chain->next) {
2745 btn = chain->aicon;
2746 if (btn->xindex==0 && btn->yindex < 0 && btn->yindex > -vcount)
2747 vmap[-btn->yindex] = 1;
2748 else if (btn->yindex==0 && btn->xindex<0 &&btn->xindex>-hcount)
2749 hmap[-btn->xindex] = 1;
2752 x=0; y=0;
2753 done = 0;
2754 /* search a vacant slot */
2755 for (i=1; i<MAX(vcount, hcount); i++) {
2756 if (i < vcount && vmap[i]==0) {
2757 /* found a slot */
2758 x = 0;
2759 y = i;
2760 done = 1;
2761 break;
2762 } else if (i < hcount && hmap[i]==0) {
2763 /* found a slot */
2764 x = i;
2765 y = 0;
2766 done = 1;
2767 break;
2770 free(vmap);
2771 free(hmap);
2772 /* If found a slot, translate and return */
2773 if (done) {
2774 if (corner==C_NW || corner==C_NE) {
2775 *y_pos = y;
2776 } else {
2777 *y_pos = -y;
2779 if (corner==C_NE || corner==C_SE) {
2780 *x_pos = x;
2781 } else {
2782 *x_pos = -x;
2784 return True;
2786 /* else, try to find a slot somewhere else */
2789 /* a map of mwidth x mwidth would be enough if we allowed icons to be
2790 * placed outside of screen */
2791 mwidth = (int)ceil(sqrt(dock->max_icons));
2793 /* In the worst case (the clip is in the corner of the screen),
2794 * the amount of icons that fit in the clip is smaller.
2795 * Double the map to get a safe value.
2797 mwidth += mwidth;
2799 r = (mwidth-1)/2;
2801 slot_map = wmalloc(mwidth*mwidth);
2802 memset(slot_map, 0, mwidth*mwidth);
2804 #define XY2OFS(x,y) (MAX(abs(x),abs(y)) > r) ? 0 : (((y)+r)*(mwidth)+(x)+r)
2806 /* mark used slots in the map. If the slot falls outside the map
2807 * (for example, when all icons are placed in line), ignore them. */
2808 for (i=0; i<dock->max_icons; i++) {
2809 btn = dock->icon_array[i];
2810 if (btn)
2811 slot_map[XY2OFS(btn->xindex, btn->yindex)] = 1;
2813 for (chain=scr->global_icons; chain!=NULL; chain=chain->next) {
2814 slot_map[XY2OFS(chain->aicon->xindex, chain->aicon->yindex)] = 1;
2816 /* Find closest slot from the center that is free by scanning the
2817 * map from the center to outward in circular passes.
2818 * This will not result in a neat layout, but will be optimal
2819 * in the sense that there will not be holes left.
2821 done = 0;
2822 for (i = 1; i <= r && !done; i++) {
2823 int tx, ty;
2825 /* top and bottom parts of the ring */
2826 for (x = -i; x <= i && !done; x++) {
2827 tx = dock->x_pos + x*ICON_SIZE;
2828 y = -i;
2829 ty = dock->y_pos + y*ICON_SIZE;
2830 if (slot_map[XY2OFS(x,y)]==0
2831 && ON_SCREEN(tx, ty, sx, ex, sy, ey)) {
2832 *x_pos = x;
2833 *y_pos = y;
2834 done = 1;
2835 break;
2837 y = i;
2838 ty = dock->y_pos + y*ICON_SIZE;
2839 if (slot_map[XY2OFS(x,y)]==0
2840 && ON_SCREEN(tx, ty, sx, ex, sy, ey)) {
2841 *x_pos = x;
2842 *y_pos = y;
2843 done = 1;
2844 break;
2847 /* left and right parts of the ring */
2848 for (y = -i+1; y <= i-1; y++) {
2849 ty = dock->y_pos + y*ICON_SIZE;
2850 x = -i;
2851 tx = dock->x_pos + x*ICON_SIZE;
2852 if (slot_map[XY2OFS(x,y)]==0
2853 && ON_SCREEN(tx, ty, sx, ex, sy, ey)) {
2854 *x_pos = x;
2855 *y_pos = y;
2856 done = 1;
2857 break;
2859 x = i;
2860 tx = dock->x_pos + x*ICON_SIZE;
2861 if (slot_map[XY2OFS(x,y)]==0
2862 && ON_SCREEN(tx, ty, sx, ex, sy, ey)) {
2863 *x_pos = x;
2864 *y_pos = y;
2865 done = 1;
2866 break;
2870 free(slot_map);
2871 #undef XY2OFS
2872 return done;
2876 static void
2877 moveDock(WDock *dock, int new_x, int new_y)
2879 WAppIcon *btn;
2880 int i;
2882 dock->x_pos = new_x;
2883 dock->y_pos = new_y;
2884 for (i=0; i<dock->max_icons; i++) {
2885 btn = dock->icon_array[i];
2886 if (btn) {
2887 btn->x_pos = new_x + btn->xindex*ICON_SIZE;
2888 btn->y_pos = new_y + btn->yindex*ICON_SIZE;
2889 XMoveWindow(dpy, btn->icon->core->window, btn->x_pos, btn->y_pos);
2895 static void
2896 swapDock(WDock *dock)
2898 WScreen *scr = dock->screen_ptr;
2899 WAppIcon *btn;
2900 int x, i;
2903 if (dock->on_right_side) {
2904 x = dock->x_pos = scr->scr_width - ICON_SIZE - DOCK_EXTRA_SPACE;
2905 } else {
2906 x = dock->x_pos = DOCK_EXTRA_SPACE;
2909 for (i=0; i<dock->max_icons; i++) {
2910 btn = dock->icon_array[i];
2911 if (btn) {
2912 btn->x_pos = x;
2913 XMoveWindow(dpy, btn->icon->core->window, btn->x_pos, btn->y_pos);
2917 wScreenUpdateUsableArea(scr);
2921 static pid_t
2922 execCommand(WAppIcon *btn, char *command, WSavedState *state)
2924 WScreen *scr = btn->icon->core->screen_ptr;
2925 pid_t pid;
2926 char **argv;
2927 int argc;
2928 char *cmdline;
2930 cmdline = ExpandOptions(scr, command);
2932 if (scr->flags.dnd_data_convertion_status || !cmdline) {
2933 if (cmdline)
2934 free(cmdline);
2935 if (state)
2936 free(state);
2937 return 0;
2940 ParseCommand(cmdline, &argv, &argc);
2942 if (argv==NULL) {
2943 if (cmdline)
2944 free(cmdline);
2945 if (state)
2946 free(state);
2947 return 0;
2950 if ((pid=fork())==0) {
2951 char **args;
2952 int i;
2954 SetupEnvironment(scr);
2956 #ifdef HAVE_SETPGID
2957 setpgid(0, 0);
2958 #endif
2960 args = malloc(sizeof(char*)*(argc+1));
2961 if (!args)
2962 exit(111);
2963 for (i=0; i<argc; i++) {
2964 args[i] = argv[i];
2966 args[argc] = NULL;
2967 execvp(argv[0], args);
2968 exit(111);
2970 while (argc > 0)
2971 free(argv[--argc]);
2972 free(argv);
2974 if (pid > 0) {
2975 if (!state) {
2976 state = wmalloc(sizeof(WSavedState));
2977 memset(state, 0, sizeof(WSavedState));
2978 state->hidden = -1;
2979 state->miniaturized = -1;
2980 state->shaded = -1;
2981 if (btn->dock==scr->dock || btn->omnipresent)
2982 state->workspace = -1;
2983 else
2984 state->workspace = scr->current_workspace;
2986 wWindowAddSavedState(btn->wm_instance, btn->wm_class, cmdline, pid,
2987 state);
2988 wAddDeathHandler(pid, (WDeathHandler*)trackDeadProcess,
2989 btn->dock);
2990 } else if (state) {
2991 free(state);
2993 free(cmdline);
2994 return pid;
2998 void
2999 wDockHideIcons(WDock *dock)
3001 int i;
3003 if (dock==NULL)
3004 return;
3006 for (i=1; i<dock->max_icons; i++) {
3007 if (dock->icon_array[i])
3008 XUnmapWindow(dpy, dock->icon_array[i]->icon->core->window);
3010 dock->mapped = 0;
3012 dockIconPaint(dock->icon_array[0]);
3016 void
3017 wDockShowIcons(WDock *dock)
3019 int i, newlevel;
3020 WAppIcon *btn;
3022 if (dock==NULL)
3023 return;
3025 btn = dock->icon_array[0];
3026 moveDock(dock, btn->x_pos, btn->y_pos);
3028 newlevel = dock->lowered ? WMNormalLevel : WMDockLevel;
3029 ChangeStackingLevel(btn->icon->core, newlevel);
3031 for (i=1; i<dock->max_icons; i++) {
3032 if (dock->icon_array[i]) {
3033 MoveInStackListAbove(dock->icon_array[i]->icon->core,
3034 btn->icon->core);
3035 break;
3039 if (!dock->collapsed) {
3040 for (i=1; i<dock->max_icons; i++) {
3041 if (dock->icon_array[i]) {
3042 XMapWindow(dpy, dock->icon_array[i]->icon->core->window);
3046 dock->mapped = 1;
3048 dockIconPaint(btn);
3052 void
3053 wDockLower(WDock *dock)
3055 int i;
3057 for (i=0; i<dock->max_icons; i++) {
3058 if (dock->icon_array[i])
3059 wLowerFrame(dock->icon_array[i]->icon->core);
3064 void
3065 wDockRaise(WDock *dock)
3067 int i;
3069 for (i=dock->max_icons-1; i>=0; i--) {
3070 if (dock->icon_array[i])
3071 wRaiseFrame(dock->icon_array[i]->icon->core);
3076 void
3077 wDockRaiseLower(WDock *dock)
3079 if (!dock->icon_array[0]->icon->core->stacking->above
3080 ||(dock->icon_array[0]->icon->core->stacking->window_level
3081 !=dock->icon_array[0]->icon->core->stacking->above->stacking->window_level))
3082 wDockLower(dock);
3083 else
3084 wDockRaise(dock);
3088 void
3089 wDockFinishLaunch(WDock *dock, WAppIcon *icon)
3091 icon->launching = 0;
3092 icon->relaunching = 0;
3093 dockIconPaint(icon);
3097 WAppIcon*
3098 wDockFindIconForWindow(WDock *dock, Window window)
3100 WAppIcon *icon;
3101 int i;
3103 for (i=0; i<dock->max_icons; i++) {
3104 icon = dock->icon_array[i];
3105 if (icon && icon->main_window == window)
3106 return icon;
3108 return NULL;
3112 void
3113 wDockTrackWindowLaunch(WDock *dock, Window window)
3115 WAppIcon *icon;
3116 #ifdef REDUCE_APPICONS
3117 WAppIconAppList *tapplist;
3118 #endif
3119 char *wm_class, *wm_instance;
3120 int i;
3121 Bool firstPass = True;
3122 Bool found = False;
3123 char *command = NULL;
3126 int argc;
3127 char **argv;
3129 if (XGetCommand(dpy, window, &argv, &argc)) {
3130 if (argc > 0 && argv != NULL)
3131 command = FlattenStringList(argv,argc);
3132 if (argv) {
3133 XFreeStringList(argv);
3138 if (!PropGetWMClass(window, &wm_class, &wm_instance) ||
3139 (!wm_class && !wm_instance))
3140 return;
3142 retry:
3143 for (i=0; i<dock->max_icons; i++) {
3144 icon = dock->icon_array[i];
3145 if (!icon)
3146 continue;
3148 /* app is already attached to icon */
3149 if (icon->main_window == window) {
3150 found = True;
3151 break;
3154 if ((icon->wm_instance || icon->wm_class)
3155 && (icon->launching
3156 || (dock->screen_ptr->flags.startup && !icon->running))) {
3158 if (icon->wm_instance && wm_instance &&
3159 strcmp(icon->wm_instance, wm_instance)!=0) {
3160 continue;
3162 if (icon->wm_class && wm_class &&
3163 strcmp(icon->wm_class, wm_class)!=0) {
3164 continue;
3166 if (firstPass && command && strcmp(icon->command, command)!=0) {
3167 continue;
3170 if (!icon->relaunching) {
3171 WApplication *wapp;
3173 /* Possibly an application that was docked with dockit,
3174 * but the user did not update WMState to indicate that
3175 * it was docked by force */
3176 wapp = wApplicationOf(window);
3177 if (!wapp) {
3178 icon->forced_dock = 1;
3179 icon->running = 0;
3181 if (!icon->forced_dock)
3182 icon->main_window = window;
3184 #ifdef REDUCE_APPICONS
3185 tapplist = wmalloc(sizeof(WAppIconAppList));
3186 memset(tapplist, 0, sizeof(WAppIconAppList));
3187 tapplist->next = icon->applist;
3188 if (icon->applist)
3189 icon->applist->prev = tapplist;
3190 icon->applist = tapplist;
3191 tapplist->wapp = wApplicationOf(window);
3192 icon->num_apps++;
3193 #endif
3195 found = True;
3196 wDockFinishLaunch(dock, icon);
3197 break;
3201 if (firstPass && !found) {
3202 firstPass = False;
3203 goto retry;
3206 if (command)
3207 free(command);
3209 if (wm_class)
3210 XFree(wm_class);
3211 if (wm_instance)
3212 XFree(wm_instance);
3217 void
3218 wClipUpdateForWorkspaceChange(WScreen *scr, int workspace)
3220 if (!wPreferences.flags.noclip) {
3221 scr->clip_icon->dock = scr->workspaces[workspace]->clip;
3222 if (scr->current_workspace != workspace) {
3223 WDock *old_clip = scr->workspaces[scr->current_workspace]->clip;
3224 WAppIconChain *chain = scr->global_icons;
3226 while (chain) {
3227 moveIconBetweenDocks(chain->aicon->dock,
3228 scr->workspaces[workspace]->clip,
3229 chain->aicon, chain->aicon->xindex,
3230 chain->aicon->yindex);
3231 if (scr->workspaces[workspace]->clip->collapsed)
3232 XUnmapWindow(dpy, chain->aicon->icon->core->window);
3233 chain = chain->next;
3236 wDockHideIcons(old_clip);
3237 if (old_clip->auto_raise_lower) {
3238 if (old_clip->auto_raise_magic) {
3239 WMDeleteTimerHandler(old_clip->auto_raise_magic);
3240 old_clip->auto_raise_magic = NULL;
3242 wDockLower(old_clip);
3244 if (old_clip->auto_collapse) {
3245 if (old_clip->auto_expand_magic) {
3246 WMDeleteTimerHandler(old_clip->auto_expand_magic);
3247 old_clip->auto_expand_magic = NULL;
3249 old_clip->collapsed = 1;
3251 wDockShowIcons(scr->workspaces[workspace]->clip);
3253 if (scr->flags.clip_balloon_mapped)
3254 showClipBalloon(scr->clip_icon->dock, workspace);
3260 static void
3261 trackDeadProcess(pid_t pid, unsigned char status, WDock *dock)
3263 WAppIcon *icon;
3264 int i;
3266 for (i=0; i<dock->max_icons; i++) {
3267 icon = dock->icon_array[i];
3268 if (!icon)
3269 continue;
3271 if (icon->launching && icon->pid == pid) {
3272 if (!icon->relaunching) {
3273 icon->running = 0;
3274 icon->main_window = None;
3276 wDockFinishLaunch(dock, icon);
3277 icon->pid = 0;
3278 if (status==111) {
3279 char msg[PATH_MAX];
3280 #ifdef OFFIX_DND
3281 sprintf(msg, _("Could not execute command \"%s\""),
3282 icon->drop_launch && icon->dnd_command
3283 ? icon->dnd_command : icon->command);
3284 #else
3285 sprintf(msg, _("Could not execute command \"%s\""),
3286 icon->command);
3287 #endif
3288 wMessageDialog(dock->screen_ptr, _("Error"), msg,
3289 _("OK"), NULL, NULL);
3291 break;
3297 static void
3298 toggleLowered(WDock *dock)
3300 WAppIcon *tmp;
3301 int newlevel, i;
3303 /* lower/raise Dock */
3304 if (!dock->lowered) {
3305 newlevel = WMNormalLevel;
3306 dock->lowered = 1;
3307 } else {
3308 newlevel = WMDockLevel;
3309 dock->lowered = 0;
3312 for (i=0; i<dock->max_icons; i++) {
3313 tmp = dock->icon_array[i];
3314 if (!tmp)
3315 continue;
3317 ChangeStackingLevel(tmp->icon->core, newlevel);
3318 if (dock->lowered)
3319 wLowerFrame(tmp->icon->core);
3322 if (dock->type == WM_DOCK)
3323 wScreenUpdateUsableArea(dock->screen_ptr);
3327 static void
3328 toggleCollapsed(WDock *dock)
3330 if (dock->collapsed) {
3331 dock->collapsed = 0;
3332 wDockShowIcons(dock);
3334 else {
3335 dock->collapsed = 1;
3336 wDockHideIcons(dock);
3341 static void
3342 openDockMenu(WDock *dock, WAppIcon *aicon, XEvent *event)
3344 WScreen *scr = dock->screen_ptr;
3345 WObjDescriptor *desc;
3346 WMenuEntry *entry;
3347 WApplication *wapp = NULL;
3348 int index = 0;
3349 int x_pos;
3350 int n_selected;
3351 int appIsRunning = aicon->running && aicon->icon && aicon->icon->owner;
3353 if (dock->type == WM_DOCK) {
3354 /* keep on top */
3355 entry = dock->menu->entries[index];
3357 if (dock->lowered) {
3358 entry->text = _("Keep on Top");
3359 } else {
3360 entry->text = _("Allow Lowering");
3362 entry->clientdata = dock;
3364 dock->menu->flags.realized = 0;
3365 } else {
3366 /* clip options */
3367 if (scr->clip_options)
3368 updateClipOptionsMenu(scr->clip_options, dock);
3370 n_selected = numberOfSelectedIcons(dock);
3372 /* Rename Workspace */
3373 entry = dock->menu->entries[++index];
3374 if (aicon == scr->clip_icon) {
3375 entry->callback = renameCallback;
3376 entry->clientdata = dock;
3377 entry->flags.indicator = 0;
3378 entry->text = _("Rename Workspace");
3379 } else {
3380 entry->callback = omnipresentCallback;
3381 entry->clientdata = aicon;
3382 if (n_selected > 0) {
3383 entry->flags.indicator = 0;
3384 entry->text = _("Toggle Omnipresent");
3385 } else {
3386 entry->flags.indicator = 1;
3387 entry->flags.indicator_on = aicon->omnipresent;
3388 entry->flags.indicator_type = MI_CHECK;
3389 entry->text = _("Omnipresent");
3393 /* select/unselect icon */
3394 entry = dock->menu->entries[++index];
3395 entry->clientdata = aicon;
3396 entry->flags.indicator_on = aicon->icon->selected;
3397 wMenuSetEnabled(dock->menu, index, aicon!=scr->clip_icon);
3399 /* select/unselect all icons */
3400 entry = dock->menu->entries[++index];
3401 entry->clientdata = aicon;
3402 if (n_selected > 0)
3403 entry->text = _("Unselect All Icons");
3404 else
3405 entry->text = _("Select All Icons");
3406 wMenuSetEnabled(dock->menu, index, dock->icon_count > 1);
3408 /* keep icon(s) */
3409 entry = dock->menu->entries[++index];
3410 entry->clientdata = aicon;
3411 if (n_selected > 1)
3412 entry->text = _("Keep Icons");
3413 else
3414 entry->text = _("Keep Icon");
3415 wMenuSetEnabled(dock->menu, index, dock->icon_count > 1);
3417 /* this is the workspace submenu part */
3418 entry = dock->menu->entries[++index];
3419 if (n_selected > 1)
3420 entry->text = _("Move Icons To");
3421 else
3422 entry->text = _("Move Icon To");
3423 if (scr->clip_submenu)
3424 updateWorkspaceMenu(scr->clip_submenu, aicon);
3425 wMenuSetEnabled(dock->menu, index, !aicon->omnipresent);
3427 /* remove icon(s) */
3428 entry = dock->menu->entries[++index];
3429 entry->clientdata = aicon;
3430 if (n_selected > 1)
3431 entry->text = _("Remove Icons");
3432 else
3433 entry->text = _("Remove Icon");
3434 wMenuSetEnabled(dock->menu, index, dock->icon_count > 1);
3436 /* attract icon(s) */
3437 entry = dock->menu->entries[++index];
3438 entry->clientdata = aicon;
3440 dock->menu->flags.realized = 0;
3441 wMenuRealize(dock->menu);
3444 /* launch */
3445 entry = dock->menu->entries[++index];
3446 entry->clientdata = aicon;
3447 wMenuSetEnabled(dock->menu, index, aicon->command!=NULL);
3449 /* unhide here */
3450 entry = dock->menu->entries[++index];
3451 entry->clientdata = aicon;
3452 wMenuSetEnabled(dock->menu, index, appIsRunning);
3454 /* hide */
3455 entry = dock->menu->entries[++index];
3456 entry->clientdata = aicon;
3457 if (aicon->icon->owner) {
3458 wapp = wApplicationOf(aicon->icon->owner->main_window);
3459 if (wapp && wapp->flags.hidden)
3460 entry->text = _("Unhide");
3461 else
3462 entry->text = _("Hide");
3463 } else {
3464 entry->text = _("Hide");
3466 wMenuSetEnabled(dock->menu, index, appIsRunning);
3468 /* settings */
3469 entry = dock->menu->entries[++index];
3470 entry->clientdata = aicon;
3471 wMenuSetEnabled(dock->menu, index, !aicon->editing
3472 && !wPreferences.flags.noupdates);
3474 /* kill */
3475 entry = dock->menu->entries[++index];
3476 entry->clientdata = aicon;
3477 wMenuSetEnabled(dock->menu, index, appIsRunning);
3479 if (!dock->menu->flags.realized)
3480 wMenuRealize(dock->menu);
3482 if (dock->type == WM_CLIP) {
3483 x_pos = event->xbutton.x_root+2;
3484 } else {
3485 x_pos = dock->on_right_side ?
3486 scr->scr_width - dock->menu->frame->core->width - 2 : 0;
3489 wMenuMapAt(dock->menu, x_pos, event->xbutton.y_root+2, False);
3491 /* allow drag select */
3492 event->xany.send_event = True;
3493 desc = &dock->menu->menu->descriptor;
3494 (*desc->handle_mousedown)(desc, event);
3498 static void
3499 openClipWorkspaceMenu(WScreen *scr, int x, int y)
3501 if (!scr->clip_ws_menu) {
3502 scr->clip_ws_menu = wWorkspaceMenuMake(scr, False);
3504 wWorkspaceMenuUpdate(scr, scr->clip_ws_menu);
3505 wMenuMapAt(scr->clip_ws_menu, x, y, False);
3509 /******************************************************************/
3510 static void
3511 iconDblClick(WObjDescriptor *desc, XEvent *event)
3513 WAppIcon *btn = desc->parent;
3514 WDock *dock = btn->dock;
3515 WApplication *wapp = NULL;
3516 int unhideHere = 0;
3518 #ifdef REDUCE_APPICONS
3519 if ((btn->icon->owner && !(event->xbutton.state & ControlMask)) ||
3520 ((btn->icon->owner == NULL) && (btn->applist != NULL))) {
3521 if (btn->icon->owner == NULL)
3522 btn->icon->owner = btn->applist->wapp->main_window_desc;
3523 #else
3524 if (btn->icon->owner && !(event->xbutton.state & ControlMask)) {
3525 #endif
3526 wapp = wApplicationOf(btn->icon->owner->main_window);
3528 assert(wapp!=NULL);
3530 unhideHere = (event->xbutton.state & ShiftMask);
3532 /* go to the last workspace that the user worked on the app */
3533 if (wapp->last_workspace != dock->screen_ptr->current_workspace
3534 && !unhideHere) {
3535 wWorkspaceChange(dock->screen_ptr, wapp->last_workspace);
3538 wUnhideApplication(wapp, event->xbutton.button==Button2,
3539 unhideHere);
3541 if (event->xbutton.state & MOD_MASK) {
3542 wHideOtherApplications(btn->icon->owner);
3544 } else {
3545 if (event->xbutton.button==Button1) {
3547 if (event->xbutton.state & MOD_MASK) {
3548 /* raise/lower dock */
3549 toggleLowered(dock);
3550 } else if (btn == dock->screen_ptr->clip_icon) {
3551 if (getClipButton(event->xbutton.x, event->xbutton.y)==CLIP_IDLE)
3552 toggleCollapsed(dock);
3553 else
3554 handleClipChangeWorkspace(dock->screen_ptr, event);
3555 } else if (btn->command) {
3556 if (!btn->launching &&
3557 (!btn->running || (event->xbutton.state & ControlMask))) {
3558 launchDockedApplication(btn);
3560 } else if (btn->xindex == 0 && btn->yindex == 0
3561 && btn->dock->type == WM_DOCK) {
3563 wShowGNUstepPanel(dock->screen_ptr);
3570 static void
3571 handleDockMove(WDock *dock, WAppIcon *aicon, XEvent *event)
3573 WScreen *scr = dock->screen_ptr;
3574 int ofs_x=event->xbutton.x, ofs_y=event->xbutton.y;
3575 int x, y;
3576 XEvent ev;
3577 int grabbed = 0, swapped = 0, done;
3578 Pixmap ghost = None;
3579 int superfluous = wPreferences.superfluous; /* we catch it to avoid problems */
3581 #ifdef DEBUG
3582 puts("moving dock");
3583 #endif
3584 if (XGrabPointer(dpy, aicon->icon->core->window, True, ButtonMotionMask
3585 |ButtonReleaseMask|ButtonPressMask, GrabModeAsync,
3586 GrabModeAsync, None, None, CurrentTime) !=GrabSuccess) {
3587 wwarning("pointer grab failed for dock move");
3589 y = 0;
3590 for (x=0; x<dock->max_icons; x++) {
3591 if (dock->icon_array[x]!=NULL &&
3592 dock->icon_array[x]->yindex > y)
3593 y = dock->icon_array[x]->yindex;
3595 y++;
3596 XResizeWindow(dpy, scr->dock_shadow, ICON_SIZE, ICON_SIZE*y);
3598 done = 0;
3599 while (!done) {
3600 WMMaskEvent(dpy, PointerMotionMask|ButtonReleaseMask|ButtonPressMask
3601 |ButtonMotionMask|ExposureMask, &ev);
3602 switch (ev.type) {
3603 case Expose:
3604 WMHandleEvent(&ev);
3605 break;
3607 case MotionNotify:
3608 if (!grabbed) {
3609 if (abs(ofs_x-ev.xmotion.x)>=MOVE_THRESHOLD
3610 || abs(ofs_y-ev.xmotion.y)>=MOVE_THRESHOLD) {
3611 XChangeActivePointerGrab(dpy, ButtonMotionMask
3612 |ButtonReleaseMask|ButtonPressMask,
3613 wCursor[WCUR_MOVE], CurrentTime);
3614 grabbed=1;
3616 break;
3618 if (dock->type == WM_CLIP) {
3619 if (ev.xmotion.x_root - ofs_x < 0) {
3620 x = 0;
3621 } else if (ev.xmotion.x_root - ofs_x + ICON_SIZE >
3622 scr->scr_width) {
3623 x = scr->scr_width - ICON_SIZE;
3624 } else {
3625 x = ev.xmotion.x_root - ofs_x;
3627 if (ev.xmotion.y_root - ofs_y < 0) {
3628 y = 0;
3629 } else if (ev.xmotion.y_root - ofs_y + ICON_SIZE >
3630 scr->scr_height) {
3631 y = scr->scr_height - ICON_SIZE;
3632 } else {
3633 y = ev.xmotion.y_root - ofs_y;
3635 moveDock(dock, x, y);
3636 } else {
3637 /* move vertically if pointer is inside the dock*/
3638 if ((dock->on_right_side &&
3639 ev.xmotion.x_root >= dock->x_pos - ICON_SIZE)
3640 || (!dock->on_right_side &&
3641 ev.xmotion.x_root <= dock->x_pos + ICON_SIZE*2)) {
3643 if (ev.xmotion.y_root - ofs_y < 0) {
3644 y = 0;
3645 } else if (ev.xmotion.y_root - ofs_y + ICON_SIZE >
3646 scr->scr_height) {
3647 y = scr->scr_height - ICON_SIZE;
3648 } else {
3649 y = ev.xmotion.y_root - ofs_y;
3651 moveDock(dock, dock->x_pos, y);
3653 /* move horizontally to change sides */
3654 x = ev.xmotion.x_root - ofs_x;
3655 if (!dock->on_right_side) {
3657 /* is on left */
3659 if (ev.xmotion.x_root > dock->x_pos + ICON_SIZE*2) {
3660 XMoveWindow(dpy, scr->dock_shadow, scr->scr_width-ICON_SIZE
3661 -DOCK_EXTRA_SPACE-1, dock->y_pos);
3662 if (superfluous && ghost==None) {
3663 ghost = MakeGhostDock(dock, dock->x_pos,
3664 scr->scr_width-ICON_SIZE
3665 -DOCK_EXTRA_SPACE-1,
3666 dock->y_pos);
3667 XSetWindowBackgroundPixmap(dpy, scr->dock_shadow,
3668 ghost);
3669 XClearWindow(dpy, scr->dock_shadow);
3671 XMapRaised(dpy, scr->dock_shadow);
3672 swapped = 1;
3673 } else {
3674 if (superfluous && ghost!=None) {
3675 XFreePixmap(dpy, ghost);
3676 ghost = None;
3678 XUnmapWindow(dpy, scr->dock_shadow);
3679 swapped = 0;
3681 } else {
3682 /* is on right */
3683 if (ev.xmotion.x_root < dock->x_pos - ICON_SIZE) {
3684 XMoveWindow(dpy, scr->dock_shadow,
3685 DOCK_EXTRA_SPACE, dock->y_pos);
3686 if (superfluous && ghost==None) {
3687 ghost = MakeGhostDock(dock, dock->x_pos,
3688 DOCK_EXTRA_SPACE, dock->y_pos);
3689 XSetWindowBackgroundPixmap(dpy, scr->dock_shadow,
3690 ghost);
3691 XClearWindow(dpy, scr->dock_shadow);
3693 XMapRaised(dpy, scr->dock_shadow);
3694 swapped = -1;
3695 } else {
3696 XUnmapWindow(dpy, scr->dock_shadow);
3697 swapped = 0;
3698 if (superfluous && ghost!=None) {
3699 XFreePixmap(dpy, ghost);
3700 ghost = None;
3705 break;
3707 case ButtonPress:
3708 break;
3710 case ButtonRelease:
3711 if (ev.xbutton.button != event->xbutton.button)
3712 break;
3713 XUngrabPointer(dpy, CurrentTime);
3714 XUnmapWindow(dpy, scr->dock_shadow);
3715 XResizeWindow(dpy, scr->dock_shadow, ICON_SIZE, ICON_SIZE);
3716 if (dock->type == WM_DOCK) {
3717 if (swapped!=0) {
3718 if (swapped>0)
3719 dock->on_right_side = 1;
3720 else
3721 dock->on_right_side = 0;
3722 swapDock(dock);
3723 wArrangeIcons(scr, False);
3726 done = 1;
3727 break;
3730 if (superfluous) {
3731 if (ghost!=None)
3732 XFreePixmap(dpy, ghost);
3733 XSetWindowBackground(dpy, scr->dock_shadow, scr->white_pixel);
3735 #ifdef DEBUG
3736 puts("End dock move");
3737 #endif
3742 static void
3743 handleIconMove(WDock *dock, WAppIcon *aicon, XEvent *event)
3745 WScreen *scr = dock->screen_ptr;
3746 Window wins[2];
3747 WIcon *icon = aicon->icon;
3748 WDock *dock2 = NULL, *last_dock = dock, *clip = NULL;
3749 int ondock, grabbed = 0, change_dock = 0, collapsed = 0;
3750 XEvent ev;
3751 int x = aicon->x_pos, y = aicon->y_pos;
3752 int ofs_x = event->xbutton.x, ofs_y = event->xbutton.y;
3753 int shad_x = x, shad_y = y;
3754 int ix = aicon->xindex, iy = aicon->yindex;
3755 int tmp;
3756 Pixmap ghost = None;
3757 Bool docked;
3758 int superfluous = wPreferences.superfluous; /* we catch it to avoid problems */
3759 int omnipresent = aicon->omnipresent; /* this must be cached!!! */
3762 if (wPreferences.flags.noupdates)
3763 return;
3765 if (XGrabPointer(dpy, icon->core->window, True, ButtonMotionMask
3766 |ButtonReleaseMask|ButtonPressMask, GrabModeAsync,
3767 GrabModeAsync, None, None, CurrentTime) !=GrabSuccess) {
3768 #ifdef DEBUG0
3769 wwarning("pointer grab failed for icon move");
3770 #endif
3773 if (!(event->xbutton.state & MOD_MASK))
3774 wRaiseFrame(icon->core);
3776 if (!wPreferences.flags.noclip)
3777 clip = scr->workspaces[scr->current_workspace]->clip;
3779 if (dock == scr->dock && !wPreferences.flags.noclip)
3780 dock2 = clip;
3781 else if (dock != scr->dock && !wPreferences.flags.nodock)
3782 dock2 = scr->dock;
3784 wins[0] = icon->core->window;
3785 wins[1] = scr->dock_shadow;
3786 XRestackWindows(dpy, wins, 2);
3787 XMoveResizeWindow(dpy, scr->dock_shadow, aicon->x_pos, aicon->y_pos,
3788 ICON_SIZE, ICON_SIZE);
3789 if (superfluous) {
3790 if (icon->pixmap!=None)
3791 ghost = MakeGhostIcon(scr, icon->pixmap);
3792 else
3793 ghost = MakeGhostIcon(scr, icon->core->window);
3795 XSetWindowBackgroundPixmap(dpy, scr->dock_shadow, ghost);
3796 XClearWindow(dpy, scr->dock_shadow);
3798 XMapWindow(dpy, scr->dock_shadow);
3800 ondock = 1;
3803 while(1) {
3804 XMaskEvent(dpy, PointerMotionMask|ButtonReleaseMask|ButtonPressMask
3805 |ButtonMotionMask|ExposureMask, &ev);
3806 switch (ev.type) {
3807 case Expose:
3808 WMHandleEvent(&ev);
3809 break;
3811 case MotionNotify:
3812 if (!grabbed) {
3813 if (abs(ofs_x-ev.xmotion.x)>=MOVE_THRESHOLD
3814 || abs(ofs_y-ev.xmotion.y)>=MOVE_THRESHOLD) {
3815 XChangeActivePointerGrab(dpy, ButtonMotionMask
3816 |ButtonReleaseMask|ButtonPressMask,
3817 wCursor[WCUR_MOVE], CurrentTime);
3818 grabbed=1;
3819 } else {
3820 break;
3824 if (omnipresent) {
3825 int i;
3826 for (i=0; i<scr->workspace_count; i++) {
3827 if (i == scr->current_workspace)
3828 continue;
3829 wDockShowIcons(scr->workspaces[i]->clip);
3833 x = ev.xmotion.x_root - ofs_x;
3834 y = ev.xmotion.y_root - ofs_y;
3835 tmp = wDockSnapIcon(dock, aicon, x, y, &ix, &iy, True);
3836 if (tmp && dock2) {
3837 change_dock = 0;
3838 if (last_dock != dock && collapsed) {
3839 last_dock->collapsed = 1;
3840 wDockHideIcons(last_dock);
3841 collapsed = 0;
3843 if (!collapsed && (collapsed = dock->collapsed)) {
3844 dock->collapsed = 0;
3845 wDockShowIcons(dock);
3847 if (dock->auto_raise_lower)
3848 wDockRaise(dock);
3849 last_dock = dock;
3850 } else if (dock2) {
3851 tmp = wDockSnapIcon(dock2, aicon, x, y, &ix, &iy, False);
3852 if (tmp) {
3853 change_dock = 1;
3854 if (last_dock != dock2 && collapsed) {
3855 last_dock->collapsed = 1;
3856 wDockHideIcons(last_dock);
3857 collapsed = 0;
3859 if (!collapsed && (collapsed = dock2->collapsed)) {
3860 dock2->collapsed = 0;
3861 wDockShowIcons(dock2);
3863 if (dock2->auto_raise_lower)
3864 wDockRaise(dock2);
3865 last_dock = dock2;
3868 if (aicon->launching
3869 || aicon->lock
3870 || (aicon->running && !(ev.xmotion.state & MOD_MASK))
3871 || (!aicon->running && tmp)) {
3872 shad_x = last_dock->x_pos + ix*wPreferences.icon_size;
3873 shad_y = last_dock->y_pos + iy*wPreferences.icon_size;
3875 XMoveWindow(dpy, scr->dock_shadow, shad_x, shad_y);
3877 if (!ondock) {
3878 XMapWindow(dpy, scr->dock_shadow);
3880 ondock = 1;
3881 } else {
3882 if (ondock) {
3883 XUnmapWindow(dpy, scr->dock_shadow);
3885 ondock = 0;
3887 XMoveWindow(dpy, icon->core->window, x, y);
3888 break;
3890 case ButtonPress:
3891 break;
3893 case ButtonRelease:
3894 if (ev.xbutton.button != event->xbutton.button)
3895 break;
3896 XUngrabPointer(dpy, CurrentTime);
3897 if (ondock) {
3898 SlideWindow(icon->core->window, x, y, shad_x, shad_y);
3899 XUnmapWindow(dpy, scr->dock_shadow);
3900 if (!change_dock) {
3901 reattachIcon(dock, aicon, ix, iy);
3902 if (clip && dock!=clip && clip->auto_raise_lower)
3903 wDockLower(clip);
3904 } else {
3905 docked = moveIconBetweenDocks(dock, dock2, aicon, ix, iy);
3906 if (!docked) {
3907 /* Slide it back if dock rejected it */
3908 SlideWindow(icon->core->window, x, y, aicon->x_pos,
3909 aicon->y_pos);
3910 reattachIcon(dock, aicon, aicon->xindex,aicon->yindex);
3912 if (last_dock->type==WM_CLIP && last_dock->auto_collapse) {
3913 collapsed = 0;
3916 } else {
3917 aicon->x_pos = x;
3918 aicon->y_pos = y;
3919 if (superfluous) {
3920 if (!aicon->running && !wPreferences.no_animations) {
3921 /* We need to deselect it, even if is deselected in
3922 * wDockDetach(), because else DoKaboom() will fail.
3924 if (aicon->icon->selected)
3925 wIconSelect(aicon->icon);
3927 #ifdef WMSOUND
3928 wSoundPlay(WMSOUND_KABOOM);
3929 #endif
3930 DoKaboom(scr,aicon->icon->core->window, x, y);
3931 #ifdef WMSOUND
3932 } else {
3933 wSoundPlay(WMSOUND_UNDOCK);
3934 #endif
3936 #ifdef WMSOUND
3937 } else {
3938 wSoundPlay(WMSOUND_UNDOCK);
3939 #endif
3941 if (clip && clip->auto_raise_lower)
3942 wDockLower(clip);
3943 wDockDetach(dock, aicon);
3945 if (collapsed) {
3946 last_dock->collapsed = 1;
3947 wDockHideIcons(last_dock);
3948 collapsed = 0;
3950 if (superfluous) {
3951 if (ghost!=None)
3952 XFreePixmap(dpy, ghost);
3953 XSetWindowBackground(dpy, scr->dock_shadow, scr->white_pixel);
3955 if (omnipresent) {
3956 int i;
3957 for (i=0; i<scr->workspace_count; i++) {
3958 if (i == scr->current_workspace)
3959 continue;
3960 wDockHideIcons(scr->workspaces[i]->clip);
3964 #ifdef DEBUG
3965 puts("End icon move");
3966 #endif
3967 return;
3973 static int
3974 getClipButton(int px, int py)
3976 int pt = (CLIP_BUTTON_SIZE+2)*ICON_SIZE/64;
3978 if (px < 0 || py < 0 || px >= ICON_SIZE || py >= ICON_SIZE)
3979 return CLIP_IDLE;
3981 if (py <= pt-((int)ICON_SIZE-1-px))
3982 return CLIP_FORWARD;
3983 else if (px <= pt-((int)ICON_SIZE-1-py))
3984 return CLIP_REWIND;
3986 return CLIP_IDLE;
3990 static void
3991 handleClipChangeWorkspace(WScreen *scr, XEvent *event)
3993 XEvent ev;
3994 int done, direction, new_ws;
3995 int new_dir;
3996 WDock *clip = scr->clip_icon->dock;
3998 direction = getClipButton(event->xbutton.x, event->xbutton.y);
4000 clip->lclip_button_pushed = direction==CLIP_REWIND;
4001 clip->rclip_button_pushed = direction==CLIP_FORWARD;
4003 wClipIconPaint(scr->clip_icon);
4004 done = 0;
4005 while(!done) {
4006 WMMaskEvent(dpy, ExposureMask|ButtonMotionMask|ButtonReleaseMask
4007 |ButtonPressMask, &ev);
4008 switch (ev.type) {
4009 case Expose:
4010 WMHandleEvent(&ev);
4011 break;
4013 case MotionNotify:
4014 new_dir = getClipButton(ev.xmotion.x, ev.xmotion.y);
4015 if (new_dir != direction) {
4016 direction = new_dir;
4017 clip->lclip_button_pushed = direction==CLIP_REWIND;
4018 clip->rclip_button_pushed = direction==CLIP_FORWARD;
4019 wClipIconPaint(scr->clip_icon);
4021 break;
4023 case ButtonPress:
4024 break;
4026 case ButtonRelease:
4027 if (ev.xbutton.button == event->xbutton.button)
4028 done = 1;
4032 clip->lclip_button_pushed = 0;
4033 clip->rclip_button_pushed = 0;
4035 new_ws = wPreferences.ws_advance || (event->xbutton.state & ControlMask);
4037 if (direction == CLIP_FORWARD) {
4038 if (scr->current_workspace < scr->workspace_count-1)
4039 wWorkspaceChange(scr, scr->current_workspace+1);
4040 else if (new_ws && scr->current_workspace < MAX_WORKSPACES-1)
4041 wWorkspaceChange(scr, scr->current_workspace+1);
4042 else if (wPreferences.ws_cycle)
4043 wWorkspaceChange(scr, 0);
4045 else if (direction == CLIP_REWIND) {
4046 if (scr->current_workspace > 0)
4047 wWorkspaceChange(scr, scr->current_workspace-1);
4048 else if (scr->current_workspace==0 && wPreferences.ws_cycle)
4049 wWorkspaceChange(scr, scr->workspace_count-1);
4052 wClipIconPaint(scr->clip_icon);
4056 static void
4057 iconMouseDown(WObjDescriptor *desc, XEvent *event)
4059 WAppIcon *aicon = desc->parent;
4060 WDock *dock = aicon->dock;
4061 WScreen *scr = aicon->icon->core->screen_ptr;
4063 if (aicon->editing || WCHECK_STATE(WSTATE_MODAL))
4064 return;
4066 scr->last_dock = dock;
4068 if (dock->menu->flags.mapped)
4069 wMenuUnmap(dock->menu);
4071 if (IsDoubleClick(scr, event)) {
4072 /* double-click was not in the main clip icon */
4073 if (dock->type != WM_CLIP || aicon->xindex!=0 || aicon->yindex!=0
4074 || getClipButton(event->xbutton.x, event->xbutton.y)==CLIP_IDLE) {
4075 iconDblClick(desc, event);
4076 return;
4080 if (dock->type == WM_CLIP && scr->flags.clip_balloon_mapped) {
4081 XUnmapWindow(dpy, scr->clip_balloon);
4082 scr->flags.clip_balloon_mapped = 0;
4085 #ifdef DEBUG
4086 puts("handling dock");
4087 #endif
4088 if (event->xbutton.button == Button1) {
4089 if (event->xbutton.state & MOD_MASK)
4090 wDockLower(dock);
4091 else
4092 wDockRaise(dock);
4094 if ((event->xbutton.state & ShiftMask) && aicon!=scr->clip_icon &&
4095 dock->type!=WM_DOCK) {
4096 wIconSelect(aicon->icon);
4097 return;
4100 if (aicon->yindex==0 && aicon->xindex==0) {
4101 if (getClipButton(event->xbutton.x, event->xbutton.y)!=CLIP_IDLE
4102 && dock->type==WM_CLIP)
4103 handleClipChangeWorkspace(scr, event);
4104 else
4105 handleDockMove(dock, aicon, event);
4106 } else
4107 handleIconMove(dock, aicon, event);
4109 } else if (event->xbutton.button==Button2 && dock->type==WM_CLIP &&
4110 aicon==scr->clip_icon) {
4111 openClipWorkspaceMenu(scr, event->xbutton.x_root+2,
4112 event->xbutton.y_root+2);
4113 if (scr->clip_ws_menu) {
4114 WMenu *menu;
4115 menu = scr->clip_ws_menu;
4116 desc = &menu->menu->descriptor;
4118 event->xany.send_event = True;
4119 (*desc->handle_mousedown)(desc, event);
4121 } else if (event->xbutton.button==Button2 && dock->type==WM_CLIP &&
4122 (event->xbutton.state & ShiftMask) && aicon!=scr->clip_icon) {
4123 wClipMakeIconOmnipresent(aicon, !aicon->omnipresent);
4124 } else if (event->xbutton.button == Button3) {
4125 openDockMenu(dock, aicon, event);
4130 static void
4131 showClipBalloon(WDock *dock, int workspace)
4133 int w, h;
4134 int x, y;
4135 WScreen *scr = dock->screen_ptr;
4136 char *text;
4137 Window stack[2];
4139 scr->flags.clip_balloon_mapped = 1;
4140 XMapWindow(dpy, scr->clip_balloon);
4142 text = scr->workspaces[workspace]->name;
4144 w = WMWidthOfString(scr->clip_title_font, text, strlen(text));
4146 h = WMFontHeight(scr->clip_title_font);
4147 XResizeWindow(dpy, scr->clip_balloon, w, h);
4149 x = dock->x_pos + CLIP_BUTTON_SIZE*ICON_SIZE/64;
4150 y = dock->y_pos + ICON_SIZE - WMFontHeight(scr->clip_title_font) - 3;
4152 if (x+w > scr->scr_width) {
4153 x = scr->scr_width - w;
4154 if (dock->y_pos + ICON_SIZE + h > scr->scr_height)
4155 y = dock->y_pos - h - 1;
4156 else
4157 y = dock->y_pos + ICON_SIZE;
4158 XRaiseWindow(dpy, scr->clip_balloon);
4159 } else {
4160 stack[0] = scr->clip_icon->icon->core->window;
4161 stack[1] = scr->clip_balloon;
4162 XRestackWindows(dpy, stack, 2);
4164 XMoveWindow(dpy, scr->clip_balloon, x, y);
4165 XSetForeground(dpy, scr->clip_title_gc,
4166 scr->clip_title_pixel[CLIP_NORMAL]);
4167 XClearWindow(dpy, scr->clip_balloon);
4168 WMDrawString(scr->wmscreen, scr->clip_balloon, scr->clip_title_gc,
4169 scr->clip_title_font, 0, 0, text, strlen(text));
4173 static void
4174 clipEnterNotify(WObjDescriptor *desc, XEvent *event)
4176 WAppIcon *btn = (WAppIcon*)desc->parent;
4177 WDock *dock;
4178 WScreen *scr;
4180 assert(event->type==EnterNotify);
4182 if(desc->parent_type!=WCLASS_DOCK_ICON)
4183 return;
4185 scr = btn->icon->core->screen_ptr;
4186 if (!btn->omnipresent)
4187 dock = btn->dock;
4188 else
4189 dock = scr->workspaces[scr->current_workspace]->clip;
4191 if (!dock || dock->type!=WM_CLIP)
4192 return;
4194 /* The auto raise/lower code */
4195 if (dock->auto_lower_magic) {
4196 WMDeleteTimerHandler(dock->auto_lower_magic);
4197 dock->auto_lower_magic = NULL;
4199 if (dock->auto_raise_lower && !dock->auto_raise_magic) {
4200 dock->auto_raise_magic = WMAddTimerHandler(AUTO_RAISE_DELAY,
4201 clipAutoRaise,
4202 (void *)dock);
4205 /* The auto expand/collapse code */
4206 if (dock->auto_collapse_magic) {
4207 WMDeleteTimerHandler(dock->auto_collapse_magic);
4208 dock->auto_collapse_magic = NULL;
4210 if (dock->auto_collapse && !dock->auto_expand_magic) {
4211 dock->auto_expand_magic = WMAddTimerHandler(AUTO_EXPAND_DELAY,
4212 clipAutoExpand,
4213 (void *)dock);
4216 if (btn->xindex == 0 && btn->yindex == 0)
4217 showClipBalloon(dock, dock->screen_ptr->current_workspace);
4218 else {
4219 if (dock->screen_ptr->flags.clip_balloon_mapped) {
4220 XUnmapWindow(dpy, dock->screen_ptr->clip_balloon);
4221 dock->screen_ptr->flags.clip_balloon_mapped = 0;
4227 static void
4228 clipLeave(WDock *dock)
4230 XEvent event;
4231 WObjDescriptor *desc = NULL;
4233 if (!dock || dock->type!=WM_CLIP)
4234 return;
4236 if (XCheckTypedEvent(dpy, EnterNotify, &event)!=False) {
4237 if (XFindContext(dpy, event.xcrossing.window, wWinContext,
4238 (XPointer *)&desc)!=XCNOENT
4239 && desc && desc->parent_type==WCLASS_DOCK_ICON
4240 && ((WAppIcon*)desc->parent)->dock
4241 && ((WAppIcon*)desc->parent)->dock->type==WM_CLIP) {
4242 /* We didn't left the Clip yet */
4243 XPutBackEvent(dpy, &event);
4244 return;
4247 XPutBackEvent(dpy, &event);
4248 } else {
4249 /* We entered a withdrawn window, so we're still in Clip */
4250 return;
4253 if (dock->auto_raise_magic) {
4254 WMDeleteTimerHandler(dock->auto_raise_magic);
4255 dock->auto_raise_magic = NULL;
4257 if (dock->auto_raise_lower && !dock->auto_lower_magic) {
4258 dock->auto_lower_magic = WMAddTimerHandler(AUTO_LOWER_DELAY,
4259 clipAutoLower,
4260 (void *)dock);
4263 if (dock->auto_expand_magic) {
4264 WMDeleteTimerHandler(dock->auto_expand_magic);
4265 dock->auto_expand_magic = NULL;
4267 if (dock->auto_collapse && !dock->auto_collapse_magic) {
4268 dock->auto_collapse_magic = WMAddTimerHandler(AUTO_COLLAPSE_DELAY,
4269 clipAutoCollapse,
4270 (void *)dock);
4275 static void
4276 clipLeaveNotify(WObjDescriptor *desc, XEvent *event)
4278 WAppIcon *btn = (WAppIcon*)desc->parent;
4280 assert(event->type==LeaveNotify);
4282 if(desc->parent_type!=WCLASS_DOCK_ICON)
4283 return;
4285 clipLeave(btn->dock);
4289 static void
4290 clipAutoCollapse(void *cdata)
4292 WDock *dock = (WDock *)cdata;
4294 if (dock->type!=WM_CLIP)
4295 return;
4297 if (dock->auto_collapse) {
4298 dock->collapsed = 1;
4299 wDockHideIcons(dock);
4301 dock->auto_collapse_magic = NULL;
4305 static void
4306 clipAutoExpand(void *cdata)
4308 WDock *dock = (WDock *)cdata;
4310 if (dock->type!=WM_CLIP)
4311 return;
4313 if (dock->auto_collapse) {
4314 dock->collapsed = 0;
4315 wDockShowIcons(dock);
4317 dock->auto_expand_magic = NULL;
4321 static void
4322 clipAutoLower(void *cdata)
4324 WDock *dock = (WDock *)cdata;
4326 if (dock->type!=WM_CLIP)
4327 return;
4329 if (dock->auto_raise_lower)
4330 wDockLower(dock);
4332 dock->auto_lower_magic = NULL;
4336 static void
4337 clipAutoRaise(void *cdata)
4339 WDock *dock = (WDock *)cdata;
4341 if (dock->type!=WM_CLIP)
4342 return;
4344 if (dock->auto_raise_lower)
4345 wDockRaise(dock);
4347 if (dock->screen_ptr->flags.clip_balloon_mapped) {
4348 showClipBalloon(dock, dock->screen_ptr->current_workspace);
4351 dock->auto_raise_magic = NULL;
4355 static Bool
4356 iconCanBeOmnipresent(WAppIcon *aicon)
4358 WScreen *scr = aicon->icon->core->screen_ptr;
4359 WDock *clip;
4360 WAppIcon *btn;
4361 int i, j;
4363 for (i=0; i<scr->workspace_count; i++) {
4364 clip = scr->workspaces[i]->clip;
4366 if (clip == aicon->dock)
4367 continue;
4369 if (clip->icon_count + scr->global_icon_count >= clip->max_icons)
4370 return False; /* Clip is full in some workspace */
4372 for (j=0; j<clip->max_icons; j++) {
4373 btn = clip->icon_array[j];
4374 if(btn && btn->xindex==aicon->xindex && btn->yindex==aicon->yindex)
4375 return False;
4379 return True;
4384 wClipMakeIconOmnipresent(WAppIcon *aicon, int omnipresent)
4386 WScreen *scr = aicon->icon->core->screen_ptr;
4387 WAppIconChain *new_entry, *tmp, *tmp1;
4388 int status = WO_SUCCESS;
4390 if ((scr->dock && aicon->dock==scr->dock) || aicon==scr->clip_icon) {
4391 return WO_NOT_APPLICABLE;
4394 if (aicon->omnipresent == omnipresent)
4395 return WO_SUCCESS;
4397 if (omnipresent) {
4398 if (iconCanBeOmnipresent(aicon)) {
4399 aicon->omnipresent = 1;
4400 new_entry = wmalloc(sizeof(WAppIconChain));
4401 new_entry->aicon = aicon;
4402 new_entry->next = scr->global_icons;
4403 scr->global_icons = new_entry;
4404 scr->global_icon_count++;
4405 } else {
4406 aicon->omnipresent = 0;
4407 status = WO_FAILED;
4409 } else {
4410 aicon->omnipresent = 0;
4411 if (aicon == scr->global_icons->aicon) {
4412 tmp = scr->global_icons->next;
4413 free(scr->global_icons);
4414 scr->global_icons = tmp;
4415 scr->global_icon_count--;
4416 } else {
4417 tmp = scr->global_icons;
4418 while (tmp->next) {
4419 if (tmp->next->aicon == aicon) {
4420 tmp1 = tmp->next->next;
4421 free(tmp->next);
4422 tmp->next = tmp1;
4423 scr->global_icon_count--;
4424 break;
4426 tmp = tmp->next;
4431 wAppIconPaint(aicon);
4433 return status;