Merge from emacs-24 branch; up to 2012-05-01T10:20:43Z!rgm@gnu.org
[bpt/emacs.git] / oldXMenu / Activate.c
CommitLineData
e745ede7
DL
1/* Copyright Massachusetts Institute of Technology 1985 */
2
3#include "copyright.h"
4
54861d5e 5/*
acaf905b 6Copyright (C) 2001-2012 Free Software Foundation, Inc.
54861d5e 7
4eaa4034 8This program is free software: you can redistribute it and/or modify
54861d5e 9it under the terms of the GNU General Public License as published by
4eaa4034
GM
10the Free Software Foundation, either version 3 of the License, or
11(at your option) any later version.
54861d5e
GM
12
13This program is distributed in the hope that it will be useful,
14but WITHOUT ANY WARRANTY; without even the implied warranty of
15MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16GNU General Public License for more details.
17
18You should have received a copy of the GNU General Public License
4eaa4034 19along with this program. If not, see <http://www.gnu.org/licenses/>. */
54861d5e 20
e745ede7
DL
21/*
22 * XMenu: MIT Project Athena, X Window system menu package
23 *
24 * XMenuActivate - Maps a given menu to the display and activates
25 * the menu for user selection. The user is allowed to
26 * specify which pane and selection will be current,
27 * the X and Y location of the menu (relative to the
28 * parent window) and the mouse button event mask that
29 * will be used to identify a selection request.
30 *
31 * A menu selection is shown to be current by placing
32 * a highlight box around the selection as the mouse
33 * cursor enters its active region. Inactive selections
34 * will not be highlighted. As the mouse cursor moved
35 * from one menu pane to another menu pane the pane being
36 * entered is raised and made current and the pane being
37 * left is lowered.
38 *
39 * Anytime XMenuActivate returns, the p_num and
40 * s_num are left at their last known values (i.e.,
41 * the last known current pane and selection indices).
42 * The following are the defined return states:
43 *
44 * 1) If at any time an error occurs the data
45 * pointer is left untouched and XM_FAILURE
177c0ea7 46 * is returned.
e745ede7
DL
47 *
48 * 2) When a selection request is received (i.e.,
49 * when the specified mouse event occurs) the
50 * data pointer will be set to the data
51 * associated with the particular selection
52 * current at the time of the selection request
53 * and XM_SUCCESS is returned.
54 *
55 * 3) If no selection was current at the time a
56 * selection request is made the data pointer
57 * will be left untouched and XM_NO_SELECT will
58 * be returned.
59 *
177c0ea7 60 * 4) If the selection that was current at the time
e745ede7
DL
61 * a selection request is made is not an active
62 * selection the data pointer will be left
63 * untouched and XM_IA_SELECT will be returned.
64 *
65 * Since X processes events in an asynchronous manner
66 * it is likely that XMenuActivate will encounter
67 * a "foreign event" while it is executing. Foreign
68 * events are handled in one of three ways:
69 *
70 * 1) The event is discarded. This is the default
71 * mode and requires no action on the part of the
72 * application.
73 *
74 * 2) The application has identified an asynchronous
75 * event handler that will be called and the
76 * foreign event handed off to it. Note:
77 * AEQ mode disables this mode temporarily.
78 *
79 * 3) The application has enabled asynchronous event
80 * queuing mode. In this mode all foreign events
ee7683eb 81 * will be queued up until XMenuActivate
e745ede7
DL
82 * terminates; at which time they will be
83 * returned to the X event queue. As long as
84 * AEQ mode is enabled any asynchronous event
85 * handler as temporarily disabled.
86 *
87 * Any events encountered while taking down the menu
88 * (i.e., exposure events from occluded windows) will
89 * automatically be returned to the X event queue after
90 * XMenuActivate has cleaned the queue of any of its own
91 * events that are no longer needed.
92 *
93 * Author: Tony Della Fera, DEC
94 * March 12, 1986
95 *
96 */
97
98#include <config.h>
99#include "XMenuInt.h"
e89f4e4b 100#include <X11/keysym.h>
e745ede7 101
38ee86a8
JD
102/* For debug, set this to 0 to not grab the keyboard on menu popup */
103int x_menu_grab_keyboard = 1;
104
141dbd2b
JD
105static Wait_func wait_func;
106static void* wait_data;
107
108void
b782e2d7 109XMenuActivateSetWaitFunction (Wait_func func, void *data)
141dbd2b
JD
110{
111 wait_func = func;
112 wait_data = data;
113}
114
e745ede7 115int
b782e2d7
DN
116XMenuActivate(
117 register Display *display, /* Display to put menu on. */
118 register XMenu *menu, /* Menu to activate. */
119 int *p_num, /* Pane number selected. */
120 int *s_num, /* Selection number selected. */
121 int x_pos, /* X coordinate of menu position. */
122 int y_pos, /* Y coordinate of menu position. */
123 unsigned int event_mask, /* Mouse button event mask. */
124 char **data, /* Pointer to return data value. */
55660072 125 void (*help_callback) (char const *, int, int)) /* Help callback. */
e745ede7
DL
126{
127 int status; /* X routine call status. */
128 int orig_x; /* Upper left menu origin X coord. */
129 int orig_y; /* Upper left menu origin Y coord. */
130 int ret_val; /* Return value. */
131
132 register XMPane *p_ptr; /* Current XMPane. */
133 register XMPane *event_xmp; /* Event XMPane pointer. */
134 register XMPane *cur_p; /* Current pane. */
135 register XMSelect *cur_s; /* Current selection. */
136 XMWindow *event_xmw; /* Event XMWindow pointer. */
137 XEvent event; /* X input event. */
138 XEvent peek_event; /* X input peek ahead event. */
139
140 Bool selection = False; /* Selection has been made. */
141 Bool forward = True; /* Moving forward in the pane list. */
142
143 Window root, child;
144 int root_x, root_y, win_x, win_y;
145 unsigned int mask;
e89f4e4b 146 KeySym keysym;
e745ede7
DL
147
148 /*
149 * Define and allocate a foreign event queue to hold events
150 * that don't belong to XMenu. These events are later restored
151 * to the X event queue.
152 */
153 typedef struct _xmeventque {
154 XEvent event;
155 struct _xmeventque *next;
156 } XMEventQue;
157
158 XMEventQue *feq = NULL; /* Foreign event queue. */
159 XMEventQue *feq_tmp; /* Foreign event queue temporary. */
177c0ea7 160
e745ede7
DL
161 /*
162 * If there are no panes in the menu then return failure
163 * because the menu is not initialized.
164 */
165 if (menu->p_count == 0) {
166 _XMErrorCode = XME_NOT_INIT;
167 return(XM_FAILURE);
168 }
169
170 /*
171 * Find the desired current pane.
172 */
173 cur_p = _XMGetPanePtr(menu, *p_num);
174 if (cur_p == NULL) {
175 return(XM_FAILURE);
176 }
177 cur_p->activated = cur_p->active;
178
179 /*
180 * Find the desired current selection.
181 * If the current selection index is out of range a null current selection
182 * will be assumed and the cursor will be placed in the current pane
183 * header.
184 */
185 cur_s = _XMGetSelectionPtr(cur_p, *s_num);
186
187 /*
188 * Compute origin of menu so that cursor is in
189 * Correct pane and selection.
190 */
177c0ea7
JB
191 _XMTransToOrigin(display,
192 menu,
193 cur_p, cur_s,
194 x_pos, y_pos,
e745ede7
DL
195 &orig_x, &orig_y);
196 menu->x_pos = orig_x; /* Store X and Y coords of menu. */
197 menu->y_pos = orig_y;
177c0ea7 198
e745ede7
DL
199 if (XMenuRecompute(display, menu) == XM_FAILURE) {
200 return(XM_FAILURE);
201 }
202
203 /*
204 * Flush the window creation queue.
205 * This batches all window creates since lazy evaluation
206 * is more efficient than individual evaluation.
207 * This routine also does an XFlush().
208 */
209 if (_XMWinQueFlush(display, menu, cur_p, cur_s) == _FAILURE) {
210 return(XM_FAILURE);
211 }
212
213 /*
214 * Make sure windows are in correct order (in case we were passed
215 * an already created menu in incorrect order.)
216 */
217 for(p_ptr = menu->p_list->next; p_ptr != cur_p; p_ptr = p_ptr->next)
218 XRaiseWindow(display, p_ptr->window);
219 for(p_ptr = menu->p_list->prev; p_ptr != cur_p->prev; p_ptr = p_ptr->prev)
220 XRaiseWindow(display, p_ptr->window);
221
222 /*
223 * Make sure all selection windows are mapped.
224 */
225 for (
226 p_ptr = menu->p_list->next;
227 p_ptr != menu->p_list;
228 p_ptr = p_ptr->next
229 ){
230 XMapSubwindows(display, p_ptr->window);
231 }
232
233 /*
234 * Synchronize the X buffers and the event queue.
235 * From here on, all events in the queue that don't belong to
236 * XMenu are sent back to the application via an application
237 * provided event handler or discarded if the application has
238 * not provided an event handler.
239 */
240 XSync(display, 0);
177c0ea7 241
e745ede7
DL
242 /*
243 * Grab the mouse for menu input.
244 */
177c0ea7 245
e745ede7
DL
246 status = XGrabPointer(
247 display,
248 menu->parent,
249 True,
250 event_mask,
251 GrabModeAsync,
252 GrabModeAsync,
253 None,
254 menu->mouse_cursor,
255 CurrentTime
256 );
38ee86a8
JD
257 if (status == Success && x_menu_grab_keyboard)
258 {
259 status = XGrabKeyboard (display,
260 menu->parent,
261 False,
262 GrabModeAsync,
263 GrabModeAsync,
264 CurrentTime);
265 if (status != Success)
266 XUngrabPointer(display, CurrentTime);
267 }
177c0ea7 268
e745ede7
DL
269 if (status == _X_FAILURE) {
270 _XMErrorCode = XME_GRAB_MOUSE;
271 return(XM_FAILURE);
272 }
273
274 /*
275 * Map the menu panes.
276 */
277 XMapWindow(display, cur_p->window);
278 for (p_ptr = menu->p_list->next;
177c0ea7 279 p_ptr != cur_p;
e745ede7
DL
280 p_ptr = p_ptr->next)
281 XMapWindow(display, p_ptr->window);
282 for (p_ptr = cur_p->next;
283 p_ptr != menu->p_list;
284 p_ptr = p_ptr->next)
285 XMapWindow(display, p_ptr->window);
286
287 XRaiseWindow(display, cur_p->window); /* Make sure current */
288 /* pane is on top. */
177c0ea7 289
e745ede7
DL
290 cur_s = NULL; /* Clear current selection. */
291
292 /*
293 * Begin event processing loop.
294 */
295 while (1) {
141dbd2b 296 if (wait_func) (*wait_func) (wait_data);
e745ede7
DL
297 XNextEvent(display, &event); /* Get next event. */
298 switch (event.type) { /* Dispatch on the event type. */
299 case Expose:
300 event_xmp = (XMPane *)XLookUpAssoc(display,
177c0ea7 301 menu->assoc_tab,
e745ede7
DL
302 event.xexpose.window);
303 if (event_xmp == NULL) {
304 /*
305 * If AEQ mode is enabled then queue the event.
306 */
307 if (menu->aeq) {
308 feq_tmp = (XMEventQue *)malloc(sizeof(XMEventQue));
309 if (feq_tmp == NULL) {
310 _XMErrorCode = XME_CALLOC;
311 return(XM_FAILURE);
312 }
313 feq_tmp->event = event;
314 feq_tmp->next = feq;
315 feq = feq_tmp;
316 }
317 else if (_XMEventHandler) (*_XMEventHandler)(&event);
318 break;
319 }
320 if (event_xmp->activated) {
321 XSetWindowBackground(display,
177c0ea7 322 event_xmp->window,
e745ede7
DL
323 menu->bkgnd_color);
324 }
325 else {
326 XSetWindowBackgroundPixmap(display,
327 event_xmp->window,
328 menu->inact_pixmap);
329 }
330 _XMRefreshPane(display, menu, event_xmp);
331 break;
332 case EnterNotify:
177c0ea7 333 /*
e745ede7
DL
334 * First wait a small period of time, and see
335 * if another EnterNotify event follows hard on the
336 * heels of this one. i.e., the user is simply
337 * "passing through". If so, ignore this one.
338 */
177c0ea7 339
e745ede7
DL
340 event_xmw = (XMWindow *)XLookUpAssoc(display,
341 menu->assoc_tab,
342 event.xcrossing.window);
343 if (event_xmw == NULL) break;
344 if (event_xmw->type == SELECTION) {
345 /*
346 * We have entered a selection.
347 */
348 /* if (XPending(display) == 0) usleep(150000); */
349 if (XPending(display) != 0) {
350 XPeekEvent(display, &peek_event);
351 if(peek_event.type == LeaveNotify) {
352 break;
353 }
177c0ea7 354 }
e745ede7 355 cur_s = (XMSelect *)event_xmw;
aabb1e71
GM
356 help_callback (cur_s->help_string,
357 cur_p->serial, cur_s->serial);
177c0ea7 358
e745ede7
DL
359 /*
360 * If the pane we are in is active and the
361 * selection entered is active then activate
362 * the selection.
363 */
364 if (cur_p->active && cur_s->active > 0) {
365 cur_s->activated = 1;
366 _XMRefreshSelection(display, menu, cur_s);
367 }
368 }
369 else {
370 /*
371 * We have entered a pane.
372 */
373 /* if (XPending(display) == 0) usleep(150000); */
374 if (XPending(display) != 0) {
375 XPeekEvent(display, &peek_event);
376 if (peek_event.type == EnterNotify) break;
377 }
378 XQueryPointer(display,
379 menu->parent,
380 &root, &child,
381 &root_x, &root_y,
382 &win_x, &win_y,
383 &mask);
384 event_xmp = (XMPane *)XLookUpAssoc(display,
385 menu->assoc_tab,
386 child);
387 if (event_xmp == NULL) break;
388 if (event_xmp == cur_p) break;
389 if (event_xmp->serial > cur_p->serial) forward = True;
390 else forward = False;
391 p_ptr = cur_p;
392 while (p_ptr != event_xmp) {
393 if (forward) p_ptr = p_ptr->next;
394 else p_ptr = p_ptr->prev;
395 XRaiseWindow(display, p_ptr->window);
396 }
397 if (cur_p->activated) {
398 cur_p->activated = False;
399 XSetWindowBackgroundPixmap(display,
400 cur_p->window,
401 menu->inact_pixmap);
402 _XMRefreshPane(display, menu, cur_p);
403 }
404 if (event_xmp->active) event_xmp->activated = True;
405#if 1
406 /*
407 * i suspect the we don't get an EXPOSE event when backing
408 * store is enabled; the menu windows content is probably
409 * not drawn in when it should be in that case.
410 * in that case, this is probably an ugly fix!
411 * i hope someone more familiar with this code would
412 * take it from here. -- caveh@eng.sun.com.
413 */
414 XSetWindowBackground(display,
177c0ea7 415 event_xmp->window,
e745ede7
DL
416 menu->bkgnd_color);
417 _XMRefreshPane(display, menu, event_xmp);
418#endif
419 cur_p = event_xmp;
420 }
421 break;
422 case LeaveNotify:
423 event_xmw = (XMWindow *)XLookUpAssoc(
424 display,
425 menu->assoc_tab,
426 event.xcrossing.window
427 );
428 if (event_xmw == NULL) break;
429 if(cur_s == NULL) break;
177c0ea7 430
e745ede7
DL
431 /*
432 * If the current selection was activated then
433 * deactivate it.
434 */
435 if (cur_s->activated) {
436 cur_s->activated = False;
437 _XMRefreshSelection(display, menu, cur_s);
438 }
439 cur_s = NULL;
440 break;
177c0ea7 441
e745ede7
DL
442 case ButtonPress:
443 case ButtonRelease:
444 *p_num = cur_p->serial;
445 /*
446 * Check to see if there is a current selection.
447 */
448 if (cur_s != NULL) {
449 /*
450 * Set the selection number to the current selection.
451 */
452 *s_num = cur_s->serial;
453 /*
454 * If the current selection was activated then
455 * we have a valid selection otherwise we have
456 * an inactive selection.
457 */
458 if (cur_s->activated) {
459 *data = cur_s->data;
460 ret_val = XM_SUCCESS;
461 }
462 else {
463 ret_val = XM_IA_SELECT;
464 }
465 }
466 else {
467 /*
468 * No selection was current.
469 */
470 ret_val = XM_NO_SELECT;
471 }
472 selection = True;
473 break;
e89f4e4b
JD
474 case KeyPress:
475 case KeyRelease:
476 keysym = XLookupKeysym (&event.xkey, 0);
477
478 /* Pop down on C-g and Escape. */
479 if ((keysym == XK_g && (event.xkey.state & ControlMask) != 0)
480 || keysym == XK_Escape) /* Any escape, ignore modifiers. */
481 {
482 ret_val = XM_NO_SELECT;
483 selection = True;
484 }
485 break;
e745ede7
DL
486 default:
487 /*
488 * If AEQ mode is enabled then queue the event.
489 */
490 if (menu->aeq) {
491 feq_tmp = (XMEventQue *)malloc(sizeof(XMEventQue));
492 if (feq_tmp == NULL) {
493 _XMErrorCode = XME_CALLOC;
494 return(XM_FAILURE);
495 }
496 feq_tmp->event = event;
497 feq_tmp->next = feq;
498 feq = feq_tmp;
499 }
500 else if (_XMEventHandler) (*_XMEventHandler)(&event);
501 }
502 /*
503 * If a selection has been made, break out of the event loop.
504 */
505 if (selection == True) break;
506 }
507
508 /*
509 * Unmap the menu.
510 */
511 for ( p_ptr = menu->p_list->next;
512 p_ptr != menu->p_list;
177c0ea7 513 p_ptr = p_ptr->next)
e745ede7
DL
514 {
515 XUnmapWindow(display, p_ptr->window);
516 }
517
518 /*
519 * Ungrab the mouse.
520 */
521 XUngrabPointer(display, CurrentTime);
38ee86a8 522 XUngrabKeyboard(display, CurrentTime);
e745ede7 523
177c0ea7 524 /*
e745ede7
DL
525 * Restore bits under where the menu was if we managed
526 * to save them and free the pixmap.
527 */
528
529 /*
530 * If there is a current selection deactivate it.
531 */
532 if (cur_s != NULL) cur_s->activated = 0;
533
534 /*
535 * Deactivate the current pane.
536 */
537 cur_p->activated = 0;
538 XSetWindowBackgroundPixmap(display, cur_p->window, menu->inact_pixmap);
539
540 /*
541 * Synchronize the X buffers and the X event queue.
542 */
543 XSync(display, 0);
177c0ea7 544
e745ede7
DL
545 /*
546 * Dispatch any events remaining on the queue.
547 */
548 while (QLength(display)) {
549 /*
550 * Fetch the next event.
551 */
552 XNextEvent(display, &event);
553
554 /*
555 * Discard any events left on the queue that belong to XMenu.
556 * All others are held and then returned to the event queue.
557 */
558 switch (event.type) {
559 case Expose:
560 case EnterNotify:
561 case LeaveNotify:
562 case ButtonPress:
563 case ButtonRelease:
564 /*
565 * Does this event belong to one of XMenu's windows?
566 * If so, discard it and process the next event.
567 * If not fall through and treat it as a foreign event.
568 */
569 event_xmp = (XMPane *)XLookUpAssoc(
570 display,
571 menu->assoc_tab,
572 event.xbutton.window
573 );
574 if (event_xmp != NULL) continue;
575 default:
576 /*
577 * This is a foreign event.
578 * Queue it for later return to the X event queue.
579 */
580 feq_tmp = (XMEventQue *)malloc(sizeof(XMEventQue));
581 if (feq_tmp == NULL) {
582 _XMErrorCode = XME_CALLOC;
583 return(XM_FAILURE);
584 }
585 feq_tmp->event = event;
586 feq_tmp->next = feq;
587 feq = feq_tmp;
588 }
589 }
590 /*
591 * Return any foreign events that were queued to the X event queue.
592 */
593 while (feq != NULL) {
594 feq_tmp = feq;
595 XPutBackEvent(display, &feq_tmp->event);
596 feq = feq_tmp->next;
597 free((char *)feq_tmp);
598 }
177c0ea7 599
141dbd2b
JD
600 wait_func = 0;
601
e745ede7
DL
602 /*
603 * Return successfully.
604 */
605 _XMErrorCode = XME_NO_ERROR;
606 return(ret_val);
607
608}