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