wmaker: add miniwindow apercu
[wmaker-crm.git] / src / balloon.c
blob1fae947d8416d2017f4ee79ec167b2bc708b531f
1 /*
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.
22 #include "wconfig.h"
24 #ifdef BALLOON_TEXT
26 #include <stdio.h>
27 #include <X11/Xlib.h>
28 #include <X11/Xutil.h>
29 #ifdef SHAPED_BALLOON
30 #include <X11/extensions/shape.h>
31 #endif
33 #include <stdlib.h>
34 #include <string.h>
36 #include <wraster.h>
38 #include "WindowMaker.h"
39 #include "screen.h"
40 #include "texture.h"
41 #include "framewin.h"
42 #include "icon.h"
43 #include "appicon.h"
44 #include "workspace.h"
45 #include "balloon.h"
46 #include "misc.h"
49 typedef struct _WBalloon {
50 Window window;
52 #ifdef SHAPED_BALLOON
53 GC monoGC;
54 #endif
55 int prevType;
57 Window objectWindow;
58 char *text;
59 int h;
61 WMHandlerID timer;
63 Pixmap contents;
64 Pixmap apercu;
66 char mapped;
67 char ignoreTimer;
68 } WBalloon;
70 #define TOP 0
71 #define BOTTOM 1
72 #define LEFT 0
73 #define RIGHT 2
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)
82 const char *p = text;
83 int h = 1;
85 while (*p) {
86 if (*p == '\n' && p[1] != 0)
87 h++;
88 p++;
90 return h;
93 static int getMaxStringWidth(WMFont * font, const char *text)
95 const char *p = text;
96 const char *pb = p;
97 int pos = 0;
98 int w = 0, wt;
100 while (*p) {
101 if (*p == '\n') {
102 wt = WMWidthOfString(font, pb, pos);
103 if (wt > w)
104 w = wt;
105 pos = 0;
106 pb = p + 1;
107 } else {
108 pos++;
110 p++;
112 if (pos > 0) {
113 wt = WMWidthOfString(font, pb, pos);
114 if (wt > w)
115 w = wt;
117 return w;
120 static void
121 drawMultiLineString(WMScreen * scr, Pixmap pixmap, WMColor * color,
122 WMFont *font, int x, int y, const char *text, int len)
124 const char *p = text;
125 const char *pb = p;
126 int l = 0, pos = 0;
127 int height = WMFontHeight(font);
129 while (*p && p - text < len) {
130 if (*p == '\n') {
131 WMDrawString(scr, pixmap, color, font, x, y + l * height, pb, pos);
132 l++;
133 pos = 0;
134 pb = p + 1;
135 } else {
136 pos++;
138 p++;
140 if (pos > 0) {
141 WMDrawString(scr, pixmap, color, font, x, y + l * height, pb, pos);
145 #ifdef SHAPED_BALLOON
147 #define SPACE 12
149 static void drawBalloon(WScreen * scr, Pixmap bitmap, Pixmap pix, int x, int y, int w, int h, int side)
151 GC bgc = scr->balloon->monoGC;
152 GC gc = scr->draw_gc;
153 int rad = h * 3 / 10;
154 XPoint pt[3], ipt[3];
155 int w1;
157 /* outline */
158 XSetForeground(dpy, bgc, 1);
160 XFillArc(dpy, bitmap, bgc, x, y, rad, rad, 90 * 64, 90 * 64);
161 XFillArc(dpy, bitmap, bgc, x, y + h - 1 - rad, rad, rad, 180 * 64, 90 * 64);
163 XFillArc(dpy, bitmap, bgc, x + w - 1 - rad, y, rad, rad, 0 * 64, 90 * 64);
164 XFillArc(dpy, bitmap, bgc, x + w - 1 - rad, y + h - 1 - rad, rad, rad, 270 * 64, 90 * 64);
166 XFillRectangle(dpy, bitmap, bgc, x, y + rad / 2, w, h - rad);
167 XFillRectangle(dpy, bitmap, bgc, x + rad / 2, y, w - rad, h);
169 /* interior */
170 XSetForeground(dpy, gc, scr->white_pixel);
172 XFillArc(dpy, pix, gc, x + 1, y + 1, rad, rad, 90 * 64, 90 * 64);
173 XFillArc(dpy, pix, gc, x + 1, y + h - 2 - rad, rad, rad, 180 * 64, 90 * 64);
175 XFillArc(dpy, pix, gc, x + w - 2 - rad, y + 1, rad, rad, 0 * 64, 90 * 64);
176 XFillArc(dpy, pix, gc, x + w - 2 - rad, y + h - 2 - rad, rad, rad, 270 * 64, 90 * 64);
178 XFillRectangle(dpy, pix, gc, x + 1, y + 1 + rad / 2, w - 2, h - 2 - rad);
179 XFillRectangle(dpy, pix, gc, x + 1 + rad / 2, y + 1, w - 2 - rad, h - 2);
181 if (side & BOTTOM) {
182 pt[0].y = y + h - 1;
183 pt[1].y = y + h - 1 + SPACE;
184 pt[2].y = y + h - 1;
185 ipt[0].y = pt[0].y - 1;
186 ipt[1].y = pt[1].y - 1;
187 ipt[2].y = pt[2].y - 1;
188 } else {
189 pt[0].y = y;
190 pt[1].y = y - SPACE;
191 pt[2].y = y;
192 ipt[0].y = pt[0].y + 1;
193 ipt[1].y = pt[1].y + 1;
194 ipt[2].y = pt[2].y + 1;
197 /*w1 = WMAX(h, 24); */
198 w1 = WMAX(h, 21);
200 if (side & RIGHT) {
201 pt[0].x = x + w - w1 + 2 * w1 / 16;
202 pt[1].x = x + w - w1 + 11 * w1 / 16;
203 pt[2].x = x + w - w1 + 7 * w1 / 16;
204 ipt[0].x = x + 1 + w - w1 + 2 * (w1 - 1) / 16;
205 ipt[1].x = x + 1 + w - w1 + 11 * (w1 - 1) / 16;
206 ipt[2].x = x + 1 + w - w1 + 7 * (w1 - 1) / 16;
207 /*ipt[0].x = pt[0].x+1;
208 ipt[1].x = pt[1].x;
209 ipt[2].x = pt[2].x; */
210 } else {
211 pt[0].x = x + w1 - 2 * w1 / 16;
212 pt[1].x = x + w1 - 11 * w1 / 16;
213 pt[2].x = x + w1 - 7 * w1 / 16;
214 ipt[0].x = x - 1 + w1 - 2 * (w1 - 1) / 16;
215 ipt[1].x = x - 1 + w1 - 11 * (w1 - 1) / 16;
216 ipt[2].x = x - 1 + w1 - 7 * (w1 - 1) / 16;
217 /*ipt[0].x = pt[0].x-1;
218 ipt[1].x = pt[1].x;
219 ipt[2].x = pt[2].x; */
222 XFillPolygon(dpy, bitmap, bgc, pt, 3, Convex, CoordModeOrigin);
223 XFillPolygon(dpy, pix, gc, ipt, 3, Convex, CoordModeOrigin);
225 /* fix outline */
226 XSetForeground(dpy, gc, scr->black_pixel);
228 XDrawLines(dpy, pix, gc, pt, 3, CoordModeOrigin);
229 if (side & RIGHT) {
230 pt[0].x++;
231 pt[2].x--;
232 } else {
233 pt[0].x--;
234 pt[2].x++;
236 XDrawLines(dpy, pix, gc, pt, 3, CoordModeOrigin);
239 static Pixmap makePixmap(WScreen * scr, int width, int height, int side, Pixmap * mask)
241 WBalloon *bal = scr->balloon;
242 Pixmap bitmap;
243 Pixmap pixmap;
244 int x, y;
246 bitmap = XCreatePixmap(dpy, scr->root_win, width + SPACE, height + SPACE, 1);
248 if (!bal->monoGC) {
249 bal->monoGC = XCreateGC(dpy, bitmap, 0, NULL);
251 XSetForeground(dpy, bal->monoGC, 0);
252 XFillRectangle(dpy, bitmap, bal->monoGC, 0, 0, width + SPACE, height + SPACE);
254 pixmap = XCreatePixmap(dpy, scr->root_win, width + SPACE, height + SPACE, scr->w_depth);
255 XSetForeground(dpy, scr->draw_gc, scr->black_pixel);
256 XFillRectangle(dpy, pixmap, scr->draw_gc, 0, 0, width + SPACE, height + SPACE);
258 if (side & BOTTOM) {
259 y = 0;
260 } else {
261 y = SPACE;
263 x = 0;
265 drawBalloon(scr, bitmap, pixmap, x, y, width, height, side);
267 *mask = bitmap;
269 return pixmap;
272 static void showText(WScreen *scr, int x, int y, int h, int w, const char *text)
274 int width;
275 int height;
276 Pixmap pixmap;
277 Pixmap mask;
278 WMFont *font = scr->info_text_font;
279 int side = 0;
280 int ty;
281 int bx, by;
283 if (scr->balloon->contents)
284 XFreePixmap(dpy, scr->balloon->contents);
286 width = getMaxStringWidth(font, text) + 16;
287 height = countLines(text) * WMFontHeight(font) + 4;
289 if (height < 16)
290 height = 16;
291 if (width < height)
292 width = height;
294 if (x + width > scr->scr_width) {
295 side = RIGHT;
296 bx = x - width + w / 2;
297 if (bx < 0)
298 bx = 0;
299 } else {
300 side = LEFT;
301 bx = x + w / 2;
303 if (bx + width > scr->scr_width)
304 bx = scr->scr_width - width;
306 if (y - (height + SPACE) < 0) {
307 side |= TOP;
308 by = y + h - 1;
309 ty = SPACE;
310 } else {
311 side |= BOTTOM;
312 by = y - (height + SPACE);
313 ty = 0;
315 pixmap = makePixmap(scr, width, height, side, &mask);
317 drawMultiLineString(scr->wmscreen, pixmap, scr->black, font, 8, ty + 2, text, strlen(text));
319 XSetWindowBackgroundPixmap(dpy, scr->balloon->window, pixmap);
320 scr->balloon->contents = pixmap;
322 XResizeWindow(dpy, scr->balloon->window, width, height + SPACE);
323 XShapeCombineMask(dpy, scr->balloon->window, ShapeBounding, 0, 0, mask, ShapeSet);
324 XFreePixmap(dpy, mask);
325 XMoveWindow(dpy, scr->balloon->window, bx, by);
326 XMapRaised(dpy, scr->balloon->window);
328 scr->balloon->mapped = 1;
330 #else /* !SHAPED_BALLOON */
332 static void showText(WScreen *scr, int x, int y, int h, int w, const char *text)
334 int width;
335 int height;
336 Pixmap pixmap;
337 WMFont *font = scr->info_text_font;
339 if (scr->balloon->contents)
340 XFreePixmap(dpy, scr->balloon->contents);
342 width = getMaxStringWidth(font, text) + 8;
343 /*width = WMWidthOfString(font, text, strlen(text))+8; */
344 height = countLines(text) * WMFontHeight(font) + 4;
346 if (x < 0)
347 x = 0;
348 else if (x + width > scr->scr_width - 1)
349 x = scr->scr_width - width;
351 if (y - height - 2 < 0) {
352 y += h;
353 if (y < 0)
354 y = 0;
355 } else {
356 y -= height + 2;
359 if (scr->window_title_texture[0])
360 XSetForeground(dpy, scr->draw_gc, scr->window_title_texture[0]->any.color.pixel);
361 else
362 XSetForeground(dpy, scr->draw_gc, scr->light_pixel);
364 pixmap = XCreatePixmap(dpy, scr->root_win, width, height, scr->w_depth);
365 XFillRectangle(dpy, pixmap, scr->draw_gc, 0, 0, width, height);
367 drawMultiLineString(scr->wmscreen, pixmap, scr->window_title_color[0], font, 4, 2, text, strlen(text));
369 XResizeWindow(dpy, scr->balloon->window, width, height);
370 XMoveWindow(dpy, scr->balloon->window, x, y);
372 XSetWindowBackgroundPixmap(dpy, scr->balloon->window, pixmap);
373 XClearWindow(dpy, scr->balloon->window);
374 XMapRaised(dpy, scr->balloon->window);
376 scr->balloon->contents = pixmap;
378 scr->balloon->mapped = 1;
380 #endif /* !SHAPED_BALLOON */
382 static void showApercu(WScreen *scr, int x, int y, int height, int width, char *title, Pixmap apercu)
384 Pixmap pixmap;
385 WMFont *font = scr->info_text_font;
386 int titleHeight = 0;
387 char *shortenTitle = title;
389 if (scr->balloon->contents)
390 XFreePixmap(dpy, scr->balloon->contents);
392 if (wPreferences.miniwin_title_balloon) {
393 shortenTitle = ShrinkString(font, title, width - APERCU_BORDER);
394 titleHeight = countLines(shortenTitle) * WMFontHeight(font) + 4;
395 height += titleHeight;
398 if (x < 0)
399 x = 0;
400 else if (x + width > scr->scr_width - 1)
401 x = scr->scr_width - width - APERCU_BORDER;
403 if (y - height - 2 < 0) {
404 y += wPreferences.icon_size;
405 if (y < 0)
406 y = 0;
407 } else {
408 y -= height + 2;
411 if (scr->window_title_texture[0])
412 XSetForeground(dpy, scr->draw_gc, scr->window_title_texture[0]->any.color.pixel);
413 else
414 XSetForeground(dpy, scr->draw_gc, scr->light_pixel);
416 pixmap = XCreatePixmap(dpy, scr->root_win, width, height, scr->w_depth);
417 XFillRectangle(dpy, pixmap, scr->draw_gc, 0, 0, width, height);
419 if (shortenTitle && wPreferences.miniwin_title_balloon) {
420 drawMultiLineString(scr->wmscreen, pixmap, scr->window_title_color[0], font,
421 APERCU_BORDER, APERCU_BORDER, shortenTitle, strlen(shortenTitle));
422 wfree(shortenTitle);
425 XCopyArea(dpy, apercu, pixmap, scr->draw_gc,
426 0, 0, (wPreferences.icon_size - 1 - APERCU_BORDER) * 2,
427 (wPreferences.icon_size - 1 - APERCU_BORDER) * 2,
428 APERCU_BORDER, APERCU_BORDER + titleHeight);
430 #ifdef SHAPED_BALLOON
431 XShapeCombineMask(dpy, scr->balloon->window, ShapeBounding, 0, 0, None, ShapeSet);
432 #endif
433 XResizeWindow(dpy, scr->balloon->window, width, height);
434 XMoveWindow(dpy, scr->balloon->window, x, y);
436 XSetWindowBackgroundPixmap(dpy, scr->balloon->window, pixmap);
438 XClearWindow(dpy, scr->balloon->window);
439 XMapRaised(dpy, scr->balloon->window);
442 scr->balloon->contents = pixmap;
444 scr->balloon->mapped = 1;
447 static void showBalloon(WScreen * scr)
449 int x, y;
450 Window foow;
451 unsigned foo, w;
453 scr->balloon->timer = NULL;
454 scr->balloon->ignoreTimer = 1;
456 if (!XGetGeometry(dpy, scr->balloon->objectWindow, &foow, &x, &y, &w, &foo, &foo, &foo)) {
457 scr->balloon->prevType = 0;
458 return;
461 if (wPreferences.miniwin_apercu_balloon && scr->balloon->apercu != None)
462 /* used to display either the apercu alone or the apercu and the title */
463 showApercu(scr, x, y, (wPreferences.icon_size - 1) * 2, (wPreferences.icon_size - 1) * 2,
464 scr->balloon->text, scr->balloon->apercu);
465 else
466 showText(scr, x, y, scr->balloon->h, w, scr->balloon->text);
469 static void frameBalloon(WObjDescriptor * object)
471 WFrameWindow *fwin = (WFrameWindow *) object->parent;
472 WScreen *scr = fwin->core->screen_ptr;
474 if (fwin->titlebar != object->self || !fwin->flags.is_client_window_frame) {
475 wBalloonHide(scr);
476 return;
478 if (fwin->title && fwin->flags.incomplete_title) {
479 scr->balloon->h = (fwin->titlebar ? fwin->titlebar->height : 0);
480 scr->balloon->text = wstrdup(fwin->title);
481 scr->balloon->objectWindow = fwin->core->window;
482 scr->balloon->timer = WMAddTimerHandler(BALLOON_DELAY, (WMCallback *) showBalloon, scr);
486 static void miniwindowBalloon(WObjDescriptor * object)
488 WIcon *icon = (WIcon *) object->parent;
489 WScreen *scr = icon->core->screen_ptr;
491 if (!icon->icon_name) {
492 wBalloonHide(scr);
493 return;
495 scr->balloon->h = icon->core->height;
496 scr->balloon->text = wstrdup(icon->icon_name);
497 scr->balloon->apercu = icon->apercu;
498 scr->balloon->objectWindow = icon->core->window;
500 if ((scr->balloon->prevType == object->parent_type || scr->balloon->prevType == WCLASS_APPICON)
501 && scr->balloon->ignoreTimer) {
502 XUnmapWindow(dpy, scr->balloon->window);
503 showBalloon(scr);
504 } else {
505 scr->balloon->timer = WMAddTimerHandler(BALLOON_DELAY, (WMCallback *) showBalloon, scr);
509 static void appiconBalloon(WObjDescriptor *object)
511 WAppIcon *aicon = (WAppIcon *) object->parent;
512 WScreen *scr = aicon->icon->core->screen_ptr;
513 char *tmp;
515 /* Show balloon if it is the Clip and the workspace name is > 5 chars */
516 if (object->parent == w_global.clip.icon) {
517 if (strlen(w_global.workspace.array[w_global.workspace.current]->name) > 5) {
518 scr->balloon->text = wstrdup(w_global.workspace.array[w_global.workspace.current]->name);
519 } else {
520 wBalloonHide(scr);
521 return;
523 } else if (aicon->command && aicon->wm_class) {
524 int len;
525 /* Check to see if it is a GNUstep app */
526 if (strcmp(aicon->wm_class, "GNUstep") == 0)
527 len = strlen(aicon->command) + strlen(aicon->wm_instance) + 8;
528 else
529 len = strlen(aicon->command) + strlen(aicon->wm_class) + 8;
530 tmp = wmalloc(len);
531 /* Check to see if it is a GNUstep App */
532 if (strcmp(aicon->wm_class, "GNUstep") == 0)
533 snprintf(tmp, len, "%s\n(%s)", aicon->wm_instance, aicon->command);
534 else
535 snprintf(tmp, len, "%s\n(%s)", aicon->wm_class, aicon->command);
536 scr->balloon->text = tmp;
537 } else if (aicon->command) {
538 scr->balloon->text = wstrdup(aicon->command);
539 } else if (aicon->wm_class) {
540 /* Check to see if it is a GNUstep App */
541 if (strcmp(aicon->wm_class, "GNUstep") == 0)
542 scr->balloon->text = wstrdup(aicon->wm_instance);
543 else
544 scr->balloon->text = wstrdup(aicon->wm_class);
545 } else {
546 wBalloonHide(scr);
547 return;
549 scr->balloon->h = aicon->icon->core->height - 2;
551 scr->balloon->objectWindow = aicon->icon->core->window;
552 if ((scr->balloon->prevType == object->parent_type || scr->balloon->prevType == WCLASS_MINIWINDOW)
553 && scr->balloon->ignoreTimer) {
554 XUnmapWindow(dpy, scr->balloon->window);
555 showBalloon(scr);
556 } else {
557 scr->balloon->timer = WMAddTimerHandler(BALLOON_DELAY, (WMCallback *) showBalloon, scr);
561 void wBalloonInitialize(WScreen * scr)
563 WBalloon *bal;
564 XSetWindowAttributes attribs;
565 unsigned long vmask;
567 bal = wmalloc(sizeof(WBalloon));
569 scr->balloon = bal;
571 vmask = CWSaveUnder | CWOverrideRedirect | CWColormap | CWBackPixel | CWBorderPixel;
572 attribs.save_under = True;
573 attribs.override_redirect = True;
574 attribs.colormap = scr->w_colormap;
575 attribs.background_pixel = scr->icon_back_texture->normal.pixel;
576 attribs.border_pixel = 0; /* do not care */
578 bal->window = XCreateWindow(dpy, scr->root_win, 1, 1, 10, 10, 1,
579 scr->w_depth, CopyFromParent, scr->w_visual, vmask, &attribs);
580 #if 0
581 /* select EnterNotify to so that the balloon will be unmapped
582 * when the pointer is moved over it */
583 XSelectInput(dpy, bal->window, EnterWindowMask);
584 #endif
587 void wBalloonEnteredObject(WScreen * scr, WObjDescriptor * object)
589 WBalloon *balloon = scr->balloon;
591 if (balloon->timer) {
592 WMDeleteTimerHandler(balloon->timer);
593 balloon->timer = NULL;
594 balloon->ignoreTimer = 0;
597 if (scr->balloon->text)
598 wfree(scr->balloon->text);
599 scr->balloon->text = NULL;
601 scr->balloon->apercu = None;
603 if (!object) {
604 wBalloonHide(scr);
605 balloon->ignoreTimer = 0;
606 return;
609 switch (object->parent_type) {
610 case WCLASS_FRAME:
611 if (wPreferences.window_balloon)
612 frameBalloon(object);
613 break;
614 case WCLASS_DOCK_ICON:
615 if (wPreferences.appicon_balloon)
616 appiconBalloon(object);
617 break;
618 case WCLASS_MINIWINDOW:
619 if (wPreferences.miniwin_title_balloon || wPreferences.miniwin_apercu_balloon)
620 miniwindowBalloon(object);
621 break;
622 case WCLASS_APPICON:
623 if (wPreferences.appicon_balloon)
624 appiconBalloon(object);
625 break;
626 default:
627 wBalloonHide(scr);
628 break;
630 scr->balloon->prevType = object->parent_type;
633 void wBalloonHide(WScreen * scr)
635 if (scr) {
636 if (scr->balloon->mapped) {
637 XUnmapWindow(dpy, scr->balloon->window);
638 scr->balloon->mapped = 0;
639 } else if (scr->balloon->timer) {
640 WMDeleteTimerHandler(scr->balloon->timer);
641 scr->balloon->timer = NULL;
643 scr->balloon->prevType = 0;
647 #endif