Better default icon management
[wmaker-crm.git] / src / icon.c
blobb0da0d8d2f9aa3935c25fd2c39bb52c2a480dd14
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 void get_pixmap_icon_from_user_icon(WIcon *icon);
67 static Pixmap makeIcon(WScreen *scr, RImage *image,
68 int titled, int shadowed,
69 int tileType, int highlighted);
70 static void icon_update_pixmap(WIcon *icon, RImage *image);
72 static RImage *get_default_image(WScreen *scr);
73 /****** Notification Observers ******/
75 static void appearanceObserver(void *self, WMNotification * notif)
77 WIcon *icon = (WIcon *) self;
78 uintptr_t flags = (uintptr_t)WMGetNotificationClientData(notif);
80 if ((flags & WTextureSettings) || (flags & WFontSettings))
81 icon->force_paint = 1;
83 wIconPaint(icon);
85 /* so that the appicon expose handlers will paint the appicon specific
86 * stuff */
87 XClearArea(dpy, icon->core->window, 0, 0, icon->core->width, icon->core->height, True);
90 static void tileObserver(void *self, WMNotification * notif)
92 WIcon *icon = (WIcon *) self;
94 icon->force_paint = 1;
95 wIconPaint(icon);
97 XClearArea(dpy, icon->core->window, 0, 0, 1, 1, True);
100 /************************************/
102 INLINE static void getSize(Drawable d, unsigned int *w, unsigned int *h, unsigned int *dep)
104 Window rjunk;
105 int xjunk, yjunk;
106 unsigned int bjunk;
108 XGetGeometry(dpy, d, &rjunk, &xjunk, &yjunk, w, h, &bjunk, dep);
111 WIcon *icon_create_for_wwindow(WWindow *wwin)
113 WScreen *scr = wwin->screen_ptr;
114 WIcon *icon;
115 char *file;
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 /* Get the application icon, default included */
144 file = get_default_icon_filename(scr, wwin->wm_instance, wwin->wm_class, NULL, True);
145 if (file) {
146 icon->file = wstrdup(file);
147 icon->file_image = get_default_icon_rimage(scr, icon->file, wPreferences.icon_size);
148 wfree(file);
151 icon->tile_type = TILE_NORMAL;
153 wIconUpdate(icon);
155 WMAddNotificationObserver(appearanceObserver, icon, WNIconAppearanceSettingsChanged, icon);
156 WMAddNotificationObserver(tileObserver, icon, WNIconTileSettingsChanged, icon);
158 return icon;
161 WIcon *icon_create_for_dock(WScreen *scr, char *command, char *wm_instance, char *wm_class, int tile)
163 WIcon *icon;
164 char *file = NULL;
166 icon = icon_create_core(scr, 0, 0);
168 /* Search the icon using instance and class, without default icon */
169 file = get_default_icon_filename(scr, wm_instance, wm_class, command, False);
170 if (file) {
171 icon->file = wstrdup(file);
172 icon->file_image = get_default_icon_rimage(scr, icon->file, wPreferences.icon_size);
173 wfree(file);
176 icon->tile_type = tile;
178 wIconUpdate(icon);
180 WMAddNotificationObserver(appearanceObserver, icon, WNIconAppearanceSettingsChanged, icon);
181 WMAddNotificationObserver(tileObserver, icon, WNIconTileSettingsChanged, icon);
183 return icon;
186 static WIcon *icon_create_core(WScreen *scr, int coord_x, int coord_y)
188 WIcon *icon;
189 unsigned long vmask = 0;
190 XSetWindowAttributes attribs;
192 icon = wmalloc(sizeof(WIcon));
193 icon->core = wCoreCreateTopLevel(scr,
194 coord_x,
195 coord_y,
196 wPreferences.icon_size,
197 wPreferences.icon_size,
198 0, scr->w_depth, scr->w_visual, scr->w_colormap);
200 if (wPreferences.use_saveunders) {
201 vmask = CWSaveUnder;
202 attribs.save_under = True;
205 /* a white border for selecting it */
206 vmask |= CWBorderPixel;
207 attribs.border_pixel = scr->white_pixel;
209 XChangeWindowAttributes(dpy, icon->core->window, vmask, &attribs);
211 /* will be overriden if this is a application icon */
212 icon->core->descriptor.handle_mousedown = miniwindowMouseDown;
213 icon->core->descriptor.handle_expose = miniwindowExpose;
214 icon->core->descriptor.parent_type = WCLASS_MINIWINDOW;
215 icon->core->descriptor.parent = icon;
217 icon->core->stacking = wmalloc(sizeof(WStacking));
218 icon->core->stacking->above = NULL;
219 icon->core->stacking->under = NULL;
220 icon->core->stacking->window_level = NORMAL_ICON_LEVEL;
221 icon->core->stacking->child_of = NULL;
223 /* Icon image */
224 icon->file = NULL;
225 icon->file_image = NULL;
227 return icon;
230 void wIconDestroy(WIcon * icon)
232 WCoreWindow *core = icon->core;
233 WScreen *scr = core->screen_ptr;
235 WMRemoveNotificationObserver(icon);
237 if (icon->handlerID)
238 WMDeleteTimerHandler(icon->handlerID);
240 if (icon->icon_win) {
241 int x = 0, y = 0;
243 if (icon->owner) {
244 x = icon->owner->icon_x;
245 y = icon->owner->icon_y;
247 XUnmapWindow(dpy, icon->icon_win);
248 XReparentWindow(dpy, icon->icon_win, scr->root_win, x, y);
250 if (icon->icon_name)
251 XFree(icon->icon_name);
253 if (icon->pixmap)
254 XFreePixmap(dpy, icon->pixmap);
256 if (icon->file)
257 wfree(icon->file);
259 if (icon->file_image != NULL)
260 RReleaseImage(icon->file_image);
262 wCoreDestroy(icon->core);
263 wfree(icon);
266 static void drawIconTitle(WScreen * scr, Pixmap pixmap, int height)
268 XFillRectangle(dpy, pixmap, scr->icon_title_texture->normal_gc, 0, 0, wPreferences.icon_size, height + 1);
269 XDrawLine(dpy, pixmap, scr->icon_title_texture->light_gc, 0, 0, wPreferences.icon_size, 0);
270 XDrawLine(dpy, pixmap, scr->icon_title_texture->light_gc, 0, 0, 0, height + 1);
271 XDrawLine(dpy, pixmap, scr->icon_title_texture->dim_gc,
272 wPreferences.icon_size - 1, 0, wPreferences.icon_size - 1, height + 1);
275 static void icon_update_pixmap(WIcon *icon, RImage *image)
277 WScreen *scr = icon->core->screen_ptr;
279 /* Update the WIcon pixmap */
280 icon->pixmap = makeIcon(scr, image,
281 icon->show_title, icon->shadowed,
282 icon->tile_type, icon->highlighted);
285 static Pixmap makeIcon(WScreen *scr, RImage *image, int titled, int shadowed, int tileType, int highlighted)
287 RImage *tile;
288 Pixmap pixmap;
289 int x, y, sx, sy;
290 unsigned w, h;
291 int theight = 0;
293 if (tileType == TILE_NORMAL) {
294 tile = RCloneImage(scr->icon_tile);
295 } else {
296 assert(scr->clip_tile);
297 tile = RCloneImage(scr->clip_tile);
300 if (image) {
301 w = (image->width > wPreferences.icon_size)
302 ? wPreferences.icon_size : image->width;
303 x = (wPreferences.icon_size - w) / 2;
304 sx = (image->width - w) / 2;
306 if (titled)
307 theight = WMFontHeight(scr->icon_title_font);
309 h = (image->height + theight > wPreferences.icon_size
310 ? wPreferences.icon_size - theight : image->height);
311 y = theight + (wPreferences.icon_size - theight - h) / 2;
312 sy = (image->height - h) / 2;
314 RCombineArea(tile, image, sx, sy, w, h, x, y);
317 if (shadowed) {
318 RColor color;
320 color.red = scr->icon_back_texture->light.red >> 8;
321 color.green = scr->icon_back_texture->light.green >> 8;
322 color.blue = scr->icon_back_texture->light.blue >> 8;
323 color.alpha = 150; /* about 60% */
324 RClearImage(tile, &color);
327 if (highlighted) {
328 RColor color;
330 color.red = color.green = color.blue = 0;
331 color.alpha = 160;
332 RLightImage(tile, &color);
335 if (!RConvertImage(scr->rcontext, tile, &pixmap))
336 wwarning(_("error rendering image:%s"), RMessageForError(RErrorCode));
338 RReleaseImage(tile);
340 if (titled)
341 drawIconTitle(scr, pixmap, theight);
343 return pixmap;
346 void wIconChangeTitle(WIcon * icon, char *new_title)
348 int changed;
350 changed = (new_title == NULL && icon->icon_name != NULL)
351 || (new_title != NULL && icon->icon_name == NULL);
353 if (icon->icon_name != NULL)
354 XFree(icon->icon_name);
356 icon->icon_name = new_title;
358 if (changed)
359 icon->force_paint = 1;
360 wIconPaint(icon);
363 RImage *wIconValidateIconSize(RImage *icon, int max_size)
365 RImage *nimage;
367 if (!icon)
368 return NULL;
370 /* We should hold "ICON_BORDER" (~2) pixels to include the icon border */
371 if ((icon->width - max_size) > -ICON_BORDER ||
372 (icon->height - max_size) > -ICON_BORDER) {
373 nimage = RScaleImage(icon, max_size - ICON_BORDER,
374 (icon->height * (max_size - ICON_BORDER) / icon->width));
375 RReleaseImage(icon);
376 icon = nimage;
379 return icon;
382 Bool wIconChangeImageFile(WIcon * icon, char *file)
384 WScreen *scr = icon->core->screen_ptr;
385 RImage *image;
386 char *path;
387 int error = 0;
389 if (icon->file_image) {
390 RReleaseImage(icon->file_image);
391 icon->file_image = NULL;
394 if (!file) {
395 wIconUpdate(icon);
396 return True;
399 path = FindImage(wPreferences.icon_path, file);
401 if (path && (image = RLoadImage(scr->rcontext, path, 0))) {
402 icon->file_image = wIconValidateIconSize(image, wPreferences.icon_size);
403 wIconUpdate(icon);
404 } else {
405 error = 1;
408 if (path)
409 wfree(path);
411 return !error;
414 static char *get_name_for_wwin(WWindow *wwin)
416 return get_name_for_instance_class(wwin->wm_instance, wwin->wm_class);
419 char *get_name_for_instance_class(char *wm_instance, char *wm_class)
421 char *suffix;
422 int len;
424 if (wm_class && wm_instance) {
425 len = strlen(wm_class) + strlen(wm_instance) + 2;
426 suffix = wmalloc(len);
427 snprintf(suffix, len, "%s.%s", wm_instance, wm_class);
428 } else if (wm_class) {
429 len = strlen(wm_class) + 1;
430 suffix = wmalloc(len);
431 snprintf(suffix, len, "%s", wm_class);
432 } else if (wm_instance) {
433 len = strlen(wm_instance) + 1;
434 suffix = wmalloc(len);
435 snprintf(suffix, len, "%s", wm_instance);
436 } else {
437 return NULL;
440 return suffix;
443 static char *get_icon_cache_path(void)
445 char *prefix, *path;
446 int len, ret;
448 prefix = wusergnusteppath();
449 len = strlen(prefix) + strlen(CACHE_ICON_PATH) + 2;
450 path = wmalloc(len);
451 snprintf(path, len, "%s%s/", prefix, CACHE_ICON_PATH);
453 /* If the folder exists, exit */
454 if (access(path, F_OK) == 0)
455 return path;
457 /* Create the folder */
458 ret = wmkdirhier((const char *) path);
460 /* Exit 1 on success, 0 on failure */
461 if (ret == 1)
462 return path;
464 /* Fail */
465 wfree(path);
466 return NULL;
469 static RImage *get_wwindow_image_from_wmhints(WWindow *wwin, WIcon *icon)
471 RImage *image = NULL;
472 XWMHints *hints = wwin->wm_hints;
474 if (hints && (hints->flags & IconPixmapHint) && hints->icon_pixmap != None)
475 image = RCreateImageFromDrawable(icon->core->screen_ptr->rcontext,
476 hints->icon_pixmap,
477 (hints->flags & IconMaskHint)
478 ? hints->icon_mask : None);
480 return image;
484 * wIconStore--
485 * Stores the client supplied icon at CACHE_ICON_PATH
486 * and returns the path for that icon. Returns NULL if there is no
487 * client supplied icon or on failure.
489 * Side effects:
490 * New directories might be created.
492 char *wIconStore(WIcon * icon)
494 char *path, *dir_path, *file;
495 int len = 0;
496 RImage *image = NULL;
497 WWindow *wwin = icon->owner;
499 if (!wwin)
500 return NULL;
502 dir_path = get_icon_cache_path();
503 if (!dir_path)
504 return NULL;
506 file = get_name_for_wwin(wwin);
507 if (!file) {
508 wfree(dir_path);
509 return NULL;
512 len = strlen(dir_path) + strlen(file) + 5;
513 path = wmalloc(len);
514 snprintf(path, len, "%s%s.xpm", dir_path, file);
515 wfree(dir_path);
516 wfree(file);
518 /* If icon exists, exit */
519 if (access(path, F_OK) == 0)
520 return path;
522 if (wwin->net_icon_image)
523 image = RRetainImage(wwin->net_icon_image);
524 else
525 image = get_wwindow_image_from_wmhints(wwin, icon);
527 if (!image) {
528 wfree(path);
529 return NULL;
532 if (!RSaveImage(image, path, "XPM")) {
533 wfree(path);
534 path = NULL;
537 RReleaseImage(image);
539 return path;
542 static void cycleColor(void *data)
544 WIcon *icon = (WIcon *) data;
545 WScreen *scr = icon->core->screen_ptr;
546 XGCValues gcv;
548 icon->step--;
549 gcv.dash_offset = icon->step;
550 XChangeGC(dpy, scr->icon_select_gc, GCDashOffset, &gcv);
552 XDrawRectangle(dpy, icon->core->window, scr->icon_select_gc, 0, 0,
553 icon->core->width - 1, icon->core->height - 1);
554 icon->handlerID = WMAddTimerHandler(COLOR_CYCLE_DELAY, cycleColor, icon);
557 #ifdef NEWAPPICON
558 void wIconSetHighlited(WIcon *icon, Bool flag)
560 if (icon->highlighted == flag)
561 return;
563 icon->highlighted = flag;
564 icon->force_paint = True;
565 wIconPaint(icon);
567 #endif
569 void wIconSelect(WIcon * icon)
571 WScreen *scr = icon->core->screen_ptr;
572 icon->selected = !icon->selected;
574 if (icon->selected) {
575 icon->step = 0;
576 if (!wPreferences.dont_blink)
577 icon->handlerID = WMAddTimerHandler(10, cycleColor, icon);
578 else
579 XDrawRectangle(dpy, icon->core->window, scr->icon_select_gc, 0, 0,
580 icon->core->width - 1, icon->core->height - 1);
581 } else {
582 if (icon->handlerID) {
583 WMDeleteTimerHandler(icon->handlerID);
584 icon->handlerID = NULL;
586 XClearArea(dpy, icon->core->window, 0, 0, icon->core->width, icon->core->height, True);
590 void wIconUpdate(WIcon *icon)
592 WScreen *scr = icon->core->screen_ptr;
593 WWindow *wwin = icon->owner;
595 assert(scr->icon_tile != NULL);
597 if (icon->pixmap != None)
598 XFreePixmap(dpy, icon->pixmap);
600 icon->pixmap = None;
602 if (wwin && WFLAGP(wwin, always_user_icon)) {
603 /* Forced use user_icon */
604 get_pixmap_icon_from_user_icon(icon);
605 } else if (icon->icon_win != None) {
606 /* Get the Pixmap from the WIcon */
607 get_pixmap_icon_from_icon_win(icon);
608 } else if (wwin && wwin->net_icon_image) {
609 /* Use _NET_WM_ICON icon */
610 icon_update_pixmap(icon, wwin->net_icon_image);
611 } else if (wwin && wwin->wm_hints && (wwin->wm_hints->flags & IconPixmapHint)) {
612 /* Get the Pixmap from the wm_hints, else, from the user */
613 if (get_pixmap_icon_from_wm_hints(icon))
614 get_pixmap_icon_from_user_icon(icon);
615 } else {
616 /* Get the Pixmap from the user */
617 get_pixmap_icon_from_user_icon(icon);
620 /* No pixmap, set default background */
621 if (icon->pixmap != None)
622 XSetWindowBackgroundPixmap(dpy, icon->core->window, icon->pixmap);
624 /* Paint it */
625 XClearWindow(dpy, icon->core->window);
626 wIconPaint(icon);
629 static void get_pixmap_icon_from_user_icon(WIcon *icon)
631 WScreen *scr = icon->core->screen_ptr;
633 /* If the icon has image, update it and continue */
634 if (icon->file_image) {
635 icon_update_pixmap(icon, icon->file_image);
636 return;
639 /* If the icon don't have image, we should use the default image. */
640 if (!scr->def_icon_rimage)
641 scr->def_icon_rimage = get_default_image(scr);
643 /* Now, create the pixmap using the default (saved) image */
644 icon_update_pixmap(icon, scr->def_icon_rimage);
647 /* This function creates the RImage using the default icon */
648 static RImage *get_default_image(WScreen *scr)
650 RImage *image = NULL;
651 char *path, *file;
653 /* Get the default icon */
654 file = wDefaultGetIconFile(NULL, NULL, True);
655 if (file) {
656 path = FindImage(wPreferences.icon_path, file);
657 if (path) {
658 image = RLoadImage(scr->rcontext, path, 0);
659 if (!image)
660 wwarning(_("could not load default icon \"%s\":%s"),
661 file, RMessageForError(RErrorCode));
662 wfree(path);
663 } else {
664 wwarning(_("could not find default icon \"%s\""), file);
668 /* Validate the icon size */
669 image = wIconValidateIconSize(image, wPreferences.icon_size);
671 return image;
674 /* Get the Pixmap from the WIcon of the WWindow */
675 static void get_pixmap_icon_from_icon_win(WIcon * icon)
677 XWindowAttributes attr;
678 WScreen *scr = icon->core->screen_ptr;
679 int title_height = WMFontHeight(scr->icon_title_font);
680 unsigned int width, height, depth;
681 int theight;
682 int resize = 0;
683 Pixmap pixmap;
685 getSize(icon->icon_win, &width, &height, &depth);
687 if (width > wPreferences.icon_size) {
688 resize = 1;
689 width = wPreferences.icon_size;
692 if (height > wPreferences.icon_size) {
693 resize = 1;
694 height = wPreferences.icon_size;
697 if (icon->show_title && (height + title_height < wPreferences.icon_size)) {
698 pixmap = XCreatePixmap(dpy, scr->w_win, wPreferences.icon_size,
699 wPreferences.icon_size, scr->w_depth);
700 XSetClipMask(dpy, scr->copy_gc, None);
701 XCopyArea(dpy, scr->icon_tile_pixmap, pixmap, scr->copy_gc, 0, 0,
702 wPreferences.icon_size, wPreferences.icon_size, 0, 0);
703 drawIconTitle(scr, pixmap, title_height);
704 theight = title_height;
705 } else {
706 pixmap = None;
707 theight = 0;
708 XSetWindowBackgroundPixmap(dpy, icon->core->window, scr->icon_tile_pixmap);
711 XSetWindowBorderWidth(dpy, icon->icon_win, 0);
712 XReparentWindow(dpy, icon->icon_win, icon->core->window,
713 (wPreferences.icon_size - width) / 2,
714 theight + (wPreferences.icon_size - height - theight) / 2);
715 if (resize)
716 XResizeWindow(dpy, icon->icon_win, width, height);
718 XMapWindow(dpy, icon->icon_win);
719 XAddToSaveSet(dpy, icon->icon_win);
721 /* Save it */
722 icon->pixmap = pixmap;
724 if ((XGetWindowAttributes(dpy, icon->icon_win, &attr)) &&
725 (attr.all_event_masks & ButtonPressMask))
726 wHackedGrabButton(Button1, MOD_MASK, icon->core->window, True,
727 ButtonPressMask, GrabModeSync, GrabModeAsync,
728 None, wCursor[WCUR_ARROW]);
731 /* Get the Pixmap from the XWindow wm_hints */
732 static int get_pixmap_icon_from_wm_hints(WIcon *icon)
734 Window jw;
735 Pixmap pixmap;
736 unsigned int w, h, ju, d;
737 int ji, x, y;
738 WWindow *wwin = icon->owner;
739 WScreen *scr = icon->core->screen_ptr;
740 int title_height = WMFontHeight(scr->icon_title_font);
742 if (!XGetGeometry(dpy, wwin->wm_hints->icon_pixmap, &jw, &ji, &ji, &w, &h, &ju, &d)) {
743 icon->owner->wm_hints->flags &= ~IconPixmapHint;
744 return 1;
747 pixmap = XCreatePixmap(dpy, icon->core->window, wPreferences.icon_size,
748 wPreferences.icon_size, scr->w_depth);
749 XSetClipMask(dpy, scr->copy_gc, None);
750 XCopyArea(dpy, scr->icon_tile_pixmap, pixmap, scr->copy_gc, 0, 0,
751 wPreferences.icon_size, wPreferences.icon_size, 0, 0);
753 if (w > wPreferences.icon_size)
754 w = wPreferences.icon_size;
755 x = (wPreferences.icon_size - w) / 2;
757 if (icon->show_title && (title_height < wPreferences.icon_size)) {
758 drawIconTitle(scr, pixmap, title_height);
760 if (h > wPreferences.icon_size - title_height - 2) {
761 h = wPreferences.icon_size - title_height - 2;
762 y = title_height + 1;
763 } else {
764 y = (wPreferences.icon_size - h - title_height) / 2 + title_height + 1;
766 } else {
767 if (w > wPreferences.icon_size)
768 w = wPreferences.icon_size;
769 y = (wPreferences.icon_size - h) / 2;
772 if (wwin->wm_hints->flags & IconMaskHint)
773 XSetClipMask(dpy, scr->copy_gc, wwin->wm_hints->icon_mask);
775 XSetClipOrigin(dpy, scr->copy_gc, x, y);
777 if (d != scr->w_depth) {
778 XSetForeground(dpy, scr->copy_gc, scr->black_pixel);
779 XSetBackground(dpy, scr->copy_gc, scr->white_pixel);
780 XCopyPlane(dpy, wwin->wm_hints->icon_pixmap, pixmap, scr->copy_gc, 0, 0, w, h, x, y, 1);
781 } else {
782 XCopyArea(dpy, wwin->wm_hints->icon_pixmap, pixmap, scr->copy_gc, 0, 0, w, h, x, y);
785 XSetClipOrigin(dpy, scr->copy_gc, 0, 0);
787 icon->pixmap = pixmap;
788 return (0);
791 void wIconPaint(WIcon * icon)
793 WScreen *scr = icon->core->screen_ptr;
794 int x;
795 char *tmp;
797 if (icon->force_paint) {
798 icon->force_paint = 0;
799 wIconUpdate(icon);
800 return;
803 XClearWindow(dpy, icon->core->window);
805 /* draw the icon title */
806 if (icon->show_title && icon->icon_name != NULL) {
807 int l;
808 int w;
810 tmp = ShrinkString(scr->icon_title_font, icon->icon_name, wPreferences.icon_size - 4);
811 w = WMWidthOfString(scr->icon_title_font, tmp, l = strlen(tmp));
813 if (w > icon->core->width - 4)
814 x = (icon->core->width - 4) - w;
815 else
816 x = (icon->core->width - w) / 2;
818 WMDrawString(scr->wmscreen, icon->core->window, scr->icon_title_color,
819 scr->icon_title_font, x, 1, tmp, l);
820 wfree(tmp);
823 if (icon->selected)
824 XDrawRectangle(dpy, icon->core->window, scr->icon_select_gc, 0, 0,
825 icon->core->width - 1, icon->core->height - 1);
828 /******************************************************************/
830 static void miniwindowExpose(WObjDescriptor * desc, XEvent * event)
832 wIconPaint(desc->parent);
835 static void miniwindowDblClick(WObjDescriptor * desc, XEvent * event)
837 WIcon *icon = desc->parent;
839 assert(icon->owner != NULL);
841 wDeiconifyWindow(icon->owner);
844 static void miniwindowMouseDown(WObjDescriptor * desc, XEvent * event)
846 WIcon *icon = desc->parent;
847 WWindow *wwin = icon->owner;
848 XEvent ev;
849 int x = wwin->icon_x, y = wwin->icon_y;
850 int dx = event->xbutton.x, dy = event->xbutton.y;
851 int grabbed = 0;
852 int clickButton = event->xbutton.button;
853 Bool hasMoved = False;
855 if (WCHECK_STATE(WSTATE_MODAL))
856 return;
858 if (IsDoubleClick(icon->core->screen_ptr, event)) {
859 miniwindowDblClick(desc, event);
860 return;
863 if (event->xbutton.button == Button1) {
864 if (event->xbutton.state & MOD_MASK)
865 wLowerFrame(icon->core);
866 else
867 wRaiseFrame(icon->core);
868 if (event->xbutton.state & ShiftMask) {
869 wIconSelect(icon);
870 wSelectWindow(icon->owner, !wwin->flags.selected);
872 } else if (event->xbutton.button == Button3) {
873 WObjDescriptor *desc;
875 OpenMiniwindowMenu(wwin, event->xbutton.x_root, event->xbutton.y_root);
877 /* allow drag select of menu */
878 desc = &wwin->screen_ptr->window_menu->menu->descriptor;
879 event->xbutton.send_event = True;
880 (*desc->handle_mousedown) (desc, event);
882 return;
885 if (XGrabPointer(dpy, icon->core->window, False, ButtonMotionMask
886 | ButtonReleaseMask | ButtonPressMask, GrabModeAsync,
887 GrabModeAsync, None, None, CurrentTime) != GrabSuccess) {
889 while (1) {
890 WMMaskEvent(dpy, PointerMotionMask | ButtonReleaseMask | ButtonPressMask
891 | ButtonMotionMask | ExposureMask, &ev);
892 switch (ev.type) {
893 case Expose:
894 WMHandleEvent(&ev);
895 break;
897 case MotionNotify:
898 hasMoved = True;
899 if (!grabbed) {
900 if (abs(dx - ev.xmotion.x) >= MOVE_THRESHOLD
901 || abs(dy - ev.xmotion.y) >= MOVE_THRESHOLD) {
902 XChangeActivePointerGrab(dpy, ButtonMotionMask
903 | ButtonReleaseMask | ButtonPressMask,
904 wCursor[WCUR_MOVE], CurrentTime);
905 grabbed = 1;
906 } else {
907 break;
910 x = ev.xmotion.x_root - dx;
911 y = ev.xmotion.y_root - dy;
912 XMoveWindow(dpy, icon->core->window, x, y);
913 break;
915 case ButtonPress:
916 break;
918 case ButtonRelease:
919 if (ev.xbutton.button != clickButton)
920 break;
922 if (wwin->icon_x != x || wwin->icon_y != y)
923 wwin->flags.icon_moved = 1;
925 XMoveWindow(dpy, icon->core->window, x, y);
927 wwin->icon_x = x;
928 wwin->icon_y = y;
929 XUngrabPointer(dpy, CurrentTime);
931 if (wPreferences.auto_arrange_icons)
932 wArrangeIcons(wwin->screen_ptr, True);
933 if (wPreferences.single_click && !hasMoved)
934 miniwindowDblClick(desc, event);
935 return;