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