Code refactoring: replaced macro 'HAVE_XRANDR' by 'USE_XRANDR' for consistency
[wmaker-crm.git] / src / icon.c
blobd297cbc59c1915da47468927a462c48c355077f4
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 "stacking.h"
42 #include "application.h"
43 #include "defaults.h"
44 #include "appicon.h"
45 #include "wmspec.h"
46 #include "misc.h"
47 #include "startup.h"
48 #include "event.h"
49 #include "winmenu.h"
51 /**** Global varianebles ****/
53 #define MOD_MASK wPreferences.modifier_mask
54 #define CACHE_ICON_PATH "/Library/WindowMaker/CachedPixmaps"
55 #define ICON_BORDER 3
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 /* Parameter not used, but tell the compiler that it is ok */
97 (void) notif;
99 update_icon_pixmap(icon);
101 XClearArea(dpy, icon->core->window, 0, 0, 1, 1, True);
104 /************************************/
106 static int getSize(Drawable d, unsigned int *w, unsigned int *h, unsigned int *dep)
108 Window rjunk;
109 int xjunk, yjunk;
110 unsigned int bjunk;
112 return XGetGeometry(dpy, d, &rjunk, &xjunk, &yjunk, w, h, &bjunk, dep);
115 WIcon *icon_create_for_wwindow(WWindow *wwin)
117 WScreen *scr = wwin->screen_ptr;
118 WIcon *icon;
120 icon = icon_create_core(scr, wwin->icon_x, wwin->icon_y);
122 icon->owner = wwin;
123 if (wwin->wm_hints && (wwin->wm_hints->flags & IconWindowHint)) {
124 if (wwin->client_win == wwin->main_window) {
125 WApplication *wapp;
126 /* do not let miniwindow steal app-icon's icon window */
127 wapp = wApplicationOf(wwin->client_win);
128 if (!wapp || wapp->app_icon == NULL)
129 icon->icon_win = wwin->wm_hints->icon_window;
130 } else {
131 icon->icon_win = wwin->wm_hints->icon_window;
134 #ifdef NO_MINIWINDOW_TITLES
135 icon->show_title = 0;
136 #else
137 icon->show_title = 1;
138 #endif
140 wIconChangeTitle(icon, wwin);
141 icon->tile_type = TILE_NORMAL;
143 set_icon_image_from_database(icon, wwin->wm_instance, wwin->wm_class, NULL);
144 /* Update the icon, because icon could be NULL */
145 wIconUpdate(icon);
147 WMAddNotificationObserver(appearanceObserver, icon, WNIconAppearanceSettingsChanged, icon);
148 WMAddNotificationObserver(tileObserver, icon, WNIconTileSettingsChanged, icon);
150 return icon;
153 WIcon *icon_create_for_dock(WScreen *scr, const char *command, const char *wm_instance, const 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);
161 /* Update the icon, because icon could be NULL */
162 wIconUpdate(icon);
164 WMAddNotificationObserver(appearanceObserver, icon, WNIconAppearanceSettingsChanged, icon);
165 WMAddNotificationObserver(tileObserver, icon, WNIconTileSettingsChanged, icon);
167 return icon;
170 static WIcon *icon_create_core(WScreen *scr, int coord_x, int coord_y)
172 WIcon *icon;
174 icon = wmalloc(sizeof(WIcon));
175 icon->core = wCoreCreateTopLevel(scr,
176 coord_x,
177 coord_y,
178 wPreferences.icon_size,
179 wPreferences.icon_size,
180 0, scr->w_depth, scr->w_visual, scr->w_colormap,
181 scr->white_pixel);
183 /* will be overriden if this is a application icon */
184 icon->core->descriptor.handle_mousedown = miniwindowMouseDown;
185 icon->core->descriptor.handle_expose = miniwindowExpose;
186 icon->core->descriptor.parent_type = WCLASS_MINIWINDOW;
187 icon->core->descriptor.parent = icon;
189 icon->core->stacking = wmalloc(sizeof(WStacking));
190 icon->core->stacking->above = NULL;
191 icon->core->stacking->under = NULL;
192 icon->core->stacking->window_level = NORMAL_ICON_LEVEL;
193 icon->core->stacking->child_of = NULL;
195 /* Icon image */
196 icon->file = NULL;
197 icon->file_image = NULL;
199 return icon;
202 void wIconDestroy(WIcon * icon)
204 WCoreWindow *core = icon->core;
205 WScreen *scr = core->screen_ptr;
207 WMRemoveNotificationObserver(icon);
209 if (icon->handlerID)
210 WMDeleteTimerHandler(icon->handlerID);
212 if (icon->icon_win) {
213 int x = 0, y = 0;
215 if (icon->owner) {
216 x = icon->owner->icon_x;
217 y = icon->owner->icon_y;
219 XUnmapWindow(dpy, icon->icon_win);
220 XReparentWindow(dpy, icon->icon_win, scr->root_win, x, y);
222 if (icon->icon_name)
223 XFree(icon->icon_name);
225 if (icon->pixmap)
226 XFreePixmap(dpy, icon->pixmap);
228 unset_icon_image(icon);
230 wCoreDestroy(icon->core);
231 wfree(icon);
234 static void drawIconTitleBackground(WScreen *scr, Pixmap pixmap, int height)
236 XFillRectangle(dpy, pixmap, scr->icon_title_texture->normal_gc, 0, 0, wPreferences.icon_size, height + 1);
237 XDrawLine(dpy, pixmap, scr->icon_title_texture->light_gc, 0, 0, wPreferences.icon_size, 0);
238 XDrawLine(dpy, pixmap, scr->icon_title_texture->light_gc, 0, 0, 0, height + 1);
239 XDrawLine(dpy, pixmap, scr->icon_title_texture->dim_gc,
240 wPreferences.icon_size - 1, 0, wPreferences.icon_size - 1, height + 1);
243 static void icon_update_pixmap(WIcon *icon, RImage *image)
245 RImage *tile;
246 Pixmap pixmap;
247 int x, y, sx, sy;
248 unsigned w, h;
249 int theight = 0;
250 WScreen *scr = icon->core->screen_ptr;
252 switch (icon->tile_type) {
253 case TILE_NORMAL:
254 tile = RCloneImage(scr->icon_tile);
255 break;
256 case TILE_CLIP:
257 tile = RCloneImage(scr->clip_tile);
258 break;
259 case TILE_DRAWER:
260 tile = RCloneImage(scr->drawer_tile);
261 break;
262 default:
264 * The icon has always rigth value, this case is
265 * only to avoid a compiler warning with "tile"
266 * "may be used uninitialized"
268 wwarning("Unknown tile type: %d.\n", icon->tile_type);
269 tile = RCloneImage(scr->icon_tile);
272 if (image) {
273 w = (image->width > wPreferences.icon_size)
274 ? wPreferences.icon_size : image->width;
275 x = (wPreferences.icon_size - w) / 2;
276 sx = (image->width - w) / 2;
278 if (icon->show_title)
279 theight = WMFontHeight(scr->icon_title_font);
281 h = (image->height + theight > wPreferences.icon_size
282 ? wPreferences.icon_size - theight : image->height);
283 y = theight + (wPreferences.icon_size - theight - h) / 2;
284 sy = (image->height - h) / 2;
286 RCombineArea(tile, image, sx, sy, w, h, x, y);
289 if (icon->shadowed) {
290 RColor color;
292 color.red = scr->icon_back_texture->light.red >> 8;
293 color.green = scr->icon_back_texture->light.green >> 8;
294 color.blue = scr->icon_back_texture->light.blue >> 8;
295 color.alpha = 150; /* about 60% */
296 RClearImage(tile, &color);
299 if (icon->highlighted) {
300 RColor color;
302 color.red = color.green = color.blue = 0;
303 color.alpha = 160;
304 RLightImage(tile, &color);
307 if (!RConvertImage(scr->rcontext, tile, &pixmap))
308 wwarning(_("error rendering image:%s"), RMessageForError(RErrorCode));
310 RReleaseImage(tile);
312 /* Draw the icon's title background (without text) */
313 if (icon->show_title)
314 drawIconTitleBackground(scr, pixmap, theight);
316 icon->pixmap = pixmap;
319 void wIconChangeTitle(WIcon *icon, WWindow *wwin)
321 if (!icon || !wwin)
322 return;
324 /* Remove the previous icon title */
325 if (icon->icon_name != NULL)
326 XFree(icon->icon_name);
328 /* Set the new one, using two methods */
329 icon->icon_name = wNETWMGetIconName(wwin->client_win);
330 if (!icon->icon_name)
331 wGetIconName(dpy, wwin->client_win, &icon->icon_name);
334 RImage *wIconValidateIconSize(RImage *icon, int max_size)
336 RImage *nimage;
338 if (!icon)
339 return NULL;
341 /* We should hold "ICON_BORDER" (~2) pixels to include the icon border */
342 if (((max_size + ICON_BORDER) < icon->width) ||
343 ((max_size + ICON_BORDER) < icon->height)) {
344 if (icon->width > icon->height)
345 nimage = RScaleImage(icon, max_size - ICON_BORDER,
346 (icon->height * (max_size - ICON_BORDER) / icon->width));
347 else
348 nimage = RScaleImage(icon, (icon->width * (max_size - ICON_BORDER) / icon->height),
349 max_size - ICON_BORDER);
350 RReleaseImage(icon);
351 icon = nimage;
354 return icon;
357 int wIconChangeImageFile(WIcon *icon, const char *file)
359 WScreen *scr = icon->core->screen_ptr;
360 char *path;
361 RImage *image = NULL;
363 /* If no new image, don't do nothing */
364 if (!file)
365 return 1;
367 /* Find the new image */
368 path = FindImage(wPreferences.icon_path, file);
369 if (!path)
370 return 0;
372 image = get_rimage_from_file(scr, path, wPreferences.icon_size);
373 if (!image) {
374 wfree(path);
375 return 0;
378 /* Set the new image */
379 set_icon_image_from_image(icon, image);
380 icon->file = wstrdup(path);
381 update_icon_pixmap(icon);
383 wfree(path);
384 return 1;
387 static char *get_name_for_wwin(WWindow *wwin)
389 return get_name_for_instance_class(wwin->wm_instance, wwin->wm_class);
392 char *get_name_for_instance_class(const char *wm_instance, const char *wm_class)
394 char *suffix;
395 int len;
397 if (wm_class && wm_instance) {
398 len = strlen(wm_class) + strlen(wm_instance) + 2;
399 suffix = wmalloc(len);
400 snprintf(suffix, len, "%s.%s", wm_instance, wm_class);
401 } else if (wm_class) {
402 len = strlen(wm_class) + 1;
403 suffix = wmalloc(len);
404 snprintf(suffix, len, "%s", wm_class);
405 } else if (wm_instance) {
406 len = strlen(wm_instance) + 1;
407 suffix = wmalloc(len);
408 snprintf(suffix, len, "%s", wm_instance);
409 } else {
410 return NULL;
413 return suffix;
416 static char *get_icon_cache_path(void)
418 const char *prefix;
419 char *path;
420 int len, ret;
422 prefix = wusergnusteppath();
423 len = strlen(prefix) + strlen(CACHE_ICON_PATH) + 2;
424 path = wmalloc(len);
425 snprintf(path, len, "%s%s/", prefix, CACHE_ICON_PATH);
427 /* If the folder exists, exit */
428 if (access(path, F_OK) == 0)
429 return path;
431 /* Create the folder */
432 ret = wmkdirhier((const char *) path);
434 /* Exit 1 on success, 0 on failure */
435 if (ret == 1)
436 return path;
438 /* Fail */
439 wfree(path);
440 return NULL;
443 static RImage *get_wwindow_image_from_wmhints(WWindow *wwin, WIcon *icon)
445 RImage *image = NULL;
446 XWMHints *hints = wwin->wm_hints;
448 if (hints && (hints->flags & IconPixmapHint) && hints->icon_pixmap != None)
449 image = RCreateImageFromDrawable(icon->core->screen_ptr->rcontext,
450 hints->icon_pixmap,
451 (hints->flags & IconMaskHint)
452 ? hints->icon_mask : None);
454 return image;
458 * wIconStore--
459 * Stores the client supplied icon at CACHE_ICON_PATH
460 * and returns the path for that icon. Returns NULL if there is no
461 * client supplied icon or on failure.
463 * Side effects:
464 * New directories might be created.
466 char *wIconStore(WIcon * icon)
468 char *path, *dir_path, *file;
469 int len = 0;
470 RImage *image = NULL;
471 WWindow *wwin = icon->owner;
473 if (!wwin)
474 return NULL;
476 dir_path = get_icon_cache_path();
477 if (!dir_path)
478 return NULL;
480 file = get_name_for_wwin(wwin);
481 if (!file) {
482 wfree(dir_path);
483 return NULL;
486 len = strlen(dir_path) + strlen(file) + 5;
487 path = wmalloc(len);
488 snprintf(path, len, "%s%s.xpm", dir_path, file);
489 wfree(dir_path);
490 wfree(file);
492 /* If icon exists, exit */
493 if (access(path, F_OK) == 0)
494 return path;
496 if (wwin->net_icon_image)
497 image = RRetainImage(wwin->net_icon_image);
498 else
499 image = get_wwindow_image_from_wmhints(wwin, icon);
501 if (!image) {
502 wfree(path);
503 return NULL;
506 if (!RSaveImage(image, path, "XPM")) {
507 wfree(path);
508 path = NULL;
511 RReleaseImage(image);
513 return path;
516 static void cycleColor(void *data)
518 WIcon *icon = (WIcon *) data;
519 WScreen *scr = icon->core->screen_ptr;
520 XGCValues gcv;
522 icon->step--;
523 gcv.dash_offset = icon->step;
524 XChangeGC(dpy, scr->icon_select_gc, GCDashOffset, &gcv);
526 XDrawRectangle(dpy, icon->core->window, scr->icon_select_gc, 0, 0,
527 icon->core->width - 1, icon->core->height - 1);
528 icon->handlerID = WMAddTimerHandler(COLOR_CYCLE_DELAY, cycleColor, icon);
531 void wIconSetHighlited(WIcon *icon, Bool flag)
533 if (icon->highlighted == flag)
534 return;
536 icon->highlighted = flag;
537 update_icon_pixmap(icon);
540 void wIconSelect(WIcon * icon)
542 WScreen *scr = icon->core->screen_ptr;
543 icon->selected = !icon->selected;
545 if (icon->selected) {
546 icon->step = 0;
547 if (!wPreferences.dont_blink)
548 icon->handlerID = WMAddTimerHandler(10, cycleColor, icon);
549 else
550 XDrawRectangle(dpy, icon->core->window, scr->icon_select_gc, 0, 0,
551 icon->core->width - 1, icon->core->height - 1);
552 } else {
553 if (icon->handlerID) {
554 WMDeleteTimerHandler(icon->handlerID);
555 icon->handlerID = NULL;
557 XClearArea(dpy, icon->core->window, 0, 0, icon->core->width, icon->core->height, True);
561 static void unset_icon_image(WIcon *icon)
563 if (icon->file) {
564 wfree(icon->file);
565 icon->file = NULL;
568 if (icon->file_image) {
569 RReleaseImage(icon->file_image);
570 icon->file_image = NULL;
574 void set_icon_image_from_image(WIcon *icon, RImage *image)
576 if (!icon)
577 return;
579 unset_icon_image(icon);
581 icon->file_image = NULL;
582 icon->file_image = image;
585 void wIconUpdate(WIcon *icon)
587 WWindow *wwin = NULL;
589 if (icon && icon->owner)
590 wwin = icon->owner;
592 if (wwin && WFLAGP(wwin, always_user_icon)) {
593 /* Forced use user_icon */
594 get_rimage_icon_from_user_icon(icon);
595 } else if (icon->icon_win != None) {
596 /* Get the Pixmap from the WIcon */
597 get_rimage_icon_from_icon_win(icon);
598 } else if (wwin && wwin->net_icon_image) {
599 /* Use _NET_WM_ICON icon */
600 get_rimage_icon_from_x11(icon);
601 } else if (wwin && wwin->wm_hints && (wwin->wm_hints->flags & IconPixmapHint)) {
602 /* Get the Pixmap from the wm_hints, else, from the user */
603 unset_icon_image(icon);
604 icon->file_image = get_rimage_icon_from_wm_hints(icon);
605 if (!icon->file_image)
606 get_rimage_icon_from_user_icon(icon);
607 } else {
608 /* Get the Pixmap from the user */
609 get_rimage_icon_from_user_icon(icon);
612 update_icon_pixmap(icon);
615 void update_icon_pixmap(WIcon *icon)
617 if (icon->pixmap != None)
618 XFreePixmap(dpy, icon->pixmap);
620 icon->pixmap = None;
622 /* Create the pixmap */
623 if (icon->file_image)
624 icon_update_pixmap(icon, icon->file_image);
626 /* If dockapp, put inside the icon */
627 if (icon->icon_win != None) {
628 /* file_image is NULL, because is docked app */
629 icon_update_pixmap(icon, NULL);
630 set_dockapp_in_icon(icon);
633 /* No pixmap, set default background */
634 if (icon->pixmap != None)
635 XSetWindowBackgroundPixmap(dpy, icon->core->window, icon->pixmap);
637 /* Paint it */
638 wIconPaint(icon);
641 static void get_rimage_icon_from_x11(WIcon *icon)
643 /* Remove the icon image */
644 unset_icon_image(icon);
646 /* Set the new icon image */
647 icon->file_image = RRetainImage(icon->owner->net_icon_image);
650 static void get_rimage_icon_from_user_icon(WIcon *icon)
652 if (icon->file_image)
653 return;
655 get_rimage_icon_from_default_icon(icon);
658 static void get_rimage_icon_from_default_icon(WIcon *icon)
660 WScreen *scr = icon->core->screen_ptr;
662 /* If the icon don't have image, we should use the default image. */
663 if (!scr->def_icon_rimage)
664 scr->def_icon_rimage = get_default_image(scr);
666 /* Remove the icon image */
667 unset_icon_image(icon);
669 /* Set the new icon image */
670 icon->file_image = RRetainImage(scr->def_icon_rimage);
673 /* Get the RImage from the WIcon of the WWindow */
674 static void get_rimage_icon_from_icon_win(WIcon *icon)
676 RImage *image;
678 /* Create the new RImage */
679 image = get_window_image_from_x11(icon->icon_win);
681 /* Free the icon info */
682 unset_icon_image(icon);
684 /* Set the new info */
685 icon->file_image = image;
688 /* Set the dockapp in the WIcon */
689 static void set_dockapp_in_icon(WIcon *icon)
691 XWindowAttributes attr;
692 WScreen *scr = icon->core->screen_ptr;
693 unsigned int w, h, d;
695 /* Reparent the dock application to the icon */
697 /* We need the application size to center it
698 * and show in the correct position */
699 getSize(icon->icon_win, &w, &h, &d);
701 /* Set the background pixmap */
702 XSetWindowBackgroundPixmap(dpy, icon->core->window, scr->icon_tile_pixmap);
704 /* Set the icon border */
705 XSetWindowBorderWidth(dpy, icon->icon_win, 0);
707 /* Put the dock application in the icon */
708 XReparentWindow(dpy, icon->icon_win, icon->core->window,
709 (wPreferences.icon_size - w) / 2,
710 (wPreferences.icon_size - h) / 2);
712 /* Show it and save */
713 XMapWindow(dpy, icon->icon_win);
714 XAddToSaveSet(dpy, icon->icon_win);
716 /* Needed to move the icon clicking on the application part */
717 if ((XGetWindowAttributes(dpy, icon->icon_win, &attr)) &&
718 (attr.all_event_masks & ButtonPressMask))
719 wHackedGrabButton(Button1, MOD_MASK, icon->core->window, True,
720 ButtonPressMask, GrabModeSync, GrabModeAsync,
721 None, wPreferences.cursor[WCUR_ARROW]);
724 /* Get the RImage from the XWindow wm_hints */
725 RImage *get_rimage_icon_from_wm_hints(WIcon *icon)
727 RImage *image = NULL;
728 unsigned int w, h, d;
729 WWindow *wwin = icon->owner;
731 if (!getSize(wwin->wm_hints->icon_pixmap, &w, &h, &d)) {
732 icon->owner->wm_hints->flags &= ~IconPixmapHint;
733 return NULL;
736 image = get_wwindow_image_from_wmhints(wwin, icon);
737 if (!image)
738 return NULL;
740 /* Resize the icon to the wPreferences.icon_size size */
741 image = wIconValidateIconSize(image, wPreferences.icon_size);
743 return image;
746 /* This function updates in the screen the icon title */
747 static void update_icon_title(WIcon *icon)
749 WScreen *scr = icon->core->screen_ptr;
750 int x, l, w;
751 char *tmp;
753 /* draw the icon title */
754 if (icon->show_title && icon->icon_name != NULL) {
755 tmp = ShrinkString(scr->icon_title_font, icon->icon_name, wPreferences.icon_size - 4);
756 w = WMWidthOfString(scr->icon_title_font, tmp, l = strlen(tmp));
758 if (w > icon->core->width - 4)
759 x = (icon->core->width - 4) - w;
760 else
761 x = (icon->core->width - w) / 2;
763 WMDrawString(scr->wmscreen, icon->core->window, scr->icon_title_color,
764 scr->icon_title_font, x, 1, tmp, l);
765 wfree(tmp);
770 void wIconPaint(WIcon *icon)
772 if (!icon || !icon->core || !icon->core->screen_ptr)
773 return;
775 WScreen *scr = icon->core->screen_ptr;
777 XClearWindow(dpy, icon->core->window);
779 update_icon_title(icon);
781 if (icon->selected)
782 XDrawRectangle(dpy, icon->core->window, scr->icon_select_gc, 0, 0,
783 icon->core->width - 1, icon->core->height - 1);
786 /******************************************************************/
788 static void miniwindowExpose(WObjDescriptor * desc, XEvent * event)
790 /* Parameter not used, but tell the compiler that it is ok */
791 (void) event;
793 wIconPaint(desc->parent);
796 static void miniwindowDblClick(WObjDescriptor * desc, XEvent * event)
798 WIcon *icon = desc->parent;
800 /* Parameter not used, but tell the compiler that it is ok */
801 (void) event;
803 assert(icon->owner != NULL);
805 wDeiconifyWindow(icon->owner);
808 static void miniwindowMouseDown(WObjDescriptor * desc, XEvent * event)
810 WIcon *icon = desc->parent;
811 WWindow *wwin = icon->owner;
812 XEvent ev;
813 int x = wwin->icon_x, y = wwin->icon_y;
814 int dx = event->xbutton.x, dy = event->xbutton.y;
815 int grabbed = 0;
816 int clickButton = event->xbutton.button;
817 Bool hasMoved = False;
819 if (WCHECK_STATE(WSTATE_MODAL))
820 return;
822 if (IsDoubleClick(icon->core->screen_ptr, event)) {
823 miniwindowDblClick(desc, event);
824 return;
827 if (event->xbutton.button == Button1) {
828 if (event->xbutton.state & MOD_MASK)
829 wLowerFrame(icon->core);
830 else
831 wRaiseFrame(icon->core);
832 if (event->xbutton.state & ShiftMask) {
833 wIconSelect(icon);
834 wSelectWindow(icon->owner, !wwin->flags.selected);
836 } else if (event->xbutton.button == Button3) {
837 WObjDescriptor *desc;
839 OpenMiniwindowMenu(wwin, event->xbutton.x_root, event->xbutton.y_root);
841 /* allow drag select of menu */
842 desc = &wwin->screen_ptr->window_menu->menu->descriptor;
843 event->xbutton.send_event = True;
844 (*desc->handle_mousedown) (desc, event);
846 return;
849 if (XGrabPointer(dpy, icon->core->window, False, ButtonMotionMask
850 | ButtonReleaseMask | ButtonPressMask, GrabModeAsync,
851 GrabModeAsync, None, None, CurrentTime) != GrabSuccess) {
853 while (1) {
854 WMMaskEvent(dpy, PointerMotionMask | ButtonReleaseMask | ButtonPressMask
855 | ButtonMotionMask | ExposureMask, &ev);
856 switch (ev.type) {
857 case Expose:
858 WMHandleEvent(&ev);
859 break;
861 case MotionNotify:
862 hasMoved = True;
863 if (!grabbed) {
864 if (abs(dx - ev.xmotion.x) >= MOVE_THRESHOLD
865 || abs(dy - ev.xmotion.y) >= MOVE_THRESHOLD) {
866 XChangeActivePointerGrab(dpy, ButtonMotionMask
867 | ButtonReleaseMask | ButtonPressMask,
868 wPreferences.cursor[WCUR_MOVE], CurrentTime);
869 grabbed = 1;
870 } else {
871 break;
874 x = ev.xmotion.x_root - dx;
875 y = ev.xmotion.y_root - dy;
876 XMoveWindow(dpy, icon->core->window, x, y);
877 break;
879 case ButtonPress:
880 break;
882 case ButtonRelease:
883 if (ev.xbutton.button != clickButton)
884 break;
886 if (wwin->icon_x != x || wwin->icon_y != y)
887 wwin->flags.icon_moved = 1;
889 XMoveWindow(dpy, icon->core->window, x, y);
891 wwin->icon_x = x;
892 wwin->icon_y = y;
893 XUngrabPointer(dpy, CurrentTime);
895 if (wPreferences.auto_arrange_icons)
896 wArrangeIcons(wwin->screen_ptr, True);
897 if (wPreferences.single_click && !hasMoved)
898 miniwindowDblClick(desc, event);
899 return;
905 void set_icon_image_from_database(WIcon *icon, const char *wm_instance, const char *wm_class, const char *command)
907 char *file = NULL;
909 file = get_icon_filename(wm_instance, wm_class, command, False);
910 if (file) {
911 icon->file = wstrdup(file);
912 icon->file_image = get_rimage_from_file(icon->core->screen_ptr, icon->file, wPreferences.icon_size);
913 wfree(file);