(Fmake_temp_name): forgot the \n\ in the docstring
[emacs.git] / src / xmenu.c
blobfde77cad82d0682f96c0ab6248703ade5b3e5b2b
1 /* X Communication module for terminals which understand the X protocol.
2 Copyright (C) 1986, 1988, 1993, 1994, 1996 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 /* On 4.3 this loses if it comes after xterm.h. */
34 #include <signal.h>
35 #include <config.h>
36 #ifdef HAVE_STDLIB_H
37 #include <stdlib.h>
38 #endif
40 #include <stdio.h>
41 #include "lisp.h"
42 #include "termhooks.h"
43 #include "frame.h"
44 #include "window.h"
45 #include "keyboard.h"
46 #include "blockinput.h"
47 #include "buffer.h"
49 #ifdef MSDOS
50 #include "msdos.h"
51 #endif
53 #ifdef HAVE_X_WINDOWS
54 /* This may include sys/types.h, and that somehow loses
55 if this is not done before the other system files. */
56 #include "xterm.h"
57 #endif
59 /* Load sys/types.h if not already loaded.
60 In some systems loading it twice is suicidal. */
61 #ifndef makedev
62 #include <sys/types.h>
63 #endif
65 #include "dispextern.h"
67 #ifdef HAVE_X_WINDOWS
68 #undef HAVE_MULTILINGUAL_MENU
69 #ifdef USE_X_TOOLKIT
70 #include <X11/Xlib.h>
71 #include <X11/IntrinsicP.h>
72 #include <X11/CoreP.h>
73 #include <X11/StringDefs.h>
74 #include <X11/Shell.h>
75 #ifdef USE_LUCID
76 #include <X11/Xaw/Paned.h>
77 #endif /* USE_LUCID */
78 #include "../lwlib/lwlib.h"
79 #else /* not USE_X_TOOLKIT */
80 #include "../oldXMenu/XMenu.h"
81 #endif /* not USE_X_TOOLKIT */
82 #endif /* HAVE_X_WINDOWS */
84 #ifdef USE_MOTIF
85 #include <Xm/Xm.h> /* for LESSTIF_VERSION */
86 #endif
88 #define min(x,y) (((x) < (y)) ? (x) : (y))
89 #define max(x,y) (((x) > (y)) ? (x) : (y))
91 #ifndef TRUE
92 #define TRUE 1
93 #define FALSE 0
94 #endif /* no TRUE */
96 Lisp_Object Vmenu_updating_frame;
98 Lisp_Object Qdebug_on_next_call;
100 extern Lisp_Object Qmenu_bar;
101 extern Lisp_Object Qmouse_click, Qevent_kind;
103 extern Lisp_Object QCtoggle, QCradio;
105 extern Lisp_Object Voverriding_local_map;
106 extern Lisp_Object Voverriding_local_map_menu_flag;
108 extern Lisp_Object Qoverriding_local_map, Qoverriding_terminal_local_map;
110 extern Lisp_Object Qmenu_bar_update_hook;
112 #ifdef USE_X_TOOLKIT
113 extern void set_frame_menubar ();
114 extern void process_expose_from_menu ();
115 extern XtAppContext Xt_app_con;
117 static Lisp_Object xdialog_show ();
118 void popup_get_selection ();
119 #endif
121 #ifdef USE_X_TOOLKIT
123 /* Define HAVE_BOXES if meus can handle radio and toggle buttons. */
125 #define HAVE_BOXES 1
126 #endif
128 static void push_menu_item P_ ((Lisp_Object, Lisp_Object, Lisp_Object,
129 Lisp_Object, Lisp_Object, Lisp_Object,
130 Lisp_Object));
131 static Lisp_Object xmenu_show ();
132 static void keymap_panes ();
133 static void single_keymap_panes ();
134 static void single_menu_item ();
135 static void list_of_panes ();
136 static void list_of_items ();
138 /* This holds a Lisp vector that holds the results of decoding
139 the keymaps or alist-of-alists that specify a menu.
141 It describes the panes and items within the panes.
143 Each pane is described by 3 elements in the vector:
144 t, the pane name, the pane's prefix key.
145 Then follow the pane's items, with 5 elements per item:
146 the item string, the enable flag, the item's value,
147 the definition, and the equivalent keyboard key's description string.
149 In some cases, multiple levels of menus may be described.
150 A single vector slot containing nil indicates the start of a submenu.
151 A single vector slot containing lambda indicates the end of a submenu.
152 The submenu follows a menu item which is the way to reach the submenu.
154 A single vector slot containing quote indicates that the
155 following items should appear on the right of a dialog box.
157 Using a Lisp vector to hold this information while we decode it
158 takes care of protecting all the data from GC. */
160 #define MENU_ITEMS_PANE_NAME 1
161 #define MENU_ITEMS_PANE_PREFIX 2
162 #define MENU_ITEMS_PANE_LENGTH 3
164 #define MENU_ITEMS_ITEM_NAME 0
165 #define MENU_ITEMS_ITEM_ENABLE 1
166 #define MENU_ITEMS_ITEM_VALUE 2
167 #define MENU_ITEMS_ITEM_EQUIV_KEY 3
168 #define MENU_ITEMS_ITEM_DEFINITION 4
169 #define MENU_ITEMS_ITEM_TYPE 5
170 #define MENU_ITEMS_ITEM_SELECTED 6
171 #define MENU_ITEMS_ITEM_LENGTH 7
173 static Lisp_Object menu_items;
175 /* Number of slots currently allocated in menu_items. */
176 static int menu_items_allocated;
178 /* This is the index in menu_items of the first empty slot. */
179 static int menu_items_used;
181 /* The number of panes currently recorded in menu_items,
182 excluding those within submenus. */
183 static int menu_items_n_panes;
185 /* Current depth within submenus. */
186 static int menu_items_submenu_depth;
188 /* Flag which when set indicates a dialog or menu has been posted by
189 Xt on behalf of one of the widget sets. */
190 static int popup_activated_flag;
192 static int next_menubar_widget_id;
194 /* This is set nonzero after the user activates the menu bar, and set
195 to zero again after the menu bars are redisplayed by prepare_menu_bar.
196 While it is nonzero, all calls to set_frame_menubar go deep.
198 I don't understand why this is needed, but it does seem to be
199 needed on Motif, according to Marcus Daniels <marcus@sysc.pdx.edu>. */
201 int pending_menu_activation;
203 #ifdef USE_X_TOOLKIT
205 /* Return the frame whose ->output_data.x->id equals ID, or 0 if none. */
207 static struct frame *
208 menubar_id_to_frame (id)
209 LWLIB_ID id;
211 Lisp_Object tail, frame;
212 FRAME_PTR f;
214 for (tail = Vframe_list; GC_CONSP (tail); tail = XCDR (tail))
216 frame = XCAR (tail);
217 if (!GC_FRAMEP (frame))
218 continue;
219 f = XFRAME (frame);
220 if (f->output_data.nothing == 1)
221 continue;
222 if (f->output_data.x->id == id)
223 return f;
225 return 0;
228 #endif
230 /* Initialize the menu_items structure if we haven't already done so.
231 Also mark it as currently empty. */
233 static void
234 init_menu_items ()
236 if (NILP (menu_items))
238 menu_items_allocated = 60;
239 menu_items = Fmake_vector (make_number (menu_items_allocated), Qnil);
242 menu_items_used = 0;
243 menu_items_n_panes = 0;
244 menu_items_submenu_depth = 0;
247 /* Call at the end of generating the data in menu_items.
248 This fills in the number of items in the last pane. */
250 static void
251 finish_menu_items ()
255 /* Call when finished using the data for the current menu
256 in menu_items. */
258 static void
259 discard_menu_items ()
261 /* Free the structure if it is especially large.
262 Otherwise, hold on to it, to save time. */
263 if (menu_items_allocated > 200)
265 menu_items = Qnil;
266 menu_items_allocated = 0;
270 /* Make the menu_items vector twice as large. */
272 static void
273 grow_menu_items ()
275 Lisp_Object old;
276 int old_size = menu_items_allocated;
277 old = menu_items;
279 menu_items_allocated *= 2;
280 menu_items = Fmake_vector (make_number (menu_items_allocated), Qnil);
281 bcopy (XVECTOR (old)->contents, XVECTOR (menu_items)->contents,
282 old_size * sizeof (Lisp_Object));
285 /* Begin a submenu. */
287 static void
288 push_submenu_start ()
290 if (menu_items_used + 1 > menu_items_allocated)
291 grow_menu_items ();
293 XVECTOR (menu_items)->contents[menu_items_used++] = Qnil;
294 menu_items_submenu_depth++;
297 /* End a submenu. */
299 static void
300 push_submenu_end ()
302 if (menu_items_used + 1 > menu_items_allocated)
303 grow_menu_items ();
305 XVECTOR (menu_items)->contents[menu_items_used++] = Qlambda;
306 menu_items_submenu_depth--;
309 /* Indicate boundary between left and right. */
311 static void
312 push_left_right_boundary ()
314 if (menu_items_used + 1 > menu_items_allocated)
315 grow_menu_items ();
317 XVECTOR (menu_items)->contents[menu_items_used++] = Qquote;
320 /* Start a new menu pane in menu_items..
321 NAME is the pane name. PREFIX_VEC is a prefix key for this pane. */
323 static void
324 push_menu_pane (name, prefix_vec)
325 Lisp_Object name, prefix_vec;
327 if (menu_items_used + MENU_ITEMS_PANE_LENGTH > menu_items_allocated)
328 grow_menu_items ();
330 if (menu_items_submenu_depth == 0)
331 menu_items_n_panes++;
332 XVECTOR (menu_items)->contents[menu_items_used++] = Qt;
333 XVECTOR (menu_items)->contents[menu_items_used++] = name;
334 XVECTOR (menu_items)->contents[menu_items_used++] = prefix_vec;
337 /* Push one menu item into the current pane. NAME is the string to
338 display. ENABLE if non-nil means this item can be selected. KEY
339 is the key generated by choosing this item, or nil if this item
340 doesn't really have a definition. DEF is the definition of this
341 item. EQUIV is the textual description of the keyboard equivalent
342 for this item (or nil if none). TYPE is the type of this menu
343 item, one of nil, `toggle' or `radio'. */
345 static void
346 push_menu_item (name, enable, key, def, equiv, type, selected)
347 Lisp_Object name, enable, key, def, equiv, type, selected;
349 if (menu_items_used + MENU_ITEMS_ITEM_LENGTH > menu_items_allocated)
350 grow_menu_items ();
352 XVECTOR (menu_items)->contents[menu_items_used++] = name;
353 XVECTOR (menu_items)->contents[menu_items_used++] = enable;
354 XVECTOR (menu_items)->contents[menu_items_used++] = key;
355 XVECTOR (menu_items)->contents[menu_items_used++] = equiv;
356 XVECTOR (menu_items)->contents[menu_items_used++] = def;
357 XVECTOR (menu_items)->contents[menu_items_used++] = type;
358 XVECTOR (menu_items)->contents[menu_items_used++] = selected;
361 /* Look through KEYMAPS, a vector of keymaps that is NMAPS long,
362 and generate menu panes for them in menu_items.
363 If NOTREAL is nonzero,
364 don't bother really computing whether an item is enabled. */
366 static void
367 keymap_panes (keymaps, nmaps, notreal)
368 Lisp_Object *keymaps;
369 int nmaps;
370 int notreal;
372 int mapno;
374 init_menu_items ();
376 /* Loop over the given keymaps, making a pane for each map.
377 But don't make a pane that is empty--ignore that map instead.
378 P is the number of panes we have made so far. */
379 for (mapno = 0; mapno < nmaps; mapno++)
380 single_keymap_panes (keymaps[mapno], Qnil, Qnil, notreal, 10);
382 finish_menu_items ();
385 /* This is a recursive subroutine of keymap_panes.
386 It handles one keymap, KEYMAP.
387 The other arguments are passed along
388 or point to local variables of the previous function.
389 If NOTREAL is nonzero, only check for equivalent key bindings, don't
390 evaluate expressions in menu items and don't make any menu.
392 If we encounter submenus deeper than MAXDEPTH levels, ignore them. */
394 static void
395 single_keymap_panes (keymap, pane_name, prefix, notreal, maxdepth)
396 Lisp_Object keymap;
397 Lisp_Object pane_name;
398 Lisp_Object prefix;
399 int notreal;
400 int maxdepth;
402 Lisp_Object pending_maps = Qnil;
403 Lisp_Object tail, item;
404 struct gcpro gcpro1, gcpro2;
405 int notbuttons = 0;
407 if (maxdepth <= 0)
408 return;
410 push_menu_pane (pane_name, prefix);
412 #ifndef HAVE_BOXES
413 /* Remember index for first item in this pane so we can go back and
414 add a prefix when (if) we see the first button. After that, notbuttons
415 is set to 0, to mark that we have seen a button and all non button
416 items need a prefix. */
417 notbuttons = menu_items_used;
418 #endif
420 for (tail = keymap; CONSP (tail); tail = XCDR (tail))
422 GCPRO2 (keymap, pending_maps);
423 /* Look at each key binding, and if it is a menu item add it
424 to this menu. */
425 item = XCAR (tail);
426 if (CONSP (item))
427 single_menu_item (XCAR (item), XCDR (item),
428 &pending_maps, notreal, maxdepth, &notbuttons);
429 else if (VECTORP (item))
431 /* Loop over the char values represented in the vector. */
432 int len = XVECTOR (item)->size;
433 int c;
434 for (c = 0; c < len; c++)
436 Lisp_Object character;
437 XSETFASTINT (character, c);
438 single_menu_item (character, XVECTOR (item)->contents[c],
439 &pending_maps, notreal, maxdepth, &notbuttons);
442 UNGCPRO;
445 /* Process now any submenus which want to be panes at this level. */
446 while (!NILP (pending_maps))
448 Lisp_Object elt, eltcdr, string;
449 elt = Fcar (pending_maps);
450 eltcdr = XCDR (elt);
451 string = XCAR (eltcdr);
452 /* We no longer discard the @ from the beginning of the string here.
453 Instead, we do this in xmenu_show. */
454 single_keymap_panes (Fcar (elt), string,
455 XCDR (eltcdr), notreal, maxdepth - 1);
456 pending_maps = Fcdr (pending_maps);
460 /* This is a subroutine of single_keymap_panes that handles one
461 keymap entry.
462 KEY is a key in a keymap and ITEM is its binding.
463 PENDING_MAPS_PTR points to a list of keymaps waiting to be made into
464 separate panes.
465 If NOTREAL is nonzero, only check for equivalent key bindings, don't
466 evaluate expressions in menu items and don't make any menu.
467 If we encounter submenus deeper than MAXDEPTH levels, ignore them.
468 NOTBUTTONS_PTR is only used when simulating toggle boxes and radio
469 buttons. It points to variable notbuttons in single_keymap_panes,
470 which keeps track of if we have seen a button in this menu or not. */
472 static void
473 single_menu_item (key, item, pending_maps_ptr, notreal, maxdepth,
474 notbuttons_ptr)
475 Lisp_Object key, item;
476 Lisp_Object *pending_maps_ptr;
477 int maxdepth, notreal;
478 int *notbuttons_ptr;
480 Lisp_Object map, item_string, enabled;
481 struct gcpro gcpro1, gcpro2;
482 int res;
484 /* Parse the menu item and leave the result in item_properties. */
485 GCPRO2 (key, item);
486 res = parse_menu_item (item, notreal, 0);
487 UNGCPRO;
488 if (!res)
489 return; /* Not a menu item. */
491 map = XVECTOR (item_properties)->contents[ITEM_PROPERTY_MAP];
493 if (notreal)
495 /* We don't want to make a menu, just traverse the keymaps to
496 precompute equivalent key bindings. */
497 if (!NILP (map))
498 single_keymap_panes (map, Qnil, key, 1, maxdepth - 1);
499 return;
502 enabled = XVECTOR (item_properties)->contents[ITEM_PROPERTY_ENABLE];
503 item_string = XVECTOR (item_properties)->contents[ITEM_PROPERTY_NAME];
505 if (!NILP (map) && XSTRING (item_string)->data[0] == '@')
507 if (!NILP (enabled))
508 /* An enabled separate pane. Remember this to handle it later. */
509 *pending_maps_ptr = Fcons (Fcons (map, Fcons (item_string, key)),
510 *pending_maps_ptr);
511 return;
514 #ifndef HAVE_BOXES
515 /* Simulate radio buttons and toggle boxes by putting a prefix in
516 front of them. */
518 Lisp_Object prefix = Qnil;
519 Lisp_Object type = XVECTOR (item_properties)->contents[ITEM_PROPERTY_TYPE];
520 if (!NILP (type))
522 Lisp_Object selected
523 = XVECTOR (item_properties)->contents[ITEM_PROPERTY_SELECTED];
525 if (*notbuttons_ptr)
526 /* The first button. Line up previous items in this menu. */
528 int index = *notbuttons_ptr; /* Index for first item this menu. */
529 int submenu = 0;
530 Lisp_Object tem;
531 while (index < menu_items_used)
534 = XVECTOR (menu_items)->contents[index + MENU_ITEMS_ITEM_NAME];
535 if (NILP (tem))
537 index++;
538 submenu++; /* Skip sub menu. */
540 else if (EQ (tem, Qlambda))
542 index++;
543 submenu--; /* End sub menu. */
545 else if (EQ (tem, Qt))
546 index += 3; /* Skip new pane marker. */
547 else if (EQ (tem, Qquote))
548 index++; /* Skip a left, right divider. */
549 else
551 if (!submenu && XSTRING (tem)->data[0] != '\0'
552 && XSTRING (tem)->data[0] != '-')
553 XVECTOR (menu_items)->contents[index + MENU_ITEMS_ITEM_NAME]
554 = concat2 (build_string (" "), tem);
555 index += MENU_ITEMS_ITEM_LENGTH;
558 *notbuttons_ptr = 0;
561 /* Calculate prefix, if any, for this item. */
562 if (EQ (type, QCtoggle))
563 prefix = build_string (NILP (selected) ? "[ ] " : "[X] ");
564 else if (EQ (type, QCradio))
565 prefix = build_string (NILP (selected) ? "( ) " : "(*) ");
567 /* Not a button. If we have earlier buttons, then we need a prefix. */
568 else if (!*notbuttons_ptr && XSTRING (item_string)->data[0] != '\0'
569 && XSTRING (item_string)->data[0] != '-')
570 prefix = build_string (" ");
572 if (!NILP (prefix))
573 item_string = concat2 (prefix, item_string);
575 #endif /* not HAVE_BOXES */
577 #ifndef USE_X_TOOLKIT
578 if (!NILP(map))
579 /* Indicate visually that this is a submenu. */
580 item_string = concat2 (item_string, build_string (" >"));
581 #endif
583 push_menu_item (item_string, enabled, key,
584 XVECTOR (item_properties)->contents[ITEM_PROPERTY_DEF],
585 XVECTOR (item_properties)->contents[ITEM_PROPERTY_KEYEQ],
586 XVECTOR (item_properties)->contents[ITEM_PROPERTY_TYPE],
587 XVECTOR (item_properties)->contents[ITEM_PROPERTY_SELECTED]);
589 #ifdef USE_X_TOOLKIT
590 /* Display a submenu using the toolkit. */
591 if (! (NILP (map) || NILP (enabled)))
593 push_submenu_start ();
594 single_keymap_panes (map, Qnil, key, 0, maxdepth - 1);
595 push_submenu_end ();
597 #endif
600 /* Push all the panes and items of a menu described by the
601 alist-of-alists MENU.
602 This handles old-fashioned calls to x-popup-menu. */
604 static void
605 list_of_panes (menu)
606 Lisp_Object menu;
608 Lisp_Object tail;
610 init_menu_items ();
612 for (tail = menu; !NILP (tail); tail = Fcdr (tail))
614 Lisp_Object elt, pane_name, pane_data;
615 elt = Fcar (tail);
616 pane_name = Fcar (elt);
617 CHECK_STRING (pane_name, 0);
618 push_menu_pane (pane_name, Qnil);
619 pane_data = Fcdr (elt);
620 CHECK_CONS (pane_data, 0);
621 list_of_items (pane_data);
624 finish_menu_items ();
627 /* Push the items in a single pane defined by the alist PANE. */
629 static void
630 list_of_items (pane)
631 Lisp_Object pane;
633 Lisp_Object tail, item, item1;
635 for (tail = pane; !NILP (tail); tail = Fcdr (tail))
637 item = Fcar (tail);
638 if (STRINGP (item))
639 push_menu_item (item, Qnil, Qnil, Qt, Qnil, Qnil, Qnil);
640 else if (NILP (item))
641 push_left_right_boundary ();
642 else
644 CHECK_CONS (item, 0);
645 item1 = Fcar (item);
646 CHECK_STRING (item1, 1);
647 push_menu_item (item1, Qt, Fcdr (item), Qt, Qnil, Qnil, Qnil);
652 DEFUN ("x-popup-menu", Fx_popup_menu, Sx_popup_menu, 2, 2, 0,
653 "Pop up a deck-of-cards menu and return user's selection.\n\
654 POSITION is a position specification. This is either a mouse button event\n\
655 or a list ((XOFFSET YOFFSET) WINDOW)\n\
656 where XOFFSET and YOFFSET are positions in pixels from the top left\n\
657 corner of WINDOW's frame. (WINDOW may be a frame object instead of a window.)\n\
658 This controls the position of the center of the first line\n\
659 in the first pane of the menu, not the top left of the menu as a whole.\n\
660 If POSITION is t, it means to use the current mouse position.\n\
662 MENU is a specifier for a menu. For the simplest case, MENU is a keymap.\n\
663 The menu items come from key bindings that have a menu string as well as\n\
664 a definition; actually, the \"definition\" in such a key binding looks like\n\
665 \(STRING . REAL-DEFINITION). To give the menu a title, put a string into\n\
666 the keymap as a top-level element.\n\n\
667 If REAL-DEFINITION is nil, that puts a nonselectable string in the menu.\n\
668 Otherwise, REAL-DEFINITION should be a valid key binding definition.\n\
670 You can also use a list of keymaps as MENU.\n\
671 Then each keymap makes a separate pane.\n\
672 When MENU is a keymap or a list of keymaps, the return value\n\
673 is a list of events.\n\n\
675 Alternatively, you can specify a menu of multiple panes\n\
676 with a list of the form (TITLE PANE1 PANE2...),\n\
677 where each pane is a list of form (TITLE ITEM1 ITEM2...).\n\
678 Each ITEM is normally a cons cell (STRING . VALUE);\n\
679 but a string can appear as an item--that makes a nonselectable line\n\
680 in the menu.\n\
681 With this form of menu, the return value is VALUE from the chosen item.\n\
683 If POSITION is nil, don't display the menu at all, just precalculate the\n\
684 cached information about equivalent key sequences.")
685 (position, menu)
686 Lisp_Object position, menu;
688 Lisp_Object keymap, tem;
689 int xpos, ypos;
690 Lisp_Object title;
691 char *error_name;
692 Lisp_Object selection;
693 FRAME_PTR f;
694 Lisp_Object x, y, window;
695 int keymaps = 0;
696 int for_click = 0;
697 struct gcpro gcpro1;
699 #ifdef HAVE_MENUS
700 if (! NILP (position))
702 check_x ();
704 /* Decode the first argument: find the window and the coordinates. */
705 if (EQ (position, Qt)
706 || (CONSP (position) && EQ (XCAR (position), Qmenu_bar)))
708 /* Use the mouse's current position. */
709 FRAME_PTR new_f = SELECTED_FRAME ();
710 Lisp_Object bar_window;
711 enum scroll_bar_part part;
712 unsigned long time;
714 if (mouse_position_hook)
715 (*mouse_position_hook) (&new_f, 1, &bar_window,
716 &part, &x, &y, &time);
717 if (new_f != 0)
718 XSETFRAME (window, new_f);
719 else
721 window = selected_window;
722 XSETFASTINT (x, 0);
723 XSETFASTINT (y, 0);
726 else
728 tem = Fcar (position);
729 if (CONSP (tem))
731 window = Fcar (Fcdr (position));
732 x = Fcar (tem);
733 y = Fcar (Fcdr (tem));
735 else
737 for_click = 1;
738 tem = Fcar (Fcdr (position)); /* EVENT_START (position) */
739 window = Fcar (tem); /* POSN_WINDOW (tem) */
740 tem = Fcar (Fcdr (Fcdr (tem))); /* POSN_WINDOW_POSN (tem) */
741 x = Fcar (tem);
742 y = Fcdr (tem);
746 CHECK_NUMBER (x, 0);
747 CHECK_NUMBER (y, 0);
749 /* Decode where to put the menu. */
751 if (FRAMEP (window))
753 f = XFRAME (window);
754 xpos = 0;
755 ypos = 0;
757 else if (WINDOWP (window))
759 CHECK_LIVE_WINDOW (window, 0);
760 f = XFRAME (WINDOW_FRAME (XWINDOW (window)));
762 xpos = (FONT_WIDTH (f->output_data.x->font)
763 * XFASTINT (XWINDOW (window)->left));
764 ypos = (f->output_data.x->line_height
765 * XFASTINT (XWINDOW (window)->top));
767 else
768 /* ??? Not really clean; should be CHECK_WINDOW_OR_FRAME,
769 but I don't want to make one now. */
770 CHECK_WINDOW (window, 0);
772 xpos += XINT (x);
773 ypos += XINT (y);
775 XSETFRAME (Vmenu_updating_frame, f);
777 Vmenu_updating_frame = Qnil;
778 #endif /* HAVE_MENUS */
780 title = Qnil;
781 GCPRO1 (title);
783 /* Decode the menu items from what was specified. */
785 keymap = Fkeymapp (menu);
786 tem = Qnil;
787 if (CONSP (menu))
788 tem = Fkeymapp (Fcar (menu));
789 if (!NILP (keymap))
791 /* We were given a keymap. Extract menu info from the keymap. */
792 Lisp_Object prompt;
793 keymap = get_keymap (menu);
795 /* Extract the detailed info to make one pane. */
796 keymap_panes (&menu, 1, NILP (position));
798 /* Search for a string appearing directly as an element of the keymap.
799 That string is the title of the menu. */
800 prompt = map_prompt (keymap);
801 if (NILP (title) && !NILP (prompt))
802 title = prompt;
804 /* Make that be the pane title of the first pane. */
805 if (!NILP (prompt) && menu_items_n_panes >= 0)
806 XVECTOR (menu_items)->contents[MENU_ITEMS_PANE_NAME] = prompt;
808 keymaps = 1;
810 else if (!NILP (tem))
812 /* We were given a list of keymaps. */
813 int nmaps = XFASTINT (Flength (menu));
814 Lisp_Object *maps
815 = (Lisp_Object *) alloca (nmaps * sizeof (Lisp_Object));
816 int i;
818 title = Qnil;
820 /* The first keymap that has a prompt string
821 supplies the menu title. */
822 for (tem = menu, i = 0; CONSP (tem); tem = Fcdr (tem))
824 Lisp_Object prompt;
826 maps[i++] = keymap = get_keymap (Fcar (tem));
828 prompt = map_prompt (keymap);
829 if (NILP (title) && !NILP (prompt))
830 title = prompt;
833 /* Extract the detailed info to make one pane. */
834 keymap_panes (maps, nmaps, NILP (position));
836 /* Make the title be the pane title of the first pane. */
837 if (!NILP (title) && menu_items_n_panes >= 0)
838 XVECTOR (menu_items)->contents[MENU_ITEMS_PANE_NAME] = title;
840 keymaps = 1;
842 else
844 /* We were given an old-fashioned menu. */
845 title = Fcar (menu);
846 CHECK_STRING (title, 1);
848 list_of_panes (Fcdr (menu));
850 keymaps = 0;
853 if (NILP (position))
855 discard_menu_items ();
856 UNGCPRO;
857 return Qnil;
860 #ifdef HAVE_MENUS
861 /* Display them in a menu. */
862 BLOCK_INPUT;
864 selection = xmenu_show (f, xpos, ypos, for_click,
865 keymaps, title, &error_name);
866 UNBLOCK_INPUT;
868 discard_menu_items ();
870 UNGCPRO;
871 #endif /* HAVE_MENUS */
873 if (error_name) error (error_name);
874 return selection;
877 #ifdef HAVE_MENUS
879 DEFUN ("x-popup-dialog", Fx_popup_dialog, Sx_popup_dialog, 2, 2, 0,
880 "Pop up a dialog box and return user's selection.\n\
881 POSITION specifies which frame to use.\n\
882 This is normally a mouse button event or a window or frame.\n\
883 If POSITION is t, it means to use the frame the mouse is on.\n\
884 The dialog box appears in the middle of the specified frame.\n\
886 CONTENTS specifies the alternatives to display in the dialog box.\n\
887 It is a list of the form (TITLE ITEM1 ITEM2...).\n\
888 Each ITEM is a cons cell (STRING . VALUE).\n\
889 The return value is VALUE from the chosen item.\n\n\
890 An ITEM may also be just a string--that makes a nonselectable item.\n\
891 An ITEM may also be nil--that means to put all preceding items\n\
892 on the left of the dialog box and all following items on the right.\n\
893 \(By default, approximately half appear on each side.)")
894 (position, contents)
895 Lisp_Object position, contents;
897 FRAME_PTR f;
898 Lisp_Object window;
900 check_x ();
902 /* Decode the first argument: find the window or frame to use. */
903 if (EQ (position, Qt)
904 || (CONSP (position) && EQ (XCAR (position), Qmenu_bar)))
906 #if 0 /* Using the frame the mouse is on may not be right. */
907 /* Use the mouse's current position. */
908 FRAME_PTR new_f = SELECTED_FRAME ();
909 Lisp_Object bar_window;
910 int part;
911 unsigned long time;
912 Lisp_Object x, y;
914 (*mouse_position_hook) (&new_f, 1, &bar_window, &part, &x, &y, &time);
916 if (new_f != 0)
917 XSETFRAME (window, new_f);
918 else
919 window = selected_window;
920 #endif
921 window = selected_window;
923 else if (CONSP (position))
925 Lisp_Object tem;
926 tem = Fcar (position);
927 if (CONSP (tem))
928 window = Fcar (Fcdr (position));
929 else
931 tem = Fcar (Fcdr (position)); /* EVENT_START (position) */
932 window = Fcar (tem); /* POSN_WINDOW (tem) */
935 else if (WINDOWP (position) || FRAMEP (position))
936 window = position;
937 else
938 window = Qnil;
940 /* Decode where to put the menu. */
942 if (FRAMEP (window))
943 f = XFRAME (window);
944 else if (WINDOWP (window))
946 CHECK_LIVE_WINDOW (window, 0);
947 f = XFRAME (WINDOW_FRAME (XWINDOW (window)));
949 else
950 /* ??? Not really clean; should be CHECK_WINDOW_OR_FRAME,
951 but I don't want to make one now. */
952 CHECK_WINDOW (window, 0);
954 #ifndef USE_X_TOOLKIT
955 /* Display a menu with these alternatives
956 in the middle of frame F. */
958 Lisp_Object x, y, frame, newpos;
959 XSETFRAME (frame, f);
960 XSETINT (x, x_pixel_width (f) / 2);
961 XSETINT (y, x_pixel_height (f) / 2);
962 newpos = Fcons (Fcons (x, Fcons (y, Qnil)), Fcons (frame, Qnil));
964 return Fx_popup_menu (newpos,
965 Fcons (Fcar (contents), Fcons (contents, Qnil)));
967 #else
969 Lisp_Object title;
970 char *error_name;
971 Lisp_Object selection;
973 /* Decode the dialog items from what was specified. */
974 title = Fcar (contents);
975 CHECK_STRING (title, 1);
977 list_of_panes (Fcons (contents, Qnil));
979 /* Display them in a dialog box. */
980 BLOCK_INPUT;
981 selection = xdialog_show (f, 0, title, &error_name);
982 UNBLOCK_INPUT;
984 discard_menu_items ();
986 if (error_name) error (error_name);
987 return selection;
989 #endif
992 #ifdef USE_X_TOOLKIT
994 /* Loop in Xt until the menu pulldown or dialog popup has been
995 popped down (deactivated). This is used for x-popup-menu
996 and x-popup-dialog; it is not used for the menu bar any more.
998 NOTE: All calls to popup_get_selection should be protected
999 with BLOCK_INPUT, UNBLOCK_INPUT wrappers. */
1001 void
1002 popup_get_selection (initial_event, dpyinfo, id)
1003 XEvent *initial_event;
1004 struct x_display_info *dpyinfo;
1005 LWLIB_ID id;
1007 XEvent event;
1009 /* Define a queue to save up for later unreading
1010 all X events that don't pertain to the menu. */
1011 struct event_queue
1013 XEvent event;
1014 struct event_queue *next;
1017 struct event_queue *queue = NULL;
1018 struct event_queue *queue_tmp;
1020 if (initial_event)
1021 event = *initial_event;
1022 else
1023 XtAppNextEvent (Xt_app_con, &event);
1025 while (1)
1027 /* Handle expose events for editor frames right away. */
1028 if (event.type == Expose)
1029 process_expose_from_menu (event);
1030 /* Make sure we don't consider buttons grabbed after menu goes.
1031 And make sure to deactivate for any ButtonRelease,
1032 even if XtDispatchEvent doesn't do that. */
1033 else if (event.type == ButtonRelease
1034 && dpyinfo->display == event.xbutton.display)
1036 dpyinfo->grabbed &= ~(1 << event.xbutton.button);
1037 popup_activated_flag = 0;
1038 #ifdef USE_MOTIF /* Pretending that the event came from a
1039 Btn1Down seems the only way to convince Motif to
1040 activate its callbacks; setting the XmNmenuPost
1041 isn't working. --marcus@sysc.pdx.edu. */
1042 event.xbutton.button = 1;
1043 #endif
1045 /* If the user presses a key, deactivate the menu.
1046 The user is likely to do that if we get wedged. */
1047 else if (event.type == KeyPress
1048 && dpyinfo->display == event.xbutton.display)
1050 KeySym keysym = XLookupKeysym (&event.xkey, 0);
1051 if (!IsModifierKey (keysym))
1053 popup_activated_flag = 0;
1054 break;
1057 /* Button presses outside the menu also pop it down. */
1058 else if (event.type == ButtonPress
1059 && event.xany.display == dpyinfo->display
1060 && x_any_window_to_frame (dpyinfo, event.xany.window))
1062 popup_activated_flag = 0;
1063 break;
1066 /* Queue all events not for this popup,
1067 except for Expose, which we've already handled, and ButtonRelease.
1068 Note that the X window is associated with the frame if this
1069 is a menu bar popup, but not if it's a dialog box. So we use
1070 x_non_menubar_window_to_frame, not x_any_window_to_frame. */
1071 if (event.type != Expose
1072 && !(event.type == ButtonRelease
1073 && dpyinfo->display == event.xbutton.display)
1074 && (event.xany.display != dpyinfo->display
1075 || x_non_menubar_window_to_frame (dpyinfo, event.xany.window)))
1077 queue_tmp = (struct event_queue *) malloc (sizeof (struct event_queue));
1079 if (queue_tmp != NULL)
1081 queue_tmp->event = event;
1082 queue_tmp->next = queue;
1083 queue = queue_tmp;
1086 else
1087 XtDispatchEvent (&event);
1089 if (!popup_activated ())
1090 break;
1091 XtAppNextEvent (Xt_app_con, &event);
1094 /* Unread any events that we got but did not handle. */
1095 while (queue != NULL)
1097 queue_tmp = queue;
1098 XPutBackEvent (queue_tmp->event.xany.display, &queue_tmp->event);
1099 queue = queue_tmp->next;
1100 free ((char *)queue_tmp);
1101 /* Cause these events to get read as soon as we UNBLOCK_INPUT. */
1102 interrupt_input_pending = 1;
1106 /* Activate the menu bar of frame F.
1107 This is called from keyboard.c when it gets the
1108 menu_bar_activate_event out of the Emacs event queue.
1110 To activate the menu bar, we use the X button-press event
1111 that was saved in saved_menu_event.
1112 That makes the toolkit do its thing.
1114 But first we recompute the menu bar contents (the whole tree).
1116 The reason for saving the button event until here, instead of
1117 passing it to the toolkit right away, is that we can safely
1118 execute Lisp code. */
1120 void
1121 x_activate_menubar (f)
1122 FRAME_PTR f;
1124 if (!f->output_data.x->saved_menu_event->type)
1125 return;
1127 set_frame_menubar (f, 0, 1);
1128 BLOCK_INPUT;
1129 XtDispatchEvent ((XEvent *) f->output_data.x->saved_menu_event);
1130 UNBLOCK_INPUT;
1131 #ifdef USE_MOTIF
1132 if (f->output_data.x->saved_menu_event->type == ButtonRelease)
1133 pending_menu_activation = 1;
1134 #endif
1136 /* Ignore this if we get it a second time. */
1137 f->output_data.x->saved_menu_event->type = 0;
1140 /* Detect if a dialog or menu has been posted. */
1143 popup_activated ()
1145 return popup_activated_flag;
1149 /* This callback is invoked when the user selects a menubar cascade
1150 pushbutton, but before the pulldown menu is posted. */
1152 static void
1153 popup_activate_callback (widget, id, client_data)
1154 Widget widget;
1155 LWLIB_ID id;
1156 XtPointer client_data;
1158 popup_activated_flag = 1;
1161 /* This callback is called from the menu bar pulldown menu
1162 when the user makes a selection.
1163 Figure out what the user chose
1164 and put the appropriate events into the keyboard buffer. */
1166 static void
1167 menubar_selection_callback (widget, id, client_data)
1168 Widget widget;
1169 LWLIB_ID id;
1170 XtPointer client_data;
1172 Lisp_Object prefix, entry;
1173 FRAME_PTR f = menubar_id_to_frame (id);
1174 Lisp_Object vector;
1175 Lisp_Object *subprefix_stack;
1176 int submenu_depth = 0;
1177 int i;
1179 if (!f)
1180 return;
1181 subprefix_stack = (Lisp_Object *) alloca (f->menu_bar_items_used * sizeof (Lisp_Object));
1182 vector = f->menu_bar_vector;
1183 prefix = Qnil;
1184 i = 0;
1185 while (i < f->menu_bar_items_used)
1187 if (EQ (XVECTOR (vector)->contents[i], Qnil))
1189 subprefix_stack[submenu_depth++] = prefix;
1190 prefix = entry;
1191 i++;
1193 else if (EQ (XVECTOR (vector)->contents[i], Qlambda))
1195 prefix = subprefix_stack[--submenu_depth];
1196 i++;
1198 else if (EQ (XVECTOR (vector)->contents[i], Qt))
1200 prefix = XVECTOR (vector)->contents[i + MENU_ITEMS_PANE_PREFIX];
1201 i += MENU_ITEMS_PANE_LENGTH;
1203 else
1205 entry = XVECTOR (vector)->contents[i + MENU_ITEMS_ITEM_VALUE];
1206 /* The EMACS_INT cast avoids a warning. There's no problem
1207 as long as pointers have enough bits to hold small integers. */
1208 if ((int) (EMACS_INT) client_data == i)
1210 int j;
1211 struct input_event buf;
1212 Lisp_Object frame;
1214 XSETFRAME (frame, f);
1215 buf.kind = menu_bar_event;
1216 buf.frame_or_window = Fcons (frame, Fcons (Qmenu_bar, Qnil));
1217 kbd_buffer_store_event (&buf);
1219 for (j = 0; j < submenu_depth; j++)
1220 if (!NILP (subprefix_stack[j]))
1222 buf.kind = menu_bar_event;
1223 buf.frame_or_window = Fcons (frame, subprefix_stack[j]);
1224 kbd_buffer_store_event (&buf);
1227 if (!NILP (prefix))
1229 buf.kind = menu_bar_event;
1230 buf.frame_or_window = Fcons (frame, prefix);
1231 kbd_buffer_store_event (&buf);
1234 buf.kind = menu_bar_event;
1235 buf.frame_or_window = Fcons (frame, entry);
1236 kbd_buffer_store_event (&buf);
1238 return;
1240 i += MENU_ITEMS_ITEM_LENGTH;
1245 /* This callback is invoked when a dialog or menu is finished being
1246 used and has been unposted. */
1248 static void
1249 popup_deactivate_callback (widget, id, client_data)
1250 Widget widget;
1251 LWLIB_ID id;
1252 XtPointer client_data;
1254 popup_activated_flag = 0;
1257 /* Allocate a widget_value, blocking input. */
1259 widget_value *
1260 xmalloc_widget_value ()
1262 widget_value *value;
1264 BLOCK_INPUT;
1265 value = malloc_widget_value ();
1266 UNBLOCK_INPUT;
1268 return value;
1271 /* This recursively calls free_widget_value on the tree of widgets.
1272 It must free all data that was malloc'ed for these widget_values.
1273 In Emacs, many slots are pointers into the data of Lisp_Strings, and
1274 must be left alone. */
1276 void
1277 free_menubar_widget_value_tree (wv)
1278 widget_value *wv;
1280 if (! wv) return;
1282 wv->name = wv->value = wv->key = (char *) 0xDEADBEEF;
1284 if (wv->contents && (wv->contents != (widget_value*)1))
1286 free_menubar_widget_value_tree (wv->contents);
1287 wv->contents = (widget_value *) 0xDEADBEEF;
1289 if (wv->next)
1291 free_menubar_widget_value_tree (wv->next);
1292 wv->next = (widget_value *) 0xDEADBEEF;
1294 BLOCK_INPUT;
1295 free_widget_value (wv);
1296 UNBLOCK_INPUT;
1299 /* Return a tree of widget_value structures for a menu bar item
1300 whose event type is ITEM_KEY (with string ITEM_NAME)
1301 and whose contents come from the list of keymaps MAPS. */
1303 static widget_value *
1304 single_submenu (item_key, item_name, maps)
1305 Lisp_Object item_key, item_name, maps;
1307 widget_value *wv, *prev_wv, *save_wv, *first_wv;
1308 int i;
1309 int submenu_depth = 0;
1310 Lisp_Object length;
1311 int len;
1312 Lisp_Object *mapvec;
1313 widget_value **submenu_stack;
1314 int previous_items = menu_items_used;
1315 int top_level_items = 0;
1317 length = Flength (maps);
1318 len = XINT (length);
1320 /* Convert the list MAPS into a vector MAPVEC. */
1321 mapvec = (Lisp_Object *) alloca (len * sizeof (Lisp_Object));
1322 for (i = 0; i < len; i++)
1324 mapvec[i] = Fcar (maps);
1325 maps = Fcdr (maps);
1328 menu_items_n_panes = 0;
1330 /* Loop over the given keymaps, making a pane for each map.
1331 But don't make a pane that is empty--ignore that map instead. */
1332 for (i = 0; i < len; i++)
1334 if (SYMBOLP (mapvec[i])
1335 || (CONSP (mapvec[i])
1336 && NILP (Fkeymapp (mapvec[i]))))
1338 /* Here we have a command at top level in the menu bar
1339 as opposed to a submenu. */
1340 top_level_items = 1;
1341 push_menu_pane (Qnil, Qnil);
1342 push_menu_item (item_name, Qt, item_key, mapvec[i], Qnil, Qnil, Qnil);
1344 else
1345 single_keymap_panes (mapvec[i], item_name, item_key, 0, 10);
1348 /* Create a tree of widget_value objects
1349 representing the panes and their items. */
1351 submenu_stack
1352 = (widget_value **) alloca (menu_items_used * sizeof (widget_value *));
1353 wv = xmalloc_widget_value ();
1354 wv->name = "menu";
1355 wv->value = 0;
1356 wv->enabled = 1;
1357 wv->button_type = BUTTON_TYPE_NONE;
1358 first_wv = wv;
1359 save_wv = 0;
1360 prev_wv = 0;
1362 /* Loop over all panes and items made during this call
1363 and construct a tree of widget_value objects.
1364 Ignore the panes and items made by previous calls to
1365 single_submenu, even though those are also in menu_items. */
1366 i = previous_items;
1367 while (i < menu_items_used)
1369 if (EQ (XVECTOR (menu_items)->contents[i], Qnil))
1371 submenu_stack[submenu_depth++] = save_wv;
1372 save_wv = prev_wv;
1373 prev_wv = 0;
1374 i++;
1376 else if (EQ (XVECTOR (menu_items)->contents[i], Qlambda))
1378 prev_wv = save_wv;
1379 save_wv = submenu_stack[--submenu_depth];
1380 i++;
1382 else if (EQ (XVECTOR (menu_items)->contents[i], Qt)
1383 && submenu_depth != 0)
1384 i += MENU_ITEMS_PANE_LENGTH;
1385 /* Ignore a nil in the item list.
1386 It's meaningful only for dialog boxes. */
1387 else if (EQ (XVECTOR (menu_items)->contents[i], Qquote))
1388 i += 1;
1389 else if (EQ (XVECTOR (menu_items)->contents[i], Qt))
1391 /* Create a new pane. */
1392 Lisp_Object pane_name, prefix;
1393 char *pane_string;
1394 pane_name = XVECTOR (menu_items)->contents[i + MENU_ITEMS_PANE_NAME];
1395 prefix = XVECTOR (menu_items)->contents[i + MENU_ITEMS_PANE_PREFIX];
1396 #ifndef HAVE_MULTILINGUAL_MENU
1397 if (STRINGP (pane_name) && STRING_MULTIBYTE (pane_name))
1398 pane_name = string_make_unibyte (pane_name);
1399 #endif
1400 pane_string = (NILP (pane_name)
1401 ? "" : (char *) XSTRING (pane_name)->data);
1402 /* If there is just one top-level pane, put all its items directly
1403 under the top-level menu. */
1404 if (menu_items_n_panes == 1)
1405 pane_string = "";
1407 /* If the pane has a meaningful name,
1408 make the pane a top-level menu item
1409 with its items as a submenu beneath it. */
1410 if (strcmp (pane_string, ""))
1412 wv = xmalloc_widget_value ();
1413 if (save_wv)
1414 save_wv->next = wv;
1415 else
1416 first_wv->contents = wv;
1417 wv->name = pane_string;
1418 /* Ignore the @ that means "separate pane".
1419 This is a kludge, but this isn't worth more time. */
1420 if (!NILP (prefix) && wv->name[0] == '@')
1421 wv->name++;
1422 wv->value = 0;
1423 wv->enabled = 1;
1424 wv->button_type = BUTTON_TYPE_NONE;
1426 save_wv = wv;
1427 prev_wv = 0;
1428 i += MENU_ITEMS_PANE_LENGTH;
1430 else
1432 /* Create a new item within current pane. */
1433 Lisp_Object item_name, enable, descrip, def, type, selected;
1434 item_name = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_NAME];
1435 enable = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_ENABLE];
1436 descrip
1437 = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_EQUIV_KEY];
1438 def = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_DEFINITION];
1439 type = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_TYPE];
1440 selected = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_SELECTED];
1442 #ifndef HAVE_MULTILINGUAL_MENU
1443 if (STRING_MULTIBYTE (item_name))
1444 item_name = string_make_unibyte (item_name);
1445 if (STRINGP (descrip) && STRING_MULTIBYTE (descrip))
1446 descrip = string_make_unibyte (descrip);
1447 #endif
1449 wv = xmalloc_widget_value ();
1450 if (prev_wv)
1451 prev_wv->next = wv;
1452 else
1453 save_wv->contents = wv;
1455 wv->name = (char *) XSTRING (item_name)->data;
1456 if (!NILP (descrip))
1457 wv->key = (char *) XSTRING (descrip)->data;
1458 wv->value = 0;
1459 /* The EMACS_INT cast avoids a warning. There's no problem
1460 as long as pointers have enough bits to hold small integers. */
1461 wv->call_data = (!NILP (def) ? (void *) (EMACS_INT) i : 0);
1462 wv->enabled = !NILP (enable);
1464 if (NILP (type))
1465 wv->button_type = BUTTON_TYPE_NONE;
1466 else if (EQ (type, QCradio))
1467 wv->button_type = BUTTON_TYPE_RADIO;
1468 else if (EQ (type, QCtoggle))
1469 wv->button_type = BUTTON_TYPE_TOGGLE;
1470 else
1471 abort ();
1473 wv->selected = !NILP (selected);
1475 prev_wv = wv;
1477 i += MENU_ITEMS_ITEM_LENGTH;
1481 /* If we have just one "menu item"
1482 that was originally a button, return it by itself. */
1483 if (top_level_items && first_wv->contents && first_wv->contents->next == 0)
1485 wv = first_wv->contents;
1486 free_widget_value (first_wv);
1487 return wv;
1490 return first_wv;
1493 extern void EmacsFrameSetCharSize ();
1495 /* Recompute all the widgets of frame F, when the menu bar
1496 has been changed. */
1498 static void
1499 update_frame_menubar (f)
1500 FRAME_PTR f;
1502 struct x_output *x = f->output_data.x;
1503 int columns, rows;
1504 int menubar_changed;
1506 /* We assume the menubar contents has changed if the global flag is set,
1507 or if the current buffer has changed, or if the menubar has never
1508 been updated before.
1510 menubar_changed = (x->menubar_widget
1511 && !XtIsManaged (x->menubar_widget));
1513 if (! (menubar_changed))
1514 return;
1516 BLOCK_INPUT;
1517 /* Save the size of the frame because the pane widget doesn't accept to
1518 resize itself. So force it. */
1519 columns = f->width;
1520 rows = f->height;
1522 /* Do the voodoo which means "I'm changing lots of things, don't try to
1523 refigure sizes until I'm done." */
1524 lw_refigure_widget (x->column_widget, False);
1526 /* the order in which children are managed is the top to
1527 bottom order in which they are displayed in the paned window.
1528 First, remove the text-area widget.
1530 XtUnmanageChild (x->edit_widget);
1532 /* remove the menubar that is there now, and put up the menubar that
1533 should be there.
1535 if (menubar_changed)
1537 XtManageChild (x->menubar_widget);
1538 XtMapWidget (x->menubar_widget);
1539 XtVaSetValues (x->menubar_widget, XtNmappedWhenManaged, 1, 0);
1542 /* Re-manage the text-area widget, and then thrash the sizes. */
1543 XtManageChild (x->edit_widget);
1544 lw_refigure_widget (x->column_widget, True);
1546 /* Force the pane widget to resize itself with the right values. */
1547 EmacsFrameSetCharSize (x->edit_widget, columns, rows);
1548 UNBLOCK_INPUT;
1551 /* Set the contents of the menubar widgets of frame F.
1552 The argument FIRST_TIME is currently ignored;
1553 it is set the first time this is called, from initialize_frame_menubar. */
1555 void
1556 set_frame_menubar (f, first_time, deep_p)
1557 FRAME_PTR f;
1558 int first_time;
1559 int deep_p;
1561 Widget menubar_widget = f->output_data.x->menubar_widget;
1562 Lisp_Object items;
1563 widget_value *wv, *first_wv, *prev_wv = 0;
1564 int i;
1565 LWLIB_ID id;
1567 XSETFRAME (Vmenu_updating_frame, f);
1569 if (f->output_data.x->id == 0)
1570 f->output_data.x->id = next_menubar_widget_id++;
1571 id = f->output_data.x->id;
1573 if (! menubar_widget)
1574 deep_p = 1;
1575 else if (pending_menu_activation && !deep_p)
1576 deep_p = 1;
1577 /* Make the first call for any given frame always go deep. */
1578 else if (!f->output_data.x->saved_menu_event && !deep_p)
1580 deep_p = 1;
1581 f->output_data.x->saved_menu_event = (XEvent*)xmalloc (sizeof (XEvent));
1582 f->output_data.x->saved_menu_event->type = 0;
1585 wv = xmalloc_widget_value ();
1586 wv->name = "menubar";
1587 wv->value = 0;
1588 wv->enabled = 1;
1589 wv->button_type = BUTTON_TYPE_NONE;
1590 first_wv = wv;
1592 if (deep_p)
1594 /* Make a widget-value tree representing the entire menu trees. */
1596 struct buffer *prev = current_buffer;
1597 Lisp_Object buffer;
1598 int specpdl_count = specpdl_ptr - specpdl;
1599 int previous_menu_items_used = f->menu_bar_items_used;
1600 Lisp_Object *previous_items
1601 = (Lisp_Object *) alloca (previous_menu_items_used
1602 * sizeof (Lisp_Object));
1604 /* If we are making a new widget, its contents are empty,
1605 do always reinitialize them. */
1606 if (! menubar_widget)
1607 previous_menu_items_used = 0;
1609 buffer = XWINDOW (FRAME_SELECTED_WINDOW (f))->buffer;
1610 specbind (Qinhibit_quit, Qt);
1611 /* Don't let the debugger step into this code
1612 because it is not reentrant. */
1613 specbind (Qdebug_on_next_call, Qnil);
1615 record_unwind_protect (Fset_match_data, Fmatch_data (Qnil, Qnil));
1616 if (NILP (Voverriding_local_map_menu_flag))
1618 specbind (Qoverriding_terminal_local_map, Qnil);
1619 specbind (Qoverriding_local_map, Qnil);
1622 set_buffer_internal_1 (XBUFFER (buffer));
1624 /* Run the Lucid hook. */
1625 call1 (Vrun_hooks, Qactivate_menubar_hook);
1626 /* If it has changed current-menubar from previous value,
1627 really recompute the menubar from the value. */
1628 if (! NILP (Vlucid_menu_bar_dirty_flag))
1629 call0 (Qrecompute_lucid_menubar);
1630 safe_run_hooks (Qmenu_bar_update_hook);
1631 FRAME_MENU_BAR_ITEMS (f) = menu_bar_items (FRAME_MENU_BAR_ITEMS (f));
1633 items = FRAME_MENU_BAR_ITEMS (f);
1635 inhibit_garbage_collection ();
1637 /* Save the frame's previous menu bar contents data. */
1638 bcopy (XVECTOR (f->menu_bar_vector)->contents, previous_items,
1639 previous_menu_items_used * sizeof (Lisp_Object));
1641 /* Fill in the current menu bar contents. */
1642 menu_items = f->menu_bar_vector;
1643 menu_items_allocated = XVECTOR (menu_items)->size;
1644 init_menu_items ();
1645 for (i = 0; i < XVECTOR (items)->size; i += 4)
1647 Lisp_Object key, string, maps;
1649 key = XVECTOR (items)->contents[i];
1650 string = XVECTOR (items)->contents[i + 1];
1651 maps = XVECTOR (items)->contents[i + 2];
1652 if (NILP (string))
1653 break;
1655 wv = single_submenu (key, string, maps);
1656 if (prev_wv)
1657 prev_wv->next = wv;
1658 else
1659 first_wv->contents = wv;
1660 /* Don't set wv->name here; GC during the loop might relocate it. */
1661 wv->enabled = 1;
1662 wv->button_type = BUTTON_TYPE_NONE;
1663 prev_wv = wv;
1666 finish_menu_items ();
1668 set_buffer_internal_1 (prev);
1669 unbind_to (specpdl_count, Qnil);
1671 /* If there has been no change in the Lisp-level contents
1672 of the menu bar, skip redisplaying it. Just exit. */
1674 for (i = 0; i < previous_menu_items_used; i++)
1675 if (menu_items_used == i
1676 || (!EQ (previous_items[i], XVECTOR (menu_items)->contents[i])))
1677 break;
1678 if (i == menu_items_used && i == previous_menu_items_used && i != 0)
1680 free_menubar_widget_value_tree (first_wv);
1681 menu_items = Qnil;
1683 return;
1686 /* Now GC cannot happen during the lifetime of the widget_value,
1687 so it's safe to store data from a Lisp_String. */
1688 wv = first_wv->contents;
1689 for (i = 0; i < XVECTOR (items)->size; i += 4)
1691 Lisp_Object string;
1692 string = XVECTOR (items)->contents[i + 1];
1693 if (NILP (string))
1694 break;
1695 wv->name = (char *) XSTRING (string)->data;
1696 wv = wv->next;
1699 f->menu_bar_vector = menu_items;
1700 f->menu_bar_items_used = menu_items_used;
1701 menu_items = Qnil;
1703 else
1705 /* Make a widget-value tree containing
1706 just the top level menu bar strings. */
1708 items = FRAME_MENU_BAR_ITEMS (f);
1709 for (i = 0; i < XVECTOR (items)->size; i += 4)
1711 Lisp_Object string;
1713 string = XVECTOR (items)->contents[i + 1];
1714 if (NILP (string))
1715 break;
1717 wv = xmalloc_widget_value ();
1718 wv->name = (char *) XSTRING (string)->data;
1719 wv->value = 0;
1720 wv->enabled = 1;
1721 wv->button_type = BUTTON_TYPE_NONE;
1722 /* This prevents lwlib from assuming this
1723 menu item is really supposed to be empty. */
1724 /* The EMACS_INT cast avoids a warning.
1725 This value just has to be different from small integers. */
1726 wv->call_data = (void *) (EMACS_INT) (-1);
1728 if (prev_wv)
1729 prev_wv->next = wv;
1730 else
1731 first_wv->contents = wv;
1732 prev_wv = wv;
1735 /* Forget what we thought we knew about what is in the
1736 detailed contents of the menu bar menus.
1737 Changing the top level always destroys the contents. */
1738 f->menu_bar_items_used = 0;
1741 /* Create or update the menu bar widget. */
1743 BLOCK_INPUT;
1745 if (menubar_widget)
1747 /* Disable resizing (done for Motif!) */
1748 lw_allow_resizing (f->output_data.x->widget, False);
1750 /* The third arg is DEEP_P, which says to consider the entire
1751 menu trees we supply, rather than just the menu bar item names. */
1752 lw_modify_all_widgets (id, first_wv, deep_p);
1754 /* Re-enable the edit widget to resize. */
1755 lw_allow_resizing (f->output_data.x->widget, True);
1757 else
1759 menubar_widget = lw_create_widget ("menubar", "menubar", id, first_wv,
1760 f->output_data.x->column_widget,
1762 popup_activate_callback,
1763 menubar_selection_callback,
1764 popup_deactivate_callback);
1765 f->output_data.x->menubar_widget = menubar_widget;
1769 int menubar_size
1770 = (f->output_data.x->menubar_widget
1771 ? (f->output_data.x->menubar_widget->core.height
1772 + f->output_data.x->menubar_widget->core.border_width)
1773 : 0);
1775 #if 0 /* Experimentally, we now get the right results
1776 for -geometry -0-0 without this. 24 Aug 96, rms. */
1777 #ifdef USE_LUCID
1778 if (FRAME_EXTERNAL_MENU_BAR (f))
1780 Dimension ibw = 0;
1781 XtVaGetValues (f->output_data.x->column_widget,
1782 XtNinternalBorderWidth, &ibw, NULL);
1783 menubar_size += ibw;
1785 #endif /* USE_LUCID */
1786 #endif /* 0 */
1788 f->output_data.x->menubar_height = menubar_size;
1791 x_set_menu_resources_from_menu_face (f, f->output_data.x->menubar_widget);
1792 free_menubar_widget_value_tree (first_wv);
1793 update_frame_menubar (f);
1795 UNBLOCK_INPUT;
1798 /* Called from Fx_create_frame to create the initial menubar of a frame
1799 before it is mapped, so that the window is mapped with the menubar already
1800 there instead of us tacking it on later and thrashing the window after it
1801 is visible. */
1803 void
1804 initialize_frame_menubar (f)
1805 FRAME_PTR f;
1807 /* This function is called before the first chance to redisplay
1808 the frame. It has to be, so the frame will have the right size. */
1809 FRAME_MENU_BAR_ITEMS (f) = menu_bar_items (FRAME_MENU_BAR_ITEMS (f));
1810 set_frame_menubar (f, 1, 1);
1813 /* Get rid of the menu bar of frame F, and free its storage.
1814 This is used when deleting a frame, and when turning off the menu bar. */
1816 void
1817 free_frame_menubar (f)
1818 FRAME_PTR f;
1820 Widget menubar_widget;
1822 menubar_widget = f->output_data.x->menubar_widget;
1824 f->output_data.x->menubar_height = 0;
1826 if (menubar_widget)
1828 BLOCK_INPUT;
1829 lw_destroy_all_widgets ((LWLIB_ID) f->output_data.x->id);
1830 UNBLOCK_INPUT;
1834 #endif /* USE_X_TOOLKIT */
1836 /* xmenu_show actually displays a menu using the panes and items in menu_items
1837 and returns the value selected from it.
1838 There are two versions of xmenu_show, one for Xt and one for Xlib.
1839 Both assume input is blocked by the caller. */
1841 /* F is the frame the menu is for.
1842 X and Y are the frame-relative specified position,
1843 relative to the inside upper left corner of the frame F.
1844 FOR_CLICK is nonzero if this menu was invoked for a mouse click.
1845 KEYMAPS is 1 if this menu was specified with keymaps;
1846 in that case, we return a list containing the chosen item's value
1847 and perhaps also the pane's prefix.
1848 TITLE is the specified menu title.
1849 ERROR is a place to store an error message string in case of failure.
1850 (We return nil on failure, but the value doesn't actually matter.) */
1852 #ifdef USE_X_TOOLKIT
1854 /* We need a unique id for each widget handled by the Lucid Widget
1855 library.
1857 For the main windows, and popup menus, we use this counter,
1858 which we increment each time after use. This starts from 1<<16.
1860 For menu bars, we use numbers starting at 0, counted in
1861 next_menubar_widget_id. */
1862 LWLIB_ID widget_id_tick;
1864 #ifdef __STDC__
1865 static Lisp_Object *volatile menu_item_selection;
1866 #else
1867 static Lisp_Object *menu_item_selection;
1868 #endif
1870 static void
1871 popup_selection_callback (widget, id, client_data)
1872 Widget widget;
1873 LWLIB_ID id;
1874 XtPointer client_data;
1876 menu_item_selection = (Lisp_Object *) client_data;
1879 static Lisp_Object
1880 xmenu_show (f, x, y, for_click, keymaps, title, error)
1881 FRAME_PTR f;
1882 int x;
1883 int y;
1884 int for_click;
1885 int keymaps;
1886 Lisp_Object title;
1887 char **error;
1889 int i;
1890 LWLIB_ID menu_id;
1891 Widget menu;
1892 Arg av[2];
1893 int ac = 0;
1894 widget_value *wv, *save_wv = 0, *first_wv = 0, *prev_wv = 0;
1895 widget_value **submenu_stack
1896 = (widget_value **) alloca (menu_items_used * sizeof (widget_value *));
1897 Lisp_Object *subprefix_stack
1898 = (Lisp_Object *) alloca (menu_items_used * sizeof (Lisp_Object));
1899 int submenu_depth = 0;
1900 XButtonPressedEvent dummy;
1902 int first_pane;
1904 *error = NULL;
1906 if (menu_items_used <= MENU_ITEMS_PANE_LENGTH)
1908 *error = "Empty menu";
1909 return Qnil;
1912 /* Create a tree of widget_value objects
1913 representing the panes and their items. */
1914 wv = xmalloc_widget_value ();
1915 wv->name = "menu";
1916 wv->value = 0;
1917 wv->enabled = 1;
1918 wv->button_type = BUTTON_TYPE_NONE;
1919 first_wv = wv;
1920 first_pane = 1;
1922 /* Loop over all panes and items, filling in the tree. */
1923 i = 0;
1924 while (i < menu_items_used)
1926 if (EQ (XVECTOR (menu_items)->contents[i], Qnil))
1928 submenu_stack[submenu_depth++] = save_wv;
1929 save_wv = prev_wv;
1930 prev_wv = 0;
1931 first_pane = 1;
1932 i++;
1934 else if (EQ (XVECTOR (menu_items)->contents[i], Qlambda))
1936 prev_wv = save_wv;
1937 save_wv = submenu_stack[--submenu_depth];
1938 first_pane = 0;
1939 i++;
1941 else if (EQ (XVECTOR (menu_items)->contents[i], Qt)
1942 && submenu_depth != 0)
1943 i += MENU_ITEMS_PANE_LENGTH;
1944 /* Ignore a nil in the item list.
1945 It's meaningful only for dialog boxes. */
1946 else if (EQ (XVECTOR (menu_items)->contents[i], Qquote))
1947 i += 1;
1948 else if (EQ (XVECTOR (menu_items)->contents[i], Qt))
1950 /* Create a new pane. */
1951 Lisp_Object pane_name, prefix;
1952 char *pane_string;
1953 pane_name = XVECTOR (menu_items)->contents[i + MENU_ITEMS_PANE_NAME];
1954 prefix = XVECTOR (menu_items)->contents[i + MENU_ITEMS_PANE_PREFIX];
1955 #ifndef HAVE_MULTILINGUAL_MENU
1956 if (!NILP (pane_name) && STRING_MULTIBYTE (pane_name))
1957 pane_name = string_make_unibyte (pane_name);
1958 #endif
1959 pane_string = (NILP (pane_name)
1960 ? "" : (char *) XSTRING (pane_name)->data);
1961 /* If there is just one top-level pane, put all its items directly
1962 under the top-level menu. */
1963 if (menu_items_n_panes == 1)
1964 pane_string = "";
1966 /* If the pane has a meaningful name,
1967 make the pane a top-level menu item
1968 with its items as a submenu beneath it. */
1969 if (!keymaps && strcmp (pane_string, ""))
1971 wv = xmalloc_widget_value ();
1972 if (save_wv)
1973 save_wv->next = wv;
1974 else
1975 first_wv->contents = wv;
1976 wv->name = pane_string;
1977 if (keymaps && !NILP (prefix))
1978 wv->name++;
1979 wv->value = 0;
1980 wv->enabled = 1;
1981 wv->button_type = BUTTON_TYPE_NONE;
1982 save_wv = wv;
1983 prev_wv = 0;
1985 else if (first_pane)
1987 save_wv = wv;
1988 prev_wv = 0;
1990 first_pane = 0;
1991 i += MENU_ITEMS_PANE_LENGTH;
1993 else
1995 /* Create a new item within current pane. */
1996 Lisp_Object item_name, enable, descrip, def, type, selected;
1997 item_name = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_NAME];
1998 enable = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_ENABLE];
1999 descrip
2000 = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_EQUIV_KEY];
2001 def = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_DEFINITION];
2002 type = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_TYPE];
2003 selected = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_SELECTED];
2005 #ifndef HAVE_MULTILINGUAL_MENU
2006 if (STRINGP (item_name) && STRING_MULTIBYTE (item_name))
2007 item_name = string_make_unibyte (item_name);
2008 if (STRINGP (descrip) && STRING_MULTIBYTE (descrip))
2009 item_name = string_make_unibyte (descrip);
2010 #endif
2012 wv = xmalloc_widget_value ();
2013 if (prev_wv)
2014 prev_wv->next = wv;
2015 else
2016 save_wv->contents = wv;
2017 wv->name = (char *) XSTRING (item_name)->data;
2018 if (!NILP (descrip))
2019 wv->key = (char *) XSTRING (descrip)->data;
2020 wv->value = 0;
2021 /* If this item has a null value,
2022 make the call_data null so that it won't display a box
2023 when the mouse is on it. */
2024 wv->call_data
2025 = (!NILP (def) ? (void *) &XVECTOR (menu_items)->contents[i] : 0);
2026 wv->enabled = !NILP (enable);
2028 if (NILP (type))
2029 wv->button_type = BUTTON_TYPE_NONE;
2030 else if (EQ (type, QCtoggle))
2031 wv->button_type = BUTTON_TYPE_TOGGLE;
2032 else if (EQ (type, QCradio))
2033 wv->button_type = BUTTON_TYPE_RADIO;
2034 else
2035 abort ();
2037 wv->selected = !NILP (selected);
2039 prev_wv = wv;
2041 i += MENU_ITEMS_ITEM_LENGTH;
2045 /* Deal with the title, if it is non-nil. */
2046 if (!NILP (title))
2048 widget_value *wv_title = xmalloc_widget_value ();
2049 widget_value *wv_sep1 = xmalloc_widget_value ();
2050 widget_value *wv_sep2 = xmalloc_widget_value ();
2052 wv_sep2->name = "--";
2053 wv_sep2->next = first_wv->contents;
2055 wv_sep1->name = "--";
2056 wv_sep1->next = wv_sep2;
2058 #ifndef HAVE_MULTILINGUAL_MENU
2059 if (STRING_MULTIBYTE (title))
2060 title = string_make_unibyte (title);
2061 #endif
2062 wv_title->name = (char *) XSTRING (title)->data;
2063 wv_title->enabled = True;
2064 wv_title->button_type = BUTTON_TYPE_NONE;
2065 wv_title->next = wv_sep1;
2066 first_wv->contents = wv_title;
2069 /* Actually create the menu. */
2070 menu_id = widget_id_tick++;
2071 menu = lw_create_widget ("popup", first_wv->name, menu_id, first_wv,
2072 f->output_data.x->widget, 1, 0,
2073 popup_selection_callback,
2074 popup_deactivate_callback);
2076 /* Adjust coordinates to relative to the outer (window manager) window. */
2078 Window child;
2079 int win_x = 0, win_y = 0;
2081 /* Find the position of the outside upper-left corner of
2082 the inner window, with respect to the outer window. */
2083 if (f->output_data.x->parent_desc != FRAME_X_DISPLAY_INFO (f)->root_window)
2085 BLOCK_INPUT;
2086 XTranslateCoordinates (FRAME_X_DISPLAY (f),
2088 /* From-window, to-window. */
2089 f->output_data.x->window_desc,
2090 f->output_data.x->parent_desc,
2092 /* From-position, to-position. */
2093 0, 0, &win_x, &win_y,
2095 /* Child of window. */
2096 &child);
2097 UNBLOCK_INPUT;
2098 x += win_x;
2099 y += win_y;
2103 /* Adjust coordinates to be root-window-relative. */
2104 x += f->output_data.x->left_pos;
2105 y += f->output_data.x->top_pos;
2107 dummy.type = ButtonPress;
2108 dummy.serial = 0;
2109 dummy.send_event = 0;
2110 dummy.display = FRAME_X_DISPLAY (f);
2111 dummy.time = CurrentTime;
2112 dummy.root = FRAME_X_DISPLAY_INFO (f)->root_window;
2113 dummy.window = dummy.root;
2114 dummy.subwindow = dummy.root;
2115 dummy.x_root = x;
2116 dummy.y_root = y;
2117 dummy.x = x;
2118 dummy.y = y;
2119 dummy.state = (FRAME_X_DISPLAY_INFO (f)->grabbed >> 1) * Button1Mask;
2120 dummy.button = 0;
2121 for (i = 0; i < 5; i++)
2122 if (FRAME_X_DISPLAY_INFO (f)->grabbed & (1 << i))
2123 dummy.button = i;
2125 /* Don't allow any geometry request from the user. */
2126 XtSetArg (av[ac], XtNgeometry, 0); ac++;
2127 XtSetValues (menu, av, ac);
2129 /* Free the widget_value objects we used to specify the contents. */
2130 free_menubar_widget_value_tree (first_wv);
2132 /* No selection has been chosen yet. */
2133 menu_item_selection = 0;
2135 /* Display the menu. */
2136 lw_popup_menu (menu, &dummy);
2137 x_set_menu_resources_from_menu_face (f, menu);
2138 popup_activated_flag = 1;
2140 /* Process events that apply to the menu. */
2141 popup_get_selection ((XEvent *) 0, FRAME_X_DISPLAY_INFO (f), menu_id);
2143 #ifdef LESSTIF_VERSION
2144 /* Nov 1998: For an unknown reason a button grab remains active
2145 after the popup menu has gone. */
2146 XUngrabButton (XtDisplay (f->output_data.x->widget),
2147 AnyButton, AnyModifier,
2148 XtWindow (f->output_data.x->widget));
2149 XUngrabButton (XtDisplay (f->output_data.x->edit_widget),
2150 AnyButton, AnyModifier,
2151 XtWindow (f->output_data.x->edit_widget));
2152 #endif /* LESSTIF_VERSION */
2154 /* fp turned off the following statement and wrote a comment
2155 that it is unnecessary--that the menu has already disappeared.
2156 Nowadays the menu disappears ok, all right, but
2157 we need to delete the widgets or multiple ones will pile up. */
2158 lw_destroy_all_widgets (menu_id);
2160 /* Find the selected item, and its pane, to return
2161 the proper value. */
2162 if (menu_item_selection != 0)
2164 Lisp_Object prefix, entry;
2166 prefix = Qnil;
2167 i = 0;
2168 while (i < menu_items_used)
2170 if (EQ (XVECTOR (menu_items)->contents[i], Qnil))
2172 subprefix_stack[submenu_depth++] = prefix;
2173 prefix = entry;
2174 i++;
2176 else if (EQ (XVECTOR (menu_items)->contents[i], Qlambda))
2178 prefix = subprefix_stack[--submenu_depth];
2179 i++;
2181 else if (EQ (XVECTOR (menu_items)->contents[i], Qt))
2183 prefix
2184 = XVECTOR (menu_items)->contents[i + MENU_ITEMS_PANE_PREFIX];
2185 i += MENU_ITEMS_PANE_LENGTH;
2187 /* Ignore a nil in the item list.
2188 It's meaningful only for dialog boxes. */
2189 else if (EQ (XVECTOR (menu_items)->contents[i], Qquote))
2190 i += 1;
2191 else
2193 entry
2194 = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_VALUE];
2195 if (menu_item_selection == &XVECTOR (menu_items)->contents[i])
2197 if (keymaps != 0)
2199 int j;
2201 entry = Fcons (entry, Qnil);
2202 if (!NILP (prefix))
2203 entry = Fcons (prefix, entry);
2204 for (j = submenu_depth - 1; j >= 0; j--)
2205 if (!NILP (subprefix_stack[j]))
2206 entry = Fcons (subprefix_stack[j], entry);
2208 return entry;
2210 i += MENU_ITEMS_ITEM_LENGTH;
2215 return Qnil;
2218 static void
2219 dialog_selection_callback (widget, id, client_data)
2220 Widget widget;
2221 LWLIB_ID id;
2222 XtPointer client_data;
2224 /* The EMACS_INT cast avoids a warning. There's no problem
2225 as long as pointers have enough bits to hold small integers. */
2226 if ((int) (EMACS_INT) client_data != -1)
2227 menu_item_selection = (Lisp_Object *) client_data;
2228 BLOCK_INPUT;
2229 lw_destroy_all_widgets (id);
2230 UNBLOCK_INPUT;
2231 popup_activated_flag = 0;
2234 static char * button_names [] = {
2235 "button1", "button2", "button3", "button4", "button5",
2236 "button6", "button7", "button8", "button9", "button10" };
2238 static Lisp_Object
2239 xdialog_show (f, keymaps, title, error)
2240 FRAME_PTR f;
2241 int keymaps;
2242 Lisp_Object title;
2243 char **error;
2245 int i, nb_buttons=0;
2246 LWLIB_ID dialog_id;
2247 Widget menu;
2248 char dialog_name[6];
2250 widget_value *wv, *first_wv = 0, *prev_wv = 0;
2252 /* Number of elements seen so far, before boundary. */
2253 int left_count = 0;
2254 /* 1 means we've seen the boundary between left-hand elts and right-hand. */
2255 int boundary_seen = 0;
2257 *error = NULL;
2259 if (menu_items_n_panes > 1)
2261 *error = "Multiple panes in dialog box";
2262 return Qnil;
2265 /* Create a tree of widget_value objects
2266 representing the text label and buttons. */
2268 Lisp_Object pane_name, prefix;
2269 char *pane_string;
2270 pane_name = XVECTOR (menu_items)->contents[MENU_ITEMS_PANE_NAME];
2271 prefix = XVECTOR (menu_items)->contents[MENU_ITEMS_PANE_PREFIX];
2272 pane_string = (NILP (pane_name)
2273 ? "" : (char *) XSTRING (pane_name)->data);
2274 prev_wv = xmalloc_widget_value ();
2275 prev_wv->value = pane_string;
2276 if (keymaps && !NILP (prefix))
2277 prev_wv->name++;
2278 prev_wv->enabled = 1;
2279 prev_wv->name = "message";
2280 first_wv = prev_wv;
2282 /* Loop over all panes and items, filling in the tree. */
2283 i = MENU_ITEMS_PANE_LENGTH;
2284 while (i < menu_items_used)
2287 /* Create a new item within current pane. */
2288 Lisp_Object item_name, enable, descrip;
2289 item_name = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_NAME];
2290 enable = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_ENABLE];
2291 descrip
2292 = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_EQUIV_KEY];
2294 if (NILP (item_name))
2296 free_menubar_widget_value_tree (first_wv);
2297 *error = "Submenu in dialog items";
2298 return Qnil;
2300 if (EQ (item_name, Qquote))
2302 /* This is the boundary between left-side elts
2303 and right-side elts. Stop incrementing right_count. */
2304 boundary_seen = 1;
2305 i++;
2306 continue;
2308 if (nb_buttons >= 9)
2310 free_menubar_widget_value_tree (first_wv);
2311 *error = "Too many dialog items";
2312 return Qnil;
2315 wv = xmalloc_widget_value ();
2316 prev_wv->next = wv;
2317 wv->name = (char *) button_names[nb_buttons];
2318 if (!NILP (descrip))
2319 wv->key = (char *) XSTRING (descrip)->data;
2320 wv->value = (char *) XSTRING (item_name)->data;
2321 wv->call_data = (void *) &XVECTOR (menu_items)->contents[i];
2322 wv->enabled = !NILP (enable);
2323 prev_wv = wv;
2325 if (! boundary_seen)
2326 left_count++;
2328 nb_buttons++;
2329 i += MENU_ITEMS_ITEM_LENGTH;
2332 /* If the boundary was not specified,
2333 by default put half on the left and half on the right. */
2334 if (! boundary_seen)
2335 left_count = nb_buttons - nb_buttons / 2;
2337 wv = xmalloc_widget_value ();
2338 wv->name = dialog_name;
2340 /* Dialog boxes use a really stupid name encoding
2341 which specifies how many buttons to use
2342 and how many buttons are on the right.
2343 The Q means something also. */
2344 dialog_name[0] = 'Q';
2345 dialog_name[1] = '0' + nb_buttons;
2346 dialog_name[2] = 'B';
2347 dialog_name[3] = 'R';
2348 /* Number of buttons to put on the right. */
2349 dialog_name[4] = '0' + nb_buttons - left_count;
2350 dialog_name[5] = 0;
2351 wv->contents = first_wv;
2352 first_wv = wv;
2355 /* Actually create the dialog. */
2356 dialog_id = widget_id_tick++;
2357 menu = lw_create_widget (first_wv->name, "dialog", dialog_id, first_wv,
2358 f->output_data.x->widget, 1, 0,
2359 dialog_selection_callback, 0);
2360 lw_modify_all_widgets (dialog_id, first_wv->contents, True);
2361 /* Free the widget_value objects we used to specify the contents. */
2362 free_menubar_widget_value_tree (first_wv);
2364 /* No selection has been chosen yet. */
2365 menu_item_selection = 0;
2367 /* Display the menu. */
2368 lw_pop_up_all_widgets (dialog_id);
2369 popup_activated_flag = 1;
2371 /* Process events that apply to the menu. */
2372 popup_get_selection ((XEvent *) 0, FRAME_X_DISPLAY_INFO (f), dialog_id);
2374 lw_destroy_all_widgets (dialog_id);
2376 /* Find the selected item, and its pane, to return
2377 the proper value. */
2378 if (menu_item_selection != 0)
2380 Lisp_Object prefix;
2382 prefix = Qnil;
2383 i = 0;
2384 while (i < menu_items_used)
2386 Lisp_Object entry;
2388 if (EQ (XVECTOR (menu_items)->contents[i], Qt))
2390 prefix
2391 = XVECTOR (menu_items)->contents[i + MENU_ITEMS_PANE_PREFIX];
2392 i += MENU_ITEMS_PANE_LENGTH;
2394 else
2396 entry
2397 = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_VALUE];
2398 if (menu_item_selection == &XVECTOR (menu_items)->contents[i])
2400 if (keymaps != 0)
2402 entry = Fcons (entry, Qnil);
2403 if (!NILP (prefix))
2404 entry = Fcons (prefix, entry);
2406 return entry;
2408 i += MENU_ITEMS_ITEM_LENGTH;
2413 return Qnil;
2415 #else /* not USE_X_TOOLKIT */
2417 static Lisp_Object
2418 xmenu_show (f, x, y, for_click, keymaps, title, error)
2419 FRAME_PTR f;
2420 int x, y;
2421 int for_click;
2422 int keymaps;
2423 Lisp_Object title;
2424 char **error;
2426 Window root;
2427 XMenu *menu;
2428 int pane, selidx, lpane, status;
2429 Lisp_Object entry, pane_prefix;
2430 char *datap;
2431 int ulx, uly, width, height;
2432 int dispwidth, dispheight;
2433 int i, j;
2434 int maxwidth;
2435 int dummy_int;
2436 unsigned int dummy_uint;
2438 *error = 0;
2439 if (menu_items_n_panes == 0)
2440 return Qnil;
2442 if (menu_items_used <= MENU_ITEMS_PANE_LENGTH)
2444 *error = "Empty menu";
2445 return Qnil;
2448 /* Figure out which root window F is on. */
2449 XGetGeometry (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), &root,
2450 &dummy_int, &dummy_int, &dummy_uint, &dummy_uint,
2451 &dummy_uint, &dummy_uint);
2453 /* Make the menu on that window. */
2454 menu = XMenuCreate (FRAME_X_DISPLAY (f), root, "emacs");
2455 if (menu == NULL)
2457 *error = "Can't create menu";
2458 return Qnil;
2461 #ifdef HAVE_X_WINDOWS
2462 /* Adjust coordinates to relative to the outer (window manager) window. */
2464 Window child;
2465 int win_x = 0, win_y = 0;
2467 /* Find the position of the outside upper-left corner of
2468 the inner window, with respect to the outer window. */
2469 if (f->output_data.x->parent_desc != FRAME_X_DISPLAY_INFO (f)->root_window)
2471 BLOCK_INPUT;
2472 XTranslateCoordinates (FRAME_X_DISPLAY (f),
2474 /* From-window, to-window. */
2475 f->output_data.x->window_desc,
2476 f->output_data.x->parent_desc,
2478 /* From-position, to-position. */
2479 0, 0, &win_x, &win_y,
2481 /* Child of window. */
2482 &child);
2483 UNBLOCK_INPUT;
2484 x += win_x;
2485 y += win_y;
2488 #endif /* HAVE_X_WINDOWS */
2490 /* Adjust coordinates to be root-window-relative. */
2491 x += f->output_data.x->left_pos;
2492 y += f->output_data.x->top_pos;
2494 /* Create all the necessary panes and their items. */
2495 i = 0;
2496 while (i < menu_items_used)
2498 if (EQ (XVECTOR (menu_items)->contents[i], Qt))
2500 /* Create a new pane. */
2501 Lisp_Object pane_name, prefix;
2502 char *pane_string;
2504 pane_name = XVECTOR (menu_items)->contents[i + MENU_ITEMS_PANE_NAME];
2505 prefix = XVECTOR (menu_items)->contents[i + MENU_ITEMS_PANE_PREFIX];
2506 pane_string = (NILP (pane_name)
2507 ? "" : (char *) XSTRING (pane_name)->data);
2508 if (keymaps && !NILP (prefix))
2509 pane_string++;
2511 lpane = XMenuAddPane (FRAME_X_DISPLAY (f), menu, pane_string, TRUE);
2512 if (lpane == XM_FAILURE)
2514 XMenuDestroy (FRAME_X_DISPLAY (f), menu);
2515 *error = "Can't create pane";
2516 return Qnil;
2518 i += MENU_ITEMS_PANE_LENGTH;
2520 /* Find the width of the widest item in this pane. */
2521 maxwidth = 0;
2522 j = i;
2523 while (j < menu_items_used)
2525 Lisp_Object item;
2526 item = XVECTOR (menu_items)->contents[j];
2527 if (EQ (item, Qt))
2528 break;
2529 if (NILP (item))
2531 j++;
2532 continue;
2534 width = STRING_BYTES (XSTRING (item));
2535 if (width > maxwidth)
2536 maxwidth = width;
2538 j += MENU_ITEMS_ITEM_LENGTH;
2541 /* Ignore a nil in the item list.
2542 It's meaningful only for dialog boxes. */
2543 else if (EQ (XVECTOR (menu_items)->contents[i], Qquote))
2544 i += 1;
2545 else
2547 /* Create a new item within current pane. */
2548 Lisp_Object item_name, enable, descrip;
2549 unsigned char *item_data;
2551 item_name = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_NAME];
2552 enable = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_ENABLE];
2553 descrip
2554 = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_EQUIV_KEY];
2555 if (!NILP (descrip))
2557 int gap = maxwidth - STRING_BYTES (XSTRING (item_name));
2558 #ifdef C_ALLOCA
2559 Lisp_Object spacer;
2560 spacer = Fmake_string (make_number (gap), make_number (' '));
2561 item_name = concat2 (item_name, spacer);
2562 item_name = concat2 (item_name, descrip);
2563 item_data = XSTRING (item_name)->data;
2564 #else
2565 /* if alloca is fast, use that to make the space,
2566 to reduce gc needs. */
2567 item_data
2568 = (unsigned char *) alloca (maxwidth
2569 + STRING_BYTES (XSTRING (descrip)) + 1);
2570 bcopy (XSTRING (item_name)->data, item_data,
2571 STRING_BYTES (XSTRING (item_name)));
2572 for (j = XSTRING (item_name)->size; j < maxwidth; j++)
2573 item_data[j] = ' ';
2574 bcopy (XSTRING (descrip)->data, item_data + j,
2575 STRING_BYTES (XSTRING (descrip)));
2576 item_data[j + STRING_BYTES (XSTRING (descrip))] = 0;
2577 #endif
2579 else
2580 item_data = XSTRING (item_name)->data;
2582 if (XMenuAddSelection (FRAME_X_DISPLAY (f),
2583 menu, lpane, 0, item_data,
2584 !NILP (enable))
2585 == XM_FAILURE)
2587 XMenuDestroy (FRAME_X_DISPLAY (f), menu);
2588 *error = "Can't add selection to menu";
2589 return Qnil;
2591 i += MENU_ITEMS_ITEM_LENGTH;
2595 /* All set and ready to fly. */
2596 XMenuRecompute (FRAME_X_DISPLAY (f), menu);
2597 dispwidth = DisplayWidth (FRAME_X_DISPLAY (f),
2598 XScreenNumberOfScreen (FRAME_X_SCREEN (f)));
2599 dispheight = DisplayHeight (FRAME_X_DISPLAY (f),
2600 XScreenNumberOfScreen (FRAME_X_SCREEN (f)));
2601 x = min (x, dispwidth);
2602 y = min (y, dispheight);
2603 x = max (x, 1);
2604 y = max (y, 1);
2605 XMenuLocate (FRAME_X_DISPLAY (f), menu, 0, 0, x, y,
2606 &ulx, &uly, &width, &height);
2607 if (ulx+width > dispwidth)
2609 x -= (ulx + width) - dispwidth;
2610 ulx = dispwidth - width;
2612 if (uly+height > dispheight)
2614 y -= (uly + height) - dispheight;
2615 uly = dispheight - height;
2617 if (ulx < 0) x -= ulx;
2618 if (uly < 0) y -= uly;
2620 XMenuSetAEQ (menu, TRUE);
2621 XMenuSetFreeze (menu, TRUE);
2622 pane = selidx = 0;
2624 status = XMenuActivate (FRAME_X_DISPLAY (f), menu, &pane, &selidx,
2625 x, y, ButtonReleaseMask, &datap);
2628 #ifdef HAVE_X_WINDOWS
2629 /* Assume the mouse has moved out of the X window.
2630 If it has actually moved in, we will get an EnterNotify. */
2631 x_mouse_leave (FRAME_X_DISPLAY_INFO (f));
2632 #endif
2634 switch (status)
2636 case XM_SUCCESS:
2637 #ifdef XDEBUG
2638 fprintf (stderr, "pane= %d line = %d\n", panes, selidx);
2639 #endif
2641 /* Find the item number SELIDX in pane number PANE. */
2642 i = 0;
2643 while (i < menu_items_used)
2645 if (EQ (XVECTOR (menu_items)->contents[i], Qt))
2647 if (pane == 0)
2648 pane_prefix
2649 = XVECTOR (menu_items)->contents[i + MENU_ITEMS_PANE_PREFIX];
2650 pane--;
2651 i += MENU_ITEMS_PANE_LENGTH;
2653 else
2655 if (pane == -1)
2657 if (selidx == 0)
2659 entry
2660 = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_VALUE];
2661 if (keymaps != 0)
2663 entry = Fcons (entry, Qnil);
2664 if (!NILP (pane_prefix))
2665 entry = Fcons (pane_prefix, entry);
2667 break;
2669 selidx--;
2671 i += MENU_ITEMS_ITEM_LENGTH;
2674 break;
2676 case XM_FAILURE:
2677 *error = "Can't activate menu";
2678 case XM_IA_SELECT:
2679 case XM_NO_SELECT:
2680 entry = Qnil;
2681 break;
2683 XMenuDestroy (FRAME_X_DISPLAY (f), menu);
2685 #ifdef HAVE_X_WINDOWS
2686 /* State that no mouse buttons are now held.
2687 (The oldXMenu code doesn't track this info for us.)
2688 That is not necessarily true, but the fiction leads to reasonable
2689 results, and it is a pain to ask which are actually held now. */
2690 FRAME_X_DISPLAY_INFO (f)->grabbed = 0;
2691 #endif
2693 return entry;
2696 #endif /* not USE_X_TOOLKIT */
2698 #endif /* HAVE_MENUS */
2700 void
2701 syms_of_xmenu ()
2703 staticpro (&menu_items);
2704 menu_items = Qnil;
2706 Qdebug_on_next_call = intern ("debug-on-next-call");
2707 staticpro (&Qdebug_on_next_call);
2709 DEFVAR_LISP ("menu-updating-frame", &Vmenu_updating_frame,
2710 "Frame for which we are updating a menu.\n\
2711 The enable predicate for a menu command should check this variable.");
2712 Vmenu_updating_frame = Qnil;
2714 #ifdef USE_X_TOOLKIT
2715 widget_id_tick = (1<<16);
2716 next_menubar_widget_id = 1;
2717 #endif
2719 defsubr (&Sx_popup_menu);
2720 #ifdef HAVE_MENUS
2721 defsubr (&Sx_popup_dialog);
2722 #endif