File: MwTooltip.c

package info (click to toggle)
mowitz 0.2.0-1
  • links: PTS
  • area: main
  • in suites: woody
  • size: 4,168 kB
  • ctags: 4,214
  • sloc: ansic: 30,869; sh: 9,051; makefile: 169
file content (357 lines) | stat: -rw-r--r-- 9,217 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
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

/*
 * MwTooltip.c - Source code file for MwTooltip widget.
 */

#include <stdio.h>
#include <X11/IntrinsicP.h>
#include <X11/StringDefs.h>

#include "MwTooltipP.h"
#include <X11/Xaw/Cardinals.h>
#include <X11/Xaw/Label.h>

#include "MwUtils.h"

#define offset(field) XtOffsetOf(MwTooltipRec, tooltip.field)

static XtResource resources[] = { 
	{
		XtNtooltipLabel,	/* name */
		XtCTooltipLabel,	/* class */
		XtRWidget,		/* type */
		sizeof(Widget),		/* size */
		offset(label),		/* offset */
		XtRImmediate,		/* default_type */
		(XtPointer)None		/* default_addr */
	}, {
		XtNtooltipMode,
		XtCTooltipMode,
		XtRInt,
		sizeof(int),
		offset(mode),
		XtRImmediate,
		(XtPointer)TOOLTIP_POPUP
	}, {
		XtNtooltipInterval,
		XtCTooltipInterval,
		XtRInt,
		sizeof(int),
		offset(interval),
		XtRImmediate,
		(XtPointer)500
	}, {
		XtNheight,
		XtCHeight,
		XtRDimension,
		sizeof(Dimension),
		XtOffsetOf(CoreRec, core.height),
		XtRImmediate,
		(XtPointer)20
	}
};

#undef offset


/*
 * Semi Public function definitions. 
 */

static void Resize(Widget), ChangeManaged(Widget);
static void Initialize(Widget, Widget, ArgList, Cardinal *),
		ClassPartInitialize(WidgetClass);
static void Destroy(Widget), DoLayout(Widget);
static Boolean SetValues(Widget, Widget, Widget, ArgList, Cardinal *);
static XtGeometryResult GeometryManager(Widget,
		XtWidgetGeometry *, XtWidgetGeometry *);

static CompositeClassExtensionRec extension_rec = {
    /* next_extension */  NULL,
    /* record_type */     NULLQUARK,
    /* version */         XtCompositeExtensionVersion,
    /* record_size */     sizeof(CompositeClassExtensionRec),
    /* accepts_objects */ TRUE,
};

#define superclass (&overrideShellClassRec)
    
MwTooltipClassRec mwTooltipClassRec = {
  {
    /* superclass         */    (WidgetClass) superclass,
    /* class_name         */    "MwTooltip",
    /* size               */    sizeof(MwTooltipRec),
    /* class_initialize   */	NULL,
    /* class_part_initialize*/	ClassPartInitialize,
    /* Class init'ed      */	FALSE,
    /* initialize         */    Initialize,
    /* initialize_hook    */	NULL,
    /* realize            */    XtInheritRealize,
    /* actions            */    NULL,
    /* num_actions        */    0,
    /* resources          */    resources,
    /* resource_count     */	XtNumber(resources),
    /* xrm_class          */    NULLQUARK,
    /* compress_motion    */    TRUE, 
    /* compress_exposure  */    TRUE,
    /* compress_enterleave*/ 	TRUE,
    /* visible_interest   */    FALSE,
    /* destroy            */    Destroy,
    /* resize             */    Resize,
    /* expose             */    NULL,
    /* set_values         */    SetValues,
    /* set_values_hook    */	NULL,
    /* set_values_almost  */	XtInheritSetValuesAlmost,  
    /* get_values_hook    */	NULL,			
    /* accept_focus       */    NULL,
    /* intrinsics version */	XtVersion,
    /* callback offsets   */    NULL,
    /* tm_table		  */    NULL,
    /* query_geometry	  */    XtInheritQueryGeometry,
    /* display_accelerator*/    XtInheritDisplayAccelerator,
    /* extension	  */    NULL
  },{
    /* geometry_manager   */    GeometryManager,
    /* change_managed     */    ChangeManaged,
    /* insert_child	  */	XtInheritInsertChild,
    /* delete_child	  */	XtInheritDeleteChild,
    /* extension	  */    NULL
  },{
    /* Shell extension	  */    NULL
  },{
    /* Override extension */    NULL
  },{
    /* Simple Menu extension*/  NULL
  }
};

WidgetClass mwTooltipWidgetClass = (WidgetClass)&mwTooltipClassRec;


/*      Function Name: ClassPartInitialize
 *      Description: Class Part Initialize routine, called for every
 *                   subclass.  Makes sure that the subclasses pick up 
 *                   the extension record.
 *      Arguments: wc - the widget class of the subclass.
 *      Returns: none.
 */

static void ClassPartInitialize(WidgetClass wc)
{
    MwTooltipWidgetClass smwc = (MwTooltipWidgetClass) wc;

/*
 * Make sure that our subclass gets the extension rec too.
 */

    extension_rec.next_extension = smwc->composite_class.extension;
    smwc->composite_class.extension = (XtPointer) &extension_rec;
}

/*      Function Name: Initialize
 *      Description: Initializes the tooltip widget
 *      Arguments: request - the widget requested by the argument list.
 *                 new     - the new widget with both resource and non
 *                           resource values.
 *      Returns: none.
 */

/* ARGSUSED */
static void Initialize(Widget request, Widget new,
		ArgList args, Cardinal *num_args)
{
  MwTooltipWidget smw = (MwTooltipWidget) new;

  smw->tooltip.plabel = XtVaCreateManagedWidget("popup_label",
	labelWidgetClass, new,
	(char *)0);
  smw->tooltip.timer = None;
}

