summaryrefslogtreecommitdiff
path: root/Build/source/texk/xdvik/gui/Tip.c
blob: 76a83f188b9f8efe2b4c52e1df4852dad0c6a766 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
/*
 * Copyright (c) 2001-2004 Marcin Dalecki and others
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to
 * deal in the Software without restriction, including without limitation the
 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
 * sell copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
 * PAUL VOJTA OR ANY OTHER AUTHOR OF THIS SOFTWARE BE LIABLE FOR ANY CLAIM,
 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
 * OTHER DEALINGS IN THE SOFTWARE.
 *
 */

#include "xdvi-config.h"
#include "xdvi.h"

#include "Tip.h"
#include "xdvi-debug.h"
#include "util.h"

#ifdef MOTIF /* needed for `make depend' */

#ifndef UNUSED
#define UNUSED(x) ((void)(x))
#endif

#include <signal.h>
#include <stdio.h>
#include <stdlib.h>

#include <X11/IntrinsicP.h>
#include <X11/StringDefs.h>
#include <Xm/XmP.h>

#include <Xm/PushB.h>
#include <X11/ShellP.h>

#define TIP_NUM 1024 /* FIXME: remove this hard-coded value */

typedef struct {
    int __empty;
} TipClassPart;

/* Full class record declaration */
typedef struct _TipClassRec {
    CoreClassPart core_class;
    CompositeClassPart composite_class;
    ShellClassPart shell_class;
    OverrideShellClassPart override_shell_class;
    TipClassPart tip_class;
} TipClassRec;

/* keep information about each widget we are keeping track of */
struct tip_context {
    Widget watched;		/* the widget we are watching */
    Window window;		/* Window of the object we are monitoring */
    TipWidget tw;		/* pointer back to the tip widget */
    Position abs_x, abs_y;
    Boolean active;		/* if False, tip is suppressed */
    char *text;			/* text to display */
    short size;			/* its size */
};

/* New fields for the widget record */
typedef struct {
    /* resources */
    Pixel foreground;
    XFontSet fontset;		/* the font for text in box */
    int waitPeriod;		/* the delay resource - pointer must be
				 * in watched widget this long before
				 * help is popped up - in millisecs
				 */
    unsigned int cwp;		/* after help is popped down - normal
				 * wait period is cancelled for this
				 * period - in millisecs
				 */

    /* private state */
    struct tip_context twl[TIP_NUM];	/* list of widgets we are liteClue-ing */
    Cardinal nr_twl;		/* number of widgets we have attached */
    Dimension font_width;	/* width of '1' character */
    Dimension font_height;	/* height of font, rows are spaced using this */
    Dimension font_baseline;	/* relative displacement to baseline from top */
    GC text_GC;			/* for drawing text */
    XtIntervalId tid;		/* holds timer id */
    Widget isup;		/* the help popup is up on this widget */
    Time HelpPopDownTime;	/* the time at which help popup
				   was popped down */
} TipPart;

/*
 * Full instance record declaration
 */
typedef struct _TipRec {
    CorePart core;
    CompositePart composite;
    ShellPart shell;
    OverrideShellPart override;
    TipPart tip;
} TipRec;

#define CheckWidgetClass(routine) \
	if (XtClass(w) != tipWidgetClass) \
		wrong_widget(routine)

static void initialize(Widget, Widget, ArgList, Cardinal *);
static Boolean set_values(Widget, Widget, Widget, ArgList, Cardinal *);
static void destroy(Widget);

/*
 * Widget resources: eg to set tip box background: *tipShell.background: yellow.
 */

