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