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