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