6537eced7f26140ad8dca8ffd7f642ca573ac025
[wmaker-crm.git] / src / icon.c
blob6537eced7f26140ad8dca8ffd7f642ca573ac025
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"
54 extern Cursor wCursor[WCUR_LAST];
56 static void miniwindowExpose(WObjDescriptor * desc, XEvent * event);
57 static void miniwindowMouseDown(WObjDescriptor * desc, XEvent * event);
58 static void miniwindowDblClick(WObjDescriptor * desc, XEvent * event);
60 static WIcon *wIconCreateCore(WScreen *scr, int coord_x, int coord_y);
62 void get_pixmap_icon_from_icon_win(WIcon *icon);
63 int get_pixmap_icon_from_wm_hints(WScreen *scr, WWindow *wwin, WIcon *icon);
64 void get_pixmap_icon_from_user_icon(WScreen *scr, WIcon * icon);
65 /****** Notification Observers ******/
67 static void appearanceObserver(void *self, WMNotification * notif)
69 WIcon *icon = (WIcon *) self;
70 uintptr_t flags = (uintptr_t)WMGetNotificationClientData(notif);
72 if ((flags & WTextureSettings) || (flags & WFontSettings))
73 icon->force_paint = 1;
75 wIconPaint(icon);
77 /* so that the appicon expose handlers will paint the appicon specific
78 * stuff */
79 XClearArea(dpy, icon->core->window, 0, 0, icon->core->width, icon->core->height, True);
82 static void tileObserver(void *self, WMNotification * notif)
84 WIcon *icon = (WIcon *) self;
86 icon->force_paint = 1;
87 wIconPaint(icon);
89 XClearArea(dpy, icon->core->window, 0, 0, 1, 1, True);
92 /************************************/
94 INLINE static void getSize(Drawable d, unsigned int *w, unsigned int *h, unsigned int *dep)
96 Window rjunk;
97 int xjunk, yjunk;
98 unsigned int bjunk;
100 XGetGeometry(dpy, d, &rjunk, &xjunk, &yjunk, w, h, &bjunk, dep);
103 WIcon *wIconCreate(WWindow * wwin)
105 WScreen *scr = wwin->screen_ptr;
106 WIcon *icon;
107 char *file;
109 icon = wIconCreateCore(scr, wwin->icon_x, wwin->icon_y);
111 icon->owner = wwin;
112 if (wwin->wm_hints && (wwin->wm_hints->flags & IconWindowHint)) {
113 if (wwin->client_win == wwin->main_window) {
114 WApplication *wapp;
115 /* do not let miniwindow steal app-icon's icon window */
116 wapp = wApplicationOf(wwin->client_win);
117 if (!wapp || wapp->app_icon == NULL)
118 icon->icon_win = wwin->wm_hints->icon_window;
119 } else {
120 icon->icon_win = wwin->wm_hints->icon_window;
123 #ifdef NO_MINIWINDOW_TITLES
124 icon->show_title = 0;
125 #else
126 icon->show_title = 1;
127 #endif
128 icon->file_image = wDefaultGetImage(scr, wwin->wm_instance, wwin->wm_class, wPreferences.icon_size);
130 file = wDefaultGetIconFile(wwin->wm_instance, wwin->wm_class, False);
131 if (file)
132 icon->file = wstrdup(file);
134 icon->icon_name = wNETWMGetIconName(wwin->client_win);
135 if (icon->icon_name)
136 wwin->flags.net_has_icon_title = 1;
137 else
138 wGetIconName(dpy, wwin->client_win, &icon->icon_name);
140 icon->tile_type = TILE_NORMAL;
142 wIconUpdate(icon);
144 XFlush(dpy);
146 WMAddNotificationObserver(appearanceObserver, icon, WNIconAppearanceSettingsChanged, icon);
147 WMAddNotificationObserver(tileObserver, icon, WNIconTileSettingsChanged, icon);
148 return icon;
151 WIcon *wIconCreateWithIconFile(WScreen * scr, char *iconfile, int tile)
153 WIcon *icon;
155 icon = wIconCreateCore(scr, 0, 0);
157 if (iconfile) {
158 icon->file_image = RLoadImage(scr->rcontext, iconfile, 0);
159 if (!icon->file_image)
160 wwarning(_("error loading image file \"%s\": %s"), iconfile, RMessageForError(RErrorCode));
162 icon->file_image = wIconValidateIconSize(scr, icon->file_image, wPreferences.icon_size);
164 icon->file = wstrdup(iconfile);
167 icon->tile_type = tile;
169 wIconUpdate(icon);
171 WMAddNotificationObserver(appearanceObserver, icon, WNIconAppearanceSettingsChanged, icon);
172 WMAddNotificationObserver(tileObserver, icon, WNIconTileSettingsChanged, icon);
174 return icon;
177 static WIcon *wIconCreateCore(WScreen *scr, int coord_x, int coord_y)
179 WIcon *icon;
180 unsigned long vmask = 0;
181 XSetWindowAttributes attribs;
183 icon = wmalloc(sizeof(WIcon));
184 icon->core = wCoreCreateTopLevel(scr,
185 coord_x,
186 coord_y,
187 wPreferences.icon_size,
188 wPreferences.icon_size,
191 if (wPreferences.use_saveunders) {
192 vmask = CWSaveUnder;
193 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 return icon;
217 void wIconDestroy(WIcon * icon)
219 WCoreWindow *core = icon->core;
220 WScreen *scr = core->screen_ptr;
222 WMRemoveNotificationObserver(icon);
224 if (icon->handlerID)
225 WMDeleteTimerHandler(icon->handlerID);
227 if (icon->icon_win) {
228 int x = 0, y = 0;
230 if (icon->owner) {
231 x = icon->owner->icon_x;
232 y = icon->owner->icon_y;
234 XUnmapWindow(dpy, icon->icon_win);
235 XReparentWindow(dpy, icon->icon_win, scr->root_win, x, y);
237 if (icon->icon_name)
238 XFree(icon->icon_name);
240 if (icon->pixmap)
241 XFreePixmap(dpy, icon->pixmap);
243 if (icon->file)
244 wfree(icon->file);
246 if (icon->file_image != NULL)
247 RReleaseImage(icon->file_image);
249 wCoreDestroy(icon->core);
250 wfree(icon);
253 static void drawIconTitle(WScreen * scr, Pixmap pixmap, int height)
255 XFillRectangle(dpy, pixmap, scr->icon_title_texture->normal_gc, 0, 0, wPreferences.icon_size, height + 1);
256 XDrawLine(dpy, pixmap, scr->icon_title_texture->light_gc, 0, 0, wPreferences.icon_size, 0);
257 XDrawLine(dpy, pixmap, scr->icon_title_texture->light_gc, 0, 0, 0, height + 1);
258 XDrawLine(dpy, pixmap, scr->icon_title_texture->dim_gc,
259 wPreferences.icon_size - 1, 0, wPreferences.icon_size - 1, height + 1);
262 static Pixmap makeIcon(WScreen *scr, RImage *icon, int titled, int shadowed, int tileType, int highlighted)
264 RImage *tile;
265 Pixmap pixmap;
266 int x, y, sx, sy;
267 unsigned w, h;
268 int theight = 0;
270 if (tileType == TILE_NORMAL) {
271 tile = RCloneImage(scr->icon_tile);
272 } else {
273 assert(scr->clip_tile);
274 tile = RCloneImage(scr->clip_tile);
277 if (icon) {
278 w = (icon->width > wPreferences.icon_size)
279 ? wPreferences.icon_size : icon->width;
280 x = (wPreferences.icon_size - w) / 2;
281 sx = (icon->width - w) / 2;
283 if (titled)
284 theight = WMFontHeight(scr->icon_title_font);
286 h = (icon->height + theight > wPreferences.icon_size
287 ? wPreferences.icon_size - theight : icon->height);
288 y = theight + (wPreferences.icon_size - theight - h) / 2;
289 sy = (icon->height - h) / 2;
291 RCombineArea(tile, icon, sx, sy, w, h, x, y);
294 if (shadowed) {
295 RColor color;
297 color.red = scr->icon_back_texture->light.red >> 8;
298 color.green = scr->icon_back_texture->light.green >> 8;
299 color.blue = scr->icon_back_texture->light.blue >> 8;
300 color.alpha = 150; /* about 60% */
301 RClearImage(tile, &color);
304 if (highlighted) {
305 RColor color;
307 color.red = color.green = color.blue = 0;
308 color.alpha = 160;
309 RLightImage(tile, &color);
312 if (!RConvertImage(scr->rcontext, tile, &pixmap))
313 wwarning(_("error rendering image:%s"), RMessageForError(RErrorCode));
315 RReleaseImage(tile);
317 if (titled)
318 drawIconTitle(scr, pixmap, theight);
320 return pixmap;
323 void wIconChangeTitle(WIcon * icon, char *new_title)
325 int changed;
327 changed = (new_title == NULL && icon->icon_name != NULL)
328 || (new_title != NULL && icon->icon_name == NULL);
330 if (icon->icon_name != NULL)
331 XFree(icon->icon_name);
333 icon->icon_name = new_title;
335 if (changed)
336 icon->force_paint = 1;
337 wIconPaint(icon);
340 RImage *wIconValidateIconSize(WScreen * scr, RImage * icon, int max_size)
342 RImage *tmp;
343 int w, h;
345 if (!icon)
346 return NULL;
347 #ifndef DONT_SCALE_ICONS
348 if (max_size != 64) {
349 w = max_size * icon->width / 64;
350 h = max_size * icon->height / 64;
352 tmp = RScaleImage(icon, w, h);
353 RReleaseImage(icon);
354 icon = tmp;
356 #endif
358 return icon;
361 Bool wIconChangeImageFile(WIcon * icon, char *file)
363 WScreen *scr = icon->core->screen_ptr;
364 RImage *image;
365 char *path;
366 int error = 0;
368 if (icon->file_image)
369 RReleaseImage(icon->file_image);
371 if (!file) {
372 icon->file_image = NULL;
373 wIconUpdate(icon);
374 return True;
377 path = FindImage(wPreferences.icon_path, file);
379 if (path && (image = RLoadImage(scr->rcontext, path, 0))) {
380 icon->file_image = wIconValidateIconSize(icon->core->screen_ptr, image, wPreferences.icon_size);
381 wIconUpdate(icon);
382 } else {
383 error = 1;
386 if (path)
387 wfree(path);
389 return !error;
392 static char *get_name_for_wwin(WWindow *wwin)
394 return get_name_for_instance_class(wwin->wm_instance, wwin->wm_class);
397 char *get_name_for_instance_class(char *wm_instance, char *wm_class)
399 char *suffix;
400 int len;
402 if (wm_class && wm_instance) {
403 len = strlen(wm_class) + strlen(wm_instance) + 2;
404 suffix = wmalloc(len);
405 snprintf(suffix, len, "%s.%s", wm_instance, wm_class);
406 } else if (wm_class) {
407 len = strlen(wm_class) + 1;
408 suffix = wmalloc(len);
409 snprintf(suffix, len, "%s", wm_class);
410 } else if (wm_instance) {
411 len = strlen(wm_instance) + 1;
412 suffix = wmalloc(len);
413 snprintf(suffix, len, "%s", wm_instance);
414 } else {
415 return NULL;
418 return suffix;
421 static char *get_icon_cache_path(void)
423 char *prefix, *path;
424 int len, ret;
426 prefix = wusergnusteppath();
427 len = strlen(prefix) + strlen(CACHE_ICON_PATH) + 2;
428 path = wmalloc(len);
429 snprintf(path, len, "%s%s/", prefix, CACHE_ICON_PATH);
431 /* If the folder exists, exit */
432 if (access(path, F_OK) == 0)
433 return path;
435 /* Create the folder */
436 ret = wmkdirhier((const char *) path);
438 /* Exit 1 on success, 0 on failure */
439 if (ret == 1)
440 return path;
442 /* Fail */
443 wfree(path);
444 return NULL;
447 static RImage *get_wwindow_image_from_wmhints(WWindow *wwin, WIcon *icon)
449 RImage *image = NULL;
450 XWMHints *hints = wwin->wm_hints;
452 if (hints && (hints->flags & IconPixmapHint) && hints->icon_pixmap != None)
453 image = RCreateImageFromDrawable(icon->core->screen_ptr->rcontext,
454 hints->icon_pixmap,
455 (hints->flags & IconMaskHint)
456 ? hints->icon_mask : None);
458 return image;
462 * wIconStore--
463 * Stores the client supplied icon at CACHE_ICON_PATH
464 * and returns the path for that icon. Returns NULL if there is no
465 * client supplied icon or on failure.
467 * Side effects:
468 * New directories might be created.
470 char *wIconStore(WIcon * icon)
472 char *path, *dir_path, *file;
473 int len = 0;
474 RImage *image = NULL;
475 WWindow *wwin = icon->owner;
477 if (!wwin)
478 return NULL;
480 dir_path = get_icon_cache_path();
481 if (!dir_path)
482 return NULL;
484 file = get_name_for_wwin(wwin);
485 if (!file) {
486 wfree(dir_path);
487 return NULL;
490 len = strlen(dir_path) + strlen(file) + 5;
491 path = wmalloc(len);
492 snprintf(path, len, "%s%s.xpm", dir_path, file);
493 wfree(dir_path);
494 wfree(file);
496 /* If icon exists, exit */
497 if (access(path, F_OK) == 0)
498 return path;
500 if (wwin->net_icon_image)
501 image = RRetainImage(wwin->net_icon_image);
502 else
503 image = get_wwindow_image_from_wmhints(wwin, icon);
505 if (!image) {
506 wfree(path);
507 return NULL;
510 if (!RSaveImage(image, path, "XPM")) {
511 wfree(path);
512 path = NULL;
515 RReleaseImage(image);
517 return path;
520 static void cycleColor(void *data)
522 WIcon *icon = (WIcon *) data;
523 WScreen *scr = icon->core->screen_ptr;
524 XGCValues gcv;
526 icon->step--;
527 gcv.dash_offset = icon->step;
528 XChangeGC(dpy, scr->icon_select_gc, GCDashOffset, &gcv);
530 XDrawRectangle(dpy, icon->core->window, scr->icon_select_gc, 0, 0,
531 icon->core->width - 1, icon->core->height - 1);
532 icon->handlerID = WMAddTimerHandler(COLOR_CYCLE_DELAY, cycleColor, icon);
535 #ifdef NEWAPPICON
536 void wIconSetHighlited(WIcon *icon, Bool flag)
538 if (icon->highlighted == flag)
539 return;
541 icon->highlighted = flag;
542 icon->force_paint = True;
543 wIconPaint(icon);
545 #endif
547 void wIconSelect(WIcon * icon)
549 WScreen *scr = icon->core->screen_ptr;
550 icon->selected = !icon->selected;
552 if (icon->selected) {
553 icon->step = 0;
554 if (!wPreferences.dont_blink)
555 icon->handlerID = WMAddTimerHandler(10, cycleColor, icon);
556 else
557 XDrawRectangle(dpy, icon->core->window, scr->icon_select_gc, 0, 0,
558 icon->core->width - 1, icon->core->height - 1);
559 } else {
560 if (icon->handlerID) {
561 WMDeleteTimerHandler(icon->handlerID);
562 icon->handlerID = NULL;
564 XClearArea(dpy, icon->core->window, 0, 0, icon->core->width, icon->core->height, True);
568 void wIconUpdate(WIcon * icon)
570 WScreen *scr = icon->core->screen_ptr;
571 WWindow *wwin = icon->owner;
573 assert(scr->icon_tile != NULL);
575 if (icon->pixmap != None)
576 XFreePixmap(dpy, icon->pixmap);
578 icon->pixmap = None;
580 if (wwin && WFLAGP(wwin, always_user_icon)) {
581 /* Forced use user_icon */
582 get_pixmap_icon_from_user_icon(scr, icon);
583 } else if (icon->icon_win != None) {
584 /* Get the Pixmap from the WIcon */
585 get_pixmap_icon_from_icon_win(icon);
586 } else if (wwin && wwin->net_icon_image) {
587 /* Use _NET_WM_ICON icon */
588 icon->pixmap = makeIcon(scr, wwin->net_icon_image, icon->show_title,
589 icon->shadowed, icon->tile_type, icon->highlighted);
590 } else if (wwin && wwin->wm_hints && (wwin->wm_hints->flags & IconPixmapHint)) {
591 /* Get the Pixmap from the wm_hints, else, from the user */
592 if (get_pixmap_icon_from_wm_hints(scr, wwin, icon))
593 get_pixmap_icon_from_user_icon(scr, icon);
594 } else {
595 /* Get the Pixmap from the user */
596 get_pixmap_icon_from_user_icon(scr, icon);
599 /* No pixmap, set default background */
600 if (icon->pixmap != None)
601 XSetWindowBackgroundPixmap(dpy, icon->core->window, icon->pixmap);
603 /* Paint it */
604 XClearWindow(dpy, icon->core->window);
605 wIconPaint(icon);
608 void get_pixmap_icon_from_user_icon(WScreen *scr, WIcon * icon)
610 RImage *image = NULL;
611 char *path;
612 char *file;
614 if (icon->file_image) {
615 icon->pixmap = makeIcon(scr, icon->file_image, icon->show_title,
616 icon->shadowed, icon->tile_type, icon->highlighted);
617 } else {
618 /* make default icons */
619 if (!scr->def_icon_pixmap) {
620 file = wDefaultGetIconFile(NULL, NULL, False);
621 if (file) {
622 path = FindImage(wPreferences.icon_path, file);
623 if (path) {
624 image = RLoadImage(scr->rcontext, path, 0);
625 if (!image)
626 wwarning(_("could not load default icon \"%s\":%s"),
627 file, RMessageForError(RErrorCode));
628 wfree(path);
629 } else {
630 wwarning(_("could not find default icon \"%s\""), file);
634 image = wIconValidateIconSize(scr, image, wPreferences.icon_size);
635 scr->def_icon_pixmap = makeIcon(scr, image, False, False, icon->tile_type, icon->highlighted);
636 scr->def_ticon_pixmap = makeIcon(scr, image, True, False, icon->tile_type, icon->highlighted);
637 if (image)
638 RReleaseImage(image);
641 if (icon->show_title)
642 XSetWindowBackgroundPixmap(dpy, icon->core->window, scr->def_ticon_pixmap);
643 else
644 XSetWindowBackgroundPixmap(dpy, icon->core->window, scr->def_icon_pixmap);
646 icon->pixmap = None;
650 /* Get the Pixmap from the WIcon of the WWindow */
651 void get_pixmap_icon_from_icon_win(WIcon * icon)
653 XWindowAttributes attr;
654 WScreen *scr = icon->core->screen_ptr;
655 int title_height = WMFontHeight(scr->icon_title_font);
656 unsigned int width, height, depth;
657 int theight;
658 int resize = 0;
659 Pixmap pixmap;
661 getSize(icon->icon_win, &width, &height, &depth);
663 if (width > wPreferences.icon_size) {
664 resize = 1;
665 width = wPreferences.icon_size;
668 if (height > wPreferences.icon_size) {
669 resize = 1;
670 height = wPreferences.icon_size;
673 if (icon->show_title && (height + title_height < wPreferences.icon_size)) {
674 pixmap = XCreatePixmap(dpy, scr->w_win, wPreferences.icon_size,
675 wPreferences.icon_size, scr->w_depth);
676 XSetClipMask(dpy, scr->copy_gc, None);
677 XCopyArea(dpy, scr->icon_tile_pixmap, pixmap, scr->copy_gc, 0, 0,
678 wPreferences.icon_size, wPreferences.icon_size, 0, 0);
679 drawIconTitle(scr, pixmap, title_height);
680 theight = title_height;
681 } else {
682 pixmap = None;
683 theight = 0;
684 XSetWindowBackgroundPixmap(dpy, icon->core->window, scr->icon_tile_pixmap);
687 XSetWindowBorderWidth(dpy, icon->icon_win, 0);
688 XReparentWindow(dpy, icon->icon_win, icon->core->window,
689 (wPreferences.icon_size - width) / 2,
690 theight + (wPreferences.icon_size - height - theight) / 2);
691 if (resize)
692 XResizeWindow(dpy, icon->icon_win, width, height);
694 XMapWindow(dpy, icon->icon_win);
695 XAddToSaveSet(dpy, icon->icon_win);
697 /* Save it */
698 icon->pixmap = pixmap;
700 if ((XGetWindowAttributes(dpy, icon->icon_win, &attr)) &&
701 (attr.all_event_masks & ButtonPressMask))
702 wHackedGrabButton(Button1, MOD_MASK, icon->core->window, True,
703 ButtonPressMask, GrabModeSync, GrabModeAsync,
704 None, wCursor[WCUR_ARROW]);
707 /* Get the Pixmap from the XWindow wm_hints */
708 int get_pixmap_icon_from_wm_hints(WScreen *scr, WWindow *wwin, WIcon *icon)
710 Window jw;
711 Pixmap pixmap;
712 unsigned int w, h, ju, d;
713 int ji, x, y;
714 int title_height = WMFontHeight(scr->icon_title_font);
716 if (!XGetGeometry(dpy, wwin->wm_hints->icon_pixmap, &jw, &ji, &ji, &w, &h, &ju, &d)) {
717 icon->owner->wm_hints->flags &= ~IconPixmapHint;
718 return 1;
721 pixmap = XCreatePixmap(dpy, icon->core->window, wPreferences.icon_size,
722 wPreferences.icon_size, scr->w_depth);
723 XSetClipMask(dpy, scr->copy_gc, None);
724 XCopyArea(dpy, scr->icon_tile_pixmap, pixmap, scr->copy_gc, 0, 0,
725 wPreferences.icon_size, wPreferences.icon_size, 0, 0);
727 if (w > wPreferences.icon_size)
728 w = wPreferences.icon_size;
729 x = (wPreferences.icon_size - w) / 2;
731 if (icon->show_title && (title_height < wPreferences.icon_size)) {
732 drawIconTitle(scr, pixmap, title_height);
734 if (h > wPreferences.icon_size - title_height - 2) {
735 h = wPreferences.icon_size - title_height - 2;
736 y = title_height + 1;
737 } else {
738 y = (wPreferences.icon_size - h - title_height) / 2 + title_height + 1;
740 } else {
741 if (w > wPreferences.icon_size)
742 w = wPreferences.icon_size;
743 y = (wPreferences.icon_size - h) / 2;
746 if (wwin->wm_hints->flags & IconMaskHint)
747 XSetClipMask(dpy, scr->copy_gc, wwin->wm_hints->icon_mask);
749 XSetClipOrigin(dpy, scr->copy_gc, x, y);
751 if (d != scr->w_depth) {
752 XSetForeground(dpy, scr->copy_gc, scr->black_pixel);
753 XSetBackground(dpy, scr->copy_gc, scr->white_pixel);
754 XCopyPlane(dpy, wwin->wm_hints->icon_pixmap, pixmap, scr->copy_gc, 0, 0, w, h, x, y, 1);
755 } else {
756 XCopyArea(dpy, wwin->wm_hints->icon_pixmap, pixmap, scr->copy_gc, 0, 0, w, h, x, y);
759 XSetClipOrigin(dpy, scr->copy_gc, 0, 0);
761 icon->pixmap = pixmap;
762 return (0);
765 void wIconPaint(WIcon * icon)
767 WScreen *scr = icon->core->screen_ptr;
768 int x;
769 char *tmp;
771 if (icon->force_paint) {
772 icon->force_paint = 0;
773 wIconUpdate(icon);
774 return;
777 XClearWindow(dpy, icon->core->window);
779 /* draw the icon title */
780 if (icon->show_title && icon->icon_name != NULL) {
781 int l;
782 int w;
784 tmp = ShrinkString(scr->icon_title_font, icon->icon_name, wPreferences.icon_size - 4);
785 w = WMWidthOfString(scr->icon_title_font, tmp, l = strlen(tmp));
787 if (w > icon->core->width - 4)
788 x = (icon->core->width - 4) - w;
789 else
790 x = (icon->core->width - w) / 2;
792 WMDrawString(scr->wmscreen, icon->core->window, scr->icon_title_color,
793 scr->icon_title_font, x, 1, tmp, l);
794 wfree(tmp);
797 if (icon->selected)
798 XDrawRectangle(dpy, icon->core->window, scr->icon_select_gc, 0, 0,
799 icon->core->width - 1, icon->core->height - 1);
802 /******************************************************************/
804 static void miniwindowExpose(WObjDescriptor * desc, XEvent * event)
806 wIconPaint(desc->parent);
809 static void miniwindowDblClick(WObjDescriptor * desc, XEvent * event)
811 WIcon *icon = desc->parent;
813 assert(icon->owner != NULL);
815 wDeiconifyWindow(icon->owner);
818 static void miniwindowMouseDown(WObjDescriptor * desc, XEvent * event)
820 WIcon *icon = desc->parent;
821 WWindow *wwin = icon->owner;
822 XEvent ev;
823 int x = wwin->icon_x, y = wwin->icon_y;
824 int dx = event->xbutton.x, dy = event->xbutton.y;
825 int grabbed = 0;
826 int clickButton = event->xbutton.button;
827 Bool hasMoved = False;
829 if (WCHECK_STATE(WSTATE_MODAL))
830 return;
832 if (IsDoubleClick(icon->core->screen_ptr, event)) {
833 miniwindowDblClick(desc, event);
834 return;
837 if (event->xbutton.button == Button1) {
838 if (event->xbutton.state & MOD_MASK)
839 wLowerFrame(icon->core);
840 else
841 wRaiseFrame(icon->core);
842 if (event->xbutton.state & ShiftMask) {
843 wIconSelect(icon);
844 wSelectWindow(icon->owner, !wwin->flags.selected);
846 } else if (event->xbutton.button == Button3) {
847 WObjDescriptor *desc;
849 OpenMiniwindowMenu(wwin, event->xbutton.x_root, event->xbutton.y_root);
851 /* allow drag select of menu */
852 desc = &wwin->screen_ptr->window_menu->menu->descriptor;
853 event->xbutton.send_event = True;
854 (*desc->handle_mousedown) (desc, event);
856 return;
859 if (XGrabPointer(dpy, icon->core->window, False, ButtonMotionMask
860 | ButtonReleaseMask | ButtonPressMask, GrabModeAsync,
861 GrabModeAsync, None, None, CurrentTime) != GrabSuccess) {
863 while (1) {
864 WMMaskEvent(dpy, PointerMotionMask | ButtonReleaseMask | ButtonPressMask
865 | ButtonMotionMask | ExposureMask, &ev);
866 switch (ev.type) {
867 case Expose:
868 WMHandleEvent(&ev);
869 break;
871 case MotionNotify:
872 hasMoved = True;
873 if (!grabbed) {
874 if (abs(dx - ev.xmotion.x) >= MOVE_THRESHOLD
875 || abs(dy - ev.xmotion.y) >= MOVE_THRESHOLD) {
876 XChangeActivePointerGrab(dpy, ButtonMotionMask
877 | ButtonReleaseMask | ButtonPressMask,
878 wCursor[WCUR_MOVE], CurrentTime);
879 grabbed = 1;
880 } else {
881 break;
884 x = ev.xmotion.x_root - dx;
885 y = ev.xmotion.y_root - dy;
886 XMoveWindow(dpy, icon->core->window, x, y);
887 break;
889 case ButtonPress:
890 break;
892 case ButtonRelease:
893 if (ev.xbutton.button != clickButton)
894 break;
896 if (wwin->icon_x != x || wwin->icon_y != y)
897 wwin->flags.icon_moved = 1;
899 XMoveWindow(dpy, icon->core->window, x, y);
901 wwin->icon_x = x;
902 wwin->icon_y = y;
903 XUngrabPointer(dpy, CurrentTime);
905 if (wPreferences.auto_arrange_icons)
906 wArrangeIcons(wwin->screen_ptr, True);
907 if (wPreferences.single_click && !hasMoved)
908 miniwindowDblClick(desc, event);
909 return;