6a93b7ad34539e12d5fd9325b22efda64a41fc2f
[wmaker-crm.git] / src / icon.c
blob6a93b7ad34539e12d5fd9325b22efda64a41fc2f
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 ****/
52 extern WPreferences wPreferences;
54 #define MOD_MASK wPreferences.modifier_mask
55 #define CACHE_ICON_PATH "/Library/WindowMaker/CachedPixmaps"
56 #define ICON_BORDER 3
58 extern Cursor wCursor[WCUR_LAST];
60 static void miniwindowExpose(WObjDescriptor * desc, XEvent * event);
61 static void miniwindowMouseDown(WObjDescriptor * desc, XEvent * event);
62 static void miniwindowDblClick(WObjDescriptor * desc, XEvent * event);
64 static WIcon *icon_create_core(WScreen *scr, int coord_x, int coord_y);
66 static void set_dockapp_in_icon(WIcon *icon);
67 static void get_rimage_icon_from_icon_win(WIcon *icon);
68 static void get_rimage_icon_from_user_icon(WIcon *icon);
69 static void get_rimage_icon_from_default_icon(WIcon *icon);
70 static void get_rimage_icon_from_x11(WIcon *icon);
72 static void icon_update_pixmap(WIcon *icon, RImage *image);
73 static void unset_icon_image(WIcon *icon);
75 /****** Notification Observers ******/
77 static void appearanceObserver(void *self, WMNotification *notif)
79 WIcon *icon = (WIcon *) self;
80 uintptr_t flags = (uintptr_t)WMGetNotificationClientData(notif);
82 if ((flags & WTextureSettings) || (flags & WFontSettings)) {
83 /* If the rimage exists, update the icon, else create it */
84 if (icon->file_image)
85 update_icon_pixmap(icon);
86 else
87 wIconPaint(icon);
90 /* so that the appicon expose handlers will paint the appicon specific
91 * stuff */
92 XClearArea(dpy, icon->core->window, 0, 0, icon->core->width, icon->core->height, True);
95 static void tileObserver(void *self, WMNotification *notif)
97 WIcon *icon = (WIcon *) self;
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, 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);
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 Bool wIconChangeImageFile(WIcon *icon, char *file)
359 WScreen *scr = icon->core->screen_ptr;
360 char *path;
361 RImage *image = NULL;
362 int error = 0;
364 /* If no new image, don't do nothing */
365 if (!file)
366 return True;
368 /* Find the new image */
369 path = FindImage(wPreferences.icon_path, file);
370 if (path)
371 image = get_rimage_from_file(scr, path, wPreferences.icon_size);
372 else
373 error = 1;
375 /* New image! */
376 if (!error && image) {
377 /* Set the new image */
378 set_icon_image_from_image(icon, image);
379 icon->file = wstrdup(path);
380 update_icon_pixmap(icon);
381 } else {
382 error = 1;
385 if (path)
386 wfree(path);
388 return !error;
391 static char *get_name_for_wwin(WWindow *wwin)
393 return get_name_for_instance_class(wwin->wm_instance, wwin->wm_class);
396 char *get_name_for_instance_class(char *wm_instance, char *wm_class)
398 char *suffix;
399 int len;
401 if (wm_class && wm_instance) {
402 len = strlen(wm_class) + strlen(wm_instance) + 2;
403 suffix = wmalloc(len);
404 snprintf(suffix, len, "%s.%s", wm_instance, wm_class);
405 } else if (wm_class) {
406 len = strlen(wm_class) + 1;
407 suffix = wmalloc(len);
408 snprintf(suffix, len, "%s", wm_class);
409 } else if (wm_instance) {
410 len = strlen(wm_instance) + 1;
411 suffix = wmalloc(len);
412 snprintf(suffix, len, "%s", wm_instance);
413 } else {
414 return NULL;
417 return suffix;
420 static char *get_icon_cache_path(void)
422 const char *prefix;
423 char *path;
424 int len, ret;
426 prefix = wusergnusteppath();
427 len = strlen(prefix) + strlen(CACHE_ICON_PATH) + 2;
428 path = wmalloc(len);
429 snprintf(path, len, "%s%s/", prefix, CACHE_ICON_PATH);
431 /* If the folder exists, exit */
432 if (access(path, F_OK) == 0)
433 return path;
435 /* Create the folder */
436 ret = wmkdirhier((const char *) path);
438 /* Exit 1 on success, 0 on failure */
439 if (ret == 1)
440 return path;
442 /* Fail */
443 wfree(path);
444 return NULL;
447 static RImage *get_wwindow_image_from_wmhints(WWindow *wwin, WIcon *icon)
449 RImage *image = NULL;
450 XWMHints *hints = wwin->wm_hints;
452 if (hints && (hints->flags & IconPixmapHint) && hints->icon_pixmap != None)
453 image = RCreateImageFromDrawable(icon->core->screen_ptr->rcontext,
454 hints->icon_pixmap,
455 (hints->flags & IconMaskHint)
456 ? hints->icon_mask : None);
458 return image;
462 * wIconStore--
463 * Stores the client supplied icon at CACHE_ICON_PATH
464 * and returns the path for that icon. Returns NULL if there is no
465 * client supplied icon or on failure.
467 * Side effects:
468 * New directories might be created.
470 char *wIconStore(WIcon * icon)
472 char *path, *dir_path, *file;
473 int len = 0;
474 RImage *image = NULL;
475 WWindow *wwin = icon->owner;
477 if (!wwin)
478 return NULL;
480 dir_path = get_icon_cache_path();
481 if (!dir_path)
482 return NULL;
484 file = get_name_for_wwin(wwin);
485 if (!file) {
486 wfree(dir_path);
487 return NULL;
490 len = strlen(dir_path) + strlen(file) + 5;
491 path = wmalloc(len);
492 snprintf(path, len, "%s%s.xpm", dir_path, file);
493 wfree(dir_path);
494 wfree(file);
496 /* If icon exists, exit */
497 if (access(path, F_OK) == 0)
498 return path;
500 if (wwin->net_icon_image)
501 image = RRetainImage(wwin->net_icon_image);
502 else
503 image = get_wwindow_image_from_wmhints(wwin, icon);
505 if (!image) {
506 wfree(path);
507 return NULL;
510 if (!RSaveImage(image, path, "XPM")) {
511 wfree(path);
512 path = NULL;
515 RReleaseImage(image);
517 return path;
520 static void cycleColor(void *data)
522 WIcon *icon = (WIcon *) data;
523 WScreen *scr = icon->core->screen_ptr;
524 XGCValues gcv;
526 icon->step--;
527 gcv.dash_offset = icon->step;
528 XChangeGC(dpy, scr->icon_select_gc, GCDashOffset, &gcv);
530 XDrawRectangle(dpy, icon->core->window, scr->icon_select_gc, 0, 0,
531 icon->core->width - 1, icon->core->height - 1);
532 icon->handlerID = WMAddTimerHandler(COLOR_CYCLE_DELAY, cycleColor, icon);
535 void wIconSetHighlited(WIcon *icon, Bool flag)
537 if (icon->highlighted == flag)
538 return;
540 icon->highlighted = flag;
541 update_icon_pixmap(icon);
544 void wIconSelect(WIcon * icon)
546 WScreen *scr = icon->core->screen_ptr;
547 icon->selected = !icon->selected;
549 if (icon->selected) {
550 icon->step = 0;
551 if (!wPreferences.dont_blink)
552 icon->handlerID = WMAddTimerHandler(10, cycleColor, icon);
553 else
554 XDrawRectangle(dpy, icon->core->window, scr->icon_select_gc, 0, 0,
555 icon->core->width - 1, icon->core->height - 1);
556 } else {
557 if (icon->handlerID) {
558 WMDeleteTimerHandler(icon->handlerID);
559 icon->handlerID = NULL;
561 XClearArea(dpy, icon->core->window, 0, 0, icon->core->width, icon->core->height, True);
565 static void unset_icon_image(WIcon *icon)
567 if (icon->file) {
568 wfree(icon->file);
569 icon->file = NULL;
572 if (icon->file_image) {
573 RReleaseImage(icon->file_image);
574 icon->file_image = NULL;
578 void set_icon_image_from_image(WIcon *icon, RImage *image)
580 if (!icon)
581 return;
583 unset_icon_image(icon);
585 icon->file_image = NULL;
586 icon->file_image = image;
589 void wIconUpdate(WIcon *icon)
591 WWindow *wwin = NULL;
593 if (icon && icon->owner)
594 wwin = icon->owner;
596 if (wwin && WFLAGP(wwin, always_user_icon)) {
597 /* Forced use user_icon */
598 get_rimage_icon_from_user_icon(icon);
599 } else if (icon->icon_win != None) {
600 /* Get the Pixmap from the WIcon */
601 get_rimage_icon_from_icon_win(icon);
602 } else if (wwin && wwin->net_icon_image) {
603 /* Use _NET_WM_ICON icon */
604 get_rimage_icon_from_x11(icon);
605 } else if (wwin && wwin->wm_hints && (wwin->wm_hints->flags & IconPixmapHint)) {
606 /* Get the Pixmap from the wm_hints, else, from the user */
607 unset_icon_image(icon);
608 icon->file_image = get_rimage_icon_from_wm_hints(icon);
609 if (!icon->file_image)
610 get_rimage_icon_from_user_icon(icon);
611 } else {
612 /* Get the Pixmap from the user */
613 get_rimage_icon_from_user_icon(icon);
616 update_icon_pixmap(icon);
619 void update_icon_pixmap(WIcon *icon)
621 if (icon->pixmap != None)
622 XFreePixmap(dpy, icon->pixmap);
624 icon->pixmap = None;
626 /* Create the pixmap */
627 if (icon->file_image)
628 icon_update_pixmap(icon, icon->file_image);
630 /* If dockapp, put inside the icon */
631 if (icon->icon_win != None) {
632 /* file_image is NULL, because is docked app */
633 icon_update_pixmap(icon, NULL);
634 set_dockapp_in_icon(icon);
637 /* No pixmap, set default background */
638 if (icon->pixmap != None)
639 XSetWindowBackgroundPixmap(dpy, icon->core->window, icon->pixmap);
641 /* Paint it */
642 wIconPaint(icon);
645 static void get_rimage_icon_from_x11(WIcon *icon)
647 /* Remove the icon image */
648 unset_icon_image(icon);
650 /* Set the new icon image */
651 icon->file_image = RRetainImage(icon->owner->net_icon_image);
654 static void get_rimage_icon_from_user_icon(WIcon *icon)
656 if (icon->file_image)
657 return;
659 get_rimage_icon_from_default_icon(icon);
662 static void get_rimage_icon_from_default_icon(WIcon *icon)
664 WScreen *scr = icon->core->screen_ptr;
666 /* If the icon don't have image, we should use the default image. */
667 if (!scr->def_icon_rimage)
668 scr->def_icon_rimage = get_default_image(scr);
670 /* Remove the icon image */
671 unset_icon_image(icon);
673 /* Set the new icon image */
674 icon->file_image = RRetainImage(scr->def_icon_rimage);
677 /* Get the RImage from the WIcon of the WWindow */
678 static void get_rimage_icon_from_icon_win(WIcon *icon)
680 RImage *image;
682 /* Create the new RImage */
683 image = get_window_image_from_x11(icon->icon_win);
685 /* Free the icon info */
686 unset_icon_image(icon);
688 /* Set the new info */
689 icon->file_image = image;
692 /* Set the dockapp in the WIcon */
693 static void set_dockapp_in_icon(WIcon *icon)
695 XWindowAttributes attr;
696 WScreen *scr = icon->core->screen_ptr;
697 unsigned int w, h, d;
699 /* Reparent the dock application to the icon */
701 /* We need the application size to center it
702 * and show in the correct position */
703 getSize(icon->icon_win, &w, &h, &d);
705 /* Set the background pixmap */
706 XSetWindowBackgroundPixmap(dpy, icon->core->window, scr->icon_tile_pixmap);
708 /* Set the icon border */
709 XSetWindowBorderWidth(dpy, icon->icon_win, 0);
711 /* Put the dock application in the icon */
712 XReparentWindow(dpy, icon->icon_win, icon->core->window,
713 (wPreferences.icon_size - w) / 2,
714 (wPreferences.icon_size - h) / 2);
716 /* Show it and save */
717 XMapWindow(dpy, icon->icon_win);
718 XAddToSaveSet(dpy, icon->icon_win);
720 /* Needed to move the icon clicking on the application part */
721 if ((XGetWindowAttributes(dpy, icon->icon_win, &attr)) &&
722 (attr.all_event_masks & ButtonPressMask))
723 wHackedGrabButton(Button1, MOD_MASK, icon->core->window, True,
724 ButtonPressMask, GrabModeSync, GrabModeAsync,
725 None, wCursor[WCUR_ARROW]);
728 /* Get the RImage from the XWindow wm_hints */
729 RImage *get_rimage_icon_from_wm_hints(WIcon *icon)
731 RImage *image = NULL;
732 unsigned int w, h, d;
733 WWindow *wwin = icon->owner;
735 if (!getSize(wwin->wm_hints->icon_pixmap, &w, &h, &d)) {
736 icon->owner->wm_hints->flags &= ~IconPixmapHint;
737 return NULL;
740 image = get_wwindow_image_from_wmhints(wwin, icon);
741 if (!image)
742 return NULL;
744 /* Resize the icon to the wPreferences.icon_size size */
745 image = wIconValidateIconSize(image, wPreferences.icon_size);
747 return image;
750 /* This function updates in the screen the icon title */
751 static void update_icon_title(WIcon *icon)
753 WScreen *scr = icon->core->screen_ptr;
754 int x, l, w;
755 char *tmp;
757 /* draw the icon title */
758 if (icon->show_title && icon->icon_name != NULL) {
759 tmp = ShrinkString(scr->icon_title_font, icon->icon_name, wPreferences.icon_size - 4);
760 w = WMWidthOfString(scr->icon_title_font, tmp, l = strlen(tmp));
762 if (w > icon->core->width - 4)
763 x = (icon->core->width - 4) - w;
764 else
765 x = (icon->core->width - w) / 2;
767 WMDrawString(scr->wmscreen, icon->core->window, scr->icon_title_color,
768 scr->icon_title_font, x, 1, tmp, l);
769 wfree(tmp);
774 void wIconPaint(WIcon *icon)
776 if (!icon || !icon->core || !icon->core->screen_ptr)
777 return;
779 WScreen *scr = icon->core->screen_ptr;
781 XClearWindow(dpy, icon->core->window);
783 update_icon_title(icon);
785 if (icon->selected)
786 XDrawRectangle(dpy, icon->core->window, scr->icon_select_gc, 0, 0,
787 icon->core->width - 1, icon->core->height - 1);
790 /******************************************************************/
792 static void miniwindowExpose(WObjDescriptor * desc, XEvent * event)
794 wIconPaint(desc->parent);
797 static void miniwindowDblClick(WObjDescriptor * desc, XEvent * event)
799 WIcon *icon = desc->parent;
801 assert(icon->owner != NULL);
803 wDeiconifyWindow(icon->owner);
806 static void miniwindowMouseDown(WObjDescriptor * desc, XEvent * event)
808 WIcon *icon = desc->parent;
809 WWindow *wwin = icon->owner;
810 XEvent ev;
811 int x = wwin->icon_x, y = wwin->icon_y;
812 int dx = event->xbutton.x, dy = event->xbutton.y;
813 int grabbed = 0;
814 int clickButton = event->xbutton.button;
815 Bool hasMoved = False;
817 if (WCHECK_STATE(WSTATE_MODAL))
818 return;
820 if (IsDoubleClick(icon->core->screen_ptr, event)) {
821 miniwindowDblClick(desc, event);
822 return;
825 if (event->xbutton.button == Button1) {
826 if (event->xbutton.state & MOD_MASK)
827 wLowerFrame(icon->core);
828 else
829 wRaiseFrame(icon->core);
830 if (event->xbutton.state & ShiftMask) {
831 wIconSelect(icon);
832 wSelectWindow(icon->owner, !wwin->flags.selected);
834 } else if (event->xbutton.button == Button3) {
835 WObjDescriptor *desc;
837 OpenMiniwindowMenu(wwin, event->xbutton.x_root, event->xbutton.y_root);
839 /* allow drag select of menu */
840 desc = &wwin->screen_ptr->window_menu->menu->descriptor;
841 event->xbutton.send_event = True;
842 (*desc->handle_mousedown) (desc, event);
844 return;
847 if (XGrabPointer(dpy, icon->core->window, False, ButtonMotionMask
848 | ButtonReleaseMask | ButtonPressMask, GrabModeAsync,
849 GrabModeAsync, None, None, CurrentTime) != GrabSuccess) {
851 while (1) {
852 WMMaskEvent(dpy, PointerMotionMask | ButtonReleaseMask | ButtonPressMask
853 | ButtonMotionMask | ExposureMask, &ev);
854 switch (ev.type) {
855 case Expose:
856 WMHandleEvent(&ev);
857 break;
859 case MotionNotify:
860 hasMoved = True;
861 if (!grabbed) {
862 if (abs(dx - ev.xmotion.x) >= MOVE_THRESHOLD
863 || abs(dy - ev.xmotion.y) >= MOVE_THRESHOLD) {
864 XChangeActivePointerGrab(dpy, ButtonMotionMask
865 | ButtonReleaseMask | ButtonPressMask,
866 wCursor[WCUR_MOVE], CurrentTime);
867 grabbed = 1;
868 } else {
869 break;
872 x = ev.xmotion.x_root - dx;
873 y = ev.xmotion.y_root - dy;
874 XMoveWindow(dpy, icon->core->window, x, y);
875 break;
877 case ButtonPress:
878 break;
880 case ButtonRelease:
881 if (ev.xbutton.button != clickButton)
882 break;
884 if (wwin->icon_x != x || wwin->icon_y != y)
885 wwin->flags.icon_moved = 1;
887 XMoveWindow(dpy, icon->core->window, x, y);
889 wwin->icon_x = x;
890 wwin->icon_y = y;
891 XUngrabPointer(dpy, CurrentTime);
893 if (wPreferences.auto_arrange_icons)
894 wArrangeIcons(wwin->screen_ptr, True);
895 if (wPreferences.single_click && !hasMoved)
896 miniwindowDblClick(desc, event);
897 return;
903 void set_icon_image_from_database(WIcon *icon, char *wm_instance, char *wm_class, char *command)
905 char *file = NULL;
907 file = get_icon_filename(icon->core->screen_ptr, wm_instance, wm_class, command, False);
908 if (file) {
909 icon->file = wstrdup(file);
910 icon->file_image = get_rimage_from_file(icon->core->screen_ptr, icon->file, wPreferences.icon_size);
911 wfree(file);