* config.guess, config.sub: Updated from master source.
[emacs.git] / src / xmenu.c
blob51867f0478f9cb9d25160da1628fab7a045b44f7
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 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))
271 menu_items_allocated = 60;
272 menu_items = Fmake_vector (make_number (menu_items_allocated), Qnil);
275 if (!NILP (menu_items_inuse))
276 error ("Trying to use a menu from within a menu-entry");
277 menu_items_inuse = Qt;
278 menu_items_used = 0;
279 menu_items_n_panes = 0;
280 menu_items_submenu_depth = 0;
283 /* Call at the end of generating the data in menu_items. */
285 static void
286 finish_menu_items ()
290 static Lisp_Object
291 unuse_menu_items (dummy)
292 Lisp_Object dummy;
294 return menu_items_inuse = Qnil;
297 /* Call when finished using the data for the current menu
298 in menu_items. */
300 static void
301 discard_menu_items ()
303 /* Free the structure if it is especially large.
304 Otherwise, hold on to it, to save time. */
305 if (menu_items_allocated > 200)
307 menu_items = Qnil;
308 menu_items_allocated = 0;
310 xassert (NILP (menu_items_inuse));
313 /* Make the menu_items vector twice as large. */
315 static void
316 grow_menu_items ()
318 Lisp_Object old;
319 int old_size = menu_items_allocated;
320 old = menu_items;
322 menu_items_allocated *= 2;
323 menu_items = Fmake_vector (make_number (menu_items_allocated), Qnil);
324 bcopy (XVECTOR (old)->contents, XVECTOR (menu_items)->contents,
325 old_size * sizeof (Lisp_Object));
328 /* Begin a submenu. */
330 static void
331 push_submenu_start ()
333 if (menu_items_used + 1 > menu_items_allocated)
334 grow_menu_items ();
336 XVECTOR (menu_items)->contents[menu_items_used++] = Qnil;
337 menu_items_submenu_depth++;
340 /* End a submenu. */
342 static void
343 push_submenu_end ()
345 if (menu_items_used + 1 > menu_items_allocated)
346 grow_menu_items ();
348 XVECTOR (menu_items)->contents[menu_items_used++] = Qlambda;
349 menu_items_submenu_depth--;
352 /* Indicate boundary between left and right. */
354 static void
355 push_left_right_boundary ()
357 if (menu_items_used + 1 > menu_items_allocated)
358 grow_menu_items ();
360 XVECTOR (menu_items)->contents[menu_items_used++] = Qquote;
363 /* Start a new menu pane in menu_items.
364 NAME is the pane name. PREFIX_VEC is a prefix key for this pane. */
366 static void
367 push_menu_pane (name, prefix_vec)
368 Lisp_Object name, prefix_vec;
370 if (menu_items_used + MENU_ITEMS_PANE_LENGTH > menu_items_allocated)
371 grow_menu_items ();
373 if (menu_items_submenu_depth == 0)
374 menu_items_n_panes++;
375 XVECTOR (menu_items)->contents[menu_items_used++] = Qt;
376 XVECTOR (menu_items)->contents[menu_items_used++] = name;
377 XVECTOR (menu_items)->contents[menu_items_used++] = prefix_vec;
380 /* Push one menu item into the current pane. NAME is the string to
381 display. ENABLE if non-nil means this item can be selected. KEY
382 is the key generated by choosing this item, or nil if this item
383 doesn't really have a definition. DEF is the definition of this
384 item. EQUIV is the textual description of the keyboard equivalent
385 for this item (or nil if none). TYPE is the type of this menu
386 item, one of nil, `toggle' or `radio'. */
388 static void
389 push_menu_item (name, enable, key, def, equiv, type, selected, help)
390 Lisp_Object name, enable, key, def, equiv, type, selected, help;
392 if (menu_items_used + MENU_ITEMS_ITEM_LENGTH > menu_items_allocated)
393 grow_menu_items ();
395 XVECTOR (menu_items)->contents[menu_items_used++] = name;
396 XVECTOR (menu_items)->contents[menu_items_used++] = enable;
397 XVECTOR (menu_items)->contents[menu_items_used++] = key;
398 XVECTOR (menu_items)->contents[menu_items_used++] = equiv;
399 XVECTOR (menu_items)->contents[menu_items_used++] = def;
400 XVECTOR (menu_items)->contents[menu_items_used++] = type;
401 XVECTOR (menu_items)->contents[menu_items_used++] = selected;
402 XVECTOR (menu_items)->contents[menu_items_used++] = help;
405 /* Look through KEYMAPS, a vector of keymaps that is NMAPS long,
406 and generate menu panes for them in menu_items.
407 If NOTREAL is nonzero,
408 don't bother really computing whether an item is enabled. */
410 static void
411 keymap_panes (keymaps, nmaps, notreal)
412 Lisp_Object *keymaps;
413 int nmaps;
414 int notreal;
416 int mapno;
418 init_menu_items ();
420 /* Loop over the given keymaps, making a pane for each map.
421 But don't make a pane that is empty--ignore that map instead.
422 P is the number of panes we have made so far. */
423 for (mapno = 0; mapno < nmaps; mapno++)
424 single_keymap_panes (keymaps[mapno],
425 Fkeymap_prompt (keymaps[mapno]), Qnil, notreal, 10);
427 finish_menu_items ();
430 /* Args passed between single_keymap_panes and single_menu_item. */
431 struct skp
433 Lisp_Object pending_maps;
434 int maxdepth, notreal;
435 int notbuttons;
438 static void single_menu_item P_ ((Lisp_Object, Lisp_Object, Lisp_Object,
439 void *));
441 /* This is a recursive subroutine of keymap_panes.
442 It handles one keymap, KEYMAP.
443 The other arguments are passed along
444 or point to local variables of the previous function.
445 If NOTREAL is nonzero, only check for equivalent key bindings, don't
446 evaluate expressions in menu items and don't make any menu.
448 If we encounter submenus deeper than MAXDEPTH levels, ignore them. */
450 static void
451 single_keymap_panes (keymap, pane_name, prefix, notreal, maxdepth)
452 Lisp_Object keymap;
453 Lisp_Object pane_name;
454 Lisp_Object prefix;
455 int notreal;
456 int maxdepth;
458 struct skp skp;
459 struct gcpro gcpro1;
461 skp.pending_maps = Qnil;
462 skp.maxdepth = maxdepth;
463 skp.notreal = notreal;
464 skp.notbuttons = 0;
466 if (maxdepth <= 0)
467 return;
469 push_menu_pane (pane_name, prefix);
471 #ifndef HAVE_BOXES
472 /* Remember index for first item in this pane so we can go back and
473 add a prefix when (if) we see the first button. After that, notbuttons
474 is set to 0, to mark that we have seen a button and all non button
475 items need a prefix. */
476 skp.notbuttons = menu_items_used;
477 #endif
479 GCPRO1 (skp.pending_maps);
480 map_keymap (keymap, single_menu_item, Qnil, &skp, 1);
481 UNGCPRO;
483 /* Process now any submenus which want to be panes at this level. */
484 while (CONSP (skp.pending_maps))
486 Lisp_Object elt, eltcdr, string;
487 elt = XCAR (skp.pending_maps);
488 eltcdr = XCDR (elt);
489 string = XCAR (eltcdr);
490 /* We no longer discard the @ from the beginning of the string here.
491 Instead, we do this in xmenu_show. */
492 single_keymap_panes (Fcar (elt), string,
493 XCDR (eltcdr), notreal, maxdepth - 1);
494 skp.pending_maps = XCDR (skp.pending_maps);
498 /* This is a subroutine of single_keymap_panes that handles one
499 keymap entry.
500 KEY is a key in a keymap and ITEM is its binding.
501 SKP->PENDING_MAPS_PTR is a list of keymaps waiting to be made into
502 separate panes.
503 If SKP->NOTREAL is nonzero, only check for equivalent key bindings, don't
504 evaluate expressions in menu items and don't make any menu.
505 If we encounter submenus deeper than SKP->MAXDEPTH levels, ignore them.
506 SKP->NOTBUTTONS is only used when simulating toggle boxes and radio
507 buttons. It keeps track of if we have seen a button in this menu or
508 not. */
510 static void
511 single_menu_item (key, item, dummy, skp_v)
512 Lisp_Object key, item, dummy;
513 void *skp_v;
515 Lisp_Object map, item_string, enabled;
516 struct gcpro gcpro1, gcpro2;
517 int res;
518 struct skp *skp = skp_v;
520 /* Parse the menu item and leave the result in item_properties. */
521 GCPRO2 (key, item);
522 res = parse_menu_item (item, skp->notreal, 0);
523 UNGCPRO;
524 if (!res)
525 return; /* Not a menu item. */
527 map = XVECTOR (item_properties)->contents[ITEM_PROPERTY_MAP];
529 if (skp->notreal)
531 /* We don't want to make a menu, just traverse the keymaps to
532 precompute equivalent key bindings. */
533 if (!NILP (map))
534 single_keymap_panes (map, Qnil, key, 1, skp->maxdepth - 1);
535 return;
538 enabled = XVECTOR (item_properties)->contents[ITEM_PROPERTY_ENABLE];
539 item_string = XVECTOR (item_properties)->contents[ITEM_PROPERTY_NAME];
541 if (!NILP (map) && SREF (item_string, 0) == '@')
543 if (!NILP (enabled))
544 /* An enabled separate pane. Remember this to handle it later. */
545 skp->pending_maps = Fcons (Fcons (map, Fcons (item_string, key)),
546 skp->pending_maps);
547 return;
550 #ifndef HAVE_BOXES
551 /* Simulate radio buttons and toggle boxes by putting a prefix in
552 front of them. */
554 Lisp_Object prefix = Qnil;
555 Lisp_Object type = XVECTOR (item_properties)->contents[ITEM_PROPERTY_TYPE];
556 if (!NILP (type))
558 Lisp_Object selected
559 = XVECTOR (item_properties)->contents[ITEM_PROPERTY_SELECTED];
561 if (skp->notbuttons)
562 /* The first button. Line up previous items in this menu. */
564 int index = skp->notbuttons; /* Index for first item this menu. */
565 int submenu = 0;
566 Lisp_Object tem;
567 while (index < menu_items_used)
570 = XVECTOR (menu_items)->contents[index + MENU_ITEMS_ITEM_NAME];
571 if (NILP (tem))
573 index++;
574 submenu++; /* Skip sub menu. */
576 else if (EQ (tem, Qlambda))
578 index++;
579 submenu--; /* End sub menu. */
581 else if (EQ (tem, Qt))
582 index += 3; /* Skip new pane marker. */
583 else if (EQ (tem, Qquote))
584 index++; /* Skip a left, right divider. */
585 else
587 if (!submenu && SREF (tem, 0) != '\0'
588 && SREF (tem, 0) != '-')
589 XVECTOR (menu_items)->contents[index + MENU_ITEMS_ITEM_NAME]
590 = concat2 (build_string (" "), tem);
591 index += MENU_ITEMS_ITEM_LENGTH;
594 skp->notbuttons = 0;
597 /* Calculate prefix, if any, for this item. */
598 if (EQ (type, QCtoggle))
599 prefix = build_string (NILP (selected) ? "[ ] " : "[X] ");
600 else if (EQ (type, QCradio))
601 prefix = build_string (NILP (selected) ? "( ) " : "(*) ");
603 /* Not a button. If we have earlier buttons, then we need a prefix. */
604 else if (!skp->notbuttons && SREF (item_string, 0) != '\0'
605 && SREF (item_string, 0) != '-')
606 prefix = build_string (" ");
608 if (!NILP (prefix))
609 item_string = concat2 (prefix, item_string);
611 #endif /* not HAVE_BOXES */
613 #if ! defined (USE_X_TOOLKIT) && ! defined (USE_GTK)
614 if (!NILP (map))
615 /* Indicate visually that this is a submenu. */
616 item_string = concat2 (item_string, build_string (" >"));
617 #endif
619 push_menu_item (item_string, enabled, key,
620 XVECTOR (item_properties)->contents[ITEM_PROPERTY_DEF],
621 XVECTOR (item_properties)->contents[ITEM_PROPERTY_KEYEQ],
622 XVECTOR (item_properties)->contents[ITEM_PROPERTY_TYPE],
623 XVECTOR (item_properties)->contents[ITEM_PROPERTY_SELECTED],
624 XVECTOR (item_properties)->contents[ITEM_PROPERTY_HELP]);
626 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
627 /* Display a submenu using the toolkit. */
628 if (! (NILP (map) || NILP (enabled)))
630 push_submenu_start ();
631 single_keymap_panes (map, Qnil, key, 0, skp->maxdepth - 1);
632 push_submenu_end ();
634 #endif
637 /* Push all the panes and items of a menu described by the
638 alist-of-alists MENU.
639 This handles old-fashioned calls to x-popup-menu. */
641 static void
642 list_of_panes (menu)
643 Lisp_Object menu;
645 Lisp_Object tail;
647 init_menu_items ();
649 for (tail = menu; CONSP (tail); tail = XCDR (tail))
651 Lisp_Object elt, pane_name, pane_data;
652 elt = XCAR (tail);
653 pane_name = Fcar (elt);
654 CHECK_STRING (pane_name);
655 push_menu_pane (ENCODE_MENU_STRING (pane_name), Qnil);
656 pane_data = Fcdr (elt);
657 CHECK_CONS (pane_data);
658 list_of_items (pane_data);
661 finish_menu_items ();
664 /* Push the items in a single pane defined by the alist PANE. */
666 static void
667 list_of_items (pane)
668 Lisp_Object pane;
670 Lisp_Object tail, item, item1;
672 for (tail = pane; CONSP (tail); tail = XCDR (tail))
674 item = XCAR (tail);
675 if (STRINGP (item))
676 push_menu_item (ENCODE_MENU_STRING (item), Qnil, Qnil, Qt,
677 Qnil, Qnil, Qnil, Qnil);
678 else if (CONSP (item))
680 item1 = XCAR (item);
681 CHECK_STRING (item1);
682 push_menu_item (ENCODE_MENU_STRING (item1), Qt, XCDR (item),
683 Qt, Qnil, Qnil, Qnil, Qnil);
685 else
686 push_left_right_boundary ();
691 #ifdef HAVE_X_WINDOWS
692 /* Return the mouse position in *X and *Y. The coordinates are window
693 relative for the edit window in frame F.
694 This is for Fx_popup_menu. The mouse_position_hook can not
695 be used for X, as it returns window relative coordinates
696 for the window where the mouse is in. This could be the menu bar,
697 the scroll bar or the edit window. Fx_popup_menu needs to be
698 sure it is the edit window. */
699 static void
700 mouse_position_for_popup (f, x, y)
701 FRAME_PTR f;
702 int *x;
703 int *y;
705 Window root, dummy_window;
706 int dummy;
708 BLOCK_INPUT;
710 XQueryPointer (FRAME_X_DISPLAY (f),
711 DefaultRootWindow (FRAME_X_DISPLAY (f)),
713 /* The root window which contains the pointer. */
714 &root,
716 /* Window pointer is on, not used */
717 &dummy_window,
719 /* The position on that root window. */
720 x, y,
722 /* x/y in dummy_window coordinates, not used. */
723 &dummy, &dummy,
725 /* Modifier keys and pointer buttons, about which
726 we don't care. */
727 (unsigned int *) &dummy);
729 UNBLOCK_INPUT;
731 /* xmenu_show expects window coordinates, not root window
732 coordinates. Translate. */
733 *x -= f->left_pos + FRAME_OUTER_TO_INNER_DIFF_X (f);
734 *y -= f->top_pos + FRAME_OUTER_TO_INNER_DIFF_Y (f);
737 #endif /* HAVE_X_WINDOWS */
739 DEFUN ("x-popup-menu", Fx_popup_menu, Sx_popup_menu, 2, 2, 0,
740 doc: /* Pop up a deck-of-cards menu and return user's selection.
741 POSITION is a position specification. This is either a mouse button event
742 or a list ((XOFFSET YOFFSET) WINDOW)
743 where XOFFSET and YOFFSET are positions in pixels from the top left
744 corner of WINDOW. (WINDOW may be a window or a frame object.)
745 This controls the position of the top left of the menu as a whole.
746 If POSITION is t, it means to use the current mouse position.
748 MENU is a specifier for a menu. For the simplest case, MENU is a keymap.
749 The menu items come from key bindings that have a menu string as well as
750 a definition; actually, the "definition" in such a key binding looks like
751 \(STRING . REAL-DEFINITION). To give the menu a title, put a string into
752 the keymap as a top-level element.
754 If REAL-DEFINITION is nil, that puts a nonselectable string in the menu.
755 Otherwise, REAL-DEFINITION should be a valid key binding definition.
757 You can also use a list of keymaps as MENU.
758 Then each keymap makes a separate pane.
760 When MENU is a keymap or a list of keymaps, the return value is the
761 list of events corresponding to the user's choice. Note that
762 `x-popup-menu' does not actually execute the command bound to that
763 sequence of events.
765 Alternatively, you can specify a menu of multiple panes
766 with a list of the form (TITLE PANE1 PANE2...),
767 where each pane is a list of form (TITLE ITEM1 ITEM2...).
768 Each ITEM is normally a cons cell (STRING . VALUE);
769 but a string can appear as an item--that makes a nonselectable line
770 in the menu.
771 With this form of menu, the return value is VALUE from the chosen item.
773 If POSITION is nil, don't display the menu at all, just precalculate the
774 cached information about equivalent key sequences.
776 If the user gets rid of the menu without making a valid choice, for
777 instance by clicking the mouse away from a valid choice or by typing
778 keyboard input, then this normally results in a quit and
779 `x-popup-menu' does not return. But if POSITION is a mouse button
780 event (indicating that the user invoked the menu with the mouse) then
781 no quit occurs and `x-popup-menu' returns nil. */)
782 (position, menu)
783 Lisp_Object position, menu;
785 Lisp_Object keymap, tem;
786 int xpos = 0, ypos = 0;
787 Lisp_Object title;
788 char *error_name = NULL;
789 Lisp_Object selection;
790 FRAME_PTR f = NULL;
791 Lisp_Object x, y, window;
792 int keymaps = 0;
793 int for_click = 0;
794 int specpdl_count = SPECPDL_INDEX ();
795 struct gcpro gcpro1;
797 #ifdef HAVE_MENUS
798 if (! NILP (position))
800 int get_current_pos_p = 0;
801 check_x ();
803 /* Decode the first argument: find the window and the coordinates. */
804 if (EQ (position, Qt)
805 || (CONSP (position) && (EQ (XCAR (position), Qmenu_bar)
806 || EQ (XCAR (position), Qtool_bar))))
808 get_current_pos_p = 1;
810 else
812 tem = Fcar (position);
813 if (CONSP (tem))
815 window = Fcar (Fcdr (position));
816 x = XCAR (tem);
817 y = Fcar (XCDR (tem));
819 else
821 for_click = 1;
822 tem = Fcar (Fcdr (position)); /* EVENT_START (position) */
823 window = Fcar (tem); /* POSN_WINDOW (tem) */
824 tem = Fcar (Fcdr (Fcdr (tem))); /* POSN_WINDOW_POSN (tem) */
825 x = Fcar (tem);
826 y = Fcdr (tem);
829 /* If a click happens in an external tool bar or a detached
830 tool bar, x and y is NIL. In that case, use the current
831 mouse position. This happens for the help button in the
832 tool bar. Ideally popup-menu should pass NIL to
833 this function, but it doesn't. */
834 if (NILP (x) && NILP (y))
835 get_current_pos_p = 1;
838 if (get_current_pos_p)
840 /* Use the mouse's current position. */
841 FRAME_PTR new_f = SELECTED_FRAME ();
842 #ifdef HAVE_X_WINDOWS
843 /* Can't use mouse_position_hook for X since it returns
844 coordinates relative to the window the mouse is in,
845 we need coordinates relative to the edit widget always. */
846 if (new_f != 0)
848 int cur_x, cur_y;
850 mouse_position_for_popup (new_f, &cur_x, &cur_y);
851 /* cur_x/y may be negative, so use make_number. */
852 x = make_number (cur_x);
853 y = make_number (cur_y);
856 #else /* not HAVE_X_WINDOWS */
857 Lisp_Object bar_window;
858 enum scroll_bar_part part;
859 unsigned long time;
861 if (mouse_position_hook)
862 (*mouse_position_hook) (&new_f, 1, &bar_window,
863 &part, &x, &y, &time);
864 #endif /* not HAVE_X_WINDOWS */
866 if (new_f != 0)
867 XSETFRAME (window, new_f);
868 else
870 window = selected_window;
871 XSETFASTINT (x, 0);
872 XSETFASTINT (y, 0);
876 CHECK_NUMBER (x);
877 CHECK_NUMBER (y);
879 /* Decode where to put the menu. */
881 if (FRAMEP (window))
883 f = XFRAME (window);
884 xpos = 0;
885 ypos = 0;
887 else if (WINDOWP (window))
889 CHECK_LIVE_WINDOW (window);
890 f = XFRAME (WINDOW_FRAME (XWINDOW (window)));
892 xpos = WINDOW_LEFT_EDGE_X (XWINDOW (window));
893 ypos = WINDOW_TOP_EDGE_Y (XWINDOW (window));
895 else
896 /* ??? Not really clean; should be CHECK_WINDOW_OR_FRAME,
897 but I don't want to make one now. */
898 CHECK_WINDOW (window);
900 xpos += XINT (x);
901 ypos += XINT (y);
903 XSETFRAME (Vmenu_updating_frame, f);
905 else
906 Vmenu_updating_frame = Qnil;
907 #endif /* HAVE_MENUS */
909 record_unwind_protect (unuse_menu_items, Qnil);
910 title = Qnil;
911 GCPRO1 (title);
913 /* Decode the menu items from what was specified. */
915 keymap = get_keymap (menu, 0, 0);
916 if (CONSP (keymap))
918 /* We were given a keymap. Extract menu info from the keymap. */
919 Lisp_Object prompt;
921 /* Extract the detailed info to make one pane. */
922 keymap_panes (&menu, 1, NILP (position));
924 /* Search for a string appearing directly as an element of the keymap.
925 That string is the title of the menu. */
926 prompt = Fkeymap_prompt (keymap);
927 if (NILP (title) && !NILP (prompt))
928 title = prompt;
930 /* Make that be the pane title of the first pane. */
931 if (!NILP (prompt) && menu_items_n_panes >= 0)
932 XVECTOR (menu_items)->contents[MENU_ITEMS_PANE_NAME] = prompt;
934 keymaps = 1;
936 else if (CONSP (menu) && KEYMAPP (XCAR (menu)))
938 /* We were given a list of keymaps. */
939 int nmaps = XFASTINT (Flength (menu));
940 Lisp_Object *maps
941 = (Lisp_Object *) alloca (nmaps * sizeof (Lisp_Object));
942 int i;
944 title = Qnil;
946 /* The first keymap that has a prompt string
947 supplies the menu title. */
948 for (tem = menu, i = 0; CONSP (tem); tem = XCDR (tem))
950 Lisp_Object prompt;
952 maps[i++] = keymap = get_keymap (XCAR (tem), 1, 0);
954 prompt = Fkeymap_prompt (keymap);
955 if (NILP (title) && !NILP (prompt))
956 title = prompt;
959 /* Extract the detailed info to make one pane. */
960 keymap_panes (maps, nmaps, NILP (position));
962 /* Make the title be the pane title of the first pane. */
963 if (!NILP (title) && menu_items_n_panes >= 0)
964 XVECTOR (menu_items)->contents[MENU_ITEMS_PANE_NAME] = title;
966 keymaps = 1;
968 else
970 /* We were given an old-fashioned menu. */
971 title = Fcar (menu);
972 CHECK_STRING (title);
974 list_of_panes (Fcdr (menu));
976 keymaps = 0;
979 unbind_to (specpdl_count, Qnil);
981 if (NILP (position))
983 discard_menu_items ();
984 UNGCPRO;
985 return Qnil;
988 #ifdef HAVE_MENUS
989 /* Display them in a menu. */
990 BLOCK_INPUT;
992 selection = xmenu_show (f, xpos, ypos, for_click,
993 keymaps, title, &error_name);
994 UNBLOCK_INPUT;
996 discard_menu_items ();
998 UNGCPRO;
999 #endif /* HAVE_MENUS */
1001 if (error_name) error (error_name);
1002 return selection;
1005 #ifdef HAVE_MENUS
1007 DEFUN ("x-popup-dialog", Fx_popup_dialog, Sx_popup_dialog, 2, 3, 0,
1008 doc: /* Pop up a dialog box and return user's selection.
1009 POSITION specifies which frame to use.
1010 This is normally a mouse button event or a window or frame.
1011 If POSITION is t, it means to use the frame the mouse is on.
1012 The dialog box appears in the middle of the specified frame.
1014 CONTENTS specifies the alternatives to display in the dialog box.
1015 It is a list of the form (DIALOG ITEM1 ITEM2...).
1016 Each ITEM is a cons cell (STRING . VALUE).
1017 The return value is VALUE from the chosen item.
1019 An ITEM may also be just a string--that makes a nonselectable item.
1020 An ITEM may also be nil--that means to put all preceding items
1021 on the left of the dialog box and all following items on the right.
1022 \(By default, approximately half appear on each side.)
1024 If HEADER is non-nil, the frame title for the box is "Information",
1025 otherwise it is "Question".
1027 If the user gets rid of the dialog box without making a valid choice,
1028 for instance using the window manager, then this produces a quit and
1029 `x-popup-dialog' does not return. */)
1030 (position, contents, header)
1031 Lisp_Object position, contents, header;
1033 FRAME_PTR f = NULL;
1034 Lisp_Object window;
1036 check_x ();
1038 /* Decode the first argument: find the window or frame to use. */
1039 if (EQ (position, Qt)
1040 || (CONSP (position) && (EQ (XCAR (position), Qmenu_bar)
1041 || EQ (XCAR (position), Qtool_bar))))
1043 #if 0 /* Using the frame the mouse is on may not be right. */
1044 /* Use the mouse's current position. */
1045 FRAME_PTR new_f = SELECTED_FRAME ();
1046 Lisp_Object bar_window;
1047 enum scroll_bar_part part;
1048 unsigned long time;
1049 Lisp_Object x, y;
1051 (*mouse_position_hook) (&new_f, 1, &bar_window, &part, &x, &y, &time);
1053 if (new_f != 0)
1054 XSETFRAME (window, new_f);
1055 else
1056 window = selected_window;
1057 #endif
1058 window = selected_window;
1060 else if (CONSP (position))
1062 Lisp_Object tem;
1063 tem = Fcar (position);
1064 if (CONSP (tem))
1065 window = Fcar (Fcdr (position));
1066 else
1068 tem = Fcar (Fcdr (position)); /* EVENT_START (position) */
1069 window = Fcar (tem); /* POSN_WINDOW (tem) */
1072 else if (WINDOWP (position) || FRAMEP (position))
1073 window = position;
1074 else
1075 window = Qnil;
1077 /* Decode where to put the menu. */
1079 if (FRAMEP (window))
1080 f = XFRAME (window);
1081 else if (WINDOWP (window))
1083 CHECK_LIVE_WINDOW (window);
1084 f = XFRAME (WINDOW_FRAME (XWINDOW (window)));
1086 else
1087 /* ??? Not really clean; should be CHECK_WINDOW_OR_FRAME,
1088 but I don't want to make one now. */
1089 CHECK_WINDOW (window);
1091 #if ! defined (USE_X_TOOLKIT) && ! defined (USE_GTK)
1092 /* Display a menu with these alternatives
1093 in the middle of frame F. */
1095 Lisp_Object x, y, frame, newpos;
1096 XSETFRAME (frame, f);
1097 XSETINT (x, x_pixel_width (f) / 2);
1098 XSETINT (y, x_pixel_height (f) / 2);
1099 newpos = Fcons (Fcons (x, Fcons (y, Qnil)), Fcons (frame, Qnil));
1101 return Fx_popup_menu (newpos,
1102 Fcons (Fcar (contents), Fcons (contents, Qnil)));
1104 #else
1106 Lisp_Object title;
1107 char *error_name;
1108 Lisp_Object selection;
1109 int specpdl_count = SPECPDL_INDEX ();
1111 /* Decode the dialog items from what was specified. */
1112 title = Fcar (contents);
1113 CHECK_STRING (title);
1114 record_unwind_protect (unuse_menu_items, Qnil);
1116 if (NILP (Fcar (Fcdr (contents))))
1117 /* No buttons specified, add an "Ok" button so users can pop down
1118 the dialog. Also, the lesstif/motif version crashes if there are
1119 no buttons. */
1120 contents = Fcons (title, Fcons (Fcons (build_string ("Ok"), Qt), Qnil));
1122 list_of_panes (Fcons (contents, Qnil));
1124 /* Display them in a dialog box. */
1125 BLOCK_INPUT;
1126 selection = xdialog_show (f, 0, title, header, &error_name);
1127 UNBLOCK_INPUT;
1129 unbind_to (specpdl_count, Qnil);
1130 discard_menu_items ();
1132 if (error_name) error (error_name);
1133 return selection;
1135 #endif
1139 #ifndef MSDOS
1141 /* Set menu_items_inuse so no other popup menu or dialog is created. */
1143 void
1144 x_menu_set_in_use (in_use)
1145 int in_use;
1147 menu_items_inuse = in_use ? Qt : Qnil;
1148 popup_activated_flag = in_use;
1151 /* Wait for an X event to arrive or for a timer to expire. */
1153 void
1154 x_menu_wait_for_event (void *data)
1156 extern EMACS_TIME timer_check P_ ((int));
1158 /* Another way to do this is to register a timer callback, that can be
1159 done in GTK and Xt. But we have to do it like this when using only X
1160 anyway, and with callbacks we would have three variants for timer handling
1161 instead of the small ifdefs below. */
1163 while (
1164 #ifdef USE_X_TOOLKIT
1165 ! XtAppPending (Xt_app_con)
1166 #elif defined USE_GTK
1167 ! gtk_events_pending ()
1168 #else
1169 ! XPending ((Display*) data)
1170 #endif
1173 EMACS_TIME next_time = timer_check (1);
1174 long secs = EMACS_SECS (next_time);
1175 long usecs = EMACS_USECS (next_time);
1176 SELECT_TYPE read_fds;
1177 struct x_display_info *dpyinfo;
1178 int n = 0;
1180 FD_ZERO (&read_fds);
1181 for (dpyinfo = x_display_list; dpyinfo; dpyinfo = dpyinfo->next)
1183 int fd = ConnectionNumber (dpyinfo->display);
1184 FD_SET (fd, &read_fds);
1185 if (fd > n) n = fd;
1188 if (secs < 0 || (secs == 0 && usecs == 0))
1190 /* Sometimes timer_check returns -1 (no timers) even if there are
1191 timers. So do a timeout anyway. */
1192 EMACS_SET_SECS (next_time, 1);
1193 EMACS_SET_USECS (next_time, 0);
1196 select (n + 1, &read_fds, (SELECT_TYPE *)0, (SELECT_TYPE *)0, &next_time);
1199 #endif /* ! MSDOS */
1202 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
1204 #ifdef USE_X_TOOLKIT
1206 /* Loop in Xt until the menu pulldown or dialog popup has been
1207 popped down (deactivated). This is used for x-popup-menu
1208 and x-popup-dialog; it is not used for the menu bar.
1210 NOTE: All calls to popup_get_selection should be protected
1211 with BLOCK_INPUT, UNBLOCK_INPUT wrappers. */
1213 static void
1214 popup_get_selection (initial_event, dpyinfo, id, do_timers)
1215 XEvent *initial_event;
1216 struct x_display_info *dpyinfo;
1217 LWLIB_ID id;
1218 int do_timers;
1220 XEvent event;
1222 while (popup_activated_flag)
1224 if (initial_event)
1226 event = *initial_event;
1227 initial_event = 0;
1229 else
1231 if (do_timers) x_menu_wait_for_event (0);
1232 XtAppNextEvent (Xt_app_con, &event);
1235 /* Make sure we don't consider buttons grabbed after menu goes.
1236 And make sure to deactivate for any ButtonRelease,
1237 even if XtDispatchEvent doesn't do that. */
1238 if (event.type == ButtonRelease
1239 && dpyinfo->display == event.xbutton.display)
1241 dpyinfo->grabbed &= ~(1 << event.xbutton.button);
1242 #ifdef USE_MOTIF /* Pretending that the event came from a
1243 Btn1Down seems the only way to convince Motif to
1244 activate its callbacks; setting the XmNmenuPost
1245 isn't working. --marcus@sysc.pdx.edu. */
1246 event.xbutton.button = 1;
1247 /* Motif only pops down menus when no Ctrl, Alt or Mod
1248 key is pressed and the button is released. So reset key state
1249 so Motif thinks this is the case. */
1250 event.xbutton.state = 0;
1251 #endif
1253 /* Pop down on C-g and Escape. */
1254 else if (event.type == KeyPress
1255 && dpyinfo->display == event.xbutton.display)
1257 KeySym keysym = XLookupKeysym (&event.xkey, 0);
1259 if ((keysym == XK_g && (event.xkey.state & ControlMask) != 0)
1260 || keysym == XK_Escape) /* Any escape, ignore modifiers. */
1261 popup_activated_flag = 0;
1264 x_dispatch_event (&event, event.xany.display);
1268 #endif /* USE_X_TOOLKIT */
1270 #ifdef USE_GTK
1271 /* Loop util popup_activated_flag is set to zero in a callback.
1272 Used for popup menus and dialogs. */
1274 static void
1275 popup_widget_loop (do_timers, widget)
1276 int do_timers;
1277 GtkWidget *widget;
1279 ++popup_activated_flag;
1281 /* Process events in the Gtk event loop until done. */
1282 while (popup_activated_flag)
1284 if (do_timers) x_menu_wait_for_event (0);
1285 gtk_main_iteration ();
1288 #endif
1290 /* Activate the menu bar of frame F.
1291 This is called from keyboard.c when it gets the
1292 MENU_BAR_ACTIVATE_EVENT out of the Emacs event queue.
1294 To activate the menu bar, we use the X button-press event
1295 that was saved in saved_menu_event.
1296 That makes the toolkit do its thing.
1298 But first we recompute the menu bar contents (the whole tree).
1300 The reason for saving the button event until here, instead of
1301 passing it to the toolkit right away, is that we can safely
1302 execute Lisp code. */
1304 void
1305 x_activate_menubar (f)
1306 FRAME_PTR f;
1308 if (!f->output_data.x->saved_menu_event->type)
1309 return;
1311 #ifdef USE_GTK
1312 if (! xg_win_to_widget (FRAME_X_DISPLAY (f),
1313 f->output_data.x->saved_menu_event->xany.window))
1314 return;
1315 #endif
1317 set_frame_menubar (f, 0, 1);
1318 BLOCK_INPUT;
1319 #ifdef USE_GTK
1320 XPutBackEvent (f->output_data.x->display_info->display,
1321 f->output_data.x->saved_menu_event);
1322 popup_activated_flag = 1;
1323 #else
1324 XtDispatchEvent (f->output_data.x->saved_menu_event);
1325 #endif
1326 UNBLOCK_INPUT;
1327 #ifdef USE_MOTIF
1328 if (f->output_data.x->saved_menu_event->type == ButtonRelease)
1329 pending_menu_activation = 1;
1330 #endif
1332 /* Ignore this if we get it a second time. */
1333 f->output_data.x->saved_menu_event->type = 0;
1336 /* Detect if a dialog or menu has been posted. */
1339 popup_activated ()
1341 return popup_activated_flag;
1344 /* This callback is invoked when the user selects a menubar cascade
1345 pushbutton, but before the pulldown menu is posted. */
1347 #ifndef USE_GTK
1348 static void
1349 popup_activate_callback (widget, id, client_data)
1350 Widget widget;
1351 LWLIB_ID id;
1352 XtPointer client_data;
1354 popup_activated_flag = 1;
1356 #endif
1358 /* This callback is invoked when a dialog or menu is finished being
1359 used and has been unposted. */
1361 #ifdef USE_GTK
1362 static void
1363 popup_deactivate_callback (widget, client_data)
1364 GtkWidget *widget;
1365 gpointer client_data;
1367 popup_activated_flag = 0;
1369 #else
1370 static void
1371 popup_deactivate_callback (widget, id, client_data)
1372 Widget widget;
1373 LWLIB_ID id;
1374 XtPointer client_data;
1376 popup_activated_flag = 0;
1378 #endif
1381 /* Function that finds the frame for WIDGET and shows the HELP text
1382 for that widget.
1383 F is the frame if known, or NULL if not known. */
1384 static void
1385 show_help_event (f, widget, help)
1386 FRAME_PTR f;
1387 xt_or_gtk_widget widget;
1388 Lisp_Object help;
1390 Lisp_Object frame;
1392 if (f)
1394 XSETFRAME (frame, f);
1395 kbd_buffer_store_help_event (frame, help);
1397 else
1399 #if 0 /* This code doesn't do anything useful. ++kfs */
1400 /* WIDGET is the popup menu. It's parent is the frame's
1401 widget. See which frame that is. */
1402 xt_or_gtk_widget frame_widget = XtParent (widget);
1403 Lisp_Object tail;
1405 for (tail = Vframe_list; GC_CONSP (tail); tail = XCDR (tail))
1407 frame = XCAR (tail);
1408 if (GC_FRAMEP (frame)
1409 && (f = XFRAME (frame),
1410 FRAME_X_P (f) && f->output_data.x->widget == frame_widget))
1411 break;
1413 #endif
1414 show_help_echo (help, Qnil, Qnil, Qnil, 1);
1418 /* Callback called when menu items are highlighted/unhighlighted
1419 while moving the mouse over them. WIDGET is the menu bar or menu
1420 popup widget. ID is its LWLIB_ID. CALL_DATA contains a pointer to
1421 the data structure for the menu item, or null in case of
1422 unhighlighting. */
1424 #ifdef USE_GTK
1425 void
1426 menu_highlight_callback (widget, call_data)
1427 GtkWidget *widget;
1428 gpointer call_data;
1430 xg_menu_item_cb_data *cb_data;
1431 Lisp_Object help;
1433 cb_data = (xg_menu_item_cb_data*) g_object_get_data (G_OBJECT (widget),
1434 XG_ITEM_DATA);
1435 if (! cb_data) return;
1437 help = call_data ? cb_data->help : Qnil;
1439 /* If popup_activated_flag is greater than 1 we are in a popup menu.
1440 Don't show help for them, they won't appear before the
1441 popup is popped down. */
1442 if (popup_activated_flag <= 1)
1443 show_help_event (cb_data->cl_data->f, widget, help);
1445 #else
1446 void
1447 menu_highlight_callback (widget, id, call_data)
1448 Widget widget;
1449 LWLIB_ID id;
1450 void *call_data;
1452 struct frame *f;
1453 Lisp_Object help;
1455 widget_value *wv = (widget_value *) call_data;
1457 help = wv ? wv->help : Qnil;
1459 /* Determine the frame for the help event. */
1460 f = menubar_id_to_frame (id);
1462 show_help_event (f, widget, help);
1464 #endif
1466 /* Find the menu selection and store it in the keyboard buffer.
1467 F is the frame the menu is on.
1468 MENU_BAR_ITEMS_USED is the length of VECTOR.
1469 VECTOR is an array of menu events for the whole menu. */
1471 static void
1472 find_and_call_menu_selection (f, menu_bar_items_used, vector, client_data)
1473 FRAME_PTR f;
1474 int menu_bar_items_used;
1475 Lisp_Object vector;
1476 void *client_data;
1478 Lisp_Object prefix, entry;
1479 Lisp_Object *subprefix_stack;
1480 int submenu_depth = 0;
1481 int i;
1483 entry = Qnil;
1484 subprefix_stack = (Lisp_Object *) alloca (menu_bar_items_used * sizeof (Lisp_Object));
1485 prefix = Qnil;
1486 i = 0;
1488 while (i < menu_bar_items_used)
1490 if (EQ (XVECTOR (vector)->contents[i], Qnil))
1492 subprefix_stack[submenu_depth++] = prefix;
1493 prefix = entry;
1494 i++;
1496 else if (EQ (XVECTOR (vector)->contents[i], Qlambda))
1498 prefix = subprefix_stack[--submenu_depth];
1499 i++;
1501 else if (EQ (XVECTOR (vector)->contents[i], Qt))
1503 prefix = XVECTOR (vector)->contents[i + MENU_ITEMS_PANE_PREFIX];
1504 i += MENU_ITEMS_PANE_LENGTH;
1506 else
1508 entry = XVECTOR (vector)->contents[i + MENU_ITEMS_ITEM_VALUE];
1509 /* The EMACS_INT cast avoids a warning. There's no problem
1510 as long as pointers have enough bits to hold small integers. */
1511 if ((int) (EMACS_INT) client_data == i)
1513 int j;
1514 struct input_event buf;
1515 Lisp_Object frame;
1516 EVENT_INIT (buf);
1518 XSETFRAME (frame, f);
1519 buf.kind = MENU_BAR_EVENT;
1520 buf.frame_or_window = frame;
1521 buf.arg = frame;
1522 kbd_buffer_store_event (&buf);
1524 for (j = 0; j < submenu_depth; j++)
1525 if (!NILP (subprefix_stack[j]))
1527 buf.kind = MENU_BAR_EVENT;
1528 buf.frame_or_window = frame;
1529 buf.arg = subprefix_stack[j];
1530 kbd_buffer_store_event (&buf);
1533 if (!NILP (prefix))
1535 buf.kind = MENU_BAR_EVENT;
1536 buf.frame_or_window = frame;
1537 buf.arg = prefix;
1538 kbd_buffer_store_event (&buf);
1541 buf.kind = MENU_BAR_EVENT;
1542 buf.frame_or_window = frame;
1543 buf.arg = entry;
1544 kbd_buffer_store_event (&buf);
1546 return;
1548 i += MENU_ITEMS_ITEM_LENGTH;
1554 #ifdef USE_GTK
1555 /* Gtk calls callbacks just because we tell it what item should be
1556 selected in a radio group. If this variable is set to a non-zero
1557 value, we are creating menus and don't want callbacks right now.
1559 static int xg_crazy_callback_abort;
1561 /* This callback is called from the menu bar pulldown menu
1562 when the user makes a selection.
1563 Figure out what the user chose
1564 and put the appropriate events into the keyboard buffer. */
1565 static void
1566 menubar_selection_callback (widget, client_data)
1567 GtkWidget *widget;
1568 gpointer client_data;
1570 xg_menu_item_cb_data *cb_data = (xg_menu_item_cb_data*) client_data;
1572 if (xg_crazy_callback_abort)
1573 return;
1575 if (! cb_data || ! cb_data->cl_data || ! cb_data->cl_data->f)
1576 return;
1578 /* For a group of radio buttons, GTK calls the selection callback first
1579 for the item that was active before the selection and then for the one that
1580 is active after the selection. For C-h k this means we get the help on
1581 the deselected item and then the selected item is executed. Prevent that
1582 by ignoring the non-active item. */
1583 if (GTK_IS_RADIO_MENU_ITEM (widget)
1584 && ! gtk_check_menu_item_get_active (GTK_CHECK_MENU_ITEM (widget)))
1585 return;
1587 /* When a menu is popped down, X generates a focus event (i.e. focus
1588 goes back to the frame below the menu). Since GTK buffers events,
1589 we force it out here before the menu selection event. Otherwise
1590 sit-for will exit at once if the focus event follows the menu selection
1591 event. */
1593 BLOCK_INPUT;
1594 while (gtk_events_pending ())
1595 gtk_main_iteration ();
1596 UNBLOCK_INPUT;
1598 find_and_call_menu_selection (cb_data->cl_data->f,
1599 cb_data->cl_data->menu_bar_items_used,
1600 cb_data->cl_data->menu_bar_vector,
1601 cb_data->call_data);
1604 #else /* not USE_GTK */
1606 /* This callback is called from the menu bar pulldown menu
1607 when the user makes a selection.
1608 Figure out what the user chose
1609 and put the appropriate events into the keyboard buffer. */
1610 static void
1611 menubar_selection_callback (widget, id, client_data)
1612 Widget widget;
1613 LWLIB_ID id;
1614 XtPointer client_data;
1616 FRAME_PTR f;
1618 f = menubar_id_to_frame (id);
1619 if (!f)
1620 return;
1621 find_and_call_menu_selection (f, f->menu_bar_items_used,
1622 f->menu_bar_vector, client_data);
1624 #endif /* not USE_GTK */
1626 /* Allocate a widget_value, blocking input. */
1628 widget_value *
1629 xmalloc_widget_value ()
1631 widget_value *value;
1633 BLOCK_INPUT;
1634 value = malloc_widget_value ();
1635 UNBLOCK_INPUT;
1637 return value;
1640 /* This recursively calls free_widget_value on the tree of widgets.
1641 It must free all data that was malloc'ed for these widget_values.
1642 In Emacs, many slots are pointers into the data of Lisp_Strings, and
1643 must be left alone. */
1645 void
1646 free_menubar_widget_value_tree (wv)
1647 widget_value *wv;
1649 if (! wv) return;
1651 wv->name = wv->value = wv->key = (char *) 0xDEADBEEF;
1653 if (wv->contents && (wv->contents != (widget_value*)1))
1655 free_menubar_widget_value_tree (wv->contents);
1656 wv->contents = (widget_value *) 0xDEADBEEF;
1658 if (wv->next)
1660 free_menubar_widget_value_tree (wv->next);
1661 wv->next = (widget_value *) 0xDEADBEEF;
1663 BLOCK_INPUT;
1664 free_widget_value (wv);
1665 UNBLOCK_INPUT;
1668 /* Set up data in menu_items for a menu bar item
1669 whose event type is ITEM_KEY (with string ITEM_NAME)
1670 and whose contents come from the list of keymaps MAPS. */
1672 static int
1673 parse_single_submenu (item_key, item_name, maps)
1674 Lisp_Object item_key, item_name, maps;
1676 Lisp_Object length;
1677 int len;
1678 Lisp_Object *mapvec;
1679 int i;
1680 int top_level_items = 0;
1682 length = Flength (maps);
1683 len = XINT (length);
1685 /* Convert the list MAPS into a vector MAPVEC. */
1686 mapvec = (Lisp_Object *) alloca (len * sizeof (Lisp_Object));
1687 for (i = 0; i < len; i++)
1689 mapvec[i] = Fcar (maps);
1690 maps = Fcdr (maps);
1693 /* Loop over the given keymaps, making a pane for each map.
1694 But don't make a pane that is empty--ignore that map instead. */
1695 for (i = 0; i < len; i++)
1697 if (!KEYMAPP (mapvec[i]))
1699 /* Here we have a command at top level in the menu bar
1700 as opposed to a submenu. */
1701 top_level_items = 1;
1702 push_menu_pane (Qnil, Qnil);
1703 push_menu_item (item_name, Qt, item_key, mapvec[i],
1704 Qnil, Qnil, Qnil, Qnil);
1706 else
1708 Lisp_Object prompt;
1709 prompt = Fkeymap_prompt (mapvec[i]);
1710 single_keymap_panes (mapvec[i],
1711 !NILP (prompt) ? prompt : item_name,
1712 item_key, 0, 10);
1716 return top_level_items;
1719 /* Create a tree of widget_value objects
1720 representing the panes and items
1721 in menu_items starting at index START, up to index END. */
1723 static widget_value *
1724 digest_single_submenu (start, end, top_level_items)
1725 int start, end, top_level_items;
1727 widget_value *wv, *prev_wv, *save_wv, *first_wv;
1728 int i;
1729 int submenu_depth = 0;
1730 widget_value **submenu_stack;
1732 submenu_stack
1733 = (widget_value **) alloca (menu_items_used * sizeof (widget_value *));
1734 wv = xmalloc_widget_value ();
1735 wv->name = "menu";
1736 wv->value = 0;
1737 wv->enabled = 1;
1738 wv->button_type = BUTTON_TYPE_NONE;
1739 wv->help = Qnil;
1740 first_wv = wv;
1741 save_wv = 0;
1742 prev_wv = 0;
1744 /* Loop over all panes and items made by the preceding call
1745 to parse_single_submenu and construct a tree of widget_value objects.
1746 Ignore the panes and items used by previous calls to
1747 digest_single_submenu, even though those are also in menu_items. */
1748 i = start;
1749 while (i < end)
1751 if (EQ (XVECTOR (menu_items)->contents[i], Qnil))
1753 submenu_stack[submenu_depth++] = save_wv;
1754 save_wv = prev_wv;
1755 prev_wv = 0;
1756 i++;
1758 else if (EQ (XVECTOR (menu_items)->contents[i], Qlambda))
1760 prev_wv = save_wv;
1761 save_wv = submenu_stack[--submenu_depth];
1762 i++;
1764 else if (EQ (XVECTOR (menu_items)->contents[i], Qt)
1765 && submenu_depth != 0)
1766 i += MENU_ITEMS_PANE_LENGTH;
1767 /* Ignore a nil in the item list.
1768 It's meaningful only for dialog boxes. */
1769 else if (EQ (XVECTOR (menu_items)->contents[i], Qquote))
1770 i += 1;
1771 else if (EQ (XVECTOR (menu_items)->contents[i], Qt))
1773 /* Create a new pane. */
1774 Lisp_Object pane_name, prefix;
1775 char *pane_string;
1777 pane_name = XVECTOR (menu_items)->contents[i + MENU_ITEMS_PANE_NAME];
1778 prefix = XVECTOR (menu_items)->contents[i + MENU_ITEMS_PANE_PREFIX];
1780 #ifndef HAVE_MULTILINGUAL_MENU
1781 if (STRINGP (pane_name) && STRING_MULTIBYTE (pane_name))
1783 pane_name = ENCODE_MENU_STRING (pane_name);
1784 AREF (menu_items, i + MENU_ITEMS_PANE_NAME) = pane_name;
1786 #endif
1787 pane_string = (NILP (pane_name)
1788 ? "" : (char *) SDATA (pane_name));
1789 /* If there is just one top-level pane, put all its items directly
1790 under the top-level menu. */
1791 if (menu_items_n_panes == 1)
1792 pane_string = "";
1794 /* If the pane has a meaningful name,
1795 make the pane a top-level menu item
1796 with its items as a submenu beneath it. */
1797 if (strcmp (pane_string, ""))
1799 wv = xmalloc_widget_value ();
1800 if (save_wv)
1801 save_wv->next = wv;
1802 else
1803 first_wv->contents = wv;
1804 wv->lname = pane_name;
1805 /* Set value to 1 so update_submenu_strings can handle '@' */
1806 wv->value = (char *)1;
1807 wv->enabled = 1;
1808 wv->button_type = BUTTON_TYPE_NONE;
1809 wv->help = Qnil;
1811 save_wv = wv;
1812 prev_wv = 0;
1813 i += MENU_ITEMS_PANE_LENGTH;
1815 else
1817 /* Create a new item within current pane. */
1818 Lisp_Object item_name, enable, descrip, def, type, selected;
1819 Lisp_Object help;
1821 item_name = AREF (menu_items, i + MENU_ITEMS_ITEM_NAME);
1822 enable = AREF (menu_items, i + MENU_ITEMS_ITEM_ENABLE);
1823 descrip = AREF (menu_items, i + MENU_ITEMS_ITEM_EQUIV_KEY);
1824 def = AREF (menu_items, i + MENU_ITEMS_ITEM_DEFINITION);
1825 type = AREF (menu_items, i + MENU_ITEMS_ITEM_TYPE);
1826 selected = AREF (menu_items, i + MENU_ITEMS_ITEM_SELECTED);
1827 help = AREF (menu_items, i + MENU_ITEMS_ITEM_HELP);
1829 #ifndef HAVE_MULTILINGUAL_MENU
1830 if (STRING_MULTIBYTE (item_name))
1832 item_name = ENCODE_MENU_STRING (item_name);
1833 AREF (menu_items, i + MENU_ITEMS_ITEM_NAME) = item_name;
1836 if (STRINGP (descrip) && STRING_MULTIBYTE (descrip))
1838 descrip = ENCODE_MENU_STRING (descrip);
1839 AREF (menu_items, i + MENU_ITEMS_ITEM_EQUIV_KEY) = descrip;
1841 #endif /* not HAVE_MULTILINGUAL_MENU */
1843 wv = xmalloc_widget_value ();
1844 if (prev_wv)
1845 prev_wv->next = wv;
1846 else
1847 save_wv->contents = wv;
1849 wv->lname = item_name;
1850 if (!NILP (descrip))
1851 wv->lkey = descrip;
1852 wv->value = 0;
1853 /* The EMACS_INT cast avoids a warning. There's no problem
1854 as long as pointers have enough bits to hold small integers. */
1855 wv->call_data = (!NILP (def) ? (void *) (EMACS_INT) i : 0);
1856 wv->enabled = !NILP (enable);
1858 if (NILP (type))
1859 wv->button_type = BUTTON_TYPE_NONE;
1860 else if (EQ (type, QCradio))
1861 wv->button_type = BUTTON_TYPE_RADIO;
1862 else if (EQ (type, QCtoggle))
1863 wv->button_type = BUTTON_TYPE_TOGGLE;
1864 else
1865 abort ();
1867 wv->selected = !NILP (selected);
1868 if (! STRINGP (help))
1869 help = Qnil;
1871 wv->help = help;
1873 prev_wv = wv;
1875 i += MENU_ITEMS_ITEM_LENGTH;
1879 /* If we have just one "menu item"
1880 that was originally a button, return it by itself. */
1881 if (top_level_items && first_wv->contents && first_wv->contents->next == 0)
1883 wv = first_wv->contents;
1884 free_widget_value (first_wv);
1885 return wv;
1888 return first_wv;
1891 /* Walk through the widget_value tree starting at FIRST_WV and update
1892 the char * pointers from the corresponding lisp values.
1893 We do this after building the whole tree, since GC may happen while the
1894 tree is constructed, and small strings are relocated. So we must wait
1895 until no GC can happen before storing pointers into lisp values. */
1896 static void
1897 update_submenu_strings (first_wv)
1898 widget_value *first_wv;
1900 widget_value *wv;
1902 for (wv = first_wv; wv; wv = wv->next)
1904 if (STRINGP (wv->lname))
1906 wv->name = (char *) SDATA (wv->lname);
1908 /* Ignore the @ that means "separate pane".
1909 This is a kludge, but this isn't worth more time. */
1910 if (wv->value == (char *)1)
1912 if (wv->name[0] == '@')
1913 wv->name++;
1914 wv->value = 0;
1918 if (STRINGP (wv->lkey))
1919 wv->key = (char *) SDATA (wv->lkey);
1921 if (wv->contents)
1922 update_submenu_strings (wv->contents);
1927 /* Recompute all the widgets of frame F, when the menu bar has been
1928 changed. Value is non-zero if widgets were updated. */
1930 static int
1931 update_frame_menubar (f)
1932 FRAME_PTR f;
1934 #ifdef USE_GTK
1935 return xg_update_frame_menubar (f);
1936 #else
1937 struct x_output *x = f->output_data.x;
1938 int columns, rows;
1940 if (!x->menubar_widget || XtIsManaged (x->menubar_widget))
1941 return 0;
1943 BLOCK_INPUT;
1944 /* Save the size of the frame because the pane widget doesn't accept
1945 to resize itself. So force it. */
1946 columns = FRAME_COLS (f);
1947 rows = FRAME_LINES (f);
1949 /* Do the voodoo which means "I'm changing lots of things, don't try
1950 to refigure sizes until I'm done." */
1951 lw_refigure_widget (x->column_widget, False);
1953 /* The order in which children are managed is the top to bottom
1954 order in which they are displayed in the paned window. First,
1955 remove the text-area widget. */
1956 XtUnmanageChild (x->edit_widget);
1958 /* Remove the menubar that is there now, and put up the menubar that
1959 should be there. */
1960 XtManageChild (x->menubar_widget);
1961 XtMapWidget (x->menubar_widget);
1962 XtVaSetValues (x->menubar_widget, XtNmappedWhenManaged, 1, NULL);
1964 /* Re-manage the text-area widget, and then thrash the sizes. */
1965 XtManageChild (x->edit_widget);
1966 lw_refigure_widget (x->column_widget, True);
1968 /* Force the pane widget to resize itself with the right values. */
1969 EmacsFrameSetCharSize (x->edit_widget, columns, rows);
1970 UNBLOCK_INPUT;
1971 #endif
1972 return 1;
1975 /* Set the contents of the menubar widgets of frame F.
1976 The argument FIRST_TIME is currently ignored;
1977 it is set the first time this is called, from initialize_frame_menubar. */
1979 void
1980 set_frame_menubar (f, first_time, deep_p)
1981 FRAME_PTR f;
1982 int first_time;
1983 int deep_p;
1985 xt_or_gtk_widget menubar_widget = f->output_data.x->menubar_widget;
1986 #ifdef USE_X_TOOLKIT
1987 LWLIB_ID id;
1988 #endif
1989 Lisp_Object items;
1990 widget_value *wv, *first_wv, *prev_wv = 0;
1991 int i, last_i = 0;
1992 int *submenu_start, *submenu_end;
1993 int *submenu_top_level_items, *submenu_n_panes;
1996 XSETFRAME (Vmenu_updating_frame, f);
1998 #ifdef USE_X_TOOLKIT
1999 if (f->output_data.x->id == 0)
2000 f->output_data.x->id = next_menubar_widget_id++;
2001 id = f->output_data.x->id;
2002 #endif
2004 if (! menubar_widget)
2005 deep_p = 1;
2006 else if (pending_menu_activation && !deep_p)
2007 deep_p = 1;
2008 /* Make the first call for any given frame always go deep. */
2009 else if (!f->output_data.x->saved_menu_event && !deep_p)
2011 deep_p = 1;
2012 f->output_data.x->saved_menu_event = (XEvent*)xmalloc (sizeof (XEvent));
2013 f->output_data.x->saved_menu_event->type = 0;
2016 #ifdef USE_GTK
2017 /* If we have detached menus, we must update deep so detached menus
2018 also gets updated. */
2019 deep_p = deep_p || xg_have_tear_offs ();
2020 #endif
2022 if (deep_p)
2024 /* Make a widget-value tree representing the entire menu trees. */
2026 struct buffer *prev = current_buffer;
2027 Lisp_Object buffer;
2028 int specpdl_count = SPECPDL_INDEX ();
2029 int previous_menu_items_used = f->menu_bar_items_used;
2030 Lisp_Object *previous_items
2031 = (Lisp_Object *) alloca (previous_menu_items_used
2032 * sizeof (Lisp_Object));
2034 /* If we are making a new widget, its contents are empty,
2035 do always reinitialize them. */
2036 if (! menubar_widget)
2037 previous_menu_items_used = 0;
2039 buffer = XWINDOW (FRAME_SELECTED_WINDOW (f))->buffer;
2040 specbind (Qinhibit_quit, Qt);
2041 /* Don't let the debugger step into this code
2042 because it is not reentrant. */
2043 specbind (Qdebug_on_next_call, Qnil);
2045 record_unwind_save_match_data ();
2046 record_unwind_protect (unuse_menu_items, Qnil);
2047 if (NILP (Voverriding_local_map_menu_flag))
2049 specbind (Qoverriding_terminal_local_map, Qnil);
2050 specbind (Qoverriding_local_map, Qnil);
2053 set_buffer_internal_1 (XBUFFER (buffer));
2055 /* Run the Lucid hook. */
2056 safe_run_hooks (Qactivate_menubar_hook);
2058 /* If it has changed current-menubar from previous value,
2059 really recompute the menubar from the value. */
2060 if (! NILP (Vlucid_menu_bar_dirty_flag))
2061 call0 (Qrecompute_lucid_menubar);
2062 safe_run_hooks (Qmenu_bar_update_hook);
2063 FRAME_MENU_BAR_ITEMS (f) = menu_bar_items (FRAME_MENU_BAR_ITEMS (f));
2065 items = FRAME_MENU_BAR_ITEMS (f);
2067 /* Save the frame's previous menu bar contents data. */
2068 if (previous_menu_items_used)
2069 bcopy (XVECTOR (f->menu_bar_vector)->contents, previous_items,
2070 previous_menu_items_used * sizeof (Lisp_Object));
2072 /* Fill in menu_items with the current menu bar contents.
2073 This can evaluate Lisp code. */
2074 menu_items = f->menu_bar_vector;
2075 menu_items_allocated = VECTORP (menu_items) ? ASIZE (menu_items) : 0;
2076 submenu_start = (int *) alloca (XVECTOR (items)->size * sizeof (int *));
2077 submenu_end = (int *) alloca (XVECTOR (items)->size * sizeof (int *));
2078 submenu_n_panes = (int *) alloca (XVECTOR (items)->size * sizeof (int));
2079 submenu_top_level_items
2080 = (int *) alloca (XVECTOR (items)->size * sizeof (int *));
2081 init_menu_items ();
2082 for (i = 0; i < XVECTOR (items)->size; i += 4)
2084 Lisp_Object key, string, maps;
2086 last_i = i;
2088 key = XVECTOR (items)->contents[i];
2089 string = XVECTOR (items)->contents[i + 1];
2090 maps = XVECTOR (items)->contents[i + 2];
2091 if (NILP (string))
2092 break;
2094 submenu_start[i] = menu_items_used;
2096 menu_items_n_panes = 0;
2097 submenu_top_level_items[i]
2098 = parse_single_submenu (key, string, maps);
2099 submenu_n_panes[i] = menu_items_n_panes;
2101 submenu_end[i] = menu_items_used;
2104 finish_menu_items ();
2106 /* Convert menu_items into widget_value trees
2107 to display the menu. This cannot evaluate Lisp code. */
2109 wv = xmalloc_widget_value ();
2110 wv->name = "menubar";
2111 wv->value = 0;
2112 wv->enabled = 1;
2113 wv->button_type = BUTTON_TYPE_NONE;
2114 wv->help = Qnil;
2115 first_wv = wv;
2117 for (i = 0; i < last_i; i += 4)
2119 menu_items_n_panes = submenu_n_panes[i];
2120 wv = digest_single_submenu (submenu_start[i], submenu_end[i],
2121 submenu_top_level_items[i]);
2122 if (prev_wv)
2123 prev_wv->next = wv;
2124 else
2125 first_wv->contents = wv;
2126 /* Don't set wv->name here; GC during the loop might relocate it. */
2127 wv->enabled = 1;
2128 wv->button_type = BUTTON_TYPE_NONE;
2129 prev_wv = wv;
2132 set_buffer_internal_1 (prev);
2133 unbind_to (specpdl_count, Qnil);
2135 /* If there has been no change in the Lisp-level contents
2136 of the menu bar, skip redisplaying it. Just exit. */
2138 for (i = 0; i < previous_menu_items_used; i++)
2139 if (menu_items_used == i
2140 || (!EQ (previous_items[i], XVECTOR (menu_items)->contents[i])))
2141 break;
2142 if (i == menu_items_used && i == previous_menu_items_used && i != 0)
2144 free_menubar_widget_value_tree (first_wv);
2145 discard_menu_items ();
2147 return;
2150 /* Now GC cannot happen during the lifetime of the widget_value,
2151 so it's safe to store data from a Lisp_String. */
2152 wv = first_wv->contents;
2153 for (i = 0; i < XVECTOR (items)->size; i += 4)
2155 Lisp_Object string;
2156 string = XVECTOR (items)->contents[i + 1];
2157 if (NILP (string))
2158 break;
2159 wv->name = (char *) SDATA (string);
2160 update_submenu_strings (wv->contents);
2161 wv = wv->next;
2164 f->menu_bar_vector = menu_items;
2165 f->menu_bar_items_used = menu_items_used;
2166 discard_menu_items ();
2168 else
2170 /* Make a widget-value tree containing
2171 just the top level menu bar strings. */
2173 wv = xmalloc_widget_value ();
2174 wv->name = "menubar";
2175 wv->value = 0;
2176 wv->enabled = 1;
2177 wv->button_type = BUTTON_TYPE_NONE;
2178 wv->help = Qnil;
2179 first_wv = wv;
2181 items = FRAME_MENU_BAR_ITEMS (f);
2182 for (i = 0; i < XVECTOR (items)->size; i += 4)
2184 Lisp_Object string;
2186 string = XVECTOR (items)->contents[i + 1];
2187 if (NILP (string))
2188 break;
2190 wv = xmalloc_widget_value ();
2191 wv->name = (char *) SDATA (string);
2192 wv->value = 0;
2193 wv->enabled = 1;
2194 wv->button_type = BUTTON_TYPE_NONE;
2195 wv->help = Qnil;
2196 /* This prevents lwlib from assuming this
2197 menu item is really supposed to be empty. */
2198 /* The EMACS_INT cast avoids a warning.
2199 This value just has to be different from small integers. */
2200 wv->call_data = (void *) (EMACS_INT) (-1);
2202 if (prev_wv)
2203 prev_wv->next = wv;
2204 else
2205 first_wv->contents = wv;
2206 prev_wv = wv;
2209 /* Forget what we thought we knew about what is in the
2210 detailed contents of the menu bar menus.
2211 Changing the top level always destroys the contents. */
2212 f->menu_bar_items_used = 0;
2215 /* Create or update the menu bar widget. */
2217 BLOCK_INPUT;
2219 #ifdef USE_GTK
2220 xg_crazy_callback_abort = 1;
2221 if (menubar_widget)
2223 /* The fourth arg is DEEP_P, which says to consider the entire
2224 menu trees we supply, rather than just the menu bar item names. */
2225 xg_modify_menubar_widgets (menubar_widget,
2227 first_wv,
2228 deep_p,
2229 G_CALLBACK (menubar_selection_callback),
2230 G_CALLBACK (popup_deactivate_callback),
2231 G_CALLBACK (menu_highlight_callback));
2233 else
2235 GtkWidget *wvbox = f->output_data.x->vbox_widget;
2237 menubar_widget
2238 = xg_create_widget ("menubar", "menubar", f, first_wv,
2239 G_CALLBACK (menubar_selection_callback),
2240 G_CALLBACK (popup_deactivate_callback),
2241 G_CALLBACK (menu_highlight_callback));
2243 f->output_data.x->menubar_widget = menubar_widget;
2247 #else /* not USE_GTK */
2248 if (menubar_widget)
2250 /* Disable resizing (done for Motif!) */
2251 lw_allow_resizing (f->output_data.x->widget, False);
2253 /* The third arg is DEEP_P, which says to consider the entire
2254 menu trees we supply, rather than just the menu bar item names. */
2255 lw_modify_all_widgets (id, first_wv, deep_p);
2257 /* Re-enable the edit widget to resize. */
2258 lw_allow_resizing (f->output_data.x->widget, True);
2260 else
2262 char menuOverride[] = "Ctrl<KeyPress>g: MenuGadgetEscape()";
2263 XtTranslations override = XtParseTranslationTable (menuOverride);
2265 menubar_widget = lw_create_widget ("menubar", "menubar", id, first_wv,
2266 f->output_data.x->column_widget,
2268 popup_activate_callback,
2269 menubar_selection_callback,
2270 popup_deactivate_callback,
2271 menu_highlight_callback);
2272 f->output_data.x->menubar_widget = menubar_widget;
2274 /* Make menu pop down on C-g. */
2275 XtOverrideTranslations (menubar_widget, override);
2279 int menubar_size
2280 = (f->output_data.x->menubar_widget
2281 ? (f->output_data.x->menubar_widget->core.height
2282 + f->output_data.x->menubar_widget->core.border_width)
2283 : 0);
2285 #if 0 /* Experimentally, we now get the right results
2286 for -geometry -0-0 without this. 24 Aug 96, rms. */
2287 #ifdef USE_LUCID
2288 if (FRAME_EXTERNAL_MENU_BAR (f))
2290 Dimension ibw = 0;
2291 XtVaGetValues (f->output_data.x->column_widget,
2292 XtNinternalBorderWidth, &ibw, NULL);
2293 menubar_size += ibw;
2295 #endif /* USE_LUCID */
2296 #endif /* 0 */
2298 f->output_data.x->menubar_height = menubar_size;
2300 #endif /* not USE_GTK */
2302 free_menubar_widget_value_tree (first_wv);
2303 update_frame_menubar (f);
2305 #ifdef USE_GTK
2306 xg_crazy_callback_abort = 0;
2307 #endif
2309 UNBLOCK_INPUT;
2312 /* Called from Fx_create_frame to create the initial menubar of a frame
2313 before it is mapped, so that the window is mapped with the menubar already
2314 there instead of us tacking it on later and thrashing the window after it
2315 is visible. */
2317 void
2318 initialize_frame_menubar (f)
2319 FRAME_PTR f;
2321 /* This function is called before the first chance to redisplay
2322 the frame. It has to be, so the frame will have the right size. */
2323 FRAME_MENU_BAR_ITEMS (f) = menu_bar_items (FRAME_MENU_BAR_ITEMS (f));
2324 set_frame_menubar (f, 1, 1);
2328 /* Get rid of the menu bar of frame F, and free its storage.
2329 This is used when deleting a frame, and when turning off the menu bar.
2330 For GTK this function is in gtkutil.c. */
2332 #ifndef USE_GTK
2333 void
2334 free_frame_menubar (f)
2335 FRAME_PTR f;
2337 Widget menubar_widget;
2339 menubar_widget = f->output_data.x->menubar_widget;
2341 f->output_data.x->menubar_height = 0;
2343 if (menubar_widget)
2345 #ifdef USE_MOTIF
2346 /* Removing the menu bar magically changes the shell widget's x
2347 and y position of (0, 0) which, when the menu bar is turned
2348 on again, leads to pull-down menuss appearing in strange
2349 positions near the upper-left corner of the display. This
2350 happens only with some window managers like twm and ctwm,
2351 but not with other like Motif's mwm or kwm, because the
2352 latter generate ConfigureNotify events when the menu bar
2353 is switched off, which fixes the shell position. */
2354 Position x0, y0, x1, y1;
2355 #endif
2357 BLOCK_INPUT;
2359 #ifdef USE_MOTIF
2360 if (f->output_data.x->widget)
2361 XtVaGetValues (f->output_data.x->widget, XtNx, &x0, XtNy, &y0, NULL);
2362 #endif
2364 lw_destroy_all_widgets ((LWLIB_ID) f->output_data.x->id);
2365 f->output_data.x->menubar_widget = NULL;
2367 #ifdef USE_MOTIF
2368 if (f->output_data.x->widget)
2370 XtVaGetValues (f->output_data.x->widget, XtNx, &x1, XtNy, &y1, NULL);
2371 if (x1 == 0 && y1 == 0)
2372 XtVaSetValues (f->output_data.x->widget, XtNx, x0, XtNy, y0, NULL);
2374 #endif
2376 UNBLOCK_INPUT;
2379 #endif /* not USE_GTK */
2381 #endif /* USE_X_TOOLKIT || USE_GTK */
2383 /* xmenu_show actually displays a menu using the panes and items in menu_items
2384 and returns the value selected from it.
2385 There are two versions of xmenu_show, one for Xt and one for Xlib.
2386 Both assume input is blocked by the caller. */
2388 /* F is the frame the menu is for.
2389 X and Y are the frame-relative specified position,
2390 relative to the inside upper left corner of the frame F.
2391 FOR_CLICK is nonzero if this menu was invoked for a mouse click.
2392 KEYMAPS is 1 if this menu was specified with keymaps;
2393 in that case, we return a list containing the chosen item's value
2394 and perhaps also the pane's prefix.
2395 TITLE is the specified menu title.
2396 ERROR is a place to store an error message string in case of failure.
2397 (We return nil on failure, but the value doesn't actually matter.) */
2399 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
2401 /* The item selected in the popup menu. */
2402 static Lisp_Object *volatile menu_item_selection;
2404 #ifdef USE_GTK
2406 /* Used when position a popup menu. See menu_position_func and
2407 create_and_show_popup_menu below. */
2408 struct next_popup_x_y
2410 FRAME_PTR f;
2411 int x;
2412 int y;
2415 /* The menu position function to use if we are not putting a popup
2416 menu where the pointer is.
2417 MENU is the menu to pop up.
2418 X and Y shall on exit contain x/y where the menu shall pop up.
2419 PUSH_IN is not documented in the GTK manual.
2420 USER_DATA is any data passed in when calling gtk_menu_popup.
2421 Here it points to a struct next_popup_x_y where the coordinates
2422 to store in *X and *Y are as well as the frame for the popup.
2424 Here only X and Y are used. */
2425 static void
2426 menu_position_func (menu, x, y, push_in, user_data)
2427 GtkMenu *menu;
2428 gint *x;
2429 gint *y;
2430 gboolean *push_in;
2431 gpointer user_data;
2433 struct next_popup_x_y* data = (struct next_popup_x_y*)user_data;
2434 GtkRequisition req;
2435 int disp_width = FRAME_X_DISPLAY_INFO (data->f)->width;
2436 int disp_height = FRAME_X_DISPLAY_INFO (data->f)->height;
2438 *x = data->x;
2439 *y = data->y;
2441 /* Check if there is room for the menu. If not, adjust x/y so that
2442 the menu is fully visible. */
2443 gtk_widget_size_request (GTK_WIDGET (menu), &req);
2444 if (data->x + req.width > disp_width)
2445 *x -= data->x + req.width - disp_width;
2446 if (data->y + req.height > disp_height)
2447 *y -= data->y + req.height - disp_height;
2450 static void
2451 popup_selection_callback (widget, client_data)
2452 GtkWidget *widget;
2453 gpointer client_data;
2455 xg_menu_item_cb_data *cb_data = (xg_menu_item_cb_data*) client_data;
2457 if (xg_crazy_callback_abort) return;
2458 if (cb_data) menu_item_selection = (Lisp_Object *) cb_data->call_data;
2461 static Lisp_Object
2462 pop_down_menu (arg)
2463 Lisp_Object arg;
2465 struct Lisp_Save_Value *p = XSAVE_VALUE (arg);
2467 popup_activated_flag = 0;
2468 BLOCK_INPUT;
2469 gtk_widget_destroy (GTK_WIDGET (p->pointer));
2470 UNBLOCK_INPUT;
2471 return Qnil;
2474 /* Pop up the menu for frame F defined by FIRST_WV at X/Y and loop until the
2475 menu pops down.
2476 menu_item_selection will be set to the selection. */
2477 static void
2478 create_and_show_popup_menu (f, first_wv, x, y, for_click)
2479 FRAME_PTR f;
2480 widget_value *first_wv;
2481 int x;
2482 int y;
2483 int for_click;
2485 int i;
2486 GtkWidget *menu;
2487 GtkMenuPositionFunc pos_func = 0; /* Pop up at pointer. */
2488 struct next_popup_x_y popup_x_y;
2489 int specpdl_count = SPECPDL_INDEX ();
2491 xg_crazy_callback_abort = 1;
2492 menu = xg_create_widget ("popup", first_wv->name, f, first_wv,
2493 G_CALLBACK (popup_selection_callback),
2494 G_CALLBACK (popup_deactivate_callback),
2495 G_CALLBACK (menu_highlight_callback));
2496 xg_crazy_callback_abort = 0;
2498 if (! for_click)
2500 /* Not invoked by a click. pop up at x/y. */
2501 pos_func = menu_position_func;
2503 /* Adjust coordinates to be root-window-relative. */
2504 x += f->left_pos + FRAME_OUTER_TO_INNER_DIFF_X (f);
2505 y += f->top_pos + FRAME_OUTER_TO_INNER_DIFF_Y (f);
2507 popup_x_y.x = x;
2508 popup_x_y.y = y;
2509 popup_x_y.f = f;
2511 i = 0; /* gtk_menu_popup needs this to be 0 for a non-button popup. */
2513 else
2515 for (i = 0; i < 5; i++)
2516 if (FRAME_X_DISPLAY_INFO (f)->grabbed & (1 << i))
2517 break;
2520 /* Display the menu. */
2521 gtk_widget_show_all (menu);
2522 gtk_menu_popup (GTK_MENU (menu), 0, 0, pos_func, &popup_x_y, i, 0);
2524 record_unwind_protect (pop_down_menu, make_save_value (menu, 0));
2526 if (GTK_WIDGET_MAPPED (menu))
2528 /* Set this to one. popup_widget_loop increases it by one, so it becomes
2529 two. show_help_echo uses this to detect popup menus. */
2530 popup_activated_flag = 1;
2531 /* Process events that apply to the menu. */
2532 popup_widget_loop (1, menu);
2535 unbind_to (specpdl_count, Qnil);
2537 /* Must reset this manually because the button release event is not passed
2538 to Emacs event loop. */
2539 FRAME_X_DISPLAY_INFO (f)->grabbed = 0;
2542 #else /* not USE_GTK */
2544 /* We need a unique id for each widget handled by the Lucid Widget
2545 library.
2547 For the main windows, and popup menus, we use this counter,
2548 which we increment each time after use. This starts from 1<<16.
2550 For menu bars, we use numbers starting at 0, counted in
2551 next_menubar_widget_id. */
2552 LWLIB_ID widget_id_tick;
2554 static void
2555 popup_selection_callback (widget, id, client_data)
2556 Widget widget;
2557 LWLIB_ID id;
2558 XtPointer client_data;
2560 menu_item_selection = (Lisp_Object *) client_data;
2563 /* ARG is the LWLIB ID of the dialog box, represented
2564 as a Lisp object as (HIGHPART . LOWPART). */
2566 static Lisp_Object
2567 pop_down_menu (arg)
2568 Lisp_Object arg;
2570 LWLIB_ID id = (XINT (XCAR (arg)) << 4 * sizeof (LWLIB_ID)
2571 | XINT (XCDR (arg)));
2573 BLOCK_INPUT;
2574 lw_destroy_all_widgets (id);
2575 UNBLOCK_INPUT;
2576 popup_activated_flag = 0;
2578 return Qnil;
2581 /* Pop up the menu for frame F defined by FIRST_WV at X/Y and loop until the
2582 menu pops down.
2583 menu_item_selection will be set to the selection. */
2584 static void
2585 create_and_show_popup_menu (f, first_wv, x, y, for_click)
2586 FRAME_PTR f;
2587 widget_value *first_wv;
2588 int x;
2589 int y;
2590 int for_click;
2592 int i;
2593 Arg av[2];
2594 int ac = 0;
2595 XButtonPressedEvent dummy;
2596 LWLIB_ID menu_id;
2597 Widget menu;
2599 menu_id = widget_id_tick++;
2600 menu = lw_create_widget ("popup", first_wv->name, menu_id, first_wv,
2601 f->output_data.x->widget, 1, 0,
2602 popup_selection_callback,
2603 popup_deactivate_callback,
2604 menu_highlight_callback);
2606 dummy.type = ButtonPress;
2607 dummy.serial = 0;
2608 dummy.send_event = 0;
2609 dummy.display = FRAME_X_DISPLAY (f);
2610 dummy.time = CurrentTime;
2611 dummy.root = FRAME_X_DISPLAY_INFO (f)->root_window;
2612 dummy.window = dummy.root;
2613 dummy.subwindow = dummy.root;
2614 dummy.x = x;
2615 dummy.y = y;
2617 /* Adjust coordinates to be root-window-relative. */
2618 x += f->left_pos + FRAME_OUTER_TO_INNER_DIFF_X (f);
2619 y += f->top_pos + FRAME_OUTER_TO_INNER_DIFF_Y (f);
2621 dummy.x_root = x;
2622 dummy.y_root = y;
2624 dummy.state = 0;
2625 dummy.button = 0;
2626 for (i = 0; i < 5; i++)
2627 if (FRAME_X_DISPLAY_INFO (f)->grabbed & (1 << i))
2628 dummy.button = i;
2630 /* Don't allow any geometry request from the user. */
2631 XtSetArg (av[ac], XtNgeometry, 0); ac++;
2632 XtSetValues (menu, av, ac);
2634 /* Display the menu. */
2635 lw_popup_menu (menu, (XEvent *) &dummy);
2636 popup_activated_flag = 1;
2639 int fact = 4 * sizeof (LWLIB_ID);
2640 int specpdl_count = SPECPDL_INDEX ();
2641 record_unwind_protect (pop_down_menu,
2642 Fcons (make_number (menu_id >> (fact)),
2643 make_number (menu_id & ~(-1 << (fact)))));
2645 /* Process events that apply to the menu. */
2646 popup_get_selection ((XEvent *) 0, FRAME_X_DISPLAY_INFO (f), menu_id, 1);
2648 unbind_to (specpdl_count, Qnil);
2652 #endif /* not USE_GTK */
2654 static Lisp_Object
2655 xmenu_show (f, x, y, for_click, keymaps, title, error)
2656 FRAME_PTR f;
2657 int x;
2658 int y;
2659 int for_click;
2660 int keymaps;
2661 Lisp_Object title;
2662 char **error;
2664 int i;
2665 widget_value *wv, *save_wv = 0, *first_wv = 0, *prev_wv = 0;
2666 widget_value **submenu_stack
2667 = (widget_value **) alloca (menu_items_used * sizeof (widget_value *));
2668 Lisp_Object *subprefix_stack
2669 = (Lisp_Object *) alloca (menu_items_used * sizeof (Lisp_Object));
2670 int submenu_depth = 0;
2672 int first_pane;
2674 *error = NULL;
2676 if (menu_items_used <= MENU_ITEMS_PANE_LENGTH)
2678 *error = "Empty menu";
2679 return Qnil;
2682 /* Create a tree of widget_value objects
2683 representing the panes and their items. */
2684 wv = xmalloc_widget_value ();
2685 wv->name = "menu";
2686 wv->value = 0;
2687 wv->enabled = 1;
2688 wv->button_type = BUTTON_TYPE_NONE;
2689 wv->help =Qnil;
2690 first_wv = wv;
2691 first_pane = 1;
2693 /* Loop over all panes and items, filling in the tree. */
2694 i = 0;
2695 while (i < menu_items_used)
2697 if (EQ (XVECTOR (menu_items)->contents[i], Qnil))
2699 submenu_stack[submenu_depth++] = save_wv;
2700 save_wv = prev_wv;
2701 prev_wv = 0;
2702 first_pane = 1;
2703 i++;
2705 else if (EQ (XVECTOR (menu_items)->contents[i], Qlambda))
2707 prev_wv = save_wv;
2708 save_wv = submenu_stack[--submenu_depth];
2709 first_pane = 0;
2710 i++;
2712 else if (EQ (XVECTOR (menu_items)->contents[i], Qt)
2713 && submenu_depth != 0)
2714 i += MENU_ITEMS_PANE_LENGTH;
2715 /* Ignore a nil in the item list.
2716 It's meaningful only for dialog boxes. */
2717 else if (EQ (XVECTOR (menu_items)->contents[i], Qquote))
2718 i += 1;
2719 else if (EQ (XVECTOR (menu_items)->contents[i], Qt))
2721 /* Create a new pane. */
2722 Lisp_Object pane_name, prefix;
2723 char *pane_string;
2725 pane_name = AREF (menu_items, i + MENU_ITEMS_PANE_NAME);
2726 prefix = AREF (menu_items, i + MENU_ITEMS_PANE_PREFIX);
2728 #ifndef HAVE_MULTILINGUAL_MENU
2729 if (STRINGP (pane_name) && STRING_MULTIBYTE (pane_name))
2731 pane_name = ENCODE_MENU_STRING (pane_name);
2732 AREF (menu_items, i + MENU_ITEMS_PANE_NAME) = pane_name;
2734 #endif
2735 pane_string = (NILP (pane_name)
2736 ? "" : (char *) SDATA (pane_name));
2737 /* If there is just one top-level pane, put all its items directly
2738 under the top-level menu. */
2739 if (menu_items_n_panes == 1)
2740 pane_string = "";
2742 /* If the pane has a meaningful name,
2743 make the pane a top-level menu item
2744 with its items as a submenu beneath it. */
2745 if (!keymaps && strcmp (pane_string, ""))
2747 wv = xmalloc_widget_value ();
2748 if (save_wv)
2749 save_wv->next = wv;
2750 else
2751 first_wv->contents = wv;
2752 wv->name = pane_string;
2753 if (keymaps && !NILP (prefix))
2754 wv->name++;
2755 wv->value = 0;
2756 wv->enabled = 1;
2757 wv->button_type = BUTTON_TYPE_NONE;
2758 wv->help = Qnil;
2759 save_wv = wv;
2760 prev_wv = 0;
2762 else if (first_pane)
2764 save_wv = wv;
2765 prev_wv = 0;
2767 first_pane = 0;
2768 i += MENU_ITEMS_PANE_LENGTH;
2770 else
2772 /* Create a new item within current pane. */
2773 Lisp_Object item_name, enable, descrip, def, type, selected, help;
2774 item_name = AREF (menu_items, i + MENU_ITEMS_ITEM_NAME);
2775 enable = AREF (menu_items, i + MENU_ITEMS_ITEM_ENABLE);
2776 descrip = AREF (menu_items, i + MENU_ITEMS_ITEM_EQUIV_KEY);
2777 def = AREF (menu_items, i + MENU_ITEMS_ITEM_DEFINITION);
2778 type = AREF (menu_items, i + MENU_ITEMS_ITEM_TYPE);
2779 selected = AREF (menu_items, i + MENU_ITEMS_ITEM_SELECTED);
2780 help = AREF (menu_items, i + MENU_ITEMS_ITEM_HELP);
2782 #ifndef HAVE_MULTILINGUAL_MENU
2783 if (STRINGP (item_name) && STRING_MULTIBYTE (item_name))
2785 item_name = ENCODE_MENU_STRING (item_name);
2786 AREF (menu_items, i + MENU_ITEMS_ITEM_NAME) = item_name;
2789 if (STRINGP (descrip) && STRING_MULTIBYTE (descrip))
2791 descrip = ENCODE_MENU_STRING (descrip);
2792 AREF (menu_items, i + MENU_ITEMS_ITEM_EQUIV_KEY) = descrip;
2794 #endif /* not HAVE_MULTILINGUAL_MENU */
2796 wv = xmalloc_widget_value ();
2797 if (prev_wv)
2798 prev_wv->next = wv;
2799 else
2800 save_wv->contents = wv;
2801 wv->name = (char *) SDATA (item_name);
2802 if (!NILP (descrip))
2803 wv->key = (char *) SDATA (descrip);
2804 wv->value = 0;
2805 /* If this item has a null value,
2806 make the call_data null so that it won't display a box
2807 when the mouse is on it. */
2808 wv->call_data
2809 = (!NILP (def) ? (void *) &XVECTOR (menu_items)->contents[i] : 0);
2810 wv->enabled = !NILP (enable);
2812 if (NILP (type))
2813 wv->button_type = BUTTON_TYPE_NONE;
2814 else if (EQ (type, QCtoggle))
2815 wv->button_type = BUTTON_TYPE_TOGGLE;
2816 else if (EQ (type, QCradio))
2817 wv->button_type = BUTTON_TYPE_RADIO;
2818 else
2819 abort ();
2821 wv->selected = !NILP (selected);
2823 if (! STRINGP (help))
2824 help = Qnil;
2826 wv->help = help;
2828 prev_wv = wv;
2830 i += MENU_ITEMS_ITEM_LENGTH;
2834 /* Deal with the title, if it is non-nil. */
2835 if (!NILP (title))
2837 widget_value *wv_title = xmalloc_widget_value ();
2838 widget_value *wv_sep1 = xmalloc_widget_value ();
2839 widget_value *wv_sep2 = xmalloc_widget_value ();
2841 wv_sep2->name = "--";
2842 wv_sep2->next = first_wv->contents;
2843 wv_sep2->help = Qnil;
2845 wv_sep1->name = "--";
2846 wv_sep1->next = wv_sep2;
2847 wv_sep1->help = Qnil;
2849 #ifndef HAVE_MULTILINGUAL_MENU
2850 if (STRING_MULTIBYTE (title))
2851 title = ENCODE_MENU_STRING (title);
2852 #endif
2854 wv_title->name = (char *) SDATA (title);
2855 wv_title->enabled = TRUE;
2856 wv_title->button_type = BUTTON_TYPE_NONE;
2857 wv_title->next = wv_sep1;
2858 wv_title->help = Qnil;
2859 first_wv->contents = wv_title;
2862 /* No selection has been chosen yet. */
2863 menu_item_selection = 0;
2865 /* Actually create and show the menu until popped down. */
2866 create_and_show_popup_menu (f, first_wv, x, y, for_click);
2868 /* Free the widget_value objects we used to specify the contents. */
2869 free_menubar_widget_value_tree (first_wv);
2871 /* Find the selected item, and its pane, to return
2872 the proper value. */
2873 if (menu_item_selection != 0)
2875 Lisp_Object prefix, entry;
2877 prefix = entry = Qnil;
2878 i = 0;
2879 while (i < menu_items_used)
2881 if (EQ (XVECTOR (menu_items)->contents[i], Qnil))
2883 subprefix_stack[submenu_depth++] = prefix;
2884 prefix = entry;
2885 i++;
2887 else if (EQ (XVECTOR (menu_items)->contents[i], Qlambda))
2889 prefix = subprefix_stack[--submenu_depth];
2890 i++;
2892 else if (EQ (XVECTOR (menu_items)->contents[i], Qt))
2894 prefix
2895 = XVECTOR (menu_items)->contents[i + MENU_ITEMS_PANE_PREFIX];
2896 i += MENU_ITEMS_PANE_LENGTH;
2898 /* Ignore a nil in the item list.
2899 It's meaningful only for dialog boxes. */
2900 else if (EQ (XVECTOR (menu_items)->contents[i], Qquote))
2901 i += 1;
2902 else
2904 entry
2905 = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_VALUE];
2906 if (menu_item_selection == &XVECTOR (menu_items)->contents[i])
2908 if (keymaps != 0)
2910 int j;
2912 entry = Fcons (entry, Qnil);
2913 if (!NILP (prefix))
2914 entry = Fcons (prefix, entry);
2915 for (j = submenu_depth - 1; j >= 0; j--)
2916 if (!NILP (subprefix_stack[j]))
2917 entry = Fcons (subprefix_stack[j], entry);
2919 return entry;
2921 i += MENU_ITEMS_ITEM_LENGTH;
2925 else if (!for_click)
2926 /* Make "Cancel" equivalent to C-g. */
2927 Fsignal (Qquit, Qnil);
2929 return Qnil;
2932 #ifdef USE_GTK
2933 static void
2934 dialog_selection_callback (widget, client_data)
2935 GtkWidget *widget;
2936 gpointer client_data;
2938 /* The EMACS_INT cast avoids a warning. There's no problem
2939 as long as pointers have enough bits to hold small integers. */
2940 if ((int) (EMACS_INT) client_data != -1)
2941 menu_item_selection = (Lisp_Object *) client_data;
2943 popup_activated_flag = 0;
2946 /* Pop up the dialog for frame F defined by FIRST_WV and loop until the
2947 dialog pops down.
2948 menu_item_selection will be set to the selection. */
2949 static void
2950 create_and_show_dialog (f, first_wv)
2951 FRAME_PTR f;
2952 widget_value *first_wv;
2954 GtkWidget *menu;
2956 menu = xg_create_widget ("dialog", first_wv->name, f, first_wv,
2957 G_CALLBACK (dialog_selection_callback),
2958 G_CALLBACK (popup_deactivate_callback),
2961 if (menu)
2963 int specpdl_count = SPECPDL_INDEX ();
2964 record_unwind_protect (pop_down_menu, make_save_value (menu, 0));
2966 /* Display the menu. */
2967 gtk_widget_show_all (menu);
2969 /* Process events that apply to the menu. */
2970 popup_widget_loop (1, menu);
2972 unbind_to (specpdl_count, Qnil);
2976 #else /* not USE_GTK */
2977 static void
2978 dialog_selection_callback (widget, id, client_data)
2979 Widget widget;
2980 LWLIB_ID id;
2981 XtPointer client_data;
2983 /* The EMACS_INT cast avoids a warning. There's no problem
2984 as long as pointers have enough bits to hold small integers. */
2985 if ((int) (EMACS_INT) client_data != -1)
2986 menu_item_selection = (Lisp_Object *) client_data;
2988 BLOCK_INPUT;
2989 lw_destroy_all_widgets (id);
2990 UNBLOCK_INPUT;
2991 popup_activated_flag = 0;
2995 /* Pop up the dialog for frame F defined by FIRST_WV and loop until the
2996 dialog pops down.
2997 menu_item_selection will be set to the selection. */
2998 static void
2999 create_and_show_dialog (f, first_wv)
3000 FRAME_PTR f;
3001 widget_value *first_wv;
3003 LWLIB_ID dialog_id;
3005 dialog_id = widget_id_tick++;
3006 lw_create_widget (first_wv->name, "dialog", dialog_id, first_wv,
3007 f->output_data.x->widget, 1, 0,
3008 dialog_selection_callback, 0, 0);
3009 lw_modify_all_widgets (dialog_id, first_wv->contents, True);
3011 /* Display the dialog box. */
3012 lw_pop_up_all_widgets (dialog_id);
3013 popup_activated_flag = 1;
3015 /* Process events that apply to the dialog box.
3016 Also handle timers. */
3018 int count = SPECPDL_INDEX ();
3019 int fact = 4 * sizeof (LWLIB_ID);
3021 /* xdialog_show_unwind is responsible for popping the dialog box down. */
3022 record_unwind_protect (pop_down_menu,
3023 Fcons (make_number (dialog_id >> (fact)),
3024 make_number (dialog_id & ~(-1 << (fact)))));
3026 popup_get_selection ((XEvent *) 0, FRAME_X_DISPLAY_INFO (f),
3027 dialog_id, 1);
3029 unbind_to (count, Qnil);
3033 #endif /* not USE_GTK */
3035 static char * button_names [] = {
3036 "button1", "button2", "button3", "button4", "button5",
3037 "button6", "button7", "button8", "button9", "button10" };
3039 static Lisp_Object
3040 xdialog_show (f, keymaps, title, header, error_name)
3041 FRAME_PTR f;
3042 int keymaps;
3043 Lisp_Object title, header;
3044 char **error_name;
3046 int i, nb_buttons=0;
3047 char dialog_name[6];
3049 widget_value *wv, *first_wv = 0, *prev_wv = 0;
3051 /* Number of elements seen so far, before boundary. */
3052 int left_count = 0;
3053 /* 1 means we've seen the boundary between left-hand elts and right-hand. */
3054 int boundary_seen = 0;
3056 *error_name = NULL;
3058 if (menu_items_n_panes > 1)
3060 *error_name = "Multiple panes in dialog box";
3061 return Qnil;
3064 /* Create a tree of widget_value objects
3065 representing the text label and buttons. */
3067 Lisp_Object pane_name, prefix;
3068 char *pane_string;
3069 pane_name = XVECTOR (menu_items)->contents[MENU_ITEMS_PANE_NAME];
3070 prefix = XVECTOR (menu_items)->contents[MENU_ITEMS_PANE_PREFIX];
3071 pane_string = (NILP (pane_name)
3072 ? "" : (char *) SDATA (pane_name));
3073 prev_wv = xmalloc_widget_value ();
3074 prev_wv->value = pane_string;
3075 if (keymaps && !NILP (prefix))
3076 prev_wv->name++;
3077 prev_wv->enabled = 1;
3078 prev_wv->name = "message";
3079 prev_wv->help = Qnil;
3080 first_wv = prev_wv;
3082 /* Loop over all panes and items, filling in the tree. */
3083 i = MENU_ITEMS_PANE_LENGTH;
3084 while (i < menu_items_used)
3087 /* Create a new item within current pane. */
3088 Lisp_Object item_name, enable, descrip;
3089 item_name = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_NAME];
3090 enable = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_ENABLE];
3091 descrip
3092 = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_EQUIV_KEY];
3094 if (NILP (item_name))
3096 free_menubar_widget_value_tree (first_wv);
3097 *error_name = "Submenu in dialog items";
3098 return Qnil;
3100 if (EQ (item_name, Qquote))
3102 /* This is the boundary between left-side elts
3103 and right-side elts. Stop incrementing right_count. */
3104 boundary_seen = 1;
3105 i++;
3106 continue;
3108 if (nb_buttons >= 9)
3110 free_menubar_widget_value_tree (first_wv);
3111 *error_name = "Too many dialog items";
3112 return Qnil;
3115 wv = xmalloc_widget_value ();
3116 prev_wv->next = wv;
3117 wv->name = (char *) button_names[nb_buttons];
3118 if (!NILP (descrip))
3119 wv->key = (char *) SDATA (descrip);
3120 wv->value = (char *) SDATA (item_name);
3121 wv->call_data = (void *) &XVECTOR (menu_items)->contents[i];
3122 wv->enabled = !NILP (enable);
3123 wv->help = Qnil;
3124 prev_wv = wv;
3126 if (! boundary_seen)
3127 left_count++;
3129 nb_buttons++;
3130 i += MENU_ITEMS_ITEM_LENGTH;
3133 /* If the boundary was not specified,
3134 by default put half on the left and half on the right. */
3135 if (! boundary_seen)
3136 left_count = nb_buttons - nb_buttons / 2;
3138 wv = xmalloc_widget_value ();
3139 wv->name = dialog_name;
3140 wv->help = Qnil;
3142 /* Frame title: 'Q' = Question, 'I' = Information.
3143 Can also have 'E' = Error if, one day, we want
3144 a popup for errors. */
3145 if (NILP(header))
3146 dialog_name[0] = 'Q';
3147 else
3148 dialog_name[0] = 'I';
3150 /* Dialog boxes use a really stupid name encoding
3151 which specifies how many buttons to use
3152 and how many buttons are on the right. */
3153 dialog_name[1] = '0' + nb_buttons;
3154 dialog_name[2] = 'B';
3155 dialog_name[3] = 'R';
3156 /* Number of buttons to put on the right. */
3157 dialog_name[4] = '0' + nb_buttons - left_count;
3158 dialog_name[5] = 0;
3159 wv->contents = first_wv;
3160 first_wv = wv;
3163 /* No selection has been chosen yet. */
3164 menu_item_selection = 0;
3166 /* Actually create and show the dialog. */
3167 create_and_show_dialog (f, first_wv);
3169 /* Free the widget_value objects we used to specify the contents. */
3170 free_menubar_widget_value_tree (first_wv);
3172 /* Find the selected item, and its pane, to return
3173 the proper value. */
3174 if (menu_item_selection != 0)
3176 Lisp_Object prefix;
3178 prefix = Qnil;
3179 i = 0;
3180 while (i < menu_items_used)
3182 Lisp_Object entry;
3184 if (EQ (XVECTOR (menu_items)->contents[i], Qt))
3186 prefix
3187 = XVECTOR (menu_items)->contents[i + MENU_ITEMS_PANE_PREFIX];
3188 i += MENU_ITEMS_PANE_LENGTH;
3190 else if (EQ (XVECTOR (menu_items)->contents[i], Qquote))
3192 /* This is the boundary between left-side elts and
3193 right-side elts. */
3194 ++i;
3196 else
3198 entry
3199 = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_VALUE];
3200 if (menu_item_selection == &XVECTOR (menu_items)->contents[i])
3202 if (keymaps != 0)
3204 entry = Fcons (entry, Qnil);
3205 if (!NILP (prefix))
3206 entry = Fcons (prefix, entry);
3208 return entry;
3210 i += MENU_ITEMS_ITEM_LENGTH;
3214 else
3215 /* Make "Cancel" equivalent to C-g. */
3216 Fsignal (Qquit, Qnil);
3218 return Qnil;
3221 #else /* not USE_X_TOOLKIT && not USE_GTK */
3223 /* The frame of the last activated non-toolkit menu bar.
3224 Used to generate menu help events. */
3226 static struct frame *menu_help_frame;
3229 /* Show help HELP_STRING, or clear help if HELP_STRING is null.
3231 PANE is the pane number, and ITEM is the menu item number in
3232 the menu (currently not used).
3234 This cannot be done with generating a HELP_EVENT because
3235 XMenuActivate contains a loop that doesn't let Emacs process
3236 keyboard events. */
3238 static void
3239 menu_help_callback (help_string, pane, item)
3240 char *help_string;
3241 int pane, item;
3243 extern Lisp_Object Qmenu_item;
3244 Lisp_Object *first_item;
3245 Lisp_Object pane_name;
3246 Lisp_Object menu_object;
3248 first_item = XVECTOR (menu_items)->contents;
3249 if (EQ (first_item[0], Qt))
3250 pane_name = first_item[MENU_ITEMS_PANE_NAME];
3251 else if (EQ (first_item[0], Qquote))
3252 /* This shouldn't happen, see xmenu_show. */
3253 pane_name = empty_string;
3254 else
3255 pane_name = first_item[MENU_ITEMS_ITEM_NAME];
3257 /* (menu-item MENU-NAME PANE-NUMBER) */
3258 menu_object = Fcons (Qmenu_item,
3259 Fcons (pane_name,
3260 Fcons (make_number (pane), Qnil)));
3261 show_help_echo (help_string ? build_string (help_string) : Qnil,
3262 Qnil, menu_object, make_number (item), 1);
3265 static Lisp_Object
3266 pop_down_menu (arg)
3267 Lisp_Object arg;
3269 struct Lisp_Save_Value *p1 = XSAVE_VALUE (Fcar (arg));
3270 struct Lisp_Save_Value *p2 = XSAVE_VALUE (Fcdr (arg));
3272 FRAME_PTR f = p1->pointer;
3273 XMenu *menu = p2->pointer;
3275 BLOCK_INPUT;
3276 #ifndef MSDOS
3277 XUngrabPointer (FRAME_X_DISPLAY (f), CurrentTime);
3278 XUngrabKeyboard (FRAME_X_DISPLAY (f), CurrentTime);
3279 #endif
3280 XMenuDestroy (FRAME_X_DISPLAY (f), menu);
3282 #ifdef HAVE_X_WINDOWS
3283 /* Assume the mouse has moved out of the X window.
3284 If it has actually moved in, we will get an EnterNotify. */
3285 x_mouse_leave (FRAME_X_DISPLAY_INFO (f));
3287 /* State that no mouse buttons are now held.
3288 (The oldXMenu code doesn't track this info for us.)
3289 That is not necessarily true, but the fiction leads to reasonable
3290 results, and it is a pain to ask which are actually held now. */
3291 FRAME_X_DISPLAY_INFO (f)->grabbed = 0;
3293 #endif /* HAVE_X_WINDOWS */
3295 UNBLOCK_INPUT;
3297 return Qnil;
3301 static Lisp_Object
3302 xmenu_show (f, x, y, for_click, keymaps, title, error)
3303 FRAME_PTR f;
3304 int x, y;
3305 int for_click;
3306 int keymaps;
3307 Lisp_Object title;
3308 char **error;
3310 Window root;
3311 XMenu *menu;
3312 int pane, selidx, lpane, status;
3313 Lisp_Object entry, pane_prefix;
3314 char *datap;
3315 int ulx, uly, width, height;
3316 int dispwidth, dispheight;
3317 int i, j, lines, maxlines;
3318 int maxwidth;
3319 int dummy_int;
3320 unsigned int dummy_uint;
3321 int specpdl_count = SPECPDL_INDEX ();
3323 *error = 0;
3324 if (menu_items_n_panes == 0)
3325 return Qnil;
3327 if (menu_items_used <= MENU_ITEMS_PANE_LENGTH)
3329 *error = "Empty menu";
3330 return Qnil;
3333 /* Figure out which root window F is on. */
3334 XGetGeometry (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), &root,
3335 &dummy_int, &dummy_int, &dummy_uint, &dummy_uint,
3336 &dummy_uint, &dummy_uint);
3338 /* Make the menu on that window. */
3339 menu = XMenuCreate (FRAME_X_DISPLAY (f), root, "emacs");
3340 if (menu == NULL)
3342 *error = "Can't create menu";
3343 return Qnil;
3346 /* Don't GC while we prepare and show the menu,
3347 because we give the oldxmenu library pointers to the
3348 contents of strings. */
3349 inhibit_garbage_collection ();
3351 #ifdef HAVE_X_WINDOWS
3352 /* Adjust coordinates to relative to the outer (window manager) window. */
3353 x += FRAME_OUTER_TO_INNER_DIFF_X (f);
3354 y += FRAME_OUTER_TO_INNER_DIFF_Y (f);
3355 #endif /* HAVE_X_WINDOWS */
3357 /* Adjust coordinates to be root-window-relative. */
3358 x += f->left_pos;
3359 y += f->top_pos;
3361 /* Create all the necessary panes and their items. */
3362 maxlines = lines = i = 0;
3363 while (i < menu_items_used)
3365 if (EQ (XVECTOR (menu_items)->contents[i], Qt))
3367 /* Create a new pane. */
3368 Lisp_Object pane_name, prefix;
3369 char *pane_string;
3371 maxlines = max (maxlines, lines);
3372 lines = 0;
3373 pane_name = XVECTOR (menu_items)->contents[i + MENU_ITEMS_PANE_NAME];
3374 prefix = XVECTOR (menu_items)->contents[i + MENU_ITEMS_PANE_PREFIX];
3375 pane_string = (NILP (pane_name)
3376 ? "" : (char *) SDATA (pane_name));
3377 if (keymaps && !NILP (prefix))
3378 pane_string++;
3380 lpane = XMenuAddPane (FRAME_X_DISPLAY (f), menu, pane_string, TRUE);
3381 if (lpane == XM_FAILURE)
3383 XMenuDestroy (FRAME_X_DISPLAY (f), menu);
3384 *error = "Can't create pane";
3385 return Qnil;
3387 i += MENU_ITEMS_PANE_LENGTH;
3389 /* Find the width of the widest item in this pane. */
3390 maxwidth = 0;
3391 j = i;
3392 while (j < menu_items_used)
3394 Lisp_Object item;
3395 item = XVECTOR (menu_items)->contents[j];
3396 if (EQ (item, Qt))
3397 break;
3398 if (NILP (item))
3400 j++;
3401 continue;
3403 width = SBYTES (item);
3404 if (width > maxwidth)
3405 maxwidth = width;
3407 j += MENU_ITEMS_ITEM_LENGTH;
3410 /* Ignore a nil in the item list.
3411 It's meaningful only for dialog boxes. */
3412 else if (EQ (XVECTOR (menu_items)->contents[i], Qquote))
3413 i += 1;
3414 else
3416 /* Create a new item within current pane. */
3417 Lisp_Object item_name, enable, descrip, help;
3418 unsigned char *item_data;
3419 char *help_string;
3421 item_name = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_NAME];
3422 enable = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_ENABLE];
3423 descrip
3424 = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_EQUIV_KEY];
3425 help = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_HELP];
3426 help_string = STRINGP (help) ? SDATA (help) : NULL;
3428 if (!NILP (descrip))
3430 int gap = maxwidth - SBYTES (item_name);
3431 #ifdef C_ALLOCA
3432 Lisp_Object spacer;
3433 spacer = Fmake_string (make_number (gap), make_number (' '));
3434 item_name = concat2 (item_name, spacer);
3435 item_name = concat2 (item_name, descrip);
3436 item_data = SDATA (item_name);
3437 #else
3438 /* if alloca is fast, use that to make the space,
3439 to reduce gc needs. */
3440 item_data
3441 = (unsigned char *) alloca (maxwidth
3442 + SBYTES (descrip) + 1);
3443 bcopy (SDATA (item_name), item_data,
3444 SBYTES (item_name));
3445 for (j = SCHARS (item_name); j < maxwidth; j++)
3446 item_data[j] = ' ';
3447 bcopy (SDATA (descrip), item_data + j,
3448 SBYTES (descrip));
3449 item_data[j + SBYTES (descrip)] = 0;
3450 #endif
3452 else
3453 item_data = SDATA (item_name);
3455 if (XMenuAddSelection (FRAME_X_DISPLAY (f),
3456 menu, lpane, 0, item_data,
3457 !NILP (enable), help_string)
3458 == XM_FAILURE)
3460 XMenuDestroy (FRAME_X_DISPLAY (f), menu);
3461 *error = "Can't add selection to menu";
3462 return Qnil;
3464 i += MENU_ITEMS_ITEM_LENGTH;
3465 lines++;
3469 maxlines = max (maxlines, lines);
3471 /* All set and ready to fly. */
3472 XMenuRecompute (FRAME_X_DISPLAY (f), menu);
3473 dispwidth = DisplayWidth (FRAME_X_DISPLAY (f), FRAME_X_SCREEN_NUMBER (f));
3474 dispheight = DisplayHeight (FRAME_X_DISPLAY (f), FRAME_X_SCREEN_NUMBER (f));
3475 x = min (x, dispwidth);
3476 y = min (y, dispheight);
3477 x = max (x, 1);
3478 y = max (y, 1);
3479 XMenuLocate (FRAME_X_DISPLAY (f), menu, 0, 0, x, y,
3480 &ulx, &uly, &width, &height);
3481 if (ulx+width > dispwidth)
3483 x -= (ulx + width) - dispwidth;
3484 ulx = dispwidth - width;
3486 if (uly+height > dispheight)
3488 y -= (uly + height) - dispheight;
3489 uly = dispheight - height;
3491 if (ulx < 0) x -= ulx;
3492 if (uly < 0) y -= uly;
3494 if (! for_click)
3496 /* If position was not given by a mouse click, adjust so upper left
3497 corner of the menu as a whole ends up at given coordinates. This
3498 is what x-popup-menu says in its documentation. */
3499 x += width/2;
3500 y += 1.5*height/(maxlines+2);
3503 XMenuSetAEQ (menu, TRUE);
3504 XMenuSetFreeze (menu, TRUE);
3505 pane = selidx = 0;
3507 #ifndef MSDOS
3508 XMenuActivateSetWaitFunction (x_menu_wait_for_event, FRAME_X_DISPLAY (f));
3509 #endif
3511 record_unwind_protect (pop_down_menu,
3512 Fcons (make_save_value (f, 0),
3513 make_save_value (menu, 0)));
3515 /* Help display under X won't work because XMenuActivate contains
3516 a loop that doesn't give Emacs a chance to process it. */
3517 menu_help_frame = f;
3518 status = XMenuActivate (FRAME_X_DISPLAY (f), menu, &pane, &selidx,
3519 x, y, ButtonReleaseMask, &datap,
3520 menu_help_callback);
3522 switch (status)
3524 case XM_SUCCESS:
3525 #ifdef XDEBUG
3526 fprintf (stderr, "pane= %d line = %d\n", panes, selidx);
3527 #endif
3529 /* Find the item number SELIDX in pane number PANE. */
3530 i = 0;
3531 while (i < menu_items_used)
3533 if (EQ (XVECTOR (menu_items)->contents[i], Qt))
3535 if (pane == 0)
3536 pane_prefix
3537 = XVECTOR (menu_items)->contents[i + MENU_ITEMS_PANE_PREFIX];
3538 pane--;
3539 i += MENU_ITEMS_PANE_LENGTH;
3541 else
3543 if (pane == -1)
3545 if (selidx == 0)
3547 entry
3548 = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_VALUE];
3549 if (keymaps != 0)
3551 entry = Fcons (entry, Qnil);
3552 if (!NILP (pane_prefix))
3553 entry = Fcons (pane_prefix, entry);
3555 break;
3557 selidx--;
3559 i += MENU_ITEMS_ITEM_LENGTH;
3562 break;
3564 case XM_FAILURE:
3565 *error = "Can't activate menu";
3566 case XM_IA_SELECT:
3567 entry = Qnil;
3568 break;
3569 case XM_NO_SELECT:
3570 /* Make "Cancel" equivalent to C-g unless FOR_CLICK (which means
3571 the menu was invoked with a mouse event as POSITION). */
3572 if (! for_click)
3573 Fsignal (Qquit, Qnil);
3574 entry = Qnil;
3575 break;
3578 unbind_to (specpdl_count, Qnil);
3580 return entry;
3583 #endif /* not USE_X_TOOLKIT */
3585 #endif /* HAVE_MENUS */
3587 void
3588 syms_of_xmenu ()
3590 staticpro (&menu_items);
3591 menu_items = Qnil;
3592 menu_items_inuse = Qnil;
3594 Qdebug_on_next_call = intern ("debug-on-next-call");
3595 staticpro (&Qdebug_on_next_call);
3597 DEFVAR_LISP ("menu-updating-frame", &Vmenu_updating_frame,
3598 doc: /* Frame for which we are updating a menu.
3599 The enable predicate for a menu command should check this variable. */);
3600 Vmenu_updating_frame = Qnil;
3602 #ifdef USE_X_TOOLKIT
3603 widget_id_tick = (1<<16);
3604 next_menubar_widget_id = 1;
3605 #endif
3607 defsubr (&Sx_popup_menu);
3608 #ifdef HAVE_MENUS
3609 defsubr (&Sx_popup_dialog);
3610 #endif
3613 /* arch-tag: 92ea573c-398e-496e-ac73-2436f7d63242
3614 (do not change this comment) */