New helper function get_default_image
[wmaker-crm.git] / src / icon.c
blob2739ca19036374095d11b29ac5f7ad5d9a2e6574
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 RImage *image = NULL;
632 WScreen *scr = icon->core->screen_ptr;
634 if (icon->file_image) {
635 icon_update_pixmap(icon, icon->file_image);
636 } else {
637 /* make default icons */
638 if (!scr->def_icon_pixmap) {
639 image = get_default_image(scr);
640 scr->def_icon_pixmap = makeIcon(scr, image, False, False, icon->tile_type, icon->highlighted);
641 scr->def_ticon_pixmap = makeIcon(scr, image, True, False, icon->tile_type, icon->highlighted);
642 if (image)
643 RReleaseImage(image);
646 if (icon->show_title)
647 XSetWindowBackgroundPixmap(dpy, icon->core->window, scr->def_ticon_pixmap);
648 else
649 XSetWindowBackgroundPixmap(dpy, icon->core->window, scr->def_icon_pixmap);
651 icon->pixmap = None;
655 /* This function creates the RImage using the default icon */
656 static RImage *get_default_image(WScreen *scr)
658 RImage *image = NULL;
659 char *path, *file;
661 /* Get the default icon */
662 file = wDefaultGetIconFile(NULL, NULL, True);
663 if (file) {
664 path = FindImage(wPreferences.icon_path, file);
665 if (path) {
666 image = RLoadImage(scr->rcontext, path, 0);
667 if (!image)
668 wwarning(_("could not load default icon \"%s\":%s"),
669 file, RMessageForError(RErrorCode));
670 wfree(path);
671 } else {
672 wwarning(_("could not find default icon \"%s\""), file);
676 /* Validate the icon size */
677 image = wIconValidateIconSize(image, wPreferences.icon_size);
679 return image;
682 /* Get the Pixmap from the WIcon of the WWindow */
683 static void get_pixmap_icon_from_icon_win(WIcon * icon)
685 XWindowAttributes attr;
686 WScreen *scr = icon->core->screen_ptr;
687 int title_height = WMFontHeight(scr->icon_title_font);
688 unsigned int width, height, depth;
689 int theight;
690 int resize = 0;
691 Pixmap pixmap;
693 getSize(icon->icon_win, &width, &height, &depth);
695 if (width > wPreferences.icon_size) {
696 resize = 1;
697 width = wPreferences.icon_size;
700 if (height > wPreferences.icon_size) {
701 resize = 1;
702 height = wPreferences.icon_size;
705 if (icon->show_title && (height + title_height < wPreferences.icon_size)) {
706 pixmap = XCreatePixmap(dpy, scr->w_win, wPreferences.icon_size,
707 wPreferences.icon_size, scr->w_depth);
708 XSetClipMask(dpy, scr->copy_gc, None);
709 XCopyArea(dpy, scr->icon_tile_pixmap, pixmap, scr->copy_gc, 0, 0,
710 wPreferences.icon_size, wPreferences.icon_size, 0, 0);
711 drawIconTitle(scr, pixmap, title_height);
712 theight = title_height;
713 } else {
714 pixmap = None;
715 theight = 0;
716 XSetWindowBackgroundPixmap(dpy, icon->core->window, scr->icon_tile_pixmap);
719 XSetWindowBorderWidth(dpy, icon->icon_win, 0);
720 XReparentWindow(dpy, icon->icon_win, icon->core->window,
721 (wPreferences.icon_size - width) / 2,
722 theight + (wPreferences.icon_size - height - theight) / 2);
723 if (resize)
724 XResizeWindow(dpy, icon->icon_win, width, height);
726 XMapWindow(dpy, icon->icon_win);
727 XAddToSaveSet(dpy, icon->icon_win);
729 /* Save it */
730 icon->pixmap = pixmap;
732 if ((XGetWindowAttributes(dpy, icon->icon_win, &attr)) &&
733 (attr.all_event_masks & ButtonPressMask))
734 wHackedGrabButton(Button1, MOD_MASK, icon->core->window, True,
735 ButtonPressMask, GrabModeSync, GrabModeAsync,
736 None, wCursor[WCUR_ARROW]);
739 /* Get the Pixmap from the XWindow wm_hints */
740 static int get_pixmap_icon_from_wm_hints(WIcon *icon)
742 Window jw;
743 Pixmap pixmap;
744 unsigned int w, h, ju, d;
745 int ji, x, y;
746 WWindow *wwin = icon->owner;
747 WScreen *scr = icon->core->screen_ptr;
748 int title_height = WMFontHeight(scr->icon_title_font);
750 if (!XGetGeometry(dpy, wwin->wm_hints->icon_pixmap, &jw, &ji, &ji, &w, &h, &ju, &d)) {
751 icon->owner->wm_hints->flags &= ~IconPixmapHint;
752 return 1;
755 pixmap = XCreatePixmap(dpy, icon->core->window, wPreferences.icon_size,
756 wPreferences.icon_size, scr->w_depth);
757 XSetClipMask(dpy, scr->copy_gc, None);
758 XCopyArea(dpy, scr->icon_tile_pixmap, pixmap, scr->copy_gc, 0, 0,
759 wPreferences.icon_size, wPreferences.icon_size, 0, 0);
761 if (w > wPreferences.icon_size)
762 w = wPreferences.icon_size;
763 x = (wPreferences.icon_size - w) / 2;
765 if (icon->show_title && (title_height < wPreferences.icon_size)) {
766 drawIconTitle(scr, pixmap, title_height);
768 if (h > wPreferences.icon_size - title_height - 2) {
769 h = wPreferences.icon_size - title_height - 2;
770 y = title_height + 1;
771 } else {
772 y = (wPreferences.icon_size - h - title_height) / 2 + title_height + 1;
774 } else {
775 if (w > wPreferences.icon_size)
776 w = wPreferences.icon_size;
777 y = (wPreferences.icon_size - h) / 2;
780 if (wwin->wm_hints->flags & IconMaskHint)
781 XSetClipMask(dpy, scr->copy_gc, wwin->wm_hints->icon_mask);
783 XSetClipOrigin(dpy, scr->copy_gc, x, y);
785 if (d != scr->w_depth) {
786 XSetForeground(dpy, scr->copy_gc, scr->black_pixel);
787 XSetBackground(dpy, scr->copy_gc, scr->white_pixel);
788 XCopyPlane(dpy, wwin->wm_hints->icon_pixmap, pixmap, scr->copy_gc, 0, 0, w, h, x, y, 1);
789 } else {
790 XCopyArea(dpy, wwin->wm_hints->icon_pixmap, pixmap, scr->copy_gc, 0, 0, w, h, x, y);
793 XSetClipOrigin(dpy, scr->copy_gc, 0, 0);
795 icon->pixmap = pixmap;
796 return (0);
799 void wIconPaint(WIcon * icon)
801 WScreen *scr = icon->core->screen_ptr;
802 int x;
803 char *tmp;
805 if (icon->force_paint) {
806 icon->force_paint = 0;
807 wIconUpdate(icon);
808 return;
811 XClearWindow(dpy, icon->core->window);
813 /* draw the icon title */
814 if (icon->show_title && icon->icon_name != NULL) {
815 int l;
816 int w;
818 tmp = ShrinkString(scr->icon_title_font, icon->icon_name, wPreferences.icon_size - 4);
819 w = WMWidthOfString(scr->icon_title_font, tmp, l = strlen(tmp));
821 if (w > icon->core->width - 4)
822 x = (icon->core->width - 4) - w;
823 else
824 x = (icon->core->width - w) / 2;
826 WMDrawString(scr->wmscreen, icon->core->window, scr->icon_title_color,
827 scr->icon_title_font, x, 1, tmp, l);
828 wfree(tmp);
831 if (icon->selected)
832 XDrawRectangle(dpy, icon->core->window, scr->icon_select_gc, 0, 0,
833 icon->core->width - 1, icon->core->height - 1);
836 /******************************************************************/
838 static void miniwindowExpose(WObjDescriptor * desc, XEvent * event)
840 wIconPaint(desc->parent);
843 static void miniwindowDblClick(WObjDescriptor * desc, XEvent * event)
845 WIcon *icon = desc->parent;
847 assert(icon->owner != NULL);
849 wDeiconifyWindow(icon->owner);
852 static void miniwindowMouseDown(WObjDescriptor * desc, XEvent * event)
854 WIcon *icon = desc->parent;
855 WWindow *wwin = icon->owner;
856 XEvent ev;
857 int x = wwin->icon_x, y = wwin->icon_y;
858 int dx = event->xbutton.x, dy = event->xbutton.y;
859 int grabbed = 0;
860 int clickButton = event->xbutton.button;
861 Bool hasMoved = False;
863 if (WCHECK_STATE(WSTATE_MODAL))
864 return;
866 if (IsDoubleClick(icon->core->screen_ptr, event)) {
867 miniwindowDblClick(desc, event);
868 return;
871 if (event->xbutton.button == Button1) {
872 if (event->xbutton.state & MOD_MASK)
873 wLowerFrame(icon->core);
874 else
875 wRaiseFrame(icon->core);
876 if (event->xbutton.state & ShiftMask) {
877 wIconSelect(icon);
878 wSelectWindow(icon->owner, !wwin->flags.selected);
880 } else if (event->xbutton.button == Button3) {
881 WObjDescriptor *desc;
883 OpenMiniwindowMenu(wwin, event->xbutton.x_root, event->xbutton.y_root);
885 /* allow drag select of menu */
886 desc = &wwin->screen_ptr->window_menu->menu->descriptor;
887 event->xbutton.send_event = True;
888 (*desc->handle_mousedown) (desc, event);
890 return;
893 if (XGrabPointer(dpy, icon->core->window, False, ButtonMotionMask
894 | ButtonReleaseMask | ButtonPressMask, GrabModeAsync,
895 GrabModeAsync, None, None, CurrentTime) != GrabSuccess) {
897 while (1) {
898 WMMaskEvent(dpy, PointerMotionMask | ButtonReleaseMask | ButtonPressMask
899 | ButtonMotionMask | ExposureMask, &ev);
900 switch (ev.type) {
901 case Expose:
902 WMHandleEvent(&ev);
903 break;
905 case MotionNotify:
906 hasMoved = True;
907 if (!grabbed) {
908 if (abs(dx - ev.xmotion.x) >= MOVE_THRESHOLD
909 || abs(dy - ev.xmotion.y) >= MOVE_THRESHOLD) {
910 XChangeActivePointerGrab(dpy, ButtonMotionMask
911 | ButtonReleaseMask | ButtonPressMask,
912 wCursor[WCUR_MOVE], CurrentTime);
913 grabbed = 1;
914 } else {
915 break;
918 x = ev.xmotion.x_root - dx;
919 y = ev.xmotion.y_root - dy;
920 XMoveWindow(dpy, icon->core->window, x, y);
921 break;
923 case ButtonPress:
924 break;
926 case ButtonRelease:
927 if (ev.xbutton.button != clickButton)
928 break;
930 if (wwin->icon_x != x || wwin->icon_y != y)
931 wwin->flags.icon_moved = 1;
933 XMoveWindow(dpy, icon->core->window, x, y);
935 wwin->icon_x = x;
936 wwin->icon_y = y;
937 XUngrabPointer(dpy, CurrentTime);
939 if (wPreferences.auto_arrange_icons)
940 wArrangeIcons(wwin->screen_ptr, True);
941 if (wPreferences.single_click && !hasMoved)
942 miniwindowDblClick(desc, event);
943 return;