Fix some bugs, DisableMiniwindows, _KWM_WIN_ICON_GEOMETRY..
[wmaker-crm.git] / src / icon.c
blobb9a59501b29a9c1b8c0f9f9c266182419475df2f
1 /* icon.c - window icon and dock and appicon parent
2 *
3 * Window Maker window manager
4 *
5 * Copyright (c) 1997, 1998 Alfredo K. Kojima
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
20 * USA.
23 #include "wconfig.h"
25 #include <X11/Xlib.h>
26 #include <X11/Xutil.h>
27 #include <stdlib.h>
28 #include <stdio.h>
29 #include <string.h>
30 #include <unistd.h>
31 #include <ctype.h>
32 #include <wraster.h>
33 #include <sys/stat.h>
35 #include "WindowMaker.h"
36 #include "wcore.h"
37 #include "texture.h"
38 #include "window.h"
39 #include "icon.h"
40 #include "actions.h"
41 #include "funcs.h"
42 #include "stacking.h"
43 #include "application.h"
44 #include "defaults.h"
45 #include "appicon.h"
47 /**** Global variables ****/
48 extern WPreferences wPreferences;
50 #define MOD_MASK wPreferences.modifier_mask
52 extern Cursor wCursor[WCUR_LAST];
55 static void miniwindowExpose(WObjDescriptor *desc, XEvent *event);
56 static void miniwindowMouseDown(WObjDescriptor *desc, XEvent *event);
57 static void miniwindowDblClick(WObjDescriptor *desc, XEvent *event);
60 INLINE static void
61 getSize(Drawable d, unsigned int *w, unsigned int *h, unsigned int *dep)
63 Window rjunk;
64 int xjunk, yjunk;
65 unsigned int bjunk;
67 XGetGeometry(dpy, d, &rjunk, &xjunk, &yjunk, w, h, &bjunk, dep);
71 WIcon*
72 wIconCreate(WWindow *wwin)
74 WScreen *scr=wwin->screen_ptr;
75 WIcon *icon;
76 char *file;
77 unsigned long vmask = 0;
78 XSetWindowAttributes attribs;
80 icon = wmalloc(sizeof(WIcon));
81 memset(icon, 0, sizeof(WIcon));
82 icon->core = wCoreCreateTopLevel(scr, wwin->icon_x, wwin->icon_y,
83 wPreferences.icon_size,
84 wPreferences.icon_size, 0);
86 if (wPreferences.use_saveunders) {
87 vmask |= CWSaveUnder;
88 attribs.save_under = True;
90 /* a white border for selecting it */
91 vmask |= CWBorderPixel;
92 attribs.border_pixel = scr->white_pixel;
94 XChangeWindowAttributes(dpy, icon->core->window, vmask, &attribs);
97 /* will be overriden if this is an application icon */
98 icon->core->descriptor.handle_mousedown = miniwindowMouseDown;
99 icon->core->descriptor.handle_expose = miniwindowExpose;
100 icon->core->descriptor.parent_type = WCLASS_MINIWINDOW;
101 icon->core->descriptor.parent = icon;
103 icon->core->stacking = wmalloc(sizeof(WStacking));
104 icon->core->stacking->above = NULL;
105 icon->core->stacking->under = NULL;
106 icon->core->stacking->window_level = NORMAL_ICON_LEVEL;
107 icon->core->stacking->child_of = NULL;
109 icon->owner = wwin;
110 if (wwin->wm_hints && (wwin->wm_hints->flags & IconWindowHint)) {
111 if (wwin->client_win == wwin->main_window) {
112 WApplication *wapp;
113 /* do not let miniwindow steal app-icon's icon window */
114 wapp = wApplicationOf(wwin->client_win);
115 if (!wapp || wapp->app_icon==NULL)
116 icon->icon_win = wwin->wm_hints->icon_window;
117 } else {
118 icon->icon_win = wwin->wm_hints->icon_window;
121 #ifdef NO_MINIWINDOW_TITLES
122 icon->show_title = 0;
123 #else
124 icon->show_title = 1;
125 #endif
126 icon->image = wDefaultGetImage(scr, wwin->wm_instance, wwin->wm_class);
128 file = wDefaultGetIconFile(scr, wwin->wm_instance, wwin->wm_class,
129 False);
130 if (file) {
131 icon->file = wstrdup(file);
134 wGetIconName(dpy, wwin->client_win, &icon->icon_name);
136 icon->tile_type = TILE_NORMAL;
138 wIconUpdate(icon);
140 XFlush(dpy);
142 return icon;
146 WIcon*
147 wIconCreateWithIconFile(WScreen *scr, char *iconfile, int tile)
149 WIcon *icon;
150 unsigned long vmask = 0;
151 XSetWindowAttributes attribs;
153 icon = wmalloc(sizeof(WIcon));
154 memset(icon, 0, sizeof(WIcon));
155 icon->core = wCoreCreateTopLevel(scr, 0, 0, wPreferences.icon_size,
156 wPreferences.icon_size, 0);
157 if (wPreferences.use_saveunders) {
158 vmask = CWSaveUnder;
159 attribs.save_under = True;
161 /* a white border for selecting it */
162 vmask |= CWBorderPixel;
163 attribs.border_pixel = scr->white_pixel;
165 XChangeWindowAttributes(dpy, icon->core->window, vmask, &attribs);
167 /* will be overriden if this is a application icon */
168 icon->core->descriptor.handle_mousedown = miniwindowMouseDown;
169 icon->core->descriptor.handle_expose = miniwindowExpose;
170 icon->core->descriptor.parent_type = WCLASS_MINIWINDOW;
171 icon->core->descriptor.parent = icon;
173 icon->core->stacking = wmalloc(sizeof(WStacking));
174 icon->core->stacking->above = NULL;
175 icon->core->stacking->under = NULL;
176 icon->core->stacking->window_level = NORMAL_ICON_LEVEL;
177 icon->core->stacking->child_of = NULL;
179 if (iconfile) {
180 icon->image = RLoadImage(scr->rcontext, iconfile, 0);
181 if (!icon->image) {
182 wwarning(_("error loading image file \"%s\""), iconfile, RMessageForError(RErrorCode));
185 icon->image = wIconValidateIconSize(scr, icon->image);
187 icon->file = wstrdup(iconfile);
190 icon->tile_type = tile;
192 wIconUpdate(icon);
194 return icon;
199 void
200 wIconDestroy(WIcon *icon)
202 WCoreWindow *core = icon->core;
203 WScreen *scr = core->screen_ptr;
205 if (icon->handlerID)
206 WMDeleteTimerHandler(icon->handlerID);
208 if (icon->icon_win) {
209 int x=0, y=0;
211 if (icon->owner) {
212 x = icon->owner->icon_x;
213 y = icon->owner->icon_y;
215 XUnmapWindow(dpy, icon->icon_win);
216 XReparentWindow(dpy, icon->icon_win, scr->root_win, x, y);
218 if (icon->icon_name)
219 XFree(icon->icon_name);
221 if (icon->pixmap)
222 XFreePixmap(dpy, icon->pixmap);
224 if (icon->file)
225 free(icon->file);
227 if (icon->image!=NULL)
228 RDestroyImage(icon->image);
230 wCoreDestroy(icon->core);
231 free(icon);
236 static void
237 drawIconTitle(WScreen *scr, Pixmap pixmap, int height)
239 XFillRectangle(dpy, pixmap, scr->icon_title_texture->normal_gc,
240 0, 0, wPreferences.icon_size, height+1);
241 XDrawLine(dpy, pixmap, scr->icon_title_texture->light_gc, 0, 0,
242 wPreferences.icon_size, 0);
243 XDrawLine(dpy, pixmap, scr->icon_title_texture->light_gc, 0, 0,
244 0, height+1);
245 XDrawLine(dpy, pixmap, scr->icon_title_texture->dim_gc,
246 wPreferences.icon_size-1, 0, wPreferences.icon_size-1, height+1);
250 static Pixmap
251 makeIcon(WScreen *scr, RImage *icon, int titled, int shadowed, int tileType)
253 RImage *tile;
254 Pixmap pixmap;
255 int x, y, sx, sy;
256 unsigned w, h;
257 int theight = scr->icon_title_font->height;
259 if (tileType == TILE_NORMAL)
260 tile = RCloneImage(scr->icon_tile);
261 else {
262 assert(scr->clip_tile);
263 tile = RCloneImage(scr->clip_tile);
265 if (icon) {
266 w = (icon->width > wPreferences.icon_size)
267 ? wPreferences.icon_size : icon->width;
268 x = (wPreferences.icon_size - w) / 2;
269 sx = (icon->width - w)/2;
271 if (!titled) {
272 h = (icon->height > wPreferences.icon_size)
273 ? wPreferences.icon_size : icon->height;
274 y = (wPreferences.icon_size - h) / 2;
275 sy = (icon->height - h)/2;
276 } else {
277 h = (icon->height+theight > wPreferences.icon_size
278 ? wPreferences.icon_size-theight : icon->height);
279 y = theight+((int)wPreferences.icon_size-theight-h)/2;
280 sy = (icon->height - h)/2;
282 RCombineArea(tile, icon, sx, sy, w, h, x, y);
285 if (shadowed) {
286 RColor color;
288 color.red = scr->icon_back_texture->light.red >> 8;
289 color.green = scr->icon_back_texture->light.green >> 8;
290 color.blue = scr->icon_back_texture->light.blue >> 8;
291 color.alpha = 150; /* about 60% */
292 RClearImage(tile, &color);
295 if (!RConvertImage(scr->rcontext, tile, &pixmap)) {
296 wwarning(_("error rendering image:%s"), RMessageForError(RErrorCode));
298 RDestroyImage(tile);
300 if (titled)
301 drawIconTitle(scr, pixmap, theight);
303 return pixmap;
307 void
308 wIconChangeTitle(WIcon *icon, char *new_title)
310 int changed;
312 changed = (new_title==NULL && icon->icon_name!=NULL)
313 || (new_title!=NULL && icon->icon_name==NULL);
315 if (icon->icon_name!=NULL)
316 XFree(icon->icon_name);
318 icon->icon_name = new_title;
320 if (changed)
321 icon->force_paint = 1;
322 wIconPaint(icon);
326 void
327 wIconChangeImage(WIcon *icon, RImage *new_image)
329 assert(icon != NULL);
331 if (icon->image)
332 RDestroyImage(icon->image);
334 icon->image = wIconValidateIconSize(icon->core->screen_ptr, new_image);
336 wIconUpdate(icon);
340 RImage*
341 wIconValidateIconSize(WScreen *scr, RImage *icon)
343 RImage *tmp;
344 int w, h;
346 if (!icon)
347 return NULL;
349 if (icon->width > wPreferences.icon_size
350 || icon->height > wPreferences.icon_size) {
351 if (icon->width > icon->height) {
352 w = wPreferences.icon_size - 4;
353 h = w*icon->height/icon->width;
354 } else {
355 h = wPreferences.icon_size - 4;
356 w = h*icon->width/icon->height;
358 tmp = RScaleImage(icon, w, h);
359 RDestroyImage(icon);
360 icon = tmp;
363 return icon;
367 Bool
368 wIconChangeImageFile(WIcon *icon, char *file)
370 WScreen *scr = icon->core->screen_ptr;
371 RImage *image;
372 char *path;
373 int error = 0;
375 if (!file) {
376 wIconChangeImage(icon, NULL);
377 return True;
380 path = FindImage(wPreferences.icon_path, file);
382 if (path && (image = RLoadImage(scr->rcontext, path, 0))) {
383 wIconChangeImage(icon, image);
384 } else {
385 error = 1;
388 if (path)
389 free(path);
391 return !error;
396 static char*
397 getnameforicon(WWindow *wwin)
399 char *prefix, *suffix;
400 char *path;
401 int len;
403 if (wwin->wm_class && wwin->wm_instance) {
404 suffix = wmalloc(strlen(wwin->wm_class)+strlen(wwin->wm_instance)+2);
405 sprintf(suffix, "%s.%s", wwin->wm_instance, wwin->wm_class);
406 } else if (wwin->wm_class) {
407 suffix = wmalloc(strlen(wwin->wm_class)+1);
408 strcpy(suffix, wwin->wm_class);
409 } else if (wwin->wm_instance) {
410 suffix = wmalloc(strlen(wwin->wm_instance)+1);
411 strcpy(suffix, wwin->wm_instance);
412 } else {
413 return NULL;
416 prefix = wusergnusteppath();
417 len = strlen(prefix)+64+strlen(suffix);
418 path = wmalloc(len+1);
419 sprintf(path, "%s/.AppInfo", prefix);
421 if (access(path, F_OK)!=0) {
422 if (mkdir(path, S_IRUSR|S_IWUSR|S_IXUSR)) {
423 wsyserror(_("could not create directory %s"), path);
424 free(path);
425 free(suffix);
426 return NULL;
429 strcat(path, "/WindowMaker");
430 if (access(path, F_OK)!=0) {
431 if (mkdir(path, S_IRUSR|S_IWUSR|S_IXUSR)!=0) {
432 wsyserror(_("could not create directory %s"), path);
433 free(path);
434 free(suffix);
435 return NULL;
439 strcat(path, "/");
440 strcat(path, suffix);
441 strcat(path, ".xpm");
442 free(suffix);
444 return path;
449 * wIconStore--
450 * Stores the client supplied icon at ~/GNUstep/.AppInfo/WindowMaker
451 * and returns the path for that icon. Returns NULL if there is no
452 * client supplied icon or on failure.
454 * Side effects:
455 * New directories might be created.
457 char*
458 wIconStore(WIcon *icon)
460 char *path;
461 RImage *image;
462 WWindow *wwin = icon->owner;
464 if (!wwin || !wwin->wm_hints || !(wwin->wm_hints->flags & IconPixmapHint)
465 || wwin->wm_hints->icon_pixmap == None)
466 return NULL;
468 path = getnameforicon(wwin);
469 if (!path)
470 return NULL;
472 image = RCreateImageFromDrawable(icon->core->screen_ptr->rcontext,
473 wwin->wm_hints->icon_pixmap,
474 (wwin->wm_hints->flags & IconMaskHint)
475 ? wwin->wm_hints->icon_mask : None);
476 if (!image) {
477 free(path);
478 return NULL;
481 if (!RSaveImage(image, path, "XPM")) {
482 free(path);
483 path = NULL;
485 RDestroyImage(image);
487 return path;
492 void
493 wIconChangeIconWindow(WIcon *icon, Window new_window);
496 static void
497 cycleColor(void *data)
499 WIcon *icon = (WIcon*)data;
500 WScreen *scr = icon->core->screen_ptr;
501 XGCValues gcv;
503 icon->step--;
504 gcv.dash_offset = icon->step;
505 XChangeGC(dpy, scr->icon_select_gc, GCDashOffset, &gcv);
507 XDrawRectangle(dpy, icon->core->window, scr->icon_select_gc, 0, 0,
508 icon->core->width-1, icon->core->height-1);
509 icon->handlerID = WMAddTimerHandler(COLOR_CYCLE_DELAY, cycleColor, icon);
513 void
514 wIconSetHighlited(WIcon *icon, Bool flag)
516 if (icon->highlighted == flag) {
517 return;
520 icon->highlighted = flag;
521 wIconPaint(icon);
526 void
527 wIconSelect(WIcon *icon)
529 WScreen *scr = icon->core->screen_ptr;
530 icon->selected = !icon->selected;
532 if (icon->selected) {
533 icon->step = 0;
534 if (!wPreferences.dont_blink)
535 icon->handlerID = WMAddTimerHandler(10, cycleColor, icon);
536 else
537 XDrawRectangle(dpy, icon->core->window, scr->icon_select_gc, 0, 0,
538 icon->core->width-1, icon->core->height-1);
539 } else {
540 if (icon->handlerID) {
541 WMDeleteTimerHandler(icon->handlerID);
542 icon->handlerID = NULL;
544 XClearArea(dpy, icon->core->window, 0, 0, icon->core->width,
545 icon->core->height, True);
550 void
551 wIconUpdate(WIcon *icon)
553 WScreen *scr = icon->core->screen_ptr;
554 int title_height = scr->icon_title_font->height;
555 WWindow *wwin = icon->owner;
557 assert(scr->icon_tile!=NULL);
559 if (icon->pixmap!=None)
560 XFreePixmap(dpy, icon->pixmap);
561 icon->pixmap = None;
564 if (wwin && WFLAGP(wwin, always_user_icon))
565 goto user_icon;
567 /* use client specified icon window */
568 if (icon->icon_win!=None) {
569 XWindowAttributes attr;
570 int resize=0;
571 int width, height, depth;
572 int theight;
573 Pixmap pixmap;
575 getSize(icon->icon_win, &width, &height, &depth);
577 if (width > wPreferences.icon_size) {
578 resize = 1;
579 width = wPreferences.icon_size;
581 if (height > wPreferences.icon_size) {
582 resize = 1;
583 height = wPreferences.icon_size;
585 if (icon->show_title
586 && (height+title_height < wPreferences.icon_size)) {
587 pixmap = XCreatePixmap(dpy, scr->w_win, wPreferences.icon_size,
588 wPreferences.icon_size, scr->w_depth);
589 XSetClipMask(dpy, scr->copy_gc, None);
590 XCopyArea(dpy, scr->icon_tile_pixmap, pixmap, scr->copy_gc, 0, 0,
591 wPreferences.icon_size, wPreferences.icon_size, 0, 0);
592 drawIconTitle(scr, pixmap, title_height);
593 theight = title_height;
594 } else {
595 pixmap = None;
596 theight = 0;
597 XSetWindowBackgroundPixmap(dpy, icon->core->window,
598 scr->icon_tile_pixmap);
601 XSetWindowBorderWidth(dpy, icon->icon_win, 0);
602 XReparentWindow(dpy, icon->icon_win, icon->core->window,
603 (wPreferences.icon_size-width)/2,
604 theight+(wPreferences.icon_size-height-theight)/2);
605 if (resize)
606 XResizeWindow(dpy, icon->icon_win, width, height);
608 XMapWindow(dpy, icon->icon_win);
610 XAddToSaveSet(dpy, icon->icon_win);
612 icon->pixmap = pixmap;
614 if (XGetWindowAttributes(dpy, icon->icon_win, &attr)) {
615 if (attr.all_event_masks & ButtonPressMask) {
616 wHackedGrabButton(Button1, MOD_MASK, icon->core->window, True,
617 ButtonPressMask, GrabModeSync, GrabModeAsync,
618 None, wCursor[WCUR_ARROW]);
621 } else if (wwin && wwin->wm_hints
622 && (wwin->wm_hints->flags & IconPixmapHint)) {
623 int x, y;
624 unsigned int w, h;
625 Window jw;
626 int ji, dotitle;
627 unsigned int ju, d;
628 Pixmap pixmap;
630 if (!XGetGeometry(dpy, wwin->wm_hints->icon_pixmap, &jw,
631 &ji, &ji, &w, &h, &ju, &d)) {
632 icon->owner->wm_hints->flags &= ~IconPixmapHint;
633 goto user_icon;
636 pixmap = XCreatePixmap(dpy, icon->core->window, wPreferences.icon_size,
637 wPreferences.icon_size, scr->w_depth);
638 XSetClipMask(dpy, scr->copy_gc, None);
639 XCopyArea(dpy, scr->icon_tile_pixmap, pixmap, scr->copy_gc, 0, 0,
640 wPreferences.icon_size, wPreferences.icon_size, 0, 0);
642 if (w > wPreferences.icon_size)
643 w = wPreferences.icon_size;
644 x = (wPreferences.icon_size-w)/2;
646 if (icon->show_title && (title_height < wPreferences.icon_size)) {
647 drawIconTitle(scr, pixmap, title_height);
648 dotitle = 1;
650 if (h > wPreferences.icon_size - title_height - 2) {
651 h = wPreferences.icon_size - title_height - 2;
652 y = title_height + 1;
653 } else {
654 y = (wPreferences.icon_size-h-title_height)/2+title_height + 1;
656 } else {
657 dotitle = 0;
658 if (w > wPreferences.icon_size)
659 w = wPreferences.icon_size;
660 y = (wPreferences.icon_size-h)/2;
663 if (wwin->wm_hints->flags & IconMaskHint)
664 XSetClipMask(dpy, scr->copy_gc, wwin->wm_hints->icon_mask);
666 XSetClipOrigin(dpy, scr->copy_gc, x, y);
668 if (d != scr->w_depth) {
669 XSetForeground(dpy, scr->copy_gc, scr->black_pixel);
670 XSetBackground(dpy, scr->copy_gc, scr->white_pixel);
671 XCopyPlane(dpy, wwin->wm_hints->icon_pixmap, pixmap, scr->copy_gc,
672 0, 0, w, h, x, y, 1);
673 } else {
674 XCopyArea(dpy, wwin->wm_hints->icon_pixmap, pixmap, scr->copy_gc,
675 0, 0, w, h, x, y);
678 XSetClipOrigin(dpy, scr->copy_gc, 0, 0);
680 icon->pixmap = pixmap;
681 } else {
682 user_icon:
684 if (icon->image) {
685 icon->pixmap = makeIcon(scr, icon->image, icon->show_title,
686 icon->shadowed, icon->tile_type);
687 } else {
688 /* make default icons */
690 if (!scr->def_icon_pixmap) {
691 RImage *image = NULL;
692 char *path;
693 char *file;
695 file = wDefaultGetIconFile(scr, NULL, NULL, False);
696 if (file) {
697 path = FindImage(wPreferences.icon_path, file);
698 if (!path) {
699 wwarning(_("could not find default icon \"%s\""),file);
700 goto make_icons;
703 image = RLoadImage(scr->rcontext, path, 0);
704 if (!image) {
705 wwarning(_("could not load default icon \"%s\":%s"),
706 file, RMessageForError(RErrorCode));
708 free(path);
710 make_icons:
712 image = wIconValidateIconSize(scr, image);
713 scr->def_icon_pixmap = makeIcon(scr, image, False, False,
714 icon->tile_type);
715 scr->def_ticon_pixmap = makeIcon(scr, image, True, False,
716 icon->tile_type);
717 if (image)
718 RDestroyImage(image);
721 if (icon->show_title) {
722 XSetWindowBackgroundPixmap(dpy, icon->core->window,
723 scr->def_ticon_pixmap);
724 } else {
725 XSetWindowBackgroundPixmap(dpy, icon->core->window,
726 scr->def_icon_pixmap);
728 icon->pixmap = None;
731 if (icon->pixmap != None) {
732 XSetWindowBackgroundPixmap(dpy, icon->core->window, icon->pixmap);
734 XClearWindow(dpy, icon->core->window);
736 wIconPaint(icon);
741 void
742 wIconPaint(WIcon *icon)
744 WScreen *scr=icon->core->screen_ptr;
745 GC gc = scr->icon_title_gc;
746 int x;
747 char *tmp;
749 if (icon->force_paint) {
750 icon->force_paint = 0;
751 wIconUpdate(icon);
752 return;
755 XClearWindow(dpy, icon->core->window);
757 /* draw the icon title */
758 if (icon->show_title && icon->icon_name!=NULL) {
759 int l;
760 int w;
762 tmp = ShrinkString(scr->icon_title_font, icon->icon_name,
763 wPreferences.icon_size-4);
764 w = wTextWidth(scr->icon_title_font->font, tmp, l=strlen(tmp));
766 if (w > icon->core->width - 4)
767 x = (icon->core->width - 4) - w;
768 else
769 x = (icon->core->width - w)/2;
771 wDrawString(icon->core->window, scr->icon_title_font, gc,
772 x, 1 + scr->icon_title_font->y, tmp, l);
773 free(tmp);
776 if (icon->selected)
777 XDrawRectangle(dpy, icon->core->window, scr->icon_select_gc, 0, 0,
778 icon->core->width-1, icon->core->height-1);
782 /******************************************************************/
784 static void
785 miniwindowExpose(WObjDescriptor *desc, XEvent *event)
787 wIconPaint(desc->parent);
791 static void
792 miniwindowDblClick(WObjDescriptor *desc, XEvent *event)
794 WIcon *icon = desc->parent;
796 assert(icon->owner!=NULL);
798 wDeiconifyWindow(icon->owner);
802 static void
803 miniwindowMouseDown(WObjDescriptor *desc, XEvent *event)
805 WIcon *icon = desc->parent;
806 WWindow *wwin = icon->owner;
807 XEvent ev;
808 int x=wwin->icon_x, y=wwin->icon_y;
809 int dx=event->xbutton.x, dy=event->xbutton.y;
810 int grabbed=0;
811 int clickButton=event->xbutton.button;
813 if (WCHECK_STATE(WSTATE_MODAL))
814 return;
816 if (IsDoubleClick(icon->core->screen_ptr, event)) {
817 miniwindowDblClick(desc, event);
818 return;
821 #ifdef DEBUG
822 puts("Moving miniwindow");
823 #endif
824 if (event->xbutton.button == Button1) {
825 if (event->xbutton.state & MOD_MASK)
826 wLowerFrame(icon->core);
827 else
828 wRaiseFrame(icon->core);
829 if (event->xbutton.state & ShiftMask) {
830 wIconSelect(icon);
831 wSelectWindow(icon->owner, !wwin->flags.selected);
833 } else if (event->xbutton.button == Button3) {
834 WObjDescriptor *desc;
836 OpenMiniwindowMenu(wwin, event->xbutton.x_root,
837 event->xbutton.y_root);
839 /* allow drag select of menu */
840 desc = &wwin->screen_ptr->window_menu->menu->descriptor;
841 event->xbutton.send_event = True;
842 (*desc->handle_mousedown)(desc, event);
844 return;
847 if (XGrabPointer(dpy, icon->core->window, False, ButtonMotionMask
848 |ButtonReleaseMask|ButtonPressMask, GrabModeAsync,
849 GrabModeAsync, None, None, CurrentTime) !=GrabSuccess) {
850 #ifdef DEBUG0
851 wwarning("pointer grab failed for icon move");
852 #endif
854 while(1) {
855 WMMaskEvent(dpy, PointerMotionMask|ButtonReleaseMask|ButtonPressMask
856 |ButtonMotionMask|ExposureMask, &ev);
857 switch (ev.type) {
858 case Expose:
859 WMHandleEvent(&ev);
860 break;
862 case MotionNotify:
863 if (!grabbed) {
864 if (abs(dx-ev.xmotion.x)>=MOVE_THRESHOLD
865 || abs(dy-ev.xmotion.y)>=MOVE_THRESHOLD) {
866 XChangeActivePointerGrab(dpy, ButtonMotionMask
867 |ButtonReleaseMask|ButtonPressMask,
868 wCursor[WCUR_MOVE], CurrentTime);
869 grabbed=1;
870 } else {
871 break;
874 x = ev.xmotion.x_root - dx;
875 y = ev.xmotion.y_root - dy;
876 XMoveWindow(dpy, icon->core->window, x, y);
877 break;
879 case ButtonPress:
880 break;
882 case ButtonRelease:
883 if (ev.xbutton.button != clickButton)
884 break;
886 if (wwin->icon_x!=x || wwin->icon_y!=y)
887 wwin->flags.icon_moved = 1;
889 XMoveWindow(dpy, icon->core->window, x, y);
891 wwin->icon_x = x;
892 wwin->icon_y = y;
893 #ifdef DEBUG
894 puts("End miniwindow move");
895 #endif
896 XUngrabPointer(dpy, CurrentTime);
898 if (wPreferences.auto_arrange_icons)
899 wArrangeIcons(wwin->screen_ptr, True);
900 return;