Merge from emacs-23
[bpt/emacs.git] / oldXMenu / Activate.c
1 /* Copyright Massachusetts Institute of Technology 1985 */
2
3 #include "copyright.h"
4
5 /*
6 Copyright (C) 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010
7 Free Software Foundation, Inc.
8
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.
13
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.
18
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/>. */
21
22 /*
23 * XMenu: MIT Project Athena, X Window system menu package
24 *
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.
31 *
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.
39 *
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:
44 *
45 * 1) If at any time an error occurs the data
46 * pointer is left untouched and XM_FAILURE
47 * is returned.
48 *
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.
55 *
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.
60 *
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.
65 *
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:
70 *
71 * 1) The event is discarded. This is the default
72 * mode and requires no action on the part of the
73 * application.
74 *
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.
79 *
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.
87 *
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.
93 *
94 * Author: Tony Della Fera, DEC
95 * March 12, 1986
96 *
97 */
98
99 #include <config.h>
100 #include "XMenuInt.h"
101 #include <X11/keysym.h>
102
103 /* For debug, set this to 0 to not grab the keyboard on menu popup */
104 int x_menu_grab_keyboard = 1;
105
106 static Wait_func wait_func;
107 static void* wait_data;
108
109 void
110 XMenuActivateSetWaitFunction (Wait_func func, void *data)
111 {
112 wait_func = func;
113 wait_data = data;
114 }
115
116 int
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. */
127 {
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. */
132
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. */
140
141 Bool selection = False; /* Selection has been made. */
142 Bool forward = True; /* Moving forward in the pane list. */
143
144 Window root, child;
145 int root_x, root_y, win_x, win_y;
146 unsigned int mask;
147 KeySym keysym;
148
149 /*
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.
153 */
154 typedef struct _xmeventque {
155 XEvent event;
156 struct _xmeventque *next;
157 } XMEventQue;
158
159 XMEventQue *feq = NULL; /* Foreign event queue. */
160 XMEventQue *feq_tmp; /* Foreign event queue temporary. */
161
162 /*
163 * If there are no panes in the menu then return failure
164 * because the menu is not initialized.
165 */
166 if (menu->p_count == 0) {
167 _XMErrorCode = XME_NOT_INIT;
168 return(XM_FAILURE);
169 }
170
171 /*
172 * Find the desired current pane.
173 */
174 cur_p = _XMGetPanePtr(menu, *p_num);
175 if (cur_p == NULL) {
176 return(XM_FAILURE);
177 }
178 cur_p->activated = cur_p->active;
179
180 /*
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.
185 */
186 cur_s = _XMGetSelectionPtr(cur_p, *s_num);
187
188 /*
189 * Compute origin of menu so that cursor is in
190 * Correct pane and selection.
191 */
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;
199
200 if (XMenuRecompute(display, menu) == XM_FAILURE) {
201 return(XM_FAILURE);
202 }
203
204 /*
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().
209 */
210 if (_XMWinQueFlush(display, menu, cur_p, cur_s) == _FAILURE) {
211 return(XM_FAILURE);
212 }
213
214 /*
215 * Make sure windows are in correct order (in case we were passed
216 * an already created menu in incorrect order.)
217 */
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);
222
223 /*
224 * Make sure all selection windows are mapped.
225 */
226 for (
227 p_ptr = menu->p_list->next;
228 p_ptr != menu->p_list;
229 p_ptr = p_ptr->next
230 ){
231 XMapSubwindows(display, p_ptr->window);
232 }
233
234 /*
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.
240 */
241 XSync(display, 0);
242
243 /*
244 * Grab the mouse for menu input.
245 */
246
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
257 );
258 if (status == Success && x_menu_grab_keyboard)
259 {
260 status = XGrabKeyboard (display,
261 menu->parent,
262 False,
263 GrabModeAsync,
264 GrabModeAsync,
265 CurrentTime);
266 if (status != Success)
267 XUngrabPointer(display, CurrentTime);
268 }
269
270 if (status == _X_FAILURE) {
271 _XMErrorCode = XME_GRAB_MOUSE;
272 return(XM_FAILURE);
273 }
274
275 /*
276 * Map the menu panes.
277 */
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);
287
288 XRaiseWindow(display, cur_p->window); /* Make sure current */
289 /* pane is on top. */
290
291 cur_s = NULL; /* Clear current selection. */
292
293 /*
294 * Begin event processing loop.
295 */
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) {
305 /*
306 * If AEQ mode is enabled then queue the event.
307 */
308 if (menu->aeq) {
309 feq_tmp = (XMEventQue *)malloc(sizeof(XMEventQue));
310 if (feq_tmp == NULL) {
311 _XMErrorCode = XME_CALLOC;
312 return(XM_FAILURE);
313 }
314 feq_tmp->event = event;
315 feq_tmp->next = feq;
316 feq = feq_tmp;
317 }
318 else if (_XMEventHandler) (*_XMEventHandler)(&event);
319 break;
320 }
321 if (event_xmp->activated) {
322 XSetWindowBackground(display,
323 event_xmp->window,
324 menu->bkgnd_color);
325 }
326 else {
327 XSetWindowBackgroundPixmap(display,
328 event_xmp->window,
329 menu->inact_pixmap);
330 }
331 _XMRefreshPane(display, menu, event_xmp);
332 break;
333 case EnterNotify:
334 /*
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.
339 */
340
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) {
346 /*
347 * We have entered a selection.
348 */
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;
354 }
355 }
356 cur_s = (XMSelect *)event_xmw;
357 help_callback (cur_s->help_string,
358 cur_p->serial, cur_s->serial);
359
360 /*
361 * If the pane we are in is active and the
362 * selection entered is active then activate
363 * the selection.
364 */
365 if (cur_p->active && cur_s->active > 0) {
366 cur_s->activated = 1;
367 _XMRefreshSelection(display, menu, cur_s);
368 }
369 }
370 else {
371 /*
372 * We have entered a pane.
373 */
374 /* if (XPending(display) == 0) usleep(150000); */
375 if (XPending(display) != 0) {
376 XPeekEvent(display, &peek_event);
377 if (peek_event.type == EnterNotify) break;
378 }
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);
397 }
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);
404 }
405 if (event_xmp->active) event_xmp->activated = True;
406 #if 1
407 /*
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.
414 */
415 XSetWindowBackground(display,
416 event_xmp->window,
417 menu->bkgnd_color);
418 _XMRefreshPane(display, menu, event_xmp);
419 #endif
420 cur_p = event_xmp;
421 }
422 break;
423 case LeaveNotify:
424 event_xmw = (XMWindow *)XLookUpAssoc(
425 display,
426 menu->assoc_tab,
427 event.xcrossing.window
428 );
429 if (event_xmw == NULL) break;
430 if(cur_s == NULL) break;
431
432 /*
433 * If the current selection was activated then
434 * deactivate it.
435 */
436 if (cur_s->activated) {
437 cur_s->activated = False;
438 _XMRefreshSelection(display, menu, cur_s);
439 }
440 cur_s = NULL;
441 break;
442
443 case ButtonPress:
444 case ButtonRelease:
445 *p_num = cur_p->serial;
446 /*
447 * Check to see if there is a current selection.
448 */
449 if (cur_s != NULL) {
450 /*
451 * Set the selection number to the current selection.
452 */
453 *s_num = cur_s->serial;
454 /*
455 * If the current selection was activated then
456 * we have a valid selection otherwise we have
457 * an inactive selection.
458 */
459 if (cur_s->activated) {
460 *data = cur_s->data;
461 ret_val = XM_SUCCESS;
462 }
463 else {
464 ret_val = XM_IA_SELECT;
465 }
466 }
467 else {
468 /*
469 * No selection was current.
470 */
471 ret_val = XM_NO_SELECT;
472 }
473 selection = True;
474 break;
475 case KeyPress:
476 case KeyRelease:
477 keysym = XLookupKeysym (&event.xkey, 0);
478
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. */
482 {
483 ret_val = XM_NO_SELECT;
484 selection = True;
485 }
486 break;
487 default:
488 /*
489 * If AEQ mode is enabled then queue the event.
490 */
491 if (menu->aeq) {
492 feq_tmp = (XMEventQue *)malloc(sizeof(XMEventQue));
493 if (feq_tmp == NULL) {
494 _XMErrorCode = XME_CALLOC;
495 return(XM_FAILURE);
496 }
497 feq_tmp->event = event;
498 feq_tmp->next = feq;
499 feq = feq_tmp;
500 }
501 else if (_XMEventHandler) (*_XMEventHandler)(&event);
502 }
503 /*
504 * If a selection has been made, break out of the event loop.
505 */
506 if (selection == True) break;
507 }
508
509 /*
510 * Unmap the menu.
511 */
512 for ( p_ptr = menu->p_list->next;
513 p_ptr != menu->p_list;
514 p_ptr = p_ptr->next)
515 {
516 XUnmapWindow(display, p_ptr->window);
517 }
518
519 /*
520 * Ungrab the mouse.
521 */
522 XUngrabPointer(display, CurrentTime);
523 XUngrabKeyboard(display, CurrentTime);
524
525 /*
526 * Restore bits under where the menu was if we managed
527 * to save them and free the pixmap.
528 */
529
530 /*
531 * If there is a current selection deactivate it.
532 */
533 if (cur_s != NULL) cur_s->activated = 0;
534
535 /*
536 * Deactivate the current pane.
537 */
538 cur_p->activated = 0;
539 XSetWindowBackgroundPixmap(display, cur_p->window, menu->inact_pixmap);
540
541 /*
542 * Synchronize the X buffers and the X event queue.
543 */
544 XSync(display, 0);
545
546 /*
547 * Dispatch any events remaining on the queue.
548 */
549 while (QLength(display)) {
550 /*
551 * Fetch the next event.
552 */
553 XNextEvent(display, &event);
554
555 /*
556 * Discard any events left on the queue that belong to XMenu.
557 * All others are held and then returned to the event queue.
558 */
559 switch (event.type) {
560 case Expose:
561 case EnterNotify:
562 case LeaveNotify:
563 case ButtonPress:
564 case ButtonRelease:
565 /*
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.
569 */
570 event_xmp = (XMPane *)XLookUpAssoc(
571 display,
572 menu->assoc_tab,
573 event.xbutton.window
574 );
575 if (event_xmp != NULL) continue;
576 default:
577 /*
578 * This is a foreign event.
579 * Queue it for later return to the X event queue.
580 */
581 feq_tmp = (XMEventQue *)malloc(sizeof(XMEventQue));
582 if (feq_tmp == NULL) {
583 _XMErrorCode = XME_CALLOC;
584 return(XM_FAILURE);
585 }
586 feq_tmp->event = event;
587 feq_tmp->next = feq;
588 feq = feq_tmp;
589 }
590 }
591 /*
592 * Return any foreign events that were queued to the X event queue.
593 */
594 while (feq != NULL) {
595 feq_tmp = feq;
596 XPutBackEvent(display, &feq_tmp->event);
597 feq = feq_tmp->next;
598 free((char *)feq_tmp);
599 }
600
601 wait_func = 0;
602
603 /*
604 * Return successfully.
605 */
606 _XMErrorCode = XME_NO_ERROR;
607 return(ret_val);
608
609 }
610
611 /* arch-tag: 6b90b578-ecea-4328-b460-a0c96963f872
612 (do not change this comment) */