Add OPEN_PLMENU option to parse command generated proplist style menus
[wmaker-crm.git] / src / icon.c
blobe88ec30dba7ccd5a2ef079a3e8546db17e31f946
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 void get_rimage_icon_from_user_icon(WIcon *icon);
66 static void get_rimage_icon_from_default_icon(WIcon *icon);
67 static void get_rimage_icon_from_x11(WIcon *icon);
69 static void icon_update_pixmap(WIcon *icon, RImage *image);
70 static void unset_icon_image(WIcon *icon);
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 /* If the rimage exists, update the icon, else create it */
81 if (icon->file_image)
82 update_icon_pixmap(icon);
83 else
84 wIconPaint(icon);
87 /* so that the appicon expose handlers will paint the appicon specific
88 * stuff */
89 XClearArea(dpy, icon->core->window, 0, 0, icon->core->width, icon->core->height, True);
92 static void tileObserver(void *self, WMNotification *notif)
94 WIcon *icon = (WIcon *) self;
96 wIconUpdate(icon, NULL);
98 XClearArea(dpy, icon->core->window, 0, 0, 1, 1, True);
101 /************************************/
103 static int getSize(Drawable d, unsigned int *w, unsigned int *h, unsigned int *dep)
105 Window rjunk;
106 int xjunk, yjunk;
107 unsigned int bjunk;
109 return XGetGeometry(dpy, d, &rjunk, &xjunk, &yjunk, w, h, &bjunk, dep);
112 WIcon *icon_create_for_wwindow(WWindow *wwin)
114 WScreen *scr = wwin->screen_ptr;
115 WIcon *icon;
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 icon->tile_type = TILE_NORMAL;
145 set_icon_image_from_database(icon, wwin->wm_instance, wwin->wm_class, NULL);
147 WMAddNotificationObserver(appearanceObserver, icon, WNIconAppearanceSettingsChanged, icon);
148 WMAddNotificationObserver(tileObserver, icon, WNIconTileSettingsChanged, icon);
150 return icon;
153 WIcon *icon_create_for_dock(WScreen *scr, char *command, char *wm_instance, char *wm_class, int tile)
155 WIcon *icon;
157 icon = icon_create_core(scr, 0, 0);
158 icon->tile_type = tile;
160 set_icon_image_from_database(icon, wm_instance, wm_class, command);
162 WMAddNotificationObserver(appearanceObserver, icon, WNIconAppearanceSettingsChanged, icon);
163 WMAddNotificationObserver(tileObserver, icon, WNIconTileSettingsChanged, icon);
165 return icon;
168 static WIcon *icon_create_core(WScreen *scr, int coord_x, int coord_y)
170 WIcon *icon;
171 unsigned long vmask = 0;
172 XSetWindowAttributes attribs;
174 icon = wmalloc(sizeof(WIcon));
175 icon->core = wCoreCreateTopLevel(scr,
176 coord_x,
177 coord_y,
178 wPreferences.icon_size,
179 wPreferences.icon_size,
180 0, scr->w_depth, scr->w_visual, scr->w_colormap);
182 if (wPreferences.use_saveunders) {
183 vmask = CWSaveUnder;
184 attribs.save_under = True;
187 /* a white border for selecting it */
188 vmask |= CWBorderPixel;
189 attribs.border_pixel = scr->white_pixel;
191 XChangeWindowAttributes(dpy, icon->core->window, vmask, &attribs);
193 /* will be overriden if this is a application icon */
194 icon->core->descriptor.handle_mousedown = miniwindowMouseDown;
195 icon->core->descriptor.handle_expose = miniwindowExpose;
196 icon->core->descriptor.parent_type = WCLASS_MINIWINDOW;
197 icon->core->descriptor.parent = icon;
199 icon->core->stacking = wmalloc(sizeof(WStacking));
200 icon->core->stacking->above = NULL;
201 icon->core->stacking->under = NULL;
202 icon->core->stacking->window_level = NORMAL_ICON_LEVEL;
203 icon->core->stacking->child_of = NULL;
205 /* Icon image */
206 icon->file = NULL;
207 icon->file_image = NULL;
209 return icon;
212 void wIconDestroy(WIcon * icon)
214 WCoreWindow *core = icon->core;
215 WScreen *scr = core->screen_ptr;
217 WMRemoveNotificationObserver(icon);
219 if (icon->handlerID)
220 WMDeleteTimerHandler(icon->handlerID);
222 if (icon->icon_win) {
223 int x = 0, y = 0;
225 if (icon->owner) {
226 x = icon->owner->icon_x;
227 y = icon->owner->icon_y;
229 XUnmapWindow(dpy, icon->icon_win);
230 XReparentWindow(dpy, icon->icon_win, scr->root_win, x, y);
232 if (icon->icon_name)
233 XFree(icon->icon_name);
235 if (icon->pixmap)
236 XFreePixmap(dpy, icon->pixmap);
238 unset_icon_image(icon);
240 wCoreDestroy(icon->core);
241 wfree(icon);
244 static void drawIconTitle(WScreen * scr, Pixmap pixmap, int height)
246 XFillRectangle(dpy, pixmap, scr->icon_title_texture->normal_gc, 0, 0, wPreferences.icon_size, height + 1);
247 XDrawLine(dpy, pixmap, scr->icon_title_texture->light_gc, 0, 0, wPreferences.icon_size, 0);
248 XDrawLine(dpy, pixmap, scr->icon_title_texture->light_gc, 0, 0, 0, height + 1);
249 XDrawLine(dpy, pixmap, scr->icon_title_texture->dim_gc,
250 wPreferences.icon_size - 1, 0, wPreferences.icon_size - 1, height + 1);
253 static void icon_update_pixmap(WIcon *icon, RImage *image)
255 RImage *tile;
256 Pixmap pixmap;
257 int x, y, sx, sy;
258 unsigned w, h;
259 int theight = 0;
260 WScreen *scr = icon->core->screen_ptr;
261 int titled = icon->show_title;
263 if (icon->tile_type == TILE_NORMAL) {
264 tile = RCloneImage(scr->icon_tile);
265 } else {
266 assert(scr->clip_tile);
267 tile = RCloneImage(scr->clip_tile);
270 if (image) {
271 w = (image->width > wPreferences.icon_size)
272 ? wPreferences.icon_size : image->width;
273 x = (wPreferences.icon_size - w) / 2;
274 sx = (image->width - w) / 2;
276 if (titled)
277 theight = WMFontHeight(scr->icon_title_font);
279 h = (image->height + theight > wPreferences.icon_size
280 ? wPreferences.icon_size - theight : image->height);
281 y = theight + (wPreferences.icon_size - theight - h) / 2;
282 sy = (image->height - h) / 2;
284 RCombineArea(tile, image, sx, sy, w, h, x, y);
287 if (icon->shadowed) {
288 RColor color;
290 color.red = scr->icon_back_texture->light.red >> 8;
291 color.green = scr->icon_back_texture->light.green >> 8;
292 color.blue = scr->icon_back_texture->light.blue >> 8;
293 color.alpha = 150; /* about 60% */
294 RClearImage(tile, &color);
297 if (icon->highlighted) {
298 RColor color;
300 color.red = color.green = color.blue = 0;
301 color.alpha = 160;
302 RLightImage(tile, &color);
305 if (!RConvertImage(scr->rcontext, tile, &pixmap))
306 wwarning(_("error rendering image:%s"), RMessageForError(RErrorCode));
308 RReleaseImage(tile);
310 if (titled)
311 drawIconTitle(scr, pixmap, theight);
313 icon->pixmap = pixmap;
316 void wIconChangeTitle(WIcon *icon, char *new_title)
318 int changed;
320 changed = (new_title == NULL && icon->icon_name != NULL) ||
321 (new_title != NULL && icon->icon_name == NULL);
323 if (icon->icon_name != NULL)
324 XFree(icon->icon_name);
326 icon->icon_name = new_title;
328 if (changed)
329 wIconUpdate(icon, NULL);
330 else
331 wIconPaint(icon);
334 RImage *wIconValidateIconSize(RImage *icon, int max_size)
336 RImage *nimage;
338 if (!icon)
339 return NULL;
341 /* We should hold "ICON_BORDER" (~2) pixels to include the icon border */
342 if (((max_size + ICON_BORDER) < icon->width) ||
343 ((max_size + ICON_BORDER) < icon->height)) {
344 if (icon->width > icon->height)
345 nimage = RScaleImage(icon, max_size - ICON_BORDER,
346 (icon->height * (max_size - ICON_BORDER) / icon->width));
347 else
348 nimage = RScaleImage(icon, (icon->width * (max_size - ICON_BORDER) / icon->height),
349 max_size - ICON_BORDER);
350 RReleaseImage(icon);
351 icon = nimage;
354 return icon;
357 Bool wIconChangeImageFile(WIcon *icon, char *file)
359 WScreen *scr = icon->core->screen_ptr;
360 char *path;
361 RImage *image = NULL;
362 int error = 0;
364 /* If no new image, don't do nothing */
365 if (!file)
366 return True;
368 /* Find the new image */
369 path = FindImage(wPreferences.icon_path, file);
370 if (path)
371 image = get_rimage_from_file(scr, path, wPreferences.icon_size);
372 else
373 error = 1;
375 /* New image! */
376 if (!error && image) {
377 /* Remove the old one */
378 unset_icon_image(icon);
380 /* Set the new image */
381 icon->file = wstrdup(path);
382 wIconUpdate(icon, image);
383 } else {
384 error = 1;
387 if (path)
388 wfree(path);
390 return !error;
393 static char *get_name_for_wwin(WWindow *wwin)
395 return get_name_for_instance_class(wwin->wm_instance, wwin->wm_class);
398 char *get_name_for_instance_class(char *wm_instance, char *wm_class)
400 char *suffix;
401 int len;
403 if (wm_class && wm_instance) {
404 len = strlen(wm_class) + strlen(wm_instance) + 2;
405 suffix = wmalloc(len);
406 snprintf(suffix, len, "%s.%s", wm_instance, wm_class);
407 } else if (wm_class) {
408 len = strlen(wm_class) + 1;
409 suffix = wmalloc(len);
410 snprintf(suffix, len, "%s", wm_class);
411 } else if (wm_instance) {
412 len = strlen(wm_instance) + 1;
413 suffix = wmalloc(len);
414 snprintf(suffix, len, "%s", wm_instance);
415 } else {
416 return NULL;
419 return suffix;
422 static char *get_icon_cache_path(void)
424 char *prefix, *path;
425 int len, ret;
427 prefix = wusergnusteppath();
428 len = strlen(prefix) + strlen(CACHE_ICON_PATH) + 2;
429 path = wmalloc(len);
430 snprintf(path, len, "%s%s/", prefix, CACHE_ICON_PATH);
432 /* If the folder exists, exit */
433 if (access(path, F_OK) == 0)
434 return path;
436 /* Create the folder */
437 ret = wmkdirhier((const char *) path);
439 /* Exit 1 on success, 0 on failure */
440 if (ret == 1)
441 return path;
443 /* Fail */
444 wfree(path);
445 return NULL;
448 static RImage *get_wwindow_image_from_wmhints(WWindow *wwin, WIcon *icon)
450 RImage *image = NULL;
451 XWMHints *hints = wwin->wm_hints;
453 if (hints && (hints->flags & IconPixmapHint) && hints->icon_pixmap != None)
454 image = RCreateImageFromDrawable(icon->core->screen_ptr->rcontext,
455 hints->icon_pixmap,
456 (hints->flags & IconMaskHint)
457 ? hints->icon_mask : None);
459 return image;
463 * wIconStore--
464 * Stores the client supplied icon at CACHE_ICON_PATH
465 * and returns the path for that icon. Returns NULL if there is no
466 * client supplied icon or on failure.
468 * Side effects:
469 * New directories might be created.
471 char *wIconStore(WIcon * icon)
473 char *path, *dir_path, *file;
474 int len = 0;
475 RImage *image = NULL;
476 WWindow *wwin = icon->owner;
478 if (!wwin)
479 return NULL;
481 dir_path = get_icon_cache_path();
482 if (!dir_path)
483 return NULL;
485 file = get_name_for_wwin(wwin);
486 if (!file) {
487 wfree(dir_path);
488 return NULL;
491 len = strlen(dir_path) + strlen(file) + 5;
492 path = wmalloc(len);
493 snprintf(path, len, "%s%s.xpm", dir_path, file);
494 wfree(dir_path);
495 wfree(file);
497 /* If icon exists, exit */
498 if (access(path, F_OK) == 0)
499 return path;
501 if (wwin->net_icon_image)
502 image = RRetainImage(wwin->net_icon_image);
503 else
504 image = get_wwindow_image_from_wmhints(wwin, icon);
506 if (!image) {
507 wfree(path);
508 return NULL;
511 if (!RSaveImage(image, path, "XPM")) {
512 wfree(path);
513 path = NULL;
516 RReleaseImage(image);
518 return path;
521 static void cycleColor(void *data)
523 WIcon *icon = (WIcon *) data;
524 WScreen *scr = icon->core->screen_ptr;
525 XGCValues gcv;
527 icon->step--;
528 gcv.dash_offset = icon->step;
529 XChangeGC(dpy, scr->icon_select_gc, GCDashOffset, &gcv);
531 XDrawRectangle(dpy, icon->core->window, scr->icon_select_gc, 0, 0,
532 icon->core->width - 1, icon->core->height - 1);
533 icon->handlerID = WMAddTimerHandler(COLOR_CYCLE_DELAY, cycleColor, icon);
536 void wIconSetHighlited(WIcon *icon, Bool flag)
538 if (icon->highlighted == flag)
539 return;
541 icon->highlighted = flag;
542 update_icon_pixmap(icon);
545 void wIconSelect(WIcon * icon)
547 WScreen *scr = icon->core->screen_ptr;
548 icon->selected = !icon->selected;
550 if (icon->selected) {
551 icon->step = 0;
552 if (!wPreferences.dont_blink)
553 icon->handlerID = WMAddTimerHandler(10, cycleColor, icon);
554 else
555 XDrawRectangle(dpy, icon->core->window, scr->icon_select_gc, 0, 0,
556 icon->core->width - 1, icon->core->height - 1);
557 } else {
558 if (icon->handlerID) {
559 WMDeleteTimerHandler(icon->handlerID);
560 icon->handlerID = NULL;
562 XClearArea(dpy, icon->core->window, 0, 0, icon->core->width, icon->core->height, True);
566 static void unset_icon_image(WIcon *icon)
568 if (icon->file) {
569 wfree(icon->file);
570 icon->file = NULL;
573 if (icon->file_image) {
574 RReleaseImage(icon->file_image);
575 icon->file_image = NULL;
579 void wIconUpdate(WIcon *icon, RImage *image)
581 WWindow *wwin = NULL;
583 if (image) {
584 icon->file_image = image;
585 } else {
586 if (icon && icon->owner)
587 wwin = icon->owner;
589 if (wwin && WFLAGP(wwin, always_user_icon)) {
590 /* Forced use user_icon */
591 get_rimage_icon_from_user_icon(icon);
592 } else if (icon->icon_win != None) {
593 /* Get the Pixmap from the WIcon */
594 get_rimage_icon_from_icon_win(icon);
595 } else if (wwin && wwin->net_icon_image) {
596 /* Use _NET_WM_ICON icon */
597 get_rimage_icon_from_x11(icon);
598 } else if (wwin && wwin->wm_hints && (wwin->wm_hints->flags & IconPixmapHint)) {
599 /* Get the Pixmap from the wm_hints, else, from the user */
600 unset_icon_image(icon);
601 icon->file_image = get_rimage_icon_from_wm_hints(icon);
602 if (!icon->file_image)
603 get_rimage_icon_from_user_icon(icon);
604 } else {
605 /* Get the Pixmap from the user */
606 get_rimage_icon_from_user_icon(icon);
610 update_icon_pixmap(icon);
613 void update_icon_pixmap(WIcon *icon)
615 if (icon->pixmap != None)
616 XFreePixmap(dpy, icon->pixmap);
618 icon->pixmap = None;
620 /* Create the pixmap */
621 if (icon->file_image)
622 icon_update_pixmap(icon, icon->file_image);
624 /* If dockapp, put inside the icon */
625 if (icon->icon_win != None) {
626 /* file_image is NULL, because is docked app */
627 icon_update_pixmap(icon, NULL);
628 set_dockapp_in_icon(icon);
631 /* No pixmap, set default background */
632 if (icon->pixmap != None)
633 XSetWindowBackgroundPixmap(dpy, icon->core->window, icon->pixmap);
635 /* Paint it */
636 XClearWindow(dpy, icon->core->window);
637 wIconPaint(icon);
640 static void get_rimage_icon_from_x11(WIcon *icon)
642 /* Remove the icon image */
643 unset_icon_image(icon);
645 /* Set the new icon image */
646 icon->file_image = RRetainImage(icon->owner->net_icon_image);
649 static void get_rimage_icon_from_user_icon(WIcon *icon)
651 if (icon->file_image)
652 return;
654 get_rimage_icon_from_default_icon(icon);
657 static void get_rimage_icon_from_default_icon(WIcon *icon)
659 WScreen *scr = icon->core->screen_ptr;
661 /* If the icon don't have image, we should use the default image. */
662 if (!scr->def_icon_rimage)
663 scr->def_icon_rimage = get_default_image(scr);
665 /* Remove the icon image */
666 unset_icon_image(icon);
668 /* Set the new icon image */
669 icon->file_image = RRetainImage(scr->def_icon_rimage);
672 /* Get the RImage from the WIcon of the WWindow */
673 static void get_rimage_icon_from_icon_win(WIcon *icon)
675 RImage *image;
677 /* Create the new RImage */
678 image = get_window_image_from_x11(icon->icon_win);
680 /* Free the icon info */
681 unset_icon_image(icon);
683 /* Set the new info */
684 icon->file_image = image;
687 /* Set the dockapp in the WIcon */
688 static void set_dockapp_in_icon(WIcon *icon)
690 XWindowAttributes attr;
691 WScreen *scr = icon->core->screen_ptr;
692 int title_height = WMFontHeight(scr->icon_title_font);
693 unsigned int w, h, d;
694 int theight = 0;
696 /* Reparent the dock application to the icon */
698 /* We need the application size to center it
699 * and show in the correct position */
700 getSize(icon->icon_win, &w, &h, &d);
702 /* Set extra space for title */
703 if (icon->show_title && (h + title_height < wPreferences.icon_size)) {
704 theight = title_height;
705 drawIconTitle(scr, icon->pixmap, theight);
706 } else {
707 XSetWindowBackgroundPixmap(dpy, icon->core->window, scr->icon_tile_pixmap);
710 /* Set the icon border */
711 XSetWindowBorderWidth(dpy, icon->icon_win, 0);
713 /* Put the dock application in the icon */
714 XReparentWindow(dpy, icon->icon_win, icon->core->window,
715 (wPreferences.icon_size - w) / 2,
716 theight + (wPreferences.icon_size - h - theight) / 2);
718 /* Show it and save */
719 XMapWindow(dpy, icon->icon_win);
720 XAddToSaveSet(dpy, icon->icon_win);
722 /* Needed to move the icon clicking on the application part */
723 if ((XGetWindowAttributes(dpy, icon->icon_win, &attr)) &&
724 (attr.all_event_masks & ButtonPressMask))
725 wHackedGrabButton(Button1, MOD_MASK, icon->core->window, True,
726 ButtonPressMask, GrabModeSync, GrabModeAsync,
727 None, wCursor[WCUR_ARROW]);
730 /* Get the RImage from the XWindow wm_hints */
731 RImage *get_rimage_icon_from_wm_hints(WIcon *icon)
733 RImage *image = NULL;
734 unsigned int w, h, d;
735 WWindow *wwin = icon->owner;
737 if (!getSize(wwin->wm_hints->icon_pixmap, &w, &h, &d)) {
738 icon->owner->wm_hints->flags &= ~IconPixmapHint;
739 return NULL;
742 image = get_wwindow_image_from_wmhints(wwin, icon);
743 if (!image)
744 return NULL;
746 /* Resize the icon to the wPreferences.icon_size size */
747 image = wIconValidateIconSize(image, wPreferences.icon_size);
749 return image;
752 void wIconPaint(WIcon *icon)
754 WScreen *scr = icon->core->screen_ptr;
755 int x, l, w;
756 char *tmp;
758 XClearWindow(dpy, icon->core->window);
760 /* draw the icon title */
761 if (icon->show_title && icon->icon_name != NULL) {
762 tmp = ShrinkString(scr->icon_title_font, icon->icon_name, wPreferences.icon_size - 4);
763 w = WMWidthOfString(scr->icon_title_font, tmp, l = strlen(tmp));
765 if (w > icon->core->width - 4)
766 x = (icon->core->width - 4) - w;
767 else
768 x = (icon->core->width - w) / 2;
770 WMDrawString(scr->wmscreen, icon->core->window, scr->icon_title_color,
771 scr->icon_title_font, x, 1, tmp, l);
772 wfree(tmp);
775 if (icon->selected)
776 XDrawRectangle(dpy, icon->core->window, scr->icon_select_gc, 0, 0,
777 icon->core->width - 1, icon->core->height - 1);
780 /******************************************************************/
782 static void miniwindowExpose(WObjDescriptor * desc, XEvent * event)
784 wIconPaint(desc->parent);
787 static void miniwindowDblClick(WObjDescriptor * desc, XEvent * event)
789 WIcon *icon = desc->parent;
791 assert(icon->owner != NULL);
793 wDeiconifyWindow(icon->owner);
796 static void miniwindowMouseDown(WObjDescriptor * desc, XEvent * event)
798 WIcon *icon = desc->parent;
799 WWindow *wwin = icon->owner;
800 XEvent ev;
801 int x = wwin->icon_x, y = wwin->icon_y;
802 int dx = event->xbutton.x, dy = event->xbutton.y;
803 int grabbed = 0;
804 int clickButton = event->xbutton.button;
805 Bool hasMoved = False;
807 if (WCHECK_STATE(WSTATE_MODAL))
808 return;
810 if (IsDoubleClick(icon->core->screen_ptr, event)) {
811 miniwindowDblClick(desc, event);
812 return;
815 if (event->xbutton.button == Button1) {
816 if (event->xbutton.state & MOD_MASK)
817 wLowerFrame(icon->core);
818 else
819 wRaiseFrame(icon->core);
820 if (event->xbutton.state & ShiftMask) {
821 wIconSelect(icon);
822 wSelectWindow(icon->owner, !wwin->flags.selected);
824 } else if (event->xbutton.button == Button3) {
825 WObjDescriptor *desc;
827 OpenMiniwindowMenu(wwin, event->xbutton.x_root, event->xbutton.y_root);
829 /* allow drag select of menu */
830 desc = &wwin->screen_ptr->window_menu->menu->descriptor;
831 event->xbutton.send_event = True;
832 (*desc->handle_mousedown) (desc, event);
834 return;
837 if (XGrabPointer(dpy, icon->core->window, False, ButtonMotionMask
838 | ButtonReleaseMask | ButtonPressMask, GrabModeAsync,
839 GrabModeAsync, None, None, CurrentTime) != GrabSuccess) {
841 while (1) {
842 WMMaskEvent(dpy, PointerMotionMask | ButtonReleaseMask | ButtonPressMask
843 | ButtonMotionMask | ExposureMask, &ev);
844 switch (ev.type) {
845 case Expose:
846 WMHandleEvent(&ev);
847 break;
849 case MotionNotify:
850 hasMoved = True;
851 if (!grabbed) {
852 if (abs(dx - ev.xmotion.x) >= MOVE_THRESHOLD
853 || abs(dy - ev.xmotion.y) >= MOVE_THRESHOLD) {
854 XChangeActivePointerGrab(dpy, ButtonMotionMask
855 | ButtonReleaseMask | ButtonPressMask,
856 wCursor[WCUR_MOVE], CurrentTime);
857 grabbed = 1;
858 } else {
859 break;
862 x = ev.xmotion.x_root - dx;
863 y = ev.xmotion.y_root - dy;
864 XMoveWindow(dpy, icon->core->window, x, y);
865 break;
867 case ButtonPress:
868 break;
870 case ButtonRelease:
871 if (ev.xbutton.button != clickButton)
872 break;
874 if (wwin->icon_x != x || wwin->icon_y != y)
875 wwin->flags.icon_moved = 1;
877 XMoveWindow(dpy, icon->core->window, x, y);
879 wwin->icon_x = x;
880 wwin->icon_y = y;
881 XUngrabPointer(dpy, CurrentTime);
883 if (wPreferences.auto_arrange_icons)
884 wArrangeIcons(wwin->screen_ptr, True);
885 if (wPreferences.single_click && !hasMoved)
886 miniwindowDblClick(desc, event);
887 return;
893 void set_icon_image_from_database(WIcon *icon, char *wm_instance, char *wm_class, char *command)
895 char *file = NULL;
897 file = get_icon_filename(icon->core->screen_ptr, wm_instance, wm_class, command, False);
898 if (file) {
899 icon->file = wstrdup(file);
900 icon->file_image = get_rimage_from_file(icon->core->screen_ptr, icon->file, wPreferences.icon_size);
901 wfree(file);
904 wIconUpdate(icon, NULL);