*** empty log message ***
[emacs.git] / src / xmenu.c
blobd6ee9056a55286bee183460dd79f40877e08140d
1 /* X Communication module for terminals which understand the X protocol.
2 Copyright (C) 1986, 88, 93, 94, 96, 99, 2000, 2001
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 gnuemacs.
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>
40 #include "lisp.h"
41 #include "termhooks.h"
42 #include "keyboard.h"
43 #include "frame.h"
44 #include "window.h"
45 #include "blockinput.h"
46 #include "buffer.h"
48 #ifdef MSDOS
49 #include "msdos.h"
50 #endif
52 #ifdef HAVE_X_WINDOWS
53 /* This may include sys/types.h, and that somehow loses
54 if this is not done before the other system files. */
55 #include "xterm.h"
56 #endif
58 /* Load sys/types.h if not already loaded.
59 In some systems loading it twice is suicidal. */
60 #ifndef makedev
61 #include <sys/types.h>
62 #endif
64 #include "dispextern.h"
66 #ifdef HAVE_X_WINDOWS
67 #undef HAVE_MULTILINGUAL_MENU
68 #ifdef USE_X_TOOLKIT
69 #include "widget.h"
70 #include <X11/Xlib.h>
71 #include <X11/IntrinsicP.h>
72 #include <X11/CoreP.h>
73 #include <X11/StringDefs.h>
74 #include <X11/Shell.h>
75 #ifdef USE_LUCID
76 #include <X11/Xaw/Paned.h>
77 #endif /* USE_LUCID */
78 #include "../lwlib/lwlib.h"
79 #else /* not USE_X_TOOLKIT */
80 #include "../oldXMenu/XMenu.h"
81 #endif /* not USE_X_TOOLKIT */
82 #endif /* HAVE_X_WINDOWS */
84 #define min(x,y) (((x) < (y)) ? (x) : (y))
85 #define max(x,y) (((x) > (y)) ? (x) : (y))
87 #ifndef TRUE
88 #define TRUE 1
89 #define FALSE 0
90 #endif /* no TRUE */
92 Lisp_Object Vmenu_updating_frame;
94 Lisp_Object Qdebug_on_next_call;
96 extern Lisp_Object Qmenu_bar;
97 extern Lisp_Object Qmouse_click, Qevent_kind;
99 extern Lisp_Object QCtoggle, QCradio;
101 extern Lisp_Object Voverriding_local_map;
102 extern Lisp_Object Voverriding_local_map_menu_flag;
104 extern Lisp_Object Qoverriding_local_map, Qoverriding_terminal_local_map;
106 extern Lisp_Object Qmenu_bar_update_hook;
108 #ifdef USE_X_TOOLKIT
109 extern void set_frame_menubar ();
110 extern void process_expose_from_menu ();
111 extern XtAppContext Xt_app_con;
113 static Lisp_Object xdialog_show ();
114 void popup_get_selection ();
115 #endif
117 #ifdef USE_X_TOOLKIT
119 /* Define HAVE_BOXES if meus can handle radio and toggle buttons. */
121 #define HAVE_BOXES 1
122 #endif
124 static void push_menu_item P_ ((Lisp_Object, Lisp_Object, Lisp_Object,
125 Lisp_Object, Lisp_Object, Lisp_Object,
126 Lisp_Object, Lisp_Object));
127 static int update_frame_menubar P_ ((struct frame *));
128 static Lisp_Object xmenu_show P_ ((struct frame *, int, int, int, int,
129 Lisp_Object, char **));
130 static void keymap_panes P_ ((Lisp_Object *, int, int));
131 static void single_keymap_panes P_ ((Lisp_Object, Lisp_Object, Lisp_Object,
132 int, int));
133 static void single_menu_item P_ ((Lisp_Object, Lisp_Object, Lisp_Object *,
134 int, int, int *));
135 static void list_of_panes P_ ((Lisp_Object));
136 static void list_of_items P_ ((Lisp_Object));
138 /* This holds a Lisp vector that holds the results of decoding
139 the keymaps or alist-of-alists that specify a menu.
141 It describes the panes and items within the panes.
143 Each pane is described by 3 elements in the vector:
144 t, the pane name, the pane's prefix key.
145 Then follow the pane's items, with 5 elements per item:
146 the item string, the enable flag, the item's value,
147 the definition, and the equivalent keyboard key's description string.
149 In some cases, multiple levels of menus may be described.
150 A single vector slot containing nil indicates the start of a submenu.
151 A single vector slot containing lambda indicates the end of a submenu.
152 The submenu follows a menu item which is the way to reach the submenu.
154 A single vector slot containing quote indicates that the
155 following items should appear on the right of a dialog box.
157 Using a Lisp vector to hold this information while we decode it
158 takes care of protecting all the data from GC. */
160 #define MENU_ITEMS_PANE_NAME 1
161 #define MENU_ITEMS_PANE_PREFIX 2
162 #define MENU_ITEMS_PANE_LENGTH 3
164 enum menu_item_idx
166 MENU_ITEMS_ITEM_NAME = 0,
167 MENU_ITEMS_ITEM_ENABLE,
168 MENU_ITEMS_ITEM_VALUE,
169 MENU_ITEMS_ITEM_EQUIV_KEY,
170 MENU_ITEMS_ITEM_DEFINITION,
171 MENU_ITEMS_ITEM_TYPE,
172 MENU_ITEMS_ITEM_SELECTED,
173 MENU_ITEMS_ITEM_HELP,
174 MENU_ITEMS_ITEM_LENGTH
177 static Lisp_Object menu_items;
179 /* Number of slots currently allocated in menu_items. */
180 static int menu_items_allocated;
182 /* This is the index in menu_items of the first empty slot. */
183 static int menu_items_used;
185 /* The number of panes currently recorded in menu_items,
186 excluding those within submenus. */
187 static int menu_items_n_panes;
189 /* Current depth within submenus. */
190 static int menu_items_submenu_depth;
192 /* Flag which when set indicates a dialog or menu has been posted by
193 Xt on behalf of one of the widget sets. */
194 int popup_activated_flag;
196 static int next_menubar_widget_id;
198 /* This is set nonzero after the user activates the menu bar, and set
199 to zero again after the menu bars are redisplayed by prepare_menu_bar.
200 While it is nonzero, all calls to set_frame_menubar go deep.
202 I don't understand why this is needed, but it does seem to be
203 needed on Motif, according to Marcus Daniels <marcus@sysc.pdx.edu>. */
205 int pending_menu_activation;
207 #ifdef USE_X_TOOLKIT
209 /* Return the frame whose ->output_data.x->id equals ID, or 0 if none. */
211 static struct frame *
212 menubar_id_to_frame (id)
213 LWLIB_ID id;
215 Lisp_Object tail, frame;
216 FRAME_PTR f;
218 for (tail = Vframe_list; GC_CONSP (tail); tail = XCDR (tail))
220 frame = XCAR (tail);
221 if (!GC_FRAMEP (frame))
222 continue;
223 f = XFRAME (frame);
224 if (!FRAME_WINDOW_P (f))
225 continue;
226 if (f->output_data.x->id == id)
227 return f;
229 return 0;
232 #endif
234 /* Initialize the menu_items structure if we haven't already done so.
235 Also mark it as currently empty. */
237 static void
238 init_menu_items ()
240 if (NILP (menu_items))
242 menu_items_allocated = 60;
243 menu_items = Fmake_vector (make_number (menu_items_allocated), Qnil);
246 menu_items_used = 0;
247 menu_items_n_panes = 0;
248 menu_items_submenu_depth = 0;
251 /* Call at the end of generating the data in menu_items.
252 This fills in the number of items in the last pane. */
254 static void
255 finish_menu_items ()
259 /* Call when finished using the data for the current menu
260 in menu_items. */
262 static void
263 discard_menu_items ()
265 /* Free the structure if it is especially large.
266 Otherwise, hold on to it, to save time. */
267 if (menu_items_allocated > 200)
269 menu_items = Qnil;
270 menu_items_allocated = 0;
274 /* Make the menu_items vector twice as large. */
276 static void
277 grow_menu_items ()
279 Lisp_Object old;
280 int old_size = menu_items_allocated;
281 old = menu_items;
283 menu_items_allocated *= 2;
284 menu_items = Fmake_vector (make_number (menu_items_allocated), Qnil);
285 bcopy (XVECTOR (old)->contents, XVECTOR (menu_items)->contents,
286 old_size * sizeof (Lisp_Object));
289 /* Begin a submenu. */
291 static void
292 push_submenu_start ()
294 if (menu_items_used + 1 > menu_items_allocated)
295 grow_menu_items ();
297 XVECTOR (menu_items)->contents[menu_items_used++] = Qnil;
298 menu_items_submenu_depth++;
301 /* End a submenu. */
303 static void
304 push_submenu_end ()
306 if (menu_items_used + 1 > menu_items_allocated)
307 grow_menu_items ();
309 XVECTOR (menu_items)->contents[menu_items_used++] = Qlambda;
310 menu_items_submenu_depth--;
313 /* Indicate boundary between left and right. */
315 static void
316 push_left_right_boundary ()
318 if (menu_items_used + 1 > menu_items_allocated)
319 grow_menu_items ();
321 XVECTOR (menu_items)->contents[menu_items_used++] = Qquote;
324 /* Start a new menu pane in menu_items..
325 NAME is the pane name. PREFIX_VEC is a prefix key for this pane. */
327 static void
328 push_menu_pane (name, prefix_vec)
329 Lisp_Object name, prefix_vec;
331 if (menu_items_used + MENU_ITEMS_PANE_LENGTH > menu_items_allocated)
332 grow_menu_items ();
334 if (menu_items_submenu_depth == 0)
335 menu_items_n_panes++;
336 XVECTOR (menu_items)->contents[menu_items_used++] = Qt;
337 XVECTOR (menu_items)->contents[menu_items_used++] = name;
338 XVECTOR (menu_items)->contents[menu_items_used++] = prefix_vec;
341 /* Push one menu item into the current pane. NAME is the string to
342 display. ENABLE if non-nil means this item can be selected. KEY
343 is the key generated by choosing this item, or nil if this item
344 doesn't really have a definition. DEF is the definition of this
345 item. EQUIV is the textual description of the keyboard equivalent
346 for this item (or nil if none). TYPE is the type of this menu
347 item, one of nil, `toggle' or `radio'. */
349 static void
350 push_menu_item (name, enable, key, def, equiv, type, selected, help)
351 Lisp_Object name, enable, key, def, equiv, type, selected, help;
353 if (menu_items_used + MENU_ITEMS_ITEM_LENGTH > menu_items_allocated)
354 grow_menu_items ();
356 XVECTOR (menu_items)->contents[menu_items_used++] = name;
357 XVECTOR (menu_items)->contents[menu_items_used++] = enable;
358 XVECTOR (menu_items)->contents[menu_items_used++] = key;
359 XVECTOR (menu_items)->contents[menu_items_used++] = equiv;
360 XVECTOR (menu_items)->contents[menu_items_used++] = def;
361 XVECTOR (menu_items)->contents[menu_items_used++] = type;
362 XVECTOR (menu_items)->contents[menu_items_used++] = selected;
363 XVECTOR (menu_items)->contents[menu_items_used++] = help;
366 /* Look through KEYMAPS, a vector of keymaps that is NMAPS long,
367 and generate menu panes for them in menu_items.
368 If NOTREAL is nonzero,
369 don't bother really computing whether an item is enabled. */
371 static void
372 keymap_panes (keymaps, nmaps, notreal)
373 Lisp_Object *keymaps;
374 int nmaps;
375 int notreal;
377 int mapno;
379 init_menu_items ();
381 /* Loop over the given keymaps, making a pane for each map.
382 But don't make a pane that is empty--ignore that map instead.
383 P is the number of panes we have made so far. */
384 for (mapno = 0; mapno < nmaps; mapno++)
385 single_keymap_panes (keymaps[mapno],
386 map_prompt (keymaps[mapno]), Qnil, notreal, 10);
388 finish_menu_items ();
391 /* This is a recursive subroutine of keymap_panes.
392 It handles one keymap, KEYMAP.
393 The other arguments are passed along
394 or point to local variables of the previous function.
395 If NOTREAL is nonzero, only check for equivalent key bindings, don't
396 evaluate expressions in menu items and don't make any menu.
398 If we encounter submenus deeper than MAXDEPTH levels, ignore them. */
400 static void
401 single_keymap_panes (keymap, pane_name, prefix, notreal, maxdepth)
402 Lisp_Object keymap;
403 Lisp_Object pane_name;
404 Lisp_Object prefix;
405 int notreal;
406 int maxdepth;
408 Lisp_Object pending_maps = Qnil;
409 Lisp_Object tail, item;
410 struct gcpro gcpro1, gcpro2;
411 int notbuttons = 0;
413 if (maxdepth <= 0)
414 return;
416 push_menu_pane (pane_name, prefix);
418 #ifndef HAVE_BOXES
419 /* Remember index for first item in this pane so we can go back and
420 add a prefix when (if) we see the first button. After that, notbuttons
421 is set to 0, to mark that we have seen a button and all non button
422 items need a prefix. */
423 notbuttons = menu_items_used;
424 #endif
426 for (tail = keymap; CONSP (tail); tail = XCDR (tail))
428 GCPRO2 (keymap, pending_maps);
429 /* Look at each key binding, and if it is a menu item add it
430 to this menu. */
431 item = XCAR (tail);
432 if (CONSP (item))
433 single_menu_item (XCAR (item), XCDR (item),
434 &pending_maps, notreal, maxdepth, &notbuttons);
435 else if (VECTORP (item))
437 /* Loop over the char values represented in the vector. */
438 int len = XVECTOR (item)->size;
439 int c;
440 for (c = 0; c < len; c++)
442 Lisp_Object character;
443 XSETFASTINT (character, c);
444 single_menu_item (character, XVECTOR (item)->contents[c],
445 &pending_maps, notreal, maxdepth, &notbuttons);
448 UNGCPRO;
451 /* Process now any submenus which want to be panes at this level. */
452 while (!NILP (pending_maps))
454 Lisp_Object elt, eltcdr, string;
455 elt = Fcar (pending_maps);
456 eltcdr = XCDR (elt);
457 string = XCAR (eltcdr);
458 /* We no longer discard the @ from the beginning of the string here.
459 Instead, we do this in xmenu_show. */
460 single_keymap_panes (Fcar (elt), string,
461 XCDR (eltcdr), notreal, maxdepth - 1);
462 pending_maps = Fcdr (pending_maps);
466 /* This is a subroutine of single_keymap_panes that handles one
467 keymap entry.
468 KEY is a key in a keymap and ITEM is its binding.
469 PENDING_MAPS_PTR points to a list of keymaps waiting to be made into
470 separate panes.
471 If NOTREAL is nonzero, only check for equivalent key bindings, don't
472 evaluate expressions in menu items and don't make any menu.
473 If we encounter submenus deeper than MAXDEPTH levels, ignore them.
474 NOTBUTTONS_PTR is only used when simulating toggle boxes and radio
475 buttons. It points to variable notbuttons in single_keymap_panes,
476 which keeps track of if we have seen a button in this menu or not. */
478 static void
479 single_menu_item (key, item, pending_maps_ptr, notreal, maxdepth,
480 notbuttons_ptr)
481 Lisp_Object key, item;
482 Lisp_Object *pending_maps_ptr;
483 int maxdepth, notreal;
484 int *notbuttons_ptr;
486 Lisp_Object map, item_string, enabled;
487 struct gcpro gcpro1, gcpro2;
488 int res;
490 /* Parse the menu item and leave the result in item_properties. */
491 GCPRO2 (key, item);
492 res = parse_menu_item (item, notreal, 0);
493 UNGCPRO;
494 if (!res)
495 return; /* Not a menu item. */
497 map = XVECTOR (item_properties)->contents[ITEM_PROPERTY_MAP];
499 if (notreal)
501 /* We don't want to make a menu, just traverse the keymaps to
502 precompute equivalent key bindings. */
503 if (!NILP (map))
504 single_keymap_panes (map, Qnil, key, 1, maxdepth - 1);
505 return;
508 enabled = XVECTOR (item_properties)->contents[ITEM_PROPERTY_ENABLE];
509 item_string = XVECTOR (item_properties)->contents[ITEM_PROPERTY_NAME];
511 if (!NILP (map) && XSTRING (item_string)->data[0] == '@')
513 if (!NILP (enabled))
514 /* An enabled separate pane. Remember this to handle it later. */
515 *pending_maps_ptr = Fcons (Fcons (map, Fcons (item_string, key)),
516 *pending_maps_ptr);
517 return;
520 #ifndef HAVE_BOXES
521 /* Simulate radio buttons and toggle boxes by putting a prefix in
522 front of them. */
524 Lisp_Object prefix = Qnil;
525 Lisp_Object type = XVECTOR (item_properties)->contents[ITEM_PROPERTY_TYPE];
526 if (!NILP (type))
528 Lisp_Object selected
529 = XVECTOR (item_properties)->contents[ITEM_PROPERTY_SELECTED];
531 if (*notbuttons_ptr)
532 /* The first button. Line up previous items in this menu. */
534 int index = *notbuttons_ptr; /* Index for first item this menu. */
535 int submenu = 0;
536 Lisp_Object tem;
537 while (index < menu_items_used)
540 = XVECTOR (menu_items)->contents[index + MENU_ITEMS_ITEM_NAME];
541 if (NILP (tem))
543 index++;
544 submenu++; /* Skip sub menu. */
546 else if (EQ (tem, Qlambda))
548 index++;
549 submenu--; /* End sub menu. */
551 else if (EQ (tem, Qt))
552 index += 3; /* Skip new pane marker. */
553 else if (EQ (tem, Qquote))
554 index++; /* Skip a left, right divider. */
555 else
557 if (!submenu && XSTRING (tem)->data[0] != '\0'
558 && XSTRING (tem)->data[0] != '-')
559 XVECTOR (menu_items)->contents[index + MENU_ITEMS_ITEM_NAME]
560 = concat2 (build_string (" "), tem);
561 index += MENU_ITEMS_ITEM_LENGTH;
564 *notbuttons_ptr = 0;
567 /* Calculate prefix, if any, for this item. */
568 if (EQ (type, QCtoggle))
569 prefix = build_string (NILP (selected) ? "[ ] " : "[X] ");
570 else if (EQ (type, QCradio))
571 prefix = build_string (NILP (selected) ? "( ) " : "(*) ");
573 /* Not a button. If we have earlier buttons, then we need a prefix. */
574 else if (!*notbuttons_ptr && XSTRING (item_string)->data[0] != '\0'
575 && XSTRING (item_string)->data[0] != '-')
576 prefix = build_string (" ");
578 if (!NILP (prefix))
579 item_string = concat2 (prefix, item_string);
581 #endif /* not HAVE_BOXES */
583 #ifndef USE_X_TOOLKIT
584 if (!NILP(map))
585 /* Indicate visually that this is a submenu. */
586 item_string = concat2 (item_string, build_string (" >"));
587 #endif
589 push_menu_item (item_string, enabled, key,
590 XVECTOR (item_properties)->contents[ITEM_PROPERTY_DEF],
591 XVECTOR (item_properties)->contents[ITEM_PROPERTY_KEYEQ],
592 XVECTOR (item_properties)->contents[ITEM_PROPERTY_TYPE],
593 XVECTOR (item_properties)->contents[ITEM_PROPERTY_SELECTED],
594 XVECTOR (item_properties)->contents[ITEM_PROPERTY_HELP]);
596 #ifdef USE_X_TOOLKIT
597 /* Display a submenu using the toolkit. */
598 if (! (NILP (map) || NILP (enabled)))
600 push_submenu_start ();
601 single_keymap_panes (map, Qnil, key, 0, maxdepth - 1);
602 push_submenu_end ();
604 #endif
607 /* Push all the panes and items of a menu described by the
608 alist-of-alists MENU.
609 This handles old-fashioned calls to x-popup-menu. */
611 static void
612 list_of_panes (menu)
613 Lisp_Object menu;
615 Lisp_Object tail;
617 init_menu_items ();
619 for (tail = menu; !NILP (tail); tail = Fcdr (tail))
621 Lisp_Object elt, pane_name, pane_data;
622 elt = Fcar (tail);
623 pane_name = Fcar (elt);
624 CHECK_STRING (pane_name, 0);
625 push_menu_pane (pane_name, Qnil);
626 pane_data = Fcdr (elt);
627 CHECK_CONS (pane_data, 0);
628 list_of_items (pane_data);
631 finish_menu_items ();
634 /* Push the items in a single pane defined by the alist PANE. */
636 static void
637 list_of_items (pane)
638 Lisp_Object pane;
640 Lisp_Object tail, item, item1;
642 for (tail = pane; !NILP (tail); tail = Fcdr (tail))
644 item = Fcar (tail);
645 if (STRINGP (item))
646 push_menu_item (item, Qnil, Qnil, Qt, Qnil, Qnil, Qnil, Qnil);
647 else if (NILP (item))
648 push_left_right_boundary ();
649 else
651 CHECK_CONS (item, 0);
652 item1 = Fcar (item);
653 CHECK_STRING (item1, 1);
654 push_menu_item (item1, Qt, Fcdr (item), Qt, Qnil, Qnil, Qnil, Qnil);
659 DEFUN ("x-popup-menu", Fx_popup_menu, Sx_popup_menu, 2, 2, 0,
660 "Pop up a deck-of-cards menu and return user's selection.\n\
661 POSITION is a position specification. This is either a mouse button event\n\
662 or a list ((XOFFSET YOFFSET) WINDOW)\n\
663 where XOFFSET and YOFFSET are positions in pixels from the top left\n\
664 corner of WINDOW's frame. (WINDOW may be a frame object instead of a window.)\n\
665 This controls the position of the center of the first line\n\
666 in the first pane of the menu, not the top left of the menu as a whole.\n\
667 If POSITION is t, it means to use the current mouse position.\n\
669 MENU is a specifier for a menu. For the simplest case, MENU is a keymap.\n\
670 The menu items come from key bindings that have a menu string as well as\n\
671 a definition; actually, the \"definition\" in such a key binding looks like\n\
672 \(STRING . REAL-DEFINITION). To give the menu a title, put a string into\n\
673 the keymap as a top-level element.\n\n\
674 If REAL-DEFINITION is nil, that puts a nonselectable string in the menu.\n\
675 Otherwise, REAL-DEFINITION should be a valid key binding definition.\n\
677 You can also use a list of keymaps as MENU.\n\
678 Then each keymap makes a separate pane.\n\
679 When MENU is a keymap or a list of keymaps, the return value\n\
680 is a list of events.\n\n\
682 Alternatively, you can specify a menu of multiple panes\n\
683 with a list of the form (TITLE PANE1 PANE2...),\n\
684 where each pane is a list of form (TITLE ITEM1 ITEM2...).\n\
685 Each ITEM is normally a cons cell (STRING . VALUE);\n\
686 but a string can appear as an item--that makes a nonselectable line\n\
687 in the menu.\n\
688 With this form of menu, the return value is VALUE from the chosen item.\n\
690 If POSITION is nil, don't display the menu at all, just precalculate the\n\
691 cached information about equivalent key sequences.")
692 (position, menu)
693 Lisp_Object position, menu;
695 Lisp_Object keymap, tem;
696 int xpos = 0, ypos = 0;
697 Lisp_Object title;
698 char *error_name;
699 Lisp_Object selection;
700 struct frame *f = NULL;
701 Lisp_Object x, y, window;
702 int keymaps = 0;
703 int for_click = 0;
704 struct gcpro gcpro1;
706 #ifdef HAVE_MENUS
707 if (! NILP (position))
709 check_x ();
711 /* Decode the first argument: find the window and the coordinates. */
712 if (EQ (position, Qt)
713 || (CONSP (position) && (EQ (XCAR (position), Qmenu_bar)
714 || EQ (XCAR (position), Qtool_bar))))
716 /* Use the mouse's current position. */
717 FRAME_PTR new_f = SELECTED_FRAME ();
718 Lisp_Object bar_window;
719 enum scroll_bar_part part;
720 unsigned long time;
722 if (mouse_position_hook)
723 (*mouse_position_hook) (&new_f, 1, &bar_window,
724 &part, &x, &y, &time);
725 if (new_f != 0)
726 XSETFRAME (window, new_f);
727 else
729 window = selected_window;
730 XSETFASTINT (x, 0);
731 XSETFASTINT (y, 0);
734 else
736 tem = Fcar (position);
737 if (CONSP (tem))
739 window = Fcar (Fcdr (position));
740 x = Fcar (tem);
741 y = Fcar (Fcdr (tem));
743 else
745 for_click = 1;
746 tem = Fcar (Fcdr (position)); /* EVENT_START (position) */
747 window = Fcar (tem); /* POSN_WINDOW (tem) */
748 tem = Fcar (Fcdr (Fcdr (tem))); /* POSN_WINDOW_POSN (tem) */
749 x = Fcar (tem);
750 y = Fcdr (tem);
754 CHECK_NUMBER (x, 0);
755 CHECK_NUMBER (y, 0);
757 /* Decode where to put the menu. */
759 if (FRAMEP (window))
761 f = XFRAME (window);
762 xpos = 0;
763 ypos = 0;
765 else if (WINDOWP (window))
767 CHECK_LIVE_WINDOW (window, 0);
768 f = XFRAME (WINDOW_FRAME (XWINDOW (window)));
770 xpos = (FONT_WIDTH (f->output_data.x->font)
771 * XFASTINT (XWINDOW (window)->left));
772 ypos = (f->output_data.x->line_height
773 * XFASTINT (XWINDOW (window)->top));
775 else
776 /* ??? Not really clean; should be CHECK_WINDOW_OR_FRAME,
777 but I don't want to make one now. */
778 CHECK_WINDOW (window, 0);
780 xpos += XINT (x);
781 ypos += XINT (y);
783 XSETFRAME (Vmenu_updating_frame, f);
785 Vmenu_updating_frame = Qnil;
786 #endif /* HAVE_MENUS */
788 title = Qnil;
789 GCPRO1 (title);
791 /* Decode the menu items from what was specified. */
793 keymap = get_keymap (menu, 0, 0);
794 if (CONSP (keymap))
796 /* We were given a keymap. Extract menu info from the keymap. */
797 Lisp_Object prompt;
799 /* Extract the detailed info to make one pane. */
800 keymap_panes (&menu, 1, NILP (position));
802 /* Search for a string appearing directly as an element of the keymap.
803 That string is the title of the menu. */
804 prompt = map_prompt (keymap);
805 if (NILP (title) && !NILP (prompt))
806 title = prompt;
808 /* Make that be the pane title of the first pane. */
809 if (!NILP (prompt) && menu_items_n_panes >= 0)
810 XVECTOR (menu_items)->contents[MENU_ITEMS_PANE_NAME] = prompt;
812 keymaps = 1;
814 else if (CONSP (menu) && KEYMAPP (XCAR (menu)))
816 /* We were given a list of keymaps. */
817 int nmaps = XFASTINT (Flength (menu));
818 Lisp_Object *maps
819 = (Lisp_Object *) alloca (nmaps * sizeof (Lisp_Object));
820 int i;
822 title = Qnil;
824 /* The first keymap that has a prompt string
825 supplies the menu title. */
826 for (tem = menu, i = 0; CONSP (tem); tem = Fcdr (tem))
828 Lisp_Object prompt;
830 maps[i++] = keymap = get_keymap (Fcar (tem), 1, 0);
832 prompt = map_prompt (keymap);
833 if (NILP (title) && !NILP (prompt))
834 title = prompt;
837 /* Extract the detailed info to make one pane. */
838 keymap_panes (maps, nmaps, NILP (position));
840 /* Make the title be the pane title of the first pane. */
841 if (!NILP (title) && menu_items_n_panes >= 0)
842 XVECTOR (menu_items)->contents[MENU_ITEMS_PANE_NAME] = title;
844 keymaps = 1;
846 else
848 /* We were given an old-fashioned menu. */
849 title = Fcar (menu);
850 CHECK_STRING (title, 1);
852 list_of_panes (Fcdr (menu));
854 keymaps = 0;
857 if (NILP (position))
859 discard_menu_items ();
860 UNGCPRO;
861 return Qnil;
864 #ifdef HAVE_MENUS
865 /* Display them in a menu. */
866 BLOCK_INPUT;
868 selection = xmenu_show (f, xpos, ypos, for_click,
869 keymaps, title, &error_name);
870 UNBLOCK_INPUT;
872 discard_menu_items ();
874 UNGCPRO;
875 #endif /* HAVE_MENUS */
877 if (error_name) error (error_name);
878 return selection;
881 #ifdef HAVE_MENUS
883 DEFUN ("x-popup-dialog", Fx_popup_dialog, Sx_popup_dialog, 2, 2, 0,
884 "Pop up a dialog box and return user's selection.\n\
885 POSITION specifies which frame to use.\n\
886 This is normally a mouse button event or a window or frame.\n\
887 If POSITION is t, it means to use the frame the mouse is on.\n\
888 The dialog box appears in the middle of the specified frame.\n\
890 CONTENTS specifies the alternatives to display in the dialog box.\n\
891 It is a list of the form (TITLE ITEM1 ITEM2...).\n\
892 Each ITEM is a cons cell (STRING . VALUE).\n\
893 The return value is VALUE from the chosen item.\n\n\
894 An ITEM may also be just a string--that makes a nonselectable item.\n\
895 An ITEM may also be nil--that means to put all preceding items\n\
896 on the left of the dialog box and all following items on the right.\n\
897 \(By default, approximately half appear on each side.)")
898 (position, contents)
899 Lisp_Object position, contents;
901 struct frame * f = NULL;
902 Lisp_Object window;
904 check_x ();
906 /* Decode the first argument: find the window or frame to use. */
907 if (EQ (position, Qt)
908 || (CONSP (position) && (EQ (XCAR (position), Qmenu_bar)
909 || EQ (XCAR (position), Qtool_bar))))
911 #if 0 /* Using the frame the mouse is on may not be right. */
912 /* Use the mouse's current position. */
913 FRAME_PTR new_f = SELECTED_FRAME ();
914 Lisp_Object bar_window;
915 int part;
916 unsigned long time;
917 Lisp_Object x, y;
919 (*mouse_position_hook) (&new_f, 1, &bar_window, &part, &x, &y, &time);
921 if (new_f != 0)
922 XSETFRAME (window, new_f);
923 else
924 window = selected_window;
925 #endif
926 window = selected_window;
928 else if (CONSP (position))
930 Lisp_Object tem;
931 tem = Fcar (position);
932 if (CONSP (tem))
933 window = Fcar (Fcdr (position));
934 else
936 tem = Fcar (Fcdr (position)); /* EVENT_START (position) */
937 window = Fcar (tem); /* POSN_WINDOW (tem) */
940 else if (WINDOWP (position) || FRAMEP (position))
941 window = position;
942 else
943 window = Qnil;
945 /* Decode where to put the menu. */
947 if (FRAMEP (window))
948 f = XFRAME (window);
949 else if (WINDOWP (window))
951 CHECK_LIVE_WINDOW (window, 0);
952 f = XFRAME (WINDOW_FRAME (XWINDOW (window)));
954 else
955 /* ??? Not really clean; should be CHECK_WINDOW_OR_FRAME,
956 but I don't want to make one now. */
957 CHECK_WINDOW (window, 0);
959 #ifndef USE_X_TOOLKIT
960 /* Display a menu with these alternatives
961 in the middle of frame F. */
963 Lisp_Object x, y, frame, newpos;
964 XSETFRAME (frame, f);
965 XSETINT (x, x_pixel_width (f) / 2);
966 XSETINT (y, x_pixel_height (f) / 2);
967 newpos = Fcons (Fcons (x, Fcons (y, Qnil)), Fcons (frame, Qnil));
969 return Fx_popup_menu (newpos,
970 Fcons (Fcar (contents), Fcons (contents, Qnil)));
972 #else
974 Lisp_Object title;
975 char *error_name;
976 Lisp_Object selection;
978 /* Decode the dialog items from what was specified. */
979 title = Fcar (contents);
980 CHECK_STRING (title, 1);
982 list_of_panes (Fcons (contents, Qnil));
984 /* Display them in a dialog box. */
985 BLOCK_INPUT;
986 selection = xdialog_show (f, 0, title, &error_name);
987 UNBLOCK_INPUT;
989 discard_menu_items ();
991 if (error_name) error (error_name);
992 return selection;
994 #endif
997 #ifdef USE_X_TOOLKIT
999 /* Loop in Xt until the menu pulldown or dialog popup has been
1000 popped down (deactivated). This is used for x-popup-menu
1001 and x-popup-dialog; it is not used for the menu bar any more.
1003 NOTE: All calls to popup_get_selection should be protected
1004 with BLOCK_INPUT, UNBLOCK_INPUT wrappers. */
1006 void
1007 popup_get_selection (initial_event, dpyinfo, id)
1008 XEvent *initial_event;
1009 struct x_display_info *dpyinfo;
1010 LWLIB_ID id;
1012 XEvent event;
1014 /* Define a queue to save up for later unreading
1015 all X events that don't pertain to the menu. */
1016 struct event_queue
1018 XEvent event;
1019 struct event_queue *next;
1022 struct event_queue *queue = NULL;
1023 struct event_queue *queue_tmp;
1025 if (initial_event)
1026 event = *initial_event;
1027 else
1028 XtAppNextEvent (Xt_app_con, &event);
1030 while (1)
1032 /* Handle expose events for editor frames right away. */
1033 if (event.type == Expose)
1034 process_expose_from_menu (event);
1035 /* Make sure we don't consider buttons grabbed after menu goes.
1036 And make sure to deactivate for any ButtonRelease,
1037 even if XtDispatchEvent doesn't do that. */
1038 else if (event.type == ButtonRelease
1039 && dpyinfo->display == event.xbutton.display)
1041 dpyinfo->grabbed &= ~(1 << event.xbutton.button);
1042 popup_activated_flag = 0;
1043 #ifdef USE_MOTIF /* Pretending that the event came from a
1044 Btn1Down seems the only way to convince Motif to
1045 activate its callbacks; setting the XmNmenuPost
1046 isn't working. --marcus@sysc.pdx.edu. */
1047 event.xbutton.button = 1;
1048 #endif
1050 /* If the user presses a key, deactivate the menu.
1051 The user is likely to do that if we get wedged. */
1052 else if (event.type == KeyPress
1053 && dpyinfo->display == event.xbutton.display)
1055 KeySym keysym = XLookupKeysym (&event.xkey, 0);
1056 if (!IsModifierKey (keysym))
1058 popup_activated_flag = 0;
1059 break;
1062 /* Button presses outside the menu also pop it down. */
1063 else if (event.type == ButtonPress
1064 && event.xany.display == dpyinfo->display
1065 && x_any_window_to_frame (dpyinfo, event.xany.window))
1067 popup_activated_flag = 0;
1068 break;
1071 /* Queue all events not for this popup,
1072 except for Expose, which we've already handled, and ButtonRelease.
1073 Note that the X window is associated with the frame if this
1074 is a menu bar popup, but not if it's a dialog box. So we use
1075 x_non_menubar_window_to_frame, not x_any_window_to_frame. */
1076 if (event.type != Expose
1077 && !(event.type == ButtonRelease
1078 && dpyinfo->display == event.xbutton.display)
1079 && (event.xany.display != dpyinfo->display
1080 || x_non_menubar_window_to_frame (dpyinfo, event.xany.window)))
1082 queue_tmp = (struct event_queue *) xmalloc (sizeof *queue_tmp);
1083 queue_tmp->event = event;
1084 queue_tmp->next = queue;
1085 queue = queue_tmp;
1087 else
1088 XtDispatchEvent (&event);
1090 if (!popup_activated ())
1091 break;
1092 XtAppNextEvent (Xt_app_con, &event);
1095 /* Unread any events that we got but did not handle. */
1096 while (queue != NULL)
1098 queue_tmp = queue;
1099 XPutBackEvent (queue_tmp->event.xany.display, &queue_tmp->event);
1100 queue = queue_tmp->next;
1101 xfree ((char *)queue_tmp);
1102 /* Cause these events to get read as soon as we UNBLOCK_INPUT. */
1103 interrupt_input_pending = 1;
1107 /* Activate the menu bar of frame F.
1108 This is called from keyboard.c when it gets the
1109 menu_bar_activate_event out of the Emacs event queue.
1111 To activate the menu bar, we use the X button-press event
1112 that was saved in saved_menu_event.
1113 That makes the toolkit do its thing.
1115 But first we recompute the menu bar contents (the whole tree).
1117 The reason for saving the button event until here, instead of
1118 passing it to the toolkit right away, is that we can safely
1119 execute Lisp code. */
1121 void
1122 x_activate_menubar (f)
1123 FRAME_PTR f;
1125 if (!f->output_data.x->saved_menu_event->type)
1126 return;
1128 set_frame_menubar (f, 0, 1);
1129 BLOCK_INPUT;
1130 XtDispatchEvent ((XEvent *) f->output_data.x->saved_menu_event);
1131 UNBLOCK_INPUT;
1132 #ifdef USE_MOTIF
1133 if (f->output_data.x->saved_menu_event->type == ButtonRelease)
1134 pending_menu_activation = 1;
1135 #endif
1137 /* Ignore this if we get it a second time. */
1138 f->output_data.x->saved_menu_event->type = 0;
1141 /* Detect if a dialog or menu has been posted. */
1144 popup_activated ()
1146 return popup_activated_flag;
1149 /* This callback is invoked when the user selects a menubar cascade
1150 pushbutton, but before the pulldown menu is posted. */
1152 static void
1153 popup_activate_callback (widget, id, client_data)
1154 Widget widget;
1155 LWLIB_ID id;
1156 XtPointer client_data;
1158 popup_activated_flag = 1;
1161 /* This callback is invoked when a dialog or menu is finished being
1162 used and has been unposted. */
1164 static void
1165 popup_deactivate_callback (widget, id, client_data)
1166 Widget widget;
1167 LWLIB_ID id;
1168 XtPointer client_data;
1170 popup_activated_flag = 0;
1173 /* Lwlib callback called when menu items are highlighted/unhighlighted
1174 while moving the mouse over them. WIDGET is the menu bar or menu
1175 popup widget. ID is its LWLIB_ID. CALL_DATA contains a pointer to
1176 the widget_value structure for the menu item, or null in case of
1177 unhighlighting. */
1179 void
1180 menu_highlight_callback (widget, id, call_data)
1181 Widget widget;
1182 LWLIB_ID id;
1183 void *call_data;
1185 widget_value *wv = (widget_value *) call_data;
1186 struct frame *f;
1187 Lisp_Object frame, help;
1189 help = wv && wv->help ? build_string (wv->help) : Qnil;
1191 /* Determine the frame for the help event. */
1192 f = menubar_id_to_frame (id);
1193 if (f)
1195 XSETFRAME (frame, f);
1196 kbd_buffer_store_help_event (frame, help);
1198 else
1200 /* WIDGET is the popup menu. It's parent is the frame's
1201 widget. See which frame that is. */
1202 Widget frame_widget = XtParent (widget);
1203 Lisp_Object tail;
1205 for (tail = Vframe_list; GC_CONSP (tail); tail = XCDR (tail))
1207 frame = XCAR (tail);
1208 if (GC_FRAMEP (frame)
1209 && (f = XFRAME (frame),
1210 FRAME_X_P (f) && f->output_data.x->widget == frame_widget))
1211 break;
1214 show_help_echo (help, Qnil, Qnil, Qnil, 1);
1218 /* This callback is called from the menu bar pulldown menu
1219 when the user makes a selection.
1220 Figure out what the user chose
1221 and put the appropriate events into the keyboard buffer. */
1223 static void
1224 menubar_selection_callback (widget, id, client_data)
1225 Widget widget;
1226 LWLIB_ID id;
1227 XtPointer client_data;
1229 Lisp_Object prefix, entry;
1230 FRAME_PTR f = menubar_id_to_frame (id);
1231 Lisp_Object vector;
1232 Lisp_Object *subprefix_stack;
1233 int submenu_depth = 0;
1234 int i;
1236 if (!f)
1237 return;
1238 entry = Qnil;
1239 subprefix_stack = (Lisp_Object *) alloca (f->menu_bar_items_used * sizeof (Lisp_Object));
1240 vector = f->menu_bar_vector;
1241 prefix = Qnil;
1242 i = 0;
1243 while (i < f->menu_bar_items_used)
1245 if (EQ (XVECTOR (vector)->contents[i], Qnil))
1247 subprefix_stack[submenu_depth++] = prefix;
1248 prefix = entry;
1249 i++;
1251 else if (EQ (XVECTOR (vector)->contents[i], Qlambda))
1253 prefix = subprefix_stack[--submenu_depth];
1254 i++;
1256 else if (EQ (XVECTOR (vector)->contents[i], Qt))
1258 prefix = XVECTOR (vector)->contents[i + MENU_ITEMS_PANE_PREFIX];
1259 i += MENU_ITEMS_PANE_LENGTH;
1261 else
1263 entry = XVECTOR (vector)->contents[i + MENU_ITEMS_ITEM_VALUE];
1264 /* The EMACS_INT cast avoids a warning. There's no problem
1265 as long as pointers have enough bits to hold small integers. */
1266 if ((int) (EMACS_INT) client_data == i)
1268 int j;
1269 struct input_event buf;
1270 Lisp_Object frame;
1272 XSETFRAME (frame, f);
1273 buf.kind = MENU_BAR_EVENT;
1274 buf.frame_or_window = frame;
1275 buf.arg = frame;
1276 kbd_buffer_store_event (&buf);
1278 for (j = 0; j < submenu_depth; j++)
1279 if (!NILP (subprefix_stack[j]))
1281 buf.kind = MENU_BAR_EVENT;
1282 buf.frame_or_window = frame;
1283 buf.arg = subprefix_stack[j];
1284 kbd_buffer_store_event (&buf);
1287 if (!NILP (prefix))
1289 buf.kind = MENU_BAR_EVENT;
1290 buf.frame_or_window = frame;
1291 buf.arg = prefix;
1292 kbd_buffer_store_event (&buf);
1295 buf.kind = MENU_BAR_EVENT;
1296 buf.frame_or_window = frame;
1297 buf.arg = entry;
1298 kbd_buffer_store_event (&buf);
1300 return;
1302 i += MENU_ITEMS_ITEM_LENGTH;
1307 /* Allocate a widget_value, blocking input. */
1309 widget_value *
1310 xmalloc_widget_value ()
1312 widget_value *value;
1314 BLOCK_INPUT;
1315 value = malloc_widget_value ();
1316 UNBLOCK_INPUT;
1318 return value;
1321 /* This recursively calls free_widget_value on the tree of widgets.
1322 It must free all data that was malloc'ed for these widget_values.
1323 In Emacs, many slots are pointers into the data of Lisp_Strings, and
1324 must be left alone. */
1326 void
1327 free_menubar_widget_value_tree (wv)
1328 widget_value *wv;
1330 if (! wv) return;
1332 wv->name = wv->value = wv->key = (char *) 0xDEADBEEF;
1334 if (wv->contents && (wv->contents != (widget_value*)1))
1336 free_menubar_widget_value_tree (wv->contents);
1337 wv->contents = (widget_value *) 0xDEADBEEF;
1339 if (wv->next)
1341 free_menubar_widget_value_tree (wv->next);
1342 wv->next = (widget_value *) 0xDEADBEEF;
1344 BLOCK_INPUT;
1345 free_widget_value (wv);
1346 UNBLOCK_INPUT;
1349 /* Return a tree of widget_value structures for a menu bar item
1350 whose event type is ITEM_KEY (with string ITEM_NAME)
1351 and whose contents come from the list of keymaps MAPS. */
1353 static widget_value *
1354 single_submenu (item_key, item_name, maps)
1355 Lisp_Object item_key, item_name, maps;
1357 widget_value *wv, *prev_wv, *save_wv, *first_wv;
1358 int i;
1359 int submenu_depth = 0;
1360 Lisp_Object length;
1361 int len;
1362 Lisp_Object *mapvec;
1363 widget_value **submenu_stack;
1364 int previous_items = menu_items_used;
1365 int top_level_items = 0;
1367 length = Flength (maps);
1368 len = XINT (length);
1370 /* Convert the list MAPS into a vector MAPVEC. */
1371 mapvec = (Lisp_Object *) alloca (len * sizeof (Lisp_Object));
1372 for (i = 0; i < len; i++)
1374 mapvec[i] = Fcar (maps);
1375 maps = Fcdr (maps);
1378 menu_items_n_panes = 0;
1380 /* Loop over the given keymaps, making a pane for each map.
1381 But don't make a pane that is empty--ignore that map instead. */
1382 for (i = 0; i < len; i++)
1384 if (SYMBOLP (mapvec[i])
1385 || (CONSP (mapvec[i]) && !KEYMAPP (mapvec[i])))
1387 /* Here we have a command at top level in the menu bar
1388 as opposed to a submenu. */
1389 top_level_items = 1;
1390 push_menu_pane (Qnil, Qnil);
1391 push_menu_item (item_name, Qt, item_key, mapvec[i],
1392 Qnil, Qnil, Qnil, Qnil);
1394 else
1395 single_keymap_panes (mapvec[i], item_name, item_key, 0, 10);
1398 /* Create a tree of widget_value objects
1399 representing the panes and their items. */
1401 submenu_stack
1402 = (widget_value **) alloca (menu_items_used * sizeof (widget_value *));
1403 wv = xmalloc_widget_value ();
1404 wv->name = "menu";
1405 wv->value = 0;
1406 wv->enabled = 1;
1407 wv->button_type = BUTTON_TYPE_NONE;
1408 first_wv = wv;
1409 save_wv = 0;
1410 prev_wv = 0;
1412 /* Loop over all panes and items made during this call
1413 and construct a tree of widget_value objects.
1414 Ignore the panes and items made by previous calls to
1415 single_submenu, even though those are also in menu_items. */
1416 i = previous_items;
1417 while (i < menu_items_used)
1419 if (EQ (XVECTOR (menu_items)->contents[i], Qnil))
1421 submenu_stack[submenu_depth++] = save_wv;
1422 save_wv = prev_wv;
1423 prev_wv = 0;
1424 i++;
1426 else if (EQ (XVECTOR (menu_items)->contents[i], Qlambda))
1428 prev_wv = save_wv;
1429 save_wv = submenu_stack[--submenu_depth];
1430 i++;
1432 else if (EQ (XVECTOR (menu_items)->contents[i], Qt)
1433 && submenu_depth != 0)
1434 i += MENU_ITEMS_PANE_LENGTH;
1435 /* Ignore a nil in the item list.
1436 It's meaningful only for dialog boxes. */
1437 else if (EQ (XVECTOR (menu_items)->contents[i], Qquote))
1438 i += 1;
1439 else if (EQ (XVECTOR (menu_items)->contents[i], Qt))
1441 /* Create a new pane. */
1442 Lisp_Object pane_name, prefix;
1443 char *pane_string;
1445 pane_name = XVECTOR (menu_items)->contents[i + MENU_ITEMS_PANE_NAME];
1446 prefix = XVECTOR (menu_items)->contents[i + MENU_ITEMS_PANE_PREFIX];
1448 #ifndef HAVE_MULTILINGUAL_MENU
1449 if (STRINGP (pane_name) && STRING_MULTIBYTE (pane_name))
1451 pane_name = string_make_unibyte (pane_name);
1452 AREF (menu_items, i + MENU_ITEMS_PANE_NAME) = pane_name;
1454 #endif
1455 pane_string = (NILP (pane_name)
1456 ? "" : (char *) XSTRING (pane_name)->data);
1457 /* If there is just one top-level pane, put all its items directly
1458 under the top-level menu. */
1459 if (menu_items_n_panes == 1)
1460 pane_string = "";
1462 /* If the pane has a meaningful name,
1463 make the pane a top-level menu item
1464 with its items as a submenu beneath it. */
1465 if (strcmp (pane_string, ""))
1467 wv = xmalloc_widget_value ();
1468 if (save_wv)
1469 save_wv->next = wv;
1470 else
1471 first_wv->contents = wv;
1472 wv->name = pane_string;
1473 /* Ignore the @ that means "separate pane".
1474 This is a kludge, but this isn't worth more time. */
1475 if (!NILP (prefix) && wv->name[0] == '@')
1476 wv->name++;
1477 wv->value = 0;
1478 wv->enabled = 1;
1479 wv->button_type = BUTTON_TYPE_NONE;
1481 save_wv = wv;
1482 prev_wv = 0;
1483 i += MENU_ITEMS_PANE_LENGTH;
1485 else
1487 /* Create a new item within current pane. */
1488 Lisp_Object item_name, enable, descrip, def, type, selected;
1489 Lisp_Object help;
1491 item_name = AREF (menu_items, i + MENU_ITEMS_ITEM_NAME);
1492 enable = AREF (menu_items, i + MENU_ITEMS_ITEM_ENABLE);
1493 descrip = AREF (menu_items, i + MENU_ITEMS_ITEM_EQUIV_KEY);
1494 def = AREF (menu_items, i + MENU_ITEMS_ITEM_DEFINITION);
1495 type = AREF (menu_items, i + MENU_ITEMS_ITEM_TYPE);
1496 selected = AREF (menu_items, i + MENU_ITEMS_ITEM_SELECTED);
1497 help = AREF (menu_items, i + MENU_ITEMS_ITEM_HELP);
1499 #ifndef HAVE_MULTILINGUAL_MENU
1500 if (STRING_MULTIBYTE (item_name))
1502 item_name = string_make_unibyte (item_name);
1503 AREF (menu_items, i + MENU_ITEMS_ITEM_NAME) = item_name;
1506 if (STRINGP (descrip) && STRING_MULTIBYTE (descrip))
1508 descrip = string_make_unibyte (descrip);
1509 AREF (menu_items, i + MENU_ITEMS_ITEM_EQUIV_KEY) = descrip;
1511 #endif /* not HAVE_MULTILINGUAL_MENU */
1513 wv = xmalloc_widget_value ();
1514 if (prev_wv)
1515 prev_wv->next = wv;
1516 else
1517 save_wv->contents = wv;
1519 wv->name = (char *) XSTRING (item_name)->data;
1520 if (!NILP (descrip))
1521 wv->key = (char *) XSTRING (descrip)->data;
1522 wv->value = 0;
1523 /* The EMACS_INT cast avoids a warning. There's no problem
1524 as long as pointers have enough bits to hold small integers. */
1525 wv->call_data = (!NILP (def) ? (void *) (EMACS_INT) i : 0);
1526 wv->enabled = !NILP (enable);
1528 if (NILP (type))
1529 wv->button_type = BUTTON_TYPE_NONE;
1530 else if (EQ (type, QCradio))
1531 wv->button_type = BUTTON_TYPE_RADIO;
1532 else if (EQ (type, QCtoggle))
1533 wv->button_type = BUTTON_TYPE_TOGGLE;
1534 else
1535 abort ();
1537 wv->selected = !NILP (selected);
1538 if (STRINGP (help))
1539 wv->help = XSTRING (help)->data;
1541 prev_wv = wv;
1543 i += MENU_ITEMS_ITEM_LENGTH;
1547 /* If we have just one "menu item"
1548 that was originally a button, return it by itself. */
1549 if (top_level_items && first_wv->contents && first_wv->contents->next == 0)
1551 wv = first_wv->contents;
1552 free_widget_value (first_wv);
1553 return wv;
1556 return first_wv;
1561 /* Recompute all the widgets of frame F, when the menu bar has been
1562 changed. Value is non-zero if widgets were updated. */
1564 static int
1565 update_frame_menubar (f)
1566 FRAME_PTR f;
1568 struct x_output *x = f->output_data.x;
1569 int columns, rows;
1571 if (!x->menubar_widget || XtIsManaged (x->menubar_widget))
1572 return 0;
1574 BLOCK_INPUT;
1575 /* Save the size of the frame because the pane widget doesn't accept
1576 to resize itself. So force it. */
1577 columns = f->width;
1578 rows = f->height;
1580 /* Do the voodoo which means "I'm changing lots of things, don't try
1581 to refigure sizes until I'm done." */
1582 lw_refigure_widget (x->column_widget, False);
1584 /* The order in which children are managed is the top to bottom
1585 order in which they are displayed in the paned window. First,
1586 remove the text-area widget. */
1587 XtUnmanageChild (x->edit_widget);
1589 /* Remove the menubar that is there now, and put up the menubar that
1590 should be there. */
1591 XtManageChild (x->menubar_widget);
1592 XtMapWidget (x->menubar_widget);
1593 XtVaSetValues (x->menubar_widget, XtNmappedWhenManaged, 1, NULL);
1595 /* Re-manage the text-area widget, and then thrash the sizes. */
1596 XtManageChild (x->edit_widget);
1597 lw_refigure_widget (x->column_widget, True);
1599 /* Force the pane widget to resize itself with the right values. */
1600 EmacsFrameSetCharSize (x->edit_widget, columns, rows);
1601 UNBLOCK_INPUT;
1602 return 1;
1605 /* Set the contents of the menubar widgets of frame F.
1606 The argument FIRST_TIME is currently ignored;
1607 it is set the first time this is called, from initialize_frame_menubar. */
1609 void
1610 set_frame_menubar (f, first_time, deep_p)
1611 FRAME_PTR f;
1612 int first_time;
1613 int deep_p;
1615 Widget menubar_widget = f->output_data.x->menubar_widget;
1616 Lisp_Object items;
1617 widget_value *wv, *first_wv, *prev_wv = 0;
1618 int i;
1619 LWLIB_ID id;
1621 XSETFRAME (Vmenu_updating_frame, f);
1623 if (f->output_data.x->id == 0)
1624 f->output_data.x->id = next_menubar_widget_id++;
1625 id = f->output_data.x->id;
1627 if (! menubar_widget)
1628 deep_p = 1;
1629 else if (pending_menu_activation && !deep_p)
1630 deep_p = 1;
1631 /* Make the first call for any given frame always go deep. */
1632 else if (!f->output_data.x->saved_menu_event && !deep_p)
1634 deep_p = 1;
1635 f->output_data.x->saved_menu_event = (XEvent*)xmalloc (sizeof (XEvent));
1636 f->output_data.x->saved_menu_event->type = 0;
1639 wv = xmalloc_widget_value ();
1640 wv->name = "menubar";
1641 wv->value = 0;
1642 wv->enabled = 1;
1643 wv->button_type = BUTTON_TYPE_NONE;
1644 first_wv = wv;
1646 if (deep_p)
1648 /* Make a widget-value tree representing the entire menu trees. */
1650 struct buffer *prev = current_buffer;
1651 Lisp_Object buffer;
1652 int specpdl_count = specpdl_ptr - specpdl;
1653 int previous_menu_items_used = f->menu_bar_items_used;
1654 Lisp_Object *previous_items
1655 = (Lisp_Object *) alloca (previous_menu_items_used
1656 * sizeof (Lisp_Object));
1658 /* If we are making a new widget, its contents are empty,
1659 do always reinitialize them. */
1660 if (! menubar_widget)
1661 previous_menu_items_used = 0;
1663 buffer = XWINDOW (FRAME_SELECTED_WINDOW (f))->buffer;
1664 specbind (Qinhibit_quit, Qt);
1665 /* Don't let the debugger step into this code
1666 because it is not reentrant. */
1667 specbind (Qdebug_on_next_call, Qnil);
1669 record_unwind_protect (Fset_match_data, Fmatch_data (Qnil, Qnil));
1670 if (NILP (Voverriding_local_map_menu_flag))
1672 specbind (Qoverriding_terminal_local_map, Qnil);
1673 specbind (Qoverriding_local_map, Qnil);
1676 set_buffer_internal_1 (XBUFFER (buffer));
1678 /* Run the Lucid hook. */
1679 safe_run_hooks (Qactivate_menubar_hook);
1681 /* If it has changed current-menubar from previous value,
1682 really recompute the menubar from the value. */
1683 if (! NILP (Vlucid_menu_bar_dirty_flag))
1684 call0 (Qrecompute_lucid_menubar);
1685 safe_run_hooks (Qmenu_bar_update_hook);
1686 FRAME_MENU_BAR_ITEMS (f) = menu_bar_items (FRAME_MENU_BAR_ITEMS (f));
1688 items = FRAME_MENU_BAR_ITEMS (f);
1690 inhibit_garbage_collection ();
1692 /* Save the frame's previous menu bar contents data. */
1693 bcopy (XVECTOR (f->menu_bar_vector)->contents, previous_items,
1694 previous_menu_items_used * sizeof (Lisp_Object));
1696 /* Fill in the current menu bar contents. */
1697 menu_items = f->menu_bar_vector;
1698 menu_items_allocated = XVECTOR (menu_items)->size;
1699 init_menu_items ();
1700 for (i = 0; i < XVECTOR (items)->size; i += 4)
1702 Lisp_Object key, string, maps;
1704 key = XVECTOR (items)->contents[i];
1705 string = XVECTOR (items)->contents[i + 1];
1706 maps = XVECTOR (items)->contents[i + 2];
1707 if (NILP (string))
1708 break;
1710 wv = single_submenu (key, string, maps);
1711 if (prev_wv)
1712 prev_wv->next = wv;
1713 else
1714 first_wv->contents = wv;
1715 /* Don't set wv->name here; GC during the loop might relocate it. */
1716 wv->enabled = 1;
1717 wv->button_type = BUTTON_TYPE_NONE;
1718 prev_wv = wv;
1721 finish_menu_items ();
1723 set_buffer_internal_1 (prev);
1724 unbind_to (specpdl_count, Qnil);
1726 /* If there has been no change in the Lisp-level contents
1727 of the menu bar, skip redisplaying it. Just exit. */
1729 for (i = 0; i < previous_menu_items_used; i++)
1730 if (menu_items_used == i
1731 || (!EQ (previous_items[i], XVECTOR (menu_items)->contents[i])))
1732 break;
1733 if (i == menu_items_used && i == previous_menu_items_used && i != 0)
1735 free_menubar_widget_value_tree (first_wv);
1736 menu_items = Qnil;
1738 return;
1741 /* Now GC cannot happen during the lifetime of the widget_value,
1742 so it's safe to store data from a Lisp_String. */
1743 wv = first_wv->contents;
1744 for (i = 0; i < XVECTOR (items)->size; i += 4)
1746 Lisp_Object string;
1747 string = XVECTOR (items)->contents[i + 1];
1748 if (NILP (string))
1749 break;
1750 wv->name = (char *) XSTRING (string)->data;
1751 wv = wv->next;
1754 f->menu_bar_vector = menu_items;
1755 f->menu_bar_items_used = menu_items_used;
1756 menu_items = Qnil;
1758 else
1760 /* Make a widget-value tree containing
1761 just the top level menu bar strings. */
1763 items = FRAME_MENU_BAR_ITEMS (f);
1764 for (i = 0; i < XVECTOR (items)->size; i += 4)
1766 Lisp_Object string;
1768 string = XVECTOR (items)->contents[i + 1];
1769 if (NILP (string))
1770 break;
1772 wv = xmalloc_widget_value ();
1773 wv->name = (char *) XSTRING (string)->data;
1774 wv->value = 0;
1775 wv->enabled = 1;
1776 wv->button_type = BUTTON_TYPE_NONE;
1777 /* This prevents lwlib from assuming this
1778 menu item is really supposed to be empty. */
1779 /* The EMACS_INT cast avoids a warning.
1780 This value just has to be different from small integers. */
1781 wv->call_data = (void *) (EMACS_INT) (-1);
1783 if (prev_wv)
1784 prev_wv->next = wv;
1785 else
1786 first_wv->contents = wv;
1787 prev_wv = wv;
1790 /* Forget what we thought we knew about what is in the
1791 detailed contents of the menu bar menus.
1792 Changing the top level always destroys the contents. */
1793 f->menu_bar_items_used = 0;
1796 /* Create or update the menu bar widget. */
1798 BLOCK_INPUT;
1800 if (menubar_widget)
1802 /* Disable resizing (done for Motif!) */
1803 lw_allow_resizing (f->output_data.x->widget, False);
1805 /* The third arg is DEEP_P, which says to consider the entire
1806 menu trees we supply, rather than just the menu bar item names. */
1807 lw_modify_all_widgets (id, first_wv, deep_p);
1809 /* Re-enable the edit widget to resize. */
1810 lw_allow_resizing (f->output_data.x->widget, True);
1812 else
1814 menubar_widget = lw_create_widget ("menubar", "menubar", id, first_wv,
1815 f->output_data.x->column_widget,
1817 popup_activate_callback,
1818 menubar_selection_callback,
1819 popup_deactivate_callback,
1820 menu_highlight_callback);
1821 f->output_data.x->menubar_widget = menubar_widget;
1825 int menubar_size
1826 = (f->output_data.x->menubar_widget
1827 ? (f->output_data.x->menubar_widget->core.height
1828 + f->output_data.x->menubar_widget->core.border_width)
1829 : 0);
1831 #if 0 /* Experimentally, we now get the right results
1832 for -geometry -0-0 without this. 24 Aug 96, rms. */
1833 #ifdef USE_LUCID
1834 if (FRAME_EXTERNAL_MENU_BAR (f))
1836 Dimension ibw = 0;
1837 XtVaGetValues (f->output_data.x->column_widget,
1838 XtNinternalBorderWidth, &ibw, NULL);
1839 menubar_size += ibw;
1841 #endif /* USE_LUCID */
1842 #endif /* 0 */
1844 f->output_data.x->menubar_height = menubar_size;
1847 free_menubar_widget_value_tree (first_wv);
1848 update_frame_menubar (f);
1850 UNBLOCK_INPUT;
1853 /* Called from Fx_create_frame to create the initial menubar of a frame
1854 before it is mapped, so that the window is mapped with the menubar already
1855 there instead of us tacking it on later and thrashing the window after it
1856 is visible. */
1858 void
1859 initialize_frame_menubar (f)
1860 FRAME_PTR f;
1862 /* This function is called before the first chance to redisplay
1863 the frame. It has to be, so the frame will have the right size. */
1864 FRAME_MENU_BAR_ITEMS (f) = menu_bar_items (FRAME_MENU_BAR_ITEMS (f));
1865 set_frame_menubar (f, 1, 1);
1869 /* Get rid of the menu bar of frame F, and free its storage.
1870 This is used when deleting a frame, and when turning off the menu bar. */
1872 void
1873 free_frame_menubar (f)
1874 FRAME_PTR f;
1876 Widget menubar_widget;
1878 menubar_widget = f->output_data.x->menubar_widget;
1880 f->output_data.x->menubar_height = 0;
1882 if (menubar_widget)
1884 #ifdef USE_MOTIF
1885 /* Removing the menu bar magically changes the shell widget's x
1886 and y position of (0, 0) which, when the menu bar is turned
1887 on again, leads to pull-down menuss appearing in strange
1888 positions near the upper-left corner of the display. This
1889 happens only with some window managers like twm and ctwm,
1890 but not with other like Motif's mwm or kwm, because the
1891 latter generate ConfigureNotify events when the menu bar
1892 is switched off, which fixes the shell position. */
1893 Position x0, y0, x1, y1;
1894 #endif
1896 BLOCK_INPUT;
1898 #ifdef USE_MOTIF
1899 if (f->output_data.x->widget)
1900 XtVaGetValues (f->output_data.x->widget, XtNx, &x0, XtNy, &y0, NULL);
1901 #endif
1903 lw_destroy_all_widgets ((LWLIB_ID) f->output_data.x->id);
1904 f->output_data.x->menubar_widget = NULL;
1906 #ifdef USE_MOTIF
1907 if (f->output_data.x->widget)
1909 XtVaGetValues (f->output_data.x->widget, XtNx, &x1, XtNy, &y1, NULL);
1910 if (x1 == 0 && y1 == 0)
1911 XtVaSetValues (f->output_data.x->widget, XtNx, x0, XtNy, y0, NULL);
1913 #endif
1915 UNBLOCK_INPUT;
1919 #endif /* USE_X_TOOLKIT */
1921 /* xmenu_show actually displays a menu using the panes and items in menu_items
1922 and returns the value selected from it.
1923 There are two versions of xmenu_show, one for Xt and one for Xlib.
1924 Both assume input is blocked by the caller. */
1926 /* F is the frame the menu is for.
1927 X and Y are the frame-relative specified position,
1928 relative to the inside upper left corner of the frame F.
1929 FOR_CLICK is nonzero if this menu was invoked for a mouse click.
1930 KEYMAPS is 1 if this menu was specified with keymaps;
1931 in that case, we return a list containing the chosen item's value
1932 and perhaps also the pane's prefix.
1933 TITLE is the specified menu title.
1934 ERROR is a place to store an error message string in case of failure.
1935 (We return nil on failure, but the value doesn't actually matter.) */
1937 #ifdef USE_X_TOOLKIT
1939 /* We need a unique id for each widget handled by the Lucid Widget
1940 library.
1942 For the main windows, and popup menus, we use this counter,
1943 which we increment each time after use. This starts from 1<<16.
1945 For menu bars, we use numbers starting at 0, counted in
1946 next_menubar_widget_id. */
1947 LWLIB_ID widget_id_tick;
1949 static Lisp_Object *volatile menu_item_selection;
1951 static void
1952 popup_selection_callback (widget, id, client_data)
1953 Widget widget;
1954 LWLIB_ID id;
1955 XtPointer client_data;
1957 menu_item_selection = (Lisp_Object *) client_data;
1960 static Lisp_Object
1961 xmenu_show (f, x, y, for_click, keymaps, title, error)
1962 FRAME_PTR f;
1963 int x;
1964 int y;
1965 int for_click;
1966 int keymaps;
1967 Lisp_Object title;
1968 char **error;
1970 int i;
1971 LWLIB_ID menu_id;
1972 Widget menu;
1973 Arg av[2];
1974 int ac = 0;
1975 widget_value *wv, *save_wv = 0, *first_wv = 0, *prev_wv = 0;
1976 widget_value **submenu_stack
1977 = (widget_value **) alloca (menu_items_used * sizeof (widget_value *));
1978 Lisp_Object *subprefix_stack
1979 = (Lisp_Object *) alloca (menu_items_used * sizeof (Lisp_Object));
1980 int submenu_depth = 0;
1981 XButtonPressedEvent dummy;
1983 int first_pane;
1985 *error = NULL;
1987 if (menu_items_used <= MENU_ITEMS_PANE_LENGTH)
1989 *error = "Empty menu";
1990 return Qnil;
1993 /* Create a tree of widget_value objects
1994 representing the panes and their items. */
1995 wv = xmalloc_widget_value ();
1996 wv->name = "menu";
1997 wv->value = 0;
1998 wv->enabled = 1;
1999 wv->button_type = BUTTON_TYPE_NONE;
2000 first_wv = wv;
2001 first_pane = 1;
2003 /* Loop over all panes and items, filling in the tree. */
2004 i = 0;
2005 while (i < menu_items_used)
2007 if (EQ (XVECTOR (menu_items)->contents[i], Qnil))
2009 submenu_stack[submenu_depth++] = save_wv;
2010 save_wv = prev_wv;
2011 prev_wv = 0;
2012 first_pane = 1;
2013 i++;
2015 else if (EQ (XVECTOR (menu_items)->contents[i], Qlambda))
2017 prev_wv = save_wv;
2018 save_wv = submenu_stack[--submenu_depth];
2019 first_pane = 0;
2020 i++;
2022 else if (EQ (XVECTOR (menu_items)->contents[i], Qt)
2023 && submenu_depth != 0)
2024 i += MENU_ITEMS_PANE_LENGTH;
2025 /* Ignore a nil in the item list.
2026 It's meaningful only for dialog boxes. */
2027 else if (EQ (XVECTOR (menu_items)->contents[i], Qquote))
2028 i += 1;
2029 else if (EQ (XVECTOR (menu_items)->contents[i], Qt))
2031 /* Create a new pane. */
2032 Lisp_Object pane_name, prefix;
2033 char *pane_string;
2035 pane_name = AREF (menu_items, i + MENU_ITEMS_PANE_NAME);
2036 prefix = AREF (menu_items, i + MENU_ITEMS_PANE_PREFIX);
2038 #ifndef HAVE_MULTILINGUAL_MENU
2039 if (STRINGP (pane_name) && STRING_MULTIBYTE (pane_name))
2041 pane_name = string_make_unibyte (pane_name);
2042 AREF (menu_items, i + MENU_ITEMS_PANE_NAME) = pane_name;
2044 #endif
2045 pane_string = (NILP (pane_name)
2046 ? "" : (char *) XSTRING (pane_name)->data);
2047 /* If there is just one top-level pane, put all its items directly
2048 under the top-level menu. */
2049 if (menu_items_n_panes == 1)
2050 pane_string = "";
2052 /* If the pane has a meaningful name,
2053 make the pane a top-level menu item
2054 with its items as a submenu beneath it. */
2055 if (!keymaps && strcmp (pane_string, ""))
2057 wv = xmalloc_widget_value ();
2058 if (save_wv)
2059 save_wv->next = wv;
2060 else
2061 first_wv->contents = wv;
2062 wv->name = pane_string;
2063 if (keymaps && !NILP (prefix))
2064 wv->name++;
2065 wv->value = 0;
2066 wv->enabled = 1;
2067 wv->button_type = BUTTON_TYPE_NONE;
2068 save_wv = wv;
2069 prev_wv = 0;
2071 else if (first_pane)
2073 save_wv = wv;
2074 prev_wv = 0;
2076 first_pane = 0;
2077 i += MENU_ITEMS_PANE_LENGTH;
2079 else
2081 /* Create a new item within current pane. */
2082 Lisp_Object item_name, enable, descrip, def, type, selected, help;
2083 item_name = AREF (menu_items, i + MENU_ITEMS_ITEM_NAME);
2084 enable = AREF (menu_items, i + MENU_ITEMS_ITEM_ENABLE);
2085 descrip = AREF (menu_items, i + MENU_ITEMS_ITEM_EQUIV_KEY);
2086 def = AREF (menu_items, i + MENU_ITEMS_ITEM_DEFINITION);
2087 type = AREF (menu_items, i + MENU_ITEMS_ITEM_TYPE);
2088 selected = AREF (menu_items, i + MENU_ITEMS_ITEM_SELECTED);
2089 help = AREF (menu_items, i + MENU_ITEMS_ITEM_HELP);
2091 #ifndef HAVE_MULTILINGUAL_MENU
2092 if (STRINGP (item_name) && STRING_MULTIBYTE (item_name))
2094 item_name = string_make_unibyte (item_name);
2095 AREF (menu_items, i + MENU_ITEMS_ITEM_NAME) = item_name;
2098 if (STRINGP (descrip) && STRING_MULTIBYTE (descrip))
2100 descrip = string_make_unibyte (descrip);
2101 AREF (menu_items, i + MENU_ITEMS_ITEM_EQUIV_KEY) = descrip;
2103 #endif /* not HAVE_MULTILINGUAL_MENU */
2105 wv = xmalloc_widget_value ();
2106 if (prev_wv)
2107 prev_wv->next = wv;
2108 else
2109 save_wv->contents = wv;
2110 wv->name = (char *) XSTRING (item_name)->data;
2111 if (!NILP (descrip))
2112 wv->key = (char *) XSTRING (descrip)->data;
2113 wv->value = 0;
2114 /* If this item has a null value,
2115 make the call_data null so that it won't display a box
2116 when the mouse is on it. */
2117 wv->call_data
2118 = (!NILP (def) ? (void *) &XVECTOR (menu_items)->contents[i] : 0);
2119 wv->enabled = !NILP (enable);
2121 if (NILP (type))
2122 wv->button_type = BUTTON_TYPE_NONE;
2123 else if (EQ (type, QCtoggle))
2124 wv->button_type = BUTTON_TYPE_TOGGLE;
2125 else if (EQ (type, QCradio))
2126 wv->button_type = BUTTON_TYPE_RADIO;
2127 else
2128 abort ();
2130 wv->selected = !NILP (selected);
2131 if (STRINGP (help))
2132 wv->help = XSTRING (help)->data;
2134 prev_wv = wv;
2136 i += MENU_ITEMS_ITEM_LENGTH;
2140 /* Deal with the title, if it is non-nil. */
2141 if (!NILP (title))
2143 widget_value *wv_title = xmalloc_widget_value ();
2144 widget_value *wv_sep1 = xmalloc_widget_value ();
2145 widget_value *wv_sep2 = xmalloc_widget_value ();
2147 wv_sep2->name = "--";
2148 wv_sep2->next = first_wv->contents;
2150 wv_sep1->name = "--";
2151 wv_sep1->next = wv_sep2;
2153 #ifndef HAVE_MULTILINGUAL_MENU
2154 if (STRING_MULTIBYTE (title))
2155 title = string_make_unibyte (title);
2156 #endif
2158 wv_title->name = (char *) XSTRING (title)->data;
2159 wv_title->enabled = True;
2160 wv_title->button_type = BUTTON_TYPE_NONE;
2161 wv_title->next = wv_sep1;
2162 first_wv->contents = wv_title;
2165 /* Actually create the menu. */
2166 menu_id = widget_id_tick++;
2167 menu = lw_create_widget ("popup", first_wv->name, menu_id, first_wv,
2168 f->output_data.x->widget, 1, 0,
2169 popup_selection_callback,
2170 popup_deactivate_callback,
2171 menu_highlight_callback);
2173 /* Adjust coordinates to relative to the outer (window manager) window. */
2175 Window child;
2176 int win_x = 0, win_y = 0;
2178 /* Find the position of the outside upper-left corner of
2179 the inner window, with respect to the outer window. */
2180 if (f->output_data.x->parent_desc != FRAME_X_DISPLAY_INFO (f)->root_window)
2182 BLOCK_INPUT;
2183 XTranslateCoordinates (FRAME_X_DISPLAY (f),
2185 /* From-window, to-window. */
2186 f->output_data.x->window_desc,
2187 f->output_data.x->parent_desc,
2189 /* From-position, to-position. */
2190 0, 0, &win_x, &win_y,
2192 /* Child of window. */
2193 &child);
2194 UNBLOCK_INPUT;
2195 x += win_x;
2196 y += win_y;
2200 /* Adjust coordinates to be root-window-relative. */
2201 x += f->output_data.x->left_pos;
2202 y += f->output_data.x->top_pos;
2204 dummy.type = ButtonPress;
2205 dummy.serial = 0;
2206 dummy.send_event = 0;
2207 dummy.display = FRAME_X_DISPLAY (f);
2208 dummy.time = CurrentTime;
2209 dummy.root = FRAME_X_DISPLAY_INFO (f)->root_window;
2210 dummy.window = dummy.root;
2211 dummy.subwindow = dummy.root;
2212 dummy.x_root = x;
2213 dummy.y_root = y;
2214 dummy.x = x;
2215 dummy.y = y;
2216 dummy.state = (FRAME_X_DISPLAY_INFO (f)->grabbed >> 1) * Button1Mask;
2217 dummy.button = 0;
2218 for (i = 0; i < 5; i++)
2219 if (FRAME_X_DISPLAY_INFO (f)->grabbed & (1 << i))
2220 dummy.button = i;
2222 /* Don't allow any geometry request from the user. */
2223 XtSetArg (av[ac], XtNgeometry, 0); ac++;
2224 XtSetValues (menu, av, ac);
2226 /* Free the widget_value objects we used to specify the contents. */
2227 free_menubar_widget_value_tree (first_wv);
2229 /* No selection has been chosen yet. */
2230 menu_item_selection = 0;
2232 /* Display the menu. */
2233 lw_popup_menu (menu, (XEvent *) &dummy);
2234 popup_activated_flag = 1;
2236 /* Process events that apply to the menu. */
2237 popup_get_selection ((XEvent *) 0, FRAME_X_DISPLAY_INFO (f), menu_id);
2239 /* fp turned off the following statement and wrote a comment
2240 that it is unnecessary--that the menu has already disappeared.
2241 Nowadays the menu disappears ok, all right, but
2242 we need to delete the widgets or multiple ones will pile up. */
2243 lw_destroy_all_widgets (menu_id);
2245 /* Find the selected item, and its pane, to return
2246 the proper value. */
2247 if (menu_item_selection != 0)
2249 Lisp_Object prefix, entry;
2251 prefix = entry = Qnil;
2252 i = 0;
2253 while (i < menu_items_used)
2255 if (EQ (XVECTOR (menu_items)->contents[i], Qnil))
2257 subprefix_stack[submenu_depth++] = prefix;
2258 prefix = entry;
2259 i++;
2261 else if (EQ (XVECTOR (menu_items)->contents[i], Qlambda))
2263 prefix = subprefix_stack[--submenu_depth];
2264 i++;
2266 else if (EQ (XVECTOR (menu_items)->contents[i], Qt))
2268 prefix
2269 = XVECTOR (menu_items)->contents[i + MENU_ITEMS_PANE_PREFIX];
2270 i += MENU_ITEMS_PANE_LENGTH;
2272 /* Ignore a nil in the item list.
2273 It's meaningful only for dialog boxes. */
2274 else if (EQ (XVECTOR (menu_items)->contents[i], Qquote))
2275 i += 1;
2276 else
2278 entry
2279 = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_VALUE];
2280 if (menu_item_selection == &XVECTOR (menu_items)->contents[i])
2282 if (keymaps != 0)
2284 int j;
2286 entry = Fcons (entry, Qnil);
2287 if (!NILP (prefix))
2288 entry = Fcons (prefix, entry);
2289 for (j = submenu_depth - 1; j >= 0; j--)
2290 if (!NILP (subprefix_stack[j]))
2291 entry = Fcons (subprefix_stack[j], entry);
2293 return entry;
2295 i += MENU_ITEMS_ITEM_LENGTH;
2300 return Qnil;
2303 static void
2304 dialog_selection_callback (widget, id, client_data)
2305 Widget widget;
2306 LWLIB_ID id;
2307 XtPointer client_data;
2309 /* The EMACS_INT cast avoids a warning. There's no problem
2310 as long as pointers have enough bits to hold small integers. */
2311 if ((int) (EMACS_INT) client_data != -1)
2312 menu_item_selection = (Lisp_Object *) client_data;
2313 BLOCK_INPUT;
2314 lw_destroy_all_widgets (id);
2315 UNBLOCK_INPUT;
2316 popup_activated_flag = 0;
2319 static char * button_names [] = {
2320 "button1", "button2", "button3", "button4", "button5",
2321 "button6", "button7", "button8", "button9", "button10" };
2323 static Lisp_Object
2324 xdialog_show (f, keymaps, title, error)
2325 FRAME_PTR f;
2326 int keymaps;
2327 Lisp_Object title;
2328 char **error;
2330 int i, nb_buttons=0;
2331 LWLIB_ID dialog_id;
2332 Widget menu;
2333 char dialog_name[6];
2335 widget_value *wv, *first_wv = 0, *prev_wv = 0;
2337 /* Number of elements seen so far, before boundary. */
2338 int left_count = 0;
2339 /* 1 means we've seen the boundary between left-hand elts and right-hand. */
2340 int boundary_seen = 0;
2342 *error = NULL;
2344 if (menu_items_n_panes > 1)
2346 *error = "Multiple panes in dialog box";
2347 return Qnil;
2350 /* Create a tree of widget_value objects
2351 representing the text label and buttons. */
2353 Lisp_Object pane_name, prefix;
2354 char *pane_string;
2355 pane_name = XVECTOR (menu_items)->contents[MENU_ITEMS_PANE_NAME];
2356 prefix = XVECTOR (menu_items)->contents[MENU_ITEMS_PANE_PREFIX];
2357 pane_string = (NILP (pane_name)
2358 ? "" : (char *) XSTRING (pane_name)->data);
2359 prev_wv = xmalloc_widget_value ();
2360 prev_wv->value = pane_string;
2361 if (keymaps && !NILP (prefix))
2362 prev_wv->name++;
2363 prev_wv->enabled = 1;
2364 prev_wv->name = "message";
2365 first_wv = prev_wv;
2367 /* Loop over all panes and items, filling in the tree. */
2368 i = MENU_ITEMS_PANE_LENGTH;
2369 while (i < menu_items_used)
2372 /* Create a new item within current pane. */
2373 Lisp_Object item_name, enable, descrip;
2374 item_name = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_NAME];
2375 enable = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_ENABLE];
2376 descrip
2377 = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_EQUIV_KEY];
2379 if (NILP (item_name))
2381 free_menubar_widget_value_tree (first_wv);
2382 *error = "Submenu in dialog items";
2383 return Qnil;
2385 if (EQ (item_name, Qquote))
2387 /* This is the boundary between left-side elts
2388 and right-side elts. Stop incrementing right_count. */
2389 boundary_seen = 1;
2390 i++;
2391 continue;
2393 if (nb_buttons >= 9)
2395 free_menubar_widget_value_tree (first_wv);
2396 *error = "Too many dialog items";
2397 return Qnil;
2400 wv = xmalloc_widget_value ();
2401 prev_wv->next = wv;
2402 wv->name = (char *) button_names[nb_buttons];
2403 if (!NILP (descrip))
2404 wv->key = (char *) XSTRING (descrip)->data;
2405 wv->value = (char *) XSTRING (item_name)->data;
2406 wv->call_data = (void *) &XVECTOR (menu_items)->contents[i];
2407 wv->enabled = !NILP (enable);
2408 prev_wv = wv;
2410 if (! boundary_seen)
2411 left_count++;
2413 nb_buttons++;
2414 i += MENU_ITEMS_ITEM_LENGTH;
2417 /* If the boundary was not specified,
2418 by default put half on the left and half on the right. */
2419 if (! boundary_seen)
2420 left_count = nb_buttons - nb_buttons / 2;
2422 wv = xmalloc_widget_value ();
2423 wv->name = dialog_name;
2425 /* Dialog boxes use a really stupid name encoding
2426 which specifies how many buttons to use
2427 and how many buttons are on the right.
2428 The Q means something also. */
2429 dialog_name[0] = 'Q';
2430 dialog_name[1] = '0' + nb_buttons;
2431 dialog_name[2] = 'B';
2432 dialog_name[3] = 'R';
2433 /* Number of buttons to put on the right. */
2434 dialog_name[4] = '0' + nb_buttons - left_count;
2435 dialog_name[5] = 0;
2436 wv->contents = first_wv;
2437 first_wv = wv;
2440 /* Actually create the dialog. */
2441 dialog_id = widget_id_tick++;
2442 menu = lw_create_widget (first_wv->name, "dialog", dialog_id, first_wv,
2443 f->output_data.x->widget, 1, 0,
2444 dialog_selection_callback, 0, 0);
2445 lw_modify_all_widgets (dialog_id, first_wv->contents, True);
2446 /* Free the widget_value objects we used to specify the contents. */
2447 free_menubar_widget_value_tree (first_wv);
2449 /* No selection has been chosen yet. */
2450 menu_item_selection = 0;
2452 /* Display the menu. */
2453 lw_pop_up_all_widgets (dialog_id);
2454 popup_activated_flag = 1;
2456 /* Process events that apply to the menu. */
2457 popup_get_selection ((XEvent *) 0, FRAME_X_DISPLAY_INFO (f), dialog_id);
2459 lw_destroy_all_widgets (dialog_id);
2461 /* Find the selected item, and its pane, to return
2462 the proper value. */
2463 if (menu_item_selection != 0)
2465 Lisp_Object prefix;
2467 prefix = Qnil;
2468 i = 0;
2469 while (i < menu_items_used)
2471 Lisp_Object entry;
2473 if (EQ (XVECTOR (menu_items)->contents[i], Qt))
2475 prefix
2476 = XVECTOR (menu_items)->contents[i + MENU_ITEMS_PANE_PREFIX];
2477 i += MENU_ITEMS_PANE_LENGTH;
2479 else if (EQ (XVECTOR (menu_items)->contents[i], Qquote))
2481 /* This is the boundary between left-side elts and
2482 right-side elts. */
2483 ++i;
2485 else
2487 entry
2488 = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_VALUE];
2489 if (menu_item_selection == &XVECTOR (menu_items)->contents[i])
2491 if (keymaps != 0)
2493 entry = Fcons (entry, Qnil);
2494 if (!NILP (prefix))
2495 entry = Fcons (prefix, entry);
2497 return entry;
2499 i += MENU_ITEMS_ITEM_LENGTH;
2504 return Qnil;
2507 #else /* not USE_X_TOOLKIT */
2509 /* The frame of the last activated non-toolkit menu bar.
2510 Used to generate menu help events. */
2512 static struct frame *menu_help_frame;
2515 /* Show help HELP_STRING, or clear help if HELP_STRING is null.
2517 PANE is the pane number, and ITEM is the menu item number in
2518 the menu (currently not used).
2520 This cannot be done with generating a HELP_EVENT because
2521 XMenuActivate contains a loop that doesn't let Emacs process
2522 keyboard events. */
2524 static void
2525 menu_help_callback (help_string, pane, item)
2526 char *help_string;
2527 int pane, item;
2529 extern Lisp_Object Qmenu_item;
2530 Lisp_Object *first_item;
2531 Lisp_Object pane_name;
2532 Lisp_Object menu_object;
2534 first_item = XVECTOR (menu_items)->contents;
2535 if (EQ (first_item[0], Qt))
2536 pane_name = first_item[MENU_ITEMS_PANE_NAME];
2537 else if (EQ (first_item[0], Qquote))
2538 /* This shouldn't happen, see xmenu_show. */
2539 pane_name = build_string ("");
2540 else
2541 pane_name = first_item[MENU_ITEMS_ITEM_NAME];
2543 /* (menu-item MENU-NAME PANE-NUMBER) */
2544 menu_object = Fcons (Qmenu_item,
2545 Fcons (pane_name,
2546 Fcons (make_number (pane), Qnil)));
2547 show_help_echo (help_string ? build_string (help_string) : Qnil,
2548 Qnil, menu_object, make_number (item), 1);
2552 static Lisp_Object
2553 xmenu_show (f, x, y, for_click, keymaps, title, error)
2554 FRAME_PTR f;
2555 int x, y;
2556 int for_click;
2557 int keymaps;
2558 Lisp_Object title;
2559 char **error;
2561 Window root;
2562 XMenu *menu;
2563 int pane, selidx, lpane, status;
2564 Lisp_Object entry, pane_prefix;
2565 char *datap;
2566 int ulx, uly, width, height;
2567 int dispwidth, dispheight;
2568 int i, j;
2569 int maxwidth;
2570 int dummy_int;
2571 unsigned int dummy_uint;
2573 *error = 0;
2574 if (menu_items_n_panes == 0)
2575 return Qnil;
2577 if (menu_items_used <= MENU_ITEMS_PANE_LENGTH)
2579 *error = "Empty menu";
2580 return Qnil;
2583 /* Figure out which root window F is on. */
2584 XGetGeometry (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), &root,
2585 &dummy_int, &dummy_int, &dummy_uint, &dummy_uint,
2586 &dummy_uint, &dummy_uint);
2588 /* Make the menu on that window. */
2589 menu = XMenuCreate (FRAME_X_DISPLAY (f), root, "emacs");
2590 if (menu == NULL)
2592 *error = "Can't create menu";
2593 return Qnil;
2596 #ifdef HAVE_X_WINDOWS
2597 /* Adjust coordinates to relative to the outer (window manager) window. */
2599 Window child;
2600 int win_x = 0, win_y = 0;
2602 /* Find the position of the outside upper-left corner of
2603 the inner window, with respect to the outer window. */
2604 if (f->output_data.x->parent_desc != FRAME_X_DISPLAY_INFO (f)->root_window)
2606 BLOCK_INPUT;
2607 XTranslateCoordinates (FRAME_X_DISPLAY (f),
2609 /* From-window, to-window. */
2610 f->output_data.x->window_desc,
2611 f->output_data.x->parent_desc,
2613 /* From-position, to-position. */
2614 0, 0, &win_x, &win_y,
2616 /* Child of window. */
2617 &child);
2618 UNBLOCK_INPUT;
2619 x += win_x;
2620 y += win_y;
2623 #endif /* HAVE_X_WINDOWS */
2625 /* Adjust coordinates to be root-window-relative. */
2626 x += f->output_data.x->left_pos;
2627 y += f->output_data.x->top_pos;
2629 /* Create all the necessary panes and their items. */
2630 i = 0;
2631 while (i < menu_items_used)
2633 if (EQ (XVECTOR (menu_items)->contents[i], Qt))
2635 /* Create a new pane. */
2636 Lisp_Object pane_name, prefix;
2637 char *pane_string;
2639 pane_name = XVECTOR (menu_items)->contents[i + MENU_ITEMS_PANE_NAME];
2640 prefix = XVECTOR (menu_items)->contents[i + MENU_ITEMS_PANE_PREFIX];
2641 pane_string = (NILP (pane_name)
2642 ? "" : (char *) XSTRING (pane_name)->data);
2643 if (keymaps && !NILP (prefix))
2644 pane_string++;
2646 lpane = XMenuAddPane (FRAME_X_DISPLAY (f), menu, pane_string, TRUE);
2647 if (lpane == XM_FAILURE)
2649 XMenuDestroy (FRAME_X_DISPLAY (f), menu);
2650 *error = "Can't create pane";
2651 return Qnil;
2653 i += MENU_ITEMS_PANE_LENGTH;
2655 /* Find the width of the widest item in this pane. */
2656 maxwidth = 0;
2657 j = i;
2658 while (j < menu_items_used)
2660 Lisp_Object item;
2661 item = XVECTOR (menu_items)->contents[j];
2662 if (EQ (item, Qt))
2663 break;
2664 if (NILP (item))
2666 j++;
2667 continue;
2669 width = STRING_BYTES (XSTRING (item));
2670 if (width > maxwidth)
2671 maxwidth = width;
2673 j += MENU_ITEMS_ITEM_LENGTH;
2676 /* Ignore a nil in the item list.
2677 It's meaningful only for dialog boxes. */
2678 else if (EQ (XVECTOR (menu_items)->contents[i], Qquote))
2679 i += 1;
2680 else
2682 /* Create a new item within current pane. */
2683 Lisp_Object item_name, enable, descrip, help;
2684 unsigned char *item_data;
2685 char *help_string;
2687 item_name = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_NAME];
2688 enable = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_ENABLE];
2689 descrip
2690 = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_EQUIV_KEY];
2691 help = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_HELP];
2692 help_string = STRINGP (help) ? XSTRING (help)->data : NULL;
2694 if (!NILP (descrip))
2696 int gap = maxwidth - STRING_BYTES (XSTRING (item_name));
2697 #ifdef C_ALLOCA
2698 Lisp_Object spacer;
2699 spacer = Fmake_string (make_number (gap), make_number (' '));
2700 item_name = concat2 (item_name, spacer);
2701 item_name = concat2 (item_name, descrip);
2702 item_data = XSTRING (item_name)->data;
2703 #else
2704 /* if alloca is fast, use that to make the space,
2705 to reduce gc needs. */
2706 item_data
2707 = (unsigned char *) alloca (maxwidth
2708 + STRING_BYTES (XSTRING (descrip)) + 1);
2709 bcopy (XSTRING (item_name)->data, item_data,
2710 STRING_BYTES (XSTRING (item_name)));
2711 for (j = XSTRING (item_name)->size; j < maxwidth; j++)
2712 item_data[j] = ' ';
2713 bcopy (XSTRING (descrip)->data, item_data + j,
2714 STRING_BYTES (XSTRING (descrip)));
2715 item_data[j + STRING_BYTES (XSTRING (descrip))] = 0;
2716 #endif
2718 else
2719 item_data = XSTRING (item_name)->data;
2721 if (XMenuAddSelection (FRAME_X_DISPLAY (f),
2722 menu, lpane, 0, item_data,
2723 !NILP (enable), help_string)
2724 == XM_FAILURE)
2726 XMenuDestroy (FRAME_X_DISPLAY (f), menu);
2727 *error = "Can't add selection to menu";
2728 return Qnil;
2730 i += MENU_ITEMS_ITEM_LENGTH;
2734 /* All set and ready to fly. */
2735 XMenuRecompute (FRAME_X_DISPLAY (f), menu);
2736 dispwidth = DisplayWidth (FRAME_X_DISPLAY (f), FRAME_X_SCREEN_NUMBER (f));
2737 dispheight = DisplayHeight (FRAME_X_DISPLAY (f), FRAME_X_SCREEN_NUMBER (f));
2738 x = min (x, dispwidth);
2739 y = min (y, dispheight);
2740 x = max (x, 1);
2741 y = max (y, 1);
2742 XMenuLocate (FRAME_X_DISPLAY (f), menu, 0, 0, x, y,
2743 &ulx, &uly, &width, &height);
2744 if (ulx+width > dispwidth)
2746 x -= (ulx + width) - dispwidth;
2747 ulx = dispwidth - width;
2749 if (uly+height > dispheight)
2751 y -= (uly + height) - dispheight;
2752 uly = dispheight - height;
2754 if (ulx < 0) x -= ulx;
2755 if (uly < 0) y -= uly;
2757 XMenuSetAEQ (menu, TRUE);
2758 XMenuSetFreeze (menu, TRUE);
2759 pane = selidx = 0;
2761 /* Help display under X won't work because XMenuActivate contains
2762 a loop that doesn't give Emacs a chance to process it. */
2763 menu_help_frame = f;
2764 status = XMenuActivate (FRAME_X_DISPLAY (f), menu, &pane, &selidx,
2765 x, y, ButtonReleaseMask, &datap,
2766 menu_help_callback);
2769 #ifdef HAVE_X_WINDOWS
2770 /* Assume the mouse has moved out of the X window.
2771 If it has actually moved in, we will get an EnterNotify. */
2772 x_mouse_leave (FRAME_X_DISPLAY_INFO (f));
2773 #endif
2775 switch (status)
2777 case XM_SUCCESS:
2778 #ifdef XDEBUG
2779 fprintf (stderr, "pane= %d line = %d\n", panes, selidx);
2780 #endif
2782 /* Find the item number SELIDX in pane number PANE. */
2783 i = 0;
2784 while (i < menu_items_used)
2786 if (EQ (XVECTOR (menu_items)->contents[i], Qt))
2788 if (pane == 0)
2789 pane_prefix
2790 = XVECTOR (menu_items)->contents[i + MENU_ITEMS_PANE_PREFIX];
2791 pane--;
2792 i += MENU_ITEMS_PANE_LENGTH;
2794 else
2796 if (pane == -1)
2798 if (selidx == 0)
2800 entry
2801 = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_VALUE];
2802 if (keymaps != 0)
2804 entry = Fcons (entry, Qnil);
2805 if (!NILP (pane_prefix))
2806 entry = Fcons (pane_prefix, entry);
2808 break;
2810 selidx--;
2812 i += MENU_ITEMS_ITEM_LENGTH;
2815 break;
2817 case XM_FAILURE:
2818 *error = "Can't activate menu";
2819 case XM_IA_SELECT:
2820 case XM_NO_SELECT:
2821 entry = Qnil;
2822 break;
2824 XMenuDestroy (FRAME_X_DISPLAY (f), menu);
2826 #ifdef HAVE_X_WINDOWS
2827 /* State that no mouse buttons are now held.
2828 (The oldXMenu code doesn't track this info for us.)
2829 That is not necessarily true, but the fiction leads to reasonable
2830 results, and it is a pain to ask which are actually held now. */
2831 FRAME_X_DISPLAY_INFO (f)->grabbed = 0;
2832 #endif
2834 return entry;
2837 #endif /* not USE_X_TOOLKIT */
2839 #endif /* HAVE_MENUS */
2841 void
2842 syms_of_xmenu ()
2844 staticpro (&menu_items);
2845 menu_items = Qnil;
2847 Qdebug_on_next_call = intern ("debug-on-next-call");
2848 staticpro (&Qdebug_on_next_call);
2850 DEFVAR_LISP ("menu-updating-frame", &Vmenu_updating_frame,
2851 "Frame for which we are updating a menu.\n\
2852 The enable predicate for a menu command should check this variable.");
2853 Vmenu_updating_frame = Qnil;
2855 #ifdef USE_X_TOOLKIT
2856 widget_id_tick = (1<<16);
2857 next_menubar_widget_id = 1;
2858 #endif
2860 defsubr (&Sx_popup_menu);
2861 #ifdef HAVE_MENUS
2862 defsubr (&Sx_popup_dialog);
2863 #endif