File: tooltip.c

package info (click to toggle)
siag 3.0.6-4
  • links: PTS
  • area: main
  • in suites: slink
  • size: 3,652 kB
  • ctags: 6,202
  • sloc: ansic: 40,351; lisp: 3,435; sh: 143; makefile: 112
file content (151 lines) | stat: -rw-r--r-- 4,054 bytes parent folder | download
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

/*
 * tooltip.c
 */

#include <stdlib.h>

#include <X11/Intrinsic.h>
#include <X11/StringDefs.h>
#include <X11/Shell.h>
#include <X11/Xaw/Label.h>
#include "../common/common.h"
#include "xcommon.h"
#define TOOLTIP_INTERVAL 500
#define HIGHLIGHT_WIDTH 2
#define UNHIGHLIGHT_WIDTH 1

static Widget label1, tooltip_shell, tooltip_label;
static int tooltip_mode = TOOLTIP_POPUP;
static int tooltip_timeout = None;
static char tooltip_text[256];
static unsigned long highlight_color, unhighlight_color;

static void highlight_action(Widget w, XEvent *event,
        String *params, Cardinal *num_params)
{
	int sw;

	if (*num_params) sw = atoi(*params);
	else sw = HIGHLIGHT_WIDTH;

        if (w) XtVaSetValues(w,
#if 0
                XtNbackground, highlight_color,
#else
		"shadowWidth", sw,
#endif
                (char *)0);
}

static void unhighlight_action(Widget w, XEvent *event,
        String *params, Cardinal *num_params)
{
	int sw;

	if (*num_params) sw = atoi(*params);
        else sw = UNHIGHLIGHT_WIDTH;

        if (w) XtVaSetValues(w,
#if 0
                XtNbackground, unhighlight_color,
#else
		"shadowWidth", sw,
#endif
                (char *)0);
}

static void tooltip_popup(XtPointer client_data, XtIntervalId *id)
{
        tooltip_timeout = None;
        XtPopup(tooltip_shell, XtGrabNone);
}

static void tooltip_popdown(void)
{
        if (tooltip_timeout != None)
                XtRemoveTimeOut(tooltip_timeout);
        tooltip_timeout = None;
        XtPopdown(tooltip_shell);
}

static void draw_input_action(Widget w, XEvent *event,
        String *params, Cardinal *num_params)
{
        if (tooltip_mode & TOOLTIP_LABEL) {
                char *p = "";
                label_set(label1, p);
        }
        if (tooltip_mode & TOOLTIP_POPUP) {
                tooltip_popdown();
        }
}

static void tooltip_action(Widget w, XEvent *event,
        String *params, Cardinal *num_params)
{
        int i;
	char b[256];

        strncpy(b, params[0], 255);
        for (i = 1; i < *num_params; i++) {
                strncat(b, " ", 255);
                strncat(b, params[i], 255);
        }
	strcpy(tooltip_text, translate(b));
        if (tooltip_mode & TOOLTIP_LABEL) {
                XtVaSetValues(label1, XtNlabel, tooltip_text, (char *)0);
        }
        if (tooltip_mode & TOOLTIP_POPUP) {
                Position x, y;
                Dimension height, width;
                XFontStruct *font;

                XtVaGetValues(w,
                        XtNheight, &height, (char *)0);
                XtTranslateCoords(w,
                        0, height+10, &x, &y);
                XtVaGetValues(tooltip_label,
                        XtNfont, &font, (char *)0);
                width = XTextWidth(font, tooltip_text, strlen(tooltip_text));
                XtVaSetValues(tooltip_shell,
                        XtNx, x,
                        XtNy, y,
                        XtNwidth, width+8, (char *)0);
                XtVaSetValues(tooltip_label, XtNlabel, tooltip_text, (char *)0);
                tooltip_timeout = XtAppAddTimeOut(
			XtWidgetToApplicationContext(w),
                        TOOLTIP_INTERVAL,
                        tooltip_popup, (XtPointer)w);
        }
}

static XtActionsRec actions[] =
{
        {"siag-highlight", highlight_action},
        {"siag-unhighlight", unhighlight_action},
        {"tooltip", tooltip_action},
        {"draw-input", draw_input_action}
};

void tooltip_mode_set(int mode, Widget label)
{
	draw_input_action(NULL, NULL, NULL, NULL);
	tooltip_mode = mode;
	label1 = label;
	if (label1 == None) mode &= ~TOOLTIP_LABEL;
}

void tooltip_init(Widget pw, unsigned long hl, unsigned long uhl)
{
	XtAppAddActions(XtWidgetToApplicationContext(pw),
		actions, XtNumber(actions));
	tooltip_shell = XtVaCreatePopupShell("tooltip_shell",
		overrideShellWidgetClass, pw, (char *)0);
	tooltip_label = XtVaCreateManagedWidget("tooltip_label",
		labelWidgetClass, tooltip_shell, (char *)0);
	label1 = None;
	highlight_color = hl;
	unhighlight_color = uhl;
}