#define offset(field) XtOffsetOf(TipRec, field)
static XtResource resources[] = {
    {XtNforeground, XtCForeground,
     XtRPixel, sizeof(Pixel), offset(tip.foreground),
     XtRString, "black"},
    {XtNfontSet, XtCFontSet,
     XtRFontSet, sizeof(XFontSet), offset(tip.fontset),
     XtRString, "fixed"},
    {XmNwaitPeriod, XmCWaitPeriod,
     XtRInt, sizeof(int), offset(tip.waitPeriod),
     XtRImmediate, (XtPointer) 800},
    {XmNcancelWaitPeriod, XmCCancelWaitPeriod,
     XtRInt, sizeof(int), offset(tip.cwp),
     XtRImmediate, (XtPointer) 250},
};
#undef offset

TipClassRec tipClassRec = {
    {
     /* superclass           */ (WidgetClass) & overrideShellClassRec,
     /* class_name           */ "Tip",
     /* widget size          */ (Cardinal) sizeof(TipRec),
     /* class_init           */ NULL,
     /* class_part_init      */ (XtWidgetClassProc) NULL,
     /* class_inited         */ (XtEnum) FALSE,
     /* initialize           */ (XtInitProc) initialize,
     /* init_hook            */ (XtArgsProc) NULL,
     /* realize              */ XtInheritRealize,
     /* actions              */ (XtActionList) 0,
     /* num_actions          */ (Cardinal) 0,
     /* resources            */ (XtResourceList) resources,
     /* num_resources        */ (Cardinal) XtNumber(resources),
     /* xrm_class            */ NULLQUARK,
     /* compress_motion      */ TRUE,
     /* compress_exposur     */ (XtEnum) FALSE,
     /* compress enterleave  */ TRUE,
     /* visibility_interest  */ FALSE,
     /* destroy              */ destroy,
     /* resize               */ XtInheritResize,
     /* expose,              */ XtInheritExpose,
     /* set_values           */ (XtSetValuesFunc) set_values,
     /* set_values_hook      */ (XtArgsFunc) NULL,
     /* set_values_almost    */ XtInheritSetValuesAlmost,
     /* get_values_hook      */ (XtArgsProc) NULL,
     /* accept_focus         */ XtInheritAcceptFocus,
     /* version              */ XtVersion,
     /* callback_private     */ (XtPointer) NULL,
     /* translations         */ XtInheritTranslations,
     /* query_geometry       */ XtInheritQueryGeometry,
     /* display_accelerator  */ XtInheritDisplayAccelerator,
     /* extension            */ (XtPointer) 0,
     },
    /* composite part */
    {
     /* geometry_manager     */ XtInheritGeometryManager,
     /* change_managed       */ XtInheritChangeManaged,
     /* insert_child         */ XtInheritInsertChild,
     /* delete_child         */ XtInheritDeleteChild,
     /* extension            */ NULL
     },
    /* Shell */
    {
     (XtPointer) NULL,
     },
    /* Override Shell */
    {
     0,
     },
    /* tip */
    {
     0,
     }
};

WidgetClass tipWidgetClass = (WidgetClass) & tipClassRec;

/*
 * The font_information is derived.
 */
static void compute_font_info(TipWidget cw)
{
    XRectangle ink;
    XRectangle logical;

    if (!cw->tip.fontset)
	return;
    XmbTextExtents(cw->tip.fontset, "1", 1, &ink, &logical);

    cw->tip.font_baseline = -logical.y;	/* y offset from top to baseline, don't
					   know why this is returned as
					   negative */

    cw->tip.font_width = logical.width;	/* the width and height of the object */

    cw->tip.font_height = logical.height;
    TRACE_GUI((stderr, "baseline: %d, width: %d, height: %d\n",
	       cw->tip.font_baseline, cw->tip.font_width, cw->tip.font_height));
}

/*
 * Creates the various graphic contexts we will need.
 */
static void create_GC(TipWidget cw)
{
    XtGCMask valuemask;
    XGCValues myXGCV;

    valuemask = GCForeground | GCBackground | GCFillStyle;
    myXGCV.foreground = cw->tip.foreground;
    myXGCV.background = cw->core.background_pixel;
    myXGCV.fill_style = FillSolid;

    if (cw->tip.text_GC)
	XtReleaseGC((Widget) cw, cw->tip.text_GC);
    cw->tip.text_GC = XtGetGC((Widget) cw, valuemask, &myXGCV);
}

