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