added new sound events
[wmaker-crm.git] / src / appicon.c
blob17e5f83bb61747823f93e8a66f19a31106b5c71b
1 /* appicon.c- icon for applications (not mini-window)
3 * Window Maker window manager
5 * Copyright (c) 1997, 1998 Alfredo K. Kojima
6 * Copyright (c) 1998 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.
24 #include "wconfig.h"
26 #include <X11/Xlib.h>
27 #include <X11/Xutil.h>
28 #include <stdlib.h>
29 #include <string.h>
31 #include "WindowMaker.h"
32 #include "wcore.h"
33 #include "window.h"
34 #include "icon.h"
35 #include "appicon.h"
36 #include "actions.h"
37 #include "stacking.h"
38 #include "dock.h"
39 #include "funcs.h"
40 #include "defaults.h"
41 #include "workspace.h"
42 #include "superfluous.h"
43 #include "menu.h"
44 #include "framewin.h"
45 #include "dialog.h"
46 #include "client.h"
47 #ifdef XDND
48 #include "xdnd.h"
49 #endif
50 #include "wmsound.h"
51 #include "WINGsP.h"
55 * icon_file for the dock is got from the preferences file by
56 * using the classname/instancename
59 /**** Global variables ****/
60 extern Cursor wCursor[WCUR_LAST];
61 extern WPreferences wPreferences;
63 #define MOD_MASK wPreferences.modifier_mask
65 void appIconMouseDown(WObjDescriptor *desc, XEvent *event);
66 static void iconDblClick(WObjDescriptor *desc, XEvent *event);
67 static void iconExpose(WObjDescriptor *desc, XEvent *event);
71 WAppIcon*
72 wAppIconCreateForDock(WScreen *scr, char *command, char *wm_instance,
73 char *wm_class, int tile)
75 WAppIcon *dicon;
76 char *path;
78 dicon = wmalloc(sizeof(WAppIcon));
79 wretain(dicon);
80 memset(dicon, 0, sizeof(WAppIcon));
81 dicon->yindex = -1;
82 dicon->xindex = -1;
84 dicon->prev = NULL;
85 dicon->next = scr->app_icon_list;
86 if (scr->app_icon_list) {
87 scr->app_icon_list->prev = dicon;
89 scr->app_icon_list = dicon;
91 if (command) {
92 dicon->command = wstrdup(command);
94 if (wm_class)
95 dicon->wm_class = wstrdup(wm_class);
96 if (wm_instance)
97 dicon->wm_instance = wstrdup(wm_instance);
99 path = wDefaultGetIconFile(scr, wm_instance, wm_class, True);
100 if (!path && command) {
101 wApplicationExtractDirPackIcon(scr, command, wm_instance, wm_class);
103 path = wDefaultGetIconFile(scr, wm_instance, wm_class, False);
106 if (path)
107 path = FindImage(wPreferences.icon_path, path);
109 dicon->icon = wIconCreateWithIconFile(scr, path, tile);
110 if (path)
111 free(path);
112 #ifdef XDND
113 wXDNDMakeAwareness(dicon->icon->core->window);
114 #endif
115 #ifdef REDUCE_APPICONS
116 dicon->num_apps = 0;
117 #endif
118 #ifdef DEMATERIALIZE_ICON
120 XSetWindowAttributes attribs;
121 attribs.save_under = True;
122 XChangeWindowAttributes(dpy, dicon->icon->core->window,
123 CWSaveUnder, &attribs);
125 #endif
127 /* will be overriden by dock */
128 dicon->icon->core->descriptor.handle_mousedown = appIconMouseDown;
129 dicon->icon->core->descriptor.handle_expose = iconExpose;
130 dicon->icon->core->descriptor.parent_type = WCLASS_APPICON;
131 dicon->icon->core->descriptor.parent = dicon;
132 AddToStackList(dicon->icon->core);
134 return dicon;
139 WAppIcon*
140 wAppIconCreate(WWindow *leader_win)
142 WAppIcon *aicon;
143 #ifdef REDUCE_APPICONS
144 WAppIcon *atmp;
145 WAppIconAppList *applist;
146 char *tinstance, *tclass;
147 #endif
148 WScreen *scr = leader_win->screen_ptr;
150 aicon = wmalloc(sizeof(WAppIcon));
151 wretain(aicon);
152 memset(aicon, 0, sizeof(WAppIcon));
153 #ifdef REDUCE_APPICONS
154 applist = wmalloc(sizeof(WAppIconAppList));
155 memset(applist, 0, sizeof(WAppIconAppList));
156 applist->wapp = wApplicationOf(leader_win->main_window);
157 aicon->applist = applist;
158 if (applist->wapp == NULL) {
159 /* Something's wrong. wApplicationOf() should always return a
160 * valid structure. Rather than violate assumptions, bail. -cls
162 free(applist);
163 wrelease(aicon);
164 return NULL;
166 #endif
168 aicon->yindex = -1;
169 aicon->xindex = -1;
171 aicon->prev = NULL;
172 aicon->next = scr->app_icon_list;
173 if (scr->app_icon_list) {
174 #ifndef REDUCE_APPICONS
175 scr->app_icon_list->prev = aicon;
176 #else
177 /* If we aren't going to have a match, jump straight to new appicon */
178 if (leader_win->wm_class == NULL || leader_win->wm_class == NULL)
179 atmp = NULL;
180 else
181 atmp = scr->app_icon_list;
183 while (atmp != NULL) {
184 if ((tinstance = atmp->wm_instance) == NULL)
185 tinstance = "";
186 if ((tclass = atmp->wm_class) == NULL)
187 tclass = "";
188 if ((strcmp(leader_win->wm_class, tclass) == 0) &&
189 (strcmp(leader_win->wm_instance, tinstance) == 0))
191 /* We have a winner */
192 wrelease(aicon);
193 atmp->num_apps++;
194 applist->next = atmp->applist;
195 if (atmp->applist)
196 atmp->applist->prev = applist;
197 atmp->applist = applist;
199 if (atmp->docked) {
200 wDockSimulateLaunch(atmp->dock, atmp);
203 return atmp;
205 atmp = atmp->next;
207 if (atmp == NULL) {
208 scr->app_icon_list->prev = aicon;
210 #endif /* REDUCE_APPICONS */
212 scr->app_icon_list = aicon;
214 if (leader_win->wm_class)
215 aicon->wm_class = wstrdup(leader_win->wm_class);
216 if (leader_win->wm_instance)
217 aicon->wm_instance = wstrdup(leader_win->wm_instance);
218 #ifdef REDUCE_APPICONS
219 aicon->num_apps = 1;
220 #endif
222 aicon->icon = wIconCreate(leader_win);
223 #ifdef DEMATERIALIZE_ICON
225 XSetWindowAttributes attribs;
226 attribs.save_under = True;
227 XChangeWindowAttributes(dpy, aicon->icon->core->window,
228 CWSaveUnder, &attribs);
230 #endif
231 #ifdef XDND
232 wXDNDMakeAwareness(aicon->icon->core->window);
233 #endif
235 /* will be overriden if docked */
236 aicon->icon->core->descriptor.handle_mousedown = appIconMouseDown;
237 aicon->icon->core->descriptor.handle_expose = iconExpose;
238 aicon->icon->core->descriptor.parent_type = WCLASS_APPICON;
239 aicon->icon->core->descriptor.parent = aicon;
240 AddToStackList(aicon->icon->core);
241 aicon->icon->show_title = 0;
242 wIconUpdate(aicon->icon);
244 return aicon;
248 void
249 wAppIconDestroy(WAppIcon *aicon)
251 WScreen *scr = aicon->icon->core->screen_ptr;
252 #ifdef REDUCE_APPICONS
253 WAppIconAppList *aptmp;
254 #endif
256 RemoveFromStackList(aicon->icon->core);
257 wIconDestroy(aicon->icon);
258 if (aicon->command)
259 free(aicon->command);
260 #ifdef OFFIX_DND
261 if (aicon->dnd_command)
262 free(aicon->dnd_command);
263 #endif
264 if (aicon->wm_instance)
265 free(aicon->wm_instance);
266 if (aicon->wm_class)
267 free(aicon->wm_class);
268 #ifdef REDUCE_APPICONS
269 /* There should never be a list but just in case */
270 if (aicon->applist != NULL) {
271 aptmp = aicon->applist;
272 while (aptmp->next) {
273 aptmp = aptmp->next;
274 free(aptmp->prev);
276 free(aptmp);
278 #endif
280 if (aicon == scr->app_icon_list) {
281 if (aicon->next)
282 aicon->next->prev = NULL;
283 scr->app_icon_list = aicon->next;
285 else {
286 if (aicon->next)
287 aicon->next->prev = aicon->prev;
288 if (aicon->prev)
289 aicon->prev->next = aicon->next;
292 aicon->destroyed = 1;
293 wrelease(aicon);
298 #ifdef NEWAPPICON
299 static void
300 drawCorner(WIcon *icon, WWindow *wwin, int active)
302 WScreen *scr = wwin->screen_ptr;
303 XPoint points[3];
304 GC gc;
306 points[0].x = 2;
307 points[0].y = 2;
308 points[1].x = 12;
309 points[1].y = 2;
310 points[2].x = 2;
311 points[2].y = 12;
312 if (active) {
313 gc=scr->focused_texture->any.gc;
314 } else {
315 gc=scr->unfocused_texture->any.gc;
317 XFillPolygon(dpy, icon->core->window, gc, points, 3,
318 Convex, CoordModeOrigin);
320 #endif /* NEWAPPICON */
323 static void
324 drawCorner(WIcon *icon)
326 WScreen *scr = icon->core->screen_ptr;
327 XPoint points[3];
329 points[0].x = 1;
330 points[0].y = 1;
331 points[1].x = 12;
332 points[1].y = 1;
333 points[2].x = 1;
334 points[2].y = 12;
335 XFillPolygon(dpy, icon->core->window, scr->icon_title_texture->normal_gc,
336 points, 3, Convex, CoordModeOrigin);
337 XDrawLine(dpy, icon->core->window, scr->icon_title_texture->light_gc,
338 0, 0, 0, 12);
339 XDrawLine(dpy, icon->core->window, scr->icon_title_texture->light_gc,
340 0, 0, 12, 0);
341 /* drawing the second line gives a weird concave look. -Dan */
342 #if 0
343 XDrawLine(dpy, icon->core->window, scr->icon_title_texture->light_gc,
344 1, 1, 1, 11);
345 XDrawLine(dpy, icon->core->window, scr->icon_title_texture->light_gc,
346 1, 1, 11, 1);
347 #endif
351 void
352 wAppIconMove(WAppIcon *aicon, int x, int y)
354 XMoveWindow(dpy, aicon->icon->core->window, x, y);
355 aicon->x_pos = x;
356 aicon->y_pos = y;
360 #ifdef WS_INDICATOR
361 static void
362 updateDockNumbers(WScreen *scr)
364 int length;
365 char *ws_numbers;
366 GC numbers_gc;
367 XGCValues my_gc_values;
368 unsigned long my_v_mask = (GCForeground);
369 WAppIcon *dicon = scr->dock->icon_array[0];
371 my_gc_values.foreground = scr->white_pixel;
372 numbers_gc = XCreateGC(dpy, dicon->icon->core->window,
373 my_v_mask, &my_gc_values);
375 ws_numbers = malloc(20);
376 sprintf(ws_numbers, "%i [ %i ]", scr->current_workspace+1,
377 ((scr->current_workspace/10)+1));
378 length = strlen(ws_numbers);
380 XClearArea(dpy, dicon->icon->core->window, 2, 2, 50,
381 WMFontHeight(scr->icon_title_font)+1, False);
383 XSetForeground(dpy, numbers_gc, scr->black_pixel);
384 WMDrawString(scr->wmscreen, dicon->icon->core->window, numbers_gc,
385 scr->icon_title_font, 4, 3, ws_numbers, length);
387 XSetForeground(dpy, numbers_gc, scr->white_pixel);
388 WMDrawString(scr->wmscreen, dicon->icon->core->window, numbers_gc,
389 scr->icon_title_font, 3, 2, ws_numbers, length);
391 XFreeGC(dpy, numbers_gc);
392 free(ws_numbers);
394 #endif /* WS_INDICATOR */
397 void
398 wAppIconPaint(WAppIcon *aicon)
400 WScreen *scr = aicon->icon->core->screen_ptr;
402 wIconPaint(aicon->icon);
405 # ifdef WS_INDICATOR
406 if (aicon->docked && scr->dock && scr->dock==aicon->dock &&
407 aicon->yindex==0)
408 updateDockNumbers(scr);
409 # endif
410 if (scr->dock_dots && aicon->docked && !aicon->running
411 && aicon->command!=NULL) {
412 XSetClipMask(dpy, scr->copy_gc, scr->dock_dots->mask);
413 XSetClipOrigin(dpy, scr->copy_gc, 0, 0);
414 XCopyArea(dpy, scr->dock_dots->image, aicon->icon->core->window,
415 scr->copy_gc, 0, 0, scr->dock_dots->width,
416 scr->dock_dots->height, 0, 0);
419 #ifdef HIDDENDOT
421 WApplication *wapp;
422 wapp = wApplicationOf(aicon->main_window);
423 if (wapp && wapp->flags.hidden) {
424 XSetClipMask(dpy, scr->copy_gc, scr->dock_dots->mask);
425 XSetClipOrigin(dpy, scr->copy_gc, 0, 0);
426 XCopyArea(dpy, scr->dock_dots->image,
427 aicon->icon->core->window,
428 scr->copy_gc, 0, 0, 7,
429 scr->dock_dots->height, 0, 0);
432 #endif /* HIDDENDOT */
434 #ifdef NEWAPPICON
435 if (!wPreferences.strict_ns && aicon->icon->owner!=NULL) {
436 int active=0;
437 if (aicon->main_window==None) {
438 active = (aicon->icon->owner->flags.focused ? 1 : 0);
439 } else {
440 WApplication *wapp;
442 wapp = wApplicationOf(aicon->main_window);
443 if (!wapp) {
444 active = aicon->icon->owner->flags.focused;
445 wwarning("error in wAppIconPaint(). Please report it");
446 } else {
447 active = wapp->main_window_desc->flags.focused;
448 if (wapp->main_window_desc->flags.hidden
449 || !wapp->main_window_desc->flags.mapped)
450 active = -1;
453 if (active>=0)
454 drawCorner(aicon->icon, aicon->icon->owner, active);
456 #endif /* NEWAPPICON */
458 if (aicon->omnipresent)
459 drawCorner(aicon->icon);
461 XSetClipMask(dpy, scr->copy_gc, None);
462 if (aicon->launching) {
463 XFillRectangle(dpy, aicon->icon->core->window, scr->stipple_gc,
464 0, 0, wPreferences.icon_size, wPreferences.icon_size);
470 #define canBeDocked(wwin) ((wwin) && ((wwin)->wm_class||(wwin)->wm_instance))
472 #ifdef REDUCE_APPICONS
473 unsigned int
474 wAppIconReduceAppCount(WApplication *wapp)
476 WAppIconAppList *applist;
478 if (wapp == NULL)
479 return 0;
481 if (wapp->app_icon == NULL)
482 return 0;
484 /* If given a main window, check the applist
485 * and remove the if it exists
487 applist = wapp->app_icon->applist;
488 while (applist != NULL) {
489 if (applist->wapp == wapp) {
490 /* If this app owns the appicon, change the appicon's
491 * owner to the next app in the list or NULL
493 if (wapp->app_icon->icon->owner == applist->wapp->main_window_desc)
495 if (applist->next) {
496 wapp->app_icon->icon->owner = applist->next->wapp->main_window_desc;
497 } else if (applist->prev) {
498 wapp->app_icon->icon->owner = applist->prev->wapp->main_window_desc;
499 } else {
500 wapp->app_icon->icon->owner = NULL;
503 if (applist->prev)
504 applist->prev->next = applist->next;
506 if (applist->next)
507 applist->next->prev = applist->prev;
509 if (applist == wapp->app_icon->applist)
510 wapp->app_icon->applist = applist->next;
512 free(applist);
514 if (wapp->app_icon->applist != NULL)
515 wapp->app_icon->main_window = wapp->app_icon->applist->wapp->main_window;
517 return (--wapp->app_icon->num_apps);
519 applist = applist->next;
521 return (--wapp->app_icon->num_apps);
523 #endif
526 static void
527 hideCallback(WMenu *menu, WMenuEntry *entry)
529 WApplication *wapp = (WApplication*)entry->clientdata;
531 if (wapp->flags.hidden) {
532 wWorkspaceChange(menu->menu->screen_ptr, wapp->last_workspace);
533 wUnhideApplication(wapp, False, False);
534 } else {
535 wHideApplication(wapp);
540 static void
541 unhideHereCallback(WMenu *menu, WMenuEntry *entry)
543 WApplication *wapp = (WApplication*)entry->clientdata;
545 wUnhideApplication(wapp, False, True);
549 static void
550 setIconCallback(WMenu *menu, WMenuEntry *entry)
552 WAppIcon *icon = ((WApplication*)entry->clientdata)->app_icon;
553 char *file=NULL;
554 WScreen *scr;
555 int result;
557 assert(icon!=NULL);
559 if (icon->editing)
560 return;
561 icon->editing = 1;
562 scr = icon->icon->core->screen_ptr;
564 wretain(icon);
566 result = wIconChooserDialog(scr, &file, icon->wm_instance, icon->wm_class);
568 if (result && !icon->destroyed) {
569 if (file[0]==0) {
570 free(file);
571 file = NULL;
573 if (!wIconChangeImageFile(icon->icon, file)) {
574 wMessageDialog(scr, _("Error"),
575 _("Could not open specified icon file"),
576 _("OK"), NULL, NULL);
577 } else {
578 wDefaultChangeIcon(scr, icon->wm_instance, icon->wm_class, file);
579 wAppIconPaint(icon);
581 if (file)
582 free(file);
584 icon->editing = 0;
585 wrelease(icon);
589 static void
590 killCallback(WMenu *menu, WMenuEntry *entry)
592 WApplication *wapp = (WApplication*)entry->clientdata;
593 char *buffer;
595 if (!WCHECK_STATE(WSTATE_NORMAL))
596 return;
598 WCHANGE_STATE(WSTATE_MODAL);
600 assert(entry->clientdata!=NULL);
602 buffer = wstrappend(wapp->app_icon ? wapp->app_icon->wm_class : NULL,
603 _(" will be forcibly closed.\n"
604 "Any unsaved changes will be lost.\n"
605 "Please confirm."));
607 wretain(wapp->main_window_desc);
608 if (wPreferences.dont_confirm_kill
609 || wMessageDialog(menu->frame->screen_ptr, _("Kill Application"),
610 buffer, _("Yes"), _("No"), NULL)==WAPRDefault) {
611 if (!wapp->main_window_desc->flags.destroyed)
612 wClientKill(wapp->main_window_desc);
614 wrelease(wapp->main_window_desc);
616 free(buffer);
618 WCHANGE_STATE(WSTATE_NORMAL);
622 static WMenu*
623 createApplicationMenu(WScreen *scr)
625 WMenu *menu;
627 menu = wMenuCreate(scr, NULL, False);
628 wMenuAddCallback(menu, _("Unhide Here"), unhideHereCallback, NULL);
629 wMenuAddCallback(menu, _("Hide"), hideCallback, NULL);
630 wMenuAddCallback(menu, _("Set Icon..."), setIconCallback, NULL);
631 wMenuAddCallback(menu, _("Kill"), killCallback, NULL);
633 return menu;
637 static void
638 openApplicationMenu(WApplication *wapp, int x, int y)
640 WMenu *menu;
641 WScreen *scr = wapp->main_window_desc->screen_ptr;
642 int i;
644 if (!scr->icon_menu) {
645 scr->icon_menu = createApplicationMenu(scr);
646 free(scr->icon_menu->entries[1]->text);
649 menu = scr->icon_menu;
651 if (wapp->flags.hidden) {
652 menu->entries[1]->text = _("Unhide");
653 } else {
654 menu->entries[1]->text = _("Hide");
657 menu->flags.realized = 0;
658 wMenuRealize(menu);
660 x -= menu->frame->core->width/2;
661 if (x + menu->frame->core->width > scr->scr_width)
662 x = scr->scr_width - menu->frame->core->width;
663 if (x < 0)
664 x = 0;
666 /* set client data */
667 for (i = 0; i < menu->entry_no; i++) {
668 menu->entries[i]->clientdata = wapp;
670 wMenuMapAt(menu, x, y, False);
674 /******************************************************************/
676 static void
677 iconExpose(WObjDescriptor *desc, XEvent *event)
679 wAppIconPaint(desc->parent);
683 static void
684 iconDblClick(WObjDescriptor *desc, XEvent *event)
686 WAppIcon *aicon = desc->parent;
687 WApplication *wapp;
688 WScreen *scr = aicon->icon->core->screen_ptr;
689 int unhideHere;
691 #ifndef REDUCE_APPICONS
692 assert(aicon->icon->owner!=NULL);
693 #else
694 if (aicon->icon->owner == NULL) {
695 fprintf(stderr, "Double-click disabled: missing main window.\n");
696 return;
698 #endif
700 wapp = wApplicationOf(aicon->icon->owner->main_window);
701 #ifdef DEBUG0
702 if (!wapp) {
703 wwarning("could not find application descriptor for app icon!!");
704 return;
706 #endif
707 #ifdef REDUCE_APPICONS
708 if (!wapp) {
709 fprintf(stderr, "Double-click disabled: missing wapp.\n");
710 return;
712 #endif /* REDUCE_APPICONS */
714 unhideHere = (event->xbutton.state & ShiftMask);
716 /* go to the last workspace that the user worked on the app */
717 if (!unhideHere && wapp->last_workspace != scr->current_workspace)
718 wWorkspaceChange(scr, wapp->last_workspace);
720 wUnhideApplication(wapp, event->xbutton.button==Button2, unhideHere);
722 if (event->xbutton.state & MOD_MASK) {
723 wHideOtherApplications(aicon->icon->owner);
728 void
729 appIconMouseDown(WObjDescriptor *desc, XEvent *event)
731 WAppIcon *aicon = desc->parent;
732 WIcon *icon = aicon->icon;
733 XEvent ev;
734 int x=aicon->x_pos, y=aicon->y_pos;
735 int dx=event->xbutton.x, dy=event->xbutton.y;
736 int grabbed=0;
737 int done=0;
738 int superfluous = wPreferences.superfluous; /* we catch it to avoid problems */
739 WScreen *scr = icon->core->screen_ptr;
740 WWorkspace *workspace = scr->workspaces[scr->current_workspace];
741 int shad_x = 0, shad_y = 0, docking=0, dockable, collapsed = 0;
742 int ix, iy;
743 int clickButton = event->xbutton.button;
744 Pixmap ghost = None;
745 Window wins[2];
747 if (aicon->editing || WCHECK_STATE(WSTATE_MODAL))
748 return;
750 if (IsDoubleClick(scr, event)) {
751 iconDblClick(desc, event);
752 return;
755 if (event->xbutton.button == Button3) {
756 WObjDescriptor *desc;
757 WApplication *wapp = wApplicationOf(aicon->icon->owner->main_window);
759 if (!wapp)
760 return;
762 openApplicationMenu(wapp, event->xbutton.x_root,
763 event->xbutton.y_root);
765 /* allow drag select of menu */
766 desc = &scr->icon_menu->menu->descriptor;
767 event->xbutton.send_event = True;
768 (*desc->handle_mousedown)(desc, event);
769 return;
772 #ifdef DEBUG
773 puts("Moving icon");
774 #endif
775 if (event->xbutton.state & MOD_MASK)
776 wLowerFrame(icon->core);
777 else
778 wRaiseFrame(icon->core);
781 if (XGrabPointer(dpy, icon->core->window, True, ButtonMotionMask
782 |ButtonReleaseMask|ButtonPressMask, GrabModeAsync,
783 GrabModeAsync, None, None, CurrentTime) !=GrabSuccess) {
784 wwarning("pointer grab failed for appicon move");
787 if (wPreferences.flags.nodock && wPreferences.flags.noclip)
788 dockable = 0;
789 else
790 dockable = canBeDocked(icon->owner);
792 wins[0] = icon->core->window;
793 wins[1] = scr->dock_shadow;
794 XRestackWindows(dpy, wins, 2);
795 if (superfluous) {
796 if (icon->pixmap!=None)
797 ghost = MakeGhostIcon(scr, icon->pixmap);
798 else
799 ghost = MakeGhostIcon(scr, icon->core->window);
800 XSetWindowBackgroundPixmap(dpy, scr->dock_shadow,
801 ghost);
802 XClearWindow(dpy, scr->dock_shadow);
805 while (!done) {
806 WMMaskEvent(dpy, PointerMotionMask|ButtonReleaseMask|ButtonPressMask
807 |ButtonMotionMask|ExposureMask, &ev);
808 switch (ev.type) {
809 case Expose:
810 WMHandleEvent(&ev);
811 break;
813 case MotionNotify:
814 if (!grabbed) {
815 if (abs(dx-ev.xmotion.x)>=MOVE_THRESHOLD
816 || abs(dy-ev.xmotion.y)>=MOVE_THRESHOLD) {
817 XChangeActivePointerGrab(dpy, ButtonMotionMask
818 |ButtonReleaseMask|ButtonPressMask,
819 wCursor[WCUR_MOVE], CurrentTime);
820 grabbed=1;
821 } else {
822 break;
825 x = ev.xmotion.x_root - dx;
826 y = ev.xmotion.y_root - dy;
827 XMoveWindow(dpy, icon->core->window, x, y);
829 if (dockable) {
830 if (scr->dock && wDockSnapIcon(scr->dock, aicon, x, y,
831 &ix, &iy, False)) {
832 shad_x = scr->dock->x_pos + ix*wPreferences.icon_size;
833 shad_y = scr->dock->y_pos + iy*wPreferences.icon_size;
835 if (scr->last_dock != scr->dock && collapsed) {
836 scr->last_dock->collapsed = 1;
837 wDockHideIcons(scr->last_dock);
838 collapsed = 0;
840 if (!collapsed && (collapsed = scr->dock->collapsed)) {
841 scr->dock->collapsed = 0;
842 wDockShowIcons(scr->dock);
845 if (scr->dock->auto_raise_lower)
846 wDockRaise(scr->dock);
848 scr->last_dock = scr->dock;
850 XMoveWindow(dpy, scr->dock_shadow, shad_x, shad_y);
851 if (!docking) {
852 XMapWindow(dpy, scr->dock_shadow);
854 docking = 1;
855 } else if (workspace->clip &&
856 wDockSnapIcon(workspace->clip, aicon, x, y,
857 &ix, &iy, False)) {
858 shad_x = workspace->clip->x_pos + ix*wPreferences.icon_size;
859 shad_y = workspace->clip->y_pos + iy*wPreferences.icon_size;
861 if (scr->last_dock != workspace->clip && collapsed) {
862 scr->last_dock->collapsed = 1;
863 wDockHideIcons(scr->last_dock);
864 collapsed = 0;
866 if (!collapsed && (collapsed = workspace->clip->collapsed)) {
867 workspace->clip->collapsed = 0;
868 wDockShowIcons(workspace->clip);
871 if (workspace->clip->auto_raise_lower)
872 wDockRaise(workspace->clip);
874 scr->last_dock = workspace->clip;
876 XMoveWindow(dpy, scr->dock_shadow, shad_x, shad_y);
877 if (!docking) {
878 XMapWindow(dpy, scr->dock_shadow);
880 docking = 1;
881 } else if (docking) {
882 XUnmapWindow(dpy, scr->dock_shadow);
883 docking = 0;
887 break;
889 case ButtonPress:
890 break;
892 case ButtonRelease:
893 if (ev.xbutton.button != clickButton)
894 break;
895 XUngrabPointer(dpy, CurrentTime);
897 if (docking) {
898 Bool docked;
900 /* icon is trying to be docked */
901 SlideWindow(icon->core->window, x, y, shad_x, shad_y);
902 XUnmapWindow(dpy, scr->dock_shadow);
903 docked = wDockAttachIcon(scr->last_dock, aicon, ix, iy);
904 if (scr->last_dock->auto_collapse) {
905 collapsed = 0;
907 if (workspace->clip &&
908 workspace->clip != scr->last_dock &&
909 workspace->clip->auto_raise_lower)
910 wDockLower(workspace->clip);
912 if (!docked) {
913 /* If icon could not be docked, slide it back to the old
914 * position */
915 SlideWindow(icon->core->window, x, y, aicon->x_pos,
916 aicon->y_pos);
919 #ifdef WMSOUND
920 wSoundPlay(WMSOUND_DOCK);
921 #endif
922 } else {
923 XMoveWindow(dpy, icon->core->window, x, y);
924 aicon->x_pos = x;
925 aicon->y_pos = y;
926 if (workspace->clip && workspace->clip->auto_raise_lower)
927 wDockLower(workspace->clip);
929 if (collapsed) {
930 scr->last_dock->collapsed = 1;
931 wDockHideIcons(scr->last_dock);
932 collapsed = 0;
934 if (superfluous) {
935 if (ghost!=None)
936 XFreePixmap(dpy, ghost);
937 XSetWindowBackground(dpy, scr->dock_shadow, scr->white_pixel);
940 if (wPreferences.auto_arrange_icons)
941 wArrangeIcons(scr, True);
943 done = 1;
944 break;
947 #ifdef DEBUG
948 puts("End icon move");
949 #endif