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