changed indentation to use spaces only
[wmaker-crm.git] / src / icon.c
blob92fec5b37d6e36cc4516df85b49a4a5f06301287
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"
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 wIconChangeIconWindow(WIcon *icon, Window new_window);
570 static void
571 cycleColor(void *data)
573 WIcon *icon = (WIcon*)data;
574 WScreen *scr = icon->core->screen_ptr;
575 XGCValues gcv;
577 icon->step--;
578 gcv.dash_offset = icon->step;
579 XChangeGC(dpy, scr->icon_select_gc, GCDashOffset, &gcv);
581 XDrawRectangle(dpy, icon->core->window, scr->icon_select_gc, 0, 0,
582 icon->core->width-1, icon->core->height-1);
583 icon->handlerID = WMAddTimerHandler(COLOR_CYCLE_DELAY, cycleColor, icon);
587 void
588 wIconSetHighlited(WIcon *icon, Bool flag)
590 if (icon->highlighted == flag) {
591 return;
594 icon->highlighted = flag;
595 wIconPaint(icon);
600 void
601 wIconSelect(WIcon *icon)
603 WScreen *scr = icon->core->screen_ptr;
604 icon->selected = !icon->selected;
606 if (icon->selected) {
607 icon->step = 0;
608 if (!wPreferences.dont_blink)
609 icon->handlerID = WMAddTimerHandler(10, cycleColor, icon);
610 else
611 XDrawRectangle(dpy, icon->core->window, scr->icon_select_gc, 0, 0,
612 icon->core->width-1, icon->core->height-1);
613 } else {
614 if (icon->handlerID) {
615 WMDeleteTimerHandler(icon->handlerID);
616 icon->handlerID = NULL;
618 XClearArea(dpy, icon->core->window, 0, 0, icon->core->width,
619 icon->core->height, True);
624 void
625 wIconUpdate(WIcon *icon)
627 WScreen *scr = icon->core->screen_ptr;
628 int title_height = WMFontHeight(scr->icon_title_font);
629 WWindow *wwin = icon->owner;
631 assert(scr->icon_tile!=NULL);
633 if (icon->pixmap!=None)
634 XFreePixmap(dpy, icon->pixmap);
635 icon->pixmap = None;
638 if (wwin && (WFLAGP(wwin, always_user_icon)
639 #ifdef NETWM_HINTS
640 || wwin->net_icon_image
641 #endif
643 goto user_icon;
645 /* use client specified icon window */
646 if (icon->icon_win!=None) {
647 XWindowAttributes attr;
648 int resize=0;
649 int width, height, depth;
650 int theight;
651 Pixmap pixmap;
653 getSize(icon->icon_win, &width, &height, &depth);
655 if (width > wPreferences.icon_size) {
656 resize = 1;
657 width = wPreferences.icon_size;
659 if (height > wPreferences.icon_size) {
660 resize = 1;
661 height = wPreferences.icon_size;
663 if (icon->show_title
664 && (height+title_height < wPreferences.icon_size)) {
665 pixmap = XCreatePixmap(dpy, scr->w_win, wPreferences.icon_size,
666 wPreferences.icon_size, scr->w_depth);
667 XSetClipMask(dpy, scr->copy_gc, None);
668 XCopyArea(dpy, scr->icon_tile_pixmap, pixmap, scr->copy_gc, 0, 0,
669 wPreferences.icon_size, wPreferences.icon_size, 0, 0);
670 drawIconTitle(scr, pixmap, title_height);
671 theight = title_height;
672 } else {
673 pixmap = None;
674 theight = 0;
675 XSetWindowBackgroundPixmap(dpy, icon->core->window,
676 scr->icon_tile_pixmap);
679 XSetWindowBorderWidth(dpy, icon->icon_win, 0);
680 XReparentWindow(dpy, icon->icon_win, icon->core->window,
681 (wPreferences.icon_size-width)/2,
682 theight+(wPreferences.icon_size-height-theight)/2);
683 if (resize)
684 XResizeWindow(dpy, icon->icon_win, width, height);
686 XMapWindow(dpy, icon->icon_win);
688 XAddToSaveSet(dpy, icon->icon_win);
690 icon->pixmap = pixmap;
692 if (XGetWindowAttributes(dpy, icon->icon_win, &attr)) {
693 if (attr.all_event_masks & ButtonPressMask) {
694 wHackedGrabButton(Button1, MOD_MASK, icon->core->window, True,
695 ButtonPressMask, GrabModeSync, GrabModeAsync,
696 None, wCursor[WCUR_ARROW]);
699 } else if (wwin && wwin->wm_hints
700 && (wwin->wm_hints->flags & IconPixmapHint)) {
701 int x, y;
702 unsigned int w, h;
703 Window jw;
704 int ji, dotitle;
705 unsigned int ju, d;
706 Pixmap pixmap;
708 if (!XGetGeometry(dpy, wwin->wm_hints->icon_pixmap, &jw,
709 &ji, &ji, &w, &h, &ju, &d)) {
710 icon->owner->wm_hints->flags &= ~IconPixmapHint;
711 goto user_icon;
714 pixmap = XCreatePixmap(dpy, icon->core->window, wPreferences.icon_size,
715 wPreferences.icon_size, scr->w_depth);
716 XSetClipMask(dpy, scr->copy_gc, None);
717 XCopyArea(dpy, scr->icon_tile_pixmap, pixmap, scr->copy_gc, 0, 0,
718 wPreferences.icon_size, wPreferences.icon_size, 0, 0);
720 if (w > wPreferences.icon_size)
721 w = wPreferences.icon_size;
722 x = (wPreferences.icon_size-w)/2;
724 if (icon->show_title && (title_height < wPreferences.icon_size)) {
725 drawIconTitle(scr, pixmap, title_height);
726 dotitle = 1;
728 if (h > wPreferences.icon_size - title_height - 2) {
729 h = wPreferences.icon_size - title_height - 2;
730 y = title_height + 1;
731 } else {
732 y = (wPreferences.icon_size-h-title_height)/2+title_height + 1;
734 } else {
735 dotitle = 0;
736 if (w > wPreferences.icon_size)
737 w = wPreferences.icon_size;
738 y = (wPreferences.icon_size-h)/2;
741 if (wwin->wm_hints->flags & IconMaskHint)
742 XSetClipMask(dpy, scr->copy_gc, wwin->wm_hints->icon_mask);
744 XSetClipOrigin(dpy, scr->copy_gc, x, y);
746 if (d != scr->w_depth) {
747 XSetForeground(dpy, scr->copy_gc, scr->black_pixel);
748 XSetBackground(dpy, scr->copy_gc, scr->white_pixel);
749 XCopyPlane(dpy, wwin->wm_hints->icon_pixmap, pixmap, scr->copy_gc,
750 0, 0, w, h, x, y, 1);
751 } else {
752 XCopyArea(dpy, wwin->wm_hints->icon_pixmap, pixmap, scr->copy_gc,
753 0, 0, w, h, x, y);
756 XSetClipOrigin(dpy, scr->copy_gc, 0, 0);
758 icon->pixmap = pixmap;
759 } else {
760 user_icon:
762 if (icon->image) {
763 icon->pixmap = makeIcon(scr, icon->image, icon->show_title,
764 icon->shadowed, icon->tile_type);
765 } else {
766 /* make default icons */
768 if (!scr->def_icon_pixmap) {
769 RImage *image = NULL;
770 char *path;
771 char *file;
773 file = wDefaultGetIconFile(scr, NULL, NULL, False);
774 if (file) {
775 path = FindImage(wPreferences.icon_path, file);
776 if (!path) {
777 wwarning(_("could not find default icon \"%s\""),file);
778 goto make_icons;
781 image = RLoadImage(scr->rcontext, path, 0);
782 if (!image) {
783 wwarning(_("could not load default icon \"%s\":%s"),
784 file, RMessageForError(RErrorCode));
786 wfree(path);
788 make_icons:
790 image = wIconValidateIconSize(scr, image);
791 scr->def_icon_pixmap = makeIcon(scr, image, False, False,
792 icon->tile_type);
793 scr->def_ticon_pixmap = makeIcon(scr, image, True, False,
794 icon->tile_type);
795 if (image)
796 RReleaseImage(image);
799 if (icon->show_title) {
800 XSetWindowBackgroundPixmap(dpy, icon->core->window,
801 scr->def_ticon_pixmap);
802 } else {
803 XSetWindowBackgroundPixmap(dpy, icon->core->window,
804 scr->def_icon_pixmap);
806 icon->pixmap = None;
809 if (icon->pixmap != None) {
810 XSetWindowBackgroundPixmap(dpy, icon->core->window, icon->pixmap);
812 XClearWindow(dpy, icon->core->window);
814 wIconPaint(icon);
819 void
820 wIconPaint(WIcon *icon)
822 WScreen *scr=icon->core->screen_ptr;
823 int x;
824 char *tmp;
826 if (icon->force_paint) {
827 icon->force_paint = 0;
828 wIconUpdate(icon);
829 return;
832 XClearWindow(dpy, icon->core->window);
834 /* draw the icon title */
835 if (icon->show_title && icon->icon_name!=NULL) {
836 int l;
837 int w;
839 tmp = ShrinkString(scr->icon_title_font, icon->icon_name,
840 wPreferences.icon_size-4);
841 w = WMWidthOfString(scr->icon_title_font, tmp, l=strlen(tmp));
843 if (w > icon->core->width - 4)
844 x = (icon->core->width - 4) - w;
845 else
846 x = (icon->core->width - w)/2;
848 WMDrawString(scr->wmscreen, icon->core->window, scr->icon_title_color,
849 scr->icon_title_font, x, 1, tmp, l);
850 wfree(tmp);
853 if (icon->selected)
854 XDrawRectangle(dpy, icon->core->window, scr->icon_select_gc, 0, 0,
855 icon->core->width-1, icon->core->height-1);
859 /******************************************************************/
861 static void
862 miniwindowExpose(WObjDescriptor *desc, XEvent *event)
864 wIconPaint(desc->parent);
868 static void
869 miniwindowDblClick(WObjDescriptor *desc, XEvent *event)
871 WIcon *icon = desc->parent;
873 assert(icon->owner!=NULL);
875 wDeiconifyWindow(icon->owner);
879 static void
880 miniwindowMouseDown(WObjDescriptor *desc, XEvent *event)
882 WIcon *icon = desc->parent;
883 WWindow *wwin = icon->owner;
884 XEvent ev;
885 int x=wwin->icon_x, y=wwin->icon_y;
886 int dx=event->xbutton.x, dy=event->xbutton.y;
887 int grabbed=0;
888 int clickButton=event->xbutton.button;
890 if (WCHECK_STATE(WSTATE_MODAL))
891 return;
893 if (IsDoubleClick(icon->core->screen_ptr, event)) {
894 miniwindowDblClick(desc, event);
895 return;
898 #ifdef DEBUG
899 puts("Moving miniwindow");
900 #endif
901 if (event->xbutton.button == Button1) {
902 if (event->xbutton.state & MOD_MASK)
903 wLowerFrame(icon->core);
904 else
905 wRaiseFrame(icon->core);
906 if (event->xbutton.state & ShiftMask) {
907 wIconSelect(icon);
908 wSelectWindow(icon->owner, !wwin->flags.selected);
910 } else if (event->xbutton.button == Button3) {
911 WObjDescriptor *desc;
913 OpenMiniwindowMenu(wwin, event->xbutton.x_root,
914 event->xbutton.y_root);
916 /* allow drag select of menu */
917 desc = &wwin->screen_ptr->window_menu->menu->descriptor;
918 event->xbutton.send_event = True;
919 (*desc->handle_mousedown)(desc, event);
921 return;
924 if (XGrabPointer(dpy, icon->core->window, False, ButtonMotionMask
925 |ButtonReleaseMask|ButtonPressMask, GrabModeAsync,
926 GrabModeAsync, None, None, CurrentTime) !=GrabSuccess) {
927 #ifdef DEBUG0
928 wwarning("pointer grab failed for icon move");
929 #endif
931 while(1) {
932 WMMaskEvent(dpy, PointerMotionMask|ButtonReleaseMask|ButtonPressMask
933 |ButtonMotionMask|ExposureMask, &ev);
934 switch (ev.type) {
935 case Expose:
936 WMHandleEvent(&ev);
937 break;
939 case MotionNotify:
940 if (!grabbed) {
941 if (abs(dx-ev.xmotion.x)>=MOVE_THRESHOLD
942 || abs(dy-ev.xmotion.y)>=MOVE_THRESHOLD) {
943 XChangeActivePointerGrab(dpy, ButtonMotionMask
944 |ButtonReleaseMask|ButtonPressMask,
945 wCursor[WCUR_MOVE], CurrentTime);
946 grabbed=1;
947 } else {
948 break;
951 x = ev.xmotion.x_root - dx;
952 y = ev.xmotion.y_root - dy;
953 XMoveWindow(dpy, icon->core->window, x, y);
954 break;
956 case ButtonPress:
957 break;
959 case ButtonRelease:
960 if (ev.xbutton.button != clickButton)
961 break;
963 if (wwin->icon_x!=x || wwin->icon_y!=y)
964 wwin->flags.icon_moved = 1;
966 XMoveWindow(dpy, icon->core->window, x, y);
968 wwin->icon_x = x;
969 wwin->icon_y = y;
970 #ifdef DEBUG
971 puts("End miniwindow move");
972 #endif
973 XUngrabPointer(dpy, CurrentTime);
975 if (wPreferences.auto_arrange_icons)
976 wArrangeIcons(wwin->screen_ptr, True);
977 return;