(Fx_create_frame): Move unwind_create_frame setup down.
[emacs.git] / src / xmenu.c
blob5a71d82a424e54955d89e9e66d5069a047393f91
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 "termhooks.h"
45 #include "keyboard.h"
46 #include "keymap.h"
47 #include "frame.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 BLOCK_INPUT;
746 XQueryPointer (FRAME_X_DISPLAY (f),
747 DefaultRootWindow (FRAME_X_DISPLAY (f)),
749 /* The root window which contains the pointer. */
750 &root,
752 /* Window pointer is on, not used */
753 &dummy_window,
755 /* The position on that root window. */
756 x, y,
758 /* x/y in dummy_window coordinates, not used. */
759 &dummy, &dummy,
761 /* Modifier keys and pointer buttons, about which
762 we don't care. */
763 (unsigned int *) &dummy);
765 UNBLOCK_INPUT;
767 /* xmenu_show expects window coordinates, not root window
768 coordinates. Translate. */
769 *x -= f->left_pos + FRAME_OUTER_TO_INNER_DIFF_X (f);
770 *y -= f->top_pos + FRAME_OUTER_TO_INNER_DIFF_Y (f);
773 #endif /* HAVE_X_WINDOWS */
775 DEFUN ("x-popup-menu", Fx_popup_menu, Sx_popup_menu, 2, 2, 0,
776 doc: /* Pop up a deck-of-cards menu and return user's selection.
777 POSITION is a position specification. This is either a mouse button event
778 or a list ((XOFFSET YOFFSET) WINDOW)
779 where XOFFSET and YOFFSET are positions in pixels from the top left
780 corner of WINDOW. (WINDOW may be a window or a frame object.)
781 This controls the position of the top left of the menu as a whole.
782 If POSITION is t, it means to use the current mouse position.
784 MENU is a specifier for a menu. For the simplest case, MENU is a keymap.
785 The menu items come from key bindings that have a menu string as well as
786 a definition; actually, the "definition" in such a key binding looks like
787 \(STRING . REAL-DEFINITION). To give the menu a title, put a string into
788 the keymap as a top-level element.
790 If REAL-DEFINITION is nil, that puts a nonselectable string in the menu.
791 Otherwise, REAL-DEFINITION should be a valid key binding definition.
793 You can also use a list of keymaps as MENU.
794 Then each keymap makes a separate pane.
796 When MENU is a keymap or a list of keymaps, the return value is the
797 list of events corresponding to the user's choice. Note that
798 `x-popup-menu' does not actually execute the command bound to that
799 sequence of events.
801 Alternatively, you can specify a menu of multiple panes
802 with a list of the form (TITLE PANE1 PANE2...),
803 where each pane is a list of form (TITLE ITEM1 ITEM2...).
804 Each ITEM is normally a cons cell (STRING . VALUE);
805 but a string can appear as an item--that makes a nonselectable line
806 in the menu.
807 With this form of menu, the return value is VALUE from the chosen item.
809 If POSITION is nil, don't display the menu at all, just precalculate the
810 cached information about equivalent key sequences.
812 If the user gets rid of the menu without making a valid choice, for
813 instance by clicking the mouse away from a valid choice or by typing
814 keyboard input, then this normally results in a quit and
815 `x-popup-menu' does not return. But if POSITION is a mouse button
816 event (indicating that the user invoked the menu with the mouse) then
817 no quit occurs and `x-popup-menu' returns nil. */)
818 (position, menu)
819 Lisp_Object position, menu;
821 Lisp_Object keymap, tem;
822 int xpos = 0, ypos = 0;
823 Lisp_Object title;
824 char *error_name = NULL;
825 Lisp_Object selection;
826 FRAME_PTR f = NULL;
827 Lisp_Object x, y, window;
828 int keymaps = 0;
829 int for_click = 0;
830 int specpdl_count = SPECPDL_INDEX ();
831 struct gcpro gcpro1;
833 #ifdef HAVE_MENUS
834 if (! NILP (position))
836 int get_current_pos_p = 0;
837 check_x ();
839 /* Decode the first argument: find the window and the coordinates. */
840 if (EQ (position, Qt)
841 || (CONSP (position) && (EQ (XCAR (position), Qmenu_bar)
842 || EQ (XCAR (position), Qtool_bar))))
844 get_current_pos_p = 1;
846 else
848 tem = Fcar (position);
849 if (CONSP (tem))
851 window = Fcar (Fcdr (position));
852 x = XCAR (tem);
853 y = Fcar (XCDR (tem));
855 else
857 for_click = 1;
858 tem = Fcar (Fcdr (position)); /* EVENT_START (position) */
859 window = Fcar (tem); /* POSN_WINDOW (tem) */
860 tem = Fcar (Fcdr (Fcdr (tem))); /* POSN_WINDOW_POSN (tem) */
861 x = Fcar (tem);
862 y = Fcdr (tem);
865 /* If a click happens in an external tool bar or a detached
866 tool bar, x and y is NIL. In that case, use the current
867 mouse position. This happens for the help button in the
868 tool bar. Ideally popup-menu should pass NIL to
869 this function, but it doesn't. */
870 if (NILP (x) && NILP (y))
871 get_current_pos_p = 1;
874 if (get_current_pos_p)
876 /* Use the mouse's current position. */
877 FRAME_PTR new_f = SELECTED_FRAME ();
878 #ifdef HAVE_X_WINDOWS
879 /* Can't use mouse_position_hook for X since it returns
880 coordinates relative to the window the mouse is in,
881 we need coordinates relative to the edit widget always. */
882 if (new_f != 0)
884 int cur_x, cur_y;
886 mouse_position_for_popup (new_f, &cur_x, &cur_y);
887 /* cur_x/y may be negative, so use make_number. */
888 x = make_number (cur_x);
889 y = make_number (cur_y);
892 #else /* not HAVE_X_WINDOWS */
893 Lisp_Object bar_window;
894 enum scroll_bar_part part;
895 unsigned long time;
897 if (mouse_position_hook)
898 (*mouse_position_hook) (&new_f, 1, &bar_window,
899 &part, &x, &y, &time);
900 #endif /* not HAVE_X_WINDOWS */
902 if (new_f != 0)
903 XSETFRAME (window, new_f);
904 else
906 window = selected_window;
907 XSETFASTINT (x, 0);
908 XSETFASTINT (y, 0);
912 CHECK_NUMBER (x);
913 CHECK_NUMBER (y);
915 /* Decode where to put the menu. */
917 if (FRAMEP (window))
919 f = XFRAME (window);
920 xpos = 0;
921 ypos = 0;
923 else if (WINDOWP (window))
925 CHECK_LIVE_WINDOW (window);
926 f = XFRAME (WINDOW_FRAME (XWINDOW (window)));
928 xpos = WINDOW_LEFT_EDGE_X (XWINDOW (window));
929 ypos = WINDOW_TOP_EDGE_Y (XWINDOW (window));
931 else
932 /* ??? Not really clean; should be CHECK_WINDOW_OR_FRAME,
933 but I don't want to make one now. */
934 CHECK_WINDOW (window);
936 xpos += XINT (x);
937 ypos += XINT (y);
939 XSETFRAME (Vmenu_updating_frame, f);
941 else
942 Vmenu_updating_frame = Qnil;
943 #endif /* HAVE_MENUS */
945 record_unwind_protect (unuse_menu_items, Qnil);
946 title = Qnil;
947 GCPRO1 (title);
949 /* Decode the menu items from what was specified. */
951 keymap = get_keymap (menu, 0, 0);
952 if (CONSP (keymap))
954 /* We were given a keymap. Extract menu info from the keymap. */
955 Lisp_Object prompt;
957 /* Extract the detailed info to make one pane. */
958 keymap_panes (&menu, 1, NILP (position));
960 /* Search for a string appearing directly as an element of the keymap.
961 That string is the title of the menu. */
962 prompt = Fkeymap_prompt (keymap);
963 if (NILP (title) && !NILP (prompt))
964 title = prompt;
966 /* Make that be the pane title of the first pane. */
967 if (!NILP (prompt) && menu_items_n_panes >= 0)
968 XVECTOR (menu_items)->contents[MENU_ITEMS_PANE_NAME] = prompt;
970 keymaps = 1;
972 else if (CONSP (menu) && KEYMAPP (XCAR (menu)))
974 /* We were given a list of keymaps. */
975 int nmaps = XFASTINT (Flength (menu));
976 Lisp_Object *maps
977 = (Lisp_Object *) alloca (nmaps * sizeof (Lisp_Object));
978 int i;
980 title = Qnil;
982 /* The first keymap that has a prompt string
983 supplies the menu title. */
984 for (tem = menu, i = 0; CONSP (tem); tem = XCDR (tem))
986 Lisp_Object prompt;
988 maps[i++] = keymap = get_keymap (XCAR (tem), 1, 0);
990 prompt = Fkeymap_prompt (keymap);
991 if (NILP (title) && !NILP (prompt))
992 title = prompt;
995 /* Extract the detailed info to make one pane. */
996 keymap_panes (maps, nmaps, NILP (position));
998 /* Make the title be the pane title of the first pane. */
999 if (!NILP (title) && menu_items_n_panes >= 0)
1000 XVECTOR (menu_items)->contents[MENU_ITEMS_PANE_NAME] = title;
1002 keymaps = 1;
1004 else
1006 /* We were given an old-fashioned menu. */
1007 title = Fcar (menu);
1008 CHECK_STRING (title);
1010 list_of_panes (Fcdr (menu));
1012 keymaps = 0;
1015 unbind_to (specpdl_count, Qnil);
1017 if (NILP (position))
1019 discard_menu_items ();
1020 UNGCPRO;
1021 return Qnil;
1024 #ifdef HAVE_MENUS
1025 /* Display them in a menu. */
1026 BLOCK_INPUT;
1028 selection = xmenu_show (f, xpos, ypos, for_click,
1029 keymaps, title, &error_name);
1030 UNBLOCK_INPUT;
1032 discard_menu_items ();
1034 UNGCPRO;
1035 #endif /* HAVE_MENUS */
1037 if (error_name) error (error_name);
1038 return selection;
1041 #ifdef HAVE_MENUS
1043 DEFUN ("x-popup-dialog", Fx_popup_dialog, Sx_popup_dialog, 2, 3, 0,
1044 doc: /* Pop up a dialog box and return user's selection.
1045 POSITION specifies which frame to use.
1046 This is normally a mouse button event or a window or frame.
1047 If POSITION is t, it means to use the frame the mouse is on.
1048 The dialog box appears in the middle of the specified frame.
1050 CONTENTS specifies the alternatives to display in the dialog box.
1051 It is a list of the form (DIALOG ITEM1 ITEM2...).
1052 Each ITEM is a cons cell (STRING . VALUE).
1053 The return value is VALUE from the chosen item.
1055 An ITEM may also be just a string--that makes a nonselectable item.
1056 An ITEM may also be nil--that means to put all preceding items
1057 on the left of the dialog box and all following items on the right.
1058 \(By default, approximately half appear on each side.)
1060 If HEADER is non-nil, the frame title for the box is "Information",
1061 otherwise it is "Question".
1063 If the user gets rid of the dialog box without making a valid choice,
1064 for instance using the window manager, then this produces a quit and
1065 `x-popup-dialog' does not return. */)
1066 (position, contents, header)
1067 Lisp_Object position, contents, header;
1069 FRAME_PTR f = NULL;
1070 Lisp_Object window;
1072 check_x ();
1074 /* Decode the first argument: find the window or frame to use. */
1075 if (EQ (position, Qt)
1076 || (CONSP (position) && (EQ (XCAR (position), Qmenu_bar)
1077 || EQ (XCAR (position), Qtool_bar))))
1079 #if 0 /* Using the frame the mouse is on may not be right. */
1080 /* Use the mouse's current position. */
1081 FRAME_PTR new_f = SELECTED_FRAME ();
1082 Lisp_Object bar_window;
1083 enum scroll_bar_part part;
1084 unsigned long time;
1085 Lisp_Object x, y;
1087 (*mouse_position_hook) (&new_f, 1, &bar_window, &part, &x, &y, &time);
1089 if (new_f != 0)
1090 XSETFRAME (window, new_f);
1091 else
1092 window = selected_window;
1093 #endif
1094 window = selected_window;
1096 else if (CONSP (position))
1098 Lisp_Object tem;
1099 tem = Fcar (position);
1100 if (CONSP (tem))
1101 window = Fcar (Fcdr (position));
1102 else
1104 tem = Fcar (Fcdr (position)); /* EVENT_START (position) */
1105 window = Fcar (tem); /* POSN_WINDOW (tem) */
1108 else if (WINDOWP (position) || FRAMEP (position))
1109 window = position;
1110 else
1111 window = Qnil;
1113 /* Decode where to put the menu. */
1115 if (FRAMEP (window))
1116 f = XFRAME (window);
1117 else if (WINDOWP (window))
1119 CHECK_LIVE_WINDOW (window);
1120 f = XFRAME (WINDOW_FRAME (XWINDOW (window)));
1122 else
1123 /* ??? Not really clean; should be CHECK_WINDOW_OR_FRAME,
1124 but I don't want to make one now. */
1125 CHECK_WINDOW (window);
1127 #if ! defined (USE_X_TOOLKIT) && ! defined (USE_GTK)
1128 /* Display a menu with these alternatives
1129 in the middle of frame F. */
1131 Lisp_Object x, y, frame, newpos;
1132 XSETFRAME (frame, f);
1133 XSETINT (x, x_pixel_width (f) / 2);
1134 XSETINT (y, x_pixel_height (f) / 2);
1135 newpos = Fcons (Fcons (x, Fcons (y, Qnil)), Fcons (frame, Qnil));
1137 return Fx_popup_menu (newpos,
1138 Fcons (Fcar (contents), Fcons (contents, Qnil)));
1140 #else
1142 Lisp_Object title;
1143 char *error_name;
1144 Lisp_Object selection;
1145 int specpdl_count = SPECPDL_INDEX ();
1147 /* Decode the dialog items from what was specified. */
1148 title = Fcar (contents);
1149 CHECK_STRING (title);
1150 record_unwind_protect (unuse_menu_items, Qnil);
1152 if (NILP (Fcar (Fcdr (contents))))
1153 /* No buttons specified, add an "Ok" button so users can pop down
1154 the dialog. Also, the lesstif/motif version crashes if there are
1155 no buttons. */
1156 contents = Fcons (title, Fcons (Fcons (build_string ("Ok"), Qt), Qnil));
1158 list_of_panes (Fcons (contents, Qnil));
1160 /* Display them in a dialog box. */
1161 BLOCK_INPUT;
1162 selection = xdialog_show (f, 0, title, header, &error_name);
1163 UNBLOCK_INPUT;
1165 unbind_to (specpdl_count, Qnil);
1166 discard_menu_items ();
1168 if (error_name) error (error_name);
1169 return selection;
1171 #endif
1175 #ifndef MSDOS
1177 /* Set menu_items_inuse so no other popup menu or dialog is created. */
1179 void
1180 x_menu_set_in_use (in_use)
1181 int in_use;
1183 menu_items_inuse = in_use ? Qt : Qnil;
1184 popup_activated_flag = in_use;
1187 /* Wait for an X event to arrive or for a timer to expire. */
1189 void
1190 x_menu_wait_for_event (void *data)
1192 extern EMACS_TIME timer_check P_ ((int));
1194 /* Another way to do this is to register a timer callback, that can be
1195 done in GTK and Xt. But we have to do it like this when using only X
1196 anyway, and with callbacks we would have three variants for timer handling
1197 instead of the small ifdefs below. */
1199 while (
1200 #ifdef USE_X_TOOLKIT
1201 ! XtAppPending (Xt_app_con)
1202 #elif defined USE_GTK
1203 ! gtk_events_pending ()
1204 #else
1205 ! XPending ((Display*) data)
1206 #endif
1209 EMACS_TIME next_time = timer_check (1);
1210 long secs = EMACS_SECS (next_time);
1211 long usecs = EMACS_USECS (next_time);
1212 SELECT_TYPE read_fds;
1213 struct x_display_info *dpyinfo;
1214 int n = 0;
1216 FD_ZERO (&read_fds);
1217 for (dpyinfo = x_display_list; dpyinfo; dpyinfo = dpyinfo->next)
1219 int fd = ConnectionNumber (dpyinfo->display);
1220 FD_SET (fd, &read_fds);
1221 if (fd > n) n = fd;
1224 if (secs < 0 || (secs == 0 && usecs == 0))
1226 /* Sometimes timer_check returns -1 (no timers) even if there are
1227 timers. So do a timeout anyway. */
1228 EMACS_SET_SECS (next_time, 1);
1229 EMACS_SET_USECS (next_time, 0);
1232 select (n + 1, &read_fds, (SELECT_TYPE *)0, (SELECT_TYPE *)0, &next_time);
1235 #endif /* ! MSDOS */
1238 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
1240 #ifdef USE_X_TOOLKIT
1242 /* Loop in Xt until the menu pulldown or dialog popup has been
1243 popped down (deactivated). This is used for x-popup-menu
1244 and x-popup-dialog; it is not used for the menu bar.
1246 NOTE: All calls to popup_get_selection should be protected
1247 with BLOCK_INPUT, UNBLOCK_INPUT wrappers. */
1249 static void
1250 popup_get_selection (initial_event, dpyinfo, id, do_timers)
1251 XEvent *initial_event;
1252 struct x_display_info *dpyinfo;
1253 LWLIB_ID id;
1254 int do_timers;
1256 XEvent event;
1258 while (popup_activated_flag)
1260 if (initial_event)
1262 event = *initial_event;
1263 initial_event = 0;
1265 else
1267 if (do_timers) x_menu_wait_for_event (0);
1268 XtAppNextEvent (Xt_app_con, &event);
1271 /* Make sure we don't consider buttons grabbed after menu goes.
1272 And make sure to deactivate for any ButtonRelease,
1273 even if XtDispatchEvent doesn't do that. */
1274 if (event.type == ButtonRelease
1275 && dpyinfo->display == event.xbutton.display)
1277 dpyinfo->grabbed &= ~(1 << event.xbutton.button);
1278 #ifdef USE_MOTIF /* Pretending that the event came from a
1279 Btn1Down seems the only way to convince Motif to
1280 activate its callbacks; setting the XmNmenuPost
1281 isn't working. --marcus@sysc.pdx.edu. */
1282 event.xbutton.button = 1;
1283 /* Motif only pops down menus when no Ctrl, Alt or Mod
1284 key is pressed and the button is released. So reset key state
1285 so Motif thinks this is the case. */
1286 event.xbutton.state = 0;
1287 #endif
1289 /* Pop down on C-g and Escape. */
1290 else if (event.type == KeyPress
1291 && dpyinfo->display == event.xbutton.display)
1293 KeySym keysym = XLookupKeysym (&event.xkey, 0);
1295 if ((keysym == XK_g && (event.xkey.state & ControlMask) != 0)
1296 || keysym == XK_Escape) /* Any escape, ignore modifiers. */
1297 popup_activated_flag = 0;
1300 x_dispatch_event (&event, event.xany.display);
1304 DEFUN ("menu-bar-open", Fmenu_bar_open, Smenu_bar_open, 0, 1, "i",
1305 doc: /* Start key navigation of the menu bar in FRAME.
1306 This initially opens the first menu bar item and you can then navigate with the
1307 arrow keys, select a menu entry with the return key or cancel with the
1308 escape key. If FRAME has no menu bar this function does nothing.
1310 If FRAME is nil or not given, use the selected frame. */)
1311 (frame)
1312 Lisp_Object frame;
1314 XEvent ev;
1315 FRAME_PTR f = check_x_frame (frame);
1316 Widget menubar;
1317 BLOCK_INPUT;
1319 if (FRAME_EXTERNAL_MENU_BAR (f))
1320 set_frame_menubar (f, 0, 1);
1322 menubar = FRAME_X_OUTPUT (f)->menubar_widget;
1323 if (menubar)
1325 Window child;
1326 int error_p = 0;
1328 x_catch_errors (FRAME_X_DISPLAY (f));
1329 memset (&ev, 0, sizeof ev);
1330 ev.xbutton.display = FRAME_X_DISPLAY (f);
1331 ev.xbutton.window = XtWindow (menubar);
1332 ev.xbutton.root = FRAME_X_DISPLAY_INFO (f)->root_window;
1333 ev.xbutton.time = XtLastTimestampProcessed (FRAME_X_DISPLAY (f));
1334 ev.xbutton.button = Button1;
1335 ev.xbutton.x = ev.xbutton.y = FRAME_MENUBAR_HEIGHT (f) / 2;
1336 ev.xbutton.same_screen = True;
1338 #ifdef USE_MOTIF
1340 Arg al[2];
1341 WidgetList list;
1342 Cardinal nr;
1343 XtSetArg (al[0], XtNchildren, &list);
1344 XtSetArg (al[1], XtNnumChildren, &nr);
1345 XtGetValues (menubar, al, 2);
1346 ev.xbutton.window = XtWindow (list[0]);
1348 #endif
1350 XTranslateCoordinates (FRAME_X_DISPLAY (f),
1351 /* From-window, to-window. */
1352 ev.xbutton.window, ev.xbutton.root,
1354 /* From-position, to-position. */
1355 ev.xbutton.x, ev.xbutton.y,
1356 &ev.xbutton.x_root, &ev.xbutton.y_root,
1358 /* Child of win. */
1359 &child);
1360 error_p = x_had_errors_p (FRAME_X_DISPLAY (f));
1361 x_uncatch_errors ();
1363 if (! error_p)
1365 ev.type = ButtonPress;
1366 ev.xbutton.state = 0;
1368 XtDispatchEvent (&ev);
1369 ev.xbutton.type = ButtonRelease;
1370 ev.xbutton.state = Button1Mask;
1371 XtDispatchEvent (&ev);
1375 UNBLOCK_INPUT;
1377 return Qnil;
1379 #endif /* USE_X_TOOLKIT */
1382 #ifdef USE_GTK
1383 DEFUN ("menu-bar-open", Fmenu_bar_open, Smenu_bar_open, 0, 1, "i",
1384 doc: /* Start key navigation of the menu bar in FRAME.
1385 This initially opens the first menu bar item and you can then navigate with the
1386 arrow keys, select a menu entry with the return key or cancel with the
1387 escape key. If FRAME has no menu bar this function does nothing.
1389 If FRAME is nil or not given, use the selected frame. */)
1390 (frame)
1391 Lisp_Object frame;
1393 GtkWidget *menubar;
1394 BLOCK_INPUT;
1395 FRAME_PTR f = check_x_frame (frame);
1397 if (FRAME_EXTERNAL_MENU_BAR (f))
1398 set_frame_menubar (f, 0, 1);
1400 menubar = FRAME_X_OUTPUT (f)->menubar_widget;
1401 if (menubar)
1403 /* Activate the first menu. */
1404 GList *children = gtk_container_get_children (GTK_CONTAINER (menubar));
1406 gtk_menu_shell_select_item (GTK_MENU_SHELL (menubar),
1407 GTK_WIDGET (children->data));
1409 popup_activated_flag = 1;
1410 g_list_free (children);
1412 UNBLOCK_INPUT;
1414 return Qnil;
1417 /* Loop util popup_activated_flag is set to zero in a callback.
1418 Used for popup menus and dialogs. */
1420 static void
1421 popup_widget_loop (do_timers, widget)
1422 int do_timers;
1423 GtkWidget *widget;
1425 ++popup_activated_flag;
1427 /* Process events in the Gtk event loop until done. */
1428 while (popup_activated_flag)
1430 if (do_timers) x_menu_wait_for_event (0);
1431 gtk_main_iteration ();
1434 #endif
1436 /* Activate the menu bar of frame F.
1437 This is called from keyboard.c when it gets the
1438 MENU_BAR_ACTIVATE_EVENT out of the Emacs event queue.
1440 To activate the menu bar, we use the X button-press event
1441 that was saved in saved_menu_event.
1442 That makes the toolkit do its thing.
1444 But first we recompute the menu bar contents (the whole tree).
1446 The reason for saving the button event until here, instead of
1447 passing it to the toolkit right away, is that we can safely
1448 execute Lisp code. */
1450 void
1451 x_activate_menubar (f)
1452 FRAME_PTR f;
1454 if (!f->output_data.x->saved_menu_event->type)
1455 return;
1457 #ifdef USE_GTK
1458 if (! xg_win_to_widget (FRAME_X_DISPLAY (f),
1459 f->output_data.x->saved_menu_event->xany.window))
1460 return;
1461 #endif
1463 set_frame_menubar (f, 0, 1);
1464 BLOCK_INPUT;
1465 #ifdef USE_GTK
1466 XPutBackEvent (f->output_data.x->display_info->display,
1467 f->output_data.x->saved_menu_event);
1468 popup_activated_flag = 1;
1469 #else
1470 XtDispatchEvent (f->output_data.x->saved_menu_event);
1471 #endif
1472 UNBLOCK_INPUT;
1473 #ifdef USE_MOTIF
1474 if (f->output_data.x->saved_menu_event->type == ButtonRelease)
1475 pending_menu_activation = 1;
1476 #endif
1478 /* Ignore this if we get it a second time. */
1479 f->output_data.x->saved_menu_event->type = 0;
1482 /* Detect if a dialog or menu has been posted. */
1485 popup_activated ()
1487 return popup_activated_flag;
1490 /* This callback is invoked when the user selects a menubar cascade
1491 pushbutton, but before the pulldown menu is posted. */
1493 #ifndef USE_GTK
1494 static void
1495 popup_activate_callback (widget, id, client_data)
1496 Widget widget;
1497 LWLIB_ID id;
1498 XtPointer client_data;
1500 popup_activated_flag = 1;
1502 #endif
1504 /* This callback is invoked when a dialog or menu is finished being
1505 used and has been unposted. */
1507 #ifdef USE_GTK
1508 static void
1509 popup_deactivate_callback (widget, client_data)
1510 GtkWidget *widget;
1511 gpointer client_data;
1513 popup_activated_flag = 0;
1515 #else
1516 static void
1517 popup_deactivate_callback (widget, id, client_data)
1518 Widget widget;
1519 LWLIB_ID id;
1520 XtPointer client_data;
1522 popup_activated_flag = 0;
1524 #endif
1527 /* Function that finds the frame for WIDGET and shows the HELP text
1528 for that widget.
1529 F is the frame if known, or NULL if not known. */
1530 static void
1531 show_help_event (f, widget, help)
1532 FRAME_PTR f;
1533 xt_or_gtk_widget widget;
1534 Lisp_Object help;
1536 Lisp_Object frame;
1538 if (f)
1540 XSETFRAME (frame, f);
1541 kbd_buffer_store_help_event (frame, help);
1543 else
1545 #if 0 /* This code doesn't do anything useful. ++kfs */
1546 /* WIDGET is the popup menu. It's parent is the frame's
1547 widget. See which frame that is. */
1548 xt_or_gtk_widget frame_widget = XtParent (widget);
1549 Lisp_Object tail;
1551 for (tail = Vframe_list; GC_CONSP (tail); tail = XCDR (tail))
1553 frame = XCAR (tail);
1554 if (GC_FRAMEP (frame)
1555 && (f = XFRAME (frame),
1556 FRAME_X_P (f) && f->output_data.x->widget == frame_widget))
1557 break;
1559 #endif
1560 show_help_echo (help, Qnil, Qnil, Qnil, 1);
1564 /* Callback called when menu items are highlighted/unhighlighted
1565 while moving the mouse over them. WIDGET is the menu bar or menu
1566 popup widget. ID is its LWLIB_ID. CALL_DATA contains a pointer to
1567 the data structure for the menu item, or null in case of
1568 unhighlighting. */
1570 #ifdef USE_GTK
1571 void
1572 menu_highlight_callback (widget, call_data)
1573 GtkWidget *widget;
1574 gpointer call_data;
1576 xg_menu_item_cb_data *cb_data;
1577 Lisp_Object help;
1579 cb_data = (xg_menu_item_cb_data*) g_object_get_data (G_OBJECT (widget),
1580 XG_ITEM_DATA);
1581 if (! cb_data) return;
1583 help = call_data ? cb_data->help : Qnil;
1585 /* If popup_activated_flag is greater than 1 we are in a popup menu.
1586 Don't show help for them, they won't appear before the
1587 popup is popped down. */
1588 if (popup_activated_flag <= 1)
1589 show_help_event (cb_data->cl_data->f, widget, help);
1591 #else
1592 void
1593 menu_highlight_callback (widget, id, call_data)
1594 Widget widget;
1595 LWLIB_ID id;
1596 void *call_data;
1598 struct frame *f;
1599 Lisp_Object help;
1601 widget_value *wv = (widget_value *) call_data;
1603 help = wv ? wv->help : Qnil;
1605 /* Determine the frame for the help event. */
1606 f = menubar_id_to_frame (id);
1608 show_help_event (f, widget, help);
1610 #endif
1612 /* Find the menu selection and store it in the keyboard buffer.
1613 F is the frame the menu is on.
1614 MENU_BAR_ITEMS_USED is the length of VECTOR.
1615 VECTOR is an array of menu events for the whole menu. */
1617 static void
1618 find_and_call_menu_selection (f, menu_bar_items_used, vector, client_data)
1619 FRAME_PTR f;
1620 int menu_bar_items_used;
1621 Lisp_Object vector;
1622 void *client_data;
1624 Lisp_Object prefix, entry;
1625 Lisp_Object *subprefix_stack;
1626 int submenu_depth = 0;
1627 int i;
1629 entry = Qnil;
1630 subprefix_stack = (Lisp_Object *) alloca (menu_bar_items_used * sizeof (Lisp_Object));
1631 prefix = Qnil;
1632 i = 0;
1634 while (i < menu_bar_items_used)
1636 if (EQ (XVECTOR (vector)->contents[i], Qnil))
1638 subprefix_stack[submenu_depth++] = prefix;
1639 prefix = entry;
1640 i++;
1642 else if (EQ (XVECTOR (vector)->contents[i], Qlambda))
1644 prefix = subprefix_stack[--submenu_depth];
1645 i++;
1647 else if (EQ (XVECTOR (vector)->contents[i], Qt))
1649 prefix = XVECTOR (vector)->contents[i + MENU_ITEMS_PANE_PREFIX];
1650 i += MENU_ITEMS_PANE_LENGTH;
1652 else
1654 entry = XVECTOR (vector)->contents[i + MENU_ITEMS_ITEM_VALUE];
1655 /* The EMACS_INT cast avoids a warning. There's no problem
1656 as long as pointers have enough bits to hold small integers. */
1657 if ((int) (EMACS_INT) client_data == i)
1659 int j;
1660 struct input_event buf;
1661 Lisp_Object frame;
1662 EVENT_INIT (buf);
1664 XSETFRAME (frame, f);
1665 buf.kind = MENU_BAR_EVENT;
1666 buf.frame_or_window = frame;
1667 buf.arg = frame;
1668 kbd_buffer_store_event (&buf);
1670 for (j = 0; j < submenu_depth; j++)
1671 if (!NILP (subprefix_stack[j]))
1673 buf.kind = MENU_BAR_EVENT;
1674 buf.frame_or_window = frame;
1675 buf.arg = subprefix_stack[j];
1676 kbd_buffer_store_event (&buf);
1679 if (!NILP (prefix))
1681 buf.kind = MENU_BAR_EVENT;
1682 buf.frame_or_window = frame;
1683 buf.arg = prefix;
1684 kbd_buffer_store_event (&buf);
1687 buf.kind = MENU_BAR_EVENT;
1688 buf.frame_or_window = frame;
1689 buf.arg = entry;
1690 kbd_buffer_store_event (&buf);
1692 return;
1694 i += MENU_ITEMS_ITEM_LENGTH;
1700 #ifdef USE_GTK
1701 /* Gtk calls callbacks just because we tell it what item should be
1702 selected in a radio group. If this variable is set to a non-zero
1703 value, we are creating menus and don't want callbacks right now.
1705 static int xg_crazy_callback_abort;
1707 /* This callback is called from the menu bar pulldown menu
1708 when the user makes a selection.
1709 Figure out what the user chose
1710 and put the appropriate events into the keyboard buffer. */
1711 static void
1712 menubar_selection_callback (widget, client_data)
1713 GtkWidget *widget;
1714 gpointer client_data;
1716 xg_menu_item_cb_data *cb_data = (xg_menu_item_cb_data*) client_data;
1718 if (xg_crazy_callback_abort)
1719 return;
1721 if (! cb_data || ! cb_data->cl_data || ! cb_data->cl_data->f)
1722 return;
1724 /* For a group of radio buttons, GTK calls the selection callback first
1725 for the item that was active before the selection and then for the one that
1726 is active after the selection. For C-h k this means we get the help on
1727 the deselected item and then the selected item is executed. Prevent that
1728 by ignoring the non-active item. */
1729 if (GTK_IS_RADIO_MENU_ITEM (widget)
1730 && ! gtk_check_menu_item_get_active (GTK_CHECK_MENU_ITEM (widget)))
1731 return;
1733 /* When a menu is popped down, X generates a focus event (i.e. focus
1734 goes back to the frame below the menu). Since GTK buffers events,
1735 we force it out here before the menu selection event. Otherwise
1736 sit-for will exit at once if the focus event follows the menu selection
1737 event. */
1739 BLOCK_INPUT;
1740 while (gtk_events_pending ())
1741 gtk_main_iteration ();
1742 UNBLOCK_INPUT;
1744 find_and_call_menu_selection (cb_data->cl_data->f,
1745 cb_data->cl_data->menu_bar_items_used,
1746 cb_data->cl_data->menu_bar_vector,
1747 cb_data->call_data);
1750 #else /* not USE_GTK */
1752 /* This callback is called from the menu bar pulldown menu
1753 when the user makes a selection.
1754 Figure out what the user chose
1755 and put the appropriate events into the keyboard buffer. */
1756 static void
1757 menubar_selection_callback (widget, id, client_data)
1758 Widget widget;
1759 LWLIB_ID id;
1760 XtPointer client_data;
1762 FRAME_PTR f;
1764 f = menubar_id_to_frame (id);
1765 if (!f)
1766 return;
1767 find_and_call_menu_selection (f, f->menu_bar_items_used,
1768 f->menu_bar_vector, client_data);
1770 #endif /* not USE_GTK */
1772 /* Allocate a widget_value, blocking input. */
1774 widget_value *
1775 xmalloc_widget_value ()
1777 widget_value *value;
1779 BLOCK_INPUT;
1780 value = malloc_widget_value ();
1781 UNBLOCK_INPUT;
1783 return value;
1786 /* This recursively calls free_widget_value on the tree of widgets.
1787 It must free all data that was malloc'ed for these widget_values.
1788 In Emacs, many slots are pointers into the data of Lisp_Strings, and
1789 must be left alone. */
1791 void
1792 free_menubar_widget_value_tree (wv)
1793 widget_value *wv;
1795 if (! wv) return;
1797 wv->name = wv->value = wv->key = (char *) 0xDEADBEEF;
1799 if (wv->contents && (wv->contents != (widget_value*)1))
1801 free_menubar_widget_value_tree (wv->contents);
1802 wv->contents = (widget_value *) 0xDEADBEEF;
1804 if (wv->next)
1806 free_menubar_widget_value_tree (wv->next);
1807 wv->next = (widget_value *) 0xDEADBEEF;
1809 BLOCK_INPUT;
1810 free_widget_value (wv);
1811 UNBLOCK_INPUT;
1814 /* Set up data in menu_items for a menu bar item
1815 whose event type is ITEM_KEY (with string ITEM_NAME)
1816 and whose contents come from the list of keymaps MAPS. */
1818 static int
1819 parse_single_submenu (item_key, item_name, maps)
1820 Lisp_Object item_key, item_name, maps;
1822 Lisp_Object length;
1823 int len;
1824 Lisp_Object *mapvec;
1825 int i;
1826 int top_level_items = 0;
1828 length = Flength (maps);
1829 len = XINT (length);
1831 /* Convert the list MAPS into a vector MAPVEC. */
1832 mapvec = (Lisp_Object *) alloca (len * sizeof (Lisp_Object));
1833 for (i = 0; i < len; i++)
1835 mapvec[i] = Fcar (maps);
1836 maps = Fcdr (maps);
1839 /* Loop over the given keymaps, making a pane for each map.
1840 But don't make a pane that is empty--ignore that map instead. */
1841 for (i = 0; i < len; i++)
1843 if (!KEYMAPP (mapvec[i]))
1845 /* Here we have a command at top level in the menu bar
1846 as opposed to a submenu. */
1847 top_level_items = 1;
1848 push_menu_pane (Qnil, Qnil);
1849 push_menu_item (item_name, Qt, item_key, mapvec[i],
1850 Qnil, Qnil, Qnil, Qnil);
1852 else
1854 Lisp_Object prompt;
1855 prompt = Fkeymap_prompt (mapvec[i]);
1856 single_keymap_panes (mapvec[i],
1857 !NILP (prompt) ? prompt : item_name,
1858 item_key, 0, 10);
1862 return top_level_items;
1865 /* Create a tree of widget_value objects
1866 representing the panes and items
1867 in menu_items starting at index START, up to index END. */
1869 static widget_value *
1870 digest_single_submenu (start, end, top_level_items)
1871 int start, end, top_level_items;
1873 widget_value *wv, *prev_wv, *save_wv, *first_wv;
1874 int i;
1875 int submenu_depth = 0;
1876 widget_value **submenu_stack;
1877 int panes_seen = 0;
1879 submenu_stack
1880 = (widget_value **) alloca (menu_items_used * sizeof (widget_value *));
1881 wv = xmalloc_widget_value ();
1882 wv->name = "menu";
1883 wv->value = 0;
1884 wv->enabled = 1;
1885 wv->button_type = BUTTON_TYPE_NONE;
1886 wv->help = Qnil;
1887 first_wv = wv;
1888 save_wv = 0;
1889 prev_wv = 0;
1891 /* Loop over all panes and items made by the preceding call
1892 to parse_single_submenu and construct a tree of widget_value objects.
1893 Ignore the panes and items used by previous calls to
1894 digest_single_submenu, even though those are also in menu_items. */
1895 i = start;
1896 while (i < end)
1898 if (EQ (XVECTOR (menu_items)->contents[i], Qnil))
1900 submenu_stack[submenu_depth++] = save_wv;
1901 save_wv = prev_wv;
1902 prev_wv = 0;
1903 i++;
1905 else if (EQ (XVECTOR (menu_items)->contents[i], Qlambda))
1907 prev_wv = save_wv;
1908 save_wv = submenu_stack[--submenu_depth];
1909 i++;
1911 else if (EQ (XVECTOR (menu_items)->contents[i], Qt)
1912 && submenu_depth != 0)
1913 i += MENU_ITEMS_PANE_LENGTH;
1914 /* Ignore a nil in the item list.
1915 It's meaningful only for dialog boxes. */
1916 else if (EQ (XVECTOR (menu_items)->contents[i], Qquote))
1917 i += 1;
1918 else if (EQ (XVECTOR (menu_items)->contents[i], Qt))
1920 /* Create a new pane. */
1921 Lisp_Object pane_name, prefix;
1922 char *pane_string;
1924 panes_seen++;
1926 pane_name = XVECTOR (menu_items)->contents[i + MENU_ITEMS_PANE_NAME];
1927 prefix = XVECTOR (menu_items)->contents[i + MENU_ITEMS_PANE_PREFIX];
1929 #ifndef HAVE_MULTILINGUAL_MENU
1930 if (STRINGP (pane_name) && STRING_MULTIBYTE (pane_name))
1932 pane_name = ENCODE_MENU_STRING (pane_name);
1933 AREF (menu_items, i + MENU_ITEMS_PANE_NAME) = pane_name;
1935 #endif
1936 pane_string = (NILP (pane_name)
1937 ? "" : (char *) SDATA (pane_name));
1938 /* If there is just one top-level pane, put all its items directly
1939 under the top-level menu. */
1940 if (menu_items_n_panes == 1)
1941 pane_string = "";
1943 /* If the pane has a meaningful name,
1944 make the pane a top-level menu item
1945 with its items as a submenu beneath it. */
1946 if (strcmp (pane_string, ""))
1948 wv = xmalloc_widget_value ();
1949 if (save_wv)
1950 save_wv->next = wv;
1951 else
1952 first_wv->contents = wv;
1953 wv->lname = pane_name;
1954 /* Set value to 1 so update_submenu_strings can handle '@' */
1955 wv->value = (char *)1;
1956 wv->enabled = 1;
1957 wv->button_type = BUTTON_TYPE_NONE;
1958 wv->help = Qnil;
1959 save_wv = wv;
1961 else
1962 save_wv = first_wv;
1964 prev_wv = 0;
1965 i += MENU_ITEMS_PANE_LENGTH;
1967 else
1969 /* Create a new item within current pane. */
1970 Lisp_Object item_name, enable, descrip, def, type, selected;
1971 Lisp_Object help;
1973 /* All items should be contained in panes. */
1974 if (panes_seen == 0)
1975 abort ();
1977 item_name = AREF (menu_items, i + MENU_ITEMS_ITEM_NAME);
1978 enable = AREF (menu_items, i + MENU_ITEMS_ITEM_ENABLE);
1979 descrip = AREF (menu_items, i + MENU_ITEMS_ITEM_EQUIV_KEY);
1980 def = AREF (menu_items, i + MENU_ITEMS_ITEM_DEFINITION);
1981 type = AREF (menu_items, i + MENU_ITEMS_ITEM_TYPE);
1982 selected = AREF (menu_items, i + MENU_ITEMS_ITEM_SELECTED);
1983 help = AREF (menu_items, i + MENU_ITEMS_ITEM_HELP);
1985 #ifndef HAVE_MULTILINGUAL_MENU
1986 if (STRING_MULTIBYTE (item_name))
1988 item_name = ENCODE_MENU_STRING (item_name);
1989 AREF (menu_items, i + MENU_ITEMS_ITEM_NAME) = item_name;
1992 if (STRINGP (descrip) && STRING_MULTIBYTE (descrip))
1994 descrip = ENCODE_MENU_STRING (descrip);
1995 AREF (menu_items, i + MENU_ITEMS_ITEM_EQUIV_KEY) = descrip;
1997 #endif /* not HAVE_MULTILINGUAL_MENU */
1999 wv = xmalloc_widget_value ();
2000 if (prev_wv)
2001 prev_wv->next = wv;
2002 else
2003 save_wv->contents = wv;
2005 wv->lname = item_name;
2006 if (!NILP (descrip))
2007 wv->lkey = descrip;
2008 wv->value = 0;
2009 /* The EMACS_INT cast avoids a warning. There's no problem
2010 as long as pointers have enough bits to hold small integers. */
2011 wv->call_data = (!NILP (def) ? (void *) (EMACS_INT) i : 0);
2012 wv->enabled = !NILP (enable);
2014 if (NILP (type))
2015 wv->button_type = BUTTON_TYPE_NONE;
2016 else if (EQ (type, QCradio))
2017 wv->button_type = BUTTON_TYPE_RADIO;
2018 else if (EQ (type, QCtoggle))
2019 wv->button_type = BUTTON_TYPE_TOGGLE;
2020 else
2021 abort ();
2023 wv->selected = !NILP (selected);
2024 if (! STRINGP (help))
2025 help = Qnil;
2027 wv->help = help;
2029 prev_wv = wv;
2031 i += MENU_ITEMS_ITEM_LENGTH;
2035 /* If we have just one "menu item"
2036 that was originally a button, return it by itself. */
2037 if (top_level_items && first_wv->contents && first_wv->contents->next == 0)
2039 wv = first_wv->contents;
2040 free_widget_value (first_wv);
2041 return wv;
2044 return first_wv;
2047 /* Walk through the widget_value tree starting at FIRST_WV and update
2048 the char * pointers from the corresponding lisp values.
2049 We do this after building the whole tree, since GC may happen while the
2050 tree is constructed, and small strings are relocated. So we must wait
2051 until no GC can happen before storing pointers into lisp values. */
2052 static void
2053 update_submenu_strings (first_wv)
2054 widget_value *first_wv;
2056 widget_value *wv;
2058 for (wv = first_wv; wv; wv = wv->next)
2060 if (STRINGP (wv->lname))
2062 wv->name = (char *) SDATA (wv->lname);
2064 /* Ignore the @ that means "separate pane".
2065 This is a kludge, but this isn't worth more time. */
2066 if (wv->value == (char *)1)
2068 if (wv->name[0] == '@')
2069 wv->name++;
2070 wv->value = 0;
2074 if (STRINGP (wv->lkey))
2075 wv->key = (char *) SDATA (wv->lkey);
2077 if (wv->contents)
2078 update_submenu_strings (wv->contents);
2083 /* Recompute all the widgets of frame F, when the menu bar has been
2084 changed. Value is non-zero if widgets were updated. */
2086 static int
2087 update_frame_menubar (f)
2088 FRAME_PTR f;
2090 #ifdef USE_GTK
2091 return xg_update_frame_menubar (f);
2092 #else
2093 struct x_output *x = f->output_data.x;
2094 int columns, rows;
2096 if (!x->menubar_widget || XtIsManaged (x->menubar_widget))
2097 return 0;
2099 BLOCK_INPUT;
2100 /* Save the size of the frame because the pane widget doesn't accept
2101 to resize itself. So force it. */
2102 columns = FRAME_COLS (f);
2103 rows = FRAME_LINES (f);
2105 /* Do the voodoo which means "I'm changing lots of things, don't try
2106 to refigure sizes until I'm done." */
2107 lw_refigure_widget (x->column_widget, False);
2109 /* The order in which children are managed is the top to bottom
2110 order in which they are displayed in the paned window. First,
2111 remove the text-area widget. */
2112 XtUnmanageChild (x->edit_widget);
2114 /* Remove the menubar that is there now, and put up the menubar that
2115 should be there. */
2116 XtManageChild (x->menubar_widget);
2117 XtMapWidget (x->menubar_widget);
2118 XtVaSetValues (x->menubar_widget, XtNmappedWhenManaged, 1, NULL);
2120 /* Re-manage the text-area widget, and then thrash the sizes. */
2121 XtManageChild (x->edit_widget);
2122 lw_refigure_widget (x->column_widget, True);
2124 /* Force the pane widget to resize itself with the right values. */
2125 EmacsFrameSetCharSize (x->edit_widget, columns, rows);
2126 UNBLOCK_INPUT;
2127 #endif
2128 return 1;
2131 /* Set the contents of the menubar widgets of frame F.
2132 The argument FIRST_TIME is currently ignored;
2133 it is set the first time this is called, from initialize_frame_menubar. */
2135 void
2136 set_frame_menubar (f, first_time, deep_p)
2137 FRAME_PTR f;
2138 int first_time;
2139 int deep_p;
2141 xt_or_gtk_widget menubar_widget = f->output_data.x->menubar_widget;
2142 #ifdef USE_X_TOOLKIT
2143 LWLIB_ID id;
2144 #endif
2145 Lisp_Object items;
2146 widget_value *wv, *first_wv, *prev_wv = 0;
2147 int i, last_i = 0;
2148 int *submenu_start, *submenu_end;
2149 int *submenu_top_level_items, *submenu_n_panes;
2152 XSETFRAME (Vmenu_updating_frame, f);
2154 #ifdef USE_X_TOOLKIT
2155 if (f->output_data.x->id == 0)
2156 f->output_data.x->id = next_menubar_widget_id++;
2157 id = f->output_data.x->id;
2158 #endif
2160 if (! menubar_widget)
2161 deep_p = 1;
2162 else if (pending_menu_activation && !deep_p)
2163 deep_p = 1;
2164 /* Make the first call for any given frame always go deep. */
2165 else if (!f->output_data.x->saved_menu_event && !deep_p)
2167 deep_p = 1;
2168 f->output_data.x->saved_menu_event = (XEvent*)xmalloc (sizeof (XEvent));
2169 f->output_data.x->saved_menu_event->type = 0;
2172 #ifdef USE_GTK
2173 /* If we have detached menus, we must update deep so detached menus
2174 also gets updated. */
2175 deep_p = deep_p || xg_have_tear_offs ();
2176 #endif
2178 if (deep_p)
2180 /* Make a widget-value tree representing the entire menu trees. */
2182 struct buffer *prev = current_buffer;
2183 Lisp_Object buffer;
2184 int specpdl_count = SPECPDL_INDEX ();
2185 int previous_menu_items_used = f->menu_bar_items_used;
2186 Lisp_Object *previous_items
2187 = (Lisp_Object *) alloca (previous_menu_items_used
2188 * sizeof (Lisp_Object));
2190 /* If we are making a new widget, its contents are empty,
2191 do always reinitialize them. */
2192 if (! menubar_widget)
2193 previous_menu_items_used = 0;
2195 buffer = XWINDOW (FRAME_SELECTED_WINDOW (f))->buffer;
2196 specbind (Qinhibit_quit, Qt);
2197 /* Don't let the debugger step into this code
2198 because it is not reentrant. */
2199 specbind (Qdebug_on_next_call, Qnil);
2201 record_unwind_save_match_data ();
2202 if (NILP (Voverriding_local_map_menu_flag))
2204 specbind (Qoverriding_terminal_local_map, Qnil);
2205 specbind (Qoverriding_local_map, Qnil);
2208 set_buffer_internal_1 (XBUFFER (buffer));
2210 /* Run the Lucid hook. */
2211 safe_run_hooks (Qactivate_menubar_hook);
2213 /* If it has changed current-menubar from previous value,
2214 really recompute the menubar from the value. */
2215 if (! NILP (Vlucid_menu_bar_dirty_flag))
2216 call0 (Qrecompute_lucid_menubar);
2217 safe_run_hooks (Qmenu_bar_update_hook);
2218 FRAME_MENU_BAR_ITEMS (f) = menu_bar_items (FRAME_MENU_BAR_ITEMS (f));
2220 items = FRAME_MENU_BAR_ITEMS (f);
2222 /* Save the frame's previous menu bar contents data. */
2223 if (previous_menu_items_used)
2224 bcopy (XVECTOR (f->menu_bar_vector)->contents, previous_items,
2225 previous_menu_items_used * sizeof (Lisp_Object));
2227 /* Fill in menu_items with the current menu bar contents.
2228 This can evaluate Lisp code. */
2229 save_menu_items ();
2231 menu_items = f->menu_bar_vector;
2232 menu_items_allocated = VECTORP (menu_items) ? ASIZE (menu_items) : 0;
2233 submenu_start = (int *) alloca (XVECTOR (items)->size * sizeof (int *));
2234 submenu_end = (int *) alloca (XVECTOR (items)->size * sizeof (int *));
2235 submenu_n_panes = (int *) alloca (XVECTOR (items)->size * sizeof (int));
2236 submenu_top_level_items
2237 = (int *) alloca (XVECTOR (items)->size * sizeof (int *));
2238 init_menu_items ();
2239 for (i = 0; i < XVECTOR (items)->size; i += 4)
2241 Lisp_Object key, string, maps;
2243 last_i = i;
2245 key = XVECTOR (items)->contents[i];
2246 string = XVECTOR (items)->contents[i + 1];
2247 maps = XVECTOR (items)->contents[i + 2];
2248 if (NILP (string))
2249 break;
2251 submenu_start[i] = menu_items_used;
2253 menu_items_n_panes = 0;
2254 submenu_top_level_items[i]
2255 = parse_single_submenu (key, string, maps);
2256 submenu_n_panes[i] = menu_items_n_panes;
2258 submenu_end[i] = menu_items_used;
2261 finish_menu_items ();
2263 /* Convert menu_items into widget_value trees
2264 to display the menu. This cannot evaluate Lisp code. */
2266 wv = xmalloc_widget_value ();
2267 wv->name = "menubar";
2268 wv->value = 0;
2269 wv->enabled = 1;
2270 wv->button_type = BUTTON_TYPE_NONE;
2271 wv->help = Qnil;
2272 first_wv = wv;
2274 for (i = 0; i < last_i; i += 4)
2276 menu_items_n_panes = submenu_n_panes[i];
2277 wv = digest_single_submenu (submenu_start[i], submenu_end[i],
2278 submenu_top_level_items[i]);
2279 if (prev_wv)
2280 prev_wv->next = wv;
2281 else
2282 first_wv->contents = wv;
2283 /* Don't set wv->name here; GC during the loop might relocate it. */
2284 wv->enabled = 1;
2285 wv->button_type = BUTTON_TYPE_NONE;
2286 prev_wv = wv;
2289 set_buffer_internal_1 (prev);
2291 /* If there has been no change in the Lisp-level contents
2292 of the menu bar, skip redisplaying it. Just exit. */
2294 /* Compare the new menu items with the ones computed last time. */
2295 for (i = 0; i < previous_menu_items_used; i++)
2296 if (menu_items_used == i
2297 || (!EQ (previous_items[i], XVECTOR (menu_items)->contents[i])))
2298 break;
2299 if (i == menu_items_used && i == previous_menu_items_used && i != 0)
2301 /* The menu items have not changed. Don't bother updating
2302 the menus in any form, since it would be a no-op. */
2303 free_menubar_widget_value_tree (first_wv);
2304 discard_menu_items ();
2305 unbind_to (specpdl_count, Qnil);
2306 return;
2309 /* The menu items are different, so store them in the frame. */
2310 f->menu_bar_vector = menu_items;
2311 f->menu_bar_items_used = menu_items_used;
2313 /* This calls restore_menu_items to restore menu_items, etc.,
2314 as they were outside. */
2315 unbind_to (specpdl_count, Qnil);
2317 /* Now GC cannot happen during the lifetime of the widget_value,
2318 so it's safe to store data from a Lisp_String. */
2319 wv = first_wv->contents;
2320 for (i = 0; i < XVECTOR (items)->size; i += 4)
2322 Lisp_Object string;
2323 string = XVECTOR (items)->contents[i + 1];
2324 if (NILP (string))
2325 break;
2326 wv->name = (char *) SDATA (string);
2327 update_submenu_strings (wv->contents);
2328 wv = wv->next;
2332 else
2334 /* Make a widget-value tree containing
2335 just the top level menu bar strings. */
2337 wv = xmalloc_widget_value ();
2338 wv->name = "menubar";
2339 wv->value = 0;
2340 wv->enabled = 1;
2341 wv->button_type = BUTTON_TYPE_NONE;
2342 wv->help = Qnil;
2343 first_wv = wv;
2345 items = FRAME_MENU_BAR_ITEMS (f);
2346 for (i = 0; i < XVECTOR (items)->size; i += 4)
2348 Lisp_Object string;
2350 string = XVECTOR (items)->contents[i + 1];
2351 if (NILP (string))
2352 break;
2354 wv = xmalloc_widget_value ();
2355 wv->name = (char *) SDATA (string);
2356 wv->value = 0;
2357 wv->enabled = 1;
2358 wv->button_type = BUTTON_TYPE_NONE;
2359 wv->help = Qnil;
2360 /* This prevents lwlib from assuming this
2361 menu item is really supposed to be empty. */
2362 /* The EMACS_INT cast avoids a warning.
2363 This value just has to be different from small integers. */
2364 wv->call_data = (void *) (EMACS_INT) (-1);
2366 if (prev_wv)
2367 prev_wv->next = wv;
2368 else
2369 first_wv->contents = wv;
2370 prev_wv = wv;
2373 /* Forget what we thought we knew about what is in the
2374 detailed contents of the menu bar menus.
2375 Changing the top level always destroys the contents. */
2376 f->menu_bar_items_used = 0;
2379 /* Create or update the menu bar widget. */
2381 BLOCK_INPUT;
2383 #ifdef USE_GTK
2384 xg_crazy_callback_abort = 1;
2385 if (menubar_widget)
2387 /* The fourth arg is DEEP_P, which says to consider the entire
2388 menu trees we supply, rather than just the menu bar item names. */
2389 xg_modify_menubar_widgets (menubar_widget,
2391 first_wv,
2392 deep_p,
2393 G_CALLBACK (menubar_selection_callback),
2394 G_CALLBACK (popup_deactivate_callback),
2395 G_CALLBACK (menu_highlight_callback));
2397 else
2399 GtkWidget *wvbox = f->output_data.x->vbox_widget;
2401 menubar_widget
2402 = xg_create_widget ("menubar", "menubar", f, first_wv,
2403 G_CALLBACK (menubar_selection_callback),
2404 G_CALLBACK (popup_deactivate_callback),
2405 G_CALLBACK (menu_highlight_callback));
2407 f->output_data.x->menubar_widget = menubar_widget;
2411 #else /* not USE_GTK */
2412 if (menubar_widget)
2414 /* Disable resizing (done for Motif!) */
2415 lw_allow_resizing (f->output_data.x->widget, False);
2417 /* The third arg is DEEP_P, which says to consider the entire
2418 menu trees we supply, rather than just the menu bar item names. */
2419 lw_modify_all_widgets (id, first_wv, deep_p);
2421 /* Re-enable the edit widget to resize. */
2422 lw_allow_resizing (f->output_data.x->widget, True);
2424 else
2426 char menuOverride[] = "Ctrl<KeyPress>g: MenuGadgetEscape()";
2427 XtTranslations override = XtParseTranslationTable (menuOverride);
2429 menubar_widget = lw_create_widget ("menubar", "menubar", id, first_wv,
2430 f->output_data.x->column_widget,
2432 popup_activate_callback,
2433 menubar_selection_callback,
2434 popup_deactivate_callback,
2435 menu_highlight_callback);
2436 f->output_data.x->menubar_widget = menubar_widget;
2438 /* Make menu pop down on C-g. */
2439 XtOverrideTranslations (menubar_widget, override);
2443 int menubar_size
2444 = (f->output_data.x->menubar_widget
2445 ? (f->output_data.x->menubar_widget->core.height
2446 + f->output_data.x->menubar_widget->core.border_width)
2447 : 0);
2449 #if 0 /* Experimentally, we now get the right results
2450 for -geometry -0-0 without this. 24 Aug 96, rms. */
2451 #ifdef USE_LUCID
2452 if (FRAME_EXTERNAL_MENU_BAR (f))
2454 Dimension ibw = 0;
2455 XtVaGetValues (f->output_data.x->column_widget,
2456 XtNinternalBorderWidth, &ibw, NULL);
2457 menubar_size += ibw;
2459 #endif /* USE_LUCID */
2460 #endif /* 0 */
2462 f->output_data.x->menubar_height = menubar_size;
2464 #endif /* not USE_GTK */
2466 free_menubar_widget_value_tree (first_wv);
2467 update_frame_menubar (f);
2469 #ifdef USE_GTK
2470 xg_crazy_callback_abort = 0;
2471 #endif
2473 UNBLOCK_INPUT;
2476 /* Called from Fx_create_frame to create the initial menubar of a frame
2477 before it is mapped, so that the window is mapped with the menubar already
2478 there instead of us tacking it on later and thrashing the window after it
2479 is visible. */
2481 void
2482 initialize_frame_menubar (f)
2483 FRAME_PTR f;
2485 /* This function is called before the first chance to redisplay
2486 the frame. It has to be, so the frame will have the right size. */
2487 FRAME_MENU_BAR_ITEMS (f) = menu_bar_items (FRAME_MENU_BAR_ITEMS (f));
2488 set_frame_menubar (f, 1, 1);
2492 /* Get rid of the menu bar of frame F, and free its storage.
2493 This is used when deleting a frame, and when turning off the menu bar.
2494 For GTK this function is in gtkutil.c. */
2496 #ifndef USE_GTK
2497 void
2498 free_frame_menubar (f)
2499 FRAME_PTR f;
2501 Widget menubar_widget;
2503 menubar_widget = f->output_data.x->menubar_widget;
2505 f->output_data.x->menubar_height = 0;
2507 if (menubar_widget)
2509 #ifdef USE_MOTIF
2510 /* Removing the menu bar magically changes the shell widget's x
2511 and y position of (0, 0) which, when the menu bar is turned
2512 on again, leads to pull-down menuss appearing in strange
2513 positions near the upper-left corner of the display. This
2514 happens only with some window managers like twm and ctwm,
2515 but not with other like Motif's mwm or kwm, because the
2516 latter generate ConfigureNotify events when the menu bar
2517 is switched off, which fixes the shell position. */
2518 Position x0, y0, x1, y1;
2519 #endif
2521 BLOCK_INPUT;
2523 #ifdef USE_MOTIF
2524 if (f->output_data.x->widget)
2525 XtVaGetValues (f->output_data.x->widget, XtNx, &x0, XtNy, &y0, NULL);
2526 #endif
2528 lw_destroy_all_widgets ((LWLIB_ID) f->output_data.x->id);
2529 f->output_data.x->menubar_widget = NULL;
2531 #ifdef USE_MOTIF
2532 if (f->output_data.x->widget)
2534 XtVaGetValues (f->output_data.x->widget, XtNx, &x1, XtNy, &y1, NULL);
2535 if (x1 == 0 && y1 == 0)
2536 XtVaSetValues (f->output_data.x->widget, XtNx, x0, XtNy, y0, NULL);
2538 #endif
2540 UNBLOCK_INPUT;
2543 #endif /* not USE_GTK */
2545 #endif /* USE_X_TOOLKIT || USE_GTK */
2547 /* xmenu_show actually displays a menu using the panes and items in menu_items
2548 and returns the value selected from it.
2549 There are two versions of xmenu_show, one for Xt and one for Xlib.
2550 Both assume input is blocked by the caller. */
2552 /* F is the frame the menu is for.
2553 X and Y are the frame-relative specified position,
2554 relative to the inside upper left corner of the frame F.
2555 FOR_CLICK is nonzero if this menu was invoked for a mouse click.
2556 KEYMAPS is 1 if this menu was specified with keymaps;
2557 in that case, we return a list containing the chosen item's value
2558 and perhaps also the pane's prefix.
2559 TITLE is the specified menu title.
2560 ERROR is a place to store an error message string in case of failure.
2561 (We return nil on failure, but the value doesn't actually matter.) */
2563 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
2565 /* The item selected in the popup menu. */
2566 static Lisp_Object *volatile menu_item_selection;
2568 #ifdef USE_GTK
2570 /* Used when position a popup menu. See menu_position_func and
2571 create_and_show_popup_menu below. */
2572 struct next_popup_x_y
2574 FRAME_PTR f;
2575 int x;
2576 int y;
2579 /* The menu position function to use if we are not putting a popup
2580 menu where the pointer is.
2581 MENU is the menu to pop up.
2582 X and Y shall on exit contain x/y where the menu shall pop up.
2583 PUSH_IN is not documented in the GTK manual.
2584 USER_DATA is any data passed in when calling gtk_menu_popup.
2585 Here it points to a struct next_popup_x_y where the coordinates
2586 to store in *X and *Y are as well as the frame for the popup.
2588 Here only X and Y are used. */
2589 static void
2590 menu_position_func (menu, x, y, push_in, user_data)
2591 GtkMenu *menu;
2592 gint *x;
2593 gint *y;
2594 gboolean *push_in;
2595 gpointer user_data;
2597 struct next_popup_x_y* data = (struct next_popup_x_y*)user_data;
2598 GtkRequisition req;
2599 int disp_width = FRAME_X_DISPLAY_INFO (data->f)->width;
2600 int disp_height = FRAME_X_DISPLAY_INFO (data->f)->height;
2602 *x = data->x;
2603 *y = data->y;
2605 /* Check if there is room for the menu. If not, adjust x/y so that
2606 the menu is fully visible. */
2607 gtk_widget_size_request (GTK_WIDGET (menu), &req);
2608 if (data->x + req.width > disp_width)
2609 *x -= data->x + req.width - disp_width;
2610 if (data->y + req.height > disp_height)
2611 *y -= data->y + req.height - disp_height;
2614 static void
2615 popup_selection_callback (widget, client_data)
2616 GtkWidget *widget;
2617 gpointer client_data;
2619 xg_menu_item_cb_data *cb_data = (xg_menu_item_cb_data*) client_data;
2621 if (xg_crazy_callback_abort) return;
2622 if (cb_data) menu_item_selection = (Lisp_Object *) cb_data->call_data;
2625 static Lisp_Object
2626 pop_down_menu (arg)
2627 Lisp_Object arg;
2629 struct Lisp_Save_Value *p = XSAVE_VALUE (arg);
2631 popup_activated_flag = 0;
2632 BLOCK_INPUT;
2633 gtk_widget_destroy (GTK_WIDGET (p->pointer));
2634 UNBLOCK_INPUT;
2635 return Qnil;
2638 /* Pop up the menu for frame F defined by FIRST_WV at X/Y and loop until the
2639 menu pops down.
2640 menu_item_selection will be set to the selection. */
2641 static void
2642 create_and_show_popup_menu (f, first_wv, x, y, for_click)
2643 FRAME_PTR f;
2644 widget_value *first_wv;
2645 int x;
2646 int y;
2647 int for_click;
2649 int i;
2650 GtkWidget *menu;
2651 GtkMenuPositionFunc pos_func = 0; /* Pop up at pointer. */
2652 struct next_popup_x_y popup_x_y;
2653 int specpdl_count = SPECPDL_INDEX ();
2655 xg_crazy_callback_abort = 1;
2656 menu = xg_create_widget ("popup", first_wv->name, f, first_wv,
2657 G_CALLBACK (popup_selection_callback),
2658 G_CALLBACK (popup_deactivate_callback),
2659 G_CALLBACK (menu_highlight_callback));
2660 xg_crazy_callback_abort = 0;
2662 if (! for_click)
2664 /* Not invoked by a click. pop up at x/y. */
2665 pos_func = menu_position_func;
2667 /* Adjust coordinates to be root-window-relative. */
2668 x += f->left_pos + FRAME_OUTER_TO_INNER_DIFF_X (f);
2669 y += f->top_pos + FRAME_OUTER_TO_INNER_DIFF_Y (f);
2671 popup_x_y.x = x;
2672 popup_x_y.y = y;
2673 popup_x_y.f = f;
2675 i = 0; /* gtk_menu_popup needs this to be 0 for a non-button popup. */
2677 else
2679 for (i = 0; i < 5; i++)
2680 if (FRAME_X_DISPLAY_INFO (f)->grabbed & (1 << i))
2681 break;
2684 /* Display the menu. */
2685 gtk_widget_show_all (menu);
2686 gtk_menu_popup (GTK_MENU (menu), 0, 0, pos_func, &popup_x_y, i, 0);
2688 record_unwind_protect (pop_down_menu, make_save_value (menu, 0));
2690 if (GTK_WIDGET_MAPPED (menu))
2692 /* Set this to one. popup_widget_loop increases it by one, so it becomes
2693 two. show_help_echo uses this to detect popup menus. */
2694 popup_activated_flag = 1;
2695 /* Process events that apply to the menu. */
2696 popup_widget_loop (1, menu);
2699 unbind_to (specpdl_count, Qnil);
2701 /* Must reset this manually because the button release event is not passed
2702 to Emacs event loop. */
2703 FRAME_X_DISPLAY_INFO (f)->grabbed = 0;
2706 #else /* not USE_GTK */
2708 /* We need a unique id for each widget handled by the Lucid Widget
2709 library.
2711 For the main windows, and popup menus, we use this counter,
2712 which we increment each time after use. This starts from 1<<16.
2714 For menu bars, we use numbers starting at 0, counted in
2715 next_menubar_widget_id. */
2716 LWLIB_ID widget_id_tick;
2718 static void
2719 popup_selection_callback (widget, id, client_data)
2720 Widget widget;
2721 LWLIB_ID id;
2722 XtPointer client_data;
2724 menu_item_selection = (Lisp_Object *) client_data;
2727 /* ARG is the LWLIB ID of the dialog box, represented
2728 as a Lisp object as (HIGHPART . LOWPART). */
2730 static Lisp_Object
2731 pop_down_menu (arg)
2732 Lisp_Object arg;
2734 LWLIB_ID id = (XINT (XCAR (arg)) << 4 * sizeof (LWLIB_ID)
2735 | XINT (XCDR (arg)));
2737 BLOCK_INPUT;
2738 lw_destroy_all_widgets (id);
2739 UNBLOCK_INPUT;
2740 popup_activated_flag = 0;
2742 return Qnil;
2745 /* Pop up the menu for frame F defined by FIRST_WV at X/Y and loop until the
2746 menu pops down.
2747 menu_item_selection will be set to the selection. */
2748 static void
2749 create_and_show_popup_menu (f, first_wv, x, y, for_click)
2750 FRAME_PTR f;
2751 widget_value *first_wv;
2752 int x;
2753 int y;
2754 int for_click;
2756 int i;
2757 Arg av[2];
2758 int ac = 0;
2759 XButtonPressedEvent dummy;
2760 LWLIB_ID menu_id;
2761 Widget menu;
2763 menu_id = widget_id_tick++;
2764 menu = lw_create_widget ("popup", first_wv->name, menu_id, first_wv,
2765 f->output_data.x->widget, 1, 0,
2766 popup_selection_callback,
2767 popup_deactivate_callback,
2768 menu_highlight_callback);
2770 dummy.type = ButtonPress;
2771 dummy.serial = 0;
2772 dummy.send_event = 0;
2773 dummy.display = FRAME_X_DISPLAY (f);
2774 dummy.time = CurrentTime;
2775 dummy.root = FRAME_X_DISPLAY_INFO (f)->root_window;
2776 dummy.window = dummy.root;
2777 dummy.subwindow = dummy.root;
2778 dummy.x = x;
2779 dummy.y = y;
2781 /* Adjust coordinates to be root-window-relative. */
2782 x += f->left_pos + FRAME_OUTER_TO_INNER_DIFF_X (f);
2783 y += f->top_pos + FRAME_OUTER_TO_INNER_DIFF_Y (f);
2785 dummy.x_root = x;
2786 dummy.y_root = y;
2788 dummy.state = 0;
2789 dummy.button = 0;
2790 for (i = 0; i < 5; i++)
2791 if (FRAME_X_DISPLAY_INFO (f)->grabbed & (1 << i))
2792 dummy.button = i;
2794 /* Don't allow any geometry request from the user. */
2795 XtSetArg (av[ac], XtNgeometry, 0); ac++;
2796 XtSetValues (menu, av, ac);
2798 /* Display the menu. */
2799 lw_popup_menu (menu, (XEvent *) &dummy);
2800 popup_activated_flag = 1;
2803 int fact = 4 * sizeof (LWLIB_ID);
2804 int specpdl_count = SPECPDL_INDEX ();
2805 record_unwind_protect (pop_down_menu,
2806 Fcons (make_number (menu_id >> (fact)),
2807 make_number (menu_id & ~(-1 << (fact)))));
2809 /* Process events that apply to the menu. */
2810 popup_get_selection ((XEvent *) 0, FRAME_X_DISPLAY_INFO (f), menu_id, 1);
2812 unbind_to (specpdl_count, Qnil);
2816 #endif /* not USE_GTK */
2818 static Lisp_Object
2819 xmenu_show (f, x, y, for_click, keymaps, title, error)
2820 FRAME_PTR f;
2821 int x;
2822 int y;
2823 int for_click;
2824 int keymaps;
2825 Lisp_Object title;
2826 char **error;
2828 int i;
2829 widget_value *wv, *save_wv = 0, *first_wv = 0, *prev_wv = 0;
2830 widget_value **submenu_stack
2831 = (widget_value **) alloca (menu_items_used * sizeof (widget_value *));
2832 Lisp_Object *subprefix_stack
2833 = (Lisp_Object *) alloca (menu_items_used * sizeof (Lisp_Object));
2834 int submenu_depth = 0;
2836 int first_pane;
2838 *error = NULL;
2840 if (menu_items_used <= MENU_ITEMS_PANE_LENGTH)
2842 *error = "Empty menu";
2843 return Qnil;
2846 /* Create a tree of widget_value objects
2847 representing the panes and their items. */
2848 wv = xmalloc_widget_value ();
2849 wv->name = "menu";
2850 wv->value = 0;
2851 wv->enabled = 1;
2852 wv->button_type = BUTTON_TYPE_NONE;
2853 wv->help =Qnil;
2854 first_wv = wv;
2855 first_pane = 1;
2857 /* Loop over all panes and items, filling in the tree. */
2858 i = 0;
2859 while (i < menu_items_used)
2861 if (EQ (XVECTOR (menu_items)->contents[i], Qnil))
2863 submenu_stack[submenu_depth++] = save_wv;
2864 save_wv = prev_wv;
2865 prev_wv = 0;
2866 first_pane = 1;
2867 i++;
2869 else if (EQ (XVECTOR (menu_items)->contents[i], Qlambda))
2871 prev_wv = save_wv;
2872 save_wv = submenu_stack[--submenu_depth];
2873 first_pane = 0;
2874 i++;
2876 else if (EQ (XVECTOR (menu_items)->contents[i], Qt)
2877 && submenu_depth != 0)
2878 i += MENU_ITEMS_PANE_LENGTH;
2879 /* Ignore a nil in the item list.
2880 It's meaningful only for dialog boxes. */
2881 else if (EQ (XVECTOR (menu_items)->contents[i], Qquote))
2882 i += 1;
2883 else if (EQ (XVECTOR (menu_items)->contents[i], Qt))
2885 /* Create a new pane. */
2886 Lisp_Object pane_name, prefix;
2887 char *pane_string;
2889 pane_name = AREF (menu_items, i + MENU_ITEMS_PANE_NAME);
2890 prefix = AREF (menu_items, i + MENU_ITEMS_PANE_PREFIX);
2892 #ifndef HAVE_MULTILINGUAL_MENU
2893 if (STRINGP (pane_name) && STRING_MULTIBYTE (pane_name))
2895 pane_name = ENCODE_MENU_STRING (pane_name);
2896 AREF (menu_items, i + MENU_ITEMS_PANE_NAME) = pane_name;
2898 #endif
2899 pane_string = (NILP (pane_name)
2900 ? "" : (char *) SDATA (pane_name));
2901 /* If there is just one top-level pane, put all its items directly
2902 under the top-level menu. */
2903 if (menu_items_n_panes == 1)
2904 pane_string = "";
2906 /* If the pane has a meaningful name,
2907 make the pane a top-level menu item
2908 with its items as a submenu beneath it. */
2909 if (!keymaps && strcmp (pane_string, ""))
2911 wv = xmalloc_widget_value ();
2912 if (save_wv)
2913 save_wv->next = wv;
2914 else
2915 first_wv->contents = wv;
2916 wv->name = pane_string;
2917 if (keymaps && !NILP (prefix))
2918 wv->name++;
2919 wv->value = 0;
2920 wv->enabled = 1;
2921 wv->button_type = BUTTON_TYPE_NONE;
2922 wv->help = Qnil;
2923 save_wv = wv;
2924 prev_wv = 0;
2926 else if (first_pane)
2928 save_wv = wv;
2929 prev_wv = 0;
2931 first_pane = 0;
2932 i += MENU_ITEMS_PANE_LENGTH;
2934 else
2936 /* Create a new item within current pane. */
2937 Lisp_Object item_name, enable, descrip, def, type, selected, help;
2938 item_name = AREF (menu_items, i + MENU_ITEMS_ITEM_NAME);
2939 enable = AREF (menu_items, i + MENU_ITEMS_ITEM_ENABLE);
2940 descrip = AREF (menu_items, i + MENU_ITEMS_ITEM_EQUIV_KEY);
2941 def = AREF (menu_items, i + MENU_ITEMS_ITEM_DEFINITION);
2942 type = AREF (menu_items, i + MENU_ITEMS_ITEM_TYPE);
2943 selected = AREF (menu_items, i + MENU_ITEMS_ITEM_SELECTED);
2944 help = AREF (menu_items, i + MENU_ITEMS_ITEM_HELP);
2946 #ifndef HAVE_MULTILINGUAL_MENU
2947 if (STRINGP (item_name) && STRING_MULTIBYTE (item_name))
2949 item_name = ENCODE_MENU_STRING (item_name);
2950 AREF (menu_items, i + MENU_ITEMS_ITEM_NAME) = item_name;
2953 if (STRINGP (descrip) && STRING_MULTIBYTE (descrip))
2955 descrip = ENCODE_MENU_STRING (descrip);
2956 AREF (menu_items, i + MENU_ITEMS_ITEM_EQUIV_KEY) = descrip;
2958 #endif /* not HAVE_MULTILINGUAL_MENU */
2960 wv = xmalloc_widget_value ();
2961 if (prev_wv)
2962 prev_wv->next = wv;
2963 else
2964 save_wv->contents = wv;
2965 wv->name = (char *) SDATA (item_name);
2966 if (!NILP (descrip))
2967 wv->key = (char *) SDATA (descrip);
2968 wv->value = 0;
2969 /* If this item has a null value,
2970 make the call_data null so that it won't display a box
2971 when the mouse is on it. */
2972 wv->call_data
2973 = (!NILP (def) ? (void *) &XVECTOR (menu_items)->contents[i] : 0);
2974 wv->enabled = !NILP (enable);
2976 if (NILP (type))
2977 wv->button_type = BUTTON_TYPE_NONE;
2978 else if (EQ (type, QCtoggle))
2979 wv->button_type = BUTTON_TYPE_TOGGLE;
2980 else if (EQ (type, QCradio))
2981 wv->button_type = BUTTON_TYPE_RADIO;
2982 else
2983 abort ();
2985 wv->selected = !NILP (selected);
2987 if (! STRINGP (help))
2988 help = Qnil;
2990 wv->help = help;
2992 prev_wv = wv;
2994 i += MENU_ITEMS_ITEM_LENGTH;
2998 /* Deal with the title, if it is non-nil. */
2999 if (!NILP (title))
3001 widget_value *wv_title = xmalloc_widget_value ();
3002 widget_value *wv_sep1 = xmalloc_widget_value ();
3003 widget_value *wv_sep2 = xmalloc_widget_value ();
3005 wv_sep2->name = "--";
3006 wv_sep2->next = first_wv->contents;
3007 wv_sep2->help = Qnil;
3009 wv_sep1->name = "--";
3010 wv_sep1->next = wv_sep2;
3011 wv_sep1->help = Qnil;
3013 #ifndef HAVE_MULTILINGUAL_MENU
3014 if (STRING_MULTIBYTE (title))
3015 title = ENCODE_MENU_STRING (title);
3016 #endif
3018 wv_title->name = (char *) SDATA (title);
3019 wv_title->enabled = TRUE;
3020 wv_title->button_type = BUTTON_TYPE_NONE;
3021 wv_title->next = wv_sep1;
3022 wv_title->help = Qnil;
3023 first_wv->contents = wv_title;
3026 /* No selection has been chosen yet. */
3027 menu_item_selection = 0;
3029 /* Actually create and show the menu until popped down. */
3030 create_and_show_popup_menu (f, first_wv, x, y, for_click);
3032 /* Free the widget_value objects we used to specify the contents. */
3033 free_menubar_widget_value_tree (first_wv);
3035 /* Find the selected item, and its pane, to return
3036 the proper value. */
3037 if (menu_item_selection != 0)
3039 Lisp_Object prefix, entry;
3041 prefix = entry = Qnil;
3042 i = 0;
3043 while (i < menu_items_used)
3045 if (EQ (XVECTOR (menu_items)->contents[i], Qnil))
3047 subprefix_stack[submenu_depth++] = prefix;
3048 prefix = entry;
3049 i++;
3051 else if (EQ (XVECTOR (menu_items)->contents[i], Qlambda))
3053 prefix = subprefix_stack[--submenu_depth];
3054 i++;
3056 else if (EQ (XVECTOR (menu_items)->contents[i], Qt))
3058 prefix
3059 = XVECTOR (menu_items)->contents[i + MENU_ITEMS_PANE_PREFIX];
3060 i += MENU_ITEMS_PANE_LENGTH;
3062 /* Ignore a nil in the item list.
3063 It's meaningful only for dialog boxes. */
3064 else if (EQ (XVECTOR (menu_items)->contents[i], Qquote))
3065 i += 1;
3066 else
3068 entry
3069 = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_VALUE];
3070 if (menu_item_selection == &XVECTOR (menu_items)->contents[i])
3072 if (keymaps != 0)
3074 int j;
3076 entry = Fcons (entry, Qnil);
3077 if (!NILP (prefix))
3078 entry = Fcons (prefix, entry);
3079 for (j = submenu_depth - 1; j >= 0; j--)
3080 if (!NILP (subprefix_stack[j]))
3081 entry = Fcons (subprefix_stack[j], entry);
3083 return entry;
3085 i += MENU_ITEMS_ITEM_LENGTH;
3089 else if (!for_click)
3090 /* Make "Cancel" equivalent to C-g. */
3091 Fsignal (Qquit, Qnil);
3093 return Qnil;
3096 #ifdef USE_GTK
3097 static void
3098 dialog_selection_callback (widget, client_data)
3099 GtkWidget *widget;
3100 gpointer client_data;
3102 /* The EMACS_INT cast avoids a warning. There's no problem
3103 as long as pointers have enough bits to hold small integers. */
3104 if ((int) (EMACS_INT) client_data != -1)
3105 menu_item_selection = (Lisp_Object *) client_data;
3107 popup_activated_flag = 0;
3110 /* Pop up the dialog for frame F defined by FIRST_WV and loop until the
3111 dialog pops down.
3112 menu_item_selection will be set to the selection. */
3113 static void
3114 create_and_show_dialog (f, first_wv)
3115 FRAME_PTR f;
3116 widget_value *first_wv;
3118 GtkWidget *menu;
3120 menu = xg_create_widget ("dialog", first_wv->name, f, first_wv,
3121 G_CALLBACK (dialog_selection_callback),
3122 G_CALLBACK (popup_deactivate_callback),
3125 if (menu)
3127 int specpdl_count = SPECPDL_INDEX ();
3128 record_unwind_protect (pop_down_menu, make_save_value (menu, 0));
3130 /* Display the menu. */
3131 gtk_widget_show_all (menu);
3133 /* Process events that apply to the menu. */
3134 popup_widget_loop (1, menu);
3136 unbind_to (specpdl_count, Qnil);
3140 #else /* not USE_GTK */
3141 static void
3142 dialog_selection_callback (widget, id, client_data)
3143 Widget widget;
3144 LWLIB_ID id;
3145 XtPointer client_data;
3147 /* The EMACS_INT cast avoids a warning. There's no problem
3148 as long as pointers have enough bits to hold small integers. */
3149 if ((int) (EMACS_INT) client_data != -1)
3150 menu_item_selection = (Lisp_Object *) client_data;
3152 BLOCK_INPUT;
3153 lw_destroy_all_widgets (id);
3154 UNBLOCK_INPUT;
3155 popup_activated_flag = 0;
3159 /* Pop up the dialog for frame F defined by FIRST_WV and loop until the
3160 dialog pops down.
3161 menu_item_selection will be set to the selection. */
3162 static void
3163 create_and_show_dialog (f, first_wv)
3164 FRAME_PTR f;
3165 widget_value *first_wv;
3167 LWLIB_ID dialog_id;
3169 dialog_id = widget_id_tick++;
3170 lw_create_widget (first_wv->name, "dialog", dialog_id, first_wv,
3171 f->output_data.x->widget, 1, 0,
3172 dialog_selection_callback, 0, 0);
3173 lw_modify_all_widgets (dialog_id, first_wv->contents, True);
3175 /* Display the dialog box. */
3176 lw_pop_up_all_widgets (dialog_id);
3177 popup_activated_flag = 1;
3179 /* Process events that apply to the dialog box.
3180 Also handle timers. */
3182 int count = SPECPDL_INDEX ();
3183 int fact = 4 * sizeof (LWLIB_ID);
3185 /* xdialog_show_unwind is responsible for popping the dialog box down. */
3186 record_unwind_protect (pop_down_menu,
3187 Fcons (make_number (dialog_id >> (fact)),
3188 make_number (dialog_id & ~(-1 << (fact)))));
3190 popup_get_selection ((XEvent *) 0, FRAME_X_DISPLAY_INFO (f),
3191 dialog_id, 1);
3193 unbind_to (count, Qnil);
3197 #endif /* not USE_GTK */
3199 static char * button_names [] = {
3200 "button1", "button2", "button3", "button4", "button5",
3201 "button6", "button7", "button8", "button9", "button10" };
3203 static Lisp_Object
3204 xdialog_show (f, keymaps, title, header, error_name)
3205 FRAME_PTR f;
3206 int keymaps;
3207 Lisp_Object title, header;
3208 char **error_name;
3210 int i, nb_buttons=0;
3211 char dialog_name[6];
3213 widget_value *wv, *first_wv = 0, *prev_wv = 0;
3215 /* Number of elements seen so far, before boundary. */
3216 int left_count = 0;
3217 /* 1 means we've seen the boundary between left-hand elts and right-hand. */
3218 int boundary_seen = 0;
3220 *error_name = NULL;
3222 if (menu_items_n_panes > 1)
3224 *error_name = "Multiple panes in dialog box";
3225 return Qnil;
3228 /* Create a tree of widget_value objects
3229 representing the text label and buttons. */
3231 Lisp_Object pane_name, prefix;
3232 char *pane_string;
3233 pane_name = XVECTOR (menu_items)->contents[MENU_ITEMS_PANE_NAME];
3234 prefix = XVECTOR (menu_items)->contents[MENU_ITEMS_PANE_PREFIX];
3235 pane_string = (NILP (pane_name)
3236 ? "" : (char *) SDATA (pane_name));
3237 prev_wv = xmalloc_widget_value ();
3238 prev_wv->value = pane_string;
3239 if (keymaps && !NILP (prefix))
3240 prev_wv->name++;
3241 prev_wv->enabled = 1;
3242 prev_wv->name = "message";
3243 prev_wv->help = Qnil;
3244 first_wv = prev_wv;
3246 /* Loop over all panes and items, filling in the tree. */
3247 i = MENU_ITEMS_PANE_LENGTH;
3248 while (i < menu_items_used)
3251 /* Create a new item within current pane. */
3252 Lisp_Object item_name, enable, descrip;
3253 item_name = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_NAME];
3254 enable = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_ENABLE];
3255 descrip
3256 = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_EQUIV_KEY];
3258 if (NILP (item_name))
3260 free_menubar_widget_value_tree (first_wv);
3261 *error_name = "Submenu in dialog items";
3262 return Qnil;
3264 if (EQ (item_name, Qquote))
3266 /* This is the boundary between left-side elts
3267 and right-side elts. Stop incrementing right_count. */
3268 boundary_seen = 1;
3269 i++;
3270 continue;
3272 if (nb_buttons >= 9)
3274 free_menubar_widget_value_tree (first_wv);
3275 *error_name = "Too many dialog items";
3276 return Qnil;
3279 wv = xmalloc_widget_value ();
3280 prev_wv->next = wv;
3281 wv->name = (char *) button_names[nb_buttons];
3282 if (!NILP (descrip))
3283 wv->key = (char *) SDATA (descrip);
3284 wv->value = (char *) SDATA (item_name);
3285 wv->call_data = (void *) &XVECTOR (menu_items)->contents[i];
3286 wv->enabled = !NILP (enable);
3287 wv->help = Qnil;
3288 prev_wv = wv;
3290 if (! boundary_seen)
3291 left_count++;
3293 nb_buttons++;
3294 i += MENU_ITEMS_ITEM_LENGTH;
3297 /* If the boundary was not specified,
3298 by default put half on the left and half on the right. */
3299 if (! boundary_seen)
3300 left_count = nb_buttons - nb_buttons / 2;
3302 wv = xmalloc_widget_value ();
3303 wv->name = dialog_name;
3304 wv->help = Qnil;
3306 /* Frame title: 'Q' = Question, 'I' = Information.
3307 Can also have 'E' = Error if, one day, we want
3308 a popup for errors. */
3309 if (NILP(header))
3310 dialog_name[0] = 'Q';
3311 else
3312 dialog_name[0] = 'I';
3314 /* Dialog boxes use a really stupid name encoding
3315 which specifies how many buttons to use
3316 and how many buttons are on the right. */
3317 dialog_name[1] = '0' + nb_buttons;
3318 dialog_name[2] = 'B';
3319 dialog_name[3] = 'R';
3320 /* Number of buttons to put on the right. */
3321 dialog_name[4] = '0' + nb_buttons - left_count;
3322 dialog_name[5] = 0;
3323 wv->contents = first_wv;
3324 first_wv = wv;
3327 /* No selection has been chosen yet. */
3328 menu_item_selection = 0;
3330 /* Actually create and show the dialog. */
3331 create_and_show_dialog (f, first_wv);
3333 /* Free the widget_value objects we used to specify the contents. */
3334 free_menubar_widget_value_tree (first_wv);
3336 /* Find the selected item, and its pane, to return
3337 the proper value. */
3338 if (menu_item_selection != 0)
3340 Lisp_Object prefix;
3342 prefix = Qnil;
3343 i = 0;
3344 while (i < menu_items_used)
3346 Lisp_Object entry;
3348 if (EQ (XVECTOR (menu_items)->contents[i], Qt))
3350 prefix
3351 = XVECTOR (menu_items)->contents[i + MENU_ITEMS_PANE_PREFIX];
3352 i += MENU_ITEMS_PANE_LENGTH;
3354 else if (EQ (XVECTOR (menu_items)->contents[i], Qquote))
3356 /* This is the boundary between left-side elts and
3357 right-side elts. */
3358 ++i;
3360 else
3362 entry
3363 = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_VALUE];
3364 if (menu_item_selection == &XVECTOR (menu_items)->contents[i])
3366 if (keymaps != 0)
3368 entry = Fcons (entry, Qnil);
3369 if (!NILP (prefix))
3370 entry = Fcons (prefix, entry);
3372 return entry;
3374 i += MENU_ITEMS_ITEM_LENGTH;
3378 else
3379 /* Make "Cancel" equivalent to C-g. */
3380 Fsignal (Qquit, Qnil);
3382 return Qnil;
3385 #else /* not USE_X_TOOLKIT && not USE_GTK */
3387 /* The frame of the last activated non-toolkit menu bar.
3388 Used to generate menu help events. */
3390 static struct frame *menu_help_frame;
3393 /* Show help HELP_STRING, or clear help if HELP_STRING is null.
3395 PANE is the pane number, and ITEM is the menu item number in
3396 the menu (currently not used).
3398 This cannot be done with generating a HELP_EVENT because
3399 XMenuActivate contains a loop that doesn't let Emacs process
3400 keyboard events. */
3402 static void
3403 menu_help_callback (help_string, pane, item)
3404 char *help_string;
3405 int pane, item;
3407 extern Lisp_Object Qmenu_item;
3408 Lisp_Object *first_item;
3409 Lisp_Object pane_name;
3410 Lisp_Object menu_object;
3412 first_item = XVECTOR (menu_items)->contents;
3413 if (EQ (first_item[0], Qt))
3414 pane_name = first_item[MENU_ITEMS_PANE_NAME];
3415 else if (EQ (first_item[0], Qquote))
3416 /* This shouldn't happen, see xmenu_show. */
3417 pane_name = empty_string;
3418 else
3419 pane_name = first_item[MENU_ITEMS_ITEM_NAME];
3421 /* (menu-item MENU-NAME PANE-NUMBER) */
3422 menu_object = Fcons (Qmenu_item,
3423 Fcons (pane_name,
3424 Fcons (make_number (pane), Qnil)));
3425 show_help_echo (help_string ? build_string (help_string) : Qnil,
3426 Qnil, menu_object, make_number (item), 1);
3429 static Lisp_Object
3430 pop_down_menu (arg)
3431 Lisp_Object arg;
3433 struct Lisp_Save_Value *p1 = XSAVE_VALUE (Fcar (arg));
3434 struct Lisp_Save_Value *p2 = XSAVE_VALUE (Fcdr (arg));
3436 FRAME_PTR f = p1->pointer;
3437 XMenu *menu = p2->pointer;
3439 BLOCK_INPUT;
3440 #ifndef MSDOS
3441 XUngrabPointer (FRAME_X_DISPLAY (f), CurrentTime);
3442 XUngrabKeyboard (FRAME_X_DISPLAY (f), CurrentTime);
3443 #endif
3444 XMenuDestroy (FRAME_X_DISPLAY (f), menu);
3446 #ifdef HAVE_X_WINDOWS
3447 /* Assume the mouse has moved out of the X window.
3448 If it has actually moved in, we will get an EnterNotify. */
3449 x_mouse_leave (FRAME_X_DISPLAY_INFO (f));
3451 /* State that no mouse buttons are now held.
3452 (The oldXMenu code doesn't track this info for us.)
3453 That is not necessarily true, but the fiction leads to reasonable
3454 results, and it is a pain to ask which are actually held now. */
3455 FRAME_X_DISPLAY_INFO (f)->grabbed = 0;
3457 #endif /* HAVE_X_WINDOWS */
3459 UNBLOCK_INPUT;
3461 return Qnil;
3465 static Lisp_Object
3466 xmenu_show (f, x, y, for_click, keymaps, title, error)
3467 FRAME_PTR f;
3468 int x, y;
3469 int for_click;
3470 int keymaps;
3471 Lisp_Object title;
3472 char **error;
3474 Window root;
3475 XMenu *menu;
3476 int pane, selidx, lpane, status;
3477 Lisp_Object entry, pane_prefix;
3478 char *datap;
3479 int ulx, uly, width, height;
3480 int dispwidth, dispheight;
3481 int i, j, lines, maxlines;
3482 int maxwidth;
3483 int dummy_int;
3484 unsigned int dummy_uint;
3485 int specpdl_count = SPECPDL_INDEX ();
3487 *error = 0;
3488 if (menu_items_n_panes == 0)
3489 return Qnil;
3491 if (menu_items_used <= MENU_ITEMS_PANE_LENGTH)
3493 *error = "Empty menu";
3494 return Qnil;
3497 /* Figure out which root window F is on. */
3498 XGetGeometry (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), &root,
3499 &dummy_int, &dummy_int, &dummy_uint, &dummy_uint,
3500 &dummy_uint, &dummy_uint);
3502 /* Make the menu on that window. */
3503 menu = XMenuCreate (FRAME_X_DISPLAY (f), root, "emacs");
3504 if (menu == NULL)
3506 *error = "Can't create menu";
3507 return Qnil;
3510 /* Don't GC while we prepare and show the menu,
3511 because we give the oldxmenu library pointers to the
3512 contents of strings. */
3513 inhibit_garbage_collection ();
3515 #ifdef HAVE_X_WINDOWS
3516 /* Adjust coordinates to relative to the outer (window manager) window. */
3517 x += FRAME_OUTER_TO_INNER_DIFF_X (f);
3518 y += FRAME_OUTER_TO_INNER_DIFF_Y (f);
3519 #endif /* HAVE_X_WINDOWS */
3521 /* Adjust coordinates to be root-window-relative. */
3522 x += f->left_pos;
3523 y += f->top_pos;
3525 /* Create all the necessary panes and their items. */
3526 maxlines = lines = i = 0;
3527 while (i < menu_items_used)
3529 if (EQ (XVECTOR (menu_items)->contents[i], Qt))
3531 /* Create a new pane. */
3532 Lisp_Object pane_name, prefix;
3533 char *pane_string;
3535 maxlines = max (maxlines, lines);
3536 lines = 0;
3537 pane_name = XVECTOR (menu_items)->contents[i + MENU_ITEMS_PANE_NAME];
3538 prefix = XVECTOR (menu_items)->contents[i + MENU_ITEMS_PANE_PREFIX];
3539 pane_string = (NILP (pane_name)
3540 ? "" : (char *) SDATA (pane_name));
3541 if (keymaps && !NILP (prefix))
3542 pane_string++;
3544 lpane = XMenuAddPane (FRAME_X_DISPLAY (f), menu, pane_string, TRUE);
3545 if (lpane == XM_FAILURE)
3547 XMenuDestroy (FRAME_X_DISPLAY (f), menu);
3548 *error = "Can't create pane";
3549 return Qnil;
3551 i += MENU_ITEMS_PANE_LENGTH;
3553 /* Find the width of the widest item in this pane. */
3554 maxwidth = 0;
3555 j = i;
3556 while (j < menu_items_used)
3558 Lisp_Object item;
3559 item = XVECTOR (menu_items)->contents[j];
3560 if (EQ (item, Qt))
3561 break;
3562 if (NILP (item))
3564 j++;
3565 continue;
3567 width = SBYTES (item);
3568 if (width > maxwidth)
3569 maxwidth = width;
3571 j += MENU_ITEMS_ITEM_LENGTH;
3574 /* Ignore a nil in the item list.
3575 It's meaningful only for dialog boxes. */
3576 else if (EQ (XVECTOR (menu_items)->contents[i], Qquote))
3577 i += 1;
3578 else
3580 /* Create a new item within current pane. */
3581 Lisp_Object item_name, enable, descrip, help;
3582 unsigned char *item_data;
3583 char *help_string;
3585 item_name = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_NAME];
3586 enable = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_ENABLE];
3587 descrip
3588 = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_EQUIV_KEY];
3589 help = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_HELP];
3590 help_string = STRINGP (help) ? SDATA (help) : NULL;
3592 if (!NILP (descrip))
3594 int gap = maxwidth - SBYTES (item_name);
3595 #ifdef C_ALLOCA
3596 Lisp_Object spacer;
3597 spacer = Fmake_string (make_number (gap), make_number (' '));
3598 item_name = concat2 (item_name, spacer);
3599 item_name = concat2 (item_name, descrip);
3600 item_data = SDATA (item_name);
3601 #else
3602 /* if alloca is fast, use that to make the space,
3603 to reduce gc needs. */
3604 item_data
3605 = (unsigned char *) alloca (maxwidth
3606 + SBYTES (descrip) + 1);
3607 bcopy (SDATA (item_name), item_data,
3608 SBYTES (item_name));
3609 for (j = SCHARS (item_name); j < maxwidth; j++)
3610 item_data[j] = ' ';
3611 bcopy (SDATA (descrip), item_data + j,
3612 SBYTES (descrip));
3613 item_data[j + SBYTES (descrip)] = 0;
3614 #endif
3616 else
3617 item_data = SDATA (item_name);
3619 if (XMenuAddSelection (FRAME_X_DISPLAY (f),
3620 menu, lpane, 0, item_data,
3621 !NILP (enable), help_string)
3622 == XM_FAILURE)
3624 XMenuDestroy (FRAME_X_DISPLAY (f), menu);
3625 *error = "Can't add selection to menu";
3626 return Qnil;
3628 i += MENU_ITEMS_ITEM_LENGTH;
3629 lines++;
3633 maxlines = max (maxlines, lines);
3635 /* All set and ready to fly. */
3636 XMenuRecompute (FRAME_X_DISPLAY (f), menu);
3637 dispwidth = DisplayWidth (FRAME_X_DISPLAY (f), FRAME_X_SCREEN_NUMBER (f));
3638 dispheight = DisplayHeight (FRAME_X_DISPLAY (f), FRAME_X_SCREEN_NUMBER (f));
3639 x = min (x, dispwidth);
3640 y = min (y, dispheight);
3641 x = max (x, 1);
3642 y = max (y, 1);
3643 XMenuLocate (FRAME_X_DISPLAY (f), menu, 0, 0, x, y,
3644 &ulx, &uly, &width, &height);
3645 if (ulx+width > dispwidth)
3647 x -= (ulx + width) - dispwidth;
3648 ulx = dispwidth - width;
3650 if (uly+height > dispheight)
3652 y -= (uly + height) - dispheight;
3653 uly = dispheight - height;
3655 if (ulx < 0) x -= ulx;
3656 if (uly < 0) y -= uly;
3658 if (! for_click)
3660 /* If position was not given by a mouse click, adjust so upper left
3661 corner of the menu as a whole ends up at given coordinates. This
3662 is what x-popup-menu says in its documentation. */
3663 x += width/2;
3664 y += 1.5*height/(maxlines+2);
3667 XMenuSetAEQ (menu, TRUE);
3668 XMenuSetFreeze (menu, TRUE);
3669 pane = selidx = 0;
3671 #ifndef MSDOS
3672 XMenuActivateSetWaitFunction (x_menu_wait_for_event, FRAME_X_DISPLAY (f));
3673 #endif
3675 record_unwind_protect (pop_down_menu,
3676 Fcons (make_save_value (f, 0),
3677 make_save_value (menu, 0)));
3679 /* Help display under X won't work because XMenuActivate contains
3680 a loop that doesn't give Emacs a chance to process it. */
3681 menu_help_frame = f;
3682 status = XMenuActivate (FRAME_X_DISPLAY (f), menu, &pane, &selidx,
3683 x, y, ButtonReleaseMask, &datap,
3684 menu_help_callback);
3686 switch (status)
3688 case XM_SUCCESS:
3689 #ifdef XDEBUG
3690 fprintf (stderr, "pane= %d line = %d\n", panes, selidx);
3691 #endif
3693 /* Find the item number SELIDX in pane number PANE. */
3694 i = 0;
3695 while (i < menu_items_used)
3697 if (EQ (XVECTOR (menu_items)->contents[i], Qt))
3699 if (pane == 0)
3700 pane_prefix
3701 = XVECTOR (menu_items)->contents[i + MENU_ITEMS_PANE_PREFIX];
3702 pane--;
3703 i += MENU_ITEMS_PANE_LENGTH;
3705 else
3707 if (pane == -1)
3709 if (selidx == 0)
3711 entry
3712 = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_VALUE];
3713 if (keymaps != 0)
3715 entry = Fcons (entry, Qnil);
3716 if (!NILP (pane_prefix))
3717 entry = Fcons (pane_prefix, entry);
3719 break;
3721 selidx--;
3723 i += MENU_ITEMS_ITEM_LENGTH;
3726 break;
3728 case XM_FAILURE:
3729 *error = "Can't activate menu";
3730 case XM_IA_SELECT:
3731 entry = Qnil;
3732 break;
3733 case XM_NO_SELECT:
3734 /* Make "Cancel" equivalent to C-g unless FOR_CLICK (which means
3735 the menu was invoked with a mouse event as POSITION). */
3736 if (! for_click)
3737 Fsignal (Qquit, Qnil);
3738 entry = Qnil;
3739 break;
3742 unbind_to (specpdl_count, Qnil);
3744 return entry;
3747 #endif /* not USE_X_TOOLKIT */
3749 #endif /* HAVE_MENUS */
3751 void
3752 syms_of_xmenu ()
3754 staticpro (&menu_items);
3755 menu_items = Qnil;
3756 menu_items_inuse = Qnil;
3758 Qdebug_on_next_call = intern ("debug-on-next-call");
3759 staticpro (&Qdebug_on_next_call);
3761 DEFVAR_LISP ("menu-updating-frame", &Vmenu_updating_frame,
3762 doc: /* Frame for which we are updating a menu.
3763 The enable predicate for a menu command should check this variable. */);
3764 Vmenu_updating_frame = Qnil;
3766 #ifdef USE_X_TOOLKIT
3767 widget_id_tick = (1<<16);
3768 next_menubar_widget_id = 1;
3769 #endif
3771 defsubr (&Sx_popup_menu);
3773 #if defined (USE_GTK) || defined (USE_X_TOOLKIT)
3774 defsubr (&Smenu_bar_open);
3775 Fdefalias (intern ("accelerate-menu"), intern (Smenu_bar_open.symbol_name),
3776 Qnil);
3777 #endif
3779 #ifdef HAVE_MENUS
3780 defsubr (&Sx_popup_dialog);
3781 #endif
3784 /* arch-tag: 92ea573c-398e-496e-ac73-2436f7d63242
3785 (do not change this comment) */