Fixes: debbugs:17865
[bpt/emacs.git] / src / menu.c
CommitLineData
279a1d4b 1/* Platform-independent code for terminal communications.
95df8112 2
ba318903 3Copyright (C) 1986, 1988, 1993-1994, 1996, 1999-2014 Free Software
ab422c4d 4Foundation, Inc.
279a1d4b
CY
5
6This file is part of GNU Emacs.
7
8GNU Emacs is free software: you can redistribute it and/or modify
9it under the terms of the GNU General Public License as published by
10the Free Software Foundation, either version 3 of the License, or
11(at your option) any later version.
12
13GNU Emacs is distributed in the hope that it will be useful,
14but WITHOUT ANY WARRANTY; without even the implied warranty of
15MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16GNU General Public License for more details.
17
18You should have received a copy of the GNU General Public License
19along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
20
21#include <config.h>
22#include <stdio.h>
3c24731f 23#include <limits.h> /* for INT_MAX */
279a1d4b
CY
24
25#include "lisp.h"
26#include "keyboard.h"
27#include "keymap.h"
28#include "frame.h"
ef7417fd 29#include "window.h"
279a1d4b
CY
30#include "termhooks.h"
31#include "blockinput.h"
32#include "dispextern.h"
401cf890 33#include "buffer.h"
279a1d4b
CY
34
35#ifdef USE_X_TOOLKIT
36#include "../lwlib/lwlib.h"
37#endif
38
17a2cbbd
DC
39#ifdef HAVE_WINDOW_SYSTEM
40#include TERM_HEADER
41#endif /* HAVE_WINDOW_SYSTEM */
279a1d4b
CY
42
43#ifdef HAVE_NTGUI
0fda9b75
DC
44# ifdef NTGUI_UNICODE
45# define unicode_append_menu AppendMenuW
46# else /* !NTGUI_UNICODE */
279a1d4b 47extern AppendMenuW_Proc unicode_append_menu;
0fda9b75 48# endif /* NTGUI_UNICODE */
04e452cb 49extern HMENU current_popup_menu;
279a1d4b
CY
50#endif /* HAVE_NTGUI */
51
e7c9048f 52#include "menu.h"
279a1d4b 53
f1303b52
EZ
54/* Return non-zero if menus can handle radio and toggle buttons. */
55static bool
56have_boxes (void)
57{
b911a94d 58#if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (HAVE_NTGUI) || defined(HAVE_NS)
f1303b52
EZ
59 if (FRAME_WINDOW_P (XFRAME (Vmenu_updating_frame)))
60 return 1;
279a1d4b 61#endif
f1303b52
EZ
62 return 0;
63}
279a1d4b 64
279a1d4b
CY
65Lisp_Object menu_items;
66
67/* If non-nil, means that the global vars defined here are already in use.
68 Used to detect cases where we try to re-enter this non-reentrant code. */
4475bec4
PE
69#if ! (defined USE_GTK || defined USE_MOTIF)
70static
71#endif
279a1d4b
CY
72Lisp_Object menu_items_inuse;
73
74/* Number of slots currently allocated in menu_items. */
75int menu_items_allocated;
76
77/* This is the index in menu_items of the first empty slot. */
78int menu_items_used;
79
80/* The number of panes currently recorded in menu_items,
81 excluding those within submenus. */
82int menu_items_n_panes;
83
84/* Current depth within submenus. */
85static int menu_items_submenu_depth;
86
87void
971de7fb 88init_menu_items (void)
279a1d4b
CY
89{
90 if (!NILP (menu_items_inuse))
91 error ("Trying to use a menu from within a menu-entry");
92
93 if (NILP (menu_items))
94 {
95 menu_items_allocated = 60;
96 menu_items = Fmake_vector (make_number (menu_items_allocated), Qnil);
97 }
98
99 menu_items_inuse = Qt;
100 menu_items_used = 0;
101 menu_items_n_panes = 0;
102 menu_items_submenu_depth = 0;
103}
104
105/* Call at the end of generating the data in menu_items. */
106
107void
971de7fb 108finish_menu_items (void)
279a1d4b
CY
109{
110}
111
27e498e6
PE
112void
113unuse_menu_items (void)
279a1d4b 114{
27e498e6 115 menu_items_inuse = Qnil;
279a1d4b
CY
116}
117
118/* Call when finished using the data for the current menu
119 in menu_items. */
120
121void
971de7fb 122discard_menu_items (void)
279a1d4b
CY
123{
124 /* Free the structure if it is especially large.
125 Otherwise, hold on to it, to save time. */
126 if (menu_items_allocated > 200)
127 {
128 menu_items = Qnil;
129 menu_items_allocated = 0;
130 }
a54e2c05 131 eassert (NILP (menu_items_inuse));
279a1d4b
CY
132}
133
134/* This undoes save_menu_items, and it is called by the specpdl unwind
135 mechanism. */
136
27e498e6 137static void
971de7fb 138restore_menu_items (Lisp_Object saved)
279a1d4b
CY
139{
140 menu_items = XCAR (saved);
141 menu_items_inuse = (! NILP (menu_items) ? Qt : Qnil);
142 menu_items_allocated = (VECTORP (menu_items) ? ASIZE (menu_items) : 0);
143 saved = XCDR (saved);
144 menu_items_used = XINT (XCAR (saved));
145 saved = XCDR (saved);
146 menu_items_n_panes = XINT (XCAR (saved));
147 saved = XCDR (saved);
148 menu_items_submenu_depth = XINT (XCAR (saved));
279a1d4b
CY
149}
150
151/* Push the whole state of menu_items processing onto the specpdl.
152 It will be restored when the specpdl is unwound. */
153
154void
971de7fb 155save_menu_items (void)
279a1d4b
CY
156{
157 Lisp_Object saved = list4 (!NILP (menu_items_inuse) ? menu_items : Qnil,
158 make_number (menu_items_used),
159 make_number (menu_items_n_panes),
160 make_number (menu_items_submenu_depth));
161 record_unwind_protect (restore_menu_items, saved);
162 menu_items_inuse = Qnil;
163 menu_items = Qnil;
164}
165
166\f
d311d28c 167/* Ensure that there is room for ITEMS items in the menu_items vector. */
279a1d4b
CY
168
169static void
d311d28c 170ensure_menu_items (int items)
279a1d4b 171{
d311d28c 172 int incr = items - (menu_items_allocated - menu_items_used);
908589fd 173 if (incr > 0)
d311d28c
PE
174 {
175 menu_items = larger_vector (menu_items, incr, INT_MAX);
176 menu_items_allocated = ASIZE (menu_items);
177 }
279a1d4b
CY
178}
179
60d9e1db
PE
180#if (defined USE_X_TOOLKIT || defined USE_GTK || defined HAVE_NS \
181 || defined HAVE_NTGUI)
182
279a1d4b
CY
183/* Begin a submenu. */
184
185static void
971de7fb 186push_submenu_start (void)
279a1d4b 187{
d311d28c 188 ensure_menu_items (1);
fb7da12e
AS
189 ASET (menu_items, menu_items_used, Qnil);
190 menu_items_used++;
279a1d4b
CY
191 menu_items_submenu_depth++;
192}
193
194/* End a submenu. */
195
196static void
971de7fb 197push_submenu_end (void)
279a1d4b 198{
d311d28c 199 ensure_menu_items (1);
fb7da12e
AS
200 ASET (menu_items, menu_items_used, Qlambda);
201 menu_items_used++;
279a1d4b
CY
202 menu_items_submenu_depth--;
203}
204
60d9e1db
PE
205#endif /* USE_X_TOOLKIT || USE_GTK || HAVE_NS || defined HAVE_NTGUI */
206
279a1d4b
CY
207/* Indicate boundary between left and right. */
208
209static void
971de7fb 210push_left_right_boundary (void)
279a1d4b 211{
d311d28c 212 ensure_menu_items (1);
fb7da12e
AS
213 ASET (menu_items, menu_items_used, Qquote);
214 menu_items_used++;
279a1d4b
CY
215}
216
217/* Start a new menu pane in menu_items.
218 NAME is the pane name. PREFIX_VEC is a prefix key for this pane. */
219
593c843c 220static void
971de7fb 221push_menu_pane (Lisp_Object name, Lisp_Object prefix_vec)
279a1d4b 222{
d311d28c 223 ensure_menu_items (MENU_ITEMS_PANE_LENGTH);
279a1d4b
CY
224 if (menu_items_submenu_depth == 0)
225 menu_items_n_panes++;
fb7da12e
AS
226 ASET (menu_items, menu_items_used, Qt);
227 menu_items_used++;
228 ASET (menu_items, menu_items_used, name);
229 menu_items_used++;
230 ASET (menu_items, menu_items_used, prefix_vec);
231 menu_items_used++;
279a1d4b
CY
232}
233
234/* Push one menu item into the current pane. NAME is the string to
235 display. ENABLE if non-nil means this item can be selected. KEY
236 is the key generated by choosing this item, or nil if this item
237 doesn't really have a definition. DEF is the definition of this
238 item. EQUIV is the textual description of the keyboard equivalent
239 for this item (or nil if none). TYPE is the type of this menu
240 item, one of nil, `toggle' or `radio'. */
241
593c843c 242static void
971de7fb 243push_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)
279a1d4b 244{
d311d28c 245 ensure_menu_items (MENU_ITEMS_ITEM_LENGTH);
279a1d4b 246
b7c7a4d1
SM
247 ASET (menu_items, menu_items_used + MENU_ITEMS_ITEM_NAME, name);
248 ASET (menu_items, menu_items_used + MENU_ITEMS_ITEM_ENABLE, enable);
249 ASET (menu_items, menu_items_used + MENU_ITEMS_ITEM_VALUE, key);
250 ASET (menu_items, menu_items_used + MENU_ITEMS_ITEM_EQUIV_KEY, equiv);
251 ASET (menu_items, menu_items_used + MENU_ITEMS_ITEM_DEFINITION, def);
252 ASET (menu_items, menu_items_used + MENU_ITEMS_ITEM_TYPE, type);
253 ASET (menu_items, menu_items_used + MENU_ITEMS_ITEM_SELECTED, selected);
254 ASET (menu_items, menu_items_used + MENU_ITEMS_ITEM_HELP, help);
255
256 menu_items_used += MENU_ITEMS_ITEM_LENGTH;
279a1d4b
CY
257}
258
259/* Args passed between single_keymap_panes and single_menu_item. */
260struct skp
261 {
262 Lisp_Object pending_maps;
ef7417fd 263 int maxdepth;
279a1d4b
CY
264 int notbuttons;
265 };
266
f57e2426
J
267static void single_menu_item (Lisp_Object, Lisp_Object, Lisp_Object,
268 void *);
279a1d4b
CY
269
270/* This is a recursive subroutine of keymap_panes.
271 It handles one keymap, KEYMAP.
272 The other arguments are passed along
273 or point to local variables of the previous function.
279a1d4b
CY
274
275 If we encounter submenus deeper than MAXDEPTH levels, ignore them. */
276
593c843c 277static void
ef7417fd
SM
278single_keymap_panes (Lisp_Object keymap, Lisp_Object pane_name,
279 Lisp_Object prefix, int maxdepth)
279a1d4b
CY
280{
281 struct skp skp;
282 struct gcpro gcpro1;
283
284 skp.pending_maps = Qnil;
285 skp.maxdepth = maxdepth;
279a1d4b
CY
286 skp.notbuttons = 0;
287
288 if (maxdepth <= 0)
289 return;
290
291 push_menu_pane (pane_name, prefix);
292
f1303b52
EZ
293 if (!have_boxes ())
294 {
295 /* Remember index for first item in this pane so we can go back
296 and add a prefix when (if) we see the first button. After
297 that, notbuttons is set to 0, to mark that we have seen a
298 button and all non button items need a prefix. */
299 skp.notbuttons = menu_items_used;
300 }
279a1d4b
CY
301
302 GCPRO1 (skp.pending_maps);
303 map_keymap_canonical (keymap, single_menu_item, Qnil, &skp);
304 UNGCPRO;
305
306 /* Process now any submenus which want to be panes at this level. */
307 while (CONSP (skp.pending_maps))
308 {
309 Lisp_Object elt, eltcdr, string;
310 elt = XCAR (skp.pending_maps);
311 eltcdr = XCDR (elt);
312 string = XCAR (eltcdr);
313 /* We no longer discard the @ from the beginning of the string here.
314 Instead, we do this in *menu_show. */
ef7417fd 315 single_keymap_panes (Fcar (elt), string, XCDR (eltcdr), maxdepth - 1);
279a1d4b
CY
316 skp.pending_maps = XCDR (skp.pending_maps);
317 }
318}
319
320/* This is a subroutine of single_keymap_panes that handles one
321 keymap entry.
322 KEY is a key in a keymap and ITEM is its binding.
323 SKP->PENDING_MAPS_PTR is a list of keymaps waiting to be made into
324 separate panes.
279a1d4b
CY
325 If we encounter submenus deeper than SKP->MAXDEPTH levels, ignore them. */
326
327static void
971de7fb 328single_menu_item (Lisp_Object key, Lisp_Object item, Lisp_Object dummy, void *skp_v)
279a1d4b
CY
329{
330 Lisp_Object map, item_string, enabled;
331 struct gcpro gcpro1, gcpro2;
9fa1de30 332 bool res;
279a1d4b
CY
333 struct skp *skp = skp_v;
334
335 /* Parse the menu item and leave the result in item_properties. */
336 GCPRO2 (key, item);
ef7417fd 337 res = parse_menu_item (item, 0);
279a1d4b
CY
338 UNGCPRO;
339 if (!res)
340 return; /* Not a menu item. */
341
28be1ada 342 map = AREF (item_properties, ITEM_PROPERTY_MAP);
279a1d4b 343
28be1ada
DA
344 enabled = AREF (item_properties, ITEM_PROPERTY_ENABLE);
345 item_string = AREF (item_properties, ITEM_PROPERTY_NAME);
279a1d4b
CY
346
347 if (!NILP (map) && SREF (item_string, 0) == '@')
348 {
349 if (!NILP (enabled))
350 /* An enabled separate pane. Remember this to handle it later. */
351 skp->pending_maps = Fcons (Fcons (map, Fcons (item_string, key)),
352 skp->pending_maps);
353 return;
354 }
355
279a1d4b
CY
356 /* Simulate radio buttons and toggle boxes by putting a prefix in
357 front of them. */
f1303b52
EZ
358 if (!have_boxes ())
359 {
360 Lisp_Object prefix = Qnil;
361 Lisp_Object type = AREF (item_properties, ITEM_PROPERTY_TYPE);
362 if (!NILP (type))
363 {
364 Lisp_Object selected
365 = AREF (item_properties, ITEM_PROPERTY_SELECTED);
366
367 if (skp->notbuttons)
368 /* The first button. Line up previous items in this menu. */
369 {
370 int idx = skp->notbuttons; /* Index for first item this menu. */
371 int submenu = 0;
372 Lisp_Object tem;
373 while (idx < menu_items_used)
374 {
375 tem
376 = AREF (menu_items, idx + MENU_ITEMS_ITEM_NAME);
377 if (NILP (tem))
378 {
379 idx++;
380 submenu++; /* Skip sub menu. */
381 }
382 else if (EQ (tem, Qlambda))
383 {
384 idx++;
385 submenu--; /* End sub menu. */
386 }
387 else if (EQ (tem, Qt))
388 idx += 3; /* Skip new pane marker. */
389 else if (EQ (tem, Qquote))
390 idx++; /* Skip a left, right divider. */
391 else
392 {
393 if (!submenu && SREF (tem, 0) != '\0'
394 && SREF (tem, 0) != '-')
395 ASET (menu_items, idx + MENU_ITEMS_ITEM_NAME,
396 concat2 (build_string (" "), tem));
397 idx += MENU_ITEMS_ITEM_LENGTH;
398 }
399 }
400 skp->notbuttons = 0;
401 }
402
403 /* Calculate prefix, if any, for this item. */
404 if (EQ (type, QCtoggle))
405 prefix = build_string (NILP (selected) ? "[ ] " : "[X] ");
406 else if (EQ (type, QCradio))
407 prefix = build_string (NILP (selected) ? "( ) " : "(*) ");
408 }
409 /* Not a button. If we have earlier buttons, then we need a prefix. */
410 else if (!skp->notbuttons && SREF (item_string, 0) != '\0'
411 && SREF (item_string, 0) != '-')
412 prefix = build_string (" ");
413
414 if (!NILP (prefix))
415 item_string = concat2 (prefix, item_string);
279a1d4b 416 }
279a1d4b 417
bf6b4923
EZ
418 if ((FRAME_TERMCAP_P (XFRAME (Vmenu_updating_frame))
419 || FRAME_MSDOS_P (XFRAME (Vmenu_updating_frame)))
f1303b52 420 && !NILP (map))
279a1d4b
CY
421 /* Indicate visually that this is a submenu. */
422 item_string = concat2 (item_string, build_string (" >"));
279a1d4b
CY
423
424 push_menu_item (item_string, enabled, key,
28be1ada
DA
425 AREF (item_properties, ITEM_PROPERTY_DEF),
426 AREF (item_properties, ITEM_PROPERTY_KEYEQ),
427 AREF (item_properties, ITEM_PROPERTY_TYPE),
428 AREF (item_properties, ITEM_PROPERTY_SELECTED),
429 AREF (item_properties, ITEM_PROPERTY_HELP));
279a1d4b 430
edfda783 431#if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (HAVE_NS) || defined (HAVE_NTGUI)
279a1d4b 432 /* Display a submenu using the toolkit. */
f1303b52
EZ
433 if (FRAME_WINDOW_P (XFRAME (Vmenu_updating_frame))
434 && ! (NILP (map) || NILP (enabled)))
279a1d4b
CY
435 {
436 push_submenu_start ();
ef7417fd 437 single_keymap_panes (map, Qnil, key, skp->maxdepth - 1);
279a1d4b
CY
438 push_submenu_end ();
439 }
440#endif
441}
442
443/* Look through KEYMAPS, a vector of keymaps that is NMAPS long,
ef7417fd 444 and generate menu panes for them in menu_items. */
279a1d4b 445
ef7417fd 446static void
d311d28c 447keymap_panes (Lisp_Object *keymaps, ptrdiff_t nmaps)
279a1d4b 448{
d311d28c 449 ptrdiff_t mapno;
279a1d4b
CY
450
451 init_menu_items ();
452
453 /* Loop over the given keymaps, making a pane for each map.
454 But don't make a pane that is empty--ignore that map instead.
455 P is the number of panes we have made so far. */
456 for (mapno = 0; mapno < nmaps; mapno++)
457 single_keymap_panes (keymaps[mapno],
ef7417fd 458 Fkeymap_prompt (keymaps[mapno]), Qnil, 10);
279a1d4b
CY
459
460 finish_menu_items ();
461}
462
06284c32
EZ
463/* Encode a menu string as appropriate for menu-updating-frame's type. */
464static Lisp_Object
465encode_menu_string (Lisp_Object str)
466{
467 /* TTY menu strings are encoded by write_glyphs, when they are
468 delivered to the glass, so no need to encode them here. */
469 if (FRAME_TERMCAP_P (XFRAME (Vmenu_updating_frame)))
470 return str;
471 return ENCODE_MENU_STRING (str);
472}
279a1d4b
CY
473
474/* Push the items in a single pane defined by the alist PANE. */
475static void
971de7fb 476list_of_items (Lisp_Object pane)
279a1d4b
CY
477{
478 Lisp_Object tail, item, item1;
479
480 for (tail = pane; CONSP (tail); tail = XCDR (tail))
481 {
482 item = XCAR (tail);
483 if (STRINGP (item))
06284c32 484 push_menu_item (encode_menu_string (item), Qnil, Qnil, Qt,
279a1d4b
CY
485 Qnil, Qnil, Qnil, Qnil);
486 else if (CONSP (item))
487 {
488 item1 = XCAR (item);
489 CHECK_STRING (item1);
06284c32 490 push_menu_item (encode_menu_string (item1), Qt, XCDR (item),
279a1d4b
CY
491 Qt, Qnil, Qnil, Qnil, Qnil);
492 }
493 else
494 push_left_right_boundary ();
495
496 }
497}
498
499/* Push all the panes and items of a menu described by the
500 alist-of-alists MENU.
501 This handles old-fashioned calls to x-popup-menu. */
502void
971de7fb 503list_of_panes (Lisp_Object menu)
279a1d4b
CY
504{
505 Lisp_Object tail;
506
507 init_menu_items ();
508
509 for (tail = menu; CONSP (tail); tail = XCDR (tail))
510 {
511 Lisp_Object elt, pane_name, pane_data;
512 elt = XCAR (tail);
513 pane_name = Fcar (elt);
514 CHECK_STRING (pane_name);
06284c32 515 push_menu_pane (encode_menu_string (pane_name), Qnil);
279a1d4b
CY
516 pane_data = Fcdr (elt);
517 CHECK_CONS (pane_data);
518 list_of_items (pane_data);
519 }
520
521 finish_menu_items ();
522}
523
524/* Set up data in menu_items for a menu bar item
525 whose event type is ITEM_KEY (with string ITEM_NAME)
526 and whose contents come from the list of keymaps MAPS. */
7cded46f
PE
527bool
528parse_single_submenu (Lisp_Object item_key, Lisp_Object item_name,
529 Lisp_Object maps)
279a1d4b
CY
530{
531 Lisp_Object length;
d311d28c 532 EMACS_INT len;
279a1d4b 533 Lisp_Object *mapvec;
d311d28c 534 ptrdiff_t i;
7cded46f 535 bool top_level_items = 0;
d311d28c 536 USE_SAFE_ALLOCA;
279a1d4b
CY
537
538 length = Flength (maps);
539 len = XINT (length);
540
541 /* Convert the list MAPS into a vector MAPVEC. */
d311d28c 542 SAFE_ALLOCA_LISP (mapvec, len);
279a1d4b
CY
543 for (i = 0; i < len; i++)
544 {
545 mapvec[i] = Fcar (maps);
546 maps = Fcdr (maps);
547 }
548
549 /* Loop over the given keymaps, making a pane for each map.
550 But don't make a pane that is empty--ignore that map instead. */
551 for (i = 0; i < len; i++)
552 {
553 if (!KEYMAPP (mapvec[i]))
554 {
555 /* Here we have a command at top level in the menu bar
556 as opposed to a submenu. */
557 top_level_items = 1;
558 push_menu_pane (Qnil, Qnil);
559 push_menu_item (item_name, Qt, item_key, mapvec[i],
560 Qnil, Qnil, Qnil, Qnil);
561 }
562 else
563 {
564 Lisp_Object prompt;
565 prompt = Fkeymap_prompt (mapvec[i]);
566 single_keymap_panes (mapvec[i],
567 !NILP (prompt) ? prompt : item_name,
ef7417fd 568 item_key, 10);
279a1d4b
CY
569 }
570 }
571
d311d28c 572 SAFE_FREE ();
279a1d4b
CY
573 return top_level_items;
574}
575
576\f
edfda783 577#if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (HAVE_NS) || defined (HAVE_NTGUI)
279a1d4b
CY
578
579/* Allocate a widget_value, blocking input. */
580
581widget_value *
971de7fb 582xmalloc_widget_value (void)
279a1d4b
CY
583{
584 widget_value *value;
585
4d7e6e51 586 block_input ();
279a1d4b 587 value = malloc_widget_value ();
4d7e6e51 588 unblock_input ();
279a1d4b
CY
589
590 return value;
591}
592
593/* This recursively calls free_widget_value on the tree of widgets.
594 It must free all data that was malloc'ed for these widget_values.
595 In Emacs, many slots are pointers into the data of Lisp_Strings, and
596 must be left alone. */
597
598void
971de7fb 599free_menubar_widget_value_tree (widget_value *wv)
279a1d4b
CY
600{
601 if (! wv) return;
602
603 wv->name = wv->value = wv->key = (char *) 0xDEADBEEF;
604
605 if (wv->contents && (wv->contents != (widget_value*)1))
606 {
607 free_menubar_widget_value_tree (wv->contents);
608 wv->contents = (widget_value *) 0xDEADBEEF;
609 }
610 if (wv->next)
611 {
612 free_menubar_widget_value_tree (wv->next);
613 wv->next = (widget_value *) 0xDEADBEEF;
614 }
4d7e6e51 615 block_input ();
279a1d4b 616 free_widget_value (wv);
4d7e6e51 617 unblock_input ();
279a1d4b
CY
618}
619
620/* Create a tree of widget_value objects
621 representing the panes and items
622 in menu_items starting at index START, up to index END. */
623
624widget_value *
7cded46f 625digest_single_submenu (int start, int end, bool top_level_items)
279a1d4b
CY
626{
627 widget_value *wv, *prev_wv, *save_wv, *first_wv;
628 int i;
629 int submenu_depth = 0;
630 widget_value **submenu_stack;
7cded46f 631 bool panes_seen = 0;
06284c32 632 struct frame *f = XFRAME (Vmenu_updating_frame);
279a1d4b 633
38182d90 634 submenu_stack = alloca (menu_items_used * sizeof *submenu_stack);
279a1d4b
CY
635 wv = xmalloc_widget_value ();
636 wv->name = "menu";
637 wv->value = 0;
638 wv->enabled = 1;
639 wv->button_type = BUTTON_TYPE_NONE;
640 wv->help = Qnil;
641 first_wv = wv;
642 save_wv = 0;
643 prev_wv = 0;
644
645 /* Loop over all panes and items made by the preceding call
646 to parse_single_submenu and construct a tree of widget_value objects.
647 Ignore the panes and items used by previous calls to
648 digest_single_submenu, even though those are also in menu_items. */
649 i = start;
650 while (i < end)
651 {
28be1ada 652 if (EQ (AREF (menu_items, i), Qnil))
279a1d4b
CY
653 {
654 submenu_stack[submenu_depth++] = save_wv;
655 save_wv = prev_wv;
656 prev_wv = 0;
657 i++;
658 }
28be1ada 659 else if (EQ (AREF (menu_items, i), Qlambda))
279a1d4b
CY
660 {
661 prev_wv = save_wv;
662 save_wv = submenu_stack[--submenu_depth];
663 i++;
664 }
28be1ada 665 else if (EQ (AREF (menu_items, i), Qt)
279a1d4b
CY
666 && submenu_depth != 0)
667 i += MENU_ITEMS_PANE_LENGTH;
668 /* Ignore a nil in the item list.
669 It's meaningful only for dialog boxes. */
28be1ada 670 else if (EQ (AREF (menu_items, i), Qquote))
279a1d4b 671 i += 1;
28be1ada 672 else if (EQ (AREF (menu_items, i), Qt))
279a1d4b
CY
673 {
674 /* Create a new pane. */
b01a1c29 675 Lisp_Object pane_name;
675e2c69 676 const char *pane_string;
279a1d4b 677
7cded46f 678 panes_seen = 1;
279a1d4b 679
28be1ada 680 pane_name = AREF (menu_items, i + MENU_ITEMS_PANE_NAME);
279a1d4b 681
06284c32
EZ
682 /* TTY menus display menu items via tty_write_glyphs, which
683 will encode the strings as appropriate. */
684 if (!FRAME_TERMCAP_P (f))
279a1d4b 685 {
06284c32
EZ
686#ifdef HAVE_NTGUI
687 if (STRINGP (pane_name))
688 {
689 if (unicode_append_menu)
690 /* Encode as UTF-8 for now. */
691 pane_name = ENCODE_UTF_8 (pane_name);
692 else if (STRING_MULTIBYTE (pane_name))
693 pane_name = ENCODE_SYSTEM (pane_name);
279a1d4b 694
06284c32
EZ
695 ASET (menu_items, i + MENU_ITEMS_PANE_NAME, pane_name);
696 }
99852628 697#elif defined (USE_LUCID) && defined (HAVE_XFT)
06284c32
EZ
698 if (STRINGP (pane_name))
699 {
700 pane_name = ENCODE_UTF_8 (pane_name);
701 ASET (menu_items, i + MENU_ITEMS_PANE_NAME, pane_name);
702 }
279a1d4b 703#elif !defined (HAVE_MULTILINGUAL_MENU)
06284c32
EZ
704 if (STRINGP (pane_name) && STRING_MULTIBYTE (pane_name))
705 {
706 pane_name = ENCODE_MENU_STRING (pane_name);
707 ASET (menu_items, i + MENU_ITEMS_PANE_NAME, pane_name);
708 }
279a1d4b 709#endif
06284c32 710 }
279a1d4b
CY
711
712 pane_string = (NILP (pane_name)
51b59d79 713 ? "" : SSDATA (pane_name));
279a1d4b
CY
714 /* If there is just one top-level pane, put all its items directly
715 under the top-level menu. */
716 if (menu_items_n_panes == 1)
717 pane_string = "";
718
719 /* If the pane has a meaningful name,
720 make the pane a top-level menu item
721 with its items as a submenu beneath it. */
722 if (strcmp (pane_string, ""))
723 {
724 wv = xmalloc_widget_value ();
725 if (save_wv)
726 save_wv->next = wv;
727 else
728 first_wv->contents = wv;
729 wv->lname = pane_name;
730 /* Set value to 1 so update_submenu_strings can handle '@' */
731 wv->value = (char *)1;
732 wv->enabled = 1;
733 wv->button_type = BUTTON_TYPE_NONE;
734 wv->help = Qnil;
735 save_wv = wv;
736 }
737 else
738 save_wv = first_wv;
739
740 prev_wv = 0;
741 i += MENU_ITEMS_PANE_LENGTH;
742 }
743 else
744 {
745 /* Create a new item within current pane. */
746 Lisp_Object item_name, enable, descrip, def, type, selected;
747 Lisp_Object help;
748
749 /* All items should be contained in panes. */
7cded46f 750 if (! panes_seen)
1088b922 751 emacs_abort ();
279a1d4b
CY
752
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);
760
06284c32
EZ
761 /* TTY menu items and their descriptions will be encoded by
762 tty_write_glyphs. */
763 if (!FRAME_TERMCAP_P (f))
279a1d4b 764 {
06284c32
EZ
765#ifdef HAVE_NTGUI
766 if (STRINGP (item_name))
767 {
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);
279a1d4b 772
06284c32
EZ
773 ASET (menu_items, i + MENU_ITEMS_ITEM_NAME, item_name);
774 }
279a1d4b 775
06284c32
EZ
776 if (STRINGP (descrip) && STRING_MULTIBYTE (descrip))
777 {
778 descrip = ENCODE_SYSTEM (descrip);
779 ASET (menu_items, i + MENU_ITEMS_ITEM_EQUIV_KEY, descrip);
780 }
99852628 781#elif USE_LUCID
06284c32
EZ
782 if (STRINGP (item_name))
783 {
784 item_name = ENCODE_UTF_8 (item_name);
785 ASET (menu_items, i + MENU_ITEMS_ITEM_NAME, item_name);
786 }
99852628 787
06284c32
EZ
788 if (STRINGP (descrip))
789 {
790 descrip = ENCODE_UTF_8 (descrip);
791 ASET (menu_items, i + MENU_ITEMS_ITEM_EQUIV_KEY, descrip);
792 }
279a1d4b 793#elif !defined (HAVE_MULTILINGUAL_MENU)
06284c32
EZ
794 if (STRING_MULTIBYTE (item_name))
795 {
796 item_name = ENCODE_MENU_STRING (item_name);
797 ASET (menu_items, i + MENU_ITEMS_ITEM_NAME, item_name);
798 }
279a1d4b 799
06284c32
EZ
800 if (STRINGP (descrip) && STRING_MULTIBYTE (descrip))
801 {
802 descrip = ENCODE_MENU_STRING (descrip);
803 ASET (menu_items, i + MENU_ITEMS_ITEM_EQUIV_KEY, descrip);
804 }
279a1d4b 805#endif
06284c32 806 }
279a1d4b
CY
807
808 wv = xmalloc_widget_value ();
809 if (prev_wv)
810 prev_wv->next = wv;
811 else
812 save_wv->contents = wv;
813
814 wv->lname = item_name;
815 if (!NILP (descrip))
816 wv->lkey = descrip;
817 wv->value = 0;
d01a7826 818 /* The intptr_t cast avoids a warning. There's no problem
279a1d4b 819 as long as pointers have enough bits to hold small integers. */
d01a7826 820 wv->call_data = (!NILP (def) ? (void *) (intptr_t) i : 0);
279a1d4b
CY
821 wv->enabled = !NILP (enable);
822
823 if (NILP (type))
824 wv->button_type = BUTTON_TYPE_NONE;
825 else if (EQ (type, QCradio))
826 wv->button_type = BUTTON_TYPE_RADIO;
827 else if (EQ (type, QCtoggle))
828 wv->button_type = BUTTON_TYPE_TOGGLE;
829 else
1088b922 830 emacs_abort ();
279a1d4b
CY
831
832 wv->selected = !NILP (selected);
833 if (! STRINGP (help))
834 help = Qnil;
835
836 wv->help = help;
837
838 prev_wv = wv;
839
840 i += MENU_ITEMS_ITEM_LENGTH;
841 }
842 }
843
844 /* If we have just one "menu item"
845 that was originally a button, return it by itself. */
846 if (top_level_items && first_wv->contents && first_wv->contents->next == 0)
847 {
848 wv = first_wv->contents;
849 free_widget_value (first_wv);
850 return wv;
851 }
852
853 return first_wv;
854}
855
856/* Walk through the widget_value tree starting at FIRST_WV and update
857 the char * pointers from the corresponding lisp values.
858 We do this after building the whole tree, since GC may happen while the
859 tree is constructed, and small strings are relocated. So we must wait
860 until no GC can happen before storing pointers into lisp values. */
861void
971de7fb 862update_submenu_strings (widget_value *first_wv)
279a1d4b
CY
863{
864 widget_value *wv;
865
866 for (wv = first_wv; wv; wv = wv->next)
867 {
868 if (STRINGP (wv->lname))
869 {
51b59d79 870 wv->name = SSDATA (wv->lname);
279a1d4b
CY
871
872 /* Ignore the @ that means "separate pane".
873 This is a kludge, but this isn't worth more time. */
874 if (wv->value == (char *)1)
875 {
876 if (wv->name[0] == '@')
877 wv->name++;
878 wv->value = 0;
879 }
880 }
881
882 if (STRINGP (wv->lkey))
51b59d79 883 wv->key = SSDATA (wv->lkey);
279a1d4b
CY
884
885 if (wv->contents)
886 update_submenu_strings (wv->contents);
887 }
888}
889
890/* Find the menu selection and store it in the keyboard buffer.
891 F is the frame the menu is on.
892 MENU_BAR_ITEMS_USED is the length of VECTOR.
893 VECTOR is an array of menu events for the whole menu. */
894
895void
a10c8269
DA
896find_and_call_menu_selection (struct frame *f, int menu_bar_items_used,
897 Lisp_Object vector, void *client_data)
279a1d4b
CY
898{
899 Lisp_Object prefix, entry;
900 Lisp_Object *subprefix_stack;
901 int submenu_depth = 0;
902 int i;
903
904 entry = Qnil;
38182d90 905 subprefix_stack = alloca (menu_bar_items_used * sizeof *subprefix_stack);
279a1d4b
CY
906 prefix = Qnil;
907 i = 0;
908
909 while (i < menu_bar_items_used)
910 {
28be1ada 911 if (EQ (AREF (vector, i), Qnil))
279a1d4b
CY
912 {
913 subprefix_stack[submenu_depth++] = prefix;
914 prefix = entry;
915 i++;
916 }
28be1ada 917 else if (EQ (AREF (vector, i), Qlambda))
279a1d4b
CY
918 {
919 prefix = subprefix_stack[--submenu_depth];
920 i++;
921 }
28be1ada 922 else if (EQ (AREF (vector, i), Qt))
279a1d4b 923 {
28be1ada 924 prefix = AREF (vector, i + MENU_ITEMS_PANE_PREFIX);
279a1d4b
CY
925 i += MENU_ITEMS_PANE_LENGTH;
926 }
927 else
928 {
28be1ada 929 entry = AREF (vector, i + MENU_ITEMS_ITEM_VALUE);
8ac068ac 930 /* Treat the pointer as an integer. There's no problem
279a1d4b 931 as long as pointers have enough bits to hold small integers. */
d01a7826 932 if ((intptr_t) client_data == i)
279a1d4b
CY
933 {
934 int j;
935 struct input_event buf;
936 Lisp_Object frame;
937 EVENT_INIT (buf);
938
939 XSETFRAME (frame, f);
940 buf.kind = MENU_BAR_EVENT;
941 buf.frame_or_window = frame;
942 buf.arg = frame;
943 kbd_buffer_store_event (&buf);
944
945 for (j = 0; j < submenu_depth; j++)
946 if (!NILP (subprefix_stack[j]))
947 {
948 buf.kind = MENU_BAR_EVENT;
949 buf.frame_or_window = frame;
950 buf.arg = subprefix_stack[j];
951 kbd_buffer_store_event (&buf);
952 }
953
954 if (!NILP (prefix))
955 {
956 buf.kind = MENU_BAR_EVENT;
957 buf.frame_or_window = frame;
958 buf.arg = prefix;
959 kbd_buffer_store_event (&buf);
960 }
961
962 buf.kind = MENU_BAR_EVENT;
963 buf.frame_or_window = frame;
964 buf.arg = entry;
965 kbd_buffer_store_event (&buf);
966
967 return;
968 }
969 i += MENU_ITEMS_ITEM_LENGTH;
970 }
971 }
972}
973
edfda783
AR
974#endif /* USE_X_TOOLKIT || USE_GTK || HAVE_NS || HAVE_NTGUI */
975
976#ifdef HAVE_NS
977/* As above, but return the menu selection instead of storing in kb buffer.
7cded46f 978 If KEYMAPS, return full prefixes to selection. */
edfda783 979Lisp_Object
a10c8269 980find_and_return_menu_selection (struct frame *f, bool keymaps, void *client_data)
edfda783
AR
981{
982 Lisp_Object prefix, entry;
983 int i;
984 Lisp_Object *subprefix_stack;
985 int submenu_depth = 0;
986
987 prefix = entry = Qnil;
988 i = 0;
663e2b3f 989 subprefix_stack = alloca (menu_items_used * word_size);
edfda783
AR
990
991 while (i < menu_items_used)
992 {
28be1ada 993 if (EQ (AREF (menu_items, i), Qnil))
edfda783
AR
994 {
995 subprefix_stack[submenu_depth++] = prefix;
996 prefix = entry;
997 i++;
998 }
28be1ada 999 else if (EQ (AREF (menu_items, i), Qlambda))
edfda783
AR
1000 {
1001 prefix = subprefix_stack[--submenu_depth];
1002 i++;
1003 }
28be1ada 1004 else if (EQ (AREF (menu_items, i), Qt))
edfda783
AR
1005 {
1006 prefix
28be1ada 1007 = AREF (menu_items, i + MENU_ITEMS_PANE_PREFIX);
edfda783
AR
1008 i += MENU_ITEMS_PANE_LENGTH;
1009 }
1010 /* Ignore a nil in the item list.
1011 It's meaningful only for dialog boxes. */
28be1ada 1012 else if (EQ (AREF (menu_items, i), Qquote))
edfda783
AR
1013 i += 1;
1014 else
1015 {
1016 entry
28be1ada 1017 = AREF (menu_items, i + MENU_ITEMS_ITEM_VALUE);
4939150c 1018 if (aref_addr (menu_items, i) == client_data)
edfda783 1019 {
7cded46f 1020 if (keymaps)
edfda783
AR
1021 {
1022 int j;
1023
6c6f1994 1024 entry = list1 (entry);
edfda783
AR
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);
1030 }
1031 return entry;
1032 }
1033 i += MENU_ITEMS_ITEM_LENGTH;
1034 }
1035 }
facfbbbd 1036 return Qnil;
edfda783 1037}
31c2d412 1038#endif /* HAVE_NS */
279a1d4b 1039
3b158d11
PE
1040ptrdiff_t
1041menu_item_width (const unsigned char *str)
401cf890 1042{
3b158d11
PE
1043 ptrdiff_t len;
1044 const unsigned char *p;
401cf890
EZ
1045
1046 for (len = 0, p = str; *p; )
1047 {
1048 int ch_len;
1049 int ch = STRING_CHAR_AND_LENGTH (p, ch_len);
1050
1051 len += CHAR_WIDTH (ch);
1052 p += ch_len;
1053 }
1054 return len;
1055}
1056
63dfbda7
EZ
1057DEFUN ("menu-bar-menu-at-x-y", Fmenu_bar_menu_at_x_y, Smenu_bar_menu_at_x_y,
1058 2, 3, 0,
1059 doc: /* Return the menu-bar menu on FRAME at pixel coordinates X, Y.
1060X and Y are frame-relative pixel coordinates, assumed to define
1061a location within the menu bar.
1062If FRAME is nil or omitted, it defaults to the selected frame.
1063
1064Value is the symbol of the menu at X/Y, or nil if the specified
1065coordinates are not within the FRAME's menu bar. The symbol can
1066be used to look up the menu like this:
1067
493a1978
EZ
1068 (lookup-key MAP [menu-bar SYMBOL])
1069
1070where MAP is either the current global map or the current local map,
1071since menu-bar items come from both.
63dfbda7
EZ
1072
1073This function can return non-nil only on a text-terminal frame
1074or on an X frame that doesn't use any GUI toolkit. Otherwise,
1075Emacs does not manage the menu bar and cannot convert coordinates
1076into menu items. */)
1077 (Lisp_Object x, Lisp_Object y, Lisp_Object frame)
1078{
1079 int row, col;
1080 struct frame *f = decode_any_frame (frame);
1081
1082 if (!FRAME_LIVE_P (f))
1083 return Qnil;
1084
1085 pixel_to_glyph_coords (f, XINT (x), XINT (y), &col, &row, NULL, 1);
1086 if (0 <= row && row < FRAME_MENU_BAR_LINES (f))
1087 {
1088 Lisp_Object items, item;
1089 int i;
1090
1091 /* Find the menu bar item under `col'. */
1092 item = Qnil;
1093 items = FRAME_MENU_BAR_ITEMS (f);
f9e5cb0a
EZ
1094 /* This loop assumes a single menu-bar line, and will fail to
1095 find an item if it is not in the first line. Note that
1096 make_lispy_event in keyboard.c makes the same assumption. */
63dfbda7
EZ
1097 for (i = 0; i < ASIZE (items); i += 4)
1098 {
1099 Lisp_Object pos, str;
1100
1101 str = AREF (items, i + 1);
1102 pos = AREF (items, i + 3);
1103 if (NILP (str))
1104 return item;
401cf890
EZ
1105 if (XINT (pos) <= col
1106 /* We use <= so the blank between 2 items on a TTY is
1107 considered part of the previous item. */
3b158d11 1108 && col <= XINT (pos) + menu_item_width (SDATA (str)))
63dfbda7
EZ
1109 {
1110 item = AREF (items, i);
1111 return item;
1112 }
1113 }
1114 }
1115 return Qnil;
1116}
1117
1118
a7ca3326 1119DEFUN ("x-popup-menu", Fx_popup_menu, Sx_popup_menu, 2, 2, 0,
ef7417fd
SM
1120 doc: /* Pop up a deck-of-cards menu and return user's selection.
1121POSITION is a position specification. This is either a mouse button event
1122or a list ((XOFFSET YOFFSET) WINDOW)
1123where XOFFSET and YOFFSET are positions in pixels from the top left
1124corner of WINDOW. (WINDOW may be a window or a frame object.)
1125This controls the position of the top left of the menu as a whole.
1126If POSITION is t, it means to use the current mouse position.
1127
1128MENU is a specifier for a menu. For the simplest case, MENU is a keymap.
1129The menu items come from key bindings that have a menu string as well as
1130a definition; actually, the "definition" in such a key binding looks like
1131\(STRING . REAL-DEFINITION). To give the menu a title, put a string into
1132the keymap as a top-level element.
1133
1134If REAL-DEFINITION is nil, that puts a nonselectable string in the menu.
1135Otherwise, REAL-DEFINITION should be a valid key binding definition.
1136
1137You can also use a list of keymaps as MENU.
1138 Then each keymap makes a separate pane.
1139
1140When MENU is a keymap or a list of keymaps, the return value is the
1141list of events corresponding to the user's choice. Note that
1142`x-popup-menu' does not actually execute the command bound to that
1143sequence of events.
1144
1145Alternatively, you can specify a menu of multiple panes
1146 with a list of the form (TITLE PANE1 PANE2...),
1147where each pane is a list of form (TITLE ITEM1 ITEM2...).
1148Each ITEM is normally a cons cell (STRING . VALUE);
1149but a string can appear as an item--that makes a nonselectable line
1150in the menu.
1151With this form of menu, the return value is VALUE from the chosen item.
1152
1153If POSITION is nil, don't display the menu at all, just precalculate the
1154cached information about equivalent key sequences.
1155
1156If the user gets rid of the menu without making a valid choice, for
1157instance by clicking the mouse away from a valid choice or by typing
1158keyboard input, then this normally results in a quit and
1159`x-popup-menu' does not return. But if POSITION is a mouse button
1160event (indicating that the user invoked the menu with the mouse) then
1161no quit occurs and `x-popup-menu' returns nil. */)
5842a27b 1162 (Lisp_Object position, Lisp_Object menu)
ef7417fd 1163{
4a48e94d 1164 Lisp_Object keymap, tem, tem2;
ef7417fd
SM
1165 int xpos = 0, ypos = 0;
1166 Lisp_Object title;
42ca4633 1167 const char *error_name = NULL;
ef7417fd 1168 Lisp_Object selection = Qnil;
a10c8269 1169 struct frame *f = NULL;
ef7417fd 1170 Lisp_Object x, y, window;
7cded46f
PE
1171 bool keymaps = 0;
1172 bool for_click = 0;
4a48e94d 1173 bool kbd_menu_navigation = 0;
d311d28c 1174 ptrdiff_t specpdl_count = SPECPDL_INDEX ();
ef7417fd 1175 struct gcpro gcpro1;
ef7417fd 1176
ef7417fd
SM
1177 if (NILP (position))
1178 /* This is an obsolete call, which wants us to precompute the
1179 keybinding equivalents, but we don't do that any more anyway. */
1180 return Qnil;
1181
ef7417fd 1182 {
7cded46f 1183 bool get_current_pos_p = 0;
73931ad1 1184
ef7417fd
SM
1185 /* Decode the first argument: find the window and the coordinates. */
1186 if (EQ (position, Qt)
1187 || (CONSP (position) && (EQ (XCAR (position), Qmenu_bar)
1188 || EQ (XCAR (position), Qtool_bar))))
1189 {
1190 get_current_pos_p = 1;
1191 }
1192 else
1193 {
1194 tem = Fcar (position);
1195 if (CONSP (tem))
1196 {
1197 window = Fcar (Fcdr (position));
1198 x = XCAR (tem);
1199 y = Fcar (XCDR (tem));
1200 }
1201 else
1202 {
1203 for_click = 1;
1204 tem = Fcar (Fcdr (position)); /* EVENT_START (position) */
1205 window = Fcar (tem); /* POSN_WINDOW (tem) */
4a48e94d
EZ
1206 tem2 = Fcar (Fcdr (tem)); /* POSN_POSN (tem) */
1207 /* The kbd_menu_navigation flag is set when the menu was
1208 invoked by F10, which probably means they have no
1209 mouse. In that case, we let them switch between
1210 top-level menu-bar menus by using C-f/C-b and
1211 horizontal arrow keys, since they cannot click the
1212 mouse to open a different submenu. This flag is only
1213 supported by tty_menu_show. We set it when POSITION
1214 and last_nonmenu_event are different, which means we
1215 constructed POSITION by hand (in popup-menu, see
1216 menu-bar.el) to look like a mouse click on the menu bar
1217 event. */
1218 if (!EQ (POSN_POSN (last_nonmenu_event),
1219 POSN_POSN (position))
1220 && CONSP (tem2) && EQ (Fcar (tem2), Qmenu_bar))
1221 kbd_menu_navigation = 1;
a6d676d9
CY
1222 tem = Fcar (Fcdr (Fcdr (tem))); /* POSN_WINDOW_POSN (tem) */
1223 x = Fcar (tem);
1224 y = Fcdr (tem);
ef7417fd
SM
1225 }
1226
1227 /* If a click happens in an external tool bar or a detached
1228 tool bar, x and y is NIL. In that case, use the current
1229 mouse position. This happens for the help button in the
1230 tool bar. Ideally popup-menu should pass NIL to
1231 this function, but it doesn't. */
1232 if (NILP (x) && NILP (y))
1233 get_current_pos_p = 1;
1234 }
1235
1236 if (get_current_pos_p)
1237 {
1238 /* Use the mouse's current position. */
a10c8269 1239 struct frame *new_f = SELECTED_FRAME ();
ef7417fd
SM
1240#ifdef HAVE_X_WINDOWS
1241 /* Can't use mouse_position_hook for X since it returns
1242 coordinates relative to the window the mouse is in,
1243 we need coordinates relative to the edit widget always. */
1244 if (new_f != 0)
1245 {
1246 int cur_x, cur_y;
1247
1248 mouse_position_for_popup (new_f, &cur_x, &cur_y);
1249 /* cur_x/y may be negative, so use make_number. */
1250 x = make_number (cur_x);
1251 y = make_number (cur_y);
1252 }
1253
1254#else /* not HAVE_X_WINDOWS */
1255 Lisp_Object bar_window;
1256 enum scroll_bar_part part;
08dc5ae6 1257 Time time;
f57e2426
J
1258 void (*mouse_position_hook) (struct frame **, int,
1259 Lisp_Object *,
1260 enum scroll_bar_part *,
1261 Lisp_Object *,
1262 Lisp_Object *,
08dc5ae6 1263 Time *) =
ef7417fd
SM
1264 FRAME_TERMINAL (new_f)->mouse_position_hook;
1265
1266 if (mouse_position_hook)
1267 (*mouse_position_hook) (&new_f, 1, &bar_window,
1268 &part, &x, &y, &time);
1269#endif /* not HAVE_X_WINDOWS */
1270
1271 if (new_f != 0)
1272 XSETFRAME (window, new_f);
1273 else
1274 {
1275 window = selected_window;
1276 XSETFASTINT (x, 0);
1277 XSETFASTINT (y, 0);
1278 }
1279 }
1280
ef7417fd
SM
1281 /* Decode where to put the menu. */
1282
1283 if (FRAMEP (window))
1284 {
1285 f = XFRAME (window);
1286 xpos = 0;
1287 ypos = 0;
1288 }
1289 else if (WINDOWP (window))
1290 {
1291 struct window *win = XWINDOW (window);
1292 CHECK_LIVE_WINDOW (window);
1293 f = XFRAME (WINDOW_FRAME (win));
1294
ef7417fd
SM
1295 xpos = WINDOW_LEFT_EDGE_X (win);
1296 ypos = WINDOW_TOP_EDGE_Y (win);
ef7417fd
SM
1297 }
1298 else
1299 /* ??? Not really clean; should be CHECK_WINDOW_OR_FRAME,
1300 but I don't want to make one now. */
1301 CHECK_WINDOW (window);
1302
af5a5a98
AS
1303 CHECK_RANGED_INTEGER (x,
1304 (xpos < INT_MIN - MOST_NEGATIVE_FIXNUM
d311d28c
PE
1305 ? (EMACS_INT) INT_MIN - xpos
1306 : MOST_NEGATIVE_FIXNUM),
af5a5a98
AS
1307 INT_MAX - xpos);
1308 CHECK_RANGED_INTEGER (y,
1309 (ypos < INT_MIN - MOST_NEGATIVE_FIXNUM
d311d28c
PE
1310 ? (EMACS_INT) INT_MIN - ypos
1311 : MOST_NEGATIVE_FIXNUM),
af5a5a98 1312 INT_MAX - ypos);
ef7417fd
SM
1313 xpos += XINT (x);
1314 ypos += XINT (y);
1315
ef7417fd
SM
1316 XSETFRAME (Vmenu_updating_frame, f);
1317 }
ef7417fd
SM
1318
1319 /* Now parse the lisp menus. */
27e498e6 1320 record_unwind_protect_void (unuse_menu_items);
ef7417fd
SM
1321
1322 title = Qnil;
1323 GCPRO1 (title);
1324
1325 /* Decode the menu items from what was specified. */
1326
1327 keymap = get_keymap (menu, 0, 0);
1328 if (CONSP (keymap))
1329 {
1330 /* We were given a keymap. Extract menu info from the keymap. */
1331 Lisp_Object prompt;
1332
1333 /* Extract the detailed info to make one pane. */
1334 keymap_panes (&menu, 1);
1335
1336 /* Search for a string appearing directly as an element of the keymap.
1337 That string is the title of the menu. */
1338 prompt = Fkeymap_prompt (keymap);
1339 if (!NILP (prompt))
1340 title = prompt;
1341#ifdef HAVE_NS /* Is that needed and NS-specific? --Stef */
1342 else
1343 title = build_string ("Select");
1344#endif
1345
1346 /* Make that be the pane title of the first pane. */
1347 if (!NILP (prompt) && menu_items_n_panes >= 0)
1348 ASET (menu_items, MENU_ITEMS_PANE_NAME, prompt);
1349
1350 keymaps = 1;
1351 }
1352 else if (CONSP (menu) && KEYMAPP (XCAR (menu)))
1353 {
1354 /* We were given a list of keymaps. */
d311d28c
PE
1355 EMACS_INT nmaps = XFASTINT (Flength (menu));
1356 Lisp_Object *maps;
1357 ptrdiff_t i;
1358 USE_SAFE_ALLOCA;
ef7417fd 1359
d311d28c 1360 SAFE_ALLOCA_LISP (maps, nmaps);
ef7417fd
SM
1361 title = Qnil;
1362
1363 /* The first keymap that has a prompt string
1364 supplies the menu title. */
1365 for (tem = menu, i = 0; CONSP (tem); tem = XCDR (tem))
1366 {
1367 Lisp_Object prompt;
1368
1369 maps[i++] = keymap = get_keymap (XCAR (tem), 1, 0);
1370
1371 prompt = Fkeymap_prompt (keymap);
1372 if (NILP (title) && !NILP (prompt))
1373 title = prompt;
1374 }
1375
1376 /* Extract the detailed info to make one pane. */
1377 keymap_panes (maps, nmaps);
1378
1379 /* Make the title be the pane title of the first pane. */
1380 if (!NILP (title) && menu_items_n_panes >= 0)
1381 ASET (menu_items, MENU_ITEMS_PANE_NAME, title);
1382
1383 keymaps = 1;
d311d28c
PE
1384
1385 SAFE_FREE ();
ef7417fd
SM
1386 }
1387 else
1388 {
1389 /* We were given an old-fashioned menu. */
1390 title = Fcar (menu);
1391 CHECK_STRING (title);
1392
1393 list_of_panes (Fcdr (menu));
1394
1395 keymaps = 0;
1396 }
1397
1398 unbind_to (specpdl_count, Qnil);
1399
dc92c039 1400#ifdef HAVE_WINDOW_SYSTEM
ef7417fd 1401 /* Hide a previous tip, if any. */
ffc3882f
EZ
1402 if (!FRAME_TERMCAP_P (f))
1403 Fx_hide_tip ();
dc92c039 1404#endif
ef7417fd
SM
1405
1406#ifdef HAVE_NTGUI /* FIXME: Is it really w32-specific? --Stef */
1407 /* If resources from a previous popup menu still exist, does nothing
1408 until the `menu_free_timer' has freed them (see w32fns.c). This
1409 can occur if you press ESC or click outside a menu without selecting
1410 a menu item.
1411 */
ffc3882f 1412 if (current_popup_menu && FRAME_W32_P (f))
ef7417fd
SM
1413 {
1414 discard_menu_items ();
aad3612f 1415 FRAME_DISPLAY_INFO (f)->grabbed = 0;
ef7417fd
SM
1416 UNGCPRO;
1417 return Qnil;
1418 }
1419#endif
1420
1421#ifdef HAVE_NS /* FIXME: ns-specific, why? --Stef */
27e498e6 1422 record_unwind_protect_void (discard_menu_items);
ef7417fd
SM
1423#endif
1424
1425 /* Display them in a menu. */
ef7417fd
SM
1426
1427 /* FIXME: Use a terminal hook! */
1428#if defined HAVE_NTGUI
b5e9cbb6
EZ
1429 if (FRAME_W32_P (f))
1430 selection = w32_menu_show (f, xpos, ypos, for_click,
1431 keymaps, title, &error_name);
1432 else
1433#endif
1434#if defined HAVE_NS
1435 if (FRAME_NS_P (f))
1436 selection = ns_menu_show (f, xpos, ypos, for_click,
1437 keymaps, title, &error_name);
1438 else
1439#endif
1440#if (defined (HAVE_X_WINDOWS) || defined (MSDOS))
b5e9cbb6
EZ
1441 if (FRAME_X_P (f) || FRAME_MSDOS_P (f))
1442 selection = xmenu_show (f, xpos, ypos, for_click,
6bbe6da8 1443 keymaps, title, &error_name);
b5e9cbb6 1444 else
ef7417fd 1445#endif
bf6b4923 1446#ifndef MSDOS
b5e9cbb6 1447 if (FRAME_TERMCAP_P (f))
20adfbd8
EZ
1448 {
1449 ptrdiff_t count1 = SPECPDL_INDEX ();
1450
1451 /* Avoid crashes if, e.g., another client will connect while we
1452 are in a menu. */
1453 temporarily_switch_to_single_kboard (f);
1454 selection = tty_menu_show (f, xpos, ypos, for_click, keymaps, title,
1455 kbd_menu_navigation, &error_name);
1456 unbind_to (count1, Qnil);
1457 }
bf6b4923 1458#endif
ef7417fd 1459
ef7417fd
SM
1460#ifdef HAVE_NS
1461 unbind_to (specpdl_count, Qnil);
1462#else
1463 discard_menu_items ();
1464#endif
1465
1466#ifdef HAVE_NTGUI /* FIXME: Is it really w32-specific? --Stef */
ffc3882f 1467 if (FRAME_W32_P (f))
b87c4ff2 1468 FRAME_DISPLAY_INFO (f)->grabbed = 0;
ef7417fd
SM
1469#endif
1470
ef7417fd
SM
1471 UNGCPRO;
1472
42eea0d0 1473 if (error_name) error ("%s", error_name);
ef7417fd
SM
1474 return selection;
1475}
1476
0afa0aab
EZ
1477DEFUN ("x-popup-dialog", Fx_popup_dialog, Sx_popup_dialog, 2, 3, 0,
1478 doc: /* Pop up a dialog box and return user's selection.
1479POSITION specifies which frame to use.
1480This is normally a mouse button event or a window or frame.
1481If POSITION is t, it means to use the frame the mouse is on.
1482The dialog box appears in the middle of the specified frame.
1483
1484CONTENTS specifies the alternatives to display in the dialog box.
1485It is a list of the form (DIALOG ITEM1 ITEM2...).
1486Each ITEM is a cons cell (STRING . VALUE).
1487The return value is VALUE from the chosen item.
1488
1489An ITEM may also be just a string--that makes a nonselectable item.
1490An ITEM may also be nil--that means to put all preceding items
1491on the left of the dialog box and all following items on the right.
1492\(By default, approximately half appear on each side.)
1493
1494If HEADER is non-nil, the frame title for the box is "Information",
1495otherwise it is "Question".
1496
1497If the user gets rid of the dialog box without making a valid choice,
1498for instance using the window manager, then this produces a quit and
1499`x-popup-dialog' does not return. */)
1500 (Lisp_Object position, Lisp_Object contents, Lisp_Object header)
1501{
1502 struct frame *f = NULL;
1503 Lisp_Object window;
1504
1505 /* Decode the first argument: find the window or frame to use. */
1506 if (EQ (position, Qt)
1507 || (CONSP (position) && (EQ (XCAR (position), Qmenu_bar)
1508 || EQ (XCAR (position), Qtool_bar))))
1509 {
1510#if 0 /* Using the frame the mouse is on may not be right. */
1511 /* Use the mouse's current position. */
1512 struct frame *new_f = SELECTED_FRAME ();
1513 Lisp_Object bar_window;
1514 enum scroll_bar_part part;
1515 Time time;
1516 Lisp_Object x, y;
1517
1518 (*mouse_position_hook) (&new_f, 1, &bar_window, &part, &x, &y, &time);
1519
1520 if (new_f != 0)
1521 XSETFRAME (window, new_f);
1522 else
1523 window = selected_window;
1524#endif
1525 window = selected_window;
1526 }
1527 else if (CONSP (position))
1528 {
1529 Lisp_Object tem = XCAR (position);
1530 if (CONSP (tem))
1531 window = Fcar (XCDR (position));
1532 else
1533 {
1534 tem = Fcar (XCDR (position)); /* EVENT_START (position) */
1535 window = Fcar (tem); /* POSN_WINDOW (tem) */
1536 }
1537 }
1538 else if (WINDOWP (position) || FRAMEP (position))
1539 window = position;
1540 else
1541 window = Qnil;
1542
1543 /* Decode where to put the menu. */
1544
1545 if (FRAMEP (window))
1546 f = XFRAME (window);
1547 else if (WINDOWP (window))
1548 {
1549 CHECK_LIVE_WINDOW (window);
1550 f = XFRAME (WINDOW_FRAME (XWINDOW (window)));
1551 }
1552 else
1553 /* ??? Not really clean; should be CHECK_WINDOW_OR_FRAME,
1554 but I don't want to make one now. */
1555 CHECK_WINDOW (window);
1556
1557 /* Force a redisplay before showing the dialog. If a frame is created
1558 just before showing the dialog, its contents may not have been fully
1559 drawn, as this depends on timing of events from the X server. Redisplay
1560 is not done when a dialog is shown. If redisplay could be done in the
1561 X event loop (i.e. the X event loop does not run in a signal handler)
1562 this would not be needed.
1563
1564 Do this before creating the widget value that points to Lisp
1565 string contents, because Fredisplay may GC and relocate them. */
1566 Fredisplay (Qt);
8e5917ec
PE
1567
1568#if defined USE_X_TOOLKIT || defined USE_GTK
0afa0aab
EZ
1569 if (FRAME_WINDOW_P (f))
1570 return xw_popup_dialog (f, header, contents);
0afa0aab 1571#endif
8e5917ec 1572#ifdef HAVE_NTGUI
0afa0aab 1573 if (FRAME_W32_P (f))
e139a33c
EZ
1574 {
1575 Lisp_Object selection = w32_popup_dialog (f, header, contents);
1576
1577 if (!EQ (selection, Qunsupported__w32_dialog))
1578 return selection;
e139a33c 1579 }
0afa0aab
EZ
1580#endif
1581#ifdef HAVE_NS
1582 if (FRAME_NS_P (f))
1583 return ns_popup_dialog (position, header, contents);
0afa0aab
EZ
1584#endif
1585 /* Display a menu with these alternatives
1586 in the middle of frame F. */
1587 {
bdaed46f
EZ
1588 Lisp_Object x, y, frame, newpos, prompt;
1589 int x_coord, y_coord;
0afa0aab 1590
bdaed46f 1591 prompt = Fcar (contents);
0afa0aab
EZ
1592 if (FRAME_WINDOW_P (f))
1593 {
bdaed46f
EZ
1594 x_coord = FRAME_PIXEL_WIDTH (f);
1595 y_coord = FRAME_PIXEL_HEIGHT (f);
0afa0aab
EZ
1596 }
1597 else
1598 {
bdaed46f
EZ
1599 x_coord = FRAME_COLS (f);
1600 /* Center the title at frame middle. (TTY menus have their
1601 upper-left corner at the given position.) */
1602 if (STRINGP (prompt))
1603 x_coord -= SCHARS (prompt);
1604 y_coord = FRAME_LINES (f);
0afa0aab
EZ
1605 }
1606 XSETFRAME (frame, f);
bdaed46f
EZ
1607 XSETINT (x, x_coord / 2);
1608 XSETINT (y, y_coord / 2);
0afa0aab
EZ
1609 newpos = list2 (list2 (x, y), frame);
1610
bdaed46f 1611 return Fx_popup_menu (newpos, list2 (prompt, contents));
0afa0aab
EZ
1612 }
1613}
1614
279a1d4b 1615void
971de7fb 1616syms_of_menu (void)
279a1d4b
CY
1617{
1618 staticpro (&menu_items);
1619 menu_items = Qnil;
1620 menu_items_inuse = Qnil;
ef7417fd
SM
1621
1622 defsubr (&Sx_popup_menu);
0afa0aab 1623 defsubr (&Sx_popup_dialog);
63dfbda7 1624 defsubr (&Smenu_bar_menu_at_x_y);
279a1d4b 1625}