Coding-style cleanup
[wmaker-crm.git] / src / icon.c
blobd7fd3cdfb1e5785673d5283e1299731404828684
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
53 extern Cursor wCursor[WCUR_LAST];
55 static void miniwindowExpose(WObjDescriptor * desc, XEvent * event);
56 static void miniwindowMouseDown(WObjDescriptor * desc, XEvent * event);
57 static void miniwindowDblClick(WObjDescriptor * desc, XEvent * event);
59 void get_pixmap_icon_from_icon_win(WIcon *icon);
60 int get_pixmap_icon_from_wm_hints(WScreen *scr, WWindow *wwin, WIcon *icon);
61 void get_pixmap_icon_from_user_icon(WScreen *scr, WIcon * icon);
62 /****** Notification Observers ******/
64 static void appearanceObserver(void *self, WMNotification * notif)
66 WIcon *icon = (WIcon *) self;
67 uintptr_t flags = (uintptr_t)WMGetNotificationClientData(notif);
69 if (flags & WTextureSettings) {
70 icon->force_paint = 1;
72 if (flags & WFontSettings) {
73 icon->force_paint = 1;
76 if (flags & WColorSettings) {
80 wIconPaint(icon);
82 /* so that the appicon expose handlers will paint the appicon specific
83 * stuff */
84 XClearArea(dpy, icon->core->window, 0, 0, icon->core->width, icon->core->height, True);
87 static void tileObserver(void *self, WMNotification * notif)
89 WIcon *icon = (WIcon *) self;
91 icon->force_paint = 1;
92 wIconPaint(icon);
94 XClearArea(dpy, icon->core->window, 0, 0, 1, 1, True);
97 /************************************/
99 INLINE static void getSize(Drawable d, unsigned int *w, unsigned int *h, unsigned int *dep)
101 Window rjunk;
102 int xjunk, yjunk;
103 unsigned int bjunk;
105 XGetGeometry(dpy, d, &rjunk, &xjunk, &yjunk, w, h, &bjunk, dep);
108 WIcon *wIconCreate(WWindow * wwin)
110 WScreen *scr = wwin->screen_ptr;
111 WIcon *icon;
112 char *file;
113 unsigned long vmask = 0;
114 XSetWindowAttributes attribs;
116 icon = wmalloc(sizeof(WIcon));
117 memset(icon, 0, sizeof(WIcon));
118 icon->core = wCoreCreateTopLevel(scr, wwin->icon_x, wwin->icon_y,
119 wPreferences.icon_size, wPreferences.icon_size, 0);
121 if (wPreferences.use_saveunders) {
122 vmask |= CWSaveUnder;
123 attribs.save_under = True;
125 /* a white border for selecting it */
126 vmask |= CWBorderPixel;
127 attribs.border_pixel = scr->white_pixel;
129 XChangeWindowAttributes(dpy, icon->core->window, vmask, &attribs);
131 /* will be overriden if this is an application icon */
132 icon->core->descriptor.handle_mousedown = miniwindowMouseDown;
133 icon->core->descriptor.handle_expose = miniwindowExpose;
134 icon->core->descriptor.parent_type = WCLASS_MINIWINDOW;
135 icon->core->descriptor.parent = icon;
137 icon->core->stacking = wmalloc(sizeof(WStacking));
138 icon->core->stacking->above = NULL;
139 icon->core->stacking->under = NULL;
140 icon->core->stacking->window_level = NORMAL_ICON_LEVEL;
141 icon->core->stacking->child_of = NULL;
143 icon->owner = wwin;
144 if (wwin->wm_hints && (wwin->wm_hints->flags & IconWindowHint)) {
145 if (wwin->client_win == wwin->main_window) {
146 WApplication *wapp;
147 /* do not let miniwindow steal app-icon's icon window */
148 wapp = wApplicationOf(wwin->client_win);
149 if (!wapp || wapp->app_icon == NULL)
150 icon->icon_win = wwin->wm_hints->icon_window;
151 } else {
152 icon->icon_win = wwin->wm_hints->icon_window;
155 #ifdef NO_MINIWINDOW_TITLES
156 icon->show_title = 0;
157 #else
158 icon->show_title = 1;
159 #endif
160 icon->file_image = wDefaultGetImage(scr, wwin->wm_instance, wwin->wm_class, wPreferences.icon_size);
162 file = wDefaultGetIconFile(scr, wwin->wm_instance, wwin->wm_class, False);
163 if (file)
164 icon->file = wstrdup(file);
166 icon->icon_name = wNETWMGetIconName(wwin->client_win);
167 if (icon->icon_name)
168 wwin->flags.net_has_icon_title = 1;
169 else
170 wGetIconName(dpy, wwin->client_win, &icon->icon_name);
172 icon->tile_type = TILE_NORMAL;
174 wIconUpdate(icon);
176 XFlush(dpy);
178 WMAddNotificationObserver(appearanceObserver, icon, WNIconAppearanceSettingsChanged, icon);
179 WMAddNotificationObserver(tileObserver, icon, WNIconTileSettingsChanged, icon);
180 return icon;
183 WIcon *wIconCreateWithIconFile(WScreen * scr, char *iconfile, int tile)
185 WIcon *icon;
186 unsigned long vmask = 0;
187 XSetWindowAttributes attribs;
189 icon = wmalloc(sizeof(WIcon));
190 memset(icon, 0, sizeof(WIcon));
191 icon->core = wCoreCreateTopLevel(scr, 0, 0, wPreferences.icon_size, wPreferences.icon_size, 0);
192 if (wPreferences.use_saveunders) {
193 vmask = CWSaveUnder;
194 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 if (iconfile) {
215 icon->file_image = RLoadImage(scr->rcontext, iconfile, 0);
216 if (!icon->file_image) {
217 wwarning(_("error loading image file \"%s\": %s"), iconfile, RMessageForError(RErrorCode));
220 icon->file_image = wIconValidateIconSize(scr, icon->file_image, wPreferences.icon_size);
222 icon->file = wstrdup(iconfile);
225 icon->tile_type = tile;
227 wIconUpdate(icon);
229 WMAddNotificationObserver(appearanceObserver, icon, WNIconAppearanceSettingsChanged, icon);
230 WMAddNotificationObserver(tileObserver, icon, WNIconTileSettingsChanged, icon);
232 return icon;
235 void wIconDestroy(WIcon * icon)
237 WCoreWindow *core = icon->core;
238 WScreen *scr = core->screen_ptr;
240 WMRemoveNotificationObserver(icon);
242 if (icon->handlerID)
243 WMDeleteTimerHandler(icon->handlerID);
245 if (icon->icon_win) {
246 int x = 0, y = 0;
248 if (icon->owner) {
249 x = icon->owner->icon_x;
250 y = icon->owner->icon_y;
252 XUnmapWindow(dpy, icon->icon_win);
253 XReparentWindow(dpy, icon->icon_win, scr->root_win, x, y);
255 if (icon->icon_name)
256 XFree(icon->icon_name);
258 if (icon->pixmap)
259 XFreePixmap(dpy, icon->pixmap);
261 if (icon->file)
262 wfree(icon->file);
264 if (icon->file_image != NULL)
265 RReleaseImage(icon->file_image);
267 wCoreDestroy(icon->core);
268 wfree(icon);
271 static void drawIconTitle(WScreen * scr, Pixmap pixmap, int height)
273 XFillRectangle(dpy, pixmap, scr->icon_title_texture->normal_gc, 0, 0, wPreferences.icon_size, height + 1);
274 XDrawLine(dpy, pixmap, scr->icon_title_texture->light_gc, 0, 0, wPreferences.icon_size, 0);
275 XDrawLine(dpy, pixmap, scr->icon_title_texture->light_gc, 0, 0, 0, height + 1);
276 XDrawLine(dpy, pixmap, scr->icon_title_texture->dim_gc,
277 wPreferences.icon_size - 1, 0, wPreferences.icon_size - 1, height + 1);
280 static Pixmap makeIcon(WScreen *scr, RImage *icon, int titled, int shadowed, int tileType, int highlighted)
282 RImage *tile;
283 Pixmap pixmap;
284 int x, y, sx, sy;
285 unsigned w, h;
286 int theight = 0;
288 if (tileType == TILE_NORMAL) {
289 tile = RCloneImage(scr->icon_tile);
290 } else {
291 assert(scr->clip_tile);
292 tile = RCloneImage(scr->clip_tile);
295 if (icon) {
296 w = (icon->width > wPreferences.icon_size)
297 ? wPreferences.icon_size : icon->width;
298 x = (wPreferences.icon_size - w) / 2;
299 sx = (icon->width - w) / 2;
301 if (titled)
302 theight = WMFontHeight(scr->icon_title_font);
304 h = (icon->height + theight > wPreferences.icon_size
305 ? wPreferences.icon_size - theight : icon->height);
306 y = theight + (wPreferences.icon_size - theight - h) / 2;
307 sy = (icon->height - h) / 2;
309 RCombineArea(tile, icon, sx, sy, w, h, x, y);
312 if (shadowed) {
313 RColor color;
315 color.red = scr->icon_back_texture->light.red >> 8;
316 color.green = scr->icon_back_texture->light.green >> 8;
317 color.blue = scr->icon_back_texture->light.blue >> 8;
318 color.alpha = 150; /* about 60% */
319 RClearImage(tile, &color);
322 if (highlighted) {
323 RColor color;
325 color.red = color.green = color.blue = 0;
326 color.alpha = 160;
327 RLightImage(tile, &color);
330 if (!RConvertImage(scr->rcontext, tile, &pixmap))
331 wwarning(_("error rendering image:%s"), RMessageForError(RErrorCode));
333 RReleaseImage(tile);
335 if (titled)
336 drawIconTitle(scr, pixmap, theight);
338 return pixmap;
341 void wIconChangeTitle(WIcon * icon, char *new_title)
343 int changed;
345 changed = (new_title == NULL && icon->icon_name != NULL)
346 || (new_title != NULL && icon->icon_name == NULL);
348 if (icon->icon_name != NULL)
349 XFree(icon->icon_name);
351 icon->icon_name = new_title;
353 if (changed)
354 icon->force_paint = 1;
355 wIconPaint(icon);
358 RImage *wIconValidateIconSize(WScreen * scr, RImage * icon, int max_size)
360 RImage *tmp;
361 int w, h;
363 if (!icon)
364 return NULL;
365 #ifndef DONT_SCALE_ICONS
366 if (max_size != 64) {
367 w = max_size * icon->width / 64;
368 h = max_size * icon->height / 64;
370 tmp = RScaleImage(icon, w, h);
371 RReleaseImage(icon);
372 icon = tmp;
374 #endif
376 return icon;
379 Bool wIconChangeImageFile(WIcon * icon, char *file)
381 WScreen *scr = icon->core->screen_ptr;
382 RImage *image;
383 char *path;
384 int error = 0;
386 if (icon->file_image)
387 RReleaseImage(icon->file_image);
389 if (!file) {
390 icon->file_image = NULL;
391 wIconUpdate(icon);
392 return True;
395 path = FindImage(wPreferences.icon_path, file);
397 if (path && (image = RLoadImage(scr->rcontext, path, 0))) {
398 icon->file_image = wIconValidateIconSize(icon->core->screen_ptr, image, wPreferences.icon_size);
399 wIconUpdate(icon);
400 } else {
401 error = 1;
404 if (path)
405 wfree(path);
407 return !error;
410 static char *getnameforicon(WWindow * wwin)
412 char *prefix, *suffix;
413 char *path;
414 int len;
416 if (wwin->wm_class && wwin->wm_instance) {
417 int len = strlen(wwin->wm_class) + strlen(wwin->wm_instance) + 2;
418 suffix = wmalloc(len);
419 snprintf(suffix, len, "%s.%s", wwin->wm_instance, wwin->wm_class);
420 } else if (wwin->wm_class) {
421 int len = strlen(wwin->wm_class) + 1;
422 suffix = wmalloc(len);
423 snprintf(suffix, len, "%s", wwin->wm_class);
424 } else if (wwin->wm_instance) {
425 int len = strlen(wwin->wm_instance) + 1;
426 suffix = wmalloc(len);
427 snprintf(suffix, len, "%s", wwin->wm_instance);
428 } else {
429 return NULL;
432 prefix = wusergnusteppath();
433 len = strlen(prefix) + 64 + strlen(suffix);
434 path = wmalloc(len + 1);
435 snprintf(path, len, "%s/Library/WindowMaker", prefix);
437 if (access(path, F_OK) != 0) {
438 if (mkdir(path, S_IRUSR | S_IWUSR | S_IXUSR)) {
439 werror(_("could not create directory %s"), path);
440 wfree(path);
441 wfree(suffix);
442 return NULL;
445 strcat(path, "/CachedPixmaps");
446 if (access(path, F_OK) != 0) {
447 if (mkdir(path, S_IRUSR | S_IWUSR | S_IXUSR) != 0) {
448 werror(_("could not create directory %s"), path);
449 wfree(path);
450 wfree(suffix);
451 return NULL;
455 strcat(path, "/");
456 strcat(path, suffix);
457 strcat(path, ".xpm");
458 wfree(suffix);
460 return path;
464 * wIconStore--
465 * Stores the client supplied icon at ~/GNUstep/Library/WindowMaker/CachedPixmaps
466 * and returns the path for that icon. Returns NULL if there is no
467 * client supplied icon or on failure.
469 * Side effects:
470 * New directories might be created.
472 char *wIconStore(WIcon * icon)
474 char *path;
475 RImage *image = NULL;
476 WWindow *wwin = icon->owner;
478 if (!wwin)
479 return NULL;
481 path = getnameforicon(wwin);
482 if (!path)
483 return NULL;
485 if (wwin->net_icon_image) {
486 image = RRetainImage(wwin->net_icon_image);
487 } else if (wwin->wm_hints && (wwin->wm_hints->flags & IconPixmapHint)
488 && wwin->wm_hints->icon_pixmap != None) {
489 image = RCreateImageFromDrawable(icon->core->screen_ptr->rcontext,
490 wwin->wm_hints->icon_pixmap, (wwin->wm_hints->flags & IconMaskHint)
491 ? wwin->wm_hints->icon_mask : None);
494 if (!image) {
495 wfree(path);
496 return NULL;
499 if (!RSaveImage(image, path, "XPM")) {
500 wfree(path);
501 path = NULL;
504 RReleaseImage(image);
506 return path;
509 static void cycleColor(void *data)
511 WIcon *icon = (WIcon *) data;
512 WScreen *scr = icon->core->screen_ptr;
513 XGCValues gcv;
515 icon->step--;
516 gcv.dash_offset = icon->step;
517 XChangeGC(dpy, scr->icon_select_gc, GCDashOffset, &gcv);
519 XDrawRectangle(dpy, icon->core->window, scr->icon_select_gc, 0, 0,
520 icon->core->width - 1, icon->core->height - 1);
521 icon->handlerID = WMAddTimerHandler(COLOR_CYCLE_DELAY, cycleColor, icon);
524 #ifdef NEWAPPICON
525 void wIconSetHighlited(WIcon *icon, Bool flag)
527 if (icon->highlighted == flag)
528 return;
530 icon->highlighted = flag;
531 icon->force_paint = True;
532 wIconPaint(icon);
534 #endif
536 void wIconSelect(WIcon * icon)
538 WScreen *scr = icon->core->screen_ptr;
539 icon->selected = !icon->selected;
541 if (icon->selected) {
542 icon->step = 0;
543 if (!wPreferences.dont_blink)
544 icon->handlerID = WMAddTimerHandler(10, cycleColor, icon);
545 else
546 XDrawRectangle(dpy, icon->core->window, scr->icon_select_gc, 0, 0,
547 icon->core->width - 1, icon->core->height - 1);
548 } else {
549 if (icon->handlerID) {
550 WMDeleteTimerHandler(icon->handlerID);
551 icon->handlerID = NULL;
553 XClearArea(dpy, icon->core->window, 0, 0, icon->core->width, icon->core->height, True);
557 void wIconUpdate(WIcon * icon)
559 WScreen *scr = icon->core->screen_ptr;
560 WWindow *wwin = icon->owner;
562 assert(scr->icon_tile != NULL);
564 if (icon->pixmap != None)
565 XFreePixmap(dpy, icon->pixmap);
567 icon->pixmap = None;
569 if (wwin && WFLAGP(wwin, always_user_icon)) {
570 /* Forced use user_icon */
571 get_pixmap_icon_from_user_icon(scr, icon);
572 } else if (icon->icon_win != None) {
573 /* Get the Pixmap from the WIcon */
574 get_pixmap_icon_from_icon_win(icon);
575 } else if (wwin && wwin->net_icon_image) {
576 /* Use _NET_WM_ICON icon */
577 icon->pixmap = makeIcon(scr, wwin->net_icon_image, icon->show_title,
578 icon->shadowed, icon->tile_type, icon->highlighted);
579 } else if (wwin && wwin->wm_hints && (wwin->wm_hints->flags & IconPixmapHint)) {
580 /* Get the Pixmap from the wm_hints, else, from the user */
581 if (get_pixmap_icon_from_wm_hints(scr, wwin, icon))
582 get_pixmap_icon_from_user_icon(scr, icon);
583 } else {
584 /* Get the Pixmap from the user */
585 get_pixmap_icon_from_user_icon(scr, icon);
588 /* No pixmap, set default background */
589 if (icon->pixmap != None)
590 XSetWindowBackgroundPixmap(dpy, icon->core->window, icon->pixmap);
592 /* Paint it */
593 XClearWindow(dpy, icon->core->window);
594 wIconPaint(icon);
597 void get_pixmap_icon_from_user_icon(WScreen *scr, WIcon * icon)
599 RImage *image = NULL;
600 char *path;
601 char *file;
603 if (icon->file_image) {
604 icon->pixmap = makeIcon(scr, icon->file_image, icon->show_title,
605 icon->shadowed, icon->tile_type, icon->highlighted);
606 } else {
607 /* make default icons */
608 if (!scr->def_icon_pixmap) {
609 file = wDefaultGetIconFile(scr, NULL, NULL, False);
610 if (file) {
611 path = FindImage(wPreferences.icon_path, file);
612 if (path) {
613 image = RLoadImage(scr->rcontext, path, 0);
614 if (!image)
615 wwarning(_("could not load default icon \"%s\":%s"),
616 file, RMessageForError(RErrorCode));
617 wfree(path);
618 } else {
619 wwarning(_("could not find default icon \"%s\""), file);
621 /* FIXME: Probably wfree(file) here! */
624 image = wIconValidateIconSize(scr, image, wPreferences.icon_size);
625 scr->def_icon_pixmap = makeIcon(scr, image, False, False, icon->tile_type, icon->highlighted);
626 scr->def_ticon_pixmap = makeIcon(scr, image, True, False, icon->tile_type, icon->highlighted);
627 if (image)
628 RReleaseImage(image);
631 if (icon->show_title)
632 XSetWindowBackgroundPixmap(dpy, icon->core->window, scr->def_ticon_pixmap);
633 else
634 XSetWindowBackgroundPixmap(dpy, icon->core->window, scr->def_icon_pixmap);
636 icon->pixmap = None;
640 /* Get the Pixmap from the WIcon of the WWindow */
641 void get_pixmap_icon_from_icon_win(WIcon * icon)
643 XWindowAttributes attr;
644 WScreen *scr = icon->core->screen_ptr;
645 int title_height = WMFontHeight(scr->icon_title_font);
646 unsigned int width, height, depth;
647 int theight;
648 int resize = 0;
649 Pixmap pixmap;
651 getSize(icon->icon_win, &width, &height, &depth);
653 if (width > wPreferences.icon_size) {
654 resize = 1;
655 width = wPreferences.icon_size;
658 if (height > wPreferences.icon_size) {
659 resize = 1;
660 height = wPreferences.icon_size;
663 if (icon->show_title && (height + title_height < wPreferences.icon_size)) {
664 pixmap = XCreatePixmap(dpy, scr->w_win, wPreferences.icon_size,
665 wPreferences.icon_size, scr->w_depth);
666 XSetClipMask(dpy, scr->copy_gc, None);
667 XCopyArea(dpy, scr->icon_tile_pixmap, pixmap, scr->copy_gc, 0, 0,
668 wPreferences.icon_size, wPreferences.icon_size, 0, 0);
669 drawIconTitle(scr, pixmap, title_height);
670 theight = title_height;
671 } else {
672 pixmap = None;
673 theight = 0;
674 XSetWindowBackgroundPixmap(dpy, icon->core->window, scr->icon_tile_pixmap);
677 XSetWindowBorderWidth(dpy, icon->icon_win, 0);
678 XReparentWindow(dpy, icon->icon_win, icon->core->window,
679 (wPreferences.icon_size - width) / 2,
680 theight + (wPreferences.icon_size - height - theight) / 2);
681 if (resize)
682 XResizeWindow(dpy, icon->icon_win, width, height);
684 XMapWindow(dpy, icon->icon_win);
685 XAddToSaveSet(dpy, icon->icon_win);
687 /* Save it */
688 icon->pixmap = pixmap;
690 if ((XGetWindowAttributes(dpy, icon->icon_win, &attr)) &&
691 (attr.all_event_masks & ButtonPressMask))
692 wHackedGrabButton(Button1, MOD_MASK, icon->core->window, True,
693 ButtonPressMask, GrabModeSync, GrabModeAsync,
694 None, wCursor[WCUR_ARROW]);
697 /* Get the Pixmap from the XWindow wm_hints */
698 int get_pixmap_icon_from_wm_hints(WScreen *scr, WWindow *wwin, WIcon *icon)
700 Window jw;
701 Pixmap pixmap;
702 unsigned int w, h, ju, d;
703 int ji, x, y;
704 int title_height = WMFontHeight(scr->icon_title_font);
706 if (!XGetGeometry(dpy, wwin->wm_hints->icon_pixmap, &jw, &ji, &ji, &w, &h, &ju, &d)) {
707 icon->owner->wm_hints->flags &= ~IconPixmapHint;
708 return(1);
711 pixmap = XCreatePixmap(dpy, icon->core->window, wPreferences.icon_size,
712 wPreferences.icon_size, scr->w_depth);
713 XSetClipMask(dpy, scr->copy_gc, None);
714 XCopyArea(dpy, scr->icon_tile_pixmap, pixmap, scr->copy_gc, 0, 0,
715 wPreferences.icon_size, wPreferences.icon_size, 0, 0);
717 if (w > wPreferences.icon_size)
718 w = wPreferences.icon_size;
719 x = (wPreferences.icon_size - w) / 2;
721 if (icon->show_title && (title_height < wPreferences.icon_size)) {
722 drawIconTitle(scr, pixmap, title_height);
724 if (h > wPreferences.icon_size - title_height - 2) {
725 h = wPreferences.icon_size - title_height - 2;
726 y = title_height + 1;
727 } else {
728 y = (wPreferences.icon_size - h - title_height) / 2 + title_height + 1;
730 } else {
731 if (w > wPreferences.icon_size)
732 w = wPreferences.icon_size;
733 y = (wPreferences.icon_size - h) / 2;
736 if (wwin->wm_hints->flags & IconMaskHint)
737 XSetClipMask(dpy, scr->copy_gc, wwin->wm_hints->icon_mask);
739 XSetClipOrigin(dpy, scr->copy_gc, x, y);
741 if (d != scr->w_depth) {
742 XSetForeground(dpy, scr->copy_gc, scr->black_pixel);
743 XSetBackground(dpy, scr->copy_gc, scr->white_pixel);
744 XCopyPlane(dpy, wwin->wm_hints->icon_pixmap, pixmap, scr->copy_gc, 0, 0, w, h, x, y, 1);
745 } else {
746 XCopyArea(dpy, wwin->wm_hints->icon_pixmap, pixmap, scr->copy_gc, 0, 0, w, h, x, y);
749 XSetClipOrigin(dpy, scr->copy_gc, 0, 0);
751 icon->pixmap = pixmap;
752 return (0);
755 void wIconPaint(WIcon * icon)
757 WScreen *scr = icon->core->screen_ptr;
758 int x;
759 char *tmp;
761 if (icon->force_paint) {
762 icon->force_paint = 0;
763 wIconUpdate(icon);
764 return;
767 XClearWindow(dpy, icon->core->window);
769 /* draw the icon title */
770 if (icon->show_title && icon->icon_name != NULL) {
771 int l;
772 int w;
774 tmp = ShrinkString(scr->icon_title_font, icon->icon_name, wPreferences.icon_size - 4);
775 w = WMWidthOfString(scr->icon_title_font, tmp, l = strlen(tmp));
777 if (w > icon->core->width - 4)
778 x = (icon->core->width - 4) - w;
779 else
780 x = (icon->core->width - w) / 2;
782 WMDrawString(scr->wmscreen, icon->core->window, scr->icon_title_color,
783 scr->icon_title_font, x, 1, tmp, l);
784 wfree(tmp);
787 if (icon->selected)
788 XDrawRectangle(dpy, icon->core->window, scr->icon_select_gc, 0, 0,
789 icon->core->width - 1, icon->core->height - 1);
792 /******************************************************************/
794 static void miniwindowExpose(WObjDescriptor * desc, XEvent * event)
796 wIconPaint(desc->parent);
799 static void miniwindowDblClick(WObjDescriptor * desc, XEvent * event)
801 WIcon *icon = desc->parent;
803 assert(icon->owner != NULL);
805 wDeiconifyWindow(icon->owner);
808 static void miniwindowMouseDown(WObjDescriptor * desc, XEvent * event)
810 WIcon *icon = desc->parent;
811 WWindow *wwin = icon->owner;
812 XEvent ev;
813 int x = wwin->icon_x, y = wwin->icon_y;
814 int dx = event->xbutton.x, dy = event->xbutton.y;
815 int grabbed = 0;
816 int clickButton = event->xbutton.button;
817 Bool hasMoved = False;
819 if (WCHECK_STATE(WSTATE_MODAL))
820 return;
822 if (IsDoubleClick(icon->core->screen_ptr, event)) {
823 miniwindowDblClick(desc, event);
824 return;
827 if (event->xbutton.button == Button1) {
828 if (event->xbutton.state & MOD_MASK)
829 wLowerFrame(icon->core);
830 else
831 wRaiseFrame(icon->core);
832 if (event->xbutton.state & ShiftMask) {
833 wIconSelect(icon);
834 wSelectWindow(icon->owner, !wwin->flags.selected);
836 } else if (event->xbutton.button == Button3) {
837 WObjDescriptor *desc;
839 OpenMiniwindowMenu(wwin, event->xbutton.x_root, event->xbutton.y_root);
841 /* allow drag select of menu */
842 desc = &wwin->screen_ptr->window_menu->menu->descriptor;
843 event->xbutton.send_event = True;
844 (*desc->handle_mousedown) (desc, event);
846 return;
849 if (XGrabPointer(dpy, icon->core->window, False, ButtonMotionMask
850 | ButtonReleaseMask | ButtonPressMask, GrabModeAsync,
851 GrabModeAsync, None, None, CurrentTime) != GrabSuccess) {
853 while (1) {
854 WMMaskEvent(dpy, PointerMotionMask | ButtonReleaseMask | ButtonPressMask
855 | ButtonMotionMask | ExposureMask, &ev);
856 switch (ev.type) {
857 case Expose:
858 WMHandleEvent(&ev);
859 break;
861 case MotionNotify:
862 hasMoved = True;
863 if (!grabbed) {
864 if (abs(dx - ev.xmotion.x) >= MOVE_THRESHOLD
865 || abs(dy - ev.xmotion.y) >= MOVE_THRESHOLD) {
866 XChangeActivePointerGrab(dpy, ButtonMotionMask
867 | ButtonReleaseMask | ButtonPressMask,
868 wCursor[WCUR_MOVE], CurrentTime);
869 grabbed = 1;
870 } else {
871 break;
874 x = ev.xmotion.x_root - dx;
875 y = ev.xmotion.y_root - dy;
876 XMoveWindow(dpy, icon->core->window, x, y);
877 break;
879 case ButtonPress:
880 break;
882 case ButtonRelease:
883 if (ev.xbutton.button != clickButton)
884 break;
886 if (wwin->icon_x != x || wwin->icon_y != y)
887 wwin->flags.icon_moved = 1;
889 XMoveWindow(dpy, icon->core->window, x, y);
891 wwin->icon_x = x;
892 wwin->icon_y = y;
893 XUngrabPointer(dpy, CurrentTime);
895 if (wPreferences.auto_arrange_icons)
896 wArrangeIcons(wwin->screen_ptr, True);
897 if (wPreferences.single_click && !hasMoved)
898 miniwindowDblClick(desc, event);
899 return;