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