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