f1f9c67d8f8dd428e40f5de31bcd89895bfe6472
[wmaker-crm.git] / src / icon.c
blobf1f9c67d8f8dd428e40f5de31bcd89895bfe6472
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 int get_rimage_icon_from_wm_hints(WIcon *icon);
66 static void get_rimage_icon_from_user_icon(WIcon *icon);
67 static void get_rimage_icon_from_default_icon(WIcon *icon);
68 static void get_rimage_icon_from_x11(WIcon *icon);
70 static void icon_update_pixmap(WIcon *icon, RImage *image);
71 static void unset_icon_image(WIcon *icon);
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 /* If the rimage exists, update the icon, else create it */
82 if (icon->file_image)
83 update_icon_pixmap(icon);
84 else
85 wIconPaint(icon);
88 /* so that the appicon expose handlers will paint the appicon specific
89 * stuff */
90 XClearArea(dpy, icon->core->window, 0, 0, icon->core->width, icon->core->height, True);
93 static void tileObserver(void *self, WMNotification *notif)
95 WIcon *icon = (WIcon *) self;
97 wIconUpdate(icon, NULL);
99 XClearArea(dpy, icon->core->window, 0, 0, 1, 1, True);
102 /************************************/
104 static int getSize(Drawable d, unsigned int *w, unsigned int *h, unsigned int *dep)
106 Window rjunk;
107 int xjunk, yjunk;
108 unsigned int bjunk;
110 return XGetGeometry(dpy, d, &rjunk, &xjunk, &yjunk, w, h, &bjunk, dep);
113 WIcon *icon_create_for_wwindow(WWindow *wwin)
115 WScreen *scr = wwin->screen_ptr;
116 WIcon *icon;
117 char *file;
119 icon = icon_create_core(scr, wwin->icon_x, wwin->icon_y);
121 icon->owner = wwin;
122 if (wwin->wm_hints && (wwin->wm_hints->flags & IconWindowHint)) {
123 if (wwin->client_win == wwin->main_window) {
124 WApplication *wapp;
125 /* do not let miniwindow steal app-icon's icon window */
126 wapp = wApplicationOf(wwin->client_win);
127 if (!wapp || wapp->app_icon == NULL)
128 icon->icon_win = wwin->wm_hints->icon_window;
129 } else {
130 icon->icon_win = wwin->wm_hints->icon_window;
133 #ifdef NO_MINIWINDOW_TITLES
134 icon->show_title = 0;
135 #else
136 icon->show_title = 1;
137 #endif
139 icon->icon_name = wNETWMGetIconName(wwin->client_win);
140 if (icon->icon_name)
141 wwin->flags.net_has_icon_title = 1;
142 else
143 wGetIconName(dpy, wwin->client_win, &icon->icon_name);
145 /* Get the application icon, default included */
146 file = get_icon_filename(scr, wwin->wm_instance, wwin->wm_class, NULL, True);
147 if (file) {
148 icon->file = wstrdup(file);
149 icon->file_image = get_rimage_from_file(scr, icon->file, wPreferences.icon_size);
150 wfree(file);
153 icon->tile_type = TILE_NORMAL;
155 wIconUpdate(icon, NULL);
157 WMAddNotificationObserver(appearanceObserver, icon, WNIconAppearanceSettingsChanged, icon);
158 WMAddNotificationObserver(tileObserver, icon, WNIconTileSettingsChanged, icon);
160 return icon;
163 WIcon *icon_create_for_dock(WScreen *scr, char *command, char *wm_instance, char *wm_class, int tile)
165 WIcon *icon;
166 char *file = NULL;
168 icon = icon_create_core(scr, 0, 0);
170 /* Search the icon using instance and class, without default icon */
171 file = get_icon_filename(scr, wm_instance, wm_class, command, False);
172 if (file) {
173 icon->file = wstrdup(file);
174 icon->file_image = get_rimage_from_file(scr, icon->file, wPreferences.icon_size);
175 wfree(file);
178 icon->tile_type = tile;
180 wIconUpdate(icon, NULL);
182 WMAddNotificationObserver(appearanceObserver, icon, WNIconAppearanceSettingsChanged, icon);
183 WMAddNotificationObserver(tileObserver, icon, WNIconTileSettingsChanged, icon);
185 return icon;
188 static WIcon *icon_create_core(WScreen *scr, int coord_x, int coord_y)
190 WIcon *icon;
191 unsigned long vmask = 0;
192 XSetWindowAttributes attribs;
194 icon = wmalloc(sizeof(WIcon));
195 icon->core = wCoreCreateTopLevel(scr,
196 coord_x,
197 coord_y,
198 wPreferences.icon_size,
199 wPreferences.icon_size,
200 0, scr->w_depth, scr->w_visual, scr->w_colormap);
202 if (wPreferences.use_saveunders) {
203 vmask = CWSaveUnder;
204 attribs.save_under = True;
207 /* a white border for selecting it */
208 vmask |= CWBorderPixel;
209 attribs.border_pixel = scr->white_pixel;
211 XChangeWindowAttributes(dpy, icon->core->window, vmask, &attribs);
213 /* will be overriden if this is a application icon */
214 icon->core->descriptor.handle_mousedown = miniwindowMouseDown;
215 icon->core->descriptor.handle_expose = miniwindowExpose;
216 icon->core->descriptor.parent_type = WCLASS_MINIWINDOW;
217 icon->core->descriptor.parent = icon;
219 icon->core->stacking = wmalloc(sizeof(WStacking));
220 icon->core->stacking->above = NULL;
221 icon->core->stacking->under = NULL;
222 icon->core->stacking->window_level = NORMAL_ICON_LEVEL;
223 icon->core->stacking->child_of = NULL;
225 /* Icon image */
226 icon->file = NULL;
227 icon->file_image = NULL;
229 return icon;
232 void wIconDestroy(WIcon * icon)
234 WCoreWindow *core = icon->core;
235 WScreen *scr = core->screen_ptr;
237 WMRemoveNotificationObserver(icon);
239 if (icon->handlerID)
240 WMDeleteTimerHandler(icon->handlerID);
242 if (icon->icon_win) {
243 int x = 0, y = 0;
245 if (icon->owner) {
246 x = icon->owner->icon_x;
247 y = icon->owner->icon_y;
249 XUnmapWindow(dpy, icon->icon_win);
250 XReparentWindow(dpy, icon->icon_win, scr->root_win, x, y);
252 if (icon->icon_name)
253 XFree(icon->icon_name);
255 if (icon->pixmap)
256 XFreePixmap(dpy, icon->pixmap);
258 unset_icon_image(icon);
260 wCoreDestroy(icon->core);
261 wfree(icon);
264 static void drawIconTitle(WScreen * scr, Pixmap pixmap, int height)
266 XFillRectangle(dpy, pixmap, scr->icon_title_texture->normal_gc, 0, 0, wPreferences.icon_size, height + 1);
267 XDrawLine(dpy, pixmap, scr->icon_title_texture->light_gc, 0, 0, wPreferences.icon_size, 0);
268 XDrawLine(dpy, pixmap, scr->icon_title_texture->light_gc, 0, 0, 0, height + 1);
269 XDrawLine(dpy, pixmap, scr->icon_title_texture->dim_gc,
270 wPreferences.icon_size - 1, 0, wPreferences.icon_size - 1, height + 1);
273 static void icon_update_pixmap(WIcon *icon, RImage *image)
275 RImage *tile;
276 Pixmap pixmap;
277 int x, y, sx, sy;
278 unsigned w, h;
279 int theight = 0;
280 WScreen *scr = icon->core->screen_ptr;
281 int titled = icon->show_title;
283 if (icon->tile_type == TILE_NORMAL) {
284 tile = RCloneImage(scr->icon_tile);
285 } else {
286 assert(scr->clip_tile);
287 tile = RCloneImage(scr->clip_tile);
290 if (image) {
291 w = (image->width > wPreferences.icon_size)
292 ? wPreferences.icon_size : image->width;
293 x = (wPreferences.icon_size - w) / 2;
294 sx = (image->width - w) / 2;
296 if (titled)
297 theight = WMFontHeight(scr->icon_title_font);
299 h = (image->height + theight > wPreferences.icon_size
300 ? wPreferences.icon_size - theight : image->height);
301 y = theight + (wPreferences.icon_size - theight - h) / 2;
302 sy = (image->height - h) / 2;
304 RCombineArea(tile, image, sx, sy, w, h, x, y);
307 if (icon->shadowed) {
308 RColor color;
310 color.red = scr->icon_back_texture->light.red >> 8;
311 color.green = scr->icon_back_texture->light.green >> 8;
312 color.blue = scr->icon_back_texture->light.blue >> 8;
313 color.alpha = 150; /* about 60% */
314 RClearImage(tile, &color);
317 if (icon->highlighted) {
318 RColor color;
320 color.red = color.green = color.blue = 0;
321 color.alpha = 160;
322 RLightImage(tile, &color);
325 if (!RConvertImage(scr->rcontext, tile, &pixmap))
326 wwarning(_("error rendering image:%s"), RMessageForError(RErrorCode));
328 RReleaseImage(tile);
330 if (titled)
331 drawIconTitle(scr, pixmap, theight);
333 icon->pixmap = pixmap;
336 void wIconChangeTitle(WIcon *icon, char *new_title)
338 int changed;
340 changed = (new_title == NULL && icon->icon_name != NULL) ||
341 (new_title != NULL && icon->icon_name == NULL);
343 if (icon->icon_name != NULL)
344 XFree(icon->icon_name);
346 icon->icon_name = new_title;
348 if (changed)
349 wIconUpdate(icon, NULL);
350 else
351 wIconPaint(icon);
354 RImage *wIconValidateIconSize(RImage *icon, int max_size)
356 RImage *nimage;
358 if (!icon)
359 return NULL;
361 /* We should hold "ICON_BORDER" (~2) pixels to include the icon border */
362 if (((max_size + ICON_BORDER) < icon->width) ||
363 ((max_size + ICON_BORDER) < icon->height)) {
364 if (icon->width > icon->height)
365 nimage = RScaleImage(icon, max_size - ICON_BORDER,
366 (icon->height * (max_size - ICON_BORDER) / icon->width));
367 else
368 nimage = RScaleImage(icon, (icon->width * (max_size - ICON_BORDER) / icon->height),
369 max_size - ICON_BORDER);
370 RReleaseImage(icon);
371 icon = nimage;
374 return icon;
377 Bool wIconChangeImageFile(WIcon *icon, char *file)
379 WScreen *scr = icon->core->screen_ptr;
380 char *path;
381 RImage *image = NULL;
382 int error = 0;
384 /* If no new image, don't do nothing */
385 if (!file)
386 return True;
388 /* Find the new image */
389 path = FindImage(wPreferences.icon_path, file);
390 if (path)
391 image = get_rimage_from_file(scr, path, wPreferences.icon_size);
392 else
393 error = 1;
395 /* New image! */
396 if (!error && image) {
397 /* Remove the old one */
398 unset_icon_image(icon);
400 /* Set the new image */
401 icon->file = wstrdup(path);
402 wIconUpdate(icon, image);
403 } else {
404 error = 1;
407 if (path)
408 wfree(path);
410 return !error;
413 static char *get_name_for_wwin(WWindow *wwin)
415 return get_name_for_instance_class(wwin->wm_instance, wwin->wm_class);
418 char *get_name_for_instance_class(char *wm_instance, char *wm_class)
420 char *suffix;
421 int len;
423 if (wm_class && wm_instance) {
424 len = strlen(wm_class) + strlen(wm_instance) + 2;
425 suffix = wmalloc(len);
426 snprintf(suffix, len, "%s.%s", wm_instance, wm_class);
427 } else if (wm_class) {
428 len = strlen(wm_class) + 1;
429 suffix = wmalloc(len);
430 snprintf(suffix, len, "%s", wm_class);
431 } else if (wm_instance) {
432 len = strlen(wm_instance) + 1;
433 suffix = wmalloc(len);
434 snprintf(suffix, len, "%s", wm_instance);
435 } else {
436 return NULL;
439 return suffix;
442 static char *get_icon_cache_path(void)
444 char *prefix, *path;
445 int len, ret;
447 prefix = wusergnusteppath();
448 len = strlen(prefix) + strlen(CACHE_ICON_PATH) + 2;
449 path = wmalloc(len);
450 snprintf(path, len, "%s%s/", prefix, CACHE_ICON_PATH);
452 /* If the folder exists, exit */
453 if (access(path, F_OK) == 0)
454 return path;
456 /* Create the folder */
457 ret = wmkdirhier((const char *) path);
459 /* Exit 1 on success, 0 on failure */
460 if (ret == 1)
461 return path;
463 /* Fail */
464 wfree(path);
465 return NULL;
468 static RImage *get_wwindow_image_from_wmhints(WWindow *wwin, WIcon *icon)
470 RImage *image = NULL;
471 XWMHints *hints = wwin->wm_hints;
473 if (hints && (hints->flags & IconPixmapHint) && hints->icon_pixmap != None)
474 image = RCreateImageFromDrawable(icon->core->screen_ptr->rcontext,
475 hints->icon_pixmap,
476 (hints->flags & IconMaskHint)
477 ? hints->icon_mask : None);
479 return image;
483 * wIconStore--
484 * Stores the client supplied icon at CACHE_ICON_PATH
485 * and returns the path for that icon. Returns NULL if there is no
486 * client supplied icon or on failure.
488 * Side effects:
489 * New directories might be created.
491 char *wIconStore(WIcon * icon)
493 char *path, *dir_path, *file;
494 int len = 0;
495 RImage *image = NULL;
496 WWindow *wwin = icon->owner;
498 if (!wwin)
499 return NULL;
501 dir_path = get_icon_cache_path();
502 if (!dir_path)
503 return NULL;
505 file = get_name_for_wwin(wwin);
506 if (!file) {
507 wfree(dir_path);
508 return NULL;
511 len = strlen(dir_path) + strlen(file) + 5;
512 path = wmalloc(len);
513 snprintf(path, len, "%s%s.xpm", dir_path, file);
514 wfree(dir_path);
515 wfree(file);
517 /* If icon exists, exit */
518 if (access(path, F_OK) == 0)
519 return path;
521 if (wwin->net_icon_image)
522 image = RRetainImage(wwin->net_icon_image);
523 else
524 image = get_wwindow_image_from_wmhints(wwin, icon);
526 if (!image) {
527 wfree(path);
528 return NULL;
531 if (!RSaveImage(image, path, "XPM")) {
532 wfree(path);
533 path = NULL;
536 RReleaseImage(image);
538 return path;
541 static void cycleColor(void *data)
543 WIcon *icon = (WIcon *) data;
544 WScreen *scr = icon->core->screen_ptr;
545 XGCValues gcv;
547 icon->step--;
548 gcv.dash_offset = icon->step;
549 XChangeGC(dpy, scr->icon_select_gc, GCDashOffset, &gcv);
551 XDrawRectangle(dpy, icon->core->window, scr->icon_select_gc, 0, 0,
552 icon->core->width - 1, icon->core->height - 1);
553 icon->handlerID = WMAddTimerHandler(COLOR_CYCLE_DELAY, cycleColor, icon);
556 void wIconSetHighlited(WIcon *icon, Bool flag)
558 if (icon->highlighted == flag)
559 return;
561 icon->highlighted = flag;
562 update_icon_pixmap(icon);
565 void wIconSelect(WIcon * icon)
567 WScreen *scr = icon->core->screen_ptr;
568 icon->selected = !icon->selected;
570 if (icon->selected) {
571 icon->step = 0;
572 if (!wPreferences.dont_blink)
573 icon->handlerID = WMAddTimerHandler(10, cycleColor, icon);
574 else
575 XDrawRectangle(dpy, icon->core->window, scr->icon_select_gc, 0, 0,
576 icon->core->width - 1, icon->core->height - 1);
577 } else {
578 if (icon->handlerID) {
579 WMDeleteTimerHandler(icon->handlerID);
580 icon->handlerID = NULL;
582 XClearArea(dpy, icon->core->window, 0, 0, icon->core->width, icon->core->height, True);
586 static void unset_icon_image(WIcon *icon)
588 if (icon->file) {
589 wfree(icon->file);
590 icon->file = NULL;
593 if (icon->file_image) {
594 RReleaseImage(icon->file_image);
595 icon->file_image = NULL;
599 void wIconUpdate(WIcon *icon, RImage *image)
601 WWindow *wwin = NULL;
603 if (image) {
604 icon->file_image = image;
605 } else {
606 if (icon && icon->owner)
607 wwin = icon->owner;
609 if (wwin && WFLAGP(wwin, always_user_icon)) {
610 /* Forced use user_icon */
611 get_rimage_icon_from_user_icon(icon);
612 } else if (icon->icon_win != None) {
613 /* Get the Pixmap from the WIcon */
614 get_rimage_icon_from_icon_win(icon);
615 } else if (wwin && wwin->net_icon_image) {
616 /* Use _NET_WM_ICON icon */
617 get_rimage_icon_from_x11(icon);
618 } else if (wwin && wwin->wm_hints && (wwin->wm_hints->flags & IconPixmapHint)) {
619 /* Get the Pixmap from the wm_hints, else, from the user */
620 if (get_rimage_icon_from_wm_hints(icon))
621 get_rimage_icon_from_user_icon(icon);
622 } else {
623 /* Get the Pixmap from the user */
624 get_rimage_icon_from_user_icon(icon);
628 update_icon_pixmap(icon);
631 void update_icon_pixmap(WIcon *icon)
633 if (icon->pixmap != None)
634 XFreePixmap(dpy, icon->pixmap);
636 icon->pixmap = None;
638 /* Create the pixmap */
639 if (icon->file_image)
640 icon_update_pixmap(icon, icon->file_image);
642 /* If dockapp, put inside the icon */
643 if (icon->icon_win != None) {
644 /* file_image is NULL, because is docked app */
645 icon_update_pixmap(icon, NULL);
646 set_dockapp_in_icon(icon);
649 /* No pixmap, set default background */
650 if (icon->pixmap != None)
651 XSetWindowBackgroundPixmap(dpy, icon->core->window, icon->pixmap);
653 /* Paint it */
654 XClearWindow(dpy, icon->core->window);
655 wIconPaint(icon);
658 static void get_rimage_icon_from_x11(WIcon *icon)
660 /* Remove the icon image */
661 unset_icon_image(icon);
663 /* Set the new icon image */
664 icon->file_image = RRetainImage(icon->owner->net_icon_image);
667 static void get_rimage_icon_from_user_icon(WIcon *icon)
669 if (icon->file_image)
670 return;
672 get_rimage_icon_from_default_icon(icon);
675 static void get_rimage_icon_from_default_icon(WIcon *icon)
677 WScreen *scr = icon->core->screen_ptr;
679 /* If the icon don't have image, we should use the default image. */
680 if (!scr->def_icon_rimage)
681 scr->def_icon_rimage = get_default_image(scr);
683 /* Remove the icon image */
684 unset_icon_image(icon);
686 /* Set the new icon image */
687 icon->file_image = RRetainImage(scr->def_icon_rimage);
690 /* Get the RImage from the WIcon of the WWindow */
691 static void get_rimage_icon_from_icon_win(WIcon *icon)
693 RImage *image;
695 /* Create the new RImage */
696 image = get_window_image_from_x11(icon->icon_win);
698 /* Free the icon info */
699 unset_icon_image(icon);
701 /* Set the new info */
702 icon->file_image = image;
705 /* Set the dockapp in the WIcon */
706 static void set_dockapp_in_icon(WIcon *icon)
708 XWindowAttributes attr;
709 WScreen *scr = icon->core->screen_ptr;
710 int title_height = WMFontHeight(scr->icon_title_font);
711 unsigned int w, h, d;
712 int theight = 0;
714 /* Reparent the dock application to the icon */
716 /* We need the application size to center it
717 * and show in the correct position */
718 getSize(icon->icon_win, &w, &h, &d);
720 /* Set extra space for title */
721 if (icon->show_title && (h + title_height < wPreferences.icon_size)) {
722 theight = title_height;
723 drawIconTitle(scr, icon->pixmap, theight);
724 } else {
725 XSetWindowBackgroundPixmap(dpy, icon->core->window, scr->icon_tile_pixmap);
728 /* Set the icon border */
729 XSetWindowBorderWidth(dpy, icon->icon_win, 0);
731 /* Put the dock application in the icon */
732 XReparentWindow(dpy, icon->icon_win, icon->core->window,
733 (wPreferences.icon_size - w) / 2,
734 theight + (wPreferences.icon_size - h - theight) / 2);
736 /* Show it and save */
737 XMapWindow(dpy, icon->icon_win);
738 XAddToSaveSet(dpy, icon->icon_win);
740 /* Needed to move the icon clicking on the application part */
741 if ((XGetWindowAttributes(dpy, icon->icon_win, &attr)) &&
742 (attr.all_event_masks & ButtonPressMask))
743 wHackedGrabButton(Button1, MOD_MASK, icon->core->window, True,
744 ButtonPressMask, GrabModeSync, GrabModeAsync,
745 None, wCursor[WCUR_ARROW]);
748 /* Get the RImage from the XWindow wm_hints */
749 static int get_rimage_icon_from_wm_hints(WIcon *icon)
751 RImage *image = NULL;
752 unsigned int w, h, d;
753 WWindow *wwin = icon->owner;
755 if (!getSize(wwin->wm_hints->icon_pixmap, &w, &h, &d)) {
756 icon->owner->wm_hints->flags &= ~IconPixmapHint;
757 return 1;
760 image = get_wwindow_image_from_wmhints(wwin, icon);
761 if (!image)
762 return 1;
764 /* Resize the icon to the wPreferences.icon_size size */
765 image = wIconValidateIconSize(image, wPreferences.icon_size);
767 unset_icon_image(icon);
768 icon->file_image = image;
770 return 0;
773 void wIconPaint(WIcon *icon)
775 WScreen *scr = icon->core->screen_ptr;
776 int x, l, w;
777 char *tmp;
779 XClearWindow(dpy, icon->core->window);
781 /* draw the icon title */
782 if (icon->show_title && icon->icon_name != NULL) {
783 tmp = ShrinkString(scr->icon_title_font, icon->icon_name, wPreferences.icon_size - 4);
784 w = WMWidthOfString(scr->icon_title_font, tmp, l = strlen(tmp));
786 if (w > icon->core->width - 4)
787 x = (icon->core->width - 4) - w;
788 else
789 x = (icon->core->width - w) / 2;
791 WMDrawString(scr->wmscreen, icon->core->window, scr->icon_title_color,
792 scr->icon_title_font, x, 1, tmp, l);
793 wfree(tmp);
796 if (icon->selected)
797 XDrawRectangle(dpy, icon->core->window, scr->icon_select_gc, 0, 0,
798 icon->core->width - 1, icon->core->height - 1);
801 /******************************************************************/
803 static void miniwindowExpose(WObjDescriptor * desc, XEvent * event)
805 wIconPaint(desc->parent);
808 static void miniwindowDblClick(WObjDescriptor * desc, XEvent * event)
810 WIcon *icon = desc->parent;
812 assert(icon->owner != NULL);
814 wDeiconifyWindow(icon->owner);
817 static void miniwindowMouseDown(WObjDescriptor * desc, XEvent * event)
819 WIcon *icon = desc->parent;
820 WWindow *wwin = icon->owner;
821 XEvent ev;
822 int x = wwin->icon_x, y = wwin->icon_y;
823 int dx = event->xbutton.x, dy = event->xbutton.y;
824 int grabbed = 0;
825 int clickButton = event->xbutton.button;
826 Bool hasMoved = False;
828 if (WCHECK_STATE(WSTATE_MODAL))
829 return;
831 if (IsDoubleClick(icon->core->screen_ptr, event)) {
832 miniwindowDblClick(desc, event);
833 return;
836 if (event->xbutton.button == Button1) {
837 if (event->xbutton.state & MOD_MASK)
838 wLowerFrame(icon->core);
839 else
840 wRaiseFrame(icon->core);
841 if (event->xbutton.state & ShiftMask) {
842 wIconSelect(icon);
843 wSelectWindow(icon->owner, !wwin->flags.selected);
845 } else if (event->xbutton.button == Button3) {
846 WObjDescriptor *desc;
848 OpenMiniwindowMenu(wwin, event->xbutton.x_root, event->xbutton.y_root);
850 /* allow drag select of menu */
851 desc = &wwin->screen_ptr->window_menu->menu->descriptor;
852 event->xbutton.send_event = True;
853 (*desc->handle_mousedown) (desc, event);
855 return;
858 if (XGrabPointer(dpy, icon->core->window, False, ButtonMotionMask
859 | ButtonReleaseMask | ButtonPressMask, GrabModeAsync,
860 GrabModeAsync, None, None, CurrentTime) != GrabSuccess) {
862 while (1) {
863 WMMaskEvent(dpy, PointerMotionMask | ButtonReleaseMask | ButtonPressMask
864 | ButtonMotionMask | ExposureMask, &ev);
865 switch (ev.type) {
866 case Expose:
867 WMHandleEvent(&ev);
868 break;
870 case MotionNotify:
871 hasMoved = True;
872 if (!grabbed) {
873 if (abs(dx - ev.xmotion.x) >= MOVE_THRESHOLD
874 || abs(dy - ev.xmotion.y) >= MOVE_THRESHOLD) {
875 XChangeActivePointerGrab(dpy, ButtonMotionMask
876 | ButtonReleaseMask | ButtonPressMask,
877 wCursor[WCUR_MOVE], CurrentTime);
878 grabbed = 1;
879 } else {
880 break;
883 x = ev.xmotion.x_root - dx;
884 y = ev.xmotion.y_root - dy;
885 XMoveWindow(dpy, icon->core->window, x, y);
886 break;
888 case ButtonPress:
889 break;
891 case ButtonRelease:
892 if (ev.xbutton.button != clickButton)
893 break;
895 if (wwin->icon_x != x || wwin->icon_y != y)
896 wwin->flags.icon_moved = 1;
898 XMoveWindow(dpy, icon->core->window, x, y);
900 wwin->icon_x = x;
901 wwin->icon_y = y;
902 XUngrabPointer(dpy, CurrentTime);
904 if (wPreferences.auto_arrange_icons)
905 wArrangeIcons(wwin->screen_ptr, True);
906 if (wPreferences.single_click && !hasMoved)
907 miniwindowDblClick(desc, event);
908 return;