WindowMaker: New function get_pixmap_icon_from_icon_win
[wmaker-crm.git] / src / icon.c
blobf5225475501f2628196cbdd8fbfb65bb66c22b9c
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 /****** Notification Observers ******/
62 static void appearanceObserver(void *self, WMNotification * notif)
64 WIcon *icon = (WIcon *) self;
65 uintptr_t flags = (uintptr_t)WMGetNotificationClientData(notif);
67 if (flags & WTextureSettings) {
68 icon->force_paint = 1;
70 if (flags & WFontSettings) {
71 icon->force_paint = 1;
74 if (flags & WColorSettings) {
78 wIconPaint(icon);
80 /* so that the appicon expose handlers will paint the appicon specific
81 * stuff */
82 XClearArea(dpy, icon->core->window, 0, 0, icon->core->width, icon->core->height, True);
85 static void tileObserver(void *self, WMNotification * notif)
87 WIcon *icon = (WIcon *) self;
89 icon->force_paint = 1;
90 wIconPaint(icon);
92 XClearArea(dpy, icon->core->window, 0, 0, 1, 1, True);
95 /************************************/
97 INLINE static void getSize(Drawable d, unsigned int *w, unsigned int *h, unsigned int *dep)
99 Window rjunk;
100 int xjunk, yjunk;
101 unsigned int bjunk;
103 XGetGeometry(dpy, d, &rjunk, &xjunk, &yjunk, w, h, &bjunk, dep);
106 WIcon *wIconCreate(WWindow * wwin)
108 WScreen *scr = wwin->screen_ptr;
109 WIcon *icon;
110 char *file;
111 unsigned long vmask = 0;
112 XSetWindowAttributes attribs;
114 icon = wmalloc(sizeof(WIcon));
115 memset(icon, 0, sizeof(WIcon));
116 icon->core = wCoreCreateTopLevel(scr, wwin->icon_x, wwin->icon_y,
117 wPreferences.icon_size, wPreferences.icon_size, 0);
119 if (wPreferences.use_saveunders) {
120 vmask |= CWSaveUnder;
121 attribs.save_under = True;
123 /* a white border for selecting it */
124 vmask |= CWBorderPixel;
125 attribs.border_pixel = scr->white_pixel;
127 XChangeWindowAttributes(dpy, icon->core->window, vmask, &attribs);
129 /* will be overriden if this is an application icon */
130 icon->core->descriptor.handle_mousedown = miniwindowMouseDown;
131 icon->core->descriptor.handle_expose = miniwindowExpose;
132 icon->core->descriptor.parent_type = WCLASS_MINIWINDOW;
133 icon->core->descriptor.parent = icon;
135 icon->core->stacking = wmalloc(sizeof(WStacking));
136 icon->core->stacking->above = NULL;
137 icon->core->stacking->under = NULL;
138 icon->core->stacking->window_level = NORMAL_ICON_LEVEL;
139 icon->core->stacking->child_of = NULL;
141 icon->owner = wwin;
142 if (wwin->wm_hints && (wwin->wm_hints->flags & IconWindowHint)) {
143 if (wwin->client_win == wwin->main_window) {
144 WApplication *wapp;
145 /* do not let miniwindow steal app-icon's icon window */
146 wapp = wApplicationOf(wwin->client_win);
147 if (!wapp || wapp->app_icon == NULL)
148 icon->icon_win = wwin->wm_hints->icon_window;
149 } else {
150 icon->icon_win = wwin->wm_hints->icon_window;
153 #ifdef NO_MINIWINDOW_TITLES
154 icon->show_title = 0;
155 #else
156 icon->show_title = 1;
157 #endif
158 icon->file_image = wDefaultGetImage(scr, wwin->wm_instance, wwin->wm_class, wPreferences.icon_size);
160 file = wDefaultGetIconFile(scr, wwin->wm_instance, wwin->wm_class, False);
161 if (file) {
162 icon->file = wstrdup(file);
165 icon->icon_name = wNETWMGetIconName(wwin->client_win);
166 if (icon->icon_name)
167 wwin->flags.net_has_icon_title = 1;
168 else
169 wGetIconName(dpy, wwin->client_win, &icon->icon_name);
171 icon->tile_type = TILE_NORMAL;
173 wIconUpdate(icon);
175 XFlush(dpy);
177 WMAddNotificationObserver(appearanceObserver, icon, WNIconAppearanceSettingsChanged, icon);
178 WMAddNotificationObserver(tileObserver, icon, WNIconTileSettingsChanged, icon);
179 return icon;
182 WIcon *wIconCreateWithIconFile(WScreen * scr, char *iconfile, int tile)
184 WIcon *icon;
185 unsigned long vmask = 0;
186 XSetWindowAttributes attribs;
188 icon = wmalloc(sizeof(WIcon));
189 memset(icon, 0, sizeof(WIcon));
190 icon->core = wCoreCreateTopLevel(scr, 0, 0, wPreferences.icon_size, wPreferences.icon_size, 0);
191 if (wPreferences.use_saveunders) {
192 vmask = CWSaveUnder;
193 attribs.save_under = True;
195 /* a white border for selecting it */
196 vmask |= CWBorderPixel;
197 attribs.border_pixel = scr->white_pixel;
199 XChangeWindowAttributes(dpy, icon->core->window, vmask, &attribs);
201 /* will be overriden if this is a application icon */
202 icon->core->descriptor.handle_mousedown = miniwindowMouseDown;
203 icon->core->descriptor.handle_expose = miniwindowExpose;
204 icon->core->descriptor.parent_type = WCLASS_MINIWINDOW;
205 icon->core->descriptor.parent = icon;
207 icon->core->stacking = wmalloc(sizeof(WStacking));
208 icon->core->stacking->above = NULL;
209 icon->core->stacking->under = NULL;
210 icon->core->stacking->window_level = NORMAL_ICON_LEVEL;
211 icon->core->stacking->child_of = NULL;
213 if (iconfile) {
214 icon->file_image = RLoadImage(scr->rcontext, iconfile, 0);
215 if (!icon->file_image) {
216 wwarning(_("error loading image file \"%s\": %s"), iconfile, RMessageForError(RErrorCode));
219 icon->file_image = wIconValidateIconSize(scr, icon->file_image, wPreferences.icon_size);
221 icon->file = wstrdup(iconfile);
224 icon->tile_type = tile;
226 wIconUpdate(icon);
228 WMAddNotificationObserver(appearanceObserver, icon, WNIconAppearanceSettingsChanged, icon);
229 WMAddNotificationObserver(tileObserver, icon, WNIconTileSettingsChanged, icon);
231 return icon;
234 void wIconDestroy(WIcon * icon)
236 WCoreWindow *core = icon->core;
237 WScreen *scr = core->screen_ptr;
239 WMRemoveNotificationObserver(icon);
241 if (icon->handlerID)
242 WMDeleteTimerHandler(icon->handlerID);
244 if (icon->icon_win) {
245 int x = 0, y = 0;
247 if (icon->owner) {
248 x = icon->owner->icon_x;
249 y = icon->owner->icon_y;
251 XUnmapWindow(dpy, icon->icon_win);
252 XReparentWindow(dpy, icon->icon_win, scr->root_win, x, y);
254 if (icon->icon_name)
255 XFree(icon->icon_name);
257 if (icon->pixmap)
258 XFreePixmap(dpy, icon->pixmap);
260 if (icon->file)
261 wfree(icon->file);
263 if (icon->file_image != NULL)
264 RReleaseImage(icon->file_image);
266 wCoreDestroy(icon->core);
267 wfree(icon);
270 static void drawIconTitle(WScreen * scr, Pixmap pixmap, int height)
272 XFillRectangle(dpy, pixmap, scr->icon_title_texture->normal_gc, 0, 0, wPreferences.icon_size, height + 1);
273 XDrawLine(dpy, pixmap, scr->icon_title_texture->light_gc, 0, 0, wPreferences.icon_size, 0);
274 XDrawLine(dpy, pixmap, scr->icon_title_texture->light_gc, 0, 0, 0, height + 1);
275 XDrawLine(dpy, pixmap, scr->icon_title_texture->dim_gc,
276 wPreferences.icon_size - 1, 0, wPreferences.icon_size - 1, height + 1);
279 static Pixmap makeIcon(WScreen *scr, RImage *icon, int titled, int shadowed, int tileType, int highlighted)
281 RImage *tile;
282 Pixmap pixmap;
283 int x, y, sx, sy;
284 unsigned w, h;
285 int theight = 0;
287 if (tileType == TILE_NORMAL) {
288 tile = RCloneImage(scr->icon_tile);
289 } else {
290 assert(scr->clip_tile);
291 tile = RCloneImage(scr->clip_tile);
294 if (icon) {
295 w = (icon->width > wPreferences.icon_size)
296 ? wPreferences.icon_size : icon->width;
297 x = (wPreferences.icon_size - w) / 2;
298 sx = (icon->width - w) / 2;
300 if (titled)
301 theight = WMFontHeight(scr->icon_title_font);
303 h = (icon->height + theight > wPreferences.icon_size
304 ? wPreferences.icon_size - theight : icon->height);
305 y = theight + (wPreferences.icon_size - theight - h) / 2;
306 sy = (icon->height - h) / 2;
308 RCombineArea(tile, icon, sx, sy, w, h, x, y);
311 if (shadowed) {
312 RColor color;
314 color.red = scr->icon_back_texture->light.red >> 8;
315 color.green = scr->icon_back_texture->light.green >> 8;
316 color.blue = scr->icon_back_texture->light.blue >> 8;
317 color.alpha = 150; /* about 60% */
318 RClearImage(tile, &color);
321 if (highlighted) {
322 RColor color;
324 color.red = color.green = color.blue = 0;
325 color.alpha = 160;
326 RLightImage(tile, &color);
329 if (!RConvertImage(scr->rcontext, tile, &pixmap))
330 wwarning(_("error rendering image:%s"), RMessageForError(RErrorCode));
332 RReleaseImage(tile);
334 if (titled)
335 drawIconTitle(scr, pixmap, theight);
337 return pixmap;
340 void wIconChangeTitle(WIcon * icon, char *new_title)
342 int changed;
344 changed = (new_title == NULL && icon->icon_name != NULL)
345 || (new_title != NULL && icon->icon_name == NULL);
347 if (icon->icon_name != NULL)
348 XFree(icon->icon_name);
350 icon->icon_name = new_title;
352 if (changed)
353 icon->force_paint = 1;
354 wIconPaint(icon);
357 RImage *wIconValidateIconSize(WScreen * scr, RImage * icon, int max_size)
359 RImage *tmp;
360 int w, h;
362 if (!icon)
363 return NULL;
364 #ifndef DONT_SCALE_ICONS
365 if (max_size != 64) {
366 w = max_size * icon->width / 64;
367 h = max_size * icon->height / 64;
369 tmp = RScaleImage(icon, w, h);
370 RReleaseImage(icon);
371 icon = tmp;
373 #endif
375 return icon;
378 Bool wIconChangeImageFile(WIcon * icon, char *file)
380 WScreen *scr = icon->core->screen_ptr;
381 RImage *image;
382 char *path;
383 int error = 0;
385 if (icon->file_image)
386 RReleaseImage(icon->file_image);
388 if (!file) {
389 icon->file_image = NULL;
390 wIconUpdate(icon);
391 return True;
394 path = FindImage(wPreferences.icon_path, file);
396 if (path && (image = RLoadImage(scr->rcontext, path, 0))) {
397 icon->file_image = wIconValidateIconSize(icon->core->screen_ptr, image, wPreferences.icon_size);
398 wIconUpdate(icon);
399 } else {
400 error = 1;
403 if (path)
404 wfree(path);
406 return !error;
409 static char *getnameforicon(WWindow * wwin)
411 char *prefix, *suffix;
412 char *path;
413 int len;
415 if (wwin->wm_class && wwin->wm_instance) {
416 int len = strlen(wwin->wm_class) + strlen(wwin->wm_instance) + 2;
417 suffix = wmalloc(len);
418 snprintf(suffix, len, "%s.%s", wwin->wm_instance, wwin->wm_class);
419 } else if (wwin->wm_class) {
420 int len = strlen(wwin->wm_class) + 1;
421 suffix = wmalloc(len);
422 snprintf(suffix, len, "%s", wwin->wm_class);
423 } else if (wwin->wm_instance) {
424 int len = strlen(wwin->wm_instance) + 1;
425 suffix = wmalloc(len);
426 snprintf(suffix, len, "%s", wwin->wm_instance);
427 } else {
428 return NULL;
431 prefix = wusergnusteppath();
432 len = strlen(prefix) + 64 + strlen(suffix);
433 path = wmalloc(len + 1);
434 snprintf(path, len, "%s/Library/WindowMaker", prefix);
436 if (access(path, F_OK) != 0) {
437 if (mkdir(path, S_IRUSR | S_IWUSR | S_IXUSR)) {
438 werror(_("could not create directory %s"), path);
439 wfree(path);
440 wfree(suffix);
441 return NULL;
444 strcat(path, "/CachedPixmaps");
445 if (access(path, F_OK) != 0) {
446 if (mkdir(path, S_IRUSR | S_IWUSR | S_IXUSR) != 0) {
447 werror(_("could not create directory %s"), path);
448 wfree(path);
449 wfree(suffix);
450 return NULL;
454 strcat(path, "/");
455 strcat(path, suffix);
456 strcat(path, ".xpm");
457 wfree(suffix);
459 return path;
463 * wIconStore--
464 * Stores the client supplied icon at ~/GNUstep/Library/WindowMaker/CachedPixmaps
465 * and returns the path for that icon. Returns NULL if there is no
466 * client supplied icon or on failure.
468 * Side effects:
469 * New directories might be created.
471 char *wIconStore(WIcon * icon)
473 char *path;
474 RImage *image = NULL;
475 WWindow *wwin = icon->owner;
477 if (!wwin) return NULL;
479 path = getnameforicon(wwin);
480 if (!path)
481 return NULL;
483 if (wwin->net_icon_image) {
484 image = RRetainImage(wwin->net_icon_image);
485 } else if (wwin->wm_hints && (wwin->wm_hints->flags & IconPixmapHint)
486 && wwin->wm_hints->icon_pixmap != None) {
487 image = RCreateImageFromDrawable(icon->core->screen_ptr->rcontext,
488 wwin->wm_hints->icon_pixmap, (wwin->wm_hints->flags & IconMaskHint)
489 ? wwin->wm_hints->icon_mask : None);
491 if (!image) {
492 wfree(path);
493 return NULL;
496 if (!RSaveImage(image, path, "XPM")) {
497 wfree(path);
498 path = NULL;
500 RReleaseImage(image);
502 return path;
505 static void cycleColor(void *data)
507 WIcon *icon = (WIcon *) data;
508 WScreen *scr = icon->core->screen_ptr;
509 XGCValues gcv;
511 icon->step--;
512 gcv.dash_offset = icon->step;
513 XChangeGC(dpy, scr->icon_select_gc, GCDashOffset, &gcv);
515 XDrawRectangle(dpy, icon->core->window, scr->icon_select_gc, 0, 0,
516 icon->core->width - 1, icon->core->height - 1);
517 icon->handlerID = WMAddTimerHandler(COLOR_CYCLE_DELAY, cycleColor, icon);
520 #ifdef NEWAPPICON
521 void wIconSetHighlited(WIcon *icon, Bool flag)
523 if (icon->highlighted == flag)
524 return;
526 icon->highlighted = flag;
527 icon->force_paint = True;
528 wIconPaint(icon);
530 #endif
532 void wIconSelect(WIcon * icon)
534 WScreen *scr = icon->core->screen_ptr;
535 icon->selected = !icon->selected;
537 if (icon->selected) {
538 icon->step = 0;
539 if (!wPreferences.dont_blink)
540 icon->handlerID = WMAddTimerHandler(10, cycleColor, icon);
541 else
542 XDrawRectangle(dpy, icon->core->window, scr->icon_select_gc, 0, 0,
543 icon->core->width - 1, icon->core->height - 1);
544 } else {
545 if (icon->handlerID) {
546 WMDeleteTimerHandler(icon->handlerID);
547 icon->handlerID = NULL;
549 XClearArea(dpy, icon->core->window, 0, 0, icon->core->width, icon->core->height, True);
553 void wIconUpdate(WIcon * icon)
555 WScreen *scr = icon->core->screen_ptr;
556 int title_height = WMFontHeight(scr->icon_title_font);
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 int x, y;
577 unsigned int w, h;
578 Window jw;
579 int ji;
580 unsigned int ju, d;
581 Pixmap pixmap;
583 if (!XGetGeometry(dpy, wwin->wm_hints->icon_pixmap, &jw, &ji, &ji, &w, &h, &ju, &d)) {
584 icon->owner->wm_hints->flags &= ~IconPixmapHint;
585 goto user_icon;
588 pixmap = XCreatePixmap(dpy, icon->core->window, wPreferences.icon_size,
589 wPreferences.icon_size, scr->w_depth);
590 XSetClipMask(dpy, scr->copy_gc, None);
591 XCopyArea(dpy, scr->icon_tile_pixmap, pixmap, scr->copy_gc, 0, 0,
592 wPreferences.icon_size, wPreferences.icon_size, 0, 0);
594 if (w > wPreferences.icon_size)
595 w = wPreferences.icon_size;
596 x = (wPreferences.icon_size - w) / 2;
598 if (icon->show_title && (title_height < wPreferences.icon_size)) {
599 drawIconTitle(scr, pixmap, title_height);
601 if (h > wPreferences.icon_size - title_height - 2) {
602 h = wPreferences.icon_size - title_height - 2;
603 y = title_height + 1;
604 } else {
605 y = (wPreferences.icon_size - h - title_height) / 2 + title_height + 1;
607 } else {
608 if (w > wPreferences.icon_size)
609 w = wPreferences.icon_size;
610 y = (wPreferences.icon_size - h) / 2;
613 if (wwin->wm_hints->flags & IconMaskHint)
614 XSetClipMask(dpy, scr->copy_gc, wwin->wm_hints->icon_mask);
616 XSetClipOrigin(dpy, scr->copy_gc, x, y);
618 if (d != scr->w_depth) {
619 XSetForeground(dpy, scr->copy_gc, scr->black_pixel);
620 XSetBackground(dpy, scr->copy_gc, scr->white_pixel);
621 XCopyPlane(dpy, wwin->wm_hints->icon_pixmap, pixmap, scr->copy_gc, 0, 0, w, h, x, y, 1);
622 } else {
623 XCopyArea(dpy, wwin->wm_hints->icon_pixmap, pixmap, scr->copy_gc, 0, 0, w, h, x, y);
626 XSetClipOrigin(dpy, scr->copy_gc, 0, 0);
628 icon->pixmap = pixmap;
629 } else {
630 user_icon:
631 if (icon->file_image) {
632 icon->pixmap = makeIcon(scr, icon->file_image, icon->show_title,
633 icon->shadowed, icon->tile_type, icon->highlighted);
634 } else {
635 /* make default icons */
636 if (!scr->def_icon_pixmap) {
637 RImage *image = NULL;
638 char *path;
639 char *file;
641 file = wDefaultGetIconFile(scr, NULL, NULL, False);
642 if (file) {
643 path = FindImage(wPreferences.icon_path, file);
644 if (!path) {
645 wwarning(_("could not find default icon \"%s\""), file);
646 goto make_icons;
649 image = RLoadImage(scr->rcontext, path, 0);
650 if (!image)
651 wwarning(_("could not load default icon \"%s\":%s"),
652 file, RMessageForError(RErrorCode));
653 wfree(path);
655 make_icons:
657 image = wIconValidateIconSize(scr, image, wPreferences.icon_size);
658 scr->def_icon_pixmap = makeIcon(scr, image, False, False, icon->tile_type, icon->highlighted);
659 scr->def_ticon_pixmap = makeIcon(scr, image, True, False, icon->tile_type, icon->highlighted);
660 if (image)
661 RReleaseImage(image);
664 if (icon->show_title)
665 XSetWindowBackgroundPixmap(dpy, icon->core->window, scr->def_ticon_pixmap);
666 else
667 XSetWindowBackgroundPixmap(dpy, icon->core->window, scr->def_icon_pixmap);
669 icon->pixmap = None;
672 if (icon->pixmap != None)
673 XSetWindowBackgroundPixmap(dpy, icon->core->window, icon->pixmap);
675 XClearWindow(dpy, icon->core->window);
676 wIconPaint(icon);
679 /* Get the Pixmap from the WIcon of the WWindow */
680 void get_pixmap_icon_from_icon_win(WIcon * icon)
682 XWindowAttributes attr;
683 WScreen *scr = icon->core->screen_ptr;
684 int title_height = WMFontHeight(scr->icon_title_font);
685 unsigned int width, height, depth;
686 int theight;
687 int resize = 0;
688 Pixmap pixmap;
690 getSize(icon->icon_win, &width, &height, &depth);
692 if (width > wPreferences.icon_size) {
693 resize = 1;
694 width = wPreferences.icon_size;
697 if (height > wPreferences.icon_size) {
698 resize = 1;
699 height = wPreferences.icon_size;
702 if (icon->show_title && (height + title_height < wPreferences.icon_size)) {
703 pixmap = XCreatePixmap(dpy, scr->w_win, wPreferences.icon_size,
704 wPreferences.icon_size, scr->w_depth);
705 XSetClipMask(dpy, scr->copy_gc, None);
706 XCopyArea(dpy, scr->icon_tile_pixmap, pixmap, scr->copy_gc, 0, 0,
707 wPreferences.icon_size, wPreferences.icon_size, 0, 0);
708 drawIconTitle(scr, pixmap, title_height);
709 theight = title_height;
710 } else {
711 pixmap = None;
712 theight = 0;
713 XSetWindowBackgroundPixmap(dpy, icon->core->window, scr->icon_tile_pixmap);
716 XSetWindowBorderWidth(dpy, icon->icon_win, 0);
717 XReparentWindow(dpy, icon->icon_win, icon->core->window,
718 (wPreferences.icon_size - width) / 2,
719 theight + (wPreferences.icon_size - height - theight) / 2);
720 if (resize)
721 XResizeWindow(dpy, icon->icon_win, width, height);
723 XMapWindow(dpy, icon->icon_win);
724 XAddToSaveSet(dpy, icon->icon_win);
726 /* Save it */
727 icon->pixmap = pixmap;
729 if ((XGetWindowAttributes(dpy, icon->icon_win, &attr)) &&
730 (attr.all_event_masks & ButtonPressMask))
731 wHackedGrabButton(Button1, MOD_MASK, icon->core->window, True,
732 ButtonPressMask, GrabModeSync, GrabModeAsync,
733 None, wCursor[WCUR_ARROW]);
737 void wIconPaint(WIcon * icon)
739 WScreen *scr = icon->core->screen_ptr;
740 int x;
741 char *tmp;
743 if (icon->force_paint) {
744 icon->force_paint = 0;
745 wIconUpdate(icon);
746 return;
749 XClearWindow(dpy, icon->core->window);
751 /* draw the icon title */
752 if (icon->show_title && icon->icon_name != NULL) {
753 int l;
754 int w;
756 tmp = ShrinkString(scr->icon_title_font, icon->icon_name, wPreferences.icon_size - 4);
757 w = WMWidthOfString(scr->icon_title_font, tmp, l = strlen(tmp));
759 if (w > icon->core->width - 4)
760 x = (icon->core->width - 4) - w;
761 else
762 x = (icon->core->width - w) / 2;
764 WMDrawString(scr->wmscreen, icon->core->window, scr->icon_title_color,
765 scr->icon_title_font, x, 1, tmp, l);
766 wfree(tmp);
769 if (icon->selected)
770 XDrawRectangle(dpy, icon->core->window, scr->icon_select_gc, 0, 0,
771 icon->core->width - 1, icon->core->height - 1);
774 /******************************************************************/
776 static void miniwindowExpose(WObjDescriptor * desc, XEvent * event)
778 wIconPaint(desc->parent);
781 static void miniwindowDblClick(WObjDescriptor * desc, XEvent * event)
783 WIcon *icon = desc->parent;
785 assert(icon->owner != NULL);
787 wDeiconifyWindow(icon->owner);
790 static void miniwindowMouseDown(WObjDescriptor * desc, XEvent * event)
792 WIcon *icon = desc->parent;
793 WWindow *wwin = icon->owner;
794 XEvent ev;
795 int x = wwin->icon_x, y = wwin->icon_y;
796 int dx = event->xbutton.x, dy = event->xbutton.y;
797 int grabbed = 0;
798 int clickButton = event->xbutton.button;
799 Bool hasMoved = False;
801 if (WCHECK_STATE(WSTATE_MODAL))
802 return;
804 if (IsDoubleClick(icon->core->screen_ptr, event)) {
805 miniwindowDblClick(desc, event);
806 return;
809 if (event->xbutton.button == Button1) {
810 if (event->xbutton.state & MOD_MASK)
811 wLowerFrame(icon->core);
812 else
813 wRaiseFrame(icon->core);
814 if (event->xbutton.state & ShiftMask) {
815 wIconSelect(icon);
816 wSelectWindow(icon->owner, !wwin->flags.selected);
818 } else if (event->xbutton.button == Button3) {
819 WObjDescriptor *desc;
821 OpenMiniwindowMenu(wwin, event->xbutton.x_root, event->xbutton.y_root);
823 /* allow drag select of menu */
824 desc = &wwin->screen_ptr->window_menu->menu->descriptor;
825 event->xbutton.send_event = True;
826 (*desc->handle_mousedown) (desc, event);
828 return;
831 if (XGrabPointer(dpy, icon->core->window, False, ButtonMotionMask
832 | ButtonReleaseMask | ButtonPressMask, GrabModeAsync,
833 GrabModeAsync, None, None, CurrentTime) != GrabSuccess) {
835 while (1) {
836 WMMaskEvent(dpy, PointerMotionMask | ButtonReleaseMask | ButtonPressMask
837 | ButtonMotionMask | ExposureMask, &ev);
838 switch (ev.type) {
839 case Expose:
840 WMHandleEvent(&ev);
841 break;
843 case MotionNotify:
844 hasMoved = True;
845 if (!grabbed) {
846 if (abs(dx - ev.xmotion.x) >= MOVE_THRESHOLD
847 || abs(dy - ev.xmotion.y) >= MOVE_THRESHOLD) {
848 XChangeActivePointerGrab(dpy, ButtonMotionMask
849 | ButtonReleaseMask | ButtonPressMask,
850 wCursor[WCUR_MOVE], CurrentTime);
851 grabbed = 1;
852 } else {
853 break;
856 x = ev.xmotion.x_root - dx;
857 y = ev.xmotion.y_root - dy;
858 XMoveWindow(dpy, icon->core->window, x, y);
859 break;
861 case ButtonPress:
862 break;
864 case ButtonRelease:
865 if (ev.xbutton.button != clickButton)
866 break;
868 if (wwin->icon_x != x || wwin->icon_y != y)
869 wwin->flags.icon_moved = 1;
871 XMoveWindow(dpy, icon->core->window, x, y);
873 wwin->icon_x = x;
874 wwin->icon_y = y;
875 XUngrabPointer(dpy, CurrentTime);
877 if (wPreferences.auto_arrange_icons)
878 wArrangeIcons(wwin->screen_ptr, True);
879 if (wPreferences.single_click && !hasMoved)
880 miniwindowDblClick(desc, event);
881 return;