Better icon scale
[wmaker-crm.git] / src / icon.c
blob8772f2c96add5b9fcf1ec2d1213f051ed06c0c84
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 *wIconCreateCore(WScreen *scr, int coord_x, int coord_y);
63 void get_pixmap_icon_from_icon_win(WIcon *icon);
64 int get_pixmap_icon_from_wm_hints(WScreen *scr, WWindow *wwin, WIcon *icon);
65 void get_pixmap_icon_from_user_icon(WScreen *scr, WIcon * icon);
66 /****** Notification Observers ******/
68 static void appearanceObserver(void *self, WMNotification * notif)
70 WIcon *icon = (WIcon *) self;
71 uintptr_t flags = (uintptr_t)WMGetNotificationClientData(notif);
73 if ((flags & WTextureSettings) || (flags & WFontSettings))
74 icon->force_paint = 1;
76 wIconPaint(icon);
78 /* so that the appicon expose handlers will paint the appicon specific
79 * stuff */
80 XClearArea(dpy, icon->core->window, 0, 0, icon->core->width, icon->core->height, True);
83 static void tileObserver(void *self, WMNotification * notif)
85 WIcon *icon = (WIcon *) self;
87 icon->force_paint = 1;
88 wIconPaint(icon);
90 XClearArea(dpy, icon->core->window, 0, 0, 1, 1, True);
93 /************************************/
95 INLINE static void getSize(Drawable d, unsigned int *w, unsigned int *h, unsigned int *dep)
97 Window rjunk;
98 int xjunk, yjunk;
99 unsigned int bjunk;
101 XGetGeometry(dpy, d, &rjunk, &xjunk, &yjunk, w, h, &bjunk, dep);
104 WIcon *wIconCreate(WWindow * wwin)
106 WScreen *scr = wwin->screen_ptr;
107 WIcon *icon;
108 char *file;
110 icon = wIconCreateCore(scr, wwin->icon_x, wwin->icon_y);
112 icon->owner = wwin;
113 if (wwin->wm_hints && (wwin->wm_hints->flags & IconWindowHint)) {
114 if (wwin->client_win == wwin->main_window) {
115 WApplication *wapp;
116 /* do not let miniwindow steal app-icon's icon window */
117 wapp = wApplicationOf(wwin->client_win);
118 if (!wapp || wapp->app_icon == NULL)
119 icon->icon_win = wwin->wm_hints->icon_window;
120 } else {
121 icon->icon_win = wwin->wm_hints->icon_window;
124 #ifdef NO_MINIWINDOW_TITLES
125 icon->show_title = 0;
126 #else
127 icon->show_title = 1;
128 #endif
129 icon->file_image = wDefaultGetImage(scr, wwin->wm_instance, wwin->wm_class, wPreferences.icon_size);
131 file = wDefaultGetIconFile(wwin->wm_instance, wwin->wm_class, False);
132 if (file)
133 icon->file = wstrdup(file);
135 icon->icon_name = wNETWMGetIconName(wwin->client_win);
136 if (icon->icon_name)
137 wwin->flags.net_has_icon_title = 1;
138 else
139 wGetIconName(dpy, wwin->client_win, &icon->icon_name);
141 icon->tile_type = TILE_NORMAL;
143 wIconUpdate(icon);
145 XFlush(dpy);
147 WMAddNotificationObserver(appearanceObserver, icon, WNIconAppearanceSettingsChanged, icon);
148 WMAddNotificationObserver(tileObserver, icon, WNIconTileSettingsChanged, icon);
149 return icon;
152 WIcon *wIconCreateWithIconFile(WScreen * scr, char *iconfile, int tile)
154 WIcon *icon;
156 icon = wIconCreateCore(scr, 0, 0);
158 if (iconfile) {
159 icon->file_image = RLoadImage(scr->rcontext, iconfile, 0);
160 if (!icon->file_image)
161 wwarning(_("error loading image file \"%s\": %s"), iconfile, RMessageForError(RErrorCode));
163 icon->file_image = wIconValidateIconSize(scr, icon->file_image, wPreferences.icon_size);
165 icon->file = wstrdup(iconfile);
168 icon->tile_type = tile;
170 wIconUpdate(icon);
172 WMAddNotificationObserver(appearanceObserver, icon, WNIconAppearanceSettingsChanged, icon);
173 WMAddNotificationObserver(tileObserver, icon, WNIconTileSettingsChanged, icon);
175 return icon;
178 static WIcon *wIconCreateCore(WScreen *scr, int coord_x, int coord_y)
180 WIcon *icon;
181 unsigned long vmask = 0;
182 XSetWindowAttributes attribs;
184 icon = wmalloc(sizeof(WIcon));
185 icon->core = wCoreCreateTopLevel(scr,
186 coord_x,
187 coord_y,
188 wPreferences.icon_size,
189 wPreferences.icon_size,
192 if (wPreferences.use_saveunders) {
193 vmask = CWSaveUnder;
194 attribs.save_under = True;
197 /* a white border for selecting it */
198 vmask |= CWBorderPixel;
199 attribs.border_pixel = scr->white_pixel;
201 XChangeWindowAttributes(dpy, icon->core->window, vmask, &attribs);
203 /* will be overriden if this is a application icon */
204 icon->core->descriptor.handle_mousedown = miniwindowMouseDown;
205 icon->core->descriptor.handle_expose = miniwindowExpose;
206 icon->core->descriptor.parent_type = WCLASS_MINIWINDOW;
207 icon->core->descriptor.parent = icon;
209 icon->core->stacking = wmalloc(sizeof(WStacking));
210 icon->core->stacking->above = NULL;
211 icon->core->stacking->under = NULL;
212 icon->core->stacking->window_level = NORMAL_ICON_LEVEL;
213 icon->core->stacking->child_of = NULL;
215 return icon;
218 void wIconDestroy(WIcon * icon)
220 WCoreWindow *core = icon->core;
221 WScreen *scr = core->screen_ptr;
223 WMRemoveNotificationObserver(icon);
225 if (icon->handlerID)
226 WMDeleteTimerHandler(icon->handlerID);
228 if (icon->icon_win) {
229 int x = 0, y = 0;
231 if (icon->owner) {
232 x = icon->owner->icon_x;
233 y = icon->owner->icon_y;
235 XUnmapWindow(dpy, icon->icon_win);
236 XReparentWindow(dpy, icon->icon_win, scr->root_win, x, y);
238 if (icon->icon_name)
239 XFree(icon->icon_name);
241 if (icon->pixmap)
242 XFreePixmap(dpy, icon->pixmap);
244 if (icon->file)
245 wfree(icon->file);
247 if (icon->file_image != NULL)
248 RReleaseImage(icon->file_image);
250 wCoreDestroy(icon->core);
251 wfree(icon);
254 static void drawIconTitle(WScreen * scr, Pixmap pixmap, int height)
256 XFillRectangle(dpy, pixmap, scr->icon_title_texture->normal_gc, 0, 0, wPreferences.icon_size, height + 1);
257 XDrawLine(dpy, pixmap, scr->icon_title_texture->light_gc, 0, 0, wPreferences.icon_size, 0);
258 XDrawLine(dpy, pixmap, scr->icon_title_texture->light_gc, 0, 0, 0, height + 1);
259 XDrawLine(dpy, pixmap, scr->icon_title_texture->dim_gc,
260 wPreferences.icon_size - 1, 0, wPreferences.icon_size - 1, height + 1);
263 static Pixmap makeIcon(WScreen *scr, RImage *icon, int titled, int shadowed, int tileType, int highlighted)
265 RImage *tile;
266 Pixmap pixmap;
267 int x, y, sx, sy;
268 unsigned w, h;
269 int theight = 0;
271 if (tileType == TILE_NORMAL) {
272 tile = RCloneImage(scr->icon_tile);
273 } else {
274 assert(scr->clip_tile);
275 tile = RCloneImage(scr->clip_tile);
278 if (icon) {
279 w = (icon->width > wPreferences.icon_size)
280 ? wPreferences.icon_size : icon->width;
281 x = (wPreferences.icon_size - w) / 2;
282 sx = (icon->width - w) / 2;
284 if (titled)
285 theight = WMFontHeight(scr->icon_title_font);
287 h = (icon->height + theight > wPreferences.icon_size
288 ? wPreferences.icon_size - theight : icon->height);
289 y = theight + (wPreferences.icon_size - theight - h) / 2;
290 sy = (icon->height - h) / 2;
292 RCombineArea(tile, icon, sx, sy, w, h, x, y);
295 if (shadowed) {
296 RColor color;
298 color.red = scr->icon_back_texture->light.red >> 8;
299 color.green = scr->icon_back_texture->light.green >> 8;
300 color.blue = scr->icon_back_texture->light.blue >> 8;
301 color.alpha = 150; /* about 60% */
302 RClearImage(tile, &color);
305 if (highlighted) {
306 RColor color;
308 color.red = color.green = color.blue = 0;
309 color.alpha = 160;
310 RLightImage(tile, &color);
313 if (!RConvertImage(scr->rcontext, tile, &pixmap))
314 wwarning(_("error rendering image:%s"), RMessageForError(RErrorCode));
316 RReleaseImage(tile);
318 if (titled)
319 drawIconTitle(scr, pixmap, theight);
321 return pixmap;
324 void wIconChangeTitle(WIcon * icon, char *new_title)
326 int changed;
328 changed = (new_title == NULL && icon->icon_name != NULL)
329 || (new_title != NULL && icon->icon_name == NULL);
331 if (icon->icon_name != NULL)
332 XFree(icon->icon_name);
334 icon->icon_name = new_title;
336 if (changed)
337 icon->force_paint = 1;
338 wIconPaint(icon);
341 RImage *wIconValidateIconSize(WScreen *scr, RImage *icon, int max_size)
343 RImage *nimage;
345 if (!icon)
346 return NULL;
348 /* We should hold "ICON_BORDER" (~2) pixels to include the icon border */
349 if ((icon->width - max_size) > -ICON_BORDER ||
350 (icon->height - max_size) > -ICON_BORDER) {
351 nimage = RScaleImage(icon, max_size - ICON_BORDER,
352 (icon->height * (max_size - ICON_BORDER) / icon->width));
353 RReleaseImage(icon);
354 icon = nimage;
357 return icon;
360 Bool wIconChangeImageFile(WIcon * icon, char *file)
362 WScreen *scr = icon->core->screen_ptr;
363 RImage *image;
364 char *path;
365 int error = 0;
367 if (icon->file_image)
368 RReleaseImage(icon->file_image);
370 if (!file) {
371 icon->file_image = NULL;
372 wIconUpdate(icon);
373 return True;
376 path = FindImage(wPreferences.icon_path, file);
378 if (path && (image = RLoadImage(scr->rcontext, path, 0))) {
379 icon->file_image = wIconValidateIconSize(icon->core->screen_ptr, image, wPreferences.icon_size);
380 wIconUpdate(icon);
381 } else {
382 error = 1;
385 if (path)
386 wfree(path);
388 return !error;
391 static char *get_name_for_wwin(WWindow *wwin)
393 return get_name_for_instance_class(wwin->wm_instance, wwin->wm_class);
396 char *get_name_for_instance_class(char *wm_instance, char *wm_class)
398 char *suffix;
399 int len;
401 if (wm_class && wm_instance) {
402 len = strlen(wm_class) + strlen(wm_instance) + 2;
403 suffix = wmalloc(len);
404 snprintf(suffix, len, "%s.%s", wm_instance, wm_class);
405 } else if (wm_class) {
406 len = strlen(wm_class) + 1;
407 suffix = wmalloc(len);
408 snprintf(suffix, len, "%s", wm_class);
409 } else if (wm_instance) {
410 len = strlen(wm_instance) + 1;
411 suffix = wmalloc(len);
412 snprintf(suffix, len, "%s", wm_instance);
413 } else {
414 return NULL;
417 return suffix;
420 static char *get_icon_cache_path(void)
422 char *prefix, *path;
423 int len, ret;
425 prefix = wusergnusteppath();
426 len = strlen(prefix) + strlen(CACHE_ICON_PATH) + 2;
427 path = wmalloc(len);
428 snprintf(path, len, "%s%s/", prefix, CACHE_ICON_PATH);
430 /* If the folder exists, exit */
431 if (access(path, F_OK) == 0)
432 return path;
434 /* Create the folder */
435 ret = wmkdirhier((const char *) path);
437 /* Exit 1 on success, 0 on failure */
438 if (ret == 1)
439 return path;
441 /* Fail */
442 wfree(path);
443 return NULL;
446 static RImage *get_wwindow_image_from_wmhints(WWindow *wwin, WIcon *icon)
448 RImage *image = NULL;
449 XWMHints *hints = wwin->wm_hints;
451 if (hints && (hints->flags & IconPixmapHint) && hints->icon_pixmap != None)
452 image = RCreateImageFromDrawable(icon->core->screen_ptr->rcontext,
453 hints->icon_pixmap,
454 (hints->flags & IconMaskHint)
455 ? hints->icon_mask : None);
457 return image;
461 * wIconStore--
462 * Stores the client supplied icon at CACHE_ICON_PATH
463 * and returns the path for that icon. Returns NULL if there is no
464 * client supplied icon or on failure.
466 * Side effects:
467 * New directories might be created.
469 char *wIconStore(WIcon * icon)
471 char *path, *dir_path, *file;
472 int len = 0;
473 RImage *image = NULL;
474 WWindow *wwin = icon->owner;
476 if (!wwin)
477 return NULL;
479 dir_path = get_icon_cache_path();
480 if (!dir_path)
481 return NULL;
483 file = get_name_for_wwin(wwin);
484 if (!file) {
485 wfree(dir_path);
486 return NULL;
489 len = strlen(dir_path) + strlen(file) + 5;
490 path = wmalloc(len);
491 snprintf(path, len, "%s%s.xpm", dir_path, file);
492 wfree(dir_path);
493 wfree(file);
495 /* If icon exists, exit */
496 if (access(path, F_OK) == 0)
497 return path;
499 if (wwin->net_icon_image)
500 image = RRetainImage(wwin->net_icon_image);
501 else
502 image = get_wwindow_image_from_wmhints(wwin, icon);
504 if (!image) {
505 wfree(path);
506 return NULL;
509 if (!RSaveImage(image, path, "XPM")) {
510 wfree(path);
511 path = NULL;
514 RReleaseImage(image);
516 return path;
519 static void cycleColor(void *data)
521 WIcon *icon = (WIcon *) data;
522 WScreen *scr = icon->core->screen_ptr;
523 XGCValues gcv;
525 icon->step--;
526 gcv.dash_offset = icon->step;
527 XChangeGC(dpy, scr->icon_select_gc, GCDashOffset, &gcv);
529 XDrawRectangle(dpy, icon->core->window, scr->icon_select_gc, 0, 0,
530 icon->core->width - 1, icon->core->height - 1);
531 icon->handlerID = WMAddTimerHandler(COLOR_CYCLE_DELAY, cycleColor, icon);
534 #ifdef NEWAPPICON
535 void wIconSetHighlited(WIcon *icon, Bool flag)
537 if (icon->highlighted == flag)
538 return;
540 icon->highlighted = flag;
541 icon->force_paint = True;
542 wIconPaint(icon);
544 #endif
546 void wIconSelect(WIcon * icon)
548 WScreen *scr = icon->core->screen_ptr;
549 icon->selected = !icon->selected;
551 if (icon->selected) {
552 icon->step = 0;
553 if (!wPreferences.dont_blink)
554 icon->handlerID = WMAddTimerHandler(10, cycleColor, icon);
555 else
556 XDrawRectangle(dpy, icon->core->window, scr->icon_select_gc, 0, 0,
557 icon->core->width - 1, icon->core->height - 1);
558 } else {
559 if (icon->handlerID) {
560 WMDeleteTimerHandler(icon->handlerID);
561 icon->handlerID = NULL;
563 XClearArea(dpy, icon->core->window, 0, 0, icon->core->width, icon->core->height, True);
567 void wIconUpdate(WIcon * icon)
569 WScreen *scr = icon->core->screen_ptr;
570 WWindow *wwin = icon->owner;
572 assert(scr->icon_tile != NULL);
574 if (icon->pixmap != None)
575 XFreePixmap(dpy, icon->pixmap);
577 icon->pixmap = None;
579 if (wwin && WFLAGP(wwin, always_user_icon)) {
580 /* Forced use user_icon */
581 get_pixmap_icon_from_user_icon(scr, icon);
582 } else if (icon->icon_win != None) {
583 /* Get the Pixmap from the WIcon */
584 get_pixmap_icon_from_icon_win(icon);
585 } else if (wwin && wwin->net_icon_image) {
586 /* Use _NET_WM_ICON icon */
587 icon->pixmap = makeIcon(scr, wwin->net_icon_image, icon->show_title,
588 icon->shadowed, icon->tile_type, icon->highlighted);
589 } else if (wwin && wwin->wm_hints && (wwin->wm_hints->flags & IconPixmapHint)) {
590 /* Get the Pixmap from the wm_hints, else, from the user */
591 if (get_pixmap_icon_from_wm_hints(scr, wwin, icon))
592 get_pixmap_icon_from_user_icon(scr, icon);
593 } else {
594 /* Get the Pixmap from the user */
595 get_pixmap_icon_from_user_icon(scr, icon);
598 /* No pixmap, set default background */
599 if (icon->pixmap != None)
600 XSetWindowBackgroundPixmap(dpy, icon->core->window, icon->pixmap);
602 /* Paint it */
603 XClearWindow(dpy, icon->core->window);
604 wIconPaint(icon);
607 void get_pixmap_icon_from_user_icon(WScreen *scr, WIcon * icon)
609 RImage *image = NULL;
610 char *path;
611 char *file;
613 if (icon->file_image) {
614 icon->pixmap = makeIcon(scr, icon->file_image, icon->show_title,
615 icon->shadowed, icon->tile_type, icon->highlighted);
616 } else {
617 /* make default icons */
618 if (!scr->def_icon_pixmap) {
619 file = wDefaultGetIconFile(NULL, NULL, False);
620 if (file) {
621 path = FindImage(wPreferences.icon_path, file);
622 if (path) {
623 image = RLoadImage(scr->rcontext, path, 0);
624 if (!image)
625 wwarning(_("could not load default icon \"%s\":%s"),
626 file, RMessageForError(RErrorCode));
627 wfree(path);
628 } else {
629 wwarning(_("could not find default icon \"%s\""), file);
633 image = wIconValidateIconSize(scr, image, wPreferences.icon_size);
634 scr->def_icon_pixmap = makeIcon(scr, image, False, False, icon->tile_type, icon->highlighted);
635 scr->def_ticon_pixmap = makeIcon(scr, image, True, False, icon->tile_type, icon->highlighted);
636 if (image)
637 RReleaseImage(image);
640 if (icon->show_title)
641 XSetWindowBackgroundPixmap(dpy, icon->core->window, scr->def_ticon_pixmap);
642 else
643 XSetWindowBackgroundPixmap(dpy, icon->core->window, scr->def_icon_pixmap);
645 icon->pixmap = None;
649 /* Get the Pixmap from the WIcon of the WWindow */
650 void get_pixmap_icon_from_icon_win(WIcon * icon)
652 XWindowAttributes attr;
653 WScreen *scr = icon->core->screen_ptr;
654 int title_height = WMFontHeight(scr->icon_title_font);
655 unsigned int width, height, depth;
656 int theight;
657 int resize = 0;
658 Pixmap pixmap;
660 getSize(icon->icon_win, &width, &height, &depth);
662 if (width > wPreferences.icon_size) {
663 resize = 1;
664 width = wPreferences.icon_size;
667 if (height > wPreferences.icon_size) {
668 resize = 1;
669 height = wPreferences.icon_size;
672 if (icon->show_title && (height + title_height < wPreferences.icon_size)) {
673 pixmap = XCreatePixmap(dpy, scr->w_win, wPreferences.icon_size,
674 wPreferences.icon_size, scr->w_depth);
675 XSetClipMask(dpy, scr->copy_gc, None);
676 XCopyArea(dpy, scr->icon_tile_pixmap, pixmap, scr->copy_gc, 0, 0,
677 wPreferences.icon_size, wPreferences.icon_size, 0, 0);
678 drawIconTitle(scr, pixmap, title_height);
679 theight = title_height;
680 } else {
681 pixmap = None;
682 theight = 0;
683 XSetWindowBackgroundPixmap(dpy, icon->core->window, scr->icon_tile_pixmap);
686 XSetWindowBorderWidth(dpy, icon->icon_win, 0);
687 XReparentWindow(dpy, icon->icon_win, icon->core->window,
688 (wPreferences.icon_size - width) / 2,
689 theight + (wPreferences.icon_size - height - theight) / 2);
690 if (resize)
691 XResizeWindow(dpy, icon->icon_win, width, height);
693 XMapWindow(dpy, icon->icon_win);
694 XAddToSaveSet(dpy, icon->icon_win);
696 /* Save it */
697 icon->pixmap = pixmap;
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 Pixmap from the XWindow wm_hints */
707 int get_pixmap_icon_from_wm_hints(WScreen *scr, WWindow *wwin, WIcon *icon)
709 Window jw;
710 Pixmap pixmap;
711 unsigned int w, h, ju, d;
712 int ji, x, y;
713 int title_height = WMFontHeight(scr->icon_title_font);
715 if (!XGetGeometry(dpy, wwin->wm_hints->icon_pixmap, &jw, &ji, &ji, &w, &h, &ju, &d)) {
716 icon->owner->wm_hints->flags &= ~IconPixmapHint;
717 return 1;
720 pixmap = XCreatePixmap(dpy, icon->core->window, wPreferences.icon_size,
721 wPreferences.icon_size, scr->w_depth);
722 XSetClipMask(dpy, scr->copy_gc, None);
723 XCopyArea(dpy, scr->icon_tile_pixmap, pixmap, scr->copy_gc, 0, 0,
724 wPreferences.icon_size, wPreferences.icon_size, 0, 0);
726 if (w > wPreferences.icon_size)
727 w = wPreferences.icon_size;
728 x = (wPreferences.icon_size - w) / 2;
730 if (icon->show_title && (title_height < wPreferences.icon_size)) {
731 drawIconTitle(scr, pixmap, title_height);
733 if (h > wPreferences.icon_size - title_height - 2) {
734 h = wPreferences.icon_size - title_height - 2;
735 y = title_height + 1;
736 } else {
737 y = (wPreferences.icon_size - h - title_height) / 2 + title_height + 1;
739 } else {
740 if (w > wPreferences.icon_size)
741 w = wPreferences.icon_size;
742 y = (wPreferences.icon_size - h) / 2;
745 if (wwin->wm_hints->flags & IconMaskHint)
746 XSetClipMask(dpy, scr->copy_gc, wwin->wm_hints->icon_mask);
748 XSetClipOrigin(dpy, scr->copy_gc, x, y);
750 if (d != scr->w_depth) {
751 XSetForeground(dpy, scr->copy_gc, scr->black_pixel);
752 XSetBackground(dpy, scr->copy_gc, scr->white_pixel);
753 XCopyPlane(dpy, wwin->wm_hints->icon_pixmap, pixmap, scr->copy_gc, 0, 0, w, h, x, y, 1);
754 } else {
755 XCopyArea(dpy, wwin->wm_hints->icon_pixmap, pixmap, scr->copy_gc, 0, 0, w, h, x, y);
758 XSetClipOrigin(dpy, scr->copy_gc, 0, 0);
760 icon->pixmap = pixmap;
761 return (0);
764 void wIconPaint(WIcon * icon)
766 WScreen *scr = icon->core->screen_ptr;
767 int x;
768 char *tmp;
770 if (icon->force_paint) {
771 icon->force_paint = 0;
772 wIconUpdate(icon);
773 return;
776 XClearWindow(dpy, icon->core->window);
778 /* draw the icon title */
779 if (icon->show_title && icon->icon_name != NULL) {
780 int l;
781 int w;
783 tmp = ShrinkString(scr->icon_title_font, icon->icon_name, wPreferences.icon_size - 4);
784 w = WMWidthOfString(scr->icon_title_font, tmp, l = strlen(tmp));
786 if (w > icon->core->width - 4)
787 x = (icon->core->width - 4) - w;
788 else
789 x = (icon->core->width - w) / 2;
791 WMDrawString(scr->wmscreen, icon->core->window, scr->icon_title_color,
792 scr->icon_title_font, x, 1, tmp, l);
793 wfree(tmp);
796 if (icon->selected)
797 XDrawRectangle(dpy, icon->core->window, scr->icon_select_gc, 0, 0,
798 icon->core->width - 1, icon->core->height - 1);
801 /******************************************************************/
803 static void miniwindowExpose(WObjDescriptor * desc, XEvent * event)
805 wIconPaint(desc->parent);
808 static void miniwindowDblClick(WObjDescriptor * desc, XEvent * event)
810 WIcon *icon = desc->parent;
812 assert(icon->owner != NULL);
814 wDeiconifyWindow(icon->owner);
817 static void miniwindowMouseDown(WObjDescriptor * desc, XEvent * event)
819 WIcon *icon = desc->parent;
820 WWindow *wwin = icon->owner;
821 XEvent ev;
822 int x = wwin->icon_x, y = wwin->icon_y;
823 int dx = event->xbutton.x, dy = event->xbutton.y;
824 int grabbed = 0;
825 int clickButton = event->xbutton.button;
826 Bool hasMoved = False;
828 if (WCHECK_STATE(WSTATE_MODAL))
829 return;
831 if (IsDoubleClick(icon->core->screen_ptr, event)) {
832 miniwindowDblClick(desc, event);
833 return;
836 if (event->xbutton.button == Button1) {
837 if (event->xbutton.state & MOD_MASK)
838 wLowerFrame(icon->core);
839 else
840 wRaiseFrame(icon->core);
841 if (event->xbutton.state & ShiftMask) {
842 wIconSelect(icon);
843 wSelectWindow(icon->owner, !wwin->flags.selected);
845 } else if (event->xbutton.button == Button3) {
846 WObjDescriptor *desc;
848 OpenMiniwindowMenu(wwin, event->xbutton.x_root, event->xbutton.y_root);
850 /* allow drag select of menu */
851 desc = &wwin->screen_ptr->window_menu->menu->descriptor;
852 event->xbutton.send_event = True;
853 (*desc->handle_mousedown) (desc, event);
855 return;
858 if (XGrabPointer(dpy, icon->core->window, False, ButtonMotionMask
859 | ButtonReleaseMask | ButtonPressMask, GrabModeAsync,
860 GrabModeAsync, None, None, CurrentTime) != GrabSuccess) {
862 while (1) {
863 WMMaskEvent(dpy, PointerMotionMask | ButtonReleaseMask | ButtonPressMask
864 | ButtonMotionMask | ExposureMask, &ev);
865 switch (ev.type) {
866 case Expose:
867 WMHandleEvent(&ev);
868 break;
870 case MotionNotify:
871 hasMoved = True;
872 if (!grabbed) {
873 if (abs(dx - ev.xmotion.x) >= MOVE_THRESHOLD
874 || abs(dy - ev.xmotion.y) >= MOVE_THRESHOLD) {
875 XChangeActivePointerGrab(dpy, ButtonMotionMask
876 | ButtonReleaseMask | ButtonPressMask,
877 wCursor[WCUR_MOVE], CurrentTime);
878 grabbed = 1;
879 } else {
880 break;
883 x = ev.xmotion.x_root - dx;
884 y = ev.xmotion.y_root - dy;
885 XMoveWindow(dpy, icon->core->window, x, y);
886 break;
888 case ButtonPress:
889 break;
891 case ButtonRelease:
892 if (ev.xbutton.button != clickButton)
893 break;
895 if (wwin->icon_x != x || wwin->icon_y != y)
896 wwin->flags.icon_moved = 1;
898 XMoveWindow(dpy, icon->core->window, x, y);
900 wwin->icon_x = x;
901 wwin->icon_y = y;
902 XUngrabPointer(dpy, CurrentTime);
904 if (wPreferences.auto_arrange_icons)
905 wArrangeIcons(wwin->screen_ptr, True);
906 if (wPreferences.single_click && !hasMoved)
907 miniwindowDblClick(desc, event);
908 return;