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/>. */
27 #include "termhooks.h"
28 #include "blockinput.h"
29 #include "dispextern.h"
32 #include "../lwlib/lwlib.h"
44 /* Definitions copied from lwlib.h */
45 typedef void * XtPointer
;
54 /* This structure is based on the one in ../lwlib/lwlib.h */
55 typedef struct _widget_value
65 enum button_type button_type
;
67 struct _widget_value
* contents
;
69 struct _widget_value
* next
;
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)
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. */
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
;
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
;
125 menu_items_n_panes
= 0;
126 menu_items_submenu_depth
= 0;
129 /* Call at the end of generating the data in menu_items. */
137 unuse_menu_items (dummy
)
140 return menu_items_inuse
= Qnil
;
143 /* Call when finished using the data for the current menu
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)
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
163 restore_menu_items (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
));
178 /* Push the whole state of menu_items processing onto the specpdl.
179 It will be restored when the specpdl is unwound. */
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
;
194 /* Make the menu_items vector twice as large. */
199 menu_items_allocated
*= 2;
200 menu_items
= larger_vector (menu_items
, menu_items_allocated
, Qnil
);
203 /* Begin a submenu. */
206 push_submenu_start ()
208 if (menu_items_used
+ 1 > menu_items_allocated
)
211 XVECTOR (menu_items
)->contents
[menu_items_used
++] = Qnil
;
212 menu_items_submenu_depth
++;
220 if (menu_items_used
+ 1 > menu_items_allocated
)
223 XVECTOR (menu_items
)->contents
[menu_items_used
++] = Qlambda
;
224 menu_items_submenu_depth
--;
227 /* Indicate boundary between left and right. */
230 push_left_right_boundary ()
232 if (menu_items_used
+ 1 > menu_items_allocated
)
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. */
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
)
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'. */
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
)
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. */
283 Lisp_Object pending_maps
;
284 int maxdepth
, notreal
;
288 static void single_menu_item
P_ ((Lisp_Object
, Lisp_Object
, Lisp_Object
,
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. */
301 single_keymap_panes (keymap
, pane_name
, prefix
, notreal
, maxdepth
)
303 Lisp_Object pane_name
;
311 skp
.pending_maps
= Qnil
;
312 skp
.maxdepth
= maxdepth
;
313 skp
.notreal
= notreal
;
319 push_menu_pane (pane_name
, prefix
);
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
;
329 GCPRO1 (skp
.pending_maps
);
330 map_keymap_canonical (keymap
, single_menu_item
, Qnil
, &skp
);
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
);
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
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
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. */
358 single_menu_item (key
, item
, dummy
, skp_v
)
359 Lisp_Object key
, item
, dummy
;
362 Lisp_Object map
, item_string
, enabled
;
363 struct gcpro gcpro1
, gcpro2
;
365 struct skp
*skp
= skp_v
;
367 /* Parse the menu item and leave the result in item_properties. */
369 res
= parse_menu_item (item
, skp
->notreal
, 0);
372 return; /* Not a menu item. */
374 map
= XVECTOR (item_properties
)->contents
[ITEM_PROPERTY_MAP
];
378 /* We don't want to make a menu, just traverse the keymaps to
379 precompute equivalent key bindings. */
381 single_keymap_panes (map
, Qnil
, key
, 1, skp
->maxdepth
- 1);
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) == '@')
391 /* An enabled separate pane. Remember this to handle it later. */
392 skp
->pending_maps
= Fcons (Fcons (map
, Fcons (item_string
, key
)),
397 #ifdef HAVE_X_WINDOWS
399 /* Simulate radio buttons and toggle boxes by putting a prefix in
402 Lisp_Object prefix
= Qnil
;
403 Lisp_Object type
= XVECTOR (item_properties
)->contents
[ITEM_PROPERTY_TYPE
];
407 = XVECTOR (item_properties
)->contents
[ITEM_PROPERTY_SELECTED
];
410 /* The first button. Line up previous items in this menu. */
412 int index
= skp
->notbuttons
; /* Index for first item this menu. */
415 while (index
< menu_items_used
)
418 = XVECTOR (menu_items
)->contents
[index
+ MENU_ITEMS_ITEM_NAME
];
422 submenu
++; /* Skip sub menu. */
424 else if (EQ (tem
, Qlambda
))
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. */
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
;
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 (" ");
457 item_string
= concat2 (prefix
, item_string
);
459 #endif /* not HAVE_BOXES */
461 #if ! defined (USE_X_TOOLKIT) && ! defined (USE_GTK)
463 /* Indicate visually that this is a submenu. */
464 item_string
= concat2 (item_string
, build_string (" >"));
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);
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. */
493 keymap_panes (keymaps
, nmaps
, notreal
)
494 Lisp_Object
*keymaps
;
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. */
518 Lisp_Object tail
, item
, item1
;
520 for (tail
= pane
; CONSP (tail
); tail
= XCDR (tail
))
524 push_menu_item (ENCODE_MENU_STRING (item
), Qnil
, Qnil
, Qt
,
525 Qnil
, Qnil
, Qnil
, Qnil
);
526 else if (CONSP (item
))
529 CHECK_STRING (item1
);
530 push_menu_item (ENCODE_MENU_STRING (item1
), Qt
, XCDR (item
),
531 Qt
, Qnil
, Qnil
, Qnil
, Qnil
);
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. */
550 for (tail
= menu
; CONSP (tail
); tail
= XCDR (tail
))
552 Lisp_Object elt
, pane_name
, pane_data
;
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
;
576 int top_level_items
= 0;
578 length
= Flength (maps
);
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
);
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. */
598 push_menu_pane (Qnil
, Qnil
);
599 push_menu_item (item_name
, Qt
, item_key
, mapvec
[i
],
600 Qnil
, Qnil
, Qnil
, Qnil
);
605 prompt
= Fkeymap_prompt (mapvec
[i
]);
606 single_keymap_panes (mapvec
[i
],
607 !NILP (prompt
) ? prompt
: item_name
,
612 return top_level_items
;
616 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (HAVE_NTGUI)
618 /* Allocate a widget_value, blocking input. */
621 xmalloc_widget_value ()
626 value
= malloc_widget_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. */
638 free_menubar_widget_value_tree (wv
)
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;
652 free_menubar_widget_value_tree (wv
->next
);
653 wv
->next
= (widget_value
*) 0xDEADBEEF;
656 free_widget_value (wv
);
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. */
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
;
670 int submenu_depth
= 0;
671 widget_value
**submenu_stack
;
675 = (widget_value
**) alloca (menu_items_used
* sizeof (widget_value
*));
676 wv
= xmalloc_widget_value ();
680 wv
->button_type
= BUTTON_TYPE_NONE
;
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. */
693 if (EQ (XVECTOR (menu_items
)->contents
[i
], Qnil
))
695 submenu_stack
[submenu_depth
++] = save_wv
;
700 else if (EQ (XVECTOR (menu_items
)->contents
[i
], Qlambda
))
703 save_wv
= submenu_stack
[--submenu_depth
];
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
))
713 else if (EQ (XVECTOR (menu_items
)->contents
[i
], Qt
))
715 /* Create a new pane. */
716 Lisp_Object pane_name
, prefix
;
721 pane_name
= XVECTOR (menu_items
)->contents
[i
+ MENU_ITEMS_PANE_NAME
];
722 prefix
= XVECTOR (menu_items
)->contents
[i
+ MENU_ITEMS_PANE_PREFIX
];
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
);
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)
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 ();
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;
764 wv
->button_type
= BUTTON_TYPE_NONE
;
772 i
+= MENU_ITEMS_PANE_LENGTH
;
776 /* Create a new item within current pane. */
777 Lisp_Object item_name
, enable
, descrip
, def
, type
, selected
;
780 /* All items should be contained in panes. */
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
);
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
);
822 wv
= xmalloc_widget_value ();
826 save_wv
->contents
= wv
;
828 wv
->lname
= item_name
;
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
);
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
;
846 wv
->selected
= !NILP (selected
);
847 if (! STRINGP (help
))
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
);
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. */
876 update_submenu_strings (first_wv
)
877 widget_value
*first_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] == '@')
897 if (STRINGP (wv
->lkey
))
898 wv
->key
= (char *) SDATA (wv
->lkey
);
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. */
911 find_and_call_menu_selection (f
, menu_bar_items_used
, vector
, client_data
)
913 EMACS_INT menu_bar_items_used
;
917 Lisp_Object prefix
, entry
;
918 Lisp_Object
*subprefix_stack
;
919 int submenu_depth
= 0;
923 subprefix_stack
= (Lisp_Object
*) alloca (menu_bar_items_used
* sizeof (Lisp_Object
));
927 while (i
< menu_bar_items_used
)
929 if (EQ (XVECTOR (vector
)->contents
[i
], Qnil
))
931 subprefix_stack
[submenu_depth
++] = prefix
;
935 else if (EQ (XVECTOR (vector
)->contents
[i
], Qlambda
))
937 prefix
= subprefix_stack
[--submenu_depth
];
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
;
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
)
953 struct input_event buf
;
957 XSETFRAME (frame
, f
);
958 buf
.kind
= MENU_BAR_EVENT
;
959 buf
.frame_or_window
= 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
);
974 buf
.kind
= MENU_BAR_EVENT
;
975 buf
.frame_or_window
= frame
;
977 kbd_buffer_store_event (&buf
);
980 buf
.kind
= MENU_BAR_EVENT
;
981 buf
.frame_or_window
= frame
;
983 kbd_buffer_store_event (&buf
);
987 i
+= MENU_ITEMS_ITEM_LENGTH
;
992 #endif /* USE_X_TOOLKIT || USE_GTK || HAVE_NTGUI */
997 staticpro (&menu_items
);
999 menu_items_inuse
= Qnil
;
1002 /* arch-tag: 78bbc7cf-8025-4156-aa8a-6c7fd99bf51d
1003 (do not change this comment) */