*** empty log message ***
[wmaker-crm.git] / src / icon.c
blob1d0581398ae64167a584a32fa4f971bdab73af92
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, 1, 1, True);
87 static void
88 tileObserver(void *self, WMNotification *notif)
90 WIcon *icon = (WIcon*)self;
92 icon->force_paint = 1;
93 wIconPaint(icon);
95 XClearArea(dpy, icon->core->window, 0, 0, 1, 1, True);
98 /************************************/
102 INLINE static void
103 getSize(Drawable d, unsigned int *w, unsigned int *h, unsigned int *dep)
105 Window rjunk;
106 int xjunk, yjunk;
107 unsigned int bjunk;
109 XGetGeometry(dpy, d, &rjunk, &xjunk, &yjunk, w, h, &bjunk, dep);
113 WIcon*
114 wIconCreate(WWindow *wwin)
116 WScreen *scr=wwin->screen_ptr;
117 WIcon *icon;
118 char *file;
119 unsigned long vmask = 0;
120 XSetWindowAttributes attribs;
122 icon = wmalloc(sizeof(WIcon));
123 memset(icon, 0, sizeof(WIcon));
124 icon->core = wCoreCreateTopLevel(scr, wwin->icon_x, wwin->icon_y,
125 wPreferences.icon_size,
126 wPreferences.icon_size, 0);
128 if (wPreferences.use_saveunders) {
129 vmask |= CWSaveUnder;
130 attribs.save_under = True;
132 /* a white border for selecting it */
133 vmask |= CWBorderPixel;
134 attribs.border_pixel = scr->white_pixel;
136 XChangeWindowAttributes(dpy, icon->core->window, vmask, &attribs);
139 /* will be overriden if this is an application icon */
140 icon->core->descriptor.handle_mousedown = miniwindowMouseDown;
141 icon->core->descriptor.handle_expose = miniwindowExpose;
142 icon->core->descriptor.parent_type = WCLASS_MINIWINDOW;
143 icon->core->descriptor.parent = icon;
145 icon->core->stacking = wmalloc(sizeof(WStacking));
146 icon->core->stacking->above = NULL;
147 icon->core->stacking->under = NULL;
148 icon->core->stacking->window_level = NORMAL_ICON_LEVEL;
149 icon->core->stacking->child_of = NULL;
151 icon->owner = wwin;
152 if (wwin->wm_hints && (wwin->wm_hints->flags & IconWindowHint)) {
153 if (wwin->client_win == wwin->main_window) {
154 WApplication *wapp;
155 /* do not let miniwindow steal app-icon's icon window */
156 wapp = wApplicationOf(wwin->client_win);
157 if (!wapp || wapp->app_icon==NULL)
158 icon->icon_win = wwin->wm_hints->icon_window;
159 } else {
160 icon->icon_win = wwin->wm_hints->icon_window;
163 #ifdef NO_MINIWINDOW_TITLES
164 icon->show_title = 0;
165 #else
166 icon->show_title = 1;
167 #endif
168 icon->image = wDefaultGetImage(scr, wwin->wm_instance, wwin->wm_class);
170 file = wDefaultGetIconFile(scr, wwin->wm_instance, wwin->wm_class,
171 False);
172 if (file) {
173 icon->file = wstrdup(file);
176 wGetIconName(dpy, wwin->client_win, &icon->icon_name);
178 icon->tile_type = TILE_NORMAL;
180 wIconUpdate(icon);
182 XFlush(dpy);
184 WMAddNotificationObserver(appearanceObserver, icon,
185 WNIconAppearanceSettingsChanged, icon);
186 WMAddNotificationObserver(tileObserver, icon,
187 WNIconTileSettingsChanged, icon);
188 return icon;
192 WIcon*
193 wIconCreateWithIconFile(WScreen *scr, char *iconfile, int tile)
195 WIcon *icon;
196 unsigned long vmask = 0;
197 XSetWindowAttributes attribs;
199 icon = wmalloc(sizeof(WIcon));
200 memset(icon, 0, sizeof(WIcon));
201 icon->core = wCoreCreateTopLevel(scr, 0, 0, wPreferences.icon_size,
202 wPreferences.icon_size, 0);
203 if (wPreferences.use_saveunders) {
204 vmask = CWSaveUnder;
205 attribs.save_under = True;
207 /* a white border for selecting it */
208 vmask |= CWBorderPixel;
209 attribs.border_pixel = scr->white_pixel;
211 XChangeWindowAttributes(dpy, icon->core->window, vmask, &attribs);
213 /* will be overriden if this is a application icon */
214 icon->core->descriptor.handle_mousedown = miniwindowMouseDown;
215 icon->core->descriptor.handle_expose = miniwindowExpose;
216 icon->core->descriptor.parent_type = WCLASS_MINIWINDOW;
217 icon->core->descriptor.parent = icon;
219 icon->core->stacking = wmalloc(sizeof(WStacking));
220 icon->core->stacking->above = NULL;
221 icon->core->stacking->under = NULL;
222 icon->core->stacking->window_level = NORMAL_ICON_LEVEL;
223 icon->core->stacking->child_of = NULL;
225 if (iconfile) {
226 icon->image = RLoadImage(scr->rcontext, iconfile, 0);
227 if (!icon->image) {
228 wwarning(_("error loading image file \"%s\""), iconfile, RMessageForError(RErrorCode));
231 icon->image = wIconValidateIconSize(scr, icon->image);
233 icon->file = wstrdup(iconfile);
236 icon->tile_type = tile;
238 wIconUpdate(icon);
240 WMAddNotificationObserver(tileObserver, icon,
241 WNIconTileSettingsChanged, icon);
243 return icon;
248 void
249 wIconDestroy(WIcon *icon)
251 WCoreWindow *core = icon->core;
252 WScreen *scr = core->screen_ptr;
254 WMRemoveNotificationObserver(icon);
256 if (icon->handlerID)
257 WMDeleteTimerHandler(icon->handlerID);
259 if (icon->icon_win) {
260 int x=0, y=0;
262 if (icon->owner) {
263 x = icon->owner->icon_x;
264 y = icon->owner->icon_y;
266 XUnmapWindow(dpy, icon->icon_win);
267 XReparentWindow(dpy, icon->icon_win, scr->root_win, x, y);
269 if (icon->icon_name)
270 XFree(icon->icon_name);
272 if (icon->pixmap)
273 XFreePixmap(dpy, icon->pixmap);
275 if (icon->file)
276 free(icon->file);
278 if (icon->image!=NULL)
279 RDestroyImage(icon->image);
281 wCoreDestroy(icon->core);
282 free(icon);
287 static void
288 drawIconTitle(WScreen *scr, Pixmap pixmap, int height)
290 XFillRectangle(dpy, pixmap, scr->icon_title_texture->normal_gc,
291 0, 0, wPreferences.icon_size, height+1);
292 XDrawLine(dpy, pixmap, scr->icon_title_texture->light_gc, 0, 0,
293 wPreferences.icon_size, 0);
294 XDrawLine(dpy, pixmap, scr->icon_title_texture->light_gc, 0, 0,
295 0, height+1);
296 XDrawLine(dpy, pixmap, scr->icon_title_texture->dim_gc,
297 wPreferences.icon_size-1, 0, wPreferences.icon_size-1, height+1);
301 static Pixmap
302 makeIcon(WScreen *scr, RImage *icon, int titled, int shadowed, int tileType)
304 RImage *tile;
305 Pixmap pixmap;
306 int x, y, sx, sy;
307 unsigned w, h;
308 int theight = scr->icon_title_font->height;
310 if (tileType == TILE_NORMAL)
311 tile = RCloneImage(scr->icon_tile);
312 else {
313 assert(scr->clip_tile);
314 tile = RCloneImage(scr->clip_tile);
316 if (icon) {
317 w = (icon->width > wPreferences.icon_size)
318 ? wPreferences.icon_size : icon->width;
319 x = (wPreferences.icon_size - w) / 2;
320 sx = (icon->width - w)/2;
322 if (!titled) {
323 h = (icon->height > wPreferences.icon_size)
324 ? wPreferences.icon_size : icon->height;
325 y = (wPreferences.icon_size - h) / 2;
326 sy = (icon->height - h)/2;
327 } else {
328 h = (icon->height+theight > wPreferences.icon_size
329 ? wPreferences.icon_size-theight : icon->height);
330 y = theight+((int)wPreferences.icon_size-theight-h)/2;
331 sy = (icon->height - h)/2;
333 RCombineArea(tile, icon, sx, sy, w, h, x, y);
336 if (shadowed) {
337 RColor color;
339 color.red = scr->icon_back_texture->light.red >> 8;
340 color.green = scr->icon_back_texture->light.green >> 8;
341 color.blue = scr->icon_back_texture->light.blue >> 8;
342 color.alpha = 150; /* about 60% */
343 RClearImage(tile, &color);
346 if (!RConvertImage(scr->rcontext, tile, &pixmap)) {
347 wwarning(_("error rendering image:%s"), RMessageForError(RErrorCode));
349 RDestroyImage(tile);
351 if (titled)
352 drawIconTitle(scr, pixmap, theight);
354 return pixmap;
358 void
359 wIconChangeTitle(WIcon *icon, char *new_title)
361 int changed;
363 changed = (new_title==NULL && icon->icon_name!=NULL)
364 || (new_title!=NULL && icon->icon_name==NULL);
366 if (icon->icon_name!=NULL)
367 XFree(icon->icon_name);
369 icon->icon_name = new_title;
371 if (changed)
372 icon->force_paint = 1;
373 wIconPaint(icon);
377 void
378 wIconChangeImage(WIcon *icon, RImage *new_image)
380 assert(icon != NULL);
382 if (icon->image)
383 RDestroyImage(icon->image);
385 icon->image = wIconValidateIconSize(icon->core->screen_ptr, new_image);
387 wIconUpdate(icon);
391 RImage*
392 wIconValidateIconSize(WScreen *scr, RImage *icon)
394 RImage *tmp;
395 int w, h;
397 if (!icon)
398 return NULL;
400 if (icon->width > wPreferences.icon_size
401 || icon->height > wPreferences.icon_size) {
402 if (icon->width > icon->height) {
403 w = wPreferences.icon_size - 4;
404 h = w*icon->height/icon->width;
405 } else {
406 h = wPreferences.icon_size - 4;
407 w = h*icon->width/icon->height;
409 tmp = RScaleImage(icon, w, h);
410 RDestroyImage(icon);
411 icon = tmp;
414 return icon;
418 Bool
419 wIconChangeImageFile(WIcon *icon, char *file)
421 WScreen *scr = icon->core->screen_ptr;
422 RImage *image;
423 char *path;
424 int error = 0;
426 if (!file) {
427 wIconChangeImage(icon, NULL);
428 return True;
431 path = FindImage(wPreferences.icon_path, file);
433 if (path && (image = RLoadImage(scr->rcontext, path, 0))) {
434 wIconChangeImage(icon, image);
435 } else {
436 error = 1;
439 if (path)
440 free(path);
442 return !error;
447 static char*
448 getnameforicon(WWindow *wwin)
450 char *prefix, *suffix;
451 char *path;
452 int len;
454 if (wwin->wm_class && wwin->wm_instance) {
455 suffix = wmalloc(strlen(wwin->wm_class)+strlen(wwin->wm_instance)+2);
456 sprintf(suffix, "%s.%s", wwin->wm_instance, wwin->wm_class);
457 } else if (wwin->wm_class) {
458 suffix = wmalloc(strlen(wwin->wm_class)+1);
459 strcpy(suffix, wwin->wm_class);
460 } else if (wwin->wm_instance) {
461 suffix = wmalloc(strlen(wwin->wm_instance)+1);
462 strcpy(suffix, wwin->wm_instance);
463 } else {
464 return NULL;
467 prefix = wusergnusteppath();
468 len = strlen(prefix)+64+strlen(suffix);
469 path = wmalloc(len+1);
470 sprintf(path, "%s/.AppInfo", prefix);
472 if (access(path, F_OK)!=0) {
473 if (mkdir(path, S_IRUSR|S_IWUSR|S_IXUSR)) {
474 wsyserror(_("could not create directory %s"), path);
475 free(path);
476 free(suffix);
477 return NULL;
480 strcat(path, "/WindowMaker");
481 if (access(path, F_OK)!=0) {
482 if (mkdir(path, S_IRUSR|S_IWUSR|S_IXUSR)!=0) {
483 wsyserror(_("could not create directory %s"), path);
484 free(path);
485 free(suffix);
486 return NULL;
490 strcat(path, "/");
491 strcat(path, suffix);
492 strcat(path, ".xpm");
493 free(suffix);
495 return path;
500 * wIconStore--
501 * Stores the client supplied icon at ~/GNUstep/.AppInfo/WindowMaker
502 * and returns the path for that icon. Returns NULL if there is no
503 * client supplied icon or on failure.
505 * Side effects:
506 * New directories might be created.
508 char*
509 wIconStore(WIcon *icon)
511 char *path;
512 RImage *image;
513 WWindow *wwin = icon->owner;
515 if (!wwin || !wwin->wm_hints || !(wwin->wm_hints->flags & IconPixmapHint)
516 || wwin->wm_hints->icon_pixmap == None)
517 return NULL;
519 path = getnameforicon(wwin);
520 if (!path)
521 return NULL;
523 image = RCreateImageFromDrawable(icon->core->screen_ptr->rcontext,
524 wwin->wm_hints->icon_pixmap,
525 (wwin->wm_hints->flags & IconMaskHint)
526 ? wwin->wm_hints->icon_mask : None);
527 if (!image) {
528 free(path);
529 return NULL;
532 if (!RSaveImage(image, path, "XPM")) {
533 free(path);
534 path = NULL;
536 RDestroyImage(image);
538 return path;
543 void
544 wIconChangeIconWindow(WIcon *icon, Window new_window);
547 static void
548 cycleColor(void *data)
550 WIcon *icon = (WIcon*)data;
551 WScreen *scr = icon->core->screen_ptr;
552 XGCValues gcv;
554 icon->step--;
555 gcv.dash_offset = icon->step;
556 XChangeGC(dpy, scr->icon_select_gc, GCDashOffset, &gcv);
558 XDrawRectangle(dpy, icon->core->window, scr->icon_select_gc, 0, 0,
559 icon->core->width-1, icon->core->height-1);
560 icon->handlerID = WMAddTimerHandler(COLOR_CYCLE_DELAY, cycleColor, icon);
564 void
565 wIconSetHighlited(WIcon *icon, Bool flag)
567 if (icon->highlighted == flag) {
568 return;
571 icon->highlighted = flag;
572 wIconPaint(icon);
577 void
578 wIconSelect(WIcon *icon)
580 WScreen *scr = icon->core->screen_ptr;
581 icon->selected = !icon->selected;
583 if (icon->selected) {
584 icon->step = 0;
585 if (!wPreferences.dont_blink)
586 icon->handlerID = WMAddTimerHandler(10, cycleColor, icon);
587 else
588 XDrawRectangle(dpy, icon->core->window, scr->icon_select_gc, 0, 0,
589 icon->core->width-1, icon->core->height-1);
590 } else {
591 if (icon->handlerID) {
592 WMDeleteTimerHandler(icon->handlerID);
593 icon->handlerID = NULL;
595 XClearArea(dpy, icon->core->window, 0, 0, icon->core->width,
596 icon->core->height, True);
601 void
602 wIconUpdate(WIcon *icon)
604 WScreen *scr = icon->core->screen_ptr;
605 int title_height = scr->icon_title_font->height;
606 WWindow *wwin = icon->owner;
608 assert(scr->icon_tile!=NULL);
610 if (icon->pixmap!=None)
611 XFreePixmap(dpy, icon->pixmap);
612 icon->pixmap = None;
615 if (wwin && WFLAGP(wwin, always_user_icon))
616 goto user_icon;
618 /* use client specified icon window */
619 if (icon->icon_win!=None) {
620 XWindowAttributes attr;
621 int resize=0;
622 int width, height, depth;
623 int theight;
624 Pixmap pixmap;
626 getSize(icon->icon_win, &width, &height, &depth);
628 if (width > wPreferences.icon_size) {
629 resize = 1;
630 width = wPreferences.icon_size;
632 if (height > wPreferences.icon_size) {
633 resize = 1;
634 height = wPreferences.icon_size;
636 if (icon->show_title
637 && (height+title_height < wPreferences.icon_size)) {
638 pixmap = XCreatePixmap(dpy, scr->w_win, wPreferences.icon_size,
639 wPreferences.icon_size, scr->w_depth);
640 XSetClipMask(dpy, scr->copy_gc, None);
641 XCopyArea(dpy, scr->icon_tile_pixmap, pixmap, scr->copy_gc, 0, 0,
642 wPreferences.icon_size, wPreferences.icon_size, 0, 0);
643 drawIconTitle(scr, pixmap, title_height);
644 theight = title_height;
645 } else {
646 pixmap = None;
647 theight = 0;
648 XSetWindowBackgroundPixmap(dpy, icon->core->window,
649 scr->icon_tile_pixmap);
652 XSetWindowBorderWidth(dpy, icon->icon_win, 0);
653 XReparentWindow(dpy, icon->icon_win, icon->core->window,
654 (wPreferences.icon_size-width)/2,
655 theight+(wPreferences.icon_size-height-theight)/2);
656 if (resize)
657 XResizeWindow(dpy, icon->icon_win, width, height);
659 XMapWindow(dpy, icon->icon_win);
661 XAddToSaveSet(dpy, icon->icon_win);
663 icon->pixmap = pixmap;
665 if (XGetWindowAttributes(dpy, icon->icon_win, &attr)) {
666 if (attr.all_event_masks & ButtonPressMask) {
667 wHackedGrabButton(Button1, MOD_MASK, icon->core->window, True,
668 ButtonPressMask, GrabModeSync, GrabModeAsync,
669 None, wCursor[WCUR_ARROW]);
672 } else if (wwin && wwin->wm_hints
673 && (wwin->wm_hints->flags & IconPixmapHint)) {
674 int x, y;
675 unsigned int w, h;
676 Window jw;
677 int ji, dotitle;
678 unsigned int ju, d;
679 Pixmap pixmap;
681 if (!XGetGeometry(dpy, wwin->wm_hints->icon_pixmap, &jw,
682 &ji, &ji, &w, &h, &ju, &d)) {
683 icon->owner->wm_hints->flags &= ~IconPixmapHint;
684 goto user_icon;
687 pixmap = XCreatePixmap(dpy, icon->core->window, wPreferences.icon_size,
688 wPreferences.icon_size, scr->w_depth);
689 XSetClipMask(dpy, scr->copy_gc, None);
690 XCopyArea(dpy, scr->icon_tile_pixmap, pixmap, scr->copy_gc, 0, 0,
691 wPreferences.icon_size, wPreferences.icon_size, 0, 0);
693 if (w > wPreferences.icon_size)
694 w = wPreferences.icon_size;
695 x = (wPreferences.icon_size-w)/2;
697 if (icon->show_title && (title_height < wPreferences.icon_size)) {
698 drawIconTitle(scr, pixmap, title_height);
699 dotitle = 1;
701 if (h > wPreferences.icon_size - title_height - 2) {
702 h = wPreferences.icon_size - title_height - 2;
703 y = title_height + 1;
704 } else {
705 y = (wPreferences.icon_size-h-title_height)/2+title_height + 1;
707 } else {
708 dotitle = 0;
709 if (w > wPreferences.icon_size)
710 w = wPreferences.icon_size;
711 y = (wPreferences.icon_size-h)/2;
714 if (wwin->wm_hints->flags & IconMaskHint)
715 XSetClipMask(dpy, scr->copy_gc, wwin->wm_hints->icon_mask);
717 XSetClipOrigin(dpy, scr->copy_gc, x, y);
719 if (d != scr->w_depth) {
720 XSetForeground(dpy, scr->copy_gc, scr->black_pixel);
721 XSetBackground(dpy, scr->copy_gc, scr->white_pixel);
722 XCopyPlane(dpy, wwin->wm_hints->icon_pixmap, pixmap, scr->copy_gc,
723 0, 0, w, h, x, y, 1);
724 } else {
725 XCopyArea(dpy, wwin->wm_hints->icon_pixmap, pixmap, scr->copy_gc,
726 0, 0, w, h, x, y);
729 XSetClipOrigin(dpy, scr->copy_gc, 0, 0);
731 icon->pixmap = pixmap;
732 } else {
733 user_icon:
735 if (icon->image) {
736 icon->pixmap = makeIcon(scr, icon->image, icon->show_title,
737 icon->shadowed, icon->tile_type);
738 } else {
739 /* make default icons */
741 if (!scr->def_icon_pixmap) {
742 RImage *image = NULL;
743 char *path;
744 char *file;
746 file = wDefaultGetIconFile(scr, NULL, NULL, False);
747 if (file) {
748 path = FindImage(wPreferences.icon_path, file);
749 if (!path) {
750 wwarning(_("could not find default icon \"%s\""),file);
751 goto make_icons;
754 image = RLoadImage(scr->rcontext, path, 0);
755 if (!image) {
756 wwarning(_("could not load default icon \"%s\":%s"),
757 file, RMessageForError(RErrorCode));
759 free(path);
761 make_icons:
763 image = wIconValidateIconSize(scr, image);
764 scr->def_icon_pixmap = makeIcon(scr, image, False, False,
765 icon->tile_type);
766 scr->def_ticon_pixmap = makeIcon(scr, image, True, False,
767 icon->tile_type);
768 if (image)
769 RDestroyImage(image);
772 if (icon->show_title) {
773 XSetWindowBackgroundPixmap(dpy, icon->core->window,
774 scr->def_ticon_pixmap);
775 } else {
776 XSetWindowBackgroundPixmap(dpy, icon->core->window,
777 scr->def_icon_pixmap);
779 icon->pixmap = None;
782 if (icon->pixmap != None) {
783 XSetWindowBackgroundPixmap(dpy, icon->core->window, icon->pixmap);
785 XClearWindow(dpy, icon->core->window);
787 wIconPaint(icon);
792 void
793 wIconPaint(WIcon *icon)
795 WScreen *scr=icon->core->screen_ptr;
796 GC gc = scr->icon_title_gc;
797 int x;
798 char *tmp;
800 if (icon->force_paint) {
801 icon->force_paint = 0;
802 wIconUpdate(icon);
803 return;
806 XClearWindow(dpy, icon->core->window);
808 /* draw the icon title */
809 if (icon->show_title && icon->icon_name!=NULL) {
810 int l;
811 int w;
813 tmp = ShrinkString(scr->icon_title_font, icon->icon_name,
814 wPreferences.icon_size-4);
815 w = wTextWidth(scr->icon_title_font->font, tmp, l=strlen(tmp));
817 if (w > icon->core->width - 4)
818 x = (icon->core->width - 4) - w;
819 else
820 x = (icon->core->width - w)/2;
822 wDrawString(icon->core->window, scr->icon_title_font, gc,
823 x, 1 + scr->icon_title_font->y, tmp, l);
824 free(tmp);
827 if (icon->selected)
828 XDrawRectangle(dpy, icon->core->window, scr->icon_select_gc, 0, 0,
829 icon->core->width-1, icon->core->height-1);
833 /******************************************************************/
835 static void
836 miniwindowExpose(WObjDescriptor *desc, XEvent *event)
838 wIconPaint(desc->parent);
842 static void
843 miniwindowDblClick(WObjDescriptor *desc, XEvent *event)
845 WIcon *icon = desc->parent;
847 assert(icon->owner!=NULL);
849 wDeiconifyWindow(icon->owner);
853 static void
854 miniwindowMouseDown(WObjDescriptor *desc, XEvent *event)
856 WIcon *icon = desc->parent;
857 WWindow *wwin = icon->owner;
858 XEvent ev;
859 int x=wwin->icon_x, y=wwin->icon_y;
860 int dx=event->xbutton.x, dy=event->xbutton.y;
861 int grabbed=0;
862 int clickButton=event->xbutton.button;
864 if (WCHECK_STATE(WSTATE_MODAL))
865 return;
867 if (IsDoubleClick(icon->core->screen_ptr, event)) {
868 miniwindowDblClick(desc, event);
869 return;
872 #ifdef DEBUG
873 puts("Moving miniwindow");
874 #endif
875 if (event->xbutton.button == Button1) {
876 if (event->xbutton.state & MOD_MASK)
877 wLowerFrame(icon->core);
878 else
879 wRaiseFrame(icon->core);
880 if (event->xbutton.state & ShiftMask) {
881 wIconSelect(icon);
882 wSelectWindow(icon->owner, !wwin->flags.selected);
884 } else if (event->xbutton.button == Button3) {
885 WObjDescriptor *desc;
887 OpenMiniwindowMenu(wwin, event->xbutton.x_root,
888 event->xbutton.y_root);
890 /* allow drag select of menu */
891 desc = &wwin->screen_ptr->window_menu->menu->descriptor;
892 event->xbutton.send_event = True;
893 (*desc->handle_mousedown)(desc, event);
895 return;
898 if (XGrabPointer(dpy, icon->core->window, False, ButtonMotionMask
899 |ButtonReleaseMask|ButtonPressMask, GrabModeAsync,
900 GrabModeAsync, None, None, CurrentTime) !=GrabSuccess) {
901 #ifdef DEBUG0
902 wwarning("pointer grab failed for icon move");
903 #endif
905 while(1) {
906 WMMaskEvent(dpy, PointerMotionMask|ButtonReleaseMask|ButtonPressMask
907 |ButtonMotionMask|ExposureMask, &ev);
908 switch (ev.type) {
909 case Expose:
910 WMHandleEvent(&ev);
911 break;
913 case MotionNotify:
914 if (!grabbed) {
915 if (abs(dx-ev.xmotion.x)>=MOVE_THRESHOLD
916 || abs(dy-ev.xmotion.y)>=MOVE_THRESHOLD) {
917 XChangeActivePointerGrab(dpy, ButtonMotionMask
918 |ButtonReleaseMask|ButtonPressMask,
919 wCursor[WCUR_MOVE], CurrentTime);
920 grabbed=1;
921 } else {
922 break;
925 x = ev.xmotion.x_root - dx;
926 y = ev.xmotion.y_root - dy;
927 XMoveWindow(dpy, icon->core->window, x, y);
928 break;
930 case ButtonPress:
931 break;
933 case ButtonRelease:
934 if (ev.xbutton.button != clickButton)
935 break;
937 if (wwin->icon_x!=x || wwin->icon_y!=y)
938 wwin->flags.icon_moved = 1;
940 XMoveWindow(dpy, icon->core->window, x, y);
942 wwin->icon_x = x;
943 wwin->icon_y = y;
944 #ifdef DEBUG
945 puts("End miniwindow move");
946 #endif
947 XUngrabPointer(dpy, CurrentTime);
949 if (wPreferences.auto_arrange_icons)
950 wArrangeIcons(wwin->screen_ptr, True);
951 return;