(tmm-prompt): Delete tmm-add-prompt if we fail.
[emacs.git] / src / xmenu.c
blob8fd87175a1442dfc36534a7304641efe095b7810
1 /* X Communication module for terminals which understand the X protocol.
2 Copyright (C) 1986, 1988, 1993, 1994 Free Software Foundation, Inc.
4 This file is part of GNU Emacs.
6 GNU Emacs is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
9 any later version.
11 GNU Emacs is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU Emacs; see the file COPYING. If not, write to
18 the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
20 /* X pop-up deck-of-cards menu facility for gnuemacs.
22 * Written by Jon Arnold and Roman Budzianowski
23 * Mods and rewrite by Robert Krawitz
27 /* Modified by Fred Pierresteguy on December 93
28 to make the popup menus and menubar use the Xt. */
30 /* Rewritten for clarity and GC protection by rms in Feb 94. */
32 /* On 4.3 this loses if it comes after xterm.h. */
33 #include <signal.h>
34 #include <config.h>
36 #include <stdio.h>
37 #include "lisp.h"
38 #include "termhooks.h"
39 #include "frame.h"
40 #include "window.h"
41 #include "keyboard.h"
42 #include "blockinput.h"
43 #include "puresize.h"
44 #include "buffer.h"
46 #ifdef MSDOS
47 #include "msdos.h"
48 #endif
50 #ifdef HAVE_X_WINDOWS
51 /* This may include sys/types.h, and that somehow loses
52 if this is not done before the other system files. */
53 #include "xterm.h"
54 #endif
56 /* Load sys/types.h if not already loaded.
57 In some systems loading it twice is suicidal. */
58 #ifndef makedev
59 #include <sys/types.h>
60 #endif
62 #include "dispextern.h"
64 #ifdef HAVE_X_WINDOWS
65 #ifdef USE_X_TOOLKIT
66 #include <X11/Xlib.h>
67 #include <X11/IntrinsicP.h>
68 #include <X11/CoreP.h>
69 #include <X11/StringDefs.h>
70 #include <X11/Shell.h>
71 #ifdef USE_LUCID
72 #include <X11/Xaw/Paned.h>
73 #endif /* USE_LUCID */
74 #include "../lwlib/lwlib.h"
75 #else /* not USE_X_TOOLKIT */
76 #include "../oldXMenu/XMenu.h"
77 #endif /* not USE_X_TOOLKIT */
78 #endif /* HAVE_X_WINDOWS */
80 #define min(x,y) (((x) < (y)) ? (x) : (y))
81 #define max(x,y) (((x) > (y)) ? (x) : (y))
83 #ifndef TRUE
84 #define TRUE 1
85 #define FALSE 0
86 #endif /* no TRUE */
88 Lisp_Object Qdebug_on_next_call;
90 extern Lisp_Object Qmenu_enable;
91 extern Lisp_Object Qmenu_bar;
92 extern Lisp_Object Qmouse_click, Qevent_kind;
94 extern Lisp_Object Vdefine_key_rebound_commands;
96 extern Lisp_Object Voverriding_local_map;
97 extern Lisp_Object Voverriding_local_map_menu_flag;
99 extern Lisp_Object Qoverriding_local_map, Qoverriding_terminal_local_map;
101 extern Lisp_Object Qmenu_bar_update_hook;
103 #ifdef USE_X_TOOLKIT
104 extern void set_frame_menubar ();
105 extern void process_expose_from_menu ();
106 extern XtAppContext Xt_app_con;
108 static Lisp_Object xdialog_show ();
109 void popup_get_selection ();
110 #endif
112 static Lisp_Object xmenu_show ();
113 static void keymap_panes ();
114 static void single_keymap_panes ();
115 static void list_of_panes ();
116 static void list_of_items ();
118 /* This holds a Lisp vector that holds the results of decoding
119 the keymaps or alist-of-alists that specify a menu.
121 It describes the panes and items within the panes.
123 Each pane is described by 3 elements in the vector:
124 t, the pane name, the pane's prefix key.
125 Then follow the pane's items, with 5 elements per item:
126 the item string, the enable flag, the item's value,
127 the definition, and the equivalent keyboard key's description string.
129 In some cases, multiple levels of menus may be described.
130 A single vector slot containing nil indicates the start of a submenu.
131 A single vector slot containing lambda indicates the end of a submenu.
132 The submenu follows a menu item which is the way to reach the submenu.
134 A single vector slot containing quote indicates that the
135 following items should appear on the right of a dialog box.
137 Using a Lisp vector to hold this information while we decode it
138 takes care of protecting all the data from GC. */
140 #define MENU_ITEMS_PANE_NAME 1
141 #define MENU_ITEMS_PANE_PREFIX 2
142 #define MENU_ITEMS_PANE_LENGTH 3
144 #define MENU_ITEMS_ITEM_NAME 0
145 #define MENU_ITEMS_ITEM_ENABLE 1
146 #define MENU_ITEMS_ITEM_VALUE 2
147 #define MENU_ITEMS_ITEM_EQUIV_KEY 3
148 #define MENU_ITEMS_ITEM_DEFINITION 4
149 #define MENU_ITEMS_ITEM_LENGTH 5
151 static Lisp_Object menu_items;
153 /* Number of slots currently allocated in menu_items. */
154 static int menu_items_allocated;
156 /* This is the index in menu_items of the first empty slot. */
157 static int menu_items_used;
159 /* The number of panes currently recorded in menu_items,
160 excluding those within submenus. */
161 static int menu_items_n_panes;
163 /* Current depth within submenus. */
164 static int menu_items_submenu_depth;
166 /* Flag which when set indicates a dialog or menu has been posted by
167 Xt on behalf of one of the widget sets. */
168 static int popup_activated_flag;
170 static int next_menubar_widget_id;
172 #ifdef USE_X_TOOLKIT
174 /* Return the frame whose ->output_data.x->id equals ID, or 0 if none. */
176 static struct frame *
177 menubar_id_to_frame (id)
178 LWLIB_ID id;
180 Lisp_Object tail, frame;
181 FRAME_PTR f;
183 for (tail = Vframe_list; GC_CONSP (tail); tail = XCONS (tail)->cdr)
185 frame = XCONS (tail)->car;
186 if (!GC_FRAMEP (frame))
187 continue;
188 f = XFRAME (frame);
189 if (f->output_data.nothing == 1)
190 continue;
191 if (f->output_data.x->id == id)
192 return f;
194 return 0;
197 #endif
199 /* Initialize the menu_items structure if we haven't already done so.
200 Also mark it as currently empty. */
202 static void
203 init_menu_items ()
205 if (NILP (menu_items))
207 menu_items_allocated = 60;
208 menu_items = Fmake_vector (make_number (menu_items_allocated), Qnil);
211 menu_items_used = 0;
212 menu_items_n_panes = 0;
213 menu_items_submenu_depth = 0;
216 /* Call at the end of generating the data in menu_items.
217 This fills in the number of items in the last pane. */
219 static void
220 finish_menu_items ()
224 /* Call when finished using the data for the current menu
225 in menu_items. */
227 static void
228 discard_menu_items ()
230 /* Free the structure if it is especially large.
231 Otherwise, hold on to it, to save time. */
232 if (menu_items_allocated > 200)
234 menu_items = Qnil;
235 menu_items_allocated = 0;
239 /* Make the menu_items vector twice as large. */
241 static void
242 grow_menu_items ()
244 Lisp_Object old;
245 int old_size = menu_items_allocated;
246 old = menu_items;
248 menu_items_allocated *= 2;
249 menu_items = Fmake_vector (make_number (menu_items_allocated), Qnil);
250 bcopy (XVECTOR (old)->contents, XVECTOR (menu_items)->contents,
251 old_size * sizeof (Lisp_Object));
254 /* Begin a submenu. */
256 static void
257 push_submenu_start ()
259 if (menu_items_used + 1 > menu_items_allocated)
260 grow_menu_items ();
262 XVECTOR (menu_items)->contents[menu_items_used++] = Qnil;
263 menu_items_submenu_depth++;
266 /* End a submenu. */
268 static void
269 push_submenu_end ()
271 if (menu_items_used + 1 > menu_items_allocated)
272 grow_menu_items ();
274 XVECTOR (menu_items)->contents[menu_items_used++] = Qlambda;
275 menu_items_submenu_depth--;
278 /* Indicate boundary between left and right. */
280 static void
281 push_left_right_boundary ()
283 if (menu_items_used + 1 > menu_items_allocated)
284 grow_menu_items ();
286 XVECTOR (menu_items)->contents[menu_items_used++] = Qquote;
289 /* Start a new menu pane in menu_items..
290 NAME is the pane name. PREFIX_VEC is a prefix key for this pane. */
292 static void
293 push_menu_pane (name, prefix_vec)
294 Lisp_Object name, prefix_vec;
296 if (menu_items_used + MENU_ITEMS_PANE_LENGTH > menu_items_allocated)
297 grow_menu_items ();
299 if (menu_items_submenu_depth == 0)
300 menu_items_n_panes++;
301 XVECTOR (menu_items)->contents[menu_items_used++] = Qt;
302 XVECTOR (menu_items)->contents[menu_items_used++] = name;
303 XVECTOR (menu_items)->contents[menu_items_used++] = prefix_vec;
306 /* Push one menu item into the current pane.
307 NAME is the string to display. ENABLE if non-nil means
308 this item can be selected. KEY is the key generated by
309 choosing this item, or nil if this item doesn't really have a definition.
310 DEF is the definition of this item.
311 EQUIV is the textual description of the keyboard equivalent for
312 this item (or nil if none). */
314 static void
315 push_menu_item (name, enable, key, def, equiv)
316 Lisp_Object name, enable, key, def, equiv;
318 if (menu_items_used + MENU_ITEMS_ITEM_LENGTH > menu_items_allocated)
319 grow_menu_items ();
321 XVECTOR (menu_items)->contents[menu_items_used++] = name;
322 XVECTOR (menu_items)->contents[menu_items_used++] = enable;
323 XVECTOR (menu_items)->contents[menu_items_used++] = key;
324 XVECTOR (menu_items)->contents[menu_items_used++] = equiv;
325 XVECTOR (menu_items)->contents[menu_items_used++] = def;
328 /* Figure out the current keyboard equivalent of a menu item ITEM1.
329 The item string for menu display should be ITEM_STRING.
330 Store the equivalent keyboard key sequence's
331 textual description into *DESCRIP_PTR.
332 Also cache them in the item itself.
333 Return the real definition to execute. */
335 static Lisp_Object
336 menu_item_equiv_key (item_string, item1, descrip_ptr)
337 Lisp_Object item_string;
338 Lisp_Object item1;
339 Lisp_Object *descrip_ptr;
341 /* This is the real definition--the function to run. */
342 Lisp_Object def;
343 /* This is the sublist that records cached equiv key data
344 so we can save time. */
345 Lisp_Object cachelist;
346 /* These are the saved equivalent keyboard key sequence
347 and its key-description. */
348 Lisp_Object savedkey, descrip;
349 Lisp_Object def1;
350 int changed = 0;
351 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
353 /* If a help string follows the item string, skip it. */
354 if (CONSP (XCONS (item1)->cdr)
355 && STRINGP (XCONS (XCONS (item1)->cdr)->car))
356 item1 = XCONS (item1)->cdr;
358 def = Fcdr (item1);
360 /* Get out the saved equivalent-keyboard-key info. */
361 cachelist = savedkey = descrip = Qnil;
362 if (CONSP (def) && CONSP (XCONS (def)->car)
363 && (NILP (XCONS (XCONS (def)->car)->car)
364 || VECTORP (XCONS (XCONS (def)->car)->car)))
366 cachelist = XCONS (def)->car;
367 def = XCONS (def)->cdr;
368 savedkey = XCONS (cachelist)->car;
369 descrip = XCONS (cachelist)->cdr;
372 GCPRO4 (def, def1, savedkey, descrip);
374 /* Is it still valid? */
375 def1 = Qnil;
376 if (!NILP (savedkey))
377 def1 = Fkey_binding (savedkey, Qnil);
378 /* If not, update it. */
379 if (! EQ (def1, def)
380 /* If the command is an alias for another
381 (such as easymenu.el and lmenu.el set it up),
382 check if the original command matches the cached command. */
383 && !(SYMBOLP (def) && SYMBOLP (XSYMBOL (def)->function)
384 && EQ (def1, XSYMBOL (def)->function))
385 /* If something had no key binding before, don't recheck it
386 because that is too slow--except if we have a list of rebound
387 commands in Vdefine_key_rebound_commands, do recheck any command
388 that appears in that list. */
389 && (NILP (cachelist) || !NILP (savedkey)
390 || (! EQ (Qt, Vdefine_key_rebound_commands)
391 && !NILP (Fmemq (def, Vdefine_key_rebound_commands)))))
393 changed = 1;
394 descrip = Qnil;
395 /* If the command is an alias for another
396 (such as easymenu.el and lmenu.el set it up),
397 see if the original command name has equivalent keys. */
398 if (SYMBOLP (def) && SYMBOLP (XSYMBOL (def)->function))
399 savedkey = Fwhere_is_internal (XSYMBOL (def)->function,
400 Qnil, Qt, Qnil);
401 else
402 /* Otherwise look up the specified command itself.
403 We don't try both, because that makes easymenu menus slow. */
404 savedkey = Fwhere_is_internal (def, Qnil, Qt, Qnil);
406 if (!NILP (savedkey))
408 descrip = Fkey_description (savedkey);
409 descrip = concat2 (make_string (" (", 3), descrip);
410 descrip = concat2 (descrip, make_string (")", 1));
414 /* Cache the data we just got in a sublist of the menu binding. */
415 if (NILP (cachelist))
417 CHECK_IMPURE (item1);
418 XCONS (item1)->cdr = Fcons (Fcons (savedkey, descrip), def);
420 else if (changed)
422 XCONS (cachelist)->car = savedkey;
423 XCONS (cachelist)->cdr = descrip;
426 UNGCPRO;
427 *descrip_ptr = descrip;
428 return def;
431 /* This is used as the handler when calling internal_condition_case_1. */
433 static Lisp_Object
434 menu_item_enabled_p_1 (arg)
435 Lisp_Object arg;
437 /* If we got a quit from within the menu computation,
438 quit all the way out of it. This takes care of C-] in the debugger. */
439 if (CONSP (arg) && EQ (XCONS (arg)->car, Qquit))
440 Fsignal (Qquit, Qnil);
442 return Qnil;
445 /* Return non-nil if the command DEF is enabled when used as a menu item.
446 This is based on looking for a menu-enable property.
447 If NOTREAL is set, don't bother really computing this. */
449 static Lisp_Object
450 menu_item_enabled_p (def, notreal)
451 Lisp_Object def;
452 int notreal;
454 Lisp_Object enabled, tem;
456 enabled = Qt;
457 if (notreal)
458 return enabled;
459 if (SYMBOLP (def))
461 /* No property, or nil, means enable.
462 Otherwise, enable if value is not nil. */
463 tem = Fget (def, Qmenu_enable);
464 if (!NILP (tem))
465 /* (condition-case nil (eval tem)
466 (error nil)) */
467 enabled = internal_condition_case_1 (Feval, tem, Qerror,
468 menu_item_enabled_p_1);
470 return enabled;
473 /* Look through KEYMAPS, a vector of keymaps that is NMAPS long,
474 and generate menu panes for them in menu_items.
475 If NOTREAL is nonzero,
476 don't bother really computing whether an item is enabled. */
478 static void
479 keymap_panes (keymaps, nmaps, notreal)
480 Lisp_Object *keymaps;
481 int nmaps;
482 int notreal;
484 int mapno;
486 init_menu_items ();
488 /* Loop over the given keymaps, making a pane for each map.
489 But don't make a pane that is empty--ignore that map instead.
490 P is the number of panes we have made so far. */
491 for (mapno = 0; mapno < nmaps; mapno++)
492 single_keymap_panes (keymaps[mapno], Qnil, Qnil, notreal);
494 finish_menu_items ();
497 /* This is a recursive subroutine of keymap_panes.
498 It handles one keymap, KEYMAP.
499 The other arguments are passed along
500 or point to local variables of the previous function.
501 If NOTREAL is nonzero,
502 don't bother really computing whether an item is enabled. */
504 static void
505 single_keymap_panes (keymap, pane_name, prefix, notreal)
506 Lisp_Object keymap;
507 Lisp_Object pane_name;
508 Lisp_Object prefix;
509 int notreal;
511 Lisp_Object pending_maps;
512 Lisp_Object tail, item, item1, item_string, table;
513 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
515 pending_maps = Qnil;
517 push_menu_pane (pane_name, prefix);
519 for (tail = keymap; CONSP (tail); tail = XCONS (tail)->cdr)
521 /* Look at each key binding, and if it has a menu string,
522 make a menu item from it. */
523 item = XCONS (tail)->car;
524 if (CONSP (item))
526 item1 = XCONS (item)->cdr;
527 if (CONSP (item1))
529 item_string = XCONS (item1)->car;
530 if (STRINGP (item_string))
532 /* This is the real definition--the function to run. */
533 Lisp_Object def;
534 /* These are the saved equivalent keyboard key sequence
535 and its key-description. */
536 Lisp_Object descrip;
537 Lisp_Object tem, enabled;
539 /* GCPRO because ...enabled_p will call eval
540 and ..._equiv_key may autoload something.
541 Protecting KEYMAP preserves everything we use;
542 aside from that, must protect whatever might be
543 a string. Since there's no GCPRO5, we refetch
544 item_string instead of protecting it. */
545 descrip = def = Qnil;
546 GCPRO4 (keymap, pending_maps, def, descrip);
548 def = menu_item_equiv_key (item_string, item1, &descrip);
549 enabled = menu_item_enabled_p (def, notreal);
551 UNGCPRO;
553 item_string = XCONS (item1)->car;
555 tem = Fkeymapp (def);
556 if (XSTRING (item_string)->data[0] == '@' && !NILP (tem))
557 pending_maps = Fcons (Fcons (def, Fcons (item_string, XCONS (item)->car)),
558 pending_maps);
559 else
561 Lisp_Object submap;
562 GCPRO4 (keymap, pending_maps, descrip, item_string);
563 submap = get_keymap_1 (def, 0, 1);
564 UNGCPRO;
565 #ifndef USE_X_TOOLKIT
566 /* Indicate visually that this is a submenu. */
567 if (!NILP (submap))
568 item_string = concat2 (item_string,
569 build_string (" >"));
570 #endif
571 /* If definition is nil, pass nil as the key. */
572 push_menu_item (item_string, enabled,
573 XCONS (item)->car, def,
574 descrip);
575 #ifdef USE_X_TOOLKIT
576 /* Display a submenu using the toolkit. */
577 if (! NILP (submap))
579 push_submenu_start ();
580 single_keymap_panes (submap, Qnil,
581 XCONS (item)->car, notreal);
582 push_submenu_end ();
584 #endif
589 else if (VECTORP (item))
591 /* Loop over the char values represented in the vector. */
592 int len = XVECTOR (item)->size;
593 int c;
594 for (c = 0; c < len; c++)
596 Lisp_Object character;
597 XSETFASTINT (character, c);
598 item1 = XVECTOR (item)->contents[c];
599 if (CONSP (item1))
601 item_string = XCONS (item1)->car;
602 if (STRINGP (item_string))
604 Lisp_Object def;
606 /* These are the saved equivalent keyboard key sequence
607 and its key-description. */
608 Lisp_Object descrip;
609 Lisp_Object tem, enabled;
611 /* GCPRO because ...enabled_p will call eval
612 and ..._equiv_key may autoload something.
613 Protecting KEYMAP preserves everything we use;
614 aside from that, must protect whatever might be
615 a string. Since there's no GCPRO5, we refetch
616 item_string instead of protecting it. */
617 GCPRO4 (keymap, pending_maps, def, descrip);
618 descrip = def = Qnil;
620 def = menu_item_equiv_key (item_string, item1, &descrip);
621 enabled = menu_item_enabled_p (def, notreal);
623 UNGCPRO;
625 item_string = XCONS (item1)->car;
627 tem = Fkeymapp (def);
628 if (XSTRING (item_string)->data[0] == '@' && !NILP (tem))
629 pending_maps = Fcons (Fcons (def, Fcons (item_string, character)),
630 pending_maps);
631 else
633 Lisp_Object submap;
634 GCPRO4 (keymap, pending_maps, descrip, item_string);
635 submap = get_keymap_1 (def, 0, 1);
636 UNGCPRO;
637 #ifndef USE_X_TOOLKIT
638 if (!NILP (submap))
639 item_string = concat2 (item_string,
640 build_string (" >"));
641 #endif
642 /* If definition is nil, pass nil as the key. */
643 push_menu_item (item_string, enabled, character,
644 def, descrip);
645 #ifdef USE_X_TOOLKIT
646 if (! NILP (submap))
648 push_submenu_start ();
649 single_keymap_panes (submap, Qnil,
650 character, notreal);
651 push_submenu_end ();
653 #endif
661 /* Process now any submenus which want to be panes at this level. */
662 while (!NILP (pending_maps))
664 Lisp_Object elt, eltcdr, string;
665 elt = Fcar (pending_maps);
666 eltcdr = XCONS (elt)->cdr;
667 string = XCONS (eltcdr)->car;
668 /* We no longer discard the @ from the beginning of the string here.
669 Instead, we do this in xmenu_show. */
670 single_keymap_panes (Fcar (elt), string,
671 XCONS (eltcdr)->cdr, notreal);
672 pending_maps = Fcdr (pending_maps);
676 /* Push all the panes and items of a menu decsribed by the
677 alist-of-alists MENU.
678 This handles old-fashioned calls to x-popup-menu. */
680 static void
681 list_of_panes (menu)
682 Lisp_Object menu;
684 Lisp_Object tail;
686 init_menu_items ();
688 for (tail = menu; !NILP (tail); tail = Fcdr (tail))
690 Lisp_Object elt, pane_name, pane_data;
691 elt = Fcar (tail);
692 pane_name = Fcar (elt);
693 CHECK_STRING (pane_name, 0);
694 push_menu_pane (pane_name, Qnil);
695 pane_data = Fcdr (elt);
696 CHECK_CONS (pane_data, 0);
697 list_of_items (pane_data);
700 finish_menu_items ();
703 /* Push the items in a single pane defined by the alist PANE. */
705 static void
706 list_of_items (pane)
707 Lisp_Object pane;
709 Lisp_Object tail, item, item1;
711 for (tail = pane; !NILP (tail); tail = Fcdr (tail))
713 item = Fcar (tail);
714 if (STRINGP (item))
715 push_menu_item (item, Qnil, Qnil, Qt, Qnil);
716 else if (NILP (item))
717 push_left_right_boundary ();
718 else
720 CHECK_CONS (item, 0);
721 item1 = Fcar (item);
722 CHECK_STRING (item1, 1);
723 push_menu_item (item1, Qt, Fcdr (item), Qt, Qnil);
728 DEFUN ("x-popup-menu", Fx_popup_menu, Sx_popup_menu, 2, 2, 0,
729 "Pop up a deck-of-cards menu and return user's selection.\n\
730 POSITION is a position specification. This is either a mouse button event\n\
731 or a list ((XOFFSET YOFFSET) WINDOW)\n\
732 where XOFFSET and YOFFSET are positions in pixels from the top left\n\
733 corner of WINDOW's frame. (WINDOW may be a frame object instead of a window.)\n\
734 This controls the position of the center of the first line\n\
735 in the first pane of the menu, not the top left of the menu as a whole.\n\
736 If POSITION is t, it means to use the current mouse position.\n\
738 MENU is a specifier for a menu. For the simplest case, MENU is a keymap.\n\
739 The menu items come from key bindings that have a menu string as well as\n\
740 a definition; actually, the \"definition\" in such a key binding looks like\n\
741 \(STRING . REAL-DEFINITION). To give the menu a title, put a string into\n\
742 the keymap as a top-level element.\n\n\
743 You can also use a list of keymaps as MENU.\n\
744 Then each keymap makes a separate pane.\n\
745 When MENU is a keymap or a list of keymaps, the return value\n\
746 is a list of events.\n\n\
747 Alternatively, you can specify a menu of multiple panes\n\
748 with a list of the form (TITLE PANE1 PANE2...),\n\
749 where each pane is a list of form (TITLE ITEM1 ITEM2...).\n\
750 Each ITEM is normally a cons cell (STRING . VALUE);\n\
751 but a string can appear as an item--that makes a nonselectable line\n\
752 in the menu.\n\
753 With this form of menu, the return value is VALUE from the chosen item.\n\
755 If POSITION is nil, don't display the menu at all, just precalculate the\n\
756 cached information about equivalent key sequences.")
757 (position, menu)
758 Lisp_Object position, menu;
760 int number_of_panes, panes;
761 Lisp_Object keymap, tem;
762 int xpos, ypos;
763 Lisp_Object title;
764 char *error_name;
765 Lisp_Object selection;
766 int i, j;
767 FRAME_PTR f;
768 Lisp_Object x, y, window;
769 int keymaps = 0;
770 int for_click = 0;
771 struct gcpro gcpro1;
773 if (! NILP (position))
775 check_x ();
777 /* Decode the first argument: find the window and the coordinates. */
778 if (EQ (position, Qt)
779 || (CONSP (position) && EQ (XCONS (position)->car, Qmenu_bar)))
781 /* Use the mouse's current position. */
782 FRAME_PTR new_f = selected_frame;
783 Lisp_Object bar_window;
784 int part;
785 unsigned long time;
787 if (mouse_position_hook)
788 (*mouse_position_hook) (&new_f, 1, &bar_window,
789 &part, &x, &y, &time);
790 if (new_f != 0)
791 XSETFRAME (window, new_f);
792 else
794 window = selected_window;
795 XSETFASTINT (x, 0);
796 XSETFASTINT (y, 0);
799 else
801 tem = Fcar (position);
802 if (CONSP (tem))
804 window = Fcar (Fcdr (position));
805 x = Fcar (tem);
806 y = Fcar (Fcdr (tem));
808 else
810 for_click = 1;
811 tem = Fcar (Fcdr (position)); /* EVENT_START (position) */
812 window = Fcar (tem); /* POSN_WINDOW (tem) */
813 tem = Fcar (Fcdr (Fcdr (tem))); /* POSN_WINDOW_POSN (tem) */
814 x = Fcar (tem);
815 y = Fcdr (tem);
819 CHECK_NUMBER (x, 0);
820 CHECK_NUMBER (y, 0);
822 /* Decode where to put the menu. */
824 if (FRAMEP (window))
826 f = XFRAME (window);
827 xpos = 0;
828 ypos = 0;
830 else if (WINDOWP (window))
832 CHECK_LIVE_WINDOW (window, 0);
833 f = XFRAME (WINDOW_FRAME (XWINDOW (window)));
835 xpos = (FONT_WIDTH (f->output_data.x->font) * XWINDOW (window)->left);
836 ypos = (f->output_data.x->line_height * XWINDOW (window)->top);
838 else
839 /* ??? Not really clean; should be CHECK_WINDOW_OR_FRAME,
840 but I don't want to make one now. */
841 CHECK_WINDOW (window, 0);
843 xpos += XINT (x);
844 ypos += XINT (y);
847 title = Qnil;
848 GCPRO1 (title);
850 /* Decode the menu items from what was specified. */
852 keymap = Fkeymapp (menu);
853 tem = Qnil;
854 if (CONSP (menu))
855 tem = Fkeymapp (Fcar (menu));
856 if (!NILP (keymap))
858 /* We were given a keymap. Extract menu info from the keymap. */
859 Lisp_Object prompt;
860 keymap = get_keymap (menu);
862 /* Extract the detailed info to make one pane. */
863 keymap_panes (&menu, 1, NILP (position));
865 /* Search for a string appearing directly as an element of the keymap.
866 That string is the title of the menu. */
867 prompt = map_prompt (keymap);
869 /* Make that be the pane title of the first pane. */
870 if (!NILP (prompt) && menu_items_n_panes >= 0)
871 XVECTOR (menu_items)->contents[MENU_ITEMS_PANE_NAME] = prompt;
873 keymaps = 1;
875 else if (!NILP (tem))
877 /* We were given a list of keymaps. */
878 int nmaps = XFASTINT (Flength (menu));
879 Lisp_Object *maps
880 = (Lisp_Object *) alloca (nmaps * sizeof (Lisp_Object));
881 int i;
883 title = Qnil;
885 /* The first keymap that has a prompt string
886 supplies the menu title. */
887 for (tem = menu, i = 0; CONSP (tem); tem = Fcdr (tem))
889 Lisp_Object prompt;
891 maps[i++] = keymap = get_keymap (Fcar (tem));
893 prompt = map_prompt (keymap);
894 if (NILP (title) && !NILP (prompt))
895 title = prompt;
898 /* Extract the detailed info to make one pane. */
899 keymap_panes (maps, nmaps, NILP (position));
901 /* Make the title be the pane title of the first pane. */
902 if (!NILP (title) && menu_items_n_panes >= 0)
903 XVECTOR (menu_items)->contents[MENU_ITEMS_PANE_NAME] = title;
905 keymaps = 1;
907 else
909 /* We were given an old-fashioned menu. */
910 title = Fcar (menu);
911 CHECK_STRING (title, 1);
913 list_of_panes (Fcdr (menu));
915 keymaps = 0;
918 if (NILP (position))
920 discard_menu_items ();
921 UNGCPRO;
922 return Qnil;
925 /* Display them in a menu. */
926 BLOCK_INPUT;
928 selection = xmenu_show (f, xpos, ypos, for_click,
929 keymaps, title, &error_name);
930 UNBLOCK_INPUT;
932 discard_menu_items ();
934 UNGCPRO;
936 if (error_name) error (error_name);
937 return selection;
940 DEFUN ("x-popup-dialog", Fx_popup_dialog, Sx_popup_dialog, 2, 2, 0,
941 "Pop up a dialog box and return user's selection.\n\
942 POSITION specifies which frame to use.\n\
943 This is normally a mouse button event or a window or frame.\n\
944 If POSITION is t, it means to use the frame the mouse is on.\n\
945 The dialog box appears in the middle of the specified frame.\n\
947 CONTENTS specifies the alternatives to display in the dialog box.\n\
948 It is a list of the form (TITLE ITEM1 ITEM2...).\n\
949 Each ITEM is a cons cell (STRING . VALUE).\n\
950 The return value is VALUE from the chosen item.\n\n\
951 An ITEM may also be just a string--that makes a nonselectable item.\n\
952 An ITEM may also be nil--that means to put all preceding items\n\
953 on the left of the dialog box and all following items on the right.\n\
954 \(By default, approximately half appear on each side.)")
955 (position, contents)
956 Lisp_Object position, contents;
958 FRAME_PTR f;
959 Lisp_Object window;
961 check_x ();
963 /* Decode the first argument: find the window or frame to use. */
964 if (EQ (position, Qt)
965 || (CONSP (position) && EQ (XCONS (position)->car, Qmenu_bar)))
967 #if 0 /* Using the frame the mouse is on may not be right. */
968 /* Use the mouse's current position. */
969 FRAME_PTR new_f = selected_frame;
970 Lisp_Object bar_window;
971 int part;
972 unsigned long time;
973 Lisp_Object x, y;
975 (*mouse_position_hook) (&new_f, 1, &bar_window, &part, &x, &y, &time);
977 if (new_f != 0)
978 XSETFRAME (window, new_f);
979 else
980 window = selected_window;
981 #endif
982 window = selected_window;
984 else if (CONSP (position))
986 Lisp_Object tem;
987 tem = Fcar (position);
988 if (CONSP (tem))
989 window = Fcar (Fcdr (position));
990 else
992 tem = Fcar (Fcdr (position)); /* EVENT_START (position) */
993 window = Fcar (tem); /* POSN_WINDOW (tem) */
996 else if (WINDOWP (position) || FRAMEP (position))
997 window = position;
999 /* Decode where to put the menu. */
1001 if (FRAMEP (window))
1002 f = XFRAME (window);
1003 else if (WINDOWP (window))
1005 CHECK_LIVE_WINDOW (window, 0);
1006 f = XFRAME (WINDOW_FRAME (XWINDOW (window)));
1008 else
1009 /* ??? Not really clean; should be CHECK_WINDOW_OR_FRAME,
1010 but I don't want to make one now. */
1011 CHECK_WINDOW (window, 0);
1013 #ifndef USE_X_TOOLKIT
1014 /* Display a menu with these alternatives
1015 in the middle of frame F. */
1017 Lisp_Object x, y, frame, newpos;
1018 XSETFRAME (frame, f);
1019 XSETINT (x, x_pixel_width (f) / 2);
1020 XSETINT (y, x_pixel_height (f) / 2);
1021 newpos = Fcons (Fcons (x, Fcons (y, Qnil)), Fcons (frame, Qnil));
1023 return Fx_popup_menu (newpos,
1024 Fcons (Fcar (contents), Fcons (contents, Qnil)));
1026 #else
1028 Lisp_Object title;
1029 char *error_name;
1030 Lisp_Object selection;
1032 /* Decode the dialog items from what was specified. */
1033 title = Fcar (contents);
1034 CHECK_STRING (title, 1);
1036 list_of_panes (Fcons (contents, Qnil));
1038 /* Display them in a dialog box. */
1039 BLOCK_INPUT;
1040 selection = xdialog_show (f, 0, title, &error_name);
1041 UNBLOCK_INPUT;
1043 discard_menu_items ();
1045 if (error_name) error (error_name);
1046 return selection;
1048 #endif
1051 #ifdef USE_X_TOOLKIT
1053 /* Loop in Xt until the menu pulldown or dialog popup has been
1054 popped down (deactivated).
1056 NOTE: All calls to popup_get_selection should be protected
1057 with BLOCK_INPUT, UNBLOCK_INPUT wrappers. */
1059 void
1060 popup_get_selection (initial_event, dpyinfo, id)
1061 XEvent *initial_event;
1062 struct x_display_info *dpyinfo;
1063 LWLIB_ID id;
1065 XEvent event;
1067 /* Define a queue to save up for later unreading
1068 all X events that don't pertain to the menu. */
1069 struct event_queue
1071 XEvent event;
1072 struct event_queue *next;
1075 struct event_queue *queue = NULL;
1076 struct event_queue *queue_tmp;
1078 if (initial_event)
1079 event = *initial_event;
1080 else
1081 XtAppNextEvent (Xt_app_con, &event);
1083 while (1)
1085 /* Handle expose events for editor frames right away. */
1086 if (event.type == Expose)
1087 process_expose_from_menu (event);
1088 /* Make sure we don't consider buttons grabbed after menu goes. */
1089 else if (event.type == ButtonRelease
1090 && dpyinfo->display == event.xbutton.display)
1091 dpyinfo->grabbed &= ~(1 << event.xbutton.button);
1092 /* If the user presses a key, deactivate the menu.
1093 The user is likely to do that if we get wedged. */
1094 else if (event.type == KeyPress
1095 && dpyinfo->display == event.xbutton.display)
1097 popup_activated_flag = 0;
1098 break;
1100 /* Button presses outside the menu also pop it down. */
1101 else if (event.type == ButtonPress
1102 && event.xany.display == dpyinfo->display
1103 && x_any_window_to_frame (dpyinfo, event.xany.window))
1105 popup_activated_flag = 0;
1106 break;
1109 /* Queue all events not for this popup,
1110 except for Expose, which we've already handled.
1111 Note that the X window is associated with the frame if this
1112 is a menu bar popup, but not if it's a dialog box. So we use
1113 x_non_menubar_window_to_frame, not x_any_window_to_frame. */
1114 if (event.type != Expose
1115 && (event.xany.display != dpyinfo->display
1116 || x_non_menubar_window_to_frame (dpyinfo, event.xany.window)))
1118 queue_tmp = (struct event_queue *) malloc (sizeof (struct event_queue));
1120 if (queue_tmp != NULL)
1122 queue_tmp->event = event;
1123 queue_tmp->next = queue;
1124 queue = queue_tmp;
1127 else
1128 XtDispatchEvent (&event);
1130 if (!popup_activated ())
1131 break;
1132 XtAppNextEvent (Xt_app_con, &event);
1135 /* Unread any events that we got but did not handle. */
1136 while (queue != NULL)
1138 queue_tmp = queue;
1139 XPutBackEvent (queue_tmp->event.xany.display, &queue_tmp->event);
1140 queue = queue_tmp->next;
1141 free ((char *)queue_tmp);
1142 /* Cause these events to get read as soon as we UNBLOCK_INPUT. */
1143 interrupt_input_pending = 1;
1147 /* Activate the menu bar of frame F.
1148 This is called from keyboard.c when it gets the
1149 menu_bar_activate_event out of the Emacs event queue.
1151 To activate the menu bar, we use the X button-press event
1152 that was saved in saved_button_event.
1153 That makes the toolkit do its thing.
1155 But first we recompute the menu bar contents (the whole tree).
1157 The reason for saving the button event until here, instead of
1158 passing it to the toolkit right away, is that we can safely
1159 execute Lisp code. */
1161 x_activate_menubar (f)
1162 FRAME_PTR f;
1164 if (f->output_data.x->saved_button_event->type != ButtonPress)
1165 return;
1167 set_frame_menubar (f, 0, 1);
1169 BLOCK_INPUT;
1170 XtDispatchEvent ((XEvent *) f->output_data.x->saved_button_event);
1171 UNBLOCK_INPUT;
1173 /* Ignore this if we get it a second time. */
1174 f->output_data.x->saved_button_event->type = 0;
1177 /* Detect if a dialog or menu has been posted. */
1180 popup_activated ()
1182 return popup_activated_flag;
1186 /* This callback is invoked when the user selects a menubar cascade
1187 pushbutton, but before the pulldown menu is posted. */
1189 static void
1190 popup_activate_callback (widget, id, client_data)
1191 Widget widget;
1192 LWLIB_ID id;
1193 XtPointer client_data;
1195 popup_activated_flag = 1;
1198 /* This callback is called from the menu bar pulldown menu
1199 when the user makes a selection.
1200 Figure out what the user chose
1201 and put the appropriate events into the keyboard buffer. */
1203 static void
1204 menubar_selection_callback (widget, id, client_data)
1205 Widget widget;
1206 LWLIB_ID id;
1207 XtPointer client_data;
1209 Lisp_Object prefix, entry;
1210 FRAME_PTR f = menubar_id_to_frame (id);
1211 Lisp_Object vector;
1212 Lisp_Object *subprefix_stack;
1213 int submenu_depth = 0;
1214 int i;
1216 if (!f)
1217 return;
1218 subprefix_stack = (Lisp_Object *) alloca (f->menu_bar_items_used * sizeof (Lisp_Object));
1219 vector = f->menu_bar_vector;
1220 prefix = Qnil;
1221 i = 0;
1222 while (i < f->menu_bar_items_used)
1224 if (EQ (XVECTOR (vector)->contents[i], Qnil))
1226 subprefix_stack[submenu_depth++] = prefix;
1227 prefix = entry;
1228 i++;
1230 else if (EQ (XVECTOR (vector)->contents[i], Qlambda))
1232 prefix = subprefix_stack[--submenu_depth];
1233 i++;
1235 else if (EQ (XVECTOR (vector)->contents[i], Qt))
1237 prefix = XVECTOR (vector)->contents[i + MENU_ITEMS_PANE_PREFIX];
1238 i += MENU_ITEMS_PANE_LENGTH;
1240 else
1242 entry = XVECTOR (vector)->contents[i + MENU_ITEMS_ITEM_VALUE];
1243 /* The EMACS_INT cast avoids a warning. There's no problem
1244 as long as pointers have enough bits to hold small integers. */
1245 if ((int) (EMACS_INT) client_data == i)
1247 int j;
1248 struct input_event buf;
1249 Lisp_Object frame;
1251 XSETFRAME (frame, f);
1252 buf.kind = menu_bar_event;
1253 buf.frame_or_window = Fcons (frame, Fcons (Qmenu_bar, Qnil));
1254 kbd_buffer_store_event (&buf);
1256 for (j = 0; j < submenu_depth; j++)
1257 if (!NILP (subprefix_stack[j]))
1259 buf.kind = menu_bar_event;
1260 buf.frame_or_window = Fcons (frame, subprefix_stack[j]);
1261 kbd_buffer_store_event (&buf);
1264 if (!NILP (prefix))
1266 buf.kind = menu_bar_event;
1267 buf.frame_or_window = Fcons (frame, prefix);
1268 kbd_buffer_store_event (&buf);
1271 buf.kind = menu_bar_event;
1272 buf.frame_or_window = Fcons (frame, entry);
1273 kbd_buffer_store_event (&buf);
1275 return;
1277 i += MENU_ITEMS_ITEM_LENGTH;
1282 /* This callback is invoked when a dialog or menu is finished being
1283 used and has been unposted. */
1285 static void
1286 popup_deactivate_callback (widget, id, client_data)
1287 Widget widget;
1288 LWLIB_ID id;
1289 XtPointer client_data;
1291 popup_activated_flag = 0;
1295 /* This recursively calls free_widget_value on the tree of widgets.
1296 It must free all data that was malloc'ed for these widget_values.
1297 In Emacs, many slots are pointers into the data of Lisp_Strings, and
1298 must be left alone. */
1300 void
1301 free_menubar_widget_value_tree (wv)
1302 widget_value *wv;
1304 if (! wv) return;
1306 wv->name = wv->value = wv->key = (char *) 0xDEADBEEF;
1308 if (wv->contents && (wv->contents != (widget_value*)1))
1310 free_menubar_widget_value_tree (wv->contents);
1311 wv->contents = (widget_value *) 0xDEADBEEF;
1313 if (wv->next)
1315 free_menubar_widget_value_tree (wv->next);
1316 wv->next = (widget_value *) 0xDEADBEEF;
1318 BLOCK_INPUT;
1319 free_widget_value (wv);
1320 UNBLOCK_INPUT;
1323 /* Return a tree of widget_value structures for a menu bar item
1324 whose event type is ITEM_KEY (with string ITEM_NAME)
1325 and whose contents come from the list of keymaps MAPS. */
1327 static widget_value *
1328 single_submenu (item_key, item_name, maps)
1329 Lisp_Object item_key, item_name, maps;
1331 widget_value *wv, *prev_wv, *save_wv, *first_wv;
1332 int i;
1333 int submenu_depth = 0;
1334 Lisp_Object length;
1335 int len;
1336 Lisp_Object *mapvec;
1337 widget_value **submenu_stack;
1338 int mapno;
1339 int previous_items = menu_items_used;
1340 int top_level_items = 0;
1342 length = Flength (maps);
1343 len = XINT (length);
1345 /* Convert the list MAPS into a vector MAPVEC. */
1346 mapvec = (Lisp_Object *) alloca (len * sizeof (Lisp_Object));
1347 for (i = 0; i < len; i++)
1349 mapvec[i] = Fcar (maps);
1350 maps = Fcdr (maps);
1353 menu_items_n_panes = 0;
1355 /* Loop over the given keymaps, making a pane for each map.
1356 But don't make a pane that is empty--ignore that map instead. */
1357 for (i = 0; i < len; i++)
1359 if (SYMBOLP (mapvec[i]))
1361 top_level_items = 1;
1362 push_menu_pane (Qnil, Qnil);
1363 push_menu_item (item_name, Qt, item_key, mapvec[i], Qnil);
1365 else
1366 single_keymap_panes (mapvec[i], item_name, item_key, 0);
1369 /* Create a tree of widget_value objects
1370 representing the panes and their items. */
1372 submenu_stack
1373 = (widget_value **) alloca (menu_items_used * sizeof (widget_value *));
1374 wv = malloc_widget_value ();
1375 wv->name = "menu";
1376 wv->value = 0;
1377 wv->enabled = 1;
1378 first_wv = wv;
1379 save_wv = 0;
1380 prev_wv = 0;
1382 /* Loop over all panes and items made during this call
1383 and construct a tree of widget_value objects.
1384 Ignore the panes and items made by previous calls to
1385 single_submenu, even though those are also in menu_items. */
1386 i = previous_items;
1387 while (i < menu_items_used)
1389 if (EQ (XVECTOR (menu_items)->contents[i], Qnil))
1391 submenu_stack[submenu_depth++] = save_wv;
1392 save_wv = prev_wv;
1393 prev_wv = 0;
1394 i++;
1396 else if (EQ (XVECTOR (menu_items)->contents[i], Qlambda))
1398 prev_wv = save_wv;
1399 save_wv = submenu_stack[--submenu_depth];
1400 i++;
1402 else if (EQ (XVECTOR (menu_items)->contents[i], Qt)
1403 && submenu_depth != 0)
1404 i += MENU_ITEMS_PANE_LENGTH;
1405 /* Ignore a nil in the item list.
1406 It's meaningful only for dialog boxes. */
1407 else if (EQ (XVECTOR (menu_items)->contents[i], Qquote))
1408 i += 1;
1409 else if (EQ (XVECTOR (menu_items)->contents[i], Qt))
1411 /* Create a new pane. */
1412 Lisp_Object pane_name, prefix;
1413 char *pane_string;
1414 pane_name = XVECTOR (menu_items)->contents[i + MENU_ITEMS_PANE_NAME];
1415 prefix = XVECTOR (menu_items)->contents[i + MENU_ITEMS_PANE_PREFIX];
1416 pane_string = (NILP (pane_name)
1417 ? "" : (char *) XSTRING (pane_name)->data);
1418 /* If there is just one top-level pane, put all its items directly
1419 under the top-level menu. */
1420 if (menu_items_n_panes == 1)
1421 pane_string = "";
1423 /* If the pane has a meaningful name,
1424 make the pane a top-level menu item
1425 with its items as a submenu beneath it. */
1426 if (strcmp (pane_string, ""))
1428 wv = malloc_widget_value ();
1429 if (save_wv)
1430 save_wv->next = wv;
1431 else
1432 first_wv->contents = wv;
1433 wv->name = pane_string;
1434 /* Ignore the @ that means "separate pane".
1435 This is a kludge, but this isn't worth more time. */
1436 if (!NILP (prefix) && wv->name[0] == '@')
1437 wv->name++;
1438 wv->value = 0;
1439 wv->enabled = 1;
1441 save_wv = wv;
1442 prev_wv = 0;
1443 i += MENU_ITEMS_PANE_LENGTH;
1445 else
1447 /* Create a new item within current pane. */
1448 Lisp_Object item_name, enable, descrip, def;
1449 item_name = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_NAME];
1450 enable = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_ENABLE];
1451 descrip
1452 = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_EQUIV_KEY];
1453 def = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_DEFINITION];
1455 wv = malloc_widget_value ();
1456 if (prev_wv)
1457 prev_wv->next = wv;
1458 else
1459 save_wv->contents = wv;
1461 wv->name = (char *) XSTRING (item_name)->data;
1462 if (!NILP (descrip))
1463 wv->key = (char *) XSTRING (descrip)->data;
1464 wv->value = 0;
1465 /* The EMACS_INT cast avoids a warning. There's no problem
1466 as long as pointers have enough bits to hold small integers. */
1467 wv->call_data = (!NILP (def) ? (void *) (EMACS_INT) i : 0);
1468 wv->enabled = !NILP (enable);
1469 prev_wv = wv;
1471 i += MENU_ITEMS_ITEM_LENGTH;
1475 /* If we have just one "menu item"
1476 that was originally a button, return it by itself. */
1477 if (top_level_items && first_wv->contents && first_wv->contents->next == 0)
1479 wv = first_wv->contents;
1480 free_widget_value (first_wv);
1481 return wv;
1484 return first_wv;
1487 extern void EmacsFrameSetCharSize ();
1489 /* Recompute all the widgets of frame F, when the menu bar
1490 has been changed. */
1492 static void
1493 update_frame_menubar (f)
1494 FRAME_PTR f;
1496 struct x_output *x = f->output_data.x;
1497 int columns, rows;
1498 int menubar_changed;
1500 Dimension shell_height;
1502 /* We assume the menubar contents has changed if the global flag is set,
1503 or if the current buffer has changed, or if the menubar has never
1504 been updated before.
1506 menubar_changed = (x->menubar_widget
1507 && !XtIsManaged (x->menubar_widget));
1509 if (! (menubar_changed))
1510 return;
1512 BLOCK_INPUT;
1513 /* Save the size of the frame because the pane widget doesn't accept to
1514 resize itself. So force it. */
1515 columns = f->width;
1516 rows = f->height;
1518 /* Do the voodoo which means "I'm changing lots of things, don't try to
1519 refigure sizes until I'm done." */
1520 lw_refigure_widget (x->column_widget, False);
1522 /* the order in which children are managed is the top to
1523 bottom order in which they are displayed in the paned window.
1524 First, remove the text-area widget.
1526 XtUnmanageChild (x->edit_widget);
1528 /* remove the menubar that is there now, and put up the menubar that
1529 should be there.
1531 if (menubar_changed)
1533 XtManageChild (x->menubar_widget);
1534 XtMapWidget (x->menubar_widget);
1535 XtVaSetValues (x->menubar_widget, XtNmappedWhenManaged, 1, 0);
1538 /* Re-manage the text-area widget, and then thrash the sizes. */
1539 XtManageChild (x->edit_widget);
1540 lw_refigure_widget (x->column_widget, True);
1542 /* Force the pane widget to resize itself with the right values. */
1543 EmacsFrameSetCharSize (x->edit_widget, columns, rows);
1545 UNBLOCK_INPUT;
1548 /* Set the contents of the menubar widgets of frame F.
1549 The argument FIRST_TIME is currently ignored;
1550 it is set the first time this is called, from initialize_frame_menubar. */
1552 void
1553 set_frame_menubar (f, first_time, deep_p)
1554 FRAME_PTR f;
1555 int first_time;
1556 int deep_p;
1558 Widget menubar_widget = f->output_data.x->menubar_widget;
1559 Lisp_Object tail, items, frame;
1560 widget_value *wv, *first_wv, *prev_wv = 0;
1561 int i;
1562 LWLIB_ID id;
1564 if (f->output_data.x->id == 0)
1565 f->output_data.x->id = next_menubar_widget_id++;
1566 id = f->output_data.x->id;
1568 if (! menubar_widget)
1569 deep_p = 1;
1571 wv = malloc_widget_value ();
1572 wv->name = "menubar";
1573 wv->value = 0;
1574 wv->enabled = 1;
1575 first_wv = wv;
1577 if (deep_p)
1579 /* Make a widget-value tree representing the entire menu trees. */
1581 struct buffer *prev = current_buffer;
1582 Lisp_Object buffer;
1583 int specpdl_count = specpdl_ptr - specpdl;
1584 int previous_menu_items_used = f->menu_bar_items_used;
1585 Lisp_Object *previous_items
1586 = (Lisp_Object *) alloca (previous_menu_items_used
1587 * sizeof (Lisp_Object));
1589 buffer = XWINDOW (FRAME_SELECTED_WINDOW (f))->buffer;
1590 specbind (Qinhibit_quit, Qt);
1591 /* Don't let the debugger step into this code
1592 because it is not reentrant. */
1593 specbind (Qdebug_on_next_call, Qnil);
1595 record_unwind_protect (Fstore_match_data, Fmatch_data ());
1596 if (NILP (Voverriding_local_map_menu_flag))
1598 specbind (Qoverriding_terminal_local_map, Qnil);
1599 specbind (Qoverriding_local_map, Qnil);
1602 set_buffer_internal_1 (XBUFFER (buffer));
1604 /* Run the Lucid hook. */
1605 call1 (Vrun_hooks, Qactivate_menubar_hook);
1606 /* If it has changed current-menubar from previous value,
1607 really recompute the menubar from the value. */
1608 if (! NILP (Vlucid_menu_bar_dirty_flag))
1609 call0 (Qrecompute_lucid_menubar);
1610 call1 (Vrun_hooks, Qmenu_bar_update_hook);
1611 FRAME_MENU_BAR_ITEMS (f) = menu_bar_items (FRAME_MENU_BAR_ITEMS (f));
1613 items = FRAME_MENU_BAR_ITEMS (f);
1615 inhibit_garbage_collection ();
1617 /* Save the frame's previous menu bar contents data. */
1618 bcopy (XVECTOR (f->menu_bar_vector)->contents, previous_items,
1619 previous_menu_items_used * sizeof (Lisp_Object));
1621 /* Fill in the current menu bar contents. */
1622 menu_items = f->menu_bar_vector;
1623 menu_items_allocated = XVECTOR (menu_items)->size;
1624 init_menu_items ();
1625 for (i = 0; i < XVECTOR (items)->size; i += 3)
1627 Lisp_Object key, string, maps;
1629 key = XVECTOR (items)->contents[i];
1630 string = XVECTOR (items)->contents[i + 1];
1631 maps = XVECTOR (items)->contents[i + 2];
1632 if (NILP (string))
1633 break;
1635 wv = single_submenu (key, string, maps);
1636 if (prev_wv)
1637 prev_wv->next = wv;
1638 else
1639 first_wv->contents = wv;
1640 /* Don't set wv->name here; GC during the loop might relocate it. */
1641 wv->enabled = 1;
1642 prev_wv = wv;
1645 finish_menu_items ();
1647 set_buffer_internal_1 (prev);
1648 unbind_to (specpdl_count, Qnil);
1650 /* If there has been no change in the Lisp-level contents
1651 of the menu bar, skip redisplaying it. Just exit. */
1653 for (i = 0; i < previous_menu_items_used; i++)
1654 if (menu_items_used == i
1655 || (previous_items[i] != XVECTOR (menu_items)->contents[i]))
1656 break;
1657 if (i == menu_items_used && i == previous_menu_items_used)
1659 free_menubar_widget_value_tree (first_wv);
1660 menu_items = Qnil;
1662 return;
1665 /* Now GC cannot happen during the lifetime of the widget_value,
1666 so it's safe to store data from a Lisp_String. */
1667 wv = first_wv->contents;
1668 for (i = 0; i < XVECTOR (items)->size; i += 3)
1670 Lisp_Object string;
1671 string = XVECTOR (items)->contents[i + 1];
1672 if (NILP (string))
1673 break;
1674 wv->name = (char *) XSTRING (string)->data;
1675 wv = wv->next;
1678 f->menu_bar_vector = menu_items;
1679 f->menu_bar_items_used = menu_items_used;
1680 menu_items = Qnil;
1682 else
1684 /* Make a widget-value tree containing
1685 just the top level menu bar strings. */
1687 items = FRAME_MENU_BAR_ITEMS (f);
1688 for (i = 0; i < XVECTOR (items)->size; i += 3)
1690 Lisp_Object string;
1692 string = XVECTOR (items)->contents[i + 1];
1693 if (NILP (string))
1694 break;
1696 wv = malloc_widget_value ();
1697 wv->name = (char *) XSTRING (string)->data;
1698 wv->value = 0;
1699 wv->enabled = 1;
1701 if (prev_wv)
1702 prev_wv->next = wv;
1703 else
1704 first_wv->contents = wv;
1705 prev_wv = wv;
1709 /* Create or update the menu bar widget. */
1711 BLOCK_INPUT;
1713 if (menubar_widget)
1715 /* Disable resizing (done for Motif!) */
1716 lw_allow_resizing (f->output_data.x->widget, False);
1718 /* The third arg is DEEP_P, which says to consider the entire
1719 menu trees we supply, rather than just the menu bar item names. */
1720 lw_modify_all_widgets (id, first_wv, deep_p);
1722 /* Re-enable the edit widget to resize. */
1723 lw_allow_resizing (f->output_data.x->widget, True);
1725 else
1727 menubar_widget = lw_create_widget ("menubar", "menubar", id, first_wv,
1728 f->output_data.x->column_widget,
1730 popup_activate_callback,
1731 menubar_selection_callback,
1732 popup_deactivate_callback);
1733 f->output_data.x->menubar_widget = menubar_widget;
1737 int menubar_size
1738 = (f->output_data.x->menubar_widget
1739 ? (f->output_data.x->menubar_widget->core.height
1740 + f->output_data.x->menubar_widget->core.border_width)
1741 : 0);
1743 #ifdef USE_LUCID
1744 if (FRAME_EXTERNAL_MENU_BAR (f))
1746 Dimension ibw = 0;
1747 XtVaGetValues (f->output_data.x->column_widget,
1748 XtNinternalBorderWidth, &ibw, NULL);
1749 menubar_size += ibw;
1751 #endif /* USE_LUCID */
1753 f->output_data.x->menubar_height = menubar_size;
1756 free_menubar_widget_value_tree (first_wv);
1758 update_frame_menubar (f);
1760 UNBLOCK_INPUT;
1763 /* Called from Fx_create_frame to create the inital menubar of a frame
1764 before it is mapped, so that the window is mapped with the menubar already
1765 there instead of us tacking it on later and thrashing the window after it
1766 is visible. */
1768 void
1769 initialize_frame_menubar (f)
1770 FRAME_PTR f;
1772 /* This function is called before the first chance to redisplay
1773 the frame. It has to be, so the frame will have the right size. */
1774 FRAME_MENU_BAR_ITEMS (f) = menu_bar_items (FRAME_MENU_BAR_ITEMS (f));
1775 set_frame_menubar (f, 1, 1);
1778 /* Get rid of the menu bar of frame F, and free its storage.
1779 This is used when deleting a frame, and when turning off the menu bar. */
1781 void
1782 free_frame_menubar (f)
1783 FRAME_PTR f;
1785 Widget menubar_widget;
1786 int id;
1788 menubar_widget = f->output_data.x->menubar_widget;
1790 if (menubar_widget)
1792 BLOCK_INPUT;
1793 lw_destroy_all_widgets ((LWLIB_ID) f->output_data.x->id);
1794 UNBLOCK_INPUT;
1798 #endif /* USE_X_TOOLKIT */
1800 /* xmenu_show actually displays a menu using the panes and items in menu_items
1801 and returns the value selected from it.
1802 There are two versions of xmenu_show, one for Xt and one for Xlib.
1803 Both assume input is blocked by the caller. */
1805 /* F is the frame the menu is for.
1806 X and Y are the frame-relative specified position,
1807 relative to the inside upper left corner of the frame F.
1808 FOR_CLICK if this menu was invoked for a mouse click.
1809 KEYMAPS is 1 if this menu was specified with keymaps;
1810 in that case, we return a list containing the chosen item's value
1811 and perhaps also the pane's prefix.
1812 TITLE is the specified menu title.
1813 ERROR is a place to store an error message string in case of failure.
1814 (We return nil on failure, but the value doesn't actually matter.) */
1816 #ifdef USE_X_TOOLKIT
1818 /* We need a unique id for each widget handled by the Lucid Widget
1819 library.
1821 For the main windows, and popup menus, we use this counter,
1822 which we increment each time after use. This starts from 1<<16.
1824 For menu bars, we use numbers starting at 0, counted in
1825 next_menubar_widget_id. */
1826 LWLIB_ID widget_id_tick;
1828 #ifdef __STDC__
1829 static Lisp_Object *volatile menu_item_selection;
1830 #else
1831 static Lisp_Object *menu_item_selection;
1832 #endif
1834 static void
1835 popup_selection_callback (widget, id, client_data)
1836 Widget widget;
1837 LWLIB_ID id;
1838 XtPointer client_data;
1840 menu_item_selection = (Lisp_Object *) client_data;
1843 static Lisp_Object
1844 xmenu_show (f, x, y, for_click, keymaps, title, error)
1845 FRAME_PTR f;
1846 int x;
1847 int y;
1848 int for_click;
1849 int keymaps;
1850 Lisp_Object title;
1851 char **error;
1853 int i;
1854 LWLIB_ID menu_id;
1855 Widget menu;
1856 Arg av[2];
1857 int ac = 0;
1858 widget_value *wv, *save_wv = 0, *first_wv = 0, *prev_wv = 0;
1859 widget_value **submenu_stack
1860 = (widget_value **) alloca (menu_items_used * sizeof (widget_value *));
1861 Lisp_Object *subprefix_stack
1862 = (Lisp_Object *) alloca (menu_items_used * sizeof (Lisp_Object));
1863 int submenu_depth = 0;
1864 XButtonPressedEvent dummy;
1866 int first_pane;
1867 int next_release_must_exit = 0;
1869 *error = NULL;
1871 if (menu_items_used <= MENU_ITEMS_PANE_LENGTH)
1873 *error = "Empty menu";
1874 return Qnil;
1877 /* Create a tree of widget_value objects
1878 representing the panes and their items. */
1879 wv = malloc_widget_value ();
1880 wv->name = "menu";
1881 wv->value = 0;
1882 wv->enabled = 1;
1883 first_wv = wv;
1884 first_pane = 1;
1886 /* Loop over all panes and items, filling in the tree. */
1887 i = 0;
1888 while (i < menu_items_used)
1890 if (EQ (XVECTOR (menu_items)->contents[i], Qnil))
1892 submenu_stack[submenu_depth++] = save_wv;
1893 save_wv = prev_wv;
1894 prev_wv = 0;
1895 first_pane = 1;
1896 i++;
1898 else if (EQ (XVECTOR (menu_items)->contents[i], Qlambda))
1900 prev_wv = save_wv;
1901 save_wv = submenu_stack[--submenu_depth];
1902 first_pane = 0;
1903 i++;
1905 else if (EQ (XVECTOR (menu_items)->contents[i], Qt)
1906 && submenu_depth != 0)
1907 i += MENU_ITEMS_PANE_LENGTH;
1908 /* Ignore a nil in the item list.
1909 It's meaningful only for dialog boxes. */
1910 else if (EQ (XVECTOR (menu_items)->contents[i], Qquote))
1911 i += 1;
1912 else if (EQ (XVECTOR (menu_items)->contents[i], Qt))
1914 /* Create a new pane. */
1915 Lisp_Object pane_name, prefix;
1916 char *pane_string;
1917 pane_name = XVECTOR (menu_items)->contents[i + MENU_ITEMS_PANE_NAME];
1918 prefix = XVECTOR (menu_items)->contents[i + MENU_ITEMS_PANE_PREFIX];
1919 pane_string = (NILP (pane_name)
1920 ? "" : (char *) XSTRING (pane_name)->data);
1921 /* If there is just one top-level pane, put all its items directly
1922 under the top-level menu. */
1923 if (menu_items_n_panes == 1)
1924 pane_string = "";
1926 /* If the pane has a meaningful name,
1927 make the pane a top-level menu item
1928 with its items as a submenu beneath it. */
1929 if (!keymaps && strcmp (pane_string, ""))
1931 wv = malloc_widget_value ();
1932 if (save_wv)
1933 save_wv->next = wv;
1934 else
1935 first_wv->contents = wv;
1936 wv->name = pane_string;
1937 if (keymaps && !NILP (prefix))
1938 wv->name++;
1939 wv->value = 0;
1940 wv->enabled = 1;
1941 save_wv = wv;
1942 prev_wv = 0;
1944 else if (first_pane)
1946 save_wv = wv;
1947 prev_wv = 0;
1949 first_pane = 0;
1950 i += MENU_ITEMS_PANE_LENGTH;
1952 else
1954 /* Create a new item within current pane. */
1955 Lisp_Object item_name, enable, descrip, def;
1956 item_name = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_NAME];
1957 enable = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_ENABLE];
1958 descrip
1959 = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_EQUIV_KEY];
1960 def = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_DEFINITION];
1962 wv = malloc_widget_value ();
1963 if (prev_wv)
1964 prev_wv->next = wv;
1965 else
1966 save_wv->contents = wv;
1967 wv->name = (char *) XSTRING (item_name)->data;
1968 if (!NILP (descrip))
1969 wv->key = (char *) XSTRING (descrip)->data;
1970 wv->value = 0;
1971 /* If this item has a null value,
1972 make the call_data null so that it won't display a box
1973 when the mouse is on it. */
1974 wv->call_data
1975 = (!NILP (def) ? (void *) &XVECTOR (menu_items)->contents[i] : 0);
1976 wv->enabled = !NILP (enable);
1977 prev_wv = wv;
1979 i += MENU_ITEMS_ITEM_LENGTH;
1983 /* Deal with the title, if it is non-nil. */
1984 if (!NILP (title))
1986 widget_value *wv_title = malloc_widget_value ();
1987 widget_value *wv_sep1 = malloc_widget_value ();
1988 widget_value *wv_sep2 = malloc_widget_value ();
1990 wv_sep2->name = "--";
1991 wv_sep2->next = first_wv->contents;
1993 wv_sep1->name = "--";
1994 wv_sep1->next = wv_sep2;
1996 wv_title->name = (char *) XSTRING (title)->data;
1997 wv_title->enabled = True;
1998 wv_title->next = wv_sep1;
1999 first_wv->contents = wv_title;
2002 /* Actually create the menu. */
2003 menu_id = widget_id_tick++;
2004 menu = lw_create_widget ("popup", first_wv->name, menu_id, first_wv,
2005 f->output_data.x->widget, 1, 0,
2006 popup_selection_callback,
2007 popup_deactivate_callback);
2009 /* Adjust coordinates to relative to the outer (window manager) window. */
2011 Window child;
2012 int win_x = 0, win_y = 0;
2014 /* Find the position of the outside upper-left corner of
2015 the inner window, with respect to the outer window. */
2016 if (f->output_data.x->parent_desc != FRAME_X_DISPLAY_INFO (f)->root_window)
2018 BLOCK_INPUT;
2019 XTranslateCoordinates (FRAME_X_DISPLAY (f),
2021 /* From-window, to-window. */
2022 f->output_data.x->window_desc,
2023 f->output_data.x->parent_desc,
2025 /* From-position, to-position. */
2026 0, 0, &win_x, &win_y,
2028 /* Child of window. */
2029 &child);
2030 UNBLOCK_INPUT;
2031 x += win_x;
2032 y += win_y;
2036 /* Adjust coordinates to be root-window-relative. */
2037 x += f->output_data.x->left_pos;
2038 y += f->output_data.x->top_pos;
2040 dummy.type = ButtonPress;
2041 dummy.serial = 0;
2042 dummy.send_event = 0;
2043 dummy.display = FRAME_X_DISPLAY (f);
2044 dummy.time = CurrentTime;
2045 dummy.button = 0;
2046 dummy.root = FRAME_X_DISPLAY_INFO (f)->root_window;
2047 dummy.window = dummy.root;
2048 dummy.subwindow = dummy.root;
2049 dummy.x_root = x;
2050 dummy.y_root = y;
2051 dummy.x = x;
2052 dummy.y = y;
2054 /* Don't allow any geometry request from the user. */
2055 XtSetArg (av[ac], XtNgeometry, 0); ac++;
2056 XtSetValues (menu, av, ac);
2058 /* Free the widget_value objects we used to specify the contents. */
2059 free_menubar_widget_value_tree (first_wv);
2061 /* No selection has been chosen yet. */
2062 menu_item_selection = 0;
2064 /* Display the menu. */
2065 lw_popup_menu (menu, &dummy);
2066 popup_activated_flag = 1;
2068 /* Process events that apply to the menu. */
2069 popup_get_selection ((XEvent *) 0, FRAME_X_DISPLAY_INFO (f), menu_id);
2071 /* fp turned off the following statement and wrote a comment
2072 that it is unnecessary--that the menu has already disappeared.
2073 Nowadays the menu disappears ok, all right, but
2074 we need to delete the widgets or multiple ones will pile up. */
2075 lw_destroy_all_widgets (menu_id);
2077 /* Find the selected item, and its pane, to return
2078 the proper value. */
2079 if (menu_item_selection != 0)
2081 Lisp_Object prefix, entry;
2083 prefix = Qnil;
2084 i = 0;
2085 while (i < menu_items_used)
2087 if (EQ (XVECTOR (menu_items)->contents[i], Qnil))
2089 subprefix_stack[submenu_depth++] = prefix;
2090 prefix = entry;
2091 i++;
2093 else if (EQ (XVECTOR (menu_items)->contents[i], Qlambda))
2095 prefix = subprefix_stack[--submenu_depth];
2096 i++;
2098 else if (EQ (XVECTOR (menu_items)->contents[i], Qt))
2100 prefix
2101 = XVECTOR (menu_items)->contents[i + MENU_ITEMS_PANE_PREFIX];
2102 i += MENU_ITEMS_PANE_LENGTH;
2104 /* Ignore a nil in the item list.
2105 It's meaningful only for dialog boxes. */
2106 else if (EQ (XVECTOR (menu_items)->contents[i], Qquote))
2107 i += 1;
2108 else
2110 entry
2111 = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_VALUE];
2112 if (menu_item_selection == &XVECTOR (menu_items)->contents[i])
2114 if (keymaps != 0)
2116 int j;
2118 entry = Fcons (entry, Qnil);
2119 if (!NILP (prefix))
2120 entry = Fcons (prefix, entry);
2121 for (j = submenu_depth - 1; j >= 0; j--)
2122 if (!NILP (subprefix_stack[j]))
2123 entry = Fcons (subprefix_stack[j], entry);
2125 return entry;
2127 i += MENU_ITEMS_ITEM_LENGTH;
2132 return Qnil;
2135 static void
2136 dialog_selection_callback (widget, id, client_data)
2137 Widget widget;
2138 LWLIB_ID id;
2139 XtPointer client_data;
2141 /* The EMACS_INT cast avoids a warning. There's no problem
2142 as long as pointers have enough bits to hold small integers. */
2143 if ((int) (EMACS_INT) client_data != -1)
2144 menu_item_selection = (Lisp_Object *) client_data;
2145 BLOCK_INPUT;
2146 lw_destroy_all_widgets (id);
2147 UNBLOCK_INPUT;
2148 popup_activated_flag = 0;
2151 static char * button_names [] = {
2152 "button1", "button2", "button3", "button4", "button5",
2153 "button6", "button7", "button8", "button9", "button10" };
2155 static Lisp_Object
2156 xdialog_show (f, keymaps, title, error)
2157 FRAME_PTR f;
2158 int keymaps;
2159 Lisp_Object title;
2160 char **error;
2162 int i, nb_buttons=0;
2163 LWLIB_ID dialog_id;
2164 Widget menu;
2165 char dialog_name[6];
2167 widget_value *wv, *save_wv = 0, *first_wv = 0, *prev_wv = 0;
2169 /* Number of elements seen so far, before boundary. */
2170 int left_count = 0;
2171 /* 1 means we've seen the boundary between left-hand elts and right-hand. */
2172 int boundary_seen = 0;
2174 *error = NULL;
2176 if (menu_items_n_panes > 1)
2178 *error = "Multiple panes in dialog box";
2179 return Qnil;
2182 /* Create a tree of widget_value objects
2183 representing the text label and buttons. */
2185 Lisp_Object pane_name, prefix;
2186 char *pane_string;
2187 pane_name = XVECTOR (menu_items)->contents[MENU_ITEMS_PANE_NAME];
2188 prefix = XVECTOR (menu_items)->contents[MENU_ITEMS_PANE_PREFIX];
2189 pane_string = (NILP (pane_name)
2190 ? "" : (char *) XSTRING (pane_name)->data);
2191 prev_wv = malloc_widget_value ();
2192 prev_wv->value = pane_string;
2193 if (keymaps && !NILP (prefix))
2194 prev_wv->name++;
2195 prev_wv->enabled = 1;
2196 prev_wv->name = "message";
2197 first_wv = prev_wv;
2199 /* Loop over all panes and items, filling in the tree. */
2200 i = MENU_ITEMS_PANE_LENGTH;
2201 while (i < menu_items_used)
2204 /* Create a new item within current pane. */
2205 Lisp_Object item_name, enable, descrip;
2206 item_name = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_NAME];
2207 enable = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_ENABLE];
2208 descrip
2209 = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_EQUIV_KEY];
2211 if (NILP (item_name))
2213 free_menubar_widget_value_tree (first_wv);
2214 *error = "Submenu in dialog items";
2215 return Qnil;
2217 if (EQ (item_name, Qquote))
2219 /* This is the boundary between left-side elts
2220 and right-side elts. Stop incrementing right_count. */
2221 boundary_seen = 1;
2222 i++;
2223 continue;
2225 if (nb_buttons >= 10)
2227 free_menubar_widget_value_tree (first_wv);
2228 *error = "Too many dialog items";
2229 return Qnil;
2232 wv = malloc_widget_value ();
2233 prev_wv->next = wv;
2234 wv->name = (char *) button_names[nb_buttons];
2235 if (!NILP (descrip))
2236 wv->key = (char *) XSTRING (descrip)->data;
2237 wv->value = (char *) XSTRING (item_name)->data;
2238 wv->call_data = (void *) &XVECTOR (menu_items)->contents[i];
2239 wv->enabled = !NILP (enable);
2240 prev_wv = wv;
2242 if (! boundary_seen)
2243 left_count++;
2245 nb_buttons++;
2246 i += MENU_ITEMS_ITEM_LENGTH;
2249 /* If the boundary was not specified,
2250 by default put half on the left and half on the right. */
2251 if (! boundary_seen)
2252 left_count = nb_buttons - nb_buttons / 2;
2254 wv = malloc_widget_value ();
2255 wv->name = dialog_name;
2257 /* Dialog boxes use a really stupid name encoding
2258 which specifies how many buttons to use
2259 and how many buttons are on the right.
2260 The Q means something also. */
2261 dialog_name[0] = 'Q';
2262 dialog_name[1] = '0' + nb_buttons;
2263 dialog_name[2] = 'B';
2264 dialog_name[3] = 'R';
2265 /* Number of buttons to put on the right. */
2266 dialog_name[4] = '0' + nb_buttons - left_count;
2267 dialog_name[5] = 0;
2268 wv->contents = first_wv;
2269 first_wv = wv;
2272 /* Actually create the dialog. */
2273 dialog_id = widget_id_tick++;
2274 menu = lw_create_widget (first_wv->name, "dialog", dialog_id, first_wv,
2275 f->output_data.x->widget, 1, 0,
2276 dialog_selection_callback, 0);
2277 lw_modify_all_widgets (dialog_id, first_wv->contents, True);
2278 /* Free the widget_value objects we used to specify the contents. */
2279 free_menubar_widget_value_tree (first_wv);
2281 /* No selection has been chosen yet. */
2282 menu_item_selection = 0;
2284 /* Display the menu. */
2285 lw_pop_up_all_widgets (dialog_id);
2286 popup_activated_flag = 1;
2288 /* Process events that apply to the menu. */
2289 popup_get_selection ((XEvent *) 0, FRAME_X_DISPLAY_INFO (f), dialog_id);
2291 lw_destroy_all_widgets (dialog_id);
2293 /* Find the selected item, and its pane, to return
2294 the proper value. */
2295 if (menu_item_selection != 0)
2297 Lisp_Object prefix;
2299 prefix = Qnil;
2300 i = 0;
2301 while (i < menu_items_used)
2303 Lisp_Object entry;
2305 if (EQ (XVECTOR (menu_items)->contents[i], Qt))
2307 prefix
2308 = XVECTOR (menu_items)->contents[i + MENU_ITEMS_PANE_PREFIX];
2309 i += MENU_ITEMS_PANE_LENGTH;
2311 else
2313 entry
2314 = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_VALUE];
2315 if (menu_item_selection == &XVECTOR (menu_items)->contents[i])
2317 if (keymaps != 0)
2319 entry = Fcons (entry, Qnil);
2320 if (!NILP (prefix))
2321 entry = Fcons (prefix, entry);
2323 return entry;
2325 i += MENU_ITEMS_ITEM_LENGTH;
2330 return Qnil;
2332 #else /* not USE_X_TOOLKIT */
2334 static Lisp_Object
2335 xmenu_show (f, x, y, for_click, keymaps, title, error)
2336 FRAME_PTR f;
2337 int x, y;
2338 int for_click;
2339 int keymaps;
2340 Lisp_Object title;
2341 char **error;
2343 Window root;
2344 XMenu *menu;
2345 int pane, selidx, lpane, status;
2346 Lisp_Object entry, pane_prefix;
2347 char *datap;
2348 int ulx, uly, width, height;
2349 int dispwidth, dispheight;
2350 int i, j;
2351 int maxwidth;
2352 int dummy_int;
2353 unsigned int dummy_uint;
2355 *error = 0;
2356 if (menu_items_n_panes == 0)
2357 return Qnil;
2359 if (menu_items_used <= MENU_ITEMS_PANE_LENGTH)
2361 *error = "Empty menu";
2362 return Qnil;
2365 /* Figure out which root window F is on. */
2366 XGetGeometry (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), &root,
2367 &dummy_int, &dummy_int, &dummy_uint, &dummy_uint,
2368 &dummy_uint, &dummy_uint);
2370 /* Make the menu on that window. */
2371 menu = XMenuCreate (FRAME_X_DISPLAY (f), root, "emacs");
2372 if (menu == NULL)
2374 *error = "Can't create menu";
2375 return Qnil;
2378 #ifdef HAVE_X_WINDOWS
2379 /* Adjust coordinates to relative to the outer (window manager) window. */
2381 Window child;
2382 int win_x = 0, win_y = 0;
2384 /* Find the position of the outside upper-left corner of
2385 the inner window, with respect to the outer window. */
2386 if (f->output_data.x->parent_desc != FRAME_X_DISPLAY_INFO (f)->root_window)
2388 BLOCK_INPUT;
2389 XTranslateCoordinates (FRAME_X_DISPLAY (f),
2391 /* From-window, to-window. */
2392 f->output_data.x->window_desc,
2393 f->output_data.x->parent_desc,
2395 /* From-position, to-position. */
2396 0, 0, &win_x, &win_y,
2398 /* Child of window. */
2399 &child);
2400 UNBLOCK_INPUT;
2401 x += win_x;
2402 y += win_y;
2405 #endif /* HAVE_X_WINDOWS */
2407 /* Adjust coordinates to be root-window-relative. */
2408 x += f->output_data.x->left_pos;
2409 y += f->output_data.x->top_pos;
2411 /* Create all the necessary panes and their items. */
2412 i = 0;
2413 while (i < menu_items_used)
2415 if (EQ (XVECTOR (menu_items)->contents[i], Qt))
2417 /* Create a new pane. */
2418 Lisp_Object pane_name, prefix;
2419 char *pane_string;
2421 pane_name = XVECTOR (menu_items)->contents[i + MENU_ITEMS_PANE_NAME];
2422 prefix = XVECTOR (menu_items)->contents[i + MENU_ITEMS_PANE_PREFIX];
2423 pane_string = (NILP (pane_name)
2424 ? "" : (char *) XSTRING (pane_name)->data);
2425 if (keymaps && !NILP (prefix))
2426 pane_string++;
2428 lpane = XMenuAddPane (FRAME_X_DISPLAY (f), menu, pane_string, TRUE);
2429 if (lpane == XM_FAILURE)
2431 XMenuDestroy (FRAME_X_DISPLAY (f), menu);
2432 *error = "Can't create pane";
2433 return Qnil;
2435 i += MENU_ITEMS_PANE_LENGTH;
2437 /* Find the width of the widest item in this pane. */
2438 maxwidth = 0;
2439 j = i;
2440 while (j < menu_items_used)
2442 Lisp_Object item;
2443 item = XVECTOR (menu_items)->contents[j];
2444 if (EQ (item, Qt))
2445 break;
2446 if (NILP (item))
2448 j++;
2449 continue;
2451 width = XSTRING (item)->size;
2452 if (width > maxwidth)
2453 maxwidth = width;
2455 j += MENU_ITEMS_ITEM_LENGTH;
2458 /* Ignore a nil in the item list.
2459 It's meaningful only for dialog boxes. */
2460 else if (EQ (XVECTOR (menu_items)->contents[i], Qquote))
2461 i += 1;
2462 else
2464 /* Create a new item within current pane. */
2465 Lisp_Object item_name, enable, descrip;
2466 unsigned char *item_data;
2468 item_name = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_NAME];
2469 enable = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_ENABLE];
2470 descrip
2471 = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_EQUIV_KEY];
2472 if (!NILP (descrip))
2474 int gap = maxwidth - XSTRING (item_name)->size;
2475 #ifdef C_ALLOCA
2476 Lisp_Object spacer;
2477 spacer = Fmake_string (make_number (gap), make_number (' '));
2478 item_name = concat2 (item_name, spacer);
2479 item_name = concat2 (item_name, descrip);
2480 item_data = XSTRING (item_name)->data;
2481 #else
2482 /* if alloca is fast, use that to make the space,
2483 to reduce gc needs. */
2484 item_data
2485 = (unsigned char *) alloca (maxwidth
2486 + XSTRING (descrip)->size + 1);
2487 bcopy (XSTRING (item_name)->data, item_data,
2488 XSTRING (item_name)->size);
2489 for (j = XSTRING (item_name)->size; j < maxwidth; j++)
2490 item_data[j] = ' ';
2491 bcopy (XSTRING (descrip)->data, item_data + j,
2492 XSTRING (descrip)->size);
2493 item_data[j + XSTRING (descrip)->size] = 0;
2494 #endif
2496 else
2497 item_data = XSTRING (item_name)->data;
2499 if (XMenuAddSelection (FRAME_X_DISPLAY (f),
2500 menu, lpane, 0, item_data,
2501 !NILP (enable))
2502 == XM_FAILURE)
2504 XMenuDestroy (FRAME_X_DISPLAY (f), menu);
2505 *error = "Can't add selection to menu";
2506 return Qnil;
2508 i += MENU_ITEMS_ITEM_LENGTH;
2512 /* All set and ready to fly. */
2513 XMenuRecompute (FRAME_X_DISPLAY (f), menu);
2514 dispwidth = DisplayWidth (FRAME_X_DISPLAY (f),
2515 XScreenNumberOfScreen (FRAME_X_SCREEN (f)));
2516 dispheight = DisplayHeight (FRAME_X_DISPLAY (f),
2517 XScreenNumberOfScreen (FRAME_X_SCREEN (f)));
2518 x = min (x, dispwidth);
2519 y = min (y, dispheight);
2520 x = max (x, 1);
2521 y = max (y, 1);
2522 XMenuLocate (FRAME_X_DISPLAY (f), menu, 0, 0, x, y,
2523 &ulx, &uly, &width, &height);
2524 if (ulx+width > dispwidth)
2526 x -= (ulx + width) - dispwidth;
2527 ulx = dispwidth - width;
2529 if (uly+height > dispheight)
2531 y -= (uly + height) - dispheight;
2532 uly = dispheight - height;
2534 if (ulx < 0) x -= ulx;
2535 if (uly < 0) y -= uly;
2537 XMenuSetAEQ (menu, TRUE);
2538 XMenuSetFreeze (menu, TRUE);
2539 pane = selidx = 0;
2541 status = XMenuActivate (FRAME_X_DISPLAY (f), menu, &pane, &selidx,
2542 x, y, ButtonReleaseMask, &datap);
2545 #ifdef HAVE_X_WINDOWS
2546 /* Assume the mouse has moved out of the X window.
2547 If it has actually moved in, we will get an EnterNotify. */
2548 x_mouse_leave (FRAME_X_DISPLAY_INFO (f));
2549 #endif
2551 switch (status)
2553 case XM_SUCCESS:
2554 #ifdef XDEBUG
2555 fprintf (stderr, "pane= %d line = %d\n", panes, selidx);
2556 #endif
2558 /* Find the item number SELIDX in pane number PANE. */
2559 i = 0;
2560 while (i < menu_items_used)
2562 if (EQ (XVECTOR (menu_items)->contents[i], Qt))
2564 if (pane == 0)
2565 pane_prefix
2566 = XVECTOR (menu_items)->contents[i + MENU_ITEMS_PANE_PREFIX];
2567 pane--;
2568 i += MENU_ITEMS_PANE_LENGTH;
2570 else
2572 if (pane == -1)
2574 if (selidx == 0)
2576 entry
2577 = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_VALUE];
2578 if (keymaps != 0)
2580 entry = Fcons (entry, Qnil);
2581 if (!NILP (pane_prefix))
2582 entry = Fcons (pane_prefix, entry);
2584 break;
2586 selidx--;
2588 i += MENU_ITEMS_ITEM_LENGTH;
2591 break;
2593 case XM_FAILURE:
2594 *error = "Can't activate menu";
2595 case XM_IA_SELECT:
2596 case XM_NO_SELECT:
2597 entry = Qnil;
2598 break;
2600 XMenuDestroy (FRAME_X_DISPLAY (f), menu);
2602 #ifdef HAVE_X_WINDOWS
2603 /* State that no mouse buttons are now held.
2604 (The oldXMenu code doesn't track this info for us.)
2605 That is not necessarily true, but the fiction leads to reasonable
2606 results, and it is a pain to ask which are actually held now. */
2607 FRAME_X_DISPLAY_INFO (f)->grabbed = 0;
2608 #endif
2610 return entry;
2613 #endif /* not USE_X_TOOLKIT */
2615 syms_of_xmenu ()
2617 staticpro (&menu_items);
2618 menu_items = Qnil;
2620 Qdebug_on_next_call = intern ("debug-on-next-call");
2621 staticpro (&Qdebug_on_next_call);
2623 #ifdef USE_X_TOOLKIT
2624 widget_id_tick = (1<<16);
2625 next_menubar_widget_id = 1;
2626 #endif
2628 defsubr (&Sx_popup_menu);
2629 defsubr (&Sx_popup_dialog);