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