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 Massachusetts Institute of Technology 1985 */
#include "copyright.h"
/*
Copyright (C) 2001-2016 Free Software Foundation, Inc.
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>. */
/*
* XMenu: MIT Project Athena, X Window system menu package
*
* XMenuActivate - Maps a given menu to the display and activates
* the menu for user selection. The user is allowed to
* specify which pane and selection will be current,
* the X and Y location of the menu (relative to the
* parent window) and the mouse button event mask that
* will be used to identify a selection request.
*
* A menu selection is shown to be current by placing
* a highlight box around the selection as the mouse
* cursor enters its active region. Inactive selections
* will not be highlighted. As the mouse cursor moved
* from one menu pane to another menu pane the pane being
* entered is raised and made current and the pane being
* left is lowered.
*
* Anytime XMenuActivate returns, the p_num and
* s_num are left at their last known values (i.e.,
* the last known current pane and selection indices).
* The following are the defined return states:
*
* 1) If at any time an error occurs the data
* pointer is left untouched and XM_FAILURE
* is returned.
*
* 2) When a selection request is received (i.e.,
* when the specified mouse event occurs) the
* data pointer will be set to the data
* associated with the particular selection
* current at the time of the selection request
* and XM_SUCCESS is returned.
*
* 3) If no selection was current at the time a
* selection request is made the data pointer
* will be left untouched and XM_NO_SELECT will
* be returned.
*
* 4) If the selection that was current at the time
* a selection request is made is not an active
* selection the data pointer will be left
* untouched and XM_IA_SELECT will be returned.
*
* Since X processes events in an asynchronous manner
* it is likely that XMenuActivate will encounter
* a "foreign event" while it is executing. Foreign
* events are handled in one of three ways:
*
* 1) The event is discarded. This is the default
* mode and requires no action on the part of the
* application.
*
* 2) The application has identified an asynchronous
* event handler that will be called and the
* foreign event handed off to it. Note:
* AEQ mode disables this mode temporarily.
*
* 3) The application has enabled asynchronous event
* queuing mode. In this mode all foreign events
* will be queued up until XMenuActivate
* terminates; at which time they will be
* returned to the X event queue. As long as
* AEQ mode is enabled any asynchronous event
* handler as temporarily disabled.
*
* Any events encountered while taking down the menu
* (i.e., exposure events from occluded windows) will
* automatically be returned to the X event queue after
* XMenuActivate has cleaned the queue of any of its own
* events that are no longer needed.
*
* Author: Tony Della Fera, DEC
* March 12, 1986
*
*/
#include "XMenuInt.h"
#include <X11/keysym.h>
/* For debug, set this to 0 to not grab the keyboard on menu popup */
int x_menu_grab_keyboard = 1;
static Wait_func wait_func;
static void* wait_data;
void
XMenuActivateSetWaitFunction (Wait_func func, void *data)
{
wait_func = func;
wait_data = data;
}
int
XMenuActivate(
register Display *display, /* Display to put menu on. */
register XMenu *menu, /* Menu to activate. */
int *p_num, /* Pane number selected. */
int *s_num, /* Selection number selected. */
int x_pos, /* X coordinate of menu position. */
int y_pos, /* Y coordinate of menu position. */
unsigned int event_mask, /* Mouse button event mask. */
char **data, /* Pointer to return data value. */
void (*help_callback) (char const *, int, int)) /* Help callback. */
{
int status; /* X routine call status. */
int orig_x; /* Upper left menu origin X coord. */
int orig_y; /* Upper left menu origin Y coord. */
int ret_val; /* Return value. */
register XMPane *p_ptr; /* Current XMPane. */
register XMPane *event_xmp; /* Event XMPane pointer. */
register XMPane *cur_p; /* Current pane. */
register XMSelect *cur_s; /* Current selection. */
XMWindow *event_xmw; /* Event XMWindow pointer. */
XEvent event; /* X input event. */
XEvent peek_event; /* X input peek ahead event. */
Bool selection = False; /* Selection has been made. */
Bool forward = True; /* Moving forward in the pane list. */
Window root, child;
int root_x, root_y, win_x, win_y;
unsigned int mask;
KeySym keysym;
/*
* Define and allocate a foreign event queue to hold events
* that don't belong to XMenu. These events are later restored
* to the X event queue.
*/
typedef struct _xmeventque {
XEvent event;
struct _xmeventque *next;
} XMEventQue;
XMEventQue *feq = NULL; /* Foreign event queue. */
XMEventQue *feq_tmp; /* Foreign event queue temporary. */
/*
* If there are no panes in the menu then return failure
* because the menu is not initialized.
*/
if (menu->p_count == 0) {
_XMErrorCode = XME_NOT_INIT;
return(XM_FAILURE);
}
/*
* Find the desired current pane.
*/
cur_p = _XMGetPanePtr(menu, *p_num);
if (cur_p == NULL) {
return(XM_FAILURE);
}
cur_p->activated = cur_p->active;
/*
* Find the desired current selection.
* If the current selection index is out of range a null current selection
* will be assumed and the cursor will be placed in the current pane
* header.
*/
cur_s = _XMGetSelectionPtr(cur_p, *s_num);
/*
* Compute origin of menu so that cursor is in
* Correct pane and selection.
*/
_XMTransToOrigin(display,
menu,
cur_p, cur_s,
x_pos, y_pos,
&orig_x, &orig_y);
menu->x_pos = orig_x; /* Store X and Y coords of menu. */
menu->y_pos = orig_y;
if (XMenuRecompute(display, menu) == XM_FAILURE) {
return(XM_FAILURE);
}
/*
* Flush the window creation queue.
* This batches all window creates since lazy evaluation
* is more efficient than individual evaluation.
* This routine also does an XFlush().
*/
if (_XMWinQueFlush(display, menu, cur_p, cur_s) == _FAILURE) {
return(XM_FAILURE);
}
/*
* Make sure windows are in correct order (in case we were passed
* an already created menu in incorrect order.)
*/
for(p_ptr = menu->p_list->next; p_ptr != cur_p; p_ptr = p_ptr->next)
XRaiseWindow(display, p_ptr->window);
for(p_ptr = menu->p_list->prev; p_ptr != cur_p->prev; p_ptr = p_ptr->prev)
XRaiseWindow(display, p_ptr->window);
/*
* Make sure all selection windows are mapped.
*/
for (
p_ptr = menu->p_list->next;
p_ptr != menu->p_list;
p_ptr = p_ptr->next
){
XMapSubwindows(display, p_ptr->window);
}
/*
* Synchronize the X buffers and the event queue.
* From here on, all events in the queue that don't belong to
* XMenu are sent back to the application via an application
* provided event handler or discarded if the application has
* not provided an event handler.
*/
XSync(display, 0);
/*
* Grab the mouse for menu input.
*/
status = XGrabPointer(
display,
menu->parent,
True,
event_mask,
GrabModeAsync,
GrabModeAsync,
None,
menu->mouse_cursor,
CurrentTime
);
if (status == Success && x_menu_grab_keyboard)
{
status = XGrabKeyboard (display,
menu->parent,
False,
GrabModeAsync,
GrabModeAsync,
CurrentTime);
if (status != Success)
XUngrabPointer(display, CurrentTime);
}
if (status == _X_FAILURE) {
_XMErrorCode = XME_GRAB_MOUSE;
return(XM_FAILURE);
}
/*
* Map the menu panes.
*/
XMapWindow(display, cur_p->window);
for (p_ptr = menu->p_list->next;
p_ptr != cur_p;
p_ptr = p_ptr->next)
XMapWindow(display, p_ptr->window);
for (p_ptr = cur_p->next;
p_ptr != menu->p_list;
p_ptr = p_ptr->next)
XMapWindow(display, p_ptr->window);
XRaiseWindow(display, cur_p->window); /* Make sure current */
/* pane is on top. */
cur_s = NULL; /* Clear current selection. */
/*
* Begin event processing loop.
*/
while (1) {
if (wait_func) (*wait_func) (wait_data);
XNextEvent(display, &event); /* Get next event. */
switch (event.type) { /* Dispatch on the event type. */
case Expose:
event_xmp = (XMPane *)XLookUpAssoc(display,
menu->assoc_tab,
event.xexpose.window);
if (event_xmp == NULL) {
/*
* If AEQ mode is enabled then queue the event.
*/
if (menu->aeq) {
feq_tmp = (XMEventQue *)malloc(sizeof(XMEventQue));
if (feq_tmp == NULL) {
_XMErrorCode = XME_CALLOC;
return(XM_FAILURE);
}
feq_tmp->event = event;
feq_tmp->next = feq;
feq = feq_tmp;
}
else if (_XMEventHandler) (*_XMEventHandler)(&event);
break;
}
if (event_xmp->activated) {
XSetWindowBackground(display,
event_xmp->window,
menu->bkgnd_color);
}
else {
XSetWindowBackgroundPixmap(display,
event_xmp->window,
menu->inact_pixmap);
}
_XMRefreshPane(display, menu, event_xmp);
break;
case EnterNotify:
/*
* First wait a small period of time, and see
* if another EnterNotify event follows hard on the
* heels of this one. i.e., the user is simply
* "passing through". If so, ignore this one.
*/
event_xmw = (XMWindow *)XLookUpAssoc(display,
menu->assoc_tab,
event.xcrossing.window);
if (event_xmw == NULL) break;
if (event_xmw->type == SELECTION) {
/*
* We have entered a selection.
*/
/* if (XPending(display) == 0) usleep(150000); */
if (XPending(display) != 0) {
XPeekEvent(display, &peek_event);
if(peek_event.type == LeaveNotify) {
break;
}
}
cur_s = (XMSelect *)event_xmw;
help_callback (cur_s->help_string,
cur_p->serial, cur_s->serial);
/*
* If the pane we are in is active and the
* selection entered is active then activate
* the selection.
*/
if (cur_p->active && cur_s->active > 0) {
cur_s->activated = 1;
_XMRefreshSelection(display, menu, cur_s);
}
}
else {
/*
* We have entered a pane.
*/
/* if (XPending(display) == 0) usleep(150000); */
if (XPending(display) != 0) {
XPeekEvent(display, &peek_event);
if (peek_event.type == EnterNotify) break;
}
XQueryPointer(display,
menu->parent,
&root, &child,
&root_x, &root_y,
&win_x, &win_y,
&mask);
event_xmp = (XMPane *)XLookUpAssoc(display,
menu->assoc_tab,
child);
if (event_xmp == NULL) break;
if (event_xmp == cur_p) break;
if (event_xmp->serial > cur_p->serial) forward = True;
else forward = False;
p_ptr = cur_p;
while (p_ptr != event_xmp) {
if (forward) p_ptr = p_ptr->next;
else p_ptr = p_ptr->prev;
XRaiseWindow(display, p_ptr->window);
}
if (cur_p->activated) {
cur_p->activated = False;
XSetWindowBackgroundPixmap(display,
cur_p->window,
menu->inact_pixmap);
_XMRefreshPane(display, menu, cur_p);
}
if (event_xmp->active) event_xmp->activated = True;
#if 1
/*
* i suspect the we don't get an EXPOSE event when backing
* store is enabled; the menu windows content is probably
* not drawn in when it should be in that case.
* in that case, this is probably an ugly fix!
* i hope someone more familiar with this code would
* take it from here. -- caveh@eng.sun.com.
*/
XSetWindowBackground(display,
event_xmp->window,
menu->bkgnd_color);
_XMRefreshPane(display, menu, event_xmp);
#endif
cur_p = event_xmp;
}
break;
case LeaveNotify:
event_xmw = (XMWindow *)XLookUpAssoc(
display,
menu->assoc_tab,
event.xcrossing.window
);
if (event_xmw == NULL) break;
if(cur_s == NULL) break;
/*
* If the current selection was activated then
* deactivate it.
*/
if (cur_s->activated) {
cur_s->activated = False;
_XMRefreshSelection(display, menu, cur_s);
}
cur_s = NULL;
break;
case ButtonPress:
case ButtonRelease:
*p_num = cur_p->serial;
/*
* Check to see if there is a current selection.
*/
if (cur_s != NULL) {
/*
* Set the selection number to the current selection.
*/
*s_num = cur_s->serial;
/*
* If the current selection was activated then
* we have a valid selection otherwise we have
* an inactive selection.
*/
if (cur_s->activated) {
*data = cur_s->data;
ret_val = XM_SUCCESS;
}
else {
ret_val = XM_IA_SELECT;
}
}
else {
/*
* No selection was current.
*/
ret_val = XM_NO_SELECT;
}
selection = True;
break;
case KeyPress:
case KeyRelease:
keysym = XLookupKeysym (&event.xkey, 0);
/* Pop down on C-g and Escape. */
if ((keysym == XK_g && (event.xkey.state & ControlMask) != 0)
|| keysym == XK_Escape) /* Any escape, ignore modifiers. */
{
ret_val = XM_NO_SELECT;
selection = True;
}
break;
default:
/*
* If AEQ mode is enabled then queue the event.
*/
if (menu->aeq) {
feq_tmp = (XMEventQue *)malloc(sizeof(XMEventQue));
if (feq_tmp == NULL) {
_XMErrorCode = XME_CALLOC;
return(XM_FAILURE);
}
feq_tmp->event = event;
feq_tmp->next = feq;
feq = feq_tmp;
}
else if (_XMEventHandler) (*_XMEventHandler)(&event);
}
/*
* If a selection has been made, break out of the event loop.
*/
if (selection == True) break;
}
/*
* Unmap the menu.
*/
for ( p_ptr = menu->p_list->next;
p_ptr != menu->p_list;
p_ptr = p_ptr->next)
{
XUnmapWindow(display, p_ptr->window);
}
/*
* Ungrab the mouse.
*/
XUngrabPointer(display, CurrentTime);
XUngrabKeyboard(display, CurrentTime);
/*
* Restore bits under where the menu was if we managed
* to save them and free the pixmap.
*/
/*
* If there is a current selection deactivate it.
*/
if (cur_s != NULL) cur_s->activated = 0;
/*
* Deactivate the current pane.
*/
cur_p->activated = 0;
XSetWindowBackgroundPixmap(display, cur_p->window, menu->inact_pixmap);
/*
* Synchronize the X buffers and the X event queue.
*/
XSync(display, 0);
/*
* Dispatch any events remaining on the queue.
*/
while (QLength(display)) {
/*
* Fetch the next event.
*/
XNextEvent(display, &event);
/*
* Discard any events left on the queue that belong to XMenu.
* All others are held and then returned to the event queue.
*/
switch (event.type) {
case Expose:
case EnterNotify:
case LeaveNotify:
case ButtonPress:
case ButtonRelease:
/*
* Does this event belong to one of XMenu's windows?
* If so, discard it and process the next event.
* If not fall through and treat it as a foreign event.
*/
event_xmp = (XMPane *)XLookUpAssoc(
display,
menu->assoc_tab,
event.xbutton.window
);
if (event_xmp != NULL) continue;
default:
/*
* This is a foreign event.
* Queue it for later return to the X event queue.
*/
feq_tmp = (XMEventQue *)malloc(sizeof(XMEventQue));
if (feq_tmp == NULL) {
_XMErrorCode = XME_CALLOC;
return(XM_FAILURE);
}
feq_tmp->event = event;
feq_tmp->next = feq;
feq = feq_tmp;
}
}
/*
* Return any foreign events that were queued to the X event queue.
*/
while (feq != NULL) {
feq_tmp = feq;
XPutBackEvent(display, &feq_tmp->event);
feq = feq_tmp->next;
free((char *)feq_tmp);
}
wait_func = 0;
/*
* Return successfully.
*/
_XMErrorCode = XME_NO_ERROR;
return(ret_val);
}
|