(nsfont_draw): Remove code for stippling, which actually
[emacs.git] / src / menu.c
blob940986fd536cdc075976c667b949fbacbc4ef23e
1 /* Platform-independent code for terminal communications.
2 Copyright (C) 1986, 1988, 1993, 1994, 1996, 1999, 2000, 2001, 2002, 2003,
3 2004, 2005, 2006, 2007, 2008, 2009 Free Software Foundation, Inc.
5 This file is part of GNU Emacs.
7 GNU Emacs is free software: you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation, either version 3 of the License, or
10 (at your option) any later version.
12 GNU Emacs is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
20 #include <config.h>
21 #include <stdio.h>
23 #include "lisp.h"
24 #include "keyboard.h"
25 #include "keymap.h"
26 #include "frame.h"
27 #include "termhooks.h"
28 #include "blockinput.h"
29 #include "dispextern.h"
31 #ifdef USE_X_TOOLKIT
32 #include "../lwlib/lwlib.h"
33 #endif
35 #ifdef HAVE_X_WINDOWS
36 #include "xterm.h"
37 #endif
39 #ifdef HAVE_NS
40 #include "nsterm.h"
41 #endif
43 #ifdef USE_GTK
44 #include "gtkutil.h"
45 #endif
47 #ifdef HAVE_NTGUI
48 #include "w32term.h"
50 extern AppendMenuW_Proc unicode_append_menu;
52 #endif /* HAVE_NTGUI */
54 #include "menu.h"
56 /* Define HAVE_BOXES if menus can handle radio and toggle buttons. */
57 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (HAVE_NTGUI)
58 #define HAVE_BOXES 1
59 #endif
61 extern Lisp_Object QCtoggle, QCradio;
63 Lisp_Object menu_items;
65 /* If non-nil, means that the global vars defined here are already in use.
66 Used to detect cases where we try to re-enter this non-reentrant code. */
67 Lisp_Object menu_items_inuse;
69 /* Number of slots currently allocated in menu_items. */
70 int menu_items_allocated;
72 /* This is the index in menu_items of the first empty slot. */
73 int menu_items_used;
75 /* The number of panes currently recorded in menu_items,
76 excluding those within submenus. */
77 int menu_items_n_panes;
79 /* Current depth within submenus. */
80 static int menu_items_submenu_depth;
82 void
83 init_menu_items ()
85 if (!NILP (menu_items_inuse))
86 error ("Trying to use a menu from within a menu-entry");
88 if (NILP (menu_items))
90 menu_items_allocated = 60;
91 menu_items = Fmake_vector (make_number (menu_items_allocated), Qnil);
94 menu_items_inuse = Qt;
95 menu_items_used = 0;
96 menu_items_n_panes = 0;
97 menu_items_submenu_depth = 0;
100 /* Call at the end of generating the data in menu_items. */
102 void
103 finish_menu_items ()
107 Lisp_Object
108 unuse_menu_items (dummy)
109 Lisp_Object dummy;
111 return menu_items_inuse = Qnil;
114 /* Call when finished using the data for the current menu
115 in menu_items. */
117 void
118 discard_menu_items ()
120 /* Free the structure if it is especially large.
121 Otherwise, hold on to it, to save time. */
122 if (menu_items_allocated > 200)
124 menu_items = Qnil;
125 menu_items_allocated = 0;
127 xassert (NILP (menu_items_inuse));
130 /* This undoes save_menu_items, and it is called by the specpdl unwind
131 mechanism. */
133 static Lisp_Object
134 restore_menu_items (saved)
135 Lisp_Object saved;
137 menu_items = XCAR (saved);
138 menu_items_inuse = (! NILP (menu_items) ? Qt : Qnil);
139 menu_items_allocated = (VECTORP (menu_items) ? ASIZE (menu_items) : 0);
140 saved = XCDR (saved);
141 menu_items_used = XINT (XCAR (saved));
142 saved = XCDR (saved);
143 menu_items_n_panes = XINT (XCAR (saved));
144 saved = XCDR (saved);
145 menu_items_submenu_depth = XINT (XCAR (saved));
146 return Qnil;
149 /* Push the whole state of menu_items processing onto the specpdl.
150 It will be restored when the specpdl is unwound. */
152 void
153 save_menu_items ()
155 Lisp_Object saved = list4 (!NILP (menu_items_inuse) ? menu_items : Qnil,
156 make_number (menu_items_used),
157 make_number (menu_items_n_panes),
158 make_number (menu_items_submenu_depth));
159 record_unwind_protect (restore_menu_items, saved);
160 menu_items_inuse = Qnil;
161 menu_items = Qnil;
165 /* Make the menu_items vector twice as large. */
167 static void
168 grow_menu_items ()
170 menu_items_allocated *= 2;
171 menu_items = larger_vector (menu_items, menu_items_allocated, Qnil);
174 /* Begin a submenu. */
176 static void
177 push_submenu_start ()
179 if (menu_items_used + 1 > menu_items_allocated)
180 grow_menu_items ();
182 XVECTOR (menu_items)->contents[menu_items_used++] = Qnil;
183 menu_items_submenu_depth++;
186 /* End a submenu. */
188 static void
189 push_submenu_end ()
191 if (menu_items_used + 1 > menu_items_allocated)
192 grow_menu_items ();
194 XVECTOR (menu_items)->contents[menu_items_used++] = Qlambda;
195 menu_items_submenu_depth--;
198 /* Indicate boundary between left and right. */
200 static void
201 push_left_right_boundary ()
203 if (menu_items_used + 1 > menu_items_allocated)
204 grow_menu_items ();
206 XVECTOR (menu_items)->contents[menu_items_used++] = Qquote;
209 /* Start a new menu pane in menu_items.
210 NAME is the pane name. PREFIX_VEC is a prefix key for this pane. */
212 static void
213 push_menu_pane (name, prefix_vec)
214 Lisp_Object name, prefix_vec;
216 if (menu_items_used + MENU_ITEMS_PANE_LENGTH > menu_items_allocated)
217 grow_menu_items ();
219 if (menu_items_submenu_depth == 0)
220 menu_items_n_panes++;
221 XVECTOR (menu_items)->contents[menu_items_used++] = Qt;
222 XVECTOR (menu_items)->contents[menu_items_used++] = name;
223 XVECTOR (menu_items)->contents[menu_items_used++] = prefix_vec;
226 /* Push one menu item into the current pane. NAME is the string to
227 display. ENABLE if non-nil means this item can be selected. KEY
228 is the key generated by choosing this item, or nil if this item
229 doesn't really have a definition. DEF is the definition of this
230 item. EQUIV is the textual description of the keyboard equivalent
231 for this item (or nil if none). TYPE is the type of this menu
232 item, one of nil, `toggle' or `radio'. */
234 static void
235 push_menu_item (name, enable, key, def, equiv, type, selected, help)
236 Lisp_Object name, enable, key, def, equiv, type, selected, help;
238 if (menu_items_used + MENU_ITEMS_ITEM_LENGTH > menu_items_allocated)
239 grow_menu_items ();
241 XVECTOR (menu_items)->contents[menu_items_used++] = name;
242 XVECTOR (menu_items)->contents[menu_items_used++] = enable;
243 XVECTOR (menu_items)->contents[menu_items_used++] = key;
244 XVECTOR (menu_items)->contents[menu_items_used++] = equiv;
245 XVECTOR (menu_items)->contents[menu_items_used++] = def;
246 XVECTOR (menu_items)->contents[menu_items_used++] = type;
247 XVECTOR (menu_items)->contents[menu_items_used++] = selected;
248 XVECTOR (menu_items)->contents[menu_items_used++] = help;
251 /* Args passed between single_keymap_panes and single_menu_item. */
252 struct skp
254 Lisp_Object pending_maps;
255 int maxdepth, notreal;
256 int notbuttons;
259 static void single_menu_item P_ ((Lisp_Object, Lisp_Object, Lisp_Object,
260 void *));
262 /* This is a recursive subroutine of keymap_panes.
263 It handles one keymap, KEYMAP.
264 The other arguments are passed along
265 or point to local variables of the previous function.
266 If NOTREAL is nonzero, only check for equivalent key bindings, don't
267 evaluate expressions in menu items and don't make any menu.
269 If we encounter submenus deeper than MAXDEPTH levels, ignore them. */
271 static void
272 single_keymap_panes (keymap, pane_name, prefix, notreal, maxdepth)
273 Lisp_Object keymap;
274 Lisp_Object pane_name;
275 Lisp_Object prefix;
276 int notreal;
277 int maxdepth;
279 struct skp skp;
280 struct gcpro gcpro1;
282 skp.pending_maps = Qnil;
283 skp.maxdepth = maxdepth;
284 skp.notreal = notreal;
285 skp.notbuttons = 0;
287 if (maxdepth <= 0)
288 return;
290 push_menu_pane (pane_name, prefix);
292 #ifndef HAVE_BOXES
293 /* Remember index for first item in this pane so we can go back and
294 add a prefix when (if) we see the first button. After that, notbuttons
295 is set to 0, to mark that we have seen a button and all non button
296 items need a prefix. */
297 skp.notbuttons = menu_items_used;
298 #endif
300 GCPRO1 (skp.pending_maps);
301 map_keymap_canonical (keymap, single_menu_item, Qnil, &skp);
302 UNGCPRO;
304 /* Process now any submenus which want to be panes at this level. */
305 while (CONSP (skp.pending_maps))
307 Lisp_Object elt, eltcdr, string;
308 elt = XCAR (skp.pending_maps);
309 eltcdr = XCDR (elt);
310 string = XCAR (eltcdr);
311 /* We no longer discard the @ from the beginning of the string here.
312 Instead, we do this in *menu_show. */
313 single_keymap_panes (Fcar (elt), string,
314 XCDR (eltcdr), notreal, maxdepth - 1);
315 skp.pending_maps = XCDR (skp.pending_maps);
319 /* This is a subroutine of single_keymap_panes that handles one
320 keymap entry.
321 KEY is a key in a keymap and ITEM is its binding.
322 SKP->PENDING_MAPS_PTR is a list of keymaps waiting to be made into
323 separate panes.
324 If SKP->NOTREAL is nonzero, only check for equivalent key bindings, don't
325 evaluate expressions in menu items and don't make any menu.
326 If we encounter submenus deeper than SKP->MAXDEPTH levels, ignore them. */
328 static void
329 single_menu_item (key, item, dummy, skp_v)
330 Lisp_Object key, item, dummy;
331 void *skp_v;
333 Lisp_Object map, item_string, enabled;
334 struct gcpro gcpro1, gcpro2;
335 int res;
336 struct skp *skp = skp_v;
338 /* Parse the menu item and leave the result in item_properties. */
339 GCPRO2 (key, item);
340 res = parse_menu_item (item, skp->notreal, 0);
341 UNGCPRO;
342 if (!res)
343 return; /* Not a menu item. */
345 map = XVECTOR (item_properties)->contents[ITEM_PROPERTY_MAP];
347 if (skp->notreal)
349 /* We don't want to make a menu, just traverse the keymaps to
350 precompute equivalent key bindings. */
351 if (!NILP (map))
352 single_keymap_panes (map, Qnil, key, 1, skp->maxdepth - 1);
353 return;
356 enabled = XVECTOR (item_properties)->contents[ITEM_PROPERTY_ENABLE];
357 item_string = XVECTOR (item_properties)->contents[ITEM_PROPERTY_NAME];
359 if (!NILP (map) && SREF (item_string, 0) == '@')
361 if (!NILP (enabled))
362 /* An enabled separate pane. Remember this to handle it later. */
363 skp->pending_maps = Fcons (Fcons (map, Fcons (item_string, key)),
364 skp->pending_maps);
365 return;
368 #if defined(HAVE_X_WINDOWS) || defined(MSDOS)
369 #ifndef HAVE_BOXES
370 /* Simulate radio buttons and toggle boxes by putting a prefix in
371 front of them. */
373 Lisp_Object prefix = Qnil;
374 Lisp_Object type = XVECTOR (item_properties)->contents[ITEM_PROPERTY_TYPE];
375 if (!NILP (type))
377 Lisp_Object selected
378 = XVECTOR (item_properties)->contents[ITEM_PROPERTY_SELECTED];
380 if (skp->notbuttons)
381 /* The first button. Line up previous items in this menu. */
383 int index = skp->notbuttons; /* Index for first item this menu. */
384 int submenu = 0;
385 Lisp_Object tem;
386 while (index < menu_items_used)
389 = XVECTOR (menu_items)->contents[index + MENU_ITEMS_ITEM_NAME];
390 if (NILP (tem))
392 index++;
393 submenu++; /* Skip sub menu. */
395 else if (EQ (tem, Qlambda))
397 index++;
398 submenu--; /* End sub menu. */
400 else if (EQ (tem, Qt))
401 index += 3; /* Skip new pane marker. */
402 else if (EQ (tem, Qquote))
403 index++; /* Skip a left, right divider. */
404 else
406 if (!submenu && SREF (tem, 0) != '\0'
407 && SREF (tem, 0) != '-')
408 XVECTOR (menu_items)->contents[index + MENU_ITEMS_ITEM_NAME]
409 = concat2 (build_string (" "), tem);
410 index += MENU_ITEMS_ITEM_LENGTH;
413 skp->notbuttons = 0;
416 /* Calculate prefix, if any, for this item. */
417 if (EQ (type, QCtoggle))
418 prefix = build_string (NILP (selected) ? "[ ] " : "[X] ");
419 else if (EQ (type, QCradio))
420 prefix = build_string (NILP (selected) ? "( ) " : "(*) ");
422 /* Not a button. If we have earlier buttons, then we need a prefix. */
423 else if (!skp->notbuttons && SREF (item_string, 0) != '\0'
424 && SREF (item_string, 0) != '-')
425 prefix = build_string (" ");
427 if (!NILP (prefix))
428 item_string = concat2 (prefix, item_string);
430 #endif /* not HAVE_BOXES */
432 #if ! defined (USE_X_TOOLKIT) && ! defined (USE_GTK)
433 if (!NILP (map))
434 /* Indicate visually that this is a submenu. */
435 item_string = concat2 (item_string, build_string (" >"));
436 #endif
438 #endif /* HAVE_X_WINDOWS || MSDOS */
440 push_menu_item (item_string, enabled, key,
441 XVECTOR (item_properties)->contents[ITEM_PROPERTY_DEF],
442 XVECTOR (item_properties)->contents[ITEM_PROPERTY_KEYEQ],
443 XVECTOR (item_properties)->contents[ITEM_PROPERTY_TYPE],
444 XVECTOR (item_properties)->contents[ITEM_PROPERTY_SELECTED],
445 XVECTOR (item_properties)->contents[ITEM_PROPERTY_HELP]);
447 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (HAVE_NS) || defined (HAVE_NTGUI)
448 /* Display a submenu using the toolkit. */
449 if (! (NILP (map) || NILP (enabled)))
451 push_submenu_start ();
452 single_keymap_panes (map, Qnil, key, 0, skp->maxdepth - 1);
453 push_submenu_end ();
455 #endif
458 /* Look through KEYMAPS, a vector of keymaps that is NMAPS long,
459 and generate menu panes for them in menu_items.
460 If NOTREAL is nonzero,
461 don't bother really computing whether an item is enabled. */
463 void
464 keymap_panes (keymaps, nmaps, notreal)
465 Lisp_Object *keymaps;
466 int nmaps;
467 int notreal;
469 int mapno;
471 init_menu_items ();
473 /* Loop over the given keymaps, making a pane for each map.
474 But don't make a pane that is empty--ignore that map instead.
475 P is the number of panes we have made so far. */
476 for (mapno = 0; mapno < nmaps; mapno++)
477 single_keymap_panes (keymaps[mapno],
478 Fkeymap_prompt (keymaps[mapno]), Qnil, notreal, 10);
480 finish_menu_items ();
484 /* Push the items in a single pane defined by the alist PANE. */
485 static void
486 list_of_items (pane)
487 Lisp_Object pane;
489 Lisp_Object tail, item, item1;
491 for (tail = pane; CONSP (tail); tail = XCDR (tail))
493 item = XCAR (tail);
494 if (STRINGP (item))
495 push_menu_item (ENCODE_MENU_STRING (item), Qnil, Qnil, Qt,
496 Qnil, Qnil, Qnil, Qnil);
497 else if (CONSP (item))
499 item1 = XCAR (item);
500 CHECK_STRING (item1);
501 push_menu_item (ENCODE_MENU_STRING (item1), Qt, XCDR (item),
502 Qt, Qnil, Qnil, Qnil, Qnil);
504 else
505 push_left_right_boundary ();
510 /* Push all the panes and items of a menu described by the
511 alist-of-alists MENU.
512 This handles old-fashioned calls to x-popup-menu. */
513 void
514 list_of_panes (menu)
515 Lisp_Object menu;
517 Lisp_Object tail;
519 init_menu_items ();
521 for (tail = menu; CONSP (tail); tail = XCDR (tail))
523 Lisp_Object elt, pane_name, pane_data;
524 elt = XCAR (tail);
525 pane_name = Fcar (elt);
526 CHECK_STRING (pane_name);
527 push_menu_pane (ENCODE_MENU_STRING (pane_name), Qnil);
528 pane_data = Fcdr (elt);
529 CHECK_CONS (pane_data);
530 list_of_items (pane_data);
533 finish_menu_items ();
536 /* Set up data in menu_items for a menu bar item
537 whose event type is ITEM_KEY (with string ITEM_NAME)
538 and whose contents come from the list of keymaps MAPS. */
540 parse_single_submenu (item_key, item_name, maps)
541 Lisp_Object item_key, item_name, maps;
543 Lisp_Object length;
544 int len;
545 Lisp_Object *mapvec;
546 int i;
547 int top_level_items = 0;
549 length = Flength (maps);
550 len = XINT (length);
552 /* Convert the list MAPS into a vector MAPVEC. */
553 mapvec = (Lisp_Object *) alloca (len * sizeof (Lisp_Object));
554 for (i = 0; i < len; i++)
556 mapvec[i] = Fcar (maps);
557 maps = Fcdr (maps);
560 /* Loop over the given keymaps, making a pane for each map.
561 But don't make a pane that is empty--ignore that map instead. */
562 for (i = 0; i < len; i++)
564 if (!KEYMAPP (mapvec[i]))
566 /* Here we have a command at top level in the menu bar
567 as opposed to a submenu. */
568 top_level_items = 1;
569 push_menu_pane (Qnil, Qnil);
570 push_menu_item (item_name, Qt, item_key, mapvec[i],
571 Qnil, Qnil, Qnil, Qnil);
573 else
575 Lisp_Object prompt;
576 prompt = Fkeymap_prompt (mapvec[i]);
577 single_keymap_panes (mapvec[i],
578 !NILP (prompt) ? prompt : item_name,
579 item_key, 0, 10);
583 return top_level_items;
587 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (HAVE_NS) || defined (HAVE_NTGUI)
589 /* Allocate a widget_value, blocking input. */
591 widget_value *
592 xmalloc_widget_value ()
594 widget_value *value;
596 BLOCK_INPUT;
597 value = malloc_widget_value ();
598 UNBLOCK_INPUT;
600 return value;
603 /* This recursively calls free_widget_value on the tree of widgets.
604 It must free all data that was malloc'ed for these widget_values.
605 In Emacs, many slots are pointers into the data of Lisp_Strings, and
606 must be left alone. */
608 void
609 free_menubar_widget_value_tree (wv)
610 widget_value *wv;
612 if (! wv) return;
614 wv->name = wv->value = wv->key = (char *) 0xDEADBEEF;
616 if (wv->contents && (wv->contents != (widget_value*)1))
618 free_menubar_widget_value_tree (wv->contents);
619 wv->contents = (widget_value *) 0xDEADBEEF;
621 if (wv->next)
623 free_menubar_widget_value_tree (wv->next);
624 wv->next = (widget_value *) 0xDEADBEEF;
626 BLOCK_INPUT;
627 free_widget_value (wv);
628 UNBLOCK_INPUT;
631 /* Create a tree of widget_value objects
632 representing the panes and items
633 in menu_items starting at index START, up to index END. */
635 widget_value *
636 digest_single_submenu (start, end, top_level_items)
637 int start, end, top_level_items;
639 widget_value *wv, *prev_wv, *save_wv, *first_wv;
640 int i;
641 int submenu_depth = 0;
642 widget_value **submenu_stack;
643 int panes_seen = 0;
645 submenu_stack
646 = (widget_value **) alloca (menu_items_used * sizeof (widget_value *));
647 wv = xmalloc_widget_value ();
648 wv->name = "menu";
649 wv->value = 0;
650 wv->enabled = 1;
651 wv->button_type = BUTTON_TYPE_NONE;
652 wv->help = Qnil;
653 first_wv = wv;
654 save_wv = 0;
655 prev_wv = 0;
657 /* Loop over all panes and items made by the preceding call
658 to parse_single_submenu and construct a tree of widget_value objects.
659 Ignore the panes and items used by previous calls to
660 digest_single_submenu, even though those are also in menu_items. */
661 i = start;
662 while (i < end)
664 if (EQ (XVECTOR (menu_items)->contents[i], Qnil))
666 submenu_stack[submenu_depth++] = save_wv;
667 save_wv = prev_wv;
668 prev_wv = 0;
669 i++;
671 else if (EQ (XVECTOR (menu_items)->contents[i], Qlambda))
673 prev_wv = save_wv;
674 save_wv = submenu_stack[--submenu_depth];
675 i++;
677 else if (EQ (XVECTOR (menu_items)->contents[i], Qt)
678 && submenu_depth != 0)
679 i += MENU_ITEMS_PANE_LENGTH;
680 /* Ignore a nil in the item list.
681 It's meaningful only for dialog boxes. */
682 else if (EQ (XVECTOR (menu_items)->contents[i], Qquote))
683 i += 1;
684 else if (EQ (XVECTOR (menu_items)->contents[i], Qt))
686 /* Create a new pane. */
687 Lisp_Object pane_name, prefix;
688 char *pane_string;
690 panes_seen++;
692 pane_name = XVECTOR (menu_items)->contents[i + MENU_ITEMS_PANE_NAME];
693 prefix = XVECTOR (menu_items)->contents[i + MENU_ITEMS_PANE_PREFIX];
695 #ifdef HAVE_NTGUI
696 if (STRINGP (pane_name))
698 if (unicode_append_menu)
699 /* Encode as UTF-8 for now. */
700 pane_name = ENCODE_UTF_8 (pane_name);
701 else if (STRING_MULTIBYTE (pane_name))
702 pane_name = ENCODE_SYSTEM (pane_name);
704 ASET (menu_items, i + MENU_ITEMS_PANE_NAME, pane_name);
706 #elif !defined (HAVE_MULTILINGUAL_MENU)
707 if (STRINGP (pane_name) && STRING_MULTIBYTE (pane_name))
709 pane_name = ENCODE_MENU_STRING (pane_name);
710 ASET (menu_items, i + MENU_ITEMS_PANE_NAME, pane_name);
712 #endif
714 pane_string = (NILP (pane_name)
715 ? "" : (char *) SDATA (pane_name));
716 /* If there is just one top-level pane, put all its items directly
717 under the top-level menu. */
718 if (menu_items_n_panes == 1)
719 pane_string = "";
721 /* If the pane has a meaningful name,
722 make the pane a top-level menu item
723 with its items as a submenu beneath it. */
724 if (strcmp (pane_string, ""))
726 wv = xmalloc_widget_value ();
727 if (save_wv)
728 save_wv->next = wv;
729 else
730 first_wv->contents = wv;
731 wv->lname = pane_name;
732 /* Set value to 1 so update_submenu_strings can handle '@' */
733 wv->value = (char *)1;
734 wv->enabled = 1;
735 wv->button_type = BUTTON_TYPE_NONE;
736 wv->help = Qnil;
737 save_wv = wv;
739 else
740 save_wv = first_wv;
742 prev_wv = 0;
743 i += MENU_ITEMS_PANE_LENGTH;
745 else
747 /* Create a new item within current pane. */
748 Lisp_Object item_name, enable, descrip, def, type, selected;
749 Lisp_Object help;
751 /* All items should be contained in panes. */
752 if (panes_seen == 0)
753 abort ();
755 item_name = AREF (menu_items, i + MENU_ITEMS_ITEM_NAME);
756 enable = AREF (menu_items, i + MENU_ITEMS_ITEM_ENABLE);
757 descrip = AREF (menu_items, i + MENU_ITEMS_ITEM_EQUIV_KEY);
758 def = AREF (menu_items, i + MENU_ITEMS_ITEM_DEFINITION);
759 type = AREF (menu_items, i + MENU_ITEMS_ITEM_TYPE);
760 selected = AREF (menu_items, i + MENU_ITEMS_ITEM_SELECTED);
761 help = AREF (menu_items, i + MENU_ITEMS_ITEM_HELP);
763 #ifdef HAVE_NTGUI
764 if (STRINGP (item_name))
766 if (unicode_append_menu)
767 item_name = ENCODE_UTF_8 (item_name);
768 else if (STRING_MULTIBYTE (item_name))
769 item_name = ENCODE_SYSTEM (item_name);
771 ASET (menu_items, i + MENU_ITEMS_ITEM_NAME, item_name);
774 if (STRINGP (descrip) && STRING_MULTIBYTE (descrip))
776 descrip = ENCODE_SYSTEM (descrip);
777 ASET (menu_items, i + MENU_ITEMS_ITEM_EQUIV_KEY, descrip);
779 #elif !defined (HAVE_MULTILINGUAL_MENU)
780 if (STRING_MULTIBYTE (item_name))
782 item_name = ENCODE_MENU_STRING (item_name);
783 ASET (menu_items, i + MENU_ITEMS_ITEM_NAME, item_name);
786 if (STRINGP (descrip) && STRING_MULTIBYTE (descrip))
788 descrip = ENCODE_MENU_STRING (descrip);
789 ASET (menu_items, i + MENU_ITEMS_ITEM_EQUIV_KEY, descrip);
791 #endif
793 wv = xmalloc_widget_value ();
794 if (prev_wv)
795 prev_wv->next = wv;
796 else
797 save_wv->contents = wv;
799 wv->lname = item_name;
800 if (!NILP (descrip))
801 wv->lkey = descrip;
802 wv->value = 0;
803 /* The EMACS_INT cast avoids a warning. There's no problem
804 as long as pointers have enough bits to hold small integers. */
805 wv->call_data = (!NILP (def) ? (void *) (EMACS_INT) i : 0);
806 wv->enabled = !NILP (enable);
808 if (NILP (type))
809 wv->button_type = BUTTON_TYPE_NONE;
810 else if (EQ (type, QCradio))
811 wv->button_type = BUTTON_TYPE_RADIO;
812 else if (EQ (type, QCtoggle))
813 wv->button_type = BUTTON_TYPE_TOGGLE;
814 else
815 abort ();
817 wv->selected = !NILP (selected);
818 if (! STRINGP (help))
819 help = Qnil;
821 wv->help = help;
823 prev_wv = wv;
825 i += MENU_ITEMS_ITEM_LENGTH;
829 /* If we have just one "menu item"
830 that was originally a button, return it by itself. */
831 if (top_level_items && first_wv->contents && first_wv->contents->next == 0)
833 wv = first_wv->contents;
834 free_widget_value (first_wv);
835 return wv;
838 return first_wv;
841 /* Walk through the widget_value tree starting at FIRST_WV and update
842 the char * pointers from the corresponding lisp values.
843 We do this after building the whole tree, since GC may happen while the
844 tree is constructed, and small strings are relocated. So we must wait
845 until no GC can happen before storing pointers into lisp values. */
846 void
847 update_submenu_strings (first_wv)
848 widget_value *first_wv;
850 widget_value *wv;
852 for (wv = first_wv; wv; wv = wv->next)
854 if (STRINGP (wv->lname))
856 wv->name = (char *) SDATA (wv->lname);
858 /* Ignore the @ that means "separate pane".
859 This is a kludge, but this isn't worth more time. */
860 if (wv->value == (char *)1)
862 if (wv->name[0] == '@')
863 wv->name++;
864 wv->value = 0;
868 if (STRINGP (wv->lkey))
869 wv->key = (char *) SDATA (wv->lkey);
871 if (wv->contents)
872 update_submenu_strings (wv->contents);
876 /* Find the menu selection and store it in the keyboard buffer.
877 F is the frame the menu is on.
878 MENU_BAR_ITEMS_USED is the length of VECTOR.
879 VECTOR is an array of menu events for the whole menu. */
881 void
882 find_and_call_menu_selection (f, menu_bar_items_used, vector, client_data)
883 FRAME_PTR f;
884 int menu_bar_items_used;
885 Lisp_Object vector;
886 void *client_data;
888 Lisp_Object prefix, entry;
889 Lisp_Object *subprefix_stack;
890 int submenu_depth = 0;
891 int i;
893 entry = Qnil;
894 subprefix_stack = (Lisp_Object *) alloca (menu_bar_items_used * sizeof (Lisp_Object));
895 prefix = Qnil;
896 i = 0;
898 while (i < menu_bar_items_used)
900 if (EQ (XVECTOR (vector)->contents[i], Qnil))
902 subprefix_stack[submenu_depth++] = prefix;
903 prefix = entry;
904 i++;
906 else if (EQ (XVECTOR (vector)->contents[i], Qlambda))
908 prefix = subprefix_stack[--submenu_depth];
909 i++;
911 else if (EQ (XVECTOR (vector)->contents[i], Qt))
913 prefix = XVECTOR (vector)->contents[i + MENU_ITEMS_PANE_PREFIX];
914 i += MENU_ITEMS_PANE_LENGTH;
916 else
918 entry = XVECTOR (vector)->contents[i + MENU_ITEMS_ITEM_VALUE];
919 /* The EMACS_INT cast avoids a warning. There's no problem
920 as long as pointers have enough bits to hold small integers. */
921 if ((int) (EMACS_INT) client_data == i)
923 int j;
924 struct input_event buf;
925 Lisp_Object frame;
926 EVENT_INIT (buf);
928 XSETFRAME (frame, f);
929 buf.kind = MENU_BAR_EVENT;
930 buf.frame_or_window = frame;
931 buf.arg = frame;
932 kbd_buffer_store_event (&buf);
934 for (j = 0; j < submenu_depth; j++)
935 if (!NILP (subprefix_stack[j]))
937 buf.kind = MENU_BAR_EVENT;
938 buf.frame_or_window = frame;
939 buf.arg = subprefix_stack[j];
940 kbd_buffer_store_event (&buf);
943 if (!NILP (prefix))
945 buf.kind = MENU_BAR_EVENT;
946 buf.frame_or_window = frame;
947 buf.arg = prefix;
948 kbd_buffer_store_event (&buf);
951 buf.kind = MENU_BAR_EVENT;
952 buf.frame_or_window = frame;
953 buf.arg = entry;
954 kbd_buffer_store_event (&buf);
956 return;
958 i += MENU_ITEMS_ITEM_LENGTH;
963 #endif /* USE_X_TOOLKIT || USE_GTK || HAVE_NS || HAVE_NTGUI */
965 #ifdef HAVE_NS
966 /* As above, but return the menu selection instead of storing in kb buffer.
967 If keymaps==1, return full prefixes to selection. */
968 Lisp_Object
969 find_and_return_menu_selection (FRAME_PTR f, int keymaps, void *client_data)
971 Lisp_Object prefix, entry;
972 int i;
973 Lisp_Object *subprefix_stack;
974 int submenu_depth = 0;
976 prefix = entry = Qnil;
977 i = 0;
978 subprefix_stack =
979 (Lisp_Object *)alloca(menu_items_used * sizeof (Lisp_Object));
981 while (i < menu_items_used)
983 if (EQ (XVECTOR (menu_items)->contents[i], Qnil))
985 subprefix_stack[submenu_depth++] = prefix;
986 prefix = entry;
987 i++;
989 else if (EQ (XVECTOR (menu_items)->contents[i], Qlambda))
991 prefix = subprefix_stack[--submenu_depth];
992 i++;
994 else if (EQ (XVECTOR (menu_items)->contents[i], Qt))
996 prefix
997 = XVECTOR (menu_items)->contents[i + MENU_ITEMS_PANE_PREFIX];
998 i += MENU_ITEMS_PANE_LENGTH;
1000 /* Ignore a nil in the item list.
1001 It's meaningful only for dialog boxes. */
1002 else if (EQ (XVECTOR (menu_items)->contents[i], Qquote))
1003 i += 1;
1004 else
1006 entry
1007 = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_VALUE];
1008 if ((int) (EMACS_INT)client_data == (int)(&XVECTOR (menu_items)->contents[i]))
1010 if (keymaps != 0)
1012 int j;
1014 entry = Fcons (entry, Qnil);
1015 if (!NILP (prefix))
1016 entry = Fcons (prefix, entry);
1017 for (j = submenu_depth - 1; j >= 0; j--)
1018 if (!NILP (subprefix_stack[j]))
1019 entry = Fcons (subprefix_stack[j], entry);
1021 return entry;
1023 i += MENU_ITEMS_ITEM_LENGTH;
1026 return Qnil;
1028 #endif
1030 void
1031 syms_of_menu ()
1033 staticpro (&menu_items);
1034 menu_items = Qnil;
1035 menu_items_inuse = Qnil;
1038 /* arch-tag: 78bbc7cf-8025-4156-aa8a-6c7fd99bf51d
1039 (do not change this comment) */