wIconChangeImageFile use set_icon_image_from_image
[wmaker-crm.git] / src / icon.c
bloba2cb835691fd26a7610d8b3222fb52e3dba75b32
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 wIconUpdate(icon, NULL);
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);
147 WMAddNotificationObserver(appearanceObserver, icon, WNIconAppearanceSettingsChanged, icon);
148 WMAddNotificationObserver(tileObserver, icon, WNIconTileSettingsChanged, icon);
150 return icon;
153 WIcon *icon_create_for_dock(WScreen *scr, char *command, char *wm_instance, char *wm_class, int tile)
155 WIcon *icon;
157 icon = icon_create_core(scr, 0, 0);
158 icon->tile_type = tile;
160 set_icon_image_from_database(icon, wm_instance, wm_class, command);
162 WMAddNotificationObserver(appearanceObserver, icon, WNIconAppearanceSettingsChanged, icon);
163 WMAddNotificationObserver(tileObserver, icon, WNIconTileSettingsChanged, icon);
165 return icon;
168 static WIcon *icon_create_core(WScreen *scr, int coord_x, int coord_y)
170 WIcon *icon;
172 icon = wmalloc(sizeof(WIcon));
173 icon->core = wCoreCreateTopLevel(scr,
174 coord_x,
175 coord_y,
176 wPreferences.icon_size,
177 wPreferences.icon_size,
178 0, scr->w_depth, scr->w_visual, scr->w_colormap,
179 scr->white_pixel);
181 /* will be overriden if this is a application icon */
182 icon->core->descriptor.handle_mousedown = miniwindowMouseDown;
183 icon->core->descriptor.handle_expose = miniwindowExpose;
184 icon->core->descriptor.parent_type = WCLASS_MINIWINDOW;
185 icon->core->descriptor.parent = icon;
187 icon->core->stacking = wmalloc(sizeof(WStacking));
188 icon->core->stacking->above = NULL;
189 icon->core->stacking->under = NULL;
190 icon->core->stacking->window_level = NORMAL_ICON_LEVEL;
191 icon->core->stacking->child_of = NULL;
193 /* Icon image */
194 icon->file = NULL;
195 icon->file_image = NULL;
197 return icon;
200 void wIconDestroy(WIcon * icon)
202 WCoreWindow *core = icon->core;
203 WScreen *scr = core->screen_ptr;
205 WMRemoveNotificationObserver(icon);
207 if (icon->handlerID)
208 WMDeleteTimerHandler(icon->handlerID);
210 if (icon->icon_win) {
211 int x = 0, y = 0;
213 if (icon->owner) {
214 x = icon->owner->icon_x;
215 y = icon->owner->icon_y;
217 XUnmapWindow(dpy, icon->icon_win);
218 XReparentWindow(dpy, icon->icon_win, scr->root_win, x, y);
220 if (icon->icon_name)
221 XFree(icon->icon_name);
223 if (icon->pixmap)
224 XFreePixmap(dpy, icon->pixmap);
226 unset_icon_image(icon);
228 wCoreDestroy(icon->core);
229 wfree(icon);
232 static void drawIconTitle(WScreen * scr, Pixmap pixmap, int height)
234 XFillRectangle(dpy, pixmap, scr->icon_title_texture->normal_gc, 0, 0, wPreferences.icon_size, height + 1);
235 XDrawLine(dpy, pixmap, scr->icon_title_texture->light_gc, 0, 0, wPreferences.icon_size, 0);
236 XDrawLine(dpy, pixmap, scr->icon_title_texture->light_gc, 0, 0, 0, height + 1);
237 XDrawLine(dpy, pixmap, scr->icon_title_texture->dim_gc,
238 wPreferences.icon_size - 1, 0, wPreferences.icon_size - 1, height + 1);
241 static void icon_update_pixmap(WIcon *icon, RImage *image)
243 RImage *tile;
244 Pixmap pixmap;
245 int x, y, sx, sy;
246 unsigned w, h;
247 int theight = 0;
248 WScreen *scr = icon->core->screen_ptr;
249 int titled = icon->show_title;
251 if (icon->tile_type == TILE_NORMAL) {
252 tile = RCloneImage(scr->icon_tile);
253 } else {
254 assert(scr->clip_tile);
255 tile = RCloneImage(scr->clip_tile);
258 if (image) {
259 w = (image->width > wPreferences.icon_size)
260 ? wPreferences.icon_size : image->width;
261 x = (wPreferences.icon_size - w) / 2;
262 sx = (image->width - w) / 2;
264 if (titled)
265 theight = WMFontHeight(scr->icon_title_font);
267 h = (image->height + theight > wPreferences.icon_size
268 ? wPreferences.icon_size - theight : image->height);
269 y = theight + (wPreferences.icon_size - theight - h) / 2;
270 sy = (image->height - h) / 2;
272 RCombineArea(tile, image, sx, sy, w, h, x, y);
275 if (icon->shadowed) {
276 RColor color;
278 color.red = scr->icon_back_texture->light.red >> 8;
279 color.green = scr->icon_back_texture->light.green >> 8;
280 color.blue = scr->icon_back_texture->light.blue >> 8;
281 color.alpha = 150; /* about 60% */
282 RClearImage(tile, &color);
285 if (icon->highlighted) {
286 RColor color;
288 color.red = color.green = color.blue = 0;
289 color.alpha = 160;
290 RLightImage(tile, &color);
293 if (!RConvertImage(scr->rcontext, tile, &pixmap))
294 wwarning(_("error rendering image:%s"), RMessageForError(RErrorCode));
296 RReleaseImage(tile);
298 if (titled)
299 drawIconTitle(scr, pixmap, theight);
301 icon->pixmap = pixmap;
304 void wIconChangeTitle(WIcon *icon, char *new_title)
306 int changed;
308 changed = (new_title == NULL && icon->icon_name != NULL) ||
309 (new_title != NULL && icon->icon_name == NULL);
311 if (icon->icon_name != NULL)
312 XFree(icon->icon_name);
314 icon->icon_name = new_title;
316 if (changed)
317 wIconUpdate(icon, NULL);
318 else
319 wIconPaint(icon);
322 RImage *wIconValidateIconSize(RImage *icon, int max_size)
324 RImage *nimage;
326 if (!icon)
327 return NULL;
329 /* We should hold "ICON_BORDER" (~2) pixels to include the icon border */
330 if (((max_size + ICON_BORDER) < icon->width) ||
331 ((max_size + ICON_BORDER) < icon->height)) {
332 if (icon->width > icon->height)
333 nimage = RScaleImage(icon, max_size - ICON_BORDER,
334 (icon->height * (max_size - ICON_BORDER) / icon->width));
335 else
336 nimage = RScaleImage(icon, (icon->width * (max_size - ICON_BORDER) / icon->height),
337 max_size - ICON_BORDER);
338 RReleaseImage(icon);
339 icon = nimage;
342 return icon;
345 Bool wIconChangeImageFile(WIcon *icon, char *file)
347 WScreen *scr = icon->core->screen_ptr;
348 char *path;
349 RImage *image = NULL;
350 int error = 0;
352 /* If no new image, don't do nothing */
353 if (!file)
354 return True;
356 /* Find the new image */
357 path = FindImage(wPreferences.icon_path, file);
358 if (path)
359 image = get_rimage_from_file(scr, path, wPreferences.icon_size);
360 else
361 error = 1;
363 /* New image! */
364 if (!error && image) {
365 /* Set the new image */
366 set_icon_image_from_image(icon, image);
367 icon->file = wstrdup(path);
368 update_icon_pixmap(icon);
369 } else {
370 error = 1;
373 if (path)
374 wfree(path);
376 return !error;
379 static char *get_name_for_wwin(WWindow *wwin)
381 return get_name_for_instance_class(wwin->wm_instance, wwin->wm_class);
384 char *get_name_for_instance_class(char *wm_instance, char *wm_class)
386 char *suffix;
387 int len;
389 if (wm_class && wm_instance) {
390 len = strlen(wm_class) + strlen(wm_instance) + 2;
391 suffix = wmalloc(len);
392 snprintf(suffix, len, "%s.%s", wm_instance, wm_class);
393 } else if (wm_class) {
394 len = strlen(wm_class) + 1;
395 suffix = wmalloc(len);
396 snprintf(suffix, len, "%s", wm_class);
397 } else if (wm_instance) {
398 len = strlen(wm_instance) + 1;
399 suffix = wmalloc(len);
400 snprintf(suffix, len, "%s", wm_instance);
401 } else {
402 return NULL;
405 return suffix;
408 static char *get_icon_cache_path(void)
410 char *prefix, *path;
411 int len, ret;
413 prefix = wusergnusteppath();
414 len = strlen(prefix) + strlen(CACHE_ICON_PATH) + 2;
415 path = wmalloc(len);
416 snprintf(path, len, "%s%s/", prefix, CACHE_ICON_PATH);
418 /* If the folder exists, exit */
419 if (access(path, F_OK) == 0)
420 return path;
422 /* Create the folder */
423 ret = wmkdirhier((const char *) path);
425 /* Exit 1 on success, 0 on failure */
426 if (ret == 1)
427 return path;
429 /* Fail */
430 wfree(path);
431 return NULL;
434 static RImage *get_wwindow_image_from_wmhints(WWindow *wwin, WIcon *icon)
436 RImage *image = NULL;
437 XWMHints *hints = wwin->wm_hints;
439 if (hints && (hints->flags & IconPixmapHint) && hints->icon_pixmap != None)
440 image = RCreateImageFromDrawable(icon->core->screen_ptr->rcontext,
441 hints->icon_pixmap,
442 (hints->flags & IconMaskHint)
443 ? hints->icon_mask : None);
445 return image;
449 * wIconStore--
450 * Stores the client supplied icon at CACHE_ICON_PATH
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 *wIconStore(WIcon * icon)
459 char *path, *dir_path, *file;
460 int len = 0;
461 RImage *image = NULL;
462 WWindow *wwin = icon->owner;
464 if (!wwin)
465 return NULL;
467 dir_path = get_icon_cache_path();
468 if (!dir_path)
469 return NULL;
471 file = get_name_for_wwin(wwin);
472 if (!file) {
473 wfree(dir_path);
474 return NULL;
477 len = strlen(dir_path) + strlen(file) + 5;
478 path = wmalloc(len);
479 snprintf(path, len, "%s%s.xpm", dir_path, file);
480 wfree(dir_path);
481 wfree(file);
483 /* If icon exists, exit */
484 if (access(path, F_OK) == 0)
485 return path;
487 if (wwin->net_icon_image)
488 image = RRetainImage(wwin->net_icon_image);
489 else
490 image = get_wwindow_image_from_wmhints(wwin, icon);
492 if (!image) {
493 wfree(path);
494 return NULL;
497 if (!RSaveImage(image, path, "XPM")) {
498 wfree(path);
499 path = NULL;
502 RReleaseImage(image);
504 return path;
507 static void cycleColor(void *data)
509 WIcon *icon = (WIcon *) data;
510 WScreen *scr = icon->core->screen_ptr;
511 XGCValues gcv;
513 icon->step--;
514 gcv.dash_offset = icon->step;
515 XChangeGC(dpy, scr->icon_select_gc, GCDashOffset, &gcv);
517 XDrawRectangle(dpy, icon->core->window, scr->icon_select_gc, 0, 0,
518 icon->core->width - 1, icon->core->height - 1);
519 icon->handlerID = WMAddTimerHandler(COLOR_CYCLE_DELAY, cycleColor, icon);
522 void wIconSetHighlited(WIcon *icon, Bool flag)
524 if (icon->highlighted == flag)
525 return;
527 icon->highlighted = flag;
528 update_icon_pixmap(icon);
531 void wIconSelect(WIcon * icon)
533 WScreen *scr = icon->core->screen_ptr;
534 icon->selected = !icon->selected;
536 if (icon->selected) {
537 icon->step = 0;
538 if (!wPreferences.dont_blink)
539 icon->handlerID = WMAddTimerHandler(10, cycleColor, icon);
540 else
541 XDrawRectangle(dpy, icon->core->window, scr->icon_select_gc, 0, 0,
542 icon->core->width - 1, icon->core->height - 1);
543 } else {
544 if (icon->handlerID) {
545 WMDeleteTimerHandler(icon->handlerID);
546 icon->handlerID = NULL;
548 XClearArea(dpy, icon->core->window, 0, 0, icon->core->width, icon->core->height, True);
552 static void unset_icon_image(WIcon *icon)
554 if (icon->file) {
555 wfree(icon->file);
556 icon->file = NULL;
559 if (icon->file_image) {
560 RReleaseImage(icon->file_image);
561 icon->file_image = NULL;
565 void set_icon_image_from_image(WIcon *icon, RImage *image)
567 if (!icon)
568 return;
570 unset_icon_image(icon);
572 icon->file_image = NULL;
573 icon->file_image = image;
576 void wIconUpdate(WIcon *icon, RImage *image)
578 WWindow *wwin = NULL;
580 if (image) {
581 icon->file_image = image;
582 } else {
583 if (icon && icon->owner)
584 wwin = icon->owner;
586 if (wwin && WFLAGP(wwin, always_user_icon)) {
587 /* Forced use user_icon */
588 get_rimage_icon_from_user_icon(icon);
589 } else if (icon->icon_win != None) {
590 /* Get the Pixmap from the WIcon */
591 get_rimage_icon_from_icon_win(icon);
592 } else if (wwin && wwin->net_icon_image) {
593 /* Use _NET_WM_ICON icon */
594 get_rimage_icon_from_x11(icon);
595 } else if (wwin && wwin->wm_hints && (wwin->wm_hints->flags & IconPixmapHint)) {
596 /* Get the Pixmap from the wm_hints, else, from the user */
597 unset_icon_image(icon);
598 icon->file_image = get_rimage_icon_from_wm_hints(icon);
599 if (!icon->file_image)
600 get_rimage_icon_from_user_icon(icon);
601 } else {
602 /* Get the Pixmap from the user */
603 get_rimage_icon_from_user_icon(icon);
607 update_icon_pixmap(icon);
610 void update_icon_pixmap(WIcon *icon)
612 if (icon->pixmap != None)
613 XFreePixmap(dpy, icon->pixmap);
615 icon->pixmap = None;
617 /* Create the pixmap */
618 if (icon->file_image)
619 icon_update_pixmap(icon, icon->file_image);
621 /* If dockapp, put inside the icon */
622 if (icon->icon_win != None) {
623 /* file_image is NULL, because is docked app */
624 icon_update_pixmap(icon, NULL);
625 set_dockapp_in_icon(icon);
628 /* No pixmap, set default background */
629 if (icon->pixmap != None)
630 XSetWindowBackgroundPixmap(dpy, icon->core->window, icon->pixmap);
632 /* Paint it */
633 wIconPaint(icon);
636 static void get_rimage_icon_from_x11(WIcon *icon)
638 /* Remove the icon image */
639 unset_icon_image(icon);
641 /* Set the new icon image */
642 icon->file_image = RRetainImage(icon->owner->net_icon_image);
645 static void get_rimage_icon_from_user_icon(WIcon *icon)
647 if (icon->file_image)
648 return;
650 get_rimage_icon_from_default_icon(icon);
653 static void get_rimage_icon_from_default_icon(WIcon *icon)
655 WScreen *scr = icon->core->screen_ptr;
657 /* If the icon don't have image, we should use the default image. */
658 if (!scr->def_icon_rimage)
659 scr->def_icon_rimage = get_default_image(scr);
661 /* Remove the icon image */
662 unset_icon_image(icon);
664 /* Set the new icon image */
665 icon->file_image = RRetainImage(scr->def_icon_rimage);
668 /* Get the RImage from the WIcon of the WWindow */
669 static void get_rimage_icon_from_icon_win(WIcon *icon)
671 RImage *image;
673 /* Create the new RImage */
674 image = get_window_image_from_x11(icon->icon_win);
676 /* Free the icon info */
677 unset_icon_image(icon);
679 /* Set the new info */
680 icon->file_image = image;
683 /* Set the dockapp in the WIcon */
684 static void set_dockapp_in_icon(WIcon *icon)
686 XWindowAttributes attr;
687 WScreen *scr = icon->core->screen_ptr;
688 int title_height = WMFontHeight(scr->icon_title_font);
689 unsigned int w, h, d;
690 int theight = 0;
692 /* Reparent the dock application to the icon */
694 /* We need the application size to center it
695 * and show in the correct position */
696 getSize(icon->icon_win, &w, &h, &d);
698 /* Set extra space for title */
699 if (icon->show_title && (h + title_height < wPreferences.icon_size)) {
700 theight = title_height;
701 drawIconTitle(scr, icon->pixmap, theight);
702 } else {
703 XSetWindowBackgroundPixmap(dpy, icon->core->window, scr->icon_tile_pixmap);
706 /* Set the icon border */
707 XSetWindowBorderWidth(dpy, icon->icon_win, 0);
709 /* Put the dock application in the icon */
710 XReparentWindow(dpy, icon->icon_win, icon->core->window,
711 (wPreferences.icon_size - w) / 2,
712 theight + (wPreferences.icon_size - h - theight) / 2);
714 /* Show it and save */
715 XMapWindow(dpy, icon->icon_win);
716 XAddToSaveSet(dpy, icon->icon_win);
718 /* Needed to move the icon clicking on the application part */
719 if ((XGetWindowAttributes(dpy, icon->icon_win, &attr)) &&
720 (attr.all_event_masks & ButtonPressMask))
721 wHackedGrabButton(Button1, MOD_MASK, icon->core->window, True,
722 ButtonPressMask, GrabModeSync, GrabModeAsync,
723 None, wCursor[WCUR_ARROW]);
726 /* Get the RImage from the XWindow wm_hints */
727 RImage *get_rimage_icon_from_wm_hints(WIcon *icon)
729 RImage *image = NULL;
730 unsigned int w, h, d;
731 WWindow *wwin = icon->owner;
733 if (!getSize(wwin->wm_hints->icon_pixmap, &w, &h, &d)) {
734 icon->owner->wm_hints->flags &= ~IconPixmapHint;
735 return NULL;
738 image = get_wwindow_image_from_wmhints(wwin, icon);
739 if (!image)
740 return NULL;
742 /* Resize the icon to the wPreferences.icon_size size */
743 image = wIconValidateIconSize(image, wPreferences.icon_size);
745 return image;
748 void wIconPaint(WIcon *icon)
750 WScreen *scr = icon->core->screen_ptr;
751 int x, l, w;
752 char *tmp;
754 XClearWindow(dpy, icon->core->window);
756 /* draw the icon title */
757 if (icon->show_title && icon->icon_name != NULL) {
758 tmp = ShrinkString(scr->icon_title_font, icon->icon_name, wPreferences.icon_size - 4);
759 w = WMWidthOfString(scr->icon_title_font, tmp, l = strlen(tmp));
761 if (w > icon->core->width - 4)
762 x = (icon->core->width - 4) - w;
763 else
764 x = (icon->core->width - w) / 2;
766 WMDrawString(scr->wmscreen, icon->core->window, scr->icon_title_color,
767 scr->icon_title_font, x, 1, tmp, l);
768 wfree(tmp);
771 if (icon->selected)
772 XDrawRectangle(dpy, icon->core->window, scr->icon_select_gc, 0, 0,
773 icon->core->width - 1, icon->core->height - 1);
776 /******************************************************************/
778 static void miniwindowExpose(WObjDescriptor * desc, XEvent * event)
780 wIconPaint(desc->parent);
783 static void miniwindowDblClick(WObjDescriptor * desc, XEvent * event)
785 WIcon *icon = desc->parent;
787 assert(icon->owner != NULL);
789 wDeiconifyWindow(icon->owner);
792 static void miniwindowMouseDown(WObjDescriptor * desc, XEvent * event)
794 WIcon *icon = desc->parent;
795 WWindow *wwin = icon->owner;
796 XEvent ev;
797 int x = wwin->icon_x, y = wwin->icon_y;
798 int dx = event->xbutton.x, dy = event->xbutton.y;
799 int grabbed = 0;
800 int clickButton = event->xbutton.button;
801 Bool hasMoved = False;
803 if (WCHECK_STATE(WSTATE_MODAL))
804 return;
806 if (IsDoubleClick(icon->core->screen_ptr, event)) {
807 miniwindowDblClick(desc, event);
808 return;
811 if (event->xbutton.button == Button1) {
812 if (event->xbutton.state & MOD_MASK)
813 wLowerFrame(icon->core);
814 else
815 wRaiseFrame(icon->core);
816 if (event->xbutton.state & ShiftMask) {
817 wIconSelect(icon);
818 wSelectWindow(icon->owner, !wwin->flags.selected);
820 } else if (event->xbutton.button == Button3) {
821 WObjDescriptor *desc;
823 OpenMiniwindowMenu(wwin, event->xbutton.x_root, event->xbutton.y_root);
825 /* allow drag select of menu */
826 desc = &wwin->screen_ptr->window_menu->menu->descriptor;
827 event->xbutton.send_event = True;
828 (*desc->handle_mousedown) (desc, event);
830 return;
833 if (XGrabPointer(dpy, icon->core->window, False, ButtonMotionMask
834 | ButtonReleaseMask | ButtonPressMask, GrabModeAsync,
835 GrabModeAsync, None, None, CurrentTime) != GrabSuccess) {
837 while (1) {
838 WMMaskEvent(dpy, PointerMotionMask | ButtonReleaseMask | ButtonPressMask
839 | ButtonMotionMask | ExposureMask, &ev);
840 switch (ev.type) {
841 case Expose:
842 WMHandleEvent(&ev);
843 break;
845 case MotionNotify:
846 hasMoved = True;
847 if (!grabbed) {
848 if (abs(dx - ev.xmotion.x) >= MOVE_THRESHOLD
849 || abs(dy - ev.xmotion.y) >= MOVE_THRESHOLD) {
850 XChangeActivePointerGrab(dpy, ButtonMotionMask
851 | ButtonReleaseMask | ButtonPressMask,
852 wCursor[WCUR_MOVE], CurrentTime);
853 grabbed = 1;
854 } else {
855 break;
858 x = ev.xmotion.x_root - dx;
859 y = ev.xmotion.y_root - dy;
860 XMoveWindow(dpy, icon->core->window, x, y);
861 break;
863 case ButtonPress:
864 break;
866 case ButtonRelease:
867 if (ev.xbutton.button != clickButton)
868 break;
870 if (wwin->icon_x != x || wwin->icon_y != y)
871 wwin->flags.icon_moved = 1;
873 XMoveWindow(dpy, icon->core->window, x, y);
875 wwin->icon_x = x;
876 wwin->icon_y = y;
877 XUngrabPointer(dpy, CurrentTime);
879 if (wPreferences.auto_arrange_icons)
880 wArrangeIcons(wwin->screen_ptr, True);
881 if (wPreferences.single_click && !hasMoved)
882 miniwindowDblClick(desc, event);
883 return;
889 void set_icon_image_from_database(WIcon *icon, char *wm_instance, char *wm_class, char *command)
891 char *file = NULL;
893 file = get_icon_filename(icon->core->screen_ptr, wm_instance, wm_class, command, False);
894 if (file) {
895 icon->file = wstrdup(file);
896 icon->file_image = get_rimage_from_file(icon->core->screen_ptr, icon->file, wPreferences.icon_size);
897 wfree(file);
900 wIconUpdate(icon, NULL);