812301e752f707576850f42428a6fb2fec502694
[wmaker-crm.git] / src / icon.c
blob812301e752f707576850f42428a6fb2fec502694
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 file = wDefaultGetIconFile(wwin->wm_instance, wwin->wm_class, False);
132 if (file)
133 icon->file = wstrdup(file);
135 icon->icon_name = wNETWMGetIconName(wwin->client_win);
136 if (icon->icon_name)
137 wwin->flags.net_has_icon_title = 1;
138 else
139 wGetIconName(dpy, wwin->client_win, &icon->icon_name);
141 icon->tile_type = TILE_NORMAL;
143 wIconUpdate(icon);
145 XFlush(dpy);
147 WMAddNotificationObserver(appearanceObserver, icon, WNIconAppearanceSettingsChanged, icon);
148 WMAddNotificationObserver(tileObserver, icon, WNIconTileSettingsChanged, icon);
149 return icon;
152 WIcon *wIconCreateWithIconFile(WScreen *scr, char *iconfile, int tile)
154 WIcon *icon;
156 icon = wIconCreateCore(scr, 0, 0);
158 if (iconfile) {
159 icon->file_image = RLoadImage(scr->rcontext, iconfile, 0);
160 if (!icon->file_image)
161 wwarning(_("error loading image file \"%s\": %s"), iconfile, RMessageForError(RErrorCode));
163 icon->file_image = wIconValidateIconSize(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(RImage *icon, int max_size)
342 RImage *nimage;
344 if (!icon)
345 return NULL;
347 /* We should hold "ICON_BORDER" (~2) pixels to include the icon border */
348 if ((icon->width - max_size) > -ICON_BORDER ||
349 (icon->height - max_size) > -ICON_BORDER) {
350 nimage = RScaleImage(icon, max_size - ICON_BORDER,
351 (icon->height * (max_size - ICON_BORDER) / icon->width));
352 RReleaseImage(icon);
353 icon = nimage;
356 return icon;
359 Bool wIconChangeImageFile(WIcon * icon, char *file)
361 WScreen *scr = icon->core->screen_ptr;
362 RImage *image;
363 char *path;
364 int error = 0;
366 if (icon->file_image)
367 RReleaseImage(icon->file_image);
369 if (!file) {
370 icon->file_image = NULL;
371 wIconUpdate(icon);
372 return True;
375 path = FindImage(wPreferences.icon_path, file);
377 if (path && (image = RLoadImage(scr->rcontext, path, 0))) {
378 icon->file_image = wIconValidateIconSize(image, wPreferences.icon_size);
379 wIconUpdate(icon);
380 } else {
381 error = 1;
384 if (path)
385 wfree(path);
387 return !error;
390 static char *get_name_for_wwin(WWindow *wwin)
392 return get_name_for_instance_class(wwin->wm_instance, wwin->wm_class);
395 char *get_name_for_instance_class(char *wm_instance, char *wm_class)
397 char *suffix;
398 int len;
400 if (wm_class && wm_instance) {
401 len = strlen(wm_class) + strlen(wm_instance) + 2;
402 suffix = wmalloc(len);
403 snprintf(suffix, len, "%s.%s", wm_instance, wm_class);
404 } else if (wm_class) {
405 len = strlen(wm_class) + 1;
406 suffix = wmalloc(len);
407 snprintf(suffix, len, "%s", wm_class);
408 } else if (wm_instance) {
409 len = strlen(wm_instance) + 1;
410 suffix = wmalloc(len);
411 snprintf(suffix, len, "%s", wm_instance);
412 } else {
413 return NULL;
416 return suffix;
419 static char *get_icon_cache_path(void)
421 char *prefix, *path;
422 int len, ret;
424 prefix = wusergnusteppath();
425 len = strlen(prefix) + strlen(CACHE_ICON_PATH) + 2;
426 path = wmalloc(len);
427 snprintf(path, len, "%s%s/", prefix, CACHE_ICON_PATH);
429 /* If the folder exists, exit */
430 if (access(path, F_OK) == 0)
431 return path;
433 /* Create the folder */
434 ret = wmkdirhier((const char *) path);
436 /* Exit 1 on success, 0 on failure */
437 if (ret == 1)
438 return path;
440 /* Fail */
441 wfree(path);
442 return NULL;
445 static RImage *get_wwindow_image_from_wmhints(WWindow *wwin, WIcon *icon)
447 RImage *image = NULL;
448 XWMHints *hints = wwin->wm_hints;
450 if (hints && (hints->flags & IconPixmapHint) && hints->icon_pixmap != None)
451 image = RCreateImageFromDrawable(icon->core->screen_ptr->rcontext,
452 hints->icon_pixmap,
453 (hints->flags & IconMaskHint)
454 ? hints->icon_mask : None);
456 return image;
460 * wIconStore--
461 * Stores the client supplied icon at CACHE_ICON_PATH
462 * and returns the path for that icon. Returns NULL if there is no
463 * client supplied icon or on failure.
465 * Side effects:
466 * New directories might be created.
468 char *wIconStore(WIcon * icon)
470 char *path, *dir_path, *file;
471 int len = 0;
472 RImage *image = NULL;
473 WWindow *wwin = icon->owner;
475 if (!wwin)
476 return NULL;
478 dir_path = get_icon_cache_path();
479 if (!dir_path)
480 return NULL;
482 file = get_name_for_wwin(wwin);
483 if (!file) {
484 wfree(dir_path);
485 return NULL;
488 len = strlen(dir_path) + strlen(file) + 5;
489 path = wmalloc(len);
490 snprintf(path, len, "%s%s.xpm", dir_path, file);
491 wfree(dir_path);
492 wfree(file);
494 /* If icon exists, exit */
495 if (access(path, F_OK) == 0)
496 return path;
498 if (wwin->net_icon_image)
499 image = RRetainImage(wwin->net_icon_image);
500 else
501 image = get_wwindow_image_from_wmhints(wwin, icon);
503 if (!image) {
504 wfree(path);
505 return NULL;
508 if (!RSaveImage(image, path, "XPM")) {
509 wfree(path);
510 path = NULL;
513 RReleaseImage(image);
515 return path;
518 static void cycleColor(void *data)
520 WIcon *icon = (WIcon *) data;
521 WScreen *scr = icon->core->screen_ptr;
522 XGCValues gcv;
524 icon->step--;
525 gcv.dash_offset = icon->step;
526 XChangeGC(dpy, scr->icon_select_gc, GCDashOffset, &gcv);
528 XDrawRectangle(dpy, icon->core->window, scr->icon_select_gc, 0, 0,
529 icon->core->width - 1, icon->core->height - 1);
530 icon->handlerID = WMAddTimerHandler(COLOR_CYCLE_DELAY, cycleColor, icon);
533 #ifdef NEWAPPICON
534 void wIconSetHighlited(WIcon *icon, Bool flag)
536 if (icon->highlighted == flag)
537 return;
539 icon->highlighted = flag;
540 icon->force_paint = True;
541 wIconPaint(icon);
543 #endif
545 void wIconSelect(WIcon * icon)
547 WScreen *scr = icon->core->screen_ptr;
548 icon->selected = !icon->selected;
550 if (icon->selected) {
551 icon->step = 0;
552 if (!wPreferences.dont_blink)
553 icon->handlerID = WMAddTimerHandler(10, cycleColor, icon);
554 else
555 XDrawRectangle(dpy, icon->core->window, scr->icon_select_gc, 0, 0,
556 icon->core->width - 1, icon->core->height - 1);
557 } else {
558 if (icon->handlerID) {
559 WMDeleteTimerHandler(icon->handlerID);
560 icon->handlerID = NULL;
562 XClearArea(dpy, icon->core->window, 0, 0, icon->core->width, icon->core->height, True);
566 void wIconUpdate(WIcon * icon)
568 WScreen *scr = icon->core->screen_ptr;
569 WWindow *wwin = icon->owner;
571 assert(scr->icon_tile != NULL);
573 if (icon->pixmap != None)
574 XFreePixmap(dpy, icon->pixmap);
576 icon->pixmap = None;
578 if (wwin && WFLAGP(wwin, always_user_icon)) {
579 /* Forced use user_icon */
580 get_pixmap_icon_from_user_icon(scr, icon);
581 } else if (icon->icon_win != None) {
582 /* Get the Pixmap from the WIcon */
583 get_pixmap_icon_from_icon_win(icon);
584 } else if (wwin && wwin->net_icon_image) {
585 /* Use _NET_WM_ICON icon */
586 icon->pixmap = makeIcon(scr, wwin->net_icon_image, icon->show_title,
587 icon->shadowed, icon->tile_type, icon->highlighted);
588 } else if (wwin && wwin->wm_hints && (wwin->wm_hints->flags & IconPixmapHint)) {
589 /* Get the Pixmap from the wm_hints, else, from the user */
590 if (get_pixmap_icon_from_wm_hints(scr, wwin, icon))
591 get_pixmap_icon_from_user_icon(scr, icon);
592 } else {
593 /* Get the Pixmap from the user */
594 get_pixmap_icon_from_user_icon(scr, icon);
597 /* No pixmap, set default background */
598 if (icon->pixmap != None)
599 XSetWindowBackgroundPixmap(dpy, icon->core->window, icon->pixmap);
601 /* Paint it */
602 XClearWindow(dpy, icon->core->window);
603 wIconPaint(icon);
606 void get_pixmap_icon_from_user_icon(WScreen *scr, WIcon * icon)
608 RImage *image = NULL;
609 char *path;
610 char *file;
612 if (icon->file_image) {
613 icon->pixmap = makeIcon(scr, icon->file_image, icon->show_title,
614 icon->shadowed, icon->tile_type, icon->highlighted);
615 } else {
616 /* make default icons */
617 if (!scr->def_icon_pixmap) {
618 file = wDefaultGetIconFile(NULL, NULL, False);
619 if (file) {
620 path = FindImage(wPreferences.icon_path, file);
621 if (path) {
622 image = RLoadImage(scr->rcontext, path, 0);
623 if (!image)
624 wwarning(_("could not load default icon \"%s\":%s"),
625 file, RMessageForError(RErrorCode));
626 wfree(path);
627 } else {
628 wwarning(_("could not find default icon \"%s\""), file);
632 image = wIconValidateIconSize(image, wPreferences.icon_size);
633 scr->def_icon_pixmap = makeIcon(scr, image, False, False, icon->tile_type, icon->highlighted);
634 scr->def_ticon_pixmap = makeIcon(scr, image, True, False, icon->tile_type, icon->highlighted);
635 if (image)
636 RReleaseImage(image);
639 if (icon->show_title)
640 XSetWindowBackgroundPixmap(dpy, icon->core->window, scr->def_ticon_pixmap);
641 else
642 XSetWindowBackgroundPixmap(dpy, icon->core->window, scr->def_icon_pixmap);
644 icon->pixmap = None;
648 /* Get the Pixmap from the WIcon of the WWindow */
649 void get_pixmap_icon_from_icon_win(WIcon * icon)
651 XWindowAttributes attr;
652 WScreen *scr = icon->core->screen_ptr;
653 int title_height = WMFontHeight(scr->icon_title_font);
654 unsigned int width, height, depth;
655 int theight;
656 int resize = 0;
657 Pixmap pixmap;
659 getSize(icon->icon_win, &width, &height, &depth);
661 if (width > wPreferences.icon_size) {
662 resize = 1;
663 width = wPreferences.icon_size;
666 if (height > wPreferences.icon_size) {
667 resize = 1;
668 height = wPreferences.icon_size;
671 if (icon->show_title && (height + title_height < wPreferences.icon_size)) {
672 pixmap = XCreatePixmap(dpy, scr->w_win, wPreferences.icon_size,
673 wPreferences.icon_size, scr->w_depth);
674 XSetClipMask(dpy, scr->copy_gc, None);
675 XCopyArea(dpy, scr->icon_tile_pixmap, pixmap, scr->copy_gc, 0, 0,
676 wPreferences.icon_size, wPreferences.icon_size, 0, 0);
677 drawIconTitle(scr, pixmap, title_height);
678 theight = title_height;
679 } else {
680 pixmap = None;
681 theight = 0;
682 XSetWindowBackgroundPixmap(dpy, icon->core->window, scr->icon_tile_pixmap);
685 XSetWindowBorderWidth(dpy, icon->icon_win, 0);
686 XReparentWindow(dpy, icon->icon_win, icon->core->window,
687 (wPreferences.icon_size - width) / 2,
688 theight + (wPreferences.icon_size - height - theight) / 2);
689 if (resize)
690 XResizeWindow(dpy, icon->icon_win, width, height);
692 XMapWindow(dpy, icon->icon_win);
693 XAddToSaveSet(dpy, icon->icon_win);
695 /* Save it */
696 icon->pixmap = pixmap;
698 if ((XGetWindowAttributes(dpy, icon->icon_win, &attr)) &&
699 (attr.all_event_masks & ButtonPressMask))
700 wHackedGrabButton(Button1, MOD_MASK, icon->core->window, True,
701 ButtonPressMask, GrabModeSync, GrabModeAsync,
702 None, wCursor[WCUR_ARROW]);
705 /* Get the Pixmap from the XWindow wm_hints */
706 int get_pixmap_icon_from_wm_hints(WScreen *scr, WWindow *wwin, WIcon *icon)
708 Window jw;
709 Pixmap pixmap;
710 unsigned int w, h, ju, d;
711 int ji, x, y;
712 int title_height = WMFontHeight(scr->icon_title_font);
714 if (!XGetGeometry(dpy, wwin->wm_hints->icon_pixmap, &jw, &ji, &ji, &w, &h, &ju, &d)) {
715 icon->owner->wm_hints->flags &= ~IconPixmapHint;
716 return 1;
719 pixmap = XCreatePixmap(dpy, icon->core->window, wPreferences.icon_size,
720 wPreferences.icon_size, scr->w_depth);
721 XSetClipMask(dpy, scr->copy_gc, None);
722 XCopyArea(dpy, scr->icon_tile_pixmap, pixmap, scr->copy_gc, 0, 0,
723 wPreferences.icon_size, wPreferences.icon_size, 0, 0);
725 if (w > wPreferences.icon_size)
726 w = wPreferences.icon_size;
727 x = (wPreferences.icon_size - w) / 2;
729 if (icon->show_title && (title_height < wPreferences.icon_size)) {
730 drawIconTitle(scr, pixmap, title_height);
732 if (h > wPreferences.icon_size - title_height - 2) {
733 h = wPreferences.icon_size - title_height - 2;
734 y = title_height + 1;
735 } else {
736 y = (wPreferences.icon_size - h - title_height) / 2 + title_height + 1;
738 } else {
739 if (w > wPreferences.icon_size)
740 w = wPreferences.icon_size;
741 y = (wPreferences.icon_size - h) / 2;
744 if (wwin->wm_hints->flags & IconMaskHint)
745 XSetClipMask(dpy, scr->copy_gc, wwin->wm_hints->icon_mask);
747 XSetClipOrigin(dpy, scr->copy_gc, x, y);
749 if (d != scr->w_depth) {
750 XSetForeground(dpy, scr->copy_gc, scr->black_pixel);
751 XSetBackground(dpy, scr->copy_gc, scr->white_pixel);
752 XCopyPlane(dpy, wwin->wm_hints->icon_pixmap, pixmap, scr->copy_gc, 0, 0, w, h, x, y, 1);
753 } else {
754 XCopyArea(dpy, wwin->wm_hints->icon_pixmap, pixmap, scr->copy_gc, 0, 0, w, h, x, y);
757 XSetClipOrigin(dpy, scr->copy_gc, 0, 0);
759 icon->pixmap = pixmap;
760 return (0);
763 void wIconPaint(WIcon * icon)
765 WScreen *scr = icon->core->screen_ptr;
766 int x;
767 char *tmp;
769 if (icon->force_paint) {
770 icon->force_paint = 0;
771 wIconUpdate(icon);
772 return;
775 XClearWindow(dpy, icon->core->window);
777 /* draw the icon title */
778 if (icon->show_title && icon->icon_name != NULL) {
779 int l;
780 int w;
782 tmp = ShrinkString(scr->icon_title_font, icon->icon_name, wPreferences.icon_size - 4);
783 w = WMWidthOfString(scr->icon_title_font, tmp, l = strlen(tmp));
785 if (w > icon->core->width - 4)
786 x = (icon->core->width - 4) - w;
787 else
788 x = (icon->core->width - w) / 2;
790 WMDrawString(scr->wmscreen, icon->core->window, scr->icon_title_color,
791 scr->icon_title_font, x, 1, tmp, l);
792 wfree(tmp);
795 if (icon->selected)
796 XDrawRectangle(dpy, icon->core->window, scr->icon_select_gc, 0, 0,
797 icon->core->width - 1, icon->core->height - 1);
800 /******************************************************************/
802 static void miniwindowExpose(WObjDescriptor * desc, XEvent * event)
804 wIconPaint(desc->parent);
807 static void miniwindowDblClick(WObjDescriptor * desc, XEvent * event)
809 WIcon *icon = desc->parent;
811 assert(icon->owner != NULL);
813 wDeiconifyWindow(icon->owner);
816 static void miniwindowMouseDown(WObjDescriptor * desc, XEvent * event)
818 WIcon *icon = desc->parent;
819 WWindow *wwin = icon->owner;
820 XEvent ev;
821 int x = wwin->icon_x, y = wwin->icon_y;
822 int dx = event->xbutton.x, dy = event->xbutton.y;
823 int grabbed = 0;
824 int clickButton = event->xbutton.button;
825 Bool hasMoved = False;
827 if (WCHECK_STATE(WSTATE_MODAL))
828 return;
830 if (IsDoubleClick(icon->core->screen_ptr, event)) {
831 miniwindowDblClick(desc, event);
832 return;
835 if (event->xbutton.button == Button1) {
836 if (event->xbutton.state & MOD_MASK)
837 wLowerFrame(icon->core);
838 else
839 wRaiseFrame(icon->core);
840 if (event->xbutton.state & ShiftMask) {
841 wIconSelect(icon);
842 wSelectWindow(icon->owner, !wwin->flags.selected);
844 } else if (event->xbutton.button == Button3) {
845 WObjDescriptor *desc;
847 OpenMiniwindowMenu(wwin, event->xbutton.x_root, event->xbutton.y_root);
849 /* allow drag select of menu */
850 desc = &wwin->screen_ptr->window_menu->menu->descriptor;
851 event->xbutton.send_event = True;
852 (*desc->handle_mousedown) (desc, event);
854 return;
857 if (XGrabPointer(dpy, icon->core->window, False, ButtonMotionMask
858 | ButtonReleaseMask | ButtonPressMask, GrabModeAsync,
859 GrabModeAsync, None, None, CurrentTime) != GrabSuccess) {
861 while (1) {
862 WMMaskEvent(dpy, PointerMotionMask | ButtonReleaseMask | ButtonPressMask
863 | ButtonMotionMask | ExposureMask, &ev);
864 switch (ev.type) {
865 case Expose:
866 WMHandleEvent(&ev);
867 break;
869 case MotionNotify:
870 hasMoved = True;
871 if (!grabbed) {
872 if (abs(dx - ev.xmotion.x) >= MOVE_THRESHOLD
873 || abs(dy - ev.xmotion.y) >= MOVE_THRESHOLD) {
874 XChangeActivePointerGrab(dpy, ButtonMotionMask
875 | ButtonReleaseMask | ButtonPressMask,
876 wCursor[WCUR_MOVE], CurrentTime);
877 grabbed = 1;
878 } else {
879 break;
882 x = ev.xmotion.x_root - dx;
883 y = ev.xmotion.y_root - dy;
884 XMoveWindow(dpy, icon->core->window, x, y);
885 break;
887 case ButtonPress:
888 break;
890 case ButtonRelease:
891 if (ev.xbutton.button != clickButton)
892 break;
894 if (wwin->icon_x != x || wwin->icon_y != y)
895 wwin->flags.icon_moved = 1;
897 XMoveWindow(dpy, icon->core->window, x, y);
899 wwin->icon_x = x;
900 wwin->icon_y = y;
901 XUngrabPointer(dpy, CurrentTime);
903 if (wPreferences.auto_arrange_icons)
904 wArrangeIcons(wwin->screen_ptr, True);
905 if (wPreferences.single_click && !hasMoved)
906 miniwindowDblClick(desc, event);
907 return;