1 /* Copyright Massachusetts Institute of Technology 1985 */
2 /* Copyright (C) 2002, 2003, 2004, 2005 Free Software Foundation, Inc. */
7 * XMenu: MIT Project Athena, X Window system menu package
9 * XMenuActivate - Maps a given menu to the display and activates
10 * the menu for user selection. The user is allowed to
11 * specify which pane and selection will be current,
12 * the X and Y location of the menu (relative to the
13 * parent window) and the mouse button event mask that
14 * will be used to identify a selection request.
16 * A menu selection is shown to be current by placing
17 * a highlight box around the selection as the mouse
18 * cursor enters its active region. Inactive selections
19 * will not be highlighted. As the mouse cursor moved
20 * from one menu pane to another menu pane the pane being
21 * entered is raised and made current and the pane being
24 * Anytime XMenuActivate returns, the p_num and
25 * s_num are left at their last known values (i.e.,
26 * the last known current pane and selection indices).
27 * The following are the defined return states:
29 * 1) If at any time an error occurs the data
30 * pointer is left untouched and XM_FAILURE
33 * 2) When a selection request is received (i.e.,
34 * when the specified mouse event occurs) the
35 * data pointer will be set to the data
36 * associated with the particular selection
37 * current at the time of the selection request
38 * and XM_SUCCESS is returned.
40 * 3) If no selection was current at the time a
41 * selection request is made the data pointer
42 * will be left untouched and XM_NO_SELECT will
45 * 4) If the selection that was current at the time
46 * a selection request is made is not an active
47 * selection the data pointer will be left
48 * untouched and XM_IA_SELECT will be returned.
50 * Since X processes events in an asynchronous manner
51 * it is likely that XMenuActivate will encounter
52 * a "foreign event" while it is executing. Foreign
53 * events are handled in one of three ways:
55 * 1) The event is discarded. This is the default
56 * mode and requires no action on the part of the
59 * 2) The application has identified an asynchronous
60 * event handler that will be called and the
61 * foreign event handed off to it. Note:
62 * AEQ mode disables this mode temporarily.
64 * 3) The application has enabled asynchronous event
65 * queuing mode. In this mode all foreign events
66 * will be queued up untill XMenuActivate
67 * terminates; at which time they will be
68 * returned to the X event queue. As long as
69 * AEQ mode is enabled any asynchronous event
70 * handler as temporarily disabled.
72 * Any events encountered while taking down the menu
73 * (i.e., exposure events from occluded windows) will
74 * automatically be returned to the X event queue after
75 * XMenuActivate has cleaned the queue of any of its own
76 * events that are no longer needed.
78 * Author: Tony Della Fera, DEC
85 #include <X11/keysym.h>
87 /* For debug, set this to 0 to not grab the keyboard on menu popup */
88 int x_menu_grab_keyboard
= 1;
90 typedef void (*Wait_func
)();
92 static Wait_func wait_func
;
93 static void* wait_data
;
96 XMenuActivateSetWaitFunction (func
, data
)
105 XMenuActivate(display
, menu
, p_num
, s_num
, x_pos
, y_pos
, event_mask
, data
,
107 register Display
*display
; /* Display to put menu on. */
108 register XMenu
*menu
; /* Menu to activate. */
109 int *p_num
; /* Pane number selected. */
110 int *s_num
; /* Selection number selected. */
111 int x_pos
; /* X coordinate of menu position. */
112 int y_pos
; /* Y coordinate of menu position. */
113 unsigned int event_mask
; /* Mouse button event mask. */
114 char **data
; /* Pointer to return data value. */
115 void (* help_callback
) (); /* Help callback. */
117 int status
; /* X routine call status. */
118 int orig_x
; /* Upper left menu origin X coord. */
119 int orig_y
; /* Upper left menu origin Y coord. */
120 int ret_val
; /* Return value. */
122 register XMPane
*p_ptr
; /* Current XMPane. */
123 register XMPane
*event_xmp
; /* Event XMPane pointer. */
124 register XMPane
*cur_p
; /* Current pane. */
125 register XMSelect
*cur_s
; /* Current selection. */
126 XMWindow
*event_xmw
; /* Event XMWindow pointer. */
127 XEvent event
; /* X input event. */
128 XEvent peek_event
; /* X input peek ahead event. */
130 Bool selection
= False
; /* Selection has been made. */
131 Bool forward
= True
; /* Moving forward in the pane list. */
134 int root_x
, root_y
, win_x
, win_y
;
139 * Define and allocate a foreign event queue to hold events
140 * that don't belong to XMenu. These events are later restored
141 * to the X event queue.
143 typedef struct _xmeventque
{
145 struct _xmeventque
*next
;
148 XMEventQue
*feq
= NULL
; /* Foreign event queue. */
149 XMEventQue
*feq_tmp
; /* Foreign event queue temporary. */
152 * If there are no panes in the menu then return failure
153 * because the menu is not initialized.
155 if (menu
->p_count
== 0) {
156 _XMErrorCode
= XME_NOT_INIT
;
161 * Find the desired current pane.
163 cur_p
= _XMGetPanePtr(menu
, *p_num
);
167 cur_p
->activated
= cur_p
->active
;
170 * Find the desired current selection.
171 * If the current selection index is out of range a null current selection
172 * will be assumed and the cursor will be placed in the current pane
175 cur_s
= _XMGetSelectionPtr(cur_p
, *s_num
);
178 * Compute origin of menu so that cursor is in
179 * Correct pane and selection.
181 _XMTransToOrigin(display
,
186 menu
->x_pos
= orig_x
; /* Store X and Y coords of menu. */
187 menu
->y_pos
= orig_y
;
189 if (XMenuRecompute(display
, menu
) == XM_FAILURE
) {
194 * Flush the window creation queue.
195 * This batches all window creates since lazy evaluation
196 * is more efficient than individual evaluation.
197 * This routine also does an XFlush().
199 if (_XMWinQueFlush(display
, menu
, cur_p
, cur_s
) == _FAILURE
) {
204 * Make sure windows are in correct order (in case we were passed
205 * an already created menu in incorrect order.)
207 for(p_ptr
= menu
->p_list
->next
; p_ptr
!= cur_p
; p_ptr
= p_ptr
->next
)
208 XRaiseWindow(display
, p_ptr
->window
);
209 for(p_ptr
= menu
->p_list
->prev
; p_ptr
!= cur_p
->prev
; p_ptr
= p_ptr
->prev
)
210 XRaiseWindow(display
, p_ptr
->window
);
213 * Make sure all selection windows are mapped.
216 p_ptr
= menu
->p_list
->next
;
217 p_ptr
!= menu
->p_list
;
220 XMapSubwindows(display
, p_ptr
->window
);
224 * Synchronize the X buffers and the event queue.
225 * From here on, all events in the queue that don't belong to
226 * XMenu are sent back to the application via an application
227 * provided event handler or discarded if the application has
228 * not provided an event handler.
233 * Grab the mouse for menu input.
236 status
= XGrabPointer(
247 if (status
== Success
&& x_menu_grab_keyboard
)
249 status
= XGrabKeyboard (display
,
255 if (status
!= Success
)
256 XUngrabPointer(display
, CurrentTime
);
259 if (status
== _X_FAILURE
) {
260 _XMErrorCode
= XME_GRAB_MOUSE
;
265 * Map the menu panes.
267 XMapWindow(display
, cur_p
->window
);
268 for (p_ptr
= menu
->p_list
->next
;
271 XMapWindow(display
, p_ptr
->window
);
272 for (p_ptr
= cur_p
->next
;
273 p_ptr
!= menu
->p_list
;
275 XMapWindow(display
, p_ptr
->window
);
277 XRaiseWindow(display
, cur_p
->window
); /* Make sure current */
278 /* pane is on top. */
280 cur_s
= NULL
; /* Clear current selection. */
283 * Begin event processing loop.
286 if (wait_func
) (*wait_func
) (wait_data
);
287 XNextEvent(display
, &event
); /* Get next event. */
288 switch (event
.type
) { /* Dispatch on the event type. */
290 event_xmp
= (XMPane
*)XLookUpAssoc(display
,
292 event
.xexpose
.window
);
293 if (event_xmp
== NULL
) {
295 * If AEQ mode is enabled then queue the event.
298 feq_tmp
= (XMEventQue
*)malloc(sizeof(XMEventQue
));
299 if (feq_tmp
== NULL
) {
300 _XMErrorCode
= XME_CALLOC
;
303 feq_tmp
->event
= event
;
307 else if (_XMEventHandler
) (*_XMEventHandler
)(&event
);
310 if (event_xmp
->activated
) {
311 XSetWindowBackground(display
,
316 XSetWindowBackgroundPixmap(display
,
320 _XMRefreshPane(display
, menu
, event_xmp
);
324 * First wait a small period of time, and see
325 * if another EnterNotify event follows hard on the
326 * heels of this one. i.e., the user is simply
327 * "passing through". If so, ignore this one.
330 event_xmw
= (XMWindow
*)XLookUpAssoc(display
,
332 event
.xcrossing
.window
);
333 if (event_xmw
== NULL
) break;
334 if (event_xmw
->type
== SELECTION
) {
336 * We have entered a selection.
338 /* if (XPending(display) == 0) usleep(150000); */
339 if (XPending(display
) != 0) {
340 XPeekEvent(display
, &peek_event
);
341 if(peek_event
.type
== LeaveNotify
) {
345 cur_s
= (XMSelect
*)event_xmw
;
346 help_callback (cur_s
->help_string
,
347 cur_p
->serial
, cur_s
->serial
);
350 * If the pane we are in is active and the
351 * selection entered is active then activate
354 if (cur_p
->active
&& cur_s
->active
> 0) {
355 cur_s
->activated
= 1;
356 _XMRefreshSelection(display
, menu
, cur_s
);
361 * We have entered a pane.
363 /* if (XPending(display) == 0) usleep(150000); */
364 if (XPending(display
) != 0) {
365 XPeekEvent(display
, &peek_event
);
366 if (peek_event
.type
== EnterNotify
) break;
368 XQueryPointer(display
,
374 event_xmp
= (XMPane
*)XLookUpAssoc(display
,
377 if (event_xmp
== NULL
) break;
378 if (event_xmp
== cur_p
) break;
379 if (event_xmp
->serial
> cur_p
->serial
) forward
= True
;
380 else forward
= False
;
382 while (p_ptr
!= event_xmp
) {
383 if (forward
) p_ptr
= p_ptr
->next
;
384 else p_ptr
= p_ptr
->prev
;
385 XRaiseWindow(display
, p_ptr
->window
);
387 if (cur_p
->activated
) {
388 cur_p
->activated
= False
;
389 XSetWindowBackgroundPixmap(display
,
392 _XMRefreshPane(display
, menu
, cur_p
);
394 if (event_xmp
->active
) event_xmp
->activated
= True
;
397 * i suspect the we don't get an EXPOSE event when backing
398 * store is enabled; the menu windows content is probably
399 * not drawn in when it should be in that case.
400 * in that case, this is probably an ugly fix!
401 * i hope someone more familiar with this code would
402 * take it from here. -- caveh@eng.sun.com.
404 XSetWindowBackground(display
,
407 _XMRefreshPane(display
, menu
, event_xmp
);
413 event_xmw
= (XMWindow
*)XLookUpAssoc(
416 event
.xcrossing
.window
418 if (event_xmw
== NULL
) break;
419 if(cur_s
== NULL
) break;
422 * If the current selection was activated then
425 if (cur_s
->activated
) {
426 cur_s
->activated
= False
;
427 _XMRefreshSelection(display
, menu
, cur_s
);
434 *p_num
= cur_p
->serial
;
436 * Check to see if there is a current selection.
440 * Set the selection number to the current selection.
442 *s_num
= cur_s
->serial
;
444 * If the current selection was activated then
445 * we have a valid selection otherwise we have
446 * an inactive selection.
448 if (cur_s
->activated
) {
450 ret_val
= XM_SUCCESS
;
453 ret_val
= XM_IA_SELECT
;
458 * No selection was current.
460 ret_val
= XM_NO_SELECT
;
466 keysym
= XLookupKeysym (&event
.xkey
, 0);
468 /* Pop down on C-g and Escape. */
469 if ((keysym
== XK_g
&& (event
.xkey
.state
& ControlMask
) != 0)
470 || keysym
== XK_Escape
) /* Any escape, ignore modifiers. */
472 ret_val
= XM_NO_SELECT
;
478 * If AEQ mode is enabled then queue the event.
481 feq_tmp
= (XMEventQue
*)malloc(sizeof(XMEventQue
));
482 if (feq_tmp
== NULL
) {
483 _XMErrorCode
= XME_CALLOC
;
486 feq_tmp
->event
= event
;
490 else if (_XMEventHandler
) (*_XMEventHandler
)(&event
);
493 * If a selection has been made, break out of the event loop.
495 if (selection
== True
) break;
501 for ( p_ptr
= menu
->p_list
->next
;
502 p_ptr
!= menu
->p_list
;
505 XUnmapWindow(display
, p_ptr
->window
);
511 XUngrabPointer(display
, CurrentTime
);
512 XUngrabKeyboard(display
, CurrentTime
);
515 * Restore bits under where the menu was if we managed
516 * to save them and free the pixmap.
520 * If there is a current selection deactivate it.
522 if (cur_s
!= NULL
) cur_s
->activated
= 0;
525 * Deactivate the current pane.
527 cur_p
->activated
= 0;
528 XSetWindowBackgroundPixmap(display
, cur_p
->window
, menu
->inact_pixmap
);
531 * Synchronize the X buffers and the X event queue.
536 * Dispatch any events remaining on the queue.
538 while (QLength(display
)) {
540 * Fetch the next event.
542 XNextEvent(display
, &event
);
545 * Discard any events left on the queue that belong to XMenu.
546 * All others are held and then returned to the event queue.
548 switch (event
.type
) {
555 * Does this event belong to one of XMenu's windows?
556 * If so, discard it and process the next event.
557 * If not fall through and treat it as a foreign event.
559 event_xmp
= (XMPane
*)XLookUpAssoc(
564 if (event_xmp
!= NULL
) continue;
567 * This is a foreign event.
568 * Queue it for later return to the X event queue.
570 feq_tmp
= (XMEventQue
*)malloc(sizeof(XMEventQue
));
571 if (feq_tmp
== NULL
) {
572 _XMErrorCode
= XME_CALLOC
;
575 feq_tmp
->event
= event
;
581 * Return any foreign events that were queued to the X event queue.
583 while (feq
!= NULL
) {
585 XPutBackEvent(display
, &feq_tmp
->event
);
587 free((char *)feq_tmp
);
593 * Return successfully.
595 _XMErrorCode
= XME_NO_ERROR
;
600 /* arch-tag: 6b90b578-ecea-4328-b460-a0c96963f872
601 (do not change this comment) */