1 /* Copyright Massachusetts Institute of Technology 1985 */
6 * XMenu: MIT Project Athena, X Window system menu package
8 * XMenuActivate - Maps a given menu to the display and activates
9 * the menu for user selection. The user is allowed to
10 * specify which pane and selection will be current,
11 * the X and Y location of the menu (relative to the
12 * parent window) and the mouse button event mask that
13 * will be used to identify a selection request.
15 * A menu selection is shown to be current by placing
16 * a highlight box around the selection as the mouse
17 * cursor enters its active region. Inactive selections
18 * will not be highlighted. As the mouse cursor moved
19 * from one menu pane to another menu pane the pane being
20 * entered is raised and made current and the pane being
23 * Anytime XMenuActivate returns, the p_num and
24 * s_num are left at their last known values (i.e.,
25 * the last known current pane and selection indices).
26 * The following are the defined return states:
28 * 1) If at any time an error occurs the data
29 * pointer is left untouched and XM_FAILURE
32 * 2) When a selection request is received (i.e.,
33 * when the specified mouse event occurs) the
34 * data pointer will be set to the data
35 * associated with the particular selection
36 * current at the time of the selection request
37 * and XM_SUCCESS is returned.
39 * 3) If no selection was current at the time a
40 * selection request is made the data pointer
41 * will be left untouched and XM_NO_SELECT will
44 * 4) If the selection that was current at the time
45 * a selection request is made is not an active
46 * selection the data pointer will be left
47 * untouched and XM_IA_SELECT will be returned.
49 * Since X processes events in an asynchronous manner
50 * it is likely that XMenuActivate will encounter
51 * a "foreign event" while it is executing. Foreign
52 * events are handled in one of three ways:
54 * 1) The event is discarded. This is the default
55 * mode and requires no action on the part of the
58 * 2) The application has identified an asynchronous
59 * event handler that will be called and the
60 * foreign event handed off to it. Note:
61 * AEQ mode disables this mode temporarily.
63 * 3) The application has enabled asynchronous event
64 * queuing mode. In this mode all foreign events
65 * will be queued up untill XMenuActivate
66 * terminates; at which time they will be
67 * returned to the X event queue. As long as
68 * AEQ mode is enabled any asynchronous event
69 * handler as temporarily disabled.
71 * Any events encountered while taking down the menu
72 * (i.e., exposure events from occluded windows) will
73 * automatically be returned to the X event queue after
74 * XMenuActivate has cleaned the queue of any of its own
75 * events that are no longer needed.
77 * Author: Tony Della Fera, DEC
85 /* For debug, set this to 0 to not grab the keyboard on menu popup */
86 int x_menu_grab_keyboard
= 1;
89 XMenuActivate(display
, menu
, p_num
, s_num
, x_pos
, y_pos
, event_mask
, data
,
91 register Display
*display
; /* Display to put menu on. */
92 register XMenu
*menu
; /* Menu to activate. */
93 int *p_num
; /* Pane number selected. */
94 int *s_num
; /* Selection number selected. */
95 int x_pos
; /* X coordinate of menu position. */
96 int y_pos
; /* Y coordinate of menu position. */
97 unsigned int event_mask
; /* Mouse button event mask. */
98 char **data
; /* Pointer to return data value. */
99 void (* help_callback
) (); /* Help callback. */
101 int status
; /* X routine call status. */
102 int orig_x
; /* Upper left menu origin X coord. */
103 int orig_y
; /* Upper left menu origin Y coord. */
104 int ret_val
; /* Return value. */
106 register XMPane
*p_ptr
; /* Current XMPane. */
107 register XMPane
*event_xmp
; /* Event XMPane pointer. */
108 register XMPane
*cur_p
; /* Current pane. */
109 register XMSelect
*cur_s
; /* Current selection. */
110 XMWindow
*event_xmw
; /* Event XMWindow pointer. */
111 XEvent event
; /* X input event. */
112 XEvent peek_event
; /* X input peek ahead event. */
114 Bool selection
= False
; /* Selection has been made. */
115 Bool forward
= True
; /* Moving forward in the pane list. */
118 int root_x
, root_y
, win_x
, win_y
;
122 * Define and allocate a foreign event queue to hold events
123 * that don't belong to XMenu. These events are later restored
124 * to the X event queue.
126 typedef struct _xmeventque
{
128 struct _xmeventque
*next
;
131 XMEventQue
*feq
= NULL
; /* Foreign event queue. */
132 XMEventQue
*feq_tmp
; /* Foreign event queue temporary. */
135 * If there are no panes in the menu then return failure
136 * because the menu is not initialized.
138 if (menu
->p_count
== 0) {
139 _XMErrorCode
= XME_NOT_INIT
;
144 * Find the desired current pane.
146 cur_p
= _XMGetPanePtr(menu
, *p_num
);
150 cur_p
->activated
= cur_p
->active
;
153 * Find the desired current selection.
154 * If the current selection index is out of range a null current selection
155 * will be assumed and the cursor will be placed in the current pane
158 cur_s
= _XMGetSelectionPtr(cur_p
, *s_num
);
161 * Compute origin of menu so that cursor is in
162 * Correct pane and selection.
164 _XMTransToOrigin(display
,
169 menu
->x_pos
= orig_x
; /* Store X and Y coords of menu. */
170 menu
->y_pos
= orig_y
;
172 if (XMenuRecompute(display
, menu
) == XM_FAILURE
) {
177 * Flush the window creation queue.
178 * This batches all window creates since lazy evaluation
179 * is more efficient than individual evaluation.
180 * This routine also does an XFlush().
182 if (_XMWinQueFlush(display
, menu
, cur_p
, cur_s
) == _FAILURE
) {
187 * Make sure windows are in correct order (in case we were passed
188 * an already created menu in incorrect order.)
190 for(p_ptr
= menu
->p_list
->next
; p_ptr
!= cur_p
; p_ptr
= p_ptr
->next
)
191 XRaiseWindow(display
, p_ptr
->window
);
192 for(p_ptr
= menu
->p_list
->prev
; p_ptr
!= cur_p
->prev
; p_ptr
= p_ptr
->prev
)
193 XRaiseWindow(display
, p_ptr
->window
);
196 * Make sure all selection windows are mapped.
199 p_ptr
= menu
->p_list
->next
;
200 p_ptr
!= menu
->p_list
;
203 XMapSubwindows(display
, p_ptr
->window
);
207 * Synchronize the X buffers and the event queue.
208 * From here on, all events in the queue that don't belong to
209 * XMenu are sent back to the application via an application
210 * provided event handler or discarded if the application has
211 * not provided an event handler.
216 * Grab the mouse for menu input.
219 status
= XGrabPointer(
230 if (status
== Success
&& x_menu_grab_keyboard
)
232 status
= XGrabKeyboard (display
,
238 if (status
!= Success
)
239 XUngrabPointer(display
, CurrentTime
);
242 if (status
== _X_FAILURE
) {
243 _XMErrorCode
= XME_GRAB_MOUSE
;
248 * Map the menu panes.
250 XMapWindow(display
, cur_p
->window
);
251 for (p_ptr
= menu
->p_list
->next
;
254 XMapWindow(display
, p_ptr
->window
);
255 for (p_ptr
= cur_p
->next
;
256 p_ptr
!= menu
->p_list
;
258 XMapWindow(display
, p_ptr
->window
);
260 XRaiseWindow(display
, cur_p
->window
); /* Make sure current */
261 /* pane is on top. */
263 cur_s
= NULL
; /* Clear current selection. */
266 * Begin event processing loop.
269 XNextEvent(display
, &event
); /* Get next event. */
270 switch (event
.type
) { /* Dispatch on the event type. */
272 event_xmp
= (XMPane
*)XLookUpAssoc(display
,
274 event
.xexpose
.window
);
275 if (event_xmp
== NULL
) {
277 * If AEQ mode is enabled then queue the event.
280 feq_tmp
= (XMEventQue
*)malloc(sizeof(XMEventQue
));
281 if (feq_tmp
== NULL
) {
282 _XMErrorCode
= XME_CALLOC
;
285 feq_tmp
->event
= event
;
289 else if (_XMEventHandler
) (*_XMEventHandler
)(&event
);
292 if (event_xmp
->activated
) {
293 XSetWindowBackground(display
,
298 XSetWindowBackgroundPixmap(display
,
302 _XMRefreshPane(display
, menu
, event_xmp
);
306 * First wait a small period of time, and see
307 * if another EnterNotify event follows hard on the
308 * heels of this one. i.e., the user is simply
309 * "passing through". If so, ignore this one.
312 event_xmw
= (XMWindow
*)XLookUpAssoc(display
,
314 event
.xcrossing
.window
);
315 if (event_xmw
== NULL
) break;
316 if (event_xmw
->type
== SELECTION
) {
318 * We have entered a selection.
320 /* if (XPending(display) == 0) usleep(150000); */
321 if (XPending(display
) != 0) {
322 XPeekEvent(display
, &peek_event
);
323 if(peek_event
.type
== LeaveNotify
) {
327 cur_s
= (XMSelect
*)event_xmw
;
328 help_callback (cur_s
->help_string
,
329 cur_p
->serial
, cur_s
->serial
);
332 * If the pane we are in is active and the
333 * selection entered is active then activate
336 if (cur_p
->active
&& cur_s
->active
> 0) {
337 cur_s
->activated
= 1;
338 _XMRefreshSelection(display
, menu
, cur_s
);
343 * We have entered a pane.
345 /* if (XPending(display) == 0) usleep(150000); */
346 if (XPending(display
) != 0) {
347 XPeekEvent(display
, &peek_event
);
348 if (peek_event
.type
== EnterNotify
) break;
350 XQueryPointer(display
,
356 event_xmp
= (XMPane
*)XLookUpAssoc(display
,
359 if (event_xmp
== NULL
) break;
360 if (event_xmp
== cur_p
) break;
361 if (event_xmp
->serial
> cur_p
->serial
) forward
= True
;
362 else forward
= False
;
364 while (p_ptr
!= event_xmp
) {
365 if (forward
) p_ptr
= p_ptr
->next
;
366 else p_ptr
= p_ptr
->prev
;
367 XRaiseWindow(display
, p_ptr
->window
);
369 if (cur_p
->activated
) {
370 cur_p
->activated
= False
;
371 XSetWindowBackgroundPixmap(display
,
374 _XMRefreshPane(display
, menu
, cur_p
);
376 if (event_xmp
->active
) event_xmp
->activated
= True
;
379 * i suspect the we don't get an EXPOSE event when backing
380 * store is enabled; the menu windows content is probably
381 * not drawn in when it should be in that case.
382 * in that case, this is probably an ugly fix!
383 * i hope someone more familiar with this code would
384 * take it from here. -- caveh@eng.sun.com.
386 XSetWindowBackground(display
,
389 _XMRefreshPane(display
, menu
, event_xmp
);
395 event_xmw
= (XMWindow
*)XLookUpAssoc(
398 event
.xcrossing
.window
400 if (event_xmw
== NULL
) break;
401 if(cur_s
== NULL
) break;
404 * If the current selection was activated then
407 if (cur_s
->activated
) {
408 cur_s
->activated
= False
;
409 _XMRefreshSelection(display
, menu
, cur_s
);
416 *p_num
= cur_p
->serial
;
418 * Check to see if there is a current selection.
422 * Set the selection number to the current selection.
424 *s_num
= cur_s
->serial
;
426 * If the current selection was activated then
427 * we have a valid selection otherwise we have
428 * an inactive selection.
430 if (cur_s
->activated
) {
432 ret_val
= XM_SUCCESS
;
435 ret_val
= XM_IA_SELECT
;
440 * No selection was current.
442 ret_val
= XM_NO_SELECT
;
448 * If AEQ mode is enabled then queue the event.
451 feq_tmp
= (XMEventQue
*)malloc(sizeof(XMEventQue
));
452 if (feq_tmp
== NULL
) {
453 _XMErrorCode
= XME_CALLOC
;
456 feq_tmp
->event
= event
;
460 else if (_XMEventHandler
) (*_XMEventHandler
)(&event
);
463 * If a selection has been made, break out of the event loop.
465 if (selection
== True
) break;
471 for ( p_ptr
= menu
->p_list
->next
;
472 p_ptr
!= menu
->p_list
;
475 XUnmapWindow(display
, p_ptr
->window
);
481 XUngrabPointer(display
, CurrentTime
);
482 XUngrabKeyboard(display
, CurrentTime
);
485 * Restore bits under where the menu was if we managed
486 * to save them and free the pixmap.
490 * If there is a current selection deactivate it.
492 if (cur_s
!= NULL
) cur_s
->activated
= 0;
495 * Deactivate the current pane.
497 cur_p
->activated
= 0;
498 XSetWindowBackgroundPixmap(display
, cur_p
->window
, menu
->inact_pixmap
);
501 * Synchronize the X buffers and the X event queue.
506 * Dispatch any events remaining on the queue.
508 while (QLength(display
)) {
510 * Fetch the next event.
512 XNextEvent(display
, &event
);
515 * Discard any events left on the queue that belong to XMenu.
516 * All others are held and then returned to the event queue.
518 switch (event
.type
) {
525 * Does this event belong to one of XMenu's windows?
526 * If so, discard it and process the next event.
527 * If not fall through and treat it as a foreign event.
529 event_xmp
= (XMPane
*)XLookUpAssoc(
534 if (event_xmp
!= NULL
) continue;
537 * This is a foreign event.
538 * Queue it for later return to the X event queue.
540 feq_tmp
= (XMEventQue
*)malloc(sizeof(XMEventQue
));
541 if (feq_tmp
== NULL
) {
542 _XMErrorCode
= XME_CALLOC
;
545 feq_tmp
->event
= event
;
551 * Return any foreign events that were queued to the X event queue.
553 while (feq
!= NULL
) {
555 XPutBackEvent(display
, &feq_tmp
->event
);
557 free((char *)feq_tmp
);
561 * Return successfully.
563 _XMErrorCode
= XME_NO_ERROR
;
568 /* arch-tag: 6b90b578-ecea-4328-b460-a0c96963f872
569 (do not change this comment) */