get_rimage_icon_from_wm_hints returns image
[wmaker-crm.git] / src / icon.c
blob13a0c94849ef65f8c60ecb891509a9c1eb55e083
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 wIconUpdate(icon, NULL);
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;
116 char *file;
118 icon = icon_create_core(scr, wwin->icon_x, wwin->icon_y);
120 icon->owner = wwin;
121 if (wwin->wm_hints && (wwin->wm_hints->flags & IconWindowHint)) {
122 if (wwin->client_win == wwin->main_window) {
123 WApplication *wapp;
124 /* do not let miniwindow steal app-icon's icon window */
125 wapp = wApplicationOf(wwin->client_win);
126 if (!wapp || wapp->app_icon == NULL)
127 icon->icon_win = wwin->wm_hints->icon_window;
128 } else {
129 icon->icon_win = wwin->wm_hints->icon_window;
132 #ifdef NO_MINIWINDOW_TITLES
133 icon->show_title = 0;
134 #else
135 icon->show_title = 1;
136 #endif
138 icon->icon_name = wNETWMGetIconName(wwin->client_win);
139 if (icon->icon_name)
140 wwin->flags.net_has_icon_title = 1;
141 else
142 wGetIconName(dpy, wwin->client_win, &icon->icon_name);
144 /* Get the application icon, default included */
145 file = get_icon_filename(scr, wwin->wm_instance, wwin->wm_class, NULL, True);
146 if (file) {
147 icon->file = wstrdup(file);
148 icon->file_image = get_rimage_from_file(scr, icon->file, wPreferences.icon_size);
149 wfree(file);
152 icon->tile_type = TILE_NORMAL;
154 wIconUpdate(icon, NULL);
156 WMAddNotificationObserver(appearanceObserver, icon, WNIconAppearanceSettingsChanged, icon);
157 WMAddNotificationObserver(tileObserver, icon, WNIconTileSettingsChanged, icon);
159 return icon;
162 WIcon *icon_create_for_dock(WScreen *scr, char *command, char *wm_instance, char *wm_class, int tile)
164 WIcon *icon;
165 char *file = NULL;
167 icon = icon_create_core(scr, 0, 0);
169 /* Search the icon using instance and class, without default icon */
170 file = get_icon_filename(scr, wm_instance, wm_class, command, False);
171 if (file) {
172 icon->file = wstrdup(file);
173 icon->file_image = get_rimage_from_file(scr, icon->file, wPreferences.icon_size);
174 wfree(file);
177 icon->tile_type = tile;
179 wIconUpdate(icon, NULL);
181 WMAddNotificationObserver(appearanceObserver, icon, WNIconAppearanceSettingsChanged, icon);
182 WMAddNotificationObserver(tileObserver, icon, WNIconTileSettingsChanged, icon);
184 return icon;
187 static WIcon *icon_create_core(WScreen *scr, int coord_x, int coord_y)
189 WIcon *icon;
190 unsigned long vmask = 0;
191 XSetWindowAttributes attribs;
193 icon = wmalloc(sizeof(WIcon));
194 icon->core = wCoreCreateTopLevel(scr,
195 coord_x,
196 coord_y,
197 wPreferences.icon_size,
198 wPreferences.icon_size,
199 0, scr->w_depth, scr->w_visual, scr->w_colormap);
201 if (wPreferences.use_saveunders) {
202 vmask = CWSaveUnder;
203 attribs.save_under = True;
206 /* a white border for selecting it */
207 vmask |= CWBorderPixel;
208 attribs.border_pixel = scr->white_pixel;
210 XChangeWindowAttributes(dpy, icon->core->window, vmask, &attribs);
212 /* will be overriden if this is a application icon */
213 icon->core->descriptor.handle_mousedown = miniwindowMouseDown;
214 icon->core->descriptor.handle_expose = miniwindowExpose;
215 icon->core->descriptor.parent_type = WCLASS_MINIWINDOW;
216 icon->core->descriptor.parent = icon;
218 icon->core->stacking = wmalloc(sizeof(WStacking));
219 icon->core->stacking->above = NULL;
220 icon->core->stacking->under = NULL;
221 icon->core->stacking->window_level = NORMAL_ICON_LEVEL;
222 icon->core->stacking->child_of = NULL;
224 /* Icon image */
225 icon->file = NULL;
226 icon->file_image = NULL;
228 return icon;
231 void wIconDestroy(WIcon * icon)
233 WCoreWindow *core = icon->core;
234 WScreen *scr = core->screen_ptr;
236 WMRemoveNotificationObserver(icon);
238 if (icon->handlerID)
239 WMDeleteTimerHandler(icon->handlerID);
241 if (icon->icon_win) {
242 int x = 0, y = 0;
244 if (icon->owner) {
245 x = icon->owner->icon_x;
246 y = icon->owner->icon_y;
248 XUnmapWindow(dpy, icon->icon_win);
249 XReparentWindow(dpy, icon->icon_win, scr->root_win, x, y);
251 if (icon->icon_name)
252 XFree(icon->icon_name);
254 if (icon->pixmap)
255 XFreePixmap(dpy, icon->pixmap);
257 unset_icon_image(icon);
259 wCoreDestroy(icon->core);
260 wfree(icon);
263 static void drawIconTitle(WScreen * scr, Pixmap pixmap, int height)
265 XFillRectangle(dpy, pixmap, scr->icon_title_texture->normal_gc, 0, 0, wPreferences.icon_size, height + 1);
266 XDrawLine(dpy, pixmap, scr->icon_title_texture->light_gc, 0, 0, wPreferences.icon_size, 0);
267 XDrawLine(dpy, pixmap, scr->icon_title_texture->light_gc, 0, 0, 0, height + 1);
268 XDrawLine(dpy, pixmap, scr->icon_title_texture->dim_gc,
269 wPreferences.icon_size - 1, 0, wPreferences.icon_size - 1, height + 1);
272 static void icon_update_pixmap(WIcon *icon, RImage *image)
274 RImage *tile;
275 Pixmap pixmap;
276 int x, y, sx, sy;
277 unsigned w, h;
278 int theight = 0;
279 WScreen *scr = icon->core->screen_ptr;
280 int titled = icon->show_title;
282 if (icon->tile_type == TILE_NORMAL) {
283 tile = RCloneImage(scr->icon_tile);
284 } else {
285 assert(scr->clip_tile);
286 tile = RCloneImage(scr->clip_tile);
289 if (image) {
290 w = (image->width > wPreferences.icon_size)
291 ? wPreferences.icon_size : image->width;
292 x = (wPreferences.icon_size - w) / 2;
293 sx = (image->width - w) / 2;
295 if (titled)
296 theight = WMFontHeight(scr->icon_title_font);
298 h = (image->height + theight > wPreferences.icon_size
299 ? wPreferences.icon_size - theight : image->height);
300 y = theight + (wPreferences.icon_size - theight - h) / 2;
301 sy = (image->height - h) / 2;
303 RCombineArea(tile, image, sx, sy, w, h, x, y);
306 if (icon->shadowed) {
307 RColor color;
309 color.red = scr->icon_back_texture->light.red >> 8;
310 color.green = scr->icon_back_texture->light.green >> 8;
311 color.blue = scr->icon_back_texture->light.blue >> 8;
312 color.alpha = 150; /* about 60% */
313 RClearImage(tile, &color);
316 if (icon->highlighted) {
317 RColor color;
319 color.red = color.green = color.blue = 0;
320 color.alpha = 160;
321 RLightImage(tile, &color);
324 if (!RConvertImage(scr->rcontext, tile, &pixmap))
325 wwarning(_("error rendering image:%s"), RMessageForError(RErrorCode));
327 RReleaseImage(tile);
329 if (titled)
330 drawIconTitle(scr, pixmap, theight);
332 icon->pixmap = pixmap;
335 void wIconChangeTitle(WIcon *icon, char *new_title)
337 int changed;
339 changed = (new_title == NULL && icon->icon_name != NULL) ||
340 (new_title != NULL && icon->icon_name == NULL);
342 if (icon->icon_name != NULL)
343 XFree(icon->icon_name);
345 icon->icon_name = new_title;
347 if (changed)
348 wIconUpdate(icon, NULL);
349 else
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 (((max_size + ICON_BORDER) < icon->width) ||
362 ((max_size + ICON_BORDER) < icon->height)) {
363 if (icon->width > icon->height)
364 nimage = RScaleImage(icon, max_size - ICON_BORDER,
365 (icon->height * (max_size - ICON_BORDER) / icon->width));
366 else
367 nimage = RScaleImage(icon, (icon->width * (max_size - ICON_BORDER) / icon->height),
368 max_size - ICON_BORDER);
369 RReleaseImage(icon);
370 icon = nimage;
373 return icon;
376 Bool wIconChangeImageFile(WIcon *icon, char *file)
378 WScreen *scr = icon->core->screen_ptr;
379 char *path;
380 RImage *image = NULL;
381 int error = 0;
383 /* If no new image, don't do nothing */
384 if (!file)
385 return True;
387 /* Find the new image */
388 path = FindImage(wPreferences.icon_path, file);
389 if (path)
390 image = get_rimage_from_file(scr, path, wPreferences.icon_size);
391 else
392 error = 1;
394 /* New image! */
395 if (!error && image) {
396 /* Remove the old one */
397 unset_icon_image(icon);
399 /* Set the new image */
400 icon->file = wstrdup(path);
401 wIconUpdate(icon, image);
402 } else {
403 error = 1;
406 if (path)
407 wfree(path);
409 return !error;
412 static char *get_name_for_wwin(WWindow *wwin)
414 return get_name_for_instance_class(wwin->wm_instance, wwin->wm_class);
417 char *get_name_for_instance_class(char *wm_instance, char *wm_class)
419 char *suffix;
420 int len;
422 if (wm_class && wm_instance) {
423 len = strlen(wm_class) + strlen(wm_instance) + 2;
424 suffix = wmalloc(len);
425 snprintf(suffix, len, "%s.%s", wm_instance, wm_class);
426 } else if (wm_class) {
427 len = strlen(wm_class) + 1;
428 suffix = wmalloc(len);
429 snprintf(suffix, len, "%s", wm_class);
430 } else if (wm_instance) {
431 len = strlen(wm_instance) + 1;
432 suffix = wmalloc(len);
433 snprintf(suffix, len, "%s", wm_instance);
434 } else {
435 return NULL;
438 return suffix;
441 static char *get_icon_cache_path(void)
443 char *prefix, *path;
444 int len, ret;
446 prefix = wusergnusteppath();
447 len = strlen(prefix) + strlen(CACHE_ICON_PATH) + 2;
448 path = wmalloc(len);
449 snprintf(path, len, "%s%s/", prefix, CACHE_ICON_PATH);
451 /* If the folder exists, exit */
452 if (access(path, F_OK) == 0)
453 return path;
455 /* Create the folder */
456 ret = wmkdirhier((const char *) path);
458 /* Exit 1 on success, 0 on failure */
459 if (ret == 1)
460 return path;
462 /* Fail */
463 wfree(path);
464 return NULL;
467 static RImage *get_wwindow_image_from_wmhints(WWindow *wwin, WIcon *icon)
469 RImage *image = NULL;
470 XWMHints *hints = wwin->wm_hints;
472 if (hints && (hints->flags & IconPixmapHint) && hints->icon_pixmap != None)
473 image = RCreateImageFromDrawable(icon->core->screen_ptr->rcontext,
474 hints->icon_pixmap,
475 (hints->flags & IconMaskHint)
476 ? hints->icon_mask : None);
478 return image;
482 * wIconStore--
483 * Stores the client supplied icon at CACHE_ICON_PATH
484 * and returns the path for that icon. Returns NULL if there is no
485 * client supplied icon or on failure.
487 * Side effects:
488 * New directories might be created.
490 char *wIconStore(WIcon * icon)
492 char *path, *dir_path, *file;
493 int len = 0;
494 RImage *image = NULL;
495 WWindow *wwin = icon->owner;
497 if (!wwin)
498 return NULL;
500 dir_path = get_icon_cache_path();
501 if (!dir_path)
502 return NULL;
504 file = get_name_for_wwin(wwin);
505 if (!file) {
506 wfree(dir_path);
507 return NULL;
510 len = strlen(dir_path) + strlen(file) + 5;
511 path = wmalloc(len);
512 snprintf(path, len, "%s%s.xpm", dir_path, file);
513 wfree(dir_path);
514 wfree(file);
516 /* If icon exists, exit */
517 if (access(path, F_OK) == 0)
518 return path;
520 if (wwin->net_icon_image)
521 image = RRetainImage(wwin->net_icon_image);
522 else
523 image = get_wwindow_image_from_wmhints(wwin, icon);
525 if (!image) {
526 wfree(path);
527 return NULL;
530 if (!RSaveImage(image, path, "XPM")) {
531 wfree(path);
532 path = NULL;
535 RReleaseImage(image);
537 return path;
540 static void cycleColor(void *data)
542 WIcon *icon = (WIcon *) data;
543 WScreen *scr = icon->core->screen_ptr;
544 XGCValues gcv;
546 icon->step--;
547 gcv.dash_offset = icon->step;
548 XChangeGC(dpy, scr->icon_select_gc, GCDashOffset, &gcv);
550 XDrawRectangle(dpy, icon->core->window, scr->icon_select_gc, 0, 0,
551 icon->core->width - 1, icon->core->height - 1);
552 icon->handlerID = WMAddTimerHandler(COLOR_CYCLE_DELAY, cycleColor, icon);
555 void wIconSetHighlited(WIcon *icon, Bool flag)
557 if (icon->highlighted == flag)
558 return;
560 icon->highlighted = flag;
561 update_icon_pixmap(icon);
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);
589 icon->file = NULL;
592 if (icon->file_image) {
593 RReleaseImage(icon->file_image);
594 icon->file_image = NULL;
598 void wIconUpdate(WIcon *icon, RImage *image)
600 WWindow *wwin = NULL;
602 if (image) {
603 icon->file_image = image;
604 } else {
605 if (icon && icon->owner)
606 wwin = icon->owner;
608 if (wwin && WFLAGP(wwin, always_user_icon)) {
609 /* Forced use user_icon */
610 get_rimage_icon_from_user_icon(icon);
611 } else if (icon->icon_win != None) {
612 /* Get the Pixmap from the WIcon */
613 get_rimage_icon_from_icon_win(icon);
614 } else if (wwin && wwin->net_icon_image) {
615 /* Use _NET_WM_ICON icon */
616 get_rimage_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 unset_icon_image(icon);
620 icon->file_image = get_rimage_icon_from_wm_hints(icon);
621 if (!icon->file_image)
622 get_rimage_icon_from_user_icon(icon);
623 } else {
624 /* Get the Pixmap from the user */
625 get_rimage_icon_from_user_icon(icon);
629 update_icon_pixmap(icon);
632 void update_icon_pixmap(WIcon *icon)
634 if (icon->pixmap != None)
635 XFreePixmap(dpy, icon->pixmap);
637 icon->pixmap = None;
639 /* Create the pixmap */
640 if (icon->file_image)
641 icon_update_pixmap(icon, icon->file_image);
643 /* If dockapp, put inside the icon */
644 if (icon->icon_win != None) {
645 /* file_image is NULL, because is docked app */
646 icon_update_pixmap(icon, NULL);
647 set_dockapp_in_icon(icon);
650 /* No pixmap, set default background */
651 if (icon->pixmap != None)
652 XSetWindowBackgroundPixmap(dpy, icon->core->window, icon->pixmap);
654 /* Paint it */
655 XClearWindow(dpy, icon->core->window);
656 wIconPaint(icon);
659 static void get_rimage_icon_from_x11(WIcon *icon)
661 /* Remove the icon image */
662 unset_icon_image(icon);
664 /* Set the new icon image */
665 icon->file_image = RRetainImage(icon->owner->net_icon_image);
668 static void get_rimage_icon_from_user_icon(WIcon *icon)
670 if (icon->file_image)
671 return;
673 get_rimage_icon_from_default_icon(icon);
676 static void get_rimage_icon_from_default_icon(WIcon *icon)
678 WScreen *scr = icon->core->screen_ptr;
680 /* If the icon don't have image, we should use the default image. */
681 if (!scr->def_icon_rimage)
682 scr->def_icon_rimage = get_default_image(scr);
684 /* Remove the icon image */
685 unset_icon_image(icon);
687 /* Set the new icon image */
688 icon->file_image = RRetainImage(scr->def_icon_rimage);
691 /* Get the RImage from the WIcon of the WWindow */
692 static void get_rimage_icon_from_icon_win(WIcon *icon)
694 RImage *image;
696 /* Create the new RImage */
697 image = get_window_image_from_x11(icon->icon_win);
699 /* Free the icon info */
700 unset_icon_image(icon);
702 /* Set the new info */
703 icon->file_image = image;
706 /* Set the dockapp in the WIcon */
707 static void set_dockapp_in_icon(WIcon *icon)
709 XWindowAttributes attr;
710 WScreen *scr = icon->core->screen_ptr;
711 int title_height = WMFontHeight(scr->icon_title_font);
712 unsigned int w, h, d;
713 int theight = 0;
715 /* Reparent the dock application to the icon */
717 /* We need the application size to center it
718 * and show in the correct position */
719 getSize(icon->icon_win, &w, &h, &d);
721 /* Set extra space for title */
722 if (icon->show_title && (h + title_height < wPreferences.icon_size)) {
723 theight = title_height;
724 drawIconTitle(scr, icon->pixmap, theight);
725 } else {
726 XSetWindowBackgroundPixmap(dpy, icon->core->window, scr->icon_tile_pixmap);
729 /* Set the icon border */
730 XSetWindowBorderWidth(dpy, icon->icon_win, 0);
732 /* Put the dock application in the icon */
733 XReparentWindow(dpy, icon->icon_win, icon->core->window,
734 (wPreferences.icon_size - w) / 2,
735 theight + (wPreferences.icon_size - h - theight) / 2);
737 /* Show it and save */
738 XMapWindow(dpy, icon->icon_win);
739 XAddToSaveSet(dpy, icon->icon_win);
741 /* Needed to move the icon clicking on the application part */
742 if ((XGetWindowAttributes(dpy, icon->icon_win, &attr)) &&
743 (attr.all_event_masks & ButtonPressMask))
744 wHackedGrabButton(Button1, MOD_MASK, icon->core->window, True,
745 ButtonPressMask, GrabModeSync, GrabModeAsync,
746 None, wCursor[WCUR_ARROW]);
749 /* Get the RImage from the XWindow wm_hints */
750 RImage *get_rimage_icon_from_wm_hints(WIcon *icon)
752 RImage *image = NULL;
753 unsigned int w, h, d;
754 WWindow *wwin = icon->owner;
756 if (!getSize(wwin->wm_hints->icon_pixmap, &w, &h, &d)) {
757 icon->owner->wm_hints->flags &= ~IconPixmapHint;
758 return NULL;
761 image = get_wwindow_image_from_wmhints(wwin, icon);
762 if (!image)
763 return NULL;
765 /* Resize the icon to the wPreferences.icon_size size */
766 image = wIconValidateIconSize(image, wPreferences.icon_size);
768 return image;
771 void wIconPaint(WIcon *icon)
773 WScreen *scr = icon->core->screen_ptr;
774 int x, l, w;
775 char *tmp;
777 XClearWindow(dpy, icon->core->window);
779 /* draw the icon title */
780 if (icon->show_title && icon->icon_name != NULL) {
781 tmp = ShrinkString(scr->icon_title_font, icon->icon_name, wPreferences.icon_size - 4);
782 w = WMWidthOfString(scr->icon_title_font, tmp, l = strlen(tmp));
784 if (w > icon->core->width - 4)
785 x = (icon->core->width - 4) - w;
786 else
787 x = (icon->core->width - w) / 2;
789 WMDrawString(scr->wmscreen, icon->core->window, scr->icon_title_color,
790 scr->icon_title_font, x, 1, tmp, l);
791 wfree(tmp);
794 if (icon->selected)
795 XDrawRectangle(dpy, icon->core->window, scr->icon_select_gc, 0, 0,
796 icon->core->width - 1, icon->core->height - 1);
799 /******************************************************************/
801 static void miniwindowExpose(WObjDescriptor * desc, XEvent * event)
803 wIconPaint(desc->parent);
806 static void miniwindowDblClick(WObjDescriptor * desc, XEvent * event)
808 WIcon *icon = desc->parent;
810 assert(icon->owner != NULL);
812 wDeiconifyWindow(icon->owner);
815 static void miniwindowMouseDown(WObjDescriptor * desc, XEvent * event)
817 WIcon *icon = desc->parent;
818 WWindow *wwin = icon->owner;
819 XEvent ev;
820 int x = wwin->icon_x, y = wwin->icon_y;
821 int dx = event->xbutton.x, dy = event->xbutton.y;
822 int grabbed = 0;
823 int clickButton = event->xbutton.button;
824 Bool hasMoved = False;
826 if (WCHECK_STATE(WSTATE_MODAL))
827 return;
829 if (IsDoubleClick(icon->core->screen_ptr, event)) {
830 miniwindowDblClick(desc, event);
831 return;
834 if (event->xbutton.button == Button1) {
835 if (event->xbutton.state & MOD_MASK)
836 wLowerFrame(icon->core);
837 else
838 wRaiseFrame(icon->core);
839 if (event->xbutton.state & ShiftMask) {
840 wIconSelect(icon);
841 wSelectWindow(icon->owner, !wwin->flags.selected);
843 } else if (event->xbutton.button == Button3) {
844 WObjDescriptor *desc;
846 OpenMiniwindowMenu(wwin, event->xbutton.x_root, event->xbutton.y_root);
848 /* allow drag select of menu */
849 desc = &wwin->screen_ptr->window_menu->menu->descriptor;
850 event->xbutton.send_event = True;
851 (*desc->handle_mousedown) (desc, event);
853 return;
856 if (XGrabPointer(dpy, icon->core->window, False, ButtonMotionMask
857 | ButtonReleaseMask | ButtonPressMask, GrabModeAsync,
858 GrabModeAsync, None, None, CurrentTime) != GrabSuccess) {
860 while (1) {
861 WMMaskEvent(dpy, PointerMotionMask | ButtonReleaseMask | ButtonPressMask
862 | ButtonMotionMask | ExposureMask, &ev);
863 switch (ev.type) {
864 case Expose:
865 WMHandleEvent(&ev);
866 break;
868 case MotionNotify:
869 hasMoved = True;
870 if (!grabbed) {
871 if (abs(dx - ev.xmotion.x) >= MOVE_THRESHOLD
872 || abs(dy - ev.xmotion.y) >= MOVE_THRESHOLD) {
873 XChangeActivePointerGrab(dpy, ButtonMotionMask
874 | ButtonReleaseMask | ButtonPressMask,
875 wCursor[WCUR_MOVE], CurrentTime);
876 grabbed = 1;
877 } else {
878 break;
881 x = ev.xmotion.x_root - dx;
882 y = ev.xmotion.y_root - dy;
883 XMoveWindow(dpy, icon->core->window, x, y);
884 break;
886 case ButtonPress:
887 break;
889 case ButtonRelease:
890 if (ev.xbutton.button != clickButton)
891 break;
893 if (wwin->icon_x != x || wwin->icon_y != y)
894 wwin->flags.icon_moved = 1;
896 XMoveWindow(dpy, icon->core->window, x, y);
898 wwin->icon_x = x;
899 wwin->icon_y = y;
900 XUngrabPointer(dpy, CurrentTime);
902 if (wPreferences.auto_arrange_icons)
903 wArrangeIcons(wwin->screen_ptr, True);
904 if (wPreferences.single_click && !hasMoved)
905 miniwindowDblClick(desc, event);
906 return;