get_pixmap_icon_from_icon_win rewritten
[wmaker-crm.git] / src / icon.c
blob6aca13b4f6fea0f0a8bcf74c1d8e36a341fd32bb
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);
70 static void get_pixmap_icon_from_x11(WIcon *icon);
71 static void get_rimage_icon_from_x11(WIcon *icon);
73 static void icon_update_pixmap(WIcon *icon, RImage *image);
74 static void unset_icon_image(WIcon *icon);
76 static RImage *get_default_image(WScreen *scr);
77 /****** Notification Observers ******/
79 static void appearanceObserver(void *self, WMNotification * notif)
81 WIcon *icon = (WIcon *) self;
82 uintptr_t flags = (uintptr_t)WMGetNotificationClientData(notif);
84 if ((flags & WTextureSettings) || (flags & WFontSettings))
85 icon->force_paint = 1;
87 wIconPaint(icon);
89 /* so that the appicon expose handlers will paint the appicon specific
90 * stuff */
91 XClearArea(dpy, icon->core->window, 0, 0, icon->core->width, icon->core->height, True);
94 static void tileObserver(void *self, WMNotification * notif)
96 WIcon *icon = (WIcon *) self;
98 icon->force_paint = 1;
99 wIconPaint(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;
119 char *file;
121 icon = icon_create_core(scr, wwin->icon_x, wwin->icon_y);
123 icon->owner = wwin;
124 if (wwin->wm_hints && (wwin->wm_hints->flags & IconWindowHint)) {
125 if (wwin->client_win == wwin->main_window) {
126 WApplication *wapp;
127 /* do not let miniwindow steal app-icon's icon window */
128 wapp = wApplicationOf(wwin->client_win);
129 if (!wapp || wapp->app_icon == NULL)
130 icon->icon_win = wwin->wm_hints->icon_window;
131 } else {
132 icon->icon_win = wwin->wm_hints->icon_window;
135 #ifdef NO_MINIWINDOW_TITLES
136 icon->show_title = 0;
137 #else
138 icon->show_title = 1;
139 #endif
141 icon->icon_name = wNETWMGetIconName(wwin->client_win);
142 if (icon->icon_name)
143 wwin->flags.net_has_icon_title = 1;
144 else
145 wGetIconName(dpy, wwin->client_win, &icon->icon_name);
147 /* Get the application icon, default included */
148 file = get_default_icon_filename(scr, wwin->wm_instance, wwin->wm_class, NULL, True);
149 if (file) {
150 icon->file = wstrdup(file);
151 icon->file_image = get_rimage_from_file(scr, icon->file, wPreferences.icon_size);
152 wfree(file);
155 icon->tile_type = TILE_NORMAL;
157 wIconUpdate(icon);
159 WMAddNotificationObserver(appearanceObserver, icon, WNIconAppearanceSettingsChanged, icon);
160 WMAddNotificationObserver(tileObserver, icon, WNIconTileSettingsChanged, icon);
162 return icon;
165 WIcon *icon_create_for_dock(WScreen *scr, char *command, char *wm_instance, char *wm_class, int tile)
167 WIcon *icon;
168 char *file = NULL;
170 icon = icon_create_core(scr, 0, 0);
172 /* Search the icon using instance and class, without default icon */
173 file = get_default_icon_filename(scr, wm_instance, wm_class, command, False);
174 if (file) {
175 icon->file = wstrdup(file);
176 icon->file_image = get_rimage_from_file(scr, icon->file, wPreferences.icon_size);
177 wfree(file);
180 icon->tile_type = tile;
182 wIconUpdate(icon);
184 WMAddNotificationObserver(appearanceObserver, icon, WNIconAppearanceSettingsChanged, icon);
185 WMAddNotificationObserver(tileObserver, icon, WNIconTileSettingsChanged, icon);
187 return icon;
190 static WIcon *icon_create_core(WScreen *scr, int coord_x, int coord_y)
192 WIcon *icon;
193 unsigned long vmask = 0;
194 XSetWindowAttributes attribs;
196 icon = wmalloc(sizeof(WIcon));
197 icon->core = wCoreCreateTopLevel(scr,
198 coord_x,
199 coord_y,
200 wPreferences.icon_size,
201 wPreferences.icon_size,
202 0, scr->w_depth, scr->w_visual, scr->w_colormap);
204 if (wPreferences.use_saveunders) {
205 vmask = CWSaveUnder;
206 attribs.save_under = True;
209 /* a white border for selecting it */
210 vmask |= CWBorderPixel;
211 attribs.border_pixel = scr->white_pixel;
213 XChangeWindowAttributes(dpy, icon->core->window, vmask, &attribs);
215 /* will be overriden if this is a application icon */
216 icon->core->descriptor.handle_mousedown = miniwindowMouseDown;
217 icon->core->descriptor.handle_expose = miniwindowExpose;
218 icon->core->descriptor.parent_type = WCLASS_MINIWINDOW;
219 icon->core->descriptor.parent = icon;
221 icon->core->stacking = wmalloc(sizeof(WStacking));
222 icon->core->stacking->above = NULL;
223 icon->core->stacking->under = NULL;
224 icon->core->stacking->window_level = NORMAL_ICON_LEVEL;
225 icon->core->stacking->child_of = NULL;
227 /* Icon image */
228 icon->file = NULL;
229 icon->file_image = NULL;
231 return icon;
234 void wIconDestroy(WIcon * icon)
236 WCoreWindow *core = icon->core;
237 WScreen *scr = core->screen_ptr;
239 WMRemoveNotificationObserver(icon);
241 if (icon->handlerID)
242 WMDeleteTimerHandler(icon->handlerID);
244 if (icon->icon_win) {
245 int x = 0, y = 0;
247 if (icon->owner) {
248 x = icon->owner->icon_x;
249 y = icon->owner->icon_y;
251 XUnmapWindow(dpy, icon->icon_win);
252 XReparentWindow(dpy, icon->icon_win, scr->root_win, x, y);
254 if (icon->icon_name)
255 XFree(icon->icon_name);
257 if (icon->pixmap)
258 XFreePixmap(dpy, icon->pixmap);
260 unset_icon_image(icon);
262 wCoreDestroy(icon->core);
263 wfree(icon);
266 static void drawIconTitle(WScreen * scr, Pixmap pixmap, int height)
268 XFillRectangle(dpy, pixmap, scr->icon_title_texture->normal_gc, 0, 0, wPreferences.icon_size, height + 1);
269 XDrawLine(dpy, pixmap, scr->icon_title_texture->light_gc, 0, 0, wPreferences.icon_size, 0);
270 XDrawLine(dpy, pixmap, scr->icon_title_texture->light_gc, 0, 0, 0, height + 1);
271 XDrawLine(dpy, pixmap, scr->icon_title_texture->dim_gc,
272 wPreferences.icon_size - 1, 0, wPreferences.icon_size - 1, height + 1);
275 static void icon_update_pixmap(WIcon *icon, RImage *image)
277 RImage *tile;
278 Pixmap pixmap;
279 int x, y, sx, sy;
280 unsigned w, h;
281 int theight = 0;
282 WScreen *scr = icon->core->screen_ptr;
283 int titled = icon->show_title;
285 if (icon->tile_type == TILE_NORMAL) {
286 tile = RCloneImage(scr->icon_tile);
287 } else {
288 assert(scr->clip_tile);
289 tile = RCloneImage(scr->clip_tile);
292 if (image) {
293 w = (image->width > wPreferences.icon_size)
294 ? wPreferences.icon_size : image->width;
295 x = (wPreferences.icon_size - w) / 2;
296 sx = (image->width - w) / 2;
298 if (titled)
299 theight = WMFontHeight(scr->icon_title_font);
301 h = (image->height + theight > wPreferences.icon_size
302 ? wPreferences.icon_size - theight : image->height);
303 y = theight + (wPreferences.icon_size - theight - h) / 2;
304 sy = (image->height - h) / 2;
306 RCombineArea(tile, image, sx, sy, w, h, x, y);
309 if (icon->shadowed) {
310 RColor color;
312 color.red = scr->icon_back_texture->light.red >> 8;
313 color.green = scr->icon_back_texture->light.green >> 8;
314 color.blue = scr->icon_back_texture->light.blue >> 8;
315 color.alpha = 150; /* about 60% */
316 RClearImage(tile, &color);
319 if (icon->highlighted) {
320 RColor color;
322 color.red = color.green = color.blue = 0;
323 color.alpha = 160;
324 RLightImage(tile, &color);
327 if (!RConvertImage(scr->rcontext, tile, &pixmap))
328 wwarning(_("error rendering image:%s"), RMessageForError(RErrorCode));
330 RReleaseImage(tile);
332 if (titled)
333 drawIconTitle(scr, pixmap, theight);
335 icon->pixmap = pixmap;
338 void wIconChangeTitle(WIcon * icon, char *new_title)
340 int changed;
342 changed = (new_title == NULL && icon->icon_name != NULL)
343 || (new_title != NULL && icon->icon_name == NULL);
345 if (icon->icon_name != NULL)
346 XFree(icon->icon_name);
348 icon->icon_name = new_title;
350 if (changed)
351 icon->force_paint = 1;
352 wIconPaint(icon);
355 RImage *wIconValidateIconSize(RImage *icon, int max_size)
357 RImage *nimage;
359 if (!icon)
360 return NULL;
362 /* We should hold "ICON_BORDER" (~2) pixels to include the icon border */
363 if ((icon->width - max_size) > -ICON_BORDER ||
364 (icon->height - max_size) > -ICON_BORDER) {
365 nimage = RScaleImage(icon, max_size - ICON_BORDER,
366 (icon->height * (max_size - ICON_BORDER) / icon->width));
367 RReleaseImage(icon);
368 icon = nimage;
371 return icon;
374 Bool wIconChangeImageFile(WIcon *icon, char *file)
376 WScreen *scr = icon->core->screen_ptr;
377 char *path;
378 RImage *image = NULL;
379 int error = 0;
381 /* If no new image, don't do nothing */
382 if (!file)
383 return True;
385 /* Find the new image */
386 path = FindImage(wPreferences.icon_path, file);
387 if (path)
388 image = get_rimage_from_file(scr, path, wPreferences.icon_size);
389 else
390 error = 1;
392 /* New image! */
393 if (!error && image) {
394 /* Remove the old one */
395 unset_icon_image(icon);
397 /* Set the new image */
398 icon->file_image = image;
399 icon->file = wstrdup(path);
400 wIconUpdate(icon);
401 } else {
402 error = 1;
405 if (path)
406 wfree(path);
408 return !error;
411 static char *get_name_for_wwin(WWindow *wwin)
413 return get_name_for_instance_class(wwin->wm_instance, wwin->wm_class);
416 char *get_name_for_instance_class(char *wm_instance, char *wm_class)
418 char *suffix;
419 int len;
421 if (wm_class && wm_instance) {
422 len = strlen(wm_class) + strlen(wm_instance) + 2;
423 suffix = wmalloc(len);
424 snprintf(suffix, len, "%s.%s", wm_instance, wm_class);
425 } else if (wm_class) {
426 len = strlen(wm_class) + 1;
427 suffix = wmalloc(len);
428 snprintf(suffix, len, "%s", wm_class);
429 } else if (wm_instance) {
430 len = strlen(wm_instance) + 1;
431 suffix = wmalloc(len);
432 snprintf(suffix, len, "%s", wm_instance);
433 } else {
434 return NULL;
437 return suffix;
440 static char *get_icon_cache_path(void)
442 char *prefix, *path;
443 int len, ret;
445 prefix = wusergnusteppath();
446 len = strlen(prefix) + strlen(CACHE_ICON_PATH) + 2;
447 path = wmalloc(len);
448 snprintf(path, len, "%s%s/", prefix, CACHE_ICON_PATH);
450 /* If the folder exists, exit */
451 if (access(path, F_OK) == 0)
452 return path;
454 /* Create the folder */
455 ret = wmkdirhier((const char *) path);
457 /* Exit 1 on success, 0 on failure */
458 if (ret == 1)
459 return path;
461 /* Fail */
462 wfree(path);
463 return NULL;
466 static RImage *get_wwindow_image_from_wmhints(WWindow *wwin, WIcon *icon)
468 RImage *image = NULL;
469 XWMHints *hints = wwin->wm_hints;
471 if (hints && (hints->flags & IconPixmapHint) && hints->icon_pixmap != None)
472 image = RCreateImageFromDrawable(icon->core->screen_ptr->rcontext,
473 hints->icon_pixmap,
474 (hints->flags & IconMaskHint)
475 ? hints->icon_mask : None);
477 return image;
481 * wIconStore--
482 * Stores the client supplied icon at CACHE_ICON_PATH
483 * and returns the path for that icon. Returns NULL if there is no
484 * client supplied icon or on failure.
486 * Side effects:
487 * New directories might be created.
489 char *wIconStore(WIcon * icon)
491 char *path, *dir_path, *file;
492 int len = 0;
493 RImage *image = NULL;
494 WWindow *wwin = icon->owner;
496 if (!wwin)
497 return NULL;
499 dir_path = get_icon_cache_path();
500 if (!dir_path)
501 return NULL;
503 file = get_name_for_wwin(wwin);
504 if (!file) {
505 wfree(dir_path);
506 return NULL;
509 len = strlen(dir_path) + strlen(file) + 5;
510 path = wmalloc(len);
511 snprintf(path, len, "%s%s.xpm", dir_path, file);
512 wfree(dir_path);
513 wfree(file);
515 /* If icon exists, exit */
516 if (access(path, F_OK) == 0)
517 return path;
519 if (wwin->net_icon_image)
520 image = RRetainImage(wwin->net_icon_image);
521 else
522 image = get_wwindow_image_from_wmhints(wwin, icon);
524 if (!image) {
525 wfree(path);
526 return NULL;
529 if (!RSaveImage(image, path, "XPM")) {
530 wfree(path);
531 path = NULL;
534 RReleaseImage(image);
536 return path;
539 static void cycleColor(void *data)
541 WIcon *icon = (WIcon *) data;
542 WScreen *scr = icon->core->screen_ptr;
543 XGCValues gcv;
545 icon->step--;
546 gcv.dash_offset = icon->step;
547 XChangeGC(dpy, scr->icon_select_gc, GCDashOffset, &gcv);
549 XDrawRectangle(dpy, icon->core->window, scr->icon_select_gc, 0, 0,
550 icon->core->width - 1, icon->core->height - 1);
551 icon->handlerID = WMAddTimerHandler(COLOR_CYCLE_DELAY, cycleColor, icon);
554 #ifdef NEWAPPICON
555 void wIconSetHighlited(WIcon *icon, Bool flag)
557 if (icon->highlighted == flag)
558 return;
560 icon->highlighted = flag;
561 icon->force_paint = True;
562 wIconPaint(icon);
564 #endif
566 void wIconSelect(WIcon * icon)
568 WScreen *scr = icon->core->screen_ptr;
569 icon->selected = !icon->selected;
571 if (icon->selected) {
572 icon->step = 0;
573 if (!wPreferences.dont_blink)
574 icon->handlerID = WMAddTimerHandler(10, cycleColor, icon);
575 else
576 XDrawRectangle(dpy, icon->core->window, scr->icon_select_gc, 0, 0,
577 icon->core->width - 1, icon->core->height - 1);
578 } else {
579 if (icon->handlerID) {
580 WMDeleteTimerHandler(icon->handlerID);
581 icon->handlerID = NULL;
583 XClearArea(dpy, icon->core->window, 0, 0, icon->core->width, icon->core->height, True);
587 static void unset_icon_image(WIcon *icon)
589 if (icon->file)
590 wfree(icon->file);
592 if (icon->file_image)
593 RReleaseImage(icon->file_image);
596 void wIconUpdate(WIcon *icon)
598 WScreen *scr = icon->core->screen_ptr;
599 WWindow *wwin = icon->owner;
601 assert(scr->icon_tile != NULL);
603 if (icon->pixmap != None)
604 XFreePixmap(dpy, icon->pixmap);
606 icon->pixmap = None;
608 if (wwin && WFLAGP(wwin, always_user_icon)) {
609 /* Forced use user_icon */
610 get_pixmap_icon_from_user_icon(icon);
611 } else if (icon->icon_win != None) {
612 /* Get the Pixmap from the WIcon */
613 get_pixmap_icon_from_icon_win(icon);
614 } else if (wwin && wwin->net_icon_image) {
615 /* Use _NET_WM_ICON icon */
616 get_pixmap_icon_from_x11(icon);
617 } else if (wwin && wwin->wm_hints && (wwin->wm_hints->flags & IconPixmapHint)) {
618 /* Get the Pixmap from the wm_hints, else, from the user */
619 if (get_pixmap_icon_from_wm_hints(icon))
620 get_pixmap_icon_from_user_icon(icon);
621 } else {
622 /* Get the Pixmap from the user */
623 get_pixmap_icon_from_user_icon(icon);
626 /* No pixmap, set default background */
627 if (icon->pixmap != None)
628 XSetWindowBackgroundPixmap(dpy, icon->core->window, icon->pixmap);
630 /* Paint it */
631 XClearWindow(dpy, icon->core->window);
632 wIconPaint(icon);
635 static void get_pixmap_icon_from_x11(WIcon *icon)
637 /* Set the icon->file_image */
638 get_rimage_icon_from_x11(icon);
640 /* Update icon->pixmap */
641 icon_update_pixmap(icon, icon->file_image);
644 static void get_rimage_icon_from_x11(WIcon *icon)
646 /* Remove the icon image */
647 unset_icon_image(icon);
649 /* Set the new icon image */
650 icon->file = NULL;
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_pixmap_icon_from_user_icon(WIcon *icon)
664 /* Set the icon->file_image */
665 get_rimage_icon_from_user_icon(icon);
667 /* Update icon->pixmap */
668 icon_update_pixmap(icon, icon->file_image);
671 static void get_rimage_icon_from_default_icon(WIcon *icon)
673 WScreen *scr = icon->core->screen_ptr;
675 /* If the icon don't have image, we should use the default image. */
676 if (!scr->def_icon_rimage)
677 scr->def_icon_rimage = get_default_image(scr);
679 /* Remove the icon image */
680 unset_icon_image(icon);
682 /* Set the new icon image */
683 icon->file = NULL;
684 icon->file_image = RRetainImage(scr->def_icon_rimage);
687 static void get_pixmap_icon_from_default_icon(WIcon *icon)
689 /* Update icon->file image */
690 get_rimage_icon_from_default_icon(icon);
692 /* Now, create the pixmap using the default (saved) image */
693 icon_update_pixmap(icon, icon->file_image);
696 /* This function creates the RImage using the default icon */
697 static RImage *get_default_image(WScreen *scr)
699 RImage *image = NULL;
700 char *path, *file;
702 /* Get the default icon */
703 file = wDefaultGetIconFile(NULL, NULL, True);
704 if (file) {
705 path = FindImage(wPreferences.icon_path, file);
706 image = get_rimage_from_file(scr, path, wPreferences.icon_size);
708 if (!image)
709 wwarning(_("could not find default icon \"%s\""), file);
711 wfree(file);
714 return image;
717 /* Get the Pixmap from the WIcon of the WWindow */
718 static void get_pixmap_icon_from_icon_win(WIcon *icon)
720 XWindowAttributes attr;
721 RImage *image;
722 WScreen *scr = icon->core->screen_ptr;
723 int title_height = WMFontHeight(scr->icon_title_font);
724 unsigned int w, h, d;
725 int theight = 0;
727 /* Create the new RImage */
728 image = get_window_image_from_x11(icon->icon_win);
730 /* Free the icon info */
731 unset_icon_image(icon);
733 /* Set the new info */
734 icon->file = NULL;
735 icon->file_image = image;
737 /* Paint the image at the icon */
738 icon_update_pixmap(icon, image);
740 /* Reparent the dock application to the icon */
742 /* We need the application size to center it
743 * and show in the correct position */
744 getSize(icon->icon_win, &w, &h, &d);
746 /* Set extra space for title */
747 if (icon->show_title && (h + title_height < wPreferences.icon_size)) {
748 theight = title_height;
749 drawIconTitle(scr, icon->pixmap, theight);
750 } else {
751 XSetWindowBackgroundPixmap(dpy, icon->core->window, scr->icon_tile_pixmap);
754 /* Set the icon border */
755 XSetWindowBorderWidth(dpy, icon->icon_win, 0);
757 /* Put the dock application in the icon */
758 XReparentWindow(dpy, icon->icon_win, icon->core->window,
759 (wPreferences.icon_size - w) / 2,
760 theight + (wPreferences.icon_size - h - theight) / 2);
762 /* Show it and save */
763 XMapWindow(dpy, icon->icon_win);
764 XAddToSaveSet(dpy, icon->icon_win);
766 /* Needed to move the icon clicking on the application part */
767 if ((XGetWindowAttributes(dpy, icon->icon_win, &attr)) &&
768 (attr.all_event_masks & ButtonPressMask))
769 wHackedGrabButton(Button1, MOD_MASK, icon->core->window, True,
770 ButtonPressMask, GrabModeSync, GrabModeAsync,
771 None, wCursor[WCUR_ARROW]);
774 /* Get the RImage from the XWindow wm_hints */
775 static int get_rimage_icon_from_wm_hints(WIcon *icon)
777 RImage *image = NULL;
778 unsigned int w, h, d;
779 WWindow *wwin = icon->owner;
781 if (!getSize(wwin->wm_hints->icon_pixmap, &w, &h, &d)) {
782 icon->owner->wm_hints->flags &= ~IconPixmapHint;
783 return 1;
786 image = get_wwindow_image_from_wmhints(wwin, icon);
787 if (!image)
788 return 1;
790 /* FIXME: If unset_icon_image, pointer double free then crash
791 unset_icon_image(icon); */
792 icon->file_image = image;
794 return 0;
797 /* Get the Pixmap from the XWindow wm_hints */
798 static int get_pixmap_icon_from_wm_hints(WIcon *icon)
800 int ret;
802 ret = get_rimage_icon_from_wm_hints(icon);
803 if (ret == 0)
804 icon_update_pixmap(icon, icon->file_image);
806 return ret;
809 void wIconPaint(WIcon * icon)
811 WScreen *scr = icon->core->screen_ptr;
812 int x;
813 char *tmp;
815 if (icon->force_paint) {
816 icon->force_paint = 0;
817 wIconUpdate(icon);
818 return;
821 XClearWindow(dpy, icon->core->window);
823 /* draw the icon title */
824 if (icon->show_title && icon->icon_name != NULL) {
825 int l;
826 int w;
828 tmp = ShrinkString(scr->icon_title_font, icon->icon_name, wPreferences.icon_size - 4);
829 w = WMWidthOfString(scr->icon_title_font, tmp, l = strlen(tmp));
831 if (w > icon->core->width - 4)
832 x = (icon->core->width - 4) - w;
833 else
834 x = (icon->core->width - w) / 2;
836 WMDrawString(scr->wmscreen, icon->core->window, scr->icon_title_color,
837 scr->icon_title_font, x, 1, tmp, l);
838 wfree(tmp);
841 if (icon->selected)
842 XDrawRectangle(dpy, icon->core->window, scr->icon_select_gc, 0, 0,
843 icon->core->width - 1, icon->core->height - 1);
846 /******************************************************************/
848 static void miniwindowExpose(WObjDescriptor * desc, XEvent * event)
850 wIconPaint(desc->parent);
853 static void miniwindowDblClick(WObjDescriptor * desc, XEvent * event)
855 WIcon *icon = desc->parent;
857 assert(icon->owner != NULL);
859 wDeiconifyWindow(icon->owner);
862 static void miniwindowMouseDown(WObjDescriptor * desc, XEvent * event)
864 WIcon *icon = desc->parent;
865 WWindow *wwin = icon->owner;
866 XEvent ev;
867 int x = wwin->icon_x, y = wwin->icon_y;
868 int dx = event->xbutton.x, dy = event->xbutton.y;
869 int grabbed = 0;
870 int clickButton = event->xbutton.button;
871 Bool hasMoved = False;
873 if (WCHECK_STATE(WSTATE_MODAL))
874 return;
876 if (IsDoubleClick(icon->core->screen_ptr, event)) {
877 miniwindowDblClick(desc, event);
878 return;
881 if (event->xbutton.button == Button1) {
882 if (event->xbutton.state & MOD_MASK)
883 wLowerFrame(icon->core);
884 else
885 wRaiseFrame(icon->core);
886 if (event->xbutton.state & ShiftMask) {
887 wIconSelect(icon);
888 wSelectWindow(icon->owner, !wwin->flags.selected);
890 } else if (event->xbutton.button == Button3) {
891 WObjDescriptor *desc;
893 OpenMiniwindowMenu(wwin, event->xbutton.x_root, event->xbutton.y_root);
895 /* allow drag select of menu */
896 desc = &wwin->screen_ptr->window_menu->menu->descriptor;
897 event->xbutton.send_event = True;
898 (*desc->handle_mousedown) (desc, event);
900 return;
903 if (XGrabPointer(dpy, icon->core->window, False, ButtonMotionMask
904 | ButtonReleaseMask | ButtonPressMask, GrabModeAsync,
905 GrabModeAsync, None, None, CurrentTime) != GrabSuccess) {
907 while (1) {
908 WMMaskEvent(dpy, PointerMotionMask | ButtonReleaseMask | ButtonPressMask
909 | ButtonMotionMask | ExposureMask, &ev);
910 switch (ev.type) {
911 case Expose:
912 WMHandleEvent(&ev);
913 break;
915 case MotionNotify:
916 hasMoved = True;
917 if (!grabbed) {
918 if (abs(dx - ev.xmotion.x) >= MOVE_THRESHOLD
919 || abs(dy - ev.xmotion.y) >= MOVE_THRESHOLD) {
920 XChangeActivePointerGrab(dpy, ButtonMotionMask
921 | ButtonReleaseMask | ButtonPressMask,
922 wCursor[WCUR_MOVE], CurrentTime);
923 grabbed = 1;
924 } else {
925 break;
928 x = ev.xmotion.x_root - dx;
929 y = ev.xmotion.y_root - dy;
930 XMoveWindow(dpy, icon->core->window, x, y);
931 break;
933 case ButtonPress:
934 break;
936 case ButtonRelease:
937 if (ev.xbutton.button != clickButton)
938 break;
940 if (wwin->icon_x != x || wwin->icon_y != y)
941 wwin->flags.icon_moved = 1;
943 XMoveWindow(dpy, icon->core->window, x, y);
945 wwin->icon_x = x;
946 wwin->icon_y = y;
947 XUngrabPointer(dpy, CurrentTime);
949 if (wPreferences.auto_arrange_icons)
950 wArrangeIcons(wwin->screen_ptr, True);
951 if (wPreferences.single_click && !hasMoved)
952 miniwindowDblClick(desc, event);
953 return;