Debian Codegraph @

src/Tip.c

raw
1 /*
2 * Copyright (c) 1999 by The XFree86 Project, Inc.
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
13 *
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
17 * THE XFREE86 PROJECT BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
18 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF
19 * OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20 * SOFTWARE.
21 *
22 * Except as contained in this notice, the name of the XFree86 Project shall
23 * not be used in advertising or otherwise to promote the sale, use or other
24 * dealings in this Software without prior written authorization from the
25 * XFree86 Project.
26 *
27 * Author: Paulo César Pereira de Andrade
28 */
29
30 #ifdef HAVE_CONFIG_H
31 #include <config.h>
32 #endif
33 #include <X11/IntrinsicP.h>
34 #include <X11/StringDefs.h>
35 #include <X11/Xos.h>
36 #include <X11/Xaw/TipP.h>
37 #include <X11/Xaw/XawInit.h>
38 #include <X11/Xmu/Converters.h>
39 #include "Private.h"
40
41 #define TIP_EVENT_MASK (ButtonPressMask | \
42 ButtonReleaseMask | \
43 PointerMotionMask | \
44 ButtonMotionMask | \
45 KeyPressMask | \
46 KeyReleaseMask | \
47 EnterWindowMask | \
48 LeaveWindowMask)
49
50 /*
51 * Types
52 */
53 typedef struct _XawTipInfo {
54 Screen *screen;
55 TipWidget tip;
56 Widget widget;
57 Bool mapped;
58 struct _XawTipInfo *next;
59 } XawTipInfo;
60
61 /*
62 * Class Methods
63 */
64 static void XawTipClassInitialize(void);
65 static void XawTipInitialize(Widget, Widget, ArgList, Cardinal*);
66 static void XawTipDestroy(Widget);
67 static void XawTipExpose(Widget, XEvent*, Region);
68 static void XawTipRealize(Widget, Mask*, XSetWindowAttributes*);
69 static Boolean XawTipSetValues(Widget, Widget, Widget, ArgList, Cardinal*);
70
71 /*
72 * Prototypes
73 */
74 static void TipEventHandler(Widget, XtPointer, XEvent*, Boolean*);
75 static void TipShellEventHandler(Widget, XtPointer, XEvent*, Boolean*);
76 static XawTipInfo *CreateTipInfo(Widget);
77 static XawTipInfo *FindTipInfo(Widget);
78 static void ResetTip(XawTipInfo*, Bool);
79 static void TipTimeoutCallback(XtPointer, XtIntervalId*);
80 static void TipLayout(XawTipInfo*);
81 static void TipPosition(XawTipInfo*);
82
83 /*
84 * Initialization
85 */
86 #define offset(field) XtOffsetOf(TipRec, tip.field)
87 static XtResource resources[] = {
88 {
89 XtNforeground,
90 XtCForeground,
91 XtRPixel,
92 sizeof(Pixel),
93 offset(foreground),
94 XtRString,
95 (XtPointer)XtDefaultForeground,
96 },
97 {
98 XtNfont,
99 XtCFont,
100 XtRFontStruct,
101 sizeof(XFontStruct*),
102 offset(font),
103 XtRString,
104 (XtPointer)XtDefaultFont
105 },
106 {
107 XtNfontSet,
108 XtCFontSet,
109 XtRFontSet,
110 sizeof(XFontSet),
111 offset(fontset),
112 XtRString,
113 (XtPointer)XtDefaultFontSet
114 },
115 {
116 XtNtopMargin,
117 XtCVerticalMargins,
118 XtRDimension,
119 sizeof(Dimension),
120 offset(top_margin),
121 XtRImmediate,
122 (XtPointer)2
123 },
124 {
125 XtNbottomMargin,
126 XtCVerticalMargins,
127 XtRDimension,
128 sizeof(Dimension),
129 offset(bottom_margin),
130 XtRImmediate,
131 (XtPointer)2
132 },
133 {
134 XtNleftMargin,
135 XtCHorizontalMargins,
136 XtRDimension,
137 sizeof(Dimension),
138 offset(left_margin),
139 XtRImmediate,
140 (XtPointer)6
141 },
142 {
143 XtNrightMargin,
144 XtCHorizontalMargins,
145 XtRDimension,
146 sizeof(Dimension),
147 offset(right_margin),
148 XtRImmediate,
149 (XtPointer)6
150 },
151 {
152 XtNbackingStore,
153 XtCBackingStore,
154 XtRBackingStore,
155 sizeof(int),
156 offset(backing_store),
157 XtRImmediate,
158 (XtPointer)(Always + WhenMapped + NotUseful)
159 },
160 {
161 XtNtimeout,
162 XtCTimeout,
163 XtRInt,
164 sizeof(int),
165 offset(timeout),
166 XtRImmediate,
167 (XtPointer)500
168 },
169 {
170 XawNdisplayList,
171 XawCDisplayList,
172 XawRDisplayList,
173 sizeof(XawDisplayList*),
174 offset(display_list),
175 XtRImmediate,
176 NULL
177 },
178 };
179 #undef offset
180
181 TipClassRec tipClassRec = {
182 /* core */
183 {
184 (WidgetClass)&widgetClassRec, /* superclass */
185 "Tip", /* class_name */
186 sizeof(TipRec), /* widget_size */
187 XawTipClassInitialize, /* class_initialize */
188 NULL, /* class_part_initialize */
189 False, /* class_inited */
190 XawTipInitialize, /* initialize */
191 NULL, /* initialize_hook */
192 XawTipRealize, /* realize */
193 NULL, /* actions */
194 0, /* num_actions */
195 resources, /* resources */
196 XtNumber(resources), /* num_resources */
197 NULLQUARK, /* xrm_class */
198 True, /* compress_motion */
199 True, /* compress_exposure */
200 True, /* compress_enterleave */
201 False, /* visible_interest */
202 XawTipDestroy, /* destroy */
203 NULL, /* resize */
204 XawTipExpose, /* expose */
205 XawTipSetValues, /* set_values */
206 NULL, /* set_values_hook */
207 XtInheritSetValuesAlmost, /* set_values_almost */
208 NULL, /* get_values_hook */
209 NULL, /* accept_focus */
210 XtVersion, /* version */
211 NULL, /* callback_private */
212 NULL, /* tm_table */
213 XtInheritQueryGeometry, /* query_geometry */
214 XtInheritDisplayAccelerator, /* display_accelerator */
215 NULL, /* extension */
216 },
217 /* tip */
218 {
219 NULL, /* extension */
220 },
221 };
222
223 WidgetClass tipWidgetClass = (WidgetClass)&tipClassRec;
224
225 static XawTipInfo *first_tip;
226
227 /*
228 * Implementation
229 */
230 static void
231 XawTipClassInitialize(void)
232 {
233 XawInitializeWidgetSet();
234 XtAddConverter(XtRString, XtRBackingStore, XmuCvtStringToBackingStore,
235 NULL, 0);
236 XtSetTypeConverter(XtRBackingStore, XtRString, XmuCvtBackingStoreToString,
237 NULL, 0, XtCacheNone, NULL);
238 }
239
240 /*ARGSUSED*/
241 static void
242 XawTipInitialize(Widget req _X_UNUSED, Widget w _X_UNUSED, ArgList args _X_UNUSED, Cardinal *num_args _X_UNUSED)
243 {
244 TipWidget tip = (TipWidget)w;
245 XGCValues values;
246
247 if (!tip->tip.font) XtError("Aborting: no font found\n");
248 if (tip->tip.international && !tip->tip.fontset)
249 XtError("Aborting: no fontset found\n");
250
251 tip->tip.timer = 0;
252
253 values.foreground = tip->tip.foreground;
254 values.background = tip->core.background_pixel;
255 values.font = tip->tip.font->fid;
256 values.graphics_exposures = False;
257
258 tip->tip.gc = XtAllocateGC(w, 0, GCForeground | GCBackground | GCFont |
259 GCGraphicsExposures, &values, GCFont, 0);
260 }
261
262 static void
263 XawTipDestroy(Widget w)
264 {
265 XawTipInfo *info = FindTipInfo(w);
266 TipWidget tip = (TipWidget)w;
267
268 if (tip->tip.timer)
269 XtRemoveTimeOut(tip->tip.timer);
270
271 XtReleaseGC(w, tip->tip.gc);
272
273 XtRemoveEventHandler(XtParent(w), KeyPressMask, False, TipShellEventHandler,
274 (XtPointer)NULL);
275 if (info == first_tip)
276 first_tip = first_tip->next;
277 else {
278 XawTipInfo *p = first_tip;
279
280 while (p && p->next != info)
281 p = p->next;
282 if (p)
283 p->next = info->next;
284 }
285 XtFree((char*)info);
286 }
287
288 static void
289 XawTipRealize(Widget w, Mask *mask, XSetWindowAttributes *attr)
290 {
291 TipWidget tip = (TipWidget)w;
292
293 if (tip->tip.backing_store == Always ||
294 tip->tip.backing_store == NotUseful ||
295 tip->tip.backing_store == WhenMapped) {
296 *mask |= CWBackingStore;
297 attr->backing_store = tip->tip.backing_store;
298 }
299 else
300 *mask &= (Mask)(~CWBackingStore);
301 *mask |= CWOverrideRedirect;
302 attr->override_redirect = True;
303
304 XtWindow(w) = XCreateWindow(DisplayOfScreen(XtScreen(w)),
305 RootWindowOfScreen(XtScreen(w)),
306 XtX(w), XtY(w),
307 XtWidth(w) ? XtWidth(w) : 1,
308 XtHeight(w) ? XtHeight(w) : 1,
309 XtBorderWidth(w),
310 DefaultDepthOfScreen(XtScreen(w)),
311 InputOutput,
312 (Visual *)CopyFromParent,
313 *mask, attr);
314 }
315
316 static void
317 XawTipExpose(Widget w, XEvent *event, Region region)
318 {
319 TipWidget tip = (TipWidget)w;
320 GC gc = tip->tip.gc;
321 char *nl;
322 _Xconst char * label = tip->tip.label;
323 Position y = (Position)(tip->tip.top_margin + tip->tip.font->max_bounds.ascent);
324 int len;
325
326 if (tip->tip.display_list)
327 XawRunDisplayList(w, tip->tip.display_list, event, region);
328
329 if (tip->tip.international == True) {
330 Position ksy = (Position)tip->tip.top_margin;
331 XFontSetExtents *ext = XExtentsOfFontSet(tip->tip.fontset);
332
333 ksy = (Position) (ksy + XawAbs(ext->max_ink_extent.y));
334
335 while ((nl = strchr(label, '\n')) != NULL) {
336 XmbDrawString(XtDisplay(w), XtWindow(w), tip->tip.fontset,
337 gc, tip->tip.left_margin, ksy, label,
338 (int)(nl - label));
339 ksy = (Position) (ksy + ext->max_ink_extent.height);
340 label = nl + 1;
341 }
342 len = (int)strlen(label);
343 if (len)
344 XmbDrawString(XtDisplay(w), XtWindow(w), tip->tip.fontset, gc,
345 tip->tip.left_margin, ksy, label, len);
346 }
347 else {
348 while ((nl = strchr(label, '\n')) != NULL) {
349 if (tip->tip.encoding)
350 XDrawString16(XtDisplay(w), XtWindow(w), gc,
351 tip->tip.left_margin, y,
352 (_Xconst XChar2b*)label, (int)(nl - label) >> 1);
353 else
354 XDrawString(XtDisplay(w), XtWindow(w), gc,
355 tip->tip.left_margin, y, label, (int)(nl - label));
356 y = (Position)(y + (tip->tip.font->max_bounds.ascent +
357 tip->tip.font->max_bounds.descent));
358 label = nl + 1;
359 }
360 len = (int)strlen(label);
361 if (len) {
362 if (tip->tip.encoding)
363 XDrawString16(XtDisplay(w), XtWindow(w), gc,
364 tip->tip.left_margin, y, (_Xconst XChar2b*)label, len >> 1);
365 else
366 XDrawString(XtDisplay(w), XtWindow(w), gc,
367 tip->tip.left_margin, y, label, len);
368 }
369 }
370 }
371
372 /*ARGSUSED*/
373 static Boolean
374 XawTipSetValues(Widget current, Widget request _X_UNUSED, Widget cnew,
375 ArgList args _X_UNUSED, Cardinal *num_args _X_UNUSED)
376 {
377 TipWidget curtip = (TipWidget)current;
378 TipWidget newtip = (TipWidget)cnew;
379 Boolean redisplay = False;
380
381 if (curtip->tip.font->fid != newtip->tip.font->fid ||
382 curtip->tip.foreground != newtip->tip.foreground) {
383 XGCValues values = {
384 .foreground = newtip->tip.foreground,
385 .background = newtip->core.background_pixel,
386 .font = newtip->tip.font->fid,
387 .graphics_exposures = False
388 };
389
390 XtReleaseGC(cnew, curtip->tip.gc);
391 newtip->tip.gc = XtAllocateGC(cnew, 0, GCForeground | GCBackground |
392 GCFont | GCGraphicsExposures, &values,
393 GCFont, 0);
394 redisplay = True;
395 }
396 if (curtip->tip.display_list != newtip->tip.display_list)
397 redisplay = True;
398
399 return (redisplay);
400 }
401
402 static void
403 TipLayout(XawTipInfo *info)
404 {
405 XFontStruct *fs = info->tip->tip.font;
406 int width = 0, height;
407 char *nl;
408 _Xconst char *label = info->tip->tip.label;
409
410 if (info->tip->tip.international == True) {
411 XFontSet fset = info->tip->tip.fontset;
412 XFontSetExtents *ext = XExtentsOfFontSet(fset);
413
414 height = ext->max_ink_extent.height;
415 if ((nl = strchr(label, '\n')) != NULL) {
416 /*CONSTCOND*/
417 while (True) {
418 int w = XmbTextEscapement(fset, label, (int)(nl - label));
419
420 if (w > width)
421 width = w;
422 if (*nl == '\0')
423 break;
424 label = nl + 1;
425 if (*label)
426 height += ext->max_ink_extent.height;
427 if ((nl = strchr(label, '\n')) == NULL)
428 nl = strchr(label, '\0');
429 }
430 }
431 else
432 width = XmbTextEscapement(fset, label, (int)strlen(label));
433 }
434 else {
435 height = fs->max_bounds.ascent + fs->max_bounds.descent;
436 if ((nl = strchr(label, '\n')) != NULL) {
437 /*CONSTCOND*/
438 while (True) {
439 int w = info->tip->tip.encoding ?
440 XTextWidth16(fs, (_Xconst XChar2b*)label, (int)(nl - label) >> 1) :
441 XTextWidth(fs, label, (int)(nl - label));
442 if (w > width)
443 width = w;
444 if (*nl == '\0')
445 break;
446 label = nl + 1;
447 if (*label)
448 height += fs->max_bounds.ascent + fs->max_bounds.descent;
449 if ((nl = strchr(label, '\n')) == NULL)
450 nl = strchr(label, '\0');
451 }
452 }
453 else
454 width = info->tip->tip.encoding ?
455 XTextWidth16(fs, (_Xconst XChar2b*)label, (int)(strlen(label) >> 1)) :
456 XTextWidth(fs, label, (int)strlen(label));
457 }
458 XtWidth(info->tip) = (Dimension) (width + info->tip->tip.left_margin +
459 info->tip->tip.right_margin);
460 XtHeight(info->tip) = (Dimension) (height + info->tip->tip.top_margin +
461 info->tip->tip.bottom_margin);
462 }
463
464 #define DEFAULT_TIP_Y_OFFSET 12
465 static void
466 TipPosition(XawTipInfo *info)
467 {
468 Window r, c;
469 int rx, ry, wx, wy;
470 unsigned mask;
471 Position x, y;
472
473 XQueryPointer(XtDisplay((Widget)info->tip), XtWindow((Widget)info->tip),
474 &r, &c, &rx, &ry, &wx, &wy, &mask);
475 x = (Position)(rx - (XtWidth(info->tip) >> 1));
476 y = (Position)(ry + DEFAULT_TIP_Y_OFFSET);
477
478 if (x >= 0) {
479 int scr_width = WidthOfScreen(XtScreen(info->tip));
480
481 if (x + XtWidth(info->tip) + XtBorderWidth(info->tip) > scr_width)
482 x = (Position)(scr_width - XtWidth(info->tip) - XtBorderWidth(info->tip));
483 }
484 if (x < 0)
485 x = 0;
486 if (y >= 0) {
487 int scr_height = HeightOfScreen(XtScreen(info->tip));
488
489 if (y + XtHeight(info->tip) + XtBorderWidth(info->tip) > scr_height)
490 y = (Position)(y - (XtHeight(info->tip) + XtBorderWidth(info->tip) +
491 (DEFAULT_TIP_Y_OFFSET << 1)));
492 }
493 if (y < 0)
494 y = 0;
495
496 XMoveResizeWindow(XtDisplay(info->tip), XtWindow(info->tip),
497 (int)(XtX(info->tip) = x), (int)(XtY(info->tip) = y),
498 (unsigned)XtWidth(info->tip), (unsigned)XtHeight(info->tip));
499 }
500
501 static XawTipInfo *
502 CreateTipInfo(Widget w)
503 {
504 XawTipInfo *info = XtNew(XawTipInfo);
505 Widget shell = w;
506
507 info->screen = XtScreen(w);
508
509 while (XtParent(shell))
510 shell = XtParent(shell);
511
512 info->tip = (TipWidget)XtCreateWidget("tip", tipWidgetClass, shell, NULL, 0);
513 XtRealizeWidget((Widget)info->tip);
514 info->widget = NULL;
515 info->mapped = False;
516 info->next = NULL;
517 XtAddEventHandler(shell, KeyPressMask, False, TipShellEventHandler,
518 (XtPointer)NULL);
519
520 return (info);
521 }
522
523 static XawTipInfo *
524 FindTipInfo(Widget w)
525 {
526 XawTipInfo *ptip, *tip = first_tip;
527 Screen *screen = XtScreenOfObject(w);
528
529 if (tip == NULL)
530 return (first_tip = CreateTipInfo(w));
531
532 for (ptip = tip; tip; ptip = tip, tip = tip->next)
533 if (tip->screen == screen)
534 return (tip);
535
536 return (ptip->next = CreateTipInfo(w));
537 }
538
539 static void
540 ResetTip(XawTipInfo *info, Bool add_timeout)
541 {
542 if (info->tip->tip.timer) {
543 XtRemoveTimeOut(info->tip->tip.timer);
544 info->tip->tip.timer = 0;
545 }
546 if (info->mapped) {
547 XtRemoveGrab(XtParent((Widget)info->tip));
548 XUnmapWindow(XtDisplay((Widget)info->tip), XtWindow((Widget)info->tip));
549 info->mapped = False;
550 }
551 if (add_timeout) {
552 info->tip->tip.timer =
553 XtAppAddTimeOut(XtWidgetToApplicationContext((Widget)info->tip),
554 (unsigned long)info->tip->tip.timeout,
555 TipTimeoutCallback,
556 (XtPointer)info);
557 }
558 }
559
560 static void
561 TipTimeoutCallback(XtPointer closure, XtIntervalId *id _X_UNUSED)
562 {
563 XawTipInfo *info = (XawTipInfo*)closure;
564 Arg args[3];
565
566 info->tip->tip.label = NULL;
567 info->tip->tip.international = False;
568 info->tip->tip.encoding = 0;
569 info->tip->tip.timer = 0;
570 XtSetArg(args[0], XtNtip, &info->tip->tip.label);
571 XtSetArg(args[1], XtNinternational, &info->tip->tip.international);
572 XtSetArg(args[2], XtNencoding, &info->tip->tip.encoding);
573 XtGetValues(info->widget, args, 3);
574
575 if (info->tip->tip.label) {
576 TipLayout(info);
577 TipPosition(info);
578 XMapRaised(XtDisplay((Widget)info->tip), XtWindow((Widget)info->tip));
579 XtAddGrab(XtParent((Widget)info->tip), True, True);
580 info->mapped = True;
581 }
582 }
583
584 /*ARGSUSED*/
585 static void
586 TipShellEventHandler(Widget w, XtPointer client_data _X_UNUSED, XEvent *event _X_UNUSED,
587 Boolean *continue_to_dispatch _X_UNUSED)
588 {
589 ResetTip(FindTipInfo(w), False);
590 }
591
592 /*ARGSUSED*/
593 static void
594 TipEventHandler(Widget w, XtPointer client_data _X_UNUSED, XEvent *event,
595 Boolean *continue_to_dispatch _X_UNUSED)
596 {
597 XawTipInfo *info = FindTipInfo(w);
598 Boolean add_timeout;
599
600 if (info->widget != w) {
601 ResetTip(info, False);
602 info->widget = w;
603 }
604
605 switch (event->type) {
606 case EnterNotify:
607 add_timeout = True;
608 break;
609 case MotionNotify:
610 /* If any button is pressed, timer is 0 */
611 if (info->mapped)
612 return;
613 add_timeout = info->tip->tip.timer != 0;
614 break;
615 default:
616 add_timeout = False;
617 break;
618 }
619 ResetTip(info, add_timeout);
620 }
621
622 /*
623 * Public routines
624 */
625 void
626 XawTipEnable(Widget w)
627 {
628 XtAddEventHandler(w, TIP_EVENT_MASK, False, TipEventHandler,
629 (XtPointer)NULL);
630 }
631
632 void
633 XawTipDisable(Widget w)
634 {
635 XawTipInfo *info = FindTipInfo(w);
636
637 XtRemoveEventHandler(w, TIP_EVENT_MASK, False, TipEventHandler,
638 (XtPointer)NULL);
639 if (info->widget == w)
640 ResetTip(info, False);
641 }