applied patch to rename .AppInfo directory
[wmaker-crm.git] / src / icon.c
blobcd6c90ae7f8520c9cbeb60c83a7c15ee620db6c6
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
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"
46 #include "wmspec.h"
48 /**** Global varianebles ****/
49 extern WPreferences wPreferences;
51 #define MOD_MASK wPreferences.modifier_mask
53 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);
61 /****** Notification Observers ******/
63 static void
64 appearanceObserver(void *self, WMNotification *notif)
66 WIcon *icon = (WIcon*)self;
67 int flags = (int)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,
85 icon->core->height, True);
89 static void
90 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 /************************************/
104 INLINE static void
105 getSize(Drawable d, unsigned int *w, unsigned int *h, unsigned int *dep)
107 Window rjunk;
108 int xjunk, yjunk;
109 unsigned int bjunk;
111 XGetGeometry(dpy, d, &rjunk, &xjunk, &yjunk, w, h, &bjunk, dep);
115 WIcon*
116 wIconCreate(WWindow *wwin)
118 WScreen *scr=wwin->screen_ptr;
119 WIcon *icon;
120 char *file;
121 unsigned long vmask = 0;
122 XSetWindowAttributes attribs;
124 icon = wmalloc(sizeof(WIcon));
125 memset(icon, 0, sizeof(WIcon));
126 icon->core = wCoreCreateTopLevel(scr, wwin->icon_x, wwin->icon_y,
127 wPreferences.icon_size,
128 wPreferences.icon_size, 0);
130 if (wPreferences.use_saveunders) {
131 vmask |= CWSaveUnder;
132 attribs.save_under = True;
134 /* a white border for selecting it */
135 vmask |= CWBorderPixel;
136 attribs.border_pixel = scr->white_pixel;
138 XChangeWindowAttributes(dpy, icon->core->window, vmask, &attribs);
141 /* will be overriden if this is an application icon */
142 icon->core->descriptor.handle_mousedown = miniwindowMouseDown;
143 icon->core->descriptor.handle_expose = miniwindowExpose;
144 icon->core->descriptor.parent_type = WCLASS_MINIWINDOW;
145 icon->core->descriptor.parent = icon;
147 icon->core->stacking = wmalloc(sizeof(WStacking));
148 icon->core->stacking->above = NULL;
149 icon->core->stacking->under = NULL;
150 icon->core->stacking->window_level = NORMAL_ICON_LEVEL;
151 icon->core->stacking->child_of = NULL;
153 icon->owner = wwin;
154 if (wwin->wm_hints && (wwin->wm_hints->flags & IconWindowHint)) {
155 if (wwin->client_win == wwin->main_window) {
156 WApplication *wapp;
157 /* do not let miniwindow steal app-icon's icon window */
158 wapp = wApplicationOf(wwin->client_win);
159 if (!wapp || wapp->app_icon==NULL)
160 icon->icon_win = wwin->wm_hints->icon_window;
161 } else {
162 icon->icon_win = wwin->wm_hints->icon_window;
165 #ifdef NO_MINIWINDOW_TITLES
166 icon->show_title = 0;
167 #else
168 icon->show_title = 1;
169 #endif
170 #ifdef NETWM_HINTS
171 if (!icon->image && !WFLAGP(wwin, always_user_icon))
172 icon->image = RRetainImage(wwin->net_icon_image);
173 if (!icon->image)
174 #endif
175 icon->image = wDefaultGetImage(scr, wwin->wm_instance, wwin->wm_class);
177 file = wDefaultGetIconFile(scr, wwin->wm_instance, wwin->wm_class,
178 False);
179 if (file) {
180 icon->file = wstrdup(file);
183 icon->icon_name = wNETWMGetIconName(wwin->client_win);
184 if (icon->icon_name)
185 wwin->flags.net_has_icon_title= 1;
186 else
187 wGetIconName(dpy, wwin->client_win, &icon->icon_name);
189 icon->tile_type = TILE_NORMAL;
191 wIconUpdate(icon);
193 XFlush(dpy);
195 WMAddNotificationObserver(appearanceObserver, icon,
196 WNIconAppearanceSettingsChanged, icon);
197 WMAddNotificationObserver(tileObserver, icon,
198 WNIconTileSettingsChanged, icon);
199 return icon;
203 WIcon*
204 wIconCreateWithIconFile(WScreen *scr, char *iconfile, int tile)
206 WIcon *icon;
207 unsigned long vmask = 0;
208 XSetWindowAttributes attribs;
210 icon = wmalloc(sizeof(WIcon));
211 memset(icon, 0, sizeof(WIcon));
212 icon->core = wCoreCreateTopLevel(scr, 0, 0, wPreferences.icon_size,
213 wPreferences.icon_size, 0);
214 if (wPreferences.use_saveunders) {
215 vmask = CWSaveUnder;
216 attribs.save_under = True;
218 /* a white border for selecting it */
219 vmask |= CWBorderPixel;
220 attribs.border_pixel = scr->white_pixel;
222 XChangeWindowAttributes(dpy, icon->core->window, vmask, &attribs);
224 /* will be overriden if this is a application icon */
225 icon->core->descriptor.handle_mousedown = miniwindowMouseDown;
226 icon->core->descriptor.handle_expose = miniwindowExpose;
227 icon->core->descriptor.parent_type = WCLASS_MINIWINDOW;
228 icon->core->descriptor.parent = icon;
230 icon->core->stacking = wmalloc(sizeof(WStacking));
231 icon->core->stacking->above = NULL;
232 icon->core->stacking->under = NULL;
233 icon->core->stacking->window_level = NORMAL_ICON_LEVEL;
234 icon->core->stacking->child_of = NULL;
236 if (iconfile) {
237 icon->image = RLoadImage(scr->rcontext, iconfile, 0);
238 if (!icon->image) {
239 wwarning(_("error loading image file \"%s\""), iconfile, RMessageForError(RErrorCode));
242 icon->image = wIconValidateIconSize(scr, icon->image);
244 icon->file = wstrdup(iconfile);
247 icon->tile_type = tile;
249 wIconUpdate(icon);
251 WMAddNotificationObserver(appearanceObserver, icon,
252 WNIconAppearanceSettingsChanged, icon);
253 WMAddNotificationObserver(tileObserver, icon,
254 WNIconTileSettingsChanged, icon);
256 return icon;
261 void
262 wIconDestroy(WIcon *icon)
264 WCoreWindow *core = icon->core;
265 WScreen *scr = core->screen_ptr;
267 WMRemoveNotificationObserver(icon);
269 if (icon->handlerID)
270 WMDeleteTimerHandler(icon->handlerID);
272 if (icon->icon_win) {
273 int x=0, y=0;
275 if (icon->owner) {
276 x = icon->owner->icon_x;
277 y = icon->owner->icon_y;
279 XUnmapWindow(dpy, icon->icon_win);
280 XReparentWindow(dpy, icon->icon_win, scr->root_win, x, y);
282 if (icon->icon_name)
283 XFree(icon->icon_name);
285 if (icon->pixmap)
286 XFreePixmap(dpy, icon->pixmap);
288 if (icon->file)
289 wfree(icon->file);
291 if (icon->image!=NULL)
292 RReleaseImage(icon->image);
294 wCoreDestroy(icon->core);
295 wfree(icon);
300 static void
301 drawIconTitle(WScreen *scr, Pixmap pixmap, int height)
303 XFillRectangle(dpy, pixmap, scr->icon_title_texture->normal_gc,
304 0, 0, wPreferences.icon_size, height+1);
305 XDrawLine(dpy, pixmap, scr->icon_title_texture->light_gc, 0, 0,
306 wPreferences.icon_size, 0);
307 XDrawLine(dpy, pixmap, scr->icon_title_texture->light_gc, 0, 0,
308 0, height+1);
309 XDrawLine(dpy, pixmap, scr->icon_title_texture->dim_gc,
310 wPreferences.icon_size-1, 0, wPreferences.icon_size-1, height+1);
314 static Pixmap
315 makeIcon(WScreen *scr, RImage *icon, int titled, int shadowed, int tileType)
317 RImage *tile;
318 Pixmap pixmap;
319 int x, y, sx, sy;
320 unsigned w, h;
321 int theight = WMFontHeight(scr->icon_title_font);
323 if (tileType == TILE_NORMAL)
324 tile = RCloneImage(scr->icon_tile);
325 else {
326 assert(scr->clip_tile);
327 tile = RCloneImage(scr->clip_tile);
329 if (icon) {
330 w = (icon->width > wPreferences.icon_size)
331 ? wPreferences.icon_size : icon->width;
332 x = (wPreferences.icon_size - w) / 2;
333 sx = (icon->width - w)/2;
335 if (!titled) {
336 h = (icon->height > wPreferences.icon_size)
337 ? wPreferences.icon_size : icon->height;
338 y = (wPreferences.icon_size - h) / 2;
339 sy = (icon->height - h)/2;
340 } else {
341 h = (icon->height+theight > wPreferences.icon_size
342 ? wPreferences.icon_size-theight : icon->height);
343 y = theight+((int)wPreferences.icon_size-theight-h)/2;
344 sy = (icon->height - h)/2;
346 RCombineArea(tile, icon, sx, sy, w, h, x, y);
349 if (shadowed) {
350 RColor color;
352 color.red = scr->icon_back_texture->light.red >> 8;
353 color.green = scr->icon_back_texture->light.green >> 8;
354 color.blue = scr->icon_back_texture->light.blue >> 8;
355 color.alpha = 150; /* about 60% */
356 RClearImage(tile, &color);
359 if (!RConvertImage(scr->rcontext, tile, &pixmap)) {
360 wwarning(_("error rendering image:%s"), RMessageForError(RErrorCode));
362 RReleaseImage(tile);
364 if (titled)
365 drawIconTitle(scr, pixmap, theight);
367 return pixmap;
371 void
372 wIconChangeTitle(WIcon *icon, char *new_title)
374 int changed;
376 changed = (new_title==NULL && icon->icon_name!=NULL)
377 || (new_title!=NULL && icon->icon_name==NULL);
379 if (icon->icon_name!=NULL)
380 XFree(icon->icon_name);
382 icon->icon_name = new_title;
384 if (changed)
385 icon->force_paint = 1;
386 wIconPaint(icon);
390 void
391 wIconChangeImage(WIcon *icon, RImage *new_image)
393 assert(icon != NULL);
395 if (icon->image)
396 RReleaseImage(icon->image);
398 icon->image = new_image;
400 wIconUpdate(icon);
404 RImage*
405 wIconValidateIconSize(WScreen *scr, RImage *icon)
407 RImage *tmp;
408 int w, h;
410 if (!icon)
411 return NULL;
412 #ifndef DONT_SCALE_ICONS
413 if (wPreferences.icon_size != 64) {
414 w = wPreferences.icon_size * icon->width / 64;
415 h = wPreferences.icon_size * icon->height / 64;
417 tmp = RScaleImage(icon, w, h);
418 RReleaseImage(icon);
419 icon = tmp;
421 #endif
422 #if 0
423 if (icon->width > wPreferences.icon_size
424 || icon->height > wPreferences.icon_size) {
425 if (icon->width > icon->height) {
426 w = wPreferences.icon_size - 4;
427 h = w*icon->height/icon->width;
428 } else {
429 h = wPreferences.icon_size - 4;
430 w = h*icon->width/icon->height;
432 tmp = RScaleImage(icon, w, h);
433 RReleaseImage(icon);
434 icon = tmp;
436 #endif
438 return icon;
442 Bool
443 wIconChangeImageFile(WIcon *icon, char *file)
445 WScreen *scr = icon->core->screen_ptr;
446 RImage *image;
447 char *path;
448 int error = 0;
450 if (!file) {
451 wIconChangeImage(icon, NULL);
452 return True;
455 path = FindImage(wPreferences.icon_path, file);
457 if (path && (image = RLoadImage(scr->rcontext, path, 0))) {
458 image = wIconValidateIconSize(icon->core->screen_ptr, image);
460 wIconChangeImage(icon, image);
461 } else {
462 error = 1;
465 if (path)
466 wfree(path);
468 return !error;
473 static char*
474 getnameforicon(WWindow *wwin)
476 char *prefix, *suffix;
477 char *path;
478 int len;
480 if (wwin->wm_class && wwin->wm_instance) {
481 int len = strlen(wwin->wm_class)+strlen(wwin->wm_instance)+2;
482 suffix = wmalloc(len);
483 snprintf(suffix, len, "%s.%s", wwin->wm_instance, wwin->wm_class);
484 } else if (wwin->wm_class) {
485 int len = strlen(wwin->wm_class)+1;
486 suffix = wmalloc(len);
487 snprintf(suffix, len, "%s", wwin->wm_class);
488 } else if (wwin->wm_instance) {
489 int len = strlen(wwin->wm_instance)+1;
490 suffix = wmalloc(len);
491 snprintf(suffix, len, "%s", wwin->wm_instance);
492 } else {
493 return NULL;
496 prefix = wusergnusteppath();
497 len = strlen(prefix)+64+strlen(suffix);
498 path = wmalloc(len+1);
499 snprintf(path, len, "%s/Library/WindowMaker/CachedPixmaps", prefix);
501 if (access(path, F_OK)!=0) {
502 if (mkdir(path, S_IRUSR|S_IWUSR|S_IXUSR)) {
503 wsyserror(_("could not create directory %s"), path);
504 wfree(path);
505 wfree(suffix);
506 return NULL;
509 strcat(path, "/WindowMaker");
510 if (access(path, F_OK)!=0) {
511 if (mkdir(path, S_IRUSR|S_IWUSR|S_IXUSR)!=0) {
512 wsyserror(_("could not create directory %s"), path);
513 wfree(path);
514 wfree(suffix);
515 return NULL;
519 strcat(path, "/");
520 strcat(path, suffix);
521 strcat(path, ".xpm");
522 wfree(suffix);
524 return path;
529 * wIconStore--
530 * Stores the client supplied icon at ~/GNUstep/Library/WindowMaker/CachedPixmaps
531 * and returns the path for that icon. Returns NULL if there is no
532 * client supplied icon or on failure.
534 * Side effects:
535 * New directories might be created.
537 char*
538 wIconStore(WIcon *icon)
540 char *path;
541 RImage *image;
542 WWindow *wwin = icon->owner;
544 if (!wwin || !wwin->wm_hints || !(wwin->wm_hints->flags & IconPixmapHint)
545 || wwin->wm_hints->icon_pixmap == None)
546 return NULL;
548 path = getnameforicon(wwin);
549 if (!path)
550 return NULL;
552 image = RCreateImageFromDrawable(icon->core->screen_ptr->rcontext,
553 wwin->wm_hints->icon_pixmap,
554 (wwin->wm_hints->flags & IconMaskHint)
555 ? wwin->wm_hints->icon_mask : None);
556 if (!image) {
557 wfree(path);
558 return NULL;
561 if (!RSaveImage(image, path, "XPM")) {
562 wfree(path);
563 path = NULL;
565 RReleaseImage(image);
567 return path;
572 void wIconChangeIconWindow(WIcon *icon, Window new_window);
575 static void
576 cycleColor(void *data)
578 WIcon *icon = (WIcon*)data;
579 WScreen *scr = icon->core->screen_ptr;
580 XGCValues gcv;
582 icon->step--;
583 gcv.dash_offset = icon->step;
584 XChangeGC(dpy, scr->icon_select_gc, GCDashOffset, &gcv);
586 XDrawRectangle(dpy, icon->core->window, scr->icon_select_gc, 0, 0,
587 icon->core->width-1, icon->core->height-1);
588 icon->handlerID = WMAddTimerHandler(COLOR_CYCLE_DELAY, cycleColor, icon);
592 void
593 wIconSetHighlited(WIcon *icon, Bool flag)
595 if (icon->highlighted == flag) {
596 return;
599 icon->highlighted = flag;
600 wIconPaint(icon);
605 void
606 wIconSelect(WIcon *icon)
608 WScreen *scr = icon->core->screen_ptr;
609 icon->selected = !icon->selected;
611 if (icon->selected) {
612 icon->step = 0;
613 if (!wPreferences.dont_blink)
614 icon->handlerID = WMAddTimerHandler(10, cycleColor, icon);
615 else
616 XDrawRectangle(dpy, icon->core->window, scr->icon_select_gc, 0, 0,
617 icon->core->width-1, icon->core->height-1);
618 } else {
619 if (icon->handlerID) {
620 WMDeleteTimerHandler(icon->handlerID);
621 icon->handlerID = NULL;
623 XClearArea(dpy, icon->core->window, 0, 0, icon->core->width,
624 icon->core->height, True);
629 void
630 wIconUpdate(WIcon *icon)
632 WScreen *scr = icon->core->screen_ptr;
633 int title_height = WMFontHeight(scr->icon_title_font);
634 WWindow *wwin = icon->owner;
636 assert(scr->icon_tile!=NULL);
638 if (icon->pixmap!=None)
639 XFreePixmap(dpy, icon->pixmap);
640 icon->pixmap = None;
643 if (wwin && (WFLAGP(wwin, always_user_icon)
644 #ifdef NETWM_HINTS
645 || wwin->net_icon_image
646 #endif
648 goto user_icon;
650 /* use client specified icon window */
651 if (icon->icon_win!=None) {
652 XWindowAttributes attr;
653 int resize=0;
654 int width, height, depth;
655 int theight;
656 Pixmap pixmap;
658 getSize(icon->icon_win, &width, &height, &depth);
660 if (width > wPreferences.icon_size) {
661 resize = 1;
662 width = wPreferences.icon_size;
664 if (height > wPreferences.icon_size) {
665 resize = 1;
666 height = wPreferences.icon_size;
668 if (icon->show_title
669 && (height+title_height < wPreferences.icon_size)) {
670 pixmap = XCreatePixmap(dpy, scr->w_win, wPreferences.icon_size,
671 wPreferences.icon_size, scr->w_depth);
672 XSetClipMask(dpy, scr->copy_gc, None);
673 XCopyArea(dpy, scr->icon_tile_pixmap, pixmap, scr->copy_gc, 0, 0,
674 wPreferences.icon_size, wPreferences.icon_size, 0, 0);
675 drawIconTitle(scr, pixmap, title_height);
676 theight = title_height;
677 } else {
678 pixmap = None;
679 theight = 0;
680 XSetWindowBackgroundPixmap(dpy, icon->core->window,
681 scr->icon_tile_pixmap);
684 XSetWindowBorderWidth(dpy, icon->icon_win, 0);
685 XReparentWindow(dpy, icon->icon_win, icon->core->window,
686 (wPreferences.icon_size-width)/2,
687 theight+(wPreferences.icon_size-height-theight)/2);
688 if (resize)
689 XResizeWindow(dpy, icon->icon_win, width, height);
691 XMapWindow(dpy, icon->icon_win);
693 XAddToSaveSet(dpy, icon->icon_win);
695 icon->pixmap = pixmap;
697 if (XGetWindowAttributes(dpy, icon->icon_win, &attr)) {
698 if (attr.all_event_masks & ButtonPressMask) {
699 wHackedGrabButton(Button1, MOD_MASK, icon->core->window, True,
700 ButtonPressMask, GrabModeSync, GrabModeAsync,
701 None, wCursor[WCUR_ARROW]);
704 } else if (wwin && wwin->wm_hints
705 && (wwin->wm_hints->flags & IconPixmapHint)) {
706 int x, y;
707 unsigned int w, h;
708 Window jw;
709 int ji, dotitle;
710 unsigned int ju, d;
711 Pixmap pixmap;
713 if (!XGetGeometry(dpy, wwin->wm_hints->icon_pixmap, &jw,
714 &ji, &ji, &w, &h, &ju, &d)) {
715 icon->owner->wm_hints->flags &= ~IconPixmapHint;
716 goto user_icon;
719 pixmap = XCreatePixmap(dpy, icon->core->window, wPreferences.icon_size,
720 wPreferences.icon_size, scr->w_depth);
721 XSetClipMask(dpy, scr->copy_gc, None);
722 XCopyArea(dpy, scr->icon_tile_pixmap, pixmap, scr->copy_gc, 0, 0,
723 wPreferences.icon_size, wPreferences.icon_size, 0, 0);
725 if (w > wPreferences.icon_size)
726 w = wPreferences.icon_size;
727 x = (wPreferences.icon_size-w)/2;
729 if (icon->show_title && (title_height < wPreferences.icon_size)) {
730 drawIconTitle(scr, pixmap, title_height);
731 dotitle = 1;
733 if (h > wPreferences.icon_size - title_height - 2) {
734 h = wPreferences.icon_size - title_height - 2;
735 y = title_height + 1;
736 } else {
737 y = (wPreferences.icon_size-h-title_height)/2+title_height + 1;
739 } else {
740 dotitle = 0;
741 if (w > wPreferences.icon_size)
742 w = wPreferences.icon_size;
743 y = (wPreferences.icon_size-h)/2;
746 if (wwin->wm_hints->flags & IconMaskHint)
747 XSetClipMask(dpy, scr->copy_gc, wwin->wm_hints->icon_mask);
749 XSetClipOrigin(dpy, scr->copy_gc, x, y);
751 if (d != scr->w_depth) {
752 XSetForeground(dpy, scr->copy_gc, scr->black_pixel);
753 XSetBackground(dpy, scr->copy_gc, scr->white_pixel);
754 XCopyPlane(dpy, wwin->wm_hints->icon_pixmap, pixmap, scr->copy_gc,
755 0, 0, w, h, x, y, 1);
756 } else {
757 XCopyArea(dpy, wwin->wm_hints->icon_pixmap, pixmap, scr->copy_gc,
758 0, 0, w, h, x, y);
761 XSetClipOrigin(dpy, scr->copy_gc, 0, 0);
763 icon->pixmap = pixmap;
764 } else {
765 user_icon:
767 if (icon->image) {
768 icon->pixmap = makeIcon(scr, icon->image, icon->show_title,
769 icon->shadowed, icon->tile_type);
770 } else {
771 /* make default icons */
773 if (!scr->def_icon_pixmap) {
774 RImage *image = NULL;
775 char *path;
776 char *file;
778 file = wDefaultGetIconFile(scr, NULL, NULL, False);
779 if (file) {
780 path = FindImage(wPreferences.icon_path, file);
781 if (!path) {
782 wwarning(_("could not find default icon \"%s\""),file);
783 goto make_icons;
786 image = RLoadImage(scr->rcontext, path, 0);
787 if (!image) {
788 wwarning(_("could not load default icon \"%s\":%s"),
789 file, RMessageForError(RErrorCode));
791 wfree(path);
793 make_icons:
795 image = wIconValidateIconSize(scr, image);
796 scr->def_icon_pixmap = makeIcon(scr, image, False, False,
797 icon->tile_type);
798 scr->def_ticon_pixmap = makeIcon(scr, image, True, False,
799 icon->tile_type);
800 if (image)
801 RReleaseImage(image);
804 if (icon->show_title) {
805 XSetWindowBackgroundPixmap(dpy, icon->core->window,
806 scr->def_ticon_pixmap);
807 } else {
808 XSetWindowBackgroundPixmap(dpy, icon->core->window,
809 scr->def_icon_pixmap);
811 icon->pixmap = None;
814 if (icon->pixmap != None) {
815 XSetWindowBackgroundPixmap(dpy, icon->core->window, icon->pixmap);
817 XClearWindow(dpy, icon->core->window);
819 wIconPaint(icon);
824 void
825 wIconPaint(WIcon *icon)
827 WScreen *scr=icon->core->screen_ptr;
828 int x;
829 char *tmp;
831 if (icon->force_paint) {
832 icon->force_paint = 0;
833 wIconUpdate(icon);
834 return;
837 XClearWindow(dpy, icon->core->window);
839 /* draw the icon title */
840 if (icon->show_title && icon->icon_name!=NULL) {
841 int l;
842 int w;
844 tmp = ShrinkString(scr->icon_title_font, icon->icon_name,
845 wPreferences.icon_size-4);
846 w = WMWidthOfString(scr->icon_title_font, tmp, l=strlen(tmp));
848 if (w > icon->core->width - 4)
849 x = (icon->core->width - 4) - w;
850 else
851 x = (icon->core->width - w)/2;
853 WMDrawString(scr->wmscreen, icon->core->window, scr->icon_title_color,
854 scr->icon_title_font, x, 1, tmp, l);
855 wfree(tmp);
858 if (icon->selected)
859 XDrawRectangle(dpy, icon->core->window, scr->icon_select_gc, 0, 0,
860 icon->core->width-1, icon->core->height-1);
864 /******************************************************************/
866 static void
867 miniwindowExpose(WObjDescriptor *desc, XEvent *event)
869 wIconPaint(desc->parent);
873 static void
874 miniwindowDblClick(WObjDescriptor *desc, XEvent *event)
876 WIcon *icon = desc->parent;
878 assert(icon->owner!=NULL);
880 wDeiconifyWindow(icon->owner);
884 static void
885 miniwindowMouseDown(WObjDescriptor *desc, XEvent *event)
887 WIcon *icon = desc->parent;
888 WWindow *wwin = icon->owner;
889 XEvent ev;
890 int x=wwin->icon_x, y=wwin->icon_y;
891 int dx=event->xbutton.x, dy=event->xbutton.y;
892 int grabbed=0;
893 int clickButton=event->xbutton.button;
895 if (WCHECK_STATE(WSTATE_MODAL))
896 return;
898 if (IsDoubleClick(icon->core->screen_ptr, event)) {
899 miniwindowDblClick(desc, event);
900 return;
903 #ifdef DEBUG
904 puts("Moving miniwindow");
905 #endif
906 if (event->xbutton.button == Button1) {
907 if (event->xbutton.state & MOD_MASK)
908 wLowerFrame(icon->core);
909 else
910 wRaiseFrame(icon->core);
911 if (event->xbutton.state & ShiftMask) {
912 wIconSelect(icon);
913 wSelectWindow(icon->owner, !wwin->flags.selected);
915 } else if (event->xbutton.button == Button3) {
916 WObjDescriptor *desc;
918 OpenMiniwindowMenu(wwin, event->xbutton.x_root,
919 event->xbutton.y_root);
921 /* allow drag select of menu */
922 desc = &wwin->screen_ptr->window_menu->menu->descriptor;
923 event->xbutton.send_event = True;
924 (*desc->handle_mousedown)(desc, event);
926 return;
929 if (XGrabPointer(dpy, icon->core->window, False, ButtonMotionMask
930 |ButtonReleaseMask|ButtonPressMask, GrabModeAsync,
931 GrabModeAsync, None, None, CurrentTime) !=GrabSuccess) {
932 #ifdef DEBUG0
933 wwarning("pointer grab failed for icon move");
934 #endif
936 while(1) {
937 WMMaskEvent(dpy, PointerMotionMask|ButtonReleaseMask|ButtonPressMask
938 |ButtonMotionMask|ExposureMask, &ev);
939 switch (ev.type) {
940 case Expose:
941 WMHandleEvent(&ev);
942 break;
944 case MotionNotify:
945 if (!grabbed) {
946 if (abs(dx-ev.xmotion.x)>=MOVE_THRESHOLD
947 || abs(dy-ev.xmotion.y)>=MOVE_THRESHOLD) {
948 XChangeActivePointerGrab(dpy, ButtonMotionMask
949 |ButtonReleaseMask|ButtonPressMask,
950 wCursor[WCUR_MOVE], CurrentTime);
951 grabbed=1;
952 } else {
953 break;
956 x = ev.xmotion.x_root - dx;
957 y = ev.xmotion.y_root - dy;
958 XMoveWindow(dpy, icon->core->window, x, y);
959 break;
961 case ButtonPress:
962 break;
964 case ButtonRelease:
965 if (ev.xbutton.button != clickButton)
966 break;
968 if (wwin->icon_x!=x || wwin->icon_y!=y)
969 wwin->flags.icon_moved = 1;
971 XMoveWindow(dpy, icon->core->window, x, y);
973 wwin->icon_x = x;
974 wwin->icon_y = y;
975 #ifdef DEBUG
976 puts("End miniwindow move");
977 #endif
978 XUngrabPointer(dpy, CurrentTime);
980 if (wPreferences.auto_arrange_icons)
981 wArrangeIcons(wwin->screen_ptr, True);
982 return;