1 /* Platform-independent code for terminal communications.
3 Copyright (C) 1986, 1988, 1993-1994, 1996, 1999-2016 Free Software
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/>. */
23 #include <limits.h> /* for INT_MAX */
26 #include "character.h"
32 #include "termhooks.h"
33 #include "blockinput.h"
37 #include "../lwlib/lwlib.h"
40 #ifdef HAVE_WINDOW_SYSTEM
42 #endif /* HAVE_WINDOW_SYSTEM */
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 */
55 /* Return non-zero if menus can handle radio and toggle buttons. */
59 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (HAVE_NTGUI) || defined(HAVE_NS)
60 if (FRAME_WINDOW_P (XFRAME (Vmenu_updating_frame
)))
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. */
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
;
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
;
99 menu_items_n_panes
= 0;
100 menu_items_submenu_depth
= 0;
103 /* Call at the end of generating the data in menu_items. */
106 finish_menu_items (void)
111 unuse_menu_items (void)
113 menu_items_inuse
= Qnil
;
116 /* Call when finished using the data for the current menu
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)
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
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. */
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
;
165 /* Ensure that there is room for ITEMS items in the menu_items vector. */
168 ensure_menu_items (int items
)
170 int incr
= items
- (menu_items_allocated
- menu_items_used
);
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. */
184 push_submenu_start (void)
186 ensure_menu_items (1);
187 ASET (menu_items
, menu_items_used
, Qnil
);
189 menu_items_submenu_depth
++;
195 push_submenu_end (void)
197 ensure_menu_items (1);
198 ASET (menu_items
, menu_items_used
, Qlambda
);
200 menu_items_submenu_depth
--;
203 #endif /* USE_X_TOOLKIT || USE_GTK || HAVE_NS || defined HAVE_NTGUI */
205 /* Indicate boundary between left and right. */
208 push_left_right_boundary (void)
210 ensure_menu_items (1);
211 ASET (menu_items
, menu_items_used
, Qquote
);
215 /* Start a new menu pane in menu_items.
216 NAME is the pane name. PREFIX_VEC is a prefix key for this pane. */
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
);
226 ASET (menu_items
, menu_items_used
, name
);
228 ASET (menu_items
, menu_items_used
, prefix_vec
);
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'. */
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. */
260 Lisp_Object pending_maps
;
265 static void single_menu_item (Lisp_Object
, Lisp_Object
, Lisp_Object
,
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. */
276 single_keymap_panes (Lisp_Object keymap
, Lisp_Object pane_name
,
277 Lisp_Object prefix
, int maxdepth
)
281 skp
.pending_maps
= Qnil
;
282 skp
.maxdepth
= maxdepth
;
288 push_menu_pane (pane_name
, prefix
);
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
);
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
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
320 If we encounter submenus deeper than SKP->MAXDEPTH levels, ignore them. */
323 single_menu_item (Lisp_Object key
, Lisp_Object item
, Lisp_Object dummy
, void *skp_v
)
325 Lisp_Object map
, item_string
, enabled
;
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);
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) == '@')
342 /* An enabled separate pane. Remember this to handle it later. */
343 skp
->pending_maps
= Fcons (Fcons (map
, Fcons (item_string
, key
)),
348 /* Simulate radio buttons and toggle boxes by putting a prefix in
352 char const *prefix
= 0;
353 Lisp_Object type
= AREF (item_properties
, ITEM_PROPERTY_TYPE
);
357 = AREF (item_properties
, ITEM_PROPERTY_SELECTED
);
360 /* The first button. Line up previous items in this menu. */
362 int idx
= skp
->notbuttons
; /* Index for first item this menu. */
365 while (idx
< menu_items_used
)
368 = AREF (menu_items
, idx
+ MENU_ITEMS_ITEM_NAME
);
372 submenu
++; /* Skip sub menu. */
374 else if (EQ (tem
, Qlambda
))
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. */
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
;
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) != '-')
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
)))
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);
444 /* Look through KEYMAPS, a vector of keymaps that is NMAPS long,
445 and generate menu panes for them in menu_items. */
448 keymap_panes (Lisp_Object
*keymaps
, ptrdiff_t nmaps
)
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. */
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
)))
472 return ENCODE_MENU_STRING (str
);
475 /* Push the items in a single pane defined by the alist PANE. */
477 list_of_items (Lisp_Object pane
)
479 Lisp_Object tail
, item
, item1
;
481 for (tail
= pane
; CONSP (tail
); tail
= XCDR (tail
))
485 push_menu_item (encode_menu_string (item
), Qnil
, Qnil
, Qt
,
486 Qnil
, Qnil
, Qnil
, Qnil
);
487 else if (CONSP (item
))
490 CHECK_STRING (item1
);
491 push_menu_item (encode_menu_string (item1
), Qt
, XCDR (item
),
492 Qt
, Qnil
, Qnil
, Qnil
, Qnil
);
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. */
504 list_of_panes (Lisp_Object menu
)
510 for (tail
= menu
; CONSP (tail
); tail
= XCDR (tail
))
512 Lisp_Object elt
, pane_name
, pane_data
;
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. */
529 parse_single_submenu (Lisp_Object item_key
, Lisp_Object item_name
,
536 bool top_level_items
= 0;
539 length
= Flength (maps
);
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
);
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. */
559 push_menu_pane (Qnil
, Qnil
);
560 push_menu_item (item_name
, Qt
, item_key
, mapvec
[i
],
561 Qnil
, Qnil
, Qnil
, Qnil
);
566 prompt
= Fkeymap_prompt (mapvec
[i
]);
567 single_keymap_panes (mapvec
[i
],
568 !NILP (prompt
) ? prompt
: item_name
,
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. */
583 make_widget_value (const char *name
, char *value
,
584 bool enabled
, Lisp_Object help
)
589 wv
= xzalloc (sizeof (widget_value
));
592 wv
->name
= (char *) name
;
594 wv
->enabled
= enabled
;
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. */
605 free_menubar_widget_value_tree (widget_value
*wv
)
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;
618 free_menubar_widget_value_tree (wv
->next
);
619 wv
->next
= (widget_value
*) 0xDEADBEEF;
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. */
631 digest_single_submenu (int start
, int end
, bool top_level_items
)
633 widget_value
*wv
, *prev_wv
, *save_wv
, *first_wv
;
635 int submenu_depth
= 0;
636 widget_value
**submenu_stack
;
638 struct frame
*f
= XFRAME (Vmenu_updating_frame
);
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
;
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. */
655 if (EQ (AREF (menu_items
, i
), Qnil
))
657 submenu_stack
[submenu_depth
++] = save_wv
;
662 else if (EQ (AREF (menu_items
, i
), Qlambda
))
665 save_wv
= submenu_stack
[--submenu_depth
];
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
))
675 else if (EQ (AREF (menu_items
, i
), Qt
))
677 /* Create a new pane. */
678 Lisp_Object pane_name
;
679 const char *pane_string
;
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
))
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
);
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)
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
);
732 first_wv
->contents
= wv
;
733 wv
->lname
= pane_name
;
734 wv
->button_type
= BUTTON_TYPE_NONE
;
741 i
+= MENU_ITEMS_PANE_LENGTH
;
745 /* Create a new item within current pane. */
746 Lisp_Object item_name
, enable
, descrip
, def
, type
, selected
;
749 /* All items should be contained in panes. */
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
763 if (!FRAME_TERMCAP_P (f
))
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
);
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
);
808 wv
= make_widget_value (NULL
, NULL
, !NILP (enable
),
809 STRINGP (help
) ? help
: Qnil
);
813 save_wv
->contents
= wv
;
815 wv
->lname
= item_name
;
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);
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
;
831 wv
->selected
= !NILP (selected
);
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)
844 first_wv
= first_wv
->contents
;
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. */
858 update_submenu_strings (widget_value
*first_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] == '@')
878 if (STRINGP (wv
->lkey
))
879 wv
->key
= SSDATA (wv
->lkey
);
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. */
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;
902 SAFE_NALLOCA (subprefix_stack
, 1, menu_bar_items_used
);
906 while (i
< menu_bar_items_used
)
908 if (EQ (AREF (vector
, i
), Qnil
))
910 subprefix_stack
[submenu_depth
++] = prefix
;
914 else if (EQ (AREF (vector
, i
), Qlambda
))
916 prefix
= subprefix_stack
[--submenu_depth
];
919 else if (EQ (AREF (vector
, i
), Qt
))
921 prefix
= AREF (vector
, i
+ MENU_ITEMS_PANE_PREFIX
);
922 i
+= MENU_ITEMS_PANE_LENGTH
;
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
)
932 struct input_event buf
;
936 XSETFRAME (frame
, f
);
937 buf
.kind
= MENU_BAR_EVENT
;
938 buf
.frame_or_window
= 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
);
953 buf
.kind
= MENU_BAR_EVENT
;
954 buf
.frame_or_window
= frame
;
956 kbd_buffer_store_event (&buf
);
959 buf
.kind
= MENU_BAR_EVENT
;
960 buf
.frame_or_window
= frame
;
962 kbd_buffer_store_event (&buf
);
966 i
+= MENU_ITEMS_ITEM_LENGTH
;
973 #endif /* USE_X_TOOLKIT || USE_GTK || HAVE_NS || HAVE_NTGUI */
976 /* As above, but return the menu selection instead of storing in kb buffer.
977 If KEYMAPS, return full prefixes to selection. */
979 find_and_return_menu_selection (struct frame
*f
, bool keymaps
, void *client_data
)
981 Lisp_Object prefix
, entry
;
983 Lisp_Object
*subprefix_stack
;
984 int submenu_depth
= 0;
987 prefix
= entry
= Qnil
;
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
;
999 else if (EQ (AREF (menu_items
, i
), Qlambda
))
1001 prefix
= subprefix_stack
[--submenu_depth
];
1004 else if (EQ (AREF (menu_items
, i
), Qt
))
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
))
1017 = AREF (menu_items
, i
+ MENU_ITEMS_ITEM_VALUE
);
1018 if (aref_addr (menu_items
, i
) == client_data
)
1024 entry
= list1 (entry
);
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
);
1034 i
+= MENU_ITEMS_ITEM_LENGTH
;
1040 #endif /* HAVE_NS */
1043 menu_item_width (const unsigned char *str
)
1046 const unsigned char *p
;
1048 for (len
= 0, p
= str
; *p
; )
1051 int ch
= STRING_CHAR_AND_LENGTH (p
, ch_len
);
1053 len
+= CHAR_WIDTH (ch
);
1059 DEFUN ("menu-bar-menu-at-x-y", Fmenu_bar_menu_at_x_y
, Smenu_bar_menu_at_x_y
,
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
)
1082 struct frame
*f
= decode_any_frame (frame
);
1084 if (!FRAME_LIVE_P (f
))
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
;
1093 /* Find the menu bar item under `col'. */
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);
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
);
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
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
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;
1169 const char *error_name
= NULL
;
1170 Lisp_Object selection
= Qnil
;
1171 struct frame
*f
= NULL
;
1172 Lisp_Object x
, y
, window
;
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. */
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;
1193 tem
= Fcar (position
);
1196 window
= Fcar (Fcdr (position
));
1198 y
= Fcar (XCDR (tem
));
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
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) */
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 ();
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. */
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
);
1259 #endif /* HAVE_X_WINDOWS */
1261 Lisp_Object bar_window
;
1262 enum scroll_bar_part part
;
1264 void (*mouse_position_hook
) (struct frame
**, int,
1266 enum scroll_bar_part
*,
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
);
1278 XSETFRAME (window
, new_f
);
1281 window
= selected_window
;
1287 /* Decode where to put the menu. */
1289 if (FRAMEP (window
))
1291 f
= XFRAME (window
);
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
);
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
),
1314 CHECK_RANGED_INTEGER (y
,
1315 (ypos
< INT_MIN
- MOST_NEGATIVE_FIXNUM
1316 ? (EMACS_INT
) INT_MIN
- ypos
1317 : MOST_NEGATIVE_FIXNUM
),
1322 XSETFRAME (Vmenu_updating_frame
, f
);
1325 /* Now parse the lisp menus. */
1326 record_unwind_protect_void (unuse_menu_items
);
1330 /* Decode the menu items from what was specified. */
1332 keymap
= get_keymap (menu
, 0, 0);
1335 /* We were given a keymap. Extract menu info from the keymap. */
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
);
1346 #ifdef HAVE_NS /* Is that needed and NS-specific? --Stef */
1348 title
= build_string ("Select");
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
));
1365 SAFE_ALLOCA_LISP (maps
, nmaps
);
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
))
1374 maps
[i
++] = keymap
= get_keymap (XCAR (tem
), 1, 0);
1376 prompt
= Fkeymap_prompt (keymap
);
1377 if (NILP (title
) && !NILP (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
;
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
))
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
1417 if (current_popup_menu
&& FRAME_W32_P (f
))
1419 discard_menu_items ();
1420 FRAME_DISPLAY_INFO (f
)->grabbed
= 0;
1425 #ifdef HAVE_NS /* FIXME: ns-specific, why? --Stef */
1426 record_unwind_protect_void (discard_menu_items
);
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
);
1437 unbind_to (specpdl_count
, Qnil
);
1439 discard_menu_items ();
1442 #ifdef HAVE_NTGUI /* FIXME: Is it really w32-specific? --Stef */
1443 if (FRAME_W32_P (f
))
1444 FRAME_DISPLAY_INFO (f
)->grabbed
= 0;
1447 if (error_name
) error ("%s", error_name
);
1451 /* If F's terminal is not capable of displaying a popup dialog,
1452 emulate it with a menu. */
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
);
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
;
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
);
1520 window
= Fcar (XCDR (position
));
1523 tem
= Fcar (XCDR (position
)); /* EVENT_START (position) */
1524 window
= Fcar (tem
); /* POSN_WINDOW (tem) */
1527 else if (WINDOWP (position
) || FRAMEP (position
))
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
)));
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. */
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
);
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
))
1575 /* ... or emulate it with a menu. */
1576 return emulate_dialog_with_menu (f
, contents
);
1582 staticpro (&menu_items
);
1584 menu_items_inuse
= Qnil
;
1586 defsubr (&Sx_popup_menu
);
1587 defsubr (&Sx_popup_dialog
);
1588 defsubr (&Smenu_bar_menu_at_x_y
);