Fixed crash when changing icon to non-existent file
[wmaker-crm.git] / src / icon.c
blob0370d71bf6c8000b0d48bca72f1da9b5cbe7188c
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 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 *icon_create_for_wwindow(WWindow *wwin)
106 WScreen *scr = wwin->screen_ptr;
107 WIcon *icon;
108 char *file;
110 icon = icon_create_core(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
130 icon->icon_name = wNETWMGetIconName(wwin->client_win);
131 if (icon->icon_name)
132 wwin->flags.net_has_icon_title = 1;
133 else
134 wGetIconName(dpy, wwin->client_win, &icon->icon_name);
136 /* Get the application icon, default included */
137 file = get_default_icon_filename(scr, wwin->wm_instance, wwin->wm_class, NULL, True);
138 if (file) {
139 icon->file = wstrdup(file);
140 icon->file_image = get_default_icon_rimage(scr, icon->file, wPreferences.icon_size);
141 wfree(file);
144 icon->tile_type = TILE_NORMAL;
146 wIconUpdate(icon);
148 WMAddNotificationObserver(appearanceObserver, icon, WNIconAppearanceSettingsChanged, icon);
149 WMAddNotificationObserver(tileObserver, icon, WNIconTileSettingsChanged, icon);
151 return icon;
154 WIcon *icon_create_for_dock(WScreen *scr, char *command, char *wm_instance, char *wm_class, int tile)
156 WIcon *icon;
157 char *file = NULL;
159 icon = icon_create_core(scr, 0, 0);
161 /* Search the icon using instance and class, without default icon */
162 file = get_default_icon_filename(scr, wm_instance, wm_class, command, False);
163 if (file) {
164 icon->file = wstrdup(file);
165 icon->file_image = get_default_icon_rimage(scr, icon->file, wPreferences.icon_size);
166 wfree(file);
169 icon->tile_type = tile;
171 wIconUpdate(icon);
173 WMAddNotificationObserver(appearanceObserver, icon, WNIconAppearanceSettingsChanged, icon);
174 WMAddNotificationObserver(tileObserver, icon, WNIconTileSettingsChanged, icon);
176 return icon;
179 static WIcon *icon_create_core(WScreen *scr, int coord_x, int coord_y)
181 WIcon *icon;
182 unsigned long vmask = 0;
183 XSetWindowAttributes attribs;
185 icon = wmalloc(sizeof(WIcon));
186 icon->core = wCoreCreateTopLevel(scr,
187 coord_x,
188 coord_y,
189 wPreferences.icon_size,
190 wPreferences.icon_size,
191 0, scr->w_depth, scr->w_visual, scr->w_colormap);
193 if (wPreferences.use_saveunders) {
194 vmask = CWSaveUnder;
195 attribs.save_under = True;
198 /* a white border for selecting it */
199 vmask |= CWBorderPixel;
200 attribs.border_pixel = scr->white_pixel;
202 XChangeWindowAttributes(dpy, icon->core->window, vmask, &attribs);
204 /* will be overriden if this is a application icon */
205 icon->core->descriptor.handle_mousedown = miniwindowMouseDown;
206 icon->core->descriptor.handle_expose = miniwindowExpose;
207 icon->core->descriptor.parent_type = WCLASS_MINIWINDOW;
208 icon->core->descriptor.parent = icon;
210 icon->core->stacking = wmalloc(sizeof(WStacking));
211 icon->core->stacking->above = NULL;
212 icon->core->stacking->under = NULL;
213 icon->core->stacking->window_level = NORMAL_ICON_LEVEL;
214 icon->core->stacking->child_of = NULL;
216 /* Icon image */
217 icon->file = NULL;
218 icon->file_image = NULL;
220 return icon;
223 void wIconDestroy(WIcon * icon)
225 WCoreWindow *core = icon->core;
226 WScreen *scr = core->screen_ptr;
228 WMRemoveNotificationObserver(icon);
230 if (icon->handlerID)
231 WMDeleteTimerHandler(icon->handlerID);
233 if (icon->icon_win) {
234 int x = 0, y = 0;
236 if (icon->owner) {
237 x = icon->owner->icon_x;
238 y = icon->owner->icon_y;
240 XUnmapWindow(dpy, icon->icon_win);
241 XReparentWindow(dpy, icon->icon_win, scr->root_win, x, y);
243 if (icon->icon_name)
244 XFree(icon->icon_name);
246 if (icon->pixmap)
247 XFreePixmap(dpy, icon->pixmap);
249 if (icon->file)
250 wfree(icon->file);
252 if (icon->file_image != NULL)
253 RReleaseImage(icon->file_image);
255 wCoreDestroy(icon->core);
256 wfree(icon);
259 static void drawIconTitle(WScreen * scr, Pixmap pixmap, int height)
261 XFillRectangle(dpy, pixmap, scr->icon_title_texture->normal_gc, 0, 0, wPreferences.icon_size, height + 1);
262 XDrawLine(dpy, pixmap, scr->icon_title_texture->light_gc, 0, 0, wPreferences.icon_size, 0);
263 XDrawLine(dpy, pixmap, scr->icon_title_texture->light_gc, 0, 0, 0, height + 1);
264 XDrawLine(dpy, pixmap, scr->icon_title_texture->dim_gc,
265 wPreferences.icon_size - 1, 0, wPreferences.icon_size - 1, height + 1);
268 static Pixmap makeIcon(WScreen *scr, RImage *icon, int titled, int shadowed, int tileType, int highlighted)
270 RImage *tile;
271 Pixmap pixmap;
272 int x, y, sx, sy;
273 unsigned w, h;
274 int theight = 0;
276 if (tileType == TILE_NORMAL) {
277 tile = RCloneImage(scr->icon_tile);
278 } else {
279 assert(scr->clip_tile);
280 tile = RCloneImage(scr->clip_tile);
283 if (icon) {
284 w = (icon->width > wPreferences.icon_size)
285 ? wPreferences.icon_size : icon->width;
286 x = (wPreferences.icon_size - w) / 2;
287 sx = (icon->width - w) / 2;
289 if (titled)
290 theight = WMFontHeight(scr->icon_title_font);
292 h = (icon->height + theight > wPreferences.icon_size
293 ? wPreferences.icon_size - theight : icon->height);
294 y = theight + (wPreferences.icon_size - theight - h) / 2;
295 sy = (icon->height - h) / 2;
297 RCombineArea(tile, icon, sx, sy, w, h, x, y);
300 if (shadowed) {
301 RColor color;
303 color.red = scr->icon_back_texture->light.red >> 8;
304 color.green = scr->icon_back_texture->light.green >> 8;
305 color.blue = scr->icon_back_texture->light.blue >> 8;
306 color.alpha = 150; /* about 60% */
307 RClearImage(tile, &color);
310 if (highlighted) {
311 RColor color;
313 color.red = color.green = color.blue = 0;
314 color.alpha = 160;
315 RLightImage(tile, &color);
318 if (!RConvertImage(scr->rcontext, tile, &pixmap))
319 wwarning(_("error rendering image:%s"), RMessageForError(RErrorCode));
321 RReleaseImage(tile);
323 if (titled)
324 drawIconTitle(scr, pixmap, theight);
326 return pixmap;
329 void wIconChangeTitle(WIcon * icon, char *new_title)
331 int changed;
333 changed = (new_title == NULL && icon->icon_name != NULL)
334 || (new_title != NULL && icon->icon_name == NULL);
336 if (icon->icon_name != NULL)
337 XFree(icon->icon_name);
339 icon->icon_name = new_title;
341 if (changed)
342 icon->force_paint = 1;
343 wIconPaint(icon);
346 RImage *wIconValidateIconSize(RImage *icon, int max_size)
348 RImage *nimage;
350 if (!icon)
351 return NULL;
353 /* We should hold "ICON_BORDER" (~2) pixels to include the icon border */
354 if ((icon->width - max_size) > -ICON_BORDER ||
355 (icon->height - max_size) > -ICON_BORDER) {
356 nimage = RScaleImage(icon, max_size - ICON_BORDER,
357 (icon->height * (max_size - ICON_BORDER) / icon->width));
358 RReleaseImage(icon);
359 icon = nimage;
362 return icon;
365 Bool wIconChangeImageFile(WIcon * icon, char *file)
367 WScreen *scr = icon->core->screen_ptr;
368 RImage *image;
369 char *path;
370 int error = 0;
372 if (icon->file_image) {
373 RReleaseImage(icon->file_image);
374 icon->file_image = NULL;
377 if (!file) {
378 wIconUpdate(icon);
379 return True;
382 path = FindImage(wPreferences.icon_path, file);
384 if (path && (image = RLoadImage(scr->rcontext, path, 0))) {
385 icon->file_image = wIconValidateIconSize(image, wPreferences.icon_size);
386 wIconUpdate(icon);
387 } else {
388 error = 1;
391 if (path)
392 wfree(path);
394 return !error;
397 static char *get_name_for_wwin(WWindow *wwin)
399 return get_name_for_instance_class(wwin->wm_instance, wwin->wm_class);
402 char *get_name_for_instance_class(char *wm_instance, char *wm_class)
404 char *suffix;
405 int len;
407 if (wm_class && wm_instance) {
408 len = strlen(wm_class) + strlen(wm_instance) + 2;
409 suffix = wmalloc(len);
410 snprintf(suffix, len, "%s.%s", wm_instance, wm_class);
411 } else if (wm_class) {
412 len = strlen(wm_class) + 1;
413 suffix = wmalloc(len);
414 snprintf(suffix, len, "%s", wm_class);
415 } else if (wm_instance) {
416 len = strlen(wm_instance) + 1;
417 suffix = wmalloc(len);
418 snprintf(suffix, len, "%s", wm_instance);
419 } else {
420 return NULL;
423 return suffix;
426 static char *get_icon_cache_path(void)
428 char *prefix, *path;
429 int len, ret;
431 prefix = wusergnusteppath();
432 len = strlen(prefix) + strlen(CACHE_ICON_PATH) + 2;
433 path = wmalloc(len);
434 snprintf(path, len, "%s%s/", prefix, CACHE_ICON_PATH);
436 /* If the folder exists, exit */
437 if (access(path, F_OK) == 0)
438 return path;
440 /* Create the folder */
441 ret = wmkdirhier((const char *) path);
443 /* Exit 1 on success, 0 on failure */
444 if (ret == 1)
445 return path;
447 /* Fail */
448 wfree(path);
449 return NULL;
452 static RImage *get_wwindow_image_from_wmhints(WWindow *wwin, WIcon *icon)
454 RImage *image = NULL;
455 XWMHints *hints = wwin->wm_hints;
457 if (hints && (hints->flags & IconPixmapHint) && hints->icon_pixmap != None)
458 image = RCreateImageFromDrawable(icon->core->screen_ptr->rcontext,
459 hints->icon_pixmap,
460 (hints->flags & IconMaskHint)
461 ? hints->icon_mask : None);
463 return image;
467 * wIconStore--
468 * Stores the client supplied icon at CACHE_ICON_PATH
469 * and returns the path for that icon. Returns NULL if there is no
470 * client supplied icon or on failure.
472 * Side effects:
473 * New directories might be created.
475 char *wIconStore(WIcon * icon)
477 char *path, *dir_path, *file;
478 int len = 0;
479 RImage *image = NULL;
480 WWindow *wwin = icon->owner;
482 if (!wwin)
483 return NULL;
485 dir_path = get_icon_cache_path();
486 if (!dir_path)
487 return NULL;
489 file = get_name_for_wwin(wwin);
490 if (!file) {
491 wfree(dir_path);
492 return NULL;
495 len = strlen(dir_path) + strlen(file) + 5;
496 path = wmalloc(len);
497 snprintf(path, len, "%s%s.xpm", dir_path, file);
498 wfree(dir_path);
499 wfree(file);
501 /* If icon exists, exit */
502 if (access(path, F_OK) == 0)
503 return path;
505 if (wwin->net_icon_image)
506 image = RRetainImage(wwin->net_icon_image);
507 else
508 image = get_wwindow_image_from_wmhints(wwin, icon);
510 if (!image) {
511 wfree(path);
512 return NULL;
515 if (!RSaveImage(image, path, "XPM")) {
516 wfree(path);
517 path = NULL;
520 RReleaseImage(image);
522 return path;
525 static void cycleColor(void *data)
527 WIcon *icon = (WIcon *) data;
528 WScreen *scr = icon->core->screen_ptr;
529 XGCValues gcv;
531 icon->step--;
532 gcv.dash_offset = icon->step;
533 XChangeGC(dpy, scr->icon_select_gc, GCDashOffset, &gcv);
535 XDrawRectangle(dpy, icon->core->window, scr->icon_select_gc, 0, 0,
536 icon->core->width - 1, icon->core->height - 1);
537 icon->handlerID = WMAddTimerHandler(COLOR_CYCLE_DELAY, cycleColor, icon);
540 #ifdef NEWAPPICON
541 void wIconSetHighlited(WIcon *icon, Bool flag)
543 if (icon->highlighted == flag)
544 return;
546 icon->highlighted = flag;
547 icon->force_paint = True;
548 wIconPaint(icon);
550 #endif
552 void wIconSelect(WIcon * icon)
554 WScreen *scr = icon->core->screen_ptr;
555 icon->selected = !icon->selected;
557 if (icon->selected) {
558 icon->step = 0;
559 if (!wPreferences.dont_blink)
560 icon->handlerID = WMAddTimerHandler(10, cycleColor, icon);
561 else
562 XDrawRectangle(dpy, icon->core->window, scr->icon_select_gc, 0, 0,
563 icon->core->width - 1, icon->core->height - 1);
564 } else {
565 if (icon->handlerID) {
566 WMDeleteTimerHandler(icon->handlerID);
567 icon->handlerID = NULL;
569 XClearArea(dpy, icon->core->window, 0, 0, icon->core->width, icon->core->height, True);
573 void wIconUpdate(WIcon * icon)
575 WScreen *scr = icon->core->screen_ptr;
576 WWindow *wwin = icon->owner;
578 assert(scr->icon_tile != NULL);
580 if (icon->pixmap != None)
581 XFreePixmap(dpy, icon->pixmap);
583 icon->pixmap = None;
585 if (wwin && WFLAGP(wwin, always_user_icon)) {
586 /* Forced use user_icon */
587 get_pixmap_icon_from_user_icon(scr, icon);
588 } else if (icon->icon_win != None) {
589 /* Get the Pixmap from the WIcon */
590 get_pixmap_icon_from_icon_win(icon);
591 } else if (wwin && wwin->net_icon_image) {
592 /* Use _NET_WM_ICON icon */
593 icon->pixmap = makeIcon(scr, wwin->net_icon_image, icon->show_title,
594 icon->shadowed, icon->tile_type, icon->highlighted);
595 } else if (wwin && wwin->wm_hints && (wwin->wm_hints->flags & IconPixmapHint)) {
596 /* Get the Pixmap from the wm_hints, else, from the user */
597 if (get_pixmap_icon_from_wm_hints(scr, wwin, icon))
598 get_pixmap_icon_from_user_icon(scr, icon);
599 } else {
600 /* Get the Pixmap from the user */
601 get_pixmap_icon_from_user_icon(scr, icon);
604 /* No pixmap, set default background */
605 if (icon->pixmap != None)
606 XSetWindowBackgroundPixmap(dpy, icon->core->window, icon->pixmap);
608 /* Paint it */
609 XClearWindow(dpy, icon->core->window);
610 wIconPaint(icon);
613 void get_pixmap_icon_from_user_icon(WScreen *scr, WIcon * icon)
615 RImage *image = NULL;
616 char *path;
617 char *file;
619 if (icon->file_image) {
620 icon->pixmap = makeIcon(scr, icon->file_image, icon->show_title,
621 icon->shadowed, icon->tile_type, icon->highlighted);
622 } else {
623 /* make default icons */
624 if (!scr->def_icon_pixmap) {
625 /* Get the default icon */
626 file = wDefaultGetIconFile(NULL, NULL, True);
627 if (file) {
628 path = FindImage(wPreferences.icon_path, file);
629 if (path) {
630 image = RLoadImage(scr->rcontext, path, 0);
631 if (!image)
632 wwarning(_("could not load default icon \"%s\":%s"),
633 file, RMessageForError(RErrorCode));
634 wfree(path);
635 } else {
636 wwarning(_("could not find default icon \"%s\""), file);
640 image = wIconValidateIconSize(image, wPreferences.icon_size);
641 scr->def_icon_pixmap = makeIcon(scr, image, False, False, icon->tile_type, icon->highlighted);
642 scr->def_ticon_pixmap = makeIcon(scr, image, True, False, icon->tile_type, icon->highlighted);
643 if (image)
644 RReleaseImage(image);
647 if (icon->show_title)
648 XSetWindowBackgroundPixmap(dpy, icon->core->window, scr->def_ticon_pixmap);
649 else
650 XSetWindowBackgroundPixmap(dpy, icon->core->window, scr->def_icon_pixmap);
652 icon->pixmap = None;
656 /* Get the Pixmap from the WIcon of the WWindow */
657 void get_pixmap_icon_from_icon_win(WIcon * icon)
659 XWindowAttributes attr;
660 WScreen *scr = icon->core->screen_ptr;
661 int title_height = WMFontHeight(scr->icon_title_font);
662 unsigned int width, height, depth;
663 int theight;
664 int resize = 0;
665 Pixmap pixmap;
667 getSize(icon->icon_win, &width, &height, &depth);
669 if (width > wPreferences.icon_size) {
670 resize = 1;
671 width = wPreferences.icon_size;
674 if (height > wPreferences.icon_size) {
675 resize = 1;
676 height = wPreferences.icon_size;
679 if (icon->show_title && (height + title_height < wPreferences.icon_size)) {
680 pixmap = XCreatePixmap(dpy, scr->w_win, wPreferences.icon_size,
681 wPreferences.icon_size, scr->w_depth);
682 XSetClipMask(dpy, scr->copy_gc, None);
683 XCopyArea(dpy, scr->icon_tile_pixmap, pixmap, scr->copy_gc, 0, 0,
684 wPreferences.icon_size, wPreferences.icon_size, 0, 0);
685 drawIconTitle(scr, pixmap, title_height);
686 theight = title_height;
687 } else {
688 pixmap = None;
689 theight = 0;
690 XSetWindowBackgroundPixmap(dpy, icon->core->window, scr->icon_tile_pixmap);
693 XSetWindowBorderWidth(dpy, icon->icon_win, 0);
694 XReparentWindow(dpy, icon->icon_win, icon->core->window,
695 (wPreferences.icon_size - width) / 2,
696 theight + (wPreferences.icon_size - height - theight) / 2);
697 if (resize)
698 XResizeWindow(dpy, icon->icon_win, width, height);
700 XMapWindow(dpy, icon->icon_win);
701 XAddToSaveSet(dpy, icon->icon_win);
703 /* Save it */
704 icon->pixmap = pixmap;
706 if ((XGetWindowAttributes(dpy, icon->icon_win, &attr)) &&
707 (attr.all_event_masks & ButtonPressMask))
708 wHackedGrabButton(Button1, MOD_MASK, icon->core->window, True,
709 ButtonPressMask, GrabModeSync, GrabModeAsync,
710 None, wCursor[WCUR_ARROW]);
713 /* Get the Pixmap from the XWindow wm_hints */
714 int get_pixmap_icon_from_wm_hints(WScreen *scr, WWindow *wwin, WIcon *icon)
716 Window jw;
717 Pixmap pixmap;
718 unsigned int w, h, ju, d;
719 int ji, x, y;
720 int title_height = WMFontHeight(scr->icon_title_font);
722 if (!XGetGeometry(dpy, wwin->wm_hints->icon_pixmap, &jw, &ji, &ji, &w, &h, &ju, &d)) {
723 icon->owner->wm_hints->flags &= ~IconPixmapHint;
724 return 1;
727 pixmap = XCreatePixmap(dpy, icon->core->window, wPreferences.icon_size,
728 wPreferences.icon_size, scr->w_depth);
729 XSetClipMask(dpy, scr->copy_gc, None);
730 XCopyArea(dpy, scr->icon_tile_pixmap, pixmap, scr->copy_gc, 0, 0,
731 wPreferences.icon_size, wPreferences.icon_size, 0, 0);
733 if (w > wPreferences.icon_size)
734 w = wPreferences.icon_size;
735 x = (wPreferences.icon_size - w) / 2;
737 if (icon->show_title && (title_height < wPreferences.icon_size)) {
738 drawIconTitle(scr, pixmap, title_height);
740 if (h > wPreferences.icon_size - title_height - 2) {
741 h = wPreferences.icon_size - title_height - 2;
742 y = title_height + 1;
743 } else {
744 y = (wPreferences.icon_size - h - title_height) / 2 + title_height + 1;
746 } else {
747 if (w > wPreferences.icon_size)
748 w = wPreferences.icon_size;
749 y = (wPreferences.icon_size - h) / 2;
752 if (wwin->wm_hints->flags & IconMaskHint)
753 XSetClipMask(dpy, scr->copy_gc, wwin->wm_hints->icon_mask);
755 XSetClipOrigin(dpy, scr->copy_gc, x, y);
757 if (d != scr->w_depth) {
758 XSetForeground(dpy, scr->copy_gc, scr->black_pixel);
759 XSetBackground(dpy, scr->copy_gc, scr->white_pixel);
760 XCopyPlane(dpy, wwin->wm_hints->icon_pixmap, pixmap, scr->copy_gc, 0, 0, w, h, x, y, 1);
761 } else {
762 XCopyArea(dpy, wwin->wm_hints->icon_pixmap, pixmap, scr->copy_gc, 0, 0, w, h, x, y);
765 XSetClipOrigin(dpy, scr->copy_gc, 0, 0);
767 icon->pixmap = pixmap;
768 return (0);
771 void wIconPaint(WIcon * icon)
773 WScreen *scr = icon->core->screen_ptr;
774 int x;
775 char *tmp;
777 if (icon->force_paint) {
778 icon->force_paint = 0;
779 wIconUpdate(icon);
780 return;
783 XClearWindow(dpy, icon->core->window);
785 /* draw the icon title */
786 if (icon->show_title && icon->icon_name != NULL) {
787 int l;
788 int w;
790 tmp = ShrinkString(scr->icon_title_font, icon->icon_name, wPreferences.icon_size - 4);
791 w = WMWidthOfString(scr->icon_title_font, tmp, l = strlen(tmp));
793 if (w > icon->core->width - 4)
794 x = (icon->core->width - 4) - w;
795 else
796 x = (icon->core->width - w) / 2;
798 WMDrawString(scr->wmscreen, icon->core->window, scr->icon_title_color,
799 scr->icon_title_font, x, 1, tmp, l);
800 wfree(tmp);
803 if (icon->selected)
804 XDrawRectangle(dpy, icon->core->window, scr->icon_select_gc, 0, 0,
805 icon->core->width - 1, icon->core->height - 1);
808 /******************************************************************/
810 static void miniwindowExpose(WObjDescriptor * desc, XEvent * event)
812 wIconPaint(desc->parent);
815 static void miniwindowDblClick(WObjDescriptor * desc, XEvent * event)
817 WIcon *icon = desc->parent;
819 assert(icon->owner != NULL);
821 wDeiconifyWindow(icon->owner);
824 static void miniwindowMouseDown(WObjDescriptor * desc, XEvent * event)
826 WIcon *icon = desc->parent;
827 WWindow *wwin = icon->owner;
828 XEvent ev;
829 int x = wwin->icon_x, y = wwin->icon_y;
830 int dx = event->xbutton.x, dy = event->xbutton.y;
831 int grabbed = 0;
832 int clickButton = event->xbutton.button;
833 Bool hasMoved = False;
835 if (WCHECK_STATE(WSTATE_MODAL))
836 return;
838 if (IsDoubleClick(icon->core->screen_ptr, event)) {
839 miniwindowDblClick(desc, event);
840 return;
843 if (event->xbutton.button == Button1) {
844 if (event->xbutton.state & MOD_MASK)
845 wLowerFrame(icon->core);
846 else
847 wRaiseFrame(icon->core);
848 if (event->xbutton.state & ShiftMask) {
849 wIconSelect(icon);
850 wSelectWindow(icon->owner, !wwin->flags.selected);
852 } else if (event->xbutton.button == Button3) {
853 WObjDescriptor *desc;
855 OpenMiniwindowMenu(wwin, event->xbutton.x_root, event->xbutton.y_root);
857 /* allow drag select of menu */
858 desc = &wwin->screen_ptr->window_menu->menu->descriptor;
859 event->xbutton.send_event = True;
860 (*desc->handle_mousedown) (desc, event);
862 return;
865 if (XGrabPointer(dpy, icon->core->window, False, ButtonMotionMask
866 | ButtonReleaseMask | ButtonPressMask, GrabModeAsync,
867 GrabModeAsync, None, None, CurrentTime) != GrabSuccess) {
869 while (1) {
870 WMMaskEvent(dpy, PointerMotionMask | ButtonReleaseMask | ButtonPressMask
871 | ButtonMotionMask | ExposureMask, &ev);
872 switch (ev.type) {
873 case Expose:
874 WMHandleEvent(&ev);
875 break;
877 case MotionNotify:
878 hasMoved = True;
879 if (!grabbed) {
880 if (abs(dx - ev.xmotion.x) >= MOVE_THRESHOLD
881 || abs(dy - ev.xmotion.y) >= MOVE_THRESHOLD) {
882 XChangeActivePointerGrab(dpy, ButtonMotionMask
883 | ButtonReleaseMask | ButtonPressMask,
884 wCursor[WCUR_MOVE], CurrentTime);
885 grabbed = 1;
886 } else {
887 break;
890 x = ev.xmotion.x_root - dx;
891 y = ev.xmotion.y_root - dy;
892 XMoveWindow(dpy, icon->core->window, x, y);
893 break;
895 case ButtonPress:
896 break;
898 case ButtonRelease:
899 if (ev.xbutton.button != clickButton)
900 break;
902 if (wwin->icon_x != x || wwin->icon_y != y)
903 wwin->flags.icon_moved = 1;
905 XMoveWindow(dpy, icon->core->window, x, y);
907 wwin->icon_x = x;
908 wwin->icon_y = y;
909 XUngrabPointer(dpy, CurrentTime);
911 if (wPreferences.auto_arrange_icons)
912 wArrangeIcons(wwin->screen_ptr, True);
913 if (wPreferences.single_click && !hasMoved)
914 miniwindowDblClick(desc, event);
915 return;