- Fixed text in info panel for multibyte (Seiichi SATO <ssato@sh.rim.or.jp>)
[wmaker-crm.git] / src / icon.c
blob80ec28d7731e9ad372b0f8124f2fdb75362f24a5
1 /* icon.c - window icon and dock and appicon parent
2 *
3 * Window Maker window manager
4 *
5 * Copyright (c) 1997-2002 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 icon->image = wDefaultGetImage(scr, wwin->wm_instance, wwin->wm_class);
171 file = wDefaultGetIconFile(scr, wwin->wm_instance, wwin->wm_class,
172 False);
173 if (file) {
174 icon->file = wstrdup(file);
177 wGetIconName(dpy, wwin->client_win, &icon->icon_name);
179 icon->tile_type = TILE_NORMAL;
181 wIconUpdate(icon);
183 XFlush(dpy);
185 WMAddNotificationObserver(appearanceObserver, icon,
186 WNIconAppearanceSettingsChanged, icon);
187 WMAddNotificationObserver(tileObserver, icon,
188 WNIconTileSettingsChanged, icon);
189 return icon;
193 WIcon*
194 wIconCreateWithIconFile(WScreen *scr, char *iconfile, int tile)
196 WIcon *icon;
197 unsigned long vmask = 0;
198 XSetWindowAttributes attribs;
200 icon = wmalloc(sizeof(WIcon));
201 memset(icon, 0, sizeof(WIcon));
202 icon->core = wCoreCreateTopLevel(scr, 0, 0, wPreferences.icon_size,
203 wPreferences.icon_size, 0);
204 if (wPreferences.use_saveunders) {
205 vmask = CWSaveUnder;
206 attribs.save_under = True;
208 /* a white border for selecting it */
209 vmask |= CWBorderPixel;
210 attribs.border_pixel = scr->white_pixel;
212 XChangeWindowAttributes(dpy, icon->core->window, vmask, &attribs);
214 /* will be overriden if this is a application icon */
215 icon->core->descriptor.handle_mousedown = miniwindowMouseDown;
216 icon->core->descriptor.handle_expose = miniwindowExpose;
217 icon->core->descriptor.parent_type = WCLASS_MINIWINDOW;
218 icon->core->descriptor.parent = icon;
220 icon->core->stacking = wmalloc(sizeof(WStacking));
221 icon->core->stacking->above = NULL;
222 icon->core->stacking->under = NULL;
223 icon->core->stacking->window_level = NORMAL_ICON_LEVEL;
224 icon->core->stacking->child_of = NULL;
226 if (iconfile) {
227 icon->image = RLoadImage(scr->rcontext, iconfile, 0);
228 if (!icon->image) {
229 wwarning(_("error loading image file \"%s\""), iconfile, RMessageForError(RErrorCode));
232 icon->image = wIconValidateIconSize(scr, icon->image);
234 icon->file = wstrdup(iconfile);
237 icon->tile_type = tile;
239 wIconUpdate(icon);
241 WMAddNotificationObserver(appearanceObserver, icon,
242 WNIconAppearanceSettingsChanged, icon);
243 WMAddNotificationObserver(tileObserver, icon,
244 WNIconTileSettingsChanged, icon);
246 return icon;
251 void
252 wIconDestroy(WIcon *icon)
254 WCoreWindow *core = icon->core;
255 WScreen *scr = core->screen_ptr;
257 WMRemoveNotificationObserver(icon);
259 if (icon->handlerID)
260 WMDeleteTimerHandler(icon->handlerID);
262 if (icon->icon_win) {
263 int x=0, y=0;
265 if (icon->owner) {
266 x = icon->owner->icon_x;
267 y = icon->owner->icon_y;
269 XUnmapWindow(dpy, icon->icon_win);
270 XReparentWindow(dpy, icon->icon_win, scr->root_win, x, y);
272 if (icon->icon_name)
273 XFree(icon->icon_name);
275 if (icon->pixmap)
276 XFreePixmap(dpy, icon->pixmap);
278 if (icon->file)
279 wfree(icon->file);
281 if (icon->image!=NULL)
282 RReleaseImage(icon->image);
284 wCoreDestroy(icon->core);
285 wfree(icon);
290 static void
291 drawIconTitle(WScreen *scr, Pixmap pixmap, int height)
293 XFillRectangle(dpy, pixmap, scr->icon_title_texture->normal_gc,
294 0, 0, wPreferences.icon_size, height+1);
295 XDrawLine(dpy, pixmap, scr->icon_title_texture->light_gc, 0, 0,
296 wPreferences.icon_size, 0);
297 XDrawLine(dpy, pixmap, scr->icon_title_texture->light_gc, 0, 0,
298 0, height+1);
299 XDrawLine(dpy, pixmap, scr->icon_title_texture->dim_gc,
300 wPreferences.icon_size-1, 0, wPreferences.icon_size-1, height+1);
304 static Pixmap
305 makeIcon(WScreen *scr, RImage *icon, int titled, int shadowed, int tileType)
307 RImage *tile;
308 Pixmap pixmap;
309 int x, y, sx, sy;
310 unsigned w, h;
311 int theight = WMFontHeight(scr->icon_title_font);
313 if (tileType == TILE_NORMAL)
314 tile = RCloneImage(scr->icon_tile);
315 else {
316 assert(scr->clip_tile);
317 tile = RCloneImage(scr->clip_tile);
319 if (icon) {
320 w = (icon->width > wPreferences.icon_size)
321 ? wPreferences.icon_size : icon->width;
322 x = (wPreferences.icon_size - w) / 2;
323 sx = (icon->width - w)/2;
325 if (!titled) {
326 h = (icon->height > wPreferences.icon_size)
327 ? wPreferences.icon_size : icon->height;
328 y = (wPreferences.icon_size - h) / 2;
329 sy = (icon->height - h)/2;
330 } else {
331 h = (icon->height+theight > wPreferences.icon_size
332 ? wPreferences.icon_size-theight : icon->height);
333 y = theight+((int)wPreferences.icon_size-theight-h)/2;
334 sy = (icon->height - h)/2;
336 RCombineArea(tile, icon, sx, sy, w, h, x, y);
339 if (shadowed) {
340 RColor color;
342 color.red = scr->icon_back_texture->light.red >> 8;
343 color.green = scr->icon_back_texture->light.green >> 8;
344 color.blue = scr->icon_back_texture->light.blue >> 8;
345 color.alpha = 150; /* about 60% */
346 RClearImage(tile, &color);
349 if (!RConvertImage(scr->rcontext, tile, &pixmap)) {
350 wwarning(_("error rendering image:%s"), RMessageForError(RErrorCode));
352 RReleaseImage(tile);
354 if (titled)
355 drawIconTitle(scr, pixmap, theight);
357 return pixmap;
361 void
362 wIconChangeTitle(WIcon *icon, char *new_title)
364 int changed;
366 changed = (new_title==NULL && icon->icon_name!=NULL)
367 || (new_title!=NULL && icon->icon_name==NULL);
369 if (icon->icon_name!=NULL)
370 XFree(icon->icon_name);
372 icon->icon_name = new_title;
374 if (changed)
375 icon->force_paint = 1;
376 wIconPaint(icon);
380 void
381 wIconChangeImage(WIcon *icon, RImage *new_image)
383 assert(icon != NULL);
385 if (icon->image)
386 RReleaseImage(icon->image);
388 icon->image = wIconValidateIconSize(icon->core->screen_ptr, new_image);
390 wIconUpdate(icon);
394 RImage*
395 wIconValidateIconSize(WScreen *scr, RImage *icon)
397 RImage *tmp;
398 int w, h;
400 if (!icon)
401 return NULL;
402 #ifndef DONT_SCALE_ICONS
403 if (wPreferences.icon_size != 64) {
404 w = wPreferences.icon_size * icon->width / 64;
405 h = wPreferences.icon_size * icon->height / 64;
407 tmp = RScaleImage(icon, w, h);
408 RReleaseImage(icon);
409 icon = tmp;
411 #endif
412 #if 0
413 if (icon->width > wPreferences.icon_size
414 || icon->height > wPreferences.icon_size) {
415 if (icon->width > icon->height) {
416 w = wPreferences.icon_size - 4;
417 h = w*icon->height/icon->width;
418 } else {
419 h = wPreferences.icon_size - 4;
420 w = h*icon->width/icon->height;
422 tmp = RScaleImage(icon, w, h);
423 RReleaseImage(icon);
424 icon = tmp;
426 #endif
428 return icon;
432 Bool
433 wIconChangeImageFile(WIcon *icon, char *file)
435 WScreen *scr = icon->core->screen_ptr;
436 RImage *image;
437 char *path;
438 int error = 0;
440 if (!file) {
441 wIconChangeImage(icon, NULL);
442 return True;
445 path = FindImage(wPreferences.icon_path, file);
447 if (path && (image = RLoadImage(scr->rcontext, path, 0))) {
448 wIconChangeImage(icon, image);
449 } else {
450 error = 1;
453 if (path)
454 wfree(path);
456 return !error;
461 static char*
462 getnameforicon(WWindow *wwin)
464 char *prefix, *suffix;
465 char *path;
466 int len;
468 if (wwin->wm_class && wwin->wm_instance) {
469 int len = strlen(wwin->wm_class)+strlen(wwin->wm_instance)+2;
470 suffix = wmalloc(len);
471 snprintf(suffix, len, "%s.%s", wwin->wm_instance, wwin->wm_class);
472 } else if (wwin->wm_class) {
473 int len = strlen(wwin->wm_class)+1;
474 suffix = wmalloc(len);
475 snprintf(suffix, len, "%s", wwin->wm_class);
476 } else if (wwin->wm_instance) {
477 int len = strlen(wwin->wm_instance)+1;
478 suffix = wmalloc(len);
479 snprintf(suffix, len, "%s", wwin->wm_instance);
480 } else {
481 return NULL;
484 prefix = wusergnusteppath();
485 len = strlen(prefix)+64+strlen(suffix);
486 path = wmalloc(len+1);
487 snprintf(path, len, "%s/.AppInfo", prefix);
489 if (access(path, F_OK)!=0) {
490 if (mkdir(path, S_IRUSR|S_IWUSR|S_IXUSR)) {
491 wsyserror(_("could not create directory %s"), path);
492 wfree(path);
493 wfree(suffix);
494 return NULL;
497 strcat(path, "/WindowMaker");
498 if (access(path, F_OK)!=0) {
499 if (mkdir(path, S_IRUSR|S_IWUSR|S_IXUSR)!=0) {
500 wsyserror(_("could not create directory %s"), path);
501 wfree(path);
502 wfree(suffix);
503 return NULL;
507 strcat(path, "/");
508 strcat(path, suffix);
509 strcat(path, ".xpm");
510 wfree(suffix);
512 return path;
517 * wIconStore--
518 * Stores the client supplied icon at ~/GNUstep/.AppInfo/WindowMaker
519 * and returns the path for that icon. Returns NULL if there is no
520 * client supplied icon or on failure.
522 * Side effects:
523 * New directories might be created.
525 char*
526 wIconStore(WIcon *icon)
528 char *path;
529 RImage *image;
530 WWindow *wwin = icon->owner;
532 if (!wwin || !wwin->wm_hints || !(wwin->wm_hints->flags & IconPixmapHint)
533 || wwin->wm_hints->icon_pixmap == None)
534 return NULL;
536 path = getnameforicon(wwin);
537 if (!path)
538 return NULL;
540 image = RCreateImageFromDrawable(icon->core->screen_ptr->rcontext,
541 wwin->wm_hints->icon_pixmap,
542 (wwin->wm_hints->flags & IconMaskHint)
543 ? wwin->wm_hints->icon_mask : None);
544 if (!image) {
545 wfree(path);
546 return NULL;
549 if (!RSaveImage(image, path, "XPM")) {
550 wfree(path);
551 path = NULL;
553 RReleaseImage(image);
555 return path;
560 void
561 wIconChangeIconWindow(WIcon *icon, Window new_window);
564 static void
565 cycleColor(void *data)
567 WIcon *icon = (WIcon*)data;
568 WScreen *scr = icon->core->screen_ptr;
569 XGCValues gcv;
571 icon->step--;
572 gcv.dash_offset = icon->step;
573 XChangeGC(dpy, scr->icon_select_gc, GCDashOffset, &gcv);
575 XDrawRectangle(dpy, icon->core->window, scr->icon_select_gc, 0, 0,
576 icon->core->width-1, icon->core->height-1);
577 icon->handlerID = WMAddTimerHandler(COLOR_CYCLE_DELAY, cycleColor, icon);
581 void
582 wIconSetHighlited(WIcon *icon, Bool flag)
584 if (icon->highlighted == flag) {
585 return;
588 icon->highlighted = flag;
589 wIconPaint(icon);
594 void
595 wIconSelect(WIcon *icon)
597 WScreen *scr = icon->core->screen_ptr;
598 icon->selected = !icon->selected;
600 if (icon->selected) {
601 icon->step = 0;
602 if (!wPreferences.dont_blink)
603 icon->handlerID = WMAddTimerHandler(10, cycleColor, icon);
604 else
605 XDrawRectangle(dpy, icon->core->window, scr->icon_select_gc, 0, 0,
606 icon->core->width-1, icon->core->height-1);
607 } else {
608 if (icon->handlerID) {
609 WMDeleteTimerHandler(icon->handlerID);
610 icon->handlerID = NULL;
612 XClearArea(dpy, icon->core->window, 0, 0, icon->core->width,
613 icon->core->height, True);
618 void
619 wIconUpdate(WIcon *icon)
621 WScreen *scr = icon->core->screen_ptr;
622 int title_height = WMFontHeight(scr->icon_title_font);
623 WWindow *wwin = icon->owner;
625 assert(scr->icon_tile!=NULL);
627 if (icon->pixmap!=None)
628 XFreePixmap(dpy, icon->pixmap);
629 icon->pixmap = None;
632 if (wwin && WFLAGP(wwin, always_user_icon))
633 goto user_icon;
635 /* use client specified icon window */
636 if (icon->icon_win!=None) {
637 XWindowAttributes attr;
638 int resize=0;
639 int width, height, depth;
640 int theight;
641 Pixmap pixmap;
643 getSize(icon->icon_win, &width, &height, &depth);
645 if (width > wPreferences.icon_size) {
646 resize = 1;
647 width = wPreferences.icon_size;
649 if (height > wPreferences.icon_size) {
650 resize = 1;
651 height = wPreferences.icon_size;
653 if (icon->show_title
654 && (height+title_height < wPreferences.icon_size)) {
655 pixmap = XCreatePixmap(dpy, scr->w_win, wPreferences.icon_size,
656 wPreferences.icon_size, scr->w_depth);
657 XSetClipMask(dpy, scr->copy_gc, None);
658 XCopyArea(dpy, scr->icon_tile_pixmap, pixmap, scr->copy_gc, 0, 0,
659 wPreferences.icon_size, wPreferences.icon_size, 0, 0);
660 drawIconTitle(scr, pixmap, title_height);
661 theight = title_height;
662 } else {
663 pixmap = None;
664 theight = 0;
665 XSetWindowBackgroundPixmap(dpy, icon->core->window,
666 scr->icon_tile_pixmap);
669 XSetWindowBorderWidth(dpy, icon->icon_win, 0);
670 XReparentWindow(dpy, icon->icon_win, icon->core->window,
671 (wPreferences.icon_size-width)/2,
672 theight+(wPreferences.icon_size-height-theight)/2);
673 if (resize)
674 XResizeWindow(dpy, icon->icon_win, width, height);
676 XMapWindow(dpy, icon->icon_win);
678 XAddToSaveSet(dpy, icon->icon_win);
680 icon->pixmap = pixmap;
682 if (XGetWindowAttributes(dpy, icon->icon_win, &attr)) {
683 if (attr.all_event_masks & ButtonPressMask) {
684 wHackedGrabButton(Button1, MOD_MASK, icon->core->window, True,
685 ButtonPressMask, GrabModeSync, GrabModeAsync,
686 None, wCursor[WCUR_ARROW]);
689 } else if (wwin && wwin->wm_hints
690 && (wwin->wm_hints->flags & IconPixmapHint)) {
691 int x, y;
692 unsigned int w, h;
693 Window jw;
694 int ji, dotitle;
695 unsigned int ju, d;
696 Pixmap pixmap;
698 if (!XGetGeometry(dpy, wwin->wm_hints->icon_pixmap, &jw,
699 &ji, &ji, &w, &h, &ju, &d)) {
700 icon->owner->wm_hints->flags &= ~IconPixmapHint;
701 goto user_icon;
704 pixmap = XCreatePixmap(dpy, icon->core->window, wPreferences.icon_size,
705 wPreferences.icon_size, scr->w_depth);
706 XSetClipMask(dpy, scr->copy_gc, None);
707 XCopyArea(dpy, scr->icon_tile_pixmap, pixmap, scr->copy_gc, 0, 0,
708 wPreferences.icon_size, wPreferences.icon_size, 0, 0);
710 if (w > wPreferences.icon_size)
711 w = wPreferences.icon_size;
712 x = (wPreferences.icon_size-w)/2;
714 if (icon->show_title && (title_height < wPreferences.icon_size)) {
715 drawIconTitle(scr, pixmap, title_height);
716 dotitle = 1;
718 if (h > wPreferences.icon_size - title_height - 2) {
719 h = wPreferences.icon_size - title_height - 2;
720 y = title_height + 1;
721 } else {
722 y = (wPreferences.icon_size-h-title_height)/2+title_height + 1;
724 } else {
725 dotitle = 0;
726 if (w > wPreferences.icon_size)
727 w = wPreferences.icon_size;
728 y = (wPreferences.icon_size-h)/2;
731 if (wwin->wm_hints->flags & IconMaskHint)
732 XSetClipMask(dpy, scr->copy_gc, wwin->wm_hints->icon_mask);
734 XSetClipOrigin(dpy, scr->copy_gc, x, y);
736 if (d != scr->w_depth) {
737 XSetForeground(dpy, scr->copy_gc, scr->black_pixel);
738 XSetBackground(dpy, scr->copy_gc, scr->white_pixel);
739 XCopyPlane(dpy, wwin->wm_hints->icon_pixmap, pixmap, scr->copy_gc,
740 0, 0, w, h, x, y, 1);
741 } else {
742 XCopyArea(dpy, wwin->wm_hints->icon_pixmap, pixmap, scr->copy_gc,
743 0, 0, w, h, x, y);
746 XSetClipOrigin(dpy, scr->copy_gc, 0, 0);
748 icon->pixmap = pixmap;
749 } else {
750 user_icon:
752 if (icon->image) {
753 icon->pixmap = makeIcon(scr, icon->image, icon->show_title,
754 icon->shadowed, icon->tile_type);
755 } else {
756 /* make default icons */
758 if (!scr->def_icon_pixmap) {
759 RImage *image = NULL;
760 char *path;
761 char *file;
763 file = wDefaultGetIconFile(scr, NULL, NULL, False);
764 if (file) {
765 path = FindImage(wPreferences.icon_path, file);
766 if (!path) {
767 wwarning(_("could not find default icon \"%s\""),file);
768 goto make_icons;
771 image = RLoadImage(scr->rcontext, path, 0);
772 if (!image) {
773 wwarning(_("could not load default icon \"%s\":%s"),
774 file, RMessageForError(RErrorCode));
776 wfree(path);
778 make_icons:
780 image = wIconValidateIconSize(scr, image);
781 scr->def_icon_pixmap = makeIcon(scr, image, False, False,
782 icon->tile_type);
783 scr->def_ticon_pixmap = makeIcon(scr, image, True, False,
784 icon->tile_type);
785 if (image)
786 RReleaseImage(image);
789 if (icon->show_title) {
790 XSetWindowBackgroundPixmap(dpy, icon->core->window,
791 scr->def_ticon_pixmap);
792 } else {
793 XSetWindowBackgroundPixmap(dpy, icon->core->window,
794 scr->def_icon_pixmap);
796 icon->pixmap = None;
799 if (icon->pixmap != None) {
800 XSetWindowBackgroundPixmap(dpy, icon->core->window, icon->pixmap);
802 XClearWindow(dpy, icon->core->window);
804 wIconPaint(icon);
809 void
810 wIconPaint(WIcon *icon)
812 WScreen *scr=icon->core->screen_ptr;
813 GC gc = scr->icon_title_gc;
814 int x;
815 char *tmp;
817 if (icon->force_paint) {
818 icon->force_paint = 0;
819 wIconUpdate(icon);
820 return;
823 XClearWindow(dpy, icon->core->window);
825 /* draw the icon title */
826 if (icon->show_title && icon->icon_name!=NULL) {
827 int l;
828 int w;
830 tmp = ShrinkString(scr->icon_title_font, icon->icon_name,
831 wPreferences.icon_size-4);
832 w = WMWidthOfString(scr->icon_title_font, tmp, l=strlen(tmp));
834 if (w > icon->core->width - 4)
835 x = (icon->core->width - 4) - w;
836 else
837 x = (icon->core->width - w)/2;
839 WMDrawString(scr->wmscreen, icon->core->window, gc,
840 scr->icon_title_font, x, 1, tmp, l);
841 wfree(tmp);
844 if (icon->selected)
845 XDrawRectangle(dpy, icon->core->window, scr->icon_select_gc, 0, 0,
846 icon->core->width-1, icon->core->height-1);
850 /******************************************************************/
852 static void
853 miniwindowExpose(WObjDescriptor *desc, XEvent *event)
855 wIconPaint(desc->parent);
859 static void
860 miniwindowDblClick(WObjDescriptor *desc, XEvent *event)
862 WIcon *icon = desc->parent;
864 assert(icon->owner!=NULL);
866 wDeiconifyWindow(icon->owner);
870 static void
871 miniwindowMouseDown(WObjDescriptor *desc, XEvent *event)
873 WIcon *icon = desc->parent;
874 WWindow *wwin = icon->owner;
875 XEvent ev;
876 int x=wwin->icon_x, y=wwin->icon_y;
877 int dx=event->xbutton.x, dy=event->xbutton.y;
878 int grabbed=0;
879 int clickButton=event->xbutton.button;
881 if (WCHECK_STATE(WSTATE_MODAL))
882 return;
884 if (IsDoubleClick(icon->core->screen_ptr, event)) {
885 miniwindowDblClick(desc, event);
886 return;
889 #ifdef DEBUG
890 puts("Moving miniwindow");
891 #endif
892 if (event->xbutton.button == Button1) {
893 if (event->xbutton.state & MOD_MASK)
894 wLowerFrame(icon->core);
895 else
896 wRaiseFrame(icon->core);
897 if (event->xbutton.state & ShiftMask) {
898 wIconSelect(icon);
899 wSelectWindow(icon->owner, !wwin->flags.selected);
901 } else if (event->xbutton.button == Button3) {
902 WObjDescriptor *desc;
904 OpenMiniwindowMenu(wwin, event->xbutton.x_root,
905 event->xbutton.y_root);
907 /* allow drag select of menu */
908 desc = &wwin->screen_ptr->window_menu->menu->descriptor;
909 event->xbutton.send_event = True;
910 (*desc->handle_mousedown)(desc, event);
912 return;
915 if (XGrabPointer(dpy, icon->core->window, False, ButtonMotionMask
916 |ButtonReleaseMask|ButtonPressMask, GrabModeAsync,
917 GrabModeAsync, None, None, CurrentTime) !=GrabSuccess) {
918 #ifdef DEBUG0
919 wwarning("pointer grab failed for icon move");
920 #endif
922 while(1) {
923 WMMaskEvent(dpy, PointerMotionMask|ButtonReleaseMask|ButtonPressMask
924 |ButtonMotionMask|ExposureMask, &ev);
925 switch (ev.type) {
926 case Expose:
927 WMHandleEvent(&ev);
928 break;
930 case MotionNotify:
931 if (!grabbed) {
932 if (abs(dx-ev.xmotion.x)>=MOVE_THRESHOLD
933 || abs(dy-ev.xmotion.y)>=MOVE_THRESHOLD) {
934 XChangeActivePointerGrab(dpy, ButtonMotionMask
935 |ButtonReleaseMask|ButtonPressMask,
936 wCursor[WCUR_MOVE], CurrentTime);
937 grabbed=1;
938 } else {
939 break;
942 x = ev.xmotion.x_root - dx;
943 y = ev.xmotion.y_root - dy;
944 XMoveWindow(dpy, icon->core->window, x, y);
945 break;
947 case ButtonPress:
948 break;
950 case ButtonRelease:
951 if (ev.xbutton.button != clickButton)
952 break;
954 if (wwin->icon_x!=x || wwin->icon_y!=y)
955 wwin->flags.icon_moved = 1;
957 XMoveWindow(dpy, icon->core->window, x, y);
959 wwin->icon_x = x;
960 wwin->icon_y = y;
961 #ifdef DEBUG
962 puts("End miniwindow move");
963 #endif
964 XUngrabPointer(dpy, CurrentTime);
966 if (wPreferences.auto_arrange_icons)
967 wArrangeIcons(wwin->screen_ptr, True);
968 return;