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>
39 #include <X11/Xutil.h>
40 #include <X11/extensions/shape.h>
42 #include "xintrinsic.h"
44 #include "balloon_help.h"
47 #define max(x,y) (x>y?x:y)
53 #define MARGIN_WIDTH 4
54 #define POINTER_OFFSET 8
55 #define BORDER_WIDTH 2
56 #define BORDER_WIDTH_HALF 1
58 #define CONE_HEIGHT 20
61 #define SHAPE_CONE_TOP (1<<0)
62 #define SHAPE_CONE_LEFT (1<<1)
63 #define SHAPE_CONE_TOP_LEFT (SHAPE_CONE_TOP | SHAPE_CONE_LEFT)
64 #define SHAPE_CONE_TOP_RIGHT (SHAPE_CONE_TOP)
65 #define SHAPE_CONE_BOTTOM_LEFT (SHAPE_CONE_LEFT)
66 #define SHAPE_CONE_BOTTOM_RIGHT (0)
67 #define SHAPE_CONE_FREE (-1)
70 static Display* b_dpy;
72 static XFontStruct* b_fontStruct;
79 static bool b_winMapped;
82 static int b_maskWidth, b_maskHeight;
85 static CONST char* b_text;
86 static int b_width, b_height;
88 static XtIntervalId b_timer;
89 static unsigned long b_delay;
91 static int b_screenWidth, b_screenHeight;
93 static int b_lastShape;
95 /*============================================================================
97 ============================================================================*/
100 create_gc (Display* dpy, Window win, unsigned long fg, unsigned long bg,
101 XFontStruct* fontStruct)
108 gcv.font = fontStruct->fid;
109 gcv.join_style = JoinMiter;
110 gcv.line_width = BORDER_WIDTH;
112 mask = GCFont | GCBackground | GCForeground | GCJoinStyle | GCLineWidth;
114 return XCreateGC (dpy, win, mask, &gcv);
118 destroy_gc (Display* dpy, GC gc)
126 /*============================================================================
128 ============================================================================*/
131 create_window (Display* dpy, unsigned long bg)
134 XSetWindowAttributes attr;
135 unsigned long attr_mask;
137 attr_mask = CWOverrideRedirect | CWBackPixel | CWSaveUnder;
138 attr.override_redirect = True;
139 attr.background_pixel = bg;
140 attr.save_under = True;
144 DefaultRootWindow (dpy),
147 CopyFromParent, InputOutput, CopyFromParent,
150 XSelectInput (dpy, win,
151 SubstructureRedirectMask |
152 SubstructureNotifyMask |
160 destroy_window (Display* dpy, Window win)
164 XDestroyWindow (dpy, win);
168 /*============================================================================
170 ============================================================================*/
173 get_pointer_xy (Display* dpy, int* x_return, int* y_return)
179 XQueryPointer (dpy, RootWindow(dpy, DefaultScreen(dpy)), &dummy_win, &dummy_win,
180 x_return, y_return, &dummy, &dummy, &mask);
183 /*============================================================================
185 ============================================================================*/
188 create_pixmap_mask (int width, int height)
191 b_maskHeight = height;
192 b_mask = XCreatePixmap (b_dpy, b_win, width, height, 1);
196 destroy_pixmap_mask(void)
198 XFreePixmap (b_dpy, b_mask);
202 grow_pixmap_mask (int width, int height)
204 if (width > b_maskWidth || height > b_maskHeight)
206 destroy_pixmap_mask ();
207 create_pixmap_mask (width, height);
211 /*============================================================================
213 ============================================================================*/
216 text_extent (XFontStruct* fontStruct, CONST char* text, int len,
217 int* width, int* height)
222 XTextExtents (fontStruct, text, len, &dummy, &dummy, &dummy, &extent);
224 *width = extent.width;
225 *height = fontStruct->ascent + fontStruct->descent;
229 get_text_size (Display* dpy, XFontStruct* fontStruct, CONST char* text,
230 int* max_width, int* max_height)
237 *max_width = *max_height = 0;
240 while ((end = strchr(start, '\n')))
242 text_extent (fontStruct, start, end - start, &width, &height);
243 *max_width = max (width, *max_width);
244 *max_height += height;
248 text_extent (fontStruct, start, strlen (start), &width, &height);
249 *max_width = max (width, *max_width);
250 *max_height += height;
253 *max_width = max (*max_width, CONE_WIDTH / 2 * 3);
258 draw_text (Display* dpy, Window win, GC gc, XFontStruct* fontStruct,
259 int x, int y, CONST char* text)
265 y += fontStruct->ascent;
267 font_height = fontStruct->ascent + fontStruct->descent;
270 while ((end = strchr(start, '\n')))
272 XDrawString (dpy, win, gc, x, y, start, end - start);
277 XDrawString (dpy, win, gc, x, y, start, strlen (start));
280 /*============================================================================
282 ============================================================================*/
285 get_shape (int last_shape, int x, int y, int width, int height,
286 int screen_width, int screen_height)
288 /* Can we use last_shape? */
289 if (((last_shape == SHAPE_CONE_TOP_LEFT) &&
290 (x + width < screen_width) && (y + height < screen_height)) ||
291 ((last_shape == SHAPE_CONE_TOP_RIGHT) &&
292 (x - width > 0) && (y + height < screen_height)) ||
293 ((last_shape == SHAPE_CONE_BOTTOM_LEFT) &&
294 (x + width < screen_width) && (y - height > 0)) ||
295 ((last_shape == SHAPE_CONE_BOTTOM_RIGHT) &&
296 (x - width > 0) && (y - height > 0)))
299 /* Try to pick a shape that will not get changed,
300 e.g. if top left quadrant, top_left */
301 return (x < screen_width / 2) ?
302 (y < screen_height / 2 ? SHAPE_CONE_TOP_LEFT: SHAPE_CONE_BOTTOM_LEFT) :
303 (y < screen_height / 2 ? SHAPE_CONE_TOP_RIGHT: SHAPE_CONE_BOTTOM_RIGHT);
307 make_mask (int shape, int x, int y, int width, int height)
311 grow_pixmap_mask (width, height);
314 XSetForeground (b_dpy, b_maskGC, 0);
315 XFillRectangle (b_dpy, b_mask, b_maskGC,
316 0, 0, width, height);
318 /* Enable text area */
319 XSetForeground (b_dpy, b_maskGC, 1);
320 XFillRectangle (b_dpy, b_mask, b_maskGC, 0,
321 shape & SHAPE_CONE_TOP ? CONE_HEIGHT : 0, width, height - CONE_HEIGHT);
323 /* Enable for cone area */
324 cone[0].x = (shape & SHAPE_CONE_LEFT) ? CONE_WIDTH / 2 : width - (CONE_WIDTH / 2);
325 cone[0].y = (shape & SHAPE_CONE_TOP) ? CONE_HEIGHT : height - CONE_HEIGHT;
326 cone[1].x = (shape & SHAPE_CONE_LEFT) ? 0 : width;
327 cone[1].y = (shape & SHAPE_CONE_TOP) ? 0 : height;
328 cone[2].x = (shape & SHAPE_CONE_LEFT) ? CONE_WIDTH : width - CONE_WIDTH;
329 cone[2].y = (shape & SHAPE_CONE_TOP) ? CONE_HEIGHT : height - CONE_HEIGHT;
331 XFillPolygon (b_dpy, b_mask, b_maskGC, cone, 3, Nonconvex, CoordModeOrigin);
336 show_help (XtPointer data, XtIntervalId* id)
342 if (id == NULL || ((id && b_timer) && b_text))
347 get_text_size (b_dpy, b_fontStruct, b_text, &b_width, &b_height);
348 b_width += 2 * MARGIN_WIDTH + 2 * BORDER_WIDTH;
349 b_height += 2 * MARGIN_WIDTH + 2 * BORDER_WIDTH + CONE_HEIGHT;
352 get_pointer_xy (b_dpy, &x, &y);
355 shape = get_shape(b_lastShape, x, y, b_width, b_height,
356 b_screenWidth, b_screenHeight);
358 x += (shape & SHAPE_CONE_LEFT) ? POINTER_OFFSET : -POINTER_OFFSET;
359 y += (shape & SHAPE_CONE_TOP) ? POINTER_OFFSET : -POINTER_OFFSET;
361 /* make sure it is still ok with offset */
362 shape = get_shape (shape, x, y, b_width, b_height, b_screenWidth, b_screenHeight);
366 make_mask (shape, x, y, b_width, b_height);
368 XShapeCombineMask (b_dpy, b_win, ShapeBounding, 0, 0, b_mask, ShapeSet);
370 XMoveResizeWindow(b_dpy, b_win,
371 (shape & SHAPE_CONE_LEFT) ? x : x - b_width,
372 (shape & SHAPE_CONE_TOP) ? y : y - b_height,
375 XClearWindow (b_dpy, b_win);
377 XMapRaised (b_dpy, b_win);
380 draw_text (b_dpy, b_win, b_gc, b_fontStruct,
381 BORDER_WIDTH + MARGIN_WIDTH,
382 BORDER_WIDTH + MARGIN_WIDTH + ((shape & SHAPE_CONE_TOP) ? CONE_HEIGHT : 0),
386 /* shine- top left */
387 border[0].x = 0 + BORDER_WIDTH_HALF;
388 border[0].y = ((shape & SHAPE_CONE_TOP) ? b_height : b_height - CONE_HEIGHT) - BORDER_WIDTH_HALF;
389 border[1].x = 0 + BORDER_WIDTH_HALF;
390 border[1].y = ((shape & SHAPE_CONE_TOP) ? CONE_HEIGHT : 0) + BORDER_WIDTH_HALF;
391 border[2].x = b_width - BORDER_WIDTH_HALF;
392 border[2].y = border[1].y;
393 XDrawLines (b_dpy, b_win, b_shineGC, border, 3, CoordModeOrigin);
395 /* shadow- bottom right */
396 border[0].x = 0 + BORDER_WIDTH_HALF;
397 border[0].y = ((shape & SHAPE_CONE_TOP) ? b_height : b_height - CONE_HEIGHT) - BORDER_WIDTH_HALF;
398 border[1].x = b_width - BORDER_WIDTH_HALF;
399 border[1].y = border[0].y;
400 border[2].x = b_width - BORDER_WIDTH_HALF;
401 border[2].y = ((shape & SHAPE_CONE_TOP) ? CONE_HEIGHT : 0) + BORDER_WIDTH_HALF;
402 XDrawLines (b_dpy, b_win, b_shadowGC, border, 3, CoordModeOrigin);
405 if (SHAPE_CONE_TOP_LEFT == shape)
407 XClearArea (b_dpy, b_win,
408 CONE_WIDTH / 2 + BORDER_WIDTH,
410 CONE_WIDTH / 2 - BORDER_WIDTH,
411 BORDER_WIDTH, False);
412 XDrawLine (b_dpy, b_win, b_shadowGC,
415 CONE_WIDTH / 2 + BORDER_WIDTH_HALF,
417 XDrawLine (b_dpy, b_win, b_shineGC,
420 CONE_WIDTH - BORDER_WIDTH_HALF,
423 else if (SHAPE_CONE_TOP_RIGHT == shape)
425 XClearArea (b_dpy, b_win,
426 b_width - CONE_WIDTH + BORDER_WIDTH,
428 CONE_WIDTH / 2 - BORDER_WIDTH,
429 BORDER_WIDTH, False);
430 XDrawLine (b_dpy, b_win, b_shadowGC,
433 b_width - CONE_WIDTH / 2 - BORDER_WIDTH_HALF,
435 XDrawLine (b_dpy, b_win, b_shineGC,
438 b_width - CONE_WIDTH + BORDER_WIDTH_HALF,
441 else if (SHAPE_CONE_BOTTOM_LEFT == shape)
443 XClearArea (b_dpy, b_win,
444 CONE_WIDTH / 2 + BORDER_WIDTH,
445 b_height - CONE_HEIGHT - BORDER_WIDTH,
446 CONE_WIDTH / 2 - BORDER_WIDTH,
447 BORDER_WIDTH, False);
448 XDrawLine (b_dpy, b_win, b_shadowGC,
452 b_height - 1 - CONE_HEIGHT);
453 XDrawLine (b_dpy, b_win, b_shineGC,
456 CONE_WIDTH / 2 + BORDER_WIDTH,
457 b_height - 1 - CONE_HEIGHT);
459 else if (SHAPE_CONE_BOTTOM_RIGHT == shape)
461 XClearArea (b_dpy, b_win,
462 b_width - 1 - CONE_WIDTH + BORDER_WIDTH,
463 b_height - CONE_HEIGHT - BORDER_WIDTH,
464 CONE_WIDTH / 2 - BORDER_WIDTH - 1,
465 BORDER_WIDTH, False);
466 XDrawLine (b_dpy, b_win, b_shadowGC,
469 b_width - 1 - CONE_WIDTH,
470 b_height - 1 - CONE_HEIGHT);
471 XDrawLine (b_dpy, b_win, b_shineGC,
474 b_width - 1 - CONE_WIDTH / 2 - BORDER_WIDTH,
475 b_height - 1 - CONE_HEIGHT);
481 /*============================================================================
483 ============================================================================*/
486 balloon_help_destroy (void)
488 assert (b_dpy != NULL);
491 destroy_window (b_dpy, b_win);
492 destroy_gc (b_dpy, b_gc);
494 destroy_gc (b_dpy, b_shineGC);
495 destroy_gc (b_dpy, b_shadowGC);
497 destroy_pixmap_mask ();
498 destroy_gc (b_dpy, b_maskGC);
500 if (b_timer) XtRemoveTimeOut (b_timer);
504 balloon_help_create (Display* dpy,
505 Pixel fg, Pixel bg, Pixel shine, Pixel shadow,
508 if (b_dpy) balloon_help_destroy ();
514 b_win = create_window (dpy, bg);
515 b_gc = create_gc (dpy, b_win, fg, bg, b_fontStruct);
517 b_shineGC = create_gc (dpy, b_win, shine, bg, b_fontStruct);
518 b_shadowGC = create_gc (dpy, b_win, shadow, bg, b_fontStruct);
520 create_pixmap_mask (1, 1);
521 b_maskGC = create_gc (dpy, b_mask, bg, fg, b_fontStruct);
527 b_screenWidth = DisplayWidth (b_dpy, DefaultScreen(b_dpy));
528 b_screenHeight = DisplayHeight (b_dpy, DefaultScreen(b_dpy));
530 b_lastShape = SHAPE_CONE_FREE;
534 balloon_help_set_delay (unsigned long milliseconds)
536 b_delay = milliseconds;
540 balloon_help_show (CONST char* text)
542 assert (b_dpy != NULL);
544 /* We don't copy the text */
546 b_lastShape = SHAPE_CONE_FREE;
550 /* If help is already being shown, don't delay just update */
551 show_help (NULL, NULL);
556 XtAppAddTimeOut (XtDisplayToApplicationContext(b_dpy),
557 b_delay, show_help, NULL);
562 balloon_help_hide (void)
564 assert (b_dpy != NULL);
567 XUnmapWindow (b_dpy, b_win);
571 XtRemoveTimeOut (b_timer);
577 balloon_help_move_to_pointer (void)
579 assert (b_dpy != NULL);
584 int shape = b_lastShape;
586 get_pointer_xy (b_dpy, &x, &y);
588 x += (shape & SHAPE_CONE_LEFT) ? POINTER_OFFSET : -POINTER_OFFSET;
589 y += (shape & SHAPE_CONE_TOP) ? POINTER_OFFSET : -POINTER_OFFSET;
591 shape = get_shape (shape, x, y, b_width, b_height, b_screenWidth, b_screenHeight);
593 if (shape == b_lastShape)
595 XMoveWindow (b_dpy, b_win,
596 shape & SHAPE_CONE_LEFT ? x : x - b_width,
597 shape & SHAPE_CONE_TOP ? y : y - b_height);
601 /* text would be off screen, rebuild with new shape */
602 b_lastShape = SHAPE_CONE_FREE;
603 show_help (NULL, NULL);