- Fixed crashing bug in menu.c
[wmaker-crm.git] / src / icon.c
blobfc5333cc038efae1c6c108ce6602979aa987f9d0
1 /* icon.c - window icon and dock and appicon parent
2 *
3 * Window Maker window manager
4 *
5 * Copyright (c) 1997-2003 Alfredo K. Kojima
6 *
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 <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"
47 /**** Global variables ****/
48 extern WPreferences wPreferences;
50 #define MOD_MASK wPreferences.modifier_mask
52 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);
60 /****** Notification Observers ******/
62 static void
63 appearanceObserver(void *self, WMNotification *notif)
65 WIcon *icon = (WIcon*)self;
66 int flags = (int)WMGetNotificationClientData(notif);
68 if (flags & WTextureSettings) {
69 icon->force_paint = 1;
71 if (flags & WFontSettings) {
72 icon->force_paint = 1;
75 if (flags & WColorSettings) {
79 wIconPaint(icon);
81 /* so that the appicon expose handlers will paint the appicon specific
82 * stuff */
83 XClearArea(dpy, icon->core->window, 0, 0, icon->core->width,
84 icon->core->height, True);
88 static void
89 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 /************************************/
103 INLINE static void
104 getSize(Drawable d, unsigned int *w, unsigned int *h, unsigned int *dep)
106 Window rjunk;
107 int xjunk, yjunk;
108 unsigned int bjunk;
110 XGetGeometry(dpy, d, &rjunk, &xjunk, &yjunk, w, h, &bjunk, dep);
114 WIcon*
115 wIconCreate(WWindow *wwin)
117 WScreen *scr=wwin->screen_ptr;
118 WIcon *icon;
119 char *file;
120 unsigned long vmask = 0;
121 XSetWindowAttributes attribs;
123 icon = wmalloc(sizeof(WIcon));
124 memset(icon, 0, sizeof(WIcon));
125 icon->core = wCoreCreateTopLevel(scr, wwin->icon_x, wwin->icon_y,
126 wPreferences.icon_size,
127 wPreferences.icon_size, 0);
129 if (wPreferences.use_saveunders) {
130 vmask |= CWSaveUnder;
131 attribs.save_under = True;
133 /* a white border for selecting it */
134 vmask |= CWBorderPixel;
135 attribs.border_pixel = scr->white_pixel;
137 XChangeWindowAttributes(dpy, icon->core->window, vmask, &attribs);
140 /* will be overriden if this is an application icon */
141 icon->core->descriptor.handle_mousedown = miniwindowMouseDown;
142 icon->core->descriptor.handle_expose = miniwindowExpose;
143 icon->core->descriptor.parent_type = WCLASS_MINIWINDOW;
144 icon->core->descriptor.parent = icon;
146 icon->core->stacking = wmalloc(sizeof(WStacking));
147 icon->core->stacking->above = NULL;
148 icon->core->stacking->under = NULL;
149 icon->core->stacking->window_level = NORMAL_ICON_LEVEL;
150 icon->core->stacking->child_of = NULL;
152 icon->owner = wwin;
153 if (wwin->wm_hints && (wwin->wm_hints->flags & IconWindowHint)) {
154 if (wwin->client_win == wwin->main_window) {
155 WApplication *wapp;
156 /* do not let miniwindow steal app-icon's icon window */
157 wapp = wApplicationOf(wwin->client_win);
158 if (!wapp || wapp->app_icon==NULL)
159 icon->icon_win = wwin->wm_hints->icon_window;
160 } else {
161 icon->icon_win = wwin->wm_hints->icon_window;
164 #ifdef NO_MINIWINDOW_TITLES
165 icon->show_title = 0;
166 #else
167 icon->show_title = 1;
168 #endif
169 icon->image = wDefaultGetImage(scr, wwin->wm_instance, wwin->wm_class);
171 file = wDefaultGetIconFile(scr, wwin->wm_instance, wwin->wm_class,
172 False);
173 if (file) {
174 icon->file = wstrdup(file);
177 wGetIconName(dpy, wwin->client_win, &icon->icon_name);
179 icon->tile_type = TILE_NORMAL;
181 wIconUpdate(icon);
183 XFlush(dpy);
185 WMAddNotificationObserver(appearanceObserver, icon,
186 WNIconAppearanceSettingsChanged, icon);
187 WMAddNotificationObserver(tileObserver, icon,
188 WNIconTileSettingsChanged, icon);
189 return icon;
193 WIcon*
194 wIconCreateWithIconFile(WScreen *scr, char *iconfile, int tile)
196 WIcon *icon;
197 unsigned long vmask = 0;
198 XSetWindowAttributes attribs;
200 icon = wmalloc(sizeof(WIcon));
201 memset(icon, 0, sizeof(WIcon));
202 icon->core = wCoreCreateTopLevel(scr, 0, 0, wPreferences.icon_size,
203 wPreferences.icon_size, 0);
204 if (wPreferences.use_saveunders) {
205 vmask = CWSaveUnder;
206 attribs.save_under = True;
208 /* a white border for selecting it */
209 vmask |= CWBorderPixel;
210 attribs.border_pixel = scr->white_pixel;
212 XChangeWindowAttributes(dpy, icon->core->window, vmask, &attribs);
214 /* will be overriden if this is a application icon */
215 icon->core->descriptor.handle_mousedown = miniwindowMouseDown;
216 icon->core->descriptor.handle_expose = miniwindowExpose;
217 icon->core->descriptor.parent_type = WCLASS_MINIWINDOW;
218 icon->core->descriptor.parent = icon;
220 icon->core->stacking = wmalloc(sizeof(WStacking));
221 icon->core->stacking->above = NULL;
222 icon->core->stacking->under = NULL;
223 icon->core->stacking->window_level = NORMAL_ICON_LEVEL;
224 icon->core->stacking->child_of = NULL;
226 if (iconfile) {
227 icon->image = RLoadImage(scr->rcontext, iconfile, 0);
228 if (!icon->image) {
229 wwarning(_("error loading image file \"%s\""), iconfile, RMessageForError(RErrorCode));
232 icon->image = wIconValidateIconSize(scr, icon->image);
234 icon->file = wstrdup(iconfile);
237 icon->tile_type = tile;
239 wIconUpdate(icon);
241 WMAddNotificationObserver(appearanceObserver, icon,
242 WNIconAppearanceSettingsChanged, icon);
243 WMAddNotificationObserver(tileObserver, icon,
244 WNIconTileSettingsChanged, icon);
246 return icon;
251 void
252 wIconDestroy(WIcon *icon)
254 WCoreWindow *core = icon->core;
255 WScreen *scr = core->screen_ptr;
257 WMRemoveNotificationObserver(icon);
259 if (icon->handlerID)
260 WMDeleteTimerHandler(icon->handlerID);
262 if (icon->icon_win) {
263 int x=0, y=0;
265 if (icon->owner) {
266 x = icon->owner->icon_x;
267 y = icon->owner->icon_y;
269 XUnmapWindow(dpy, icon->icon_win);
270 XReparentWindow(dpy, icon->icon_win, scr->root_win, x, y);
272 if (icon->icon_name)
273 XFree(icon->icon_name);
275 if (icon->pixmap)
276 XFreePixmap(dpy, icon->pixmap);
278 if (icon->file)
279 wfree(icon->file);
281 if (icon->image!=NULL)
282 RReleaseImage(icon->image);
284 wCoreDestroy(icon->core);
285 wfree(icon);
290 static void
291 drawIconTitle(WScreen *scr, Pixmap pixmap, int height)
293 XFillRectangle(dpy, pixmap, scr->icon_title_texture->normal_gc,
294 0, 0, wPreferences.icon_size, height+1);
295 XDrawLine(dpy, pixmap, scr->icon_title_texture->light_gc, 0, 0,
296 wPreferences.icon_size, 0);
297 XDrawLine(dpy, pixmap, scr->icon_title_texture->light_gc, 0, 0,
298 0, height+1);
299 XDrawLine(dpy, pixmap, scr->icon_title_texture->dim_gc,
300 wPreferences.icon_size-1, 0, wPreferences.icon_size-1, height+1);
304 static Pixmap
305 makeIcon(WScreen *scr, RImage *icon, int titled, int shadowed, int tileType)
307 RImage *tile;
308 Pixmap pixmap;
309 int x, y, sx, sy;
310 unsigned w, h;
311 int theight = WMFontHeight(scr->icon_title_font);
313 if (tileType == TILE_NORMAL)
314 tile = RCloneImage(scr->icon_tile);
315 else {
316 assert(scr->clip_tile);
317 tile = RCloneImage(scr->clip_tile);
319 if (icon) {
320 w = (icon->width > wPreferences.icon_size)
321 ? wPreferences.icon_size : icon->width;
322 x = (wPreferences.icon_size - w) / 2;
323 sx = (icon->width - w)/2;
325 if (!titled) {
326 h = (icon->height > wPreferences.icon_size)
327 ? wPreferences.icon_size : icon->height;
328 y = (wPreferences.icon_size - h) / 2;
329 sy = (icon->height - h)/2;
330 } else {
331 h = (icon->height+theight > wPreferences.icon_size
332 ? wPreferences.icon_size-theight : icon->height);
333 y = theight+((int)wPreferences.icon_size-theight-h)/2;
334 sy = (icon->height - h)/2;
336 RCombineArea(tile, icon, sx, sy, w, h, x, y);
339 if (shadowed) {
340 RColor color;
342 color.red = scr->icon_back_texture->light.red >> 8;
343 color.green = scr->icon_back_texture->light.green >> 8;
344 color.blue = scr->icon_back_texture->light.blue >> 8;
345 color.alpha = 150; /* about 60% */
346 RClearImage(tile, &color);
349 if (!RConvertImage(scr->rcontext, tile, &pixmap)) {
350 wwarning(_("error rendering image:%s"), RMessageForError(RErrorCode));
352 RReleaseImage(tile);
354 if (titled)
355 drawIconTitle(scr, pixmap, theight);
357 return pixmap;
361 void
362 wIconChangeTitle(WIcon *icon, char *new_title)
364 int changed;
366 changed = (new_title==NULL && icon->icon_name!=NULL)
367 || (new_title!=NULL && icon->icon_name==NULL);
369 if (icon->icon_name!=NULL)
370 XFree(icon->icon_name);
372 icon->icon_name = new_title;
374 if (changed)
375 icon->force_paint = 1;
376 wIconPaint(icon);
380 void
381 wIconChangeImage(WIcon *icon, RImage *new_image)
383 assert(icon != NULL);
385 if (icon->image)
386 RReleaseImage(icon->image);
388 icon->image = new_image;
390 wIconUpdate(icon);
394 RImage*
395 wIconValidateIconSize(WScreen *scr, RImage *icon)
397 RImage *tmp;
398 int w, h;
400 if (!icon)
401 return NULL;
402 #ifndef DONT_SCALE_ICONS
403 if (wPreferences.icon_size != 64) {
404 w = wPreferences.icon_size * icon->width / 64;
405 h = wPreferences.icon_size * icon->height / 64;
407 tmp = RScaleImage(icon, w, h);
408 RReleaseImage(icon);
409 icon = tmp;
411 #endif
412 #if 0
413 if (icon->width > wPreferences.icon_size
414 || icon->height > wPreferences.icon_size) {
415 if (icon->width > icon->height) {
416 w = wPreferences.icon_size - 4;
417 h = w*icon->height/icon->width;
418 } else {
419 h = wPreferences.icon_size - 4;
420 w = h*icon->width/icon->height;
422 tmp = RScaleImage(icon, w, h);
423 RReleaseImage(icon);
424 icon = tmp;
426 #endif
428 return icon;
432 Bool
433 wIconChangeImageFile(WIcon *icon, char *file)
435 WScreen *scr = icon->core->screen_ptr;
436 RImage *image;
437 char *path;
438 int error = 0;
440 if (!file) {
441 wIconChangeImage(icon, NULL);
442 return True;
445 path = FindImage(wPreferences.icon_path, file);
447 if (path && (image = RLoadImage(scr->rcontext, path, 0))) {
448 image = wIconValidateIconSize(icon->core->screen_ptr, image);
450 wIconChangeImage(icon, image);
451 } else {
452 error = 1;
455 if (path)
456 wfree(path);
458 return !error;
463 static char*
464 getnameforicon(WWindow *wwin)
466 char *prefix, *suffix;
467 char *path;
468 int len;
470 if (wwin->wm_class && wwin->wm_instance) {
471 int len = strlen(wwin->wm_class)+strlen(wwin->wm_instance)+2;
472 suffix = wmalloc(len);
473 snprintf(suffix, len, "%s.%s", wwin->wm_instance, wwin->wm_class);
474 } else if (wwin->wm_class) {
475 int len = strlen(wwin->wm_class)+1;
476 suffix = wmalloc(len);
477 snprintf(suffix, len, "%s", wwin->wm_class);
478 } else if (wwin->wm_instance) {
479 int len = strlen(wwin->wm_instance)+1;
480 suffix = wmalloc(len);
481 snprintf(suffix, len, "%s", wwin->wm_instance);
482 } else {
483 return NULL;
486 prefix = wusergnusteppath();
487 len = strlen(prefix)+64+strlen(suffix);
488 path = wmalloc(len+1);
489 snprintf(path, len, "%s/.AppInfo", prefix);
491 if (access(path, F_OK)!=0) {
492 if (mkdir(path, S_IRUSR|S_IWUSR|S_IXUSR)) {
493 wsyserror(_("could not create directory %s"), path);
494 wfree(path);
495 wfree(suffix);
496 return NULL;
499 strcat(path, "/WindowMaker");
500 if (access(path, F_OK)!=0) {
501 if (mkdir(path, S_IRUSR|S_IWUSR|S_IXUSR)!=0) {
502 wsyserror(_("could not create directory %s"), path);
503 wfree(path);
504 wfree(suffix);
505 return NULL;
509 strcat(path, "/");
510 strcat(path, suffix);
511 strcat(path, ".xpm");
512 wfree(suffix);
514 return path;
519 * wIconStore--
520 * Stores the client supplied icon at ~/GNUstep/.AppInfo/WindowMaker
521 * and returns the path for that icon. Returns NULL if there is no
522 * client supplied icon or on failure.
524 * Side effects:
525 * New directories might be created.
527 char*
528 wIconStore(WIcon *icon)
530 char *path;
531 RImage *image;
532 WWindow *wwin = icon->owner;
534 if (!wwin || !wwin->wm_hints || !(wwin->wm_hints->flags & IconPixmapHint)
535 || wwin->wm_hints->icon_pixmap == None)
536 return NULL;
538 path = getnameforicon(wwin);
539 if (!path)
540 return NULL;
542 image = RCreateImageFromDrawable(icon->core->screen_ptr->rcontext,
543 wwin->wm_hints->icon_pixmap,
544 (wwin->wm_hints->flags & IconMaskHint)
545 ? wwin->wm_hints->icon_mask : None);
546 if (!image) {
547 wfree(path);
548 return NULL;
551 if (!RSaveImage(image, path, "XPM")) {
552 wfree(path);
553 path = NULL;
555 RReleaseImage(image);
557 return path;
562 void
563 wIconChangeIconWindow(WIcon *icon, Window new_window);
566 static void
567 cycleColor(void *data)
569 WIcon *icon = (WIcon*)data;
570 WScreen *scr = icon->core->screen_ptr;
571 XGCValues gcv;
573 icon->step--;
574 gcv.dash_offset = icon->step;
575 XChangeGC(dpy, scr->icon_select_gc, GCDashOffset, &gcv);
577 XDrawRectangle(dpy, icon->core->window, scr->icon_select_gc, 0, 0,
578 icon->core->width-1, icon->core->height-1);
579 icon->handlerID = WMAddTimerHandler(COLOR_CYCLE_DELAY, cycleColor, icon);
583 void
584 wIconSetHighlited(WIcon *icon, Bool flag)
586 if (icon->highlighted == flag) {
587 return;
590 icon->highlighted = flag;
591 wIconPaint(icon);
596 void
597 wIconSelect(WIcon *icon)
599 WScreen *scr = icon->core->screen_ptr;
600 icon->selected = !icon->selected;
602 if (icon->selected) {
603 icon->step = 0;
604 if (!wPreferences.dont_blink)
605 icon->handlerID = WMAddTimerHandler(10, cycleColor, icon);
606 else
607 XDrawRectangle(dpy, icon->core->window, scr->icon_select_gc, 0, 0,
608 icon->core->width-1, icon->core->height-1);
609 } else {
610 if (icon->handlerID) {
611 WMDeleteTimerHandler(icon->handlerID);
612 icon->handlerID = NULL;
614 XClearArea(dpy, icon->core->window, 0, 0, icon->core->width,
615 icon->core->height, True);
620 void
621 wIconUpdate(WIcon *icon)
623 WScreen *scr = icon->core->screen_ptr;
624 int title_height = WMFontHeight(scr->icon_title_font);
625 WWindow *wwin = icon->owner;
627 assert(scr->icon_tile!=NULL);
629 if (icon->pixmap!=None)
630 XFreePixmap(dpy, icon->pixmap);
631 icon->pixmap = None;
634 if (wwin && WFLAGP(wwin, always_user_icon))
635 goto user_icon;
637 /* use client specified icon window */
638 if (icon->icon_win!=None) {
639 XWindowAttributes attr;
640 int resize=0;
641 int width, height, depth;
642 int theight;
643 Pixmap pixmap;
645 getSize(icon->icon_win, &width, &height, &depth);
647 if (width > wPreferences.icon_size) {
648 resize = 1;
649 width = wPreferences.icon_size;
651 if (height > wPreferences.icon_size) {
652 resize = 1;
653 height = wPreferences.icon_size;
655 if (icon->show_title
656 && (height+title_height < wPreferences.icon_size)) {
657 pixmap = XCreatePixmap(dpy, scr->w_win, wPreferences.icon_size,
658 wPreferences.icon_size, scr->w_depth);
659 XSetClipMask(dpy, scr->copy_gc, None);
660 XCopyArea(dpy, scr->icon_tile_pixmap, pixmap, scr->copy_gc, 0, 0,
661 wPreferences.icon_size, wPreferences.icon_size, 0, 0);
662 drawIconTitle(scr, pixmap, title_height);
663 theight = title_height;
664 } else {
665 pixmap = None;
666 theight = 0;
667 XSetWindowBackgroundPixmap(dpy, icon->core->window,
668 scr->icon_tile_pixmap);
671 XSetWindowBorderWidth(dpy, icon->icon_win, 0);
672 XReparentWindow(dpy, icon->icon_win, icon->core->window,
673 (wPreferences.icon_size-width)/2,
674 theight+(wPreferences.icon_size-height-theight)/2);
675 if (resize)
676 XResizeWindow(dpy, icon->icon_win, width, height);
678 XMapWindow(dpy, icon->icon_win);
680 XAddToSaveSet(dpy, icon->icon_win);
682 icon->pixmap = pixmap;
684 if (XGetWindowAttributes(dpy, icon->icon_win, &attr)) {
685 if (attr.all_event_masks & ButtonPressMask) {
686 wHackedGrabButton(Button1, MOD_MASK, icon->core->window, True,
687 ButtonPressMask, GrabModeSync, GrabModeAsync,
688 None, wCursor[WCUR_ARROW]);
691 } else if (wwin && wwin->wm_hints
692 && (wwin->wm_hints->flags & IconPixmapHint)) {
693 int x, y;
694 unsigned int w, h;
695 Window jw;
696 int ji, dotitle;
697 unsigned int ju, d;
698 Pixmap pixmap;
700 if (!XGetGeometry(dpy, wwin->wm_hints->icon_pixmap, &jw,
701 &ji, &ji, &w, &h, &ju, &d)) {
702 icon->owner->wm_hints->flags &= ~IconPixmapHint;
703 goto user_icon;
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);
718 dotitle = 1;
720 if (h > wPreferences.icon_size - title_height - 2) {
721 h = wPreferences.icon_size - title_height - 2;
722 y = title_height + 1;
723 } else {
724 y = (wPreferences.icon_size-h-title_height)/2+title_height + 1;
726 } else {
727 dotitle = 0;
728 if (w > wPreferences.icon_size)
729 w = wPreferences.icon_size;
730 y = (wPreferences.icon_size-h)/2;
733 if (wwin->wm_hints->flags & IconMaskHint)
734 XSetClipMask(dpy, scr->copy_gc, wwin->wm_hints->icon_mask);
736 XSetClipOrigin(dpy, scr->copy_gc, x, y);
738 if (d != scr->w_depth) {
739 XSetForeground(dpy, scr->copy_gc, scr->black_pixel);
740 XSetBackground(dpy, scr->copy_gc, scr->white_pixel);
741 XCopyPlane(dpy, wwin->wm_hints->icon_pixmap, pixmap, scr->copy_gc,
742 0, 0, w, h, x, y, 1);
743 } else {
744 XCopyArea(dpy, wwin->wm_hints->icon_pixmap, pixmap, scr->copy_gc,
745 0, 0, w, h, x, y);
748 XSetClipOrigin(dpy, scr->copy_gc, 0, 0);
750 icon->pixmap = pixmap;
751 } else {
752 user_icon:
754 if (icon->image) {
755 icon->pixmap = makeIcon(scr, icon->image, icon->show_title,
756 icon->shadowed, icon->tile_type);
757 } else {
758 /* make default icons */
760 if (!scr->def_icon_pixmap) {
761 RImage *image = NULL;
762 char *path;
763 char *file;
765 file = wDefaultGetIconFile(scr, NULL, NULL, False);
766 if (file) {
767 path = FindImage(wPreferences.icon_path, file);
768 if (!path) {
769 wwarning(_("could not find default icon \"%s\""),file);
770 goto make_icons;
773 image = RLoadImage(scr->rcontext, path, 0);
774 if (!image) {
775 wwarning(_("could not load default icon \"%s\":%s"),
776 file, RMessageForError(RErrorCode));
778 wfree(path);
780 make_icons:
782 image = wIconValidateIconSize(scr, image);
783 scr->def_icon_pixmap = makeIcon(scr, image, False, False,
784 icon->tile_type);
785 scr->def_ticon_pixmap = makeIcon(scr, image, True, False,
786 icon->tile_type);
787 if (image)
788 RReleaseImage(image);
791 if (icon->show_title) {
792 XSetWindowBackgroundPixmap(dpy, icon->core->window,
793 scr->def_ticon_pixmap);
794 } else {
795 XSetWindowBackgroundPixmap(dpy, icon->core->window,
796 scr->def_icon_pixmap);
798 icon->pixmap = None;
801 if (icon->pixmap != None) {
802 XSetWindowBackgroundPixmap(dpy, icon->core->window, icon->pixmap);
804 XClearWindow(dpy, icon->core->window);
806 wIconPaint(icon);
811 void
812 wIconPaint(WIcon *icon)
814 WScreen *scr=icon->core->screen_ptr;
815 int x;
816 char *tmp;
818 if (icon->force_paint) {
819 icon->force_paint = 0;
820 wIconUpdate(icon);
821 return;
824 XClearWindow(dpy, icon->core->window);
826 /* draw the icon title */
827 if (icon->show_title && icon->icon_name!=NULL) {
828 int l;
829 int w;
831 tmp = ShrinkString(scr->icon_title_font, icon->icon_name,
832 wPreferences.icon_size-4);
833 w = WMWidthOfString(scr->icon_title_font, tmp, l=strlen(tmp));
835 if (w > icon->core->width - 4)
836 x = (icon->core->width - 4) - w;
837 else
838 x = (icon->core->width - w)/2;
840 WMDrawString(scr->wmscreen, icon->core->window, scr->icon_title_color,
841 scr->icon_title_font, x, 1, tmp, l);
842 wfree(tmp);
845 if (icon->selected)
846 XDrawRectangle(dpy, icon->core->window, scr->icon_select_gc, 0, 0,
847 icon->core->width-1, icon->core->height-1);
851 /******************************************************************/
853 static void
854 miniwindowExpose(WObjDescriptor *desc, XEvent *event)
856 wIconPaint(desc->parent);
860 static void
861 miniwindowDblClick(WObjDescriptor *desc, XEvent *event)
863 WIcon *icon = desc->parent;
865 assert(icon->owner!=NULL);
867 wDeiconifyWindow(icon->owner);
871 static void
872 miniwindowMouseDown(WObjDescriptor *desc, XEvent *event)
874 WIcon *icon = desc->parent;
875 WWindow *wwin = icon->owner;
876 XEvent ev;
877 int x=wwin->icon_x, y=wwin->icon_y;
878 int dx=event->xbutton.x, dy=event->xbutton.y;
879 int grabbed=0;
880 int clickButton=event->xbutton.button;
882 if (WCHECK_STATE(WSTATE_MODAL))
883 return;
885 if (IsDoubleClick(icon->core->screen_ptr, event)) {
886 miniwindowDblClick(desc, event);
887 return;
890 #ifdef DEBUG
891 puts("Moving miniwindow");
892 #endif
893 if (event->xbutton.button == Button1) {
894 if (event->xbutton.state & MOD_MASK)
895 wLowerFrame(icon->core);
896 else
897 wRaiseFrame(icon->core);
898 if (event->xbutton.state & ShiftMask) {
899 wIconSelect(icon);
900 wSelectWindow(icon->owner, !wwin->flags.selected);
902 } else if (event->xbutton.button == Button3) {
903 WObjDescriptor *desc;
905 OpenMiniwindowMenu(wwin, event->xbutton.x_root,
906 event->xbutton.y_root);
908 /* allow drag select of menu */
909 desc = &wwin->screen_ptr->window_menu->menu->descriptor;
910 event->xbutton.send_event = True;
911 (*desc->handle_mousedown)(desc, event);
913 return;
916 if (XGrabPointer(dpy, icon->core->window, False, ButtonMotionMask
917 |ButtonReleaseMask|ButtonPressMask, GrabModeAsync,
918 GrabModeAsync, None, None, CurrentTime) !=GrabSuccess) {
919 #ifdef DEBUG0
920 wwarning("pointer grab failed for icon move");
921 #endif
923 while(1) {
924 WMMaskEvent(dpy, PointerMotionMask|ButtonReleaseMask|ButtonPressMask
925 |ButtonMotionMask|ExposureMask, &ev);
926 switch (ev.type) {
927 case Expose:
928 WMHandleEvent(&ev);
929 break;
931 case MotionNotify:
932 if (!grabbed) {
933 if (abs(dx-ev.xmotion.x)>=MOVE_THRESHOLD
934 || abs(dy-ev.xmotion.y)>=MOVE_THRESHOLD) {
935 XChangeActivePointerGrab(dpy, ButtonMotionMask
936 |ButtonReleaseMask|ButtonPressMask,
937 wCursor[WCUR_MOVE], CurrentTime);
938 grabbed=1;
939 } else {
940 break;
943 x = ev.xmotion.x_root - dx;
944 y = ev.xmotion.y_root - dy;
945 XMoveWindow(dpy, icon->core->window, x, y);
946 break;
948 case ButtonPress:
949 break;
951 case ButtonRelease:
952 if (ev.xbutton.button != clickButton)
953 break;
955 if (wwin->icon_x!=x || wwin->icon_y!=y)
956 wwin->flags.icon_moved = 1;
958 XMoveWindow(dpy, icon->core->window, x, y);
960 wwin->icon_x = x;
961 wwin->icon_y = y;
962 #ifdef DEBUG
963 puts("End miniwindow move");
964 #endif
965 XUngrabPointer(dpy, CurrentTime);
967 if (wPreferences.auto_arrange_icons)
968 wArrangeIcons(wwin->screen_ptr, True);
969 return;