wIconUpdate create rimage and then pixmap
[wmaker-crm.git] / src / icon.c
blob38814bff45038173e289bb1bb71807be7b3d1674
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 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 (wwin && WFLAGP(wwin, always_user_icon)) {
600 /* Forced use user_icon */
601 get_rimage_icon_from_user_icon(icon);
602 } else if (icon->icon_win != None) {
603 /* Get the Pixmap from the WIcon */
604 get_rimage_icon_from_icon_win(icon);
605 } else if (wwin && wwin->net_icon_image) {
606 /* Use _NET_WM_ICON icon */
607 get_rimage_icon_from_x11(icon);
608 } else if (wwin && wwin->wm_hints && (wwin->wm_hints->flags & IconPixmapHint)) {
609 /* Get the Pixmap from the wm_hints, else, from the user */
610 if (get_rimage_icon_from_wm_hints(icon))
611 get_rimage_icon_from_user_icon(icon);
612 } else {
613 /* Get the Pixmap from the user */
614 get_rimage_icon_from_user_icon(icon);
617 if (icon->pixmap != None)
618 XFreePixmap(dpy, icon->pixmap);
620 icon->pixmap = None;
622 /* Create the pixmap */
623 if (icon->file_image)
624 icon_update_pixmap(icon, icon->file_image);
626 /* If dockapp, put inside the icon */
627 if (icon->icon_win != None) {
628 /* file_image is NULL, because is docked app */
629 icon_update_pixmap(icon, icon->file_image);
630 set_dockapp_in_icon(icon);
633 /* No pixmap, set default background */
634 if (icon->pixmap != None)
635 XSetWindowBackgroundPixmap(dpy, icon->core->window, icon->pixmap);
637 /* Paint it */
638 XClearWindow(dpy, icon->core->window);
639 wIconPaint(icon);
642 static void get_rimage_icon_from_x11(WIcon *icon)
644 /* Remove the icon image */
645 unset_icon_image(icon);
647 /* Set the new icon image */
648 icon->file = NULL;
649 icon->file_image = RRetainImage(icon->owner->net_icon_image);
652 static void get_rimage_icon_from_user_icon(WIcon *icon)
654 if (icon->file_image)
655 return;
657 get_rimage_icon_from_default_icon(icon);
660 static void get_rimage_icon_from_default_icon(WIcon *icon)
662 WScreen *scr = icon->core->screen_ptr;
664 /* If the icon don't have image, we should use the default image. */
665 if (!scr->def_icon_rimage)
666 scr->def_icon_rimage = get_default_image(scr);
668 /* Remove the icon image */
669 unset_icon_image(icon);
671 /* Set the new icon image */
672 icon->file = NULL;
673 icon->file_image = RRetainImage(scr->def_icon_rimage);
676 /* Get the RImage from the WIcon of the WWindow */
677 static void get_rimage_icon_from_icon_win(WIcon *icon)
679 RImage *image;
681 /* Create the new RImage */
682 image = get_window_image_from_x11(icon->icon_win);
684 /* Free the icon info */
685 unset_icon_image(icon);
687 /* Set the new info */
688 icon->file = NULL;
689 icon->file_image = image;
692 /* Set the dockapp in the WIcon */
693 static void set_dockapp_in_icon(WIcon *icon)
695 XWindowAttributes attr;
696 WScreen *scr = icon->core->screen_ptr;
697 int title_height = WMFontHeight(scr->icon_title_font);
698 unsigned int w, h, d;
699 int theight = 0;
701 /* Reparent the dock application to the icon */
703 /* We need the application size to center it
704 * and show in the correct position */
705 getSize(icon->icon_win, &w, &h, &d);
707 /* Set extra space for title */
708 if (icon->show_title && (h + title_height < wPreferences.icon_size)) {
709 theight = title_height;
710 drawIconTitle(scr, icon->pixmap, theight);
711 } else {
712 XSetWindowBackgroundPixmap(dpy, icon->core->window, scr->icon_tile_pixmap);
715 /* Set the icon border */
716 XSetWindowBorderWidth(dpy, icon->icon_win, 0);
718 /* Put the dock application in the icon */
719 XReparentWindow(dpy, icon->icon_win, icon->core->window,
720 (wPreferences.icon_size - w) / 2,
721 theight + (wPreferences.icon_size - h - theight) / 2);
723 /* Show it and save */
724 XMapWindow(dpy, icon->icon_win);
725 XAddToSaveSet(dpy, icon->icon_win);
727 /* Needed to move the icon clicking on the application part */
728 if ((XGetWindowAttributes(dpy, icon->icon_win, &attr)) &&
729 (attr.all_event_masks & ButtonPressMask))
730 wHackedGrabButton(Button1, MOD_MASK, icon->core->window, True,
731 ButtonPressMask, GrabModeSync, GrabModeAsync,
732 None, wCursor[WCUR_ARROW]);
735 /* Get the RImage from the XWindow wm_hints */
736 static int get_rimage_icon_from_wm_hints(WIcon *icon)
738 RImage *image = NULL;
739 unsigned int w, h, d;
740 WWindow *wwin = icon->owner;
742 if (!getSize(wwin->wm_hints->icon_pixmap, &w, &h, &d)) {
743 icon->owner->wm_hints->flags &= ~IconPixmapHint;
744 return 1;
747 image = get_wwindow_image_from_wmhints(wwin, icon);
748 if (!image)
749 return 1;
751 /* FIXME: If unset_icon_image, pointer double free then crash
752 unset_icon_image(icon); */
753 icon->file_image = image;
755 return 0;
758 void wIconPaint(WIcon * icon)
760 WScreen *scr = icon->core->screen_ptr;
761 int x;
762 char *tmp;
764 if (icon->force_paint) {
765 icon->force_paint = 0;
766 wIconUpdate(icon);
767 return;
770 XClearWindow(dpy, icon->core->window);
772 /* draw the icon title */
773 if (icon->show_title && icon->icon_name != NULL) {
774 int l;
775 int w;
777 tmp = ShrinkString(scr->icon_title_font, icon->icon_name, wPreferences.icon_size - 4);
778 w = WMWidthOfString(scr->icon_title_font, tmp, l = strlen(tmp));
780 if (w > icon->core->width - 4)
781 x = (icon->core->width - 4) - w;
782 else
783 x = (icon->core->width - w) / 2;
785 WMDrawString(scr->wmscreen, icon->core->window, scr->icon_title_color,
786 scr->icon_title_font, x, 1, tmp, l);
787 wfree(tmp);
790 if (icon->selected)
791 XDrawRectangle(dpy, icon->core->window, scr->icon_select_gc, 0, 0,
792 icon->core->width - 1, icon->core->height - 1);
795 /******************************************************************/
797 static void miniwindowExpose(WObjDescriptor * desc, XEvent * event)
799 wIconPaint(desc->parent);
802 static void miniwindowDblClick(WObjDescriptor * desc, XEvent * event)
804 WIcon *icon = desc->parent;
806 assert(icon->owner != NULL);
808 wDeiconifyWindow(icon->owner);
811 static void miniwindowMouseDown(WObjDescriptor * desc, XEvent * event)
813 WIcon *icon = desc->parent;
814 WWindow *wwin = icon->owner;
815 XEvent ev;
816 int x = wwin->icon_x, y = wwin->icon_y;
817 int dx = event->xbutton.x, dy = event->xbutton.y;
818 int grabbed = 0;
819 int clickButton = event->xbutton.button;
820 Bool hasMoved = False;
822 if (WCHECK_STATE(WSTATE_MODAL))
823 return;
825 if (IsDoubleClick(icon->core->screen_ptr, event)) {
826 miniwindowDblClick(desc, event);
827 return;
830 if (event->xbutton.button == Button1) {
831 if (event->xbutton.state & MOD_MASK)
832 wLowerFrame(icon->core);
833 else
834 wRaiseFrame(icon->core);
835 if (event->xbutton.state & ShiftMask) {
836 wIconSelect(icon);
837 wSelectWindow(icon->owner, !wwin->flags.selected);
839 } else if (event->xbutton.button == Button3) {
840 WObjDescriptor *desc;
842 OpenMiniwindowMenu(wwin, event->xbutton.x_root, event->xbutton.y_root);
844 /* allow drag select of menu */
845 desc = &wwin->screen_ptr->window_menu->menu->descriptor;
846 event->xbutton.send_event = True;
847 (*desc->handle_mousedown) (desc, event);
849 return;
852 if (XGrabPointer(dpy, icon->core->window, False, ButtonMotionMask
853 | ButtonReleaseMask | ButtonPressMask, GrabModeAsync,
854 GrabModeAsync, None, None, CurrentTime) != GrabSuccess) {
856 while (1) {
857 WMMaskEvent(dpy, PointerMotionMask | ButtonReleaseMask | ButtonPressMask
858 | ButtonMotionMask | ExposureMask, &ev);
859 switch (ev.type) {
860 case Expose:
861 WMHandleEvent(&ev);
862 break;
864 case MotionNotify:
865 hasMoved = True;
866 if (!grabbed) {
867 if (abs(dx - ev.xmotion.x) >= MOVE_THRESHOLD
868 || abs(dy - ev.xmotion.y) >= MOVE_THRESHOLD) {
869 XChangeActivePointerGrab(dpy, ButtonMotionMask
870 | ButtonReleaseMask | ButtonPressMask,
871 wCursor[WCUR_MOVE], CurrentTime);
872 grabbed = 1;
873 } else {
874 break;
877 x = ev.xmotion.x_root - dx;
878 y = ev.xmotion.y_root - dy;
879 XMoveWindow(dpy, icon->core->window, x, y);
880 break;
882 case ButtonPress:
883 break;
885 case ButtonRelease:
886 if (ev.xbutton.button != clickButton)
887 break;
889 if (wwin->icon_x != x || wwin->icon_y != y)
890 wwin->flags.icon_moved = 1;
892 XMoveWindow(dpy, icon->core->window, x, y);
894 wwin->icon_x = x;
895 wwin->icon_y = y;
896 XUngrabPointer(dpy, CurrentTime);
898 if (wPreferences.auto_arrange_icons)
899 wArrangeIcons(wwin->screen_ptr, True);
900 if (wPreferences.single_click && !hasMoved)
901 miniwindowDblClick(desc, event);
902 return;