Remove dup set icon file to NULL
[wmaker-crm.git] / src / icon.c
blob5a181480ac01c7cabe2cb0fc00f4fd961399fe47
1 /* icon.c - window icon and dock and appicon parent
3 * Window Maker window manager
5 * Copyright (c) 1997-2003 Alfredo K. Kojima
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 along
18 * with this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22 #include "wconfig.h"
24 #include <X11/Xlib.h>
25 #include <X11/Xutil.h>
26 #include <stdlib.h>
27 #include <stdio.h>
28 #include <stdint.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"
46 #include "wmspec.h"
48 /**** Global varianebles ****/
49 extern WPreferences wPreferences;
51 #define MOD_MASK wPreferences.modifier_mask
52 #define CACHE_ICON_PATH "/Library/WindowMaker/CachedPixmaps"
53 #define ICON_BORDER 3
55 extern Cursor wCursor[WCUR_LAST];
57 static void miniwindowExpose(WObjDescriptor * desc, XEvent * event);
58 static void miniwindowMouseDown(WObjDescriptor * desc, XEvent * event);
59 static void miniwindowDblClick(WObjDescriptor * desc, XEvent * event);
61 static WIcon *icon_create_core(WScreen *scr, int coord_x, int coord_y);
63 static void set_dockapp_in_icon(WIcon *icon);
64 static void get_rimage_icon_from_icon_win(WIcon *icon);
65 static int get_rimage_icon_from_wm_hints(WIcon *icon);
66 static void get_rimage_icon_from_user_icon(WIcon *icon);
67 static void get_rimage_icon_from_default_icon(WIcon *icon);
68 static void get_rimage_icon_from_x11(WIcon *icon);
70 static void icon_update_pixmap(WIcon *icon, RImage *image);
71 static void unset_icon_image(WIcon *icon);
73 /****** Notification Observers ******/
75 static void appearanceObserver(void *self, WMNotification *notif)
77 WIcon *icon = (WIcon *) self;
78 uintptr_t flags = (uintptr_t)WMGetNotificationClientData(notif);
80 if ((flags & WTextureSettings) || (flags & WFontSettings)) {
81 /* If the rimage exists, update the icon, else create it */
82 if (icon->file_image)
83 update_icon_pixmap(icon);
84 else
85 wIconPaint(icon);
88 /* so that the appicon expose handlers will paint the appicon specific
89 * stuff */
90 XClearArea(dpy, icon->core->window, 0, 0, icon->core->width, icon->core->height, True);
93 static void tileObserver(void *self, WMNotification *notif)
95 WIcon *icon = (WIcon *) self;
97 wIconUpdate(icon);
99 XClearArea(dpy, icon->core->window, 0, 0, 1, 1, True);
102 /************************************/
104 static int getSize(Drawable d, unsigned int *w, unsigned int *h, unsigned int *dep)
106 Window rjunk;
107 int xjunk, yjunk;
108 unsigned int bjunk;
110 return XGetGeometry(dpy, d, &rjunk, &xjunk, &yjunk, w, h, &bjunk, dep);
113 WIcon *icon_create_for_wwindow(WWindow *wwin)
115 WScreen *scr = wwin->screen_ptr;
116 WIcon *icon;
117 char *file;
119 icon = icon_create_core(scr, wwin->icon_x, wwin->icon_y);
121 icon->owner = wwin;
122 if (wwin->wm_hints && (wwin->wm_hints->flags & IconWindowHint)) {
123 if (wwin->client_win == wwin->main_window) {
124 WApplication *wapp;
125 /* do not let miniwindow steal app-icon's icon window */
126 wapp = wApplicationOf(wwin->client_win);
127 if (!wapp || wapp->app_icon == NULL)
128 icon->icon_win = wwin->wm_hints->icon_window;
129 } else {
130 icon->icon_win = wwin->wm_hints->icon_window;
133 #ifdef NO_MINIWINDOW_TITLES
134 icon->show_title = 0;
135 #else
136 icon->show_title = 1;
137 #endif
139 icon->icon_name = wNETWMGetIconName(wwin->client_win);
140 if (icon->icon_name)
141 wwin->flags.net_has_icon_title = 1;
142 else
143 wGetIconName(dpy, wwin->client_win, &icon->icon_name);
145 /* Get the application icon, default included */
146 file = get_icon_filename(scr, wwin->wm_instance, wwin->wm_class, NULL, True);
147 if (file) {
148 icon->file = wstrdup(file);
149 icon->file_image = get_rimage_from_file(scr, icon->file, wPreferences.icon_size);
150 wfree(file);
153 icon->tile_type = TILE_NORMAL;
155 wIconUpdate(icon);
157 WMAddNotificationObserver(appearanceObserver, icon, WNIconAppearanceSettingsChanged, icon);
158 WMAddNotificationObserver(tileObserver, icon, WNIconTileSettingsChanged, icon);
160 return icon;
163 WIcon *icon_create_for_dock(WScreen *scr, char *command, char *wm_instance, char *wm_class, int tile)
165 WIcon *icon;
166 char *file = NULL;
168 icon = icon_create_core(scr, 0, 0);
170 /* Search the icon using instance and class, without default icon */
171 file = get_icon_filename(scr, wm_instance, wm_class, command, False);
172 if (file) {
173 icon->file = wstrdup(file);
174 icon->file_image = get_rimage_from_file(scr, icon->file, wPreferences.icon_size);
175 wfree(file);
178 icon->tile_type = tile;
180 wIconUpdate(icon);
182 WMAddNotificationObserver(appearanceObserver, icon, WNIconAppearanceSettingsChanged, icon);
183 WMAddNotificationObserver(tileObserver, icon, WNIconTileSettingsChanged, icon);
185 return icon;
188 static WIcon *icon_create_core(WScreen *scr, int coord_x, int coord_y)
190 WIcon *icon;
191 unsigned long vmask = 0;
192 XSetWindowAttributes attribs;
194 icon = wmalloc(sizeof(WIcon));
195 icon->core = wCoreCreateTopLevel(scr,
196 coord_x,
197 coord_y,
198 wPreferences.icon_size,
199 wPreferences.icon_size,
200 0, scr->w_depth, scr->w_visual, scr->w_colormap);
202 if (wPreferences.use_saveunders) {
203 vmask = CWSaveUnder;
204 attribs.save_under = True;
207 /* a white border for selecting it */
208 vmask |= CWBorderPixel;
209 attribs.border_pixel = scr->white_pixel;
211 XChangeWindowAttributes(dpy, icon->core->window, vmask, &attribs);
213 /* will be overriden if this is a application icon */
214 icon->core->descriptor.handle_mousedown = miniwindowMouseDown;
215 icon->core->descriptor.handle_expose = miniwindowExpose;
216 icon->core->descriptor.parent_type = WCLASS_MINIWINDOW;
217 icon->core->descriptor.parent = icon;
219 icon->core->stacking = wmalloc(sizeof(WStacking));
220 icon->core->stacking->above = NULL;
221 icon->core->stacking->under = NULL;
222 icon->core->stacking->window_level = NORMAL_ICON_LEVEL;
223 icon->core->stacking->child_of = NULL;
225 /* Icon image */
226 icon->file = NULL;
227 icon->file_image = NULL;
229 return icon;
232 void wIconDestroy(WIcon * icon)
234 WCoreWindow *core = icon->core;
235 WScreen *scr = core->screen_ptr;
237 WMRemoveNotificationObserver(icon);
239 if (icon->handlerID)
240 WMDeleteTimerHandler(icon->handlerID);
242 if (icon->icon_win) {
243 int x = 0, y = 0;
245 if (icon->owner) {
246 x = icon->owner->icon_x;
247 y = icon->owner->icon_y;
249 XUnmapWindow(dpy, icon->icon_win);
250 XReparentWindow(dpy, icon->icon_win, scr->root_win, x, y);
252 if (icon->icon_name)
253 XFree(icon->icon_name);
255 if (icon->pixmap)
256 XFreePixmap(dpy, icon->pixmap);
258 unset_icon_image(icon);
260 wCoreDestroy(icon->core);
261 wfree(icon);
264 static void drawIconTitle(WScreen * scr, Pixmap pixmap, int height)
266 XFillRectangle(dpy, pixmap, scr->icon_title_texture->normal_gc, 0, 0, wPreferences.icon_size, height + 1);
267 XDrawLine(dpy, pixmap, scr->icon_title_texture->light_gc, 0, 0, wPreferences.icon_size, 0);
268 XDrawLine(dpy, pixmap, scr->icon_title_texture->light_gc, 0, 0, 0, height + 1);
269 XDrawLine(dpy, pixmap, scr->icon_title_texture->dim_gc,
270 wPreferences.icon_size - 1, 0, wPreferences.icon_size - 1, height + 1);
273 static void icon_update_pixmap(WIcon *icon, RImage *image)
275 RImage *tile;
276 Pixmap pixmap;
277 int x, y, sx, sy;
278 unsigned w, h;
279 int theight = 0;
280 WScreen *scr = icon->core->screen_ptr;
281 int titled = icon->show_title;
283 if (icon->tile_type == TILE_NORMAL) {
284 tile = RCloneImage(scr->icon_tile);
285 } else {
286 assert(scr->clip_tile);
287 tile = RCloneImage(scr->clip_tile);
290 if (image) {
291 w = (image->width > wPreferences.icon_size)
292 ? wPreferences.icon_size : image->width;
293 x = (wPreferences.icon_size - w) / 2;
294 sx = (image->width - w) / 2;
296 if (titled)
297 theight = WMFontHeight(scr->icon_title_font);
299 h = (image->height + theight > wPreferences.icon_size
300 ? wPreferences.icon_size - theight : image->height);
301 y = theight + (wPreferences.icon_size - theight - h) / 2;
302 sy = (image->height - h) / 2;
304 RCombineArea(tile, image, sx, sy, w, h, x, y);
307 if (icon->shadowed) {
308 RColor color;
310 color.red = scr->icon_back_texture->light.red >> 8;
311 color.green = scr->icon_back_texture->light.green >> 8;
312 color.blue = scr->icon_back_texture->light.blue >> 8;
313 color.alpha = 150; /* about 60% */
314 RClearImage(tile, &color);
317 if (icon->highlighted) {
318 RColor color;
320 color.red = color.green = color.blue = 0;
321 color.alpha = 160;
322 RLightImage(tile, &color);
325 if (!RConvertImage(scr->rcontext, tile, &pixmap))
326 wwarning(_("error rendering image:%s"), RMessageForError(RErrorCode));
328 RReleaseImage(tile);
330 if (titled)
331 drawIconTitle(scr, pixmap, theight);
333 icon->pixmap = pixmap;
336 void wIconChangeTitle(WIcon *icon, char *new_title)
338 int changed;
340 changed = (new_title == NULL && icon->icon_name != NULL) ||
341 (new_title != NULL && icon->icon_name == NULL);
343 if (icon->icon_name != NULL)
344 XFree(icon->icon_name);
346 icon->icon_name = new_title;
348 if (changed)
349 wIconUpdate(icon);
350 else
351 wIconPaint(icon);
354 RImage *wIconValidateIconSize(RImage *icon, int max_size)
356 RImage *nimage;
358 if (!icon)
359 return NULL;
361 /* We should hold "ICON_BORDER" (~2) pixels to include the icon border */
362 if ((icon->width - max_size) > -ICON_BORDER ||
363 (icon->height - max_size) > -ICON_BORDER) {
364 nimage = RScaleImage(icon, max_size - ICON_BORDER,
365 (icon->height * (max_size - ICON_BORDER) / icon->width));
366 RReleaseImage(icon);
367 icon = nimage;
370 return icon;
373 Bool wIconChangeImageFile(WIcon *icon, char *file)
375 WScreen *scr = icon->core->screen_ptr;
376 char *path;
377 RImage *image = NULL;
378 int error = 0;
380 /* If no new image, don't do nothing */
381 if (!file)
382 return True;
384 /* Find the new image */
385 path = FindImage(wPreferences.icon_path, file);
386 if (path)
387 image = get_rimage_from_file(scr, path, wPreferences.icon_size);
388 else
389 error = 1;
391 /* New image! */
392 if (!error && image) {
393 /* Remove the old one */
394 unset_icon_image(icon);
396 /* Set the new image */
397 icon->file_image = image;
398 icon->file = wstrdup(path);
399 wIconUpdate(icon);
400 } else {
401 error = 1;
404 if (path)
405 wfree(path);
407 return !error;
410 static char *get_name_for_wwin(WWindow *wwin)
412 return get_name_for_instance_class(wwin->wm_instance, wwin->wm_class);
415 char *get_name_for_instance_class(char *wm_instance, char *wm_class)
417 char *suffix;
418 int len;
420 if (wm_class && wm_instance) {
421 len = strlen(wm_class) + strlen(wm_instance) + 2;
422 suffix = wmalloc(len);
423 snprintf(suffix, len, "%s.%s", wm_instance, wm_class);
424 } else if (wm_class) {
425 len = strlen(wm_class) + 1;
426 suffix = wmalloc(len);
427 snprintf(suffix, len, "%s", wm_class);
428 } else if (wm_instance) {
429 len = strlen(wm_instance) + 1;
430 suffix = wmalloc(len);
431 snprintf(suffix, len, "%s", wm_instance);
432 } else {
433 return NULL;
436 return suffix;
439 static char *get_icon_cache_path(void)
441 char *prefix, *path;
442 int len, ret;
444 prefix = wusergnusteppath();
445 len = strlen(prefix) + strlen(CACHE_ICON_PATH) + 2;
446 path = wmalloc(len);
447 snprintf(path, len, "%s%s/", prefix, CACHE_ICON_PATH);
449 /* If the folder exists, exit */
450 if (access(path, F_OK) == 0)
451 return path;
453 /* Create the folder */
454 ret = wmkdirhier((const char *) path);
456 /* Exit 1 on success, 0 on failure */
457 if (ret == 1)
458 return path;
460 /* Fail */
461 wfree(path);
462 return NULL;
465 static RImage *get_wwindow_image_from_wmhints(WWindow *wwin, WIcon *icon)
467 RImage *image = NULL;
468 XWMHints *hints = wwin->wm_hints;
470 if (hints && (hints->flags & IconPixmapHint) && hints->icon_pixmap != None)
471 image = RCreateImageFromDrawable(icon->core->screen_ptr->rcontext,
472 hints->icon_pixmap,
473 (hints->flags & IconMaskHint)
474 ? hints->icon_mask : None);
476 return image;
480 * wIconStore--
481 * Stores the client supplied icon at CACHE_ICON_PATH
482 * and returns the path for that icon. Returns NULL if there is no
483 * client supplied icon or on failure.
485 * Side effects:
486 * New directories might be created.
488 char *wIconStore(WIcon * icon)
490 char *path, *dir_path, *file;
491 int len = 0;
492 RImage *image = NULL;
493 WWindow *wwin = icon->owner;
495 if (!wwin)
496 return NULL;
498 dir_path = get_icon_cache_path();
499 if (!dir_path)
500 return NULL;
502 file = get_name_for_wwin(wwin);
503 if (!file) {
504 wfree(dir_path);
505 return NULL;
508 len = strlen(dir_path) + strlen(file) + 5;
509 path = wmalloc(len);
510 snprintf(path, len, "%s%s.xpm", dir_path, file);
511 wfree(dir_path);
512 wfree(file);
514 /* If icon exists, exit */
515 if (access(path, F_OK) == 0)
516 return path;
518 if (wwin->net_icon_image)
519 image = RRetainImage(wwin->net_icon_image);
520 else
521 image = get_wwindow_image_from_wmhints(wwin, icon);
523 if (!image) {
524 wfree(path);
525 return NULL;
528 if (!RSaveImage(image, path, "XPM")) {
529 wfree(path);
530 path = NULL;
533 RReleaseImage(image);
535 return path;
538 static void cycleColor(void *data)
540 WIcon *icon = (WIcon *) data;
541 WScreen *scr = icon->core->screen_ptr;
542 XGCValues gcv;
544 icon->step--;
545 gcv.dash_offset = icon->step;
546 XChangeGC(dpy, scr->icon_select_gc, GCDashOffset, &gcv);
548 XDrawRectangle(dpy, icon->core->window, scr->icon_select_gc, 0, 0,
549 icon->core->width - 1, icon->core->height - 1);
550 icon->handlerID = WMAddTimerHandler(COLOR_CYCLE_DELAY, cycleColor, icon);
553 #ifdef NEWAPPICON
554 void wIconSetHighlited(WIcon *icon, Bool flag)
556 if (icon->highlighted == flag)
557 return;
559 icon->highlighted = flag;
560 update_icon_pixmap(icon);
562 #endif
564 void wIconSelect(WIcon * icon)
566 WScreen *scr = icon->core->screen_ptr;
567 icon->selected = !icon->selected;
569 if (icon->selected) {
570 icon->step = 0;
571 if (!wPreferences.dont_blink)
572 icon->handlerID = WMAddTimerHandler(10, cycleColor, icon);
573 else
574 XDrawRectangle(dpy, icon->core->window, scr->icon_select_gc, 0, 0,
575 icon->core->width - 1, icon->core->height - 1);
576 } else {
577 if (icon->handlerID) {
578 WMDeleteTimerHandler(icon->handlerID);
579 icon->handlerID = NULL;
581 XClearArea(dpy, icon->core->window, 0, 0, icon->core->width, icon->core->height, True);
585 static void unset_icon_image(WIcon *icon)
587 if (icon->file) {
588 wfree(icon->file);
589 icon->file = NULL;
592 if (icon->file_image) {
593 RReleaseImage(icon->file_image);
594 icon->file_image = NULL;
598 void wIconUpdate(WIcon *icon)
600 WScreen *scr = icon->core->screen_ptr;
601 WWindow *wwin = icon->owner;
603 assert(scr->icon_tile != NULL);
605 if (wwin && WFLAGP(wwin, always_user_icon)) {
606 /* Forced use user_icon */
607 get_rimage_icon_from_user_icon(icon);
608 } else if (icon->icon_win != None) {
609 /* Get the Pixmap from the WIcon */
610 get_rimage_icon_from_icon_win(icon);
611 } else if (wwin && wwin->net_icon_image) {
612 /* Use _NET_WM_ICON icon */
613 get_rimage_icon_from_x11(icon);
614 } else if (wwin && wwin->wm_hints && (wwin->wm_hints->flags & IconPixmapHint)) {
615 /* Get the Pixmap from the wm_hints, else, from the user */
616 if (get_rimage_icon_from_wm_hints(icon))
617 get_rimage_icon_from_user_icon(icon);
618 } else {
619 /* Get the Pixmap from the user */
620 get_rimage_icon_from_user_icon(icon);
623 update_icon_pixmap(icon);
626 void update_icon_pixmap(WIcon *icon)
628 if (icon->pixmap != None)
629 XFreePixmap(dpy, icon->pixmap);
631 icon->pixmap = None;
633 /* Create the pixmap */
634 if (icon->file_image)
635 icon_update_pixmap(icon, icon->file_image);
637 /* If dockapp, put inside the icon */
638 if (icon->icon_win != None) {
639 /* file_image is NULL, because is docked app */
640 icon_update_pixmap(icon, icon->file_image);
641 set_dockapp_in_icon(icon);
644 /* No pixmap, set default background */
645 if (icon->pixmap != None)
646 XSetWindowBackgroundPixmap(dpy, icon->core->window, icon->pixmap);
648 /* Paint it */
649 XClearWindow(dpy, icon->core->window);
650 wIconPaint(icon);
653 static void get_rimage_icon_from_x11(WIcon *icon)
655 /* Remove the icon image */
656 unset_icon_image(icon);
658 /* Set the new icon image */
659 icon->file_image = RRetainImage(icon->owner->net_icon_image);
662 static void get_rimage_icon_from_user_icon(WIcon *icon)
664 if (icon->file_image)
665 return;
667 get_rimage_icon_from_default_icon(icon);
670 static void get_rimage_icon_from_default_icon(WIcon *icon)
672 WScreen *scr = icon->core->screen_ptr;
674 /* If the icon don't have image, we should use the default image. */
675 if (!scr->def_icon_rimage)
676 scr->def_icon_rimage = get_default_image(scr);
678 /* Remove the icon image */
679 unset_icon_image(icon);
681 /* Set the new icon image */
682 icon->file_image = RRetainImage(scr->def_icon_rimage);
685 /* Get the RImage from the WIcon of the WWindow */
686 static void get_rimage_icon_from_icon_win(WIcon *icon)
688 RImage *image;
690 /* Create the new RImage */
691 image = get_window_image_from_x11(icon->icon_win);
693 /* Free the icon info */
694 unset_icon_image(icon);
696 /* Set the new info */
697 icon->file_image = image;
700 /* Set the dockapp in the WIcon */
701 static void set_dockapp_in_icon(WIcon *icon)
703 XWindowAttributes attr;
704 WScreen *scr = icon->core->screen_ptr;
705 int title_height = WMFontHeight(scr->icon_title_font);
706 unsigned int w, h, d;
707 int theight = 0;
709 /* Reparent the dock application to the icon */
711 /* We need the application size to center it
712 * and show in the correct position */
713 getSize(icon->icon_win, &w, &h, &d);
715 /* Set extra space for title */
716 if (icon->show_title && (h + title_height < wPreferences.icon_size)) {
717 theight = title_height;
718 drawIconTitle(scr, icon->pixmap, theight);
719 } else {
720 XSetWindowBackgroundPixmap(dpy, icon->core->window, scr->icon_tile_pixmap);
723 /* Set the icon border */
724 XSetWindowBorderWidth(dpy, icon->icon_win, 0);
726 /* Put the dock application in the icon */
727 XReparentWindow(dpy, icon->icon_win, icon->core->window,
728 (wPreferences.icon_size - w) / 2,
729 theight + (wPreferences.icon_size - h - theight) / 2);
731 /* Show it and save */
732 XMapWindow(dpy, icon->icon_win);
733 XAddToSaveSet(dpy, icon->icon_win);
735 /* Needed to move the icon clicking on the application part */
736 if ((XGetWindowAttributes(dpy, icon->icon_win, &attr)) &&
737 (attr.all_event_masks & ButtonPressMask))
738 wHackedGrabButton(Button1, MOD_MASK, icon->core->window, True,
739 ButtonPressMask, GrabModeSync, GrabModeAsync,
740 None, wCursor[WCUR_ARROW]);
743 /* Get the RImage from the XWindow wm_hints */
744 static int get_rimage_icon_from_wm_hints(WIcon *icon)
746 RImage *image = NULL;
747 unsigned int w, h, d;
748 WWindow *wwin = icon->owner;
750 if (!getSize(wwin->wm_hints->icon_pixmap, &w, &h, &d)) {
751 icon->owner->wm_hints->flags &= ~IconPixmapHint;
752 return 1;
755 image = get_wwindow_image_from_wmhints(wwin, icon);
756 if (!image)
757 return 1;
759 unset_icon_image(icon);
760 icon->file_image = image;
762 return 0;
765 void wIconPaint(WIcon *icon)
767 WScreen *scr = icon->core->screen_ptr;
768 int x, l, w;
769 char *tmp;
771 XClearWindow(dpy, icon->core->window);
773 /* draw the icon title */
774 if (icon->show_title && icon->icon_name != NULL) {
775 tmp = ShrinkString(scr->icon_title_font, icon->icon_name, wPreferences.icon_size - 4);
776 w = WMWidthOfString(scr->icon_title_font, tmp, l = strlen(tmp));
778 if (w > icon->core->width - 4)
779 x = (icon->core->width - 4) - w;
780 else
781 x = (icon->core->width - w) / 2;
783 WMDrawString(scr->wmscreen, icon->core->window, scr->icon_title_color,
784 scr->icon_title_font, x, 1, tmp, l);
785 wfree(tmp);
788 if (icon->selected)
789 XDrawRectangle(dpy, icon->core->window, scr->icon_select_gc, 0, 0,
790 icon->core->width - 1, icon->core->height - 1);
793 /******************************************************************/
795 static void miniwindowExpose(WObjDescriptor * desc, XEvent * event)
797 wIconPaint(desc->parent);
800 static void miniwindowDblClick(WObjDescriptor * desc, XEvent * event)
802 WIcon *icon = desc->parent;
804 assert(icon->owner != NULL);
806 wDeiconifyWindow(icon->owner);
809 static void miniwindowMouseDown(WObjDescriptor * desc, XEvent * event)
811 WIcon *icon = desc->parent;
812 WWindow *wwin = icon->owner;
813 XEvent ev;
814 int x = wwin->icon_x, y = wwin->icon_y;
815 int dx = event->xbutton.x, dy = event->xbutton.y;
816 int grabbed = 0;
817 int clickButton = event->xbutton.button;
818 Bool hasMoved = False;
820 if (WCHECK_STATE(WSTATE_MODAL))
821 return;
823 if (IsDoubleClick(icon->core->screen_ptr, event)) {
824 miniwindowDblClick(desc, event);
825 return;
828 if (event->xbutton.button == Button1) {
829 if (event->xbutton.state & MOD_MASK)
830 wLowerFrame(icon->core);
831 else
832 wRaiseFrame(icon->core);
833 if (event->xbutton.state & ShiftMask) {
834 wIconSelect(icon);
835 wSelectWindow(icon->owner, !wwin->flags.selected);
837 } else if (event->xbutton.button == Button3) {
838 WObjDescriptor *desc;
840 OpenMiniwindowMenu(wwin, event->xbutton.x_root, event->xbutton.y_root);
842 /* allow drag select of menu */
843 desc = &wwin->screen_ptr->window_menu->menu->descriptor;
844 event->xbutton.send_event = True;
845 (*desc->handle_mousedown) (desc, event);
847 return;
850 if (XGrabPointer(dpy, icon->core->window, False, ButtonMotionMask
851 | ButtonReleaseMask | ButtonPressMask, GrabModeAsync,
852 GrabModeAsync, None, None, CurrentTime) != GrabSuccess) {
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 hasMoved = True;
864 if (!grabbed) {
865 if (abs(dx - ev.xmotion.x) >= MOVE_THRESHOLD
866 || abs(dy - ev.xmotion.y) >= MOVE_THRESHOLD) {
867 XChangeActivePointerGrab(dpy, ButtonMotionMask
868 | ButtonReleaseMask | ButtonPressMask,
869 wCursor[WCUR_MOVE], CurrentTime);
870 grabbed = 1;
871 } else {
872 break;
875 x = ev.xmotion.x_root - dx;
876 y = ev.xmotion.y_root - dy;
877 XMoveWindow(dpy, icon->core->window, x, y);
878 break;
880 case ButtonPress:
881 break;
883 case ButtonRelease:
884 if (ev.xbutton.button != clickButton)
885 break;
887 if (wwin->icon_x != x || wwin->icon_y != y)
888 wwin->flags.icon_moved = 1;
890 XMoveWindow(dpy, icon->core->window, x, y);
892 wwin->icon_x = x;
893 wwin->icon_y = y;
894 XUngrabPointer(dpy, CurrentTime);
896 if (wPreferences.auto_arrange_icons)
897 wArrangeIcons(wwin->screen_ptr, True);
898 if (wPreferences.single_click && !hasMoved)
899 miniwindowDblClick(desc, event);
900 return;