WINGs: Add copy_file() to libWUtil
[wmaker-crm.git] / src / icon.c
blob5660d6b653263f90cb605d39f422b7881af0d7a2
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 icon->file_image = wDefaultGetImage(scr, wwin->wm_instance, wwin->wm_class);
159 file = wDefaultGetIconFile(scr, wwin->wm_instance, wwin->wm_class, False);
160 if (file) {
161 icon->file = wstrdup(file);
164 icon->icon_name = wNETWMGetIconName(wwin->client_win);
165 if (icon->icon_name)
166 wwin->flags.net_has_icon_title = 1;
167 else
168 wGetIconName(dpy, wwin->client_win, &icon->icon_name);
170 icon->tile_type = TILE_NORMAL;
172 wIconUpdate(icon);
174 XFlush(dpy);
176 WMAddNotificationObserver(appearanceObserver, icon, WNIconAppearanceSettingsChanged, icon);
177 WMAddNotificationObserver(tileObserver, icon, WNIconTileSettingsChanged, icon);
178 return icon;
181 WIcon *wIconCreateWithIconFile(WScreen * scr, char *iconfile, int tile)
183 WIcon *icon;
184 unsigned long vmask = 0;
185 XSetWindowAttributes attribs;
187 icon = wmalloc(sizeof(WIcon));
188 memset(icon, 0, sizeof(WIcon));
189 icon->core = wCoreCreateTopLevel(scr, 0, 0, wPreferences.icon_size, wPreferences.icon_size, 0);
190 if (wPreferences.use_saveunders) {
191 vmask = CWSaveUnder;
192 attribs.save_under = True;
194 /* a white border for selecting it */
195 vmask |= CWBorderPixel;
196 attribs.border_pixel = scr->white_pixel;
198 XChangeWindowAttributes(dpy, icon->core->window, vmask, &attribs);
200 /* will be overriden if this is a application icon */
201 icon->core->descriptor.handle_mousedown = miniwindowMouseDown;
202 icon->core->descriptor.handle_expose = miniwindowExpose;
203 icon->core->descriptor.parent_type = WCLASS_MINIWINDOW;
204 icon->core->descriptor.parent = icon;
206 icon->core->stacking = wmalloc(sizeof(WStacking));
207 icon->core->stacking->above = NULL;
208 icon->core->stacking->under = NULL;
209 icon->core->stacking->window_level = NORMAL_ICON_LEVEL;
210 icon->core->stacking->child_of = NULL;
212 if (iconfile) {
213 icon->file_image = RLoadImage(scr->rcontext, iconfile, 0);
214 if (!icon->file_image) {
215 wwarning(_("error loading image file \"%s\": %s"), iconfile, RMessageForError(RErrorCode));
218 icon->file_image = wIconValidateIconSize(scr, icon->file_image);
220 icon->file = wstrdup(iconfile);
223 icon->tile_type = tile;
225 wIconUpdate(icon);
227 WMAddNotificationObserver(appearanceObserver, icon, WNIconAppearanceSettingsChanged, icon);
228 WMAddNotificationObserver(tileObserver, icon, WNIconTileSettingsChanged, icon);
230 return icon;
233 void wIconDestroy(WIcon * icon)
235 WCoreWindow *core = icon->core;
236 WScreen *scr = core->screen_ptr;
238 WMRemoveNotificationObserver(icon);
240 if (icon->handlerID)
241 WMDeleteTimerHandler(icon->handlerID);
243 if (icon->icon_win) {
244 int x = 0, y = 0;
246 if (icon->owner) {
247 x = icon->owner->icon_x;
248 y = icon->owner->icon_y;
250 XUnmapWindow(dpy, icon->icon_win);
251 XReparentWindow(dpy, icon->icon_win, scr->root_win, x, y);
253 if (icon->icon_name)
254 XFree(icon->icon_name);
256 if (icon->pixmap)
257 XFreePixmap(dpy, icon->pixmap);
259 if (icon->file)
260 wfree(icon->file);
262 if (icon->file_image != NULL)
263 RReleaseImage(icon->file_image);
265 wCoreDestroy(icon->core);
266 wfree(icon);
269 static void drawIconTitle(WScreen * scr, Pixmap pixmap, int height)
271 XFillRectangle(dpy, pixmap, scr->icon_title_texture->normal_gc, 0, 0, wPreferences.icon_size, height + 1);
272 XDrawLine(dpy, pixmap, scr->icon_title_texture->light_gc, 0, 0, wPreferences.icon_size, 0);
273 XDrawLine(dpy, pixmap, scr->icon_title_texture->light_gc, 0, 0, 0, height + 1);
274 XDrawLine(dpy, pixmap, scr->icon_title_texture->dim_gc,
275 wPreferences.icon_size - 1, 0, wPreferences.icon_size - 1, height + 1);
278 static Pixmap makeIcon(WScreen *scr, RImage *icon, int titled, int shadowed, int tileType, int highlighted)
280 RImage *tile;
281 Pixmap pixmap;
282 int x, y, sx, sy;
283 unsigned w, h;
284 int theight = WMFontHeight(scr->icon_title_font);
286 if (tileType == TILE_NORMAL)
287 tile = RCloneImage(scr->icon_tile);
288 else {
289 assert(scr->clip_tile);
290 tile = RCloneImage(scr->clip_tile);
292 if (icon) {
293 w = (icon->width > wPreferences.icon_size)
294 ? wPreferences.icon_size : icon->width;
295 x = (wPreferences.icon_size - w) / 2;
296 sx = (icon->width - w) / 2;
298 if (!titled) {
299 h = (icon->height > wPreferences.icon_size)
300 ? wPreferences.icon_size : icon->height;
301 y = (wPreferences.icon_size - h) / 2;
302 sy = (icon->height - h) / 2;
303 } else {
304 h = (icon->height + theight > wPreferences.icon_size
305 ? wPreferences.icon_size - theight : icon->height);
306 y = theight + ((int)wPreferences.icon_size - theight - h) / 2;
307 sy = (icon->height - h) / 2;
309 RCombineArea(tile, icon, sx, sy, w, h, x, y);
312 if (shadowed) {
313 RColor color;
315 color.red = scr->icon_back_texture->light.red >> 8;
316 color.green = scr->icon_back_texture->light.green >> 8;
317 color.blue = scr->icon_back_texture->light.blue >> 8;
318 color.alpha = 150; /* about 60% */
319 RClearImage(tile, &color);
321 if (highlighted) {
322 RColor color;
324 color.red = color.green = color.blue = 0;
325 color.alpha = 160;
326 RLightImage(tile, &color);
329 if (!RConvertImage(scr->rcontext, tile, &pixmap)) {
330 wwarning(_("error rendering image:%s"), RMessageForError(RErrorCode));
332 RReleaseImage(tile);
334 if (titled)
335 drawIconTitle(scr, pixmap, theight);
337 return pixmap;
340 void wIconChangeTitle(WIcon * icon, char *new_title)
342 int changed;
344 changed = (new_title == NULL && icon->icon_name != NULL)
345 || (new_title != NULL && icon->icon_name == NULL);
347 if (icon->icon_name != NULL)
348 XFree(icon->icon_name);
350 icon->icon_name = new_title;
352 if (changed)
353 icon->force_paint = 1;
354 wIconPaint(icon);
357 RImage *wIconValidateIconSize(WScreen * scr, RImage * icon)
359 RImage *tmp;
360 int w, h;
362 if (!icon)
363 return NULL;
364 #ifndef DONT_SCALE_ICONS
365 if (wPreferences.icon_size != 64) {
366 w = wPreferences.icon_size * icon->width / 64;
367 h = wPreferences.icon_size * icon->height / 64;
369 tmp = RScaleImage(icon, w, h);
370 RReleaseImage(icon);
371 icon = tmp;
373 #endif
375 return icon;
378 Bool wIconChangeImageFile(WIcon * icon, char *file)
380 WScreen *scr = icon->core->screen_ptr;
381 RImage *image;
382 char *path;
383 int error = 0;
385 if (icon->file_image)
386 RReleaseImage(icon->file_image);
388 if (!file) {
389 icon->file_image = NULL;
390 wIconUpdate(icon);
391 return True;
394 path = FindImage(wPreferences.icon_path, file);
396 if (path && (image = RLoadImage(scr->rcontext, path, 0))) {
397 icon->file_image = wIconValidateIconSize(icon->core->screen_ptr, image);
398 wIconUpdate(icon);
399 } else {
400 error = 1;
403 if (path)
404 wfree(path);
406 return !error;
409 static char *getnameforicon(WWindow * wwin)
411 char *prefix, *suffix;
412 char *path;
413 int len;
415 if (wwin->wm_class && wwin->wm_instance) {
416 int len = strlen(wwin->wm_class) + strlen(wwin->wm_instance) + 2;
417 suffix = wmalloc(len);
418 snprintf(suffix, len, "%s.%s", wwin->wm_instance, wwin->wm_class);
419 } else if (wwin->wm_class) {
420 int len = strlen(wwin->wm_class) + 1;
421 suffix = wmalloc(len);
422 snprintf(suffix, len, "%s", wwin->wm_class);
423 } else if (wwin->wm_instance) {
424 int len = strlen(wwin->wm_instance) + 1;
425 suffix = wmalloc(len);
426 snprintf(suffix, len, "%s", wwin->wm_instance);
427 } else {
428 return NULL;
431 prefix = wusergnusteppath();
432 len = strlen(prefix) + 64 + strlen(suffix);
433 path = wmalloc(len + 1);
434 snprintf(path, len, "%s/Library/WindowMaker", prefix);
436 if (access(path, F_OK) != 0) {
437 if (mkdir(path, S_IRUSR | S_IWUSR | S_IXUSR)) {
438 werror(_("could not create directory %s"), path);
439 wfree(path);
440 wfree(suffix);
441 return NULL;
444 strcat(path, "/CachedPixmaps");
445 if (access(path, F_OK) != 0) {
446 if (mkdir(path, S_IRUSR | S_IWUSR | S_IXUSR) != 0) {
447 werror(_("could not create directory %s"), path);
448 wfree(path);
449 wfree(suffix);
450 return NULL;
454 strcat(path, "/");
455 strcat(path, suffix);
456 strcat(path, ".xpm");
457 wfree(suffix);
459 return path;
463 * wIconStore--
464 * Stores the client supplied icon at ~/GNUstep/Library/WindowMaker/CachedPixmaps
465 * and returns the path for that icon. Returns NULL if there is no
466 * client supplied icon or on failure.
468 * Side effects:
469 * New directories might be created.
471 char *wIconStore(WIcon * icon)
473 char *path;
474 RImage *image = NULL;
475 WWindow *wwin = icon->owner;
477 if (!wwin) return NULL;
479 path = getnameforicon(wwin);
480 if (!path)
481 return NULL;
483 if (wwin->net_icon_image) {
484 image = RRetainImage(wwin->net_icon_image);
485 } else if (wwin->wm_hints && (wwin->wm_hints->flags & IconPixmapHint)
486 && wwin->wm_hints->icon_pixmap != None) {
487 image = RCreateImageFromDrawable(icon->core->screen_ptr->rcontext,
488 wwin->wm_hints->icon_pixmap, (wwin->wm_hints->flags & IconMaskHint)
489 ? wwin->wm_hints->icon_mask : None);
491 if (!image) {
492 wfree(path);
493 return NULL;
496 if (!RSaveImage(image, path, "XPM")) {
497 wfree(path);
498 path = NULL;
500 RReleaseImage(image);
502 return path;
505 static void cycleColor(void *data)
507 WIcon *icon = (WIcon *) data;
508 WScreen *scr = icon->core->screen_ptr;
509 XGCValues gcv;
511 icon->step--;
512 gcv.dash_offset = icon->step;
513 XChangeGC(dpy, scr->icon_select_gc, GCDashOffset, &gcv);
515 XDrawRectangle(dpy, icon->core->window, scr->icon_select_gc, 0, 0,
516 icon->core->width - 1, icon->core->height - 1);
517 icon->handlerID = WMAddTimerHandler(COLOR_CYCLE_DELAY, cycleColor, icon);
520 #ifdef NEWAPPICON
521 void wIconSetHighlited(WIcon *icon, Bool flag)
523 if (icon->highlighted == flag)
524 return;
526 icon->highlighted = flag;
527 icon->force_paint = True;
528 wIconPaint(icon);
530 #endif
532 void wIconSelect(WIcon * icon)
534 WScreen *scr = icon->core->screen_ptr;
535 icon->selected = !icon->selected;
537 if (icon->selected) {
538 icon->step = 0;
539 if (!wPreferences.dont_blink)
540 icon->handlerID = WMAddTimerHandler(10, cycleColor, icon);
541 else
542 XDrawRectangle(dpy, icon->core->window, scr->icon_select_gc, 0, 0,
543 icon->core->width - 1, icon->core->height - 1);
544 } else {
545 if (icon->handlerID) {
546 WMDeleteTimerHandler(icon->handlerID);
547 icon->handlerID = NULL;
549 XClearArea(dpy, icon->core->window, 0, 0, icon->core->width, icon->core->height, True);
553 void wIconUpdate(WIcon * icon)
555 WScreen *scr = icon->core->screen_ptr;
556 int title_height = WMFontHeight(scr->icon_title_font);
557 WWindow *wwin = icon->owner;
559 assert(scr->icon_tile != NULL);
561 if (icon->pixmap != None)
562 XFreePixmap(dpy, icon->pixmap);
563 icon->pixmap = None;
565 if (wwin && WFLAGP(wwin, always_user_icon))
566 goto user_icon;
568 if (icon->icon_win != None) {
569 /* use client specified icon window */
570 XWindowAttributes attr;
571 int resize = 0;
572 unsigned int width, height, depth;
573 int theight;
574 Pixmap pixmap;
576 getSize(icon->icon_win, &width, &height, &depth);
578 if (width > wPreferences.icon_size) {
579 resize = 1;
580 width = wPreferences.icon_size;
582 if (height > wPreferences.icon_size) {
583 resize = 1;
584 height = wPreferences.icon_size;
586 if (icon->show_title && (height + title_height < wPreferences.icon_size)) {
587 pixmap = XCreatePixmap(dpy, scr->w_win, wPreferences.icon_size,
588 wPreferences.icon_size, scr->w_depth);
589 XSetClipMask(dpy, scr->copy_gc, None);
590 XCopyArea(dpy, scr->icon_tile_pixmap, pixmap, scr->copy_gc, 0, 0,
591 wPreferences.icon_size, wPreferences.icon_size, 0, 0);
592 drawIconTitle(scr, pixmap, title_height);
593 theight = title_height;
594 } else {
595 pixmap = None;
596 theight = 0;
597 XSetWindowBackgroundPixmap(dpy, icon->core->window, scr->icon_tile_pixmap);
600 XSetWindowBorderWidth(dpy, icon->icon_win, 0);
601 XReparentWindow(dpy, icon->icon_win, icon->core->window,
602 (wPreferences.icon_size - width) / 2,
603 theight + (wPreferences.icon_size - height - theight) / 2);
604 if (resize)
605 XResizeWindow(dpy, icon->icon_win, width, height);
607 XMapWindow(dpy, icon->icon_win);
609 XAddToSaveSet(dpy, icon->icon_win);
611 icon->pixmap = pixmap;
613 if (XGetWindowAttributes(dpy, icon->icon_win, &attr)) {
614 if (attr.all_event_masks & ButtonPressMask) {
615 wHackedGrabButton(Button1, MOD_MASK, icon->core->window, True,
616 ButtonPressMask, GrabModeSync, GrabModeAsync,
617 None, wCursor[WCUR_ARROW]);
620 } else if (wwin && wwin->net_icon_image) {
621 /* Use _NET_WM_ICON icon */
622 icon->pixmap = makeIcon(scr, wwin->net_icon_image, icon->show_title,
623 icon->shadowed, icon->tile_type, icon->highlighted);
624 } else if (wwin && wwin->wm_hints && (wwin->wm_hints->flags & IconPixmapHint)) {
625 int x, y;
626 unsigned int w, h;
627 Window jw;
628 int ji;
629 unsigned int ju, d;
630 Pixmap pixmap;
632 if (!XGetGeometry(dpy, wwin->wm_hints->icon_pixmap, &jw, &ji, &ji, &w, &h, &ju, &d)) {
633 icon->owner->wm_hints->flags &= ~IconPixmapHint;
634 goto user_icon;
637 pixmap = XCreatePixmap(dpy, icon->core->window, wPreferences.icon_size,
638 wPreferences.icon_size, scr->w_depth);
639 XSetClipMask(dpy, scr->copy_gc, None);
640 XCopyArea(dpy, scr->icon_tile_pixmap, pixmap, scr->copy_gc, 0, 0,
641 wPreferences.icon_size, wPreferences.icon_size, 0, 0);
643 if (w > wPreferences.icon_size)
644 w = wPreferences.icon_size;
645 x = (wPreferences.icon_size - w) / 2;
647 if (icon->show_title && (title_height < wPreferences.icon_size)) {
648 drawIconTitle(scr, pixmap, title_height);
650 if (h > wPreferences.icon_size - title_height - 2) {
651 h = wPreferences.icon_size - title_height - 2;
652 y = title_height + 1;
653 } else {
654 y = (wPreferences.icon_size - h - title_height) / 2 + title_height + 1;
656 } else {
657 if (w > wPreferences.icon_size)
658 w = wPreferences.icon_size;
659 y = (wPreferences.icon_size - h) / 2;
662 if (wwin->wm_hints->flags & IconMaskHint)
663 XSetClipMask(dpy, scr->copy_gc, wwin->wm_hints->icon_mask);
665 XSetClipOrigin(dpy, scr->copy_gc, x, y);
667 if (d != scr->w_depth) {
668 XSetForeground(dpy, scr->copy_gc, scr->black_pixel);
669 XSetBackground(dpy, scr->copy_gc, scr->white_pixel);
670 XCopyPlane(dpy, wwin->wm_hints->icon_pixmap, pixmap, scr->copy_gc, 0, 0, w, h, x, y, 1);
671 } else {
672 XCopyArea(dpy, wwin->wm_hints->icon_pixmap, pixmap, scr->copy_gc, 0, 0, w, h, x, y);
675 XSetClipOrigin(dpy, scr->copy_gc, 0, 0);
677 icon->pixmap = pixmap;
678 } else {
679 user_icon:
681 if (icon->file_image) {
682 icon->pixmap = makeIcon(scr, icon->file_image, icon->show_title,
683 icon->shadowed, icon->tile_type, icon->highlighted);
684 } else {
685 /* make default icons */
687 if (!scr->def_icon_pixmap) {
688 RImage *image = NULL;
689 char *path;
690 char *file;
692 file = wDefaultGetIconFile(scr, NULL, NULL, False);
693 if (file) {
694 path = FindImage(wPreferences.icon_path, file);
695 if (!path) {
696 wwarning(_("could not find default icon \"%s\""), file);
697 goto make_icons;
700 image = RLoadImage(scr->rcontext, path, 0);
701 if (!image) {
702 wwarning(_("could not load default icon \"%s\":%s"),
703 file, RMessageForError(RErrorCode));
705 wfree(path);
707 make_icons:
709 image = wIconValidateIconSize(scr, image);
710 scr->def_icon_pixmap = makeIcon(scr, image, False, False, icon->tile_type, icon->highlighted);
711 scr->def_ticon_pixmap = makeIcon(scr, image, True, False, icon->tile_type, icon->highlighted);
712 if (image)
713 RReleaseImage(image);
716 if (icon->show_title) {
717 XSetWindowBackgroundPixmap(dpy, icon->core->window, scr->def_ticon_pixmap);
718 } else {
719 XSetWindowBackgroundPixmap(dpy, icon->core->window, scr->def_icon_pixmap);
721 icon->pixmap = None;
724 if (icon->pixmap != None) {
725 XSetWindowBackgroundPixmap(dpy, icon->core->window, icon->pixmap);
727 XClearWindow(dpy, icon->core->window);
729 wIconPaint(icon);
732 void wIconPaint(WIcon * icon)
734 WScreen *scr = icon->core->screen_ptr;
735 int x;
736 char *tmp;
738 if (icon->force_paint) {
739 icon->force_paint = 0;
740 wIconUpdate(icon);
741 return;
744 XClearWindow(dpy, icon->core->window);
746 /* draw the icon title */
747 if (icon->show_title && icon->icon_name != NULL) {
748 int l;
749 int w;
751 tmp = ShrinkString(scr->icon_title_font, icon->icon_name, wPreferences.icon_size - 4);
752 w = WMWidthOfString(scr->icon_title_font, tmp, l = strlen(tmp));
754 if (w > icon->core->width - 4)
755 x = (icon->core->width - 4) - w;
756 else
757 x = (icon->core->width - w) / 2;
759 WMDrawString(scr->wmscreen, icon->core->window, scr->icon_title_color,
760 scr->icon_title_font, x, 1, tmp, l);
761 wfree(tmp);
764 if (icon->selected)
765 XDrawRectangle(dpy, icon->core->window, scr->icon_select_gc, 0, 0,
766 icon->core->width - 1, icon->core->height - 1);
769 /******************************************************************/
771 static void miniwindowExpose(WObjDescriptor * desc, XEvent * event)
773 wIconPaint(desc->parent);
776 static void miniwindowDblClick(WObjDescriptor * desc, XEvent * event)
778 WIcon *icon = desc->parent;
780 assert(icon->owner != NULL);
782 wDeiconifyWindow(icon->owner);
785 static void miniwindowMouseDown(WObjDescriptor * desc, XEvent * event)
787 WIcon *icon = desc->parent;
788 WWindow *wwin = icon->owner;
789 XEvent ev;
790 int x = wwin->icon_x, y = wwin->icon_y;
791 int dx = event->xbutton.x, dy = event->xbutton.y;
792 int grabbed = 0;
793 int clickButton = event->xbutton.button;
794 Bool hasMoved = False;
796 if (WCHECK_STATE(WSTATE_MODAL))
797 return;
799 if (IsDoubleClick(icon->core->screen_ptr, event)) {
800 miniwindowDblClick(desc, event);
801 return;
804 if (event->xbutton.button == Button1) {
805 if (event->xbutton.state & MOD_MASK)
806 wLowerFrame(icon->core);
807 else
808 wRaiseFrame(icon->core);
809 if (event->xbutton.state & ShiftMask) {
810 wIconSelect(icon);
811 wSelectWindow(icon->owner, !wwin->flags.selected);
813 } else if (event->xbutton.button == Button3) {
814 WObjDescriptor *desc;
816 OpenMiniwindowMenu(wwin, event->xbutton.x_root, event->xbutton.y_root);
818 /* allow drag select of menu */
819 desc = &wwin->screen_ptr->window_menu->menu->descriptor;
820 event->xbutton.send_event = True;
821 (*desc->handle_mousedown) (desc, event);
823 return;
826 if (XGrabPointer(dpy, icon->core->window, False, ButtonMotionMask
827 | ButtonReleaseMask | ButtonPressMask, GrabModeAsync,
828 GrabModeAsync, None, None, CurrentTime) != GrabSuccess) {
830 while (1) {
831 WMMaskEvent(dpy, PointerMotionMask | ButtonReleaseMask | ButtonPressMask
832 | ButtonMotionMask | ExposureMask, &ev);
833 switch (ev.type) {
834 case Expose:
835 WMHandleEvent(&ev);
836 break;
838 case MotionNotify:
839 hasMoved = True;
840 if (!grabbed) {
841 if (abs(dx - ev.xmotion.x) >= MOVE_THRESHOLD
842 || abs(dy - ev.xmotion.y) >= MOVE_THRESHOLD) {
843 XChangeActivePointerGrab(dpy, ButtonMotionMask
844 | ButtonReleaseMask | ButtonPressMask,
845 wCursor[WCUR_MOVE], CurrentTime);
846 grabbed = 1;
847 } else {
848 break;
851 x = ev.xmotion.x_root - dx;
852 y = ev.xmotion.y_root - dy;
853 XMoveWindow(dpy, icon->core->window, x, y);
854 break;
856 case ButtonPress:
857 break;
859 case ButtonRelease:
860 if (ev.xbutton.button != clickButton)
861 break;
863 if (wwin->icon_x != x || wwin->icon_y != y)
864 wwin->flags.icon_moved = 1;
866 XMoveWindow(dpy, icon->core->window, x, y);
868 wwin->icon_x = x;
869 wwin->icon_y = y;
870 XUngrabPointer(dpy, CurrentTime);
872 if (wPreferences.auto_arrange_icons)
873 wArrangeIcons(wwin->screen_ptr, True);
874 if (wPreferences.single_click && !hasMoved)
875 miniwindowDblClick(desc, event);
876 return;