Bug fixes for 0.20.3 pre-release 2
[wmaker-crm.git] / src / icon.c
blob264ed00b96b2a84e147a01854774fd23f7b69cb0
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 = WMNormalWindowLevel;
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 = WMNormalWindowLevel;
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 {
410 suffix = wmalloc(strlen(wwin->wm_instance)+1);
411 strcpy(suffix, wwin->wm_instance);
414 prefix = getenv("GNUSTEP_USER_PATH");
415 if (!prefix) {
416 len = strlen(wgethomedir())+64+strlen(suffix);
417 path = wmalloc(len+1);
418 sprintf(path, "%s/GNUstep/.AppInfo", wgethomedir());
419 } else {
420 prefix = wexpandpath(prefix);
421 len = strlen(prefix)+64+strlen(suffix);
422 path = wmalloc(len+1);
423 sprintf(path, "%s/.AppInfo", prefix);
424 free(prefix);
426 if (access(path, F_OK)!=0) {
427 if (mkdir(path, S_IRUSR|S_IWUSR|S_IXUSR)) {
428 wsyserror(_("could not create directory %s"), path);
429 free(path);
430 free(suffix);
431 return NULL;
434 strcat(path, "/WindowMaker");
435 if (access(path, F_OK)!=0) {
436 if (mkdir(path, S_IRUSR|S_IWUSR|S_IXUSR)!=0) {
437 wsyserror(_("could not create directory %s"), path);
438 free(path);
439 free(suffix);
440 return NULL;
444 strcat(path, "/");
445 strcat(path, suffix);
446 strcat(path, ".xpm");
447 free(suffix);
449 return path;
454 * wIconStore--
455 * Stores the client supplied icon at ~/GNUstep/.AppInfo/WindowMaker
456 * and returns the path for that icon. Returns NULL if there is no
457 * client supplied icon or on failure.
459 * Side effects:
460 * New directories might be created.
462 char*
463 wIconStore(WIcon *icon)
465 char *path;
466 RImage *image;
467 WWindow *wwin = icon->owner;
469 if (!wwin || !wwin->wm_hints || !(wwin->wm_hints->flags & IconPixmapHint)
470 || wwin->wm_hints->icon_pixmap == None)
471 return NULL;
473 path = getnameforicon(wwin);
475 image = RCreateImageFromDrawable(icon->core->screen_ptr->rcontext,
476 wwin->wm_hints->icon_pixmap,
477 (wwin->wm_hints->flags & IconMaskHint)
478 ? wwin->wm_hints->icon_mask : None);
479 if (!image) {
480 free(path);
481 return NULL;
484 if (!RSaveImage(image, path, "XPM")) {
485 free(path);
486 path = NULL;
488 RDestroyImage(image);
490 return path;
495 void
496 wIconChangeIconWindow(WIcon *icon, Window new_window);
499 static void
500 cycleColor(void *data)
502 WIcon *icon = (WIcon*)data;
503 WScreen *scr = icon->core->screen_ptr;
504 XGCValues gcv;
506 icon->step--;
507 gcv.dash_offset = icon->step;
508 XChangeGC(dpy, scr->icon_select_gc, GCDashOffset, &gcv);
510 XDrawRectangle(dpy, icon->core->window, scr->icon_select_gc, 0, 0,
511 icon->core->width-1, icon->core->height-1);
512 icon->handlerID = WMAddTimerHandler(COLOR_CYCLE_DELAY, cycleColor, icon);
516 void
517 wIconSetHighlited(WIcon *icon, Bool flag)
519 if (icon->highlighted == flag) {
520 return;
523 icon->highlighted = flag;
524 wIconPaint(icon);
529 void
530 wIconSelect(WIcon *icon)
532 WScreen *scr = icon->core->screen_ptr;
533 icon->selected = !icon->selected;
535 if (icon->selected) {
536 icon->step = 0;
537 if (!wPreferences.dont_blink)
538 icon->handlerID = WMAddTimerHandler(10, cycleColor, icon);
539 else
540 XDrawRectangle(dpy, icon->core->window, scr->icon_select_gc, 0, 0,
541 icon->core->width-1, icon->core->height-1);
542 } else {
543 if (icon->handlerID) {
544 WMDeleteTimerHandler(icon->handlerID);
545 icon->handlerID = NULL;
547 XClearArea(dpy, icon->core->window, 0, 0, icon->core->width,
548 icon->core->height, True);
553 void
554 wIconUpdate(WIcon *icon)
556 WScreen *scr = icon->core->screen_ptr;
557 int title_height = scr->icon_title_font->height;
558 WWindow *wwin = icon->owner;
560 assert(scr->icon_tile!=NULL);
562 if (icon->pixmap!=None)
563 XFreePixmap(dpy, icon->pixmap);
564 icon->pixmap = None;
567 if (wwin && wwin->window_flags.always_user_icon)
568 goto user_icon;
570 /* use client specified icon window */
571 if (icon->icon_win!=None) {
572 XWindowAttributes attr;
573 int resize=0;
574 int width, height, depth;
575 int theight;
576 Pixmap pixmap;
578 getSize(icon->icon_win, &width, &height, &depth);
580 if (width > wPreferences.icon_size) {
581 resize = 1;
582 width = wPreferences.icon_size;
584 if (height > wPreferences.icon_size) {
585 resize = 1;
586 height = wPreferences.icon_size;
588 if (icon->show_title
589 && (height+title_height < wPreferences.icon_size)) {
590 pixmap = XCreatePixmap(dpy, scr->w_win, wPreferences.icon_size,
591 wPreferences.icon_size, scr->w_depth);
592 XSetClipMask(dpy, scr->copy_gc, None);
593 XCopyArea(dpy, scr->icon_tile_pixmap, pixmap, scr->copy_gc, 0, 0,
594 wPreferences.icon_size, wPreferences.icon_size, 0, 0);
595 drawIconTitle(scr, pixmap, title_height);
596 theight = title_height;
597 } else {
598 pixmap = None;
599 theight = 0;
600 XSetWindowBackgroundPixmap(dpy, icon->core->window,
601 scr->icon_tile_pixmap);
604 XSetWindowBorderWidth(dpy, icon->icon_win, 0);
605 XReparentWindow(dpy, icon->icon_win, icon->core->window,
606 (wPreferences.icon_size-width)/2,
607 theight+(wPreferences.icon_size-height-theight)/2);
608 if (resize)
609 XResizeWindow(dpy, icon->icon_win, width, height);
611 XMapWindow(dpy, icon->icon_win);
613 XAddToSaveSet(dpy, icon->icon_win);
615 icon->pixmap = pixmap;
617 if (XGetWindowAttributes(dpy, icon->icon_win, &attr)) {
618 if (attr.all_event_masks & ButtonPressMask) {
619 wHackedGrabButton(Button1, MOD_MASK, icon->core->window, True,
620 ButtonPressMask, GrabModeSync, GrabModeAsync,
621 None, wCursor[WCUR_ARROW]);
624 } else if (wwin && wwin->wm_hints
625 && (wwin->wm_hints->flags & IconPixmapHint)) {
626 int x, y;
627 unsigned int w, h;
628 Window jw;
629 int ji, dotitle;
630 unsigned int ju, d;
631 Pixmap pixmap;
633 if (!XGetGeometry(dpy, wwin->wm_hints->icon_pixmap, &jw,
634 &ji, &ji, &w, &h, &ju, &d)) {
635 icon->owner->wm_hints->flags &= ~IconPixmapHint;
636 goto user_icon;
639 pixmap = XCreatePixmap(dpy, icon->core->window, wPreferences.icon_size,
640 wPreferences.icon_size, scr->w_depth);
641 XSetClipMask(dpy, scr->copy_gc, None);
642 XCopyArea(dpy, scr->icon_tile_pixmap, pixmap, scr->copy_gc, 0, 0,
643 wPreferences.icon_size, wPreferences.icon_size, 0, 0);
645 if (w > wPreferences.icon_size)
646 w = wPreferences.icon_size;
647 x = (wPreferences.icon_size-w)/2;
649 if (icon->show_title && (title_height < wPreferences.icon_size)) {
650 drawIconTitle(scr, pixmap, title_height);
651 dotitle = 1;
653 if (h > wPreferences.icon_size - title_height - 2) {
654 h = wPreferences.icon_size - title_height - 2;
655 y = title_height + 1;
656 } else {
657 y = (wPreferences.icon_size-h-title_height)/2+title_height + 1;
659 } else {
660 dotitle = 0;
661 if (w > wPreferences.icon_size)
662 w = wPreferences.icon_size;
663 y = (wPreferences.icon_size-h)/2;
666 if (wwin->wm_hints->flags & IconMaskHint)
667 XSetClipMask(dpy, scr->copy_gc, wwin->wm_hints->icon_mask);
669 XSetClipOrigin(dpy, scr->copy_gc, x, y);
671 if (d != scr->w_depth) {
672 XSetForeground(dpy, scr->copy_gc, scr->black_pixel);
673 XSetBackground(dpy, scr->copy_gc, scr->white_pixel);
674 XCopyPlane(dpy, wwin->wm_hints->icon_pixmap, pixmap, scr->copy_gc,
675 0, 0, w, h, x, y, 1);
676 } else {
677 XCopyArea(dpy, wwin->wm_hints->icon_pixmap, pixmap, scr->copy_gc,
678 0, 0, w, h, x, y);
681 XSetClipOrigin(dpy, scr->copy_gc, 0, 0);
683 icon->pixmap = pixmap;
684 } else {
685 user_icon:
687 if (icon->image) {
688 icon->pixmap = makeIcon(scr, icon->image, icon->show_title,
689 icon->shadowed, icon->tile_type);
690 } else {
691 /* make default icons */
693 if (!scr->def_icon_pixmap) {
694 RImage *image = NULL;
695 char *path;
696 char *file;
698 file = wDefaultGetIconFile(scr, NULL, NULL, False);
699 if (file) {
700 path = FindImage(wPreferences.icon_path, file);
701 if (!path) {
702 wwarning(_("could not find default icon \"%s\""),file);
703 goto make_icons;
706 image = RLoadImage(scr->rcontext, path, 0);
707 if (!image) {
708 wwarning(_("could not load default icon \"%s\":%s"),
709 file, RMessageForError(RErrorCode));
711 free(path);
713 make_icons:
715 image = wIconValidateIconSize(scr, image);
716 scr->def_icon_pixmap = makeIcon(scr, image, False, False,
717 icon->tile_type);
718 scr->def_ticon_pixmap = makeIcon(scr, image, True, False,
719 icon->tile_type);
720 if (image)
721 RDestroyImage(image);
724 if (icon->show_title) {
725 XSetWindowBackgroundPixmap(dpy, icon->core->window,
726 scr->def_ticon_pixmap);
727 } else {
728 XSetWindowBackgroundPixmap(dpy, icon->core->window,
729 scr->def_icon_pixmap);
731 icon->pixmap = None;
734 if (icon->pixmap != None) {
735 XSetWindowBackgroundPixmap(dpy, icon->core->window, icon->pixmap);
737 XClearWindow(dpy, icon->core->window);
739 wIconPaint(icon);
744 void
745 wIconPaint(WIcon *icon)
747 WScreen *scr=icon->core->screen_ptr;
748 GC gc = scr->icon_title_gc;
749 int x;
750 char *tmp;
752 if (icon->force_paint) {
753 icon->force_paint = 0;
754 wIconUpdate(icon);
755 return;
758 XClearWindow(dpy, icon->core->window);
760 /* draw the icon title */
761 if (icon->show_title && icon->icon_name!=NULL) {
762 int l;
763 int w;
765 tmp = ShrinkString(scr->icon_title_font, icon->icon_name,
766 wPreferences.icon_size-4);
767 w = wTextWidth(scr->icon_title_font->font, tmp, l=strlen(tmp));
769 if (w > icon->core->width - 4)
770 x = (icon->core->width - 4) - w;
771 else
772 x = (icon->core->width - w)/2;
774 wDrawString(icon->core->window, scr->icon_title_font, gc,
775 x, 1 + scr->icon_title_font->y, tmp, l);
776 free(tmp);
779 if (icon->selected)
780 XDrawRectangle(dpy, icon->core->window, scr->icon_select_gc, 0, 0,
781 icon->core->width-1, icon->core->height-1);
785 /******************************************************************/
787 static void
788 miniwindowExpose(WObjDescriptor *desc, XEvent *event)
790 wIconPaint(desc->parent);
794 static void
795 miniwindowDblClick(WObjDescriptor *desc, XEvent *event)
797 WIcon *icon = desc->parent;
799 assert(icon->owner!=NULL);
801 wDeiconifyWindow(icon->owner);
805 static void
806 miniwindowMouseDown(WObjDescriptor *desc, XEvent *event)
808 WIcon *icon = desc->parent;
809 WWindow *wwin = icon->owner;
810 XEvent ev;
811 int x=wwin->icon_x, y=wwin->icon_y;
812 int dx=event->xbutton.x, dy=event->xbutton.y;
813 int grabbed=0;
814 int clickButton=event->xbutton.button;
817 if (IsDoubleClick(icon->core->screen_ptr, event)) {
818 miniwindowDblClick(desc, event);
819 return;
822 #ifdef DEBUG
823 puts("Moving miniwindow");
824 #endif
825 if (event->xbutton.button==Button1) {
826 if (event->xbutton.state & MOD_MASK)
827 wLowerFrame(icon->core);
828 else
829 wRaiseFrame(icon->core);
830 if (event->xbutton.state & ShiftMask) {
831 wIconSelect(icon);
832 wSelectWindow(icon->owner, !wwin->flags.selected);
836 if (XGrabPointer(dpy, icon->core->window, False, ButtonMotionMask
837 |ButtonReleaseMask|ButtonPressMask, GrabModeAsync,
838 GrabModeAsync, None, None, CurrentTime) !=GrabSuccess) {
839 #ifdef DEBUG0
840 wwarning("pointer grab failed for icon move");
841 #endif
843 while(1) {
844 WMMaskEvent(dpy, PointerMotionMask|ButtonReleaseMask|ButtonPressMask
845 |ButtonMotionMask|ExposureMask, &ev);
846 switch (ev.type) {
847 case Expose:
848 WMHandleEvent(&ev);
849 break;
851 case MotionNotify:
852 if (!grabbed) {
853 if (abs(dx-ev.xmotion.x)>=MOVE_THRESHOLD
854 || abs(dy-ev.xmotion.y)>=MOVE_THRESHOLD) {
855 XChangeActivePointerGrab(dpy, ButtonMotionMask
856 |ButtonReleaseMask|ButtonPressMask,
857 wCursor[WCUR_MOVE], CurrentTime);
858 grabbed=1;
859 } else {
860 break;
863 x = ev.xmotion.x_root - dx;
864 y = ev.xmotion.y_root - dy;
865 XMoveWindow(dpy, icon->core->window, x, y);
866 break;
868 case ButtonPress:
869 break;
871 case ButtonRelease:
872 if (ev.xbutton.button != clickButton)
873 break;
875 if (wwin->icon_x!=x || wwin->icon_y!=y)
876 wwin->flags.icon_moved = 1;
878 XMoveWindow(dpy, icon->core->window, x, y);
880 wwin->icon_x = x;
881 wwin->icon_y = y;
882 #ifdef DEBUG
883 puts("End miniwindow move");
884 #endif
885 XUngrabPointer(dpy, CurrentTime);
886 return;