(bookmark-default-file): Use convert-standard-filename.
[emacs.git] / src / xmenu.c
blob3eb56ff3120aa9563e5e835bd4e8a1bba63668c2
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 /* This is set nonzero after the user activates the menu bar, and set
176 to zero again after the menu bars are redisplayed by prepare_menu_bar.
177 While it is nonzero, all calls to set_frame_menubar go deep.
179 I don't understand why this is needed, but it does seem to be
180 needed on Motif, according to Marcus Daniels <marcus@sysc.pdx.edu>. */
182 int pending_menu_activation;
184 #ifdef USE_X_TOOLKIT
186 /* Return the frame whose ->output_data.x->id equals ID, or 0 if none. */
188 static struct frame *
189 menubar_id_to_frame (id)
190 LWLIB_ID id;
192 Lisp_Object tail, frame;
193 FRAME_PTR f;
195 for (tail = Vframe_list; GC_CONSP (tail); tail = XCONS (tail)->cdr)
197 frame = XCONS (tail)->car;
198 if (!GC_FRAMEP (frame))
199 continue;
200 f = XFRAME (frame);
201 if (f->output_data.nothing == 1)
202 continue;
203 if (f->output_data.x->id == id)
204 return f;
206 return 0;
209 #endif
211 /* Initialize the menu_items structure if we haven't already done so.
212 Also mark it as currently empty. */
214 static void
215 init_menu_items ()
217 if (NILP (menu_items))
219 menu_items_allocated = 60;
220 menu_items = Fmake_vector (make_number (menu_items_allocated), Qnil);
223 menu_items_used = 0;
224 menu_items_n_panes = 0;
225 menu_items_submenu_depth = 0;
228 /* Call at the end of generating the data in menu_items.
229 This fills in the number of items in the last pane. */
231 static void
232 finish_menu_items ()
236 /* Call when finished using the data for the current menu
237 in menu_items. */
239 static void
240 discard_menu_items ()
242 /* Free the structure if it is especially large.
243 Otherwise, hold on to it, to save time. */
244 if (menu_items_allocated > 200)
246 menu_items = Qnil;
247 menu_items_allocated = 0;
251 /* Make the menu_items vector twice as large. */
253 static void
254 grow_menu_items ()
256 Lisp_Object old;
257 int old_size = menu_items_allocated;
258 old = menu_items;
260 menu_items_allocated *= 2;
261 menu_items = Fmake_vector (make_number (menu_items_allocated), Qnil);
262 bcopy (XVECTOR (old)->contents, XVECTOR (menu_items)->contents,
263 old_size * sizeof (Lisp_Object));
266 /* Begin a submenu. */
268 static void
269 push_submenu_start ()
271 if (menu_items_used + 1 > menu_items_allocated)
272 grow_menu_items ();
274 XVECTOR (menu_items)->contents[menu_items_used++] = Qnil;
275 menu_items_submenu_depth++;
278 /* End a submenu. */
280 static void
281 push_submenu_end ()
283 if (menu_items_used + 1 > menu_items_allocated)
284 grow_menu_items ();
286 XVECTOR (menu_items)->contents[menu_items_used++] = Qlambda;
287 menu_items_submenu_depth--;
290 /* Indicate boundary between left and right. */
292 static void
293 push_left_right_boundary ()
295 if (menu_items_used + 1 > menu_items_allocated)
296 grow_menu_items ();
298 XVECTOR (menu_items)->contents[menu_items_used++] = Qquote;
301 /* Start a new menu pane in menu_items..
302 NAME is the pane name. PREFIX_VEC is a prefix key for this pane. */
304 static void
305 push_menu_pane (name, prefix_vec)
306 Lisp_Object name, prefix_vec;
308 if (menu_items_used + MENU_ITEMS_PANE_LENGTH > menu_items_allocated)
309 grow_menu_items ();
311 if (menu_items_submenu_depth == 0)
312 menu_items_n_panes++;
313 XVECTOR (menu_items)->contents[menu_items_used++] = Qt;
314 XVECTOR (menu_items)->contents[menu_items_used++] = name;
315 XVECTOR (menu_items)->contents[menu_items_used++] = prefix_vec;
318 /* Push one menu item into the current pane.
319 NAME is the string to display. ENABLE if non-nil means
320 this item can be selected. KEY is the key generated by
321 choosing this item, or nil if this item doesn't really have a definition.
322 DEF is the definition of this item.
323 EQUIV is the textual description of the keyboard equivalent for
324 this item (or nil if none). */
326 static void
327 push_menu_item (name, enable, key, def, equiv)
328 Lisp_Object name, enable, key, def, equiv;
330 if (menu_items_used + MENU_ITEMS_ITEM_LENGTH > menu_items_allocated)
331 grow_menu_items ();
333 XVECTOR (menu_items)->contents[menu_items_used++] = name;
334 XVECTOR (menu_items)->contents[menu_items_used++] = enable;
335 XVECTOR (menu_items)->contents[menu_items_used++] = key;
336 XVECTOR (menu_items)->contents[menu_items_used++] = equiv;
337 XVECTOR (menu_items)->contents[menu_items_used++] = def;
340 /* Figure out the current keyboard equivalent of a menu item ITEM1.
341 The item string for menu display should be ITEM_STRING.
342 Store the equivalent keyboard key sequence's
343 textual description into *DESCRIP_PTR.
344 Also cache them in the item itself.
345 Return the real definition to execute. */
347 static Lisp_Object
348 menu_item_equiv_key (item_string, item1, descrip_ptr)
349 Lisp_Object item_string;
350 Lisp_Object item1;
351 Lisp_Object *descrip_ptr;
353 /* This is the real definition--the function to run. */
354 Lisp_Object def;
355 /* This is the sublist that records cached equiv key data
356 so we can save time. */
357 Lisp_Object cachelist;
358 /* These are the saved equivalent keyboard key sequence
359 and its key-description. */
360 Lisp_Object savedkey, descrip;
361 Lisp_Object def1;
362 int changed = 0;
363 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
365 /* If a help string follows the item string, skip it. */
366 if (CONSP (XCONS (item1)->cdr)
367 && STRINGP (XCONS (XCONS (item1)->cdr)->car))
368 item1 = XCONS (item1)->cdr;
370 def = Fcdr (item1);
372 /* Get out the saved equivalent-keyboard-key info. */
373 cachelist = savedkey = descrip = Qnil;
374 if (CONSP (def) && CONSP (XCONS (def)->car)
375 && (NILP (XCONS (XCONS (def)->car)->car)
376 || VECTORP (XCONS (XCONS (def)->car)->car)))
378 cachelist = XCONS (def)->car;
379 def = XCONS (def)->cdr;
380 savedkey = XCONS (cachelist)->car;
381 descrip = XCONS (cachelist)->cdr;
384 GCPRO4 (def, def1, savedkey, descrip);
386 /* Is it still valid? */
387 def1 = Qnil;
388 if (!NILP (savedkey))
389 def1 = Fkey_binding (savedkey, Qnil);
390 /* If not, update it. */
391 if (! EQ (def1, def)
392 /* If the command is an alias for another
393 (such as easymenu.el and lmenu.el set it up),
394 check if the original command matches the cached command. */
395 && !(SYMBOLP (def) && SYMBOLP (XSYMBOL (def)->function)
396 && EQ (def1, XSYMBOL (def)->function))
397 /* If something had no key binding before, don't recheck it
398 because that is too slow--except if we have a list of rebound
399 commands in Vdefine_key_rebound_commands, do recheck any command
400 that appears in that list. */
401 && (NILP (cachelist) || !NILP (savedkey)
402 || (! EQ (Qt, Vdefine_key_rebound_commands)
403 && !NILP (Fmemq (def, Vdefine_key_rebound_commands)))))
405 changed = 1;
406 descrip = Qnil;
407 /* If the command is an alias for another
408 (such as easymenu.el and lmenu.el set it up),
409 see if the original command name has equivalent keys. */
410 if (SYMBOLP (def) && SYMBOLP (XSYMBOL (def)->function)
411 && ! NILP (Fget (def, Qmenu_alias)))
412 savedkey = Fwhere_is_internal (XSYMBOL (def)->function,
413 Qnil, Qt, Qnil);
414 else
415 /* Otherwise look up the specified command itself.
416 We don't try both, because that makes easymenu menus slow. */
417 savedkey = Fwhere_is_internal (def, Qnil, Qt, Qnil);
419 if (!NILP (savedkey))
421 descrip = Fkey_description (savedkey);
422 descrip = concat2 (make_string (" (", 3), descrip);
423 descrip = concat2 (descrip, make_string (")", 1));
427 /* Cache the data we just got in a sublist of the menu binding. */
428 if (NILP (cachelist))
430 CHECK_IMPURE (item1);
431 XCONS (item1)->cdr = Fcons (Fcons (savedkey, descrip), def);
433 else if (changed)
435 XCONS (cachelist)->car = savedkey;
436 XCONS (cachelist)->cdr = descrip;
439 UNGCPRO;
440 *descrip_ptr = descrip;
441 return def;
444 /* This is used as the handler when calling internal_condition_case_1. */
446 static Lisp_Object
447 menu_item_enabled_p_1 (arg)
448 Lisp_Object arg;
450 /* If we got a quit from within the menu computation,
451 quit all the way out of it. This takes care of C-] in the debugger. */
452 if (CONSP (arg) && EQ (XCONS (arg)->car, Qquit))
453 Fsignal (Qquit, Qnil);
455 return Qnil;
458 /* Return non-nil if the command DEF is enabled when used as a menu item.
459 This is based on looking for a menu-enable property.
460 If NOTREAL is set, don't bother really computing this. */
462 static Lisp_Object
463 menu_item_enabled_p (def, notreal)
464 Lisp_Object def;
465 int notreal;
467 Lisp_Object enabled, tem;
469 enabled = Qt;
470 if (notreal)
471 return enabled;
472 if (SYMBOLP (def))
474 /* No property, or nil, means enable.
475 Otherwise, enable if value is not nil. */
476 tem = Fget (def, Qmenu_enable);
477 if (!NILP (tem))
478 /* (condition-case nil (eval tem)
479 (error nil)) */
480 enabled = internal_condition_case_1 (Feval, tem, Qerror,
481 menu_item_enabled_p_1);
483 return enabled;
486 /* Look through KEYMAPS, a vector of keymaps that is NMAPS long,
487 and generate menu panes for them in menu_items.
488 If NOTREAL is nonzero,
489 don't bother really computing whether an item is enabled. */
491 static void
492 keymap_panes (keymaps, nmaps, notreal)
493 Lisp_Object *keymaps;
494 int nmaps;
495 int notreal;
497 int mapno;
499 init_menu_items ();
501 /* Loop over the given keymaps, making a pane for each map.
502 But don't make a pane that is empty--ignore that map instead.
503 P is the number of panes we have made so far. */
504 for (mapno = 0; mapno < nmaps; mapno++)
505 single_keymap_panes (keymaps[mapno], Qnil, Qnil, notreal);
507 finish_menu_items ();
510 /* This is a recursive subroutine of keymap_panes.
511 It handles one keymap, KEYMAP.
512 The other arguments are passed along
513 or point to local variables of the previous function.
514 If NOTREAL is nonzero,
515 don't bother really computing whether an item is enabled. */
517 static void
518 single_keymap_panes (keymap, pane_name, prefix, notreal)
519 Lisp_Object keymap;
520 Lisp_Object pane_name;
521 Lisp_Object prefix;
522 int notreal;
524 Lisp_Object pending_maps;
525 Lisp_Object tail, item, item1, item_string, table;
526 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
528 pending_maps = Qnil;
530 push_menu_pane (pane_name, prefix);
532 for (tail = keymap; CONSP (tail); tail = XCONS (tail)->cdr)
534 /* Look at each key binding, and if it has a menu string,
535 make a menu item from it. */
536 item = XCONS (tail)->car;
537 if (CONSP (item))
539 item1 = XCONS (item)->cdr;
540 if (CONSP (item1))
542 item_string = XCONS (item1)->car;
543 if (STRINGP (item_string))
545 /* This is the real definition--the function to run. */
546 Lisp_Object def;
547 /* These are the saved equivalent keyboard key sequence
548 and its key-description. */
549 Lisp_Object descrip;
550 Lisp_Object tem, enabled;
552 /* GCPRO because ...enabled_p will call eval
553 and ..._equiv_key may autoload something.
554 Protecting KEYMAP preserves everything we use;
555 aside from that, must protect whatever might be
556 a string. Since there's no GCPRO5, we refetch
557 item_string instead of protecting it. */
558 descrip = def = Qnil;
559 GCPRO4 (keymap, pending_maps, def, descrip);
561 def = menu_item_equiv_key (item_string, item1, &descrip);
562 enabled = menu_item_enabled_p (def, notreal);
564 UNGCPRO;
566 item_string = XCONS (item1)->car;
568 tem = Fkeymapp (def);
569 if (XSTRING (item_string)->data[0] == '@' && !NILP (tem))
570 pending_maps = Fcons (Fcons (def, Fcons (item_string, XCONS (item)->car)),
571 pending_maps);
572 else
574 Lisp_Object submap;
575 GCPRO4 (keymap, pending_maps, descrip, item_string);
576 submap = get_keymap_1 (def, 0, 1);
577 UNGCPRO;
578 #ifndef USE_X_TOOLKIT
579 /* Indicate visually that this is a submenu. */
580 if (!NILP (submap))
581 item_string = concat2 (item_string,
582 build_string (" >"));
583 #endif
584 /* If definition is nil, pass nil as the key. */
585 push_menu_item (item_string, enabled,
586 XCONS (item)->car, def,
587 descrip);
588 #ifdef USE_X_TOOLKIT
589 /* Display a submenu using the toolkit. */
590 if (! NILP (submap))
592 push_submenu_start ();
593 single_keymap_panes (submap, Qnil,
594 XCONS (item)->car, notreal);
595 push_submenu_end ();
597 #endif
602 else if (VECTORP (item))
604 /* Loop over the char values represented in the vector. */
605 int len = XVECTOR (item)->size;
606 int c;
607 for (c = 0; c < len; c++)
609 Lisp_Object character;
610 XSETFASTINT (character, c);
611 item1 = XVECTOR (item)->contents[c];
612 if (CONSP (item1))
614 item_string = XCONS (item1)->car;
615 if (STRINGP (item_string))
617 Lisp_Object def;
619 /* These are the saved equivalent keyboard key sequence
620 and its key-description. */
621 Lisp_Object descrip;
622 Lisp_Object tem, enabled;
624 /* GCPRO because ...enabled_p will call eval
625 and ..._equiv_key may autoload something.
626 Protecting KEYMAP preserves everything we use;
627 aside from that, must protect whatever might be
628 a string. Since there's no GCPRO5, we refetch
629 item_string instead of protecting it. */
630 GCPRO4 (keymap, pending_maps, def, descrip);
631 descrip = def = Qnil;
633 def = menu_item_equiv_key (item_string, item1, &descrip);
634 enabled = menu_item_enabled_p (def, notreal);
636 UNGCPRO;
638 item_string = XCONS (item1)->car;
640 tem = Fkeymapp (def);
641 if (XSTRING (item_string)->data[0] == '@' && !NILP (tem))
642 pending_maps = Fcons (Fcons (def, Fcons (item_string, character)),
643 pending_maps);
644 else
646 Lisp_Object submap;
647 GCPRO4 (keymap, pending_maps, descrip, item_string);
648 submap = get_keymap_1 (def, 0, 1);
649 UNGCPRO;
650 #ifndef USE_X_TOOLKIT
651 if (!NILP (submap))
652 item_string = concat2 (item_string,
653 build_string (" >"));
654 #endif
655 /* If definition is nil, pass nil as the key. */
656 push_menu_item (item_string, enabled, character,
657 def, descrip);
658 #ifdef USE_X_TOOLKIT
659 if (! NILP (submap))
661 push_submenu_start ();
662 single_keymap_panes (submap, Qnil,
663 character, notreal);
664 push_submenu_end ();
666 #endif
674 /* Process now any submenus which want to be panes at this level. */
675 while (!NILP (pending_maps))
677 Lisp_Object elt, eltcdr, string;
678 elt = Fcar (pending_maps);
679 eltcdr = XCONS (elt)->cdr;
680 string = XCONS (eltcdr)->car;
681 /* We no longer discard the @ from the beginning of the string here.
682 Instead, we do this in xmenu_show. */
683 single_keymap_panes (Fcar (elt), string,
684 XCONS (eltcdr)->cdr, notreal);
685 pending_maps = Fcdr (pending_maps);
689 /* Push all the panes and items of a menu described by the
690 alist-of-alists MENU.
691 This handles old-fashioned calls to x-popup-menu. */
693 static void
694 list_of_panes (menu)
695 Lisp_Object menu;
697 Lisp_Object tail;
699 init_menu_items ();
701 for (tail = menu; !NILP (tail); tail = Fcdr (tail))
703 Lisp_Object elt, pane_name, pane_data;
704 elt = Fcar (tail);
705 pane_name = Fcar (elt);
706 CHECK_STRING (pane_name, 0);
707 push_menu_pane (pane_name, Qnil);
708 pane_data = Fcdr (elt);
709 CHECK_CONS (pane_data, 0);
710 list_of_items (pane_data);
713 finish_menu_items ();
716 /* Push the items in a single pane defined by the alist PANE. */
718 static void
719 list_of_items (pane)
720 Lisp_Object pane;
722 Lisp_Object tail, item, item1;
724 for (tail = pane; !NILP (tail); tail = Fcdr (tail))
726 item = Fcar (tail);
727 if (STRINGP (item))
728 push_menu_item (item, Qnil, Qnil, Qt, Qnil);
729 else if (NILP (item))
730 push_left_right_boundary ();
731 else
733 CHECK_CONS (item, 0);
734 item1 = Fcar (item);
735 CHECK_STRING (item1, 1);
736 push_menu_item (item1, Qt, Fcdr (item), Qt, Qnil);
741 DEFUN ("x-popup-menu", Fx_popup_menu, Sx_popup_menu, 2, 2, 0,
742 "Pop up a deck-of-cards menu and return user's selection.\n\
743 POSITION is a position specification. This is either a mouse button event\n\
744 or a list ((XOFFSET YOFFSET) WINDOW)\n\
745 where XOFFSET and YOFFSET are positions in pixels from the top left\n\
746 corner of WINDOW's frame. (WINDOW may be a frame object instead of a window.)\n\
747 This controls the position of the center of the first line\n\
748 in the first pane of the menu, not the top left of the menu as a whole.\n\
749 If POSITION is t, it means to use the current mouse position.\n\
751 MENU is a specifier for a menu. For the simplest case, MENU is a keymap.\n\
752 The menu items come from key bindings that have a menu string as well as\n\
753 a definition; actually, the \"definition\" in such a key binding looks like\n\
754 \(STRING . REAL-DEFINITION). To give the menu a title, put a string into\n\
755 the keymap as a top-level element.\n\n\
756 You can also use a list of keymaps as MENU.\n\
757 Then each keymap makes a separate pane.\n\
758 When MENU is a keymap or a list of keymaps, the return value\n\
759 is a list of events.\n\n\
760 Alternatively, you can specify a menu of multiple panes\n\
761 with a list of the form (TITLE PANE1 PANE2...),\n\
762 where each pane is a list of form (TITLE ITEM1 ITEM2...).\n\
763 Each ITEM is normally a cons cell (STRING . VALUE);\n\
764 but a string can appear as an item--that makes a nonselectable line\n\
765 in the menu.\n\
766 With this form of menu, the return value is VALUE from the chosen item.\n\
768 If POSITION is nil, don't display the menu at all, just precalculate the\n\
769 cached information about equivalent key sequences.")
770 (position, menu)
771 Lisp_Object position, menu;
773 int number_of_panes, panes;
774 Lisp_Object keymap, tem;
775 int xpos, ypos;
776 Lisp_Object title;
777 char *error_name;
778 Lisp_Object selection;
779 int i, j;
780 FRAME_PTR f;
781 Lisp_Object x, y, window;
782 int keymaps = 0;
783 int for_click = 0;
784 struct gcpro gcpro1;
786 #ifdef HAVE_MENUS
787 if (! NILP (position))
789 check_x ();
791 /* Decode the first argument: find the window and the coordinates. */
792 if (EQ (position, Qt)
793 || (CONSP (position) && EQ (XCONS (position)->car, Qmenu_bar)))
795 /* Use the mouse's current position. */
796 FRAME_PTR new_f = selected_frame;
797 Lisp_Object bar_window;
798 int part;
799 unsigned long time;
801 if (mouse_position_hook)
802 (*mouse_position_hook) (&new_f, 1, &bar_window,
803 &part, &x, &y, &time);
804 if (new_f != 0)
805 XSETFRAME (window, new_f);
806 else
808 window = selected_window;
809 XSETFASTINT (x, 0);
810 XSETFASTINT (y, 0);
813 else
815 tem = Fcar (position);
816 if (CONSP (tem))
818 window = Fcar (Fcdr (position));
819 x = Fcar (tem);
820 y = Fcar (Fcdr (tem));
822 else
824 for_click = 1;
825 tem = Fcar (Fcdr (position)); /* EVENT_START (position) */
826 window = Fcar (tem); /* POSN_WINDOW (tem) */
827 tem = Fcar (Fcdr (Fcdr (tem))); /* POSN_WINDOW_POSN (tem) */
828 x = Fcar (tem);
829 y = Fcdr (tem);
833 CHECK_NUMBER (x, 0);
834 CHECK_NUMBER (y, 0);
836 /* Decode where to put the menu. */
838 if (FRAMEP (window))
840 f = XFRAME (window);
841 xpos = 0;
842 ypos = 0;
844 else if (WINDOWP (window))
846 CHECK_LIVE_WINDOW (window, 0);
847 f = XFRAME (WINDOW_FRAME (XWINDOW (window)));
849 xpos = (FONT_WIDTH (f->output_data.x->font) * XWINDOW (window)->left);
850 ypos = (f->output_data.x->line_height * XWINDOW (window)->top);
852 else
853 /* ??? Not really clean; should be CHECK_WINDOW_OR_FRAME,
854 but I don't want to make one now. */
855 CHECK_WINDOW (window, 0);
857 xpos += XINT (x);
858 ypos += XINT (y);
860 #endif /* HAVE_MENUS */
862 title = Qnil;
863 GCPRO1 (title);
865 /* Decode the menu items from what was specified. */
867 keymap = Fkeymapp (menu);
868 tem = Qnil;
869 if (CONSP (menu))
870 tem = Fkeymapp (Fcar (menu));
871 if (!NILP (keymap))
873 /* We were given a keymap. Extract menu info from the keymap. */
874 Lisp_Object prompt;
875 keymap = get_keymap (menu);
877 /* Extract the detailed info to make one pane. */
878 keymap_panes (&menu, 1, NILP (position));
880 /* Search for a string appearing directly as an element of the keymap.
881 That string is the title of the menu. */
882 prompt = map_prompt (keymap);
884 /* Make that be the pane title of the first pane. */
885 if (!NILP (prompt) && menu_items_n_panes >= 0)
886 XVECTOR (menu_items)->contents[MENU_ITEMS_PANE_NAME] = prompt;
888 keymaps = 1;
890 else if (!NILP (tem))
892 /* We were given a list of keymaps. */
893 int nmaps = XFASTINT (Flength (menu));
894 Lisp_Object *maps
895 = (Lisp_Object *) alloca (nmaps * sizeof (Lisp_Object));
896 int i;
898 title = Qnil;
900 /* The first keymap that has a prompt string
901 supplies the menu title. */
902 for (tem = menu, i = 0; CONSP (tem); tem = Fcdr (tem))
904 Lisp_Object prompt;
906 maps[i++] = keymap = get_keymap (Fcar (tem));
908 prompt = map_prompt (keymap);
909 if (NILP (title) && !NILP (prompt))
910 title = prompt;
913 /* Extract the detailed info to make one pane. */
914 keymap_panes (maps, nmaps, NILP (position));
916 /* Make the title be the pane title of the first pane. */
917 if (!NILP (title) && menu_items_n_panes >= 0)
918 XVECTOR (menu_items)->contents[MENU_ITEMS_PANE_NAME] = title;
920 keymaps = 1;
922 else
924 /* We were given an old-fashioned menu. */
925 title = Fcar (menu);
926 CHECK_STRING (title, 1);
928 list_of_panes (Fcdr (menu));
930 keymaps = 0;
933 if (NILP (position))
935 discard_menu_items ();
936 UNGCPRO;
937 return Qnil;
940 #ifdef HAVE_MENUS
941 /* Display them in a menu. */
942 BLOCK_INPUT;
944 selection = xmenu_show (f, xpos, ypos, for_click,
945 keymaps, title, &error_name);
946 UNBLOCK_INPUT;
948 discard_menu_items ();
950 UNGCPRO;
951 #endif /* HAVE_MENUS */
953 if (error_name) error (error_name);
954 return selection;
957 #ifdef HAVE_MENUS
959 DEFUN ("x-popup-dialog", Fx_popup_dialog, Sx_popup_dialog, 2, 2, 0,
960 "Pop up a dialog box and return user's selection.\n\
961 POSITION specifies which frame to use.\n\
962 This is normally a mouse button event or a window or frame.\n\
963 If POSITION is t, it means to use the frame the mouse is on.\n\
964 The dialog box appears in the middle of the specified frame.\n\
966 CONTENTS specifies the alternatives to display in the dialog box.\n\
967 It is a list of the form (TITLE ITEM1 ITEM2...).\n\
968 Each ITEM is a cons cell (STRING . VALUE).\n\
969 The return value is VALUE from the chosen item.\n\n\
970 An ITEM may also be just a string--that makes a nonselectable item.\n\
971 An ITEM may also be nil--that means to put all preceding items\n\
972 on the left of the dialog box and all following items on the right.\n\
973 \(By default, approximately half appear on each side.)")
974 (position, contents)
975 Lisp_Object position, contents;
977 FRAME_PTR f;
978 Lisp_Object window;
980 check_x ();
982 /* Decode the first argument: find the window or frame to use. */
983 if (EQ (position, Qt)
984 || (CONSP (position) && EQ (XCONS (position)->car, Qmenu_bar)))
986 #if 0 /* Using the frame the mouse is on may not be right. */
987 /* Use the mouse's current position. */
988 FRAME_PTR new_f = selected_frame;
989 Lisp_Object bar_window;
990 int part;
991 unsigned long time;
992 Lisp_Object x, y;
994 (*mouse_position_hook) (&new_f, 1, &bar_window, &part, &x, &y, &time);
996 if (new_f != 0)
997 XSETFRAME (window, new_f);
998 else
999 window = selected_window;
1000 #endif
1001 window = selected_window;
1003 else if (CONSP (position))
1005 Lisp_Object tem;
1006 tem = Fcar (position);
1007 if (CONSP (tem))
1008 window = Fcar (Fcdr (position));
1009 else
1011 tem = Fcar (Fcdr (position)); /* EVENT_START (position) */
1012 window = Fcar (tem); /* POSN_WINDOW (tem) */
1015 else if (WINDOWP (position) || FRAMEP (position))
1016 window = position;
1017 else
1018 window = Qnil;
1020 /* Decode where to put the menu. */
1022 if (FRAMEP (window))
1023 f = XFRAME (window);
1024 else if (WINDOWP (window))
1026 CHECK_LIVE_WINDOW (window, 0);
1027 f = XFRAME (WINDOW_FRAME (XWINDOW (window)));
1029 else
1030 /* ??? Not really clean; should be CHECK_WINDOW_OR_FRAME,
1031 but I don't want to make one now. */
1032 CHECK_WINDOW (window, 0);
1034 #ifndef USE_X_TOOLKIT
1035 /* Display a menu with these alternatives
1036 in the middle of frame F. */
1038 Lisp_Object x, y, frame, newpos;
1039 XSETFRAME (frame, f);
1040 XSETINT (x, x_pixel_width (f) / 2);
1041 XSETINT (y, x_pixel_height (f) / 2);
1042 newpos = Fcons (Fcons (x, Fcons (y, Qnil)), Fcons (frame, Qnil));
1044 return Fx_popup_menu (newpos,
1045 Fcons (Fcar (contents), Fcons (contents, Qnil)));
1047 #else
1049 Lisp_Object title;
1050 char *error_name;
1051 Lisp_Object selection;
1053 /* Decode the dialog items from what was specified. */
1054 title = Fcar (contents);
1055 CHECK_STRING (title, 1);
1057 list_of_panes (Fcons (contents, Qnil));
1059 /* Display them in a dialog box. */
1060 BLOCK_INPUT;
1061 selection = xdialog_show (f, 0, title, &error_name);
1062 UNBLOCK_INPUT;
1064 discard_menu_items ();
1066 if (error_name) error (error_name);
1067 return selection;
1069 #endif
1072 #ifdef USE_X_TOOLKIT
1074 /* Loop in Xt until the menu pulldown or dialog popup has been
1075 popped down (deactivated). This is used for x-popup-menu
1076 and x-popup-dialog; it is not used for the menu bar any more.
1078 NOTE: All calls to popup_get_selection should be protected
1079 with BLOCK_INPUT, UNBLOCK_INPUT wrappers. */
1081 void
1082 popup_get_selection (initial_event, dpyinfo, id)
1083 XEvent *initial_event;
1084 struct x_display_info *dpyinfo;
1085 LWLIB_ID id;
1087 XEvent event;
1089 /* Define a queue to save up for later unreading
1090 all X events that don't pertain to the menu. */
1091 struct event_queue
1093 XEvent event;
1094 struct event_queue *next;
1097 struct event_queue *queue = NULL;
1098 struct event_queue *queue_tmp;
1100 if (initial_event)
1101 event = *initial_event;
1102 else
1103 XtAppNextEvent (Xt_app_con, &event);
1105 while (1)
1107 /* Handle expose events for editor frames right away. */
1108 if (event.type == Expose)
1109 process_expose_from_menu (event);
1110 /* Make sure we don't consider buttons grabbed after menu goes.
1111 And make sure to deactivate for any ButtonRelease,
1112 even if XtDispatchEvent doesn't do that. */
1113 else if (event.type == ButtonRelease
1114 && dpyinfo->display == event.xbutton.display)
1116 dpyinfo->grabbed &= ~(1 << event.xbutton.button);
1117 popup_activated_flag = 0;
1118 #ifdef USE_MOTIF /* Pretending that the event came from a
1119 Btn1Down seems the only way to convince Motif to
1120 activate its callbacks; setting the XmNmenuPost
1121 isn't working. --marcus@sysc.pdx.edu. */
1122 event.xbutton.button = 1;
1123 #endif
1125 /* If the user presses a key, deactivate the menu.
1126 The user is likely to do that if we get wedged. */
1127 else if (event.type == KeyPress
1128 && dpyinfo->display == event.xbutton.display)
1130 popup_activated_flag = 0;
1131 break;
1133 /* Button presses outside the menu also pop it down. */
1134 else if (event.type == ButtonPress
1135 && event.xany.display == dpyinfo->display
1136 && x_any_window_to_frame (dpyinfo, event.xany.window))
1138 popup_activated_flag = 0;
1139 break;
1142 /* Queue all events not for this popup,
1143 except for Expose, which we've already handled, and ButtonRelease.
1144 Note that the X window is associated with the frame if this
1145 is a menu bar popup, but not if it's a dialog box. So we use
1146 x_non_menubar_window_to_frame, not x_any_window_to_frame. */
1147 if (event.type != Expose
1148 && !(event.type == ButtonRelease
1149 && dpyinfo->display == event.xbutton.display)
1150 && (event.xany.display != dpyinfo->display
1151 || x_non_menubar_window_to_frame (dpyinfo, event.xany.window)))
1153 queue_tmp = (struct event_queue *) malloc (sizeof (struct event_queue));
1155 if (queue_tmp != NULL)
1157 queue_tmp->event = event;
1158 queue_tmp->next = queue;
1159 queue = queue_tmp;
1162 else
1163 XtDispatchEvent (&event);
1165 if (!popup_activated ())
1166 break;
1167 XtAppNextEvent (Xt_app_con, &event);
1170 /* Unread any events that we got but did not handle. */
1171 while (queue != NULL)
1173 queue_tmp = queue;
1174 XPutBackEvent (queue_tmp->event.xany.display, &queue_tmp->event);
1175 queue = queue_tmp->next;
1176 free ((char *)queue_tmp);
1177 /* Cause these events to get read as soon as we UNBLOCK_INPUT. */
1178 interrupt_input_pending = 1;
1182 /* Activate the menu bar of frame F.
1183 This is called from keyboard.c when it gets the
1184 menu_bar_activate_event out of the Emacs event queue.
1186 To activate the menu bar, we use the X button-press event
1187 that was saved in saved_menu_event.
1188 That makes the toolkit do its thing.
1190 But first we recompute the menu bar contents (the whole tree).
1192 The reason for saving the button event until here, instead of
1193 passing it to the toolkit right away, is that we can safely
1194 execute Lisp code. */
1196 x_activate_menubar (f)
1197 FRAME_PTR f;
1199 if (!f->output_data.x->saved_menu_event->type)
1200 return;
1202 set_frame_menubar (f, 0, 1);
1203 BLOCK_INPUT;
1204 XtDispatchEvent ((XEvent *) f->output_data.x->saved_menu_event);
1205 UNBLOCK_INPUT;
1206 #ifdef USE_MOTIF
1207 if (f->output_data.x->saved_menu_event->type == ButtonRelease)
1208 pending_menu_activation = 1;
1209 #endif
1211 /* Ignore this if we get it a second time. */
1212 f->output_data.x->saved_menu_event->type = 0;
1215 /* Detect if a dialog or menu has been posted. */
1218 popup_activated ()
1220 return popup_activated_flag;
1224 /* This callback is invoked when the user selects a menubar cascade
1225 pushbutton, but before the pulldown menu is posted. */
1227 static void
1228 popup_activate_callback (widget, id, client_data)
1229 Widget widget;
1230 LWLIB_ID id;
1231 XtPointer client_data;
1233 popup_activated_flag = 1;
1236 /* This callback is called from the menu bar pulldown menu
1237 when the user makes a selection.
1238 Figure out what the user chose
1239 and put the appropriate events into the keyboard buffer. */
1241 static void
1242 menubar_selection_callback (widget, id, client_data)
1243 Widget widget;
1244 LWLIB_ID id;
1245 XtPointer client_data;
1247 Lisp_Object prefix, entry;
1248 FRAME_PTR f = menubar_id_to_frame (id);
1249 Lisp_Object vector;
1250 Lisp_Object *subprefix_stack;
1251 int submenu_depth = 0;
1252 int i;
1254 if (!f)
1255 return;
1256 subprefix_stack = (Lisp_Object *) alloca (f->menu_bar_items_used * sizeof (Lisp_Object));
1257 vector = f->menu_bar_vector;
1258 prefix = Qnil;
1259 i = 0;
1260 while (i < f->menu_bar_items_used)
1262 if (EQ (XVECTOR (vector)->contents[i], Qnil))
1264 subprefix_stack[submenu_depth++] = prefix;
1265 prefix = entry;
1266 i++;
1268 else if (EQ (XVECTOR (vector)->contents[i], Qlambda))
1270 prefix = subprefix_stack[--submenu_depth];
1271 i++;
1273 else if (EQ (XVECTOR (vector)->contents[i], Qt))
1275 prefix = XVECTOR (vector)->contents[i + MENU_ITEMS_PANE_PREFIX];
1276 i += MENU_ITEMS_PANE_LENGTH;
1278 else
1280 entry = XVECTOR (vector)->contents[i + MENU_ITEMS_ITEM_VALUE];
1281 /* The EMACS_INT cast avoids a warning. There's no problem
1282 as long as pointers have enough bits to hold small integers. */
1283 if ((int) (EMACS_INT) client_data == i)
1285 int j;
1286 struct input_event buf;
1287 Lisp_Object frame;
1289 XSETFRAME (frame, f);
1290 buf.kind = menu_bar_event;
1291 buf.frame_or_window = Fcons (frame, Fcons (Qmenu_bar, Qnil));
1292 kbd_buffer_store_event (&buf);
1294 for (j = 0; j < submenu_depth; j++)
1295 if (!NILP (subprefix_stack[j]))
1297 buf.kind = menu_bar_event;
1298 buf.frame_or_window = Fcons (frame, subprefix_stack[j]);
1299 kbd_buffer_store_event (&buf);
1302 if (!NILP (prefix))
1304 buf.kind = menu_bar_event;
1305 buf.frame_or_window = Fcons (frame, prefix);
1306 kbd_buffer_store_event (&buf);
1309 buf.kind = menu_bar_event;
1310 buf.frame_or_window = Fcons (frame, entry);
1311 kbd_buffer_store_event (&buf);
1313 return;
1315 i += MENU_ITEMS_ITEM_LENGTH;
1320 /* This callback is invoked when a dialog or menu is finished being
1321 used and has been unposted. */
1323 static void
1324 popup_deactivate_callback (widget, id, client_data)
1325 Widget widget;
1326 LWLIB_ID id;
1327 XtPointer client_data;
1329 popup_activated_flag = 0;
1332 /* Allocate a widget_value, blocking input. */
1334 widget_value *
1335 xmalloc_widget_value ()
1337 widget_value *value;
1339 BLOCK_INPUT;
1340 value = malloc_widget_value ();
1341 UNBLOCK_INPUT;
1343 return value;
1346 /* This recursively calls free_widget_value on the tree of widgets.
1347 It must free all data that was malloc'ed for these widget_values.
1348 In Emacs, many slots are pointers into the data of Lisp_Strings, and
1349 must be left alone. */
1351 void
1352 free_menubar_widget_value_tree (wv)
1353 widget_value *wv;
1355 if (! wv) return;
1357 wv->name = wv->value = wv->key = (char *) 0xDEADBEEF;
1359 if (wv->contents && (wv->contents != (widget_value*)1))
1361 free_menubar_widget_value_tree (wv->contents);
1362 wv->contents = (widget_value *) 0xDEADBEEF;
1364 if (wv->next)
1366 free_menubar_widget_value_tree (wv->next);
1367 wv->next = (widget_value *) 0xDEADBEEF;
1369 BLOCK_INPUT;
1370 free_widget_value (wv);
1371 UNBLOCK_INPUT;
1374 /* Return a tree of widget_value structures for a menu bar item
1375 whose event type is ITEM_KEY (with string ITEM_NAME)
1376 and whose contents come from the list of keymaps MAPS. */
1378 static widget_value *
1379 single_submenu (item_key, item_name, maps)
1380 Lisp_Object item_key, item_name, maps;
1382 widget_value *wv, *prev_wv, *save_wv, *first_wv;
1383 int i;
1384 int submenu_depth = 0;
1385 Lisp_Object length;
1386 int len;
1387 Lisp_Object *mapvec;
1388 widget_value **submenu_stack;
1389 int mapno;
1390 int previous_items = menu_items_used;
1391 int top_level_items = 0;
1393 length = Flength (maps);
1394 len = XINT (length);
1396 /* Convert the list MAPS into a vector MAPVEC. */
1397 mapvec = (Lisp_Object *) alloca (len * sizeof (Lisp_Object));
1398 for (i = 0; i < len; i++)
1400 mapvec[i] = Fcar (maps);
1401 maps = Fcdr (maps);
1404 menu_items_n_panes = 0;
1406 /* Loop over the given keymaps, making a pane for each map.
1407 But don't make a pane that is empty--ignore that map instead. */
1408 for (i = 0; i < len; i++)
1410 if (SYMBOLP (mapvec[i])
1411 || (CONSP (mapvec[i])
1412 && NILP (Fkeymapp (mapvec[i]))))
1414 /* Here we have a command at top level in the menu bar
1415 as opposed to a submenu. */
1416 top_level_items = 1;
1417 push_menu_pane (Qnil, Qnil);
1418 push_menu_item (item_name, Qt, item_key, mapvec[i], Qnil);
1420 else
1421 single_keymap_panes (mapvec[i], item_name, item_key, 0);
1424 /* Create a tree of widget_value objects
1425 representing the panes and their items. */
1427 submenu_stack
1428 = (widget_value **) alloca (menu_items_used * sizeof (widget_value *));
1429 wv = xmalloc_widget_value ();
1430 wv->name = "menu";
1431 wv->value = 0;
1432 wv->enabled = 1;
1433 first_wv = wv;
1434 save_wv = 0;
1435 prev_wv = 0;
1437 /* Loop over all panes and items made during this call
1438 and construct a tree of widget_value objects.
1439 Ignore the panes and items made by previous calls to
1440 single_submenu, even though those are also in menu_items. */
1441 i = previous_items;
1442 while (i < menu_items_used)
1444 if (EQ (XVECTOR (menu_items)->contents[i], Qnil))
1446 submenu_stack[submenu_depth++] = save_wv;
1447 save_wv = prev_wv;
1448 prev_wv = 0;
1449 i++;
1451 else if (EQ (XVECTOR (menu_items)->contents[i], Qlambda))
1453 prev_wv = save_wv;
1454 save_wv = submenu_stack[--submenu_depth];
1455 i++;
1457 else if (EQ (XVECTOR (menu_items)->contents[i], Qt)
1458 && submenu_depth != 0)
1459 i += MENU_ITEMS_PANE_LENGTH;
1460 /* Ignore a nil in the item list.
1461 It's meaningful only for dialog boxes. */
1462 else if (EQ (XVECTOR (menu_items)->contents[i], Qquote))
1463 i += 1;
1464 else if (EQ (XVECTOR (menu_items)->contents[i], Qt))
1466 /* Create a new pane. */
1467 Lisp_Object pane_name, prefix;
1468 char *pane_string;
1469 pane_name = XVECTOR (menu_items)->contents[i + MENU_ITEMS_PANE_NAME];
1470 prefix = XVECTOR (menu_items)->contents[i + MENU_ITEMS_PANE_PREFIX];
1471 pane_string = (NILP (pane_name)
1472 ? "" : (char *) XSTRING (pane_name)->data);
1473 /* If there is just one top-level pane, put all its items directly
1474 under the top-level menu. */
1475 if (menu_items_n_panes == 1)
1476 pane_string = "";
1478 /* If the pane has a meaningful name,
1479 make the pane a top-level menu item
1480 with its items as a submenu beneath it. */
1481 if (strcmp (pane_string, ""))
1483 wv = xmalloc_widget_value ();
1484 if (save_wv)
1485 save_wv->next = wv;
1486 else
1487 first_wv->contents = wv;
1488 wv->name = pane_string;
1489 /* Ignore the @ that means "separate pane".
1490 This is a kludge, but this isn't worth more time. */
1491 if (!NILP (prefix) && wv->name[0] == '@')
1492 wv->name++;
1493 wv->value = 0;
1494 wv->enabled = 1;
1496 save_wv = wv;
1497 prev_wv = 0;
1498 i += MENU_ITEMS_PANE_LENGTH;
1500 else
1502 /* Create a new item within current pane. */
1503 Lisp_Object item_name, enable, descrip, def;
1504 item_name = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_NAME];
1505 enable = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_ENABLE];
1506 descrip
1507 = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_EQUIV_KEY];
1508 def = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_DEFINITION];
1510 wv = xmalloc_widget_value ();
1511 if (prev_wv)
1512 prev_wv->next = wv;
1513 else
1514 save_wv->contents = wv;
1516 wv->name = (char *) XSTRING (item_name)->data;
1517 if (!NILP (descrip))
1518 wv->key = (char *) XSTRING (descrip)->data;
1519 wv->value = 0;
1520 /* The EMACS_INT cast avoids a warning. There's no problem
1521 as long as pointers have enough bits to hold small integers. */
1522 wv->call_data = (!NILP (def) ? (void *) (EMACS_INT) i : 0);
1523 wv->enabled = !NILP (enable);
1524 prev_wv = wv;
1526 i += MENU_ITEMS_ITEM_LENGTH;
1530 /* If we have just one "menu item"
1531 that was originally a button, return it by itself. */
1532 if (top_level_items && first_wv->contents && first_wv->contents->next == 0)
1534 wv = first_wv->contents;
1535 free_widget_value (first_wv);
1536 return wv;
1539 return first_wv;
1542 extern void EmacsFrameSetCharSize ();
1544 /* Recompute all the widgets of frame F, when the menu bar
1545 has been changed. */
1547 static void
1548 update_frame_menubar (f)
1549 FRAME_PTR f;
1551 struct x_output *x = f->output_data.x;
1552 int columns, rows;
1553 int menubar_changed;
1555 Dimension shell_height;
1557 /* We assume the menubar contents has changed if the global flag is set,
1558 or if the current buffer has changed, or if the menubar has never
1559 been updated before.
1561 menubar_changed = (x->menubar_widget
1562 && !XtIsManaged (x->menubar_widget));
1564 if (! (menubar_changed))
1565 return;
1567 BLOCK_INPUT;
1568 /* Save the size of the frame because the pane widget doesn't accept to
1569 resize itself. So force it. */
1570 columns = f->width;
1571 rows = f->height;
1573 /* Do the voodoo which means "I'm changing lots of things, don't try to
1574 refigure sizes until I'm done." */
1575 lw_refigure_widget (x->column_widget, False);
1577 /* the order in which children are managed is the top to
1578 bottom order in which they are displayed in the paned window.
1579 First, remove the text-area widget.
1581 XtUnmanageChild (x->edit_widget);
1583 /* remove the menubar that is there now, and put up the menubar that
1584 should be there.
1586 if (menubar_changed)
1588 XtManageChild (x->menubar_widget);
1589 XtMapWidget (x->menubar_widget);
1590 XtVaSetValues (x->menubar_widget, XtNmappedWhenManaged, 1, 0);
1593 /* Re-manage the text-area widget, and then thrash the sizes. */
1594 XtManageChild (x->edit_widget);
1595 lw_refigure_widget (x->column_widget, True);
1597 /* Force the pane widget to resize itself with the right values. */
1598 EmacsFrameSetCharSize (x->edit_widget, columns, rows);
1600 UNBLOCK_INPUT;
1603 /* Set the contents of the menubar widgets of frame F.
1604 The argument FIRST_TIME is currently ignored;
1605 it is set the first time this is called, from initialize_frame_menubar. */
1607 void
1608 set_frame_menubar (f, first_time, deep_p)
1609 FRAME_PTR f;
1610 int first_time;
1611 int deep_p;
1613 Widget menubar_widget = f->output_data.x->menubar_widget;
1614 Lisp_Object tail, items, frame;
1615 widget_value *wv, *first_wv, *prev_wv = 0;
1616 int i;
1617 LWLIB_ID id;
1619 if (f->output_data.x->id == 0)
1620 f->output_data.x->id = next_menubar_widget_id++;
1621 id = f->output_data.x->id;
1623 if (! menubar_widget)
1624 deep_p = 1;
1625 else if (pending_menu_activation && !deep_p)
1626 deep_p = 1;
1627 /* Make the first call for any given frame always go deep. */
1628 else if (!f->output_data.x->saved_menu_event && !deep_p)
1630 deep_p = 1;
1631 f->output_data.x->saved_menu_event = (XEvent*)xmalloc (sizeof (XEvent));
1632 f->output_data.x->saved_menu_event->type = 0;
1635 wv = xmalloc_widget_value ();
1636 wv->name = "menubar";
1637 wv->value = 0;
1638 wv->enabled = 1;
1639 first_wv = wv;
1641 if (deep_p)
1643 /* Make a widget-value tree representing the entire menu trees. */
1645 struct buffer *prev = current_buffer;
1646 Lisp_Object buffer;
1647 int specpdl_count = specpdl_ptr - specpdl;
1648 int previous_menu_items_used = f->menu_bar_items_used;
1649 Lisp_Object *previous_items
1650 = (Lisp_Object *) alloca (previous_menu_items_used
1651 * sizeof (Lisp_Object));
1653 buffer = XWINDOW (FRAME_SELECTED_WINDOW (f))->buffer;
1654 specbind (Qinhibit_quit, Qt);
1655 /* Don't let the debugger step into this code
1656 because it is not reentrant. */
1657 specbind (Qdebug_on_next_call, Qnil);
1659 record_unwind_protect (Fstore_match_data, Fmatch_data ());
1660 if (NILP (Voverriding_local_map_menu_flag))
1662 specbind (Qoverriding_terminal_local_map, Qnil);
1663 specbind (Qoverriding_local_map, Qnil);
1666 set_buffer_internal_1 (XBUFFER (buffer));
1668 /* Run the Lucid hook. */
1669 call1 (Vrun_hooks, Qactivate_menubar_hook);
1670 /* If it has changed current-menubar from previous value,
1671 really recompute the menubar from the value. */
1672 if (! NILP (Vlucid_menu_bar_dirty_flag))
1673 call0 (Qrecompute_lucid_menubar);
1674 safe_run_hooks (Qmenu_bar_update_hook);
1675 FRAME_MENU_BAR_ITEMS (f) = menu_bar_items (FRAME_MENU_BAR_ITEMS (f));
1677 items = FRAME_MENU_BAR_ITEMS (f);
1679 inhibit_garbage_collection ();
1681 /* Save the frame's previous menu bar contents data. */
1682 bcopy (XVECTOR (f->menu_bar_vector)->contents, previous_items,
1683 previous_menu_items_used * sizeof (Lisp_Object));
1685 /* Fill in the current menu bar contents. */
1686 menu_items = f->menu_bar_vector;
1687 menu_items_allocated = XVECTOR (menu_items)->size;
1688 init_menu_items ();
1689 for (i = 0; i < XVECTOR (items)->size; i += 4)
1691 Lisp_Object key, string, maps;
1693 key = XVECTOR (items)->contents[i];
1694 string = XVECTOR (items)->contents[i + 1];
1695 maps = XVECTOR (items)->contents[i + 2];
1696 if (NILP (string))
1697 break;
1699 wv = single_submenu (key, string, maps);
1700 if (prev_wv)
1701 prev_wv->next = wv;
1702 else
1703 first_wv->contents = wv;
1704 /* Don't set wv->name here; GC during the loop might relocate it. */
1705 wv->enabled = 1;
1706 prev_wv = wv;
1709 finish_menu_items ();
1711 set_buffer_internal_1 (prev);
1712 unbind_to (specpdl_count, Qnil);
1714 /* If there has been no change in the Lisp-level contents
1715 of the menu bar, skip redisplaying it. Just exit. */
1717 for (i = 0; i < previous_menu_items_used; i++)
1718 if (menu_items_used == i
1719 || (previous_items[i] != XVECTOR (menu_items)->contents[i]))
1720 break;
1721 if (i == menu_items_used && i == previous_menu_items_used && i != 0)
1723 free_menubar_widget_value_tree (first_wv);
1724 menu_items = Qnil;
1726 return;
1729 /* Now GC cannot happen during the lifetime of the widget_value,
1730 so it's safe to store data from a Lisp_String. */
1731 wv = first_wv->contents;
1732 for (i = 0; i < XVECTOR (items)->size; i += 4)
1734 Lisp_Object string;
1735 string = XVECTOR (items)->contents[i + 1];
1736 if (NILP (string))
1737 break;
1738 wv->name = (char *) XSTRING (string)->data;
1739 wv = wv->next;
1742 f->menu_bar_vector = menu_items;
1743 f->menu_bar_items_used = menu_items_used;
1744 menu_items = Qnil;
1746 else
1748 /* Make a widget-value tree containing
1749 just the top level menu bar strings. */
1751 items = FRAME_MENU_BAR_ITEMS (f);
1752 for (i = 0; i < XVECTOR (items)->size; i += 4)
1754 Lisp_Object string;
1756 string = XVECTOR (items)->contents[i + 1];
1757 if (NILP (string))
1758 break;
1760 wv = xmalloc_widget_value ();
1761 wv->name = (char *) XSTRING (string)->data;
1762 wv->value = 0;
1763 wv->enabled = 1;
1764 /* This prevents lwlib from assuming this
1765 menu item is really supposed to be empty. */
1766 /* The EMACS_INT cast avoids a warning.
1767 This value just has to be different from small integers. */
1768 wv->call_data = (void *) (EMACS_INT) (-1);
1770 if (prev_wv)
1771 prev_wv->next = wv;
1772 else
1773 first_wv->contents = wv;
1774 prev_wv = wv;
1777 /* Forget what we thought we knew about what is in the
1778 detailed contents of the menu bar menus.
1779 Changing the top level always destroys the contents. */
1780 f->menu_bar_items_used = 0;
1783 /* Create or update the menu bar widget. */
1785 BLOCK_INPUT;
1787 if (menubar_widget)
1789 /* Disable resizing (done for Motif!) */
1790 lw_allow_resizing (f->output_data.x->widget, False);
1792 /* The third arg is DEEP_P, which says to consider the entire
1793 menu trees we supply, rather than just the menu bar item names. */
1794 lw_modify_all_widgets (id, first_wv, deep_p);
1796 /* Re-enable the edit widget to resize. */
1797 lw_allow_resizing (f->output_data.x->widget, True);
1799 else
1801 menubar_widget = lw_create_widget ("menubar", "menubar", id, first_wv,
1802 f->output_data.x->column_widget,
1804 popup_activate_callback,
1805 menubar_selection_callback,
1806 popup_deactivate_callback);
1807 f->output_data.x->menubar_widget = menubar_widget;
1811 int menubar_size
1812 = (f->output_data.x->menubar_widget
1813 ? (f->output_data.x->menubar_widget->core.height
1814 + f->output_data.x->menubar_widget->core.border_width)
1815 : 0);
1817 #if 0 /* Experimentally, we now get the right results
1818 for -geometry -0-0 without this. 24 Aug 96, rms. */
1819 #ifdef USE_LUCID
1820 if (FRAME_EXTERNAL_MENU_BAR (f))
1822 Dimension ibw = 0;
1823 XtVaGetValues (f->output_data.x->column_widget,
1824 XtNinternalBorderWidth, &ibw, NULL);
1825 menubar_size += ibw;
1827 #endif /* USE_LUCID */
1828 #endif /* 0 */
1830 f->output_data.x->menubar_height = menubar_size;
1833 free_menubar_widget_value_tree (first_wv);
1835 update_frame_menubar (f);
1837 UNBLOCK_INPUT;
1840 /* Called from Fx_create_frame to create the initial menubar of a frame
1841 before it is mapped, so that the window is mapped with the menubar already
1842 there instead of us tacking it on later and thrashing the window after it
1843 is visible. */
1845 void
1846 initialize_frame_menubar (f)
1847 FRAME_PTR f;
1849 /* This function is called before the first chance to redisplay
1850 the frame. It has to be, so the frame will have the right size. */
1851 FRAME_MENU_BAR_ITEMS (f) = menu_bar_items (FRAME_MENU_BAR_ITEMS (f));
1852 set_frame_menubar (f, 1, 1);
1855 /* Get rid of the menu bar of frame F, and free its storage.
1856 This is used when deleting a frame, and when turning off the menu bar. */
1858 void
1859 free_frame_menubar (f)
1860 FRAME_PTR f;
1862 Widget menubar_widget;
1863 int id;
1865 menubar_widget = f->output_data.x->menubar_widget;
1867 if (menubar_widget)
1869 BLOCK_INPUT;
1870 lw_destroy_all_widgets ((LWLIB_ID) f->output_data.x->id);
1871 UNBLOCK_INPUT;
1875 #endif /* USE_X_TOOLKIT */
1877 /* xmenu_show actually displays a menu using the panes and items in menu_items
1878 and returns the value selected from it.
1879 There are two versions of xmenu_show, one for Xt and one for Xlib.
1880 Both assume input is blocked by the caller. */
1882 /* F is the frame the menu is for.
1883 X and Y are the frame-relative specified position,
1884 relative to the inside upper left corner of the frame F.
1885 FOR_CLICK is nonzero if this menu was invoked for a mouse click.
1886 KEYMAPS is 1 if this menu was specified with keymaps;
1887 in that case, we return a list containing the chosen item's value
1888 and perhaps also the pane's prefix.
1889 TITLE is the specified menu title.
1890 ERROR is a place to store an error message string in case of failure.
1891 (We return nil on failure, but the value doesn't actually matter.) */
1893 #ifdef USE_X_TOOLKIT
1895 /* We need a unique id for each widget handled by the Lucid Widget
1896 library.
1898 For the main windows, and popup menus, we use this counter,
1899 which we increment each time after use. This starts from 1<<16.
1901 For menu bars, we use numbers starting at 0, counted in
1902 next_menubar_widget_id. */
1903 LWLIB_ID widget_id_tick;
1905 #ifdef __STDC__
1906 static Lisp_Object *volatile menu_item_selection;
1907 #else
1908 static Lisp_Object *menu_item_selection;
1909 #endif
1911 static void
1912 popup_selection_callback (widget, id, client_data)
1913 Widget widget;
1914 LWLIB_ID id;
1915 XtPointer client_data;
1917 menu_item_selection = (Lisp_Object *) client_data;
1920 static Lisp_Object
1921 xmenu_show (f, x, y, for_click, keymaps, title, error)
1922 FRAME_PTR f;
1923 int x;
1924 int y;
1925 int for_click;
1926 int keymaps;
1927 Lisp_Object title;
1928 char **error;
1930 int i;
1931 LWLIB_ID menu_id;
1932 Widget menu;
1933 Arg av[2];
1934 int ac = 0;
1935 widget_value *wv, *save_wv = 0, *first_wv = 0, *prev_wv = 0;
1936 widget_value **submenu_stack
1937 = (widget_value **) alloca (menu_items_used * sizeof (widget_value *));
1938 Lisp_Object *subprefix_stack
1939 = (Lisp_Object *) alloca (menu_items_used * sizeof (Lisp_Object));
1940 int submenu_depth = 0;
1941 XButtonPressedEvent dummy;
1943 int first_pane;
1944 int next_release_must_exit = 0;
1946 *error = NULL;
1948 if (menu_items_used <= MENU_ITEMS_PANE_LENGTH)
1950 *error = "Empty menu";
1951 return Qnil;
1954 /* Create a tree of widget_value objects
1955 representing the panes and their items. */
1956 wv = xmalloc_widget_value ();
1957 wv->name = "menu";
1958 wv->value = 0;
1959 wv->enabled = 1;
1960 first_wv = wv;
1961 first_pane = 1;
1963 /* Loop over all panes and items, filling in the tree. */
1964 i = 0;
1965 while (i < menu_items_used)
1967 if (EQ (XVECTOR (menu_items)->contents[i], Qnil))
1969 submenu_stack[submenu_depth++] = save_wv;
1970 save_wv = prev_wv;
1971 prev_wv = 0;
1972 first_pane = 1;
1973 i++;
1975 else if (EQ (XVECTOR (menu_items)->contents[i], Qlambda))
1977 prev_wv = save_wv;
1978 save_wv = submenu_stack[--submenu_depth];
1979 first_pane = 0;
1980 i++;
1982 else if (EQ (XVECTOR (menu_items)->contents[i], Qt)
1983 && submenu_depth != 0)
1984 i += MENU_ITEMS_PANE_LENGTH;
1985 /* Ignore a nil in the item list.
1986 It's meaningful only for dialog boxes. */
1987 else if (EQ (XVECTOR (menu_items)->contents[i], Qquote))
1988 i += 1;
1989 else if (EQ (XVECTOR (menu_items)->contents[i], Qt))
1991 /* Create a new pane. */
1992 Lisp_Object pane_name, prefix;
1993 char *pane_string;
1994 pane_name = XVECTOR (menu_items)->contents[i + MENU_ITEMS_PANE_NAME];
1995 prefix = XVECTOR (menu_items)->contents[i + MENU_ITEMS_PANE_PREFIX];
1996 pane_string = (NILP (pane_name)
1997 ? "" : (char *) XSTRING (pane_name)->data);
1998 /* If there is just one top-level pane, put all its items directly
1999 under the top-level menu. */
2000 if (menu_items_n_panes == 1)
2001 pane_string = "";
2003 /* If the pane has a meaningful name,
2004 make the pane a top-level menu item
2005 with its items as a submenu beneath it. */
2006 if (!keymaps && strcmp (pane_string, ""))
2008 wv = xmalloc_widget_value ();
2009 if (save_wv)
2010 save_wv->next = wv;
2011 else
2012 first_wv->contents = wv;
2013 wv->name = pane_string;
2014 if (keymaps && !NILP (prefix))
2015 wv->name++;
2016 wv->value = 0;
2017 wv->enabled = 1;
2018 save_wv = wv;
2019 prev_wv = 0;
2021 else if (first_pane)
2023 save_wv = wv;
2024 prev_wv = 0;
2026 first_pane = 0;
2027 i += MENU_ITEMS_PANE_LENGTH;
2029 else
2031 /* Create a new item within current pane. */
2032 Lisp_Object item_name, enable, descrip, def;
2033 item_name = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_NAME];
2034 enable = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_ENABLE];
2035 descrip
2036 = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_EQUIV_KEY];
2037 def = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_DEFINITION];
2039 wv = xmalloc_widget_value ();
2040 if (prev_wv)
2041 prev_wv->next = wv;
2042 else
2043 save_wv->contents = wv;
2044 wv->name = (char *) XSTRING (item_name)->data;
2045 if (!NILP (descrip))
2046 wv->key = (char *) XSTRING (descrip)->data;
2047 wv->value = 0;
2048 /* If this item has a null value,
2049 make the call_data null so that it won't display a box
2050 when the mouse is on it. */
2051 wv->call_data
2052 = (!NILP (def) ? (void *) &XVECTOR (menu_items)->contents[i] : 0);
2053 wv->enabled = !NILP (enable);
2054 prev_wv = wv;
2056 i += MENU_ITEMS_ITEM_LENGTH;
2060 /* Deal with the title, if it is non-nil. */
2061 if (!NILP (title))
2063 widget_value *wv_title = xmalloc_widget_value ();
2064 widget_value *wv_sep1 = xmalloc_widget_value ();
2065 widget_value *wv_sep2 = xmalloc_widget_value ();
2067 wv_sep2->name = "--";
2068 wv_sep2->next = first_wv->contents;
2070 wv_sep1->name = "--";
2071 wv_sep1->next = wv_sep2;
2073 wv_title->name = (char *) XSTRING (title)->data;
2074 wv_title->enabled = True;
2075 wv_title->next = wv_sep1;
2076 first_wv->contents = wv_title;
2079 /* Actually create the menu. */
2080 menu_id = widget_id_tick++;
2081 menu = lw_create_widget ("popup", first_wv->name, menu_id, first_wv,
2082 f->output_data.x->widget, 1, 0,
2083 popup_selection_callback,
2084 popup_deactivate_callback);
2086 /* Adjust coordinates to relative to the outer (window manager) window. */
2088 Window child;
2089 int win_x = 0, win_y = 0;
2091 /* Find the position of the outside upper-left corner of
2092 the inner window, with respect to the outer window. */
2093 if (f->output_data.x->parent_desc != FRAME_X_DISPLAY_INFO (f)->root_window)
2095 BLOCK_INPUT;
2096 XTranslateCoordinates (FRAME_X_DISPLAY (f),
2098 /* From-window, to-window. */
2099 f->output_data.x->window_desc,
2100 f->output_data.x->parent_desc,
2102 /* From-position, to-position. */
2103 0, 0, &win_x, &win_y,
2105 /* Child of window. */
2106 &child);
2107 UNBLOCK_INPUT;
2108 x += win_x;
2109 y += win_y;
2113 /* Adjust coordinates to be root-window-relative. */
2114 x += f->output_data.x->left_pos;
2115 y += f->output_data.x->top_pos;
2117 dummy.type = ButtonPress;
2118 dummy.serial = 0;
2119 dummy.send_event = 0;
2120 dummy.display = FRAME_X_DISPLAY (f);
2121 dummy.time = CurrentTime;
2122 dummy.root = FRAME_X_DISPLAY_INFO (f)->root_window;
2123 dummy.window = dummy.root;
2124 dummy.subwindow = dummy.root;
2125 dummy.x_root = x;
2126 dummy.y_root = y;
2127 dummy.x = x;
2128 dummy.y = y;
2129 dummy.state = (FRAME_X_DISPLAY_INFO (f)->grabbed >> 1) * Button1Mask;
2130 dummy.button = 0;
2131 for (i = 0; i < 5; i++)
2132 if (FRAME_X_DISPLAY_INFO (f)->grabbed & (1 << i))
2133 dummy.button = i;
2135 /* Don't allow any geometry request from the user. */
2136 XtSetArg (av[ac], XtNgeometry, 0); ac++;
2137 XtSetValues (menu, av, ac);
2139 /* Free the widget_value objects we used to specify the contents. */
2140 free_menubar_widget_value_tree (first_wv);
2142 /* No selection has been chosen yet. */
2143 menu_item_selection = 0;
2145 /* Display the menu. */
2146 lw_popup_menu (menu, &dummy);
2147 popup_activated_flag = 1;
2149 /* Process events that apply to the menu. */
2150 popup_get_selection ((XEvent *) 0, FRAME_X_DISPLAY_INFO (f), menu_id);
2152 /* fp turned off the following statement and wrote a comment
2153 that it is unnecessary--that the menu has already disappeared.
2154 Nowadays the menu disappears ok, all right, but
2155 we need to delete the widgets or multiple ones will pile up. */
2156 lw_destroy_all_widgets (menu_id);
2158 /* Find the selected item, and its pane, to return
2159 the proper value. */
2160 if (menu_item_selection != 0)
2162 Lisp_Object prefix, entry;
2164 prefix = Qnil;
2165 i = 0;
2166 while (i < menu_items_used)
2168 if (EQ (XVECTOR (menu_items)->contents[i], Qnil))
2170 subprefix_stack[submenu_depth++] = prefix;
2171 prefix = entry;
2172 i++;
2174 else if (EQ (XVECTOR (menu_items)->contents[i], Qlambda))
2176 prefix = subprefix_stack[--submenu_depth];
2177 i++;
2179 else if (EQ (XVECTOR (menu_items)->contents[i], Qt))
2181 prefix
2182 = XVECTOR (menu_items)->contents[i + MENU_ITEMS_PANE_PREFIX];
2183 i += MENU_ITEMS_PANE_LENGTH;
2185 /* Ignore a nil in the item list.
2186 It's meaningful only for dialog boxes. */
2187 else if (EQ (XVECTOR (menu_items)->contents[i], Qquote))
2188 i += 1;
2189 else
2191 entry
2192 = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_VALUE];
2193 if (menu_item_selection == &XVECTOR (menu_items)->contents[i])
2195 if (keymaps != 0)
2197 int j;
2199 entry = Fcons (entry, Qnil);
2200 if (!NILP (prefix))
2201 entry = Fcons (prefix, entry);
2202 for (j = submenu_depth - 1; j >= 0; j--)
2203 if (!NILP (subprefix_stack[j]))
2204 entry = Fcons (subprefix_stack[j], entry);
2206 return entry;
2208 i += MENU_ITEMS_ITEM_LENGTH;
2213 return Qnil;
2216 static void
2217 dialog_selection_callback (widget, id, client_data)
2218 Widget widget;
2219 LWLIB_ID id;
2220 XtPointer client_data;
2222 /* The EMACS_INT cast avoids a warning. There's no problem
2223 as long as pointers have enough bits to hold small integers. */
2224 if ((int) (EMACS_INT) client_data != -1)
2225 menu_item_selection = (Lisp_Object *) client_data;
2226 BLOCK_INPUT;
2227 lw_destroy_all_widgets (id);
2228 UNBLOCK_INPUT;
2229 popup_activated_flag = 0;
2232 static char * button_names [] = {
2233 "button1", "button2", "button3", "button4", "button5",
2234 "button6", "button7", "button8", "button9", "button10" };
2236 static Lisp_Object
2237 xdialog_show (f, keymaps, title, error)
2238 FRAME_PTR f;
2239 int keymaps;
2240 Lisp_Object title;
2241 char **error;
2243 int i, nb_buttons=0;
2244 LWLIB_ID dialog_id;
2245 Widget menu;
2246 char dialog_name[6];
2248 widget_value *wv, *save_wv = 0, *first_wv = 0, *prev_wv = 0;
2250 /* Number of elements seen so far, before boundary. */
2251 int left_count = 0;
2252 /* 1 means we've seen the boundary between left-hand elts and right-hand. */
2253 int boundary_seen = 0;
2255 *error = NULL;
2257 if (menu_items_n_panes > 1)
2259 *error = "Multiple panes in dialog box";
2260 return Qnil;
2263 /* Create a tree of widget_value objects
2264 representing the text label and buttons. */
2266 Lisp_Object pane_name, prefix;
2267 char *pane_string;
2268 pane_name = XVECTOR (menu_items)->contents[MENU_ITEMS_PANE_NAME];
2269 prefix = XVECTOR (menu_items)->contents[MENU_ITEMS_PANE_PREFIX];
2270 pane_string = (NILP (pane_name)
2271 ? "" : (char *) XSTRING (pane_name)->data);
2272 prev_wv = xmalloc_widget_value ();
2273 prev_wv->value = pane_string;
2274 if (keymaps && !NILP (prefix))
2275 prev_wv->name++;
2276 prev_wv->enabled = 1;
2277 prev_wv->name = "message";
2278 first_wv = prev_wv;
2280 /* Loop over all panes and items, filling in the tree. */
2281 i = MENU_ITEMS_PANE_LENGTH;
2282 while (i < menu_items_used)
2285 /* Create a new item within current pane. */
2286 Lisp_Object item_name, enable, descrip;
2287 item_name = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_NAME];
2288 enable = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_ENABLE];
2289 descrip
2290 = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_EQUIV_KEY];
2292 if (NILP (item_name))
2294 free_menubar_widget_value_tree (first_wv);
2295 *error = "Submenu in dialog items";
2296 return Qnil;
2298 if (EQ (item_name, Qquote))
2300 /* This is the boundary between left-side elts
2301 and right-side elts. Stop incrementing right_count. */
2302 boundary_seen = 1;
2303 i++;
2304 continue;
2306 if (nb_buttons >= 9)
2308 free_menubar_widget_value_tree (first_wv);
2309 *error = "Too many dialog items";
2310 return Qnil;
2313 wv = xmalloc_widget_value ();
2314 prev_wv->next = wv;
2315 wv->name = (char *) button_names[nb_buttons];
2316 if (!NILP (descrip))
2317 wv->key = (char *) XSTRING (descrip)->data;
2318 wv->value = (char *) XSTRING (item_name)->data;
2319 wv->call_data = (void *) &XVECTOR (menu_items)->contents[i];
2320 wv->enabled = !NILP (enable);
2321 prev_wv = wv;
2323 if (! boundary_seen)
2324 left_count++;
2326 nb_buttons++;
2327 i += MENU_ITEMS_ITEM_LENGTH;
2330 /* If the boundary was not specified,
2331 by default put half on the left and half on the right. */
2332 if (! boundary_seen)
2333 left_count = nb_buttons - nb_buttons / 2;
2335 wv = xmalloc_widget_value ();
2336 wv->name = dialog_name;
2338 /* Dialog boxes use a really stupid name encoding
2339 which specifies how many buttons to use
2340 and how many buttons are on the right.
2341 The Q means something also. */
2342 dialog_name[0] = 'Q';
2343 dialog_name[1] = '0' + nb_buttons;
2344 dialog_name[2] = 'B';
2345 dialog_name[3] = 'R';
2346 /* Number of buttons to put on the right. */
2347 dialog_name[4] = '0' + nb_buttons - left_count;
2348 dialog_name[5] = 0;
2349 wv->contents = first_wv;
2350 first_wv = wv;
2353 /* Actually create the dialog. */
2354 dialog_id = widget_id_tick++;
2355 menu = lw_create_widget (first_wv->name, "dialog", dialog_id, first_wv,
2356 f->output_data.x->widget, 1, 0,
2357 dialog_selection_callback, 0);
2358 lw_modify_all_widgets (dialog_id, first_wv->contents, True);
2359 /* Free the widget_value objects we used to specify the contents. */
2360 free_menubar_widget_value_tree (first_wv);
2362 /* No selection has been chosen yet. */
2363 menu_item_selection = 0;
2365 /* Display the menu. */
2366 lw_pop_up_all_widgets (dialog_id);
2367 popup_activated_flag = 1;
2369 /* Process events that apply to the menu. */
2370 popup_get_selection ((XEvent *) 0, FRAME_X_DISPLAY_INFO (f), dialog_id);
2372 lw_destroy_all_widgets (dialog_id);
2374 /* Find the selected item, and its pane, to return
2375 the proper value. */
2376 if (menu_item_selection != 0)
2378 Lisp_Object prefix;
2380 prefix = Qnil;
2381 i = 0;
2382 while (i < menu_items_used)
2384 Lisp_Object entry;
2386 if (EQ (XVECTOR (menu_items)->contents[i], Qt))
2388 prefix
2389 = XVECTOR (menu_items)->contents[i + MENU_ITEMS_PANE_PREFIX];
2390 i += MENU_ITEMS_PANE_LENGTH;
2392 else
2394 entry
2395 = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_VALUE];
2396 if (menu_item_selection == &XVECTOR (menu_items)->contents[i])
2398 if (keymaps != 0)
2400 entry = Fcons (entry, Qnil);
2401 if (!NILP (prefix))
2402 entry = Fcons (prefix, entry);
2404 return entry;
2406 i += MENU_ITEMS_ITEM_LENGTH;
2411 return Qnil;
2413 #else /* not USE_X_TOOLKIT */
2415 static Lisp_Object
2416 xmenu_show (f, x, y, for_click, keymaps, title, error)
2417 FRAME_PTR f;
2418 int x, y;
2419 int for_click;
2420 int keymaps;
2421 Lisp_Object title;
2422 char **error;
2424 Window root;
2425 XMenu *menu;
2426 int pane, selidx, lpane, status;
2427 Lisp_Object entry, pane_prefix;
2428 char *datap;
2429 int ulx, uly, width, height;
2430 int dispwidth, dispheight;
2431 int i, j;
2432 int maxwidth;
2433 int dummy_int;
2434 unsigned int dummy_uint;
2436 *error = 0;
2437 if (menu_items_n_panes == 0)
2438 return Qnil;
2440 if (menu_items_used <= MENU_ITEMS_PANE_LENGTH)
2442 *error = "Empty menu";
2443 return Qnil;
2446 /* Figure out which root window F is on. */
2447 XGetGeometry (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), &root,
2448 &dummy_int, &dummy_int, &dummy_uint, &dummy_uint,
2449 &dummy_uint, &dummy_uint);
2451 /* Make the menu on that window. */
2452 menu = XMenuCreate (FRAME_X_DISPLAY (f), root, "emacs");
2453 if (menu == NULL)
2455 *error = "Can't create menu";
2456 return Qnil;
2459 #ifdef HAVE_X_WINDOWS
2460 /* Adjust coordinates to relative to the outer (window manager) window. */
2462 Window child;
2463 int win_x = 0, win_y = 0;
2465 /* Find the position of the outside upper-left corner of
2466 the inner window, with respect to the outer window. */
2467 if (f->output_data.x->parent_desc != FRAME_X_DISPLAY_INFO (f)->root_window)
2469 BLOCK_INPUT;
2470 XTranslateCoordinates (FRAME_X_DISPLAY (f),
2472 /* From-window, to-window. */
2473 f->output_data.x->window_desc,
2474 f->output_data.x->parent_desc,
2476 /* From-position, to-position. */
2477 0, 0, &win_x, &win_y,
2479 /* Child of window. */
2480 &child);
2481 UNBLOCK_INPUT;
2482 x += win_x;
2483 y += win_y;
2486 #endif /* HAVE_X_WINDOWS */
2488 /* Adjust coordinates to be root-window-relative. */
2489 x += f->output_data.x->left_pos;
2490 y += f->output_data.x->top_pos;
2492 /* Create all the necessary panes and their items. */
2493 i = 0;
2494 while (i < menu_items_used)
2496 if (EQ (XVECTOR (menu_items)->contents[i], Qt))
2498 /* Create a new pane. */
2499 Lisp_Object pane_name, prefix;
2500 char *pane_string;
2502 pane_name = XVECTOR (menu_items)->contents[i + MENU_ITEMS_PANE_NAME];
2503 prefix = XVECTOR (menu_items)->contents[i + MENU_ITEMS_PANE_PREFIX];
2504 pane_string = (NILP (pane_name)
2505 ? "" : (char *) XSTRING (pane_name)->data);
2506 if (keymaps && !NILP (prefix))
2507 pane_string++;
2509 lpane = XMenuAddPane (FRAME_X_DISPLAY (f), menu, pane_string, TRUE);
2510 if (lpane == XM_FAILURE)
2512 XMenuDestroy (FRAME_X_DISPLAY (f), menu);
2513 *error = "Can't create pane";
2514 return Qnil;
2516 i += MENU_ITEMS_PANE_LENGTH;
2518 /* Find the width of the widest item in this pane. */
2519 maxwidth = 0;
2520 j = i;
2521 while (j < menu_items_used)
2523 Lisp_Object item;
2524 item = XVECTOR (menu_items)->contents[j];
2525 if (EQ (item, Qt))
2526 break;
2527 if (NILP (item))
2529 j++;
2530 continue;
2532 width = XSTRING (item)->size;
2533 if (width > maxwidth)
2534 maxwidth = width;
2536 j += MENU_ITEMS_ITEM_LENGTH;
2539 /* Ignore a nil in the item list.
2540 It's meaningful only for dialog boxes. */
2541 else if (EQ (XVECTOR (menu_items)->contents[i], Qquote))
2542 i += 1;
2543 else
2545 /* Create a new item within current pane. */
2546 Lisp_Object item_name, enable, descrip;
2547 unsigned char *item_data;
2549 item_name = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_NAME];
2550 enable = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_ENABLE];
2551 descrip
2552 = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_EQUIV_KEY];
2553 if (!NILP (descrip))
2555 int gap = maxwidth - XSTRING (item_name)->size;
2556 #ifdef C_ALLOCA
2557 Lisp_Object spacer;
2558 spacer = Fmake_string (make_number (gap), make_number (' '));
2559 item_name = concat2 (item_name, spacer);
2560 item_name = concat2 (item_name, descrip);
2561 item_data = XSTRING (item_name)->data;
2562 #else
2563 /* if alloca is fast, use that to make the space,
2564 to reduce gc needs. */
2565 item_data
2566 = (unsigned char *) alloca (maxwidth
2567 + XSTRING (descrip)->size + 1);
2568 bcopy (XSTRING (item_name)->data, item_data,
2569 XSTRING (item_name)->size);
2570 for (j = XSTRING (item_name)->size; j < maxwidth; j++)
2571 item_data[j] = ' ';
2572 bcopy (XSTRING (descrip)->data, item_data + j,
2573 XSTRING (descrip)->size);
2574 item_data[j + XSTRING (descrip)->size] = 0;
2575 #endif
2577 else
2578 item_data = XSTRING (item_name)->data;
2580 if (XMenuAddSelection (FRAME_X_DISPLAY (f),
2581 menu, lpane, 0, item_data,
2582 !NILP (enable))
2583 == XM_FAILURE)
2585 XMenuDestroy (FRAME_X_DISPLAY (f), menu);
2586 *error = "Can't add selection to menu";
2587 return Qnil;
2589 i += MENU_ITEMS_ITEM_LENGTH;
2593 /* All set and ready to fly. */
2594 XMenuRecompute (FRAME_X_DISPLAY (f), menu);
2595 dispwidth = DisplayWidth (FRAME_X_DISPLAY (f),
2596 XScreenNumberOfScreen (FRAME_X_SCREEN (f)));
2597 dispheight = DisplayHeight (FRAME_X_DISPLAY (f),
2598 XScreenNumberOfScreen (FRAME_X_SCREEN (f)));
2599 x = min (x, dispwidth);
2600 y = min (y, dispheight);
2601 x = max (x, 1);
2602 y = max (y, 1);
2603 XMenuLocate (FRAME_X_DISPLAY (f), menu, 0, 0, x, y,
2604 &ulx, &uly, &width, &height);
2605 if (ulx+width > dispwidth)
2607 x -= (ulx + width) - dispwidth;
2608 ulx = dispwidth - width;
2610 if (uly+height > dispheight)
2612 y -= (uly + height) - dispheight;
2613 uly = dispheight - height;
2615 if (ulx < 0) x -= ulx;
2616 if (uly < 0) y -= uly;
2618 XMenuSetAEQ (menu, TRUE);
2619 XMenuSetFreeze (menu, TRUE);
2620 pane = selidx = 0;
2622 status = XMenuActivate (FRAME_X_DISPLAY (f), menu, &pane, &selidx,
2623 x, y, ButtonReleaseMask, &datap);
2626 #ifdef HAVE_X_WINDOWS
2627 /* Assume the mouse has moved out of the X window.
2628 If it has actually moved in, we will get an EnterNotify. */
2629 x_mouse_leave (FRAME_X_DISPLAY_INFO (f));
2630 #endif
2632 switch (status)
2634 case XM_SUCCESS:
2635 #ifdef XDEBUG
2636 fprintf (stderr, "pane= %d line = %d\n", panes, selidx);
2637 #endif
2639 /* Find the item number SELIDX in pane number PANE. */
2640 i = 0;
2641 while (i < menu_items_used)
2643 if (EQ (XVECTOR (menu_items)->contents[i], Qt))
2645 if (pane == 0)
2646 pane_prefix
2647 = XVECTOR (menu_items)->contents[i + MENU_ITEMS_PANE_PREFIX];
2648 pane--;
2649 i += MENU_ITEMS_PANE_LENGTH;
2651 else
2653 if (pane == -1)
2655 if (selidx == 0)
2657 entry
2658 = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_VALUE];
2659 if (keymaps != 0)
2661 entry = Fcons (entry, Qnil);
2662 if (!NILP (pane_prefix))
2663 entry = Fcons (pane_prefix, entry);
2665 break;
2667 selidx--;
2669 i += MENU_ITEMS_ITEM_LENGTH;
2672 break;
2674 case XM_FAILURE:
2675 *error = "Can't activate menu";
2676 case XM_IA_SELECT:
2677 case XM_NO_SELECT:
2678 entry = Qnil;
2679 break;
2681 XMenuDestroy (FRAME_X_DISPLAY (f), menu);
2683 #ifdef HAVE_X_WINDOWS
2684 /* State that no mouse buttons are now held.
2685 (The oldXMenu code doesn't track this info for us.)
2686 That is not necessarily true, but the fiction leads to reasonable
2687 results, and it is a pain to ask which are actually held now. */
2688 FRAME_X_DISPLAY_INFO (f)->grabbed = 0;
2689 #endif
2691 return entry;
2694 #endif /* not USE_X_TOOLKIT */
2696 #endif /* HAVE_MENUS */
2698 syms_of_xmenu ()
2700 staticpro (&menu_items);
2701 menu_items = Qnil;
2703 Qmenu_alias = intern ("menu-alias");
2704 staticpro (&Qmenu_alias);
2706 Qdebug_on_next_call = intern ("debug-on-next-call");
2707 staticpro (&Qdebug_on_next_call);
2709 #ifdef USE_X_TOOLKIT
2710 widget_id_tick = (1<<16);
2711 next_menubar_widget_id = 1;
2712 #endif
2714 defsubr (&Sx_popup_menu);
2715 #ifdef HAVE_MENUS
2716 defsubr (&Sx_popup_dialog);
2717 #endif