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 XtIntervalId b_timer;
90 static unsigned long b_delay;
92 static int b_screenWidth, b_screenHeight;
94 static int b_lastShape;
96 /*============================================================================
98 ============================================================================*/
101 create_gc (Display* dpy, Window win, unsigned long fg, unsigned long bg,
102 XFontStruct* fontStruct)
109 gcv.font = fontStruct->fid;
110 gcv.join_style = JoinMiter;
111 gcv.line_width = BORDER_WIDTH;
113 mask = GCFont | GCBackground | GCForeground | GCJoinStyle | GCLineWidth;
115 return XCreateGC (dpy, win, mask, &gcv);
119 destroy_gc (Display* dpy, GC gc)
127 /*============================================================================
129 ============================================================================*/
132 create_window (Display* dpy, unsigned long bg)
135 XSetWindowAttributes attr;
136 unsigned long attr_mask;
138 attr_mask = CWOverrideRedirect | CWBackPixel | CWSaveUnder;
139 attr.override_redirect = True;
140 attr.background_pixel = bg;
141 attr.save_under = True;
145 DefaultRootWindow (dpy),
148 CopyFromParent, InputOutput, CopyFromParent,
151 XSelectInput (dpy, win,
152 SubstructureRedirectMask |
153 SubstructureNotifyMask |
161 destroy_window (Display* dpy, Window win)
165 XDestroyWindow (dpy, win);
169 /*============================================================================
171 ============================================================================*/
174 get_pointer_xy (Display* dpy, int* x_return, int* y_return)
180 XQueryPointer (dpy, RootWindow(dpy, DefaultScreen(dpy)), &dummy_win, &dummy_win,
181 x_return, y_return, &dummy, &dummy, &mask);
184 /*============================================================================
186 ============================================================================*/
189 create_pixmap_mask (int width, int height)
192 b_maskHeight = height;
193 b_mask = XCreatePixmap (b_dpy, b_win, width, height, 1);
197 destroy_pixmap_mask(void)
199 XFreePixmap (b_dpy, b_mask);
203 grow_pixmap_mask (int width, int height)
205 if (width > b_maskWidth || height > b_maskHeight)
207 destroy_pixmap_mask ();
208 create_pixmap_mask (width, height);
212 /*============================================================================
214 ============================================================================*/
217 text_extent (XFontStruct* fontStruct, const char* text, int len,
218 int* width, int* height)
223 XTextExtents (fontStruct, text, len, &dummy, &dummy, &dummy, &extent);
225 *width = extent.width;
226 *height = fontStruct->ascent + fontStruct->descent;
230 get_text_size (Display* dpy, XFontStruct* fontStruct, const char* text,
231 int* max_width, int* max_height)
238 *max_width = *max_height = 0;
241 while ((end = strchr(start, '\n')))
243 text_extent (fontStruct, start, end - start, &width, &height);
244 *max_width = max (width, *max_width);
245 *max_height += height;
249 text_extent (fontStruct, start, strlen (start), &width, &height);
250 *max_width = max (width, *max_width);
251 *max_height += height;
254 *max_width = max (*max_width, CONE_WIDTH / 2 * 3);
259 draw_text (Display* dpy, Window win, GC gc, XFontStruct* fontStruct,
260 int x, int y, const char* text)
266 y += fontStruct->ascent;
268 font_height = fontStruct->ascent + fontStruct->descent;
271 while ((end = strchr(start, '\n')))
273 XDrawString (dpy, win, gc, x, y, start, end - start);
278 XDrawString (dpy, win, gc, x, y, start, strlen (start));
281 /*============================================================================
283 ============================================================================*/
286 get_shape (int last_shape, int x, int y, int width, int height,
287 int screen_width, int screen_height)
289 /* Can we use last_shape? */
290 if (((last_shape == SHAPE_CONE_TOP_LEFT) &&
291 (x + width < screen_width) && (y + height < screen_height)) ||
292 ((last_shape == SHAPE_CONE_TOP_RIGHT) &&
293 (x - width > 0) && (y + height < screen_height)) ||
294 ((last_shape == SHAPE_CONE_BOTTOM_LEFT) &&
295 (x + width < screen_width) && (y - height > 0)) ||
296 ((last_shape == SHAPE_CONE_BOTTOM_RIGHT) &&
297 (x - width > 0) && (y - height > 0)))
300 /* Try to pick a shape that will not get changed,
301 e.g. if top left quadrant, top_left */
302 return (x < screen_width / 2) ?
303 (y < screen_height / 2 ? SHAPE_CONE_TOP_LEFT: SHAPE_CONE_BOTTOM_LEFT) :
304 (y < screen_height / 2 ? SHAPE_CONE_TOP_RIGHT: SHAPE_CONE_BOTTOM_RIGHT);
308 make_mask (int shape, int x, int y, int width, int height)
312 grow_pixmap_mask (width, height);
315 XSetForeground (b_dpy, b_maskGC, 0);
316 XFillRectangle (b_dpy, b_mask, b_maskGC,
317 0, 0, width, height);
319 /* Enable text area */
320 XSetForeground (b_dpy, b_maskGC, 1);
321 XFillRectangle (b_dpy, b_mask, b_maskGC, 0,
322 shape & SHAPE_CONE_TOP ? CONE_HEIGHT : 0, width, height - CONE_HEIGHT);
324 /* Enable for cone area */
325 cone[0].x = (shape & SHAPE_CONE_LEFT) ? CONE_WIDTH / 2 : width - (CONE_WIDTH / 2);
326 cone[0].y = (shape & SHAPE_CONE_TOP) ? CONE_HEIGHT : height - CONE_HEIGHT;
327 cone[1].x = (shape & SHAPE_CONE_LEFT) ? 0 : width;
328 cone[1].y = (shape & SHAPE_CONE_TOP) ? 0 : height;
329 cone[2].x = (shape & SHAPE_CONE_LEFT) ? CONE_WIDTH : width - CONE_WIDTH;
330 cone[2].y = (shape & SHAPE_CONE_TOP) ? CONE_HEIGHT : height - CONE_HEIGHT;
332 XFillPolygon (b_dpy, b_mask, b_maskGC, cone, 3, Nonconvex, CoordModeOrigin);
337 show_help (XtPointer data, XtIntervalId* id)
343 if (id == NULL || ((id && b_timer) && b_text))
348 get_text_size (b_dpy, b_fontStruct, b_text, &b_width, &b_height);
349 b_width += 2 * MARGIN_WIDTH + 2 * BORDER_WIDTH;
350 b_height += 2 * MARGIN_WIDTH + 2 * BORDER_WIDTH + CONE_HEIGHT;
353 get_pointer_xy (b_dpy, &x, &y);
356 shape = get_shape(b_lastShape, x, y, b_width, b_height,
357 b_screenWidth, b_screenHeight);
359 x += (shape & SHAPE_CONE_LEFT) ? POINTER_OFFSET : -POINTER_OFFSET;
360 y += (shape & SHAPE_CONE_TOP) ? POINTER_OFFSET : -POINTER_OFFSET;
362 /* make sure it is still ok with offset */
363 shape = get_shape (shape, x, y, b_width, b_height, b_screenWidth, b_screenHeight);
367 make_mask (shape, x, y, b_width, b_height);
369 XShapeCombineMask (b_dpy, b_win, ShapeBounding, 0, 0, b_mask, ShapeSet);
371 XMoveResizeWindow(b_dpy, b_win,
372 (shape & SHAPE_CONE_LEFT) ? x : x - b_width,
373 (shape & SHAPE_CONE_TOP) ? y : y - b_height,
376 XClearWindow (b_dpy, b_win);
378 XMapRaised (b_dpy, b_win);
381 draw_text (b_dpy, b_win, b_gc, b_fontStruct,
382 BORDER_WIDTH + MARGIN_WIDTH,
383 BORDER_WIDTH + MARGIN_WIDTH + ((shape & SHAPE_CONE_TOP) ? CONE_HEIGHT : 0),
387 /* shine- top left */
388 border[0].x = 0 + BORDER_WIDTH_HALF;
389 border[0].y = ((shape & SHAPE_CONE_TOP) ? b_height : b_height - CONE_HEIGHT) - BORDER_WIDTH_HALF;
390 border[1].x = 0 + BORDER_WIDTH_HALF;
391 border[1].y = ((shape & SHAPE_CONE_TOP) ? CONE_HEIGHT : 0) + BORDER_WIDTH_HALF;
392 border[2].x = b_width - BORDER_WIDTH_HALF;
393 border[2].y = border[1].y;
394 XDrawLines (b_dpy, b_win, b_shineGC, border, 3, CoordModeOrigin);
396 /* shadow- bottom right */
397 border[0].x = 0 + BORDER_WIDTH_HALF;
398 border[0].y = ((shape & SHAPE_CONE_TOP) ? b_height : b_height - CONE_HEIGHT) - BORDER_WIDTH_HALF;
399 border[1].x = b_width - BORDER_WIDTH_HALF;
400 border[1].y = border[0].y;
401 border[2].x = b_width - BORDER_WIDTH_HALF;
402 border[2].y = ((shape & SHAPE_CONE_TOP) ? CONE_HEIGHT : 0) + BORDER_WIDTH_HALF;
403 XDrawLines (b_dpy, b_win, b_shadowGC, border, 3, CoordModeOrigin);
406 if (SHAPE_CONE_TOP_LEFT == shape)
408 XClearArea (b_dpy, b_win,
409 CONE_WIDTH / 2 + BORDER_WIDTH,
411 CONE_WIDTH / 2 - BORDER_WIDTH,
412 BORDER_WIDTH, False);
413 XDrawLine (b_dpy, b_win, b_shadowGC,
416 CONE_WIDTH / 2 + BORDER_WIDTH_HALF,
418 XDrawLine (b_dpy, b_win, b_shineGC,
421 CONE_WIDTH - BORDER_WIDTH_HALF,
424 else if (SHAPE_CONE_TOP_RIGHT == shape)
426 XClearArea (b_dpy, b_win,
427 b_width - CONE_WIDTH + BORDER_WIDTH,
429 CONE_WIDTH / 2 - BORDER_WIDTH,
430 BORDER_WIDTH, False);
431 XDrawLine (b_dpy, b_win, b_shadowGC,
434 b_width - CONE_WIDTH / 2 - BORDER_WIDTH_HALF,
436 XDrawLine (b_dpy, b_win, b_shineGC,
439 b_width - CONE_WIDTH + BORDER_WIDTH_HALF,
442 else if (SHAPE_CONE_BOTTOM_LEFT == shape)
444 XClearArea (b_dpy, b_win,
445 CONE_WIDTH / 2 + BORDER_WIDTH,
446 b_height - CONE_HEIGHT - BORDER_WIDTH,
447 CONE_WIDTH / 2 - BORDER_WIDTH,
448 BORDER_WIDTH, False);
449 XDrawLine (b_dpy, b_win, b_shadowGC,
453 b_height - 1 - CONE_HEIGHT);
454 XDrawLine (b_dpy, b_win, b_shineGC,
457 CONE_WIDTH / 2 + BORDER_WIDTH,
458 b_height - 1 - CONE_HEIGHT);
460 else if (SHAPE_CONE_BOTTOM_RIGHT == shape)
462 XClearArea (b_dpy, b_win,
463 b_width - 1 - CONE_WIDTH + BORDER_WIDTH,
464 b_height - CONE_HEIGHT - BORDER_WIDTH,
465 CONE_WIDTH / 2 - BORDER_WIDTH - 1,
466 BORDER_WIDTH, False);
467 XDrawLine (b_dpy, b_win, b_shadowGC,
470 b_width - 1 - CONE_WIDTH,
471 b_height - 1 - CONE_HEIGHT);
472 XDrawLine (b_dpy, b_win, b_shineGC,
475 b_width - 1 - CONE_WIDTH / 2 - BORDER_WIDTH,
476 b_height - 1 - CONE_HEIGHT);
482 /*============================================================================
484 ============================================================================*/
487 balloon_help_destroy (void)
489 assert (b_dpy != NULL);
492 destroy_window (b_dpy, b_win);
493 destroy_gc (b_dpy, b_gc);
495 destroy_gc (b_dpy, b_shineGC);
496 destroy_gc (b_dpy, b_shadowGC);
498 destroy_pixmap_mask ();
499 destroy_gc (b_dpy, b_maskGC);
501 if (b_timer) XtRemoveTimeOut (b_timer);
505 balloon_help_create (Display* dpy,
506 Pixel fg, Pixel bg, Pixel shine, Pixel shadow,
509 if (b_dpy) balloon_help_destroy ();
515 b_win = create_window (dpy, bg);
516 b_gc = create_gc (dpy, b_win, fg, bg, b_fontStruct);
518 b_shineGC = create_gc (dpy, b_win, shine, bg, b_fontStruct);
519 b_shadowGC = create_gc (dpy, b_win, shadow, bg, b_fontStruct);
521 create_pixmap_mask (1, 1);
522 b_maskGC = create_gc (dpy, b_mask, bg, fg, b_fontStruct);
528 b_screenWidth = DisplayWidth (b_dpy, DefaultScreen(b_dpy));
529 b_screenHeight = DisplayHeight (b_dpy, DefaultScreen(b_dpy));
531 b_lastShape = SHAPE_CONE_FREE;
535 balloon_help_set_delay (unsigned long milliseconds)
537 b_delay = milliseconds;
541 balloon_help_show (const char* text)
543 assert (b_dpy != NULL);
545 /* We don't copy the text */
547 b_lastShape = SHAPE_CONE_FREE;
551 /* If help is already being shown, don't delay just update */
552 show_help (NULL, NULL);
557 XtAppAddTimeOut (XtDisplayToApplicationContext(b_dpy),
558 b_delay, show_help, NULL);
563 balloon_help_hide (void)
565 assert (b_dpy != NULL);
568 XUnmapWindow (b_dpy, b_win);
572 XtRemoveTimeOut (b_timer);
578 balloon_help_move_to_pointer (void)
580 assert (b_dpy != NULL);
585 int shape = b_lastShape;
587 get_pointer_xy (b_dpy, &x, &y);
589 x += (shape & SHAPE_CONE_LEFT) ? POINTER_OFFSET : -POINTER_OFFSET;
590 y += (shape & SHAPE_CONE_TOP) ? POINTER_OFFSET : -POINTER_OFFSET;
592 shape = get_shape (shape, x, y, b_width, b_height, b_screenWidth, b_screenHeight);
594 if (shape == b_lastShape)
596 XMoveWindow (b_dpy, b_win,
597 shape & SHAPE_CONE_LEFT ? x : x - b_width,
598 shape & SHAPE_CONE_TOP ? y : y - b_height);
602 /* text would be off screen, rebuild with new shape */
603 b_lastShape = SHAPE_CONE_FREE;
604 show_help (NULL, NULL);