wIconStore(): do not save the icon if it exists
[wmaker-crm.git] / src / icon.c
blob73dae74f223cf913cd8f3e57bd951daf2a1262a8
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 static WIcon *wIconCreateCore(WScreen *scr, int coord_x, int coord_y);
61 void get_pixmap_icon_from_icon_win(WIcon *icon);
62 int get_pixmap_icon_from_wm_hints(WScreen *scr, WWindow *wwin, WIcon *icon);
63 void get_pixmap_icon_from_user_icon(WScreen *scr, WIcon * icon);
64 /****** Notification Observers ******/
66 static void appearanceObserver(void *self, WMNotification * notif)
68 WIcon *icon = (WIcon *) self;
69 uintptr_t flags = (uintptr_t)WMGetNotificationClientData(notif);
71 if (flags & WTextureSettings) {
72 icon->force_paint = 1;
74 if (flags & WFontSettings) {
75 icon->force_paint = 1;
78 if (flags & WColorSettings) {
82 wIconPaint(icon);
84 /* so that the appicon expose handlers will paint the appicon specific
85 * stuff */
86 XClearArea(dpy, icon->core->window, 0, 0, icon->core->width, icon->core->height, True);
89 static void tileObserver(void *self, WMNotification * notif)
91 WIcon *icon = (WIcon *) self;
93 icon->force_paint = 1;
94 wIconPaint(icon);
96 XClearArea(dpy, icon->core->window, 0, 0, 1, 1, True);
99 /************************************/
101 INLINE static void getSize(Drawable d, unsigned int *w, unsigned int *h, unsigned int *dep)
103 Window rjunk;
104 int xjunk, yjunk;
105 unsigned int bjunk;
107 XGetGeometry(dpy, d, &rjunk, &xjunk, &yjunk, w, h, &bjunk, dep);
110 WIcon *wIconCreate(WWindow * wwin)
112 WScreen *scr = wwin->screen_ptr;
113 WIcon *icon;
114 char *file;
116 icon = wIconCreateCore(scr, wwin->icon_x, wwin->icon_y);
118 icon->owner = wwin;
119 if (wwin->wm_hints && (wwin->wm_hints->flags & IconWindowHint)) {
120 if (wwin->client_win == wwin->main_window) {
121 WApplication *wapp;
122 /* do not let miniwindow steal app-icon's icon window */
123 wapp = wApplicationOf(wwin->client_win);
124 if (!wapp || wapp->app_icon == NULL)
125 icon->icon_win = wwin->wm_hints->icon_window;
126 } else {
127 icon->icon_win = wwin->wm_hints->icon_window;
130 #ifdef NO_MINIWINDOW_TITLES
131 icon->show_title = 0;
132 #else
133 icon->show_title = 1;
134 #endif
135 icon->file_image = wDefaultGetImage(scr, wwin->wm_instance, wwin->wm_class, wPreferences.icon_size);
137 file = wDefaultGetIconFile(scr, wwin->wm_instance, wwin->wm_class, False);
138 if (file)
139 icon->file = wstrdup(file);
141 icon->icon_name = wNETWMGetIconName(wwin->client_win);
142 if (icon->icon_name)
143 wwin->flags.net_has_icon_title = 1;
144 else
145 wGetIconName(dpy, wwin->client_win, &icon->icon_name);
147 icon->tile_type = TILE_NORMAL;
149 wIconUpdate(icon);
151 XFlush(dpy);
153 WMAddNotificationObserver(appearanceObserver, icon, WNIconAppearanceSettingsChanged, icon);
154 WMAddNotificationObserver(tileObserver, icon, WNIconTileSettingsChanged, icon);
155 return icon;
158 WIcon *wIconCreateWithIconFile(WScreen * scr, char *iconfile, int tile)
160 WIcon *icon;
162 icon = wIconCreateCore(scr, 0, 0);
164 if (iconfile) {
165 icon->file_image = RLoadImage(scr->rcontext, iconfile, 0);
166 if (!icon->file_image) {
167 wwarning(_("error loading image file \"%s\": %s"), iconfile, RMessageForError(RErrorCode));
170 icon->file_image = wIconValidateIconSize(scr, icon->file_image, wPreferences.icon_size);
172 icon->file = wstrdup(iconfile);
175 icon->tile_type = tile;
177 wIconUpdate(icon);
179 WMAddNotificationObserver(appearanceObserver, icon, WNIconAppearanceSettingsChanged, icon);
180 WMAddNotificationObserver(tileObserver, icon, WNIconTileSettingsChanged, icon);
182 return icon;
185 static WIcon *wIconCreateCore(WScreen *scr, int coord_x, int coord_y)
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,
194 coord_x,
195 coord_y,
196 wPreferences.icon_size,
197 wPreferences.icon_size,
200 if (wPreferences.use_saveunders) {
201 vmask = CWSaveUnder;
202 attribs.save_under = True;
205 /* a white border for selecting it */
206 vmask |= CWBorderPixel;
207 attribs.border_pixel = scr->white_pixel;
209 XChangeWindowAttributes(dpy, icon->core->window, vmask, &attribs);
211 /* will be overriden if this is a application icon */
212 icon->core->descriptor.handle_mousedown = miniwindowMouseDown;
213 icon->core->descriptor.handle_expose = miniwindowExpose;
214 icon->core->descriptor.parent_type = WCLASS_MINIWINDOW;
215 icon->core->descriptor.parent = icon;
217 icon->core->stacking = wmalloc(sizeof(WStacking));
218 icon->core->stacking->above = NULL;
219 icon->core->stacking->under = NULL;
220 icon->core->stacking->window_level = NORMAL_ICON_LEVEL;
221 icon->core->stacking->child_of = NULL;
223 return icon;
226 void wIconDestroy(WIcon * icon)
228 WCoreWindow *core = icon->core;
229 WScreen *scr = core->screen_ptr;
231 WMRemoveNotificationObserver(icon);
233 if (icon->handlerID)
234 WMDeleteTimerHandler(icon->handlerID);
236 if (icon->icon_win) {
237 int x = 0, y = 0;
239 if (icon->owner) {
240 x = icon->owner->icon_x;
241 y = icon->owner->icon_y;
243 XUnmapWindow(dpy, icon->icon_win);
244 XReparentWindow(dpy, icon->icon_win, scr->root_win, x, y);
246 if (icon->icon_name)
247 XFree(icon->icon_name);
249 if (icon->pixmap)
250 XFreePixmap(dpy, icon->pixmap);
252 if (icon->file)
253 wfree(icon->file);
255 if (icon->file_image != NULL)
256 RReleaseImage(icon->file_image);
258 wCoreDestroy(icon->core);
259 wfree(icon);
262 static void drawIconTitle(WScreen * scr, Pixmap pixmap, int height)
264 XFillRectangle(dpy, pixmap, scr->icon_title_texture->normal_gc, 0, 0, wPreferences.icon_size, height + 1);
265 XDrawLine(dpy, pixmap, scr->icon_title_texture->light_gc, 0, 0, wPreferences.icon_size, 0);
266 XDrawLine(dpy, pixmap, scr->icon_title_texture->light_gc, 0, 0, 0, height + 1);
267 XDrawLine(dpy, pixmap, scr->icon_title_texture->dim_gc,
268 wPreferences.icon_size - 1, 0, wPreferences.icon_size - 1, height + 1);
271 static Pixmap makeIcon(WScreen *scr, RImage *icon, int titled, int shadowed, int tileType, int highlighted)
273 RImage *tile;
274 Pixmap pixmap;
275 int x, y, sx, sy;
276 unsigned w, h;
277 int theight = 0;
279 if (tileType == TILE_NORMAL) {
280 tile = RCloneImage(scr->icon_tile);
281 } else {
282 assert(scr->clip_tile);
283 tile = RCloneImage(scr->clip_tile);
286 if (icon) {
287 w = (icon->width > wPreferences.icon_size)
288 ? wPreferences.icon_size : icon->width;
289 x = (wPreferences.icon_size - w) / 2;
290 sx = (icon->width - w) / 2;
292 if (titled)
293 theight = WMFontHeight(scr->icon_title_font);
295 h = (icon->height + theight > wPreferences.icon_size
296 ? wPreferences.icon_size - theight : icon->height);
297 y = theight + (wPreferences.icon_size - theight - h) / 2;
298 sy = (icon->height - h) / 2;
300 RCombineArea(tile, icon, sx, sy, w, h, x, y);
303 if (shadowed) {
304 RColor color;
306 color.red = scr->icon_back_texture->light.red >> 8;
307 color.green = scr->icon_back_texture->light.green >> 8;
308 color.blue = scr->icon_back_texture->light.blue >> 8;
309 color.alpha = 150; /* about 60% */
310 RClearImage(tile, &color);
313 if (highlighted) {
314 RColor color;
316 color.red = color.green = color.blue = 0;
317 color.alpha = 160;
318 RLightImage(tile, &color);
321 if (!RConvertImage(scr->rcontext, tile, &pixmap))
322 wwarning(_("error rendering image:%s"), RMessageForError(RErrorCode));
324 RReleaseImage(tile);
326 if (titled)
327 drawIconTitle(scr, pixmap, theight);
329 return pixmap;
332 void wIconChangeTitle(WIcon * icon, char *new_title)
334 int changed;
336 changed = (new_title == NULL && icon->icon_name != NULL)
337 || (new_title != NULL && icon->icon_name == NULL);
339 if (icon->icon_name != NULL)
340 XFree(icon->icon_name);
342 icon->icon_name = new_title;
344 if (changed)
345 icon->force_paint = 1;
346 wIconPaint(icon);
349 RImage *wIconValidateIconSize(WScreen * scr, RImage * icon, int max_size)
351 RImage *tmp;
352 int w, h;
354 if (!icon)
355 return NULL;
356 #ifndef DONT_SCALE_ICONS
357 if (max_size != 64) {
358 w = max_size * icon->width / 64;
359 h = max_size * icon->height / 64;
361 tmp = RScaleImage(icon, w, h);
362 RReleaseImage(icon);
363 icon = tmp;
365 #endif
367 return icon;
370 Bool wIconChangeImageFile(WIcon * icon, char *file)
372 WScreen *scr = icon->core->screen_ptr;
373 RImage *image;
374 char *path;
375 int error = 0;
377 if (icon->file_image)
378 RReleaseImage(icon->file_image);
380 if (!file) {
381 icon->file_image = NULL;
382 wIconUpdate(icon);
383 return True;
386 path = FindImage(wPreferences.icon_path, file);
388 if (path && (image = RLoadImage(scr->rcontext, path, 0))) {
389 icon->file_image = wIconValidateIconSize(icon->core->screen_ptr, image, wPreferences.icon_size);
390 wIconUpdate(icon);
391 } else {
392 error = 1;
395 if (path)
396 wfree(path);
398 return !error;
401 static char *getnameforicon(WWindow * wwin)
403 char *prefix, *suffix;
404 char *path;
405 int len;
407 if (wwin->wm_class && wwin->wm_instance) {
408 int len = strlen(wwin->wm_class) + strlen(wwin->wm_instance) + 2;
409 suffix = wmalloc(len);
410 snprintf(suffix, len, "%s.%s", wwin->wm_instance, wwin->wm_class);
411 } else if (wwin->wm_class) {
412 int len = strlen(wwin->wm_class) + 1;
413 suffix = wmalloc(len);
414 snprintf(suffix, len, "%s", wwin->wm_class);
415 } else if (wwin->wm_instance) {
416 int len = strlen(wwin->wm_instance) + 1;
417 suffix = wmalloc(len);
418 snprintf(suffix, len, "%s", wwin->wm_instance);
419 } else {
420 return NULL;
423 prefix = wusergnusteppath();
424 len = strlen(prefix) + 64 + strlen(suffix);
425 path = wmalloc(len + 1);
426 snprintf(path, len, "%s/Library/WindowMaker", prefix);
428 if (access(path, F_OK) != 0) {
429 if (mkdir(path, S_IRUSR | S_IWUSR | S_IXUSR)) {
430 werror(_("could not create directory %s"), path);
431 wfree(path);
432 wfree(suffix);
433 return NULL;
436 strcat(path, "/CachedPixmaps");
437 if (access(path, F_OK) != 0) {
438 if (mkdir(path, S_IRUSR | S_IWUSR | S_IXUSR) != 0) {
439 werror(_("could not create directory %s"), path);
440 wfree(path);
441 wfree(suffix);
442 return NULL;
446 strcat(path, "/");
447 strcat(path, suffix);
448 strcat(path, ".xpm");
449 wfree(suffix);
451 return path;
455 * wIconStore--
456 * Stores the client supplied icon at ~/GNUstep/Library/WindowMaker/CachedPixmaps
457 * and returns the path for that icon. Returns NULL if there is no
458 * client supplied icon or on failure.
460 * Side effects:
461 * New directories might be created.
463 char *wIconStore(WIcon * icon)
465 char *path;
466 RImage *image = NULL;
467 WWindow *wwin = icon->owner;
469 if (!wwin)
470 return NULL;
472 path = getnameforicon(wwin);
473 if (!path)
474 return NULL;
476 /* If icon exists, exit */
477 if (access(path, F_OK) == 0)
478 return path;
480 if (wwin->net_icon_image) {
481 image = RRetainImage(wwin->net_icon_image);
482 } else if (wwin->wm_hints && (wwin->wm_hints->flags & IconPixmapHint)
483 && wwin->wm_hints->icon_pixmap != None) {
484 image = RCreateImageFromDrawable(icon->core->screen_ptr->rcontext,
485 wwin->wm_hints->icon_pixmap, (wwin->wm_hints->flags & IconMaskHint)
486 ? wwin->wm_hints->icon_mask : None);
489 if (!image) {
490 wfree(path);
491 return NULL;
494 if (!RSaveImage(image, path, "XPM")) {
495 wfree(path);
496 path = NULL;
499 RReleaseImage(image);
501 return path;
504 static void cycleColor(void *data)
506 WIcon *icon = (WIcon *) data;
507 WScreen *scr = icon->core->screen_ptr;
508 XGCValues gcv;
510 icon->step--;
511 gcv.dash_offset = icon->step;
512 XChangeGC(dpy, scr->icon_select_gc, GCDashOffset, &gcv);
514 XDrawRectangle(dpy, icon->core->window, scr->icon_select_gc, 0, 0,
515 icon->core->width - 1, icon->core->height - 1);
516 icon->handlerID = WMAddTimerHandler(COLOR_CYCLE_DELAY, cycleColor, icon);
519 #ifdef NEWAPPICON
520 void wIconSetHighlited(WIcon *icon, Bool flag)
522 if (icon->highlighted == flag)
523 return;
525 icon->highlighted = flag;
526 icon->force_paint = True;
527 wIconPaint(icon);
529 #endif
531 void wIconSelect(WIcon * icon)
533 WScreen *scr = icon->core->screen_ptr;
534 icon->selected = !icon->selected;
536 if (icon->selected) {
537 icon->step = 0;
538 if (!wPreferences.dont_blink)
539 icon->handlerID = WMAddTimerHandler(10, cycleColor, icon);
540 else
541 XDrawRectangle(dpy, icon->core->window, scr->icon_select_gc, 0, 0,
542 icon->core->width - 1, icon->core->height - 1);
543 } else {
544 if (icon->handlerID) {
545 WMDeleteTimerHandler(icon->handlerID);
546 icon->handlerID = NULL;
548 XClearArea(dpy, icon->core->window, 0, 0, icon->core->width, icon->core->height, True);
552 void wIconUpdate(WIcon * icon)
554 WScreen *scr = icon->core->screen_ptr;
555 WWindow *wwin = icon->owner;
557 assert(scr->icon_tile != NULL);
559 if (icon->pixmap != None)
560 XFreePixmap(dpy, icon->pixmap);
562 icon->pixmap = None;
564 if (wwin && WFLAGP(wwin, always_user_icon)) {
565 /* Forced use user_icon */
566 get_pixmap_icon_from_user_icon(scr, icon);
567 } else if (icon->icon_win != None) {
568 /* Get the Pixmap from the WIcon */
569 get_pixmap_icon_from_icon_win(icon);
570 } else if (wwin && wwin->net_icon_image) {
571 /* Use _NET_WM_ICON icon */
572 icon->pixmap = makeIcon(scr, wwin->net_icon_image, icon->show_title,
573 icon->shadowed, icon->tile_type, icon->highlighted);
574 } else if (wwin && wwin->wm_hints && (wwin->wm_hints->flags & IconPixmapHint)) {
575 /* Get the Pixmap from the wm_hints, else, from the user */
576 if (get_pixmap_icon_from_wm_hints(scr, wwin, icon))
577 get_pixmap_icon_from_user_icon(scr, icon);
578 } else {
579 /* Get the Pixmap from the user */
580 get_pixmap_icon_from_user_icon(scr, icon);
583 /* No pixmap, set default background */
584 if (icon->pixmap != None)
585 XSetWindowBackgroundPixmap(dpy, icon->core->window, icon->pixmap);
587 /* Paint it */
588 XClearWindow(dpy, icon->core->window);
589 wIconPaint(icon);
592 void get_pixmap_icon_from_user_icon(WScreen *scr, WIcon * icon)
594 RImage *image = NULL;
595 char *path;
596 char *file;
598 if (icon->file_image) {
599 icon->pixmap = makeIcon(scr, icon->file_image, icon->show_title,
600 icon->shadowed, icon->tile_type, icon->highlighted);
601 } else {
602 /* make default icons */
603 if (!scr->def_icon_pixmap) {
604 file = wDefaultGetIconFile(scr, NULL, NULL, False);
605 if (file) {
606 path = FindImage(wPreferences.icon_path, file);
607 if (path) {
608 image = RLoadImage(scr->rcontext, path, 0);
609 if (!image)
610 wwarning(_("could not load default icon \"%s\":%s"),
611 file, RMessageForError(RErrorCode));
612 wfree(path);
613 } else {
614 wwarning(_("could not find default icon \"%s\""), file);
616 /* FIXME: Probably wfree(file) here! */
619 image = wIconValidateIconSize(scr, image, wPreferences.icon_size);
620 scr->def_icon_pixmap = makeIcon(scr, image, False, False, icon->tile_type, icon->highlighted);
621 scr->def_ticon_pixmap = makeIcon(scr, image, True, False, icon->tile_type, icon->highlighted);
622 if (image)
623 RReleaseImage(image);
626 if (icon->show_title)
627 XSetWindowBackgroundPixmap(dpy, icon->core->window, scr->def_ticon_pixmap);
628 else
629 XSetWindowBackgroundPixmap(dpy, icon->core->window, scr->def_icon_pixmap);
631 icon->pixmap = None;
635 /* Get the Pixmap from the WIcon of the WWindow */
636 void get_pixmap_icon_from_icon_win(WIcon * icon)
638 XWindowAttributes attr;
639 WScreen *scr = icon->core->screen_ptr;
640 int title_height = WMFontHeight(scr->icon_title_font);
641 unsigned int width, height, depth;
642 int theight;
643 int resize = 0;
644 Pixmap pixmap;
646 getSize(icon->icon_win, &width, &height, &depth);
648 if (width > wPreferences.icon_size) {
649 resize = 1;
650 width = wPreferences.icon_size;
653 if (height > wPreferences.icon_size) {
654 resize = 1;
655 height = wPreferences.icon_size;
658 if (icon->show_title && (height + title_height < wPreferences.icon_size)) {
659 pixmap = XCreatePixmap(dpy, scr->w_win, wPreferences.icon_size,
660 wPreferences.icon_size, scr->w_depth);
661 XSetClipMask(dpy, scr->copy_gc, None);
662 XCopyArea(dpy, scr->icon_tile_pixmap, pixmap, scr->copy_gc, 0, 0,
663 wPreferences.icon_size, wPreferences.icon_size, 0, 0);
664 drawIconTitle(scr, pixmap, title_height);
665 theight = title_height;
666 } else {
667 pixmap = None;
668 theight = 0;
669 XSetWindowBackgroundPixmap(dpy, icon->core->window, scr->icon_tile_pixmap);
672 XSetWindowBorderWidth(dpy, icon->icon_win, 0);
673 XReparentWindow(dpy, icon->icon_win, icon->core->window,
674 (wPreferences.icon_size - width) / 2,
675 theight + (wPreferences.icon_size - height - theight) / 2);
676 if (resize)
677 XResizeWindow(dpy, icon->icon_win, width, height);
679 XMapWindow(dpy, icon->icon_win);
680 XAddToSaveSet(dpy, icon->icon_win);
682 /* Save it */
683 icon->pixmap = pixmap;
685 if ((XGetWindowAttributes(dpy, icon->icon_win, &attr)) &&
686 (attr.all_event_masks & ButtonPressMask))
687 wHackedGrabButton(Button1, MOD_MASK, icon->core->window, True,
688 ButtonPressMask, GrabModeSync, GrabModeAsync,
689 None, wCursor[WCUR_ARROW]);
692 /* Get the Pixmap from the XWindow wm_hints */
693 int get_pixmap_icon_from_wm_hints(WScreen *scr, WWindow *wwin, WIcon *icon)
695 Window jw;
696 Pixmap pixmap;
697 unsigned int w, h, ju, d;
698 int ji, x, y;
699 int title_height = WMFontHeight(scr->icon_title_font);
701 if (!XGetGeometry(dpy, wwin->wm_hints->icon_pixmap, &jw, &ji, &ji, &w, &h, &ju, &d)) {
702 icon->owner->wm_hints->flags &= ~IconPixmapHint;
703 return(1);
706 pixmap = XCreatePixmap(dpy, icon->core->window, wPreferences.icon_size,
707 wPreferences.icon_size, scr->w_depth);
708 XSetClipMask(dpy, scr->copy_gc, None);
709 XCopyArea(dpy, scr->icon_tile_pixmap, pixmap, scr->copy_gc, 0, 0,
710 wPreferences.icon_size, wPreferences.icon_size, 0, 0);
712 if (w > wPreferences.icon_size)
713 w = wPreferences.icon_size;
714 x = (wPreferences.icon_size - w) / 2;
716 if (icon->show_title && (title_height < wPreferences.icon_size)) {
717 drawIconTitle(scr, pixmap, title_height);
719 if (h > wPreferences.icon_size - title_height - 2) {
720 h = wPreferences.icon_size - title_height - 2;
721 y = title_height + 1;
722 } else {
723 y = (wPreferences.icon_size - h - title_height) / 2 + title_height + 1;
725 } else {
726 if (w > wPreferences.icon_size)
727 w = wPreferences.icon_size;
728 y = (wPreferences.icon_size - h) / 2;
731 if (wwin->wm_hints->flags & IconMaskHint)
732 XSetClipMask(dpy, scr->copy_gc, wwin->wm_hints->icon_mask);
734 XSetClipOrigin(dpy, scr->copy_gc, x, y);
736 if (d != scr->w_depth) {
737 XSetForeground(dpy, scr->copy_gc, scr->black_pixel);
738 XSetBackground(dpy, scr->copy_gc, scr->white_pixel);
739 XCopyPlane(dpy, wwin->wm_hints->icon_pixmap, pixmap, scr->copy_gc, 0, 0, w, h, x, y, 1);
740 } else {
741 XCopyArea(dpy, wwin->wm_hints->icon_pixmap, pixmap, scr->copy_gc, 0, 0, w, h, x, y);
744 XSetClipOrigin(dpy, scr->copy_gc, 0, 0);
746 icon->pixmap = pixmap;
747 return (0);
750 void wIconPaint(WIcon * icon)
752 WScreen *scr = icon->core->screen_ptr;
753 int x;
754 char *tmp;
756 if (icon->force_paint) {
757 icon->force_paint = 0;
758 wIconUpdate(icon);
759 return;
762 XClearWindow(dpy, icon->core->window);
764 /* draw the icon title */
765 if (icon->show_title && icon->icon_name != NULL) {
766 int l;
767 int w;
769 tmp = ShrinkString(scr->icon_title_font, icon->icon_name, wPreferences.icon_size - 4);
770 w = WMWidthOfString(scr->icon_title_font, tmp, l = strlen(tmp));
772 if (w > icon->core->width - 4)
773 x = (icon->core->width - 4) - w;
774 else
775 x = (icon->core->width - w) / 2;
777 WMDrawString(scr->wmscreen, icon->core->window, scr->icon_title_color,
778 scr->icon_title_font, x, 1, tmp, l);
779 wfree(tmp);
782 if (icon->selected)
783 XDrawRectangle(dpy, icon->core->window, scr->icon_select_gc, 0, 0,
784 icon->core->width - 1, icon->core->height - 1);
787 /******************************************************************/
789 static void miniwindowExpose(WObjDescriptor * desc, XEvent * event)
791 wIconPaint(desc->parent);
794 static void miniwindowDblClick(WObjDescriptor * desc, XEvent * event)
796 WIcon *icon = desc->parent;
798 assert(icon->owner != NULL);
800 wDeiconifyWindow(icon->owner);
803 static void miniwindowMouseDown(WObjDescriptor * desc, XEvent * event)
805 WIcon *icon = desc->parent;
806 WWindow *wwin = icon->owner;
807 XEvent ev;
808 int x = wwin->icon_x, y = wwin->icon_y;
809 int dx = event->xbutton.x, dy = event->xbutton.y;
810 int grabbed = 0;
811 int clickButton = event->xbutton.button;
812 Bool hasMoved = False;
814 if (WCHECK_STATE(WSTATE_MODAL))
815 return;
817 if (IsDoubleClick(icon->core->screen_ptr, event)) {
818 miniwindowDblClick(desc, event);
819 return;
822 if (event->xbutton.button == Button1) {
823 if (event->xbutton.state & MOD_MASK)
824 wLowerFrame(icon->core);
825 else
826 wRaiseFrame(icon->core);
827 if (event->xbutton.state & ShiftMask) {
828 wIconSelect(icon);
829 wSelectWindow(icon->owner, !wwin->flags.selected);
831 } else if (event->xbutton.button == Button3) {
832 WObjDescriptor *desc;
834 OpenMiniwindowMenu(wwin, event->xbutton.x_root, event->xbutton.y_root);
836 /* allow drag select of menu */
837 desc = &wwin->screen_ptr->window_menu->menu->descriptor;
838 event->xbutton.send_event = True;
839 (*desc->handle_mousedown) (desc, event);
841 return;
844 if (XGrabPointer(dpy, icon->core->window, False, ButtonMotionMask
845 | ButtonReleaseMask | ButtonPressMask, GrabModeAsync,
846 GrabModeAsync, None, None, CurrentTime) != GrabSuccess) {
848 while (1) {
849 WMMaskEvent(dpy, PointerMotionMask | ButtonReleaseMask | ButtonPressMask
850 | ButtonMotionMask | ExposureMask, &ev);
851 switch (ev.type) {
852 case Expose:
853 WMHandleEvent(&ev);
854 break;
856 case MotionNotify:
857 hasMoved = True;
858 if (!grabbed) {
859 if (abs(dx - ev.xmotion.x) >= MOVE_THRESHOLD
860 || abs(dy - ev.xmotion.y) >= MOVE_THRESHOLD) {
861 XChangeActivePointerGrab(dpy, ButtonMotionMask
862 | ButtonReleaseMask | ButtonPressMask,
863 wCursor[WCUR_MOVE], CurrentTime);
864 grabbed = 1;
865 } else {
866 break;
869 x = ev.xmotion.x_root - dx;
870 y = ev.xmotion.y_root - dy;
871 XMoveWindow(dpy, icon->core->window, x, y);
872 break;
874 case ButtonPress:
875 break;
877 case ButtonRelease:
878 if (ev.xbutton.button != clickButton)
879 break;
881 if (wwin->icon_x != x || wwin->icon_y != y)
882 wwin->flags.icon_moved = 1;
884 XMoveWindow(dpy, icon->core->window, x, y);
886 wwin->icon_x = x;
887 wwin->icon_y = y;
888 XUngrabPointer(dpy, CurrentTime);
890 if (wPreferences.auto_arrange_icons)
891 wArrangeIcons(wwin->screen_ptr, True);
892 if (wPreferences.single_click && !hasMoved)
893 miniwindowDblClick(desc, event);
894 return;