Use dh_lintian.
[wmaker-crm.git] / src / icon.c
blobc78154210aaf27117f101ebb219152003b4d737c
1 /* icon.c - window icon and dock and appicon parent
3 * Window Maker window manager
5 * Copyright (c) 1997-2003 Alfredo K. Kojima
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
20 * USA.
23 #include "wconfig.h"
25 #include <X11/Xlib.h>
26 #include <X11/Xutil.h>
27 #include <stdlib.h>
28 #include <stdio.h>
29 #include <stdint.h>
30 #include <string.h>
31 #include <unistd.h>
32 #include <ctype.h>
33 #include <wraster.h>
34 #include <sys/stat.h>
36 #include "WindowMaker.h"
37 #include "wcore.h"
38 #include "texture.h"
39 #include "window.h"
40 #include "icon.h"
41 #include "actions.h"
42 #include "funcs.h"
43 #include "stacking.h"
44 #include "application.h"
45 #include "defaults.h"
46 #include "appicon.h"
47 #include "wmspec.h"
49 /**** Global varianebles ****/
50 extern WPreferences wPreferences;
52 #define MOD_MASK wPreferences.modifier_mask
54 extern Cursor wCursor[WCUR_LAST];
56 static void miniwindowExpose(WObjDescriptor * desc, XEvent * event);
57 static void miniwindowMouseDown(WObjDescriptor * desc, XEvent * event);
58 static void miniwindowDblClick(WObjDescriptor * desc, XEvent * event);
60 /****** Notification Observers ******/
62 static void appearanceObserver(void *self, WMNotification * notif)
64 WIcon *icon = (WIcon *) self;
65 uintptr_t flags = (uintptr_t)WMGetNotificationClientData(notif);
67 if (flags & WTextureSettings) {
68 icon->force_paint = 1;
70 if (flags & WFontSettings) {
71 icon->force_paint = 1;
74 if (flags & WColorSettings) {
78 wIconPaint(icon);
80 /* so that the appicon expose handlers will paint the appicon specific
81 * stuff */
82 XClearArea(dpy, icon->core->window, 0, 0, icon->core->width, icon->core->height, True);
85 static void tileObserver(void *self, WMNotification * notif)
87 WIcon *icon = (WIcon *) self;
89 icon->force_paint = 1;
90 wIconPaint(icon);
92 XClearArea(dpy, icon->core->window, 0, 0, 1, 1, True);
95 /************************************/
97 INLINE static void getSize(Drawable d, unsigned int *w, unsigned int *h, unsigned int *dep)
99 Window rjunk;
100 int xjunk, yjunk;
101 unsigned int bjunk;
103 XGetGeometry(dpy, d, &rjunk, &xjunk, &yjunk, w, h, &bjunk, dep);
106 WIcon *wIconCreate(WWindow * wwin)
108 WScreen *scr = wwin->screen_ptr;
109 WIcon *icon;
110 char *file;
111 unsigned long vmask = 0;
112 XSetWindowAttributes attribs;
114 icon = wmalloc(sizeof(WIcon));
115 memset(icon, 0, sizeof(WIcon));
116 icon->core = wCoreCreateTopLevel(scr, wwin->icon_x, wwin->icon_y,
117 wPreferences.icon_size, wPreferences.icon_size, 0);
119 if (wPreferences.use_saveunders) {
120 vmask |= CWSaveUnder;
121 attribs.save_under = True;
123 /* a white border for selecting it */
124 vmask |= CWBorderPixel;
125 attribs.border_pixel = scr->white_pixel;
127 XChangeWindowAttributes(dpy, icon->core->window, vmask, &attribs);
129 /* will be overriden if this is an application icon */
130 icon->core->descriptor.handle_mousedown = miniwindowMouseDown;
131 icon->core->descriptor.handle_expose = miniwindowExpose;
132 icon->core->descriptor.parent_type = WCLASS_MINIWINDOW;
133 icon->core->descriptor.parent = icon;
135 icon->core->stacking = wmalloc(sizeof(WStacking));
136 icon->core->stacking->above = NULL;
137 icon->core->stacking->under = NULL;
138 icon->core->stacking->window_level = NORMAL_ICON_LEVEL;
139 icon->core->stacking->child_of = NULL;
141 icon->owner = wwin;
142 if (wwin->wm_hints && (wwin->wm_hints->flags & IconWindowHint)) {
143 if (wwin->client_win == wwin->main_window) {
144 WApplication *wapp;
145 /* do not let miniwindow steal app-icon's icon window */
146 wapp = wApplicationOf(wwin->client_win);
147 if (!wapp || wapp->app_icon == NULL)
148 icon->icon_win = wwin->wm_hints->icon_window;
149 } else {
150 icon->icon_win = wwin->wm_hints->icon_window;
153 #ifdef NO_MINIWINDOW_TITLES
154 icon->show_title = 0;
155 #else
156 icon->show_title = 1;
157 #endif
158 if (!icon->image && !WFLAGP(wwin, always_user_icon))
159 icon->image = RRetainImage(wwin->net_icon_image);
160 if (!icon->image)
161 icon->image = wDefaultGetImage(scr, wwin->wm_instance, wwin->wm_class);
163 file = wDefaultGetIconFile(scr, wwin->wm_instance, wwin->wm_class, False);
164 if (file) {
165 icon->file = wstrdup(file);
168 icon->icon_name = wNETWMGetIconName(wwin->client_win);
169 if (icon->icon_name)
170 wwin->flags.net_has_icon_title = 1;
171 else
172 wGetIconName(dpy, wwin->client_win, &icon->icon_name);
174 icon->tile_type = TILE_NORMAL;
176 wIconUpdate(icon);
178 XFlush(dpy);
180 WMAddNotificationObserver(appearanceObserver, icon, WNIconAppearanceSettingsChanged, icon);
181 WMAddNotificationObserver(tileObserver, icon, WNIconTileSettingsChanged, icon);
182 return icon;
185 WIcon *wIconCreateWithIconFile(WScreen * scr, char *iconfile, int tile)
187 WIcon *icon;
188 unsigned long vmask = 0;
189 XSetWindowAttributes attribs;
191 icon = wmalloc(sizeof(WIcon));
192 memset(icon, 0, sizeof(WIcon));
193 icon->core = wCoreCreateTopLevel(scr, 0, 0, wPreferences.icon_size, wPreferences.icon_size, 0);
194 if (wPreferences.use_saveunders) {
195 vmask = CWSaveUnder;
196 attribs.save_under = True;
198 /* a white border for selecting it */
199 vmask |= CWBorderPixel;
200 attribs.border_pixel = scr->white_pixel;
202 XChangeWindowAttributes(dpy, icon->core->window, vmask, &attribs);
204 /* will be overriden if this is a application icon */
205 icon->core->descriptor.handle_mousedown = miniwindowMouseDown;
206 icon->core->descriptor.handle_expose = miniwindowExpose;
207 icon->core->descriptor.parent_type = WCLASS_MINIWINDOW;
208 icon->core->descriptor.parent = icon;
210 icon->core->stacking = wmalloc(sizeof(WStacking));
211 icon->core->stacking->above = NULL;
212 icon->core->stacking->under = NULL;
213 icon->core->stacking->window_level = NORMAL_ICON_LEVEL;
214 icon->core->stacking->child_of = NULL;
216 if (iconfile) {
217 icon->image = RLoadImage(scr->rcontext, iconfile, 0);
218 if (!icon->image) {
219 wwarning(_("error loading image file \"%s\": %s"), iconfile, RMessageForError(RErrorCode));
222 icon->image = wIconValidateIconSize(scr, icon->image);
224 icon->file = wstrdup(iconfile);
227 icon->tile_type = tile;
229 wIconUpdate(icon);
231 WMAddNotificationObserver(appearanceObserver, icon, WNIconAppearanceSettingsChanged, icon);
232 WMAddNotificationObserver(tileObserver, icon, WNIconTileSettingsChanged, icon);
234 return icon;
237 void wIconDestroy(WIcon * icon)
239 WCoreWindow *core = icon->core;
240 WScreen *scr = core->screen_ptr;
242 WMRemoveNotificationObserver(icon);
244 if (icon->handlerID)
245 WMDeleteTimerHandler(icon->handlerID);
247 if (icon->icon_win) {
248 int x = 0, y = 0;
250 if (icon->owner) {
251 x = icon->owner->icon_x;
252 y = icon->owner->icon_y;
254 XUnmapWindow(dpy, icon->icon_win);
255 XReparentWindow(dpy, icon->icon_win, scr->root_win, x, y);
257 if (icon->icon_name)
258 XFree(icon->icon_name);
260 if (icon->pixmap)
261 XFreePixmap(dpy, icon->pixmap);
263 if (icon->file)
264 wfree(icon->file);
266 if (icon->image != NULL)
267 RReleaseImage(icon->image);
269 wCoreDestroy(icon->core);
270 wfree(icon);
273 static void drawIconTitle(WScreen * scr, Pixmap pixmap, int height)
275 XFillRectangle(dpy, pixmap, scr->icon_title_texture->normal_gc, 0, 0, wPreferences.icon_size, height + 1);
276 XDrawLine(dpy, pixmap, scr->icon_title_texture->light_gc, 0, 0, wPreferences.icon_size, 0);
277 XDrawLine(dpy, pixmap, scr->icon_title_texture->light_gc, 0, 0, 0, height + 1);
278 XDrawLine(dpy, pixmap, scr->icon_title_texture->dim_gc,
279 wPreferences.icon_size - 1, 0, wPreferences.icon_size - 1, height + 1);
282 static Pixmap makeIcon(WScreen * scr, RImage * icon, int titled, int shadowed, int tileType)
284 RImage *tile;
285 Pixmap pixmap;
286 int x, y, sx, sy;
287 unsigned w, h;
288 int theight = WMFontHeight(scr->icon_title_font);
290 if (tileType == TILE_NORMAL)
291 tile = RCloneImage(scr->icon_tile);
292 else {
293 assert(scr->clip_tile);
294 tile = RCloneImage(scr->clip_tile);
296 if (icon) {
297 w = (icon->width > wPreferences.icon_size)
298 ? wPreferences.icon_size : icon->width;
299 x = (wPreferences.icon_size - w) / 2;
300 sx = (icon->width - w) / 2;
302 if (!titled) {
303 h = (icon->height > wPreferences.icon_size)
304 ? wPreferences.icon_size : icon->height;
305 y = (wPreferences.icon_size - h) / 2;
306 sy = (icon->height - h) / 2;
307 } else {
308 h = (icon->height + theight > wPreferences.icon_size
309 ? wPreferences.icon_size - theight : icon->height);
310 y = theight + ((int)wPreferences.icon_size - theight - h) / 2;
311 sy = (icon->height - h) / 2;
313 RCombineArea(tile, icon, sx, sy, w, h, x, y);
316 if (shadowed) {
317 RColor color;
319 color.red = scr->icon_back_texture->light.red >> 8;
320 color.green = scr->icon_back_texture->light.green >> 8;
321 color.blue = scr->icon_back_texture->light.blue >> 8;
322 color.alpha = 150; /* about 60% */
323 RClearImage(tile, &color);
326 if (!RConvertImage(scr->rcontext, tile, &pixmap)) {
327 wwarning(_("error rendering image:%s"), RMessageForError(RErrorCode));
329 RReleaseImage(tile);
331 if (titled)
332 drawIconTitle(scr, pixmap, theight);
334 return pixmap;
337 void wIconChangeTitle(WIcon * icon, char *new_title)
339 int changed;
341 changed = (new_title == NULL && icon->icon_name != NULL)
342 || (new_title != NULL && icon->icon_name == NULL);
344 if (icon->icon_name != NULL)
345 XFree(icon->icon_name);
347 icon->icon_name = new_title;
349 if (changed)
350 icon->force_paint = 1;
351 wIconPaint(icon);
354 void wIconChangeImage(WIcon * icon, RImage * new_image)
356 assert(icon != NULL);
358 if (icon->image)
359 RReleaseImage(icon->image);
361 icon->image = new_image;
363 wIconUpdate(icon);
366 RImage *wIconValidateIconSize(WScreen * scr, RImage * icon)
368 RImage *tmp;
369 int w, h;
371 if (!icon)
372 return NULL;
373 #ifndef DONT_SCALE_ICONS
374 if (wPreferences.icon_size != 64) {
375 w = wPreferences.icon_size * icon->width / 64;
376 h = wPreferences.icon_size * icon->height / 64;
378 tmp = RScaleImage(icon, w, h);
379 RReleaseImage(icon);
380 icon = tmp;
382 #endif
384 return icon;
387 Bool wIconChangeImageFile(WIcon * icon, char *file)
389 WScreen *scr = icon->core->screen_ptr;
390 RImage *image;
391 char *path;
392 int error = 0;
394 if (!file) {
395 wIconChangeImage(icon, NULL);
396 return True;
399 path = FindImage(wPreferences.icon_path, file);
401 if (path && (image = RLoadImage(scr->rcontext, path, 0))) {
402 image = wIconValidateIconSize(icon->core->screen_ptr, image);
404 wIconChangeImage(icon, image);
405 } else {
406 error = 1;
409 if (path)
410 wfree(path);
412 return !error;
415 static char *getnameforicon(WWindow * wwin)
417 char *prefix, *suffix;
418 char *path;
419 int len;
421 if (wwin->wm_class && wwin->wm_instance) {
422 int len = strlen(wwin->wm_class) + strlen(wwin->wm_instance) + 2;
423 suffix = wmalloc(len);
424 snprintf(suffix, len, "%s.%s", wwin->wm_instance, wwin->wm_class);
425 } else if (wwin->wm_class) {
426 int len = strlen(wwin->wm_class) + 1;
427 suffix = wmalloc(len);
428 snprintf(suffix, len, "%s", wwin->wm_class);
429 } else if (wwin->wm_instance) {
430 int len = strlen(wwin->wm_instance) + 1;
431 suffix = wmalloc(len);
432 snprintf(suffix, len, "%s", wwin->wm_instance);
433 } else {
434 return NULL;
437 prefix = wusergnusteppath();
438 len = strlen(prefix) + 64 + strlen(suffix);
439 path = wmalloc(len + 1);
440 snprintf(path, len, "%s/Library/WindowMaker", prefix);
442 if (access(path, F_OK) != 0) {
443 if (mkdir(path, S_IRUSR | S_IWUSR | S_IXUSR)) {
444 wsyserror(_("could not create directory %s"), path);
445 wfree(path);
446 wfree(suffix);
447 return NULL;
450 strcat(path, "/CachedPixmaps");
451 if (access(path, F_OK) != 0) {
452 if (mkdir(path, S_IRUSR | S_IWUSR | S_IXUSR) != 0) {
453 wsyserror(_("could not create directory %s"), path);
454 wfree(path);
455 wfree(suffix);
456 return NULL;
460 strcat(path, "/");
461 strcat(path, suffix);
462 strcat(path, ".xpm");
463 wfree(suffix);
465 return path;
469 * wIconStore--
470 * Stores the client supplied icon at ~/GNUstep/Library/WindowMaker/CachedPixmaps
471 * and returns the path for that icon. Returns NULL if there is no
472 * client supplied icon or on failure.
474 * Side effects:
475 * New directories might be created.
477 char *wIconStore(WIcon * icon)
479 char *path;
480 RImage *image;
481 WWindow *wwin = icon->owner;
483 if (!wwin || !wwin->wm_hints || !(wwin->wm_hints->flags & IconPixmapHint)
484 || wwin->wm_hints->icon_pixmap == None)
485 return NULL;
487 path = getnameforicon(wwin);
488 if (!path)
489 return NULL;
491 image = RCreateImageFromDrawable(icon->core->screen_ptr->rcontext,
492 wwin->wm_hints->icon_pixmap, (wwin->wm_hints->flags & IconMaskHint)
493 ? wwin->wm_hints->icon_mask : None);
494 if (!image) {
495 wfree(path);
496 return NULL;
499 if (!RSaveImage(image, path, "XPM")) {
500 wfree(path);
501 path = NULL;
503 RReleaseImage(image);
505 return path;
508 static void cycleColor(void *data)
510 WIcon *icon = (WIcon *) data;
511 WScreen *scr = icon->core->screen_ptr;
512 XGCValues gcv;
514 icon->step--;
515 gcv.dash_offset = icon->step;
516 XChangeGC(dpy, scr->icon_select_gc, GCDashOffset, &gcv);
518 XDrawRectangle(dpy, icon->core->window, scr->icon_select_gc, 0, 0,
519 icon->core->width - 1, icon->core->height - 1);
520 icon->handlerID = WMAddTimerHandler(COLOR_CYCLE_DELAY, cycleColor, icon);
523 void wIconSelect(WIcon * icon)
525 WScreen *scr = icon->core->screen_ptr;
526 icon->selected = !icon->selected;
528 if (icon->selected) {
529 icon->step = 0;
530 if (!wPreferences.dont_blink)
531 icon->handlerID = WMAddTimerHandler(10, cycleColor, icon);
532 else
533 XDrawRectangle(dpy, icon->core->window, scr->icon_select_gc, 0, 0,
534 icon->core->width - 1, icon->core->height - 1);
535 } else {
536 if (icon->handlerID) {
537 WMDeleteTimerHandler(icon->handlerID);
538 icon->handlerID = NULL;
540 XClearArea(dpy, icon->core->window, 0, 0, icon->core->width, icon->core->height, True);
544 void wIconUpdate(WIcon * icon)
546 WScreen *scr = icon->core->screen_ptr;
547 int title_height = WMFontHeight(scr->icon_title_font);
548 WWindow *wwin = icon->owner;
550 assert(scr->icon_tile != NULL);
552 if (icon->pixmap != None)
553 XFreePixmap(dpy, icon->pixmap);
554 icon->pixmap = None;
556 if (wwin && (WFLAGP(wwin, always_user_icon) || wwin->net_icon_image))
557 goto user_icon;
559 /* use client specified icon window */
560 if (icon->icon_win != None) {
561 XWindowAttributes attr;
562 int resize = 0;
563 unsigned int width, height, depth;
564 int theight;
565 Pixmap pixmap;
567 getSize(icon->icon_win, &width, &height, &depth);
569 if (width > wPreferences.icon_size) {
570 resize = 1;
571 width = wPreferences.icon_size;
573 if (height > wPreferences.icon_size) {
574 resize = 1;
575 height = wPreferences.icon_size;
577 if (icon->show_title && (height + title_height < wPreferences.icon_size)) {
578 pixmap = XCreatePixmap(dpy, scr->w_win, wPreferences.icon_size,
579 wPreferences.icon_size, scr->w_depth);
580 XSetClipMask(dpy, scr->copy_gc, None);
581 XCopyArea(dpy, scr->icon_tile_pixmap, pixmap, scr->copy_gc, 0, 0,
582 wPreferences.icon_size, wPreferences.icon_size, 0, 0);
583 drawIconTitle(scr, pixmap, title_height);
584 theight = title_height;
585 } else {
586 pixmap = None;
587 theight = 0;
588 XSetWindowBackgroundPixmap(dpy, icon->core->window, scr->icon_tile_pixmap);
591 XSetWindowBorderWidth(dpy, icon->icon_win, 0);
592 XReparentWindow(dpy, icon->icon_win, icon->core->window,
593 (wPreferences.icon_size - width) / 2,
594 theight + (wPreferences.icon_size - height - theight) / 2);
595 if (resize)
596 XResizeWindow(dpy, icon->icon_win, width, height);
598 XMapWindow(dpy, icon->icon_win);
600 XAddToSaveSet(dpy, icon->icon_win);
602 icon->pixmap = pixmap;
604 if (XGetWindowAttributes(dpy, icon->icon_win, &attr)) {
605 if (attr.all_event_masks & ButtonPressMask) {
606 wHackedGrabButton(Button1, MOD_MASK, icon->core->window, True,
607 ButtonPressMask, GrabModeSync, GrabModeAsync,
608 None, wCursor[WCUR_ARROW]);
611 } else if (wwin && wwin->wm_hints && (wwin->wm_hints->flags & IconPixmapHint)) {
612 int x, y;
613 unsigned int w, h;
614 Window jw;
615 int ji, dotitle;
616 unsigned int ju, d;
617 Pixmap pixmap;
619 if (!XGetGeometry(dpy, wwin->wm_hints->icon_pixmap, &jw, &ji, &ji, &w, &h, &ju, &d)) {
620 icon->owner->wm_hints->flags &= ~IconPixmapHint;
621 goto user_icon;
624 pixmap = XCreatePixmap(dpy, icon->core->window, wPreferences.icon_size,
625 wPreferences.icon_size, scr->w_depth);
626 XSetClipMask(dpy, scr->copy_gc, None);
627 XCopyArea(dpy, scr->icon_tile_pixmap, pixmap, scr->copy_gc, 0, 0,
628 wPreferences.icon_size, wPreferences.icon_size, 0, 0);
630 if (w > wPreferences.icon_size)
631 w = wPreferences.icon_size;
632 x = (wPreferences.icon_size - w) / 2;
634 if (icon->show_title && (title_height < wPreferences.icon_size)) {
635 drawIconTitle(scr, pixmap, title_height);
636 dotitle = 1;
638 if (h > wPreferences.icon_size - title_height - 2) {
639 h = wPreferences.icon_size - title_height - 2;
640 y = title_height + 1;
641 } else {
642 y = (wPreferences.icon_size - h - title_height) / 2 + title_height + 1;
644 } else {
645 dotitle = 0;
646 if (w > wPreferences.icon_size)
647 w = wPreferences.icon_size;
648 y = (wPreferences.icon_size - h) / 2;
651 if (wwin->wm_hints->flags & IconMaskHint)
652 XSetClipMask(dpy, scr->copy_gc, wwin->wm_hints->icon_mask);
654 XSetClipOrigin(dpy, scr->copy_gc, x, y);
656 if (d != scr->w_depth) {
657 XSetForeground(dpy, scr->copy_gc, scr->black_pixel);
658 XSetBackground(dpy, scr->copy_gc, scr->white_pixel);
659 XCopyPlane(dpy, wwin->wm_hints->icon_pixmap, pixmap, scr->copy_gc, 0, 0, w, h, x, y, 1);
660 } else {
661 XCopyArea(dpy, wwin->wm_hints->icon_pixmap, pixmap, scr->copy_gc, 0, 0, w, h, x, y);
664 XSetClipOrigin(dpy, scr->copy_gc, 0, 0);
666 icon->pixmap = pixmap;
667 } else {
668 user_icon:
670 if (icon->image) {
671 icon->pixmap = makeIcon(scr, icon->image, icon->show_title,
672 icon->shadowed, icon->tile_type);
673 } else {
674 /* make default icons */
676 if (!scr->def_icon_pixmap) {
677 RImage *image = NULL;
678 char *path;
679 char *file;
681 file = wDefaultGetIconFile(scr, NULL, NULL, False);
682 if (file) {
683 path = FindImage(wPreferences.icon_path, file);
684 if (!path) {
685 wwarning(_("could not find default icon \"%s\""), file);
686 goto make_icons;
689 image = RLoadImage(scr->rcontext, path, 0);
690 if (!image) {
691 wwarning(_("could not load default icon \"%s\":%s"),
692 file, RMessageForError(RErrorCode));
694 wfree(path);
696 make_icons:
698 image = wIconValidateIconSize(scr, image);
699 scr->def_icon_pixmap = makeIcon(scr, image, False, False, icon->tile_type);
700 scr->def_ticon_pixmap = makeIcon(scr, image, True, False, icon->tile_type);
701 if (image)
702 RReleaseImage(image);
705 if (icon->show_title) {
706 XSetWindowBackgroundPixmap(dpy, icon->core->window, scr->def_ticon_pixmap);
707 } else {
708 XSetWindowBackgroundPixmap(dpy, icon->core->window, scr->def_icon_pixmap);
710 icon->pixmap = None;
713 if (icon->pixmap != None) {
714 XSetWindowBackgroundPixmap(dpy, icon->core->window, icon->pixmap);
716 XClearWindow(dpy, icon->core->window);
718 wIconPaint(icon);
721 void wIconPaint(WIcon * icon)
723 WScreen *scr = icon->core->screen_ptr;
724 int x;
725 char *tmp;
727 if (icon->force_paint) {
728 icon->force_paint = 0;
729 wIconUpdate(icon);
730 return;
733 XClearWindow(dpy, icon->core->window);
735 /* draw the icon title */
736 if (icon->show_title && icon->icon_name != NULL) {
737 int l;
738 int w;
740 tmp = ShrinkString(scr->icon_title_font, icon->icon_name, wPreferences.icon_size - 4);
741 w = WMWidthOfString(scr->icon_title_font, tmp, l = strlen(tmp));
743 if (w > icon->core->width - 4)
744 x = (icon->core->width - 4) - w;
745 else
746 x = (icon->core->width - w) / 2;
748 WMDrawString(scr->wmscreen, icon->core->window, scr->icon_title_color,
749 scr->icon_title_font, x, 1, tmp, l);
750 wfree(tmp);
753 if (icon->selected)
754 XDrawRectangle(dpy, icon->core->window, scr->icon_select_gc, 0, 0,
755 icon->core->width - 1, icon->core->height - 1);
758 /******************************************************************/
760 static void miniwindowExpose(WObjDescriptor * desc, XEvent * event)
762 wIconPaint(desc->parent);
765 static void miniwindowDblClick(WObjDescriptor * desc, XEvent * event)
767 WIcon *icon = desc->parent;
769 assert(icon->owner != NULL);
771 wDeiconifyWindow(icon->owner);
774 static void miniwindowMouseDown(WObjDescriptor * desc, XEvent * event)
776 WIcon *icon = desc->parent;
777 WWindow *wwin = icon->owner;
778 XEvent ev;
779 int x = wwin->icon_x, y = wwin->icon_y;
780 int dx = event->xbutton.x, dy = event->xbutton.y;
781 int grabbed = 0;
782 int clickButton = event->xbutton.button;
783 Bool hasMoved = False;
785 if (WCHECK_STATE(WSTATE_MODAL))
786 return;
788 if (IsDoubleClick(icon->core->screen_ptr, event)) {
789 miniwindowDblClick(desc, event);
790 return;
793 if (event->xbutton.button == Button1) {
794 if (event->xbutton.state & MOD_MASK)
795 wLowerFrame(icon->core);
796 else
797 wRaiseFrame(icon->core);
798 if (event->xbutton.state & ShiftMask) {
799 wIconSelect(icon);
800 wSelectWindow(icon->owner, !wwin->flags.selected);
802 } else if (event->xbutton.button == Button3) {
803 WObjDescriptor *desc;
805 OpenMiniwindowMenu(wwin, event->xbutton.x_root, event->xbutton.y_root);
807 /* allow drag select of menu */
808 desc = &wwin->screen_ptr->window_menu->menu->descriptor;
809 event->xbutton.send_event = True;
810 (*desc->handle_mousedown) (desc, event);
812 return;
815 if (XGrabPointer(dpy, icon->core->window, False, ButtonMotionMask
816 | ButtonReleaseMask | ButtonPressMask, GrabModeAsync,
817 GrabModeAsync, None, None, CurrentTime) != GrabSuccess) {
819 while (1) {
820 WMMaskEvent(dpy, PointerMotionMask | ButtonReleaseMask | ButtonPressMask
821 | ButtonMotionMask | ExposureMask, &ev);
822 switch (ev.type) {
823 case Expose:
824 WMHandleEvent(&ev);
825 break;
827 case MotionNotify:
828 hasMoved = True;
829 if (!grabbed) {
830 if (abs(dx - ev.xmotion.x) >= MOVE_THRESHOLD
831 || abs(dy - ev.xmotion.y) >= MOVE_THRESHOLD) {
832 XChangeActivePointerGrab(dpy, ButtonMotionMask
833 | ButtonReleaseMask | ButtonPressMask,
834 wCursor[WCUR_MOVE], CurrentTime);
835 grabbed = 1;
836 } else {
837 break;
840 x = ev.xmotion.x_root - dx;
841 y = ev.xmotion.y_root - dy;
842 XMoveWindow(dpy, icon->core->window, x, y);
843 break;
845 case ButtonPress:
846 break;
848 case ButtonRelease:
849 if (ev.xbutton.button != clickButton)
850 break;
852 if (wwin->icon_x != x || wwin->icon_y != y)
853 wwin->flags.icon_moved = 1;
855 XMoveWindow(dpy, icon->core->window, x, y);
857 wwin->icon_x = x;
858 wwin->icon_y = y;
859 XUngrabPointer(dpy, CurrentTime);
861 if (wPreferences.auto_arrange_icons)
862 wArrangeIcons(wwin->screen_ptr, True);
863 if (wPreferences.single_click && !hasMoved)
864 miniwindowDblClick(desc, event);
865 return;