Icon: Make icon_create_for_* be more similar
[wmaker-crm.git] / src / icon.c
blobe950a7d658b64c5e2ee63ae001237984a9fd2f09
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 *icon_create_core(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 *icon_create_for_wwindow(WWindow *wwin)
106 WScreen *scr = wwin->screen_ptr;
107 WIcon *icon;
108 char *file;
110 icon = icon_create_core(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
130 icon->icon_name = wNETWMGetIconName(wwin->client_win);
131 if (icon->icon_name)
132 wwin->flags.net_has_icon_title = 1;
133 else
134 wGetIconName(dpy, wwin->client_win, &icon->icon_name);
136 /* Get the application icon, default included */
137 file = get_default_icon_filename(scr, wwin->wm_instance, wwin->wm_class, NULL, True);
138 if (file) {
139 icon->file = wstrdup(file);
140 icon->file_image = get_default_icon_rimage(scr, icon->file, wPreferences.icon_size);
141 wfree(file);
144 icon->tile_type = TILE_NORMAL;
146 wIconUpdate(icon);
148 WMAddNotificationObserver(appearanceObserver, icon, WNIconAppearanceSettingsChanged, icon);
149 WMAddNotificationObserver(tileObserver, icon, WNIconTileSettingsChanged, icon);
151 return icon;
154 WIcon *icon_create_for_dock(WScreen *scr, char *command, char *wm_instance, char *wm_class, int tile)
156 WIcon *icon;
157 char *file = NULL;
159 icon = icon_create_core(scr, 0, 0);
161 /* Search the icon using instance and class, without default icon */
162 file = get_default_icon_filename(scr, wm_instance, wm_class, command, False);
163 if (file) {
164 icon->file = wstrdup(file);
165 icon->file_image = get_default_icon_rimage(scr, icon->file, wPreferences.icon_size);
166 wfree(file);
169 icon->tile_type = tile;
171 wIconUpdate(icon);
173 WMAddNotificationObserver(appearanceObserver, icon, WNIconAppearanceSettingsChanged, icon);
174 WMAddNotificationObserver(tileObserver, icon, WNIconTileSettingsChanged, icon);
176 return icon;
179 static WIcon *icon_create_core(WScreen *scr, int coord_x, int coord_y)
181 WIcon *icon;
182 unsigned long vmask = 0;
183 XSetWindowAttributes attribs;
185 icon = wmalloc(sizeof(WIcon));
186 icon->core = wCoreCreateTopLevel(scr,
187 coord_x,
188 coord_y,
189 wPreferences.icon_size,
190 wPreferences.icon_size,
191 0, scr->w_depth, scr->w_visual, scr->w_colormap);
193 if (wPreferences.use_saveunders) {
194 vmask = CWSaveUnder;
195 attribs.save_under = True;
198 /* a white border for selecting it */
199 vmask |= CWBorderPixel;
200 attribs.border_pixel = scr->white_pixel;
202 XChangeWindowAttributes(dpy, icon->core->window, vmask, &attribs);
204 /* will be overriden if this is a application icon */
205 icon->core->descriptor.handle_mousedown = miniwindowMouseDown;
206 icon->core->descriptor.handle_expose = miniwindowExpose;
207 icon->core->descriptor.parent_type = WCLASS_MINIWINDOW;
208 icon->core->descriptor.parent = icon;
210 icon->core->stacking = wmalloc(sizeof(WStacking));
211 icon->core->stacking->above = NULL;
212 icon->core->stacking->under = NULL;
213 icon->core->stacking->window_level = NORMAL_ICON_LEVEL;
214 icon->core->stacking->child_of = NULL;
216 /* Icon image */
217 icon->file = NULL;
218 icon->file_image = NULL;
220 return icon;
223 void wIconDestroy(WIcon * icon)
225 WCoreWindow *core = icon->core;
226 WScreen *scr = core->screen_ptr;
228 WMRemoveNotificationObserver(icon);
230 if (icon->handlerID)
231 WMDeleteTimerHandler(icon->handlerID);
233 if (icon->icon_win) {
234 int x = 0, y = 0;
236 if (icon->owner) {
237 x = icon->owner->icon_x;
238 y = icon->owner->icon_y;
240 XUnmapWindow(dpy, icon->icon_win);
241 XReparentWindow(dpy, icon->icon_win, scr->root_win, x, y);
243 if (icon->icon_name)
244 XFree(icon->icon_name);
246 if (icon->pixmap)
247 XFreePixmap(dpy, icon->pixmap);
249 if (icon->file)
250 wfree(icon->file);
252 if (icon->file_image != NULL)
253 RReleaseImage(icon->file_image);
255 wCoreDestroy(icon->core);
256 wfree(icon);
259 static void drawIconTitle(WScreen * scr, Pixmap pixmap, int height)
261 XFillRectangle(dpy, pixmap, scr->icon_title_texture->normal_gc, 0, 0, wPreferences.icon_size, height + 1);
262 XDrawLine(dpy, pixmap, scr->icon_title_texture->light_gc, 0, 0, wPreferences.icon_size, 0);
263 XDrawLine(dpy, pixmap, scr->icon_title_texture->light_gc, 0, 0, 0, height + 1);
264 XDrawLine(dpy, pixmap, scr->icon_title_texture->dim_gc,
265 wPreferences.icon_size - 1, 0, wPreferences.icon_size - 1, height + 1);
268 static Pixmap makeIcon(WScreen *scr, RImage *icon, int titled, int shadowed, int tileType, int highlighted)
270 RImage *tile;
271 Pixmap pixmap;
272 int x, y, sx, sy;
273 unsigned w, h;
274 int theight = 0;
276 if (tileType == TILE_NORMAL) {
277 tile = RCloneImage(scr->icon_tile);
278 } else {
279 assert(scr->clip_tile);
280 tile = RCloneImage(scr->clip_tile);
283 if (icon) {
284 w = (icon->width > wPreferences.icon_size)
285 ? wPreferences.icon_size : icon->width;
286 x = (wPreferences.icon_size - w) / 2;
287 sx = (icon->width - w) / 2;
289 if (titled)
290 theight = WMFontHeight(scr->icon_title_font);
292 h = (icon->height + theight > wPreferences.icon_size
293 ? wPreferences.icon_size - theight : icon->height);
294 y = theight + (wPreferences.icon_size - theight - h) / 2;
295 sy = (icon->height - h) / 2;
297 RCombineArea(tile, icon, sx, sy, w, h, x, y);
300 if (shadowed) {
301 RColor color;
303 color.red = scr->icon_back_texture->light.red >> 8;
304 color.green = scr->icon_back_texture->light.green >> 8;
305 color.blue = scr->icon_back_texture->light.blue >> 8;
306 color.alpha = 150; /* about 60% */
307 RClearImage(tile, &color);
310 if (highlighted) {
311 RColor color;
313 color.red = color.green = color.blue = 0;
314 color.alpha = 160;
315 RLightImage(tile, &color);
318 if (!RConvertImage(scr->rcontext, tile, &pixmap))
319 wwarning(_("error rendering image:%s"), RMessageForError(RErrorCode));
321 RReleaseImage(tile);
323 if (titled)
324 drawIconTitle(scr, pixmap, theight);
326 return pixmap;
329 void wIconChangeTitle(WIcon * icon, char *new_title)
331 int changed;
333 changed = (new_title == NULL && icon->icon_name != NULL)
334 || (new_title != NULL && icon->icon_name == NULL);
336 if (icon->icon_name != NULL)
337 XFree(icon->icon_name);
339 icon->icon_name = new_title;
341 if (changed)
342 icon->force_paint = 1;
343 wIconPaint(icon);
346 RImage *wIconValidateIconSize(RImage *icon, int max_size)
348 RImage *nimage;
350 if (!icon)
351 return NULL;
353 /* We should hold "ICON_BORDER" (~2) pixels to include the icon border */
354 if ((icon->width - max_size) > -ICON_BORDER ||
355 (icon->height - max_size) > -ICON_BORDER) {
356 nimage = RScaleImage(icon, max_size - ICON_BORDER,
357 (icon->height * (max_size - ICON_BORDER) / icon->width));
358 RReleaseImage(icon);
359 icon = nimage;
362 return icon;
365 Bool wIconChangeImageFile(WIcon * icon, char *file)
367 WScreen *scr = icon->core->screen_ptr;
368 RImage *image;
369 char *path;
370 int error = 0;
372 if (icon->file_image)
373 RReleaseImage(icon->file_image);
375 if (!file) {
376 icon->file_image = NULL;
377 wIconUpdate(icon);
378 return True;
381 path = FindImage(wPreferences.icon_path, file);
383 if (path && (image = RLoadImage(scr->rcontext, path, 0))) {
384 icon->file_image = wIconValidateIconSize(image, wPreferences.icon_size);
385 wIconUpdate(icon);
386 } else {
387 error = 1;
390 if (path)
391 wfree(path);
393 return !error;
396 static char *get_name_for_wwin(WWindow *wwin)
398 return get_name_for_instance_class(wwin->wm_instance, wwin->wm_class);
401 char *get_name_for_instance_class(char *wm_instance, char *wm_class)
403 char *suffix;
404 int len;
406 if (wm_class && wm_instance) {
407 len = strlen(wm_class) + strlen(wm_instance) + 2;
408 suffix = wmalloc(len);
409 snprintf(suffix, len, "%s.%s", wm_instance, wm_class);
410 } else if (wm_class) {
411 len = strlen(wm_class) + 1;
412 suffix = wmalloc(len);
413 snprintf(suffix, len, "%s", wm_class);
414 } else if (wm_instance) {
415 len = strlen(wm_instance) + 1;
416 suffix = wmalloc(len);
417 snprintf(suffix, len, "%s", wm_instance);
418 } else {
419 return NULL;
422 return suffix;
425 static char *get_icon_cache_path(void)
427 char *prefix, *path;
428 int len, ret;
430 prefix = wusergnusteppath();
431 len = strlen(prefix) + strlen(CACHE_ICON_PATH) + 2;
432 path = wmalloc(len);
433 snprintf(path, len, "%s%s/", prefix, CACHE_ICON_PATH);
435 /* If the folder exists, exit */
436 if (access(path, F_OK) == 0)
437 return path;
439 /* Create the folder */
440 ret = wmkdirhier((const char *) path);
442 /* Exit 1 on success, 0 on failure */
443 if (ret == 1)
444 return path;
446 /* Fail */
447 wfree(path);
448 return NULL;
451 static RImage *get_wwindow_image_from_wmhints(WWindow *wwin, WIcon *icon)
453 RImage *image = NULL;
454 XWMHints *hints = wwin->wm_hints;
456 if (hints && (hints->flags & IconPixmapHint) && hints->icon_pixmap != None)
457 image = RCreateImageFromDrawable(icon->core->screen_ptr->rcontext,
458 hints->icon_pixmap,
459 (hints->flags & IconMaskHint)
460 ? hints->icon_mask : None);
462 return image;
466 * wIconStore--
467 * Stores the client supplied icon at CACHE_ICON_PATH
468 * and returns the path for that icon. Returns NULL if there is no
469 * client supplied icon or on failure.
471 * Side effects:
472 * New directories might be created.
474 char *wIconStore(WIcon * icon)
476 char *path, *dir_path, *file;
477 int len = 0;
478 RImage *image = NULL;
479 WWindow *wwin = icon->owner;
481 if (!wwin)
482 return NULL;
484 dir_path = get_icon_cache_path();
485 if (!dir_path)
486 return NULL;
488 file = get_name_for_wwin(wwin);
489 if (!file) {
490 wfree(dir_path);
491 return NULL;
494 len = strlen(dir_path) + strlen(file) + 5;
495 path = wmalloc(len);
496 snprintf(path, len, "%s%s.xpm", dir_path, file);
497 wfree(dir_path);
498 wfree(file);
500 /* If icon exists, exit */
501 if (access(path, F_OK) == 0)
502 return path;
504 if (wwin->net_icon_image)
505 image = RRetainImage(wwin->net_icon_image);
506 else
507 image = get_wwindow_image_from_wmhints(wwin, icon);
509 if (!image) {
510 wfree(path);
511 return NULL;
514 if (!RSaveImage(image, path, "XPM")) {
515 wfree(path);
516 path = NULL;
519 RReleaseImage(image);
521 return path;
524 static void cycleColor(void *data)
526 WIcon *icon = (WIcon *) data;
527 WScreen *scr = icon->core->screen_ptr;
528 XGCValues gcv;
530 icon->step--;
531 gcv.dash_offset = icon->step;
532 XChangeGC(dpy, scr->icon_select_gc, GCDashOffset, &gcv);
534 XDrawRectangle(dpy, icon->core->window, scr->icon_select_gc, 0, 0,
535 icon->core->width - 1, icon->core->height - 1);
536 icon->handlerID = WMAddTimerHandler(COLOR_CYCLE_DELAY, cycleColor, icon);
539 #ifdef NEWAPPICON
540 void wIconSetHighlited(WIcon *icon, Bool flag)
542 if (icon->highlighted == flag)
543 return;
545 icon->highlighted = flag;
546 icon->force_paint = True;
547 wIconPaint(icon);
549 #endif
551 void wIconSelect(WIcon * icon)
553 WScreen *scr = icon->core->screen_ptr;
554 icon->selected = !icon->selected;
556 if (icon->selected) {
557 icon->step = 0;
558 if (!wPreferences.dont_blink)
559 icon->handlerID = WMAddTimerHandler(10, cycleColor, icon);
560 else
561 XDrawRectangle(dpy, icon->core->window, scr->icon_select_gc, 0, 0,
562 icon->core->width - 1, icon->core->height - 1);
563 } else {
564 if (icon->handlerID) {
565 WMDeleteTimerHandler(icon->handlerID);
566 icon->handlerID = NULL;
568 XClearArea(dpy, icon->core->window, 0, 0, icon->core->width, icon->core->height, True);
572 void wIconUpdate(WIcon * icon)
574 WScreen *scr = icon->core->screen_ptr;
575 WWindow *wwin = icon->owner;
577 assert(scr->icon_tile != NULL);
579 if (icon->pixmap != None)
580 XFreePixmap(dpy, icon->pixmap);
582 icon->pixmap = None;
584 if (wwin && WFLAGP(wwin, always_user_icon)) {
585 /* Forced use user_icon */
586 get_pixmap_icon_from_user_icon(scr, icon);
587 } else if (icon->icon_win != None) {
588 /* Get the Pixmap from the WIcon */
589 get_pixmap_icon_from_icon_win(icon);
590 } else if (wwin && wwin->net_icon_image) {
591 /* Use _NET_WM_ICON icon */
592 icon->pixmap = makeIcon(scr, wwin->net_icon_image, icon->show_title,
593 icon->shadowed, icon->tile_type, icon->highlighted);
594 } else if (wwin && wwin->wm_hints && (wwin->wm_hints->flags & IconPixmapHint)) {
595 /* Get the Pixmap from the wm_hints, else, from the user */
596 if (get_pixmap_icon_from_wm_hints(scr, wwin, icon))
597 get_pixmap_icon_from_user_icon(scr, icon);
598 } else {
599 /* Get the Pixmap from the user */
600 get_pixmap_icon_from_user_icon(scr, icon);
603 /* No pixmap, set default background */
604 if (icon->pixmap != None)
605 XSetWindowBackgroundPixmap(dpy, icon->core->window, icon->pixmap);
607 /* Paint it */
608 XClearWindow(dpy, icon->core->window);
609 wIconPaint(icon);
612 void get_pixmap_icon_from_user_icon(WScreen *scr, WIcon * icon)
614 RImage *image = NULL;
615 char *path;
616 char *file;
618 if (icon->file_image) {
619 icon->pixmap = makeIcon(scr, icon->file_image, icon->show_title,
620 icon->shadowed, icon->tile_type, icon->highlighted);
621 } else {
622 /* make default icons */
623 if (!scr->def_icon_pixmap) {
624 /* Get the default icon */
625 file = wDefaultGetIconFile(NULL, NULL, True);
626 if (file) {
627 path = FindImage(wPreferences.icon_path, file);
628 if (path) {
629 image = RLoadImage(scr->rcontext, path, 0);
630 if (!image)
631 wwarning(_("could not load default icon \"%s\":%s"),
632 file, RMessageForError(RErrorCode));
633 wfree(path);
634 } else {
635 wwarning(_("could not find default icon \"%s\""), file);
639 image = wIconValidateIconSize(image, wPreferences.icon_size);
640 scr->def_icon_pixmap = makeIcon(scr, image, False, False, icon->tile_type, icon->highlighted);
641 scr->def_ticon_pixmap = makeIcon(scr, image, True, False, icon->tile_type, icon->highlighted);
642 if (image)
643 RReleaseImage(image);
646 if (icon->show_title)
647 XSetWindowBackgroundPixmap(dpy, icon->core->window, scr->def_ticon_pixmap);
648 else
649 XSetWindowBackgroundPixmap(dpy, icon->core->window, scr->def_icon_pixmap);
651 icon->pixmap = None;
655 /* Get the Pixmap from the WIcon of the WWindow */
656 void get_pixmap_icon_from_icon_win(WIcon * icon)
658 XWindowAttributes attr;
659 WScreen *scr = icon->core->screen_ptr;
660 int title_height = WMFontHeight(scr->icon_title_font);
661 unsigned int width, height, depth;
662 int theight;
663 int resize = 0;
664 Pixmap pixmap;
666 getSize(icon->icon_win, &width, &height, &depth);
668 if (width > wPreferences.icon_size) {
669 resize = 1;
670 width = wPreferences.icon_size;
673 if (height > wPreferences.icon_size) {
674 resize = 1;
675 height = wPreferences.icon_size;
678 if (icon->show_title && (height + title_height < wPreferences.icon_size)) {
679 pixmap = XCreatePixmap(dpy, scr->w_win, wPreferences.icon_size,
680 wPreferences.icon_size, scr->w_depth);
681 XSetClipMask(dpy, scr->copy_gc, None);
682 XCopyArea(dpy, scr->icon_tile_pixmap, pixmap, scr->copy_gc, 0, 0,
683 wPreferences.icon_size, wPreferences.icon_size, 0, 0);
684 drawIconTitle(scr, pixmap, title_height);
685 theight = title_height;
686 } else {
687 pixmap = None;
688 theight = 0;
689 XSetWindowBackgroundPixmap(dpy, icon->core->window, scr->icon_tile_pixmap);
692 XSetWindowBorderWidth(dpy, icon->icon_win, 0);
693 XReparentWindow(dpy, icon->icon_win, icon->core->window,
694 (wPreferences.icon_size - width) / 2,
695 theight + (wPreferences.icon_size - height - theight) / 2);
696 if (resize)
697 XResizeWindow(dpy, icon->icon_win, width, height);
699 XMapWindow(dpy, icon->icon_win);
700 XAddToSaveSet(dpy, icon->icon_win);
702 /* Save it */
703 icon->pixmap = pixmap;
705 if ((XGetWindowAttributes(dpy, icon->icon_win, &attr)) &&
706 (attr.all_event_masks & ButtonPressMask))
707 wHackedGrabButton(Button1, MOD_MASK, icon->core->window, True,
708 ButtonPressMask, GrabModeSync, GrabModeAsync,
709 None, wCursor[WCUR_ARROW]);
712 /* Get the Pixmap from the XWindow wm_hints */
713 int get_pixmap_icon_from_wm_hints(WScreen *scr, WWindow *wwin, WIcon *icon)
715 Window jw;
716 Pixmap pixmap;
717 unsigned int w, h, ju, d;
718 int ji, x, y;
719 int title_height = WMFontHeight(scr->icon_title_font);
721 if (!XGetGeometry(dpy, wwin->wm_hints->icon_pixmap, &jw, &ji, &ji, &w, &h, &ju, &d)) {
722 icon->owner->wm_hints->flags &= ~IconPixmapHint;
723 return 1;
726 pixmap = XCreatePixmap(dpy, icon->core->window, wPreferences.icon_size,
727 wPreferences.icon_size, scr->w_depth);
728 XSetClipMask(dpy, scr->copy_gc, None);
729 XCopyArea(dpy, scr->icon_tile_pixmap, pixmap, scr->copy_gc, 0, 0,
730 wPreferences.icon_size, wPreferences.icon_size, 0, 0);
732 if (w > wPreferences.icon_size)
733 w = wPreferences.icon_size;
734 x = (wPreferences.icon_size - w) / 2;
736 if (icon->show_title && (title_height < wPreferences.icon_size)) {
737 drawIconTitle(scr, pixmap, title_height);
739 if (h > wPreferences.icon_size - title_height - 2) {
740 h = wPreferences.icon_size - title_height - 2;
741 y = title_height + 1;
742 } else {
743 y = (wPreferences.icon_size - h - title_height) / 2 + title_height + 1;
745 } else {
746 if (w > wPreferences.icon_size)
747 w = wPreferences.icon_size;
748 y = (wPreferences.icon_size - h) / 2;
751 if (wwin->wm_hints->flags & IconMaskHint)
752 XSetClipMask(dpy, scr->copy_gc, wwin->wm_hints->icon_mask);
754 XSetClipOrigin(dpy, scr->copy_gc, x, y);
756 if (d != scr->w_depth) {
757 XSetForeground(dpy, scr->copy_gc, scr->black_pixel);
758 XSetBackground(dpy, scr->copy_gc, scr->white_pixel);
759 XCopyPlane(dpy, wwin->wm_hints->icon_pixmap, pixmap, scr->copy_gc, 0, 0, w, h, x, y, 1);
760 } else {
761 XCopyArea(dpy, wwin->wm_hints->icon_pixmap, pixmap, scr->copy_gc, 0, 0, w, h, x, y);
764 XSetClipOrigin(dpy, scr->copy_gc, 0, 0);
766 icon->pixmap = pixmap;
767 return (0);
770 void wIconPaint(WIcon * icon)
772 WScreen *scr = icon->core->screen_ptr;
773 int x;
774 char *tmp;
776 if (icon->force_paint) {
777 icon->force_paint = 0;
778 wIconUpdate(icon);
779 return;
782 XClearWindow(dpy, icon->core->window);
784 /* draw the icon title */
785 if (icon->show_title && icon->icon_name != NULL) {
786 int l;
787 int w;
789 tmp = ShrinkString(scr->icon_title_font, icon->icon_name, wPreferences.icon_size - 4);
790 w = WMWidthOfString(scr->icon_title_font, tmp, l = strlen(tmp));
792 if (w > icon->core->width - 4)
793 x = (icon->core->width - 4) - w;
794 else
795 x = (icon->core->width - w) / 2;
797 WMDrawString(scr->wmscreen, icon->core->window, scr->icon_title_color,
798 scr->icon_title_font, x, 1, tmp, l);
799 wfree(tmp);
802 if (icon->selected)
803 XDrawRectangle(dpy, icon->core->window, scr->icon_select_gc, 0, 0,
804 icon->core->width - 1, icon->core->height - 1);
807 /******************************************************************/
809 static void miniwindowExpose(WObjDescriptor * desc, XEvent * event)
811 wIconPaint(desc->parent);
814 static void miniwindowDblClick(WObjDescriptor * desc, XEvent * event)
816 WIcon *icon = desc->parent;
818 assert(icon->owner != NULL);
820 wDeiconifyWindow(icon->owner);
823 static void miniwindowMouseDown(WObjDescriptor * desc, XEvent * event)
825 WIcon *icon = desc->parent;
826 WWindow *wwin = icon->owner;
827 XEvent ev;
828 int x = wwin->icon_x, y = wwin->icon_y;
829 int dx = event->xbutton.x, dy = event->xbutton.y;
830 int grabbed = 0;
831 int clickButton = event->xbutton.button;
832 Bool hasMoved = False;
834 if (WCHECK_STATE(WSTATE_MODAL))
835 return;
837 if (IsDoubleClick(icon->core->screen_ptr, event)) {
838 miniwindowDblClick(desc, event);
839 return;
842 if (event->xbutton.button == Button1) {
843 if (event->xbutton.state & MOD_MASK)
844 wLowerFrame(icon->core);
845 else
846 wRaiseFrame(icon->core);
847 if (event->xbutton.state & ShiftMask) {
848 wIconSelect(icon);
849 wSelectWindow(icon->owner, !wwin->flags.selected);
851 } else if (event->xbutton.button == Button3) {
852 WObjDescriptor *desc;
854 OpenMiniwindowMenu(wwin, event->xbutton.x_root, event->xbutton.y_root);
856 /* allow drag select of menu */
857 desc = &wwin->screen_ptr->window_menu->menu->descriptor;
858 event->xbutton.send_event = True;
859 (*desc->handle_mousedown) (desc, event);
861 return;
864 if (XGrabPointer(dpy, icon->core->window, False, ButtonMotionMask
865 | ButtonReleaseMask | ButtonPressMask, GrabModeAsync,
866 GrabModeAsync, None, None, CurrentTime) != GrabSuccess) {
868 while (1) {
869 WMMaskEvent(dpy, PointerMotionMask | ButtonReleaseMask | ButtonPressMask
870 | ButtonMotionMask | ExposureMask, &ev);
871 switch (ev.type) {
872 case Expose:
873 WMHandleEvent(&ev);
874 break;
876 case MotionNotify:
877 hasMoved = True;
878 if (!grabbed) {
879 if (abs(dx - ev.xmotion.x) >= MOVE_THRESHOLD
880 || abs(dy - ev.xmotion.y) >= MOVE_THRESHOLD) {
881 XChangeActivePointerGrab(dpy, ButtonMotionMask
882 | ButtonReleaseMask | ButtonPressMask,
883 wCursor[WCUR_MOVE], CurrentTime);
884 grabbed = 1;
885 } else {
886 break;
889 x = ev.xmotion.x_root - dx;
890 y = ev.xmotion.y_root - dy;
891 XMoveWindow(dpy, icon->core->window, x, y);
892 break;
894 case ButtonPress:
895 break;
897 case ButtonRelease:
898 if (ev.xbutton.button != clickButton)
899 break;
901 if (wwin->icon_x != x || wwin->icon_y != y)
902 wwin->flags.icon_moved = 1;
904 XMoveWindow(dpy, icon->core->window, x, y);
906 wwin->icon_x = x;
907 wwin->icon_y = y;
908 XUngrabPointer(dpy, CurrentTime);
910 if (wPreferences.auto_arrange_icons)
911 wArrangeIcons(wwin->screen_ptr, True);
912 if (wPreferences.single_click && !hasMoved)
913 miniwindowDblClick(desc, event);
914 return;