2 Copyright (c) 1997 Douglas Keller
4 This file is part of XEmacs.
6 XEmacs is free software; you can redistribute it and/or modify it
7 under the terms of the GNU General Public License as published by the
8 Free Software Foundation; either version 2, or (at your option) any
11 XEmacs is distributed in the hope that it will be useful, but WITHOUT
12 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 You should have received a copy of the GNU General Public License
17 along with XEmacs; see the file COPYING. If not, write to
18 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA. */
21 /* Synched up with: Not in FSF. */
26 * Version: 1.337 (Sun Apr 13 04:52:10 1997)
28 * Written by Douglas Keller <dkeller@vnet.ibm.com>
40 #include <X11/Xutil.h>
41 #include <X11/extensions/shape.h>
43 #include "xintrinsic.h"
45 #include "balloon_help.h"
48 #define max(x,y) (x>y?x:y)
54 #define MARGIN_WIDTH 4
55 #define POINTER_OFFSET 8
56 #define BORDER_WIDTH 2
57 #define BORDER_WIDTH_HALF 1
59 #define CONE_HEIGHT 20
62 #define SHAPE_CONE_TOP (1<<0)
63 #define SHAPE_CONE_LEFT (1<<1)
64 #define SHAPE_CONE_TOP_LEFT (SHAPE_CONE_TOP | SHAPE_CONE_LEFT)
65 #define SHAPE_CONE_TOP_RIGHT (SHAPE_CONE_TOP)
66 #define SHAPE_CONE_BOTTOM_LEFT (SHAPE_CONE_LEFT)
67 #define SHAPE_CONE_BOTTOM_RIGHT (0)
68 #define SHAPE_CONE_FREE (-1)
71 static Display* b_dpy;
73 static XFontStruct* b_fontStruct;
80 static bool b_winMapped;
83 static int b_maskWidth, b_maskHeight;
86 static CONST char* b_text;
87 static int b_width, b_height;
89 static int b_lastX, b_lastY;
91 static XtIntervalId b_timer;
92 static unsigned long b_delay;
94 static int b_screenWidth, b_screenHeight;
96 static int b_lastShape;
98 /*============================================================================
100 ============================================================================*/
103 create_gc (Display* dpy, Window win, unsigned long fg, unsigned long bg,
104 XFontStruct* fontStruct)
111 gcv.font = fontStruct->fid;
112 gcv.join_style = JoinMiter;
113 gcv.line_width = BORDER_WIDTH;
115 mask = GCFont | GCBackground | GCForeground | GCJoinStyle | GCLineWidth;
117 return XCreateGC (dpy, win, mask, &gcv);
121 destroy_gc (Display* dpy, GC gc)
129 /*============================================================================
131 ============================================================================*/
134 create_window (Display* dpy, unsigned long bg)
137 XSetWindowAttributes attr;
138 unsigned long attr_mask;
140 attr_mask = CWOverrideRedirect | CWBackPixel | CWSaveUnder;
141 attr.override_redirect = True;
142 attr.background_pixel = bg;
143 attr.save_under = True;
147 DefaultRootWindow (dpy),
150 CopyFromParent, InputOutput, CopyFromParent,
153 XSelectInput (dpy, win,
154 SubstructureRedirectMask |
155 SubstructureNotifyMask |
163 destroy_window (Display* dpy, Window win)
167 XDestroyWindow (dpy, win);
171 /*============================================================================
173 ============================================================================*/
176 get_pointer_xy (Display* dpy, int* x_return, int* y_return)
182 XQueryPointer (dpy, RootWindow(dpy, DefaultScreen(dpy)), &dummy_win, &dummy_win,
183 x_return, y_return, &dummy, &dummy, &mask);
186 /*============================================================================
188 ============================================================================*/
191 create_pixmap_mask (int width, int height)
194 b_maskHeight = height;
195 b_mask = XCreatePixmap (b_dpy, b_win, width, height, 1);
199 destroy_pixmap_mask(void)
201 XFreePixmap (b_dpy, b_mask);
205 grow_pixmap_mask (int width, int height)
207 if (width > b_maskWidth || height > b_maskHeight)
209 destroy_pixmap_mask ();
210 create_pixmap_mask (width, height);
214 /*============================================================================
216 ============================================================================*/
219 text_extent (XFontStruct* fontStruct, CONST char* text, int len,
220 int* width, int* height)
225 XTextExtents (fontStruct, text, len, &dummy, &dummy, &dummy, &extent);
227 *width = extent.width;
228 *height = fontStruct->ascent + fontStruct->descent;
232 get_text_size (Display* dpy, XFontStruct* fontStruct, CONST char* text,
233 int* max_width, int* max_height)
240 *max_width = *max_height = 0;
243 while ((end = strchr(start, '\n')))
245 text_extent (fontStruct, start, end - start, &width, &height);
246 *max_width = max (width, *max_width);
247 *max_height += height;
251 text_extent (fontStruct, start, strlen (start), &width, &height);
252 *max_width = max (width, *max_width);
253 *max_height += height;
256 *max_width = max (*max_width, CONE_WIDTH / 2 * 3);
261 draw_text (Display* dpy, Window win, GC gc, XFontStruct* fontStruct,
262 int x, int y, CONST char* text)
268 y += fontStruct->ascent;
270 font_height = fontStruct->ascent + fontStruct->descent;
273 while ((end = strchr(start, '\n')))
275 XDrawString (dpy, win, gc, x, y, start, end - start);
280 XDrawString (dpy, win, gc, x, y, start, strlen (start));
283 /*============================================================================
285 ============================================================================*/
288 get_shape (int last_shape, int x, int y, int width, int height,
289 int screen_width, int screen_height)
291 /* Can we use last_shape? */
292 if (((last_shape == SHAPE_CONE_TOP_LEFT) &&
293 (x + width < screen_width) && (y + height < screen_height)) ||
294 ((last_shape == SHAPE_CONE_TOP_RIGHT) &&
295 (x - width > 0) && (y + height < screen_height)) ||
296 ((last_shape == SHAPE_CONE_BOTTOM_LEFT) &&
297 (x + width < screen_width) && (y - height > 0)) ||
298 ((last_shape == SHAPE_CONE_BOTTOM_RIGHT) &&
299 (x - width > 0) && (y - height > 0)))
302 /* Try to pick a shape that will not get changed,
303 e.g. if top left quadrant, top_left */
304 return (x < screen_width / 2) ?
305 (y < screen_height / 2 ? SHAPE_CONE_TOP_LEFT: SHAPE_CONE_BOTTOM_LEFT) :
306 (y < screen_height / 2 ? SHAPE_CONE_TOP_RIGHT: SHAPE_CONE_BOTTOM_RIGHT);
310 make_mask (int shape, int x, int y, int width, int height)
314 grow_pixmap_mask (width, height);
317 XSetForeground (b_dpy, b_maskGC, 0);
318 XFillRectangle (b_dpy, b_mask, b_maskGC,
319 0, 0, width, height);
321 /* Enable text area */
322 XSetForeground (b_dpy, b_maskGC, 1);
323 XFillRectangle (b_dpy, b_mask, b_maskGC, 0,
324 shape & SHAPE_CONE_TOP ? CONE_HEIGHT : 0, width, height - CONE_HEIGHT);
326 /* Enable for cone area */
327 cone[0].x = (shape & SHAPE_CONE_LEFT) ? CONE_WIDTH / 2 : width - (CONE_WIDTH / 2);
328 cone[0].y = (shape & SHAPE_CONE_TOP) ? CONE_HEIGHT : height - CONE_HEIGHT;
329 cone[1].x = (shape & SHAPE_CONE_LEFT) ? 0 : width;
330 cone[1].y = (shape & SHAPE_CONE_TOP) ? 0 : height;
331 cone[2].x = (shape & SHAPE_CONE_LEFT) ? CONE_WIDTH : width - CONE_WIDTH;
332 cone[2].y = (shape & SHAPE_CONE_TOP) ? CONE_HEIGHT : height - CONE_HEIGHT;
334 XFillPolygon (b_dpy, b_mask, b_maskGC, cone, 3, Nonconvex, CoordModeOrigin);
339 show_help (XtPointer data, XtIntervalId* id)
345 if (id == NULL || ((id && b_timer) && b_text))
350 get_text_size (b_dpy, b_fontStruct, b_text, &b_width, &b_height);
351 b_width += 2 * MARGIN_WIDTH + 2 * BORDER_WIDTH;
352 b_height += 2 * MARGIN_WIDTH + 2 * BORDER_WIDTH + CONE_HEIGHT;
355 get_pointer_xy (b_dpy, &x, &y);
358 shape = get_shape(b_lastShape, x, y, b_width, b_height,
359 b_screenWidth, b_screenHeight);
361 x += (shape & SHAPE_CONE_LEFT) ? POINTER_OFFSET : -POINTER_OFFSET;
362 y += (shape & SHAPE_CONE_TOP) ? POINTER_OFFSET : -POINTER_OFFSET;
364 /* make sure it is still ok with offset */
365 shape = get_shape (shape, x, y, b_width, b_height, b_screenWidth, b_screenHeight);
372 make_mask (shape, x, y, b_width, b_height);
374 XShapeCombineMask (b_dpy, b_win, ShapeBounding, 0, 0, b_mask, ShapeSet);
376 XMoveResizeWindow(b_dpy, b_win,
377 (shape & SHAPE_CONE_LEFT) ? x : x - b_width,
378 (shape & SHAPE_CONE_TOP) ? y : y - b_height,
381 XClearWindow (b_dpy, b_win);
383 XMapRaised (b_dpy, b_win);
386 draw_text (b_dpy, b_win, b_gc, b_fontStruct,
387 BORDER_WIDTH + MARGIN_WIDTH,
388 BORDER_WIDTH + MARGIN_WIDTH + ((shape & SHAPE_CONE_TOP) ? CONE_HEIGHT : 0),
392 /* shine- top left */
393 border[0].x = 0 + BORDER_WIDTH_HALF;
394 border[0].y = ((shape & SHAPE_CONE_TOP) ? b_height : b_height - CONE_HEIGHT) - BORDER_WIDTH_HALF;
395 border[1].x = 0 + BORDER_WIDTH_HALF;
396 border[1].y = ((shape & SHAPE_CONE_TOP) ? CONE_HEIGHT : 0) + BORDER_WIDTH_HALF;
397 border[2].x = b_width - BORDER_WIDTH_HALF;
398 border[2].y = border[1].y;
399 XDrawLines (b_dpy, b_win, b_shineGC, border, 3, CoordModeOrigin);
401 /* shadow- bottom right */
402 border[0].x = 0 + BORDER_WIDTH_HALF;
403 border[0].y = ((shape & SHAPE_CONE_TOP) ? b_height : b_height - CONE_HEIGHT) - BORDER_WIDTH_HALF;
404 border[1].x = b_width - BORDER_WIDTH_HALF;
405 border[1].y = border[0].y;
406 border[2].x = b_width - BORDER_WIDTH_HALF;
407 border[2].y = ((shape & SHAPE_CONE_TOP) ? CONE_HEIGHT : 0) + BORDER_WIDTH_HALF;
408 XDrawLines (b_dpy, b_win, b_shadowGC, border, 3, CoordModeOrigin);
411 if (SHAPE_CONE_TOP_LEFT == shape)
413 XClearArea (b_dpy, b_win,
414 CONE_WIDTH / 2 + BORDER_WIDTH,
416 CONE_WIDTH / 2 - BORDER_WIDTH,
417 BORDER_WIDTH, False);
418 XDrawLine (b_dpy, b_win, b_shadowGC,
421 CONE_WIDTH / 2 + BORDER_WIDTH_HALF,
423 XDrawLine (b_dpy, b_win, b_shineGC,
426 CONE_WIDTH - BORDER_WIDTH_HALF,
429 else if (SHAPE_CONE_TOP_RIGHT == shape)
431 XClearArea (b_dpy, b_win,
432 b_width - CONE_WIDTH + BORDER_WIDTH,
434 CONE_WIDTH / 2 - BORDER_WIDTH,
435 BORDER_WIDTH, False);
436 XDrawLine (b_dpy, b_win, b_shadowGC,
439 b_width - CONE_WIDTH / 2 - BORDER_WIDTH_HALF,
441 XDrawLine (b_dpy, b_win, b_shineGC,
444 b_width - CONE_WIDTH + BORDER_WIDTH_HALF,
447 else if (SHAPE_CONE_BOTTOM_LEFT == shape)
449 XClearArea (b_dpy, b_win,
450 CONE_WIDTH / 2 + BORDER_WIDTH,
451 b_height - CONE_HEIGHT - BORDER_WIDTH,
452 CONE_WIDTH / 2 - BORDER_WIDTH,
453 BORDER_WIDTH, False);
454 XDrawLine (b_dpy, b_win, b_shadowGC,
458 b_height - 1 - CONE_HEIGHT);
459 XDrawLine (b_dpy, b_win, b_shineGC,
462 CONE_WIDTH / 2 + BORDER_WIDTH,
463 b_height - 1 - CONE_HEIGHT);
465 else if (SHAPE_CONE_BOTTOM_RIGHT == shape)
467 XClearArea (b_dpy, b_win,
468 b_width - 1 - CONE_WIDTH + BORDER_WIDTH,
469 b_height - CONE_HEIGHT - BORDER_WIDTH,
470 CONE_WIDTH / 2 - BORDER_WIDTH - 1,
471 BORDER_WIDTH, False);
472 XDrawLine (b_dpy, b_win, b_shadowGC,
475 b_width - 1 - CONE_WIDTH,
476 b_height - 1 - CONE_HEIGHT);
477 XDrawLine (b_dpy, b_win, b_shineGC,
480 b_width - 1 - CONE_WIDTH / 2 - BORDER_WIDTH,
481 b_height - 1 - CONE_HEIGHT);
487 /*============================================================================
489 ============================================================================*/
492 balloon_help_destroy (void)
494 assert (b_dpy != NULL);
497 destroy_window (b_dpy, b_win);
498 destroy_gc (b_dpy, b_gc);
500 destroy_gc (b_dpy, b_shineGC);
501 destroy_gc (b_dpy, b_shadowGC);
503 destroy_pixmap_mask ();
504 destroy_gc (b_dpy, b_maskGC);
506 if (b_timer) XtRemoveTimeOut (b_timer);
510 balloon_help_create (Display* dpy,
511 Pixel fg, Pixel bg, Pixel shine, Pixel shadow,
514 if (b_dpy) balloon_help_destroy ();
520 b_win = create_window (dpy, bg);
521 b_gc = create_gc (dpy, b_win, fg, bg, b_fontStruct);
523 b_shineGC = create_gc (dpy, b_win, shine, bg, b_fontStruct);
524 b_shadowGC = create_gc (dpy, b_win, shadow, bg, b_fontStruct);
526 create_pixmap_mask (1, 1);
527 b_maskGC = create_gc (dpy, b_mask, bg, fg, b_fontStruct);
533 b_screenWidth = DisplayWidth (b_dpy, DefaultScreen(b_dpy));
534 b_screenHeight = DisplayHeight (b_dpy, DefaultScreen(b_dpy));
536 b_lastShape = SHAPE_CONE_FREE;
540 balloon_help_set_delay (unsigned long milliseconds)
542 b_delay = milliseconds;
546 balloon_help_show (CONST char* text)
548 assert (b_dpy != NULL);
550 /* We don't copy the text */
552 b_lastShape = SHAPE_CONE_FREE;
556 /* If help is already being shown, don't delay just update */
557 show_help (NULL, NULL);
562 XtAppAddTimeOut (XtDisplayToApplicationContext(b_dpy),
563 b_delay, show_help, NULL);
568 balloon_help_hide (void)
570 assert (b_dpy != NULL);
573 XUnmapWindow (b_dpy, b_win);
577 XtRemoveTimeOut (b_timer);
583 balloon_help_move_to_pointer (void)
585 assert (b_dpy != NULL);
590 int shape = b_lastShape;
592 get_pointer_xy (b_dpy, &x, &y);
594 x += (shape & SHAPE_CONE_LEFT) ? POINTER_OFFSET : -POINTER_OFFSET;
595 y += (shape & SHAPE_CONE_TOP) ? POINTER_OFFSET : -POINTER_OFFSET;
597 shape = get_shape (shape, x, y, b_width, b_height, b_screenWidth, b_screenHeight);
599 if (shape == b_lastShape)
604 XMoveWindow (b_dpy, b_win,
605 shape & SHAPE_CONE_LEFT ? x : x - b_width,
606 shape & SHAPE_CONE_TOP ? y : y - b_height);
610 /* text would be off screen, rebuild with new shape */
611 b_lastShape = SHAPE_CONE_FREE;
612 show_help (NULL, NULL);