New helper function unset_icon_image()
[wmaker-crm.git] / src / icon.c
blobfce0a518087b83dc48986d8a529ec4b56054a611
1 /* icon.c - window icon and dock and appicon parent
3 * Window Maker window manager
5 * Copyright (c) 1997-2003 Alfredo K. Kojima
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License along
18 * with this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22 #include "wconfig.h"
24 #include <X11/Xlib.h>
25 #include <X11/Xutil.h>
26 #include <stdlib.h>
27 #include <stdio.h>
28 #include <stdint.h>
29 #include <string.h>
30 #include <unistd.h>
31 #include <ctype.h>
32 #include <wraster.h>
33 #include <sys/stat.h>
35 #include "WindowMaker.h"
36 #include "wcore.h"
37 #include "texture.h"
38 #include "window.h"
39 #include "icon.h"
40 #include "actions.h"
41 #include "funcs.h"
42 #include "stacking.h"
43 #include "application.h"
44 #include "defaults.h"
45 #include "appicon.h"
46 #include "wmspec.h"
48 /**** Global varianebles ****/
49 extern WPreferences wPreferences;
51 #define MOD_MASK wPreferences.modifier_mask
52 #define CACHE_ICON_PATH "/Library/WindowMaker/CachedPixmaps"
53 #define ICON_BORDER 3
55 extern Cursor wCursor[WCUR_LAST];
57 static void miniwindowExpose(WObjDescriptor * desc, XEvent * event);
58 static void miniwindowMouseDown(WObjDescriptor * desc, XEvent * event);
59 static void miniwindowDblClick(WObjDescriptor * desc, XEvent * event);
61 static WIcon *icon_create_core(WScreen *scr, int coord_x, int coord_y);
63 static void get_pixmap_icon_from_icon_win(WIcon *icon);
64 static int get_pixmap_icon_from_wm_hints(WIcon *icon);
65 static void get_pixmap_icon_from_user_icon(WIcon *icon);
66 static void get_pixmap_icon_from_default_icon(WIcon *icon);
68 static void icon_update_pixmap(WIcon *icon, RImage *image);
69 static void unset_icon_image(WIcon *icon);
71 static RImage *get_default_image(WScreen *scr);
72 /****** Notification Observers ******/
74 static void appearanceObserver(void *self, WMNotification * notif)
76 WIcon *icon = (WIcon *) self;
77 uintptr_t flags = (uintptr_t)WMGetNotificationClientData(notif);
79 if ((flags & WTextureSettings) || (flags & WFontSettings))
80 icon->force_paint = 1;
82 wIconPaint(icon);
84 /* so that the appicon expose handlers will paint the appicon specific
85 * stuff */
86 XClearArea(dpy, icon->core->window, 0, 0, icon->core->width, icon->core->height, True);
89 static void tileObserver(void *self, WMNotification * notif)
91 WIcon *icon = (WIcon *) self;
93 icon->force_paint = 1;
94 wIconPaint(icon);
96 XClearArea(dpy, icon->core->window, 0, 0, 1, 1, True);
99 /************************************/
101 static int getSize(Drawable d, unsigned int *w, unsigned int *h, unsigned int *dep)
103 Window rjunk;
104 int xjunk, yjunk;
105 unsigned int bjunk;
107 return XGetGeometry(dpy, d, &rjunk, &xjunk, &yjunk, w, h, &bjunk, dep);
110 WIcon *icon_create_for_wwindow(WWindow *wwin)
112 WScreen *scr = wwin->screen_ptr;
113 WIcon *icon;
114 char *file;
116 icon = icon_create_core(scr, wwin->icon_x, wwin->icon_y);
118 icon->owner = wwin;
119 if (wwin->wm_hints && (wwin->wm_hints->flags & IconWindowHint)) {
120 if (wwin->client_win == wwin->main_window) {
121 WApplication *wapp;
122 /* do not let miniwindow steal app-icon's icon window */
123 wapp = wApplicationOf(wwin->client_win);
124 if (!wapp || wapp->app_icon == NULL)
125 icon->icon_win = wwin->wm_hints->icon_window;
126 } else {
127 icon->icon_win = wwin->wm_hints->icon_window;
130 #ifdef NO_MINIWINDOW_TITLES
131 icon->show_title = 0;
132 #else
133 icon->show_title = 1;
134 #endif
136 icon->icon_name = wNETWMGetIconName(wwin->client_win);
137 if (icon->icon_name)
138 wwin->flags.net_has_icon_title = 1;
139 else
140 wGetIconName(dpy, wwin->client_win, &icon->icon_name);
142 /* Get the application icon, default included */
143 file = get_default_icon_filename(scr, wwin->wm_instance, wwin->wm_class, NULL, True);
144 if (file) {
145 icon->file = wstrdup(file);
146 icon->file_image = get_rimage_from_file(scr, icon->file, wPreferences.icon_size);
147 wfree(file);
150 icon->tile_type = TILE_NORMAL;
152 wIconUpdate(icon);
154 WMAddNotificationObserver(appearanceObserver, icon, WNIconAppearanceSettingsChanged, icon);
155 WMAddNotificationObserver(tileObserver, icon, WNIconTileSettingsChanged, icon);
157 return icon;
160 WIcon *icon_create_for_dock(WScreen *scr, char *command, char *wm_instance, char *wm_class, int tile)
162 WIcon *icon;
163 char *file = NULL;
165 icon = icon_create_core(scr, 0, 0);
167 /* Search the icon using instance and class, without default icon */
168 file = get_default_icon_filename(scr, wm_instance, wm_class, command, False);
169 if (file) {
170 icon->file = wstrdup(file);
171 icon->file_image = get_rimage_from_file(scr, icon->file, wPreferences.icon_size);
172 wfree(file);
175 icon->tile_type = tile;
177 wIconUpdate(icon);
179 WMAddNotificationObserver(appearanceObserver, icon, WNIconAppearanceSettingsChanged, icon);
180 WMAddNotificationObserver(tileObserver, icon, WNIconTileSettingsChanged, icon);
182 return icon;
185 static WIcon *icon_create_core(WScreen *scr, int coord_x, int coord_y)
187 WIcon *icon;
188 unsigned long vmask = 0;
189 XSetWindowAttributes attribs;
191 icon = wmalloc(sizeof(WIcon));
192 icon->core = wCoreCreateTopLevel(scr,
193 coord_x,
194 coord_y,
195 wPreferences.icon_size,
196 wPreferences.icon_size,
197 0, scr->w_depth, scr->w_visual, scr->w_colormap);
199 if (wPreferences.use_saveunders) {
200 vmask = CWSaveUnder;
201 attribs.save_under = True;
204 /* a white border for selecting it */
205 vmask |= CWBorderPixel;
206 attribs.border_pixel = scr->white_pixel;
208 XChangeWindowAttributes(dpy, icon->core->window, vmask, &attribs);
210 /* will be overriden if this is a application icon */
211 icon->core->descriptor.handle_mousedown = miniwindowMouseDown;
212 icon->core->descriptor.handle_expose = miniwindowExpose;
213 icon->core->descriptor.parent_type = WCLASS_MINIWINDOW;
214 icon->core->descriptor.parent = icon;
216 icon->core->stacking = wmalloc(sizeof(WStacking));
217 icon->core->stacking->above = NULL;
218 icon->core->stacking->under = NULL;
219 icon->core->stacking->window_level = NORMAL_ICON_LEVEL;
220 icon->core->stacking->child_of = NULL;
222 /* Icon image */
223 icon->file = NULL;
224 icon->file_image = NULL;
226 return icon;
229 void wIconDestroy(WIcon * icon)
231 WCoreWindow *core = icon->core;
232 WScreen *scr = core->screen_ptr;
234 WMRemoveNotificationObserver(icon);
236 if (icon->handlerID)
237 WMDeleteTimerHandler(icon->handlerID);
239 if (icon->icon_win) {
240 int x = 0, y = 0;
242 if (icon->owner) {
243 x = icon->owner->icon_x;
244 y = icon->owner->icon_y;
246 XUnmapWindow(dpy, icon->icon_win);
247 XReparentWindow(dpy, icon->icon_win, scr->root_win, x, y);
249 if (icon->icon_name)
250 XFree(icon->icon_name);
252 if (icon->pixmap)
253 XFreePixmap(dpy, icon->pixmap);
255 unset_icon_image(icon);
257 wCoreDestroy(icon->core);
258 wfree(icon);
261 static void drawIconTitle(WScreen * scr, Pixmap pixmap, int height)
263 XFillRectangle(dpy, pixmap, scr->icon_title_texture->normal_gc, 0, 0, wPreferences.icon_size, height + 1);
264 XDrawLine(dpy, pixmap, scr->icon_title_texture->light_gc, 0, 0, wPreferences.icon_size, 0);
265 XDrawLine(dpy, pixmap, scr->icon_title_texture->light_gc, 0, 0, 0, height + 1);
266 XDrawLine(dpy, pixmap, scr->icon_title_texture->dim_gc,
267 wPreferences.icon_size - 1, 0, wPreferences.icon_size - 1, height + 1);
270 static void icon_update_pixmap(WIcon *icon, RImage *image)
272 RImage *tile;
273 Pixmap pixmap;
274 int x, y, sx, sy;
275 unsigned w, h;
276 int theight = 0;
277 WScreen *scr = icon->core->screen_ptr;
278 int titled = icon->show_title;
280 if (icon->tile_type == TILE_NORMAL) {
281 tile = RCloneImage(scr->icon_tile);
282 } else {
283 assert(scr->clip_tile);
284 tile = RCloneImage(scr->clip_tile);
287 if (image) {
288 w = (image->width > wPreferences.icon_size)
289 ? wPreferences.icon_size : image->width;
290 x = (wPreferences.icon_size - w) / 2;
291 sx = (image->width - w) / 2;
293 if (titled)
294 theight = WMFontHeight(scr->icon_title_font);
296 h = (image->height + theight > wPreferences.icon_size
297 ? wPreferences.icon_size - theight : image->height);
298 y = theight + (wPreferences.icon_size - theight - h) / 2;
299 sy = (image->height - h) / 2;
301 RCombineArea(tile, image, sx, sy, w, h, x, y);
304 if (icon->shadowed) {
305 RColor color;
307 color.red = scr->icon_back_texture->light.red >> 8;
308 color.green = scr->icon_back_texture->light.green >> 8;
309 color.blue = scr->icon_back_texture->light.blue >> 8;
310 color.alpha = 150; /* about 60% */
311 RClearImage(tile, &color);
314 if (icon->highlighted) {
315 RColor color;
317 color.red = color.green = color.blue = 0;
318 color.alpha = 160;
319 RLightImage(tile, &color);
322 if (!RConvertImage(scr->rcontext, tile, &pixmap))
323 wwarning(_("error rendering image:%s"), RMessageForError(RErrorCode));
325 RReleaseImage(tile);
327 if (titled)
328 drawIconTitle(scr, pixmap, theight);
330 icon->pixmap = pixmap;
333 void wIconChangeTitle(WIcon * icon, char *new_title)
335 int changed;
337 changed = (new_title == NULL && icon->icon_name != NULL)
338 || (new_title != NULL && icon->icon_name == NULL);
340 if (icon->icon_name != NULL)
341 XFree(icon->icon_name);
343 icon->icon_name = new_title;
345 if (changed)
346 icon->force_paint = 1;
347 wIconPaint(icon);
350 RImage *wIconValidateIconSize(RImage *icon, int max_size)
352 RImage *nimage;
354 if (!icon)
355 return NULL;
357 /* We should hold "ICON_BORDER" (~2) pixels to include the icon border */
358 if ((icon->width - max_size) > -ICON_BORDER ||
359 (icon->height - max_size) > -ICON_BORDER) {
360 nimage = RScaleImage(icon, max_size - ICON_BORDER,
361 (icon->height * (max_size - ICON_BORDER) / icon->width));
362 RReleaseImage(icon);
363 icon = nimage;
366 return icon;
369 Bool wIconChangeImageFile(WIcon *icon, char *file)
371 WScreen *scr = icon->core->screen_ptr;
372 char *path;
373 RImage *image = NULL;
374 int error = 0;
376 /* If no new image, don't do nothing */
377 if (!file)
378 return True;
380 /* Find the new image */
381 path = FindImage(wPreferences.icon_path, file);
382 if (path)
383 image = get_rimage_from_file(scr, path, wPreferences.icon_size);
384 else
385 error = 1;
387 /* New image! */
388 if (!error && image) {
389 /* Remove the old one */
390 unset_icon_image(icon);
392 /* Set the new image */
393 icon->file_image = image;
394 icon->file = wstrdup(path);
395 wIconUpdate(icon);
396 } else {
397 error = 1;
400 if (path)
401 wfree(path);
403 return !error;
406 static char *get_name_for_wwin(WWindow *wwin)
408 return get_name_for_instance_class(wwin->wm_instance, wwin->wm_class);
411 char *get_name_for_instance_class(char *wm_instance, char *wm_class)
413 char *suffix;
414 int len;
416 if (wm_class && wm_instance) {
417 len = strlen(wm_class) + strlen(wm_instance) + 2;
418 suffix = wmalloc(len);
419 snprintf(suffix, len, "%s.%s", wm_instance, wm_class);
420 } else if (wm_class) {
421 len = strlen(wm_class) + 1;
422 suffix = wmalloc(len);
423 snprintf(suffix, len, "%s", wm_class);
424 } else if (wm_instance) {
425 len = strlen(wm_instance) + 1;
426 suffix = wmalloc(len);
427 snprintf(suffix, len, "%s", wm_instance);
428 } else {
429 return NULL;
432 return suffix;
435 static char *get_icon_cache_path(void)
437 char *prefix, *path;
438 int len, ret;
440 prefix = wusergnusteppath();
441 len = strlen(prefix) + strlen(CACHE_ICON_PATH) + 2;
442 path = wmalloc(len);
443 snprintf(path, len, "%s%s/", prefix, CACHE_ICON_PATH);
445 /* If the folder exists, exit */
446 if (access(path, F_OK) == 0)
447 return path;
449 /* Create the folder */
450 ret = wmkdirhier((const char *) path);
452 /* Exit 1 on success, 0 on failure */
453 if (ret == 1)
454 return path;
456 /* Fail */
457 wfree(path);
458 return NULL;
461 static RImage *get_wwindow_image_from_wmhints(WWindow *wwin, WIcon *icon)
463 RImage *image = NULL;
464 XWMHints *hints = wwin->wm_hints;
466 if (hints && (hints->flags & IconPixmapHint) && hints->icon_pixmap != None)
467 image = RCreateImageFromDrawable(icon->core->screen_ptr->rcontext,
468 hints->icon_pixmap,
469 (hints->flags & IconMaskHint)
470 ? hints->icon_mask : None);
472 return image;
476 * wIconStore--
477 * Stores the client supplied icon at CACHE_ICON_PATH
478 * and returns the path for that icon. Returns NULL if there is no
479 * client supplied icon or on failure.
481 * Side effects:
482 * New directories might be created.
484 char *wIconStore(WIcon * icon)
486 char *path, *dir_path, *file;
487 int len = 0;
488 RImage *image = NULL;
489 WWindow *wwin = icon->owner;
491 if (!wwin)
492 return NULL;
494 dir_path = get_icon_cache_path();
495 if (!dir_path)
496 return NULL;
498 file = get_name_for_wwin(wwin);
499 if (!file) {
500 wfree(dir_path);
501 return NULL;
504 len = strlen(dir_path) + strlen(file) + 5;
505 path = wmalloc(len);
506 snprintf(path, len, "%s%s.xpm", dir_path, file);
507 wfree(dir_path);
508 wfree(file);
510 /* If icon exists, exit */
511 if (access(path, F_OK) == 0)
512 return path;
514 if (wwin->net_icon_image)
515 image = RRetainImage(wwin->net_icon_image);
516 else
517 image = get_wwindow_image_from_wmhints(wwin, icon);
519 if (!image) {
520 wfree(path);
521 return NULL;
524 if (!RSaveImage(image, path, "XPM")) {
525 wfree(path);
526 path = NULL;
529 RReleaseImage(image);
531 return path;
534 static void cycleColor(void *data)
536 WIcon *icon = (WIcon *) data;
537 WScreen *scr = icon->core->screen_ptr;
538 XGCValues gcv;
540 icon->step--;
541 gcv.dash_offset = icon->step;
542 XChangeGC(dpy, scr->icon_select_gc, GCDashOffset, &gcv);
544 XDrawRectangle(dpy, icon->core->window, scr->icon_select_gc, 0, 0,
545 icon->core->width - 1, icon->core->height - 1);
546 icon->handlerID = WMAddTimerHandler(COLOR_CYCLE_DELAY, cycleColor, icon);
549 #ifdef NEWAPPICON
550 void wIconSetHighlited(WIcon *icon, Bool flag)
552 if (icon->highlighted == flag)
553 return;
555 icon->highlighted = flag;
556 icon->force_paint = True;
557 wIconPaint(icon);
559 #endif
561 void wIconSelect(WIcon * icon)
563 WScreen *scr = icon->core->screen_ptr;
564 icon->selected = !icon->selected;
566 if (icon->selected) {
567 icon->step = 0;
568 if (!wPreferences.dont_blink)
569 icon->handlerID = WMAddTimerHandler(10, cycleColor, icon);
570 else
571 XDrawRectangle(dpy, icon->core->window, scr->icon_select_gc, 0, 0,
572 icon->core->width - 1, icon->core->height - 1);
573 } else {
574 if (icon->handlerID) {
575 WMDeleteTimerHandler(icon->handlerID);
576 icon->handlerID = NULL;
578 XClearArea(dpy, icon->core->window, 0, 0, icon->core->width, icon->core->height, True);
582 static void unset_icon_image(WIcon *icon)
584 if (icon->file)
585 wfree(icon->file);
587 if (icon->file_image)
588 RReleaseImage(icon->file_image);
591 void wIconUpdate(WIcon *icon)
593 WScreen *scr = icon->core->screen_ptr;
594 WWindow *wwin = icon->owner;
596 assert(scr->icon_tile != NULL);
598 if (icon->pixmap != None)
599 XFreePixmap(dpy, icon->pixmap);
601 icon->pixmap = None;
603 if (wwin && WFLAGP(wwin, always_user_icon)) {
604 /* Forced use user_icon */
605 get_pixmap_icon_from_user_icon(icon);
606 } else if (icon->icon_win != None) {
607 /* Get the Pixmap from the WIcon */
608 get_pixmap_icon_from_icon_win(icon);
609 } else if (wwin && wwin->net_icon_image) {
610 /* Use _NET_WM_ICON icon */
611 icon_update_pixmap(icon, wwin->net_icon_image);
612 } else if (wwin && wwin->wm_hints && (wwin->wm_hints->flags & IconPixmapHint)) {
613 /* Get the Pixmap from the wm_hints, else, from the user */
614 if (get_pixmap_icon_from_wm_hints(icon))
615 get_pixmap_icon_from_user_icon(icon);
616 } else {
617 /* Get the Pixmap from the user */
618 get_pixmap_icon_from_user_icon(icon);
621 /* No pixmap, set default background */
622 if (icon->pixmap != None)
623 XSetWindowBackgroundPixmap(dpy, icon->core->window, icon->pixmap);
625 /* Paint it */
626 XClearWindow(dpy, icon->core->window);
627 wIconPaint(icon);
630 static void get_pixmap_icon_from_user_icon(WIcon *icon)
632 /* If the icon has image, update it and continue */
633 if (icon->file_image) {
634 icon_update_pixmap(icon, icon->file_image);
635 return;
638 get_pixmap_icon_from_default_icon(icon);
641 static void get_pixmap_icon_from_default_icon(WIcon *icon)
643 WScreen *scr = icon->core->screen_ptr;
645 /* If the icon don't have image, we should use the default image. */
646 if (!scr->def_icon_rimage)
647 scr->def_icon_rimage = get_default_image(scr);
649 /* Now, create the pixmap using the default (saved) image */
650 icon_update_pixmap(icon, scr->def_icon_rimage);
653 /* This function creates the RImage using the default icon */
654 static RImage *get_default_image(WScreen *scr)
656 RImage *image = NULL;
657 char *path, *file;
659 /* Get the default icon */
660 file = wDefaultGetIconFile(NULL, NULL, True);
661 if (file) {
662 path = FindImage(wPreferences.icon_path, file);
663 image = get_rimage_from_file(scr, path, wPreferences.icon_size);
665 if (!image)
666 wwarning(_("could not find default icon \"%s\""), file);
668 wfree(file);
671 return image;
674 /* Get the Pixmap from the WIcon of the WWindow */
675 static void get_pixmap_icon_from_icon_win(WIcon * icon)
677 XWindowAttributes attr;
678 WScreen *scr = icon->core->screen_ptr;
679 int title_height = WMFontHeight(scr->icon_title_font);
680 unsigned int width, height, depth;
681 int theight;
682 int resize = 0;
683 Pixmap pixmap;
685 getSize(icon->icon_win, &width, &height, &depth);
687 if (width > wPreferences.icon_size) {
688 resize = 1;
689 width = wPreferences.icon_size;
692 if (height > wPreferences.icon_size) {
693 resize = 1;
694 height = wPreferences.icon_size;
697 if (icon->show_title && (height + title_height < wPreferences.icon_size)) {
698 pixmap = XCreatePixmap(dpy, scr->w_win, wPreferences.icon_size,
699 wPreferences.icon_size, scr->w_depth);
700 XSetClipMask(dpy, scr->copy_gc, None);
701 XCopyArea(dpy, scr->icon_tile_pixmap, pixmap, scr->copy_gc, 0, 0,
702 wPreferences.icon_size, wPreferences.icon_size, 0, 0);
703 drawIconTitle(scr, pixmap, title_height);
704 theight = title_height;
705 } else {
706 pixmap = None;
707 theight = 0;
708 XSetWindowBackgroundPixmap(dpy, icon->core->window, scr->icon_tile_pixmap);
711 XSetWindowBorderWidth(dpy, icon->icon_win, 0);
712 XReparentWindow(dpy, icon->icon_win, icon->core->window,
713 (wPreferences.icon_size - width) / 2,
714 theight + (wPreferences.icon_size - height - theight) / 2);
715 if (resize)
716 XResizeWindow(dpy, icon->icon_win, width, height);
718 XMapWindow(dpy, icon->icon_win);
719 XAddToSaveSet(dpy, icon->icon_win);
721 /* Save it */
722 icon->pixmap = pixmap;
724 if ((XGetWindowAttributes(dpy, icon->icon_win, &attr)) &&
725 (attr.all_event_masks & ButtonPressMask))
726 wHackedGrabButton(Button1, MOD_MASK, icon->core->window, True,
727 ButtonPressMask, GrabModeSync, GrabModeAsync,
728 None, wCursor[WCUR_ARROW]);
731 /* Get the Pixmap from the XWindow wm_hints */
732 static int get_pixmap_icon_from_wm_hints(WIcon *icon)
734 RImage *image = NULL;
735 unsigned int w, h, d;
736 WWindow *wwin = icon->owner;
738 if (!getSize(wwin->wm_hints->icon_pixmap, &w, &h, &d)) {
739 icon->owner->wm_hints->flags &= ~IconPixmapHint;
740 return 1;
743 image = get_wwindow_image_from_wmhints(wwin, icon);
744 if (!image)
745 return 1;
747 icon_update_pixmap(icon, image);
749 return 0;
752 void wIconPaint(WIcon * icon)
754 WScreen *scr = icon->core->screen_ptr;
755 int x;
756 char *tmp;
758 if (icon->force_paint) {
759 icon->force_paint = 0;
760 wIconUpdate(icon);
761 return;
764 XClearWindow(dpy, icon->core->window);
766 /* draw the icon title */
767 if (icon->show_title && icon->icon_name != NULL) {
768 int l;
769 int w;
771 tmp = ShrinkString(scr->icon_title_font, icon->icon_name, wPreferences.icon_size - 4);
772 w = WMWidthOfString(scr->icon_title_font, tmp, l = strlen(tmp));
774 if (w > icon->core->width - 4)
775 x = (icon->core->width - 4) - w;
776 else
777 x = (icon->core->width - w) / 2;
779 WMDrawString(scr->wmscreen, icon->core->window, scr->icon_title_color,
780 scr->icon_title_font, x, 1, tmp, l);
781 wfree(tmp);
784 if (icon->selected)
785 XDrawRectangle(dpy, icon->core->window, scr->icon_select_gc, 0, 0,
786 icon->core->width - 1, icon->core->height - 1);
789 /******************************************************************/
791 static void miniwindowExpose(WObjDescriptor * desc, XEvent * event)
793 wIconPaint(desc->parent);
796 static void miniwindowDblClick(WObjDescriptor * desc, XEvent * event)
798 WIcon *icon = desc->parent;
800 assert(icon->owner != NULL);
802 wDeiconifyWindow(icon->owner);
805 static void miniwindowMouseDown(WObjDescriptor * desc, XEvent * event)
807 WIcon *icon = desc->parent;
808 WWindow *wwin = icon->owner;
809 XEvent ev;
810 int x = wwin->icon_x, y = wwin->icon_y;
811 int dx = event->xbutton.x, dy = event->xbutton.y;
812 int grabbed = 0;
813 int clickButton = event->xbutton.button;
814 Bool hasMoved = False;
816 if (WCHECK_STATE(WSTATE_MODAL))
817 return;
819 if (IsDoubleClick(icon->core->screen_ptr, event)) {
820 miniwindowDblClick(desc, event);
821 return;
824 if (event->xbutton.button == Button1) {
825 if (event->xbutton.state & MOD_MASK)
826 wLowerFrame(icon->core);
827 else
828 wRaiseFrame(icon->core);
829 if (event->xbutton.state & ShiftMask) {
830 wIconSelect(icon);
831 wSelectWindow(icon->owner, !wwin->flags.selected);
833 } else if (event->xbutton.button == Button3) {
834 WObjDescriptor *desc;
836 OpenMiniwindowMenu(wwin, event->xbutton.x_root, event->xbutton.y_root);
838 /* allow drag select of menu */
839 desc = &wwin->screen_ptr->window_menu->menu->descriptor;
840 event->xbutton.send_event = True;
841 (*desc->handle_mousedown) (desc, event);
843 return;
846 if (XGrabPointer(dpy, icon->core->window, False, ButtonMotionMask
847 | ButtonReleaseMask | ButtonPressMask, GrabModeAsync,
848 GrabModeAsync, None, None, CurrentTime) != GrabSuccess) {
850 while (1) {
851 WMMaskEvent(dpy, PointerMotionMask | ButtonReleaseMask | ButtonPressMask
852 | ButtonMotionMask | ExposureMask, &ev);
853 switch (ev.type) {
854 case Expose:
855 WMHandleEvent(&ev);
856 break;
858 case MotionNotify:
859 hasMoved = True;
860 if (!grabbed) {
861 if (abs(dx - ev.xmotion.x) >= MOVE_THRESHOLD
862 || abs(dy - ev.xmotion.y) >= MOVE_THRESHOLD) {
863 XChangeActivePointerGrab(dpy, ButtonMotionMask
864 | ButtonReleaseMask | ButtonPressMask,
865 wCursor[WCUR_MOVE], CurrentTime);
866 grabbed = 1;
867 } else {
868 break;
871 x = ev.xmotion.x_root - dx;
872 y = ev.xmotion.y_root - dy;
873 XMoveWindow(dpy, icon->core->window, x, y);
874 break;
876 case ButtonPress:
877 break;
879 case ButtonRelease:
880 if (ev.xbutton.button != clickButton)
881 break;
883 if (wwin->icon_x != x || wwin->icon_y != y)
884 wwin->flags.icon_moved = 1;
886 XMoveWindow(dpy, icon->core->window, x, y);
888 wwin->icon_x = x;
889 wwin->icon_y = y;
890 XUngrabPointer(dpy, CurrentTime);
892 if (wPreferences.auto_arrange_icons)
893 wArrangeIcons(wwin->screen_ptr, True);
894 if (wPreferences.single_click && !hasMoved)
895 miniwindowDblClick(desc, event);
896 return;