(Coding Conventions): Separate defvar and require
[emacs.git] / oldXMenu / Activate.c
blob363b225a3559fce6c2a7c3945626c03dffdabc08
1 /* Copyright Massachusetts Institute of Technology 1985 */
3 #include "copyright.h"
5 /*
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
21 * left is lowered.
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
30 * is returned.
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
42 * be returned.
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
56 * application.
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
78 * March 12, 1986
82 #include <config.h>
83 #include "XMenuInt.h"
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;
93 void
94 XMenuActivateSetWaitFunction (func, data)
95 Wait_func func;
96 void *data;
98 wait_func = func;
99 wait_data = data;
103 XMenuActivate(display, menu, p_num, s_num, x_pos, y_pos, event_mask, data,
104 help_callback)
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. */
131 Window root, child;
132 int root_x, root_y, win_x, win_y;
133 unsigned int mask;
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 {
141 XEvent event;
142 struct _xmeventque *next;
143 } XMEventQue;
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;
154 return(XM_FAILURE);
158 * Find the desired current pane.
160 cur_p = _XMGetPanePtr(menu, *p_num);
161 if (cur_p == NULL) {
162 return(XM_FAILURE);
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
170 * header.
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,
179 menu,
180 cur_p, cur_s,
181 x_pos, y_pos,
182 &orig_x, &orig_y);
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) {
187 return(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) {
197 return(XM_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.
212 for (
213 p_ptr = menu->p_list->next;
214 p_ptr != menu->p_list;
215 p_ptr = p_ptr->next
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.
227 XSync(display, 0);
230 * Grab the mouse for menu input.
233 status = XGrabPointer(
234 display,
235 menu->parent,
236 True,
237 event_mask,
238 GrabModeAsync,
239 GrabModeAsync,
240 None,
241 menu->mouse_cursor,
242 CurrentTime
244 if (status == Success && x_menu_grab_keyboard)
246 status = XGrabKeyboard (display,
247 menu->parent,
248 False,
249 GrabModeAsync,
250 GrabModeAsync,
251 CurrentTime);
252 if (status != Success)
253 XUngrabPointer(display, CurrentTime);
256 if (status == _X_FAILURE) {
257 _XMErrorCode = XME_GRAB_MOUSE;
258 return(XM_FAILURE);
262 * Map the menu panes.
264 XMapWindow(display, cur_p->window);
265 for (p_ptr = menu->p_list->next;
266 p_ptr != cur_p;
267 p_ptr = p_ptr->next)
268 XMapWindow(display, p_ptr->window);
269 for (p_ptr = cur_p->next;
270 p_ptr != menu->p_list;
271 p_ptr = p_ptr->next)
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.
282 while (1) {
283 if (wait_func) (*wait_func) (wait_data);
284 XNextEvent(display, &event); /* Get next event. */
285 switch (event.type) { /* Dispatch on the event type. */
286 case Expose:
287 event_xmp = (XMPane *)XLookUpAssoc(display,
288 menu->assoc_tab,
289 event.xexpose.window);
290 if (event_xmp == NULL) {
292 * If AEQ mode is enabled then queue the event.
294 if (menu->aeq) {
295 feq_tmp = (XMEventQue *)malloc(sizeof(XMEventQue));
296 if (feq_tmp == NULL) {
297 _XMErrorCode = XME_CALLOC;
298 return(XM_FAILURE);
300 feq_tmp->event = event;
301 feq_tmp->next = feq;
302 feq = feq_tmp;
304 else if (_XMEventHandler) (*_XMEventHandler)(&event);
305 break;
307 if (event_xmp->activated) {
308 XSetWindowBackground(display,
309 event_xmp->window,
310 menu->bkgnd_color);
312 else {
313 XSetWindowBackgroundPixmap(display,
314 event_xmp->window,
315 menu->inact_pixmap);
317 _XMRefreshPane(display, menu, event_xmp);
318 break;
319 case EnterNotify:
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,
328 menu->assoc_tab,
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) {
339 break;
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
349 * the selection.
351 if (cur_p->active && cur_s->active > 0) {
352 cur_s->activated = 1;
353 _XMRefreshSelection(display, menu, cur_s);
356 else {
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,
366 menu->parent,
367 &root, &child,
368 &root_x, &root_y,
369 &win_x, &win_y,
370 &mask);
371 event_xmp = (XMPane *)XLookUpAssoc(display,
372 menu->assoc_tab,
373 child);
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;
378 p_ptr = cur_p;
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,
387 cur_p->window,
388 menu->inact_pixmap);
389 _XMRefreshPane(display, menu, cur_p);
391 if (event_xmp->active) event_xmp->activated = True;
392 #if 1
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,
402 event_xmp->window,
403 menu->bkgnd_color);
404 _XMRefreshPane(display, menu, event_xmp);
405 #endif
406 cur_p = event_xmp;
408 break;
409 case LeaveNotify:
410 event_xmw = (XMWindow *)XLookUpAssoc(
411 display,
412 menu->assoc_tab,
413 event.xcrossing.window
415 if (event_xmw == NULL) break;
416 if(cur_s == NULL) break;
419 * If the current selection was activated then
420 * deactivate it.
422 if (cur_s->activated) {
423 cur_s->activated = False;
424 _XMRefreshSelection(display, menu, cur_s);
426 cur_s = NULL;
427 break;
429 case ButtonPress:
430 case ButtonRelease:
431 *p_num = cur_p->serial;
433 * Check to see if there is a current selection.
435 if (cur_s != NULL) {
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) {
446 *data = cur_s->data;
447 ret_val = XM_SUCCESS;
449 else {
450 ret_val = XM_IA_SELECT;
453 else {
455 * No selection was current.
457 ret_val = XM_NO_SELECT;
459 selection = True;
460 break;
461 default:
463 * If AEQ mode is enabled then queue the event.
465 if (menu->aeq) {
466 feq_tmp = (XMEventQue *)malloc(sizeof(XMEventQue));
467 if (feq_tmp == NULL) {
468 _XMErrorCode = XME_CALLOC;
469 return(XM_FAILURE);
471 feq_tmp->event = event;
472 feq_tmp->next = feq;
473 feq = feq_tmp;
475 else if (_XMEventHandler) (*_XMEventHandler)(&event);
478 * If a selection has been made, break out of the event loop.
480 if (selection == True) break;
484 * Unmap the menu.
486 for ( p_ptr = menu->p_list->next;
487 p_ptr != menu->p_list;
488 p_ptr = p_ptr->next)
490 XUnmapWindow(display, p_ptr->window);
494 * Ungrab the mouse.
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.
518 XSync(display, 0);
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) {
534 case Expose:
535 case EnterNotify:
536 case LeaveNotify:
537 case ButtonPress:
538 case ButtonRelease:
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(
545 display,
546 menu->assoc_tab,
547 event.xbutton.window
549 if (event_xmp != NULL) continue;
550 default:
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;
558 return(XM_FAILURE);
560 feq_tmp->event = event;
561 feq_tmp->next = feq;
562 feq = feq_tmp;
566 * Return any foreign events that were queued to the X event queue.
568 while (feq != NULL) {
569 feq_tmp = feq;
570 XPutBackEvent(display, &feq_tmp->event);
571 feq = feq_tmp->next;
572 free((char *)feq_tmp);
575 wait_func = 0;
578 * Return successfully.
580 _XMErrorCode = XME_NO_ERROR;
581 return(ret_val);
585 /* arch-tag: 6b90b578-ecea-4328-b460-a0c96963f872
586 (do not change this comment) */