1 /* icon.c - window icon and dock and appicon parent
3 * WindowMaker window manager
5 * Copyright (c) 1997, 1998 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,
26 #include <X11/Xutil.h>
33 #include "WindowMaker.h"
41 #include "application.h"
45 /**** Global variables ****/
46 extern WPreferences wPreferences
;
48 #define MOD_MASK wPreferences.modifier_mask
50 extern Cursor wCursor
[WCUR_LAST
];
53 static void miniwindowExpose(WObjDescriptor
*desc
, XEvent
*event
);
54 static void miniwindowMouseDown(WObjDescriptor
*desc
, XEvent
*event
);
55 static void miniwindowDblClick(WObjDescriptor
*desc
, XEvent
*event
);
59 getSize(Drawable d
, unsigned int *w
, unsigned int *h
, unsigned int *dep
)
65 XGetGeometry(dpy
, d
, &rjunk
, &xjunk
, &yjunk
, w
, h
, &bjunk
, dep
);
70 wIconCreate(WWindow
*wwin
)
72 WScreen
*scr
=wwin
->screen_ptr
;
75 unsigned long vmask
= 0;
76 XSetWindowAttributes attribs
;
78 icon
= wmalloc(sizeof(WIcon
));
79 memset(icon
, 0, sizeof(WIcon
));
80 icon
->core
= wCoreCreateTopLevel(scr
, wwin
->icon_x
, wwin
->icon_y
,
81 wPreferences
.icon_size
,
82 wPreferences
.icon_size
, 0);
84 if (wPreferences
.use_saveunders
) {
86 attribs
.save_under
= True
;
88 /* a white border for selecting it */
89 vmask
|= CWBorderPixel
;
90 attribs
.border_pixel
= scr
->white_pixel
;
92 XChangeWindowAttributes(dpy
, icon
->core
->window
, vmask
, &attribs
);
95 /* will be overriden if this is an application icon */
96 icon
->core
->descriptor
.handle_mousedown
= miniwindowMouseDown
;
97 icon
->core
->descriptor
.handle_expose
= miniwindowExpose
;
98 icon
->core
->descriptor
.parent_type
= WCLASS_MINIWINDOW
;
99 icon
->core
->descriptor
.parent
= icon
;
101 icon
->core
->stacking
= wmalloc(sizeof(WStacking
));
102 icon
->core
->stacking
->above
= NULL
;
103 icon
->core
->stacking
->under
= NULL
;
104 icon
->core
->stacking
->window_level
= WMNormalWindowLevel
;
105 icon
->core
->stacking
->child_of
= NULL
;
108 if (wwin
->wm_hints
&& (wwin
->wm_hints
->flags
& IconWindowHint
)) {
109 if (wwin
->client_win
== wwin
->main_window
) {
111 /* do not let miniwindow steal app-icon's icon window */
112 wapp
= wApplicationOf(wwin
->client_win
);
113 if (!wapp
|| wapp
->app_icon
==NULL
)
114 icon
->icon_win
= wwin
->wm_hints
->icon_window
;
116 icon
->icon_win
= wwin
->wm_hints
->icon_window
;
119 #ifdef NO_MINIWINDOW_TITLES
120 icon
->show_title
= 0;
122 icon
->show_title
= 1;
124 icon
->image
= wDefaultGetImage(scr
, wwin
->wm_instance
, wwin
->wm_class
);
126 file
= wDefaultGetIconFile(scr
, wwin
->wm_instance
, wwin
->wm_class
,
129 icon
->file
= wstrdup(file
);
132 wGetIconName(dpy
, wwin
->client_win
, &icon
->icon_name
);
134 icon
->tile_type
= TILE_NORMAL
;
145 wIconCreateWithIconFile(WScreen
*scr
, char *iconfile
, int tile
)
148 unsigned long vmask
= 0;
149 XSetWindowAttributes attribs
;
151 icon
= wmalloc(sizeof(WIcon
));
152 memset(icon
, 0, sizeof(WIcon
));
153 icon
->core
= wCoreCreateTopLevel(scr
, 0, 0, wPreferences
.icon_size
,
154 wPreferences
.icon_size
, 0);
155 if (wPreferences
.use_saveunders
) {
157 attribs
.save_under
= True
;
159 /* a white border for selecting it */
160 vmask
|= CWBorderPixel
;
161 attribs
.border_pixel
= scr
->white_pixel
;
163 XChangeWindowAttributes(dpy
, icon
->core
->window
, vmask
, &attribs
);
165 /* will be overriden if this is a application icon */
166 icon
->core
->descriptor
.handle_mousedown
= miniwindowMouseDown
;
167 icon
->core
->descriptor
.handle_expose
= miniwindowExpose
;
168 icon
->core
->descriptor
.parent_type
= WCLASS_MINIWINDOW
;
169 icon
->core
->descriptor
.parent
= icon
;
171 icon
->core
->stacking
= wmalloc(sizeof(WStacking
));
172 icon
->core
->stacking
->above
= NULL
;
173 icon
->core
->stacking
->under
= NULL
;
174 icon
->core
->stacking
->window_level
= WMNormalWindowLevel
;
175 icon
->core
->stacking
->child_of
= NULL
;
178 icon
->image
= RLoadImage(scr
->rcontext
, iconfile
, 0);
180 wwarning(_("error loading image file \"%s\""), iconfile
, RErrorString
);
183 icon
->image
= wIconValidateIconSize(scr
, icon
->image
);
185 icon
->file
= wstrdup(iconfile
);
188 icon
->tile_type
= tile
;
198 wIconDestroy(WIcon
*icon
)
200 WCoreWindow
*core
= icon
->core
;
201 WScreen
*scr
= core
->screen_ptr
;
204 WMDeleteTimerHandler(icon
->handlerID
);
206 if (icon
->icon_win
) {
210 x
= icon
->owner
->icon_x
;
211 y
= icon
->owner
->icon_y
;
213 XUnmapWindow(dpy
, icon
->icon_win
);
214 XReparentWindow(dpy
, icon
->icon_win
, scr
->root_win
, x
, y
);
217 XFree(icon
->icon_name
);
220 XFreePixmap(dpy
, icon
->pixmap
);
225 if (icon
->image
!=NULL
)
226 RDestroyImage(icon
->image
);
228 wCoreDestroy(icon
->core
);
235 drawIconTitle(WScreen
*scr
, Pixmap pixmap
, int height
)
237 XFillRectangle(dpy
, pixmap
, scr
->icon_title_texture
->normal_gc
,
238 0, 0, wPreferences
.icon_size
, height
+1);
239 XDrawLine(dpy
, pixmap
, scr
->icon_title_texture
->light_gc
, 0, 0,
240 wPreferences
.icon_size
, 0);
241 XDrawLine(dpy
, pixmap
, scr
->icon_title_texture
->light_gc
, 0, 0,
243 XDrawLine(dpy
, pixmap
, scr
->icon_title_texture
->dim_gc
,
244 wPreferences
.icon_size
-1, 0, wPreferences
.icon_size
-1, height
+1);
249 makeIcon(WScreen
*scr
, RImage
*icon
, int titled
, int shadowed
, int tileType
)
255 int theight
= scr
->icon_title_font
->height
;
257 if (tileType
== TILE_NORMAL
)
258 tile
= RCloneImage(scr
->icon_tile
);
260 assert(scr
->clip_tile
);
261 tile
= RCloneImage(scr
->clip_tile
);
264 w
= (icon
->width
> wPreferences
.icon_size
)
265 ? wPreferences
.icon_size
: icon
->width
;
266 x
= (wPreferences
.icon_size
- w
) / 2;
267 sx
= (icon
->width
- w
)/2;
270 h
= (icon
->height
> wPreferences
.icon_size
)
271 ? wPreferences
.icon_size
: icon
->height
;
272 y
= (wPreferences
.icon_size
- h
) / 2;
273 sy
= (icon
->height
- h
)/2;
275 h
= (icon
->height
+theight
> wPreferences
.icon_size
276 ? wPreferences
.icon_size
-theight
: icon
->height
);
277 y
= theight
+((int)wPreferences
.icon_size
-theight
-h
)/2;
278 sy
= (icon
->height
- h
)/2;
280 RCombineArea(tile
, icon
, sx
, sy
, w
, h
, x
, y
);
286 color
.red
= scr
->icon_back_texture
->light
.red
>> 8;
287 color
.green
= scr
->icon_back_texture
->light
.green
>> 8;
288 color
.blue
= scr
->icon_back_texture
->light
.blue
>> 8;
289 color
.alpha
= 150; /* about 60% */
290 RClearImage(tile
, &color
);
293 if (!RConvertImage(scr
->rcontext
, tile
, &pixmap
)) {
294 wwarning(_("error rendering image:%s"), RErrorString
);
299 drawIconTitle(scr
, pixmap
, theight
);
306 wIconChangeTitle(WIcon
*icon
, char *new_title
)
310 changed
= (new_title
==NULL
&& icon
->icon_name
!=NULL
)
311 || (new_title
!=NULL
&& icon
->icon_name
==NULL
);
313 if (icon
->icon_name
!=NULL
)
314 XFree(icon
->icon_name
);
316 icon
->icon_name
= new_title
;
319 icon
->force_paint
= 1;
325 wIconChangeImage(WIcon
*icon
, RImage
*new_image
)
327 assert(icon
!= NULL
);
330 RDestroyImage(icon
->image
);
332 icon
->image
= wIconValidateIconSize(icon
->core
->screen_ptr
, new_image
);
339 wIconValidateIconSize(WScreen
*scr
, RImage
*icon
)
347 if (icon
->width
> wPreferences
.icon_size
348 || icon
->height
> wPreferences
.icon_size
) {
349 if (icon
->width
> icon
->height
) {
350 w
= wPreferences
.icon_size
- 4;
351 h
= w
*icon
->height
/icon
->width
;
353 h
= wPreferences
.icon_size
- 4;
354 w
= h
*icon
->width
/icon
->height
;
356 tmp
= RScaleImage(icon
, w
, h
);
366 wIconChangeImageFile(WIcon
*icon
, char *file
)
368 WScreen
*scr
= icon
->core
->screen_ptr
;
374 wIconChangeImage(icon
, NULL
);
378 path
= FindImage(wPreferences
.icon_path
, file
);
380 if (path
&& (image
= RLoadImage(scr
->rcontext
, path
, 0))) {
381 wIconChangeImage(icon
, image
);
394 wIconChangeIconWindow(WIcon *icon, Window new_window);
398 cycleColor(void *data
)
400 WIcon
*icon
= (WIcon
*)data
;
401 WScreen
*scr
= icon
->core
->screen_ptr
;
403 icon
->step
= !icon
->step
;
405 XSetForeground(dpy
, scr
->icon_select_gc
, scr
->white_pixel
);
406 XSetBackground(dpy
, scr
->icon_select_gc
, scr
->black_pixel
);
408 XSetForeground(dpy
, scr
->icon_select_gc
, scr
->black_pixel
);
409 XSetBackground(dpy
, scr
->icon_select_gc
, scr
->white_pixel
);
411 XDrawRectangle(dpy
, icon
->core
->window
, scr
->icon_select_gc
, 0, 0,
412 icon
->core
->width
-1, icon
->core
->height
-1);
413 icon
->handlerID
= WMAddTimerHandler(COLOR_CYCLE_DELAY
, cycleColor
, icon
);
418 wIconSetHighlited(WIcon
*icon
, Bool flag
)
420 if (icon
->highlighted
== flag
) {
424 icon
->highlighted
= flag
;
431 wIconSelect(WIcon
*icon
)
433 WScreen
*scr
= icon
->core
->screen_ptr
;
434 icon
->selected
= !icon
->selected
;
436 if (icon
->selected
) {
438 if (!wPreferences
.dont_blink
)
439 icon
->handlerID
= WMAddTimerHandler(10, cycleColor
, icon
);
441 XDrawRectangle(dpy
, icon
->core
->window
, scr
->icon_select_gc
, 0, 0,
442 icon
->core
->width
-1, icon
->core
->height
-1);
444 if (icon
->handlerID
) {
445 WMDeleteTimerHandler(icon
->handlerID
);
446 icon
->handlerID
= NULL
;
448 XClearArea(dpy
, icon
->core
->window
, 0, 0, icon
->core
->width
,
449 icon
->core
->height
, True
);
455 wIconUpdate(WIcon
*icon
)
457 WScreen
*scr
= icon
->core
->screen_ptr
;
458 int title_height
= scr
->icon_title_font
->height
;
459 WWindow
*wwin
= icon
->owner
;
461 assert(scr
->icon_tile
!=NULL
);
463 if (icon
->pixmap
!=None
)
464 XFreePixmap(dpy
, icon
->pixmap
);
468 if (wwin
&& wwin
->window_flags
.always_user_icon
)
471 /* use client specified icon window */
472 if (icon
->icon_win
!=None
) {
473 XWindowAttributes attr
;
475 int width
, height
, depth
;
479 getSize(icon
->icon_win
, &width
, &height
, &depth
);
481 if (width
> wPreferences
.icon_size
) {
483 width
= wPreferences
.icon_size
;
485 if (height
> wPreferences
.icon_size
) {
487 height
= wPreferences
.icon_size
;
490 && (height
+title_height
< wPreferences
.icon_size
)) {
491 pixmap
= XCreatePixmap(dpy
, scr
->w_win
, wPreferences
.icon_size
,
492 wPreferences
.icon_size
, scr
->w_depth
);
493 XSetClipMask(dpy
, scr
->copy_gc
, None
);
494 XCopyArea(dpy
, scr
->icon_tile_pixmap
, pixmap
, scr
->copy_gc
, 0, 0,
495 wPreferences
.icon_size
, wPreferences
.icon_size
, 0, 0);
496 drawIconTitle(scr
, pixmap
, title_height
);
497 theight
= title_height
;
501 XSetWindowBackgroundPixmap(dpy
, icon
->core
->window
,
502 scr
->icon_tile_pixmap
);
505 XSetWindowBorderWidth(dpy
, icon
->icon_win
, 0);
506 XReparentWindow(dpy
, icon
->icon_win
, icon
->core
->window
,
507 (wPreferences
.icon_size
-width
)/2,
508 theight
+(wPreferences
.icon_size
-height
-theight
)/2);
510 XResizeWindow(dpy
, icon
->icon_win
, width
, height
);
512 XMapWindow(dpy
, icon
->icon_win
);
514 XAddToSaveSet(dpy
, icon
->icon_win
);
516 icon
->pixmap
= pixmap
;
518 if (XGetWindowAttributes(dpy
, icon
->icon_win
, &attr
)) {
519 if (attr
.all_event_masks
& ButtonPressMask
) {
520 XGrabButton(dpy
, Button1
, MOD_MASK
, icon
->core
->window
, True
,
521 ButtonPressMask
, GrabModeSync
, GrabModeAsync
, None
,
522 wCursor
[WCUR_ARROW
]);
523 XGrabButton(dpy
, Button1
, MOD_MASK
|LockMask
, icon
->core
->window
,
524 True
, ButtonPressMask
, GrabModeSync
, GrabModeAsync
,
525 None
, wCursor
[WCUR_ARROW
]);
527 wHackedGrabButton(Button1
, MOD_MASK
, icon
->core
->window
, True
,
528 ButtonPressMask
, GrabModeSync
, GrabModeAsync
,
529 None
, wCursor
[WCUR_ARROW
]);
533 } else if (wwin
&& wwin
->wm_hints
534 && (wwin
->wm_hints
->flags
& IconPixmapHint
)) {
542 if (!XGetGeometry(dpy
, wwin
->wm_hints
->icon_pixmap
, &jw
,
543 &ji
, &ji
, &w
, &h
, &ju
, &d
)) {
544 icon
->owner
->wm_hints
->flags
&= ~IconPixmapHint
;
548 pixmap
= XCreatePixmap(dpy
, icon
->core
->window
, wPreferences
.icon_size
,
549 wPreferences
.icon_size
, scr
->w_depth
);
550 XSetClipMask(dpy
, scr
->copy_gc
, None
);
551 XCopyArea(dpy
, scr
->icon_tile_pixmap
, pixmap
, scr
->copy_gc
, 0, 0,
552 wPreferences
.icon_size
, wPreferences
.icon_size
, 0, 0);
554 if (w
> wPreferences
.icon_size
)
555 w
= wPreferences
.icon_size
;
556 x
= (wPreferences
.icon_size
-w
)/2;
558 if (icon
->show_title
&& (title_height
< wPreferences
.icon_size
)) {
559 drawIconTitle(scr
, pixmap
, title_height
);
562 if (h
> wPreferences
.icon_size
- title_height
- 2) {
563 h
= wPreferences
.icon_size
- title_height
- 2;
564 y
= title_height
+ 1;
566 y
= (wPreferences
.icon_size
-h
-title_height
)/2+title_height
+ 1;
570 if (w
> wPreferences
.icon_size
)
571 w
= wPreferences
.icon_size
;
572 y
= (wPreferences
.icon_size
-h
)/2;
575 if (wwin
->wm_hints
->flags
& IconMaskHint
)
576 XSetClipMask(dpy
, scr
->copy_gc
, wwin
->wm_hints
->icon_mask
);
578 XSetClipOrigin(dpy
, scr
->copy_gc
, x
, y
);
580 if (d
!= scr
->w_depth
) {
581 XSetForeground(dpy
, scr
->copy_gc
, scr
->black_pixel
);
582 XSetBackground(dpy
, scr
->copy_gc
, scr
->white_pixel
);
583 XCopyPlane(dpy
, wwin
->wm_hints
->icon_pixmap
, pixmap
, scr
->copy_gc
,
584 0, 0, w
, h
, x
, y
, 1);
586 XCopyArea(dpy
, wwin
->wm_hints
->icon_pixmap
, pixmap
, scr
->copy_gc
,
590 XSetClipOrigin(dpy
, scr
->copy_gc
, 0, 0);
592 icon
->pixmap
= pixmap
;
597 icon
->pixmap
= makeIcon(scr
, icon
->image
, icon
->show_title
,
598 icon
->shadowed
, icon
->tile_type
);
600 /* make default icons */
602 if (!scr
->def_icon_pixmap
) {
603 RImage
*image
= NULL
;
607 file
= wDefaultGetIconFile(scr
, NULL
, NULL
, False
);
609 path
= FindImage(wPreferences
.icon_path
, file
);
611 wwarning(_("could not find default icon \"%s\""),file
);
615 image
= RLoadImage(scr
->rcontext
, path
, 0);
617 wwarning(_("could not load default icon \"%s\":%s"),
624 image
= wIconValidateIconSize(scr
, image
);
625 scr
->def_icon_pixmap
= makeIcon(scr
, image
, False
, False
,
627 scr
->def_ticon_pixmap
= makeIcon(scr
, image
, True
, False
,
630 RDestroyImage(image
);
633 if (icon
->show_title
) {
634 XSetWindowBackgroundPixmap(dpy
, icon
->core
->window
,
635 scr
->def_ticon_pixmap
);
637 XSetWindowBackgroundPixmap(dpy
, icon
->core
->window
,
638 scr
->def_icon_pixmap
);
643 if (icon
->pixmap
!= None
) {
644 XSetWindowBackgroundPixmap(dpy
, icon
->core
->window
, icon
->pixmap
);
646 XClearWindow(dpy
, icon
->core
->window
);
654 wIconPaint(WIcon
*icon
)
656 WScreen
*scr
=icon
->core
->screen_ptr
;
657 GC gc
= scr
->icon_title_gc
;
661 if (icon
->force_paint
) {
662 icon
->force_paint
= 0;
667 XClearWindow(dpy
, icon
->core
->window
);
669 /* draw the icon title */
670 if (icon
->show_title
&& icon
->icon_name
!=NULL
) {
674 tmp
= ShrinkString(scr
->icon_title_font
, icon
->icon_name
,
675 wPreferences
.icon_size
-4);
676 w
= wTextWidth(scr
->icon_title_font
->font
, tmp
, l
=strlen(tmp
));
678 if (w
> icon
->core
->width
- 4)
679 x
= (icon
->core
->width
- 4) - w
;
681 x
= (icon
->core
->width
- w
)/2;
683 wDrawString(icon
->core
->window
, scr
->icon_title_font
, gc
,
684 x
, 1 + scr
->icon_title_font
->y
, tmp
, l
);
689 XDrawRectangle(dpy
, icon
->core
->window
, scr
->icon_select_gc
, 0, 0,
690 icon
->core
->width
-1, icon
->core
->height
-1);
694 /******************************************************************/
697 miniwindowExpose(WObjDescriptor
*desc
, XEvent
*event
)
699 wIconPaint(desc
->parent
);
704 miniwindowDblClick(WObjDescriptor
*desc
, XEvent
*event
)
706 WIcon
*icon
= desc
->parent
;
708 assert(icon
->owner
!=NULL
);
710 wDeiconifyWindow(icon
->owner
);
715 miniwindowMouseDown(WObjDescriptor
*desc
, XEvent
*event
)
717 WIcon
*icon
= desc
->parent
;
718 WWindow
*wwin
= icon
->owner
;
720 int x
=wwin
->icon_x
, y
=wwin
->icon_y
;
721 int dx
=event
->xbutton
.x
, dy
=event
->xbutton
.y
;
723 int clickButton
=event
->xbutton
.button
;
726 if (IsDoubleClick(icon
->core
->screen_ptr
, event
)) {
727 miniwindowDblClick(desc
, event
);
732 puts("Moving miniwindow");
734 if (event
->xbutton
.button
==Button1
) {
735 if (event
->xbutton
.state
& MOD_MASK
)
736 wLowerFrame(icon
->core
);
738 wRaiseFrame(icon
->core
);
739 if (event
->xbutton
.state
& ShiftMask
) {
741 wSelectWindow(icon
->owner
);
745 if (XGrabPointer(dpy
, icon
->core
->window
, False
, ButtonMotionMask
746 |ButtonReleaseMask
|ButtonPressMask
, GrabModeAsync
,
747 GrabModeAsync
, None
, None
, CurrentTime
) !=GrabSuccess
) {
749 wwarning("pointer grab failed for icon move");
753 WMMaskEvent(dpy
, PointerMotionMask
|ButtonReleaseMask
|ButtonPressMask
754 |ButtonMotionMask
|ExposureMask
, &ev
);
762 if (abs(dx
-ev
.xmotion
.x
)>=MOVE_THRESHOLD
763 || abs(dy
-ev
.xmotion
.y
)>=MOVE_THRESHOLD
) {
764 XChangeActivePointerGrab(dpy
, ButtonMotionMask
765 |ButtonReleaseMask
|ButtonPressMask
,
766 wCursor
[WCUR_MOVE
], CurrentTime
);
772 x
= ev
.xmotion
.x_root
- dx
;
773 y
= ev
.xmotion
.y_root
- dy
;
774 XMoveWindow(dpy
, icon
->core
->window
, x
, y
);
781 if (ev
.xbutton
.button
!= clickButton
)
784 if (wwin
->icon_x
!=x
|| wwin
->icon_y
!=y
)
785 wwin
->flags
.icon_moved
= 1;
787 XMoveWindow(dpy
, icon
->core
->window
, x
, y
);
792 puts("End miniwindow move");
794 XUngrabPointer(dpy
, CurrentTime
);