a94b4b2a7183cfbcb71582d980073a77f9260d96
[wmaker-crm.git] / src / icon.c
bloba94b4b2a7183cfbcb71582d980073a77f9260d96
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_rimage_icon_from_user_icon(WIcon *icon);
67 static void get_pixmap_icon_from_default_icon(WIcon *icon);
68 static void get_rimage_icon_from_default_icon(WIcon *icon);
70 static void icon_update_pixmap(WIcon *icon, RImage *image);
71 static void unset_icon_image(WIcon *icon);
73 static RImage *get_default_image(WScreen *scr);
74 /****** Notification Observers ******/
76 static void appearanceObserver(void *self, WMNotification * notif)
78 WIcon *icon = (WIcon *) self;
79 uintptr_t flags = (uintptr_t)WMGetNotificationClientData(notif);
81 if ((flags & WTextureSettings) || (flags & WFontSettings))
82 icon->force_paint = 1;
84 wIconPaint(icon);
86 /* so that the appicon expose handlers will paint the appicon specific
87 * stuff */
88 XClearArea(dpy, icon->core->window, 0, 0, icon->core->width, icon->core->height, True);
91 static void tileObserver(void *self, WMNotification * notif)
93 WIcon *icon = (WIcon *) self;
95 icon->force_paint = 1;
96 wIconPaint(icon);
98 XClearArea(dpy, icon->core->window, 0, 0, 1, 1, True);
101 /************************************/
103 static int getSize(Drawable d, unsigned int *w, unsigned int *h, unsigned int *dep)
105 Window rjunk;
106 int xjunk, yjunk;
107 unsigned int bjunk;
109 return XGetGeometry(dpy, d, &rjunk, &xjunk, &yjunk, w, h, &bjunk, dep);
112 WIcon *icon_create_for_wwindow(WWindow *wwin)
114 WScreen *scr = wwin->screen_ptr;
115 WIcon *icon;
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_default_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);
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_default_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);
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 icon->force_paint = 1;
349 wIconPaint(icon);
352 RImage *wIconValidateIconSize(RImage *icon, int max_size)
354 RImage *nimage;
356 if (!icon)
357 return NULL;
359 /* We should hold "ICON_BORDER" (~2) pixels to include the icon border */
360 if ((icon->width - max_size) > -ICON_BORDER ||
361 (icon->height - max_size) > -ICON_BORDER) {
362 nimage = RScaleImage(icon, max_size - ICON_BORDER,
363 (icon->height * (max_size - ICON_BORDER) / icon->width));
364 RReleaseImage(icon);
365 icon = nimage;
368 return icon;
371 Bool wIconChangeImageFile(WIcon *icon, char *file)
373 WScreen *scr = icon->core->screen_ptr;
374 char *path;
375 RImage *image = NULL;
376 int error = 0;
378 /* If no new image, don't do nothing */
379 if (!file)
380 return True;
382 /* Find the new image */
383 path = FindImage(wPreferences.icon_path, file);
384 if (path)
385 image = get_rimage_from_file(scr, path, wPreferences.icon_size);
386 else
387 error = 1;
389 /* New image! */
390 if (!error && image) {
391 /* Remove the old one */
392 unset_icon_image(icon);
394 /* Set the new image */
395 icon->file_image = image;
396 icon->file = wstrdup(path);
397 wIconUpdate(icon);
398 } else {
399 error = 1;
402 if (path)
403 wfree(path);
405 return !error;
408 static char *get_name_for_wwin(WWindow *wwin)
410 return get_name_for_instance_class(wwin->wm_instance, wwin->wm_class);
413 char *get_name_for_instance_class(char *wm_instance, char *wm_class)
415 char *suffix;
416 int len;
418 if (wm_class && wm_instance) {
419 len = strlen(wm_class) + strlen(wm_instance) + 2;
420 suffix = wmalloc(len);
421 snprintf(suffix, len, "%s.%s", wm_instance, wm_class);
422 } else if (wm_class) {
423 len = strlen(wm_class) + 1;
424 suffix = wmalloc(len);
425 snprintf(suffix, len, "%s", wm_class);
426 } else if (wm_instance) {
427 len = strlen(wm_instance) + 1;
428 suffix = wmalloc(len);
429 snprintf(suffix, len, "%s", wm_instance);
430 } else {
431 return NULL;
434 return suffix;
437 static char *get_icon_cache_path(void)
439 char *prefix, *path;
440 int len, ret;
442 prefix = wusergnusteppath();
443 len = strlen(prefix) + strlen(CACHE_ICON_PATH) + 2;
444 path = wmalloc(len);
445 snprintf(path, len, "%s%s/", prefix, CACHE_ICON_PATH);
447 /* If the folder exists, exit */
448 if (access(path, F_OK) == 0)
449 return path;
451 /* Create the folder */
452 ret = wmkdirhier((const char *) path);
454 /* Exit 1 on success, 0 on failure */
455 if (ret == 1)
456 return path;
458 /* Fail */
459 wfree(path);
460 return NULL;
463 static RImage *get_wwindow_image_from_wmhints(WWindow *wwin, WIcon *icon)
465 RImage *image = NULL;
466 XWMHints *hints = wwin->wm_hints;
468 if (hints && (hints->flags & IconPixmapHint) && hints->icon_pixmap != None)
469 image = RCreateImageFromDrawable(icon->core->screen_ptr->rcontext,
470 hints->icon_pixmap,
471 (hints->flags & IconMaskHint)
472 ? hints->icon_mask : None);
474 return image;
478 * wIconStore--
479 * Stores the client supplied icon at CACHE_ICON_PATH
480 * and returns the path for that icon. Returns NULL if there is no
481 * client supplied icon or on failure.
483 * Side effects:
484 * New directories might be created.
486 char *wIconStore(WIcon * icon)
488 char *path, *dir_path, *file;
489 int len = 0;
490 RImage *image = NULL;
491 WWindow *wwin = icon->owner;
493 if (!wwin)
494 return NULL;
496 dir_path = get_icon_cache_path();
497 if (!dir_path)
498 return NULL;
500 file = get_name_for_wwin(wwin);
501 if (!file) {
502 wfree(dir_path);
503 return NULL;
506 len = strlen(dir_path) + strlen(file) + 5;
507 path = wmalloc(len);
508 snprintf(path, len, "%s%s.xpm", dir_path, file);
509 wfree(dir_path);
510 wfree(file);
512 /* If icon exists, exit */
513 if (access(path, F_OK) == 0)
514 return path;
516 if (wwin->net_icon_image)
517 image = RRetainImage(wwin->net_icon_image);
518 else
519 image = get_wwindow_image_from_wmhints(wwin, icon);
521 if (!image) {
522 wfree(path);
523 return NULL;
526 if (!RSaveImage(image, path, "XPM")) {
527 wfree(path);
528 path = NULL;
531 RReleaseImage(image);
533 return path;
536 static void cycleColor(void *data)
538 WIcon *icon = (WIcon *) data;
539 WScreen *scr = icon->core->screen_ptr;
540 XGCValues gcv;
542 icon->step--;
543 gcv.dash_offset = icon->step;
544 XChangeGC(dpy, scr->icon_select_gc, GCDashOffset, &gcv);
546 XDrawRectangle(dpy, icon->core->window, scr->icon_select_gc, 0, 0,
547 icon->core->width - 1, icon->core->height - 1);
548 icon->handlerID = WMAddTimerHandler(COLOR_CYCLE_DELAY, cycleColor, icon);
551 #ifdef NEWAPPICON
552 void wIconSetHighlited(WIcon *icon, Bool flag)
554 if (icon->highlighted == flag)
555 return;
557 icon->highlighted = flag;
558 icon->force_paint = True;
559 wIconPaint(icon);
561 #endif
563 void wIconSelect(WIcon * icon)
565 WScreen *scr = icon->core->screen_ptr;
566 icon->selected = !icon->selected;
568 if (icon->selected) {
569 icon->step = 0;
570 if (!wPreferences.dont_blink)
571 icon->handlerID = WMAddTimerHandler(10, cycleColor, icon);
572 else
573 XDrawRectangle(dpy, icon->core->window, scr->icon_select_gc, 0, 0,
574 icon->core->width - 1, icon->core->height - 1);
575 } else {
576 if (icon->handlerID) {
577 WMDeleteTimerHandler(icon->handlerID);
578 icon->handlerID = NULL;
580 XClearArea(dpy, icon->core->window, 0, 0, icon->core->width, icon->core->height, True);
584 static void unset_icon_image(WIcon *icon)
586 if (icon->file)
587 wfree(icon->file);
589 if (icon->file_image)
590 RReleaseImage(icon->file_image);
593 void wIconUpdate(WIcon *icon)
595 WScreen *scr = icon->core->screen_ptr;
596 WWindow *wwin = icon->owner;
598 assert(scr->icon_tile != NULL);
600 if (icon->pixmap != None)
601 XFreePixmap(dpy, icon->pixmap);
603 icon->pixmap = None;
605 if (wwin && WFLAGP(wwin, always_user_icon)) {
606 /* Forced use user_icon */
607 get_pixmap_icon_from_user_icon(icon);
608 } else if (icon->icon_win != None) {
609 /* Get the Pixmap from the WIcon */
610 get_pixmap_icon_from_icon_win(icon);
611 } else if (wwin && wwin->net_icon_image) {
612 /* Use _NET_WM_ICON icon */
613 icon_update_pixmap(icon, wwin->net_icon_image);
614 } else if (wwin && wwin->wm_hints && (wwin->wm_hints->flags & IconPixmapHint)) {
615 /* Get the Pixmap from the wm_hints, else, from the user */
616 if (get_pixmap_icon_from_wm_hints(icon))
617 get_pixmap_icon_from_user_icon(icon);
618 } else {
619 /* Get the Pixmap from the user */
620 get_pixmap_icon_from_user_icon(icon);
623 /* No pixmap, set default background */
624 if (icon->pixmap != None)
625 XSetWindowBackgroundPixmap(dpy, icon->core->window, icon->pixmap);
627 /* Paint it */
628 XClearWindow(dpy, icon->core->window);
629 wIconPaint(icon);
632 static void get_rimage_icon_from_user_icon(WIcon *icon)
634 if (icon->file_image)
635 return;
637 get_rimage_icon_from_default_icon(icon);
640 static void get_pixmap_icon_from_user_icon(WIcon *icon)
642 /* Set the icon->file_image */
643 get_rimage_icon_from_user_icon(icon);
645 /* Update icon->pixmap */
646 icon_update_pixmap(icon, icon->file_image);
649 static void get_rimage_icon_from_default_icon(WIcon *icon)
651 WScreen *scr = icon->core->screen_ptr;
653 /* If the icon don't have image, we should use the default image. */
654 if (!scr->def_icon_rimage)
655 scr->def_icon_rimage = get_default_image(scr);
657 /* Remove the icon image */
658 unset_icon_image(icon);
660 /* Set the new icon image */
661 icon->file = NULL;
662 icon->file_image = RRetainImage(scr->def_icon_rimage);
665 static void get_pixmap_icon_from_default_icon(WIcon *icon)
667 /* Update icon->file image */
668 get_rimage_icon_from_default_icon(icon);
670 /* Now, create the pixmap using the default (saved) image */
671 icon_update_pixmap(icon, icon->file_image);
674 /* This function creates the RImage using the default icon */
675 static RImage *get_default_image(WScreen *scr)
677 RImage *image = NULL;
678 char *path, *file;
680 /* Get the default icon */
681 file = wDefaultGetIconFile(NULL, NULL, True);
682 if (file) {
683 path = FindImage(wPreferences.icon_path, file);
684 image = get_rimage_from_file(scr, path, wPreferences.icon_size);
686 if (!image)
687 wwarning(_("could not find default icon \"%s\""), file);
689 wfree(file);
692 return image;
695 /* Get the Pixmap from the WIcon of the WWindow */
696 static void get_pixmap_icon_from_icon_win(WIcon * icon)
698 XWindowAttributes attr;
699 WScreen *scr = icon->core->screen_ptr;
700 int title_height = WMFontHeight(scr->icon_title_font);
701 unsigned int width, height, depth;
702 int theight;
703 int resize = 0;
704 Pixmap pixmap;
706 getSize(icon->icon_win, &width, &height, &depth);
708 if (width > wPreferences.icon_size) {
709 resize = 1;
710 width = wPreferences.icon_size;
713 if (height > wPreferences.icon_size) {
714 resize = 1;
715 height = wPreferences.icon_size;
718 if (icon->show_title && (height + title_height < wPreferences.icon_size)) {
719 pixmap = XCreatePixmap(dpy, scr->w_win, wPreferences.icon_size,
720 wPreferences.icon_size, scr->w_depth);
721 XSetClipMask(dpy, scr->copy_gc, None);
722 XCopyArea(dpy, scr->icon_tile_pixmap, pixmap, scr->copy_gc, 0, 0,
723 wPreferences.icon_size, wPreferences.icon_size, 0, 0);
724 drawIconTitle(scr, pixmap, title_height);
725 theight = title_height;
726 } else {
727 pixmap = None;
728 theight = 0;
729 XSetWindowBackgroundPixmap(dpy, icon->core->window, scr->icon_tile_pixmap);
732 XSetWindowBorderWidth(dpy, icon->icon_win, 0);
733 XReparentWindow(dpy, icon->icon_win, icon->core->window,
734 (wPreferences.icon_size - width) / 2,
735 theight + (wPreferences.icon_size - height - theight) / 2);
736 if (resize)
737 XResizeWindow(dpy, icon->icon_win, width, height);
739 XMapWindow(dpy, icon->icon_win);
740 XAddToSaveSet(dpy, icon->icon_win);
742 /* Save it */
743 icon->pixmap = pixmap;
745 if ((XGetWindowAttributes(dpy, icon->icon_win, &attr)) &&
746 (attr.all_event_masks & ButtonPressMask))
747 wHackedGrabButton(Button1, MOD_MASK, icon->core->window, True,
748 ButtonPressMask, GrabModeSync, GrabModeAsync,
749 None, wCursor[WCUR_ARROW]);
752 /* Get the Pixmap from the XWindow wm_hints */
753 static int get_pixmap_icon_from_wm_hints(WIcon *icon)
755 RImage *image = NULL;
756 unsigned int w, h, d;
757 WWindow *wwin = icon->owner;
759 if (!getSize(wwin->wm_hints->icon_pixmap, &w, &h, &d)) {
760 icon->owner->wm_hints->flags &= ~IconPixmapHint;
761 return 1;
764 image = get_wwindow_image_from_wmhints(wwin, icon);
765 if (!image)
766 return 1;
768 icon_update_pixmap(icon, image);
770 return 0;
773 void wIconPaint(WIcon * icon)
775 WScreen *scr = icon->core->screen_ptr;
776 int x;
777 char *tmp;
779 if (icon->force_paint) {
780 icon->force_paint = 0;
781 wIconUpdate(icon);
782 return;
785 XClearWindow(dpy, icon->core->window);
787 /* draw the icon title */
788 if (icon->show_title && icon->icon_name != NULL) {
789 int l;
790 int w;
792 tmp = ShrinkString(scr->icon_title_font, icon->icon_name, wPreferences.icon_size - 4);
793 w = WMWidthOfString(scr->icon_title_font, tmp, l = strlen(tmp));
795 if (w > icon->core->width - 4)
796 x = (icon->core->width - 4) - w;
797 else
798 x = (icon->core->width - w) / 2;
800 WMDrawString(scr->wmscreen, icon->core->window, scr->icon_title_color,
801 scr->icon_title_font, x, 1, tmp, l);
802 wfree(tmp);
805 if (icon->selected)
806 XDrawRectangle(dpy, icon->core->window, scr->icon_select_gc, 0, 0,
807 icon->core->width - 1, icon->core->height - 1);
810 /******************************************************************/
812 static void miniwindowExpose(WObjDescriptor * desc, XEvent * event)
814 wIconPaint(desc->parent);
817 static void miniwindowDblClick(WObjDescriptor * desc, XEvent * event)
819 WIcon *icon = desc->parent;
821 assert(icon->owner != NULL);
823 wDeiconifyWindow(icon->owner);
826 static void miniwindowMouseDown(WObjDescriptor * desc, XEvent * event)
828 WIcon *icon = desc->parent;
829 WWindow *wwin = icon->owner;
830 XEvent ev;
831 int x = wwin->icon_x, y = wwin->icon_y;
832 int dx = event->xbutton.x, dy = event->xbutton.y;
833 int grabbed = 0;
834 int clickButton = event->xbutton.button;
835 Bool hasMoved = False;
837 if (WCHECK_STATE(WSTATE_MODAL))
838 return;
840 if (IsDoubleClick(icon->core->screen_ptr, event)) {
841 miniwindowDblClick(desc, event);
842 return;
845 if (event->xbutton.button == Button1) {
846 if (event->xbutton.state & MOD_MASK)
847 wLowerFrame(icon->core);
848 else
849 wRaiseFrame(icon->core);
850 if (event->xbutton.state & ShiftMask) {
851 wIconSelect(icon);
852 wSelectWindow(icon->owner, !wwin->flags.selected);
854 } else if (event->xbutton.button == Button3) {
855 WObjDescriptor *desc;
857 OpenMiniwindowMenu(wwin, event->xbutton.x_root, event->xbutton.y_root);
859 /* allow drag select of menu */
860 desc = &wwin->screen_ptr->window_menu->menu->descriptor;
861 event->xbutton.send_event = True;
862 (*desc->handle_mousedown) (desc, event);
864 return;
867 if (XGrabPointer(dpy, icon->core->window, False, ButtonMotionMask
868 | ButtonReleaseMask | ButtonPressMask, GrabModeAsync,
869 GrabModeAsync, None, None, CurrentTime) != GrabSuccess) {
871 while (1) {
872 WMMaskEvent(dpy, PointerMotionMask | ButtonReleaseMask | ButtonPressMask
873 | ButtonMotionMask | ExposureMask, &ev);
874 switch (ev.type) {
875 case Expose:
876 WMHandleEvent(&ev);
877 break;
879 case MotionNotify:
880 hasMoved = True;
881 if (!grabbed) {
882 if (abs(dx - ev.xmotion.x) >= MOVE_THRESHOLD
883 || abs(dy - ev.xmotion.y) >= MOVE_THRESHOLD) {
884 XChangeActivePointerGrab(dpy, ButtonMotionMask
885 | ButtonReleaseMask | ButtonPressMask,
886 wCursor[WCUR_MOVE], CurrentTime);
887 grabbed = 1;
888 } else {
889 break;
892 x = ev.xmotion.x_root - dx;
893 y = ev.xmotion.y_root - dy;
894 XMoveWindow(dpy, icon->core->window, x, y);
895 break;
897 case ButtonPress:
898 break;
900 case ButtonRelease:
901 if (ev.xbutton.button != clickButton)
902 break;
904 if (wwin->icon_x != x || wwin->icon_y != y)
905 wwin->flags.icon_moved = 1;
907 XMoveWindow(dpy, icon->core->window, x, y);
909 wwin->icon_x = x;
910 wwin->icon_y = y;
911 XUngrabPointer(dpy, CurrentTime);
913 if (wPreferences.auto_arrange_icons)
914 wArrangeIcons(wwin->screen_ptr, True);
915 if (wPreferences.single_click && !hasMoved)
916 miniwindowDblClick(desc, event);
917 return;