New functions in icon.c
[wmaker-crm.git] / src / icon.c
blob861e344c752c508209c82b181fc7b3598cc5f866
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) {
73 icon->force_paint = 1;
75 if (flags & WFontSettings) {
76 icon->force_paint = 1;
79 if (flags & WColorSettings) {
83 wIconPaint(icon);
85 /* so that the appicon expose handlers will paint the appicon specific
86 * stuff */
87 XClearArea(dpy, icon->core->window, 0, 0, icon->core->width, icon->core->height, True);
90 static void tileObserver(void *self, WMNotification * notif)
92 WIcon *icon = (WIcon *) self;
94 icon->force_paint = 1;
95 wIconPaint(icon);
97 XClearArea(dpy, icon->core->window, 0, 0, 1, 1, True);
100 /************************************/
102 INLINE static void getSize(Drawable d, unsigned int *w, unsigned int *h, unsigned int *dep)
104 Window rjunk;
105 int xjunk, yjunk;
106 unsigned int bjunk;
108 XGetGeometry(dpy, d, &rjunk, &xjunk, &yjunk, w, h, &bjunk, dep);
111 WIcon *wIconCreate(WWindow * wwin)
113 WScreen *scr = wwin->screen_ptr;
114 WIcon *icon;
115 char *file;
117 icon = wIconCreateCore(scr, wwin->icon_x, wwin->icon_y);
119 icon->owner = wwin;
120 if (wwin->wm_hints && (wwin->wm_hints->flags & IconWindowHint)) {
121 if (wwin->client_win == wwin->main_window) {
122 WApplication *wapp;
123 /* do not let miniwindow steal app-icon's icon window */
124 wapp = wApplicationOf(wwin->client_win);
125 if (!wapp || wapp->app_icon == NULL)
126 icon->icon_win = wwin->wm_hints->icon_window;
127 } else {
128 icon->icon_win = wwin->wm_hints->icon_window;
131 #ifdef NO_MINIWINDOW_TITLES
132 icon->show_title = 0;
133 #else
134 icon->show_title = 1;
135 #endif
136 icon->file_image = wDefaultGetImage(scr, wwin->wm_instance, wwin->wm_class, wPreferences.icon_size);
138 file = wDefaultGetIconFile(scr, wwin->wm_instance, wwin->wm_class, False);
139 if (file)
140 icon->file = wstrdup(file);
142 icon->icon_name = wNETWMGetIconName(wwin->client_win);
143 if (icon->icon_name)
144 wwin->flags.net_has_icon_title = 1;
145 else
146 wGetIconName(dpy, wwin->client_win, &icon->icon_name);
148 icon->tile_type = TILE_NORMAL;
150 wIconUpdate(icon);
152 XFlush(dpy);
154 WMAddNotificationObserver(appearanceObserver, icon, WNIconAppearanceSettingsChanged, icon);
155 WMAddNotificationObserver(tileObserver, icon, WNIconTileSettingsChanged, icon);
156 return icon;
159 WIcon *wIconCreateWithIconFile(WScreen * scr, char *iconfile, int tile)
161 WIcon *icon;
163 icon = wIconCreateCore(scr, 0, 0);
165 if (iconfile) {
166 icon->file_image = RLoadImage(scr->rcontext, iconfile, 0);
167 if (!icon->file_image) {
168 wwarning(_("error loading image file \"%s\": %s"), iconfile, RMessageForError(RErrorCode));
171 icon->file_image = wIconValidateIconSize(scr, icon->file_image, wPreferences.icon_size);
173 icon->file = wstrdup(iconfile);
176 icon->tile_type = tile;
178 wIconUpdate(icon);
180 WMAddNotificationObserver(appearanceObserver, icon, WNIconAppearanceSettingsChanged, icon);
181 WMAddNotificationObserver(tileObserver, icon, WNIconTileSettingsChanged, icon);
183 return icon;
186 static WIcon *wIconCreateCore(WScreen *scr, int coord_x, int coord_y)
188 WIcon *icon;
189 unsigned long vmask = 0;
190 XSetWindowAttributes attribs;
192 icon = wmalloc(sizeof(WIcon));
193 icon->core = wCoreCreateTopLevel(scr,
194 coord_x,
195 coord_y,
196 wPreferences.icon_size,
197 wPreferences.icon_size,
200 if (wPreferences.use_saveunders) {
201 vmask = CWSaveUnder;
202 attribs.save_under = True;
205 /* a white border for selecting it */
206 vmask |= CWBorderPixel;
207 attribs.border_pixel = scr->white_pixel;
209 XChangeWindowAttributes(dpy, icon->core->window, vmask, &attribs);
211 /* will be overriden if this is a application icon */
212 icon->core->descriptor.handle_mousedown = miniwindowMouseDown;
213 icon->core->descriptor.handle_expose = miniwindowExpose;
214 icon->core->descriptor.parent_type = WCLASS_MINIWINDOW;
215 icon->core->descriptor.parent = icon;
217 icon->core->stacking = wmalloc(sizeof(WStacking));
218 icon->core->stacking->above = NULL;
219 icon->core->stacking->under = NULL;
220 icon->core->stacking->window_level = NORMAL_ICON_LEVEL;
221 icon->core->stacking->child_of = NULL;
223 return icon;
226 void wIconDestroy(WIcon * icon)
228 WCoreWindow *core = icon->core;
229 WScreen *scr = core->screen_ptr;
231 WMRemoveNotificationObserver(icon);
233 if (icon->handlerID)
234 WMDeleteTimerHandler(icon->handlerID);
236 if (icon->icon_win) {
237 int x = 0, y = 0;
239 if (icon->owner) {
240 x = icon->owner->icon_x;
241 y = icon->owner->icon_y;
243 XUnmapWindow(dpy, icon->icon_win);
244 XReparentWindow(dpy, icon->icon_win, scr->root_win, x, y);
246 if (icon->icon_name)
247 XFree(icon->icon_name);
249 if (icon->pixmap)
250 XFreePixmap(dpy, icon->pixmap);
252 if (icon->file)
253 wfree(icon->file);
255 if (icon->file_image != NULL)
256 RReleaseImage(icon->file_image);
258 wCoreDestroy(icon->core);
259 wfree(icon);
262 static void drawIconTitle(WScreen * scr, Pixmap pixmap, int height)
264 XFillRectangle(dpy, pixmap, scr->icon_title_texture->normal_gc, 0, 0, wPreferences.icon_size, height + 1);
265 XDrawLine(dpy, pixmap, scr->icon_title_texture->light_gc, 0, 0, wPreferences.icon_size, 0);
266 XDrawLine(dpy, pixmap, scr->icon_title_texture->light_gc, 0, 0, 0, height + 1);
267 XDrawLine(dpy, pixmap, scr->icon_title_texture->dim_gc,
268 wPreferences.icon_size - 1, 0, wPreferences.icon_size - 1, height + 1);
271 static Pixmap makeIcon(WScreen *scr, RImage *icon, int titled, int shadowed, int tileType, int highlighted)
273 RImage *tile;
274 Pixmap pixmap;
275 int x, y, sx, sy;
276 unsigned w, h;
277 int theight = 0;
279 if (tileType == TILE_NORMAL) {
280 tile = RCloneImage(scr->icon_tile);
281 } else {
282 assert(scr->clip_tile);
283 tile = RCloneImage(scr->clip_tile);
286 if (icon) {
287 w = (icon->width > wPreferences.icon_size)
288 ? wPreferences.icon_size : icon->width;
289 x = (wPreferences.icon_size - w) / 2;
290 sx = (icon->width - w) / 2;
292 if (titled)
293 theight = WMFontHeight(scr->icon_title_font);
295 h = (icon->height + theight > wPreferences.icon_size
296 ? wPreferences.icon_size - theight : icon->height);
297 y = theight + (wPreferences.icon_size - theight - h) / 2;
298 sy = (icon->height - h) / 2;
300 RCombineArea(tile, icon, sx, sy, w, h, x, y);
303 if (shadowed) {
304 RColor color;
306 color.red = scr->icon_back_texture->light.red >> 8;
307 color.green = scr->icon_back_texture->light.green >> 8;
308 color.blue = scr->icon_back_texture->light.blue >> 8;
309 color.alpha = 150; /* about 60% */
310 RClearImage(tile, &color);
313 if (highlighted) {
314 RColor color;
316 color.red = color.green = color.blue = 0;
317 color.alpha = 160;
318 RLightImage(tile, &color);
321 if (!RConvertImage(scr->rcontext, tile, &pixmap))
322 wwarning(_("error rendering image:%s"), RMessageForError(RErrorCode));
324 RReleaseImage(tile);
326 if (titled)
327 drawIconTitle(scr, pixmap, theight);
329 return pixmap;
332 void wIconChangeTitle(WIcon * icon, char *new_title)
334 int changed;
336 changed = (new_title == NULL && icon->icon_name != NULL)
337 || (new_title != NULL && icon->icon_name == NULL);
339 if (icon->icon_name != NULL)
340 XFree(icon->icon_name);
342 icon->icon_name = new_title;
344 if (changed)
345 icon->force_paint = 1;
346 wIconPaint(icon);
349 RImage *wIconValidateIconSize(WScreen * scr, RImage * icon, int max_size)
351 RImage *tmp;
352 int w, h;
354 if (!icon)
355 return NULL;
356 #ifndef DONT_SCALE_ICONS
357 if (max_size != 64) {
358 w = max_size * icon->width / 64;
359 h = max_size * icon->height / 64;
361 tmp = RScaleImage(icon, w, h);
362 RReleaseImage(icon);
363 icon = tmp;
365 #endif
367 return icon;
370 Bool wIconChangeImageFile(WIcon * icon, char *file)
372 WScreen *scr = icon->core->screen_ptr;
373 RImage *image;
374 char *path;
375 int error = 0;
377 if (icon->file_image)
378 RReleaseImage(icon->file_image);
380 if (!file) {
381 icon->file_image = NULL;
382 wIconUpdate(icon);
383 return True;
386 path = FindImage(wPreferences.icon_path, file);
388 if (path && (image = RLoadImage(scr->rcontext, path, 0))) {
389 icon->file_image = wIconValidateIconSize(icon->core->screen_ptr, image, wPreferences.icon_size);
390 wIconUpdate(icon);
391 } else {
392 error = 1;
395 if (path)
396 wfree(path);
398 return !error;
401 static char *get_name_for_icon(WWindow * wwin)
403 char *prefix, *suffix;
404 char *path;
405 int len;
407 if (wwin->wm_class && wwin->wm_instance) {
408 int len = strlen(wwin->wm_class) + strlen(wwin->wm_instance) + 2;
409 suffix = wmalloc(len);
410 snprintf(suffix, len, "%s.%s", wwin->wm_instance, wwin->wm_class);
411 } else if (wwin->wm_class) {
412 int len = strlen(wwin->wm_class) + 1;
413 suffix = wmalloc(len);
414 snprintf(suffix, len, "%s", wwin->wm_class);
415 } else if (wwin->wm_instance) {
416 int len = strlen(wwin->wm_instance) + 1;
417 suffix = wmalloc(len);
418 snprintf(suffix, len, "%s", wwin->wm_instance);
419 } else {
420 return NULL;
423 prefix = wusergnusteppath();
424 len = strlen(prefix) + 64 + strlen(suffix);
425 path = wmalloc(len + 1);
426 snprintf(path, len, "%s/Library/WindowMaker", prefix);
428 if (access(path, F_OK) != 0) {
429 if (mkdir(path, S_IRUSR | S_IWUSR | S_IXUSR)) {
430 werror(_("could not create directory %s"), path);
431 wfree(path);
432 wfree(suffix);
433 return NULL;
436 strcat(path, "/CachedPixmaps");
437 if (access(path, F_OK) != 0) {
438 if (mkdir(path, S_IRUSR | S_IWUSR | S_IXUSR) != 0) {
439 werror(_("could not create directory %s"), path);
440 wfree(path);
441 wfree(suffix);
442 return NULL;
446 strcat(path, "/");
447 strcat(path, suffix);
448 strcat(path, ".xpm");
449 wfree(suffix);
451 return path;
454 static char *get_icon_cache_path(void)
456 char *prefix, *path;
457 int len, ret;
459 prefix = wusergnusteppath();
460 len = strlen(prefix) + strlen(CACHE_ICON_PATH) + 2;
461 path = wmalloc(len);
462 snprintf(path, len, "%s%s/", prefix, CACHE_ICON_PATH);
464 /* If the folder exists, exit */
465 if (access(path, F_OK) == 0)
466 return path;
468 /* Create the folder */
469 ret = wmkdirhier((const char *) path);
471 /* Exit 1 on success, 0 on failure */
472 if (ret == 1)
473 return path;
475 /* Fail */
476 wfree(path);
477 return NULL;
480 static RImage *get_wwindow_image_from_wmhints(WWindow *wwin, WIcon *icon)
482 RImage *image = NULL;
483 XWMHints *hints = wwin->wm_hints;
485 if (hints && (hints->flags & IconPixmapHint) && hints->icon_pixmap != None)
486 image = RCreateImageFromDrawable(icon->core->screen_ptr->rcontext,
487 hints->icon_pixmap,
488 (hints->flags & IconMaskHint)
489 ? hints->icon_mask : None);
491 return image;
495 * wIconStore--
496 * Stores the client supplied icon at ~/GNUstep/Library/WindowMaker/CachedPixmaps
497 * and returns the path for that icon. Returns NULL if there is no
498 * client supplied icon or on failure.
500 * Side effects:
501 * New directories might be created.
503 char *wIconStore(WIcon * icon)
505 char *path;
506 RImage *image = NULL;
507 WWindow *wwin = icon->owner;
509 if (!wwin)
510 return NULL;
512 path = get_name_for_icon(wwin);
513 if (!path)
514 return NULL;
516 /* If icon exists, exit */
517 if (access(path, F_OK) == 0)
518 return path;
520 if (wwin->net_icon_image)
521 image = RRetainImage(wwin->net_icon_image);
522 else
523 image = get_wwindow_image_from_wmhints(wwin, icon);
525 if (!image) {
526 wfree(path);
527 return NULL;
530 if (!RSaveImage(image, path, "XPM")) {
531 wfree(path);
532 path = NULL;
535 RReleaseImage(image);
537 return path;
540 static void cycleColor(void *data)
542 WIcon *icon = (WIcon *) data;
543 WScreen *scr = icon->core->screen_ptr;
544 XGCValues gcv;
546 icon->step--;
547 gcv.dash_offset = icon->step;
548 XChangeGC(dpy, scr->icon_select_gc, GCDashOffset, &gcv);
550 XDrawRectangle(dpy, icon->core->window, scr->icon_select_gc, 0, 0,
551 icon->core->width - 1, icon->core->height - 1);
552 icon->handlerID = WMAddTimerHandler(COLOR_CYCLE_DELAY, cycleColor, icon);
555 #ifdef NEWAPPICON
556 void wIconSetHighlited(WIcon *icon, Bool flag)
558 if (icon->highlighted == flag)
559 return;
561 icon->highlighted = flag;
562 icon->force_paint = True;
563 wIconPaint(icon);
565 #endif
567 void wIconSelect(WIcon * icon)
569 WScreen *scr = icon->core->screen_ptr;
570 icon->selected = !icon->selected;
572 if (icon->selected) {
573 icon->step = 0;
574 if (!wPreferences.dont_blink)
575 icon->handlerID = WMAddTimerHandler(10, cycleColor, icon);
576 else
577 XDrawRectangle(dpy, icon->core->window, scr->icon_select_gc, 0, 0,
578 icon->core->width - 1, icon->core->height - 1);
579 } else {
580 if (icon->handlerID) {
581 WMDeleteTimerHandler(icon->handlerID);
582 icon->handlerID = NULL;
584 XClearArea(dpy, icon->core->window, 0, 0, icon->core->width, icon->core->height, True);
588 void wIconUpdate(WIcon * icon)
590 WScreen *scr = icon->core->screen_ptr;
591 WWindow *wwin = icon->owner;
593 assert(scr->icon_tile != NULL);
595 if (icon->pixmap != None)
596 XFreePixmap(dpy, icon->pixmap);
598 icon->pixmap = None;
600 if (wwin && WFLAGP(wwin, always_user_icon)) {
601 /* Forced use user_icon */
602 get_pixmap_icon_from_user_icon(scr, icon);
603 } else if (icon->icon_win != None) {
604 /* Get the Pixmap from the WIcon */
605 get_pixmap_icon_from_icon_win(icon);
606 } else if (wwin && wwin->net_icon_image) {
607 /* Use _NET_WM_ICON icon */
608 icon->pixmap = makeIcon(scr, wwin->net_icon_image, icon->show_title,
609 icon->shadowed, icon->tile_type, icon->highlighted);
610 } else if (wwin && wwin->wm_hints && (wwin->wm_hints->flags & IconPixmapHint)) {
611 /* Get the Pixmap from the wm_hints, else, from the user */
612 if (get_pixmap_icon_from_wm_hints(scr, wwin, icon))
613 get_pixmap_icon_from_user_icon(scr, icon);
614 } else {
615 /* Get the Pixmap from the user */
616 get_pixmap_icon_from_user_icon(scr, icon);
619 /* No pixmap, set default background */
620 if (icon->pixmap != None)
621 XSetWindowBackgroundPixmap(dpy, icon->core->window, icon->pixmap);
623 /* Paint it */
624 XClearWindow(dpy, icon->core->window);
625 wIconPaint(icon);
628 void get_pixmap_icon_from_user_icon(WScreen *scr, WIcon * icon)
630 RImage *image = NULL;
631 char *path;
632 char *file;
634 if (icon->file_image) {
635 icon->pixmap = makeIcon(scr, icon->file_image, icon->show_title,
636 icon->shadowed, icon->tile_type, icon->highlighted);
637 } else {
638 /* make default icons */
639 if (!scr->def_icon_pixmap) {
640 file = wDefaultGetIconFile(scr, NULL, NULL, False);
641 if (file) {
642 path = FindImage(wPreferences.icon_path, file);
643 if (path) {
644 image = RLoadImage(scr->rcontext, path, 0);
645 if (!image)
646 wwarning(_("could not load default icon \"%s\":%s"),
647 file, RMessageForError(RErrorCode));
648 wfree(path);
649 } else {
650 wwarning(_("could not find default icon \"%s\""), file);
652 /* FIXME: Probably wfree(file) here! */
655 image = wIconValidateIconSize(scr, image, wPreferences.icon_size);
656 scr->def_icon_pixmap = makeIcon(scr, image, False, False, icon->tile_type, icon->highlighted);
657 scr->def_ticon_pixmap = makeIcon(scr, image, True, False, icon->tile_type, icon->highlighted);
658 if (image)
659 RReleaseImage(image);
662 if (icon->show_title)
663 XSetWindowBackgroundPixmap(dpy, icon->core->window, scr->def_ticon_pixmap);
664 else
665 XSetWindowBackgroundPixmap(dpy, icon->core->window, scr->def_icon_pixmap);
667 icon->pixmap = None;
671 /* Get the Pixmap from the WIcon of the WWindow */
672 void get_pixmap_icon_from_icon_win(WIcon * icon)
674 XWindowAttributes attr;
675 WScreen *scr = icon->core->screen_ptr;
676 int title_height = WMFontHeight(scr->icon_title_font);
677 unsigned int width, height, depth;
678 int theight;
679 int resize = 0;
680 Pixmap pixmap;
682 getSize(icon->icon_win, &width, &height, &depth);
684 if (width > wPreferences.icon_size) {
685 resize = 1;
686 width = wPreferences.icon_size;
689 if (height > wPreferences.icon_size) {
690 resize = 1;
691 height = wPreferences.icon_size;
694 if (icon->show_title && (height + title_height < wPreferences.icon_size)) {
695 pixmap = XCreatePixmap(dpy, scr->w_win, wPreferences.icon_size,
696 wPreferences.icon_size, scr->w_depth);
697 XSetClipMask(dpy, scr->copy_gc, None);
698 XCopyArea(dpy, scr->icon_tile_pixmap, pixmap, scr->copy_gc, 0, 0,
699 wPreferences.icon_size, wPreferences.icon_size, 0, 0);
700 drawIconTitle(scr, pixmap, title_height);
701 theight = title_height;
702 } else {
703 pixmap = None;
704 theight = 0;
705 XSetWindowBackgroundPixmap(dpy, icon->core->window, scr->icon_tile_pixmap);
708 XSetWindowBorderWidth(dpy, icon->icon_win, 0);
709 XReparentWindow(dpy, icon->icon_win, icon->core->window,
710 (wPreferences.icon_size - width) / 2,
711 theight + (wPreferences.icon_size - height - theight) / 2);
712 if (resize)
713 XResizeWindow(dpy, icon->icon_win, width, height);
715 XMapWindow(dpy, icon->icon_win);
716 XAddToSaveSet(dpy, icon->icon_win);
718 /* Save it */
719 icon->pixmap = pixmap;
721 if ((XGetWindowAttributes(dpy, icon->icon_win, &attr)) &&
722 (attr.all_event_masks & ButtonPressMask))
723 wHackedGrabButton(Button1, MOD_MASK, icon->core->window, True,
724 ButtonPressMask, GrabModeSync, GrabModeAsync,
725 None, wCursor[WCUR_ARROW]);
728 /* Get the Pixmap from the XWindow wm_hints */
729 int get_pixmap_icon_from_wm_hints(WScreen *scr, WWindow *wwin, WIcon *icon)
731 Window jw;
732 Pixmap pixmap;
733 unsigned int w, h, ju, d;
734 int ji, x, y;
735 int title_height = WMFontHeight(scr->icon_title_font);
737 if (!XGetGeometry(dpy, wwin->wm_hints->icon_pixmap, &jw, &ji, &ji, &w, &h, &ju, &d)) {
738 icon->owner->wm_hints->flags &= ~IconPixmapHint;
739 return(1);
742 pixmap = XCreatePixmap(dpy, icon->core->window, wPreferences.icon_size,
743 wPreferences.icon_size, scr->w_depth);
744 XSetClipMask(dpy, scr->copy_gc, None);
745 XCopyArea(dpy, scr->icon_tile_pixmap, pixmap, scr->copy_gc, 0, 0,
746 wPreferences.icon_size, wPreferences.icon_size, 0, 0);
748 if (w > wPreferences.icon_size)
749 w = wPreferences.icon_size;
750 x = (wPreferences.icon_size - w) / 2;
752 if (icon->show_title && (title_height < wPreferences.icon_size)) {
753 drawIconTitle(scr, pixmap, title_height);
755 if (h > wPreferences.icon_size - title_height - 2) {
756 h = wPreferences.icon_size - title_height - 2;
757 y = title_height + 1;
758 } else {
759 y = (wPreferences.icon_size - h - title_height) / 2 + title_height + 1;
761 } else {
762 if (w > wPreferences.icon_size)
763 w = wPreferences.icon_size;
764 y = (wPreferences.icon_size - h) / 2;
767 if (wwin->wm_hints->flags & IconMaskHint)
768 XSetClipMask(dpy, scr->copy_gc, wwin->wm_hints->icon_mask);
770 XSetClipOrigin(dpy, scr->copy_gc, x, y);
772 if (d != scr->w_depth) {
773 XSetForeground(dpy, scr->copy_gc, scr->black_pixel);
774 XSetBackground(dpy, scr->copy_gc, scr->white_pixel);
775 XCopyPlane(dpy, wwin->wm_hints->icon_pixmap, pixmap, scr->copy_gc, 0, 0, w, h, x, y, 1);
776 } else {
777 XCopyArea(dpy, wwin->wm_hints->icon_pixmap, pixmap, scr->copy_gc, 0, 0, w, h, x, y);
780 XSetClipOrigin(dpy, scr->copy_gc, 0, 0);
782 icon->pixmap = pixmap;
783 return (0);
786 void wIconPaint(WIcon * icon)
788 WScreen *scr = icon->core->screen_ptr;
789 int x;
790 char *tmp;
792 if (icon->force_paint) {
793 icon->force_paint = 0;
794 wIconUpdate(icon);
795 return;
798 XClearWindow(dpy, icon->core->window);
800 /* draw the icon title */
801 if (icon->show_title && icon->icon_name != NULL) {
802 int l;
803 int w;
805 tmp = ShrinkString(scr->icon_title_font, icon->icon_name, wPreferences.icon_size - 4);
806 w = WMWidthOfString(scr->icon_title_font, tmp, l = strlen(tmp));
808 if (w > icon->core->width - 4)
809 x = (icon->core->width - 4) - w;
810 else
811 x = (icon->core->width - w) / 2;
813 WMDrawString(scr->wmscreen, icon->core->window, scr->icon_title_color,
814 scr->icon_title_font, x, 1, tmp, l);
815 wfree(tmp);
818 if (icon->selected)
819 XDrawRectangle(dpy, icon->core->window, scr->icon_select_gc, 0, 0,
820 icon->core->width - 1, icon->core->height - 1);
823 /******************************************************************/
825 static void miniwindowExpose(WObjDescriptor * desc, XEvent * event)
827 wIconPaint(desc->parent);
830 static void miniwindowDblClick(WObjDescriptor * desc, XEvent * event)
832 WIcon *icon = desc->parent;
834 assert(icon->owner != NULL);
836 wDeiconifyWindow(icon->owner);
839 static void miniwindowMouseDown(WObjDescriptor * desc, XEvent * event)
841 WIcon *icon = desc->parent;
842 WWindow *wwin = icon->owner;
843 XEvent ev;
844 int x = wwin->icon_x, y = wwin->icon_y;
845 int dx = event->xbutton.x, dy = event->xbutton.y;
846 int grabbed = 0;
847 int clickButton = event->xbutton.button;
848 Bool hasMoved = False;
850 if (WCHECK_STATE(WSTATE_MODAL))
851 return;
853 if (IsDoubleClick(icon->core->screen_ptr, event)) {
854 miniwindowDblClick(desc, event);
855 return;
858 if (event->xbutton.button == Button1) {
859 if (event->xbutton.state & MOD_MASK)
860 wLowerFrame(icon->core);
861 else
862 wRaiseFrame(icon->core);
863 if (event->xbutton.state & ShiftMask) {
864 wIconSelect(icon);
865 wSelectWindow(icon->owner, !wwin->flags.selected);
867 } else if (event->xbutton.button == Button3) {
868 WObjDescriptor *desc;
870 OpenMiniwindowMenu(wwin, event->xbutton.x_root, event->xbutton.y_root);
872 /* allow drag select of menu */
873 desc = &wwin->screen_ptr->window_menu->menu->descriptor;
874 event->xbutton.send_event = True;
875 (*desc->handle_mousedown) (desc, event);
877 return;
880 if (XGrabPointer(dpy, icon->core->window, False, ButtonMotionMask
881 | ButtonReleaseMask | ButtonPressMask, GrabModeAsync,
882 GrabModeAsync, None, None, CurrentTime) != GrabSuccess) {
884 while (1) {
885 WMMaskEvent(dpy, PointerMotionMask | ButtonReleaseMask | ButtonPressMask
886 | ButtonMotionMask | ExposureMask, &ev);
887 switch (ev.type) {
888 case Expose:
889 WMHandleEvent(&ev);
890 break;
892 case MotionNotify:
893 hasMoved = True;
894 if (!grabbed) {
895 if (abs(dx - ev.xmotion.x) >= MOVE_THRESHOLD
896 || abs(dy - ev.xmotion.y) >= MOVE_THRESHOLD) {
897 XChangeActivePointerGrab(dpy, ButtonMotionMask
898 | ButtonReleaseMask | ButtonPressMask,
899 wCursor[WCUR_MOVE], CurrentTime);
900 grabbed = 1;
901 } else {
902 break;
905 x = ev.xmotion.x_root - dx;
906 y = ev.xmotion.y_root - dy;
907 XMoveWindow(dpy, icon->core->window, x, y);
908 break;
910 case ButtonPress:
911 break;
913 case ButtonRelease:
914 if (ev.xbutton.button != clickButton)
915 break;
917 if (wwin->icon_x != x || wwin->icon_y != y)
918 wwin->flags.icon_moved = 1;
920 XMoveWindow(dpy, icon->core->window, x, y);
922 wwin->icon_x = x;
923 wwin->icon_y = y;
924 XUngrabPointer(dpy, CurrentTime);
926 if (wPreferences.auto_arrange_icons)
927 wArrangeIcons(wwin->screen_ptr, True);
928 if (wPreferences.single_click && !hasMoved)
929 miniwindowDblClick(desc, event);
930 return;