nnir.el (nnir-mode): Don't install registry hooks if user hasn't installed the registry.
[emacs.git] / oldXMenu / Activate.c
blob55bfca510b95ed1f629a12bb611bac5cf656b2d6
1 /* Copyright Massachusetts Institute of Technology 1985 */
3 #include "copyright.h"
5 /*
6 Copyright (C) 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010
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
38 * left is lowered.
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
47 * is returned.
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
59 * be returned.
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
73 * application.
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
95 * March 12, 1986
99 #include <config.h>
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;
109 void
110 XMenuActivateSetWaitFunction (Wait_func func, void *data)
112 wait_func = func;
113 wait_data = data;
117 XMenuActivate(
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. */
144 Window root, child;
145 int root_x, root_y, win_x, win_y;
146 unsigned int mask;
147 KeySym keysym;
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 {
155 XEvent event;
156 struct _xmeventque *next;
157 } XMEventQue;
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;
168 return(XM_FAILURE);
172 * Find the desired current pane.
174 cur_p = _XMGetPanePtr(menu, *p_num);
175 if (cur_p == NULL) {
176 return(XM_FAILURE);
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
184 * header.
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,
193 menu,
194 cur_p, cur_s,
195 x_pos, y_pos,
196 &orig_x, &orig_y);
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) {
201 return(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) {
211 return(XM_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.
226 for (
227 p_ptr = menu->p_list->next;
228 p_ptr != menu->p_list;
229 p_ptr = p_ptr->next
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.
241 XSync(display, 0);
244 * Grab the mouse for menu input.
247 status = XGrabPointer(
248 display,
249 menu->parent,
250 True,
251 event_mask,
252 GrabModeAsync,
253 GrabModeAsync,
254 None,
255 menu->mouse_cursor,
256 CurrentTime
258 if (status == Success && x_menu_grab_keyboard)
260 status = XGrabKeyboard (display,
261 menu->parent,
262 False,
263 GrabModeAsync,
264 GrabModeAsync,
265 CurrentTime);
266 if (status != Success)
267 XUngrabPointer(display, CurrentTime);
270 if (status == _X_FAILURE) {
271 _XMErrorCode = XME_GRAB_MOUSE;
272 return(XM_FAILURE);
276 * Map the menu panes.
278 XMapWindow(display, cur_p->window);
279 for (p_ptr = menu->p_list->next;
280 p_ptr != cur_p;
281 p_ptr = p_ptr->next)
282 XMapWindow(display, p_ptr->window);
283 for (p_ptr = cur_p->next;
284 p_ptr != menu->p_list;
285 p_ptr = p_ptr->next)
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.
296 while (1) {
297 if (wait_func) (*wait_func) (wait_data);
298 XNextEvent(display, &event); /* Get next event. */
299 switch (event.type) { /* Dispatch on the event type. */
300 case Expose:
301 event_xmp = (XMPane *)XLookUpAssoc(display,
302 menu->assoc_tab,
303 event.xexpose.window);
304 if (event_xmp == NULL) {
306 * If AEQ mode is enabled then queue the event.
308 if (menu->aeq) {
309 feq_tmp = (XMEventQue *)malloc(sizeof(XMEventQue));
310 if (feq_tmp == NULL) {
311 _XMErrorCode = XME_CALLOC;
312 return(XM_FAILURE);
314 feq_tmp->event = event;
315 feq_tmp->next = feq;
316 feq = feq_tmp;
318 else if (_XMEventHandler) (*_XMEventHandler)(&event);
319 break;
321 if (event_xmp->activated) {
322 XSetWindowBackground(display,
323 event_xmp->window,
324 menu->bkgnd_color);
326 else {
327 XSetWindowBackgroundPixmap(display,
328 event_xmp->window,
329 menu->inact_pixmap);
331 _XMRefreshPane(display, menu, event_xmp);
332 break;
333 case EnterNotify:
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,
342 menu->assoc_tab,
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) {
353 break;
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
363 * the selection.
365 if (cur_p->active && cur_s->active > 0) {
366 cur_s->activated = 1;
367 _XMRefreshSelection(display, menu, cur_s);
370 else {
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,
380 menu->parent,
381 &root, &child,
382 &root_x, &root_y,
383 &win_x, &win_y,
384 &mask);
385 event_xmp = (XMPane *)XLookUpAssoc(display,
386 menu->assoc_tab,
387 child);
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;
392 p_ptr = cur_p;
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,
401 cur_p->window,
402 menu->inact_pixmap);
403 _XMRefreshPane(display, menu, cur_p);
405 if (event_xmp->active) event_xmp->activated = True;
406 #if 1
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,
416 event_xmp->window,
417 menu->bkgnd_color);
418 _XMRefreshPane(display, menu, event_xmp);
419 #endif
420 cur_p = event_xmp;
422 break;
423 case LeaveNotify:
424 event_xmw = (XMWindow *)XLookUpAssoc(
425 display,
426 menu->assoc_tab,
427 event.xcrossing.window
429 if (event_xmw == NULL) break;
430 if(cur_s == NULL) break;
433 * If the current selection was activated then
434 * deactivate it.
436 if (cur_s->activated) {
437 cur_s->activated = False;
438 _XMRefreshSelection(display, menu, cur_s);
440 cur_s = NULL;
441 break;
443 case ButtonPress:
444 case ButtonRelease:
445 *p_num = cur_p->serial;
447 * Check to see if there is a current selection.
449 if (cur_s != NULL) {
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) {
460 *data = cur_s->data;
461 ret_val = XM_SUCCESS;
463 else {
464 ret_val = XM_IA_SELECT;
467 else {
469 * No selection was current.
471 ret_val = XM_NO_SELECT;
473 selection = True;
474 break;
475 case KeyPress:
476 case KeyRelease:
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;
484 selection = True;
486 break;
487 default:
489 * If AEQ mode is enabled then queue the event.
491 if (menu->aeq) {
492 feq_tmp = (XMEventQue *)malloc(sizeof(XMEventQue));
493 if (feq_tmp == NULL) {
494 _XMErrorCode = XME_CALLOC;
495 return(XM_FAILURE);
497 feq_tmp->event = event;
498 feq_tmp->next = feq;
499 feq = feq_tmp;
501 else if (_XMEventHandler) (*_XMEventHandler)(&event);
504 * If a selection has been made, break out of the event loop.
506 if (selection == True) break;
510 * Unmap the menu.
512 for ( p_ptr = menu->p_list->next;
513 p_ptr != menu->p_list;
514 p_ptr = p_ptr->next)
516 XUnmapWindow(display, p_ptr->window);
520 * Ungrab the mouse.
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.
544 XSync(display, 0);
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) {
560 case Expose:
561 case EnterNotify:
562 case LeaveNotify:
563 case ButtonPress:
564 case ButtonRelease:
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(
571 display,
572 menu->assoc_tab,
573 event.xbutton.window
575 if (event_xmp != NULL) continue;
576 default:
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;
584 return(XM_FAILURE);
586 feq_tmp->event = event;
587 feq_tmp->next = feq;
588 feq = feq_tmp;
592 * Return any foreign events that were queued to the X event queue.
594 while (feq != NULL) {
595 feq_tmp = feq;
596 XPutBackEvent(display, &feq_tmp->event);
597 feq = feq_tmp->next;
598 free((char *)feq_tmp);
601 wait_func = 0;
604 * Return successfully.
606 _XMErrorCode = XME_NO_ERROR;
607 return(ret_val);
611 /* arch-tag: 6b90b578-ecea-4328-b460-a0c96963f872
612 (do not change this comment) */