* lisp/saveplace.el (save-place-local-mode): New minor mode
[emacs.git] / src / menu.c
blobcbddef3575495337bbfec73cad2c9de0bf7922d2
1 /* Platform-independent code for terminal communications.
3 Copyright (C) 1986, 1988, 1993-1994, 1996, 1999-2016 Free Software
4 Foundation, Inc.
6 This file is part of GNU Emacs.
8 GNU Emacs is free software: you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation, either version 3 of the License, or
11 (at your option) any later version.
13 GNU Emacs is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
21 #include <config.h>
22 #include <stdio.h>
23 #include <limits.h> /* for INT_MAX */
25 #include "lisp.h"
26 #include "character.h"
27 #include "coding.h"
28 #include "keyboard.h"
29 #include "keymap.h"
30 #include "frame.h"
31 #include "window.h"
32 #include "termhooks.h"
33 #include "blockinput.h"
34 #include "buffer.h"
36 #ifdef USE_X_TOOLKIT
37 #include "../lwlib/lwlib.h"
38 #endif
40 #ifdef HAVE_WINDOW_SYSTEM
41 #include TERM_HEADER
42 #endif /* HAVE_WINDOW_SYSTEM */
44 #ifdef HAVE_NTGUI
45 # ifdef NTGUI_UNICODE
46 # define unicode_append_menu AppendMenuW
47 # else /* !NTGUI_UNICODE */
48 extern AppendMenuW_Proc unicode_append_menu;
49 # endif /* NTGUI_UNICODE */
50 extern HMENU current_popup_menu;
51 #endif /* HAVE_NTGUI */
53 #include "menu.h"
55 /* Return non-zero if menus can handle radio and toggle buttons. */
56 static bool
57 have_boxes (void)
59 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (HAVE_NTGUI) || defined(HAVE_NS)
60 if (FRAME_WINDOW_P (XFRAME (Vmenu_updating_frame)))
61 return 1;
62 #endif
63 return 0;
66 Lisp_Object menu_items;
68 /* If non-nil, means that the global vars defined here are already in use.
69 Used to detect cases where we try to re-enter this non-reentrant code. */
70 Lisp_Object menu_items_inuse;
72 /* Number of slots currently allocated in menu_items. */
73 int menu_items_allocated;
75 /* This is the index in menu_items of the first empty slot. */
76 int menu_items_used;
78 /* The number of panes currently recorded in menu_items,
79 excluding those within submenus. */
80 int menu_items_n_panes;
82 /* Current depth within submenus. */
83 static int menu_items_submenu_depth;
85 void
86 init_menu_items (void)
88 if (!NILP (menu_items_inuse))
89 error ("Trying to use a menu from within a menu-entry");
91 if (NILP (menu_items))
93 menu_items_allocated = 60;
94 menu_items = Fmake_vector (make_number (menu_items_allocated), Qnil);
97 menu_items_inuse = Qt;
98 menu_items_used = 0;
99 menu_items_n_panes = 0;
100 menu_items_submenu_depth = 0;
103 /* Call at the end of generating the data in menu_items. */
105 void
106 finish_menu_items (void)
110 void
111 unuse_menu_items (void)
113 menu_items_inuse = Qnil;
116 /* Call when finished using the data for the current menu
117 in menu_items. */
119 void
120 discard_menu_items (void)
122 /* Free the structure if it is especially large.
123 Otherwise, hold on to it, to save time. */
124 if (menu_items_allocated > 200)
126 menu_items = Qnil;
127 menu_items_allocated = 0;
129 eassert (NILP (menu_items_inuse));
132 /* This undoes save_menu_items, and it is called by the specpdl unwind
133 mechanism. */
135 static void
136 restore_menu_items (Lisp_Object saved)
138 menu_items = XCAR (saved);
139 menu_items_inuse = (! NILP (menu_items) ? Qt : Qnil);
140 menu_items_allocated = (VECTORP (menu_items) ? ASIZE (menu_items) : 0);
141 saved = XCDR (saved);
142 menu_items_used = XINT (XCAR (saved));
143 saved = XCDR (saved);
144 menu_items_n_panes = XINT (XCAR (saved));
145 saved = XCDR (saved);
146 menu_items_submenu_depth = XINT (XCAR (saved));
149 /* Push the whole state of menu_items processing onto the specpdl.
150 It will be restored when the specpdl is unwound. */
152 void
153 save_menu_items (void)
155 Lisp_Object saved = list4 (!NILP (menu_items_inuse) ? menu_items : Qnil,
156 make_number (menu_items_used),
157 make_number (menu_items_n_panes),
158 make_number (menu_items_submenu_depth));
159 record_unwind_protect (restore_menu_items, saved);
160 menu_items_inuse = Qnil;
161 menu_items = Qnil;
165 /* Ensure that there is room for ITEMS items in the menu_items vector. */
167 static void
168 ensure_menu_items (int items)
170 int incr = items - (menu_items_allocated - menu_items_used);
171 if (incr > 0)
173 menu_items = larger_vector (menu_items, incr, INT_MAX);
174 menu_items_allocated = ASIZE (menu_items);
178 #if (defined USE_X_TOOLKIT || defined USE_GTK || defined HAVE_NS \
179 || defined HAVE_NTGUI)
181 /* Begin a submenu. */
183 static void
184 push_submenu_start (void)
186 ensure_menu_items (1);
187 ASET (menu_items, menu_items_used, Qnil);
188 menu_items_used++;
189 menu_items_submenu_depth++;
192 /* End a submenu. */
194 static void
195 push_submenu_end (void)
197 ensure_menu_items (1);
198 ASET (menu_items, menu_items_used, Qlambda);
199 menu_items_used++;
200 menu_items_submenu_depth--;
203 #endif /* USE_X_TOOLKIT || USE_GTK || HAVE_NS || defined HAVE_NTGUI */
205 /* Indicate boundary between left and right. */
207 static void
208 push_left_right_boundary (void)
210 ensure_menu_items (1);
211 ASET (menu_items, menu_items_used, Qquote);
212 menu_items_used++;
215 /* Start a new menu pane in menu_items.
216 NAME is the pane name. PREFIX_VEC is a prefix key for this pane. */
218 static void
219 push_menu_pane (Lisp_Object name, Lisp_Object prefix_vec)
221 ensure_menu_items (MENU_ITEMS_PANE_LENGTH);
222 if (menu_items_submenu_depth == 0)
223 menu_items_n_panes++;
224 ASET (menu_items, menu_items_used, Qt);
225 menu_items_used++;
226 ASET (menu_items, menu_items_used, name);
227 menu_items_used++;
228 ASET (menu_items, menu_items_used, prefix_vec);
229 menu_items_used++;
232 /* Push one menu item into the current pane. NAME is the string to
233 display. ENABLE if non-nil means this item can be selected. KEY
234 is the key generated by choosing this item, or nil if this item
235 doesn't really have a definition. DEF is the definition of this
236 item. EQUIV is the textual description of the keyboard equivalent
237 for this item (or nil if none). TYPE is the type of this menu
238 item, one of nil, `toggle' or `radio'. */
240 static void
241 push_menu_item (Lisp_Object name, Lisp_Object enable, Lisp_Object key, Lisp_Object def, Lisp_Object equiv, Lisp_Object type, Lisp_Object selected, Lisp_Object help)
243 ensure_menu_items (MENU_ITEMS_ITEM_LENGTH);
245 ASET (menu_items, menu_items_used + MENU_ITEMS_ITEM_NAME, name);
246 ASET (menu_items, menu_items_used + MENU_ITEMS_ITEM_ENABLE, enable);
247 ASET (menu_items, menu_items_used + MENU_ITEMS_ITEM_VALUE, key);
248 ASET (menu_items, menu_items_used + MENU_ITEMS_ITEM_EQUIV_KEY, equiv);
249 ASET (menu_items, menu_items_used + MENU_ITEMS_ITEM_DEFINITION, def);
250 ASET (menu_items, menu_items_used + MENU_ITEMS_ITEM_TYPE, type);
251 ASET (menu_items, menu_items_used + MENU_ITEMS_ITEM_SELECTED, selected);
252 ASET (menu_items, menu_items_used + MENU_ITEMS_ITEM_HELP, help);
254 menu_items_used += MENU_ITEMS_ITEM_LENGTH;
257 /* Args passed between single_keymap_panes and single_menu_item. */
258 struct skp
260 Lisp_Object pending_maps;
261 int maxdepth;
262 int notbuttons;
265 static void single_menu_item (Lisp_Object, Lisp_Object, Lisp_Object,
266 void *);
268 /* This is a recursive subroutine of keymap_panes.
269 It handles one keymap, KEYMAP.
270 The other arguments are passed along
271 or point to local variables of the previous function.
273 If we encounter submenus deeper than MAXDEPTH levels, ignore them. */
275 static void
276 single_keymap_panes (Lisp_Object keymap, Lisp_Object pane_name,
277 Lisp_Object prefix, int maxdepth)
279 struct skp skp;
281 skp.pending_maps = Qnil;
282 skp.maxdepth = maxdepth;
283 skp.notbuttons = 0;
285 if (maxdepth <= 0)
286 return;
288 push_menu_pane (pane_name, prefix);
290 if (!have_boxes ())
292 /* Remember index for first item in this pane so we can go back
293 and add a prefix when (if) we see the first button. After
294 that, notbuttons is set to 0, to mark that we have seen a
295 button and all non button items need a prefix. */
296 skp.notbuttons = menu_items_used;
299 map_keymap_canonical (keymap, single_menu_item, Qnil, &skp);
301 /* Process now any submenus which want to be panes at this level. */
302 while (CONSP (skp.pending_maps))
304 Lisp_Object elt, eltcdr, string;
305 elt = XCAR (skp.pending_maps);
306 eltcdr = XCDR (elt);
307 string = XCAR (eltcdr);
308 /* We no longer discard the @ from the beginning of the string here.
309 Instead, we do this in *menu_show. */
310 single_keymap_panes (Fcar (elt), string, XCDR (eltcdr), maxdepth - 1);
311 skp.pending_maps = XCDR (skp.pending_maps);
315 /* This is a subroutine of single_keymap_panes that handles one
316 keymap entry.
317 KEY is a key in a keymap and ITEM is its binding.
318 SKP->PENDING_MAPS_PTR is a list of keymaps waiting to be made into
319 separate panes.
320 If we encounter submenus deeper than SKP->MAXDEPTH levels, ignore them. */
322 static void
323 single_menu_item (Lisp_Object key, Lisp_Object item, Lisp_Object dummy, void *skp_v)
325 Lisp_Object map, item_string, enabled;
326 bool res;
327 struct skp *skp = skp_v;
329 /* Parse the menu item and leave the result in item_properties. */
330 res = parse_menu_item (item, 0);
331 if (!res)
332 return; /* Not a menu item. */
334 map = AREF (item_properties, ITEM_PROPERTY_MAP);
336 enabled = AREF (item_properties, ITEM_PROPERTY_ENABLE);
337 item_string = AREF (item_properties, ITEM_PROPERTY_NAME);
339 if (!NILP (map) && SREF (item_string, 0) == '@')
341 if (!NILP (enabled))
342 /* An enabled separate pane. Remember this to handle it later. */
343 skp->pending_maps = Fcons (Fcons (map, Fcons (item_string, key)),
344 skp->pending_maps);
345 return;
348 /* Simulate radio buttons and toggle boxes by putting a prefix in
349 front of them. */
350 if (!have_boxes ())
352 char const *prefix = 0;
353 Lisp_Object type = AREF (item_properties, ITEM_PROPERTY_TYPE);
354 if (!NILP (type))
356 Lisp_Object selected
357 = AREF (item_properties, ITEM_PROPERTY_SELECTED);
359 if (skp->notbuttons)
360 /* The first button. Line up previous items in this menu. */
362 int idx = skp->notbuttons; /* Index for first item this menu. */
363 int submenu = 0;
364 Lisp_Object tem;
365 while (idx < menu_items_used)
368 = AREF (menu_items, idx + MENU_ITEMS_ITEM_NAME);
369 if (NILP (tem))
371 idx++;
372 submenu++; /* Skip sub menu. */
374 else if (EQ (tem, Qlambda))
376 idx++;
377 submenu--; /* End sub menu. */
379 else if (EQ (tem, Qt))
380 idx += 3; /* Skip new pane marker. */
381 else if (EQ (tem, Qquote))
382 idx++; /* Skip a left, right divider. */
383 else
385 if (!submenu && SREF (tem, 0) != '\0'
386 && SREF (tem, 0) != '-')
388 AUTO_STRING (spaces, " ");
389 ASET (menu_items, idx + MENU_ITEMS_ITEM_NAME,
390 concat2 (spaces, tem));
392 idx += MENU_ITEMS_ITEM_LENGTH;
395 skp->notbuttons = 0;
398 /* Calculate prefix, if any, for this item. */
399 if (EQ (type, QCtoggle))
400 prefix = NILP (selected) ? "[ ] " : "[X] ";
401 else if (EQ (type, QCradio))
402 prefix = NILP (selected) ? "( ) " : "(*) ";
404 /* Not a button. If we have earlier buttons, then we need a prefix. */
405 else if (!skp->notbuttons && SREF (item_string, 0) != '\0'
406 && SREF (item_string, 0) != '-')
407 prefix = " ";
409 if (prefix)
411 AUTO_STRING (prefix_obj, prefix);
412 item_string = concat2 (prefix_obj, item_string);
416 if ((FRAME_TERMCAP_P (XFRAME (Vmenu_updating_frame))
417 || FRAME_MSDOS_P (XFRAME (Vmenu_updating_frame)))
418 && !NILP (map))
419 /* Indicate visually that this is a submenu. */
421 AUTO_STRING (space_gt, " >");
422 item_string = concat2 (item_string, space_gt);
425 push_menu_item (item_string, enabled, key,
426 AREF (item_properties, ITEM_PROPERTY_DEF),
427 AREF (item_properties, ITEM_PROPERTY_KEYEQ),
428 AREF (item_properties, ITEM_PROPERTY_TYPE),
429 AREF (item_properties, ITEM_PROPERTY_SELECTED),
430 AREF (item_properties, ITEM_PROPERTY_HELP));
432 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (HAVE_NS) || defined (HAVE_NTGUI)
433 /* Display a submenu using the toolkit. */
434 if (FRAME_WINDOW_P (XFRAME (Vmenu_updating_frame))
435 && ! (NILP (map) || NILP (enabled)))
437 push_submenu_start ();
438 single_keymap_panes (map, Qnil, key, skp->maxdepth - 1);
439 push_submenu_end ();
441 #endif
444 /* Look through KEYMAPS, a vector of keymaps that is NMAPS long,
445 and generate menu panes for them in menu_items. */
447 static void
448 keymap_panes (Lisp_Object *keymaps, ptrdiff_t nmaps)
450 ptrdiff_t mapno;
452 init_menu_items ();
454 /* Loop over the given keymaps, making a pane for each map.
455 But don't make a pane that is empty--ignore that map instead.
456 P is the number of panes we have made so far. */
457 for (mapno = 0; mapno < nmaps; mapno++)
458 single_keymap_panes (keymaps[mapno],
459 Fkeymap_prompt (keymaps[mapno]), Qnil, 10);
461 finish_menu_items ();
464 /* Encode a menu string as appropriate for menu-updating-frame's type. */
465 static Lisp_Object
466 encode_menu_string (Lisp_Object str)
468 /* TTY menu strings are encoded by write_glyphs, when they are
469 delivered to the glass, so no need to encode them here. */
470 if (FRAME_TERMCAP_P (XFRAME (Vmenu_updating_frame)))
471 return str;
472 return ENCODE_MENU_STRING (str);
475 /* Push the items in a single pane defined by the alist PANE. */
476 static void
477 list_of_items (Lisp_Object pane)
479 Lisp_Object tail, item, item1;
481 for (tail = pane; CONSP (tail); tail = XCDR (tail))
483 item = XCAR (tail);
484 if (STRINGP (item))
485 push_menu_item (encode_menu_string (item), Qnil, Qnil, Qt,
486 Qnil, Qnil, Qnil, Qnil);
487 else if (CONSP (item))
489 item1 = XCAR (item);
490 CHECK_STRING (item1);
491 push_menu_item (encode_menu_string (item1), Qt, XCDR (item),
492 Qt, Qnil, Qnil, Qnil, Qnil);
494 else
495 push_left_right_boundary ();
500 /* Push all the panes and items of a menu described by the
501 alist-of-alists MENU.
502 This handles old-fashioned calls to x-popup-menu. */
503 void
504 list_of_panes (Lisp_Object menu)
506 Lisp_Object tail;
508 init_menu_items ();
510 for (tail = menu; CONSP (tail); tail = XCDR (tail))
512 Lisp_Object elt, pane_name, pane_data;
513 elt = XCAR (tail);
514 pane_name = Fcar (elt);
515 CHECK_STRING (pane_name);
516 push_menu_pane (encode_menu_string (pane_name), Qnil);
517 pane_data = Fcdr (elt);
518 CHECK_CONS (pane_data);
519 list_of_items (pane_data);
522 finish_menu_items ();
525 /* Set up data in menu_items for a menu bar item
526 whose event type is ITEM_KEY (with string ITEM_NAME)
527 and whose contents come from the list of keymaps MAPS. */
528 bool
529 parse_single_submenu (Lisp_Object item_key, Lisp_Object item_name,
530 Lisp_Object maps)
532 Lisp_Object length;
533 EMACS_INT len;
534 Lisp_Object *mapvec;
535 ptrdiff_t i;
536 bool top_level_items = 0;
537 USE_SAFE_ALLOCA;
539 length = Flength (maps);
540 len = XINT (length);
542 /* Convert the list MAPS into a vector MAPVEC. */
543 SAFE_ALLOCA_LISP (mapvec, len);
544 for (i = 0; i < len; i++)
546 mapvec[i] = Fcar (maps);
547 maps = Fcdr (maps);
550 /* Loop over the given keymaps, making a pane for each map.
551 But don't make a pane that is empty--ignore that map instead. */
552 for (i = 0; i < len; i++)
554 if (!KEYMAPP (mapvec[i]))
556 /* Here we have a command at top level in the menu bar
557 as opposed to a submenu. */
558 top_level_items = 1;
559 push_menu_pane (Qnil, Qnil);
560 push_menu_item (item_name, Qt, item_key, mapvec[i],
561 Qnil, Qnil, Qnil, Qnil);
563 else
565 Lisp_Object prompt;
566 prompt = Fkeymap_prompt (mapvec[i]);
567 single_keymap_panes (mapvec[i],
568 !NILP (prompt) ? prompt : item_name,
569 item_key, 10);
573 SAFE_FREE ();
574 return top_level_items;
578 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (HAVE_NS) || defined (HAVE_NTGUI)
580 /* Allocate and basically initialize widget_value, blocking input. */
582 widget_value *
583 make_widget_value (const char *name, char *value,
584 bool enabled, Lisp_Object help)
586 widget_value *wv;
588 block_input ();
589 wv = xzalloc (sizeof (widget_value));
590 unblock_input ();
592 wv->name = (char *) name;
593 wv->value = value;
594 wv->enabled = enabled;
595 wv->help = help;
596 return wv;
599 /* This recursively calls xfree on the tree of widgets.
600 It must free all data that was malloc'ed for these widget_values.
601 In Emacs, many slots are pointers into the data of Lisp_Strings, and
602 must be left alone. */
604 void
605 free_menubar_widget_value_tree (widget_value *wv)
607 if (! wv) return;
609 wv->name = wv->value = wv->key = (char *) 0xDEADBEEF;
611 if (wv->contents && (wv->contents != (widget_value*)1))
613 free_menubar_widget_value_tree (wv->contents);
614 wv->contents = (widget_value *) 0xDEADBEEF;
616 if (wv->next)
618 free_menubar_widget_value_tree (wv->next);
619 wv->next = (widget_value *) 0xDEADBEEF;
621 block_input ();
622 xfree (wv);
623 unblock_input ();
626 /* Create a tree of widget_value objects
627 representing the panes and items
628 in menu_items starting at index START, up to index END. */
630 widget_value *
631 digest_single_submenu (int start, int end, bool top_level_items)
633 widget_value *wv, *prev_wv, *save_wv, *first_wv;
634 int i;
635 int submenu_depth = 0;
636 widget_value **submenu_stack;
637 bool panes_seen = 0;
638 struct frame *f = XFRAME (Vmenu_updating_frame);
639 USE_SAFE_ALLOCA;
641 SAFE_NALLOCA (submenu_stack, 1, menu_items_used);
642 wv = make_widget_value ("menu", NULL, true, Qnil);
643 wv->button_type = BUTTON_TYPE_NONE;
644 first_wv = wv;
645 save_wv = 0;
646 prev_wv = 0;
648 /* Loop over all panes and items made by the preceding call
649 to parse_single_submenu and construct a tree of widget_value objects.
650 Ignore the panes and items used by previous calls to
651 digest_single_submenu, even though those are also in menu_items. */
652 i = start;
653 while (i < end)
655 if (EQ (AREF (menu_items, i), Qnil))
657 submenu_stack[submenu_depth++] = save_wv;
658 save_wv = prev_wv;
659 prev_wv = 0;
660 i++;
662 else if (EQ (AREF (menu_items, i), Qlambda))
664 prev_wv = save_wv;
665 save_wv = submenu_stack[--submenu_depth];
666 i++;
668 else if (EQ (AREF (menu_items, i), Qt)
669 && submenu_depth != 0)
670 i += MENU_ITEMS_PANE_LENGTH;
671 /* Ignore a nil in the item list.
672 It's meaningful only for dialog boxes. */
673 else if (EQ (AREF (menu_items, i), Qquote))
674 i += 1;
675 else if (EQ (AREF (menu_items, i), Qt))
677 /* Create a new pane. */
678 Lisp_Object pane_name;
679 const char *pane_string;
681 panes_seen = 1;
683 pane_name = AREF (menu_items, i + MENU_ITEMS_PANE_NAME);
685 /* TTY menus display menu items via tty_write_glyphs, which
686 will encode the strings as appropriate. */
687 if (!FRAME_TERMCAP_P (f))
689 #ifdef HAVE_NTGUI
690 if (STRINGP (pane_name))
692 if (unicode_append_menu)
693 /* Encode as UTF-8 for now. */
694 pane_name = ENCODE_UTF_8 (pane_name);
695 else if (STRING_MULTIBYTE (pane_name))
696 pane_name = ENCODE_SYSTEM (pane_name);
698 ASET (menu_items, i + MENU_ITEMS_PANE_NAME, pane_name);
700 #elif defined (USE_LUCID) && defined (HAVE_XFT)
701 if (STRINGP (pane_name))
703 pane_name = ENCODE_UTF_8 (pane_name);
704 ASET (menu_items, i + MENU_ITEMS_PANE_NAME, pane_name);
706 #elif !defined (HAVE_MULTILINGUAL_MENU)
707 if (STRINGP (pane_name) && STRING_MULTIBYTE (pane_name))
709 pane_name = ENCODE_MENU_STRING (pane_name);
710 ASET (menu_items, i + MENU_ITEMS_PANE_NAME, pane_name);
712 #endif
715 pane_string = (NILP (pane_name)
716 ? "" : SSDATA (pane_name));
717 /* If there is just one top-level pane, put all its items directly
718 under the top-level menu. */
719 if (menu_items_n_panes == 1)
720 pane_string = "";
722 /* If the pane has a meaningful name,
723 make the pane a top-level menu item
724 with its items as a submenu beneath it. */
725 if (strcmp (pane_string, ""))
727 /* Set value to 1 so update_submenu_strings can handle '@'. */
728 wv = make_widget_value (NULL, (char *) 1, true, Qnil);
729 if (save_wv)
730 save_wv->next = wv;
731 else
732 first_wv->contents = wv;
733 wv->lname = pane_name;
734 wv->button_type = BUTTON_TYPE_NONE;
735 save_wv = wv;
737 else
738 save_wv = first_wv;
740 prev_wv = 0;
741 i += MENU_ITEMS_PANE_LENGTH;
743 else
745 /* Create a new item within current pane. */
746 Lisp_Object item_name, enable, descrip, def, type, selected;
747 Lisp_Object help;
749 /* All items should be contained in panes. */
750 if (! panes_seen)
751 emacs_abort ();
753 item_name = AREF (menu_items, i + MENU_ITEMS_ITEM_NAME);
754 enable = AREF (menu_items, i + MENU_ITEMS_ITEM_ENABLE);
755 descrip = AREF (menu_items, i + MENU_ITEMS_ITEM_EQUIV_KEY);
756 def = AREF (menu_items, i + MENU_ITEMS_ITEM_DEFINITION);
757 type = AREF (menu_items, i + MENU_ITEMS_ITEM_TYPE);
758 selected = AREF (menu_items, i + MENU_ITEMS_ITEM_SELECTED);
759 help = AREF (menu_items, i + MENU_ITEMS_ITEM_HELP);
761 /* TTY menu items and their descriptions will be encoded by
762 tty_write_glyphs. */
763 if (!FRAME_TERMCAP_P (f))
765 #ifdef HAVE_NTGUI
766 if (STRINGP (item_name))
768 if (unicode_append_menu)
769 item_name = ENCODE_UTF_8 (item_name);
770 else if (STRING_MULTIBYTE (item_name))
771 item_name = ENCODE_SYSTEM (item_name);
773 ASET (menu_items, i + MENU_ITEMS_ITEM_NAME, item_name);
776 if (STRINGP (descrip) && STRING_MULTIBYTE (descrip))
778 descrip = ENCODE_SYSTEM (descrip);
779 ASET (menu_items, i + MENU_ITEMS_ITEM_EQUIV_KEY, descrip);
781 #elif USE_LUCID
782 if (STRINGP (item_name))
784 item_name = ENCODE_UTF_8 (item_name);
785 ASET (menu_items, i + MENU_ITEMS_ITEM_NAME, item_name);
788 if (STRINGP (descrip))
790 descrip = ENCODE_UTF_8 (descrip);
791 ASET (menu_items, i + MENU_ITEMS_ITEM_EQUIV_KEY, descrip);
793 #elif !defined (HAVE_MULTILINGUAL_MENU)
794 if (STRING_MULTIBYTE (item_name))
796 item_name = ENCODE_MENU_STRING (item_name);
797 ASET (menu_items, i + MENU_ITEMS_ITEM_NAME, item_name);
800 if (STRINGP (descrip) && STRING_MULTIBYTE (descrip))
802 descrip = ENCODE_MENU_STRING (descrip);
803 ASET (menu_items, i + MENU_ITEMS_ITEM_EQUIV_KEY, descrip);
805 #endif
808 wv = make_widget_value (NULL, NULL, !NILP (enable),
809 STRINGP (help) ? help : Qnil);
810 if (prev_wv)
811 prev_wv->next = wv;
812 else
813 save_wv->contents = wv;
815 wv->lname = item_name;
816 if (!NILP (descrip))
817 wv->lkey = descrip;
818 /* The intptr_t cast avoids a warning. There's no problem
819 as long as pointers have enough bits to hold small integers. */
820 wv->call_data = (!NILP (def) ? (void *) (intptr_t) i : 0);
822 if (NILP (type))
823 wv->button_type = BUTTON_TYPE_NONE;
824 else if (EQ (type, QCradio))
825 wv->button_type = BUTTON_TYPE_RADIO;
826 else if (EQ (type, QCtoggle))
827 wv->button_type = BUTTON_TYPE_TOGGLE;
828 else
829 emacs_abort ();
831 wv->selected = !NILP (selected);
833 prev_wv = wv;
835 i += MENU_ITEMS_ITEM_LENGTH;
839 /* If we have just one "menu item"
840 that was originally a button, return it by itself. */
841 if (top_level_items && first_wv->contents && first_wv->contents->next == 0)
843 wv = first_wv;
844 first_wv = first_wv->contents;
845 xfree (wv);
848 SAFE_FREE ();
849 return first_wv;
852 /* Walk through the widget_value tree starting at FIRST_WV and update
853 the char * pointers from the corresponding lisp values.
854 We do this after building the whole tree, since GC may happen while the
855 tree is constructed, and small strings are relocated. So we must wait
856 until no GC can happen before storing pointers into lisp values. */
857 void
858 update_submenu_strings (widget_value *first_wv)
860 widget_value *wv;
862 for (wv = first_wv; wv; wv = wv->next)
864 if (STRINGP (wv->lname))
866 wv->name = SSDATA (wv->lname);
868 /* Ignore the @ that means "separate pane".
869 This is a kludge, but this isn't worth more time. */
870 if (wv->value == (char *)1)
872 if (wv->name[0] == '@')
873 wv->name++;
874 wv->value = 0;
878 if (STRINGP (wv->lkey))
879 wv->key = SSDATA (wv->lkey);
881 if (wv->contents)
882 update_submenu_strings (wv->contents);
886 /* Find the menu selection and store it in the keyboard buffer.
887 F is the frame the menu is on.
888 MENU_BAR_ITEMS_USED is the length of VECTOR.
889 VECTOR is an array of menu events for the whole menu. */
891 void
892 find_and_call_menu_selection (struct frame *f, int menu_bar_items_used,
893 Lisp_Object vector, void *client_data)
895 Lisp_Object prefix, entry;
896 Lisp_Object *subprefix_stack;
897 int submenu_depth = 0;
898 int i;
899 USE_SAFE_ALLOCA;
901 entry = Qnil;
902 SAFE_NALLOCA (subprefix_stack, 1, menu_bar_items_used);
903 prefix = Qnil;
904 i = 0;
906 while (i < menu_bar_items_used)
908 if (EQ (AREF (vector, i), Qnil))
910 subprefix_stack[submenu_depth++] = prefix;
911 prefix = entry;
912 i++;
914 else if (EQ (AREF (vector, i), Qlambda))
916 prefix = subprefix_stack[--submenu_depth];
917 i++;
919 else if (EQ (AREF (vector, i), Qt))
921 prefix = AREF (vector, i + MENU_ITEMS_PANE_PREFIX);
922 i += MENU_ITEMS_PANE_LENGTH;
924 else
926 entry = AREF (vector, i + MENU_ITEMS_ITEM_VALUE);
927 /* Treat the pointer as an integer. There's no problem
928 as long as pointers have enough bits to hold small integers. */
929 if ((intptr_t) client_data == i)
931 int j;
932 struct input_event buf;
933 Lisp_Object frame;
934 EVENT_INIT (buf);
936 XSETFRAME (frame, f);
937 buf.kind = MENU_BAR_EVENT;
938 buf.frame_or_window = frame;
939 buf.arg = frame;
940 kbd_buffer_store_event (&buf);
942 for (j = 0; j < submenu_depth; j++)
943 if (!NILP (subprefix_stack[j]))
945 buf.kind = MENU_BAR_EVENT;
946 buf.frame_or_window = frame;
947 buf.arg = subprefix_stack[j];
948 kbd_buffer_store_event (&buf);
951 if (!NILP (prefix))
953 buf.kind = MENU_BAR_EVENT;
954 buf.frame_or_window = frame;
955 buf.arg = prefix;
956 kbd_buffer_store_event (&buf);
959 buf.kind = MENU_BAR_EVENT;
960 buf.frame_or_window = frame;
961 buf.arg = entry;
962 kbd_buffer_store_event (&buf);
964 break;
966 i += MENU_ITEMS_ITEM_LENGTH;
970 SAFE_FREE ();
973 #endif /* USE_X_TOOLKIT || USE_GTK || HAVE_NS || HAVE_NTGUI */
975 #ifdef HAVE_NS
976 /* As above, but return the menu selection instead of storing in kb buffer.
977 If KEYMAPS, return full prefixes to selection. */
978 Lisp_Object
979 find_and_return_menu_selection (struct frame *f, bool keymaps, void *client_data)
981 Lisp_Object prefix, entry;
982 int i;
983 Lisp_Object *subprefix_stack;
984 int submenu_depth = 0;
985 USE_SAFE_ALLOCA;
987 prefix = entry = Qnil;
988 i = 0;
989 SAFE_ALLOCA_LISP (subprefix_stack, menu_items_used);
991 while (i < menu_items_used)
993 if (EQ (AREF (menu_items, i), Qnil))
995 subprefix_stack[submenu_depth++] = prefix;
996 prefix = entry;
997 i++;
999 else if (EQ (AREF (menu_items, i), Qlambda))
1001 prefix = subprefix_stack[--submenu_depth];
1002 i++;
1004 else if (EQ (AREF (menu_items, i), Qt))
1006 prefix
1007 = AREF (menu_items, i + MENU_ITEMS_PANE_PREFIX);
1008 i += MENU_ITEMS_PANE_LENGTH;
1010 /* Ignore a nil in the item list.
1011 It's meaningful only for dialog boxes. */
1012 else if (EQ (AREF (menu_items, i), Qquote))
1013 i += 1;
1014 else
1016 entry
1017 = AREF (menu_items, i + MENU_ITEMS_ITEM_VALUE);
1018 if (aref_addr (menu_items, i) == client_data)
1020 if (keymaps)
1022 int j;
1024 entry = list1 (entry);
1025 if (!NILP (prefix))
1026 entry = Fcons (prefix, entry);
1027 for (j = submenu_depth - 1; j >= 0; j--)
1028 if (!NILP (subprefix_stack[j]))
1029 entry = Fcons (subprefix_stack[j], entry);
1031 SAFE_FREE ();
1032 return entry;
1034 i += MENU_ITEMS_ITEM_LENGTH;
1037 SAFE_FREE ();
1038 return Qnil;
1040 #endif /* HAVE_NS */
1042 ptrdiff_t
1043 menu_item_width (const unsigned char *str)
1045 ptrdiff_t len;
1046 const unsigned char *p;
1048 for (len = 0, p = str; *p; )
1050 int ch_len;
1051 int ch = STRING_CHAR_AND_LENGTH (p, ch_len);
1053 len += CHAR_WIDTH (ch);
1054 p += ch_len;
1056 return len;
1059 DEFUN ("menu-bar-menu-at-x-y", Fmenu_bar_menu_at_x_y, Smenu_bar_menu_at_x_y,
1060 2, 3, 0,
1061 doc: /* Return the menu-bar menu on FRAME at pixel coordinates X, Y.
1062 X and Y are frame-relative pixel coordinates, assumed to define
1063 a location within the menu bar.
1064 If FRAME is nil or omitted, it defaults to the selected frame.
1066 Value is the symbol of the menu at X/Y, or nil if the specified
1067 coordinates are not within the FRAME's menu bar. The symbol can
1068 be used to look up the menu like this:
1070 (lookup-key MAP [menu-bar SYMBOL])
1072 where MAP is either the current global map or the current local map,
1073 since menu-bar items come from both.
1075 This function can return non-nil only on a text-terminal frame
1076 or on an X frame that doesn't use any GUI toolkit. Otherwise,
1077 Emacs does not manage the menu bar and cannot convert coordinates
1078 into menu items. */)
1079 (Lisp_Object x, Lisp_Object y, Lisp_Object frame)
1081 int row, col;
1082 struct frame *f = decode_any_frame (frame);
1084 if (!FRAME_LIVE_P (f))
1085 return Qnil;
1087 pixel_to_glyph_coords (f, XINT (x), XINT (y), &col, &row, NULL, 1);
1088 if (0 <= row && row < FRAME_MENU_BAR_LINES (f))
1090 Lisp_Object items, item;
1091 int i;
1093 /* Find the menu bar item under `col'. */
1094 item = Qnil;
1095 items = FRAME_MENU_BAR_ITEMS (f);
1096 /* This loop assumes a single menu-bar line, and will fail to
1097 find an item if it is not in the first line. Note that
1098 make_lispy_event in keyboard.c makes the same assumption. */
1099 for (i = 0; i < ASIZE (items); i += 4)
1101 Lisp_Object pos, str;
1103 str = AREF (items, i + 1);
1104 pos = AREF (items, i + 3);
1105 if (NILP (str))
1106 return item;
1107 if (XINT (pos) <= col
1108 /* We use <= so the blank between 2 items on a TTY is
1109 considered part of the previous item. */
1110 && col <= XINT (pos) + menu_item_width (SDATA (str)))
1112 item = AREF (items, i);
1113 return item;
1117 return Qnil;
1121 DEFUN ("x-popup-menu", Fx_popup_menu, Sx_popup_menu, 2, 2, 0,
1122 doc: /* Pop up a deck-of-cards menu and return user's selection.
1123 POSITION is a position specification. This is either a mouse button event
1124 or a list ((XOFFSET YOFFSET) WINDOW)
1125 where XOFFSET and YOFFSET are positions in pixels from the top left
1126 corner of WINDOW. (WINDOW may be a window or a frame object.)
1127 This controls the position of the top left of the menu as a whole.
1128 If POSITION is t, it means to use the current mouse position.
1130 MENU is a specifier for a menu. For the simplest case, MENU is a keymap.
1131 The menu items come from key bindings that have a menu string as well as
1132 a definition; actually, the "definition" in such a key binding looks like
1133 (STRING . REAL-DEFINITION). To give the menu a title, put a string into
1134 the keymap as a top-level element.
1136 If REAL-DEFINITION is nil, that puts a nonselectable string in the menu.
1137 Otherwise, REAL-DEFINITION should be a valid key binding definition.
1139 You can also use a list of keymaps as MENU.
1140 Then each keymap makes a separate pane.
1142 When MENU is a keymap or a list of keymaps, the return value is the
1143 list of events corresponding to the user's choice. Note that
1144 `x-popup-menu' does not actually execute the command bound to that
1145 sequence of events.
1147 Alternatively, you can specify a menu of multiple panes
1148 with a list of the form (TITLE PANE1 PANE2...),
1149 where each pane is a list of form (TITLE ITEM1 ITEM2...).
1150 Each ITEM is normally a cons cell (STRING . VALUE);
1151 but a string can appear as an item--that makes a nonselectable line
1152 in the menu.
1153 With this form of menu, the return value is VALUE from the chosen item.
1155 If POSITION is nil, don't display the menu at all, just precalculate the
1156 cached information about equivalent key sequences.
1158 If the user gets rid of the menu without making a valid choice, for
1159 instance by clicking the mouse away from a valid choice or by typing
1160 keyboard input, then this normally results in a quit and
1161 `x-popup-menu' does not return. But if POSITION is a mouse button
1162 event (indicating that the user invoked the menu with the mouse) then
1163 no quit occurs and `x-popup-menu' returns nil. */)
1164 (Lisp_Object position, Lisp_Object menu)
1166 Lisp_Object keymap, tem, tem2;
1167 int xpos = 0, ypos = 0;
1168 Lisp_Object title;
1169 const char *error_name = NULL;
1170 Lisp_Object selection = Qnil;
1171 struct frame *f = NULL;
1172 Lisp_Object x, y, window;
1173 int menuflags = 0;
1174 ptrdiff_t specpdl_count = SPECPDL_INDEX ();
1176 if (NILP (position))
1177 /* This is an obsolete call, which wants us to precompute the
1178 keybinding equivalents, but we don't do that any more anyway. */
1179 return Qnil;
1182 bool get_current_pos_p = 0;
1184 /* Decode the first argument: find the window and the coordinates. */
1185 if (EQ (position, Qt)
1186 || (CONSP (position) && (EQ (XCAR (position), Qmenu_bar)
1187 || EQ (XCAR (position), Qtool_bar))))
1189 get_current_pos_p = 1;
1191 else
1193 tem = Fcar (position);
1194 if (CONSP (tem))
1196 window = Fcar (Fcdr (position));
1197 x = XCAR (tem);
1198 y = Fcar (XCDR (tem));
1200 else
1202 menuflags |= MENU_FOR_CLICK;
1203 tem = Fcar (Fcdr (position)); /* EVENT_START (position) */
1204 window = Fcar (tem); /* POSN_WINDOW (tem) */
1205 tem2 = Fcar (Fcdr (tem)); /* POSN_POSN (tem) */
1206 /* The MENU_KBD_NAVIGATION field is set when the menu
1207 was invoked by F10, which probably means they have no
1208 mouse. In that case, we let them switch between
1209 top-level menu-bar menus by using C-f/C-b and
1210 horizontal arrow keys, since they cannot click the
1211 mouse to open a different submenu. This flag is only
1212 supported by tty_menu_show. We set it when POSITION
1213 and last_nonmenu_event are different, which means we
1214 constructed POSITION by hand (in popup-menu, see
1215 menu-bar.el) to look like a mouse click on the menu bar
1216 event. */
1217 if (!EQ (POSN_POSN (last_nonmenu_event),
1218 POSN_POSN (position))
1219 && CONSP (tem2) && EQ (Fcar (tem2), Qmenu_bar))
1220 menuflags |= MENU_KBD_NAVIGATION;
1221 tem = Fcar (Fcdr (Fcdr (tem))); /* POSN_WINDOW_POSN (tem) */
1222 x = Fcar (tem);
1223 y = Fcdr (tem);
1226 /* If a click happens in an external tool bar or a detached
1227 tool bar, x and y is NIL. In that case, use the current
1228 mouse position. This happens for the help button in the
1229 tool bar. Ideally popup-menu should pass NIL to
1230 this function, but it doesn't. */
1231 if (NILP (x) && NILP (y))
1232 get_current_pos_p = 1;
1235 if (get_current_pos_p)
1237 /* Use the mouse's current position. */
1238 struct frame *new_f = SELECTED_FRAME ();
1240 XSETFASTINT (x, 0);
1241 XSETFASTINT (y, 0);
1242 #ifdef HAVE_X_WINDOWS
1243 if (FRAME_X_P (new_f))
1245 /* Can't use mouse_position_hook for X since it returns
1246 coordinates relative to the window the mouse is in,
1247 we need coordinates relative to the edit widget always. */
1248 if (new_f != 0)
1250 int cur_x, cur_y;
1252 x_relative_mouse_position (new_f, &cur_x, &cur_y);
1253 /* cur_x/y may be negative, so use make_number. */
1254 x = make_number (cur_x);
1255 y = make_number (cur_y);
1258 else
1259 #endif /* HAVE_X_WINDOWS */
1261 Lisp_Object bar_window;
1262 enum scroll_bar_part part;
1263 Time time;
1264 void (*mouse_position_hook) (struct frame **, int,
1265 Lisp_Object *,
1266 enum scroll_bar_part *,
1267 Lisp_Object *,
1268 Lisp_Object *,
1269 Time *) =
1270 FRAME_TERMINAL (new_f)->mouse_position_hook;
1272 if (mouse_position_hook)
1273 (*mouse_position_hook) (&new_f, 1, &bar_window,
1274 &part, &x, &y, &time);
1277 if (new_f != 0)
1278 XSETFRAME (window, new_f);
1279 else
1281 window = selected_window;
1282 XSETFASTINT (x, 0);
1283 XSETFASTINT (y, 0);
1287 /* Decode where to put the menu. */
1289 if (FRAMEP (window))
1291 f = XFRAME (window);
1292 xpos = 0;
1293 ypos = 0;
1295 else if (WINDOWP (window))
1297 struct window *win = XWINDOW (window);
1298 CHECK_LIVE_WINDOW (window);
1299 f = XFRAME (WINDOW_FRAME (win));
1301 xpos = WINDOW_LEFT_EDGE_X (win);
1302 ypos = WINDOW_TOP_EDGE_Y (win);
1304 else
1305 /* ??? Not really clean; should be CHECK_WINDOW_OR_FRAME,
1306 but I don't want to make one now. */
1307 CHECK_WINDOW (window);
1309 CHECK_RANGED_INTEGER (x,
1310 (xpos < INT_MIN - MOST_NEGATIVE_FIXNUM
1311 ? (EMACS_INT) INT_MIN - xpos
1312 : MOST_NEGATIVE_FIXNUM),
1313 INT_MAX - xpos);
1314 CHECK_RANGED_INTEGER (y,
1315 (ypos < INT_MIN - MOST_NEGATIVE_FIXNUM
1316 ? (EMACS_INT) INT_MIN - ypos
1317 : MOST_NEGATIVE_FIXNUM),
1318 INT_MAX - ypos);
1319 xpos += XINT (x);
1320 ypos += XINT (y);
1322 XSETFRAME (Vmenu_updating_frame, f);
1325 /* Now parse the lisp menus. */
1326 record_unwind_protect_void (unuse_menu_items);
1328 title = Qnil;
1330 /* Decode the menu items from what was specified. */
1332 keymap = get_keymap (menu, 0, 0);
1333 if (CONSP (keymap))
1335 /* We were given a keymap. Extract menu info from the keymap. */
1336 Lisp_Object prompt;
1338 /* Extract the detailed info to make one pane. */
1339 keymap_panes (&menu, 1);
1341 /* Search for a string appearing directly as an element of the keymap.
1342 That string is the title of the menu. */
1343 prompt = Fkeymap_prompt (keymap);
1344 if (!NILP (prompt))
1345 title = prompt;
1346 #ifdef HAVE_NS /* Is that needed and NS-specific? --Stef */
1347 else
1348 title = build_string ("Select");
1349 #endif
1351 /* Make that be the pane title of the first pane. */
1352 if (!NILP (prompt) && menu_items_n_panes >= 0)
1353 ASET (menu_items, MENU_ITEMS_PANE_NAME, prompt);
1355 menuflags |= MENU_KEYMAPS;
1357 else if (CONSP (menu) && KEYMAPP (XCAR (menu)))
1359 /* We were given a list of keymaps. */
1360 EMACS_INT nmaps = XFASTINT (Flength (menu));
1361 Lisp_Object *maps;
1362 ptrdiff_t i;
1363 USE_SAFE_ALLOCA;
1365 SAFE_ALLOCA_LISP (maps, nmaps);
1366 title = Qnil;
1368 /* The first keymap that has a prompt string
1369 supplies the menu title. */
1370 for (tem = menu, i = 0; CONSP (tem); tem = XCDR (tem))
1372 Lisp_Object prompt;
1374 maps[i++] = keymap = get_keymap (XCAR (tem), 1, 0);
1376 prompt = Fkeymap_prompt (keymap);
1377 if (NILP (title) && !NILP (prompt))
1378 title = prompt;
1381 /* Extract the detailed info to make one pane. */
1382 keymap_panes (maps, nmaps);
1384 /* Make the title be the pane title of the first pane. */
1385 if (!NILP (title) && menu_items_n_panes >= 0)
1386 ASET (menu_items, MENU_ITEMS_PANE_NAME, title);
1388 menuflags |= MENU_KEYMAPS;
1390 SAFE_FREE ();
1392 else
1394 /* We were given an old-fashioned menu. */
1395 title = Fcar (menu);
1396 CHECK_STRING (title);
1398 list_of_panes (Fcdr (menu));
1400 menuflags &= ~MENU_KEYMAPS;
1403 unbind_to (specpdl_count, Qnil);
1405 #ifdef HAVE_WINDOW_SYSTEM
1406 /* Hide a previous tip, if any. */
1407 if (!FRAME_TERMCAP_P (f))
1408 Fx_hide_tip ();
1409 #endif
1411 #ifdef HAVE_NTGUI /* FIXME: Is it really w32-specific? --Stef */
1412 /* If resources from a previous popup menu still exist, does nothing
1413 until the `menu_free_timer' has freed them (see w32fns.c). This
1414 can occur if you press ESC or click outside a menu without selecting
1415 a menu item.
1417 if (current_popup_menu && FRAME_W32_P (f))
1419 discard_menu_items ();
1420 FRAME_DISPLAY_INFO (f)->grabbed = 0;
1421 return Qnil;
1423 #endif
1425 #ifdef HAVE_NS /* FIXME: ns-specific, why? --Stef */
1426 record_unwind_protect_void (discard_menu_items);
1427 #endif
1429 /* Display them in a menu, but not if F is the initial frame that
1430 doesn't have its hooks set (e.g., in a batch session), because
1431 such a frame cannot display menus. */
1432 if (!FRAME_INITIAL_P (f))
1433 selection = FRAME_TERMINAL (f)->menu_show_hook (f, xpos, ypos, menuflags,
1434 title, &error_name);
1436 #ifdef HAVE_NS
1437 unbind_to (specpdl_count, Qnil);
1438 #else
1439 discard_menu_items ();
1440 #endif
1442 #ifdef HAVE_NTGUI /* FIXME: Is it really w32-specific? --Stef */
1443 if (FRAME_W32_P (f))
1444 FRAME_DISPLAY_INFO (f)->grabbed = 0;
1445 #endif
1447 if (error_name) error ("%s", error_name);
1448 return selection;
1451 /* If F's terminal is not capable of displaying a popup dialog,
1452 emulate it with a menu. */
1454 static Lisp_Object
1455 emulate_dialog_with_menu (struct frame *f, Lisp_Object contents)
1457 Lisp_Object x, y, frame, newpos, prompt = Fcar (contents);
1458 int x_coord, y_coord;
1460 if (FRAME_WINDOW_P (f))
1462 x_coord = FRAME_PIXEL_WIDTH (f);
1463 y_coord = FRAME_PIXEL_HEIGHT (f);
1465 else
1467 x_coord = FRAME_COLS (f);
1468 /* Center the title at frame middle. (TTY menus have
1469 their upper-left corner at the given position.) */
1470 if (STRINGP (prompt))
1471 x_coord -= SCHARS (prompt);
1472 y_coord = FRAME_TOTAL_LINES (f);
1475 XSETFRAME (frame, f);
1476 XSETINT (x, x_coord / 2);
1477 XSETINT (y, y_coord / 2);
1478 newpos = list2 (list2 (x, y), frame);
1480 return Fx_popup_menu (newpos, list2 (prompt, contents));
1483 DEFUN ("x-popup-dialog", Fx_popup_dialog, Sx_popup_dialog, 2, 3, 0,
1484 doc: /* Pop up a dialog box and return user's selection.
1485 POSITION specifies which frame to use.
1486 This is normally a mouse button event or a window or frame.
1487 If POSITION is t, it means to use the frame the mouse is on.
1488 The dialog box appears in the middle of the specified frame.
1490 CONTENTS specifies the alternatives to display in the dialog box.
1491 It is a list of the form (DIALOG ITEM1 ITEM2...).
1492 Each ITEM is a cons cell (STRING . VALUE).
1493 The return value is VALUE from the chosen item.
1495 An ITEM may also be just a string--that makes a nonselectable item.
1496 An ITEM may also be nil--that means to put all preceding items
1497 on the left of the dialog box and all following items on the right.
1498 (By default, approximately half appear on each side.)
1500 If HEADER is non-nil, the frame title for the box is "Information",
1501 otherwise it is "Question".
1503 If the user gets rid of the dialog box without making a valid choice,
1504 for instance using the window manager, then this produces a quit and
1505 `x-popup-dialog' does not return. */)
1506 (Lisp_Object position, Lisp_Object contents, Lisp_Object header)
1508 struct frame *f = NULL;
1509 Lisp_Object window;
1511 /* Decode the first argument: find the window or frame to use. */
1512 if (EQ (position, Qt)
1513 || (CONSP (position) && (EQ (XCAR (position), Qmenu_bar)
1514 || EQ (XCAR (position), Qtool_bar))))
1515 window = selected_window;
1516 else if (CONSP (position))
1518 Lisp_Object tem = XCAR (position);
1519 if (CONSP (tem))
1520 window = Fcar (XCDR (position));
1521 else
1523 tem = Fcar (XCDR (position)); /* EVENT_START (position) */
1524 window = Fcar (tem); /* POSN_WINDOW (tem) */
1527 else if (WINDOWP (position) || FRAMEP (position))
1528 window = position;
1529 else
1530 window = Qnil;
1532 /* Decode where to put the menu. */
1534 if (FRAMEP (window))
1535 f = XFRAME (window);
1536 else if (WINDOWP (window))
1538 CHECK_LIVE_WINDOW (window);
1539 f = XFRAME (WINDOW_FRAME (XWINDOW (window)));
1541 else
1542 /* ??? Not really clean; should be CHECK_WINDOW_OR_FRAME,
1543 but I don't want to make one now. */
1544 CHECK_WINDOW (window);
1546 /* Note that xw_popup_dialog can call menu code, so
1547 Vmenu_updating_frame should be set (Bug#17891). */
1548 eassert (f && FRAME_LIVE_P (f));
1549 XSETFRAME (Vmenu_updating_frame, f);
1551 /* Force a redisplay before showing the dialog. If a frame is created
1552 just before showing the dialog, its contents may not have been fully
1553 drawn, as this depends on timing of events from the X server. Redisplay
1554 is not done when a dialog is shown. If redisplay could be done in the
1555 X event loop (i.e. the X event loop does not run in a signal handler)
1556 this would not be needed.
1558 Do this before creating the widget value that points to Lisp
1559 string contents, because Fredisplay may GC and relocate them. */
1560 Fredisplay (Qt);
1562 /* Display the popup dialog by a terminal-specific hook ... */
1563 if (FRAME_TERMINAL (f)->popup_dialog_hook)
1565 Lisp_Object selection
1566 = FRAME_TERMINAL (f)->popup_dialog_hook (f, header, contents);
1567 #ifdef HAVE_NTGUI
1568 /* NTGUI supports only simple dialogs with Yes/No choices. For
1569 other dialogs, it returns the symbol 'unsupported--w32-dialog',
1570 as a signal for the caller to fall back to the emulation code. */
1571 if (!EQ (selection, Qunsupported__w32_dialog))
1572 #endif
1573 return selection;
1575 /* ... or emulate it with a menu. */
1576 return emulate_dialog_with_menu (f, contents);
1579 void
1580 syms_of_menu (void)
1582 staticpro (&menu_items);
1583 menu_items = Qnil;
1584 menu_items_inuse = Qnil;
1586 defsubr (&Sx_popup_menu);
1587 defsubr (&Sx_popup_dialog);
1588 defsubr (&Smenu_bar_menu_at_x_y);