static void Resize(Widget w)
{
	DoLayout(w);
}

static void DoLayout(Widget w)
{
	int i;
	MwTooltipWidget tw = (MwTooltipWidget)w;

	for (i = 0; i < tw->composite.num_children; i++) {
		Widget cw = tw->composite.children[i];
		XtConfigureWidget(cw,
			0, 0, tw->core.width, tw->core.height, 0);
	}
}

static void Destroy(Widget w)
{
	MwTooltipWidget tw = (MwTooltipWidget)w;

	while (tw->tooltip.plist)
		MwTooltipRemove(w, tw->tooltip.plist->w);
	if (tw->tooltip.plabel)
		XtDestroyWidget(tw->tooltip.plabel);
}

static Boolean SetValues(Widget current, Widget request, Widget new,
			 ArgList args, Cardinal *num_args)
{
	return False;
}

/************************************************************
 *
 * Geometry Management routines.
 *
 ************************************************************/

static XtGeometryResult GeometryManager(Widget w,
	XtWidgetGeometry *request, XtWidgetGeometry *reply)
{
	return XtGeometryYes;
}

static void ChangeManaged(Widget w)
{
    DoLayout(w);
}


void MwTooltipAddGlobalActions(XtAppContext app_con)
{
    XtInitializeWidgetClass(mwTooltipWidgetClass);
} 

static void tooltip_popup(XtPointer client_data, XtIntervalId *id)
{
	Widget w = (Widget)client_data;
	MwTooltipWidget tw = (MwTooltipWidget)w;
        tw->tooltip.timer = None;
        XtPopup(w, XtGrabNone);
}

static void tooltip_popdown(MwTooltipWidget tw)
{
        if (tw->tooltip.timer != None)
                XtRemoveTimeOut(tw->tooltip.timer);
        tw->tooltip.timer = None;
        XtPopdown((Widget)tw);
}


static void tooltip_show(Widget w, XtPointer p, XEvent *event, Boolean *n)
{
	MwTooltipWidget tw = (MwTooltipWidget)p;
	char *t = MwTooltipGet((Widget)tw, w);

	if (t == NULL) return;
        if ((tw->tooltip.mode & TOOLTIP_LABEL)
	     && (tw->tooltip.label != None)) {
                XtVaSetValues(tw->tooltip.label,
			XtNlabel, (String)t,
			(char *)0);
        }
        if (tw->tooltip.mode & TOOLTIP_POPUP) {
		int x, y;
		Window child;
                Dimension height, width;
                XFontStruct *font;

                XtVaGetValues(w,
                        XtNheight, &height, (char *)0);
		XTranslateCoordinates(XtDisplay(w),
			XtWindow(w), DefaultRootWindow(XtDisplay(w)),
			0, height+10,
			&x, &y,
			&child);
                XtVaGetValues(tw->tooltip.plabel,
                        XtNfont, &font, (char *)0);
                width = XTextWidth(font, t, strlen(t));
                XtVaSetValues((Widget)tw,
                        XtNx, x,
                        XtNy, y,
                        XtNwidth, width+8,
			(char *)0);
                XtVaSetValues(tw->tooltip.plabel,
			XtNlabel, t,
			(char *)0);
                tw->tooltip.timer = XtAppAddTimeOut(
			XtWidgetToApplicationContext(w),
                        tw->tooltip.interval,
                        tooltip_popup, (XtPointer)tw);
        }
}

static void tooltip_hide(Widget w, XtPointer p, XEvent *event, Boolean *n)
{
	MwTooltipWidget tw = (MwTooltipWidget)p;

        if ((tw->tooltip.mode & TOOLTIP_LABEL)
	    && (tw->tooltip.label != None)) {
                XtVaSetValues(tw->tooltip.label,
			      XtNlabel, "",
			      (char *)0);
        }
        if (tw->tooltip.mode & TOOLTIP_POPUP) {
                tooltip_popdown(tw);
        }
}

void MwTooltipAdd(Widget w, Widget w2, char *p)
{
	MwTooltipWidget tw = (MwTooltipWidget)w;
	p_list *entry = (p_list *)MwMalloc(sizeof(p_list));

	entry->w = w2;
	entry->p = MwStrdup(p);
	entry->next = tw->tooltip.plist;
	tw->tooltip.plist = entry;
	XtAddEventHandler(w2, EnterWindowMask, False,
			 tooltip_show, (XtPointer)w);
	XtAddEventHandler(w2, LeaveWindowMask, False,
			 tooltip_hide, (XtPointer)w);
}

char *MwTooltipGet(Widget w, Widget w2)
{
	MwTooltipWidget tw = (MwTooltipWidget)w;
	char *p = NULL;

	p_list *entry;

	for (entry = tw->tooltip.plist; entry; entry = entry->next) {
		if (entry->w == w2) {
			p = entry->p;
			break;
		}
	}
	return p;
}

void MwTooltipRemove(Widget w, Widget w2)
{
	MwTooltipWidget tw = (MwTooltipWidget)w;

	p_list *entry = tw->tooltip.plist, *tmp = NULL;

	if (entry == NULL) return;	/* no tooltips at all */
	if (entry->w == w2) {		/* remove first tooltip */
		tmp = entry;
		tw->tooltip.plist = entry->next;
	}
	while (entry->next) {		/* check all others */
		if (entry->next->w == w2) {
			p_list *tmp = entry->next;
			entry->next = tmp->next;
		}
		entry = entry->next;
	}
	if (tmp) {
		MwFree(tmp->p);
		MwFree(tmp);
	}
}