fixed many bugs, removed linked list
[wmaker-crm.git] / src / appicon.c
blob0c81aac3ba4097f68416daf1600c7f74da3e8d7c
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 && aicon->yindex==0)
406 updateDockNumbers(scr);
407 # endif
408 if (scr->dock_dots && aicon->docked && !aicon->running
409 && aicon->command!=NULL) {
410 XSetClipMask(dpy, scr->copy_gc, scr->dock_dots->mask);
411 XSetClipOrigin(dpy, scr->copy_gc, 0, 0);
412 XCopyArea(dpy, scr->dock_dots->image, aicon->icon->core->window,
413 scr->copy_gc, 0, 0, scr->dock_dots->width,
414 scr->dock_dots->height, 0, 0);
417 #ifdef HIDDENDOT
419 WApplication *wapp;
420 wapp = wApplicationOf(aicon->main_window);
421 if (wapp && wapp->flags.hidden) {
422 XSetClipMask(dpy, scr->copy_gc, scr->dock_dots->mask);
423 XSetClipOrigin(dpy, scr->copy_gc, 0, 0);
424 XCopyArea(dpy, scr->dock_dots->image,
425 aicon->icon->core->window,
426 scr->copy_gc, 0, 0, 7,
427 scr->dock_dots->height, 0, 0);
430 #endif /* HIDDENDOT */
432 #ifdef NEWAPPICON
433 if (!wPreferences.strict_ns && aicon->icon->owner!=NULL) {
434 int active=0;
435 if (aicon->main_window==None) {
436 active = (aicon->icon->owner->flags.focused ? 1 : 0);
437 } else {
438 WApplication *wapp;
440 wapp = wApplicationOf(aicon->main_window);
441 if (!wapp) {
442 active = aicon->icon->owner->flags.focused;
443 wwarning("error in wAppIconPaint(). Please report it");
444 } else {
445 active = wapp->main_window_desc->flags.focused;
446 if (wapp->main_window_desc->flags.hidden
447 || !wapp->main_window_desc->flags.mapped)
448 active = -1;
451 if (active>=0)
452 drawCorner(aicon->icon, aicon->icon->owner, active);
454 #endif /* NEWAPPICON */
456 if (aicon->omnipresent)
457 drawCorner(aicon->icon);
459 XSetClipMask(dpy, scr->copy_gc, None);
460 if (aicon->launching) {
461 XFillRectangle(dpy, aicon->icon->core->window, scr->stipple_gc,
462 0, 0, wPreferences.icon_size, wPreferences.icon_size);
468 #define canBeDocked(wwin) ((wwin) && ((wwin)->wm_class||(wwin)->wm_instance))
470 #ifdef REDUCE_APPICONS
471 unsigned int
472 wAppIconReduceAppCount(WApplication *wapp)
474 WAppIconAppList *applist;
476 if (wapp == NULL)
477 return 0;
479 if (wapp->app_icon == NULL)
480 return 0;
482 /* If given a main window, check the applist
483 * and remove the if it exists
485 applist = wapp->app_icon->applist;
486 while (applist != NULL) {
487 if (applist->wapp == wapp) {
488 /* If this app owns the appicon, change the appicon's
489 * owner to the next app in the list or NULL
491 if (wapp->app_icon->icon->owner == applist->wapp->main_window_desc)
493 if (applist->next) {
494 wapp->app_icon->icon->owner = applist->next->wapp->main_window_desc;
495 } else if (applist->prev) {
496 wapp->app_icon->icon->owner = applist->prev->wapp->main_window_desc;
497 } else {
498 wapp->app_icon->icon->owner = NULL;
501 if (applist->prev)
502 applist->prev->next = applist->next;
504 if (applist->next)
505 applist->next->prev = applist->prev;
507 if (applist == wapp->app_icon->applist)
508 wapp->app_icon->applist = applist->next;
510 free(applist);
512 if (wapp->app_icon->applist != NULL)
513 wapp->app_icon->main_window = wapp->app_icon->applist->wapp->main_window;
515 return (--wapp->app_icon->num_apps);
517 applist = applist->next;
519 return (--wapp->app_icon->num_apps);
521 #endif
524 static void
525 hideCallback(WMenu *menu, WMenuEntry *entry)
527 WApplication *wapp = (WApplication*)entry->clientdata;
529 if (wapp->flags.hidden) {
530 wWorkspaceChange(menu->menu->screen_ptr, wapp->last_workspace);
531 wUnhideApplication(wapp, False, False);
532 } else {
533 wHideApplication(wapp);
538 static void
539 unhideHereCallback(WMenu *menu, WMenuEntry *entry)
541 WApplication *wapp = (WApplication*)entry->clientdata;
543 wUnhideApplication(wapp, False, True);
547 static void
548 setIconCallback(WMenu *menu, WMenuEntry *entry)
550 WAppIcon *icon = ((WApplication*)entry->clientdata)->app_icon;
551 char *file=NULL;
552 WScreen *scr;
553 int result;
555 assert(icon!=NULL);
557 if (icon->editing)
558 return;
559 icon->editing = 1;
560 scr = icon->icon->core->screen_ptr;
562 wretain(icon);
564 result = wIconChooserDialog(scr, &file, icon->wm_instance, icon->wm_class);
566 if (result && !icon->destroyed) {
567 if (file[0]==0) {
568 free(file);
569 file = NULL;
571 if (!wIconChangeImageFile(icon->icon, file)) {
572 wMessageDialog(scr, _("Error"),
573 _("Could not open specified icon file"),
574 _("OK"), NULL, NULL);
575 } else {
576 wDefaultChangeIcon(scr, icon->wm_instance, icon->wm_class, file);
577 wAppIconPaint(icon);
579 if (file)
580 free(file);
582 icon->editing = 0;
583 wrelease(icon);
587 static void
588 killCallback(WMenu *menu, WMenuEntry *entry)
590 WApplication *wapp = (WApplication*)entry->clientdata;
591 char *buffer;
593 if (!WCHECK_STATE(WSTATE_NORMAL))
594 return;
596 WCHANGE_STATE(WSTATE_MODAL);
598 assert(entry->clientdata!=NULL);
600 buffer = wstrappend(wapp->app_icon ? wapp->app_icon->wm_class : NULL,
601 _(" will be forcibly closed.\n"
602 "Any unsaved changes will be lost.\n"
603 "Please confirm."));
605 wretain(wapp->main_window_desc);
606 if (wPreferences.dont_confirm_kill
607 || wMessageDialog(menu->frame->screen_ptr, _("Kill Application"),
608 buffer, _("Yes"), _("No"), NULL)==WAPRDefault) {
609 if (!wapp->main_window_desc->flags.destroyed)
610 wClientKill(wapp->main_window_desc);
612 wrelease(wapp->main_window_desc);
614 free(buffer);
616 WCHANGE_STATE(WSTATE_NORMAL);
620 static WMenu*
621 createApplicationMenu(WScreen *scr)
623 WMenu *menu;
625 menu = wMenuCreate(scr, NULL, False);
626 wMenuAddCallback(menu, _("Unhide Here"), unhideHereCallback, NULL);
627 wMenuAddCallback(menu, _("Hide"), hideCallback, NULL);
628 wMenuAddCallback(menu, _("Set Icon..."), setIconCallback, NULL);
629 wMenuAddCallback(menu, _("Kill"), killCallback, NULL);
631 return menu;
635 static void
636 openApplicationMenu(WApplication *wapp, int x, int y)
638 WMenu *menu;
639 WScreen *scr = wapp->main_window_desc->screen_ptr;
640 int i;
642 if (!scr->icon_menu) {
643 scr->icon_menu = createApplicationMenu(scr);
644 free(scr->icon_menu->entries[1]->text);
647 menu = scr->icon_menu;
649 if (wapp->flags.hidden) {
650 menu->entries[1]->text = _("Unhide");
651 } else {
652 menu->entries[1]->text = _("Hide");
655 menu->flags.realized = 0;
656 wMenuRealize(menu);
658 x -= menu->frame->core->width/2;
659 if (x + menu->frame->core->width > scr->scr_width)
660 x = scr->scr_width - menu->frame->core->width;
661 if (x < 0)
662 x = 0;
664 /* set client data */
665 for (i = 0; i < menu->entry_no; i++) {
666 menu->entries[i]->clientdata = wapp;
668 wMenuMapAt(menu, x, y, False);
672 /******************************************************************/
674 static void
675 iconExpose(WObjDescriptor *desc, XEvent *event)
677 wAppIconPaint(desc->parent);
681 static void
682 iconDblClick(WObjDescriptor *desc, XEvent *event)
684 WAppIcon *aicon = desc->parent;
685 WApplication *wapp;
686 WScreen *scr = aicon->icon->core->screen_ptr;
687 int unhideHere;
689 #ifndef REDUCE_APPICONS
690 assert(aicon->icon->owner!=NULL);
691 #else
692 if (aicon->icon->owner == NULL) {
693 fprintf(stderr, "Double-click disabled: missing main window.\n");
694 return;
696 #endif
698 wapp = wApplicationOf(aicon->icon->owner->main_window);
699 #ifdef DEBUG0
700 if (!wapp) {
701 wwarning("could not find application descriptor for app icon!!");
702 return;
704 #endif
705 #ifdef REDUCE_APPICONS
706 if (!wapp) {
707 fprintf(stderr, "Double-click disabled: missing wapp.\n");
708 return;
710 #endif /* REDUCE_APPICONS */
712 unhideHere = (event->xbutton.state & ShiftMask);
714 /* go to the last workspace that the user worked on the app */
715 if (!unhideHere && wapp->last_workspace != scr->current_workspace)
716 wWorkspaceChange(scr, wapp->last_workspace);
718 wUnhideApplication(wapp, event->xbutton.button==Button2, unhideHere);
720 if (event->xbutton.state & MOD_MASK) {
721 wHideOtherApplications(aicon->icon->owner);
726 void
727 appIconMouseDown(WObjDescriptor *desc, XEvent *event)
729 WAppIcon *aicon = desc->parent;
730 WIcon *icon = aicon->icon;
731 XEvent ev;
732 int x=aicon->x_pos, y=aicon->y_pos;
733 int dx=event->xbutton.x, dy=event->xbutton.y;
734 int grabbed=0;
735 int done=0;
736 int superfluous = wPreferences.superfluous; /* we catch it to avoid problems */
737 WScreen *scr = icon->core->screen_ptr;
738 WWorkspace *workspace = scr->workspaces[scr->current_workspace];
739 int shad_x = 0, shad_y = 0, docking=0, dockable, collapsed = 0;
740 int ix, iy;
741 int clickButton = event->xbutton.button;
742 Pixmap ghost = None;
743 Window wins[2];
745 if (aicon->editing || WCHECK_STATE(WSTATE_MODAL))
746 return;
748 if (IsDoubleClick(scr, event)) {
749 iconDblClick(desc, event);
750 return;
753 if (event->xbutton.button == Button3) {
754 WObjDescriptor *desc;
755 WApplication *wapp = wApplicationOf(aicon->icon->owner->main_window);
757 if (!wapp)
758 return;
760 openApplicationMenu(wapp, event->xbutton.x_root,
761 event->xbutton.y_root);
763 /* allow drag select of menu */
764 desc = &scr->icon_menu->menu->descriptor;
765 event->xbutton.send_event = True;
766 (*desc->handle_mousedown)(desc, event);
767 return;
770 #ifdef DEBUG
771 puts("Moving icon");
772 #endif
773 if (event->xbutton.state & MOD_MASK)
774 wLowerFrame(icon->core);
775 else
776 wRaiseFrame(icon->core);
779 if (XGrabPointer(dpy, icon->core->window, True, ButtonMotionMask
780 |ButtonReleaseMask|ButtonPressMask, GrabModeAsync,
781 GrabModeAsync, None, None, CurrentTime) !=GrabSuccess) {
782 wwarning("pointer grab failed for appicon move");
785 if (wPreferences.flags.nodock && wPreferences.flags.noclip)
786 dockable = 0;
787 else
788 dockable = canBeDocked(icon->owner);
790 wins[0] = icon->core->window;
791 wins[1] = scr->dock_shadow;
792 XRestackWindows(dpy, wins, 2);
793 if (superfluous) {
794 if (icon->pixmap!=None)
795 ghost = MakeGhostIcon(scr, icon->pixmap);
796 else
797 ghost = MakeGhostIcon(scr, icon->core->window);
798 XSetWindowBackgroundPixmap(dpy, scr->dock_shadow,
799 ghost);
800 XClearWindow(dpy, scr->dock_shadow);
803 while (!done) {
804 WMMaskEvent(dpy, PointerMotionMask|ButtonReleaseMask|ButtonPressMask
805 |ButtonMotionMask|ExposureMask, &ev);
806 switch (ev.type) {
807 case Expose:
808 WMHandleEvent(&ev);
809 break;
811 case MotionNotify:
812 if (!grabbed) {
813 if (abs(dx-ev.xmotion.x)>=MOVE_THRESHOLD
814 || abs(dy-ev.xmotion.y)>=MOVE_THRESHOLD) {
815 XChangeActivePointerGrab(dpy, ButtonMotionMask
816 |ButtonReleaseMask|ButtonPressMask,
817 wCursor[WCUR_MOVE], CurrentTime);
818 grabbed=1;
819 } else {
820 break;
823 x = ev.xmotion.x_root - dx;
824 y = ev.xmotion.y_root - dy;
825 XMoveWindow(dpy, icon->core->window, x, y);
827 if (dockable) {
828 if (scr->dock && wDockSnapIcon(scr->dock, aicon, x, y,
829 &ix, &iy, False)) {
830 shad_x = scr->dock->x_pos + ix*wPreferences.icon_size;
831 shad_y = scr->dock->y_pos + iy*wPreferences.icon_size;
833 if (scr->last_dock != scr->dock && collapsed) {
834 scr->last_dock->collapsed = 1;
835 wDockHideIcons(scr->last_dock);
836 collapsed = 0;
838 if (!collapsed && (collapsed = scr->dock->collapsed)) {
839 scr->dock->collapsed = 0;
840 wDockShowIcons(scr->dock);
843 if (scr->dock->auto_raise_lower)
844 wDockRaise(scr->dock);
846 scr->last_dock = scr->dock;
848 XMoveWindow(dpy, scr->dock_shadow, shad_x, shad_y);
849 if (!docking) {
850 XMapWindow(dpy, scr->dock_shadow);
852 docking = 1;
853 } else if (workspace->clip &&
854 wDockSnapIcon(workspace->clip, aicon, x, y,
855 &ix, &iy, False)) {
856 shad_x = workspace->clip->x_pos + ix*wPreferences.icon_size;
857 shad_y = workspace->clip->y_pos + iy*wPreferences.icon_size;
859 if (scr->last_dock != workspace->clip && collapsed) {
860 scr->last_dock->collapsed = 1;
861 wDockHideIcons(scr->last_dock);
862 collapsed = 0;
864 if (!collapsed && (collapsed = workspace->clip->collapsed)) {
865 workspace->clip->collapsed = 0;
866 wDockShowIcons(workspace->clip);
869 if (workspace->clip->auto_raise_lower)
870 wDockRaise(workspace->clip);
872 scr->last_dock = workspace->clip;
874 XMoveWindow(dpy, scr->dock_shadow, shad_x, shad_y);
875 if (!docking) {
876 XMapWindow(dpy, scr->dock_shadow);
878 docking = 1;
879 } else if (docking) {
880 XUnmapWindow(dpy, scr->dock_shadow);
881 docking = 0;
885 break;
887 case ButtonPress:
888 break;
890 case ButtonRelease:
891 if (ev.xbutton.button != clickButton)
892 break;
893 XUngrabPointer(dpy, CurrentTime);
895 if (docking) {
896 Bool docked;
898 /* icon is trying to be docked */
899 SlideWindow(icon->core->window, x, y, shad_x, shad_y);
900 XUnmapWindow(dpy, scr->dock_shadow);
901 docked = wDockAttachIcon(scr->last_dock, aicon, ix, iy);
902 if (scr->last_dock->auto_collapse) {
903 collapsed = 0;
905 if (workspace->clip &&
906 workspace->clip != scr->last_dock &&
907 workspace->clip->auto_raise_lower)
908 wDockLower(workspace->clip);
910 if (!docked) {
911 /* If icon could not be docked, slide it back to the old
912 * position */
913 SlideWindow(icon->core->window, x, y, aicon->x_pos,
914 aicon->y_pos);
916 } else {
917 XMoveWindow(dpy, icon->core->window, x, y);
918 aicon->x_pos = x;
919 aicon->y_pos = y;
920 if (workspace->clip && workspace->clip->auto_raise_lower)
921 wDockLower(workspace->clip);
923 if (collapsed) {
924 scr->last_dock->collapsed = 1;
925 wDockHideIcons(scr->last_dock);
926 collapsed = 0;
928 if (superfluous) {
929 if (ghost!=None)
930 XFreePixmap(dpy, ghost);
931 XSetWindowBackground(dpy, scr->dock_shadow, scr->white_pixel);
934 if (wPreferences.auto_arrange_icons)
935 wArrangeIcons(scr, True);
937 done = 1;
938 break;
941 #ifdef DEBUG
942 puts("End icon move");
943 #endif