(ewoc-location): New function.
[emacs.git] / src / xmenu.c
blobdb20b6b0b61b64bea976592d9baed6f030951cf9
1 /* X Communication module for terminals which understand the X protocol.
2 Copyright (C) 1986, 88, 93, 94, 96, 99, 2000 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, Inc., 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA. */
21 /* X pop-up deck-of-cards menu facility for gnuemacs.
23 * Written by Jon Arnold and Roman Budzianowski
24 * Mods and rewrite by Robert Krawitz
28 /* Modified by Fred Pierresteguy on December 93
29 to make the popup menus and menubar use the Xt. */
31 /* Rewritten for clarity and GC protection by rms in Feb 94. */
33 #include <config.h>
35 /* On 4.3 this loses if it comes after xterm.h. */
36 #include <signal.h>
38 #include <stdio.h>
39 #include "lisp.h"
40 #include "termhooks.h"
41 #include "frame.h"
42 #include "window.h"
43 #include "keyboard.h"
44 #include "blockinput.h"
45 #include "buffer.h"
47 #ifdef MSDOS
48 #include "msdos.h"
49 #endif
51 #ifdef HAVE_X_WINDOWS
52 /* This may include sys/types.h, and that somehow loses
53 if this is not done before the other system files. */
54 #include "xterm.h"
55 #endif
57 /* Load sys/types.h if not already loaded.
58 In some systems loading it twice is suicidal. */
59 #ifndef makedev
60 #include <sys/types.h>
61 #endif
63 #include "dispextern.h"
65 #ifdef HAVE_X_WINDOWS
66 #undef HAVE_MULTILINGUAL_MENU
67 #ifdef USE_X_TOOLKIT
68 #include <X11/Xlib.h>
69 #include <X11/IntrinsicP.h>
70 #include <X11/CoreP.h>
71 #include <X11/StringDefs.h>
72 #include <X11/Shell.h>
73 #ifdef USE_LUCID
74 #include <X11/Xaw/Paned.h>
75 #endif /* USE_LUCID */
76 #include "../lwlib/lwlib.h"
77 #else /* not USE_X_TOOLKIT */
78 #include "../oldXMenu/XMenu.h"
79 #endif /* not USE_X_TOOLKIT */
80 #endif /* HAVE_X_WINDOWS */
82 #ifdef USE_MOTIF
83 #include <Xm/Xm.h> /* for LESSTIF_VERSION */
84 #endif
86 #define min(x,y) (((x) < (y)) ? (x) : (y))
87 #define max(x,y) (((x) > (y)) ? (x) : (y))
89 #ifndef TRUE
90 #define TRUE 1
91 #define FALSE 0
92 #endif /* no TRUE */
94 Lisp_Object Vmenu_updating_frame;
96 Lisp_Object Qdebug_on_next_call;
98 extern Lisp_Object Qmenu_bar;
99 extern Lisp_Object Qmouse_click, Qevent_kind;
101 extern Lisp_Object QCtoggle, QCradio;
103 extern Lisp_Object Voverriding_local_map;
104 extern Lisp_Object Voverriding_local_map_menu_flag;
106 extern Lisp_Object Qoverriding_local_map, Qoverriding_terminal_local_map;
108 extern Lisp_Object Qmenu_bar_update_hook;
110 #ifdef USE_X_TOOLKIT
111 extern void set_frame_menubar ();
112 extern void process_expose_from_menu ();
113 extern XtAppContext Xt_app_con;
115 static Lisp_Object xdialog_show ();
116 void popup_get_selection ();
117 #endif
119 #ifdef USE_X_TOOLKIT
121 /* Define HAVE_BOXES if meus can handle radio and toggle buttons. */
123 #define HAVE_BOXES 1
124 #endif
126 static void push_menu_item P_ ((Lisp_Object, Lisp_Object, Lisp_Object,
127 Lisp_Object, Lisp_Object, Lisp_Object,
128 Lisp_Object, Lisp_Object));
129 static Lisp_Object xmenu_show ();
130 static void keymap_panes ();
131 static void single_keymap_panes ();
132 static void single_menu_item ();
133 static void list_of_panes ();
134 static void list_of_items ();
136 /* This holds a Lisp vector that holds the results of decoding
137 the keymaps or alist-of-alists that specify a menu.
139 It describes the panes and items within the panes.
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.
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.
152 A single vector slot containing quote indicates that the
153 following items should appear on the right of a dialog box.
155 Using a Lisp vector to hold this information while we decode it
156 takes care of protecting all the data from GC. */
158 #define MENU_ITEMS_PANE_NAME 1
159 #define MENU_ITEMS_PANE_PREFIX 2
160 #define MENU_ITEMS_PANE_LENGTH 3
162 enum menu_item_idx
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
175 static Lisp_Object menu_items;
177 /* Number of slots currently allocated in menu_items. */
178 static int menu_items_allocated;
180 /* This is the index in menu_items of the first empty slot. */
181 static int menu_items_used;
183 /* The number of panes currently recorded in menu_items,
184 excluding those within submenus. */
185 static int menu_items_n_panes;
187 /* Current depth within submenus. */
188 static int menu_items_submenu_depth;
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;
194 static int next_menubar_widget_id;
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.
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>. */
203 int pending_menu_activation;
205 #ifdef USE_X_TOOLKIT
207 /* Return the frame whose ->output_data.x->id equals ID, or 0 if none. */
209 static struct frame *
210 menubar_id_to_frame (id)
211 LWLIB_ID id;
213 Lisp_Object tail, frame;
214 FRAME_PTR f;
216 for (tail = Vframe_list; GC_CONSP (tail); tail = XCDR (tail))
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;
227 return 0;
230 #endif
232 /* Initialize the menu_items structure if we haven't already done so.
233 Also mark it as currently empty. */
235 static void
236 init_menu_items ()
238 if (NILP (menu_items))
240 menu_items_allocated = 60;
241 menu_items = Fmake_vector (make_number (menu_items_allocated), Qnil);
244 menu_items_used = 0;
245 menu_items_n_panes = 0;
246 menu_items_submenu_depth = 0;
249 /* Call at the end of generating the data in menu_items.
250 This fills in the number of items in the last pane. */
252 static void
253 finish_menu_items ()
257 /* Call when finished using the data for the current menu
258 in menu_items. */
260 static void
261 discard_menu_items ()
263 /* Free the structure if it is especially large.
264 Otherwise, hold on to it, to save time. */
265 if (menu_items_allocated > 200)
267 menu_items = Qnil;
268 menu_items_allocated = 0;
272 /* Make the menu_items vector twice as large. */
274 static void
275 grow_menu_items ()
277 Lisp_Object old;
278 int old_size = menu_items_allocated;
279 old = menu_items;
281 menu_items_allocated *= 2;
282 menu_items = Fmake_vector (make_number (menu_items_allocated), Qnil);
283 bcopy (XVECTOR (old)->contents, XVECTOR (menu_items)->contents,
284 old_size * sizeof (Lisp_Object));
287 /* Begin a submenu. */
289 static void
290 push_submenu_start ()
292 if (menu_items_used + 1 > menu_items_allocated)
293 grow_menu_items ();
295 XVECTOR (menu_items)->contents[menu_items_used++] = Qnil;
296 menu_items_submenu_depth++;
299 /* End a submenu. */
301 static void
302 push_submenu_end ()
304 if (menu_items_used + 1 > menu_items_allocated)
305 grow_menu_items ();
307 XVECTOR (menu_items)->contents[menu_items_used++] = Qlambda;
308 menu_items_submenu_depth--;
311 /* Indicate boundary between left and right. */
313 static void
314 push_left_right_boundary ()
316 if (menu_items_used + 1 > menu_items_allocated)
317 grow_menu_items ();
319 XVECTOR (menu_items)->contents[menu_items_used++] = Qquote;
322 /* Start a new menu pane in menu_items..
323 NAME is the pane name. PREFIX_VEC is a prefix key for this pane. */
325 static void
326 push_menu_pane (name, prefix_vec)
327 Lisp_Object name, prefix_vec;
329 if (menu_items_used + MENU_ITEMS_PANE_LENGTH > menu_items_allocated)
330 grow_menu_items ();
332 if (menu_items_submenu_depth == 0)
333 menu_items_n_panes++;
334 XVECTOR (menu_items)->contents[menu_items_used++] = Qt;
335 XVECTOR (menu_items)->contents[menu_items_used++] = name;
336 XVECTOR (menu_items)->contents[menu_items_used++] = prefix_vec;
339 /* Push one menu item into the current pane. NAME is the string to
340 display. ENABLE if non-nil means this item can be selected. KEY
341 is the key generated by choosing this item, or nil if this item
342 doesn't really have a definition. DEF is the definition of this
343 item. EQUIV is the textual description of the keyboard equivalent
344 for this item (or nil if none). TYPE is the type of this menu
345 item, one of nil, `toggle' or `radio'. */
347 static void
348 push_menu_item (name, enable, key, def, equiv, type, selected, help)
349 Lisp_Object name, enable, key, def, equiv, type, selected, help;
351 if (menu_items_used + MENU_ITEMS_ITEM_LENGTH > menu_items_allocated)
352 grow_menu_items ();
354 XVECTOR (menu_items)->contents[menu_items_used++] = name;
355 XVECTOR (menu_items)->contents[menu_items_used++] = enable;
356 XVECTOR (menu_items)->contents[menu_items_used++] = key;
357 XVECTOR (menu_items)->contents[menu_items_used++] = equiv;
358 XVECTOR (menu_items)->contents[menu_items_used++] = def;
359 XVECTOR (menu_items)->contents[menu_items_used++] = type;
360 XVECTOR (menu_items)->contents[menu_items_used++] = selected;
361 XVECTOR (menu_items)->contents[menu_items_used++] = help;
364 /* Look through KEYMAPS, a vector of keymaps that is NMAPS long,
365 and generate menu panes for them in menu_items.
366 If NOTREAL is nonzero,
367 don't bother really computing whether an item is enabled. */
369 static void
370 keymap_panes (keymaps, nmaps, notreal)
371 Lisp_Object *keymaps;
372 int nmaps;
373 int notreal;
375 int mapno;
377 init_menu_items ();
379 /* Loop over the given keymaps, making a pane for each map.
380 But don't make a pane that is empty--ignore that map instead.
381 P is the number of panes we have made so far. */
382 for (mapno = 0; mapno < nmaps; mapno++)
383 single_keymap_panes (keymaps[mapno],
384 map_prompt (keymaps[mapno]), Qnil, notreal, 10);
386 finish_menu_items ();
389 /* This is a recursive subroutine of keymap_panes.
390 It handles one keymap, KEYMAP.
391 The other arguments are passed along
392 or point to local variables of the previous function.
393 If NOTREAL is nonzero, only check for equivalent key bindings, don't
394 evaluate expressions in menu items and don't make any menu.
396 If we encounter submenus deeper than MAXDEPTH levels, ignore them. */
398 static void
399 single_keymap_panes (keymap, pane_name, prefix, notreal, maxdepth)
400 Lisp_Object keymap;
401 Lisp_Object pane_name;
402 Lisp_Object prefix;
403 int notreal;
404 int maxdepth;
406 Lisp_Object pending_maps = Qnil;
407 Lisp_Object tail, item;
408 struct gcpro gcpro1, gcpro2;
409 int notbuttons = 0;
411 if (maxdepth <= 0)
412 return;
414 push_menu_pane (pane_name, prefix);
416 #ifndef HAVE_BOXES
417 /* Remember index for first item in this pane so we can go back and
418 add a prefix when (if) we see the first button. After that, notbuttons
419 is set to 0, to mark that we have seen a button and all non button
420 items need a prefix. */
421 notbuttons = menu_items_used;
422 #endif
424 for (tail = keymap; CONSP (tail); tail = XCDR (tail))
426 GCPRO2 (keymap, pending_maps);
427 /* Look at each key binding, and if it is a menu item add it
428 to this menu. */
429 item = XCAR (tail);
430 if (CONSP (item))
431 single_menu_item (XCAR (item), XCDR (item),
432 &pending_maps, notreal, maxdepth, &notbuttons);
433 else if (VECTORP (item))
435 /* Loop over the char values represented in the vector. */
436 int len = XVECTOR (item)->size;
437 int c;
438 for (c = 0; c < len; c++)
440 Lisp_Object character;
441 XSETFASTINT (character, c);
442 single_menu_item (character, XVECTOR (item)->contents[c],
443 &pending_maps, notreal, maxdepth, &notbuttons);
446 UNGCPRO;
449 /* Process now any submenus which want to be panes at this level. */
450 while (!NILP (pending_maps))
452 Lisp_Object elt, eltcdr, string;
453 elt = Fcar (pending_maps);
454 eltcdr = XCDR (elt);
455 string = XCAR (eltcdr);
456 /* We no longer discard the @ from the beginning of the string here.
457 Instead, we do this in xmenu_show. */
458 single_keymap_panes (Fcar (elt), string,
459 XCDR (eltcdr), notreal, maxdepth - 1);
460 pending_maps = Fcdr (pending_maps);
464 /* This is a subroutine of single_keymap_panes that handles one
465 keymap entry.
466 KEY is a key in a keymap and ITEM is its binding.
467 PENDING_MAPS_PTR points to a list of keymaps waiting to be made into
468 separate panes.
469 If NOTREAL is nonzero, only check for equivalent key bindings, don't
470 evaluate expressions in menu items and don't make any menu.
471 If we encounter submenus deeper than MAXDEPTH levels, ignore them.
472 NOTBUTTONS_PTR is only used when simulating toggle boxes and radio
473 buttons. It points to variable notbuttons in single_keymap_panes,
474 which keeps track of if we have seen a button in this menu or not. */
476 static void
477 single_menu_item (key, item, pending_maps_ptr, notreal, maxdepth,
478 notbuttons_ptr)
479 Lisp_Object key, item;
480 Lisp_Object *pending_maps_ptr;
481 int maxdepth, notreal;
482 int *notbuttons_ptr;
484 Lisp_Object map, item_string, enabled;
485 struct gcpro gcpro1, gcpro2;
486 int res;
488 /* Parse the menu item and leave the result in item_properties. */
489 GCPRO2 (key, item);
490 res = parse_menu_item (item, notreal, 0);
491 UNGCPRO;
492 if (!res)
493 return; /* Not a menu item. */
495 map = XVECTOR (item_properties)->contents[ITEM_PROPERTY_MAP];
497 if (notreal)
499 /* We don't want to make a menu, just traverse the keymaps to
500 precompute equivalent key bindings. */
501 if (!NILP (map))
502 single_keymap_panes (map, Qnil, key, 1, maxdepth - 1);
503 return;
506 enabled = XVECTOR (item_properties)->contents[ITEM_PROPERTY_ENABLE];
507 item_string = XVECTOR (item_properties)->contents[ITEM_PROPERTY_NAME];
509 if (!NILP (map) && XSTRING (item_string)->data[0] == '@')
511 if (!NILP (enabled))
512 /* An enabled separate pane. Remember this to handle it later. */
513 *pending_maps_ptr = Fcons (Fcons (map, Fcons (item_string, key)),
514 *pending_maps_ptr);
515 return;
518 #ifndef HAVE_BOXES
519 /* Simulate radio buttons and toggle boxes by putting a prefix in
520 front of them. */
522 Lisp_Object prefix = Qnil;
523 Lisp_Object type = XVECTOR (item_properties)->contents[ITEM_PROPERTY_TYPE];
524 if (!NILP (type))
526 Lisp_Object selected
527 = XVECTOR (item_properties)->contents[ITEM_PROPERTY_SELECTED];
529 if (*notbuttons_ptr)
530 /* The first button. Line up previous items in this menu. */
532 int index = *notbuttons_ptr; /* Index for first item this menu. */
533 int submenu = 0;
534 Lisp_Object tem;
535 while (index < menu_items_used)
538 = XVECTOR (menu_items)->contents[index + MENU_ITEMS_ITEM_NAME];
539 if (NILP (tem))
541 index++;
542 submenu++; /* Skip sub menu. */
544 else if (EQ (tem, Qlambda))
546 index++;
547 submenu--; /* End sub menu. */
549 else if (EQ (tem, Qt))
550 index += 3; /* Skip new pane marker. */
551 else if (EQ (tem, Qquote))
552 index++; /* Skip a left, right divider. */
553 else
555 if (!submenu && XSTRING (tem)->data[0] != '\0'
556 && XSTRING (tem)->data[0] != '-')
557 XVECTOR (menu_items)->contents[index + MENU_ITEMS_ITEM_NAME]
558 = concat2 (build_string (" "), tem);
559 index += MENU_ITEMS_ITEM_LENGTH;
562 *notbuttons_ptr = 0;
565 /* Calculate prefix, if any, for this item. */
566 if (EQ (type, QCtoggle))
567 prefix = build_string (NILP (selected) ? "[ ] " : "[X] ");
568 else if (EQ (type, QCradio))
569 prefix = build_string (NILP (selected) ? "( ) " : "(*) ");
571 /* Not a button. If we have earlier buttons, then we need a prefix. */
572 else if (!*notbuttons_ptr && XSTRING (item_string)->data[0] != '\0'
573 && XSTRING (item_string)->data[0] != '-')
574 prefix = build_string (" ");
576 if (!NILP (prefix))
577 item_string = concat2 (prefix, item_string);
579 #endif /* not HAVE_BOXES */
581 #ifndef USE_X_TOOLKIT
582 if (!NILP(map))
583 /* Indicate visually that this is a submenu. */
584 item_string = concat2 (item_string, build_string (" >"));
585 #endif
587 push_menu_item (item_string, enabled, key,
588 XVECTOR (item_properties)->contents[ITEM_PROPERTY_DEF],
589 XVECTOR (item_properties)->contents[ITEM_PROPERTY_KEYEQ],
590 XVECTOR (item_properties)->contents[ITEM_PROPERTY_TYPE],
591 XVECTOR (item_properties)->contents[ITEM_PROPERTY_SELECTED],
592 XVECTOR (item_properties)->contents[ITEM_PROPERTY_HELP]);
594 #ifdef USE_X_TOOLKIT
595 /* Display a submenu using the toolkit. */
596 if (! (NILP (map) || NILP (enabled)))
598 push_submenu_start ();
599 single_keymap_panes (map, Qnil, key, 0, maxdepth - 1);
600 push_submenu_end ();
602 #endif
605 /* Push all the panes and items of a menu described by the
606 alist-of-alists MENU.
607 This handles old-fashioned calls to x-popup-menu. */
609 static void
610 list_of_panes (menu)
611 Lisp_Object menu;
613 Lisp_Object tail;
615 init_menu_items ();
617 for (tail = menu; !NILP (tail); tail = Fcdr (tail))
619 Lisp_Object elt, pane_name, pane_data;
620 elt = Fcar (tail);
621 pane_name = Fcar (elt);
622 CHECK_STRING (pane_name, 0);
623 push_menu_pane (pane_name, Qnil);
624 pane_data = Fcdr (elt);
625 CHECK_CONS (pane_data, 0);
626 list_of_items (pane_data);
629 finish_menu_items ();
632 /* Push the items in a single pane defined by the alist PANE. */
634 static void
635 list_of_items (pane)
636 Lisp_Object pane;
638 Lisp_Object tail, item, item1;
640 for (tail = pane; !NILP (tail); tail = Fcdr (tail))
642 item = Fcar (tail);
643 if (STRINGP (item))
644 push_menu_item (item, Qnil, Qnil, Qt, Qnil, Qnil, Qnil, Qnil);
645 else if (NILP (item))
646 push_left_right_boundary ();
647 else
649 CHECK_CONS (item, 0);
650 item1 = Fcar (item);
651 CHECK_STRING (item1, 1);
652 push_menu_item (item1, Qt, Fcdr (item), Qt, Qnil, Qnil, Qnil, Qnil);
657 DEFUN ("x-popup-menu", Fx_popup_menu, Sx_popup_menu, 2, 2, 0,
658 "Pop up a deck-of-cards menu and return user's selection.\n\
659 POSITION is a position specification. This is either a mouse button event\n\
660 or a list ((XOFFSET YOFFSET) WINDOW)\n\
661 where XOFFSET and YOFFSET are positions in pixels from the top left\n\
662 corner of WINDOW's frame. (WINDOW may be a frame object instead of a window.)\n\
663 This controls the position of the center of the first line\n\
664 in the first pane of the menu, not the top left of the menu as a whole.\n\
665 If POSITION is t, it means to use the current mouse position.\n\
667 MENU is a specifier for a menu. For the simplest case, MENU is a keymap.\n\
668 The menu items come from key bindings that have a menu string as well as\n\
669 a definition; actually, the \"definition\" in such a key binding looks like\n\
670 \(STRING . REAL-DEFINITION). To give the menu a title, put a string into\n\
671 the keymap as a top-level element.\n\n\
672 If REAL-DEFINITION is nil, that puts a nonselectable string in the menu.\n\
673 Otherwise, REAL-DEFINITION should be a valid key binding definition.\n\
675 You can also use a list of keymaps as MENU.\n\
676 Then each keymap makes a separate pane.\n\
677 When MENU is a keymap or a list of keymaps, the return value\n\
678 is a list of events.\n\n\
680 Alternatively, you can specify a menu of multiple panes\n\
681 with a list of the form (TITLE PANE1 PANE2...),\n\
682 where each pane is a list of form (TITLE ITEM1 ITEM2...).\n\
683 Each ITEM is normally a cons cell (STRING . VALUE);\n\
684 but a string can appear as an item--that makes a nonselectable line\n\
685 in the menu.\n\
686 With this form of menu, the return value is VALUE from the chosen item.\n\
688 If POSITION is nil, don't display the menu at all, just precalculate the\n\
689 cached information about equivalent key sequences.")
690 (position, menu)
691 Lisp_Object position, menu;
693 Lisp_Object keymap, tem;
694 int xpos = 0, ypos = 0;
695 Lisp_Object title;
696 char *error_name;
697 Lisp_Object selection;
698 struct frame *f = NULL;
699 Lisp_Object x, y, window;
700 int keymaps = 0;
701 int for_click = 0;
702 struct gcpro gcpro1;
704 #ifdef HAVE_MENUS
705 if (! NILP (position))
707 check_x ();
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))))
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;
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
727 window = selected_window;
728 XSETFASTINT (x, 0);
729 XSETFASTINT (y, 0);
732 else
734 tem = Fcar (position);
735 if (CONSP (tem))
737 window = Fcar (Fcdr (position));
738 x = Fcar (tem);
739 y = Fcar (Fcdr (tem));
741 else
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);
752 CHECK_NUMBER (x, 0);
753 CHECK_NUMBER (y, 0);
755 /* Decode where to put the menu. */
757 if (FRAMEP (window))
759 f = XFRAME (window);
760 xpos = 0;
761 ypos = 0;
763 else if (WINDOWP (window))
765 CHECK_LIVE_WINDOW (window, 0);
766 f = XFRAME (WINDOW_FRAME (XWINDOW (window)));
768 xpos = (FONT_WIDTH (f->output_data.x->font)
769 * XFASTINT (XWINDOW (window)->left));
770 ypos = (f->output_data.x->line_height
771 * XFASTINT (XWINDOW (window)->top));
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, 0);
778 xpos += XINT (x);
779 ypos += XINT (y);
781 XSETFRAME (Vmenu_updating_frame, f);
783 Vmenu_updating_frame = Qnil;
784 #endif /* HAVE_MENUS */
786 title = Qnil;
787 GCPRO1 (title);
789 /* Decode the menu items from what was specified. */
791 keymap = Fkeymapp (menu);
792 tem = Qnil;
793 if (CONSP (menu))
794 tem = Fkeymapp (Fcar (menu));
795 if (!NILP (keymap))
797 /* We were given a keymap. Extract menu info from the keymap. */
798 Lisp_Object prompt;
799 keymap = get_keymap (menu);
801 /* Extract the detailed info to make one pane. */
802 keymap_panes (&menu, 1, NILP (position));
804 /* Search for a string appearing directly as an element of the keymap.
805 That string is the title of the menu. */
806 prompt = map_prompt (keymap);
807 if (NILP (title) && !NILP (prompt))
808 title = prompt;
810 /* Make that be the pane title of the first pane. */
811 if (!NILP (prompt) && menu_items_n_panes >= 0)
812 XVECTOR (menu_items)->contents[MENU_ITEMS_PANE_NAME] = prompt;
814 keymaps = 1;
816 else if (!NILP (tem))
818 /* We were given a list of keymaps. */
819 int nmaps = XFASTINT (Flength (menu));
820 Lisp_Object *maps
821 = (Lisp_Object *) alloca (nmaps * sizeof (Lisp_Object));
822 int i;
824 title = Qnil;
826 /* The first keymap that has a prompt string
827 supplies the menu title. */
828 for (tem = menu, i = 0; CONSP (tem); tem = Fcdr (tem))
830 Lisp_Object prompt;
832 maps[i++] = keymap = get_keymap (Fcar (tem));
834 prompt = map_prompt (keymap);
835 if (NILP (title) && !NILP (prompt))
836 title = prompt;
839 /* Extract the detailed info to make one pane. */
840 keymap_panes (maps, nmaps, NILP (position));
842 /* Make the title be the pane title of the first pane. */
843 if (!NILP (title) && menu_items_n_panes >= 0)
844 XVECTOR (menu_items)->contents[MENU_ITEMS_PANE_NAME] = title;
846 keymaps = 1;
848 else
850 /* We were given an old-fashioned menu. */
851 title = Fcar (menu);
852 CHECK_STRING (title, 1);
854 list_of_panes (Fcdr (menu));
856 keymaps = 0;
859 if (NILP (position))
861 discard_menu_items ();
862 UNGCPRO;
863 return Qnil;
866 #ifdef HAVE_MENUS
867 /* Display them in a menu. */
868 BLOCK_INPUT;
870 selection = xmenu_show (f, xpos, ypos, for_click,
871 keymaps, title, &error_name);
872 UNBLOCK_INPUT;
874 discard_menu_items ();
876 UNGCPRO;
877 #endif /* HAVE_MENUS */
879 if (error_name) error (error_name);
880 return selection;
883 #ifdef HAVE_MENUS
885 DEFUN ("x-popup-dialog", Fx_popup_dialog, Sx_popup_dialog, 2, 2, 0,
886 "Pop up a dialog box and return user's selection.\n\
887 POSITION specifies which frame to use.\n\
888 This is normally a mouse button event or a window or frame.\n\
889 If POSITION is t, it means to use the frame the mouse is on.\n\
890 The dialog box appears in the middle of the specified frame.\n\
892 CONTENTS specifies the alternatives to display in the dialog box.\n\
893 It is a list of the form (TITLE ITEM1 ITEM2...).\n\
894 Each ITEM is a cons cell (STRING . VALUE).\n\
895 The return value is VALUE from the chosen item.\n\n\
896 An ITEM may also be just a string--that makes a nonselectable item.\n\
897 An ITEM may also be nil--that means to put all preceding items\n\
898 on the left of the dialog box and all following items on the right.\n\
899 \(By default, approximately half appear on each side.)")
900 (position, contents)
901 Lisp_Object position, contents;
903 struct frame * f = NULL;
904 Lisp_Object window;
906 check_x ();
908 /* Decode the first argument: find the window or frame to use. */
909 if (EQ (position, Qt)
910 || (CONSP (position) && (EQ (XCAR (position), Qmenu_bar)
911 || EQ (XCAR (position), Qtool_bar))))
913 #if 0 /* Using the frame the mouse is on may not be right. */
914 /* Use the mouse's current position. */
915 FRAME_PTR new_f = SELECTED_FRAME ();
916 Lisp_Object bar_window;
917 int part;
918 unsigned long time;
919 Lisp_Object x, y;
921 (*mouse_position_hook) (&new_f, 1, &bar_window, &part, &x, &y, &time);
923 if (new_f != 0)
924 XSETFRAME (window, new_f);
925 else
926 window = selected_window;
927 #endif
928 window = selected_window;
930 else if (CONSP (position))
932 Lisp_Object tem;
933 tem = Fcar (position);
934 if (CONSP (tem))
935 window = Fcar (Fcdr (position));
936 else
938 tem = Fcar (Fcdr (position)); /* EVENT_START (position) */
939 window = Fcar (tem); /* POSN_WINDOW (tem) */
942 else if (WINDOWP (position) || FRAMEP (position))
943 window = position;
944 else
945 window = Qnil;
947 /* Decode where to put the menu. */
949 if (FRAMEP (window))
950 f = XFRAME (window);
951 else if (WINDOWP (window))
953 CHECK_LIVE_WINDOW (window, 0);
954 f = XFRAME (WINDOW_FRAME (XWINDOW (window)));
956 else
957 /* ??? Not really clean; should be CHECK_WINDOW_OR_FRAME,
958 but I don't want to make one now. */
959 CHECK_WINDOW (window, 0);
961 #ifndef USE_X_TOOLKIT
962 /* Display a menu with these alternatives
963 in the middle of frame F. */
965 Lisp_Object x, y, frame, newpos;
966 XSETFRAME (frame, f);
967 XSETINT (x, x_pixel_width (f) / 2);
968 XSETINT (y, x_pixel_height (f) / 2);
969 newpos = Fcons (Fcons (x, Fcons (y, Qnil)), Fcons (frame, Qnil));
971 return Fx_popup_menu (newpos,
972 Fcons (Fcar (contents), Fcons (contents, Qnil)));
974 #else
976 Lisp_Object title;
977 char *error_name;
978 Lisp_Object selection;
980 /* Decode the dialog items from what was specified. */
981 title = Fcar (contents);
982 CHECK_STRING (title, 1);
984 list_of_panes (Fcons (contents, Qnil));
986 /* Display them in a dialog box. */
987 BLOCK_INPUT;
988 selection = xdialog_show (f, 0, title, &error_name);
989 UNBLOCK_INPUT;
991 discard_menu_items ();
993 if (error_name) error (error_name);
994 return selection;
996 #endif
999 #ifdef USE_X_TOOLKIT
1001 /* Loop in Xt until the menu pulldown or dialog popup has been
1002 popped down (deactivated). This is used for x-popup-menu
1003 and x-popup-dialog; it is not used for the menu bar any more.
1005 NOTE: All calls to popup_get_selection should be protected
1006 with BLOCK_INPUT, UNBLOCK_INPUT wrappers. */
1008 void
1009 popup_get_selection (initial_event, dpyinfo, id)
1010 XEvent *initial_event;
1011 struct x_display_info *dpyinfo;
1012 LWLIB_ID id;
1014 XEvent event;
1016 /* Define a queue to save up for later unreading
1017 all X events that don't pertain to the menu. */
1018 struct event_queue
1020 XEvent event;
1021 struct event_queue *next;
1024 struct event_queue *queue = NULL;
1025 struct event_queue *queue_tmp;
1027 if (initial_event)
1028 event = *initial_event;
1029 else
1030 XtAppNextEvent (Xt_app_con, &event);
1032 while (1)
1034 /* Handle expose events for editor frames right away. */
1035 if (event.type == Expose)
1036 process_expose_from_menu (event);
1037 /* Make sure we don't consider buttons grabbed after menu goes.
1038 And make sure to deactivate for any ButtonRelease,
1039 even if XtDispatchEvent doesn't do that. */
1040 else if (event.type == ButtonRelease
1041 && dpyinfo->display == event.xbutton.display)
1043 dpyinfo->grabbed &= ~(1 << event.xbutton.button);
1044 popup_activated_flag = 0;
1045 #ifdef USE_MOTIF /* Pretending that the event came from a
1046 Btn1Down seems the only way to convince Motif to
1047 activate its callbacks; setting the XmNmenuPost
1048 isn't working. --marcus@sysc.pdx.edu. */
1049 event.xbutton.button = 1;
1050 #endif
1052 /* If the user presses a key, deactivate the menu.
1053 The user is likely to do that if we get wedged. */
1054 else if (event.type == KeyPress
1055 && dpyinfo->display == event.xbutton.display)
1057 KeySym keysym = XLookupKeysym (&event.xkey, 0);
1058 if (!IsModifierKey (keysym))
1060 popup_activated_flag = 0;
1061 break;
1064 /* Button presses outside the menu also pop it down. */
1065 else if (event.type == ButtonPress
1066 && event.xany.display == dpyinfo->display
1067 && x_any_window_to_frame (dpyinfo, event.xany.window))
1069 popup_activated_flag = 0;
1070 break;
1073 /* Queue all events not for this popup,
1074 except for Expose, which we've already handled, and ButtonRelease.
1075 Note that the X window is associated with the frame if this
1076 is a menu bar popup, but not if it's a dialog box. So we use
1077 x_non_menubar_window_to_frame, not x_any_window_to_frame. */
1078 if (event.type != Expose
1079 && !(event.type == ButtonRelease
1080 && dpyinfo->display == event.xbutton.display)
1081 && (event.xany.display != dpyinfo->display
1082 || x_non_menubar_window_to_frame (dpyinfo, event.xany.window)))
1084 queue_tmp = (struct event_queue *) malloc (sizeof (struct event_queue));
1086 if (queue_tmp != NULL)
1088 queue_tmp->event = event;
1089 queue_tmp->next = queue;
1090 queue = queue_tmp;
1093 else
1094 XtDispatchEvent (&event);
1096 if (!popup_activated ())
1097 break;
1098 XtAppNextEvent (Xt_app_con, &event);
1101 /* Unread any events that we got but did not handle. */
1102 while (queue != NULL)
1104 queue_tmp = queue;
1105 XPutBackEvent (queue_tmp->event.xany.display, &queue_tmp->event);
1106 queue = queue_tmp->next;
1107 xfree ((char *)queue_tmp);
1108 /* Cause these events to get read as soon as we UNBLOCK_INPUT. */
1109 interrupt_input_pending = 1;
1113 /* Activate the menu bar of frame F.
1114 This is called from keyboard.c when it gets the
1115 menu_bar_activate_event out of the Emacs event queue.
1117 To activate the menu bar, we use the X button-press event
1118 that was saved in saved_menu_event.
1119 That makes the toolkit do its thing.
1121 But first we recompute the menu bar contents (the whole tree).
1123 The reason for saving the button event until here, instead of
1124 passing it to the toolkit right away, is that we can safely
1125 execute Lisp code. */
1127 void
1128 x_activate_menubar (f)
1129 FRAME_PTR f;
1131 if (!f->output_data.x->saved_menu_event->type)
1132 return;
1134 set_frame_menubar (f, 0, 1);
1135 BLOCK_INPUT;
1136 XtDispatchEvent ((XEvent *) f->output_data.x->saved_menu_event);
1137 UNBLOCK_INPUT;
1138 #ifdef USE_MOTIF
1139 if (f->output_data.x->saved_menu_event->type == ButtonRelease)
1140 pending_menu_activation = 1;
1141 #endif
1143 /* Ignore this if we get it a second time. */
1144 f->output_data.x->saved_menu_event->type = 0;
1147 /* Detect if a dialog or menu has been posted. */
1150 popup_activated ()
1152 return popup_activated_flag;
1155 /* This callback is invoked when the user selects a menubar cascade
1156 pushbutton, but before the pulldown menu is posted. */
1158 static void
1159 popup_activate_callback (widget, id, client_data)
1160 Widget widget;
1161 LWLIB_ID id;
1162 XtPointer client_data;
1164 #ifdef USE_MOTIF
1165 ++popup_activated_flag;
1166 #else
1167 popup_activated_flag = 1;
1168 #endif
1171 /* This callback is invoked when a dialog or menu is finished being
1172 used and has been unposted. */
1174 static void
1175 popup_deactivate_callback (widget, id, client_data)
1176 Widget widget;
1177 LWLIB_ID id;
1178 XtPointer client_data;
1180 #ifdef USE_MOTIF
1181 --popup_activated_flag;
1182 #else
1183 popup_activated_flag = 0;
1184 #endif
1187 /* Lwlib callback called when menu items are highlighted/unhighlighted
1188 while moving the mouse over them. WIDGET is the menu bar or menu
1189 popup widget. ID is its LWLIB_ID. CALL_DATA contains a pointer to
1190 the widget_value structure for the menu item, or null in case of
1191 unhighlighting. */
1193 void
1194 menu_highlight_callback (widget, id, call_data)
1195 Widget widget;
1196 LWLIB_ID id;
1197 void *call_data;
1199 widget_value *wv = (widget_value *) call_data;
1200 struct frame *f;
1201 Lisp_Object frame, help;
1203 help = wv && wv->help ? build_string (wv->help) : Qnil;
1205 /* Determine the frame for the help event. */
1206 f = menubar_id_to_frame (id);
1207 if (f)
1209 XSETFRAME (frame, f);
1210 kbd_buffer_store_help_event (frame, help);
1212 else
1214 /* WIDGET is the popup menu. It's parent is the frame's
1215 widget. See which frame that is. */
1216 Widget frame_widget = XtParent (widget);
1217 Lisp_Object tail;
1219 for (tail = Vframe_list; GC_CONSP (tail); tail = XCDR (tail))
1221 frame = XCAR (tail);
1222 if (GC_FRAMEP (frame)
1223 && (f = XFRAME (frame),
1224 FRAME_X_P (f) && f->output_data.x->widget == frame_widget))
1225 break;
1228 show_help_echo (help, Qnil, Qnil, Qnil, 1);
1232 /* This callback is called from the menu bar pulldown menu
1233 when the user makes a selection.
1234 Figure out what the user chose
1235 and put the appropriate events into the keyboard buffer. */
1237 static void
1238 menubar_selection_callback (widget, id, client_data)
1239 Widget widget;
1240 LWLIB_ID id;
1241 XtPointer client_data;
1243 Lisp_Object prefix, entry;
1244 FRAME_PTR f = menubar_id_to_frame (id);
1245 Lisp_Object vector;
1246 Lisp_Object *subprefix_stack;
1247 int submenu_depth = 0;
1248 int i;
1250 if (!f)
1251 return;
1252 entry = Qnil;
1253 subprefix_stack = (Lisp_Object *) alloca (f->menu_bar_items_used * sizeof (Lisp_Object));
1254 vector = f->menu_bar_vector;
1255 prefix = Qnil;
1256 i = 0;
1257 while (i < f->menu_bar_items_used)
1259 if (EQ (XVECTOR (vector)->contents[i], Qnil))
1261 subprefix_stack[submenu_depth++] = prefix;
1262 prefix = entry;
1263 i++;
1265 else if (EQ (XVECTOR (vector)->contents[i], Qlambda))
1267 prefix = subprefix_stack[--submenu_depth];
1268 i++;
1270 else if (EQ (XVECTOR (vector)->contents[i], Qt))
1272 prefix = XVECTOR (vector)->contents[i + MENU_ITEMS_PANE_PREFIX];
1273 i += MENU_ITEMS_PANE_LENGTH;
1275 else
1277 entry = XVECTOR (vector)->contents[i + MENU_ITEMS_ITEM_VALUE];
1278 /* The EMACS_INT cast avoids a warning. There's no problem
1279 as long as pointers have enough bits to hold small integers. */
1280 if ((int) (EMACS_INT) client_data == i)
1282 int j;
1283 struct input_event buf;
1284 Lisp_Object frame;
1286 XSETFRAME (frame, f);
1287 buf.kind = MENU_BAR_EVENT;
1288 buf.frame_or_window = frame;
1289 buf.arg = frame;
1290 kbd_buffer_store_event (&buf);
1292 for (j = 0; j < submenu_depth; j++)
1293 if (!NILP (subprefix_stack[j]))
1295 buf.kind = MENU_BAR_EVENT;
1296 buf.frame_or_window = frame;
1297 buf.arg = subprefix_stack[j];
1298 kbd_buffer_store_event (&buf);
1301 if (!NILP (prefix))
1303 buf.kind = MENU_BAR_EVENT;
1304 buf.frame_or_window = frame;
1305 buf.arg = prefix;
1306 kbd_buffer_store_event (&buf);
1309 buf.kind = MENU_BAR_EVENT;
1310 buf.frame_or_window = frame;
1311 buf.arg = entry;
1312 kbd_buffer_store_event (&buf);
1314 return;
1316 i += MENU_ITEMS_ITEM_LENGTH;
1321 /* Allocate a widget_value, blocking input. */
1323 widget_value *
1324 xmalloc_widget_value ()
1326 widget_value *value;
1328 BLOCK_INPUT;
1329 value = malloc_widget_value ();
1330 UNBLOCK_INPUT;
1332 return value;
1335 /* This recursively calls free_widget_value on the tree of widgets.
1336 It must free all data that was malloc'ed for these widget_values.
1337 In Emacs, many slots are pointers into the data of Lisp_Strings, and
1338 must be left alone. */
1340 void
1341 free_menubar_widget_value_tree (wv)
1342 widget_value *wv;
1344 if (! wv) return;
1346 wv->name = wv->value = wv->key = (char *) 0xDEADBEEF;
1348 if (wv->contents && (wv->contents != (widget_value*)1))
1350 free_menubar_widget_value_tree (wv->contents);
1351 wv->contents = (widget_value *) 0xDEADBEEF;
1353 if (wv->next)
1355 free_menubar_widget_value_tree (wv->next);
1356 wv->next = (widget_value *) 0xDEADBEEF;
1358 BLOCK_INPUT;
1359 free_widget_value (wv);
1360 UNBLOCK_INPUT;
1363 /* Return a tree of widget_value structures for a menu bar item
1364 whose event type is ITEM_KEY (with string ITEM_NAME)
1365 and whose contents come from the list of keymaps MAPS. */
1367 static widget_value *
1368 single_submenu (item_key, item_name, maps)
1369 Lisp_Object item_key, item_name, maps;
1371 widget_value *wv, *prev_wv, *save_wv, *first_wv;
1372 int i;
1373 int submenu_depth = 0;
1374 Lisp_Object length;
1375 int len;
1376 Lisp_Object *mapvec;
1377 widget_value **submenu_stack;
1378 int previous_items = menu_items_used;
1379 int top_level_items = 0;
1381 length = Flength (maps);
1382 len = XINT (length);
1384 /* Convert the list MAPS into a vector MAPVEC. */
1385 mapvec = (Lisp_Object *) alloca (len * sizeof (Lisp_Object));
1386 for (i = 0; i < len; i++)
1388 mapvec[i] = Fcar (maps);
1389 maps = Fcdr (maps);
1392 menu_items_n_panes = 0;
1394 /* Loop over the given keymaps, making a pane for each map.
1395 But don't make a pane that is empty--ignore that map instead. */
1396 for (i = 0; i < len; i++)
1398 if (SYMBOLP (mapvec[i])
1399 || (CONSP (mapvec[i])
1400 && NILP (Fkeymapp (mapvec[i]))))
1402 /* Here we have a command at top level in the menu bar
1403 as opposed to a submenu. */
1404 top_level_items = 1;
1405 push_menu_pane (Qnil, Qnil);
1406 push_menu_item (item_name, Qt, item_key, mapvec[i],
1407 Qnil, Qnil, Qnil, Qnil);
1409 else
1410 single_keymap_panes (mapvec[i], item_name, item_key, 0, 10);
1413 /* Create a tree of widget_value objects
1414 representing the panes and their items. */
1416 submenu_stack
1417 = (widget_value **) alloca (menu_items_used * sizeof (widget_value *));
1418 wv = xmalloc_widget_value ();
1419 wv->name = "menu";
1420 wv->value = 0;
1421 wv->enabled = 1;
1422 wv->button_type = BUTTON_TYPE_NONE;
1423 first_wv = wv;
1424 save_wv = 0;
1425 prev_wv = 0;
1427 /* Loop over all panes and items made during this call
1428 and construct a tree of widget_value objects.
1429 Ignore the panes and items made by previous calls to
1430 single_submenu, even though those are also in menu_items. */
1431 i = previous_items;
1432 while (i < menu_items_used)
1434 if (EQ (XVECTOR (menu_items)->contents[i], Qnil))
1436 submenu_stack[submenu_depth++] = save_wv;
1437 save_wv = prev_wv;
1438 prev_wv = 0;
1439 i++;
1441 else if (EQ (XVECTOR (menu_items)->contents[i], Qlambda))
1443 prev_wv = save_wv;
1444 save_wv = submenu_stack[--submenu_depth];
1445 i++;
1447 else if (EQ (XVECTOR (menu_items)->contents[i], Qt)
1448 && submenu_depth != 0)
1449 i += MENU_ITEMS_PANE_LENGTH;
1450 /* Ignore a nil in the item list.
1451 It's meaningful only for dialog boxes. */
1452 else if (EQ (XVECTOR (menu_items)->contents[i], Qquote))
1453 i += 1;
1454 else if (EQ (XVECTOR (menu_items)->contents[i], Qt))
1456 /* Create a new pane. */
1457 Lisp_Object pane_name, prefix;
1458 char *pane_string;
1459 pane_name = XVECTOR (menu_items)->contents[i + MENU_ITEMS_PANE_NAME];
1460 prefix = XVECTOR (menu_items)->contents[i + MENU_ITEMS_PANE_PREFIX];
1461 #ifndef HAVE_MULTILINGUAL_MENU
1462 if (STRINGP (pane_name) && STRING_MULTIBYTE (pane_name))
1463 pane_name = string_make_unibyte (pane_name);
1464 #endif
1465 pane_string = (NILP (pane_name)
1466 ? "" : (char *) XSTRING (pane_name)->data);
1467 /* If there is just one top-level pane, put all its items directly
1468 under the top-level menu. */
1469 if (menu_items_n_panes == 1)
1470 pane_string = "";
1472 /* If the pane has a meaningful name,
1473 make the pane a top-level menu item
1474 with its items as a submenu beneath it. */
1475 if (strcmp (pane_string, ""))
1477 wv = xmalloc_widget_value ();
1478 if (save_wv)
1479 save_wv->next = wv;
1480 else
1481 first_wv->contents = wv;
1482 wv->name = pane_string;
1483 /* Ignore the @ that means "separate pane".
1484 This is a kludge, but this isn't worth more time. */
1485 if (!NILP (prefix) && wv->name[0] == '@')
1486 wv->name++;
1487 wv->value = 0;
1488 wv->enabled = 1;
1489 wv->button_type = BUTTON_TYPE_NONE;
1491 save_wv = wv;
1492 prev_wv = 0;
1493 i += MENU_ITEMS_PANE_LENGTH;
1495 else
1497 /* Create a new item within current pane. */
1498 Lisp_Object item_name, enable, descrip, def, type, selected;
1499 Lisp_Object help;
1501 item_name = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_NAME];
1502 enable = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_ENABLE];
1503 descrip
1504 = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_EQUIV_KEY];
1505 def = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_DEFINITION];
1506 type = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_TYPE];
1507 selected = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_SELECTED];
1508 help = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_HELP];
1510 #ifndef HAVE_MULTILINGUAL_MENU
1511 if (STRING_MULTIBYTE (item_name))
1512 item_name = string_make_unibyte (item_name);
1513 if (STRINGP (descrip) && STRING_MULTIBYTE (descrip))
1514 descrip = string_make_unibyte (descrip);
1515 #endif
1517 wv = xmalloc_widget_value ();
1518 if (prev_wv)
1519 prev_wv->next = wv;
1520 else
1521 save_wv->contents = wv;
1523 wv->name = (char *) XSTRING (item_name)->data;
1524 if (!NILP (descrip))
1525 wv->key = (char *) XSTRING (descrip)->data;
1526 wv->value = 0;
1527 /* The EMACS_INT cast avoids a warning. There's no problem
1528 as long as pointers have enough bits to hold small integers. */
1529 wv->call_data = (!NILP (def) ? (void *) (EMACS_INT) i : 0);
1530 wv->enabled = !NILP (enable);
1532 if (NILP (type))
1533 wv->button_type = BUTTON_TYPE_NONE;
1534 else if (EQ (type, QCradio))
1535 wv->button_type = BUTTON_TYPE_RADIO;
1536 else if (EQ (type, QCtoggle))
1537 wv->button_type = BUTTON_TYPE_TOGGLE;
1538 else
1539 abort ();
1541 wv->selected = !NILP (selected);
1542 if (STRINGP (help))
1543 wv->help = XSTRING (help)->data;
1545 prev_wv = wv;
1547 i += MENU_ITEMS_ITEM_LENGTH;
1551 /* If we have just one "menu item"
1552 that was originally a button, return it by itself. */
1553 if (top_level_items && first_wv->contents && first_wv->contents->next == 0)
1555 wv = first_wv->contents;
1556 free_widget_value (first_wv);
1557 return wv;
1560 return first_wv;
1563 extern void EmacsFrameSetCharSize ();
1565 /* Recompute all the widgets of frame F, when the menu bar
1566 has been changed. */
1568 static void
1569 update_frame_menubar (f)
1570 FRAME_PTR f;
1572 struct x_output *x = f->output_data.x;
1573 int columns, rows;
1574 int menubar_changed;
1576 /* We assume the menubar contents has changed if the global flag is set,
1577 or if the current buffer has changed, or if the menubar has never
1578 been updated before.
1580 menubar_changed = (x->menubar_widget
1581 && !XtIsManaged (x->menubar_widget));
1583 if (! (menubar_changed))
1584 return;
1586 BLOCK_INPUT;
1587 /* Save the size of the frame because the pane widget doesn't accept to
1588 resize itself. So force it. */
1589 columns = f->width;
1590 rows = f->height;
1592 /* Do the voodoo which means "I'm changing lots of things, don't try to
1593 refigure sizes until I'm done." */
1594 lw_refigure_widget (x->column_widget, False);
1596 /* the order in which children are managed is the top to
1597 bottom order in which they are displayed in the paned window.
1598 First, remove the text-area widget.
1600 XtUnmanageChild (x->edit_widget);
1602 /* remove the menubar that is there now, and put up the menubar that
1603 should be there.
1605 if (menubar_changed)
1607 XtManageChild (x->menubar_widget);
1608 XtMapWidget (x->menubar_widget);
1609 XtVaSetValues (x->menubar_widget, XtNmappedWhenManaged, 1, NULL);
1612 /* Re-manage the text-area widget, and then thrash the sizes. */
1613 XtManageChild (x->edit_widget);
1614 x_set_menu_resources_from_menu_face (f, x->menubar_widget);
1615 lw_refigure_widget (x->column_widget, True);
1617 /* Force the pane widget to resize itself with the right values. */
1618 EmacsFrameSetCharSize (x->edit_widget, columns, rows);
1619 UNBLOCK_INPUT;
1622 /* Set the contents of the menubar widgets of frame F.
1623 The argument FIRST_TIME is currently ignored;
1624 it is set the first time this is called, from initialize_frame_menubar. */
1626 void
1627 set_frame_menubar (f, first_time, deep_p)
1628 FRAME_PTR f;
1629 int first_time;
1630 int deep_p;
1632 Widget menubar_widget = f->output_data.x->menubar_widget;
1633 Lisp_Object items;
1634 widget_value *wv, *first_wv, *prev_wv = 0;
1635 int i;
1636 LWLIB_ID id;
1638 XSETFRAME (Vmenu_updating_frame, f);
1640 if (f->output_data.x->id == 0)
1641 f->output_data.x->id = next_menubar_widget_id++;
1642 id = f->output_data.x->id;
1644 if (! menubar_widget)
1645 deep_p = 1;
1646 else if (pending_menu_activation && !deep_p)
1647 deep_p = 1;
1648 /* Make the first call for any given frame always go deep. */
1649 else if (!f->output_data.x->saved_menu_event && !deep_p)
1651 deep_p = 1;
1652 f->output_data.x->saved_menu_event = (XEvent*)xmalloc (sizeof (XEvent));
1653 f->output_data.x->saved_menu_event->type = 0;
1656 wv = xmalloc_widget_value ();
1657 wv->name = "menubar";
1658 wv->value = 0;
1659 wv->enabled = 1;
1660 wv->button_type = BUTTON_TYPE_NONE;
1661 first_wv = wv;
1663 if (deep_p)
1665 /* Make a widget-value tree representing the entire menu trees. */
1667 struct buffer *prev = current_buffer;
1668 Lisp_Object buffer;
1669 int specpdl_count = specpdl_ptr - specpdl;
1670 int previous_menu_items_used = f->menu_bar_items_used;
1671 Lisp_Object *previous_items
1672 = (Lisp_Object *) alloca (previous_menu_items_used
1673 * sizeof (Lisp_Object));
1675 /* If we are making a new widget, its contents are empty,
1676 do always reinitialize them. */
1677 if (! menubar_widget)
1678 previous_menu_items_used = 0;
1680 buffer = XWINDOW (FRAME_SELECTED_WINDOW (f))->buffer;
1681 specbind (Qinhibit_quit, Qt);
1682 /* Don't let the debugger step into this code
1683 because it is not reentrant. */
1684 specbind (Qdebug_on_next_call, Qnil);
1686 record_unwind_protect (Fset_match_data, Fmatch_data (Qnil, Qnil));
1687 if (NILP (Voverriding_local_map_menu_flag))
1689 specbind (Qoverriding_terminal_local_map, Qnil);
1690 specbind (Qoverriding_local_map, Qnil);
1693 set_buffer_internal_1 (XBUFFER (buffer));
1695 /* Run the Lucid hook. */
1696 call1 (Vrun_hooks, Qactivate_menubar_hook);
1697 /* If it has changed current-menubar from previous value,
1698 really recompute the menubar from the value. */
1699 if (! NILP (Vlucid_menu_bar_dirty_flag))
1700 call0 (Qrecompute_lucid_menubar);
1701 safe_run_hooks (Qmenu_bar_update_hook);
1702 FRAME_MENU_BAR_ITEMS (f) = menu_bar_items (FRAME_MENU_BAR_ITEMS (f));
1704 items = FRAME_MENU_BAR_ITEMS (f);
1706 inhibit_garbage_collection ();
1708 /* Save the frame's previous menu bar contents data. */
1709 bcopy (XVECTOR (f->menu_bar_vector)->contents, previous_items,
1710 previous_menu_items_used * sizeof (Lisp_Object));
1712 /* Fill in the current menu bar contents. */
1713 menu_items = f->menu_bar_vector;
1714 menu_items_allocated = XVECTOR (menu_items)->size;
1715 init_menu_items ();
1716 for (i = 0; i < XVECTOR (items)->size; i += 4)
1718 Lisp_Object key, string, maps;
1720 key = XVECTOR (items)->contents[i];
1721 string = XVECTOR (items)->contents[i + 1];
1722 maps = XVECTOR (items)->contents[i + 2];
1723 if (NILP (string))
1724 break;
1726 wv = single_submenu (key, string, maps);
1727 if (prev_wv)
1728 prev_wv->next = wv;
1729 else
1730 first_wv->contents = wv;
1731 /* Don't set wv->name here; GC during the loop might relocate it. */
1732 wv->enabled = 1;
1733 wv->button_type = BUTTON_TYPE_NONE;
1734 prev_wv = wv;
1737 finish_menu_items ();
1739 set_buffer_internal_1 (prev);
1740 unbind_to (specpdl_count, Qnil);
1742 /* If there has been no change in the Lisp-level contents
1743 of the menu bar, skip redisplaying it. Just exit. */
1745 for (i = 0; i < previous_menu_items_used; i++)
1746 if (menu_items_used == i
1747 || (!EQ (previous_items[i], XVECTOR (menu_items)->contents[i])))
1748 break;
1749 if (i == menu_items_used && i == previous_menu_items_used && i != 0)
1751 free_menubar_widget_value_tree (first_wv);
1752 menu_items = Qnil;
1754 return;
1757 /* Now GC cannot happen during the lifetime of the widget_value,
1758 so it's safe to store data from a Lisp_String. */
1759 wv = first_wv->contents;
1760 for (i = 0; i < XVECTOR (items)->size; i += 4)
1762 Lisp_Object string;
1763 string = XVECTOR (items)->contents[i + 1];
1764 if (NILP (string))
1765 break;
1766 wv->name = (char *) XSTRING (string)->data;
1767 wv = wv->next;
1770 f->menu_bar_vector = menu_items;
1771 f->menu_bar_items_used = menu_items_used;
1772 menu_items = Qnil;
1774 else
1776 /* Make a widget-value tree containing
1777 just the top level menu bar strings. */
1779 items = FRAME_MENU_BAR_ITEMS (f);
1780 for (i = 0; i < XVECTOR (items)->size; i += 4)
1782 Lisp_Object string;
1784 string = XVECTOR (items)->contents[i + 1];
1785 if (NILP (string))
1786 break;
1788 wv = xmalloc_widget_value ();
1789 wv->name = (char *) XSTRING (string)->data;
1790 wv->value = 0;
1791 wv->enabled = 1;
1792 wv->button_type = BUTTON_TYPE_NONE;
1793 /* This prevents lwlib from assuming this
1794 menu item is really supposed to be empty. */
1795 /* The EMACS_INT cast avoids a warning.
1796 This value just has to be different from small integers. */
1797 wv->call_data = (void *) (EMACS_INT) (-1);
1799 if (prev_wv)
1800 prev_wv->next = wv;
1801 else
1802 first_wv->contents = wv;
1803 prev_wv = wv;
1806 /* Forget what we thought we knew about what is in the
1807 detailed contents of the menu bar menus.
1808 Changing the top level always destroys the contents. */
1809 f->menu_bar_items_used = 0;
1812 /* Create or update the menu bar widget. */
1814 BLOCK_INPUT;
1816 if (menubar_widget)
1818 /* Disable resizing (done for Motif!) */
1819 lw_allow_resizing (f->output_data.x->widget, False);
1821 /* The third arg is DEEP_P, which says to consider the entire
1822 menu trees we supply, rather than just the menu bar item names. */
1823 lw_modify_all_widgets (id, first_wv, deep_p);
1825 /* Re-enable the edit widget to resize. */
1826 lw_allow_resizing (f->output_data.x->widget, True);
1828 else
1830 menubar_widget = lw_create_widget ("menubar", "menubar", id, first_wv,
1831 f->output_data.x->column_widget,
1833 popup_activate_callback,
1834 menubar_selection_callback,
1835 popup_deactivate_callback,
1836 menu_highlight_callback);
1837 f->output_data.x->menubar_widget = menubar_widget;
1841 int menubar_size
1842 = (f->output_data.x->menubar_widget
1843 ? (f->output_data.x->menubar_widget->core.height
1844 + f->output_data.x->menubar_widget->core.border_width)
1845 : 0);
1847 #if 0 /* Experimentally, we now get the right results
1848 for -geometry -0-0 without this. 24 Aug 96, rms. */
1849 #ifdef USE_LUCID
1850 if (FRAME_EXTERNAL_MENU_BAR (f))
1852 Dimension ibw = 0;
1853 XtVaGetValues (f->output_data.x->column_widget,
1854 XtNinternalBorderWidth, &ibw, NULL);
1855 menubar_size += ibw;
1857 #endif /* USE_LUCID */
1858 #endif /* 0 */
1860 f->output_data.x->menubar_height = menubar_size;
1863 free_menubar_widget_value_tree (first_wv);
1864 update_frame_menubar (f);
1866 UNBLOCK_INPUT;
1869 /* Called from Fx_create_frame to create the initial menubar of a frame
1870 before it is mapped, so that the window is mapped with the menubar already
1871 there instead of us tacking it on later and thrashing the window after it
1872 is visible. */
1874 void
1875 initialize_frame_menubar (f)
1876 FRAME_PTR f;
1878 /* This function is called before the first chance to redisplay
1879 the frame. It has to be, so the frame will have the right size. */
1880 FRAME_MENU_BAR_ITEMS (f) = menu_bar_items (FRAME_MENU_BAR_ITEMS (f));
1881 set_frame_menubar (f, 1, 1);
1884 /* Get rid of the menu bar of frame F, and free its storage.
1885 This is used when deleting a frame, and when turning off the menu bar. */
1887 void
1888 free_frame_menubar (f)
1889 FRAME_PTR f;
1891 Widget menubar_widget;
1893 menubar_widget = f->output_data.x->menubar_widget;
1895 f->output_data.x->menubar_height = 0;
1897 if (menubar_widget)
1899 BLOCK_INPUT;
1900 lw_destroy_all_widgets ((LWLIB_ID) f->output_data.x->id);
1901 UNBLOCK_INPUT;
1905 #endif /* USE_X_TOOLKIT */
1907 /* xmenu_show actually displays a menu using the panes and items in menu_items
1908 and returns the value selected from it.
1909 There are two versions of xmenu_show, one for Xt and one for Xlib.
1910 Both assume input is blocked by the caller. */
1912 /* F is the frame the menu is for.
1913 X and Y are the frame-relative specified position,
1914 relative to the inside upper left corner of the frame F.
1915 FOR_CLICK is nonzero if this menu was invoked for a mouse click.
1916 KEYMAPS is 1 if this menu was specified with keymaps;
1917 in that case, we return a list containing the chosen item's value
1918 and perhaps also the pane's prefix.
1919 TITLE is the specified menu title.
1920 ERROR is a place to store an error message string in case of failure.
1921 (We return nil on failure, but the value doesn't actually matter.) */
1923 #ifdef USE_X_TOOLKIT
1925 /* We need a unique id for each widget handled by the Lucid Widget
1926 library.
1928 For the main windows, and popup menus, we use this counter,
1929 which we increment each time after use. This starts from 1<<16.
1931 For menu bars, we use numbers starting at 0, counted in
1932 next_menubar_widget_id. */
1933 LWLIB_ID widget_id_tick;
1935 static Lisp_Object *volatile menu_item_selection;
1937 static void
1938 popup_selection_callback (widget, id, client_data)
1939 Widget widget;
1940 LWLIB_ID id;
1941 XtPointer client_data;
1943 menu_item_selection = (Lisp_Object *) client_data;
1946 static Lisp_Object
1947 xmenu_show (f, x, y, for_click, keymaps, title, error)
1948 FRAME_PTR f;
1949 int x;
1950 int y;
1951 int for_click;
1952 int keymaps;
1953 Lisp_Object title;
1954 char **error;
1956 int i;
1957 LWLIB_ID menu_id;
1958 Widget menu;
1959 Arg av[2];
1960 int ac = 0;
1961 widget_value *wv, *save_wv = 0, *first_wv = 0, *prev_wv = 0;
1962 widget_value **submenu_stack
1963 = (widget_value **) alloca (menu_items_used * sizeof (widget_value *));
1964 Lisp_Object *subprefix_stack
1965 = (Lisp_Object *) alloca (menu_items_used * sizeof (Lisp_Object));
1966 int submenu_depth = 0;
1967 XButtonPressedEvent dummy;
1969 int first_pane;
1971 *error = NULL;
1973 if (menu_items_used <= MENU_ITEMS_PANE_LENGTH)
1975 *error = "Empty menu";
1976 return Qnil;
1979 /* Create a tree of widget_value objects
1980 representing the panes and their items. */
1981 wv = xmalloc_widget_value ();
1982 wv->name = "menu";
1983 wv->value = 0;
1984 wv->enabled = 1;
1985 wv->button_type = BUTTON_TYPE_NONE;
1986 first_wv = wv;
1987 first_pane = 1;
1989 /* Loop over all panes and items, filling in the tree. */
1990 i = 0;
1991 while (i < menu_items_used)
1993 if (EQ (XVECTOR (menu_items)->contents[i], Qnil))
1995 submenu_stack[submenu_depth++] = save_wv;
1996 save_wv = prev_wv;
1997 prev_wv = 0;
1998 first_pane = 1;
1999 i++;
2001 else if (EQ (XVECTOR (menu_items)->contents[i], Qlambda))
2003 prev_wv = save_wv;
2004 save_wv = submenu_stack[--submenu_depth];
2005 first_pane = 0;
2006 i++;
2008 else if (EQ (XVECTOR (menu_items)->contents[i], Qt)
2009 && submenu_depth != 0)
2010 i += MENU_ITEMS_PANE_LENGTH;
2011 /* Ignore a nil in the item list.
2012 It's meaningful only for dialog boxes. */
2013 else if (EQ (XVECTOR (menu_items)->contents[i], Qquote))
2014 i += 1;
2015 else if (EQ (XVECTOR (menu_items)->contents[i], Qt))
2017 /* Create a new pane. */
2018 Lisp_Object pane_name, prefix;
2019 char *pane_string;
2020 pane_name = XVECTOR (menu_items)->contents[i + MENU_ITEMS_PANE_NAME];
2021 prefix = XVECTOR (menu_items)->contents[i + MENU_ITEMS_PANE_PREFIX];
2022 #ifndef HAVE_MULTILINGUAL_MENU
2023 if (!NILP (pane_name) && STRING_MULTIBYTE (pane_name))
2024 pane_name = string_make_unibyte (pane_name);
2025 #endif
2026 pane_string = (NILP (pane_name)
2027 ? "" : (char *) XSTRING (pane_name)->data);
2028 /* If there is just one top-level pane, put all its items directly
2029 under the top-level menu. */
2030 if (menu_items_n_panes == 1)
2031 pane_string = "";
2033 /* If the pane has a meaningful name,
2034 make the pane a top-level menu item
2035 with its items as a submenu beneath it. */
2036 if (!keymaps && strcmp (pane_string, ""))
2038 wv = xmalloc_widget_value ();
2039 if (save_wv)
2040 save_wv->next = wv;
2041 else
2042 first_wv->contents = wv;
2043 wv->name = pane_string;
2044 if (keymaps && !NILP (prefix))
2045 wv->name++;
2046 wv->value = 0;
2047 wv->enabled = 1;
2048 wv->button_type = BUTTON_TYPE_NONE;
2049 save_wv = wv;
2050 prev_wv = 0;
2052 else if (first_pane)
2054 save_wv = wv;
2055 prev_wv = 0;
2057 first_pane = 0;
2058 i += MENU_ITEMS_PANE_LENGTH;
2060 else
2062 /* Create a new item within current pane. */
2063 Lisp_Object item_name, enable, descrip, def, type, selected, help;
2064 item_name = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_NAME];
2065 enable = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_ENABLE];
2066 descrip
2067 = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_EQUIV_KEY];
2068 def = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_DEFINITION];
2069 type = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_TYPE];
2070 selected = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_SELECTED];
2071 help = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_HELP];
2073 #ifndef HAVE_MULTILINGUAL_MENU
2074 if (STRINGP (item_name) && STRING_MULTIBYTE (item_name))
2075 item_name = string_make_unibyte (item_name);
2076 if (STRINGP (descrip) && STRING_MULTIBYTE (descrip))
2077 item_name = string_make_unibyte (descrip);
2078 #endif
2080 wv = xmalloc_widget_value ();
2081 if (prev_wv)
2082 prev_wv->next = wv;
2083 else
2084 save_wv->contents = wv;
2085 wv->name = (char *) XSTRING (item_name)->data;
2086 if (!NILP (descrip))
2087 wv->key = (char *) XSTRING (descrip)->data;
2088 wv->value = 0;
2089 /* If this item has a null value,
2090 make the call_data null so that it won't display a box
2091 when the mouse is on it. */
2092 wv->call_data
2093 = (!NILP (def) ? (void *) &XVECTOR (menu_items)->contents[i] : 0);
2094 wv->enabled = !NILP (enable);
2096 if (NILP (type))
2097 wv->button_type = BUTTON_TYPE_NONE;
2098 else if (EQ (type, QCtoggle))
2099 wv->button_type = BUTTON_TYPE_TOGGLE;
2100 else if (EQ (type, QCradio))
2101 wv->button_type = BUTTON_TYPE_RADIO;
2102 else
2103 abort ();
2105 wv->selected = !NILP (selected);
2106 if (STRINGP (help))
2107 wv->help = XSTRING (help)->data;
2109 prev_wv = wv;
2111 i += MENU_ITEMS_ITEM_LENGTH;
2115 /* Deal with the title, if it is non-nil. */
2116 if (!NILP (title))
2118 widget_value *wv_title = xmalloc_widget_value ();
2119 widget_value *wv_sep1 = xmalloc_widget_value ();
2120 widget_value *wv_sep2 = xmalloc_widget_value ();
2122 wv_sep2->name = "--";
2123 wv_sep2->next = first_wv->contents;
2125 wv_sep1->name = "--";
2126 wv_sep1->next = wv_sep2;
2128 #ifndef HAVE_MULTILINGUAL_MENU
2129 if (STRING_MULTIBYTE (title))
2130 title = string_make_unibyte (title);
2131 #endif
2132 wv_title->name = (char *) XSTRING (title)->data;
2133 wv_title->enabled = True;
2134 wv_title->button_type = BUTTON_TYPE_NONE;
2135 wv_title->next = wv_sep1;
2136 first_wv->contents = wv_title;
2139 /* Actually create the menu. */
2140 menu_id = widget_id_tick++;
2141 menu = lw_create_widget ("popup", first_wv->name, menu_id, first_wv,
2142 f->output_data.x->widget, 1, 0,
2143 popup_selection_callback,
2144 popup_deactivate_callback,
2145 menu_highlight_callback);
2147 /* Adjust coordinates to relative to the outer (window manager) window. */
2149 Window child;
2150 int win_x = 0, win_y = 0;
2152 /* Find the position of the outside upper-left corner of
2153 the inner window, with respect to the outer window. */
2154 if (f->output_data.x->parent_desc != FRAME_X_DISPLAY_INFO (f)->root_window)
2156 BLOCK_INPUT;
2157 XTranslateCoordinates (FRAME_X_DISPLAY (f),
2159 /* From-window, to-window. */
2160 f->output_data.x->window_desc,
2161 f->output_data.x->parent_desc,
2163 /* From-position, to-position. */
2164 0, 0, &win_x, &win_y,
2166 /* Child of window. */
2167 &child);
2168 UNBLOCK_INPUT;
2169 x += win_x;
2170 y += win_y;
2174 /* Adjust coordinates to be root-window-relative. */
2175 x += f->output_data.x->left_pos;
2176 y += f->output_data.x->top_pos;
2178 dummy.type = ButtonPress;
2179 dummy.serial = 0;
2180 dummy.send_event = 0;
2181 dummy.display = FRAME_X_DISPLAY (f);
2182 dummy.time = CurrentTime;
2183 dummy.root = FRAME_X_DISPLAY_INFO (f)->root_window;
2184 dummy.window = dummy.root;
2185 dummy.subwindow = dummy.root;
2186 dummy.x_root = x;
2187 dummy.y_root = y;
2188 dummy.x = x;
2189 dummy.y = y;
2190 dummy.state = (FRAME_X_DISPLAY_INFO (f)->grabbed >> 1) * Button1Mask;
2191 dummy.button = 0;
2192 for (i = 0; i < 5; i++)
2193 if (FRAME_X_DISPLAY_INFO (f)->grabbed & (1 << i))
2194 dummy.button = i;
2196 /* Don't allow any geometry request from the user. */
2197 XtSetArg (av[ac], XtNgeometry, 0); ac++;
2198 XtSetValues (menu, av, ac);
2200 /* Free the widget_value objects we used to specify the contents. */
2201 free_menubar_widget_value_tree (first_wv);
2203 /* Override any default settings with ones from the `menu' face. */
2204 x_set_menu_resources_from_menu_face (f, menu);
2206 /* No selection has been chosen yet. */
2207 menu_item_selection = 0;
2209 /* Display the menu. */
2210 lw_popup_menu (menu, (XEvent *) &dummy);
2211 popup_activated_flag = 1;
2213 /* Process events that apply to the menu. */
2214 popup_get_selection ((XEvent *) 0, FRAME_X_DISPLAY_INFO (f), menu_id);
2216 #ifdef LESSTIF_VERSION
2217 /* Nov 1998: For an unknown reason a button grab remains active
2218 after the popup menu has gone. */
2219 XUngrabButton (XtDisplay (f->output_data.x->widget),
2220 AnyButton, AnyModifier,
2221 XtWindow (f->output_data.x->widget));
2222 XUngrabButton (XtDisplay (f->output_data.x->edit_widget),
2223 AnyButton, AnyModifier,
2224 XtWindow (f->output_data.x->edit_widget));
2225 #endif /* LESSTIF_VERSION */
2227 /* fp turned off the following statement and wrote a comment
2228 that it is unnecessary--that the menu has already disappeared.
2229 Nowadays the menu disappears ok, all right, but
2230 we need to delete the widgets or multiple ones will pile up. */
2231 lw_destroy_all_widgets (menu_id);
2233 /* Find the selected item, and its pane, to return
2234 the proper value. */
2235 if (menu_item_selection != 0)
2237 Lisp_Object prefix, entry;
2239 prefix = entry = Qnil;
2240 i = 0;
2241 while (i < menu_items_used)
2243 if (EQ (XVECTOR (menu_items)->contents[i], Qnil))
2245 subprefix_stack[submenu_depth++] = prefix;
2246 prefix = entry;
2247 i++;
2249 else if (EQ (XVECTOR (menu_items)->contents[i], Qlambda))
2251 prefix = subprefix_stack[--submenu_depth];
2252 i++;
2254 else if (EQ (XVECTOR (menu_items)->contents[i], Qt))
2256 prefix
2257 = XVECTOR (menu_items)->contents[i + MENU_ITEMS_PANE_PREFIX];
2258 i += MENU_ITEMS_PANE_LENGTH;
2260 /* Ignore a nil in the item list.
2261 It's meaningful only for dialog boxes. */
2262 else if (EQ (XVECTOR (menu_items)->contents[i], Qquote))
2263 i += 1;
2264 else
2266 entry
2267 = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_VALUE];
2268 if (menu_item_selection == &XVECTOR (menu_items)->contents[i])
2270 if (keymaps != 0)
2272 int j;
2274 entry = Fcons (entry, Qnil);
2275 if (!NILP (prefix))
2276 entry = Fcons (prefix, entry);
2277 for (j = submenu_depth - 1; j >= 0; j--)
2278 if (!NILP (subprefix_stack[j]))
2279 entry = Fcons (subprefix_stack[j], entry);
2281 return entry;
2283 i += MENU_ITEMS_ITEM_LENGTH;
2288 return Qnil;
2291 static void
2292 dialog_selection_callback (widget, id, client_data)
2293 Widget widget;
2294 LWLIB_ID id;
2295 XtPointer client_data;
2297 /* The EMACS_INT cast avoids a warning. There's no problem
2298 as long as pointers have enough bits to hold small integers. */
2299 if ((int) (EMACS_INT) client_data != -1)
2300 menu_item_selection = (Lisp_Object *) client_data;
2301 BLOCK_INPUT;
2302 lw_destroy_all_widgets (id);
2303 UNBLOCK_INPUT;
2304 popup_activated_flag = 0;
2307 static char * button_names [] = {
2308 "button1", "button2", "button3", "button4", "button5",
2309 "button6", "button7", "button8", "button9", "button10" };
2311 static Lisp_Object
2312 xdialog_show (f, keymaps, title, error)
2313 FRAME_PTR f;
2314 int keymaps;
2315 Lisp_Object title;
2316 char **error;
2318 int i, nb_buttons=0;
2319 LWLIB_ID dialog_id;
2320 Widget menu;
2321 char dialog_name[6];
2323 widget_value *wv, *first_wv = 0, *prev_wv = 0;
2325 /* Number of elements seen so far, before boundary. */
2326 int left_count = 0;
2327 /* 1 means we've seen the boundary between left-hand elts and right-hand. */
2328 int boundary_seen = 0;
2330 *error = NULL;
2332 if (menu_items_n_panes > 1)
2334 *error = "Multiple panes in dialog box";
2335 return Qnil;
2338 /* Create a tree of widget_value objects
2339 representing the text label and buttons. */
2341 Lisp_Object pane_name, prefix;
2342 char *pane_string;
2343 pane_name = XVECTOR (menu_items)->contents[MENU_ITEMS_PANE_NAME];
2344 prefix = XVECTOR (menu_items)->contents[MENU_ITEMS_PANE_PREFIX];
2345 pane_string = (NILP (pane_name)
2346 ? "" : (char *) XSTRING (pane_name)->data);
2347 prev_wv = xmalloc_widget_value ();
2348 prev_wv->value = pane_string;
2349 if (keymaps && !NILP (prefix))
2350 prev_wv->name++;
2351 prev_wv->enabled = 1;
2352 prev_wv->name = "message";
2353 first_wv = prev_wv;
2355 /* Loop over all panes and items, filling in the tree. */
2356 i = MENU_ITEMS_PANE_LENGTH;
2357 while (i < menu_items_used)
2360 /* Create a new item within current pane. */
2361 Lisp_Object item_name, enable, descrip;
2362 item_name = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_NAME];
2363 enable = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_ENABLE];
2364 descrip
2365 = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_EQUIV_KEY];
2367 if (NILP (item_name))
2369 free_menubar_widget_value_tree (first_wv);
2370 *error = "Submenu in dialog items";
2371 return Qnil;
2373 if (EQ (item_name, Qquote))
2375 /* This is the boundary between left-side elts
2376 and right-side elts. Stop incrementing right_count. */
2377 boundary_seen = 1;
2378 i++;
2379 continue;
2381 if (nb_buttons >= 9)
2383 free_menubar_widget_value_tree (first_wv);
2384 *error = "Too many dialog items";
2385 return Qnil;
2388 wv = xmalloc_widget_value ();
2389 prev_wv->next = wv;
2390 wv->name = (char *) button_names[nb_buttons];
2391 if (!NILP (descrip))
2392 wv->key = (char *) XSTRING (descrip)->data;
2393 wv->value = (char *) XSTRING (item_name)->data;
2394 wv->call_data = (void *) &XVECTOR (menu_items)->contents[i];
2395 wv->enabled = !NILP (enable);
2396 prev_wv = wv;
2398 if (! boundary_seen)
2399 left_count++;
2401 nb_buttons++;
2402 i += MENU_ITEMS_ITEM_LENGTH;
2405 /* If the boundary was not specified,
2406 by default put half on the left and half on the right. */
2407 if (! boundary_seen)
2408 left_count = nb_buttons - nb_buttons / 2;
2410 wv = xmalloc_widget_value ();
2411 wv->name = dialog_name;
2413 /* Dialog boxes use a really stupid name encoding
2414 which specifies how many buttons to use
2415 and how many buttons are on the right.
2416 The Q means something also. */
2417 dialog_name[0] = 'Q';
2418 dialog_name[1] = '0' + nb_buttons;
2419 dialog_name[2] = 'B';
2420 dialog_name[3] = 'R';
2421 /* Number of buttons to put on the right. */
2422 dialog_name[4] = '0' + nb_buttons - left_count;
2423 dialog_name[5] = 0;
2424 wv->contents = first_wv;
2425 first_wv = wv;
2428 /* Actually create the dialog. */
2429 dialog_id = widget_id_tick++;
2430 menu = lw_create_widget (first_wv->name, "dialog", dialog_id, first_wv,
2431 f->output_data.x->widget, 1, 0,
2432 dialog_selection_callback, 0, 0);
2433 lw_modify_all_widgets (dialog_id, first_wv->contents, True);
2434 /* Free the widget_value objects we used to specify the contents. */
2435 free_menubar_widget_value_tree (first_wv);
2437 /* No selection has been chosen yet. */
2438 menu_item_selection = 0;
2440 /* Display the menu. */
2441 lw_pop_up_all_widgets (dialog_id);
2442 popup_activated_flag = 1;
2444 /* Process events that apply to the menu. */
2445 popup_get_selection ((XEvent *) 0, FRAME_X_DISPLAY_INFO (f), dialog_id);
2447 lw_destroy_all_widgets (dialog_id);
2449 /* Find the selected item, and its pane, to return
2450 the proper value. */
2451 if (menu_item_selection != 0)
2453 Lisp_Object prefix;
2455 prefix = Qnil;
2456 i = 0;
2457 while (i < menu_items_used)
2459 Lisp_Object entry;
2461 if (EQ (XVECTOR (menu_items)->contents[i], Qt))
2463 prefix
2464 = XVECTOR (menu_items)->contents[i + MENU_ITEMS_PANE_PREFIX];
2465 i += MENU_ITEMS_PANE_LENGTH;
2467 else if (EQ (XVECTOR (menu_items)->contents[i], Qquote))
2469 /* This is the boundary between left-side elts and
2470 right-side elts. */
2471 ++i;
2473 else
2475 entry
2476 = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_VALUE];
2477 if (menu_item_selection == &XVECTOR (menu_items)->contents[i])
2479 if (keymaps != 0)
2481 entry = Fcons (entry, Qnil);
2482 if (!NILP (prefix))
2483 entry = Fcons (prefix, entry);
2485 return entry;
2487 i += MENU_ITEMS_ITEM_LENGTH;
2492 return Qnil;
2495 #else /* not USE_X_TOOLKIT */
2497 /* The frame of the last activated non-toolkit menu bar.
2498 Used to generate menu help events. */
2500 static struct frame *menu_help_frame;
2503 /* Show help HELP_STRING, or clear help if HELP_STRING is null.
2505 PANE is the pane number, and ITEM is the menu item number in
2506 the menu (currently not used).
2508 This cannot be done with generating a HELP_EVENT because
2509 XMenuActivate contains a loop that doesn't let Emacs process
2510 keyboard events. */
2512 static void
2513 menu_help_callback (help_string, pane, item)
2514 char *help_string;
2515 int pane, item;
2517 extern Lisp_Object Qmenu_item;
2518 Lisp_Object *first_item;
2519 Lisp_Object pane_name;
2520 Lisp_Object menu_object;
2522 first_item = XVECTOR (menu_items)->contents;
2523 if (EQ (first_item[0], Qt))
2524 pane_name = first_item[MENU_ITEMS_PANE_NAME];
2525 else if (EQ (first_item[0], Qquote))
2526 /* This shouldn't happen, see xmenu_show. */
2527 pane_name = build_string ("");
2528 else
2529 pane_name = first_item[MENU_ITEMS_ITEM_NAME];
2531 /* (menu-item MENU-NAME PANE-NUMBER) */
2532 menu_object = Fcons (Qmenu_item,
2533 Fcons (pane_name,
2534 Fcons (make_number (pane), Qnil)));
2535 show_help_echo (help_string ? build_string (help_string) : Qnil,
2536 Qnil, menu_object, make_number (item), 1);
2540 static Lisp_Object
2541 xmenu_show (f, x, y, for_click, keymaps, title, error)
2542 FRAME_PTR f;
2543 int x, y;
2544 int for_click;
2545 int keymaps;
2546 Lisp_Object title;
2547 char **error;
2549 Window root;
2550 XMenu *menu;
2551 int pane, selidx, lpane, status;
2552 Lisp_Object entry, pane_prefix;
2553 char *datap;
2554 int ulx, uly, width, height;
2555 int dispwidth, dispheight;
2556 int i, j;
2557 int maxwidth;
2558 int dummy_int;
2559 unsigned int dummy_uint;
2561 *error = 0;
2562 if (menu_items_n_panes == 0)
2563 return Qnil;
2565 if (menu_items_used <= MENU_ITEMS_PANE_LENGTH)
2567 *error = "Empty menu";
2568 return Qnil;
2571 /* Figure out which root window F is on. */
2572 XGetGeometry (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), &root,
2573 &dummy_int, &dummy_int, &dummy_uint, &dummy_uint,
2574 &dummy_uint, &dummy_uint);
2576 /* Make the menu on that window. */
2577 menu = XMenuCreate (FRAME_X_DISPLAY (f), root, "emacs");
2578 if (menu == NULL)
2580 *error = "Can't create menu";
2581 return Qnil;
2584 #ifdef HAVE_X_WINDOWS
2585 /* Adjust coordinates to relative to the outer (window manager) window. */
2587 Window child;
2588 int win_x = 0, win_y = 0;
2590 /* Find the position of the outside upper-left corner of
2591 the inner window, with respect to the outer window. */
2592 if (f->output_data.x->parent_desc != FRAME_X_DISPLAY_INFO (f)->root_window)
2594 BLOCK_INPUT;
2595 XTranslateCoordinates (FRAME_X_DISPLAY (f),
2597 /* From-window, to-window. */
2598 f->output_data.x->window_desc,
2599 f->output_data.x->parent_desc,
2601 /* From-position, to-position. */
2602 0, 0, &win_x, &win_y,
2604 /* Child of window. */
2605 &child);
2606 UNBLOCK_INPUT;
2607 x += win_x;
2608 y += win_y;
2611 #endif /* HAVE_X_WINDOWS */
2613 /* Adjust coordinates to be root-window-relative. */
2614 x += f->output_data.x->left_pos;
2615 y += f->output_data.x->top_pos;
2617 /* Create all the necessary panes and their items. */
2618 i = 0;
2619 while (i < menu_items_used)
2621 if (EQ (XVECTOR (menu_items)->contents[i], Qt))
2623 /* Create a new pane. */
2624 Lisp_Object pane_name, prefix;
2625 char *pane_string;
2627 pane_name = XVECTOR (menu_items)->contents[i + MENU_ITEMS_PANE_NAME];
2628 prefix = XVECTOR (menu_items)->contents[i + MENU_ITEMS_PANE_PREFIX];
2629 pane_string = (NILP (pane_name)
2630 ? "" : (char *) XSTRING (pane_name)->data);
2631 if (keymaps && !NILP (prefix))
2632 pane_string++;
2634 lpane = XMenuAddPane (FRAME_X_DISPLAY (f), menu, pane_string, TRUE);
2635 if (lpane == XM_FAILURE)
2637 XMenuDestroy (FRAME_X_DISPLAY (f), menu);
2638 *error = "Can't create pane";
2639 return Qnil;
2641 i += MENU_ITEMS_PANE_LENGTH;
2643 /* Find the width of the widest item in this pane. */
2644 maxwidth = 0;
2645 j = i;
2646 while (j < menu_items_used)
2648 Lisp_Object item;
2649 item = XVECTOR (menu_items)->contents[j];
2650 if (EQ (item, Qt))
2651 break;
2652 if (NILP (item))
2654 j++;
2655 continue;
2657 width = STRING_BYTES (XSTRING (item));
2658 if (width > maxwidth)
2659 maxwidth = width;
2661 j += MENU_ITEMS_ITEM_LENGTH;
2664 /* Ignore a nil in the item list.
2665 It's meaningful only for dialog boxes. */
2666 else if (EQ (XVECTOR (menu_items)->contents[i], Qquote))
2667 i += 1;
2668 else
2670 /* Create a new item within current pane. */
2671 Lisp_Object item_name, enable, descrip, help;
2672 unsigned char *item_data;
2673 char *help_string;
2675 item_name = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_NAME];
2676 enable = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_ENABLE];
2677 descrip
2678 = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_EQUIV_KEY];
2679 help = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_HELP];
2680 help_string = STRINGP (help) ? XSTRING (help)->data : NULL;
2682 if (!NILP (descrip))
2684 int gap = maxwidth - STRING_BYTES (XSTRING (item_name));
2685 #ifdef C_ALLOCA
2686 Lisp_Object spacer;
2687 spacer = Fmake_string (make_number (gap), make_number (' '));
2688 item_name = concat2 (item_name, spacer);
2689 item_name = concat2 (item_name, descrip);
2690 item_data = XSTRING (item_name)->data;
2691 #else
2692 /* if alloca is fast, use that to make the space,
2693 to reduce gc needs. */
2694 item_data
2695 = (unsigned char *) alloca (maxwidth
2696 + STRING_BYTES (XSTRING (descrip)) + 1);
2697 bcopy (XSTRING (item_name)->data, item_data,
2698 STRING_BYTES (XSTRING (item_name)));
2699 for (j = XSTRING (item_name)->size; j < maxwidth; j++)
2700 item_data[j] = ' ';
2701 bcopy (XSTRING (descrip)->data, item_data + j,
2702 STRING_BYTES (XSTRING (descrip)));
2703 item_data[j + STRING_BYTES (XSTRING (descrip))] = 0;
2704 #endif
2706 else
2707 item_data = XSTRING (item_name)->data;
2709 if (XMenuAddSelection (FRAME_X_DISPLAY (f),
2710 menu, lpane, 0, item_data,
2711 !NILP (enable), help_string)
2712 == XM_FAILURE)
2714 XMenuDestroy (FRAME_X_DISPLAY (f), menu);
2715 *error = "Can't add selection to menu";
2716 return Qnil;
2718 i += MENU_ITEMS_ITEM_LENGTH;
2722 /* All set and ready to fly. */
2723 XMenuRecompute (FRAME_X_DISPLAY (f), menu);
2724 dispwidth = DisplayWidth (FRAME_X_DISPLAY (f),
2725 XScreenNumberOfScreen (FRAME_X_SCREEN (f)));
2726 dispheight = DisplayHeight (FRAME_X_DISPLAY (f),
2727 XScreenNumberOfScreen (FRAME_X_SCREEN (f)));
2728 x = min (x, dispwidth);
2729 y = min (y, dispheight);
2730 x = max (x, 1);
2731 y = max (y, 1);
2732 XMenuLocate (FRAME_X_DISPLAY (f), menu, 0, 0, x, y,
2733 &ulx, &uly, &width, &height);
2734 if (ulx+width > dispwidth)
2736 x -= (ulx + width) - dispwidth;
2737 ulx = dispwidth - width;
2739 if (uly+height > dispheight)
2741 y -= (uly + height) - dispheight;
2742 uly = dispheight - height;
2744 if (ulx < 0) x -= ulx;
2745 if (uly < 0) y -= uly;
2747 XMenuSetAEQ (menu, TRUE);
2748 XMenuSetFreeze (menu, TRUE);
2749 pane = selidx = 0;
2751 /* Help display under X won't work because XMenuActivate contains
2752 a loop that doesn't give Emacs a chance to process it. */
2753 menu_help_frame = f;
2754 status = XMenuActivate (FRAME_X_DISPLAY (f), menu, &pane, &selidx,
2755 x, y, ButtonReleaseMask, &datap,
2756 menu_help_callback);
2759 #ifdef HAVE_X_WINDOWS
2760 /* Assume the mouse has moved out of the X window.
2761 If it has actually moved in, we will get an EnterNotify. */
2762 x_mouse_leave (FRAME_X_DISPLAY_INFO (f));
2763 #endif
2765 switch (status)
2767 case XM_SUCCESS:
2768 #ifdef XDEBUG
2769 fprintf (stderr, "pane= %d line = %d\n", panes, selidx);
2770 #endif
2772 /* Find the item number SELIDX in pane number PANE. */
2773 i = 0;
2774 while (i < menu_items_used)
2776 if (EQ (XVECTOR (menu_items)->contents[i], Qt))
2778 if (pane == 0)
2779 pane_prefix
2780 = XVECTOR (menu_items)->contents[i + MENU_ITEMS_PANE_PREFIX];
2781 pane--;
2782 i += MENU_ITEMS_PANE_LENGTH;
2784 else
2786 if (pane == -1)
2788 if (selidx == 0)
2790 entry
2791 = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_VALUE];
2792 if (keymaps != 0)
2794 entry = Fcons (entry, Qnil);
2795 if (!NILP (pane_prefix))
2796 entry = Fcons (pane_prefix, entry);
2798 break;
2800 selidx--;
2802 i += MENU_ITEMS_ITEM_LENGTH;
2805 break;
2807 case XM_FAILURE:
2808 *error = "Can't activate menu";
2809 case XM_IA_SELECT:
2810 case XM_NO_SELECT:
2811 entry = Qnil;
2812 break;
2814 XMenuDestroy (FRAME_X_DISPLAY (f), menu);
2816 #ifdef HAVE_X_WINDOWS
2817 /* State that no mouse buttons are now held.
2818 (The oldXMenu code doesn't track this info for us.)
2819 That is not necessarily true, but the fiction leads to reasonable
2820 results, and it is a pain to ask which are actually held now. */
2821 FRAME_X_DISPLAY_INFO (f)->grabbed = 0;
2822 #endif
2824 return entry;
2827 #endif /* not USE_X_TOOLKIT */
2829 #endif /* HAVE_MENUS */
2831 void
2832 syms_of_xmenu ()
2834 staticpro (&menu_items);
2835 menu_items = Qnil;
2837 Qdebug_on_next_call = intern ("debug-on-next-call");
2838 staticpro (&Qdebug_on_next_call);
2840 DEFVAR_LISP ("menu-updating-frame", &Vmenu_updating_frame,
2841 "Frame for which we are updating a menu.\n\
2842 The enable predicate for a menu command should check this variable.");
2843 Vmenu_updating_frame = Qnil;
2845 #ifdef USE_X_TOOLKIT
2846 widget_id_tick = (1<<16);
2847 next_menubar_widget_id = 1;
2848 #endif
2850 defsubr (&Sx_popup_menu);
2851 #ifdef HAVE_MENUS
2852 defsubr (&Sx_popup_dialog);
2853 #endif