Merged from emacs@sv.gnu.org
[emacs.git] / src / xmenu.c
blob6e5ec6c505810489cc040aa918bba1eafdf296ce
1 /* X Communication module for terminals which understand the X protocol.
2 Copyright (C) 1986, 1988, 1993, 1994, 1996, 1999, 2000, 2001, 2002, 2003,
3 2004, 2005, 2006 Free Software Foundation, Inc.
5 This file is part of GNU Emacs.
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.
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.
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., 51 Franklin Street, Fifth Floor,
20 Boston, MA 02110-1301, USA. */
22 /* X pop-up deck-of-cards menu facility for GNU Emacs.
24 * Written by Jon Arnold and Roman Budzianowski
25 * Mods and rewrite by Robert Krawitz
29 /* Modified by Fred Pierresteguy on December 93
30 to make the popup menus and menubar use the Xt. */
32 /* Rewritten for clarity and GC protection by rms in Feb 94. */
34 #include <config.h>
36 #if 0 /* Why was this included? And without syssignal.h? */
37 /* On 4.3 this loses if it comes after xterm.h. */
38 #include <signal.h>
39 #endif
41 #include <stdio.h>
43 #include "lisp.h"
44 #include "keyboard.h"
45 #include "keymap.h"
46 #include "frame.h"
47 #include "termhooks.h"
48 #include "window.h"
49 #include "blockinput.h"
50 #include "buffer.h"
51 #include "charset.h"
52 #include "coding.h"
53 #include "sysselect.h"
55 #ifdef MSDOS
56 #include "msdos.h"
57 #endif
59 #ifdef HAVE_X_WINDOWS
60 /* This may include sys/types.h, and that somehow loses
61 if this is not done before the other system files. */
62 #include "xterm.h"
63 #endif
65 /* Load sys/types.h if not already loaded.
66 In some systems loading it twice is suicidal. */
67 #ifndef makedev
68 #include <sys/types.h>
69 #endif
71 #include "dispextern.h"
73 #ifdef HAVE_X_WINDOWS
74 /* Defining HAVE_MULTILINGUAL_MENU would mean that the toolkit menu
75 code accepts the Emacs internal encoding. */
76 #undef HAVE_MULTILINGUAL_MENU
77 #ifdef USE_X_TOOLKIT
78 #include "widget.h"
79 #include <X11/Xlib.h>
80 #include <X11/IntrinsicP.h>
81 #include <X11/CoreP.h>
82 #include <X11/StringDefs.h>
83 #include <X11/Shell.h>
84 #ifdef USE_LUCID
85 #include <X11/Xaw/Paned.h>
86 #endif /* USE_LUCID */
87 #include "../lwlib/lwlib.h"
88 #else /* not USE_X_TOOLKIT */
89 #ifndef USE_GTK
90 #include "../oldXMenu/XMenu.h"
91 #endif
92 #endif /* not USE_X_TOOLKIT */
93 #endif /* HAVE_X_WINDOWS */
95 #ifndef TRUE
96 #define TRUE 1
97 #define FALSE 0
98 #endif /* no TRUE */
100 Lisp_Object Vmenu_updating_frame;
102 Lisp_Object Qdebug_on_next_call;
104 extern Lisp_Object Qmenu_bar;
106 extern Lisp_Object QCtoggle, QCradio;
108 extern Lisp_Object Voverriding_local_map;
109 extern Lisp_Object Voverriding_local_map_menu_flag;
111 extern Lisp_Object Qoverriding_local_map, Qoverriding_terminal_local_map;
113 extern Lisp_Object Qmenu_bar_update_hook;
115 #ifdef USE_X_TOOLKIT
116 extern void set_frame_menubar P_ ((FRAME_PTR, int, int));
117 extern XtAppContext Xt_app_con;
119 static Lisp_Object xdialog_show P_ ((FRAME_PTR, int, Lisp_Object, Lisp_Object,
120 char **));
121 static void popup_get_selection P_ ((XEvent *, struct x_display_info *,
122 LWLIB_ID, int));
124 /* Define HAVE_BOXES if menus can handle radio and toggle buttons. */
126 #define HAVE_BOXES 1
127 #endif /* USE_X_TOOLKIT */
129 #ifdef USE_GTK
130 #include "gtkutil.h"
131 #define HAVE_BOXES 1
132 extern void set_frame_menubar P_ ((FRAME_PTR, int, int));
133 static Lisp_Object xdialog_show P_ ((FRAME_PTR, int, Lisp_Object, Lisp_Object,
134 char **));
135 #endif
137 /* This is how to deal with multibyte text if HAVE_MULTILINGUAL_MENU
138 isn't defined. The use of HAVE_MULTILINGUAL_MENU could probably be
139 confined to an extended version of this with sections of code below
140 using it unconditionally. */
141 #ifdef USE_GTK
142 /* gtk just uses utf-8. */
143 # define ENCODE_MENU_STRING(str) ENCODE_UTF_8 (str)
144 #elif defined HAVE_X_I18N
145 # define ENCODE_MENU_STRING(str) ENCODE_SYSTEM (str)
146 #else
147 # define ENCODE_MENU_STRING(str) string_make_unibyte (str)
148 #endif
150 static void push_menu_item P_ ((Lisp_Object, Lisp_Object, Lisp_Object,
151 Lisp_Object, Lisp_Object, Lisp_Object,
152 Lisp_Object, Lisp_Object));
153 static int update_frame_menubar P_ ((struct frame *));
154 static Lisp_Object xmenu_show P_ ((struct frame *, int, int, int, int,
155 Lisp_Object, char **));
156 static void keymap_panes P_ ((Lisp_Object *, int, int));
157 static void single_keymap_panes P_ ((Lisp_Object, Lisp_Object, Lisp_Object,
158 int, int));
159 static void list_of_panes P_ ((Lisp_Object));
160 static void list_of_items P_ ((Lisp_Object));
163 /* This holds a Lisp vector that holds the results of decoding
164 the keymaps or alist-of-alists that specify a menu.
166 It describes the panes and items within the panes.
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.
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.
179 A single vector slot containing quote indicates that the
180 following items should appear on the right of a dialog box.
182 Using a Lisp vector to hold this information while we decode it
183 takes care of protecting all the data from GC. */
185 #define MENU_ITEMS_PANE_NAME 1
186 #define MENU_ITEMS_PANE_PREFIX 2
187 #define MENU_ITEMS_PANE_LENGTH 3
189 enum menu_item_idx
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
202 static Lisp_Object menu_items;
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;
208 /* Number of slots currently allocated in menu_items. */
209 static int menu_items_allocated;
211 /* This is the index in menu_items of the first empty slot. */
212 static int menu_items_used;
214 /* The number of panes currently recorded in menu_items,
215 excluding those within submenus. */
216 static int menu_items_n_panes;
218 /* Current depth within submenus. */
219 static int menu_items_submenu_depth;
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;
225 static int next_menubar_widget_id;
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.
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>. */
234 int pending_menu_activation;
236 #ifdef USE_X_TOOLKIT
238 /* Return the frame whose ->output_data.x->id equals ID, or 0 if none. */
240 static struct frame *
241 menubar_id_to_frame (id)
242 LWLIB_ID id;
244 Lisp_Object tail, frame;
245 FRAME_PTR f;
247 for (tail = Vframe_list; GC_CONSP (tail); tail = XCDR (tail))
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;
258 return 0;
261 #endif
263 /* Initialize the menu_items structure if we haven't already done so.
264 Also mark it as currently empty. */
266 static void
267 init_menu_items ()
269 if (!NILP (menu_items_inuse))
270 error ("Trying to use a menu from within a menu-entry");
272 if (NILP (menu_items))
274 menu_items_allocated = 60;
275 menu_items = Fmake_vector (make_number (menu_items_allocated), Qnil);
278 menu_items_inuse = Qt;
279 menu_items_used = 0;
280 menu_items_n_panes = 0;
281 menu_items_submenu_depth = 0;
284 /* Call at the end of generating the data in menu_items. */
286 static void
287 finish_menu_items ()
291 static Lisp_Object
292 unuse_menu_items (dummy)
293 Lisp_Object dummy;
295 return menu_items_inuse = Qnil;
298 /* Call when finished using the data for the current menu
299 in menu_items. */
301 static void
302 discard_menu_items ()
304 /* Free the structure if it is especially large.
305 Otherwise, hold on to it, to save time. */
306 if (menu_items_allocated > 200)
308 menu_items = Qnil;
309 menu_items_allocated = 0;
311 xassert (NILP (menu_items_inuse));
314 /* This undoes save_menu_items, and it is called by the specpdl unwind
315 mechanism. */
317 static Lisp_Object
318 restore_menu_items (saved)
319 Lisp_Object saved;
321 menu_items = XCAR (saved);
322 menu_items_inuse = (! NILP (menu_items) ? Qt : Qnil);
323 menu_items_allocated = (VECTORP (menu_items) ? ASIZE (menu_items) : 0);
324 saved = XCDR (saved);
325 menu_items_used = XINT (XCAR (saved));
326 saved = XCDR (saved);
327 menu_items_n_panes = XINT (XCAR (saved));
328 saved = XCDR (saved);
329 menu_items_submenu_depth = XINT (XCAR (saved));
332 /* Push the whole state of menu_items processing onto the specpdl.
333 It will be restored when the specpdl is unwound. */
335 static void
336 save_menu_items ()
338 Lisp_Object saved = list4 (!NILP (menu_items_inuse) ? menu_items : Qnil,
339 make_number (menu_items_used),
340 make_number (menu_items_n_panes),
341 make_number (menu_items_submenu_depth));
342 record_unwind_protect (restore_menu_items, saved);
343 menu_items_inuse = Qnil;
344 menu_items = Qnil;
347 /* Make the menu_items vector twice as large. */
349 static void
350 grow_menu_items ()
352 Lisp_Object old;
353 int old_size = menu_items_allocated;
354 old = menu_items;
356 menu_items_allocated *= 2;
358 menu_items = Fmake_vector (make_number (menu_items_allocated), Qnil);
359 bcopy (XVECTOR (old)->contents, XVECTOR (menu_items)->contents,
360 old_size * sizeof (Lisp_Object));
363 /* Begin a submenu. */
365 static void
366 push_submenu_start ()
368 if (menu_items_used + 1 > menu_items_allocated)
369 grow_menu_items ();
371 XVECTOR (menu_items)->contents[menu_items_used++] = Qnil;
372 menu_items_submenu_depth++;
375 /* End a submenu. */
377 static void
378 push_submenu_end ()
380 if (menu_items_used + 1 > menu_items_allocated)
381 grow_menu_items ();
383 XVECTOR (menu_items)->contents[menu_items_used++] = Qlambda;
384 menu_items_submenu_depth--;
387 /* Indicate boundary between left and right. */
389 static void
390 push_left_right_boundary ()
392 if (menu_items_used + 1 > menu_items_allocated)
393 grow_menu_items ();
395 XVECTOR (menu_items)->contents[menu_items_used++] = Qquote;
398 /* Start a new menu pane in menu_items.
399 NAME is the pane name. PREFIX_VEC is a prefix key for this pane. */
401 static void
402 push_menu_pane (name, prefix_vec)
403 Lisp_Object name, prefix_vec;
405 if (menu_items_used + MENU_ITEMS_PANE_LENGTH > menu_items_allocated)
406 grow_menu_items ();
408 if (menu_items_submenu_depth == 0)
409 menu_items_n_panes++;
410 XVECTOR (menu_items)->contents[menu_items_used++] = Qt;
411 XVECTOR (menu_items)->contents[menu_items_used++] = name;
412 XVECTOR (menu_items)->contents[menu_items_used++] = prefix_vec;
415 /* Push one menu item into the current pane. NAME is the string to
416 display. ENABLE if non-nil means this item can be selected. KEY
417 is the key generated by choosing this item, or nil if this item
418 doesn't really have a definition. DEF is the definition of this
419 item. EQUIV is the textual description of the keyboard equivalent
420 for this item (or nil if none). TYPE is the type of this menu
421 item, one of nil, `toggle' or `radio'. */
423 static void
424 push_menu_item (name, enable, key, def, equiv, type, selected, help)
425 Lisp_Object name, enable, key, def, equiv, type, selected, help;
427 if (menu_items_used + MENU_ITEMS_ITEM_LENGTH > menu_items_allocated)
428 grow_menu_items ();
430 XVECTOR (menu_items)->contents[menu_items_used++] = name;
431 XVECTOR (menu_items)->contents[menu_items_used++] = enable;
432 XVECTOR (menu_items)->contents[menu_items_used++] = key;
433 XVECTOR (menu_items)->contents[menu_items_used++] = equiv;
434 XVECTOR (menu_items)->contents[menu_items_used++] = def;
435 XVECTOR (menu_items)->contents[menu_items_used++] = type;
436 XVECTOR (menu_items)->contents[menu_items_used++] = selected;
437 XVECTOR (menu_items)->contents[menu_items_used++] = help;
440 /* Look through KEYMAPS, a vector of keymaps that is NMAPS long,
441 and generate menu panes for them in menu_items.
442 If NOTREAL is nonzero,
443 don't bother really computing whether an item is enabled. */
445 static void
446 keymap_panes (keymaps, nmaps, notreal)
447 Lisp_Object *keymaps;
448 int nmaps;
449 int notreal;
451 int mapno;
453 init_menu_items ();
455 /* Loop over the given keymaps, making a pane for each map.
456 But don't make a pane that is empty--ignore that map instead.
457 P is the number of panes we have made so far. */
458 for (mapno = 0; mapno < nmaps; mapno++)
459 single_keymap_panes (keymaps[mapno],
460 Fkeymap_prompt (keymaps[mapno]), Qnil, notreal, 10);
462 finish_menu_items ();
465 /* Args passed between single_keymap_panes and single_menu_item. */
466 struct skp
468 Lisp_Object pending_maps;
469 int maxdepth, notreal;
470 int notbuttons;
473 static void single_menu_item P_ ((Lisp_Object, Lisp_Object, Lisp_Object,
474 void *));
476 /* This is a recursive subroutine of keymap_panes.
477 It handles one keymap, KEYMAP.
478 The other arguments are passed along
479 or point to local variables of the previous function.
480 If NOTREAL is nonzero, only check for equivalent key bindings, don't
481 evaluate expressions in menu items and don't make any menu.
483 If we encounter submenus deeper than MAXDEPTH levels, ignore them. */
485 static void
486 single_keymap_panes (keymap, pane_name, prefix, notreal, maxdepth)
487 Lisp_Object keymap;
488 Lisp_Object pane_name;
489 Lisp_Object prefix;
490 int notreal;
491 int maxdepth;
493 struct skp skp;
494 struct gcpro gcpro1;
496 skp.pending_maps = Qnil;
497 skp.maxdepth = maxdepth;
498 skp.notreal = notreal;
499 skp.notbuttons = 0;
501 if (maxdepth <= 0)
502 return;
504 push_menu_pane (pane_name, prefix);
506 #ifndef HAVE_BOXES
507 /* Remember index for first item in this pane so we can go back and
508 add a prefix when (if) we see the first button. After that, notbuttons
509 is set to 0, to mark that we have seen a button and all non button
510 items need a prefix. */
511 skp.notbuttons = menu_items_used;
512 #endif
514 GCPRO1 (skp.pending_maps);
515 map_keymap (keymap, single_menu_item, Qnil, &skp, 1);
516 UNGCPRO;
518 /* Process now any submenus which want to be panes at this level. */
519 while (CONSP (skp.pending_maps))
521 Lisp_Object elt, eltcdr, string;
522 elt = XCAR (skp.pending_maps);
523 eltcdr = XCDR (elt);
524 string = XCAR (eltcdr);
525 /* We no longer discard the @ from the beginning of the string here.
526 Instead, we do this in xmenu_show. */
527 single_keymap_panes (Fcar (elt), string,
528 XCDR (eltcdr), notreal, maxdepth - 1);
529 skp.pending_maps = XCDR (skp.pending_maps);
533 /* This is a subroutine of single_keymap_panes that handles one
534 keymap entry.
535 KEY is a key in a keymap and ITEM is its binding.
536 SKP->PENDING_MAPS_PTR is a list of keymaps waiting to be made into
537 separate panes.
538 If SKP->NOTREAL is nonzero, only check for equivalent key bindings, don't
539 evaluate expressions in menu items and don't make any menu.
540 If we encounter submenus deeper than SKP->MAXDEPTH levels, ignore them.
541 SKP->NOTBUTTONS is only used when simulating toggle boxes and radio
542 buttons. It keeps track of if we have seen a button in this menu or
543 not. */
545 static void
546 single_menu_item (key, item, dummy, skp_v)
547 Lisp_Object key, item, dummy;
548 void *skp_v;
550 Lisp_Object map, item_string, enabled;
551 struct gcpro gcpro1, gcpro2;
552 int res;
553 struct skp *skp = skp_v;
555 /* Parse the menu item and leave the result in item_properties. */
556 GCPRO2 (key, item);
557 res = parse_menu_item (item, skp->notreal, 0);
558 UNGCPRO;
559 if (!res)
560 return; /* Not a menu item. */
562 map = XVECTOR (item_properties)->contents[ITEM_PROPERTY_MAP];
564 if (skp->notreal)
566 /* We don't want to make a menu, just traverse the keymaps to
567 precompute equivalent key bindings. */
568 if (!NILP (map))
569 single_keymap_panes (map, Qnil, key, 1, skp->maxdepth - 1);
570 return;
573 enabled = XVECTOR (item_properties)->contents[ITEM_PROPERTY_ENABLE];
574 item_string = XVECTOR (item_properties)->contents[ITEM_PROPERTY_NAME];
576 if (!NILP (map) && SREF (item_string, 0) == '@')
578 if (!NILP (enabled))
579 /* An enabled separate pane. Remember this to handle it later. */
580 skp->pending_maps = Fcons (Fcons (map, Fcons (item_string, key)),
581 skp->pending_maps);
582 return;
585 #ifndef HAVE_BOXES
586 /* Simulate radio buttons and toggle boxes by putting a prefix in
587 front of them. */
589 Lisp_Object prefix = Qnil;
590 Lisp_Object type = XVECTOR (item_properties)->contents[ITEM_PROPERTY_TYPE];
591 if (!NILP (type))
593 Lisp_Object selected
594 = XVECTOR (item_properties)->contents[ITEM_PROPERTY_SELECTED];
596 if (skp->notbuttons)
597 /* The first button. Line up previous items in this menu. */
599 int index = skp->notbuttons; /* Index for first item this menu. */
600 int submenu = 0;
601 Lisp_Object tem;
602 while (index < menu_items_used)
605 = XVECTOR (menu_items)->contents[index + MENU_ITEMS_ITEM_NAME];
606 if (NILP (tem))
608 index++;
609 submenu++; /* Skip sub menu. */
611 else if (EQ (tem, Qlambda))
613 index++;
614 submenu--; /* End sub menu. */
616 else if (EQ (tem, Qt))
617 index += 3; /* Skip new pane marker. */
618 else if (EQ (tem, Qquote))
619 index++; /* Skip a left, right divider. */
620 else
622 if (!submenu && SREF (tem, 0) != '\0'
623 && SREF (tem, 0) != '-')
624 XVECTOR (menu_items)->contents[index + MENU_ITEMS_ITEM_NAME]
625 = concat2 (build_string (" "), tem);
626 index += MENU_ITEMS_ITEM_LENGTH;
629 skp->notbuttons = 0;
632 /* Calculate prefix, if any, for this item. */
633 if (EQ (type, QCtoggle))
634 prefix = build_string (NILP (selected) ? "[ ] " : "[X] ");
635 else if (EQ (type, QCradio))
636 prefix = build_string (NILP (selected) ? "( ) " : "(*) ");
638 /* Not a button. If we have earlier buttons, then we need a prefix. */
639 else if (!skp->notbuttons && SREF (item_string, 0) != '\0'
640 && SREF (item_string, 0) != '-')
641 prefix = build_string (" ");
643 if (!NILP (prefix))
644 item_string = concat2 (prefix, item_string);
646 #endif /* not HAVE_BOXES */
648 #if ! defined (USE_X_TOOLKIT) && ! defined (USE_GTK)
649 if (!NILP (map))
650 /* Indicate visually that this is a submenu. */
651 item_string = concat2 (item_string, build_string (" >"));
652 #endif
654 push_menu_item (item_string, enabled, key,
655 XVECTOR (item_properties)->contents[ITEM_PROPERTY_DEF],
656 XVECTOR (item_properties)->contents[ITEM_PROPERTY_KEYEQ],
657 XVECTOR (item_properties)->contents[ITEM_PROPERTY_TYPE],
658 XVECTOR (item_properties)->contents[ITEM_PROPERTY_SELECTED],
659 XVECTOR (item_properties)->contents[ITEM_PROPERTY_HELP]);
661 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
662 /* Display a submenu using the toolkit. */
663 if (! (NILP (map) || NILP (enabled)))
665 push_submenu_start ();
666 single_keymap_panes (map, Qnil, key, 0, skp->maxdepth - 1);
667 push_submenu_end ();
669 #endif
672 /* Push all the panes and items of a menu described by the
673 alist-of-alists MENU.
674 This handles old-fashioned calls to x-popup-menu. */
676 static void
677 list_of_panes (menu)
678 Lisp_Object menu;
680 Lisp_Object tail;
682 init_menu_items ();
684 for (tail = menu; CONSP (tail); tail = XCDR (tail))
686 Lisp_Object elt, pane_name, pane_data;
687 elt = XCAR (tail);
688 pane_name = Fcar (elt);
689 CHECK_STRING (pane_name);
690 push_menu_pane (ENCODE_MENU_STRING (pane_name), Qnil);
691 pane_data = Fcdr (elt);
692 CHECK_CONS (pane_data);
693 list_of_items (pane_data);
696 finish_menu_items ();
699 /* Push the items in a single pane defined by the alist PANE. */
701 static void
702 list_of_items (pane)
703 Lisp_Object pane;
705 Lisp_Object tail, item, item1;
707 for (tail = pane; CONSP (tail); tail = XCDR (tail))
709 item = XCAR (tail);
710 if (STRINGP (item))
711 push_menu_item (ENCODE_MENU_STRING (item), Qnil, Qnil, Qt,
712 Qnil, Qnil, Qnil, Qnil);
713 else if (CONSP (item))
715 item1 = XCAR (item);
716 CHECK_STRING (item1);
717 push_menu_item (ENCODE_MENU_STRING (item1), Qt, XCDR (item),
718 Qt, Qnil, Qnil, Qnil, Qnil);
720 else
721 push_left_right_boundary ();
726 #ifdef HAVE_X_WINDOWS
727 /* Return the mouse position in *X and *Y. The coordinates are window
728 relative for the edit window in frame F.
729 This is for Fx_popup_menu. The mouse_position_hook can not
730 be used for X, as it returns window relative coordinates
731 for the window where the mouse is in. This could be the menu bar,
732 the scroll bar or the edit window. Fx_popup_menu needs to be
733 sure it is the edit window. */
734 static void
735 mouse_position_for_popup (f, x, y)
736 FRAME_PTR f;
737 int *x;
738 int *y;
740 Window root, dummy_window;
741 int dummy;
743 if (! FRAME_X_P (f))
744 abort ();
746 BLOCK_INPUT;
748 XQueryPointer (FRAME_X_DISPLAY (f),
749 DefaultRootWindow (FRAME_X_DISPLAY (f)),
751 /* The root window which contains the pointer. */
752 &root,
754 /* Window pointer is on, not used */
755 &dummy_window,
757 /* The position on that root window. */
758 x, y,
760 /* x/y in dummy_window coordinates, not used. */
761 &dummy, &dummy,
763 /* Modifier keys and pointer buttons, about which
764 we don't care. */
765 (unsigned int *) &dummy);
767 UNBLOCK_INPUT;
769 /* xmenu_show expects window coordinates, not root window
770 coordinates. Translate. */
771 *x -= f->left_pos + FRAME_OUTER_TO_INNER_DIFF_X (f);
772 *y -= f->top_pos + FRAME_OUTER_TO_INNER_DIFF_Y (f);
775 #endif /* HAVE_X_WINDOWS */
777 DEFUN ("x-popup-menu", Fx_popup_menu, Sx_popup_menu, 2, 2, 0,
778 doc: /* Pop up a deck-of-cards menu and return user's selection.
779 POSITION is a position specification. This is either a mouse button event
780 or a list ((XOFFSET YOFFSET) WINDOW)
781 where XOFFSET and YOFFSET are positions in pixels from the top left
782 corner of WINDOW. (WINDOW may be a window or a frame object.)
783 This controls the position of the top left of the menu as a whole.
784 If POSITION is t, it means to use the current mouse position.
786 MENU is a specifier for a menu. For the simplest case, MENU is a keymap.
787 The menu items come from key bindings that have a menu string as well as
788 a definition; actually, the "definition" in such a key binding looks like
789 \(STRING . REAL-DEFINITION). To give the menu a title, put a string into
790 the keymap as a top-level element.
792 If REAL-DEFINITION is nil, that puts a nonselectable string in the menu.
793 Otherwise, REAL-DEFINITION should be a valid key binding definition.
795 You can also use a list of keymaps as MENU.
796 Then each keymap makes a separate pane.
798 When MENU is a keymap or a list of keymaps, the return value is the
799 list of events corresponding to the user's choice. Note that
800 `x-popup-menu' does not actually execute the command bound to that
801 sequence of events.
803 Alternatively, you can specify a menu of multiple panes
804 with a list of the form (TITLE PANE1 PANE2...),
805 where each pane is a list of form (TITLE ITEM1 ITEM2...).
806 Each ITEM is normally a cons cell (STRING . VALUE);
807 but a string can appear as an item--that makes a nonselectable line
808 in the menu.
809 With this form of menu, the return value is VALUE from the chosen item.
811 If POSITION is nil, don't display the menu at all, just precalculate the
812 cached information about equivalent key sequences.
814 If the user gets rid of the menu without making a valid choice, for
815 instance by clicking the mouse away from a valid choice or by typing
816 keyboard input, then this normally results in a quit and
817 `x-popup-menu' does not return. But if POSITION is a mouse button
818 event (indicating that the user invoked the menu with the mouse) then
819 no quit occurs and `x-popup-menu' returns nil. */)
820 (position, menu)
821 Lisp_Object position, menu;
823 Lisp_Object keymap, tem;
824 int xpos = 0, ypos = 0;
825 Lisp_Object title;
826 char *error_name = NULL;
827 Lisp_Object selection;
828 FRAME_PTR f = NULL;
829 Lisp_Object x, y, window;
830 int keymaps = 0;
831 int for_click = 0;
832 int specpdl_count = SPECPDL_INDEX ();
833 struct gcpro gcpro1;
835 #ifdef HAVE_MENUS
836 if (! NILP (position))
838 int get_current_pos_p = 0;
839 check_x ();
841 /* Decode the first argument: find the window and the coordinates. */
842 if (EQ (position, Qt)
843 || (CONSP (position) && (EQ (XCAR (position), Qmenu_bar)
844 || EQ (XCAR (position), Qtool_bar))))
846 get_current_pos_p = 1;
848 else
850 tem = Fcar (position);
851 if (CONSP (tem))
853 window = Fcar (Fcdr (position));
854 x = XCAR (tem);
855 y = Fcar (XCDR (tem));
857 else
859 for_click = 1;
860 tem = Fcar (Fcdr (position)); /* EVENT_START (position) */
861 window = Fcar (tem); /* POSN_WINDOW (tem) */
862 tem = Fcar (Fcdr (Fcdr (tem))); /* POSN_WINDOW_POSN (tem) */
863 x = Fcar (tem);
864 y = Fcdr (tem);
867 /* If a click happens in an external tool bar or a detached
868 tool bar, x and y is NIL. In that case, use the current
869 mouse position. This happens for the help button in the
870 tool bar. Ideally popup-menu should pass NIL to
871 this function, but it doesn't. */
872 if (NILP (x) && NILP (y))
873 get_current_pos_p = 1;
876 if (get_current_pos_p)
878 /* Use the mouse's current position. */
879 FRAME_PTR new_f = SELECTED_FRAME ();
880 #ifdef HAVE_X_WINDOWS
881 /* Can't use mouse_position_hook for X since it returns
882 coordinates relative to the window the mouse is in,
883 we need coordinates relative to the edit widget always. */
884 if (new_f != 0)
886 int cur_x, cur_y;
888 mouse_position_for_popup (new_f, &cur_x, &cur_y);
889 /* cur_x/y may be negative, so use make_number. */
890 x = make_number (cur_x);
891 y = make_number (cur_y);
894 #else /* not HAVE_X_WINDOWS */
895 Lisp_Object bar_window;
896 enum scroll_bar_part part;
897 unsigned long time;
899 if (mouse_position_hook)
900 (*mouse_position_hook) (&new_f, 1, &bar_window,
901 &part, &x, &y, &time);
902 #endif /* not HAVE_X_WINDOWS */
904 if (new_f != 0)
905 XSETFRAME (window, new_f);
906 else
908 window = selected_window;
909 XSETFASTINT (x, 0);
910 XSETFASTINT (y, 0);
914 CHECK_NUMBER (x);
915 CHECK_NUMBER (y);
917 /* Decode where to put the menu. */
919 if (FRAMEP (window))
921 f = XFRAME (window);
922 xpos = 0;
923 ypos = 0;
925 else if (WINDOWP (window))
927 CHECK_LIVE_WINDOW (window);
928 f = XFRAME (WINDOW_FRAME (XWINDOW (window)));
930 xpos = WINDOW_LEFT_EDGE_X (XWINDOW (window));
931 ypos = WINDOW_TOP_EDGE_Y (XWINDOW (window));
933 else
934 /* ??? Not really clean; should be CHECK_WINDOW_OR_FRAME,
935 but I don't want to make one now. */
936 CHECK_WINDOW (window);
938 xpos += XINT (x);
939 ypos += XINT (y);
941 if (! FRAME_X_P (f))
942 error ("Can not put X menu on non-X terminal");
944 XSETFRAME (Vmenu_updating_frame, f);
946 else
947 Vmenu_updating_frame = Qnil;
948 #endif /* HAVE_MENUS */
950 record_unwind_protect (unuse_menu_items, Qnil);
951 title = Qnil;
952 GCPRO1 (title);
954 /* Decode the menu items from what was specified. */
956 keymap = get_keymap (menu, 0, 0);
957 if (CONSP (keymap))
959 /* We were given a keymap. Extract menu info from the keymap. */
960 Lisp_Object prompt;
962 /* Extract the detailed info to make one pane. */
963 keymap_panes (&menu, 1, NILP (position));
965 /* Search for a string appearing directly as an element of the keymap.
966 That string is the title of the menu. */
967 prompt = Fkeymap_prompt (keymap);
968 if (NILP (title) && !NILP (prompt))
969 title = prompt;
971 /* Make that be the pane title of the first pane. */
972 if (!NILP (prompt) && menu_items_n_panes >= 0)
973 XVECTOR (menu_items)->contents[MENU_ITEMS_PANE_NAME] = prompt;
975 keymaps = 1;
977 else if (CONSP (menu) && KEYMAPP (XCAR (menu)))
979 /* We were given a list of keymaps. */
980 int nmaps = XFASTINT (Flength (menu));
981 Lisp_Object *maps
982 = (Lisp_Object *) alloca (nmaps * sizeof (Lisp_Object));
983 int i;
985 title = Qnil;
987 /* The first keymap that has a prompt string
988 supplies the menu title. */
989 for (tem = menu, i = 0; CONSP (tem); tem = XCDR (tem))
991 Lisp_Object prompt;
993 maps[i++] = keymap = get_keymap (XCAR (tem), 1, 0);
995 prompt = Fkeymap_prompt (keymap);
996 if (NILP (title) && !NILP (prompt))
997 title = prompt;
1000 /* Extract the detailed info to make one pane. */
1001 keymap_panes (maps, nmaps, NILP (position));
1003 /* Make the title be the pane title of the first pane. */
1004 if (!NILP (title) && menu_items_n_panes >= 0)
1005 XVECTOR (menu_items)->contents[MENU_ITEMS_PANE_NAME] = title;
1007 keymaps = 1;
1009 else
1011 /* We were given an old-fashioned menu. */
1012 title = Fcar (menu);
1013 CHECK_STRING (title);
1015 list_of_panes (Fcdr (menu));
1017 keymaps = 0;
1020 unbind_to (specpdl_count, Qnil);
1022 if (NILP (position))
1024 discard_menu_items ();
1025 UNGCPRO;
1026 return Qnil;
1029 #ifdef HAVE_MENUS
1030 /* Display them in a menu. */
1031 BLOCK_INPUT;
1033 selection = xmenu_show (f, xpos, ypos, for_click,
1034 keymaps, title, &error_name);
1035 UNBLOCK_INPUT;
1037 discard_menu_items ();
1039 UNGCPRO;
1040 #endif /* HAVE_MENUS */
1042 if (error_name) error (error_name);
1043 return selection;
1046 #ifdef HAVE_MENUS
1048 DEFUN ("x-popup-dialog", Fx_popup_dialog, Sx_popup_dialog, 2, 3, 0,
1049 doc: /* Pop up a dialog box and return user's selection.
1050 POSITION specifies which frame to use.
1051 This is normally a mouse button event or a window or frame.
1052 If POSITION is t, it means to use the frame the mouse is on.
1053 The dialog box appears in the middle of the specified frame.
1055 CONTENTS specifies the alternatives to display in the dialog box.
1056 It is a list of the form (DIALOG ITEM1 ITEM2...).
1057 Each ITEM is a cons cell (STRING . VALUE).
1058 The return value is VALUE from the chosen item.
1060 An ITEM may also be just a string--that makes a nonselectable item.
1061 An ITEM may also be nil--that means to put all preceding items
1062 on the left of the dialog box and all following items on the right.
1063 \(By default, approximately half appear on each side.)
1065 If HEADER is non-nil, the frame title for the box is "Information",
1066 otherwise it is "Question".
1068 If the user gets rid of the dialog box without making a valid choice,
1069 for instance using the window manager, then this produces a quit and
1070 `x-popup-dialog' does not return. */)
1071 (position, contents, header)
1072 Lisp_Object position, contents, header;
1074 FRAME_PTR f = NULL;
1075 Lisp_Object window;
1077 check_x ();
1079 /* Decode the first argument: find the window or frame to use. */
1080 if (EQ (position, Qt)
1081 || (CONSP (position) && (EQ (XCAR (position), Qmenu_bar)
1082 || EQ (XCAR (position), Qtool_bar))))
1084 #if 0 /* Using the frame the mouse is on may not be right. */
1085 /* Use the mouse's current position. */
1086 FRAME_PTR new_f = SELECTED_FRAME ();
1087 Lisp_Object bar_window;
1088 enum scroll_bar_part part;
1089 unsigned long time;
1090 Lisp_Object x, y;
1092 (*mouse_position_hook) (&new_f, 1, &bar_window, &part, &x, &y, &time);
1094 if (new_f != 0)
1095 XSETFRAME (window, new_f);
1096 else
1097 window = selected_window;
1098 #endif
1099 window = selected_window;
1101 else if (CONSP (position))
1103 Lisp_Object tem;
1104 tem = Fcar (position);
1105 if (CONSP (tem))
1106 window = Fcar (Fcdr (position));
1107 else
1109 tem = Fcar (Fcdr (position)); /* EVENT_START (position) */
1110 window = Fcar (tem); /* POSN_WINDOW (tem) */
1113 else if (WINDOWP (position) || FRAMEP (position))
1114 window = position;
1115 else
1116 window = Qnil;
1118 /* Decode where to put the menu. */
1120 if (FRAMEP (window))
1121 f = XFRAME (window);
1122 else if (WINDOWP (window))
1124 CHECK_LIVE_WINDOW (window);
1125 f = XFRAME (WINDOW_FRAME (XWINDOW (window)));
1127 else
1128 /* ??? Not really clean; should be CHECK_WINDOW_OR_FRAME,
1129 but I don't want to make one now. */
1130 CHECK_WINDOW (window);
1132 if (! FRAME_X_P (f))
1133 error ("Can not put X dialog on non-X terminal");
1135 #if ! defined (USE_X_TOOLKIT) && ! defined (USE_GTK)
1136 /* Display a menu with these alternatives
1137 in the middle of frame F. */
1139 Lisp_Object x, y, frame, newpos;
1140 XSETFRAME (frame, f);
1141 XSETINT (x, x_pixel_width (f) / 2);
1142 XSETINT (y, x_pixel_height (f) / 2);
1143 newpos = Fcons (Fcons (x, Fcons (y, Qnil)), Fcons (frame, Qnil));
1145 return Fx_popup_menu (newpos,
1146 Fcons (Fcar (contents), Fcons (contents, Qnil)));
1148 #else
1150 Lisp_Object title;
1151 char *error_name;
1152 Lisp_Object selection;
1153 int specpdl_count = SPECPDL_INDEX ();
1155 /* Decode the dialog items from what was specified. */
1156 title = Fcar (contents);
1157 CHECK_STRING (title);
1158 record_unwind_protect (unuse_menu_items, Qnil);
1160 if (NILP (Fcar (Fcdr (contents))))
1161 /* No buttons specified, add an "Ok" button so users can pop down
1162 the dialog. Also, the lesstif/motif version crashes if there are
1163 no buttons. */
1164 contents = Fcons (title, Fcons (Fcons (build_string ("Ok"), Qt), Qnil));
1166 list_of_panes (Fcons (contents, Qnil));
1168 /* Display them in a dialog box. */
1169 BLOCK_INPUT;
1170 selection = xdialog_show (f, 0, title, header, &error_name);
1171 UNBLOCK_INPUT;
1173 unbind_to (specpdl_count, Qnil);
1174 discard_menu_items ();
1176 if (error_name) error (error_name);
1177 return selection;
1179 #endif
1183 #ifndef MSDOS
1185 /* Set menu_items_inuse so no other popup menu or dialog is created. */
1187 void
1188 x_menu_set_in_use (in_use)
1189 int in_use;
1191 menu_items_inuse = in_use ? Qt : Qnil;
1192 popup_activated_flag = in_use;
1195 /* Wait for an X event to arrive or for a timer to expire. */
1197 void
1198 x_menu_wait_for_event (void *data)
1200 extern EMACS_TIME timer_check P_ ((int));
1202 /* Another way to do this is to register a timer callback, that can be
1203 done in GTK and Xt. But we have to do it like this when using only X
1204 anyway, and with callbacks we would have three variants for timer handling
1205 instead of the small ifdefs below. */
1207 while (
1208 #ifdef USE_X_TOOLKIT
1209 ! XtAppPending (Xt_app_con)
1210 #elif defined USE_GTK
1211 ! gtk_events_pending ()
1212 #else
1213 ! XPending ((Display*) data)
1214 #endif
1217 EMACS_TIME next_time = timer_check (1);
1218 long secs = EMACS_SECS (next_time);
1219 long usecs = EMACS_USECS (next_time);
1220 SELECT_TYPE read_fds;
1221 struct x_display_info *dpyinfo;
1222 int n = 0;
1224 FD_ZERO (&read_fds);
1225 for (dpyinfo = x_display_list; dpyinfo; dpyinfo = dpyinfo->next)
1227 int fd = ConnectionNumber (dpyinfo->display);
1228 FD_SET (fd, &read_fds);
1229 if (fd > n) n = fd;
1232 if (secs < 0 || (secs == 0 && usecs == 0))
1234 /* Sometimes timer_check returns -1 (no timers) even if there are
1235 timers. So do a timeout anyway. */
1236 EMACS_SET_SECS (next_time, 1);
1237 EMACS_SET_USECS (next_time, 0);
1240 select (n + 1, &read_fds, (SELECT_TYPE *)0, (SELECT_TYPE *)0, &next_time);
1243 #endif /* ! MSDOS */
1246 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
1248 #ifdef USE_X_TOOLKIT
1250 /* Loop in Xt until the menu pulldown or dialog popup has been
1251 popped down (deactivated). This is used for x-popup-menu
1252 and x-popup-dialog; it is not used for the menu bar.
1254 NOTE: All calls to popup_get_selection should be protected
1255 with BLOCK_INPUT, UNBLOCK_INPUT wrappers. */
1257 static void
1258 popup_get_selection (initial_event, dpyinfo, id, do_timers)
1259 XEvent *initial_event;
1260 struct x_display_info *dpyinfo;
1261 LWLIB_ID id;
1262 int do_timers;
1264 XEvent event;
1266 while (popup_activated_flag)
1268 if (initial_event)
1270 event = *initial_event;
1271 initial_event = 0;
1273 else
1275 if (do_timers) x_menu_wait_for_event (0);
1276 XtAppNextEvent (Xt_app_con, &event);
1279 /* Make sure we don't consider buttons grabbed after menu goes.
1280 And make sure to deactivate for any ButtonRelease,
1281 even if XtDispatchEvent doesn't do that. */
1282 if (event.type == ButtonRelease
1283 && dpyinfo->display == event.xbutton.display)
1285 dpyinfo->grabbed &= ~(1 << event.xbutton.button);
1286 #ifdef USE_MOTIF /* Pretending that the event came from a
1287 Btn1Down seems the only way to convince Motif to
1288 activate its callbacks; setting the XmNmenuPost
1289 isn't working. --marcus@sysc.pdx.edu. */
1290 event.xbutton.button = 1;
1291 /* Motif only pops down menus when no Ctrl, Alt or Mod
1292 key is pressed and the button is released. So reset key state
1293 so Motif thinks this is the case. */
1294 event.xbutton.state = 0;
1295 #endif
1297 /* Pop down on C-g and Escape. */
1298 else if (event.type == KeyPress
1299 && dpyinfo->display == event.xbutton.display)
1301 KeySym keysym = XLookupKeysym (&event.xkey, 0);
1303 if ((keysym == XK_g && (event.xkey.state & ControlMask) != 0)
1304 || keysym == XK_Escape) /* Any escape, ignore modifiers. */
1305 popup_activated_flag = 0;
1308 x_dispatch_event (&event, event.xany.display);
1312 #endif /* USE_X_TOOLKIT */
1314 #ifdef USE_GTK
1315 /* Loop util popup_activated_flag is set to zero in a callback.
1316 Used for popup menus and dialogs. */
1318 static void
1319 popup_widget_loop (do_timers, widget)
1320 int do_timers;
1321 GtkWidget *widget;
1323 ++popup_activated_flag;
1325 /* Process events in the Gtk event loop until done. */
1326 while (popup_activated_flag)
1328 if (do_timers) x_menu_wait_for_event (0);
1329 gtk_main_iteration ();
1332 #endif
1334 /* Activate the menu bar of frame F.
1335 This is called from keyboard.c when it gets the
1336 MENU_BAR_ACTIVATE_EVENT out of the Emacs event queue.
1338 To activate the menu bar, we use the X button-press event
1339 that was saved in saved_menu_event.
1340 That makes the toolkit do its thing.
1342 But first we recompute the menu bar contents (the whole tree).
1344 The reason for saving the button event until here, instead of
1345 passing it to the toolkit right away, is that we can safely
1346 execute Lisp code. */
1348 void
1349 x_activate_menubar (f)
1350 FRAME_PTR f;
1352 if (! FRAME_X_P (f))
1353 abort ();
1355 if (!f->output_data.x->saved_menu_event->type)
1356 return;
1358 #ifdef USE_GTK
1359 if (! xg_win_to_widget (FRAME_X_DISPLAY (f),
1360 f->output_data.x->saved_menu_event->xany.window))
1361 return;
1362 #endif
1364 set_frame_menubar (f, 0, 1);
1365 BLOCK_INPUT;
1366 #ifdef USE_GTK
1367 XPutBackEvent (f->output_data.x->display_info->display,
1368 f->output_data.x->saved_menu_event);
1369 popup_activated_flag = 1;
1370 #else
1371 XtDispatchEvent (f->output_data.x->saved_menu_event);
1372 #endif
1373 UNBLOCK_INPUT;
1374 #ifdef USE_MOTIF
1375 if (f->output_data.x->saved_menu_event->type == ButtonRelease)
1376 pending_menu_activation = 1;
1377 #endif
1379 /* Ignore this if we get it a second time. */
1380 f->output_data.x->saved_menu_event->type = 0;
1383 /* Detect if a dialog or menu has been posted. */
1386 popup_activated ()
1388 return popup_activated_flag;
1391 /* This callback is invoked when the user selects a menubar cascade
1392 pushbutton, but before the pulldown menu is posted. */
1394 #ifndef USE_GTK
1395 static void
1396 popup_activate_callback (widget, id, client_data)
1397 Widget widget;
1398 LWLIB_ID id;
1399 XtPointer client_data;
1401 popup_activated_flag = 1;
1403 #endif
1405 /* This callback is invoked when a dialog or menu is finished being
1406 used and has been unposted. */
1408 #ifdef USE_GTK
1409 static void
1410 popup_deactivate_callback (widget, client_data)
1411 GtkWidget *widget;
1412 gpointer client_data;
1414 popup_activated_flag = 0;
1416 #else
1417 static void
1418 popup_deactivate_callback (widget, id, client_data)
1419 Widget widget;
1420 LWLIB_ID id;
1421 XtPointer client_data;
1423 popup_activated_flag = 0;
1425 #endif
1428 /* Function that finds the frame for WIDGET and shows the HELP text
1429 for that widget.
1430 F is the frame if known, or NULL if not known. */
1431 static void
1432 show_help_event (f, widget, help)
1433 FRAME_PTR f;
1434 xt_or_gtk_widget widget;
1435 Lisp_Object help;
1437 Lisp_Object frame;
1439 if (f)
1441 XSETFRAME (frame, f);
1442 kbd_buffer_store_help_event (frame, help);
1444 else
1446 #if 0 /* This code doesn't do anything useful. ++kfs */
1447 /* WIDGET is the popup menu. It's parent is the frame's
1448 widget. See which frame that is. */
1449 xt_or_gtk_widget frame_widget = XtParent (widget);
1450 Lisp_Object tail;
1452 for (tail = Vframe_list; GC_CONSP (tail); tail = XCDR (tail))
1454 frame = XCAR (tail);
1455 if (GC_FRAMEP (frame)
1456 && (f = XFRAME (frame),
1457 FRAME_X_P (f) && f->output_data.x->widget == frame_widget))
1458 break;
1460 #endif
1461 show_help_echo (help, Qnil, Qnil, Qnil, 1);
1465 /* Callback called when menu items are highlighted/unhighlighted
1466 while moving the mouse over them. WIDGET is the menu bar or menu
1467 popup widget. ID is its LWLIB_ID. CALL_DATA contains a pointer to
1468 the data structure for the menu item, or null in case of
1469 unhighlighting. */
1471 #ifdef USE_GTK
1472 void
1473 menu_highlight_callback (widget, call_data)
1474 GtkWidget *widget;
1475 gpointer call_data;
1477 xg_menu_item_cb_data *cb_data;
1478 Lisp_Object help;
1480 cb_data = (xg_menu_item_cb_data*) g_object_get_data (G_OBJECT (widget),
1481 XG_ITEM_DATA);
1482 if (! cb_data) return;
1484 help = call_data ? cb_data->help : Qnil;
1486 /* If popup_activated_flag is greater than 1 we are in a popup menu.
1487 Don't show help for them, they won't appear before the
1488 popup is popped down. */
1489 if (popup_activated_flag <= 1)
1490 show_help_event (cb_data->cl_data->f, widget, help);
1492 #else
1493 void
1494 menu_highlight_callback (widget, id, call_data)
1495 Widget widget;
1496 LWLIB_ID id;
1497 void *call_data;
1499 struct frame *f;
1500 Lisp_Object help;
1502 widget_value *wv = (widget_value *) call_data;
1504 help = wv ? wv->help : Qnil;
1506 /* Determine the frame for the help event. */
1507 f = menubar_id_to_frame (id);
1509 show_help_event (f, widget, help);
1511 #endif
1513 /* Find the menu selection and store it in the keyboard buffer.
1514 F is the frame the menu is on.
1515 MENU_BAR_ITEMS_USED is the length of VECTOR.
1516 VECTOR is an array of menu events for the whole menu. */
1518 static void
1519 find_and_call_menu_selection (f, menu_bar_items_used, vector, client_data)
1520 FRAME_PTR f;
1521 int menu_bar_items_used;
1522 Lisp_Object vector;
1523 void *client_data;
1525 Lisp_Object prefix, entry;
1526 Lisp_Object *subprefix_stack;
1527 int submenu_depth = 0;
1528 int i;
1530 entry = Qnil;
1531 subprefix_stack = (Lisp_Object *) alloca (menu_bar_items_used * sizeof (Lisp_Object));
1532 prefix = Qnil;
1533 i = 0;
1535 while (i < menu_bar_items_used)
1537 if (EQ (XVECTOR (vector)->contents[i], Qnil))
1539 subprefix_stack[submenu_depth++] = prefix;
1540 prefix = entry;
1541 i++;
1543 else if (EQ (XVECTOR (vector)->contents[i], Qlambda))
1545 prefix = subprefix_stack[--submenu_depth];
1546 i++;
1548 else if (EQ (XVECTOR (vector)->contents[i], Qt))
1550 prefix = XVECTOR (vector)->contents[i + MENU_ITEMS_PANE_PREFIX];
1551 i += MENU_ITEMS_PANE_LENGTH;
1553 else
1555 entry = XVECTOR (vector)->contents[i + MENU_ITEMS_ITEM_VALUE];
1556 /* The EMACS_INT cast avoids a warning. There's no problem
1557 as long as pointers have enough bits to hold small integers. */
1558 if ((int) (EMACS_INT) client_data == i)
1560 int j;
1561 struct input_event buf;
1562 Lisp_Object frame;
1563 EVENT_INIT (buf);
1565 XSETFRAME (frame, f);
1566 buf.kind = MENU_BAR_EVENT;
1567 buf.frame_or_window = frame;
1568 buf.arg = frame;
1569 kbd_buffer_store_event (&buf);
1571 for (j = 0; j < submenu_depth; j++)
1572 if (!NILP (subprefix_stack[j]))
1574 buf.kind = MENU_BAR_EVENT;
1575 buf.frame_or_window = frame;
1576 buf.arg = subprefix_stack[j];
1577 kbd_buffer_store_event (&buf);
1580 if (!NILP (prefix))
1582 buf.kind = MENU_BAR_EVENT;
1583 buf.frame_or_window = frame;
1584 buf.arg = prefix;
1585 kbd_buffer_store_event (&buf);
1588 buf.kind = MENU_BAR_EVENT;
1589 buf.frame_or_window = frame;
1590 buf.arg = entry;
1591 kbd_buffer_store_event (&buf);
1593 return;
1595 i += MENU_ITEMS_ITEM_LENGTH;
1601 #ifdef USE_GTK
1602 /* Gtk calls callbacks just because we tell it what item should be
1603 selected in a radio group. If this variable is set to a non-zero
1604 value, we are creating menus and don't want callbacks right now.
1606 static int xg_crazy_callback_abort;
1608 /* This callback is called from the menu bar pulldown menu
1609 when the user makes a selection.
1610 Figure out what the user chose
1611 and put the appropriate events into the keyboard buffer. */
1612 static void
1613 menubar_selection_callback (widget, client_data)
1614 GtkWidget *widget;
1615 gpointer client_data;
1617 xg_menu_item_cb_data *cb_data = (xg_menu_item_cb_data*) client_data;
1619 if (xg_crazy_callback_abort)
1620 return;
1622 if (! cb_data || ! cb_data->cl_data || ! cb_data->cl_data->f)
1623 return;
1625 /* For a group of radio buttons, GTK calls the selection callback first
1626 for the item that was active before the selection and then for the one that
1627 is active after the selection. For C-h k this means we get the help on
1628 the deselected item and then the selected item is executed. Prevent that
1629 by ignoring the non-active item. */
1630 if (GTK_IS_RADIO_MENU_ITEM (widget)
1631 && ! gtk_check_menu_item_get_active (GTK_CHECK_MENU_ITEM (widget)))
1632 return;
1634 /* When a menu is popped down, X generates a focus event (i.e. focus
1635 goes back to the frame below the menu). Since GTK buffers events,
1636 we force it out here before the menu selection event. Otherwise
1637 sit-for will exit at once if the focus event follows the menu selection
1638 event. */
1640 BLOCK_INPUT;
1641 while (gtk_events_pending ())
1642 gtk_main_iteration ();
1643 UNBLOCK_INPUT;
1645 find_and_call_menu_selection (cb_data->cl_data->f,
1646 cb_data->cl_data->menu_bar_items_used,
1647 cb_data->cl_data->menu_bar_vector,
1648 cb_data->call_data);
1651 #else /* not USE_GTK */
1653 /* This callback is called from the menu bar pulldown menu
1654 when the user makes a selection.
1655 Figure out what the user chose
1656 and put the appropriate events into the keyboard buffer. */
1657 static void
1658 menubar_selection_callback (widget, id, client_data)
1659 Widget widget;
1660 LWLIB_ID id;
1661 XtPointer client_data;
1663 FRAME_PTR f;
1665 f = menubar_id_to_frame (id);
1666 if (!f)
1667 return;
1668 find_and_call_menu_selection (f, f->menu_bar_items_used,
1669 f->menu_bar_vector, client_data);
1671 #endif /* not USE_GTK */
1673 /* Allocate a widget_value, blocking input. */
1675 widget_value *
1676 xmalloc_widget_value ()
1678 widget_value *value;
1680 BLOCK_INPUT;
1681 value = malloc_widget_value ();
1682 UNBLOCK_INPUT;
1684 return value;
1687 /* This recursively calls free_widget_value on the tree of widgets.
1688 It must free all data that was malloc'ed for these widget_values.
1689 In Emacs, many slots are pointers into the data of Lisp_Strings, and
1690 must be left alone. */
1692 void
1693 free_menubar_widget_value_tree (wv)
1694 widget_value *wv;
1696 if (! wv) return;
1698 wv->name = wv->value = wv->key = (char *) 0xDEADBEEF;
1700 if (wv->contents && (wv->contents != (widget_value*)1))
1702 free_menubar_widget_value_tree (wv->contents);
1703 wv->contents = (widget_value *) 0xDEADBEEF;
1705 if (wv->next)
1707 free_menubar_widget_value_tree (wv->next);
1708 wv->next = (widget_value *) 0xDEADBEEF;
1710 BLOCK_INPUT;
1711 free_widget_value (wv);
1712 UNBLOCK_INPUT;
1715 /* Set up data in menu_items for a menu bar item
1716 whose event type is ITEM_KEY (with string ITEM_NAME)
1717 and whose contents come from the list of keymaps MAPS. */
1719 static int
1720 parse_single_submenu (item_key, item_name, maps)
1721 Lisp_Object item_key, item_name, maps;
1723 Lisp_Object length;
1724 int len;
1725 Lisp_Object *mapvec;
1726 int i;
1727 int top_level_items = 0;
1729 length = Flength (maps);
1730 len = XINT (length);
1732 /* Convert the list MAPS into a vector MAPVEC. */
1733 mapvec = (Lisp_Object *) alloca (len * sizeof (Lisp_Object));
1734 for (i = 0; i < len; i++)
1736 mapvec[i] = Fcar (maps);
1737 maps = Fcdr (maps);
1740 /* Loop over the given keymaps, making a pane for each map.
1741 But don't make a pane that is empty--ignore that map instead. */
1742 for (i = 0; i < len; i++)
1744 if (!KEYMAPP (mapvec[i]))
1746 /* Here we have a command at top level in the menu bar
1747 as opposed to a submenu. */
1748 top_level_items = 1;
1749 push_menu_pane (Qnil, Qnil);
1750 push_menu_item (item_name, Qt, item_key, mapvec[i],
1751 Qnil, Qnil, Qnil, Qnil);
1753 else
1755 Lisp_Object prompt;
1756 prompt = Fkeymap_prompt (mapvec[i]);
1757 single_keymap_panes (mapvec[i],
1758 !NILP (prompt) ? prompt : item_name,
1759 item_key, 0, 10);
1763 return top_level_items;
1766 /* Create a tree of widget_value objects
1767 representing the panes and items
1768 in menu_items starting at index START, up to index END. */
1770 static widget_value *
1771 digest_single_submenu (start, end, top_level_items)
1772 int start, end, top_level_items;
1774 widget_value *wv, *prev_wv, *save_wv, *first_wv;
1775 int i;
1776 int submenu_depth = 0;
1777 widget_value **submenu_stack;
1778 int panes_seen = 0;
1780 submenu_stack
1781 = (widget_value **) alloca (menu_items_used * sizeof (widget_value *));
1782 wv = xmalloc_widget_value ();
1783 wv->name = "menu";
1784 wv->value = 0;
1785 wv->enabled = 1;
1786 wv->button_type = BUTTON_TYPE_NONE;
1787 wv->help = Qnil;
1788 first_wv = wv;
1789 save_wv = 0;
1790 prev_wv = 0;
1792 /* Loop over all panes and items made by the preceding call
1793 to parse_single_submenu and construct a tree of widget_value objects.
1794 Ignore the panes and items used by previous calls to
1795 digest_single_submenu, even though those are also in menu_items. */
1796 i = start;
1797 while (i < end)
1799 if (EQ (XVECTOR (menu_items)->contents[i], Qnil))
1801 submenu_stack[submenu_depth++] = save_wv;
1802 save_wv = prev_wv;
1803 prev_wv = 0;
1804 i++;
1806 else if (EQ (XVECTOR (menu_items)->contents[i], Qlambda))
1808 prev_wv = save_wv;
1809 save_wv = submenu_stack[--submenu_depth];
1810 i++;
1812 else if (EQ (XVECTOR (menu_items)->contents[i], Qt)
1813 && submenu_depth != 0)
1814 i += MENU_ITEMS_PANE_LENGTH;
1815 /* Ignore a nil in the item list.
1816 It's meaningful only for dialog boxes. */
1817 else if (EQ (XVECTOR (menu_items)->contents[i], Qquote))
1818 i += 1;
1819 else if (EQ (XVECTOR (menu_items)->contents[i], Qt))
1821 /* Create a new pane. */
1822 Lisp_Object pane_name, prefix;
1823 char *pane_string;
1825 panes_seen++;
1827 pane_name = XVECTOR (menu_items)->contents[i + MENU_ITEMS_PANE_NAME];
1828 prefix = XVECTOR (menu_items)->contents[i + MENU_ITEMS_PANE_PREFIX];
1830 #ifndef HAVE_MULTILINGUAL_MENU
1831 if (STRINGP (pane_name) && STRING_MULTIBYTE (pane_name))
1833 pane_name = ENCODE_MENU_STRING (pane_name);
1834 AREF (menu_items, i + MENU_ITEMS_PANE_NAME) = pane_name;
1836 #endif
1837 pane_string = (NILP (pane_name)
1838 ? "" : (char *) SDATA (pane_name));
1839 /* If there is just one top-level pane, put all its items directly
1840 under the top-level menu. */
1841 if (menu_items_n_panes == 1)
1842 pane_string = "";
1844 /* If the pane has a meaningful name,
1845 make the pane a top-level menu item
1846 with its items as a submenu beneath it. */
1847 if (strcmp (pane_string, ""))
1849 wv = xmalloc_widget_value ();
1850 if (save_wv)
1851 save_wv->next = wv;
1852 else
1853 first_wv->contents = wv;
1854 wv->lname = pane_name;
1855 /* Set value to 1 so update_submenu_strings can handle '@' */
1856 wv->value = (char *)1;
1857 wv->enabled = 1;
1858 wv->button_type = BUTTON_TYPE_NONE;
1859 wv->help = Qnil;
1860 save_wv = wv;
1862 else
1863 save_wv = first_wv;
1865 prev_wv = 0;
1866 i += MENU_ITEMS_PANE_LENGTH;
1868 else
1870 /* Create a new item within current pane. */
1871 Lisp_Object item_name, enable, descrip, def, type, selected;
1872 Lisp_Object help;
1874 /* All items should be contained in panes. */
1875 if (panes_seen == 0)
1876 abort ();
1878 item_name = AREF (menu_items, i + MENU_ITEMS_ITEM_NAME);
1879 enable = AREF (menu_items, i + MENU_ITEMS_ITEM_ENABLE);
1880 descrip = AREF (menu_items, i + MENU_ITEMS_ITEM_EQUIV_KEY);
1881 def = AREF (menu_items, i + MENU_ITEMS_ITEM_DEFINITION);
1882 type = AREF (menu_items, i + MENU_ITEMS_ITEM_TYPE);
1883 selected = AREF (menu_items, i + MENU_ITEMS_ITEM_SELECTED);
1884 help = AREF (menu_items, i + MENU_ITEMS_ITEM_HELP);
1886 #ifndef HAVE_MULTILINGUAL_MENU
1887 if (STRING_MULTIBYTE (item_name))
1889 item_name = ENCODE_MENU_STRING (item_name);
1890 AREF (menu_items, i + MENU_ITEMS_ITEM_NAME) = item_name;
1893 if (STRINGP (descrip) && STRING_MULTIBYTE (descrip))
1895 descrip = ENCODE_MENU_STRING (descrip);
1896 AREF (menu_items, i + MENU_ITEMS_ITEM_EQUIV_KEY) = descrip;
1898 #endif /* not HAVE_MULTILINGUAL_MENU */
1900 wv = xmalloc_widget_value ();
1901 if (prev_wv)
1902 prev_wv->next = wv;
1903 else
1904 save_wv->contents = wv;
1906 wv->lname = item_name;
1907 if (!NILP (descrip))
1908 wv->lkey = descrip;
1909 wv->value = 0;
1910 /* The EMACS_INT cast avoids a warning. There's no problem
1911 as long as pointers have enough bits to hold small integers. */
1912 wv->call_data = (!NILP (def) ? (void *) (EMACS_INT) i : 0);
1913 wv->enabled = !NILP (enable);
1915 if (NILP (type))
1916 wv->button_type = BUTTON_TYPE_NONE;
1917 else if (EQ (type, QCradio))
1918 wv->button_type = BUTTON_TYPE_RADIO;
1919 else if (EQ (type, QCtoggle))
1920 wv->button_type = BUTTON_TYPE_TOGGLE;
1921 else
1922 abort ();
1924 wv->selected = !NILP (selected);
1925 if (! STRINGP (help))
1926 help = Qnil;
1928 wv->help = help;
1930 prev_wv = wv;
1932 i += MENU_ITEMS_ITEM_LENGTH;
1936 /* If we have just one "menu item"
1937 that was originally a button, return it by itself. */
1938 if (top_level_items && first_wv->contents && first_wv->contents->next == 0)
1940 wv = first_wv->contents;
1941 free_widget_value (first_wv);
1942 return wv;
1945 return first_wv;
1948 /* Walk through the widget_value tree starting at FIRST_WV and update
1949 the char * pointers from the corresponding lisp values.
1950 We do this after building the whole tree, since GC may happen while the
1951 tree is constructed, and small strings are relocated. So we must wait
1952 until no GC can happen before storing pointers into lisp values. */
1953 static void
1954 update_submenu_strings (first_wv)
1955 widget_value *first_wv;
1957 widget_value *wv;
1959 for (wv = first_wv; wv; wv = wv->next)
1961 if (STRINGP (wv->lname))
1963 wv->name = (char *) SDATA (wv->lname);
1965 /* Ignore the @ that means "separate pane".
1966 This is a kludge, but this isn't worth more time. */
1967 if (wv->value == (char *)1)
1969 if (wv->name[0] == '@')
1970 wv->name++;
1971 wv->value = 0;
1975 if (STRINGP (wv->lkey))
1976 wv->key = (char *) SDATA (wv->lkey);
1978 if (wv->contents)
1979 update_submenu_strings (wv->contents);
1984 /* Recompute all the widgets of frame F, when the menu bar has been
1985 changed. Value is non-zero if widgets were updated. */
1987 static int
1988 update_frame_menubar (f)
1989 FRAME_PTR f;
1991 #ifdef USE_GTK
1992 return xg_update_frame_menubar (f);
1993 #else
1994 struct x_output *x;
1995 int columns, rows;
1997 if (! FRAME_X_P (f))
1998 abort ();
2000 x = f->output_data.x;
2002 if (!x->menubar_widget || XtIsManaged (x->menubar_widget))
2003 return 0;
2005 BLOCK_INPUT;
2006 /* Save the size of the frame because the pane widget doesn't accept
2007 to resize itself. So force it. */
2008 columns = FRAME_COLS (f);
2009 rows = FRAME_LINES (f);
2011 /* Do the voodoo which means "I'm changing lots of things, don't try
2012 to refigure sizes until I'm done." */
2013 lw_refigure_widget (x->column_widget, False);
2015 /* The order in which children are managed is the top to bottom
2016 order in which they are displayed in the paned window. First,
2017 remove the text-area widget. */
2018 XtUnmanageChild (x->edit_widget);
2020 /* Remove the menubar that is there now, and put up the menubar that
2021 should be there. */
2022 XtManageChild (x->menubar_widget);
2023 XtMapWidget (x->menubar_widget);
2024 XtVaSetValues (x->menubar_widget, XtNmappedWhenManaged, 1, NULL);
2026 /* Re-manage the text-area widget, and then thrash the sizes. */
2027 XtManageChild (x->edit_widget);
2028 lw_refigure_widget (x->column_widget, True);
2030 /* Force the pane widget to resize itself with the right values. */
2031 EmacsFrameSetCharSize (x->edit_widget, columns, rows);
2032 UNBLOCK_INPUT;
2033 #endif
2034 return 1;
2037 /* Set the contents of the menubar widgets of frame F.
2038 The argument FIRST_TIME is currently ignored;
2039 it is set the first time this is called, from initialize_frame_menubar. */
2041 void
2042 set_frame_menubar (f, first_time, deep_p)
2043 FRAME_PTR f;
2044 int first_time;
2045 int deep_p;
2047 xt_or_gtk_widget menubar_widget;
2048 #ifdef USE_X_TOOLKIT
2049 LWLIB_ID id;
2050 #endif
2051 Lisp_Object items;
2052 widget_value *wv, *first_wv, *prev_wv = 0;
2053 int i, last_i = 0;
2054 int *submenu_start, *submenu_end;
2055 int *submenu_top_level_items, *submenu_n_panes;
2057 if (! FRAME_X_P (f))
2058 abort ();
2060 menubar_widget = f->output_data.x->menubar_widget;
2062 XSETFRAME (Vmenu_updating_frame, f);
2064 #ifdef USE_X_TOOLKIT
2065 if (f->output_data.x->id == 0)
2066 f->output_data.x->id = next_menubar_widget_id++;
2067 id = f->output_data.x->id;
2068 #endif
2070 if (! menubar_widget)
2071 deep_p = 1;
2072 else if (pending_menu_activation && !deep_p)
2073 deep_p = 1;
2074 /* Make the first call for any given frame always go deep. */
2075 else if (!f->output_data.x->saved_menu_event && !deep_p)
2077 deep_p = 1;
2078 f->output_data.x->saved_menu_event = (XEvent*)xmalloc (sizeof (XEvent));
2079 f->output_data.x->saved_menu_event->type = 0;
2082 #ifdef USE_GTK
2083 /* If we have detached menus, we must update deep so detached menus
2084 also gets updated. */
2085 deep_p = deep_p || xg_have_tear_offs ();
2086 #endif
2088 if (deep_p)
2090 /* Make a widget-value tree representing the entire menu trees. */
2092 struct buffer *prev = current_buffer;
2093 Lisp_Object buffer;
2094 int specpdl_count = SPECPDL_INDEX ();
2095 int previous_menu_items_used = f->menu_bar_items_used;
2096 Lisp_Object *previous_items
2097 = (Lisp_Object *) alloca (previous_menu_items_used
2098 * sizeof (Lisp_Object));
2100 /* If we are making a new widget, its contents are empty,
2101 do always reinitialize them. */
2102 if (! menubar_widget)
2103 previous_menu_items_used = 0;
2105 buffer = XWINDOW (FRAME_SELECTED_WINDOW (f))->buffer;
2106 specbind (Qinhibit_quit, Qt);
2107 /* Don't let the debugger step into this code
2108 because it is not reentrant. */
2109 specbind (Qdebug_on_next_call, Qnil);
2111 record_unwind_save_match_data ();
2112 if (NILP (Voverriding_local_map_menu_flag))
2114 specbind (Qoverriding_terminal_local_map, Qnil);
2115 specbind (Qoverriding_local_map, Qnil);
2118 set_buffer_internal_1 (XBUFFER (buffer));
2120 /* Run the Lucid hook. */
2121 safe_run_hooks (Qactivate_menubar_hook);
2123 /* If it has changed current-menubar from previous value,
2124 really recompute the menubar from the value. */
2125 if (! NILP (Vlucid_menu_bar_dirty_flag))
2126 call0 (Qrecompute_lucid_menubar);
2127 safe_run_hooks (Qmenu_bar_update_hook);
2128 FRAME_MENU_BAR_ITEMS (f) = menu_bar_items (FRAME_MENU_BAR_ITEMS (f));
2130 items = FRAME_MENU_BAR_ITEMS (f);
2132 /* Save the frame's previous menu bar contents data. */
2133 if (previous_menu_items_used)
2134 bcopy (XVECTOR (f->menu_bar_vector)->contents, previous_items,
2135 previous_menu_items_used * sizeof (Lisp_Object));
2137 /* Fill in menu_items with the current menu bar contents.
2138 This can evaluate Lisp code. */
2139 save_menu_items ();
2141 menu_items = f->menu_bar_vector;
2142 menu_items_allocated = VECTORP (menu_items) ? ASIZE (menu_items) : 0;
2143 submenu_start = (int *) alloca (XVECTOR (items)->size * sizeof (int *));
2144 submenu_end = (int *) alloca (XVECTOR (items)->size * sizeof (int *));
2145 submenu_n_panes = (int *) alloca (XVECTOR (items)->size * sizeof (int));
2146 submenu_top_level_items
2147 = (int *) alloca (XVECTOR (items)->size * sizeof (int *));
2148 init_menu_items ();
2149 for (i = 0; i < XVECTOR (items)->size; i += 4)
2151 Lisp_Object key, string, maps;
2153 last_i = i;
2155 key = XVECTOR (items)->contents[i];
2156 string = XVECTOR (items)->contents[i + 1];
2157 maps = XVECTOR (items)->contents[i + 2];
2158 if (NILP (string))
2159 break;
2161 submenu_start[i] = menu_items_used;
2163 menu_items_n_panes = 0;
2164 submenu_top_level_items[i]
2165 = parse_single_submenu (key, string, maps);
2166 submenu_n_panes[i] = menu_items_n_panes;
2168 submenu_end[i] = menu_items_used;
2171 finish_menu_items ();
2173 /* Convert menu_items into widget_value trees
2174 to display the menu. This cannot evaluate Lisp code. */
2176 wv = xmalloc_widget_value ();
2177 wv->name = "menubar";
2178 wv->value = 0;
2179 wv->enabled = 1;
2180 wv->button_type = BUTTON_TYPE_NONE;
2181 wv->help = Qnil;
2182 first_wv = wv;
2184 for (i = 0; i < last_i; i += 4)
2186 menu_items_n_panes = submenu_n_panes[i];
2187 wv = digest_single_submenu (submenu_start[i], submenu_end[i],
2188 submenu_top_level_items[i]);
2189 if (prev_wv)
2190 prev_wv->next = wv;
2191 else
2192 first_wv->contents = wv;
2193 /* Don't set wv->name here; GC during the loop might relocate it. */
2194 wv->enabled = 1;
2195 wv->button_type = BUTTON_TYPE_NONE;
2196 prev_wv = wv;
2199 set_buffer_internal_1 (prev);
2201 /* If there has been no change in the Lisp-level contents
2202 of the menu bar, skip redisplaying it. Just exit. */
2204 /* Compare the new menu items with the ones computed last time. */
2205 for (i = 0; i < previous_menu_items_used; i++)
2206 if (menu_items_used == i
2207 || (!EQ (previous_items[i], XVECTOR (menu_items)->contents[i])))
2208 break;
2209 if (i == menu_items_used && i == previous_menu_items_used && i != 0)
2211 /* The menu items have not changed. Don't bother updating
2212 the menus in any form, since it would be a no-op. */
2213 free_menubar_widget_value_tree (first_wv);
2214 discard_menu_items ();
2215 unbind_to (specpdl_count, Qnil);
2216 return;
2219 /* The menu items are different, so store them in the frame. */
2220 f->menu_bar_vector = menu_items;
2221 f->menu_bar_items_used = menu_items_used;
2223 /* This calls restore_menu_items to restore menu_items, etc.,
2224 as they were outside. */
2225 unbind_to (specpdl_count, Qnil);
2227 /* Now GC cannot happen during the lifetime of the widget_value,
2228 so it's safe to store data from a Lisp_String. */
2229 wv = first_wv->contents;
2230 for (i = 0; i < XVECTOR (items)->size; i += 4)
2232 Lisp_Object string;
2233 string = XVECTOR (items)->contents[i + 1];
2234 if (NILP (string))
2235 break;
2236 wv->name = (char *) SDATA (string);
2237 update_submenu_strings (wv->contents);
2238 wv = wv->next;
2242 else
2244 /* Make a widget-value tree containing
2245 just the top level menu bar strings. */
2247 wv = xmalloc_widget_value ();
2248 wv->name = "menubar";
2249 wv->value = 0;
2250 wv->enabled = 1;
2251 wv->button_type = BUTTON_TYPE_NONE;
2252 wv->help = Qnil;
2253 first_wv = wv;
2255 items = FRAME_MENU_BAR_ITEMS (f);
2256 for (i = 0; i < XVECTOR (items)->size; i += 4)
2258 Lisp_Object string;
2260 string = XVECTOR (items)->contents[i + 1];
2261 if (NILP (string))
2262 break;
2264 wv = xmalloc_widget_value ();
2265 wv->name = (char *) SDATA (string);
2266 wv->value = 0;
2267 wv->enabled = 1;
2268 wv->button_type = BUTTON_TYPE_NONE;
2269 wv->help = Qnil;
2270 /* This prevents lwlib from assuming this
2271 menu item is really supposed to be empty. */
2272 /* The EMACS_INT cast avoids a warning.
2273 This value just has to be different from small integers. */
2274 wv->call_data = (void *) (EMACS_INT) (-1);
2276 if (prev_wv)
2277 prev_wv->next = wv;
2278 else
2279 first_wv->contents = wv;
2280 prev_wv = wv;
2283 /* Forget what we thought we knew about what is in the
2284 detailed contents of the menu bar menus.
2285 Changing the top level always destroys the contents. */
2286 f->menu_bar_items_used = 0;
2289 /* Create or update the menu bar widget. */
2291 BLOCK_INPUT;
2293 #ifdef USE_GTK
2294 xg_crazy_callback_abort = 1;
2295 if (menubar_widget)
2297 /* The fourth arg is DEEP_P, which says to consider the entire
2298 menu trees we supply, rather than just the menu bar item names. */
2299 xg_modify_menubar_widgets (menubar_widget,
2301 first_wv,
2302 deep_p,
2303 G_CALLBACK (menubar_selection_callback),
2304 G_CALLBACK (popup_deactivate_callback),
2305 G_CALLBACK (menu_highlight_callback));
2307 else
2309 GtkWidget *wvbox = f->output_data.x->vbox_widget;
2311 menubar_widget
2312 = xg_create_widget ("menubar", "menubar", f, first_wv,
2313 G_CALLBACK (menubar_selection_callback),
2314 G_CALLBACK (popup_deactivate_callback),
2315 G_CALLBACK (menu_highlight_callback));
2317 f->output_data.x->menubar_widget = menubar_widget;
2321 #else /* not USE_GTK */
2322 if (menubar_widget)
2324 /* Disable resizing (done for Motif!) */
2325 lw_allow_resizing (f->output_data.x->widget, False);
2327 /* The third arg is DEEP_P, which says to consider the entire
2328 menu trees we supply, rather than just the menu bar item names. */
2329 lw_modify_all_widgets (id, first_wv, deep_p);
2331 /* Re-enable the edit widget to resize. */
2332 lw_allow_resizing (f->output_data.x->widget, True);
2334 else
2336 char menuOverride[] = "Ctrl<KeyPress>g: MenuGadgetEscape()";
2337 XtTranslations override = XtParseTranslationTable (menuOverride);
2339 menubar_widget = lw_create_widget ("menubar", "menubar", id, first_wv,
2340 f->output_data.x->column_widget,
2342 popup_activate_callback,
2343 menubar_selection_callback,
2344 popup_deactivate_callback,
2345 menu_highlight_callback);
2346 f->output_data.x->menubar_widget = menubar_widget;
2348 /* Make menu pop down on C-g. */
2349 XtOverrideTranslations (menubar_widget, override);
2353 int menubar_size
2354 = (f->output_data.x->menubar_widget
2355 ? (f->output_data.x->menubar_widget->core.height
2356 + f->output_data.x->menubar_widget->core.border_width)
2357 : 0);
2359 #if 0 /* Experimentally, we now get the right results
2360 for -geometry -0-0 without this. 24 Aug 96, rms. */
2361 #ifdef USE_LUCID
2362 if (FRAME_EXTERNAL_MENU_BAR (f))
2364 Dimension ibw = 0;
2365 XtVaGetValues (f->output_data.x->column_widget,
2366 XtNinternalBorderWidth, &ibw, NULL);
2367 menubar_size += ibw;
2369 #endif /* USE_LUCID */
2370 #endif /* 0 */
2372 f->output_data.x->menubar_height = menubar_size;
2374 #endif /* not USE_GTK */
2376 free_menubar_widget_value_tree (first_wv);
2377 update_frame_menubar (f);
2379 #ifdef USE_GTK
2380 xg_crazy_callback_abort = 0;
2381 #endif
2383 UNBLOCK_INPUT;
2386 /* Called from Fx_create_frame to create the initial menubar of a frame
2387 before it is mapped, so that the window is mapped with the menubar already
2388 there instead of us tacking it on later and thrashing the window after it
2389 is visible. */
2391 void
2392 initialize_frame_menubar (f)
2393 FRAME_PTR f;
2395 /* This function is called before the first chance to redisplay
2396 the frame. It has to be, so the frame will have the right size. */
2397 FRAME_MENU_BAR_ITEMS (f) = menu_bar_items (FRAME_MENU_BAR_ITEMS (f));
2398 set_frame_menubar (f, 1, 1);
2402 /* Get rid of the menu bar of frame F, and free its storage.
2403 This is used when deleting a frame, and when turning off the menu bar.
2404 For GTK this function is in gtkutil.c. */
2406 #ifndef USE_GTK
2407 void
2408 free_frame_menubar (f)
2409 FRAME_PTR f;
2411 Widget menubar_widget;
2413 if (! FRAME_X_P (f))
2414 abort ();
2416 menubar_widget = f->output_data.x->menubar_widget;
2418 f->output_data.x->menubar_height = 0;
2420 if (menubar_widget)
2422 #ifdef USE_MOTIF
2423 /* Removing the menu bar magically changes the shell widget's x
2424 and y position of (0, 0) which, when the menu bar is turned
2425 on again, leads to pull-down menuss appearing in strange
2426 positions near the upper-left corner of the display. This
2427 happens only with some window managers like twm and ctwm,
2428 but not with other like Motif's mwm or kwm, because the
2429 latter generate ConfigureNotify events when the menu bar
2430 is switched off, which fixes the shell position. */
2431 Position x0, y0, x1, y1;
2432 #endif
2434 BLOCK_INPUT;
2436 #ifdef USE_MOTIF
2437 if (f->output_data.x->widget)
2438 XtVaGetValues (f->output_data.x->widget, XtNx, &x0, XtNy, &y0, NULL);
2439 #endif
2441 lw_destroy_all_widgets ((LWLIB_ID) f->output_data.x->id);
2442 f->output_data.x->menubar_widget = NULL;
2444 #ifdef USE_MOTIF
2445 if (f->output_data.x->widget)
2447 XtVaGetValues (f->output_data.x->widget, XtNx, &x1, XtNy, &y1, NULL);
2448 if (x1 == 0 && y1 == 0)
2449 XtVaSetValues (f->output_data.x->widget, XtNx, x0, XtNy, y0, NULL);
2451 #endif
2453 UNBLOCK_INPUT;
2456 #endif /* not USE_GTK */
2458 #endif /* USE_X_TOOLKIT || USE_GTK */
2460 /* xmenu_show actually displays a menu using the panes and items in menu_items
2461 and returns the value selected from it.
2462 There are two versions of xmenu_show, one for Xt and one for Xlib.
2463 Both assume input is blocked by the caller. */
2465 /* F is the frame the menu is for.
2466 X and Y are the frame-relative specified position,
2467 relative to the inside upper left corner of the frame F.
2468 FOR_CLICK is nonzero if this menu was invoked for a mouse click.
2469 KEYMAPS is 1 if this menu was specified with keymaps;
2470 in that case, we return a list containing the chosen item's value
2471 and perhaps also the pane's prefix.
2472 TITLE is the specified menu title.
2473 ERROR is a place to store an error message string in case of failure.
2474 (We return nil on failure, but the value doesn't actually matter.) */
2476 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
2478 /* The item selected in the popup menu. */
2479 static Lisp_Object *volatile menu_item_selection;
2481 #ifdef USE_GTK
2483 /* Used when position a popup menu. See menu_position_func and
2484 create_and_show_popup_menu below. */
2485 struct next_popup_x_y
2487 FRAME_PTR f;
2488 int x;
2489 int y;
2492 /* The menu position function to use if we are not putting a popup
2493 menu where the pointer is.
2494 MENU is the menu to pop up.
2495 X and Y shall on exit contain x/y where the menu shall pop up.
2496 PUSH_IN is not documented in the GTK manual.
2497 USER_DATA is any data passed in when calling gtk_menu_popup.
2498 Here it points to a struct next_popup_x_y where the coordinates
2499 to store in *X and *Y are as well as the frame for the popup.
2501 Here only X and Y are used. */
2502 static void
2503 menu_position_func (menu, x, y, push_in, user_data)
2504 GtkMenu *menu;
2505 gint *x;
2506 gint *y;
2507 gboolean *push_in;
2508 gpointer user_data;
2510 struct next_popup_x_y* data = (struct next_popup_x_y*)user_data;
2511 GtkRequisition req;
2512 int disp_width = FRAME_X_DISPLAY_INFO (data->f)->width;
2513 int disp_height = FRAME_X_DISPLAY_INFO (data->f)->height;
2515 *x = data->x;
2516 *y = data->y;
2518 /* Check if there is room for the menu. If not, adjust x/y so that
2519 the menu is fully visible. */
2520 gtk_widget_size_request (GTK_WIDGET (menu), &req);
2521 if (data->x + req.width > disp_width)
2522 *x -= data->x + req.width - disp_width;
2523 if (data->y + req.height > disp_height)
2524 *y -= data->y + req.height - disp_height;
2527 static void
2528 popup_selection_callback (widget, client_data)
2529 GtkWidget *widget;
2530 gpointer client_data;
2532 xg_menu_item_cb_data *cb_data = (xg_menu_item_cb_data*) client_data;
2534 if (xg_crazy_callback_abort) return;
2535 if (cb_data) menu_item_selection = (Lisp_Object *) cb_data->call_data;
2538 static Lisp_Object
2539 pop_down_menu (arg)
2540 Lisp_Object arg;
2542 struct Lisp_Save_Value *p = XSAVE_VALUE (arg);
2544 popup_activated_flag = 0;
2545 BLOCK_INPUT;
2546 gtk_widget_destroy (GTK_WIDGET (p->pointer));
2547 UNBLOCK_INPUT;
2548 return Qnil;
2551 /* Pop up the menu for frame F defined by FIRST_WV at X/Y and loop until the
2552 menu pops down.
2553 menu_item_selection will be set to the selection. */
2554 static void
2555 create_and_show_popup_menu (f, first_wv, x, y, for_click)
2556 FRAME_PTR f;
2557 widget_value *first_wv;
2558 int x;
2559 int y;
2560 int for_click;
2562 int i;
2563 GtkWidget *menu;
2564 GtkMenuPositionFunc pos_func = 0; /* Pop up at pointer. */
2565 struct next_popup_x_y popup_x_y;
2566 int specpdl_count = SPECPDL_INDEX ();
2568 if (! FRAME_X_P (f))
2569 abort ();
2571 xg_crazy_callback_abort = 1;
2572 menu = xg_create_widget ("popup", first_wv->name, f, first_wv,
2573 G_CALLBACK (popup_selection_callback),
2574 G_CALLBACK (popup_deactivate_callback),
2575 G_CALLBACK (menu_highlight_callback));
2576 xg_crazy_callback_abort = 0;
2578 if (! for_click)
2580 /* Not invoked by a click. pop up at x/y. */
2581 pos_func = menu_position_func;
2583 /* Adjust coordinates to be root-window-relative. */
2584 x += f->left_pos + FRAME_OUTER_TO_INNER_DIFF_X (f);
2585 y += f->top_pos + FRAME_OUTER_TO_INNER_DIFF_Y (f);
2587 popup_x_y.x = x;
2588 popup_x_y.y = y;
2589 popup_x_y.f = f;
2591 i = 0; /* gtk_menu_popup needs this to be 0 for a non-button popup. */
2593 else
2595 for (i = 0; i < 5; i++)
2596 if (FRAME_X_DISPLAY_INFO (f)->grabbed & (1 << i))
2597 break;
2600 /* Display the menu. */
2601 gtk_widget_show_all (menu);
2602 gtk_menu_popup (GTK_MENU (menu), 0, 0, pos_func, &popup_x_y, i, 0);
2604 record_unwind_protect (pop_down_menu, make_save_value (menu, 0));
2606 if (GTK_WIDGET_MAPPED (menu))
2608 /* Set this to one. popup_widget_loop increases it by one, so it becomes
2609 two. show_help_echo uses this to detect popup menus. */
2610 popup_activated_flag = 1;
2611 /* Process events that apply to the menu. */
2612 popup_widget_loop (1, menu);
2615 unbind_to (specpdl_count, Qnil);
2617 /* Must reset this manually because the button release event is not passed
2618 to Emacs event loop. */
2619 FRAME_X_DISPLAY_INFO (f)->grabbed = 0;
2622 #else /* not USE_GTK */
2624 /* We need a unique id for each widget handled by the Lucid Widget
2625 library.
2627 For the main windows, and popup menus, we use this counter,
2628 which we increment each time after use. This starts from 1<<16.
2630 For menu bars, we use numbers starting at 0, counted in
2631 next_menubar_widget_id. */
2632 LWLIB_ID widget_id_tick;
2634 static void
2635 popup_selection_callback (widget, id, client_data)
2636 Widget widget;
2637 LWLIB_ID id;
2638 XtPointer client_data;
2640 menu_item_selection = (Lisp_Object *) client_data;
2643 /* ARG is the LWLIB ID of the dialog box, represented
2644 as a Lisp object as (HIGHPART . LOWPART). */
2646 static Lisp_Object
2647 pop_down_menu (arg)
2648 Lisp_Object arg;
2650 LWLIB_ID id = (XINT (XCAR (arg)) << 4 * sizeof (LWLIB_ID)
2651 | XINT (XCDR (arg)));
2653 BLOCK_INPUT;
2654 lw_destroy_all_widgets (id);
2655 UNBLOCK_INPUT;
2656 popup_activated_flag = 0;
2658 return Qnil;
2661 /* Pop up the menu for frame F defined by FIRST_WV at X/Y and loop until the
2662 menu pops down.
2663 menu_item_selection will be set to the selection. */
2664 static void
2665 create_and_show_popup_menu (f, first_wv, x, y, for_click)
2666 FRAME_PTR f;
2667 widget_value *first_wv;
2668 int x;
2669 int y;
2670 int for_click;
2672 int i;
2673 Arg av[2];
2674 int ac = 0;
2675 XButtonPressedEvent dummy;
2676 LWLIB_ID menu_id;
2677 Widget menu;
2679 if (! FRAME_X_P (f))
2680 abort ();
2682 menu_id = widget_id_tick++;
2683 menu = lw_create_widget ("popup", first_wv->name, menu_id, first_wv,
2684 f->output_data.x->widget, 1, 0,
2685 popup_selection_callback,
2686 popup_deactivate_callback,
2687 menu_highlight_callback);
2689 dummy.type = ButtonPress;
2690 dummy.serial = 0;
2691 dummy.send_event = 0;
2692 dummy.display = FRAME_X_DISPLAY (f);
2693 dummy.time = CurrentTime;
2694 dummy.root = FRAME_X_DISPLAY_INFO (f)->root_window;
2695 dummy.window = dummy.root;
2696 dummy.subwindow = dummy.root;
2697 dummy.x = x;
2698 dummy.y = y;
2700 /* Adjust coordinates to be root-window-relative. */
2701 x += f->left_pos + FRAME_OUTER_TO_INNER_DIFF_X (f);
2702 y += f->top_pos + FRAME_OUTER_TO_INNER_DIFF_Y (f);
2704 dummy.x_root = x;
2705 dummy.y_root = y;
2707 dummy.state = 0;
2708 dummy.button = 0;
2709 for (i = 0; i < 5; i++)
2710 if (FRAME_X_DISPLAY_INFO (f)->grabbed & (1 << i))
2711 dummy.button = i;
2713 /* Don't allow any geometry request from the user. */
2714 XtSetArg (av[ac], XtNgeometry, 0); ac++;
2715 XtSetValues (menu, av, ac);
2717 /* Display the menu. */
2718 lw_popup_menu (menu, (XEvent *) &dummy);
2719 popup_activated_flag = 1;
2722 int fact = 4 * sizeof (LWLIB_ID);
2723 int specpdl_count = SPECPDL_INDEX ();
2724 record_unwind_protect (pop_down_menu,
2725 Fcons (make_number (menu_id >> (fact)),
2726 make_number (menu_id & ~(-1 << (fact)))));
2728 /* Process events that apply to the menu. */
2729 popup_get_selection ((XEvent *) 0, FRAME_X_DISPLAY_INFO (f), menu_id, 1);
2731 unbind_to (specpdl_count, Qnil);
2735 #endif /* not USE_GTK */
2737 static Lisp_Object
2738 xmenu_show (f, x, y, for_click, keymaps, title, error)
2739 FRAME_PTR f;
2740 int x;
2741 int y;
2742 int for_click;
2743 int keymaps;
2744 Lisp_Object title;
2745 char **error;
2747 int i;
2748 widget_value *wv, *save_wv = 0, *first_wv = 0, *prev_wv = 0;
2749 widget_value **submenu_stack
2750 = (widget_value **) alloca (menu_items_used * sizeof (widget_value *));
2751 Lisp_Object *subprefix_stack
2752 = (Lisp_Object *) alloca (menu_items_used * sizeof (Lisp_Object));
2753 int submenu_depth = 0;
2755 int first_pane;
2757 if (! FRAME_X_P (f))
2758 abort ();
2760 *error = NULL;
2762 if (menu_items_used <= MENU_ITEMS_PANE_LENGTH)
2764 *error = "Empty menu";
2765 return Qnil;
2768 /* Create a tree of widget_value objects
2769 representing the panes and their items. */
2770 wv = xmalloc_widget_value ();
2771 wv->name = "menu";
2772 wv->value = 0;
2773 wv->enabled = 1;
2774 wv->button_type = BUTTON_TYPE_NONE;
2775 wv->help =Qnil;
2776 first_wv = wv;
2777 first_pane = 1;
2779 /* Loop over all panes and items, filling in the tree. */
2780 i = 0;
2781 while (i < menu_items_used)
2783 if (EQ (XVECTOR (menu_items)->contents[i], Qnil))
2785 submenu_stack[submenu_depth++] = save_wv;
2786 save_wv = prev_wv;
2787 prev_wv = 0;
2788 first_pane = 1;
2789 i++;
2791 else if (EQ (XVECTOR (menu_items)->contents[i], Qlambda))
2793 prev_wv = save_wv;
2794 save_wv = submenu_stack[--submenu_depth];
2795 first_pane = 0;
2796 i++;
2798 else if (EQ (XVECTOR (menu_items)->contents[i], Qt)
2799 && submenu_depth != 0)
2800 i += MENU_ITEMS_PANE_LENGTH;
2801 /* Ignore a nil in the item list.
2802 It's meaningful only for dialog boxes. */
2803 else if (EQ (XVECTOR (menu_items)->contents[i], Qquote))
2804 i += 1;
2805 else if (EQ (XVECTOR (menu_items)->contents[i], Qt))
2807 /* Create a new pane. */
2808 Lisp_Object pane_name, prefix;
2809 char *pane_string;
2811 pane_name = AREF (menu_items, i + MENU_ITEMS_PANE_NAME);
2812 prefix = AREF (menu_items, i + MENU_ITEMS_PANE_PREFIX);
2814 #ifndef HAVE_MULTILINGUAL_MENU
2815 if (STRINGP (pane_name) && STRING_MULTIBYTE (pane_name))
2817 pane_name = ENCODE_MENU_STRING (pane_name);
2818 AREF (menu_items, i + MENU_ITEMS_PANE_NAME) = pane_name;
2820 #endif
2821 pane_string = (NILP (pane_name)
2822 ? "" : (char *) SDATA (pane_name));
2823 /* If there is just one top-level pane, put all its items directly
2824 under the top-level menu. */
2825 if (menu_items_n_panes == 1)
2826 pane_string = "";
2828 /* If the pane has a meaningful name,
2829 make the pane a top-level menu item
2830 with its items as a submenu beneath it. */
2831 if (!keymaps && strcmp (pane_string, ""))
2833 wv = xmalloc_widget_value ();
2834 if (save_wv)
2835 save_wv->next = wv;
2836 else
2837 first_wv->contents = wv;
2838 wv->name = pane_string;
2839 if (keymaps && !NILP (prefix))
2840 wv->name++;
2841 wv->value = 0;
2842 wv->enabled = 1;
2843 wv->button_type = BUTTON_TYPE_NONE;
2844 wv->help = Qnil;
2845 save_wv = wv;
2846 prev_wv = 0;
2848 else if (first_pane)
2850 save_wv = wv;
2851 prev_wv = 0;
2853 first_pane = 0;
2854 i += MENU_ITEMS_PANE_LENGTH;
2856 else
2858 /* Create a new item within current pane. */
2859 Lisp_Object item_name, enable, descrip, def, type, selected, help;
2860 item_name = AREF (menu_items, i + MENU_ITEMS_ITEM_NAME);
2861 enable = AREF (menu_items, i + MENU_ITEMS_ITEM_ENABLE);
2862 descrip = AREF (menu_items, i + MENU_ITEMS_ITEM_EQUIV_KEY);
2863 def = AREF (menu_items, i + MENU_ITEMS_ITEM_DEFINITION);
2864 type = AREF (menu_items, i + MENU_ITEMS_ITEM_TYPE);
2865 selected = AREF (menu_items, i + MENU_ITEMS_ITEM_SELECTED);
2866 help = AREF (menu_items, i + MENU_ITEMS_ITEM_HELP);
2868 #ifndef HAVE_MULTILINGUAL_MENU
2869 if (STRINGP (item_name) && STRING_MULTIBYTE (item_name))
2871 item_name = ENCODE_MENU_STRING (item_name);
2872 AREF (menu_items, i + MENU_ITEMS_ITEM_NAME) = item_name;
2875 if (STRINGP (descrip) && STRING_MULTIBYTE (descrip))
2877 descrip = ENCODE_MENU_STRING (descrip);
2878 AREF (menu_items, i + MENU_ITEMS_ITEM_EQUIV_KEY) = descrip;
2880 #endif /* not HAVE_MULTILINGUAL_MENU */
2882 wv = xmalloc_widget_value ();
2883 if (prev_wv)
2884 prev_wv->next = wv;
2885 else
2886 save_wv->contents = wv;
2887 wv->name = (char *) SDATA (item_name);
2888 if (!NILP (descrip))
2889 wv->key = (char *) SDATA (descrip);
2890 wv->value = 0;
2891 /* If this item has a null value,
2892 make the call_data null so that it won't display a box
2893 when the mouse is on it. */
2894 wv->call_data
2895 = (!NILP (def) ? (void *) &XVECTOR (menu_items)->contents[i] : 0);
2896 wv->enabled = !NILP (enable);
2898 if (NILP (type))
2899 wv->button_type = BUTTON_TYPE_NONE;
2900 else if (EQ (type, QCtoggle))
2901 wv->button_type = BUTTON_TYPE_TOGGLE;
2902 else if (EQ (type, QCradio))
2903 wv->button_type = BUTTON_TYPE_RADIO;
2904 else
2905 abort ();
2907 wv->selected = !NILP (selected);
2909 if (! STRINGP (help))
2910 help = Qnil;
2912 wv->help = help;
2914 prev_wv = wv;
2916 i += MENU_ITEMS_ITEM_LENGTH;
2920 /* Deal with the title, if it is non-nil. */
2921 if (!NILP (title))
2923 widget_value *wv_title = xmalloc_widget_value ();
2924 widget_value *wv_sep1 = xmalloc_widget_value ();
2925 widget_value *wv_sep2 = xmalloc_widget_value ();
2927 wv_sep2->name = "--";
2928 wv_sep2->next = first_wv->contents;
2929 wv_sep2->help = Qnil;
2931 wv_sep1->name = "--";
2932 wv_sep1->next = wv_sep2;
2933 wv_sep1->help = Qnil;
2935 #ifndef HAVE_MULTILINGUAL_MENU
2936 if (STRING_MULTIBYTE (title))
2937 title = ENCODE_MENU_STRING (title);
2938 #endif
2940 wv_title->name = (char *) SDATA (title);
2941 wv_title->enabled = TRUE;
2942 wv_title->button_type = BUTTON_TYPE_NONE;
2943 wv_title->next = wv_sep1;
2944 wv_title->help = Qnil;
2945 first_wv->contents = wv_title;
2948 /* No selection has been chosen yet. */
2949 menu_item_selection = 0;
2951 /* Actually create and show the menu until popped down. */
2952 create_and_show_popup_menu (f, first_wv, x, y, for_click);
2954 /* Free the widget_value objects we used to specify the contents. */
2955 free_menubar_widget_value_tree (first_wv);
2957 /* Find the selected item, and its pane, to return
2958 the proper value. */
2959 if (menu_item_selection != 0)
2961 Lisp_Object prefix, entry;
2963 prefix = entry = Qnil;
2964 i = 0;
2965 while (i < menu_items_used)
2967 if (EQ (XVECTOR (menu_items)->contents[i], Qnil))
2969 subprefix_stack[submenu_depth++] = prefix;
2970 prefix = entry;
2971 i++;
2973 else if (EQ (XVECTOR (menu_items)->contents[i], Qlambda))
2975 prefix = subprefix_stack[--submenu_depth];
2976 i++;
2978 else if (EQ (XVECTOR (menu_items)->contents[i], Qt))
2980 prefix
2981 = XVECTOR (menu_items)->contents[i + MENU_ITEMS_PANE_PREFIX];
2982 i += MENU_ITEMS_PANE_LENGTH;
2984 /* Ignore a nil in the item list.
2985 It's meaningful only for dialog boxes. */
2986 else if (EQ (XVECTOR (menu_items)->contents[i], Qquote))
2987 i += 1;
2988 else
2990 entry
2991 = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_VALUE];
2992 if (menu_item_selection == &XVECTOR (menu_items)->contents[i])
2994 if (keymaps != 0)
2996 int j;
2998 entry = Fcons (entry, Qnil);
2999 if (!NILP (prefix))
3000 entry = Fcons (prefix, entry);
3001 for (j = submenu_depth - 1; j >= 0; j--)
3002 if (!NILP (subprefix_stack[j]))
3003 entry = Fcons (subprefix_stack[j], entry);
3005 return entry;
3007 i += MENU_ITEMS_ITEM_LENGTH;
3011 else if (!for_click)
3012 /* Make "Cancel" equivalent to C-g. */
3013 Fsignal (Qquit, Qnil);
3015 return Qnil;
3018 #ifdef USE_GTK
3019 static void
3020 dialog_selection_callback (widget, client_data)
3021 GtkWidget *widget;
3022 gpointer client_data;
3024 /* The EMACS_INT cast avoids a warning. There's no problem
3025 as long as pointers have enough bits to hold small integers. */
3026 if ((int) (EMACS_INT) client_data != -1)
3027 menu_item_selection = (Lisp_Object *) client_data;
3029 popup_activated_flag = 0;
3032 /* Pop up the dialog for frame F defined by FIRST_WV and loop until the
3033 dialog pops down.
3034 menu_item_selection will be set to the selection. */
3035 static void
3036 create_and_show_dialog (f, first_wv)
3037 FRAME_PTR f;
3038 widget_value *first_wv;
3040 GtkWidget *menu;
3042 if (! FRAME_X_P (f))
3043 abort ();
3045 menu = xg_create_widget ("dialog", first_wv->name, f, first_wv,
3046 G_CALLBACK (dialog_selection_callback),
3047 G_CALLBACK (popup_deactivate_callback),
3050 if (menu)
3052 int specpdl_count = SPECPDL_INDEX ();
3053 record_unwind_protect (pop_down_menu, make_save_value (menu, 0));
3055 /* Display the menu. */
3056 gtk_widget_show_all (menu);
3058 /* Process events that apply to the menu. */
3059 popup_widget_loop (1, menu);
3061 unbind_to (specpdl_count, Qnil);
3065 #else /* not USE_GTK */
3066 static void
3067 dialog_selection_callback (widget, id, client_data)
3068 Widget widget;
3069 LWLIB_ID id;
3070 XtPointer client_data;
3072 /* The EMACS_INT cast avoids a warning. There's no problem
3073 as long as pointers have enough bits to hold small integers. */
3074 if ((int) (EMACS_INT) client_data != -1)
3075 menu_item_selection = (Lisp_Object *) client_data;
3077 BLOCK_INPUT;
3078 lw_destroy_all_widgets (id);
3079 UNBLOCK_INPUT;
3080 popup_activated_flag = 0;
3084 /* Pop up the dialog for frame F defined by FIRST_WV and loop until the
3085 dialog pops down.
3086 menu_item_selection will be set to the selection. */
3087 static void
3088 create_and_show_dialog (f, first_wv)
3089 FRAME_PTR f;
3090 widget_value *first_wv;
3092 LWLIB_ID dialog_id;
3094 if (!FRAME_X_P (f))
3095 abort();
3097 dialog_id = widget_id_tick++;
3098 lw_create_widget (first_wv->name, "dialog", dialog_id, first_wv,
3099 f->output_data.x->widget, 1, 0,
3100 dialog_selection_callback, 0, 0);
3101 lw_modify_all_widgets (dialog_id, first_wv->contents, True);
3103 /* Display the dialog box. */
3104 lw_pop_up_all_widgets (dialog_id);
3105 popup_activated_flag = 1;
3107 /* Process events that apply to the dialog box.
3108 Also handle timers. */
3110 int count = SPECPDL_INDEX ();
3111 int fact = 4 * sizeof (LWLIB_ID);
3113 /* xdialog_show_unwind is responsible for popping the dialog box down. */
3114 record_unwind_protect (pop_down_menu,
3115 Fcons (make_number (dialog_id >> (fact)),
3116 make_number (dialog_id & ~(-1 << (fact)))));
3118 popup_get_selection ((XEvent *) 0, FRAME_X_DISPLAY_INFO (f),
3119 dialog_id, 1);
3121 unbind_to (count, Qnil);
3125 #endif /* not USE_GTK */
3127 static char * button_names [] = {
3128 "button1", "button2", "button3", "button4", "button5",
3129 "button6", "button7", "button8", "button9", "button10" };
3131 static Lisp_Object
3132 xdialog_show (f, keymaps, title, header, error_name)
3133 FRAME_PTR f;
3134 int keymaps;
3135 Lisp_Object title, header;
3136 char **error_name;
3138 int i, nb_buttons=0;
3139 char dialog_name[6];
3141 widget_value *wv, *first_wv = 0, *prev_wv = 0;
3143 /* Number of elements seen so far, before boundary. */
3144 int left_count = 0;
3145 /* 1 means we've seen the boundary between left-hand elts and right-hand. */
3146 int boundary_seen = 0;
3148 if (! FRAME_X_P (f))
3149 abort ();
3151 *error_name = NULL;
3153 if (menu_items_n_panes > 1)
3155 *error_name = "Multiple panes in dialog box";
3156 return Qnil;
3159 /* Create a tree of widget_value objects
3160 representing the text label and buttons. */
3162 Lisp_Object pane_name, prefix;
3163 char *pane_string;
3164 pane_name = XVECTOR (menu_items)->contents[MENU_ITEMS_PANE_NAME];
3165 prefix = XVECTOR (menu_items)->contents[MENU_ITEMS_PANE_PREFIX];
3166 pane_string = (NILP (pane_name)
3167 ? "" : (char *) SDATA (pane_name));
3168 prev_wv = xmalloc_widget_value ();
3169 prev_wv->value = pane_string;
3170 if (keymaps && !NILP (prefix))
3171 prev_wv->name++;
3172 prev_wv->enabled = 1;
3173 prev_wv->name = "message";
3174 prev_wv->help = Qnil;
3175 first_wv = prev_wv;
3177 /* Loop over all panes and items, filling in the tree. */
3178 i = MENU_ITEMS_PANE_LENGTH;
3179 while (i < menu_items_used)
3182 /* Create a new item within current pane. */
3183 Lisp_Object item_name, enable, descrip;
3184 item_name = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_NAME];
3185 enable = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_ENABLE];
3186 descrip
3187 = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_EQUIV_KEY];
3189 if (NILP (item_name))
3191 free_menubar_widget_value_tree (first_wv);
3192 *error_name = "Submenu in dialog items";
3193 return Qnil;
3195 if (EQ (item_name, Qquote))
3197 /* This is the boundary between left-side elts
3198 and right-side elts. Stop incrementing right_count. */
3199 boundary_seen = 1;
3200 i++;
3201 continue;
3203 if (nb_buttons >= 9)
3205 free_menubar_widget_value_tree (first_wv);
3206 *error_name = "Too many dialog items";
3207 return Qnil;
3210 wv = xmalloc_widget_value ();
3211 prev_wv->next = wv;
3212 wv->name = (char *) button_names[nb_buttons];
3213 if (!NILP (descrip))
3214 wv->key = (char *) SDATA (descrip);
3215 wv->value = (char *) SDATA (item_name);
3216 wv->call_data = (void *) &XVECTOR (menu_items)->contents[i];
3217 wv->enabled = !NILP (enable);
3218 wv->help = Qnil;
3219 prev_wv = wv;
3221 if (! boundary_seen)
3222 left_count++;
3224 nb_buttons++;
3225 i += MENU_ITEMS_ITEM_LENGTH;
3228 /* If the boundary was not specified,
3229 by default put half on the left and half on the right. */
3230 if (! boundary_seen)
3231 left_count = nb_buttons - nb_buttons / 2;
3233 wv = xmalloc_widget_value ();
3234 wv->name = dialog_name;
3235 wv->help = Qnil;
3237 /* Frame title: 'Q' = Question, 'I' = Information.
3238 Can also have 'E' = Error if, one day, we want
3239 a popup for errors. */
3240 if (NILP(header))
3241 dialog_name[0] = 'Q';
3242 else
3243 dialog_name[0] = 'I';
3245 /* Dialog boxes use a really stupid name encoding
3246 which specifies how many buttons to use
3247 and how many buttons are on the right. */
3248 dialog_name[1] = '0' + nb_buttons;
3249 dialog_name[2] = 'B';
3250 dialog_name[3] = 'R';
3251 /* Number of buttons to put on the right. */
3252 dialog_name[4] = '0' + nb_buttons - left_count;
3253 dialog_name[5] = 0;
3254 wv->contents = first_wv;
3255 first_wv = wv;
3258 /* No selection has been chosen yet. */
3259 menu_item_selection = 0;
3261 /* Actually create and show the dialog. */
3262 create_and_show_dialog (f, first_wv);
3264 /* Free the widget_value objects we used to specify the contents. */
3265 free_menubar_widget_value_tree (first_wv);
3267 /* Find the selected item, and its pane, to return
3268 the proper value. */
3269 if (menu_item_selection != 0)
3271 Lisp_Object prefix;
3273 prefix = Qnil;
3274 i = 0;
3275 while (i < menu_items_used)
3277 Lisp_Object entry;
3279 if (EQ (XVECTOR (menu_items)->contents[i], Qt))
3281 prefix
3282 = XVECTOR (menu_items)->contents[i + MENU_ITEMS_PANE_PREFIX];
3283 i += MENU_ITEMS_PANE_LENGTH;
3285 else if (EQ (XVECTOR (menu_items)->contents[i], Qquote))
3287 /* This is the boundary between left-side elts and
3288 right-side elts. */
3289 ++i;
3291 else
3293 entry
3294 = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_VALUE];
3295 if (menu_item_selection == &XVECTOR (menu_items)->contents[i])
3297 if (keymaps != 0)
3299 entry = Fcons (entry, Qnil);
3300 if (!NILP (prefix))
3301 entry = Fcons (prefix, entry);
3303 return entry;
3305 i += MENU_ITEMS_ITEM_LENGTH;
3309 else
3310 /* Make "Cancel" equivalent to C-g. */
3311 Fsignal (Qquit, Qnil);
3313 return Qnil;
3316 #else /* not USE_X_TOOLKIT && not USE_GTK */
3318 /* The frame of the last activated non-toolkit menu bar.
3319 Used to generate menu help events. */
3321 static struct frame *menu_help_frame;
3324 /* Show help HELP_STRING, or clear help if HELP_STRING is null.
3326 PANE is the pane number, and ITEM is the menu item number in
3327 the menu (currently not used).
3329 This cannot be done with generating a HELP_EVENT because
3330 XMenuActivate contains a loop that doesn't let Emacs process
3331 keyboard events. */
3333 static void
3334 menu_help_callback (help_string, pane, item)
3335 char *help_string;
3336 int pane, item;
3338 extern Lisp_Object Qmenu_item;
3339 Lisp_Object *first_item;
3340 Lisp_Object pane_name;
3341 Lisp_Object menu_object;
3343 first_item = XVECTOR (menu_items)->contents;
3344 if (EQ (first_item[0], Qt))
3345 pane_name = first_item[MENU_ITEMS_PANE_NAME];
3346 else if (EQ (first_item[0], Qquote))
3347 /* This shouldn't happen, see xmenu_show. */
3348 pane_name = empty_string;
3349 else
3350 pane_name = first_item[MENU_ITEMS_ITEM_NAME];
3352 /* (menu-item MENU-NAME PANE-NUMBER) */
3353 menu_object = Fcons (Qmenu_item,
3354 Fcons (pane_name,
3355 Fcons (make_number (pane), Qnil)));
3356 show_help_echo (help_string ? build_string (help_string) : Qnil,
3357 Qnil, menu_object, make_number (item), 1);
3360 static Lisp_Object
3361 pop_down_menu (arg)
3362 Lisp_Object arg;
3364 struct Lisp_Save_Value *p1 = XSAVE_VALUE (Fcar (arg));
3365 struct Lisp_Save_Value *p2 = XSAVE_VALUE (Fcdr (arg));
3367 FRAME_PTR f = p1->pointer;
3368 XMenu *menu = p2->pointer;
3370 BLOCK_INPUT;
3371 #ifndef MSDOS
3372 XUngrabPointer (FRAME_X_DISPLAY (f), CurrentTime);
3373 XUngrabKeyboard (FRAME_X_DISPLAY (f), CurrentTime);
3374 #endif
3375 XMenuDestroy (FRAME_X_DISPLAY (f), menu);
3377 #ifdef HAVE_X_WINDOWS
3378 /* Assume the mouse has moved out of the X window.
3379 If it has actually moved in, we will get an EnterNotify. */
3380 x_mouse_leave (FRAME_X_DISPLAY_INFO (f));
3382 /* State that no mouse buttons are now held.
3383 (The oldXMenu code doesn't track this info for us.)
3384 That is not necessarily true, but the fiction leads to reasonable
3385 results, and it is a pain to ask which are actually held now. */
3386 FRAME_X_DISPLAY_INFO (f)->grabbed = 0;
3388 #endif /* HAVE_X_WINDOWS */
3390 UNBLOCK_INPUT;
3392 return Qnil;
3396 static Lisp_Object
3397 xmenu_show (f, x, y, for_click, keymaps, title, error)
3398 FRAME_PTR f;
3399 int x, y;
3400 int for_click;
3401 int keymaps;
3402 Lisp_Object title;
3403 char **error;
3405 Window root;
3406 XMenu *menu;
3407 int pane, selidx, lpane, status;
3408 Lisp_Object entry, pane_prefix;
3409 char *datap;
3410 int ulx, uly, width, height;
3411 int dispwidth, dispheight;
3412 int i, j, lines, maxlines;
3413 int maxwidth;
3414 int dummy_int;
3415 unsigned int dummy_uint;
3416 int specpdl_count = SPECPDL_INDEX ();
3418 if (! FRAME_X_P (f))
3419 abort ();
3421 *error = 0;
3422 if (menu_items_n_panes == 0)
3423 return Qnil;
3425 if (menu_items_used <= MENU_ITEMS_PANE_LENGTH)
3427 *error = "Empty menu";
3428 return Qnil;
3431 /* Figure out which root window F is on. */
3432 XGetGeometry (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), &root,
3433 &dummy_int, &dummy_int, &dummy_uint, &dummy_uint,
3434 &dummy_uint, &dummy_uint);
3436 /* Make the menu on that window. */
3437 menu = XMenuCreate (FRAME_X_DISPLAY (f), root, "emacs");
3438 if (menu == NULL)
3440 *error = "Can't create menu";
3441 return Qnil;
3444 /* Don't GC while we prepare and show the menu,
3445 because we give the oldxmenu library pointers to the
3446 contents of strings. */
3447 inhibit_garbage_collection ();
3449 #ifdef HAVE_X_WINDOWS
3450 /* Adjust coordinates to relative to the outer (window manager) window. */
3451 x += FRAME_OUTER_TO_INNER_DIFF_X (f);
3452 y += FRAME_OUTER_TO_INNER_DIFF_Y (f);
3453 #endif /* HAVE_X_WINDOWS */
3455 /* Adjust coordinates to be root-window-relative. */
3456 x += f->left_pos;
3457 y += f->top_pos;
3459 /* Create all the necessary panes and their items. */
3460 maxlines = lines = i = 0;
3461 while (i < menu_items_used)
3463 if (EQ (XVECTOR (menu_items)->contents[i], Qt))
3465 /* Create a new pane. */
3466 Lisp_Object pane_name, prefix;
3467 char *pane_string;
3469 maxlines = max (maxlines, lines);
3470 lines = 0;
3471 pane_name = XVECTOR (menu_items)->contents[i + MENU_ITEMS_PANE_NAME];
3472 prefix = XVECTOR (menu_items)->contents[i + MENU_ITEMS_PANE_PREFIX];
3473 pane_string = (NILP (pane_name)
3474 ? "" : (char *) SDATA (pane_name));
3475 if (keymaps && !NILP (prefix))
3476 pane_string++;
3478 lpane = XMenuAddPane (FRAME_X_DISPLAY (f), menu, pane_string, TRUE);
3479 if (lpane == XM_FAILURE)
3481 XMenuDestroy (FRAME_X_DISPLAY (f), menu);
3482 *error = "Can't create pane";
3483 return Qnil;
3485 i += MENU_ITEMS_PANE_LENGTH;
3487 /* Find the width of the widest item in this pane. */
3488 maxwidth = 0;
3489 j = i;
3490 while (j < menu_items_used)
3492 Lisp_Object item;
3493 item = XVECTOR (menu_items)->contents[j];
3494 if (EQ (item, Qt))
3495 break;
3496 if (NILP (item))
3498 j++;
3499 continue;
3501 width = SBYTES (item);
3502 if (width > maxwidth)
3503 maxwidth = width;
3505 j += MENU_ITEMS_ITEM_LENGTH;
3508 /* Ignore a nil in the item list.
3509 It's meaningful only for dialog boxes. */
3510 else if (EQ (XVECTOR (menu_items)->contents[i], Qquote))
3511 i += 1;
3512 else
3514 /* Create a new item within current pane. */
3515 Lisp_Object item_name, enable, descrip, help;
3516 unsigned char *item_data;
3517 char *help_string;
3519 item_name = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_NAME];
3520 enable = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_ENABLE];
3521 descrip
3522 = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_EQUIV_KEY];
3523 help = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_HELP];
3524 help_string = STRINGP (help) ? SDATA (help) : NULL;
3526 if (!NILP (descrip))
3528 int gap = maxwidth - SBYTES (item_name);
3529 #ifdef C_ALLOCA
3530 Lisp_Object spacer;
3531 spacer = Fmake_string (make_number (gap), make_number (' '));
3532 item_name = concat2 (item_name, spacer);
3533 item_name = concat2 (item_name, descrip);
3534 item_data = SDATA (item_name);
3535 #else
3536 /* if alloca is fast, use that to make the space,
3537 to reduce gc needs. */
3538 item_data
3539 = (unsigned char *) alloca (maxwidth
3540 + SBYTES (descrip) + 1);
3541 bcopy (SDATA (item_name), item_data,
3542 SBYTES (item_name));
3543 for (j = SCHARS (item_name); j < maxwidth; j++)
3544 item_data[j] = ' ';
3545 bcopy (SDATA (descrip), item_data + j,
3546 SBYTES (descrip));
3547 item_data[j + SBYTES (descrip)] = 0;
3548 #endif
3550 else
3551 item_data = SDATA (item_name);
3553 if (XMenuAddSelection (FRAME_X_DISPLAY (f),
3554 menu, lpane, 0, item_data,
3555 !NILP (enable), help_string)
3556 == XM_FAILURE)
3558 XMenuDestroy (FRAME_X_DISPLAY (f), menu);
3559 *error = "Can't add selection to menu";
3560 return Qnil;
3562 i += MENU_ITEMS_ITEM_LENGTH;
3563 lines++;
3567 maxlines = max (maxlines, lines);
3569 /* All set and ready to fly. */
3570 XMenuRecompute (FRAME_X_DISPLAY (f), menu);
3571 dispwidth = DisplayWidth (FRAME_X_DISPLAY (f), FRAME_X_SCREEN_NUMBER (f));
3572 dispheight = DisplayHeight (FRAME_X_DISPLAY (f), FRAME_X_SCREEN_NUMBER (f));
3573 x = min (x, dispwidth);
3574 y = min (y, dispheight);
3575 x = max (x, 1);
3576 y = max (y, 1);
3577 XMenuLocate (FRAME_X_DISPLAY (f), menu, 0, 0, x, y,
3578 &ulx, &uly, &width, &height);
3579 if (ulx+width > dispwidth)
3581 x -= (ulx + width) - dispwidth;
3582 ulx = dispwidth - width;
3584 if (uly+height > dispheight)
3586 y -= (uly + height) - dispheight;
3587 uly = dispheight - height;
3589 if (ulx < 0) x -= ulx;
3590 if (uly < 0) y -= uly;
3592 if (! for_click)
3594 /* If position was not given by a mouse click, adjust so upper left
3595 corner of the menu as a whole ends up at given coordinates. This
3596 is what x-popup-menu says in its documentation. */
3597 x += width/2;
3598 y += 1.5*height/(maxlines+2);
3601 XMenuSetAEQ (menu, TRUE);
3602 XMenuSetFreeze (menu, TRUE);
3603 pane = selidx = 0;
3605 #ifndef MSDOS
3606 XMenuActivateSetWaitFunction (x_menu_wait_for_event, FRAME_X_DISPLAY (f));
3607 #endif
3609 record_unwind_protect (pop_down_menu,
3610 Fcons (make_save_value (f, 0),
3611 make_save_value (menu, 0)));
3613 /* Help display under X won't work because XMenuActivate contains
3614 a loop that doesn't give Emacs a chance to process it. */
3615 menu_help_frame = f;
3616 status = XMenuActivate (FRAME_X_DISPLAY (f), menu, &pane, &selidx,
3617 x, y, ButtonReleaseMask, &datap,
3618 menu_help_callback);
3620 switch (status)
3622 case XM_SUCCESS:
3623 #ifdef XDEBUG
3624 fprintf (stderr, "pane= %d line = %d\n", panes, selidx);
3625 #endif
3627 /* Find the item number SELIDX in pane number PANE. */
3628 i = 0;
3629 while (i < menu_items_used)
3631 if (EQ (XVECTOR (menu_items)->contents[i], Qt))
3633 if (pane == 0)
3634 pane_prefix
3635 = XVECTOR (menu_items)->contents[i + MENU_ITEMS_PANE_PREFIX];
3636 pane--;
3637 i += MENU_ITEMS_PANE_LENGTH;
3639 else
3641 if (pane == -1)
3643 if (selidx == 0)
3645 entry
3646 = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_VALUE];
3647 if (keymaps != 0)
3649 entry = Fcons (entry, Qnil);
3650 if (!NILP (pane_prefix))
3651 entry = Fcons (pane_prefix, entry);
3653 break;
3655 selidx--;
3657 i += MENU_ITEMS_ITEM_LENGTH;
3660 break;
3662 case XM_FAILURE:
3663 *error = "Can't activate menu";
3664 case XM_IA_SELECT:
3665 entry = Qnil;
3666 break;
3667 case XM_NO_SELECT:
3668 /* Make "Cancel" equivalent to C-g unless FOR_CLICK (which means
3669 the menu was invoked with a mouse event as POSITION). */
3670 if (! for_click)
3671 Fsignal (Qquit, Qnil);
3672 entry = Qnil;
3673 break;
3676 unbind_to (specpdl_count, Qnil);
3678 return entry;
3681 #endif /* not USE_X_TOOLKIT */
3683 #endif /* HAVE_MENUS */
3685 void
3686 syms_of_xmenu ()
3688 staticpro (&menu_items);
3689 menu_items = Qnil;
3690 menu_items_inuse = Qnil;
3692 Qdebug_on_next_call = intern ("debug-on-next-call");
3693 staticpro (&Qdebug_on_next_call);
3695 DEFVAR_LISP ("menu-updating-frame", &Vmenu_updating_frame,
3696 doc: /* Frame for which we are updating a menu.
3697 The enable predicate for a menu command should check this variable. */);
3698 Vmenu_updating_frame = Qnil;
3700 #ifdef USE_X_TOOLKIT
3701 widget_id_tick = (1<<16);
3702 next_menubar_widget_id = 1;
3703 #endif
3705 defsubr (&Sx_popup_menu);
3706 #ifdef HAVE_MENUS
3707 defsubr (&Sx_popup_dialog);
3708 #endif
3711 /* arch-tag: 92ea573c-398e-496e-ac73-2436f7d63242
3712 (do not change this comment) */