(fortran-fill): Use local var `bol' rather than duplicate call to
[emacs.git] / oldXMenu / Activate.c
blob840c423471bda0764d95168d60cb8c15545c6dd2
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 int
89 XMenuActivate(display, menu, p_num, s_num, x_pos, y_pos, event_mask, data,
90 help_callback)
91 register Display *display; /* Display to put menu on. */
92 register XMenu *menu; /* Menu to activate. */
93 int *p_num; /* Pane number selected. */
94 int *s_num; /* Selection number selected. */
95 int x_pos; /* X coordinate of menu position. */
96 int y_pos; /* Y coordinate of menu position. */
97 unsigned int event_mask; /* Mouse button event mask. */
98 char **data; /* Pointer to return data value. */
99 void (* help_callback) (); /* Help callback. */
101 int status; /* X routine call status. */
102 int orig_x; /* Upper left menu origin X coord. */
103 int orig_y; /* Upper left menu origin Y coord. */
104 int ret_val; /* Return value. */
106 register XMPane *p_ptr; /* Current XMPane. */
107 register XMPane *event_xmp; /* Event XMPane pointer. */
108 register XMPane *cur_p; /* Current pane. */
109 register XMSelect *cur_s; /* Current selection. */
110 XMWindow *event_xmw; /* Event XMWindow pointer. */
111 XEvent event; /* X input event. */
112 XEvent peek_event; /* X input peek ahead event. */
114 Bool selection = False; /* Selection has been made. */
115 Bool forward = True; /* Moving forward in the pane list. */
117 Window root, child;
118 int root_x, root_y, win_x, win_y;
119 unsigned int mask;
122 * Define and allocate a foreign event queue to hold events
123 * that don't belong to XMenu. These events are later restored
124 * to the X event queue.
126 typedef struct _xmeventque {
127 XEvent event;
128 struct _xmeventque *next;
129 } XMEventQue;
131 XMEventQue *feq = NULL; /* Foreign event queue. */
132 XMEventQue *feq_tmp; /* Foreign event queue temporary. */
135 * If there are no panes in the menu then return failure
136 * because the menu is not initialized.
138 if (menu->p_count == 0) {
139 _XMErrorCode = XME_NOT_INIT;
140 return(XM_FAILURE);
144 * Find the desired current pane.
146 cur_p = _XMGetPanePtr(menu, *p_num);
147 if (cur_p == NULL) {
148 return(XM_FAILURE);
150 cur_p->activated = cur_p->active;
153 * Find the desired current selection.
154 * If the current selection index is out of range a null current selection
155 * will be assumed and the cursor will be placed in the current pane
156 * header.
158 cur_s = _XMGetSelectionPtr(cur_p, *s_num);
161 * Compute origin of menu so that cursor is in
162 * Correct pane and selection.
164 _XMTransToOrigin(display,
165 menu,
166 cur_p, cur_s,
167 x_pos, y_pos,
168 &orig_x, &orig_y);
169 menu->x_pos = orig_x; /* Store X and Y coords of menu. */
170 menu->y_pos = orig_y;
172 if (XMenuRecompute(display, menu) == XM_FAILURE) {
173 return(XM_FAILURE);
177 * Flush the window creation queue.
178 * This batches all window creates since lazy evaluation
179 * is more efficient than individual evaluation.
180 * This routine also does an XFlush().
182 if (_XMWinQueFlush(display, menu, cur_p, cur_s) == _FAILURE) {
183 return(XM_FAILURE);
187 * Make sure windows are in correct order (in case we were passed
188 * an already created menu in incorrect order.)
190 for(p_ptr = menu->p_list->next; p_ptr != cur_p; p_ptr = p_ptr->next)
191 XRaiseWindow(display, p_ptr->window);
192 for(p_ptr = menu->p_list->prev; p_ptr != cur_p->prev; p_ptr = p_ptr->prev)
193 XRaiseWindow(display, p_ptr->window);
196 * Make sure all selection windows are mapped.
198 for (
199 p_ptr = menu->p_list->next;
200 p_ptr != menu->p_list;
201 p_ptr = p_ptr->next
203 XMapSubwindows(display, p_ptr->window);
207 * Synchronize the X buffers and the event queue.
208 * From here on, all events in the queue that don't belong to
209 * XMenu are sent back to the application via an application
210 * provided event handler or discarded if the application has
211 * not provided an event handler.
213 XSync(display, 0);
216 * Grab the mouse for menu input.
219 status = XGrabPointer(
220 display,
221 menu->parent,
222 True,
223 event_mask,
224 GrabModeAsync,
225 GrabModeAsync,
226 None,
227 menu->mouse_cursor,
228 CurrentTime
230 if (status == Success && x_menu_grab_keyboard)
232 status = XGrabKeyboard (display,
233 menu->parent,
234 False,
235 GrabModeAsync,
236 GrabModeAsync,
237 CurrentTime);
238 if (status != Success)
239 XUngrabPointer(display, CurrentTime);
242 if (status == _X_FAILURE) {
243 _XMErrorCode = XME_GRAB_MOUSE;
244 return(XM_FAILURE);
248 * Map the menu panes.
250 XMapWindow(display, cur_p->window);
251 for (p_ptr = menu->p_list->next;
252 p_ptr != cur_p;
253 p_ptr = p_ptr->next)
254 XMapWindow(display, p_ptr->window);
255 for (p_ptr = cur_p->next;
256 p_ptr != menu->p_list;
257 p_ptr = p_ptr->next)
258 XMapWindow(display, p_ptr->window);
260 XRaiseWindow(display, cur_p->window); /* Make sure current */
261 /* pane is on top. */
263 cur_s = NULL; /* Clear current selection. */
266 * Begin event processing loop.
268 while (1) {
269 XNextEvent(display, &event); /* Get next event. */
270 switch (event.type) { /* Dispatch on the event type. */
271 case Expose:
272 event_xmp = (XMPane *)XLookUpAssoc(display,
273 menu->assoc_tab,
274 event.xexpose.window);
275 if (event_xmp == NULL) {
277 * If AEQ mode is enabled then queue the event.
279 if (menu->aeq) {
280 feq_tmp = (XMEventQue *)malloc(sizeof(XMEventQue));
281 if (feq_tmp == NULL) {
282 _XMErrorCode = XME_CALLOC;
283 return(XM_FAILURE);
285 feq_tmp->event = event;
286 feq_tmp->next = feq;
287 feq = feq_tmp;
289 else if (_XMEventHandler) (*_XMEventHandler)(&event);
290 break;
292 if (event_xmp->activated) {
293 XSetWindowBackground(display,
294 event_xmp->window,
295 menu->bkgnd_color);
297 else {
298 XSetWindowBackgroundPixmap(display,
299 event_xmp->window,
300 menu->inact_pixmap);
302 _XMRefreshPane(display, menu, event_xmp);
303 break;
304 case EnterNotify:
306 * First wait a small period of time, and see
307 * if another EnterNotify event follows hard on the
308 * heels of this one. i.e., the user is simply
309 * "passing through". If so, ignore this one.
312 event_xmw = (XMWindow *)XLookUpAssoc(display,
313 menu->assoc_tab,
314 event.xcrossing.window);
315 if (event_xmw == NULL) break;
316 if (event_xmw->type == SELECTION) {
318 * We have entered a selection.
320 /* if (XPending(display) == 0) usleep(150000); */
321 if (XPending(display) != 0) {
322 XPeekEvent(display, &peek_event);
323 if(peek_event.type == LeaveNotify) {
324 break;
327 cur_s = (XMSelect *)event_xmw;
328 help_callback (cur_s->help_string,
329 cur_p->serial, cur_s->serial);
332 * If the pane we are in is active and the
333 * selection entered is active then activate
334 * the selection.
336 if (cur_p->active && cur_s->active > 0) {
337 cur_s->activated = 1;
338 _XMRefreshSelection(display, menu, cur_s);
341 else {
343 * We have entered a pane.
345 /* if (XPending(display) == 0) usleep(150000); */
346 if (XPending(display) != 0) {
347 XPeekEvent(display, &peek_event);
348 if (peek_event.type == EnterNotify) break;
350 XQueryPointer(display,
351 menu->parent,
352 &root, &child,
353 &root_x, &root_y,
354 &win_x, &win_y,
355 &mask);
356 event_xmp = (XMPane *)XLookUpAssoc(display,
357 menu->assoc_tab,
358 child);
359 if (event_xmp == NULL) break;
360 if (event_xmp == cur_p) break;
361 if (event_xmp->serial > cur_p->serial) forward = True;
362 else forward = False;
363 p_ptr = cur_p;
364 while (p_ptr != event_xmp) {
365 if (forward) p_ptr = p_ptr->next;
366 else p_ptr = p_ptr->prev;
367 XRaiseWindow(display, p_ptr->window);
369 if (cur_p->activated) {
370 cur_p->activated = False;
371 XSetWindowBackgroundPixmap(display,
372 cur_p->window,
373 menu->inact_pixmap);
374 _XMRefreshPane(display, menu, cur_p);
376 if (event_xmp->active) event_xmp->activated = True;
377 #if 1
379 * i suspect the we don't get an EXPOSE event when backing
380 * store is enabled; the menu windows content is probably
381 * not drawn in when it should be in that case.
382 * in that case, this is probably an ugly fix!
383 * i hope someone more familiar with this code would
384 * take it from here. -- caveh@eng.sun.com.
386 XSetWindowBackground(display,
387 event_xmp->window,
388 menu->bkgnd_color);
389 _XMRefreshPane(display, menu, event_xmp);
390 #endif
391 cur_p = event_xmp;
393 break;
394 case LeaveNotify:
395 event_xmw = (XMWindow *)XLookUpAssoc(
396 display,
397 menu->assoc_tab,
398 event.xcrossing.window
400 if (event_xmw == NULL) break;
401 if(cur_s == NULL) break;
404 * If the current selection was activated then
405 * deactivate it.
407 if (cur_s->activated) {
408 cur_s->activated = False;
409 _XMRefreshSelection(display, menu, cur_s);
411 cur_s = NULL;
412 break;
414 case ButtonPress:
415 case ButtonRelease:
416 *p_num = cur_p->serial;
418 * Check to see if there is a current selection.
420 if (cur_s != NULL) {
422 * Set the selection number to the current selection.
424 *s_num = cur_s->serial;
426 * If the current selection was activated then
427 * we have a valid selection otherwise we have
428 * an inactive selection.
430 if (cur_s->activated) {
431 *data = cur_s->data;
432 ret_val = XM_SUCCESS;
434 else {
435 ret_val = XM_IA_SELECT;
438 else {
440 * No selection was current.
442 ret_val = XM_NO_SELECT;
444 selection = True;
445 break;
446 default:
448 * If AEQ mode is enabled then queue the event.
450 if (menu->aeq) {
451 feq_tmp = (XMEventQue *)malloc(sizeof(XMEventQue));
452 if (feq_tmp == NULL) {
453 _XMErrorCode = XME_CALLOC;
454 return(XM_FAILURE);
456 feq_tmp->event = event;
457 feq_tmp->next = feq;
458 feq = feq_tmp;
460 else if (_XMEventHandler) (*_XMEventHandler)(&event);
463 * If a selection has been made, break out of the event loop.
465 if (selection == True) break;
469 * Unmap the menu.
471 for ( p_ptr = menu->p_list->next;
472 p_ptr != menu->p_list;
473 p_ptr = p_ptr->next)
475 XUnmapWindow(display, p_ptr->window);
479 * Ungrab the mouse.
481 XUngrabPointer(display, CurrentTime);
482 XUngrabKeyboard(display, CurrentTime);
485 * Restore bits under where the menu was if we managed
486 * to save them and free the pixmap.
490 * If there is a current selection deactivate it.
492 if (cur_s != NULL) cur_s->activated = 0;
495 * Deactivate the current pane.
497 cur_p->activated = 0;
498 XSetWindowBackgroundPixmap(display, cur_p->window, menu->inact_pixmap);
501 * Synchronize the X buffers and the X event queue.
503 XSync(display, 0);
506 * Dispatch any events remaining on the queue.
508 while (QLength(display)) {
510 * Fetch the next event.
512 XNextEvent(display, &event);
515 * Discard any events left on the queue that belong to XMenu.
516 * All others are held and then returned to the event queue.
518 switch (event.type) {
519 case Expose:
520 case EnterNotify:
521 case LeaveNotify:
522 case ButtonPress:
523 case ButtonRelease:
525 * Does this event belong to one of XMenu's windows?
526 * If so, discard it and process the next event.
527 * If not fall through and treat it as a foreign event.
529 event_xmp = (XMPane *)XLookUpAssoc(
530 display,
531 menu->assoc_tab,
532 event.xbutton.window
534 if (event_xmp != NULL) continue;
535 default:
537 * This is a foreign event.
538 * Queue it for later return to the X event queue.
540 feq_tmp = (XMEventQue *)malloc(sizeof(XMEventQue));
541 if (feq_tmp == NULL) {
542 _XMErrorCode = XME_CALLOC;
543 return(XM_FAILURE);
545 feq_tmp->event = event;
546 feq_tmp->next = feq;
547 feq = feq_tmp;
551 * Return any foreign events that were queued to the X event queue.
553 while (feq != NULL) {
554 feq_tmp = feq;
555 XPutBackEvent(display, &feq_tmp->event);
556 feq = feq_tmp->next;
557 free((char *)feq_tmp);
561 * Return successfully.
563 _XMErrorCode = XME_NO_ERROR;
564 return(ret_val);
568 /* arch-tag: 6b90b578-ecea-4328-b460-a0c96963f872
569 (do not change this comment) */