(menu-bar-file-menu): Make this the real name
[emacs.git] / src / xmenu.c
blob9868902f3f38b18942fa00f036585e3a1f456099
1 /* X Communication module for terminals which understand the X protocol.
2 Copyright (C) 1986, 1988, 1993, 1994, 1996, 1999, 2000, 2001, 2003, 2004
3 Free Software Foundation, Inc.
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., 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, 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 /* On 4.3 this loses if it comes after xterm.h. */
37 #include <signal.h>
39 #include <stdio.h>
41 #include "lisp.h"
42 #include "termhooks.h"
43 #include "keyboard.h"
44 #include "keymap.h"
45 #include "frame.h"
46 #include "window.h"
47 #include "blockinput.h"
48 #include "buffer.h"
49 #include "charset.h"
50 #include "coding.h"
52 #ifdef MSDOS
53 #include "msdos.h"
54 #endif
56 #ifdef HAVE_X_WINDOWS
57 /* This may include sys/types.h, and that somehow loses
58 if this is not done before the other system files. */
59 #include "xterm.h"
60 #endif
62 /* Load sys/types.h if not already loaded.
63 In some systems loading it twice is suicidal. */
64 #ifndef makedev
65 #include <sys/types.h>
66 #endif
68 #include "dispextern.h"
70 #ifdef HAVE_X_WINDOWS
71 /* Defining HAVE_MULTILINGUAL_MENU would mean that the toolkit menu
72 code accepts the Emacs internal encoding. */
73 #undef HAVE_MULTILINGUAL_MENU
74 #ifdef USE_X_TOOLKIT
75 #include "widget.h"
76 #include <X11/Xlib.h>
77 #include <X11/IntrinsicP.h>
78 #include <X11/CoreP.h>
79 #include <X11/StringDefs.h>
80 #include <X11/Shell.h>
81 #ifdef USE_LUCID
82 #include <X11/Xaw/Paned.h>
83 #endif /* USE_LUCID */
84 #include "../lwlib/lwlib.h"
85 #else /* not USE_X_TOOLKIT */
86 #ifndef USE_GTK
87 #include "../oldXMenu/XMenu.h"
88 #endif
89 #endif /* not USE_X_TOOLKIT */
90 #endif /* HAVE_X_WINDOWS */
92 #ifndef TRUE
93 #define TRUE 1
94 #define FALSE 0
95 #endif /* no TRUE */
97 Lisp_Object Vmenu_updating_frame;
99 Lisp_Object Qdebug_on_next_call;
101 extern Lisp_Object Qmenu_bar;
103 extern Lisp_Object QCtoggle, QCradio;
105 extern Lisp_Object Voverriding_local_map;
106 extern Lisp_Object Voverriding_local_map_menu_flag;
108 extern Lisp_Object Qoverriding_local_map, Qoverriding_terminal_local_map;
110 extern Lisp_Object Qmenu_bar_update_hook;
112 #ifdef USE_X_TOOLKIT
113 extern void set_frame_menubar P_ ((FRAME_PTR, int, int));
114 extern XtAppContext Xt_app_con;
116 static Lisp_Object xdialog_show P_ ((FRAME_PTR, int, Lisp_Object, char **));
117 static void popup_get_selection P_ ((XEvent *, struct x_display_info *,
118 LWLIB_ID, int, int));
120 /* Define HAVE_BOXES if menus can handle radio and toggle buttons. */
122 #define HAVE_BOXES 1
123 #endif /* USE_X_TOOLKIT */
125 #ifdef USE_GTK
126 #include "gtkutil.h"
127 #define HAVE_BOXES 1
128 extern void set_frame_menubar P_ ((FRAME_PTR, int, int));
129 static Lisp_Object xdialog_show P_ ((FRAME_PTR, int, Lisp_Object, char **));
130 #endif
132 /* This is how to deal with multibyte text if HAVE_MULTILINGUAL_MENU
133 isn't defined. The use of HAVE_MULTILINGUAL_MENU could probably be
134 confined to an extended version of this with sections of code below
135 using it unconditionally. */
136 #ifdef USE_GTK
137 /* gtk just uses utf-8. */
138 # define ENCODE_MENU_STRING(str) ENCODE_UTF_8 (str)
139 #else
140 /* I'm not convinced ENCODE_SYSTEM is defined correctly, or maybe
141 something else should be used here. Except under MS-Windows it
142 just converts to unibyte, but encoding with `locale-coding-system'
143 seems better -- X may actually display the result correctly, and
144 it's not necessarily equivalent to the unibyte text. -- fx */
145 # define ENCODE_MENU_STRING(str) ENCODE_SYSTEM (str)
146 #endif
148 static void push_menu_item P_ ((Lisp_Object, Lisp_Object, Lisp_Object,
149 Lisp_Object, Lisp_Object, Lisp_Object,
150 Lisp_Object, Lisp_Object));
151 static int update_frame_menubar P_ ((struct frame *));
152 static Lisp_Object xmenu_show P_ ((struct frame *, int, int, int, int,
153 Lisp_Object, char **));
154 static void keymap_panes P_ ((Lisp_Object *, int, int));
155 static void single_keymap_panes P_ ((Lisp_Object, Lisp_Object, Lisp_Object,
156 int, int));
157 static void list_of_panes P_ ((Lisp_Object));
158 static void list_of_items P_ ((Lisp_Object));
160 extern EMACS_TIME timer_check P_ ((int));
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 int 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; !NILP (tail); tail = Fcdr (tail))
651 Lisp_Object elt, pane_name, pane_data;
652 elt = Fcar (tail);
653 pane_name = Fcar (elt);
654 CHECK_STRING (pane_name);
655 push_menu_pane (ENCODE_MENU_STRING (pane_name), Qnil);
656 pane_data = Fcdr (elt);
657 CHECK_CONS (pane_data);
658 list_of_items (pane_data);
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; !NILP (tail); tail = Fcdr (tail))
674 item = Fcar (tail);
675 if (STRINGP (item))
676 push_menu_item (ENCODE_MENU_STRING (item), Qnil, Qnil, Qt,
677 Qnil, Qnil, Qnil, Qnil);
678 else if (NILP (item))
679 push_left_right_boundary ();
680 else
682 CHECK_CONS (item);
683 item1 = Fcar (item);
684 CHECK_STRING (item1);
685 push_menu_item (ENCODE_MENU_STRING (item1), Qt, Fcdr (item),
686 Qt, Qnil, Qnil, Qnil, Qnil);
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's frame. (WINDOW may be a frame object instead of a window.)
745 This controls the position of the center of the first line
746 in the first pane of the menu, not the top left of the menu as a whole.
747 If POSITION is t, it means to use the current mouse position.
749 MENU is a specifier for a menu. For the simplest case, MENU is a keymap.
750 The menu items come from key bindings that have a menu string as well as
751 a definition; actually, the "definition" in such a key binding looks like
752 \(STRING . REAL-DEFINITION). To give the menu a title, put a string into
753 the keymap as a top-level element.
755 If REAL-DEFINITION is nil, that puts a nonselectable string in the menu.
756 Otherwise, REAL-DEFINITION should be a valid key binding definition.
758 You can also use a list of keymaps as MENU.
759 Then each keymap makes a separate pane.
760 When MENU is a keymap or a list of keymaps, the return value
761 is a list of events.
763 Alternatively, you can specify a menu of multiple panes
764 with a list of the form (TITLE PANE1 PANE2...),
765 where each pane is a list of form (TITLE ITEM1 ITEM2...).
766 Each ITEM is normally a cons cell (STRING . VALUE);
767 but a string can appear as an item--that makes a nonselectable line
768 in the menu.
769 With this form of menu, the return value is VALUE from the chosen item.
771 If POSITION is nil, don't display the menu at all, just precalculate the
772 cached information about equivalent key sequences. */)
773 (position, menu)
774 Lisp_Object position, menu;
776 Lisp_Object keymap, tem;
777 int xpos = 0, ypos = 0;
778 Lisp_Object title;
779 char *error_name;
780 Lisp_Object selection;
781 FRAME_PTR f = NULL;
782 Lisp_Object x, y, window;
783 int keymaps = 0;
784 int for_click = 0;
785 int specpdl_count = SPECPDL_INDEX ();
786 struct gcpro gcpro1;
788 #ifdef HAVE_MENUS
789 if (! NILP (position))
791 int get_current_pos_p = 0;
792 check_x ();
794 /* Decode the first argument: find the window and the coordinates. */
795 if (EQ (position, Qt)
796 || (CONSP (position) && (EQ (XCAR (position), Qmenu_bar)
797 || EQ (XCAR (position), Qtool_bar))))
799 get_current_pos_p = 1;
801 else
803 tem = Fcar (position);
804 if (CONSP (tem))
806 window = Fcar (Fcdr (position));
807 x = Fcar (tem);
808 y = Fcar (Fcdr (tem));
810 else
812 for_click = 1;
813 tem = Fcar (Fcdr (position)); /* EVENT_START (position) */
814 window = Fcar (tem); /* POSN_WINDOW (tem) */
815 tem = Fcar (Fcdr (Fcdr (tem))); /* POSN_WINDOW_POSN (tem) */
816 x = Fcar (tem);
817 y = Fcdr (tem);
820 /* If a click happens in an external tool bar or a detached
821 tool bar, x and y is NIL. In that case, use the current
822 mouse position. This happens for the help button in the
823 tool bar. Ideally popup-menu should pass NIL to
824 this function, but it doesn't. */
825 if (NILP (x) && NILP (y))
826 get_current_pos_p = 1;
829 if (get_current_pos_p)
831 /* Use the mouse's current position. */
832 FRAME_PTR new_f = SELECTED_FRAME ();
833 #ifdef HAVE_X_WINDOWS
834 /* Can't use mouse_position_hook for X since it returns
835 coordinates relative to the window the mouse is in,
836 we need coordinates relative to the edit widget always. */
837 if (new_f != 0)
839 int cur_x, cur_y;
841 mouse_position_for_popup (new_f, &cur_x, &cur_y);
842 /* cur_x/y may be negative, so use make_number. */
843 x = make_number (cur_x);
844 y = make_number (cur_y);
847 #else /* not HAVE_X_WINDOWS */
848 Lisp_Object bar_window;
849 enum scroll_bar_part part;
850 unsigned long time;
852 if (mouse_position_hook)
853 (*mouse_position_hook) (&new_f, 1, &bar_window,
854 &part, &x, &y, &time);
855 #endif /* not HAVE_X_WINDOWS */
857 if (new_f != 0)
858 XSETFRAME (window, new_f);
859 else
861 window = selected_window;
862 XSETFASTINT (x, 0);
863 XSETFASTINT (y, 0);
867 CHECK_NUMBER (x);
868 CHECK_NUMBER (y);
870 /* Decode where to put the menu. */
872 if (FRAMEP (window))
874 f = XFRAME (window);
875 xpos = 0;
876 ypos = 0;
878 else if (WINDOWP (window))
880 CHECK_LIVE_WINDOW (window);
881 f = XFRAME (WINDOW_FRAME (XWINDOW (window)));
883 xpos = WINDOW_LEFT_EDGE_X (XWINDOW (window));
884 ypos = WINDOW_TOP_EDGE_Y (XWINDOW (window));
886 else
887 /* ??? Not really clean; should be CHECK_WINDOW_OR_FRAME,
888 but I don't want to make one now. */
889 CHECK_WINDOW (window);
891 xpos += XINT (x);
892 ypos += XINT (y);
894 Vmenu_updating_frame = Qnil;
895 #endif /* HAVE_MENUS */
897 record_unwind_protect (unuse_menu_items, Qnil);
898 title = Qnil;
899 GCPRO1 (title);
901 /* Decode the menu items from what was specified. */
903 keymap = get_keymap (menu, 0, 0);
904 if (CONSP (keymap))
906 /* We were given a keymap. Extract menu info from the keymap. */
907 Lisp_Object prompt;
909 /* Extract the detailed info to make one pane. */
910 keymap_panes (&menu, 1, NILP (position));
912 /* Search for a string appearing directly as an element of the keymap.
913 That string is the title of the menu. */
914 prompt = Fkeymap_prompt (keymap);
915 if (NILP (title) && !NILP (prompt))
916 title = prompt;
918 /* Make that be the pane title of the first pane. */
919 if (!NILP (prompt) && menu_items_n_panes >= 0)
920 XVECTOR (menu_items)->contents[MENU_ITEMS_PANE_NAME] = prompt;
922 keymaps = 1;
924 else if (CONSP (menu) && KEYMAPP (XCAR (menu)))
926 /* We were given a list of keymaps. */
927 int nmaps = XFASTINT (Flength (menu));
928 Lisp_Object *maps
929 = (Lisp_Object *) alloca (nmaps * sizeof (Lisp_Object));
930 int i;
932 title = Qnil;
934 /* The first keymap that has a prompt string
935 supplies the menu title. */
936 for (tem = menu, i = 0; CONSP (tem); tem = Fcdr (tem))
938 Lisp_Object prompt;
940 maps[i++] = keymap = get_keymap (Fcar (tem), 1, 0);
942 prompt = Fkeymap_prompt (keymap);
943 if (NILP (title) && !NILP (prompt))
944 title = prompt;
947 /* Extract the detailed info to make one pane. */
948 keymap_panes (maps, nmaps, NILP (position));
950 /* Make the title be the pane title of the first pane. */
951 if (!NILP (title) && menu_items_n_panes >= 0)
952 XVECTOR (menu_items)->contents[MENU_ITEMS_PANE_NAME] = title;
954 keymaps = 1;
956 else
958 /* We were given an old-fashioned menu. */
959 title = Fcar (menu);
960 CHECK_STRING (title);
962 list_of_panes (Fcdr (menu));
964 keymaps = 0;
967 unbind_to (specpdl_count, Qnil);
969 if (NILP (position))
971 discard_menu_items ();
972 UNGCPRO;
973 return Qnil;
976 #ifdef HAVE_MENUS
977 /* Display them in a menu. */
978 BLOCK_INPUT;
980 selection = xmenu_show (f, xpos, ypos, for_click,
981 keymaps, title, &error_name);
982 UNBLOCK_INPUT;
984 discard_menu_items ();
986 UNGCPRO;
987 #endif /* HAVE_MENUS */
989 if (error_name) error (error_name);
990 return selection;
993 #ifdef HAVE_MENUS
995 DEFUN ("x-popup-dialog", Fx_popup_dialog, Sx_popup_dialog, 2, 2, 0,
996 doc: /* Pop up a dialog box and return user's selection.
997 POSITION specifies which frame to use.
998 This is normally a mouse button event or a window or frame.
999 If POSITION is t, it means to use the frame the mouse is on.
1000 The dialog box appears in the middle of the specified frame.
1002 CONTENTS specifies the alternatives to display in the dialog box.
1003 It is a list of the form (TITLE ITEM1 ITEM2...).
1004 Each ITEM is a cons cell (STRING . VALUE).
1005 The return value is VALUE from the chosen item.
1007 An ITEM may also be just a string--that makes a nonselectable item.
1008 An ITEM may also be nil--that means to put all preceding items
1009 on the left of the dialog box and all following items on the right.
1010 \(By default, approximately half appear on each side.) */)
1011 (position, contents)
1012 Lisp_Object position, contents;
1014 FRAME_PTR f = NULL;
1015 Lisp_Object window;
1017 check_x ();
1019 /* Decode the first argument: find the window or frame to use. */
1020 if (EQ (position, Qt)
1021 || (CONSP (position) && (EQ (XCAR (position), Qmenu_bar)
1022 || EQ (XCAR (position), Qtool_bar))))
1024 #if 0 /* Using the frame the mouse is on may not be right. */
1025 /* Use the mouse's current position. */
1026 FRAME_PTR new_f = SELECTED_FRAME ();
1027 Lisp_Object bar_window;
1028 enum scroll_bar_part part;
1029 unsigned long time;
1030 Lisp_Object x, y;
1032 (*mouse_position_hook) (&new_f, 1, &bar_window, &part, &x, &y, &time);
1034 if (new_f != 0)
1035 XSETFRAME (window, new_f);
1036 else
1037 window = selected_window;
1038 #endif
1039 window = selected_window;
1041 else if (CONSP (position))
1043 Lisp_Object tem;
1044 tem = Fcar (position);
1045 if (CONSP (tem))
1046 window = Fcar (Fcdr (position));
1047 else
1049 tem = Fcar (Fcdr (position)); /* EVENT_START (position) */
1050 window = Fcar (tem); /* POSN_WINDOW (tem) */
1053 else if (WINDOWP (position) || FRAMEP (position))
1054 window = position;
1055 else
1056 window = Qnil;
1058 /* Decode where to put the menu. */
1060 if (FRAMEP (window))
1061 f = XFRAME (window);
1062 else if (WINDOWP (window))
1064 CHECK_LIVE_WINDOW (window);
1065 f = XFRAME (WINDOW_FRAME (XWINDOW (window)));
1067 else
1068 /* ??? Not really clean; should be CHECK_WINDOW_OR_FRAME,
1069 but I don't want to make one now. */
1070 CHECK_WINDOW (window);
1072 #if ! defined (USE_X_TOOLKIT) && ! defined (USE_GTK)
1073 /* Display a menu with these alternatives
1074 in the middle of frame F. */
1076 Lisp_Object x, y, frame, newpos;
1077 XSETFRAME (frame, f);
1078 XSETINT (x, x_pixel_width (f) / 2);
1079 XSETINT (y, x_pixel_height (f) / 2);
1080 newpos = Fcons (Fcons (x, Fcons (y, Qnil)), Fcons (frame, Qnil));
1082 return Fx_popup_menu (newpos,
1083 Fcons (Fcar (contents), Fcons (contents, Qnil)));
1085 #else
1087 Lisp_Object title;
1088 char *error_name;
1089 Lisp_Object selection;
1090 int specpdl_count = SPECPDL_INDEX ();
1092 /* Decode the dialog items from what was specified. */
1093 title = Fcar (contents);
1094 CHECK_STRING (title);
1095 record_unwind_protect (unuse_menu_items, Qnil);
1097 if (NILP (Fcar (Fcdr (contents))))
1098 /* No buttons specified, add an "Ok" button so users can pop down
1099 the dialog. Also, the lesstif/motif version crashes if there are
1100 no buttons. */
1101 contents = Fcons (title, Fcons (Fcons (build_string ("Ok"), Qt), Qnil));
1103 list_of_panes (Fcons (contents, Qnil));
1105 /* Display them in a dialog box. */
1106 BLOCK_INPUT;
1107 selection = xdialog_show (f, 0, title, &error_name);
1108 UNBLOCK_INPUT;
1110 unbind_to (specpdl_count, Qnil);
1111 discard_menu_items ();
1113 if (error_name) error (error_name);
1114 return selection;
1116 #endif
1119 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
1121 /* Loop in Xt until the menu pulldown or dialog popup has been
1122 popped down (deactivated). This is used for x-popup-menu
1123 and x-popup-dialog; it is not used for the menu bar.
1125 If DOWN_ON_KEYPRESS is nonzero, pop down if a key is pressed.
1127 NOTE: All calls to popup_get_selection should be protected
1128 with BLOCK_INPUT, UNBLOCK_INPUT wrappers. */
1130 #ifdef USE_X_TOOLKIT
1131 static void
1132 popup_get_selection (initial_event, dpyinfo, id, do_timers, down_on_keypress)
1133 XEvent *initial_event;
1134 struct x_display_info *dpyinfo;
1135 LWLIB_ID id;
1136 int do_timers;
1137 int down_on_keypress;
1139 XEvent event;
1141 while (popup_activated_flag)
1143 /* If we have no events to run, consider timers. */
1144 if (do_timers && !XtAppPending (Xt_app_con))
1145 timer_check (1);
1147 if (initial_event)
1149 event = *initial_event;
1150 initial_event = 0;
1152 else
1153 XtAppNextEvent (Xt_app_con, &event);
1155 /* Make sure we don't consider buttons grabbed after menu goes.
1156 And make sure to deactivate for any ButtonRelease,
1157 even if XtDispatchEvent doesn't do that. */
1158 if (event.type == ButtonRelease
1159 && dpyinfo->display == event.xbutton.display)
1161 dpyinfo->grabbed &= ~(1 << event.xbutton.button);
1162 #ifdef USE_MOTIF /* Pretending that the event came from a
1163 Btn1Down seems the only way to convince Motif to
1164 activate its callbacks; setting the XmNmenuPost
1165 isn't working. --marcus@sysc.pdx.edu. */
1166 event.xbutton.button = 1;
1167 /* Motif only pops down menus when no Ctrl, Alt or Mod
1168 key is pressed and the button is released. So reset key state
1169 so Motif thinks this is the case. */
1170 event.xbutton.state = 0;
1171 #endif
1173 /* If the user presses a key that doesn't go to the menu,
1174 deactivate the menu.
1175 The user is likely to do that if we get wedged.
1176 All toolkits now pop down menus on ESC.
1177 For dialogs however, the focus may not be on the dialog, so
1178 in that case, we pop down. */
1179 else if (event.type == KeyPress
1180 && down_on_keypress
1181 && dpyinfo->display == event.xbutton.display)
1183 KeySym keysym = XLookupKeysym (&event.xkey, 0);
1184 if (!IsModifierKey (keysym)
1185 && x_any_window_to_frame (dpyinfo, event.xany.window) != NULL)
1186 popup_activated_flag = 0;
1189 x_dispatch_event (&event, event.xany.display);
1193 #endif /* USE_X_TOOLKIT */
1195 #ifdef USE_GTK
1196 /* Loop util popup_activated_flag is set to zero in a callback.
1197 Used for popup menus and dialogs. */
1198 static void
1199 popup_widget_loop ()
1201 ++popup_activated_flag;
1203 /* Process events in the Gtk event loop until done. */
1204 while (popup_activated_flag)
1206 gtk_main_iteration ();
1209 #endif
1211 /* Activate the menu bar of frame F.
1212 This is called from keyboard.c when it gets the
1213 MENU_BAR_ACTIVATE_EVENT out of the Emacs event queue.
1215 To activate the menu bar, we use the X button-press event
1216 that was saved in saved_menu_event.
1217 That makes the toolkit do its thing.
1219 But first we recompute the menu bar contents (the whole tree).
1221 The reason for saving the button event until here, instead of
1222 passing it to the toolkit right away, is that we can safely
1223 execute Lisp code. */
1225 void
1226 x_activate_menubar (f)
1227 FRAME_PTR f;
1229 if (!f->output_data.x->saved_menu_event->type)
1230 return;
1232 #ifdef USE_GTK
1233 if (! xg_win_to_widget (FRAME_X_DISPLAY (f),
1234 f->output_data.x->saved_menu_event->xany.window))
1235 return;
1236 #endif
1238 set_frame_menubar (f, 0, 1);
1239 BLOCK_INPUT;
1240 #ifdef USE_GTK
1241 XPutBackEvent (f->output_data.x->display_info->display,
1242 f->output_data.x->saved_menu_event);
1243 popup_activated_flag = 1;
1244 #else
1245 XtDispatchEvent (f->output_data.x->saved_menu_event);
1246 #endif
1247 UNBLOCK_INPUT;
1248 #ifdef USE_MOTIF
1249 if (f->output_data.x->saved_menu_event->type == ButtonRelease)
1250 pending_menu_activation = 1;
1251 #endif
1253 /* Ignore this if we get it a second time. */
1254 f->output_data.x->saved_menu_event->type = 0;
1257 /* Detect if a dialog or menu has been posted. */
1260 popup_activated ()
1262 return popup_activated_flag;
1265 /* This callback is invoked when the user selects a menubar cascade
1266 pushbutton, but before the pulldown menu is posted. */
1268 #ifndef USE_GTK
1269 static void
1270 popup_activate_callback (widget, id, client_data)
1271 Widget widget;
1272 LWLIB_ID id;
1273 XtPointer client_data;
1275 popup_activated_flag = 1;
1277 #endif
1279 /* This callback is invoked when a dialog or menu is finished being
1280 used and has been unposted. */
1282 #ifdef USE_GTK
1283 static void
1284 popup_deactivate_callback (widget, client_data)
1285 GtkWidget *widget;
1286 gpointer client_data;
1288 popup_activated_flag = 0;
1290 #else
1291 static void
1292 popup_deactivate_callback (widget, id, client_data)
1293 Widget widget;
1294 LWLIB_ID id;
1295 XtPointer client_data;
1297 popup_activated_flag = 0;
1299 #endif
1302 /* Function that finds the frame for WIDGET and shows the HELP text
1303 for that widget.
1304 F is the frame if known, or NULL if not known. */
1305 static void
1306 show_help_event (f, widget, help)
1307 FRAME_PTR f;
1308 xt_or_gtk_widget widget;
1309 Lisp_Object help;
1311 Lisp_Object frame;
1313 if (f)
1315 XSETFRAME (frame, f);
1316 kbd_buffer_store_help_event (frame, help);
1318 else
1320 #if 0 /* This code doesn't do anything useful. ++kfs */
1321 /* WIDGET is the popup menu. It's parent is the frame's
1322 widget. See which frame that is. */
1323 xt_or_gtk_widget frame_widget = XtParent (widget);
1324 Lisp_Object tail;
1326 for (tail = Vframe_list; GC_CONSP (tail); tail = XCDR (tail))
1328 frame = XCAR (tail);
1329 if (GC_FRAMEP (frame)
1330 && (f = XFRAME (frame),
1331 FRAME_X_P (f) && f->output_data.x->widget == frame_widget))
1332 break;
1334 #endif
1335 show_help_echo (help, Qnil, Qnil, Qnil, 1);
1339 /* Callback called when menu items are highlighted/unhighlighted
1340 while moving the mouse over them. WIDGET is the menu bar or menu
1341 popup widget. ID is its LWLIB_ID. CALL_DATA contains a pointer to
1342 the data structure for the menu item, or null in case of
1343 unhighlighting. */
1345 #ifdef USE_GTK
1346 void
1347 menu_highlight_callback (widget, call_data)
1348 GtkWidget *widget;
1349 gpointer call_data;
1351 xg_menu_item_cb_data *cb_data;
1352 Lisp_Object help;
1354 cb_data = (xg_menu_item_cb_data*) g_object_get_data (G_OBJECT (widget),
1355 XG_ITEM_DATA);
1356 if (! cb_data) return;
1358 help = call_data ? cb_data->help : Qnil;
1360 /* If popup_activated_flag is greater than 1 we are in a popup menu.
1361 Don't show help for them, they won't appear before the
1362 popup is popped down. */
1363 if (popup_activated_flag <= 1)
1364 show_help_event (cb_data->cl_data->f, widget, help);
1366 #else
1367 void
1368 menu_highlight_callback (widget, id, call_data)
1369 Widget widget;
1370 LWLIB_ID id;
1371 void *call_data;
1373 struct frame *f;
1374 Lisp_Object help;
1376 widget_value *wv = (widget_value *) call_data;
1378 help = wv ? wv->help : Qnil;
1380 /* Determine the frame for the help event. */
1381 f = menubar_id_to_frame (id);
1383 show_help_event (f, widget, help);
1385 #endif
1387 /* Find the menu selection and store it in the keyboard buffer.
1388 F is the frame the menu is on.
1389 MENU_BAR_ITEMS_USED is the length of VECTOR.
1390 VECTOR is an array of menu events for the whole menu.
1392 void
1393 find_and_call_menu_selection (f, menu_bar_items_used, vector, client_data)
1394 FRAME_PTR f;
1395 int menu_bar_items_used;
1396 Lisp_Object vector;
1397 void *client_data;
1399 Lisp_Object prefix, entry;
1400 Lisp_Object *subprefix_stack;
1401 int submenu_depth = 0;
1402 int i;
1404 entry = Qnil;
1405 subprefix_stack = (Lisp_Object *) alloca (menu_bar_items_used * sizeof (Lisp_Object));
1406 prefix = Qnil;
1407 i = 0;
1409 while (i < menu_bar_items_used)
1411 if (EQ (XVECTOR (vector)->contents[i], Qnil))
1413 subprefix_stack[submenu_depth++] = prefix;
1414 prefix = entry;
1415 i++;
1417 else if (EQ (XVECTOR (vector)->contents[i], Qlambda))
1419 prefix = subprefix_stack[--submenu_depth];
1420 i++;
1422 else if (EQ (XVECTOR (vector)->contents[i], Qt))
1424 prefix = XVECTOR (vector)->contents[i + MENU_ITEMS_PANE_PREFIX];
1425 i += MENU_ITEMS_PANE_LENGTH;
1427 else
1429 entry = XVECTOR (vector)->contents[i + MENU_ITEMS_ITEM_VALUE];
1430 /* The EMACS_INT cast avoids a warning. There's no problem
1431 as long as pointers have enough bits to hold small integers. */
1432 if ((int) (EMACS_INT) client_data == i)
1434 int j;
1435 struct input_event buf;
1436 Lisp_Object frame;
1437 EVENT_INIT (buf);
1439 XSETFRAME (frame, f);
1440 buf.kind = MENU_BAR_EVENT;
1441 buf.frame_or_window = frame;
1442 buf.arg = frame;
1443 kbd_buffer_store_event (&buf);
1445 for (j = 0; j < submenu_depth; j++)
1446 if (!NILP (subprefix_stack[j]))
1448 buf.kind = MENU_BAR_EVENT;
1449 buf.frame_or_window = frame;
1450 buf.arg = subprefix_stack[j];
1451 kbd_buffer_store_event (&buf);
1454 if (!NILP (prefix))
1456 buf.kind = MENU_BAR_EVENT;
1457 buf.frame_or_window = frame;
1458 buf.arg = prefix;
1459 kbd_buffer_store_event (&buf);
1462 buf.kind = MENU_BAR_EVENT;
1463 buf.frame_or_window = frame;
1464 buf.arg = entry;
1465 kbd_buffer_store_event (&buf);
1467 return;
1469 i += MENU_ITEMS_ITEM_LENGTH;
1475 #ifdef USE_GTK
1476 /* Gtk calls callbacks just because we tell it what item should be
1477 selected in a radio group. If this variable is set to a non-zero
1478 value, we are creating menus and don't want callbacks right now.
1480 static int xg_crazy_callback_abort;
1482 /* This callback is called from the menu bar pulldown menu
1483 when the user makes a selection.
1484 Figure out what the user chose
1485 and put the appropriate events into the keyboard buffer. */
1486 static void
1487 menubar_selection_callback (widget, client_data)
1488 GtkWidget *widget;
1489 gpointer client_data;
1491 xg_menu_item_cb_data *cb_data = (xg_menu_item_cb_data*) client_data;
1493 if (xg_crazy_callback_abort)
1494 return;
1496 if (! cb_data || ! cb_data->cl_data || ! cb_data->cl_data->f)
1497 return;
1499 find_and_call_menu_selection (cb_data->cl_data->f,
1500 cb_data->cl_data->menu_bar_items_used,
1501 cb_data->cl_data->menu_bar_vector,
1502 cb_data->call_data);
1505 #else /* not USE_GTK */
1507 /* This callback is called from the menu bar pulldown menu
1508 when the user makes a selection.
1509 Figure out what the user chose
1510 and put the appropriate events into the keyboard buffer. */
1511 static void
1512 menubar_selection_callback (widget, id, client_data)
1513 Widget widget;
1514 LWLIB_ID id;
1515 XtPointer client_data;
1517 FRAME_PTR f;
1519 f = menubar_id_to_frame (id);
1520 if (!f)
1521 return;
1522 find_and_call_menu_selection (f, f->menu_bar_items_used,
1523 f->menu_bar_vector, client_data);
1525 #endif /* not USE_GTK */
1527 /* Allocate a widget_value, blocking input. */
1529 widget_value *
1530 xmalloc_widget_value ()
1532 widget_value *value;
1534 BLOCK_INPUT;
1535 value = malloc_widget_value ();
1536 UNBLOCK_INPUT;
1538 return value;
1541 /* This recursively calls free_widget_value on the tree of widgets.
1542 It must free all data that was malloc'ed for these widget_values.
1543 In Emacs, many slots are pointers into the data of Lisp_Strings, and
1544 must be left alone. */
1546 void
1547 free_menubar_widget_value_tree (wv)
1548 widget_value *wv;
1550 if (! wv) return;
1552 wv->name = wv->value = wv->key = (char *) 0xDEADBEEF;
1554 if (wv->contents && (wv->contents != (widget_value*)1))
1556 free_menubar_widget_value_tree (wv->contents);
1557 wv->contents = (widget_value *) 0xDEADBEEF;
1559 if (wv->next)
1561 free_menubar_widget_value_tree (wv->next);
1562 wv->next = (widget_value *) 0xDEADBEEF;
1564 BLOCK_INPUT;
1565 free_widget_value (wv);
1566 UNBLOCK_INPUT;
1569 /* Set up data in menu_items for a menu bar item
1570 whose event type is ITEM_KEY (with string ITEM_NAME)
1571 and whose contents come from the list of keymaps MAPS. */
1573 static int
1574 parse_single_submenu (item_key, item_name, maps)
1575 Lisp_Object item_key, item_name, maps;
1577 Lisp_Object length;
1578 int len;
1579 Lisp_Object *mapvec;
1580 int i;
1581 int top_level_items = 0;
1583 length = Flength (maps);
1584 len = XINT (length);
1586 /* Convert the list MAPS into a vector MAPVEC. */
1587 mapvec = (Lisp_Object *) alloca (len * sizeof (Lisp_Object));
1588 for (i = 0; i < len; i++)
1590 mapvec[i] = Fcar (maps);
1591 maps = Fcdr (maps);
1594 /* Loop over the given keymaps, making a pane for each map.
1595 But don't make a pane that is empty--ignore that map instead. */
1596 for (i = 0; i < len; i++)
1598 if (!KEYMAPP (mapvec[i]))
1600 /* Here we have a command at top level in the menu bar
1601 as opposed to a submenu. */
1602 top_level_items = 1;
1603 push_menu_pane (Qnil, Qnil);
1604 push_menu_item (item_name, Qt, item_key, mapvec[i],
1605 Qnil, Qnil, Qnil, Qnil);
1607 else
1609 Lisp_Object prompt;
1610 prompt = Fkeymap_prompt (mapvec[i]);
1611 single_keymap_panes (mapvec[i],
1612 !NILP (prompt) ? prompt : item_name,
1613 item_key, 0, 10);
1617 return top_level_items;
1620 /* Create a tree of widget_value objects
1621 representing the panes and items
1622 in menu_items starting at index START, up to index END. */
1624 static widget_value *
1625 digest_single_submenu (start, end, top_level_items)
1626 int start, end, top_level_items;
1628 widget_value *wv, *prev_wv, *save_wv, *first_wv;
1629 int i;
1630 int submenu_depth = 0;
1631 widget_value **submenu_stack;
1633 submenu_stack
1634 = (widget_value **) alloca (menu_items_used * sizeof (widget_value *));
1635 wv = xmalloc_widget_value ();
1636 wv->name = "menu";
1637 wv->value = 0;
1638 wv->enabled = 1;
1639 wv->button_type = BUTTON_TYPE_NONE;
1640 wv->help = Qnil;
1641 first_wv = wv;
1642 save_wv = 0;
1643 prev_wv = 0;
1645 /* Loop over all panes and items made by the preceding call
1646 to parse_single_submenu and construct a tree of widget_value objects.
1647 Ignore the panes and items used by previous calls to
1648 digest_single_submenu, even though those are also in menu_items. */
1649 i = start;
1650 while (i < end)
1652 if (EQ (XVECTOR (menu_items)->contents[i], Qnil))
1654 submenu_stack[submenu_depth++] = save_wv;
1655 save_wv = prev_wv;
1656 prev_wv = 0;
1657 i++;
1659 else if (EQ (XVECTOR (menu_items)->contents[i], Qlambda))
1661 prev_wv = save_wv;
1662 save_wv = submenu_stack[--submenu_depth];
1663 i++;
1665 else if (EQ (XVECTOR (menu_items)->contents[i], Qt)
1666 && submenu_depth != 0)
1667 i += MENU_ITEMS_PANE_LENGTH;
1668 /* Ignore a nil in the item list.
1669 It's meaningful only for dialog boxes. */
1670 else if (EQ (XVECTOR (menu_items)->contents[i], Qquote))
1671 i += 1;
1672 else if (EQ (XVECTOR (menu_items)->contents[i], Qt))
1674 /* Create a new pane. */
1675 Lisp_Object pane_name, prefix;
1676 char *pane_string;
1678 pane_name = XVECTOR (menu_items)->contents[i + MENU_ITEMS_PANE_NAME];
1679 prefix = XVECTOR (menu_items)->contents[i + MENU_ITEMS_PANE_PREFIX];
1681 #ifndef HAVE_MULTILINGUAL_MENU
1682 if (STRINGP (pane_name) && STRING_MULTIBYTE (pane_name))
1684 pane_name = ENCODE_SYSTEM (pane_name);
1685 AREF (menu_items, i + MENU_ITEMS_PANE_NAME) = pane_name;
1687 #endif
1688 pane_string = (NILP (pane_name)
1689 ? "" : (char *) SDATA (pane_name));
1690 /* If there is just one top-level pane, put all its items directly
1691 under the top-level menu. */
1692 if (menu_items_n_panes == 1)
1693 pane_string = "";
1695 /* If the pane has a meaningful name,
1696 make the pane a top-level menu item
1697 with its items as a submenu beneath it. */
1698 if (strcmp (pane_string, ""))
1700 wv = xmalloc_widget_value ();
1701 if (save_wv)
1702 save_wv->next = wv;
1703 else
1704 first_wv->contents = wv;
1705 wv->lname = pane_name;
1706 /* Set value to 1 so update_submenu_strings can handle '@' */
1707 wv->value = (char *)1;
1708 wv->enabled = 1;
1709 wv->button_type = BUTTON_TYPE_NONE;
1710 wv->help = Qnil;
1712 save_wv = wv;
1713 prev_wv = 0;
1714 i += MENU_ITEMS_PANE_LENGTH;
1716 else
1718 /* Create a new item within current pane. */
1719 Lisp_Object item_name, enable, descrip, def, type, selected;
1720 Lisp_Object help;
1722 item_name = AREF (menu_items, i + MENU_ITEMS_ITEM_NAME);
1723 enable = AREF (menu_items, i + MENU_ITEMS_ITEM_ENABLE);
1724 descrip = AREF (menu_items, i + MENU_ITEMS_ITEM_EQUIV_KEY);
1725 def = AREF (menu_items, i + MENU_ITEMS_ITEM_DEFINITION);
1726 type = AREF (menu_items, i + MENU_ITEMS_ITEM_TYPE);
1727 selected = AREF (menu_items, i + MENU_ITEMS_ITEM_SELECTED);
1728 help = AREF (menu_items, i + MENU_ITEMS_ITEM_HELP);
1730 #ifndef HAVE_MULTILINGUAL_MENU
1731 if (STRING_MULTIBYTE (item_name))
1733 item_name = ENCODE_MENU_STRING (item_name);
1734 AREF (menu_items, i + MENU_ITEMS_ITEM_NAME) = item_name;
1737 if (STRINGP (descrip) && STRING_MULTIBYTE (descrip))
1739 descrip = ENCODE_MENU_STRING (descrip);
1740 AREF (menu_items, i + MENU_ITEMS_ITEM_EQUIV_KEY) = descrip;
1742 #endif /* not HAVE_MULTILINGUAL_MENU */
1744 wv = xmalloc_widget_value ();
1745 if (prev_wv)
1746 prev_wv->next = wv;
1747 else
1748 save_wv->contents = wv;
1750 wv->lname = item_name;
1751 if (!NILP (descrip))
1752 wv->lkey = descrip;
1753 wv->value = 0;
1754 /* The EMACS_INT cast avoids a warning. There's no problem
1755 as long as pointers have enough bits to hold small integers. */
1756 wv->call_data = (!NILP (def) ? (void *) (EMACS_INT) i : 0);
1757 wv->enabled = !NILP (enable);
1759 if (NILP (type))
1760 wv->button_type = BUTTON_TYPE_NONE;
1761 else if (EQ (type, QCradio))
1762 wv->button_type = BUTTON_TYPE_RADIO;
1763 else if (EQ (type, QCtoggle))
1764 wv->button_type = BUTTON_TYPE_TOGGLE;
1765 else
1766 abort ();
1768 wv->selected = !NILP (selected);
1769 if (! STRINGP (help))
1770 help = Qnil;
1772 wv->help = help;
1774 prev_wv = wv;
1776 i += MENU_ITEMS_ITEM_LENGTH;
1780 /* If we have just one "menu item"
1781 that was originally a button, return it by itself. */
1782 if (top_level_items && first_wv->contents && first_wv->contents->next == 0)
1784 wv = first_wv->contents;
1785 free_widget_value (first_wv);
1786 return wv;
1789 return first_wv;
1792 /* Walk through the widget_value tree starting at FIRST_WV and update
1793 the char * pointers from the corresponding lisp values.
1794 We do this after building the whole tree, since GC may happen while the
1795 tree is constructed, and small strings are relocated. So we must wait
1796 until no GC can happen before storing pointers into lisp values. */
1797 static void
1798 update_submenu_strings (first_wv)
1799 widget_value *first_wv;
1801 widget_value *wv;
1803 for (wv = first_wv; wv; wv = wv->next)
1805 if (STRINGP (wv->lname))
1807 wv->name = SDATA (wv->lname);
1809 /* Ignore the @ that means "separate pane".
1810 This is a kludge, but this isn't worth more time. */
1811 if (wv->value == (char *)1)
1813 if (wv->name[0] == '@')
1814 wv->name++;
1815 wv->value = 0;
1819 if (STRINGP (wv->lkey))
1820 wv->key = SDATA (wv->lkey);
1822 if (wv->contents)
1823 update_submenu_strings (wv->contents);
1828 /* Recompute all the widgets of frame F, when the menu bar has been
1829 changed. Value is non-zero if widgets were updated. */
1831 static int
1832 update_frame_menubar (f)
1833 FRAME_PTR f;
1835 #ifdef USE_GTK
1836 return xg_update_frame_menubar (f);
1837 #else
1838 struct x_output *x = f->output_data.x;
1839 int columns, rows;
1841 if (!x->menubar_widget || XtIsManaged (x->menubar_widget))
1842 return 0;
1844 BLOCK_INPUT;
1845 /* Save the size of the frame because the pane widget doesn't accept
1846 to resize itself. So force it. */
1847 columns = FRAME_COLS (f);
1848 rows = FRAME_LINES (f);
1850 /* Do the voodoo which means "I'm changing lots of things, don't try
1851 to refigure sizes until I'm done." */
1852 lw_refigure_widget (x->column_widget, False);
1854 /* The order in which children are managed is the top to bottom
1855 order in which they are displayed in the paned window. First,
1856 remove the text-area widget. */
1857 XtUnmanageChild (x->edit_widget);
1859 /* Remove the menubar that is there now, and put up the menubar that
1860 should be there. */
1861 XtManageChild (x->menubar_widget);
1862 XtMapWidget (x->menubar_widget);
1863 XtVaSetValues (x->menubar_widget, XtNmappedWhenManaged, 1, NULL);
1865 /* Re-manage the text-area widget, and then thrash the sizes. */
1866 XtManageChild (x->edit_widget);
1867 lw_refigure_widget (x->column_widget, True);
1869 /* Force the pane widget to resize itself with the right values. */
1870 EmacsFrameSetCharSize (x->edit_widget, columns, rows);
1871 UNBLOCK_INPUT;
1872 #endif
1873 return 1;
1876 /* Set the contents of the menubar widgets of frame F.
1877 The argument FIRST_TIME is currently ignored;
1878 it is set the first time this is called, from initialize_frame_menubar. */
1880 void
1881 set_frame_menubar (f, first_time, deep_p)
1882 FRAME_PTR f;
1883 int first_time;
1884 int deep_p;
1886 xt_or_gtk_widget menubar_widget = f->output_data.x->menubar_widget;
1887 #ifdef USE_X_TOOLKIT
1888 LWLIB_ID id;
1889 #endif
1890 Lisp_Object items;
1891 widget_value *wv, *first_wv, *prev_wv = 0;
1892 int i, last_i = 0;
1893 int *submenu_start, *submenu_end;
1894 int *submenu_top_level_items, *submenu_n_panes;
1897 XSETFRAME (Vmenu_updating_frame, f);
1899 #ifdef USE_X_TOOLKIT
1900 if (f->output_data.x->id == 0)
1901 f->output_data.x->id = next_menubar_widget_id++;
1902 id = f->output_data.x->id;
1903 #endif
1905 if (! menubar_widget)
1906 deep_p = 1;
1907 else if (pending_menu_activation && !deep_p)
1908 deep_p = 1;
1909 /* Make the first call for any given frame always go deep. */
1910 else if (!f->output_data.x->saved_menu_event && !deep_p)
1912 deep_p = 1;
1913 f->output_data.x->saved_menu_event = (XEvent*)xmalloc (sizeof (XEvent));
1914 f->output_data.x->saved_menu_event->type = 0;
1917 #ifdef USE_GTK
1918 /* If we have detached menus, we must update deep so detached menus
1919 also gets updated. */
1920 deep_p = deep_p || xg_have_tear_offs ();
1921 #endif
1923 if (deep_p)
1925 /* Make a widget-value tree representing the entire menu trees. */
1927 struct buffer *prev = current_buffer;
1928 Lisp_Object buffer;
1929 int specpdl_count = SPECPDL_INDEX ();
1930 int previous_menu_items_used = f->menu_bar_items_used;
1931 Lisp_Object *previous_items
1932 = (Lisp_Object *) alloca (previous_menu_items_used
1933 * sizeof (Lisp_Object));
1935 /* If we are making a new widget, its contents are empty,
1936 do always reinitialize them. */
1937 if (! menubar_widget)
1938 previous_menu_items_used = 0;
1940 buffer = XWINDOW (FRAME_SELECTED_WINDOW (f))->buffer;
1941 specbind (Qinhibit_quit, Qt);
1942 /* Don't let the debugger step into this code
1943 because it is not reentrant. */
1944 specbind (Qdebug_on_next_call, Qnil);
1946 record_unwind_protect (Fset_match_data, Fmatch_data (Qnil, Qnil));
1947 record_unwind_protect (unuse_menu_items, Qnil);
1948 if (NILP (Voverriding_local_map_menu_flag))
1950 specbind (Qoverriding_terminal_local_map, Qnil);
1951 specbind (Qoverriding_local_map, Qnil);
1954 set_buffer_internal_1 (XBUFFER (buffer));
1956 /* Run the Lucid hook. */
1957 safe_run_hooks (Qactivate_menubar_hook);
1959 /* If it has changed current-menubar from previous value,
1960 really recompute the menubar from the value. */
1961 if (! NILP (Vlucid_menu_bar_dirty_flag))
1962 call0 (Qrecompute_lucid_menubar);
1963 safe_run_hooks (Qmenu_bar_update_hook);
1964 FRAME_MENU_BAR_ITEMS (f) = menu_bar_items (FRAME_MENU_BAR_ITEMS (f));
1966 items = FRAME_MENU_BAR_ITEMS (f);
1968 /* Save the frame's previous menu bar contents data. */
1969 if (previous_menu_items_used)
1970 bcopy (XVECTOR (f->menu_bar_vector)->contents, previous_items,
1971 previous_menu_items_used * sizeof (Lisp_Object));
1973 /* Fill in menu_items with the current menu bar contents.
1974 This can evaluate Lisp code. */
1975 menu_items = f->menu_bar_vector;
1976 menu_items_allocated = VECTORP (menu_items) ? ASIZE (menu_items) : 0;
1977 submenu_start = (int *) alloca (XVECTOR (items)->size * sizeof (int *));
1978 submenu_end = (int *) alloca (XVECTOR (items)->size * sizeof (int *));
1979 submenu_n_panes = (int *) alloca (XVECTOR (items)->size * sizeof (int));
1980 submenu_top_level_items
1981 = (int *) alloca (XVECTOR (items)->size * sizeof (int *));
1982 init_menu_items ();
1983 for (i = 0; i < XVECTOR (items)->size; i += 4)
1985 Lisp_Object key, string, maps;
1987 last_i = i;
1989 key = XVECTOR (items)->contents[i];
1990 string = XVECTOR (items)->contents[i + 1];
1991 maps = XVECTOR (items)->contents[i + 2];
1992 if (NILP (string))
1993 break;
1995 submenu_start[i] = menu_items_used;
1997 menu_items_n_panes = 0;
1998 submenu_top_level_items[i]
1999 = parse_single_submenu (key, string, maps);
2000 submenu_n_panes[i] = menu_items_n_panes;
2002 submenu_end[i] = menu_items_used;
2005 finish_menu_items ();
2007 /* Convert menu_items into widget_value trees
2008 to display the menu. This cannot evaluate Lisp code. */
2010 wv = xmalloc_widget_value ();
2011 wv->name = "menubar";
2012 wv->value = 0;
2013 wv->enabled = 1;
2014 wv->button_type = BUTTON_TYPE_NONE;
2015 wv->help = Qnil;
2016 first_wv = wv;
2018 for (i = 0; i < last_i; i += 4)
2020 menu_items_n_panes = submenu_n_panes[i];
2021 wv = digest_single_submenu (submenu_start[i], submenu_end[i],
2022 submenu_top_level_items[i]);
2023 if (prev_wv)
2024 prev_wv->next = wv;
2025 else
2026 first_wv->contents = wv;
2027 /* Don't set wv->name here; GC during the loop might relocate it. */
2028 wv->enabled = 1;
2029 wv->button_type = BUTTON_TYPE_NONE;
2030 prev_wv = wv;
2033 set_buffer_internal_1 (prev);
2034 unbind_to (specpdl_count, Qnil);
2036 /* If there has been no change in the Lisp-level contents
2037 of the menu bar, skip redisplaying it. Just exit. */
2039 for (i = 0; i < previous_menu_items_used; i++)
2040 if (menu_items_used == i
2041 || (!EQ (previous_items[i], XVECTOR (menu_items)->contents[i])))
2042 break;
2043 if (i == menu_items_used && i == previous_menu_items_used && i != 0)
2045 free_menubar_widget_value_tree (first_wv);
2046 discard_menu_items ();
2048 return;
2051 /* Now GC cannot happen during the lifetime of the widget_value,
2052 so it's safe to store data from a Lisp_String. */
2053 wv = first_wv->contents;
2054 for (i = 0; i < XVECTOR (items)->size; i += 4)
2056 Lisp_Object string;
2057 string = XVECTOR (items)->contents[i + 1];
2058 if (NILP (string))
2059 break;
2060 wv->name = (char *) SDATA (string);
2061 update_submenu_strings (wv->contents);
2062 wv = wv->next;
2065 f->menu_bar_vector = menu_items;
2066 f->menu_bar_items_used = menu_items_used;
2067 discard_menu_items ();
2069 else
2071 /* Make a widget-value tree containing
2072 just the top level menu bar strings. */
2074 wv = xmalloc_widget_value ();
2075 wv->name = "menubar";
2076 wv->value = 0;
2077 wv->enabled = 1;
2078 wv->button_type = BUTTON_TYPE_NONE;
2079 wv->help = Qnil;
2080 first_wv = wv;
2082 items = FRAME_MENU_BAR_ITEMS (f);
2083 for (i = 0; i < XVECTOR (items)->size; i += 4)
2085 Lisp_Object string;
2087 string = XVECTOR (items)->contents[i + 1];
2088 if (NILP (string))
2089 break;
2091 wv = xmalloc_widget_value ();
2092 wv->name = (char *) SDATA (string);
2093 wv->value = 0;
2094 wv->enabled = 1;
2095 wv->button_type = BUTTON_TYPE_NONE;
2096 wv->help = Qnil;
2097 /* This prevents lwlib from assuming this
2098 menu item is really supposed to be empty. */
2099 /* The EMACS_INT cast avoids a warning.
2100 This value just has to be different from small integers. */
2101 wv->call_data = (void *) (EMACS_INT) (-1);
2103 if (prev_wv)
2104 prev_wv->next = wv;
2105 else
2106 first_wv->contents = wv;
2107 prev_wv = wv;
2110 /* Forget what we thought we knew about what is in the
2111 detailed contents of the menu bar menus.
2112 Changing the top level always destroys the contents. */
2113 f->menu_bar_items_used = 0;
2116 /* Create or update the menu bar widget. */
2118 BLOCK_INPUT;
2120 #ifdef USE_GTK
2121 xg_crazy_callback_abort = 1;
2122 if (menubar_widget)
2124 /* The fourth arg is DEEP_P, which says to consider the entire
2125 menu trees we supply, rather than just the menu bar item names. */
2126 xg_modify_menubar_widgets (menubar_widget,
2128 first_wv,
2129 deep_p,
2130 G_CALLBACK (menubar_selection_callback),
2131 G_CALLBACK (popup_deactivate_callback),
2132 G_CALLBACK (menu_highlight_callback));
2134 else
2136 GtkWidget *wvbox = f->output_data.x->vbox_widget;
2138 menubar_widget
2139 = xg_create_widget ("menubar", "menubar", f, first_wv,
2140 G_CALLBACK (menubar_selection_callback),
2141 G_CALLBACK (popup_deactivate_callback),
2142 G_CALLBACK (menu_highlight_callback));
2144 f->output_data.x->menubar_widget = menubar_widget;
2148 #else /* not USE_GTK */
2149 if (menubar_widget)
2151 /* Disable resizing (done for Motif!) */
2152 lw_allow_resizing (f->output_data.x->widget, False);
2154 /* The third arg is DEEP_P, which says to consider the entire
2155 menu trees we supply, rather than just the menu bar item names. */
2156 lw_modify_all_widgets (id, first_wv, deep_p);
2158 /* Re-enable the edit widget to resize. */
2159 lw_allow_resizing (f->output_data.x->widget, True);
2161 else
2163 menubar_widget = lw_create_widget ("menubar", "menubar", id, first_wv,
2164 f->output_data.x->column_widget,
2166 popup_activate_callback,
2167 menubar_selection_callback,
2168 popup_deactivate_callback,
2169 menu_highlight_callback);
2170 f->output_data.x->menubar_widget = menubar_widget;
2174 int menubar_size
2175 = (f->output_data.x->menubar_widget
2176 ? (f->output_data.x->menubar_widget->core.height
2177 + f->output_data.x->menubar_widget->core.border_width)
2178 : 0);
2180 #if 0 /* Experimentally, we now get the right results
2181 for -geometry -0-0 without this. 24 Aug 96, rms. */
2182 #ifdef USE_LUCID
2183 if (FRAME_EXTERNAL_MENU_BAR (f))
2185 Dimension ibw = 0;
2186 XtVaGetValues (f->output_data.x->column_widget,
2187 XtNinternalBorderWidth, &ibw, NULL);
2188 menubar_size += ibw;
2190 #endif /* USE_LUCID */
2191 #endif /* 0 */
2193 f->output_data.x->menubar_height = menubar_size;
2195 #endif /* not USE_GTK */
2197 free_menubar_widget_value_tree (first_wv);
2198 update_frame_menubar (f);
2200 #ifdef USE_GTK
2201 xg_crazy_callback_abort = 0;
2202 #endif
2204 UNBLOCK_INPUT;
2207 /* Called from Fx_create_frame to create the initial menubar of a frame
2208 before it is mapped, so that the window is mapped with the menubar already
2209 there instead of us tacking it on later and thrashing the window after it
2210 is visible. */
2212 void
2213 initialize_frame_menubar (f)
2214 FRAME_PTR f;
2216 /* This function is called before the first chance to redisplay
2217 the frame. It has to be, so the frame will have the right size. */
2218 FRAME_MENU_BAR_ITEMS (f) = menu_bar_items (FRAME_MENU_BAR_ITEMS (f));
2219 set_frame_menubar (f, 1, 1);
2223 /* Get rid of the menu bar of frame F, and free its storage.
2224 This is used when deleting a frame, and when turning off the menu bar.
2225 For GTK this function is in gtkutil.c. */
2227 #ifndef USE_GTK
2228 void
2229 free_frame_menubar (f)
2230 FRAME_PTR f;
2232 Widget menubar_widget;
2234 menubar_widget = f->output_data.x->menubar_widget;
2236 f->output_data.x->menubar_height = 0;
2238 if (menubar_widget)
2240 #ifdef USE_MOTIF
2241 /* Removing the menu bar magically changes the shell widget's x
2242 and y position of (0, 0) which, when the menu bar is turned
2243 on again, leads to pull-down menuss appearing in strange
2244 positions near the upper-left corner of the display. This
2245 happens only with some window managers like twm and ctwm,
2246 but not with other like Motif's mwm or kwm, because the
2247 latter generate ConfigureNotify events when the menu bar
2248 is switched off, which fixes the shell position. */
2249 Position x0, y0, x1, y1;
2250 #endif
2252 BLOCK_INPUT;
2254 #ifdef USE_MOTIF
2255 if (f->output_data.x->widget)
2256 XtVaGetValues (f->output_data.x->widget, XtNx, &x0, XtNy, &y0, NULL);
2257 #endif
2259 lw_destroy_all_widgets ((LWLIB_ID) f->output_data.x->id);
2260 f->output_data.x->menubar_widget = NULL;
2262 #ifdef USE_MOTIF
2263 if (f->output_data.x->widget)
2265 XtVaGetValues (f->output_data.x->widget, XtNx, &x1, XtNy, &y1, NULL);
2266 if (x1 == 0 && y1 == 0)
2267 XtVaSetValues (f->output_data.x->widget, XtNx, x0, XtNy, y0, NULL);
2269 #endif
2271 UNBLOCK_INPUT;
2274 #endif /* not USE_GTK */
2276 #endif /* USE_X_TOOLKIT || USE_GTK */
2278 /* xmenu_show actually displays a menu using the panes and items in menu_items
2279 and returns the value selected from it.
2280 There are two versions of xmenu_show, one for Xt and one for Xlib.
2281 Both assume input is blocked by the caller. */
2283 /* F is the frame the menu is for.
2284 X and Y are the frame-relative specified position,
2285 relative to the inside upper left corner of the frame F.
2286 FOR_CLICK is nonzero if this menu was invoked for a mouse click.
2287 KEYMAPS is 1 if this menu was specified with keymaps;
2288 in that case, we return a list containing the chosen item's value
2289 and perhaps also the pane's prefix.
2290 TITLE is the specified menu title.
2291 ERROR is a place to store an error message string in case of failure.
2292 (We return nil on failure, but the value doesn't actually matter.) */
2294 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
2296 /* The item selected in the popup menu. */
2297 static Lisp_Object *volatile menu_item_selection;
2299 #ifdef USE_GTK
2301 /* Used when position a popup menu. See menu_position_func and
2302 create_and_show_popup_menu below. */
2303 struct next_popup_x_y
2305 FRAME_PTR f;
2306 int x;
2307 int y;
2310 /* The menu position function to use if we are not putting a popup
2311 menu where the pointer is.
2312 MENU is the menu to pop up.
2313 X and Y shall on exit contain x/y where the menu shall pop up.
2314 PUSH_IN is not documented in the GTK manual.
2315 USER_DATA is any data passed in when calling gtk_menu_popup.
2316 Here it points to a struct next_popup_x_y where the coordinates
2317 to store in *X and *Y are as well as the frame for the popup.
2319 Here only X and Y are used. */
2320 static void
2321 menu_position_func (menu, x, y, push_in, user_data)
2322 GtkMenu *menu;
2323 gint *x;
2324 gint *y;
2325 gboolean *push_in;
2326 gpointer user_data;
2328 struct next_popup_x_y* data = (struct next_popup_x_y*)user_data;
2329 GtkRequisition req;
2330 int disp_width = FRAME_X_DISPLAY_INFO (data->f)->width;
2331 int disp_height = FRAME_X_DISPLAY_INFO (data->f)->height;
2333 *x = data->x;
2334 *y = data->y;
2336 /* Check if there is room for the menu. If not, adjust x/y so that
2337 the menu is fully visible. */
2338 gtk_widget_size_request (GTK_WIDGET (menu), &req);
2339 if (data->x + req.width > disp_width)
2340 *x -= data->x + req.width - disp_width;
2341 if (data->y + req.height > disp_height)
2342 *y -= data->y + req.height - disp_height;
2345 static void
2346 popup_selection_callback (widget, client_data)
2347 GtkWidget *widget;
2348 gpointer client_data;
2350 xg_menu_item_cb_data *cb_data = (xg_menu_item_cb_data*) client_data;
2352 if (xg_crazy_callback_abort) return;
2353 if (cb_data) menu_item_selection = (Lisp_Object *) cb_data->call_data;
2356 /* Pop up the menu for frame F defined by FIRST_WV at X/Y and loop until the
2357 menu pops down.
2358 menu_item_selection will be set to the selection. */
2359 static void
2360 create_and_show_popup_menu (f, first_wv, x, y, for_click)
2361 FRAME_PTR f;
2362 widget_value *first_wv;
2363 int x;
2364 int y;
2365 int for_click;
2367 int i;
2368 GtkWidget *menu;
2369 GtkMenuPositionFunc pos_func = 0; /* Pop up at pointer. */
2370 struct next_popup_x_y popup_x_y;
2372 xg_crazy_callback_abort = 1;
2373 menu = xg_create_widget ("popup", first_wv->name, f, first_wv,
2374 G_CALLBACK (popup_selection_callback),
2375 G_CALLBACK (popup_deactivate_callback),
2376 G_CALLBACK (menu_highlight_callback));
2377 xg_crazy_callback_abort = 0;
2379 for (i = 0; i < 5; i++)
2380 if (FRAME_X_DISPLAY_INFO (f)->grabbed & (1 << i))
2381 break;
2383 if (! for_click)
2385 /* Not invoked by a click. pop up at x/y. */
2386 pos_func = menu_position_func;
2388 /* Adjust coordinates to be root-window-relative. */
2389 x += f->left_pos + FRAME_OUTER_TO_INNER_DIFF_X (f);
2390 y += f->top_pos + FRAME_OUTER_TO_INNER_DIFF_Y (f);
2392 popup_x_y.x = x;
2393 popup_x_y.y = y;
2394 popup_x_y.f = f;
2397 /* Display the menu. */
2398 gtk_widget_show_all (menu);
2399 gtk_menu_popup (GTK_MENU (menu), 0, 0, pos_func, &popup_x_y, i, 0);
2401 /* Set this to one. popup_widget_loop increases it by one, so it becomes
2402 two. show_help_echo uses this to detect popup menus. */
2403 popup_activated_flag = 1;
2404 /* Process events that apply to the menu. */
2405 popup_widget_loop ();
2407 gtk_widget_destroy (menu);
2409 /* Must reset this manually because the button release event is not passed
2410 to Emacs event loop. */
2411 FRAME_X_DISPLAY_INFO (f)->grabbed = 0;
2414 #else /* not USE_GTK */
2416 /* We need a unique id for each widget handled by the Lucid Widget
2417 library.
2419 For the main windows, and popup menus, we use this counter,
2420 which we increment each time after use. This starts from 1<<16.
2422 For menu bars, we use numbers starting at 0, counted in
2423 next_menubar_widget_id. */
2424 LWLIB_ID widget_id_tick;
2426 static void
2427 popup_selection_callback (widget, id, client_data)
2428 Widget widget;
2429 LWLIB_ID id;
2430 XtPointer client_data;
2432 menu_item_selection = (Lisp_Object *) client_data;
2435 /* Pop up the menu for frame F defined by FIRST_WV at X/Y and loop until the
2436 menu pops down.
2437 menu_item_selection will be set to the selection. */
2438 static void
2439 create_and_show_popup_menu (f, first_wv, x, y, for_click)
2440 FRAME_PTR f;
2441 widget_value *first_wv;
2442 int x;
2443 int y;
2444 int for_click;
2446 int i;
2447 Arg av[2];
2448 int ac = 0;
2449 XButtonPressedEvent dummy;
2450 LWLIB_ID menu_id;
2451 Widget menu;
2453 menu_id = widget_id_tick++;
2454 menu = lw_create_widget ("popup", first_wv->name, menu_id, first_wv,
2455 f->output_data.x->widget, 1, 0,
2456 popup_selection_callback,
2457 popup_deactivate_callback,
2458 menu_highlight_callback);
2460 dummy.type = ButtonPress;
2461 dummy.serial = 0;
2462 dummy.send_event = 0;
2463 dummy.display = FRAME_X_DISPLAY (f);
2464 dummy.time = CurrentTime;
2465 dummy.root = FRAME_X_DISPLAY_INFO (f)->root_window;
2466 dummy.window = dummy.root;
2467 dummy.subwindow = dummy.root;
2468 dummy.x = x;
2469 dummy.y = y;
2471 /* Adjust coordinates to be root-window-relative. */
2472 x += f->left_pos + FRAME_OUTER_TO_INNER_DIFF_X (f);
2473 y += f->top_pos + FRAME_OUTER_TO_INNER_DIFF_Y (f);
2475 dummy.x_root = x;
2476 dummy.y_root = y;
2478 dummy.state = 0;
2479 dummy.button = 0;
2480 for (i = 0; i < 5; i++)
2481 if (FRAME_X_DISPLAY_INFO (f)->grabbed & (1 << i))
2482 dummy.button = i;
2484 /* Don't allow any geometry request from the user. */
2485 XtSetArg (av[ac], XtNgeometry, 0); ac++;
2486 XtSetValues (menu, av, ac);
2488 /* Display the menu. */
2489 lw_popup_menu (menu, (XEvent *) &dummy);
2490 popup_activated_flag = 1;
2492 /* Process events that apply to the menu. */
2493 popup_get_selection ((XEvent *) 0, FRAME_X_DISPLAY_INFO (f), menu_id, 0, 0);
2495 /* fp turned off the following statement and wrote a comment
2496 that it is unnecessary--that the menu has already disappeared.
2497 Nowadays the menu disappears ok, all right, but
2498 we need to delete the widgets or multiple ones will pile up. */
2499 lw_destroy_all_widgets (menu_id);
2502 #endif /* not USE_GTK */
2504 static Lisp_Object
2505 xmenu_show (f, x, y, for_click, keymaps, title, error)
2506 FRAME_PTR f;
2507 int x;
2508 int y;
2509 int for_click;
2510 int keymaps;
2511 Lisp_Object title;
2512 char **error;
2514 int i;
2515 widget_value *wv, *save_wv = 0, *first_wv = 0, *prev_wv = 0;
2516 widget_value **submenu_stack
2517 = (widget_value **) alloca (menu_items_used * sizeof (widget_value *));
2518 Lisp_Object *subprefix_stack
2519 = (Lisp_Object *) alloca (menu_items_used * sizeof (Lisp_Object));
2520 int submenu_depth = 0;
2522 int first_pane;
2524 *error = NULL;
2526 if (menu_items_used <= MENU_ITEMS_PANE_LENGTH)
2528 *error = "Empty menu";
2529 return Qnil;
2532 /* Create a tree of widget_value objects
2533 representing the panes and their items. */
2534 wv = xmalloc_widget_value ();
2535 wv->name = "menu";
2536 wv->value = 0;
2537 wv->enabled = 1;
2538 wv->button_type = BUTTON_TYPE_NONE;
2539 wv->help =Qnil;
2540 first_wv = wv;
2541 first_pane = 1;
2543 /* Loop over all panes and items, filling in the tree. */
2544 i = 0;
2545 while (i < menu_items_used)
2547 if (EQ (XVECTOR (menu_items)->contents[i], Qnil))
2549 submenu_stack[submenu_depth++] = save_wv;
2550 save_wv = prev_wv;
2551 prev_wv = 0;
2552 first_pane = 1;
2553 i++;
2555 else if (EQ (XVECTOR (menu_items)->contents[i], Qlambda))
2557 prev_wv = save_wv;
2558 save_wv = submenu_stack[--submenu_depth];
2559 first_pane = 0;
2560 i++;
2562 else if (EQ (XVECTOR (menu_items)->contents[i], Qt)
2563 && submenu_depth != 0)
2564 i += MENU_ITEMS_PANE_LENGTH;
2565 /* Ignore a nil in the item list.
2566 It's meaningful only for dialog boxes. */
2567 else if (EQ (XVECTOR (menu_items)->contents[i], Qquote))
2568 i += 1;
2569 else if (EQ (XVECTOR (menu_items)->contents[i], Qt))
2571 /* Create a new pane. */
2572 Lisp_Object pane_name, prefix;
2573 char *pane_string;
2575 pane_name = AREF (menu_items, i + MENU_ITEMS_PANE_NAME);
2576 prefix = AREF (menu_items, i + MENU_ITEMS_PANE_PREFIX);
2578 #ifndef HAVE_MULTILINGUAL_MENU
2579 if (STRINGP (pane_name) && STRING_MULTIBYTE (pane_name))
2581 pane_name = ENCODE_SYSTEM (pane_name);
2582 AREF (menu_items, i + MENU_ITEMS_PANE_NAME) = pane_name;
2584 #endif
2585 pane_string = (NILP (pane_name)
2586 ? "" : (char *) SDATA (pane_name));
2587 /* If there is just one top-level pane, put all its items directly
2588 under the top-level menu. */
2589 if (menu_items_n_panes == 1)
2590 pane_string = "";
2592 /* If the pane has a meaningful name,
2593 make the pane a top-level menu item
2594 with its items as a submenu beneath it. */
2595 if (!keymaps && strcmp (pane_string, ""))
2597 wv = xmalloc_widget_value ();
2598 if (save_wv)
2599 save_wv->next = wv;
2600 else
2601 first_wv->contents = wv;
2602 wv->name = pane_string;
2603 if (keymaps && !NILP (prefix))
2604 wv->name++;
2605 wv->value = 0;
2606 wv->enabled = 1;
2607 wv->button_type = BUTTON_TYPE_NONE;
2608 wv->help = Qnil;
2609 save_wv = wv;
2610 prev_wv = 0;
2612 else if (first_pane)
2614 save_wv = wv;
2615 prev_wv = 0;
2617 first_pane = 0;
2618 i += MENU_ITEMS_PANE_LENGTH;
2620 else
2622 /* Create a new item within current pane. */
2623 Lisp_Object item_name, enable, descrip, def, type, selected, help;
2624 item_name = AREF (menu_items, i + MENU_ITEMS_ITEM_NAME);
2625 enable = AREF (menu_items, i + MENU_ITEMS_ITEM_ENABLE);
2626 descrip = AREF (menu_items, i + MENU_ITEMS_ITEM_EQUIV_KEY);
2627 def = AREF (menu_items, i + MENU_ITEMS_ITEM_DEFINITION);
2628 type = AREF (menu_items, i + MENU_ITEMS_ITEM_TYPE);
2629 selected = AREF (menu_items, i + MENU_ITEMS_ITEM_SELECTED);
2630 help = AREF (menu_items, i + MENU_ITEMS_ITEM_HELP);
2632 #ifndef HAVE_MULTILINGUAL_MENU
2633 if (STRINGP (item_name) && STRING_MULTIBYTE (item_name))
2635 item_name = ENCODE_MENU_STRING (item_name);
2636 AREF (menu_items, i + MENU_ITEMS_ITEM_NAME) = item_name;
2639 if (STRINGP (descrip) && STRING_MULTIBYTE (descrip))
2641 descrip = ENCODE_MENU_STRING (descrip);
2642 AREF (menu_items, i + MENU_ITEMS_ITEM_EQUIV_KEY) = descrip;
2644 #endif /* not HAVE_MULTILINGUAL_MENU */
2646 wv = xmalloc_widget_value ();
2647 if (prev_wv)
2648 prev_wv->next = wv;
2649 else
2650 save_wv->contents = wv;
2651 wv->name = (char *) SDATA (item_name);
2652 if (!NILP (descrip))
2653 wv->key = (char *) SDATA (descrip);
2654 wv->value = 0;
2655 /* If this item has a null value,
2656 make the call_data null so that it won't display a box
2657 when the mouse is on it. */
2658 wv->call_data
2659 = (!NILP (def) ? (void *) &XVECTOR (menu_items)->contents[i] : 0);
2660 wv->enabled = !NILP (enable);
2662 if (NILP (type))
2663 wv->button_type = BUTTON_TYPE_NONE;
2664 else if (EQ (type, QCtoggle))
2665 wv->button_type = BUTTON_TYPE_TOGGLE;
2666 else if (EQ (type, QCradio))
2667 wv->button_type = BUTTON_TYPE_RADIO;
2668 else
2669 abort ();
2671 wv->selected = !NILP (selected);
2673 if (! STRINGP (help))
2674 help = Qnil;
2676 wv->help = help;
2678 prev_wv = wv;
2680 i += MENU_ITEMS_ITEM_LENGTH;
2684 /* Deal with the title, if it is non-nil. */
2685 if (!NILP (title))
2687 widget_value *wv_title = xmalloc_widget_value ();
2688 widget_value *wv_sep1 = xmalloc_widget_value ();
2689 widget_value *wv_sep2 = xmalloc_widget_value ();
2691 wv_sep2->name = "--";
2692 wv_sep2->next = first_wv->contents;
2693 wv_sep2->help = Qnil;
2695 wv_sep1->name = "--";
2696 wv_sep1->next = wv_sep2;
2697 wv_sep1->help = Qnil;
2699 #ifndef HAVE_MULTILINGUAL_MENU
2700 if (STRING_MULTIBYTE (title))
2701 title = ENCODE_MENU_STRING (title);
2702 #endif
2704 wv_title->name = (char *) SDATA (title);
2705 wv_title->enabled = TRUE;
2706 wv_title->button_type = BUTTON_TYPE_NONE;
2707 wv_title->next = wv_sep1;
2708 wv_title->help = Qnil;
2709 first_wv->contents = wv_title;
2712 /* No selection has been chosen yet. */
2713 menu_item_selection = 0;
2715 /* Actually create and show the menu until popped down. */
2716 create_and_show_popup_menu (f, first_wv, x, y, for_click);
2718 /* Free the widget_value objects we used to specify the contents. */
2719 free_menubar_widget_value_tree (first_wv);
2721 /* Find the selected item, and its pane, to return
2722 the proper value. */
2723 if (menu_item_selection != 0)
2725 Lisp_Object prefix, entry;
2727 prefix = entry = Qnil;
2728 i = 0;
2729 while (i < menu_items_used)
2731 if (EQ (XVECTOR (menu_items)->contents[i], Qnil))
2733 subprefix_stack[submenu_depth++] = prefix;
2734 prefix = entry;
2735 i++;
2737 else if (EQ (XVECTOR (menu_items)->contents[i], Qlambda))
2739 prefix = subprefix_stack[--submenu_depth];
2740 i++;
2742 else if (EQ (XVECTOR (menu_items)->contents[i], Qt))
2744 prefix
2745 = XVECTOR (menu_items)->contents[i + MENU_ITEMS_PANE_PREFIX];
2746 i += MENU_ITEMS_PANE_LENGTH;
2748 /* Ignore a nil in the item list.
2749 It's meaningful only for dialog boxes. */
2750 else if (EQ (XVECTOR (menu_items)->contents[i], Qquote))
2751 i += 1;
2752 else
2754 entry
2755 = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_VALUE];
2756 if (menu_item_selection == &XVECTOR (menu_items)->contents[i])
2758 if (keymaps != 0)
2760 int j;
2762 entry = Fcons (entry, Qnil);
2763 if (!NILP (prefix))
2764 entry = Fcons (prefix, entry);
2765 for (j = submenu_depth - 1; j >= 0; j--)
2766 if (!NILP (subprefix_stack[j]))
2767 entry = Fcons (subprefix_stack[j], entry);
2769 return entry;
2771 i += MENU_ITEMS_ITEM_LENGTH;
2776 return Qnil;
2779 #ifdef USE_GTK
2780 static void
2781 dialog_selection_callback (widget, client_data)
2782 GtkWidget *widget;
2783 gpointer client_data;
2785 /* The EMACS_INT cast avoids a warning. There's no problem
2786 as long as pointers have enough bits to hold small integers. */
2787 if ((int) (EMACS_INT) client_data != -1)
2788 menu_item_selection = (Lisp_Object *) client_data;
2790 popup_activated_flag = 0;
2793 /* Pop up the dialog for frame F defined by FIRST_WV and loop until the
2794 dialog pops down.
2795 menu_item_selection will be set to the selection. */
2796 static void
2797 create_and_show_dialog (f, first_wv)
2798 FRAME_PTR f;
2799 widget_value *first_wv;
2801 GtkWidget *menu;
2803 menu = xg_create_widget ("dialog", first_wv->name, f, first_wv,
2804 G_CALLBACK (dialog_selection_callback),
2805 G_CALLBACK (popup_deactivate_callback),
2808 if (menu)
2810 /* Display the menu. */
2811 gtk_widget_show_all (menu);
2813 /* Process events that apply to the menu. */
2814 popup_widget_loop ();
2816 gtk_widget_destroy (menu);
2820 #else /* not USE_GTK */
2821 static void
2822 dialog_selection_callback (widget, id, client_data)
2823 Widget widget;
2824 LWLIB_ID id;
2825 XtPointer client_data;
2827 /* The EMACS_INT cast avoids a warning. There's no problem
2828 as long as pointers have enough bits to hold small integers. */
2829 if ((int) (EMACS_INT) client_data != -1)
2830 menu_item_selection = (Lisp_Object *) client_data;
2832 BLOCK_INPUT;
2833 lw_destroy_all_widgets (id);
2834 UNBLOCK_INPUT;
2835 popup_activated_flag = 0;
2839 /* ARG is the LWLIB ID of the dialog box, represented
2840 as a Lisp object as (HIGHPART . LOWPART). */
2842 Lisp_Object
2843 xdialog_show_unwind (arg)
2844 Lisp_Object arg;
2846 LWLIB_ID id = (XINT (XCAR (arg)) << 4 * sizeof (LWLIB_ID)
2847 | XINT (XCDR (arg)));
2848 BLOCK_INPUT;
2849 lw_destroy_all_widgets (id);
2850 UNBLOCK_INPUT;
2851 popup_activated_flag = 0;
2852 return Qnil;
2856 /* Pop up the dialog for frame F defined by FIRST_WV and loop until the
2857 dialog pops down.
2858 menu_item_selection will be set to the selection. */
2859 static void
2860 create_and_show_dialog (f, first_wv)
2861 FRAME_PTR f;
2862 widget_value *first_wv;
2864 LWLIB_ID dialog_id;
2866 dialog_id = widget_id_tick++;
2867 lw_create_widget (first_wv->name, "dialog", dialog_id, first_wv,
2868 f->output_data.x->widget, 1, 0,
2869 dialog_selection_callback, 0, 0);
2870 lw_modify_all_widgets (dialog_id, first_wv->contents, True);
2872 /* Display the dialog box. */
2873 lw_pop_up_all_widgets (dialog_id);
2874 popup_activated_flag = 1;
2876 /* Process events that apply to the dialog box.
2877 Also handle timers. */
2879 int count = SPECPDL_INDEX ();
2880 int fact = 4 * sizeof (LWLIB_ID);
2882 /* xdialog_show_unwind is responsible for popping the dialog box down. */
2883 record_unwind_protect (xdialog_show_unwind,
2884 Fcons (make_number (dialog_id >> (fact)),
2885 make_number (dialog_id & ~(-1 << (fact)))));
2887 popup_get_selection ((XEvent *) 0, FRAME_X_DISPLAY_INFO (f),
2888 dialog_id, 1, 1);
2890 unbind_to (count, Qnil);
2894 #endif /* not USE_GTK */
2896 static char * button_names [] = {
2897 "button1", "button2", "button3", "button4", "button5",
2898 "button6", "button7", "button8", "button9", "button10" };
2900 static Lisp_Object
2901 xdialog_show (f, keymaps, title, error)
2902 FRAME_PTR f;
2903 int keymaps;
2904 Lisp_Object title;
2905 char **error;
2907 int i, nb_buttons=0;
2908 char dialog_name[6];
2910 widget_value *wv, *first_wv = 0, *prev_wv = 0;
2912 /* Number of elements seen so far, before boundary. */
2913 int left_count = 0;
2914 /* 1 means we've seen the boundary between left-hand elts and right-hand. */
2915 int boundary_seen = 0;
2917 *error = NULL;
2919 if (menu_items_n_panes > 1)
2921 *error = "Multiple panes in dialog box";
2922 return Qnil;
2925 /* Create a tree of widget_value objects
2926 representing the text label and buttons. */
2928 Lisp_Object pane_name, prefix;
2929 char *pane_string;
2930 pane_name = XVECTOR (menu_items)->contents[MENU_ITEMS_PANE_NAME];
2931 prefix = XVECTOR (menu_items)->contents[MENU_ITEMS_PANE_PREFIX];
2932 pane_string = (NILP (pane_name)
2933 ? "" : (char *) SDATA (pane_name));
2934 prev_wv = xmalloc_widget_value ();
2935 prev_wv->value = pane_string;
2936 if (keymaps && !NILP (prefix))
2937 prev_wv->name++;
2938 prev_wv->enabled = 1;
2939 prev_wv->name = "message";
2940 prev_wv->help = Qnil;
2941 first_wv = prev_wv;
2943 /* Loop over all panes and items, filling in the tree. */
2944 i = MENU_ITEMS_PANE_LENGTH;
2945 while (i < menu_items_used)
2948 /* Create a new item within current pane. */
2949 Lisp_Object item_name, enable, descrip;
2950 item_name = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_NAME];
2951 enable = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_ENABLE];
2952 descrip
2953 = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_EQUIV_KEY];
2955 if (NILP (item_name))
2957 free_menubar_widget_value_tree (first_wv);
2958 *error = "Submenu in dialog items";
2959 return Qnil;
2961 if (EQ (item_name, Qquote))
2963 /* This is the boundary between left-side elts
2964 and right-side elts. Stop incrementing right_count. */
2965 boundary_seen = 1;
2966 i++;
2967 continue;
2969 if (nb_buttons >= 9)
2971 free_menubar_widget_value_tree (first_wv);
2972 *error = "Too many dialog items";
2973 return Qnil;
2976 wv = xmalloc_widget_value ();
2977 prev_wv->next = wv;
2978 wv->name = (char *) button_names[nb_buttons];
2979 if (!NILP (descrip))
2980 wv->key = (char *) SDATA (descrip);
2981 wv->value = (char *) SDATA (item_name);
2982 wv->call_data = (void *) &XVECTOR (menu_items)->contents[i];
2983 wv->enabled = !NILP (enable);
2984 wv->help = Qnil;
2985 prev_wv = wv;
2987 if (! boundary_seen)
2988 left_count++;
2990 nb_buttons++;
2991 i += MENU_ITEMS_ITEM_LENGTH;
2994 /* If the boundary was not specified,
2995 by default put half on the left and half on the right. */
2996 if (! boundary_seen)
2997 left_count = nb_buttons - nb_buttons / 2;
2999 wv = xmalloc_widget_value ();
3000 wv->name = dialog_name;
3001 wv->help = Qnil;
3002 /* Dialog boxes use a really stupid name encoding
3003 which specifies how many buttons to use
3004 and how many buttons are on the right.
3005 The Q means something also. */
3006 dialog_name[0] = 'Q';
3007 dialog_name[1] = '0' + nb_buttons;
3008 dialog_name[2] = 'B';
3009 dialog_name[3] = 'R';
3010 /* Number of buttons to put on the right. */
3011 dialog_name[4] = '0' + nb_buttons - left_count;
3012 dialog_name[5] = 0;
3013 wv->contents = first_wv;
3014 first_wv = wv;
3017 /* No selection has been chosen yet. */
3018 menu_item_selection = 0;
3020 /* Actually create and show the dialog. */
3021 create_and_show_dialog (f, first_wv);
3023 /* Free the widget_value objects we used to specify the contents. */
3024 free_menubar_widget_value_tree (first_wv);
3026 /* Find the selected item, and its pane, to return
3027 the proper value. */
3028 if (menu_item_selection != 0)
3030 Lisp_Object prefix;
3032 prefix = Qnil;
3033 i = 0;
3034 while (i < menu_items_used)
3036 Lisp_Object entry;
3038 if (EQ (XVECTOR (menu_items)->contents[i], Qt))
3040 prefix
3041 = XVECTOR (menu_items)->contents[i + MENU_ITEMS_PANE_PREFIX];
3042 i += MENU_ITEMS_PANE_LENGTH;
3044 else if (EQ (XVECTOR (menu_items)->contents[i], Qquote))
3046 /* This is the boundary between left-side elts and
3047 right-side elts. */
3048 ++i;
3050 else
3052 entry
3053 = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_VALUE];
3054 if (menu_item_selection == &XVECTOR (menu_items)->contents[i])
3056 if (keymaps != 0)
3058 entry = Fcons (entry, Qnil);
3059 if (!NILP (prefix))
3060 entry = Fcons (prefix, entry);
3062 return entry;
3064 i += MENU_ITEMS_ITEM_LENGTH;
3069 return Qnil;
3072 #else /* not USE_X_TOOLKIT && not USE_GTK */
3074 /* The frame of the last activated non-toolkit menu bar.
3075 Used to generate menu help events. */
3077 static struct frame *menu_help_frame;
3080 /* Show help HELP_STRING, or clear help if HELP_STRING is null.
3082 PANE is the pane number, and ITEM is the menu item number in
3083 the menu (currently not used).
3085 This cannot be done with generating a HELP_EVENT because
3086 XMenuActivate contains a loop that doesn't let Emacs process
3087 keyboard events. */
3089 static void
3090 menu_help_callback (help_string, pane, item)
3091 char *help_string;
3092 int pane, item;
3094 extern Lisp_Object Qmenu_item;
3095 Lisp_Object *first_item;
3096 Lisp_Object pane_name;
3097 Lisp_Object menu_object;
3099 first_item = XVECTOR (menu_items)->contents;
3100 if (EQ (first_item[0], Qt))
3101 pane_name = first_item[MENU_ITEMS_PANE_NAME];
3102 else if (EQ (first_item[0], Qquote))
3103 /* This shouldn't happen, see xmenu_show. */
3104 pane_name = empty_string;
3105 else
3106 pane_name = first_item[MENU_ITEMS_ITEM_NAME];
3108 /* (menu-item MENU-NAME PANE-NUMBER) */
3109 menu_object = Fcons (Qmenu_item,
3110 Fcons (pane_name,
3111 Fcons (make_number (pane), Qnil)));
3112 show_help_echo (help_string ? build_string (help_string) : Qnil,
3113 Qnil, menu_object, make_number (item), 1);
3117 static Lisp_Object
3118 xmenu_show (f, x, y, for_click, keymaps, title, error)
3119 FRAME_PTR f;
3120 int x, y;
3121 int for_click;
3122 int keymaps;
3123 Lisp_Object title;
3124 char **error;
3126 Window root;
3127 XMenu *menu;
3128 int pane, selidx, lpane, status;
3129 Lisp_Object entry, pane_prefix;
3130 char *datap;
3131 int ulx, uly, width, height;
3132 int dispwidth, dispheight;
3133 int i, j;
3134 int maxwidth;
3135 int dummy_int;
3136 unsigned int dummy_uint;
3138 *error = 0;
3139 if (menu_items_n_panes == 0)
3140 return Qnil;
3142 if (menu_items_used <= MENU_ITEMS_PANE_LENGTH)
3144 *error = "Empty menu";
3145 return Qnil;
3148 /* Figure out which root window F is on. */
3149 XGetGeometry (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), &root,
3150 &dummy_int, &dummy_int, &dummy_uint, &dummy_uint,
3151 &dummy_uint, &dummy_uint);
3153 /* Make the menu on that window. */
3154 menu = XMenuCreate (FRAME_X_DISPLAY (f), root, "emacs");
3155 if (menu == NULL)
3157 *error = "Can't create menu";
3158 return Qnil;
3161 #ifdef HAVE_X_WINDOWS
3162 /* Adjust coordinates to relative to the outer (window manager) window. */
3164 Window child;
3165 int win_x = 0, win_y = 0;
3167 /* Find the position of the outside upper-left corner of
3168 the inner window, with respect to the outer window. */
3169 if (f->output_data.x->parent_desc != FRAME_X_DISPLAY_INFO (f)->root_window)
3171 BLOCK_INPUT;
3172 XTranslateCoordinates (FRAME_X_DISPLAY (f),
3174 /* From-window, to-window. */
3175 f->output_data.x->window_desc,
3176 f->output_data.x->parent_desc,
3178 /* From-position, to-position. */
3179 0, 0, &win_x, &win_y,
3181 /* Child of window. */
3182 &child);
3183 UNBLOCK_INPUT;
3184 x += win_x;
3185 y += win_y;
3188 #endif /* HAVE_X_WINDOWS */
3190 /* Adjust coordinates to be root-window-relative. */
3191 x += f->left_pos;
3192 y += f->top_pos;
3194 /* Create all the necessary panes and their items. */
3195 i = 0;
3196 while (i < menu_items_used)
3198 if (EQ (XVECTOR (menu_items)->contents[i], Qt))
3200 /* Create a new pane. */
3201 Lisp_Object pane_name, prefix;
3202 char *pane_string;
3204 pane_name = XVECTOR (menu_items)->contents[i + MENU_ITEMS_PANE_NAME];
3205 prefix = XVECTOR (menu_items)->contents[i + MENU_ITEMS_PANE_PREFIX];
3206 pane_string = (NILP (pane_name)
3207 ? "" : (char *) SDATA (pane_name));
3208 if (keymaps && !NILP (prefix))
3209 pane_string++;
3211 lpane = XMenuAddPane (FRAME_X_DISPLAY (f), menu, pane_string, TRUE);
3212 if (lpane == XM_FAILURE)
3214 XMenuDestroy (FRAME_X_DISPLAY (f), menu);
3215 *error = "Can't create pane";
3216 return Qnil;
3218 i += MENU_ITEMS_PANE_LENGTH;
3220 /* Find the width of the widest item in this pane. */
3221 maxwidth = 0;
3222 j = i;
3223 while (j < menu_items_used)
3225 Lisp_Object item;
3226 item = XVECTOR (menu_items)->contents[j];
3227 if (EQ (item, Qt))
3228 break;
3229 if (NILP (item))
3231 j++;
3232 continue;
3234 width = SBYTES (item);
3235 if (width > maxwidth)
3236 maxwidth = width;
3238 j += MENU_ITEMS_ITEM_LENGTH;
3241 /* Ignore a nil in the item list.
3242 It's meaningful only for dialog boxes. */
3243 else if (EQ (XVECTOR (menu_items)->contents[i], Qquote))
3244 i += 1;
3245 else
3247 /* Create a new item within current pane. */
3248 Lisp_Object item_name, enable, descrip, help;
3249 unsigned char *item_data;
3250 char *help_string;
3252 item_name = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_NAME];
3253 enable = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_ENABLE];
3254 descrip
3255 = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_EQUIV_KEY];
3256 help = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_HELP];
3257 help_string = STRINGP (help) ? SDATA (help) : NULL;
3259 if (!NILP (descrip))
3261 int gap = maxwidth - SBYTES (item_name);
3262 #ifdef C_ALLOCA
3263 Lisp_Object spacer;
3264 spacer = Fmake_string (make_number (gap), make_number (' '));
3265 item_name = concat2 (item_name, spacer);
3266 item_name = concat2 (item_name, descrip);
3267 item_data = SDATA (item_name);
3268 #else
3269 /* if alloca is fast, use that to make the space,
3270 to reduce gc needs. */
3271 item_data
3272 = (unsigned char *) alloca (maxwidth
3273 + SBYTES (descrip) + 1);
3274 bcopy (SDATA (item_name), item_data,
3275 SBYTES (item_name));
3276 for (j = SCHARS (item_name); j < maxwidth; j++)
3277 item_data[j] = ' ';
3278 bcopy (SDATA (descrip), item_data + j,
3279 SBYTES (descrip));
3280 item_data[j + SBYTES (descrip)] = 0;
3281 #endif
3283 else
3284 item_data = SDATA (item_name);
3286 if (XMenuAddSelection (FRAME_X_DISPLAY (f),
3287 menu, lpane, 0, item_data,
3288 !NILP (enable), help_string)
3289 == XM_FAILURE)
3291 XMenuDestroy (FRAME_X_DISPLAY (f), menu);
3292 *error = "Can't add selection to menu";
3293 return Qnil;
3295 i += MENU_ITEMS_ITEM_LENGTH;
3299 /* All set and ready to fly. */
3300 XMenuRecompute (FRAME_X_DISPLAY (f), menu);
3301 dispwidth = DisplayWidth (FRAME_X_DISPLAY (f), FRAME_X_SCREEN_NUMBER (f));
3302 dispheight = DisplayHeight (FRAME_X_DISPLAY (f), FRAME_X_SCREEN_NUMBER (f));
3303 x = min (x, dispwidth);
3304 y = min (y, dispheight);
3305 x = max (x, 1);
3306 y = max (y, 1);
3307 XMenuLocate (FRAME_X_DISPLAY (f), menu, 0, 0, x, y,
3308 &ulx, &uly, &width, &height);
3309 if (ulx+width > dispwidth)
3311 x -= (ulx + width) - dispwidth;
3312 ulx = dispwidth - width;
3314 if (uly+height > dispheight)
3316 y -= (uly + height) - dispheight;
3317 uly = dispheight - height;
3319 if (ulx < 0) x -= ulx;
3320 if (uly < 0) y -= uly;
3322 XMenuSetAEQ (menu, TRUE);
3323 XMenuSetFreeze (menu, TRUE);
3324 pane = selidx = 0;
3326 /* Help display under X won't work because XMenuActivate contains
3327 a loop that doesn't give Emacs a chance to process it. */
3328 menu_help_frame = f;
3329 status = XMenuActivate (FRAME_X_DISPLAY (f), menu, &pane, &selidx,
3330 x, y, ButtonReleaseMask, &datap,
3331 menu_help_callback);
3334 #ifdef HAVE_X_WINDOWS
3335 /* Assume the mouse has moved out of the X window.
3336 If it has actually moved in, we will get an EnterNotify. */
3337 x_mouse_leave (FRAME_X_DISPLAY_INFO (f));
3338 #endif
3340 switch (status)
3342 case XM_SUCCESS:
3343 #ifdef XDEBUG
3344 fprintf (stderr, "pane= %d line = %d\n", panes, selidx);
3345 #endif
3347 /* Find the item number SELIDX in pane number PANE. */
3348 i = 0;
3349 while (i < menu_items_used)
3351 if (EQ (XVECTOR (menu_items)->contents[i], Qt))
3353 if (pane == 0)
3354 pane_prefix
3355 = XVECTOR (menu_items)->contents[i + MENU_ITEMS_PANE_PREFIX];
3356 pane--;
3357 i += MENU_ITEMS_PANE_LENGTH;
3359 else
3361 if (pane == -1)
3363 if (selidx == 0)
3365 entry
3366 = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_VALUE];
3367 if (keymaps != 0)
3369 entry = Fcons (entry, Qnil);
3370 if (!NILP (pane_prefix))
3371 entry = Fcons (pane_prefix, entry);
3373 break;
3375 selidx--;
3377 i += MENU_ITEMS_ITEM_LENGTH;
3380 break;
3382 case XM_FAILURE:
3383 *error = "Can't activate menu";
3384 case XM_IA_SELECT:
3385 case XM_NO_SELECT:
3386 entry = Qnil;
3387 break;
3389 XMenuDestroy (FRAME_X_DISPLAY (f), menu);
3391 #ifdef HAVE_X_WINDOWS
3392 /* State that no mouse buttons are now held.
3393 (The oldXMenu code doesn't track this info for us.)
3394 That is not necessarily true, but the fiction leads to reasonable
3395 results, and it is a pain to ask which are actually held now. */
3396 FRAME_X_DISPLAY_INFO (f)->grabbed = 0;
3397 #endif
3399 return entry;
3402 #endif /* not USE_X_TOOLKIT */
3404 #endif /* HAVE_MENUS */
3406 void
3407 syms_of_xmenu ()
3409 staticpro (&menu_items);
3410 menu_items = Qnil;
3411 menu_items_inuse = Qnil;
3413 Qdebug_on_next_call = intern ("debug-on-next-call");
3414 staticpro (&Qdebug_on_next_call);
3416 DEFVAR_LISP ("menu-updating-frame", &Vmenu_updating_frame,
3417 doc: /* Frame for which we are updating a menu.
3418 The enable predicate for a menu command should check this variable. */);
3419 Vmenu_updating_frame = Qnil;
3421 #ifdef USE_X_TOOLKIT
3422 widget_id_tick = (1<<16);
3423 next_menubar_widget_id = 1;
3424 #endif
3426 defsubr (&Sx_popup_menu);
3427 #ifdef HAVE_MENUS
3428 defsubr (&Sx_popup_dialog);
3429 #endif
3432 /* arch-tag: 92ea573c-398e-496e-ac73-2436f7d63242
3433 (do not change this comment) */