(update-copyright): Set help-mode in *Help* buffer.
[emacs.git] / src / xmenu.c
blobfffcdc4626ee0831f0c67d0503a1f6b336ca03ce
1 /* X Communication module for terminals which understand the X protocol.
2 Copyright (C) 1986, 1988, 1993, 1994 Free Software Foundation, Inc.
4 This file is part of GNU Emacs.
6 GNU Emacs is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
9 any later version.
11 GNU Emacs is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU Emacs; see the file COPYING. If not, write to
18 the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
20 /* X pop-up deck-of-cards menu facility for gnuemacs.
22 * Written by Jon Arnold and Roman Budzianowski
23 * Mods and rewrite by Robert Krawitz
27 /* Modified by Fred Pierresteguy on December 93
28 to make the popup menus and menubar use the Xt. */
30 /* Rewritten for clarity and GC protection by rms in Feb 94. */
32 /* On 4.3 this loses if it comes after xterm.h. */
33 #include <signal.h>
34 #include <config.h>
36 #include <stdio.h>
37 #include "lisp.h"
38 #include "termhooks.h"
39 #include "frame.h"
40 #include "window.h"
41 #include "keyboard.h"
42 #include "blockinput.h"
43 #include "puresize.h"
45 #ifdef HAVE_X_WINDOWS
46 /* This may include sys/types.h, and that somehow loses
47 if this is not done before the other system files. */
48 #include "xterm.h"
49 #endif
51 /* Load sys/types.h if not already loaded.
52 In some systems loading it twice is suicidal. */
53 #ifndef makedev
54 #include <sys/types.h>
55 #endif
57 #include "dispextern.h"
59 #ifdef HAVE_X_WINDOWS
60 #include "../oldXMenu/XMenu.h"
61 #endif
63 #ifdef USE_X_TOOLKIT
64 #include <X11/Xlib.h>
65 #include <X11/IntrinsicP.h>
66 #include <X11/CoreP.h>
67 #include <X11/StringDefs.h>
68 #include <X11/Shell.h>
69 #include "../lwlib/lwlib.h"
70 #endif /* USE_X_TOOLKIT */
72 #define min(x,y) (((x) < (y)) ? (x) : (y))
73 #define max(x,y) (((x) > (y)) ? (x) : (y))
75 #ifndef TRUE
76 #define TRUE 1
77 #define FALSE 0
78 #endif /* no TRUE */
80 extern Lisp_Object Qmenu_enable;
81 extern Lisp_Object Qmenu_bar;
82 extern Lisp_Object Qmouse_click, Qevent_kind;
84 #ifdef USE_X_TOOLKIT
85 extern void process_expose_from_menu ();
86 extern XtAppContext Xt_app_con;
88 static Lisp_Object xdialog_show ();
89 void popup_get_selection ();
90 #endif
92 static Lisp_Object xmenu_show ();
93 static void keymap_panes ();
94 static void single_keymap_panes ();
95 static void list_of_panes ();
96 static void list_of_items ();
98 /* This holds a Lisp vector that holds the results of decoding
99 the keymaps or alist-of-alists that specify a menu.
101 It describes the panes and items within the panes.
103 Each pane is described by 3 elements in the vector:
104 t, the pane name, the pane's prefix key.
105 Then follow the pane's items, with 4 elements per item:
106 the item string, the enable flag, the item's value,
107 and the equivalent keyboard key's description string.
109 In some cases, multiple levels of menus may be described.
110 A single vector slot containing nil indicates the start of a submenu.
111 A single vector slot containing lambda indicates the end of a submenu.
112 The submenu follows a menu item which is the way to reach the submenu.
114 A single vector slot containing quote indicates that the
115 following items should appear on the right of a dialog box.
117 Using a Lisp vector to hold this information while we decode it
118 takes care of protecting all the data from GC. */
120 #define MENU_ITEMS_PANE_NAME 1
121 #define MENU_ITEMS_PANE_PREFIX 2
122 #define MENU_ITEMS_PANE_LENGTH 3
124 #define MENU_ITEMS_ITEM_NAME 0
125 #define MENU_ITEMS_ITEM_ENABLE 1
126 #define MENU_ITEMS_ITEM_VALUE 2
127 #define MENU_ITEMS_ITEM_EQUIV_KEY 3
128 #define MENU_ITEMS_ITEM_LENGTH 4
130 static Lisp_Object menu_items;
132 /* Number of slots currently allocated in menu_items. */
133 static int menu_items_allocated;
135 /* This is the index in menu_items of the first empty slot. */
136 static int menu_items_used;
138 /* The number of panes currently recorded in menu_items,
139 excluding those within submenus. */
140 static int menu_items_n_panes;
142 /* Current depth within submenus. */
143 static int menu_items_submenu_depth;
145 /* Flag which when set indicates a dialog or menu has been posted by
146 Xt on behalf of one of the widget sets. */
147 static int popup_activated_flag;
150 /* Initialize the menu_items structure if we haven't already done so.
151 Also mark it as currently empty. */
153 static void
154 init_menu_items ()
156 if (NILP (menu_items))
158 menu_items_allocated = 60;
159 menu_items = Fmake_vector (make_number (menu_items_allocated), Qnil);
162 menu_items_used = 0;
163 menu_items_n_panes = 0;
164 menu_items_submenu_depth = 0;
167 /* Call at the end of generating the data in menu_items.
168 This fills in the number of items in the last pane. */
170 static void
171 finish_menu_items ()
175 /* Call when finished using the data for the current menu
176 in menu_items. */
178 static void
179 discard_menu_items ()
181 /* Free the structure if it is especially large.
182 Otherwise, hold on to it, to save time. */
183 if (menu_items_allocated > 200)
185 menu_items = Qnil;
186 menu_items_allocated = 0;
190 /* Make the menu_items vector twice as large. */
192 static void
193 grow_menu_items ()
195 Lisp_Object old;
196 int old_size = menu_items_allocated;
197 old = menu_items;
199 menu_items_allocated *= 2;
200 menu_items = Fmake_vector (make_number (menu_items_allocated), Qnil);
201 bcopy (XVECTOR (old)->contents, XVECTOR (menu_items)->contents,
202 old_size * sizeof (Lisp_Object));
205 /* Begin a submenu. */
207 static void
208 push_submenu_start ()
210 if (menu_items_used + 1 > menu_items_allocated)
211 grow_menu_items ();
213 XVECTOR (menu_items)->contents[menu_items_used++] = Qnil;
214 menu_items_submenu_depth++;
217 /* End a submenu. */
219 static void
220 push_submenu_end ()
222 if (menu_items_used + 1 > menu_items_allocated)
223 grow_menu_items ();
225 XVECTOR (menu_items)->contents[menu_items_used++] = Qlambda;
226 menu_items_submenu_depth--;
229 /* Indicate boundary between left and right. */
231 static void
232 push_left_right_boundary ()
234 if (menu_items_used + 1 > menu_items_allocated)
235 grow_menu_items ();
237 XVECTOR (menu_items)->contents[menu_items_used++] = Qquote;
240 /* Start a new menu pane in menu_items..
241 NAME is the pane name. PREFIX_VEC is a prefix key for this pane. */
243 static void
244 push_menu_pane (name, prefix_vec)
245 Lisp_Object name, prefix_vec;
247 if (menu_items_used + MENU_ITEMS_PANE_LENGTH > menu_items_allocated)
248 grow_menu_items ();
250 if (menu_items_submenu_depth == 0)
251 menu_items_n_panes++;
252 XVECTOR (menu_items)->contents[menu_items_used++] = Qt;
253 XVECTOR (menu_items)->contents[menu_items_used++] = name;
254 XVECTOR (menu_items)->contents[menu_items_used++] = prefix_vec;
257 /* Push one menu item into the current pane.
258 NAME is the string to display. ENABLE if non-nil means
259 this item can be selected. KEY is the key generated by
260 choosing this item. EQUIV is the textual description
261 of the keyboard equivalent for this item (or nil if none). */
263 static void
264 push_menu_item (name, enable, key, equiv)
265 Lisp_Object name, enable, key, equiv;
267 if (menu_items_used + MENU_ITEMS_ITEM_LENGTH > menu_items_allocated)
268 grow_menu_items ();
270 XVECTOR (menu_items)->contents[menu_items_used++] = name;
271 XVECTOR (menu_items)->contents[menu_items_used++] = enable;
272 XVECTOR (menu_items)->contents[menu_items_used++] = key;
273 XVECTOR (menu_items)->contents[menu_items_used++] = equiv;
276 /* Figure out the current keyboard equivalent of a menu item ITEM1.
277 The item string for menu display should be ITEM_STRING.
278 Store the equivalent keyboard key sequence's
279 textual description into *DESCRIP_PTR.
280 Also cache them in the item itself.
281 Return the real definition to execute. */
283 static Lisp_Object
284 menu_item_equiv_key (item_string, item1, descrip_ptr)
285 Lisp_Object item_string;
286 Lisp_Object item1;
287 Lisp_Object *descrip_ptr;
289 /* This is the real definition--the function to run. */
290 Lisp_Object def;
291 /* This is the sublist that records cached equiv key data
292 so we can save time. */
293 Lisp_Object cachelist;
294 /* These are the saved equivalent keyboard key sequence
295 and its key-description. */
296 Lisp_Object savedkey, descrip;
297 Lisp_Object def1;
298 int changed = 0;
299 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
301 /* If a help string follows the item string, skip it. */
302 if (CONSP (XCONS (item1)->cdr)
303 && STRINGP (XCONS (XCONS (item1)->cdr)->car))
304 item1 = XCONS (item1)->cdr;
306 def = Fcdr (item1);
308 /* Get out the saved equivalent-keyboard-key info. */
309 cachelist = savedkey = descrip = Qnil;
310 if (CONSP (def) && CONSP (XCONS (def)->car)
311 && (NILP (XCONS (XCONS (def)->car)->car)
312 || VECTORP (XCONS (XCONS (def)->car)->car)))
314 cachelist = XCONS (def)->car;
315 def = XCONS (def)->cdr;
316 savedkey = XCONS (cachelist)->car;
317 descrip = XCONS (cachelist)->cdr;
320 GCPRO4 (def, def1, savedkey, descrip);
322 /* Is it still valid? */
323 def1 = Qnil;
324 if (!NILP (savedkey))
325 def1 = Fkey_binding (savedkey, Qnil);
326 /* If not, update it. */
327 if (! EQ (def1, def)
328 /* If the command is an alias for another
329 (such as easymenu.el and lmenu.el set it up),
330 check if the original command matches the cached command. */
331 && !(SYMBOLP (def) && SYMBOLP (XSYMBOL (def)->function)
332 && EQ (def1, XSYMBOL (def)->function))
333 /* If something had no key binding before, don't recheck it--
334 doing that takes too much time and makes menus too slow. */
335 && !(!NILP (cachelist) && NILP (savedkey)))
337 changed = 1;
338 descrip = Qnil;
339 savedkey = Fwhere_is_internal (def, Qnil, Qt, Qnil);
340 /* If the command is an alias for another
341 (such as easymenu.el and lmenu.el set it up),
342 see if the original command name has equivalent keys. */
343 if (SYMBOLP (def) && SYMBOLP (XSYMBOL (def)->function))
344 savedkey = Fwhere_is_internal (XSYMBOL (def)->function,
345 Qnil, Qt, Qnil);
347 if (VECTORP (savedkey)
348 && EQ (XVECTOR (savedkey)->contents[0], Qmenu_bar))
349 savedkey = Qnil;
350 /* Reject two-key sequences that start with a mouse click.
351 These are probably menu items. */
352 if (VECTORP (savedkey)
353 && XVECTOR (savedkey)->size > 1
354 && SYMBOLP (XVECTOR (savedkey)->contents[0]))
356 Lisp_Object tem;
358 tem = Fget (XVECTOR (savedkey)->contents[0], Qevent_kind);
359 if (EQ (tem, Qmouse_click))
360 savedkey = Qnil;
362 if (!NILP (savedkey))
364 descrip = Fkey_description (savedkey);
365 descrip = concat2 (make_string (" (", 3), descrip);
366 descrip = concat2 (descrip, make_string (")", 1));
370 /* Cache the data we just got in a sublist of the menu binding. */
371 if (NILP (cachelist))
373 CHECK_IMPURE (item1);
374 XCONS (item1)->cdr = Fcons (Fcons (savedkey, descrip), def);
376 else if (changed)
378 XCONS (cachelist)->car = savedkey;
379 XCONS (cachelist)->cdr = descrip;
382 UNGCPRO;
383 *descrip_ptr = descrip;
384 return def;
387 /* This is used as the handler when calling internal_condition_case_1. */
389 static Lisp_Object
390 menu_item_enabled_p_1 (arg)
391 Lisp_Object arg;
393 return Qnil;
396 /* Return non-nil if the command DEF is enabled when used as a menu item.
397 This is based on looking for a menu-enable property.
398 If NOTREAL is set, don't bother really computing this. */
400 static Lisp_Object
401 menu_item_enabled_p (def, notreal)
402 Lisp_Object def;
403 int notreal;
405 Lisp_Object enabled, tem;
407 enabled = Qt;
408 if (notreal)
409 return enabled;
410 if (SYMBOLP (def))
412 /* No property, or nil, means enable.
413 Otherwise, enable if value is not nil. */
414 tem = Fget (def, Qmenu_enable);
415 if (!NILP (tem))
416 /* (condition-case nil (eval tem)
417 (error nil)) */
418 enabled = internal_condition_case_1 (Feval, tem, Qerror,
419 menu_item_enabled_p_1);
421 return enabled;
424 /* Look through KEYMAPS, a vector of keymaps that is NMAPS long,
425 and generate menu panes for them in menu_items.
426 If NOTREAL is nonzero,
427 don't bother really computing whether an item is enabled. */
429 static void
430 keymap_panes (keymaps, nmaps, notreal)
431 Lisp_Object *keymaps;
432 int nmaps;
433 int notreal;
435 int mapno;
437 init_menu_items ();
439 /* Loop over the given keymaps, making a pane for each map.
440 But don't make a pane that is empty--ignore that map instead.
441 P is the number of panes we have made so far. */
442 for (mapno = 0; mapno < nmaps; mapno++)
443 single_keymap_panes (keymaps[mapno], Qnil, Qnil, notreal);
445 finish_menu_items ();
448 /* This is a recursive subroutine of keymap_panes.
449 It handles one keymap, KEYMAP.
450 The other arguments are passed along
451 or point to local variables of the previous function.
452 If NOTREAL is nonzero,
453 don't bother really computing whether an item is enabled. */
455 static void
456 single_keymap_panes (keymap, pane_name, prefix, notreal)
457 Lisp_Object keymap;
458 Lisp_Object pane_name;
459 Lisp_Object prefix;
460 int notreal;
462 Lisp_Object pending_maps;
463 Lisp_Object tail, item, item1, item_string, table;
464 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
466 pending_maps = Qnil;
468 push_menu_pane (pane_name, prefix);
470 for (tail = keymap; CONSP (tail); tail = XCONS (tail)->cdr)
472 /* Look at each key binding, and if it has a menu string,
473 make a menu item from it. */
474 item = XCONS (tail)->car;
475 if (CONSP (item))
477 item1 = XCONS (item)->cdr;
478 if (CONSP (item1))
480 item_string = XCONS (item1)->car;
481 if (STRINGP (item_string))
483 /* This is the real definition--the function to run. */
484 Lisp_Object def;
485 /* These are the saved equivalent keyboard key sequence
486 and its key-description. */
487 Lisp_Object descrip;
488 Lisp_Object tem, enabled;
490 /* GCPRO because ...enabled_p will call eval
491 and ..._equiv_key may autoload something.
492 Protecting KEYMAP preserves everything we use;
493 aside from that, must protect whatever might be
494 a string. Since there's no GCPRO5, we refetch
495 item_string instead of protecting it. */
496 descrip = def = Qnil;
497 GCPRO4 (keymap, pending_maps, def, descrip);
499 def = menu_item_equiv_key (item_string, item1, &descrip);
500 enabled = menu_item_enabled_p (def, notreal);
502 UNGCPRO;
504 item_string = XCONS (item1)->car;
506 tem = Fkeymapp (def);
507 if (XSTRING (item_string)->data[0] == '@' && !NILP (tem))
508 pending_maps = Fcons (Fcons (def, Fcons (item_string, XCONS (item)->car)),
509 pending_maps);
510 else
512 Lisp_Object submap;
513 GCPRO4 (keymap, pending_maps, descrip, item_string);
514 submap = get_keymap_1 (def, 0, 1);
515 UNGCPRO;
516 #ifndef USE_X_TOOLKIT
517 /* Indicate visually that this is a submenu. */
518 if (!NILP (submap))
519 item_string = concat2 (item_string,
520 build_string (" >"));
521 #endif
522 push_menu_item (item_string, enabled, XCONS (item)->car,
523 descrip);
524 #ifdef USE_X_TOOLKIT
525 /* Display a submenu using the toolkit. */
526 if (! NILP (submap))
528 push_submenu_start ();
529 single_keymap_panes (submap, Qnil,
530 XCONS (item)->car, notreal);
531 push_submenu_end ();
533 #endif
538 else if (VECTORP (item))
540 /* Loop over the char values represented in the vector. */
541 int len = XVECTOR (item)->size;
542 int c;
543 for (c = 0; c < len; c++)
545 Lisp_Object character;
546 XSETFASTINT (character, c);
547 item1 = XVECTOR (item)->contents[c];
548 if (CONSP (item1))
550 item_string = XCONS (item1)->car;
551 if (STRINGP (item_string))
553 Lisp_Object def;
555 /* These are the saved equivalent keyboard key sequence
556 and its key-description. */
557 Lisp_Object descrip;
558 Lisp_Object tem, enabled;
560 /* GCPRO because ...enabled_p will call eval
561 and ..._equiv_key may autoload something.
562 Protecting KEYMAP preserves everything we use;
563 aside from that, must protect whatever might be
564 a string. Since there's no GCPRO5, we refetch
565 item_string instead of protecting it. */
566 GCPRO4 (keymap, pending_maps, def, descrip);
567 descrip = def = Qnil;
569 def = menu_item_equiv_key (item_string, item1, &descrip);
570 enabled = menu_item_enabled_p (def, notreal);
572 UNGCPRO;
574 item_string = XCONS (item1)->car;
576 tem = Fkeymapp (def);
577 if (XSTRING (item_string)->data[0] == '@' && !NILP (tem))
578 pending_maps = Fcons (Fcons (def, Fcons (item_string, character)),
579 pending_maps);
580 else
582 Lisp_Object submap;
583 GCPRO4 (keymap, pending_maps, descrip, item_string);
584 submap = get_keymap_1 (def, 0, 1);
585 UNGCPRO;
586 #ifndef USE_X_TOOLKIT
587 if (!NILP (submap))
588 item_string = concat2 (item_string,
589 build_string (" >"));
590 #endif
591 push_menu_item (item_string, enabled, character,
592 descrip);
593 #ifdef USE_X_TOOLKIT
594 if (! NILP (submap))
596 push_submenu_start ();
597 single_keymap_panes (submap, Qnil,
598 character, notreal);
599 push_submenu_end ();
601 #endif
609 /* Process now any submenus which want to be panes at this level. */
610 while (!NILP (pending_maps))
612 Lisp_Object elt, eltcdr, string;
613 elt = Fcar (pending_maps);
614 eltcdr = XCONS (elt)->cdr;
615 string = XCONS (eltcdr)->car;
616 /* We no longer discard the @ from the beginning of the string here.
617 Instead, we do this in xmenu_show. */
618 single_keymap_panes (Fcar (elt), string,
619 XCONS (eltcdr)->cdr, notreal);
620 pending_maps = Fcdr (pending_maps);
624 /* Push all the panes and items of a menu decsribed by the
625 alist-of-alists MENU.
626 This handles old-fashioned calls to x-popup-menu. */
628 static void
629 list_of_panes (menu)
630 Lisp_Object menu;
632 Lisp_Object tail;
634 init_menu_items ();
636 for (tail = menu; !NILP (tail); tail = Fcdr (tail))
638 Lisp_Object elt, pane_name, pane_data;
639 elt = Fcar (tail);
640 pane_name = Fcar (elt);
641 CHECK_STRING (pane_name, 0);
642 push_menu_pane (pane_name, Qnil);
643 pane_data = Fcdr (elt);
644 CHECK_CONS (pane_data, 0);
645 list_of_items (pane_data);
648 finish_menu_items ();
651 /* Push the items in a single pane defined by the alist PANE. */
653 static void
654 list_of_items (pane)
655 Lisp_Object pane;
657 Lisp_Object tail, item, item1;
659 for (tail = pane; !NILP (tail); tail = Fcdr (tail))
661 item = Fcar (tail);
662 if (STRINGP (item))
663 push_menu_item (item, Qnil, Qnil, Qnil);
664 else if (NILP (item))
665 push_left_right_boundary ();
666 else
668 CHECK_CONS (item, 0);
669 item1 = Fcar (item);
670 CHECK_STRING (item1, 1);
671 push_menu_item (item1, Qt, Fcdr (item), Qnil);
676 DEFUN ("x-popup-menu", Fx_popup_menu, Sx_popup_menu, 2, 2, 0,
677 "Pop up a deck-of-cards menu and return user's selection.\n\
678 POSITION is a position specification. This is either a mouse button event\n\
679 or a list ((XOFFSET YOFFSET) WINDOW)\n\
680 where XOFFSET and YOFFSET are positions in pixels from the top left\n\
681 corner of WINDOW's frame. (WINDOW may be a frame object instead of a window.)\n\
682 This controls the position of the center of the first line\n\
683 in the first pane of the menu, not the top left of the menu as a whole.\n\
684 If POSITION is t, it means to use the current mouse position.\n\
686 MENU is a specifier for a menu. For the simplest case, MENU is a keymap.\n\
687 The menu items come from key bindings that have a menu string as well as\n\
688 a definition; actually, the \"definition\" in such a key binding looks like\n\
689 \(STRING . REAL-DEFINITION). To give the menu a title, put a string into\n\
690 the keymap as a top-level element.\n\n\
691 You can also use a list of keymaps as MENU.\n\
692 Then each keymap makes a separate pane.\n\
693 When MENU is a keymap or a list of keymaps, the return value\n\
694 is a list of events.\n\n\
695 Alternatively, you can specify a menu of multiple panes\n\
696 with a list of the form (TITLE PANE1 PANE2...),\n\
697 where each pane is a list of form (TITLE ITEM1 ITEM2...).\n\
698 Each ITEM is normally a cons cell (STRING . VALUE);\n\
699 but a string can appear as an item--that makes a nonselectable line\n\
700 in the menu.\n\
701 With this form of menu, the return value is VALUE from the chosen item.\n\
703 If POSITION is nil, don't display the menu at all, just precalculate the\n\
704 cached information about equivalent key sequences.")
705 (position, menu)
706 Lisp_Object position, menu;
708 int number_of_panes, panes;
709 Lisp_Object keymap, tem;
710 int xpos, ypos;
711 Lisp_Object title;
712 char *error_name;
713 Lisp_Object selection;
714 int i, j;
715 FRAME_PTR f;
716 Lisp_Object x, y, window;
717 int keymaps = 0;
718 int menubarp = 0;
719 struct gcpro gcpro1;
721 if (! NILP (position))
723 check_x ();
725 /* Decode the first argument: find the window and the coordinates. */
726 if (EQ (position, Qt))
728 /* Use the mouse's current position. */
729 FRAME_PTR new_f = 0;
730 Lisp_Object bar_window;
731 int part;
732 unsigned long time;
734 if (mouse_position_hook)
735 (*mouse_position_hook) (&new_f, &bar_window, &part, &x, &y, &time);
736 if (new_f != 0)
737 XSETFRAME (window, new_f);
738 else
740 window = selected_window;
741 XSETFASTINT (x, 0);
742 XSETFASTINT (y, 0);
745 else
747 tem = Fcar (position);
748 if (CONSP (tem))
750 window = Fcar (Fcdr (position));
751 x = Fcar (tem);
752 y = Fcar (Fcdr (tem));
754 else
756 tem = Fcar (Fcdr (position)); /* EVENT_START (position) */
757 window = Fcar (tem); /* POSN_WINDOW (tem) */
758 tem = Fcar (Fcdr (Fcdr (tem))); /* POSN_WINDOW_POSN (tem) */
759 x = Fcar (tem);
760 y = Fcdr (tem);
762 /* Determine whether this menu is handling a menu bar click. */
763 tem = Fcar (Fcdr (Fcar (Fcdr (position))));
764 if (CONSP (tem) && EQ (Fcar (tem), Qmenu_bar))
765 menubarp = 1;
769 CHECK_NUMBER (x, 0);
770 CHECK_NUMBER (y, 0);
772 /* Decode where to put the menu. */
774 if (FRAMEP (window))
776 f = XFRAME (window);
777 xpos = 0;
778 ypos = 0;
780 else if (WINDOWP (window))
782 CHECK_LIVE_WINDOW (window, 0);
783 f = XFRAME (WINDOW_FRAME (XWINDOW (window)));
785 xpos = (FONT_WIDTH (f->display.x->font) * XWINDOW (window)->left);
786 ypos = (f->display.x->line_height * XWINDOW (window)->top);
788 else
789 /* ??? Not really clean; should be CHECK_WINDOW_OR_FRAME,
790 but I don't want to make one now. */
791 CHECK_WINDOW (window, 0);
793 xpos += XINT (x);
794 ypos += XINT (y);
797 title = Qnil;
798 GCPRO1 (title);
800 /* Decode the menu items from what was specified. */
802 keymap = Fkeymapp (menu);
803 tem = Qnil;
804 if (CONSP (menu))
805 tem = Fkeymapp (Fcar (menu));
806 if (!NILP (keymap))
808 /* We were given a keymap. Extract menu info from the keymap. */
809 Lisp_Object prompt;
810 keymap = get_keymap (menu);
812 /* Extract the detailed info to make one pane. */
813 keymap_panes (&menu, 1, NILP (position));
815 /* Search for a string appearing directly as an element of the keymap.
816 That string is the title of the menu. */
817 prompt = map_prompt (keymap);
819 /* Make that be the pane title of the first pane. */
820 if (!NILP (prompt) && menu_items_n_panes >= 0)
821 XVECTOR (menu_items)->contents[MENU_ITEMS_PANE_NAME] = prompt;
823 keymaps = 1;
825 else if (!NILP (tem))
827 /* We were given a list of keymaps. */
828 int nmaps = XFASTINT (Flength (menu));
829 Lisp_Object *maps
830 = (Lisp_Object *) alloca (nmaps * sizeof (Lisp_Object));
831 int i;
833 title = Qnil;
835 /* The first keymap that has a prompt string
836 supplies the menu title. */
837 for (tem = menu, i = 0; CONSP (tem); tem = Fcdr (tem))
839 Lisp_Object prompt;
841 maps[i++] = keymap = get_keymap (Fcar (tem));
843 prompt = map_prompt (keymap);
844 if (NILP (title) && !NILP (prompt))
845 title = prompt;
848 /* Extract the detailed info to make one pane. */
849 keymap_panes (maps, nmaps, NILP (position));
851 /* Make the title be the pane title of the first pane. */
852 if (!NILP (title) && menu_items_n_panes >= 0)
853 XVECTOR (menu_items)->contents[MENU_ITEMS_PANE_NAME] = title;
855 keymaps = 1;
857 else
859 /* We were given an old-fashioned menu. */
860 title = Fcar (menu);
861 CHECK_STRING (title, 1);
863 list_of_panes (Fcdr (menu));
865 keymaps = 0;
868 if (NILP (position))
870 discard_menu_items ();
871 UNGCPRO;
872 return Qnil;
875 /* Display them in a menu. */
876 BLOCK_INPUT;
878 selection = xmenu_show (f, xpos, ypos, menubarp,
879 keymaps, title, &error_name);
880 UNBLOCK_INPUT;
882 discard_menu_items ();
884 UNGCPRO;
886 if (error_name) error (error_name);
887 return selection;
890 DEFUN ("x-popup-dialog", Fx_popup_dialog, Sx_popup_dialog, 2, 2, 0,
891 "Pop up a dialog box and return user's selection.\n\
892 POSITION specifies which frame to use.\n\
893 This is normally a mouse button event or a window or frame.\n\
894 If POSITION is t, it means to use the frame the mouse is on.\n\
895 The dialog box appears in the middle of the specified frame.\n\
897 CONTENTS specifies the alternatives to display in the dialog box.\n\
898 It is a list of the form (TITLE ITEM1 ITEM2...).\n\
899 Each ITEM is a cons cell (STRING . VALUE).\n\
900 The return value is VALUE from the chosen item.\n\n\
901 An ITEM may also be just a string--that makes a nonselectable item.\n\
902 An ITEM may also be nil--that means to put all preceding items\n\
903 on the left of the dialog box and all following items on the right.\n\
904 \(By default, approximately half appear on each side.)")
905 (position, contents)
906 Lisp_Object position, contents;
908 FRAME_PTR f;
909 Lisp_Object window;
911 check_x ();
913 /* Decode the first argument: find the window or frame to use. */
914 if (EQ (position, Qt))
916 #if 0 /* Using the frame the mouse is on may not be right. */
917 /* Use the mouse's current position. */
918 FRAME_PTR new_f = 0;
919 Lisp_Object bar_window;
920 int part;
921 unsigned long time;
922 Lisp_Object x, y;
924 (*mouse_position_hook) (&new_f, &bar_window, &part, &x, &y, &time);
926 if (new_f != 0)
927 XSETFRAME (window, new_f);
928 else
929 window = selected_window;
930 #endif
931 /* Decode the first argument: find the window and the coordinates. */
932 if (EQ (position, Qt))
933 window = selected_window;
935 else if (CONSP (position))
937 Lisp_Object tem;
938 tem = Fcar (position);
939 if (CONSP (tem))
940 window = Fcar (Fcdr (position));
941 else
943 tem = Fcar (Fcdr (position)); /* EVENT_START (position) */
944 window = Fcar (tem); /* POSN_WINDOW (tem) */
947 else if (WINDOWP (position) || FRAMEP (position))
948 window = position;
950 /* Decode where to put the menu. */
952 if (FRAMEP (window))
953 f = XFRAME (window);
954 else if (WINDOWP (window))
956 CHECK_LIVE_WINDOW (window, 0);
957 f = XFRAME (WINDOW_FRAME (XWINDOW (window)));
959 else
960 /* ??? Not really clean; should be CHECK_WINDOW_OR_FRAME,
961 but I don't want to make one now. */
962 CHECK_WINDOW (window, 0);
964 #ifndef USE_X_TOOLKIT
965 /* Display a menu with these alternatives
966 in the middle of frame F. */
968 Lisp_Object x, y, frame, newpos;
969 XSETFRAME (frame, f);
970 XSETINT (x, x_pixel_width (f) / 2);
971 XSETINT (y, x_pixel_height (f) / 2);
972 newpos = Fcons (Fcons (x, Fcons (y, Qnil)), Fcons (frame, Qnil));
974 return Fx_popup_menu (newpos,
975 Fcons (Fcar (contents), Fcons (contents, Qnil)));
977 #else
979 Lisp_Object title;
980 char *error_name;
981 Lisp_Object selection;
983 /* Decode the dialog items from what was specified. */
984 title = Fcar (contents);
985 CHECK_STRING (title, 1);
987 list_of_panes (Fcons (contents, Qnil));
989 /* Display them in a dialog box. */
990 BLOCK_INPUT;
991 selection = xdialog_show (f, 0, 0, title, &error_name);
992 UNBLOCK_INPUT;
994 discard_menu_items ();
996 if (error_name) error (error_name);
997 return selection;
999 #endif
1002 #ifdef USE_X_TOOLKIT
1004 /* Loop in Xt until the menu pulldown or dialog popup has been
1005 popped down (deactivated).
1007 NOTE: All calls to popup_get_selection() should be protected
1008 with BLOCK_INPUT, UNBLOCK_INPUT wrappers. */
1009 void
1010 popup_get_selection (initial_event)
1011 XEvent *initial_event;
1013 XEvent event;
1015 if (initial_event)
1016 event = *initial_event;
1017 else
1018 XtAppNextEvent (Xt_app_con, &event);
1020 while (1)
1022 XtDispatchEvent (&event);
1023 if (!popup_activated())
1024 break;
1025 XtAppNextEvent (Xt_app_con, &event);
1029 /* Detect if a dialog or menu has been posted. */
1031 popup_activated ()
1033 return popup_activated_flag;
1037 /* This callback is invoked when the user selects a menubar cascade
1038 pushbutton, but before the pulldown menu is posted. */
1040 static void
1041 popup_activate_callback (widget, id, client_data)
1042 Widget widget;
1043 LWLIB_ID id;
1044 XtPointer client_data;
1046 popup_activated_flag = 1;
1049 /* This callback is called from the menu bar pulldown menu
1050 when the user makes a selection.
1051 Figure out what the user chose
1052 and put the appropriate events into the keyboard buffer. */
1054 static void
1055 menubar_selection_callback (widget, id, client_data)
1056 Widget widget;
1057 LWLIB_ID id;
1058 XtPointer client_data;
1060 Lisp_Object prefix;
1061 FRAME_PTR f = (FRAME_PTR) id;
1062 Lisp_Object vector;
1063 Lisp_Object *subprefix_stack;
1064 int submenu_depth = 0;
1065 int i;
1067 if (!f)
1068 return;
1069 subprefix_stack = (Lisp_Object *) alloca (f->menu_bar_items_used * sizeof (Lisp_Object));
1070 vector = f->menu_bar_vector;
1071 prefix = Qnil;
1072 i = 0;
1073 while (i < f->menu_bar_items_used)
1075 Lisp_Object entry;
1077 if (EQ (XVECTOR (vector)->contents[i], Qnil))
1079 subprefix_stack[submenu_depth++] = prefix;
1080 prefix = entry;
1081 i++;
1083 else if (EQ (XVECTOR (vector)->contents[i], Qlambda))
1085 prefix = subprefix_stack[--submenu_depth];
1086 i++;
1088 else if (EQ (XVECTOR (vector)->contents[i], Qt))
1090 prefix
1091 = XVECTOR (vector)->contents[i + MENU_ITEMS_PANE_PREFIX];
1092 i += MENU_ITEMS_PANE_LENGTH;
1094 else
1096 entry
1097 = XVECTOR (vector)->contents[i + MENU_ITEMS_ITEM_VALUE];
1098 if ((int) client_data == i)
1100 int j;
1101 struct input_event buf;
1103 buf.kind = menu_bar_event;
1104 buf.frame_or_window = Qmenu_bar;
1105 kbd_buffer_store_event (&buf);
1107 for (j = 0; j < submenu_depth; j++)
1108 if (!NILP (subprefix_stack[j]))
1110 buf.kind = menu_bar_event;
1111 buf.frame_or_window = subprefix_stack[j];
1112 kbd_buffer_store_event (&buf);
1115 if (!NILP (prefix))
1117 buf.kind = menu_bar_event;
1118 buf.frame_or_window = prefix;
1119 kbd_buffer_store_event (&buf);
1122 buf.kind = menu_bar_event;
1123 buf.frame_or_window = entry;
1124 kbd_buffer_store_event (&buf);
1126 return;
1128 i += MENU_ITEMS_ITEM_LENGTH;
1133 /* This callback is invoked when a dialog or menu is finished being
1134 used and has been unposted. */
1136 static void
1137 popup_deactivate_callback (widget, id, client_data)
1138 Widget widget;
1139 LWLIB_ID id;
1140 XtPointer client_data;
1142 popup_activated_flag = 0;
1146 /* This recursively calls free_widget_value on the tree of widgets.
1147 It must free all data that was malloc'ed for these widget_values.
1148 In Emacs, many slots are pointers into the data of Lisp_Strings, and
1149 must be left alone. */
1151 void
1152 free_menubar_widget_value_tree (wv)
1153 widget_value *wv;
1155 if (! wv) return;
1157 wv->name = wv->value = wv->key = (char *) 0xDEADBEEF;
1159 if (wv->contents && (wv->contents != (widget_value*)1))
1161 free_menubar_widget_value_tree (wv->contents);
1162 wv->contents = (widget_value *) 0xDEADBEEF;
1164 if (wv->next)
1166 free_menubar_widget_value_tree (wv->next);
1167 wv->next = (widget_value *) 0xDEADBEEF;
1169 BLOCK_INPUT;
1170 free_widget_value (wv);
1171 UNBLOCK_INPUT;
1174 /* Return a tree of widget_value structures for a menu bar item
1175 whose event type is ITEM_KEY (with string ITEM_NAME)
1176 and whose contents come from the list of keymaps MAPS. */
1178 static widget_value *
1179 single_submenu (item_key, item_name, maps)
1180 Lisp_Object item_key, item_name, maps;
1182 widget_value *wv, *prev_wv, *save_wv, *first_wv;
1183 int i;
1184 int submenu_depth = 0;
1185 Lisp_Object length;
1186 int len;
1187 Lisp_Object *mapvec;
1188 widget_value **submenu_stack;
1189 int mapno;
1190 int previous_items = menu_items_used;
1192 length = Flength (maps);
1193 len = XINT (length);
1195 /* Convert the list MAPS into a vector MAPVEC. */
1196 mapvec = (Lisp_Object *) alloca (len * sizeof (Lisp_Object));
1197 for (i = 0; i < len; i++)
1199 mapvec[i] = Fcar (maps);
1200 maps = Fcdr (maps);
1203 menu_items_n_panes = 0;
1205 /* Loop over the given keymaps, making a pane for each map.
1206 But don't make a pane that is empty--ignore that map instead. */
1207 for (i = 0; i < len; i++)
1208 single_keymap_panes (mapvec[i], item_name, item_key, 0);
1210 /* Create a tree of widget_value objects
1211 representing the panes and their items. */
1213 submenu_stack
1214 = (widget_value **) alloca (menu_items_used * sizeof (widget_value *));
1215 wv = malloc_widget_value ();
1216 wv->name = "menu";
1217 wv->value = 0;
1218 wv->enabled = 1;
1219 first_wv = wv;
1220 save_wv = 0;
1222 /* Loop over all panes and items made during this call
1223 and construct a tree of widget_value objects.
1224 Ignore the panes and items made by previous calls to
1225 single_submenu, even though those are also in menu_items. */
1226 i = previous_items;
1227 while (i < menu_items_used)
1229 if (EQ (XVECTOR (menu_items)->contents[i], Qnil))
1231 submenu_stack[submenu_depth++] = save_wv;
1232 save_wv = prev_wv;
1233 prev_wv = 0;
1234 i++;
1236 else if (EQ (XVECTOR (menu_items)->contents[i], Qlambda))
1238 prev_wv = save_wv;
1239 save_wv = submenu_stack[--submenu_depth];
1240 i++;
1242 else if (EQ (XVECTOR (menu_items)->contents[i], Qt)
1243 && submenu_depth != 0)
1244 i += MENU_ITEMS_PANE_LENGTH;
1245 /* Ignore a nil in the item list.
1246 It's meaningful only for dialog boxes. */
1247 else if (EQ (XVECTOR (menu_items)->contents[i], Qquote))
1248 i += 1;
1249 else if (EQ (XVECTOR (menu_items)->contents[i], Qt))
1251 /* Create a new pane. */
1252 Lisp_Object pane_name, prefix;
1253 char *pane_string;
1254 pane_name = XVECTOR (menu_items)->contents[i + MENU_ITEMS_PANE_NAME];
1255 prefix = XVECTOR (menu_items)->contents[i + MENU_ITEMS_PANE_PREFIX];
1256 pane_string = (NILP (pane_name)
1257 ? "" : (char *) XSTRING (pane_name)->data);
1258 /* If there is just one top-level pane, put all its items directly
1259 under the top-level menu. */
1260 if (menu_items_n_panes == 1)
1261 pane_string = "";
1263 /* If the pane has a meaningful name,
1264 make the pane a top-level menu item
1265 with its items as a submenu beneath it. */
1266 if (strcmp (pane_string, ""))
1268 wv = malloc_widget_value ();
1269 if (save_wv)
1270 save_wv->next = wv;
1271 else
1272 first_wv->contents = wv;
1273 wv->name = pane_string;
1274 if (!NILP (prefix))
1275 wv->name++;
1276 wv->value = 0;
1277 wv->enabled = 1;
1279 save_wv = wv;
1280 prev_wv = 0;
1281 i += MENU_ITEMS_PANE_LENGTH;
1283 else
1285 /* Create a new item within current pane. */
1286 Lisp_Object item_name, enable, descrip;
1287 item_name = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_NAME];
1288 enable = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_ENABLE];
1289 descrip
1290 = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_EQUIV_KEY];
1292 wv = malloc_widget_value ();
1293 if (prev_wv)
1294 prev_wv->next = wv;
1295 else
1296 save_wv->contents = wv;
1297 wv->name = (char *) XSTRING (item_name)->data;
1298 if (!NILP (descrip))
1299 wv->key = (char *) XSTRING (descrip)->data;
1300 wv->value = 0;
1301 wv->call_data = (void *) i;
1302 wv->enabled = !NILP (enable);
1303 prev_wv = wv;
1305 i += MENU_ITEMS_ITEM_LENGTH;
1309 return first_wv;
1312 extern void EmacsFrameSetCharSize ();
1314 /* Recompute the menu bar of frame F. */
1316 static void
1317 update_frame_menubar (f)
1318 FRAME_PTR f;
1320 struct x_display *x = f->display.x;
1321 int columns, rows;
1322 int menubar_changed;
1324 Dimension shell_height;
1326 /* We assume the menubar contents has changed if the global flag is set,
1327 or if the current buffer has changed, or if the menubar has never
1328 been updated before.
1330 menubar_changed = (x->menubar_widget
1331 && !XtIsManaged (x->menubar_widget));
1333 if (! (menubar_changed))
1334 return;
1336 BLOCK_INPUT;
1337 /* Save the size of the frame because the pane widget doesn't accept to
1338 resize itself. So force it. */
1339 columns = f->width;
1340 rows = f->height;
1342 /* Do the voodoo which means "I'm changing lots of things, don't try to
1343 refigure sizes until I'm done." */
1344 lw_refigure_widget (x->column_widget, False);
1346 /* the order in which children are managed is the top to
1347 bottom order in which they are displayed in the paned window.
1348 First, remove the text-area widget.
1350 XtUnmanageChild (x->edit_widget);
1352 /* remove the menubar that is there now, and put up the menubar that
1353 should be there.
1355 if (menubar_changed)
1357 XtManageChild (x->menubar_widget);
1358 XtMapWidget (x->menubar_widget);
1359 XtVaSetValues (x->menubar_widget, XtNmappedWhenManaged, 1, 0);
1362 /* Re-manage the text-area widget, and then thrash the sizes. */
1363 XtManageChild (x->edit_widget);
1364 lw_refigure_widget (x->column_widget, True);
1366 /* Force the pane widget to resize itself with the right values. */
1367 EmacsFrameSetCharSize (x->edit_widget, columns, rows);
1369 UNBLOCK_INPUT;
1372 void
1373 set_frame_menubar (f, first_time)
1374 FRAME_PTR f;
1375 int first_time;
1377 Widget menubar_widget = f->display.x->menubar_widget;
1378 int id = (int) f;
1379 Lisp_Object tail, items;
1380 widget_value *wv, *first_wv, *prev_wv = 0;
1381 int i;
1383 BLOCK_INPUT;
1385 wv = malloc_widget_value ();
1386 wv->name = "menubar";
1387 wv->value = 0;
1388 wv->enabled = 1;
1389 first_wv = wv;
1390 items = FRAME_MENU_BAR_ITEMS (f);
1391 menu_items = f->menu_bar_vector;
1392 menu_items_allocated = XVECTOR (menu_items)->size;
1393 init_menu_items ();
1395 for (i = 0; i < XVECTOR (items)->size; i += 3)
1397 Lisp_Object key, string, maps;
1399 key = XVECTOR (items)->contents[i];
1400 string = XVECTOR (items)->contents[i + 1];
1401 maps = XVECTOR (items)->contents[i + 2];
1402 if (NILP (string))
1403 break;
1405 wv = single_submenu (key, string, maps);
1406 if (prev_wv)
1407 prev_wv->next = wv;
1408 else
1409 first_wv->contents = wv;
1410 /* Don't set wv->name here; GC during the loop might relocate it. */
1411 wv->enabled = 1;
1412 prev_wv = wv;
1415 /* Now GC cannot happen during the lifetime of the widget_value,
1416 so it's safe to store data from a Lisp_String. */
1417 wv = first_wv->contents;
1418 for (i = 0; i < XVECTOR (items)->size; i += 3)
1420 Lisp_Object string;
1421 string = XVECTOR (items)->contents[i + 1];
1422 if (NILP (string))
1423 break;
1424 wv->name = (char *) XSTRING (string)->data;
1425 wv = wv->next;
1428 finish_menu_items ();
1430 f->menu_bar_vector = menu_items;
1431 f->menu_bar_items_used = menu_items_used;
1432 menu_items = Qnil;
1434 if (menubar_widget)
1436 /* Disable resizing (done for Motif!) */
1437 lw_allow_resizing (f->display.x->widget, False);
1439 /* The third arg is DEEP_P, which says to consider the entire
1440 menu trees we supply, rather than just the menu bar item names. */
1441 lw_modify_all_widgets (id, first_wv, 1);
1443 /* Re-enable the edit widget to resize. */
1444 lw_allow_resizing (f->display.x->widget, True);
1446 else
1448 menubar_widget = lw_create_widget ("menubar", "menubar",
1449 id, first_wv,
1450 f->display.x->column_widget,
1452 popup_activate_callback,
1453 menubar_selection_callback,
1454 popup_deactivate_callback);
1455 f->display.x->menubar_widget = menubar_widget;
1458 free_menubar_widget_value_tree (first_wv);
1460 /* Don't update the menubar the first time it is created via x_window. */
1461 if (!first_time)
1462 update_frame_menubar (f);
1464 UNBLOCK_INPUT;
1467 /* Called from Fx_create_frame to create the inital menubar of a frame
1468 before it is mapped, so that the window is mapped with the menubar already
1469 there instead of us tacking it on later and thrashing the window after it
1470 is visible. */
1472 void
1473 initialize_frame_menubar (f)
1474 FRAME_PTR f;
1476 /* This function is called before the first chance to redisplay
1477 the frame. It has to be, so the frame will have the right size. */
1478 FRAME_MENU_BAR_ITEMS (f) = menu_bar_items (FRAME_MENU_BAR_ITEMS (f));
1479 set_frame_menubar (f, 1);
1482 /* Get rid of the menu bar of frame F, and free its storage.
1483 This is used when deleting a frame, and when turning off the menu bar. */
1485 void
1486 free_frame_menubar (f)
1487 FRAME_PTR f;
1489 Widget menubar_widget;
1490 int id;
1492 menubar_widget = f->display.x->menubar_widget;
1493 id = (int) f;
1495 if (menubar_widget)
1497 BLOCK_INPUT;
1498 lw_destroy_all_widgets (id);
1499 UNBLOCK_INPUT;
1503 #endif /* USE_X_TOOLKIT */
1505 /* xmenu_show actually displays a menu using the panes and items in menu_items
1506 and returns the value selected from it.
1507 There are two versions of xmenu_show, one for Xt and one for Xlib.
1508 Both assume input is blocked by the caller. */
1510 /* F is the frame the menu is for.
1511 X and Y are the frame-relative specified position,
1512 relative to the inside upper left corner of the frame F.
1513 MENUBARP is 1 if the click that asked for this menu came from the menu bar.
1514 KEYMAPS is 1 if this menu was specified with keymaps;
1515 in that case, we return a list containing the chosen item's value
1516 and perhaps also the pane's prefix.
1517 TITLE is the specified menu title.
1518 ERROR is a place to store an error message string in case of failure.
1519 (We return nil on failure, but the value doesn't actually matter.) */
1521 #ifdef USE_X_TOOLKIT
1523 /* We need a unique id for each widget handled by the Lucid Widget
1524 library. This includes the frame main windows, popup menu and
1525 dialog box. */
1526 LWLIB_ID widget_id_tick;
1528 #ifdef __STDC__
1529 static Lisp_Object *volatile menu_item_selection;
1530 #else
1531 static Lisp_Object *menu_item_selection;
1532 #endif
1534 static void
1535 popup_selection_callback (widget, id, client_data)
1536 Widget widget;
1537 LWLIB_ID id;
1538 XtPointer client_data;
1540 menu_item_selection = (Lisp_Object *) client_data;
1543 static Lisp_Object
1544 xmenu_show (f, x, y, menubarp, keymaps, title, error)
1545 FRAME_PTR f;
1546 int x;
1547 int y;
1548 int menubarp; /* Dummy parameter for Xt version of
1549 xmenu_show() */
1550 int keymaps;
1551 Lisp_Object title;
1552 char **error;
1554 int i;
1555 int menu_id;
1556 Widget menu;
1557 Arg av [2];
1558 int ac = 0;
1559 widget_value *wv, *save_wv = 0, *first_wv = 0, *prev_wv = 0;
1560 widget_value **submenu_stack
1561 = (widget_value **) alloca (menu_items_used * sizeof (widget_value *));
1562 Lisp_Object *subprefix_stack
1563 = (Lisp_Object *) alloca (menu_items_used * sizeof (Lisp_Object));
1564 int submenu_depth = 0;
1566 /* Define a queue to save up for later unreading
1567 all X events that don't pertain to the menu. */
1568 struct event_queue
1570 XEvent event;
1571 struct event_queue *next;
1574 struct event_queue *queue = NULL;
1575 struct event_queue *queue_tmp;
1577 Position root_x, root_y;
1579 int first_pane;
1580 int next_release_must_exit = 0;
1582 *error = NULL;
1584 if (menu_items_used <= MENU_ITEMS_PANE_LENGTH)
1586 *error = "Empty menu";
1587 return Qnil;
1590 /* Create a tree of widget_value objects
1591 representing the panes and their items. */
1592 wv = malloc_widget_value ();
1593 wv->name = "menu";
1594 wv->value = 0;
1595 wv->enabled = 1;
1596 first_wv = wv;
1597 first_pane = 1;
1599 /* Loop over all panes and items, filling in the tree. */
1600 i = 0;
1601 while (i < menu_items_used)
1603 if (EQ (XVECTOR (menu_items)->contents[i], Qnil))
1605 submenu_stack[submenu_depth++] = save_wv;
1606 save_wv = prev_wv;
1607 prev_wv = 0;
1608 first_pane = 1;
1609 i++;
1611 else if (EQ (XVECTOR (menu_items)->contents[i], Qlambda))
1613 prev_wv = save_wv;
1614 save_wv = submenu_stack[--submenu_depth];
1615 first_pane = 0;
1616 i++;
1618 else if (EQ (XVECTOR (menu_items)->contents[i], Qt)
1619 && submenu_depth != 0)
1620 i += MENU_ITEMS_PANE_LENGTH;
1621 /* Ignore a nil in the item list.
1622 It's meaningful only for dialog boxes. */
1623 else if (EQ (XVECTOR (menu_items)->contents[i], Qquote))
1624 i += 1;
1625 else if (EQ (XVECTOR (menu_items)->contents[i], Qt))
1627 /* Create a new pane. */
1628 Lisp_Object pane_name, prefix;
1629 char *pane_string;
1630 pane_name = XVECTOR (menu_items)->contents[i + MENU_ITEMS_PANE_NAME];
1631 prefix = XVECTOR (menu_items)->contents[i + MENU_ITEMS_PANE_PREFIX];
1632 pane_string = (NILP (pane_name)
1633 ? "" : (char *) XSTRING (pane_name)->data);
1634 /* If there is just one top-level pane, put all its items directly
1635 under the top-level menu. */
1636 if (menu_items_n_panes == 1)
1637 pane_string = "";
1639 /* If the pane has a meaningful name,
1640 make the pane a top-level menu item
1641 with its items as a submenu beneath it. */
1642 if (!keymaps && strcmp (pane_string, ""))
1644 wv = malloc_widget_value ();
1645 if (save_wv)
1646 save_wv->next = wv;
1647 else
1648 first_wv->contents = wv;
1649 wv->name = pane_string;
1650 if (keymaps && !NILP (prefix))
1651 wv->name++;
1652 wv->value = 0;
1653 wv->enabled = 1;
1654 save_wv = wv;
1655 prev_wv = 0;
1657 else if (first_pane)
1659 save_wv = wv;
1660 prev_wv = 0;
1662 first_pane = 0;
1663 i += MENU_ITEMS_PANE_LENGTH;
1665 else
1667 /* Create a new item within current pane. */
1668 Lisp_Object item_name, enable, descrip;
1669 item_name = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_NAME];
1670 enable = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_ENABLE];
1671 descrip
1672 = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_EQUIV_KEY];
1674 wv = malloc_widget_value ();
1675 if (prev_wv)
1676 prev_wv->next = wv;
1677 else
1678 save_wv->contents = wv;
1679 wv->name = (char *) XSTRING (item_name)->data;
1680 if (!NILP (descrip))
1681 wv->key = (char *) XSTRING (descrip)->data;
1682 wv->value = 0;
1683 wv->call_data = (void *) &XVECTOR (menu_items)->contents[i];
1684 wv->enabled = !NILP (enable);
1685 prev_wv = wv;
1687 i += MENU_ITEMS_ITEM_LENGTH;
1691 /* Deal with the title, if it is non-nil. */
1692 if (!NILP (title))
1694 widget_value *wv_title = malloc_widget_value ();
1695 widget_value *wv_sep1 = malloc_widget_value ();
1696 widget_value *wv_sep2 = malloc_widget_value ();
1698 wv_sep2->name = "--";
1699 wv_sep2->next = first_wv->contents;
1701 wv_sep1->name = "--";
1702 wv_sep1->next = wv_sep2;
1704 wv_title->name = (char *) XSTRING (title)->data;
1705 wv_title->enabled = True;
1706 wv_title->next = wv_sep1;
1707 first_wv->contents = wv_title;
1710 /* Actually create the menu. */
1711 menu_id = ++widget_id_tick;
1712 menu = lw_create_widget ("popup", first_wv->name, menu_id, first_wv,
1713 f->display.x->widget, 1, 0,
1714 popup_selection_callback,
1715 popup_deactivate_callback);
1717 /* Don't allow any geometry request from the user. */
1718 XtSetArg (av[ac], XtNgeometry, 0); ac++;
1719 XtSetValues (menu, av, ac);
1721 /* Free the widget_value objects we used to specify the contents. */
1722 free_menubar_widget_value_tree (first_wv);
1724 /* No selection has been chosen yet. */
1725 menu_item_selection = 0;
1727 /* Display the menu. */
1728 lw_popup_menu (menu);
1729 popup_activated_flag = 1;
1731 /* Process events that apply to the menu. */
1732 popup_get_selection ((XEvent *) 0);
1734 pop_down:
1735 /* fp turned off the following statement and wrote a comment
1736 that it is unnecessary--that the menu has already disappeared.
1737 I observer that is not so. -- rms. */
1738 /* Make sure the menu disappears. */
1739 lw_destroy_all_widgets (menu_id);
1741 /* Unread any events that we got but did not handle. */
1742 while (queue != NULL)
1744 queue_tmp = queue;
1745 XPutBackEvent (FRAME_X_DISPLAY (f), &queue_tmp->event);
1746 queue = queue_tmp->next;
1747 free ((char *)queue_tmp);
1748 /* Cause these events to get read as soon as we UNBLOCK_INPUT. */
1749 interrupt_input_pending = 1;
1752 /* Find the selected item, and its pane, to return
1753 the proper value. */
1754 if (menu_item_selection != 0)
1756 Lisp_Object prefix;
1758 prefix = Qnil;
1759 i = 0;
1760 while (i < menu_items_used)
1762 Lisp_Object entry;
1764 if (EQ (XVECTOR (menu_items)->contents[i], Qnil))
1766 subprefix_stack[submenu_depth++] = prefix;
1767 prefix = entry;
1768 i++;
1770 else if (EQ (XVECTOR (menu_items)->contents[i], Qlambda))
1772 prefix = subprefix_stack[--submenu_depth];
1773 i++;
1775 else if (EQ (XVECTOR (menu_items)->contents[i], Qt))
1777 prefix
1778 = XVECTOR (menu_items)->contents[i + MENU_ITEMS_PANE_PREFIX];
1779 i += MENU_ITEMS_PANE_LENGTH;
1781 else
1783 entry
1784 = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_VALUE];
1785 if (menu_item_selection == &XVECTOR (menu_items)->contents[i])
1787 if (keymaps != 0)
1789 int j;
1791 entry = Fcons (entry, Qnil);
1792 if (!NILP (prefix))
1793 entry = Fcons (prefix, entry);
1794 for (j = submenu_depth - 1; j >= 0; j--)
1795 if (!NILP (subprefix_stack[j]))
1796 entry = Fcons (subprefix_stack[j], entry);
1798 return entry;
1800 i += MENU_ITEMS_ITEM_LENGTH;
1805 return Qnil;
1808 static void
1809 dialog_selection_callback (widget, id, client_data)
1810 Widget widget;
1811 LWLIB_ID id;
1812 XtPointer client_data;
1814 if ((int)client_data != -1)
1815 menu_item_selection = (Lisp_Object *) client_data;
1816 BLOCK_INPUT;
1817 lw_destroy_all_widgets (id);
1818 UNBLOCK_INPUT;
1821 static char * button_names [] = {
1822 "button1", "button2", "button3", "button4", "button5",
1823 "button6", "button7", "button8", "button9", "button10" };
1825 static Lisp_Object
1826 xdialog_show (f, menubarp, keymaps, title, error)
1827 FRAME_PTR f;
1828 int menubarp;
1829 int keymaps;
1830 Lisp_Object title;
1831 char **error;
1833 int i, nb_buttons=0;
1834 int dialog_id;
1835 Widget menu;
1836 char dialog_name[6];
1838 widget_value *wv, *save_wv = 0, *first_wv = 0, *prev_wv = 0;
1840 /* Define a queue to save up for later unreading
1841 all X events that don't pertain to the menu. */
1842 struct event_queue
1844 XEvent event;
1845 struct event_queue *next;
1848 struct event_queue *queue = NULL;
1849 struct event_queue *queue_tmp;
1851 /* Number of elements seen so far, before boundary. */
1852 int left_count = 0;
1853 /* 1 means we've seen the boundary between left-hand elts and right-hand. */
1854 int boundary_seen = 0;
1856 *error = NULL;
1858 if (menu_items_n_panes > 1)
1860 *error = "Multiple panes in dialog box";
1861 return Qnil;
1864 /* Create a tree of widget_value objects
1865 representing the text label and buttons. */
1867 Lisp_Object pane_name, prefix;
1868 char *pane_string;
1869 pane_name = XVECTOR (menu_items)->contents[MENU_ITEMS_PANE_NAME];
1870 prefix = XVECTOR (menu_items)->contents[MENU_ITEMS_PANE_PREFIX];
1871 pane_string = (NILP (pane_name)
1872 ? "" : (char *) XSTRING (pane_name)->data);
1873 prev_wv = malloc_widget_value ();
1874 prev_wv->value = pane_string;
1875 if (keymaps && !NILP (prefix))
1876 prev_wv->name++;
1877 prev_wv->enabled = 1;
1878 prev_wv->name = "message";
1879 first_wv = prev_wv;
1881 /* Loop over all panes and items, filling in the tree. */
1882 i = MENU_ITEMS_PANE_LENGTH;
1883 while (i < menu_items_used)
1886 /* Create a new item within current pane. */
1887 Lisp_Object item_name, enable, descrip;
1888 item_name = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_NAME];
1889 enable = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_ENABLE];
1890 descrip
1891 = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_EQUIV_KEY];
1893 if (NILP (item_name))
1895 free_menubar_widget_value_tree (first_wv);
1896 *error = "Submenu in dialog items";
1897 return Qnil;
1899 if (EQ (item_name, Qquote))
1901 /* This is the boundary between left-side elts
1902 and right-side elts. Stop incrementing right_count. */
1903 boundary_seen = 1;
1904 i++;
1905 continue;
1907 if (nb_buttons >= 10)
1909 free_menubar_widget_value_tree (first_wv);
1910 *error = "Too many dialog items";
1911 return Qnil;
1914 wv = malloc_widget_value ();
1915 prev_wv->next = wv;
1916 wv->name = (char *) button_names[nb_buttons];
1917 if (!NILP (descrip))
1918 wv->key = (char *) XSTRING (descrip)->data;
1919 wv->value = (char *) XSTRING (item_name)->data;
1920 wv->call_data = (void *) &XVECTOR (menu_items)->contents[i];
1921 wv->enabled = !NILP (enable);
1922 prev_wv = wv;
1924 if (! boundary_seen)
1925 left_count++;
1927 nb_buttons++;
1928 i += MENU_ITEMS_ITEM_LENGTH;
1931 /* If the boundary was not specified,
1932 by default put half on the left and half on the right. */
1933 if (! boundary_seen)
1934 left_count = nb_buttons - nb_buttons / 2;
1936 wv = malloc_widget_value ();
1937 wv->name = dialog_name;
1939 /* Dialog boxes use a really stupid name encoding
1940 which specifies how many buttons to use
1941 and how many buttons are on the right.
1942 The Q means something also. */
1943 dialog_name[0] = 'Q';
1944 dialog_name[1] = '0' + nb_buttons;
1945 dialog_name[2] = 'B';
1946 dialog_name[3] = 'R';
1947 /* Number of buttons to put on the right. */
1948 dialog_name[4] = '0' + nb_buttons - left_count;
1949 dialog_name[5] = 0;
1950 wv->contents = first_wv;
1951 first_wv = wv;
1954 /* Actually create the dialog. */
1955 dialog_id = ++widget_id_tick;
1956 menu = lw_create_widget (first_wv->name, "dialog", dialog_id, first_wv,
1957 f->display.x->widget, 1, 0,
1958 dialog_selection_callback, 0);
1959 lw_modify_all_widgets (dialog_id, first_wv->contents, True);
1960 /* Free the widget_value objects we used to specify the contents. */
1961 free_menubar_widget_value_tree (first_wv);
1963 /* No selection has been chosen yet. */
1964 menu_item_selection = 0;
1966 /* Display the menu. */
1967 lw_pop_up_all_widgets (dialog_id);
1969 /* Process events that apply to the menu. */
1970 while (1)
1972 XEvent event;
1974 XtAppNextEvent (Xt_app_con, &event);
1975 if (event.type == ButtonRelease)
1977 XtDispatchEvent (&event);
1978 break;
1980 else if (event.type == Expose)
1981 process_expose_from_menu (event);
1982 XtDispatchEvent (&event);
1983 if (XtWindowToWidget (FRAME_X_DISPLAY (f), event.xany.window) != menu)
1985 queue_tmp = (struct event_queue *) malloc (sizeof (struct event_queue));
1987 if (queue_tmp != NULL)
1989 queue_tmp->event = event;
1990 queue_tmp->next = queue;
1991 queue = queue_tmp;
1995 pop_down:
1997 #ifdef HAVE_X_WINDOWS
1998 /* State that no mouse buttons are now held.
1999 That is not necessarily true, but the fiction leads to reasonable
2000 results, and it is a pain to ask which are actually held now
2001 or track this in the loop above. */
2002 FRAME_X_DISPLAY_INFO (f)->grabbed = 0;
2003 #endif
2005 /* Unread any events that we got but did not handle. */
2006 while (queue != NULL)
2008 queue_tmp = queue;
2009 XPutBackEvent (FRAME_X_DISPLAY (f), &queue_tmp->event);
2010 queue = queue_tmp->next;
2011 free ((char *)queue_tmp);
2012 /* Cause these events to get read as soon as we UNBLOCK_INPUT. */
2013 interrupt_input_pending = 1;
2016 /* Find the selected item, and its pane, to return
2017 the proper value. */
2018 if (menu_item_selection != 0)
2020 Lisp_Object prefix;
2022 prefix = Qnil;
2023 i = 0;
2024 while (i < menu_items_used)
2026 Lisp_Object entry;
2028 if (EQ (XVECTOR (menu_items)->contents[i], Qt))
2030 prefix
2031 = XVECTOR (menu_items)->contents[i + MENU_ITEMS_PANE_PREFIX];
2032 i += MENU_ITEMS_PANE_LENGTH;
2034 else
2036 entry
2037 = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_VALUE];
2038 if (menu_item_selection == &XVECTOR (menu_items)->contents[i])
2040 if (keymaps != 0)
2042 entry = Fcons (entry, Qnil);
2043 if (!NILP (prefix))
2044 entry = Fcons (prefix, entry);
2046 return entry;
2048 i += MENU_ITEMS_ITEM_LENGTH;
2053 return Qnil;
2055 #else /* not USE_X_TOOLKIT */
2057 static Lisp_Object
2058 xmenu_show (f, x, y, menubarp, keymaps, title, error)
2059 FRAME_PTR f;
2060 int x, y;
2061 int keymaps;
2062 int menubarp;
2063 Lisp_Object title;
2064 char **error;
2066 Window root;
2067 XMenu *menu;
2068 int pane, selidx, lpane, status;
2069 Lisp_Object entry, pane_prefix;
2070 char *datap;
2071 int ulx, uly, width, height;
2072 int dispwidth, dispheight;
2073 int i, j;
2074 int maxwidth;
2075 int dummy_int;
2076 unsigned int dummy_uint;
2078 *error = 0;
2079 if (menu_items_n_panes == 0)
2080 return Qnil;
2082 if (menu_items_used <= MENU_ITEMS_PANE_LENGTH)
2084 *error = "Empty menu";
2085 return Qnil;
2088 /* Figure out which root window F is on. */
2089 XGetGeometry (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), &root,
2090 &dummy_int, &dummy_int, &dummy_uint, &dummy_uint,
2091 &dummy_uint, &dummy_uint);
2093 /* Make the menu on that window. */
2094 menu = XMenuCreate (FRAME_X_DISPLAY (f), root, "emacs");
2095 if (menu == NULL)
2097 *error = "Can't create menu";
2098 return Qnil;
2101 #ifdef HAVE_X_WINDOWS
2102 /* Adjust coordinates to relative to the outer (window manager) window. */
2104 Window child;
2105 int win_x = 0, win_y = 0;
2107 /* Find the position of the outside upper-left corner of
2108 the inner window, with respect to the outer window. */
2109 if (f->display.x->parent_desc != FRAME_X_DISPLAY_INFO (f)->root_window)
2111 BLOCK_INPUT;
2112 XTranslateCoordinates (FRAME_X_DISPLAY (f),
2114 /* From-window, to-window. */
2115 f->display.x->window_desc,
2116 f->display.x->parent_desc,
2118 /* From-position, to-position. */
2119 0, 0, &win_x, &win_y,
2121 /* Child of window. */
2122 &child);
2123 UNBLOCK_INPUT;
2124 x += win_x;
2125 y += win_y;
2128 #endif /* HAVE_X_WINDOWS */
2130 /* Adjust coordinates to be root-window-relative. */
2131 x += f->display.x->left_pos;
2132 y += f->display.x->top_pos;
2134 /* Create all the necessary panes and their items. */
2135 i = 0;
2136 while (i < menu_items_used)
2138 if (EQ (XVECTOR (menu_items)->contents[i], Qt))
2140 /* Create a new pane. */
2141 Lisp_Object pane_name, prefix;
2142 char *pane_string;
2144 pane_name = XVECTOR (menu_items)->contents[i + MENU_ITEMS_PANE_NAME];
2145 prefix = XVECTOR (menu_items)->contents[i + MENU_ITEMS_PANE_PREFIX];
2146 pane_string = (NILP (pane_name)
2147 ? "" : (char *) XSTRING (pane_name)->data);
2148 if (keymaps && !NILP (prefix))
2149 pane_string++;
2151 lpane = XMenuAddPane (FRAME_X_DISPLAY (f), menu, pane_string, TRUE);
2152 if (lpane == XM_FAILURE)
2154 XMenuDestroy (FRAME_X_DISPLAY (f), menu);
2155 *error = "Can't create pane";
2156 return Qnil;
2158 i += MENU_ITEMS_PANE_LENGTH;
2160 /* Find the width of the widest item in this pane. */
2161 maxwidth = 0;
2162 j = i;
2163 while (j < menu_items_used)
2165 Lisp_Object item;
2166 item = XVECTOR (menu_items)->contents[j];
2167 if (EQ (item, Qt))
2168 break;
2169 if (NILP (item))
2171 j++;
2172 continue;
2174 width = XSTRING (item)->size;
2175 if (width > maxwidth)
2176 maxwidth = width;
2178 j += MENU_ITEMS_ITEM_LENGTH;
2181 /* Ignore a nil in the item list.
2182 It's meaningful only for dialog boxes. */
2183 else if (EQ (XVECTOR (menu_items)->contents[i], Qquote))
2184 i += 1;
2185 else
2187 /* Create a new item within current pane. */
2188 Lisp_Object item_name, enable, descrip;
2189 unsigned char *item_data;
2191 item_name = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_NAME];
2192 enable = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_ENABLE];
2193 descrip
2194 = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_EQUIV_KEY];
2195 if (!NILP (descrip))
2197 int gap = maxwidth - XSTRING (item_name)->size;
2198 #ifdef C_ALLOCA
2199 Lisp_Object spacer;
2200 spacer = Fmake_string (make_number (gap), make_number (' '));
2201 item_name = concat2 (item_name, spacer);
2202 item_name = concat2 (item_name, descrip);
2203 item_data = XSTRING (item_name)->data;
2204 #else
2205 /* if alloca is fast, use that to make the space,
2206 to reduce gc needs. */
2207 item_data
2208 = (unsigned char *) alloca (maxwidth
2209 + XSTRING (descrip)->size + 1);
2210 bcopy (XSTRING (item_name)->data, item_data,
2211 XSTRING (item_name)->size);
2212 for (j = XSTRING (item_name)->size; j < maxwidth; j++)
2213 item_data[j] = ' ';
2214 bcopy (XSTRING (descrip)->data, item_data + j,
2215 XSTRING (descrip)->size);
2216 item_data[j + XSTRING (descrip)->size] = 0;
2217 #endif
2219 else
2220 item_data = XSTRING (item_name)->data;
2222 if (XMenuAddSelection (FRAME_X_DISPLAY (f),
2223 menu, lpane, 0, item_data,
2224 !NILP (enable))
2225 == XM_FAILURE)
2227 XMenuDestroy (FRAME_X_DISPLAY (f), menu);
2228 *error = "Can't add selection to menu";
2229 return Qnil;
2231 i += MENU_ITEMS_ITEM_LENGTH;
2235 /* All set and ready to fly. */
2236 XMenuRecompute (FRAME_X_DISPLAY (f), menu);
2237 dispwidth = DisplayWidth (FRAME_X_DISPLAY (f),
2238 XScreenNumberOfScreen (FRAME_X_SCREEN (f)));
2239 dispheight = DisplayHeight (FRAME_X_DISPLAY (f),
2240 XScreenNumberOfScreen (FRAME_X_SCREEN (f)));
2241 x = min (x, dispwidth);
2242 y = min (y, dispheight);
2243 x = max (x, 1);
2244 y = max (y, 1);
2245 XMenuLocate (FRAME_X_DISPLAY (f), menu, 0, 0, x, y,
2246 &ulx, &uly, &width, &height);
2247 if (ulx+width > dispwidth)
2249 x -= (ulx + width) - dispwidth;
2250 ulx = dispwidth - width;
2252 if (uly+height > dispheight)
2254 y -= (uly + height) - dispheight;
2255 uly = dispheight - height;
2257 if (ulx < 0) x -= ulx;
2258 if (uly < 0) y -= uly;
2260 XMenuSetAEQ (menu, TRUE);
2261 XMenuSetFreeze (menu, TRUE);
2262 pane = selidx = 0;
2264 status = XMenuActivate (FRAME_X_DISPLAY (f), menu, &pane, &selidx,
2265 x, y, ButtonReleaseMask, &datap);
2266 switch (status)
2268 case XM_SUCCESS:
2269 #ifdef XDEBUG
2270 fprintf (stderr, "pane= %d line = %d\n", panes, selidx);
2271 #endif
2273 /* Find the item number SELIDX in pane number PANE. */
2274 i = 0;
2275 while (i < menu_items_used)
2277 if (EQ (XVECTOR (menu_items)->contents[i], Qt))
2279 if (pane == 0)
2280 pane_prefix
2281 = XVECTOR (menu_items)->contents[i + MENU_ITEMS_PANE_PREFIX];
2282 pane--;
2283 i += MENU_ITEMS_PANE_LENGTH;
2285 else
2287 if (pane == -1)
2289 if (selidx == 0)
2291 entry
2292 = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_VALUE];
2293 if (keymaps != 0)
2295 entry = Fcons (entry, Qnil);
2296 if (!NILP (pane_prefix))
2297 entry = Fcons (pane_prefix, entry);
2299 break;
2301 selidx--;
2303 i += MENU_ITEMS_ITEM_LENGTH;
2306 break;
2308 case XM_FAILURE:
2309 *error = "Can't activate menu";
2310 case XM_IA_SELECT:
2311 case XM_NO_SELECT:
2312 entry = Qnil;
2313 break;
2315 XMenuDestroy (FRAME_X_DISPLAY (f), menu);
2317 #ifdef HAVE_X_WINDOWS
2318 /* State that no mouse buttons are now held.
2319 (The oldXMenu code doesn't track this info for us.)
2320 That is not necessarily true, but the fiction leads to reasonable
2321 results, and it is a pain to ask which are actually held now. */
2322 FRAME_X_DISPLAY_INFO (f)->grabbed = 0;
2323 #endif
2325 return entry;
2328 #endif /* not USE_X_TOOLKIT */
2330 syms_of_xmenu ()
2332 staticpro (&menu_items);
2333 menu_items = Qnil;
2335 #ifdef USE_X_TOOLKIT
2336 widget_id_tick = (1<<16);
2337 #endif
2339 defsubr (&Sx_popup_menu);
2340 defsubr (&Sx_popup_dialog);