fixed cosmetic bug in geom. dpy window for 8bpp
[wmaker-crm.git] / src / icon.c
blob3a1677030f2ee02a8925ebac732c6156b9ab7022
1 /* icon.c - window icon and dock and appicon parent
2 *
3 * Window Maker window manager
4 *
5 * Copyright (c) 1997, 1998 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 free(icon->file);
281 if (icon->image!=NULL)
282 RDestroyImage(icon->image);
284 wCoreDestroy(icon->core);
285 free(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 RDestroyImage(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 RDestroyImage(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;
403 if (icon->width > wPreferences.icon_size
404 || icon->height > wPreferences.icon_size) {
405 if (icon->width > icon->height) {
406 w = wPreferences.icon_size - 4;
407 h = w*icon->height/icon->width;
408 } else {
409 h = wPreferences.icon_size - 4;
410 w = h*icon->width/icon->height;
412 tmp = RScaleImage(icon, w, h);
413 RDestroyImage(icon);
414 icon = tmp;
417 return icon;
421 Bool
422 wIconChangeImageFile(WIcon *icon, char *file)
424 WScreen *scr = icon->core->screen_ptr;
425 RImage *image;
426 char *path;
427 int error = 0;
429 if (!file) {
430 wIconChangeImage(icon, NULL);
431 return True;
434 path = FindImage(wPreferences.icon_path, file);
436 if (path && (image = RLoadImage(scr->rcontext, path, 0))) {
437 wIconChangeImage(icon, image);
438 } else {
439 error = 1;
442 if (path)
443 free(path);
445 return !error;
450 static char*
451 getnameforicon(WWindow *wwin)
453 char *prefix, *suffix;
454 char *path;
455 int len;
457 if (wwin->wm_class && wwin->wm_instance) {
458 suffix = wmalloc(strlen(wwin->wm_class)+strlen(wwin->wm_instance)+2);
459 sprintf(suffix, "%s.%s", wwin->wm_instance, wwin->wm_class);
460 } else if (wwin->wm_class) {
461 suffix = wmalloc(strlen(wwin->wm_class)+1);
462 strcpy(suffix, wwin->wm_class);
463 } else if (wwin->wm_instance) {
464 suffix = wmalloc(strlen(wwin->wm_instance)+1);
465 strcpy(suffix, wwin->wm_instance);
466 } else {
467 return NULL;
470 prefix = wusergnusteppath();
471 len = strlen(prefix)+64+strlen(suffix);
472 path = wmalloc(len+1);
473 sprintf(path, "%s/.AppInfo", prefix);
475 if (access(path, F_OK)!=0) {
476 if (mkdir(path, S_IRUSR|S_IWUSR|S_IXUSR)) {
477 wsyserror(_("could not create directory %s"), path);
478 free(path);
479 free(suffix);
480 return NULL;
483 strcat(path, "/WindowMaker");
484 if (access(path, F_OK)!=0) {
485 if (mkdir(path, S_IRUSR|S_IWUSR|S_IXUSR)!=0) {
486 wsyserror(_("could not create directory %s"), path);
487 free(path);
488 free(suffix);
489 return NULL;
493 strcat(path, "/");
494 strcat(path, suffix);
495 strcat(path, ".xpm");
496 free(suffix);
498 return path;
503 * wIconStore--
504 * Stores the client supplied icon at ~/GNUstep/.AppInfo/WindowMaker
505 * and returns the path for that icon. Returns NULL if there is no
506 * client supplied icon or on failure.
508 * Side effects:
509 * New directories might be created.
511 char*
512 wIconStore(WIcon *icon)
514 char *path;
515 RImage *image;
516 WWindow *wwin = icon->owner;
518 if (!wwin || !wwin->wm_hints || !(wwin->wm_hints->flags & IconPixmapHint)
519 || wwin->wm_hints->icon_pixmap == None)
520 return NULL;
522 path = getnameforicon(wwin);
523 if (!path)
524 return NULL;
526 image = RCreateImageFromDrawable(icon->core->screen_ptr->rcontext,
527 wwin->wm_hints->icon_pixmap,
528 (wwin->wm_hints->flags & IconMaskHint)
529 ? wwin->wm_hints->icon_mask : None);
530 if (!image) {
531 free(path);
532 return NULL;
535 if (!RSaveImage(image, path, "XPM")) {
536 free(path);
537 path = NULL;
539 RDestroyImage(image);
541 return path;
546 void
547 wIconChangeIconWindow(WIcon *icon, Window new_window);
550 static void
551 cycleColor(void *data)
553 WIcon *icon = (WIcon*)data;
554 WScreen *scr = icon->core->screen_ptr;
555 XGCValues gcv;
557 icon->step--;
558 gcv.dash_offset = icon->step;
559 XChangeGC(dpy, scr->icon_select_gc, GCDashOffset, &gcv);
561 XDrawRectangle(dpy, icon->core->window, scr->icon_select_gc, 0, 0,
562 icon->core->width-1, icon->core->height-1);
563 icon->handlerID = WMAddTimerHandler(COLOR_CYCLE_DELAY, cycleColor, icon);
567 void
568 wIconSetHighlited(WIcon *icon, Bool flag)
570 if (icon->highlighted == flag) {
571 return;
574 icon->highlighted = flag;
575 wIconPaint(icon);
580 void
581 wIconSelect(WIcon *icon)
583 WScreen *scr = icon->core->screen_ptr;
584 icon->selected = !icon->selected;
586 if (icon->selected) {
587 icon->step = 0;
588 if (!wPreferences.dont_blink)
589 icon->handlerID = WMAddTimerHandler(10, cycleColor, icon);
590 else
591 XDrawRectangle(dpy, icon->core->window, scr->icon_select_gc, 0, 0,
592 icon->core->width-1, icon->core->height-1);
593 } else {
594 if (icon->handlerID) {
595 WMDeleteTimerHandler(icon->handlerID);
596 icon->handlerID = NULL;
598 XClearArea(dpy, icon->core->window, 0, 0, icon->core->width,
599 icon->core->height, True);
604 void
605 wIconUpdate(WIcon *icon)
607 WScreen *scr = icon->core->screen_ptr;
608 int title_height = WMFontHeight(scr->icon_title_font);
609 WWindow *wwin = icon->owner;
611 assert(scr->icon_tile!=NULL);
613 if (icon->pixmap!=None)
614 XFreePixmap(dpy, icon->pixmap);
615 icon->pixmap = None;
618 if (wwin && WFLAGP(wwin, always_user_icon))
619 goto user_icon;
621 /* use client specified icon window */
622 if (icon->icon_win!=None) {
623 XWindowAttributes attr;
624 int resize=0;
625 int width, height, depth;
626 int theight;
627 Pixmap pixmap;
629 getSize(icon->icon_win, &width, &height, &depth);
631 if (width > wPreferences.icon_size) {
632 resize = 1;
633 width = wPreferences.icon_size;
635 if (height > wPreferences.icon_size) {
636 resize = 1;
637 height = wPreferences.icon_size;
639 if (icon->show_title
640 && (height+title_height < wPreferences.icon_size)) {
641 pixmap = XCreatePixmap(dpy, scr->w_win, wPreferences.icon_size,
642 wPreferences.icon_size, scr->w_depth);
643 XSetClipMask(dpy, scr->copy_gc, None);
644 XCopyArea(dpy, scr->icon_tile_pixmap, pixmap, scr->copy_gc, 0, 0,
645 wPreferences.icon_size, wPreferences.icon_size, 0, 0);
646 drawIconTitle(scr, pixmap, title_height);
647 theight = title_height;
648 } else {
649 pixmap = None;
650 theight = 0;
651 XSetWindowBackgroundPixmap(dpy, icon->core->window,
652 scr->icon_tile_pixmap);
655 XSetWindowBorderWidth(dpy, icon->icon_win, 0);
656 XReparentWindow(dpy, icon->icon_win, icon->core->window,
657 (wPreferences.icon_size-width)/2,
658 theight+(wPreferences.icon_size-height-theight)/2);
659 if (resize)
660 XResizeWindow(dpy, icon->icon_win, width, height);
662 XMapWindow(dpy, icon->icon_win);
664 XAddToSaveSet(dpy, icon->icon_win);
666 icon->pixmap = pixmap;
668 if (XGetWindowAttributes(dpy, icon->icon_win, &attr)) {
669 if (attr.all_event_masks & ButtonPressMask) {
670 wHackedGrabButton(Button1, MOD_MASK, icon->core->window, True,
671 ButtonPressMask, GrabModeSync, GrabModeAsync,
672 None, wCursor[WCUR_ARROW]);
675 } else if (wwin && wwin->wm_hints
676 && (wwin->wm_hints->flags & IconPixmapHint)) {
677 int x, y;
678 unsigned int w, h;
679 Window jw;
680 int ji, dotitle;
681 unsigned int ju, d;
682 Pixmap pixmap;
684 if (!XGetGeometry(dpy, wwin->wm_hints->icon_pixmap, &jw,
685 &ji, &ji, &w, &h, &ju, &d)) {
686 icon->owner->wm_hints->flags &= ~IconPixmapHint;
687 goto user_icon;
690 pixmap = XCreatePixmap(dpy, icon->core->window, wPreferences.icon_size,
691 wPreferences.icon_size, scr->w_depth);
692 XSetClipMask(dpy, scr->copy_gc, None);
693 XCopyArea(dpy, scr->icon_tile_pixmap, pixmap, scr->copy_gc, 0, 0,
694 wPreferences.icon_size, wPreferences.icon_size, 0, 0);
696 if (w > wPreferences.icon_size)
697 w = wPreferences.icon_size;
698 x = (wPreferences.icon_size-w)/2;
700 if (icon->show_title && (title_height < wPreferences.icon_size)) {
701 drawIconTitle(scr, pixmap, title_height);
702 dotitle = 1;
704 if (h > wPreferences.icon_size - title_height - 2) {
705 h = wPreferences.icon_size - title_height - 2;
706 y = title_height + 1;
707 } else {
708 y = (wPreferences.icon_size-h-title_height)/2+title_height + 1;
710 } else {
711 dotitle = 0;
712 if (w > wPreferences.icon_size)
713 w = wPreferences.icon_size;
714 y = (wPreferences.icon_size-h)/2;
717 if (wwin->wm_hints->flags & IconMaskHint)
718 XSetClipMask(dpy, scr->copy_gc, wwin->wm_hints->icon_mask);
720 XSetClipOrigin(dpy, scr->copy_gc, x, y);
722 if (d != scr->w_depth) {
723 XSetForeground(dpy, scr->copy_gc, scr->black_pixel);
724 XSetBackground(dpy, scr->copy_gc, scr->white_pixel);
725 XCopyPlane(dpy, wwin->wm_hints->icon_pixmap, pixmap, scr->copy_gc,
726 0, 0, w, h, x, y, 1);
727 } else {
728 XCopyArea(dpy, wwin->wm_hints->icon_pixmap, pixmap, scr->copy_gc,
729 0, 0, w, h, x, y);
732 XSetClipOrigin(dpy, scr->copy_gc, 0, 0);
734 icon->pixmap = pixmap;
735 } else {
736 user_icon:
738 if (icon->image) {
739 icon->pixmap = makeIcon(scr, icon->image, icon->show_title,
740 icon->shadowed, icon->tile_type);
741 } else {
742 /* make default icons */
744 if (!scr->def_icon_pixmap) {
745 RImage *image = NULL;
746 char *path;
747 char *file;
749 file = wDefaultGetIconFile(scr, NULL, NULL, False);
750 if (file) {
751 path = FindImage(wPreferences.icon_path, file);
752 if (!path) {
753 wwarning(_("could not find default icon \"%s\""),file);
754 goto make_icons;
757 image = RLoadImage(scr->rcontext, path, 0);
758 if (!image) {
759 wwarning(_("could not load default icon \"%s\":%s"),
760 file, RMessageForError(RErrorCode));
762 free(path);
764 make_icons:
766 image = wIconValidateIconSize(scr, image);
767 scr->def_icon_pixmap = makeIcon(scr, image, False, False,
768 icon->tile_type);
769 scr->def_ticon_pixmap = makeIcon(scr, image, True, False,
770 icon->tile_type);
771 if (image)
772 RDestroyImage(image);
775 if (icon->show_title) {
776 XSetWindowBackgroundPixmap(dpy, icon->core->window,
777 scr->def_ticon_pixmap);
778 } else {
779 XSetWindowBackgroundPixmap(dpy, icon->core->window,
780 scr->def_icon_pixmap);
782 icon->pixmap = None;
785 if (icon->pixmap != None) {
786 XSetWindowBackgroundPixmap(dpy, icon->core->window, icon->pixmap);
788 XClearWindow(dpy, icon->core->window);
790 wIconPaint(icon);
795 void
796 wIconPaint(WIcon *icon)
798 WScreen *scr=icon->core->screen_ptr;
799 GC gc = scr->icon_title_gc;
800 int x;
801 char *tmp;
803 if (icon->force_paint) {
804 icon->force_paint = 0;
805 wIconUpdate(icon);
806 return;
809 XClearWindow(dpy, icon->core->window);
811 /* draw the icon title */
812 if (icon->show_title && icon->icon_name!=NULL) {
813 int l;
814 int w;
816 tmp = ShrinkString(scr->icon_title_font, icon->icon_name,
817 wPreferences.icon_size-4);
818 w = WMWidthOfString(scr->icon_title_font, tmp, l=strlen(tmp));
820 if (w > icon->core->width - 4)
821 x = (icon->core->width - 4) - w;
822 else
823 x = (icon->core->width - w)/2;
825 WMDrawString(scr->wmscreen, icon->core->window, gc,
826 scr->icon_title_font, x, 1, tmp, l);
827 free(tmp);
830 if (icon->selected)
831 XDrawRectangle(dpy, icon->core->window, scr->icon_select_gc, 0, 0,
832 icon->core->width-1, icon->core->height-1);
836 /******************************************************************/
838 static void
839 miniwindowExpose(WObjDescriptor *desc, XEvent *event)
841 wIconPaint(desc->parent);
845 static void
846 miniwindowDblClick(WObjDescriptor *desc, XEvent *event)
848 WIcon *icon = desc->parent;
850 assert(icon->owner!=NULL);
852 wDeiconifyWindow(icon->owner);
856 static void
857 miniwindowMouseDown(WObjDescriptor *desc, XEvent *event)
859 WIcon *icon = desc->parent;
860 WWindow *wwin = icon->owner;
861 XEvent ev;
862 int x=wwin->icon_x, y=wwin->icon_y;
863 int dx=event->xbutton.x, dy=event->xbutton.y;
864 int grabbed=0;
865 int clickButton=event->xbutton.button;
867 if (WCHECK_STATE(WSTATE_MODAL))
868 return;
870 if (IsDoubleClick(icon->core->screen_ptr, event)) {
871 miniwindowDblClick(desc, event);
872 return;
875 #ifdef DEBUG
876 puts("Moving miniwindow");
877 #endif
878 if (event->xbutton.button == Button1) {
879 if (event->xbutton.state & MOD_MASK)
880 wLowerFrame(icon->core);
881 else
882 wRaiseFrame(icon->core);
883 if (event->xbutton.state & ShiftMask) {
884 wIconSelect(icon);
885 wSelectWindow(icon->owner, !wwin->flags.selected);
887 } else if (event->xbutton.button == Button3) {
888 WObjDescriptor *desc;
890 OpenMiniwindowMenu(wwin, event->xbutton.x_root,
891 event->xbutton.y_root);
893 /* allow drag select of menu */
894 desc = &wwin->screen_ptr->window_menu->menu->descriptor;
895 event->xbutton.send_event = True;
896 (*desc->handle_mousedown)(desc, event);
898 return;
901 if (XGrabPointer(dpy, icon->core->window, False, ButtonMotionMask
902 |ButtonReleaseMask|ButtonPressMask, GrabModeAsync,
903 GrabModeAsync, None, None, CurrentTime) !=GrabSuccess) {
904 #ifdef DEBUG0
905 wwarning("pointer grab failed for icon move");
906 #endif
908 while(1) {
909 WMMaskEvent(dpy, PointerMotionMask|ButtonReleaseMask|ButtonPressMask
910 |ButtonMotionMask|ExposureMask, &ev);
911 switch (ev.type) {
912 case Expose:
913 WMHandleEvent(&ev);
914 break;
916 case MotionNotify:
917 if (!grabbed) {
918 if (abs(dx-ev.xmotion.x)>=MOVE_THRESHOLD
919 || abs(dy-ev.xmotion.y)>=MOVE_THRESHOLD) {
920 XChangeActivePointerGrab(dpy, ButtonMotionMask
921 |ButtonReleaseMask|ButtonPressMask,
922 wCursor[WCUR_MOVE], CurrentTime);
923 grabbed=1;
924 } else {
925 break;
928 x = ev.xmotion.x_root - dx;
929 y = ev.xmotion.y_root - dy;
930 XMoveWindow(dpy, icon->core->window, x, y);
931 break;
933 case ButtonPress:
934 break;
936 case ButtonRelease:
937 if (ev.xbutton.button != clickButton)
938 break;
940 if (wwin->icon_x!=x || wwin->icon_y!=y)
941 wwin->flags.icon_moved = 1;
943 XMoveWindow(dpy, icon->core->window, x, y);
945 wwin->icon_x = x;
946 wwin->icon_y = y;
947 #ifdef DEBUG
948 puts("End miniwindow move");
949 #endif
950 XUngrabPointer(dpy, CurrentTime);
952 if (wPreferences.auto_arrange_icons)
953 wArrangeIcons(wwin->screen_ptr, True);
954 return;