a940123dcc83a3a88a01d19617baee8bc7e15272
[wmaker-crm.git] / src / icon.c
bloba940123dcc83a3a88a01d19617baee8bc7e15272
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 *wIconCreateCore(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 *wIconCreate(WWindow * wwin)
106 WScreen *scr = wwin->screen_ptr;
107 WIcon *icon;
108 char *file;
110 icon = wIconCreateCore(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
129 icon->file_image = wDefaultGetImage(scr, wwin->wm_instance, wwin->wm_class, wPreferences.icon_size);
131 /* Get the application icon, default included */
132 file = wDefaultGetIconFile(wwin->wm_instance, wwin->wm_class, True);
133 if (file)
134 icon->file = wstrdup(file);
136 icon->icon_name = wNETWMGetIconName(wwin->client_win);
137 if (icon->icon_name)
138 wwin->flags.net_has_icon_title = 1;
139 else
140 wGetIconName(dpy, wwin->client_win, &icon->icon_name);
142 icon->tile_type = TILE_NORMAL;
144 wIconUpdate(icon);
146 XFlush(dpy);
148 WMAddNotificationObserver(appearanceObserver, icon, WNIconAppearanceSettingsChanged, icon);
149 WMAddNotificationObserver(tileObserver, icon, WNIconTileSettingsChanged, icon);
150 return icon;
153 WIcon *wIconCreateWithIconFile(WScreen *scr, char *iconfile, int tile)
155 WIcon *icon;
157 icon = wIconCreateCore(scr, 0, 0);
159 icon->file_image = get_default_icon_rimage(scr, iconfile, wPreferences.icon_size);
160 icon->file = wstrdup(iconfile);
162 icon->tile_type = tile;
164 wIconUpdate(icon);
166 WMAddNotificationObserver(appearanceObserver, icon, WNIconAppearanceSettingsChanged, icon);
167 WMAddNotificationObserver(tileObserver, icon, WNIconTileSettingsChanged, icon);
169 return icon;
172 static WIcon *wIconCreateCore(WScreen *scr, int coord_x, int coord_y)
174 WIcon *icon;
175 unsigned long vmask = 0;
176 XSetWindowAttributes attribs;
178 icon = wmalloc(sizeof(WIcon));
179 icon->core = wCoreCreateTopLevel(scr,
180 coord_x,
181 coord_y,
182 wPreferences.icon_size,
183 wPreferences.icon_size,
184 0, scr->w_depth, scr->w_visual, scr->w_colormap);
186 if (wPreferences.use_saveunders) {
187 vmask = CWSaveUnder;
188 attribs.save_under = True;
191 /* a white border for selecting it */
192 vmask |= CWBorderPixel;
193 attribs.border_pixel = scr->white_pixel;
195 XChangeWindowAttributes(dpy, icon->core->window, vmask, &attribs);
197 /* will be overriden if this is a application icon */
198 icon->core->descriptor.handle_mousedown = miniwindowMouseDown;
199 icon->core->descriptor.handle_expose = miniwindowExpose;
200 icon->core->descriptor.parent_type = WCLASS_MINIWINDOW;
201 icon->core->descriptor.parent = icon;
203 icon->core->stacking = wmalloc(sizeof(WStacking));
204 icon->core->stacking->above = NULL;
205 icon->core->stacking->under = NULL;
206 icon->core->stacking->window_level = NORMAL_ICON_LEVEL;
207 icon->core->stacking->child_of = NULL;
209 return icon;
212 void wIconDestroy(WIcon * icon)
214 WCoreWindow *core = icon->core;
215 WScreen *scr = core->screen_ptr;
217 WMRemoveNotificationObserver(icon);
219 if (icon->handlerID)
220 WMDeleteTimerHandler(icon->handlerID);
222 if (icon->icon_win) {
223 int x = 0, y = 0;
225 if (icon->owner) {
226 x = icon->owner->icon_x;
227 y = icon->owner->icon_y;
229 XUnmapWindow(dpy, icon->icon_win);
230 XReparentWindow(dpy, icon->icon_win, scr->root_win, x, y);
232 if (icon->icon_name)
233 XFree(icon->icon_name);
235 if (icon->pixmap)
236 XFreePixmap(dpy, icon->pixmap);
238 if (icon->file)
239 wfree(icon->file);
241 if (icon->file_image != NULL)
242 RReleaseImage(icon->file_image);
244 wCoreDestroy(icon->core);
245 wfree(icon);
248 static void drawIconTitle(WScreen * scr, Pixmap pixmap, int height)
250 XFillRectangle(dpy, pixmap, scr->icon_title_texture->normal_gc, 0, 0, wPreferences.icon_size, height + 1);
251 XDrawLine(dpy, pixmap, scr->icon_title_texture->light_gc, 0, 0, wPreferences.icon_size, 0);
252 XDrawLine(dpy, pixmap, scr->icon_title_texture->light_gc, 0, 0, 0, height + 1);
253 XDrawLine(dpy, pixmap, scr->icon_title_texture->dim_gc,
254 wPreferences.icon_size - 1, 0, wPreferences.icon_size - 1, height + 1);
257 static Pixmap makeIcon(WScreen *scr, RImage *icon, int titled, int shadowed, int tileType, int highlighted)
259 RImage *tile;
260 Pixmap pixmap;
261 int x, y, sx, sy;
262 unsigned w, h;
263 int theight = 0;
265 if (tileType == TILE_NORMAL) {
266 tile = RCloneImage(scr->icon_tile);
267 } else {
268 assert(scr->clip_tile);
269 tile = RCloneImage(scr->clip_tile);
272 if (icon) {
273 w = (icon->width > wPreferences.icon_size)
274 ? wPreferences.icon_size : icon->width;
275 x = (wPreferences.icon_size - w) / 2;
276 sx = (icon->width - w) / 2;
278 if (titled)
279 theight = WMFontHeight(scr->icon_title_font);
281 h = (icon->height + theight > wPreferences.icon_size
282 ? wPreferences.icon_size - theight : icon->height);
283 y = theight + (wPreferences.icon_size - theight - h) / 2;
284 sy = (icon->height - h) / 2;
286 RCombineArea(tile, icon, sx, sy, w, h, x, y);
289 if (shadowed) {
290 RColor color;
292 color.red = scr->icon_back_texture->light.red >> 8;
293 color.green = scr->icon_back_texture->light.green >> 8;
294 color.blue = scr->icon_back_texture->light.blue >> 8;
295 color.alpha = 150; /* about 60% */
296 RClearImage(tile, &color);
299 if (highlighted) {
300 RColor color;
302 color.red = color.green = color.blue = 0;
303 color.alpha = 160;
304 RLightImage(tile, &color);
307 if (!RConvertImage(scr->rcontext, tile, &pixmap))
308 wwarning(_("error rendering image:%s"), RMessageForError(RErrorCode));
310 RReleaseImage(tile);
312 if (titled)
313 drawIconTitle(scr, pixmap, theight);
315 return pixmap;
318 void wIconChangeTitle(WIcon * icon, char *new_title)
320 int changed;
322 changed = (new_title == NULL && icon->icon_name != NULL)
323 || (new_title != NULL && icon->icon_name == NULL);
325 if (icon->icon_name != NULL)
326 XFree(icon->icon_name);
328 icon->icon_name = new_title;
330 if (changed)
331 icon->force_paint = 1;
332 wIconPaint(icon);
335 RImage *wIconValidateIconSize(RImage *icon, int max_size)
337 RImage *nimage;
339 if (!icon)
340 return NULL;
342 /* We should hold "ICON_BORDER" (~2) pixels to include the icon border */
343 if ((icon->width - max_size) > -ICON_BORDER ||
344 (icon->height - max_size) > -ICON_BORDER) {
345 nimage = RScaleImage(icon, max_size - ICON_BORDER,
346 (icon->height * (max_size - ICON_BORDER) / icon->width));
347 RReleaseImage(icon);
348 icon = nimage;
351 return icon;
354 Bool wIconChangeImageFile(WIcon * icon, char *file)
356 WScreen *scr = icon->core->screen_ptr;
357 RImage *image;
358 char *path;
359 int error = 0;
361 if (icon->file_image)
362 RReleaseImage(icon->file_image);
364 if (!file) {
365 icon->file_image = NULL;
366 wIconUpdate(icon);
367 return True;
370 path = FindImage(wPreferences.icon_path, file);
372 if (path && (image = RLoadImage(scr->rcontext, path, 0))) {
373 icon->file_image = wIconValidateIconSize(image, wPreferences.icon_size);
374 wIconUpdate(icon);
375 } else {
376 error = 1;
379 if (path)
380 wfree(path);
382 return !error;
385 static char *get_name_for_wwin(WWindow *wwin)
387 return get_name_for_instance_class(wwin->wm_instance, wwin->wm_class);
390 char *get_name_for_instance_class(char *wm_instance, char *wm_class)
392 char *suffix;
393 int len;
395 if (wm_class && wm_instance) {
396 len = strlen(wm_class) + strlen(wm_instance) + 2;
397 suffix = wmalloc(len);
398 snprintf(suffix, len, "%s.%s", wm_instance, wm_class);
399 } else if (wm_class) {
400 len = strlen(wm_class) + 1;
401 suffix = wmalloc(len);
402 snprintf(suffix, len, "%s", wm_class);
403 } else if (wm_instance) {
404 len = strlen(wm_instance) + 1;
405 suffix = wmalloc(len);
406 snprintf(suffix, len, "%s", wm_instance);
407 } else {
408 return NULL;
411 return suffix;
414 static char *get_icon_cache_path(void)
416 char *prefix, *path;
417 int len, ret;
419 prefix = wusergnusteppath();
420 len = strlen(prefix) + strlen(CACHE_ICON_PATH) + 2;
421 path = wmalloc(len);
422 snprintf(path, len, "%s%s/", prefix, CACHE_ICON_PATH);
424 /* If the folder exists, exit */
425 if (access(path, F_OK) == 0)
426 return path;
428 /* Create the folder */
429 ret = wmkdirhier((const char *) path);
431 /* Exit 1 on success, 0 on failure */
432 if (ret == 1)
433 return path;
435 /* Fail */
436 wfree(path);
437 return NULL;
440 static RImage *get_wwindow_image_from_wmhints(WWindow *wwin, WIcon *icon)
442 RImage *image = NULL;
443 XWMHints *hints = wwin->wm_hints;
445 if (hints && (hints->flags & IconPixmapHint) && hints->icon_pixmap != None)
446 image = RCreateImageFromDrawable(icon->core->screen_ptr->rcontext,
447 hints->icon_pixmap,
448 (hints->flags & IconMaskHint)
449 ? hints->icon_mask : None);
451 return image;
455 * wIconStore--
456 * Stores the client supplied icon at CACHE_ICON_PATH
457 * and returns the path for that icon. Returns NULL if there is no
458 * client supplied icon or on failure.
460 * Side effects:
461 * New directories might be created.
463 char *wIconStore(WIcon * icon)
465 char *path, *dir_path, *file;
466 int len = 0;
467 RImage *image = NULL;
468 WWindow *wwin = icon->owner;
470 if (!wwin)
471 return NULL;
473 dir_path = get_icon_cache_path();
474 if (!dir_path)
475 return NULL;
477 file = get_name_for_wwin(wwin);
478 if (!file) {
479 wfree(dir_path);
480 return NULL;
483 len = strlen(dir_path) + strlen(file) + 5;
484 path = wmalloc(len);
485 snprintf(path, len, "%s%s.xpm", dir_path, file);
486 wfree(dir_path);
487 wfree(file);
489 /* If icon exists, exit */
490 if (access(path, F_OK) == 0)
491 return path;
493 if (wwin->net_icon_image)
494 image = RRetainImage(wwin->net_icon_image);
495 else
496 image = get_wwindow_image_from_wmhints(wwin, icon);
498 if (!image) {
499 wfree(path);
500 return NULL;
503 if (!RSaveImage(image, path, "XPM")) {
504 wfree(path);
505 path = NULL;
508 RReleaseImage(image);
510 return path;
513 static void cycleColor(void *data)
515 WIcon *icon = (WIcon *) data;
516 WScreen *scr = icon->core->screen_ptr;
517 XGCValues gcv;
519 icon->step--;
520 gcv.dash_offset = icon->step;
521 XChangeGC(dpy, scr->icon_select_gc, GCDashOffset, &gcv);
523 XDrawRectangle(dpy, icon->core->window, scr->icon_select_gc, 0, 0,
524 icon->core->width - 1, icon->core->height - 1);
525 icon->handlerID = WMAddTimerHandler(COLOR_CYCLE_DELAY, cycleColor, icon);
528 #ifdef NEWAPPICON
529 void wIconSetHighlited(WIcon *icon, Bool flag)
531 if (icon->highlighted == flag)
532 return;
534 icon->highlighted = flag;
535 icon->force_paint = True;
536 wIconPaint(icon);
538 #endif
540 void wIconSelect(WIcon * icon)
542 WScreen *scr = icon->core->screen_ptr;
543 icon->selected = !icon->selected;
545 if (icon->selected) {
546 icon->step = 0;
547 if (!wPreferences.dont_blink)
548 icon->handlerID = WMAddTimerHandler(10, cycleColor, icon);
549 else
550 XDrawRectangle(dpy, icon->core->window, scr->icon_select_gc, 0, 0,
551 icon->core->width - 1, icon->core->height - 1);
552 } else {
553 if (icon->handlerID) {
554 WMDeleteTimerHandler(icon->handlerID);
555 icon->handlerID = NULL;
557 XClearArea(dpy, icon->core->window, 0, 0, icon->core->width, icon->core->height, True);
561 void wIconUpdate(WIcon * icon)
563 WScreen *scr = icon->core->screen_ptr;
564 WWindow *wwin = icon->owner;
566 assert(scr->icon_tile != NULL);
568 if (icon->pixmap != None)
569 XFreePixmap(dpy, icon->pixmap);
571 icon->pixmap = None;
573 if (wwin && WFLAGP(wwin, always_user_icon)) {
574 /* Forced use user_icon */
575 get_pixmap_icon_from_user_icon(scr, icon);
576 } else if (icon->icon_win != None) {
577 /* Get the Pixmap from the WIcon */
578 get_pixmap_icon_from_icon_win(icon);
579 } else if (wwin && wwin->net_icon_image) {
580 /* Use _NET_WM_ICON icon */
581 icon->pixmap = makeIcon(scr, wwin->net_icon_image, icon->show_title,
582 icon->shadowed, icon->tile_type, icon->highlighted);
583 } else if (wwin && wwin->wm_hints && (wwin->wm_hints->flags & IconPixmapHint)) {
584 /* Get the Pixmap from the wm_hints, else, from the user */
585 if (get_pixmap_icon_from_wm_hints(scr, wwin, icon))
586 get_pixmap_icon_from_user_icon(scr, icon);
587 } else {
588 /* Get the Pixmap from the user */
589 get_pixmap_icon_from_user_icon(scr, icon);
592 /* No pixmap, set default background */
593 if (icon->pixmap != None)
594 XSetWindowBackgroundPixmap(dpy, icon->core->window, icon->pixmap);
596 /* Paint it */
597 XClearWindow(dpy, icon->core->window);
598 wIconPaint(icon);
601 void get_pixmap_icon_from_user_icon(WScreen *scr, WIcon * icon)
603 RImage *image = NULL;
604 char *path;
605 char *file;
607 if (icon->file_image) {
608 icon->pixmap = makeIcon(scr, icon->file_image, icon->show_title,
609 icon->shadowed, icon->tile_type, icon->highlighted);
610 } else {
611 /* make default icons */
612 if (!scr->def_icon_pixmap) {
613 /* Get the default icon */
614 file = wDefaultGetIconFile(NULL, NULL, True);
615 if (file) {
616 path = FindImage(wPreferences.icon_path, file);
617 if (path) {
618 image = RLoadImage(scr->rcontext, path, 0);
619 if (!image)
620 wwarning(_("could not load default icon \"%s\":%s"),
621 file, RMessageForError(RErrorCode));
622 wfree(path);
623 } else {
624 wwarning(_("could not find default icon \"%s\""), file);
628 image = wIconValidateIconSize(image, wPreferences.icon_size);
629 scr->def_icon_pixmap = makeIcon(scr, image, False, False, icon->tile_type, icon->highlighted);
630 scr->def_ticon_pixmap = makeIcon(scr, image, True, False, icon->tile_type, icon->highlighted);
631 if (image)
632 RReleaseImage(image);
635 if (icon->show_title)
636 XSetWindowBackgroundPixmap(dpy, icon->core->window, scr->def_ticon_pixmap);
637 else
638 XSetWindowBackgroundPixmap(dpy, icon->core->window, scr->def_icon_pixmap);
640 icon->pixmap = None;
644 /* Get the Pixmap from the WIcon of the WWindow */
645 void get_pixmap_icon_from_icon_win(WIcon * icon)
647 XWindowAttributes attr;
648 WScreen *scr = icon->core->screen_ptr;
649 int title_height = WMFontHeight(scr->icon_title_font);
650 unsigned int width, height, depth;
651 int theight;
652 int resize = 0;
653 Pixmap pixmap;
655 getSize(icon->icon_win, &width, &height, &depth);
657 if (width > wPreferences.icon_size) {
658 resize = 1;
659 width = wPreferences.icon_size;
662 if (height > wPreferences.icon_size) {
663 resize = 1;
664 height = wPreferences.icon_size;
667 if (icon->show_title && (height + title_height < wPreferences.icon_size)) {
668 pixmap = XCreatePixmap(dpy, scr->w_win, wPreferences.icon_size,
669 wPreferences.icon_size, scr->w_depth);
670 XSetClipMask(dpy, scr->copy_gc, None);
671 XCopyArea(dpy, scr->icon_tile_pixmap, pixmap, scr->copy_gc, 0, 0,
672 wPreferences.icon_size, wPreferences.icon_size, 0, 0);
673 drawIconTitle(scr, pixmap, title_height);
674 theight = title_height;
675 } else {
676 pixmap = None;
677 theight = 0;
678 XSetWindowBackgroundPixmap(dpy, icon->core->window, scr->icon_tile_pixmap);
681 XSetWindowBorderWidth(dpy, icon->icon_win, 0);
682 XReparentWindow(dpy, icon->icon_win, icon->core->window,
683 (wPreferences.icon_size - width) / 2,
684 theight + (wPreferences.icon_size - height - theight) / 2);
685 if (resize)
686 XResizeWindow(dpy, icon->icon_win, width, height);
688 XMapWindow(dpy, icon->icon_win);
689 XAddToSaveSet(dpy, icon->icon_win);
691 /* Save it */
692 icon->pixmap = pixmap;
694 if ((XGetWindowAttributes(dpy, icon->icon_win, &attr)) &&
695 (attr.all_event_masks & ButtonPressMask))
696 wHackedGrabButton(Button1, MOD_MASK, icon->core->window, True,
697 ButtonPressMask, GrabModeSync, GrabModeAsync,
698 None, wCursor[WCUR_ARROW]);
701 /* Get the Pixmap from the XWindow wm_hints */
702 int get_pixmap_icon_from_wm_hints(WScreen *scr, WWindow *wwin, WIcon *icon)
704 Window jw;
705 Pixmap pixmap;
706 unsigned int w, h, ju, d;
707 int ji, x, y;
708 int title_height = WMFontHeight(scr->icon_title_font);
710 if (!XGetGeometry(dpy, wwin->wm_hints->icon_pixmap, &jw, &ji, &ji, &w, &h, &ju, &d)) {
711 icon->owner->wm_hints->flags &= ~IconPixmapHint;
712 return 1;
715 pixmap = XCreatePixmap(dpy, icon->core->window, wPreferences.icon_size,
716 wPreferences.icon_size, scr->w_depth);
717 XSetClipMask(dpy, scr->copy_gc, None);
718 XCopyArea(dpy, scr->icon_tile_pixmap, pixmap, scr->copy_gc, 0, 0,
719 wPreferences.icon_size, wPreferences.icon_size, 0, 0);
721 if (w > wPreferences.icon_size)
722 w = wPreferences.icon_size;
723 x = (wPreferences.icon_size - w) / 2;
725 if (icon->show_title && (title_height < wPreferences.icon_size)) {
726 drawIconTitle(scr, pixmap, title_height);
728 if (h > wPreferences.icon_size - title_height - 2) {
729 h = wPreferences.icon_size - title_height - 2;
730 y = title_height + 1;
731 } else {
732 y = (wPreferences.icon_size - h - title_height) / 2 + title_height + 1;
734 } else {
735 if (w > wPreferences.icon_size)
736 w = wPreferences.icon_size;
737 y = (wPreferences.icon_size - h) / 2;
740 if (wwin->wm_hints->flags & IconMaskHint)
741 XSetClipMask(dpy, scr->copy_gc, wwin->wm_hints->icon_mask);
743 XSetClipOrigin(dpy, scr->copy_gc, x, y);
745 if (d != scr->w_depth) {
746 XSetForeground(dpy, scr->copy_gc, scr->black_pixel);
747 XSetBackground(dpy, scr->copy_gc, scr->white_pixel);
748 XCopyPlane(dpy, wwin->wm_hints->icon_pixmap, pixmap, scr->copy_gc, 0, 0, w, h, x, y, 1);
749 } else {
750 XCopyArea(dpy, wwin->wm_hints->icon_pixmap, pixmap, scr->copy_gc, 0, 0, w, h, x, y);
753 XSetClipOrigin(dpy, scr->copy_gc, 0, 0);
755 icon->pixmap = pixmap;
756 return (0);
759 void wIconPaint(WIcon * icon)
761 WScreen *scr = icon->core->screen_ptr;
762 int x;
763 char *tmp;
765 if (icon->force_paint) {
766 icon->force_paint = 0;
767 wIconUpdate(icon);
768 return;
771 XClearWindow(dpy, icon->core->window);
773 /* draw the icon title */
774 if (icon->show_title && icon->icon_name != NULL) {
775 int l;
776 int w;
778 tmp = ShrinkString(scr->icon_title_font, icon->icon_name, wPreferences.icon_size - 4);
779 w = WMWidthOfString(scr->icon_title_font, tmp, l = strlen(tmp));
781 if (w > icon->core->width - 4)
782 x = (icon->core->width - 4) - w;
783 else
784 x = (icon->core->width - w) / 2;
786 WMDrawString(scr->wmscreen, icon->core->window, scr->icon_title_color,
787 scr->icon_title_font, x, 1, tmp, l);
788 wfree(tmp);
791 if (icon->selected)
792 XDrawRectangle(dpy, icon->core->window, scr->icon_select_gc, 0, 0,
793 icon->core->width - 1, icon->core->height - 1);
796 /******************************************************************/
798 static void miniwindowExpose(WObjDescriptor * desc, XEvent * event)
800 wIconPaint(desc->parent);
803 static void miniwindowDblClick(WObjDescriptor * desc, XEvent * event)
805 WIcon *icon = desc->parent;
807 assert(icon->owner != NULL);
809 wDeiconifyWindow(icon->owner);
812 static void miniwindowMouseDown(WObjDescriptor * desc, XEvent * event)
814 WIcon *icon = desc->parent;
815 WWindow *wwin = icon->owner;
816 XEvent ev;
817 int x = wwin->icon_x, y = wwin->icon_y;
818 int dx = event->xbutton.x, dy = event->xbutton.y;
819 int grabbed = 0;
820 int clickButton = event->xbutton.button;
821 Bool hasMoved = False;
823 if (WCHECK_STATE(WSTATE_MODAL))
824 return;
826 if (IsDoubleClick(icon->core->screen_ptr, event)) {
827 miniwindowDblClick(desc, event);
828 return;
831 if (event->xbutton.button == Button1) {
832 if (event->xbutton.state & MOD_MASK)
833 wLowerFrame(icon->core);
834 else
835 wRaiseFrame(icon->core);
836 if (event->xbutton.state & ShiftMask) {
837 wIconSelect(icon);
838 wSelectWindow(icon->owner, !wwin->flags.selected);
840 } else if (event->xbutton.button == Button3) {
841 WObjDescriptor *desc;
843 OpenMiniwindowMenu(wwin, event->xbutton.x_root, event->xbutton.y_root);
845 /* allow drag select of menu */
846 desc = &wwin->screen_ptr->window_menu->menu->descriptor;
847 event->xbutton.send_event = True;
848 (*desc->handle_mousedown) (desc, event);
850 return;
853 if (XGrabPointer(dpy, icon->core->window, False, ButtonMotionMask
854 | ButtonReleaseMask | ButtonPressMask, GrabModeAsync,
855 GrabModeAsync, None, None, CurrentTime) != GrabSuccess) {
857 while (1) {
858 WMMaskEvent(dpy, PointerMotionMask | ButtonReleaseMask | ButtonPressMask
859 | ButtonMotionMask | ExposureMask, &ev);
860 switch (ev.type) {
861 case Expose:
862 WMHandleEvent(&ev);
863 break;
865 case MotionNotify:
866 hasMoved = True;
867 if (!grabbed) {
868 if (abs(dx - ev.xmotion.x) >= MOVE_THRESHOLD
869 || abs(dy - ev.xmotion.y) >= MOVE_THRESHOLD) {
870 XChangeActivePointerGrab(dpy, ButtonMotionMask
871 | ButtonReleaseMask | ButtonPressMask,
872 wCursor[WCUR_MOVE], CurrentTime);
873 grabbed = 1;
874 } else {
875 break;
878 x = ev.xmotion.x_root - dx;
879 y = ev.xmotion.y_root - dy;
880 XMoveWindow(dpy, icon->core->window, x, y);
881 break;
883 case ButtonPress:
884 break;
886 case ButtonRelease:
887 if (ev.xbutton.button != clickButton)
888 break;
890 if (wwin->icon_x != x || wwin->icon_y != y)
891 wwin->flags.icon_moved = 1;
893 XMoveWindow(dpy, icon->core->window, x, y);
895 wwin->icon_x = x;
896 wwin->icon_y = y;
897 XUngrabPointer(dpy, CurrentTime);
899 if (wPreferences.auto_arrange_icons)
900 wArrangeIcons(wwin->screen_ptr, True);
901 if (wPreferences.single_click && !hasMoved)
902 miniwindowDblClick(desc, event);
903 return;