d472553d4491788fd4d807ddd811379a55cbc8bd
[wmaker-crm.git] / src / icon.c
blobd472553d4491788fd4d807ddd811379a55cbc8bd
1 /* icon.c - window icon and dock and appicon parent
3 * Window Maker window manager
5 * Copyright (c) 1997-2003 Alfredo K. Kojima
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License along
18 * with this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22 #include "wconfig.h"
24 #include <X11/Xlib.h>
25 #include <X11/Xutil.h>
26 #include <stdlib.h>
27 #include <stdio.h>
28 #include <stdint.h>
29 #include <string.h>
30 #include <unistd.h>
31 #include <ctype.h>
32 #include <wraster.h>
33 #include <sys/stat.h>
35 #include "WindowMaker.h"
36 #include "wcore.h"
37 #include "texture.h"
38 #include "window.h"
39 #include "icon.h"
40 #include "actions.h"
41 #include "funcs.h"
42 #include "stacking.h"
43 #include "application.h"
44 #include "defaults.h"
45 #include "appicon.h"
46 #include "wmspec.h"
48 /**** Global varianebles ****/
49 extern WPreferences wPreferences;
51 #define MOD_MASK wPreferences.modifier_mask
52 #define CACHE_ICON_PATH "/Library/WindowMaker/CachedPixmaps"
53 #define ICON_BORDER 3
55 extern Cursor wCursor[WCUR_LAST];
57 static void miniwindowExpose(WObjDescriptor * desc, XEvent * event);
58 static void miniwindowMouseDown(WObjDescriptor * desc, XEvent * event);
59 static void miniwindowDblClick(WObjDescriptor * desc, XEvent * event);
61 static WIcon *icon_create_core(WScreen *scr, int coord_x, int coord_y);
63 static void get_pixmap_icon_from_icon_win(WIcon *icon);
64 static int get_pixmap_icon_from_wm_hints(WIcon *icon);
65 static int get_rimage_icon_from_wm_hints(WIcon *icon);
66 static void get_pixmap_icon_from_user_icon(WIcon *icon);
67 static void get_rimage_icon_from_user_icon(WIcon *icon);
68 static void get_pixmap_icon_from_default_icon(WIcon *icon);
69 static void get_rimage_icon_from_default_icon(WIcon *icon);
70 static void get_pixmap_icon_from_x11(WIcon *icon);
71 static void get_rimage_icon_from_x11(WIcon *icon);
73 static void icon_update_pixmap(WIcon *icon, RImage *image);
74 static void unset_icon_image(WIcon *icon);
76 /****** Notification Observers ******/
78 static void appearanceObserver(void *self, WMNotification * notif)
80 WIcon *icon = (WIcon *) self;
81 uintptr_t flags = (uintptr_t)WMGetNotificationClientData(notif);
83 if ((flags & WTextureSettings) || (flags & WFontSettings))
84 icon->force_paint = 1;
86 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_default_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_default_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 icon->force_paint = True;
561 wIconPaint(icon);
563 #endif
565 void wIconSelect(WIcon * icon)
567 WScreen *scr = icon->core->screen_ptr;
568 icon->selected = !icon->selected;
570 if (icon->selected) {
571 icon->step = 0;
572 if (!wPreferences.dont_blink)
573 icon->handlerID = WMAddTimerHandler(10, cycleColor, icon);
574 else
575 XDrawRectangle(dpy, icon->core->window, scr->icon_select_gc, 0, 0,
576 icon->core->width - 1, icon->core->height - 1);
577 } else {
578 if (icon->handlerID) {
579 WMDeleteTimerHandler(icon->handlerID);
580 icon->handlerID = NULL;
582 XClearArea(dpy, icon->core->window, 0, 0, icon->core->width, icon->core->height, True);
586 static void unset_icon_image(WIcon *icon)
588 if (icon->file)
589 wfree(icon->file);
591 if (icon->file_image)
592 RReleaseImage(icon->file_image);
595 void wIconUpdate(WIcon *icon)
597 WScreen *scr = icon->core->screen_ptr;
598 WWindow *wwin = icon->owner;
600 assert(scr->icon_tile != NULL);
602 if (icon->pixmap != None)
603 XFreePixmap(dpy, icon->pixmap);
605 icon->pixmap = None;
607 if (wwin && WFLAGP(wwin, always_user_icon)) {
608 /* Forced use user_icon */
609 get_pixmap_icon_from_user_icon(icon);
610 } else if (icon->icon_win != None) {
611 /* Get the Pixmap from the WIcon */
612 get_pixmap_icon_from_icon_win(icon);
613 } else if (wwin && wwin->net_icon_image) {
614 /* Use _NET_WM_ICON icon */
615 get_pixmap_icon_from_x11(icon);
616 } else if (wwin && wwin->wm_hints && (wwin->wm_hints->flags & IconPixmapHint)) {
617 /* Get the Pixmap from the wm_hints, else, from the user */
618 if (get_pixmap_icon_from_wm_hints(icon))
619 get_pixmap_icon_from_user_icon(icon);
620 } else {
621 /* Get the Pixmap from the user */
622 get_pixmap_icon_from_user_icon(icon);
625 /* No pixmap, set default background */
626 if (icon->pixmap != None)
627 XSetWindowBackgroundPixmap(dpy, icon->core->window, icon->pixmap);
629 /* Paint it */
630 XClearWindow(dpy, icon->core->window);
631 wIconPaint(icon);
634 static void get_pixmap_icon_from_x11(WIcon *icon)
636 /* Set the icon->file_image */
637 get_rimage_icon_from_x11(icon);
639 /* Update icon->pixmap */
640 icon_update_pixmap(icon, icon->file_image);
643 static void get_rimage_icon_from_x11(WIcon *icon)
645 /* Remove the icon image */
646 unset_icon_image(icon);
648 /* Set the new icon image */
649 icon->file = NULL;
650 icon->file_image = RRetainImage(icon->owner->net_icon_image);
653 static void get_rimage_icon_from_user_icon(WIcon *icon)
655 if (icon->file_image)
656 return;
658 get_rimage_icon_from_default_icon(icon);
661 static void get_pixmap_icon_from_user_icon(WIcon *icon)
663 /* Set the icon->file_image */
664 get_rimage_icon_from_user_icon(icon);
666 /* Update icon->pixmap */
667 icon_update_pixmap(icon, icon->file_image);
670 static void get_rimage_icon_from_default_icon(WIcon *icon)
672 WScreen *scr = icon->core->screen_ptr;
674 /* If the icon don't have image, we should use the default image. */
675 if (!scr->def_icon_rimage)
676 scr->def_icon_rimage = get_default_image(scr);
678 /* Remove the icon image */
679 unset_icon_image(icon);
681 /* Set the new icon image */
682 icon->file = NULL;
683 icon->file_image = RRetainImage(scr->def_icon_rimage);
686 static void get_pixmap_icon_from_default_icon(WIcon *icon)
688 /* Update icon->file image */
689 get_rimage_icon_from_default_icon(icon);
691 /* Now, create the pixmap using the default (saved) image */
692 icon_update_pixmap(icon, icon->file_image);
695 /* Get the Pixmap from the WIcon of the WWindow */
696 static void get_pixmap_icon_from_icon_win(WIcon *icon)
698 XWindowAttributes attr;
699 RImage *image;
700 WScreen *scr = icon->core->screen_ptr;
701 int title_height = WMFontHeight(scr->icon_title_font);
702 unsigned int w, h, d;
703 int theight = 0;
705 /* Create the new RImage */
706 image = get_window_image_from_x11(icon->icon_win);
708 /* Free the icon info */
709 unset_icon_image(icon);
711 /* Set the new info */
712 icon->file = NULL;
713 icon->file_image = image;
715 /* Paint the image at the icon */
716 icon_update_pixmap(icon, image);
718 /* Reparent the dock application to the icon */
720 /* We need the application size to center it
721 * and show in the correct position */
722 getSize(icon->icon_win, &w, &h, &d);
724 /* Set extra space for title */
725 if (icon->show_title && (h + title_height < wPreferences.icon_size)) {
726 theight = title_height;
727 drawIconTitle(scr, icon->pixmap, theight);
728 } else {
729 XSetWindowBackgroundPixmap(dpy, icon->core->window, scr->icon_tile_pixmap);
732 /* Set the icon border */
733 XSetWindowBorderWidth(dpy, icon->icon_win, 0);
735 /* Put the dock application in the icon */
736 XReparentWindow(dpy, icon->icon_win, icon->core->window,
737 (wPreferences.icon_size - w) / 2,
738 theight + (wPreferences.icon_size - h - theight) / 2);
740 /* Show it and save */
741 XMapWindow(dpy, icon->icon_win);
742 XAddToSaveSet(dpy, icon->icon_win);
744 /* Needed to move the icon clicking on the application part */
745 if ((XGetWindowAttributes(dpy, icon->icon_win, &attr)) &&
746 (attr.all_event_masks & ButtonPressMask))
747 wHackedGrabButton(Button1, MOD_MASK, icon->core->window, True,
748 ButtonPressMask, GrabModeSync, GrabModeAsync,
749 None, wCursor[WCUR_ARROW]);
752 /* Get the RImage from the XWindow wm_hints */
753 static int get_rimage_icon_from_wm_hints(WIcon *icon)
755 RImage *image = NULL;
756 unsigned int w, h, d;
757 WWindow *wwin = icon->owner;
759 if (!getSize(wwin->wm_hints->icon_pixmap, &w, &h, &d)) {
760 icon->owner->wm_hints->flags &= ~IconPixmapHint;
761 return 1;
764 image = get_wwindow_image_from_wmhints(wwin, icon);
765 if (!image)
766 return 1;
768 /* FIXME: If unset_icon_image, pointer double free then crash
769 unset_icon_image(icon); */
770 icon->file_image = image;
772 return 0;
775 /* Get the Pixmap from the XWindow wm_hints */
776 static int get_pixmap_icon_from_wm_hints(WIcon *icon)
778 int ret;
780 ret = get_rimage_icon_from_wm_hints(icon);
781 if (ret == 0)
782 icon_update_pixmap(icon, icon->file_image);
784 return ret;
787 void wIconPaint(WIcon * icon)
789 WScreen *scr = icon->core->screen_ptr;
790 int x;
791 char *tmp;
793 if (icon->force_paint) {
794 icon->force_paint = 0;
795 wIconUpdate(icon);
796 return;
799 XClearWindow(dpy, icon->core->window);
801 /* draw the icon title */
802 if (icon->show_title && icon->icon_name != NULL) {
803 int l;
804 int w;
806 tmp = ShrinkString(scr->icon_title_font, icon->icon_name, wPreferences.icon_size - 4);
807 w = WMWidthOfString(scr->icon_title_font, tmp, l = strlen(tmp));
809 if (w > icon->core->width - 4)
810 x = (icon->core->width - 4) - w;
811 else
812 x = (icon->core->width - w) / 2;
814 WMDrawString(scr->wmscreen, icon->core->window, scr->icon_title_color,
815 scr->icon_title_font, x, 1, tmp, l);
816 wfree(tmp);
819 if (icon->selected)
820 XDrawRectangle(dpy, icon->core->window, scr->icon_select_gc, 0, 0,
821 icon->core->width - 1, icon->core->height - 1);
824 /******************************************************************/
826 static void miniwindowExpose(WObjDescriptor * desc, XEvent * event)
828 wIconPaint(desc->parent);
831 static void miniwindowDblClick(WObjDescriptor * desc, XEvent * event)
833 WIcon *icon = desc->parent;
835 assert(icon->owner != NULL);
837 wDeiconifyWindow(icon->owner);
840 static void miniwindowMouseDown(WObjDescriptor * desc, XEvent * event)
842 WIcon *icon = desc->parent;
843 WWindow *wwin = icon->owner;
844 XEvent ev;
845 int x = wwin->icon_x, y = wwin->icon_y;
846 int dx = event->xbutton.x, dy = event->xbutton.y;
847 int grabbed = 0;
848 int clickButton = event->xbutton.button;
849 Bool hasMoved = False;
851 if (WCHECK_STATE(WSTATE_MODAL))
852 return;
854 if (IsDoubleClick(icon->core->screen_ptr, event)) {
855 miniwindowDblClick(desc, event);
856 return;
859 if (event->xbutton.button == Button1) {
860 if (event->xbutton.state & MOD_MASK)
861 wLowerFrame(icon->core);
862 else
863 wRaiseFrame(icon->core);
864 if (event->xbutton.state & ShiftMask) {
865 wIconSelect(icon);
866 wSelectWindow(icon->owner, !wwin->flags.selected);
868 } else if (event->xbutton.button == Button3) {
869 WObjDescriptor *desc;
871 OpenMiniwindowMenu(wwin, event->xbutton.x_root, event->xbutton.y_root);
873 /* allow drag select of menu */
874 desc = &wwin->screen_ptr->window_menu->menu->descriptor;
875 event->xbutton.send_event = True;
876 (*desc->handle_mousedown) (desc, event);
878 return;
881 if (XGrabPointer(dpy, icon->core->window, False, ButtonMotionMask
882 | ButtonReleaseMask | ButtonPressMask, GrabModeAsync,
883 GrabModeAsync, None, None, CurrentTime) != GrabSuccess) {
885 while (1) {
886 WMMaskEvent(dpy, PointerMotionMask | ButtonReleaseMask | ButtonPressMask
887 | ButtonMotionMask | ExposureMask, &ev);
888 switch (ev.type) {
889 case Expose:
890 WMHandleEvent(&ev);
891 break;
893 case MotionNotify:
894 hasMoved = True;
895 if (!grabbed) {
896 if (abs(dx - ev.xmotion.x) >= MOVE_THRESHOLD
897 || abs(dy - ev.xmotion.y) >= MOVE_THRESHOLD) {
898 XChangeActivePointerGrab(dpy, ButtonMotionMask
899 | ButtonReleaseMask | ButtonPressMask,
900 wCursor[WCUR_MOVE], CurrentTime);
901 grabbed = 1;
902 } else {
903 break;
906 x = ev.xmotion.x_root - dx;
907 y = ev.xmotion.y_root - dy;
908 XMoveWindow(dpy, icon->core->window, x, y);
909 break;
911 case ButtonPress:
912 break;
914 case ButtonRelease:
915 if (ev.xbutton.button != clickButton)
916 break;
918 if (wwin->icon_x != x || wwin->icon_y != y)
919 wwin->flags.icon_moved = 1;
921 XMoveWindow(dpy, icon->core->window, x, y);
923 wwin->icon_x = x;
924 wwin->icon_y = y;
925 XUngrabPointer(dpy, CurrentTime);
927 if (wPreferences.auto_arrange_icons)
928 wArrangeIcons(wwin->screen_ptr, True);
929 if (wPreferences.single_click && !hasMoved)
930 miniwindowDblClick(desc, event);
931 return;