Commit | Line | Data |
---|---|---|
279a1d4b CY |
1 | /* Platform-independent code for terminal communications. |
2 | Copyright (C) 1986, 1988, 1993, 1994, 1996, 1999, 2000, 2001, 2002, 2003, | |
76b6f707 | 3 | 2004, 2005, 2006, 2007, 2008, 2009 Free Software Foundation, Inc. |
279a1d4b CY |
4 | |
5 | This file is part of GNU Emacs. | |
6 | ||
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. | |
11 | ||
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. | |
16 | ||
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/>. */ | |
19 | ||
20 | #include <config.h> | |
21 | #include <stdio.h> | |
22 | ||
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" | |
30 | ||
31 | #ifdef USE_X_TOOLKIT | |
32 | #include "../lwlib/lwlib.h" | |
33 | #endif | |
34 | ||
a4240420 AS |
35 | #ifdef HAVE_X_WINDOWS |
36 | #include "xterm.h" | |
37 | #endif | |
38 | ||
edfda783 AR |
39 | #ifdef HAVE_NS |
40 | #include "nsterm.h" | |
41 | #endif | |
42 | ||
279a1d4b CY |
43 | #ifdef USE_GTK |
44 | #include "gtkutil.h" | |
45 | #endif | |
46 | ||
47 | #ifdef HAVE_NTGUI | |
a8495745 | 48 | #include "w32term.h" |
279a1d4b | 49 | |
279a1d4b CY |
50 | extern AppendMenuW_Proc unicode_append_menu; |
51 | ||
52 | #endif /* HAVE_NTGUI */ | |
53 | ||
e7c9048f | 54 | #include "menu.h" |
279a1d4b CY |
55 | |
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 | |
60 | ||
61 | extern Lisp_Object QCtoggle, QCradio; | |
62 | ||
63 | Lisp_Object menu_items; | |
64 | ||
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; | |
68 | ||
69 | /* Number of slots currently allocated in menu_items. */ | |
70 | int menu_items_allocated; | |
71 | ||
72 | /* This is the index in menu_items of the first empty slot. */ | |
73 | int menu_items_used; | |
74 | ||
75 | /* The number of panes currently recorded in menu_items, | |
76 | excluding those within submenus. */ | |
77 | int menu_items_n_panes; | |
78 | ||
79 | /* Current depth within submenus. */ | |
80 | static int menu_items_submenu_depth; | |
81 | ||
82 | void | |
83 | init_menu_items () | |
84 | { | |
85 | if (!NILP (menu_items_inuse)) | |
86 | error ("Trying to use a menu from within a menu-entry"); | |
87 | ||
88 | if (NILP (menu_items)) | |
89 | { | |
90 | menu_items_allocated = 60; | |
91 | menu_items = Fmake_vector (make_number (menu_items_allocated), Qnil); | |
92 | } | |
93 | ||
94 | menu_items_inuse = Qt; | |
95 | menu_items_used = 0; | |
96 | menu_items_n_panes = 0; | |
97 | menu_items_submenu_depth = 0; | |
98 | } | |
99 | ||
100 | /* Call at the end of generating the data in menu_items. */ | |
101 | ||
102 | void | |
103 | finish_menu_items () | |
104 | { | |
105 | } | |
106 | ||
107 | Lisp_Object | |
108 | unuse_menu_items (dummy) | |
109 | Lisp_Object dummy; | |
110 | { | |
111 | return menu_items_inuse = Qnil; | |
112 | } | |
113 | ||
114 | /* Call when finished using the data for the current menu | |
115 | in menu_items. */ | |
116 | ||
117 | void | |
118 | discard_menu_items () | |
119 | { | |
120 | /* Free the structure if it is especially large. | |
121 | Otherwise, hold on to it, to save time. */ | |
122 | if (menu_items_allocated > 200) | |
123 | { | |
124 | menu_items = Qnil; | |
125 | menu_items_allocated = 0; | |
126 | } | |
127 | xassert (NILP (menu_items_inuse)); | |
128 | } | |
129 | ||
130 | /* This undoes save_menu_items, and it is called by the specpdl unwind | |
131 | mechanism. */ | |
132 | ||
133 | static Lisp_Object | |
134 | restore_menu_items (saved) | |
135 | Lisp_Object saved; | |
136 | { | |
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; | |
147 | } | |
148 | ||
149 | /* Push the whole state of menu_items processing onto the specpdl. | |
150 | It will be restored when the specpdl is unwound. */ | |
151 | ||
152 | void | |
153 | save_menu_items () | |
154 | { | |
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; | |
162 | } | |
163 | ||
164 | \f | |
165 | /* Make the menu_items vector twice as large. */ | |
166 | ||
167 | static void | |
168 | grow_menu_items () | |
169 | { | |
170 | menu_items_allocated *= 2; | |
171 | menu_items = larger_vector (menu_items, menu_items_allocated, Qnil); | |
172 | } | |
173 | ||
174 | /* Begin a submenu. */ | |
175 | ||
176 | static void | |
177 | push_submenu_start () | |
178 | { | |
179 | if (menu_items_used + 1 > menu_items_allocated) | |
180 | grow_menu_items (); | |
181 | ||
182 | XVECTOR (menu_items)->contents[menu_items_used++] = Qnil; | |
183 | menu_items_submenu_depth++; | |
184 | } | |
185 | ||
186 | /* End a submenu. */ | |
187 | ||
188 | static void | |
189 | push_submenu_end () | |
190 | { | |
191 | if (menu_items_used + 1 > menu_items_allocated) | |
192 | grow_menu_items (); | |
193 | ||
194 | XVECTOR (menu_items)->contents[menu_items_used++] = Qlambda; | |
195 | menu_items_submenu_depth--; | |
196 | } | |
197 | ||
198 | /* Indicate boundary between left and right. */ | |
199 | ||
200 | static void | |
201 | push_left_right_boundary () | |
202 | { | |
203 | if (menu_items_used + 1 > menu_items_allocated) | |
204 | grow_menu_items (); | |
205 | ||
206 | XVECTOR (menu_items)->contents[menu_items_used++] = Qquote; | |
207 | } | |
208 | ||
209 | /* Start a new menu pane in menu_items. | |
210 | NAME is the pane name. PREFIX_VEC is a prefix key for this pane. */ | |
211 | ||
593c843c | 212 | static void |
279a1d4b CY |
213 | push_menu_pane (name, prefix_vec) |
214 | Lisp_Object name, prefix_vec; | |
215 | { | |
216 | if (menu_items_used + MENU_ITEMS_PANE_LENGTH > menu_items_allocated) | |
217 | grow_menu_items (); | |
218 | ||
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; | |
224 | } | |
225 | ||
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'. */ | |
233 | ||
593c843c | 234 | static void |
279a1d4b CY |
235 | push_menu_item (name, enable, key, def, equiv, type, selected, help) |
236 | Lisp_Object name, enable, key, def, equiv, type, selected, help; | |
237 | { | |
238 | if (menu_items_used + MENU_ITEMS_ITEM_LENGTH > menu_items_allocated) | |
239 | grow_menu_items (); | |
240 | ||
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; | |
249 | } | |
250 | ||
251 | /* Args passed between single_keymap_panes and single_menu_item. */ | |
252 | struct skp | |
253 | { | |
254 | Lisp_Object pending_maps; | |
255 | int maxdepth, notreal; | |
256 | int notbuttons; | |
257 | }; | |
258 | ||
259 | static void single_menu_item P_ ((Lisp_Object, Lisp_Object, Lisp_Object, | |
260 | void *)); | |
261 | ||
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. | |
268 | ||
269 | If we encounter submenus deeper than MAXDEPTH levels, ignore them. */ | |
270 | ||
593c843c | 271 | static void |
279a1d4b CY |
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; | |
278 | { | |
279 | struct skp skp; | |
280 | struct gcpro gcpro1; | |
281 | ||
282 | skp.pending_maps = Qnil; | |
283 | skp.maxdepth = maxdepth; | |
284 | skp.notreal = notreal; | |
285 | skp.notbuttons = 0; | |
286 | ||
287 | if (maxdepth <= 0) | |
288 | return; | |
289 | ||
290 | push_menu_pane (pane_name, prefix); | |
291 | ||
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 | |
299 | ||
300 | GCPRO1 (skp.pending_maps); | |
301 | map_keymap_canonical (keymap, single_menu_item, Qnil, &skp); | |
302 | UNGCPRO; | |
303 | ||
304 | /* Process now any submenus which want to be panes at this level. */ | |
305 | while (CONSP (skp.pending_maps)) | |
306 | { | |
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); | |
316 | } | |
317 | } | |
318 | ||
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. */ | |
327 | ||
328 | static void | |
329 | single_menu_item (key, item, dummy, skp_v) | |
330 | Lisp_Object key, item, dummy; | |
331 | void *skp_v; | |
332 | { | |
333 | Lisp_Object map, item_string, enabled; | |
334 | struct gcpro gcpro1, gcpro2; | |
335 | int res; | |
336 | struct skp *skp = skp_v; | |
337 | ||
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. */ | |
344 | ||
345 | map = XVECTOR (item_properties)->contents[ITEM_PROPERTY_MAP]; | |
346 | ||
347 | if (skp->notreal) | |
348 | { | |
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; | |
354 | } | |
355 | ||
356 | enabled = XVECTOR (item_properties)->contents[ITEM_PROPERTY_ENABLE]; | |
357 | item_string = XVECTOR (item_properties)->contents[ITEM_PROPERTY_NAME]; | |
358 | ||
359 | if (!NILP (map) && SREF (item_string, 0) == '@') | |
360 | { | |
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; | |
366 | } | |
367 | ||
dda86321 | 368 | #if defined(HAVE_X_WINDOWS) || defined(MSDOS) |
279a1d4b CY |
369 | #ifndef HAVE_BOXES |
370 | /* Simulate radio buttons and toggle boxes by putting a prefix in | |
371 | front of them. */ | |
372 | { | |
373 | Lisp_Object prefix = Qnil; | |
374 | Lisp_Object type = XVECTOR (item_properties)->contents[ITEM_PROPERTY_TYPE]; | |
375 | if (!NILP (type)) | |
376 | { | |
377 | Lisp_Object selected | |
378 | = XVECTOR (item_properties)->contents[ITEM_PROPERTY_SELECTED]; | |
379 | ||
380 | if (skp->notbuttons) | |
381 | /* The first button. Line up previous items in this menu. */ | |
382 | { | |
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) | |
387 | { | |
388 | tem | |
389 | = XVECTOR (menu_items)->contents[index + MENU_ITEMS_ITEM_NAME]; | |
390 | if (NILP (tem)) | |
391 | { | |
392 | index++; | |
393 | submenu++; /* Skip sub menu. */ | |
394 | } | |
395 | else if (EQ (tem, Qlambda)) | |
396 | { | |
397 | index++; | |
398 | submenu--; /* End sub menu. */ | |
399 | } | |
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 | |
405 | { | |
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; | |
411 | } | |
412 | } | |
413 | skp->notbuttons = 0; | |
414 | } | |
415 | ||
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) ? "( ) " : "(*) "); | |
421 | } | |
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 (" "); | |
426 | ||
427 | if (!NILP (prefix)) | |
428 | item_string = concat2 (prefix, item_string); | |
429 | } | |
430 | #endif /* not HAVE_BOXES */ | |
431 | ||
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 | |
437 | ||
dda86321 | 438 | #endif /* HAVE_X_WINDOWS || MSDOS */ |
279a1d4b CY |
439 | |
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]); | |
446 | ||
edfda783 | 447 | #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (HAVE_NS) || defined (HAVE_NTGUI) |
279a1d4b CY |
448 | /* Display a submenu using the toolkit. */ |
449 | if (! (NILP (map) || NILP (enabled))) | |
450 | { | |
451 | push_submenu_start (); | |
452 | single_keymap_panes (map, Qnil, key, 0, skp->maxdepth - 1); | |
453 | push_submenu_end (); | |
454 | } | |
455 | #endif | |
456 | } | |
457 | ||
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. */ | |
462 | ||
463 | void | |
464 | keymap_panes (keymaps, nmaps, notreal) | |
465 | Lisp_Object *keymaps; | |
466 | int nmaps; | |
467 | int notreal; | |
468 | { | |
469 | int mapno; | |
470 | ||
471 | init_menu_items (); | |
472 | ||
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); | |
479 | ||
480 | finish_menu_items (); | |
481 | } | |
482 | ||
483 | ||
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; | |
488 | { | |
489 | Lisp_Object tail, item, item1; | |
490 | ||
491 | for (tail = pane; CONSP (tail); tail = XCDR (tail)) | |
492 | { | |
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)) | |
498 | { | |
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); | |
503 | } | |
504 | else | |
505 | push_left_right_boundary (); | |
506 | ||
507 | } | |
508 | } | |
509 | ||
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; | |
516 | { | |
517 | Lisp_Object tail; | |
518 | ||
519 | init_menu_items (); | |
520 | ||
521 | for (tail = menu; CONSP (tail); tail = XCDR (tail)) | |
522 | { | |
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); | |
531 | } | |
532 | ||
533 | finish_menu_items (); | |
534 | } | |
535 | ||
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. */ | |
539 | int | |
540 | parse_single_submenu (item_key, item_name, maps) | |
541 | Lisp_Object item_key, item_name, maps; | |
542 | { | |
543 | Lisp_Object length; | |
544 | int len; | |
545 | Lisp_Object *mapvec; | |
546 | int i; | |
547 | int top_level_items = 0; | |
548 | ||
549 | length = Flength (maps); | |
550 | len = XINT (length); | |
551 | ||
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++) | |
555 | { | |
556 | mapvec[i] = Fcar (maps); | |
557 | maps = Fcdr (maps); | |
558 | } | |
559 | ||
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++) | |
563 | { | |
564 | if (!KEYMAPP (mapvec[i])) | |
565 | { | |
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); | |
572 | } | |
573 | else | |
574 | { | |
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); | |
580 | } | |
581 | } | |
582 | ||
583 | return top_level_items; | |
584 | } | |
585 | ||
586 | \f | |
edfda783 | 587 | #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (HAVE_NS) || defined (HAVE_NTGUI) |
279a1d4b CY |
588 | |
589 | /* Allocate a widget_value, blocking input. */ | |
590 | ||
591 | widget_value * | |
592 | xmalloc_widget_value () | |
593 | { | |
594 | widget_value *value; | |
595 | ||
596 | BLOCK_INPUT; | |
597 | value = malloc_widget_value (); | |
598 | UNBLOCK_INPUT; | |
599 | ||
600 | return value; | |
601 | } | |
602 | ||
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. */ | |
607 | ||
608 | void | |
609 | free_menubar_widget_value_tree (wv) | |
610 | widget_value *wv; | |
611 | { | |
612 | if (! wv) return; | |
613 | ||
614 | wv->name = wv->value = wv->key = (char *) 0xDEADBEEF; | |
615 | ||
616 | if (wv->contents && (wv->contents != (widget_value*)1)) | |
617 | { | |
618 | free_menubar_widget_value_tree (wv->contents); | |
619 | wv->contents = (widget_value *) 0xDEADBEEF; | |
620 | } | |
621 | if (wv->next) | |
622 | { | |
623 | free_menubar_widget_value_tree (wv->next); | |
624 | wv->next = (widget_value *) 0xDEADBEEF; | |
625 | } | |
626 | BLOCK_INPUT; | |
627 | free_widget_value (wv); | |
628 | UNBLOCK_INPUT; | |
629 | } | |
630 | ||
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. */ | |
634 | ||
635 | widget_value * | |
636 | digest_single_submenu (start, end, top_level_items) | |
637 | int start, end, top_level_items; | |
638 | { | |
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; | |
644 | ||
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; | |
656 | ||
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) | |
663 | { | |
664 | if (EQ (XVECTOR (menu_items)->contents[i], Qnil)) | |
665 | { | |
666 | submenu_stack[submenu_depth++] = save_wv; | |
667 | save_wv = prev_wv; | |
668 | prev_wv = 0; | |
669 | i++; | |
670 | } | |
671 | else if (EQ (XVECTOR (menu_items)->contents[i], Qlambda)) | |
672 | { | |
673 | prev_wv = save_wv; | |
674 | save_wv = submenu_stack[--submenu_depth]; | |
675 | i++; | |
676 | } | |
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)) | |
685 | { | |
686 | /* Create a new pane. */ | |
687 | Lisp_Object pane_name, prefix; | |
688 | char *pane_string; | |
689 | ||
690 | panes_seen++; | |
691 | ||
692 | pane_name = XVECTOR (menu_items)->contents[i + MENU_ITEMS_PANE_NAME]; | |
693 | prefix = XVECTOR (menu_items)->contents[i + MENU_ITEMS_PANE_PREFIX]; | |
694 | ||
695 | #ifdef HAVE_NTGUI | |
696 | if (STRINGP (pane_name)) | |
697 | { | |
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); | |
703 | ||
704 | ASET (menu_items, i + MENU_ITEMS_PANE_NAME, pane_name); | |
705 | } | |
706 | #elif !defined (HAVE_MULTILINGUAL_MENU) | |
707 | if (STRINGP (pane_name) && STRING_MULTIBYTE (pane_name)) | |
708 | { | |
709 | pane_name = ENCODE_MENU_STRING (pane_name); | |
710 | ASET (menu_items, i + MENU_ITEMS_PANE_NAME, pane_name); | |
711 | } | |
712 | #endif | |
713 | ||
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 = ""; | |
720 | ||
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, "")) | |
725 | { | |
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; | |
738 | } | |
739 | else | |
740 | save_wv = first_wv; | |
741 | ||
742 | prev_wv = 0; | |
743 | i += MENU_ITEMS_PANE_LENGTH; | |
744 | } | |
745 | else | |
746 | { | |
747 | /* Create a new item within current pane. */ | |
748 | Lisp_Object item_name, enable, descrip, def, type, selected; | |
749 | Lisp_Object help; | |
750 | ||
751 | /* All items should be contained in panes. */ | |
752 | if (panes_seen == 0) | |
753 | abort (); | |
754 | ||
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); | |
762 | ||
763 | #ifdef HAVE_NTGUI | |
764 | if (STRINGP (item_name)) | |
765 | { | |
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); | |
770 | ||
771 | ASET (menu_items, i + MENU_ITEMS_ITEM_NAME, item_name); | |
772 | } | |
773 | ||
774 | if (STRINGP (descrip) && STRING_MULTIBYTE (descrip)) | |
775 | { | |
776 | descrip = ENCODE_SYSTEM (descrip); | |
777 | ASET (menu_items, i + MENU_ITEMS_ITEM_EQUIV_KEY, descrip); | |
778 | } | |
779 | #elif !defined (HAVE_MULTILINGUAL_MENU) | |
780 | if (STRING_MULTIBYTE (item_name)) | |
781 | { | |
782 | item_name = ENCODE_MENU_STRING (item_name); | |
783 | ASET (menu_items, i + MENU_ITEMS_ITEM_NAME, item_name); | |
784 | } | |
785 | ||
786 | if (STRINGP (descrip) && STRING_MULTIBYTE (descrip)) | |
787 | { | |
788 | descrip = ENCODE_MENU_STRING (descrip); | |
789 | ASET (menu_items, i + MENU_ITEMS_ITEM_EQUIV_KEY, descrip); | |
790 | } | |
791 | #endif | |
792 | ||
793 | wv = xmalloc_widget_value (); | |
794 | if (prev_wv) | |
795 | prev_wv->next = wv; | |
796 | else | |
797 | save_wv->contents = wv; | |
798 | ||
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); | |
807 | ||
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 (); | |
816 | ||
817 | wv->selected = !NILP (selected); | |
818 | if (! STRINGP (help)) | |
819 | help = Qnil; | |
820 | ||
821 | wv->help = help; | |
822 | ||
823 | prev_wv = wv; | |
824 | ||
825 | i += MENU_ITEMS_ITEM_LENGTH; | |
826 | } | |
827 | } | |
828 | ||
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) | |
832 | { | |
833 | wv = first_wv->contents; | |
834 | free_widget_value (first_wv); | |
835 | return wv; | |
836 | } | |
837 | ||
838 | return first_wv; | |
839 | } | |
840 | ||
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; | |
849 | { | |
850 | widget_value *wv; | |
851 | ||
852 | for (wv = first_wv; wv; wv = wv->next) | |
853 | { | |
854 | if (STRINGP (wv->lname)) | |
855 | { | |
856 | wv->name = (char *) SDATA (wv->lname); | |
857 | ||
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) | |
861 | { | |
862 | if (wv->name[0] == '@') | |
863 | wv->name++; | |
864 | wv->value = 0; | |
865 | } | |
866 | } | |
867 | ||
868 | if (STRINGP (wv->lkey)) | |
869 | wv->key = (char *) SDATA (wv->lkey); | |
870 | ||
871 | if (wv->contents) | |
872 | update_submenu_strings (wv->contents); | |
873 | } | |
874 | } | |
875 | ||
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. */ | |
880 | ||
881 | void | |
882 | find_and_call_menu_selection (f, menu_bar_items_used, vector, client_data) | |
883 | FRAME_PTR f; | |
facfbbbd | 884 | int menu_bar_items_used; |
279a1d4b CY |
885 | Lisp_Object vector; |
886 | void *client_data; | |
887 | { | |
888 | Lisp_Object prefix, entry; | |
889 | Lisp_Object *subprefix_stack; | |
890 | int submenu_depth = 0; | |
891 | int i; | |
892 | ||
893 | entry = Qnil; | |
894 | subprefix_stack = (Lisp_Object *) alloca (menu_bar_items_used * sizeof (Lisp_Object)); | |
895 | prefix = Qnil; | |
896 | i = 0; | |
897 | ||
898 | while (i < menu_bar_items_used) | |
899 | { | |
900 | if (EQ (XVECTOR (vector)->contents[i], Qnil)) | |
901 | { | |
902 | subprefix_stack[submenu_depth++] = prefix; | |
903 | prefix = entry; | |
904 | i++; | |
905 | } | |
906 | else if (EQ (XVECTOR (vector)->contents[i], Qlambda)) | |
907 | { | |
908 | prefix = subprefix_stack[--submenu_depth]; | |
909 | i++; | |
910 | } | |
911 | else if (EQ (XVECTOR (vector)->contents[i], Qt)) | |
912 | { | |
913 | prefix = XVECTOR (vector)->contents[i + MENU_ITEMS_PANE_PREFIX]; | |
914 | i += MENU_ITEMS_PANE_LENGTH; | |
915 | } | |
916 | else | |
917 | { | |
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) | |
922 | { | |
923 | int j; | |
924 | struct input_event buf; | |
925 | Lisp_Object frame; | |
926 | EVENT_INIT (buf); | |
927 | ||
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); | |
933 | ||
934 | for (j = 0; j < submenu_depth; j++) | |
935 | if (!NILP (subprefix_stack[j])) | |
936 | { | |
937 | buf.kind = MENU_BAR_EVENT; | |
938 | buf.frame_or_window = frame; | |
939 | buf.arg = subprefix_stack[j]; | |
940 | kbd_buffer_store_event (&buf); | |
941 | } | |
942 | ||
943 | if (!NILP (prefix)) | |
944 | { | |
945 | buf.kind = MENU_BAR_EVENT; | |
946 | buf.frame_or_window = frame; | |
947 | buf.arg = prefix; | |
948 | kbd_buffer_store_event (&buf); | |
949 | } | |
950 | ||
951 | buf.kind = MENU_BAR_EVENT; | |
952 | buf.frame_or_window = frame; | |
953 | buf.arg = entry; | |
954 | kbd_buffer_store_event (&buf); | |
955 | ||
956 | return; | |
957 | } | |
958 | i += MENU_ITEMS_ITEM_LENGTH; | |
959 | } | |
960 | } | |
961 | } | |
962 | ||
edfda783 AR |
963 | #endif /* USE_X_TOOLKIT || USE_GTK || HAVE_NS || HAVE_NTGUI */ |
964 | ||
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) | |
970 | { | |
971 | Lisp_Object prefix, entry; | |
972 | int i; | |
973 | Lisp_Object *subprefix_stack; | |
974 | int submenu_depth = 0; | |
975 | ||
976 | prefix = entry = Qnil; | |
977 | i = 0; | |
978 | subprefix_stack = | |
979 | (Lisp_Object *)alloca(menu_items_used * sizeof (Lisp_Object)); | |
980 | ||
981 | while (i < menu_items_used) | |
982 | { | |
983 | if (EQ (XVECTOR (menu_items)->contents[i], Qnil)) | |
984 | { | |
985 | subprefix_stack[submenu_depth++] = prefix; | |
986 | prefix = entry; | |
987 | i++; | |
988 | } | |
989 | else if (EQ (XVECTOR (menu_items)->contents[i], Qlambda)) | |
990 | { | |
991 | prefix = subprefix_stack[--submenu_depth]; | |
992 | i++; | |
993 | } | |
994 | else if (EQ (XVECTOR (menu_items)->contents[i], Qt)) | |
995 | { | |
996 | prefix | |
997 | = XVECTOR (menu_items)->contents[i + MENU_ITEMS_PANE_PREFIX]; | |
998 | i += MENU_ITEMS_PANE_LENGTH; | |
999 | } | |
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 | |
1005 | { | |
1006 | entry | |
1007 | = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_VALUE]; | |
2da2fd38 | 1008 | if ((int) (EMACS_INT)client_data == (int)(&XVECTOR (menu_items)->contents[i])) |
edfda783 AR |
1009 | { |
1010 | if (keymaps != 0) | |
1011 | { | |
1012 | int j; | |
1013 | ||
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); | |
1020 | } | |
1021 | return entry; | |
1022 | } | |
1023 | i += MENU_ITEMS_ITEM_LENGTH; | |
1024 | } | |
1025 | } | |
facfbbbd | 1026 | return Qnil; |
edfda783 AR |
1027 | } |
1028 | #endif | |
279a1d4b CY |
1029 | |
1030 | void | |
1031 | syms_of_menu () | |
1032 | { | |
1033 | staticpro (&menu_items); | |
1034 | menu_items = Qnil; | |
1035 | menu_items_inuse = Qnil; | |
1036 | } | |
041fa0d4 MB |
1037 | |
1038 | /* arch-tag: 78bbc7cf-8025-4156-aa8a-6c7fd99bf51d | |
1039 | (do not change this comment) */ |