Window attributes moved to wCoreCreateTopLevel
[wmaker-crm.git] / src / icon.c
blob62f618a5c0bd7a93efddc983c85f6bb895aca61b
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 /* Remove the old one */
366 unset_icon_image(icon);
368 /* Set the new image */
369 icon->file = wstrdup(path);
370 wIconUpdate(icon, image);
371 } else {
372 error = 1;
375 if (path)
376 wfree(path);
378 return !error;
381 static char *get_name_for_wwin(WWindow *wwin)
383 return get_name_for_instance_class(wwin->wm_instance, wwin->wm_class);
386 char *get_name_for_instance_class(char *wm_instance, char *wm_class)
388 char *suffix;
389 int len;
391 if (wm_class && wm_instance) {
392 len = strlen(wm_class) + strlen(wm_instance) + 2;
393 suffix = wmalloc(len);
394 snprintf(suffix, len, "%s.%s", wm_instance, wm_class);
395 } else if (wm_class) {
396 len = strlen(wm_class) + 1;
397 suffix = wmalloc(len);
398 snprintf(suffix, len, "%s", wm_class);
399 } else if (wm_instance) {
400 len = strlen(wm_instance) + 1;
401 suffix = wmalloc(len);
402 snprintf(suffix, len, "%s", wm_instance);
403 } else {
404 return NULL;
407 return suffix;
410 static char *get_icon_cache_path(void)
412 char *prefix, *path;
413 int len, ret;
415 prefix = wusergnusteppath();
416 len = strlen(prefix) + strlen(CACHE_ICON_PATH) + 2;
417 path = wmalloc(len);
418 snprintf(path, len, "%s%s/", prefix, CACHE_ICON_PATH);
420 /* If the folder exists, exit */
421 if (access(path, F_OK) == 0)
422 return path;
424 /* Create the folder */
425 ret = wmkdirhier((const char *) path);
427 /* Exit 1 on success, 0 on failure */
428 if (ret == 1)
429 return path;
431 /* Fail */
432 wfree(path);
433 return NULL;
436 static RImage *get_wwindow_image_from_wmhints(WWindow *wwin, WIcon *icon)
438 RImage *image = NULL;
439 XWMHints *hints = wwin->wm_hints;
441 if (hints && (hints->flags & IconPixmapHint) && hints->icon_pixmap != None)
442 image = RCreateImageFromDrawable(icon->core->screen_ptr->rcontext,
443 hints->icon_pixmap,
444 (hints->flags & IconMaskHint)
445 ? hints->icon_mask : None);
447 return image;
451 * wIconStore--
452 * Stores the client supplied icon at CACHE_ICON_PATH
453 * and returns the path for that icon. Returns NULL if there is no
454 * client supplied icon or on failure.
456 * Side effects:
457 * New directories might be created.
459 char *wIconStore(WIcon * icon)
461 char *path, *dir_path, *file;
462 int len = 0;
463 RImage *image = NULL;
464 WWindow *wwin = icon->owner;
466 if (!wwin)
467 return NULL;
469 dir_path = get_icon_cache_path();
470 if (!dir_path)
471 return NULL;
473 file = get_name_for_wwin(wwin);
474 if (!file) {
475 wfree(dir_path);
476 return NULL;
479 len = strlen(dir_path) + strlen(file) + 5;
480 path = wmalloc(len);
481 snprintf(path, len, "%s%s.xpm", dir_path, file);
482 wfree(dir_path);
483 wfree(file);
485 /* If icon exists, exit */
486 if (access(path, F_OK) == 0)
487 return path;
489 if (wwin->net_icon_image)
490 image = RRetainImage(wwin->net_icon_image);
491 else
492 image = get_wwindow_image_from_wmhints(wwin, icon);
494 if (!image) {
495 wfree(path);
496 return NULL;
499 if (!RSaveImage(image, path, "XPM")) {
500 wfree(path);
501 path = NULL;
504 RReleaseImage(image);
506 return path;
509 static void cycleColor(void *data)
511 WIcon *icon = (WIcon *) data;
512 WScreen *scr = icon->core->screen_ptr;
513 XGCValues gcv;
515 icon->step--;
516 gcv.dash_offset = icon->step;
517 XChangeGC(dpy, scr->icon_select_gc, GCDashOffset, &gcv);
519 XDrawRectangle(dpy, icon->core->window, scr->icon_select_gc, 0, 0,
520 icon->core->width - 1, icon->core->height - 1);
521 icon->handlerID = WMAddTimerHandler(COLOR_CYCLE_DELAY, cycleColor, icon);
524 void wIconSetHighlited(WIcon *icon, Bool flag)
526 if (icon->highlighted == flag)
527 return;
529 icon->highlighted = flag;
530 update_icon_pixmap(icon);
533 void wIconSelect(WIcon * icon)
535 WScreen *scr = icon->core->screen_ptr;
536 icon->selected = !icon->selected;
538 if (icon->selected) {
539 icon->step = 0;
540 if (!wPreferences.dont_blink)
541 icon->handlerID = WMAddTimerHandler(10, cycleColor, icon);
542 else
543 XDrawRectangle(dpy, icon->core->window, scr->icon_select_gc, 0, 0,
544 icon->core->width - 1, icon->core->height - 1);
545 } else {
546 if (icon->handlerID) {
547 WMDeleteTimerHandler(icon->handlerID);
548 icon->handlerID = NULL;
550 XClearArea(dpy, icon->core->window, 0, 0, icon->core->width, icon->core->height, True);
554 static void unset_icon_image(WIcon *icon)
556 if (icon->file) {
557 wfree(icon->file);
558 icon->file = NULL;
561 if (icon->file_image) {
562 RReleaseImage(icon->file_image);
563 icon->file_image = NULL;
567 void wIconUpdate(WIcon *icon, RImage *image)
569 WWindow *wwin = NULL;
571 if (image) {
572 icon->file_image = image;
573 } else {
574 if (icon && icon->owner)
575 wwin = icon->owner;
577 if (wwin && WFLAGP(wwin, always_user_icon)) {
578 /* Forced use user_icon */
579 get_rimage_icon_from_user_icon(icon);
580 } else if (icon->icon_win != None) {
581 /* Get the Pixmap from the WIcon */
582 get_rimage_icon_from_icon_win(icon);
583 } else if (wwin && wwin->net_icon_image) {
584 /* Use _NET_WM_ICON icon */
585 get_rimage_icon_from_x11(icon);
586 } else if (wwin && wwin->wm_hints && (wwin->wm_hints->flags & IconPixmapHint)) {
587 /* Get the Pixmap from the wm_hints, else, from the user */
588 unset_icon_image(icon);
589 icon->file_image = get_rimage_icon_from_wm_hints(icon);
590 if (!icon->file_image)
591 get_rimage_icon_from_user_icon(icon);
592 } else {
593 /* Get the Pixmap from the user */
594 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 XClearWindow(dpy, icon->core->window);
625 wIconPaint(icon);
628 static void get_rimage_icon_from_x11(WIcon *icon)
630 /* Remove the icon image */
631 unset_icon_image(icon);
633 /* Set the new icon image */
634 icon->file_image = RRetainImage(icon->owner->net_icon_image);
637 static void get_rimage_icon_from_user_icon(WIcon *icon)
639 if (icon->file_image)
640 return;
642 get_rimage_icon_from_default_icon(icon);
645 static void get_rimage_icon_from_default_icon(WIcon *icon)
647 WScreen *scr = icon->core->screen_ptr;
649 /* If the icon don't have image, we should use the default image. */
650 if (!scr->def_icon_rimage)
651 scr->def_icon_rimage = get_default_image(scr);
653 /* Remove the icon image */
654 unset_icon_image(icon);
656 /* Set the new icon image */
657 icon->file_image = RRetainImage(scr->def_icon_rimage);
660 /* Get the RImage from the WIcon of the WWindow */
661 static void get_rimage_icon_from_icon_win(WIcon *icon)
663 RImage *image;
665 /* Create the new RImage */
666 image = get_window_image_from_x11(icon->icon_win);
668 /* Free the icon info */
669 unset_icon_image(icon);
671 /* Set the new info */
672 icon->file_image = image;
675 /* Set the dockapp in the WIcon */
676 static void set_dockapp_in_icon(WIcon *icon)
678 XWindowAttributes attr;
679 WScreen *scr = icon->core->screen_ptr;
680 int title_height = WMFontHeight(scr->icon_title_font);
681 unsigned int w, h, d;
682 int theight = 0;
684 /* Reparent the dock application to the icon */
686 /* We need the application size to center it
687 * and show in the correct position */
688 getSize(icon->icon_win, &w, &h, &d);
690 /* Set extra space for title */
691 if (icon->show_title && (h + title_height < wPreferences.icon_size)) {
692 theight = title_height;
693 drawIconTitle(scr, icon->pixmap, theight);
694 } else {
695 XSetWindowBackgroundPixmap(dpy, icon->core->window, scr->icon_tile_pixmap);
698 /* Set the icon border */
699 XSetWindowBorderWidth(dpy, icon->icon_win, 0);
701 /* Put the dock application in the icon */
702 XReparentWindow(dpy, icon->icon_win, icon->core->window,
703 (wPreferences.icon_size - w) / 2,
704 theight + (wPreferences.icon_size - h - theight) / 2);
706 /* Show it and save */
707 XMapWindow(dpy, icon->icon_win);
708 XAddToSaveSet(dpy, icon->icon_win);
710 /* Needed to move the icon clicking on the application part */
711 if ((XGetWindowAttributes(dpy, icon->icon_win, &attr)) &&
712 (attr.all_event_masks & ButtonPressMask))
713 wHackedGrabButton(Button1, MOD_MASK, icon->core->window, True,
714 ButtonPressMask, GrabModeSync, GrabModeAsync,
715 None, wCursor[WCUR_ARROW]);
718 /* Get the RImage from the XWindow wm_hints */
719 RImage *get_rimage_icon_from_wm_hints(WIcon *icon)
721 RImage *image = NULL;
722 unsigned int w, h, d;
723 WWindow *wwin = icon->owner;
725 if (!getSize(wwin->wm_hints->icon_pixmap, &w, &h, &d)) {
726 icon->owner->wm_hints->flags &= ~IconPixmapHint;
727 return NULL;
730 image = get_wwindow_image_from_wmhints(wwin, icon);
731 if (!image)
732 return NULL;
734 /* Resize the icon to the wPreferences.icon_size size */
735 image = wIconValidateIconSize(image, wPreferences.icon_size);
737 return image;
740 void wIconPaint(WIcon *icon)
742 WScreen *scr = icon->core->screen_ptr;
743 int x, l, w;
744 char *tmp;
746 XClearWindow(dpy, icon->core->window);
748 /* draw the icon title */
749 if (icon->show_title && icon->icon_name != NULL) {
750 tmp = ShrinkString(scr->icon_title_font, icon->icon_name, wPreferences.icon_size - 4);
751 w = WMWidthOfString(scr->icon_title_font, tmp, l = strlen(tmp));
753 if (w > icon->core->width - 4)
754 x = (icon->core->width - 4) - w;
755 else
756 x = (icon->core->width - w) / 2;
758 WMDrawString(scr->wmscreen, icon->core->window, scr->icon_title_color,
759 scr->icon_title_font, x, 1, tmp, l);
760 wfree(tmp);
763 if (icon->selected)
764 XDrawRectangle(dpy, icon->core->window, scr->icon_select_gc, 0, 0,
765 icon->core->width - 1, icon->core->height - 1);
768 /******************************************************************/
770 static void miniwindowExpose(WObjDescriptor * desc, XEvent * event)
772 wIconPaint(desc->parent);
775 static void miniwindowDblClick(WObjDescriptor * desc, XEvent * event)
777 WIcon *icon = desc->parent;
779 assert(icon->owner != NULL);
781 wDeiconifyWindow(icon->owner);
784 static void miniwindowMouseDown(WObjDescriptor * desc, XEvent * event)
786 WIcon *icon = desc->parent;
787 WWindow *wwin = icon->owner;
788 XEvent ev;
789 int x = wwin->icon_x, y = wwin->icon_y;
790 int dx = event->xbutton.x, dy = event->xbutton.y;
791 int grabbed = 0;
792 int clickButton = event->xbutton.button;
793 Bool hasMoved = False;
795 if (WCHECK_STATE(WSTATE_MODAL))
796 return;
798 if (IsDoubleClick(icon->core->screen_ptr, event)) {
799 miniwindowDblClick(desc, event);
800 return;
803 if (event->xbutton.button == Button1) {
804 if (event->xbutton.state & MOD_MASK)
805 wLowerFrame(icon->core);
806 else
807 wRaiseFrame(icon->core);
808 if (event->xbutton.state & ShiftMask) {
809 wIconSelect(icon);
810 wSelectWindow(icon->owner, !wwin->flags.selected);
812 } else if (event->xbutton.button == Button3) {
813 WObjDescriptor *desc;
815 OpenMiniwindowMenu(wwin, event->xbutton.x_root, event->xbutton.y_root);
817 /* allow drag select of menu */
818 desc = &wwin->screen_ptr->window_menu->menu->descriptor;
819 event->xbutton.send_event = True;
820 (*desc->handle_mousedown) (desc, event);
822 return;
825 if (XGrabPointer(dpy, icon->core->window, False, ButtonMotionMask
826 | ButtonReleaseMask | ButtonPressMask, GrabModeAsync,
827 GrabModeAsync, None, None, CurrentTime) != GrabSuccess) {
829 while (1) {
830 WMMaskEvent(dpy, PointerMotionMask | ButtonReleaseMask | ButtonPressMask
831 | ButtonMotionMask | ExposureMask, &ev);
832 switch (ev.type) {
833 case Expose:
834 WMHandleEvent(&ev);
835 break;
837 case MotionNotify:
838 hasMoved = True;
839 if (!grabbed) {
840 if (abs(dx - ev.xmotion.x) >= MOVE_THRESHOLD
841 || abs(dy - ev.xmotion.y) >= MOVE_THRESHOLD) {
842 XChangeActivePointerGrab(dpy, ButtonMotionMask
843 | ButtonReleaseMask | ButtonPressMask,
844 wCursor[WCUR_MOVE], CurrentTime);
845 grabbed = 1;
846 } else {
847 break;
850 x = ev.xmotion.x_root - dx;
851 y = ev.xmotion.y_root - dy;
852 XMoveWindow(dpy, icon->core->window, x, y);
853 break;
855 case ButtonPress:
856 break;
858 case ButtonRelease:
859 if (ev.xbutton.button != clickButton)
860 break;
862 if (wwin->icon_x != x || wwin->icon_y != y)
863 wwin->flags.icon_moved = 1;
865 XMoveWindow(dpy, icon->core->window, x, y);
867 wwin->icon_x = x;
868 wwin->icon_y = y;
869 XUngrabPointer(dpy, CurrentTime);
871 if (wPreferences.auto_arrange_icons)
872 wArrangeIcons(wwin->screen_ptr, True);
873 if (wPreferences.single_click && !hasMoved)
874 miniwindowDblClick(desc, event);
875 return;
881 void set_icon_image_from_database(WIcon *icon, char *wm_instance, char *wm_class, char *command)
883 char *file = NULL;
885 file = get_icon_filename(icon->core->screen_ptr, wm_instance, wm_class, command, False);
886 if (file) {
887 icon->file = wstrdup(file);
888 icon->file_image = get_rimage_from_file(icon->core->screen_ptr, icon->file, wPreferences.icon_size);
889 wfree(file);
892 wIconUpdate(icon, NULL);