Upgraded to mh-e version 6.1.1.
[bpt/emacs.git] / src / xmenu.c
1 /* X Communication module for terminals which understand the X protocol.
2 Copyright (C) 1986, 88, 93, 94, 96, 99, 2000, 2001
3 Free Software Foundation, Inc.
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 2, or (at your option)
10 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; see the file COPYING. If not, write to
19 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA. */
21
22 /* X pop-up deck-of-cards menu facility for GNU Emacs.
23 *
24 * Written by Jon Arnold and Roman Budzianowski
25 * Mods and rewrite by Robert Krawitz
26 *
27 */
28
29 /* Modified by Fred Pierresteguy on December 93
30 to make the popup menus and menubar use the Xt. */
31
32 /* Rewritten for clarity and GC protection by rms in Feb 94. */
33
34 #include <config.h>
35
36 /* On 4.3 this loses if it comes after xterm.h. */
37 #include <signal.h>
38
39 #include <stdio.h>
40
41 #include "lisp.h"
42 #include "termhooks.h"
43 #include "keyboard.h"
44 #include "keymap.h"
45 #include "frame.h"
46 #include "window.h"
47 #include "blockinput.h"
48 #include "buffer.h"
49 #include "charset.h"
50 #include "coding.h"
51
52 #ifdef MSDOS
53 #include "msdos.h"
54 #endif
55
56 #ifdef HAVE_X_WINDOWS
57 /* This may include sys/types.h, and that somehow loses
58 if this is not done before the other system files. */
59 #include "xterm.h"
60 #endif
61
62 /* Load sys/types.h if not already loaded.
63 In some systems loading it twice is suicidal. */
64 #ifndef makedev
65 #include <sys/types.h>
66 #endif
67
68 #include "dispextern.h"
69
70 #ifdef HAVE_X_WINDOWS
71 #undef HAVE_MULTILINGUAL_MENU
72 #ifdef USE_X_TOOLKIT
73 #include "widget.h"
74 #include <X11/Xlib.h>
75 #include <X11/IntrinsicP.h>
76 #include <X11/CoreP.h>
77 #include <X11/StringDefs.h>
78 #include <X11/Shell.h>
79 #ifdef USE_LUCID
80 #include <X11/Xaw/Paned.h>
81 #endif /* USE_LUCID */
82 #include "../lwlib/lwlib.h"
83 #else /* not USE_X_TOOLKIT */
84 #include "../oldXMenu/XMenu.h"
85 #endif /* not USE_X_TOOLKIT */
86 #endif /* HAVE_X_WINDOWS */
87
88 #ifndef TRUE
89 #define TRUE 1
90 #define FALSE 0
91 #endif /* no TRUE */
92
93 Lisp_Object Vmenu_updating_frame;
94
95 Lisp_Object Qdebug_on_next_call;
96
97 extern Lisp_Object Qmenu_bar;
98 extern Lisp_Object Qmouse_click, Qevent_kind;
99
100 extern Lisp_Object QCtoggle, QCradio;
101
102 extern Lisp_Object Voverriding_local_map;
103 extern Lisp_Object Voverriding_local_map_menu_flag;
104
105 extern Lisp_Object Qoverriding_local_map, Qoverriding_terminal_local_map;
106
107 extern Lisp_Object Qmenu_bar_update_hook;
108
109 #ifdef USE_X_TOOLKIT
110 extern void set_frame_menubar ();
111 extern void process_expose_from_menu ();
112 extern XtAppContext Xt_app_con;
113
114 static Lisp_Object xdialog_show ();
115 void popup_get_selection ();
116
117 /* Define HAVE_BOXES if menus can handle radio and toggle buttons. */
118
119 #define HAVE_BOXES 1
120 #endif
121
122 static void push_menu_item P_ ((Lisp_Object, Lisp_Object, Lisp_Object,
123 Lisp_Object, Lisp_Object, Lisp_Object,
124 Lisp_Object, Lisp_Object));
125 static int update_frame_menubar P_ ((struct frame *));
126 static Lisp_Object xmenu_show P_ ((struct frame *, int, int, int, int,
127 Lisp_Object, char **));
128 static void keymap_panes P_ ((Lisp_Object *, int, int));
129 static void single_keymap_panes P_ ((Lisp_Object, Lisp_Object, Lisp_Object,
130 int, int));
131 static void single_menu_item P_ ((Lisp_Object, Lisp_Object, Lisp_Object *,
132 int, int, int *));
133 static void list_of_panes P_ ((Lisp_Object));
134 static void list_of_items P_ ((Lisp_Object));
135 \f
136 /* This holds a Lisp vector that holds the results of decoding
137 the keymaps or alist-of-alists that specify a menu.
138
139 It describes the panes and items within the panes.
140
141 Each pane is described by 3 elements in the vector:
142 t, the pane name, the pane's prefix key.
143 Then follow the pane's items, with 5 elements per item:
144 the item string, the enable flag, the item's value,
145 the definition, and the equivalent keyboard key's description string.
146
147 In some cases, multiple levels of menus may be described.
148 A single vector slot containing nil indicates the start of a submenu.
149 A single vector slot containing lambda indicates the end of a submenu.
150 The submenu follows a menu item which is the way to reach the submenu.
151
152 A single vector slot containing quote indicates that the
153 following items should appear on the right of a dialog box.
154
155 Using a Lisp vector to hold this information while we decode it
156 takes care of protecting all the data from GC. */
157
158 #define MENU_ITEMS_PANE_NAME 1
159 #define MENU_ITEMS_PANE_PREFIX 2
160 #define MENU_ITEMS_PANE_LENGTH 3
161
162 enum menu_item_idx
163 {
164 MENU_ITEMS_ITEM_NAME = 0,
165 MENU_ITEMS_ITEM_ENABLE,
166 MENU_ITEMS_ITEM_VALUE,
167 MENU_ITEMS_ITEM_EQUIV_KEY,
168 MENU_ITEMS_ITEM_DEFINITION,
169 MENU_ITEMS_ITEM_TYPE,
170 MENU_ITEMS_ITEM_SELECTED,
171 MENU_ITEMS_ITEM_HELP,
172 MENU_ITEMS_ITEM_LENGTH
173 };
174
175 static Lisp_Object menu_items;
176
177 /* Number of slots currently allocated in menu_items. */
178 static int menu_items_allocated;
179
180 /* This is the index in menu_items of the first empty slot. */
181 static int menu_items_used;
182
183 /* The number of panes currently recorded in menu_items,
184 excluding those within submenus. */
185 static int menu_items_n_panes;
186
187 /* Current depth within submenus. */
188 static int menu_items_submenu_depth;
189
190 /* Flag which when set indicates a dialog or menu has been posted by
191 Xt on behalf of one of the widget sets. */
192 int popup_activated_flag;
193
194 static int next_menubar_widget_id;
195
196 /* This is set nonzero after the user activates the menu bar, and set
197 to zero again after the menu bars are redisplayed by prepare_menu_bar.
198 While it is nonzero, all calls to set_frame_menubar go deep.
199
200 I don't understand why this is needed, but it does seem to be
201 needed on Motif, according to Marcus Daniels <marcus@sysc.pdx.edu>. */
202
203 int pending_menu_activation;
204 \f
205 #ifdef USE_X_TOOLKIT
206
207 /* Return the frame whose ->output_data.x->id equals ID, or 0 if none. */
208
209 static struct frame *
210 menubar_id_to_frame (id)
211 LWLIB_ID id;
212 {
213 Lisp_Object tail, frame;
214 FRAME_PTR f;
215
216 for (tail = Vframe_list; GC_CONSP (tail); tail = XCDR (tail))
217 {
218 frame = XCAR (tail);
219 if (!GC_FRAMEP (frame))
220 continue;
221 f = XFRAME (frame);
222 if (!FRAME_WINDOW_P (f))
223 continue;
224 if (f->output_data.x->id == id)
225 return f;
226 }
227 return 0;
228 }
229
230 #endif
231 \f
232 /* Initialize the menu_items structure if we haven't already done so.
233 Also mark it as currently empty. */
234
235 static void
236 init_menu_items ()
237 {
238 if (NILP (menu_items))
239 {
240 menu_items_allocated = 60;
241 menu_items = Fmake_vector (make_number (menu_items_allocated), Qnil);
242 }
243
244 menu_items_used = 0;
245 menu_items_n_panes = 0;
246 menu_items_submenu_depth = 0;
247 }
248
249 /* Call at the end of generating the data in menu_items. */
250
251 static void
252 finish_menu_items ()
253 {
254 }
255
256 /* Call when finished using the data for the current menu
257 in menu_items. */
258
259 static void
260 discard_menu_items ()
261 {
262 /* Free the structure if it is especially large.
263 Otherwise, hold on to it, to save time. */
264 if (menu_items_allocated > 200)
265 {
266 menu_items = Qnil;
267 menu_items_allocated = 0;
268 }
269 }
270
271 /* Make the menu_items vector twice as large. */
272
273 static void
274 grow_menu_items ()
275 {
276 Lisp_Object old;
277 int old_size = menu_items_allocated;
278 old = menu_items;
279
280 menu_items_allocated *= 2;
281 menu_items = Fmake_vector (make_number (menu_items_allocated), Qnil);
282 bcopy (XVECTOR (old)->contents, XVECTOR (menu_items)->contents,
283 old_size * sizeof (Lisp_Object));
284 }
285
286 /* Begin a submenu. */
287
288 static void
289 push_submenu_start ()
290 {
291 if (menu_items_used + 1 > menu_items_allocated)
292 grow_menu_items ();
293
294 XVECTOR (menu_items)->contents[menu_items_used++] = Qnil;
295 menu_items_submenu_depth++;
296 }
297
298 /* End a submenu. */
299
300 static void
301 push_submenu_end ()
302 {
303 if (menu_items_used + 1 > menu_items_allocated)
304 grow_menu_items ();
305
306 XVECTOR (menu_items)->contents[menu_items_used++] = Qlambda;
307 menu_items_submenu_depth--;
308 }
309
310 /* Indicate boundary between left and right. */
311
312 static void
313 push_left_right_boundary ()
314 {
315 if (menu_items_used + 1 > menu_items_allocated)
316 grow_menu_items ();
317
318 XVECTOR (menu_items)->contents[menu_items_used++] = Qquote;
319 }
320
321 /* Start a new menu pane in menu_items.
322 NAME is the pane name. PREFIX_VEC is a prefix key for this pane. */
323
324 static void
325 push_menu_pane (name, prefix_vec)
326 Lisp_Object name, prefix_vec;
327 {
328 if (menu_items_used + MENU_ITEMS_PANE_LENGTH > menu_items_allocated)
329 grow_menu_items ();
330
331 if (menu_items_submenu_depth == 0)
332 menu_items_n_panes++;
333 XVECTOR (menu_items)->contents[menu_items_used++] = Qt;
334 XVECTOR (menu_items)->contents[menu_items_used++] = name;
335 XVECTOR (menu_items)->contents[menu_items_used++] = prefix_vec;
336 }
337
338 /* Push one menu item into the current pane. NAME is the string to
339 display. ENABLE if non-nil means this item can be selected. KEY
340 is the key generated by choosing this item, or nil if this item
341 doesn't really have a definition. DEF is the definition of this
342 item. EQUIV is the textual description of the keyboard equivalent
343 for this item (or nil if none). TYPE is the type of this menu
344 item, one of nil, `toggle' or `radio'. */
345
346 static void
347 push_menu_item (name, enable, key, def, equiv, type, selected, help)
348 Lisp_Object name, enable, key, def, equiv, type, selected, help;
349 {
350 if (menu_items_used + MENU_ITEMS_ITEM_LENGTH > menu_items_allocated)
351 grow_menu_items ();
352
353 XVECTOR (menu_items)->contents[menu_items_used++] = name;
354 XVECTOR (menu_items)->contents[menu_items_used++] = enable;
355 XVECTOR (menu_items)->contents[menu_items_used++] = key;
356 XVECTOR (menu_items)->contents[menu_items_used++] = equiv;
357 XVECTOR (menu_items)->contents[menu_items_used++] = def;
358 XVECTOR (menu_items)->contents[menu_items_used++] = type;
359 XVECTOR (menu_items)->contents[menu_items_used++] = selected;
360 XVECTOR (menu_items)->contents[menu_items_used++] = help;
361 }
362 \f
363 /* Look through KEYMAPS, a vector of keymaps that is NMAPS long,
364 and generate menu panes for them in menu_items.
365 If NOTREAL is nonzero,
366 don't bother really computing whether an item is enabled. */
367
368 static void
369 keymap_panes (keymaps, nmaps, notreal)
370 Lisp_Object *keymaps;
371 int nmaps;
372 int notreal;
373 {
374 int mapno;
375
376 init_menu_items ();
377
378 /* Loop over the given keymaps, making a pane for each map.
379 But don't make a pane that is empty--ignore that map instead.
380 P is the number of panes we have made so far. */
381 for (mapno = 0; mapno < nmaps; mapno++)
382 single_keymap_panes (keymaps[mapno],
383 Fkeymap_prompt (keymaps[mapno]), Qnil, notreal, 10);
384
385 finish_menu_items ();
386 }
387
388 /* This is a recursive subroutine of keymap_panes.
389 It handles one keymap, KEYMAP.
390 The other arguments are passed along
391 or point to local variables of the previous function.
392 If NOTREAL is nonzero, only check for equivalent key bindings, don't
393 evaluate expressions in menu items and don't make any menu.
394
395 If we encounter submenus deeper than MAXDEPTH levels, ignore them. */
396
397 static void
398 single_keymap_panes (keymap, pane_name, prefix, notreal, maxdepth)
399 Lisp_Object keymap;
400 Lisp_Object pane_name;
401 Lisp_Object prefix;
402 int notreal;
403 int maxdepth;
404 {
405 Lisp_Object pending_maps = Qnil;
406 Lisp_Object tail, item;
407 struct gcpro gcpro1, gcpro2;
408 int notbuttons = 0;
409
410 if (maxdepth <= 0)
411 return;
412
413 push_menu_pane (pane_name, prefix);
414
415 #ifndef HAVE_BOXES
416 /* Remember index for first item in this pane so we can go back and
417 add a prefix when (if) we see the first button. After that, notbuttons
418 is set to 0, to mark that we have seen a button and all non button
419 items need a prefix. */
420 notbuttons = menu_items_used;
421 #endif
422
423 for (tail = keymap; CONSP (tail); tail = XCDR (tail))
424 {
425 GCPRO2 (keymap, pending_maps);
426 /* Look at each key binding, and if it is a menu item add it
427 to this menu. */
428 item = XCAR (tail);
429 if (CONSP (item))
430 single_menu_item (XCAR (item), XCDR (item),
431 &pending_maps, notreal, maxdepth, &notbuttons);
432 else if (VECTORP (item))
433 {
434 /* Loop over the char values represented in the vector. */
435 int len = XVECTOR (item)->size;
436 int c;
437 for (c = 0; c < len; c++)
438 {
439 Lisp_Object character;
440 XSETFASTINT (character, c);
441 single_menu_item (character, XVECTOR (item)->contents[c],
442 &pending_maps, notreal, maxdepth, &notbuttons);
443 }
444 }
445 UNGCPRO;
446 }
447
448 /* Process now any submenus which want to be panes at this level. */
449 while (!NILP (pending_maps))
450 {
451 Lisp_Object elt, eltcdr, string;
452 elt = Fcar (pending_maps);
453 eltcdr = XCDR (elt);
454 string = XCAR (eltcdr);
455 /* We no longer discard the @ from the beginning of the string here.
456 Instead, we do this in xmenu_show. */
457 single_keymap_panes (Fcar (elt), string,
458 XCDR (eltcdr), notreal, maxdepth - 1);
459 pending_maps = Fcdr (pending_maps);
460 }
461 }
462 \f
463 /* This is a subroutine of single_keymap_panes that handles one
464 keymap entry.
465 KEY is a key in a keymap and ITEM is its binding.
466 PENDING_MAPS_PTR points to a list of keymaps waiting to be made into
467 separate panes.
468 If NOTREAL is nonzero, only check for equivalent key bindings, don't
469 evaluate expressions in menu items and don't make any menu.
470 If we encounter submenus deeper than MAXDEPTH levels, ignore them.
471 NOTBUTTONS_PTR is only used when simulating toggle boxes and radio
472 buttons. It points to variable notbuttons in single_keymap_panes,
473 which keeps track of if we have seen a button in this menu or not. */
474
475 static void
476 single_menu_item (key, item, pending_maps_ptr, notreal, maxdepth,
477 notbuttons_ptr)
478 Lisp_Object key, item;
479 Lisp_Object *pending_maps_ptr;
480 int maxdepth, notreal;
481 int *notbuttons_ptr;
482 {
483 Lisp_Object map, item_string, enabled;
484 struct gcpro gcpro1, gcpro2;
485 int res;
486
487 /* Parse the menu item and leave the result in item_properties. */
488 GCPRO2 (key, item);
489 res = parse_menu_item (item, notreal, 0);
490 UNGCPRO;
491 if (!res)
492 return; /* Not a menu item. */
493
494 map = XVECTOR (item_properties)->contents[ITEM_PROPERTY_MAP];
495
496 if (notreal)
497 {
498 /* We don't want to make a menu, just traverse the keymaps to
499 precompute equivalent key bindings. */
500 if (!NILP (map))
501 single_keymap_panes (map, Qnil, key, 1, maxdepth - 1);
502 return;
503 }
504
505 enabled = XVECTOR (item_properties)->contents[ITEM_PROPERTY_ENABLE];
506 item_string = XVECTOR (item_properties)->contents[ITEM_PROPERTY_NAME];
507
508 if (!NILP (map) && SREF (item_string, 0) == '@')
509 {
510 if (!NILP (enabled))
511 /* An enabled separate pane. Remember this to handle it later. */
512 *pending_maps_ptr = Fcons (Fcons (map, Fcons (item_string, key)),
513 *pending_maps_ptr);
514 return;
515 }
516
517 #ifndef HAVE_BOXES
518 /* Simulate radio buttons and toggle boxes by putting a prefix in
519 front of them. */
520 {
521 Lisp_Object prefix = Qnil;
522 Lisp_Object type = XVECTOR (item_properties)->contents[ITEM_PROPERTY_TYPE];
523 if (!NILP (type))
524 {
525 Lisp_Object selected
526 = XVECTOR (item_properties)->contents[ITEM_PROPERTY_SELECTED];
527
528 if (*notbuttons_ptr)
529 /* The first button. Line up previous items in this menu. */
530 {
531 int index = *notbuttons_ptr; /* Index for first item this menu. */
532 int submenu = 0;
533 Lisp_Object tem;
534 while (index < menu_items_used)
535 {
536 tem
537 = XVECTOR (menu_items)->contents[index + MENU_ITEMS_ITEM_NAME];
538 if (NILP (tem))
539 {
540 index++;
541 submenu++; /* Skip sub menu. */
542 }
543 else if (EQ (tem, Qlambda))
544 {
545 index++;
546 submenu--; /* End sub menu. */
547 }
548 else if (EQ (tem, Qt))
549 index += 3; /* Skip new pane marker. */
550 else if (EQ (tem, Qquote))
551 index++; /* Skip a left, right divider. */
552 else
553 {
554 if (!submenu && SREF (tem, 0) != '\0'
555 && SREF (tem, 0) != '-')
556 XVECTOR (menu_items)->contents[index + MENU_ITEMS_ITEM_NAME]
557 = concat2 (build_string (" "), tem);
558 index += MENU_ITEMS_ITEM_LENGTH;
559 }
560 }
561 *notbuttons_ptr = 0;
562 }
563
564 /* Calculate prefix, if any, for this item. */
565 if (EQ (type, QCtoggle))
566 prefix = build_string (NILP (selected) ? "[ ] " : "[X] ");
567 else if (EQ (type, QCradio))
568 prefix = build_string (NILP (selected) ? "( ) " : "(*) ");
569 }
570 /* Not a button. If we have earlier buttons, then we need a prefix. */
571 else if (!*notbuttons_ptr && SREF (item_string, 0) != '\0'
572 && SREF (item_string, 0) != '-')
573 prefix = build_string (" ");
574
575 if (!NILP (prefix))
576 item_string = concat2 (prefix, item_string);
577 }
578 #endif /* not HAVE_BOXES */
579
580 #ifndef USE_X_TOOLKIT
581 if (!NILP(map))
582 /* Indicate visually that this is a submenu. */
583 item_string = concat2 (item_string, build_string (" >"));
584 #endif
585
586 push_menu_item (item_string, enabled, key,
587 XVECTOR (item_properties)->contents[ITEM_PROPERTY_DEF],
588 XVECTOR (item_properties)->contents[ITEM_PROPERTY_KEYEQ],
589 XVECTOR (item_properties)->contents[ITEM_PROPERTY_TYPE],
590 XVECTOR (item_properties)->contents[ITEM_PROPERTY_SELECTED],
591 XVECTOR (item_properties)->contents[ITEM_PROPERTY_HELP]);
592
593 #ifdef USE_X_TOOLKIT
594 /* Display a submenu using the toolkit. */
595 if (! (NILP (map) || NILP (enabled)))
596 {
597 push_submenu_start ();
598 single_keymap_panes (map, Qnil, key, 0, maxdepth - 1);
599 push_submenu_end ();
600 }
601 #endif
602 }
603 \f
604 /* Push all the panes and items of a menu described by the
605 alist-of-alists MENU.
606 This handles old-fashioned calls to x-popup-menu. */
607
608 static void
609 list_of_panes (menu)
610 Lisp_Object menu;
611 {
612 Lisp_Object tail;
613
614 init_menu_items ();
615
616 for (tail = menu; !NILP (tail); tail = Fcdr (tail))
617 {
618 Lisp_Object elt, pane_name, pane_data;
619 elt = Fcar (tail);
620 pane_name = Fcar (elt);
621 CHECK_STRING (pane_name);
622 push_menu_pane (pane_name, Qnil);
623 pane_data = Fcdr (elt);
624 CHECK_CONS (pane_data);
625 list_of_items (pane_data);
626 }
627
628 finish_menu_items ();
629 }
630
631 /* Push the items in a single pane defined by the alist PANE. */
632
633 static void
634 list_of_items (pane)
635 Lisp_Object pane;
636 {
637 Lisp_Object tail, item, item1;
638
639 for (tail = pane; !NILP (tail); tail = Fcdr (tail))
640 {
641 item = Fcar (tail);
642 if (STRINGP (item))
643 push_menu_item (item, Qnil, Qnil, Qt, Qnil, Qnil, Qnil, Qnil);
644 else if (NILP (item))
645 push_left_right_boundary ();
646 else
647 {
648 CHECK_CONS (item);
649 item1 = Fcar (item);
650 CHECK_STRING (item1);
651 push_menu_item (item1, Qt, Fcdr (item), Qt, Qnil, Qnil, Qnil, Qnil);
652 }
653 }
654 }
655 \f
656 DEFUN ("x-popup-menu", Fx_popup_menu, Sx_popup_menu, 2, 2, 0,
657 doc: /* Pop up a deck-of-cards menu and return user's selection.
658 POSITION is a position specification. This is either a mouse button event
659 or a list ((XOFFSET YOFFSET) WINDOW)
660 where XOFFSET and YOFFSET are positions in pixels from the top left
661 corner of WINDOW's frame. (WINDOW may be a frame object instead of a window.)
662 This controls the position of the center of the first line
663 in the first pane of the menu, not the top left of the menu as a whole.
664 If POSITION is t, it means to use the current mouse position.
665
666 MENU is a specifier for a menu. For the simplest case, MENU is a keymap.
667 The menu items come from key bindings that have a menu string as well as
668 a definition; actually, the "definition" in such a key binding looks like
669 \(STRING . REAL-DEFINITION). To give the menu a title, put a string into
670 the keymap as a top-level element.
671
672 If REAL-DEFINITION is nil, that puts a nonselectable string in the menu.
673 Otherwise, REAL-DEFINITION should be a valid key binding definition.
674
675 You can also use a list of keymaps as MENU.
676 Then each keymap makes a separate pane.
677 When MENU is a keymap or a list of keymaps, the return value
678 is a list of events.
679
680 Alternatively, you can specify a menu of multiple panes
681 with a list of the form (TITLE PANE1 PANE2...),
682 where each pane is a list of form (TITLE ITEM1 ITEM2...).
683 Each ITEM is normally a cons cell (STRING . VALUE);
684 but a string can appear as an item--that makes a nonselectable line
685 in the menu.
686 With this form of menu, the return value is VALUE from the chosen item.
687
688 If POSITION is nil, don't display the menu at all, just precalculate the
689 cached information about equivalent key sequences. */)
690 (position, menu)
691 Lisp_Object position, menu;
692 {
693 Lisp_Object keymap, tem;
694 int xpos = 0, ypos = 0;
695 Lisp_Object title;
696 char *error_name;
697 Lisp_Object selection;
698 FRAME_PTR f = NULL;
699 Lisp_Object x, y, window;
700 int keymaps = 0;
701 int for_click = 0;
702 struct gcpro gcpro1;
703
704 #ifdef HAVE_MENUS
705 if (! NILP (position))
706 {
707 check_x ();
708
709 /* Decode the first argument: find the window and the coordinates. */
710 if (EQ (position, Qt)
711 || (CONSP (position) && (EQ (XCAR (position), Qmenu_bar)
712 || EQ (XCAR (position), Qtool_bar))))
713 {
714 /* Use the mouse's current position. */
715 FRAME_PTR new_f = SELECTED_FRAME ();
716 Lisp_Object bar_window;
717 enum scroll_bar_part part;
718 unsigned long time;
719
720 if (mouse_position_hook)
721 (*mouse_position_hook) (&new_f, 1, &bar_window,
722 &part, &x, &y, &time);
723 if (new_f != 0)
724 XSETFRAME (window, new_f);
725 else
726 {
727 window = selected_window;
728 XSETFASTINT (x, 0);
729 XSETFASTINT (y, 0);
730 }
731 }
732 else
733 {
734 tem = Fcar (position);
735 if (CONSP (tem))
736 {
737 window = Fcar (Fcdr (position));
738 x = Fcar (tem);
739 y = Fcar (Fcdr (tem));
740 }
741 else
742 {
743 for_click = 1;
744 tem = Fcar (Fcdr (position)); /* EVENT_START (position) */
745 window = Fcar (tem); /* POSN_WINDOW (tem) */
746 tem = Fcar (Fcdr (Fcdr (tem))); /* POSN_WINDOW_POSN (tem) */
747 x = Fcar (tem);
748 y = Fcdr (tem);
749 }
750 }
751
752 CHECK_NUMBER (x);
753 CHECK_NUMBER (y);
754
755 /* Decode where to put the menu. */
756
757 if (FRAMEP (window))
758 {
759 f = XFRAME (window);
760 xpos = 0;
761 ypos = 0;
762 }
763 else if (WINDOWP (window))
764 {
765 CHECK_LIVE_WINDOW (window);
766 f = XFRAME (WINDOW_FRAME (XWINDOW (window)));
767
768 xpos = (FONT_WIDTH (FRAME_FONT (f))
769 * XFASTINT (XWINDOW (window)->left));
770 ypos = (FRAME_LINE_HEIGHT (f)
771 * XFASTINT (XWINDOW (window)->top));
772 }
773 else
774 /* ??? Not really clean; should be CHECK_WINDOW_OR_FRAME,
775 but I don't want to make one now. */
776 CHECK_WINDOW (window);
777
778 xpos += XINT (x);
779 ypos += XINT (y);
780
781 XSETFRAME (Vmenu_updating_frame, f);
782 }
783 Vmenu_updating_frame = Qnil;
784 #endif /* HAVE_MENUS */
785
786 title = Qnil;
787 GCPRO1 (title);
788
789 /* Decode the menu items from what was specified. */
790
791 keymap = get_keymap (menu, 0, 0);
792 if (CONSP (keymap))
793 {
794 /* We were given a keymap. Extract menu info from the keymap. */
795 Lisp_Object prompt;
796
797 /* Extract the detailed info to make one pane. */
798 keymap_panes (&menu, 1, NILP (position));
799
800 /* Search for a string appearing directly as an element of the keymap.
801 That string is the title of the menu. */
802 prompt = Fkeymap_prompt (keymap);
803 if (NILP (title) && !NILP (prompt))
804 title = prompt;
805
806 /* Make that be the pane title of the first pane. */
807 if (!NILP (prompt) && menu_items_n_panes >= 0)
808 XVECTOR (menu_items)->contents[MENU_ITEMS_PANE_NAME] = prompt;
809
810 keymaps = 1;
811 }
812 else if (CONSP (menu) && KEYMAPP (XCAR (menu)))
813 {
814 /* We were given a list of keymaps. */
815 int nmaps = XFASTINT (Flength (menu));
816 Lisp_Object *maps
817 = (Lisp_Object *) alloca (nmaps * sizeof (Lisp_Object));
818 int i;
819
820 title = Qnil;
821
822 /* The first keymap that has a prompt string
823 supplies the menu title. */
824 for (tem = menu, i = 0; CONSP (tem); tem = Fcdr (tem))
825 {
826 Lisp_Object prompt;
827
828 maps[i++] = keymap = get_keymap (Fcar (tem), 1, 0);
829
830 prompt = Fkeymap_prompt (keymap);
831 if (NILP (title) && !NILP (prompt))
832 title = prompt;
833 }
834
835 /* Extract the detailed info to make one pane. */
836 keymap_panes (maps, nmaps, NILP (position));
837
838 /* Make the title be the pane title of the first pane. */
839 if (!NILP (title) && menu_items_n_panes >= 0)
840 XVECTOR (menu_items)->contents[MENU_ITEMS_PANE_NAME] = title;
841
842 keymaps = 1;
843 }
844 else
845 {
846 /* We were given an old-fashioned menu. */
847 title = Fcar (menu);
848 CHECK_STRING (title);
849
850 list_of_panes (Fcdr (menu));
851
852 keymaps = 0;
853 }
854
855 if (NILP (position))
856 {
857 discard_menu_items ();
858 UNGCPRO;
859 return Qnil;
860 }
861
862 #ifdef HAVE_MENUS
863 /* Display them in a menu. */
864 BLOCK_INPUT;
865
866 selection = xmenu_show (f, xpos, ypos, for_click,
867 keymaps, title, &error_name);
868 UNBLOCK_INPUT;
869
870 discard_menu_items ();
871
872 UNGCPRO;
873 #endif /* HAVE_MENUS */
874
875 if (error_name) error (error_name);
876 return selection;
877 }
878
879 #ifdef HAVE_MENUS
880
881 DEFUN ("x-popup-dialog", Fx_popup_dialog, Sx_popup_dialog, 2, 2, 0,
882 doc: /* Pop up a dialog box and return user's selection.
883 POSITION specifies which frame to use.
884 This is normally a mouse button event or a window or frame.
885 If POSITION is t, it means to use the frame the mouse is on.
886 The dialog box appears in the middle of the specified frame.
887
888 CONTENTS specifies the alternatives to display in the dialog box.
889 It is a list of the form (TITLE ITEM1 ITEM2...).
890 Each ITEM is a cons cell (STRING . VALUE).
891 The return value is VALUE from the chosen item.
892
893 An ITEM may also be just a string--that makes a nonselectable item.
894 An ITEM may also be nil--that means to put all preceding items
895 on the left of the dialog box and all following items on the right.
896 \(By default, approximately half appear on each side.) */)
897 (position, contents)
898 Lisp_Object position, contents;
899 {
900 FRAME_PTR f = NULL;
901 Lisp_Object window;
902
903 check_x ();
904
905 /* Decode the first argument: find the window or frame to use. */
906 if (EQ (position, Qt)
907 || (CONSP (position) && (EQ (XCAR (position), Qmenu_bar)
908 || EQ (XCAR (position), Qtool_bar))))
909 {
910 #if 0 /* Using the frame the mouse is on may not be right. */
911 /* Use the mouse's current position. */
912 FRAME_PTR new_f = SELECTED_FRAME ();
913 Lisp_Object bar_window;
914 enum scroll_bar_part part;
915 unsigned long time;
916 Lisp_Object x, y;
917
918 (*mouse_position_hook) (&new_f, 1, &bar_window, &part, &x, &y, &time);
919
920 if (new_f != 0)
921 XSETFRAME (window, new_f);
922 else
923 window = selected_window;
924 #endif
925 window = selected_window;
926 }
927 else if (CONSP (position))
928 {
929 Lisp_Object tem;
930 tem = Fcar (position);
931 if (CONSP (tem))
932 window = Fcar (Fcdr (position));
933 else
934 {
935 tem = Fcar (Fcdr (position)); /* EVENT_START (position) */
936 window = Fcar (tem); /* POSN_WINDOW (tem) */
937 }
938 }
939 else if (WINDOWP (position) || FRAMEP (position))
940 window = position;
941 else
942 window = Qnil;
943
944 /* Decode where to put the menu. */
945
946 if (FRAMEP (window))
947 f = XFRAME (window);
948 else if (WINDOWP (window))
949 {
950 CHECK_LIVE_WINDOW (window);
951 f = XFRAME (WINDOW_FRAME (XWINDOW (window)));
952 }
953 else
954 /* ??? Not really clean; should be CHECK_WINDOW_OR_FRAME,
955 but I don't want to make one now. */
956 CHECK_WINDOW (window);
957
958 #ifndef USE_X_TOOLKIT
959 /* Display a menu with these alternatives
960 in the middle of frame F. */
961 {
962 Lisp_Object x, y, frame, newpos;
963 XSETFRAME (frame, f);
964 XSETINT (x, x_pixel_width (f) / 2);
965 XSETINT (y, x_pixel_height (f) / 2);
966 newpos = Fcons (Fcons (x, Fcons (y, Qnil)), Fcons (frame, Qnil));
967
968 return Fx_popup_menu (newpos,
969 Fcons (Fcar (contents), Fcons (contents, Qnil)));
970 }
971 #else
972 {
973 Lisp_Object title;
974 char *error_name;
975 Lisp_Object selection;
976
977 /* Decode the dialog items from what was specified. */
978 title = Fcar (contents);
979 CHECK_STRING (title);
980
981 list_of_panes (Fcons (contents, Qnil));
982
983 /* Display them in a dialog box. */
984 BLOCK_INPUT;
985 selection = xdialog_show (f, 0, title, &error_name);
986 UNBLOCK_INPUT;
987
988 discard_menu_items ();
989
990 if (error_name) error (error_name);
991 return selection;
992 }
993 #endif
994 }
995 \f
996 #ifdef USE_X_TOOLKIT
997
998 /* Loop in Xt until the menu pulldown or dialog popup has been
999 popped down (deactivated). This is used for x-popup-menu
1000 and x-popup-dialog; it is not used for the menu bar any more.
1001
1002 NOTE: All calls to popup_get_selection should be protected
1003 with BLOCK_INPUT, UNBLOCK_INPUT wrappers. */
1004
1005 void
1006 popup_get_selection (initial_event, dpyinfo, id)
1007 XEvent *initial_event;
1008 struct x_display_info *dpyinfo;
1009 LWLIB_ID id;
1010 {
1011 XEvent event;
1012
1013 /* Define a queue to save up for later unreading
1014 all X events that don't pertain to the menu. */
1015 struct event_queue
1016 {
1017 XEvent event;
1018 struct event_queue *next;
1019 };
1020
1021 struct event_queue *queue = NULL;
1022 struct event_queue *queue_tmp;
1023
1024 if (initial_event)
1025 event = *initial_event;
1026 else
1027 XtAppNextEvent (Xt_app_con, &event);
1028
1029 while (1)
1030 {
1031 /* Handle expose events for editor frames right away. */
1032 if (event.type == Expose)
1033 process_expose_from_menu (event);
1034 /* Make sure we don't consider buttons grabbed after menu goes.
1035 And make sure to deactivate for any ButtonRelease,
1036 even if XtDispatchEvent doesn't do that. */
1037 else if (event.type == ButtonRelease
1038 && dpyinfo->display == event.xbutton.display)
1039 {
1040 dpyinfo->grabbed &= ~(1 << event.xbutton.button);
1041 popup_activated_flag = 0;
1042 #ifdef USE_MOTIF /* Pretending that the event came from a
1043 Btn1Down seems the only way to convince Motif to
1044 activate its callbacks; setting the XmNmenuPost
1045 isn't working. --marcus@sysc.pdx.edu. */
1046 event.xbutton.button = 1;
1047 #endif
1048 }
1049 /* If the user presses a key, deactivate the menu.
1050 The user is likely to do that if we get wedged. */
1051 else if (event.type == KeyPress
1052 && dpyinfo->display == event.xbutton.display)
1053 {
1054 KeySym keysym = XLookupKeysym (&event.xkey, 0);
1055 if (!IsModifierKey (keysym))
1056 {
1057 popup_activated_flag = 0;
1058 break;
1059 }
1060 }
1061 /* Button presses outside the menu also pop it down. */
1062 else if (event.type == ButtonPress
1063 && event.xany.display == dpyinfo->display
1064 && x_any_window_to_frame (dpyinfo, event.xany.window))
1065 {
1066 popup_activated_flag = 0;
1067 break;
1068 }
1069
1070 /* Queue all events not for this popup,
1071 except for Expose, which we've already handled, and ButtonRelease.
1072 Note that the X window is associated with the frame if this
1073 is a menu bar popup, but not if it's a dialog box. So we use
1074 x_non_menubar_window_to_frame, not x_any_window_to_frame. */
1075 if (event.type != Expose
1076 && !(event.type == ButtonRelease
1077 && dpyinfo->display == event.xbutton.display)
1078 && (event.xany.display != dpyinfo->display
1079 || x_non_menubar_window_to_frame (dpyinfo, event.xany.window)))
1080 {
1081 queue_tmp = (struct event_queue *) xmalloc (sizeof *queue_tmp);
1082 queue_tmp->event = event;
1083 queue_tmp->next = queue;
1084 queue = queue_tmp;
1085 }
1086 else
1087 XtDispatchEvent (&event);
1088
1089 if (!popup_activated ())
1090 break;
1091 XtAppNextEvent (Xt_app_con, &event);
1092 }
1093
1094 /* Unread any events that we got but did not handle. */
1095 while (queue != NULL)
1096 {
1097 queue_tmp = queue;
1098 XPutBackEvent (queue_tmp->event.xany.display, &queue_tmp->event);
1099 queue = queue_tmp->next;
1100 xfree ((char *)queue_tmp);
1101 /* Cause these events to get read as soon as we UNBLOCK_INPUT. */
1102 interrupt_input_pending = 1;
1103 }
1104 }
1105
1106 /* Activate the menu bar of frame F.
1107 This is called from keyboard.c when it gets the
1108 MENU_BAR_ACTIVATE_EVENT out of the Emacs event queue.
1109
1110 To activate the menu bar, we use the X button-press event
1111 that was saved in saved_menu_event.
1112 That makes the toolkit do its thing.
1113
1114 But first we recompute the menu bar contents (the whole tree).
1115
1116 The reason for saving the button event until here, instead of
1117 passing it to the toolkit right away, is that we can safely
1118 execute Lisp code. */
1119
1120 void
1121 x_activate_menubar (f)
1122 FRAME_PTR f;
1123 {
1124 if (!f->output_data.x->saved_menu_event->type)
1125 return;
1126
1127 set_frame_menubar (f, 0, 1);
1128 BLOCK_INPUT;
1129 XtDispatchEvent ((XEvent *) f->output_data.x->saved_menu_event);
1130 UNBLOCK_INPUT;
1131 #ifdef USE_MOTIF
1132 if (f->output_data.x->saved_menu_event->type == ButtonRelease)
1133 pending_menu_activation = 1;
1134 #endif
1135
1136 /* Ignore this if we get it a second time. */
1137 f->output_data.x->saved_menu_event->type = 0;
1138 }
1139
1140 /* Detect if a dialog or menu has been posted. */
1141
1142 int
1143 popup_activated ()
1144 {
1145 return popup_activated_flag;
1146 }
1147
1148 /* This callback is invoked when the user selects a menubar cascade
1149 pushbutton, but before the pulldown menu is posted. */
1150
1151 static void
1152 popup_activate_callback (widget, id, client_data)
1153 Widget widget;
1154 LWLIB_ID id;
1155 XtPointer client_data;
1156 {
1157 popup_activated_flag = 1;
1158 }
1159
1160 /* This callback is invoked when a dialog or menu is finished being
1161 used and has been unposted. */
1162
1163 static void
1164 popup_deactivate_callback (widget, id, client_data)
1165 Widget widget;
1166 LWLIB_ID id;
1167 XtPointer client_data;
1168 {
1169 popup_activated_flag = 0;
1170 }
1171
1172 /* Lwlib callback called when menu items are highlighted/unhighlighted
1173 while moving the mouse over them. WIDGET is the menu bar or menu
1174 popup widget. ID is its LWLIB_ID. CALL_DATA contains a pointer to
1175 the widget_value structure for the menu item, or null in case of
1176 unhighlighting. */
1177
1178 void
1179 menu_highlight_callback (widget, id, call_data)
1180 Widget widget;
1181 LWLIB_ID id;
1182 void *call_data;
1183 {
1184 widget_value *wv = (widget_value *) call_data;
1185 struct frame *f;
1186 Lisp_Object frame, help;
1187
1188 help = wv ? wv->help : Qnil;
1189
1190 /* Determine the frame for the help event. */
1191 f = menubar_id_to_frame (id);
1192 if (f)
1193 {
1194 XSETFRAME (frame, f);
1195 kbd_buffer_store_help_event (frame, help);
1196 }
1197 else
1198 {
1199 /* WIDGET is the popup menu. It's parent is the frame's
1200 widget. See which frame that is. */
1201 Widget frame_widget = XtParent (widget);
1202 Lisp_Object tail;
1203
1204 for (tail = Vframe_list; GC_CONSP (tail); tail = XCDR (tail))
1205 {
1206 frame = XCAR (tail);
1207 if (GC_FRAMEP (frame)
1208 && (f = XFRAME (frame),
1209 FRAME_X_P (f) && f->output_data.x->widget == frame_widget))
1210 break;
1211 }
1212
1213 show_help_echo (help, Qnil, Qnil, Qnil, 1);
1214 }
1215 }
1216
1217 /* This callback is called from the menu bar pulldown menu
1218 when the user makes a selection.
1219 Figure out what the user chose
1220 and put the appropriate events into the keyboard buffer. */
1221
1222 static void
1223 menubar_selection_callback (widget, id, client_data)
1224 Widget widget;
1225 LWLIB_ID id;
1226 XtPointer client_data;
1227 {
1228 Lisp_Object prefix, entry;
1229 FRAME_PTR f = menubar_id_to_frame (id);
1230 Lisp_Object vector;
1231 Lisp_Object *subprefix_stack;
1232 int submenu_depth = 0;
1233 int i;
1234
1235 if (!f)
1236 return;
1237 entry = Qnil;
1238 subprefix_stack = (Lisp_Object *) alloca (f->menu_bar_items_used * sizeof (Lisp_Object));
1239 vector = f->menu_bar_vector;
1240 prefix = Qnil;
1241 i = 0;
1242 while (i < f->menu_bar_items_used)
1243 {
1244 if (EQ (XVECTOR (vector)->contents[i], Qnil))
1245 {
1246 subprefix_stack[submenu_depth++] = prefix;
1247 prefix = entry;
1248 i++;
1249 }
1250 else if (EQ (XVECTOR (vector)->contents[i], Qlambda))
1251 {
1252 prefix = subprefix_stack[--submenu_depth];
1253 i++;
1254 }
1255 else if (EQ (XVECTOR (vector)->contents[i], Qt))
1256 {
1257 prefix = XVECTOR (vector)->contents[i + MENU_ITEMS_PANE_PREFIX];
1258 i += MENU_ITEMS_PANE_LENGTH;
1259 }
1260 else
1261 {
1262 entry = XVECTOR (vector)->contents[i + MENU_ITEMS_ITEM_VALUE];
1263 /* The EMACS_INT cast avoids a warning. There's no problem
1264 as long as pointers have enough bits to hold small integers. */
1265 if ((int) (EMACS_INT) client_data == i)
1266 {
1267 int j;
1268 struct input_event buf;
1269 Lisp_Object frame;
1270
1271 XSETFRAME (frame, f);
1272 buf.kind = MENU_BAR_EVENT;
1273 buf.frame_or_window = frame;
1274 buf.arg = frame;
1275 kbd_buffer_store_event (&buf);
1276
1277 for (j = 0; j < submenu_depth; j++)
1278 if (!NILP (subprefix_stack[j]))
1279 {
1280 buf.kind = MENU_BAR_EVENT;
1281 buf.frame_or_window = frame;
1282 buf.arg = subprefix_stack[j];
1283 kbd_buffer_store_event (&buf);
1284 }
1285
1286 if (!NILP (prefix))
1287 {
1288 buf.kind = MENU_BAR_EVENT;
1289 buf.frame_or_window = frame;
1290 buf.arg = prefix;
1291 kbd_buffer_store_event (&buf);
1292 }
1293
1294 buf.kind = MENU_BAR_EVENT;
1295 buf.frame_or_window = frame;
1296 buf.arg = entry;
1297 kbd_buffer_store_event (&buf);
1298
1299 return;
1300 }
1301 i += MENU_ITEMS_ITEM_LENGTH;
1302 }
1303 }
1304 }
1305
1306 /* Allocate a widget_value, blocking input. */
1307
1308 widget_value *
1309 xmalloc_widget_value ()
1310 {
1311 widget_value *value;
1312
1313 BLOCK_INPUT;
1314 value = malloc_widget_value ();
1315 UNBLOCK_INPUT;
1316
1317 return value;
1318 }
1319
1320 /* This recursively calls free_widget_value on the tree of widgets.
1321 It must free all data that was malloc'ed for these widget_values.
1322 In Emacs, many slots are pointers into the data of Lisp_Strings, and
1323 must be left alone. */
1324
1325 void
1326 free_menubar_widget_value_tree (wv)
1327 widget_value *wv;
1328 {
1329 if (! wv) return;
1330
1331 wv->name = wv->value = wv->key = (char *) 0xDEADBEEF;
1332
1333 if (wv->contents && (wv->contents != (widget_value*)1))
1334 {
1335 free_menubar_widget_value_tree (wv->contents);
1336 wv->contents = (widget_value *) 0xDEADBEEF;
1337 }
1338 if (wv->next)
1339 {
1340 free_menubar_widget_value_tree (wv->next);
1341 wv->next = (widget_value *) 0xDEADBEEF;
1342 }
1343 BLOCK_INPUT;
1344 free_widget_value (wv);
1345 UNBLOCK_INPUT;
1346 }
1347 \f
1348 /* Set up data i menu_items for a menu bar item
1349 whose event type is ITEM_KEY (with string ITEM_NAME)
1350 and whose contents come from the list of keymaps MAPS. */
1351
1352 static int
1353 parse_single_submenu (item_key, item_name, maps)
1354 Lisp_Object item_key, item_name, maps;
1355 {
1356 Lisp_Object length;
1357 int len;
1358 Lisp_Object *mapvec;
1359 int i;
1360 int top_level_items = 0;
1361
1362 length = Flength (maps);
1363 len = XINT (length);
1364
1365 /* Convert the list MAPS into a vector MAPVEC. */
1366 mapvec = (Lisp_Object *) alloca (len * sizeof (Lisp_Object));
1367 for (i = 0; i < len; i++)
1368 {
1369 mapvec[i] = Fcar (maps);
1370 maps = Fcdr (maps);
1371 }
1372
1373 /* Loop over the given keymaps, making a pane for each map.
1374 But don't make a pane that is empty--ignore that map instead. */
1375 for (i = 0; i < len; i++)
1376 {
1377 if (SYMBOLP (mapvec[i])
1378 || (CONSP (mapvec[i]) && !KEYMAPP (mapvec[i])))
1379 {
1380 /* Here we have a command at top level in the menu bar
1381 as opposed to a submenu. */
1382 top_level_items = 1;
1383 push_menu_pane (Qnil, Qnil);
1384 push_menu_item (item_name, Qt, item_key, mapvec[i],
1385 Qnil, Qnil, Qnil, Qnil);
1386 }
1387 else
1388 single_keymap_panes (mapvec[i], item_name, item_key, 0, 10);
1389 }
1390
1391 return top_level_items;
1392 }
1393
1394 /* Create a tree of widget_value objects
1395 representing the panes and items
1396 in menu_items starting at index START, up to index END. */
1397
1398 static widget_value *
1399 digest_single_submenu (start, end, top_level_items)
1400 int start, end;
1401 {
1402 widget_value *wv, *prev_wv, *save_wv, *first_wv;
1403 int i;
1404 int submenu_depth = 0;
1405 widget_value **submenu_stack;
1406
1407 submenu_stack
1408 = (widget_value **) alloca (menu_items_used * sizeof (widget_value *));
1409 wv = xmalloc_widget_value ();
1410 wv->name = "menu";
1411 wv->value = 0;
1412 wv->enabled = 1;
1413 wv->button_type = BUTTON_TYPE_NONE;
1414 wv->help = Qnil;
1415 first_wv = wv;
1416 save_wv = 0;
1417 prev_wv = 0;
1418
1419 /* Loop over all panes and items made during this call
1420 and construct a tree of widget_value objects.
1421 Ignore the panes and items made by previous calls to
1422 single_submenu, even though those are also in menu_items. */
1423 i = start;
1424 while (i < end)
1425 {
1426 if (EQ (XVECTOR (menu_items)->contents[i], Qnil))
1427 {
1428 submenu_stack[submenu_depth++] = save_wv;
1429 save_wv = prev_wv;
1430 prev_wv = 0;
1431 i++;
1432 }
1433 else if (EQ (XVECTOR (menu_items)->contents[i], Qlambda))
1434 {
1435 prev_wv = save_wv;
1436 save_wv = submenu_stack[--submenu_depth];
1437 i++;
1438 }
1439 else if (EQ (XVECTOR (menu_items)->contents[i], Qt)
1440 && submenu_depth != 0)
1441 i += MENU_ITEMS_PANE_LENGTH;
1442 /* Ignore a nil in the item list.
1443 It's meaningful only for dialog boxes. */
1444 else if (EQ (XVECTOR (menu_items)->contents[i], Qquote))
1445 i += 1;
1446 else if (EQ (XVECTOR (menu_items)->contents[i], Qt))
1447 {
1448 /* Create a new pane. */
1449 Lisp_Object pane_name, prefix;
1450 char *pane_string;
1451
1452 pane_name = XVECTOR (menu_items)->contents[i + MENU_ITEMS_PANE_NAME];
1453 prefix = XVECTOR (menu_items)->contents[i + MENU_ITEMS_PANE_PREFIX];
1454
1455 #ifndef HAVE_MULTILINGUAL_MENU
1456 if (STRINGP (pane_name) && STRING_MULTIBYTE (pane_name))
1457 {
1458 pane_name = ENCODE_SYSTEM (pane_name);
1459 AREF (menu_items, i + MENU_ITEMS_PANE_NAME) = pane_name;
1460 }
1461 #endif
1462 pane_string = (NILP (pane_name)
1463 ? "" : (char *) SDATA (pane_name));
1464 /* If there is just one top-level pane, put all its items directly
1465 under the top-level menu. */
1466 if (menu_items_n_panes == 1)
1467 pane_string = "";
1468
1469 /* If the pane has a meaningful name,
1470 make the pane a top-level menu item
1471 with its items as a submenu beneath it. */
1472 if (strcmp (pane_string, ""))
1473 {
1474 wv = xmalloc_widget_value ();
1475 if (save_wv)
1476 save_wv->next = wv;
1477 else
1478 first_wv->contents = wv;
1479 wv->name = pane_string;
1480 /* Ignore the @ that means "separate pane".
1481 This is a kludge, but this isn't worth more time. */
1482 if (!NILP (prefix) && wv->name[0] == '@')
1483 wv->name++;
1484 wv->value = 0;
1485 wv->enabled = 1;
1486 wv->button_type = BUTTON_TYPE_NONE;
1487 wv->help = Qnil;
1488 }
1489 save_wv = wv;
1490 prev_wv = 0;
1491 i += MENU_ITEMS_PANE_LENGTH;
1492 }
1493 else
1494 {
1495 /* Create a new item within current pane. */
1496 Lisp_Object item_name, enable, descrip, def, type, selected;
1497 Lisp_Object help;
1498
1499 item_name = AREF (menu_items, i + MENU_ITEMS_ITEM_NAME);
1500 enable = AREF (menu_items, i + MENU_ITEMS_ITEM_ENABLE);
1501 descrip = AREF (menu_items, i + MENU_ITEMS_ITEM_EQUIV_KEY);
1502 def = AREF (menu_items, i + MENU_ITEMS_ITEM_DEFINITION);
1503 type = AREF (menu_items, i + MENU_ITEMS_ITEM_TYPE);
1504 selected = AREF (menu_items, i + MENU_ITEMS_ITEM_SELECTED);
1505 help = AREF (menu_items, i + MENU_ITEMS_ITEM_HELP);
1506
1507 #ifndef HAVE_MULTILINGUAL_MENU
1508 if (STRING_MULTIBYTE (item_name))
1509 {
1510 item_name = ENCODE_SYSTEM (item_name);
1511 AREF (menu_items, i + MENU_ITEMS_ITEM_NAME) = item_name;
1512 }
1513
1514 if (STRINGP (descrip) && STRING_MULTIBYTE (descrip))
1515 {
1516 descrip = ENCODE_SYSTEM (descrip);
1517 AREF (menu_items, i + MENU_ITEMS_ITEM_EQUIV_KEY) = descrip;
1518 }
1519 #endif /* not HAVE_MULTILINGUAL_MENU */
1520
1521 wv = xmalloc_widget_value ();
1522 if (prev_wv)
1523 prev_wv->next = wv;
1524 else
1525 save_wv->contents = wv;
1526
1527 wv->name = (char *) SDATA (item_name);
1528 if (!NILP (descrip))
1529 wv->key = (char *) SDATA (descrip);
1530 wv->value = 0;
1531 /* The EMACS_INT cast avoids a warning. There's no problem
1532 as long as pointers have enough bits to hold small integers. */
1533 wv->call_data = (!NILP (def) ? (void *) (EMACS_INT) i : 0);
1534 wv->enabled = !NILP (enable);
1535
1536 if (NILP (type))
1537 wv->button_type = BUTTON_TYPE_NONE;
1538 else if (EQ (type, QCradio))
1539 wv->button_type = BUTTON_TYPE_RADIO;
1540 else if (EQ (type, QCtoggle))
1541 wv->button_type = BUTTON_TYPE_TOGGLE;
1542 else
1543 abort ();
1544
1545 wv->selected = !NILP (selected);
1546 if (! STRINGP (help))
1547 help = Qnil;
1548
1549 wv->help = help;
1550
1551 prev_wv = wv;
1552
1553 i += MENU_ITEMS_ITEM_LENGTH;
1554 }
1555 }
1556
1557 /* If we have just one "menu item"
1558 that was originally a button, return it by itself. */
1559 if (top_level_items && first_wv->contents && first_wv->contents->next == 0)
1560 {
1561 wv = first_wv->contents;
1562 free_widget_value (first_wv);
1563 return wv;
1564 }
1565
1566 return first_wv;
1567 }
1568 \f
1569 /* Recompute all the widgets of frame F, when the menu bar has been
1570 changed. Value is non-zero if widgets were updated. */
1571
1572 static int
1573 update_frame_menubar (f)
1574 FRAME_PTR f;
1575 {
1576 struct x_output *x = f->output_data.x;
1577 int columns, rows;
1578
1579 if (!x->menubar_widget || XtIsManaged (x->menubar_widget))
1580 return 0;
1581
1582 BLOCK_INPUT;
1583 /* Save the size of the frame because the pane widget doesn't accept
1584 to resize itself. So force it. */
1585 columns = f->width;
1586 rows = f->height;
1587
1588 /* Do the voodoo which means "I'm changing lots of things, don't try
1589 to refigure sizes until I'm done." */
1590 lw_refigure_widget (x->column_widget, False);
1591
1592 /* The order in which children are managed is the top to bottom
1593 order in which they are displayed in the paned window. First,
1594 remove the text-area widget. */
1595 XtUnmanageChild (x->edit_widget);
1596
1597 /* Remove the menubar that is there now, and put up the menubar that
1598 should be there. */
1599 XtManageChild (x->menubar_widget);
1600 XtMapWidget (x->menubar_widget);
1601 XtVaSetValues (x->menubar_widget, XtNmappedWhenManaged, 1, NULL);
1602
1603 /* Re-manage the text-area widget, and then thrash the sizes. */
1604 XtManageChild (x->edit_widget);
1605 lw_refigure_widget (x->column_widget, True);
1606
1607 /* Force the pane widget to resize itself with the right values. */
1608 EmacsFrameSetCharSize (x->edit_widget, columns, rows);
1609 UNBLOCK_INPUT;
1610 return 1;
1611 }
1612
1613 /* Set the contents of the menubar widgets of frame F.
1614 The argument FIRST_TIME is currently ignored;
1615 it is set the first time this is called, from initialize_frame_menubar. */
1616
1617 void
1618 set_frame_menubar (f, first_time, deep_p)
1619 FRAME_PTR f;
1620 int first_time;
1621 int deep_p;
1622 {
1623 Widget menubar_widget = f->output_data.x->menubar_widget;
1624 Lisp_Object items;
1625 widget_value *wv, *first_wv, *prev_wv = 0;
1626 int i, last_i;
1627 int *submenu_start, *submenu_end;
1628 int *submenu_top_level_items;
1629
1630 LWLIB_ID id;
1631
1632 XSETFRAME (Vmenu_updating_frame, f);
1633
1634 if (f->output_data.x->id == 0)
1635 f->output_data.x->id = next_menubar_widget_id++;
1636 id = f->output_data.x->id;
1637
1638 if (! menubar_widget)
1639 deep_p = 1;
1640 else if (pending_menu_activation && !deep_p)
1641 deep_p = 1;
1642 /* Make the first call for any given frame always go deep. */
1643 else if (!f->output_data.x->saved_menu_event && !deep_p)
1644 {
1645 deep_p = 1;
1646 f->output_data.x->saved_menu_event = (XEvent*)xmalloc (sizeof (XEvent));
1647 f->output_data.x->saved_menu_event->type = 0;
1648 }
1649
1650 if (deep_p)
1651 {
1652 /* Make a widget-value tree representing the entire menu trees. */
1653
1654 struct buffer *prev = current_buffer;
1655 Lisp_Object buffer;
1656 int specpdl_count = SPECPDL_INDEX ();
1657 int previous_menu_items_used = f->menu_bar_items_used;
1658 Lisp_Object *previous_items
1659 = (Lisp_Object *) alloca (previous_menu_items_used
1660 * sizeof (Lisp_Object));
1661
1662 /* If we are making a new widget, its contents are empty,
1663 do always reinitialize them. */
1664 if (! menubar_widget)
1665 previous_menu_items_used = 0;
1666
1667 buffer = XWINDOW (FRAME_SELECTED_WINDOW (f))->buffer;
1668 specbind (Qinhibit_quit, Qt);
1669 /* Don't let the debugger step into this code
1670 because it is not reentrant. */
1671 specbind (Qdebug_on_next_call, Qnil);
1672
1673 record_unwind_protect (Fset_match_data, Fmatch_data (Qnil, Qnil));
1674 if (NILP (Voverriding_local_map_menu_flag))
1675 {
1676 specbind (Qoverriding_terminal_local_map, Qnil);
1677 specbind (Qoverriding_local_map, Qnil);
1678 }
1679
1680 set_buffer_internal_1 (XBUFFER (buffer));
1681
1682 /* Run the Lucid hook. */
1683 safe_run_hooks (Qactivate_menubar_hook);
1684
1685 /* If it has changed current-menubar from previous value,
1686 really recompute the menubar from the value. */
1687 if (! NILP (Vlucid_menu_bar_dirty_flag))
1688 call0 (Qrecompute_lucid_menubar);
1689 safe_run_hooks (Qmenu_bar_update_hook);
1690 FRAME_MENU_BAR_ITEMS (f) = menu_bar_items (FRAME_MENU_BAR_ITEMS (f));
1691
1692 items = FRAME_MENU_BAR_ITEMS (f);
1693
1694 /* Save the frame's previous menu bar contents data. */
1695 if (previous_menu_items_used)
1696 bcopy (XVECTOR (f->menu_bar_vector)->contents, previous_items,
1697 previous_menu_items_used * sizeof (Lisp_Object));
1698
1699 /* Fill in menu_items with the current menu bar contents.
1700 This can evaluate Lisp code. */
1701 menu_items = f->menu_bar_vector;
1702 menu_items_allocated = VECTORP (menu_items) ? ASIZE (menu_items) : 0;
1703 submenu_start = (int *) alloca (XVECTOR (items)->size * sizeof (int *));
1704 submenu_end = (int *) alloca (XVECTOR (items)->size * sizeof (int *));
1705 submenu_top_level_items
1706 = (int *) alloca (XVECTOR (items)->size * sizeof (int *));
1707 init_menu_items ();
1708 for (i = 0; i < XVECTOR (items)->size; i += 4)
1709 {
1710 Lisp_Object key, string, maps;
1711
1712 last_i = i;
1713
1714 key = XVECTOR (items)->contents[i];
1715 string = XVECTOR (items)->contents[i + 1];
1716 maps = XVECTOR (items)->contents[i + 2];
1717 if (NILP (string))
1718 break;
1719
1720 submenu_start[i] = menu_items_used;
1721
1722 menu_items_n_panes = 0;
1723 submenu_top_level_items[i]
1724 = parse_single_submenu (key, string, maps);
1725
1726 submenu_end[i] = menu_items_used;
1727 }
1728
1729 finish_menu_items ();
1730
1731 /* Convert menu_items into widget_value trees
1732 to display the menu. This cannot evaluate Lisp code. */
1733
1734 wv = xmalloc_widget_value ();
1735 wv->name = "menubar";
1736 wv->value = 0;
1737 wv->enabled = 1;
1738 wv->button_type = BUTTON_TYPE_NONE;
1739 wv->help = Qnil;
1740 first_wv = wv;
1741
1742 for (i = 0; i < last_i; i += 4)
1743 {
1744 wv = digest_single_submenu (submenu_start[i], submenu_end[i],
1745 submenu_top_level_items[i]);
1746 if (prev_wv)
1747 prev_wv->next = wv;
1748 else
1749 first_wv->contents = wv;
1750 /* Don't set wv->name here; GC during the loop might relocate it. */
1751 wv->enabled = 1;
1752 wv->button_type = BUTTON_TYPE_NONE;
1753 prev_wv = wv;
1754 }
1755
1756 set_buffer_internal_1 (prev);
1757 unbind_to (specpdl_count, Qnil);
1758
1759 /* If there has been no change in the Lisp-level contents
1760 of the menu bar, skip redisplaying it. Just exit. */
1761
1762 for (i = 0; i < previous_menu_items_used; i++)
1763 if (menu_items_used == i
1764 || (!EQ (previous_items[i], XVECTOR (menu_items)->contents[i])))
1765 break;
1766 if (i == menu_items_used && i == previous_menu_items_used && i != 0)
1767 {
1768 free_menubar_widget_value_tree (first_wv);
1769 menu_items = Qnil;
1770
1771 return;
1772 }
1773
1774 /* Now GC cannot happen during the lifetime of the widget_value,
1775 so it's safe to store data from a Lisp_String. */
1776 wv = first_wv->contents;
1777 for (i = 0; i < XVECTOR (items)->size; i += 4)
1778 {
1779 Lisp_Object string;
1780 string = XVECTOR (items)->contents[i + 1];
1781 if (NILP (string))
1782 break;
1783 wv->name = (char *) SDATA (string);
1784 wv = wv->next;
1785 }
1786
1787 f->menu_bar_vector = menu_items;
1788 f->menu_bar_items_used = menu_items_used;
1789 menu_items = Qnil;
1790 }
1791 else
1792 {
1793 /* Make a widget-value tree containing
1794 just the top level menu bar strings. */
1795
1796 wv = xmalloc_widget_value ();
1797 wv->name = "menubar";
1798 wv->value = 0;
1799 wv->enabled = 1;
1800 wv->button_type = BUTTON_TYPE_NONE;
1801 wv->help = Qnil;
1802 first_wv = wv;
1803
1804 items = FRAME_MENU_BAR_ITEMS (f);
1805 for (i = 0; i < XVECTOR (items)->size; i += 4)
1806 {
1807 Lisp_Object string;
1808
1809 string = XVECTOR (items)->contents[i + 1];
1810 if (NILP (string))
1811 break;
1812
1813 wv = xmalloc_widget_value ();
1814 wv->name = (char *) SDATA (string);
1815 wv->value = 0;
1816 wv->enabled = 1;
1817 wv->button_type = BUTTON_TYPE_NONE;
1818 wv->help = Qnil;
1819 /* This prevents lwlib from assuming this
1820 menu item is really supposed to be empty. */
1821 /* The EMACS_INT cast avoids a warning.
1822 This value just has to be different from small integers. */
1823 wv->call_data = (void *) (EMACS_INT) (-1);
1824
1825 if (prev_wv)
1826 prev_wv->next = wv;
1827 else
1828 first_wv->contents = wv;
1829 prev_wv = wv;
1830 }
1831
1832 /* Forget what we thought we knew about what is in the
1833 detailed contents of the menu bar menus.
1834 Changing the top level always destroys the contents. */
1835 f->menu_bar_items_used = 0;
1836 }
1837
1838 /* Create or update the menu bar widget. */
1839
1840 BLOCK_INPUT;
1841
1842 if (menubar_widget)
1843 {
1844 /* Disable resizing (done for Motif!) */
1845 lw_allow_resizing (f->output_data.x->widget, False);
1846
1847 /* The third arg is DEEP_P, which says to consider the entire
1848 menu trees we supply, rather than just the menu bar item names. */
1849 lw_modify_all_widgets (id, first_wv, deep_p);
1850
1851 /* Re-enable the edit widget to resize. */
1852 lw_allow_resizing (f->output_data.x->widget, True);
1853 }
1854 else
1855 {
1856 menubar_widget = lw_create_widget ("menubar", "menubar", id, first_wv,
1857 f->output_data.x->column_widget,
1858 0,
1859 popup_activate_callback,
1860 menubar_selection_callback,
1861 popup_deactivate_callback,
1862 menu_highlight_callback);
1863 f->output_data.x->menubar_widget = menubar_widget;
1864 }
1865
1866 {
1867 int menubar_size
1868 = (f->output_data.x->menubar_widget
1869 ? (f->output_data.x->menubar_widget->core.height
1870 + f->output_data.x->menubar_widget->core.border_width)
1871 : 0);
1872
1873 #if 0 /* Experimentally, we now get the right results
1874 for -geometry -0-0 without this. 24 Aug 96, rms. */
1875 #ifdef USE_LUCID
1876 if (FRAME_EXTERNAL_MENU_BAR (f))
1877 {
1878 Dimension ibw = 0;
1879 XtVaGetValues (f->output_data.x->column_widget,
1880 XtNinternalBorderWidth, &ibw, NULL);
1881 menubar_size += ibw;
1882 }
1883 #endif /* USE_LUCID */
1884 #endif /* 0 */
1885
1886 f->output_data.x->menubar_height = menubar_size;
1887 }
1888
1889 free_menubar_widget_value_tree (first_wv);
1890 update_frame_menubar (f);
1891
1892 UNBLOCK_INPUT;
1893 }
1894
1895 /* Called from Fx_create_frame to create the initial menubar of a frame
1896 before it is mapped, so that the window is mapped with the menubar already
1897 there instead of us tacking it on later and thrashing the window after it
1898 is visible. */
1899
1900 void
1901 initialize_frame_menubar (f)
1902 FRAME_PTR f;
1903 {
1904 /* This function is called before the first chance to redisplay
1905 the frame. It has to be, so the frame will have the right size. */
1906 FRAME_MENU_BAR_ITEMS (f) = menu_bar_items (FRAME_MENU_BAR_ITEMS (f));
1907 set_frame_menubar (f, 1, 1);
1908 }
1909
1910
1911 /* Get rid of the menu bar of frame F, and free its storage.
1912 This is used when deleting a frame, and when turning off the menu bar. */
1913
1914 void
1915 free_frame_menubar (f)
1916 FRAME_PTR f;
1917 {
1918 Widget menubar_widget;
1919
1920 menubar_widget = f->output_data.x->menubar_widget;
1921
1922 f->output_data.x->menubar_height = 0;
1923
1924 if (menubar_widget)
1925 {
1926 #ifdef USE_MOTIF
1927 /* Removing the menu bar magically changes the shell widget's x
1928 and y position of (0, 0) which, when the menu bar is turned
1929 on again, leads to pull-down menuss appearing in strange
1930 positions near the upper-left corner of the display. This
1931 happens only with some window managers like twm and ctwm,
1932 but not with other like Motif's mwm or kwm, because the
1933 latter generate ConfigureNotify events when the menu bar
1934 is switched off, which fixes the shell position. */
1935 Position x0, y0, x1, y1;
1936 #endif
1937
1938 BLOCK_INPUT;
1939
1940 #ifdef USE_MOTIF
1941 if (f->output_data.x->widget)
1942 XtVaGetValues (f->output_data.x->widget, XtNx, &x0, XtNy, &y0, NULL);
1943 #endif
1944
1945 lw_destroy_all_widgets ((LWLIB_ID) f->output_data.x->id);
1946 f->output_data.x->menubar_widget = NULL;
1947
1948 #ifdef USE_MOTIF
1949 if (f->output_data.x->widget)
1950 {
1951 XtVaGetValues (f->output_data.x->widget, XtNx, &x1, XtNy, &y1, NULL);
1952 if (x1 == 0 && y1 == 0)
1953 XtVaSetValues (f->output_data.x->widget, XtNx, x0, XtNy, y0, NULL);
1954 }
1955 #endif
1956
1957 UNBLOCK_INPUT;
1958 }
1959 }
1960
1961 #endif /* USE_X_TOOLKIT */
1962 \f
1963 /* xmenu_show actually displays a menu using the panes and items in menu_items
1964 and returns the value selected from it.
1965 There are two versions of xmenu_show, one for Xt and one for Xlib.
1966 Both assume input is blocked by the caller. */
1967
1968 /* F is the frame the menu is for.
1969 X and Y are the frame-relative specified position,
1970 relative to the inside upper left corner of the frame F.
1971 FOR_CLICK is nonzero if this menu was invoked for a mouse click.
1972 KEYMAPS is 1 if this menu was specified with keymaps;
1973 in that case, we return a list containing the chosen item's value
1974 and perhaps also the pane's prefix.
1975 TITLE is the specified menu title.
1976 ERROR is a place to store an error message string in case of failure.
1977 (We return nil on failure, but the value doesn't actually matter.) */
1978
1979 #ifdef USE_X_TOOLKIT
1980
1981 /* We need a unique id for each widget handled by the Lucid Widget
1982 library.
1983
1984 For the main windows, and popup menus, we use this counter,
1985 which we increment each time after use. This starts from 1<<16.
1986
1987 For menu bars, we use numbers starting at 0, counted in
1988 next_menubar_widget_id. */
1989 LWLIB_ID widget_id_tick;
1990
1991 static Lisp_Object *volatile menu_item_selection;
1992
1993 static void
1994 popup_selection_callback (widget, id, client_data)
1995 Widget widget;
1996 LWLIB_ID id;
1997 XtPointer client_data;
1998 {
1999 menu_item_selection = (Lisp_Object *) client_data;
2000 }
2001
2002 static Lisp_Object
2003 xmenu_show (f, x, y, for_click, keymaps, title, error)
2004 FRAME_PTR f;
2005 int x;
2006 int y;
2007 int for_click;
2008 int keymaps;
2009 Lisp_Object title;
2010 char **error;
2011 {
2012 int i;
2013 LWLIB_ID menu_id;
2014 Widget menu;
2015 Arg av[2];
2016 int ac = 0;
2017 widget_value *wv, *save_wv = 0, *first_wv = 0, *prev_wv = 0;
2018 widget_value **submenu_stack
2019 = (widget_value **) alloca (menu_items_used * sizeof (widget_value *));
2020 Lisp_Object *subprefix_stack
2021 = (Lisp_Object *) alloca (menu_items_used * sizeof (Lisp_Object));
2022 int submenu_depth = 0;
2023 XButtonPressedEvent dummy;
2024
2025 int first_pane;
2026
2027 *error = NULL;
2028
2029 if (menu_items_used <= MENU_ITEMS_PANE_LENGTH)
2030 {
2031 *error = "Empty menu";
2032 return Qnil;
2033 }
2034
2035 /* Create a tree of widget_value objects
2036 representing the panes and their items. */
2037 wv = xmalloc_widget_value ();
2038 wv->name = "menu";
2039 wv->value = 0;
2040 wv->enabled = 1;
2041 wv->button_type = BUTTON_TYPE_NONE;
2042 wv->help =Qnil;
2043 first_wv = wv;
2044 first_pane = 1;
2045
2046 /* Loop over all panes and items, filling in the tree. */
2047 i = 0;
2048 while (i < menu_items_used)
2049 {
2050 if (EQ (XVECTOR (menu_items)->contents[i], Qnil))
2051 {
2052 submenu_stack[submenu_depth++] = save_wv;
2053 save_wv = prev_wv;
2054 prev_wv = 0;
2055 first_pane = 1;
2056 i++;
2057 }
2058 else if (EQ (XVECTOR (menu_items)->contents[i], Qlambda))
2059 {
2060 prev_wv = save_wv;
2061 save_wv = submenu_stack[--submenu_depth];
2062 first_pane = 0;
2063 i++;
2064 }
2065 else if (EQ (XVECTOR (menu_items)->contents[i], Qt)
2066 && submenu_depth != 0)
2067 i += MENU_ITEMS_PANE_LENGTH;
2068 /* Ignore a nil in the item list.
2069 It's meaningful only for dialog boxes. */
2070 else if (EQ (XVECTOR (menu_items)->contents[i], Qquote))
2071 i += 1;
2072 else if (EQ (XVECTOR (menu_items)->contents[i], Qt))
2073 {
2074 /* Create a new pane. */
2075 Lisp_Object pane_name, prefix;
2076 char *pane_string;
2077
2078 pane_name = AREF (menu_items, i + MENU_ITEMS_PANE_NAME);
2079 prefix = AREF (menu_items, i + MENU_ITEMS_PANE_PREFIX);
2080
2081 #ifndef HAVE_MULTILINGUAL_MENU
2082 if (STRINGP (pane_name) && STRING_MULTIBYTE (pane_name))
2083 {
2084 pane_name = ENCODE_SYSTEM (pane_name);
2085 AREF (menu_items, i + MENU_ITEMS_PANE_NAME) = pane_name;
2086 }
2087 #endif
2088 pane_string = (NILP (pane_name)
2089 ? "" : (char *) SDATA (pane_name));
2090 /* If there is just one top-level pane, put all its items directly
2091 under the top-level menu. */
2092 if (menu_items_n_panes == 1)
2093 pane_string = "";
2094
2095 /* If the pane has a meaningful name,
2096 make the pane a top-level menu item
2097 with its items as a submenu beneath it. */
2098 if (!keymaps && strcmp (pane_string, ""))
2099 {
2100 wv = xmalloc_widget_value ();
2101 if (save_wv)
2102 save_wv->next = wv;
2103 else
2104 first_wv->contents = wv;
2105 wv->name = pane_string;
2106 if (keymaps && !NILP (prefix))
2107 wv->name++;
2108 wv->value = 0;
2109 wv->enabled = 1;
2110 wv->button_type = BUTTON_TYPE_NONE;
2111 wv->help = Qnil;
2112 save_wv = wv;
2113 prev_wv = 0;
2114 }
2115 else if (first_pane)
2116 {
2117 save_wv = wv;
2118 prev_wv = 0;
2119 }
2120 first_pane = 0;
2121 i += MENU_ITEMS_PANE_LENGTH;
2122 }
2123 else
2124 {
2125 /* Create a new item within current pane. */
2126 Lisp_Object item_name, enable, descrip, def, type, selected, help;
2127 item_name = AREF (menu_items, i + MENU_ITEMS_ITEM_NAME);
2128 enable = AREF (menu_items, i + MENU_ITEMS_ITEM_ENABLE);
2129 descrip = AREF (menu_items, i + MENU_ITEMS_ITEM_EQUIV_KEY);
2130 def = AREF (menu_items, i + MENU_ITEMS_ITEM_DEFINITION);
2131 type = AREF (menu_items, i + MENU_ITEMS_ITEM_TYPE);
2132 selected = AREF (menu_items, i + MENU_ITEMS_ITEM_SELECTED);
2133 help = AREF (menu_items, i + MENU_ITEMS_ITEM_HELP);
2134
2135 #ifndef HAVE_MULTILINGUAL_MENU
2136 if (STRINGP (item_name) && STRING_MULTIBYTE (item_name))
2137 {
2138 item_name = ENCODE_SYSTEM (item_name);
2139 AREF (menu_items, i + MENU_ITEMS_ITEM_NAME) = item_name;
2140 }
2141
2142 if (STRINGP (descrip) && STRING_MULTIBYTE (descrip))
2143 {
2144 descrip = ENCODE_SYSTEM (descrip);
2145 AREF (menu_items, i + MENU_ITEMS_ITEM_EQUIV_KEY) = descrip;
2146 }
2147 #endif /* not HAVE_MULTILINGUAL_MENU */
2148
2149 wv = xmalloc_widget_value ();
2150 if (prev_wv)
2151 prev_wv->next = wv;
2152 else
2153 save_wv->contents = wv;
2154 wv->name = (char *) SDATA (item_name);
2155 if (!NILP (descrip))
2156 wv->key = (char *) SDATA (descrip);
2157 wv->value = 0;
2158 /* If this item has a null value,
2159 make the call_data null so that it won't display a box
2160 when the mouse is on it. */
2161 wv->call_data
2162 = (!NILP (def) ? (void *) &XVECTOR (menu_items)->contents[i] : 0);
2163 wv->enabled = !NILP (enable);
2164
2165 if (NILP (type))
2166 wv->button_type = BUTTON_TYPE_NONE;
2167 else if (EQ (type, QCtoggle))
2168 wv->button_type = BUTTON_TYPE_TOGGLE;
2169 else if (EQ (type, QCradio))
2170 wv->button_type = BUTTON_TYPE_RADIO;
2171 else
2172 abort ();
2173
2174 wv->selected = !NILP (selected);
2175
2176 if (! STRINGP (help))
2177 help = Qnil;
2178
2179 wv->help = help;
2180
2181 prev_wv = wv;
2182
2183 i += MENU_ITEMS_ITEM_LENGTH;
2184 }
2185 }
2186
2187 /* Deal with the title, if it is non-nil. */
2188 if (!NILP (title))
2189 {
2190 widget_value *wv_title = xmalloc_widget_value ();
2191 widget_value *wv_sep1 = xmalloc_widget_value ();
2192 widget_value *wv_sep2 = xmalloc_widget_value ();
2193
2194 wv_sep2->name = "--";
2195 wv_sep2->next = first_wv->contents;
2196 wv_sep2->help = Qnil;
2197
2198 wv_sep1->name = "--";
2199 wv_sep1->next = wv_sep2;
2200 wv_sep1->help = Qnil;
2201
2202 #ifndef HAVE_MULTILINGUAL_MENU
2203 if (STRING_MULTIBYTE (title))
2204 title = ENCODE_SYSTEM (title);
2205 #endif
2206
2207 wv_title->name = (char *) SDATA (title);
2208 wv_title->enabled = TRUE;
2209 wv_title->button_type = BUTTON_TYPE_NONE;
2210 wv_title->next = wv_sep1;
2211 wv_title->help = Qnil;
2212 first_wv->contents = wv_title;
2213 }
2214
2215 /* Actually create the menu. */
2216 menu_id = widget_id_tick++;
2217 menu = lw_create_widget ("popup", first_wv->name, menu_id, first_wv,
2218 f->output_data.x->widget, 1, 0,
2219 popup_selection_callback,
2220 popup_deactivate_callback,
2221 menu_highlight_callback);
2222
2223 /* Adjust coordinates to relative to the outer (window manager) window. */
2224 {
2225 Window child;
2226 int win_x = 0, win_y = 0;
2227
2228 /* Find the position of the outside upper-left corner of
2229 the inner window, with respect to the outer window. */
2230 if (f->output_data.x->parent_desc != FRAME_X_DISPLAY_INFO (f)->root_window)
2231 {
2232 BLOCK_INPUT;
2233 XTranslateCoordinates (FRAME_X_DISPLAY (f),
2234
2235 /* From-window, to-window. */
2236 f->output_data.x->window_desc,
2237 f->output_data.x->parent_desc,
2238
2239 /* From-position, to-position. */
2240 0, 0, &win_x, &win_y,
2241
2242 /* Child of window. */
2243 &child);
2244 UNBLOCK_INPUT;
2245 x += win_x;
2246 y += win_y;
2247 }
2248 }
2249
2250 /* Adjust coordinates to be root-window-relative. */
2251 x += f->output_data.x->left_pos;
2252 y += f->output_data.x->top_pos;
2253
2254 dummy.type = ButtonPress;
2255 dummy.serial = 0;
2256 dummy.send_event = 0;
2257 dummy.display = FRAME_X_DISPLAY (f);
2258 dummy.time = CurrentTime;
2259 dummy.root = FRAME_X_DISPLAY_INFO (f)->root_window;
2260 dummy.window = dummy.root;
2261 dummy.subwindow = dummy.root;
2262 dummy.x_root = x;
2263 dummy.y_root = y;
2264 dummy.x = x;
2265 dummy.y = y;
2266 dummy.state = (FRAME_X_DISPLAY_INFO (f)->grabbed >> 1) * Button1Mask;
2267 dummy.button = 0;
2268 for (i = 0; i < 5; i++)
2269 if (FRAME_X_DISPLAY_INFO (f)->grabbed & (1 << i))
2270 dummy.button = i;
2271
2272 /* Don't allow any geometry request from the user. */
2273 XtSetArg (av[ac], XtNgeometry, 0); ac++;
2274 XtSetValues (menu, av, ac);
2275
2276 /* Free the widget_value objects we used to specify the contents. */
2277 free_menubar_widget_value_tree (first_wv);
2278
2279 /* No selection has been chosen yet. */
2280 menu_item_selection = 0;
2281
2282 /* Display the menu. */
2283 lw_popup_menu (menu, (XEvent *) &dummy);
2284 popup_activated_flag = 1;
2285
2286 /* Process events that apply to the menu. */
2287 popup_get_selection ((XEvent *) 0, FRAME_X_DISPLAY_INFO (f), menu_id);
2288
2289 /* fp turned off the following statement and wrote a comment
2290 that it is unnecessary--that the menu has already disappeared.
2291 Nowadays the menu disappears ok, all right, but
2292 we need to delete the widgets or multiple ones will pile up. */
2293 lw_destroy_all_widgets (menu_id);
2294
2295 /* Find the selected item, and its pane, to return
2296 the proper value. */
2297 if (menu_item_selection != 0)
2298 {
2299 Lisp_Object prefix, entry;
2300
2301 prefix = entry = Qnil;
2302 i = 0;
2303 while (i < menu_items_used)
2304 {
2305 if (EQ (XVECTOR (menu_items)->contents[i], Qnil))
2306 {
2307 subprefix_stack[submenu_depth++] = prefix;
2308 prefix = entry;
2309 i++;
2310 }
2311 else if (EQ (XVECTOR (menu_items)->contents[i], Qlambda))
2312 {
2313 prefix = subprefix_stack[--submenu_depth];
2314 i++;
2315 }
2316 else if (EQ (XVECTOR (menu_items)->contents[i], Qt))
2317 {
2318 prefix
2319 = XVECTOR (menu_items)->contents[i + MENU_ITEMS_PANE_PREFIX];
2320 i += MENU_ITEMS_PANE_LENGTH;
2321 }
2322 /* Ignore a nil in the item list.
2323 It's meaningful only for dialog boxes. */
2324 else if (EQ (XVECTOR (menu_items)->contents[i], Qquote))
2325 i += 1;
2326 else
2327 {
2328 entry
2329 = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_VALUE];
2330 if (menu_item_selection == &XVECTOR (menu_items)->contents[i])
2331 {
2332 if (keymaps != 0)
2333 {
2334 int j;
2335
2336 entry = Fcons (entry, Qnil);
2337 if (!NILP (prefix))
2338 entry = Fcons (prefix, entry);
2339 for (j = submenu_depth - 1; j >= 0; j--)
2340 if (!NILP (subprefix_stack[j]))
2341 entry = Fcons (subprefix_stack[j], entry);
2342 }
2343 return entry;
2344 }
2345 i += MENU_ITEMS_ITEM_LENGTH;
2346 }
2347 }
2348 }
2349
2350 return Qnil;
2351 }
2352 \f
2353 static void
2354 dialog_selection_callback (widget, id, client_data)
2355 Widget widget;
2356 LWLIB_ID id;
2357 XtPointer client_data;
2358 {
2359 /* The EMACS_INT cast avoids a warning. There's no problem
2360 as long as pointers have enough bits to hold small integers. */
2361 if ((int) (EMACS_INT) client_data != -1)
2362 menu_item_selection = (Lisp_Object *) client_data;
2363 BLOCK_INPUT;
2364 lw_destroy_all_widgets (id);
2365 UNBLOCK_INPUT;
2366 popup_activated_flag = 0;
2367 }
2368
2369 static char * button_names [] = {
2370 "button1", "button2", "button3", "button4", "button5",
2371 "button6", "button7", "button8", "button9", "button10" };
2372
2373 static Lisp_Object
2374 xdialog_show (f, keymaps, title, error)
2375 FRAME_PTR f;
2376 int keymaps;
2377 Lisp_Object title;
2378 char **error;
2379 {
2380 int i, nb_buttons=0;
2381 LWLIB_ID dialog_id;
2382 Widget menu;
2383 char dialog_name[6];
2384
2385 widget_value *wv, *first_wv = 0, *prev_wv = 0;
2386
2387 /* Number of elements seen so far, before boundary. */
2388 int left_count = 0;
2389 /* 1 means we've seen the boundary between left-hand elts and right-hand. */
2390 int boundary_seen = 0;
2391
2392 *error = NULL;
2393
2394 if (menu_items_n_panes > 1)
2395 {
2396 *error = "Multiple panes in dialog box";
2397 return Qnil;
2398 }
2399
2400 /* Create a tree of widget_value objects
2401 representing the text label and buttons. */
2402 {
2403 Lisp_Object pane_name, prefix;
2404 char *pane_string;
2405 pane_name = XVECTOR (menu_items)->contents[MENU_ITEMS_PANE_NAME];
2406 prefix = XVECTOR (menu_items)->contents[MENU_ITEMS_PANE_PREFIX];
2407 pane_string = (NILP (pane_name)
2408 ? "" : (char *) SDATA (pane_name));
2409 prev_wv = xmalloc_widget_value ();
2410 prev_wv->value = pane_string;
2411 if (keymaps && !NILP (prefix))
2412 prev_wv->name++;
2413 prev_wv->enabled = 1;
2414 prev_wv->name = "message";
2415 prev_wv->help = Qnil;
2416 first_wv = prev_wv;
2417
2418 /* Loop over all panes and items, filling in the tree. */
2419 i = MENU_ITEMS_PANE_LENGTH;
2420 while (i < menu_items_used)
2421 {
2422
2423 /* Create a new item within current pane. */
2424 Lisp_Object item_name, enable, descrip;
2425 item_name = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_NAME];
2426 enable = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_ENABLE];
2427 descrip
2428 = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_EQUIV_KEY];
2429
2430 if (NILP (item_name))
2431 {
2432 free_menubar_widget_value_tree (first_wv);
2433 *error = "Submenu in dialog items";
2434 return Qnil;
2435 }
2436 if (EQ (item_name, Qquote))
2437 {
2438 /* This is the boundary between left-side elts
2439 and right-side elts. Stop incrementing right_count. */
2440 boundary_seen = 1;
2441 i++;
2442 continue;
2443 }
2444 if (nb_buttons >= 9)
2445 {
2446 free_menubar_widget_value_tree (first_wv);
2447 *error = "Too many dialog items";
2448 return Qnil;
2449 }
2450
2451 wv = xmalloc_widget_value ();
2452 prev_wv->next = wv;
2453 wv->name = (char *) button_names[nb_buttons];
2454 if (!NILP (descrip))
2455 wv->key = (char *) SDATA (descrip);
2456 wv->value = (char *) SDATA (item_name);
2457 wv->call_data = (void *) &XVECTOR (menu_items)->contents[i];
2458 wv->enabled = !NILP (enable);
2459 wv->help = Qnil;
2460 prev_wv = wv;
2461
2462 if (! boundary_seen)
2463 left_count++;
2464
2465 nb_buttons++;
2466 i += MENU_ITEMS_ITEM_LENGTH;
2467 }
2468
2469 /* If the boundary was not specified,
2470 by default put half on the left and half on the right. */
2471 if (! boundary_seen)
2472 left_count = nb_buttons - nb_buttons / 2;
2473
2474 wv = xmalloc_widget_value ();
2475 wv->name = dialog_name;
2476 wv->help = Qnil;
2477 /* Dialog boxes use a really stupid name encoding
2478 which specifies how many buttons to use
2479 and how many buttons are on the right.
2480 The Q means something also. */
2481 dialog_name[0] = 'Q';
2482 dialog_name[1] = '0' + nb_buttons;
2483 dialog_name[2] = 'B';
2484 dialog_name[3] = 'R';
2485 /* Number of buttons to put on the right. */
2486 dialog_name[4] = '0' + nb_buttons - left_count;
2487 dialog_name[5] = 0;
2488 wv->contents = first_wv;
2489 first_wv = wv;
2490 }
2491
2492 /* Actually create the dialog. */
2493 dialog_id = widget_id_tick++;
2494 menu = lw_create_widget (first_wv->name, "dialog", dialog_id, first_wv,
2495 f->output_data.x->widget, 1, 0,
2496 dialog_selection_callback, 0, 0);
2497 lw_modify_all_widgets (dialog_id, first_wv->contents, True);
2498 /* Free the widget_value objects we used to specify the contents. */
2499 free_menubar_widget_value_tree (first_wv);
2500
2501 /* No selection has been chosen yet. */
2502 menu_item_selection = 0;
2503
2504 /* Display the menu. */
2505 lw_pop_up_all_widgets (dialog_id);
2506 popup_activated_flag = 1;
2507
2508 /* Process events that apply to the menu. */
2509 popup_get_selection ((XEvent *) 0, FRAME_X_DISPLAY_INFO (f), dialog_id);
2510
2511 lw_destroy_all_widgets (dialog_id);
2512
2513 /* Find the selected item, and its pane, to return
2514 the proper value. */
2515 if (menu_item_selection != 0)
2516 {
2517 Lisp_Object prefix;
2518
2519 prefix = Qnil;
2520 i = 0;
2521 while (i < menu_items_used)
2522 {
2523 Lisp_Object entry;
2524
2525 if (EQ (XVECTOR (menu_items)->contents[i], Qt))
2526 {
2527 prefix
2528 = XVECTOR (menu_items)->contents[i + MENU_ITEMS_PANE_PREFIX];
2529 i += MENU_ITEMS_PANE_LENGTH;
2530 }
2531 else if (EQ (XVECTOR (menu_items)->contents[i], Qquote))
2532 {
2533 /* This is the boundary between left-side elts and
2534 right-side elts. */
2535 ++i;
2536 }
2537 else
2538 {
2539 entry
2540 = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_VALUE];
2541 if (menu_item_selection == &XVECTOR (menu_items)->contents[i])
2542 {
2543 if (keymaps != 0)
2544 {
2545 entry = Fcons (entry, Qnil);
2546 if (!NILP (prefix))
2547 entry = Fcons (prefix, entry);
2548 }
2549 return entry;
2550 }
2551 i += MENU_ITEMS_ITEM_LENGTH;
2552 }
2553 }
2554 }
2555
2556 return Qnil;
2557 }
2558
2559 #else /* not USE_X_TOOLKIT */
2560
2561 /* The frame of the last activated non-toolkit menu bar.
2562 Used to generate menu help events. */
2563
2564 static struct frame *menu_help_frame;
2565
2566
2567 /* Show help HELP_STRING, or clear help if HELP_STRING is null.
2568
2569 PANE is the pane number, and ITEM is the menu item number in
2570 the menu (currently not used).
2571
2572 This cannot be done with generating a HELP_EVENT because
2573 XMenuActivate contains a loop that doesn't let Emacs process
2574 keyboard events. */
2575
2576 static void
2577 menu_help_callback (help_string, pane, item)
2578 char *help_string;
2579 int pane, item;
2580 {
2581 extern Lisp_Object Qmenu_item;
2582 Lisp_Object *first_item;
2583 Lisp_Object pane_name;
2584 Lisp_Object menu_object;
2585
2586 first_item = XVECTOR (menu_items)->contents;
2587 if (EQ (first_item[0], Qt))
2588 pane_name = first_item[MENU_ITEMS_PANE_NAME];
2589 else if (EQ (first_item[0], Qquote))
2590 /* This shouldn't happen, see xmenu_show. */
2591 pane_name = empty_string;
2592 else
2593 pane_name = first_item[MENU_ITEMS_ITEM_NAME];
2594
2595 /* (menu-item MENU-NAME PANE-NUMBER) */
2596 menu_object = Fcons (Qmenu_item,
2597 Fcons (pane_name,
2598 Fcons (make_number (pane), Qnil)));
2599 show_help_echo (help_string ? build_string (help_string) : Qnil,
2600 Qnil, menu_object, make_number (item), 1);
2601 }
2602
2603
2604 static Lisp_Object
2605 xmenu_show (f, x, y, for_click, keymaps, title, error)
2606 FRAME_PTR f;
2607 int x, y;
2608 int for_click;
2609 int keymaps;
2610 Lisp_Object title;
2611 char **error;
2612 {
2613 Window root;
2614 XMenu *menu;
2615 int pane, selidx, lpane, status;
2616 Lisp_Object entry, pane_prefix;
2617 char *datap;
2618 int ulx, uly, width, height;
2619 int dispwidth, dispheight;
2620 int i, j;
2621 int maxwidth;
2622 int dummy_int;
2623 unsigned int dummy_uint;
2624
2625 *error = 0;
2626 if (menu_items_n_panes == 0)
2627 return Qnil;
2628
2629 if (menu_items_used <= MENU_ITEMS_PANE_LENGTH)
2630 {
2631 *error = "Empty menu";
2632 return Qnil;
2633 }
2634
2635 /* Figure out which root window F is on. */
2636 XGetGeometry (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), &root,
2637 &dummy_int, &dummy_int, &dummy_uint, &dummy_uint,
2638 &dummy_uint, &dummy_uint);
2639
2640 /* Make the menu on that window. */
2641 menu = XMenuCreate (FRAME_X_DISPLAY (f), root, "emacs");
2642 if (menu == NULL)
2643 {
2644 *error = "Can't create menu";
2645 return Qnil;
2646 }
2647
2648 #ifdef HAVE_X_WINDOWS
2649 /* Adjust coordinates to relative to the outer (window manager) window. */
2650 {
2651 Window child;
2652 int win_x = 0, win_y = 0;
2653
2654 /* Find the position of the outside upper-left corner of
2655 the inner window, with respect to the outer window. */
2656 if (f->output_data.x->parent_desc != FRAME_X_DISPLAY_INFO (f)->root_window)
2657 {
2658 BLOCK_INPUT;
2659 XTranslateCoordinates (FRAME_X_DISPLAY (f),
2660
2661 /* From-window, to-window. */
2662 f->output_data.x->window_desc,
2663 f->output_data.x->parent_desc,
2664
2665 /* From-position, to-position. */
2666 0, 0, &win_x, &win_y,
2667
2668 /* Child of window. */
2669 &child);
2670 UNBLOCK_INPUT;
2671 x += win_x;
2672 y += win_y;
2673 }
2674 }
2675 #endif /* HAVE_X_WINDOWS */
2676
2677 /* Adjust coordinates to be root-window-relative. */
2678 x += f->output_data.x->left_pos;
2679 y += f->output_data.x->top_pos;
2680
2681 /* Create all the necessary panes and their items. */
2682 i = 0;
2683 while (i < menu_items_used)
2684 {
2685 if (EQ (XVECTOR (menu_items)->contents[i], Qt))
2686 {
2687 /* Create a new pane. */
2688 Lisp_Object pane_name, prefix;
2689 char *pane_string;
2690
2691 pane_name = XVECTOR (menu_items)->contents[i + MENU_ITEMS_PANE_NAME];
2692 prefix = XVECTOR (menu_items)->contents[i + MENU_ITEMS_PANE_PREFIX];
2693 pane_string = (NILP (pane_name)
2694 ? "" : (char *) SDATA (pane_name));
2695 if (keymaps && !NILP (prefix))
2696 pane_string++;
2697
2698 lpane = XMenuAddPane (FRAME_X_DISPLAY (f), menu, pane_string, TRUE);
2699 if (lpane == XM_FAILURE)
2700 {
2701 XMenuDestroy (FRAME_X_DISPLAY (f), menu);
2702 *error = "Can't create pane";
2703 return Qnil;
2704 }
2705 i += MENU_ITEMS_PANE_LENGTH;
2706
2707 /* Find the width of the widest item in this pane. */
2708 maxwidth = 0;
2709 j = i;
2710 while (j < menu_items_used)
2711 {
2712 Lisp_Object item;
2713 item = XVECTOR (menu_items)->contents[j];
2714 if (EQ (item, Qt))
2715 break;
2716 if (NILP (item))
2717 {
2718 j++;
2719 continue;
2720 }
2721 width = SBYTES (item);
2722 if (width > maxwidth)
2723 maxwidth = width;
2724
2725 j += MENU_ITEMS_ITEM_LENGTH;
2726 }
2727 }
2728 /* Ignore a nil in the item list.
2729 It's meaningful only for dialog boxes. */
2730 else if (EQ (XVECTOR (menu_items)->contents[i], Qquote))
2731 i += 1;
2732 else
2733 {
2734 /* Create a new item within current pane. */
2735 Lisp_Object item_name, enable, descrip, help;
2736 unsigned char *item_data;
2737 char *help_string;
2738
2739 item_name = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_NAME];
2740 enable = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_ENABLE];
2741 descrip
2742 = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_EQUIV_KEY];
2743 help = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_HELP];
2744 help_string = STRINGP (help) ? SDATA (help) : NULL;
2745
2746 if (!NILP (descrip))
2747 {
2748 int gap = maxwidth - SBYTES (item_name);
2749 #ifdef C_ALLOCA
2750 Lisp_Object spacer;
2751 spacer = Fmake_string (make_number (gap), make_number (' '));
2752 item_name = concat2 (item_name, spacer);
2753 item_name = concat2 (item_name, descrip);
2754 item_data = SDATA (item_name);
2755 #else
2756 /* if alloca is fast, use that to make the space,
2757 to reduce gc needs. */
2758 item_data
2759 = (unsigned char *) alloca (maxwidth
2760 + SBYTES (descrip) + 1);
2761 bcopy (SDATA (item_name), item_data,
2762 SBYTES (item_name));
2763 for (j = SCHARS (item_name); j < maxwidth; j++)
2764 item_data[j] = ' ';
2765 bcopy (SDATA (descrip), item_data + j,
2766 SBYTES (descrip));
2767 item_data[j + SBYTES (descrip)] = 0;
2768 #endif
2769 }
2770 else
2771 item_data = SDATA (item_name);
2772
2773 if (XMenuAddSelection (FRAME_X_DISPLAY (f),
2774 menu, lpane, 0, item_data,
2775 !NILP (enable), help_string)
2776 == XM_FAILURE)
2777 {
2778 XMenuDestroy (FRAME_X_DISPLAY (f), menu);
2779 *error = "Can't add selection to menu";
2780 return Qnil;
2781 }
2782 i += MENU_ITEMS_ITEM_LENGTH;
2783 }
2784 }
2785
2786 /* All set and ready to fly. */
2787 XMenuRecompute (FRAME_X_DISPLAY (f), menu);
2788 dispwidth = DisplayWidth (FRAME_X_DISPLAY (f), FRAME_X_SCREEN_NUMBER (f));
2789 dispheight = DisplayHeight (FRAME_X_DISPLAY (f), FRAME_X_SCREEN_NUMBER (f));
2790 x = min (x, dispwidth);
2791 y = min (y, dispheight);
2792 x = max (x, 1);
2793 y = max (y, 1);
2794 XMenuLocate (FRAME_X_DISPLAY (f), menu, 0, 0, x, y,
2795 &ulx, &uly, &width, &height);
2796 if (ulx+width > dispwidth)
2797 {
2798 x -= (ulx + width) - dispwidth;
2799 ulx = dispwidth - width;
2800 }
2801 if (uly+height > dispheight)
2802 {
2803 y -= (uly + height) - dispheight;
2804 uly = dispheight - height;
2805 }
2806 if (ulx < 0) x -= ulx;
2807 if (uly < 0) y -= uly;
2808
2809 XMenuSetAEQ (menu, TRUE);
2810 XMenuSetFreeze (menu, TRUE);
2811 pane = selidx = 0;
2812
2813 /* Help display under X won't work because XMenuActivate contains
2814 a loop that doesn't give Emacs a chance to process it. */
2815 menu_help_frame = f;
2816 status = XMenuActivate (FRAME_X_DISPLAY (f), menu, &pane, &selidx,
2817 x, y, ButtonReleaseMask, &datap,
2818 menu_help_callback);
2819
2820
2821 #ifdef HAVE_X_WINDOWS
2822 /* Assume the mouse has moved out of the X window.
2823 If it has actually moved in, we will get an EnterNotify. */
2824 x_mouse_leave (FRAME_X_DISPLAY_INFO (f));
2825 #endif
2826
2827 switch (status)
2828 {
2829 case XM_SUCCESS:
2830 #ifdef XDEBUG
2831 fprintf (stderr, "pane= %d line = %d\n", panes, selidx);
2832 #endif
2833
2834 /* Find the item number SELIDX in pane number PANE. */
2835 i = 0;
2836 while (i < menu_items_used)
2837 {
2838 if (EQ (XVECTOR (menu_items)->contents[i], Qt))
2839 {
2840 if (pane == 0)
2841 pane_prefix
2842 = XVECTOR (menu_items)->contents[i + MENU_ITEMS_PANE_PREFIX];
2843 pane--;
2844 i += MENU_ITEMS_PANE_LENGTH;
2845 }
2846 else
2847 {
2848 if (pane == -1)
2849 {
2850 if (selidx == 0)
2851 {
2852 entry
2853 = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_VALUE];
2854 if (keymaps != 0)
2855 {
2856 entry = Fcons (entry, Qnil);
2857 if (!NILP (pane_prefix))
2858 entry = Fcons (pane_prefix, entry);
2859 }
2860 break;
2861 }
2862 selidx--;
2863 }
2864 i += MENU_ITEMS_ITEM_LENGTH;
2865 }
2866 }
2867 break;
2868
2869 case XM_FAILURE:
2870 *error = "Can't activate menu";
2871 case XM_IA_SELECT:
2872 case XM_NO_SELECT:
2873 entry = Qnil;
2874 break;
2875 }
2876 XMenuDestroy (FRAME_X_DISPLAY (f), menu);
2877
2878 #ifdef HAVE_X_WINDOWS
2879 /* State that no mouse buttons are now held.
2880 (The oldXMenu code doesn't track this info for us.)
2881 That is not necessarily true, but the fiction leads to reasonable
2882 results, and it is a pain to ask which are actually held now. */
2883 FRAME_X_DISPLAY_INFO (f)->grabbed = 0;
2884 #endif
2885
2886 return entry;
2887 }
2888
2889 #endif /* not USE_X_TOOLKIT */
2890
2891 #endif /* HAVE_MENUS */
2892 \f
2893 void
2894 syms_of_xmenu ()
2895 {
2896 staticpro (&menu_items);
2897 menu_items = Qnil;
2898
2899 Qdebug_on_next_call = intern ("debug-on-next-call");
2900 staticpro (&Qdebug_on_next_call);
2901
2902 DEFVAR_LISP ("menu-updating-frame", &Vmenu_updating_frame,
2903 doc: /* Frame for which we are updating a menu.
2904 The enable predicate for a menu command should check this variable. */);
2905 Vmenu_updating_frame = Qnil;
2906
2907 #ifdef USE_X_TOOLKIT
2908 widget_id_tick = (1<<16);
2909 next_menubar_widget_id = 1;
2910 #endif
2911
2912 defsubr (&Sx_popup_menu);
2913 #ifdef HAVE_MENUS
2914 defsubr (&Sx_popup_dialog);
2915 #endif
2916 }