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;
88 typedef void (*Wait_func
)();
90 static Wait_func wait_func
;
91 static void* wait_data
;
94 XMenuActivateSetWaitFunction (func
, data
)
103 XMenuActivate(display
, menu
, p_num
, s_num
, x_pos
, y_pos
, event_mask
, data
,
105 register Display
*display
; /* Display to put menu on. */
106 register XMenu
*menu
; /* Menu to activate. */
107 int *p_num
; /* Pane number selected. */
108 int *s_num
; /* Selection number selected. */
109 int x_pos
; /* X coordinate of menu position. */
110 int y_pos
; /* Y coordinate of menu position. */
111 unsigned int event_mask
; /* Mouse button event mask. */
112 char **data
; /* Pointer to return data value. */
113 void (* help_callback
) (); /* Help callback. */
115 int status
; /* X routine call status. */
116 int orig_x
; /* Upper left menu origin X coord. */
117 int orig_y
; /* Upper left menu origin Y coord. */
118 int ret_val
; /* Return value. */
120 register XMPane
*p_ptr
; /* Current XMPane. */
121 register XMPane
*event_xmp
; /* Event XMPane pointer. */
122 register XMPane
*cur_p
; /* Current pane. */
123 register XMSelect
*cur_s
; /* Current selection. */
124 XMWindow
*event_xmw
; /* Event XMWindow pointer. */
125 XEvent event
; /* X input event. */
126 XEvent peek_event
; /* X input peek ahead event. */
128 Bool selection
= False
; /* Selection has been made. */
129 Bool forward
= True
; /* Moving forward in the pane list. */
132 int root_x
, root_y
, win_x
, win_y
;
136 * Define and allocate a foreign event queue to hold events
137 * that don't belong to XMenu. These events are later restored
138 * to the X event queue.
140 typedef struct _xmeventque
{
142 struct _xmeventque
*next
;
145 XMEventQue
*feq
= NULL
; /* Foreign event queue. */
146 XMEventQue
*feq_tmp
; /* Foreign event queue temporary. */
149 * If there are no panes in the menu then return failure
150 * because the menu is not initialized.
152 if (menu
->p_count
== 0) {
153 _XMErrorCode
= XME_NOT_INIT
;
158 * Find the desired current pane.
160 cur_p
= _XMGetPanePtr(menu
, *p_num
);
164 cur_p
->activated
= cur_p
->active
;
167 * Find the desired current selection.
168 * If the current selection index is out of range a null current selection
169 * will be assumed and the cursor will be placed in the current pane
172 cur_s
= _XMGetSelectionPtr(cur_p
, *s_num
);
175 * Compute origin of menu so that cursor is in
176 * Correct pane and selection.
178 _XMTransToOrigin(display
,
183 menu
->x_pos
= orig_x
; /* Store X and Y coords of menu. */
184 menu
->y_pos
= orig_y
;
186 if (XMenuRecompute(display
, menu
) == XM_FAILURE
) {
191 * Flush the window creation queue.
192 * This batches all window creates since lazy evaluation
193 * is more efficient than individual evaluation.
194 * This routine also does an XFlush().
196 if (_XMWinQueFlush(display
, menu
, cur_p
, cur_s
) == _FAILURE
) {
201 * Make sure windows are in correct order (in case we were passed
202 * an already created menu in incorrect order.)
204 for(p_ptr
= menu
->p_list
->next
; p_ptr
!= cur_p
; p_ptr
= p_ptr
->next
)
205 XRaiseWindow(display
, p_ptr
->window
);
206 for(p_ptr
= menu
->p_list
->prev
; p_ptr
!= cur_p
->prev
; p_ptr
= p_ptr
->prev
)
207 XRaiseWindow(display
, p_ptr
->window
);
210 * Make sure all selection windows are mapped.
213 p_ptr
= menu
->p_list
->next
;
214 p_ptr
!= menu
->p_list
;
217 XMapSubwindows(display
, p_ptr
->window
);
221 * Synchronize the X buffers and the event queue.
222 * From here on, all events in the queue that don't belong to
223 * XMenu are sent back to the application via an application
224 * provided event handler or discarded if the application has
225 * not provided an event handler.
230 * Grab the mouse for menu input.
233 status
= XGrabPointer(
244 if (status
== Success
&& x_menu_grab_keyboard
)
246 status
= XGrabKeyboard (display
,
252 if (status
!= Success
)
253 XUngrabPointer(display
, CurrentTime
);
256 if (status
== _X_FAILURE
) {
257 _XMErrorCode
= XME_GRAB_MOUSE
;
262 * Map the menu panes.
264 XMapWindow(display
, cur_p
->window
);
265 for (p_ptr
= menu
->p_list
->next
;
268 XMapWindow(display
, p_ptr
->window
);
269 for (p_ptr
= cur_p
->next
;
270 p_ptr
!= menu
->p_list
;
272 XMapWindow(display
, p_ptr
->window
);
274 XRaiseWindow(display
, cur_p
->window
); /* Make sure current */
275 /* pane is on top. */
277 cur_s
= NULL
; /* Clear current selection. */
280 * Begin event processing loop.
283 if (wait_func
) (*wait_func
) (wait_data
);
284 XNextEvent(display
, &event
); /* Get next event. */
285 switch (event
.type
) { /* Dispatch on the event type. */
287 event_xmp
= (XMPane
*)XLookUpAssoc(display
,
289 event
.xexpose
.window
);
290 if (event_xmp
== NULL
) {
292 * If AEQ mode is enabled then queue the event.
295 feq_tmp
= (XMEventQue
*)malloc(sizeof(XMEventQue
));
296 if (feq_tmp
== NULL
) {
297 _XMErrorCode
= XME_CALLOC
;
300 feq_tmp
->event
= event
;
304 else if (_XMEventHandler
) (*_XMEventHandler
)(&event
);
307 if (event_xmp
->activated
) {
308 XSetWindowBackground(display
,
313 XSetWindowBackgroundPixmap(display
,
317 _XMRefreshPane(display
, menu
, event_xmp
);
321 * First wait a small period of time, and see
322 * if another EnterNotify event follows hard on the
323 * heels of this one. i.e., the user is simply
324 * "passing through". If so, ignore this one.
327 event_xmw
= (XMWindow
*)XLookUpAssoc(display
,
329 event
.xcrossing
.window
);
330 if (event_xmw
== NULL
) break;
331 if (event_xmw
->type
== SELECTION
) {
333 * We have entered a selection.
335 /* if (XPending(display) == 0) usleep(150000); */
336 if (XPending(display
) != 0) {
337 XPeekEvent(display
, &peek_event
);
338 if(peek_event
.type
== LeaveNotify
) {
342 cur_s
= (XMSelect
*)event_xmw
;
343 help_callback (cur_s
->help_string
,
344 cur_p
->serial
, cur_s
->serial
);
347 * If the pane we are in is active and the
348 * selection entered is active then activate
351 if (cur_p
->active
&& cur_s
->active
> 0) {
352 cur_s
->activated
= 1;
353 _XMRefreshSelection(display
, menu
, cur_s
);
358 * We have entered a pane.
360 /* if (XPending(display) == 0) usleep(150000); */
361 if (XPending(display
) != 0) {
362 XPeekEvent(display
, &peek_event
);
363 if (peek_event
.type
== EnterNotify
) break;
365 XQueryPointer(display
,
371 event_xmp
= (XMPane
*)XLookUpAssoc(display
,
374 if (event_xmp
== NULL
) break;
375 if (event_xmp
== cur_p
) break;
376 if (event_xmp
->serial
> cur_p
->serial
) forward
= True
;
377 else forward
= False
;
379 while (p_ptr
!= event_xmp
) {
380 if (forward
) p_ptr
= p_ptr
->next
;
381 else p_ptr
= p_ptr
->prev
;
382 XRaiseWindow(display
, p_ptr
->window
);
384 if (cur_p
->activated
) {
385 cur_p
->activated
= False
;
386 XSetWindowBackgroundPixmap(display
,
389 _XMRefreshPane(display
, menu
, cur_p
);
391 if (event_xmp
->active
) event_xmp
->activated
= True
;
394 * i suspect the we don't get an EXPOSE event when backing
395 * store is enabled; the menu windows content is probably
396 * not drawn in when it should be in that case.
397 * in that case, this is probably an ugly fix!
398 * i hope someone more familiar with this code would
399 * take it from here. -- caveh@eng.sun.com.
401 XSetWindowBackground(display
,
404 _XMRefreshPane(display
, menu
, event_xmp
);
410 event_xmw
= (XMWindow
*)XLookUpAssoc(
413 event
.xcrossing
.window
415 if (event_xmw
== NULL
) break;
416 if(cur_s
== NULL
) break;
419 * If the current selection was activated then
422 if (cur_s
->activated
) {
423 cur_s
->activated
= False
;
424 _XMRefreshSelection(display
, menu
, cur_s
);
431 *p_num
= cur_p
->serial
;
433 * Check to see if there is a current selection.
437 * Set the selection number to the current selection.
439 *s_num
= cur_s
->serial
;
441 * If the current selection was activated then
442 * we have a valid selection otherwise we have
443 * an inactive selection.
445 if (cur_s
->activated
) {
447 ret_val
= XM_SUCCESS
;
450 ret_val
= XM_IA_SELECT
;
455 * No selection was current.
457 ret_val
= XM_NO_SELECT
;
463 * If AEQ mode is enabled then queue the event.
466 feq_tmp
= (XMEventQue
*)malloc(sizeof(XMEventQue
));
467 if (feq_tmp
== NULL
) {
468 _XMErrorCode
= XME_CALLOC
;
471 feq_tmp
->event
= event
;
475 else if (_XMEventHandler
) (*_XMEventHandler
)(&event
);
478 * If a selection has been made, break out of the event loop.
480 if (selection
== True
) break;
486 for ( p_ptr
= menu
->p_list
->next
;
487 p_ptr
!= menu
->p_list
;
490 XUnmapWindow(display
, p_ptr
->window
);
496 XUngrabPointer(display
, CurrentTime
);
497 XUngrabKeyboard(display
, CurrentTime
);
500 * Restore bits under where the menu was if we managed
501 * to save them and free the pixmap.
505 * If there is a current selection deactivate it.
507 if (cur_s
!= NULL
) cur_s
->activated
= 0;
510 * Deactivate the current pane.
512 cur_p
->activated
= 0;
513 XSetWindowBackgroundPixmap(display
, cur_p
->window
, menu
->inact_pixmap
);
516 * Synchronize the X buffers and the X event queue.
521 * Dispatch any events remaining on the queue.
523 while (QLength(display
)) {
525 * Fetch the next event.
527 XNextEvent(display
, &event
);
530 * Discard any events left on the queue that belong to XMenu.
531 * All others are held and then returned to the event queue.
533 switch (event
.type
) {
540 * Does this event belong to one of XMenu's windows?
541 * If so, discard it and process the next event.
542 * If not fall through and treat it as a foreign event.
544 event_xmp
= (XMPane
*)XLookUpAssoc(
549 if (event_xmp
!= NULL
) continue;
552 * This is a foreign event.
553 * Queue it for later return to the X event queue.
555 feq_tmp
= (XMEventQue
*)malloc(sizeof(XMEventQue
));
556 if (feq_tmp
== NULL
) {
557 _XMErrorCode
= XME_CALLOC
;
560 feq_tmp
->event
= event
;
566 * Return any foreign events that were queued to the X event queue.
568 while (feq
!= NULL
) {
570 XPutBackEvent(display
, &feq_tmp
->event
);
572 free((char *)feq_tmp
);
578 * Return successfully.
580 _XMErrorCode
= XME_NO_ERROR
;
585 /* arch-tag: 6b90b578-ecea-4328-b460-a0c96963f872
586 (do not change this comment) */