(menu_highlight_callback): Call show_help_echo directly
[bpt/emacs.git] / src / xmenu.c
1 /* X Communication module for terminals which understand the X protocol.
2 Copyright (C) 1986, 88, 93, 94, 96, 1999 Free Software Foundation, Inc.
3
4 This file is part of GNU Emacs.
5
6 GNU Emacs is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
9 any later version.
10
11 GNU Emacs is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with GNU Emacs; see the file COPYING. If not, write to
18 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA. */
20
21 /* X pop-up deck-of-cards menu facility for gnuemacs.
22 *
23 * Written by Jon Arnold and Roman Budzianowski
24 * Mods and rewrite by Robert Krawitz
25 *
26 */
27
28 /* Modified by Fred Pierresteguy on December 93
29 to make the popup menus and menubar use the Xt. */
30
31 /* Rewritten for clarity and GC protection by rms in Feb 94. */
32
33 #include <config.h>
34
35 /* On 4.3 this loses if it comes after xterm.h. */
36 #include <signal.h>
37
38 #include <stdio.h>
39 #include "lisp.h"
40 #include "termhooks.h"
41 #include "frame.h"
42 #include "window.h"
43 #include "keyboard.h"
44 #include "blockinput.h"
45 #include "buffer.h"
46
47 #ifdef MSDOS
48 #include "msdos.h"
49 #endif
50
51 #ifdef HAVE_X_WINDOWS
52 /* This may include sys/types.h, and that somehow loses
53 if this is not done before the other system files. */
54 #include "xterm.h"
55 #endif
56
57 /* Load sys/types.h if not already loaded.
58 In some systems loading it twice is suicidal. */
59 #ifndef makedev
60 #include <sys/types.h>
61 #endif
62
63 #include "dispextern.h"
64
65 #ifdef HAVE_X_WINDOWS
66 #undef HAVE_MULTILINGUAL_MENU
67 #ifdef USE_X_TOOLKIT
68 #include <X11/Xlib.h>
69 #include <X11/IntrinsicP.h>
70 #include <X11/CoreP.h>
71 #include <X11/StringDefs.h>
72 #include <X11/Shell.h>
73 #ifdef USE_LUCID
74 #include <X11/Xaw/Paned.h>
75 #endif /* USE_LUCID */
76 #include "../lwlib/lwlib.h"
77 #else /* not USE_X_TOOLKIT */
78 #include "../oldXMenu/XMenu.h"
79 #endif /* not USE_X_TOOLKIT */
80 #endif /* HAVE_X_WINDOWS */
81
82 #ifdef USE_MOTIF
83 #include <Xm/Xm.h> /* for LESSTIF_VERSION */
84 #endif
85
86 #define min(x,y) (((x) < (y)) ? (x) : (y))
87 #define max(x,y) (((x) > (y)) ? (x) : (y))
88
89 #ifndef TRUE
90 #define TRUE 1
91 #define FALSE 0
92 #endif /* no TRUE */
93
94 Lisp_Object Vmenu_updating_frame;
95
96 Lisp_Object Qdebug_on_next_call;
97
98 extern Lisp_Object Qmenu_bar;
99 extern Lisp_Object Qmouse_click, Qevent_kind;
100
101 extern Lisp_Object QCtoggle, QCradio;
102
103 extern Lisp_Object Voverriding_local_map;
104 extern Lisp_Object Voverriding_local_map_menu_flag;
105
106 extern Lisp_Object Qoverriding_local_map, Qoverriding_terminal_local_map;
107
108 extern Lisp_Object Qmenu_bar_update_hook;
109
110 #ifdef USE_X_TOOLKIT
111 extern void set_frame_menubar ();
112 extern void process_expose_from_menu ();
113 extern XtAppContext Xt_app_con;
114
115 static Lisp_Object xdialog_show ();
116 void popup_get_selection ();
117 #endif
118
119 #ifdef USE_X_TOOLKIT
120
121 /* Define HAVE_BOXES if meus can handle radio and toggle buttons. */
122
123 #define HAVE_BOXES 1
124 #endif
125
126 static void push_menu_item P_ ((Lisp_Object, Lisp_Object, Lisp_Object,
127 Lisp_Object, Lisp_Object, Lisp_Object,
128 Lisp_Object, Lisp_Object));
129 static Lisp_Object xmenu_show ();
130 static void keymap_panes ();
131 static void single_keymap_panes ();
132 static void single_menu_item ();
133 static void list_of_panes ();
134 static void list_of_items ();
135 \f
136 /* This holds a Lisp vector that holds the results of decoding
137 the keymaps or alist-of-alists that specify a menu.
138
139 It describes the panes and items within the panes.
140
141 Each pane is described by 3 elements in the vector:
142 t, the pane name, the pane's prefix key.
143 Then follow the pane's items, with 5 elements per item:
144 the item string, the enable flag, the item's value,
145 the definition, and the equivalent keyboard key's description string.
146
147 In some cases, multiple levels of menus may be described.
148 A single vector slot containing nil indicates the start of a submenu.
149 A single vector slot containing lambda indicates the end of a submenu.
150 The submenu follows a menu item which is the way to reach the submenu.
151
152 A single vector slot containing quote indicates that the
153 following items should appear on the right of a dialog box.
154
155 Using a Lisp vector to hold this information while we decode it
156 takes care of protecting all the data from GC. */
157
158 #define MENU_ITEMS_PANE_NAME 1
159 #define MENU_ITEMS_PANE_PREFIX 2
160 #define MENU_ITEMS_PANE_LENGTH 3
161
162 enum menu_item_idx
163 {
164 MENU_ITEMS_ITEM_NAME = 0,
165 MENU_ITEMS_ITEM_ENABLE,
166 MENU_ITEMS_ITEM_VALUE,
167 MENU_ITEMS_ITEM_EQUIV_KEY,
168 MENU_ITEMS_ITEM_DEFINITION,
169 MENU_ITEMS_ITEM_TYPE,
170 MENU_ITEMS_ITEM_SELECTED,
171 MENU_ITEMS_ITEM_HELP,
172 MENU_ITEMS_ITEM_LENGTH
173 };
174
175 static Lisp_Object menu_items;
176
177 /* Number of slots currently allocated in menu_items. */
178 static int menu_items_allocated;
179
180 /* This is the index in menu_items of the first empty slot. */
181 static int menu_items_used;
182
183 /* The number of panes currently recorded in menu_items,
184 excluding those within submenus. */
185 static int menu_items_n_panes;
186
187 /* Current depth within submenus. */
188 static int menu_items_submenu_depth;
189
190 /* Flag which when set indicates a dialog or menu has been posted by
191 Xt on behalf of one of the widget sets. */
192 int popup_activated_flag;
193
194 static int next_menubar_widget_id;
195
196 /* This is set nonzero after the user activates the menu bar, and set
197 to zero again after the menu bars are redisplayed by prepare_menu_bar.
198 While it is nonzero, all calls to set_frame_menubar go deep.
199
200 I don't understand why this is needed, but it does seem to be
201 needed on Motif, according to Marcus Daniels <marcus@sysc.pdx.edu>. */
202
203 int pending_menu_activation;
204 \f
205 #ifdef USE_X_TOOLKIT
206
207 /* Return the frame whose ->output_data.x->id equals ID, or 0 if none. */
208
209 static struct frame *
210 menubar_id_to_frame (id)
211 LWLIB_ID id;
212 {
213 Lisp_Object tail, frame;
214 FRAME_PTR f;
215
216 for (tail = Vframe_list; GC_CONSP (tail); tail = XCDR (tail))
217 {
218 frame = XCAR (tail);
219 if (!GC_FRAMEP (frame))
220 continue;
221 f = XFRAME (frame);
222 if (!FRAME_WINDOW_P (f))
223 continue;
224 if (f->output_data.x->id == id)
225 return f;
226 }
227 return 0;
228 }
229
230 #endif
231 \f
232 /* Initialize the menu_items structure if we haven't already done so.
233 Also mark it as currently empty. */
234
235 static void
236 init_menu_items ()
237 {
238 if (NILP (menu_items))
239 {
240 menu_items_allocated = 60;
241 menu_items = Fmake_vector (make_number (menu_items_allocated), Qnil);
242 }
243
244 menu_items_used = 0;
245 menu_items_n_panes = 0;
246 menu_items_submenu_depth = 0;
247 }
248
249 /* Call at the end of generating the data in menu_items.
250 This fills in the number of items in the last pane. */
251
252 static void
253 finish_menu_items ()
254 {
255 }
256
257 /* Call when finished using the data for the current menu
258 in menu_items. */
259
260 static void
261 discard_menu_items ()
262 {
263 /* Free the structure if it is especially large.
264 Otherwise, hold on to it, to save time. */
265 if (menu_items_allocated > 200)
266 {
267 menu_items = Qnil;
268 menu_items_allocated = 0;
269 }
270 }
271
272 /* Make the menu_items vector twice as large. */
273
274 static void
275 grow_menu_items ()
276 {
277 Lisp_Object old;
278 int old_size = menu_items_allocated;
279 old = menu_items;
280
281 menu_items_allocated *= 2;
282 menu_items = Fmake_vector (make_number (menu_items_allocated), Qnil);
283 bcopy (XVECTOR (old)->contents, XVECTOR (menu_items)->contents,
284 old_size * sizeof (Lisp_Object));
285 }
286
287 /* Begin a submenu. */
288
289 static void
290 push_submenu_start ()
291 {
292 if (menu_items_used + 1 > menu_items_allocated)
293 grow_menu_items ();
294
295 XVECTOR (menu_items)->contents[menu_items_used++] = Qnil;
296 menu_items_submenu_depth++;
297 }
298
299 /* End a submenu. */
300
301 static void
302 push_submenu_end ()
303 {
304 if (menu_items_used + 1 > menu_items_allocated)
305 grow_menu_items ();
306
307 XVECTOR (menu_items)->contents[menu_items_used++] = Qlambda;
308 menu_items_submenu_depth--;
309 }
310
311 /* Indicate boundary between left and right. */
312
313 static void
314 push_left_right_boundary ()
315 {
316 if (menu_items_used + 1 > menu_items_allocated)
317 grow_menu_items ();
318
319 XVECTOR (menu_items)->contents[menu_items_used++] = Qquote;
320 }
321
322 /* Start a new menu pane in menu_items..
323 NAME is the pane name. PREFIX_VEC is a prefix key for this pane. */
324
325 static void
326 push_menu_pane (name, prefix_vec)
327 Lisp_Object name, prefix_vec;
328 {
329 if (menu_items_used + MENU_ITEMS_PANE_LENGTH > menu_items_allocated)
330 grow_menu_items ();
331
332 if (menu_items_submenu_depth == 0)
333 menu_items_n_panes++;
334 XVECTOR (menu_items)->contents[menu_items_used++] = Qt;
335 XVECTOR (menu_items)->contents[menu_items_used++] = name;
336 XVECTOR (menu_items)->contents[menu_items_used++] = prefix_vec;
337 }
338
339 /* Push one menu item into the current pane. NAME is the string to
340 display. ENABLE if non-nil means this item can be selected. KEY
341 is the key generated by choosing this item, or nil if this item
342 doesn't really have a definition. DEF is the definition of this
343 item. EQUIV is the textual description of the keyboard equivalent
344 for this item (or nil if none). TYPE is the type of this menu
345 item, one of nil, `toggle' or `radio'. */
346
347 static void
348 push_menu_item (name, enable, key, def, equiv, type, selected, help)
349 Lisp_Object name, enable, key, def, equiv, type, selected, help;
350 {
351 if (menu_items_used + MENU_ITEMS_ITEM_LENGTH > menu_items_allocated)
352 grow_menu_items ();
353
354 XVECTOR (menu_items)->contents[menu_items_used++] = name;
355 XVECTOR (menu_items)->contents[menu_items_used++] = enable;
356 XVECTOR (menu_items)->contents[menu_items_used++] = key;
357 XVECTOR (menu_items)->contents[menu_items_used++] = equiv;
358 XVECTOR (menu_items)->contents[menu_items_used++] = def;
359 XVECTOR (menu_items)->contents[menu_items_used++] = type;
360 XVECTOR (menu_items)->contents[menu_items_used++] = selected;
361 XVECTOR (menu_items)->contents[menu_items_used++] = help;
362 }
363 \f
364 /* Look through KEYMAPS, a vector of keymaps that is NMAPS long,
365 and generate menu panes for them in menu_items.
366 If NOTREAL is nonzero,
367 don't bother really computing whether an item is enabled. */
368
369 static void
370 keymap_panes (keymaps, nmaps, notreal)
371 Lisp_Object *keymaps;
372 int nmaps;
373 int notreal;
374 {
375 int mapno;
376
377 init_menu_items ();
378
379 /* Loop over the given keymaps, making a pane for each map.
380 But don't make a pane that is empty--ignore that map instead.
381 P is the number of panes we have made so far. */
382 for (mapno = 0; mapno < nmaps; mapno++)
383 single_keymap_panes (keymaps[mapno],
384 map_prompt (keymaps[mapno]), Qnil, notreal, 10);
385
386 finish_menu_items ();
387 }
388
389 /* This is a recursive subroutine of keymap_panes.
390 It handles one keymap, KEYMAP.
391 The other arguments are passed along
392 or point to local variables of the previous function.
393 If NOTREAL is nonzero, only check for equivalent key bindings, don't
394 evaluate expressions in menu items and don't make any menu.
395
396 If we encounter submenus deeper than MAXDEPTH levels, ignore them. */
397
398 static void
399 single_keymap_panes (keymap, pane_name, prefix, notreal, maxdepth)
400 Lisp_Object keymap;
401 Lisp_Object pane_name;
402 Lisp_Object prefix;
403 int notreal;
404 int maxdepth;
405 {
406 Lisp_Object pending_maps = Qnil;
407 Lisp_Object tail, item;
408 struct gcpro gcpro1, gcpro2;
409 int notbuttons = 0;
410
411 if (maxdepth <= 0)
412 return;
413
414 push_menu_pane (pane_name, prefix);
415
416 #ifndef HAVE_BOXES
417 /* Remember index for first item in this pane so we can go back and
418 add a prefix when (if) we see the first button. After that, notbuttons
419 is set to 0, to mark that we have seen a button and all non button
420 items need a prefix. */
421 notbuttons = menu_items_used;
422 #endif
423
424 for (tail = keymap; CONSP (tail); tail = XCDR (tail))
425 {
426 GCPRO2 (keymap, pending_maps);
427 /* Look at each key binding, and if it is a menu item add it
428 to this menu. */
429 item = XCAR (tail);
430 if (CONSP (item))
431 single_menu_item (XCAR (item), XCDR (item),
432 &pending_maps, notreal, maxdepth, &notbuttons);
433 else if (VECTORP (item))
434 {
435 /* Loop over the char values represented in the vector. */
436 int len = XVECTOR (item)->size;
437 int c;
438 for (c = 0; c < len; c++)
439 {
440 Lisp_Object character;
441 XSETFASTINT (character, c);
442 single_menu_item (character, XVECTOR (item)->contents[c],
443 &pending_maps, notreal, maxdepth, &notbuttons);
444 }
445 }
446 UNGCPRO;
447 }
448
449 /* Process now any submenus which want to be panes at this level. */
450 while (!NILP (pending_maps))
451 {
452 Lisp_Object elt, eltcdr, string;
453 elt = Fcar (pending_maps);
454 eltcdr = XCDR (elt);
455 string = XCAR (eltcdr);
456 /* We no longer discard the @ from the beginning of the string here.
457 Instead, we do this in xmenu_show. */
458 single_keymap_panes (Fcar (elt), string,
459 XCDR (eltcdr), notreal, maxdepth - 1);
460 pending_maps = Fcdr (pending_maps);
461 }
462 }
463 \f
464 /* This is a subroutine of single_keymap_panes that handles one
465 keymap entry.
466 KEY is a key in a keymap and ITEM is its binding.
467 PENDING_MAPS_PTR points to a list of keymaps waiting to be made into
468 separate panes.
469 If NOTREAL is nonzero, only check for equivalent key bindings, don't
470 evaluate expressions in menu items and don't make any menu.
471 If we encounter submenus deeper than MAXDEPTH levels, ignore them.
472 NOTBUTTONS_PTR is only used when simulating toggle boxes and radio
473 buttons. It points to variable notbuttons in single_keymap_panes,
474 which keeps track of if we have seen a button in this menu or not. */
475
476 static void
477 single_menu_item (key, item, pending_maps_ptr, notreal, maxdepth,
478 notbuttons_ptr)
479 Lisp_Object key, item;
480 Lisp_Object *pending_maps_ptr;
481 int maxdepth, notreal;
482 int *notbuttons_ptr;
483 {
484 Lisp_Object map, item_string, enabled;
485 struct gcpro gcpro1, gcpro2;
486 int res;
487
488 /* Parse the menu item and leave the result in item_properties. */
489 GCPRO2 (key, item);
490 res = parse_menu_item (item, notreal, 0);
491 UNGCPRO;
492 if (!res)
493 return; /* Not a menu item. */
494
495 map = XVECTOR (item_properties)->contents[ITEM_PROPERTY_MAP];
496
497 if (notreal)
498 {
499 /* We don't want to make a menu, just traverse the keymaps to
500 precompute equivalent key bindings. */
501 if (!NILP (map))
502 single_keymap_panes (map, Qnil, key, 1, maxdepth - 1);
503 return;
504 }
505
506 enabled = XVECTOR (item_properties)->contents[ITEM_PROPERTY_ENABLE];
507 item_string = XVECTOR (item_properties)->contents[ITEM_PROPERTY_NAME];
508
509 if (!NILP (map) && XSTRING (item_string)->data[0] == '@')
510 {
511 if (!NILP (enabled))
512 /* An enabled separate pane. Remember this to handle it later. */
513 *pending_maps_ptr = Fcons (Fcons (map, Fcons (item_string, key)),
514 *pending_maps_ptr);
515 return;
516 }
517
518 #ifndef HAVE_BOXES
519 /* Simulate radio buttons and toggle boxes by putting a prefix in
520 front of them. */
521 {
522 Lisp_Object prefix = Qnil;
523 Lisp_Object type = XVECTOR (item_properties)->contents[ITEM_PROPERTY_TYPE];
524 if (!NILP (type))
525 {
526 Lisp_Object selected
527 = XVECTOR (item_properties)->contents[ITEM_PROPERTY_SELECTED];
528
529 if (*notbuttons_ptr)
530 /* The first button. Line up previous items in this menu. */
531 {
532 int index = *notbuttons_ptr; /* Index for first item this menu. */
533 int submenu = 0;
534 Lisp_Object tem;
535 while (index < menu_items_used)
536 {
537 tem
538 = XVECTOR (menu_items)->contents[index + MENU_ITEMS_ITEM_NAME];
539 if (NILP (tem))
540 {
541 index++;
542 submenu++; /* Skip sub menu. */
543 }
544 else if (EQ (tem, Qlambda))
545 {
546 index++;
547 submenu--; /* End sub menu. */
548 }
549 else if (EQ (tem, Qt))
550 index += 3; /* Skip new pane marker. */
551 else if (EQ (tem, Qquote))
552 index++; /* Skip a left, right divider. */
553 else
554 {
555 if (!submenu && XSTRING (tem)->data[0] != '\0'
556 && XSTRING (tem)->data[0] != '-')
557 XVECTOR (menu_items)->contents[index + MENU_ITEMS_ITEM_NAME]
558 = concat2 (build_string (" "), tem);
559 index += MENU_ITEMS_ITEM_LENGTH;
560 }
561 }
562 *notbuttons_ptr = 0;
563 }
564
565 /* Calculate prefix, if any, for this item. */
566 if (EQ (type, QCtoggle))
567 prefix = build_string (NILP (selected) ? "[ ] " : "[X] ");
568 else if (EQ (type, QCradio))
569 prefix = build_string (NILP (selected) ? "( ) " : "(*) ");
570 }
571 /* Not a button. If we have earlier buttons, then we need a prefix. */
572 else if (!*notbuttons_ptr && XSTRING (item_string)->data[0] != '\0'
573 && XSTRING (item_string)->data[0] != '-')
574 prefix = build_string (" ");
575
576 if (!NILP (prefix))
577 item_string = concat2 (prefix, item_string);
578 }
579 #endif /* not HAVE_BOXES */
580
581 #ifndef USE_X_TOOLKIT
582 if (!NILP(map))
583 /* Indicate visually that this is a submenu. */
584 item_string = concat2 (item_string, build_string (" >"));
585 #endif
586
587 push_menu_item (item_string, enabled, key,
588 XVECTOR (item_properties)->contents[ITEM_PROPERTY_DEF],
589 XVECTOR (item_properties)->contents[ITEM_PROPERTY_KEYEQ],
590 XVECTOR (item_properties)->contents[ITEM_PROPERTY_TYPE],
591 XVECTOR (item_properties)->contents[ITEM_PROPERTY_SELECTED],
592 XVECTOR (item_properties)->contents[ITEM_PROPERTY_HELP]);
593
594 #ifdef USE_X_TOOLKIT
595 /* Display a submenu using the toolkit. */
596 if (! (NILP (map) || NILP (enabled)))
597 {
598 push_submenu_start ();
599 single_keymap_panes (map, Qnil, key, 0, maxdepth - 1);
600 push_submenu_end ();
601 }
602 #endif
603 }
604 \f
605 /* Push all the panes and items of a menu described by the
606 alist-of-alists MENU.
607 This handles old-fashioned calls to x-popup-menu. */
608
609 static void
610 list_of_panes (menu)
611 Lisp_Object menu;
612 {
613 Lisp_Object tail;
614
615 init_menu_items ();
616
617 for (tail = menu; !NILP (tail); tail = Fcdr (tail))
618 {
619 Lisp_Object elt, pane_name, pane_data;
620 elt = Fcar (tail);
621 pane_name = Fcar (elt);
622 CHECK_STRING (pane_name, 0);
623 push_menu_pane (pane_name, Qnil);
624 pane_data = Fcdr (elt);
625 CHECK_CONS (pane_data, 0);
626 list_of_items (pane_data);
627 }
628
629 finish_menu_items ();
630 }
631
632 /* Push the items in a single pane defined by the alist PANE. */
633
634 static void
635 list_of_items (pane)
636 Lisp_Object pane;
637 {
638 Lisp_Object tail, item, item1;
639
640 for (tail = pane; !NILP (tail); tail = Fcdr (tail))
641 {
642 item = Fcar (tail);
643 if (STRINGP (item))
644 push_menu_item (item, Qnil, Qnil, Qt, Qnil, Qnil, Qnil, Qnil);
645 else if (NILP (item))
646 push_left_right_boundary ();
647 else
648 {
649 CHECK_CONS (item, 0);
650 item1 = Fcar (item);
651 CHECK_STRING (item1, 1);
652 push_menu_item (item1, Qt, Fcdr (item), Qt, Qnil, Qnil, Qnil, Qnil);
653 }
654 }
655 }
656 \f
657 DEFUN ("x-popup-menu", Fx_popup_menu, Sx_popup_menu, 2, 2, 0,
658 "Pop up a deck-of-cards menu and return user's selection.\n\
659 POSITION is a position specification. This is either a mouse button event\n\
660 or a list ((XOFFSET YOFFSET) WINDOW)\n\
661 where XOFFSET and YOFFSET are positions in pixels from the top left\n\
662 corner of WINDOW's frame. (WINDOW may be a frame object instead of a window.)\n\
663 This controls the position of the center of the first line\n\
664 in the first pane of the menu, not the top left of the menu as a whole.\n\
665 If POSITION is t, it means to use the current mouse position.\n\
666 \n\
667 MENU is a specifier for a menu. For the simplest case, MENU is a keymap.\n\
668 The menu items come from key bindings that have a menu string as well as\n\
669 a definition; actually, the \"definition\" in such a key binding looks like\n\
670 \(STRING . REAL-DEFINITION). To give the menu a title, put a string into\n\
671 the keymap as a top-level element.\n\n\
672 If REAL-DEFINITION is nil, that puts a nonselectable string in the menu.\n\
673 Otherwise, REAL-DEFINITION should be a valid key binding definition.\n\
674 \n\
675 You can also use a list of keymaps as MENU.\n\
676 Then each keymap makes a separate pane.\n\
677 When MENU is a keymap or a list of keymaps, the return value\n\
678 is a list of events.\n\n\
679 \n\
680 Alternatively, you can specify a menu of multiple panes\n\
681 with a list of the form (TITLE PANE1 PANE2...),\n\
682 where each pane is a list of form (TITLE ITEM1 ITEM2...).\n\
683 Each ITEM is normally a cons cell (STRING . VALUE);\n\
684 but a string can appear as an item--that makes a nonselectable line\n\
685 in the menu.\n\
686 With this form of menu, the return value is VALUE from the chosen item.\n\
687 \n\
688 If POSITION is nil, don't display the menu at all, just precalculate the\n\
689 cached information about equivalent key sequences.")
690 (position, menu)
691 Lisp_Object position, menu;
692 {
693 Lisp_Object keymap, tem;
694 int xpos, ypos;
695 Lisp_Object title;
696 char *error_name;
697 Lisp_Object selection;
698 FRAME_PTR f;
699 Lisp_Object x, y, window;
700 int keymaps = 0;
701 int for_click = 0;
702 struct gcpro gcpro1;
703
704 #ifdef HAVE_MENUS
705 if (! NILP (position))
706 {
707 check_x ();
708
709 /* Decode the first argument: find the window and the coordinates. */
710 if (EQ (position, Qt)
711 || (CONSP (position) && EQ (XCAR (position), Qmenu_bar)))
712 {
713 /* Use the mouse's current position. */
714 FRAME_PTR new_f = SELECTED_FRAME ();
715 Lisp_Object bar_window;
716 enum scroll_bar_part part;
717 unsigned long time;
718
719 if (mouse_position_hook)
720 (*mouse_position_hook) (&new_f, 1, &bar_window,
721 &part, &x, &y, &time);
722 if (new_f != 0)
723 XSETFRAME (window, new_f);
724 else
725 {
726 window = selected_window;
727 XSETFASTINT (x, 0);
728 XSETFASTINT (y, 0);
729 }
730 }
731 else
732 {
733 tem = Fcar (position);
734 if (CONSP (tem))
735 {
736 window = Fcar (Fcdr (position));
737 x = Fcar (tem);
738 y = Fcar (Fcdr (tem));
739 }
740 else
741 {
742 for_click = 1;
743 tem = Fcar (Fcdr (position)); /* EVENT_START (position) */
744 window = Fcar (tem); /* POSN_WINDOW (tem) */
745 tem = Fcar (Fcdr (Fcdr (tem))); /* POSN_WINDOW_POSN (tem) */
746 x = Fcar (tem);
747 y = Fcdr (tem);
748 }
749 }
750
751 CHECK_NUMBER (x, 0);
752 CHECK_NUMBER (y, 0);
753
754 /* Decode where to put the menu. */
755
756 if (FRAMEP (window))
757 {
758 f = XFRAME (window);
759 xpos = 0;
760 ypos = 0;
761 }
762 else if (WINDOWP (window))
763 {
764 CHECK_LIVE_WINDOW (window, 0);
765 f = XFRAME (WINDOW_FRAME (XWINDOW (window)));
766
767 xpos = (FONT_WIDTH (f->output_data.x->font)
768 * XFASTINT (XWINDOW (window)->left));
769 ypos = (f->output_data.x->line_height
770 * XFASTINT (XWINDOW (window)->top));
771 }
772 else
773 /* ??? Not really clean; should be CHECK_WINDOW_OR_FRAME,
774 but I don't want to make one now. */
775 CHECK_WINDOW (window, 0);
776
777 xpos += XINT (x);
778 ypos += XINT (y);
779
780 XSETFRAME (Vmenu_updating_frame, f);
781 }
782 Vmenu_updating_frame = Qnil;
783 #endif /* HAVE_MENUS */
784
785 title = Qnil;
786 GCPRO1 (title);
787
788 /* Decode the menu items from what was specified. */
789
790 keymap = Fkeymapp (menu);
791 tem = Qnil;
792 if (CONSP (menu))
793 tem = Fkeymapp (Fcar (menu));
794 if (!NILP (keymap))
795 {
796 /* We were given a keymap. Extract menu info from the keymap. */
797 Lisp_Object prompt;
798 keymap = get_keymap (menu);
799
800 /* Extract the detailed info to make one pane. */
801 keymap_panes (&menu, 1, NILP (position));
802
803 /* Search for a string appearing directly as an element of the keymap.
804 That string is the title of the menu. */
805 prompt = map_prompt (keymap);
806 if (NILP (title) && !NILP (prompt))
807 title = prompt;
808
809 /* Make that be the pane title of the first pane. */
810 if (!NILP (prompt) && menu_items_n_panes >= 0)
811 XVECTOR (menu_items)->contents[MENU_ITEMS_PANE_NAME] = prompt;
812
813 keymaps = 1;
814 }
815 else if (!NILP (tem))
816 {
817 /* We were given a list of keymaps. */
818 int nmaps = XFASTINT (Flength (menu));
819 Lisp_Object *maps
820 = (Lisp_Object *) alloca (nmaps * sizeof (Lisp_Object));
821 int i;
822
823 title = Qnil;
824
825 /* The first keymap that has a prompt string
826 supplies the menu title. */
827 for (tem = menu, i = 0; CONSP (tem); tem = Fcdr (tem))
828 {
829 Lisp_Object prompt;
830
831 maps[i++] = keymap = get_keymap (Fcar (tem));
832
833 prompt = map_prompt (keymap);
834 if (NILP (title) && !NILP (prompt))
835 title = prompt;
836 }
837
838 /* Extract the detailed info to make one pane. */
839 keymap_panes (maps, nmaps, NILP (position));
840
841 /* Make the title be the pane title of the first pane. */
842 if (!NILP (title) && menu_items_n_panes >= 0)
843 XVECTOR (menu_items)->contents[MENU_ITEMS_PANE_NAME] = title;
844
845 keymaps = 1;
846 }
847 else
848 {
849 /* We were given an old-fashioned menu. */
850 title = Fcar (menu);
851 CHECK_STRING (title, 1);
852
853 list_of_panes (Fcdr (menu));
854
855 keymaps = 0;
856 }
857
858 if (NILP (position))
859 {
860 discard_menu_items ();
861 UNGCPRO;
862 return Qnil;
863 }
864
865 #ifdef HAVE_MENUS
866 /* Display them in a menu. */
867 BLOCK_INPUT;
868
869 selection = xmenu_show (f, xpos, ypos, for_click,
870 keymaps, title, &error_name);
871 UNBLOCK_INPUT;
872
873 discard_menu_items ();
874
875 UNGCPRO;
876 #endif /* HAVE_MENUS */
877
878 if (error_name) error (error_name);
879 return selection;
880 }
881
882 #ifdef HAVE_MENUS
883
884 DEFUN ("x-popup-dialog", Fx_popup_dialog, Sx_popup_dialog, 2, 2, 0,
885 "Pop up a dialog box and return user's selection.\n\
886 POSITION specifies which frame to use.\n\
887 This is normally a mouse button event or a window or frame.\n\
888 If POSITION is t, it means to use the frame the mouse is on.\n\
889 The dialog box appears in the middle of the specified frame.\n\
890 \n\
891 CONTENTS specifies the alternatives to display in the dialog box.\n\
892 It is a list of the form (TITLE ITEM1 ITEM2...).\n\
893 Each ITEM is a cons cell (STRING . VALUE).\n\
894 The return value is VALUE from the chosen item.\n\n\
895 An ITEM may also be just a string--that makes a nonselectable item.\n\
896 An ITEM may also be nil--that means to put all preceding items\n\
897 on the left of the dialog box and all following items on the right.\n\
898 \(By default, approximately half appear on each side.)")
899 (position, contents)
900 Lisp_Object position, contents;
901 {
902 FRAME_PTR f;
903 Lisp_Object window;
904
905 check_x ();
906
907 /* Decode the first argument: find the window or frame to use. */
908 if (EQ (position, Qt)
909 || (CONSP (position) && EQ (XCAR (position), Qmenu_bar)))
910 {
911 #if 0 /* Using the frame the mouse is on may not be right. */
912 /* Use the mouse's current position. */
913 FRAME_PTR new_f = SELECTED_FRAME ();
914 Lisp_Object bar_window;
915 int part;
916 unsigned long time;
917 Lisp_Object x, y;
918
919 (*mouse_position_hook) (&new_f, 1, &bar_window, &part, &x, &y, &time);
920
921 if (new_f != 0)
922 XSETFRAME (window, new_f);
923 else
924 window = selected_window;
925 #endif
926 window = selected_window;
927 }
928 else if (CONSP (position))
929 {
930 Lisp_Object tem;
931 tem = Fcar (position);
932 if (CONSP (tem))
933 window = Fcar (Fcdr (position));
934 else
935 {
936 tem = Fcar (Fcdr (position)); /* EVENT_START (position) */
937 window = Fcar (tem); /* POSN_WINDOW (tem) */
938 }
939 }
940 else if (WINDOWP (position) || FRAMEP (position))
941 window = position;
942 else
943 window = Qnil;
944
945 /* Decode where to put the menu. */
946
947 if (FRAMEP (window))
948 f = XFRAME (window);
949 else if (WINDOWP (window))
950 {
951 CHECK_LIVE_WINDOW (window, 0);
952 f = XFRAME (WINDOW_FRAME (XWINDOW (window)));
953 }
954 else
955 /* ??? Not really clean; should be CHECK_WINDOW_OR_FRAME,
956 but I don't want to make one now. */
957 CHECK_WINDOW (window, 0);
958
959 #ifndef USE_X_TOOLKIT
960 /* Display a menu with these alternatives
961 in the middle of frame F. */
962 {
963 Lisp_Object x, y, frame, newpos;
964 XSETFRAME (frame, f);
965 XSETINT (x, x_pixel_width (f) / 2);
966 XSETINT (y, x_pixel_height (f) / 2);
967 newpos = Fcons (Fcons (x, Fcons (y, Qnil)), Fcons (frame, Qnil));
968
969 return Fx_popup_menu (newpos,
970 Fcons (Fcar (contents), Fcons (contents, Qnil)));
971 }
972 #else
973 {
974 Lisp_Object title;
975 char *error_name;
976 Lisp_Object selection;
977
978 /* Decode the dialog items from what was specified. */
979 title = Fcar (contents);
980 CHECK_STRING (title, 1);
981
982 list_of_panes (Fcons (contents, Qnil));
983
984 /* Display them in a dialog box. */
985 BLOCK_INPUT;
986 selection = xdialog_show (f, 0, title, &error_name);
987 UNBLOCK_INPUT;
988
989 discard_menu_items ();
990
991 if (error_name) error (error_name);
992 return selection;
993 }
994 #endif
995 }
996 \f
997 #ifdef USE_X_TOOLKIT
998
999 /* Loop in Xt until the menu pulldown or dialog popup has been
1000 popped down (deactivated). This is used for x-popup-menu
1001 and x-popup-dialog; it is not used for the menu bar any more.
1002
1003 NOTE: All calls to popup_get_selection should be protected
1004 with BLOCK_INPUT, UNBLOCK_INPUT wrappers. */
1005
1006 void
1007 popup_get_selection (initial_event, dpyinfo, id)
1008 XEvent *initial_event;
1009 struct x_display_info *dpyinfo;
1010 LWLIB_ID id;
1011 {
1012 XEvent event;
1013
1014 /* Define a queue to save up for later unreading
1015 all X events that don't pertain to the menu. */
1016 struct event_queue
1017 {
1018 XEvent event;
1019 struct event_queue *next;
1020 };
1021
1022 struct event_queue *queue = NULL;
1023 struct event_queue *queue_tmp;
1024
1025 if (initial_event)
1026 event = *initial_event;
1027 else
1028 XtAppNextEvent (Xt_app_con, &event);
1029
1030 while (1)
1031 {
1032 /* Handle expose events for editor frames right away. */
1033 if (event.type == Expose)
1034 process_expose_from_menu (event);
1035 /* Make sure we don't consider buttons grabbed after menu goes.
1036 And make sure to deactivate for any ButtonRelease,
1037 even if XtDispatchEvent doesn't do that. */
1038 else if (event.type == ButtonRelease
1039 && dpyinfo->display == event.xbutton.display)
1040 {
1041 dpyinfo->grabbed &= ~(1 << event.xbutton.button);
1042 popup_activated_flag = 0;
1043 #ifdef USE_MOTIF /* Pretending that the event came from a
1044 Btn1Down seems the only way to convince Motif to
1045 activate its callbacks; setting the XmNmenuPost
1046 isn't working. --marcus@sysc.pdx.edu. */
1047 event.xbutton.button = 1;
1048 #endif
1049 }
1050 /* If the user presses a key, deactivate the menu.
1051 The user is likely to do that if we get wedged. */
1052 else if (event.type == KeyPress
1053 && dpyinfo->display == event.xbutton.display)
1054 {
1055 KeySym keysym = XLookupKeysym (&event.xkey, 0);
1056 if (!IsModifierKey (keysym))
1057 {
1058 popup_activated_flag = 0;
1059 break;
1060 }
1061 }
1062 /* Button presses outside the menu also pop it down. */
1063 else if (event.type == ButtonPress
1064 && event.xany.display == dpyinfo->display
1065 && x_any_window_to_frame (dpyinfo, event.xany.window))
1066 {
1067 popup_activated_flag = 0;
1068 break;
1069 }
1070
1071 /* Queue all events not for this popup,
1072 except for Expose, which we've already handled, and ButtonRelease.
1073 Note that the X window is associated with the frame if this
1074 is a menu bar popup, but not if it's a dialog box. So we use
1075 x_non_menubar_window_to_frame, not x_any_window_to_frame. */
1076 if (event.type != Expose
1077 && !(event.type == ButtonRelease
1078 && dpyinfo->display == event.xbutton.display)
1079 && (event.xany.display != dpyinfo->display
1080 || x_non_menubar_window_to_frame (dpyinfo, event.xany.window)))
1081 {
1082 queue_tmp = (struct event_queue *) malloc (sizeof (struct event_queue));
1083
1084 if (queue_tmp != NULL)
1085 {
1086 queue_tmp->event = event;
1087 queue_tmp->next = queue;
1088 queue = queue_tmp;
1089 }
1090 }
1091 else
1092 XtDispatchEvent (&event);
1093
1094 if (!popup_activated ())
1095 break;
1096 XtAppNextEvent (Xt_app_con, &event);
1097 }
1098
1099 /* Unread any events that we got but did not handle. */
1100 while (queue != NULL)
1101 {
1102 queue_tmp = queue;
1103 XPutBackEvent (queue_tmp->event.xany.display, &queue_tmp->event);
1104 queue = queue_tmp->next;
1105 free ((char *)queue_tmp);
1106 /* Cause these events to get read as soon as we UNBLOCK_INPUT. */
1107 interrupt_input_pending = 1;
1108 }
1109 }
1110
1111 /* Activate the menu bar of frame F.
1112 This is called from keyboard.c when it gets the
1113 menu_bar_activate_event out of the Emacs event queue.
1114
1115 To activate the menu bar, we use the X button-press event
1116 that was saved in saved_menu_event.
1117 That makes the toolkit do its thing.
1118
1119 But first we recompute the menu bar contents (the whole tree).
1120
1121 The reason for saving the button event until here, instead of
1122 passing it to the toolkit right away, is that we can safely
1123 execute Lisp code. */
1124
1125 void
1126 x_activate_menubar (f)
1127 FRAME_PTR f;
1128 {
1129 if (!f->output_data.x->saved_menu_event->type)
1130 return;
1131
1132 set_frame_menubar (f, 0, 1);
1133 BLOCK_INPUT;
1134 XtDispatchEvent ((XEvent *) f->output_data.x->saved_menu_event);
1135 UNBLOCK_INPUT;
1136 #ifdef USE_MOTIF
1137 if (f->output_data.x->saved_menu_event->type == ButtonRelease)
1138 pending_menu_activation = 1;
1139 #endif
1140
1141 /* Ignore this if we get it a second time. */
1142 f->output_data.x->saved_menu_event->type = 0;
1143 }
1144
1145 /* Detect if a dialog or menu has been posted. */
1146
1147 int
1148 popup_activated ()
1149 {
1150 return popup_activated_flag;
1151 }
1152
1153 /* This callback is invoked when the user selects a menubar cascade
1154 pushbutton, but before the pulldown menu is posted. */
1155
1156 static void
1157 popup_activate_callback (widget, id, client_data)
1158 Widget widget;
1159 LWLIB_ID id;
1160 XtPointer client_data;
1161 {
1162 #ifdef USE_MOTIF
1163 ++popup_activated_flag;
1164 #else
1165 popup_activated_flag = 1;
1166 #endif
1167 }
1168
1169 /* This callback is invoked when a dialog or menu is finished being
1170 used and has been unposted. */
1171
1172 static void
1173 popup_deactivate_callback (widget, id, client_data)
1174 Widget widget;
1175 LWLIB_ID id;
1176 XtPointer client_data;
1177 {
1178 #ifdef USE_MOTIF
1179 --popup_activated_flag;
1180 #else
1181 popup_activated_flag = 0;
1182 #endif
1183 }
1184
1185 /* Lwlib callback called when menu items are highlighted/unhighlighted
1186 while moving the mouse over them. WIDGET is the menu bar or menu
1187 popup widget. ID is its LWLIB_ID. CALL_DATA contains a pointer to
1188 the widget_value structure for the menu item, or null in case of
1189 unhighlighting. */
1190
1191 void
1192 menu_highlight_callback (widget, id, call_data)
1193 Widget widget;
1194 LWLIB_ID id;
1195 void *call_data;
1196 {
1197 widget_value *wv = (widget_value *) call_data;
1198 struct frame *f;
1199 Lisp_Object frame, help;
1200
1201 help = wv && wv->help ? build_string (wv->help) : Qnil;
1202
1203 /* Determine the frame for the help event. */
1204 f = menubar_id_to_frame (id);
1205 if (f)
1206 {
1207 XSETFRAME (frame, f);
1208 kbd_buffer_store_help_event (frame, help);
1209 }
1210 else
1211 {
1212 /* WIDGET is the popup menu. It's parent is the frame's
1213 widget. See which frame that is. */
1214 Widget frame_widget = XtParent (widget);
1215 Lisp_Object tail;
1216
1217 for (tail = Vframe_list; GC_CONSP (tail); tail = XCDR (tail))
1218 {
1219 frame = XCAR (tail);
1220 if (GC_FRAMEP (frame)
1221 && (f = XFRAME (frame),
1222 FRAME_X_P (f) && f->output_data.x->widget == frame_widget))
1223 break;
1224 }
1225
1226 show_help_echo (help, Qnil, Qnil, Qnil, 1);
1227 }
1228 }
1229
1230 /* This callback is called from the menu bar pulldown menu
1231 when the user makes a selection.
1232 Figure out what the user chose
1233 and put the appropriate events into the keyboard buffer. */
1234
1235 static void
1236 menubar_selection_callback (widget, id, client_data)
1237 Widget widget;
1238 LWLIB_ID id;
1239 XtPointer client_data;
1240 {
1241 Lisp_Object prefix, entry;
1242 FRAME_PTR f = menubar_id_to_frame (id);
1243 Lisp_Object vector;
1244 Lisp_Object *subprefix_stack;
1245 int submenu_depth = 0;
1246 int i;
1247
1248 if (!f)
1249 return;
1250 subprefix_stack = (Lisp_Object *) alloca (f->menu_bar_items_used * sizeof (Lisp_Object));
1251 vector = f->menu_bar_vector;
1252 prefix = Qnil;
1253 i = 0;
1254 while (i < f->menu_bar_items_used)
1255 {
1256 if (EQ (XVECTOR (vector)->contents[i], Qnil))
1257 {
1258 subprefix_stack[submenu_depth++] = prefix;
1259 prefix = entry;
1260 i++;
1261 }
1262 else if (EQ (XVECTOR (vector)->contents[i], Qlambda))
1263 {
1264 prefix = subprefix_stack[--submenu_depth];
1265 i++;
1266 }
1267 else if (EQ (XVECTOR (vector)->contents[i], Qt))
1268 {
1269 prefix = XVECTOR (vector)->contents[i + MENU_ITEMS_PANE_PREFIX];
1270 i += MENU_ITEMS_PANE_LENGTH;
1271 }
1272 else
1273 {
1274 entry = XVECTOR (vector)->contents[i + MENU_ITEMS_ITEM_VALUE];
1275 /* The EMACS_INT cast avoids a warning. There's no problem
1276 as long as pointers have enough bits to hold small integers. */
1277 if ((int) (EMACS_INT) client_data == i)
1278 {
1279 int j;
1280 struct input_event buf;
1281 Lisp_Object frame;
1282
1283 XSETFRAME (frame, f);
1284 buf.kind = MENU_BAR_EVENT;
1285 buf.frame_or_window = frame;
1286 buf.arg = frame;
1287 kbd_buffer_store_event (&buf);
1288
1289 for (j = 0; j < submenu_depth; j++)
1290 if (!NILP (subprefix_stack[j]))
1291 {
1292 buf.kind = MENU_BAR_EVENT;
1293 buf.frame_or_window = frame;
1294 buf.arg = subprefix_stack[j];
1295 kbd_buffer_store_event (&buf);
1296 }
1297
1298 if (!NILP (prefix))
1299 {
1300 buf.kind = MENU_BAR_EVENT;
1301 buf.frame_or_window = frame;
1302 buf.arg = prefix;
1303 kbd_buffer_store_event (&buf);
1304 }
1305
1306 buf.kind = MENU_BAR_EVENT;
1307 buf.frame_or_window = frame;
1308 buf.arg = entry;
1309 kbd_buffer_store_event (&buf);
1310
1311 return;
1312 }
1313 i += MENU_ITEMS_ITEM_LENGTH;
1314 }
1315 }
1316 }
1317
1318 /* Allocate a widget_value, blocking input. */
1319
1320 widget_value *
1321 xmalloc_widget_value ()
1322 {
1323 widget_value *value;
1324
1325 BLOCK_INPUT;
1326 value = malloc_widget_value ();
1327 UNBLOCK_INPUT;
1328
1329 return value;
1330 }
1331
1332 /* This recursively calls free_widget_value on the tree of widgets.
1333 It must free all data that was malloc'ed for these widget_values.
1334 In Emacs, many slots are pointers into the data of Lisp_Strings, and
1335 must be left alone. */
1336
1337 void
1338 free_menubar_widget_value_tree (wv)
1339 widget_value *wv;
1340 {
1341 if (! wv) return;
1342
1343 wv->name = wv->value = wv->key = (char *) 0xDEADBEEF;
1344
1345 if (wv->contents && (wv->contents != (widget_value*)1))
1346 {
1347 free_menubar_widget_value_tree (wv->contents);
1348 wv->contents = (widget_value *) 0xDEADBEEF;
1349 }
1350 if (wv->next)
1351 {
1352 free_menubar_widget_value_tree (wv->next);
1353 wv->next = (widget_value *) 0xDEADBEEF;
1354 }
1355 BLOCK_INPUT;
1356 free_widget_value (wv);
1357 UNBLOCK_INPUT;
1358 }
1359 \f
1360 /* Return a tree of widget_value structures for a menu bar item
1361 whose event type is ITEM_KEY (with string ITEM_NAME)
1362 and whose contents come from the list of keymaps MAPS. */
1363
1364 static widget_value *
1365 single_submenu (item_key, item_name, maps)
1366 Lisp_Object item_key, item_name, maps;
1367 {
1368 widget_value *wv, *prev_wv, *save_wv, *first_wv;
1369 int i;
1370 int submenu_depth = 0;
1371 Lisp_Object length;
1372 int len;
1373 Lisp_Object *mapvec;
1374 widget_value **submenu_stack;
1375 int previous_items = menu_items_used;
1376 int top_level_items = 0;
1377
1378 length = Flength (maps);
1379 len = XINT (length);
1380
1381 /* Convert the list MAPS into a vector MAPVEC. */
1382 mapvec = (Lisp_Object *) alloca (len * sizeof (Lisp_Object));
1383 for (i = 0; i < len; i++)
1384 {
1385 mapvec[i] = Fcar (maps);
1386 maps = Fcdr (maps);
1387 }
1388
1389 menu_items_n_panes = 0;
1390
1391 /* Loop over the given keymaps, making a pane for each map.
1392 But don't make a pane that is empty--ignore that map instead. */
1393 for (i = 0; i < len; i++)
1394 {
1395 if (SYMBOLP (mapvec[i])
1396 || (CONSP (mapvec[i])
1397 && NILP (Fkeymapp (mapvec[i]))))
1398 {
1399 /* Here we have a command at top level in the menu bar
1400 as opposed to a submenu. */
1401 top_level_items = 1;
1402 push_menu_pane (Qnil, Qnil);
1403 push_menu_item (item_name, Qt, item_key, mapvec[i],
1404 Qnil, Qnil, Qnil, Qnil);
1405 }
1406 else
1407 single_keymap_panes (mapvec[i], item_name, item_key, 0, 10);
1408 }
1409
1410 /* Create a tree of widget_value objects
1411 representing the panes and their items. */
1412
1413 submenu_stack
1414 = (widget_value **) alloca (menu_items_used * sizeof (widget_value *));
1415 wv = xmalloc_widget_value ();
1416 wv->name = "menu";
1417 wv->value = 0;
1418 wv->enabled = 1;
1419 wv->button_type = BUTTON_TYPE_NONE;
1420 first_wv = wv;
1421 save_wv = 0;
1422 prev_wv = 0;
1423
1424 /* Loop over all panes and items made during this call
1425 and construct a tree of widget_value objects.
1426 Ignore the panes and items made by previous calls to
1427 single_submenu, even though those are also in menu_items. */
1428 i = previous_items;
1429 while (i < menu_items_used)
1430 {
1431 if (EQ (XVECTOR (menu_items)->contents[i], Qnil))
1432 {
1433 submenu_stack[submenu_depth++] = save_wv;
1434 save_wv = prev_wv;
1435 prev_wv = 0;
1436 i++;
1437 }
1438 else if (EQ (XVECTOR (menu_items)->contents[i], Qlambda))
1439 {
1440 prev_wv = save_wv;
1441 save_wv = submenu_stack[--submenu_depth];
1442 i++;
1443 }
1444 else if (EQ (XVECTOR (menu_items)->contents[i], Qt)
1445 && submenu_depth != 0)
1446 i += MENU_ITEMS_PANE_LENGTH;
1447 /* Ignore a nil in the item list.
1448 It's meaningful only for dialog boxes. */
1449 else if (EQ (XVECTOR (menu_items)->contents[i], Qquote))
1450 i += 1;
1451 else if (EQ (XVECTOR (menu_items)->contents[i], Qt))
1452 {
1453 /* Create a new pane. */
1454 Lisp_Object pane_name, prefix;
1455 char *pane_string;
1456 pane_name = XVECTOR (menu_items)->contents[i + MENU_ITEMS_PANE_NAME];
1457 prefix = XVECTOR (menu_items)->contents[i + MENU_ITEMS_PANE_PREFIX];
1458 #ifndef HAVE_MULTILINGUAL_MENU
1459 if (STRINGP (pane_name) && STRING_MULTIBYTE (pane_name))
1460 pane_name = string_make_unibyte (pane_name);
1461 #endif
1462 pane_string = (NILP (pane_name)
1463 ? "" : (char *) XSTRING (pane_name)->data);
1464 /* If there is just one top-level pane, put all its items directly
1465 under the top-level menu. */
1466 if (menu_items_n_panes == 1)
1467 pane_string = "";
1468
1469 /* If the pane has a meaningful name,
1470 make the pane a top-level menu item
1471 with its items as a submenu beneath it. */
1472 if (strcmp (pane_string, ""))
1473 {
1474 wv = xmalloc_widget_value ();
1475 if (save_wv)
1476 save_wv->next = wv;
1477 else
1478 first_wv->contents = wv;
1479 wv->name = pane_string;
1480 /* Ignore the @ that means "separate pane".
1481 This is a kludge, but this isn't worth more time. */
1482 if (!NILP (prefix) && wv->name[0] == '@')
1483 wv->name++;
1484 wv->value = 0;
1485 wv->enabled = 1;
1486 wv->button_type = BUTTON_TYPE_NONE;
1487 }
1488 save_wv = wv;
1489 prev_wv = 0;
1490 i += MENU_ITEMS_PANE_LENGTH;
1491 }
1492 else
1493 {
1494 /* Create a new item within current pane. */
1495 Lisp_Object item_name, enable, descrip, def, type, selected;
1496 Lisp_Object help;
1497
1498 item_name = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_NAME];
1499 enable = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_ENABLE];
1500 descrip
1501 = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_EQUIV_KEY];
1502 def = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_DEFINITION];
1503 type = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_TYPE];
1504 selected = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_SELECTED];
1505 help = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_HELP];
1506
1507 #ifndef HAVE_MULTILINGUAL_MENU
1508 if (STRING_MULTIBYTE (item_name))
1509 item_name = string_make_unibyte (item_name);
1510 if (STRINGP (descrip) && STRING_MULTIBYTE (descrip))
1511 descrip = string_make_unibyte (descrip);
1512 #endif
1513
1514 wv = xmalloc_widget_value ();
1515 if (prev_wv)
1516 prev_wv->next = wv;
1517 else
1518 save_wv->contents = wv;
1519
1520 wv->name = (char *) XSTRING (item_name)->data;
1521 if (!NILP (descrip))
1522 wv->key = (char *) XSTRING (descrip)->data;
1523 wv->value = 0;
1524 /* The EMACS_INT cast avoids a warning. There's no problem
1525 as long as pointers have enough bits to hold small integers. */
1526 wv->call_data = (!NILP (def) ? (void *) (EMACS_INT) i : 0);
1527 wv->enabled = !NILP (enable);
1528
1529 if (NILP (type))
1530 wv->button_type = BUTTON_TYPE_NONE;
1531 else if (EQ (type, QCradio))
1532 wv->button_type = BUTTON_TYPE_RADIO;
1533 else if (EQ (type, QCtoggle))
1534 wv->button_type = BUTTON_TYPE_TOGGLE;
1535 else
1536 abort ();
1537
1538 wv->selected = !NILP (selected);
1539 if (STRINGP (help))
1540 wv->help = XSTRING (help)->data;
1541
1542 prev_wv = wv;
1543
1544 i += MENU_ITEMS_ITEM_LENGTH;
1545 }
1546 }
1547
1548 /* If we have just one "menu item"
1549 that was originally a button, return it by itself. */
1550 if (top_level_items && first_wv->contents && first_wv->contents->next == 0)
1551 {
1552 wv = first_wv->contents;
1553 free_widget_value (first_wv);
1554 return wv;
1555 }
1556
1557 return first_wv;
1558 }
1559 \f
1560 extern void EmacsFrameSetCharSize ();
1561
1562 /* Recompute all the widgets of frame F, when the menu bar
1563 has been changed. */
1564
1565 static void
1566 update_frame_menubar (f)
1567 FRAME_PTR f;
1568 {
1569 struct x_output *x = f->output_data.x;
1570 int columns, rows;
1571 int menubar_changed;
1572
1573 /* We assume the menubar contents has changed if the global flag is set,
1574 or if the current buffer has changed, or if the menubar has never
1575 been updated before.
1576 */
1577 menubar_changed = (x->menubar_widget
1578 && !XtIsManaged (x->menubar_widget));
1579
1580 if (! (menubar_changed))
1581 return;
1582
1583 BLOCK_INPUT;
1584 /* Save the size of the frame because the pane widget doesn't accept to
1585 resize itself. So force it. */
1586 columns = f->width;
1587 rows = f->height;
1588
1589 /* Do the voodoo which means "I'm changing lots of things, don't try to
1590 refigure sizes until I'm done." */
1591 lw_refigure_widget (x->column_widget, False);
1592
1593 /* the order in which children are managed is the top to
1594 bottom order in which they are displayed in the paned window.
1595 First, remove the text-area widget.
1596 */
1597 XtUnmanageChild (x->edit_widget);
1598
1599 /* remove the menubar that is there now, and put up the menubar that
1600 should be there.
1601 */
1602 if (menubar_changed)
1603 {
1604 XtManageChild (x->menubar_widget);
1605 XtMapWidget (x->menubar_widget);
1606 XtVaSetValues (x->menubar_widget, XtNmappedWhenManaged, 1, 0);
1607 }
1608
1609 /* Re-manage the text-area widget, and then thrash the sizes. */
1610 XtManageChild (x->edit_widget);
1611 x_set_menu_resources_from_menu_face (f, x->menubar_widget);
1612 lw_refigure_widget (x->column_widget, True);
1613
1614 /* Force the pane widget to resize itself with the right values. */
1615 EmacsFrameSetCharSize (x->edit_widget, columns, rows);
1616 UNBLOCK_INPUT;
1617 }
1618
1619 /* Set the contents of the menubar widgets of frame F.
1620 The argument FIRST_TIME is currently ignored;
1621 it is set the first time this is called, from initialize_frame_menubar. */
1622
1623 void
1624 set_frame_menubar (f, first_time, deep_p)
1625 FRAME_PTR f;
1626 int first_time;
1627 int deep_p;
1628 {
1629 Widget menubar_widget = f->output_data.x->menubar_widget;
1630 Lisp_Object items;
1631 widget_value *wv, *first_wv, *prev_wv = 0;
1632 int i;
1633 LWLIB_ID id;
1634
1635 XSETFRAME (Vmenu_updating_frame, f);
1636
1637 if (f->output_data.x->id == 0)
1638 f->output_data.x->id = next_menubar_widget_id++;
1639 id = f->output_data.x->id;
1640
1641 if (! menubar_widget)
1642 deep_p = 1;
1643 else if (pending_menu_activation && !deep_p)
1644 deep_p = 1;
1645 /* Make the first call for any given frame always go deep. */
1646 else if (!f->output_data.x->saved_menu_event && !deep_p)
1647 {
1648 deep_p = 1;
1649 f->output_data.x->saved_menu_event = (XEvent*)xmalloc (sizeof (XEvent));
1650 f->output_data.x->saved_menu_event->type = 0;
1651 }
1652
1653 wv = xmalloc_widget_value ();
1654 wv->name = "menubar";
1655 wv->value = 0;
1656 wv->enabled = 1;
1657 wv->button_type = BUTTON_TYPE_NONE;
1658 first_wv = wv;
1659
1660 if (deep_p)
1661 {
1662 /* Make a widget-value tree representing the entire menu trees. */
1663
1664 struct buffer *prev = current_buffer;
1665 Lisp_Object buffer;
1666 int specpdl_count = specpdl_ptr - specpdl;
1667 int previous_menu_items_used = f->menu_bar_items_used;
1668 Lisp_Object *previous_items
1669 = (Lisp_Object *) alloca (previous_menu_items_used
1670 * sizeof (Lisp_Object));
1671
1672 /* If we are making a new widget, its contents are empty,
1673 do always reinitialize them. */
1674 if (! menubar_widget)
1675 previous_menu_items_used = 0;
1676
1677 buffer = XWINDOW (FRAME_SELECTED_WINDOW (f))->buffer;
1678 specbind (Qinhibit_quit, Qt);
1679 /* Don't let the debugger step into this code
1680 because it is not reentrant. */
1681 specbind (Qdebug_on_next_call, Qnil);
1682
1683 record_unwind_protect (Fset_match_data, Fmatch_data (Qnil, Qnil));
1684 if (NILP (Voverriding_local_map_menu_flag))
1685 {
1686 specbind (Qoverriding_terminal_local_map, Qnil);
1687 specbind (Qoverriding_local_map, Qnil);
1688 }
1689
1690 set_buffer_internal_1 (XBUFFER (buffer));
1691
1692 /* Run the Lucid hook. */
1693 call1 (Vrun_hooks, Qactivate_menubar_hook);
1694 /* If it has changed current-menubar from previous value,
1695 really recompute the menubar from the value. */
1696 if (! NILP (Vlucid_menu_bar_dirty_flag))
1697 call0 (Qrecompute_lucid_menubar);
1698 safe_run_hooks (Qmenu_bar_update_hook);
1699 FRAME_MENU_BAR_ITEMS (f) = menu_bar_items (FRAME_MENU_BAR_ITEMS (f));
1700
1701 items = FRAME_MENU_BAR_ITEMS (f);
1702
1703 inhibit_garbage_collection ();
1704
1705 /* Save the frame's previous menu bar contents data. */
1706 bcopy (XVECTOR (f->menu_bar_vector)->contents, previous_items,
1707 previous_menu_items_used * sizeof (Lisp_Object));
1708
1709 /* Fill in the current menu bar contents. */
1710 menu_items = f->menu_bar_vector;
1711 menu_items_allocated = XVECTOR (menu_items)->size;
1712 init_menu_items ();
1713 for (i = 0; i < XVECTOR (items)->size; i += 4)
1714 {
1715 Lisp_Object key, string, maps;
1716
1717 key = XVECTOR (items)->contents[i];
1718 string = XVECTOR (items)->contents[i + 1];
1719 maps = XVECTOR (items)->contents[i + 2];
1720 if (NILP (string))
1721 break;
1722
1723 wv = single_submenu (key, string, maps);
1724 if (prev_wv)
1725 prev_wv->next = wv;
1726 else
1727 first_wv->contents = wv;
1728 /* Don't set wv->name here; GC during the loop might relocate it. */
1729 wv->enabled = 1;
1730 wv->button_type = BUTTON_TYPE_NONE;
1731 prev_wv = wv;
1732 }
1733
1734 finish_menu_items ();
1735
1736 set_buffer_internal_1 (prev);
1737 unbind_to (specpdl_count, Qnil);
1738
1739 /* If there has been no change in the Lisp-level contents
1740 of the menu bar, skip redisplaying it. Just exit. */
1741
1742 for (i = 0; i < previous_menu_items_used; i++)
1743 if (menu_items_used == i
1744 || (!EQ (previous_items[i], XVECTOR (menu_items)->contents[i])))
1745 break;
1746 if (i == menu_items_used && i == previous_menu_items_used && i != 0)
1747 {
1748 free_menubar_widget_value_tree (first_wv);
1749 menu_items = Qnil;
1750
1751 return;
1752 }
1753
1754 /* Now GC cannot happen during the lifetime of the widget_value,
1755 so it's safe to store data from a Lisp_String. */
1756 wv = first_wv->contents;
1757 for (i = 0; i < XVECTOR (items)->size; i += 4)
1758 {
1759 Lisp_Object string;
1760 string = XVECTOR (items)->contents[i + 1];
1761 if (NILP (string))
1762 break;
1763 wv->name = (char *) XSTRING (string)->data;
1764 wv = wv->next;
1765 }
1766
1767 f->menu_bar_vector = menu_items;
1768 f->menu_bar_items_used = menu_items_used;
1769 menu_items = Qnil;
1770 }
1771 else
1772 {
1773 /* Make a widget-value tree containing
1774 just the top level menu bar strings. */
1775
1776 items = FRAME_MENU_BAR_ITEMS (f);
1777 for (i = 0; i < XVECTOR (items)->size; i += 4)
1778 {
1779 Lisp_Object string;
1780
1781 string = XVECTOR (items)->contents[i + 1];
1782 if (NILP (string))
1783 break;
1784
1785 wv = xmalloc_widget_value ();
1786 wv->name = (char *) XSTRING (string)->data;
1787 wv->value = 0;
1788 wv->enabled = 1;
1789 wv->button_type = BUTTON_TYPE_NONE;
1790 /* This prevents lwlib from assuming this
1791 menu item is really supposed to be empty. */
1792 /* The EMACS_INT cast avoids a warning.
1793 This value just has to be different from small integers. */
1794 wv->call_data = (void *) (EMACS_INT) (-1);
1795
1796 if (prev_wv)
1797 prev_wv->next = wv;
1798 else
1799 first_wv->contents = wv;
1800 prev_wv = wv;
1801 }
1802
1803 /* Forget what we thought we knew about what is in the
1804 detailed contents of the menu bar menus.
1805 Changing the top level always destroys the contents. */
1806 f->menu_bar_items_used = 0;
1807 }
1808
1809 /* Create or update the menu bar widget. */
1810
1811 BLOCK_INPUT;
1812
1813 if (menubar_widget)
1814 {
1815 /* Disable resizing (done for Motif!) */
1816 lw_allow_resizing (f->output_data.x->widget, False);
1817
1818 /* The third arg is DEEP_P, which says to consider the entire
1819 menu trees we supply, rather than just the menu bar item names. */
1820 lw_modify_all_widgets (id, first_wv, deep_p);
1821
1822 /* Re-enable the edit widget to resize. */
1823 lw_allow_resizing (f->output_data.x->widget, True);
1824 }
1825 else
1826 {
1827 menubar_widget = lw_create_widget ("menubar", "menubar", id, first_wv,
1828 f->output_data.x->column_widget,
1829 0,
1830 popup_activate_callback,
1831 menubar_selection_callback,
1832 popup_deactivate_callback,
1833 menu_highlight_callback);
1834 f->output_data.x->menubar_widget = menubar_widget;
1835 }
1836
1837 {
1838 int menubar_size
1839 = (f->output_data.x->menubar_widget
1840 ? (f->output_data.x->menubar_widget->core.height
1841 + f->output_data.x->menubar_widget->core.border_width)
1842 : 0);
1843
1844 #if 0 /* Experimentally, we now get the right results
1845 for -geometry -0-0 without this. 24 Aug 96, rms. */
1846 #ifdef USE_LUCID
1847 if (FRAME_EXTERNAL_MENU_BAR (f))
1848 {
1849 Dimension ibw = 0;
1850 XtVaGetValues (f->output_data.x->column_widget,
1851 XtNinternalBorderWidth, &ibw, NULL);
1852 menubar_size += ibw;
1853 }
1854 #endif /* USE_LUCID */
1855 #endif /* 0 */
1856
1857 f->output_data.x->menubar_height = menubar_size;
1858 }
1859
1860 free_menubar_widget_value_tree (first_wv);
1861 update_frame_menubar (f);
1862
1863 UNBLOCK_INPUT;
1864 }
1865
1866 /* Called from Fx_create_frame to create the initial menubar of a frame
1867 before it is mapped, so that the window is mapped with the menubar already
1868 there instead of us tacking it on later and thrashing the window after it
1869 is visible. */
1870
1871 void
1872 initialize_frame_menubar (f)
1873 FRAME_PTR f;
1874 {
1875 /* This function is called before the first chance to redisplay
1876 the frame. It has to be, so the frame will have the right size. */
1877 FRAME_MENU_BAR_ITEMS (f) = menu_bar_items (FRAME_MENU_BAR_ITEMS (f));
1878 set_frame_menubar (f, 1, 1);
1879 }
1880
1881 /* Get rid of the menu bar of frame F, and free its storage.
1882 This is used when deleting a frame, and when turning off the menu bar. */
1883
1884 void
1885 free_frame_menubar (f)
1886 FRAME_PTR f;
1887 {
1888 Widget menubar_widget;
1889
1890 menubar_widget = f->output_data.x->menubar_widget;
1891
1892 f->output_data.x->menubar_height = 0;
1893
1894 if (menubar_widget)
1895 {
1896 BLOCK_INPUT;
1897 lw_destroy_all_widgets ((LWLIB_ID) f->output_data.x->id);
1898 UNBLOCK_INPUT;
1899 }
1900 }
1901
1902 #endif /* USE_X_TOOLKIT */
1903 \f
1904 /* xmenu_show actually displays a menu using the panes and items in menu_items
1905 and returns the value selected from it.
1906 There are two versions of xmenu_show, one for Xt and one for Xlib.
1907 Both assume input is blocked by the caller. */
1908
1909 /* F is the frame the menu is for.
1910 X and Y are the frame-relative specified position,
1911 relative to the inside upper left corner of the frame F.
1912 FOR_CLICK is nonzero if this menu was invoked for a mouse click.
1913 KEYMAPS is 1 if this menu was specified with keymaps;
1914 in that case, we return a list containing the chosen item's value
1915 and perhaps also the pane's prefix.
1916 TITLE is the specified menu title.
1917 ERROR is a place to store an error message string in case of failure.
1918 (We return nil on failure, but the value doesn't actually matter.) */
1919
1920 #ifdef USE_X_TOOLKIT
1921
1922 /* We need a unique id for each widget handled by the Lucid Widget
1923 library.
1924
1925 For the main windows, and popup menus, we use this counter,
1926 which we increment each time after use. This starts from 1<<16.
1927
1928 For menu bars, we use numbers starting at 0, counted in
1929 next_menubar_widget_id. */
1930 LWLIB_ID widget_id_tick;
1931
1932 static Lisp_Object *volatile menu_item_selection;
1933
1934 static void
1935 popup_selection_callback (widget, id, client_data)
1936 Widget widget;
1937 LWLIB_ID id;
1938 XtPointer client_data;
1939 {
1940 menu_item_selection = (Lisp_Object *) client_data;
1941 }
1942
1943 static Lisp_Object
1944 xmenu_show (f, x, y, for_click, keymaps, title, error)
1945 FRAME_PTR f;
1946 int x;
1947 int y;
1948 int for_click;
1949 int keymaps;
1950 Lisp_Object title;
1951 char **error;
1952 {
1953 int i;
1954 LWLIB_ID menu_id;
1955 Widget menu;
1956 Arg av[2];
1957 int ac = 0;
1958 widget_value *wv, *save_wv = 0, *first_wv = 0, *prev_wv = 0;
1959 widget_value **submenu_stack
1960 = (widget_value **) alloca (menu_items_used * sizeof (widget_value *));
1961 Lisp_Object *subprefix_stack
1962 = (Lisp_Object *) alloca (menu_items_used * sizeof (Lisp_Object));
1963 int submenu_depth = 0;
1964 XButtonPressedEvent dummy;
1965
1966 int first_pane;
1967
1968 *error = NULL;
1969
1970 if (menu_items_used <= MENU_ITEMS_PANE_LENGTH)
1971 {
1972 *error = "Empty menu";
1973 return Qnil;
1974 }
1975
1976 /* Create a tree of widget_value objects
1977 representing the panes and their items. */
1978 wv = xmalloc_widget_value ();
1979 wv->name = "menu";
1980 wv->value = 0;
1981 wv->enabled = 1;
1982 wv->button_type = BUTTON_TYPE_NONE;
1983 first_wv = wv;
1984 first_pane = 1;
1985
1986 /* Loop over all panes and items, filling in the tree. */
1987 i = 0;
1988 while (i < menu_items_used)
1989 {
1990 if (EQ (XVECTOR (menu_items)->contents[i], Qnil))
1991 {
1992 submenu_stack[submenu_depth++] = save_wv;
1993 save_wv = prev_wv;
1994 prev_wv = 0;
1995 first_pane = 1;
1996 i++;
1997 }
1998 else if (EQ (XVECTOR (menu_items)->contents[i], Qlambda))
1999 {
2000 prev_wv = save_wv;
2001 save_wv = submenu_stack[--submenu_depth];
2002 first_pane = 0;
2003 i++;
2004 }
2005 else if (EQ (XVECTOR (menu_items)->contents[i], Qt)
2006 && submenu_depth != 0)
2007 i += MENU_ITEMS_PANE_LENGTH;
2008 /* Ignore a nil in the item list.
2009 It's meaningful only for dialog boxes. */
2010 else if (EQ (XVECTOR (menu_items)->contents[i], Qquote))
2011 i += 1;
2012 else if (EQ (XVECTOR (menu_items)->contents[i], Qt))
2013 {
2014 /* Create a new pane. */
2015 Lisp_Object pane_name, prefix;
2016 char *pane_string;
2017 pane_name = XVECTOR (menu_items)->contents[i + MENU_ITEMS_PANE_NAME];
2018 prefix = XVECTOR (menu_items)->contents[i + MENU_ITEMS_PANE_PREFIX];
2019 #ifndef HAVE_MULTILINGUAL_MENU
2020 if (!NILP (pane_name) && STRING_MULTIBYTE (pane_name))
2021 pane_name = string_make_unibyte (pane_name);
2022 #endif
2023 pane_string = (NILP (pane_name)
2024 ? "" : (char *) XSTRING (pane_name)->data);
2025 /* If there is just one top-level pane, put all its items directly
2026 under the top-level menu. */
2027 if (menu_items_n_panes == 1)
2028 pane_string = "";
2029
2030 /* If the pane has a meaningful name,
2031 make the pane a top-level menu item
2032 with its items as a submenu beneath it. */
2033 if (!keymaps && strcmp (pane_string, ""))
2034 {
2035 wv = xmalloc_widget_value ();
2036 if (save_wv)
2037 save_wv->next = wv;
2038 else
2039 first_wv->contents = wv;
2040 wv->name = pane_string;
2041 if (keymaps && !NILP (prefix))
2042 wv->name++;
2043 wv->value = 0;
2044 wv->enabled = 1;
2045 wv->button_type = BUTTON_TYPE_NONE;
2046 save_wv = wv;
2047 prev_wv = 0;
2048 }
2049 else if (first_pane)
2050 {
2051 save_wv = wv;
2052 prev_wv = 0;
2053 }
2054 first_pane = 0;
2055 i += MENU_ITEMS_PANE_LENGTH;
2056 }
2057 else
2058 {
2059 /* Create a new item within current pane. */
2060 Lisp_Object item_name, enable, descrip, def, type, selected, help;
2061 item_name = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_NAME];
2062 enable = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_ENABLE];
2063 descrip
2064 = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_EQUIV_KEY];
2065 def = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_DEFINITION];
2066 type = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_TYPE];
2067 selected = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_SELECTED];
2068 help = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_HELP];
2069
2070 #ifndef HAVE_MULTILINGUAL_MENU
2071 if (STRINGP (item_name) && STRING_MULTIBYTE (item_name))
2072 item_name = string_make_unibyte (item_name);
2073 if (STRINGP (descrip) && STRING_MULTIBYTE (descrip))
2074 item_name = string_make_unibyte (descrip);
2075 #endif
2076
2077 wv = xmalloc_widget_value ();
2078 if (prev_wv)
2079 prev_wv->next = wv;
2080 else
2081 save_wv->contents = wv;
2082 wv->name = (char *) XSTRING (item_name)->data;
2083 if (!NILP (descrip))
2084 wv->key = (char *) XSTRING (descrip)->data;
2085 wv->value = 0;
2086 /* If this item has a null value,
2087 make the call_data null so that it won't display a box
2088 when the mouse is on it. */
2089 wv->call_data
2090 = (!NILP (def) ? (void *) &XVECTOR (menu_items)->contents[i] : 0);
2091 wv->enabled = !NILP (enable);
2092
2093 if (NILP (type))
2094 wv->button_type = BUTTON_TYPE_NONE;
2095 else if (EQ (type, QCtoggle))
2096 wv->button_type = BUTTON_TYPE_TOGGLE;
2097 else if (EQ (type, QCradio))
2098 wv->button_type = BUTTON_TYPE_RADIO;
2099 else
2100 abort ();
2101
2102 wv->selected = !NILP (selected);
2103 if (STRINGP (help))
2104 wv->help = XSTRING (help)->data;
2105
2106 prev_wv = wv;
2107
2108 i += MENU_ITEMS_ITEM_LENGTH;
2109 }
2110 }
2111
2112 /* Deal with the title, if it is non-nil. */
2113 if (!NILP (title))
2114 {
2115 widget_value *wv_title = xmalloc_widget_value ();
2116 widget_value *wv_sep1 = xmalloc_widget_value ();
2117 widget_value *wv_sep2 = xmalloc_widget_value ();
2118
2119 wv_sep2->name = "--";
2120 wv_sep2->next = first_wv->contents;
2121
2122 wv_sep1->name = "--";
2123 wv_sep1->next = wv_sep2;
2124
2125 #ifndef HAVE_MULTILINGUAL_MENU
2126 if (STRING_MULTIBYTE (title))
2127 title = string_make_unibyte (title);
2128 #endif
2129 wv_title->name = (char *) XSTRING (title)->data;
2130 wv_title->enabled = True;
2131 wv_title->button_type = BUTTON_TYPE_NONE;
2132 wv_title->next = wv_sep1;
2133 first_wv->contents = wv_title;
2134 }
2135
2136 /* Actually create the menu. */
2137 menu_id = widget_id_tick++;
2138 menu = lw_create_widget ("popup", first_wv->name, menu_id, first_wv,
2139 f->output_data.x->widget, 1, 0,
2140 popup_selection_callback,
2141 popup_deactivate_callback,
2142 menu_highlight_callback);
2143
2144 /* Adjust coordinates to relative to the outer (window manager) window. */
2145 {
2146 Window child;
2147 int win_x = 0, win_y = 0;
2148
2149 /* Find the position of the outside upper-left corner of
2150 the inner window, with respect to the outer window. */
2151 if (f->output_data.x->parent_desc != FRAME_X_DISPLAY_INFO (f)->root_window)
2152 {
2153 BLOCK_INPUT;
2154 XTranslateCoordinates (FRAME_X_DISPLAY (f),
2155
2156 /* From-window, to-window. */
2157 f->output_data.x->window_desc,
2158 f->output_data.x->parent_desc,
2159
2160 /* From-position, to-position. */
2161 0, 0, &win_x, &win_y,
2162
2163 /* Child of window. */
2164 &child);
2165 UNBLOCK_INPUT;
2166 x += win_x;
2167 y += win_y;
2168 }
2169 }
2170
2171 /* Adjust coordinates to be root-window-relative. */
2172 x += f->output_data.x->left_pos;
2173 y += f->output_data.x->top_pos;
2174
2175 dummy.type = ButtonPress;
2176 dummy.serial = 0;
2177 dummy.send_event = 0;
2178 dummy.display = FRAME_X_DISPLAY (f);
2179 dummy.time = CurrentTime;
2180 dummy.root = FRAME_X_DISPLAY_INFO (f)->root_window;
2181 dummy.window = dummy.root;
2182 dummy.subwindow = dummy.root;
2183 dummy.x_root = x;
2184 dummy.y_root = y;
2185 dummy.x = x;
2186 dummy.y = y;
2187 dummy.state = (FRAME_X_DISPLAY_INFO (f)->grabbed >> 1) * Button1Mask;
2188 dummy.button = 0;
2189 for (i = 0; i < 5; i++)
2190 if (FRAME_X_DISPLAY_INFO (f)->grabbed & (1 << i))
2191 dummy.button = i;
2192
2193 /* Don't allow any geometry request from the user. */
2194 XtSetArg (av[ac], XtNgeometry, 0); ac++;
2195 XtSetValues (menu, av, ac);
2196
2197 /* Free the widget_value objects we used to specify the contents. */
2198 free_menubar_widget_value_tree (first_wv);
2199
2200 /* No selection has been chosen yet. */
2201 menu_item_selection = 0;
2202
2203 /* Display the menu. */
2204 lw_popup_menu (menu, (XEvent *) &dummy);
2205 x_set_menu_resources_from_menu_face (f, menu);
2206 popup_activated_flag = 1;
2207
2208 /* Process events that apply to the menu. */
2209 popup_get_selection ((XEvent *) 0, FRAME_X_DISPLAY_INFO (f), menu_id);
2210
2211 #ifdef LESSTIF_VERSION
2212 /* Nov 1998: For an unknown reason a button grab remains active
2213 after the popup menu has gone. */
2214 XUngrabButton (XtDisplay (f->output_data.x->widget),
2215 AnyButton, AnyModifier,
2216 XtWindow (f->output_data.x->widget));
2217 XUngrabButton (XtDisplay (f->output_data.x->edit_widget),
2218 AnyButton, AnyModifier,
2219 XtWindow (f->output_data.x->edit_widget));
2220 #endif /* LESSTIF_VERSION */
2221
2222 /* fp turned off the following statement and wrote a comment
2223 that it is unnecessary--that the menu has already disappeared.
2224 Nowadays the menu disappears ok, all right, but
2225 we need to delete the widgets or multiple ones will pile up. */
2226 lw_destroy_all_widgets (menu_id);
2227
2228 /* Find the selected item, and its pane, to return
2229 the proper value. */
2230 if (menu_item_selection != 0)
2231 {
2232 Lisp_Object prefix, entry;
2233
2234 prefix = Qnil;
2235 i = 0;
2236 while (i < menu_items_used)
2237 {
2238 if (EQ (XVECTOR (menu_items)->contents[i], Qnil))
2239 {
2240 subprefix_stack[submenu_depth++] = prefix;
2241 prefix = entry;
2242 i++;
2243 }
2244 else if (EQ (XVECTOR (menu_items)->contents[i], Qlambda))
2245 {
2246 prefix = subprefix_stack[--submenu_depth];
2247 i++;
2248 }
2249 else if (EQ (XVECTOR (menu_items)->contents[i], Qt))
2250 {
2251 prefix
2252 = XVECTOR (menu_items)->contents[i + MENU_ITEMS_PANE_PREFIX];
2253 i += MENU_ITEMS_PANE_LENGTH;
2254 }
2255 /* Ignore a nil in the item list.
2256 It's meaningful only for dialog boxes. */
2257 else if (EQ (XVECTOR (menu_items)->contents[i], Qquote))
2258 i += 1;
2259 else
2260 {
2261 entry
2262 = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_VALUE];
2263 if (menu_item_selection == &XVECTOR (menu_items)->contents[i])
2264 {
2265 if (keymaps != 0)
2266 {
2267 int j;
2268
2269 entry = Fcons (entry, Qnil);
2270 if (!NILP (prefix))
2271 entry = Fcons (prefix, entry);
2272 for (j = submenu_depth - 1; j >= 0; j--)
2273 if (!NILP (subprefix_stack[j]))
2274 entry = Fcons (subprefix_stack[j], entry);
2275 }
2276 return entry;
2277 }
2278 i += MENU_ITEMS_ITEM_LENGTH;
2279 }
2280 }
2281 }
2282
2283 return Qnil;
2284 }
2285 \f
2286 static void
2287 dialog_selection_callback (widget, id, client_data)
2288 Widget widget;
2289 LWLIB_ID id;
2290 XtPointer client_data;
2291 {
2292 /* The EMACS_INT cast avoids a warning. There's no problem
2293 as long as pointers have enough bits to hold small integers. */
2294 if ((int) (EMACS_INT) client_data != -1)
2295 menu_item_selection = (Lisp_Object *) client_data;
2296 BLOCK_INPUT;
2297 lw_destroy_all_widgets (id);
2298 UNBLOCK_INPUT;
2299 popup_activated_flag = 0;
2300 }
2301
2302 static char * button_names [] = {
2303 "button1", "button2", "button3", "button4", "button5",
2304 "button6", "button7", "button8", "button9", "button10" };
2305
2306 static Lisp_Object
2307 xdialog_show (f, keymaps, title, error)
2308 FRAME_PTR f;
2309 int keymaps;
2310 Lisp_Object title;
2311 char **error;
2312 {
2313 int i, nb_buttons=0;
2314 LWLIB_ID dialog_id;
2315 Widget menu;
2316 char dialog_name[6];
2317
2318 widget_value *wv, *first_wv = 0, *prev_wv = 0;
2319
2320 /* Number of elements seen so far, before boundary. */
2321 int left_count = 0;
2322 /* 1 means we've seen the boundary between left-hand elts and right-hand. */
2323 int boundary_seen = 0;
2324
2325 *error = NULL;
2326
2327 if (menu_items_n_panes > 1)
2328 {
2329 *error = "Multiple panes in dialog box";
2330 return Qnil;
2331 }
2332
2333 /* Create a tree of widget_value objects
2334 representing the text label and buttons. */
2335 {
2336 Lisp_Object pane_name, prefix;
2337 char *pane_string;
2338 pane_name = XVECTOR (menu_items)->contents[MENU_ITEMS_PANE_NAME];
2339 prefix = XVECTOR (menu_items)->contents[MENU_ITEMS_PANE_PREFIX];
2340 pane_string = (NILP (pane_name)
2341 ? "" : (char *) XSTRING (pane_name)->data);
2342 prev_wv = xmalloc_widget_value ();
2343 prev_wv->value = pane_string;
2344 if (keymaps && !NILP (prefix))
2345 prev_wv->name++;
2346 prev_wv->enabled = 1;
2347 prev_wv->name = "message";
2348 first_wv = prev_wv;
2349
2350 /* Loop over all panes and items, filling in the tree. */
2351 i = MENU_ITEMS_PANE_LENGTH;
2352 while (i < menu_items_used)
2353 {
2354
2355 /* Create a new item within current pane. */
2356 Lisp_Object item_name, enable, descrip;
2357 item_name = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_NAME];
2358 enable = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_ENABLE];
2359 descrip
2360 = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_EQUIV_KEY];
2361
2362 if (NILP (item_name))
2363 {
2364 free_menubar_widget_value_tree (first_wv);
2365 *error = "Submenu in dialog items";
2366 return Qnil;
2367 }
2368 if (EQ (item_name, Qquote))
2369 {
2370 /* This is the boundary between left-side elts
2371 and right-side elts. Stop incrementing right_count. */
2372 boundary_seen = 1;
2373 i++;
2374 continue;
2375 }
2376 if (nb_buttons >= 9)
2377 {
2378 free_menubar_widget_value_tree (first_wv);
2379 *error = "Too many dialog items";
2380 return Qnil;
2381 }
2382
2383 wv = xmalloc_widget_value ();
2384 prev_wv->next = wv;
2385 wv->name = (char *) button_names[nb_buttons];
2386 if (!NILP (descrip))
2387 wv->key = (char *) XSTRING (descrip)->data;
2388 wv->value = (char *) XSTRING (item_name)->data;
2389 wv->call_data = (void *) &XVECTOR (menu_items)->contents[i];
2390 wv->enabled = !NILP (enable);
2391 prev_wv = wv;
2392
2393 if (! boundary_seen)
2394 left_count++;
2395
2396 nb_buttons++;
2397 i += MENU_ITEMS_ITEM_LENGTH;
2398 }
2399
2400 /* If the boundary was not specified,
2401 by default put half on the left and half on the right. */
2402 if (! boundary_seen)
2403 left_count = nb_buttons - nb_buttons / 2;
2404
2405 wv = xmalloc_widget_value ();
2406 wv->name = dialog_name;
2407
2408 /* Dialog boxes use a really stupid name encoding
2409 which specifies how many buttons to use
2410 and how many buttons are on the right.
2411 The Q means something also. */
2412 dialog_name[0] = 'Q';
2413 dialog_name[1] = '0' + nb_buttons;
2414 dialog_name[2] = 'B';
2415 dialog_name[3] = 'R';
2416 /* Number of buttons to put on the right. */
2417 dialog_name[4] = '0' + nb_buttons - left_count;
2418 dialog_name[5] = 0;
2419 wv->contents = first_wv;
2420 first_wv = wv;
2421 }
2422
2423 /* Actually create the dialog. */
2424 dialog_id = widget_id_tick++;
2425 menu = lw_create_widget (first_wv->name, "dialog", dialog_id, first_wv,
2426 f->output_data.x->widget, 1, 0,
2427 dialog_selection_callback, 0, 0);
2428 lw_modify_all_widgets (dialog_id, first_wv->contents, True);
2429 /* Free the widget_value objects we used to specify the contents. */
2430 free_menubar_widget_value_tree (first_wv);
2431
2432 /* No selection has been chosen yet. */
2433 menu_item_selection = 0;
2434
2435 /* Display the menu. */
2436 lw_pop_up_all_widgets (dialog_id);
2437 popup_activated_flag = 1;
2438
2439 /* Process events that apply to the menu. */
2440 popup_get_selection ((XEvent *) 0, FRAME_X_DISPLAY_INFO (f), dialog_id);
2441
2442 lw_destroy_all_widgets (dialog_id);
2443
2444 /* Find the selected item, and its pane, to return
2445 the proper value. */
2446 if (menu_item_selection != 0)
2447 {
2448 Lisp_Object prefix;
2449
2450 prefix = Qnil;
2451 i = 0;
2452 while (i < menu_items_used)
2453 {
2454 Lisp_Object entry;
2455
2456 if (EQ (XVECTOR (menu_items)->contents[i], Qt))
2457 {
2458 prefix
2459 = XVECTOR (menu_items)->contents[i + MENU_ITEMS_PANE_PREFIX];
2460 i += MENU_ITEMS_PANE_LENGTH;
2461 }
2462 else
2463 {
2464 entry
2465 = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_VALUE];
2466 if (menu_item_selection == &XVECTOR (menu_items)->contents[i])
2467 {
2468 if (keymaps != 0)
2469 {
2470 entry = Fcons (entry, Qnil);
2471 if (!NILP (prefix))
2472 entry = Fcons (prefix, entry);
2473 }
2474 return entry;
2475 }
2476 i += MENU_ITEMS_ITEM_LENGTH;
2477 }
2478 }
2479 }
2480
2481 return Qnil;
2482 }
2483
2484 #else /* not USE_X_TOOLKIT */
2485
2486 /* The frame of the last activated non-toolkit menu bar.
2487 Used to generate menu help events. */
2488
2489 static struct frame *menu_help_frame;
2490
2491
2492 /* Show help HELP_STRING, or clear help if HELP_STRING is null.
2493
2494 PANE is the pane number, and ITEM is the menu item number in
2495 the menu (currently not used).
2496
2497 This cannot be done with generating a HELP_EVENT because
2498 XMenuActivate contains a loop that doesn't let Emacs process
2499 keyboard events. */
2500
2501 static void
2502 menu_help_callback (help_string, pane, item)
2503 char *help_string;
2504 int pane, item;
2505 {
2506 extern Lisp_Object Qmenu_item;
2507 Lisp_Object *first_item;
2508 Lisp_Object pane_name;
2509 Lisp_Object menu_object;
2510
2511 first_item = XVECTOR (menu_items)->contents;
2512 if (EQ (first_item[0], Qt))
2513 pane_name = first_item[MENU_ITEMS_PANE_NAME];
2514 else if (EQ (first_item[0], Qquote))
2515 /* This shouldn't happen, see xmenu_show. */
2516 pane_name = build_string ("");
2517 else
2518 pane_name = first_item[MENU_ITEMS_ITEM_NAME];
2519
2520 /* (menu-item MENU-NAME PANE-NUMBER) */
2521 menu_object = Fcons (Qmenu_item,
2522 Fcons (pane_name,
2523 Fcons (make_number (pane), Qnil)));
2524 show_help_echo (help_string ? build_string (help_string) : Qnil,
2525 Qnil, menu_object, make_number (item), 1);
2526 }
2527
2528
2529 static Lisp_Object
2530 xmenu_show (f, x, y, for_click, keymaps, title, error)
2531 FRAME_PTR f;
2532 int x, y;
2533 int for_click;
2534 int keymaps;
2535 Lisp_Object title;
2536 char **error;
2537 {
2538 Window root;
2539 XMenu *menu;
2540 int pane, selidx, lpane, status;
2541 Lisp_Object entry, pane_prefix;
2542 char *datap;
2543 int ulx, uly, width, height;
2544 int dispwidth, dispheight;
2545 int i, j;
2546 int maxwidth;
2547 int dummy_int;
2548 unsigned int dummy_uint;
2549
2550 *error = 0;
2551 if (menu_items_n_panes == 0)
2552 return Qnil;
2553
2554 if (menu_items_used <= MENU_ITEMS_PANE_LENGTH)
2555 {
2556 *error = "Empty menu";
2557 return Qnil;
2558 }
2559
2560 /* Figure out which root window F is on. */
2561 XGetGeometry (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), &root,
2562 &dummy_int, &dummy_int, &dummy_uint, &dummy_uint,
2563 &dummy_uint, &dummy_uint);
2564
2565 /* Make the menu on that window. */
2566 menu = XMenuCreate (FRAME_X_DISPLAY (f), root, "emacs");
2567 if (menu == NULL)
2568 {
2569 *error = "Can't create menu";
2570 return Qnil;
2571 }
2572
2573 #ifdef HAVE_X_WINDOWS
2574 /* Adjust coordinates to relative to the outer (window manager) window. */
2575 {
2576 Window child;
2577 int win_x = 0, win_y = 0;
2578
2579 /* Find the position of the outside upper-left corner of
2580 the inner window, with respect to the outer window. */
2581 if (f->output_data.x->parent_desc != FRAME_X_DISPLAY_INFO (f)->root_window)
2582 {
2583 BLOCK_INPUT;
2584 XTranslateCoordinates (FRAME_X_DISPLAY (f),
2585
2586 /* From-window, to-window. */
2587 f->output_data.x->window_desc,
2588 f->output_data.x->parent_desc,
2589
2590 /* From-position, to-position. */
2591 0, 0, &win_x, &win_y,
2592
2593 /* Child of window. */
2594 &child);
2595 UNBLOCK_INPUT;
2596 x += win_x;
2597 y += win_y;
2598 }
2599 }
2600 #endif /* HAVE_X_WINDOWS */
2601
2602 /* Adjust coordinates to be root-window-relative. */
2603 x += f->output_data.x->left_pos;
2604 y += f->output_data.x->top_pos;
2605
2606 /* Create all the necessary panes and their items. */
2607 i = 0;
2608 while (i < menu_items_used)
2609 {
2610 if (EQ (XVECTOR (menu_items)->contents[i], Qt))
2611 {
2612 /* Create a new pane. */
2613 Lisp_Object pane_name, prefix;
2614 char *pane_string;
2615
2616 pane_name = XVECTOR (menu_items)->contents[i + MENU_ITEMS_PANE_NAME];
2617 prefix = XVECTOR (menu_items)->contents[i + MENU_ITEMS_PANE_PREFIX];
2618 pane_string = (NILP (pane_name)
2619 ? "" : (char *) XSTRING (pane_name)->data);
2620 if (keymaps && !NILP (prefix))
2621 pane_string++;
2622
2623 lpane = XMenuAddPane (FRAME_X_DISPLAY (f), menu, pane_string, TRUE);
2624 if (lpane == XM_FAILURE)
2625 {
2626 XMenuDestroy (FRAME_X_DISPLAY (f), menu);
2627 *error = "Can't create pane";
2628 return Qnil;
2629 }
2630 i += MENU_ITEMS_PANE_LENGTH;
2631
2632 /* Find the width of the widest item in this pane. */
2633 maxwidth = 0;
2634 j = i;
2635 while (j < menu_items_used)
2636 {
2637 Lisp_Object item;
2638 item = XVECTOR (menu_items)->contents[j];
2639 if (EQ (item, Qt))
2640 break;
2641 if (NILP (item))
2642 {
2643 j++;
2644 continue;
2645 }
2646 width = STRING_BYTES (XSTRING (item));
2647 if (width > maxwidth)
2648 maxwidth = width;
2649
2650 j += MENU_ITEMS_ITEM_LENGTH;
2651 }
2652 }
2653 /* Ignore a nil in the item list.
2654 It's meaningful only for dialog boxes. */
2655 else if (EQ (XVECTOR (menu_items)->contents[i], Qquote))
2656 i += 1;
2657 else
2658 {
2659 /* Create a new item within current pane. */
2660 Lisp_Object item_name, enable, descrip, help;
2661 unsigned char *item_data;
2662 char *help_string;
2663
2664 item_name = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_NAME];
2665 enable = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_ENABLE];
2666 descrip
2667 = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_EQUIV_KEY];
2668 help = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_HELP];
2669 help_string = STRINGP (help) ? XSTRING (help)->data : NULL;
2670
2671 if (!NILP (descrip))
2672 {
2673 int gap = maxwidth - STRING_BYTES (XSTRING (item_name));
2674 #ifdef C_ALLOCA
2675 Lisp_Object spacer;
2676 spacer = Fmake_string (make_number (gap), make_number (' '));
2677 item_name = concat2 (item_name, spacer);
2678 item_name = concat2 (item_name, descrip);
2679 item_data = XSTRING (item_name)->data;
2680 #else
2681 /* if alloca is fast, use that to make the space,
2682 to reduce gc needs. */
2683 item_data
2684 = (unsigned char *) alloca (maxwidth
2685 + STRING_BYTES (XSTRING (descrip)) + 1);
2686 bcopy (XSTRING (item_name)->data, item_data,
2687 STRING_BYTES (XSTRING (item_name)));
2688 for (j = XSTRING (item_name)->size; j < maxwidth; j++)
2689 item_data[j] = ' ';
2690 bcopy (XSTRING (descrip)->data, item_data + j,
2691 STRING_BYTES (XSTRING (descrip)));
2692 item_data[j + STRING_BYTES (XSTRING (descrip))] = 0;
2693 #endif
2694 }
2695 else
2696 item_data = XSTRING (item_name)->data;
2697
2698 if (XMenuAddSelection (FRAME_X_DISPLAY (f),
2699 menu, lpane, 0, item_data,
2700 !NILP (enable), help_string)
2701 == XM_FAILURE)
2702 {
2703 XMenuDestroy (FRAME_X_DISPLAY (f), menu);
2704 *error = "Can't add selection to menu";
2705 return Qnil;
2706 }
2707 i += MENU_ITEMS_ITEM_LENGTH;
2708 }
2709 }
2710
2711 /* All set and ready to fly. */
2712 XMenuRecompute (FRAME_X_DISPLAY (f), menu);
2713 dispwidth = DisplayWidth (FRAME_X_DISPLAY (f),
2714 XScreenNumberOfScreen (FRAME_X_SCREEN (f)));
2715 dispheight = DisplayHeight (FRAME_X_DISPLAY (f),
2716 XScreenNumberOfScreen (FRAME_X_SCREEN (f)));
2717 x = min (x, dispwidth);
2718 y = min (y, dispheight);
2719 x = max (x, 1);
2720 y = max (y, 1);
2721 XMenuLocate (FRAME_X_DISPLAY (f), menu, 0, 0, x, y,
2722 &ulx, &uly, &width, &height);
2723 if (ulx+width > dispwidth)
2724 {
2725 x -= (ulx + width) - dispwidth;
2726 ulx = dispwidth - width;
2727 }
2728 if (uly+height > dispheight)
2729 {
2730 y -= (uly + height) - dispheight;
2731 uly = dispheight - height;
2732 }
2733 if (ulx < 0) x -= ulx;
2734 if (uly < 0) y -= uly;
2735
2736 XMenuSetAEQ (menu, TRUE);
2737 XMenuSetFreeze (menu, TRUE);
2738 pane = selidx = 0;
2739
2740 /* Help display under X won't work because XMenuActivate contains
2741 a loop that doesn't give Emacs a chance to process it. */
2742 menu_help_frame = f;
2743 status = XMenuActivate (FRAME_X_DISPLAY (f), menu, &pane, &selidx,
2744 x, y, ButtonReleaseMask, &datap,
2745 menu_help_callback);
2746
2747
2748 #ifdef HAVE_X_WINDOWS
2749 /* Assume the mouse has moved out of the X window.
2750 If it has actually moved in, we will get an EnterNotify. */
2751 x_mouse_leave (FRAME_X_DISPLAY_INFO (f));
2752 #endif
2753
2754 switch (status)
2755 {
2756 case XM_SUCCESS:
2757 #ifdef XDEBUG
2758 fprintf (stderr, "pane= %d line = %d\n", panes, selidx);
2759 #endif
2760
2761 /* Find the item number SELIDX in pane number PANE. */
2762 i = 0;
2763 while (i < menu_items_used)
2764 {
2765 if (EQ (XVECTOR (menu_items)->contents[i], Qt))
2766 {
2767 if (pane == 0)
2768 pane_prefix
2769 = XVECTOR (menu_items)->contents[i + MENU_ITEMS_PANE_PREFIX];
2770 pane--;
2771 i += MENU_ITEMS_PANE_LENGTH;
2772 }
2773 else
2774 {
2775 if (pane == -1)
2776 {
2777 if (selidx == 0)
2778 {
2779 entry
2780 = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_VALUE];
2781 if (keymaps != 0)
2782 {
2783 entry = Fcons (entry, Qnil);
2784 if (!NILP (pane_prefix))
2785 entry = Fcons (pane_prefix, entry);
2786 }
2787 break;
2788 }
2789 selidx--;
2790 }
2791 i += MENU_ITEMS_ITEM_LENGTH;
2792 }
2793 }
2794 break;
2795
2796 case XM_FAILURE:
2797 *error = "Can't activate menu";
2798 case XM_IA_SELECT:
2799 case XM_NO_SELECT:
2800 entry = Qnil;
2801 break;
2802 }
2803 XMenuDestroy (FRAME_X_DISPLAY (f), menu);
2804
2805 #ifdef HAVE_X_WINDOWS
2806 /* State that no mouse buttons are now held.
2807 (The oldXMenu code doesn't track this info for us.)
2808 That is not necessarily true, but the fiction leads to reasonable
2809 results, and it is a pain to ask which are actually held now. */
2810 FRAME_X_DISPLAY_INFO (f)->grabbed = 0;
2811 #endif
2812
2813 return entry;
2814 }
2815
2816 #endif /* not USE_X_TOOLKIT */
2817
2818 #endif /* HAVE_MENUS */
2819 \f
2820 void
2821 syms_of_xmenu ()
2822 {
2823 staticpro (&menu_items);
2824 menu_items = Qnil;
2825
2826 Qdebug_on_next_call = intern ("debug-on-next-call");
2827 staticpro (&Qdebug_on_next_call);
2828
2829 DEFVAR_LISP ("menu-updating-frame", &Vmenu_updating_frame,
2830 "Frame for which we are updating a menu.\n\
2831 The enable predicate for a menu command should check this variable.");
2832 Vmenu_updating_frame = Qnil;
2833
2834 #ifdef USE_X_TOOLKIT
2835 widget_id_tick = (1<<16);
2836 next_menubar_widget_id = 1;
2837 #endif
2838
2839 defsubr (&Sx_popup_menu);
2840 #ifdef HAVE_MENUS
2841 defsubr (&Sx_popup_dialog);
2842 #endif
2843 }