Merge from emacs-24; up to 2012-12-06T01:39:03Z!monnier@iro.umontreal.ca
[bpt/emacs.git] / oldXMenu / Activate.c
CommitLineData
e745ede7
DL
1/* Copyright Massachusetts Institute of Technology 1985 */
2
3#include "copyright.h"
4
54861d5e 5/*
ab422c4d 6Copyright (C) 2001-2013 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
e745ede7 98#include "XMenuInt.h"
e89f4e4b 99#include <X11/keysym.h>
e745ede7 100
38ee86a8
JD
101/* For debug, set this to 0 to not grab the keyboard on menu popup */
102int x_menu_grab_keyboard = 1;
103
141dbd2b
JD
104static Wait_func wait_func;
105static void* wait_data;
106
107void
b782e2d7 108XMenuActivateSetWaitFunction (Wait_func func, void *data)
141dbd2b
JD
109{
110 wait_func = func;
111 wait_data = data;
112}
113
e745ede7 114int
b782e2d7
DN
115XMenuActivate(
116 register Display *display, /* Display to put menu on. */
117 register XMenu *menu, /* Menu to activate. */
118 int *p_num, /* Pane number selected. */
119 int *s_num, /* Selection number selected. */
120 int x_pos, /* X coordinate of menu position. */
121 int y_pos, /* Y coordinate of menu position. */
122 unsigned int event_mask, /* Mouse button event mask. */
123 char **data, /* Pointer to return data value. */
55660072 124 void (*help_callback) (char const *, int, int)) /* Help callback. */
e745ede7
DL
125{
126 int status; /* X routine call status. */
127 int orig_x; /* Upper left menu origin X coord. */
128 int orig_y; /* Upper left menu origin Y coord. */
129 int ret_val; /* Return value. */
130
131 register XMPane *p_ptr; /* Current XMPane. */
132 register XMPane *event_xmp; /* Event XMPane pointer. */
133 register XMPane *cur_p; /* Current pane. */
134 register XMSelect *cur_s; /* Current selection. */
135 XMWindow *event_xmw; /* Event XMWindow pointer. */
136 XEvent event; /* X input event. */
137 XEvent peek_event; /* X input peek ahead event. */
138
139 Bool selection = False; /* Selection has been made. */
140 Bool forward = True; /* Moving forward in the pane list. */
141
142 Window root, child;
143 int root_x, root_y, win_x, win_y;
144 unsigned int mask;
e89f4e4b 145 KeySym keysym;
e745ede7
DL
146
147 /*
148 * Define and allocate a foreign event queue to hold events
149 * that don't belong to XMenu. These events are later restored
150 * to the X event queue.
151 */
152 typedef struct _xmeventque {
153 XEvent event;
154 struct _xmeventque *next;
155 } XMEventQue;
156
157 XMEventQue *feq = NULL; /* Foreign event queue. */
158 XMEventQue *feq_tmp; /* Foreign event queue temporary. */
177c0ea7 159
e745ede7
DL
160 /*
161 * If there are no panes in the menu then return failure
162 * because the menu is not initialized.
163 */
164 if (menu->p_count == 0) {
165 _XMErrorCode = XME_NOT_INIT;
166 return(XM_FAILURE);
167 }
168
169 /*
170 * Find the desired current pane.
171 */
172 cur_p = _XMGetPanePtr(menu, *p_num);
173 if (cur_p == NULL) {
174 return(XM_FAILURE);
175 }
176 cur_p->activated = cur_p->active;
177
178 /*
179 * Find the desired current selection.
180 * If the current selection index is out of range a null current selection
181 * will be assumed and the cursor will be placed in the current pane
182 * header.
183 */
184 cur_s = _XMGetSelectionPtr(cur_p, *s_num);
185
186 /*
187 * Compute origin of menu so that cursor is in
188 * Correct pane and selection.
189 */
177c0ea7
JB
190 _XMTransToOrigin(display,
191 menu,
192 cur_p, cur_s,
193 x_pos, y_pos,
e745ede7
DL
194 &orig_x, &orig_y);
195 menu->x_pos = orig_x; /* Store X and Y coords of menu. */
196 menu->y_pos = orig_y;
177c0ea7 197
e745ede7
DL
198 if (XMenuRecompute(display, menu) == XM_FAILURE) {
199 return(XM_FAILURE);
200 }
201
202 /*
203 * Flush the window creation queue.
204 * This batches all window creates since lazy evaluation
205 * is more efficient than individual evaluation.
206 * This routine also does an XFlush().
207 */
208 if (_XMWinQueFlush(display, menu, cur_p, cur_s) == _FAILURE) {
209 return(XM_FAILURE);
210 }
211
212 /*
213 * Make sure windows are in correct order (in case we were passed
214 * an already created menu in incorrect order.)
215 */
216 for(p_ptr = menu->p_list->next; p_ptr != cur_p; p_ptr = p_ptr->next)
217 XRaiseWindow(display, p_ptr->window);
218 for(p_ptr = menu->p_list->prev; p_ptr != cur_p->prev; p_ptr = p_ptr->prev)
219 XRaiseWindow(display, p_ptr->window);
220
221 /*
222 * Make sure all selection windows are mapped.
223 */
224 for (
225 p_ptr = menu->p_list->next;
226 p_ptr != menu->p_list;
227 p_ptr = p_ptr->next
228 ){
229 XMapSubwindows(display, p_ptr->window);
230 }
231
232 /*
233 * Synchronize the X buffers and the event queue.
234 * From here on, all events in the queue that don't belong to
235 * XMenu are sent back to the application via an application
236 * provided event handler or discarded if the application has
237 * not provided an event handler.
238 */
239 XSync(display, 0);
177c0ea7 240
e745ede7
DL
241 /*
242 * Grab the mouse for menu input.
243 */
177c0ea7 244
e745ede7
DL
245 status = XGrabPointer(
246 display,
247 menu->parent,
248 True,
249 event_mask,
250 GrabModeAsync,
251 GrabModeAsync,
252 None,
253 menu->mouse_cursor,
254 CurrentTime
255 );
38ee86a8
JD
256 if (status == Success && x_menu_grab_keyboard)
257 {
258 status = XGrabKeyboard (display,
259 menu->parent,
260 False,
261 GrabModeAsync,
262 GrabModeAsync,
263 CurrentTime);
264 if (status != Success)
265 XUngrabPointer(display, CurrentTime);
266 }
177c0ea7 267
e745ede7
DL
268 if (status == _X_FAILURE) {
269 _XMErrorCode = XME_GRAB_MOUSE;
270 return(XM_FAILURE);
271 }
272
273 /*
274 * Map the menu panes.
275 */
276 XMapWindow(display, cur_p->window);
277 for (p_ptr = menu->p_list->next;
177c0ea7 278 p_ptr != cur_p;
e745ede7
DL
279 p_ptr = p_ptr->next)
280 XMapWindow(display, p_ptr->window);
281 for (p_ptr = cur_p->next;
282 p_ptr != menu->p_list;
283 p_ptr = p_ptr->next)
284 XMapWindow(display, p_ptr->window);
285
286 XRaiseWindow(display, cur_p->window); /* Make sure current */
287 /* pane is on top. */
177c0ea7 288
e745ede7
DL
289 cur_s = NULL; /* Clear current selection. */
290
291 /*
292 * Begin event processing loop.
293 */
294 while (1) {
141dbd2b 295 if (wait_func) (*wait_func) (wait_data);
e745ede7
DL
296 XNextEvent(display, &event); /* Get next event. */
297 switch (event.type) { /* Dispatch on the event type. */
298 case Expose:
299 event_xmp = (XMPane *)XLookUpAssoc(display,
177c0ea7 300 menu->assoc_tab,
e745ede7
DL
301 event.xexpose.window);
302 if (event_xmp == NULL) {
303 /*
304 * If AEQ mode is enabled then queue the event.
305 */
306 if (menu->aeq) {
307 feq_tmp = (XMEventQue *)malloc(sizeof(XMEventQue));
308 if (feq_tmp == NULL) {
309 _XMErrorCode = XME_CALLOC;
310 return(XM_FAILURE);
311 }
312 feq_tmp->event = event;
313 feq_tmp->next = feq;
314 feq = feq_tmp;
315 }
316 else if (_XMEventHandler) (*_XMEventHandler)(&event);
317 break;
318 }
319 if (event_xmp->activated) {
320 XSetWindowBackground(display,
177c0ea7 321 event_xmp->window,
e745ede7
DL
322 menu->bkgnd_color);
323 }
324 else {
325 XSetWindowBackgroundPixmap(display,
326 event_xmp->window,
327 menu->inact_pixmap);
328 }
329 _XMRefreshPane(display, menu, event_xmp);
330 break;
331 case EnterNotify:
177c0ea7 332 /*
e745ede7
DL
333 * First wait a small period of time, and see
334 * if another EnterNotify event follows hard on the
335 * heels of this one. i.e., the user is simply
336 * "passing through". If so, ignore this one.
337 */
177c0ea7 338
e745ede7
DL
339 event_xmw = (XMWindow *)XLookUpAssoc(display,
340 menu->assoc_tab,
341 event.xcrossing.window);
342 if (event_xmw == NULL) break;
343 if (event_xmw->type == SELECTION) {
344 /*
345 * We have entered a selection.
346 */
347 /* if (XPending(display) == 0) usleep(150000); */
348 if (XPending(display) != 0) {
349 XPeekEvent(display, &peek_event);
350 if(peek_event.type == LeaveNotify) {
351 break;
352 }
177c0ea7 353 }
e745ede7 354 cur_s = (XMSelect *)event_xmw;
aabb1e71
GM
355 help_callback (cur_s->help_string,
356 cur_p->serial, cur_s->serial);
177c0ea7 357
e745ede7
DL
358 /*
359 * If the pane we are in is active and the
360 * selection entered is active then activate
361 * the selection.
362 */
363 if (cur_p->active && cur_s->active > 0) {
364 cur_s->activated = 1;
365 _XMRefreshSelection(display, menu, cur_s);
366 }
367 }
368 else {
369 /*
370 * We have entered a pane.
371 */
372 /* if (XPending(display) == 0) usleep(150000); */
373 if (XPending(display) != 0) {
374 XPeekEvent(display, &peek_event);
375 if (peek_event.type == EnterNotify) break;
376 }
377 XQueryPointer(display,
378 menu->parent,
379 &root, &child,
380 &root_x, &root_y,
381 &win_x, &win_y,
382 &mask);
383 event_xmp = (XMPane *)XLookUpAssoc(display,
384 menu->assoc_tab,
385 child);
386 if (event_xmp == NULL) break;
387 if (event_xmp == cur_p) break;
388 if (event_xmp->serial > cur_p->serial) forward = True;
389 else forward = False;
390 p_ptr = cur_p;
391 while (p_ptr != event_xmp) {
392 if (forward) p_ptr = p_ptr->next;
393 else p_ptr = p_ptr->prev;
394 XRaiseWindow(display, p_ptr->window);
395 }
396 if (cur_p->activated) {
397 cur_p->activated = False;
398 XSetWindowBackgroundPixmap(display,
399 cur_p->window,
400 menu->inact_pixmap);
401 _XMRefreshPane(display, menu, cur_p);
402 }
403 if (event_xmp->active) event_xmp->activated = True;
404#if 1
405 /*
406 * i suspect the we don't get an EXPOSE event when backing
407 * store is enabled; the menu windows content is probably
408 * not drawn in when it should be in that case.
409 * in that case, this is probably an ugly fix!
410 * i hope someone more familiar with this code would
411 * take it from here. -- caveh@eng.sun.com.
412 */
413 XSetWindowBackground(display,
177c0ea7 414 event_xmp->window,
e745ede7
DL
415 menu->bkgnd_color);
416 _XMRefreshPane(display, menu, event_xmp);
417#endif
418 cur_p = event_xmp;
419 }
420 break;
421 case LeaveNotify:
422 event_xmw = (XMWindow *)XLookUpAssoc(
423 display,
424 menu->assoc_tab,
425 event.xcrossing.window
426 );
427 if (event_xmw == NULL) break;
428 if(cur_s == NULL) break;
177c0ea7 429
e745ede7
DL
430 /*
431 * If the current selection was activated then
432 * deactivate it.
433 */
434 if (cur_s->activated) {
435 cur_s->activated = False;
436 _XMRefreshSelection(display, menu, cur_s);
437 }
438 cur_s = NULL;
439 break;
177c0ea7 440
e745ede7
DL
441 case ButtonPress:
442 case ButtonRelease:
443 *p_num = cur_p->serial;
444 /*
445 * Check to see if there is a current selection.
446 */
447 if (cur_s != NULL) {
448 /*
449 * Set the selection number to the current selection.
450 */
451 *s_num = cur_s->serial;
452 /*
453 * If the current selection was activated then
454 * we have a valid selection otherwise we have
455 * an inactive selection.
456 */
457 if (cur_s->activated) {
458 *data = cur_s->data;
459 ret_val = XM_SUCCESS;
460 }
461 else {
462 ret_val = XM_IA_SELECT;
463 }
464 }
465 else {
466 /*
467 * No selection was current.
468 */
469 ret_val = XM_NO_SELECT;
470 }
471 selection = True;
472 break;
e89f4e4b
JD
473 case KeyPress:
474 case KeyRelease:
475 keysym = XLookupKeysym (&event.xkey, 0);
476
477 /* Pop down on C-g and Escape. */
478 if ((keysym == XK_g && (event.xkey.state & ControlMask) != 0)
479 || keysym == XK_Escape) /* Any escape, ignore modifiers. */
480 {
481 ret_val = XM_NO_SELECT;
482 selection = True;
483 }
484 break;
e745ede7
DL
485 default:
486 /*
487 * If AEQ mode is enabled then queue the event.
488 */
489 if (menu->aeq) {
490 feq_tmp = (XMEventQue *)malloc(sizeof(XMEventQue));
491 if (feq_tmp == NULL) {
492 _XMErrorCode = XME_CALLOC;
493 return(XM_FAILURE);
494 }
495 feq_tmp->event = event;
496 feq_tmp->next = feq;
497 feq = feq_tmp;
498 }
499 else if (_XMEventHandler) (*_XMEventHandler)(&event);
500 }
501 /*
502 * If a selection has been made, break out of the event loop.
503 */
504 if (selection == True) break;
505 }
506
507 /*
508 * Unmap the menu.
509 */
510 for ( p_ptr = menu->p_list->next;
511 p_ptr != menu->p_list;
177c0ea7 512 p_ptr = p_ptr->next)
e745ede7
DL
513 {
514 XUnmapWindow(display, p_ptr->window);
515 }
516
517 /*
518 * Ungrab the mouse.
519 */
520 XUngrabPointer(display, CurrentTime);
38ee86a8 521 XUngrabKeyboard(display, CurrentTime);
e745ede7 522
177c0ea7 523 /*
e745ede7
DL
524 * Restore bits under where the menu was if we managed
525 * to save them and free the pixmap.
526 */
527
528 /*
529 * If there is a current selection deactivate it.
530 */
531 if (cur_s != NULL) cur_s->activated = 0;
532
533 /*
534 * Deactivate the current pane.
535 */
536 cur_p->activated = 0;
537 XSetWindowBackgroundPixmap(display, cur_p->window, menu->inact_pixmap);
538
539 /*
540 * Synchronize the X buffers and the X event queue.
541 */
542 XSync(display, 0);
177c0ea7 543
e745ede7
DL
544 /*
545 * Dispatch any events remaining on the queue.
546 */
547 while (QLength(display)) {
548 /*
549 * Fetch the next event.
550 */
551 XNextEvent(display, &event);
552
553 /*
554 * Discard any events left on the queue that belong to XMenu.
555 * All others are held and then returned to the event queue.
556 */
557 switch (event.type) {
558 case Expose:
559 case EnterNotify:
560 case LeaveNotify:
561 case ButtonPress:
562 case ButtonRelease:
563 /*
564 * Does this event belong to one of XMenu's windows?
565 * If so, discard it and process the next event.
566 * If not fall through and treat it as a foreign event.
567 */
568 event_xmp = (XMPane *)XLookUpAssoc(
569 display,
570 menu->assoc_tab,
571 event.xbutton.window
572 );
573 if (event_xmp != NULL) continue;
574 default:
575 /*
576 * This is a foreign event.
577 * Queue it for later return to the X event queue.
578 */
579 feq_tmp = (XMEventQue *)malloc(sizeof(XMEventQue));
580 if (feq_tmp == NULL) {
581 _XMErrorCode = XME_CALLOC;
582 return(XM_FAILURE);
583 }
584 feq_tmp->event = event;
585 feq_tmp->next = feq;
586 feq = feq_tmp;
587 }
588 }
589 /*
590 * Return any foreign events that were queued to the X event queue.
591 */
592 while (feq != NULL) {
593 feq_tmp = feq;
594 XPutBackEvent(display, &feq_tmp->event);
595 feq = feq_tmp->next;
596 free((char *)feq_tmp);
597 }
177c0ea7 598
141dbd2b
JD
599 wait_func = 0;
600
e745ede7
DL
601 /*
602 * Return successfully.
603 */
604 _XMErrorCode = XME_NO_ERROR;
605 return(ret_val);
606
607}