Update Serbian translation from master branch
[wmaker-crm.git] / src / icon.c
blob943e2d8487806684dd51abf7bcd06f477856238b
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 "stacking.h"
42 #include "application.h"
43 #include "defaults.h"
44 #include "appicon.h"
45 #include "wmspec.h"
46 #include "misc.h"
47 #include "startup.h"
48 #include "event.h"
49 #include "winmenu.h"
51 /**** Global varianebles ****/
53 #define MOD_MASK wPreferences.modifier_mask
54 #define CACHE_ICON_PATH "/" PACKAGE_TARNAME "/CachedPixmaps"
55 #define ICON_BORDER 3
57 static void miniwindowExpose(WObjDescriptor *desc, XEvent *event);
58 static void miniwindowMouseDown(WObjDescriptor *desc, XEvent *event);
59 static void miniwindowDblClick(WObjDescriptor *desc, XEvent *event);
61 static WIcon *icon_create_core(WScreen *scr, int coord_x, int coord_y);
63 static void set_dockapp_in_icon(WIcon *icon);
64 static void get_rimage_icon_from_icon_win(WIcon *icon);
65 static void get_rimage_icon_from_user_icon(WIcon *icon);
66 static void get_rimage_icon_from_default_icon(WIcon *icon);
67 static void get_rimage_icon_from_x11(WIcon *icon);
69 static void icon_update_pixmap(WIcon *icon, RImage *image);
70 static void unset_icon_image(WIcon *icon);
72 /****** Notification Observers ******/
74 static void appearanceObserver(void *self, WMNotification *notif)
76 WIcon *icon = (WIcon *) self;
77 uintptr_t flags = (uintptr_t)WMGetNotificationClientData(notif);
79 if ((flags & WTextureSettings) || (flags & WFontSettings)) {
80 /* If the rimage exists, update the icon, else create it */
81 if (icon->file_image)
82 update_icon_pixmap(icon);
83 else
84 wIconPaint(icon);
87 /* so that the appicon expose handlers will paint the appicon specific
88 * stuff */
89 XClearArea(dpy, icon->core->window, 0, 0, icon->core->width, icon->core->height, True);
92 static void tileObserver(void *self, WMNotification *notif)
94 WIcon *icon = (WIcon *) self;
96 /* Parameter not used, but tell the compiler that it is ok */
97 (void) notif;
99 update_icon_pixmap(icon);
101 XClearArea(dpy, icon->core->window, 0, 0, 1, 1, True);
104 /************************************/
106 static int getSize(Drawable d, unsigned int *w, unsigned int *h, unsigned int *dep)
108 Window rjunk;
109 int xjunk, yjunk;
110 unsigned int bjunk;
112 return XGetGeometry(dpy, d, &rjunk, &xjunk, &yjunk, w, h, &bjunk, dep);
115 WIcon *icon_create_for_wwindow(WWindow *wwin)
117 WScreen *scr = wwin->screen_ptr;
118 WIcon *icon;
120 icon = icon_create_core(scr, wwin->icon_x, wwin->icon_y);
122 icon->owner = wwin;
123 if (wwin->wm_hints && (wwin->wm_hints->flags & IconWindowHint)) {
124 if (wwin->client_win == wwin->main_window) {
125 WApplication *wapp;
126 /* do not let miniwindow steal app-icon's icon window */
127 wapp = wApplicationOf(wwin->client_win);
128 if (!wapp || wapp->app_icon == NULL)
129 icon->icon_win = wwin->wm_hints->icon_window;
130 } else {
131 icon->icon_win = wwin->wm_hints->icon_window;
134 #ifdef NO_MINIWINDOW_TITLES
135 icon->show_title = 0;
136 #else
137 icon->show_title = 1;
138 #endif
140 wIconChangeTitle(icon, wwin);
141 icon->tile_type = TILE_NORMAL;
143 set_icon_image_from_database(icon, wwin->wm_instance, wwin->wm_class, NULL);
144 /* Update the icon, because icon could be NULL */
145 wIconUpdate(icon);
147 WMAddNotificationObserver(appearanceObserver, icon, WNIconAppearanceSettingsChanged, icon);
148 WMAddNotificationObserver(tileObserver, icon, WNIconTileSettingsChanged, icon);
150 return icon;
153 WIcon *icon_create_for_dock(WScreen *scr, const char *command, const char *wm_instance, const char *wm_class, int tile)
155 WIcon *icon;
157 icon = icon_create_core(scr, 0, 0);
158 icon->tile_type = tile;
160 set_icon_image_from_database(icon, wm_instance, wm_class, command);
161 /* Update the icon, because icon could be NULL */
162 wIconUpdate(icon);
164 WMAddNotificationObserver(appearanceObserver, icon, WNIconAppearanceSettingsChanged, icon);
165 WMAddNotificationObserver(tileObserver, icon, WNIconTileSettingsChanged, icon);
167 return icon;
170 static WIcon *icon_create_core(WScreen *scr, int coord_x, int coord_y)
172 WIcon *icon;
174 icon = wmalloc(sizeof(WIcon));
175 icon->core = wCoreCreateTopLevel(scr,
176 coord_x,
177 coord_y,
178 wPreferences.icon_size,
179 wPreferences.icon_size,
180 0, scr->w_depth, scr->w_visual, scr->w_colormap,
181 scr->white_pixel);
183 /* will be overriden if this is a application icon */
184 icon->core->descriptor.handle_mousedown = miniwindowMouseDown;
185 icon->core->descriptor.handle_expose = miniwindowExpose;
186 icon->core->descriptor.parent_type = WCLASS_MINIWINDOW;
187 icon->core->descriptor.parent = icon;
189 icon->core->stacking = wmalloc(sizeof(WStacking));
190 icon->core->stacking->above = NULL;
191 icon->core->stacking->under = NULL;
192 icon->core->stacking->window_level = NORMAL_ICON_LEVEL;
193 icon->core->stacking->child_of = NULL;
195 /* Icon image */
196 icon->file = NULL;
197 icon->file_image = NULL;
199 return icon;
202 void wIconDestroy(WIcon *icon)
204 WCoreWindow *core = icon->core;
205 WScreen *scr = core->screen_ptr;
207 WMRemoveNotificationObserver(icon);
209 if (icon->handlerID)
210 WMDeleteTimerHandler(icon->handlerID);
212 if (icon->icon_win) {
213 int x = 0, y = 0;
215 if (icon->owner) {
216 x = icon->owner->icon_x;
217 y = icon->owner->icon_y;
219 XUnmapWindow(dpy, icon->icon_win);
220 XReparentWindow(dpy, icon->icon_win, scr->root_win, x, y);
222 if (icon->icon_name)
223 XFree(icon->icon_name);
225 if (icon->pixmap)
226 XFreePixmap(dpy, icon->pixmap);
228 if (icon->mini_preview)
229 XFreePixmap(dpy, icon->mini_preview);
231 unset_icon_image(icon);
233 wCoreDestroy(icon->core);
234 wfree(icon);
237 static void drawIconTitleBackground(WScreen *scr, Pixmap pixmap, int height)
239 XFillRectangle(dpy, pixmap, scr->icon_title_texture->normal_gc, 0, 0, wPreferences.icon_size, height + 1);
240 XDrawLine(dpy, pixmap, scr->icon_title_texture->light_gc, 0, 0, wPreferences.icon_size, 0);
241 XDrawLine(dpy, pixmap, scr->icon_title_texture->light_gc, 0, 0, 0, height + 1);
242 XDrawLine(dpy, pixmap, scr->icon_title_texture->dim_gc,
243 wPreferences.icon_size - 1, 0, wPreferences.icon_size - 1, height + 1);
246 static void icon_update_pixmap(WIcon *icon, RImage *image)
248 RImage *tile;
249 Pixmap pixmap;
250 int x, y, sx, sy;
251 unsigned w, h;
252 int theight = 0;
253 WScreen *scr = icon->core->screen_ptr;
255 switch (icon->tile_type) {
256 case TILE_NORMAL:
257 tile = RCloneImage(scr->icon_tile);
258 break;
259 case TILE_CLIP:
260 tile = RCloneImage(scr->clip_tile);
261 break;
262 case TILE_DRAWER:
263 tile = RCloneImage(scr->drawer_tile);
264 break;
265 default:
267 * The icon has always rigth value, this case is
268 * only to avoid a compiler warning with "tile"
269 * "may be used uninitialized"
271 wwarning("Unknown tile type: %d.\n", icon->tile_type);
272 tile = RCloneImage(scr->icon_tile);
275 if (image) {
276 w = (image->width > wPreferences.icon_size)
277 ? wPreferences.icon_size : image->width;
278 x = (wPreferences.icon_size - w) / 2;
279 sx = (image->width - w) / 2;
281 if (icon->show_title)
282 theight = WMFontHeight(scr->icon_title_font);
284 h = (image->height + theight > wPreferences.icon_size
285 ? wPreferences.icon_size - theight : image->height);
286 y = theight + (wPreferences.icon_size - theight - h) / 2;
287 sy = (image->height - h) / 2;
289 RCombineArea(tile, image, sx, sy, w, h, x, y);
292 if (icon->shadowed) {
293 RColor color;
295 color.red = scr->icon_back_texture->light.red >> 8;
296 color.green = scr->icon_back_texture->light.green >> 8;
297 color.blue = scr->icon_back_texture->light.blue >> 8;
298 color.alpha = 150; /* about 60% */
299 RClearImage(tile, &color);
302 if (icon->highlighted) {
303 RColor color;
305 color.red = color.green = color.blue = 0;
306 color.alpha = 160;
307 RLightImage(tile, &color);
310 if (!RConvertImage(scr->rcontext, tile, &pixmap))
311 wwarning(_("error rendering image:%s"), RMessageForError(RErrorCode));
313 RReleaseImage(tile);
315 /* Draw the icon's title background (without text) */
316 if (icon->show_title)
317 drawIconTitleBackground(scr, pixmap, theight);
319 icon->pixmap = pixmap;
322 void wIconChangeTitle(WIcon *icon, WWindow *wwin)
324 if (!icon || !wwin)
325 return;
327 /* Remove the previous icon title */
328 if (icon->icon_name != NULL)
329 XFree(icon->icon_name);
331 /* Set the new one, using two methods to identify
332 the icon name or switch back to window name */
333 icon->icon_name = wNETWMGetIconName(wwin->client_win);
334 if (!icon->icon_name)
335 if (!wGetIconName(dpy, wwin->client_win, &icon->icon_name))
336 icon->icon_name = wNETWMGetWindowName(wwin->client_win);
339 RImage *wIconValidateIconSize(RImage *icon, int max_size)
341 RImage *nimage;
343 if (!icon)
344 return NULL;
346 /* We should hold "ICON_BORDER" (~2) pixels to include the icon border */
347 if (((max_size + ICON_BORDER) < icon->width) ||
348 ((max_size + ICON_BORDER) < icon->height)) {
349 if (icon->width > icon->height)
350 nimage = RScaleImage(icon, max_size - ICON_BORDER,
351 (icon->height * (max_size - ICON_BORDER) / icon->width));
352 else
353 nimage = RScaleImage(icon, (icon->width * (max_size - ICON_BORDER) / icon->height),
354 max_size - ICON_BORDER);
355 RReleaseImage(icon);
356 icon = nimage;
359 return icon;
362 int wIconChangeImageFile(WIcon *icon, const char *file)
364 WScreen *scr = icon->core->screen_ptr;
365 char *path;
366 RImage *image = NULL;
368 /* If no new image, don't do nothing */
369 if (!file)
370 return 1;
372 /* Find the new image */
373 path = FindImage(wPreferences.icon_path, file);
374 if (!path)
375 return 0;
377 image = get_rimage_from_file(scr, path, wPreferences.icon_size);
378 if (!image) {
379 wfree(path);
380 return 0;
383 /* Set the new image */
384 set_icon_image_from_image(icon, image);
385 icon->file = wstrdup(path);
386 update_icon_pixmap(icon);
388 wfree(path);
389 return 1;
392 static char *get_name_for_wwin(WWindow *wwin)
394 return get_name_for_instance_class(wwin->wm_instance, wwin->wm_class);
397 char *get_name_for_instance_class(const char *wm_instance, const char *wm_class)
399 char *suffix;
400 int len;
402 if (wm_class && wm_instance) {
403 len = strlen(wm_class) + strlen(wm_instance) + 2;
404 suffix = wmalloc(len);
405 snprintf(suffix, len, "%s.%s", wm_instance, wm_class);
406 } else if (wm_class) {
407 len = strlen(wm_class) + 1;
408 suffix = wmalloc(len);
409 snprintf(suffix, len, "%s", wm_class);
410 } else if (wm_instance) {
411 len = strlen(wm_instance) + 1;
412 suffix = wmalloc(len);
413 snprintf(suffix, len, "%s", wm_instance);
414 } else {
415 return NULL;
418 return suffix;
421 static char *get_icon_cache_path(void)
423 const char *prefix;
424 char *path;
425 int ret;
427 prefix = wuserdatapath();
428 path = wstrconcat(prefix, CACHE_ICON_PATH "/");
430 /* If the folder exists, exit */
431 if (access(path, F_OK) == 0)
432 return path;
434 /* Create the folder */
435 ret = wmkdirhier(path);
437 /* Exit 1 on success, 0 on failure */
438 if (ret == 1)
439 return path;
441 /* Fail */
442 wfree(path);
443 return NULL;
446 static RImage *get_wwindow_image_from_wmhints(WWindow *wwin, WIcon *icon)
448 RImage *image = NULL;
449 XWMHints *hints = wwin->wm_hints;
451 if (hints && (hints->flags & IconPixmapHint) && hints->icon_pixmap != None)
452 image = RCreateImageFromDrawable(icon->core->screen_ptr->rcontext,
453 hints->icon_pixmap,
454 (hints->flags & IconMaskHint)
455 ? hints->icon_mask : None);
457 return image;
461 * wIconStore--
462 * Stores the client supplied icon at CACHE_ICON_PATH
463 * and returns the path for that icon. Returns NULL if there is no
464 * client supplied icon or on failure.
466 * Side effects:
467 * New directories might be created.
469 char *wIconStore(WIcon *icon)
471 char *path, *dir_path, *file, *filename;
472 int len = 0;
473 RImage *image = NULL;
474 WWindow *wwin = icon->owner;
476 if (!wwin)
477 return NULL;
479 dir_path = get_icon_cache_path();
480 if (!dir_path)
481 return NULL;
483 file = get_name_for_wwin(wwin);
484 if (!file) {
485 wfree(dir_path);
486 return NULL;
489 /* Create the file name */
490 len = strlen(file) + 5;
491 filename = wmalloc(len);
492 snprintf(filename, len, "%s.xpm", file);
493 wfree(file);
495 /* Create the full path, including the filename */
496 len = strlen(dir_path) + strlen(filename) + 1;
497 path = wmalloc(len);
498 snprintf(path, len, "%s%s", dir_path, filename);
499 wfree(dir_path);
501 /* If icon exists, exit */
502 if (access(path, F_OK) == 0) {
503 wfree(path);
504 return filename;
507 if (wwin->net_icon_image)
508 image = RRetainImage(wwin->net_icon_image);
509 else
510 image = get_wwindow_image_from_wmhints(wwin, icon);
512 if (!image) {
513 wfree(path);
514 wfree(filename);
515 return NULL;
518 if (!RSaveImage(image, path, "XPM")) {
519 wfree(filename);
520 filename = NULL;
523 wfree(path);
524 RReleaseImage(image);
526 return filename;
529 void remove_cache_icon(char *filename)
531 char *cachepath;
533 if (!filename)
534 return;
536 cachepath = get_icon_cache_path();
537 if (!cachepath)
538 return;
540 /* Filename check/parse could be here */
541 if (!strncmp(filename, cachepath, strlen(cachepath)))
542 unlink(filename);
544 wfree(cachepath);
547 static void cycleColor(void *data)
549 WIcon *icon = (WIcon *) data;
550 WScreen *scr = icon->core->screen_ptr;
551 XGCValues gcv;
553 icon->step--;
554 gcv.dash_offset = icon->step;
555 XChangeGC(dpy, scr->icon_select_gc, GCDashOffset, &gcv);
557 XDrawRectangle(dpy, icon->core->window, scr->icon_select_gc, 0, 0,
558 icon->core->width - 1, icon->core->height - 1);
559 icon->handlerID = WMAddTimerHandler(COLOR_CYCLE_DELAY, cycleColor, icon);
562 void wIconSetHighlited(WIcon *icon, Bool flag)
564 if (icon->highlighted == flag)
565 return;
567 icon->highlighted = flag;
568 update_icon_pixmap(icon);
571 void wIconSelect(WIcon *icon)
573 WScreen *scr = icon->core->screen_ptr;
574 icon->selected = !icon->selected;
576 if (icon->selected) {
577 icon->step = 0;
578 if (!wPreferences.dont_blink)
579 icon->handlerID = WMAddTimerHandler(10, cycleColor, icon);
580 else
581 XDrawRectangle(dpy, icon->core->window, scr->icon_select_gc, 0, 0,
582 icon->core->width - 1, icon->core->height - 1);
583 } else {
584 if (icon->handlerID) {
585 WMDeleteTimerHandler(icon->handlerID);
586 icon->handlerID = NULL;
588 XClearArea(dpy, icon->core->window, 0, 0, icon->core->width, icon->core->height, True);
592 static void unset_icon_image(WIcon *icon)
594 if (icon->file) {
595 wfree(icon->file);
596 icon->file = NULL;
599 if (icon->file_image) {
600 RReleaseImage(icon->file_image);
601 icon->file_image = NULL;
605 void set_icon_image_from_image(WIcon *icon, RImage *image)
607 if (!icon)
608 return;
610 unset_icon_image(icon);
612 icon->file_image = NULL;
613 icon->file_image = image;
616 void set_icon_minipreview(WIcon *icon, RImage *image)
618 Pixmap tmp;
619 RImage *scaled_mini_preview;
620 WScreen *scr = icon->core->screen_ptr;
622 scaled_mini_preview = RSmoothScaleImage(image, wPreferences.minipreview_size - 2 * MINIPREVIEW_BORDER,
623 wPreferences.minipreview_size - 2 * MINIPREVIEW_BORDER);
625 if (RConvertImage(scr->rcontext, scaled_mini_preview, &tmp)) {
626 if (icon->mini_preview != None)
627 XFreePixmap(dpy, icon->mini_preview);
628 icon->mini_preview = tmp;
630 RReleaseImage(scaled_mini_preview);
633 void wIconUpdate(WIcon *icon)
635 WWindow *wwin = NULL;
637 if (icon && icon->owner)
638 wwin = icon->owner;
640 if (wwin && WFLAGP(wwin, always_user_icon)) {
641 /* Forced use user_icon */
642 get_rimage_icon_from_user_icon(icon);
643 } else if (icon->icon_win != None) {
644 /* Get the Pixmap from the WIcon */
645 get_rimage_icon_from_icon_win(icon);
646 } else if (wwin && wwin->net_icon_image) {
647 /* Use _NET_WM_ICON icon */
648 get_rimage_icon_from_x11(icon);
649 } else if (wwin && wwin->wm_hints && (wwin->wm_hints->flags & IconPixmapHint)) {
650 /* Get the Pixmap from the wm_hints, else, from the user */
651 unset_icon_image(icon);
652 icon->file_image = get_rimage_icon_from_wm_hints(icon);
653 if (!icon->file_image)
654 get_rimage_icon_from_user_icon(icon);
655 } else {
656 /* Get the Pixmap from the user */
657 get_rimage_icon_from_user_icon(icon);
660 update_icon_pixmap(icon);
663 void update_icon_pixmap(WIcon *icon)
665 if (icon->pixmap != None)
666 XFreePixmap(dpy, icon->pixmap);
668 icon->pixmap = None;
670 /* Create the pixmap */
671 if (icon->file_image)
672 icon_update_pixmap(icon, icon->file_image);
674 /* If dockapp, put inside the icon */
675 if (icon->icon_win != None) {
676 /* file_image is NULL, because is docked app */
677 icon_update_pixmap(icon, NULL);
678 set_dockapp_in_icon(icon);
681 /* No pixmap, set default background */
682 if (icon->pixmap != None)
683 XSetWindowBackgroundPixmap(dpy, icon->core->window, icon->pixmap);
685 /* Paint it */
686 wIconPaint(icon);
689 static void get_rimage_icon_from_x11(WIcon *icon)
691 /* Remove the icon image */
692 unset_icon_image(icon);
694 /* Set the new icon image */
695 icon->file_image = RRetainImage(icon->owner->net_icon_image);
698 static void get_rimage_icon_from_user_icon(WIcon *icon)
700 if (icon->file_image)
701 return;
703 get_rimage_icon_from_default_icon(icon);
706 static void get_rimage_icon_from_default_icon(WIcon *icon)
708 WScreen *scr = icon->core->screen_ptr;
710 /* If the icon don't have image, we should use the default image. */
711 if (!scr->def_icon_rimage)
712 scr->def_icon_rimage = get_default_image(scr);
714 /* Remove the icon image */
715 unset_icon_image(icon);
717 /* Set the new icon image */
718 icon->file_image = RRetainImage(scr->def_icon_rimage);
721 /* Get the RImage from the WIcon of the WWindow */
722 static void get_rimage_icon_from_icon_win(WIcon *icon)
724 RImage *image;
726 /* Create the new RImage */
727 image = get_window_image_from_x11(icon->icon_win);
729 /* Free the icon info */
730 unset_icon_image(icon);
732 /* Set the new info */
733 icon->file_image = image;
736 /* Set the dockapp in the WIcon */
737 static void set_dockapp_in_icon(WIcon *icon)
739 XWindowAttributes attr;
740 WScreen *scr = icon->core->screen_ptr;
741 unsigned int w, h, d;
743 /* Reparent the dock application to the icon */
745 /* We need the application size to center it
746 * and show in the correct position */
747 getSize(icon->icon_win, &w, &h, &d);
749 /* Set the background pixmap */
750 XSetWindowBackgroundPixmap(dpy, icon->core->window, scr->icon_tile_pixmap);
752 /* Set the icon border */
753 XSetWindowBorderWidth(dpy, icon->icon_win, 0);
755 /* Put the dock application in the icon */
756 XReparentWindow(dpy, icon->icon_win, icon->core->window,
757 (wPreferences.icon_size - w) / 2,
758 (wPreferences.icon_size - h) / 2);
760 /* Show it and save */
761 XMapWindow(dpy, icon->icon_win);
762 XAddToSaveSet(dpy, icon->icon_win);
764 /* Needed to move the icon clicking on the application part */
765 if ((XGetWindowAttributes(dpy, icon->icon_win, &attr)) &&
766 (attr.all_event_masks & ButtonPressMask))
767 wHackedGrabButton(Button1, MOD_MASK, icon->core->window, True,
768 ButtonPressMask, GrabModeSync, GrabModeAsync,
769 None, wPreferences.cursor[WCUR_ARROW]);
772 /* Get the RImage from the XWindow wm_hints */
773 RImage *get_rimage_icon_from_wm_hints(WIcon *icon)
775 RImage *image = NULL;
776 unsigned int w, h, d;
777 WWindow *wwin;
779 if ((!icon) || (!icon->owner))
780 return NULL;
782 wwin = icon->owner;
784 if (!getSize(wwin->wm_hints->icon_pixmap, &w, &h, &d)) {
785 icon->owner->wm_hints->flags &= ~IconPixmapHint;
786 return NULL;
789 image = get_wwindow_image_from_wmhints(wwin, icon);
790 if (!image)
791 return NULL;
793 /* Resize the icon to the wPreferences.icon_size size */
794 image = wIconValidateIconSize(image, wPreferences.icon_size);
796 return image;
799 /* This function updates in the screen the icon title */
800 static void update_icon_title(WIcon *icon)
802 WScreen *scr = icon->core->screen_ptr;
803 int x, l, w;
804 char *tmp;
806 /* draw the icon title */
807 if (icon->show_title && icon->icon_name != NULL) {
808 tmp = ShrinkString(scr->icon_title_font, icon->icon_name, wPreferences.icon_size - 4);
809 w = WMWidthOfString(scr->icon_title_font, tmp, l = strlen(tmp));
811 if (w > icon->core->width - 4)
812 x = (icon->core->width - 4) - w;
813 else
814 x = (icon->core->width - w) / 2;
816 WMDrawString(scr->wmscreen, icon->core->window, scr->icon_title_color,
817 scr->icon_title_font, x, 1, tmp, l);
818 wfree(tmp);
823 void wIconPaint(WIcon *icon)
825 if (!icon || !icon->core || !icon->core->screen_ptr)
826 return;
828 WScreen *scr = icon->core->screen_ptr;
830 XClearWindow(dpy, icon->core->window);
832 update_icon_title(icon);
834 if (icon->selected)
835 XDrawRectangle(dpy, icon->core->window, scr->icon_select_gc, 0, 0,
836 icon->core->width - 1, icon->core->height - 1);
839 /******************************************************************/
841 static void miniwindowExpose(WObjDescriptor *desc, XEvent *event)
843 /* Parameter not used, but tell the compiler that it is ok */
844 (void) event;
846 wIconPaint(desc->parent);
849 static void miniwindowDblClick(WObjDescriptor *desc, XEvent *event)
851 WIcon *icon = desc->parent;
853 /* Parameter not used, but tell the compiler that it is ok */
854 (void) event;
856 assert(icon->owner != NULL);
858 wDeiconifyWindow(icon->owner);
861 static void miniwindowMouseDown(WObjDescriptor *desc, XEvent *event)
863 WIcon *icon = desc->parent;
864 WWindow *wwin = icon->owner;
865 XEvent ev;
866 int x = wwin->icon_x, y = wwin->icon_y;
867 int dx = event->xbutton.x, dy = event->xbutton.y;
868 int grabbed = 0;
869 int clickButton = event->xbutton.button;
870 Bool hasMoved = False;
872 if (WCHECK_STATE(WSTATE_MODAL))
873 return;
875 if (IsDoubleClick(icon->core->screen_ptr, event)) {
876 miniwindowDblClick(desc, event);
877 return;
880 if (event->xbutton.button == Button1) {
881 if (event->xbutton.state & MOD_MASK)
882 wLowerFrame(icon->core);
883 else
884 wRaiseFrame(icon->core);
885 if (event->xbutton.state & ShiftMask) {
886 wIconSelect(icon);
887 wSelectWindow(icon->owner, !wwin->flags.selected);
889 } else if (event->xbutton.button == Button3) {
890 WObjDescriptor *desc;
892 OpenMiniwindowMenu(wwin, event->xbutton.x_root, event->xbutton.y_root);
894 /* allow drag select of menu */
895 desc = &wwin->screen_ptr->window_menu->menu->descriptor;
896 event->xbutton.send_event = True;
897 (*desc->handle_mousedown) (desc, event);
899 return;
902 if (XGrabPointer(dpy, icon->core->window, False, ButtonMotionMask
903 | ButtonReleaseMask | ButtonPressMask, GrabModeAsync,
904 GrabModeAsync, None, None, CurrentTime) != GrabSuccess) {
906 while (1) {
907 WMMaskEvent(dpy, PointerMotionMask | ButtonReleaseMask | ButtonPressMask
908 | ButtonMotionMask | ExposureMask, &ev);
909 switch (ev.type) {
910 case Expose:
911 WMHandleEvent(&ev);
912 break;
914 case MotionNotify:
915 hasMoved = True;
916 if (!grabbed) {
917 if (abs(dx - ev.xmotion.x) >= MOVE_THRESHOLD
918 || abs(dy - ev.xmotion.y) >= MOVE_THRESHOLD) {
919 XChangeActivePointerGrab(dpy, ButtonMotionMask
920 | ButtonReleaseMask | ButtonPressMask,
921 wPreferences.cursor[WCUR_MOVE], CurrentTime);
922 grabbed = 1;
923 } else {
924 break;
927 x = ev.xmotion.x_root - dx;
928 y = ev.xmotion.y_root - dy;
929 XMoveWindow(dpy, icon->core->window, x, y);
930 break;
932 case ButtonPress:
933 break;
935 case ButtonRelease:
936 if (ev.xbutton.button != clickButton)
937 break;
939 if (wwin->icon_x != x || wwin->icon_y != y)
940 wwin->flags.icon_moved = 1;
942 XMoveWindow(dpy, icon->core->window, x, y);
944 wwin->icon_x = x;
945 wwin->icon_y = y;
946 XUngrabPointer(dpy, CurrentTime);
948 if (wPreferences.auto_arrange_icons)
949 wArrangeIcons(wwin->screen_ptr, True);
950 if (wPreferences.single_click && !hasMoved)
951 miniwindowDblClick(desc, event);
952 return;
958 void set_icon_image_from_database(WIcon *icon, const char *wm_instance, const char *wm_class, const char *command)
960 char *file = NULL;
962 file = get_icon_filename(wm_instance, wm_class, command, False);
963 if (file) {
964 icon->file = wstrdup(file);
965 icon->file_image = get_rimage_from_file(icon->core->screen_ptr, icon->file, wPreferences.icon_size);
966 wfree(file);