1 /* Copyright Massachusetts Institute of Technology 1985 */
2 /* Copyright (C) 2002, 2003, 2004, 2005,
3 2006 Free Software Foundation, Inc. */
8 * XMenu: MIT Project Athena, X Window system menu package
10 * XMenuActivate - Maps a given menu to the display and activates
11 * the menu for user selection. The user is allowed to
12 * specify which pane and selection will be current,
13 * the X and Y location of the menu (relative to the
14 * parent window) and the mouse button event mask that
15 * will be used to identify a selection request.
17 * A menu selection is shown to be current by placing
18 * a highlight box around the selection as the mouse
19 * cursor enters its active region. Inactive selections
20 * will not be highlighted. As the mouse cursor moved
21 * from one menu pane to another menu pane the pane being
22 * entered is raised and made current and the pane being
25 * Anytime XMenuActivate returns, the p_num and
26 * s_num are left at their last known values (i.e.,
27 * the last known current pane and selection indices).
28 * The following are the defined return states:
30 * 1) If at any time an error occurs the data
31 * pointer is left untouched and XM_FAILURE
34 * 2) When a selection request is received (i.e.,
35 * when the specified mouse event occurs) the
36 * data pointer will be set to the data
37 * associated with the particular selection
38 * current at the time of the selection request
39 * and XM_SUCCESS is returned.
41 * 3) If no selection was current at the time a
42 * selection request is made the data pointer
43 * will be left untouched and XM_NO_SELECT will
46 * 4) If the selection that was current at the time
47 * a selection request is made is not an active
48 * selection the data pointer will be left
49 * untouched and XM_IA_SELECT will be returned.
51 * Since X processes events in an asynchronous manner
52 * it is likely that XMenuActivate will encounter
53 * a "foreign event" while it is executing. Foreign
54 * events are handled in one of three ways:
56 * 1) The event is discarded. This is the default
57 * mode and requires no action on the part of the
60 * 2) The application has identified an asynchronous
61 * event handler that will be called and the
62 * foreign event handed off to it. Note:
63 * AEQ mode disables this mode temporarily.
65 * 3) The application has enabled asynchronous event
66 * queuing mode. In this mode all foreign events
67 * will be queued up untill XMenuActivate
68 * terminates; at which time they will be
69 * returned to the X event queue. As long as
70 * AEQ mode is enabled any asynchronous event
71 * handler as temporarily disabled.
73 * Any events encountered while taking down the menu
74 * (i.e., exposure events from occluded windows) will
75 * automatically be returned to the X event queue after
76 * XMenuActivate has cleaned the queue of any of its own
77 * events that are no longer needed.
79 * Author: Tony Della Fera, DEC
86 #include <X11/keysym.h>
88 /* For debug, set this to 0 to not grab the keyboard on menu popup */
89 int x_menu_grab_keyboard
= 1;
91 typedef void (*Wait_func
)();
93 static Wait_func wait_func
;
94 static void* wait_data
;
97 XMenuActivateSetWaitFunction (func
, data
)
106 XMenuActivate(display
, menu
, p_num
, s_num
, x_pos
, y_pos
, event_mask
, data
,
108 register Display
*display
; /* Display to put menu on. */
109 register XMenu
*menu
; /* Menu to activate. */
110 int *p_num
; /* Pane number selected. */
111 int *s_num
; /* Selection number selected. */
112 int x_pos
; /* X coordinate of menu position. */
113 int y_pos
; /* Y coordinate of menu position. */
114 unsigned int event_mask
; /* Mouse button event mask. */
115 char **data
; /* Pointer to return data value. */
116 void (* help_callback
) (); /* Help callback. */
118 int status
; /* X routine call status. */
119 int orig_x
; /* Upper left menu origin X coord. */
120 int orig_y
; /* Upper left menu origin Y coord. */
121 int ret_val
; /* Return value. */
123 register XMPane
*p_ptr
; /* Current XMPane. */
124 register XMPane
*event_xmp
; /* Event XMPane pointer. */
125 register XMPane
*cur_p
; /* Current pane. */
126 register XMSelect
*cur_s
; /* Current selection. */
127 XMWindow
*event_xmw
; /* Event XMWindow pointer. */
128 XEvent event
; /* X input event. */
129 XEvent peek_event
; /* X input peek ahead event. */
131 Bool selection
= False
; /* Selection has been made. */
132 Bool forward
= True
; /* Moving forward in the pane list. */
135 int root_x
, root_y
, win_x
, win_y
;
140 * Define and allocate a foreign event queue to hold events
141 * that don't belong to XMenu. These events are later restored
142 * to the X event queue.
144 typedef struct _xmeventque
{
146 struct _xmeventque
*next
;
149 XMEventQue
*feq
= NULL
; /* Foreign event queue. */
150 XMEventQue
*feq_tmp
; /* Foreign event queue temporary. */
153 * If there are no panes in the menu then return failure
154 * because the menu is not initialized.
156 if (menu
->p_count
== 0) {
157 _XMErrorCode
= XME_NOT_INIT
;
162 * Find the desired current pane.
164 cur_p
= _XMGetPanePtr(menu
, *p_num
);
168 cur_p
->activated
= cur_p
->active
;
171 * Find the desired current selection.
172 * If the current selection index is out of range a null current selection
173 * will be assumed and the cursor will be placed in the current pane
176 cur_s
= _XMGetSelectionPtr(cur_p
, *s_num
);
179 * Compute origin of menu so that cursor is in
180 * Correct pane and selection.
182 _XMTransToOrigin(display
,
187 menu
->x_pos
= orig_x
; /* Store X and Y coords of menu. */
188 menu
->y_pos
= orig_y
;
190 if (XMenuRecompute(display
, menu
) == XM_FAILURE
) {
195 * Flush the window creation queue.
196 * This batches all window creates since lazy evaluation
197 * is more efficient than individual evaluation.
198 * This routine also does an XFlush().
200 if (_XMWinQueFlush(display
, menu
, cur_p
, cur_s
) == _FAILURE
) {
205 * Make sure windows are in correct order (in case we were passed
206 * an already created menu in incorrect order.)
208 for(p_ptr
= menu
->p_list
->next
; p_ptr
!= cur_p
; p_ptr
= p_ptr
->next
)
209 XRaiseWindow(display
, p_ptr
->window
);
210 for(p_ptr
= menu
->p_list
->prev
; p_ptr
!= cur_p
->prev
; p_ptr
= p_ptr
->prev
)
211 XRaiseWindow(display
, p_ptr
->window
);
214 * Make sure all selection windows are mapped.
217 p_ptr
= menu
->p_list
->next
;
218 p_ptr
!= menu
->p_list
;
221 XMapSubwindows(display
, p_ptr
->window
);
225 * Synchronize the X buffers and the event queue.
226 * From here on, all events in the queue that don't belong to
227 * XMenu are sent back to the application via an application
228 * provided event handler or discarded if the application has
229 * not provided an event handler.
234 * Grab the mouse for menu input.
237 status
= XGrabPointer(
248 if (status
== Success
&& x_menu_grab_keyboard
)
250 status
= XGrabKeyboard (display
,
256 if (status
!= Success
)
257 XUngrabPointer(display
, CurrentTime
);
260 if (status
== _X_FAILURE
) {
261 _XMErrorCode
= XME_GRAB_MOUSE
;
266 * Map the menu panes.
268 XMapWindow(display
, cur_p
->window
);
269 for (p_ptr
= menu
->p_list
->next
;
272 XMapWindow(display
, p_ptr
->window
);
273 for (p_ptr
= cur_p
->next
;
274 p_ptr
!= menu
->p_list
;
276 XMapWindow(display
, p_ptr
->window
);
278 XRaiseWindow(display
, cur_p
->window
); /* Make sure current */
279 /* pane is on top. */
281 cur_s
= NULL
; /* Clear current selection. */
284 * Begin event processing loop.
287 if (wait_func
) (*wait_func
) (wait_data
);
288 XNextEvent(display
, &event
); /* Get next event. */
289 switch (event
.type
) { /* Dispatch on the event type. */
291 event_xmp
= (XMPane
*)XLookUpAssoc(display
,
293 event
.xexpose
.window
);
294 if (event_xmp
== NULL
) {
296 * If AEQ mode is enabled then queue the event.
299 feq_tmp
= (XMEventQue
*)malloc(sizeof(XMEventQue
));
300 if (feq_tmp
== NULL
) {
301 _XMErrorCode
= XME_CALLOC
;
304 feq_tmp
->event
= event
;
308 else if (_XMEventHandler
) (*_XMEventHandler
)(&event
);
311 if (event_xmp
->activated
) {
312 XSetWindowBackground(display
,
317 XSetWindowBackgroundPixmap(display
,
321 _XMRefreshPane(display
, menu
, event_xmp
);
325 * First wait a small period of time, and see
326 * if another EnterNotify event follows hard on the
327 * heels of this one. i.e., the user is simply
328 * "passing through". If so, ignore this one.
331 event_xmw
= (XMWindow
*)XLookUpAssoc(display
,
333 event
.xcrossing
.window
);
334 if (event_xmw
== NULL
) break;
335 if (event_xmw
->type
== SELECTION
) {
337 * We have entered a selection.
339 /* if (XPending(display) == 0) usleep(150000); */
340 if (XPending(display
) != 0) {
341 XPeekEvent(display
, &peek_event
);
342 if(peek_event
.type
== LeaveNotify
) {
346 cur_s
= (XMSelect
*)event_xmw
;
347 help_callback (cur_s
->help_string
,
348 cur_p
->serial
, cur_s
->serial
);
351 * If the pane we are in is active and the
352 * selection entered is active then activate
355 if (cur_p
->active
&& cur_s
->active
> 0) {
356 cur_s
->activated
= 1;
357 _XMRefreshSelection(display
, menu
, cur_s
);
362 * We have entered a pane.
364 /* if (XPending(display) == 0) usleep(150000); */
365 if (XPending(display
) != 0) {
366 XPeekEvent(display
, &peek_event
);
367 if (peek_event
.type
== EnterNotify
) break;
369 XQueryPointer(display
,
375 event_xmp
= (XMPane
*)XLookUpAssoc(display
,
378 if (event_xmp
== NULL
) break;
379 if (event_xmp
== cur_p
) break;
380 if (event_xmp
->serial
> cur_p
->serial
) forward
= True
;
381 else forward
= False
;
383 while (p_ptr
!= event_xmp
) {
384 if (forward
) p_ptr
= p_ptr
->next
;
385 else p_ptr
= p_ptr
->prev
;
386 XRaiseWindow(display
, p_ptr
->window
);
388 if (cur_p
->activated
) {
389 cur_p
->activated
= False
;
390 XSetWindowBackgroundPixmap(display
,
393 _XMRefreshPane(display
, menu
, cur_p
);
395 if (event_xmp
->active
) event_xmp
->activated
= True
;
398 * i suspect the we don't get an EXPOSE event when backing
399 * store is enabled; the menu windows content is probably
400 * not drawn in when it should be in that case.
401 * in that case, this is probably an ugly fix!
402 * i hope someone more familiar with this code would
403 * take it from here. -- caveh@eng.sun.com.
405 XSetWindowBackground(display
,
408 _XMRefreshPane(display
, menu
, event_xmp
);
414 event_xmw
= (XMWindow
*)XLookUpAssoc(
417 event
.xcrossing
.window
419 if (event_xmw
== NULL
) break;
420 if(cur_s
== NULL
) break;
423 * If the current selection was activated then
426 if (cur_s
->activated
) {
427 cur_s
->activated
= False
;
428 _XMRefreshSelection(display
, menu
, cur_s
);
435 *p_num
= cur_p
->serial
;
437 * Check to see if there is a current selection.
441 * Set the selection number to the current selection.
443 *s_num
= cur_s
->serial
;
445 * If the current selection was activated then
446 * we have a valid selection otherwise we have
447 * an inactive selection.
449 if (cur_s
->activated
) {
451 ret_val
= XM_SUCCESS
;
454 ret_val
= XM_IA_SELECT
;
459 * No selection was current.
461 ret_val
= XM_NO_SELECT
;
467 keysym
= XLookupKeysym (&event
.xkey
, 0);
469 /* Pop down on C-g and Escape. */
470 if ((keysym
== XK_g
&& (event
.xkey
.state
& ControlMask
) != 0)
471 || keysym
== XK_Escape
) /* Any escape, ignore modifiers. */
473 ret_val
= XM_NO_SELECT
;
479 * If AEQ mode is enabled then queue the event.
482 feq_tmp
= (XMEventQue
*)malloc(sizeof(XMEventQue
));
483 if (feq_tmp
== NULL
) {
484 _XMErrorCode
= XME_CALLOC
;
487 feq_tmp
->event
= event
;
491 else if (_XMEventHandler
) (*_XMEventHandler
)(&event
);
494 * If a selection has been made, break out of the event loop.
496 if (selection
== True
) break;
502 for ( p_ptr
= menu
->p_list
->next
;
503 p_ptr
!= menu
->p_list
;
506 XUnmapWindow(display
, p_ptr
->window
);
512 XUngrabPointer(display
, CurrentTime
);
513 XUngrabKeyboard(display
, CurrentTime
);
516 * Restore bits under where the menu was if we managed
517 * to save them and free the pixmap.
521 * If there is a current selection deactivate it.
523 if (cur_s
!= NULL
) cur_s
->activated
= 0;
526 * Deactivate the current pane.
528 cur_p
->activated
= 0;
529 XSetWindowBackgroundPixmap(display
, cur_p
->window
, menu
->inact_pixmap
);
532 * Synchronize the X buffers and the X event queue.
537 * Dispatch any events remaining on the queue.
539 while (QLength(display
)) {
541 * Fetch the next event.
543 XNextEvent(display
, &event
);
546 * Discard any events left on the queue that belong to XMenu.
547 * All others are held and then returned to the event queue.
549 switch (event
.type
) {
556 * Does this event belong to one of XMenu's windows?
557 * If so, discard it and process the next event.
558 * If not fall through and treat it as a foreign event.
560 event_xmp
= (XMPane
*)XLookUpAssoc(
565 if (event_xmp
!= NULL
) continue;
568 * This is a foreign event.
569 * Queue it for later return to the X event queue.
571 feq_tmp
= (XMEventQue
*)malloc(sizeof(XMEventQue
));
572 if (feq_tmp
== NULL
) {
573 _XMErrorCode
= XME_CALLOC
;
576 feq_tmp
->event
= event
;
582 * Return any foreign events that were queued to the X event queue.
584 while (feq
!= NULL
) {
586 XPutBackEvent(display
, &feq_tmp
->event
);
588 free((char *)feq_tmp
);
594 * Return successfully.
596 _XMErrorCode
= XME_NO_ERROR
;
601 /* arch-tag: 6b90b578-ecea-4328-b460-a0c96963f872
602 (do not change this comment) */