Fix F10 behaviour. (Reported by Bernard Adrian.)
[emacs.git] / src / xmenu.c
blob1650222ae843299ae48892fdfe594f69c36e2672
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));
330 return Qnil;
333 /* Push the whole state of menu_items processing onto the specpdl.
334 It will be restored when the specpdl is unwound. */
336 static void
337 save_menu_items ()
339 Lisp_Object saved = list4 (!NILP (menu_items_inuse) ? menu_items : Qnil,
340 make_number (menu_items_used),
341 make_number (menu_items_n_panes),
342 make_number (menu_items_submenu_depth));
343 record_unwind_protect (restore_menu_items, saved);
344 menu_items_inuse = Qnil;
345 menu_items = Qnil;
348 /* Make the menu_items vector twice as large. */
350 static void
351 grow_menu_items ()
353 Lisp_Object old;
354 int old_size = menu_items_allocated;
355 old = menu_items;
357 menu_items_allocated *= 2;
359 menu_items = Fmake_vector (make_number (menu_items_allocated), Qnil);
360 bcopy (XVECTOR (old)->contents, XVECTOR (menu_items)->contents,
361 old_size * sizeof (Lisp_Object));
364 /* Begin a submenu. */
366 static void
367 push_submenu_start ()
369 if (menu_items_used + 1 > menu_items_allocated)
370 grow_menu_items ();
372 XVECTOR (menu_items)->contents[menu_items_used++] = Qnil;
373 menu_items_submenu_depth++;
376 /* End a submenu. */
378 static void
379 push_submenu_end ()
381 if (menu_items_used + 1 > menu_items_allocated)
382 grow_menu_items ();
384 XVECTOR (menu_items)->contents[menu_items_used++] = Qlambda;
385 menu_items_submenu_depth--;
388 /* Indicate boundary between left and right. */
390 static void
391 push_left_right_boundary ()
393 if (menu_items_used + 1 > menu_items_allocated)
394 grow_menu_items ();
396 XVECTOR (menu_items)->contents[menu_items_used++] = Qquote;
399 /* Start a new menu pane in menu_items.
400 NAME is the pane name. PREFIX_VEC is a prefix key for this pane. */
402 static void
403 push_menu_pane (name, prefix_vec)
404 Lisp_Object name, prefix_vec;
406 if (menu_items_used + MENU_ITEMS_PANE_LENGTH > menu_items_allocated)
407 grow_menu_items ();
409 if (menu_items_submenu_depth == 0)
410 menu_items_n_panes++;
411 XVECTOR (menu_items)->contents[menu_items_used++] = Qt;
412 XVECTOR (menu_items)->contents[menu_items_used++] = name;
413 XVECTOR (menu_items)->contents[menu_items_used++] = prefix_vec;
416 /* Push one menu item into the current pane. NAME is the string to
417 display. ENABLE if non-nil means this item can be selected. KEY
418 is the key generated by choosing this item, or nil if this item
419 doesn't really have a definition. DEF is the definition of this
420 item. EQUIV is the textual description of the keyboard equivalent
421 for this item (or nil if none). TYPE is the type of this menu
422 item, one of nil, `toggle' or `radio'. */
424 static void
425 push_menu_item (name, enable, key, def, equiv, type, selected, help)
426 Lisp_Object name, enable, key, def, equiv, type, selected, help;
428 if (menu_items_used + MENU_ITEMS_ITEM_LENGTH > menu_items_allocated)
429 grow_menu_items ();
431 XVECTOR (menu_items)->contents[menu_items_used++] = name;
432 XVECTOR (menu_items)->contents[menu_items_used++] = enable;
433 XVECTOR (menu_items)->contents[menu_items_used++] = key;
434 XVECTOR (menu_items)->contents[menu_items_used++] = equiv;
435 XVECTOR (menu_items)->contents[menu_items_used++] = def;
436 XVECTOR (menu_items)->contents[menu_items_used++] = type;
437 XVECTOR (menu_items)->contents[menu_items_used++] = selected;
438 XVECTOR (menu_items)->contents[menu_items_used++] = help;
441 /* Look through KEYMAPS, a vector of keymaps that is NMAPS long,
442 and generate menu panes for them in menu_items.
443 If NOTREAL is nonzero,
444 don't bother really computing whether an item is enabled. */
446 static void
447 keymap_panes (keymaps, nmaps, notreal)
448 Lisp_Object *keymaps;
449 int nmaps;
450 int notreal;
452 int mapno;
454 init_menu_items ();
456 /* Loop over the given keymaps, making a pane for each map.
457 But don't make a pane that is empty--ignore that map instead.
458 P is the number of panes we have made so far. */
459 for (mapno = 0; mapno < nmaps; mapno++)
460 single_keymap_panes (keymaps[mapno],
461 Fkeymap_prompt (keymaps[mapno]), Qnil, notreal, 10);
463 finish_menu_items ();
466 /* Args passed between single_keymap_panes and single_menu_item. */
467 struct skp
469 Lisp_Object pending_maps;
470 int maxdepth, notreal;
471 int notbuttons;
474 static void single_menu_item P_ ((Lisp_Object, Lisp_Object, Lisp_Object,
475 void *));
477 /* This is a recursive subroutine of keymap_panes.
478 It handles one keymap, KEYMAP.
479 The other arguments are passed along
480 or point to local variables of the previous function.
481 If NOTREAL is nonzero, only check for equivalent key bindings, don't
482 evaluate expressions in menu items and don't make any menu.
484 If we encounter submenus deeper than MAXDEPTH levels, ignore them. */
486 static void
487 single_keymap_panes (keymap, pane_name, prefix, notreal, maxdepth)
488 Lisp_Object keymap;
489 Lisp_Object pane_name;
490 Lisp_Object prefix;
491 int notreal;
492 int maxdepth;
494 struct skp skp;
495 struct gcpro gcpro1;
497 skp.pending_maps = Qnil;
498 skp.maxdepth = maxdepth;
499 skp.notreal = notreal;
500 skp.notbuttons = 0;
502 if (maxdepth <= 0)
503 return;
505 push_menu_pane (pane_name, prefix);
507 #ifndef HAVE_BOXES
508 /* Remember index for first item in this pane so we can go back and
509 add a prefix when (if) we see the first button. After that, notbuttons
510 is set to 0, to mark that we have seen a button and all non button
511 items need a prefix. */
512 skp.notbuttons = menu_items_used;
513 #endif
515 GCPRO1 (skp.pending_maps);
516 map_keymap (keymap, single_menu_item, Qnil, &skp, 1);
517 UNGCPRO;
519 /* Process now any submenus which want to be panes at this level. */
520 while (CONSP (skp.pending_maps))
522 Lisp_Object elt, eltcdr, string;
523 elt = XCAR (skp.pending_maps);
524 eltcdr = XCDR (elt);
525 string = XCAR (eltcdr);
526 /* We no longer discard the @ from the beginning of the string here.
527 Instead, we do this in xmenu_show. */
528 single_keymap_panes (Fcar (elt), string,
529 XCDR (eltcdr), notreal, maxdepth - 1);
530 skp.pending_maps = XCDR (skp.pending_maps);
534 /* This is a subroutine of single_keymap_panes that handles one
535 keymap entry.
536 KEY is a key in a keymap and ITEM is its binding.
537 SKP->PENDING_MAPS_PTR is a list of keymaps waiting to be made into
538 separate panes.
539 If SKP->NOTREAL is nonzero, only check for equivalent key bindings, don't
540 evaluate expressions in menu items and don't make any menu.
541 If we encounter submenus deeper than SKP->MAXDEPTH levels, ignore them.
542 SKP->NOTBUTTONS is only used when simulating toggle boxes and radio
543 buttons. It keeps track of if we have seen a button in this menu or
544 not. */
546 static void
547 single_menu_item (key, item, dummy, skp_v)
548 Lisp_Object key, item, dummy;
549 void *skp_v;
551 Lisp_Object map, item_string, enabled;
552 struct gcpro gcpro1, gcpro2;
553 int res;
554 struct skp *skp = skp_v;
556 /* Parse the menu item and leave the result in item_properties. */
557 GCPRO2 (key, item);
558 res = parse_menu_item (item, skp->notreal, 0);
559 UNGCPRO;
560 if (!res)
561 return; /* Not a menu item. */
563 map = XVECTOR (item_properties)->contents[ITEM_PROPERTY_MAP];
565 if (skp->notreal)
567 /* We don't want to make a menu, just traverse the keymaps to
568 precompute equivalent key bindings. */
569 if (!NILP (map))
570 single_keymap_panes (map, Qnil, key, 1, skp->maxdepth - 1);
571 return;
574 enabled = XVECTOR (item_properties)->contents[ITEM_PROPERTY_ENABLE];
575 item_string = XVECTOR (item_properties)->contents[ITEM_PROPERTY_NAME];
577 if (!NILP (map) && SREF (item_string, 0) == '@')
579 if (!NILP (enabled))
580 /* An enabled separate pane. Remember this to handle it later. */
581 skp->pending_maps = Fcons (Fcons (map, Fcons (item_string, key)),
582 skp->pending_maps);
583 return;
586 #ifndef HAVE_BOXES
587 /* Simulate radio buttons and toggle boxes by putting a prefix in
588 front of them. */
590 Lisp_Object prefix = Qnil;
591 Lisp_Object type = XVECTOR (item_properties)->contents[ITEM_PROPERTY_TYPE];
592 if (!NILP (type))
594 Lisp_Object selected
595 = XVECTOR (item_properties)->contents[ITEM_PROPERTY_SELECTED];
597 if (skp->notbuttons)
598 /* The first button. Line up previous items in this menu. */
600 int index = skp->notbuttons; /* Index for first item this menu. */
601 int submenu = 0;
602 Lisp_Object tem;
603 while (index < menu_items_used)
606 = XVECTOR (menu_items)->contents[index + MENU_ITEMS_ITEM_NAME];
607 if (NILP (tem))
609 index++;
610 submenu++; /* Skip sub menu. */
612 else if (EQ (tem, Qlambda))
614 index++;
615 submenu--; /* End sub menu. */
617 else if (EQ (tem, Qt))
618 index += 3; /* Skip new pane marker. */
619 else if (EQ (tem, Qquote))
620 index++; /* Skip a left, right divider. */
621 else
623 if (!submenu && SREF (tem, 0) != '\0'
624 && SREF (tem, 0) != '-')
625 XVECTOR (menu_items)->contents[index + MENU_ITEMS_ITEM_NAME]
626 = concat2 (build_string (" "), tem);
627 index += MENU_ITEMS_ITEM_LENGTH;
630 skp->notbuttons = 0;
633 /* Calculate prefix, if any, for this item. */
634 if (EQ (type, QCtoggle))
635 prefix = build_string (NILP (selected) ? "[ ] " : "[X] ");
636 else if (EQ (type, QCradio))
637 prefix = build_string (NILP (selected) ? "( ) " : "(*) ");
639 /* Not a button. If we have earlier buttons, then we need a prefix. */
640 else if (!skp->notbuttons && SREF (item_string, 0) != '\0'
641 && SREF (item_string, 0) != '-')
642 prefix = build_string (" ");
644 if (!NILP (prefix))
645 item_string = concat2 (prefix, item_string);
647 #endif /* not HAVE_BOXES */
649 #if ! defined (USE_X_TOOLKIT) && ! defined (USE_GTK)
650 if (!NILP (map))
651 /* Indicate visually that this is a submenu. */
652 item_string = concat2 (item_string, build_string (" >"));
653 #endif
655 push_menu_item (item_string, enabled, key,
656 XVECTOR (item_properties)->contents[ITEM_PROPERTY_DEF],
657 XVECTOR (item_properties)->contents[ITEM_PROPERTY_KEYEQ],
658 XVECTOR (item_properties)->contents[ITEM_PROPERTY_TYPE],
659 XVECTOR (item_properties)->contents[ITEM_PROPERTY_SELECTED],
660 XVECTOR (item_properties)->contents[ITEM_PROPERTY_HELP]);
662 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
663 /* Display a submenu using the toolkit. */
664 if (! (NILP (map) || NILP (enabled)))
666 push_submenu_start ();
667 single_keymap_panes (map, Qnil, key, 0, skp->maxdepth - 1);
668 push_submenu_end ();
670 #endif
673 /* Push all the panes and items of a menu described by the
674 alist-of-alists MENU.
675 This handles old-fashioned calls to x-popup-menu. */
677 static void
678 list_of_panes (menu)
679 Lisp_Object menu;
681 Lisp_Object tail;
683 init_menu_items ();
685 for (tail = menu; CONSP (tail); tail = XCDR (tail))
687 Lisp_Object elt, pane_name, pane_data;
688 elt = XCAR (tail);
689 pane_name = Fcar (elt);
690 CHECK_STRING (pane_name);
691 push_menu_pane (ENCODE_MENU_STRING (pane_name), Qnil);
692 pane_data = Fcdr (elt);
693 CHECK_CONS (pane_data);
694 list_of_items (pane_data);
697 finish_menu_items ();
700 /* Push the items in a single pane defined by the alist PANE. */
702 static void
703 list_of_items (pane)
704 Lisp_Object pane;
706 Lisp_Object tail, item, item1;
708 for (tail = pane; CONSP (tail); tail = XCDR (tail))
710 item = XCAR (tail);
711 if (STRINGP (item))
712 push_menu_item (ENCODE_MENU_STRING (item), Qnil, Qnil, Qt,
713 Qnil, Qnil, Qnil, Qnil);
714 else if (CONSP (item))
716 item1 = XCAR (item);
717 CHECK_STRING (item1);
718 push_menu_item (ENCODE_MENU_STRING (item1), Qt, XCDR (item),
719 Qt, Qnil, Qnil, Qnil, Qnil);
721 else
722 push_left_right_boundary ();
727 #ifdef HAVE_X_WINDOWS
728 /* Return the mouse position in *X and *Y. The coordinates are window
729 relative for the edit window in frame F.
730 This is for Fx_popup_menu. The mouse_position_hook can not
731 be used for X, as it returns window relative coordinates
732 for the window where the mouse is in. This could be the menu bar,
733 the scroll bar or the edit window. Fx_popup_menu needs to be
734 sure it is the edit window. */
735 static void
736 mouse_position_for_popup (f, x, y)
737 FRAME_PTR f;
738 int *x;
739 int *y;
741 Window root, dummy_window;
742 int dummy;
744 if (! FRAME_X_P (f))
745 abort ();
747 BLOCK_INPUT;
749 XQueryPointer (FRAME_X_DISPLAY (f),
750 DefaultRootWindow (FRAME_X_DISPLAY (f)),
752 /* The root window which contains the pointer. */
753 &root,
755 /* Window pointer is on, not used */
756 &dummy_window,
758 /* The position on that root window. */
759 x, y,
761 /* x/y in dummy_window coordinates, not used. */
762 &dummy, &dummy,
764 /* Modifier keys and pointer buttons, about which
765 we don't care. */
766 (unsigned int *) &dummy);
768 UNBLOCK_INPUT;
770 /* xmenu_show expects window coordinates, not root window
771 coordinates. Translate. */
772 *x -= f->left_pos + FRAME_OUTER_TO_INNER_DIFF_X (f);
773 *y -= f->top_pos + FRAME_OUTER_TO_INNER_DIFF_Y (f);
776 #endif /* HAVE_X_WINDOWS */
778 DEFUN ("x-popup-menu", Fx_popup_menu, Sx_popup_menu, 2, 2, 0,
779 doc: /* Pop up a deck-of-cards menu and return user's selection.
780 POSITION is a position specification. This is either a mouse button event
781 or a list ((XOFFSET YOFFSET) WINDOW)
782 where XOFFSET and YOFFSET are positions in pixels from the top left
783 corner of WINDOW. (WINDOW may be a window or a frame object.)
784 This controls the position of the top left of the menu as a whole.
785 If POSITION is t, it means to use the current mouse position.
787 MENU is a specifier for a menu. For the simplest case, MENU is a keymap.
788 The menu items come from key bindings that have a menu string as well as
789 a definition; actually, the "definition" in such a key binding looks like
790 \(STRING . REAL-DEFINITION). To give the menu a title, put a string into
791 the keymap as a top-level element.
793 If REAL-DEFINITION is nil, that puts a nonselectable string in the menu.
794 Otherwise, REAL-DEFINITION should be a valid key binding definition.
796 You can also use a list of keymaps as MENU.
797 Then each keymap makes a separate pane.
799 When MENU is a keymap or a list of keymaps, the return value is the
800 list of events corresponding to the user's choice. Note that
801 `x-popup-menu' does not actually execute the command bound to that
802 sequence of events.
804 Alternatively, you can specify a menu of multiple panes
805 with a list of the form (TITLE PANE1 PANE2...),
806 where each pane is a list of form (TITLE ITEM1 ITEM2...).
807 Each ITEM is normally a cons cell (STRING . VALUE);
808 but a string can appear as an item--that makes a nonselectable line
809 in the menu.
810 With this form of menu, the return value is VALUE from the chosen item.
812 If POSITION is nil, don't display the menu at all, just precalculate the
813 cached information about equivalent key sequences.
815 If the user gets rid of the menu without making a valid choice, for
816 instance by clicking the mouse away from a valid choice or by typing
817 keyboard input, then this normally results in a quit and
818 `x-popup-menu' does not return. But if POSITION is a mouse button
819 event (indicating that the user invoked the menu with the mouse) then
820 no quit occurs and `x-popup-menu' returns nil. */)
821 (position, menu)
822 Lisp_Object position, menu;
824 Lisp_Object keymap, tem;
825 int xpos = 0, ypos = 0;
826 Lisp_Object title;
827 char *error_name = NULL;
828 Lisp_Object selection;
829 FRAME_PTR f = NULL;
830 Lisp_Object x, y, window;
831 int keymaps = 0;
832 int for_click = 0;
833 int specpdl_count = SPECPDL_INDEX ();
834 struct gcpro gcpro1;
836 #ifdef HAVE_MENUS
837 if (! NILP (position))
839 int get_current_pos_p = 0;
840 check_x ();
842 /* Decode the first argument: find the window and the coordinates. */
843 if (EQ (position, Qt)
844 || (CONSP (position) && (EQ (XCAR (position), Qmenu_bar)
845 || EQ (XCAR (position), Qtool_bar))))
847 get_current_pos_p = 1;
849 else
851 tem = Fcar (position);
852 if (CONSP (tem))
854 window = Fcar (Fcdr (position));
855 x = XCAR (tem);
856 y = Fcar (XCDR (tem));
858 else
860 for_click = 1;
861 tem = Fcar (Fcdr (position)); /* EVENT_START (position) */
862 window = Fcar (tem); /* POSN_WINDOW (tem) */
863 tem = Fcar (Fcdr (Fcdr (tem))); /* POSN_WINDOW_POSN (tem) */
864 x = Fcar (tem);
865 y = Fcdr (tem);
868 /* If a click happens in an external tool bar or a detached
869 tool bar, x and y is NIL. In that case, use the current
870 mouse position. This happens for the help button in the
871 tool bar. Ideally popup-menu should pass NIL to
872 this function, but it doesn't. */
873 if (NILP (x) && NILP (y))
874 get_current_pos_p = 1;
877 if (get_current_pos_p)
879 /* Use the mouse's current position. */
880 FRAME_PTR new_f = SELECTED_FRAME ();
881 #ifdef HAVE_X_WINDOWS
882 /* Can't use mouse_position_hook for X since it returns
883 coordinates relative to the window the mouse is in,
884 we need coordinates relative to the edit widget always. */
885 if (new_f != 0)
887 int cur_x, cur_y;
889 mouse_position_for_popup (new_f, &cur_x, &cur_y);
890 /* cur_x/y may be negative, so use make_number. */
891 x = make_number (cur_x);
892 y = make_number (cur_y);
895 #else /* not HAVE_X_WINDOWS */
896 Lisp_Object bar_window;
897 enum scroll_bar_part part;
898 unsigned long time;
900 if (mouse_position_hook)
901 (*mouse_position_hook) (&new_f, 1, &bar_window,
902 &part, &x, &y, &time);
903 #endif /* not HAVE_X_WINDOWS */
905 if (new_f != 0)
906 XSETFRAME (window, new_f);
907 else
909 window = selected_window;
910 XSETFASTINT (x, 0);
911 XSETFASTINT (y, 0);
915 CHECK_NUMBER (x);
916 CHECK_NUMBER (y);
918 /* Decode where to put the menu. */
920 if (FRAMEP (window))
922 f = XFRAME (window);
923 xpos = 0;
924 ypos = 0;
926 else if (WINDOWP (window))
928 CHECK_LIVE_WINDOW (window);
929 f = XFRAME (WINDOW_FRAME (XWINDOW (window)));
931 xpos = WINDOW_LEFT_EDGE_X (XWINDOW (window));
932 ypos = WINDOW_TOP_EDGE_Y (XWINDOW (window));
934 else
935 /* ??? Not really clean; should be CHECK_WINDOW_OR_FRAME,
936 but I don't want to make one now. */
937 CHECK_WINDOW (window);
939 xpos += XINT (x);
940 ypos += XINT (y);
942 if (! FRAME_X_P (f))
943 error ("Can not put X menu on non-X terminal");
945 XSETFRAME (Vmenu_updating_frame, f);
947 else
948 Vmenu_updating_frame = Qnil;
949 #endif /* HAVE_MENUS */
951 record_unwind_protect (unuse_menu_items, Qnil);
952 title = Qnil;
953 GCPRO1 (title);
955 /* Decode the menu items from what was specified. */
957 keymap = get_keymap (menu, 0, 0);
958 if (CONSP (keymap))
960 /* We were given a keymap. Extract menu info from the keymap. */
961 Lisp_Object prompt;
963 /* Extract the detailed info to make one pane. */
964 keymap_panes (&menu, 1, NILP (position));
966 /* Search for a string appearing directly as an element of the keymap.
967 That string is the title of the menu. */
968 prompt = Fkeymap_prompt (keymap);
969 if (NILP (title) && !NILP (prompt))
970 title = prompt;
972 /* Make that be the pane title of the first pane. */
973 if (!NILP (prompt) && menu_items_n_panes >= 0)
974 XVECTOR (menu_items)->contents[MENU_ITEMS_PANE_NAME] = prompt;
976 keymaps = 1;
978 else if (CONSP (menu) && KEYMAPP (XCAR (menu)))
980 /* We were given a list of keymaps. */
981 int nmaps = XFASTINT (Flength (menu));
982 Lisp_Object *maps
983 = (Lisp_Object *) alloca (nmaps * sizeof (Lisp_Object));
984 int i;
986 title = Qnil;
988 /* The first keymap that has a prompt string
989 supplies the menu title. */
990 for (tem = menu, i = 0; CONSP (tem); tem = XCDR (tem))
992 Lisp_Object prompt;
994 maps[i++] = keymap = get_keymap (XCAR (tem), 1, 0);
996 prompt = Fkeymap_prompt (keymap);
997 if (NILP (title) && !NILP (prompt))
998 title = prompt;
1001 /* Extract the detailed info to make one pane. */
1002 keymap_panes (maps, nmaps, NILP (position));
1004 /* Make the title be the pane title of the first pane. */
1005 if (!NILP (title) && menu_items_n_panes >= 0)
1006 XVECTOR (menu_items)->contents[MENU_ITEMS_PANE_NAME] = title;
1008 keymaps = 1;
1010 else
1012 /* We were given an old-fashioned menu. */
1013 title = Fcar (menu);
1014 CHECK_STRING (title);
1016 list_of_panes (Fcdr (menu));
1018 keymaps = 0;
1021 unbind_to (specpdl_count, Qnil);
1023 if (NILP (position))
1025 discard_menu_items ();
1026 UNGCPRO;
1027 return Qnil;
1030 #ifdef HAVE_MENUS
1031 /* Display them in a menu. */
1032 BLOCK_INPUT;
1034 selection = xmenu_show (f, xpos, ypos, for_click,
1035 keymaps, title, &error_name);
1036 UNBLOCK_INPUT;
1038 discard_menu_items ();
1040 UNGCPRO;
1041 #endif /* HAVE_MENUS */
1043 if (error_name) error (error_name);
1044 return selection;
1047 #ifdef HAVE_MENUS
1049 DEFUN ("x-popup-dialog", Fx_popup_dialog, Sx_popup_dialog, 2, 3, 0,
1050 doc: /* Pop up a dialog box and return user's selection.
1051 POSITION specifies which frame to use.
1052 This is normally a mouse button event or a window or frame.
1053 If POSITION is t, it means to use the frame the mouse is on.
1054 The dialog box appears in the middle of the specified frame.
1056 CONTENTS specifies the alternatives to display in the dialog box.
1057 It is a list of the form (DIALOG ITEM1 ITEM2...).
1058 Each ITEM is a cons cell (STRING . VALUE).
1059 The return value is VALUE from the chosen item.
1061 An ITEM may also be just a string--that makes a nonselectable item.
1062 An ITEM may also be nil--that means to put all preceding items
1063 on the left of the dialog box and all following items on the right.
1064 \(By default, approximately half appear on each side.)
1066 If HEADER is non-nil, the frame title for the box is "Information",
1067 otherwise it is "Question".
1069 If the user gets rid of the dialog box without making a valid choice,
1070 for instance using the window manager, then this produces a quit and
1071 `x-popup-dialog' does not return. */)
1072 (position, contents, header)
1073 Lisp_Object position, contents, header;
1075 FRAME_PTR f = NULL;
1076 Lisp_Object window;
1078 check_x ();
1080 /* Decode the first argument: find the window or frame to use. */
1081 if (EQ (position, Qt)
1082 || (CONSP (position) && (EQ (XCAR (position), Qmenu_bar)
1083 || EQ (XCAR (position), Qtool_bar))))
1085 #if 0 /* Using the frame the mouse is on may not be right. */
1086 /* Use the mouse's current position. */
1087 FRAME_PTR new_f = SELECTED_FRAME ();
1088 Lisp_Object bar_window;
1089 enum scroll_bar_part part;
1090 unsigned long time;
1091 Lisp_Object x, y;
1093 (*mouse_position_hook) (&new_f, 1, &bar_window, &part, &x, &y, &time);
1095 if (new_f != 0)
1096 XSETFRAME (window, new_f);
1097 else
1098 window = selected_window;
1099 #endif
1100 window = selected_window;
1102 else if (CONSP (position))
1104 Lisp_Object tem;
1105 tem = Fcar (position);
1106 if (CONSP (tem))
1107 window = Fcar (Fcdr (position));
1108 else
1110 tem = Fcar (Fcdr (position)); /* EVENT_START (position) */
1111 window = Fcar (tem); /* POSN_WINDOW (tem) */
1114 else if (WINDOWP (position) || FRAMEP (position))
1115 window = position;
1116 else
1117 window = Qnil;
1119 /* Decode where to put the menu. */
1121 if (FRAMEP (window))
1122 f = XFRAME (window);
1123 else if (WINDOWP (window))
1125 CHECK_LIVE_WINDOW (window);
1126 f = XFRAME (WINDOW_FRAME (XWINDOW (window)));
1128 else
1129 /* ??? Not really clean; should be CHECK_WINDOW_OR_FRAME,
1130 but I don't want to make one now. */
1131 CHECK_WINDOW (window);
1133 if (! FRAME_X_P (f))
1134 error ("Can not put X dialog on non-X terminal");
1136 #if ! defined (USE_X_TOOLKIT) && ! defined (USE_GTK)
1137 /* Display a menu with these alternatives
1138 in the middle of frame F. */
1140 Lisp_Object x, y, frame, newpos;
1141 XSETFRAME (frame, f);
1142 XSETINT (x, x_pixel_width (f) / 2);
1143 XSETINT (y, x_pixel_height (f) / 2);
1144 newpos = Fcons (Fcons (x, Fcons (y, Qnil)), Fcons (frame, Qnil));
1146 return Fx_popup_menu (newpos,
1147 Fcons (Fcar (contents), Fcons (contents, Qnil)));
1149 #else
1151 Lisp_Object title;
1152 char *error_name;
1153 Lisp_Object selection;
1154 int specpdl_count = SPECPDL_INDEX ();
1156 /* Decode the dialog items from what was specified. */
1157 title = Fcar (contents);
1158 CHECK_STRING (title);
1159 record_unwind_protect (unuse_menu_items, Qnil);
1161 if (NILP (Fcar (Fcdr (contents))))
1162 /* No buttons specified, add an "Ok" button so users can pop down
1163 the dialog. Also, the lesstif/motif version crashes if there are
1164 no buttons. */
1165 contents = Fcons (title, Fcons (Fcons (build_string ("Ok"), Qt), Qnil));
1167 list_of_panes (Fcons (contents, Qnil));
1169 /* Display them in a dialog box. */
1170 BLOCK_INPUT;
1171 selection = xdialog_show (f, 0, title, header, &error_name);
1172 UNBLOCK_INPUT;
1174 unbind_to (specpdl_count, Qnil);
1175 discard_menu_items ();
1177 if (error_name) error (error_name);
1178 return selection;
1180 #endif
1184 #ifndef MSDOS
1186 /* Set menu_items_inuse so no other popup menu or dialog is created. */
1188 void
1189 x_menu_set_in_use (in_use)
1190 int in_use;
1192 menu_items_inuse = in_use ? Qt : Qnil;
1193 popup_activated_flag = in_use;
1196 /* Wait for an X event to arrive or for a timer to expire. */
1198 void
1199 x_menu_wait_for_event (void *data)
1201 extern EMACS_TIME timer_check P_ ((int));
1203 /* Another way to do this is to register a timer callback, that can be
1204 done in GTK and Xt. But we have to do it like this when using only X
1205 anyway, and with callbacks we would have three variants for timer handling
1206 instead of the small ifdefs below. */
1208 while (
1209 #ifdef USE_X_TOOLKIT
1210 ! XtAppPending (Xt_app_con)
1211 #elif defined USE_GTK
1212 ! gtk_events_pending ()
1213 #else
1214 ! XPending ((Display*) data)
1215 #endif
1218 EMACS_TIME next_time = timer_check (1);
1219 long secs = EMACS_SECS (next_time);
1220 long usecs = EMACS_USECS (next_time);
1221 SELECT_TYPE read_fds;
1222 struct x_display_info *dpyinfo;
1223 int n = 0;
1225 FD_ZERO (&read_fds);
1226 for (dpyinfo = x_display_list; dpyinfo; dpyinfo = dpyinfo->next)
1228 int fd = ConnectionNumber (dpyinfo->display);
1229 FD_SET (fd, &read_fds);
1230 if (fd > n) n = fd;
1233 if (secs < 0 || (secs == 0 && usecs == 0))
1235 /* Sometimes timer_check returns -1 (no timers) even if there are
1236 timers. So do a timeout anyway. */
1237 EMACS_SET_SECS (next_time, 1);
1238 EMACS_SET_USECS (next_time, 0);
1241 select (n + 1, &read_fds, (SELECT_TYPE *)0, (SELECT_TYPE *)0, &next_time);
1244 #endif /* ! MSDOS */
1247 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
1249 #ifdef USE_X_TOOLKIT
1251 /* Loop in Xt until the menu pulldown or dialog popup has been
1252 popped down (deactivated). This is used for x-popup-menu
1253 and x-popup-dialog; it is not used for the menu bar.
1255 NOTE: All calls to popup_get_selection should be protected
1256 with BLOCK_INPUT, UNBLOCK_INPUT wrappers. */
1258 static void
1259 popup_get_selection (initial_event, dpyinfo, id, do_timers)
1260 XEvent *initial_event;
1261 struct x_display_info *dpyinfo;
1262 LWLIB_ID id;
1263 int do_timers;
1265 XEvent event;
1267 while (popup_activated_flag)
1269 if (initial_event)
1271 event = *initial_event;
1272 initial_event = 0;
1274 else
1276 if (do_timers) x_menu_wait_for_event (0);
1277 XtAppNextEvent (Xt_app_con, &event);
1280 /* Make sure we don't consider buttons grabbed after menu goes.
1281 And make sure to deactivate for any ButtonRelease,
1282 even if XtDispatchEvent doesn't do that. */
1283 if (event.type == ButtonRelease
1284 && dpyinfo->display == event.xbutton.display)
1286 dpyinfo->grabbed &= ~(1 << event.xbutton.button);
1287 #ifdef USE_MOTIF /* Pretending that the event came from a
1288 Btn1Down seems the only way to convince Motif to
1289 activate its callbacks; setting the XmNmenuPost
1290 isn't working. --marcus@sysc.pdx.edu. */
1291 event.xbutton.button = 1;
1292 /* Motif only pops down menus when no Ctrl, Alt or Mod
1293 key is pressed and the button is released. So reset key state
1294 so Motif thinks this is the case. */
1295 event.xbutton.state = 0;
1296 #endif
1298 /* Pop down on C-g and Escape. */
1299 else if (event.type == KeyPress
1300 && dpyinfo->display == event.xbutton.display)
1302 KeySym keysym = XLookupKeysym (&event.xkey, 0);
1304 if ((keysym == XK_g && (event.xkey.state & ControlMask) != 0)
1305 || keysym == XK_Escape) /* Any escape, ignore modifiers. */
1306 popup_activated_flag = 0;
1309 x_dispatch_event (&event, event.xany.display);
1313 DEFUN ("x-menu-bar-open", Fx_menu_bar_open, Sx_menu_bar_open, 0, 1, "i",
1314 doc: /* Start key navigation of the menu bar in FRAME.
1315 This initially opens the first menu bar item and you can then navigate with the
1316 arrow keys, select a menu entry with the return key or cancel with the
1317 escape key. If FRAME has no menu bar this function does nothing.
1319 If FRAME is nil or not given, use the selected frame. */)
1320 (frame)
1321 Lisp_Object frame;
1323 XEvent ev;
1324 FRAME_PTR f = check_x_frame (frame);
1325 Widget menubar;
1326 BLOCK_INPUT;
1328 if (FRAME_EXTERNAL_MENU_BAR (f))
1329 set_frame_menubar (f, 0, 1);
1331 menubar = FRAME_X_OUTPUT (f)->menubar_widget;
1332 if (menubar)
1334 Window child;
1335 int error_p = 0;
1337 x_catch_errors (FRAME_X_DISPLAY (f));
1338 memset (&ev, 0, sizeof ev);
1339 ev.xbutton.display = FRAME_X_DISPLAY (f);
1340 ev.xbutton.window = XtWindow (menubar);
1341 ev.xbutton.root = FRAME_X_DISPLAY_INFO (f)->root_window;
1342 ev.xbutton.time = XtLastTimestampProcessed (FRAME_X_DISPLAY (f));
1343 ev.xbutton.button = Button1;
1344 ev.xbutton.x = ev.xbutton.y = FRAME_MENUBAR_HEIGHT (f) / 2;
1345 ev.xbutton.same_screen = True;
1347 #ifdef USE_MOTIF
1349 Arg al[2];
1350 WidgetList list;
1351 Cardinal nr;
1352 XtSetArg (al[0], XtNchildren, &list);
1353 XtSetArg (al[1], XtNnumChildren, &nr);
1354 XtGetValues (menubar, al, 2);
1355 ev.xbutton.window = XtWindow (list[0]);
1357 #endif
1359 XTranslateCoordinates (FRAME_X_DISPLAY (f),
1360 /* From-window, to-window. */
1361 ev.xbutton.window, ev.xbutton.root,
1363 /* From-position, to-position. */
1364 ev.xbutton.x, ev.xbutton.y,
1365 &ev.xbutton.x_root, &ev.xbutton.y_root,
1367 /* Child of win. */
1368 &child);
1369 error_p = x_had_errors_p (FRAME_X_DISPLAY (f));
1370 x_uncatch_errors ();
1372 if (! error_p)
1374 ev.type = ButtonPress;
1375 ev.xbutton.state = 0;
1377 XtDispatchEvent (&ev);
1378 ev.xbutton.type = ButtonRelease;
1379 ev.xbutton.state = Button1Mask;
1380 XtDispatchEvent (&ev);
1384 UNBLOCK_INPUT;
1386 return Qnil;
1388 #endif /* USE_X_TOOLKIT */
1391 #ifdef USE_GTK
1392 DEFUN ("x-menu-bar-open", Fx_menu_bar_open, Sx_menu_bar_open, 0, 1, "i",
1393 doc: /* Start key navigation of the menu bar in FRAME.
1394 This initially opens the first menu bar item and you can then navigate with the
1395 arrow keys, select a menu entry with the return key or cancel with the
1396 escape key. If FRAME has no menu bar this function does nothing.
1398 If FRAME is nil or not given, use the selected frame. */)
1399 (frame)
1400 Lisp_Object frame;
1402 GtkWidget *menubar;
1403 BLOCK_INPUT;
1404 FRAME_PTR f = check_x_frame (frame);
1406 if (FRAME_EXTERNAL_MENU_BAR (f))
1407 set_frame_menubar (f, 0, 1);
1409 menubar = FRAME_X_OUTPUT (f)->menubar_widget;
1410 if (menubar)
1412 /* Activate the first menu. */
1413 GList *children = gtk_container_get_children (GTK_CONTAINER (menubar));
1415 gtk_menu_shell_select_item (GTK_MENU_SHELL (menubar),
1416 GTK_WIDGET (children->data));
1418 popup_activated_flag = 1;
1419 g_list_free (children);
1421 UNBLOCK_INPUT;
1423 return Qnil;
1426 /* Loop util popup_activated_flag is set to zero in a callback.
1427 Used for popup menus and dialogs. */
1429 static void
1430 popup_widget_loop (do_timers, widget)
1431 int do_timers;
1432 GtkWidget *widget;
1434 ++popup_activated_flag;
1436 /* Process events in the Gtk event loop until done. */
1437 while (popup_activated_flag)
1439 if (do_timers) x_menu_wait_for_event (0);
1440 gtk_main_iteration ();
1443 #endif
1445 /* Activate the menu bar of frame F.
1446 This is called from keyboard.c when it gets the
1447 MENU_BAR_ACTIVATE_EVENT out of the Emacs event queue.
1449 To activate the menu bar, we use the X button-press event
1450 that was saved in saved_menu_event.
1451 That makes the toolkit do its thing.
1453 But first we recompute the menu bar contents (the whole tree).
1455 The reason for saving the button event until here, instead of
1456 passing it to the toolkit right away, is that we can safely
1457 execute Lisp code. */
1459 void
1460 x_activate_menubar (f)
1461 FRAME_PTR f;
1463 if (! FRAME_X_P (f))
1464 abort ();
1466 if (!f->output_data.x->saved_menu_event->type)
1467 return;
1469 #ifdef USE_GTK
1470 if (! xg_win_to_widget (FRAME_X_DISPLAY (f),
1471 f->output_data.x->saved_menu_event->xany.window))
1472 return;
1473 #endif
1475 set_frame_menubar (f, 0, 1);
1476 BLOCK_INPUT;
1477 #ifdef USE_GTK
1478 XPutBackEvent (f->output_data.x->display_info->display,
1479 f->output_data.x->saved_menu_event);
1480 popup_activated_flag = 1;
1481 #else
1482 XtDispatchEvent (f->output_data.x->saved_menu_event);
1483 #endif
1484 UNBLOCK_INPUT;
1485 #ifdef USE_MOTIF
1486 if (f->output_data.x->saved_menu_event->type == ButtonRelease)
1487 pending_menu_activation = 1;
1488 #endif
1490 /* Ignore this if we get it a second time. */
1491 f->output_data.x->saved_menu_event->type = 0;
1494 /* Detect if a dialog or menu has been posted. */
1497 popup_activated ()
1499 return popup_activated_flag;
1502 /* This callback is invoked when the user selects a menubar cascade
1503 pushbutton, but before the pulldown menu is posted. */
1505 #ifndef USE_GTK
1506 static void
1507 popup_activate_callback (widget, id, client_data)
1508 Widget widget;
1509 LWLIB_ID id;
1510 XtPointer client_data;
1512 popup_activated_flag = 1;
1514 #endif
1516 /* This callback is invoked when a dialog or menu is finished being
1517 used and has been unposted. */
1519 #ifdef USE_GTK
1520 static void
1521 popup_deactivate_callback (widget, client_data)
1522 GtkWidget *widget;
1523 gpointer client_data;
1525 popup_activated_flag = 0;
1527 #else
1528 static void
1529 popup_deactivate_callback (widget, id, client_data)
1530 Widget widget;
1531 LWLIB_ID id;
1532 XtPointer client_data;
1534 popup_activated_flag = 0;
1536 #endif
1539 /* Function that finds the frame for WIDGET and shows the HELP text
1540 for that widget.
1541 F is the frame if known, or NULL if not known. */
1542 static void
1543 show_help_event (f, widget, help)
1544 FRAME_PTR f;
1545 xt_or_gtk_widget widget;
1546 Lisp_Object help;
1548 Lisp_Object frame;
1550 if (f)
1552 XSETFRAME (frame, f);
1553 kbd_buffer_store_help_event (frame, help);
1555 else
1557 #if 0 /* This code doesn't do anything useful. ++kfs */
1558 /* WIDGET is the popup menu. It's parent is the frame's
1559 widget. See which frame that is. */
1560 xt_or_gtk_widget frame_widget = XtParent (widget);
1561 Lisp_Object tail;
1563 for (tail = Vframe_list; GC_CONSP (tail); tail = XCDR (tail))
1565 frame = XCAR (tail);
1566 if (GC_FRAMEP (frame)
1567 && (f = XFRAME (frame),
1568 FRAME_X_P (f) && f->output_data.x->widget == frame_widget))
1569 break;
1571 #endif
1572 show_help_echo (help, Qnil, Qnil, Qnil, 1);
1576 /* Callback called when menu items are highlighted/unhighlighted
1577 while moving the mouse over them. WIDGET is the menu bar or menu
1578 popup widget. ID is its LWLIB_ID. CALL_DATA contains a pointer to
1579 the data structure for the menu item, or null in case of
1580 unhighlighting. */
1582 #ifdef USE_GTK
1583 void
1584 menu_highlight_callback (widget, call_data)
1585 GtkWidget *widget;
1586 gpointer call_data;
1588 xg_menu_item_cb_data *cb_data;
1589 Lisp_Object help;
1591 cb_data = (xg_menu_item_cb_data*) g_object_get_data (G_OBJECT (widget),
1592 XG_ITEM_DATA);
1593 if (! cb_data) return;
1595 help = call_data ? cb_data->help : Qnil;
1597 /* If popup_activated_flag is greater than 1 we are in a popup menu.
1598 Don't show help for them, they won't appear before the
1599 popup is popped down. */
1600 if (popup_activated_flag <= 1)
1601 show_help_event (cb_data->cl_data->f, widget, help);
1603 #else
1604 void
1605 menu_highlight_callback (widget, id, call_data)
1606 Widget widget;
1607 LWLIB_ID id;
1608 void *call_data;
1610 struct frame *f;
1611 Lisp_Object help;
1613 widget_value *wv = (widget_value *) call_data;
1615 help = wv ? wv->help : Qnil;
1617 /* Determine the frame for the help event. */
1618 f = menubar_id_to_frame (id);
1620 show_help_event (f, widget, help);
1622 #endif
1624 /* Find the menu selection and store it in the keyboard buffer.
1625 F is the frame the menu is on.
1626 MENU_BAR_ITEMS_USED is the length of VECTOR.
1627 VECTOR is an array of menu events for the whole menu. */
1629 static void
1630 find_and_call_menu_selection (f, menu_bar_items_used, vector, client_data)
1631 FRAME_PTR f;
1632 int menu_bar_items_used;
1633 Lisp_Object vector;
1634 void *client_data;
1636 Lisp_Object prefix, entry;
1637 Lisp_Object *subprefix_stack;
1638 int submenu_depth = 0;
1639 int i;
1641 entry = Qnil;
1642 subprefix_stack = (Lisp_Object *) alloca (menu_bar_items_used * sizeof (Lisp_Object));
1643 prefix = Qnil;
1644 i = 0;
1646 while (i < menu_bar_items_used)
1648 if (EQ (XVECTOR (vector)->contents[i], Qnil))
1650 subprefix_stack[submenu_depth++] = prefix;
1651 prefix = entry;
1652 i++;
1654 else if (EQ (XVECTOR (vector)->contents[i], Qlambda))
1656 prefix = subprefix_stack[--submenu_depth];
1657 i++;
1659 else if (EQ (XVECTOR (vector)->contents[i], Qt))
1661 prefix = XVECTOR (vector)->contents[i + MENU_ITEMS_PANE_PREFIX];
1662 i += MENU_ITEMS_PANE_LENGTH;
1664 else
1666 entry = XVECTOR (vector)->contents[i + MENU_ITEMS_ITEM_VALUE];
1667 /* The EMACS_INT cast avoids a warning. There's no problem
1668 as long as pointers have enough bits to hold small integers. */
1669 if ((int) (EMACS_INT) client_data == i)
1671 int j;
1672 struct input_event buf;
1673 Lisp_Object frame;
1674 EVENT_INIT (buf);
1676 XSETFRAME (frame, f);
1677 buf.kind = MENU_BAR_EVENT;
1678 buf.frame_or_window = frame;
1679 buf.arg = frame;
1680 kbd_buffer_store_event (&buf);
1682 for (j = 0; j < submenu_depth; j++)
1683 if (!NILP (subprefix_stack[j]))
1685 buf.kind = MENU_BAR_EVENT;
1686 buf.frame_or_window = frame;
1687 buf.arg = subprefix_stack[j];
1688 kbd_buffer_store_event (&buf);
1691 if (!NILP (prefix))
1693 buf.kind = MENU_BAR_EVENT;
1694 buf.frame_or_window = frame;
1695 buf.arg = prefix;
1696 kbd_buffer_store_event (&buf);
1699 buf.kind = MENU_BAR_EVENT;
1700 buf.frame_or_window = frame;
1701 buf.arg = entry;
1702 kbd_buffer_store_event (&buf);
1704 return;
1706 i += MENU_ITEMS_ITEM_LENGTH;
1712 #ifdef USE_GTK
1713 /* Gtk calls callbacks just because we tell it what item should be
1714 selected in a radio group. If this variable is set to a non-zero
1715 value, we are creating menus and don't want callbacks right now.
1717 static int xg_crazy_callback_abort;
1719 /* This callback is called from the menu bar pulldown menu
1720 when the user makes a selection.
1721 Figure out what the user chose
1722 and put the appropriate events into the keyboard buffer. */
1723 static void
1724 menubar_selection_callback (widget, client_data)
1725 GtkWidget *widget;
1726 gpointer client_data;
1728 xg_menu_item_cb_data *cb_data = (xg_menu_item_cb_data*) client_data;
1730 if (xg_crazy_callback_abort)
1731 return;
1733 if (! cb_data || ! cb_data->cl_data || ! cb_data->cl_data->f)
1734 return;
1736 /* For a group of radio buttons, GTK calls the selection callback first
1737 for the item that was active before the selection and then for the one that
1738 is active after the selection. For C-h k this means we get the help on
1739 the deselected item and then the selected item is executed. Prevent that
1740 by ignoring the non-active item. */
1741 if (GTK_IS_RADIO_MENU_ITEM (widget)
1742 && ! gtk_check_menu_item_get_active (GTK_CHECK_MENU_ITEM (widget)))
1743 return;
1745 /* When a menu is popped down, X generates a focus event (i.e. focus
1746 goes back to the frame below the menu). Since GTK buffers events,
1747 we force it out here before the menu selection event. Otherwise
1748 sit-for will exit at once if the focus event follows the menu selection
1749 event. */
1751 BLOCK_INPUT;
1752 while (gtk_events_pending ())
1753 gtk_main_iteration ();
1754 UNBLOCK_INPUT;
1756 find_and_call_menu_selection (cb_data->cl_data->f,
1757 cb_data->cl_data->menu_bar_items_used,
1758 cb_data->cl_data->menu_bar_vector,
1759 cb_data->call_data);
1762 #else /* not USE_GTK */
1764 /* This callback is called from the menu bar pulldown menu
1765 when the user makes a selection.
1766 Figure out what the user chose
1767 and put the appropriate events into the keyboard buffer. */
1768 static void
1769 menubar_selection_callback (widget, id, client_data)
1770 Widget widget;
1771 LWLIB_ID id;
1772 XtPointer client_data;
1774 FRAME_PTR f;
1776 f = menubar_id_to_frame (id);
1777 if (!f)
1778 return;
1779 find_and_call_menu_selection (f, f->menu_bar_items_used,
1780 f->menu_bar_vector, client_data);
1782 #endif /* not USE_GTK */
1784 /* Allocate a widget_value, blocking input. */
1786 widget_value *
1787 xmalloc_widget_value ()
1789 widget_value *value;
1791 BLOCK_INPUT;
1792 value = malloc_widget_value ();
1793 UNBLOCK_INPUT;
1795 return value;
1798 /* This recursively calls free_widget_value on the tree of widgets.
1799 It must free all data that was malloc'ed for these widget_values.
1800 In Emacs, many slots are pointers into the data of Lisp_Strings, and
1801 must be left alone. */
1803 void
1804 free_menubar_widget_value_tree (wv)
1805 widget_value *wv;
1807 if (! wv) return;
1809 wv->name = wv->value = wv->key = (char *) 0xDEADBEEF;
1811 if (wv->contents && (wv->contents != (widget_value*)1))
1813 free_menubar_widget_value_tree (wv->contents);
1814 wv->contents = (widget_value *) 0xDEADBEEF;
1816 if (wv->next)
1818 free_menubar_widget_value_tree (wv->next);
1819 wv->next = (widget_value *) 0xDEADBEEF;
1821 BLOCK_INPUT;
1822 free_widget_value (wv);
1823 UNBLOCK_INPUT;
1826 /* Set up data in menu_items for a menu bar item
1827 whose event type is ITEM_KEY (with string ITEM_NAME)
1828 and whose contents come from the list of keymaps MAPS. */
1830 static int
1831 parse_single_submenu (item_key, item_name, maps)
1832 Lisp_Object item_key, item_name, maps;
1834 Lisp_Object length;
1835 int len;
1836 Lisp_Object *mapvec;
1837 int i;
1838 int top_level_items = 0;
1840 length = Flength (maps);
1841 len = XINT (length);
1843 /* Convert the list MAPS into a vector MAPVEC. */
1844 mapvec = (Lisp_Object *) alloca (len * sizeof (Lisp_Object));
1845 for (i = 0; i < len; i++)
1847 mapvec[i] = Fcar (maps);
1848 maps = Fcdr (maps);
1851 /* Loop over the given keymaps, making a pane for each map.
1852 But don't make a pane that is empty--ignore that map instead. */
1853 for (i = 0; i < len; i++)
1855 if (!KEYMAPP (mapvec[i]))
1857 /* Here we have a command at top level in the menu bar
1858 as opposed to a submenu. */
1859 top_level_items = 1;
1860 push_menu_pane (Qnil, Qnil);
1861 push_menu_item (item_name, Qt, item_key, mapvec[i],
1862 Qnil, Qnil, Qnil, Qnil);
1864 else
1866 Lisp_Object prompt;
1867 prompt = Fkeymap_prompt (mapvec[i]);
1868 single_keymap_panes (mapvec[i],
1869 !NILP (prompt) ? prompt : item_name,
1870 item_key, 0, 10);
1874 return top_level_items;
1877 /* Create a tree of widget_value objects
1878 representing the panes and items
1879 in menu_items starting at index START, up to index END. */
1881 static widget_value *
1882 digest_single_submenu (start, end, top_level_items)
1883 int start, end, top_level_items;
1885 widget_value *wv, *prev_wv, *save_wv, *first_wv;
1886 int i;
1887 int submenu_depth = 0;
1888 widget_value **submenu_stack;
1889 int panes_seen = 0;
1891 submenu_stack
1892 = (widget_value **) alloca (menu_items_used * sizeof (widget_value *));
1893 wv = xmalloc_widget_value ();
1894 wv->name = "menu";
1895 wv->value = 0;
1896 wv->enabled = 1;
1897 wv->button_type = BUTTON_TYPE_NONE;
1898 wv->help = Qnil;
1899 first_wv = wv;
1900 save_wv = 0;
1901 prev_wv = 0;
1903 /* Loop over all panes and items made by the preceding call
1904 to parse_single_submenu and construct a tree of widget_value objects.
1905 Ignore the panes and items used by previous calls to
1906 digest_single_submenu, even though those are also in menu_items. */
1907 i = start;
1908 while (i < end)
1910 if (EQ (XVECTOR (menu_items)->contents[i], Qnil))
1912 submenu_stack[submenu_depth++] = save_wv;
1913 save_wv = prev_wv;
1914 prev_wv = 0;
1915 i++;
1917 else if (EQ (XVECTOR (menu_items)->contents[i], Qlambda))
1919 prev_wv = save_wv;
1920 save_wv = submenu_stack[--submenu_depth];
1921 i++;
1923 else if (EQ (XVECTOR (menu_items)->contents[i], Qt)
1924 && submenu_depth != 0)
1925 i += MENU_ITEMS_PANE_LENGTH;
1926 /* Ignore a nil in the item list.
1927 It's meaningful only for dialog boxes. */
1928 else if (EQ (XVECTOR (menu_items)->contents[i], Qquote))
1929 i += 1;
1930 else if (EQ (XVECTOR (menu_items)->contents[i], Qt))
1932 /* Create a new pane. */
1933 Lisp_Object pane_name, prefix;
1934 char *pane_string;
1936 panes_seen++;
1938 pane_name = XVECTOR (menu_items)->contents[i + MENU_ITEMS_PANE_NAME];
1939 prefix = XVECTOR (menu_items)->contents[i + MENU_ITEMS_PANE_PREFIX];
1941 #ifndef HAVE_MULTILINGUAL_MENU
1942 if (STRINGP (pane_name) && STRING_MULTIBYTE (pane_name))
1944 pane_name = ENCODE_MENU_STRING (pane_name);
1945 AREF (menu_items, i + MENU_ITEMS_PANE_NAME) = pane_name;
1947 #endif
1948 pane_string = (NILP (pane_name)
1949 ? "" : (char *) SDATA (pane_name));
1950 /* If there is just one top-level pane, put all its items directly
1951 under the top-level menu. */
1952 if (menu_items_n_panes == 1)
1953 pane_string = "";
1955 /* If the pane has a meaningful name,
1956 make the pane a top-level menu item
1957 with its items as a submenu beneath it. */
1958 if (strcmp (pane_string, ""))
1960 wv = xmalloc_widget_value ();
1961 if (save_wv)
1962 save_wv->next = wv;
1963 else
1964 first_wv->contents = wv;
1965 wv->lname = pane_name;
1966 /* Set value to 1 so update_submenu_strings can handle '@' */
1967 wv->value = (char *)1;
1968 wv->enabled = 1;
1969 wv->button_type = BUTTON_TYPE_NONE;
1970 wv->help = Qnil;
1971 save_wv = wv;
1973 else
1974 save_wv = first_wv;
1976 prev_wv = 0;
1977 i += MENU_ITEMS_PANE_LENGTH;
1979 else
1981 /* Create a new item within current pane. */
1982 Lisp_Object item_name, enable, descrip, def, type, selected;
1983 Lisp_Object help;
1985 /* All items should be contained in panes. */
1986 if (panes_seen == 0)
1987 abort ();
1989 item_name = AREF (menu_items, i + MENU_ITEMS_ITEM_NAME);
1990 enable = AREF (menu_items, i + MENU_ITEMS_ITEM_ENABLE);
1991 descrip = AREF (menu_items, i + MENU_ITEMS_ITEM_EQUIV_KEY);
1992 def = AREF (menu_items, i + MENU_ITEMS_ITEM_DEFINITION);
1993 type = AREF (menu_items, i + MENU_ITEMS_ITEM_TYPE);
1994 selected = AREF (menu_items, i + MENU_ITEMS_ITEM_SELECTED);
1995 help = AREF (menu_items, i + MENU_ITEMS_ITEM_HELP);
1997 #ifndef HAVE_MULTILINGUAL_MENU
1998 if (STRING_MULTIBYTE (item_name))
2000 item_name = ENCODE_MENU_STRING (item_name);
2001 AREF (menu_items, i + MENU_ITEMS_ITEM_NAME) = item_name;
2004 if (STRINGP (descrip) && STRING_MULTIBYTE (descrip))
2006 descrip = ENCODE_MENU_STRING (descrip);
2007 AREF (menu_items, i + MENU_ITEMS_ITEM_EQUIV_KEY) = descrip;
2009 #endif /* not HAVE_MULTILINGUAL_MENU */
2011 wv = xmalloc_widget_value ();
2012 if (prev_wv)
2013 prev_wv->next = wv;
2014 else
2015 save_wv->contents = wv;
2017 wv->lname = item_name;
2018 if (!NILP (descrip))
2019 wv->lkey = descrip;
2020 wv->value = 0;
2021 /* The EMACS_INT cast avoids a warning. There's no problem
2022 as long as pointers have enough bits to hold small integers. */
2023 wv->call_data = (!NILP (def) ? (void *) (EMACS_INT) i : 0);
2024 wv->enabled = !NILP (enable);
2026 if (NILP (type))
2027 wv->button_type = BUTTON_TYPE_NONE;
2028 else if (EQ (type, QCradio))
2029 wv->button_type = BUTTON_TYPE_RADIO;
2030 else if (EQ (type, QCtoggle))
2031 wv->button_type = BUTTON_TYPE_TOGGLE;
2032 else
2033 abort ();
2035 wv->selected = !NILP (selected);
2036 if (! STRINGP (help))
2037 help = Qnil;
2039 wv->help = help;
2041 prev_wv = wv;
2043 i += MENU_ITEMS_ITEM_LENGTH;
2047 /* If we have just one "menu item"
2048 that was originally a button, return it by itself. */
2049 if (top_level_items && first_wv->contents && first_wv->contents->next == 0)
2051 wv = first_wv->contents;
2052 free_widget_value (first_wv);
2053 return wv;
2056 return first_wv;
2059 /* Walk through the widget_value tree starting at FIRST_WV and update
2060 the char * pointers from the corresponding lisp values.
2061 We do this after building the whole tree, since GC may happen while the
2062 tree is constructed, and small strings are relocated. So we must wait
2063 until no GC can happen before storing pointers into lisp values. */
2064 static void
2065 update_submenu_strings (first_wv)
2066 widget_value *first_wv;
2068 widget_value *wv;
2070 for (wv = first_wv; wv; wv = wv->next)
2072 if (STRINGP (wv->lname))
2074 wv->name = (char *) SDATA (wv->lname);
2076 /* Ignore the @ that means "separate pane".
2077 This is a kludge, but this isn't worth more time. */
2078 if (wv->value == (char *)1)
2080 if (wv->name[0] == '@')
2081 wv->name++;
2082 wv->value = 0;
2086 if (STRINGP (wv->lkey))
2087 wv->key = (char *) SDATA (wv->lkey);
2089 if (wv->contents)
2090 update_submenu_strings (wv->contents);
2095 /* Recompute all the widgets of frame F, when the menu bar has been
2096 changed. Value is non-zero if widgets were updated. */
2098 static int
2099 update_frame_menubar (f)
2100 FRAME_PTR f;
2102 #ifdef USE_GTK
2103 return xg_update_frame_menubar (f);
2104 #else
2105 struct x_output *x;
2106 int columns, rows;
2108 if (! FRAME_X_P (f))
2109 abort ();
2111 x = f->output_data.x;
2113 if (!x->menubar_widget || XtIsManaged (x->menubar_widget))
2114 return 0;
2116 BLOCK_INPUT;
2117 /* Save the size of the frame because the pane widget doesn't accept
2118 to resize itself. So force it. */
2119 columns = FRAME_COLS (f);
2120 rows = FRAME_LINES (f);
2122 /* Do the voodoo which means "I'm changing lots of things, don't try
2123 to refigure sizes until I'm done." */
2124 lw_refigure_widget (x->column_widget, False);
2126 /* The order in which children are managed is the top to bottom
2127 order in which they are displayed in the paned window. First,
2128 remove the text-area widget. */
2129 XtUnmanageChild (x->edit_widget);
2131 /* Remove the menubar that is there now, and put up the menubar that
2132 should be there. */
2133 XtManageChild (x->menubar_widget);
2134 XtMapWidget (x->menubar_widget);
2135 XtVaSetValues (x->menubar_widget, XtNmappedWhenManaged, 1, NULL);
2137 /* Re-manage the text-area widget, and then thrash the sizes. */
2138 XtManageChild (x->edit_widget);
2139 lw_refigure_widget (x->column_widget, True);
2141 /* Force the pane widget to resize itself with the right values. */
2142 EmacsFrameSetCharSize (x->edit_widget, columns, rows);
2143 UNBLOCK_INPUT;
2144 #endif
2145 return 1;
2148 /* Set the contents of the menubar widgets of frame F.
2149 The argument FIRST_TIME is currently ignored;
2150 it is set the first time this is called, from initialize_frame_menubar. */
2152 void
2153 set_frame_menubar (f, first_time, deep_p)
2154 FRAME_PTR f;
2155 int first_time;
2156 int deep_p;
2158 xt_or_gtk_widget menubar_widget;
2159 #ifdef USE_X_TOOLKIT
2160 LWLIB_ID id;
2161 #endif
2162 Lisp_Object items;
2163 widget_value *wv, *first_wv, *prev_wv = 0;
2164 int i, last_i = 0;
2165 int *submenu_start, *submenu_end;
2166 int *submenu_top_level_items, *submenu_n_panes;
2168 if (! FRAME_X_P (f))
2169 abort ();
2171 menubar_widget = f->output_data.x->menubar_widget;
2173 XSETFRAME (Vmenu_updating_frame, f);
2175 #ifdef USE_X_TOOLKIT
2176 if (f->output_data.x->id == 0)
2177 f->output_data.x->id = next_menubar_widget_id++;
2178 id = f->output_data.x->id;
2179 #endif
2181 if (! menubar_widget)
2182 deep_p = 1;
2183 else if (pending_menu_activation && !deep_p)
2184 deep_p = 1;
2185 /* Make the first call for any given frame always go deep. */
2186 else if (!f->output_data.x->saved_menu_event && !deep_p)
2188 deep_p = 1;
2189 f->output_data.x->saved_menu_event = (XEvent*)xmalloc (sizeof (XEvent));
2190 f->output_data.x->saved_menu_event->type = 0;
2193 #ifdef USE_GTK
2194 /* If we have detached menus, we must update deep so detached menus
2195 also gets updated. */
2196 deep_p = deep_p || xg_have_tear_offs ();
2197 #endif
2199 if (deep_p)
2201 /* Make a widget-value tree representing the entire menu trees. */
2203 struct buffer *prev = current_buffer;
2204 Lisp_Object buffer;
2205 int specpdl_count = SPECPDL_INDEX ();
2206 int previous_menu_items_used = f->menu_bar_items_used;
2207 Lisp_Object *previous_items
2208 = (Lisp_Object *) alloca (previous_menu_items_used
2209 * sizeof (Lisp_Object));
2211 /* If we are making a new widget, its contents are empty,
2212 do always reinitialize them. */
2213 if (! menubar_widget)
2214 previous_menu_items_used = 0;
2216 buffer = XWINDOW (FRAME_SELECTED_WINDOW (f))->buffer;
2217 specbind (Qinhibit_quit, Qt);
2218 /* Don't let the debugger step into this code
2219 because it is not reentrant. */
2220 specbind (Qdebug_on_next_call, Qnil);
2222 record_unwind_save_match_data ();
2223 if (NILP (Voverriding_local_map_menu_flag))
2225 specbind (Qoverriding_terminal_local_map, Qnil);
2226 specbind (Qoverriding_local_map, Qnil);
2229 set_buffer_internal_1 (XBUFFER (buffer));
2231 /* Run the Lucid hook. */
2232 safe_run_hooks (Qactivate_menubar_hook);
2234 /* If it has changed current-menubar from previous value,
2235 really recompute the menubar from the value. */
2236 if (! NILP (Vlucid_menu_bar_dirty_flag))
2237 call0 (Qrecompute_lucid_menubar);
2238 safe_run_hooks (Qmenu_bar_update_hook);
2239 FRAME_MENU_BAR_ITEMS (f) = menu_bar_items (FRAME_MENU_BAR_ITEMS (f));
2241 items = FRAME_MENU_BAR_ITEMS (f);
2243 /* Save the frame's previous menu bar contents data. */
2244 if (previous_menu_items_used)
2245 bcopy (XVECTOR (f->menu_bar_vector)->contents, previous_items,
2246 previous_menu_items_used * sizeof (Lisp_Object));
2248 /* Fill in menu_items with the current menu bar contents.
2249 This can evaluate Lisp code. */
2250 save_menu_items ();
2252 menu_items = f->menu_bar_vector;
2253 menu_items_allocated = VECTORP (menu_items) ? ASIZE (menu_items) : 0;
2254 submenu_start = (int *) alloca (XVECTOR (items)->size * sizeof (int *));
2255 submenu_end = (int *) alloca (XVECTOR (items)->size * sizeof (int *));
2256 submenu_n_panes = (int *) alloca (XVECTOR (items)->size * sizeof (int));
2257 submenu_top_level_items
2258 = (int *) alloca (XVECTOR (items)->size * sizeof (int *));
2259 init_menu_items ();
2260 for (i = 0; i < XVECTOR (items)->size; i += 4)
2262 Lisp_Object key, string, maps;
2264 last_i = i;
2266 key = XVECTOR (items)->contents[i];
2267 string = XVECTOR (items)->contents[i + 1];
2268 maps = XVECTOR (items)->contents[i + 2];
2269 if (NILP (string))
2270 break;
2272 submenu_start[i] = menu_items_used;
2274 menu_items_n_panes = 0;
2275 submenu_top_level_items[i]
2276 = parse_single_submenu (key, string, maps);
2277 submenu_n_panes[i] = menu_items_n_panes;
2279 submenu_end[i] = menu_items_used;
2282 finish_menu_items ();
2284 /* Convert menu_items into widget_value trees
2285 to display the menu. This cannot evaluate Lisp code. */
2287 wv = xmalloc_widget_value ();
2288 wv->name = "menubar";
2289 wv->value = 0;
2290 wv->enabled = 1;
2291 wv->button_type = BUTTON_TYPE_NONE;
2292 wv->help = Qnil;
2293 first_wv = wv;
2295 for (i = 0; i < last_i; i += 4)
2297 menu_items_n_panes = submenu_n_panes[i];
2298 wv = digest_single_submenu (submenu_start[i], submenu_end[i],
2299 submenu_top_level_items[i]);
2300 if (prev_wv)
2301 prev_wv->next = wv;
2302 else
2303 first_wv->contents = wv;
2304 /* Don't set wv->name here; GC during the loop might relocate it. */
2305 wv->enabled = 1;
2306 wv->button_type = BUTTON_TYPE_NONE;
2307 prev_wv = wv;
2310 set_buffer_internal_1 (prev);
2312 /* If there has been no change in the Lisp-level contents
2313 of the menu bar, skip redisplaying it. Just exit. */
2315 /* Compare the new menu items with the ones computed last time. */
2316 for (i = 0; i < previous_menu_items_used; i++)
2317 if (menu_items_used == i
2318 || (!EQ (previous_items[i], XVECTOR (menu_items)->contents[i])))
2319 break;
2320 if (i == menu_items_used && i == previous_menu_items_used && i != 0)
2322 /* The menu items have not changed. Don't bother updating
2323 the menus in any form, since it would be a no-op. */
2324 free_menubar_widget_value_tree (first_wv);
2325 discard_menu_items ();
2326 unbind_to (specpdl_count, Qnil);
2327 return;
2330 /* The menu items are different, so store them in the frame. */
2331 f->menu_bar_vector = menu_items;
2332 f->menu_bar_items_used = menu_items_used;
2334 /* This calls restore_menu_items to restore menu_items, etc.,
2335 as they were outside. */
2336 unbind_to (specpdl_count, Qnil);
2338 /* Now GC cannot happen during the lifetime of the widget_value,
2339 so it's safe to store data from a Lisp_String. */
2340 wv = first_wv->contents;
2341 for (i = 0; i < XVECTOR (items)->size; i += 4)
2343 Lisp_Object string;
2344 string = XVECTOR (items)->contents[i + 1];
2345 if (NILP (string))
2346 break;
2347 wv->name = (char *) SDATA (string);
2348 update_submenu_strings (wv->contents);
2349 wv = wv->next;
2353 else
2355 /* Make a widget-value tree containing
2356 just the top level menu bar strings. */
2358 wv = xmalloc_widget_value ();
2359 wv->name = "menubar";
2360 wv->value = 0;
2361 wv->enabled = 1;
2362 wv->button_type = BUTTON_TYPE_NONE;
2363 wv->help = Qnil;
2364 first_wv = wv;
2366 items = FRAME_MENU_BAR_ITEMS (f);
2367 for (i = 0; i < XVECTOR (items)->size; i += 4)
2369 Lisp_Object string;
2371 string = XVECTOR (items)->contents[i + 1];
2372 if (NILP (string))
2373 break;
2375 wv = xmalloc_widget_value ();
2376 wv->name = (char *) SDATA (string);
2377 wv->value = 0;
2378 wv->enabled = 1;
2379 wv->button_type = BUTTON_TYPE_NONE;
2380 wv->help = Qnil;
2381 /* This prevents lwlib from assuming this
2382 menu item is really supposed to be empty. */
2383 /* The EMACS_INT cast avoids a warning.
2384 This value just has to be different from small integers. */
2385 wv->call_data = (void *) (EMACS_INT) (-1);
2387 if (prev_wv)
2388 prev_wv->next = wv;
2389 else
2390 first_wv->contents = wv;
2391 prev_wv = wv;
2394 /* Forget what we thought we knew about what is in the
2395 detailed contents of the menu bar menus.
2396 Changing the top level always destroys the contents. */
2397 f->menu_bar_items_used = 0;
2400 /* Create or update the menu bar widget. */
2402 BLOCK_INPUT;
2404 #ifdef USE_GTK
2405 xg_crazy_callback_abort = 1;
2406 if (menubar_widget)
2408 /* The fourth arg is DEEP_P, which says to consider the entire
2409 menu trees we supply, rather than just the menu bar item names. */
2410 xg_modify_menubar_widgets (menubar_widget,
2412 first_wv,
2413 deep_p,
2414 G_CALLBACK (menubar_selection_callback),
2415 G_CALLBACK (popup_deactivate_callback),
2416 G_CALLBACK (menu_highlight_callback));
2418 else
2420 GtkWidget *wvbox = f->output_data.x->vbox_widget;
2422 menubar_widget
2423 = xg_create_widget ("menubar", "menubar", f, first_wv,
2424 G_CALLBACK (menubar_selection_callback),
2425 G_CALLBACK (popup_deactivate_callback),
2426 G_CALLBACK (menu_highlight_callback));
2428 f->output_data.x->menubar_widget = menubar_widget;
2432 #else /* not USE_GTK */
2433 if (menubar_widget)
2435 /* Disable resizing (done for Motif!) */
2436 lw_allow_resizing (f->output_data.x->widget, False);
2438 /* The third arg is DEEP_P, which says to consider the entire
2439 menu trees we supply, rather than just the menu bar item names. */
2440 lw_modify_all_widgets (id, first_wv, deep_p);
2442 /* Re-enable the edit widget to resize. */
2443 lw_allow_resizing (f->output_data.x->widget, True);
2445 else
2447 char menuOverride[] = "Ctrl<KeyPress>g: MenuGadgetEscape()";
2448 XtTranslations override = XtParseTranslationTable (menuOverride);
2450 menubar_widget = lw_create_widget ("menubar", "menubar", id, first_wv,
2451 f->output_data.x->column_widget,
2453 popup_activate_callback,
2454 menubar_selection_callback,
2455 popup_deactivate_callback,
2456 menu_highlight_callback);
2457 f->output_data.x->menubar_widget = menubar_widget;
2459 /* Make menu pop down on C-g. */
2460 XtOverrideTranslations (menubar_widget, override);
2464 int menubar_size
2465 = (f->output_data.x->menubar_widget
2466 ? (f->output_data.x->menubar_widget->core.height
2467 + f->output_data.x->menubar_widget->core.border_width)
2468 : 0);
2470 #if 0 /* Experimentally, we now get the right results
2471 for -geometry -0-0 without this. 24 Aug 96, rms. */
2472 #ifdef USE_LUCID
2473 if (FRAME_EXTERNAL_MENU_BAR (f))
2475 Dimension ibw = 0;
2476 XtVaGetValues (f->output_data.x->column_widget,
2477 XtNinternalBorderWidth, &ibw, NULL);
2478 menubar_size += ibw;
2480 #endif /* USE_LUCID */
2481 #endif /* 0 */
2483 f->output_data.x->menubar_height = menubar_size;
2485 #endif /* not USE_GTK */
2487 free_menubar_widget_value_tree (first_wv);
2488 update_frame_menubar (f);
2490 #ifdef USE_GTK
2491 xg_crazy_callback_abort = 0;
2492 #endif
2494 UNBLOCK_INPUT;
2497 /* Called from Fx_create_frame to create the initial menubar of a frame
2498 before it is mapped, so that the window is mapped with the menubar already
2499 there instead of us tacking it on later and thrashing the window after it
2500 is visible. */
2502 void
2503 initialize_frame_menubar (f)
2504 FRAME_PTR f;
2506 /* This function is called before the first chance to redisplay
2507 the frame. It has to be, so the frame will have the right size. */
2508 FRAME_MENU_BAR_ITEMS (f) = menu_bar_items (FRAME_MENU_BAR_ITEMS (f));
2509 set_frame_menubar (f, 1, 1);
2513 /* Get rid of the menu bar of frame F, and free its storage.
2514 This is used when deleting a frame, and when turning off the menu bar.
2515 For GTK this function is in gtkutil.c. */
2517 #ifndef USE_GTK
2518 void
2519 free_frame_menubar (f)
2520 FRAME_PTR f;
2522 Widget menubar_widget;
2524 if (! FRAME_X_P (f))
2525 abort ();
2527 menubar_widget = f->output_data.x->menubar_widget;
2529 f->output_data.x->menubar_height = 0;
2531 if (menubar_widget)
2533 #ifdef USE_MOTIF
2534 /* Removing the menu bar magically changes the shell widget's x
2535 and y position of (0, 0) which, when the menu bar is turned
2536 on again, leads to pull-down menuss appearing in strange
2537 positions near the upper-left corner of the display. This
2538 happens only with some window managers like twm and ctwm,
2539 but not with other like Motif's mwm or kwm, because the
2540 latter generate ConfigureNotify events when the menu bar
2541 is switched off, which fixes the shell position. */
2542 Position x0, y0, x1, y1;
2543 #endif
2545 BLOCK_INPUT;
2547 #ifdef USE_MOTIF
2548 if (f->output_data.x->widget)
2549 XtVaGetValues (f->output_data.x->widget, XtNx, &x0, XtNy, &y0, NULL);
2550 #endif
2552 lw_destroy_all_widgets ((LWLIB_ID) f->output_data.x->id);
2553 f->output_data.x->menubar_widget = NULL;
2555 #ifdef USE_MOTIF
2556 if (f->output_data.x->widget)
2558 XtVaGetValues (f->output_data.x->widget, XtNx, &x1, XtNy, &y1, NULL);
2559 if (x1 == 0 && y1 == 0)
2560 XtVaSetValues (f->output_data.x->widget, XtNx, x0, XtNy, y0, NULL);
2562 #endif
2564 UNBLOCK_INPUT;
2567 #endif /* not USE_GTK */
2569 #endif /* USE_X_TOOLKIT || USE_GTK */
2571 /* xmenu_show actually displays a menu using the panes and items in menu_items
2572 and returns the value selected from it.
2573 There are two versions of xmenu_show, one for Xt and one for Xlib.
2574 Both assume input is blocked by the caller. */
2576 /* F is the frame the menu is for.
2577 X and Y are the frame-relative specified position,
2578 relative to the inside upper left corner of the frame F.
2579 FOR_CLICK is nonzero if this menu was invoked for a mouse click.
2580 KEYMAPS is 1 if this menu was specified with keymaps;
2581 in that case, we return a list containing the chosen item's value
2582 and perhaps also the pane's prefix.
2583 TITLE is the specified menu title.
2584 ERROR is a place to store an error message string in case of failure.
2585 (We return nil on failure, but the value doesn't actually matter.) */
2587 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
2589 /* The item selected in the popup menu. */
2590 static Lisp_Object *volatile menu_item_selection;
2592 #ifdef USE_GTK
2594 /* Used when position a popup menu. See menu_position_func and
2595 create_and_show_popup_menu below. */
2596 struct next_popup_x_y
2598 FRAME_PTR f;
2599 int x;
2600 int y;
2603 /* The menu position function to use if we are not putting a popup
2604 menu where the pointer is.
2605 MENU is the menu to pop up.
2606 X and Y shall on exit contain x/y where the menu shall pop up.
2607 PUSH_IN is not documented in the GTK manual.
2608 USER_DATA is any data passed in when calling gtk_menu_popup.
2609 Here it points to a struct next_popup_x_y where the coordinates
2610 to store in *X and *Y are as well as the frame for the popup.
2612 Here only X and Y are used. */
2613 static void
2614 menu_position_func (menu, x, y, push_in, user_data)
2615 GtkMenu *menu;
2616 gint *x;
2617 gint *y;
2618 gboolean *push_in;
2619 gpointer user_data;
2621 struct next_popup_x_y* data = (struct next_popup_x_y*)user_data;
2622 GtkRequisition req;
2623 int disp_width = FRAME_X_DISPLAY_INFO (data->f)->width;
2624 int disp_height = FRAME_X_DISPLAY_INFO (data->f)->height;
2626 *x = data->x;
2627 *y = data->y;
2629 /* Check if there is room for the menu. If not, adjust x/y so that
2630 the menu is fully visible. */
2631 gtk_widget_size_request (GTK_WIDGET (menu), &req);
2632 if (data->x + req.width > disp_width)
2633 *x -= data->x + req.width - disp_width;
2634 if (data->y + req.height > disp_height)
2635 *y -= data->y + req.height - disp_height;
2638 static void
2639 popup_selection_callback (widget, client_data)
2640 GtkWidget *widget;
2641 gpointer client_data;
2643 xg_menu_item_cb_data *cb_data = (xg_menu_item_cb_data*) client_data;
2645 if (xg_crazy_callback_abort) return;
2646 if (cb_data) menu_item_selection = (Lisp_Object *) cb_data->call_data;
2649 static Lisp_Object
2650 pop_down_menu (arg)
2651 Lisp_Object arg;
2653 struct Lisp_Save_Value *p = XSAVE_VALUE (arg);
2655 popup_activated_flag = 0;
2656 BLOCK_INPUT;
2657 gtk_widget_destroy (GTK_WIDGET (p->pointer));
2658 UNBLOCK_INPUT;
2659 return Qnil;
2662 /* Pop up the menu for frame F defined by FIRST_WV at X/Y and loop until the
2663 menu pops down.
2664 menu_item_selection will be set to the selection. */
2665 static void
2666 create_and_show_popup_menu (f, first_wv, x, y, for_click)
2667 FRAME_PTR f;
2668 widget_value *first_wv;
2669 int x;
2670 int y;
2671 int for_click;
2673 int i;
2674 GtkWidget *menu;
2675 GtkMenuPositionFunc pos_func = 0; /* Pop up at pointer. */
2676 struct next_popup_x_y popup_x_y;
2677 int specpdl_count = SPECPDL_INDEX ();
2679 if (! FRAME_X_P (f))
2680 abort ();
2682 xg_crazy_callback_abort = 1;
2683 menu = xg_create_widget ("popup", first_wv->name, f, first_wv,
2684 G_CALLBACK (popup_selection_callback),
2685 G_CALLBACK (popup_deactivate_callback),
2686 G_CALLBACK (menu_highlight_callback));
2687 xg_crazy_callback_abort = 0;
2689 if (! for_click)
2691 /* Not invoked by a click. pop up at x/y. */
2692 pos_func = menu_position_func;
2694 /* Adjust coordinates to be root-window-relative. */
2695 x += f->left_pos + FRAME_OUTER_TO_INNER_DIFF_X (f);
2696 y += f->top_pos + FRAME_OUTER_TO_INNER_DIFF_Y (f);
2698 popup_x_y.x = x;
2699 popup_x_y.y = y;
2700 popup_x_y.f = f;
2702 i = 0; /* gtk_menu_popup needs this to be 0 for a non-button popup. */
2704 else
2706 for (i = 0; i < 5; i++)
2707 if (FRAME_X_DISPLAY_INFO (f)->grabbed & (1 << i))
2708 break;
2711 /* Display the menu. */
2712 gtk_widget_show_all (menu);
2713 gtk_menu_popup (GTK_MENU (menu), 0, 0, pos_func, &popup_x_y, i, 0);
2715 record_unwind_protect (pop_down_menu, make_save_value (menu, 0));
2717 if (GTK_WIDGET_MAPPED (menu))
2719 /* Set this to one. popup_widget_loop increases it by one, so it becomes
2720 two. show_help_echo uses this to detect popup menus. */
2721 popup_activated_flag = 1;
2722 /* Process events that apply to the menu. */
2723 popup_widget_loop (1, menu);
2726 unbind_to (specpdl_count, Qnil);
2728 /* Must reset this manually because the button release event is not passed
2729 to Emacs event loop. */
2730 FRAME_X_DISPLAY_INFO (f)->grabbed = 0;
2733 #else /* not USE_GTK */
2735 /* We need a unique id for each widget handled by the Lucid Widget
2736 library.
2738 For the main windows, and popup menus, we use this counter,
2739 which we increment each time after use. This starts from 1<<16.
2741 For menu bars, we use numbers starting at 0, counted in
2742 next_menubar_widget_id. */
2743 LWLIB_ID widget_id_tick;
2745 static void
2746 popup_selection_callback (widget, id, client_data)
2747 Widget widget;
2748 LWLIB_ID id;
2749 XtPointer client_data;
2751 menu_item_selection = (Lisp_Object *) client_data;
2754 /* ARG is the LWLIB ID of the dialog box, represented
2755 as a Lisp object as (HIGHPART . LOWPART). */
2757 static Lisp_Object
2758 pop_down_menu (arg)
2759 Lisp_Object arg;
2761 LWLIB_ID id = (XINT (XCAR (arg)) << 4 * sizeof (LWLIB_ID)
2762 | XINT (XCDR (arg)));
2764 BLOCK_INPUT;
2765 lw_destroy_all_widgets (id);
2766 UNBLOCK_INPUT;
2767 popup_activated_flag = 0;
2769 return Qnil;
2772 /* Pop up the menu for frame F defined by FIRST_WV at X/Y and loop until the
2773 menu pops down.
2774 menu_item_selection will be set to the selection. */
2775 static void
2776 create_and_show_popup_menu (f, first_wv, x, y, for_click)
2777 FRAME_PTR f;
2778 widget_value *first_wv;
2779 int x;
2780 int y;
2781 int for_click;
2783 int i;
2784 Arg av[2];
2785 int ac = 0;
2786 XButtonPressedEvent dummy;
2787 LWLIB_ID menu_id;
2788 Widget menu;
2790 if (! FRAME_X_P (f))
2791 abort ();
2793 menu_id = widget_id_tick++;
2794 menu = lw_create_widget ("popup", first_wv->name, menu_id, first_wv,
2795 f->output_data.x->widget, 1, 0,
2796 popup_selection_callback,
2797 popup_deactivate_callback,
2798 menu_highlight_callback);
2800 dummy.type = ButtonPress;
2801 dummy.serial = 0;
2802 dummy.send_event = 0;
2803 dummy.display = FRAME_X_DISPLAY (f);
2804 dummy.time = CurrentTime;
2805 dummy.root = FRAME_X_DISPLAY_INFO (f)->root_window;
2806 dummy.window = dummy.root;
2807 dummy.subwindow = dummy.root;
2808 dummy.x = x;
2809 dummy.y = y;
2811 /* Adjust coordinates to be root-window-relative. */
2812 x += f->left_pos + FRAME_OUTER_TO_INNER_DIFF_X (f);
2813 y += f->top_pos + FRAME_OUTER_TO_INNER_DIFF_Y (f);
2815 dummy.x_root = x;
2816 dummy.y_root = y;
2818 dummy.state = 0;
2819 dummy.button = 0;
2820 for (i = 0; i < 5; i++)
2821 if (FRAME_X_DISPLAY_INFO (f)->grabbed & (1 << i))
2822 dummy.button = i;
2824 /* Don't allow any geometry request from the user. */
2825 XtSetArg (av[ac], XtNgeometry, 0); ac++;
2826 XtSetValues (menu, av, ac);
2828 /* Display the menu. */
2829 lw_popup_menu (menu, (XEvent *) &dummy);
2830 popup_activated_flag = 1;
2833 int fact = 4 * sizeof (LWLIB_ID);
2834 int specpdl_count = SPECPDL_INDEX ();
2835 record_unwind_protect (pop_down_menu,
2836 Fcons (make_number (menu_id >> (fact)),
2837 make_number (menu_id & ~(-1 << (fact)))));
2839 /* Process events that apply to the menu. */
2840 popup_get_selection ((XEvent *) 0, FRAME_X_DISPLAY_INFO (f), menu_id, 1);
2842 unbind_to (specpdl_count, Qnil);
2846 #endif /* not USE_GTK */
2848 static Lisp_Object
2849 xmenu_show (f, x, y, for_click, keymaps, title, error)
2850 FRAME_PTR f;
2851 int x;
2852 int y;
2853 int for_click;
2854 int keymaps;
2855 Lisp_Object title;
2856 char **error;
2858 int i;
2859 widget_value *wv, *save_wv = 0, *first_wv = 0, *prev_wv = 0;
2860 widget_value **submenu_stack
2861 = (widget_value **) alloca (menu_items_used * sizeof (widget_value *));
2862 Lisp_Object *subprefix_stack
2863 = (Lisp_Object *) alloca (menu_items_used * sizeof (Lisp_Object));
2864 int submenu_depth = 0;
2866 int first_pane;
2868 if (! FRAME_X_P (f))
2869 abort ();
2871 *error = NULL;
2873 if (menu_items_used <= MENU_ITEMS_PANE_LENGTH)
2875 *error = "Empty menu";
2876 return Qnil;
2879 /* Create a tree of widget_value objects
2880 representing the panes and their items. */
2881 wv = xmalloc_widget_value ();
2882 wv->name = "menu";
2883 wv->value = 0;
2884 wv->enabled = 1;
2885 wv->button_type = BUTTON_TYPE_NONE;
2886 wv->help =Qnil;
2887 first_wv = wv;
2888 first_pane = 1;
2890 /* Loop over all panes and items, filling in the tree. */
2891 i = 0;
2892 while (i < menu_items_used)
2894 if (EQ (XVECTOR (menu_items)->contents[i], Qnil))
2896 submenu_stack[submenu_depth++] = save_wv;
2897 save_wv = prev_wv;
2898 prev_wv = 0;
2899 first_pane = 1;
2900 i++;
2902 else if (EQ (XVECTOR (menu_items)->contents[i], Qlambda))
2904 prev_wv = save_wv;
2905 save_wv = submenu_stack[--submenu_depth];
2906 first_pane = 0;
2907 i++;
2909 else if (EQ (XVECTOR (menu_items)->contents[i], Qt)
2910 && submenu_depth != 0)
2911 i += MENU_ITEMS_PANE_LENGTH;
2912 /* Ignore a nil in the item list.
2913 It's meaningful only for dialog boxes. */
2914 else if (EQ (XVECTOR (menu_items)->contents[i], Qquote))
2915 i += 1;
2916 else if (EQ (XVECTOR (menu_items)->contents[i], Qt))
2918 /* Create a new pane. */
2919 Lisp_Object pane_name, prefix;
2920 char *pane_string;
2922 pane_name = AREF (menu_items, i + MENU_ITEMS_PANE_NAME);
2923 prefix = AREF (menu_items, i + MENU_ITEMS_PANE_PREFIX);
2925 #ifndef HAVE_MULTILINGUAL_MENU
2926 if (STRINGP (pane_name) && STRING_MULTIBYTE (pane_name))
2928 pane_name = ENCODE_MENU_STRING (pane_name);
2929 AREF (menu_items, i + MENU_ITEMS_PANE_NAME) = pane_name;
2931 #endif
2932 pane_string = (NILP (pane_name)
2933 ? "" : (char *) SDATA (pane_name));
2934 /* If there is just one top-level pane, put all its items directly
2935 under the top-level menu. */
2936 if (menu_items_n_panes == 1)
2937 pane_string = "";
2939 /* If the pane has a meaningful name,
2940 make the pane a top-level menu item
2941 with its items as a submenu beneath it. */
2942 if (!keymaps && strcmp (pane_string, ""))
2944 wv = xmalloc_widget_value ();
2945 if (save_wv)
2946 save_wv->next = wv;
2947 else
2948 first_wv->contents = wv;
2949 wv->name = pane_string;
2950 if (keymaps && !NILP (prefix))
2951 wv->name++;
2952 wv->value = 0;
2953 wv->enabled = 1;
2954 wv->button_type = BUTTON_TYPE_NONE;
2955 wv->help = Qnil;
2956 save_wv = wv;
2957 prev_wv = 0;
2959 else if (first_pane)
2961 save_wv = wv;
2962 prev_wv = 0;
2964 first_pane = 0;
2965 i += MENU_ITEMS_PANE_LENGTH;
2967 else
2969 /* Create a new item within current pane. */
2970 Lisp_Object item_name, enable, descrip, def, type, selected, help;
2971 item_name = AREF (menu_items, i + MENU_ITEMS_ITEM_NAME);
2972 enable = AREF (menu_items, i + MENU_ITEMS_ITEM_ENABLE);
2973 descrip = AREF (menu_items, i + MENU_ITEMS_ITEM_EQUIV_KEY);
2974 def = AREF (menu_items, i + MENU_ITEMS_ITEM_DEFINITION);
2975 type = AREF (menu_items, i + MENU_ITEMS_ITEM_TYPE);
2976 selected = AREF (menu_items, i + MENU_ITEMS_ITEM_SELECTED);
2977 help = AREF (menu_items, i + MENU_ITEMS_ITEM_HELP);
2979 #ifndef HAVE_MULTILINGUAL_MENU
2980 if (STRINGP (item_name) && STRING_MULTIBYTE (item_name))
2982 item_name = ENCODE_MENU_STRING (item_name);
2983 AREF (menu_items, i + MENU_ITEMS_ITEM_NAME) = item_name;
2986 if (STRINGP (descrip) && STRING_MULTIBYTE (descrip))
2988 descrip = ENCODE_MENU_STRING (descrip);
2989 AREF (menu_items, i + MENU_ITEMS_ITEM_EQUIV_KEY) = descrip;
2991 #endif /* not HAVE_MULTILINGUAL_MENU */
2993 wv = xmalloc_widget_value ();
2994 if (prev_wv)
2995 prev_wv->next = wv;
2996 else
2997 save_wv->contents = wv;
2998 wv->name = (char *) SDATA (item_name);
2999 if (!NILP (descrip))
3000 wv->key = (char *) SDATA (descrip);
3001 wv->value = 0;
3002 /* If this item has a null value,
3003 make the call_data null so that it won't display a box
3004 when the mouse is on it. */
3005 wv->call_data
3006 = (!NILP (def) ? (void *) &XVECTOR (menu_items)->contents[i] : 0);
3007 wv->enabled = !NILP (enable);
3009 if (NILP (type))
3010 wv->button_type = BUTTON_TYPE_NONE;
3011 else if (EQ (type, QCtoggle))
3012 wv->button_type = BUTTON_TYPE_TOGGLE;
3013 else if (EQ (type, QCradio))
3014 wv->button_type = BUTTON_TYPE_RADIO;
3015 else
3016 abort ();
3018 wv->selected = !NILP (selected);
3020 if (! STRINGP (help))
3021 help = Qnil;
3023 wv->help = help;
3025 prev_wv = wv;
3027 i += MENU_ITEMS_ITEM_LENGTH;
3031 /* Deal with the title, if it is non-nil. */
3032 if (!NILP (title))
3034 widget_value *wv_title = xmalloc_widget_value ();
3035 widget_value *wv_sep1 = xmalloc_widget_value ();
3036 widget_value *wv_sep2 = xmalloc_widget_value ();
3038 wv_sep2->name = "--";
3039 wv_sep2->next = first_wv->contents;
3040 wv_sep2->help = Qnil;
3042 wv_sep1->name = "--";
3043 wv_sep1->next = wv_sep2;
3044 wv_sep1->help = Qnil;
3046 #ifndef HAVE_MULTILINGUAL_MENU
3047 if (STRING_MULTIBYTE (title))
3048 title = ENCODE_MENU_STRING (title);
3049 #endif
3051 wv_title->name = (char *) SDATA (title);
3052 wv_title->enabled = TRUE;
3053 wv_title->button_type = BUTTON_TYPE_NONE;
3054 wv_title->next = wv_sep1;
3055 wv_title->help = Qnil;
3056 first_wv->contents = wv_title;
3059 /* No selection has been chosen yet. */
3060 menu_item_selection = 0;
3062 /* Actually create and show the menu until popped down. */
3063 create_and_show_popup_menu (f, first_wv, x, y, for_click);
3065 /* Free the widget_value objects we used to specify the contents. */
3066 free_menubar_widget_value_tree (first_wv);
3068 /* Find the selected item, and its pane, to return
3069 the proper value. */
3070 if (menu_item_selection != 0)
3072 Lisp_Object prefix, entry;
3074 prefix = entry = Qnil;
3075 i = 0;
3076 while (i < menu_items_used)
3078 if (EQ (XVECTOR (menu_items)->contents[i], Qnil))
3080 subprefix_stack[submenu_depth++] = prefix;
3081 prefix = entry;
3082 i++;
3084 else if (EQ (XVECTOR (menu_items)->contents[i], Qlambda))
3086 prefix = subprefix_stack[--submenu_depth];
3087 i++;
3089 else if (EQ (XVECTOR (menu_items)->contents[i], Qt))
3091 prefix
3092 = XVECTOR (menu_items)->contents[i + MENU_ITEMS_PANE_PREFIX];
3093 i += MENU_ITEMS_PANE_LENGTH;
3095 /* Ignore a nil in the item list.
3096 It's meaningful only for dialog boxes. */
3097 else if (EQ (XVECTOR (menu_items)->contents[i], Qquote))
3098 i += 1;
3099 else
3101 entry
3102 = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_VALUE];
3103 if (menu_item_selection == &XVECTOR (menu_items)->contents[i])
3105 if (keymaps != 0)
3107 int j;
3109 entry = Fcons (entry, Qnil);
3110 if (!NILP (prefix))
3111 entry = Fcons (prefix, entry);
3112 for (j = submenu_depth - 1; j >= 0; j--)
3113 if (!NILP (subprefix_stack[j]))
3114 entry = Fcons (subprefix_stack[j], entry);
3116 return entry;
3118 i += MENU_ITEMS_ITEM_LENGTH;
3122 else if (!for_click)
3123 /* Make "Cancel" equivalent to C-g. */
3124 Fsignal (Qquit, Qnil);
3126 return Qnil;
3129 #ifdef USE_GTK
3130 static void
3131 dialog_selection_callback (widget, client_data)
3132 GtkWidget *widget;
3133 gpointer client_data;
3135 /* The EMACS_INT cast avoids a warning. There's no problem
3136 as long as pointers have enough bits to hold small integers. */
3137 if ((int) (EMACS_INT) client_data != -1)
3138 menu_item_selection = (Lisp_Object *) client_data;
3140 popup_activated_flag = 0;
3143 /* Pop up the dialog for frame F defined by FIRST_WV and loop until the
3144 dialog pops down.
3145 menu_item_selection will be set to the selection. */
3146 static void
3147 create_and_show_dialog (f, first_wv)
3148 FRAME_PTR f;
3149 widget_value *first_wv;
3151 GtkWidget *menu;
3153 if (! FRAME_X_P (f))
3154 abort ();
3156 menu = xg_create_widget ("dialog", first_wv->name, f, first_wv,
3157 G_CALLBACK (dialog_selection_callback),
3158 G_CALLBACK (popup_deactivate_callback),
3161 if (menu)
3163 int specpdl_count = SPECPDL_INDEX ();
3164 record_unwind_protect (pop_down_menu, make_save_value (menu, 0));
3166 /* Display the menu. */
3167 gtk_widget_show_all (menu);
3169 /* Process events that apply to the menu. */
3170 popup_widget_loop (1, menu);
3172 unbind_to (specpdl_count, Qnil);
3176 #else /* not USE_GTK */
3177 static void
3178 dialog_selection_callback (widget, id, client_data)
3179 Widget widget;
3180 LWLIB_ID id;
3181 XtPointer client_data;
3183 /* The EMACS_INT cast avoids a warning. There's no problem
3184 as long as pointers have enough bits to hold small integers. */
3185 if ((int) (EMACS_INT) client_data != -1)
3186 menu_item_selection = (Lisp_Object *) client_data;
3188 BLOCK_INPUT;
3189 lw_destroy_all_widgets (id);
3190 UNBLOCK_INPUT;
3191 popup_activated_flag = 0;
3195 /* Pop up the dialog for frame F defined by FIRST_WV and loop until the
3196 dialog pops down.
3197 menu_item_selection will be set to the selection. */
3198 static void
3199 create_and_show_dialog (f, first_wv)
3200 FRAME_PTR f;
3201 widget_value *first_wv;
3203 LWLIB_ID dialog_id;
3205 if (!FRAME_X_P (f))
3206 abort();
3208 dialog_id = widget_id_tick++;
3209 lw_create_widget (first_wv->name, "dialog", dialog_id, first_wv,
3210 f->output_data.x->widget, 1, 0,
3211 dialog_selection_callback, 0, 0);
3212 lw_modify_all_widgets (dialog_id, first_wv->contents, True);
3214 /* Display the dialog box. */
3215 lw_pop_up_all_widgets (dialog_id);
3216 popup_activated_flag = 1;
3218 /* Process events that apply to the dialog box.
3219 Also handle timers. */
3221 int count = SPECPDL_INDEX ();
3222 int fact = 4 * sizeof (LWLIB_ID);
3224 /* xdialog_show_unwind is responsible for popping the dialog box down. */
3225 record_unwind_protect (pop_down_menu,
3226 Fcons (make_number (dialog_id >> (fact)),
3227 make_number (dialog_id & ~(-1 << (fact)))));
3229 popup_get_selection ((XEvent *) 0, FRAME_X_DISPLAY_INFO (f),
3230 dialog_id, 1);
3232 unbind_to (count, Qnil);
3236 #endif /* not USE_GTK */
3238 static char * button_names [] = {
3239 "button1", "button2", "button3", "button4", "button5",
3240 "button6", "button7", "button8", "button9", "button10" };
3242 static Lisp_Object
3243 xdialog_show (f, keymaps, title, header, error_name)
3244 FRAME_PTR f;
3245 int keymaps;
3246 Lisp_Object title, header;
3247 char **error_name;
3249 int i, nb_buttons=0;
3250 char dialog_name[6];
3252 widget_value *wv, *first_wv = 0, *prev_wv = 0;
3254 /* Number of elements seen so far, before boundary. */
3255 int left_count = 0;
3256 /* 1 means we've seen the boundary between left-hand elts and right-hand. */
3257 int boundary_seen = 0;
3259 if (! FRAME_X_P (f))
3260 abort ();
3262 *error_name = NULL;
3264 if (menu_items_n_panes > 1)
3266 *error_name = "Multiple panes in dialog box";
3267 return Qnil;
3270 /* Create a tree of widget_value objects
3271 representing the text label and buttons. */
3273 Lisp_Object pane_name, prefix;
3274 char *pane_string;
3275 pane_name = XVECTOR (menu_items)->contents[MENU_ITEMS_PANE_NAME];
3276 prefix = XVECTOR (menu_items)->contents[MENU_ITEMS_PANE_PREFIX];
3277 pane_string = (NILP (pane_name)
3278 ? "" : (char *) SDATA (pane_name));
3279 prev_wv = xmalloc_widget_value ();
3280 prev_wv->value = pane_string;
3281 if (keymaps && !NILP (prefix))
3282 prev_wv->name++;
3283 prev_wv->enabled = 1;
3284 prev_wv->name = "message";
3285 prev_wv->help = Qnil;
3286 first_wv = prev_wv;
3288 /* Loop over all panes and items, filling in the tree. */
3289 i = MENU_ITEMS_PANE_LENGTH;
3290 while (i < menu_items_used)
3293 /* Create a new item within current pane. */
3294 Lisp_Object item_name, enable, descrip;
3295 item_name = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_NAME];
3296 enable = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_ENABLE];
3297 descrip
3298 = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_EQUIV_KEY];
3300 if (NILP (item_name))
3302 free_menubar_widget_value_tree (first_wv);
3303 *error_name = "Submenu in dialog items";
3304 return Qnil;
3306 if (EQ (item_name, Qquote))
3308 /* This is the boundary between left-side elts
3309 and right-side elts. Stop incrementing right_count. */
3310 boundary_seen = 1;
3311 i++;
3312 continue;
3314 if (nb_buttons >= 9)
3316 free_menubar_widget_value_tree (first_wv);
3317 *error_name = "Too many dialog items";
3318 return Qnil;
3321 wv = xmalloc_widget_value ();
3322 prev_wv->next = wv;
3323 wv->name = (char *) button_names[nb_buttons];
3324 if (!NILP (descrip))
3325 wv->key = (char *) SDATA (descrip);
3326 wv->value = (char *) SDATA (item_name);
3327 wv->call_data = (void *) &XVECTOR (menu_items)->contents[i];
3328 wv->enabled = !NILP (enable);
3329 wv->help = Qnil;
3330 prev_wv = wv;
3332 if (! boundary_seen)
3333 left_count++;
3335 nb_buttons++;
3336 i += MENU_ITEMS_ITEM_LENGTH;
3339 /* If the boundary was not specified,
3340 by default put half on the left and half on the right. */
3341 if (! boundary_seen)
3342 left_count = nb_buttons - nb_buttons / 2;
3344 wv = xmalloc_widget_value ();
3345 wv->name = dialog_name;
3346 wv->help = Qnil;
3348 /* Frame title: 'Q' = Question, 'I' = Information.
3349 Can also have 'E' = Error if, one day, we want
3350 a popup for errors. */
3351 if (NILP(header))
3352 dialog_name[0] = 'Q';
3353 else
3354 dialog_name[0] = 'I';
3356 /* Dialog boxes use a really stupid name encoding
3357 which specifies how many buttons to use
3358 and how many buttons are on the right. */
3359 dialog_name[1] = '0' + nb_buttons;
3360 dialog_name[2] = 'B';
3361 dialog_name[3] = 'R';
3362 /* Number of buttons to put on the right. */
3363 dialog_name[4] = '0' + nb_buttons - left_count;
3364 dialog_name[5] = 0;
3365 wv->contents = first_wv;
3366 first_wv = wv;
3369 /* No selection has been chosen yet. */
3370 menu_item_selection = 0;
3372 /* Actually create and show the dialog. */
3373 create_and_show_dialog (f, first_wv);
3375 /* Free the widget_value objects we used to specify the contents. */
3376 free_menubar_widget_value_tree (first_wv);
3378 /* Find the selected item, and its pane, to return
3379 the proper value. */
3380 if (menu_item_selection != 0)
3382 Lisp_Object prefix;
3384 prefix = Qnil;
3385 i = 0;
3386 while (i < menu_items_used)
3388 Lisp_Object entry;
3390 if (EQ (XVECTOR (menu_items)->contents[i], Qt))
3392 prefix
3393 = XVECTOR (menu_items)->contents[i + MENU_ITEMS_PANE_PREFIX];
3394 i += MENU_ITEMS_PANE_LENGTH;
3396 else if (EQ (XVECTOR (menu_items)->contents[i], Qquote))
3398 /* This is the boundary between left-side elts and
3399 right-side elts. */
3400 ++i;
3402 else
3404 entry
3405 = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_VALUE];
3406 if (menu_item_selection == &XVECTOR (menu_items)->contents[i])
3408 if (keymaps != 0)
3410 entry = Fcons (entry, Qnil);
3411 if (!NILP (prefix))
3412 entry = Fcons (prefix, entry);
3414 return entry;
3416 i += MENU_ITEMS_ITEM_LENGTH;
3420 else
3421 /* Make "Cancel" equivalent to C-g. */
3422 Fsignal (Qquit, Qnil);
3424 return Qnil;
3427 #else /* not USE_X_TOOLKIT && not USE_GTK */
3429 /* The frame of the last activated non-toolkit menu bar.
3430 Used to generate menu help events. */
3432 static struct frame *menu_help_frame;
3435 /* Show help HELP_STRING, or clear help if HELP_STRING is null.
3437 PANE is the pane number, and ITEM is the menu item number in
3438 the menu (currently not used).
3440 This cannot be done with generating a HELP_EVENT because
3441 XMenuActivate contains a loop that doesn't let Emacs process
3442 keyboard events. */
3444 static void
3445 menu_help_callback (help_string, pane, item)
3446 char *help_string;
3447 int pane, item;
3449 extern Lisp_Object Qmenu_item;
3450 Lisp_Object *first_item;
3451 Lisp_Object pane_name;
3452 Lisp_Object menu_object;
3454 first_item = XVECTOR (menu_items)->contents;
3455 if (EQ (first_item[0], Qt))
3456 pane_name = first_item[MENU_ITEMS_PANE_NAME];
3457 else if (EQ (first_item[0], Qquote))
3458 /* This shouldn't happen, see xmenu_show. */
3459 pane_name = empty_string;
3460 else
3461 pane_name = first_item[MENU_ITEMS_ITEM_NAME];
3463 /* (menu-item MENU-NAME PANE-NUMBER) */
3464 menu_object = Fcons (Qmenu_item,
3465 Fcons (pane_name,
3466 Fcons (make_number (pane), Qnil)));
3467 show_help_echo (help_string ? build_string (help_string) : Qnil,
3468 Qnil, menu_object, make_number (item), 1);
3471 static Lisp_Object
3472 pop_down_menu (arg)
3473 Lisp_Object arg;
3475 struct Lisp_Save_Value *p1 = XSAVE_VALUE (Fcar (arg));
3476 struct Lisp_Save_Value *p2 = XSAVE_VALUE (Fcdr (arg));
3478 FRAME_PTR f = p1->pointer;
3479 XMenu *menu = p2->pointer;
3481 BLOCK_INPUT;
3482 #ifndef MSDOS
3483 XUngrabPointer (FRAME_X_DISPLAY (f), CurrentTime);
3484 XUngrabKeyboard (FRAME_X_DISPLAY (f), CurrentTime);
3485 #endif
3486 XMenuDestroy (FRAME_X_DISPLAY (f), menu);
3488 #ifdef HAVE_X_WINDOWS
3489 /* Assume the mouse has moved out of the X window.
3490 If it has actually moved in, we will get an EnterNotify. */
3491 x_mouse_leave (FRAME_X_DISPLAY_INFO (f));
3493 /* State that no mouse buttons are now held.
3494 (The oldXMenu code doesn't track this info for us.)
3495 That is not necessarily true, but the fiction leads to reasonable
3496 results, and it is a pain to ask which are actually held now. */
3497 FRAME_X_DISPLAY_INFO (f)->grabbed = 0;
3499 #endif /* HAVE_X_WINDOWS */
3501 UNBLOCK_INPUT;
3503 return Qnil;
3507 static Lisp_Object
3508 xmenu_show (f, x, y, for_click, keymaps, title, error)
3509 FRAME_PTR f;
3510 int x, y;
3511 int for_click;
3512 int keymaps;
3513 Lisp_Object title;
3514 char **error;
3516 Window root;
3517 XMenu *menu;
3518 int pane, selidx, lpane, status;
3519 Lisp_Object entry, pane_prefix;
3520 char *datap;
3521 int ulx, uly, width, height;
3522 int dispwidth, dispheight;
3523 int i, j, lines, maxlines;
3524 int maxwidth;
3525 int dummy_int;
3526 unsigned int dummy_uint;
3527 int specpdl_count = SPECPDL_INDEX ();
3529 if (! FRAME_X_P (f))
3530 abort ();
3532 *error = 0;
3533 if (menu_items_n_panes == 0)
3534 return Qnil;
3536 if (menu_items_used <= MENU_ITEMS_PANE_LENGTH)
3538 *error = "Empty menu";
3539 return Qnil;
3542 /* Figure out which root window F is on. */
3543 XGetGeometry (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), &root,
3544 &dummy_int, &dummy_int, &dummy_uint, &dummy_uint,
3545 &dummy_uint, &dummy_uint);
3547 /* Make the menu on that window. */
3548 menu = XMenuCreate (FRAME_X_DISPLAY (f), root, "emacs");
3549 if (menu == NULL)
3551 *error = "Can't create menu";
3552 return Qnil;
3555 /* Don't GC while we prepare and show the menu,
3556 because we give the oldxmenu library pointers to the
3557 contents of strings. */
3558 inhibit_garbage_collection ();
3560 #ifdef HAVE_X_WINDOWS
3561 /* Adjust coordinates to relative to the outer (window manager) window. */
3562 x += FRAME_OUTER_TO_INNER_DIFF_X (f);
3563 y += FRAME_OUTER_TO_INNER_DIFF_Y (f);
3564 #endif /* HAVE_X_WINDOWS */
3566 /* Adjust coordinates to be root-window-relative. */
3567 x += f->left_pos;
3568 y += f->top_pos;
3570 /* Create all the necessary panes and their items. */
3571 maxlines = lines = i = 0;
3572 while (i < menu_items_used)
3574 if (EQ (XVECTOR (menu_items)->contents[i], Qt))
3576 /* Create a new pane. */
3577 Lisp_Object pane_name, prefix;
3578 char *pane_string;
3580 maxlines = max (maxlines, lines);
3581 lines = 0;
3582 pane_name = XVECTOR (menu_items)->contents[i + MENU_ITEMS_PANE_NAME];
3583 prefix = XVECTOR (menu_items)->contents[i + MENU_ITEMS_PANE_PREFIX];
3584 pane_string = (NILP (pane_name)
3585 ? "" : (char *) SDATA (pane_name));
3586 if (keymaps && !NILP (prefix))
3587 pane_string++;
3589 lpane = XMenuAddPane (FRAME_X_DISPLAY (f), menu, pane_string, TRUE);
3590 if (lpane == XM_FAILURE)
3592 XMenuDestroy (FRAME_X_DISPLAY (f), menu);
3593 *error = "Can't create pane";
3594 return Qnil;
3596 i += MENU_ITEMS_PANE_LENGTH;
3598 /* Find the width of the widest item in this pane. */
3599 maxwidth = 0;
3600 j = i;
3601 while (j < menu_items_used)
3603 Lisp_Object item;
3604 item = XVECTOR (menu_items)->contents[j];
3605 if (EQ (item, Qt))
3606 break;
3607 if (NILP (item))
3609 j++;
3610 continue;
3612 width = SBYTES (item);
3613 if (width > maxwidth)
3614 maxwidth = width;
3616 j += MENU_ITEMS_ITEM_LENGTH;
3619 /* Ignore a nil in the item list.
3620 It's meaningful only for dialog boxes. */
3621 else if (EQ (XVECTOR (menu_items)->contents[i], Qquote))
3622 i += 1;
3623 else
3625 /* Create a new item within current pane. */
3626 Lisp_Object item_name, enable, descrip, help;
3627 unsigned char *item_data;
3628 char *help_string;
3630 item_name = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_NAME];
3631 enable = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_ENABLE];
3632 descrip
3633 = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_EQUIV_KEY];
3634 help = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_HELP];
3635 help_string = STRINGP (help) ? SDATA (help) : NULL;
3637 if (!NILP (descrip))
3639 int gap = maxwidth - SBYTES (item_name);
3640 #ifdef C_ALLOCA
3641 Lisp_Object spacer;
3642 spacer = Fmake_string (make_number (gap), make_number (' '));
3643 item_name = concat2 (item_name, spacer);
3644 item_name = concat2 (item_name, descrip);
3645 item_data = SDATA (item_name);
3646 #else
3647 /* if alloca is fast, use that to make the space,
3648 to reduce gc needs. */
3649 item_data
3650 = (unsigned char *) alloca (maxwidth
3651 + SBYTES (descrip) + 1);
3652 bcopy (SDATA (item_name), item_data,
3653 SBYTES (item_name));
3654 for (j = SCHARS (item_name); j < maxwidth; j++)
3655 item_data[j] = ' ';
3656 bcopy (SDATA (descrip), item_data + j,
3657 SBYTES (descrip));
3658 item_data[j + SBYTES (descrip)] = 0;
3659 #endif
3661 else
3662 item_data = SDATA (item_name);
3664 if (XMenuAddSelection (FRAME_X_DISPLAY (f),
3665 menu, lpane, 0, item_data,
3666 !NILP (enable), help_string)
3667 == XM_FAILURE)
3669 XMenuDestroy (FRAME_X_DISPLAY (f), menu);
3670 *error = "Can't add selection to menu";
3671 return Qnil;
3673 i += MENU_ITEMS_ITEM_LENGTH;
3674 lines++;
3678 maxlines = max (maxlines, lines);
3680 /* All set and ready to fly. */
3681 XMenuRecompute (FRAME_X_DISPLAY (f), menu);
3682 dispwidth = DisplayWidth (FRAME_X_DISPLAY (f), FRAME_X_SCREEN_NUMBER (f));
3683 dispheight = DisplayHeight (FRAME_X_DISPLAY (f), FRAME_X_SCREEN_NUMBER (f));
3684 x = min (x, dispwidth);
3685 y = min (y, dispheight);
3686 x = max (x, 1);
3687 y = max (y, 1);
3688 XMenuLocate (FRAME_X_DISPLAY (f), menu, 0, 0, x, y,
3689 &ulx, &uly, &width, &height);
3690 if (ulx+width > dispwidth)
3692 x -= (ulx + width) - dispwidth;
3693 ulx = dispwidth - width;
3695 if (uly+height > dispheight)
3697 y -= (uly + height) - dispheight;
3698 uly = dispheight - height;
3700 if (ulx < 0) x -= ulx;
3701 if (uly < 0) y -= uly;
3703 if (! for_click)
3705 /* If position was not given by a mouse click, adjust so upper left
3706 corner of the menu as a whole ends up at given coordinates. This
3707 is what x-popup-menu says in its documentation. */
3708 x += width/2;
3709 y += 1.5*height/(maxlines+2);
3712 XMenuSetAEQ (menu, TRUE);
3713 XMenuSetFreeze (menu, TRUE);
3714 pane = selidx = 0;
3716 #ifndef MSDOS
3717 XMenuActivateSetWaitFunction (x_menu_wait_for_event, FRAME_X_DISPLAY (f));
3718 #endif
3720 record_unwind_protect (pop_down_menu,
3721 Fcons (make_save_value (f, 0),
3722 make_save_value (menu, 0)));
3724 /* Help display under X won't work because XMenuActivate contains
3725 a loop that doesn't give Emacs a chance to process it. */
3726 menu_help_frame = f;
3727 status = XMenuActivate (FRAME_X_DISPLAY (f), menu, &pane, &selidx,
3728 x, y, ButtonReleaseMask, &datap,
3729 menu_help_callback);
3731 switch (status)
3733 case XM_SUCCESS:
3734 #ifdef XDEBUG
3735 fprintf (stderr, "pane= %d line = %d\n", panes, selidx);
3736 #endif
3738 /* Find the item number SELIDX in pane number PANE. */
3739 i = 0;
3740 while (i < menu_items_used)
3742 if (EQ (XVECTOR (menu_items)->contents[i], Qt))
3744 if (pane == 0)
3745 pane_prefix
3746 = XVECTOR (menu_items)->contents[i + MENU_ITEMS_PANE_PREFIX];
3747 pane--;
3748 i += MENU_ITEMS_PANE_LENGTH;
3750 else
3752 if (pane == -1)
3754 if (selidx == 0)
3756 entry
3757 = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_VALUE];
3758 if (keymaps != 0)
3760 entry = Fcons (entry, Qnil);
3761 if (!NILP (pane_prefix))
3762 entry = Fcons (pane_prefix, entry);
3764 break;
3766 selidx--;
3768 i += MENU_ITEMS_ITEM_LENGTH;
3771 break;
3773 case XM_FAILURE:
3774 *error = "Can't activate menu";
3775 case XM_IA_SELECT:
3776 entry = Qnil;
3777 break;
3778 case XM_NO_SELECT:
3779 /* Make "Cancel" equivalent to C-g unless FOR_CLICK (which means
3780 the menu was invoked with a mouse event as POSITION). */
3781 if (! for_click)
3782 Fsignal (Qquit, Qnil);
3783 entry = Qnil;
3784 break;
3787 unbind_to (specpdl_count, Qnil);
3789 return entry;
3792 #endif /* not USE_X_TOOLKIT */
3794 #endif /* HAVE_MENUS */
3796 void
3797 syms_of_xmenu ()
3799 staticpro (&menu_items);
3800 menu_items = Qnil;
3801 menu_items_inuse = Qnil;
3803 Qdebug_on_next_call = intern ("debug-on-next-call");
3804 staticpro (&Qdebug_on_next_call);
3806 DEFVAR_LISP ("menu-updating-frame", &Vmenu_updating_frame,
3807 doc: /* Frame for which we are updating a menu.
3808 The enable predicate for a menu command should check this variable. */);
3809 Vmenu_updating_frame = Qnil;
3811 #ifdef USE_X_TOOLKIT
3812 widget_id_tick = (1<<16);
3813 next_menubar_widget_id = 1;
3814 #endif
3816 defsubr (&Sx_popup_menu);
3818 #if defined (USE_GTK) || defined (USE_X_TOOLKIT)
3819 defsubr (&Sx_menu_bar_open);
3820 Fdefalias (intern ("accelerate-menu"),
3821 intern (Sx_menu_bar_open.symbol_name),
3822 Qnil);
3823 #endif
3825 #ifdef HAVE_MENUS
3826 defsubr (&Sx_popup_dialog);
3827 #endif
3830 /* arch-tag: 92ea573c-398e-496e-ac73-2436f7d63242
3831 (do not change this comment) */