-
Notifications
You must be signed in to change notification settings - Fork 0
/
SmeCascade.c
executable file
·403 lines (347 loc) · 10.5 KB
/
SmeCascade.c
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
/*
* FIG : Facility for Interactive Generation of figures
* Parts Copyright (c) 1989-2003 by Brian V. Smith
*
* Any party obtaining a copy of these files is granted, free of charge, a
* full and unrestricted irrevocable, world-wide, paid up, royalty-free,
* nonexclusive right and license to deal in this software and
* documentation files (the "Software"), including without limitation the
* rights to use, copy, modify, merge, publish and/or distribute copies of
* the Software, and to permit persons who receive copies from any such
* party to do so, with the only requirement being that this copyright
* notice remain intact.
*
*/
/* This subclasses the SmeBSB object to make a cascade menu */
#include <stdio.h>
#include "fig.h"
#include "figx.h"
#include <X11/Intrinsic.h>
#include <X11/IntrinsicP.h>
#include <X11/Xmu/Drawing.h>
#ifdef XAW3D
#include <X11/Xaw3d/XawInit.h>
#else
#include <X11/Xaw/XawInit.h>
#endif /* XAW3D */
#include "SmeCascadeP.h"
#include <X11/ShellP.h>
#define offset(field) XtOffset(SmeCascadeObject, sme_cascade.field)
static XtResource resources[] = {
{XtNsubMenu, XtCSubMenu, XtRWidget, sizeof(Widget),
offset(subMenu), XtRString, NULL},
{XtNselectCascade, XtCSelectCascade, XtRBoolean, sizeof(Boolean),
offset(selectCascade), XtRBoolean, (XtPointer) False},
{"highlighted", "Highlighted", XtRBoolean, sizeof(Boolean),
offset(highlighted), XtRBoolean, (XtPointer) False},
};
#undef offset
/*
* Semi Public function definitions.
*/
static void ClassInitialize(void);
static void HighlightCascade(Widget w);
static void UnhighlightCascade(Widget w);
static void Notify(Widget w);
/*
* Private Function Definitions.
*/
void popdown_subs(void);
static void popdown(Widget w);
#define superclass (&smeBSBClassRec)
SmeCascadeClassRec smeCascadeClassRec = {
{
/* superclass */ (WidgetClass) superclass,
/* class_name */ "SmeCascade",
/* size */ sizeof(SmeCascadeRec),
/* class_initializer */ ClassInitialize,
/* class_part_initialize*/ NULL,
/* Class init'ed */ FALSE,
/* initialize */ NULL,
/* initialize_hook */ NULL,
/* realize */ NULL,
/* actions */ NULL,
/* num_actions */ ZERO,
/* resources */ resources,
/* resource_count */ XtNumber(resources),
/* xrm_class */ NULLQUARK,
/* compress_motion */ FALSE,
/* compress_exposure */ FALSE,
/* compress_enterleave*/ FALSE,
/* visible_interest */ FALSE,
/* destroy */ NULL,
/* resize */ NULL,
/* expose */ XtInheritExpose,
/* set_values */ NULL,
/* 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*/ NULL,
/* extension */ NULL
},{
/* Simple Menu Entry (Sme) Fields */
/* highlight */ HighlightCascade,
/* unhighlight */ UnhighlightCascade,
/* notify */ Notify,
/* extension */ NULL
}, {
#ifdef XAW3D
/* ThreeDClass Fields */
/* shadowdraw */ XtInheritXawSme3dShadowDraw
}, {
#endif /* XAW3D */
/* SmeBSB Menu entry Fields */
/* extension */ NULL
}, {
/* Cascade Menu entry Fields */
/* extension */ NULL
}
};
WidgetClass smeCascadeObjectClass = (WidgetClass) &smeCascadeClassRec;
/* really, really private stuff */
static Widget popped_up[50]; /* to keep track of which menus are still popped up */
static int popped; /* index into popped_up[] */
/************************************************************
*
* Public Functions.
*
************************************************************/
/* Function Name: popdown_subs
* Description: pops down any remaining submenus if user releases
* pointer button outside of submenu
* Arguments: none.
* Returns: none.
*/
void
popdown_subs(void)
{
int i;
/* pop down all submenus */
for (i=popped-1; i>=0; i--)
popdown(popped_up[i]);
popped = 0;
}
/************************************************************
*
* Semi-Public Functions.
*
************************************************************/
/* Function Name: ClassInitialize
* Description: Initializes the SmeBSBObject.
* Arguments: none.
* Returns: none.
*/
static void
ClassInitialize(void)
{
XawInitializeWidgetSet();
XtAddConverter( XtRString, XtRWidget, XmuCvtStringToWidget, NULL, 0 );
popped = 0;
}
/* Function Name: MoveMenu
* Description: Actually moves the menu, may force it to
* to be fully visable if menu_on_screen is TRUE.
* Arguments: w - the simple menu widget.
* x, y - the current location of the widget.
* Returns: none
* Modified from SimpleMenu.c by Chris D. Peterson
*/
static void
MoveMenu(Widget w, Position x, Position y)
{
Arg arglist[2];
Cardinal num_args = 0;
int width = w->core.width + 2 * w->core.border_width;
int height = w->core.height + 2 * w->core.border_width;
if (x < 0)
x = 0;
else {
int scr_width = WidthOfScreen(XtScreen(w));
if (x + width > scr_width)
x = scr_width - width;
}
if (y < 0)
y = 0;
else {
int scr_height = HeightOfScreen(XtScreen(w));
if (y + height > scr_height)
y = scr_height - height;
}
XtSetArg(arglist[num_args], XtNx, x); num_args++;
XtSetArg(arglist[num_args], XtNy, y); num_args++;
XtSetValues(w, arglist, num_args);
}
/************************************************************
*
* Highlight entry and pop up submenu of cascade entry.
*
************************************************************/
static void
HighlightCascade(Widget w)
{
SmeCascadeObject entry = (SmeCascadeObject) w;
int destX, destY, i;
Window childRtrn;
ShellWidget shellparent;
Widget parent, submenu;
parent = XtParent(w);
shellparent = (ShellWidget) parent;
submenu = entry->sme_cascade.subMenu;
/* if there are any submenus popped up and they are not connected to us, pop them down */
if (popped) {
/* if this is a cascade */
if (submenu) {
if (parent != popped_up[popped-1]) {
for (i=0; i<popped; i++)
if (submenu == popped_up[i])
break;
if (i>=popped) {
for (i=popped-1; i>=0; i--) {
if (popped_up[i] == parent)
break;
popdown(popped_up[i]);
}
popped = i+1;
}
}
} else {
/* an ordinary BSB */
if (parent != popped_up[popped-1]) {
for (i=popped-1; i>=0; i--) {
if (popped_up[i] == parent)
break;
popdown(popped_up[i]);
}
popped = i+1;
}
}
}
/* if this is an ordinary BSB entry */
if (submenu == NULL) {
/* highlight the entry */
if (!entry->sme_cascade.highlighted) {
(*superclass->sme_class.highlight) (w);
entry->sme_cascade.highlighted = True;
}
} else {
/* else, a cascade entry */
/* if the user has asked for this cascade to be selectable (Notify) */
if (entry->sme_cascade.selectCascade && !entry->sme_cascade.highlighted) {
(*superclass->sme_class.highlight) (w);
entry->sme_cascade.highlighted = True;
}
if (shellparent->shell.popped_up) {
XtRealizeWidget(submenu);
XTranslateCoordinates(XtDisplayOfObject(w),
XtWindowOfObject(w),
RootWindowOfScreen(XtScreenOfObject(w)),
entry->rectangle.width - 12,
entry->rectangle.y+2,
&destX,&destY,&childRtrn);
MoveMenu(submenu,destX,destY);
XtPopup(submenu,XtGrabNonexclusive);
/* put this one on the stack if it isn't already there */
for (i=0; i<popped; i++) {
if (popped_up[i] == submenu)
break;
}
if (i >= popped) {
popped_up[popped] = submenu;
popped++;
}
}
}
}
/************************************************************
*
* Check if the pointer is actually leaving the menu or is in a submenu.
*
************************************************************/
static void
UnhighlightCascade(Widget w)
{
Dimension height, width;
Window root, child;
int root_x, root_y, px, py;
Position x, y;
unsigned int mask;
SmeCascadeObject entry = (SmeCascadeObject) w;
Widget submenu;
submenu = entry->sme_cascade.subMenu;
if (submenu == NULL) {
if (entry->sme_cascade.highlighted) {
(*superclass->sme_class.unhighlight) (w);
entry->sme_cascade.highlighted = False;
}
return;
}
XtVaGetValues(w,
XtNx, &x,
XtNy, &y,
XtNheight, &height,
XtNwidth, &width,
NULL);
XQueryPointer(XtDisplayOfObject(w),
XtWindowOfObject(w),
&root, &child, &root_x, &root_y, &px, &py, &mask);
x = px-(int)x;
y = py-(int)y;
/* see if the user moved the pointer into the submenu */
if ((x >= width-12) && (x < width) && (y >= 2) && (y < height)) {
/* yes, if this is a selectable entry, unhighlight it */
if (entry->sme_cascade.selectCascade) {
(*superclass->sme_class.unhighlight) (w);
entry->sme_cascade.highlighted = False;
}
return; /* don't pop it down in either case */
}
/* if the user has asked for this cascade to be selectable (Notify) */
if (entry->sme_cascade.selectCascade && entry->sme_cascade.highlighted) {
(*superclass->sme_class.unhighlight) (w);
}
entry->sme_cascade.highlighted = False;
if (submenu != NULL) {
if (popped > 0)
popped--;
popdown(submenu);
}
}
static void
Notify(Widget w)
{
Window root, child;
int root_x, root_y, px, py;
unsigned int mask;
XQueryPointer(XtDisplayOfObject(w),
XtWindowOfObject(w),
&root, &child, &root_x, &root_y, &px, &py, &mask);
/* call superclass notify method */
if (px < w->core.width - 12) {
(*superclass->sme_class.notify) (w);
popdown_subs();
}
}
/************************************************************
*
* Private Functions.
*
************************************************************/
extern void _XtDefaultWarningMsg();
static void
null_sme_proc(void)
{
}
static void
popdown(Widget w)
{
/* nullify the warning handler because we may have an unmatched grab */
XtAppSetWarningMsgHandler(XtWidgetToApplicationContext(w), null_sme_proc);
XtPopdown(w);
/* restore the warning handler */
XtAppSetWarningMsgHandler(XtWidgetToApplicationContext(w), _XtDefaultWarningMsg);
}