wmaker: unset default app logo size
[wmaker-crm.git] / src / icon.c
blobb811a4f6f2c9320d39e2a99b7125d50c8f9c944f
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 to identify
329 the icon name or switch back to window name */
330 icon->icon_name = wNETWMGetIconName(wwin->client_win);
331 if (!icon->icon_name)
332 if (!wGetIconName(dpy, wwin->client_win, &icon->icon_name))
333 icon->icon_name = wNETWMGetWindowName(wwin->client_win);
336 RImage *wIconValidateIconSize(RImage *icon, int max_size)
338 RImage *nimage;
340 if (!icon)
341 return NULL;
343 /* We should hold "ICON_BORDER" (~2) pixels to include the icon border */
344 if (((max_size + ICON_BORDER) < icon->width) ||
345 ((max_size + ICON_BORDER) < icon->height)) {
346 if (icon->width > icon->height)
347 nimage = RScaleImage(icon, max_size - ICON_BORDER,
348 (icon->height * (max_size - ICON_BORDER) / icon->width));
349 else
350 nimage = RScaleImage(icon, (icon->width * (max_size - ICON_BORDER) / icon->height),
351 max_size - ICON_BORDER);
352 RReleaseImage(icon);
353 icon = nimage;
356 return icon;
359 int wIconChangeImageFile(WIcon *icon, const char *file)
361 WScreen *scr = icon->core->screen_ptr;
362 char *path;
363 RImage *image = NULL;
365 /* If no new image, don't do nothing */
366 if (!file)
367 return 1;
369 /* Find the new image */
370 path = FindImage(wPreferences.icon_path, file);
371 if (!path)
372 return 0;
374 image = get_rimage_from_file(scr, path, wPreferences.icon_size);
375 if (!image) {
376 wfree(path);
377 return 0;
380 /* Set the new image */
381 set_icon_image_from_image(icon, image);
382 icon->file = wstrdup(path);
383 update_icon_pixmap(icon);
385 wfree(path);
386 return 1;
389 static char *get_name_for_wwin(WWindow *wwin)
391 return get_name_for_instance_class(wwin->wm_instance, wwin->wm_class);
394 char *get_name_for_instance_class(const char *wm_instance, const char *wm_class)
396 char *suffix;
397 int len;
399 if (wm_class && wm_instance) {
400 len = strlen(wm_class) + strlen(wm_instance) + 2;
401 suffix = wmalloc(len);
402 snprintf(suffix, len, "%s.%s", wm_instance, wm_class);
403 } else if (wm_class) {
404 len = strlen(wm_class) + 1;
405 suffix = wmalloc(len);
406 snprintf(suffix, len, "%s", wm_class);
407 } else if (wm_instance) {
408 len = strlen(wm_instance) + 1;
409 suffix = wmalloc(len);
410 snprintf(suffix, len, "%s", wm_instance);
411 } else {
412 return NULL;
415 return suffix;
418 static char *get_icon_cache_path(void)
420 const char *prefix;
421 char *path;
422 int len, ret;
424 prefix = wusergnusteppath();
425 len = strlen(prefix) + strlen(CACHE_ICON_PATH) + 2;
426 path = wmalloc(len);
427 snprintf(path, len, "%s%s/", prefix, CACHE_ICON_PATH);
429 /* If the folder exists, exit */
430 if (access(path, F_OK) == 0)
431 return path;
433 /* Create the folder */
434 ret = wmkdirhier((const char *) path);
436 /* Exit 1 on success, 0 on failure */
437 if (ret == 1)
438 return path;
440 /* Fail */
441 wfree(path);
442 return NULL;
445 static RImage *get_wwindow_image_from_wmhints(WWindow *wwin, WIcon *icon)
447 RImage *image = NULL;
448 XWMHints *hints = wwin->wm_hints;
450 if (hints && (hints->flags & IconPixmapHint) && hints->icon_pixmap != None)
451 image = RCreateImageFromDrawable(icon->core->screen_ptr->rcontext,
452 hints->icon_pixmap,
453 (hints->flags & IconMaskHint)
454 ? hints->icon_mask : None);
456 return image;
460 * wIconStore--
461 * Stores the client supplied icon at CACHE_ICON_PATH
462 * and returns the path for that icon. Returns NULL if there is no
463 * client supplied icon or on failure.
465 * Side effects:
466 * New directories might be created.
468 char *wIconStore(WIcon *icon)
470 char *path, *dir_path, *file;
471 int len = 0;
472 RImage *image = NULL;
473 WWindow *wwin = icon->owner;
475 if (!wwin)
476 return NULL;
478 dir_path = get_icon_cache_path();
479 if (!dir_path)
480 return NULL;
482 file = get_name_for_wwin(wwin);
483 if (!file) {
484 wfree(dir_path);
485 return NULL;
488 len = strlen(dir_path) + strlen(file) + 5;
489 path = wmalloc(len);
490 snprintf(path, len, "%s%s.xpm", dir_path, file);
491 wfree(dir_path);
492 wfree(file);
494 /* If icon exists, exit */
495 if (access(path, F_OK) == 0)
496 return path;
498 if (wwin->net_icon_image)
499 image = RRetainImage(wwin->net_icon_image);
500 else
501 image = get_wwindow_image_from_wmhints(wwin, icon);
503 if (!image) {
504 wfree(path);
505 return NULL;
508 if (!RSaveImage(image, path, "XPM")) {
509 wfree(path);
510 path = NULL;
513 RReleaseImage(image);
515 return path;
518 static void cycleColor(void *data)
520 WIcon *icon = (WIcon *) data;
521 WScreen *scr = icon->core->screen_ptr;
522 XGCValues gcv;
524 icon->step--;
525 gcv.dash_offset = icon->step;
526 XChangeGC(dpy, scr->icon_select_gc, GCDashOffset, &gcv);
528 XDrawRectangle(dpy, icon->core->window, scr->icon_select_gc, 0, 0,
529 icon->core->width - 1, icon->core->height - 1);
530 icon->handlerID = WMAddTimerHandler(COLOR_CYCLE_DELAY, cycleColor, icon);
533 void wIconSetHighlited(WIcon *icon, Bool flag)
535 if (icon->highlighted == flag)
536 return;
538 icon->highlighted = flag;
539 update_icon_pixmap(icon);
542 void wIconSelect(WIcon *icon)
544 WScreen *scr = icon->core->screen_ptr;
545 icon->selected = !icon->selected;
547 if (icon->selected) {
548 icon->step = 0;
549 if (!wPreferences.dont_blink)
550 icon->handlerID = WMAddTimerHandler(10, cycleColor, icon);
551 else
552 XDrawRectangle(dpy, icon->core->window, scr->icon_select_gc, 0, 0,
553 icon->core->width - 1, icon->core->height - 1);
554 } else {
555 if (icon->handlerID) {
556 WMDeleteTimerHandler(icon->handlerID);
557 icon->handlerID = NULL;
559 XClearArea(dpy, icon->core->window, 0, 0, icon->core->width, icon->core->height, True);
563 static void unset_icon_image(WIcon *icon)
565 if (icon->file) {
566 wfree(icon->file);
567 icon->file = NULL;
570 if (icon->file_image) {
571 RReleaseImage(icon->file_image);
572 icon->file_image = NULL;
576 void set_icon_image_from_image(WIcon *icon, RImage *image)
578 if (!icon)
579 return;
581 unset_icon_image(icon);
583 icon->file_image = NULL;
584 icon->file_image = image;
587 void wIconUpdate(WIcon *icon)
589 WWindow *wwin = NULL;
591 if (icon && icon->owner)
592 wwin = icon->owner;
594 if (wwin && WFLAGP(wwin, always_user_icon)) {
595 /* Forced use user_icon */
596 get_rimage_icon_from_user_icon(icon);
597 } else if (icon->icon_win != None) {
598 /* Get the Pixmap from the WIcon */
599 get_rimage_icon_from_icon_win(icon);
600 } else if (wwin && wwin->net_icon_image) {
601 /* Use _NET_WM_ICON icon */
602 get_rimage_icon_from_x11(icon);
603 } else if (wwin && wwin->wm_hints && (wwin->wm_hints->flags & IconPixmapHint)) {
604 /* Get the Pixmap from the wm_hints, else, from the user */
605 unset_icon_image(icon);
606 icon->file_image = get_rimage_icon_from_wm_hints(icon);
607 if (!icon->file_image)
608 get_rimage_icon_from_user_icon(icon);
609 } else {
610 /* Get the Pixmap from the user */
611 get_rimage_icon_from_user_icon(icon);
614 update_icon_pixmap(icon);
617 void update_icon_pixmap(WIcon *icon)
619 if (icon->pixmap != None)
620 XFreePixmap(dpy, icon->pixmap);
622 icon->pixmap = None;
624 /* Create the pixmap */
625 if (icon->file_image)
626 icon_update_pixmap(icon, icon->file_image);
628 /* If dockapp, put inside the icon */
629 if (icon->icon_win != None) {
630 /* file_image is NULL, because is docked app */
631 icon_update_pixmap(icon, NULL);
632 set_dockapp_in_icon(icon);
635 /* No pixmap, set default background */
636 if (icon->pixmap != None)
637 XSetWindowBackgroundPixmap(dpy, icon->core->window, icon->pixmap);
639 /* Paint it */
640 wIconPaint(icon);
643 static void get_rimage_icon_from_x11(WIcon *icon)
645 /* Remove the icon image */
646 unset_icon_image(icon);
648 /* Set the new icon image */
649 icon->file_image = RRetainImage(icon->owner->net_icon_image);
652 static void get_rimage_icon_from_user_icon(WIcon *icon)
654 if (icon->file_image)
655 return;
657 get_rimage_icon_from_default_icon(icon);
660 static void get_rimage_icon_from_default_icon(WIcon *icon)
662 WScreen *scr = icon->core->screen_ptr;
664 /* If the icon don't have image, we should use the default image. */
665 if (!scr->def_icon_rimage)
666 scr->def_icon_rimage = get_default_image(scr);
668 /* Remove the icon image */
669 unset_icon_image(icon);
671 /* Set the new icon image */
672 icon->file_image = RRetainImage(scr->def_icon_rimage);
675 /* Get the RImage from the WIcon of the WWindow */
676 static void get_rimage_icon_from_icon_win(WIcon *icon)
678 RImage *image;
680 /* Create the new RImage */
681 image = get_window_image_from_x11(icon->icon_win);
683 /* Free the icon info */
684 unset_icon_image(icon);
686 /* Set the new info */
687 icon->file_image = image;
690 /* Set the dockapp in the WIcon */
691 static void set_dockapp_in_icon(WIcon *icon)
693 XWindowAttributes attr;
694 WScreen *scr = icon->core->screen_ptr;
695 unsigned int w, h, d;
697 /* Reparent the dock application to the icon */
699 /* We need the application size to center it
700 * and show in the correct position */
701 getSize(icon->icon_win, &w, &h, &d);
703 /* Set the background pixmap */
704 XSetWindowBackgroundPixmap(dpy, icon->core->window, scr->icon_tile_pixmap);
706 /* Set the icon border */
707 XSetWindowBorderWidth(dpy, icon->icon_win, 0);
709 /* Put the dock application in the icon */
710 XReparentWindow(dpy, icon->icon_win, icon->core->window,
711 (wPreferences.icon_size - w) / 2,
712 (wPreferences.icon_size - h) / 2);
714 /* Show it and save */
715 XMapWindow(dpy, icon->icon_win);
716 XAddToSaveSet(dpy, icon->icon_win);
718 /* Needed to move the icon clicking on the application part */
719 if ((XGetWindowAttributes(dpy, icon->icon_win, &attr)) &&
720 (attr.all_event_masks & ButtonPressMask))
721 wHackedGrabButton(Button1, MOD_MASK, icon->core->window, True,
722 ButtonPressMask, GrabModeSync, GrabModeAsync,
723 None, wPreferences.cursor[WCUR_ARROW]);
726 /* Get the RImage from the XWindow wm_hints */
727 RImage *get_rimage_icon_from_wm_hints(WIcon *icon)
729 RImage *image = NULL;
730 unsigned int w, h, d;
731 WWindow *wwin = icon->owner;
733 if (!getSize(wwin->wm_hints->icon_pixmap, &w, &h, &d)) {
734 icon->owner->wm_hints->flags &= ~IconPixmapHint;
735 return NULL;
738 image = get_wwindow_image_from_wmhints(wwin, icon);
739 if (!image)
740 return NULL;
742 /* Resize the icon to the wPreferences.icon_size size */
743 image = wIconValidateIconSize(image, wPreferences.icon_size);
745 return image;
748 /* This function updates in the screen the icon title */
749 static void update_icon_title(WIcon *icon)
751 WScreen *scr = icon->core->screen_ptr;
752 int x, l, w;
753 char *tmp;
755 /* draw the icon title */
756 if (icon->show_title && icon->icon_name != NULL) {
757 tmp = ShrinkString(scr->icon_title_font, icon->icon_name, wPreferences.icon_size - 4);
758 w = WMWidthOfString(scr->icon_title_font, tmp, l = strlen(tmp));
760 if (w > icon->core->width - 4)
761 x = (icon->core->width - 4) - w;
762 else
763 x = (icon->core->width - w) / 2;
765 WMDrawString(scr->wmscreen, icon->core->window, scr->icon_title_color,
766 scr->icon_title_font, x, 1, tmp, l);
767 wfree(tmp);
772 void wIconPaint(WIcon *icon)
774 if (!icon || !icon->core || !icon->core->screen_ptr)
775 return;
777 WScreen *scr = icon->core->screen_ptr;
779 XClearWindow(dpy, icon->core->window);
781 update_icon_title(icon);
783 if (icon->selected)
784 XDrawRectangle(dpy, icon->core->window, scr->icon_select_gc, 0, 0,
785 icon->core->width - 1, icon->core->height - 1);
788 /******************************************************************/
790 static void miniwindowExpose(WObjDescriptor *desc, XEvent *event)
792 /* Parameter not used, but tell the compiler that it is ok */
793 (void) event;
795 wIconPaint(desc->parent);
798 static void miniwindowDblClick(WObjDescriptor *desc, XEvent *event)
800 WIcon *icon = desc->parent;
802 /* Parameter not used, but tell the compiler that it is ok */
803 (void) event;
805 assert(icon->owner != NULL);
807 wDeiconifyWindow(icon->owner);
810 static void miniwindowMouseDown(WObjDescriptor *desc, XEvent *event)
812 WIcon *icon = desc->parent;
813 WWindow *wwin = icon->owner;
814 XEvent ev;
815 int x = wwin->icon_x, y = wwin->icon_y;
816 int dx = event->xbutton.x, dy = event->xbutton.y;
817 int grabbed = 0;
818 int clickButton = event->xbutton.button;
819 Bool hasMoved = False;
821 if (WCHECK_STATE(WSTATE_MODAL))
822 return;
824 if (IsDoubleClick(icon->core->screen_ptr, event)) {
825 miniwindowDblClick(desc, event);
826 return;
829 if (event->xbutton.button == Button1) {
830 if (event->xbutton.state & MOD_MASK)
831 wLowerFrame(icon->core);
832 else
833 wRaiseFrame(icon->core);
834 if (event->xbutton.state & ShiftMask) {
835 wIconSelect(icon);
836 wSelectWindow(icon->owner, !wwin->flags.selected);
838 } else if (event->xbutton.button == Button3) {
839 WObjDescriptor *desc;
841 OpenMiniwindowMenu(wwin, event->xbutton.x_root, event->xbutton.y_root);
843 /* allow drag select of menu */
844 desc = &wwin->screen_ptr->window_menu->menu->descriptor;
845 event->xbutton.send_event = True;
846 (*desc->handle_mousedown) (desc, event);
848 return;
851 if (XGrabPointer(dpy, icon->core->window, False, ButtonMotionMask
852 | ButtonReleaseMask | ButtonPressMask, GrabModeAsync,
853 GrabModeAsync, None, None, CurrentTime) != GrabSuccess) {
855 while (1) {
856 WMMaskEvent(dpy, PointerMotionMask | ButtonReleaseMask | ButtonPressMask
857 | ButtonMotionMask | ExposureMask, &ev);
858 switch (ev.type) {
859 case Expose:
860 WMHandleEvent(&ev);
861 break;
863 case MotionNotify:
864 hasMoved = True;
865 if (!grabbed) {
866 if (abs(dx - ev.xmotion.x) >= MOVE_THRESHOLD
867 || abs(dy - ev.xmotion.y) >= MOVE_THRESHOLD) {
868 XChangeActivePointerGrab(dpy, ButtonMotionMask
869 | ButtonReleaseMask | ButtonPressMask,
870 wPreferences.cursor[WCUR_MOVE], CurrentTime);
871 grabbed = 1;
872 } else {
873 break;
876 x = ev.xmotion.x_root - dx;
877 y = ev.xmotion.y_root - dy;
878 XMoveWindow(dpy, icon->core->window, x, y);
879 break;
881 case ButtonPress:
882 break;
884 case ButtonRelease:
885 if (ev.xbutton.button != clickButton)
886 break;
888 if (wwin->icon_x != x || wwin->icon_y != y)
889 wwin->flags.icon_moved = 1;
891 XMoveWindow(dpy, icon->core->window, x, y);
893 wwin->icon_x = x;
894 wwin->icon_y = y;
895 XUngrabPointer(dpy, CurrentTime);
897 if (wPreferences.auto_arrange_icons)
898 wArrangeIcons(wwin->screen_ptr, True);
899 if (wPreferences.single_click && !hasMoved)
900 miniwindowDblClick(desc, event);
901 return;
907 void set_icon_image_from_database(WIcon *icon, const char *wm_instance, const char *wm_class, const char *command)
909 char *file = NULL;
911 file = get_icon_filename(wm_instance, wm_class, command, False);
912 if (file) {
913 icon->file = wstrdup(file);
914 icon->file_image = get_rimage_from_file(icon->core->screen_ptr, icon->file, wPreferences.icon_size);
915 wfree(file);