- Check whether libXft is at least version 2.1.2 else refuse to compile.
[wmaker-crm.git] / src / icon.c
blob86efbb03332dbca48773fa19716ed90a341dd6e0
1 /* icon.c - window icon and dock and appicon parent
2 *
3 * Window Maker window manager
4 *
5 * Copyright (c) 1997-2003 Alfredo K. Kojima
6 *
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
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
20 * USA.
23 #include "wconfig.h"
25 #include <X11/Xlib.h>
26 #include <X11/Xutil.h>
27 #include <stdlib.h>
28 #include <stdio.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"
47 /**** Global variables ****/
48 extern WPreferences wPreferences;
50 #define MOD_MASK wPreferences.modifier_mask
52 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);
60 /****** Notification Observers ******/
62 static void
63 appearanceObserver(void *self, WMNotification *notif)
65 WIcon *icon = (WIcon*)self;
66 int flags = (int)WMGetNotificationClientData(notif);
68 if (flags & WTextureSettings) {
69 icon->force_paint = 1;
71 if (flags & WFontSettings) {
72 icon->force_paint = 1;
75 if (flags & WColorSettings) {
79 wIconPaint(icon);
81 /* so that the appicon expose handlers will paint the appicon specific
82 * stuff */
83 XClearArea(dpy, icon->core->window, 0, 0, icon->core->width,
84 icon->core->height, True);
88 static void
89 tileObserver(void *self, WMNotification *notif)
91 WIcon *icon = (WIcon*)self;
93 icon->force_paint = 1;
94 wIconPaint(icon);
96 XClearArea(dpy, icon->core->window, 0, 0, 1, 1, True);
99 /************************************/
103 INLINE static void
104 getSize(Drawable d, unsigned int *w, unsigned int *h, unsigned int *dep)
106 Window rjunk;
107 int xjunk, yjunk;
108 unsigned int bjunk;
110 XGetGeometry(dpy, d, &rjunk, &xjunk, &yjunk, w, h, &bjunk, dep);
114 WIcon*
115 wIconCreate(WWindow *wwin)
117 WScreen *scr=wwin->screen_ptr;
118 WIcon *icon;
119 char *file;
120 unsigned long vmask = 0;
121 XSetWindowAttributes attribs;
123 icon = wmalloc(sizeof(WIcon));
124 memset(icon, 0, sizeof(WIcon));
125 icon->core = wCoreCreateTopLevel(scr, wwin->icon_x, wwin->icon_y,
126 wPreferences.icon_size,
127 wPreferences.icon_size, 0);
129 if (wPreferences.use_saveunders) {
130 vmask |= CWSaveUnder;
131 attribs.save_under = True;
133 /* a white border for selecting it */
134 vmask |= CWBorderPixel;
135 attribs.border_pixel = scr->white_pixel;
137 XChangeWindowAttributes(dpy, icon->core->window, vmask, &attribs);
140 /* will be overriden if this is an application icon */
141 icon->core->descriptor.handle_mousedown = miniwindowMouseDown;
142 icon->core->descriptor.handle_expose = miniwindowExpose;
143 icon->core->descriptor.parent_type = WCLASS_MINIWINDOW;
144 icon->core->descriptor.parent = icon;
146 icon->core->stacking = wmalloc(sizeof(WStacking));
147 icon->core->stacking->above = NULL;
148 icon->core->stacking->under = NULL;
149 icon->core->stacking->window_level = NORMAL_ICON_LEVEL;
150 icon->core->stacking->child_of = NULL;
152 icon->owner = wwin;
153 if (wwin->wm_hints && (wwin->wm_hints->flags & IconWindowHint)) {
154 if (wwin->client_win == wwin->main_window) {
155 WApplication *wapp;
156 /* do not let miniwindow steal app-icon's icon window */
157 wapp = wApplicationOf(wwin->client_win);
158 if (!wapp || wapp->app_icon==NULL)
159 icon->icon_win = wwin->wm_hints->icon_window;
160 } else {
161 icon->icon_win = wwin->wm_hints->icon_window;
164 #ifdef NO_MINIWINDOW_TITLES
165 icon->show_title = 0;
166 #else
167 icon->show_title = 1;
168 #endif
169 #ifdef NETWM_HINTS
170 if (!icon->image && !WFLAGP(wwin, always_user_icon))
171 icon->image = RRetainImage(wwin->net_icon_image);
172 if (!icon->image)
173 #endif
174 icon->image = wDefaultGetImage(scr, wwin->wm_instance, wwin->wm_class);
176 file = wDefaultGetIconFile(scr, wwin->wm_instance, wwin->wm_class,
177 False);
178 if (file) {
179 icon->file = wstrdup(file);
182 wGetIconName(dpy, wwin->client_win, &icon->icon_name);
184 icon->tile_type = TILE_NORMAL;
186 wIconUpdate(icon);
188 XFlush(dpy);
190 WMAddNotificationObserver(appearanceObserver, icon,
191 WNIconAppearanceSettingsChanged, icon);
192 WMAddNotificationObserver(tileObserver, icon,
193 WNIconTileSettingsChanged, icon);
194 return icon;
198 WIcon*
199 wIconCreateWithIconFile(WScreen *scr, char *iconfile, int tile)
201 WIcon *icon;
202 unsigned long vmask = 0;
203 XSetWindowAttributes attribs;
205 icon = wmalloc(sizeof(WIcon));
206 memset(icon, 0, sizeof(WIcon));
207 icon->core = wCoreCreateTopLevel(scr, 0, 0, wPreferences.icon_size,
208 wPreferences.icon_size, 0);
209 if (wPreferences.use_saveunders) {
210 vmask = CWSaveUnder;
211 attribs.save_under = True;
213 /* a white border for selecting it */
214 vmask |= CWBorderPixel;
215 attribs.border_pixel = scr->white_pixel;
217 XChangeWindowAttributes(dpy, icon->core->window, vmask, &attribs);
219 /* will be overriden if this is a application icon */
220 icon->core->descriptor.handle_mousedown = miniwindowMouseDown;
221 icon->core->descriptor.handle_expose = miniwindowExpose;
222 icon->core->descriptor.parent_type = WCLASS_MINIWINDOW;
223 icon->core->descriptor.parent = icon;
225 icon->core->stacking = wmalloc(sizeof(WStacking));
226 icon->core->stacking->above = NULL;
227 icon->core->stacking->under = NULL;
228 icon->core->stacking->window_level = NORMAL_ICON_LEVEL;
229 icon->core->stacking->child_of = NULL;
231 if (iconfile) {
232 icon->image = RLoadImage(scr->rcontext, iconfile, 0);
233 if (!icon->image) {
234 wwarning(_("error loading image file \"%s\""), iconfile, RMessageForError(RErrorCode));
237 icon->image = wIconValidateIconSize(scr, icon->image);
239 icon->file = wstrdup(iconfile);
242 icon->tile_type = tile;
244 wIconUpdate(icon);
246 WMAddNotificationObserver(appearanceObserver, icon,
247 WNIconAppearanceSettingsChanged, icon);
248 WMAddNotificationObserver(tileObserver, icon,
249 WNIconTileSettingsChanged, icon);
251 return icon;
256 void
257 wIconDestroy(WIcon *icon)
259 WCoreWindow *core = icon->core;
260 WScreen *scr = core->screen_ptr;
262 WMRemoveNotificationObserver(icon);
264 if (icon->handlerID)
265 WMDeleteTimerHandler(icon->handlerID);
267 if (icon->icon_win) {
268 int x=0, y=0;
270 if (icon->owner) {
271 x = icon->owner->icon_x;
272 y = icon->owner->icon_y;
274 XUnmapWindow(dpy, icon->icon_win);
275 XReparentWindow(dpy, icon->icon_win, scr->root_win, x, y);
277 if (icon->icon_name)
278 XFree(icon->icon_name);
280 if (icon->pixmap)
281 XFreePixmap(dpy, icon->pixmap);
283 if (icon->file)
284 wfree(icon->file);
286 if (icon->image!=NULL)
287 RReleaseImage(icon->image);
289 wCoreDestroy(icon->core);
290 wfree(icon);
295 static void
296 drawIconTitle(WScreen *scr, Pixmap pixmap, int height)
298 XFillRectangle(dpy, pixmap, scr->icon_title_texture->normal_gc,
299 0, 0, wPreferences.icon_size, height+1);
300 XDrawLine(dpy, pixmap, scr->icon_title_texture->light_gc, 0, 0,
301 wPreferences.icon_size, 0);
302 XDrawLine(dpy, pixmap, scr->icon_title_texture->light_gc, 0, 0,
303 0, height+1);
304 XDrawLine(dpy, pixmap, scr->icon_title_texture->dim_gc,
305 wPreferences.icon_size-1, 0, wPreferences.icon_size-1, height+1);
309 static Pixmap
310 makeIcon(WScreen *scr, RImage *icon, int titled, int shadowed, int tileType)
312 RImage *tile;
313 Pixmap pixmap;
314 int x, y, sx, sy;
315 unsigned w, h;
316 int theight = WMFontHeight(scr->icon_title_font);
318 if (tileType == TILE_NORMAL)
319 tile = RCloneImage(scr->icon_tile);
320 else {
321 assert(scr->clip_tile);
322 tile = RCloneImage(scr->clip_tile);
324 if (icon) {
325 w = (icon->width > wPreferences.icon_size)
326 ? wPreferences.icon_size : icon->width;
327 x = (wPreferences.icon_size - w) / 2;
328 sx = (icon->width - w)/2;
330 if (!titled) {
331 h = (icon->height > wPreferences.icon_size)
332 ? wPreferences.icon_size : icon->height;
333 y = (wPreferences.icon_size - h) / 2;
334 sy = (icon->height - h)/2;
335 } else {
336 h = (icon->height+theight > wPreferences.icon_size
337 ? wPreferences.icon_size-theight : icon->height);
338 y = theight+((int)wPreferences.icon_size-theight-h)/2;
339 sy = (icon->height - h)/2;
341 RCombineArea(tile, icon, sx, sy, w, h, x, y);
344 if (shadowed) {
345 RColor color;
347 color.red = scr->icon_back_texture->light.red >> 8;
348 color.green = scr->icon_back_texture->light.green >> 8;
349 color.blue = scr->icon_back_texture->light.blue >> 8;
350 color.alpha = 150; /* about 60% */
351 RClearImage(tile, &color);
354 if (!RConvertImage(scr->rcontext, tile, &pixmap)) {
355 wwarning(_("error rendering image:%s"), RMessageForError(RErrorCode));
357 RReleaseImage(tile);
359 if (titled)
360 drawIconTitle(scr, pixmap, theight);
362 return pixmap;
366 void
367 wIconChangeTitle(WIcon *icon, char *new_title)
369 int changed;
371 changed = (new_title==NULL && icon->icon_name!=NULL)
372 || (new_title!=NULL && icon->icon_name==NULL);
374 if (icon->icon_name!=NULL)
375 XFree(icon->icon_name);
377 icon->icon_name = new_title;
379 if (changed)
380 icon->force_paint = 1;
381 wIconPaint(icon);
385 void
386 wIconChangeImage(WIcon *icon, RImage *new_image)
388 assert(icon != NULL);
390 if (icon->image)
391 RReleaseImage(icon->image);
393 icon->image = new_image;
395 wIconUpdate(icon);
399 RImage*
400 wIconValidateIconSize(WScreen *scr, RImage *icon)
402 RImage *tmp;
403 int w, h;
405 if (!icon)
406 return NULL;
407 #ifndef DONT_SCALE_ICONS
408 if (wPreferences.icon_size != 64) {
409 w = wPreferences.icon_size * icon->width / 64;
410 h = wPreferences.icon_size * icon->height / 64;
412 tmp = RScaleImage(icon, w, h);
413 RReleaseImage(icon);
414 icon = tmp;
416 #endif
417 #if 0
418 if (icon->width > wPreferences.icon_size
419 || icon->height > wPreferences.icon_size) {
420 if (icon->width > icon->height) {
421 w = wPreferences.icon_size - 4;
422 h = w*icon->height/icon->width;
423 } else {
424 h = wPreferences.icon_size - 4;
425 w = h*icon->width/icon->height;
427 tmp = RScaleImage(icon, w, h);
428 RReleaseImage(icon);
429 icon = tmp;
431 #endif
433 return icon;
437 Bool
438 wIconChangeImageFile(WIcon *icon, char *file)
440 WScreen *scr = icon->core->screen_ptr;
441 RImage *image;
442 char *path;
443 int error = 0;
445 if (!file) {
446 wIconChangeImage(icon, NULL);
447 return True;
450 path = FindImage(wPreferences.icon_path, file);
452 if (path && (image = RLoadImage(scr->rcontext, path, 0))) {
453 image = wIconValidateIconSize(icon->core->screen_ptr, image);
455 wIconChangeImage(icon, image);
456 } else {
457 error = 1;
460 if (path)
461 wfree(path);
463 return !error;
468 static char*
469 getnameforicon(WWindow *wwin)
471 char *prefix, *suffix;
472 char *path;
473 int len;
475 if (wwin->wm_class && wwin->wm_instance) {
476 int len = strlen(wwin->wm_class)+strlen(wwin->wm_instance)+2;
477 suffix = wmalloc(len);
478 snprintf(suffix, len, "%s.%s", wwin->wm_instance, wwin->wm_class);
479 } else if (wwin->wm_class) {
480 int len = strlen(wwin->wm_class)+1;
481 suffix = wmalloc(len);
482 snprintf(suffix, len, "%s", wwin->wm_class);
483 } else if (wwin->wm_instance) {
484 int len = strlen(wwin->wm_instance)+1;
485 suffix = wmalloc(len);
486 snprintf(suffix, len, "%s", wwin->wm_instance);
487 } else {
488 return NULL;
491 prefix = wusergnusteppath();
492 len = strlen(prefix)+64+strlen(suffix);
493 path = wmalloc(len+1);
494 snprintf(path, len, "%s/.AppInfo", prefix);
496 if (access(path, F_OK)!=0) {
497 if (mkdir(path, S_IRUSR|S_IWUSR|S_IXUSR)) {
498 wsyserror(_("could not create directory %s"), path);
499 wfree(path);
500 wfree(suffix);
501 return NULL;
504 strcat(path, "/WindowMaker");
505 if (access(path, F_OK)!=0) {
506 if (mkdir(path, S_IRUSR|S_IWUSR|S_IXUSR)!=0) {
507 wsyserror(_("could not create directory %s"), path);
508 wfree(path);
509 wfree(suffix);
510 return NULL;
514 strcat(path, "/");
515 strcat(path, suffix);
516 strcat(path, ".xpm");
517 wfree(suffix);
519 return path;
524 * wIconStore--
525 * Stores the client supplied icon at ~/GNUstep/.AppInfo/WindowMaker
526 * and returns the path for that icon. Returns NULL if there is no
527 * client supplied icon or on failure.
529 * Side effects:
530 * New directories might be created.
532 char*
533 wIconStore(WIcon *icon)
535 char *path;
536 RImage *image;
537 WWindow *wwin = icon->owner;
539 if (!wwin || !wwin->wm_hints || !(wwin->wm_hints->flags & IconPixmapHint)
540 || wwin->wm_hints->icon_pixmap == None)
541 return NULL;
543 path = getnameforicon(wwin);
544 if (!path)
545 return NULL;
547 image = RCreateImageFromDrawable(icon->core->screen_ptr->rcontext,
548 wwin->wm_hints->icon_pixmap,
549 (wwin->wm_hints->flags & IconMaskHint)
550 ? wwin->wm_hints->icon_mask : None);
551 if (!image) {
552 wfree(path);
553 return NULL;
556 if (!RSaveImage(image, path, "XPM")) {
557 wfree(path);
558 path = NULL;
560 RReleaseImage(image);
562 return path;
567 void
568 wIconChangeIconWindow(WIcon *icon, Window new_window);
571 static void
572 cycleColor(void *data)
574 WIcon *icon = (WIcon*)data;
575 WScreen *scr = icon->core->screen_ptr;
576 XGCValues gcv;
578 icon->step--;
579 gcv.dash_offset = icon->step;
580 XChangeGC(dpy, scr->icon_select_gc, GCDashOffset, &gcv);
582 XDrawRectangle(dpy, icon->core->window, scr->icon_select_gc, 0, 0,
583 icon->core->width-1, icon->core->height-1);
584 icon->handlerID = WMAddTimerHandler(COLOR_CYCLE_DELAY, cycleColor, icon);
588 void
589 wIconSetHighlited(WIcon *icon, Bool flag)
591 if (icon->highlighted == flag) {
592 return;
595 icon->highlighted = flag;
596 wIconPaint(icon);
601 void
602 wIconSelect(WIcon *icon)
604 WScreen *scr = icon->core->screen_ptr;
605 icon->selected = !icon->selected;
607 if (icon->selected) {
608 icon->step = 0;
609 if (!wPreferences.dont_blink)
610 icon->handlerID = WMAddTimerHandler(10, cycleColor, icon);
611 else
612 XDrawRectangle(dpy, icon->core->window, scr->icon_select_gc, 0, 0,
613 icon->core->width-1, icon->core->height-1);
614 } else {
615 if (icon->handlerID) {
616 WMDeleteTimerHandler(icon->handlerID);
617 icon->handlerID = NULL;
619 XClearArea(dpy, icon->core->window, 0, 0, icon->core->width,
620 icon->core->height, True);
625 void
626 wIconUpdate(WIcon *icon)
628 WScreen *scr = icon->core->screen_ptr;
629 int title_height = WMFontHeight(scr->icon_title_font);
630 WWindow *wwin = icon->owner;
632 assert(scr->icon_tile!=NULL);
634 if (icon->pixmap!=None)
635 XFreePixmap(dpy, icon->pixmap);
636 icon->pixmap = None;
639 if (wwin && (WFLAGP(wwin, always_user_icon)
640 #ifdef NETWM_HINTS
641 || wwin->net_icon_image
642 #endif
644 goto user_icon;
646 /* use client specified icon window */
647 if (icon->icon_win!=None) {
648 XWindowAttributes attr;
649 int resize=0;
650 int width, height, depth;
651 int theight;
652 Pixmap pixmap;
654 getSize(icon->icon_win, &width, &height, &depth);
656 if (width > wPreferences.icon_size) {
657 resize = 1;
658 width = wPreferences.icon_size;
660 if (height > wPreferences.icon_size) {
661 resize = 1;
662 height = wPreferences.icon_size;
664 if (icon->show_title
665 && (height+title_height < wPreferences.icon_size)) {
666 pixmap = XCreatePixmap(dpy, scr->w_win, wPreferences.icon_size,
667 wPreferences.icon_size, scr->w_depth);
668 XSetClipMask(dpy, scr->copy_gc, None);
669 XCopyArea(dpy, scr->icon_tile_pixmap, pixmap, scr->copy_gc, 0, 0,
670 wPreferences.icon_size, wPreferences.icon_size, 0, 0);
671 drawIconTitle(scr, pixmap, title_height);
672 theight = title_height;
673 } else {
674 pixmap = None;
675 theight = 0;
676 XSetWindowBackgroundPixmap(dpy, icon->core->window,
677 scr->icon_tile_pixmap);
680 XSetWindowBorderWidth(dpy, icon->icon_win, 0);
681 XReparentWindow(dpy, icon->icon_win, icon->core->window,
682 (wPreferences.icon_size-width)/2,
683 theight+(wPreferences.icon_size-height-theight)/2);
684 if (resize)
685 XResizeWindow(dpy, icon->icon_win, width, height);
687 XMapWindow(dpy, icon->icon_win);
689 XAddToSaveSet(dpy, icon->icon_win);
691 icon->pixmap = pixmap;
693 if (XGetWindowAttributes(dpy, icon->icon_win, &attr)) {
694 if (attr.all_event_masks & ButtonPressMask) {
695 wHackedGrabButton(Button1, MOD_MASK, icon->core->window, True,
696 ButtonPressMask, GrabModeSync, GrabModeAsync,
697 None, wCursor[WCUR_ARROW]);
700 } else if (wwin && wwin->wm_hints
701 && (wwin->wm_hints->flags & IconPixmapHint)) {
702 int x, y;
703 unsigned int w, h;
704 Window jw;
705 int ji, dotitle;
706 unsigned int ju, d;
707 Pixmap pixmap;
709 if (!XGetGeometry(dpy, wwin->wm_hints->icon_pixmap, &jw,
710 &ji, &ji, &w, &h, &ju, &d)) {
711 icon->owner->wm_hints->flags &= ~IconPixmapHint;
712 goto user_icon;
715 pixmap = XCreatePixmap(dpy, icon->core->window, wPreferences.icon_size,
716 wPreferences.icon_size, scr->w_depth);
717 XSetClipMask(dpy, scr->copy_gc, None);
718 XCopyArea(dpy, scr->icon_tile_pixmap, pixmap, scr->copy_gc, 0, 0,
719 wPreferences.icon_size, wPreferences.icon_size, 0, 0);
721 if (w > wPreferences.icon_size)
722 w = wPreferences.icon_size;
723 x = (wPreferences.icon_size-w)/2;
725 if (icon->show_title && (title_height < wPreferences.icon_size)) {
726 drawIconTitle(scr, pixmap, title_height);
727 dotitle = 1;
729 if (h > wPreferences.icon_size - title_height - 2) {
730 h = wPreferences.icon_size - title_height - 2;
731 y = title_height + 1;
732 } else {
733 y = (wPreferences.icon_size-h-title_height)/2+title_height + 1;
735 } else {
736 dotitle = 0;
737 if (w > wPreferences.icon_size)
738 w = wPreferences.icon_size;
739 y = (wPreferences.icon_size-h)/2;
742 if (wwin->wm_hints->flags & IconMaskHint)
743 XSetClipMask(dpy, scr->copy_gc, wwin->wm_hints->icon_mask);
745 XSetClipOrigin(dpy, scr->copy_gc, x, y);
747 if (d != scr->w_depth) {
748 XSetForeground(dpy, scr->copy_gc, scr->black_pixel);
749 XSetBackground(dpy, scr->copy_gc, scr->white_pixel);
750 XCopyPlane(dpy, wwin->wm_hints->icon_pixmap, pixmap, scr->copy_gc,
751 0, 0, w, h, x, y, 1);
752 } else {
753 XCopyArea(dpy, wwin->wm_hints->icon_pixmap, pixmap, scr->copy_gc,
754 0, 0, w, h, x, y);
757 XSetClipOrigin(dpy, scr->copy_gc, 0, 0);
759 icon->pixmap = pixmap;
760 } else {
761 user_icon:
763 if (icon->image) {
764 icon->pixmap = makeIcon(scr, icon->image, icon->show_title,
765 icon->shadowed, icon->tile_type);
766 } else {
767 /* make default icons */
769 if (!scr->def_icon_pixmap) {
770 RImage *image = NULL;
771 char *path;
772 char *file;
774 file = wDefaultGetIconFile(scr, NULL, NULL, False);
775 if (file) {
776 path = FindImage(wPreferences.icon_path, file);
777 if (!path) {
778 wwarning(_("could not find default icon \"%s\""),file);
779 goto make_icons;
782 image = RLoadImage(scr->rcontext, path, 0);
783 if (!image) {
784 wwarning(_("could not load default icon \"%s\":%s"),
785 file, RMessageForError(RErrorCode));
787 wfree(path);
789 make_icons:
791 image = wIconValidateIconSize(scr, image);
792 scr->def_icon_pixmap = makeIcon(scr, image, False, False,
793 icon->tile_type);
794 scr->def_ticon_pixmap = makeIcon(scr, image, True, False,
795 icon->tile_type);
796 if (image)
797 RReleaseImage(image);
800 if (icon->show_title) {
801 XSetWindowBackgroundPixmap(dpy, icon->core->window,
802 scr->def_ticon_pixmap);
803 } else {
804 XSetWindowBackgroundPixmap(dpy, icon->core->window,
805 scr->def_icon_pixmap);
807 icon->pixmap = None;
810 if (icon->pixmap != None) {
811 XSetWindowBackgroundPixmap(dpy, icon->core->window, icon->pixmap);
813 XClearWindow(dpy, icon->core->window);
815 wIconPaint(icon);
820 void
821 wIconPaint(WIcon *icon)
823 WScreen *scr=icon->core->screen_ptr;
824 int x;
825 char *tmp;
827 if (icon->force_paint) {
828 icon->force_paint = 0;
829 wIconUpdate(icon);
830 return;
833 XClearWindow(dpy, icon->core->window);
835 /* draw the icon title */
836 if (icon->show_title && icon->icon_name!=NULL) {
837 int l;
838 int w;
840 tmp = ShrinkString(scr->icon_title_font, icon->icon_name,
841 wPreferences.icon_size-4);
842 w = WMWidthOfString(scr->icon_title_font, tmp, l=strlen(tmp));
844 if (w > icon->core->width - 4)
845 x = (icon->core->width - 4) - w;
846 else
847 x = (icon->core->width - w)/2;
849 WMDrawString(scr->wmscreen, icon->core->window, scr->icon_title_color,
850 scr->icon_title_font, x, 1, tmp, l);
851 wfree(tmp);
854 if (icon->selected)
855 XDrawRectangle(dpy, icon->core->window, scr->icon_select_gc, 0, 0,
856 icon->core->width-1, icon->core->height-1);
860 /******************************************************************/
862 static void
863 miniwindowExpose(WObjDescriptor *desc, XEvent *event)
865 wIconPaint(desc->parent);
869 static void
870 miniwindowDblClick(WObjDescriptor *desc, XEvent *event)
872 WIcon *icon = desc->parent;
874 assert(icon->owner!=NULL);
876 wDeiconifyWindow(icon->owner);
880 static void
881 miniwindowMouseDown(WObjDescriptor *desc, XEvent *event)
883 WIcon *icon = desc->parent;
884 WWindow *wwin = icon->owner;
885 XEvent ev;
886 int x=wwin->icon_x, y=wwin->icon_y;
887 int dx=event->xbutton.x, dy=event->xbutton.y;
888 int grabbed=0;
889 int clickButton=event->xbutton.button;
891 if (WCHECK_STATE(WSTATE_MODAL))
892 return;
894 if (IsDoubleClick(icon->core->screen_ptr, event)) {
895 miniwindowDblClick(desc, event);
896 return;
899 #ifdef DEBUG
900 puts("Moving miniwindow");
901 #endif
902 if (event->xbutton.button == Button1) {
903 if (event->xbutton.state & MOD_MASK)
904 wLowerFrame(icon->core);
905 else
906 wRaiseFrame(icon->core);
907 if (event->xbutton.state & ShiftMask) {
908 wIconSelect(icon);
909 wSelectWindow(icon->owner, !wwin->flags.selected);
911 } else if (event->xbutton.button == Button3) {
912 WObjDescriptor *desc;
914 OpenMiniwindowMenu(wwin, event->xbutton.x_root,
915 event->xbutton.y_root);
917 /* allow drag select of menu */
918 desc = &wwin->screen_ptr->window_menu->menu->descriptor;
919 event->xbutton.send_event = True;
920 (*desc->handle_mousedown)(desc, event);
922 return;
925 if (XGrabPointer(dpy, icon->core->window, False, ButtonMotionMask
926 |ButtonReleaseMask|ButtonPressMask, GrabModeAsync,
927 GrabModeAsync, None, None, CurrentTime) !=GrabSuccess) {
928 #ifdef DEBUG0
929 wwarning("pointer grab failed for icon move");
930 #endif
932 while(1) {
933 WMMaskEvent(dpy, PointerMotionMask|ButtonReleaseMask|ButtonPressMask
934 |ButtonMotionMask|ExposureMask, &ev);
935 switch (ev.type) {
936 case Expose:
937 WMHandleEvent(&ev);
938 break;
940 case MotionNotify:
941 if (!grabbed) {
942 if (abs(dx-ev.xmotion.x)>=MOVE_THRESHOLD
943 || abs(dy-ev.xmotion.y)>=MOVE_THRESHOLD) {
944 XChangeActivePointerGrab(dpy, ButtonMotionMask
945 |ButtonReleaseMask|ButtonPressMask,
946 wCursor[WCUR_MOVE], CurrentTime);
947 grabbed=1;
948 } else {
949 break;
952 x = ev.xmotion.x_root - dx;
953 y = ev.xmotion.y_root - dy;
954 XMoveWindow(dpy, icon->core->window, x, y);
955 break;
957 case ButtonPress:
958 break;
960 case ButtonRelease:
961 if (ev.xbutton.button != clickButton)
962 break;
964 if (wwin->icon_x!=x || wwin->icon_y!=y)
965 wwin->flags.icon_moved = 1;
967 XMoveWindow(dpy, icon->core->window, x, y);
969 wwin->icon_x = x;
970 wwin->icon_y = y;
971 #ifdef DEBUG
972 puts("End miniwindow move");
973 #endif
974 XUngrabPointer(dpy, CurrentTime);
976 if (wPreferences.auto_arrange_icons)
977 wArrangeIcons(wwin->screen_ptr, True);
978 return;