c9875658747cda64738d387306c7c2e6d13ecdab
[wmaker-crm.git] / src / icon.c
blobc9875658747cda64738d387306c7c2e6d13ecdab
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 icon->force_paint = 1;
98 wIconPaint(icon);
100 XClearArea(dpy, icon->core->window, 0, 0, 1, 1, True);
103 /************************************/
105 static int getSize(Drawable d, unsigned int *w, unsigned int *h, unsigned int *dep)
107 Window rjunk;
108 int xjunk, yjunk;
109 unsigned int bjunk;
111 return XGetGeometry(dpy, d, &rjunk, &xjunk, &yjunk, w, h, &bjunk, dep);
114 WIcon *icon_create_for_wwindow(WWindow *wwin)
116 WScreen *scr = wwin->screen_ptr;
117 WIcon *icon;
118 char *file;
120 icon = icon_create_core(scr, wwin->icon_x, wwin->icon_y);
122 icon->owner = wwin;
123 if (wwin->wm_hints && (wwin->wm_hints->flags & IconWindowHint)) {
124 if (wwin->client_win == wwin->main_window) {
125 WApplication *wapp;
126 /* do not let miniwindow steal app-icon's icon window */
127 wapp = wApplicationOf(wwin->client_win);
128 if (!wapp || wapp->app_icon == NULL)
129 icon->icon_win = wwin->wm_hints->icon_window;
130 } else {
131 icon->icon_win = wwin->wm_hints->icon_window;
134 #ifdef NO_MINIWINDOW_TITLES
135 icon->show_title = 0;
136 #else
137 icon->show_title = 1;
138 #endif
140 icon->icon_name = wNETWMGetIconName(wwin->client_win);
141 if (icon->icon_name)
142 wwin->flags.net_has_icon_title = 1;
143 else
144 wGetIconName(dpy, wwin->client_win, &icon->icon_name);
146 /* Get the application icon, default included */
147 file = get_icon_filename(scr, wwin->wm_instance, wwin->wm_class, NULL, True);
148 if (file) {
149 icon->file = wstrdup(file);
150 icon->file_image = get_rimage_from_file(scr, icon->file, wPreferences.icon_size);
151 wfree(file);
154 icon->tile_type = TILE_NORMAL;
156 wIconUpdate(icon);
158 WMAddNotificationObserver(appearanceObserver, icon, WNIconAppearanceSettingsChanged, icon);
159 WMAddNotificationObserver(tileObserver, icon, WNIconTileSettingsChanged, icon);
161 return icon;
164 WIcon *icon_create_for_dock(WScreen *scr, char *command, char *wm_instance, char *wm_class, int tile)
166 WIcon *icon;
167 char *file = NULL;
169 icon = icon_create_core(scr, 0, 0);
171 /* Search the icon using instance and class, without default icon */
172 file = get_icon_filename(scr, wm_instance, wm_class, command, False);
173 if (file) {
174 icon->file = wstrdup(file);
175 icon->file_image = get_rimage_from_file(scr, icon->file, wPreferences.icon_size);
176 wfree(file);
179 icon->tile_type = tile;
181 wIconUpdate(icon);
183 WMAddNotificationObserver(appearanceObserver, icon, WNIconAppearanceSettingsChanged, icon);
184 WMAddNotificationObserver(tileObserver, icon, WNIconTileSettingsChanged, icon);
186 return icon;
189 static WIcon *icon_create_core(WScreen *scr, int coord_x, int coord_y)
191 WIcon *icon;
192 unsigned long vmask = 0;
193 XSetWindowAttributes attribs;
195 icon = wmalloc(sizeof(WIcon));
196 icon->core = wCoreCreateTopLevel(scr,
197 coord_x,
198 coord_y,
199 wPreferences.icon_size,
200 wPreferences.icon_size,
201 0, scr->w_depth, scr->w_visual, scr->w_colormap);
203 if (wPreferences.use_saveunders) {
204 vmask = CWSaveUnder;
205 attribs.save_under = True;
208 /* a white border for selecting it */
209 vmask |= CWBorderPixel;
210 attribs.border_pixel = scr->white_pixel;
212 XChangeWindowAttributes(dpy, icon->core->window, vmask, &attribs);
214 /* will be overriden if this is a application icon */
215 icon->core->descriptor.handle_mousedown = miniwindowMouseDown;
216 icon->core->descriptor.handle_expose = miniwindowExpose;
217 icon->core->descriptor.parent_type = WCLASS_MINIWINDOW;
218 icon->core->descriptor.parent = icon;
220 icon->core->stacking = wmalloc(sizeof(WStacking));
221 icon->core->stacking->above = NULL;
222 icon->core->stacking->under = NULL;
223 icon->core->stacking->window_level = NORMAL_ICON_LEVEL;
224 icon->core->stacking->child_of = NULL;
226 /* Icon image */
227 icon->file = NULL;
228 icon->file_image = NULL;
230 return icon;
233 void wIconDestroy(WIcon * icon)
235 WCoreWindow *core = icon->core;
236 WScreen *scr = core->screen_ptr;
238 WMRemoveNotificationObserver(icon);
240 if (icon->handlerID)
241 WMDeleteTimerHandler(icon->handlerID);
243 if (icon->icon_win) {
244 int x = 0, y = 0;
246 if (icon->owner) {
247 x = icon->owner->icon_x;
248 y = icon->owner->icon_y;
250 XUnmapWindow(dpy, icon->icon_win);
251 XReparentWindow(dpy, icon->icon_win, scr->root_win, x, y);
253 if (icon->icon_name)
254 XFree(icon->icon_name);
256 if (icon->pixmap)
257 XFreePixmap(dpy, icon->pixmap);
259 unset_icon_image(icon);
261 wCoreDestroy(icon->core);
262 wfree(icon);
265 static void drawIconTitle(WScreen * scr, Pixmap pixmap, int height)
267 XFillRectangle(dpy, pixmap, scr->icon_title_texture->normal_gc, 0, 0, wPreferences.icon_size, height + 1);
268 XDrawLine(dpy, pixmap, scr->icon_title_texture->light_gc, 0, 0, wPreferences.icon_size, 0);
269 XDrawLine(dpy, pixmap, scr->icon_title_texture->light_gc, 0, 0, 0, height + 1);
270 XDrawLine(dpy, pixmap, scr->icon_title_texture->dim_gc,
271 wPreferences.icon_size - 1, 0, wPreferences.icon_size - 1, height + 1);
274 static void icon_update_pixmap(WIcon *icon, RImage *image)
276 RImage *tile;
277 Pixmap pixmap;
278 int x, y, sx, sy;
279 unsigned w, h;
280 int theight = 0;
281 WScreen *scr = icon->core->screen_ptr;
282 int titled = icon->show_title;
284 if (icon->tile_type == TILE_NORMAL) {
285 tile = RCloneImage(scr->icon_tile);
286 } else {
287 assert(scr->clip_tile);
288 tile = RCloneImage(scr->clip_tile);
291 if (image) {
292 w = (image->width > wPreferences.icon_size)
293 ? wPreferences.icon_size : image->width;
294 x = (wPreferences.icon_size - w) / 2;
295 sx = (image->width - w) / 2;
297 if (titled)
298 theight = WMFontHeight(scr->icon_title_font);
300 h = (image->height + theight > wPreferences.icon_size
301 ? wPreferences.icon_size - theight : image->height);
302 y = theight + (wPreferences.icon_size - theight - h) / 2;
303 sy = (image->height - h) / 2;
305 RCombineArea(tile, image, sx, sy, w, h, x, y);
308 if (icon->shadowed) {
309 RColor color;
311 color.red = scr->icon_back_texture->light.red >> 8;
312 color.green = scr->icon_back_texture->light.green >> 8;
313 color.blue = scr->icon_back_texture->light.blue >> 8;
314 color.alpha = 150; /* about 60% */
315 RClearImage(tile, &color);
318 if (icon->highlighted) {
319 RColor color;
321 color.red = color.green = color.blue = 0;
322 color.alpha = 160;
323 RLightImage(tile, &color);
326 if (!RConvertImage(scr->rcontext, tile, &pixmap))
327 wwarning(_("error rendering image:%s"), RMessageForError(RErrorCode));
329 RReleaseImage(tile);
331 if (titled)
332 drawIconTitle(scr, pixmap, theight);
334 icon->pixmap = pixmap;
337 void wIconChangeTitle(WIcon * icon, char *new_title)
339 int changed;
341 changed = (new_title == NULL && icon->icon_name != NULL)
342 || (new_title != NULL && icon->icon_name == NULL);
344 if (icon->icon_name != NULL)
345 XFree(icon->icon_name);
347 icon->icon_name = new_title;
349 if (changed)
350 icon->force_paint = 1;
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 ((icon->width - max_size) > -ICON_BORDER ||
363 (icon->height - max_size) > -ICON_BORDER) {
364 nimage = RScaleImage(icon, max_size - ICON_BORDER,
365 (icon->height * (max_size - ICON_BORDER) / icon->width));
366 RReleaseImage(icon);
367 icon = nimage;
370 return icon;
373 Bool wIconChangeImageFile(WIcon *icon, char *file)
375 WScreen *scr = icon->core->screen_ptr;
376 char *path;
377 RImage *image = NULL;
378 int error = 0;
380 /* If no new image, don't do nothing */
381 if (!file)
382 return True;
384 /* Find the new image */
385 path = FindImage(wPreferences.icon_path, file);
386 if (path)
387 image = get_rimage_from_file(scr, path, wPreferences.icon_size);
388 else
389 error = 1;
391 /* New image! */
392 if (!error && image) {
393 /* Remove the old one */
394 unset_icon_image(icon);
396 /* Set the new image */
397 icon->file_image = image;
398 icon->file = wstrdup(path);
399 wIconUpdate(icon);
400 } else {
401 error = 1;
404 if (path)
405 wfree(path);
407 return !error;
410 static char *get_name_for_wwin(WWindow *wwin)
412 return get_name_for_instance_class(wwin->wm_instance, wwin->wm_class);
415 char *get_name_for_instance_class(char *wm_instance, char *wm_class)
417 char *suffix;
418 int len;
420 if (wm_class && wm_instance) {
421 len = strlen(wm_class) + strlen(wm_instance) + 2;
422 suffix = wmalloc(len);
423 snprintf(suffix, len, "%s.%s", wm_instance, wm_class);
424 } else if (wm_class) {
425 len = strlen(wm_class) + 1;
426 suffix = wmalloc(len);
427 snprintf(suffix, len, "%s", wm_class);
428 } else if (wm_instance) {
429 len = strlen(wm_instance) + 1;
430 suffix = wmalloc(len);
431 snprintf(suffix, len, "%s", wm_instance);
432 } else {
433 return NULL;
436 return suffix;
439 static char *get_icon_cache_path(void)
441 char *prefix, *path;
442 int len, ret;
444 prefix = wusergnusteppath();
445 len = strlen(prefix) + strlen(CACHE_ICON_PATH) + 2;
446 path = wmalloc(len);
447 snprintf(path, len, "%s%s/", prefix, CACHE_ICON_PATH);
449 /* If the folder exists, exit */
450 if (access(path, F_OK) == 0)
451 return path;
453 /* Create the folder */
454 ret = wmkdirhier((const char *) path);
456 /* Exit 1 on success, 0 on failure */
457 if (ret == 1)
458 return path;
460 /* Fail */
461 wfree(path);
462 return NULL;
465 static RImage *get_wwindow_image_from_wmhints(WWindow *wwin, WIcon *icon)
467 RImage *image = NULL;
468 XWMHints *hints = wwin->wm_hints;
470 if (hints && (hints->flags & IconPixmapHint) && hints->icon_pixmap != None)
471 image = RCreateImageFromDrawable(icon->core->screen_ptr->rcontext,
472 hints->icon_pixmap,
473 (hints->flags & IconMaskHint)
474 ? hints->icon_mask : None);
476 return image;
480 * wIconStore--
481 * Stores the client supplied icon at CACHE_ICON_PATH
482 * and returns the path for that icon. Returns NULL if there is no
483 * client supplied icon or on failure.
485 * Side effects:
486 * New directories might be created.
488 char *wIconStore(WIcon * icon)
490 char *path, *dir_path, *file;
491 int len = 0;
492 RImage *image = NULL;
493 WWindow *wwin = icon->owner;
495 if (!wwin)
496 return NULL;
498 dir_path = get_icon_cache_path();
499 if (!dir_path)
500 return NULL;
502 file = get_name_for_wwin(wwin);
503 if (!file) {
504 wfree(dir_path);
505 return NULL;
508 len = strlen(dir_path) + strlen(file) + 5;
509 path = wmalloc(len);
510 snprintf(path, len, "%s%s.xpm", dir_path, file);
511 wfree(dir_path);
512 wfree(file);
514 /* If icon exists, exit */
515 if (access(path, F_OK) == 0)
516 return path;
518 if (wwin->net_icon_image)
519 image = RRetainImage(wwin->net_icon_image);
520 else
521 image = get_wwindow_image_from_wmhints(wwin, icon);
523 if (!image) {
524 wfree(path);
525 return NULL;
528 if (!RSaveImage(image, path, "XPM")) {
529 wfree(path);
530 path = NULL;
533 RReleaseImage(image);
535 return path;
538 static void cycleColor(void *data)
540 WIcon *icon = (WIcon *) data;
541 WScreen *scr = icon->core->screen_ptr;
542 XGCValues gcv;
544 icon->step--;
545 gcv.dash_offset = icon->step;
546 XChangeGC(dpy, scr->icon_select_gc, GCDashOffset, &gcv);
548 XDrawRectangle(dpy, icon->core->window, scr->icon_select_gc, 0, 0,
549 icon->core->width - 1, icon->core->height - 1);
550 icon->handlerID = WMAddTimerHandler(COLOR_CYCLE_DELAY, cycleColor, icon);
553 #ifdef NEWAPPICON
554 void wIconSetHighlited(WIcon *icon, Bool flag)
556 if (icon->highlighted == flag)
557 return;
559 icon->highlighted = flag;
560 update_icon_pixmap(icon);
562 #endif
564 void wIconSelect(WIcon * icon)
566 WScreen *scr = icon->core->screen_ptr;
567 icon->selected = !icon->selected;
569 if (icon->selected) {
570 icon->step = 0;
571 if (!wPreferences.dont_blink)
572 icon->handlerID = WMAddTimerHandler(10, cycleColor, icon);
573 else
574 XDrawRectangle(dpy, icon->core->window, scr->icon_select_gc, 0, 0,
575 icon->core->width - 1, icon->core->height - 1);
576 } else {
577 if (icon->handlerID) {
578 WMDeleteTimerHandler(icon->handlerID);
579 icon->handlerID = NULL;
581 XClearArea(dpy, icon->core->window, 0, 0, icon->core->width, icon->core->height, True);
585 static void unset_icon_image(WIcon *icon)
587 if (icon->file)
588 wfree(icon->file);
590 if (icon->file_image)
591 RReleaseImage(icon->file_image);
594 void wIconUpdate(WIcon *icon)
596 WScreen *scr = icon->core->screen_ptr;
597 WWindow *wwin = icon->owner;
599 assert(scr->icon_tile != NULL);
601 if (wwin && WFLAGP(wwin, always_user_icon)) {
602 /* Forced use user_icon */
603 get_rimage_icon_from_user_icon(icon);
604 } else if (icon->icon_win != None) {
605 /* Get the Pixmap from the WIcon */
606 get_rimage_icon_from_icon_win(icon);
607 } else if (wwin && wwin->net_icon_image) {
608 /* Use _NET_WM_ICON icon */
609 get_rimage_icon_from_x11(icon);
610 } else if (wwin && wwin->wm_hints && (wwin->wm_hints->flags & IconPixmapHint)) {
611 /* Get the Pixmap from the wm_hints, else, from the user */
612 if (get_rimage_icon_from_wm_hints(icon))
613 get_rimage_icon_from_user_icon(icon);
614 } else {
615 /* Get the Pixmap from the user */
616 get_rimage_icon_from_user_icon(icon);
619 update_icon_pixmap(icon);
622 void update_icon_pixmap(WIcon *icon)
624 if (icon->pixmap != None)
625 XFreePixmap(dpy, icon->pixmap);
627 icon->pixmap = None;
629 /* Create the pixmap */
630 if (icon->file_image)
631 icon_update_pixmap(icon, icon->file_image);
633 /* If dockapp, put inside the icon */
634 if (icon->icon_win != None) {
635 /* file_image is NULL, because is docked app */
636 icon_update_pixmap(icon, icon->file_image);
637 set_dockapp_in_icon(icon);
640 /* No pixmap, set default background */
641 if (icon->pixmap != None)
642 XSetWindowBackgroundPixmap(dpy, icon->core->window, icon->pixmap);
644 /* Paint it */
645 XClearWindow(dpy, icon->core->window);
646 wIconPaint(icon);
649 static void get_rimage_icon_from_x11(WIcon *icon)
651 /* Remove the icon image */
652 unset_icon_image(icon);
654 /* Set the new icon image */
655 icon->file = NULL;
656 icon->file_image = RRetainImage(icon->owner->net_icon_image);
659 static void get_rimage_icon_from_user_icon(WIcon *icon)
661 if (icon->file_image)
662 return;
664 get_rimage_icon_from_default_icon(icon);
667 static void get_rimage_icon_from_default_icon(WIcon *icon)
669 WScreen *scr = icon->core->screen_ptr;
671 /* If the icon don't have image, we should use the default image. */
672 if (!scr->def_icon_rimage)
673 scr->def_icon_rimage = get_default_image(scr);
675 /* Remove the icon image */
676 unset_icon_image(icon);
678 /* Set the new icon image */
679 icon->file = NULL;
680 icon->file_image = RRetainImage(scr->def_icon_rimage);
683 /* Get the RImage from the WIcon of the WWindow */
684 static void get_rimage_icon_from_icon_win(WIcon *icon)
686 RImage *image;
688 /* Create the new RImage */
689 image = get_window_image_from_x11(icon->icon_win);
691 /* Free the icon info */
692 unset_icon_image(icon);
694 /* Set the new info */
695 icon->file = NULL;
696 icon->file_image = image;
699 /* Set the dockapp in the WIcon */
700 static void set_dockapp_in_icon(WIcon *icon)
702 XWindowAttributes attr;
703 WScreen *scr = icon->core->screen_ptr;
704 int title_height = WMFontHeight(scr->icon_title_font);
705 unsigned int w, h, d;
706 int theight = 0;
708 /* Reparent the dock application to the icon */
710 /* We need the application size to center it
711 * and show in the correct position */
712 getSize(icon->icon_win, &w, &h, &d);
714 /* Set extra space for title */
715 if (icon->show_title && (h + title_height < wPreferences.icon_size)) {
716 theight = title_height;
717 drawIconTitle(scr, icon->pixmap, theight);
718 } else {
719 XSetWindowBackgroundPixmap(dpy, icon->core->window, scr->icon_tile_pixmap);
722 /* Set the icon border */
723 XSetWindowBorderWidth(dpy, icon->icon_win, 0);
725 /* Put the dock application in the icon */
726 XReparentWindow(dpy, icon->icon_win, icon->core->window,
727 (wPreferences.icon_size - w) / 2,
728 theight + (wPreferences.icon_size - h - theight) / 2);
730 /* Show it and save */
731 XMapWindow(dpy, icon->icon_win);
732 XAddToSaveSet(dpy, icon->icon_win);
734 /* Needed to move the icon clicking on the application part */
735 if ((XGetWindowAttributes(dpy, icon->icon_win, &attr)) &&
736 (attr.all_event_masks & ButtonPressMask))
737 wHackedGrabButton(Button1, MOD_MASK, icon->core->window, True,
738 ButtonPressMask, GrabModeSync, GrabModeAsync,
739 None, wCursor[WCUR_ARROW]);
742 /* Get the RImage from the XWindow wm_hints */
743 static int get_rimage_icon_from_wm_hints(WIcon *icon)
745 RImage *image = NULL;
746 unsigned int w, h, d;
747 WWindow *wwin = icon->owner;
749 if (!getSize(wwin->wm_hints->icon_pixmap, &w, &h, &d)) {
750 icon->owner->wm_hints->flags &= ~IconPixmapHint;
751 return 1;
754 image = get_wwindow_image_from_wmhints(wwin, icon);
755 if (!image)
756 return 1;
758 /* FIXME: If unset_icon_image, pointer double free then crash
759 unset_icon_image(icon); */
760 icon->file_image = image;
762 return 0;
765 void wIconPaint(WIcon * icon)
767 WScreen *scr = icon->core->screen_ptr;
768 int x;
769 char *tmp;
771 if (icon->force_paint) {
772 icon->force_paint = 0;
773 wIconUpdate(icon);
774 return;
777 XClearWindow(dpy, icon->core->window);
779 /* draw the icon title */
780 if (icon->show_title && icon->icon_name != NULL) {
781 int l;
782 int w;
784 tmp = ShrinkString(scr->icon_title_font, icon->icon_name, wPreferences.icon_size - 4);
785 w = WMWidthOfString(scr->icon_title_font, tmp, l = strlen(tmp));
787 if (w > icon->core->width - 4)
788 x = (icon->core->width - 4) - w;
789 else
790 x = (icon->core->width - w) / 2;
792 WMDrawString(scr->wmscreen, icon->core->window, scr->icon_title_color,
793 scr->icon_title_font, x, 1, tmp, l);
794 wfree(tmp);
797 if (icon->selected)
798 XDrawRectangle(dpy, icon->core->window, scr->icon_select_gc, 0, 0,
799 icon->core->width - 1, icon->core->height - 1);
802 /******************************************************************/
804 static void miniwindowExpose(WObjDescriptor * desc, XEvent * event)
806 wIconPaint(desc->parent);
809 static void miniwindowDblClick(WObjDescriptor * desc, XEvent * event)
811 WIcon *icon = desc->parent;
813 assert(icon->owner != NULL);
815 wDeiconifyWindow(icon->owner);
818 static void miniwindowMouseDown(WObjDescriptor * desc, XEvent * event)
820 WIcon *icon = desc->parent;
821 WWindow *wwin = icon->owner;
822 XEvent ev;
823 int x = wwin->icon_x, y = wwin->icon_y;
824 int dx = event->xbutton.x, dy = event->xbutton.y;
825 int grabbed = 0;
826 int clickButton = event->xbutton.button;
827 Bool hasMoved = False;
829 if (WCHECK_STATE(WSTATE_MODAL))
830 return;
832 if (IsDoubleClick(icon->core->screen_ptr, event)) {
833 miniwindowDblClick(desc, event);
834 return;
837 if (event->xbutton.button == Button1) {
838 if (event->xbutton.state & MOD_MASK)
839 wLowerFrame(icon->core);
840 else
841 wRaiseFrame(icon->core);
842 if (event->xbutton.state & ShiftMask) {
843 wIconSelect(icon);
844 wSelectWindow(icon->owner, !wwin->flags.selected);
846 } else if (event->xbutton.button == Button3) {
847 WObjDescriptor *desc;
849 OpenMiniwindowMenu(wwin, event->xbutton.x_root, event->xbutton.y_root);
851 /* allow drag select of menu */
852 desc = &wwin->screen_ptr->window_menu->menu->descriptor;
853 event->xbutton.send_event = True;
854 (*desc->handle_mousedown) (desc, event);
856 return;
859 if (XGrabPointer(dpy, icon->core->window, False, ButtonMotionMask
860 | ButtonReleaseMask | ButtonPressMask, GrabModeAsync,
861 GrabModeAsync, None, None, CurrentTime) != GrabSuccess) {
863 while (1) {
864 WMMaskEvent(dpy, PointerMotionMask | ButtonReleaseMask | ButtonPressMask
865 | ButtonMotionMask | ExposureMask, &ev);
866 switch (ev.type) {
867 case Expose:
868 WMHandleEvent(&ev);
869 break;
871 case MotionNotify:
872 hasMoved = True;
873 if (!grabbed) {
874 if (abs(dx - ev.xmotion.x) >= MOVE_THRESHOLD
875 || abs(dy - ev.xmotion.y) >= MOVE_THRESHOLD) {
876 XChangeActivePointerGrab(dpy, ButtonMotionMask
877 | ButtonReleaseMask | ButtonPressMask,
878 wCursor[WCUR_MOVE], CurrentTime);
879 grabbed = 1;
880 } else {
881 break;
884 x = ev.xmotion.x_root - dx;
885 y = ev.xmotion.y_root - dy;
886 XMoveWindow(dpy, icon->core->window, x, y);
887 break;
889 case ButtonPress:
890 break;
892 case ButtonRelease:
893 if (ev.xbutton.button != clickButton)
894 break;
896 if (wwin->icon_x != x || wwin->icon_y != y)
897 wwin->flags.icon_moved = 1;
899 XMoveWindow(dpy, icon->core->window, x, y);
901 wwin->icon_x = x;
902 wwin->icon_y = y;
903 XUngrabPointer(dpy, CurrentTime);
905 if (wPreferences.auto_arrange_icons)
906 wArrangeIcons(wwin->screen_ptr, True);
907 if (wPreferences.single_click && !hasMoved)
908 miniwindowDblClick(desc, event);
909 return;