wmaker: Fix compiler warnings about pointer <--> integer conversion
[wmaker-crm.git] / src / icon.c
blob3ff44ac567c56330ecccd733300a741353c88797
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 <stdint.h>
30 #include <string.h>
31 #include <unistd.h>
32 #include <ctype.h>
33 #include <wraster.h>
34 #include <sys/stat.h>
36 #include "WindowMaker.h"
37 #include "wcore.h"
38 #include "texture.h"
39 #include "window.h"
40 #include "icon.h"
41 #include "actions.h"
42 #include "funcs.h"
43 #include "stacking.h"
44 #include "application.h"
45 #include "defaults.h"
46 #include "appicon.h"
47 #include "wmspec.h"
49 /**** Global varianebles ****/
50 extern WPreferences wPreferences;
52 #define MOD_MASK wPreferences.modifier_mask
54 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);
62 /****** Notification Observers ******/
64 static void
65 appearanceObserver(void *self, WMNotification *notif)
67 WIcon *icon = (WIcon*)self;
68 int flags = (int)(uintptr_t)WMGetNotificationClientData(notif);
70 if (flags & WTextureSettings) {
71 icon->force_paint = 1;
73 if (flags & WFontSettings) {
74 icon->force_paint = 1;
77 if (flags & WColorSettings) {
81 wIconPaint(icon);
83 /* so that the appicon expose handlers will paint the appicon specific
84 * stuff */
85 XClearArea(dpy, icon->core->window, 0, 0, icon->core->width,
86 icon->core->height, True);
90 static void
91 tileObserver(void *self, WMNotification *notif)
93 WIcon *icon = (WIcon*)self;
95 icon->force_paint = 1;
96 wIconPaint(icon);
98 XClearArea(dpy, icon->core->window, 0, 0, 1, 1, True);
101 /************************************/
105 INLINE static void
106 getSize(Drawable d, unsigned int *w, unsigned int *h, unsigned int *dep)
108 Window rjunk;
109 int xjunk, yjunk;
110 unsigned int bjunk;
112 XGetGeometry(dpy, d, &rjunk, &xjunk, &yjunk, w, h, &bjunk, dep);
116 WIcon*
117 wIconCreate(WWindow *wwin)
119 WScreen *scr=wwin->screen_ptr;
120 WIcon *icon;
121 char *file;
122 unsigned long vmask = 0;
123 XSetWindowAttributes attribs;
125 icon = wmalloc(sizeof(WIcon));
126 memset(icon, 0, sizeof(WIcon));
127 icon->core = wCoreCreateTopLevel(scr, wwin->icon_x, wwin->icon_y,
128 wPreferences.icon_size,
129 wPreferences.icon_size, 0);
131 if (wPreferences.use_saveunders) {
132 vmask |= CWSaveUnder;
133 attribs.save_under = True;
135 /* a white border for selecting it */
136 vmask |= CWBorderPixel;
137 attribs.border_pixel = scr->white_pixel;
139 XChangeWindowAttributes(dpy, icon->core->window, vmask, &attribs);
142 /* will be overriden if this is an application icon */
143 icon->core->descriptor.handle_mousedown = miniwindowMouseDown;
144 icon->core->descriptor.handle_expose = miniwindowExpose;
145 icon->core->descriptor.parent_type = WCLASS_MINIWINDOW;
146 icon->core->descriptor.parent = icon;
148 icon->core->stacking = wmalloc(sizeof(WStacking));
149 icon->core->stacking->above = NULL;
150 icon->core->stacking->under = NULL;
151 icon->core->stacking->window_level = NORMAL_ICON_LEVEL;
152 icon->core->stacking->child_of = NULL;
154 icon->owner = wwin;
155 if (wwin->wm_hints && (wwin->wm_hints->flags & IconWindowHint)) {
156 if (wwin->client_win == wwin->main_window) {
157 WApplication *wapp;
158 /* do not let miniwindow steal app-icon's icon window */
159 wapp = wApplicationOf(wwin->client_win);
160 if (!wapp || wapp->app_icon==NULL)
161 icon->icon_win = wwin->wm_hints->icon_window;
162 } else {
163 icon->icon_win = wwin->wm_hints->icon_window;
166 #ifdef NO_MINIWINDOW_TITLES
167 icon->show_title = 0;
168 #else
169 icon->show_title = 1;
170 #endif
171 #ifdef NETWM_HINTS
172 if (!icon->image && !WFLAGP(wwin, always_user_icon))
173 icon->image = RRetainImage(wwin->net_icon_image);
174 if (!icon->image)
175 #endif
176 icon->image = wDefaultGetImage(scr, wwin->wm_instance, wwin->wm_class);
178 file = wDefaultGetIconFile(scr, wwin->wm_instance, wwin->wm_class,
179 False);
180 if (file) {
181 icon->file = wstrdup(file);
184 icon->icon_name = wNETWMGetIconName(wwin->client_win);
185 if (icon->icon_name)
186 wwin->flags.net_has_icon_title= 1;
187 else
188 wGetIconName(dpy, wwin->client_win, &icon->icon_name);
190 icon->tile_type = TILE_NORMAL;
192 wIconUpdate(icon);
194 XFlush(dpy);
196 WMAddNotificationObserver(appearanceObserver, icon,
197 WNIconAppearanceSettingsChanged, icon);
198 WMAddNotificationObserver(tileObserver, icon,
199 WNIconTileSettingsChanged, icon);
200 return icon;
204 WIcon*
205 wIconCreateWithIconFile(WScreen *scr, char *iconfile, int tile)
207 WIcon *icon;
208 unsigned long vmask = 0;
209 XSetWindowAttributes attribs;
211 icon = wmalloc(sizeof(WIcon));
212 memset(icon, 0, sizeof(WIcon));
213 icon->core = wCoreCreateTopLevel(scr, 0, 0, wPreferences.icon_size,
214 wPreferences.icon_size, 0);
215 if (wPreferences.use_saveunders) {
216 vmask = CWSaveUnder;
217 attribs.save_under = True;
219 /* a white border for selecting it */
220 vmask |= CWBorderPixel;
221 attribs.border_pixel = scr->white_pixel;
223 XChangeWindowAttributes(dpy, icon->core->window, vmask, &attribs);
225 /* will be overriden if this is a application icon */
226 icon->core->descriptor.handle_mousedown = miniwindowMouseDown;
227 icon->core->descriptor.handle_expose = miniwindowExpose;
228 icon->core->descriptor.parent_type = WCLASS_MINIWINDOW;
229 icon->core->descriptor.parent = icon;
231 icon->core->stacking = wmalloc(sizeof(WStacking));
232 icon->core->stacking->above = NULL;
233 icon->core->stacking->under = NULL;
234 icon->core->stacking->window_level = NORMAL_ICON_LEVEL;
235 icon->core->stacking->child_of = NULL;
237 if (iconfile) {
238 icon->image = RLoadImage(scr->rcontext, iconfile, 0);
239 if (!icon->image) {
240 wwarning(_("error loading image file \"%s\""), iconfile, RMessageForError(RErrorCode));
243 icon->image = wIconValidateIconSize(scr, icon->image);
245 icon->file = wstrdup(iconfile);
248 icon->tile_type = tile;
250 wIconUpdate(icon);
252 WMAddNotificationObserver(appearanceObserver, icon,
253 WNIconAppearanceSettingsChanged, icon);
254 WMAddNotificationObserver(tileObserver, icon,
255 WNIconTileSettingsChanged, icon);
257 return icon;
262 void
263 wIconDestroy(WIcon *icon)
265 WCoreWindow *core = icon->core;
266 WScreen *scr = core->screen_ptr;
268 WMRemoveNotificationObserver(icon);
270 if (icon->handlerID)
271 WMDeleteTimerHandler(icon->handlerID);
273 if (icon->icon_win) {
274 int x=0, y=0;
276 if (icon->owner) {
277 x = icon->owner->icon_x;
278 y = icon->owner->icon_y;
280 XUnmapWindow(dpy, icon->icon_win);
281 XReparentWindow(dpy, icon->icon_win, scr->root_win, x, y);
283 if (icon->icon_name)
284 XFree(icon->icon_name);
286 if (icon->pixmap)
287 XFreePixmap(dpy, icon->pixmap);
289 if (icon->file)
290 wfree(icon->file);
292 if (icon->image!=NULL)
293 RReleaseImage(icon->image);
295 wCoreDestroy(icon->core);
296 wfree(icon);
301 static void
302 drawIconTitle(WScreen *scr, Pixmap pixmap, int height)
304 XFillRectangle(dpy, pixmap, scr->icon_title_texture->normal_gc,
305 0, 0, wPreferences.icon_size, height+1);
306 XDrawLine(dpy, pixmap, scr->icon_title_texture->light_gc, 0, 0,
307 wPreferences.icon_size, 0);
308 XDrawLine(dpy, pixmap, scr->icon_title_texture->light_gc, 0, 0,
309 0, height+1);
310 XDrawLine(dpy, pixmap, scr->icon_title_texture->dim_gc,
311 wPreferences.icon_size-1, 0, wPreferences.icon_size-1, height+1);
315 static Pixmap
316 makeIcon(WScreen *scr, RImage *icon, int titled, int shadowed, int tileType)
318 RImage *tile;
319 Pixmap pixmap;
320 int x, y, sx, sy;
321 unsigned w, h;
322 int theight = WMFontHeight(scr->icon_title_font);
324 if (tileType == TILE_NORMAL)
325 tile = RCloneImage(scr->icon_tile);
326 else {
327 assert(scr->clip_tile);
328 tile = RCloneImage(scr->clip_tile);
330 if (icon) {
331 w = (icon->width > wPreferences.icon_size)
332 ? wPreferences.icon_size : icon->width;
333 x = (wPreferences.icon_size - w) / 2;
334 sx = (icon->width - w)/2;
336 if (!titled) {
337 h = (icon->height > wPreferences.icon_size)
338 ? wPreferences.icon_size : icon->height;
339 y = (wPreferences.icon_size - h) / 2;
340 sy = (icon->height - h)/2;
341 } else {
342 h = (icon->height+theight > wPreferences.icon_size
343 ? wPreferences.icon_size-theight : icon->height);
344 y = theight+((int)wPreferences.icon_size-theight-h)/2;
345 sy = (icon->height - h)/2;
347 RCombineArea(tile, icon, sx, sy, w, h, x, y);
350 if (shadowed) {
351 RColor color;
353 color.red = scr->icon_back_texture->light.red >> 8;
354 color.green = scr->icon_back_texture->light.green >> 8;
355 color.blue = scr->icon_back_texture->light.blue >> 8;
356 color.alpha = 150; /* about 60% */
357 RClearImage(tile, &color);
360 if (!RConvertImage(scr->rcontext, tile, &pixmap)) {
361 wwarning(_("error rendering image:%s"), RMessageForError(RErrorCode));
363 RReleaseImage(tile);
365 if (titled)
366 drawIconTitle(scr, pixmap, theight);
368 return pixmap;
372 void
373 wIconChangeTitle(WIcon *icon, char *new_title)
375 int changed;
377 changed = (new_title==NULL && icon->icon_name!=NULL)
378 || (new_title!=NULL && icon->icon_name==NULL);
380 if (icon->icon_name!=NULL)
381 XFree(icon->icon_name);
383 icon->icon_name = new_title;
385 if (changed)
386 icon->force_paint = 1;
387 wIconPaint(icon);
391 void
392 wIconChangeImage(WIcon *icon, RImage *new_image)
394 assert(icon != NULL);
396 if (icon->image)
397 RReleaseImage(icon->image);
399 icon->image = new_image;
401 wIconUpdate(icon);
405 RImage*
406 wIconValidateIconSize(WScreen *scr, RImage *icon)
408 RImage *tmp;
409 int w, h;
411 if (!icon)
412 return NULL;
413 #ifndef DONT_SCALE_ICONS
414 if (wPreferences.icon_size != 64) {
415 w = wPreferences.icon_size * icon->width / 64;
416 h = wPreferences.icon_size * icon->height / 64;
418 tmp = RScaleImage(icon, w, h);
419 RReleaseImage(icon);
420 icon = tmp;
422 #endif
423 #if 0
424 if (icon->width > wPreferences.icon_size
425 || icon->height > wPreferences.icon_size) {
426 if (icon->width > icon->height) {
427 w = wPreferences.icon_size - 4;
428 h = w*icon->height/icon->width;
429 } else {
430 h = wPreferences.icon_size - 4;
431 w = h*icon->width/icon->height;
433 tmp = RScaleImage(icon, w, h);
434 RReleaseImage(icon);
435 icon = tmp;
437 #endif
439 return icon;
443 Bool
444 wIconChangeImageFile(WIcon *icon, char *file)
446 WScreen *scr = icon->core->screen_ptr;
447 RImage *image;
448 char *path;
449 int error = 0;
451 if (!file) {
452 wIconChangeImage(icon, NULL);
453 return True;
456 path = FindImage(wPreferences.icon_path, file);
458 if (path && (image = RLoadImage(scr->rcontext, path, 0))) {
459 image = wIconValidateIconSize(icon->core->screen_ptr, image);
461 wIconChangeImage(icon, image);
462 } else {
463 error = 1;
466 if (path)
467 wfree(path);
469 return !error;
474 static char*
475 getnameforicon(WWindow *wwin)
477 char *prefix, *suffix;
478 char *path;
479 int len;
481 if (wwin->wm_class && wwin->wm_instance) {
482 int len = strlen(wwin->wm_class)+strlen(wwin->wm_instance)+2;
483 suffix = wmalloc(len);
484 snprintf(suffix, len, "%s.%s", wwin->wm_instance, wwin->wm_class);
485 } else if (wwin->wm_class) {
486 int len = strlen(wwin->wm_class)+1;
487 suffix = wmalloc(len);
488 snprintf(suffix, len, "%s", wwin->wm_class);
489 } else if (wwin->wm_instance) {
490 int len = strlen(wwin->wm_instance)+1;
491 suffix = wmalloc(len);
492 snprintf(suffix, len, "%s", wwin->wm_instance);
493 } else {
494 return NULL;
497 prefix = wusergnusteppath();
498 len = strlen(prefix)+64+strlen(suffix);
499 path = wmalloc(len+1);
500 snprintf(path, len, "%s/Library/WindowMaker", prefix);
502 if (access(path, F_OK)!=0) {
503 if (mkdir(path, S_IRUSR|S_IWUSR|S_IXUSR)) {
504 wsyserror(_("could not create directory %s"), path);
505 wfree(path);
506 wfree(suffix);
507 return NULL;
510 strcat(path, "/CachedPixmaps");
511 if (access(path, F_OK)!=0) {
512 if (mkdir(path, S_IRUSR|S_IWUSR|S_IXUSR)!=0) {
513 wsyserror(_("could not create directory %s"), path);
514 wfree(path);
515 wfree(suffix);
516 return NULL;
520 strcat(path, "/");
521 strcat(path, suffix);
522 strcat(path, ".xpm");
523 wfree(suffix);
525 return path;
530 * wIconStore--
531 * Stores the client supplied icon at ~/GNUstep/Library/WindowMaker/CachedPixmaps
532 * and returns the path for that icon. Returns NULL if there is no
533 * client supplied icon or on failure.
535 * Side effects:
536 * New directories might be created.
538 char*
539 wIconStore(WIcon *icon)
541 char *path;
542 RImage *image;
543 WWindow *wwin = icon->owner;
545 if (!wwin || !wwin->wm_hints || !(wwin->wm_hints->flags & IconPixmapHint)
546 || wwin->wm_hints->icon_pixmap == None)
547 return NULL;
549 path = getnameforicon(wwin);
550 if (!path)
551 return NULL;
553 image = RCreateImageFromDrawable(icon->core->screen_ptr->rcontext,
554 wwin->wm_hints->icon_pixmap,
555 (wwin->wm_hints->flags & IconMaskHint)
556 ? wwin->wm_hints->icon_mask : None);
557 if (!image) {
558 wfree(path);
559 return NULL;
562 if (!RSaveImage(image, path, "XPM")) {
563 wfree(path);
564 path = NULL;
566 RReleaseImage(image);
568 return path;
573 void wIconChangeIconWindow(WIcon *icon, Window new_window);
576 static void
577 cycleColor(void *data)
579 WIcon *icon = (WIcon*)data;
580 WScreen *scr = icon->core->screen_ptr;
581 XGCValues gcv;
583 icon->step--;
584 gcv.dash_offset = icon->step;
585 XChangeGC(dpy, scr->icon_select_gc, GCDashOffset, &gcv);
587 XDrawRectangle(dpy, icon->core->window, scr->icon_select_gc, 0, 0,
588 icon->core->width-1, icon->core->height-1);
589 icon->handlerID = WMAddTimerHandler(COLOR_CYCLE_DELAY, cycleColor, icon);
593 void
594 wIconSetHighlited(WIcon *icon, Bool flag)
596 if (icon->highlighted == flag) {
597 return;
600 icon->highlighted = flag;
601 wIconPaint(icon);
606 void
607 wIconSelect(WIcon *icon)
609 WScreen *scr = icon->core->screen_ptr;
610 icon->selected = !icon->selected;
612 if (icon->selected) {
613 icon->step = 0;
614 if (!wPreferences.dont_blink)
615 icon->handlerID = WMAddTimerHandler(10, cycleColor, icon);
616 else
617 XDrawRectangle(dpy, icon->core->window, scr->icon_select_gc, 0, 0,
618 icon->core->width-1, icon->core->height-1);
619 } else {
620 if (icon->handlerID) {
621 WMDeleteTimerHandler(icon->handlerID);
622 icon->handlerID = NULL;
624 XClearArea(dpy, icon->core->window, 0, 0, icon->core->width,
625 icon->core->height, True);
630 void
631 wIconUpdate(WIcon *icon)
633 WScreen *scr = icon->core->screen_ptr;
634 int title_height = WMFontHeight(scr->icon_title_font);
635 WWindow *wwin = icon->owner;
637 assert(scr->icon_tile!=NULL);
639 if (icon->pixmap!=None)
640 XFreePixmap(dpy, icon->pixmap);
641 icon->pixmap = None;
644 if (wwin && (WFLAGP(wwin, always_user_icon)
645 #ifdef NETWM_HINTS
646 || wwin->net_icon_image
647 #endif
649 goto user_icon;
651 /* use client specified icon window */
652 if (icon->icon_win!=None) {
653 XWindowAttributes attr;
654 int resize=0;
655 unsigned int width, height, depth;
656 int theight;
657 Pixmap pixmap;
659 getSize(icon->icon_win, &width, &height, &depth);
661 if (width > wPreferences.icon_size) {
662 resize = 1;
663 width = wPreferences.icon_size;
665 if (height > wPreferences.icon_size) {
666 resize = 1;
667 height = wPreferences.icon_size;
669 if (icon->show_title
670 && (height+title_height < wPreferences.icon_size)) {
671 pixmap = XCreatePixmap(dpy, scr->w_win, wPreferences.icon_size,
672 wPreferences.icon_size, scr->w_depth);
673 XSetClipMask(dpy, scr->copy_gc, None);
674 XCopyArea(dpy, scr->icon_tile_pixmap, pixmap, scr->copy_gc, 0, 0,
675 wPreferences.icon_size, wPreferences.icon_size, 0, 0);
676 drawIconTitle(scr, pixmap, title_height);
677 theight = title_height;
678 } else {
679 pixmap = None;
680 theight = 0;
681 XSetWindowBackgroundPixmap(dpy, icon->core->window,
682 scr->icon_tile_pixmap);
685 XSetWindowBorderWidth(dpy, icon->icon_win, 0);
686 XReparentWindow(dpy, icon->icon_win, icon->core->window,
687 (wPreferences.icon_size-width)/2,
688 theight+(wPreferences.icon_size-height-theight)/2);
689 if (resize)
690 XResizeWindow(dpy, icon->icon_win, width, height);
692 XMapWindow(dpy, icon->icon_win);
694 XAddToSaveSet(dpy, icon->icon_win);
696 icon->pixmap = pixmap;
698 if (XGetWindowAttributes(dpy, icon->icon_win, &attr)) {
699 if (attr.all_event_masks & ButtonPressMask) {
700 wHackedGrabButton(Button1, MOD_MASK, icon->core->window, True,
701 ButtonPressMask, GrabModeSync, GrabModeAsync,
702 None, wCursor[WCUR_ARROW]);
705 } else if (wwin && wwin->wm_hints
706 && (wwin->wm_hints->flags & IconPixmapHint)) {
707 int x, y;
708 unsigned int w, h;
709 Window jw;
710 int ji, dotitle;
711 unsigned int ju, d;
712 Pixmap pixmap;
714 if (!XGetGeometry(dpy, wwin->wm_hints->icon_pixmap, &jw,
715 &ji, &ji, &w, &h, &ju, &d)) {
716 icon->owner->wm_hints->flags &= ~IconPixmapHint;
717 goto user_icon;
720 pixmap = XCreatePixmap(dpy, icon->core->window, wPreferences.icon_size,
721 wPreferences.icon_size, scr->w_depth);
722 XSetClipMask(dpy, scr->copy_gc, None);
723 XCopyArea(dpy, scr->icon_tile_pixmap, pixmap, scr->copy_gc, 0, 0,
724 wPreferences.icon_size, wPreferences.icon_size, 0, 0);
726 if (w > wPreferences.icon_size)
727 w = wPreferences.icon_size;
728 x = (wPreferences.icon_size-w)/2;
730 if (icon->show_title && (title_height < wPreferences.icon_size)) {
731 drawIconTitle(scr, pixmap, title_height);
732 dotitle = 1;
734 if (h > wPreferences.icon_size - title_height - 2) {
735 h = wPreferences.icon_size - title_height - 2;
736 y = title_height + 1;
737 } else {
738 y = (wPreferences.icon_size-h-title_height)/2+title_height + 1;
740 } else {
741 dotitle = 0;
742 if (w > wPreferences.icon_size)
743 w = wPreferences.icon_size;
744 y = (wPreferences.icon_size-h)/2;
747 if (wwin->wm_hints->flags & IconMaskHint)
748 XSetClipMask(dpy, scr->copy_gc, wwin->wm_hints->icon_mask);
750 XSetClipOrigin(dpy, scr->copy_gc, x, y);
752 if (d != scr->w_depth) {
753 XSetForeground(dpy, scr->copy_gc, scr->black_pixel);
754 XSetBackground(dpy, scr->copy_gc, scr->white_pixel);
755 XCopyPlane(dpy, wwin->wm_hints->icon_pixmap, pixmap, scr->copy_gc,
756 0, 0, w, h, x, y, 1);
757 } else {
758 XCopyArea(dpy, wwin->wm_hints->icon_pixmap, pixmap, scr->copy_gc,
759 0, 0, w, h, x, y);
762 XSetClipOrigin(dpy, scr->copy_gc, 0, 0);
764 icon->pixmap = pixmap;
765 } else {
766 user_icon:
768 if (icon->image) {
769 icon->pixmap = makeIcon(scr, icon->image, icon->show_title,
770 icon->shadowed, icon->tile_type);
771 } else {
772 /* make default icons */
774 if (!scr->def_icon_pixmap) {
775 RImage *image = NULL;
776 char *path;
777 char *file;
779 file = wDefaultGetIconFile(scr, NULL, NULL, False);
780 if (file) {
781 path = FindImage(wPreferences.icon_path, file);
782 if (!path) {
783 wwarning(_("could not find default icon \"%s\""),file);
784 goto make_icons;
787 image = RLoadImage(scr->rcontext, path, 0);
788 if (!image) {
789 wwarning(_("could not load default icon \"%s\":%s"),
790 file, RMessageForError(RErrorCode));
792 wfree(path);
794 make_icons:
796 image = wIconValidateIconSize(scr, image);
797 scr->def_icon_pixmap = makeIcon(scr, image, False, False,
798 icon->tile_type);
799 scr->def_ticon_pixmap = makeIcon(scr, image, True, False,
800 icon->tile_type);
801 if (image)
802 RReleaseImage(image);
805 if (icon->show_title) {
806 XSetWindowBackgroundPixmap(dpy, icon->core->window,
807 scr->def_ticon_pixmap);
808 } else {
809 XSetWindowBackgroundPixmap(dpy, icon->core->window,
810 scr->def_icon_pixmap);
812 icon->pixmap = None;
815 if (icon->pixmap != None) {
816 XSetWindowBackgroundPixmap(dpy, icon->core->window, icon->pixmap);
818 XClearWindow(dpy, icon->core->window);
820 wIconPaint(icon);
825 void
826 wIconPaint(WIcon *icon)
828 WScreen *scr=icon->core->screen_ptr;
829 int x;
830 char *tmp;
832 if (icon->force_paint) {
833 icon->force_paint = 0;
834 wIconUpdate(icon);
835 return;
838 XClearWindow(dpy, icon->core->window);
840 /* draw the icon title */
841 if (icon->show_title && icon->icon_name!=NULL) {
842 int l;
843 int w;
845 tmp = ShrinkString(scr->icon_title_font, icon->icon_name,
846 wPreferences.icon_size-4);
847 w = WMWidthOfString(scr->icon_title_font, tmp, l=strlen(tmp));
849 if (w > icon->core->width - 4)
850 x = (icon->core->width - 4) - w;
851 else
852 x = (icon->core->width - w)/2;
854 WMDrawString(scr->wmscreen, icon->core->window, scr->icon_title_color,
855 scr->icon_title_font, x, 1, tmp, l);
856 wfree(tmp);
859 if (icon->selected)
860 XDrawRectangle(dpy, icon->core->window, scr->icon_select_gc, 0, 0,
861 icon->core->width-1, icon->core->height-1);
865 /******************************************************************/
867 static void
868 miniwindowExpose(WObjDescriptor *desc, XEvent *event)
870 wIconPaint(desc->parent);
874 static void
875 miniwindowDblClick(WObjDescriptor *desc, XEvent *event)
877 WIcon *icon = desc->parent;
879 assert(icon->owner!=NULL);
881 wDeiconifyWindow(icon->owner);
885 static void
886 miniwindowMouseDown(WObjDescriptor *desc, XEvent *event)
888 WIcon *icon = desc->parent;
889 WWindow *wwin = icon->owner;
890 XEvent ev;
891 int x=wwin->icon_x, y=wwin->icon_y;
892 int dx=event->xbutton.x, dy=event->xbutton.y;
893 int grabbed=0;
894 int clickButton=event->xbutton.button;
896 if (WCHECK_STATE(WSTATE_MODAL))
897 return;
899 if (IsDoubleClick(icon->core->screen_ptr, event)) {
900 miniwindowDblClick(desc, event);
901 return;
904 #ifdef DEBUG
905 puts("Moving miniwindow");
906 #endif
907 if (event->xbutton.button == Button1) {
908 if (event->xbutton.state & MOD_MASK)
909 wLowerFrame(icon->core);
910 else
911 wRaiseFrame(icon->core);
912 if (event->xbutton.state & ShiftMask) {
913 wIconSelect(icon);
914 wSelectWindow(icon->owner, !wwin->flags.selected);
916 } else if (event->xbutton.button == Button3) {
917 WObjDescriptor *desc;
919 OpenMiniwindowMenu(wwin, event->xbutton.x_root,
920 event->xbutton.y_root);
922 /* allow drag select of menu */
923 desc = &wwin->screen_ptr->window_menu->menu->descriptor;
924 event->xbutton.send_event = True;
925 (*desc->handle_mousedown)(desc, event);
927 return;
930 if (XGrabPointer(dpy, icon->core->window, False, ButtonMotionMask
931 |ButtonReleaseMask|ButtonPressMask, GrabModeAsync,
932 GrabModeAsync, None, None, CurrentTime) !=GrabSuccess) {
933 #ifdef DEBUG0
934 wwarning("pointer grab failed for icon move");
935 #endif
937 while(1) {
938 WMMaskEvent(dpy, PointerMotionMask|ButtonReleaseMask|ButtonPressMask
939 |ButtonMotionMask|ExposureMask, &ev);
940 switch (ev.type) {
941 case Expose:
942 WMHandleEvent(&ev);
943 break;
945 case MotionNotify:
946 if (!grabbed) {
947 if (abs(dx-ev.xmotion.x)>=MOVE_THRESHOLD
948 || abs(dy-ev.xmotion.y)>=MOVE_THRESHOLD) {
949 XChangeActivePointerGrab(dpy, ButtonMotionMask
950 |ButtonReleaseMask|ButtonPressMask,
951 wCursor[WCUR_MOVE], CurrentTime);
952 grabbed=1;
953 } else {
954 break;
957 x = ev.xmotion.x_root - dx;
958 y = ev.xmotion.y_root - dy;
959 XMoveWindow(dpy, icon->core->window, x, y);
960 break;
962 case ButtonPress:
963 break;
965 case ButtonRelease:
966 if (ev.xbutton.button != clickButton)
967 break;
969 if (wwin->icon_x!=x || wwin->icon_y!=y)
970 wwin->flags.icon_moved = 1;
972 XMoveWindow(dpy, icon->core->window, x, y);
974 wwin->icon_x = x;
975 wwin->icon_y = y;
976 #ifdef DEBUG
977 puts("End miniwindow move");
978 #endif
979 XUngrabPointer(dpy, CurrentTime);
981 if (wPreferences.auto_arrange_icons)
982 wArrangeIcons(wwin->screen_ptr, True);
983 return;