new/changelog fix
[wmaker-crm.git] / src / icon.c
blobacec88e0cd64a2f46c981920f8186cad28929031
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) {
78 XClearArea(dpy, icon->core->window, 0, 0, 1, 1, True);
79 /* wIconPaint(icon);*/
83 static void
84 tileObserver(void *self, WMNotification *notif)
86 WIcon *icon = (WIcon*)self;
88 icon->force_paint = 1;
89 XClearArea(dpy, icon->core->window, 0, 0, 1, 1, True);
90 /* wIconPaint(icon);*/
93 /************************************/
97 INLINE static void
98 getSize(Drawable d, unsigned int *w, unsigned int *h, unsigned int *dep)
100 Window rjunk;
101 int xjunk, yjunk;
102 unsigned int bjunk;
104 XGetGeometry(dpy, d, &rjunk, &xjunk, &yjunk, w, h, &bjunk, dep);
108 WIcon*
109 wIconCreate(WWindow *wwin)
111 WScreen *scr=wwin->screen_ptr;
112 WIcon *icon;
113 char *file;
114 unsigned long vmask = 0;
115 XSetWindowAttributes attribs;
117 icon = wmalloc(sizeof(WIcon));
118 memset(icon, 0, sizeof(WIcon));
119 icon->core = wCoreCreateTopLevel(scr, wwin->icon_x, wwin->icon_y,
120 wPreferences.icon_size,
121 wPreferences.icon_size, 0);
123 if (wPreferences.use_saveunders) {
124 vmask |= CWSaveUnder;
125 attribs.save_under = True;
127 /* a white border for selecting it */
128 vmask |= CWBorderPixel;
129 attribs.border_pixel = scr->white_pixel;
131 XChangeWindowAttributes(dpy, icon->core->window, vmask, &attribs);
134 /* will be overriden if this is an application icon */
135 icon->core->descriptor.handle_mousedown = miniwindowMouseDown;
136 icon->core->descriptor.handle_expose = miniwindowExpose;
137 icon->core->descriptor.parent_type = WCLASS_MINIWINDOW;
138 icon->core->descriptor.parent = icon;
140 icon->core->stacking = wmalloc(sizeof(WStacking));
141 icon->core->stacking->above = NULL;
142 icon->core->stacking->under = NULL;
143 icon->core->stacking->window_level = NORMAL_ICON_LEVEL;
144 icon->core->stacking->child_of = NULL;
146 icon->owner = wwin;
147 if (wwin->wm_hints && (wwin->wm_hints->flags & IconWindowHint)) {
148 if (wwin->client_win == wwin->main_window) {
149 WApplication *wapp;
150 /* do not let miniwindow steal app-icon's icon window */
151 wapp = wApplicationOf(wwin->client_win);
152 if (!wapp || wapp->app_icon==NULL)
153 icon->icon_win = wwin->wm_hints->icon_window;
154 } else {
155 icon->icon_win = wwin->wm_hints->icon_window;
158 #ifdef NO_MINIWINDOW_TITLES
159 icon->show_title = 0;
160 #else
161 icon->show_title = 1;
162 #endif
163 icon->image = wDefaultGetImage(scr, wwin->wm_instance, wwin->wm_class);
165 file = wDefaultGetIconFile(scr, wwin->wm_instance, wwin->wm_class,
166 False);
167 if (file) {
168 icon->file = wstrdup(file);
171 wGetIconName(dpy, wwin->client_win, &icon->icon_name);
173 icon->tile_type = TILE_NORMAL;
175 wIconUpdate(icon);
177 XFlush(dpy);
179 WMAddNotificationObserver(appearanceObserver, icon,
180 WNIconAppearanceSettingsChanged, icon);
181 WMAddNotificationObserver(tileObserver, icon,
182 WNIconTileSettingsChanged, icon);
183 return icon;
187 WIcon*
188 wIconCreateWithIconFile(WScreen *scr, char *iconfile, int tile)
190 WIcon *icon;
191 unsigned long vmask = 0;
192 XSetWindowAttributes attribs;
194 icon = wmalloc(sizeof(WIcon));
195 memset(icon, 0, sizeof(WIcon));
196 icon->core = wCoreCreateTopLevel(scr, 0, 0, wPreferences.icon_size,
197 wPreferences.icon_size, 0);
198 if (wPreferences.use_saveunders) {
199 vmask = CWSaveUnder;
200 attribs.save_under = True;
202 /* a white border for selecting it */
203 vmask |= CWBorderPixel;
204 attribs.border_pixel = scr->white_pixel;
206 XChangeWindowAttributes(dpy, icon->core->window, vmask, &attribs);
208 /* will be overriden if this is a application icon */
209 icon->core->descriptor.handle_mousedown = miniwindowMouseDown;
210 icon->core->descriptor.handle_expose = miniwindowExpose;
211 icon->core->descriptor.parent_type = WCLASS_MINIWINDOW;
212 icon->core->descriptor.parent = icon;
214 icon->core->stacking = wmalloc(sizeof(WStacking));
215 icon->core->stacking->above = NULL;
216 icon->core->stacking->under = NULL;
217 icon->core->stacking->window_level = NORMAL_ICON_LEVEL;
218 icon->core->stacking->child_of = NULL;
220 if (iconfile) {
221 icon->image = RLoadImage(scr->rcontext, iconfile, 0);
222 if (!icon->image) {
223 wwarning(_("error loading image file \"%s\""), iconfile, RMessageForError(RErrorCode));
226 icon->image = wIconValidateIconSize(scr, icon->image);
228 icon->file = wstrdup(iconfile);
231 icon->tile_type = tile;
233 wIconUpdate(icon);
235 WMAddNotificationObserver(tileObserver, icon,
236 WNIconTileSettingsChanged, icon);
238 return icon;
243 void
244 wIconDestroy(WIcon *icon)
246 WCoreWindow *core = icon->core;
247 WScreen *scr = core->screen_ptr;
249 WMRemoveNotificationObserver(icon);
251 if (icon->handlerID)
252 WMDeleteTimerHandler(icon->handlerID);
254 if (icon->icon_win) {
255 int x=0, y=0;
257 if (icon->owner) {
258 x = icon->owner->icon_x;
259 y = icon->owner->icon_y;
261 XUnmapWindow(dpy, icon->icon_win);
262 XReparentWindow(dpy, icon->icon_win, scr->root_win, x, y);
264 if (icon->icon_name)
265 XFree(icon->icon_name);
267 if (icon->pixmap)
268 XFreePixmap(dpy, icon->pixmap);
270 if (icon->file)
271 free(icon->file);
273 if (icon->image!=NULL)
274 RDestroyImage(icon->image);
276 wCoreDestroy(icon->core);
277 free(icon);
282 static void
283 drawIconTitle(WScreen *scr, Pixmap pixmap, int height)
285 XFillRectangle(dpy, pixmap, scr->icon_title_texture->normal_gc,
286 0, 0, wPreferences.icon_size, height+1);
287 XDrawLine(dpy, pixmap, scr->icon_title_texture->light_gc, 0, 0,
288 wPreferences.icon_size, 0);
289 XDrawLine(dpy, pixmap, scr->icon_title_texture->light_gc, 0, 0,
290 0, height+1);
291 XDrawLine(dpy, pixmap, scr->icon_title_texture->dim_gc,
292 wPreferences.icon_size-1, 0, wPreferences.icon_size-1, height+1);
296 static Pixmap
297 makeIcon(WScreen *scr, RImage *icon, int titled, int shadowed, int tileType)
299 RImage *tile;
300 Pixmap pixmap;
301 int x, y, sx, sy;
302 unsigned w, h;
303 int theight = scr->icon_title_font->height;
305 if (tileType == TILE_NORMAL)
306 tile = RCloneImage(scr->icon_tile);
307 else {
308 assert(scr->clip_tile);
309 tile = RCloneImage(scr->clip_tile);
311 if (icon) {
312 w = (icon->width > wPreferences.icon_size)
313 ? wPreferences.icon_size : icon->width;
314 x = (wPreferences.icon_size - w) / 2;
315 sx = (icon->width - w)/2;
317 if (!titled) {
318 h = (icon->height > wPreferences.icon_size)
319 ? wPreferences.icon_size : icon->height;
320 y = (wPreferences.icon_size - h) / 2;
321 sy = (icon->height - h)/2;
322 } else {
323 h = (icon->height+theight > wPreferences.icon_size
324 ? wPreferences.icon_size-theight : icon->height);
325 y = theight+((int)wPreferences.icon_size-theight-h)/2;
326 sy = (icon->height - h)/2;
328 RCombineArea(tile, icon, sx, sy, w, h, x, y);
331 if (shadowed) {
332 RColor color;
334 color.red = scr->icon_back_texture->light.red >> 8;
335 color.green = scr->icon_back_texture->light.green >> 8;
336 color.blue = scr->icon_back_texture->light.blue >> 8;
337 color.alpha = 150; /* about 60% */
338 RClearImage(tile, &color);
341 if (!RConvertImage(scr->rcontext, tile, &pixmap)) {
342 wwarning(_("error rendering image:%s"), RMessageForError(RErrorCode));
344 RDestroyImage(tile);
346 if (titled)
347 drawIconTitle(scr, pixmap, theight);
349 return pixmap;
353 void
354 wIconChangeTitle(WIcon *icon, char *new_title)
356 int changed;
358 changed = (new_title==NULL && icon->icon_name!=NULL)
359 || (new_title!=NULL && icon->icon_name==NULL);
361 if (icon->icon_name!=NULL)
362 XFree(icon->icon_name);
364 icon->icon_name = new_title;
366 if (changed)
367 icon->force_paint = 1;
368 wIconPaint(icon);
372 void
373 wIconChangeImage(WIcon *icon, RImage *new_image)
375 assert(icon != NULL);
377 if (icon->image)
378 RDestroyImage(icon->image);
380 icon->image = wIconValidateIconSize(icon->core->screen_ptr, new_image);
382 wIconUpdate(icon);
386 RImage*
387 wIconValidateIconSize(WScreen *scr, RImage *icon)
389 RImage *tmp;
390 int w, h;
392 if (!icon)
393 return NULL;
395 if (icon->width > wPreferences.icon_size
396 || icon->height > wPreferences.icon_size) {
397 if (icon->width > icon->height) {
398 w = wPreferences.icon_size - 4;
399 h = w*icon->height/icon->width;
400 } else {
401 h = wPreferences.icon_size - 4;
402 w = h*icon->width/icon->height;
404 tmp = RScaleImage(icon, w, h);
405 RDestroyImage(icon);
406 icon = tmp;
409 return icon;
413 Bool
414 wIconChangeImageFile(WIcon *icon, char *file)
416 WScreen *scr = icon->core->screen_ptr;
417 RImage *image;
418 char *path;
419 int error = 0;
421 if (!file) {
422 wIconChangeImage(icon, NULL);
423 return True;
426 path = FindImage(wPreferences.icon_path, file);
428 if (path && (image = RLoadImage(scr->rcontext, path, 0))) {
429 wIconChangeImage(icon, image);
430 } else {
431 error = 1;
434 if (path)
435 free(path);
437 return !error;
442 static char*
443 getnameforicon(WWindow *wwin)
445 char *prefix, *suffix;
446 char *path;
447 int len;
449 if (wwin->wm_class && wwin->wm_instance) {
450 suffix = wmalloc(strlen(wwin->wm_class)+strlen(wwin->wm_instance)+2);
451 sprintf(suffix, "%s.%s", wwin->wm_instance, wwin->wm_class);
452 } else if (wwin->wm_class) {
453 suffix = wmalloc(strlen(wwin->wm_class)+1);
454 strcpy(suffix, wwin->wm_class);
455 } else if (wwin->wm_instance) {
456 suffix = wmalloc(strlen(wwin->wm_instance)+1);
457 strcpy(suffix, wwin->wm_instance);
458 } else {
459 return NULL;
462 prefix = wusergnusteppath();
463 len = strlen(prefix)+64+strlen(suffix);
464 path = wmalloc(len+1);
465 sprintf(path, "%s/.AppInfo", prefix);
467 if (access(path, F_OK)!=0) {
468 if (mkdir(path, S_IRUSR|S_IWUSR|S_IXUSR)) {
469 wsyserror(_("could not create directory %s"), path);
470 free(path);
471 free(suffix);
472 return NULL;
475 strcat(path, "/WindowMaker");
476 if (access(path, F_OK)!=0) {
477 if (mkdir(path, S_IRUSR|S_IWUSR|S_IXUSR)!=0) {
478 wsyserror(_("could not create directory %s"), path);
479 free(path);
480 free(suffix);
481 return NULL;
485 strcat(path, "/");
486 strcat(path, suffix);
487 strcat(path, ".xpm");
488 free(suffix);
490 return path;
495 * wIconStore--
496 * Stores the client supplied icon at ~/GNUstep/.AppInfo/WindowMaker
497 * and returns the path for that icon. Returns NULL if there is no
498 * client supplied icon or on failure.
500 * Side effects:
501 * New directories might be created.
503 char*
504 wIconStore(WIcon *icon)
506 char *path;
507 RImage *image;
508 WWindow *wwin = icon->owner;
510 if (!wwin || !wwin->wm_hints || !(wwin->wm_hints->flags & IconPixmapHint)
511 || wwin->wm_hints->icon_pixmap == None)
512 return NULL;
514 path = getnameforicon(wwin);
515 if (!path)
516 return NULL;
518 image = RCreateImageFromDrawable(icon->core->screen_ptr->rcontext,
519 wwin->wm_hints->icon_pixmap,
520 (wwin->wm_hints->flags & IconMaskHint)
521 ? wwin->wm_hints->icon_mask : None);
522 if (!image) {
523 free(path);
524 return NULL;
527 if (!RSaveImage(image, path, "XPM")) {
528 free(path);
529 path = NULL;
531 RDestroyImage(image);
533 return path;
538 void
539 wIconChangeIconWindow(WIcon *icon, Window new_window);
542 static void
543 cycleColor(void *data)
545 WIcon *icon = (WIcon*)data;
546 WScreen *scr = icon->core->screen_ptr;
547 XGCValues gcv;
549 icon->step--;
550 gcv.dash_offset = icon->step;
551 XChangeGC(dpy, scr->icon_select_gc, GCDashOffset, &gcv);
553 XDrawRectangle(dpy, icon->core->window, scr->icon_select_gc, 0, 0,
554 icon->core->width-1, icon->core->height-1);
555 icon->handlerID = WMAddTimerHandler(COLOR_CYCLE_DELAY, cycleColor, icon);
559 void
560 wIconSetHighlited(WIcon *icon, Bool flag)
562 if (icon->highlighted == flag) {
563 return;
566 icon->highlighted = flag;
567 wIconPaint(icon);
572 void
573 wIconSelect(WIcon *icon)
575 WScreen *scr = icon->core->screen_ptr;
576 icon->selected = !icon->selected;
578 if (icon->selected) {
579 icon->step = 0;
580 if (!wPreferences.dont_blink)
581 icon->handlerID = WMAddTimerHandler(10, cycleColor, icon);
582 else
583 XDrawRectangle(dpy, icon->core->window, scr->icon_select_gc, 0, 0,
584 icon->core->width-1, icon->core->height-1);
585 } else {
586 if (icon->handlerID) {
587 WMDeleteTimerHandler(icon->handlerID);
588 icon->handlerID = NULL;
590 XClearArea(dpy, icon->core->window, 0, 0, icon->core->width,
591 icon->core->height, True);
596 void
597 wIconUpdate(WIcon *icon)
599 WScreen *scr = icon->core->screen_ptr;
600 int title_height = scr->icon_title_font->height;
601 WWindow *wwin = icon->owner;
603 assert(scr->icon_tile!=NULL);
605 if (icon->pixmap!=None)
606 XFreePixmap(dpy, icon->pixmap);
607 icon->pixmap = None;
610 if (wwin && WFLAGP(wwin, always_user_icon))
611 goto user_icon;
613 /* use client specified icon window */
614 if (icon->icon_win!=None) {
615 XWindowAttributes attr;
616 int resize=0;
617 int width, height, depth;
618 int theight;
619 Pixmap pixmap;
621 getSize(icon->icon_win, &width, &height, &depth);
623 if (width > wPreferences.icon_size) {
624 resize = 1;
625 width = wPreferences.icon_size;
627 if (height > wPreferences.icon_size) {
628 resize = 1;
629 height = wPreferences.icon_size;
631 if (icon->show_title
632 && (height+title_height < wPreferences.icon_size)) {
633 pixmap = XCreatePixmap(dpy, scr->w_win, wPreferences.icon_size,
634 wPreferences.icon_size, scr->w_depth);
635 XSetClipMask(dpy, scr->copy_gc, None);
636 XCopyArea(dpy, scr->icon_tile_pixmap, pixmap, scr->copy_gc, 0, 0,
637 wPreferences.icon_size, wPreferences.icon_size, 0, 0);
638 drawIconTitle(scr, pixmap, title_height);
639 theight = title_height;
640 } else {
641 pixmap = None;
642 theight = 0;
643 XSetWindowBackgroundPixmap(dpy, icon->core->window,
644 scr->icon_tile_pixmap);
647 XSetWindowBorderWidth(dpy, icon->icon_win, 0);
648 XReparentWindow(dpy, icon->icon_win, icon->core->window,
649 (wPreferences.icon_size-width)/2,
650 theight+(wPreferences.icon_size-height-theight)/2);
651 if (resize)
652 XResizeWindow(dpy, icon->icon_win, width, height);
654 XMapWindow(dpy, icon->icon_win);
656 XAddToSaveSet(dpy, icon->icon_win);
658 icon->pixmap = pixmap;
660 if (XGetWindowAttributes(dpy, icon->icon_win, &attr)) {
661 if (attr.all_event_masks & ButtonPressMask) {
662 wHackedGrabButton(Button1, MOD_MASK, icon->core->window, True,
663 ButtonPressMask, GrabModeSync, GrabModeAsync,
664 None, wCursor[WCUR_ARROW]);
667 } else if (wwin && wwin->wm_hints
668 && (wwin->wm_hints->flags & IconPixmapHint)) {
669 int x, y;
670 unsigned int w, h;
671 Window jw;
672 int ji, dotitle;
673 unsigned int ju, d;
674 Pixmap pixmap;
676 if (!XGetGeometry(dpy, wwin->wm_hints->icon_pixmap, &jw,
677 &ji, &ji, &w, &h, &ju, &d)) {
678 icon->owner->wm_hints->flags &= ~IconPixmapHint;
679 goto user_icon;
682 pixmap = XCreatePixmap(dpy, icon->core->window, wPreferences.icon_size,
683 wPreferences.icon_size, scr->w_depth);
684 XSetClipMask(dpy, scr->copy_gc, None);
685 XCopyArea(dpy, scr->icon_tile_pixmap, pixmap, scr->copy_gc, 0, 0,
686 wPreferences.icon_size, wPreferences.icon_size, 0, 0);
688 if (w > wPreferences.icon_size)
689 w = wPreferences.icon_size;
690 x = (wPreferences.icon_size-w)/2;
692 if (icon->show_title && (title_height < wPreferences.icon_size)) {
693 drawIconTitle(scr, pixmap, title_height);
694 dotitle = 1;
696 if (h > wPreferences.icon_size - title_height - 2) {
697 h = wPreferences.icon_size - title_height - 2;
698 y = title_height + 1;
699 } else {
700 y = (wPreferences.icon_size-h-title_height)/2+title_height + 1;
702 } else {
703 dotitle = 0;
704 if (w > wPreferences.icon_size)
705 w = wPreferences.icon_size;
706 y = (wPreferences.icon_size-h)/2;
709 if (wwin->wm_hints->flags & IconMaskHint)
710 XSetClipMask(dpy, scr->copy_gc, wwin->wm_hints->icon_mask);
712 XSetClipOrigin(dpy, scr->copy_gc, x, y);
714 if (d != scr->w_depth) {
715 XSetForeground(dpy, scr->copy_gc, scr->black_pixel);
716 XSetBackground(dpy, scr->copy_gc, scr->white_pixel);
717 XCopyPlane(dpy, wwin->wm_hints->icon_pixmap, pixmap, scr->copy_gc,
718 0, 0, w, h, x, y, 1);
719 } else {
720 XCopyArea(dpy, wwin->wm_hints->icon_pixmap, pixmap, scr->copy_gc,
721 0, 0, w, h, x, y);
724 XSetClipOrigin(dpy, scr->copy_gc, 0, 0);
726 icon->pixmap = pixmap;
727 } else {
728 user_icon:
730 if (icon->image) {
731 icon->pixmap = makeIcon(scr, icon->image, icon->show_title,
732 icon->shadowed, icon->tile_type);
733 } else {
734 /* make default icons */
736 if (!scr->def_icon_pixmap) {
737 RImage *image = NULL;
738 char *path;
739 char *file;
741 file = wDefaultGetIconFile(scr, NULL, NULL, False);
742 if (file) {
743 path = FindImage(wPreferences.icon_path, file);
744 if (!path) {
745 wwarning(_("could not find default icon \"%s\""),file);
746 goto make_icons;
749 image = RLoadImage(scr->rcontext, path, 0);
750 if (!image) {
751 wwarning(_("could not load default icon \"%s\":%s"),
752 file, RMessageForError(RErrorCode));
754 free(path);
756 make_icons:
758 image = wIconValidateIconSize(scr, image);
759 scr->def_icon_pixmap = makeIcon(scr, image, False, False,
760 icon->tile_type);
761 scr->def_ticon_pixmap = makeIcon(scr, image, True, False,
762 icon->tile_type);
763 if (image)
764 RDestroyImage(image);
767 if (icon->show_title) {
768 XSetWindowBackgroundPixmap(dpy, icon->core->window,
769 scr->def_ticon_pixmap);
770 } else {
771 XSetWindowBackgroundPixmap(dpy, icon->core->window,
772 scr->def_icon_pixmap);
774 icon->pixmap = None;
777 if (icon->pixmap != None) {
778 XSetWindowBackgroundPixmap(dpy, icon->core->window, icon->pixmap);
780 XClearWindow(dpy, icon->core->window);
782 wIconPaint(icon);
787 void
788 wIconPaint(WIcon *icon)
790 WScreen *scr=icon->core->screen_ptr;
791 GC gc = scr->icon_title_gc;
792 int x;
793 char *tmp;
795 if (icon->force_paint) {
796 icon->force_paint = 0;
797 wIconUpdate(icon);
798 return;
801 XClearWindow(dpy, icon->core->window);
803 /* draw the icon title */
804 if (icon->show_title && icon->icon_name!=NULL) {
805 int l;
806 int w;
808 tmp = ShrinkString(scr->icon_title_font, icon->icon_name,
809 wPreferences.icon_size-4);
810 w = wTextWidth(scr->icon_title_font->font, tmp, l=strlen(tmp));
812 if (w > icon->core->width - 4)
813 x = (icon->core->width - 4) - w;
814 else
815 x = (icon->core->width - w)/2;
817 wDrawString(icon->core->window, scr->icon_title_font, gc,
818 x, 1 + scr->icon_title_font->y, tmp, l);
819 free(tmp);
822 if (icon->selected)
823 XDrawRectangle(dpy, icon->core->window, scr->icon_select_gc, 0, 0,
824 icon->core->width-1, icon->core->height-1);
828 /******************************************************************/
830 static void
831 miniwindowExpose(WObjDescriptor *desc, XEvent *event)
833 wIconPaint(desc->parent);
837 static void
838 miniwindowDblClick(WObjDescriptor *desc, XEvent *event)
840 WIcon *icon = desc->parent;
842 assert(icon->owner!=NULL);
844 wDeiconifyWindow(icon->owner);
848 static void
849 miniwindowMouseDown(WObjDescriptor *desc, XEvent *event)
851 WIcon *icon = desc->parent;
852 WWindow *wwin = icon->owner;
853 XEvent ev;
854 int x=wwin->icon_x, y=wwin->icon_y;
855 int dx=event->xbutton.x, dy=event->xbutton.y;
856 int grabbed=0;
857 int clickButton=event->xbutton.button;
859 if (WCHECK_STATE(WSTATE_MODAL))
860 return;
862 if (IsDoubleClick(icon->core->screen_ptr, event)) {
863 miniwindowDblClick(desc, event);
864 return;
867 #ifdef DEBUG
868 puts("Moving miniwindow");
869 #endif
870 if (event->xbutton.button == Button1) {
871 if (event->xbutton.state & MOD_MASK)
872 wLowerFrame(icon->core);
873 else
874 wRaiseFrame(icon->core);
875 if (event->xbutton.state & ShiftMask) {
876 wIconSelect(icon);
877 wSelectWindow(icon->owner, !wwin->flags.selected);
879 } else if (event->xbutton.button == Button3) {
880 WObjDescriptor *desc;
882 OpenMiniwindowMenu(wwin, event->xbutton.x_root,
883 event->xbutton.y_root);
885 /* allow drag select of menu */
886 desc = &wwin->screen_ptr->window_menu->menu->descriptor;
887 event->xbutton.send_event = True;
888 (*desc->handle_mousedown)(desc, event);
890 return;
893 if (XGrabPointer(dpy, icon->core->window, False, ButtonMotionMask
894 |ButtonReleaseMask|ButtonPressMask, GrabModeAsync,
895 GrabModeAsync, None, None, CurrentTime) !=GrabSuccess) {
896 #ifdef DEBUG0
897 wwarning("pointer grab failed for icon move");
898 #endif
900 while(1) {
901 WMMaskEvent(dpy, PointerMotionMask|ButtonReleaseMask|ButtonPressMask
902 |ButtonMotionMask|ExposureMask, &ev);
903 switch (ev.type) {
904 case Expose:
905 WMHandleEvent(&ev);
906 break;
908 case MotionNotify:
909 if (!grabbed) {
910 if (abs(dx-ev.xmotion.x)>=MOVE_THRESHOLD
911 || abs(dy-ev.xmotion.y)>=MOVE_THRESHOLD) {
912 XChangeActivePointerGrab(dpy, ButtonMotionMask
913 |ButtonReleaseMask|ButtonPressMask,
914 wCursor[WCUR_MOVE], CurrentTime);
915 grabbed=1;
916 } else {
917 break;
920 x = ev.xmotion.x_root - dx;
921 y = ev.xmotion.y_root - dy;
922 XMoveWindow(dpy, icon->core->window, x, y);
923 break;
925 case ButtonPress:
926 break;
928 case ButtonRelease:
929 if (ev.xbutton.button != clickButton)
930 break;
932 if (wwin->icon_x!=x || wwin->icon_y!=y)
933 wwin->flags.icon_moved = 1;
935 XMoveWindow(dpy, icon->core->window, x, y);
937 wwin->icon_x = x;
938 wwin->icon_y = y;
939 #ifdef DEBUG
940 puts("End miniwindow move");
941 #endif
942 XUngrabPointer(dpy, CurrentTime);
944 if (wPreferences.auto_arrange_icons)
945 wArrangeIcons(wwin->screen_ptr, True);
946 return;