wIconUpdate removed scr variable
[wmaker-crm.git] / src / icon.c
blob6a0e817a8479829686ba84a397ed668fdd840c62
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);
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);
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);
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);
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_image = image;
402 icon->file = wstrdup(path);
403 wIconUpdate(icon);
404 } else {
405 error = 1;
408 if (path)
409 wfree(path);
411 return !error;
414 static char *get_name_for_wwin(WWindow *wwin)
416 return get_name_for_instance_class(wwin->wm_instance, wwin->wm_class);
419 char *get_name_for_instance_class(char *wm_instance, char *wm_class)
421 char *suffix;
422 int len;
424 if (wm_class && wm_instance) {
425 len = strlen(wm_class) + strlen(wm_instance) + 2;
426 suffix = wmalloc(len);
427 snprintf(suffix, len, "%s.%s", wm_instance, wm_class);
428 } else if (wm_class) {
429 len = strlen(wm_class) + 1;
430 suffix = wmalloc(len);
431 snprintf(suffix, len, "%s", wm_class);
432 } else if (wm_instance) {
433 len = strlen(wm_instance) + 1;
434 suffix = wmalloc(len);
435 snprintf(suffix, len, "%s", wm_instance);
436 } else {
437 return NULL;
440 return suffix;
443 static char *get_icon_cache_path(void)
445 char *prefix, *path;
446 int len, ret;
448 prefix = wusergnusteppath();
449 len = strlen(prefix) + strlen(CACHE_ICON_PATH) + 2;
450 path = wmalloc(len);
451 snprintf(path, len, "%s%s/", prefix, CACHE_ICON_PATH);
453 /* If the folder exists, exit */
454 if (access(path, F_OK) == 0)
455 return path;
457 /* Create the folder */
458 ret = wmkdirhier((const char *) path);
460 /* Exit 1 on success, 0 on failure */
461 if (ret == 1)
462 return path;
464 /* Fail */
465 wfree(path);
466 return NULL;
469 static RImage *get_wwindow_image_from_wmhints(WWindow *wwin, WIcon *icon)
471 RImage *image = NULL;
472 XWMHints *hints = wwin->wm_hints;
474 if (hints && (hints->flags & IconPixmapHint) && hints->icon_pixmap != None)
475 image = RCreateImageFromDrawable(icon->core->screen_ptr->rcontext,
476 hints->icon_pixmap,
477 (hints->flags & IconMaskHint)
478 ? hints->icon_mask : None);
480 return image;
484 * wIconStore--
485 * Stores the client supplied icon at CACHE_ICON_PATH
486 * and returns the path for that icon. Returns NULL if there is no
487 * client supplied icon or on failure.
489 * Side effects:
490 * New directories might be created.
492 char *wIconStore(WIcon * icon)
494 char *path, *dir_path, *file;
495 int len = 0;
496 RImage *image = NULL;
497 WWindow *wwin = icon->owner;
499 if (!wwin)
500 return NULL;
502 dir_path = get_icon_cache_path();
503 if (!dir_path)
504 return NULL;
506 file = get_name_for_wwin(wwin);
507 if (!file) {
508 wfree(dir_path);
509 return NULL;
512 len = strlen(dir_path) + strlen(file) + 5;
513 path = wmalloc(len);
514 snprintf(path, len, "%s%s.xpm", dir_path, file);
515 wfree(dir_path);
516 wfree(file);
518 /* If icon exists, exit */
519 if (access(path, F_OK) == 0)
520 return path;
522 if (wwin->net_icon_image)
523 image = RRetainImage(wwin->net_icon_image);
524 else
525 image = get_wwindow_image_from_wmhints(wwin, icon);
527 if (!image) {
528 wfree(path);
529 return NULL;
532 if (!RSaveImage(image, path, "XPM")) {
533 wfree(path);
534 path = NULL;
537 RReleaseImage(image);
539 return path;
542 static void cycleColor(void *data)
544 WIcon *icon = (WIcon *) data;
545 WScreen *scr = icon->core->screen_ptr;
546 XGCValues gcv;
548 icon->step--;
549 gcv.dash_offset = icon->step;
550 XChangeGC(dpy, scr->icon_select_gc, GCDashOffset, &gcv);
552 XDrawRectangle(dpy, icon->core->window, scr->icon_select_gc, 0, 0,
553 icon->core->width - 1, icon->core->height - 1);
554 icon->handlerID = WMAddTimerHandler(COLOR_CYCLE_DELAY, cycleColor, icon);
557 #ifdef NEWAPPICON
558 void wIconSetHighlited(WIcon *icon, Bool flag)
560 if (icon->highlighted == flag)
561 return;
563 icon->highlighted = flag;
564 update_icon_pixmap(icon);
566 #endif
568 void wIconSelect(WIcon * icon)
570 WScreen *scr = icon->core->screen_ptr;
571 icon->selected = !icon->selected;
573 if (icon->selected) {
574 icon->step = 0;
575 if (!wPreferences.dont_blink)
576 icon->handlerID = WMAddTimerHandler(10, cycleColor, icon);
577 else
578 XDrawRectangle(dpy, icon->core->window, scr->icon_select_gc, 0, 0,
579 icon->core->width - 1, icon->core->height - 1);
580 } else {
581 if (icon->handlerID) {
582 WMDeleteTimerHandler(icon->handlerID);
583 icon->handlerID = NULL;
585 XClearArea(dpy, icon->core->window, 0, 0, icon->core->width, icon->core->height, True);
589 static void unset_icon_image(WIcon *icon)
591 if (icon->file) {
592 wfree(icon->file);
593 icon->file = NULL;
596 if (icon->file_image) {
597 RReleaseImage(icon->file_image);
598 icon->file_image = NULL;
602 void wIconUpdate(WIcon *icon)
604 WWindow *wwin = icon->owner;
606 if (wwin && WFLAGP(wwin, always_user_icon)) {
607 /* Forced use user_icon */
608 get_rimage_icon_from_user_icon(icon);
609 } else if (icon->icon_win != None) {
610 /* Get the Pixmap from the WIcon */
611 get_rimage_icon_from_icon_win(icon);
612 } else if (wwin && wwin->net_icon_image) {
613 /* Use _NET_WM_ICON icon */
614 get_rimage_icon_from_x11(icon);
615 } else if (wwin && wwin->wm_hints && (wwin->wm_hints->flags & IconPixmapHint)) {
616 /* Get the Pixmap from the wm_hints, else, from the user */
617 if (get_rimage_icon_from_wm_hints(icon))
618 get_rimage_icon_from_user_icon(icon);
619 } else {
620 /* Get the Pixmap from the user */
621 get_rimage_icon_from_user_icon(icon);
624 update_icon_pixmap(icon);
627 void update_icon_pixmap(WIcon *icon)
629 if (icon->pixmap != None)
630 XFreePixmap(dpy, icon->pixmap);
632 icon->pixmap = None;
634 /* Create the pixmap */
635 if (icon->file_image)
636 icon_update_pixmap(icon, icon->file_image);
638 /* If dockapp, put inside the icon */
639 if (icon->icon_win != None) {
640 /* file_image is NULL, because is docked app */
641 icon_update_pixmap(icon, icon->file_image);
642 set_dockapp_in_icon(icon);
645 /* No pixmap, set default background */
646 if (icon->pixmap != None)
647 XSetWindowBackgroundPixmap(dpy, icon->core->window, icon->pixmap);
649 /* Paint it */
650 XClearWindow(dpy, icon->core->window);
651 wIconPaint(icon);
654 static void get_rimage_icon_from_x11(WIcon *icon)
656 /* Remove the icon image */
657 unset_icon_image(icon);
659 /* Set the new icon image */
660 icon->file_image = RRetainImage(icon->owner->net_icon_image);
663 static void get_rimage_icon_from_user_icon(WIcon *icon)
665 if (icon->file_image)
666 return;
668 get_rimage_icon_from_default_icon(icon);
671 static void get_rimage_icon_from_default_icon(WIcon *icon)
673 WScreen *scr = icon->core->screen_ptr;
675 /* If the icon don't have image, we should use the default image. */
676 if (!scr->def_icon_rimage)
677 scr->def_icon_rimage = get_default_image(scr);
679 /* Remove the icon image */
680 unset_icon_image(icon);
682 /* Set the new icon image */
683 icon->file_image = RRetainImage(scr->def_icon_rimage);
686 /* Get the RImage from the WIcon of the WWindow */
687 static void get_rimage_icon_from_icon_win(WIcon *icon)
689 RImage *image;
691 /* Create the new RImage */
692 image = get_window_image_from_x11(icon->icon_win);
694 /* Free the icon info */
695 unset_icon_image(icon);
697 /* Set the new info */
698 icon->file_image = image;
701 /* Set the dockapp in the WIcon */
702 static void set_dockapp_in_icon(WIcon *icon)
704 XWindowAttributes attr;
705 WScreen *scr = icon->core->screen_ptr;
706 int title_height = WMFontHeight(scr->icon_title_font);
707 unsigned int w, h, d;
708 int theight = 0;
710 /* Reparent the dock application to the icon */
712 /* We need the application size to center it
713 * and show in the correct position */
714 getSize(icon->icon_win, &w, &h, &d);
716 /* Set extra space for title */
717 if (icon->show_title && (h + title_height < wPreferences.icon_size)) {
718 theight = title_height;
719 drawIconTitle(scr, icon->pixmap, theight);
720 } else {
721 XSetWindowBackgroundPixmap(dpy, icon->core->window, scr->icon_tile_pixmap);
724 /* Set the icon border */
725 XSetWindowBorderWidth(dpy, icon->icon_win, 0);
727 /* Put the dock application in the icon */
728 XReparentWindow(dpy, icon->icon_win, icon->core->window,
729 (wPreferences.icon_size - w) / 2,
730 theight + (wPreferences.icon_size - h - theight) / 2);
732 /* Show it and save */
733 XMapWindow(dpy, icon->icon_win);
734 XAddToSaveSet(dpy, icon->icon_win);
736 /* Needed to move the icon clicking on the application part */
737 if ((XGetWindowAttributes(dpy, icon->icon_win, &attr)) &&
738 (attr.all_event_masks & ButtonPressMask))
739 wHackedGrabButton(Button1, MOD_MASK, icon->core->window, True,
740 ButtonPressMask, GrabModeSync, GrabModeAsync,
741 None, wCursor[WCUR_ARROW]);
744 /* Get the RImage from the XWindow wm_hints */
745 static int get_rimage_icon_from_wm_hints(WIcon *icon)
747 RImage *image = NULL;
748 unsigned int w, h, d;
749 WWindow *wwin = icon->owner;
751 if (!getSize(wwin->wm_hints->icon_pixmap, &w, &h, &d)) {
752 icon->owner->wm_hints->flags &= ~IconPixmapHint;
753 return 1;
756 image = get_wwindow_image_from_wmhints(wwin, icon);
757 if (!image)
758 return 1;
760 /* Resize the icon to the wPreferences.icon_size size */
761 image = wIconValidateIconSize(image, wPreferences.icon_size);
763 unset_icon_image(icon);
764 icon->file_image = image;
766 return 0;
769 void wIconPaint(WIcon *icon)
771 WScreen *scr = icon->core->screen_ptr;
772 int x, l, w;
773 char *tmp;
775 XClearWindow(dpy, icon->core->window);
777 /* draw the icon title */
778 if (icon->show_title && icon->icon_name != NULL) {
779 tmp = ShrinkString(scr->icon_title_font, icon->icon_name, wPreferences.icon_size - 4);
780 w = WMWidthOfString(scr->icon_title_font, tmp, l = strlen(tmp));
782 if (w > icon->core->width - 4)
783 x = (icon->core->width - 4) - w;
784 else
785 x = (icon->core->width - w) / 2;
787 WMDrawString(scr->wmscreen, icon->core->window, scr->icon_title_color,
788 scr->icon_title_font, x, 1, tmp, l);
789 wfree(tmp);
792 if (icon->selected)
793 XDrawRectangle(dpy, icon->core->window, scr->icon_select_gc, 0, 0,
794 icon->core->width - 1, icon->core->height - 1);
797 /******************************************************************/
799 static void miniwindowExpose(WObjDescriptor * desc, XEvent * event)
801 wIconPaint(desc->parent);
804 static void miniwindowDblClick(WObjDescriptor * desc, XEvent * event)
806 WIcon *icon = desc->parent;
808 assert(icon->owner != NULL);
810 wDeiconifyWindow(icon->owner);
813 static void miniwindowMouseDown(WObjDescriptor * desc, XEvent * event)
815 WIcon *icon = desc->parent;
816 WWindow *wwin = icon->owner;
817 XEvent ev;
818 int x = wwin->icon_x, y = wwin->icon_y;
819 int dx = event->xbutton.x, dy = event->xbutton.y;
820 int grabbed = 0;
821 int clickButton = event->xbutton.button;
822 Bool hasMoved = False;
824 if (WCHECK_STATE(WSTATE_MODAL))
825 return;
827 if (IsDoubleClick(icon->core->screen_ptr, event)) {
828 miniwindowDblClick(desc, event);
829 return;
832 if (event->xbutton.button == Button1) {
833 if (event->xbutton.state & MOD_MASK)
834 wLowerFrame(icon->core);
835 else
836 wRaiseFrame(icon->core);
837 if (event->xbutton.state & ShiftMask) {
838 wIconSelect(icon);
839 wSelectWindow(icon->owner, !wwin->flags.selected);
841 } else if (event->xbutton.button == Button3) {
842 WObjDescriptor *desc;
844 OpenMiniwindowMenu(wwin, event->xbutton.x_root, event->xbutton.y_root);
846 /* allow drag select of menu */
847 desc = &wwin->screen_ptr->window_menu->menu->descriptor;
848 event->xbutton.send_event = True;
849 (*desc->handle_mousedown) (desc, event);
851 return;
854 if (XGrabPointer(dpy, icon->core->window, False, ButtonMotionMask
855 | ButtonReleaseMask | ButtonPressMask, GrabModeAsync,
856 GrabModeAsync, None, None, CurrentTime) != GrabSuccess) {
858 while (1) {
859 WMMaskEvent(dpy, PointerMotionMask | ButtonReleaseMask | ButtonPressMask
860 | ButtonMotionMask | ExposureMask, &ev);
861 switch (ev.type) {
862 case Expose:
863 WMHandleEvent(&ev);
864 break;
866 case MotionNotify:
867 hasMoved = True;
868 if (!grabbed) {
869 if (abs(dx - ev.xmotion.x) >= MOVE_THRESHOLD
870 || abs(dy - ev.xmotion.y) >= MOVE_THRESHOLD) {
871 XChangeActivePointerGrab(dpy, ButtonMotionMask
872 | ButtonReleaseMask | ButtonPressMask,
873 wCursor[WCUR_MOVE], CurrentTime);
874 grabbed = 1;
875 } else {
876 break;
879 x = ev.xmotion.x_root - dx;
880 y = ev.xmotion.y_root - dy;
881 XMoveWindow(dpy, icon->core->window, x, y);
882 break;
884 case ButtonPress:
885 break;
887 case ButtonRelease:
888 if (ev.xbutton.button != clickButton)
889 break;
891 if (wwin->icon_x != x || wwin->icon_y != y)
892 wwin->flags.icon_moved = 1;
894 XMoveWindow(dpy, icon->core->window, x, y);
896 wwin->icon_x = x;
897 wwin->icon_y = y;
898 XUngrabPointer(dpy, CurrentTime);
900 if (wPreferences.auto_arrange_icons)
901 wArrangeIcons(wwin->screen_ptr, True);
902 if (wPreferences.single_click && !hasMoved)
903 miniwindowDblClick(desc, event);
904 return;