First "working" version:
[emacs.git] / src / xmenu.c
bloba9bda2154d3d41fe0a0b54743cdf86796d367fb3
1 /* X Communication module for terminals which understand the X protocol.
2 Copyright (C) 1986, 88, 93, 94, 96, 1999 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));
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 #define MENU_ITEMS_ITEM_NAME 0
163 #define MENU_ITEMS_ITEM_ENABLE 1
164 #define MENU_ITEMS_ITEM_VALUE 2
165 #define MENU_ITEMS_ITEM_EQUIV_KEY 3
166 #define MENU_ITEMS_ITEM_DEFINITION 4
167 #define MENU_ITEMS_ITEM_TYPE 5
168 #define MENU_ITEMS_ITEM_SELECTED 6
169 #define MENU_ITEMS_ITEM_LENGTH 7
171 static Lisp_Object menu_items;
173 /* Number of slots currently allocated in menu_items. */
174 static int menu_items_allocated;
176 /* This is the index in menu_items of the first empty slot. */
177 static int menu_items_used;
179 /* The number of panes currently recorded in menu_items,
180 excluding those within submenus. */
181 static int menu_items_n_panes;
183 /* Current depth within submenus. */
184 static int menu_items_submenu_depth;
186 /* Flag which when set indicates a dialog or menu has been posted by
187 Xt on behalf of one of the widget sets. */
188 static int popup_activated_flag;
190 static int next_menubar_widget_id;
192 /* This is set nonzero after the user activates the menu bar, and set
193 to zero again after the menu bars are redisplayed by prepare_menu_bar.
194 While it is nonzero, all calls to set_frame_menubar go deep.
196 I don't understand why this is needed, but it does seem to be
197 needed on Motif, according to Marcus Daniels <marcus@sysc.pdx.edu>. */
199 int pending_menu_activation;
201 #ifdef USE_X_TOOLKIT
203 /* Return the frame whose ->output_data.x->id equals ID, or 0 if none. */
205 static struct frame *
206 menubar_id_to_frame (id)
207 LWLIB_ID id;
209 Lisp_Object tail, frame;
210 FRAME_PTR f;
212 for (tail = Vframe_list; GC_CONSP (tail); tail = XCDR (tail))
214 frame = XCAR (tail);
215 if (!GC_FRAMEP (frame))
216 continue;
217 f = XFRAME (frame);
218 if (f->output_data.nothing == 1)
219 continue;
220 if (f->output_data.x->id == id)
221 return f;
223 return 0;
226 #endif
228 /* Initialize the menu_items structure if we haven't already done so.
229 Also mark it as currently empty. */
231 static void
232 init_menu_items ()
234 if (NILP (menu_items))
236 menu_items_allocated = 60;
237 menu_items = Fmake_vector (make_number (menu_items_allocated), Qnil);
240 menu_items_used = 0;
241 menu_items_n_panes = 0;
242 menu_items_submenu_depth = 0;
245 /* Call at the end of generating the data in menu_items.
246 This fills in the number of items in the last pane. */
248 static void
249 finish_menu_items ()
253 /* Call when finished using the data for the current menu
254 in menu_items. */
256 static void
257 discard_menu_items ()
259 /* Free the structure if it is especially large.
260 Otherwise, hold on to it, to save time. */
261 if (menu_items_allocated > 200)
263 menu_items = Qnil;
264 menu_items_allocated = 0;
268 /* Make the menu_items vector twice as large. */
270 static void
271 grow_menu_items ()
273 Lisp_Object old;
274 int old_size = menu_items_allocated;
275 old = menu_items;
277 menu_items_allocated *= 2;
278 menu_items = Fmake_vector (make_number (menu_items_allocated), Qnil);
279 bcopy (XVECTOR (old)->contents, XVECTOR (menu_items)->contents,
280 old_size * sizeof (Lisp_Object));
283 /* Begin a submenu. */
285 static void
286 push_submenu_start ()
288 if (menu_items_used + 1 > menu_items_allocated)
289 grow_menu_items ();
291 XVECTOR (menu_items)->contents[menu_items_used++] = Qnil;
292 menu_items_submenu_depth++;
295 /* End a submenu. */
297 static void
298 push_submenu_end ()
300 if (menu_items_used + 1 > menu_items_allocated)
301 grow_menu_items ();
303 XVECTOR (menu_items)->contents[menu_items_used++] = Qlambda;
304 menu_items_submenu_depth--;
307 /* Indicate boundary between left and right. */
309 static void
310 push_left_right_boundary ()
312 if (menu_items_used + 1 > menu_items_allocated)
313 grow_menu_items ();
315 XVECTOR (menu_items)->contents[menu_items_used++] = Qquote;
318 /* Start a new menu pane in menu_items..
319 NAME is the pane name. PREFIX_VEC is a prefix key for this pane. */
321 static void
322 push_menu_pane (name, prefix_vec)
323 Lisp_Object name, prefix_vec;
325 if (menu_items_used + MENU_ITEMS_PANE_LENGTH > menu_items_allocated)
326 grow_menu_items ();
328 if (menu_items_submenu_depth == 0)
329 menu_items_n_panes++;
330 XVECTOR (menu_items)->contents[menu_items_used++] = Qt;
331 XVECTOR (menu_items)->contents[menu_items_used++] = name;
332 XVECTOR (menu_items)->contents[menu_items_used++] = prefix_vec;
335 /* Push one menu item into the current pane. NAME is the string to
336 display. ENABLE if non-nil means this item can be selected. KEY
337 is the key generated by choosing this item, or nil if this item
338 doesn't really have a definition. DEF is the definition of this
339 item. EQUIV is the textual description of the keyboard equivalent
340 for this item (or nil if none). TYPE is the type of this menu
341 item, one of nil, `toggle' or `radio'. */
343 static void
344 push_menu_item (name, enable, key, def, equiv, type, selected)
345 Lisp_Object name, enable, key, def, equiv, type, selected;
347 if (menu_items_used + MENU_ITEMS_ITEM_LENGTH > menu_items_allocated)
348 grow_menu_items ();
350 XVECTOR (menu_items)->contents[menu_items_used++] = name;
351 XVECTOR (menu_items)->contents[menu_items_used++] = enable;
352 XVECTOR (menu_items)->contents[menu_items_used++] = key;
353 XVECTOR (menu_items)->contents[menu_items_used++] = equiv;
354 XVECTOR (menu_items)->contents[menu_items_used++] = def;
355 XVECTOR (menu_items)->contents[menu_items_used++] = type;
356 XVECTOR (menu_items)->contents[menu_items_used++] = selected;
359 /* Look through KEYMAPS, a vector of keymaps that is NMAPS long,
360 and generate menu panes for them in menu_items.
361 If NOTREAL is nonzero,
362 don't bother really computing whether an item is enabled. */
364 static void
365 keymap_panes (keymaps, nmaps, notreal)
366 Lisp_Object *keymaps;
367 int nmaps;
368 int notreal;
370 int mapno;
372 init_menu_items ();
374 /* Loop over the given keymaps, making a pane for each map.
375 But don't make a pane that is empty--ignore that map instead.
376 P is the number of panes we have made so far. */
377 for (mapno = 0; mapno < nmaps; mapno++)
378 single_keymap_panes (keymaps[mapno], Qnil, Qnil, notreal, 10);
380 finish_menu_items ();
383 /* This is a recursive subroutine of keymap_panes.
384 It handles one keymap, KEYMAP.
385 The other arguments are passed along
386 or point to local variables of the previous function.
387 If NOTREAL is nonzero, only check for equivalent key bindings, don't
388 evaluate expressions in menu items and don't make any menu.
390 If we encounter submenus deeper than MAXDEPTH levels, ignore them. */
392 static void
393 single_keymap_panes (keymap, pane_name, prefix, notreal, maxdepth)
394 Lisp_Object keymap;
395 Lisp_Object pane_name;
396 Lisp_Object prefix;
397 int notreal;
398 int maxdepth;
400 Lisp_Object pending_maps = Qnil;
401 Lisp_Object tail, item;
402 struct gcpro gcpro1, gcpro2;
403 int notbuttons = 0;
405 if (maxdepth <= 0)
406 return;
408 push_menu_pane (pane_name, prefix);
410 #ifndef HAVE_BOXES
411 /* Remember index for first item in this pane so we can go back and
412 add a prefix when (if) we see the first button. After that, notbuttons
413 is set to 0, to mark that we have seen a button and all non button
414 items need a prefix. */
415 notbuttons = menu_items_used;
416 #endif
418 for (tail = keymap; CONSP (tail); tail = XCDR (tail))
420 GCPRO2 (keymap, pending_maps);
421 /* Look at each key binding, and if it is a menu item add it
422 to this menu. */
423 item = XCAR (tail);
424 if (CONSP (item))
425 single_menu_item (XCAR (item), XCDR (item),
426 &pending_maps, notreal, maxdepth, &notbuttons);
427 else if (VECTORP (item))
429 /* Loop over the char values represented in the vector. */
430 int len = XVECTOR (item)->size;
431 int c;
432 for (c = 0; c < len; c++)
434 Lisp_Object character;
435 XSETFASTINT (character, c);
436 single_menu_item (character, XVECTOR (item)->contents[c],
437 &pending_maps, notreal, maxdepth, &notbuttons);
440 UNGCPRO;
443 /* Process now any submenus which want to be panes at this level. */
444 while (!NILP (pending_maps))
446 Lisp_Object elt, eltcdr, string;
447 elt = Fcar (pending_maps);
448 eltcdr = XCDR (elt);
449 string = XCAR (eltcdr);
450 /* We no longer discard the @ from the beginning of the string here.
451 Instead, we do this in xmenu_show. */
452 single_keymap_panes (Fcar (elt), string,
453 XCDR (eltcdr), notreal, maxdepth - 1);
454 pending_maps = Fcdr (pending_maps);
458 /* This is a subroutine of single_keymap_panes that handles one
459 keymap entry.
460 KEY is a key in a keymap and ITEM is its binding.
461 PENDING_MAPS_PTR points to a list of keymaps waiting to be made into
462 separate panes.
463 If NOTREAL is nonzero, only check for equivalent key bindings, don't
464 evaluate expressions in menu items and don't make any menu.
465 If we encounter submenus deeper than MAXDEPTH levels, ignore them.
466 NOTBUTTONS_PTR is only used when simulating toggle boxes and radio
467 buttons. It points to variable notbuttons in single_keymap_panes,
468 which keeps track of if we have seen a button in this menu or not. */
470 static void
471 single_menu_item (key, item, pending_maps_ptr, notreal, maxdepth,
472 notbuttons_ptr)
473 Lisp_Object key, item;
474 Lisp_Object *pending_maps_ptr;
475 int maxdepth, notreal;
476 int *notbuttons_ptr;
478 Lisp_Object map, item_string, enabled;
479 struct gcpro gcpro1, gcpro2;
480 int res;
482 /* Parse the menu item and leave the result in item_properties. */
483 GCPRO2 (key, item);
484 res = parse_menu_item (item, notreal, 0);
485 UNGCPRO;
486 if (!res)
487 return; /* Not a menu item. */
489 map = XVECTOR (item_properties)->contents[ITEM_PROPERTY_MAP];
491 if (notreal)
493 /* We don't want to make a menu, just traverse the keymaps to
494 precompute equivalent key bindings. */
495 if (!NILP (map))
496 single_keymap_panes (map, Qnil, key, 1, maxdepth - 1);
497 return;
500 enabled = XVECTOR (item_properties)->contents[ITEM_PROPERTY_ENABLE];
501 item_string = XVECTOR (item_properties)->contents[ITEM_PROPERTY_NAME];
503 if (!NILP (map) && XSTRING (item_string)->data[0] == '@')
505 if (!NILP (enabled))
506 /* An enabled separate pane. Remember this to handle it later. */
507 *pending_maps_ptr = Fcons (Fcons (map, Fcons (item_string, key)),
508 *pending_maps_ptr);
509 return;
512 #ifndef HAVE_BOXES
513 /* Simulate radio buttons and toggle boxes by putting a prefix in
514 front of them. */
516 Lisp_Object prefix = Qnil;
517 Lisp_Object type = XVECTOR (item_properties)->contents[ITEM_PROPERTY_TYPE];
518 if (!NILP (type))
520 Lisp_Object selected
521 = XVECTOR (item_properties)->contents[ITEM_PROPERTY_SELECTED];
523 if (*notbuttons_ptr)
524 /* The first button. Line up previous items in this menu. */
526 int index = *notbuttons_ptr; /* Index for first item this menu. */
527 int submenu = 0;
528 Lisp_Object tem;
529 while (index < menu_items_used)
532 = XVECTOR (menu_items)->contents[index + MENU_ITEMS_ITEM_NAME];
533 if (NILP (tem))
535 index++;
536 submenu++; /* Skip sub menu. */
538 else if (EQ (tem, Qlambda))
540 index++;
541 submenu--; /* End sub menu. */
543 else if (EQ (tem, Qt))
544 index += 3; /* Skip new pane marker. */
545 else if (EQ (tem, Qquote))
546 index++; /* Skip a left, right divider. */
547 else
549 if (!submenu && XSTRING (tem)->data[0] != '\0'
550 && XSTRING (tem)->data[0] != '-')
551 XVECTOR (menu_items)->contents[index + MENU_ITEMS_ITEM_NAME]
552 = concat2 (build_string (" "), tem);
553 index += MENU_ITEMS_ITEM_LENGTH;
556 *notbuttons_ptr = 0;
559 /* Calculate prefix, if any, for this item. */
560 if (EQ (type, QCtoggle))
561 prefix = build_string (NILP (selected) ? "[ ] " : "[X] ");
562 else if (EQ (type, QCradio))
563 prefix = build_string (NILP (selected) ? "( ) " : "(*) ");
565 /* Not a button. If we have earlier buttons, then we need a prefix. */
566 else if (!*notbuttons_ptr && XSTRING (item_string)->data[0] != '\0'
567 && XSTRING (item_string)->data[0] != '-')
568 prefix = build_string (" ");
570 if (!NILP (prefix))
571 item_string = concat2 (prefix, item_string);
573 #endif /* not HAVE_BOXES */
575 #ifndef USE_X_TOOLKIT
576 if (!NILP(map))
577 /* Indicate visually that this is a submenu. */
578 item_string = concat2 (item_string, build_string (" >"));
579 #endif
581 push_menu_item (item_string, enabled, key,
582 XVECTOR (item_properties)->contents[ITEM_PROPERTY_DEF],
583 XVECTOR (item_properties)->contents[ITEM_PROPERTY_KEYEQ],
584 XVECTOR (item_properties)->contents[ITEM_PROPERTY_TYPE],
585 XVECTOR (item_properties)->contents[ITEM_PROPERTY_SELECTED]);
587 #ifdef USE_X_TOOLKIT
588 /* Display a submenu using the toolkit. */
589 if (! (NILP (map) || NILP (enabled)))
591 push_submenu_start ();
592 single_keymap_panes (map, Qnil, key, 0, maxdepth - 1);
593 push_submenu_end ();
595 #endif
598 /* Push all the panes and items of a menu described by the
599 alist-of-alists MENU.
600 This handles old-fashioned calls to x-popup-menu. */
602 static void
603 list_of_panes (menu)
604 Lisp_Object menu;
606 Lisp_Object tail;
608 init_menu_items ();
610 for (tail = menu; !NILP (tail); tail = Fcdr (tail))
612 Lisp_Object elt, pane_name, pane_data;
613 elt = Fcar (tail);
614 pane_name = Fcar (elt);
615 CHECK_STRING (pane_name, 0);
616 push_menu_pane (pane_name, Qnil);
617 pane_data = Fcdr (elt);
618 CHECK_CONS (pane_data, 0);
619 list_of_items (pane_data);
622 finish_menu_items ();
625 /* Push the items in a single pane defined by the alist PANE. */
627 static void
628 list_of_items (pane)
629 Lisp_Object pane;
631 Lisp_Object tail, item, item1;
633 for (tail = pane; !NILP (tail); tail = Fcdr (tail))
635 item = Fcar (tail);
636 if (STRINGP (item))
637 push_menu_item (item, Qnil, Qnil, Qt, Qnil, Qnil, Qnil);
638 else if (NILP (item))
639 push_left_right_boundary ();
640 else
642 CHECK_CONS (item, 0);
643 item1 = Fcar (item);
644 CHECK_STRING (item1, 1);
645 push_menu_item (item1, Qt, Fcdr (item), Qt, Qnil, Qnil, Qnil);
650 DEFUN ("x-popup-menu", Fx_popup_menu, Sx_popup_menu, 2, 2, 0,
651 "Pop up a deck-of-cards menu and return user's selection.\n\
652 POSITION is a position specification. This is either a mouse button event\n\
653 or a list ((XOFFSET YOFFSET) WINDOW)\n\
654 where XOFFSET and YOFFSET are positions in pixels from the top left\n\
655 corner of WINDOW's frame. (WINDOW may be a frame object instead of a window.)\n\
656 This controls the position of the center of the first line\n\
657 in the first pane of the menu, not the top left of the menu as a whole.\n\
658 If POSITION is t, it means to use the current mouse position.\n\
660 MENU is a specifier for a menu. For the simplest case, MENU is a keymap.\n\
661 The menu items come from key bindings that have a menu string as well as\n\
662 a definition; actually, the \"definition\" in such a key binding looks like\n\
663 \(STRING . REAL-DEFINITION). To give the menu a title, put a string into\n\
664 the keymap as a top-level element.\n\n\
665 If REAL-DEFINITION is nil, that puts a nonselectable string in the menu.\n\
666 Otherwise, REAL-DEFINITION should be a valid key binding definition.\n\
668 You can also use a list of keymaps as MENU.\n\
669 Then each keymap makes a separate pane.\n\
670 When MENU is a keymap or a list of keymaps, the return value\n\
671 is a list of events.\n\n\
673 Alternatively, you can specify a menu of multiple panes\n\
674 with a list of the form (TITLE PANE1 PANE2...),\n\
675 where each pane is a list of form (TITLE ITEM1 ITEM2...).\n\
676 Each ITEM is normally a cons cell (STRING . VALUE);\n\
677 but a string can appear as an item--that makes a nonselectable line\n\
678 in the menu.\n\
679 With this form of menu, the return value is VALUE from the chosen item.\n\
681 If POSITION is nil, don't display the menu at all, just precalculate the\n\
682 cached information about equivalent key sequences.")
683 (position, menu)
684 Lisp_Object position, menu;
686 Lisp_Object keymap, tem;
687 int xpos, ypos;
688 Lisp_Object title;
689 char *error_name;
690 Lisp_Object selection;
691 FRAME_PTR f;
692 Lisp_Object x, y, window;
693 int keymaps = 0;
694 int for_click = 0;
695 struct gcpro gcpro1;
697 #ifdef HAVE_MENUS
698 if (! NILP (position))
700 check_x ();
702 /* Decode the first argument: find the window and the coordinates. */
703 if (EQ (position, Qt)
704 || (CONSP (position) && EQ (XCAR (position), Qmenu_bar)))
706 /* Use the mouse's current position. */
707 FRAME_PTR new_f = SELECTED_FRAME ();
708 Lisp_Object bar_window;
709 enum scroll_bar_part part;
710 unsigned long time;
712 if (mouse_position_hook)
713 (*mouse_position_hook) (&new_f, 1, &bar_window,
714 &part, &x, &y, &time);
715 if (new_f != 0)
716 XSETFRAME (window, new_f);
717 else
719 window = selected_window;
720 XSETFASTINT (x, 0);
721 XSETFASTINT (y, 0);
724 else
726 tem = Fcar (position);
727 if (CONSP (tem))
729 window = Fcar (Fcdr (position));
730 x = Fcar (tem);
731 y = Fcar (Fcdr (tem));
733 else
735 for_click = 1;
736 tem = Fcar (Fcdr (position)); /* EVENT_START (position) */
737 window = Fcar (tem); /* POSN_WINDOW (tem) */
738 tem = Fcar (Fcdr (Fcdr (tem))); /* POSN_WINDOW_POSN (tem) */
739 x = Fcar (tem);
740 y = Fcdr (tem);
744 CHECK_NUMBER (x, 0);
745 CHECK_NUMBER (y, 0);
747 /* Decode where to put the menu. */
749 if (FRAMEP (window))
751 f = XFRAME (window);
752 xpos = 0;
753 ypos = 0;
755 else if (WINDOWP (window))
757 CHECK_LIVE_WINDOW (window, 0);
758 f = XFRAME (WINDOW_FRAME (XWINDOW (window)));
760 xpos = (FONT_WIDTH (f->output_data.x->font)
761 * XFASTINT (XWINDOW (window)->left));
762 ypos = (f->output_data.x->line_height
763 * XFASTINT (XWINDOW (window)->top));
765 else
766 /* ??? Not really clean; should be CHECK_WINDOW_OR_FRAME,
767 but I don't want to make one now. */
768 CHECK_WINDOW (window, 0);
770 xpos += XINT (x);
771 ypos += XINT (y);
773 XSETFRAME (Vmenu_updating_frame, f);
775 Vmenu_updating_frame = Qnil;
776 #endif /* HAVE_MENUS */
778 title = Qnil;
779 GCPRO1 (title);
781 /* Decode the menu items from what was specified. */
783 keymap = Fkeymapp (menu);
784 tem = Qnil;
785 if (CONSP (menu))
786 tem = Fkeymapp (Fcar (menu));
787 if (!NILP (keymap))
789 /* We were given a keymap. Extract menu info from the keymap. */
790 Lisp_Object prompt;
791 keymap = get_keymap (menu);
793 /* Extract the detailed info to make one pane. */
794 keymap_panes (&menu, 1, NILP (position));
796 /* Search for a string appearing directly as an element of the keymap.
797 That string is the title of the menu. */
798 prompt = map_prompt (keymap);
799 if (NILP (title) && !NILP (prompt))
800 title = prompt;
802 /* Make that be the pane title of the first pane. */
803 if (!NILP (prompt) && menu_items_n_panes >= 0)
804 XVECTOR (menu_items)->contents[MENU_ITEMS_PANE_NAME] = prompt;
806 keymaps = 1;
808 else if (!NILP (tem))
810 /* We were given a list of keymaps. */
811 int nmaps = XFASTINT (Flength (menu));
812 Lisp_Object *maps
813 = (Lisp_Object *) alloca (nmaps * sizeof (Lisp_Object));
814 int i;
816 title = Qnil;
818 /* The first keymap that has a prompt string
819 supplies the menu title. */
820 for (tem = menu, i = 0; CONSP (tem); tem = Fcdr (tem))
822 Lisp_Object prompt;
824 maps[i++] = keymap = get_keymap (Fcar (tem));
826 prompt = map_prompt (keymap);
827 if (NILP (title) && !NILP (prompt))
828 title = prompt;
831 /* Extract the detailed info to make one pane. */
832 keymap_panes (maps, nmaps, NILP (position));
834 /* Make the title be the pane title of the first pane. */
835 if (!NILP (title) && menu_items_n_panes >= 0)
836 XVECTOR (menu_items)->contents[MENU_ITEMS_PANE_NAME] = title;
838 keymaps = 1;
840 else
842 /* We were given an old-fashioned menu. */
843 title = Fcar (menu);
844 CHECK_STRING (title, 1);
846 list_of_panes (Fcdr (menu));
848 keymaps = 0;
851 if (NILP (position))
853 discard_menu_items ();
854 UNGCPRO;
855 return Qnil;
858 #ifdef HAVE_MENUS
859 /* Display them in a menu. */
860 BLOCK_INPUT;
862 selection = xmenu_show (f, xpos, ypos, for_click,
863 keymaps, title, &error_name);
864 UNBLOCK_INPUT;
866 discard_menu_items ();
868 UNGCPRO;
869 #endif /* HAVE_MENUS */
871 if (error_name) error (error_name);
872 return selection;
875 #ifdef HAVE_MENUS
877 DEFUN ("x-popup-dialog", Fx_popup_dialog, Sx_popup_dialog, 2, 2, 0,
878 "Pop up a dialog box and return user's selection.\n\
879 POSITION specifies which frame to use.\n\
880 This is normally a mouse button event or a window or frame.\n\
881 If POSITION is t, it means to use the frame the mouse is on.\n\
882 The dialog box appears in the middle of the specified frame.\n\
884 CONTENTS specifies the alternatives to display in the dialog box.\n\
885 It is a list of the form (TITLE ITEM1 ITEM2...).\n\
886 Each ITEM is a cons cell (STRING . VALUE).\n\
887 The return value is VALUE from the chosen item.\n\n\
888 An ITEM may also be just a string--that makes a nonselectable item.\n\
889 An ITEM may also be nil--that means to put all preceding items\n\
890 on the left of the dialog box and all following items on the right.\n\
891 \(By default, approximately half appear on each side.)")
892 (position, contents)
893 Lisp_Object position, contents;
895 FRAME_PTR f;
896 Lisp_Object window;
898 check_x ();
900 /* Decode the first argument: find the window or frame to use. */
901 if (EQ (position, Qt)
902 || (CONSP (position) && EQ (XCAR (position), Qmenu_bar)))
904 #if 0 /* Using the frame the mouse is on may not be right. */
905 /* Use the mouse's current position. */
906 FRAME_PTR new_f = SELECTED_FRAME ();
907 Lisp_Object bar_window;
908 int part;
909 unsigned long time;
910 Lisp_Object x, y;
912 (*mouse_position_hook) (&new_f, 1, &bar_window, &part, &x, &y, &time);
914 if (new_f != 0)
915 XSETFRAME (window, new_f);
916 else
917 window = selected_window;
918 #endif
919 window = selected_window;
921 else if (CONSP (position))
923 Lisp_Object tem;
924 tem = Fcar (position);
925 if (CONSP (tem))
926 window = Fcar (Fcdr (position));
927 else
929 tem = Fcar (Fcdr (position)); /* EVENT_START (position) */
930 window = Fcar (tem); /* POSN_WINDOW (tem) */
933 else if (WINDOWP (position) || FRAMEP (position))
934 window = position;
935 else
936 window = Qnil;
938 /* Decode where to put the menu. */
940 if (FRAMEP (window))
941 f = XFRAME (window);
942 else if (WINDOWP (window))
944 CHECK_LIVE_WINDOW (window, 0);
945 f = XFRAME (WINDOW_FRAME (XWINDOW (window)));
947 else
948 /* ??? Not really clean; should be CHECK_WINDOW_OR_FRAME,
949 but I don't want to make one now. */
950 CHECK_WINDOW (window, 0);
952 #ifndef USE_X_TOOLKIT
953 /* Display a menu with these alternatives
954 in the middle of frame F. */
956 Lisp_Object x, y, frame, newpos;
957 XSETFRAME (frame, f);
958 XSETINT (x, x_pixel_width (f) / 2);
959 XSETINT (y, x_pixel_height (f) / 2);
960 newpos = Fcons (Fcons (x, Fcons (y, Qnil)), Fcons (frame, Qnil));
962 return Fx_popup_menu (newpos,
963 Fcons (Fcar (contents), Fcons (contents, Qnil)));
965 #else
967 Lisp_Object title;
968 char *error_name;
969 Lisp_Object selection;
971 /* Decode the dialog items from what was specified. */
972 title = Fcar (contents);
973 CHECK_STRING (title, 1);
975 list_of_panes (Fcons (contents, Qnil));
977 /* Display them in a dialog box. */
978 BLOCK_INPUT;
979 selection = xdialog_show (f, 0, title, &error_name);
980 UNBLOCK_INPUT;
982 discard_menu_items ();
984 if (error_name) error (error_name);
985 return selection;
987 #endif
990 #ifdef USE_X_TOOLKIT
992 /* Loop in Xt until the menu pulldown or dialog popup has been
993 popped down (deactivated). This is used for x-popup-menu
994 and x-popup-dialog; it is not used for the menu bar any more.
996 NOTE: All calls to popup_get_selection should be protected
997 with BLOCK_INPUT, UNBLOCK_INPUT wrappers. */
999 void
1000 popup_get_selection (initial_event, dpyinfo, id)
1001 XEvent *initial_event;
1002 struct x_display_info *dpyinfo;
1003 LWLIB_ID id;
1005 XEvent event;
1007 /* Define a queue to save up for later unreading
1008 all X events that don't pertain to the menu. */
1009 struct event_queue
1011 XEvent event;
1012 struct event_queue *next;
1015 struct event_queue *queue = NULL;
1016 struct event_queue *queue_tmp;
1018 if (initial_event)
1019 event = *initial_event;
1020 else
1021 XtAppNextEvent (Xt_app_con, &event);
1023 while (1)
1025 /* Handle expose events for editor frames right away. */
1026 if (event.type == Expose)
1027 process_expose_from_menu (event);
1028 /* Make sure we don't consider buttons grabbed after menu goes.
1029 And make sure to deactivate for any ButtonRelease,
1030 even if XtDispatchEvent doesn't do that. */
1031 else if (event.type == ButtonRelease
1032 && dpyinfo->display == event.xbutton.display)
1034 dpyinfo->grabbed &= ~(1 << event.xbutton.button);
1035 popup_activated_flag = 0;
1036 #ifdef USE_MOTIF /* Pretending that the event came from a
1037 Btn1Down seems the only way to convince Motif to
1038 activate its callbacks; setting the XmNmenuPost
1039 isn't working. --marcus@sysc.pdx.edu. */
1040 event.xbutton.button = 1;
1041 #endif
1043 /* If the user presses a key, deactivate the menu.
1044 The user is likely to do that if we get wedged. */
1045 else if (event.type == KeyPress
1046 && dpyinfo->display == event.xbutton.display)
1048 KeySym keysym = XLookupKeysym (&event.xkey, 0);
1049 if (!IsModifierKey (keysym))
1051 popup_activated_flag = 0;
1052 break;
1055 /* Button presses outside the menu also pop it down. */
1056 else if (event.type == ButtonPress
1057 && event.xany.display == dpyinfo->display
1058 && x_any_window_to_frame (dpyinfo, event.xany.window))
1060 popup_activated_flag = 0;
1061 break;
1064 /* Queue all events not for this popup,
1065 except for Expose, which we've already handled, and ButtonRelease.
1066 Note that the X window is associated with the frame if this
1067 is a menu bar popup, but not if it's a dialog box. So we use
1068 x_non_menubar_window_to_frame, not x_any_window_to_frame. */
1069 if (event.type != Expose
1070 && !(event.type == ButtonRelease
1071 && dpyinfo->display == event.xbutton.display)
1072 && (event.xany.display != dpyinfo->display
1073 || x_non_menubar_window_to_frame (dpyinfo, event.xany.window)))
1075 queue_tmp = (struct event_queue *) malloc (sizeof (struct event_queue));
1077 if (queue_tmp != NULL)
1079 queue_tmp->event = event;
1080 queue_tmp->next = queue;
1081 queue = queue_tmp;
1084 else
1085 XtDispatchEvent (&event);
1087 if (!popup_activated ())
1088 break;
1089 XtAppNextEvent (Xt_app_con, &event);
1092 /* Unread any events that we got but did not handle. */
1093 while (queue != NULL)
1095 queue_tmp = queue;
1096 XPutBackEvent (queue_tmp->event.xany.display, &queue_tmp->event);
1097 queue = queue_tmp->next;
1098 free ((char *)queue_tmp);
1099 /* Cause these events to get read as soon as we UNBLOCK_INPUT. */
1100 interrupt_input_pending = 1;
1104 /* Activate the menu bar of frame F.
1105 This is called from keyboard.c when it gets the
1106 menu_bar_activate_event out of the Emacs event queue.
1108 To activate the menu bar, we use the X button-press event
1109 that was saved in saved_menu_event.
1110 That makes the toolkit do its thing.
1112 But first we recompute the menu bar contents (the whole tree).
1114 The reason for saving the button event until here, instead of
1115 passing it to the toolkit right away, is that we can safely
1116 execute Lisp code. */
1118 void
1119 x_activate_menubar (f)
1120 FRAME_PTR f;
1122 if (!f->output_data.x->saved_menu_event->type)
1123 return;
1125 set_frame_menubar (f, 0, 1);
1126 BLOCK_INPUT;
1127 XtDispatchEvent ((XEvent *) f->output_data.x->saved_menu_event);
1128 UNBLOCK_INPUT;
1129 #ifdef USE_MOTIF
1130 if (f->output_data.x->saved_menu_event->type == ButtonRelease)
1131 pending_menu_activation = 1;
1132 #endif
1134 /* Ignore this if we get it a second time. */
1135 f->output_data.x->saved_menu_event->type = 0;
1138 /* Detect if a dialog or menu has been posted. */
1141 popup_activated ()
1143 return popup_activated_flag;
1147 /* This callback is invoked when the user selects a menubar cascade
1148 pushbutton, but before the pulldown menu is posted. */
1150 static void
1151 popup_activate_callback (widget, id, client_data)
1152 Widget widget;
1153 LWLIB_ID id;
1154 XtPointer client_data;
1156 popup_activated_flag = 1;
1159 /* This callback is called from the menu bar pulldown menu
1160 when the user makes a selection.
1161 Figure out what the user chose
1162 and put the appropriate events into the keyboard buffer. */
1164 static void
1165 menubar_selection_callback (widget, id, client_data)
1166 Widget widget;
1167 LWLIB_ID id;
1168 XtPointer client_data;
1170 Lisp_Object prefix, entry;
1171 FRAME_PTR f = menubar_id_to_frame (id);
1172 Lisp_Object vector;
1173 Lisp_Object *subprefix_stack;
1174 int submenu_depth = 0;
1175 int i;
1177 if (!f)
1178 return;
1179 subprefix_stack = (Lisp_Object *) alloca (f->menu_bar_items_used * sizeof (Lisp_Object));
1180 vector = f->menu_bar_vector;
1181 prefix = Qnil;
1182 i = 0;
1183 while (i < f->menu_bar_items_used)
1185 if (EQ (XVECTOR (vector)->contents[i], Qnil))
1187 subprefix_stack[submenu_depth++] = prefix;
1188 prefix = entry;
1189 i++;
1191 else if (EQ (XVECTOR (vector)->contents[i], Qlambda))
1193 prefix = subprefix_stack[--submenu_depth];
1194 i++;
1196 else if (EQ (XVECTOR (vector)->contents[i], Qt))
1198 prefix = XVECTOR (vector)->contents[i + MENU_ITEMS_PANE_PREFIX];
1199 i += MENU_ITEMS_PANE_LENGTH;
1201 else
1203 entry = XVECTOR (vector)->contents[i + MENU_ITEMS_ITEM_VALUE];
1204 /* The EMACS_INT cast avoids a warning. There's no problem
1205 as long as pointers have enough bits to hold small integers. */
1206 if ((int) (EMACS_INT) client_data == i)
1208 int j;
1209 struct input_event buf;
1210 Lisp_Object frame;
1212 XSETFRAME (frame, f);
1213 buf.kind = menu_bar_event;
1214 buf.frame_or_window = Fcons (frame, Fcons (Qmenu_bar, Qnil));
1215 kbd_buffer_store_event (&buf);
1217 for (j = 0; j < submenu_depth; j++)
1218 if (!NILP (subprefix_stack[j]))
1220 buf.kind = menu_bar_event;
1221 buf.frame_or_window = Fcons (frame, subprefix_stack[j]);
1222 kbd_buffer_store_event (&buf);
1225 if (!NILP (prefix))
1227 buf.kind = menu_bar_event;
1228 buf.frame_or_window = Fcons (frame, prefix);
1229 kbd_buffer_store_event (&buf);
1232 buf.kind = menu_bar_event;
1233 buf.frame_or_window = Fcons (frame, entry);
1234 kbd_buffer_store_event (&buf);
1236 return;
1238 i += MENU_ITEMS_ITEM_LENGTH;
1243 /* This callback is invoked when a dialog or menu is finished being
1244 used and has been unposted. */
1246 static void
1247 popup_deactivate_callback (widget, id, client_data)
1248 Widget widget;
1249 LWLIB_ID id;
1250 XtPointer client_data;
1252 popup_activated_flag = 0;
1255 /* Allocate a widget_value, blocking input. */
1257 widget_value *
1258 xmalloc_widget_value ()
1260 widget_value *value;
1262 BLOCK_INPUT;
1263 value = malloc_widget_value ();
1264 UNBLOCK_INPUT;
1266 return value;
1269 /* This recursively calls free_widget_value on the tree of widgets.
1270 It must free all data that was malloc'ed for these widget_values.
1271 In Emacs, many slots are pointers into the data of Lisp_Strings, and
1272 must be left alone. */
1274 void
1275 free_menubar_widget_value_tree (wv)
1276 widget_value *wv;
1278 if (! wv) return;
1280 wv->name = wv->value = wv->key = (char *) 0xDEADBEEF;
1282 if (wv->contents && (wv->contents != (widget_value*)1))
1284 free_menubar_widget_value_tree (wv->contents);
1285 wv->contents = (widget_value *) 0xDEADBEEF;
1287 if (wv->next)
1289 free_menubar_widget_value_tree (wv->next);
1290 wv->next = (widget_value *) 0xDEADBEEF;
1292 BLOCK_INPUT;
1293 free_widget_value (wv);
1294 UNBLOCK_INPUT;
1297 /* Return a tree of widget_value structures for a menu bar item
1298 whose event type is ITEM_KEY (with string ITEM_NAME)
1299 and whose contents come from the list of keymaps MAPS. */
1301 static widget_value *
1302 single_submenu (item_key, item_name, maps)
1303 Lisp_Object item_key, item_name, maps;
1305 widget_value *wv, *prev_wv, *save_wv, *first_wv;
1306 int i;
1307 int submenu_depth = 0;
1308 Lisp_Object length;
1309 int len;
1310 Lisp_Object *mapvec;
1311 widget_value **submenu_stack;
1312 int previous_items = menu_items_used;
1313 int top_level_items = 0;
1315 length = Flength (maps);
1316 len = XINT (length);
1318 /* Convert the list MAPS into a vector MAPVEC. */
1319 mapvec = (Lisp_Object *) alloca (len * sizeof (Lisp_Object));
1320 for (i = 0; i < len; i++)
1322 mapvec[i] = Fcar (maps);
1323 maps = Fcdr (maps);
1326 menu_items_n_panes = 0;
1328 /* Loop over the given keymaps, making a pane for each map.
1329 But don't make a pane that is empty--ignore that map instead. */
1330 for (i = 0; i < len; i++)
1332 if (SYMBOLP (mapvec[i])
1333 || (CONSP (mapvec[i])
1334 && NILP (Fkeymapp (mapvec[i]))))
1336 /* Here we have a command at top level in the menu bar
1337 as opposed to a submenu. */
1338 top_level_items = 1;
1339 push_menu_pane (Qnil, Qnil);
1340 push_menu_item (item_name, Qt, item_key, mapvec[i], Qnil, Qnil, Qnil);
1342 else
1343 single_keymap_panes (mapvec[i], item_name, item_key, 0, 10);
1346 /* Create a tree of widget_value objects
1347 representing the panes and their items. */
1349 submenu_stack
1350 = (widget_value **) alloca (menu_items_used * sizeof (widget_value *));
1351 wv = xmalloc_widget_value ();
1352 wv->name = "menu";
1353 wv->value = 0;
1354 wv->enabled = 1;
1355 wv->button_type = BUTTON_TYPE_NONE;
1356 first_wv = wv;
1357 save_wv = 0;
1358 prev_wv = 0;
1360 /* Loop over all panes and items made during this call
1361 and construct a tree of widget_value objects.
1362 Ignore the panes and items made by previous calls to
1363 single_submenu, even though those are also in menu_items. */
1364 i = previous_items;
1365 while (i < menu_items_used)
1367 if (EQ (XVECTOR (menu_items)->contents[i], Qnil))
1369 submenu_stack[submenu_depth++] = save_wv;
1370 save_wv = prev_wv;
1371 prev_wv = 0;
1372 i++;
1374 else if (EQ (XVECTOR (menu_items)->contents[i], Qlambda))
1376 prev_wv = save_wv;
1377 save_wv = submenu_stack[--submenu_depth];
1378 i++;
1380 else if (EQ (XVECTOR (menu_items)->contents[i], Qt)
1381 && submenu_depth != 0)
1382 i += MENU_ITEMS_PANE_LENGTH;
1383 /* Ignore a nil in the item list.
1384 It's meaningful only for dialog boxes. */
1385 else if (EQ (XVECTOR (menu_items)->contents[i], Qquote))
1386 i += 1;
1387 else if (EQ (XVECTOR (menu_items)->contents[i], Qt))
1389 /* Create a new pane. */
1390 Lisp_Object pane_name, prefix;
1391 char *pane_string;
1392 pane_name = XVECTOR (menu_items)->contents[i + MENU_ITEMS_PANE_NAME];
1393 prefix = XVECTOR (menu_items)->contents[i + MENU_ITEMS_PANE_PREFIX];
1394 #ifndef HAVE_MULTILINGUAL_MENU
1395 if (STRINGP (pane_name) && STRING_MULTIBYTE (pane_name))
1396 pane_name = string_make_unibyte (pane_name);
1397 #endif
1398 pane_string = (NILP (pane_name)
1399 ? "" : (char *) XSTRING (pane_name)->data);
1400 /* If there is just one top-level pane, put all its items directly
1401 under the top-level menu. */
1402 if (menu_items_n_panes == 1)
1403 pane_string = "";
1405 /* If the pane has a meaningful name,
1406 make the pane a top-level menu item
1407 with its items as a submenu beneath it. */
1408 if (strcmp (pane_string, ""))
1410 wv = xmalloc_widget_value ();
1411 if (save_wv)
1412 save_wv->next = wv;
1413 else
1414 first_wv->contents = wv;
1415 wv->name = pane_string;
1416 /* Ignore the @ that means "separate pane".
1417 This is a kludge, but this isn't worth more time. */
1418 if (!NILP (prefix) && wv->name[0] == '@')
1419 wv->name++;
1420 wv->value = 0;
1421 wv->enabled = 1;
1422 wv->button_type = BUTTON_TYPE_NONE;
1424 save_wv = wv;
1425 prev_wv = 0;
1426 i += MENU_ITEMS_PANE_LENGTH;
1428 else
1430 /* Create a new item within current pane. */
1431 Lisp_Object item_name, enable, descrip, def, type, selected;
1432 item_name = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_NAME];
1433 enable = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_ENABLE];
1434 descrip
1435 = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_EQUIV_KEY];
1436 def = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_DEFINITION];
1437 type = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_TYPE];
1438 selected = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_SELECTED];
1440 #ifndef HAVE_MULTILINGUAL_MENU
1441 if (STRING_MULTIBYTE (item_name))
1442 item_name = string_make_unibyte (item_name);
1443 if (STRINGP (descrip) && STRING_MULTIBYTE (descrip))
1444 descrip = string_make_unibyte (descrip);
1445 #endif
1447 wv = xmalloc_widget_value ();
1448 if (prev_wv)
1449 prev_wv->next = wv;
1450 else
1451 save_wv->contents = wv;
1453 wv->name = (char *) XSTRING (item_name)->data;
1454 if (!NILP (descrip))
1455 wv->key = (char *) XSTRING (descrip)->data;
1456 wv->value = 0;
1457 /* The EMACS_INT cast avoids a warning. There's no problem
1458 as long as pointers have enough bits to hold small integers. */
1459 wv->call_data = (!NILP (def) ? (void *) (EMACS_INT) i : 0);
1460 wv->enabled = !NILP (enable);
1462 if (NILP (type))
1463 wv->button_type = BUTTON_TYPE_NONE;
1464 else if (EQ (type, QCradio))
1465 wv->button_type = BUTTON_TYPE_RADIO;
1466 else if (EQ (type, QCtoggle))
1467 wv->button_type = BUTTON_TYPE_TOGGLE;
1468 else
1469 abort ();
1471 wv->selected = !NILP (selected);
1473 prev_wv = wv;
1475 i += MENU_ITEMS_ITEM_LENGTH;
1479 /* If we have just one "menu item"
1480 that was originally a button, return it by itself. */
1481 if (top_level_items && first_wv->contents && first_wv->contents->next == 0)
1483 wv = first_wv->contents;
1484 free_widget_value (first_wv);
1485 return wv;
1488 return first_wv;
1491 extern void EmacsFrameSetCharSize ();
1493 /* Recompute all the widgets of frame F, when the menu bar
1494 has been changed. */
1496 static void
1497 update_frame_menubar (f)
1498 FRAME_PTR f;
1500 struct x_output *x = f->output_data.x;
1501 int columns, rows;
1502 int menubar_changed;
1504 /* We assume the menubar contents has changed if the global flag is set,
1505 or if the current buffer has changed, or if the menubar has never
1506 been updated before.
1508 menubar_changed = (x->menubar_widget
1509 && !XtIsManaged (x->menubar_widget));
1511 if (! (menubar_changed))
1512 return;
1514 BLOCK_INPUT;
1515 /* Save the size of the frame because the pane widget doesn't accept to
1516 resize itself. So force it. */
1517 columns = f->width;
1518 rows = f->height;
1520 /* Do the voodoo which means "I'm changing lots of things, don't try to
1521 refigure sizes until I'm done." */
1522 lw_refigure_widget (x->column_widget, False);
1524 /* the order in which children are managed is the top to
1525 bottom order in which they are displayed in the paned window.
1526 First, remove the text-area widget.
1528 XtUnmanageChild (x->edit_widget);
1530 /* remove the menubar that is there now, and put up the menubar that
1531 should be there.
1533 if (menubar_changed)
1535 XtManageChild (x->menubar_widget);
1536 XtMapWidget (x->menubar_widget);
1537 XtVaSetValues (x->menubar_widget, XtNmappedWhenManaged, 1, 0);
1540 /* Re-manage the text-area widget, and then thrash the sizes. */
1541 XtManageChild (x->edit_widget);
1542 x_set_menu_resources_from_menu_face (f, x->menubar_widget);
1543 lw_refigure_widget (x->column_widget, True);
1545 /* Force the pane widget to resize itself with the right values. */
1546 EmacsFrameSetCharSize (x->edit_widget, columns, rows);
1547 UNBLOCK_INPUT;
1550 /* Set the contents of the menubar widgets of frame F.
1551 The argument FIRST_TIME is currently ignored;
1552 it is set the first time this is called, from initialize_frame_menubar. */
1554 void
1555 set_frame_menubar (f, first_time, deep_p)
1556 FRAME_PTR f;
1557 int first_time;
1558 int deep_p;
1560 Widget menubar_widget = f->output_data.x->menubar_widget;
1561 Lisp_Object items;
1562 widget_value *wv, *first_wv, *prev_wv = 0;
1563 int i;
1564 LWLIB_ID id;
1566 XSETFRAME (Vmenu_updating_frame, f);
1568 if (f->output_data.x->id == 0)
1569 f->output_data.x->id = next_menubar_widget_id++;
1570 id = f->output_data.x->id;
1572 if (! menubar_widget)
1573 deep_p = 1;
1574 else if (pending_menu_activation && !deep_p)
1575 deep_p = 1;
1576 /* Make the first call for any given frame always go deep. */
1577 else if (!f->output_data.x->saved_menu_event && !deep_p)
1579 deep_p = 1;
1580 f->output_data.x->saved_menu_event = (XEvent*)xmalloc (sizeof (XEvent));
1581 f->output_data.x->saved_menu_event->type = 0;
1584 wv = xmalloc_widget_value ();
1585 wv->name = "menubar";
1586 wv->value = 0;
1587 wv->enabled = 1;
1588 wv->button_type = BUTTON_TYPE_NONE;
1589 first_wv = wv;
1591 if (deep_p)
1593 /* Make a widget-value tree representing the entire menu trees. */
1595 struct buffer *prev = current_buffer;
1596 Lisp_Object buffer;
1597 int specpdl_count = specpdl_ptr - specpdl;
1598 int previous_menu_items_used = f->menu_bar_items_used;
1599 Lisp_Object *previous_items
1600 = (Lisp_Object *) alloca (previous_menu_items_used
1601 * sizeof (Lisp_Object));
1603 /* If we are making a new widget, its contents are empty,
1604 do always reinitialize them. */
1605 if (! menubar_widget)
1606 previous_menu_items_used = 0;
1608 buffer = XWINDOW (FRAME_SELECTED_WINDOW (f))->buffer;
1609 specbind (Qinhibit_quit, Qt);
1610 /* Don't let the debugger step into this code
1611 because it is not reentrant. */
1612 specbind (Qdebug_on_next_call, Qnil);
1614 record_unwind_protect (Fset_match_data, Fmatch_data (Qnil, Qnil));
1615 if (NILP (Voverriding_local_map_menu_flag))
1617 specbind (Qoverriding_terminal_local_map, Qnil);
1618 specbind (Qoverriding_local_map, Qnil);
1621 set_buffer_internal_1 (XBUFFER (buffer));
1623 /* Run the Lucid hook. */
1624 call1 (Vrun_hooks, Qactivate_menubar_hook);
1625 /* If it has changed current-menubar from previous value,
1626 really recompute the menubar from the value. */
1627 if (! NILP (Vlucid_menu_bar_dirty_flag))
1628 call0 (Qrecompute_lucid_menubar);
1629 safe_run_hooks (Qmenu_bar_update_hook);
1630 FRAME_MENU_BAR_ITEMS (f) = menu_bar_items (FRAME_MENU_BAR_ITEMS (f));
1632 items = FRAME_MENU_BAR_ITEMS (f);
1634 inhibit_garbage_collection ();
1636 /* Save the frame's previous menu bar contents data. */
1637 bcopy (XVECTOR (f->menu_bar_vector)->contents, previous_items,
1638 previous_menu_items_used * sizeof (Lisp_Object));
1640 /* Fill in the current menu bar contents. */
1641 menu_items = f->menu_bar_vector;
1642 menu_items_allocated = XVECTOR (menu_items)->size;
1643 init_menu_items ();
1644 for (i = 0; i < XVECTOR (items)->size; i += 4)
1646 Lisp_Object key, string, maps;
1648 key = XVECTOR (items)->contents[i];
1649 string = XVECTOR (items)->contents[i + 1];
1650 maps = XVECTOR (items)->contents[i + 2];
1651 if (NILP (string))
1652 break;
1654 wv = single_submenu (key, string, maps);
1655 if (prev_wv)
1656 prev_wv->next = wv;
1657 else
1658 first_wv->contents = wv;
1659 /* Don't set wv->name here; GC during the loop might relocate it. */
1660 wv->enabled = 1;
1661 wv->button_type = BUTTON_TYPE_NONE;
1662 prev_wv = wv;
1665 finish_menu_items ();
1667 set_buffer_internal_1 (prev);
1668 unbind_to (specpdl_count, Qnil);
1670 /* If there has been no change in the Lisp-level contents
1671 of the menu bar, skip redisplaying it. Just exit. */
1673 for (i = 0; i < previous_menu_items_used; i++)
1674 if (menu_items_used == i
1675 || (!EQ (previous_items[i], XVECTOR (menu_items)->contents[i])))
1676 break;
1677 if (i == menu_items_used && i == previous_menu_items_used && i != 0)
1679 free_menubar_widget_value_tree (first_wv);
1680 menu_items = Qnil;
1682 return;
1685 /* Now GC cannot happen during the lifetime of the widget_value,
1686 so it's safe to store data from a Lisp_String. */
1687 wv = first_wv->contents;
1688 for (i = 0; i < XVECTOR (items)->size; i += 4)
1690 Lisp_Object string;
1691 string = XVECTOR (items)->contents[i + 1];
1692 if (NILP (string))
1693 break;
1694 wv->name = (char *) XSTRING (string)->data;
1695 wv = wv->next;
1698 f->menu_bar_vector = menu_items;
1699 f->menu_bar_items_used = menu_items_used;
1700 menu_items = Qnil;
1702 else
1704 /* Make a widget-value tree containing
1705 just the top level menu bar strings. */
1707 items = FRAME_MENU_BAR_ITEMS (f);
1708 for (i = 0; i < XVECTOR (items)->size; i += 4)
1710 Lisp_Object string;
1712 string = XVECTOR (items)->contents[i + 1];
1713 if (NILP (string))
1714 break;
1716 wv = xmalloc_widget_value ();
1717 wv->name = (char *) XSTRING (string)->data;
1718 wv->value = 0;
1719 wv->enabled = 1;
1720 wv->button_type = BUTTON_TYPE_NONE;
1721 /* This prevents lwlib from assuming this
1722 menu item is really supposed to be empty. */
1723 /* The EMACS_INT cast avoids a warning.
1724 This value just has to be different from small integers. */
1725 wv->call_data = (void *) (EMACS_INT) (-1);
1727 if (prev_wv)
1728 prev_wv->next = wv;
1729 else
1730 first_wv->contents = wv;
1731 prev_wv = wv;
1734 /* Forget what we thought we knew about what is in the
1735 detailed contents of the menu bar menus.
1736 Changing the top level always destroys the contents. */
1737 f->menu_bar_items_used = 0;
1740 /* Create or update the menu bar widget. */
1742 BLOCK_INPUT;
1744 if (menubar_widget)
1746 /* Disable resizing (done for Motif!) */
1747 lw_allow_resizing (f->output_data.x->widget, False);
1749 /* The third arg is DEEP_P, which says to consider the entire
1750 menu trees we supply, rather than just the menu bar item names. */
1751 lw_modify_all_widgets (id, first_wv, deep_p);
1753 /* Re-enable the edit widget to resize. */
1754 lw_allow_resizing (f->output_data.x->widget, True);
1756 else
1758 menubar_widget = lw_create_widget ("menubar", "menubar", id, first_wv,
1759 f->output_data.x->column_widget,
1761 popup_activate_callback,
1762 menubar_selection_callback,
1763 popup_deactivate_callback);
1764 f->output_data.x->menubar_widget = menubar_widget;
1768 int menubar_size
1769 = (f->output_data.x->menubar_widget
1770 ? (f->output_data.x->menubar_widget->core.height
1771 + f->output_data.x->menubar_widget->core.border_width)
1772 : 0);
1774 #if 0 /* Experimentally, we now get the right results
1775 for -geometry -0-0 without this. 24 Aug 96, rms. */
1776 #ifdef USE_LUCID
1777 if (FRAME_EXTERNAL_MENU_BAR (f))
1779 Dimension ibw = 0;
1780 XtVaGetValues (f->output_data.x->column_widget,
1781 XtNinternalBorderWidth, &ibw, NULL);
1782 menubar_size += ibw;
1784 #endif /* USE_LUCID */
1785 #endif /* 0 */
1787 f->output_data.x->menubar_height = menubar_size;
1790 free_menubar_widget_value_tree (first_wv);
1791 update_frame_menubar (f);
1793 UNBLOCK_INPUT;
1796 /* Called from Fx_create_frame to create the initial menubar of a frame
1797 before it is mapped, so that the window is mapped with the menubar already
1798 there instead of us tacking it on later and thrashing the window after it
1799 is visible. */
1801 void
1802 initialize_frame_menubar (f)
1803 FRAME_PTR f;
1805 /* This function is called before the first chance to redisplay
1806 the frame. It has to be, so the frame will have the right size. */
1807 FRAME_MENU_BAR_ITEMS (f) = menu_bar_items (FRAME_MENU_BAR_ITEMS (f));
1808 set_frame_menubar (f, 1, 1);
1811 /* Get rid of the menu bar of frame F, and free its storage.
1812 This is used when deleting a frame, and when turning off the menu bar. */
1814 void
1815 free_frame_menubar (f)
1816 FRAME_PTR f;
1818 Widget menubar_widget;
1820 menubar_widget = f->output_data.x->menubar_widget;
1822 f->output_data.x->menubar_height = 0;
1824 if (menubar_widget)
1826 BLOCK_INPUT;
1827 lw_destroy_all_widgets ((LWLIB_ID) f->output_data.x->id);
1828 UNBLOCK_INPUT;
1832 #endif /* USE_X_TOOLKIT */
1834 /* xmenu_show actually displays a menu using the panes and items in menu_items
1835 and returns the value selected from it.
1836 There are two versions of xmenu_show, one for Xt and one for Xlib.
1837 Both assume input is blocked by the caller. */
1839 /* F is the frame the menu is for.
1840 X and Y are the frame-relative specified position,
1841 relative to the inside upper left corner of the frame F.
1842 FOR_CLICK is nonzero if this menu was invoked for a mouse click.
1843 KEYMAPS is 1 if this menu was specified with keymaps;
1844 in that case, we return a list containing the chosen item's value
1845 and perhaps also the pane's prefix.
1846 TITLE is the specified menu title.
1847 ERROR is a place to store an error message string in case of failure.
1848 (We return nil on failure, but the value doesn't actually matter.) */
1850 #ifdef USE_X_TOOLKIT
1852 /* We need a unique id for each widget handled by the Lucid Widget
1853 library.
1855 For the main windows, and popup menus, we use this counter,
1856 which we increment each time after use. This starts from 1<<16.
1858 For menu bars, we use numbers starting at 0, counted in
1859 next_menubar_widget_id. */
1860 LWLIB_ID widget_id_tick;
1862 #ifdef __STDC__
1863 static Lisp_Object *volatile menu_item_selection;
1864 #else
1865 static Lisp_Object *menu_item_selection;
1866 #endif
1868 static void
1869 popup_selection_callback (widget, id, client_data)
1870 Widget widget;
1871 LWLIB_ID id;
1872 XtPointer client_data;
1874 menu_item_selection = (Lisp_Object *) client_data;
1877 static Lisp_Object
1878 xmenu_show (f, x, y, for_click, keymaps, title, error)
1879 FRAME_PTR f;
1880 int x;
1881 int y;
1882 int for_click;
1883 int keymaps;
1884 Lisp_Object title;
1885 char **error;
1887 int i;
1888 LWLIB_ID menu_id;
1889 Widget menu;
1890 Arg av[2];
1891 int ac = 0;
1892 widget_value *wv, *save_wv = 0, *first_wv = 0, *prev_wv = 0;
1893 widget_value **submenu_stack
1894 = (widget_value **) alloca (menu_items_used * sizeof (widget_value *));
1895 Lisp_Object *subprefix_stack
1896 = (Lisp_Object *) alloca (menu_items_used * sizeof (Lisp_Object));
1897 int submenu_depth = 0;
1898 XButtonPressedEvent dummy;
1900 int first_pane;
1902 *error = NULL;
1904 if (menu_items_used <= MENU_ITEMS_PANE_LENGTH)
1906 *error = "Empty menu";
1907 return Qnil;
1910 /* Create a tree of widget_value objects
1911 representing the panes and their items. */
1912 wv = xmalloc_widget_value ();
1913 wv->name = "menu";
1914 wv->value = 0;
1915 wv->enabled = 1;
1916 wv->button_type = BUTTON_TYPE_NONE;
1917 first_wv = wv;
1918 first_pane = 1;
1920 /* Loop over all panes and items, filling in the tree. */
1921 i = 0;
1922 while (i < menu_items_used)
1924 if (EQ (XVECTOR (menu_items)->contents[i], Qnil))
1926 submenu_stack[submenu_depth++] = save_wv;
1927 save_wv = prev_wv;
1928 prev_wv = 0;
1929 first_pane = 1;
1930 i++;
1932 else if (EQ (XVECTOR (menu_items)->contents[i], Qlambda))
1934 prev_wv = save_wv;
1935 save_wv = submenu_stack[--submenu_depth];
1936 first_pane = 0;
1937 i++;
1939 else if (EQ (XVECTOR (menu_items)->contents[i], Qt)
1940 && submenu_depth != 0)
1941 i += MENU_ITEMS_PANE_LENGTH;
1942 /* Ignore a nil in the item list.
1943 It's meaningful only for dialog boxes. */
1944 else if (EQ (XVECTOR (menu_items)->contents[i], Qquote))
1945 i += 1;
1946 else if (EQ (XVECTOR (menu_items)->contents[i], Qt))
1948 /* Create a new pane. */
1949 Lisp_Object pane_name, prefix;
1950 char *pane_string;
1951 pane_name = XVECTOR (menu_items)->contents[i + MENU_ITEMS_PANE_NAME];
1952 prefix = XVECTOR (menu_items)->contents[i + MENU_ITEMS_PANE_PREFIX];
1953 #ifndef HAVE_MULTILINGUAL_MENU
1954 if (!NILP (pane_name) && STRING_MULTIBYTE (pane_name))
1955 pane_name = string_make_unibyte (pane_name);
1956 #endif
1957 pane_string = (NILP (pane_name)
1958 ? "" : (char *) XSTRING (pane_name)->data);
1959 /* If there is just one top-level pane, put all its items directly
1960 under the top-level menu. */
1961 if (menu_items_n_panes == 1)
1962 pane_string = "";
1964 /* If the pane has a meaningful name,
1965 make the pane a top-level menu item
1966 with its items as a submenu beneath it. */
1967 if (!keymaps && strcmp (pane_string, ""))
1969 wv = xmalloc_widget_value ();
1970 if (save_wv)
1971 save_wv->next = wv;
1972 else
1973 first_wv->contents = wv;
1974 wv->name = pane_string;
1975 if (keymaps && !NILP (prefix))
1976 wv->name++;
1977 wv->value = 0;
1978 wv->enabled = 1;
1979 wv->button_type = BUTTON_TYPE_NONE;
1980 save_wv = wv;
1981 prev_wv = 0;
1983 else if (first_pane)
1985 save_wv = wv;
1986 prev_wv = 0;
1988 first_pane = 0;
1989 i += MENU_ITEMS_PANE_LENGTH;
1991 else
1993 /* Create a new item within current pane. */
1994 Lisp_Object item_name, enable, descrip, def, type, selected;
1995 item_name = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_NAME];
1996 enable = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_ENABLE];
1997 descrip
1998 = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_EQUIV_KEY];
1999 def = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_DEFINITION];
2000 type = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_TYPE];
2001 selected = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_SELECTED];
2003 #ifndef HAVE_MULTILINGUAL_MENU
2004 if (STRINGP (item_name) && STRING_MULTIBYTE (item_name))
2005 item_name = string_make_unibyte (item_name);
2006 if (STRINGP (descrip) && STRING_MULTIBYTE (descrip))
2007 item_name = string_make_unibyte (descrip);
2008 #endif
2010 wv = xmalloc_widget_value ();
2011 if (prev_wv)
2012 prev_wv->next = wv;
2013 else
2014 save_wv->contents = wv;
2015 wv->name = (char *) XSTRING (item_name)->data;
2016 if (!NILP (descrip))
2017 wv->key = (char *) XSTRING (descrip)->data;
2018 wv->value = 0;
2019 /* If this item has a null value,
2020 make the call_data null so that it won't display a box
2021 when the mouse is on it. */
2022 wv->call_data
2023 = (!NILP (def) ? (void *) &XVECTOR (menu_items)->contents[i] : 0);
2024 wv->enabled = !NILP (enable);
2026 if (NILP (type))
2027 wv->button_type = BUTTON_TYPE_NONE;
2028 else if (EQ (type, QCtoggle))
2029 wv->button_type = BUTTON_TYPE_TOGGLE;
2030 else if (EQ (type, QCradio))
2031 wv->button_type = BUTTON_TYPE_RADIO;
2032 else
2033 abort ();
2035 wv->selected = !NILP (selected);
2037 prev_wv = wv;
2039 i += MENU_ITEMS_ITEM_LENGTH;
2043 /* Deal with the title, if it is non-nil. */
2044 if (!NILP (title))
2046 widget_value *wv_title = xmalloc_widget_value ();
2047 widget_value *wv_sep1 = xmalloc_widget_value ();
2048 widget_value *wv_sep2 = xmalloc_widget_value ();
2050 wv_sep2->name = "--";
2051 wv_sep2->next = first_wv->contents;
2053 wv_sep1->name = "--";
2054 wv_sep1->next = wv_sep2;
2056 #ifndef HAVE_MULTILINGUAL_MENU
2057 if (STRING_MULTIBYTE (title))
2058 title = string_make_unibyte (title);
2059 #endif
2060 wv_title->name = (char *) XSTRING (title)->data;
2061 wv_title->enabled = True;
2062 wv_title->button_type = BUTTON_TYPE_NONE;
2063 wv_title->next = wv_sep1;
2064 first_wv->contents = wv_title;
2067 /* Actually create the menu. */
2068 menu_id = widget_id_tick++;
2069 menu = lw_create_widget ("popup", first_wv->name, menu_id, first_wv,
2070 f->output_data.x->widget, 1, 0,
2071 popup_selection_callback,
2072 popup_deactivate_callback);
2074 /* Adjust coordinates to relative to the outer (window manager) window. */
2076 Window child;
2077 int win_x = 0, win_y = 0;
2079 /* Find the position of the outside upper-left corner of
2080 the inner window, with respect to the outer window. */
2081 if (f->output_data.x->parent_desc != FRAME_X_DISPLAY_INFO (f)->root_window)
2083 BLOCK_INPUT;
2084 XTranslateCoordinates (FRAME_X_DISPLAY (f),
2086 /* From-window, to-window. */
2087 f->output_data.x->window_desc,
2088 f->output_data.x->parent_desc,
2090 /* From-position, to-position. */
2091 0, 0, &win_x, &win_y,
2093 /* Child of window. */
2094 &child);
2095 UNBLOCK_INPUT;
2096 x += win_x;
2097 y += win_y;
2101 /* Adjust coordinates to be root-window-relative. */
2102 x += f->output_data.x->left_pos;
2103 y += f->output_data.x->top_pos;
2105 dummy.type = ButtonPress;
2106 dummy.serial = 0;
2107 dummy.send_event = 0;
2108 dummy.display = FRAME_X_DISPLAY (f);
2109 dummy.time = CurrentTime;
2110 dummy.root = FRAME_X_DISPLAY_INFO (f)->root_window;
2111 dummy.window = dummy.root;
2112 dummy.subwindow = dummy.root;
2113 dummy.x_root = x;
2114 dummy.y_root = y;
2115 dummy.x = x;
2116 dummy.y = y;
2117 dummy.state = (FRAME_X_DISPLAY_INFO (f)->grabbed >> 1) * Button1Mask;
2118 dummy.button = 0;
2119 for (i = 0; i < 5; i++)
2120 if (FRAME_X_DISPLAY_INFO (f)->grabbed & (1 << i))
2121 dummy.button = i;
2123 /* Don't allow any geometry request from the user. */
2124 XtSetArg (av[ac], XtNgeometry, 0); ac++;
2125 XtSetValues (menu, av, ac);
2127 /* Free the widget_value objects we used to specify the contents. */
2128 free_menubar_widget_value_tree (first_wv);
2130 /* No selection has been chosen yet. */
2131 menu_item_selection = 0;
2133 /* Display the menu. */
2134 lw_popup_menu (menu, &dummy);
2135 x_set_menu_resources_from_menu_face (f, menu);
2136 popup_activated_flag = 1;
2138 /* Process events that apply to the menu. */
2139 popup_get_selection ((XEvent *) 0, FRAME_X_DISPLAY_INFO (f), menu_id);
2141 #ifdef LESSTIF_VERSION
2142 /* Nov 1998: For an unknown reason a button grab remains active
2143 after the popup menu has gone. */
2144 XUngrabButton (XtDisplay (f->output_data.x->widget),
2145 AnyButton, AnyModifier,
2146 XtWindow (f->output_data.x->widget));
2147 XUngrabButton (XtDisplay (f->output_data.x->edit_widget),
2148 AnyButton, AnyModifier,
2149 XtWindow (f->output_data.x->edit_widget));
2150 #endif /* LESSTIF_VERSION */
2152 /* fp turned off the following statement and wrote a comment
2153 that it is unnecessary--that the menu has already disappeared.
2154 Nowadays the menu disappears ok, all right, but
2155 we need to delete the widgets or multiple ones will pile up. */
2156 lw_destroy_all_widgets (menu_id);
2158 /* Find the selected item, and its pane, to return
2159 the proper value. */
2160 if (menu_item_selection != 0)
2162 Lisp_Object prefix, entry;
2164 prefix = Qnil;
2165 i = 0;
2166 while (i < menu_items_used)
2168 if (EQ (XVECTOR (menu_items)->contents[i], Qnil))
2170 subprefix_stack[submenu_depth++] = prefix;
2171 prefix = entry;
2172 i++;
2174 else if (EQ (XVECTOR (menu_items)->contents[i], Qlambda))
2176 prefix = subprefix_stack[--submenu_depth];
2177 i++;
2179 else if (EQ (XVECTOR (menu_items)->contents[i], Qt))
2181 prefix
2182 = XVECTOR (menu_items)->contents[i + MENU_ITEMS_PANE_PREFIX];
2183 i += MENU_ITEMS_PANE_LENGTH;
2185 /* Ignore a nil in the item list.
2186 It's meaningful only for dialog boxes. */
2187 else if (EQ (XVECTOR (menu_items)->contents[i], Qquote))
2188 i += 1;
2189 else
2191 entry
2192 = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_VALUE];
2193 if (menu_item_selection == &XVECTOR (menu_items)->contents[i])
2195 if (keymaps != 0)
2197 int j;
2199 entry = Fcons (entry, Qnil);
2200 if (!NILP (prefix))
2201 entry = Fcons (prefix, entry);
2202 for (j = submenu_depth - 1; j >= 0; j--)
2203 if (!NILP (subprefix_stack[j]))
2204 entry = Fcons (subprefix_stack[j], entry);
2206 return entry;
2208 i += MENU_ITEMS_ITEM_LENGTH;
2213 return Qnil;
2216 static void
2217 dialog_selection_callback (widget, id, client_data)
2218 Widget widget;
2219 LWLIB_ID id;
2220 XtPointer client_data;
2222 /* The EMACS_INT cast avoids a warning. There's no problem
2223 as long as pointers have enough bits to hold small integers. */
2224 if ((int) (EMACS_INT) client_data != -1)
2225 menu_item_selection = (Lisp_Object *) client_data;
2226 BLOCK_INPUT;
2227 lw_destroy_all_widgets (id);
2228 UNBLOCK_INPUT;
2229 popup_activated_flag = 0;
2232 static char * button_names [] = {
2233 "button1", "button2", "button3", "button4", "button5",
2234 "button6", "button7", "button8", "button9", "button10" };
2236 static Lisp_Object
2237 xdialog_show (f, keymaps, title, error)
2238 FRAME_PTR f;
2239 int keymaps;
2240 Lisp_Object title;
2241 char **error;
2243 int i, nb_buttons=0;
2244 LWLIB_ID dialog_id;
2245 Widget menu;
2246 char dialog_name[6];
2248 widget_value *wv, *first_wv = 0, *prev_wv = 0;
2250 /* Number of elements seen so far, before boundary. */
2251 int left_count = 0;
2252 /* 1 means we've seen the boundary between left-hand elts and right-hand. */
2253 int boundary_seen = 0;
2255 *error = NULL;
2257 if (menu_items_n_panes > 1)
2259 *error = "Multiple panes in dialog box";
2260 return Qnil;
2263 /* Create a tree of widget_value objects
2264 representing the text label and buttons. */
2266 Lisp_Object pane_name, prefix;
2267 char *pane_string;
2268 pane_name = XVECTOR (menu_items)->contents[MENU_ITEMS_PANE_NAME];
2269 prefix = XVECTOR (menu_items)->contents[MENU_ITEMS_PANE_PREFIX];
2270 pane_string = (NILP (pane_name)
2271 ? "" : (char *) XSTRING (pane_name)->data);
2272 prev_wv = xmalloc_widget_value ();
2273 prev_wv->value = pane_string;
2274 if (keymaps && !NILP (prefix))
2275 prev_wv->name++;
2276 prev_wv->enabled = 1;
2277 prev_wv->name = "message";
2278 first_wv = prev_wv;
2280 /* Loop over all panes and items, filling in the tree. */
2281 i = MENU_ITEMS_PANE_LENGTH;
2282 while (i < menu_items_used)
2285 /* Create a new item within current pane. */
2286 Lisp_Object item_name, enable, descrip;
2287 item_name = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_NAME];
2288 enable = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_ENABLE];
2289 descrip
2290 = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_EQUIV_KEY];
2292 if (NILP (item_name))
2294 free_menubar_widget_value_tree (first_wv);
2295 *error = "Submenu in dialog items";
2296 return Qnil;
2298 if (EQ (item_name, Qquote))
2300 /* This is the boundary between left-side elts
2301 and right-side elts. Stop incrementing right_count. */
2302 boundary_seen = 1;
2303 i++;
2304 continue;
2306 if (nb_buttons >= 9)
2308 free_menubar_widget_value_tree (first_wv);
2309 *error = "Too many dialog items";
2310 return Qnil;
2313 wv = xmalloc_widget_value ();
2314 prev_wv->next = wv;
2315 wv->name = (char *) button_names[nb_buttons];
2316 if (!NILP (descrip))
2317 wv->key = (char *) XSTRING (descrip)->data;
2318 wv->value = (char *) XSTRING (item_name)->data;
2319 wv->call_data = (void *) &XVECTOR (menu_items)->contents[i];
2320 wv->enabled = !NILP (enable);
2321 prev_wv = wv;
2323 if (! boundary_seen)
2324 left_count++;
2326 nb_buttons++;
2327 i += MENU_ITEMS_ITEM_LENGTH;
2330 /* If the boundary was not specified,
2331 by default put half on the left and half on the right. */
2332 if (! boundary_seen)
2333 left_count = nb_buttons - nb_buttons / 2;
2335 wv = xmalloc_widget_value ();
2336 wv->name = dialog_name;
2338 /* Dialog boxes use a really stupid name encoding
2339 which specifies how many buttons to use
2340 and how many buttons are on the right.
2341 The Q means something also. */
2342 dialog_name[0] = 'Q';
2343 dialog_name[1] = '0' + nb_buttons;
2344 dialog_name[2] = 'B';
2345 dialog_name[3] = 'R';
2346 /* Number of buttons to put on the right. */
2347 dialog_name[4] = '0' + nb_buttons - left_count;
2348 dialog_name[5] = 0;
2349 wv->contents = first_wv;
2350 first_wv = wv;
2353 /* Actually create the dialog. */
2354 dialog_id = widget_id_tick++;
2355 menu = lw_create_widget (first_wv->name, "dialog", dialog_id, first_wv,
2356 f->output_data.x->widget, 1, 0,
2357 dialog_selection_callback, 0);
2358 lw_modify_all_widgets (dialog_id, first_wv->contents, True);
2359 /* Free the widget_value objects we used to specify the contents. */
2360 free_menubar_widget_value_tree (first_wv);
2362 /* No selection has been chosen yet. */
2363 menu_item_selection = 0;
2365 /* Display the menu. */
2366 lw_pop_up_all_widgets (dialog_id);
2367 popup_activated_flag = 1;
2369 /* Process events that apply to the menu. */
2370 popup_get_selection ((XEvent *) 0, FRAME_X_DISPLAY_INFO (f), dialog_id);
2372 lw_destroy_all_widgets (dialog_id);
2374 /* Find the selected item, and its pane, to return
2375 the proper value. */
2376 if (menu_item_selection != 0)
2378 Lisp_Object prefix;
2380 prefix = Qnil;
2381 i = 0;
2382 while (i < menu_items_used)
2384 Lisp_Object entry;
2386 if (EQ (XVECTOR (menu_items)->contents[i], Qt))
2388 prefix
2389 = XVECTOR (menu_items)->contents[i + MENU_ITEMS_PANE_PREFIX];
2390 i += MENU_ITEMS_PANE_LENGTH;
2392 else
2394 entry
2395 = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_VALUE];
2396 if (menu_item_selection == &XVECTOR (menu_items)->contents[i])
2398 if (keymaps != 0)
2400 entry = Fcons (entry, Qnil);
2401 if (!NILP (prefix))
2402 entry = Fcons (prefix, entry);
2404 return entry;
2406 i += MENU_ITEMS_ITEM_LENGTH;
2411 return Qnil;
2413 #else /* not USE_X_TOOLKIT */
2415 static Lisp_Object
2416 xmenu_show (f, x, y, for_click, keymaps, title, error)
2417 FRAME_PTR f;
2418 int x, y;
2419 int for_click;
2420 int keymaps;
2421 Lisp_Object title;
2422 char **error;
2424 Window root;
2425 XMenu *menu;
2426 int pane, selidx, lpane, status;
2427 Lisp_Object entry, pane_prefix;
2428 char *datap;
2429 int ulx, uly, width, height;
2430 int dispwidth, dispheight;
2431 int i, j;
2432 int maxwidth;
2433 int dummy_int;
2434 unsigned int dummy_uint;
2436 *error = 0;
2437 if (menu_items_n_panes == 0)
2438 return Qnil;
2440 if (menu_items_used <= MENU_ITEMS_PANE_LENGTH)
2442 *error = "Empty menu";
2443 return Qnil;
2446 /* Figure out which root window F is on. */
2447 XGetGeometry (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), &root,
2448 &dummy_int, &dummy_int, &dummy_uint, &dummy_uint,
2449 &dummy_uint, &dummy_uint);
2451 /* Make the menu on that window. */
2452 menu = XMenuCreate (FRAME_X_DISPLAY (f), root, "emacs");
2453 if (menu == NULL)
2455 *error = "Can't create menu";
2456 return Qnil;
2459 #ifdef HAVE_X_WINDOWS
2460 /* Adjust coordinates to relative to the outer (window manager) window. */
2462 Window child;
2463 int win_x = 0, win_y = 0;
2465 /* Find the position of the outside upper-left corner of
2466 the inner window, with respect to the outer window. */
2467 if (f->output_data.x->parent_desc != FRAME_X_DISPLAY_INFO (f)->root_window)
2469 BLOCK_INPUT;
2470 XTranslateCoordinates (FRAME_X_DISPLAY (f),
2472 /* From-window, to-window. */
2473 f->output_data.x->window_desc,
2474 f->output_data.x->parent_desc,
2476 /* From-position, to-position. */
2477 0, 0, &win_x, &win_y,
2479 /* Child of window. */
2480 &child);
2481 UNBLOCK_INPUT;
2482 x += win_x;
2483 y += win_y;
2486 #endif /* HAVE_X_WINDOWS */
2488 /* Adjust coordinates to be root-window-relative. */
2489 x += f->output_data.x->left_pos;
2490 y += f->output_data.x->top_pos;
2492 /* Create all the necessary panes and their items. */
2493 i = 0;
2494 while (i < menu_items_used)
2496 if (EQ (XVECTOR (menu_items)->contents[i], Qt))
2498 /* Create a new pane. */
2499 Lisp_Object pane_name, prefix;
2500 char *pane_string;
2502 pane_name = XVECTOR (menu_items)->contents[i + MENU_ITEMS_PANE_NAME];
2503 prefix = XVECTOR (menu_items)->contents[i + MENU_ITEMS_PANE_PREFIX];
2504 pane_string = (NILP (pane_name)
2505 ? "" : (char *) XSTRING (pane_name)->data);
2506 if (keymaps && !NILP (prefix))
2507 pane_string++;
2509 lpane = XMenuAddPane (FRAME_X_DISPLAY (f), menu, pane_string, TRUE);
2510 if (lpane == XM_FAILURE)
2512 XMenuDestroy (FRAME_X_DISPLAY (f), menu);
2513 *error = "Can't create pane";
2514 return Qnil;
2516 i += MENU_ITEMS_PANE_LENGTH;
2518 /* Find the width of the widest item in this pane. */
2519 maxwidth = 0;
2520 j = i;
2521 while (j < menu_items_used)
2523 Lisp_Object item;
2524 item = XVECTOR (menu_items)->contents[j];
2525 if (EQ (item, Qt))
2526 break;
2527 if (NILP (item))
2529 j++;
2530 continue;
2532 width = STRING_BYTES (XSTRING (item));
2533 if (width > maxwidth)
2534 maxwidth = width;
2536 j += MENU_ITEMS_ITEM_LENGTH;
2539 /* Ignore a nil in the item list.
2540 It's meaningful only for dialog boxes. */
2541 else if (EQ (XVECTOR (menu_items)->contents[i], Qquote))
2542 i += 1;
2543 else
2545 /* Create a new item within current pane. */
2546 Lisp_Object item_name, enable, descrip;
2547 unsigned char *item_data;
2549 item_name = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_NAME];
2550 enable = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_ENABLE];
2551 descrip
2552 = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_EQUIV_KEY];
2553 if (!NILP (descrip))
2555 int gap = maxwidth - STRING_BYTES (XSTRING (item_name));
2556 #ifdef C_ALLOCA
2557 Lisp_Object spacer;
2558 spacer = Fmake_string (make_number (gap), make_number (' '));
2559 item_name = concat2 (item_name, spacer);
2560 item_name = concat2 (item_name, descrip);
2561 item_data = XSTRING (item_name)->data;
2562 #else
2563 /* if alloca is fast, use that to make the space,
2564 to reduce gc needs. */
2565 item_data
2566 = (unsigned char *) alloca (maxwidth
2567 + STRING_BYTES (XSTRING (descrip)) + 1);
2568 bcopy (XSTRING (item_name)->data, item_data,
2569 STRING_BYTES (XSTRING (item_name)));
2570 for (j = XSTRING (item_name)->size; j < maxwidth; j++)
2571 item_data[j] = ' ';
2572 bcopy (XSTRING (descrip)->data, item_data + j,
2573 STRING_BYTES (XSTRING (descrip)));
2574 item_data[j + STRING_BYTES (XSTRING (descrip))] = 0;
2575 #endif
2577 else
2578 item_data = XSTRING (item_name)->data;
2580 if (XMenuAddSelection (FRAME_X_DISPLAY (f),
2581 menu, lpane, 0, item_data,
2582 !NILP (enable))
2583 == XM_FAILURE)
2585 XMenuDestroy (FRAME_X_DISPLAY (f), menu);
2586 *error = "Can't add selection to menu";
2587 return Qnil;
2589 i += MENU_ITEMS_ITEM_LENGTH;
2593 /* All set and ready to fly. */
2594 XMenuRecompute (FRAME_X_DISPLAY (f), menu);
2595 dispwidth = DisplayWidth (FRAME_X_DISPLAY (f),
2596 XScreenNumberOfScreen (FRAME_X_SCREEN (f)));
2597 dispheight = DisplayHeight (FRAME_X_DISPLAY (f),
2598 XScreenNumberOfScreen (FRAME_X_SCREEN (f)));
2599 x = min (x, dispwidth);
2600 y = min (y, dispheight);
2601 x = max (x, 1);
2602 y = max (y, 1);
2603 XMenuLocate (FRAME_X_DISPLAY (f), menu, 0, 0, x, y,
2604 &ulx, &uly, &width, &height);
2605 if (ulx+width > dispwidth)
2607 x -= (ulx + width) - dispwidth;
2608 ulx = dispwidth - width;
2610 if (uly+height > dispheight)
2612 y -= (uly + height) - dispheight;
2613 uly = dispheight - height;
2615 if (ulx < 0) x -= ulx;
2616 if (uly < 0) y -= uly;
2618 XMenuSetAEQ (menu, TRUE);
2619 XMenuSetFreeze (menu, TRUE);
2620 pane = selidx = 0;
2622 status = XMenuActivate (FRAME_X_DISPLAY (f), menu, &pane, &selidx,
2623 x, y, ButtonReleaseMask, &datap);
2626 #ifdef HAVE_X_WINDOWS
2627 /* Assume the mouse has moved out of the X window.
2628 If it has actually moved in, we will get an EnterNotify. */
2629 x_mouse_leave (FRAME_X_DISPLAY_INFO (f));
2630 #endif
2632 switch (status)
2634 case XM_SUCCESS:
2635 #ifdef XDEBUG
2636 fprintf (stderr, "pane= %d line = %d\n", panes, selidx);
2637 #endif
2639 /* Find the item number SELIDX in pane number PANE. */
2640 i = 0;
2641 while (i < menu_items_used)
2643 if (EQ (XVECTOR (menu_items)->contents[i], Qt))
2645 if (pane == 0)
2646 pane_prefix
2647 = XVECTOR (menu_items)->contents[i + MENU_ITEMS_PANE_PREFIX];
2648 pane--;
2649 i += MENU_ITEMS_PANE_LENGTH;
2651 else
2653 if (pane == -1)
2655 if (selidx == 0)
2657 entry
2658 = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_VALUE];
2659 if (keymaps != 0)
2661 entry = Fcons (entry, Qnil);
2662 if (!NILP (pane_prefix))
2663 entry = Fcons (pane_prefix, entry);
2665 break;
2667 selidx--;
2669 i += MENU_ITEMS_ITEM_LENGTH;
2672 break;
2674 case XM_FAILURE:
2675 *error = "Can't activate menu";
2676 case XM_IA_SELECT:
2677 case XM_NO_SELECT:
2678 entry = Qnil;
2679 break;
2681 XMenuDestroy (FRAME_X_DISPLAY (f), menu);
2683 #ifdef HAVE_X_WINDOWS
2684 /* State that no mouse buttons are now held.
2685 (The oldXMenu code doesn't track this info for us.)
2686 That is not necessarily true, but the fiction leads to reasonable
2687 results, and it is a pain to ask which are actually held now. */
2688 FRAME_X_DISPLAY_INFO (f)->grabbed = 0;
2689 #endif
2691 return entry;
2694 #endif /* not USE_X_TOOLKIT */
2696 #endif /* HAVE_MENUS */
2698 void
2699 syms_of_xmenu ()
2701 staticpro (&menu_items);
2702 menu_items = Qnil;
2704 Qdebug_on_next_call = intern ("debug-on-next-call");
2705 staticpro (&Qdebug_on_next_call);
2707 DEFVAR_LISP ("menu-updating-frame", &Vmenu_updating_frame,
2708 "Frame for which we are updating a menu.\n\
2709 The enable predicate for a menu command should check this variable.");
2710 Vmenu_updating_frame = Qnil;
2712 #ifdef USE_X_TOOLKIT
2713 widget_id_tick = (1<<16);
2714 next_menubar_widget_id = 1;
2715 #endif
2717 defsubr (&Sx_popup_menu);
2718 #ifdef HAVE_MENUS
2719 defsubr (&Sx_popup_dialog);
2720 #endif