Update local copy of GPLv2 and FSF address in copyrights
[wmaker-crm.git] / src / icon.c
blob535ef17ea8fb92924001442b4d4f71e182b2b2a1
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 along
18 * with this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22 #include "wconfig.h"
24 #include <X11/Xlib.h>
25 #include <X11/Xutil.h>
26 #include <stdlib.h>
27 #include <stdio.h>
28 #include <stdint.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"
46 #include "wmspec.h"
48 /**** Global varianebles ****/
49 extern WPreferences wPreferences;
51 #define MOD_MASK wPreferences.modifier_mask
53 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);
59 /****** Notification Observers ******/
61 static void appearanceObserver(void *self, WMNotification * notif)
63 WIcon *icon = (WIcon *) self;
64 uintptr_t flags = (uintptr_t)WMGetNotificationClientData(notif);
66 if (flags & WTextureSettings) {
67 icon->force_paint = 1;
69 if (flags & WFontSettings) {
70 icon->force_paint = 1;
73 if (flags & WColorSettings) {
77 wIconPaint(icon);
79 /* so that the appicon expose handlers will paint the appicon specific
80 * stuff */
81 XClearArea(dpy, icon->core->window, 0, 0, icon->core->width, icon->core->height, True);
84 static void tileObserver(void *self, WMNotification * notif)
86 WIcon *icon = (WIcon *) self;
88 icon->force_paint = 1;
89 wIconPaint(icon);
91 XClearArea(dpy, icon->core->window, 0, 0, 1, 1, True);
94 /************************************/
96 INLINE static void getSize(Drawable d, unsigned int *w, unsigned int *h, unsigned int *dep)
98 Window rjunk;
99 int xjunk, yjunk;
100 unsigned int bjunk;
102 XGetGeometry(dpy, d, &rjunk, &xjunk, &yjunk, w, h, &bjunk, dep);
105 WIcon *wIconCreate(WWindow * wwin)
107 WScreen *scr = wwin->screen_ptr;
108 WIcon *icon;
109 char *file;
110 unsigned long vmask = 0;
111 XSetWindowAttributes attribs;
113 icon = wmalloc(sizeof(WIcon));
114 memset(icon, 0, sizeof(WIcon));
115 icon->core = wCoreCreateTopLevel(scr, wwin->icon_x, wwin->icon_y,
116 wPreferences.icon_size, wPreferences.icon_size, 0);
118 if (wPreferences.use_saveunders) {
119 vmask |= CWSaveUnder;
120 attribs.save_under = True;
122 /* a white border for selecting it */
123 vmask |= CWBorderPixel;
124 attribs.border_pixel = scr->white_pixel;
126 XChangeWindowAttributes(dpy, icon->core->window, vmask, &attribs);
128 /* will be overriden if this is an application icon */
129 icon->core->descriptor.handle_mousedown = miniwindowMouseDown;
130 icon->core->descriptor.handle_expose = miniwindowExpose;
131 icon->core->descriptor.parent_type = WCLASS_MINIWINDOW;
132 icon->core->descriptor.parent = icon;
134 icon->core->stacking = wmalloc(sizeof(WStacking));
135 icon->core->stacking->above = NULL;
136 icon->core->stacking->under = NULL;
137 icon->core->stacking->window_level = NORMAL_ICON_LEVEL;
138 icon->core->stacking->child_of = NULL;
140 icon->owner = wwin;
141 if (wwin->wm_hints && (wwin->wm_hints->flags & IconWindowHint)) {
142 if (wwin->client_win == wwin->main_window) {
143 WApplication *wapp;
144 /* do not let miniwindow steal app-icon's icon window */
145 wapp = wApplicationOf(wwin->client_win);
146 if (!wapp || wapp->app_icon == NULL)
147 icon->icon_win = wwin->wm_hints->icon_window;
148 } else {
149 icon->icon_win = wwin->wm_hints->icon_window;
152 #ifdef NO_MINIWINDOW_TITLES
153 icon->show_title = 0;
154 #else
155 icon->show_title = 1;
156 #endif
157 if (!icon->image && !WFLAGP(wwin, always_user_icon))
158 icon->image = RRetainImage(wwin->net_icon_image);
159 if (!icon->image)
160 icon->image = wDefaultGetImage(scr, wwin->wm_instance, wwin->wm_class);
162 file = wDefaultGetIconFile(scr, wwin->wm_instance, wwin->wm_class, False);
163 if (file) {
164 icon->file = wstrdup(file);
167 icon->icon_name = wNETWMGetIconName(wwin->client_win);
168 if (icon->icon_name)
169 wwin->flags.net_has_icon_title = 1;
170 else
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, WNIconAppearanceSettingsChanged, icon);
180 WMAddNotificationObserver(tileObserver, icon, WNIconTileSettingsChanged, icon);
181 return icon;
184 WIcon *wIconCreateWithIconFile(WScreen * scr, char *iconfile, int tile)
186 WIcon *icon;
187 unsigned long vmask = 0;
188 XSetWindowAttributes attribs;
190 icon = wmalloc(sizeof(WIcon));
191 memset(icon, 0, sizeof(WIcon));
192 icon->core = wCoreCreateTopLevel(scr, 0, 0, wPreferences.icon_size, wPreferences.icon_size, 0);
193 if (wPreferences.use_saveunders) {
194 vmask = CWSaveUnder;
195 attribs.save_under = True;
197 /* a white border for selecting it */
198 vmask |= CWBorderPixel;
199 attribs.border_pixel = scr->white_pixel;
201 XChangeWindowAttributes(dpy, icon->core->window, vmask, &attribs);
203 /* will be overriden if this is a application icon */
204 icon->core->descriptor.handle_mousedown = miniwindowMouseDown;
205 icon->core->descriptor.handle_expose = miniwindowExpose;
206 icon->core->descriptor.parent_type = WCLASS_MINIWINDOW;
207 icon->core->descriptor.parent = icon;
209 icon->core->stacking = wmalloc(sizeof(WStacking));
210 icon->core->stacking->above = NULL;
211 icon->core->stacking->under = NULL;
212 icon->core->stacking->window_level = NORMAL_ICON_LEVEL;
213 icon->core->stacking->child_of = NULL;
215 if (iconfile) {
216 icon->image = RLoadImage(scr->rcontext, iconfile, 0);
217 if (!icon->image) {
218 wwarning(_("error loading image file \"%s\": %s"), iconfile, RMessageForError(RErrorCode));
221 icon->image = wIconValidateIconSize(scr, icon->image);
223 icon->file = wstrdup(iconfile);
226 icon->tile_type = tile;
228 wIconUpdate(icon);
230 WMAddNotificationObserver(appearanceObserver, icon, WNIconAppearanceSettingsChanged, icon);
231 WMAddNotificationObserver(tileObserver, icon, WNIconTileSettingsChanged, icon);
233 return icon;
236 void wIconDestroy(WIcon * icon)
238 WCoreWindow *core = icon->core;
239 WScreen *scr = core->screen_ptr;
241 WMRemoveNotificationObserver(icon);
243 if (icon->handlerID)
244 WMDeleteTimerHandler(icon->handlerID);
246 if (icon->icon_win) {
247 int x = 0, y = 0;
249 if (icon->owner) {
250 x = icon->owner->icon_x;
251 y = icon->owner->icon_y;
253 XUnmapWindow(dpy, icon->icon_win);
254 XReparentWindow(dpy, icon->icon_win, scr->root_win, x, y);
256 if (icon->icon_name)
257 XFree(icon->icon_name);
259 if (icon->pixmap)
260 XFreePixmap(dpy, icon->pixmap);
262 if (icon->file)
263 wfree(icon->file);
265 if (icon->image != NULL)
266 RReleaseImage(icon->image);
268 wCoreDestroy(icon->core);
269 wfree(icon);
272 static void drawIconTitle(WScreen * scr, Pixmap pixmap, int height)
274 XFillRectangle(dpy, pixmap, scr->icon_title_texture->normal_gc, 0, 0, wPreferences.icon_size, height + 1);
275 XDrawLine(dpy, pixmap, scr->icon_title_texture->light_gc, 0, 0, wPreferences.icon_size, 0);
276 XDrawLine(dpy, pixmap, scr->icon_title_texture->light_gc, 0, 0, 0, height + 1);
277 XDrawLine(dpy, pixmap, scr->icon_title_texture->dim_gc,
278 wPreferences.icon_size - 1, 0, wPreferences.icon_size - 1, height + 1);
281 static Pixmap makeIcon(WScreen *scr, RImage *icon, int titled, int shadowed, int tileType, int highlighted)
283 RImage *tile;
284 Pixmap pixmap;
285 int x, y, sx, sy;
286 unsigned w, h;
287 int theight = WMFontHeight(scr->icon_title_font);
289 if (tileType == TILE_NORMAL)
290 tile = RCloneImage(scr->icon_tile);
291 else {
292 assert(scr->clip_tile);
293 tile = RCloneImage(scr->clip_tile);
295 if (icon) {
296 w = (icon->width > wPreferences.icon_size)
297 ? wPreferences.icon_size : icon->width;
298 x = (wPreferences.icon_size - w) / 2;
299 sx = (icon->width - w) / 2;
301 if (!titled) {
302 h = (icon->height > wPreferences.icon_size)
303 ? wPreferences.icon_size : icon->height;
304 y = (wPreferences.icon_size - h) / 2;
305 sy = (icon->height - h) / 2;
306 } else {
307 h = (icon->height + theight > wPreferences.icon_size
308 ? wPreferences.icon_size - theight : icon->height);
309 y = theight + ((int)wPreferences.icon_size - theight - h) / 2;
310 sy = (icon->height - h) / 2;
312 RCombineArea(tile, icon, sx, sy, w, h, x, y);
315 if (shadowed) {
316 RColor color;
318 color.red = scr->icon_back_texture->light.red >> 8;
319 color.green = scr->icon_back_texture->light.green >> 8;
320 color.blue = scr->icon_back_texture->light.blue >> 8;
321 color.alpha = 150; /* about 60% */
322 RClearImage(tile, &color);
324 if (highlighted) {
325 RColor color;
327 color.red = color.green = color.blue = 0;
328 color.alpha = 160;
329 RLightImage(tile, &color);
332 if (!RConvertImage(scr->rcontext, tile, &pixmap)) {
333 wwarning(_("error rendering image:%s"), RMessageForError(RErrorCode));
335 RReleaseImage(tile);
337 if (titled)
338 drawIconTitle(scr, pixmap, theight);
340 return pixmap;
343 void wIconChangeTitle(WIcon * icon, char *new_title)
345 int changed;
347 changed = (new_title == NULL && icon->icon_name != NULL)
348 || (new_title != NULL && icon->icon_name == NULL);
350 if (icon->icon_name != NULL)
351 XFree(icon->icon_name);
353 icon->icon_name = new_title;
355 if (changed)
356 icon->force_paint = 1;
357 wIconPaint(icon);
360 void wIconChangeImage(WIcon * icon, RImage * new_image)
362 assert(icon != NULL);
364 if (icon->image)
365 RReleaseImage(icon->image);
367 icon->image = new_image;
369 wIconUpdate(icon);
372 RImage *wIconValidateIconSize(WScreen * scr, RImage * icon)
374 RImage *tmp;
375 int w, h;
377 if (!icon)
378 return NULL;
379 #ifndef DONT_SCALE_ICONS
380 if (wPreferences.icon_size != 64) {
381 w = wPreferences.icon_size * icon->width / 64;
382 h = wPreferences.icon_size * icon->height / 64;
384 tmp = RScaleImage(icon, w, h);
385 RReleaseImage(icon);
386 icon = tmp;
388 #endif
390 return icon;
393 Bool wIconChangeImageFile(WIcon * icon, char *file)
395 WScreen *scr = icon->core->screen_ptr;
396 RImage *image;
397 char *path;
398 int error = 0;
400 if (!file) {
401 wIconChangeImage(icon, NULL);
402 return True;
405 path = FindImage(wPreferences.icon_path, file);
407 if (path && (image = RLoadImage(scr->rcontext, path, 0))) {
408 image = wIconValidateIconSize(icon->core->screen_ptr, image);
410 wIconChangeImage(icon, image);
411 } else {
412 error = 1;
415 if (path)
416 wfree(path);
418 return !error;
421 static char *getnameforicon(WWindow * wwin)
423 char *prefix, *suffix;
424 char *path;
425 int len;
427 if (wwin->wm_class && wwin->wm_instance) {
428 int len = strlen(wwin->wm_class) + strlen(wwin->wm_instance) + 2;
429 suffix = wmalloc(len);
430 snprintf(suffix, len, "%s.%s", wwin->wm_instance, wwin->wm_class);
431 } else if (wwin->wm_class) {
432 int len = strlen(wwin->wm_class) + 1;
433 suffix = wmalloc(len);
434 snprintf(suffix, len, "%s", wwin->wm_class);
435 } else if (wwin->wm_instance) {
436 int len = strlen(wwin->wm_instance) + 1;
437 suffix = wmalloc(len);
438 snprintf(suffix, len, "%s", wwin->wm_instance);
439 } else {
440 return NULL;
443 prefix = wusergnusteppath();
444 len = strlen(prefix) + 64 + strlen(suffix);
445 path = wmalloc(len + 1);
446 snprintf(path, len, "%s/Library/WindowMaker", prefix);
448 if (access(path, F_OK) != 0) {
449 if (mkdir(path, S_IRUSR | S_IWUSR | S_IXUSR)) {
450 werror(_("could not create directory %s"), path);
451 wfree(path);
452 wfree(suffix);
453 return NULL;
456 strcat(path, "/CachedPixmaps");
457 if (access(path, F_OK) != 0) {
458 if (mkdir(path, S_IRUSR | S_IWUSR | S_IXUSR) != 0) {
459 werror(_("could not create directory %s"), path);
460 wfree(path);
461 wfree(suffix);
462 return NULL;
466 strcat(path, "/");
467 strcat(path, suffix);
468 strcat(path, ".xpm");
469 wfree(suffix);
471 return path;
475 * wIconStore--
476 * Stores the client supplied icon at ~/GNUstep/Library/WindowMaker/CachedPixmaps
477 * and returns the path for that icon. Returns NULL if there is no
478 * client supplied icon or on failure.
480 * Side effects:
481 * New directories might be created.
483 char *wIconStore(WIcon * icon)
485 char *path;
486 RImage *image;
487 WWindow *wwin = icon->owner;
489 if (!wwin || !wwin->wm_hints || !(wwin->wm_hints->flags & IconPixmapHint)
490 || wwin->wm_hints->icon_pixmap == None)
491 return NULL;
493 path = getnameforicon(wwin);
494 if (!path)
495 return NULL;
497 image = RCreateImageFromDrawable(icon->core->screen_ptr->rcontext,
498 wwin->wm_hints->icon_pixmap, (wwin->wm_hints->flags & IconMaskHint)
499 ? wwin->wm_hints->icon_mask : None);
500 if (!image) {
501 wfree(path);
502 return NULL;
505 if (!RSaveImage(image, path, "XPM")) {
506 wfree(path);
507 path = NULL;
509 RReleaseImage(image);
511 return path;
514 static void cycleColor(void *data)
516 WIcon *icon = (WIcon *) data;
517 WScreen *scr = icon->core->screen_ptr;
518 XGCValues gcv;
520 icon->step--;
521 gcv.dash_offset = icon->step;
522 XChangeGC(dpy, scr->icon_select_gc, GCDashOffset, &gcv);
524 XDrawRectangle(dpy, icon->core->window, scr->icon_select_gc, 0, 0,
525 icon->core->width - 1, icon->core->height - 1);
526 icon->handlerID = WMAddTimerHandler(COLOR_CYCLE_DELAY, cycleColor, icon);
529 #ifdef NEWAPPICON
530 void wIconSetHighlited(WIcon *icon, Bool flag)
532 if (icon->highlighted == flag)
533 return;
535 icon->highlighted = flag;
536 icon->force_paint = True;
537 wIconPaint(icon);
539 #endif
541 void wIconSelect(WIcon * icon)
543 WScreen *scr = icon->core->screen_ptr;
544 icon->selected = !icon->selected;
546 if (icon->selected) {
547 icon->step = 0;
548 if (!wPreferences.dont_blink)
549 icon->handlerID = WMAddTimerHandler(10, cycleColor, icon);
550 else
551 XDrawRectangle(dpy, icon->core->window, scr->icon_select_gc, 0, 0,
552 icon->core->width - 1, icon->core->height - 1);
553 } else {
554 if (icon->handlerID) {
555 WMDeleteTimerHandler(icon->handlerID);
556 icon->handlerID = NULL;
558 XClearArea(dpy, icon->core->window, 0, 0, icon->core->width, icon->core->height, True);
562 void wIconUpdate(WIcon * icon)
564 WScreen *scr = icon->core->screen_ptr;
565 int title_height = WMFontHeight(scr->icon_title_font);
566 WWindow *wwin = icon->owner;
568 assert(scr->icon_tile != NULL);
570 if (icon->pixmap != None)
571 XFreePixmap(dpy, icon->pixmap);
572 icon->pixmap = None;
574 if (wwin && (WFLAGP(wwin, always_user_icon) || wwin->net_icon_image))
575 goto user_icon;
577 /* use client specified icon window */
578 if (icon->icon_win != None) {
579 XWindowAttributes attr;
580 int resize = 0;
581 unsigned int width, height, depth;
582 int theight;
583 Pixmap pixmap;
585 getSize(icon->icon_win, &width, &height, &depth);
587 if (width > wPreferences.icon_size) {
588 resize = 1;
589 width = wPreferences.icon_size;
591 if (height > wPreferences.icon_size) {
592 resize = 1;
593 height = wPreferences.icon_size;
595 if (icon->show_title && (height + title_height < wPreferences.icon_size)) {
596 pixmap = XCreatePixmap(dpy, scr->w_win, wPreferences.icon_size,
597 wPreferences.icon_size, scr->w_depth);
598 XSetClipMask(dpy, scr->copy_gc, None);
599 XCopyArea(dpy, scr->icon_tile_pixmap, pixmap, scr->copy_gc, 0, 0,
600 wPreferences.icon_size, wPreferences.icon_size, 0, 0);
601 drawIconTitle(scr, pixmap, title_height);
602 theight = title_height;
603 } else {
604 pixmap = None;
605 theight = 0;
606 XSetWindowBackgroundPixmap(dpy, icon->core->window, scr->icon_tile_pixmap);
609 XSetWindowBorderWidth(dpy, icon->icon_win, 0);
610 XReparentWindow(dpy, icon->icon_win, icon->core->window,
611 (wPreferences.icon_size - width) / 2,
612 theight + (wPreferences.icon_size - height - theight) / 2);
613 if (resize)
614 XResizeWindow(dpy, icon->icon_win, width, height);
616 XMapWindow(dpy, icon->icon_win);
618 XAddToSaveSet(dpy, icon->icon_win);
620 icon->pixmap = pixmap;
622 if (XGetWindowAttributes(dpy, icon->icon_win, &attr)) {
623 if (attr.all_event_masks & ButtonPressMask) {
624 wHackedGrabButton(Button1, MOD_MASK, icon->core->window, True,
625 ButtonPressMask, GrabModeSync, GrabModeAsync,
626 None, wCursor[WCUR_ARROW]);
629 } else if (wwin && wwin->wm_hints && (wwin->wm_hints->flags & IconPixmapHint)) {
630 int x, y;
631 unsigned int w, h;
632 Window jw;
633 int ji, dotitle;
634 unsigned int ju, d;
635 Pixmap pixmap;
637 if (!XGetGeometry(dpy, wwin->wm_hints->icon_pixmap, &jw, &ji, &ji, &w, &h, &ju, &d)) {
638 icon->owner->wm_hints->flags &= ~IconPixmapHint;
639 goto user_icon;
642 pixmap = XCreatePixmap(dpy, icon->core->window, wPreferences.icon_size,
643 wPreferences.icon_size, scr->w_depth);
644 XSetClipMask(dpy, scr->copy_gc, None);
645 XCopyArea(dpy, scr->icon_tile_pixmap, pixmap, scr->copy_gc, 0, 0,
646 wPreferences.icon_size, wPreferences.icon_size, 0, 0);
648 if (w > wPreferences.icon_size)
649 w = wPreferences.icon_size;
650 x = (wPreferences.icon_size - w) / 2;
652 if (icon->show_title && (title_height < wPreferences.icon_size)) {
653 drawIconTitle(scr, pixmap, title_height);
654 dotitle = 1;
656 if (h > wPreferences.icon_size - title_height - 2) {
657 h = wPreferences.icon_size - title_height - 2;
658 y = title_height + 1;
659 } else {
660 y = (wPreferences.icon_size - h - title_height) / 2 + title_height + 1;
662 } else {
663 dotitle = 0;
664 if (w > wPreferences.icon_size)
665 w = wPreferences.icon_size;
666 y = (wPreferences.icon_size - h) / 2;
669 if (wwin->wm_hints->flags & IconMaskHint)
670 XSetClipMask(dpy, scr->copy_gc, wwin->wm_hints->icon_mask);
672 XSetClipOrigin(dpy, scr->copy_gc, x, y);
674 if (d != scr->w_depth) {
675 XSetForeground(dpy, scr->copy_gc, scr->black_pixel);
676 XSetBackground(dpy, scr->copy_gc, scr->white_pixel);
677 XCopyPlane(dpy, wwin->wm_hints->icon_pixmap, pixmap, scr->copy_gc, 0, 0, w, h, x, y, 1);
678 } else {
679 XCopyArea(dpy, wwin->wm_hints->icon_pixmap, pixmap, scr->copy_gc, 0, 0, w, h, x, y);
682 XSetClipOrigin(dpy, scr->copy_gc, 0, 0);
684 icon->pixmap = pixmap;
685 } else {
686 user_icon:
688 if (icon->image) {
689 icon->pixmap = makeIcon(scr, icon->image, icon->show_title,
690 icon->shadowed, icon->tile_type, icon->highlighted);
691 } else {
692 /* make default icons */
694 if (!scr->def_icon_pixmap) {
695 RImage *image = NULL;
696 char *path;
697 char *file;
699 file = wDefaultGetIconFile(scr, NULL, NULL, False);
700 if (file) {
701 path = FindImage(wPreferences.icon_path, file);
702 if (!path) {
703 wwarning(_("could not find default icon \"%s\""), file);
704 goto make_icons;
707 image = RLoadImage(scr->rcontext, path, 0);
708 if (!image) {
709 wwarning(_("could not load default icon \"%s\":%s"),
710 file, RMessageForError(RErrorCode));
712 wfree(path);
714 make_icons:
716 image = wIconValidateIconSize(scr, image);
717 scr->def_icon_pixmap = makeIcon(scr, image, False, False, icon->tile_type, icon->highlighted);
718 scr->def_ticon_pixmap = makeIcon(scr, image, True, False, icon->tile_type, icon->highlighted);
719 if (image)
720 RReleaseImage(image);
723 if (icon->show_title) {
724 XSetWindowBackgroundPixmap(dpy, icon->core->window, scr->def_ticon_pixmap);
725 } else {
726 XSetWindowBackgroundPixmap(dpy, icon->core->window, scr->def_icon_pixmap);
728 icon->pixmap = None;
731 if (icon->pixmap != None) {
732 XSetWindowBackgroundPixmap(dpy, icon->core->window, icon->pixmap);
734 XClearWindow(dpy, icon->core->window);
736 wIconPaint(icon);
739 void wIconPaint(WIcon * icon)
741 WScreen *scr = icon->core->screen_ptr;
742 int x;
743 char *tmp;
745 if (icon->force_paint) {
746 icon->force_paint = 0;
747 wIconUpdate(icon);
748 return;
751 XClearWindow(dpy, icon->core->window);
753 /* draw the icon title */
754 if (icon->show_title && icon->icon_name != NULL) {
755 int l;
756 int w;
758 tmp = ShrinkString(scr->icon_title_font, icon->icon_name, wPreferences.icon_size - 4);
759 w = WMWidthOfString(scr->icon_title_font, tmp, l = strlen(tmp));
761 if (w > icon->core->width - 4)
762 x = (icon->core->width - 4) - w;
763 else
764 x = (icon->core->width - w) / 2;
766 WMDrawString(scr->wmscreen, icon->core->window, scr->icon_title_color,
767 scr->icon_title_font, x, 1, tmp, l);
768 wfree(tmp);
771 if (icon->selected)
772 XDrawRectangle(dpy, icon->core->window, scr->icon_select_gc, 0, 0,
773 icon->core->width - 1, icon->core->height - 1);
776 /******************************************************************/
778 static void miniwindowExpose(WObjDescriptor * desc, XEvent * event)
780 wIconPaint(desc->parent);
783 static void miniwindowDblClick(WObjDescriptor * desc, XEvent * event)
785 WIcon *icon = desc->parent;
787 assert(icon->owner != NULL);
789 wDeiconifyWindow(icon->owner);
792 static void miniwindowMouseDown(WObjDescriptor * desc, XEvent * event)
794 WIcon *icon = desc->parent;
795 WWindow *wwin = icon->owner;
796 XEvent ev;
797 int x = wwin->icon_x, y = wwin->icon_y;
798 int dx = event->xbutton.x, dy = event->xbutton.y;
799 int grabbed = 0;
800 int clickButton = event->xbutton.button;
801 Bool hasMoved = False;
803 if (WCHECK_STATE(WSTATE_MODAL))
804 return;
806 if (IsDoubleClick(icon->core->screen_ptr, event)) {
807 miniwindowDblClick(desc, event);
808 return;
811 if (event->xbutton.button == Button1) {
812 if (event->xbutton.state & MOD_MASK)
813 wLowerFrame(icon->core);
814 else
815 wRaiseFrame(icon->core);
816 if (event->xbutton.state & ShiftMask) {
817 wIconSelect(icon);
818 wSelectWindow(icon->owner, !wwin->flags.selected);
820 } else if (event->xbutton.button == Button3) {
821 WObjDescriptor *desc;
823 OpenMiniwindowMenu(wwin, event->xbutton.x_root, event->xbutton.y_root);
825 /* allow drag select of menu */
826 desc = &wwin->screen_ptr->window_menu->menu->descriptor;
827 event->xbutton.send_event = True;
828 (*desc->handle_mousedown) (desc, event);
830 return;
833 if (XGrabPointer(dpy, icon->core->window, False, ButtonMotionMask
834 | ButtonReleaseMask | ButtonPressMask, GrabModeAsync,
835 GrabModeAsync, None, None, CurrentTime) != GrabSuccess) {
837 while (1) {
838 WMMaskEvent(dpy, PointerMotionMask | ButtonReleaseMask | ButtonPressMask
839 | ButtonMotionMask | ExposureMask, &ev);
840 switch (ev.type) {
841 case Expose:
842 WMHandleEvent(&ev);
843 break;
845 case MotionNotify:
846 hasMoved = True;
847 if (!grabbed) {
848 if (abs(dx - ev.xmotion.x) >= MOVE_THRESHOLD
849 || abs(dy - ev.xmotion.y) >= MOVE_THRESHOLD) {
850 XChangeActivePointerGrab(dpy, ButtonMotionMask
851 | ButtonReleaseMask | ButtonPressMask,
852 wCursor[WCUR_MOVE], CurrentTime);
853 grabbed = 1;
854 } else {
855 break;
858 x = ev.xmotion.x_root - dx;
859 y = ev.xmotion.y_root - dy;
860 XMoveWindow(dpy, icon->core->window, x, y);
861 break;
863 case ButtonPress:
864 break;
866 case ButtonRelease:
867 if (ev.xbutton.button != clickButton)
868 break;
870 if (wwin->icon_x != x || wwin->icon_y != y)
871 wwin->flags.icon_moved = 1;
873 XMoveWindow(dpy, icon->core->window, x, y);
875 wwin->icon_x = x;
876 wwin->icon_y = y;
877 XUngrabPointer(dpy, CurrentTime);
879 if (wPreferences.auto_arrange_icons)
880 wArrangeIcons(wwin->screen_ptr, True);
881 if (wPreferences.single_click && !hasMoved)
882 miniwindowDblClick(desc, event);
883 return;