Split get_pixmap_icon_from_default_icon() into two
[wmaker-crm.git] / src / icon.c
blob2e0f9b0a795d183bc0ef73968bf64eabdcf1bcad
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 void get_pixmap_icon_from_user_icon(WIcon *icon);
66 static void get_pixmap_icon_from_default_icon(WIcon *icon);
67 static void get_rimage_icon_from_default_icon(WIcon *icon);
69 static void icon_update_pixmap(WIcon *icon, RImage *image);
70 static void unset_icon_image(WIcon *icon);
72 static RImage *get_default_image(WScreen *scr);
73 /****** Notification Observers ******/
75 static void appearanceObserver(void *self, WMNotification * notif)
77 WIcon *icon = (WIcon *) self;
78 uintptr_t flags = (uintptr_t)WMGetNotificationClientData(notif);
80 if ((flags & WTextureSettings) || (flags & WFontSettings))
81 icon->force_paint = 1;
83 wIconPaint(icon);
85 /* so that the appicon expose handlers will paint the appicon specific
86 * stuff */
87 XClearArea(dpy, icon->core->window, 0, 0, icon->core->width, icon->core->height, True);
90 static void tileObserver(void *self, WMNotification * notif)
92 WIcon *icon = (WIcon *) self;
94 icon->force_paint = 1;
95 wIconPaint(icon);
97 XClearArea(dpy, icon->core->window, 0, 0, 1, 1, True);
100 /************************************/
102 static int getSize(Drawable d, unsigned int *w, unsigned int *h, unsigned int *dep)
104 Window rjunk;
105 int xjunk, yjunk;
106 unsigned int bjunk;
108 return XGetGeometry(dpy, d, &rjunk, &xjunk, &yjunk, w, h, &bjunk, dep);
111 WIcon *icon_create_for_wwindow(WWindow *wwin)
113 WScreen *scr = wwin->screen_ptr;
114 WIcon *icon;
115 char *file;
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 icon->icon_name = wNETWMGetIconName(wwin->client_win);
138 if (icon->icon_name)
139 wwin->flags.net_has_icon_title = 1;
140 else
141 wGetIconName(dpy, wwin->client_win, &icon->icon_name);
143 /* Get the application icon, default included */
144 file = get_default_icon_filename(scr, wwin->wm_instance, wwin->wm_class, NULL, True);
145 if (file) {
146 icon->file = wstrdup(file);
147 icon->file_image = get_rimage_from_file(scr, icon->file, wPreferences.icon_size);
148 wfree(file);
151 icon->tile_type = TILE_NORMAL;
153 wIconUpdate(icon);
155 WMAddNotificationObserver(appearanceObserver, icon, WNIconAppearanceSettingsChanged, icon);
156 WMAddNotificationObserver(tileObserver, icon, WNIconTileSettingsChanged, icon);
158 return icon;
161 WIcon *icon_create_for_dock(WScreen *scr, char *command, char *wm_instance, char *wm_class, int tile)
163 WIcon *icon;
164 char *file = NULL;
166 icon = icon_create_core(scr, 0, 0);
168 /* Search the icon using instance and class, without default icon */
169 file = get_default_icon_filename(scr, wm_instance, wm_class, command, False);
170 if (file) {
171 icon->file = wstrdup(file);
172 icon->file_image = get_rimage_from_file(scr, icon->file, wPreferences.icon_size);
173 wfree(file);
176 icon->tile_type = tile;
178 wIconUpdate(icon);
180 WMAddNotificationObserver(appearanceObserver, icon, WNIconAppearanceSettingsChanged, icon);
181 WMAddNotificationObserver(tileObserver, icon, WNIconTileSettingsChanged, icon);
183 return icon;
186 static WIcon *icon_create_core(WScreen *scr, int coord_x, int coord_y)
188 WIcon *icon;
189 unsigned long vmask = 0;
190 XSetWindowAttributes attribs;
192 icon = wmalloc(sizeof(WIcon));
193 icon->core = wCoreCreateTopLevel(scr,
194 coord_x,
195 coord_y,
196 wPreferences.icon_size,
197 wPreferences.icon_size,
198 0, scr->w_depth, scr->w_visual, scr->w_colormap);
200 if (wPreferences.use_saveunders) {
201 vmask = CWSaveUnder;
202 attribs.save_under = True;
205 /* a white border for selecting it */
206 vmask |= CWBorderPixel;
207 attribs.border_pixel = scr->white_pixel;
209 XChangeWindowAttributes(dpy, icon->core->window, vmask, &attribs);
211 /* will be overriden if this is a application icon */
212 icon->core->descriptor.handle_mousedown = miniwindowMouseDown;
213 icon->core->descriptor.handle_expose = miniwindowExpose;
214 icon->core->descriptor.parent_type = WCLASS_MINIWINDOW;
215 icon->core->descriptor.parent = icon;
217 icon->core->stacking = wmalloc(sizeof(WStacking));
218 icon->core->stacking->above = NULL;
219 icon->core->stacking->under = NULL;
220 icon->core->stacking->window_level = NORMAL_ICON_LEVEL;
221 icon->core->stacking->child_of = NULL;
223 /* Icon image */
224 icon->file = NULL;
225 icon->file_image = NULL;
227 return icon;
230 void wIconDestroy(WIcon * icon)
232 WCoreWindow *core = icon->core;
233 WScreen *scr = core->screen_ptr;
235 WMRemoveNotificationObserver(icon);
237 if (icon->handlerID)
238 WMDeleteTimerHandler(icon->handlerID);
240 if (icon->icon_win) {
241 int x = 0, y = 0;
243 if (icon->owner) {
244 x = icon->owner->icon_x;
245 y = icon->owner->icon_y;
247 XUnmapWindow(dpy, icon->icon_win);
248 XReparentWindow(dpy, icon->icon_win, scr->root_win, x, y);
250 if (icon->icon_name)
251 XFree(icon->icon_name);
253 if (icon->pixmap)
254 XFreePixmap(dpy, icon->pixmap);
256 unset_icon_image(icon);
258 wCoreDestroy(icon->core);
259 wfree(icon);
262 static void drawIconTitle(WScreen * scr, Pixmap pixmap, int height)
264 XFillRectangle(dpy, pixmap, scr->icon_title_texture->normal_gc, 0, 0, wPreferences.icon_size, height + 1);
265 XDrawLine(dpy, pixmap, scr->icon_title_texture->light_gc, 0, 0, wPreferences.icon_size, 0);
266 XDrawLine(dpy, pixmap, scr->icon_title_texture->light_gc, 0, 0, 0, height + 1);
267 XDrawLine(dpy, pixmap, scr->icon_title_texture->dim_gc,
268 wPreferences.icon_size - 1, 0, wPreferences.icon_size - 1, height + 1);
271 static void icon_update_pixmap(WIcon *icon, RImage *image)
273 RImage *tile;
274 Pixmap pixmap;
275 int x, y, sx, sy;
276 unsigned w, h;
277 int theight = 0;
278 WScreen *scr = icon->core->screen_ptr;
279 int titled = icon->show_title;
281 if (icon->tile_type == TILE_NORMAL) {
282 tile = RCloneImage(scr->icon_tile);
283 } else {
284 assert(scr->clip_tile);
285 tile = RCloneImage(scr->clip_tile);
288 if (image) {
289 w = (image->width > wPreferences.icon_size)
290 ? wPreferences.icon_size : image->width;
291 x = (wPreferences.icon_size - w) / 2;
292 sx = (image->width - w) / 2;
294 if (titled)
295 theight = WMFontHeight(scr->icon_title_font);
297 h = (image->height + theight > wPreferences.icon_size
298 ? wPreferences.icon_size - theight : image->height);
299 y = theight + (wPreferences.icon_size - theight - h) / 2;
300 sy = (image->height - h) / 2;
302 RCombineArea(tile, image, sx, sy, w, h, x, y);
305 if (icon->shadowed) {
306 RColor color;
308 color.red = scr->icon_back_texture->light.red >> 8;
309 color.green = scr->icon_back_texture->light.green >> 8;
310 color.blue = scr->icon_back_texture->light.blue >> 8;
311 color.alpha = 150; /* about 60% */
312 RClearImage(tile, &color);
315 if (icon->highlighted) {
316 RColor color;
318 color.red = color.green = color.blue = 0;
319 color.alpha = 160;
320 RLightImage(tile, &color);
323 if (!RConvertImage(scr->rcontext, tile, &pixmap))
324 wwarning(_("error rendering image:%s"), RMessageForError(RErrorCode));
326 RReleaseImage(tile);
328 if (titled)
329 drawIconTitle(scr, pixmap, theight);
331 icon->pixmap = pixmap;
334 void wIconChangeTitle(WIcon * icon, char *new_title)
336 int changed;
338 changed = (new_title == NULL && icon->icon_name != NULL)
339 || (new_title != NULL && icon->icon_name == NULL);
341 if (icon->icon_name != NULL)
342 XFree(icon->icon_name);
344 icon->icon_name = new_title;
346 if (changed)
347 icon->force_paint = 1;
348 wIconPaint(icon);
351 RImage *wIconValidateIconSize(RImage *icon, int max_size)
353 RImage *nimage;
355 if (!icon)
356 return NULL;
358 /* We should hold "ICON_BORDER" (~2) pixels to include the icon border */
359 if ((icon->width - max_size) > -ICON_BORDER ||
360 (icon->height - max_size) > -ICON_BORDER) {
361 nimage = RScaleImage(icon, max_size - ICON_BORDER,
362 (icon->height * (max_size - ICON_BORDER) / icon->width));
363 RReleaseImage(icon);
364 icon = nimage;
367 return icon;
370 Bool wIconChangeImageFile(WIcon *icon, char *file)
372 WScreen *scr = icon->core->screen_ptr;
373 char *path;
374 RImage *image = NULL;
375 int error = 0;
377 /* If no new image, don't do nothing */
378 if (!file)
379 return True;
381 /* Find the new image */
382 path = FindImage(wPreferences.icon_path, file);
383 if (path)
384 image = get_rimage_from_file(scr, path, wPreferences.icon_size);
385 else
386 error = 1;
388 /* New image! */
389 if (!error && image) {
390 /* Remove the old one */
391 unset_icon_image(icon);
393 /* Set the new image */
394 icon->file_image = image;
395 icon->file = wstrdup(path);
396 wIconUpdate(icon);
397 } else {
398 error = 1;
401 if (path)
402 wfree(path);
404 return !error;
407 static char *get_name_for_wwin(WWindow *wwin)
409 return get_name_for_instance_class(wwin->wm_instance, wwin->wm_class);
412 char *get_name_for_instance_class(char *wm_instance, char *wm_class)
414 char *suffix;
415 int len;
417 if (wm_class && wm_instance) {
418 len = strlen(wm_class) + strlen(wm_instance) + 2;
419 suffix = wmalloc(len);
420 snprintf(suffix, len, "%s.%s", wm_instance, wm_class);
421 } else if (wm_class) {
422 len = strlen(wm_class) + 1;
423 suffix = wmalloc(len);
424 snprintf(suffix, len, "%s", wm_class);
425 } else if (wm_instance) {
426 len = strlen(wm_instance) + 1;
427 suffix = wmalloc(len);
428 snprintf(suffix, len, "%s", wm_instance);
429 } else {
430 return NULL;
433 return suffix;
436 static char *get_icon_cache_path(void)
438 char *prefix, *path;
439 int len, ret;
441 prefix = wusergnusteppath();
442 len = strlen(prefix) + strlen(CACHE_ICON_PATH) + 2;
443 path = wmalloc(len);
444 snprintf(path, len, "%s%s/", prefix, CACHE_ICON_PATH);
446 /* If the folder exists, exit */
447 if (access(path, F_OK) == 0)
448 return path;
450 /* Create the folder */
451 ret = wmkdirhier((const char *) path);
453 /* Exit 1 on success, 0 on failure */
454 if (ret == 1)
455 return path;
457 /* Fail */
458 wfree(path);
459 return NULL;
462 static RImage *get_wwindow_image_from_wmhints(WWindow *wwin, WIcon *icon)
464 RImage *image = NULL;
465 XWMHints *hints = wwin->wm_hints;
467 if (hints && (hints->flags & IconPixmapHint) && hints->icon_pixmap != None)
468 image = RCreateImageFromDrawable(icon->core->screen_ptr->rcontext,
469 hints->icon_pixmap,
470 (hints->flags & IconMaskHint)
471 ? hints->icon_mask : None);
473 return image;
477 * wIconStore--
478 * Stores the client supplied icon at CACHE_ICON_PATH
479 * and returns the path for that icon. Returns NULL if there is no
480 * client supplied icon or on failure.
482 * Side effects:
483 * New directories might be created.
485 char *wIconStore(WIcon * icon)
487 char *path, *dir_path, *file;
488 int len = 0;
489 RImage *image = NULL;
490 WWindow *wwin = icon->owner;
492 if (!wwin)
493 return NULL;
495 dir_path = get_icon_cache_path();
496 if (!dir_path)
497 return NULL;
499 file = get_name_for_wwin(wwin);
500 if (!file) {
501 wfree(dir_path);
502 return NULL;
505 len = strlen(dir_path) + strlen(file) + 5;
506 path = wmalloc(len);
507 snprintf(path, len, "%s%s.xpm", dir_path, file);
508 wfree(dir_path);
509 wfree(file);
511 /* If icon exists, exit */
512 if (access(path, F_OK) == 0)
513 return path;
515 if (wwin->net_icon_image)
516 image = RRetainImage(wwin->net_icon_image);
517 else
518 image = get_wwindow_image_from_wmhints(wwin, icon);
520 if (!image) {
521 wfree(path);
522 return NULL;
525 if (!RSaveImage(image, path, "XPM")) {
526 wfree(path);
527 path = NULL;
530 RReleaseImage(image);
532 return path;
535 static void cycleColor(void *data)
537 WIcon *icon = (WIcon *) data;
538 WScreen *scr = icon->core->screen_ptr;
539 XGCValues gcv;
541 icon->step--;
542 gcv.dash_offset = icon->step;
543 XChangeGC(dpy, scr->icon_select_gc, GCDashOffset, &gcv);
545 XDrawRectangle(dpy, icon->core->window, scr->icon_select_gc, 0, 0,
546 icon->core->width - 1, icon->core->height - 1);
547 icon->handlerID = WMAddTimerHandler(COLOR_CYCLE_DELAY, cycleColor, icon);
550 #ifdef NEWAPPICON
551 void wIconSetHighlited(WIcon *icon, Bool flag)
553 if (icon->highlighted == flag)
554 return;
556 icon->highlighted = flag;
557 icon->force_paint = True;
558 wIconPaint(icon);
560 #endif
562 void wIconSelect(WIcon * icon)
564 WScreen *scr = icon->core->screen_ptr;
565 icon->selected = !icon->selected;
567 if (icon->selected) {
568 icon->step = 0;
569 if (!wPreferences.dont_blink)
570 icon->handlerID = WMAddTimerHandler(10, cycleColor, icon);
571 else
572 XDrawRectangle(dpy, icon->core->window, scr->icon_select_gc, 0, 0,
573 icon->core->width - 1, icon->core->height - 1);
574 } else {
575 if (icon->handlerID) {
576 WMDeleteTimerHandler(icon->handlerID);
577 icon->handlerID = NULL;
579 XClearArea(dpy, icon->core->window, 0, 0, icon->core->width, icon->core->height, True);
583 static void unset_icon_image(WIcon *icon)
585 if (icon->file)
586 wfree(icon->file);
588 if (icon->file_image)
589 RReleaseImage(icon->file_image);
592 void wIconUpdate(WIcon *icon)
594 WScreen *scr = icon->core->screen_ptr;
595 WWindow *wwin = icon->owner;
597 assert(scr->icon_tile != NULL);
599 if (icon->pixmap != None)
600 XFreePixmap(dpy, icon->pixmap);
602 icon->pixmap = None;
604 if (wwin && WFLAGP(wwin, always_user_icon)) {
605 /* Forced use user_icon */
606 get_pixmap_icon_from_user_icon(icon);
607 } else if (icon->icon_win != None) {
608 /* Get the Pixmap from the WIcon */
609 get_pixmap_icon_from_icon_win(icon);
610 } else if (wwin && wwin->net_icon_image) {
611 /* Use _NET_WM_ICON icon */
612 icon_update_pixmap(icon, wwin->net_icon_image);
613 } else if (wwin && wwin->wm_hints && (wwin->wm_hints->flags & IconPixmapHint)) {
614 /* Get the Pixmap from the wm_hints, else, from the user */
615 if (get_pixmap_icon_from_wm_hints(icon))
616 get_pixmap_icon_from_user_icon(icon);
617 } else {
618 /* Get the Pixmap from the user */
619 get_pixmap_icon_from_user_icon(icon);
622 /* No pixmap, set default background */
623 if (icon->pixmap != None)
624 XSetWindowBackgroundPixmap(dpy, icon->core->window, icon->pixmap);
626 /* Paint it */
627 XClearWindow(dpy, icon->core->window);
628 wIconPaint(icon);
631 static void get_pixmap_icon_from_user_icon(WIcon *icon)
633 /* If the icon has image, update it and continue */
634 if (icon->file_image) {
635 icon_update_pixmap(icon, icon->file_image);
636 return;
639 get_pixmap_icon_from_default_icon(icon);
642 static void get_rimage_icon_from_default_icon(WIcon *icon)
644 WScreen *scr = icon->core->screen_ptr;
646 /* If the icon don't have image, we should use the default image. */
647 if (!scr->def_icon_rimage)
648 scr->def_icon_rimage = get_default_image(scr);
650 /* Remove the icon image */
651 unset_icon_image(icon);
653 /* Set the new icon image */
654 icon->file = NULL;
655 icon->file_image = RRetainImage(scr->def_icon_rimage);
658 static void get_pixmap_icon_from_default_icon(WIcon *icon)
660 /* Update icon->file image */
661 get_rimage_icon_from_default_icon(icon);
663 /* Now, create the pixmap using the default (saved) image */
664 icon_update_pixmap(icon, icon->file_image);
667 /* This function creates the RImage using the default icon */
668 static RImage *get_default_image(WScreen *scr)
670 RImage *image = NULL;
671 char *path, *file;
673 /* Get the default icon */
674 file = wDefaultGetIconFile(NULL, NULL, True);
675 if (file) {
676 path = FindImage(wPreferences.icon_path, file);
677 image = get_rimage_from_file(scr, path, wPreferences.icon_size);
679 if (!image)
680 wwarning(_("could not find default icon \"%s\""), file);
682 wfree(file);
685 return image;
688 /* Get the Pixmap from the WIcon of the WWindow */
689 static void get_pixmap_icon_from_icon_win(WIcon * icon)
691 XWindowAttributes attr;
692 WScreen *scr = icon->core->screen_ptr;
693 int title_height = WMFontHeight(scr->icon_title_font);
694 unsigned int width, height, depth;
695 int theight;
696 int resize = 0;
697 Pixmap pixmap;
699 getSize(icon->icon_win, &width, &height, &depth);
701 if (width > wPreferences.icon_size) {
702 resize = 1;
703 width = wPreferences.icon_size;
706 if (height > wPreferences.icon_size) {
707 resize = 1;
708 height = wPreferences.icon_size;
711 if (icon->show_title && (height + title_height < wPreferences.icon_size)) {
712 pixmap = XCreatePixmap(dpy, scr->w_win, wPreferences.icon_size,
713 wPreferences.icon_size, scr->w_depth);
714 XSetClipMask(dpy, scr->copy_gc, None);
715 XCopyArea(dpy, scr->icon_tile_pixmap, pixmap, scr->copy_gc, 0, 0,
716 wPreferences.icon_size, wPreferences.icon_size, 0, 0);
717 drawIconTitle(scr, pixmap, title_height);
718 theight = title_height;
719 } else {
720 pixmap = None;
721 theight = 0;
722 XSetWindowBackgroundPixmap(dpy, icon->core->window, scr->icon_tile_pixmap);
725 XSetWindowBorderWidth(dpy, icon->icon_win, 0);
726 XReparentWindow(dpy, icon->icon_win, icon->core->window,
727 (wPreferences.icon_size - width) / 2,
728 theight + (wPreferences.icon_size - height - theight) / 2);
729 if (resize)
730 XResizeWindow(dpy, icon->icon_win, width, height);
732 XMapWindow(dpy, icon->icon_win);
733 XAddToSaveSet(dpy, icon->icon_win);
735 /* Save it */
736 icon->pixmap = pixmap;
738 if ((XGetWindowAttributes(dpy, icon->icon_win, &attr)) &&
739 (attr.all_event_masks & ButtonPressMask))
740 wHackedGrabButton(Button1, MOD_MASK, icon->core->window, True,
741 ButtonPressMask, GrabModeSync, GrabModeAsync,
742 None, wCursor[WCUR_ARROW]);
745 /* Get the Pixmap from the XWindow wm_hints */
746 static int get_pixmap_icon_from_wm_hints(WIcon *icon)
748 RImage *image = NULL;
749 unsigned int w, h, d;
750 WWindow *wwin = icon->owner;
752 if (!getSize(wwin->wm_hints->icon_pixmap, &w, &h, &d)) {
753 icon->owner->wm_hints->flags &= ~IconPixmapHint;
754 return 1;
757 image = get_wwindow_image_from_wmhints(wwin, icon);
758 if (!image)
759 return 1;
761 icon_update_pixmap(icon, image);
763 return 0;
766 void wIconPaint(WIcon * icon)
768 WScreen *scr = icon->core->screen_ptr;
769 int x;
770 char *tmp;
772 if (icon->force_paint) {
773 icon->force_paint = 0;
774 wIconUpdate(icon);
775 return;
778 XClearWindow(dpy, icon->core->window);
780 /* draw the icon title */
781 if (icon->show_title && icon->icon_name != NULL) {
782 int l;
783 int w;
785 tmp = ShrinkString(scr->icon_title_font, icon->icon_name, wPreferences.icon_size - 4);
786 w = WMWidthOfString(scr->icon_title_font, tmp, l = strlen(tmp));
788 if (w > icon->core->width - 4)
789 x = (icon->core->width - 4) - w;
790 else
791 x = (icon->core->width - w) / 2;
793 WMDrawString(scr->wmscreen, icon->core->window, scr->icon_title_color,
794 scr->icon_title_font, x, 1, tmp, l);
795 wfree(tmp);
798 if (icon->selected)
799 XDrawRectangle(dpy, icon->core->window, scr->icon_select_gc, 0, 0,
800 icon->core->width - 1, icon->core->height - 1);
803 /******************************************************************/
805 static void miniwindowExpose(WObjDescriptor * desc, XEvent * event)
807 wIconPaint(desc->parent);
810 static void miniwindowDblClick(WObjDescriptor * desc, XEvent * event)
812 WIcon *icon = desc->parent;
814 assert(icon->owner != NULL);
816 wDeiconifyWindow(icon->owner);
819 static void miniwindowMouseDown(WObjDescriptor * desc, XEvent * event)
821 WIcon *icon = desc->parent;
822 WWindow *wwin = icon->owner;
823 XEvent ev;
824 int x = wwin->icon_x, y = wwin->icon_y;
825 int dx = event->xbutton.x, dy = event->xbutton.y;
826 int grabbed = 0;
827 int clickButton = event->xbutton.button;
828 Bool hasMoved = False;
830 if (WCHECK_STATE(WSTATE_MODAL))
831 return;
833 if (IsDoubleClick(icon->core->screen_ptr, event)) {
834 miniwindowDblClick(desc, event);
835 return;
838 if (event->xbutton.button == Button1) {
839 if (event->xbutton.state & MOD_MASK)
840 wLowerFrame(icon->core);
841 else
842 wRaiseFrame(icon->core);
843 if (event->xbutton.state & ShiftMask) {
844 wIconSelect(icon);
845 wSelectWindow(icon->owner, !wwin->flags.selected);
847 } else if (event->xbutton.button == Button3) {
848 WObjDescriptor *desc;
850 OpenMiniwindowMenu(wwin, event->xbutton.x_root, event->xbutton.y_root);
852 /* allow drag select of menu */
853 desc = &wwin->screen_ptr->window_menu->menu->descriptor;
854 event->xbutton.send_event = True;
855 (*desc->handle_mousedown) (desc, event);
857 return;
860 if (XGrabPointer(dpy, icon->core->window, False, ButtonMotionMask
861 | ButtonReleaseMask | ButtonPressMask, GrabModeAsync,
862 GrabModeAsync, None, None, CurrentTime) != GrabSuccess) {
864 while (1) {
865 WMMaskEvent(dpy, PointerMotionMask | ButtonReleaseMask | ButtonPressMask
866 | ButtonMotionMask | ExposureMask, &ev);
867 switch (ev.type) {
868 case Expose:
869 WMHandleEvent(&ev);
870 break;
872 case MotionNotify:
873 hasMoved = True;
874 if (!grabbed) {
875 if (abs(dx - ev.xmotion.x) >= MOVE_THRESHOLD
876 || abs(dy - ev.xmotion.y) >= MOVE_THRESHOLD) {
877 XChangeActivePointerGrab(dpy, ButtonMotionMask
878 | ButtonReleaseMask | ButtonPressMask,
879 wCursor[WCUR_MOVE], CurrentTime);
880 grabbed = 1;
881 } else {
882 break;
885 x = ev.xmotion.x_root - dx;
886 y = ev.xmotion.y_root - dy;
887 XMoveWindow(dpy, icon->core->window, x, y);
888 break;
890 case ButtonPress:
891 break;
893 case ButtonRelease:
894 if (ev.xbutton.button != clickButton)
895 break;
897 if (wwin->icon_x != x || wwin->icon_y != y)
898 wwin->flags.icon_moved = 1;
900 XMoveWindow(dpy, icon->core->window, x, y);
902 wwin->icon_x = x;
903 wwin->icon_y = y;
904 XUngrabPointer(dpy, CurrentTime);
906 if (wPreferences.auto_arrange_icons)
907 wArrangeIcons(wwin->screen_ptr, True);
908 if (wPreferences.single_click && !hasMoved)
909 miniwindowDblClick(desc, event);
910 return;