declare get_pixmap_icon_from_* as static functions
[wmaker-crm.git] / src / icon.c
blobbb73c64f5e44580e438b78da173dbdc76d35b93c
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);
71 /****** Notification Observers ******/
73 static void appearanceObserver(void *self, WMNotification * notif)
75 WIcon *icon = (WIcon *) self;
76 uintptr_t flags = (uintptr_t)WMGetNotificationClientData(notif);
78 if ((flags & WTextureSettings) || (flags & WFontSettings))
79 icon->force_paint = 1;
81 wIconPaint(icon);
83 /* so that the appicon expose handlers will paint the appicon specific
84 * stuff */
85 XClearArea(dpy, icon->core->window, 0, 0, icon->core->width, icon->core->height, True);
88 static void tileObserver(void *self, WMNotification * notif)
90 WIcon *icon = (WIcon *) self;
92 icon->force_paint = 1;
93 wIconPaint(icon);
95 XClearArea(dpy, icon->core->window, 0, 0, 1, 1, True);
98 /************************************/
100 INLINE static void getSize(Drawable d, unsigned int *w, unsigned int *h, unsigned int *dep)
102 Window rjunk;
103 int xjunk, yjunk;
104 unsigned int bjunk;
106 XGetGeometry(dpy, d, &rjunk, &xjunk, &yjunk, w, h, &bjunk, dep);
109 WIcon *icon_create_for_wwindow(WWindow *wwin)
111 WScreen *scr = wwin->screen_ptr;
112 WIcon *icon;
113 char *file;
115 icon = icon_create_core(scr, wwin->icon_x, wwin->icon_y);
117 icon->owner = wwin;
118 if (wwin->wm_hints && (wwin->wm_hints->flags & IconWindowHint)) {
119 if (wwin->client_win == wwin->main_window) {
120 WApplication *wapp;
121 /* do not let miniwindow steal app-icon's icon window */
122 wapp = wApplicationOf(wwin->client_win);
123 if (!wapp || wapp->app_icon == NULL)
124 icon->icon_win = wwin->wm_hints->icon_window;
125 } else {
126 icon->icon_win = wwin->wm_hints->icon_window;
129 #ifdef NO_MINIWINDOW_TITLES
130 icon->show_title = 0;
131 #else
132 icon->show_title = 1;
133 #endif
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 /* Get the application icon, default included */
142 file = get_default_icon_filename(scr, wwin->wm_instance, wwin->wm_class, NULL, True);
143 if (file) {
144 icon->file = wstrdup(file);
145 icon->file_image = get_default_icon_rimage(scr, icon->file, wPreferences.icon_size);
146 wfree(file);
149 icon->tile_type = TILE_NORMAL;
151 wIconUpdate(icon);
153 WMAddNotificationObserver(appearanceObserver, icon, WNIconAppearanceSettingsChanged, icon);
154 WMAddNotificationObserver(tileObserver, icon, WNIconTileSettingsChanged, icon);
156 return icon;
159 WIcon *icon_create_for_dock(WScreen *scr, char *command, char *wm_instance, char *wm_class, int tile)
161 WIcon *icon;
162 char *file = NULL;
164 icon = icon_create_core(scr, 0, 0);
166 /* Search the icon using instance and class, without default icon */
167 file = get_default_icon_filename(scr, wm_instance, wm_class, command, False);
168 if (file) {
169 icon->file = wstrdup(file);
170 icon->file_image = get_default_icon_rimage(scr, icon->file, wPreferences.icon_size);
171 wfree(file);
174 icon->tile_type = tile;
176 wIconUpdate(icon);
178 WMAddNotificationObserver(appearanceObserver, icon, WNIconAppearanceSettingsChanged, icon);
179 WMAddNotificationObserver(tileObserver, icon, WNIconTileSettingsChanged, icon);
181 return icon;
184 static WIcon *icon_create_core(WScreen *scr, int coord_x, int coord_y)
186 WIcon *icon;
187 unsigned long vmask = 0;
188 XSetWindowAttributes attribs;
190 icon = wmalloc(sizeof(WIcon));
191 icon->core = wCoreCreateTopLevel(scr,
192 coord_x,
193 coord_y,
194 wPreferences.icon_size,
195 wPreferences.icon_size,
196 0, scr->w_depth, scr->w_visual, scr->w_colormap);
198 if (wPreferences.use_saveunders) {
199 vmask = CWSaveUnder;
200 attribs.save_under = True;
203 /* a white border for selecting it */
204 vmask |= CWBorderPixel;
205 attribs.border_pixel = scr->white_pixel;
207 XChangeWindowAttributes(dpy, icon->core->window, vmask, &attribs);
209 /* will be overriden if this is a application icon */
210 icon->core->descriptor.handle_mousedown = miniwindowMouseDown;
211 icon->core->descriptor.handle_expose = miniwindowExpose;
212 icon->core->descriptor.parent_type = WCLASS_MINIWINDOW;
213 icon->core->descriptor.parent = icon;
215 icon->core->stacking = wmalloc(sizeof(WStacking));
216 icon->core->stacking->above = NULL;
217 icon->core->stacking->under = NULL;
218 icon->core->stacking->window_level = NORMAL_ICON_LEVEL;
219 icon->core->stacking->child_of = NULL;
221 /* Icon image */
222 icon->file = NULL;
223 icon->file_image = NULL;
225 return icon;
228 void wIconDestroy(WIcon * icon)
230 WCoreWindow *core = icon->core;
231 WScreen *scr = core->screen_ptr;
233 WMRemoveNotificationObserver(icon);
235 if (icon->handlerID)
236 WMDeleteTimerHandler(icon->handlerID);
238 if (icon->icon_win) {
239 int x = 0, y = 0;
241 if (icon->owner) {
242 x = icon->owner->icon_x;
243 y = icon->owner->icon_y;
245 XUnmapWindow(dpy, icon->icon_win);
246 XReparentWindow(dpy, icon->icon_win, scr->root_win, x, y);
248 if (icon->icon_name)
249 XFree(icon->icon_name);
251 if (icon->pixmap)
252 XFreePixmap(dpy, icon->pixmap);
254 if (icon->file)
255 wfree(icon->file);
257 if (icon->file_image != NULL)
258 RReleaseImage(icon->file_image);
260 wCoreDestroy(icon->core);
261 wfree(icon);
264 static void drawIconTitle(WScreen * scr, Pixmap pixmap, int height)
266 XFillRectangle(dpy, pixmap, scr->icon_title_texture->normal_gc, 0, 0, wPreferences.icon_size, height + 1);
267 XDrawLine(dpy, pixmap, scr->icon_title_texture->light_gc, 0, 0, wPreferences.icon_size, 0);
268 XDrawLine(dpy, pixmap, scr->icon_title_texture->light_gc, 0, 0, 0, height + 1);
269 XDrawLine(dpy, pixmap, scr->icon_title_texture->dim_gc,
270 wPreferences.icon_size - 1, 0, wPreferences.icon_size - 1, height + 1);
273 static void icon_update_pixmap(WIcon *icon, RImage *image)
275 WScreen *scr = icon->core->screen_ptr;
277 /* Update the WIcon pixmap */
278 icon->pixmap = makeIcon(scr, image,
279 icon->show_title, icon->shadowed,
280 icon->tile_type, icon->highlighted);
283 static Pixmap makeIcon(WScreen *scr, RImage *image, int titled, int shadowed, int tileType, int highlighted)
285 RImage *tile;
286 Pixmap pixmap;
287 int x, y, sx, sy;
288 unsigned w, h;
289 int theight = 0;
291 if (tileType == TILE_NORMAL) {
292 tile = RCloneImage(scr->icon_tile);
293 } else {
294 assert(scr->clip_tile);
295 tile = RCloneImage(scr->clip_tile);
298 if (image) {
299 w = (image->width > wPreferences.icon_size)
300 ? wPreferences.icon_size : image->width;
301 x = (wPreferences.icon_size - w) / 2;
302 sx = (image->width - w) / 2;
304 if (titled)
305 theight = WMFontHeight(scr->icon_title_font);
307 h = (image->height + theight > wPreferences.icon_size
308 ? wPreferences.icon_size - theight : image->height);
309 y = theight + (wPreferences.icon_size - theight - h) / 2;
310 sy = (image->height - h) / 2;
312 RCombineArea(tile, image, sx, sy, w, h, x, y);
315 if (shadowed) {
316 RColor color;
318 color.red = scr->icon_back_texture->light.red >> 8;
319 color.green = scr->icon_back_texture->light.green >> 8;
320 color.blue = scr->icon_back_texture->light.blue >> 8;
321 color.alpha = 150; /* about 60% */
322 RClearImage(tile, &color);
325 if (highlighted) {
326 RColor color;
328 color.red = color.green = color.blue = 0;
329 color.alpha = 160;
330 RLightImage(tile, &color);
333 if (!RConvertImage(scr->rcontext, tile, &pixmap))
334 wwarning(_("error rendering image:%s"), RMessageForError(RErrorCode));
336 RReleaseImage(tile);
338 if (titled)
339 drawIconTitle(scr, pixmap, theight);
341 return pixmap;
344 void wIconChangeTitle(WIcon * icon, char *new_title)
346 int changed;
348 changed = (new_title == NULL && icon->icon_name != NULL)
349 || (new_title != NULL && icon->icon_name == NULL);
351 if (icon->icon_name != NULL)
352 XFree(icon->icon_name);
354 icon->icon_name = new_title;
356 if (changed)
357 icon->force_paint = 1;
358 wIconPaint(icon);
361 RImage *wIconValidateIconSize(RImage *icon, int max_size)
363 RImage *nimage;
365 if (!icon)
366 return NULL;
368 /* We should hold "ICON_BORDER" (~2) pixels to include the icon border */
369 if ((icon->width - max_size) > -ICON_BORDER ||
370 (icon->height - max_size) > -ICON_BORDER) {
371 nimage = RScaleImage(icon, max_size - ICON_BORDER,
372 (icon->height * (max_size - ICON_BORDER) / icon->width));
373 RReleaseImage(icon);
374 icon = nimage;
377 return icon;
380 Bool wIconChangeImageFile(WIcon * icon, char *file)
382 WScreen *scr = icon->core->screen_ptr;
383 RImage *image;
384 char *path;
385 int error = 0;
387 if (icon->file_image) {
388 RReleaseImage(icon->file_image);
389 icon->file_image = NULL;
392 if (!file) {
393 wIconUpdate(icon);
394 return True;
397 path = FindImage(wPreferences.icon_path, file);
399 if (path && (image = RLoadImage(scr->rcontext, path, 0))) {
400 icon->file_image = wIconValidateIconSize(image, wPreferences.icon_size);
401 wIconUpdate(icon);
402 } else {
403 error = 1;
406 if (path)
407 wfree(path);
409 return !error;
412 static char *get_name_for_wwin(WWindow *wwin)
414 return get_name_for_instance_class(wwin->wm_instance, wwin->wm_class);
417 char *get_name_for_instance_class(char *wm_instance, char *wm_class)
419 char *suffix;
420 int len;
422 if (wm_class && wm_instance) {
423 len = strlen(wm_class) + strlen(wm_instance) + 2;
424 suffix = wmalloc(len);
425 snprintf(suffix, len, "%s.%s", wm_instance, wm_class);
426 } else if (wm_class) {
427 len = strlen(wm_class) + 1;
428 suffix = wmalloc(len);
429 snprintf(suffix, len, "%s", wm_class);
430 } else if (wm_instance) {
431 len = strlen(wm_instance) + 1;
432 suffix = wmalloc(len);
433 snprintf(suffix, len, "%s", wm_instance);
434 } else {
435 return NULL;
438 return suffix;
441 static char *get_icon_cache_path(void)
443 char *prefix, *path;
444 int len, ret;
446 prefix = wusergnusteppath();
447 len = strlen(prefix) + strlen(CACHE_ICON_PATH) + 2;
448 path = wmalloc(len);
449 snprintf(path, len, "%s%s/", prefix, CACHE_ICON_PATH);
451 /* If the folder exists, exit */
452 if (access(path, F_OK) == 0)
453 return path;
455 /* Create the folder */
456 ret = wmkdirhier((const char *) path);
458 /* Exit 1 on success, 0 on failure */
459 if (ret == 1)
460 return path;
462 /* Fail */
463 wfree(path);
464 return NULL;
467 static RImage *get_wwindow_image_from_wmhints(WWindow *wwin, WIcon *icon)
469 RImage *image = NULL;
470 XWMHints *hints = wwin->wm_hints;
472 if (hints && (hints->flags & IconPixmapHint) && hints->icon_pixmap != None)
473 image = RCreateImageFromDrawable(icon->core->screen_ptr->rcontext,
474 hints->icon_pixmap,
475 (hints->flags & IconMaskHint)
476 ? hints->icon_mask : None);
478 return image;
482 * wIconStore--
483 * Stores the client supplied icon at CACHE_ICON_PATH
484 * and returns the path for that icon. Returns NULL if there is no
485 * client supplied icon or on failure.
487 * Side effects:
488 * New directories might be created.
490 char *wIconStore(WIcon * icon)
492 char *path, *dir_path, *file;
493 int len = 0;
494 RImage *image = NULL;
495 WWindow *wwin = icon->owner;
497 if (!wwin)
498 return NULL;
500 dir_path = get_icon_cache_path();
501 if (!dir_path)
502 return NULL;
504 file = get_name_for_wwin(wwin);
505 if (!file) {
506 wfree(dir_path);
507 return NULL;
510 len = strlen(dir_path) + strlen(file) + 5;
511 path = wmalloc(len);
512 snprintf(path, len, "%s%s.xpm", dir_path, file);
513 wfree(dir_path);
514 wfree(file);
516 /* If icon exists, exit */
517 if (access(path, F_OK) == 0)
518 return path;
520 if (wwin->net_icon_image)
521 image = RRetainImage(wwin->net_icon_image);
522 else
523 image = get_wwindow_image_from_wmhints(wwin, icon);
525 if (!image) {
526 wfree(path);
527 return NULL;
530 if (!RSaveImage(image, path, "XPM")) {
531 wfree(path);
532 path = NULL;
535 RReleaseImage(image);
537 return path;
540 static void cycleColor(void *data)
542 WIcon *icon = (WIcon *) data;
543 WScreen *scr = icon->core->screen_ptr;
544 XGCValues gcv;
546 icon->step--;
547 gcv.dash_offset = icon->step;
548 XChangeGC(dpy, scr->icon_select_gc, GCDashOffset, &gcv);
550 XDrawRectangle(dpy, icon->core->window, scr->icon_select_gc, 0, 0,
551 icon->core->width - 1, icon->core->height - 1);
552 icon->handlerID = WMAddTimerHandler(COLOR_CYCLE_DELAY, cycleColor, icon);
555 #ifdef NEWAPPICON
556 void wIconSetHighlited(WIcon *icon, Bool flag)
558 if (icon->highlighted == flag)
559 return;
561 icon->highlighted = flag;
562 icon->force_paint = True;
563 wIconPaint(icon);
565 #endif
567 void wIconSelect(WIcon * icon)
569 WScreen *scr = icon->core->screen_ptr;
570 icon->selected = !icon->selected;
572 if (icon->selected) {
573 icon->step = 0;
574 if (!wPreferences.dont_blink)
575 icon->handlerID = WMAddTimerHandler(10, cycleColor, icon);
576 else
577 XDrawRectangle(dpy, icon->core->window, scr->icon_select_gc, 0, 0,
578 icon->core->width - 1, icon->core->height - 1);
579 } else {
580 if (icon->handlerID) {
581 WMDeleteTimerHandler(icon->handlerID);
582 icon->handlerID = NULL;
584 XClearArea(dpy, icon->core->window, 0, 0, icon->core->width, icon->core->height, True);
588 void wIconUpdate(WIcon *icon)
590 WScreen *scr = icon->core->screen_ptr;
591 WWindow *wwin = icon->owner;
593 assert(scr->icon_tile != NULL);
595 if (icon->pixmap != None)
596 XFreePixmap(dpy, icon->pixmap);
598 icon->pixmap = None;
600 if (wwin && WFLAGP(wwin, always_user_icon)) {
601 /* Forced use user_icon */
602 get_pixmap_icon_from_user_icon(icon);
603 } else if (icon->icon_win != None) {
604 /* Get the Pixmap from the WIcon */
605 get_pixmap_icon_from_icon_win(icon);
606 } else if (wwin && wwin->net_icon_image) {
607 /* Use _NET_WM_ICON icon */
608 icon_update_pixmap(icon, wwin->net_icon_image);
609 } else if (wwin && wwin->wm_hints && (wwin->wm_hints->flags & IconPixmapHint)) {
610 /* Get the Pixmap from the wm_hints, else, from the user */
611 if (get_pixmap_icon_from_wm_hints(icon))
612 get_pixmap_icon_from_user_icon(icon);
613 } else {
614 /* Get the Pixmap from the user */
615 get_pixmap_icon_from_user_icon(icon);
618 /* No pixmap, set default background */
619 if (icon->pixmap != None)
620 XSetWindowBackgroundPixmap(dpy, icon->core->window, icon->pixmap);
622 /* Paint it */
623 XClearWindow(dpy, icon->core->window);
624 wIconPaint(icon);
627 static void get_pixmap_icon_from_user_icon(WIcon *icon)
629 RImage *image = NULL;
630 char *path, *file;
631 WScreen *scr = icon->core->screen_ptr;
633 if (icon->file_image) {
634 icon_update_pixmap(icon, icon->file_image);
635 } else {
636 /* make default icons */
637 if (!scr->def_icon_pixmap) {
638 /* Get the default icon */
639 file = wDefaultGetIconFile(NULL, NULL, True);
640 if (file) {
641 path = FindImage(wPreferences.icon_path, file);
642 if (path) {
643 image = RLoadImage(scr->rcontext, path, 0);
644 if (!image)
645 wwarning(_("could not load default icon \"%s\":%s"),
646 file, RMessageForError(RErrorCode));
647 wfree(path);
648 } else {
649 wwarning(_("could not find default icon \"%s\""), file);
653 image = wIconValidateIconSize(image, wPreferences.icon_size);
654 scr->def_icon_pixmap = makeIcon(scr, image, False, False, icon->tile_type, icon->highlighted);
655 scr->def_ticon_pixmap = makeIcon(scr, image, True, False, icon->tile_type, icon->highlighted);
656 if (image)
657 RReleaseImage(image);
660 if (icon->show_title)
661 XSetWindowBackgroundPixmap(dpy, icon->core->window, scr->def_ticon_pixmap);
662 else
663 XSetWindowBackgroundPixmap(dpy, icon->core->window, scr->def_icon_pixmap);
665 icon->pixmap = None;
669 /* Get the Pixmap from the WIcon of the WWindow */
670 static void get_pixmap_icon_from_icon_win(WIcon * icon)
672 XWindowAttributes attr;
673 WScreen *scr = icon->core->screen_ptr;
674 int title_height = WMFontHeight(scr->icon_title_font);
675 unsigned int width, height, depth;
676 int theight;
677 int resize = 0;
678 Pixmap pixmap;
680 getSize(icon->icon_win, &width, &height, &depth);
682 if (width > wPreferences.icon_size) {
683 resize = 1;
684 width = wPreferences.icon_size;
687 if (height > wPreferences.icon_size) {
688 resize = 1;
689 height = wPreferences.icon_size;
692 if (icon->show_title && (height + title_height < wPreferences.icon_size)) {
693 pixmap = XCreatePixmap(dpy, scr->w_win, wPreferences.icon_size,
694 wPreferences.icon_size, scr->w_depth);
695 XSetClipMask(dpy, scr->copy_gc, None);
696 XCopyArea(dpy, scr->icon_tile_pixmap, pixmap, scr->copy_gc, 0, 0,
697 wPreferences.icon_size, wPreferences.icon_size, 0, 0);
698 drawIconTitle(scr, pixmap, title_height);
699 theight = title_height;
700 } else {
701 pixmap = None;
702 theight = 0;
703 XSetWindowBackgroundPixmap(dpy, icon->core->window, scr->icon_tile_pixmap);
706 XSetWindowBorderWidth(dpy, icon->icon_win, 0);
707 XReparentWindow(dpy, icon->icon_win, icon->core->window,
708 (wPreferences.icon_size - width) / 2,
709 theight + (wPreferences.icon_size - height - theight) / 2);
710 if (resize)
711 XResizeWindow(dpy, icon->icon_win, width, height);
713 XMapWindow(dpy, icon->icon_win);
714 XAddToSaveSet(dpy, icon->icon_win);
716 /* Save it */
717 icon->pixmap = pixmap;
719 if ((XGetWindowAttributes(dpy, icon->icon_win, &attr)) &&
720 (attr.all_event_masks & ButtonPressMask))
721 wHackedGrabButton(Button1, MOD_MASK, icon->core->window, True,
722 ButtonPressMask, GrabModeSync, GrabModeAsync,
723 None, wCursor[WCUR_ARROW]);
726 /* Get the Pixmap from the XWindow wm_hints */
727 static int get_pixmap_icon_from_wm_hints(WIcon *icon)
729 Window jw;
730 Pixmap pixmap;
731 unsigned int w, h, ju, d;
732 int ji, x, y;
733 WWindow *wwin = icon->owner;
734 WScreen *scr = icon->core->screen_ptr;
735 int title_height = WMFontHeight(scr->icon_title_font);
737 if (!XGetGeometry(dpy, wwin->wm_hints->icon_pixmap, &jw, &ji, &ji, &w, &h, &ju, &d)) {
738 icon->owner->wm_hints->flags &= ~IconPixmapHint;
739 return 1;
742 pixmap = XCreatePixmap(dpy, icon->core->window, wPreferences.icon_size,
743 wPreferences.icon_size, scr->w_depth);
744 XSetClipMask(dpy, scr->copy_gc, None);
745 XCopyArea(dpy, scr->icon_tile_pixmap, pixmap, scr->copy_gc, 0, 0,
746 wPreferences.icon_size, wPreferences.icon_size, 0, 0);
748 if (w > wPreferences.icon_size)
749 w = wPreferences.icon_size;
750 x = (wPreferences.icon_size - w) / 2;
752 if (icon->show_title && (title_height < wPreferences.icon_size)) {
753 drawIconTitle(scr, pixmap, title_height);
755 if (h > wPreferences.icon_size - title_height - 2) {
756 h = wPreferences.icon_size - title_height - 2;
757 y = title_height + 1;
758 } else {
759 y = (wPreferences.icon_size - h - title_height) / 2 + title_height + 1;
761 } else {
762 if (w > wPreferences.icon_size)
763 w = wPreferences.icon_size;
764 y = (wPreferences.icon_size - h) / 2;
767 if (wwin->wm_hints->flags & IconMaskHint)
768 XSetClipMask(dpy, scr->copy_gc, wwin->wm_hints->icon_mask);
770 XSetClipOrigin(dpy, scr->copy_gc, x, y);
772 if (d != scr->w_depth) {
773 XSetForeground(dpy, scr->copy_gc, scr->black_pixel);
774 XSetBackground(dpy, scr->copy_gc, scr->white_pixel);
775 XCopyPlane(dpy, wwin->wm_hints->icon_pixmap, pixmap, scr->copy_gc, 0, 0, w, h, x, y, 1);
776 } else {
777 XCopyArea(dpy, wwin->wm_hints->icon_pixmap, pixmap, scr->copy_gc, 0, 0, w, h, x, y);
780 XSetClipOrigin(dpy, scr->copy_gc, 0, 0);
782 icon->pixmap = pixmap;
783 return (0);
786 void wIconPaint(WIcon * icon)
788 WScreen *scr = icon->core->screen_ptr;
789 int x;
790 char *tmp;
792 if (icon->force_paint) {
793 icon->force_paint = 0;
794 wIconUpdate(icon);
795 return;
798 XClearWindow(dpy, icon->core->window);
800 /* draw the icon title */
801 if (icon->show_title && icon->icon_name != NULL) {
802 int l;
803 int w;
805 tmp = ShrinkString(scr->icon_title_font, icon->icon_name, wPreferences.icon_size - 4);
806 w = WMWidthOfString(scr->icon_title_font, tmp, l = strlen(tmp));
808 if (w > icon->core->width - 4)
809 x = (icon->core->width - 4) - w;
810 else
811 x = (icon->core->width - w) / 2;
813 WMDrawString(scr->wmscreen, icon->core->window, scr->icon_title_color,
814 scr->icon_title_font, x, 1, tmp, l);
815 wfree(tmp);
818 if (icon->selected)
819 XDrawRectangle(dpy, icon->core->window, scr->icon_select_gc, 0, 0,
820 icon->core->width - 1, icon->core->height - 1);
823 /******************************************************************/
825 static void miniwindowExpose(WObjDescriptor * desc, XEvent * event)
827 wIconPaint(desc->parent);
830 static void miniwindowDblClick(WObjDescriptor * desc, XEvent * event)
832 WIcon *icon = desc->parent;
834 assert(icon->owner != NULL);
836 wDeiconifyWindow(icon->owner);
839 static void miniwindowMouseDown(WObjDescriptor * desc, XEvent * event)
841 WIcon *icon = desc->parent;
842 WWindow *wwin = icon->owner;
843 XEvent ev;
844 int x = wwin->icon_x, y = wwin->icon_y;
845 int dx = event->xbutton.x, dy = event->xbutton.y;
846 int grabbed = 0;
847 int clickButton = event->xbutton.button;
848 Bool hasMoved = False;
850 if (WCHECK_STATE(WSTATE_MODAL))
851 return;
853 if (IsDoubleClick(icon->core->screen_ptr, event)) {
854 miniwindowDblClick(desc, event);
855 return;
858 if (event->xbutton.button == Button1) {
859 if (event->xbutton.state & MOD_MASK)
860 wLowerFrame(icon->core);
861 else
862 wRaiseFrame(icon->core);
863 if (event->xbutton.state & ShiftMask) {
864 wIconSelect(icon);
865 wSelectWindow(icon->owner, !wwin->flags.selected);
867 } else if (event->xbutton.button == Button3) {
868 WObjDescriptor *desc;
870 OpenMiniwindowMenu(wwin, event->xbutton.x_root, event->xbutton.y_root);
872 /* allow drag select of menu */
873 desc = &wwin->screen_ptr->window_menu->menu->descriptor;
874 event->xbutton.send_event = True;
875 (*desc->handle_mousedown) (desc, event);
877 return;
880 if (XGrabPointer(dpy, icon->core->window, False, ButtonMotionMask
881 | ButtonReleaseMask | ButtonPressMask, GrabModeAsync,
882 GrabModeAsync, None, None, CurrentTime) != GrabSuccess) {
884 while (1) {
885 WMMaskEvent(dpy, PointerMotionMask | ButtonReleaseMask | ButtonPressMask
886 | ButtonMotionMask | ExposureMask, &ev);
887 switch (ev.type) {
888 case Expose:
889 WMHandleEvent(&ev);
890 break;
892 case MotionNotify:
893 hasMoved = True;
894 if (!grabbed) {
895 if (abs(dx - ev.xmotion.x) >= MOVE_THRESHOLD
896 || abs(dy - ev.xmotion.y) >= MOVE_THRESHOLD) {
897 XChangeActivePointerGrab(dpy, ButtonMotionMask
898 | ButtonReleaseMask | ButtonPressMask,
899 wCursor[WCUR_MOVE], CurrentTime);
900 grabbed = 1;
901 } else {
902 break;
905 x = ev.xmotion.x_root - dx;
906 y = ev.xmotion.y_root - dy;
907 XMoveWindow(dpy, icon->core->window, x, y);
908 break;
910 case ButtonPress:
911 break;
913 case ButtonRelease:
914 if (ev.xbutton.button != clickButton)
915 break;
917 if (wwin->icon_x != x || wwin->icon_y != y)
918 wwin->flags.icon_moved = 1;
920 XMoveWindow(dpy, icon->core->window, x, y);
922 wwin->icon_x = x;
923 wwin->icon_y = y;
924 XUngrabPointer(dpy, CurrentTime);
926 if (wPreferences.auto_arrange_icons)
927 wArrangeIcons(wwin->screen_ptr, True);
928 if (wPreferences.single_click && !hasMoved)
929 miniwindowDblClick(desc, event);
930 return;