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
84 #include <X11/keysym.h>
86 /* For debug, set this to 0 to not grab the keyboard on menu popup */
87 int x_menu_grab_keyboard
= 1;
89 typedef void (*Wait_func
)();
91 static Wait_func wait_func
;
92 static void* wait_data
;
95 XMenuActivateSetWaitFunction (func
, data
)
104 XMenuActivate(display
, menu
, p_num
, s_num
, x_pos
, y_pos
, event_mask
, data
,
106 register Display
*display
; /* Display to put menu on. */
107 register XMenu
*menu
; /* Menu to activate. */
108 int *p_num
; /* Pane number selected. */
109 int *s_num
; /* Selection number selected. */
110 int x_pos
; /* X coordinate of menu position. */
111 int y_pos
; /* Y coordinate of menu position. */
112 unsigned int event_mask
; /* Mouse button event mask. */
113 char **data
; /* Pointer to return data value. */
114 void (* help_callback
) (); /* Help callback. */
116 int status
; /* X routine call status. */
117 int orig_x
; /* Upper left menu origin X coord. */
118 int orig_y
; /* Upper left menu origin Y coord. */
119 int ret_val
; /* Return value. */
121 register XMPane
*p_ptr
; /* Current XMPane. */
122 register XMPane
*event_xmp
; /* Event XMPane pointer. */
123 register XMPane
*cur_p
; /* Current pane. */
124 register XMSelect
*cur_s
; /* Current selection. */
125 XMWindow
*event_xmw
; /* Event XMWindow pointer. */
126 XEvent event
; /* X input event. */
127 XEvent peek_event
; /* X input peek ahead event. */
129 Bool selection
= False
; /* Selection has been made. */
130 Bool forward
= True
; /* Moving forward in the pane list. */
133 int root_x
, root_y
, win_x
, win_y
;
138 * Define and allocate a foreign event queue to hold events
139 * that don't belong to XMenu. These events are later restored
140 * to the X event queue.
142 typedef struct _xmeventque
{
144 struct _xmeventque
*next
;
147 XMEventQue
*feq
= NULL
; /* Foreign event queue. */
148 XMEventQue
*feq_tmp
; /* Foreign event queue temporary. */
151 * If there are no panes in the menu then return failure
152 * because the menu is not initialized.
154 if (menu
->p_count
== 0) {
155 _XMErrorCode
= XME_NOT_INIT
;
160 * Find the desired current pane.
162 cur_p
= _XMGetPanePtr(menu
, *p_num
);
166 cur_p
->activated
= cur_p
->active
;
169 * Find the desired current selection.
170 * If the current selection index is out of range a null current selection
171 * will be assumed and the cursor will be placed in the current pane
174 cur_s
= _XMGetSelectionPtr(cur_p
, *s_num
);
177 * Compute origin of menu so that cursor is in
178 * Correct pane and selection.
180 _XMTransToOrigin(display
,
185 menu
->x_pos
= orig_x
; /* Store X and Y coords of menu. */
186 menu
->y_pos
= orig_y
;
188 if (XMenuRecompute(display
, menu
) == XM_FAILURE
) {
193 * Flush the window creation queue.
194 * This batches all window creates since lazy evaluation
195 * is more efficient than individual evaluation.
196 * This routine also does an XFlush().
198 if (_XMWinQueFlush(display
, menu
, cur_p
, cur_s
) == _FAILURE
) {
203 * Make sure windows are in correct order (in case we were passed
204 * an already created menu in incorrect order.)
206 for(p_ptr
= menu
->p_list
->next
; p_ptr
!= cur_p
; p_ptr
= p_ptr
->next
)
207 XRaiseWindow(display
, p_ptr
->window
);
208 for(p_ptr
= menu
->p_list
->prev
; p_ptr
!= cur_p
->prev
; p_ptr
= p_ptr
->prev
)
209 XRaiseWindow(display
, p_ptr
->window
);
212 * Make sure all selection windows are mapped.
215 p_ptr
= menu
->p_list
->next
;
216 p_ptr
!= menu
->p_list
;
219 XMapSubwindows(display
, p_ptr
->window
);
223 * Synchronize the X buffers and the event queue.
224 * From here on, all events in the queue that don't belong to
225 * XMenu are sent back to the application via an application
226 * provided event handler or discarded if the application has
227 * not provided an event handler.
232 * Grab the mouse for menu input.
235 status
= XGrabPointer(
246 if (status
== Success
&& x_menu_grab_keyboard
)
248 status
= XGrabKeyboard (display
,
254 if (status
!= Success
)
255 XUngrabPointer(display
, CurrentTime
);
258 if (status
== _X_FAILURE
) {
259 _XMErrorCode
= XME_GRAB_MOUSE
;
264 * Map the menu panes.
266 XMapWindow(display
, cur_p
->window
);
267 for (p_ptr
= menu
->p_list
->next
;
270 XMapWindow(display
, p_ptr
->window
);
271 for (p_ptr
= cur_p
->next
;
272 p_ptr
!= menu
->p_list
;
274 XMapWindow(display
, p_ptr
->window
);
276 XRaiseWindow(display
, cur_p
->window
); /* Make sure current */
277 /* pane is on top. */
279 cur_s
= NULL
; /* Clear current selection. */
282 * Begin event processing loop.
285 if (wait_func
) (*wait_func
) (wait_data
);
286 XNextEvent(display
, &event
); /* Get next event. */
287 switch (event
.type
) { /* Dispatch on the event type. */
289 event_xmp
= (XMPane
*)XLookUpAssoc(display
,
291 event
.xexpose
.window
);
292 if (event_xmp
== NULL
) {
294 * If AEQ mode is enabled then queue the event.
297 feq_tmp
= (XMEventQue
*)malloc(sizeof(XMEventQue
));
298 if (feq_tmp
== NULL
) {
299 _XMErrorCode
= XME_CALLOC
;
302 feq_tmp
->event
= event
;
306 else if (_XMEventHandler
) (*_XMEventHandler
)(&event
);
309 if (event_xmp
->activated
) {
310 XSetWindowBackground(display
,
315 XSetWindowBackgroundPixmap(display
,
319 _XMRefreshPane(display
, menu
, event_xmp
);
323 * First wait a small period of time, and see
324 * if another EnterNotify event follows hard on the
325 * heels of this one. i.e., the user is simply
326 * "passing through". If so, ignore this one.
329 event_xmw
= (XMWindow
*)XLookUpAssoc(display
,
331 event
.xcrossing
.window
);
332 if (event_xmw
== NULL
) break;
333 if (event_xmw
->type
== SELECTION
) {
335 * We have entered a selection.
337 /* if (XPending(display) == 0) usleep(150000); */
338 if (XPending(display
) != 0) {
339 XPeekEvent(display
, &peek_event
);
340 if(peek_event
.type
== LeaveNotify
) {
344 cur_s
= (XMSelect
*)event_xmw
;
345 help_callback (cur_s
->help_string
,
346 cur_p
->serial
, cur_s
->serial
);
349 * If the pane we are in is active and the
350 * selection entered is active then activate
353 if (cur_p
->active
&& cur_s
->active
> 0) {
354 cur_s
->activated
= 1;
355 _XMRefreshSelection(display
, menu
, cur_s
);
360 * We have entered a pane.
362 /* if (XPending(display) == 0) usleep(150000); */
363 if (XPending(display
) != 0) {
364 XPeekEvent(display
, &peek_event
);
365 if (peek_event
.type
== EnterNotify
) break;
367 XQueryPointer(display
,
373 event_xmp
= (XMPane
*)XLookUpAssoc(display
,
376 if (event_xmp
== NULL
) break;
377 if (event_xmp
== cur_p
) break;
378 if (event_xmp
->serial
> cur_p
->serial
) forward
= True
;
379 else forward
= False
;
381 while (p_ptr
!= event_xmp
) {
382 if (forward
) p_ptr
= p_ptr
->next
;
383 else p_ptr
= p_ptr
->prev
;
384 XRaiseWindow(display
, p_ptr
->window
);
386 if (cur_p
->activated
) {
387 cur_p
->activated
= False
;
388 XSetWindowBackgroundPixmap(display
,
391 _XMRefreshPane(display
, menu
, cur_p
);
393 if (event_xmp
->active
) event_xmp
->activated
= True
;
396 * i suspect the we don't get an EXPOSE event when backing
397 * store is enabled; the menu windows content is probably
398 * not drawn in when it should be in that case.
399 * in that case, this is probably an ugly fix!
400 * i hope someone more familiar with this code would
401 * take it from here. -- caveh@eng.sun.com.
403 XSetWindowBackground(display
,
406 _XMRefreshPane(display
, menu
, event_xmp
);
412 event_xmw
= (XMWindow
*)XLookUpAssoc(
415 event
.xcrossing
.window
417 if (event_xmw
== NULL
) break;
418 if(cur_s
== NULL
) break;
421 * If the current selection was activated then
424 if (cur_s
->activated
) {
425 cur_s
->activated
= False
;
426 _XMRefreshSelection(display
, menu
, cur_s
);
433 *p_num
= cur_p
->serial
;
435 * Check to see if there is a current selection.
439 * Set the selection number to the current selection.
441 *s_num
= cur_s
->serial
;
443 * If the current selection was activated then
444 * we have a valid selection otherwise we have
445 * an inactive selection.
447 if (cur_s
->activated
) {
449 ret_val
= XM_SUCCESS
;
452 ret_val
= XM_IA_SELECT
;
457 * No selection was current.
459 ret_val
= XM_NO_SELECT
;
465 keysym
= XLookupKeysym (&event
.xkey
, 0);
467 /* Pop down on C-g and Escape. */
468 if ((keysym
== XK_g
&& (event
.xkey
.state
& ControlMask
) != 0)
469 || keysym
== XK_Escape
) /* Any escape, ignore modifiers. */
471 ret_val
= XM_NO_SELECT
;
477 * If AEQ mode is enabled then queue the event.
480 feq_tmp
= (XMEventQue
*)malloc(sizeof(XMEventQue
));
481 if (feq_tmp
== NULL
) {
482 _XMErrorCode
= XME_CALLOC
;
485 feq_tmp
->event
= event
;
489 else if (_XMEventHandler
) (*_XMEventHandler
)(&event
);
492 * If a selection has been made, break out of the event loop.
494 if (selection
== True
) break;
500 for ( p_ptr
= menu
->p_list
->next
;
501 p_ptr
!= menu
->p_list
;
504 XUnmapWindow(display
, p_ptr
->window
);
510 XUngrabPointer(display
, CurrentTime
);
511 XUngrabKeyboard(display
, CurrentTime
);
514 * Restore bits under where the menu was if we managed
515 * to save them and free the pixmap.
519 * If there is a current selection deactivate it.
521 if (cur_s
!= NULL
) cur_s
->activated
= 0;
524 * Deactivate the current pane.
526 cur_p
->activated
= 0;
527 XSetWindowBackgroundPixmap(display
, cur_p
->window
, menu
->inact_pixmap
);
530 * Synchronize the X buffers and the X event queue.
535 * Dispatch any events remaining on the queue.
537 while (QLength(display
)) {
539 * Fetch the next event.
541 XNextEvent(display
, &event
);
544 * Discard any events left on the queue that belong to XMenu.
545 * All others are held and then returned to the event queue.
547 switch (event
.type
) {
554 * Does this event belong to one of XMenu's windows?
555 * If so, discard it and process the next event.
556 * If not fall through and treat it as a foreign event.
558 event_xmp
= (XMPane
*)XLookUpAssoc(
563 if (event_xmp
!= NULL
) continue;
566 * This is a foreign event.
567 * Queue it for later return to the X event queue.
569 feq_tmp
= (XMEventQue
*)malloc(sizeof(XMEventQue
));
570 if (feq_tmp
== NULL
) {
571 _XMErrorCode
= XME_CALLOC
;
574 feq_tmp
->event
= event
;
580 * Return any foreign events that were queued to the X event queue.
582 while (feq
!= NULL
) {
584 XPutBackEvent(display
, &feq_tmp
->event
);
586 free((char *)feq_tmp
);
592 * Return successfully.
594 _XMErrorCode
= XME_NO_ERROR
;
599 /* arch-tag: 6b90b578-ecea-4328-b460-a0c96963f872
600 (do not change this comment) */