2 * Window Maker window manager
4 * Copyright (c) 1998-2003 Alfredo K. Kojima
5 * Copyright (c) 2014 Window Maker Team
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.
28 #include <X11/Xutil.h>
30 #include <X11/extensions/shape.h>
38 #include "WindowMaker.h"
44 #include "workspace.h"
49 typedef struct _WBalloon
{
75 #define TLEFT (TOP|LEFT)
76 #define TRIGHT (TOP|RIGHT)
77 #define BLEFT (BOTTOM|LEFT)
78 #define BRIGHT (BOTTOM|RIGHT)
80 static int countLines(const char *text
)
86 if (*p
== '\n' && p
[1] != 0)
93 static int getMaxStringWidth(WMFont
*font
, const char *text
)
102 wt
= WMWidthOfString(font
, pb
, pos
);
113 wt
= WMWidthOfString(font
, pb
, pos
);
120 static void drawMultiLineString(WMScreen
*scr
, Pixmap pixmap
, WMColor
*color
,
121 WMFont
*font
, int x
, int y
, const char *text
, int len
)
123 const char *p
= text
;
126 int height
= WMFontHeight(font
);
128 while (*p
&& p
- text
< len
) {
130 WMDrawString(scr
, pixmap
, color
, font
, x
, y
+ l
* height
, pb
, pos
);
140 WMDrawString(scr
, pixmap
, color
, font
, x
, y
+ l
* height
, pb
, pos
);
143 #ifdef SHAPED_BALLOON
147 static void drawBalloon(WScreen
*scr
, Pixmap bitmap
, Pixmap pix
, int x
, int y
, int w
, int h
, int side
)
149 GC bgc
= scr
->balloon
->monoGC
;
150 GC gc
= scr
->draw_gc
;
151 int rad
= h
* 3 / 10;
152 XPoint pt
[3], ipt
[3];
156 XSetForeground(dpy
, bgc
, 1);
158 XFillArc(dpy
, bitmap
, bgc
, x
, y
, rad
, rad
, 90 * 64, 90 * 64);
159 XFillArc(dpy
, bitmap
, bgc
, x
, y
+ h
- 1 - rad
, rad
, rad
, 180 * 64, 90 * 64);
161 XFillArc(dpy
, bitmap
, bgc
, x
+ w
- 1 - rad
, y
, rad
, rad
, 0 * 64, 90 * 64);
162 XFillArc(dpy
, bitmap
, bgc
, x
+ w
- 1 - rad
, y
+ h
- 1 - rad
, rad
, rad
, 270 * 64, 90 * 64);
164 XFillRectangle(dpy
, bitmap
, bgc
, x
, y
+ rad
/ 2, w
, h
- rad
);
165 XFillRectangle(dpy
, bitmap
, bgc
, x
+ rad
/ 2, y
, w
- rad
, h
);
168 XSetForeground(dpy
, gc
, scr
->white_pixel
);
170 XFillArc(dpy
, pix
, gc
, x
+ 1, y
+ 1, rad
, rad
, 90 * 64, 90 * 64);
171 XFillArc(dpy
, pix
, gc
, x
+ 1, y
+ h
- 2 - rad
, rad
, rad
, 180 * 64, 90 * 64);
173 XFillArc(dpy
, pix
, gc
, x
+ w
- 2 - rad
, y
+ 1, rad
, rad
, 0 * 64, 90 * 64);
174 XFillArc(dpy
, pix
, gc
, x
+ w
- 2 - rad
, y
+ h
- 2 - rad
, rad
, rad
, 270 * 64, 90 * 64);
176 XFillRectangle(dpy
, pix
, gc
, x
+ 1, y
+ 1 + rad
/ 2, w
- 2, h
- 2 - rad
);
177 XFillRectangle(dpy
, pix
, gc
, x
+ 1 + rad
/ 2, y
+ 1, w
- 2 - rad
, h
- 2);
181 pt
[1].y
= y
+ h
- 1 + SPACE
;
183 ipt
[0].y
= pt
[0].y
- 1;
184 ipt
[1].y
= pt
[1].y
- 1;
185 ipt
[2].y
= pt
[2].y
- 1;
190 ipt
[0].y
= pt
[0].y
+ 1;
191 ipt
[1].y
= pt
[1].y
+ 1;
192 ipt
[2].y
= pt
[2].y
+ 1;
195 /*w1 = WMAX(h, 24); */
199 pt
[0].x
= x
+ w
- w1
+ 2 * w1
/ 16;
200 pt
[1].x
= x
+ w
- w1
+ 11 * w1
/ 16;
201 pt
[2].x
= x
+ w
- w1
+ 7 * w1
/ 16;
202 ipt
[0].x
= x
+ 1 + w
- w1
+ 2 * (w1
- 1) / 16;
203 ipt
[1].x
= x
+ 1 + w
- w1
+ 11 * (w1
- 1) / 16;
204 ipt
[2].x
= x
+ 1 + w
- w1
+ 7 * (w1
- 1) / 16;
205 /*ipt[0].x = pt[0].x+1;
207 ipt[2].x = pt[2].x; */
209 pt
[0].x
= x
+ w1
- 2 * w1
/ 16;
210 pt
[1].x
= x
+ w1
- 11 * w1
/ 16;
211 pt
[2].x
= x
+ w1
- 7 * w1
/ 16;
212 ipt
[0].x
= x
- 1 + w1
- 2 * (w1
- 1) / 16;
213 ipt
[1].x
= x
- 1 + w1
- 11 * (w1
- 1) / 16;
214 ipt
[2].x
= x
- 1 + w1
- 7 * (w1
- 1) / 16;
215 /*ipt[0].x = pt[0].x-1;
217 ipt[2].x = pt[2].x; */
220 XFillPolygon(dpy
, bitmap
, bgc
, pt
, 3, Convex
, CoordModeOrigin
);
221 XFillPolygon(dpy
, pix
, gc
, ipt
, 3, Convex
, CoordModeOrigin
);
224 XSetForeground(dpy
, gc
, scr
->black_pixel
);
226 XDrawLines(dpy
, pix
, gc
, pt
, 3, CoordModeOrigin
);
234 XDrawLines(dpy
, pix
, gc
, pt
, 3, CoordModeOrigin
);
237 static Pixmap
makePixmap(WScreen
*scr
, int width
, int height
, int side
, Pixmap
*mask
)
239 WBalloon
*bal
= scr
->balloon
;
244 bitmap
= XCreatePixmap(dpy
, scr
->root_win
, width
+ SPACE
, height
+ SPACE
, 1);
247 bal
->monoGC
= XCreateGC(dpy
, bitmap
, 0, NULL
);
249 XSetForeground(dpy
, bal
->monoGC
, 0);
250 XFillRectangle(dpy
, bitmap
, bal
->monoGC
, 0, 0, width
+ SPACE
, height
+ SPACE
);
252 pixmap
= XCreatePixmap(dpy
, scr
->root_win
, width
+ SPACE
, height
+ SPACE
, scr
->w_depth
);
253 XSetForeground(dpy
, scr
->draw_gc
, scr
->black_pixel
);
254 XFillRectangle(dpy
, pixmap
, scr
->draw_gc
, 0, 0, width
+ SPACE
, height
+ SPACE
);
262 drawBalloon(scr
, bitmap
, pixmap
, x
, y
, width
, height
, side
);
269 static void showText(WScreen
*scr
, int x
, int y
, int h
, int w
, const char *text
)
275 WMFont
*font
= scr
->info_text_font
;
280 if (scr
->balloon
->contents
)
281 XFreePixmap(dpy
, scr
->balloon
->contents
);
283 width
= getMaxStringWidth(font
, text
) + 16;
284 height
= countLines(text
) * WMFontHeight(font
) + 4;
291 if (x
+ width
> scr
->scr_width
) {
293 bx
= x
- width
+ w
/ 2;
300 if (bx
+ width
> scr
->scr_width
)
301 bx
= scr
->scr_width
- width
;
303 if (y
- (height
+ SPACE
) < 0) {
309 by
= y
- (height
+ SPACE
);
312 pixmap
= makePixmap(scr
, width
, height
, side
, &mask
);
314 drawMultiLineString(scr
->wmscreen
, pixmap
, scr
->black
, font
, 8, ty
+ 2, text
, strlen(text
));
316 XSetWindowBackgroundPixmap(dpy
, scr
->balloon
->window
, pixmap
);
317 scr
->balloon
->contents
= pixmap
;
319 XResizeWindow(dpy
, scr
->balloon
->window
, width
, height
+ SPACE
);
320 XShapeCombineMask(dpy
, scr
->balloon
->window
, ShapeBounding
, 0, 0, mask
, ShapeSet
);
321 XFreePixmap(dpy
, mask
);
322 XMoveWindow(dpy
, scr
->balloon
->window
, bx
, by
);
323 XMapRaised(dpy
, scr
->balloon
->window
);
325 scr
->balloon
->mapped
= 1;
327 #else /* !SHAPED_BALLOON */
329 static void showText(WScreen
*scr
, int x
, int y
, int h
, int w
, const char *text
)
334 WMFont
*font
= scr
->info_text_font
;
336 if (scr
->balloon
->contents
)
337 XFreePixmap(dpy
, scr
->balloon
->contents
);
339 width
= getMaxStringWidth(font
, text
) + 8;
340 /*width = WMWidthOfString(font, text, strlen(text))+8; */
341 height
= countLines(text
) * WMFontHeight(font
) + 4;
345 else if (x
+ width
> scr
->scr_width
- 1)
346 x
= scr
->scr_width
- width
;
348 if (y
- height
- 2 < 0) {
356 if (scr
->window_title_texture
[0])
357 XSetForeground(dpy
, scr
->draw_gc
, scr
->window_title_texture
[0]->any
.color
.pixel
);
359 XSetForeground(dpy
, scr
->draw_gc
, scr
->light_pixel
);
361 pixmap
= XCreatePixmap(dpy
, scr
->root_win
, width
, height
, scr
->w_depth
);
362 XFillRectangle(dpy
, pixmap
, scr
->draw_gc
, 0, 0, width
, height
);
364 drawMultiLineString(scr
->wmscreen
, pixmap
, scr
->window_title_color
[0], font
, 4, 2, text
, strlen(text
));
366 XResizeWindow(dpy
, scr
->balloon
->window
, width
, height
);
367 XMoveWindow(dpy
, scr
->balloon
->window
, x
, y
);
369 XSetWindowBackgroundPixmap(dpy
, scr
->balloon
->window
, pixmap
);
370 XClearWindow(dpy
, scr
->balloon
->window
);
371 XMapRaised(dpy
, scr
->balloon
->window
);
373 scr
->balloon
->contents
= pixmap
;
375 scr
->balloon
->mapped
= 1;
377 #endif /* !SHAPED_BALLOON */
379 static void showApercu(WScreen
*scr
, int x
, int y
, int height
, int width
, char *title
, Pixmap apercu
)
382 WMFont
*font
= scr
->info_text_font
;
384 char *shortenTitle
= title
;
386 if (scr
->balloon
->contents
)
387 XFreePixmap(dpy
, scr
->balloon
->contents
);
389 if (wPreferences
.miniwin_title_balloon
) {
390 shortenTitle
= ShrinkString(font
, title
, width
- APERCU_BORDER
);
391 titleHeight
= countLines(shortenTitle
) * WMFontHeight(font
) + 4;
392 height
+= titleHeight
;
397 else if (x
+ width
> scr
->scr_width
- 1)
398 x
= scr
->scr_width
- width
- APERCU_BORDER
;
400 if (y
- height
- 2 < 0) {
401 y
+= wPreferences
.icon_size
;
408 if (scr
->window_title_texture
[0])
409 XSetForeground(dpy
, scr
->draw_gc
, scr
->window_title_texture
[0]->any
.color
.pixel
);
411 XSetForeground(dpy
, scr
->draw_gc
, scr
->light_pixel
);
413 pixmap
= XCreatePixmap(dpy
, scr
->root_win
, width
, height
, scr
->w_depth
);
414 XFillRectangle(dpy
, pixmap
, scr
->draw_gc
, 0, 0, width
, height
);
416 if (shortenTitle
&& wPreferences
.miniwin_title_balloon
) {
417 drawMultiLineString(scr
->wmscreen
, pixmap
, scr
->window_title_color
[0], font
,
418 APERCU_BORDER
, APERCU_BORDER
, shortenTitle
, strlen(shortenTitle
));
422 XCopyArea(dpy
, apercu
, pixmap
, scr
->draw_gc
,
423 0, 0, (wPreferences
.icon_size
- 1 - APERCU_BORDER
) * wPreferences
.apercu_size
,
424 (wPreferences
.icon_size
- 1 - APERCU_BORDER
) * wPreferences
.apercu_size
,
425 APERCU_BORDER
, APERCU_BORDER
+ titleHeight
);
427 #ifdef SHAPED_BALLOON
428 XShapeCombineMask(dpy
, scr
->balloon
->window
, ShapeBounding
, 0, 0, None
, ShapeSet
);
430 XResizeWindow(dpy
, scr
->balloon
->window
, width
, height
);
431 XMoveWindow(dpy
, scr
->balloon
->window
, x
, y
);
433 XSetWindowBackgroundPixmap(dpy
, scr
->balloon
->window
, pixmap
);
435 XClearWindow(dpy
, scr
->balloon
->window
);
436 XMapRaised(dpy
, scr
->balloon
->window
);
439 scr
->balloon
->contents
= pixmap
;
441 scr
->balloon
->mapped
= 1;
444 static void showBalloon(WScreen
*scr
)
450 scr
->balloon
->timer
= NULL
;
451 scr
->balloon
->ignoreTimer
= 1;
453 if (!XGetGeometry(dpy
, scr
->balloon
->objectWindow
, &foow
, &x
, &y
, &w
, &foo
, &foo
, &foo
)) {
454 scr
->balloon
->prevType
= 0;
458 if (wPreferences
.miniwin_apercu_balloon
&& scr
->balloon
->apercu
!= None
)
459 /* used to display either the apercu alone or the apercu and the title */
460 showApercu(scr
, x
, y
, (wPreferences
.icon_size
- 1) * wPreferences
.apercu_size
,
461 (wPreferences
.icon_size
- 1) * wPreferences
.apercu_size
,
462 scr
->balloon
->text
, scr
->balloon
->apercu
);
464 showText(scr
, x
, y
, scr
->balloon
->h
, w
, scr
->balloon
->text
);
467 static void frameBalloon(WObjDescriptor
*object
)
469 WFrameWindow
*fwin
= (WFrameWindow
*) object
->parent
;
470 WScreen
*scr
= fwin
->core
->screen_ptr
;
472 if (fwin
->titlebar
!= object
->self
|| !fwin
->flags
.is_client_window_frame
) {
476 if (fwin
->title
&& fwin
->flags
.incomplete_title
) {
477 scr
->balloon
->h
= (fwin
->titlebar
? fwin
->titlebar
->height
: 0);
478 scr
->balloon
->text
= wstrdup(fwin
->title
);
479 scr
->balloon
->objectWindow
= fwin
->core
->window
;
480 scr
->balloon
->timer
= WMAddTimerHandler(BALLOON_DELAY
, (WMCallback
*) showBalloon
, scr
);
484 static void miniwindowBalloon(WObjDescriptor
*object
)
486 WIcon
*icon
= (WIcon
*) object
->parent
;
487 WScreen
*scr
= icon
->core
->screen_ptr
;
489 if (!icon
->icon_name
) {
493 scr
->balloon
->h
= icon
->core
->height
;
494 scr
->balloon
->text
= wstrdup(icon
->icon_name
);
495 scr
->balloon
->apercu
= icon
->apercu
;
496 scr
->balloon
->objectWindow
= icon
->core
->window
;
498 if ((scr
->balloon
->prevType
== object
->parent_type
|| scr
->balloon
->prevType
== WCLASS_APPICON
)
499 && scr
->balloon
->ignoreTimer
) {
500 XUnmapWindow(dpy
, scr
->balloon
->window
);
503 scr
->balloon
->timer
= WMAddTimerHandler(BALLOON_DELAY
, (WMCallback
*) showBalloon
, scr
);
507 static void appiconBalloon(WObjDescriptor
*object
)
509 WAppIcon
*aicon
= (WAppIcon
*) object
->parent
;
510 WScreen
*scr
= aicon
->icon
->core
->screen_ptr
;
513 /* Show balloon if it is the Clip and the workspace name is > 5 chars */
514 if (object
->parent
== w_global
.clip
.icon
) {
515 if (strlen(w_global
.workspace
.array
[w_global
.workspace
.current
]->name
) > 5) {
516 scr
->balloon
->text
= wstrdup(w_global
.workspace
.array
[w_global
.workspace
.current
]->name
);
521 } else if (aicon
->command
&& aicon
->wm_class
) {
524 unsigned int app_win_cnt
= 0;
526 if (object
->parent_type
== WCLASS_DOCK_ICON
) {
527 if (aicon
->main_window
) {
528 app
= wApplicationOf(aicon
->main_window
);
529 if (app
&& app
->main_window_desc
&& app
->main_window_desc
->fake_group
)
530 app_win_cnt
= app
->main_window_desc
->fake_group
->retainCount
- 1;
534 /* Check to see if it is a GNUstep app */
535 if (strcmp(aicon
->wm_class
, "GNUstep") == 0)
536 len
= strlen(aicon
->command
) + strlen(aicon
->wm_instance
) + 8;
538 len
= strlen(aicon
->command
) + strlen(aicon
->wm_class
) + 8;
541 len
+= 1 + snprintf(NULL
, 0, "%u", app_win_cnt
);
544 /* Check to see if it is a GNUstep App */
545 if (strcmp(aicon
->wm_class
, "GNUstep") == 0)
547 snprintf(tmp
, len
, "%u %s\n(%s)", app_win_cnt
, aicon
->wm_instance
, aicon
->command
);
549 snprintf(tmp
, len
, "%s\n(%s)", aicon
->wm_instance
, aicon
->command
);
552 snprintf(tmp
, len
, "%u %s\n(%s)", app_win_cnt
, aicon
->wm_class
, aicon
->command
);
554 snprintf(tmp
, len
, "%s\n(%s)", aicon
->wm_class
, aicon
->command
);
555 scr
->balloon
->text
= tmp
;
556 } else if (aicon
->command
) {
557 scr
->balloon
->text
= wstrdup(aicon
->command
);
558 } else if (aicon
->wm_class
) {
559 /* Check to see if it is a GNUstep App */
560 if (strcmp(aicon
->wm_class
, "GNUstep") == 0)
561 scr
->balloon
->text
= wstrdup(aicon
->wm_instance
);
563 scr
->balloon
->text
= wstrdup(aicon
->wm_class
);
568 scr
->balloon
->h
= aicon
->icon
->core
->height
- 2;
570 scr
->balloon
->objectWindow
= aicon
->icon
->core
->window
;
571 if ((scr
->balloon
->prevType
== object
->parent_type
|| scr
->balloon
->prevType
== WCLASS_MINIWINDOW
)
572 && scr
->balloon
->ignoreTimer
) {
573 XUnmapWindow(dpy
, scr
->balloon
->window
);
576 scr
->balloon
->timer
= WMAddTimerHandler(BALLOON_DELAY
, (WMCallback
*) showBalloon
, scr
);
580 void wBalloonInitialize(WScreen
*scr
)
583 XSetWindowAttributes attribs
;
586 bal
= wmalloc(sizeof(WBalloon
));
590 vmask
= CWSaveUnder
| CWOverrideRedirect
| CWColormap
| CWBackPixel
| CWBorderPixel
;
591 attribs
.save_under
= True
;
592 attribs
.override_redirect
= True
;
593 attribs
.colormap
= scr
->w_colormap
;
594 attribs
.background_pixel
= scr
->icon_back_texture
->normal
.pixel
;
595 attribs
.border_pixel
= 0; /* do not care */
597 bal
->window
= XCreateWindow(dpy
, scr
->root_win
, 1, 1, 10, 10, 1,
598 scr
->w_depth
, CopyFromParent
, scr
->w_visual
, vmask
, &attribs
);
600 /* select EnterNotify to so that the balloon will be unmapped
601 * when the pointer is moved over it */
602 XSelectInput(dpy
, bal
->window
, EnterWindowMask
);
606 void wBalloonEnteredObject(WScreen
*scr
, WObjDescriptor
*object
)
608 WBalloon
*balloon
= scr
->balloon
;
610 if (balloon
->timer
) {
611 WMDeleteTimerHandler(balloon
->timer
);
612 balloon
->timer
= NULL
;
613 balloon
->ignoreTimer
= 0;
616 if (scr
->balloon
->text
)
617 wfree(scr
->balloon
->text
);
618 scr
->balloon
->text
= NULL
;
620 scr
->balloon
->apercu
= None
;
624 balloon
->ignoreTimer
= 0;
628 switch (object
->parent_type
) {
630 if (wPreferences
.window_balloon
)
631 frameBalloon(object
);
633 case WCLASS_DOCK_ICON
:
634 if (wPreferences
.appicon_balloon
)
635 appiconBalloon(object
);
637 case WCLASS_MINIWINDOW
:
638 if (wPreferences
.miniwin_title_balloon
|| wPreferences
.miniwin_apercu_balloon
)
639 miniwindowBalloon(object
);
642 if (wPreferences
.appicon_balloon
)
643 appiconBalloon(object
);
649 scr
->balloon
->prevType
= object
->parent_type
;
652 void wBalloonHide(WScreen
*scr
)
655 if (scr
->balloon
->mapped
) {
656 XUnmapWindow(dpy
, scr
->balloon
->window
);
657 scr
->balloon
->mapped
= 0;
658 } else if (scr
->balloon
->timer
) {
659 WMDeleteTimerHandler(scr
->balloon
->timer
);
660 scr
->balloon
->timer
= NULL
;
662 scr
->balloon
->prevType
= 0;