WindowMaker: New function get_pixmap_icon_from_wm_hints
[wmaker-crm.git] / src / icon.c
blob8663e699c5ad52e0a672e92d12b694b7cb93e015
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
53 extern Cursor wCursor[WCUR_LAST];
55 static void miniwindowExpose(WObjDescriptor * desc, XEvent * event);
56 static void miniwindowMouseDown(WObjDescriptor * desc, XEvent * event);
57 static void miniwindowDblClick(WObjDescriptor * desc, XEvent * event);
59 void get_pixmap_icon_from_icon_win(WIcon *icon);
60 int get_pixmap_icon_from_wm_hints(WScreen *scr, WWindow *wwin, WIcon *icon);
61 /****** Notification Observers ******/
63 static void appearanceObserver(void *self, WMNotification * notif)
65 WIcon *icon = (WIcon *) self;
66 uintptr_t flags = (uintptr_t)WMGetNotificationClientData(notif);
68 if (flags & WTextureSettings) {
69 icon->force_paint = 1;
71 if (flags & WFontSettings) {
72 icon->force_paint = 1;
75 if (flags & WColorSettings) {
79 wIconPaint(icon);
81 /* so that the appicon expose handlers will paint the appicon specific
82 * stuff */
83 XClearArea(dpy, icon->core->window, 0, 0, icon->core->width, icon->core->height, True);
86 static void tileObserver(void *self, WMNotification * notif)
88 WIcon *icon = (WIcon *) self;
90 icon->force_paint = 1;
91 wIconPaint(icon);
93 XClearArea(dpy, icon->core->window, 0, 0, 1, 1, True);
96 /************************************/
98 INLINE static void getSize(Drawable d, unsigned int *w, unsigned int *h, unsigned int *dep)
100 Window rjunk;
101 int xjunk, yjunk;
102 unsigned int bjunk;
104 XGetGeometry(dpy, d, &rjunk, &xjunk, &yjunk, w, h, &bjunk, dep);
107 WIcon *wIconCreate(WWindow * wwin)
109 WScreen *scr = wwin->screen_ptr;
110 WIcon *icon;
111 char *file;
112 unsigned long vmask = 0;
113 XSetWindowAttributes attribs;
115 icon = wmalloc(sizeof(WIcon));
116 memset(icon, 0, sizeof(WIcon));
117 icon->core = wCoreCreateTopLevel(scr, wwin->icon_x, wwin->icon_y,
118 wPreferences.icon_size, wPreferences.icon_size, 0);
120 if (wPreferences.use_saveunders) {
121 vmask |= CWSaveUnder;
122 attribs.save_under = True;
124 /* a white border for selecting it */
125 vmask |= CWBorderPixel;
126 attribs.border_pixel = scr->white_pixel;
128 XChangeWindowAttributes(dpy, icon->core->window, vmask, &attribs);
130 /* will be overriden if this is an application icon */
131 icon->core->descriptor.handle_mousedown = miniwindowMouseDown;
132 icon->core->descriptor.handle_expose = miniwindowExpose;
133 icon->core->descriptor.parent_type = WCLASS_MINIWINDOW;
134 icon->core->descriptor.parent = icon;
136 icon->core->stacking = wmalloc(sizeof(WStacking));
137 icon->core->stacking->above = NULL;
138 icon->core->stacking->under = NULL;
139 icon->core->stacking->window_level = NORMAL_ICON_LEVEL;
140 icon->core->stacking->child_of = NULL;
142 icon->owner = wwin;
143 if (wwin->wm_hints && (wwin->wm_hints->flags & IconWindowHint)) {
144 if (wwin->client_win == wwin->main_window) {
145 WApplication *wapp;
146 /* do not let miniwindow steal app-icon's icon window */
147 wapp = wApplicationOf(wwin->client_win);
148 if (!wapp || wapp->app_icon == NULL)
149 icon->icon_win = wwin->wm_hints->icon_window;
150 } else {
151 icon->icon_win = wwin->wm_hints->icon_window;
154 #ifdef NO_MINIWINDOW_TITLES
155 icon->show_title = 0;
156 #else
157 icon->show_title = 1;
158 #endif
159 icon->file_image = wDefaultGetImage(scr, wwin->wm_instance, wwin->wm_class, wPreferences.icon_size);
161 file = wDefaultGetIconFile(scr, wwin->wm_instance, wwin->wm_class, False);
162 if (file) {
163 icon->file = wstrdup(file);
166 icon->icon_name = wNETWMGetIconName(wwin->client_win);
167 if (icon->icon_name)
168 wwin->flags.net_has_icon_title = 1;
169 else
170 wGetIconName(dpy, wwin->client_win, &icon->icon_name);
172 icon->tile_type = TILE_NORMAL;
174 wIconUpdate(icon);
176 XFlush(dpy);
178 WMAddNotificationObserver(appearanceObserver, icon, WNIconAppearanceSettingsChanged, icon);
179 WMAddNotificationObserver(tileObserver, icon, WNIconTileSettingsChanged, icon);
180 return icon;
183 WIcon *wIconCreateWithIconFile(WScreen * scr, char *iconfile, int tile)
185 WIcon *icon;
186 unsigned long vmask = 0;
187 XSetWindowAttributes attribs;
189 icon = wmalloc(sizeof(WIcon));
190 memset(icon, 0, sizeof(WIcon));
191 icon->core = wCoreCreateTopLevel(scr, 0, 0, wPreferences.icon_size, wPreferences.icon_size, 0);
192 if (wPreferences.use_saveunders) {
193 vmask = CWSaveUnder;
194 attribs.save_under = True;
196 /* a white border for selecting it */
197 vmask |= CWBorderPixel;
198 attribs.border_pixel = scr->white_pixel;
200 XChangeWindowAttributes(dpy, icon->core->window, vmask, &attribs);
202 /* will be overriden if this is a application icon */
203 icon->core->descriptor.handle_mousedown = miniwindowMouseDown;
204 icon->core->descriptor.handle_expose = miniwindowExpose;
205 icon->core->descriptor.parent_type = WCLASS_MINIWINDOW;
206 icon->core->descriptor.parent = icon;
208 icon->core->stacking = wmalloc(sizeof(WStacking));
209 icon->core->stacking->above = NULL;
210 icon->core->stacking->under = NULL;
211 icon->core->stacking->window_level = NORMAL_ICON_LEVEL;
212 icon->core->stacking->child_of = NULL;
214 if (iconfile) {
215 icon->file_image = RLoadImage(scr->rcontext, iconfile, 0);
216 if (!icon->file_image) {
217 wwarning(_("error loading image file \"%s\": %s"), iconfile, RMessageForError(RErrorCode));
220 icon->file_image = wIconValidateIconSize(scr, icon->file_image, wPreferences.icon_size);
222 icon->file = wstrdup(iconfile);
225 icon->tile_type = tile;
227 wIconUpdate(icon);
229 WMAddNotificationObserver(appearanceObserver, icon, WNIconAppearanceSettingsChanged, icon);
230 WMAddNotificationObserver(tileObserver, icon, WNIconTileSettingsChanged, icon);
232 return icon;
235 void wIconDestroy(WIcon * icon)
237 WCoreWindow *core = icon->core;
238 WScreen *scr = core->screen_ptr;
240 WMRemoveNotificationObserver(icon);
242 if (icon->handlerID)
243 WMDeleteTimerHandler(icon->handlerID);
245 if (icon->icon_win) {
246 int x = 0, y = 0;
248 if (icon->owner) {
249 x = icon->owner->icon_x;
250 y = icon->owner->icon_y;
252 XUnmapWindow(dpy, icon->icon_win);
253 XReparentWindow(dpy, icon->icon_win, scr->root_win, x, y);
255 if (icon->icon_name)
256 XFree(icon->icon_name);
258 if (icon->pixmap)
259 XFreePixmap(dpy, icon->pixmap);
261 if (icon->file)
262 wfree(icon->file);
264 if (icon->file_image != NULL)
265 RReleaseImage(icon->file_image);
267 wCoreDestroy(icon->core);
268 wfree(icon);
271 static void drawIconTitle(WScreen * scr, Pixmap pixmap, int height)
273 XFillRectangle(dpy, pixmap, scr->icon_title_texture->normal_gc, 0, 0, wPreferences.icon_size, height + 1);
274 XDrawLine(dpy, pixmap, scr->icon_title_texture->light_gc, 0, 0, wPreferences.icon_size, 0);
275 XDrawLine(dpy, pixmap, scr->icon_title_texture->light_gc, 0, 0, 0, height + 1);
276 XDrawLine(dpy, pixmap, scr->icon_title_texture->dim_gc,
277 wPreferences.icon_size - 1, 0, wPreferences.icon_size - 1, height + 1);
280 static Pixmap makeIcon(WScreen *scr, RImage *icon, int titled, int shadowed, int tileType, int highlighted)
282 RImage *tile;
283 Pixmap pixmap;
284 int x, y, sx, sy;
285 unsigned w, h;
286 int theight = 0;
288 if (tileType == TILE_NORMAL) {
289 tile = RCloneImage(scr->icon_tile);
290 } else {
291 assert(scr->clip_tile);
292 tile = RCloneImage(scr->clip_tile);
295 if (icon) {
296 w = (icon->width > wPreferences.icon_size)
297 ? wPreferences.icon_size : icon->width;
298 x = (wPreferences.icon_size - w) / 2;
299 sx = (icon->width - w) / 2;
301 if (titled)
302 theight = WMFontHeight(scr->icon_title_font);
304 h = (icon->height + theight > wPreferences.icon_size
305 ? wPreferences.icon_size - theight : icon->height);
306 y = theight + (wPreferences.icon_size - theight - h) / 2;
307 sy = (icon->height - h) / 2;
309 RCombineArea(tile, icon, sx, sy, w, h, x, y);
312 if (shadowed) {
313 RColor color;
315 color.red = scr->icon_back_texture->light.red >> 8;
316 color.green = scr->icon_back_texture->light.green >> 8;
317 color.blue = scr->icon_back_texture->light.blue >> 8;
318 color.alpha = 150; /* about 60% */
319 RClearImage(tile, &color);
322 if (highlighted) {
323 RColor color;
325 color.red = color.green = color.blue = 0;
326 color.alpha = 160;
327 RLightImage(tile, &color);
330 if (!RConvertImage(scr->rcontext, tile, &pixmap))
331 wwarning(_("error rendering image:%s"), RMessageForError(RErrorCode));
333 RReleaseImage(tile);
335 if (titled)
336 drawIconTitle(scr, pixmap, theight);
338 return pixmap;
341 void wIconChangeTitle(WIcon * icon, char *new_title)
343 int changed;
345 changed = (new_title == NULL && icon->icon_name != NULL)
346 || (new_title != NULL && icon->icon_name == NULL);
348 if (icon->icon_name != NULL)
349 XFree(icon->icon_name);
351 icon->icon_name = new_title;
353 if (changed)
354 icon->force_paint = 1;
355 wIconPaint(icon);
358 RImage *wIconValidateIconSize(WScreen * scr, RImage * icon, int max_size)
360 RImage *tmp;
361 int w, h;
363 if (!icon)
364 return NULL;
365 #ifndef DONT_SCALE_ICONS
366 if (max_size != 64) {
367 w = max_size * icon->width / 64;
368 h = max_size * icon->height / 64;
370 tmp = RScaleImage(icon, w, h);
371 RReleaseImage(icon);
372 icon = tmp;
374 #endif
376 return icon;
379 Bool wIconChangeImageFile(WIcon * icon, char *file)
381 WScreen *scr = icon->core->screen_ptr;
382 RImage *image;
383 char *path;
384 int error = 0;
386 if (icon->file_image)
387 RReleaseImage(icon->file_image);
389 if (!file) {
390 icon->file_image = NULL;
391 wIconUpdate(icon);
392 return True;
395 path = FindImage(wPreferences.icon_path, file);
397 if (path && (image = RLoadImage(scr->rcontext, path, 0))) {
398 icon->file_image = wIconValidateIconSize(icon->core->screen_ptr, image, wPreferences.icon_size);
399 wIconUpdate(icon);
400 } else {
401 error = 1;
404 if (path)
405 wfree(path);
407 return !error;
410 static char *getnameforicon(WWindow * wwin)
412 char *prefix, *suffix;
413 char *path;
414 int len;
416 if (wwin->wm_class && wwin->wm_instance) {
417 int len = strlen(wwin->wm_class) + strlen(wwin->wm_instance) + 2;
418 suffix = wmalloc(len);
419 snprintf(suffix, len, "%s.%s", wwin->wm_instance, wwin->wm_class);
420 } else if (wwin->wm_class) {
421 int len = strlen(wwin->wm_class) + 1;
422 suffix = wmalloc(len);
423 snprintf(suffix, len, "%s", wwin->wm_class);
424 } else if (wwin->wm_instance) {
425 int len = strlen(wwin->wm_instance) + 1;
426 suffix = wmalloc(len);
427 snprintf(suffix, len, "%s", wwin->wm_instance);
428 } else {
429 return NULL;
432 prefix = wusergnusteppath();
433 len = strlen(prefix) + 64 + strlen(suffix);
434 path = wmalloc(len + 1);
435 snprintf(path, len, "%s/Library/WindowMaker", prefix);
437 if (access(path, F_OK) != 0) {
438 if (mkdir(path, S_IRUSR | S_IWUSR | S_IXUSR)) {
439 werror(_("could not create directory %s"), path);
440 wfree(path);
441 wfree(suffix);
442 return NULL;
445 strcat(path, "/CachedPixmaps");
446 if (access(path, F_OK) != 0) {
447 if (mkdir(path, S_IRUSR | S_IWUSR | S_IXUSR) != 0) {
448 werror(_("could not create directory %s"), path);
449 wfree(path);
450 wfree(suffix);
451 return NULL;
455 strcat(path, "/");
456 strcat(path, suffix);
457 strcat(path, ".xpm");
458 wfree(suffix);
460 return path;
464 * wIconStore--
465 * Stores the client supplied icon at ~/GNUstep/Library/WindowMaker/CachedPixmaps
466 * and returns the path for that icon. Returns NULL if there is no
467 * client supplied icon or on failure.
469 * Side effects:
470 * New directories might be created.
472 char *wIconStore(WIcon * icon)
474 char *path;
475 RImage *image = NULL;
476 WWindow *wwin = icon->owner;
478 if (!wwin) return NULL;
480 path = getnameforicon(wwin);
481 if (!path)
482 return NULL;
484 if (wwin->net_icon_image) {
485 image = RRetainImage(wwin->net_icon_image);
486 } else if (wwin->wm_hints && (wwin->wm_hints->flags & IconPixmapHint)
487 && wwin->wm_hints->icon_pixmap != None) {
488 image = RCreateImageFromDrawable(icon->core->screen_ptr->rcontext,
489 wwin->wm_hints->icon_pixmap, (wwin->wm_hints->flags & IconMaskHint)
490 ? wwin->wm_hints->icon_mask : None);
492 if (!image) {
493 wfree(path);
494 return NULL;
497 if (!RSaveImage(image, path, "XPM")) {
498 wfree(path);
499 path = NULL;
501 RReleaseImage(image);
503 return path;
506 static void cycleColor(void *data)
508 WIcon *icon = (WIcon *) data;
509 WScreen *scr = icon->core->screen_ptr;
510 XGCValues gcv;
512 icon->step--;
513 gcv.dash_offset = icon->step;
514 XChangeGC(dpy, scr->icon_select_gc, GCDashOffset, &gcv);
516 XDrawRectangle(dpy, icon->core->window, scr->icon_select_gc, 0, 0,
517 icon->core->width - 1, icon->core->height - 1);
518 icon->handlerID = WMAddTimerHandler(COLOR_CYCLE_DELAY, cycleColor, icon);
521 #ifdef NEWAPPICON
522 void wIconSetHighlited(WIcon *icon, Bool flag)
524 if (icon->highlighted == flag)
525 return;
527 icon->highlighted = flag;
528 icon->force_paint = True;
529 wIconPaint(icon);
531 #endif
533 void wIconSelect(WIcon * icon)
535 WScreen *scr = icon->core->screen_ptr;
536 icon->selected = !icon->selected;
538 if (icon->selected) {
539 icon->step = 0;
540 if (!wPreferences.dont_blink)
541 icon->handlerID = WMAddTimerHandler(10, cycleColor, icon);
542 else
543 XDrawRectangle(dpy, icon->core->window, scr->icon_select_gc, 0, 0,
544 icon->core->width - 1, icon->core->height - 1);
545 } else {
546 if (icon->handlerID) {
547 WMDeleteTimerHandler(icon->handlerID);
548 icon->handlerID = NULL;
550 XClearArea(dpy, icon->core->window, 0, 0, icon->core->width, icon->core->height, True);
554 void wIconUpdate(WIcon * icon)
556 WScreen *scr = icon->core->screen_ptr;
557 WWindow *wwin = icon->owner;
559 assert(scr->icon_tile != NULL);
561 if (icon->pixmap != None)
562 XFreePixmap(dpy, icon->pixmap);
563 icon->pixmap = None;
565 if (wwin && WFLAGP(wwin, always_user_icon))
566 goto user_icon;
568 if (icon->icon_win != None) {
569 /* Get the Pixmap from the WIcon */
570 get_pixmap_icon_from_icon_win(icon);
571 } else if (wwin && wwin->net_icon_image) {
572 /* Use _NET_WM_ICON icon */
573 icon->pixmap = makeIcon(scr, wwin->net_icon_image, icon->show_title,
574 icon->shadowed, icon->tile_type, icon->highlighted);
575 } else if (wwin && wwin->wm_hints && (wwin->wm_hints->flags & IconPixmapHint)) {
576 if (get_pixmap_icon_from_wm_hints(scr, wwin, icon))
577 goto user_icon;
578 } else {
579 user_icon:
580 if (icon->file_image) {
581 icon->pixmap = makeIcon(scr, icon->file_image, icon->show_title,
582 icon->shadowed, icon->tile_type, icon->highlighted);
583 } else {
584 /* make default icons */
585 if (!scr->def_icon_pixmap) {
586 RImage *image = NULL;
587 char *path;
588 char *file;
590 file = wDefaultGetIconFile(scr, NULL, NULL, False);
591 if (file) {
592 path = FindImage(wPreferences.icon_path, file);
593 if (!path) {
594 wwarning(_("could not find default icon \"%s\""), file);
595 goto make_icons;
598 image = RLoadImage(scr->rcontext, path, 0);
599 if (!image)
600 wwarning(_("could not load default icon \"%s\":%s"),
601 file, RMessageForError(RErrorCode));
602 wfree(path);
604 make_icons:
606 image = wIconValidateIconSize(scr, image, wPreferences.icon_size);
607 scr->def_icon_pixmap = makeIcon(scr, image, False, False, icon->tile_type, icon->highlighted);
608 scr->def_ticon_pixmap = makeIcon(scr, image, True, False, icon->tile_type, icon->highlighted);
609 if (image)
610 RReleaseImage(image);
613 if (icon->show_title)
614 XSetWindowBackgroundPixmap(dpy, icon->core->window, scr->def_ticon_pixmap);
615 else
616 XSetWindowBackgroundPixmap(dpy, icon->core->window, scr->def_icon_pixmap);
618 icon->pixmap = None;
621 if (icon->pixmap != None)
622 XSetWindowBackgroundPixmap(dpy, icon->core->window, icon->pixmap);
624 XClearWindow(dpy, icon->core->window);
625 wIconPaint(icon);
628 /* Get the Pixmap from the WIcon of the WWindow */
629 void get_pixmap_icon_from_icon_win(WIcon * icon)
631 XWindowAttributes attr;
632 WScreen *scr = icon->core->screen_ptr;
633 int title_height = WMFontHeight(scr->icon_title_font);
634 unsigned int width, height, depth;
635 int theight;
636 int resize = 0;
637 Pixmap pixmap;
639 getSize(icon->icon_win, &width, &height, &depth);
641 if (width > wPreferences.icon_size) {
642 resize = 1;
643 width = wPreferences.icon_size;
646 if (height > wPreferences.icon_size) {
647 resize = 1;
648 height = wPreferences.icon_size;
651 if (icon->show_title && (height + title_height < wPreferences.icon_size)) {
652 pixmap = XCreatePixmap(dpy, scr->w_win, wPreferences.icon_size,
653 wPreferences.icon_size, scr->w_depth);
654 XSetClipMask(dpy, scr->copy_gc, None);
655 XCopyArea(dpy, scr->icon_tile_pixmap, pixmap, scr->copy_gc, 0, 0,
656 wPreferences.icon_size, wPreferences.icon_size, 0, 0);
657 drawIconTitle(scr, pixmap, title_height);
658 theight = title_height;
659 } else {
660 pixmap = None;
661 theight = 0;
662 XSetWindowBackgroundPixmap(dpy, icon->core->window, scr->icon_tile_pixmap);
665 XSetWindowBorderWidth(dpy, icon->icon_win, 0);
666 XReparentWindow(dpy, icon->icon_win, icon->core->window,
667 (wPreferences.icon_size - width) / 2,
668 theight + (wPreferences.icon_size - height - theight) / 2);
669 if (resize)
670 XResizeWindow(dpy, icon->icon_win, width, height);
672 XMapWindow(dpy, icon->icon_win);
673 XAddToSaveSet(dpy, icon->icon_win);
675 /* Save it */
676 icon->pixmap = pixmap;
678 if ((XGetWindowAttributes(dpy, icon->icon_win, &attr)) &&
679 (attr.all_event_masks & ButtonPressMask))
680 wHackedGrabButton(Button1, MOD_MASK, icon->core->window, True,
681 ButtonPressMask, GrabModeSync, GrabModeAsync,
682 None, wCursor[WCUR_ARROW]);
685 /* Get the Pixmap from the XWindow wm_hints */
686 int get_pixmap_icon_from_wm_hints(WScreen *scr, WWindow *wwin, WIcon *icon)
688 Window jw;
689 Pixmap pixmap;
690 unsigned int w, h, ju, d;
691 int ji, x, y;
692 int title_height = WMFontHeight(scr->icon_title_font);
694 if (!XGetGeometry(dpy, wwin->wm_hints->icon_pixmap, &jw, &ji, &ji, &w, &h, &ju, &d)) {
695 icon->owner->wm_hints->flags &= ~IconPixmapHint;
696 return(1);
699 pixmap = XCreatePixmap(dpy, icon->core->window, wPreferences.icon_size,
700 wPreferences.icon_size, scr->w_depth);
701 XSetClipMask(dpy, scr->copy_gc, None);
702 XCopyArea(dpy, scr->icon_tile_pixmap, pixmap, scr->copy_gc, 0, 0,
703 wPreferences.icon_size, wPreferences.icon_size, 0, 0);
705 if (w > wPreferences.icon_size)
706 w = wPreferences.icon_size;
707 x = (wPreferences.icon_size - w) / 2;
709 if (icon->show_title && (title_height < wPreferences.icon_size)) {
710 drawIconTitle(scr, pixmap, title_height);
712 if (h > wPreferences.icon_size - title_height - 2) {
713 h = wPreferences.icon_size - title_height - 2;
714 y = title_height + 1;
715 } else {
716 y = (wPreferences.icon_size - h - title_height) / 2 + title_height + 1;
718 } else {
719 if (w > wPreferences.icon_size)
720 w = wPreferences.icon_size;
721 y = (wPreferences.icon_size - h) / 2;
724 if (wwin->wm_hints->flags & IconMaskHint)
725 XSetClipMask(dpy, scr->copy_gc, wwin->wm_hints->icon_mask);
727 XSetClipOrigin(dpy, scr->copy_gc, x, y);
729 if (d != scr->w_depth) {
730 XSetForeground(dpy, scr->copy_gc, scr->black_pixel);
731 XSetBackground(dpy, scr->copy_gc, scr->white_pixel);
732 XCopyPlane(dpy, wwin->wm_hints->icon_pixmap, pixmap, scr->copy_gc, 0, 0, w, h, x, y, 1);
733 } else {
734 XCopyArea(dpy, wwin->wm_hints->icon_pixmap, pixmap, scr->copy_gc, 0, 0, w, h, x, y);
737 XSetClipOrigin(dpy, scr->copy_gc, 0, 0);
739 icon->pixmap = pixmap;
740 return (0);
743 void wIconPaint(WIcon * icon)
745 WScreen *scr = icon->core->screen_ptr;
746 int x;
747 char *tmp;
749 if (icon->force_paint) {
750 icon->force_paint = 0;
751 wIconUpdate(icon);
752 return;
755 XClearWindow(dpy, icon->core->window);
757 /* draw the icon title */
758 if (icon->show_title && icon->icon_name != NULL) {
759 int l;
760 int w;
762 tmp = ShrinkString(scr->icon_title_font, icon->icon_name, wPreferences.icon_size - 4);
763 w = WMWidthOfString(scr->icon_title_font, tmp, l = strlen(tmp));
765 if (w > icon->core->width - 4)
766 x = (icon->core->width - 4) - w;
767 else
768 x = (icon->core->width - w) / 2;
770 WMDrawString(scr->wmscreen, icon->core->window, scr->icon_title_color,
771 scr->icon_title_font, x, 1, tmp, l);
772 wfree(tmp);
775 if (icon->selected)
776 XDrawRectangle(dpy, icon->core->window, scr->icon_select_gc, 0, 0,
777 icon->core->width - 1, icon->core->height - 1);
780 /******************************************************************/
782 static void miniwindowExpose(WObjDescriptor * desc, XEvent * event)
784 wIconPaint(desc->parent);
787 static void miniwindowDblClick(WObjDescriptor * desc, XEvent * event)
789 WIcon *icon = desc->parent;
791 assert(icon->owner != NULL);
793 wDeiconifyWindow(icon->owner);
796 static void miniwindowMouseDown(WObjDescriptor * desc, XEvent * event)
798 WIcon *icon = desc->parent;
799 WWindow *wwin = icon->owner;
800 XEvent ev;
801 int x = wwin->icon_x, y = wwin->icon_y;
802 int dx = event->xbutton.x, dy = event->xbutton.y;
803 int grabbed = 0;
804 int clickButton = event->xbutton.button;
805 Bool hasMoved = False;
807 if (WCHECK_STATE(WSTATE_MODAL))
808 return;
810 if (IsDoubleClick(icon->core->screen_ptr, event)) {
811 miniwindowDblClick(desc, event);
812 return;
815 if (event->xbutton.button == Button1) {
816 if (event->xbutton.state & MOD_MASK)
817 wLowerFrame(icon->core);
818 else
819 wRaiseFrame(icon->core);
820 if (event->xbutton.state & ShiftMask) {
821 wIconSelect(icon);
822 wSelectWindow(icon->owner, !wwin->flags.selected);
824 } else if (event->xbutton.button == Button3) {
825 WObjDescriptor *desc;
827 OpenMiniwindowMenu(wwin, event->xbutton.x_root, event->xbutton.y_root);
829 /* allow drag select of menu */
830 desc = &wwin->screen_ptr->window_menu->menu->descriptor;
831 event->xbutton.send_event = True;
832 (*desc->handle_mousedown) (desc, event);
834 return;
837 if (XGrabPointer(dpy, icon->core->window, False, ButtonMotionMask
838 | ButtonReleaseMask | ButtonPressMask, GrabModeAsync,
839 GrabModeAsync, None, None, CurrentTime) != GrabSuccess) {
841 while (1) {
842 WMMaskEvent(dpy, PointerMotionMask | ButtonReleaseMask | ButtonPressMask
843 | ButtonMotionMask | ExposureMask, &ev);
844 switch (ev.type) {
845 case Expose:
846 WMHandleEvent(&ev);
847 break;
849 case MotionNotify:
850 hasMoved = True;
851 if (!grabbed) {
852 if (abs(dx - ev.xmotion.x) >= MOVE_THRESHOLD
853 || abs(dy - ev.xmotion.y) >= MOVE_THRESHOLD) {
854 XChangeActivePointerGrab(dpy, ButtonMotionMask
855 | ButtonReleaseMask | ButtonPressMask,
856 wCursor[WCUR_MOVE], CurrentTime);
857 grabbed = 1;
858 } else {
859 break;
862 x = ev.xmotion.x_root - dx;
863 y = ev.xmotion.y_root - dy;
864 XMoveWindow(dpy, icon->core->window, x, y);
865 break;
867 case ButtonPress:
868 break;
870 case ButtonRelease:
871 if (ev.xbutton.button != clickButton)
872 break;
874 if (wwin->icon_x != x || wwin->icon_y != y)
875 wwin->flags.icon_moved = 1;
877 XMoveWindow(dpy, icon->core->window, x, y);
879 wwin->icon_x = x;
880 wwin->icon_y = y;
881 XUngrabPointer(dpy, CurrentTime);
883 if (wPreferences.auto_arrange_icons)
884 wArrangeIcons(wwin->screen_ptr, True);
885 if (wPreferences.single_click && !hasMoved)
886 miniwindowDblClick(desc, event);
887 return;