/*
 * A routine to halt execution and force a core dump for debugging analysis
 * when a public routine is called with the wrong class of widget.
 */

static void wrong_widget(char *routine)
{
    XDVI_ABORT((stderr, "Wrong class of widget passed to %s", routine));
}

/*
 * Global list of shells for tips that are in use.
 */

static TipWidget *shells = NULL;
static int nr_shells = 0;

/****************************************************************************
 * Widget Methods
 */

static void initialize(Widget treq, Widget tnew, ArgList args,
		       Cardinal * nargs)
{
    TipWidget tw = (TipWidget) tnew;

    UNUSED(treq);
    UNUSED(args);
    UNUSED(nargs);
    
    tw->tip.text_GC = NULL;
    tw->tip.isup = NULL;
    tw->tip.HelpPopDownTime = 0;
    tw->tip.tid = (XtIntervalId) 0;
    tw->tip.nr_twl = 0;
    compute_font_info(tw);
    create_GC(tw);

    /* Add to our list of tip shells.
     */
    if (!shells)
	shells = (TipWidget *)XtMalloc(sizeof(TipWidget));
    else
	shells = (TipWidget *)XtRealloc((char *)shells,
					sizeof(TipWidget) * (nr_shells + 1));

    shells[nr_shells++] = tw;
}

static Boolean set_values(Widget _current, Widget _request, Widget _new,
			  ArgList args, Cardinal * nargs)
{
    TipWidget cw_new = (TipWidget) _new;
    TipWidget cw_cur = (TipWidget) _current;

    UNUSED(_request);
    UNUSED(args);
    UNUSED(nargs);
    
    /* values of cw_new->tip.cwp and
       cw_new->tip.waitPeriod are accepted without checking */

    if (cw_new->tip.foreground != cw_cur->tip.foreground
	|| cw_new->core.background_pixel !=
	cw_cur->core.background_pixel) {
	create_GC(cw_new);
    }
    return FALSE;
}

static void destroy(Widget w)
{
    TipWidget tw = (TipWidget) w;
    int i;
    Boolean copy = False;

    /* Remove this tip shell from our global list.
     */
    for (i = 0; i < nr_shells; ++i) {
	if (shells[i] == tw) {
	    copy = True;
	    --nr_shells;
	}
	if (copy && nr_shells)
	    shells[i] = shells[i + 1];
    }
    if (!nr_shells) {
	XtFree((char *) shells);
	shells = NULL;
    }
}

/****************************************************************************
 * Event handlers
 */

/* callback to popup the tip window
 */
