Merge branch 'wmdrawer' into next
[wmaker-crm.git] / src / icon.c
blob2d0134158d492037ae01fc60eb3c975ed667773c
1 /* icon.c - window icon and dock and appicon parent
3 * Window Maker window manager
5 * Copyright (c) 1997-2003 Alfredo K. Kojima
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License along
18 * with this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22 #include "wconfig.h"
24 #include <X11/Xlib.h>
25 #include <X11/Xutil.h>
26 #include <stdlib.h>
27 #include <stdio.h>
28 #include <stdint.h>
29 #include <string.h>
30 #include <unistd.h>
31 #include <ctype.h>
32 #include <wraster.h>
33 #include <sys/stat.h>
35 #include "WindowMaker.h"
36 #include "wcore.h"
37 #include "texture.h"
38 #include "window.h"
39 #include "icon.h"
40 #include "actions.h"
41 #include "funcs.h"
42 #include "stacking.h"
43 #include "application.h"
44 #include "defaults.h"
45 #include "appicon.h"
46 #include "wmspec.h"
48 /**** Global varianebles ****/
49 extern WPreferences wPreferences;
51 #define MOD_MASK wPreferences.modifier_mask
52 #define CACHE_ICON_PATH "/Library/WindowMaker/CachedPixmaps"
53 #define ICON_BORDER 3
55 extern Cursor wCursor[WCUR_LAST];
57 static void miniwindowExpose(WObjDescriptor * desc, XEvent * event);
58 static void miniwindowMouseDown(WObjDescriptor * desc, XEvent * event);
59 static void miniwindowDblClick(WObjDescriptor * desc, XEvent * event);
61 static WIcon *icon_create_core(WScreen *scr, int coord_x, int coord_y);
63 static void set_dockapp_in_icon(WIcon *icon);
64 static void get_rimage_icon_from_icon_win(WIcon *icon);
65 static void get_rimage_icon_from_user_icon(WIcon *icon);
66 static void get_rimage_icon_from_default_icon(WIcon *icon);
67 static void get_rimage_icon_from_x11(WIcon *icon);
69 static void icon_update_pixmap(WIcon *icon, RImage *image);
70 static void unset_icon_image(WIcon *icon);
72 /****** Notification Observers ******/
74 static void appearanceObserver(void *self, WMNotification *notif)
76 WIcon *icon = (WIcon *) self;
77 uintptr_t flags = (uintptr_t)WMGetNotificationClientData(notif);
79 if ((flags & WTextureSettings) || (flags & WFontSettings)) {
80 /* If the rimage exists, update the icon, else create it */
81 if (icon->file_image)
82 update_icon_pixmap(icon);
83 else
84 wIconPaint(icon);
87 /* so that the appicon expose handlers will paint the appicon specific
88 * stuff */
89 XClearArea(dpy, icon->core->window, 0, 0, icon->core->width, icon->core->height, True);
92 static void tileObserver(void *self, WMNotification *notif)
94 WIcon *icon = (WIcon *) self;
96 update_icon_pixmap(icon);
98 XClearArea(dpy, icon->core->window, 0, 0, 1, 1, True);
101 /************************************/
103 static int getSize(Drawable d, unsigned int *w, unsigned int *h, unsigned int *dep)
105 Window rjunk;
106 int xjunk, yjunk;
107 unsigned int bjunk;
109 return XGetGeometry(dpy, d, &rjunk, &xjunk, &yjunk, w, h, &bjunk, dep);
112 WIcon *icon_create_for_wwindow(WWindow *wwin)
114 WScreen *scr = wwin->screen_ptr;
115 WIcon *icon;
117 icon = icon_create_core(scr, wwin->icon_x, wwin->icon_y);
119 icon->owner = wwin;
120 if (wwin->wm_hints && (wwin->wm_hints->flags & IconWindowHint)) {
121 if (wwin->client_win == wwin->main_window) {
122 WApplication *wapp;
123 /* do not let miniwindow steal app-icon's icon window */
124 wapp = wApplicationOf(wwin->client_win);
125 if (!wapp || wapp->app_icon == NULL)
126 icon->icon_win = wwin->wm_hints->icon_window;
127 } else {
128 icon->icon_win = wwin->wm_hints->icon_window;
131 #ifdef NO_MINIWINDOW_TITLES
132 icon->show_title = 0;
133 #else
134 icon->show_title = 1;
135 #endif
137 wIconChangeTitle(icon, wwin);
138 icon->tile_type = TILE_NORMAL;
140 set_icon_image_from_database(icon, wwin->wm_instance, wwin->wm_class, NULL);
141 /* Update the icon, because icon could be NULL */
142 wIconUpdate(icon);
144 WMAddNotificationObserver(appearanceObserver, icon, WNIconAppearanceSettingsChanged, icon);
145 WMAddNotificationObserver(tileObserver, icon, WNIconTileSettingsChanged, icon);
147 return icon;
150 WIcon *icon_create_for_dock(WScreen *scr, char *command, char *wm_instance, char *wm_class, int tile)
152 WIcon *icon;
154 icon = icon_create_core(scr, 0, 0);
155 icon->tile_type = tile;
157 set_icon_image_from_database(icon, wm_instance, wm_class, command);
158 /* Update the icon, because icon could be NULL */
159 wIconUpdate(icon);
161 WMAddNotificationObserver(appearanceObserver, icon, WNIconAppearanceSettingsChanged, icon);
162 WMAddNotificationObserver(tileObserver, icon, WNIconTileSettingsChanged, icon);
164 return icon;
167 static WIcon *icon_create_core(WScreen *scr, int coord_x, int coord_y)
169 WIcon *icon;
171 icon = wmalloc(sizeof(WIcon));
172 icon->core = wCoreCreateTopLevel(scr,
173 coord_x,
174 coord_y,
175 wPreferences.icon_size,
176 wPreferences.icon_size,
177 0, scr->w_depth, scr->w_visual, scr->w_colormap,
178 scr->white_pixel);
180 /* will be overriden if this is a application icon */
181 icon->core->descriptor.handle_mousedown = miniwindowMouseDown;
182 icon->core->descriptor.handle_expose = miniwindowExpose;
183 icon->core->descriptor.parent_type = WCLASS_MINIWINDOW;
184 icon->core->descriptor.parent = icon;
186 icon->core->stacking = wmalloc(sizeof(WStacking));
187 icon->core->stacking->above = NULL;
188 icon->core->stacking->under = NULL;
189 icon->core->stacking->window_level = NORMAL_ICON_LEVEL;
190 icon->core->stacking->child_of = NULL;
192 /* Icon image */
193 icon->file = NULL;
194 icon->file_image = NULL;
196 return icon;
199 void wIconDestroy(WIcon * icon)
201 WCoreWindow *core = icon->core;
202 WScreen *scr = core->screen_ptr;
204 WMRemoveNotificationObserver(icon);
206 if (icon->handlerID)
207 WMDeleteTimerHandler(icon->handlerID);
209 if (icon->icon_win) {
210 int x = 0, y = 0;
212 if (icon->owner) {
213 x = icon->owner->icon_x;
214 y = icon->owner->icon_y;
216 XUnmapWindow(dpy, icon->icon_win);
217 XReparentWindow(dpy, icon->icon_win, scr->root_win, x, y);
219 if (icon->icon_name)
220 XFree(icon->icon_name);
222 if (icon->pixmap)
223 XFreePixmap(dpy, icon->pixmap);
225 unset_icon_image(icon);
227 wCoreDestroy(icon->core);
228 wfree(icon);
231 static void drawIconTitleBackground(WScreen *scr, Pixmap pixmap, int height)
233 XFillRectangle(dpy, pixmap, scr->icon_title_texture->normal_gc, 0, 0, wPreferences.icon_size, height + 1);
234 XDrawLine(dpy, pixmap, scr->icon_title_texture->light_gc, 0, 0, wPreferences.icon_size, 0);
235 XDrawLine(dpy, pixmap, scr->icon_title_texture->light_gc, 0, 0, 0, height + 1);
236 XDrawLine(dpy, pixmap, scr->icon_title_texture->dim_gc,
237 wPreferences.icon_size - 1, 0, wPreferences.icon_size - 1, height + 1);
240 static void icon_update_pixmap(WIcon *icon, RImage *image)
242 RImage *tile;
243 Pixmap pixmap;
244 int x, y, sx, sy;
245 unsigned w, h;
246 int theight = 0;
247 WScreen *scr = icon->core->screen_ptr;
249 switch (icon->tile_type) {
250 case TILE_NORMAL:
251 tile = RCloneImage(scr->icon_tile);
252 break;
253 default:
254 wwarning("Unknown tileType: %d.\n", icon->tile_type);
255 // fallthrough to TILE_CLIP (emulate previous behaviour)
256 case TILE_CLIP:
257 assert(scr->clip_tile);
258 tile = RCloneImage(scr->clip_tile);
259 break;
260 case TILE_DRAWER:
261 assert(scr->drawer_tile);
262 tile = RCloneImage(scr->drawer_tile);
263 break;
266 if (image) {
267 w = (image->width > wPreferences.icon_size)
268 ? wPreferences.icon_size : image->width;
269 x = (wPreferences.icon_size - w) / 2;
270 sx = (image->width - w) / 2;
272 if (icon->show_title)
273 theight = WMFontHeight(scr->icon_title_font);
275 h = (image->height + theight > wPreferences.icon_size
276 ? wPreferences.icon_size - theight : image->height);
277 y = theight + (wPreferences.icon_size - theight - h) / 2;
278 sy = (image->height - h) / 2;
280 RCombineArea(tile, image, sx, sy, w, h, x, y);
283 if (icon->shadowed) {
284 RColor color;
286 color.red = scr->icon_back_texture->light.red >> 8;
287 color.green = scr->icon_back_texture->light.green >> 8;
288 color.blue = scr->icon_back_texture->light.blue >> 8;
289 color.alpha = 150; /* about 60% */
290 RClearImage(tile, &color);
293 if (icon->highlighted) {
294 RColor color;
296 color.red = color.green = color.blue = 0;
297 color.alpha = 160;
298 RLightImage(tile, &color);
301 if (!RConvertImage(scr->rcontext, tile, &pixmap))
302 wwarning(_("error rendering image:%s"), RMessageForError(RErrorCode));
304 RReleaseImage(tile);
306 /* Draw the icon's title background (without text) */
307 if (icon->show_title)
308 drawIconTitleBackground(scr, pixmap, theight);
310 icon->pixmap = pixmap;
313 void wIconChangeTitle(WIcon *icon, WWindow *wwin)
315 if (!icon || !wwin)
316 return;
318 /* Remove the previous icon title */
319 if (icon->icon_name != NULL)
320 XFree(icon->icon_name);
322 /* Set the new one, using two methods */
323 icon->icon_name = wNETWMGetIconName(wwin->client_win);
324 if (!icon->icon_name)
325 wGetIconName(dpy, wwin->client_win, &icon->icon_name);
328 RImage *wIconValidateIconSize(RImage *icon, int max_size)
330 RImage *nimage;
332 if (!icon)
333 return NULL;
335 /* We should hold "ICON_BORDER" (~2) pixels to include the icon border */
336 if (((max_size + ICON_BORDER) < icon->width) ||
337 ((max_size + ICON_BORDER) < icon->height)) {
338 if (icon->width > icon->height)
339 nimage = RScaleImage(icon, max_size - ICON_BORDER,
340 (icon->height * (max_size - ICON_BORDER) / icon->width));
341 else
342 nimage = RScaleImage(icon, (icon->width * (max_size - ICON_BORDER) / icon->height),
343 max_size - ICON_BORDER);
344 RReleaseImage(icon);
345 icon = nimage;
348 return icon;
351 Bool wIconChangeImageFile(WIcon *icon, char *file)
353 WScreen *scr = icon->core->screen_ptr;
354 char *path;
355 RImage *image = NULL;
356 int error = 0;
358 /* If no new image, don't do nothing */
359 if (!file)
360 return True;
362 /* Find the new image */
363 path = FindImage(wPreferences.icon_path, file);
364 if (path)
365 image = get_rimage_from_file(scr, path, wPreferences.icon_size);
366 else
367 error = 1;
369 /* New image! */
370 if (!error && image) {
371 /* Set the new image */
372 set_icon_image_from_image(icon, image);
373 icon->file = wstrdup(path);
374 update_icon_pixmap(icon);
375 } else {
376 error = 1;
379 if (path)
380 wfree(path);
382 return !error;
385 static char *get_name_for_wwin(WWindow *wwin)
387 return get_name_for_instance_class(wwin->wm_instance, wwin->wm_class);
390 char *get_name_for_instance_class(char *wm_instance, char *wm_class)
392 char *suffix;
393 int len;
395 if (wm_class && wm_instance) {
396 len = strlen(wm_class) + strlen(wm_instance) + 2;
397 suffix = wmalloc(len);
398 snprintf(suffix, len, "%s.%s", wm_instance, wm_class);
399 } else if (wm_class) {
400 len = strlen(wm_class) + 1;
401 suffix = wmalloc(len);
402 snprintf(suffix, len, "%s", wm_class);
403 } else if (wm_instance) {
404 len = strlen(wm_instance) + 1;
405 suffix = wmalloc(len);
406 snprintf(suffix, len, "%s", wm_instance);
407 } else {
408 return NULL;
411 return suffix;
414 static char *get_icon_cache_path(void)
416 char *prefix, *path;
417 int len, ret;
419 prefix = wusergnusteppath();
420 len = strlen(prefix) + strlen(CACHE_ICON_PATH) + 2;
421 path = wmalloc(len);
422 snprintf(path, len, "%s%s/", prefix, CACHE_ICON_PATH);
424 /* If the folder exists, exit */
425 if (access(path, F_OK) == 0)
426 return path;
428 /* Create the folder */
429 ret = wmkdirhier((const char *) path);
431 /* Exit 1 on success, 0 on failure */
432 if (ret == 1)
433 return path;
435 /* Fail */
436 wfree(path);
437 return NULL;
440 static RImage *get_wwindow_image_from_wmhints(WWindow *wwin, WIcon *icon)
442 RImage *image = NULL;
443 XWMHints *hints = wwin->wm_hints;
445 if (hints && (hints->flags & IconPixmapHint) && hints->icon_pixmap != None)
446 image = RCreateImageFromDrawable(icon->core->screen_ptr->rcontext,
447 hints->icon_pixmap,
448 (hints->flags & IconMaskHint)
449 ? hints->icon_mask : None);
451 return image;
455 * wIconStore--
456 * Stores the client supplied icon at CACHE_ICON_PATH
457 * and returns the path for that icon. Returns NULL if there is no
458 * client supplied icon or on failure.
460 * Side effects:
461 * New directories might be created.
463 char *wIconStore(WIcon * icon)
465 char *path, *dir_path, *file;
466 int len = 0;
467 RImage *image = NULL;
468 WWindow *wwin = icon->owner;
470 if (!wwin)
471 return NULL;
473 dir_path = get_icon_cache_path();
474 if (!dir_path)
475 return NULL;
477 file = get_name_for_wwin(wwin);
478 if (!file) {
479 wfree(dir_path);
480 return NULL;
483 len = strlen(dir_path) + strlen(file) + 5;
484 path = wmalloc(len);
485 snprintf(path, len, "%s%s.xpm", dir_path, file);
486 wfree(dir_path);
487 wfree(file);
489 /* If icon exists, exit */
490 if (access(path, F_OK) == 0)
491 return path;
493 if (wwin->net_icon_image)
494 image = RRetainImage(wwin->net_icon_image);
495 else
496 image = get_wwindow_image_from_wmhints(wwin, icon);
498 if (!image) {
499 wfree(path);
500 return NULL;
503 if (!RSaveImage(image, path, "XPM")) {
504 wfree(path);
505 path = NULL;
508 RReleaseImage(image);
510 return path;
513 static void cycleColor(void *data)
515 WIcon *icon = (WIcon *) data;
516 WScreen *scr = icon->core->screen_ptr;
517 XGCValues gcv;
519 icon->step--;
520 gcv.dash_offset = icon->step;
521 XChangeGC(dpy, scr->icon_select_gc, GCDashOffset, &gcv);
523 XDrawRectangle(dpy, icon->core->window, scr->icon_select_gc, 0, 0,
524 icon->core->width - 1, icon->core->height - 1);
525 icon->handlerID = WMAddTimerHandler(COLOR_CYCLE_DELAY, cycleColor, icon);
528 void wIconSetHighlited(WIcon *icon, Bool flag)
530 if (icon->highlighted == flag)
531 return;
533 icon->highlighted = flag;
534 update_icon_pixmap(icon);
537 void wIconSelect(WIcon * icon)
539 WScreen *scr = icon->core->screen_ptr;
540 icon->selected = !icon->selected;
542 if (icon->selected) {
543 icon->step = 0;
544 if (!wPreferences.dont_blink)
545 icon->handlerID = WMAddTimerHandler(10, cycleColor, icon);
546 else
547 XDrawRectangle(dpy, icon->core->window, scr->icon_select_gc, 0, 0,
548 icon->core->width - 1, icon->core->height - 1);
549 } else {
550 if (icon->handlerID) {
551 WMDeleteTimerHandler(icon->handlerID);
552 icon->handlerID = NULL;
554 XClearArea(dpy, icon->core->window, 0, 0, icon->core->width, icon->core->height, True);
558 static void unset_icon_image(WIcon *icon)
560 if (icon->file) {
561 wfree(icon->file);
562 icon->file = NULL;
565 if (icon->file_image) {
566 RReleaseImage(icon->file_image);
567 icon->file_image = NULL;
571 void set_icon_image_from_image(WIcon *icon, RImage *image)
573 if (!icon)
574 return;
576 unset_icon_image(icon);
578 icon->file_image = NULL;
579 icon->file_image = image;
582 void wIconUpdate(WIcon *icon)
584 WWindow *wwin = NULL;
586 if (icon && icon->owner)
587 wwin = icon->owner;
589 if (wwin && WFLAGP(wwin, always_user_icon)) {
590 /* Forced use user_icon */
591 get_rimage_icon_from_user_icon(icon);
592 } else if (icon->icon_win != None) {
593 /* Get the Pixmap from the WIcon */
594 get_rimage_icon_from_icon_win(icon);
595 } else if (wwin && wwin->net_icon_image) {
596 /* Use _NET_WM_ICON icon */
597 get_rimage_icon_from_x11(icon);
598 } else if (wwin && wwin->wm_hints && (wwin->wm_hints->flags & IconPixmapHint)) {
599 /* Get the Pixmap from the wm_hints, else, from the user */
600 unset_icon_image(icon);
601 icon->file_image = get_rimage_icon_from_wm_hints(icon);
602 if (!icon->file_image)
603 get_rimage_icon_from_user_icon(icon);
604 } else {
605 /* Get the Pixmap from the user */
606 get_rimage_icon_from_user_icon(icon);
609 update_icon_pixmap(icon);
612 void update_icon_pixmap(WIcon *icon)
614 if (icon->pixmap != None)
615 XFreePixmap(dpy, icon->pixmap);
617 icon->pixmap = None;
619 /* Create the pixmap */
620 if (icon->file_image)
621 icon_update_pixmap(icon, icon->file_image);
623 /* If dockapp, put inside the icon */
624 if (icon->icon_win != None) {
625 /* file_image is NULL, because is docked app */
626 icon_update_pixmap(icon, NULL);
627 set_dockapp_in_icon(icon);
630 /* No pixmap, set default background */
631 if (icon->pixmap != None)
632 XSetWindowBackgroundPixmap(dpy, icon->core->window, icon->pixmap);
634 /* Paint it */
635 wIconPaint(icon);
638 static void get_rimage_icon_from_x11(WIcon *icon)
640 /* Remove the icon image */
641 unset_icon_image(icon);
643 /* Set the new icon image */
644 icon->file_image = RRetainImage(icon->owner->net_icon_image);
647 static void get_rimage_icon_from_user_icon(WIcon *icon)
649 if (icon->file_image)
650 return;
652 get_rimage_icon_from_default_icon(icon);
655 static void get_rimage_icon_from_default_icon(WIcon *icon)
657 WScreen *scr = icon->core->screen_ptr;
659 /* If the icon don't have image, we should use the default image. */
660 if (!scr->def_icon_rimage)
661 scr->def_icon_rimage = get_default_image(scr);
663 /* Remove the icon image */
664 unset_icon_image(icon);
666 /* Set the new icon image */
667 icon->file_image = RRetainImage(scr->def_icon_rimage);
670 /* Get the RImage from the WIcon of the WWindow */
671 static void get_rimage_icon_from_icon_win(WIcon *icon)
673 RImage *image;
675 /* Create the new RImage */
676 image = get_window_image_from_x11(icon->icon_win);
678 /* Free the icon info */
679 unset_icon_image(icon);
681 /* Set the new info */
682 icon->file_image = image;
685 /* Set the dockapp in the WIcon */
686 static void set_dockapp_in_icon(WIcon *icon)
688 XWindowAttributes attr;
689 WScreen *scr = icon->core->screen_ptr;
690 unsigned int w, h, d;
692 /* Reparent the dock application to the icon */
694 /* We need the application size to center it
695 * and show in the correct position */
696 getSize(icon->icon_win, &w, &h, &d);
698 /* Set the background pixmap */
699 XSetWindowBackgroundPixmap(dpy, icon->core->window, scr->icon_tile_pixmap);
701 /* Set the icon border */
702 XSetWindowBorderWidth(dpy, icon->icon_win, 0);
704 /* Put the dock application in the icon */
705 XReparentWindow(dpy, icon->icon_win, icon->core->window,
706 (wPreferences.icon_size - w) / 2,
707 (wPreferences.icon_size - h) / 2);
709 /* Show it and save */
710 XMapWindow(dpy, icon->icon_win);
711 XAddToSaveSet(dpy, icon->icon_win);
713 /* Needed to move the icon clicking on the application part */
714 if ((XGetWindowAttributes(dpy, icon->icon_win, &attr)) &&
715 (attr.all_event_masks & ButtonPressMask))
716 wHackedGrabButton(Button1, MOD_MASK, icon->core->window, True,
717 ButtonPressMask, GrabModeSync, GrabModeAsync,
718 None, wCursor[WCUR_ARROW]);
721 /* Get the RImage from the XWindow wm_hints */
722 RImage *get_rimage_icon_from_wm_hints(WIcon *icon)
724 RImage *image = NULL;
725 unsigned int w, h, d;
726 WWindow *wwin = icon->owner;
728 if (!getSize(wwin->wm_hints->icon_pixmap, &w, &h, &d)) {
729 icon->owner->wm_hints->flags &= ~IconPixmapHint;
730 return NULL;
733 image = get_wwindow_image_from_wmhints(wwin, icon);
734 if (!image)
735 return NULL;
737 /* Resize the icon to the wPreferences.icon_size size */
738 image = wIconValidateIconSize(image, wPreferences.icon_size);
740 return image;
743 /* This function updates in the screen the icon title */
744 static void update_icon_title(WIcon *icon)
746 WScreen *scr = icon->core->screen_ptr;
747 int x, l, w;
748 char *tmp;
750 /* draw the icon title */
751 if (icon->show_title && icon->icon_name != NULL) {
752 tmp = ShrinkString(scr->icon_title_font, icon->icon_name, wPreferences.icon_size - 4);
753 w = WMWidthOfString(scr->icon_title_font, tmp, l = strlen(tmp));
755 if (w > icon->core->width - 4)
756 x = (icon->core->width - 4) - w;
757 else
758 x = (icon->core->width - w) / 2;
760 WMDrawString(scr->wmscreen, icon->core->window, scr->icon_title_color,
761 scr->icon_title_font, x, 1, tmp, l);
762 wfree(tmp);
767 void wIconPaint(WIcon *icon)
769 if (!icon || !icon->core || !icon->core->screen_ptr)
770 return;
772 WScreen *scr = icon->core->screen_ptr;
774 XClearWindow(dpy, icon->core->window);
776 update_icon_title(icon);
778 if (icon->selected)
779 XDrawRectangle(dpy, icon->core->window, scr->icon_select_gc, 0, 0,
780 icon->core->width - 1, icon->core->height - 1);
783 /******************************************************************/
785 static void miniwindowExpose(WObjDescriptor * desc, XEvent * event)
787 wIconPaint(desc->parent);
790 static void miniwindowDblClick(WObjDescriptor * desc, XEvent * event)
792 WIcon *icon = desc->parent;
794 assert(icon->owner != NULL);
796 wDeiconifyWindow(icon->owner);
799 static void miniwindowMouseDown(WObjDescriptor * desc, XEvent * event)
801 WIcon *icon = desc->parent;
802 WWindow *wwin = icon->owner;
803 XEvent ev;
804 int x = wwin->icon_x, y = wwin->icon_y;
805 int dx = event->xbutton.x, dy = event->xbutton.y;
806 int grabbed = 0;
807 int clickButton = event->xbutton.button;
808 Bool hasMoved = False;
810 if (WCHECK_STATE(WSTATE_MODAL))
811 return;
813 if (IsDoubleClick(icon->core->screen_ptr, event)) {
814 miniwindowDblClick(desc, event);
815 return;
818 if (event->xbutton.button == Button1) {
819 if (event->xbutton.state & MOD_MASK)
820 wLowerFrame(icon->core);
821 else
822 wRaiseFrame(icon->core);
823 if (event->xbutton.state & ShiftMask) {
824 wIconSelect(icon);
825 wSelectWindow(icon->owner, !wwin->flags.selected);
827 } else if (event->xbutton.button == Button3) {
828 WObjDescriptor *desc;
830 OpenMiniwindowMenu(wwin, event->xbutton.x_root, event->xbutton.y_root);
832 /* allow drag select of menu */
833 desc = &wwin->screen_ptr->window_menu->menu->descriptor;
834 event->xbutton.send_event = True;
835 (*desc->handle_mousedown) (desc, event);
837 return;
840 if (XGrabPointer(dpy, icon->core->window, False, ButtonMotionMask
841 | ButtonReleaseMask | ButtonPressMask, GrabModeAsync,
842 GrabModeAsync, None, None, CurrentTime) != GrabSuccess) {
844 while (1) {
845 WMMaskEvent(dpy, PointerMotionMask | ButtonReleaseMask | ButtonPressMask
846 | ButtonMotionMask | ExposureMask, &ev);
847 switch (ev.type) {
848 case Expose:
849 WMHandleEvent(&ev);
850 break;
852 case MotionNotify:
853 hasMoved = True;
854 if (!grabbed) {
855 if (abs(dx - ev.xmotion.x) >= MOVE_THRESHOLD
856 || abs(dy - ev.xmotion.y) >= MOVE_THRESHOLD) {
857 XChangeActivePointerGrab(dpy, ButtonMotionMask
858 | ButtonReleaseMask | ButtonPressMask,
859 wCursor[WCUR_MOVE], CurrentTime);
860 grabbed = 1;
861 } else {
862 break;
865 x = ev.xmotion.x_root - dx;
866 y = ev.xmotion.y_root - dy;
867 XMoveWindow(dpy, icon->core->window, x, y);
868 break;
870 case ButtonPress:
871 break;
873 case ButtonRelease:
874 if (ev.xbutton.button != clickButton)
875 break;
877 if (wwin->icon_x != x || wwin->icon_y != y)
878 wwin->flags.icon_moved = 1;
880 XMoveWindow(dpy, icon->core->window, x, y);
882 wwin->icon_x = x;
883 wwin->icon_y = y;
884 XUngrabPointer(dpy, CurrentTime);
886 if (wPreferences.auto_arrange_icons)
887 wArrangeIcons(wwin->screen_ptr, True);
888 if (wPreferences.single_click && !hasMoved)
889 miniwindowDblClick(desc, event);
890 return;
896 void set_icon_image_from_database(WIcon *icon, char *wm_instance, char *wm_class, char *command)
898 char *file = NULL;
900 file = get_icon_filename(icon->core->screen_ptr, wm_instance, wm_class, command, False);
901 if (file) {
902 icon->file = wstrdup(file);
903 icon->file_image = get_rimage_from_file(icon->core->screen_ptr, icon->file, wPreferences.icon_size);
904 wfree(file);