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