static void timeout_event(XtPointer client_data, XtIntervalId *id)
{
#define HBorderPix 3
#define VBorderPix 3
    struct tip_context *obj = (struct tip_context *) client_data;
    TipWidget tw = obj->tw;
    Position abs_x, abs_y;
    int ptr_x, ptr_y;

    XRectangle ink;
    XRectangle logical;
    Position w_height, w_width;
    Widget w;
    
    UNUSED(id);

    TRACE_GUI((stderr, "timeout called!"));
    
    if (tw->tip.tid == (XtIntervalId) 0)
	return;			/* timeout was removed but callback happened
				   anyway */

    tw->tip.tid = (XtIntervalId) 0;
    if (obj->active == False)
	return;

    w = obj->watched;

    if (!XtIsManaged(w))
	return;

    { /* perform additional check that pointer is really still over the widget;
	 else, tooltips will sometimes pop up if window had received an Enter
	 event before (for some reason, not all Enters are followed by Leaves).
	 This is especially apparent when running xdvi from a remote display over
	 a slow connection.
       */
	Window root, child;
	int root_x, root_y;
	unsigned int keys_buttons;
	if (!XQueryPointer(DISP, RootWindowOfScreen(SCRN), &root, &child,
			   &root_x, &root_y, &ptr_x, &ptr_y, &keys_buttons))
	    return;

	TRACE_GUI((stderr, "Pointerlocate: %d, %d", root_x, root_y));
	
	XtVaGetValues(w, XtNheight, &w_height, XtNwidth, &w_width, NULL);
	XtTranslateCoords(w, 0, 0, &abs_x, &abs_y);
	
	TRACE_GUI((stderr, "Window: %d,%d - %d,%d",
		   abs_x, abs_y, abs_x + w_width, abs_y + w_height));
	
	if (root_x < abs_x || root_x > abs_x + w_width
	    || root_y < abs_y || root_y > abs_y + w_height) {
	    TRACE_GUI((stderr, "not really over toolbutton - returning!"));
	    return;
	}
    }

    /* position just below the pointer
     * (NOT the widget, in case the widget is large!)
     */
    ptr_y += 20;
/*     abs_x += w_width / 2; */
/*     abs_y += w_height; */
    
    XmbTextExtents(tw->tip.fontset, obj->text, obj->size, &ink, &logical);

    XtRealizeWidget((Widget)tw); /* so that setting the size etc. works */
	
    XtResizeWidget((Widget) tw,
		   2 * HBorderPix + logical.width,
		   2 * VBorderPix + tw->tip.font_height,
		   tw->core.border_width);
    TRACE_GUI((stderr, "Popup size: %d x %d (hborder: %d, vborder: %d)\n",
	       2 * HBorderPix + logical.width, 2 * VBorderPix + tw->tip.font_height,
	       HBorderPix, VBorderPix));
    XtMoveWidget((Widget)tw, ptr_x, ptr_y);

    XtPopup((Widget) tw, XtGrabNone);
    tw->tip.isup = obj->watched;

    XmbDrawImageString(XtDisplay((Widget) tw),
		       XtWindow((Widget) tw),
		       tw->tip.fontset,
		       tw->tip.text_GC,
		       HBorderPix,
		       VBorderPix + tw->tip.font_baseline,
		       obj->text, obj->size);
}

/*
 * Pointer enters watched widget, set a timer to popup the help.
 */
static void enter(struct tip_context *obj, XEvent * xevent,
		  XtAppContext app)
{
    TipWidget tw = obj->tw;
    XEnterWindowEvent *event = &xevent->xcrossing;
    int current_waitPeriod;

    /* this doesn't help against the Enter/Leave problem mentioned above,
       so it's not related to Widget creation ... */
    if (!XtIsManaged(obj->watched)) {
	TRACE_GUI((stderr, "%s:%d: Not yet managed!", __FILE__, __LINE__));
	return;
    }
    
    TRACE_GUI((stderr, "%s:%d: Enter!", __FILE__, __LINE__));
    
    if (obj->active == False)
	return;

    /* check for two enters in a row - happens when widget is
       exposed under a pop-up */
    if (tw->tip.tid != (XtIntervalId) 0)
	return;

    if (event->mode != NotifyNormal)
	return;

    /* it seems that this makes the tooltips somewhat unpredictable (they
       don't show when hovering fast over several buttons, then staying on
       one button); disabled this for the time being. */
/*     if ((event->time - tw->tip.HelpPopDownTime) > tw->tip.cwp) */
/* 	current_waitPeriod = tw->tip.waitPeriod; */
/*     else */
/* 	current_waitPeriod = 0; */

/*     current_waitPeriod = tw->tip.waitPeriod; */
    current_waitPeriod =  resource.tooltips_wait_period;
    if (current_waitPeriod >= 0) {
	tw->tip.tid = XtAppAddTimeOut(app, current_waitPeriod, timeout_event,
				      (XtPointer) obj);
    }
}

/*
 * Remove timer if its pending. Then popdown help.
 */
