(mh-alias-read-address-map): Define within defvar.
[emacs.git] / src / menu.c
blob2cf94924ffdbf5ab1129d2c9045563babf303c31
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 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 USE_GTK
40 #include "gtkutil.h"
41 #endif
43 #ifdef HAVE_NTGUI
44 /* Definitions copied from lwlib.h */
45 typedef void * XtPointer;
46 typedef char Boolean;
47 enum button_type
49 BUTTON_TYPE_NONE,
50 BUTTON_TYPE_TOGGLE,
51 BUTTON_TYPE_RADIO
54 /* This structure is based on the one in ../lwlib/lwlib.h */
55 typedef struct _widget_value
57 Lisp_Object lname;
58 char* name;
59 char* value;
60 Lisp_Object lkey;
61 char* key;
62 Lisp_Object help;
63 Boolean enabled;
64 Boolean selected;
65 enum button_type button_type;
66 Boolean title;
67 struct _widget_value* contents;
68 XtPointer call_data;
69 struct _widget_value* next;
70 } widget_value;
72 /* Local memory management */
73 #define local_heap (GetProcessHeap ())
74 #define local_alloc(n) (HeapAlloc (local_heap, HEAP_ZERO_MEMORY, (n)))
75 #define local_free(p) (HeapFree (local_heap, 0, ((LPVOID) (p))))
77 #define malloc_widget_value() ((widget_value *) local_alloc (sizeof (widget_value)))
78 #define free_widget_value(wv) (local_free ((wv)))
80 extern AppendMenuW_Proc unicode_append_menu;
82 #endif /* HAVE_NTGUI */
85 /* Define HAVE_BOXES if menus can handle radio and toggle buttons. */
86 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (HAVE_NTGUI)
87 #define HAVE_BOXES 1
88 #endif
90 extern Lisp_Object QCtoggle, QCradio;
92 Lisp_Object menu_items;
94 /* If non-nil, means that the global vars defined here are already in use.
95 Used to detect cases where we try to re-enter this non-reentrant code. */
96 Lisp_Object menu_items_inuse;
98 /* Number of slots currently allocated in menu_items. */
99 int menu_items_allocated;
101 /* This is the index in menu_items of the first empty slot. */
102 int menu_items_used;
104 /* The number of panes currently recorded in menu_items,
105 excluding those within submenus. */
106 int menu_items_n_panes;
108 /* Current depth within submenus. */
109 static int menu_items_submenu_depth;
111 void
112 init_menu_items ()
114 if (!NILP (menu_items_inuse))
115 error ("Trying to use a menu from within a menu-entry");
117 if (NILP (menu_items))
119 menu_items_allocated = 60;
120 menu_items = Fmake_vector (make_number (menu_items_allocated), Qnil);
123 menu_items_inuse = Qt;
124 menu_items_used = 0;
125 menu_items_n_panes = 0;
126 menu_items_submenu_depth = 0;
129 /* Call at the end of generating the data in menu_items. */
131 void
132 finish_menu_items ()
136 Lisp_Object
137 unuse_menu_items (dummy)
138 Lisp_Object dummy;
140 return menu_items_inuse = Qnil;
143 /* Call when finished using the data for the current menu
144 in menu_items. */
146 void
147 discard_menu_items ()
149 /* Free the structure if it is especially large.
150 Otherwise, hold on to it, to save time. */
151 if (menu_items_allocated > 200)
153 menu_items = Qnil;
154 menu_items_allocated = 0;
156 xassert (NILP (menu_items_inuse));
159 /* This undoes save_menu_items, and it is called by the specpdl unwind
160 mechanism. */
162 static Lisp_Object
163 restore_menu_items (saved)
164 Lisp_Object saved;
166 menu_items = XCAR (saved);
167 menu_items_inuse = (! NILP (menu_items) ? Qt : Qnil);
168 menu_items_allocated = (VECTORP (menu_items) ? ASIZE (menu_items) : 0);
169 saved = XCDR (saved);
170 menu_items_used = XINT (XCAR (saved));
171 saved = XCDR (saved);
172 menu_items_n_panes = XINT (XCAR (saved));
173 saved = XCDR (saved);
174 menu_items_submenu_depth = XINT (XCAR (saved));
175 return Qnil;
178 /* Push the whole state of menu_items processing onto the specpdl.
179 It will be restored when the specpdl is unwound. */
181 void
182 save_menu_items ()
184 Lisp_Object saved = list4 (!NILP (menu_items_inuse) ? menu_items : Qnil,
185 make_number (menu_items_used),
186 make_number (menu_items_n_panes),
187 make_number (menu_items_submenu_depth));
188 record_unwind_protect (restore_menu_items, saved);
189 menu_items_inuse = Qnil;
190 menu_items = Qnil;
194 /* Make the menu_items vector twice as large. */
196 static void
197 grow_menu_items ()
199 menu_items_allocated *= 2;
200 menu_items = larger_vector (menu_items, menu_items_allocated, Qnil);
203 /* Begin a submenu. */
205 static void
206 push_submenu_start ()
208 if (menu_items_used + 1 > menu_items_allocated)
209 grow_menu_items ();
211 XVECTOR (menu_items)->contents[menu_items_used++] = Qnil;
212 menu_items_submenu_depth++;
215 /* End a submenu. */
217 static void
218 push_submenu_end ()
220 if (menu_items_used + 1 > menu_items_allocated)
221 grow_menu_items ();
223 XVECTOR (menu_items)->contents[menu_items_used++] = Qlambda;
224 menu_items_submenu_depth--;
227 /* Indicate boundary between left and right. */
229 static void
230 push_left_right_boundary ()
232 if (menu_items_used + 1 > menu_items_allocated)
233 grow_menu_items ();
235 XVECTOR (menu_items)->contents[menu_items_used++] = Qquote;
238 /* Start a new menu pane in menu_items.
239 NAME is the pane name. PREFIX_VEC is a prefix key for this pane. */
241 static void
242 push_menu_pane (name, prefix_vec)
243 Lisp_Object name, prefix_vec;
245 if (menu_items_used + MENU_ITEMS_PANE_LENGTH > menu_items_allocated)
246 grow_menu_items ();
248 if (menu_items_submenu_depth == 0)
249 menu_items_n_panes++;
250 XVECTOR (menu_items)->contents[menu_items_used++] = Qt;
251 XVECTOR (menu_items)->contents[menu_items_used++] = name;
252 XVECTOR (menu_items)->contents[menu_items_used++] = prefix_vec;
255 /* Push one menu item into the current pane. NAME is the string to
256 display. ENABLE if non-nil means this item can be selected. KEY
257 is the key generated by choosing this item, or nil if this item
258 doesn't really have a definition. DEF is the definition of this
259 item. EQUIV is the textual description of the keyboard equivalent
260 for this item (or nil if none). TYPE is the type of this menu
261 item, one of nil, `toggle' or `radio'. */
263 static void
264 push_menu_item (name, enable, key, def, equiv, type, selected, help)
265 Lisp_Object name, enable, key, def, equiv, type, selected, help;
267 if (menu_items_used + MENU_ITEMS_ITEM_LENGTH > menu_items_allocated)
268 grow_menu_items ();
270 XVECTOR (menu_items)->contents[menu_items_used++] = name;
271 XVECTOR (menu_items)->contents[menu_items_used++] = enable;
272 XVECTOR (menu_items)->contents[menu_items_used++] = key;
273 XVECTOR (menu_items)->contents[menu_items_used++] = equiv;
274 XVECTOR (menu_items)->contents[menu_items_used++] = def;
275 XVECTOR (menu_items)->contents[menu_items_used++] = type;
276 XVECTOR (menu_items)->contents[menu_items_used++] = selected;
277 XVECTOR (menu_items)->contents[menu_items_used++] = help;
280 /* Args passed between single_keymap_panes and single_menu_item. */
281 struct skp
283 Lisp_Object pending_maps;
284 int maxdepth, notreal;
285 int notbuttons;
288 static void single_menu_item P_ ((Lisp_Object, Lisp_Object, Lisp_Object,
289 void *));
291 /* This is a recursive subroutine of keymap_panes.
292 It handles one keymap, KEYMAP.
293 The other arguments are passed along
294 or point to local variables of the previous function.
295 If NOTREAL is nonzero, only check for equivalent key bindings, don't
296 evaluate expressions in menu items and don't make any menu.
298 If we encounter submenus deeper than MAXDEPTH levels, ignore them. */
300 static void
301 single_keymap_panes (keymap, pane_name, prefix, notreal, maxdepth)
302 Lisp_Object keymap;
303 Lisp_Object pane_name;
304 Lisp_Object prefix;
305 int notreal;
306 int maxdepth;
308 struct skp skp;
309 struct gcpro gcpro1;
311 skp.pending_maps = Qnil;
312 skp.maxdepth = maxdepth;
313 skp.notreal = notreal;
314 skp.notbuttons = 0;
316 if (maxdepth <= 0)
317 return;
319 push_menu_pane (pane_name, prefix);
321 #ifndef HAVE_BOXES
322 /* Remember index for first item in this pane so we can go back and
323 add a prefix when (if) we see the first button. After that, notbuttons
324 is set to 0, to mark that we have seen a button and all non button
325 items need a prefix. */
326 skp.notbuttons = menu_items_used;
327 #endif
329 GCPRO1 (skp.pending_maps);
330 map_keymap_canonical (keymap, single_menu_item, Qnil, &skp);
331 UNGCPRO;
333 /* Process now any submenus which want to be panes at this level. */
334 while (CONSP (skp.pending_maps))
336 Lisp_Object elt, eltcdr, string;
337 elt = XCAR (skp.pending_maps);
338 eltcdr = XCDR (elt);
339 string = XCAR (eltcdr);
340 /* We no longer discard the @ from the beginning of the string here.
341 Instead, we do this in *menu_show. */
342 single_keymap_panes (Fcar (elt), string,
343 XCDR (eltcdr), notreal, maxdepth - 1);
344 skp.pending_maps = XCDR (skp.pending_maps);
348 /* This is a subroutine of single_keymap_panes that handles one
349 keymap entry.
350 KEY is a key in a keymap and ITEM is its binding.
351 SKP->PENDING_MAPS_PTR is a list of keymaps waiting to be made into
352 separate panes.
353 If SKP->NOTREAL is nonzero, only check for equivalent key bindings, don't
354 evaluate expressions in menu items and don't make any menu.
355 If we encounter submenus deeper than SKP->MAXDEPTH levels, ignore them. */
357 static void
358 single_menu_item (key, item, dummy, skp_v)
359 Lisp_Object key, item, dummy;
360 void *skp_v;
362 Lisp_Object map, item_string, enabled;
363 struct gcpro gcpro1, gcpro2;
364 int res;
365 struct skp *skp = skp_v;
367 /* Parse the menu item and leave the result in item_properties. */
368 GCPRO2 (key, item);
369 res = parse_menu_item (item, skp->notreal, 0);
370 UNGCPRO;
371 if (!res)
372 return; /* Not a menu item. */
374 map = XVECTOR (item_properties)->contents[ITEM_PROPERTY_MAP];
376 if (skp->notreal)
378 /* We don't want to make a menu, just traverse the keymaps to
379 precompute equivalent key bindings. */
380 if (!NILP (map))
381 single_keymap_panes (map, Qnil, key, 1, skp->maxdepth - 1);
382 return;
385 enabled = XVECTOR (item_properties)->contents[ITEM_PROPERTY_ENABLE];
386 item_string = XVECTOR (item_properties)->contents[ITEM_PROPERTY_NAME];
388 if (!NILP (map) && SREF (item_string, 0) == '@')
390 if (!NILP (enabled))
391 /* An enabled separate pane. Remember this to handle it later. */
392 skp->pending_maps = Fcons (Fcons (map, Fcons (item_string, key)),
393 skp->pending_maps);
394 return;
397 #ifdef HAVE_X_WINDOWS
398 #ifndef HAVE_BOXES
399 /* Simulate radio buttons and toggle boxes by putting a prefix in
400 front of them. */
402 Lisp_Object prefix = Qnil;
403 Lisp_Object type = XVECTOR (item_properties)->contents[ITEM_PROPERTY_TYPE];
404 if (!NILP (type))
406 Lisp_Object selected
407 = XVECTOR (item_properties)->contents[ITEM_PROPERTY_SELECTED];
409 if (skp->notbuttons)
410 /* The first button. Line up previous items in this menu. */
412 int index = skp->notbuttons; /* Index for first item this menu. */
413 int submenu = 0;
414 Lisp_Object tem;
415 while (index < menu_items_used)
418 = XVECTOR (menu_items)->contents[index + MENU_ITEMS_ITEM_NAME];
419 if (NILP (tem))
421 index++;
422 submenu++; /* Skip sub menu. */
424 else if (EQ (tem, Qlambda))
426 index++;
427 submenu--; /* End sub menu. */
429 else if (EQ (tem, Qt))
430 index += 3; /* Skip new pane marker. */
431 else if (EQ (tem, Qquote))
432 index++; /* Skip a left, right divider. */
433 else
435 if (!submenu && SREF (tem, 0) != '\0'
436 && SREF (tem, 0) != '-')
437 XVECTOR (menu_items)->contents[index + MENU_ITEMS_ITEM_NAME]
438 = concat2 (build_string (" "), tem);
439 index += MENU_ITEMS_ITEM_LENGTH;
442 skp->notbuttons = 0;
445 /* Calculate prefix, if any, for this item. */
446 if (EQ (type, QCtoggle))
447 prefix = build_string (NILP (selected) ? "[ ] " : "[X] ");
448 else if (EQ (type, QCradio))
449 prefix = build_string (NILP (selected) ? "( ) " : "(*) ");
451 /* Not a button. If we have earlier buttons, then we need a prefix. */
452 else if (!skp->notbuttons && SREF (item_string, 0) != '\0'
453 && SREF (item_string, 0) != '-')
454 prefix = build_string (" ");
456 if (!NILP (prefix))
457 item_string = concat2 (prefix, item_string);
459 #endif /* not HAVE_BOXES */
461 #if ! defined (USE_X_TOOLKIT) && ! defined (USE_GTK)
462 if (!NILP (map))
463 /* Indicate visually that this is a submenu. */
464 item_string = concat2 (item_string, build_string (" >"));
465 #endif
467 #endif /* HAVE_X_WINDOWS */
469 push_menu_item (item_string, enabled, key,
470 XVECTOR (item_properties)->contents[ITEM_PROPERTY_DEF],
471 XVECTOR (item_properties)->contents[ITEM_PROPERTY_KEYEQ],
472 XVECTOR (item_properties)->contents[ITEM_PROPERTY_TYPE],
473 XVECTOR (item_properties)->contents[ITEM_PROPERTY_SELECTED],
474 XVECTOR (item_properties)->contents[ITEM_PROPERTY_HELP]);
476 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (HAVE_NTGUI)
477 /* Display a submenu using the toolkit. */
478 if (! (NILP (map) || NILP (enabled)))
480 push_submenu_start ();
481 single_keymap_panes (map, Qnil, key, 0, skp->maxdepth - 1);
482 push_submenu_end ();
484 #endif
487 /* Look through KEYMAPS, a vector of keymaps that is NMAPS long,
488 and generate menu panes for them in menu_items.
489 If NOTREAL is nonzero,
490 don't bother really computing whether an item is enabled. */
492 void
493 keymap_panes (keymaps, nmaps, notreal)
494 Lisp_Object *keymaps;
495 int nmaps;
496 int notreal;
498 int mapno;
500 init_menu_items ();
502 /* Loop over the given keymaps, making a pane for each map.
503 But don't make a pane that is empty--ignore that map instead.
504 P is the number of panes we have made so far. */
505 for (mapno = 0; mapno < nmaps; mapno++)
506 single_keymap_panes (keymaps[mapno],
507 Fkeymap_prompt (keymaps[mapno]), Qnil, notreal, 10);
509 finish_menu_items ();
513 /* Push the items in a single pane defined by the alist PANE. */
514 static void
515 list_of_items (pane)
516 Lisp_Object pane;
518 Lisp_Object tail, item, item1;
520 for (tail = pane; CONSP (tail); tail = XCDR (tail))
522 item = XCAR (tail);
523 if (STRINGP (item))
524 push_menu_item (ENCODE_MENU_STRING (item), Qnil, Qnil, Qt,
525 Qnil, Qnil, Qnil, Qnil);
526 else if (CONSP (item))
528 item1 = XCAR (item);
529 CHECK_STRING (item1);
530 push_menu_item (ENCODE_MENU_STRING (item1), Qt, XCDR (item),
531 Qt, Qnil, Qnil, Qnil, Qnil);
533 else
534 push_left_right_boundary ();
539 /* Push all the panes and items of a menu described by the
540 alist-of-alists MENU.
541 This handles old-fashioned calls to x-popup-menu. */
542 void
543 list_of_panes (menu)
544 Lisp_Object menu;
546 Lisp_Object tail;
548 init_menu_items ();
550 for (tail = menu; CONSP (tail); tail = XCDR (tail))
552 Lisp_Object elt, pane_name, pane_data;
553 elt = XCAR (tail);
554 pane_name = Fcar (elt);
555 CHECK_STRING (pane_name);
556 push_menu_pane (ENCODE_MENU_STRING (pane_name), Qnil);
557 pane_data = Fcdr (elt);
558 CHECK_CONS (pane_data);
559 list_of_items (pane_data);
562 finish_menu_items ();
565 /* Set up data in menu_items for a menu bar item
566 whose event type is ITEM_KEY (with string ITEM_NAME)
567 and whose contents come from the list of keymaps MAPS. */
569 parse_single_submenu (item_key, item_name, maps)
570 Lisp_Object item_key, item_name, maps;
572 Lisp_Object length;
573 int len;
574 Lisp_Object *mapvec;
575 int i;
576 int top_level_items = 0;
578 length = Flength (maps);
579 len = XINT (length);
581 /* Convert the list MAPS into a vector MAPVEC. */
582 mapvec = (Lisp_Object *) alloca (len * sizeof (Lisp_Object));
583 for (i = 0; i < len; i++)
585 mapvec[i] = Fcar (maps);
586 maps = Fcdr (maps);
589 /* Loop over the given keymaps, making a pane for each map.
590 But don't make a pane that is empty--ignore that map instead. */
591 for (i = 0; i < len; i++)
593 if (!KEYMAPP (mapvec[i]))
595 /* Here we have a command at top level in the menu bar
596 as opposed to a submenu. */
597 top_level_items = 1;
598 push_menu_pane (Qnil, Qnil);
599 push_menu_item (item_name, Qt, item_key, mapvec[i],
600 Qnil, Qnil, Qnil, Qnil);
602 else
604 Lisp_Object prompt;
605 prompt = Fkeymap_prompt (mapvec[i]);
606 single_keymap_panes (mapvec[i],
607 !NILP (prompt) ? prompt : item_name,
608 item_key, 0, 10);
612 return top_level_items;
616 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (HAVE_NTGUI)
618 /* Allocate a widget_value, blocking input. */
620 widget_value *
621 xmalloc_widget_value ()
623 widget_value *value;
625 BLOCK_INPUT;
626 value = malloc_widget_value ();
627 UNBLOCK_INPUT;
629 return value;
632 /* This recursively calls free_widget_value on the tree of widgets.
633 It must free all data that was malloc'ed for these widget_values.
634 In Emacs, many slots are pointers into the data of Lisp_Strings, and
635 must be left alone. */
637 void
638 free_menubar_widget_value_tree (wv)
639 widget_value *wv;
641 if (! wv) return;
643 wv->name = wv->value = wv->key = (char *) 0xDEADBEEF;
645 if (wv->contents && (wv->contents != (widget_value*)1))
647 free_menubar_widget_value_tree (wv->contents);
648 wv->contents = (widget_value *) 0xDEADBEEF;
650 if (wv->next)
652 free_menubar_widget_value_tree (wv->next);
653 wv->next = (widget_value *) 0xDEADBEEF;
655 BLOCK_INPUT;
656 free_widget_value (wv);
657 UNBLOCK_INPUT;
660 /* Create a tree of widget_value objects
661 representing the panes and items
662 in menu_items starting at index START, up to index END. */
664 widget_value *
665 digest_single_submenu (start, end, top_level_items)
666 int start, end, top_level_items;
668 widget_value *wv, *prev_wv, *save_wv, *first_wv;
669 int i;
670 int submenu_depth = 0;
671 widget_value **submenu_stack;
672 int panes_seen = 0;
674 submenu_stack
675 = (widget_value **) alloca (menu_items_used * sizeof (widget_value *));
676 wv = xmalloc_widget_value ();
677 wv->name = "menu";
678 wv->value = 0;
679 wv->enabled = 1;
680 wv->button_type = BUTTON_TYPE_NONE;
681 wv->help = Qnil;
682 first_wv = wv;
683 save_wv = 0;
684 prev_wv = 0;
686 /* Loop over all panes and items made by the preceding call
687 to parse_single_submenu and construct a tree of widget_value objects.
688 Ignore the panes and items used by previous calls to
689 digest_single_submenu, even though those are also in menu_items. */
690 i = start;
691 while (i < end)
693 if (EQ (XVECTOR (menu_items)->contents[i], Qnil))
695 submenu_stack[submenu_depth++] = save_wv;
696 save_wv = prev_wv;
697 prev_wv = 0;
698 i++;
700 else if (EQ (XVECTOR (menu_items)->contents[i], Qlambda))
702 prev_wv = save_wv;
703 save_wv = submenu_stack[--submenu_depth];
704 i++;
706 else if (EQ (XVECTOR (menu_items)->contents[i], Qt)
707 && submenu_depth != 0)
708 i += MENU_ITEMS_PANE_LENGTH;
709 /* Ignore a nil in the item list.
710 It's meaningful only for dialog boxes. */
711 else if (EQ (XVECTOR (menu_items)->contents[i], Qquote))
712 i += 1;
713 else if (EQ (XVECTOR (menu_items)->contents[i], Qt))
715 /* Create a new pane. */
716 Lisp_Object pane_name, prefix;
717 char *pane_string;
719 panes_seen++;
721 pane_name = XVECTOR (menu_items)->contents[i + MENU_ITEMS_PANE_NAME];
722 prefix = XVECTOR (menu_items)->contents[i + MENU_ITEMS_PANE_PREFIX];
724 #ifdef HAVE_NTGUI
725 if (STRINGP (pane_name))
727 if (unicode_append_menu)
728 /* Encode as UTF-8 for now. */
729 pane_name = ENCODE_UTF_8 (pane_name);
730 else if (STRING_MULTIBYTE (pane_name))
731 pane_name = ENCODE_SYSTEM (pane_name);
733 ASET (menu_items, i + MENU_ITEMS_PANE_NAME, pane_name);
735 #elif !defined (HAVE_MULTILINGUAL_MENU)
736 if (STRINGP (pane_name) && STRING_MULTIBYTE (pane_name))
738 pane_name = ENCODE_MENU_STRING (pane_name);
739 ASET (menu_items, i + MENU_ITEMS_PANE_NAME, pane_name);
741 #endif
743 pane_string = (NILP (pane_name)
744 ? "" : (char *) SDATA (pane_name));
745 /* If there is just one top-level pane, put all its items directly
746 under the top-level menu. */
747 if (menu_items_n_panes == 1)
748 pane_string = "";
750 /* If the pane has a meaningful name,
751 make the pane a top-level menu item
752 with its items as a submenu beneath it. */
753 if (strcmp (pane_string, ""))
755 wv = xmalloc_widget_value ();
756 if (save_wv)
757 save_wv->next = wv;
758 else
759 first_wv->contents = wv;
760 wv->lname = pane_name;
761 /* Set value to 1 so update_submenu_strings can handle '@' */
762 wv->value = (char *)1;
763 wv->enabled = 1;
764 wv->button_type = BUTTON_TYPE_NONE;
765 wv->help = Qnil;
766 save_wv = wv;
768 else
769 save_wv = first_wv;
771 prev_wv = 0;
772 i += MENU_ITEMS_PANE_LENGTH;
774 else
776 /* Create a new item within current pane. */
777 Lisp_Object item_name, enable, descrip, def, type, selected;
778 Lisp_Object help;
780 /* All items should be contained in panes. */
781 if (panes_seen == 0)
782 abort ();
784 item_name = AREF (menu_items, i + MENU_ITEMS_ITEM_NAME);
785 enable = AREF (menu_items, i + MENU_ITEMS_ITEM_ENABLE);
786 descrip = AREF (menu_items, i + MENU_ITEMS_ITEM_EQUIV_KEY);
787 def = AREF (menu_items, i + MENU_ITEMS_ITEM_DEFINITION);
788 type = AREF (menu_items, i + MENU_ITEMS_ITEM_TYPE);
789 selected = AREF (menu_items, i + MENU_ITEMS_ITEM_SELECTED);
790 help = AREF (menu_items, i + MENU_ITEMS_ITEM_HELP);
792 #ifdef HAVE_NTGUI
793 if (STRINGP (item_name))
795 if (unicode_append_menu)
796 item_name = ENCODE_UTF_8 (item_name);
797 else if (STRING_MULTIBYTE (item_name))
798 item_name = ENCODE_SYSTEM (item_name);
800 ASET (menu_items, i + MENU_ITEMS_ITEM_NAME, item_name);
803 if (STRINGP (descrip) && STRING_MULTIBYTE (descrip))
805 descrip = ENCODE_SYSTEM (descrip);
806 ASET (menu_items, i + MENU_ITEMS_ITEM_EQUIV_KEY, descrip);
808 #elif !defined (HAVE_MULTILINGUAL_MENU)
809 if (STRING_MULTIBYTE (item_name))
811 item_name = ENCODE_MENU_STRING (item_name);
812 ASET (menu_items, i + MENU_ITEMS_ITEM_NAME, item_name);
815 if (STRINGP (descrip) && STRING_MULTIBYTE (descrip))
817 descrip = ENCODE_MENU_STRING (descrip);
818 ASET (menu_items, i + MENU_ITEMS_ITEM_EQUIV_KEY, descrip);
820 #endif
822 wv = xmalloc_widget_value ();
823 if (prev_wv)
824 prev_wv->next = wv;
825 else
826 save_wv->contents = wv;
828 wv->lname = item_name;
829 if (!NILP (descrip))
830 wv->lkey = descrip;
831 wv->value = 0;
832 /* The EMACS_INT cast avoids a warning. There's no problem
833 as long as pointers have enough bits to hold small integers. */
834 wv->call_data = (!NILP (def) ? (void *) (EMACS_INT) i : 0);
835 wv->enabled = !NILP (enable);
837 if (NILP (type))
838 wv->button_type = BUTTON_TYPE_NONE;
839 else if (EQ (type, QCradio))
840 wv->button_type = BUTTON_TYPE_RADIO;
841 else if (EQ (type, QCtoggle))
842 wv->button_type = BUTTON_TYPE_TOGGLE;
843 else
844 abort ();
846 wv->selected = !NILP (selected);
847 if (! STRINGP (help))
848 help = Qnil;
850 wv->help = help;
852 prev_wv = wv;
854 i += MENU_ITEMS_ITEM_LENGTH;
858 /* If we have just one "menu item"
859 that was originally a button, return it by itself. */
860 if (top_level_items && first_wv->contents && first_wv->contents->next == 0)
862 wv = first_wv->contents;
863 free_widget_value (first_wv);
864 return wv;
867 return first_wv;
870 /* Walk through the widget_value tree starting at FIRST_WV and update
871 the char * pointers from the corresponding lisp values.
872 We do this after building the whole tree, since GC may happen while the
873 tree is constructed, and small strings are relocated. So we must wait
874 until no GC can happen before storing pointers into lisp values. */
875 void
876 update_submenu_strings (first_wv)
877 widget_value *first_wv;
879 widget_value *wv;
881 for (wv = first_wv; wv; wv = wv->next)
883 if (STRINGP (wv->lname))
885 wv->name = (char *) SDATA (wv->lname);
887 /* Ignore the @ that means "separate pane".
888 This is a kludge, but this isn't worth more time. */
889 if (wv->value == (char *)1)
891 if (wv->name[0] == '@')
892 wv->name++;
893 wv->value = 0;
897 if (STRINGP (wv->lkey))
898 wv->key = (char *) SDATA (wv->lkey);
900 if (wv->contents)
901 update_submenu_strings (wv->contents);
905 /* Find the menu selection and store it in the keyboard buffer.
906 F is the frame the menu is on.
907 MENU_BAR_ITEMS_USED is the length of VECTOR.
908 VECTOR is an array of menu events for the whole menu. */
910 void
911 find_and_call_menu_selection (f, menu_bar_items_used, vector, client_data)
912 FRAME_PTR f;
913 EMACS_INT menu_bar_items_used;
914 Lisp_Object vector;
915 void *client_data;
917 Lisp_Object prefix, entry;
918 Lisp_Object *subprefix_stack;
919 int submenu_depth = 0;
920 int i;
922 entry = Qnil;
923 subprefix_stack = (Lisp_Object *) alloca (menu_bar_items_used * sizeof (Lisp_Object));
924 prefix = Qnil;
925 i = 0;
927 while (i < menu_bar_items_used)
929 if (EQ (XVECTOR (vector)->contents[i], Qnil))
931 subprefix_stack[submenu_depth++] = prefix;
932 prefix = entry;
933 i++;
935 else if (EQ (XVECTOR (vector)->contents[i], Qlambda))
937 prefix = subprefix_stack[--submenu_depth];
938 i++;
940 else if (EQ (XVECTOR (vector)->contents[i], Qt))
942 prefix = XVECTOR (vector)->contents[i + MENU_ITEMS_PANE_PREFIX];
943 i += MENU_ITEMS_PANE_LENGTH;
945 else
947 entry = XVECTOR (vector)->contents[i + MENU_ITEMS_ITEM_VALUE];
948 /* The EMACS_INT cast avoids a warning. There's no problem
949 as long as pointers have enough bits to hold small integers. */
950 if ((int) (EMACS_INT) client_data == i)
952 int j;
953 struct input_event buf;
954 Lisp_Object frame;
955 EVENT_INIT (buf);
957 XSETFRAME (frame, f);
958 buf.kind = MENU_BAR_EVENT;
959 buf.frame_or_window = frame;
960 buf.arg = frame;
961 kbd_buffer_store_event (&buf);
963 for (j = 0; j < submenu_depth; j++)
964 if (!NILP (subprefix_stack[j]))
966 buf.kind = MENU_BAR_EVENT;
967 buf.frame_or_window = frame;
968 buf.arg = subprefix_stack[j];
969 kbd_buffer_store_event (&buf);
972 if (!NILP (prefix))
974 buf.kind = MENU_BAR_EVENT;
975 buf.frame_or_window = frame;
976 buf.arg = prefix;
977 kbd_buffer_store_event (&buf);
980 buf.kind = MENU_BAR_EVENT;
981 buf.frame_or_window = frame;
982 buf.arg = entry;
983 kbd_buffer_store_event (&buf);
985 return;
987 i += MENU_ITEMS_ITEM_LENGTH;
992 #endif /* USE_X_TOOLKIT || USE_GTK || HAVE_NTGUI */
994 void
995 syms_of_menu ()
997 staticpro (&menu_items);
998 menu_items = Qnil;
999 menu_items_inuse = Qnil;
1002 /* arch-tag: 78bbc7cf-8025-4156-aa8a-6c7fd99bf51d
1003 (do not change this comment) */