(set_frame_menubar): Don't call
[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 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 Lisp_Object keymap, tem;
689 int xpos, ypos;
690 Lisp_Object title;
691 char *error_name;
692 Lisp_Object selection;
693 FRAME_PTR f;
694 Lisp_Object x, y, window;
695 int keymaps = 0;
696 int for_click = 0;
697 struct gcpro gcpro1;
698
699 #ifdef HAVE_MENUS
700 if (! NILP (position))
701 {
702 check_x ();
703
704 /* Decode the first argument: find the window and the coordinates. */
705 if (EQ (position, Qt)
706 || (CONSP (position) && EQ (XCAR (position), Qmenu_bar)))
707 {
708 /* Use the mouse's current position. */
709 FRAME_PTR new_f = SELECTED_FRAME ();
710 Lisp_Object bar_window;
711 enum scroll_bar_part part;
712 unsigned long time;
713
714 if (mouse_position_hook)
715 (*mouse_position_hook) (&new_f, 1, &bar_window,
716 &part, &x, &y, &time);
717 if (new_f != 0)
718 XSETFRAME (window, new_f);
719 else
720 {
721 window = selected_window;
722 XSETFASTINT (x, 0);
723 XSETFASTINT (y, 0);
724 }
725 }
726 else
727 {
728 tem = Fcar (position);
729 if (CONSP (tem))
730 {
731 window = Fcar (Fcdr (position));
732 x = Fcar (tem);
733 y = Fcar (Fcdr (tem));
734 }
735 else
736 {
737 for_click = 1;
738 tem = Fcar (Fcdr (position)); /* EVENT_START (position) */
739 window = Fcar (tem); /* POSN_WINDOW (tem) */
740 tem = Fcar (Fcdr (Fcdr (tem))); /* POSN_WINDOW_POSN (tem) */
741 x = Fcar (tem);
742 y = Fcdr (tem);
743 }
744 }
745
746 CHECK_NUMBER (x, 0);
747 CHECK_NUMBER (y, 0);
748
749 /* Decode where to put the menu. */
750
751 if (FRAMEP (window))
752 {
753 f = XFRAME (window);
754 xpos = 0;
755 ypos = 0;
756 }
757 else if (WINDOWP (window))
758 {
759 CHECK_LIVE_WINDOW (window, 0);
760 f = XFRAME (WINDOW_FRAME (XWINDOW (window)));
761
762 xpos = (FONT_WIDTH (f->output_data.x->font)
763 * XFASTINT (XWINDOW (window)->left));
764 ypos = (f->output_data.x->line_height
765 * XFASTINT (XWINDOW (window)->top));
766 }
767 else
768 /* ??? Not really clean; should be CHECK_WINDOW_OR_FRAME,
769 but I don't want to make one now. */
770 CHECK_WINDOW (window, 0);
771
772 xpos += XINT (x);
773 ypos += XINT (y);
774
775 XSETFRAME (Vmenu_updating_frame, f);
776 }
777 Vmenu_updating_frame = Qnil;
778 #endif /* HAVE_MENUS */
779
780 title = Qnil;
781 GCPRO1 (title);
782
783 /* Decode the menu items from what was specified. */
784
785 keymap = Fkeymapp (menu);
786 tem = Qnil;
787 if (CONSP (menu))
788 tem = Fkeymapp (Fcar (menu));
789 if (!NILP (keymap))
790 {
791 /* We were given a keymap. Extract menu info from the keymap. */
792 Lisp_Object prompt;
793 keymap = get_keymap (menu);
794
795 /* Extract the detailed info to make one pane. */
796 keymap_panes (&menu, 1, NILP (position));
797
798 /* Search for a string appearing directly as an element of the keymap.
799 That string is the title of the menu. */
800 prompt = map_prompt (keymap);
801 if (NILP (title) && !NILP (prompt))
802 title = prompt;
803
804 /* Make that be the pane title of the first pane. */
805 if (!NILP (prompt) && menu_items_n_panes >= 0)
806 XVECTOR (menu_items)->contents[MENU_ITEMS_PANE_NAME] = prompt;
807
808 keymaps = 1;
809 }
810 else if (!NILP (tem))
811 {
812 /* We were given a list of keymaps. */
813 int nmaps = XFASTINT (Flength (menu));
814 Lisp_Object *maps
815 = (Lisp_Object *) alloca (nmaps * sizeof (Lisp_Object));
816 int i;
817
818 title = Qnil;
819
820 /* The first keymap that has a prompt string
821 supplies the menu title. */
822 for (tem = menu, i = 0; CONSP (tem); tem = Fcdr (tem))
823 {
824 Lisp_Object prompt;
825
826 maps[i++] = keymap = get_keymap (Fcar (tem));
827
828 prompt = map_prompt (keymap);
829 if (NILP (title) && !NILP (prompt))
830 title = prompt;
831 }
832
833 /* Extract the detailed info to make one pane. */
834 keymap_panes (maps, nmaps, NILP (position));
835
836 /* Make the title be the pane title of the first pane. */
837 if (!NILP (title) && menu_items_n_panes >= 0)
838 XVECTOR (menu_items)->contents[MENU_ITEMS_PANE_NAME] = title;
839
840 keymaps = 1;
841 }
842 else
843 {
844 /* We were given an old-fashioned menu. */
845 title = Fcar (menu);
846 CHECK_STRING (title, 1);
847
848 list_of_panes (Fcdr (menu));
849
850 keymaps = 0;
851 }
852
853 if (NILP (position))
854 {
855 discard_menu_items ();
856 UNGCPRO;
857 return Qnil;
858 }
859
860 #ifdef HAVE_MENUS
861 /* Display them in a menu. */
862 BLOCK_INPUT;
863
864 selection = xmenu_show (f, xpos, ypos, for_click,
865 keymaps, title, &error_name);
866 UNBLOCK_INPUT;
867
868 discard_menu_items ();
869
870 UNGCPRO;
871 #endif /* HAVE_MENUS */
872
873 if (error_name) error (error_name);
874 return selection;
875 }
876
877 #ifdef HAVE_MENUS
878
879 DEFUN ("x-popup-dialog", Fx_popup_dialog, Sx_popup_dialog, 2, 2, 0,
880 "Pop up a dialog box and return user's selection.\n\
881 POSITION specifies which frame to use.\n\
882 This is normally a mouse button event or a window or frame.\n\
883 If POSITION is t, it means to use the frame the mouse is on.\n\
884 The dialog box appears in the middle of the specified frame.\n\
885 \n\
886 CONTENTS specifies the alternatives to display in the dialog box.\n\
887 It is a list of the form (TITLE ITEM1 ITEM2...).\n\
888 Each ITEM is a cons cell (STRING . VALUE).\n\
889 The return value is VALUE from the chosen item.\n\n\
890 An ITEM may also be just a string--that makes a nonselectable item.\n\
891 An ITEM may also be nil--that means to put all preceding items\n\
892 on the left of the dialog box and all following items on the right.\n\
893 \(By default, approximately half appear on each side.)")
894 (position, contents)
895 Lisp_Object position, contents;
896 {
897 FRAME_PTR f;
898 Lisp_Object window;
899
900 check_x ();
901
902 /* Decode the first argument: find the window or frame to use. */
903 if (EQ (position, Qt)
904 || (CONSP (position) && EQ (XCAR (position), Qmenu_bar)))
905 {
906 #if 0 /* Using the frame the mouse is on may not be right. */
907 /* Use the mouse's current position. */
908 FRAME_PTR new_f = SELECTED_FRAME ();
909 Lisp_Object bar_window;
910 int part;
911 unsigned long time;
912 Lisp_Object x, y;
913
914 (*mouse_position_hook) (&new_f, 1, &bar_window, &part, &x, &y, &time);
915
916 if (new_f != 0)
917 XSETFRAME (window, new_f);
918 else
919 window = selected_window;
920 #endif
921 window = selected_window;
922 }
923 else if (CONSP (position))
924 {
925 Lisp_Object tem;
926 tem = Fcar (position);
927 if (CONSP (tem))
928 window = Fcar (Fcdr (position));
929 else
930 {
931 tem = Fcar (Fcdr (position)); /* EVENT_START (position) */
932 window = Fcar (tem); /* POSN_WINDOW (tem) */
933 }
934 }
935 else if (WINDOWP (position) || FRAMEP (position))
936 window = position;
937 else
938 window = Qnil;
939
940 /* Decode where to put the menu. */
941
942 if (FRAMEP (window))
943 f = XFRAME (window);
944 else if (WINDOWP (window))
945 {
946 CHECK_LIVE_WINDOW (window, 0);
947 f = XFRAME (WINDOW_FRAME (XWINDOW (window)));
948 }
949 else
950 /* ??? Not really clean; should be CHECK_WINDOW_OR_FRAME,
951 but I don't want to make one now. */
952 CHECK_WINDOW (window, 0);
953
954 #ifndef USE_X_TOOLKIT
955 /* Display a menu with these alternatives
956 in the middle of frame F. */
957 {
958 Lisp_Object x, y, frame, newpos;
959 XSETFRAME (frame, f);
960 XSETINT (x, x_pixel_width (f) / 2);
961 XSETINT (y, x_pixel_height (f) / 2);
962 newpos = Fcons (Fcons (x, Fcons (y, Qnil)), Fcons (frame, Qnil));
963
964 return Fx_popup_menu (newpos,
965 Fcons (Fcar (contents), Fcons (contents, Qnil)));
966 }
967 #else
968 {
969 Lisp_Object title;
970 char *error_name;
971 Lisp_Object selection;
972
973 /* Decode the dialog items from what was specified. */
974 title = Fcar (contents);
975 CHECK_STRING (title, 1);
976
977 list_of_panes (Fcons (contents, Qnil));
978
979 /* Display them in a dialog box. */
980 BLOCK_INPUT;
981 selection = xdialog_show (f, 0, title, &error_name);
982 UNBLOCK_INPUT;
983
984 discard_menu_items ();
985
986 if (error_name) error (error_name);
987 return selection;
988 }
989 #endif
990 }
991 \f
992 #ifdef USE_X_TOOLKIT
993
994 /* Loop in Xt until the menu pulldown or dialog popup has been
995 popped down (deactivated). This is used for x-popup-menu
996 and x-popup-dialog; it is not used for the menu bar any more.
997
998 NOTE: All calls to popup_get_selection should be protected
999 with BLOCK_INPUT, UNBLOCK_INPUT wrappers. */
1000
1001 void
1002 popup_get_selection (initial_event, dpyinfo, id)
1003 XEvent *initial_event;
1004 struct x_display_info *dpyinfo;
1005 LWLIB_ID id;
1006 {
1007 XEvent event;
1008
1009 /* Define a queue to save up for later unreading
1010 all X events that don't pertain to the menu. */
1011 struct event_queue
1012 {
1013 XEvent event;
1014 struct event_queue *next;
1015 };
1016
1017 struct event_queue *queue = NULL;
1018 struct event_queue *queue_tmp;
1019
1020 if (initial_event)
1021 event = *initial_event;
1022 else
1023 XtAppNextEvent (Xt_app_con, &event);
1024
1025 while (1)
1026 {
1027 /* Handle expose events for editor frames right away. */
1028 if (event.type == Expose)
1029 process_expose_from_menu (event);
1030 /* Make sure we don't consider buttons grabbed after menu goes.
1031 And make sure to deactivate for any ButtonRelease,
1032 even if XtDispatchEvent doesn't do that. */
1033 else if (event.type == ButtonRelease
1034 && dpyinfo->display == event.xbutton.display)
1035 {
1036 dpyinfo->grabbed &= ~(1 << event.xbutton.button);
1037 popup_activated_flag = 0;
1038 #ifdef USE_MOTIF /* Pretending that the event came from a
1039 Btn1Down seems the only way to convince Motif to
1040 activate its callbacks; setting the XmNmenuPost
1041 isn't working. --marcus@sysc.pdx.edu. */
1042 event.xbutton.button = 1;
1043 #endif
1044 }
1045 /* If the user presses a key, deactivate the menu.
1046 The user is likely to do that if we get wedged. */
1047 else if (event.type == KeyPress
1048 && dpyinfo->display == event.xbutton.display)
1049 {
1050 KeySym keysym = XLookupKeysym (&event.xkey, 0);
1051 if (!IsModifierKey (keysym))
1052 {
1053 popup_activated_flag = 0;
1054 break;
1055 }
1056 }
1057 /* Button presses outside the menu also pop it down. */
1058 else if (event.type == ButtonPress
1059 && event.xany.display == dpyinfo->display
1060 && x_any_window_to_frame (dpyinfo, event.xany.window))
1061 {
1062 popup_activated_flag = 0;
1063 break;
1064 }
1065
1066 /* Queue all events not for this popup,
1067 except for Expose, which we've already handled, and ButtonRelease.
1068 Note that the X window is associated with the frame if this
1069 is a menu bar popup, but not if it's a dialog box. So we use
1070 x_non_menubar_window_to_frame, not x_any_window_to_frame. */
1071 if (event.type != Expose
1072 && !(event.type == ButtonRelease
1073 && dpyinfo->display == event.xbutton.display)
1074 && (event.xany.display != dpyinfo->display
1075 || x_non_menubar_window_to_frame (dpyinfo, event.xany.window)))
1076 {
1077 queue_tmp = (struct event_queue *) malloc (sizeof (struct event_queue));
1078
1079 if (queue_tmp != NULL)
1080 {
1081 queue_tmp->event = event;
1082 queue_tmp->next = queue;
1083 queue = queue_tmp;
1084 }
1085 }
1086 else
1087 XtDispatchEvent (&event);
1088
1089 if (!popup_activated ())
1090 break;
1091 XtAppNextEvent (Xt_app_con, &event);
1092 }
1093
1094 /* Unread any events that we got but did not handle. */
1095 while (queue != NULL)
1096 {
1097 queue_tmp = queue;
1098 XPutBackEvent (queue_tmp->event.xany.display, &queue_tmp->event);
1099 queue = queue_tmp->next;
1100 free ((char *)queue_tmp);
1101 /* Cause these events to get read as soon as we UNBLOCK_INPUT. */
1102 interrupt_input_pending = 1;
1103 }
1104 }
1105
1106 /* Activate the menu bar of frame F.
1107 This is called from keyboard.c when it gets the
1108 menu_bar_activate_event out of the Emacs event queue.
1109
1110 To activate the menu bar, we use the X button-press event
1111 that was saved in saved_menu_event.
1112 That makes the toolkit do its thing.
1113
1114 But first we recompute the menu bar contents (the whole tree).
1115
1116 The reason for saving the button event until here, instead of
1117 passing it to the toolkit right away, is that we can safely
1118 execute Lisp code. */
1119
1120 void
1121 x_activate_menubar (f)
1122 FRAME_PTR f;
1123 {
1124 if (!f->output_data.x->saved_menu_event->type)
1125 return;
1126
1127 set_frame_menubar (f, 0, 1);
1128 BLOCK_INPUT;
1129 XtDispatchEvent ((XEvent *) f->output_data.x->saved_menu_event);
1130 UNBLOCK_INPUT;
1131 #ifdef USE_MOTIF
1132 if (f->output_data.x->saved_menu_event->type == ButtonRelease)
1133 pending_menu_activation = 1;
1134 #endif
1135
1136 /* Ignore this if we get it a second time. */
1137 f->output_data.x->saved_menu_event->type = 0;
1138 }
1139
1140 /* Detect if a dialog or menu has been posted. */
1141
1142 int
1143 popup_activated ()
1144 {
1145 return popup_activated_flag;
1146 }
1147
1148
1149 /* This callback is invoked when the user selects a menubar cascade
1150 pushbutton, but before the pulldown menu is posted. */
1151
1152 static void
1153 popup_activate_callback (widget, id, client_data)
1154 Widget widget;
1155 LWLIB_ID id;
1156 XtPointer client_data;
1157 {
1158 popup_activated_flag = 1;
1159 }
1160
1161 /* This callback is called from the menu bar pulldown menu
1162 when the user makes a selection.
1163 Figure out what the user chose
1164 and put the appropriate events into the keyboard buffer. */
1165
1166 static void
1167 menubar_selection_callback (widget, id, client_data)
1168 Widget widget;
1169 LWLIB_ID id;
1170 XtPointer client_data;
1171 {
1172 Lisp_Object prefix, entry;
1173 FRAME_PTR f = menubar_id_to_frame (id);
1174 Lisp_Object vector;
1175 Lisp_Object *subprefix_stack;
1176 int submenu_depth = 0;
1177 int i;
1178
1179 if (!f)
1180 return;
1181 subprefix_stack = (Lisp_Object *) alloca (f->menu_bar_items_used * sizeof (Lisp_Object));
1182 vector = f->menu_bar_vector;
1183 prefix = Qnil;
1184 i = 0;
1185 while (i < f->menu_bar_items_used)
1186 {
1187 if (EQ (XVECTOR (vector)->contents[i], Qnil))
1188 {
1189 subprefix_stack[submenu_depth++] = prefix;
1190 prefix = entry;
1191 i++;
1192 }
1193 else if (EQ (XVECTOR (vector)->contents[i], Qlambda))
1194 {
1195 prefix = subprefix_stack[--submenu_depth];
1196 i++;
1197 }
1198 else if (EQ (XVECTOR (vector)->contents[i], Qt))
1199 {
1200 prefix = XVECTOR (vector)->contents[i + MENU_ITEMS_PANE_PREFIX];
1201 i += MENU_ITEMS_PANE_LENGTH;
1202 }
1203 else
1204 {
1205 entry = XVECTOR (vector)->contents[i + MENU_ITEMS_ITEM_VALUE];
1206 /* The EMACS_INT cast avoids a warning. There's no problem
1207 as long as pointers have enough bits to hold small integers. */
1208 if ((int) (EMACS_INT) client_data == i)
1209 {
1210 int j;
1211 struct input_event buf;
1212 Lisp_Object frame;
1213
1214 XSETFRAME (frame, f);
1215 buf.kind = menu_bar_event;
1216 buf.frame_or_window = Fcons (frame, Fcons (Qmenu_bar, Qnil));
1217 kbd_buffer_store_event (&buf);
1218
1219 for (j = 0; j < submenu_depth; j++)
1220 if (!NILP (subprefix_stack[j]))
1221 {
1222 buf.kind = menu_bar_event;
1223 buf.frame_or_window = Fcons (frame, subprefix_stack[j]);
1224 kbd_buffer_store_event (&buf);
1225 }
1226
1227 if (!NILP (prefix))
1228 {
1229 buf.kind = menu_bar_event;
1230 buf.frame_or_window = Fcons (frame, prefix);
1231 kbd_buffer_store_event (&buf);
1232 }
1233
1234 buf.kind = menu_bar_event;
1235 buf.frame_or_window = Fcons (frame, entry);
1236 kbd_buffer_store_event (&buf);
1237
1238 return;
1239 }
1240 i += MENU_ITEMS_ITEM_LENGTH;
1241 }
1242 }
1243 }
1244
1245 /* This callback is invoked when a dialog or menu is finished being
1246 used and has been unposted. */
1247
1248 static void
1249 popup_deactivate_callback (widget, id, client_data)
1250 Widget widget;
1251 LWLIB_ID id;
1252 XtPointer client_data;
1253 {
1254 popup_activated_flag = 0;
1255 }
1256
1257 /* Allocate a widget_value, blocking input. */
1258
1259 widget_value *
1260 xmalloc_widget_value ()
1261 {
1262 widget_value *value;
1263
1264 BLOCK_INPUT;
1265 value = malloc_widget_value ();
1266 UNBLOCK_INPUT;
1267
1268 return value;
1269 }
1270
1271 /* This recursively calls free_widget_value on the tree of widgets.
1272 It must free all data that was malloc'ed for these widget_values.
1273 In Emacs, many slots are pointers into the data of Lisp_Strings, and
1274 must be left alone. */
1275
1276 void
1277 free_menubar_widget_value_tree (wv)
1278 widget_value *wv;
1279 {
1280 if (! wv) return;
1281
1282 wv->name = wv->value = wv->key = (char *) 0xDEADBEEF;
1283
1284 if (wv->contents && (wv->contents != (widget_value*)1))
1285 {
1286 free_menubar_widget_value_tree (wv->contents);
1287 wv->contents = (widget_value *) 0xDEADBEEF;
1288 }
1289 if (wv->next)
1290 {
1291 free_menubar_widget_value_tree (wv->next);
1292 wv->next = (widget_value *) 0xDEADBEEF;
1293 }
1294 BLOCK_INPUT;
1295 free_widget_value (wv);
1296 UNBLOCK_INPUT;
1297 }
1298 \f
1299 /* Return a tree of widget_value structures for a menu bar item
1300 whose event type is ITEM_KEY (with string ITEM_NAME)
1301 and whose contents come from the list of keymaps MAPS. */
1302
1303 static widget_value *
1304 single_submenu (item_key, item_name, maps)
1305 Lisp_Object item_key, item_name, maps;
1306 {
1307 widget_value *wv, *prev_wv, *save_wv, *first_wv;
1308 int i;
1309 int submenu_depth = 0;
1310 Lisp_Object length;
1311 int len;
1312 Lisp_Object *mapvec;
1313 widget_value **submenu_stack;
1314 int previous_items = menu_items_used;
1315 int top_level_items = 0;
1316
1317 length = Flength (maps);
1318 len = XINT (length);
1319
1320 /* Convert the list MAPS into a vector MAPVEC. */
1321 mapvec = (Lisp_Object *) alloca (len * sizeof (Lisp_Object));
1322 for (i = 0; i < len; i++)
1323 {
1324 mapvec[i] = Fcar (maps);
1325 maps = Fcdr (maps);
1326 }
1327
1328 menu_items_n_panes = 0;
1329
1330 /* Loop over the given keymaps, making a pane for each map.
1331 But don't make a pane that is empty--ignore that map instead. */
1332 for (i = 0; i < len; i++)
1333 {
1334 if (SYMBOLP (mapvec[i])
1335 || (CONSP (mapvec[i])
1336 && NILP (Fkeymapp (mapvec[i]))))
1337 {
1338 /* Here we have a command at top level in the menu bar
1339 as opposed to a submenu. */
1340 top_level_items = 1;
1341 push_menu_pane (Qnil, Qnil);
1342 push_menu_item (item_name, Qt, item_key, mapvec[i], Qnil, Qnil, Qnil);
1343 }
1344 else
1345 single_keymap_panes (mapvec[i], item_name, item_key, 0, 10);
1346 }
1347
1348 /* Create a tree of widget_value objects
1349 representing the panes and their items. */
1350
1351 submenu_stack
1352 = (widget_value **) alloca (menu_items_used * sizeof (widget_value *));
1353 wv = xmalloc_widget_value ();
1354 wv->name = "menu";
1355 wv->value = 0;
1356 wv->enabled = 1;
1357 wv->button_type = BUTTON_TYPE_NONE;
1358 first_wv = wv;
1359 save_wv = 0;
1360 prev_wv = 0;
1361
1362 /* Loop over all panes and items made during this call
1363 and construct a tree of widget_value objects.
1364 Ignore the panes and items made by previous calls to
1365 single_submenu, even though those are also in menu_items. */
1366 i = previous_items;
1367 while (i < menu_items_used)
1368 {
1369 if (EQ (XVECTOR (menu_items)->contents[i], Qnil))
1370 {
1371 submenu_stack[submenu_depth++] = save_wv;
1372 save_wv = prev_wv;
1373 prev_wv = 0;
1374 i++;
1375 }
1376 else if (EQ (XVECTOR (menu_items)->contents[i], Qlambda))
1377 {
1378 prev_wv = save_wv;
1379 save_wv = submenu_stack[--submenu_depth];
1380 i++;
1381 }
1382 else if (EQ (XVECTOR (menu_items)->contents[i], Qt)
1383 && submenu_depth != 0)
1384 i += MENU_ITEMS_PANE_LENGTH;
1385 /* Ignore a nil in the item list.
1386 It's meaningful only for dialog boxes. */
1387 else if (EQ (XVECTOR (menu_items)->contents[i], Qquote))
1388 i += 1;
1389 else if (EQ (XVECTOR (menu_items)->contents[i], Qt))
1390 {
1391 /* Create a new pane. */
1392 Lisp_Object pane_name, prefix;
1393 char *pane_string;
1394 pane_name = XVECTOR (menu_items)->contents[i + MENU_ITEMS_PANE_NAME];
1395 prefix = XVECTOR (menu_items)->contents[i + MENU_ITEMS_PANE_PREFIX];
1396 #ifndef HAVE_MULTILINGUAL_MENU
1397 if (STRINGP (pane_name) && STRING_MULTIBYTE (pane_name))
1398 pane_name = string_make_unibyte (pane_name);
1399 #endif
1400 pane_string = (NILP (pane_name)
1401 ? "" : (char *) XSTRING (pane_name)->data);
1402 /* If there is just one top-level pane, put all its items directly
1403 under the top-level menu. */
1404 if (menu_items_n_panes == 1)
1405 pane_string = "";
1406
1407 /* If the pane has a meaningful name,
1408 make the pane a top-level menu item
1409 with its items as a submenu beneath it. */
1410 if (strcmp (pane_string, ""))
1411 {
1412 wv = xmalloc_widget_value ();
1413 if (save_wv)
1414 save_wv->next = wv;
1415 else
1416 first_wv->contents = wv;
1417 wv->name = pane_string;
1418 /* Ignore the @ that means "separate pane".
1419 This is a kludge, but this isn't worth more time. */
1420 if (!NILP (prefix) && wv->name[0] == '@')
1421 wv->name++;
1422 wv->value = 0;
1423 wv->enabled = 1;
1424 wv->button_type = BUTTON_TYPE_NONE;
1425 }
1426 save_wv = wv;
1427 prev_wv = 0;
1428 i += MENU_ITEMS_PANE_LENGTH;
1429 }
1430 else
1431 {
1432 /* Create a new item within current pane. */
1433 Lisp_Object item_name, enable, descrip, def, type, selected;
1434 item_name = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_NAME];
1435 enable = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_ENABLE];
1436 descrip
1437 = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_EQUIV_KEY];
1438 def = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_DEFINITION];
1439 type = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_TYPE];
1440 selected = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_SELECTED];
1441
1442 #ifndef HAVE_MULTILINGUAL_MENU
1443 if (STRING_MULTIBYTE (item_name))
1444 item_name = string_make_unibyte (item_name);
1445 if (STRINGP (descrip) && STRING_MULTIBYTE (descrip))
1446 descrip = string_make_unibyte (descrip);
1447 #endif
1448
1449 wv = xmalloc_widget_value ();
1450 if (prev_wv)
1451 prev_wv->next = wv;
1452 else
1453 save_wv->contents = wv;
1454
1455 wv->name = (char *) XSTRING (item_name)->data;
1456 if (!NILP (descrip))
1457 wv->key = (char *) XSTRING (descrip)->data;
1458 wv->value = 0;
1459 /* The EMACS_INT cast avoids a warning. There's no problem
1460 as long as pointers have enough bits to hold small integers. */
1461 wv->call_data = (!NILP (def) ? (void *) (EMACS_INT) i : 0);
1462 wv->enabled = !NILP (enable);
1463
1464 if (NILP (type))
1465 wv->button_type = BUTTON_TYPE_NONE;
1466 else if (EQ (type, QCradio))
1467 wv->button_type = BUTTON_TYPE_RADIO;
1468 else if (EQ (type, QCtoggle))
1469 wv->button_type = BUTTON_TYPE_TOGGLE;
1470 else
1471 abort ();
1472
1473 wv->selected = !NILP (selected);
1474
1475 prev_wv = wv;
1476
1477 i += MENU_ITEMS_ITEM_LENGTH;
1478 }
1479 }
1480
1481 /* If we have just one "menu item"
1482 that was originally a button, return it by itself. */
1483 if (top_level_items && first_wv->contents && first_wv->contents->next == 0)
1484 {
1485 wv = first_wv->contents;
1486 free_widget_value (first_wv);
1487 return wv;
1488 }
1489
1490 return first_wv;
1491 }
1492 \f
1493 extern void EmacsFrameSetCharSize ();
1494
1495 /* Recompute all the widgets of frame F, when the menu bar
1496 has been changed. */
1497
1498 static void
1499 update_frame_menubar (f)
1500 FRAME_PTR f;
1501 {
1502 struct x_output *x = f->output_data.x;
1503 int columns, rows;
1504 int menubar_changed;
1505
1506 /* We assume the menubar contents has changed if the global flag is set,
1507 or if the current buffer has changed, or if the menubar has never
1508 been updated before.
1509 */
1510 menubar_changed = (x->menubar_widget
1511 && !XtIsManaged (x->menubar_widget));
1512
1513 if (! (menubar_changed))
1514 return;
1515
1516 BLOCK_INPUT;
1517 /* Save the size of the frame because the pane widget doesn't accept to
1518 resize itself. So force it. */
1519 columns = f->width;
1520 rows = f->height;
1521
1522 /* Do the voodoo which means "I'm changing lots of things, don't try to
1523 refigure sizes until I'm done." */
1524 lw_refigure_widget (x->column_widget, False);
1525
1526 /* the order in which children are managed is the top to
1527 bottom order in which they are displayed in the paned window.
1528 First, remove the text-area widget.
1529 */
1530 XtUnmanageChild (x->edit_widget);
1531
1532 /* remove the menubar that is there now, and put up the menubar that
1533 should be there.
1534 */
1535 if (menubar_changed)
1536 {
1537 XtManageChild (x->menubar_widget);
1538 XtMapWidget (x->menubar_widget);
1539 XtVaSetValues (x->menubar_widget, XtNmappedWhenManaged, 1, 0);
1540 }
1541
1542 /* Re-manage the text-area widget, and then thrash the sizes. */
1543 XtManageChild (x->edit_widget);
1544 x_set_menu_resources_from_menu_face (f, x->menubar_widget);
1545 lw_refigure_widget (x->column_widget, True);
1546
1547 /* Force the pane widget to resize itself with the right values. */
1548 EmacsFrameSetCharSize (x->edit_widget, columns, rows);
1549 UNBLOCK_INPUT;
1550 }
1551
1552 /* Set the contents of the menubar widgets of frame F.
1553 The argument FIRST_TIME is currently ignored;
1554 it is set the first time this is called, from initialize_frame_menubar. */
1555
1556 void
1557 set_frame_menubar (f, first_time, deep_p)
1558 FRAME_PTR f;
1559 int first_time;
1560 int deep_p;
1561 {
1562 Widget menubar_widget = f->output_data.x->menubar_widget;
1563 Lisp_Object items;
1564 widget_value *wv, *first_wv, *prev_wv = 0;
1565 int i;
1566 LWLIB_ID id;
1567
1568 XSETFRAME (Vmenu_updating_frame, f);
1569
1570 if (f->output_data.x->id == 0)
1571 f->output_data.x->id = next_menubar_widget_id++;
1572 id = f->output_data.x->id;
1573
1574 if (! menubar_widget)
1575 deep_p = 1;
1576 else if (pending_menu_activation && !deep_p)
1577 deep_p = 1;
1578 /* Make the first call for any given frame always go deep. */
1579 else if (!f->output_data.x->saved_menu_event && !deep_p)
1580 {
1581 deep_p = 1;
1582 f->output_data.x->saved_menu_event = (XEvent*)xmalloc (sizeof (XEvent));
1583 f->output_data.x->saved_menu_event->type = 0;
1584 }
1585
1586 wv = xmalloc_widget_value ();
1587 wv->name = "menubar";
1588 wv->value = 0;
1589 wv->enabled = 1;
1590 wv->button_type = BUTTON_TYPE_NONE;
1591 first_wv = wv;
1592
1593 if (deep_p)
1594 {
1595 /* Make a widget-value tree representing the entire menu trees. */
1596
1597 struct buffer *prev = current_buffer;
1598 Lisp_Object buffer;
1599 int specpdl_count = specpdl_ptr - specpdl;
1600 int previous_menu_items_used = f->menu_bar_items_used;
1601 Lisp_Object *previous_items
1602 = (Lisp_Object *) alloca (previous_menu_items_used
1603 * sizeof (Lisp_Object));
1604
1605 /* If we are making a new widget, its contents are empty,
1606 do always reinitialize them. */
1607 if (! menubar_widget)
1608 previous_menu_items_used = 0;
1609
1610 buffer = XWINDOW (FRAME_SELECTED_WINDOW (f))->buffer;
1611 specbind (Qinhibit_quit, Qt);
1612 /* Don't let the debugger step into this code
1613 because it is not reentrant. */
1614 specbind (Qdebug_on_next_call, Qnil);
1615
1616 record_unwind_protect (Fset_match_data, Fmatch_data (Qnil, Qnil));
1617 if (NILP (Voverriding_local_map_menu_flag))
1618 {
1619 specbind (Qoverriding_terminal_local_map, Qnil);
1620 specbind (Qoverriding_local_map, Qnil);
1621 }
1622
1623 set_buffer_internal_1 (XBUFFER (buffer));
1624
1625 /* Run the Lucid hook. */
1626 call1 (Vrun_hooks, Qactivate_menubar_hook);
1627 /* If it has changed current-menubar from previous value,
1628 really recompute the menubar from the value. */
1629 if (! NILP (Vlucid_menu_bar_dirty_flag))
1630 call0 (Qrecompute_lucid_menubar);
1631 safe_run_hooks (Qmenu_bar_update_hook);
1632 FRAME_MENU_BAR_ITEMS (f) = menu_bar_items (FRAME_MENU_BAR_ITEMS (f));
1633
1634 items = FRAME_MENU_BAR_ITEMS (f);
1635
1636 inhibit_garbage_collection ();
1637
1638 /* Save the frame's previous menu bar contents data. */
1639 bcopy (XVECTOR (f->menu_bar_vector)->contents, previous_items,
1640 previous_menu_items_used * sizeof (Lisp_Object));
1641
1642 /* Fill in the current menu bar contents. */
1643 menu_items = f->menu_bar_vector;
1644 menu_items_allocated = XVECTOR (menu_items)->size;
1645 init_menu_items ();
1646 for (i = 0; i < XVECTOR (items)->size; i += 4)
1647 {
1648 Lisp_Object key, string, maps;
1649
1650 key = XVECTOR (items)->contents[i];
1651 string = XVECTOR (items)->contents[i + 1];
1652 maps = XVECTOR (items)->contents[i + 2];
1653 if (NILP (string))
1654 break;
1655
1656 wv = single_submenu (key, string, maps);
1657 if (prev_wv)
1658 prev_wv->next = wv;
1659 else
1660 first_wv->contents = wv;
1661 /* Don't set wv->name here; GC during the loop might relocate it. */
1662 wv->enabled = 1;
1663 wv->button_type = BUTTON_TYPE_NONE;
1664 prev_wv = wv;
1665 }
1666
1667 finish_menu_items ();
1668
1669 set_buffer_internal_1 (prev);
1670 unbind_to (specpdl_count, Qnil);
1671
1672 /* If there has been no change in the Lisp-level contents
1673 of the menu bar, skip redisplaying it. Just exit. */
1674
1675 for (i = 0; i < previous_menu_items_used; i++)
1676 if (menu_items_used == i
1677 || (!EQ (previous_items[i], XVECTOR (menu_items)->contents[i])))
1678 break;
1679 if (i == menu_items_used && i == previous_menu_items_used && i != 0)
1680 {
1681 free_menubar_widget_value_tree (first_wv);
1682 menu_items = Qnil;
1683
1684 return;
1685 }
1686
1687 /* Now GC cannot happen during the lifetime of the widget_value,
1688 so it's safe to store data from a Lisp_String. */
1689 wv = first_wv->contents;
1690 for (i = 0; i < XVECTOR (items)->size; i += 4)
1691 {
1692 Lisp_Object string;
1693 string = XVECTOR (items)->contents[i + 1];
1694 if (NILP (string))
1695 break;
1696 wv->name = (char *) XSTRING (string)->data;
1697 wv = wv->next;
1698 }
1699
1700 f->menu_bar_vector = menu_items;
1701 f->menu_bar_items_used = menu_items_used;
1702 menu_items = Qnil;
1703 }
1704 else
1705 {
1706 /* Make a widget-value tree containing
1707 just the top level menu bar strings. */
1708
1709 items = FRAME_MENU_BAR_ITEMS (f);
1710 for (i = 0; i < XVECTOR (items)->size; i += 4)
1711 {
1712 Lisp_Object string;
1713
1714 string = XVECTOR (items)->contents[i + 1];
1715 if (NILP (string))
1716 break;
1717
1718 wv = xmalloc_widget_value ();
1719 wv->name = (char *) XSTRING (string)->data;
1720 wv->value = 0;
1721 wv->enabled = 1;
1722 wv->button_type = BUTTON_TYPE_NONE;
1723 /* This prevents lwlib from assuming this
1724 menu item is really supposed to be empty. */
1725 /* The EMACS_INT cast avoids a warning.
1726 This value just has to be different from small integers. */
1727 wv->call_data = (void *) (EMACS_INT) (-1);
1728
1729 if (prev_wv)
1730 prev_wv->next = wv;
1731 else
1732 first_wv->contents = wv;
1733 prev_wv = wv;
1734 }
1735
1736 /* Forget what we thought we knew about what is in the
1737 detailed contents of the menu bar menus.
1738 Changing the top level always destroys the contents. */
1739 f->menu_bar_items_used = 0;
1740 }
1741
1742 /* Create or update the menu bar widget. */
1743
1744 BLOCK_INPUT;
1745
1746 if (menubar_widget)
1747 {
1748 /* Disable resizing (done for Motif!) */
1749 lw_allow_resizing (f->output_data.x->widget, False);
1750
1751 /* The third arg is DEEP_P, which says to consider the entire
1752 menu trees we supply, rather than just the menu bar item names. */
1753 lw_modify_all_widgets (id, first_wv, deep_p);
1754
1755 /* Re-enable the edit widget to resize. */
1756 lw_allow_resizing (f->output_data.x->widget, True);
1757 }
1758 else
1759 {
1760 menubar_widget = lw_create_widget ("menubar", "menubar", id, first_wv,
1761 f->output_data.x->column_widget,
1762 0,
1763 popup_activate_callback,
1764 menubar_selection_callback,
1765 popup_deactivate_callback);
1766 f->output_data.x->menubar_widget = menubar_widget;
1767 }
1768
1769 {
1770 int menubar_size
1771 = (f->output_data.x->menubar_widget
1772 ? (f->output_data.x->menubar_widget->core.height
1773 + f->output_data.x->menubar_widget->core.border_width)
1774 : 0);
1775
1776 #if 0 /* Experimentally, we now get the right results
1777 for -geometry -0-0 without this. 24 Aug 96, rms. */
1778 #ifdef USE_LUCID
1779 if (FRAME_EXTERNAL_MENU_BAR (f))
1780 {
1781 Dimension ibw = 0;
1782 XtVaGetValues (f->output_data.x->column_widget,
1783 XtNinternalBorderWidth, &ibw, NULL);
1784 menubar_size += ibw;
1785 }
1786 #endif /* USE_LUCID */
1787 #endif /* 0 */
1788
1789 f->output_data.x->menubar_height = menubar_size;
1790 }
1791
1792 free_menubar_widget_value_tree (first_wv);
1793 update_frame_menubar (f);
1794
1795 UNBLOCK_INPUT;
1796 }
1797
1798 /* Called from Fx_create_frame to create the initial menubar of a frame
1799 before it is mapped, so that the window is mapped with the menubar already
1800 there instead of us tacking it on later and thrashing the window after it
1801 is visible. */
1802
1803 void
1804 initialize_frame_menubar (f)
1805 FRAME_PTR f;
1806 {
1807 /* This function is called before the first chance to redisplay
1808 the frame. It has to be, so the frame will have the right size. */
1809 FRAME_MENU_BAR_ITEMS (f) = menu_bar_items (FRAME_MENU_BAR_ITEMS (f));
1810 set_frame_menubar (f, 1, 1);
1811 }
1812
1813 /* Get rid of the menu bar of frame F, and free its storage.
1814 This is used when deleting a frame, and when turning off the menu bar. */
1815
1816 void
1817 free_frame_menubar (f)
1818 FRAME_PTR f;
1819 {
1820 Widget menubar_widget;
1821
1822 menubar_widget = f->output_data.x->menubar_widget;
1823
1824 f->output_data.x->menubar_height = 0;
1825
1826 if (menubar_widget)
1827 {
1828 BLOCK_INPUT;
1829 lw_destroy_all_widgets ((LWLIB_ID) f->output_data.x->id);
1830 UNBLOCK_INPUT;
1831 }
1832 }
1833
1834 #endif /* USE_X_TOOLKIT */
1835 \f
1836 /* xmenu_show actually displays a menu using the panes and items in menu_items
1837 and returns the value selected from it.
1838 There are two versions of xmenu_show, one for Xt and one for Xlib.
1839 Both assume input is blocked by the caller. */
1840
1841 /* F is the frame the menu is for.
1842 X and Y are the frame-relative specified position,
1843 relative to the inside upper left corner of the frame F.
1844 FOR_CLICK is nonzero if this menu was invoked for a mouse click.
1845 KEYMAPS is 1 if this menu was specified with keymaps;
1846 in that case, we return a list containing the chosen item's value
1847 and perhaps also the pane's prefix.
1848 TITLE is the specified menu title.
1849 ERROR is a place to store an error message string in case of failure.
1850 (We return nil on failure, but the value doesn't actually matter.) */
1851
1852 #ifdef USE_X_TOOLKIT
1853
1854 /* We need a unique id for each widget handled by the Lucid Widget
1855 library.
1856
1857 For the main windows, and popup menus, we use this counter,
1858 which we increment each time after use. This starts from 1<<16.
1859
1860 For menu bars, we use numbers starting at 0, counted in
1861 next_menubar_widget_id. */
1862 LWLIB_ID widget_id_tick;
1863
1864 #ifdef __STDC__
1865 static Lisp_Object *volatile menu_item_selection;
1866 #else
1867 static Lisp_Object *menu_item_selection;
1868 #endif
1869
1870 static void
1871 popup_selection_callback (widget, id, client_data)
1872 Widget widget;
1873 LWLIB_ID id;
1874 XtPointer client_data;
1875 {
1876 menu_item_selection = (Lisp_Object *) client_data;
1877 }
1878
1879 static Lisp_Object
1880 xmenu_show (f, x, y, for_click, keymaps, title, error)
1881 FRAME_PTR f;
1882 int x;
1883 int y;
1884 int for_click;
1885 int keymaps;
1886 Lisp_Object title;
1887 char **error;
1888 {
1889 int i;
1890 LWLIB_ID menu_id;
1891 Widget menu;
1892 Arg av[2];
1893 int ac = 0;
1894 widget_value *wv, *save_wv = 0, *first_wv = 0, *prev_wv = 0;
1895 widget_value **submenu_stack
1896 = (widget_value **) alloca (menu_items_used * sizeof (widget_value *));
1897 Lisp_Object *subprefix_stack
1898 = (Lisp_Object *) alloca (menu_items_used * sizeof (Lisp_Object));
1899 int submenu_depth = 0;
1900 XButtonPressedEvent dummy;
1901
1902 int first_pane;
1903
1904 *error = NULL;
1905
1906 if (menu_items_used <= MENU_ITEMS_PANE_LENGTH)
1907 {
1908 *error = "Empty menu";
1909 return Qnil;
1910 }
1911
1912 /* Create a tree of widget_value objects
1913 representing the panes and their items. */
1914 wv = xmalloc_widget_value ();
1915 wv->name = "menu";
1916 wv->value = 0;
1917 wv->enabled = 1;
1918 wv->button_type = BUTTON_TYPE_NONE;
1919 first_wv = wv;
1920 first_pane = 1;
1921
1922 /* Loop over all panes and items, filling in the tree. */
1923 i = 0;
1924 while (i < menu_items_used)
1925 {
1926 if (EQ (XVECTOR (menu_items)->contents[i], Qnil))
1927 {
1928 submenu_stack[submenu_depth++] = save_wv;
1929 save_wv = prev_wv;
1930 prev_wv = 0;
1931 first_pane = 1;
1932 i++;
1933 }
1934 else if (EQ (XVECTOR (menu_items)->contents[i], Qlambda))
1935 {
1936 prev_wv = save_wv;
1937 save_wv = submenu_stack[--submenu_depth];
1938 first_pane = 0;
1939 i++;
1940 }
1941 else if (EQ (XVECTOR (menu_items)->contents[i], Qt)
1942 && submenu_depth != 0)
1943 i += MENU_ITEMS_PANE_LENGTH;
1944 /* Ignore a nil in the item list.
1945 It's meaningful only for dialog boxes. */
1946 else if (EQ (XVECTOR (menu_items)->contents[i], Qquote))
1947 i += 1;
1948 else if (EQ (XVECTOR (menu_items)->contents[i], Qt))
1949 {
1950 /* Create a new pane. */
1951 Lisp_Object pane_name, prefix;
1952 char *pane_string;
1953 pane_name = XVECTOR (menu_items)->contents[i + MENU_ITEMS_PANE_NAME];
1954 prefix = XVECTOR (menu_items)->contents[i + MENU_ITEMS_PANE_PREFIX];
1955 #ifndef HAVE_MULTILINGUAL_MENU
1956 if (!NILP (pane_name) && STRING_MULTIBYTE (pane_name))
1957 pane_name = string_make_unibyte (pane_name);
1958 #endif
1959 pane_string = (NILP (pane_name)
1960 ? "" : (char *) XSTRING (pane_name)->data);
1961 /* If there is just one top-level pane, put all its items directly
1962 under the top-level menu. */
1963 if (menu_items_n_panes == 1)
1964 pane_string = "";
1965
1966 /* If the pane has a meaningful name,
1967 make the pane a top-level menu item
1968 with its items as a submenu beneath it. */
1969 if (!keymaps && strcmp (pane_string, ""))
1970 {
1971 wv = xmalloc_widget_value ();
1972 if (save_wv)
1973 save_wv->next = wv;
1974 else
1975 first_wv->contents = wv;
1976 wv->name = pane_string;
1977 if (keymaps && !NILP (prefix))
1978 wv->name++;
1979 wv->value = 0;
1980 wv->enabled = 1;
1981 wv->button_type = BUTTON_TYPE_NONE;
1982 save_wv = wv;
1983 prev_wv = 0;
1984 }
1985 else if (first_pane)
1986 {
1987 save_wv = wv;
1988 prev_wv = 0;
1989 }
1990 first_pane = 0;
1991 i += MENU_ITEMS_PANE_LENGTH;
1992 }
1993 else
1994 {
1995 /* Create a new item within current pane. */
1996 Lisp_Object item_name, enable, descrip, def, type, selected;
1997 item_name = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_NAME];
1998 enable = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_ENABLE];
1999 descrip
2000 = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_EQUIV_KEY];
2001 def = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_DEFINITION];
2002 type = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_TYPE];
2003 selected = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_SELECTED];
2004
2005 #ifndef HAVE_MULTILINGUAL_MENU
2006 if (STRINGP (item_name) && STRING_MULTIBYTE (item_name))
2007 item_name = string_make_unibyte (item_name);
2008 if (STRINGP (descrip) && STRING_MULTIBYTE (descrip))
2009 item_name = string_make_unibyte (descrip);
2010 #endif
2011
2012 wv = xmalloc_widget_value ();
2013 if (prev_wv)
2014 prev_wv->next = wv;
2015 else
2016 save_wv->contents = wv;
2017 wv->name = (char *) XSTRING (item_name)->data;
2018 if (!NILP (descrip))
2019 wv->key = (char *) XSTRING (descrip)->data;
2020 wv->value = 0;
2021 /* If this item has a null value,
2022 make the call_data null so that it won't display a box
2023 when the mouse is on it. */
2024 wv->call_data
2025 = (!NILP (def) ? (void *) &XVECTOR (menu_items)->contents[i] : 0);
2026 wv->enabled = !NILP (enable);
2027
2028 if (NILP (type))
2029 wv->button_type = BUTTON_TYPE_NONE;
2030 else if (EQ (type, QCtoggle))
2031 wv->button_type = BUTTON_TYPE_TOGGLE;
2032 else if (EQ (type, QCradio))
2033 wv->button_type = BUTTON_TYPE_RADIO;
2034 else
2035 abort ();
2036
2037 wv->selected = !NILP (selected);
2038
2039 prev_wv = wv;
2040
2041 i += MENU_ITEMS_ITEM_LENGTH;
2042 }
2043 }
2044
2045 /* Deal with the title, if it is non-nil. */
2046 if (!NILP (title))
2047 {
2048 widget_value *wv_title = xmalloc_widget_value ();
2049 widget_value *wv_sep1 = xmalloc_widget_value ();
2050 widget_value *wv_sep2 = xmalloc_widget_value ();
2051
2052 wv_sep2->name = "--";
2053 wv_sep2->next = first_wv->contents;
2054
2055 wv_sep1->name = "--";
2056 wv_sep1->next = wv_sep2;
2057
2058 #ifndef HAVE_MULTILINGUAL_MENU
2059 if (STRING_MULTIBYTE (title))
2060 title = string_make_unibyte (title);
2061 #endif
2062 wv_title->name = (char *) XSTRING (title)->data;
2063 wv_title->enabled = True;
2064 wv_title->button_type = BUTTON_TYPE_NONE;
2065 wv_title->next = wv_sep1;
2066 first_wv->contents = wv_title;
2067 }
2068
2069 /* Actually create the menu. */
2070 menu_id = widget_id_tick++;
2071 menu = lw_create_widget ("popup", first_wv->name, menu_id, first_wv,
2072 f->output_data.x->widget, 1, 0,
2073 popup_selection_callback,
2074 popup_deactivate_callback);
2075
2076 /* Adjust coordinates to relative to the outer (window manager) window. */
2077 {
2078 Window child;
2079 int win_x = 0, win_y = 0;
2080
2081 /* Find the position of the outside upper-left corner of
2082 the inner window, with respect to the outer window. */
2083 if (f->output_data.x->parent_desc != FRAME_X_DISPLAY_INFO (f)->root_window)
2084 {
2085 BLOCK_INPUT;
2086 XTranslateCoordinates (FRAME_X_DISPLAY (f),
2087
2088 /* From-window, to-window. */
2089 f->output_data.x->window_desc,
2090 f->output_data.x->parent_desc,
2091
2092 /* From-position, to-position. */
2093 0, 0, &win_x, &win_y,
2094
2095 /* Child of window. */
2096 &child);
2097 UNBLOCK_INPUT;
2098 x += win_x;
2099 y += win_y;
2100 }
2101 }
2102
2103 /* Adjust coordinates to be root-window-relative. */
2104 x += f->output_data.x->left_pos;
2105 y += f->output_data.x->top_pos;
2106
2107 dummy.type = ButtonPress;
2108 dummy.serial = 0;
2109 dummy.send_event = 0;
2110 dummy.display = FRAME_X_DISPLAY (f);
2111 dummy.time = CurrentTime;
2112 dummy.root = FRAME_X_DISPLAY_INFO (f)->root_window;
2113 dummy.window = dummy.root;
2114 dummy.subwindow = dummy.root;
2115 dummy.x_root = x;
2116 dummy.y_root = y;
2117 dummy.x = x;
2118 dummy.y = y;
2119 dummy.state = (FRAME_X_DISPLAY_INFO (f)->grabbed >> 1) * Button1Mask;
2120 dummy.button = 0;
2121 for (i = 0; i < 5; i++)
2122 if (FRAME_X_DISPLAY_INFO (f)->grabbed & (1 << i))
2123 dummy.button = i;
2124
2125 /* Don't allow any geometry request from the user. */
2126 XtSetArg (av[ac], XtNgeometry, 0); ac++;
2127 XtSetValues (menu, av, ac);
2128
2129 /* Free the widget_value objects we used to specify the contents. */
2130 free_menubar_widget_value_tree (first_wv);
2131
2132 /* No selection has been chosen yet. */
2133 menu_item_selection = 0;
2134
2135 /* Display the menu. */
2136 lw_popup_menu (menu, &dummy);
2137 x_set_menu_resources_from_menu_face (f, menu);
2138 popup_activated_flag = 1;
2139
2140 /* Process events that apply to the menu. */
2141 popup_get_selection ((XEvent *) 0, FRAME_X_DISPLAY_INFO (f), menu_id);
2142
2143 #ifdef LESSTIF_VERSION
2144 /* Nov 1998: For an unknown reason a button grab remains active
2145 after the popup menu has gone. */
2146 XUngrabButton (XtDisplay (f->output_data.x->widget),
2147 AnyButton, AnyModifier,
2148 XtWindow (f->output_data.x->widget));
2149 XUngrabButton (XtDisplay (f->output_data.x->edit_widget),
2150 AnyButton, AnyModifier,
2151 XtWindow (f->output_data.x->edit_widget));
2152 #endif /* LESSTIF_VERSION */
2153
2154 /* fp turned off the following statement and wrote a comment
2155 that it is unnecessary--that the menu has already disappeared.
2156 Nowadays the menu disappears ok, all right, but
2157 we need to delete the widgets or multiple ones will pile up. */
2158 lw_destroy_all_widgets (menu_id);
2159
2160 /* Find the selected item, and its pane, to return
2161 the proper value. */
2162 if (menu_item_selection != 0)
2163 {
2164 Lisp_Object prefix, entry;
2165
2166 prefix = Qnil;
2167 i = 0;
2168 while (i < menu_items_used)
2169 {
2170 if (EQ (XVECTOR (menu_items)->contents[i], Qnil))
2171 {
2172 subprefix_stack[submenu_depth++] = prefix;
2173 prefix = entry;
2174 i++;
2175 }
2176 else if (EQ (XVECTOR (menu_items)->contents[i], Qlambda))
2177 {
2178 prefix = subprefix_stack[--submenu_depth];
2179 i++;
2180 }
2181 else if (EQ (XVECTOR (menu_items)->contents[i], Qt))
2182 {
2183 prefix
2184 = XVECTOR (menu_items)->contents[i + MENU_ITEMS_PANE_PREFIX];
2185 i += MENU_ITEMS_PANE_LENGTH;
2186 }
2187 /* Ignore a nil in the item list.
2188 It's meaningful only for dialog boxes. */
2189 else if (EQ (XVECTOR (menu_items)->contents[i], Qquote))
2190 i += 1;
2191 else
2192 {
2193 entry
2194 = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_VALUE];
2195 if (menu_item_selection == &XVECTOR (menu_items)->contents[i])
2196 {
2197 if (keymaps != 0)
2198 {
2199 int j;
2200
2201 entry = Fcons (entry, Qnil);
2202 if (!NILP (prefix))
2203 entry = Fcons (prefix, entry);
2204 for (j = submenu_depth - 1; j >= 0; j--)
2205 if (!NILP (subprefix_stack[j]))
2206 entry = Fcons (subprefix_stack[j], entry);
2207 }
2208 return entry;
2209 }
2210 i += MENU_ITEMS_ITEM_LENGTH;
2211 }
2212 }
2213 }
2214
2215 return Qnil;
2216 }
2217 \f
2218 static void
2219 dialog_selection_callback (widget, id, client_data)
2220 Widget widget;
2221 LWLIB_ID id;
2222 XtPointer client_data;
2223 {
2224 /* The EMACS_INT cast avoids a warning. There's no problem
2225 as long as pointers have enough bits to hold small integers. */
2226 if ((int) (EMACS_INT) client_data != -1)
2227 menu_item_selection = (Lisp_Object *) client_data;
2228 BLOCK_INPUT;
2229 lw_destroy_all_widgets (id);
2230 UNBLOCK_INPUT;
2231 popup_activated_flag = 0;
2232 }
2233
2234 static char * button_names [] = {
2235 "button1", "button2", "button3", "button4", "button5",
2236 "button6", "button7", "button8", "button9", "button10" };
2237
2238 static Lisp_Object
2239 xdialog_show (f, keymaps, title, error)
2240 FRAME_PTR f;
2241 int keymaps;
2242 Lisp_Object title;
2243 char **error;
2244 {
2245 int i, nb_buttons=0;
2246 LWLIB_ID dialog_id;
2247 Widget menu;
2248 char dialog_name[6];
2249
2250 widget_value *wv, *first_wv = 0, *prev_wv = 0;
2251
2252 /* Number of elements seen so far, before boundary. */
2253 int left_count = 0;
2254 /* 1 means we've seen the boundary between left-hand elts and right-hand. */
2255 int boundary_seen = 0;
2256
2257 *error = NULL;
2258
2259 if (menu_items_n_panes > 1)
2260 {
2261 *error = "Multiple panes in dialog box";
2262 return Qnil;
2263 }
2264
2265 /* Create a tree of widget_value objects
2266 representing the text label and buttons. */
2267 {
2268 Lisp_Object pane_name, prefix;
2269 char *pane_string;
2270 pane_name = XVECTOR (menu_items)->contents[MENU_ITEMS_PANE_NAME];
2271 prefix = XVECTOR (menu_items)->contents[MENU_ITEMS_PANE_PREFIX];
2272 pane_string = (NILP (pane_name)
2273 ? "" : (char *) XSTRING (pane_name)->data);
2274 prev_wv = xmalloc_widget_value ();
2275 prev_wv->value = pane_string;
2276 if (keymaps && !NILP (prefix))
2277 prev_wv->name++;
2278 prev_wv->enabled = 1;
2279 prev_wv->name = "message";
2280 first_wv = prev_wv;
2281
2282 /* Loop over all panes and items, filling in the tree. */
2283 i = MENU_ITEMS_PANE_LENGTH;
2284 while (i < menu_items_used)
2285 {
2286
2287 /* Create a new item within current pane. */
2288 Lisp_Object item_name, enable, descrip;
2289 item_name = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_NAME];
2290 enable = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_ENABLE];
2291 descrip
2292 = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_EQUIV_KEY];
2293
2294 if (NILP (item_name))
2295 {
2296 free_menubar_widget_value_tree (first_wv);
2297 *error = "Submenu in dialog items";
2298 return Qnil;
2299 }
2300 if (EQ (item_name, Qquote))
2301 {
2302 /* This is the boundary between left-side elts
2303 and right-side elts. Stop incrementing right_count. */
2304 boundary_seen = 1;
2305 i++;
2306 continue;
2307 }
2308 if (nb_buttons >= 9)
2309 {
2310 free_menubar_widget_value_tree (first_wv);
2311 *error = "Too many dialog items";
2312 return Qnil;
2313 }
2314
2315 wv = xmalloc_widget_value ();
2316 prev_wv->next = wv;
2317 wv->name = (char *) button_names[nb_buttons];
2318 if (!NILP (descrip))
2319 wv->key = (char *) XSTRING (descrip)->data;
2320 wv->value = (char *) XSTRING (item_name)->data;
2321 wv->call_data = (void *) &XVECTOR (menu_items)->contents[i];
2322 wv->enabled = !NILP (enable);
2323 prev_wv = wv;
2324
2325 if (! boundary_seen)
2326 left_count++;
2327
2328 nb_buttons++;
2329 i += MENU_ITEMS_ITEM_LENGTH;
2330 }
2331
2332 /* If the boundary was not specified,
2333 by default put half on the left and half on the right. */
2334 if (! boundary_seen)
2335 left_count = nb_buttons - nb_buttons / 2;
2336
2337 wv = xmalloc_widget_value ();
2338 wv->name = dialog_name;
2339
2340 /* Dialog boxes use a really stupid name encoding
2341 which specifies how many buttons to use
2342 and how many buttons are on the right.
2343 The Q means something also. */
2344 dialog_name[0] = 'Q';
2345 dialog_name[1] = '0' + nb_buttons;
2346 dialog_name[2] = 'B';
2347 dialog_name[3] = 'R';
2348 /* Number of buttons to put on the right. */
2349 dialog_name[4] = '0' + nb_buttons - left_count;
2350 dialog_name[5] = 0;
2351 wv->contents = first_wv;
2352 first_wv = wv;
2353 }
2354
2355 /* Actually create the dialog. */
2356 dialog_id = widget_id_tick++;
2357 menu = lw_create_widget (first_wv->name, "dialog", dialog_id, first_wv,
2358 f->output_data.x->widget, 1, 0,
2359 dialog_selection_callback, 0);
2360 lw_modify_all_widgets (dialog_id, first_wv->contents, True);
2361 /* Free the widget_value objects we used to specify the contents. */
2362 free_menubar_widget_value_tree (first_wv);
2363
2364 /* No selection has been chosen yet. */
2365 menu_item_selection = 0;
2366
2367 /* Display the menu. */
2368 lw_pop_up_all_widgets (dialog_id);
2369 popup_activated_flag = 1;
2370
2371 /* Process events that apply to the menu. */
2372 popup_get_selection ((XEvent *) 0, FRAME_X_DISPLAY_INFO (f), dialog_id);
2373
2374 lw_destroy_all_widgets (dialog_id);
2375
2376 /* Find the selected item, and its pane, to return
2377 the proper value. */
2378 if (menu_item_selection != 0)
2379 {
2380 Lisp_Object prefix;
2381
2382 prefix = Qnil;
2383 i = 0;
2384 while (i < menu_items_used)
2385 {
2386 Lisp_Object entry;
2387
2388 if (EQ (XVECTOR (menu_items)->contents[i], Qt))
2389 {
2390 prefix
2391 = XVECTOR (menu_items)->contents[i + MENU_ITEMS_PANE_PREFIX];
2392 i += MENU_ITEMS_PANE_LENGTH;
2393 }
2394 else
2395 {
2396 entry
2397 = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_VALUE];
2398 if (menu_item_selection == &XVECTOR (menu_items)->contents[i])
2399 {
2400 if (keymaps != 0)
2401 {
2402 entry = Fcons (entry, Qnil);
2403 if (!NILP (prefix))
2404 entry = Fcons (prefix, entry);
2405 }
2406 return entry;
2407 }
2408 i += MENU_ITEMS_ITEM_LENGTH;
2409 }
2410 }
2411 }
2412
2413 return Qnil;
2414 }
2415 #else /* not USE_X_TOOLKIT */
2416
2417 static Lisp_Object
2418 xmenu_show (f, x, y, for_click, keymaps, title, error)
2419 FRAME_PTR f;
2420 int x, y;
2421 int for_click;
2422 int keymaps;
2423 Lisp_Object title;
2424 char **error;
2425 {
2426 Window root;
2427 XMenu *menu;
2428 int pane, selidx, lpane, status;
2429 Lisp_Object entry, pane_prefix;
2430 char *datap;
2431 int ulx, uly, width, height;
2432 int dispwidth, dispheight;
2433 int i, j;
2434 int maxwidth;
2435 int dummy_int;
2436 unsigned int dummy_uint;
2437
2438 *error = 0;
2439 if (menu_items_n_panes == 0)
2440 return Qnil;
2441
2442 if (menu_items_used <= MENU_ITEMS_PANE_LENGTH)
2443 {
2444 *error = "Empty menu";
2445 return Qnil;
2446 }
2447
2448 /* Figure out which root window F is on. */
2449 XGetGeometry (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), &root,
2450 &dummy_int, &dummy_int, &dummy_uint, &dummy_uint,
2451 &dummy_uint, &dummy_uint);
2452
2453 /* Make the menu on that window. */
2454 menu = XMenuCreate (FRAME_X_DISPLAY (f), root, "emacs");
2455 if (menu == NULL)
2456 {
2457 *error = "Can't create menu";
2458 return Qnil;
2459 }
2460
2461 #ifdef HAVE_X_WINDOWS
2462 /* Adjust coordinates to relative to the outer (window manager) window. */
2463 {
2464 Window child;
2465 int win_x = 0, win_y = 0;
2466
2467 /* Find the position of the outside upper-left corner of
2468 the inner window, with respect to the outer window. */
2469 if (f->output_data.x->parent_desc != FRAME_X_DISPLAY_INFO (f)->root_window)
2470 {
2471 BLOCK_INPUT;
2472 XTranslateCoordinates (FRAME_X_DISPLAY (f),
2473
2474 /* From-window, to-window. */
2475 f->output_data.x->window_desc,
2476 f->output_data.x->parent_desc,
2477
2478 /* From-position, to-position. */
2479 0, 0, &win_x, &win_y,
2480
2481 /* Child of window. */
2482 &child);
2483 UNBLOCK_INPUT;
2484 x += win_x;
2485 y += win_y;
2486 }
2487 }
2488 #endif /* HAVE_X_WINDOWS */
2489
2490 /* Adjust coordinates to be root-window-relative. */
2491 x += f->output_data.x->left_pos;
2492 y += f->output_data.x->top_pos;
2493
2494 /* Create all the necessary panes and their items. */
2495 i = 0;
2496 while (i < menu_items_used)
2497 {
2498 if (EQ (XVECTOR (menu_items)->contents[i], Qt))
2499 {
2500 /* Create a new pane. */
2501 Lisp_Object pane_name, prefix;
2502 char *pane_string;
2503
2504 pane_name = XVECTOR (menu_items)->contents[i + MENU_ITEMS_PANE_NAME];
2505 prefix = XVECTOR (menu_items)->contents[i + MENU_ITEMS_PANE_PREFIX];
2506 pane_string = (NILP (pane_name)
2507 ? "" : (char *) XSTRING (pane_name)->data);
2508 if (keymaps && !NILP (prefix))
2509 pane_string++;
2510
2511 lpane = XMenuAddPane (FRAME_X_DISPLAY (f), menu, pane_string, TRUE);
2512 if (lpane == XM_FAILURE)
2513 {
2514 XMenuDestroy (FRAME_X_DISPLAY (f), menu);
2515 *error = "Can't create pane";
2516 return Qnil;
2517 }
2518 i += MENU_ITEMS_PANE_LENGTH;
2519
2520 /* Find the width of the widest item in this pane. */
2521 maxwidth = 0;
2522 j = i;
2523 while (j < menu_items_used)
2524 {
2525 Lisp_Object item;
2526 item = XVECTOR (menu_items)->contents[j];
2527 if (EQ (item, Qt))
2528 break;
2529 if (NILP (item))
2530 {
2531 j++;
2532 continue;
2533 }
2534 width = STRING_BYTES (XSTRING (item));
2535 if (width > maxwidth)
2536 maxwidth = width;
2537
2538 j += MENU_ITEMS_ITEM_LENGTH;
2539 }
2540 }
2541 /* Ignore a nil in the item list.
2542 It's meaningful only for dialog boxes. */
2543 else if (EQ (XVECTOR (menu_items)->contents[i], Qquote))
2544 i += 1;
2545 else
2546 {
2547 /* Create a new item within current pane. */
2548 Lisp_Object item_name, enable, descrip;
2549 unsigned char *item_data;
2550
2551 item_name = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_NAME];
2552 enable = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_ENABLE];
2553 descrip
2554 = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_EQUIV_KEY];
2555 if (!NILP (descrip))
2556 {
2557 int gap = maxwidth - STRING_BYTES (XSTRING (item_name));
2558 #ifdef C_ALLOCA
2559 Lisp_Object spacer;
2560 spacer = Fmake_string (make_number (gap), make_number (' '));
2561 item_name = concat2 (item_name, spacer);
2562 item_name = concat2 (item_name, descrip);
2563 item_data = XSTRING (item_name)->data;
2564 #else
2565 /* if alloca is fast, use that to make the space,
2566 to reduce gc needs. */
2567 item_data
2568 = (unsigned char *) alloca (maxwidth
2569 + STRING_BYTES (XSTRING (descrip)) + 1);
2570 bcopy (XSTRING (item_name)->data, item_data,
2571 STRING_BYTES (XSTRING (item_name)));
2572 for (j = XSTRING (item_name)->size; j < maxwidth; j++)
2573 item_data[j] = ' ';
2574 bcopy (XSTRING (descrip)->data, item_data + j,
2575 STRING_BYTES (XSTRING (descrip)));
2576 item_data[j + STRING_BYTES (XSTRING (descrip))] = 0;
2577 #endif
2578 }
2579 else
2580 item_data = XSTRING (item_name)->data;
2581
2582 if (XMenuAddSelection (FRAME_X_DISPLAY (f),
2583 menu, lpane, 0, item_data,
2584 !NILP (enable))
2585 == XM_FAILURE)
2586 {
2587 XMenuDestroy (FRAME_X_DISPLAY (f), menu);
2588 *error = "Can't add selection to menu";
2589 return Qnil;
2590 }
2591 i += MENU_ITEMS_ITEM_LENGTH;
2592 }
2593 }
2594
2595 /* All set and ready to fly. */
2596 XMenuRecompute (FRAME_X_DISPLAY (f), menu);
2597 dispwidth = DisplayWidth (FRAME_X_DISPLAY (f),
2598 XScreenNumberOfScreen (FRAME_X_SCREEN (f)));
2599 dispheight = DisplayHeight (FRAME_X_DISPLAY (f),
2600 XScreenNumberOfScreen (FRAME_X_SCREEN (f)));
2601 x = min (x, dispwidth);
2602 y = min (y, dispheight);
2603 x = max (x, 1);
2604 y = max (y, 1);
2605 XMenuLocate (FRAME_X_DISPLAY (f), menu, 0, 0, x, y,
2606 &ulx, &uly, &width, &height);
2607 if (ulx+width > dispwidth)
2608 {
2609 x -= (ulx + width) - dispwidth;
2610 ulx = dispwidth - width;
2611 }
2612 if (uly+height > dispheight)
2613 {
2614 y -= (uly + height) - dispheight;
2615 uly = dispheight - height;
2616 }
2617 if (ulx < 0) x -= ulx;
2618 if (uly < 0) y -= uly;
2619
2620 XMenuSetAEQ (menu, TRUE);
2621 XMenuSetFreeze (menu, TRUE);
2622 pane = selidx = 0;
2623
2624 status = XMenuActivate (FRAME_X_DISPLAY (f), menu, &pane, &selidx,
2625 x, y, ButtonReleaseMask, &datap);
2626
2627
2628 #ifdef HAVE_X_WINDOWS
2629 /* Assume the mouse has moved out of the X window.
2630 If it has actually moved in, we will get an EnterNotify. */
2631 x_mouse_leave (FRAME_X_DISPLAY_INFO (f));
2632 #endif
2633
2634 switch (status)
2635 {
2636 case XM_SUCCESS:
2637 #ifdef XDEBUG
2638 fprintf (stderr, "pane= %d line = %d\n", panes, selidx);
2639 #endif
2640
2641 /* Find the item number SELIDX in pane number PANE. */
2642 i = 0;
2643 while (i < menu_items_used)
2644 {
2645 if (EQ (XVECTOR (menu_items)->contents[i], Qt))
2646 {
2647 if (pane == 0)
2648 pane_prefix
2649 = XVECTOR (menu_items)->contents[i + MENU_ITEMS_PANE_PREFIX];
2650 pane--;
2651 i += MENU_ITEMS_PANE_LENGTH;
2652 }
2653 else
2654 {
2655 if (pane == -1)
2656 {
2657 if (selidx == 0)
2658 {
2659 entry
2660 = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_VALUE];
2661 if (keymaps != 0)
2662 {
2663 entry = Fcons (entry, Qnil);
2664 if (!NILP (pane_prefix))
2665 entry = Fcons (pane_prefix, entry);
2666 }
2667 break;
2668 }
2669 selidx--;
2670 }
2671 i += MENU_ITEMS_ITEM_LENGTH;
2672 }
2673 }
2674 break;
2675
2676 case XM_FAILURE:
2677 *error = "Can't activate menu";
2678 case XM_IA_SELECT:
2679 case XM_NO_SELECT:
2680 entry = Qnil;
2681 break;
2682 }
2683 XMenuDestroy (FRAME_X_DISPLAY (f), menu);
2684
2685 #ifdef HAVE_X_WINDOWS
2686 /* State that no mouse buttons are now held.
2687 (The oldXMenu code doesn't track this info for us.)
2688 That is not necessarily true, but the fiction leads to reasonable
2689 results, and it is a pain to ask which are actually held now. */
2690 FRAME_X_DISPLAY_INFO (f)->grabbed = 0;
2691 #endif
2692
2693 return entry;
2694 }
2695
2696 #endif /* not USE_X_TOOLKIT */
2697
2698 #endif /* HAVE_MENUS */
2699 \f
2700 void
2701 syms_of_xmenu ()
2702 {
2703 staticpro (&menu_items);
2704 menu_items = Qnil;
2705
2706 Qdebug_on_next_call = intern ("debug-on-next-call");
2707 staticpro (&Qdebug_on_next_call);
2708
2709 DEFVAR_LISP ("menu-updating-frame", &Vmenu_updating_frame,
2710 "Frame for which we are updating a menu.\n\
2711 The enable predicate for a menu command should check this variable.");
2712 Vmenu_updating_frame = Qnil;
2713
2714 #ifdef USE_X_TOOLKIT
2715 widget_id_tick = (1<<16);
2716 next_menubar_widget_id = 1;
2717 #endif
2718
2719 defsubr (&Sx_popup_menu);
2720 #ifdef HAVE_MENUS
2721 defsubr (&Sx_popup_dialog);
2722 #endif
2723 }