wIconChangeTitle rewrited
[wmaker-crm.git] / src / icon.c
blobc6d744c69d9c5823352709d17afc6cf643973820
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);
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;
172 icon = wmalloc(sizeof(WIcon));
173 icon->core = wCoreCreateTopLevel(scr,
174 coord_x,
175 coord_y,
176 wPreferences.icon_size,
177 wPreferences.icon_size,
178 0, scr->w_depth, scr->w_visual, scr->w_colormap,
179 scr->white_pixel);
181 /* will be overriden if this is a application icon */
182 icon->core->descriptor.handle_mousedown = miniwindowMouseDown;
183 icon->core->descriptor.handle_expose = miniwindowExpose;
184 icon->core->descriptor.parent_type = WCLASS_MINIWINDOW;
185 icon->core->descriptor.parent = icon;
187 icon->core->stacking = wmalloc(sizeof(WStacking));
188 icon->core->stacking->above = NULL;
189 icon->core->stacking->under = NULL;
190 icon->core->stacking->window_level = NORMAL_ICON_LEVEL;
191 icon->core->stacking->child_of = NULL;
193 /* Icon image */
194 icon->file = NULL;
195 icon->file_image = NULL;
197 return icon;
200 void wIconDestroy(WIcon * icon)
202 WCoreWindow *core = icon->core;
203 WScreen *scr = core->screen_ptr;
205 WMRemoveNotificationObserver(icon);
207 if (icon->handlerID)
208 WMDeleteTimerHandler(icon->handlerID);
210 if (icon->icon_win) {
211 int x = 0, y = 0;
213 if (icon->owner) {
214 x = icon->owner->icon_x;
215 y = icon->owner->icon_y;
217 XUnmapWindow(dpy, icon->icon_win);
218 XReparentWindow(dpy, icon->icon_win, scr->root_win, x, y);
220 if (icon->icon_name)
221 XFree(icon->icon_name);
223 if (icon->pixmap)
224 XFreePixmap(dpy, icon->pixmap);
226 unset_icon_image(icon);
228 wCoreDestroy(icon->core);
229 wfree(icon);
232 static void drawIconTitleBackground(WScreen *scr, Pixmap pixmap, int height)
234 XFillRectangle(dpy, pixmap, scr->icon_title_texture->normal_gc, 0, 0, wPreferences.icon_size, height + 1);
235 XDrawLine(dpy, pixmap, scr->icon_title_texture->light_gc, 0, 0, wPreferences.icon_size, 0);
236 XDrawLine(dpy, pixmap, scr->icon_title_texture->light_gc, 0, 0, 0, height + 1);
237 XDrawLine(dpy, pixmap, scr->icon_title_texture->dim_gc,
238 wPreferences.icon_size - 1, 0, wPreferences.icon_size - 1, height + 1);
241 static void icon_update_pixmap(WIcon *icon, RImage *image)
243 RImage *tile;
244 Pixmap pixmap;
245 int x, y, sx, sy;
246 unsigned w, h;
247 int theight = 0;
248 WScreen *scr = icon->core->screen_ptr;
250 if (icon->tile_type == TILE_NORMAL) {
251 tile = RCloneImage(scr->icon_tile);
252 } else {
253 assert(scr->clip_tile);
254 tile = RCloneImage(scr->clip_tile);
257 if (image) {
258 w = (image->width > wPreferences.icon_size)
259 ? wPreferences.icon_size : image->width;
260 x = (wPreferences.icon_size - w) / 2;
261 sx = (image->width - w) / 2;
263 if (icon->show_title)
264 theight = WMFontHeight(scr->icon_title_font);
266 h = (image->height + theight > wPreferences.icon_size
267 ? wPreferences.icon_size - theight : image->height);
268 y = theight + (wPreferences.icon_size - theight - h) / 2;
269 sy = (image->height - h) / 2;
271 RCombineArea(tile, image, sx, sy, w, h, x, y);
274 if (icon->shadowed) {
275 RColor color;
277 color.red = scr->icon_back_texture->light.red >> 8;
278 color.green = scr->icon_back_texture->light.green >> 8;
279 color.blue = scr->icon_back_texture->light.blue >> 8;
280 color.alpha = 150; /* about 60% */
281 RClearImage(tile, &color);
284 if (icon->highlighted) {
285 RColor color;
287 color.red = color.green = color.blue = 0;
288 color.alpha = 160;
289 RLightImage(tile, &color);
292 if (!RConvertImage(scr->rcontext, tile, &pixmap))
293 wwarning(_("error rendering image:%s"), RMessageForError(RErrorCode));
295 RReleaseImage(tile);
297 /* Draw the icon's title background (without text) */
298 if (icon->show_title)
299 drawIconTitleBackground(scr, pixmap, theight);
301 icon->pixmap = pixmap;
304 void wIconChangeTitle(WIcon *icon, char *new_title)
306 if (icon->icon_name != NULL)
307 XFree(icon->icon_name);
309 icon->icon_name = new_title;
310 wIconPaint(icon);
313 RImage *wIconValidateIconSize(RImage *icon, int max_size)
315 RImage *nimage;
317 if (!icon)
318 return NULL;
320 /* We should hold "ICON_BORDER" (~2) pixels to include the icon border */
321 if (((max_size + ICON_BORDER) < icon->width) ||
322 ((max_size + ICON_BORDER) < icon->height)) {
323 if (icon->width > icon->height)
324 nimage = RScaleImage(icon, max_size - ICON_BORDER,
325 (icon->height * (max_size - ICON_BORDER) / icon->width));
326 else
327 nimage = RScaleImage(icon, (icon->width * (max_size - ICON_BORDER) / icon->height),
328 max_size - ICON_BORDER);
329 RReleaseImage(icon);
330 icon = nimage;
333 return icon;
336 Bool wIconChangeImageFile(WIcon *icon, char *file)
338 WScreen *scr = icon->core->screen_ptr;
339 char *path;
340 RImage *image = NULL;
341 int error = 0;
343 /* If no new image, don't do nothing */
344 if (!file)
345 return True;
347 /* Find the new image */
348 path = FindImage(wPreferences.icon_path, file);
349 if (path)
350 image = get_rimage_from_file(scr, path, wPreferences.icon_size);
351 else
352 error = 1;
354 /* New image! */
355 if (!error && image) {
356 /* Set the new image */
357 set_icon_image_from_image(icon, image);
358 icon->file = wstrdup(path);
359 update_icon_pixmap(icon);
360 } else {
361 error = 1;
364 if (path)
365 wfree(path);
367 return !error;
370 static char *get_name_for_wwin(WWindow *wwin)
372 return get_name_for_instance_class(wwin->wm_instance, wwin->wm_class);
375 char *get_name_for_instance_class(char *wm_instance, char *wm_class)
377 char *suffix;
378 int len;
380 if (wm_class && wm_instance) {
381 len = strlen(wm_class) + strlen(wm_instance) + 2;
382 suffix = wmalloc(len);
383 snprintf(suffix, len, "%s.%s", wm_instance, wm_class);
384 } else if (wm_class) {
385 len = strlen(wm_class) + 1;
386 suffix = wmalloc(len);
387 snprintf(suffix, len, "%s", wm_class);
388 } else if (wm_instance) {
389 len = strlen(wm_instance) + 1;
390 suffix = wmalloc(len);
391 snprintf(suffix, len, "%s", wm_instance);
392 } else {
393 return NULL;
396 return suffix;
399 static char *get_icon_cache_path(void)
401 char *prefix, *path;
402 int len, ret;
404 prefix = wusergnusteppath();
405 len = strlen(prefix) + strlen(CACHE_ICON_PATH) + 2;
406 path = wmalloc(len);
407 snprintf(path, len, "%s%s/", prefix, CACHE_ICON_PATH);
409 /* If the folder exists, exit */
410 if (access(path, F_OK) == 0)
411 return path;
413 /* Create the folder */
414 ret = wmkdirhier((const char *) path);
416 /* Exit 1 on success, 0 on failure */
417 if (ret == 1)
418 return path;
420 /* Fail */
421 wfree(path);
422 return NULL;
425 static RImage *get_wwindow_image_from_wmhints(WWindow *wwin, WIcon *icon)
427 RImage *image = NULL;
428 XWMHints *hints = wwin->wm_hints;
430 if (hints && (hints->flags & IconPixmapHint) && hints->icon_pixmap != None)
431 image = RCreateImageFromDrawable(icon->core->screen_ptr->rcontext,
432 hints->icon_pixmap,
433 (hints->flags & IconMaskHint)
434 ? hints->icon_mask : None);
436 return image;
440 * wIconStore--
441 * Stores the client supplied icon at CACHE_ICON_PATH
442 * and returns the path for that icon. Returns NULL if there is no
443 * client supplied icon or on failure.
445 * Side effects:
446 * New directories might be created.
448 char *wIconStore(WIcon * icon)
450 char *path, *dir_path, *file;
451 int len = 0;
452 RImage *image = NULL;
453 WWindow *wwin = icon->owner;
455 if (!wwin)
456 return NULL;
458 dir_path = get_icon_cache_path();
459 if (!dir_path)
460 return NULL;
462 file = get_name_for_wwin(wwin);
463 if (!file) {
464 wfree(dir_path);
465 return NULL;
468 len = strlen(dir_path) + strlen(file) + 5;
469 path = wmalloc(len);
470 snprintf(path, len, "%s%s.xpm", dir_path, file);
471 wfree(dir_path);
472 wfree(file);
474 /* If icon exists, exit */
475 if (access(path, F_OK) == 0)
476 return path;
478 if (wwin->net_icon_image)
479 image = RRetainImage(wwin->net_icon_image);
480 else
481 image = get_wwindow_image_from_wmhints(wwin, icon);
483 if (!image) {
484 wfree(path);
485 return NULL;
488 if (!RSaveImage(image, path, "XPM")) {
489 wfree(path);
490 path = NULL;
493 RReleaseImage(image);
495 return path;
498 static void cycleColor(void *data)
500 WIcon *icon = (WIcon *) data;
501 WScreen *scr = icon->core->screen_ptr;
502 XGCValues gcv;
504 icon->step--;
505 gcv.dash_offset = icon->step;
506 XChangeGC(dpy, scr->icon_select_gc, GCDashOffset, &gcv);
508 XDrawRectangle(dpy, icon->core->window, scr->icon_select_gc, 0, 0,
509 icon->core->width - 1, icon->core->height - 1);
510 icon->handlerID = WMAddTimerHandler(COLOR_CYCLE_DELAY, cycleColor, icon);
513 void wIconSetHighlited(WIcon *icon, Bool flag)
515 if (icon->highlighted == flag)
516 return;
518 icon->highlighted = flag;
519 update_icon_pixmap(icon);
522 void wIconSelect(WIcon * icon)
524 WScreen *scr = icon->core->screen_ptr;
525 icon->selected = !icon->selected;
527 if (icon->selected) {
528 icon->step = 0;
529 if (!wPreferences.dont_blink)
530 icon->handlerID = WMAddTimerHandler(10, cycleColor, icon);
531 else
532 XDrawRectangle(dpy, icon->core->window, scr->icon_select_gc, 0, 0,
533 icon->core->width - 1, icon->core->height - 1);
534 } else {
535 if (icon->handlerID) {
536 WMDeleteTimerHandler(icon->handlerID);
537 icon->handlerID = NULL;
539 XClearArea(dpy, icon->core->window, 0, 0, icon->core->width, icon->core->height, True);
543 static void unset_icon_image(WIcon *icon)
545 if (icon->file) {
546 wfree(icon->file);
547 icon->file = NULL;
550 if (icon->file_image) {
551 RReleaseImage(icon->file_image);
552 icon->file_image = NULL;
556 void set_icon_image_from_image(WIcon *icon, RImage *image)
558 if (!icon)
559 return;
561 unset_icon_image(icon);
563 icon->file_image = NULL;
564 icon->file_image = image;
567 void wIconUpdate(WIcon *icon)
569 WWindow *wwin = NULL;
571 if (icon && icon->owner)
572 wwin = icon->owner;
574 if (wwin && WFLAGP(wwin, always_user_icon)) {
575 /* Forced use user_icon */
576 get_rimage_icon_from_user_icon(icon);
577 } else if (icon->icon_win != None) {
578 /* Get the Pixmap from the WIcon */
579 get_rimage_icon_from_icon_win(icon);
580 } else if (wwin && wwin->net_icon_image) {
581 /* Use _NET_WM_ICON icon */
582 get_rimage_icon_from_x11(icon);
583 } else if (wwin && wwin->wm_hints && (wwin->wm_hints->flags & IconPixmapHint)) {
584 /* Get the Pixmap from the wm_hints, else, from the user */
585 unset_icon_image(icon);
586 icon->file_image = get_rimage_icon_from_wm_hints(icon);
587 if (!icon->file_image)
588 get_rimage_icon_from_user_icon(icon);
589 } else {
590 /* Get the Pixmap from the user */
591 get_rimage_icon_from_user_icon(icon);
594 update_icon_pixmap(icon);
597 void update_icon_pixmap(WIcon *icon)
599 if (icon->pixmap != None)
600 XFreePixmap(dpy, icon->pixmap);
602 icon->pixmap = None;
604 /* Create the pixmap */
605 if (icon->file_image)
606 icon_update_pixmap(icon, icon->file_image);
608 /* If dockapp, put inside the icon */
609 if (icon->icon_win != None) {
610 /* file_image is NULL, because is docked app */
611 icon_update_pixmap(icon, NULL);
612 set_dockapp_in_icon(icon);
615 /* No pixmap, set default background */
616 if (icon->pixmap != None)
617 XSetWindowBackgroundPixmap(dpy, icon->core->window, icon->pixmap);
619 /* Paint it */
620 wIconPaint(icon);
623 static void get_rimage_icon_from_x11(WIcon *icon)
625 /* Remove the icon image */
626 unset_icon_image(icon);
628 /* Set the new icon image */
629 icon->file_image = RRetainImage(icon->owner->net_icon_image);
632 static void get_rimage_icon_from_user_icon(WIcon *icon)
634 if (icon->file_image)
635 return;
637 get_rimage_icon_from_default_icon(icon);
640 static void get_rimage_icon_from_default_icon(WIcon *icon)
642 WScreen *scr = icon->core->screen_ptr;
644 /* If the icon don't have image, we should use the default image. */
645 if (!scr->def_icon_rimage)
646 scr->def_icon_rimage = get_default_image(scr);
648 /* Remove the icon image */
649 unset_icon_image(icon);
651 /* Set the new icon image */
652 icon->file_image = RRetainImage(scr->def_icon_rimage);
655 /* Get the RImage from the WIcon of the WWindow */
656 static void get_rimage_icon_from_icon_win(WIcon *icon)
658 RImage *image;
660 /* Create the new RImage */
661 image = get_window_image_from_x11(icon->icon_win);
663 /* Free the icon info */
664 unset_icon_image(icon);
666 /* Set the new info */
667 icon->file_image = image;
670 /* Set the dockapp in the WIcon */
671 static void set_dockapp_in_icon(WIcon *icon)
673 XWindowAttributes attr;
674 WScreen *scr = icon->core->screen_ptr;
675 unsigned int w, h, d;
677 /* Reparent the dock application to the icon */
679 /* We need the application size to center it
680 * and show in the correct position */
681 getSize(icon->icon_win, &w, &h, &d);
683 /* Set the background pixmap */
684 XSetWindowBackgroundPixmap(dpy, icon->core->window, scr->icon_tile_pixmap);
686 /* Set the icon border */
687 XSetWindowBorderWidth(dpy, icon->icon_win, 0);
689 /* Put the dock application in the icon */
690 XReparentWindow(dpy, icon->icon_win, icon->core->window,
691 (wPreferences.icon_size - w) / 2,
692 (wPreferences.icon_size - h) / 2);
694 /* Show it and save */
695 XMapWindow(dpy, icon->icon_win);
696 XAddToSaveSet(dpy, icon->icon_win);
698 /* Needed to move the icon clicking on the application part */
699 if ((XGetWindowAttributes(dpy, icon->icon_win, &attr)) &&
700 (attr.all_event_masks & ButtonPressMask))
701 wHackedGrabButton(Button1, MOD_MASK, icon->core->window, True,
702 ButtonPressMask, GrabModeSync, GrabModeAsync,
703 None, wCursor[WCUR_ARROW]);
706 /* Get the RImage from the XWindow wm_hints */
707 RImage *get_rimage_icon_from_wm_hints(WIcon *icon)
709 RImage *image = NULL;
710 unsigned int w, h, d;
711 WWindow *wwin = icon->owner;
713 if (!getSize(wwin->wm_hints->icon_pixmap, &w, &h, &d)) {
714 icon->owner->wm_hints->flags &= ~IconPixmapHint;
715 return NULL;
718 image = get_wwindow_image_from_wmhints(wwin, icon);
719 if (!image)
720 return NULL;
722 /* Resize the icon to the wPreferences.icon_size size */
723 image = wIconValidateIconSize(image, wPreferences.icon_size);
725 return image;
728 /* This function updates in the screen the icon title */
729 static void update_icon_title(WIcon *icon)
731 WScreen *scr = icon->core->screen_ptr;
732 int x, l, w;
733 char *tmp;
735 /* draw the icon title */
736 if (icon->show_title && icon->icon_name != NULL) {
737 tmp = ShrinkString(scr->icon_title_font, icon->icon_name, wPreferences.icon_size - 4);
738 w = WMWidthOfString(scr->icon_title_font, tmp, l = strlen(tmp));
740 if (w > icon->core->width - 4)
741 x = (icon->core->width - 4) - w;
742 else
743 x = (icon->core->width - w) / 2;
745 WMDrawString(scr->wmscreen, icon->core->window, scr->icon_title_color,
746 scr->icon_title_font, x, 1, tmp, l);
747 wfree(tmp);
752 void wIconPaint(WIcon *icon)
754 WScreen *scr = icon->core->screen_ptr;
756 XClearWindow(dpy, icon->core->window);
758 update_icon_title(icon);
760 if (icon->selected)
761 XDrawRectangle(dpy, icon->core->window, scr->icon_select_gc, 0, 0,
762 icon->core->width - 1, icon->core->height - 1);
765 /******************************************************************/
767 static void miniwindowExpose(WObjDescriptor * desc, XEvent * event)
769 wIconPaint(desc->parent);
772 static void miniwindowDblClick(WObjDescriptor * desc, XEvent * event)
774 WIcon *icon = desc->parent;
776 assert(icon->owner != NULL);
778 wDeiconifyWindow(icon->owner);
781 static void miniwindowMouseDown(WObjDescriptor * desc, XEvent * event)
783 WIcon *icon = desc->parent;
784 WWindow *wwin = icon->owner;
785 XEvent ev;
786 int x = wwin->icon_x, y = wwin->icon_y;
787 int dx = event->xbutton.x, dy = event->xbutton.y;
788 int grabbed = 0;
789 int clickButton = event->xbutton.button;
790 Bool hasMoved = False;
792 if (WCHECK_STATE(WSTATE_MODAL))
793 return;
795 if (IsDoubleClick(icon->core->screen_ptr, event)) {
796 miniwindowDblClick(desc, event);
797 return;
800 if (event->xbutton.button == Button1) {
801 if (event->xbutton.state & MOD_MASK)
802 wLowerFrame(icon->core);
803 else
804 wRaiseFrame(icon->core);
805 if (event->xbutton.state & ShiftMask) {
806 wIconSelect(icon);
807 wSelectWindow(icon->owner, !wwin->flags.selected);
809 } else if (event->xbutton.button == Button3) {
810 WObjDescriptor *desc;
812 OpenMiniwindowMenu(wwin, event->xbutton.x_root, event->xbutton.y_root);
814 /* allow drag select of menu */
815 desc = &wwin->screen_ptr->window_menu->menu->descriptor;
816 event->xbutton.send_event = True;
817 (*desc->handle_mousedown) (desc, event);
819 return;
822 if (XGrabPointer(dpy, icon->core->window, False, ButtonMotionMask
823 | ButtonReleaseMask | ButtonPressMask, GrabModeAsync,
824 GrabModeAsync, None, None, CurrentTime) != GrabSuccess) {
826 while (1) {
827 WMMaskEvent(dpy, PointerMotionMask | ButtonReleaseMask | ButtonPressMask
828 | ButtonMotionMask | ExposureMask, &ev);
829 switch (ev.type) {
830 case Expose:
831 WMHandleEvent(&ev);
832 break;
834 case MotionNotify:
835 hasMoved = True;
836 if (!grabbed) {
837 if (abs(dx - ev.xmotion.x) >= MOVE_THRESHOLD
838 || abs(dy - ev.xmotion.y) >= MOVE_THRESHOLD) {
839 XChangeActivePointerGrab(dpy, ButtonMotionMask
840 | ButtonReleaseMask | ButtonPressMask,
841 wCursor[WCUR_MOVE], CurrentTime);
842 grabbed = 1;
843 } else {
844 break;
847 x = ev.xmotion.x_root - dx;
848 y = ev.xmotion.y_root - dy;
849 XMoveWindow(dpy, icon->core->window, x, y);
850 break;
852 case ButtonPress:
853 break;
855 case ButtonRelease:
856 if (ev.xbutton.button != clickButton)
857 break;
859 if (wwin->icon_x != x || wwin->icon_y != y)
860 wwin->flags.icon_moved = 1;
862 XMoveWindow(dpy, icon->core->window, x, y);
864 wwin->icon_x = x;
865 wwin->icon_y = y;
866 XUngrabPointer(dpy, CurrentTime);
868 if (wPreferences.auto_arrange_icons)
869 wArrangeIcons(wwin->screen_ptr, True);
870 if (wPreferences.single_click && !hasMoved)
871 miniwindowDblClick(desc, event);
872 return;
878 void set_icon_image_from_database(WIcon *icon, char *wm_instance, char *wm_class, char *command)
880 char *file = NULL;
882 file = get_icon_filename(icon->core->screen_ptr, wm_instance, wm_class, command, False);
883 if (file) {
884 icon->file = wstrdup(file);
885 icon->file_image = get_rimage_from_file(icon->core->screen_ptr, icon->file, wPreferences.icon_size);
886 wfree(file);
889 wIconUpdate(icon);