Fixed crashing bug in WPrefs.app's appereance section.
[wmaker-crm.git] / src / appicon.c
blob87d47a98f9f40bb6db5c825dbe61e6c9f5690959
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 "WINGsP.h"
54 * icon_file for the dock is got from the preferences file by
55 * using the classname/instancename
58 /**** Global variables ****/
59 extern Cursor wCursor[WCUR_LAST];
60 extern WPreferences wPreferences;
62 #define MOD_MASK wPreferences.modifier_mask
64 void appIconMouseDown(WObjDescriptor *desc, XEvent *event);
65 static void iconDblClick(WObjDescriptor *desc, XEvent *event);
66 static void iconExpose(WObjDescriptor *desc, XEvent *event);
70 WAppIcon*
71 wAppIconCreateForDock(WScreen *scr, char *command, char *wm_instance,
72 char *wm_class, int tile)
74 WAppIcon *dicon;
75 char *path;
77 dicon = wmalloc(sizeof(WAppIcon));
78 wretain(dicon);
79 memset(dicon, 0, sizeof(WAppIcon));
80 dicon->yindex = -1;
81 dicon->xindex = -1;
83 dicon->prev = NULL;
84 dicon->next = scr->app_icon_list;
85 if (scr->app_icon_list) {
86 scr->app_icon_list->prev = dicon;
88 scr->app_icon_list = dicon;
90 if (command) {
91 dicon->command = wstrdup(command);
93 if (wm_class)
94 dicon->wm_class = wstrdup(wm_class);
95 if (wm_instance)
96 dicon->wm_instance = wstrdup(wm_instance);
98 path = wDefaultGetIconFile(scr, wm_instance, wm_class, True);
99 if (!path && command) {
100 wApplicationExtractDirPackIcon(scr, command, wm_instance, wm_class);
102 path = wDefaultGetIconFile(scr, wm_instance, wm_class, False);
105 if (path)
106 path = FindImage(wPreferences.icon_path, path);
108 dicon->icon = wIconCreateWithIconFile(scr, path, tile);
109 if (path)
110 free(path);
111 #ifdef XDND
112 wXDNDMakeAwareness(dicon->icon->core->window);
113 #endif
114 #ifdef REDUCE_APPICONS
115 dicon->num_apps = 0;
116 #endif
117 #ifdef DEMATERIALIZE_ICON
119 XSetWindowAttributes attribs;
120 attribs.save_under = True;
121 XChangeWindowAttributes(dpy, dicon->icon->core->window,
122 CWSaveUnder, &attribs);
124 #endif
126 /* will be overriden by dock */
127 dicon->icon->core->descriptor.handle_mousedown = appIconMouseDown;
128 dicon->icon->core->descriptor.handle_expose = iconExpose;
129 dicon->icon->core->descriptor.parent_type = WCLASS_APPICON;
130 dicon->icon->core->descriptor.parent = dicon;
131 AddToStackList(dicon->icon->core);
133 return dicon;
138 WAppIcon*
139 wAppIconCreate(WWindow *leader_win)
141 WAppIcon *aicon;
142 #ifdef REDUCE_APPICONS
143 WAppIcon *atmp;
144 WAppIconAppList *applist;
145 char *tinstance, *tclass;
146 #endif
147 WScreen *scr = leader_win->screen_ptr;
149 aicon = wmalloc(sizeof(WAppIcon));
150 wretain(aicon);
151 memset(aicon, 0, sizeof(WAppIcon));
152 #ifdef REDUCE_APPICONS
153 applist = wmalloc(sizeof(WAppIconAppList));
154 memset(applist, 0, sizeof(WAppIconAppList));
155 applist->wapp = wApplicationOf(leader_win->main_window);
156 aicon->applist = applist;
157 if (applist->wapp == NULL) {
158 /* Something's wrong. wApplicationOf() should always return a
159 * valid structure. Rather than violate assumptions, bail. -cls
161 free(applist);
162 wrelease(aicon);
163 return NULL;
165 #endif
167 aicon->yindex = -1;
168 aicon->xindex = -1;
170 aicon->prev = NULL;
171 aicon->next = scr->app_icon_list;
172 if (scr->app_icon_list) {
173 #ifndef REDUCE_APPICONS
174 scr->app_icon_list->prev = aicon;
175 #else
176 /* If we aren't going to have a match, jump straight to new appicon */
177 if (leader_win->wm_class == NULL || leader_win->wm_class == NULL)
178 atmp = NULL;
179 else
180 atmp = scr->app_icon_list;
182 while (atmp != NULL) {
183 if ((tinstance = atmp->wm_instance) == NULL)
184 tinstance = "";
185 if ((tclass = atmp->wm_class) == NULL)
186 tclass = "";
187 if ((strcmp(leader_win->wm_class, tclass) == 0) &&
188 (strcmp(leader_win->wm_instance, tinstance) == 0))
190 /* We have a winner */
191 wrelease(aicon);
192 atmp->num_apps++;
193 applist->next = atmp->applist;
194 if (atmp->applist)
195 atmp->applist->prev = applist;
196 atmp->applist = applist;
198 if (atmp->docked) {
199 wDockSimulateLaunch(atmp->dock, atmp);
202 return atmp;
204 atmp = atmp->next;
206 if (atmp == NULL) {
207 scr->app_icon_list->prev = aicon;
209 #endif /* REDUCE_APPICONS */
211 scr->app_icon_list = aicon;
213 if (leader_win->wm_class)
214 aicon->wm_class = wstrdup(leader_win->wm_class);
215 if (leader_win->wm_instance)
216 aicon->wm_instance = wstrdup(leader_win->wm_instance);
217 #ifdef REDUCE_APPICONS
218 aicon->num_apps = 1;
219 #endif
221 aicon->icon = wIconCreate(leader_win);
222 #ifdef DEMATERIALIZE_ICON
224 XSetWindowAttributes attribs;
225 attribs.save_under = True;
226 XChangeWindowAttributes(dpy, aicon->icon->core->window,
227 CWSaveUnder, &attribs);
229 #endif
230 #ifdef XDND
231 wXDNDMakeAwareness(aicon->icon->core->window);
232 #endif
234 /* will be overriden if docked */
235 aicon->icon->core->descriptor.handle_mousedown = appIconMouseDown;
236 aicon->icon->core->descriptor.handle_expose = iconExpose;
237 aicon->icon->core->descriptor.parent_type = WCLASS_APPICON;
238 aicon->icon->core->descriptor.parent = aicon;
239 AddToStackList(aicon->icon->core);
240 aicon->icon->show_title = 0;
241 wIconUpdate(aicon->icon);
243 return aicon;
247 void
248 wAppIconDestroy(WAppIcon *aicon)
250 WScreen *scr = aicon->icon->core->screen_ptr;
251 #ifdef REDUCE_APPICONS
252 WAppIconAppList *aptmp;
253 #endif
255 RemoveFromStackList(aicon->icon->core);
256 wIconDestroy(aicon->icon);
257 if (aicon->command)
258 free(aicon->command);
259 #ifdef OFFIX_DND
260 if (aicon->dnd_command)
261 free(aicon->dnd_command);
262 #endif
263 if (aicon->wm_instance)
264 free(aicon->wm_instance);
265 if (aicon->wm_class)
266 free(aicon->wm_class);
267 #ifdef REDUCE_APPICONS
268 /* There should never be a list but just in case */
269 if (aicon->applist != NULL) {
270 aptmp = aicon->applist;
271 while (aptmp->next) {
272 aptmp = aptmp->next;
273 free(aptmp->prev);
275 free(aptmp);
277 #endif
279 if (aicon == scr->app_icon_list) {
280 if (aicon->next)
281 aicon->next->prev = NULL;
282 scr->app_icon_list = aicon->next;
284 else {
285 if (aicon->next)
286 aicon->next->prev = aicon->prev;
287 if (aicon->prev)
288 aicon->prev->next = aicon->next;
291 aicon->destroyed = 1;
292 wrelease(aicon);
297 #ifdef NEWAPPICON
298 static void
299 drawCorner(WIcon *icon, WWindow *wwin, int active)
301 WScreen *scr = wwin->screen_ptr;
302 XPoint points[3];
303 GC gc;
305 points[0].x = 2;
306 points[0].y = 2;
307 points[1].x = 12;
308 points[1].y = 2;
309 points[2].x = 2;
310 points[2].y = 12;
311 if (active) {
312 gc=scr->focused_texture->any.gc;
313 } else {
314 gc=scr->unfocused_texture->any.gc;
316 XFillPolygon(dpy, icon->core->window, gc, points, 3,
317 Convex, CoordModeOrigin);
319 #endif /* NEWAPPICON */
322 static void
323 drawCorner(WIcon *icon)
325 WScreen *scr = icon->core->screen_ptr;
326 XPoint points[3];
328 points[0].x = 1;
329 points[0].y = 1;
330 points[1].x = 12;
331 points[1].y = 1;
332 points[2].x = 1;
333 points[2].y = 12;
334 XFillPolygon(dpy, icon->core->window, scr->icon_title_texture->normal_gc,
335 points, 3, Convex, CoordModeOrigin);
336 XDrawLine(dpy, icon->core->window, scr->icon_title_texture->light_gc,
337 0, 0, 0, 12);
338 XDrawLine(dpy, icon->core->window, scr->icon_title_texture->light_gc,
339 0, 0, 12, 0);
340 /* drawing the second line gives a weird concave look. -Dan */
341 #if 0
342 XDrawLine(dpy, icon->core->window, scr->icon_title_texture->light_gc,
343 1, 1, 1, 11);
344 XDrawLine(dpy, icon->core->window, scr->icon_title_texture->light_gc,
345 1, 1, 11, 1);
346 #endif
350 void
351 wAppIconMove(WAppIcon *aicon, int x, int y)
353 XMoveWindow(dpy, aicon->icon->core->window, x, y);
354 aicon->x_pos = x;
355 aicon->y_pos = y;
359 #ifdef WS_INDICATOR
360 static void
361 updateDockNumbers(WScreen *scr)
363 int length;
364 char *ws_numbers;
365 GC numbers_gc;
366 XGCValues my_gc_values;
367 unsigned long my_v_mask = (GCForeground);
368 WAppIcon *dicon = scr->dock->icon_array[0];
370 my_gc_values.foreground = scr->white_pixel;
371 numbers_gc = XCreateGC(dpy, dicon->icon->core->window,
372 my_v_mask, &my_gc_values);
374 ws_numbers = malloc(20);
375 sprintf(ws_numbers, "%i [ %i ]", scr->current_workspace+1,
376 ((scr->current_workspace/10)+1));
377 length = strlen(ws_numbers);
379 XClearArea(dpy, dicon->icon->core->window, 2, 2, 50,
380 WMFontHeight(scr->icon_title_font)+1, False);
382 XSetForeground(dpy, numbers_gc, scr->black_pixel);
383 WMDrawString(scr->wmscreen, dicon->icon->core->window, numbers_gc,
384 scr->icon_title_font, 4, 3, ws_numbers, length);
386 XSetForeground(dpy, numbers_gc, scr->white_pixel);
387 WMDrawString(scr->wmscreen, dicon->icon->core->window, numbers_gc,
388 scr->icon_title_font, 3, 2, ws_numbers, length);
390 XFreeGC(dpy, numbers_gc);
391 free(ws_numbers);
393 #endif /* WS_INDICATOR */
396 void
397 wAppIconPaint(WAppIcon *aicon)
399 WScreen *scr = aicon->icon->core->screen_ptr;
401 wIconPaint(aicon->icon);
404 # ifdef WS_INDICATOR
405 if (aicon->docked && scr->dock && scr->dock==aicon->dock &&
406 aicon->yindex==0)
407 updateDockNumbers(scr);
408 # endif
409 if (scr->dock_dots && aicon->docked && !aicon->running
410 && aicon->command!=NULL) {
411 XSetClipMask(dpy, scr->copy_gc, scr->dock_dots->mask);
412 XSetClipOrigin(dpy, scr->copy_gc, 0, 0);
413 XCopyArea(dpy, scr->dock_dots->image, aicon->icon->core->window,
414 scr->copy_gc, 0, 0, scr->dock_dots->width,
415 scr->dock_dots->height, 0, 0);
418 #ifdef HIDDENDOT
420 WApplication *wapp;
421 wapp = wApplicationOf(aicon->main_window);
422 if (wapp && wapp->flags.hidden) {
423 XSetClipMask(dpy, scr->copy_gc, scr->dock_dots->mask);
424 XSetClipOrigin(dpy, scr->copy_gc, 0, 0);
425 XCopyArea(dpy, scr->dock_dots->image,
426 aicon->icon->core->window,
427 scr->copy_gc, 0, 0, 7,
428 scr->dock_dots->height, 0, 0);
431 #endif /* HIDDENDOT */
433 #ifdef NEWAPPICON
434 if (!wPreferences.strict_ns && aicon->icon->owner!=NULL) {
435 int active=0;
436 if (aicon->main_window==None) {
437 active = (aicon->icon->owner->flags.focused ? 1 : 0);
438 } else {
439 WApplication *wapp;
441 wapp = wApplicationOf(aicon->main_window);
442 if (!wapp) {
443 active = aicon->icon->owner->flags.focused;
444 wwarning("error in wAppIconPaint(). Please report it");
445 } else {
446 active = wapp->main_window_desc->flags.focused;
447 if (wapp->main_window_desc->flags.hidden
448 || !wapp->main_window_desc->flags.mapped)
449 active = -1;
452 if (active>=0)
453 drawCorner(aicon->icon, aicon->icon->owner, active);
455 #endif /* NEWAPPICON */
457 if (aicon->omnipresent)
458 drawCorner(aicon->icon);
460 XSetClipMask(dpy, scr->copy_gc, None);
461 if (aicon->launching) {
462 XFillRectangle(dpy, aicon->icon->core->window, scr->stipple_gc,
463 0, 0, wPreferences.icon_size, wPreferences.icon_size);
469 #define canBeDocked(wwin) ((wwin) && ((wwin)->wm_class||(wwin)->wm_instance))
471 #ifdef REDUCE_APPICONS
472 unsigned int
473 wAppIconReduceAppCount(WApplication *wapp)
475 WAppIconAppList *applist;
477 if (wapp == NULL)
478 return 0;
480 if (wapp->app_icon == NULL)
481 return 0;
483 /* If given a main window, check the applist
484 * and remove the if it exists
486 applist = wapp->app_icon->applist;
487 while (applist != NULL) {
488 if (applist->wapp == wapp) {
489 /* If this app owns the appicon, change the appicon's
490 * owner to the next app in the list or NULL
492 if (wapp->app_icon->icon->owner == applist->wapp->main_window_desc)
494 if (applist->next) {
495 wapp->app_icon->icon->owner = applist->next->wapp->main_window_desc;
496 } else if (applist->prev) {
497 wapp->app_icon->icon->owner = applist->prev->wapp->main_window_desc;
498 } else {
499 wapp->app_icon->icon->owner = NULL;
502 if (applist->prev)
503 applist->prev->next = applist->next;
505 if (applist->next)
506 applist->next->prev = applist->prev;
508 if (applist == wapp->app_icon->applist)
509 wapp->app_icon->applist = applist->next;
511 free(applist);
513 if (wapp->app_icon->applist != NULL)
514 wapp->app_icon->main_window = wapp->app_icon->applist->wapp->main_window;
516 return (--wapp->app_icon->num_apps);
518 applist = applist->next;
520 return (--wapp->app_icon->num_apps);
522 #endif
525 static void
526 hideCallback(WMenu *menu, WMenuEntry *entry)
528 WApplication *wapp = (WApplication*)entry->clientdata;
530 if (wapp->flags.hidden) {
531 wWorkspaceChange(menu->menu->screen_ptr, wapp->last_workspace);
532 wUnhideApplication(wapp, False, False);
533 } else {
534 wHideApplication(wapp);
539 static void
540 unhideHereCallback(WMenu *menu, WMenuEntry *entry)
542 WApplication *wapp = (WApplication*)entry->clientdata;
544 wUnhideApplication(wapp, False, True);
548 static void
549 setIconCallback(WMenu *menu, WMenuEntry *entry)
551 WAppIcon *icon = ((WApplication*)entry->clientdata)->app_icon;
552 char *file=NULL;
553 WScreen *scr;
554 int result;
556 assert(icon!=NULL);
558 if (icon->editing)
559 return;
560 icon->editing = 1;
561 scr = icon->icon->core->screen_ptr;
563 wretain(icon);
565 result = wIconChooserDialog(scr, &file, icon->wm_instance, icon->wm_class);
567 if (result && !icon->destroyed) {
568 if (file[0]==0) {
569 free(file);
570 file = NULL;
572 if (!wIconChangeImageFile(icon->icon, file)) {
573 wMessageDialog(scr, _("Error"),
574 _("Could not open specified icon file"),
575 _("OK"), NULL, NULL);
576 } else {
577 wDefaultChangeIcon(scr, icon->wm_instance, icon->wm_class, file);
578 wAppIconPaint(icon);
580 if (file)
581 free(file);
583 icon->editing = 0;
584 wrelease(icon);
588 static void
589 killCallback(WMenu *menu, WMenuEntry *entry)
591 WApplication *wapp = (WApplication*)entry->clientdata;
592 char *buffer;
594 if (!WCHECK_STATE(WSTATE_NORMAL))
595 return;
597 WCHANGE_STATE(WSTATE_MODAL);
599 assert(entry->clientdata!=NULL);
601 buffer = wstrappend(wapp->app_icon ? wapp->app_icon->wm_class : NULL,
602 _(" will be forcibly closed.\n"
603 "Any unsaved changes will be lost.\n"
604 "Please confirm."));
606 wretain(wapp->main_window_desc);
607 if (wPreferences.dont_confirm_kill
608 || wMessageDialog(menu->frame->screen_ptr, _("Kill Application"),
609 buffer, _("Yes"), _("No"), NULL)==WAPRDefault) {
610 if (!wapp->main_window_desc->flags.destroyed)
611 wClientKill(wapp->main_window_desc);
613 wrelease(wapp->main_window_desc);
615 free(buffer);
617 WCHANGE_STATE(WSTATE_NORMAL);
621 static WMenu*
622 createApplicationMenu(WScreen *scr)
624 WMenu *menu;
626 menu = wMenuCreate(scr, NULL, False);
627 wMenuAddCallback(menu, _("Unhide Here"), unhideHereCallback, NULL);
628 wMenuAddCallback(menu, _("Hide"), hideCallback, NULL);
629 wMenuAddCallback(menu, _("Set Icon..."), setIconCallback, NULL);
630 wMenuAddCallback(menu, _("Kill"), killCallback, NULL);
632 return menu;
636 static void
637 openApplicationMenu(WApplication *wapp, int x, int y)
639 WMenu *menu;
640 WScreen *scr = wapp->main_window_desc->screen_ptr;
641 int i;
643 if (!scr->icon_menu) {
644 scr->icon_menu = createApplicationMenu(scr);
645 free(scr->icon_menu->entries[1]->text);
648 menu = scr->icon_menu;
650 if (wapp->flags.hidden) {
651 menu->entries[1]->text = _("Unhide");
652 } else {
653 menu->entries[1]->text = _("Hide");
656 menu->flags.realized = 0;
657 wMenuRealize(menu);
659 x -= menu->frame->core->width/2;
660 if (x + menu->frame->core->width > scr->scr_width)
661 x = scr->scr_width - menu->frame->core->width;
662 if (x < 0)
663 x = 0;
665 /* set client data */
666 for (i = 0; i < menu->entry_no; i++) {
667 menu->entries[i]->clientdata = wapp;
669 wMenuMapAt(menu, x, y, False);
673 /******************************************************************/
675 static void
676 iconExpose(WObjDescriptor *desc, XEvent *event)
678 wAppIconPaint(desc->parent);
682 static void
683 iconDblClick(WObjDescriptor *desc, XEvent *event)
685 WAppIcon *aicon = desc->parent;
686 WApplication *wapp;
687 WScreen *scr = aicon->icon->core->screen_ptr;
688 int unhideHere;
690 #ifndef REDUCE_APPICONS
691 assert(aicon->icon->owner!=NULL);
692 #else
693 if (aicon->icon->owner == NULL) {
694 fprintf(stderr, "Double-click disabled: missing main window.\n");
695 return;
697 #endif
699 wapp = wApplicationOf(aicon->icon->owner->main_window);
700 #ifdef DEBUG0
701 if (!wapp) {
702 wwarning("could not find application descriptor for app icon!!");
703 return;
705 #endif
706 #ifdef REDUCE_APPICONS
707 if (!wapp) {
708 fprintf(stderr, "Double-click disabled: missing wapp.\n");
709 return;
711 #endif /* REDUCE_APPICONS */
713 unhideHere = (event->xbutton.state & ShiftMask);
715 /* go to the last workspace that the user worked on the app */
716 if (!unhideHere && wapp->last_workspace != scr->current_workspace)
717 wWorkspaceChange(scr, wapp->last_workspace);
719 wUnhideApplication(wapp, event->xbutton.button==Button2, unhideHere);
721 if (event->xbutton.state & MOD_MASK) {
722 wHideOtherApplications(aicon->icon->owner);
727 void
728 appIconMouseDown(WObjDescriptor *desc, XEvent *event)
730 WAppIcon *aicon = desc->parent;
731 WIcon *icon = aicon->icon;
732 XEvent ev;
733 int x=aicon->x_pos, y=aicon->y_pos;
734 int dx=event->xbutton.x, dy=event->xbutton.y;
735 int grabbed=0;
736 int done=0;
737 int superfluous = wPreferences.superfluous; /* we catch it to avoid problems */
738 WScreen *scr = icon->core->screen_ptr;
739 WWorkspace *workspace = scr->workspaces[scr->current_workspace];
740 int shad_x = 0, shad_y = 0, docking=0, dockable, collapsed = 0;
741 int ix, iy;
742 int clickButton = event->xbutton.button;
743 Pixmap ghost = None;
744 Window wins[2];
746 if (aicon->editing || WCHECK_STATE(WSTATE_MODAL))
747 return;
749 if (IsDoubleClick(scr, event)) {
750 iconDblClick(desc, event);
751 return;
754 if (event->xbutton.button == Button3) {
755 WObjDescriptor *desc;
756 WApplication *wapp = wApplicationOf(aicon->icon->owner->main_window);
758 if (!wapp)
759 return;
761 openApplicationMenu(wapp, event->xbutton.x_root,
762 event->xbutton.y_root);
764 /* allow drag select of menu */
765 desc = &scr->icon_menu->menu->descriptor;
766 event->xbutton.send_event = True;
767 (*desc->handle_mousedown)(desc, event);
768 return;
771 #ifdef DEBUG
772 puts("Moving icon");
773 #endif
774 if (event->xbutton.state & MOD_MASK)
775 wLowerFrame(icon->core);
776 else
777 wRaiseFrame(icon->core);
780 if (XGrabPointer(dpy, icon->core->window, True, ButtonMotionMask
781 |ButtonReleaseMask|ButtonPressMask, GrabModeAsync,
782 GrabModeAsync, None, None, CurrentTime) !=GrabSuccess) {
783 wwarning("pointer grab failed for appicon move");
786 if (wPreferences.flags.nodock && wPreferences.flags.noclip)
787 dockable = 0;
788 else
789 dockable = canBeDocked(icon->owner);
791 wins[0] = icon->core->window;
792 wins[1] = scr->dock_shadow;
793 XRestackWindows(dpy, wins, 2);
794 if (superfluous) {
795 if (icon->pixmap!=None)
796 ghost = MakeGhostIcon(scr, icon->pixmap);
797 else
798 ghost = MakeGhostIcon(scr, icon->core->window);
799 XSetWindowBackgroundPixmap(dpy, scr->dock_shadow,
800 ghost);
801 XClearWindow(dpy, scr->dock_shadow);
804 while (!done) {
805 WMMaskEvent(dpy, PointerMotionMask|ButtonReleaseMask|ButtonPressMask
806 |ButtonMotionMask|ExposureMask, &ev);
807 switch (ev.type) {
808 case Expose:
809 WMHandleEvent(&ev);
810 break;
812 case MotionNotify:
813 if (!grabbed) {
814 if (abs(dx-ev.xmotion.x)>=MOVE_THRESHOLD
815 || abs(dy-ev.xmotion.y)>=MOVE_THRESHOLD) {
816 XChangeActivePointerGrab(dpy, ButtonMotionMask
817 |ButtonReleaseMask|ButtonPressMask,
818 wCursor[WCUR_MOVE], CurrentTime);
819 grabbed=1;
820 } else {
821 break;
824 x = ev.xmotion.x_root - dx;
825 y = ev.xmotion.y_root - dy;
826 XMoveWindow(dpy, icon->core->window, x, y);
828 if (dockable) {
829 if (scr->dock && wDockSnapIcon(scr->dock, aicon, x, y,
830 &ix, &iy, False)) {
831 shad_x = scr->dock->x_pos + ix*wPreferences.icon_size;
832 shad_y = scr->dock->y_pos + iy*wPreferences.icon_size;
834 if (scr->last_dock != scr->dock && collapsed) {
835 scr->last_dock->collapsed = 1;
836 wDockHideIcons(scr->last_dock);
837 collapsed = 0;
839 if (!collapsed && (collapsed = scr->dock->collapsed)) {
840 scr->dock->collapsed = 0;
841 wDockShowIcons(scr->dock);
844 if (scr->dock->auto_raise_lower)
845 wDockRaise(scr->dock);
847 scr->last_dock = scr->dock;
849 XMoveWindow(dpy, scr->dock_shadow, shad_x, shad_y);
850 if (!docking) {
851 XMapWindow(dpy, scr->dock_shadow);
853 docking = 1;
854 } else if (workspace->clip &&
855 wDockSnapIcon(workspace->clip, aicon, x, y,
856 &ix, &iy, False)) {
857 shad_x = workspace->clip->x_pos + ix*wPreferences.icon_size;
858 shad_y = workspace->clip->y_pos + iy*wPreferences.icon_size;
860 if (scr->last_dock != workspace->clip && collapsed) {
861 scr->last_dock->collapsed = 1;
862 wDockHideIcons(scr->last_dock);
863 collapsed = 0;
865 if (!collapsed && (collapsed = workspace->clip->collapsed)) {
866 workspace->clip->collapsed = 0;
867 wDockShowIcons(workspace->clip);
870 if (workspace->clip->auto_raise_lower)
871 wDockRaise(workspace->clip);
873 scr->last_dock = workspace->clip;
875 XMoveWindow(dpy, scr->dock_shadow, shad_x, shad_y);
876 if (!docking) {
877 XMapWindow(dpy, scr->dock_shadow);
879 docking = 1;
880 } else if (docking) {
881 XUnmapWindow(dpy, scr->dock_shadow);
882 docking = 0;
886 break;
888 case ButtonPress:
889 break;
891 case ButtonRelease:
892 if (ev.xbutton.button != clickButton)
893 break;
894 XUngrabPointer(dpy, CurrentTime);
896 if (docking) {
897 Bool docked;
899 /* icon is trying to be docked */
900 SlideWindow(icon->core->window, x, y, shad_x, shad_y);
901 XUnmapWindow(dpy, scr->dock_shadow);
902 docked = wDockAttachIcon(scr->last_dock, aicon, ix, iy);
903 if (scr->last_dock->auto_collapse) {
904 collapsed = 0;
906 if (workspace->clip &&
907 workspace->clip != scr->last_dock &&
908 workspace->clip->auto_raise_lower)
909 wDockLower(workspace->clip);
911 if (!docked) {
912 /* If icon could not be docked, slide it back to the old
913 * position */
914 SlideWindow(icon->core->window, x, y, aicon->x_pos,
915 aicon->y_pos);
917 } else {
918 XMoveWindow(dpy, icon->core->window, x, y);
919 aicon->x_pos = x;
920 aicon->y_pos = y;
921 if (workspace->clip && workspace->clip->auto_raise_lower)
922 wDockLower(workspace->clip);
924 if (collapsed) {
925 scr->last_dock->collapsed = 1;
926 wDockHideIcons(scr->last_dock);
927 collapsed = 0;
929 if (superfluous) {
930 if (ghost!=None)
931 XFreePixmap(dpy, ghost);
932 XSetWindowBackground(dpy, scr->dock_shadow, scr->white_pixel);
935 if (wPreferences.auto_arrange_icons)
936 wArrangeIcons(scr, True);
938 done = 1;
939 break;
942 #ifdef DEBUG
943 puts("End icon move");
944 #endif