Split get_pixmap_icon_from_wm_hints() into two
[wmaker-crm.git] / src / icon.c
blob3e46f04a5b0bfafdff2a5341d950cedf77946cc6
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 get_pixmap_icon_from_icon_win(WIcon *icon);
64 static int get_pixmap_icon_from_wm_hints(WIcon *icon);
65 static int get_rimage_icon_from_wm_hints(WIcon *icon);
66 static void get_pixmap_icon_from_user_icon(WIcon *icon);
67 static void get_rimage_icon_from_user_icon(WIcon *icon);
68 static void get_pixmap_icon_from_default_icon(WIcon *icon);
69 static void get_rimage_icon_from_default_icon(WIcon *icon);
71 static void icon_update_pixmap(WIcon *icon, RImage *image);
72 static void unset_icon_image(WIcon *icon);
74 static RImage *get_default_image(WScreen *scr);
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 icon->force_paint = 1;
85 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 icon->force_paint = 1;
97 wIconPaint(icon);
99 XClearArea(dpy, icon->core->window, 0, 0, 1, 1, True);
102 /************************************/
104 static int getSize(Drawable d, unsigned int *w, unsigned int *h, unsigned int *dep)
106 Window rjunk;
107 int xjunk, yjunk;
108 unsigned int bjunk;
110 return XGetGeometry(dpy, d, &rjunk, &xjunk, &yjunk, w, h, &bjunk, dep);
113 WIcon *icon_create_for_wwindow(WWindow *wwin)
115 WScreen *scr = wwin->screen_ptr;
116 WIcon *icon;
117 char *file;
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 icon->icon_name = wNETWMGetIconName(wwin->client_win);
140 if (icon->icon_name)
141 wwin->flags.net_has_icon_title = 1;
142 else
143 wGetIconName(dpy, wwin->client_win, &icon->icon_name);
145 /* Get the application icon, default included */
146 file = get_default_icon_filename(scr, wwin->wm_instance, wwin->wm_class, NULL, True);
147 if (file) {
148 icon->file = wstrdup(file);
149 icon->file_image = get_rimage_from_file(scr, icon->file, wPreferences.icon_size);
150 wfree(file);
153 icon->tile_type = TILE_NORMAL;
155 wIconUpdate(icon);
157 WMAddNotificationObserver(appearanceObserver, icon, WNIconAppearanceSettingsChanged, icon);
158 WMAddNotificationObserver(tileObserver, icon, WNIconTileSettingsChanged, icon);
160 return icon;
163 WIcon *icon_create_for_dock(WScreen *scr, char *command, char *wm_instance, char *wm_class, int tile)
165 WIcon *icon;
166 char *file = NULL;
168 icon = icon_create_core(scr, 0, 0);
170 /* Search the icon using instance and class, without default icon */
171 file = get_default_icon_filename(scr, wm_instance, wm_class, command, False);
172 if (file) {
173 icon->file = wstrdup(file);
174 icon->file_image = get_rimage_from_file(scr, icon->file, wPreferences.icon_size);
175 wfree(file);
178 icon->tile_type = tile;
180 wIconUpdate(icon);
182 WMAddNotificationObserver(appearanceObserver, icon, WNIconAppearanceSettingsChanged, icon);
183 WMAddNotificationObserver(tileObserver, icon, WNIconTileSettingsChanged, icon);
185 return icon;
188 static WIcon *icon_create_core(WScreen *scr, int coord_x, int coord_y)
190 WIcon *icon;
191 unsigned long vmask = 0;
192 XSetWindowAttributes attribs;
194 icon = wmalloc(sizeof(WIcon));
195 icon->core = wCoreCreateTopLevel(scr,
196 coord_x,
197 coord_y,
198 wPreferences.icon_size,
199 wPreferences.icon_size,
200 0, scr->w_depth, scr->w_visual, scr->w_colormap);
202 if (wPreferences.use_saveunders) {
203 vmask = CWSaveUnder;
204 attribs.save_under = True;
207 /* a white border for selecting it */
208 vmask |= CWBorderPixel;
209 attribs.border_pixel = scr->white_pixel;
211 XChangeWindowAttributes(dpy, icon->core->window, vmask, &attribs);
213 /* will be overriden if this is a application icon */
214 icon->core->descriptor.handle_mousedown = miniwindowMouseDown;
215 icon->core->descriptor.handle_expose = miniwindowExpose;
216 icon->core->descriptor.parent_type = WCLASS_MINIWINDOW;
217 icon->core->descriptor.parent = icon;
219 icon->core->stacking = wmalloc(sizeof(WStacking));
220 icon->core->stacking->above = NULL;
221 icon->core->stacking->under = NULL;
222 icon->core->stacking->window_level = NORMAL_ICON_LEVEL;
223 icon->core->stacking->child_of = NULL;
225 /* Icon image */
226 icon->file = NULL;
227 icon->file_image = NULL;
229 return icon;
232 void wIconDestroy(WIcon * icon)
234 WCoreWindow *core = icon->core;
235 WScreen *scr = core->screen_ptr;
237 WMRemoveNotificationObserver(icon);
239 if (icon->handlerID)
240 WMDeleteTimerHandler(icon->handlerID);
242 if (icon->icon_win) {
243 int x = 0, y = 0;
245 if (icon->owner) {
246 x = icon->owner->icon_x;
247 y = icon->owner->icon_y;
249 XUnmapWindow(dpy, icon->icon_win);
250 XReparentWindow(dpy, icon->icon_win, scr->root_win, x, y);
252 if (icon->icon_name)
253 XFree(icon->icon_name);
255 if (icon->pixmap)
256 XFreePixmap(dpy, icon->pixmap);
258 unset_icon_image(icon);
260 wCoreDestroy(icon->core);
261 wfree(icon);
264 static void drawIconTitle(WScreen * scr, Pixmap pixmap, int height)
266 XFillRectangle(dpy, pixmap, scr->icon_title_texture->normal_gc, 0, 0, wPreferences.icon_size, height + 1);
267 XDrawLine(dpy, pixmap, scr->icon_title_texture->light_gc, 0, 0, wPreferences.icon_size, 0);
268 XDrawLine(dpy, pixmap, scr->icon_title_texture->light_gc, 0, 0, 0, height + 1);
269 XDrawLine(dpy, pixmap, scr->icon_title_texture->dim_gc,
270 wPreferences.icon_size - 1, 0, wPreferences.icon_size - 1, height + 1);
273 static void icon_update_pixmap(WIcon *icon, RImage *image)
275 RImage *tile;
276 Pixmap pixmap;
277 int x, y, sx, sy;
278 unsigned w, h;
279 int theight = 0;
280 WScreen *scr = icon->core->screen_ptr;
281 int titled = icon->show_title;
283 if (icon->tile_type == TILE_NORMAL) {
284 tile = RCloneImage(scr->icon_tile);
285 } else {
286 assert(scr->clip_tile);
287 tile = RCloneImage(scr->clip_tile);
290 if (image) {
291 w = (image->width > wPreferences.icon_size)
292 ? wPreferences.icon_size : image->width;
293 x = (wPreferences.icon_size - w) / 2;
294 sx = (image->width - w) / 2;
296 if (titled)
297 theight = WMFontHeight(scr->icon_title_font);
299 h = (image->height + theight > wPreferences.icon_size
300 ? wPreferences.icon_size - theight : image->height);
301 y = theight + (wPreferences.icon_size - theight - h) / 2;
302 sy = (image->height - h) / 2;
304 RCombineArea(tile, image, sx, sy, w, h, x, y);
307 if (icon->shadowed) {
308 RColor color;
310 color.red = scr->icon_back_texture->light.red >> 8;
311 color.green = scr->icon_back_texture->light.green >> 8;
312 color.blue = scr->icon_back_texture->light.blue >> 8;
313 color.alpha = 150; /* about 60% */
314 RClearImage(tile, &color);
317 if (icon->highlighted) {
318 RColor color;
320 color.red = color.green = color.blue = 0;
321 color.alpha = 160;
322 RLightImage(tile, &color);
325 if (!RConvertImage(scr->rcontext, tile, &pixmap))
326 wwarning(_("error rendering image:%s"), RMessageForError(RErrorCode));
328 RReleaseImage(tile);
330 if (titled)
331 drawIconTitle(scr, pixmap, theight);
333 icon->pixmap = pixmap;
336 void wIconChangeTitle(WIcon * icon, char *new_title)
338 int changed;
340 changed = (new_title == NULL && icon->icon_name != NULL)
341 || (new_title != NULL && icon->icon_name == NULL);
343 if (icon->icon_name != NULL)
344 XFree(icon->icon_name);
346 icon->icon_name = new_title;
348 if (changed)
349 icon->force_paint = 1;
350 wIconPaint(icon);
353 RImage *wIconValidateIconSize(RImage *icon, int max_size)
355 RImage *nimage;
357 if (!icon)
358 return NULL;
360 /* We should hold "ICON_BORDER" (~2) pixels to include the icon border */
361 if ((icon->width - max_size) > -ICON_BORDER ||
362 (icon->height - max_size) > -ICON_BORDER) {
363 nimage = RScaleImage(icon, max_size - ICON_BORDER,
364 (icon->height * (max_size - ICON_BORDER) / icon->width));
365 RReleaseImage(icon);
366 icon = nimage;
369 return icon;
372 Bool wIconChangeImageFile(WIcon *icon, char *file)
374 WScreen *scr = icon->core->screen_ptr;
375 char *path;
376 RImage *image = NULL;
377 int error = 0;
379 /* If no new image, don't do nothing */
380 if (!file)
381 return True;
383 /* Find the new image */
384 path = FindImage(wPreferences.icon_path, file);
385 if (path)
386 image = get_rimage_from_file(scr, path, wPreferences.icon_size);
387 else
388 error = 1;
390 /* New image! */
391 if (!error && image) {
392 /* Remove the old one */
393 unset_icon_image(icon);
395 /* Set the new image */
396 icon->file_image = image;
397 icon->file = wstrdup(path);
398 wIconUpdate(icon);
399 } else {
400 error = 1;
403 if (path)
404 wfree(path);
406 return !error;
409 static char *get_name_for_wwin(WWindow *wwin)
411 return get_name_for_instance_class(wwin->wm_instance, wwin->wm_class);
414 char *get_name_for_instance_class(char *wm_instance, char *wm_class)
416 char *suffix;
417 int len;
419 if (wm_class && wm_instance) {
420 len = strlen(wm_class) + strlen(wm_instance) + 2;
421 suffix = wmalloc(len);
422 snprintf(suffix, len, "%s.%s", wm_instance, wm_class);
423 } else if (wm_class) {
424 len = strlen(wm_class) + 1;
425 suffix = wmalloc(len);
426 snprintf(suffix, len, "%s", wm_class);
427 } else if (wm_instance) {
428 len = strlen(wm_instance) + 1;
429 suffix = wmalloc(len);
430 snprintf(suffix, len, "%s", wm_instance);
431 } else {
432 return NULL;
435 return suffix;
438 static char *get_icon_cache_path(void)
440 char *prefix, *path;
441 int len, ret;
443 prefix = wusergnusteppath();
444 len = strlen(prefix) + strlen(CACHE_ICON_PATH) + 2;
445 path = wmalloc(len);
446 snprintf(path, len, "%s%s/", prefix, CACHE_ICON_PATH);
448 /* If the folder exists, exit */
449 if (access(path, F_OK) == 0)
450 return path;
452 /* Create the folder */
453 ret = wmkdirhier((const char *) path);
455 /* Exit 1 on success, 0 on failure */
456 if (ret == 1)
457 return path;
459 /* Fail */
460 wfree(path);
461 return NULL;
464 static RImage *get_wwindow_image_from_wmhints(WWindow *wwin, WIcon *icon)
466 RImage *image = NULL;
467 XWMHints *hints = wwin->wm_hints;
469 if (hints && (hints->flags & IconPixmapHint) && hints->icon_pixmap != None)
470 image = RCreateImageFromDrawable(icon->core->screen_ptr->rcontext,
471 hints->icon_pixmap,
472 (hints->flags & IconMaskHint)
473 ? hints->icon_mask : None);
475 return image;
479 * wIconStore--
480 * Stores the client supplied icon at CACHE_ICON_PATH
481 * and returns the path for that icon. Returns NULL if there is no
482 * client supplied icon or on failure.
484 * Side effects:
485 * New directories might be created.
487 char *wIconStore(WIcon * icon)
489 char *path, *dir_path, *file;
490 int len = 0;
491 RImage *image = NULL;
492 WWindow *wwin = icon->owner;
494 if (!wwin)
495 return NULL;
497 dir_path = get_icon_cache_path();
498 if (!dir_path)
499 return NULL;
501 file = get_name_for_wwin(wwin);
502 if (!file) {
503 wfree(dir_path);
504 return NULL;
507 len = strlen(dir_path) + strlen(file) + 5;
508 path = wmalloc(len);
509 snprintf(path, len, "%s%s.xpm", dir_path, file);
510 wfree(dir_path);
511 wfree(file);
513 /* If icon exists, exit */
514 if (access(path, F_OK) == 0)
515 return path;
517 if (wwin->net_icon_image)
518 image = RRetainImage(wwin->net_icon_image);
519 else
520 image = get_wwindow_image_from_wmhints(wwin, icon);
522 if (!image) {
523 wfree(path);
524 return NULL;
527 if (!RSaveImage(image, path, "XPM")) {
528 wfree(path);
529 path = NULL;
532 RReleaseImage(image);
534 return path;
537 static void cycleColor(void *data)
539 WIcon *icon = (WIcon *) data;
540 WScreen *scr = icon->core->screen_ptr;
541 XGCValues gcv;
543 icon->step--;
544 gcv.dash_offset = icon->step;
545 XChangeGC(dpy, scr->icon_select_gc, GCDashOffset, &gcv);
547 XDrawRectangle(dpy, icon->core->window, scr->icon_select_gc, 0, 0,
548 icon->core->width - 1, icon->core->height - 1);
549 icon->handlerID = WMAddTimerHandler(COLOR_CYCLE_DELAY, cycleColor, icon);
552 #ifdef NEWAPPICON
553 void wIconSetHighlited(WIcon *icon, Bool flag)
555 if (icon->highlighted == flag)
556 return;
558 icon->highlighted = flag;
559 icon->force_paint = True;
560 wIconPaint(icon);
562 #endif
564 void wIconSelect(WIcon * icon)
566 WScreen *scr = icon->core->screen_ptr;
567 icon->selected = !icon->selected;
569 if (icon->selected) {
570 icon->step = 0;
571 if (!wPreferences.dont_blink)
572 icon->handlerID = WMAddTimerHandler(10, cycleColor, icon);
573 else
574 XDrawRectangle(dpy, icon->core->window, scr->icon_select_gc, 0, 0,
575 icon->core->width - 1, icon->core->height - 1);
576 } else {
577 if (icon->handlerID) {
578 WMDeleteTimerHandler(icon->handlerID);
579 icon->handlerID = NULL;
581 XClearArea(dpy, icon->core->window, 0, 0, icon->core->width, icon->core->height, True);
585 static void unset_icon_image(WIcon *icon)
587 if (icon->file)
588 wfree(icon->file);
590 if (icon->file_image)
591 RReleaseImage(icon->file_image);
594 void wIconUpdate(WIcon *icon)
596 WScreen *scr = icon->core->screen_ptr;
597 WWindow *wwin = icon->owner;
599 assert(scr->icon_tile != NULL);
601 if (icon->pixmap != None)
602 XFreePixmap(dpy, icon->pixmap);
604 icon->pixmap = None;
606 if (wwin && WFLAGP(wwin, always_user_icon)) {
607 /* Forced use user_icon */
608 get_pixmap_icon_from_user_icon(icon);
609 } else if (icon->icon_win != None) {
610 /* Get the Pixmap from the WIcon */
611 get_pixmap_icon_from_icon_win(icon);
612 } else if (wwin && wwin->net_icon_image) {
613 /* Use _NET_WM_ICON icon */
614 icon_update_pixmap(icon, wwin->net_icon_image);
615 } else if (wwin && wwin->wm_hints && (wwin->wm_hints->flags & IconPixmapHint)) {
616 /* Get the Pixmap from the wm_hints, else, from the user */
617 if (get_pixmap_icon_from_wm_hints(icon))
618 get_pixmap_icon_from_user_icon(icon);
619 } else {
620 /* Get the Pixmap from the user */
621 get_pixmap_icon_from_user_icon(icon);
624 /* No pixmap, set default background */
625 if (icon->pixmap != None)
626 XSetWindowBackgroundPixmap(dpy, icon->core->window, icon->pixmap);
628 /* Paint it */
629 XClearWindow(dpy, icon->core->window);
630 wIconPaint(icon);
633 static void get_rimage_icon_from_user_icon(WIcon *icon)
635 if (icon->file_image)
636 return;
638 get_rimage_icon_from_default_icon(icon);
641 static void get_pixmap_icon_from_user_icon(WIcon *icon)
643 /* Set the icon->file_image */
644 get_rimage_icon_from_user_icon(icon);
646 /* Update icon->pixmap */
647 icon_update_pixmap(icon, icon->file_image);
650 static void get_rimage_icon_from_default_icon(WIcon *icon)
652 WScreen *scr = icon->core->screen_ptr;
654 /* If the icon don't have image, we should use the default image. */
655 if (!scr->def_icon_rimage)
656 scr->def_icon_rimage = get_default_image(scr);
658 /* Remove the icon image */
659 unset_icon_image(icon);
661 /* Set the new icon image */
662 icon->file = NULL;
663 icon->file_image = RRetainImage(scr->def_icon_rimage);
666 static void get_pixmap_icon_from_default_icon(WIcon *icon)
668 /* Update icon->file image */
669 get_rimage_icon_from_default_icon(icon);
671 /* Now, create the pixmap using the default (saved) image */
672 icon_update_pixmap(icon, icon->file_image);
675 /* This function creates the RImage using the default icon */
676 static RImage *get_default_image(WScreen *scr)
678 RImage *image = NULL;
679 char *path, *file;
681 /* Get the default icon */
682 file = wDefaultGetIconFile(NULL, NULL, True);
683 if (file) {
684 path = FindImage(wPreferences.icon_path, file);
685 image = get_rimage_from_file(scr, path, wPreferences.icon_size);
687 if (!image)
688 wwarning(_("could not find default icon \"%s\""), file);
690 wfree(file);
693 return image;
696 /* Get the Pixmap from the WIcon of the WWindow */
697 static void get_pixmap_icon_from_icon_win(WIcon * icon)
699 XWindowAttributes attr;
700 WScreen *scr = icon->core->screen_ptr;
701 int title_height = WMFontHeight(scr->icon_title_font);
702 unsigned int width, height, depth;
703 int theight;
704 int resize = 0;
705 Pixmap pixmap;
707 getSize(icon->icon_win, &width, &height, &depth);
709 if (width > wPreferences.icon_size) {
710 resize = 1;
711 width = wPreferences.icon_size;
714 if (height > wPreferences.icon_size) {
715 resize = 1;
716 height = wPreferences.icon_size;
719 if (icon->show_title && (height + title_height < wPreferences.icon_size)) {
720 pixmap = XCreatePixmap(dpy, scr->w_win, wPreferences.icon_size,
721 wPreferences.icon_size, scr->w_depth);
722 XSetClipMask(dpy, scr->copy_gc, None);
723 XCopyArea(dpy, scr->icon_tile_pixmap, pixmap, scr->copy_gc, 0, 0,
724 wPreferences.icon_size, wPreferences.icon_size, 0, 0);
725 drawIconTitle(scr, pixmap, title_height);
726 theight = title_height;
727 } else {
728 pixmap = None;
729 theight = 0;
730 XSetWindowBackgroundPixmap(dpy, icon->core->window, scr->icon_tile_pixmap);
733 XSetWindowBorderWidth(dpy, icon->icon_win, 0);
734 XReparentWindow(dpy, icon->icon_win, icon->core->window,
735 (wPreferences.icon_size - width) / 2,
736 theight + (wPreferences.icon_size - height - theight) / 2);
737 if (resize)
738 XResizeWindow(dpy, icon->icon_win, width, height);
740 XMapWindow(dpy, icon->icon_win);
741 XAddToSaveSet(dpy, icon->icon_win);
743 /* Save it */
744 icon->pixmap = pixmap;
746 if ((XGetWindowAttributes(dpy, icon->icon_win, &attr)) &&
747 (attr.all_event_masks & ButtonPressMask))
748 wHackedGrabButton(Button1, MOD_MASK, icon->core->window, True,
749 ButtonPressMask, GrabModeSync, GrabModeAsync,
750 None, wCursor[WCUR_ARROW]);
753 /* Get the RImage from the XWindow wm_hints */
754 static int get_rimage_icon_from_wm_hints(WIcon *icon)
756 RImage *image = NULL;
757 unsigned int w, h, d;
758 WWindow *wwin = icon->owner;
760 if (!getSize(wwin->wm_hints->icon_pixmap, &w, &h, &d)) {
761 icon->owner->wm_hints->flags &= ~IconPixmapHint;
762 return 1;
765 image = get_wwindow_image_from_wmhints(wwin, icon);
766 if (!image)
767 return 1;
769 /* FIXME: If unset_icon_image, pointer double free then crash
770 unset_icon_image(icon); */
771 icon->file_image = image;
773 return 0;
776 /* Get the Pixmap from the XWindow wm_hints */
777 static int get_pixmap_icon_from_wm_hints(WIcon *icon)
779 int ret;
781 ret = get_rimage_icon_from_wm_hints(icon);
782 if (ret == 0)
783 icon_update_pixmap(icon, icon->file_image);
785 return ret;
788 void wIconPaint(WIcon * icon)
790 WScreen *scr = icon->core->screen_ptr;
791 int x;
792 char *tmp;
794 if (icon->force_paint) {
795 icon->force_paint = 0;
796 wIconUpdate(icon);
797 return;
800 XClearWindow(dpy, icon->core->window);
802 /* draw the icon title */
803 if (icon->show_title && icon->icon_name != NULL) {
804 int l;
805 int w;
807 tmp = ShrinkString(scr->icon_title_font, icon->icon_name, wPreferences.icon_size - 4);
808 w = WMWidthOfString(scr->icon_title_font, tmp, l = strlen(tmp));
810 if (w > icon->core->width - 4)
811 x = (icon->core->width - 4) - w;
812 else
813 x = (icon->core->width - w) / 2;
815 WMDrawString(scr->wmscreen, icon->core->window, scr->icon_title_color,
816 scr->icon_title_font, x, 1, tmp, l);
817 wfree(tmp);
820 if (icon->selected)
821 XDrawRectangle(dpy, icon->core->window, scr->icon_select_gc, 0, 0,
822 icon->core->width - 1, icon->core->height - 1);
825 /******************************************************************/
827 static void miniwindowExpose(WObjDescriptor * desc, XEvent * event)
829 wIconPaint(desc->parent);
832 static void miniwindowDblClick(WObjDescriptor * desc, XEvent * event)
834 WIcon *icon = desc->parent;
836 assert(icon->owner != NULL);
838 wDeiconifyWindow(icon->owner);
841 static void miniwindowMouseDown(WObjDescriptor * desc, XEvent * event)
843 WIcon *icon = desc->parent;
844 WWindow *wwin = icon->owner;
845 XEvent ev;
846 int x = wwin->icon_x, y = wwin->icon_y;
847 int dx = event->xbutton.x, dy = event->xbutton.y;
848 int grabbed = 0;
849 int clickButton = event->xbutton.button;
850 Bool hasMoved = False;
852 if (WCHECK_STATE(WSTATE_MODAL))
853 return;
855 if (IsDoubleClick(icon->core->screen_ptr, event)) {
856 miniwindowDblClick(desc, event);
857 return;
860 if (event->xbutton.button == Button1) {
861 if (event->xbutton.state & MOD_MASK)
862 wLowerFrame(icon->core);
863 else
864 wRaiseFrame(icon->core);
865 if (event->xbutton.state & ShiftMask) {
866 wIconSelect(icon);
867 wSelectWindow(icon->owner, !wwin->flags.selected);
869 } else if (event->xbutton.button == Button3) {
870 WObjDescriptor *desc;
872 OpenMiniwindowMenu(wwin, event->xbutton.x_root, event->xbutton.y_root);
874 /* allow drag select of menu */
875 desc = &wwin->screen_ptr->window_menu->menu->descriptor;
876 event->xbutton.send_event = True;
877 (*desc->handle_mousedown) (desc, event);
879 return;
882 if (XGrabPointer(dpy, icon->core->window, False, ButtonMotionMask
883 | ButtonReleaseMask | ButtonPressMask, GrabModeAsync,
884 GrabModeAsync, None, None, CurrentTime) != GrabSuccess) {
886 while (1) {
887 WMMaskEvent(dpy, PointerMotionMask | ButtonReleaseMask | ButtonPressMask
888 | ButtonMotionMask | ExposureMask, &ev);
889 switch (ev.type) {
890 case Expose:
891 WMHandleEvent(&ev);
892 break;
894 case MotionNotify:
895 hasMoved = True;
896 if (!grabbed) {
897 if (abs(dx - ev.xmotion.x) >= MOVE_THRESHOLD
898 || abs(dy - ev.xmotion.y) >= MOVE_THRESHOLD) {
899 XChangeActivePointerGrab(dpy, ButtonMotionMask
900 | ButtonReleaseMask | ButtonPressMask,
901 wCursor[WCUR_MOVE], CurrentTime);
902 grabbed = 1;
903 } else {
904 break;
907 x = ev.xmotion.x_root - dx;
908 y = ev.xmotion.y_root - dy;
909 XMoveWindow(dpy, icon->core->window, x, y);
910 break;
912 case ButtonPress:
913 break;
915 case ButtonRelease:
916 if (ev.xbutton.button != clickButton)
917 break;
919 if (wwin->icon_x != x || wwin->icon_y != y)
920 wwin->flags.icon_moved = 1;
922 XMoveWindow(dpy, icon->core->window, x, y);
924 wwin->icon_x = x;
925 wwin->icon_y = y;
926 XUngrabPointer(dpy, CurrentTime);
928 if (wPreferences.auto_arrange_icons)
929 wArrangeIcons(wwin->screen_ptr, True);
930 if (wPreferences.single_click && !hasMoved)
931 miniwindowDblClick(desc, event);
932 return;