wIconPaint: avoid crashing if icon is NULL
[wmaker-crm.git] / src / icon.c
blob166b8be9583807e092593f9d1e8c1f1d943b3356
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 void get_rimage_icon_from_user_icon(WIcon *icon);
66 static void get_rimage_icon_from_default_icon(WIcon *icon);
67 static void get_rimage_icon_from_x11(WIcon *icon);
69 static void icon_update_pixmap(WIcon *icon, RImage *image);
70 static void unset_icon_image(WIcon *icon);
72 /****** Notification Observers ******/
74 static void appearanceObserver(void *self, WMNotification *notif)
76 WIcon *icon = (WIcon *) self;
77 uintptr_t flags = (uintptr_t)WMGetNotificationClientData(notif);
79 if ((flags & WTextureSettings) || (flags & WFontSettings)) {
80 /* If the rimage exists, update the icon, else create it */
81 if (icon->file_image)
82 update_icon_pixmap(icon);
83 else
84 wIconPaint(icon);
87 /* so that the appicon expose handlers will paint the appicon specific
88 * stuff */
89 XClearArea(dpy, icon->core->window, 0, 0, icon->core->width, icon->core->height, True);
92 static void tileObserver(void *self, WMNotification *notif)
94 WIcon *icon = (WIcon *) self;
96 update_icon_pixmap(icon);
98 XClearArea(dpy, icon->core->window, 0, 0, 1, 1, True);
101 /************************************/
103 static int getSize(Drawable d, unsigned int *w, unsigned int *h, unsigned int *dep)
105 Window rjunk;
106 int xjunk, yjunk;
107 unsigned int bjunk;
109 return XGetGeometry(dpy, d, &rjunk, &xjunk, &yjunk, w, h, &bjunk, dep);
112 WIcon *icon_create_for_wwindow(WWindow *wwin)
114 WScreen *scr = wwin->screen_ptr;
115 WIcon *icon;
117 icon = icon_create_core(scr, wwin->icon_x, wwin->icon_y);
119 icon->owner = wwin;
120 if (wwin->wm_hints && (wwin->wm_hints->flags & IconWindowHint)) {
121 if (wwin->client_win == wwin->main_window) {
122 WApplication *wapp;
123 /* do not let miniwindow steal app-icon's icon window */
124 wapp = wApplicationOf(wwin->client_win);
125 if (!wapp || wapp->app_icon == NULL)
126 icon->icon_win = wwin->wm_hints->icon_window;
127 } else {
128 icon->icon_win = wwin->wm_hints->icon_window;
131 #ifdef NO_MINIWINDOW_TITLES
132 icon->show_title = 0;
133 #else
134 icon->show_title = 1;
135 #endif
137 icon->icon_name = wNETWMGetIconName(wwin->client_win);
138 if (icon->icon_name)
139 wwin->flags.net_has_icon_title = 1;
140 else
141 wGetIconName(dpy, wwin->client_win, &icon->icon_name);
143 icon->tile_type = TILE_NORMAL;
145 set_icon_image_from_database(icon, wwin->wm_instance, wwin->wm_class, NULL);
146 /* Update the icon, because icon could be NULL */
147 wIconUpdate(icon);
149 WMAddNotificationObserver(appearanceObserver, icon, WNIconAppearanceSettingsChanged, icon);
150 WMAddNotificationObserver(tileObserver, icon, WNIconTileSettingsChanged, icon);
152 return icon;
155 WIcon *icon_create_for_dock(WScreen *scr, char *command, char *wm_instance, char *wm_class, int tile)
157 WIcon *icon;
159 icon = icon_create_core(scr, 0, 0);
160 icon->tile_type = tile;
162 set_icon_image_from_database(icon, wm_instance, wm_class, command);
163 /* Update the icon, because icon could be NULL */
164 wIconUpdate(icon);
166 WMAddNotificationObserver(appearanceObserver, icon, WNIconAppearanceSettingsChanged, icon);
167 WMAddNotificationObserver(tileObserver, icon, WNIconTileSettingsChanged, icon);
169 return icon;
172 static WIcon *icon_create_core(WScreen *scr, int coord_x, int coord_y)
174 WIcon *icon;
176 icon = wmalloc(sizeof(WIcon));
177 icon->core = wCoreCreateTopLevel(scr,
178 coord_x,
179 coord_y,
180 wPreferences.icon_size,
181 wPreferences.icon_size,
182 0, scr->w_depth, scr->w_visual, scr->w_colormap,
183 scr->white_pixel);
185 /* will be overriden if this is a application icon */
186 icon->core->descriptor.handle_mousedown = miniwindowMouseDown;
187 icon->core->descriptor.handle_expose = miniwindowExpose;
188 icon->core->descriptor.parent_type = WCLASS_MINIWINDOW;
189 icon->core->descriptor.parent = icon;
191 icon->core->stacking = wmalloc(sizeof(WStacking));
192 icon->core->stacking->above = NULL;
193 icon->core->stacking->under = NULL;
194 icon->core->stacking->window_level = NORMAL_ICON_LEVEL;
195 icon->core->stacking->child_of = NULL;
197 /* Icon image */
198 icon->file = NULL;
199 icon->file_image = NULL;
201 return icon;
204 void wIconDestroy(WIcon * icon)
206 WCoreWindow *core = icon->core;
207 WScreen *scr = core->screen_ptr;
209 WMRemoveNotificationObserver(icon);
211 if (icon->handlerID)
212 WMDeleteTimerHandler(icon->handlerID);
214 if (icon->icon_win) {
215 int x = 0, y = 0;
217 if (icon->owner) {
218 x = icon->owner->icon_x;
219 y = icon->owner->icon_y;
221 XUnmapWindow(dpy, icon->icon_win);
222 XReparentWindow(dpy, icon->icon_win, scr->root_win, x, y);
224 if (icon->icon_name)
225 XFree(icon->icon_name);
227 if (icon->pixmap)
228 XFreePixmap(dpy, icon->pixmap);
230 unset_icon_image(icon);
232 wCoreDestroy(icon->core);
233 wfree(icon);
236 static void drawIconTitleBackground(WScreen *scr, Pixmap pixmap, int height)
238 XFillRectangle(dpy, pixmap, scr->icon_title_texture->normal_gc, 0, 0, wPreferences.icon_size, height + 1);
239 XDrawLine(dpy, pixmap, scr->icon_title_texture->light_gc, 0, 0, wPreferences.icon_size, 0);
240 XDrawLine(dpy, pixmap, scr->icon_title_texture->light_gc, 0, 0, 0, height + 1);
241 XDrawLine(dpy, pixmap, scr->icon_title_texture->dim_gc,
242 wPreferences.icon_size - 1, 0, wPreferences.icon_size - 1, height + 1);
245 static void icon_update_pixmap(WIcon *icon, RImage *image)
247 RImage *tile;
248 Pixmap pixmap;
249 int x, y, sx, sy;
250 unsigned w, h;
251 int theight = 0;
252 WScreen *scr = icon->core->screen_ptr;
254 if (icon->tile_type == TILE_NORMAL) {
255 tile = RCloneImage(scr->icon_tile);
256 } else {
257 assert(scr->clip_tile);
258 tile = RCloneImage(scr->clip_tile);
261 if (image) {
262 w = (image->width > wPreferences.icon_size)
263 ? wPreferences.icon_size : image->width;
264 x = (wPreferences.icon_size - w) / 2;
265 sx = (image->width - w) / 2;
267 if (icon->show_title)
268 theight = WMFontHeight(scr->icon_title_font);
270 h = (image->height + theight > wPreferences.icon_size
271 ? wPreferences.icon_size - theight : image->height);
272 y = theight + (wPreferences.icon_size - theight - h) / 2;
273 sy = (image->height - h) / 2;
275 RCombineArea(tile, image, sx, sy, w, h, x, y);
278 if (icon->shadowed) {
279 RColor color;
281 color.red = scr->icon_back_texture->light.red >> 8;
282 color.green = scr->icon_back_texture->light.green >> 8;
283 color.blue = scr->icon_back_texture->light.blue >> 8;
284 color.alpha = 150; /* about 60% */
285 RClearImage(tile, &color);
288 if (icon->highlighted) {
289 RColor color;
291 color.red = color.green = color.blue = 0;
292 color.alpha = 160;
293 RLightImage(tile, &color);
296 if (!RConvertImage(scr->rcontext, tile, &pixmap))
297 wwarning(_("error rendering image:%s"), RMessageForError(RErrorCode));
299 RReleaseImage(tile);
301 /* Draw the icon's title background (without text) */
302 if (icon->show_title)
303 drawIconTitleBackground(scr, pixmap, theight);
305 icon->pixmap = pixmap;
308 void wIconChangeTitle(WIcon *icon, char *new_title)
310 if (icon->icon_name != NULL)
311 XFree(icon->icon_name);
313 icon->icon_name = new_title;
314 wIconPaint(icon);
317 RImage *wIconValidateIconSize(RImage *icon, int max_size)
319 RImage *nimage;
321 if (!icon)
322 return NULL;
324 /* We should hold "ICON_BORDER" (~2) pixels to include the icon border */
325 if (((max_size + ICON_BORDER) < icon->width) ||
326 ((max_size + ICON_BORDER) < icon->height)) {
327 if (icon->width > icon->height)
328 nimage = RScaleImage(icon, max_size - ICON_BORDER,
329 (icon->height * (max_size - ICON_BORDER) / icon->width));
330 else
331 nimage = RScaleImage(icon, (icon->width * (max_size - ICON_BORDER) / icon->height),
332 max_size - ICON_BORDER);
333 RReleaseImage(icon);
334 icon = nimage;
337 return icon;
340 Bool wIconChangeImageFile(WIcon *icon, char *file)
342 WScreen *scr = icon->core->screen_ptr;
343 char *path;
344 RImage *image = NULL;
345 int error = 0;
347 /* If no new image, don't do nothing */
348 if (!file)
349 return True;
351 /* Find the new image */
352 path = FindImage(wPreferences.icon_path, file);
353 if (path)
354 image = get_rimage_from_file(scr, path, wPreferences.icon_size);
355 else
356 error = 1;
358 /* New image! */
359 if (!error && image) {
360 /* Set the new image */
361 set_icon_image_from_image(icon, image);
362 icon->file = wstrdup(path);
363 update_icon_pixmap(icon);
364 } else {
365 error = 1;
368 if (path)
369 wfree(path);
371 return !error;
374 static char *get_name_for_wwin(WWindow *wwin)
376 return get_name_for_instance_class(wwin->wm_instance, wwin->wm_class);
379 char *get_name_for_instance_class(char *wm_instance, char *wm_class)
381 char *suffix;
382 int len;
384 if (wm_class && wm_instance) {
385 len = strlen(wm_class) + strlen(wm_instance) + 2;
386 suffix = wmalloc(len);
387 snprintf(suffix, len, "%s.%s", wm_instance, wm_class);
388 } else if (wm_class) {
389 len = strlen(wm_class) + 1;
390 suffix = wmalloc(len);
391 snprintf(suffix, len, "%s", wm_class);
392 } else if (wm_instance) {
393 len = strlen(wm_instance) + 1;
394 suffix = wmalloc(len);
395 snprintf(suffix, len, "%s", wm_instance);
396 } else {
397 return NULL;
400 return suffix;
403 static char *get_icon_cache_path(void)
405 char *prefix, *path;
406 int len, ret;
408 prefix = wusergnusteppath();
409 len = strlen(prefix) + strlen(CACHE_ICON_PATH) + 2;
410 path = wmalloc(len);
411 snprintf(path, len, "%s%s/", prefix, CACHE_ICON_PATH);
413 /* If the folder exists, exit */
414 if (access(path, F_OK) == 0)
415 return path;
417 /* Create the folder */
418 ret = wmkdirhier((const char *) path);
420 /* Exit 1 on success, 0 on failure */
421 if (ret == 1)
422 return path;
424 /* Fail */
425 wfree(path);
426 return NULL;
429 static RImage *get_wwindow_image_from_wmhints(WWindow *wwin, WIcon *icon)
431 RImage *image = NULL;
432 XWMHints *hints = wwin->wm_hints;
434 if (hints && (hints->flags & IconPixmapHint) && hints->icon_pixmap != None)
435 image = RCreateImageFromDrawable(icon->core->screen_ptr->rcontext,
436 hints->icon_pixmap,
437 (hints->flags & IconMaskHint)
438 ? hints->icon_mask : None);
440 return image;
444 * wIconStore--
445 * Stores the client supplied icon at CACHE_ICON_PATH
446 * and returns the path for that icon. Returns NULL if there is no
447 * client supplied icon or on failure.
449 * Side effects:
450 * New directories might be created.
452 char *wIconStore(WIcon * icon)
454 char *path, *dir_path, *file;
455 int len = 0;
456 RImage *image = NULL;
457 WWindow *wwin = icon->owner;
459 if (!wwin)
460 return NULL;
462 dir_path = get_icon_cache_path();
463 if (!dir_path)
464 return NULL;
466 file = get_name_for_wwin(wwin);
467 if (!file) {
468 wfree(dir_path);
469 return NULL;
472 len = strlen(dir_path) + strlen(file) + 5;
473 path = wmalloc(len);
474 snprintf(path, len, "%s%s.xpm", dir_path, file);
475 wfree(dir_path);
476 wfree(file);
478 /* If icon exists, exit */
479 if (access(path, F_OK) == 0)
480 return path;
482 if (wwin->net_icon_image)
483 image = RRetainImage(wwin->net_icon_image);
484 else
485 image = get_wwindow_image_from_wmhints(wwin, icon);
487 if (!image) {
488 wfree(path);
489 return NULL;
492 if (!RSaveImage(image, path, "XPM")) {
493 wfree(path);
494 path = NULL;
497 RReleaseImage(image);
499 return path;
502 static void cycleColor(void *data)
504 WIcon *icon = (WIcon *) data;
505 WScreen *scr = icon->core->screen_ptr;
506 XGCValues gcv;
508 icon->step--;
509 gcv.dash_offset = icon->step;
510 XChangeGC(dpy, scr->icon_select_gc, GCDashOffset, &gcv);
512 XDrawRectangle(dpy, icon->core->window, scr->icon_select_gc, 0, 0,
513 icon->core->width - 1, icon->core->height - 1);
514 icon->handlerID = WMAddTimerHandler(COLOR_CYCLE_DELAY, cycleColor, icon);
517 void wIconSetHighlited(WIcon *icon, Bool flag)
519 if (icon->highlighted == flag)
520 return;
522 icon->highlighted = flag;
523 update_icon_pixmap(icon);
526 void wIconSelect(WIcon * icon)
528 WScreen *scr = icon->core->screen_ptr;
529 icon->selected = !icon->selected;
531 if (icon->selected) {
532 icon->step = 0;
533 if (!wPreferences.dont_blink)
534 icon->handlerID = WMAddTimerHandler(10, cycleColor, icon);
535 else
536 XDrawRectangle(dpy, icon->core->window, scr->icon_select_gc, 0, 0,
537 icon->core->width - 1, icon->core->height - 1);
538 } else {
539 if (icon->handlerID) {
540 WMDeleteTimerHandler(icon->handlerID);
541 icon->handlerID = NULL;
543 XClearArea(dpy, icon->core->window, 0, 0, icon->core->width, icon->core->height, True);
547 static void unset_icon_image(WIcon *icon)
549 if (icon->file) {
550 wfree(icon->file);
551 icon->file = NULL;
554 if (icon->file_image) {
555 RReleaseImage(icon->file_image);
556 icon->file_image = NULL;
560 void set_icon_image_from_image(WIcon *icon, RImage *image)
562 if (!icon)
563 return;
565 unset_icon_image(icon);
567 icon->file_image = NULL;
568 icon->file_image = image;
571 void wIconUpdate(WIcon *icon)
573 WWindow *wwin = NULL;
575 if (icon && icon->owner)
576 wwin = icon->owner;
578 if (wwin && WFLAGP(wwin, always_user_icon)) {
579 /* Forced use user_icon */
580 get_rimage_icon_from_user_icon(icon);
581 } else if (icon->icon_win != None) {
582 /* Get the Pixmap from the WIcon */
583 get_rimage_icon_from_icon_win(icon);
584 } else if (wwin && wwin->net_icon_image) {
585 /* Use _NET_WM_ICON icon */
586 get_rimage_icon_from_x11(icon);
587 } else if (wwin && wwin->wm_hints && (wwin->wm_hints->flags & IconPixmapHint)) {
588 /* Get the Pixmap from the wm_hints, else, from the user */
589 unset_icon_image(icon);
590 icon->file_image = get_rimage_icon_from_wm_hints(icon);
591 if (!icon->file_image)
592 get_rimage_icon_from_user_icon(icon);
593 } else {
594 /* Get the Pixmap from the user */
595 get_rimage_icon_from_user_icon(icon);
598 update_icon_pixmap(icon);
601 void update_icon_pixmap(WIcon *icon)
603 if (icon->pixmap != None)
604 XFreePixmap(dpy, icon->pixmap);
606 icon->pixmap = None;
608 /* Create the pixmap */
609 if (icon->file_image)
610 icon_update_pixmap(icon, icon->file_image);
612 /* If dockapp, put inside the icon */
613 if (icon->icon_win != None) {
614 /* file_image is NULL, because is docked app */
615 icon_update_pixmap(icon, NULL);
616 set_dockapp_in_icon(icon);
619 /* No pixmap, set default background */
620 if (icon->pixmap != None)
621 XSetWindowBackgroundPixmap(dpy, icon->core->window, icon->pixmap);
623 /* Paint it */
624 wIconPaint(icon);
627 static void get_rimage_icon_from_x11(WIcon *icon)
629 /* Remove the icon image */
630 unset_icon_image(icon);
632 /* Set the new icon image */
633 icon->file_image = RRetainImage(icon->owner->net_icon_image);
636 static void get_rimage_icon_from_user_icon(WIcon *icon)
638 if (icon->file_image)
639 return;
641 get_rimage_icon_from_default_icon(icon);
644 static void get_rimage_icon_from_default_icon(WIcon *icon)
646 WScreen *scr = icon->core->screen_ptr;
648 /* If the icon don't have image, we should use the default image. */
649 if (!scr->def_icon_rimage)
650 scr->def_icon_rimage = get_default_image(scr);
652 /* Remove the icon image */
653 unset_icon_image(icon);
655 /* Set the new icon image */
656 icon->file_image = RRetainImage(scr->def_icon_rimage);
659 /* Get the RImage from the WIcon of the WWindow */
660 static void get_rimage_icon_from_icon_win(WIcon *icon)
662 RImage *image;
664 /* Create the new RImage */
665 image = get_window_image_from_x11(icon->icon_win);
667 /* Free the icon info */
668 unset_icon_image(icon);
670 /* Set the new info */
671 icon->file_image = image;
674 /* Set the dockapp in the WIcon */
675 static void set_dockapp_in_icon(WIcon *icon)
677 XWindowAttributes attr;
678 WScreen *scr = icon->core->screen_ptr;
679 unsigned int w, h, d;
681 /* Reparent the dock application to the icon */
683 /* We need the application size to center it
684 * and show in the correct position */
685 getSize(icon->icon_win, &w, &h, &d);
687 /* Set the background pixmap */
688 XSetWindowBackgroundPixmap(dpy, icon->core->window, scr->icon_tile_pixmap);
690 /* Set the icon border */
691 XSetWindowBorderWidth(dpy, icon->icon_win, 0);
693 /* Put the dock application in the icon */
694 XReparentWindow(dpy, icon->icon_win, icon->core->window,
695 (wPreferences.icon_size - w) / 2,
696 (wPreferences.icon_size - h) / 2);
698 /* Show it and save */
699 XMapWindow(dpy, icon->icon_win);
700 XAddToSaveSet(dpy, icon->icon_win);
702 /* Needed to move the icon clicking on the application part */
703 if ((XGetWindowAttributes(dpy, icon->icon_win, &attr)) &&
704 (attr.all_event_masks & ButtonPressMask))
705 wHackedGrabButton(Button1, MOD_MASK, icon->core->window, True,
706 ButtonPressMask, GrabModeSync, GrabModeAsync,
707 None, wCursor[WCUR_ARROW]);
710 /* Get the RImage from the XWindow wm_hints */
711 RImage *get_rimage_icon_from_wm_hints(WIcon *icon)
713 RImage *image = NULL;
714 unsigned int w, h, d;
715 WWindow *wwin = icon->owner;
717 if (!getSize(wwin->wm_hints->icon_pixmap, &w, &h, &d)) {
718 icon->owner->wm_hints->flags &= ~IconPixmapHint;
719 return NULL;
722 image = get_wwindow_image_from_wmhints(wwin, icon);
723 if (!image)
724 return NULL;
726 /* Resize the icon to the wPreferences.icon_size size */
727 image = wIconValidateIconSize(image, wPreferences.icon_size);
729 return image;
732 /* This function updates in the screen the icon title */
733 static void update_icon_title(WIcon *icon)
735 WScreen *scr = icon->core->screen_ptr;
736 int x, l, w;
737 char *tmp;
739 /* draw the icon title */
740 if (icon->show_title && icon->icon_name != NULL) {
741 tmp = ShrinkString(scr->icon_title_font, icon->icon_name, wPreferences.icon_size - 4);
742 w = WMWidthOfString(scr->icon_title_font, tmp, l = strlen(tmp));
744 if (w > icon->core->width - 4)
745 x = (icon->core->width - 4) - w;
746 else
747 x = (icon->core->width - w) / 2;
749 WMDrawString(scr->wmscreen, icon->core->window, scr->icon_title_color,
750 scr->icon_title_font, x, 1, tmp, l);
751 wfree(tmp);
756 void wIconPaint(WIcon *icon)
758 if (!icon || !icon->core || !icon->core->screen_ptr)
759 return;
761 WScreen *scr = icon->core->screen_ptr;
763 XClearWindow(dpy, icon->core->window);
765 update_icon_title(icon);
767 if (icon->selected)
768 XDrawRectangle(dpy, icon->core->window, scr->icon_select_gc, 0, 0,
769 icon->core->width - 1, icon->core->height - 1);
772 /******************************************************************/
774 static void miniwindowExpose(WObjDescriptor * desc, XEvent * event)
776 wIconPaint(desc->parent);
779 static void miniwindowDblClick(WObjDescriptor * desc, XEvent * event)
781 WIcon *icon = desc->parent;
783 assert(icon->owner != NULL);
785 wDeiconifyWindow(icon->owner);
788 static void miniwindowMouseDown(WObjDescriptor * desc, XEvent * event)
790 WIcon *icon = desc->parent;
791 WWindow *wwin = icon->owner;
792 XEvent ev;
793 int x = wwin->icon_x, y = wwin->icon_y;
794 int dx = event->xbutton.x, dy = event->xbutton.y;
795 int grabbed = 0;
796 int clickButton = event->xbutton.button;
797 Bool hasMoved = False;
799 if (WCHECK_STATE(WSTATE_MODAL))
800 return;
802 if (IsDoubleClick(icon->core->screen_ptr, event)) {
803 miniwindowDblClick(desc, event);
804 return;
807 if (event->xbutton.button == Button1) {
808 if (event->xbutton.state & MOD_MASK)
809 wLowerFrame(icon->core);
810 else
811 wRaiseFrame(icon->core);
812 if (event->xbutton.state & ShiftMask) {
813 wIconSelect(icon);
814 wSelectWindow(icon->owner, !wwin->flags.selected);
816 } else if (event->xbutton.button == Button3) {
817 WObjDescriptor *desc;
819 OpenMiniwindowMenu(wwin, event->xbutton.x_root, event->xbutton.y_root);
821 /* allow drag select of menu */
822 desc = &wwin->screen_ptr->window_menu->menu->descriptor;
823 event->xbutton.send_event = True;
824 (*desc->handle_mousedown) (desc, event);
826 return;
829 if (XGrabPointer(dpy, icon->core->window, False, ButtonMotionMask
830 | ButtonReleaseMask | ButtonPressMask, GrabModeAsync,
831 GrabModeAsync, None, None, CurrentTime) != GrabSuccess) {
833 while (1) {
834 WMMaskEvent(dpy, PointerMotionMask | ButtonReleaseMask | ButtonPressMask
835 | ButtonMotionMask | ExposureMask, &ev);
836 switch (ev.type) {
837 case Expose:
838 WMHandleEvent(&ev);
839 break;
841 case MotionNotify:
842 hasMoved = True;
843 if (!grabbed) {
844 if (abs(dx - ev.xmotion.x) >= MOVE_THRESHOLD
845 || abs(dy - ev.xmotion.y) >= MOVE_THRESHOLD) {
846 XChangeActivePointerGrab(dpy, ButtonMotionMask
847 | ButtonReleaseMask | ButtonPressMask,
848 wCursor[WCUR_MOVE], CurrentTime);
849 grabbed = 1;
850 } else {
851 break;
854 x = ev.xmotion.x_root - dx;
855 y = ev.xmotion.y_root - dy;
856 XMoveWindow(dpy, icon->core->window, x, y);
857 break;
859 case ButtonPress:
860 break;
862 case ButtonRelease:
863 if (ev.xbutton.button != clickButton)
864 break;
866 if (wwin->icon_x != x || wwin->icon_y != y)
867 wwin->flags.icon_moved = 1;
869 XMoveWindow(dpy, icon->core->window, x, y);
871 wwin->icon_x = x;
872 wwin->icon_y = y;
873 XUngrabPointer(dpy, CurrentTime);
875 if (wPreferences.auto_arrange_icons)
876 wArrangeIcons(wwin->screen_ptr, True);
877 if (wPreferences.single_click && !hasMoved)
878 miniwindowDblClick(desc, event);
879 return;
885 void set_icon_image_from_database(WIcon *icon, char *wm_instance, char *wm_class, char *command)
887 char *file = NULL;
889 file = get_icon_filename(icon->core->screen_ptr, wm_instance, wm_class, command, False);
890 if (file) {
891 icon->file = wstrdup(file);
892 icon->file_image = get_rimage_from_file(icon->core->screen_ptr, icon->file, wPreferences.icon_size);
893 wfree(file);