Prevent crash when switchpanel is not initialised.
[wmaker-crm.git] / src / icon.c
blob95d3dd744214cb8a3e946daca120170522790b8e
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 "stacking.h"
42 #include "application.h"
43 #include "defaults.h"
44 #include "appicon.h"
45 #include "wmspec.h"
46 #include "misc.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 update_icon_pixmap(icon);
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 wIconChangeTitle(icon, wwin);
138 icon->tile_type = TILE_NORMAL;
140 set_icon_image_from_database(icon, wwin->wm_instance, wwin->wm_class, NULL);
141 /* Update the icon, because icon could be NULL */
142 wIconUpdate(icon);
144 WMAddNotificationObserver(appearanceObserver, icon, WNIconAppearanceSettingsChanged, icon);
145 WMAddNotificationObserver(tileObserver, icon, WNIconTileSettingsChanged, icon);
147 return icon;
150 WIcon *icon_create_for_dock(WScreen *scr, char *command, char *wm_instance, char *wm_class, int tile)
152 WIcon *icon;
154 icon = icon_create_core(scr, 0, 0);
155 icon->tile_type = tile;
157 set_icon_image_from_database(icon, wm_instance, wm_class, command);
158 /* Update the icon, because icon could be NULL */
159 wIconUpdate(icon);
161 WMAddNotificationObserver(appearanceObserver, icon, WNIconAppearanceSettingsChanged, icon);
162 WMAddNotificationObserver(tileObserver, icon, WNIconTileSettingsChanged, icon);
164 return icon;
167 static WIcon *icon_create_core(WScreen *scr, int coord_x, int coord_y)
169 WIcon *icon;
171 icon = wmalloc(sizeof(WIcon));
172 icon->core = wCoreCreateTopLevel(scr,
173 coord_x,
174 coord_y,
175 wPreferences.icon_size,
176 wPreferences.icon_size,
177 0, scr->w_depth, scr->w_visual, scr->w_colormap,
178 scr->white_pixel);
180 /* will be overriden if this is a application icon */
181 icon->core->descriptor.handle_mousedown = miniwindowMouseDown;
182 icon->core->descriptor.handle_expose = miniwindowExpose;
183 icon->core->descriptor.parent_type = WCLASS_MINIWINDOW;
184 icon->core->descriptor.parent = icon;
186 icon->core->stacking = wmalloc(sizeof(WStacking));
187 icon->core->stacking->above = NULL;
188 icon->core->stacking->under = NULL;
189 icon->core->stacking->window_level = NORMAL_ICON_LEVEL;
190 icon->core->stacking->child_of = NULL;
192 /* Icon image */
193 icon->file = NULL;
194 icon->file_image = NULL;
196 return icon;
199 void wIconDestroy(WIcon * icon)
201 WCoreWindow *core = icon->core;
202 WScreen *scr = core->screen_ptr;
204 WMRemoveNotificationObserver(icon);
206 if (icon->handlerID)
207 WMDeleteTimerHandler(icon->handlerID);
209 if (icon->icon_win) {
210 int x = 0, y = 0;
212 if (icon->owner) {
213 x = icon->owner->icon_x;
214 y = icon->owner->icon_y;
216 XUnmapWindow(dpy, icon->icon_win);
217 XReparentWindow(dpy, icon->icon_win, scr->root_win, x, y);
219 if (icon->icon_name)
220 XFree(icon->icon_name);
222 if (icon->pixmap)
223 XFreePixmap(dpy, icon->pixmap);
225 unset_icon_image(icon);
227 wCoreDestroy(icon->core);
228 wfree(icon);
231 static void drawIconTitleBackground(WScreen *scr, Pixmap pixmap, int height)
233 XFillRectangle(dpy, pixmap, scr->icon_title_texture->normal_gc, 0, 0, wPreferences.icon_size, height + 1);
234 XDrawLine(dpy, pixmap, scr->icon_title_texture->light_gc, 0, 0, wPreferences.icon_size, 0);
235 XDrawLine(dpy, pixmap, scr->icon_title_texture->light_gc, 0, 0, 0, height + 1);
236 XDrawLine(dpy, pixmap, scr->icon_title_texture->dim_gc,
237 wPreferences.icon_size - 1, 0, wPreferences.icon_size - 1, height + 1);
240 static void icon_update_pixmap(WIcon *icon, RImage *image)
242 RImage *tile;
243 Pixmap pixmap;
244 int x, y, sx, sy;
245 unsigned w, h;
246 int theight = 0;
247 WScreen *scr = icon->core->screen_ptr;
249 switch (icon->tile_type) {
250 case TILE_NORMAL:
251 tile = RCloneImage(scr->icon_tile);
252 break;
253 case TILE_CLIP:
254 tile = RCloneImage(scr->clip_tile);
255 break;
256 case TILE_DRAWER:
257 tile = RCloneImage(scr->drawer_tile);
258 break;
259 default:
261 * The icon has always rigth value, this case is
262 * only to avoid a compiler warning with "tile"
263 * "may be used uninitialized"
265 wwarning("Unknown tile type: %d.\n", icon->tile_type);
266 tile = RCloneImage(scr->icon_tile);
269 if (image) {
270 w = (image->width > wPreferences.icon_size)
271 ? wPreferences.icon_size : image->width;
272 x = (wPreferences.icon_size - w) / 2;
273 sx = (image->width - w) / 2;
275 if (icon->show_title)
276 theight = WMFontHeight(scr->icon_title_font);
278 h = (image->height + theight > wPreferences.icon_size
279 ? wPreferences.icon_size - theight : image->height);
280 y = theight + (wPreferences.icon_size - theight - h) / 2;
281 sy = (image->height - h) / 2;
283 RCombineArea(tile, image, sx, sy, w, h, x, y);
286 if (icon->shadowed) {
287 RColor color;
289 color.red = scr->icon_back_texture->light.red >> 8;
290 color.green = scr->icon_back_texture->light.green >> 8;
291 color.blue = scr->icon_back_texture->light.blue >> 8;
292 color.alpha = 150; /* about 60% */
293 RClearImage(tile, &color);
296 if (icon->highlighted) {
297 RColor color;
299 color.red = color.green = color.blue = 0;
300 color.alpha = 160;
301 RLightImage(tile, &color);
304 if (!RConvertImage(scr->rcontext, tile, &pixmap))
305 wwarning(_("error rendering image:%s"), RMessageForError(RErrorCode));
307 RReleaseImage(tile);
309 /* Draw the icon's title background (without text) */
310 if (icon->show_title)
311 drawIconTitleBackground(scr, pixmap, theight);
313 icon->pixmap = pixmap;
316 void wIconChangeTitle(WIcon *icon, WWindow *wwin)
318 if (!icon || !wwin)
319 return;
321 /* Remove the previous icon title */
322 if (icon->icon_name != NULL)
323 XFree(icon->icon_name);
325 /* Set the new one, using two methods */
326 icon->icon_name = wNETWMGetIconName(wwin->client_win);
327 if (!icon->icon_name)
328 wGetIconName(dpy, wwin->client_win, &icon->icon_name);
331 RImage *wIconValidateIconSize(RImage *icon, int max_size)
333 RImage *nimage;
335 if (!icon)
336 return NULL;
338 /* We should hold "ICON_BORDER" (~2) pixels to include the icon border */
339 if (((max_size + ICON_BORDER) < icon->width) ||
340 ((max_size + ICON_BORDER) < icon->height)) {
341 if (icon->width > icon->height)
342 nimage = RScaleImage(icon, max_size - ICON_BORDER,
343 (icon->height * (max_size - ICON_BORDER) / icon->width));
344 else
345 nimage = RScaleImage(icon, (icon->width * (max_size - ICON_BORDER) / icon->height),
346 max_size - ICON_BORDER);
347 RReleaseImage(icon);
348 icon = nimage;
351 return icon;
354 Bool wIconChangeImageFile(WIcon *icon, char *file)
356 WScreen *scr = icon->core->screen_ptr;
357 char *path;
358 RImage *image = NULL;
359 int error = 0;
361 /* If no new image, don't do nothing */
362 if (!file)
363 return True;
365 /* Find the new image */
366 path = FindImage(wPreferences.icon_path, file);
367 if (path)
368 image = get_rimage_from_file(scr, path, wPreferences.icon_size);
369 else
370 error = 1;
372 /* New image! */
373 if (!error && image) {
374 /* Set the new image */
375 set_icon_image_from_image(icon, image);
376 icon->file = wstrdup(path);
377 update_icon_pixmap(icon);
378 } else {
379 error = 1;
382 if (path)
383 wfree(path);
385 return !error;
388 static char *get_name_for_wwin(WWindow *wwin)
390 return get_name_for_instance_class(wwin->wm_instance, wwin->wm_class);
393 char *get_name_for_instance_class(char *wm_instance, char *wm_class)
395 char *suffix;
396 int len;
398 if (wm_class && wm_instance) {
399 len = strlen(wm_class) + strlen(wm_instance) + 2;
400 suffix = wmalloc(len);
401 snprintf(suffix, len, "%s.%s", wm_instance, wm_class);
402 } else if (wm_class) {
403 len = strlen(wm_class) + 1;
404 suffix = wmalloc(len);
405 snprintf(suffix, len, "%s", wm_class);
406 } else if (wm_instance) {
407 len = strlen(wm_instance) + 1;
408 suffix = wmalloc(len);
409 snprintf(suffix, len, "%s", wm_instance);
410 } else {
411 return NULL;
414 return suffix;
417 static char *get_icon_cache_path(void)
419 const char *prefix;
420 char *path;
421 int len, ret;
423 prefix = wusergnusteppath();
424 len = strlen(prefix) + strlen(CACHE_ICON_PATH) + 2;
425 path = wmalloc(len);
426 snprintf(path, len, "%s%s/", prefix, CACHE_ICON_PATH);
428 /* If the folder exists, exit */
429 if (access(path, F_OK) == 0)
430 return path;
432 /* Create the folder */
433 ret = wmkdirhier((const char *) path);
435 /* Exit 1 on success, 0 on failure */
436 if (ret == 1)
437 return path;
439 /* Fail */
440 wfree(path);
441 return NULL;
444 static RImage *get_wwindow_image_from_wmhints(WWindow *wwin, WIcon *icon)
446 RImage *image = NULL;
447 XWMHints *hints = wwin->wm_hints;
449 if (hints && (hints->flags & IconPixmapHint) && hints->icon_pixmap != None)
450 image = RCreateImageFromDrawable(icon->core->screen_ptr->rcontext,
451 hints->icon_pixmap,
452 (hints->flags & IconMaskHint)
453 ? hints->icon_mask : None);
455 return image;
459 * wIconStore--
460 * Stores the client supplied icon at CACHE_ICON_PATH
461 * and returns the path for that icon. Returns NULL if there is no
462 * client supplied icon or on failure.
464 * Side effects:
465 * New directories might be created.
467 char *wIconStore(WIcon * icon)
469 char *path, *dir_path, *file;
470 int len = 0;
471 RImage *image = NULL;
472 WWindow *wwin = icon->owner;
474 if (!wwin)
475 return NULL;
477 dir_path = get_icon_cache_path();
478 if (!dir_path)
479 return NULL;
481 file = get_name_for_wwin(wwin);
482 if (!file) {
483 wfree(dir_path);
484 return NULL;
487 len = strlen(dir_path) + strlen(file) + 5;
488 path = wmalloc(len);
489 snprintf(path, len, "%s%s.xpm", dir_path, file);
490 wfree(dir_path);
491 wfree(file);
493 /* If icon exists, exit */
494 if (access(path, F_OK) == 0)
495 return path;
497 if (wwin->net_icon_image)
498 image = RRetainImage(wwin->net_icon_image);
499 else
500 image = get_wwindow_image_from_wmhints(wwin, icon);
502 if (!image) {
503 wfree(path);
504 return NULL;
507 if (!RSaveImage(image, path, "XPM")) {
508 wfree(path);
509 path = NULL;
512 RReleaseImage(image);
514 return path;
517 static void cycleColor(void *data)
519 WIcon *icon = (WIcon *) data;
520 WScreen *scr = icon->core->screen_ptr;
521 XGCValues gcv;
523 icon->step--;
524 gcv.dash_offset = icon->step;
525 XChangeGC(dpy, scr->icon_select_gc, GCDashOffset, &gcv);
527 XDrawRectangle(dpy, icon->core->window, scr->icon_select_gc, 0, 0,
528 icon->core->width - 1, icon->core->height - 1);
529 icon->handlerID = WMAddTimerHandler(COLOR_CYCLE_DELAY, cycleColor, icon);
532 void wIconSetHighlited(WIcon *icon, Bool flag)
534 if (icon->highlighted == flag)
535 return;
537 icon->highlighted = flag;
538 update_icon_pixmap(icon);
541 void wIconSelect(WIcon * icon)
543 WScreen *scr = icon->core->screen_ptr;
544 icon->selected = !icon->selected;
546 if (icon->selected) {
547 icon->step = 0;
548 if (!wPreferences.dont_blink)
549 icon->handlerID = WMAddTimerHandler(10, cycleColor, icon);
550 else
551 XDrawRectangle(dpy, icon->core->window, scr->icon_select_gc, 0, 0,
552 icon->core->width - 1, icon->core->height - 1);
553 } else {
554 if (icon->handlerID) {
555 WMDeleteTimerHandler(icon->handlerID);
556 icon->handlerID = NULL;
558 XClearArea(dpy, icon->core->window, 0, 0, icon->core->width, icon->core->height, True);
562 static void unset_icon_image(WIcon *icon)
564 if (icon->file) {
565 wfree(icon->file);
566 icon->file = NULL;
569 if (icon->file_image) {
570 RReleaseImage(icon->file_image);
571 icon->file_image = NULL;
575 void set_icon_image_from_image(WIcon *icon, RImage *image)
577 if (!icon)
578 return;
580 unset_icon_image(icon);
582 icon->file_image = NULL;
583 icon->file_image = image;
586 void wIconUpdate(WIcon *icon)
588 WWindow *wwin = NULL;
590 if (icon && icon->owner)
591 wwin = icon->owner;
593 if (wwin && WFLAGP(wwin, always_user_icon)) {
594 /* Forced use user_icon */
595 get_rimage_icon_from_user_icon(icon);
596 } else if (icon->icon_win != None) {
597 /* Get the Pixmap from the WIcon */
598 get_rimage_icon_from_icon_win(icon);
599 } else if (wwin && wwin->net_icon_image) {
600 /* Use _NET_WM_ICON icon */
601 get_rimage_icon_from_x11(icon);
602 } else if (wwin && wwin->wm_hints && (wwin->wm_hints->flags & IconPixmapHint)) {
603 /* Get the Pixmap from the wm_hints, else, from the user */
604 unset_icon_image(icon);
605 icon->file_image = get_rimage_icon_from_wm_hints(icon);
606 if (!icon->file_image)
607 get_rimage_icon_from_user_icon(icon);
608 } else {
609 /* Get the Pixmap from the user */
610 get_rimage_icon_from_user_icon(icon);
613 update_icon_pixmap(icon);
616 void update_icon_pixmap(WIcon *icon)
618 if (icon->pixmap != None)
619 XFreePixmap(dpy, icon->pixmap);
621 icon->pixmap = None;
623 /* Create the pixmap */
624 if (icon->file_image)
625 icon_update_pixmap(icon, icon->file_image);
627 /* If dockapp, put inside the icon */
628 if (icon->icon_win != None) {
629 /* file_image is NULL, because is docked app */
630 icon_update_pixmap(icon, NULL);
631 set_dockapp_in_icon(icon);
634 /* No pixmap, set default background */
635 if (icon->pixmap != None)
636 XSetWindowBackgroundPixmap(dpy, icon->core->window, icon->pixmap);
638 /* Paint it */
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_image = RRetainImage(icon->owner->net_icon_image);
651 static void get_rimage_icon_from_user_icon(WIcon *icon)
653 if (icon->file_image)
654 return;
656 get_rimage_icon_from_default_icon(icon);
659 static void get_rimage_icon_from_default_icon(WIcon *icon)
661 WScreen *scr = icon->core->screen_ptr;
663 /* If the icon don't have image, we should use the default image. */
664 if (!scr->def_icon_rimage)
665 scr->def_icon_rimage = get_default_image(scr);
667 /* Remove the icon image */
668 unset_icon_image(icon);
670 /* Set the new icon image */
671 icon->file_image = RRetainImage(scr->def_icon_rimage);
674 /* Get the RImage from the WIcon of the WWindow */
675 static void get_rimage_icon_from_icon_win(WIcon *icon)
677 RImage *image;
679 /* Create the new RImage */
680 image = get_window_image_from_x11(icon->icon_win);
682 /* Free the icon info */
683 unset_icon_image(icon);
685 /* Set the new info */
686 icon->file_image = image;
689 /* Set the dockapp in the WIcon */
690 static void set_dockapp_in_icon(WIcon *icon)
692 XWindowAttributes attr;
693 WScreen *scr = icon->core->screen_ptr;
694 unsigned int w, h, d;
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 the background pixmap */
703 XSetWindowBackgroundPixmap(dpy, icon->core->window, scr->icon_tile_pixmap);
705 /* Set the icon border */
706 XSetWindowBorderWidth(dpy, icon->icon_win, 0);
708 /* Put the dock application in the icon */
709 XReparentWindow(dpy, icon->icon_win, icon->core->window,
710 (wPreferences.icon_size - w) / 2,
711 (wPreferences.icon_size - h) / 2);
713 /* Show it and save */
714 XMapWindow(dpy, icon->icon_win);
715 XAddToSaveSet(dpy, icon->icon_win);
717 /* Needed to move the icon clicking on the application part */
718 if ((XGetWindowAttributes(dpy, icon->icon_win, &attr)) &&
719 (attr.all_event_masks & ButtonPressMask))
720 wHackedGrabButton(Button1, MOD_MASK, icon->core->window, True,
721 ButtonPressMask, GrabModeSync, GrabModeAsync,
722 None, wCursor[WCUR_ARROW]);
725 /* Get the RImage from the XWindow wm_hints */
726 RImage *get_rimage_icon_from_wm_hints(WIcon *icon)
728 RImage *image = NULL;
729 unsigned int w, h, d;
730 WWindow *wwin = icon->owner;
732 if (!getSize(wwin->wm_hints->icon_pixmap, &w, &h, &d)) {
733 icon->owner->wm_hints->flags &= ~IconPixmapHint;
734 return NULL;
737 image = get_wwindow_image_from_wmhints(wwin, icon);
738 if (!image)
739 return NULL;
741 /* Resize the icon to the wPreferences.icon_size size */
742 image = wIconValidateIconSize(image, wPreferences.icon_size);
744 return image;
747 /* This function updates in the screen the icon title */
748 static void update_icon_title(WIcon *icon)
750 WScreen *scr = icon->core->screen_ptr;
751 int x, l, w;
752 char *tmp;
754 /* draw the icon title */
755 if (icon->show_title && icon->icon_name != NULL) {
756 tmp = ShrinkString(scr->icon_title_font, icon->icon_name, wPreferences.icon_size - 4);
757 w = WMWidthOfString(scr->icon_title_font, tmp, l = strlen(tmp));
759 if (w > icon->core->width - 4)
760 x = (icon->core->width - 4) - w;
761 else
762 x = (icon->core->width - w) / 2;
764 WMDrawString(scr->wmscreen, icon->core->window, scr->icon_title_color,
765 scr->icon_title_font, x, 1, tmp, l);
766 wfree(tmp);
771 void wIconPaint(WIcon *icon)
773 if (!icon || !icon->core || !icon->core->screen_ptr)
774 return;
776 WScreen *scr = icon->core->screen_ptr;
778 XClearWindow(dpy, icon->core->window);
780 update_icon_title(icon);
782 if (icon->selected)
783 XDrawRectangle(dpy, icon->core->window, scr->icon_select_gc, 0, 0,
784 icon->core->width - 1, icon->core->height - 1);
787 /******************************************************************/
789 static void miniwindowExpose(WObjDescriptor * desc, XEvent * event)
791 wIconPaint(desc->parent);
794 static void miniwindowDblClick(WObjDescriptor * desc, XEvent * event)
796 WIcon *icon = desc->parent;
798 assert(icon->owner != NULL);
800 wDeiconifyWindow(icon->owner);
803 static void miniwindowMouseDown(WObjDescriptor * desc, XEvent * event)
805 WIcon *icon = desc->parent;
806 WWindow *wwin = icon->owner;
807 XEvent ev;
808 int x = wwin->icon_x, y = wwin->icon_y;
809 int dx = event->xbutton.x, dy = event->xbutton.y;
810 int grabbed = 0;
811 int clickButton = event->xbutton.button;
812 Bool hasMoved = False;
814 if (WCHECK_STATE(WSTATE_MODAL))
815 return;
817 if (IsDoubleClick(icon->core->screen_ptr, event)) {
818 miniwindowDblClick(desc, event);
819 return;
822 if (event->xbutton.button == Button1) {
823 if (event->xbutton.state & MOD_MASK)
824 wLowerFrame(icon->core);
825 else
826 wRaiseFrame(icon->core);
827 if (event->xbutton.state & ShiftMask) {
828 wIconSelect(icon);
829 wSelectWindow(icon->owner, !wwin->flags.selected);
831 } else if (event->xbutton.button == Button3) {
832 WObjDescriptor *desc;
834 OpenMiniwindowMenu(wwin, event->xbutton.x_root, event->xbutton.y_root);
836 /* allow drag select of menu */
837 desc = &wwin->screen_ptr->window_menu->menu->descriptor;
838 event->xbutton.send_event = True;
839 (*desc->handle_mousedown) (desc, event);
841 return;
844 if (XGrabPointer(dpy, icon->core->window, False, ButtonMotionMask
845 | ButtonReleaseMask | ButtonPressMask, GrabModeAsync,
846 GrabModeAsync, None, None, CurrentTime) != GrabSuccess) {
848 while (1) {
849 WMMaskEvent(dpy, PointerMotionMask | ButtonReleaseMask | ButtonPressMask
850 | ButtonMotionMask | ExposureMask, &ev);
851 switch (ev.type) {
852 case Expose:
853 WMHandleEvent(&ev);
854 break;
856 case MotionNotify:
857 hasMoved = True;
858 if (!grabbed) {
859 if (abs(dx - ev.xmotion.x) >= MOVE_THRESHOLD
860 || abs(dy - ev.xmotion.y) >= MOVE_THRESHOLD) {
861 XChangeActivePointerGrab(dpy, ButtonMotionMask
862 | ButtonReleaseMask | ButtonPressMask,
863 wCursor[WCUR_MOVE], CurrentTime);
864 grabbed = 1;
865 } else {
866 break;
869 x = ev.xmotion.x_root - dx;
870 y = ev.xmotion.y_root - dy;
871 XMoveWindow(dpy, icon->core->window, x, y);
872 break;
874 case ButtonPress:
875 break;
877 case ButtonRelease:
878 if (ev.xbutton.button != clickButton)
879 break;
881 if (wwin->icon_x != x || wwin->icon_y != y)
882 wwin->flags.icon_moved = 1;
884 XMoveWindow(dpy, icon->core->window, x, y);
886 wwin->icon_x = x;
887 wwin->icon_y = y;
888 XUngrabPointer(dpy, CurrentTime);
890 if (wPreferences.auto_arrange_icons)
891 wArrangeIcons(wwin->screen_ptr, True);
892 if (wPreferences.single_click && !hasMoved)
893 miniwindowDblClick(desc, event);
894 return;
900 void set_icon_image_from_database(WIcon *icon, char *wm_instance, char *wm_class, char *command)
902 char *file = NULL;
904 file = get_icon_filename(icon->core->screen_ptr, wm_instance, wm_class, command, False);
905 if (file) {
906 icon->file = wstrdup(file);
907 icon->file_image = get_rimage_from_file(icon->core->screen_ptr, icon->file, wPreferences.icon_size);
908 wfree(file);