static void leave(struct tip_context *obj, XEvent * xevent)
{
    TipWidget tw = obj->tw;
    XEnterWindowEvent *event = &xevent->xcrossing;

    TRACE_GUI((stderr, "%s:%d: Leave!", __FILE__, __LINE__));
    
    if (tw->tip.tid != (XtIntervalId) 0) {
	if (globals.debug & DBG_EVENT)
	    fprintf(stderr, "%s:%d: removing timeout %ld\n", __FILE__, __LINE__, tw->tip.tid);
	XtRemoveTimeOut(tw->tip.tid);
	tw->tip.tid = (XtIntervalId) 0;
    }

    if (obj->active == False)
	return;

    if (tw->tip.isup) {
	XtPopdown((Widget) tw);
	tw->tip.isup = NULL;
	tw->tip.HelpPopDownTime = event->time;
    }
}

/****************************************************************************
 * Public interface implementation.
 */

void TipAppHandle(XtAppContext app, XEvent *event)
{
    int i;

    if (!(event->type == EnterNotify
	  || event->type == MotionNotify
	  || event->type == LeaveNotify
	  || event->type == ButtonPress)) {
	return;
    }

    for (i = 0; i < nr_shells; ++i) {
	unsigned int j;

	for (j = 0; j < shells[i]->tip.nr_twl; ++j) {
	    if (event->xany.window == shells[i]->tip.twl[j].window) {
		if (event->type == EnterNotify)
		    enter(shells[i]->tip.twl + j, event, app);
		if (event->xany.type == LeaveNotify
		    || event->xany.type == MotionNotify /* FIXME: this doesn' work? */
		    /* might be useful to popdown tip when mouse is moved */
		    || event->xany.type == ButtonPress) {
		    leave(shells[i]->tip.twl + j, event);
		}
	    }
	}
    }
}

/*
 * This has to replace the XtAppMainLoop in the application using
 * tooltips.
 */
void TipAppMainLoop(XtAppContext app)
{
    XEvent event;

    for (;;) {
	XtAppNextEvent(app, &event);
	TipAppHandle(app, &event);
	XtDispatchEvent(&event);
    }
}

/*
 * Add a widget to be watched for tooltips.
 *
 * This function must be called after the widget has been realized!
 * Further on please make sure that this function will not be called twice
 * for one button!
 *
 * w            - tip widget
 * watch        - the widget to give tips for
 * text         - pointer to tip text
 */
void TipAddWidget(Widget w, Widget watch, const String text)
{
#define ROUTINE "TipAddWidget"
    TipWidget tw = (TipWidget) w;
    int i;

    CheckWidgetClass(ROUTINE);	/* make sure we are called with a tip widget */

    /* Make internal resource available via resource.tooltips_wait_period(_bak) and
     * resource.show_tooltips.
     */
    resource.tooltips_wait_period_bak = resource.tooltips_wait_period = ABS(tw->tip.waitPeriod);
    if (tw->tip.waitPeriod < 0) {
	resource.show_tooltips = False;
    }
    else if (!resource.show_tooltips) {
	if (resource.tooltips_wait_period == 0)
	    resource.tooltips_wait_period = -1;
	else
	    resource.tooltips_wait_period = -resource.tooltips_wait_period;
    }
    
    for (i = 0; i < nr_shells; ++i)
	if (shells[i] == tw) {
	    struct tip_context *obj;

	    if (tw->tip.nr_twl >= TIP_NUM) {
		XDVI_FATAL((stderr, "Too many tip widgets, cannot add new tip"));
		return;
	    }
	    obj = tw->tip.twl + tw->tip.nr_twl;
	    obj->text = XtNewString(text);
	    obj->size = strlen(text);
	    obj->watched = watch;
	    obj->window = XtWindow(watch);
	    obj->active = True;
	    obj->tw = tw;
	    tw->tip.nr_twl++;
	}
#undef ROUTINE
}

#else
/* silence `empty compilation unit' warnings */
static void bar(void); static void foo() { bar(); } static void bar(void) { foo(); }
#endif /* MOTIF */