1 /* Copyright Massachusetts Institute of Technology 1985 */
6 Copyright (C) 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011
7 Free Software Foundation, Inc.
9 This program is free software: you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation, either version 3 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program. If not, see <http://www.gnu.org/licenses/>. */
23 * XMenu: MIT Project Athena, X Window system menu package
25 * XMenuActivate - Maps a given menu to the display and activates
26 * the menu for user selection. The user is allowed to
27 * specify which pane and selection will be current,
28 * the X and Y location of the menu (relative to the
29 * parent window) and the mouse button event mask that
30 * will be used to identify a selection request.
32 * A menu selection is shown to be current by placing
33 * a highlight box around the selection as the mouse
34 * cursor enters its active region. Inactive selections
35 * will not be highlighted. As the mouse cursor moved
36 * from one menu pane to another menu pane the pane being
37 * entered is raised and made current and the pane being
40 * Anytime XMenuActivate returns, the p_num and
41 * s_num are left at their last known values (i.e.,
42 * the last known current pane and selection indices).
43 * The following are the defined return states:
45 * 1) If at any time an error occurs the data
46 * pointer is left untouched and XM_FAILURE
49 * 2) When a selection request is received (i.e.,
50 * when the specified mouse event occurs) the
51 * data pointer will be set to the data
52 * associated with the particular selection
53 * current at the time of the selection request
54 * and XM_SUCCESS is returned.
56 * 3) If no selection was current at the time a
57 * selection request is made the data pointer
58 * will be left untouched and XM_NO_SELECT will
61 * 4) If the selection that was current at the time
62 * a selection request is made is not an active
63 * selection the data pointer will be left
64 * untouched and XM_IA_SELECT will be returned.
66 * Since X processes events in an asynchronous manner
67 * it is likely that XMenuActivate will encounter
68 * a "foreign event" while it is executing. Foreign
69 * events are handled in one of three ways:
71 * 1) The event is discarded. This is the default
72 * mode and requires no action on the part of the
75 * 2) The application has identified an asynchronous
76 * event handler that will be called and the
77 * foreign event handed off to it. Note:
78 * AEQ mode disables this mode temporarily.
80 * 3) The application has enabled asynchronous event
81 * queuing mode. In this mode all foreign events
82 * will be queued up untill XMenuActivate
83 * terminates; at which time they will be
84 * returned to the X event queue. As long as
85 * AEQ mode is enabled any asynchronous event
86 * handler as temporarily disabled.
88 * Any events encountered while taking down the menu
89 * (i.e., exposure events from occluded windows) will
90 * automatically be returned to the X event queue after
91 * XMenuActivate has cleaned the queue of any of its own
92 * events that are no longer needed.
94 * Author: Tony Della Fera, DEC
100 #include "XMenuInt.h"
101 #include <X11/keysym.h>
103 /* For debug, set this to 0 to not grab the keyboard on menu popup */
104 int x_menu_grab_keyboard
= 1;
106 static Wait_func wait_func
;
107 static void* wait_data
;
110 XMenuActivateSetWaitFunction (Wait_func func
, void *data
)
118 register Display
*display
, /* Display to put menu on. */
119 register XMenu
*menu
, /* Menu to activate. */
120 int *p_num
, /* Pane number selected. */
121 int *s_num
, /* Selection number selected. */
122 int x_pos
, /* X coordinate of menu position. */
123 int y_pos
, /* Y coordinate of menu position. */
124 unsigned int event_mask
, /* Mouse button event mask. */
125 char **data
, /* Pointer to return data value. */
126 void (* help_callback
) (char *, int, int)) /* Help callback. */
128 int status
; /* X routine call status. */
129 int orig_x
; /* Upper left menu origin X coord. */
130 int orig_y
; /* Upper left menu origin Y coord. */
131 int ret_val
; /* Return value. */
133 register XMPane
*p_ptr
; /* Current XMPane. */
134 register XMPane
*event_xmp
; /* Event XMPane pointer. */
135 register XMPane
*cur_p
; /* Current pane. */
136 register XMSelect
*cur_s
; /* Current selection. */
137 XMWindow
*event_xmw
; /* Event XMWindow pointer. */
138 XEvent event
; /* X input event. */
139 XEvent peek_event
; /* X input peek ahead event. */
141 Bool selection
= False
; /* Selection has been made. */
142 Bool forward
= True
; /* Moving forward in the pane list. */
145 int root_x
, root_y
, win_x
, win_y
;
150 * Define and allocate a foreign event queue to hold events
151 * that don't belong to XMenu. These events are later restored
152 * to the X event queue.
154 typedef struct _xmeventque
{
156 struct _xmeventque
*next
;
159 XMEventQue
*feq
= NULL
; /* Foreign event queue. */
160 XMEventQue
*feq_tmp
; /* Foreign event queue temporary. */
163 * If there are no panes in the menu then return failure
164 * because the menu is not initialized.
166 if (menu
->p_count
== 0) {
167 _XMErrorCode
= XME_NOT_INIT
;
172 * Find the desired current pane.
174 cur_p
= _XMGetPanePtr(menu
, *p_num
);
178 cur_p
->activated
= cur_p
->active
;
181 * Find the desired current selection.
182 * If the current selection index is out of range a null current selection
183 * will be assumed and the cursor will be placed in the current pane
186 cur_s
= _XMGetSelectionPtr(cur_p
, *s_num
);
189 * Compute origin of menu so that cursor is in
190 * Correct pane and selection.
192 _XMTransToOrigin(display
,
197 menu
->x_pos
= orig_x
; /* Store X and Y coords of menu. */
198 menu
->y_pos
= orig_y
;
200 if (XMenuRecompute(display
, menu
) == XM_FAILURE
) {
205 * Flush the window creation queue.
206 * This batches all window creates since lazy evaluation
207 * is more efficient than individual evaluation.
208 * This routine also does an XFlush().
210 if (_XMWinQueFlush(display
, menu
, cur_p
, cur_s
) == _FAILURE
) {
215 * Make sure windows are in correct order (in case we were passed
216 * an already created menu in incorrect order.)
218 for(p_ptr
= menu
->p_list
->next
; p_ptr
!= cur_p
; p_ptr
= p_ptr
->next
)
219 XRaiseWindow(display
, p_ptr
->window
);
220 for(p_ptr
= menu
->p_list
->prev
; p_ptr
!= cur_p
->prev
; p_ptr
= p_ptr
->prev
)
221 XRaiseWindow(display
, p_ptr
->window
);
224 * Make sure all selection windows are mapped.
227 p_ptr
= menu
->p_list
->next
;
228 p_ptr
!= menu
->p_list
;
231 XMapSubwindows(display
, p_ptr
->window
);
235 * Synchronize the X buffers and the event queue.
236 * From here on, all events in the queue that don't belong to
237 * XMenu are sent back to the application via an application
238 * provided event handler or discarded if the application has
239 * not provided an event handler.
244 * Grab the mouse for menu input.
247 status
= XGrabPointer(
258 if (status
== Success
&& x_menu_grab_keyboard
)
260 status
= XGrabKeyboard (display
,
266 if (status
!= Success
)
267 XUngrabPointer(display
, CurrentTime
);
270 if (status
== _X_FAILURE
) {
271 _XMErrorCode
= XME_GRAB_MOUSE
;
276 * Map the menu panes.
278 XMapWindow(display
, cur_p
->window
);
279 for (p_ptr
= menu
->p_list
->next
;
282 XMapWindow(display
, p_ptr
->window
);
283 for (p_ptr
= cur_p
->next
;
284 p_ptr
!= menu
->p_list
;
286 XMapWindow(display
, p_ptr
->window
);
288 XRaiseWindow(display
, cur_p
->window
); /* Make sure current */
289 /* pane is on top. */
291 cur_s
= NULL
; /* Clear current selection. */
294 * Begin event processing loop.
297 if (wait_func
) (*wait_func
) (wait_data
);
298 XNextEvent(display
, &event
); /* Get next event. */
299 switch (event
.type
) { /* Dispatch on the event type. */
301 event_xmp
= (XMPane
*)XLookUpAssoc(display
,
303 event
.xexpose
.window
);
304 if (event_xmp
== NULL
) {
306 * If AEQ mode is enabled then queue the event.
309 feq_tmp
= (XMEventQue
*)malloc(sizeof(XMEventQue
));
310 if (feq_tmp
== NULL
) {
311 _XMErrorCode
= XME_CALLOC
;
314 feq_tmp
->event
= event
;
318 else if (_XMEventHandler
) (*_XMEventHandler
)(&event
);
321 if (event_xmp
->activated
) {
322 XSetWindowBackground(display
,
327 XSetWindowBackgroundPixmap(display
,
331 _XMRefreshPane(display
, menu
, event_xmp
);
335 * First wait a small period of time, and see
336 * if another EnterNotify event follows hard on the
337 * heels of this one. i.e., the user is simply
338 * "passing through". If so, ignore this one.
341 event_xmw
= (XMWindow
*)XLookUpAssoc(display
,
343 event
.xcrossing
.window
);
344 if (event_xmw
== NULL
) break;
345 if (event_xmw
->type
== SELECTION
) {
347 * We have entered a selection.
349 /* if (XPending(display) == 0) usleep(150000); */
350 if (XPending(display
) != 0) {
351 XPeekEvent(display
, &peek_event
);
352 if(peek_event
.type
== LeaveNotify
) {
356 cur_s
= (XMSelect
*)event_xmw
;
357 help_callback (cur_s
->help_string
,
358 cur_p
->serial
, cur_s
->serial
);
361 * If the pane we are in is active and the
362 * selection entered is active then activate
365 if (cur_p
->active
&& cur_s
->active
> 0) {
366 cur_s
->activated
= 1;
367 _XMRefreshSelection(display
, menu
, cur_s
);
372 * We have entered a pane.
374 /* if (XPending(display) == 0) usleep(150000); */
375 if (XPending(display
) != 0) {
376 XPeekEvent(display
, &peek_event
);
377 if (peek_event
.type
== EnterNotify
) break;
379 XQueryPointer(display
,
385 event_xmp
= (XMPane
*)XLookUpAssoc(display
,
388 if (event_xmp
== NULL
) break;
389 if (event_xmp
== cur_p
) break;
390 if (event_xmp
->serial
> cur_p
->serial
) forward
= True
;
391 else forward
= False
;
393 while (p_ptr
!= event_xmp
) {
394 if (forward
) p_ptr
= p_ptr
->next
;
395 else p_ptr
= p_ptr
->prev
;
396 XRaiseWindow(display
, p_ptr
->window
);
398 if (cur_p
->activated
) {
399 cur_p
->activated
= False
;
400 XSetWindowBackgroundPixmap(display
,
403 _XMRefreshPane(display
, menu
, cur_p
);
405 if (event_xmp
->active
) event_xmp
->activated
= True
;
408 * i suspect the we don't get an EXPOSE event when backing
409 * store is enabled; the menu windows content is probably
410 * not drawn in when it should be in that case.
411 * in that case, this is probably an ugly fix!
412 * i hope someone more familiar with this code would
413 * take it from here. -- caveh@eng.sun.com.
415 XSetWindowBackground(display
,
418 _XMRefreshPane(display
, menu
, event_xmp
);
424 event_xmw
= (XMWindow
*)XLookUpAssoc(
427 event
.xcrossing
.window
429 if (event_xmw
== NULL
) break;
430 if(cur_s
== NULL
) break;
433 * If the current selection was activated then
436 if (cur_s
->activated
) {
437 cur_s
->activated
= False
;
438 _XMRefreshSelection(display
, menu
, cur_s
);
445 *p_num
= cur_p
->serial
;
447 * Check to see if there is a current selection.
451 * Set the selection number to the current selection.
453 *s_num
= cur_s
->serial
;
455 * If the current selection was activated then
456 * we have a valid selection otherwise we have
457 * an inactive selection.
459 if (cur_s
->activated
) {
461 ret_val
= XM_SUCCESS
;
464 ret_val
= XM_IA_SELECT
;
469 * No selection was current.
471 ret_val
= XM_NO_SELECT
;
477 keysym
= XLookupKeysym (&event
.xkey
, 0);
479 /* Pop down on C-g and Escape. */
480 if ((keysym
== XK_g
&& (event
.xkey
.state
& ControlMask
) != 0)
481 || keysym
== XK_Escape
) /* Any escape, ignore modifiers. */
483 ret_val
= XM_NO_SELECT
;
489 * If AEQ mode is enabled then queue the event.
492 feq_tmp
= (XMEventQue
*)malloc(sizeof(XMEventQue
));
493 if (feq_tmp
== NULL
) {
494 _XMErrorCode
= XME_CALLOC
;
497 feq_tmp
->event
= event
;
501 else if (_XMEventHandler
) (*_XMEventHandler
)(&event
);
504 * If a selection has been made, break out of the event loop.
506 if (selection
== True
) break;
512 for ( p_ptr
= menu
->p_list
->next
;
513 p_ptr
!= menu
->p_list
;
516 XUnmapWindow(display
, p_ptr
->window
);
522 XUngrabPointer(display
, CurrentTime
);
523 XUngrabKeyboard(display
, CurrentTime
);
526 * Restore bits under where the menu was if we managed
527 * to save them and free the pixmap.
531 * If there is a current selection deactivate it.
533 if (cur_s
!= NULL
) cur_s
->activated
= 0;
536 * Deactivate the current pane.
538 cur_p
->activated
= 0;
539 XSetWindowBackgroundPixmap(display
, cur_p
->window
, menu
->inact_pixmap
);
542 * Synchronize the X buffers and the X event queue.
547 * Dispatch any events remaining on the queue.
549 while (QLength(display
)) {
551 * Fetch the next event.
553 XNextEvent(display
, &event
);
556 * Discard any events left on the queue that belong to XMenu.
557 * All others are held and then returned to the event queue.
559 switch (event
.type
) {
566 * Does this event belong to one of XMenu's windows?
567 * If so, discard it and process the next event.
568 * If not fall through and treat it as a foreign event.
570 event_xmp
= (XMPane
*)XLookUpAssoc(
575 if (event_xmp
!= NULL
) continue;
578 * This is a foreign event.
579 * Queue it for later return to the X event queue.
581 feq_tmp
= (XMEventQue
*)malloc(sizeof(XMEventQue
));
582 if (feq_tmp
== NULL
) {
583 _XMErrorCode
= XME_CALLOC
;
586 feq_tmp
->event
= event
;
592 * Return any foreign events that were queued to the X event queue.
594 while (feq
!= NULL
) {
596 XPutBackEvent(display
, &feq_tmp
->event
);
598 free((char *)feq_tmp
);
604 * Return successfully.
606 _XMErrorCode
= XME_NO_ERROR
;