Comment change.
[emacs.git] / src / xmenu.c
blob2cb381a0fa8a2f4b392c864e4cc1ed3c41e1f25d
1 /* X Communication module for terminals which understand the X protocol.
2 Copyright (C) 1986, 1988, 1993, 1994, 1996 Free Software Foundation, Inc.
4 This file is part of GNU Emacs.
6 GNU Emacs is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
9 any later version.
11 GNU Emacs is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU Emacs; see the file COPYING. If not, write to
18 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA. */
21 /* X pop-up deck-of-cards menu facility for gnuemacs.
23 * Written by Jon Arnold and Roman Budzianowski
24 * Mods and rewrite by Robert Krawitz
28 /* Modified by Fred Pierresteguy on December 93
29 to make the popup menus and menubar use the Xt. */
31 /* Rewritten for clarity and GC protection by rms in Feb 94. */
33 /* On 4.3 this loses if it comes after xterm.h. */
34 #include <signal.h>
35 #include <config.h>
37 #include <stdio.h>
38 #include "lisp.h"
39 #include "termhooks.h"
40 #include "frame.h"
41 #include "window.h"
42 #include "keyboard.h"
43 #include "blockinput.h"
44 #include "puresize.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 #ifdef USE_X_TOOLKIT
67 #include <X11/Xlib.h>
68 #include <X11/IntrinsicP.h>
69 #include <X11/CoreP.h>
70 #include <X11/StringDefs.h>
71 #include <X11/Shell.h>
72 #ifdef USE_LUCID
73 #include <X11/Xaw/Paned.h>
74 #endif /* USE_LUCID */
75 #include "../lwlib/lwlib.h"
76 #else /* not USE_X_TOOLKIT */
77 #include "../oldXMenu/XMenu.h"
78 #endif /* not USE_X_TOOLKIT */
79 #endif /* HAVE_X_WINDOWS */
81 #define min(x,y) (((x) < (y)) ? (x) : (y))
82 #define max(x,y) (((x) > (y)) ? (x) : (y))
84 #ifndef TRUE
85 #define TRUE 1
86 #define FALSE 0
87 #endif /* no TRUE */
89 Lisp_Object Qdebug_on_next_call;
91 Lisp_Object Qmenu_alias;
93 extern Lisp_Object Qmenu_enable;
94 extern Lisp_Object Qmenu_bar;
95 extern Lisp_Object Qmouse_click, Qevent_kind;
97 extern Lisp_Object Vdefine_key_rebound_commands;
99 extern Lisp_Object Voverriding_local_map;
100 extern Lisp_Object Voverriding_local_map_menu_flag;
102 extern Lisp_Object Qoverriding_local_map, Qoverriding_terminal_local_map;
104 extern Lisp_Object Qmenu_bar_update_hook;
106 #ifdef USE_X_TOOLKIT
107 extern void set_frame_menubar ();
108 extern void process_expose_from_menu ();
109 extern XtAppContext Xt_app_con;
111 static Lisp_Object xdialog_show ();
112 void popup_get_selection ();
113 #endif
115 static Lisp_Object xmenu_show ();
116 static void keymap_panes ();
117 static void single_keymap_panes ();
118 static void list_of_panes ();
119 static void list_of_items ();
121 /* This holds a Lisp vector that holds the results of decoding
122 the keymaps or alist-of-alists that specify a menu.
124 It describes the panes and items within the panes.
126 Each pane is described by 3 elements in the vector:
127 t, the pane name, the pane's prefix key.
128 Then follow the pane's items, with 5 elements per item:
129 the item string, the enable flag, the item's value,
130 the definition, and the equivalent keyboard key's description string.
132 In some cases, multiple levels of menus may be described.
133 A single vector slot containing nil indicates the start of a submenu.
134 A single vector slot containing lambda indicates the end of a submenu.
135 The submenu follows a menu item which is the way to reach the submenu.
137 A single vector slot containing quote indicates that the
138 following items should appear on the right of a dialog box.
140 Using a Lisp vector to hold this information while we decode it
141 takes care of protecting all the data from GC. */
143 #define MENU_ITEMS_PANE_NAME 1
144 #define MENU_ITEMS_PANE_PREFIX 2
145 #define MENU_ITEMS_PANE_LENGTH 3
147 #define MENU_ITEMS_ITEM_NAME 0
148 #define MENU_ITEMS_ITEM_ENABLE 1
149 #define MENU_ITEMS_ITEM_VALUE 2
150 #define MENU_ITEMS_ITEM_EQUIV_KEY 3
151 #define MENU_ITEMS_ITEM_DEFINITION 4
152 #define MENU_ITEMS_ITEM_LENGTH 5
154 static Lisp_Object menu_items;
156 /* Number of slots currently allocated in menu_items. */
157 static int menu_items_allocated;
159 /* This is the index in menu_items of the first empty slot. */
160 static int menu_items_used;
162 /* The number of panes currently recorded in menu_items,
163 excluding those within submenus. */
164 static int menu_items_n_panes;
166 /* Current depth within submenus. */
167 static int menu_items_submenu_depth;
169 /* Flag which when set indicates a dialog or menu has been posted by
170 Xt on behalf of one of the widget sets. */
171 static int popup_activated_flag;
173 static int next_menubar_widget_id;
175 static int pending_menu_activation = 1;
177 #ifdef USE_X_TOOLKIT
179 /* Return the frame whose ->output_data.x->id equals ID, or 0 if none. */
181 static struct frame *
182 menubar_id_to_frame (id)
183 LWLIB_ID id;
185 Lisp_Object tail, frame;
186 FRAME_PTR f;
188 for (tail = Vframe_list; GC_CONSP (tail); tail = XCONS (tail)->cdr)
190 frame = XCONS (tail)->car;
191 if (!GC_FRAMEP (frame))
192 continue;
193 f = XFRAME (frame);
194 if (f->output_data.nothing == 1)
195 continue;
196 if (f->output_data.x->id == id)
197 return f;
199 return 0;
202 #endif
204 /* Initialize the menu_items structure if we haven't already done so.
205 Also mark it as currently empty. */
207 static void
208 init_menu_items ()
210 if (NILP (menu_items))
212 menu_items_allocated = 60;
213 menu_items = Fmake_vector (make_number (menu_items_allocated), Qnil);
216 menu_items_used = 0;
217 menu_items_n_panes = 0;
218 menu_items_submenu_depth = 0;
221 /* Call at the end of generating the data in menu_items.
222 This fills in the number of items in the last pane. */
224 static void
225 finish_menu_items ()
229 /* Call when finished using the data for the current menu
230 in menu_items. */
232 static void
233 discard_menu_items ()
235 /* Free the structure if it is especially large.
236 Otherwise, hold on to it, to save time. */
237 if (menu_items_allocated > 200)
239 menu_items = Qnil;
240 menu_items_allocated = 0;
244 /* Make the menu_items vector twice as large. */
246 static void
247 grow_menu_items ()
249 Lisp_Object old;
250 int old_size = menu_items_allocated;
251 old = menu_items;
253 menu_items_allocated *= 2;
254 menu_items = Fmake_vector (make_number (menu_items_allocated), Qnil);
255 bcopy (XVECTOR (old)->contents, XVECTOR (menu_items)->contents,
256 old_size * sizeof (Lisp_Object));
259 /* Begin a submenu. */
261 static void
262 push_submenu_start ()
264 if (menu_items_used + 1 > menu_items_allocated)
265 grow_menu_items ();
267 XVECTOR (menu_items)->contents[menu_items_used++] = Qnil;
268 menu_items_submenu_depth++;
271 /* End a submenu. */
273 static void
274 push_submenu_end ()
276 if (menu_items_used + 1 > menu_items_allocated)
277 grow_menu_items ();
279 XVECTOR (menu_items)->contents[menu_items_used++] = Qlambda;
280 menu_items_submenu_depth--;
283 /* Indicate boundary between left and right. */
285 static void
286 push_left_right_boundary ()
288 if (menu_items_used + 1 > menu_items_allocated)
289 grow_menu_items ();
291 XVECTOR (menu_items)->contents[menu_items_used++] = Qquote;
294 /* Start a new menu pane in menu_items..
295 NAME is the pane name. PREFIX_VEC is a prefix key for this pane. */
297 static void
298 push_menu_pane (name, prefix_vec)
299 Lisp_Object name, prefix_vec;
301 if (menu_items_used + MENU_ITEMS_PANE_LENGTH > menu_items_allocated)
302 grow_menu_items ();
304 if (menu_items_submenu_depth == 0)
305 menu_items_n_panes++;
306 XVECTOR (menu_items)->contents[menu_items_used++] = Qt;
307 XVECTOR (menu_items)->contents[menu_items_used++] = name;
308 XVECTOR (menu_items)->contents[menu_items_used++] = prefix_vec;
311 /* Push one menu item into the current pane.
312 NAME is the string to display. ENABLE if non-nil means
313 this item can be selected. KEY is the key generated by
314 choosing this item, or nil if this item doesn't really have a definition.
315 DEF is the definition of this item.
316 EQUIV is the textual description of the keyboard equivalent for
317 this item (or nil if none). */
319 static void
320 push_menu_item (name, enable, key, def, equiv)
321 Lisp_Object name, enable, key, def, equiv;
323 if (menu_items_used + MENU_ITEMS_ITEM_LENGTH > menu_items_allocated)
324 grow_menu_items ();
326 XVECTOR (menu_items)->contents[menu_items_used++] = name;
327 XVECTOR (menu_items)->contents[menu_items_used++] = enable;
328 XVECTOR (menu_items)->contents[menu_items_used++] = key;
329 XVECTOR (menu_items)->contents[menu_items_used++] = equiv;
330 XVECTOR (menu_items)->contents[menu_items_used++] = def;
333 /* Figure out the current keyboard equivalent of a menu item ITEM1.
334 The item string for menu display should be ITEM_STRING.
335 Store the equivalent keyboard key sequence's
336 textual description into *DESCRIP_PTR.
337 Also cache them in the item itself.
338 Return the real definition to execute. */
340 static Lisp_Object
341 menu_item_equiv_key (item_string, item1, descrip_ptr)
342 Lisp_Object item_string;
343 Lisp_Object item1;
344 Lisp_Object *descrip_ptr;
346 /* This is the real definition--the function to run. */
347 Lisp_Object def;
348 /* This is the sublist that records cached equiv key data
349 so we can save time. */
350 Lisp_Object cachelist;
351 /* These are the saved equivalent keyboard key sequence
352 and its key-description. */
353 Lisp_Object savedkey, descrip;
354 Lisp_Object def1;
355 int changed = 0;
356 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
358 /* If a help string follows the item string, skip it. */
359 if (CONSP (XCONS (item1)->cdr)
360 && STRINGP (XCONS (XCONS (item1)->cdr)->car))
361 item1 = XCONS (item1)->cdr;
363 def = Fcdr (item1);
365 /* Get out the saved equivalent-keyboard-key info. */
366 cachelist = savedkey = descrip = Qnil;
367 if (CONSP (def) && CONSP (XCONS (def)->car)
368 && (NILP (XCONS (XCONS (def)->car)->car)
369 || VECTORP (XCONS (XCONS (def)->car)->car)))
371 cachelist = XCONS (def)->car;
372 def = XCONS (def)->cdr;
373 savedkey = XCONS (cachelist)->car;
374 descrip = XCONS (cachelist)->cdr;
377 GCPRO4 (def, def1, savedkey, descrip);
379 /* Is it still valid? */
380 def1 = Qnil;
381 if (!NILP (savedkey))
382 def1 = Fkey_binding (savedkey, Qnil);
383 /* If not, update it. */
384 if (! EQ (def1, def)
385 /* If the command is an alias for another
386 (such as easymenu.el and lmenu.el set it up),
387 check if the original command matches the cached command. */
388 && !(SYMBOLP (def) && SYMBOLP (XSYMBOL (def)->function)
389 && EQ (def1, XSYMBOL (def)->function))
390 /* If something had no key binding before, don't recheck it
391 because that is too slow--except if we have a list of rebound
392 commands in Vdefine_key_rebound_commands, do recheck any command
393 that appears in that list. */
394 && (NILP (cachelist) || !NILP (savedkey)
395 || (! EQ (Qt, Vdefine_key_rebound_commands)
396 && !NILP (Fmemq (def, Vdefine_key_rebound_commands)))))
398 changed = 1;
399 descrip = Qnil;
400 /* If the command is an alias for another
401 (such as easymenu.el and lmenu.el set it up),
402 see if the original command name has equivalent keys. */
403 if (SYMBOLP (def) && SYMBOLP (XSYMBOL (def)->function)
404 && ! NILP (Fget (def, Qmenu_alias)))
405 savedkey = Fwhere_is_internal (XSYMBOL (def)->function,
406 Qnil, Qt, Qnil);
407 else
408 /* Otherwise look up the specified command itself.
409 We don't try both, because that makes easymenu menus slow. */
410 savedkey = Fwhere_is_internal (def, Qnil, Qt, Qnil);
412 if (!NILP (savedkey))
414 descrip = Fkey_description (savedkey);
415 descrip = concat2 (make_string (" (", 3), descrip);
416 descrip = concat2 (descrip, make_string (")", 1));
420 /* Cache the data we just got in a sublist of the menu binding. */
421 if (NILP (cachelist))
423 CHECK_IMPURE (item1);
424 XCONS (item1)->cdr = Fcons (Fcons (savedkey, descrip), def);
426 else if (changed)
428 XCONS (cachelist)->car = savedkey;
429 XCONS (cachelist)->cdr = descrip;
432 UNGCPRO;
433 *descrip_ptr = descrip;
434 return def;
437 /* This is used as the handler when calling internal_condition_case_1. */
439 static Lisp_Object
440 menu_item_enabled_p_1 (arg)
441 Lisp_Object arg;
443 /* If we got a quit from within the menu computation,
444 quit all the way out of it. This takes care of C-] in the debugger. */
445 if (CONSP (arg) && EQ (XCONS (arg)->car, Qquit))
446 Fsignal (Qquit, Qnil);
448 return Qnil;
451 /* Return non-nil if the command DEF is enabled when used as a menu item.
452 This is based on looking for a menu-enable property.
453 If NOTREAL is set, don't bother really computing this. */
455 static Lisp_Object
456 menu_item_enabled_p (def, notreal)
457 Lisp_Object def;
458 int notreal;
460 Lisp_Object enabled, tem;
462 enabled = Qt;
463 if (notreal)
464 return enabled;
465 if (SYMBOLP (def))
467 /* No property, or nil, means enable.
468 Otherwise, enable if value is not nil. */
469 tem = Fget (def, Qmenu_enable);
470 if (!NILP (tem))
471 /* (condition-case nil (eval tem)
472 (error nil)) */
473 enabled = internal_condition_case_1 (Feval, tem, Qerror,
474 menu_item_enabled_p_1);
476 return enabled;
479 /* Look through KEYMAPS, a vector of keymaps that is NMAPS long,
480 and generate menu panes for them in menu_items.
481 If NOTREAL is nonzero,
482 don't bother really computing whether an item is enabled. */
484 static void
485 keymap_panes (keymaps, nmaps, notreal)
486 Lisp_Object *keymaps;
487 int nmaps;
488 int notreal;
490 int mapno;
492 init_menu_items ();
494 /* Loop over the given keymaps, making a pane for each map.
495 But don't make a pane that is empty--ignore that map instead.
496 P is the number of panes we have made so far. */
497 for (mapno = 0; mapno < nmaps; mapno++)
498 single_keymap_panes (keymaps[mapno], Qnil, Qnil, notreal);
500 finish_menu_items ();
503 /* This is a recursive subroutine of keymap_panes.
504 It handles one keymap, KEYMAP.
505 The other arguments are passed along
506 or point to local variables of the previous function.
507 If NOTREAL is nonzero,
508 don't bother really computing whether an item is enabled. */
510 static void
511 single_keymap_panes (keymap, pane_name, prefix, notreal)
512 Lisp_Object keymap;
513 Lisp_Object pane_name;
514 Lisp_Object prefix;
515 int notreal;
517 Lisp_Object pending_maps;
518 Lisp_Object tail, item, item1, item_string, table;
519 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
521 pending_maps = Qnil;
523 push_menu_pane (pane_name, prefix);
525 for (tail = keymap; CONSP (tail); tail = XCONS (tail)->cdr)
527 /* Look at each key binding, and if it has a menu string,
528 make a menu item from it. */
529 item = XCONS (tail)->car;
530 if (CONSP (item))
532 item1 = XCONS (item)->cdr;
533 if (CONSP (item1))
535 item_string = XCONS (item1)->car;
536 if (STRINGP (item_string))
538 /* This is the real definition--the function to run. */
539 Lisp_Object def;
540 /* These are the saved equivalent keyboard key sequence
541 and its key-description. */
542 Lisp_Object descrip;
543 Lisp_Object tem, enabled;
545 /* GCPRO because ...enabled_p will call eval
546 and ..._equiv_key may autoload something.
547 Protecting KEYMAP preserves everything we use;
548 aside from that, must protect whatever might be
549 a string. Since there's no GCPRO5, we refetch
550 item_string instead of protecting it. */
551 descrip = def = Qnil;
552 GCPRO4 (keymap, pending_maps, def, descrip);
554 def = menu_item_equiv_key (item_string, item1, &descrip);
555 enabled = menu_item_enabled_p (def, notreal);
557 UNGCPRO;
559 item_string = XCONS (item1)->car;
561 tem = Fkeymapp (def);
562 if (XSTRING (item_string)->data[0] == '@' && !NILP (tem))
563 pending_maps = Fcons (Fcons (def, Fcons (item_string, XCONS (item)->car)),
564 pending_maps);
565 else
567 Lisp_Object submap;
568 GCPRO4 (keymap, pending_maps, descrip, item_string);
569 submap = get_keymap_1 (def, 0, 1);
570 UNGCPRO;
571 #ifndef USE_X_TOOLKIT
572 /* Indicate visually that this is a submenu. */
573 if (!NILP (submap))
574 item_string = concat2 (item_string,
575 build_string (" >"));
576 #endif
577 /* If definition is nil, pass nil as the key. */
578 push_menu_item (item_string, enabled,
579 XCONS (item)->car, def,
580 descrip);
581 #ifdef USE_X_TOOLKIT
582 /* Display a submenu using the toolkit. */
583 if (! NILP (submap))
585 push_submenu_start ();
586 single_keymap_panes (submap, Qnil,
587 XCONS (item)->car, notreal);
588 push_submenu_end ();
590 #endif
595 else if (VECTORP (item))
597 /* Loop over the char values represented in the vector. */
598 int len = XVECTOR (item)->size;
599 int c;
600 for (c = 0; c < len; c++)
602 Lisp_Object character;
603 XSETFASTINT (character, c);
604 item1 = XVECTOR (item)->contents[c];
605 if (CONSP (item1))
607 item_string = XCONS (item1)->car;
608 if (STRINGP (item_string))
610 Lisp_Object def;
612 /* These are the saved equivalent keyboard key sequence
613 and its key-description. */
614 Lisp_Object descrip;
615 Lisp_Object tem, enabled;
617 /* GCPRO because ...enabled_p will call eval
618 and ..._equiv_key may autoload something.
619 Protecting KEYMAP preserves everything we use;
620 aside from that, must protect whatever might be
621 a string. Since there's no GCPRO5, we refetch
622 item_string instead of protecting it. */
623 GCPRO4 (keymap, pending_maps, def, descrip);
624 descrip = def = Qnil;
626 def = menu_item_equiv_key (item_string, item1, &descrip);
627 enabled = menu_item_enabled_p (def, notreal);
629 UNGCPRO;
631 item_string = XCONS (item1)->car;
633 tem = Fkeymapp (def);
634 if (XSTRING (item_string)->data[0] == '@' && !NILP (tem))
635 pending_maps = Fcons (Fcons (def, Fcons (item_string, character)),
636 pending_maps);
637 else
639 Lisp_Object submap;
640 GCPRO4 (keymap, pending_maps, descrip, item_string);
641 submap = get_keymap_1 (def, 0, 1);
642 UNGCPRO;
643 #ifndef USE_X_TOOLKIT
644 if (!NILP (submap))
645 item_string = concat2 (item_string,
646 build_string (" >"));
647 #endif
648 /* If definition is nil, pass nil as the key. */
649 push_menu_item (item_string, enabled, character,
650 def, descrip);
651 #ifdef USE_X_TOOLKIT
652 if (! NILP (submap))
654 push_submenu_start ();
655 single_keymap_panes (submap, Qnil,
656 character, notreal);
657 push_submenu_end ();
659 #endif
667 /* Process now any submenus which want to be panes at this level. */
668 while (!NILP (pending_maps))
670 Lisp_Object elt, eltcdr, string;
671 elt = Fcar (pending_maps);
672 eltcdr = XCONS (elt)->cdr;
673 string = XCONS (eltcdr)->car;
674 /* We no longer discard the @ from the beginning of the string here.
675 Instead, we do this in xmenu_show. */
676 single_keymap_panes (Fcar (elt), string,
677 XCONS (eltcdr)->cdr, notreal);
678 pending_maps = Fcdr (pending_maps);
682 /* Push all the panes and items of a menu described by the
683 alist-of-alists MENU.
684 This handles old-fashioned calls to x-popup-menu. */
686 static void
687 list_of_panes (menu)
688 Lisp_Object menu;
690 Lisp_Object tail;
692 init_menu_items ();
694 for (tail = menu; !NILP (tail); tail = Fcdr (tail))
696 Lisp_Object elt, pane_name, pane_data;
697 elt = Fcar (tail);
698 pane_name = Fcar (elt);
699 CHECK_STRING (pane_name, 0);
700 push_menu_pane (pane_name, Qnil);
701 pane_data = Fcdr (elt);
702 CHECK_CONS (pane_data, 0);
703 list_of_items (pane_data);
706 finish_menu_items ();
709 /* Push the items in a single pane defined by the alist PANE. */
711 static void
712 list_of_items (pane)
713 Lisp_Object pane;
715 Lisp_Object tail, item, item1;
717 for (tail = pane; !NILP (tail); tail = Fcdr (tail))
719 item = Fcar (tail);
720 if (STRINGP (item))
721 push_menu_item (item, Qnil, Qnil, Qt, Qnil);
722 else if (NILP (item))
723 push_left_right_boundary ();
724 else
726 CHECK_CONS (item, 0);
727 item1 = Fcar (item);
728 CHECK_STRING (item1, 1);
729 push_menu_item (item1, Qt, Fcdr (item), Qt, Qnil);
734 DEFUN ("x-popup-menu", Fx_popup_menu, Sx_popup_menu, 2, 2, 0,
735 "Pop up a deck-of-cards menu and return user's selection.\n\
736 POSITION is a position specification. This is either a mouse button event\n\
737 or a list ((XOFFSET YOFFSET) WINDOW)\n\
738 where XOFFSET and YOFFSET are positions in pixels from the top left\n\
739 corner of WINDOW's frame. (WINDOW may be a frame object instead of a window.)\n\
740 This controls the position of the center of the first line\n\
741 in the first pane of the menu, not the top left of the menu as a whole.\n\
742 If POSITION is t, it means to use the current mouse position.\n\
744 MENU is a specifier for a menu. For the simplest case, MENU is a keymap.\n\
745 The menu items come from key bindings that have a menu string as well as\n\
746 a definition; actually, the \"definition\" in such a key binding looks like\n\
747 \(STRING . REAL-DEFINITION). To give the menu a title, put a string into\n\
748 the keymap as a top-level element.\n\n\
749 You can also use a list of keymaps as MENU.\n\
750 Then each keymap makes a separate pane.\n\
751 When MENU is a keymap or a list of keymaps, the return value\n\
752 is a list of events.\n\n\
753 Alternatively, you can specify a menu of multiple panes\n\
754 with a list of the form (TITLE PANE1 PANE2...),\n\
755 where each pane is a list of form (TITLE ITEM1 ITEM2...).\n\
756 Each ITEM is normally a cons cell (STRING . VALUE);\n\
757 but a string can appear as an item--that makes a nonselectable line\n\
758 in the menu.\n\
759 With this form of menu, the return value is VALUE from the chosen item.\n\
761 If POSITION is nil, don't display the menu at all, just precalculate the\n\
762 cached information about equivalent key sequences.")
763 (position, menu)
764 Lisp_Object position, menu;
766 int number_of_panes, panes;
767 Lisp_Object keymap, tem;
768 int xpos, ypos;
769 Lisp_Object title;
770 char *error_name;
771 Lisp_Object selection;
772 int i, j;
773 FRAME_PTR f;
774 Lisp_Object x, y, window;
775 int keymaps = 0;
776 int for_click = 0;
777 struct gcpro gcpro1;
779 #ifdef HAVE_MENUS
780 if (! NILP (position))
782 check_x ();
784 /* Decode the first argument: find the window and the coordinates. */
785 if (EQ (position, Qt)
786 || (CONSP (position) && EQ (XCONS (position)->car, Qmenu_bar)))
788 /* Use the mouse's current position. */
789 FRAME_PTR new_f = selected_frame;
790 Lisp_Object bar_window;
791 int part;
792 unsigned long time;
794 if (mouse_position_hook)
795 (*mouse_position_hook) (&new_f, 1, &bar_window,
796 &part, &x, &y, &time);
797 if (new_f != 0)
798 XSETFRAME (window, new_f);
799 else
801 window = selected_window;
802 XSETFASTINT (x, 0);
803 XSETFASTINT (y, 0);
806 else
808 tem = Fcar (position);
809 if (CONSP (tem))
811 window = Fcar (Fcdr (position));
812 x = Fcar (tem);
813 y = Fcar (Fcdr (tem));
815 else
817 for_click = 1;
818 tem = Fcar (Fcdr (position)); /* EVENT_START (position) */
819 window = Fcar (tem); /* POSN_WINDOW (tem) */
820 tem = Fcar (Fcdr (Fcdr (tem))); /* POSN_WINDOW_POSN (tem) */
821 x = Fcar (tem);
822 y = Fcdr (tem);
826 CHECK_NUMBER (x, 0);
827 CHECK_NUMBER (y, 0);
829 /* Decode where to put the menu. */
831 if (FRAMEP (window))
833 f = XFRAME (window);
834 xpos = 0;
835 ypos = 0;
837 else if (WINDOWP (window))
839 CHECK_LIVE_WINDOW (window, 0);
840 f = XFRAME (WINDOW_FRAME (XWINDOW (window)));
842 xpos = (FONT_WIDTH (f->output_data.x->font) * XWINDOW (window)->left);
843 ypos = (f->output_data.x->line_height * XWINDOW (window)->top);
845 else
846 /* ??? Not really clean; should be CHECK_WINDOW_OR_FRAME,
847 but I don't want to make one now. */
848 CHECK_WINDOW (window, 0);
850 xpos += XINT (x);
851 ypos += XINT (y);
853 #endif /* HAVE_MENUS */
855 title = Qnil;
856 GCPRO1 (title);
858 /* Decode the menu items from what was specified. */
860 keymap = Fkeymapp (menu);
861 tem = Qnil;
862 if (CONSP (menu))
863 tem = Fkeymapp (Fcar (menu));
864 if (!NILP (keymap))
866 /* We were given a keymap. Extract menu info from the keymap. */
867 Lisp_Object prompt;
868 keymap = get_keymap (menu);
870 /* Extract the detailed info to make one pane. */
871 keymap_panes (&menu, 1, NILP (position));
873 /* Search for a string appearing directly as an element of the keymap.
874 That string is the title of the menu. */
875 prompt = map_prompt (keymap);
877 /* Make that be the pane title of the first pane. */
878 if (!NILP (prompt) && menu_items_n_panes >= 0)
879 XVECTOR (menu_items)->contents[MENU_ITEMS_PANE_NAME] = prompt;
881 keymaps = 1;
883 else if (!NILP (tem))
885 /* We were given a list of keymaps. */
886 int nmaps = XFASTINT (Flength (menu));
887 Lisp_Object *maps
888 = (Lisp_Object *) alloca (nmaps * sizeof (Lisp_Object));
889 int i;
891 title = Qnil;
893 /* The first keymap that has a prompt string
894 supplies the menu title. */
895 for (tem = menu, i = 0; CONSP (tem); tem = Fcdr (tem))
897 Lisp_Object prompt;
899 maps[i++] = keymap = get_keymap (Fcar (tem));
901 prompt = map_prompt (keymap);
902 if (NILP (title) && !NILP (prompt))
903 title = prompt;
906 /* Extract the detailed info to make one pane. */
907 keymap_panes (maps, nmaps, NILP (position));
909 /* Make the title be the pane title of the first pane. */
910 if (!NILP (title) && menu_items_n_panes >= 0)
911 XVECTOR (menu_items)->contents[MENU_ITEMS_PANE_NAME] = title;
913 keymaps = 1;
915 else
917 /* We were given an old-fashioned menu. */
918 title = Fcar (menu);
919 CHECK_STRING (title, 1);
921 list_of_panes (Fcdr (menu));
923 keymaps = 0;
926 if (NILP (position))
928 discard_menu_items ();
929 UNGCPRO;
930 return Qnil;
933 #ifdef HAVE_MENUS
934 /* Display them in a menu. */
935 BLOCK_INPUT;
937 selection = xmenu_show (f, xpos, ypos, for_click,
938 keymaps, title, &error_name);
939 UNBLOCK_INPUT;
941 discard_menu_items ();
943 UNGCPRO;
944 #endif /* HAVE_MENUS */
946 if (error_name) error (error_name);
947 return selection;
950 #ifdef HAVE_MENUS
952 DEFUN ("x-popup-dialog", Fx_popup_dialog, Sx_popup_dialog, 2, 2, 0,
953 "Pop up a dialog box and return user's selection.\n\
954 POSITION specifies which frame to use.\n\
955 This is normally a mouse button event or a window or frame.\n\
956 If POSITION is t, it means to use the frame the mouse is on.\n\
957 The dialog box appears in the middle of the specified frame.\n\
959 CONTENTS specifies the alternatives to display in the dialog box.\n\
960 It is a list of the form (TITLE ITEM1 ITEM2...).\n\
961 Each ITEM is a cons cell (STRING . VALUE).\n\
962 The return value is VALUE from the chosen item.\n\n\
963 An ITEM may also be just a string--that makes a nonselectable item.\n\
964 An ITEM may also be nil--that means to put all preceding items\n\
965 on the left of the dialog box and all following items on the right.\n\
966 \(By default, approximately half appear on each side.)")
967 (position, contents)
968 Lisp_Object position, contents;
970 FRAME_PTR f;
971 Lisp_Object window;
973 check_x ();
975 /* Decode the first argument: find the window or frame to use. */
976 if (EQ (position, Qt)
977 || (CONSP (position) && EQ (XCONS (position)->car, Qmenu_bar)))
979 #if 0 /* Using the frame the mouse is on may not be right. */
980 /* Use the mouse's current position. */
981 FRAME_PTR new_f = selected_frame;
982 Lisp_Object bar_window;
983 int part;
984 unsigned long time;
985 Lisp_Object x, y;
987 (*mouse_position_hook) (&new_f, 1, &bar_window, &part, &x, &y, &time);
989 if (new_f != 0)
990 XSETFRAME (window, new_f);
991 else
992 window = selected_window;
993 #endif
994 window = selected_window;
996 else if (CONSP (position))
998 Lisp_Object tem;
999 tem = Fcar (position);
1000 if (CONSP (tem))
1001 window = Fcar (Fcdr (position));
1002 else
1004 tem = Fcar (Fcdr (position)); /* EVENT_START (position) */
1005 window = Fcar (tem); /* POSN_WINDOW (tem) */
1008 else if (WINDOWP (position) || FRAMEP (position))
1009 window = position;
1011 /* Decode where to put the menu. */
1013 if (FRAMEP (window))
1014 f = XFRAME (window);
1015 else if (WINDOWP (window))
1017 CHECK_LIVE_WINDOW (window, 0);
1018 f = XFRAME (WINDOW_FRAME (XWINDOW (window)));
1020 else
1021 /* ??? Not really clean; should be CHECK_WINDOW_OR_FRAME,
1022 but I don't want to make one now. */
1023 CHECK_WINDOW (window, 0);
1025 #ifndef USE_X_TOOLKIT
1026 /* Display a menu with these alternatives
1027 in the middle of frame F. */
1029 Lisp_Object x, y, frame, newpos;
1030 XSETFRAME (frame, f);
1031 XSETINT (x, x_pixel_width (f) / 2);
1032 XSETINT (y, x_pixel_height (f) / 2);
1033 newpos = Fcons (Fcons (x, Fcons (y, Qnil)), Fcons (frame, Qnil));
1035 return Fx_popup_menu (newpos,
1036 Fcons (Fcar (contents), Fcons (contents, Qnil)));
1038 #else
1040 Lisp_Object title;
1041 char *error_name;
1042 Lisp_Object selection;
1044 /* Decode the dialog items from what was specified. */
1045 title = Fcar (contents);
1046 CHECK_STRING (title, 1);
1048 list_of_panes (Fcons (contents, Qnil));
1050 /* Display them in a dialog box. */
1051 BLOCK_INPUT;
1052 selection = xdialog_show (f, 0, title, &error_name);
1053 UNBLOCK_INPUT;
1055 discard_menu_items ();
1057 if (error_name) error (error_name);
1058 return selection;
1060 #endif
1063 #ifdef USE_X_TOOLKIT
1065 /* Loop in Xt until the menu pulldown or dialog popup has been
1066 popped down (deactivated). This is used for x-popup-menu
1067 and x-popup-dialog; it is not used for the menu bar any more.
1069 NOTE: All calls to popup_get_selection should be protected
1070 with BLOCK_INPUT, UNBLOCK_INPUT wrappers. */
1072 void
1073 popup_get_selection (initial_event, dpyinfo, id)
1074 XEvent *initial_event;
1075 struct x_display_info *dpyinfo;
1076 LWLIB_ID id;
1078 XEvent event;
1080 /* Define a queue to save up for later unreading
1081 all X events that don't pertain to the menu. */
1082 struct event_queue
1084 XEvent event;
1085 struct event_queue *next;
1088 struct event_queue *queue = NULL;
1089 struct event_queue *queue_tmp;
1091 if (initial_event)
1092 event = *initial_event;
1093 else
1094 XtAppNextEvent (Xt_app_con, &event);
1096 while (1)
1098 /* Handle expose events for editor frames right away. */
1099 if (event.type == Expose)
1100 process_expose_from_menu (event);
1101 /* Make sure we don't consider buttons grabbed after menu goes.
1102 And make sure to deactivate for any ButtonRelease,
1103 even if XtDispatchEvent doesn't do that. */
1104 else if (event.type == ButtonRelease
1105 && dpyinfo->display == event.xbutton.display)
1107 dpyinfo->grabbed &= ~(1 << event.xbutton.button);
1108 popup_activated_flag = 0;
1110 /* If the user presses a key, deactivate the menu.
1111 The user is likely to do that if we get wedged. */
1112 else if (event.type == KeyPress
1113 && dpyinfo->display == event.xbutton.display)
1115 popup_activated_flag = 0;
1116 break;
1118 /* Button presses outside the menu also pop it down. */
1119 else if (event.type == ButtonPress
1120 && event.xany.display == dpyinfo->display
1121 && x_any_window_to_frame (dpyinfo, event.xany.window))
1123 popup_activated_flag = 0;
1124 break;
1127 /* Queue all events not for this popup,
1128 except for Expose, which we've already handled, and ButtonRelease.
1129 Note that the X window is associated with the frame if this
1130 is a menu bar popup, but not if it's a dialog box. So we use
1131 x_non_menubar_window_to_frame, not x_any_window_to_frame. */
1132 if (event.type != Expose
1133 && !(event.type == ButtonRelease
1134 && dpyinfo->display == event.xbutton.display)
1135 && (event.xany.display != dpyinfo->display
1136 || x_non_menubar_window_to_frame (dpyinfo, event.xany.window)))
1138 queue_tmp = (struct event_queue *) malloc (sizeof (struct event_queue));
1140 if (queue_tmp != NULL)
1142 queue_tmp->event = event;
1143 queue_tmp->next = queue;
1144 queue = queue_tmp;
1147 else
1148 XtDispatchEvent (&event);
1150 if (!popup_activated ())
1151 break;
1152 XtAppNextEvent (Xt_app_con, &event);
1155 /* Unread any events that we got but did not handle. */
1156 while (queue != NULL)
1158 queue_tmp = queue;
1159 XPutBackEvent (queue_tmp->event.xany.display, &queue_tmp->event);
1160 queue = queue_tmp->next;
1161 free ((char *)queue_tmp);
1162 /* Cause these events to get read as soon as we UNBLOCK_INPUT. */
1163 interrupt_input_pending = 1;
1167 /* Activate the menu bar of frame F.
1168 This is called from keyboard.c when it gets the
1169 menu_bar_activate_event out of the Emacs event queue.
1171 To activate the menu bar, we use the X button-press event
1172 that was saved in saved_menu_event.
1173 That makes the toolkit do its thing.
1175 But first we recompute the menu bar contents (the whole tree).
1177 The reason for saving the button event until here, instead of
1178 passing it to the toolkit right away, is that we can safely
1179 execute Lisp code. */
1181 x_activate_menubar (f)
1182 FRAME_PTR f;
1184 if (!f->output_data.x->saved_menu_event->type)
1185 return;
1187 if (f->output_data.x->saved_menu_event->type != ButtonRelease)
1188 set_frame_menubar (f, 0, 1);
1189 BLOCK_INPUT;
1190 XtDispatchEvent ((XEvent *) f->output_data.x->saved_menu_event);
1191 UNBLOCK_INPUT;
1192 if (f->output_data.x->saved_menu_event->type == ButtonRelease)
1193 pending_menu_activation = 1;
1195 /* Ignore this if we get it a second time. */
1196 f->output_data.x->saved_menu_event->type = 0;
1199 /* Detect if a dialog or menu has been posted. */
1202 popup_activated ()
1204 return popup_activated_flag;
1208 /* This callback is invoked when the user selects a menubar cascade
1209 pushbutton, but before the pulldown menu is posted. */
1211 static void
1212 popup_activate_callback (widget, id, client_data)
1213 Widget widget;
1214 LWLIB_ID id;
1215 XtPointer client_data;
1217 popup_activated_flag = 1;
1220 /* This callback is called from the menu bar pulldown menu
1221 when the user makes a selection.
1222 Figure out what the user chose
1223 and put the appropriate events into the keyboard buffer. */
1225 static void
1226 menubar_selection_callback (widget, id, client_data)
1227 Widget widget;
1228 LWLIB_ID id;
1229 XtPointer client_data;
1231 Lisp_Object prefix, entry;
1232 FRAME_PTR f = menubar_id_to_frame (id);
1233 Lisp_Object vector;
1234 Lisp_Object *subprefix_stack;
1235 int submenu_depth = 0;
1236 int i;
1238 if (!f)
1239 return;
1240 subprefix_stack = (Lisp_Object *) alloca (f->menu_bar_items_used * sizeof (Lisp_Object));
1241 vector = f->menu_bar_vector;
1242 prefix = Qnil;
1243 i = 0;
1244 while (i < f->menu_bar_items_used)
1246 if (EQ (XVECTOR (vector)->contents[i], Qnil))
1248 subprefix_stack[submenu_depth++] = prefix;
1249 prefix = entry;
1250 i++;
1252 else if (EQ (XVECTOR (vector)->contents[i], Qlambda))
1254 prefix = subprefix_stack[--submenu_depth];
1255 i++;
1257 else if (EQ (XVECTOR (vector)->contents[i], Qt))
1259 prefix = XVECTOR (vector)->contents[i + MENU_ITEMS_PANE_PREFIX];
1260 i += MENU_ITEMS_PANE_LENGTH;
1262 else
1264 entry = XVECTOR (vector)->contents[i + MENU_ITEMS_ITEM_VALUE];
1265 /* The EMACS_INT cast avoids a warning. There's no problem
1266 as long as pointers have enough bits to hold small integers. */
1267 if ((int) (EMACS_INT) client_data == i)
1269 int j;
1270 struct input_event buf;
1271 Lisp_Object frame;
1273 XSETFRAME (frame, f);
1274 buf.kind = menu_bar_event;
1275 buf.frame_or_window = Fcons (frame, Fcons (Qmenu_bar, Qnil));
1276 kbd_buffer_store_event (&buf);
1278 for (j = 0; j < submenu_depth; j++)
1279 if (!NILP (subprefix_stack[j]))
1281 buf.kind = menu_bar_event;
1282 buf.frame_or_window = Fcons (frame, subprefix_stack[j]);
1283 kbd_buffer_store_event (&buf);
1286 if (!NILP (prefix))
1288 buf.kind = menu_bar_event;
1289 buf.frame_or_window = Fcons (frame, prefix);
1290 kbd_buffer_store_event (&buf);
1293 buf.kind = menu_bar_event;
1294 buf.frame_or_window = Fcons (frame, entry);
1295 kbd_buffer_store_event (&buf);
1297 return;
1299 i += MENU_ITEMS_ITEM_LENGTH;
1304 /* This callback is invoked when a dialog or menu is finished being
1305 used and has been unposted. */
1307 static void
1308 popup_deactivate_callback (widget, id, client_data)
1309 Widget widget;
1310 LWLIB_ID id;
1311 XtPointer client_data;
1313 popup_activated_flag = 0;
1316 /* Allocate a widget_value, blocking input. */
1318 widget_value *
1319 xmalloc_widget_value ()
1321 widget_value *value;
1323 BLOCK_INPUT;
1324 value = malloc_widget_value ();
1325 UNBLOCK_INPUT;
1327 return value;
1330 /* This recursively calls free_widget_value on the tree of widgets.
1331 It must free all data that was malloc'ed for these widget_values.
1332 In Emacs, many slots are pointers into the data of Lisp_Strings, and
1333 must be left alone. */
1335 void
1336 free_menubar_widget_value_tree (wv)
1337 widget_value *wv;
1339 if (! wv) return;
1341 wv->name = wv->value = wv->key = (char *) 0xDEADBEEF;
1343 if (wv->contents && (wv->contents != (widget_value*)1))
1345 free_menubar_widget_value_tree (wv->contents);
1346 wv->contents = (widget_value *) 0xDEADBEEF;
1348 if (wv->next)
1350 free_menubar_widget_value_tree (wv->next);
1351 wv->next = (widget_value *) 0xDEADBEEF;
1353 BLOCK_INPUT;
1354 free_widget_value (wv);
1355 UNBLOCK_INPUT;
1358 /* Return a tree of widget_value structures for a menu bar item
1359 whose event type is ITEM_KEY (with string ITEM_NAME)
1360 and whose contents come from the list of keymaps MAPS. */
1362 static widget_value *
1363 single_submenu (item_key, item_name, maps)
1364 Lisp_Object item_key, item_name, maps;
1366 widget_value *wv, *prev_wv, *save_wv, *first_wv;
1367 int i;
1368 int submenu_depth = 0;
1369 Lisp_Object length;
1370 int len;
1371 Lisp_Object *mapvec;
1372 widget_value **submenu_stack;
1373 int mapno;
1374 int previous_items = menu_items_used;
1375 int top_level_items = 0;
1377 length = Flength (maps);
1378 len = XINT (length);
1380 /* Convert the list MAPS into a vector MAPVEC. */
1381 mapvec = (Lisp_Object *) alloca (len * sizeof (Lisp_Object));
1382 for (i = 0; i < len; i++)
1384 mapvec[i] = Fcar (maps);
1385 maps = Fcdr (maps);
1388 menu_items_n_panes = 0;
1390 /* Loop over the given keymaps, making a pane for each map.
1391 But don't make a pane that is empty--ignore that map instead. */
1392 for (i = 0; i < len; i++)
1394 if (SYMBOLP (mapvec[i])
1395 || (CONSP (mapvec[i])
1396 && NILP (Fkeymapp (mapvec[i]))))
1398 /* Here we have a command at top level in the menu bar
1399 as opposed to a submenu. */
1400 top_level_items = 1;
1401 push_menu_pane (Qnil, Qnil);
1402 push_menu_item (item_name, Qt, item_key, mapvec[i], Qnil);
1404 else
1405 single_keymap_panes (mapvec[i], item_name, item_key, 0);
1408 /* Create a tree of widget_value objects
1409 representing the panes and their items. */
1411 submenu_stack
1412 = (widget_value **) alloca (menu_items_used * sizeof (widget_value *));
1413 wv = xmalloc_widget_value ();
1414 wv->name = "menu";
1415 wv->value = 0;
1416 wv->enabled = 1;
1417 first_wv = wv;
1418 save_wv = 0;
1419 prev_wv = 0;
1421 /* Loop over all panes and items made during this call
1422 and construct a tree of widget_value objects.
1423 Ignore the panes and items made by previous calls to
1424 single_submenu, even though those are also in menu_items. */
1425 i = previous_items;
1426 while (i < menu_items_used)
1428 if (EQ (XVECTOR (menu_items)->contents[i], Qnil))
1430 submenu_stack[submenu_depth++] = save_wv;
1431 save_wv = prev_wv;
1432 prev_wv = 0;
1433 i++;
1435 else if (EQ (XVECTOR (menu_items)->contents[i], Qlambda))
1437 prev_wv = save_wv;
1438 save_wv = submenu_stack[--submenu_depth];
1439 i++;
1441 else if (EQ (XVECTOR (menu_items)->contents[i], Qt)
1442 && submenu_depth != 0)
1443 i += MENU_ITEMS_PANE_LENGTH;
1444 /* Ignore a nil in the item list.
1445 It's meaningful only for dialog boxes. */
1446 else if (EQ (XVECTOR (menu_items)->contents[i], Qquote))
1447 i += 1;
1448 else if (EQ (XVECTOR (menu_items)->contents[i], Qt))
1450 /* Create a new pane. */
1451 Lisp_Object pane_name, prefix;
1452 char *pane_string;
1453 pane_name = XVECTOR (menu_items)->contents[i + MENU_ITEMS_PANE_NAME];
1454 prefix = XVECTOR (menu_items)->contents[i + MENU_ITEMS_PANE_PREFIX];
1455 pane_string = (NILP (pane_name)
1456 ? "" : (char *) XSTRING (pane_name)->data);
1457 /* If there is just one top-level pane, put all its items directly
1458 under the top-level menu. */
1459 if (menu_items_n_panes == 1)
1460 pane_string = "";
1462 /* If the pane has a meaningful name,
1463 make the pane a top-level menu item
1464 with its items as a submenu beneath it. */
1465 if (strcmp (pane_string, ""))
1467 wv = xmalloc_widget_value ();
1468 if (save_wv)
1469 save_wv->next = wv;
1470 else
1471 first_wv->contents = wv;
1472 wv->name = pane_string;
1473 /* Ignore the @ that means "separate pane".
1474 This is a kludge, but this isn't worth more time. */
1475 if (!NILP (prefix) && wv->name[0] == '@')
1476 wv->name++;
1477 wv->value = 0;
1478 wv->enabled = 1;
1480 save_wv = wv;
1481 prev_wv = 0;
1482 i += MENU_ITEMS_PANE_LENGTH;
1484 else
1486 /* Create a new item within current pane. */
1487 Lisp_Object item_name, enable, descrip, def;
1488 item_name = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_NAME];
1489 enable = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_ENABLE];
1490 descrip
1491 = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_EQUIV_KEY];
1492 def = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_DEFINITION];
1494 wv = xmalloc_widget_value ();
1495 if (prev_wv)
1496 prev_wv->next = wv;
1497 else
1498 save_wv->contents = wv;
1500 wv->name = (char *) XSTRING (item_name)->data;
1501 if (!NILP (descrip))
1502 wv->key = (char *) XSTRING (descrip)->data;
1503 wv->value = 0;
1504 /* The EMACS_INT cast avoids a warning. There's no problem
1505 as long as pointers have enough bits to hold small integers. */
1506 wv->call_data = (!NILP (def) ? (void *) (EMACS_INT) i : 0);
1507 wv->enabled = !NILP (enable);
1508 prev_wv = wv;
1510 i += MENU_ITEMS_ITEM_LENGTH;
1514 /* If we have just one "menu item"
1515 that was originally a button, return it by itself. */
1516 if (top_level_items && first_wv->contents && first_wv->contents->next == 0)
1518 wv = first_wv->contents;
1519 free_widget_value (first_wv);
1520 return wv;
1523 return first_wv;
1526 extern void EmacsFrameSetCharSize ();
1528 /* Recompute all the widgets of frame F, when the menu bar
1529 has been changed. */
1531 static void
1532 update_frame_menubar (f)
1533 FRAME_PTR f;
1535 struct x_output *x = f->output_data.x;
1536 int columns, rows;
1537 int menubar_changed;
1539 Dimension shell_height;
1541 /* We assume the menubar contents has changed if the global flag is set,
1542 or if the current buffer has changed, or if the menubar has never
1543 been updated before.
1545 menubar_changed = (x->menubar_widget
1546 && !XtIsManaged (x->menubar_widget));
1548 if (! (menubar_changed))
1549 return;
1551 BLOCK_INPUT;
1552 /* Save the size of the frame because the pane widget doesn't accept to
1553 resize itself. So force it. */
1554 columns = f->width;
1555 rows = f->height;
1557 /* Do the voodoo which means "I'm changing lots of things, don't try to
1558 refigure sizes until I'm done." */
1559 lw_refigure_widget (x->column_widget, False);
1561 /* the order in which children are managed is the top to
1562 bottom order in which they are displayed in the paned window.
1563 First, remove the text-area widget.
1565 XtUnmanageChild (x->edit_widget);
1567 /* remove the menubar that is there now, and put up the menubar that
1568 should be there.
1570 if (menubar_changed)
1572 XtManageChild (x->menubar_widget);
1573 XtMapWidget (x->menubar_widget);
1574 XtVaSetValues (x->menubar_widget, XtNmappedWhenManaged, 1, 0);
1577 /* Re-manage the text-area widget, and then thrash the sizes. */
1578 XtManageChild (x->edit_widget);
1579 lw_refigure_widget (x->column_widget, True);
1581 /* Force the pane widget to resize itself with the right values. */
1582 EmacsFrameSetCharSize (x->edit_widget, columns, rows);
1584 UNBLOCK_INPUT;
1587 /* Set the contents of the menubar widgets of frame F.
1588 The argument FIRST_TIME is currently ignored;
1589 it is set the first time this is called, from initialize_frame_menubar. */
1591 void
1592 set_frame_menubar (f, first_time, deep_p)
1593 FRAME_PTR f;
1594 int first_time;
1595 int deep_p;
1597 Widget menubar_widget = f->output_data.x->menubar_widget;
1598 Lisp_Object tail, items, frame;
1599 widget_value *wv, *first_wv, *prev_wv = 0;
1600 int i;
1601 LWLIB_ID id;
1603 if (f->output_data.x->id == 0)
1604 f->output_data.x->id = next_menubar_widget_id++;
1605 id = f->output_data.x->id;
1607 if (! menubar_widget)
1608 deep_p = 1;
1609 else if (pending_menu_activation && !deep_p)
1611 deep_p = 1;
1612 pending_menu_activation = 0;
1615 wv = xmalloc_widget_value ();
1616 wv->name = "menubar";
1617 wv->value = 0;
1618 wv->enabled = 1;
1619 first_wv = wv;
1621 if (deep_p)
1623 /* Make a widget-value tree representing the entire menu trees. */
1625 struct buffer *prev = current_buffer;
1626 Lisp_Object buffer;
1627 int specpdl_count = specpdl_ptr - specpdl;
1628 int previous_menu_items_used = f->menu_bar_items_used;
1629 Lisp_Object *previous_items
1630 = (Lisp_Object *) alloca (previous_menu_items_used
1631 * sizeof (Lisp_Object));
1633 buffer = XWINDOW (FRAME_SELECTED_WINDOW (f))->buffer;
1634 specbind (Qinhibit_quit, Qt);
1635 /* Don't let the debugger step into this code
1636 because it is not reentrant. */
1637 specbind (Qdebug_on_next_call, Qnil);
1639 record_unwind_protect (Fstore_match_data, Fmatch_data ());
1640 if (NILP (Voverriding_local_map_menu_flag))
1642 specbind (Qoverriding_terminal_local_map, Qnil);
1643 specbind (Qoverriding_local_map, Qnil);
1646 set_buffer_internal_1 (XBUFFER (buffer));
1648 /* Run the Lucid hook. */
1649 call1 (Vrun_hooks, Qactivate_menubar_hook);
1650 /* If it has changed current-menubar from previous value,
1651 really recompute the menubar from the value. */
1652 if (! NILP (Vlucid_menu_bar_dirty_flag))
1653 call0 (Qrecompute_lucid_menubar);
1654 safe_run_hooks (Qmenu_bar_update_hook);
1655 FRAME_MENU_BAR_ITEMS (f) = menu_bar_items (FRAME_MENU_BAR_ITEMS (f));
1657 items = FRAME_MENU_BAR_ITEMS (f);
1659 inhibit_garbage_collection ();
1661 /* Save the frame's previous menu bar contents data. */
1662 bcopy (XVECTOR (f->menu_bar_vector)->contents, previous_items,
1663 previous_menu_items_used * sizeof (Lisp_Object));
1665 /* Fill in the current menu bar contents. */
1666 menu_items = f->menu_bar_vector;
1667 menu_items_allocated = XVECTOR (menu_items)->size;
1668 init_menu_items ();
1669 for (i = 0; i < XVECTOR (items)->size; i += 4)
1671 Lisp_Object key, string, maps;
1673 key = XVECTOR (items)->contents[i];
1674 string = XVECTOR (items)->contents[i + 1];
1675 maps = XVECTOR (items)->contents[i + 2];
1676 if (NILP (string))
1677 break;
1679 wv = single_submenu (key, string, maps);
1680 if (prev_wv)
1681 prev_wv->next = wv;
1682 else
1683 first_wv->contents = wv;
1684 /* Don't set wv->name here; GC during the loop might relocate it. */
1685 wv->enabled = 1;
1686 prev_wv = wv;
1689 finish_menu_items ();
1691 set_buffer_internal_1 (prev);
1692 unbind_to (specpdl_count, Qnil);
1694 /* If there has been no change in the Lisp-level contents
1695 of the menu bar, skip redisplaying it. Just exit. */
1697 for (i = 0; i < previous_menu_items_used; i++)
1698 if (menu_items_used == i
1699 || (previous_items[i] != XVECTOR (menu_items)->contents[i]))
1700 break;
1701 if (i == menu_items_used && i == previous_menu_items_used && i != 0)
1703 free_menubar_widget_value_tree (first_wv);
1704 menu_items = Qnil;
1706 return;
1709 /* Now GC cannot happen during the lifetime of the widget_value,
1710 so it's safe to store data from a Lisp_String. */
1711 wv = first_wv->contents;
1712 for (i = 0; i < XVECTOR (items)->size; i += 4)
1714 Lisp_Object string;
1715 string = XVECTOR (items)->contents[i + 1];
1716 if (NILP (string))
1717 break;
1718 wv->name = (char *) XSTRING (string)->data;
1719 wv = wv->next;
1722 f->menu_bar_vector = menu_items;
1723 f->menu_bar_items_used = menu_items_used;
1724 menu_items = Qnil;
1726 else
1728 /* Make a widget-value tree containing
1729 just the top level menu bar strings. */
1731 items = FRAME_MENU_BAR_ITEMS (f);
1732 for (i = 0; i < XVECTOR (items)->size; i += 4)
1734 Lisp_Object string;
1736 string = XVECTOR (items)->contents[i + 1];
1737 if (NILP (string))
1738 break;
1740 wv = xmalloc_widget_value ();
1741 wv->name = (char *) XSTRING (string)->data;
1742 wv->value = 0;
1743 wv->enabled = 1;
1744 /* This prevents lwlib from assuming this
1745 menu item is really supposed to be empty. */
1746 /* The EMACS_INT cast avoids a warning.
1747 This value just has to be different from small integers. */
1748 wv->call_data = (void *) (EMACS_INT) (-1);
1750 if (prev_wv)
1751 prev_wv->next = wv;
1752 else
1753 first_wv->contents = wv;
1754 prev_wv = wv;
1757 /* Forget what we thought we knew about what is in the
1758 detailed contents of the menu bar menus.
1759 Changing the top level always destroys the contents. */
1760 f->menu_bar_items_used = 0;
1763 /* Create or update the menu bar widget. */
1765 BLOCK_INPUT;
1767 if (menubar_widget)
1769 /* Disable resizing (done for Motif!) */
1770 lw_allow_resizing (f->output_data.x->widget, False);
1772 /* The third arg is DEEP_P, which says to consider the entire
1773 menu trees we supply, rather than just the menu bar item names. */
1774 lw_modify_all_widgets (id, first_wv, deep_p);
1776 /* Re-enable the edit widget to resize. */
1777 lw_allow_resizing (f->output_data.x->widget, True);
1779 else
1781 menubar_widget = lw_create_widget ("menubar", "menubar", id, first_wv,
1782 f->output_data.x->column_widget,
1784 popup_activate_callback,
1785 menubar_selection_callback,
1786 popup_deactivate_callback);
1787 f->output_data.x->menubar_widget = menubar_widget;
1791 int menubar_size
1792 = (f->output_data.x->menubar_widget
1793 ? (f->output_data.x->menubar_widget->core.height
1794 + f->output_data.x->menubar_widget->core.border_width)
1795 : 0);
1797 #ifdef USE_LUCID
1798 if (FRAME_EXTERNAL_MENU_BAR (f))
1800 Dimension ibw = 0;
1801 XtVaGetValues (f->output_data.x->column_widget,
1802 XtNinternalBorderWidth, &ibw, NULL);
1803 menubar_size += ibw;
1805 #endif /* USE_LUCID */
1807 f->output_data.x->menubar_height = menubar_size;
1810 free_menubar_widget_value_tree (first_wv);
1812 update_frame_menubar (f);
1814 UNBLOCK_INPUT;
1817 /* Called from Fx_create_frame to create the initial menubar of a frame
1818 before it is mapped, so that the window is mapped with the menubar already
1819 there instead of us tacking it on later and thrashing the window after it
1820 is visible. */
1822 void
1823 initialize_frame_menubar (f)
1824 FRAME_PTR f;
1826 /* This function is called before the first chance to redisplay
1827 the frame. It has to be, so the frame will have the right size. */
1828 FRAME_MENU_BAR_ITEMS (f) = menu_bar_items (FRAME_MENU_BAR_ITEMS (f));
1829 set_frame_menubar (f, 1, 1);
1832 /* Get rid of the menu bar of frame F, and free its storage.
1833 This is used when deleting a frame, and when turning off the menu bar. */
1835 void
1836 free_frame_menubar (f)
1837 FRAME_PTR f;
1839 Widget menubar_widget;
1840 int id;
1842 menubar_widget = f->output_data.x->menubar_widget;
1844 if (menubar_widget)
1846 BLOCK_INPUT;
1847 lw_destroy_all_widgets ((LWLIB_ID) f->output_data.x->id);
1848 UNBLOCK_INPUT;
1852 #endif /* USE_X_TOOLKIT */
1854 /* xmenu_show actually displays a menu using the panes and items in menu_items
1855 and returns the value selected from it.
1856 There are two versions of xmenu_show, one for Xt and one for Xlib.
1857 Both assume input is blocked by the caller. */
1859 /* F is the frame the menu is for.
1860 X and Y are the frame-relative specified position,
1861 relative to the inside upper left corner of the frame F.
1862 FOR_CLICK if this menu was invoked for a mouse click.
1863 KEYMAPS is 1 if this menu was specified with keymaps;
1864 in that case, we return a list containing the chosen item's value
1865 and perhaps also the pane's prefix.
1866 TITLE is the specified menu title.
1867 ERROR is a place to store an error message string in case of failure.
1868 (We return nil on failure, but the value doesn't actually matter.) */
1870 #ifdef USE_X_TOOLKIT
1872 /* We need a unique id for each widget handled by the Lucid Widget
1873 library.
1875 For the main windows, and popup menus, we use this counter,
1876 which we increment each time after use. This starts from 1<<16.
1878 For menu bars, we use numbers starting at 0, counted in
1879 next_menubar_widget_id. */
1880 LWLIB_ID widget_id_tick;
1882 #ifdef __STDC__
1883 static Lisp_Object *volatile menu_item_selection;
1884 #else
1885 static Lisp_Object *menu_item_selection;
1886 #endif
1888 static void
1889 popup_selection_callback (widget, id, client_data)
1890 Widget widget;
1891 LWLIB_ID id;
1892 XtPointer client_data;
1894 menu_item_selection = (Lisp_Object *) client_data;
1897 static Lisp_Object
1898 xmenu_show (f, x, y, for_click, keymaps, title, error)
1899 FRAME_PTR f;
1900 int x;
1901 int y;
1902 int for_click;
1903 int keymaps;
1904 Lisp_Object title;
1905 char **error;
1907 int i;
1908 LWLIB_ID menu_id;
1909 Widget menu;
1910 Arg av[2];
1911 int ac = 0;
1912 widget_value *wv, *save_wv = 0, *first_wv = 0, *prev_wv = 0;
1913 widget_value **submenu_stack
1914 = (widget_value **) alloca (menu_items_used * sizeof (widget_value *));
1915 Lisp_Object *subprefix_stack
1916 = (Lisp_Object *) alloca (menu_items_used * sizeof (Lisp_Object));
1917 int submenu_depth = 0;
1918 XButtonPressedEvent dummy;
1920 int first_pane;
1921 int next_release_must_exit = 0;
1923 *error = NULL;
1925 if (menu_items_used <= MENU_ITEMS_PANE_LENGTH)
1927 *error = "Empty menu";
1928 return Qnil;
1931 /* Create a tree of widget_value objects
1932 representing the panes and their items. */
1933 wv = xmalloc_widget_value ();
1934 wv->name = "menu";
1935 wv->value = 0;
1936 wv->enabled = 1;
1937 first_wv = wv;
1938 first_pane = 1;
1940 /* Loop over all panes and items, filling in the tree. */
1941 i = 0;
1942 while (i < menu_items_used)
1944 if (EQ (XVECTOR (menu_items)->contents[i], Qnil))
1946 submenu_stack[submenu_depth++] = save_wv;
1947 save_wv = prev_wv;
1948 prev_wv = 0;
1949 first_pane = 1;
1950 i++;
1952 else if (EQ (XVECTOR (menu_items)->contents[i], Qlambda))
1954 prev_wv = save_wv;
1955 save_wv = submenu_stack[--submenu_depth];
1956 first_pane = 0;
1957 i++;
1959 else if (EQ (XVECTOR (menu_items)->contents[i], Qt)
1960 && submenu_depth != 0)
1961 i += MENU_ITEMS_PANE_LENGTH;
1962 /* Ignore a nil in the item list.
1963 It's meaningful only for dialog boxes. */
1964 else if (EQ (XVECTOR (menu_items)->contents[i], Qquote))
1965 i += 1;
1966 else if (EQ (XVECTOR (menu_items)->contents[i], Qt))
1968 /* Create a new pane. */
1969 Lisp_Object pane_name, prefix;
1970 char *pane_string;
1971 pane_name = XVECTOR (menu_items)->contents[i + MENU_ITEMS_PANE_NAME];
1972 prefix = XVECTOR (menu_items)->contents[i + MENU_ITEMS_PANE_PREFIX];
1973 pane_string = (NILP (pane_name)
1974 ? "" : (char *) XSTRING (pane_name)->data);
1975 /* If there is just one top-level pane, put all its items directly
1976 under the top-level menu. */
1977 if (menu_items_n_panes == 1)
1978 pane_string = "";
1980 /* If the pane has a meaningful name,
1981 make the pane a top-level menu item
1982 with its items as a submenu beneath it. */
1983 if (!keymaps && strcmp (pane_string, ""))
1985 wv = xmalloc_widget_value ();
1986 if (save_wv)
1987 save_wv->next = wv;
1988 else
1989 first_wv->contents = wv;
1990 wv->name = pane_string;
1991 if (keymaps && !NILP (prefix))
1992 wv->name++;
1993 wv->value = 0;
1994 wv->enabled = 1;
1995 save_wv = wv;
1996 prev_wv = 0;
1998 else if (first_pane)
2000 save_wv = wv;
2001 prev_wv = 0;
2003 first_pane = 0;
2004 i += MENU_ITEMS_PANE_LENGTH;
2006 else
2008 /* Create a new item within current pane. */
2009 Lisp_Object item_name, enable, descrip, def;
2010 item_name = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_NAME];
2011 enable = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_ENABLE];
2012 descrip
2013 = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_EQUIV_KEY];
2014 def = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_DEFINITION];
2016 wv = xmalloc_widget_value ();
2017 if (prev_wv)
2018 prev_wv->next = wv;
2019 else
2020 save_wv->contents = wv;
2021 wv->name = (char *) XSTRING (item_name)->data;
2022 if (!NILP (descrip))
2023 wv->key = (char *) XSTRING (descrip)->data;
2024 wv->value = 0;
2025 /* If this item has a null value,
2026 make the call_data null so that it won't display a box
2027 when the mouse is on it. */
2028 wv->call_data
2029 = (!NILP (def) ? (void *) &XVECTOR (menu_items)->contents[i] : 0);
2030 wv->enabled = !NILP (enable);
2031 prev_wv = wv;
2033 i += MENU_ITEMS_ITEM_LENGTH;
2037 /* Deal with the title, if it is non-nil. */
2038 if (!NILP (title))
2040 widget_value *wv_title = xmalloc_widget_value ();
2041 widget_value *wv_sep1 = xmalloc_widget_value ();
2042 widget_value *wv_sep2 = xmalloc_widget_value ();
2044 wv_sep2->name = "--";
2045 wv_sep2->next = first_wv->contents;
2047 wv_sep1->name = "--";
2048 wv_sep1->next = wv_sep2;
2050 wv_title->name = (char *) XSTRING (title)->data;
2051 wv_title->enabled = True;
2052 wv_title->next = wv_sep1;
2053 first_wv->contents = wv_title;
2056 /* Actually create the menu. */
2057 menu_id = widget_id_tick++;
2058 menu = lw_create_widget ("popup", first_wv->name, menu_id, first_wv,
2059 f->output_data.x->widget, 1, 0,
2060 popup_selection_callback,
2061 popup_deactivate_callback);
2063 /* Adjust coordinates to relative to the outer (window manager) window. */
2065 Window child;
2066 int win_x = 0, win_y = 0;
2068 /* Find the position of the outside upper-left corner of
2069 the inner window, with respect to the outer window. */
2070 if (f->output_data.x->parent_desc != FRAME_X_DISPLAY_INFO (f)->root_window)
2072 BLOCK_INPUT;
2073 XTranslateCoordinates (FRAME_X_DISPLAY (f),
2075 /* From-window, to-window. */
2076 f->output_data.x->window_desc,
2077 f->output_data.x->parent_desc,
2079 /* From-position, to-position. */
2080 0, 0, &win_x, &win_y,
2082 /* Child of window. */
2083 &child);
2084 UNBLOCK_INPUT;
2085 x += win_x;
2086 y += win_y;
2090 /* Adjust coordinates to be root-window-relative. */
2091 x += f->output_data.x->left_pos;
2092 y += f->output_data.x->top_pos;
2094 dummy.type = ButtonPress;
2095 dummy.serial = 0;
2096 dummy.send_event = 0;
2097 dummy.display = FRAME_X_DISPLAY (f);
2098 dummy.time = CurrentTime;
2099 dummy.button = 0;
2100 dummy.root = FRAME_X_DISPLAY_INFO (f)->root_window;
2101 dummy.window = dummy.root;
2102 dummy.subwindow = dummy.root;
2103 dummy.x_root = x;
2104 dummy.y_root = y;
2105 dummy.x = x;
2106 dummy.y = y;
2108 /* Don't allow any geometry request from the user. */
2109 XtSetArg (av[ac], XtNgeometry, 0); ac++;
2110 XtSetValues (menu, av, ac);
2112 /* Free the widget_value objects we used to specify the contents. */
2113 free_menubar_widget_value_tree (first_wv);
2115 /* No selection has been chosen yet. */
2116 menu_item_selection = 0;
2118 /* Display the menu. */
2119 lw_popup_menu (menu, &dummy);
2120 popup_activated_flag = 1;
2122 /* Process events that apply to the menu. */
2123 popup_get_selection ((XEvent *) 0, FRAME_X_DISPLAY_INFO (f), menu_id);
2125 /* fp turned off the following statement and wrote a comment
2126 that it is unnecessary--that the menu has already disappeared.
2127 Nowadays the menu disappears ok, all right, but
2128 we need to delete the widgets or multiple ones will pile up. */
2129 lw_destroy_all_widgets (menu_id);
2131 /* Find the selected item, and its pane, to return
2132 the proper value. */
2133 if (menu_item_selection != 0)
2135 Lisp_Object prefix, entry;
2137 prefix = Qnil;
2138 i = 0;
2139 while (i < menu_items_used)
2141 if (EQ (XVECTOR (menu_items)->contents[i], Qnil))
2143 subprefix_stack[submenu_depth++] = prefix;
2144 prefix = entry;
2145 i++;
2147 else if (EQ (XVECTOR (menu_items)->contents[i], Qlambda))
2149 prefix = subprefix_stack[--submenu_depth];
2150 i++;
2152 else if (EQ (XVECTOR (menu_items)->contents[i], Qt))
2154 prefix
2155 = XVECTOR (menu_items)->contents[i + MENU_ITEMS_PANE_PREFIX];
2156 i += MENU_ITEMS_PANE_LENGTH;
2158 /* Ignore a nil in the item list.
2159 It's meaningful only for dialog boxes. */
2160 else if (EQ (XVECTOR (menu_items)->contents[i], Qquote))
2161 i += 1;
2162 else
2164 entry
2165 = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_VALUE];
2166 if (menu_item_selection == &XVECTOR (menu_items)->contents[i])
2168 if (keymaps != 0)
2170 int j;
2172 entry = Fcons (entry, Qnil);
2173 if (!NILP (prefix))
2174 entry = Fcons (prefix, entry);
2175 for (j = submenu_depth - 1; j >= 0; j--)
2176 if (!NILP (subprefix_stack[j]))
2177 entry = Fcons (subprefix_stack[j], entry);
2179 return entry;
2181 i += MENU_ITEMS_ITEM_LENGTH;
2186 return Qnil;
2189 static void
2190 dialog_selection_callback (widget, id, client_data)
2191 Widget widget;
2192 LWLIB_ID id;
2193 XtPointer client_data;
2195 /* The EMACS_INT cast avoids a warning. There's no problem
2196 as long as pointers have enough bits to hold small integers. */
2197 if ((int) (EMACS_INT) client_data != -1)
2198 menu_item_selection = (Lisp_Object *) client_data;
2199 BLOCK_INPUT;
2200 lw_destroy_all_widgets (id);
2201 UNBLOCK_INPUT;
2202 popup_activated_flag = 0;
2205 static char * button_names [] = {
2206 "button1", "button2", "button3", "button4", "button5",
2207 "button6", "button7", "button8", "button9", "button10" };
2209 static Lisp_Object
2210 xdialog_show (f, keymaps, title, error)
2211 FRAME_PTR f;
2212 int keymaps;
2213 Lisp_Object title;
2214 char **error;
2216 int i, nb_buttons=0;
2217 LWLIB_ID dialog_id;
2218 Widget menu;
2219 char dialog_name[6];
2221 widget_value *wv, *save_wv = 0, *first_wv = 0, *prev_wv = 0;
2223 /* Number of elements seen so far, before boundary. */
2224 int left_count = 0;
2225 /* 1 means we've seen the boundary between left-hand elts and right-hand. */
2226 int boundary_seen = 0;
2228 *error = NULL;
2230 if (menu_items_n_panes > 1)
2232 *error = "Multiple panes in dialog box";
2233 return Qnil;
2236 /* Create a tree of widget_value objects
2237 representing the text label and buttons. */
2239 Lisp_Object pane_name, prefix;
2240 char *pane_string;
2241 pane_name = XVECTOR (menu_items)->contents[MENU_ITEMS_PANE_NAME];
2242 prefix = XVECTOR (menu_items)->contents[MENU_ITEMS_PANE_PREFIX];
2243 pane_string = (NILP (pane_name)
2244 ? "" : (char *) XSTRING (pane_name)->data);
2245 prev_wv = xmalloc_widget_value ();
2246 prev_wv->value = pane_string;
2247 if (keymaps && !NILP (prefix))
2248 prev_wv->name++;
2249 prev_wv->enabled = 1;
2250 prev_wv->name = "message";
2251 first_wv = prev_wv;
2253 /* Loop over all panes and items, filling in the tree. */
2254 i = MENU_ITEMS_PANE_LENGTH;
2255 while (i < menu_items_used)
2258 /* Create a new item within current pane. */
2259 Lisp_Object item_name, enable, descrip;
2260 item_name = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_NAME];
2261 enable = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_ENABLE];
2262 descrip
2263 = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_EQUIV_KEY];
2265 if (NILP (item_name))
2267 free_menubar_widget_value_tree (first_wv);
2268 *error = "Submenu in dialog items";
2269 return Qnil;
2271 if (EQ (item_name, Qquote))
2273 /* This is the boundary between left-side elts
2274 and right-side elts. Stop incrementing right_count. */
2275 boundary_seen = 1;
2276 i++;
2277 continue;
2279 if (nb_buttons >= 10)
2281 free_menubar_widget_value_tree (first_wv);
2282 *error = "Too many dialog items";
2283 return Qnil;
2286 wv = xmalloc_widget_value ();
2287 prev_wv->next = wv;
2288 wv->name = (char *) button_names[nb_buttons];
2289 if (!NILP (descrip))
2290 wv->key = (char *) XSTRING (descrip)->data;
2291 wv->value = (char *) XSTRING (item_name)->data;
2292 wv->call_data = (void *) &XVECTOR (menu_items)->contents[i];
2293 wv->enabled = !NILP (enable);
2294 prev_wv = wv;
2296 if (! boundary_seen)
2297 left_count++;
2299 nb_buttons++;
2300 i += MENU_ITEMS_ITEM_LENGTH;
2303 /* If the boundary was not specified,
2304 by default put half on the left and half on the right. */
2305 if (! boundary_seen)
2306 left_count = nb_buttons - nb_buttons / 2;
2308 wv = xmalloc_widget_value ();
2309 wv->name = dialog_name;
2311 /* Dialog boxes use a really stupid name encoding
2312 which specifies how many buttons to use
2313 and how many buttons are on the right.
2314 The Q means something also. */
2315 dialog_name[0] = 'Q';
2316 dialog_name[1] = '0' + nb_buttons;
2317 dialog_name[2] = 'B';
2318 dialog_name[3] = 'R';
2319 /* Number of buttons to put on the right. */
2320 dialog_name[4] = '0' + nb_buttons - left_count;
2321 dialog_name[5] = 0;
2322 wv->contents = first_wv;
2323 first_wv = wv;
2326 /* Actually create the dialog. */
2327 dialog_id = widget_id_tick++;
2328 menu = lw_create_widget (first_wv->name, "dialog", dialog_id, first_wv,
2329 f->output_data.x->widget, 1, 0,
2330 dialog_selection_callback, 0);
2331 lw_modify_all_widgets (dialog_id, first_wv->contents, True);
2332 /* Free the widget_value objects we used to specify the contents. */
2333 free_menubar_widget_value_tree (first_wv);
2335 /* No selection has been chosen yet. */
2336 menu_item_selection = 0;
2338 /* Display the menu. */
2339 lw_pop_up_all_widgets (dialog_id);
2340 popup_activated_flag = 1;
2342 /* Process events that apply to the menu. */
2343 popup_get_selection ((XEvent *) 0, FRAME_X_DISPLAY_INFO (f), dialog_id);
2345 lw_destroy_all_widgets (dialog_id);
2347 /* Find the selected item, and its pane, to return
2348 the proper value. */
2349 if (menu_item_selection != 0)
2351 Lisp_Object prefix;
2353 prefix = Qnil;
2354 i = 0;
2355 while (i < menu_items_used)
2357 Lisp_Object entry;
2359 if (EQ (XVECTOR (menu_items)->contents[i], Qt))
2361 prefix
2362 = XVECTOR (menu_items)->contents[i + MENU_ITEMS_PANE_PREFIX];
2363 i += MENU_ITEMS_PANE_LENGTH;
2365 else
2367 entry
2368 = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_VALUE];
2369 if (menu_item_selection == &XVECTOR (menu_items)->contents[i])
2371 if (keymaps != 0)
2373 entry = Fcons (entry, Qnil);
2374 if (!NILP (prefix))
2375 entry = Fcons (prefix, entry);
2377 return entry;
2379 i += MENU_ITEMS_ITEM_LENGTH;
2384 return Qnil;
2386 #else /* not USE_X_TOOLKIT */
2388 static Lisp_Object
2389 xmenu_show (f, x, y, for_click, keymaps, title, error)
2390 FRAME_PTR f;
2391 int x, y;
2392 int for_click;
2393 int keymaps;
2394 Lisp_Object title;
2395 char **error;
2397 Window root;
2398 XMenu *menu;
2399 int pane, selidx, lpane, status;
2400 Lisp_Object entry, pane_prefix;
2401 char *datap;
2402 int ulx, uly, width, height;
2403 int dispwidth, dispheight;
2404 int i, j;
2405 int maxwidth;
2406 int dummy_int;
2407 unsigned int dummy_uint;
2409 *error = 0;
2410 if (menu_items_n_panes == 0)
2411 return Qnil;
2413 if (menu_items_used <= MENU_ITEMS_PANE_LENGTH)
2415 *error = "Empty menu";
2416 return Qnil;
2419 /* Figure out which root window F is on. */
2420 XGetGeometry (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), &root,
2421 &dummy_int, &dummy_int, &dummy_uint, &dummy_uint,
2422 &dummy_uint, &dummy_uint);
2424 /* Make the menu on that window. */
2425 menu = XMenuCreate (FRAME_X_DISPLAY (f), root, "emacs");
2426 if (menu == NULL)
2428 *error = "Can't create menu";
2429 return Qnil;
2432 #ifdef HAVE_X_WINDOWS
2433 /* Adjust coordinates to relative to the outer (window manager) window. */
2435 Window child;
2436 int win_x = 0, win_y = 0;
2438 /* Find the position of the outside upper-left corner of
2439 the inner window, with respect to the outer window. */
2440 if (f->output_data.x->parent_desc != FRAME_X_DISPLAY_INFO (f)->root_window)
2442 BLOCK_INPUT;
2443 XTranslateCoordinates (FRAME_X_DISPLAY (f),
2445 /* From-window, to-window. */
2446 f->output_data.x->window_desc,
2447 f->output_data.x->parent_desc,
2449 /* From-position, to-position. */
2450 0, 0, &win_x, &win_y,
2452 /* Child of window. */
2453 &child);
2454 UNBLOCK_INPUT;
2455 x += win_x;
2456 y += win_y;
2459 #endif /* HAVE_X_WINDOWS */
2461 /* Adjust coordinates to be root-window-relative. */
2462 x += f->output_data.x->left_pos;
2463 y += f->output_data.x->top_pos;
2465 /* Create all the necessary panes and their items. */
2466 i = 0;
2467 while (i < menu_items_used)
2469 if (EQ (XVECTOR (menu_items)->contents[i], Qt))
2471 /* Create a new pane. */
2472 Lisp_Object pane_name, prefix;
2473 char *pane_string;
2475 pane_name = XVECTOR (menu_items)->contents[i + MENU_ITEMS_PANE_NAME];
2476 prefix = XVECTOR (menu_items)->contents[i + MENU_ITEMS_PANE_PREFIX];
2477 pane_string = (NILP (pane_name)
2478 ? "" : (char *) XSTRING (pane_name)->data);
2479 if (keymaps && !NILP (prefix))
2480 pane_string++;
2482 lpane = XMenuAddPane (FRAME_X_DISPLAY (f), menu, pane_string, TRUE);
2483 if (lpane == XM_FAILURE)
2485 XMenuDestroy (FRAME_X_DISPLAY (f), menu);
2486 *error = "Can't create pane";
2487 return Qnil;
2489 i += MENU_ITEMS_PANE_LENGTH;
2491 /* Find the width of the widest item in this pane. */
2492 maxwidth = 0;
2493 j = i;
2494 while (j < menu_items_used)
2496 Lisp_Object item;
2497 item = XVECTOR (menu_items)->contents[j];
2498 if (EQ (item, Qt))
2499 break;
2500 if (NILP (item))
2502 j++;
2503 continue;
2505 width = XSTRING (item)->size;
2506 if (width > maxwidth)
2507 maxwidth = width;
2509 j += MENU_ITEMS_ITEM_LENGTH;
2512 /* Ignore a nil in the item list.
2513 It's meaningful only for dialog boxes. */
2514 else if (EQ (XVECTOR (menu_items)->contents[i], Qquote))
2515 i += 1;
2516 else
2518 /* Create a new item within current pane. */
2519 Lisp_Object item_name, enable, descrip;
2520 unsigned char *item_data;
2522 item_name = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_NAME];
2523 enable = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_ENABLE];
2524 descrip
2525 = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_EQUIV_KEY];
2526 if (!NILP (descrip))
2528 int gap = maxwidth - XSTRING (item_name)->size;
2529 #ifdef C_ALLOCA
2530 Lisp_Object spacer;
2531 spacer = Fmake_string (make_number (gap), make_number (' '));
2532 item_name = concat2 (item_name, spacer);
2533 item_name = concat2 (item_name, descrip);
2534 item_data = XSTRING (item_name)->data;
2535 #else
2536 /* if alloca is fast, use that to make the space,
2537 to reduce gc needs. */
2538 item_data
2539 = (unsigned char *) alloca (maxwidth
2540 + XSTRING (descrip)->size + 1);
2541 bcopy (XSTRING (item_name)->data, item_data,
2542 XSTRING (item_name)->size);
2543 for (j = XSTRING (item_name)->size; j < maxwidth; j++)
2544 item_data[j] = ' ';
2545 bcopy (XSTRING (descrip)->data, item_data + j,
2546 XSTRING (descrip)->size);
2547 item_data[j + XSTRING (descrip)->size] = 0;
2548 #endif
2550 else
2551 item_data = XSTRING (item_name)->data;
2553 if (XMenuAddSelection (FRAME_X_DISPLAY (f),
2554 menu, lpane, 0, item_data,
2555 !NILP (enable))
2556 == XM_FAILURE)
2558 XMenuDestroy (FRAME_X_DISPLAY (f), menu);
2559 *error = "Can't add selection to menu";
2560 return Qnil;
2562 i += MENU_ITEMS_ITEM_LENGTH;
2566 /* All set and ready to fly. */
2567 XMenuRecompute (FRAME_X_DISPLAY (f), menu);
2568 dispwidth = DisplayWidth (FRAME_X_DISPLAY (f),
2569 XScreenNumberOfScreen (FRAME_X_SCREEN (f)));
2570 dispheight = DisplayHeight (FRAME_X_DISPLAY (f),
2571 XScreenNumberOfScreen (FRAME_X_SCREEN (f)));
2572 x = min (x, dispwidth);
2573 y = min (y, dispheight);
2574 x = max (x, 1);
2575 y = max (y, 1);
2576 XMenuLocate (FRAME_X_DISPLAY (f), menu, 0, 0, x, y,
2577 &ulx, &uly, &width, &height);
2578 if (ulx+width > dispwidth)
2580 x -= (ulx + width) - dispwidth;
2581 ulx = dispwidth - width;
2583 if (uly+height > dispheight)
2585 y -= (uly + height) - dispheight;
2586 uly = dispheight - height;
2588 if (ulx < 0) x -= ulx;
2589 if (uly < 0) y -= uly;
2591 XMenuSetAEQ (menu, TRUE);
2592 XMenuSetFreeze (menu, TRUE);
2593 pane = selidx = 0;
2595 status = XMenuActivate (FRAME_X_DISPLAY (f), menu, &pane, &selidx,
2596 x, y, ButtonReleaseMask, &datap);
2599 #ifdef HAVE_X_WINDOWS
2600 /* Assume the mouse has moved out of the X window.
2601 If it has actually moved in, we will get an EnterNotify. */
2602 x_mouse_leave (FRAME_X_DISPLAY_INFO (f));
2603 #endif
2605 switch (status)
2607 case XM_SUCCESS:
2608 #ifdef XDEBUG
2609 fprintf (stderr, "pane= %d line = %d\n", panes, selidx);
2610 #endif
2612 /* Find the item number SELIDX in pane number PANE. */
2613 i = 0;
2614 while (i < menu_items_used)
2616 if (EQ (XVECTOR (menu_items)->contents[i], Qt))
2618 if (pane == 0)
2619 pane_prefix
2620 = XVECTOR (menu_items)->contents[i + MENU_ITEMS_PANE_PREFIX];
2621 pane--;
2622 i += MENU_ITEMS_PANE_LENGTH;
2624 else
2626 if (pane == -1)
2628 if (selidx == 0)
2630 entry
2631 = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_VALUE];
2632 if (keymaps != 0)
2634 entry = Fcons (entry, Qnil);
2635 if (!NILP (pane_prefix))
2636 entry = Fcons (pane_prefix, entry);
2638 break;
2640 selidx--;
2642 i += MENU_ITEMS_ITEM_LENGTH;
2645 break;
2647 case XM_FAILURE:
2648 *error = "Can't activate menu";
2649 case XM_IA_SELECT:
2650 case XM_NO_SELECT:
2651 entry = Qnil;
2652 break;
2654 XMenuDestroy (FRAME_X_DISPLAY (f), menu);
2656 #ifdef HAVE_X_WINDOWS
2657 /* State that no mouse buttons are now held.
2658 (The oldXMenu code doesn't track this info for us.)
2659 That is not necessarily true, but the fiction leads to reasonable
2660 results, and it is a pain to ask which are actually held now. */
2661 FRAME_X_DISPLAY_INFO (f)->grabbed = 0;
2662 #endif
2664 return entry;
2667 #endif /* not USE_X_TOOLKIT */
2669 #endif /* HAVE_MENUS */
2671 syms_of_xmenu ()
2673 staticpro (&menu_items);
2674 menu_items = Qnil;
2676 Qmenu_alias = intern ("menu-alias");
2677 staticpro (&Qmenu_alias);
2679 Qdebug_on_next_call = intern ("debug-on-next-call");
2680 staticpro (&Qdebug_on_next_call);
2682 #ifdef USE_X_TOOLKIT
2683 widget_id_tick = (1<<16);
2684 next_menubar_widget_id = 1;
2685 #endif
2687 defsubr (&Sx_popup_menu);
2688 #ifdef HAVE_MENUS
2689 defsubr (&Sx_popup_dialog);
2690 #endif