Various bug fixes.
[wmaker-crm.git] / src / appicon.c
blobeda33b064d941f16cc732bf7caa2dd90bcc6730c
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
52 * icon_file for the dock is got from the preferences file by
53 * using the classname/instancename
56 /**** Global variables ****/
57 extern Cursor wCursor[WCUR_LAST];
58 extern WPreferences wPreferences;
60 #define MOD_MASK wPreferences.modifier_mask
62 void appIconMouseDown(WObjDescriptor *desc, XEvent *event);
63 static void iconDblClick(WObjDescriptor *desc, XEvent *event);
64 static void iconExpose(WObjDescriptor *desc, XEvent *event);
68 WAppIcon*
69 wAppIconCreateForDock(WScreen *scr, char *command, char *wm_instance,
70 char *wm_class, int tile)
72 WAppIcon *dicon;
73 char *path;
75 dicon = wmalloc(sizeof(WAppIcon));
76 wretain(dicon);
77 memset(dicon, 0, sizeof(WAppIcon));
78 dicon->yindex = -1;
79 dicon->xindex = -1;
81 dicon->prev = NULL;
82 dicon->next = scr->app_icon_list;
83 if (scr->app_icon_list) {
84 scr->app_icon_list->prev = dicon;
86 scr->app_icon_list = dicon;
88 if (command) {
89 dicon->command = wstrdup(command);
91 if (wm_class)
92 dicon->wm_class = wstrdup(wm_class);
93 if (wm_instance)
94 dicon->wm_instance = wstrdup(wm_instance);
96 path = wDefaultGetIconFile(scr, wm_instance, wm_class, True);
97 if (!path && command) {
98 wApplicationExtractDirPackIcon(scr, command, wm_instance, wm_class);
100 path = wDefaultGetIconFile(scr, wm_instance, wm_class, False);
103 if (path)
104 path = FindImage(wPreferences.icon_path, path);
106 dicon->icon = wIconCreateWithIconFile(scr, path, tile);
107 if (path)
108 free(path);
109 #ifdef XDND
110 wXDNDMakeAwareness(dicon->icon->core->window);
111 #endif
112 #ifdef REDUCE_APPICONS
113 dicon->num_apps = 0;
114 #endif
115 #ifdef DEMATERIALIZE_ICON
117 XSetWindowAttributes attribs;
118 attribs.save_under = True;
119 XChangeWindowAttributes(dpy, dicon->icon->core->window,
120 CWSaveUnder, &attribs);
122 #endif
124 /* will be overriden by dock */
125 dicon->icon->core->descriptor.handle_mousedown = appIconMouseDown;
126 dicon->icon->core->descriptor.handle_expose = iconExpose;
127 dicon->icon->core->descriptor.parent_type = WCLASS_APPICON;
128 dicon->icon->core->descriptor.parent = dicon;
129 AddToStackList(dicon->icon->core);
131 return dicon;
136 WAppIcon*
137 wAppIconCreate(WWindow *leader_win)
139 WAppIcon *aicon;
140 #ifdef REDUCE_APPICONS
141 WAppIcon *atmp;
142 WAppIconAppList *applist;
143 char *tinstance, *tclass;
144 #endif
145 WScreen *scr = leader_win->screen_ptr;
147 aicon = wmalloc(sizeof(WAppIcon));
148 wretain(aicon);
149 memset(aicon, 0, sizeof(WAppIcon));
150 #ifdef REDUCE_APPICONS
151 applist = wmalloc(sizeof(WAppIconAppList));
152 memset(applist, 0, sizeof(WAppIconAppList));
153 applist->wapp = wApplicationOf(leader_win->main_window);
154 aicon->applist = applist;
155 if (applist->wapp == NULL) {
156 /* Something's wrong. wApplicationOf() should always return a
157 * valid structure. Rather than violate assumptions, bail. -cls
159 free(applist);
160 wrelease(aicon);
161 return NULL;
163 #endif
165 aicon->yindex = -1;
166 aicon->xindex = -1;
168 aicon->prev = NULL;
169 aicon->next = scr->app_icon_list;
170 if (scr->app_icon_list) {
171 #ifndef REDUCE_APPICONS
172 scr->app_icon_list->prev = aicon;
173 #else
174 /* If we aren't going to have a match, jump straight to new appicon */
175 if (leader_win->wm_class == NULL || leader_win->wm_class == NULL)
176 atmp = NULL;
177 else
178 atmp = scr->app_icon_list;
180 while (atmp != NULL) {
181 if ((tinstance = atmp->wm_instance) == NULL)
182 tinstance = "";
183 if ((tclass = atmp->wm_class) == NULL)
184 tclass = "";
185 if ((strcmp(leader_win->wm_class, tclass) == 0) &&
186 (strcmp(leader_win->wm_instance, tinstance) == 0))
188 /* We have a winner */
189 wrelease(aicon);
190 atmp->num_apps++;
191 applist->next = atmp->applist;
192 if (atmp->applist)
193 atmp->applist->prev = applist;
194 atmp->applist = applist;
196 if (atmp->docked) {
197 wDockSimulateLaunch(atmp->dock, atmp);
200 return atmp;
202 atmp = atmp->next;
204 if (atmp == NULL) {
205 scr->app_icon_list->prev = aicon;
207 #endif /* REDUCE_APPICONS */
209 scr->app_icon_list = aicon;
211 if (leader_win->wm_class)
212 aicon->wm_class = wstrdup(leader_win->wm_class);
213 if (leader_win->wm_instance)
214 aicon->wm_instance = wstrdup(leader_win->wm_instance);
215 #ifdef REDUCE_APPICONS
216 aicon->num_apps = 1;
217 #endif
219 aicon->icon = wIconCreate(leader_win);
220 #ifdef DEMATERIALIZE_ICON
222 XSetWindowAttributes attribs;
223 attribs.save_under = True;
224 XChangeWindowAttributes(dpy, aicon->icon->core->window,
225 CWSaveUnder, &attribs);
227 #endif
228 #ifdef XDND
229 wXDNDMakeAwareness(aicon->icon->core->window);
230 #endif
232 /* will be overriden if docked */
233 aicon->icon->core->descriptor.handle_mousedown = appIconMouseDown;
234 aicon->icon->core->descriptor.handle_expose = iconExpose;
235 aicon->icon->core->descriptor.parent_type = WCLASS_APPICON;
236 aicon->icon->core->descriptor.parent = aicon;
237 AddToStackList(aicon->icon->core);
238 aicon->icon->show_title = 0;
239 wIconUpdate(aicon->icon);
241 return aicon;
245 void
246 wAppIconDestroy(WAppIcon *aicon)
248 WScreen *scr = aicon->icon->core->screen_ptr;
249 #ifdef REDUCE_APPICONS
250 WAppIconAppList *aptmp;
251 #endif
253 RemoveFromStackList(aicon->icon->core);
254 wIconDestroy(aicon->icon);
255 if (aicon->command)
256 free(aicon->command);
257 #ifdef OFFIX_DND
258 if (aicon->dnd_command)
259 free(aicon->dnd_command);
260 #endif
261 if (aicon->wm_instance)
262 free(aicon->wm_instance);
263 if (aicon->wm_class)
264 free(aicon->wm_class);
265 #ifdef REDUCE_APPICONS
266 /* There should never be a list but just in case */
267 if (aicon->applist != NULL) {
268 aptmp = aicon->applist;
269 while (aptmp->next) {
270 aptmp = aptmp->next;
271 free(aptmp->prev);
273 free(aptmp);
275 #endif
277 if (aicon == scr->app_icon_list) {
278 if (aicon->next)
279 aicon->next->prev = NULL;
280 scr->app_icon_list = aicon->next;
282 else {
283 if (aicon->next)
284 aicon->next->prev = aicon->prev;
285 if (aicon->prev)
286 aicon->prev->next = aicon->next;
289 aicon->destroyed = 1;
290 wrelease(aicon);
295 #ifdef NEWAPPICON
296 static void
297 drawCorner(WIcon *icon, WWindow *wwin, int active)
299 WScreen *scr = wwin->screen_ptr;
300 XPoint points[3];
301 GC gc;
303 points[0].x = 2;
304 points[0].y = 2;
305 points[1].x = 12;
306 points[1].y = 2;
307 points[2].x = 2;
308 points[2].y = 12;
309 if (active) {
310 gc=scr->focused_texture->any.gc;
311 } else {
312 gc=scr->unfocused_texture->any.gc;
314 XFillPolygon(dpy, icon->core->window, gc, points, 3,
315 Convex, CoordModeOrigin);
317 #endif /* NEWAPPICON */
320 static void
321 drawCorner(WIcon *icon)
323 WScreen *scr = icon->core->screen_ptr;
324 XPoint points[3];
326 points[0].x = 1;
327 points[0].y = 1;
328 points[1].x = 12;
329 points[1].y = 1;
330 points[2].x = 1;
331 points[2].y = 12;
332 XFillPolygon(dpy, icon->core->window, scr->icon_title_texture->normal_gc,
333 points, 3, Convex, CoordModeOrigin);
334 XDrawLine(dpy, icon->core->window, scr->icon_title_texture->light_gc,
335 0, 0, 0, 12);
336 XDrawLine(dpy, icon->core->window, scr->icon_title_texture->light_gc,
337 0, 0, 12, 0);
338 /* drawing the second line gives a weird concave look. -Dan */
339 #if 0
340 XDrawLine(dpy, icon->core->window, scr->icon_title_texture->light_gc,
341 1, 1, 1, 11);
342 XDrawLine(dpy, icon->core->window, scr->icon_title_texture->light_gc,
343 1, 1, 11, 1);
344 #endif
348 void
349 wAppIconMove(WAppIcon *aicon, int x, int y)
351 XMoveWindow(dpy, aicon->icon->core->window, x, y);
352 aicon->x_pos = x;
353 aicon->y_pos = y;
357 #ifdef WS_INDICATOR
358 static void
359 updateDockNumbers(WScreen *scr)
361 int length;
362 char *ws_numbers;
363 GC numbers_gc;
364 XGCValues my_gc_values;
365 unsigned long my_v_mask = (GCForeground);
366 WAppIcon *dicon = scr->dock->icon_array[0];
368 my_gc_values.foreground = scr->white_pixel;
369 numbers_gc = XCreateGC(dpy, dicon->icon->core->window,
370 my_v_mask, &my_gc_values);
372 ws_numbers = malloc(20);
373 sprintf(ws_numbers, "%i [ %i ]", scr->current_workspace+1,
374 ((scr->current_workspace/10)+1));
375 length = strlen(ws_numbers);
377 XClearArea(dpy, dicon->icon->core->window, 2, 2, 50,
378 scr->icon_title_font->y+1, False);
380 XSetForeground(dpy, numbers_gc, scr->black_pixel);
381 WMDrawString(scr->wmscreen, dicon->icon->core->window, numbers_gc,
382 scr->icon_title_font, 4, 3, ws_numbers, length);
384 XSetForeground(dpy, numbers_gc, scr->white_pixel);
385 WMDrawString(scr->wmscreen, dicon->icon->core->window, numbers_gc,
386 scr->icon_title_font, 3, 2, ws_numbers, length);
388 XFreeGC(dpy, numbers_gc);
389 free(ws_numbers);
391 #endif /* WS_INDICATOR */
394 void
395 wAppIconPaint(WAppIcon *aicon)
397 WScreen *scr = aicon->icon->core->screen_ptr;
399 wIconPaint(aicon->icon);
402 # ifdef WS_INDICATOR
403 if (aicon->docked && scr->dock && aicon->yindex==0)
404 updateDockNumbers(scr);
405 # endif
406 if (scr->dock_dots && aicon->docked && !aicon->running
407 && aicon->command!=NULL) {
408 XSetClipMask(dpy, scr->copy_gc, scr->dock_dots->mask);
409 XSetClipOrigin(dpy, scr->copy_gc, 0, 0);
410 XCopyArea(dpy, scr->dock_dots->image, aicon->icon->core->window,
411 scr->copy_gc, 0, 0, scr->dock_dots->width,
412 scr->dock_dots->height, 0, 0);
415 #ifdef HIDDENDOT
417 WApplication *wapp;
418 wapp = wApplicationOf(aicon->main_window);
419 if (wapp && wapp->flags.hidden) {
420 XSetClipMask(dpy, scr->copy_gc, scr->dock_dots->mask);
421 XSetClipOrigin(dpy, scr->copy_gc, 0, 0);
422 XCopyArea(dpy, scr->dock_dots->image,
423 aicon->icon->core->window,
424 scr->copy_gc, 0, 0, 7,
425 scr->dock_dots->height, 0, 0);
428 #endif /* HIDDENDOT */
430 #ifdef NEWAPPICON
431 if (!wPreferences.strict_ns && aicon->icon->owner!=NULL) {
432 int active=0;
433 if (aicon->main_window==None) {
434 active = (aicon->icon->owner->flags.focused ? 1 : 0);
435 } else {
436 WApplication *wapp;
438 wapp = wApplicationOf(aicon->main_window);
439 if (!wapp) {
440 active = aicon->icon->owner->flags.focused;
441 wwarning("error in wAppIconPaint(). Please report it");
442 } else {
443 active = wapp->main_window_desc->flags.focused;
444 if (wapp->main_window_desc->flags.hidden
445 || !wapp->main_window_desc->flags.mapped)
446 active = -1;
449 if (active>=0)
450 drawCorner(aicon->icon, aicon->icon->owner, active);
452 #endif /* NEWAPPICON */
454 if (aicon->omnipresent)
455 drawCorner(aicon->icon);
457 XSetClipMask(dpy, scr->copy_gc, None);
458 if (aicon->launching) {
459 XFillRectangle(dpy, aicon->icon->core->window, scr->stipple_gc,
460 0, 0, wPreferences.icon_size, wPreferences.icon_size);
466 #define canBeDocked(wwin) ((wwin) && ((wwin)->wm_class||(wwin)->wm_instance))
468 #ifdef REDUCE_APPICONS
469 unsigned int
470 wAppIconReduceAppCount(WApplication *wapp)
472 WAppIconAppList *applist;
474 if (wapp == NULL)
475 return 0;
477 if (wapp->app_icon == NULL)
478 return 0;
480 /* If given a main window, check the applist
481 * and remove the if it exists
483 applist = wapp->app_icon->applist;
484 while (applist != NULL) {
485 if (applist->wapp == wapp) {
486 /* If this app owns the appicon, change the appicon's
487 * owner to the next app in the list or NULL
489 if (wapp->app_icon->icon->owner == applist->wapp->main_window_desc)
491 if (applist->next) {
492 wapp->app_icon->icon->owner = applist->next->wapp->main_window_desc;
493 } else if (applist->prev) {
494 wapp->app_icon->icon->owner = applist->prev->wapp->main_window_desc;
495 } else {
496 wapp->app_icon->icon->owner = NULL;
499 if (applist->prev)
500 applist->prev->next = applist->next;
502 if (applist->next)
503 applist->next->prev = applist->prev;
505 if (applist == wapp->app_icon->applist)
506 wapp->app_icon->applist = applist->next;
508 free(applist);
510 if (wapp->app_icon->applist != NULL)
511 wapp->app_icon->main_window = wapp->app_icon->applist->wapp->main_window;
513 return (--wapp->app_icon->num_apps);
515 applist = applist->next;
517 return (--wapp->app_icon->num_apps);
519 #endif
522 static void
523 hideCallback(WMenu *menu, WMenuEntry *entry)
525 WApplication *wapp = (WApplication*)entry->clientdata;
527 if (wapp->flags.hidden) {
528 wWorkspaceChange(menu->menu->screen_ptr, wapp->last_workspace);
529 wUnhideApplication(wapp, False, False);
530 } else {
531 wHideApplication(wapp);
536 static void
537 unhideHereCallback(WMenu *menu, WMenuEntry *entry)
539 WApplication *wapp = (WApplication*)entry->clientdata;
541 wUnhideApplication(wapp, False, True);
545 static void
546 setIconCallback(WMenu *menu, WMenuEntry *entry)
548 WAppIcon *icon = ((WApplication*)entry->clientdata)->app_icon;
549 char *file=NULL;
550 WScreen *scr;
551 int result;
553 assert(icon!=NULL);
555 if (icon->editing)
556 return;
557 icon->editing = 1;
558 scr = icon->icon->core->screen_ptr;
560 wretain(icon);
562 result = wIconChooserDialog(scr, &file, icon->wm_instance, icon->wm_class);
564 if (result && !icon->destroyed) {
565 if (file[0]==0) {
566 free(file);
567 file = NULL;
569 if (!wIconChangeImageFile(icon->icon, file)) {
570 wMessageDialog(scr, _("Error"),
571 _("Could not open specified icon file"),
572 _("OK"), NULL, NULL);
573 } else {
574 wDefaultChangeIcon(scr, icon->wm_instance, icon->wm_class, file);
575 wAppIconPaint(icon);
577 if (file)
578 free(file);
580 icon->editing = 0;
581 wrelease(icon);
585 static void
586 killCallback(WMenu *menu, WMenuEntry *entry)
588 WApplication *wapp = (WApplication*)entry->clientdata;
589 char *buffer;
591 if (!WCHECK_STATE(WSTATE_NORMAL))
592 return;
594 WCHANGE_STATE(WSTATE_MODAL);
596 assert(entry->clientdata!=NULL);
598 buffer = wstrappend(wapp->app_icon ? wapp->app_icon->wm_class : NULL,
599 _(" will be forcibly closed.\n"
600 "Any unsaved changes will be lost.\n"
601 "Please confirm."));
603 wretain(wapp->main_window_desc);
604 if (wPreferences.dont_confirm_kill
605 || wMessageDialog(menu->frame->screen_ptr, _("Kill Application"),
606 buffer, _("Yes"), _("No"), NULL)==WAPRDefault) {
607 if (!wapp->main_window_desc->flags.destroyed)
608 wClientKill(wapp->main_window_desc);
610 wrelease(wapp->main_window_desc);
612 free(buffer);
614 WCHANGE_STATE(WSTATE_NORMAL);
618 static WMenu*
619 createApplicationMenu(WScreen *scr)
621 WMenu *menu;
623 menu = wMenuCreate(scr, NULL, False);
624 wMenuAddCallback(menu, _("Unhide Here"), unhideHereCallback, NULL);
625 wMenuAddCallback(menu, _("Hide"), hideCallback, NULL);
626 wMenuAddCallback(menu, _("Set Icon..."), setIconCallback, NULL);
627 wMenuAddCallback(menu, _("Kill"), killCallback, NULL);
629 return menu;
633 static void
634 openApplicationMenu(WApplication *wapp, int x, int y)
636 WMenu *menu;
637 WScreen *scr = wapp->main_window_desc->screen_ptr;
638 int i;
640 if (!scr->icon_menu) {
641 scr->icon_menu = createApplicationMenu(scr);
642 free(scr->icon_menu->entries[1]->text);
645 menu = scr->icon_menu;
647 if (wapp->flags.hidden) {
648 menu->entries[1]->text = _("Unhide");
649 } else {
650 menu->entries[1]->text = _("Hide");
653 menu->flags.realized = 0;
654 wMenuRealize(menu);
656 x -= menu->frame->core->width/2;
657 if (x + menu->frame->core->width > scr->scr_width)
658 x = scr->scr_width - menu->frame->core->width;
659 if (x < 0)
660 x = 0;
662 /* set client data */
663 for (i = 0; i < menu->entry_no; i++) {
664 menu->entries[i]->clientdata = wapp;
666 wMenuMapAt(menu, x, y, False);
670 /******************************************************************/
672 static void
673 iconExpose(WObjDescriptor *desc, XEvent *event)
675 wAppIconPaint(desc->parent);
679 static void
680 iconDblClick(WObjDescriptor *desc, XEvent *event)
682 WAppIcon *aicon = desc->parent;
683 WApplication *wapp;
684 WScreen *scr = aicon->icon->core->screen_ptr;
685 int unhideHere;
687 #ifndef REDUCE_APPICONS
688 assert(aicon->icon->owner!=NULL);
689 #else
690 if (aicon->icon->owner == NULL) {
691 fprintf(stderr, "Double-click disabled: missing main window.\n");
692 return;
694 #endif
696 wapp = wApplicationOf(aicon->icon->owner->main_window);
697 #ifdef DEBUG0
698 if (!wapp) {
699 wwarning("could not find application descriptor for app icon!!");
700 return;
702 #endif
703 #ifdef REDUCE_APPICONS
704 if (!wapp) {
705 fprintf(stderr, "Double-click disabled: missing wapp.\n");
706 return;
708 #endif /* REDUCE_APPICONS */
710 unhideHere = (event->xbutton.state & ShiftMask);
712 /* go to the last workspace that the user worked on the app */
713 if (!unhideHere && wapp->last_workspace != scr->current_workspace)
714 wWorkspaceChange(scr, wapp->last_workspace);
716 wUnhideApplication(wapp, event->xbutton.button==Button2, unhideHere);
718 if (event->xbutton.state & MOD_MASK) {
719 wHideOtherApplications(aicon->icon->owner);
724 void
725 appIconMouseDown(WObjDescriptor *desc, XEvent *event)
727 WAppIcon *aicon = desc->parent;
728 WIcon *icon = aicon->icon;
729 XEvent ev;
730 int x=aicon->x_pos, y=aicon->y_pos;
731 int dx=event->xbutton.x, dy=event->xbutton.y;
732 int grabbed=0;
733 int done=0;
734 int superfluous = wPreferences.superfluous; /* we catch it to avoid problems */
735 WScreen *scr = icon->core->screen_ptr;
736 WWorkspace *workspace = scr->workspaces[scr->current_workspace];
737 int shad_x = 0, shad_y = 0, docking=0, dockable, collapsed = 0;
738 int ix, iy;
739 int clickButton = event->xbutton.button;
740 Pixmap ghost = None;
741 Window wins[2];
743 if (aicon->editing || WCHECK_STATE(WSTATE_MODAL))
744 return;
746 if (IsDoubleClick(scr, event)) {
747 iconDblClick(desc, event);
748 return;
751 if (event->xbutton.button == Button3) {
752 WObjDescriptor *desc;
753 WApplication *wapp = wApplicationOf(aicon->icon->owner->main_window);
755 if (!wapp)
756 return;
758 openApplicationMenu(wapp, event->xbutton.x_root,
759 event->xbutton.y_root);
761 /* allow drag select of menu */
762 desc = &scr->icon_menu->menu->descriptor;
763 event->xbutton.send_event = True;
764 (*desc->handle_mousedown)(desc, event);
765 return;
768 #ifdef DEBUG
769 puts("Moving icon");
770 #endif
771 if (event->xbutton.state & MOD_MASK)
772 wLowerFrame(icon->core);
773 else
774 wRaiseFrame(icon->core);
777 if (XGrabPointer(dpy, icon->core->window, True, ButtonMotionMask
778 |ButtonReleaseMask|ButtonPressMask, GrabModeAsync,
779 GrabModeAsync, None, None, CurrentTime) !=GrabSuccess) {
780 wwarning("pointer grab failed for appicon move");
783 if (wPreferences.flags.nodock && wPreferences.flags.noclip)
784 dockable = 0;
785 else
786 dockable = canBeDocked(icon->owner);
788 wins[0] = icon->core->window;
789 wins[1] = scr->dock_shadow;
790 XRestackWindows(dpy, wins, 2);
791 if (superfluous) {
792 if (icon->pixmap!=None)
793 ghost = MakeGhostIcon(scr, icon->pixmap);
794 else
795 ghost = MakeGhostIcon(scr, icon->core->window);
796 XSetWindowBackgroundPixmap(dpy, scr->dock_shadow,
797 ghost);
798 XClearWindow(dpy, scr->dock_shadow);
801 while (!done) {
802 WMMaskEvent(dpy, PointerMotionMask|ButtonReleaseMask|ButtonPressMask
803 |ButtonMotionMask|ExposureMask, &ev);
804 switch (ev.type) {
805 case Expose:
806 WMHandleEvent(&ev);
807 break;
809 case MotionNotify:
810 if (!grabbed) {
811 if (abs(dx-ev.xmotion.x)>=MOVE_THRESHOLD
812 || abs(dy-ev.xmotion.y)>=MOVE_THRESHOLD) {
813 XChangeActivePointerGrab(dpy, ButtonMotionMask
814 |ButtonReleaseMask|ButtonPressMask,
815 wCursor[WCUR_MOVE], CurrentTime);
816 grabbed=1;
817 } else {
818 break;
821 x = ev.xmotion.x_root - dx;
822 y = ev.xmotion.y_root - dy;
823 XMoveWindow(dpy, icon->core->window, x, y);
825 if (dockable) {
826 if (scr->dock && wDockSnapIcon(scr->dock, aicon, x, y,
827 &ix, &iy, False)) {
828 shad_x = scr->dock->x_pos + ix*wPreferences.icon_size;
829 shad_y = scr->dock->y_pos + iy*wPreferences.icon_size;
831 if (scr->last_dock != scr->dock && collapsed) {
832 scr->last_dock->collapsed = 1;
833 wDockHideIcons(scr->last_dock);
834 collapsed = 0;
836 if (!collapsed && (collapsed = scr->dock->collapsed)) {
837 scr->dock->collapsed = 0;
838 wDockShowIcons(scr->dock);
841 if (scr->dock->auto_raise_lower)
842 wDockRaise(scr->dock);
844 scr->last_dock = scr->dock;
846 XMoveWindow(dpy, scr->dock_shadow, shad_x, shad_y);
847 if (!docking) {
848 XMapWindow(dpy, scr->dock_shadow);
850 docking = 1;
851 } else if (workspace->clip &&
852 wDockSnapIcon(workspace->clip, aicon, x, y,
853 &ix, &iy, False)) {
854 shad_x = workspace->clip->x_pos + ix*wPreferences.icon_size;
855 shad_y = workspace->clip->y_pos + iy*wPreferences.icon_size;
857 if (scr->last_dock != workspace->clip && collapsed) {
858 scr->last_dock->collapsed = 1;
859 wDockHideIcons(scr->last_dock);
860 collapsed = 0;
862 if (!collapsed && (collapsed = workspace->clip->collapsed)) {
863 workspace->clip->collapsed = 0;
864 wDockShowIcons(workspace->clip);
867 if (workspace->clip->auto_raise_lower)
868 wDockRaise(workspace->clip);
870 scr->last_dock = workspace->clip;
872 XMoveWindow(dpy, scr->dock_shadow, shad_x, shad_y);
873 if (!docking) {
874 XMapWindow(dpy, scr->dock_shadow);
876 docking = 1;
877 } else if (docking) {
878 XUnmapWindow(dpy, scr->dock_shadow);
879 docking = 0;
883 break;
885 case ButtonPress:
886 break;
888 case ButtonRelease:
889 if (ev.xbutton.button != clickButton)
890 break;
891 XUngrabPointer(dpy, CurrentTime);
893 if (docking) {
894 Bool docked;
896 /* icon is trying to be docked */
897 SlideWindow(icon->core->window, x, y, shad_x, shad_y);
898 XUnmapWindow(dpy, scr->dock_shadow);
899 docked = wDockAttachIcon(scr->last_dock, aicon, ix, iy);
900 if (scr->last_dock->auto_collapse) {
901 collapsed = 0;
903 if (workspace->clip &&
904 workspace->clip != scr->last_dock &&
905 workspace->clip->auto_raise_lower)
906 wDockLower(workspace->clip);
908 if (!docked) {
909 /* If icon could not be docked, slide it back to the old
910 * position */
911 SlideWindow(icon->core->window, x, y, aicon->x_pos,
912 aicon->y_pos);
914 } else {
915 XMoveWindow(dpy, icon->core->window, x, y);
916 aicon->x_pos = x;
917 aicon->y_pos = y;
918 if (workspace->clip && workspace->clip->auto_raise_lower)
919 wDockLower(workspace->clip);
921 if (collapsed) {
922 scr->last_dock->collapsed = 1;
923 wDockHideIcons(scr->last_dock);
924 collapsed = 0;
926 if (superfluous) {
927 if (ghost!=None)
928 XFreePixmap(dpy, ghost);
929 XSetWindowBackground(dpy, scr->dock_shadow, scr->white_pixel);
932 if (wPreferences.auto_arrange_icons)
933 wArrangeIcons(scr, True);
935 done = 1;
936 break;
939 #ifdef DEBUG
940 puts("End icon move");
941 #endif