wmaker: Replaced local 'extern' definition of wPreferences by proper header usage
[wmaker-crm.git] / src / icon.c
blob3e065853c497adeeeb96adc55215bd7149b6019b
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 extern Cursor wCursor[WCUR_LAST];
59 static void miniwindowExpose(WObjDescriptor * desc, XEvent * event);
60 static void miniwindowMouseDown(WObjDescriptor * desc, XEvent * event);
61 static void miniwindowDblClick(WObjDescriptor * desc, XEvent * event);
63 static WIcon *icon_create_core(WScreen *scr, int coord_x, int coord_y);
65 static void set_dockapp_in_icon(WIcon *icon);
66 static void get_rimage_icon_from_icon_win(WIcon *icon);
67 static void get_rimage_icon_from_user_icon(WIcon *icon);
68 static void get_rimage_icon_from_default_icon(WIcon *icon);
69 static void get_rimage_icon_from_x11(WIcon *icon);
71 static void icon_update_pixmap(WIcon *icon, RImage *image);
72 static void unset_icon_image(WIcon *icon);
74 /****** Notification Observers ******/
76 static void appearanceObserver(void *self, WMNotification *notif)
78 WIcon *icon = (WIcon *) self;
79 uintptr_t flags = (uintptr_t)WMGetNotificationClientData(notif);
81 if ((flags & WTextureSettings) || (flags & WFontSettings)) {
82 /* If the rimage exists, update the icon, else create it */
83 if (icon->file_image)
84 update_icon_pixmap(icon);
85 else
86 wIconPaint(icon);
89 /* so that the appicon expose handlers will paint the appicon specific
90 * stuff */
91 XClearArea(dpy, icon->core->window, 0, 0, icon->core->width, icon->core->height, True);
94 static void tileObserver(void *self, WMNotification *notif)
96 WIcon *icon = (WIcon *) self;
98 update_icon_pixmap(icon);
100 XClearArea(dpy, icon->core->window, 0, 0, 1, 1, True);
103 /************************************/
105 static int getSize(Drawable d, unsigned int *w, unsigned int *h, unsigned int *dep)
107 Window rjunk;
108 int xjunk, yjunk;
109 unsigned int bjunk;
111 return XGetGeometry(dpy, d, &rjunk, &xjunk, &yjunk, w, h, &bjunk, dep);
114 WIcon *icon_create_for_wwindow(WWindow *wwin)
116 WScreen *scr = wwin->screen_ptr;
117 WIcon *icon;
119 icon = icon_create_core(scr, wwin->icon_x, wwin->icon_y);
121 icon->owner = wwin;
122 if (wwin->wm_hints && (wwin->wm_hints->flags & IconWindowHint)) {
123 if (wwin->client_win == wwin->main_window) {
124 WApplication *wapp;
125 /* do not let miniwindow steal app-icon's icon window */
126 wapp = wApplicationOf(wwin->client_win);
127 if (!wapp || wapp->app_icon == NULL)
128 icon->icon_win = wwin->wm_hints->icon_window;
129 } else {
130 icon->icon_win = wwin->wm_hints->icon_window;
133 #ifdef NO_MINIWINDOW_TITLES
134 icon->show_title = 0;
135 #else
136 icon->show_title = 1;
137 #endif
139 wIconChangeTitle(icon, wwin);
140 icon->tile_type = TILE_NORMAL;
142 set_icon_image_from_database(icon, wwin->wm_instance, wwin->wm_class, NULL);
143 /* Update the icon, because icon could be NULL */
144 wIconUpdate(icon);
146 WMAddNotificationObserver(appearanceObserver, icon, WNIconAppearanceSettingsChanged, icon);
147 WMAddNotificationObserver(tileObserver, icon, WNIconTileSettingsChanged, icon);
149 return icon;
152 WIcon *icon_create_for_dock(WScreen *scr, const char *command, const char *wm_instance, const char *wm_class, int tile)
154 WIcon *icon;
156 icon = icon_create_core(scr, 0, 0);
157 icon->tile_type = tile;
159 set_icon_image_from_database(icon, wm_instance, wm_class, command);
160 /* Update the icon, because icon could be NULL */
161 wIconUpdate(icon);
163 WMAddNotificationObserver(appearanceObserver, icon, WNIconAppearanceSettingsChanged, icon);
164 WMAddNotificationObserver(tileObserver, icon, WNIconTileSettingsChanged, icon);
166 return icon;
169 static WIcon *icon_create_core(WScreen *scr, int coord_x, int coord_y)
171 WIcon *icon;
173 icon = wmalloc(sizeof(WIcon));
174 icon->core = wCoreCreateTopLevel(scr,
175 coord_x,
176 coord_y,
177 wPreferences.icon_size,
178 wPreferences.icon_size,
179 0, scr->w_depth, scr->w_visual, scr->w_colormap,
180 scr->white_pixel);
182 /* will be overriden if this is a application icon */
183 icon->core->descriptor.handle_mousedown = miniwindowMouseDown;
184 icon->core->descriptor.handle_expose = miniwindowExpose;
185 icon->core->descriptor.parent_type = WCLASS_MINIWINDOW;
186 icon->core->descriptor.parent = icon;
188 icon->core->stacking = wmalloc(sizeof(WStacking));
189 icon->core->stacking->above = NULL;
190 icon->core->stacking->under = NULL;
191 icon->core->stacking->window_level = NORMAL_ICON_LEVEL;
192 icon->core->stacking->child_of = NULL;
194 /* Icon image */
195 icon->file = NULL;
196 icon->file_image = NULL;
198 return icon;
201 void wIconDestroy(WIcon * icon)
203 WCoreWindow *core = icon->core;
204 WScreen *scr = core->screen_ptr;
206 WMRemoveNotificationObserver(icon);
208 if (icon->handlerID)
209 WMDeleteTimerHandler(icon->handlerID);
211 if (icon->icon_win) {
212 int x = 0, y = 0;
214 if (icon->owner) {
215 x = icon->owner->icon_x;
216 y = icon->owner->icon_y;
218 XUnmapWindow(dpy, icon->icon_win);
219 XReparentWindow(dpy, icon->icon_win, scr->root_win, x, y);
221 if (icon->icon_name)
222 XFree(icon->icon_name);
224 if (icon->pixmap)
225 XFreePixmap(dpy, icon->pixmap);
227 unset_icon_image(icon);
229 wCoreDestroy(icon->core);
230 wfree(icon);
233 static void drawIconTitleBackground(WScreen *scr, Pixmap pixmap, int height)
235 XFillRectangle(dpy, pixmap, scr->icon_title_texture->normal_gc, 0, 0, wPreferences.icon_size, height + 1);
236 XDrawLine(dpy, pixmap, scr->icon_title_texture->light_gc, 0, 0, wPreferences.icon_size, 0);
237 XDrawLine(dpy, pixmap, scr->icon_title_texture->light_gc, 0, 0, 0, height + 1);
238 XDrawLine(dpy, pixmap, scr->icon_title_texture->dim_gc,
239 wPreferences.icon_size - 1, 0, wPreferences.icon_size - 1, height + 1);
242 static void icon_update_pixmap(WIcon *icon, RImage *image)
244 RImage *tile;
245 Pixmap pixmap;
246 int x, y, sx, sy;
247 unsigned w, h;
248 int theight = 0;
249 WScreen *scr = icon->core->screen_ptr;
251 switch (icon->tile_type) {
252 case TILE_NORMAL:
253 tile = RCloneImage(scr->icon_tile);
254 break;
255 case TILE_CLIP:
256 tile = RCloneImage(scr->clip_tile);
257 break;
258 case TILE_DRAWER:
259 tile = RCloneImage(scr->drawer_tile);
260 break;
261 default:
263 * The icon has always rigth value, this case is
264 * only to avoid a compiler warning with "tile"
265 * "may be used uninitialized"
267 wwarning("Unknown tile type: %d.\n", icon->tile_type);
268 tile = RCloneImage(scr->icon_tile);
271 if (image) {
272 w = (image->width > wPreferences.icon_size)
273 ? wPreferences.icon_size : image->width;
274 x = (wPreferences.icon_size - w) / 2;
275 sx = (image->width - w) / 2;
277 if (icon->show_title)
278 theight = WMFontHeight(scr->icon_title_font);
280 h = (image->height + theight > wPreferences.icon_size
281 ? wPreferences.icon_size - theight : image->height);
282 y = theight + (wPreferences.icon_size - theight - h) / 2;
283 sy = (image->height - h) / 2;
285 RCombineArea(tile, image, sx, sy, w, h, x, y);
288 if (icon->shadowed) {
289 RColor color;
291 color.red = scr->icon_back_texture->light.red >> 8;
292 color.green = scr->icon_back_texture->light.green >> 8;
293 color.blue = scr->icon_back_texture->light.blue >> 8;
294 color.alpha = 150; /* about 60% */
295 RClearImage(tile, &color);
298 if (icon->highlighted) {
299 RColor color;
301 color.red = color.green = color.blue = 0;
302 color.alpha = 160;
303 RLightImage(tile, &color);
306 if (!RConvertImage(scr->rcontext, tile, &pixmap))
307 wwarning(_("error rendering image:%s"), RMessageForError(RErrorCode));
309 RReleaseImage(tile);
311 /* Draw the icon's title background (without text) */
312 if (icon->show_title)
313 drawIconTitleBackground(scr, pixmap, theight);
315 icon->pixmap = pixmap;
318 void wIconChangeTitle(WIcon *icon, WWindow *wwin)
320 if (!icon || !wwin)
321 return;
323 /* Remove the previous icon title */
324 if (icon->icon_name != NULL)
325 XFree(icon->icon_name);
327 /* Set the new one, using two methods */
328 icon->icon_name = wNETWMGetIconName(wwin->client_win);
329 if (!icon->icon_name)
330 wGetIconName(dpy, wwin->client_win, &icon->icon_name);
333 RImage *wIconValidateIconSize(RImage *icon, int max_size)
335 RImage *nimage;
337 if (!icon)
338 return NULL;
340 /* We should hold "ICON_BORDER" (~2) pixels to include the icon border */
341 if (((max_size + ICON_BORDER) < icon->width) ||
342 ((max_size + ICON_BORDER) < icon->height)) {
343 if (icon->width > icon->height)
344 nimage = RScaleImage(icon, max_size - ICON_BORDER,
345 (icon->height * (max_size - ICON_BORDER) / icon->width));
346 else
347 nimage = RScaleImage(icon, (icon->width * (max_size - ICON_BORDER) / icon->height),
348 max_size - ICON_BORDER);
349 RReleaseImage(icon);
350 icon = nimage;
353 return icon;
356 Bool wIconChangeImageFile(WIcon *icon, const char *file)
358 WScreen *scr = icon->core->screen_ptr;
359 char *path;
360 RImage *image = NULL;
361 int error = 0;
363 /* If no new image, don't do nothing */
364 if (!file)
365 return True;
367 /* Find the new image */
368 path = FindImage(wPreferences.icon_path, file);
369 if (path)
370 image = get_rimage_from_file(scr, path, wPreferences.icon_size);
371 else
372 error = 1;
374 /* New image! */
375 if (!error && image) {
376 /* Set the new image */
377 set_icon_image_from_image(icon, image);
378 icon->file = wstrdup(path);
379 update_icon_pixmap(icon);
380 } else {
381 error = 1;
384 if (path)
385 wfree(path);
387 return !error;
390 static char *get_name_for_wwin(WWindow *wwin)
392 return get_name_for_instance_class(wwin->wm_instance, wwin->wm_class);
395 char *get_name_for_instance_class(const char *wm_instance, const char *wm_class)
397 char *suffix;
398 int len;
400 if (wm_class && wm_instance) {
401 len = strlen(wm_class) + strlen(wm_instance) + 2;
402 suffix = wmalloc(len);
403 snprintf(suffix, len, "%s.%s", wm_instance, wm_class);
404 } else if (wm_class) {
405 len = strlen(wm_class) + 1;
406 suffix = wmalloc(len);
407 snprintf(suffix, len, "%s", wm_class);
408 } else if (wm_instance) {
409 len = strlen(wm_instance) + 1;
410 suffix = wmalloc(len);
411 snprintf(suffix, len, "%s", wm_instance);
412 } else {
413 return NULL;
416 return suffix;
419 static char *get_icon_cache_path(void)
421 const char *prefix;
422 char *path;
423 int len, ret;
425 prefix = wusergnusteppath();
426 len = strlen(prefix) + strlen(CACHE_ICON_PATH) + 2;
427 path = wmalloc(len);
428 snprintf(path, len, "%s%s/", prefix, CACHE_ICON_PATH);
430 /* If the folder exists, exit */
431 if (access(path, F_OK) == 0)
432 return path;
434 /* Create the folder */
435 ret = wmkdirhier((const char *) path);
437 /* Exit 1 on success, 0 on failure */
438 if (ret == 1)
439 return path;
441 /* Fail */
442 wfree(path);
443 return NULL;
446 static RImage *get_wwindow_image_from_wmhints(WWindow *wwin, WIcon *icon)
448 RImage *image = NULL;
449 XWMHints *hints = wwin->wm_hints;
451 if (hints && (hints->flags & IconPixmapHint) && hints->icon_pixmap != None)
452 image = RCreateImageFromDrawable(icon->core->screen_ptr->rcontext,
453 hints->icon_pixmap,
454 (hints->flags & IconMaskHint)
455 ? hints->icon_mask : None);
457 return image;
461 * wIconStore--
462 * Stores the client supplied icon at CACHE_ICON_PATH
463 * and returns the path for that icon. Returns NULL if there is no
464 * client supplied icon or on failure.
466 * Side effects:
467 * New directories might be created.
469 char *wIconStore(WIcon * icon)
471 char *path, *dir_path, *file;
472 int len = 0;
473 RImage *image = NULL;
474 WWindow *wwin = icon->owner;
476 if (!wwin)
477 return NULL;
479 dir_path = get_icon_cache_path();
480 if (!dir_path)
481 return NULL;
483 file = get_name_for_wwin(wwin);
484 if (!file) {
485 wfree(dir_path);
486 return NULL;
489 len = strlen(dir_path) + strlen(file) + 5;
490 path = wmalloc(len);
491 snprintf(path, len, "%s%s.xpm", dir_path, file);
492 wfree(dir_path);
493 wfree(file);
495 /* If icon exists, exit */
496 if (access(path, F_OK) == 0)
497 return path;
499 if (wwin->net_icon_image)
500 image = RRetainImage(wwin->net_icon_image);
501 else
502 image = get_wwindow_image_from_wmhints(wwin, icon);
504 if (!image) {
505 wfree(path);
506 return NULL;
509 if (!RSaveImage(image, path, "XPM")) {
510 wfree(path);
511 path = NULL;
514 RReleaseImage(image);
516 return path;
519 static void cycleColor(void *data)
521 WIcon *icon = (WIcon *) data;
522 WScreen *scr = icon->core->screen_ptr;
523 XGCValues gcv;
525 icon->step--;
526 gcv.dash_offset = icon->step;
527 XChangeGC(dpy, scr->icon_select_gc, GCDashOffset, &gcv);
529 XDrawRectangle(dpy, icon->core->window, scr->icon_select_gc, 0, 0,
530 icon->core->width - 1, icon->core->height - 1);
531 icon->handlerID = WMAddTimerHandler(COLOR_CYCLE_DELAY, cycleColor, icon);
534 void wIconSetHighlited(WIcon *icon, Bool flag)
536 if (icon->highlighted == flag)
537 return;
539 icon->highlighted = flag;
540 update_icon_pixmap(icon);
543 void wIconSelect(WIcon * icon)
545 WScreen *scr = icon->core->screen_ptr;
546 icon->selected = !icon->selected;
548 if (icon->selected) {
549 icon->step = 0;
550 if (!wPreferences.dont_blink)
551 icon->handlerID = WMAddTimerHandler(10, cycleColor, icon);
552 else
553 XDrawRectangle(dpy, icon->core->window, scr->icon_select_gc, 0, 0,
554 icon->core->width - 1, icon->core->height - 1);
555 } else {
556 if (icon->handlerID) {
557 WMDeleteTimerHandler(icon->handlerID);
558 icon->handlerID = NULL;
560 XClearArea(dpy, icon->core->window, 0, 0, icon->core->width, icon->core->height, True);
564 static void unset_icon_image(WIcon *icon)
566 if (icon->file) {
567 wfree(icon->file);
568 icon->file = NULL;
571 if (icon->file_image) {
572 RReleaseImage(icon->file_image);
573 icon->file_image = NULL;
577 void set_icon_image_from_image(WIcon *icon, RImage *image)
579 if (!icon)
580 return;
582 unset_icon_image(icon);
584 icon->file_image = NULL;
585 icon->file_image = image;
588 void wIconUpdate(WIcon *icon)
590 WWindow *wwin = NULL;
592 if (icon && icon->owner)
593 wwin = icon->owner;
595 if (wwin && WFLAGP(wwin, always_user_icon)) {
596 /* Forced use user_icon */
597 get_rimage_icon_from_user_icon(icon);
598 } else if (icon->icon_win != None) {
599 /* Get the Pixmap from the WIcon */
600 get_rimage_icon_from_icon_win(icon);
601 } else if (wwin && wwin->net_icon_image) {
602 /* Use _NET_WM_ICON icon */
603 get_rimage_icon_from_x11(icon);
604 } else if (wwin && wwin->wm_hints && (wwin->wm_hints->flags & IconPixmapHint)) {
605 /* Get the Pixmap from the wm_hints, else, from the user */
606 unset_icon_image(icon);
607 icon->file_image = get_rimage_icon_from_wm_hints(icon);
608 if (!icon->file_image)
609 get_rimage_icon_from_user_icon(icon);
610 } else {
611 /* Get the Pixmap from the user */
612 get_rimage_icon_from_user_icon(icon);
615 update_icon_pixmap(icon);
618 void update_icon_pixmap(WIcon *icon)
620 if (icon->pixmap != None)
621 XFreePixmap(dpy, icon->pixmap);
623 icon->pixmap = None;
625 /* Create the pixmap */
626 if (icon->file_image)
627 icon_update_pixmap(icon, icon->file_image);
629 /* If dockapp, put inside the icon */
630 if (icon->icon_win != None) {
631 /* file_image is NULL, because is docked app */
632 icon_update_pixmap(icon, NULL);
633 set_dockapp_in_icon(icon);
636 /* No pixmap, set default background */
637 if (icon->pixmap != None)
638 XSetWindowBackgroundPixmap(dpy, icon->core->window, icon->pixmap);
640 /* Paint it */
641 wIconPaint(icon);
644 static void get_rimage_icon_from_x11(WIcon *icon)
646 /* Remove the icon image */
647 unset_icon_image(icon);
649 /* Set the new icon image */
650 icon->file_image = RRetainImage(icon->owner->net_icon_image);
653 static void get_rimage_icon_from_user_icon(WIcon *icon)
655 if (icon->file_image)
656 return;
658 get_rimage_icon_from_default_icon(icon);
661 static void get_rimage_icon_from_default_icon(WIcon *icon)
663 WScreen *scr = icon->core->screen_ptr;
665 /* If the icon don't have image, we should use the default image. */
666 if (!scr->def_icon_rimage)
667 scr->def_icon_rimage = get_default_image(scr);
669 /* Remove the icon image */
670 unset_icon_image(icon);
672 /* Set the new icon image */
673 icon->file_image = RRetainImage(scr->def_icon_rimage);
676 /* Get the RImage from the WIcon of the WWindow */
677 static void get_rimage_icon_from_icon_win(WIcon *icon)
679 RImage *image;
681 /* Create the new RImage */
682 image = get_window_image_from_x11(icon->icon_win);
684 /* Free the icon info */
685 unset_icon_image(icon);
687 /* Set the new info */
688 icon->file_image = image;
691 /* Set the dockapp in the WIcon */
692 static void set_dockapp_in_icon(WIcon *icon)
694 XWindowAttributes attr;
695 WScreen *scr = icon->core->screen_ptr;
696 unsigned int w, h, d;
698 /* Reparent the dock application to the icon */
700 /* We need the application size to center it
701 * and show in the correct position */
702 getSize(icon->icon_win, &w, &h, &d);
704 /* Set the background pixmap */
705 XSetWindowBackgroundPixmap(dpy, icon->core->window, scr->icon_tile_pixmap);
707 /* Set the icon border */
708 XSetWindowBorderWidth(dpy, icon->icon_win, 0);
710 /* Put the dock application in the icon */
711 XReparentWindow(dpy, icon->icon_win, icon->core->window,
712 (wPreferences.icon_size - w) / 2,
713 (wPreferences.icon_size - h) / 2);
715 /* Show it and save */
716 XMapWindow(dpy, icon->icon_win);
717 XAddToSaveSet(dpy, icon->icon_win);
719 /* Needed to move the icon clicking on the application part */
720 if ((XGetWindowAttributes(dpy, icon->icon_win, &attr)) &&
721 (attr.all_event_masks & ButtonPressMask))
722 wHackedGrabButton(Button1, MOD_MASK, icon->core->window, True,
723 ButtonPressMask, GrabModeSync, GrabModeAsync,
724 None, wCursor[WCUR_ARROW]);
727 /* Get the RImage from the XWindow wm_hints */
728 RImage *get_rimage_icon_from_wm_hints(WIcon *icon)
730 RImage *image = NULL;
731 unsigned int w, h, d;
732 WWindow *wwin = icon->owner;
734 if (!getSize(wwin->wm_hints->icon_pixmap, &w, &h, &d)) {
735 icon->owner->wm_hints->flags &= ~IconPixmapHint;
736 return NULL;
739 image = get_wwindow_image_from_wmhints(wwin, icon);
740 if (!image)
741 return NULL;
743 /* Resize the icon to the wPreferences.icon_size size */
744 image = wIconValidateIconSize(image, wPreferences.icon_size);
746 return image;
749 /* This function updates in the screen the icon title */
750 static void update_icon_title(WIcon *icon)
752 WScreen *scr = icon->core->screen_ptr;
753 int x, l, w;
754 char *tmp;
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);
773 void wIconPaint(WIcon *icon)
775 if (!icon || !icon->core || !icon->core->screen_ptr)
776 return;
778 WScreen *scr = icon->core->screen_ptr;
780 XClearWindow(dpy, icon->core->window);
782 update_icon_title(icon);
784 if (icon->selected)
785 XDrawRectangle(dpy, icon->core->window, scr->icon_select_gc, 0, 0,
786 icon->core->width - 1, icon->core->height - 1);
789 /******************************************************************/
791 static void miniwindowExpose(WObjDescriptor * desc, XEvent * event)
793 wIconPaint(desc->parent);
796 static void miniwindowDblClick(WObjDescriptor * desc, XEvent * event)
798 WIcon *icon = desc->parent;
800 assert(icon->owner != NULL);
802 wDeiconifyWindow(icon->owner);
805 static void miniwindowMouseDown(WObjDescriptor * desc, XEvent * event)
807 WIcon *icon = desc->parent;
808 WWindow *wwin = icon->owner;
809 XEvent ev;
810 int x = wwin->icon_x, y = wwin->icon_y;
811 int dx = event->xbutton.x, dy = event->xbutton.y;
812 int grabbed = 0;
813 int clickButton = event->xbutton.button;
814 Bool hasMoved = False;
816 if (WCHECK_STATE(WSTATE_MODAL))
817 return;
819 if (IsDoubleClick(icon->core->screen_ptr, event)) {
820 miniwindowDblClick(desc, event);
821 return;
824 if (event->xbutton.button == Button1) {
825 if (event->xbutton.state & MOD_MASK)
826 wLowerFrame(icon->core);
827 else
828 wRaiseFrame(icon->core);
829 if (event->xbutton.state & ShiftMask) {
830 wIconSelect(icon);
831 wSelectWindow(icon->owner, !wwin->flags.selected);
833 } else if (event->xbutton.button == Button3) {
834 WObjDescriptor *desc;
836 OpenMiniwindowMenu(wwin, event->xbutton.x_root, event->xbutton.y_root);
838 /* allow drag select of menu */
839 desc = &wwin->screen_ptr->window_menu->menu->descriptor;
840 event->xbutton.send_event = True;
841 (*desc->handle_mousedown) (desc, event);
843 return;
846 if (XGrabPointer(dpy, icon->core->window, False, ButtonMotionMask
847 | ButtonReleaseMask | ButtonPressMask, GrabModeAsync,
848 GrabModeAsync, None, None, CurrentTime) != GrabSuccess) {
850 while (1) {
851 WMMaskEvent(dpy, PointerMotionMask | ButtonReleaseMask | ButtonPressMask
852 | ButtonMotionMask | ExposureMask, &ev);
853 switch (ev.type) {
854 case Expose:
855 WMHandleEvent(&ev);
856 break;
858 case MotionNotify:
859 hasMoved = True;
860 if (!grabbed) {
861 if (abs(dx - ev.xmotion.x) >= MOVE_THRESHOLD
862 || abs(dy - ev.xmotion.y) >= MOVE_THRESHOLD) {
863 XChangeActivePointerGrab(dpy, ButtonMotionMask
864 | ButtonReleaseMask | ButtonPressMask,
865 wCursor[WCUR_MOVE], CurrentTime);
866 grabbed = 1;
867 } else {
868 break;
871 x = ev.xmotion.x_root - dx;
872 y = ev.xmotion.y_root - dy;
873 XMoveWindow(dpy, icon->core->window, x, y);
874 break;
876 case ButtonPress:
877 break;
879 case ButtonRelease:
880 if (ev.xbutton.button != clickButton)
881 break;
883 if (wwin->icon_x != x || wwin->icon_y != y)
884 wwin->flags.icon_moved = 1;
886 XMoveWindow(dpy, icon->core->window, x, y);
888 wwin->icon_x = x;
889 wwin->icon_y = y;
890 XUngrabPointer(dpy, CurrentTime);
892 if (wPreferences.auto_arrange_icons)
893 wArrangeIcons(wwin->screen_ptr, True);
894 if (wPreferences.single_click && !hasMoved)
895 miniwindowDblClick(desc, event);
896 return;
902 void set_icon_image_from_database(WIcon *icon, const char *wm_instance, const char *wm_class, const char *command)
904 char *file = NULL;
906 file = get_icon_filename(icon->core->screen_ptr, wm_instance, wm_class, command, False);
907 if (file) {
908 icon->file = wstrdup(file);
909 icon->file_image = get_rimage_from_file(icon->core->screen_ptr, icon->file, wPreferences.icon_size);
910 wfree(file);