1 /* X Communication module for terminals which understand the X protocol.
2 Copyright (C) 1986, 1988, 1993, 1994 Free Software Foundation, Inc.
4 This file is part of GNU Emacs.
6 GNU Emacs is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
11 GNU Emacs is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU Emacs; see the file COPYING. If not, write to
18 the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
20 /* X pop-up deck-of-cards menu facility for gnuemacs.
22 * Written by Jon Arnold and Roman Budzianowski
23 * Mods and rewrite by Robert Krawitz
27 /* Modified by Fred Pierresteguy on December 93
28 to make the popup menus and menubar use the Xt. */
30 /* Rewritten for clarity and GC protection by rms in Feb 94. */
32 /* On 4.3 this loses if it comes after xterm.h. */
38 #include "termhooks.h"
42 #include "blockinput.h"
50 /* This may include sys/types.h, and that somehow loses
51 if this is not done before the other system files. */
55 /* Load sys/types.h if not already loaded.
56 In some systems loading it twice is suicidal. */
58 #include <sys/types.h>
61 #include "dispextern.h"
66 #include <X11/IntrinsicP.h>
67 #include <X11/CoreP.h>
68 #include <X11/StringDefs.h>
69 #include <X11/Shell.h>
70 #include <X11/Xaw/Paned.h>
71 #include "../lwlib/lwlib.h"
72 #else /* not USE_X_TOOLKIT */
73 #include "../oldXMenu/XMenu.h"
74 #endif /* not USE_X_TOOLKIT */
75 #endif /* HAVE_X_WINDOWS */
77 #define min(x,y) (((x) < (y)) ? (x) : (y))
78 #define max(x,y) (((x) > (y)) ? (x) : (y))
85 extern Lisp_Object Qmenu_enable
;
86 extern Lisp_Object Qmenu_bar
;
87 extern Lisp_Object Qmouse_click
, Qevent_kind
;
90 extern void process_expose_from_menu ();
91 extern XtAppContext Xt_app_con
;
93 static Lisp_Object
xdialog_show ();
94 void popup_get_selection ();
97 static Lisp_Object
xmenu_show ();
98 static void keymap_panes ();
99 static void single_keymap_panes ();
100 static void list_of_panes ();
101 static void list_of_items ();
103 /* This holds a Lisp vector that holds the results of decoding
104 the keymaps or alist-of-alists that specify a menu.
106 It describes the panes and items within the panes.
108 Each pane is described by 3 elements in the vector:
109 t, the pane name, the pane's prefix key.
110 Then follow the pane's items, with 5 elements per item:
111 the item string, the enable flag, the item's value,
112 the definition, and the equivalent keyboard key's description string.
114 In some cases, multiple levels of menus may be described.
115 A single vector slot containing nil indicates the start of a submenu.
116 A single vector slot containing lambda indicates the end of a submenu.
117 The submenu follows a menu item which is the way to reach the submenu.
119 A single vector slot containing quote indicates that the
120 following items should appear on the right of a dialog box.
122 Using a Lisp vector to hold this information while we decode it
123 takes care of protecting all the data from GC. */
125 #define MENU_ITEMS_PANE_NAME 1
126 #define MENU_ITEMS_PANE_PREFIX 2
127 #define MENU_ITEMS_PANE_LENGTH 3
129 #define MENU_ITEMS_ITEM_NAME 0
130 #define MENU_ITEMS_ITEM_ENABLE 1
131 #define MENU_ITEMS_ITEM_VALUE 2
132 #define MENU_ITEMS_ITEM_EQUIV_KEY 3
133 #define MENU_ITEMS_ITEM_DEFINITION 4
134 #define MENU_ITEMS_ITEM_LENGTH 5
136 static Lisp_Object menu_items
;
138 /* Number of slots currently allocated in menu_items. */
139 static int menu_items_allocated
;
141 /* This is the index in menu_items of the first empty slot. */
142 static int menu_items_used
;
144 /* The number of panes currently recorded in menu_items,
145 excluding those within submenus. */
146 static int menu_items_n_panes
;
148 /* Current depth within submenus. */
149 static int menu_items_submenu_depth
;
151 /* Flag which when set indicates a dialog or menu has been posted by
152 Xt on behalf of one of the widget sets. */
153 static int popup_activated_flag
;
155 /* This holds a Lisp vector
156 which contains frames that have menu bars.
157 Each frame that has a menu bar is found at some index in this vector
158 and the menu bar widget refers to the frame through that index. */
159 static Lisp_Object frame_vector
;
161 /* Return the index of FRAME in frame_vector.
162 If FRAME isn't in frame_vector yet, put it in,
163 lengthening the vector if necessary. */
166 frame_vector_add_frame (f
)
169 int length
= XVECTOR (frame_vector
)->size
;
171 Lisp_Object
new, frame
;
173 XSETFRAME (frame
, f
);
175 for (i
= 0; i
< length
; i
++)
177 if (EQ (frame
, XVECTOR (frame_vector
)->contents
[i
]))
179 if (NILP (XVECTOR (frame_vector
)->contents
[i
]))
185 XVECTOR (frame_vector
)->contents
[empty
] = frame
;
189 new = Fmake_vector (make_number (length
* 2), Qnil
);
190 bcopy (XVECTOR (frame_vector
)->contents
,
191 XVECTOR (new)->contents
, sizeof (Lisp_Object
) * length
);
193 XVECTOR (frame_vector
)->contents
[length
] = frame
;
197 /* Initialize the menu_items structure if we haven't already done so.
198 Also mark it as currently empty. */
203 if (NILP (menu_items
))
205 menu_items_allocated
= 60;
206 menu_items
= Fmake_vector (make_number (menu_items_allocated
), Qnil
);
210 menu_items_n_panes
= 0;
211 menu_items_submenu_depth
= 0;
214 /* Call at the end of generating the data in menu_items.
215 This fills in the number of items in the last pane. */
222 /* Call when finished using the data for the current menu
226 discard_menu_items ()
228 /* Free the structure if it is especially large.
229 Otherwise, hold on to it, to save time. */
230 if (menu_items_allocated
> 200)
233 menu_items_allocated
= 0;
237 /* Make the menu_items vector twice as large. */
243 int old_size
= menu_items_allocated
;
246 menu_items_allocated
*= 2;
247 menu_items
= Fmake_vector (make_number (menu_items_allocated
), Qnil
);
248 bcopy (XVECTOR (old
)->contents
, XVECTOR (menu_items
)->contents
,
249 old_size
* sizeof (Lisp_Object
));
252 /* Begin a submenu. */
255 push_submenu_start ()
257 if (menu_items_used
+ 1 > menu_items_allocated
)
260 XVECTOR (menu_items
)->contents
[menu_items_used
++] = Qnil
;
261 menu_items_submenu_depth
++;
269 if (menu_items_used
+ 1 > menu_items_allocated
)
272 XVECTOR (menu_items
)->contents
[menu_items_used
++] = Qlambda
;
273 menu_items_submenu_depth
--;
276 /* Indicate boundary between left and right. */
279 push_left_right_boundary ()
281 if (menu_items_used
+ 1 > menu_items_allocated
)
284 XVECTOR (menu_items
)->contents
[menu_items_used
++] = Qquote
;
287 /* Start a new menu pane in menu_items..
288 NAME is the pane name. PREFIX_VEC is a prefix key for this pane. */
291 push_menu_pane (name
, prefix_vec
)
292 Lisp_Object name
, prefix_vec
;
294 if (menu_items_used
+ MENU_ITEMS_PANE_LENGTH
> menu_items_allocated
)
297 if (menu_items_submenu_depth
== 0)
298 menu_items_n_panes
++;
299 XVECTOR (menu_items
)->contents
[menu_items_used
++] = Qt
;
300 XVECTOR (menu_items
)->contents
[menu_items_used
++] = name
;
301 XVECTOR (menu_items
)->contents
[menu_items_used
++] = prefix_vec
;
304 /* Push one menu item into the current pane.
305 NAME is the string to display. ENABLE if non-nil means
306 this item can be selected. KEY is the key generated by
307 choosing this item, or nil if this item doesn't really have a definition.
308 DEF is the definition of this item.
309 EQUIV is the textual description of the keyboard equivalent for
310 this item (or nil if none). */
313 push_menu_item (name
, enable
, key
, def
, equiv
)
314 Lisp_Object name
, enable
, key
, def
, equiv
;
316 if (menu_items_used
+ MENU_ITEMS_ITEM_LENGTH
> menu_items_allocated
)
319 XVECTOR (menu_items
)->contents
[menu_items_used
++] = name
;
320 XVECTOR (menu_items
)->contents
[menu_items_used
++] = enable
;
321 XVECTOR (menu_items
)->contents
[menu_items_used
++] = key
;
322 XVECTOR (menu_items
)->contents
[menu_items_used
++] = equiv
;
323 XVECTOR (menu_items
)->contents
[menu_items_used
++] = def
;
326 /* Figure out the current keyboard equivalent of a menu item ITEM1.
327 The item string for menu display should be ITEM_STRING.
328 Store the equivalent keyboard key sequence's
329 textual description into *DESCRIP_PTR.
330 Also cache them in the item itself.
331 Return the real definition to execute. */
334 menu_item_equiv_key (item_string
, item1
, descrip_ptr
)
335 Lisp_Object item_string
;
337 Lisp_Object
*descrip_ptr
;
339 /* This is the real definition--the function to run. */
341 /* This is the sublist that records cached equiv key data
342 so we can save time. */
343 Lisp_Object cachelist
;
344 /* These are the saved equivalent keyboard key sequence
345 and its key-description. */
346 Lisp_Object savedkey
, descrip
;
349 struct gcpro gcpro1
, gcpro2
, gcpro3
, gcpro4
;
351 /* If a help string follows the item string, skip it. */
352 if (CONSP (XCONS (item1
)->cdr
)
353 && STRINGP (XCONS (XCONS (item1
)->cdr
)->car
))
354 item1
= XCONS (item1
)->cdr
;
358 /* Get out the saved equivalent-keyboard-key info. */
359 cachelist
= savedkey
= descrip
= Qnil
;
360 if (CONSP (def
) && CONSP (XCONS (def
)->car
)
361 && (NILP (XCONS (XCONS (def
)->car
)->car
)
362 || VECTORP (XCONS (XCONS (def
)->car
)->car
)))
364 cachelist
= XCONS (def
)->car
;
365 def
= XCONS (def
)->cdr
;
366 savedkey
= XCONS (cachelist
)->car
;
367 descrip
= XCONS (cachelist
)->cdr
;
370 GCPRO4 (def
, def1
, savedkey
, descrip
);
372 /* Is it still valid? */
374 if (!NILP (savedkey
))
375 def1
= Fkey_binding (savedkey
, Qnil
);
376 /* If not, update it. */
378 /* If the command is an alias for another
379 (such as easymenu.el and lmenu.el set it up),
380 check if the original command matches the cached command. */
381 && !(SYMBOLP (def
) && SYMBOLP (XSYMBOL (def
)->function
)
382 && EQ (def1
, XSYMBOL (def
)->function
))
383 /* If something had no key binding before, don't recheck it--
384 doing that takes too much time and makes menus too slow. */
385 && !(!NILP (cachelist
) && NILP (savedkey
)))
389 /* If the command is an alias for another
390 (such as easymenu.el and lmenu.el set it up),
391 see if the original command name has equivalent keys. */
392 if (SYMBOLP (def
) && SYMBOLP (XSYMBOL (def
)->function
))
393 savedkey
= Fwhere_is_internal (XSYMBOL (def
)->function
,
396 /* Otherwise look up the specified command itself.
397 We don't try both, because that makes easymenu menus slow. */
398 savedkey
= Fwhere_is_internal (def
, Qnil
, Qt
, Qnil
);
400 if (!NILP (savedkey
))
402 descrip
= Fkey_description (savedkey
);
403 descrip
= concat2 (make_string (" (", 3), descrip
);
404 descrip
= concat2 (descrip
, make_string (")", 1));
408 /* Cache the data we just got in a sublist of the menu binding. */
409 if (NILP (cachelist
))
411 CHECK_IMPURE (item1
);
412 XCONS (item1
)->cdr
= Fcons (Fcons (savedkey
, descrip
), def
);
416 XCONS (cachelist
)->car
= savedkey
;
417 XCONS (cachelist
)->cdr
= descrip
;
421 *descrip_ptr
= descrip
;
425 /* This is used as the handler when calling internal_condition_case_1. */
428 menu_item_enabled_p_1 (arg
)
434 /* Return non-nil if the command DEF is enabled when used as a menu item.
435 This is based on looking for a menu-enable property.
436 If NOTREAL is set, don't bother really computing this. */
439 menu_item_enabled_p (def
, notreal
)
443 Lisp_Object enabled
, tem
;
450 /* No property, or nil, means enable.
451 Otherwise, enable if value is not nil. */
452 tem
= Fget (def
, Qmenu_enable
);
454 /* (condition-case nil (eval tem)
456 enabled
= internal_condition_case_1 (Feval
, tem
, Qerror
,
457 menu_item_enabled_p_1
);
462 /* Look through KEYMAPS, a vector of keymaps that is NMAPS long,
463 and generate menu panes for them in menu_items.
464 If NOTREAL is nonzero,
465 don't bother really computing whether an item is enabled. */
468 keymap_panes (keymaps
, nmaps
, notreal
)
469 Lisp_Object
*keymaps
;
477 /* Loop over the given keymaps, making a pane for each map.
478 But don't make a pane that is empty--ignore that map instead.
479 P is the number of panes we have made so far. */
480 for (mapno
= 0; mapno
< nmaps
; mapno
++)
481 single_keymap_panes (keymaps
[mapno
], Qnil
, Qnil
, notreal
);
483 finish_menu_items ();
486 /* This is a recursive subroutine of keymap_panes.
487 It handles one keymap, KEYMAP.
488 The other arguments are passed along
489 or point to local variables of the previous function.
490 If NOTREAL is nonzero,
491 don't bother really computing whether an item is enabled. */
494 single_keymap_panes (keymap
, pane_name
, prefix
, notreal
)
496 Lisp_Object pane_name
;
500 Lisp_Object pending_maps
;
501 Lisp_Object tail
, item
, item1
, item_string
, table
;
502 struct gcpro gcpro1
, gcpro2
, gcpro3
, gcpro4
;
506 push_menu_pane (pane_name
, prefix
);
508 for (tail
= keymap
; CONSP (tail
); tail
= XCONS (tail
)->cdr
)
510 /* Look at each key binding, and if it has a menu string,
511 make a menu item from it. */
512 item
= XCONS (tail
)->car
;
515 item1
= XCONS (item
)->cdr
;
518 item_string
= XCONS (item1
)->car
;
519 if (STRINGP (item_string
))
521 /* This is the real definition--the function to run. */
523 /* These are the saved equivalent keyboard key sequence
524 and its key-description. */
526 Lisp_Object tem
, enabled
;
528 /* GCPRO because ...enabled_p will call eval
529 and ..._equiv_key may autoload something.
530 Protecting KEYMAP preserves everything we use;
531 aside from that, must protect whatever might be
532 a string. Since there's no GCPRO5, we refetch
533 item_string instead of protecting it. */
534 descrip
= def
= Qnil
;
535 GCPRO4 (keymap
, pending_maps
, def
, descrip
);
537 def
= menu_item_equiv_key (item_string
, item1
, &descrip
);
538 enabled
= menu_item_enabled_p (def
, notreal
);
542 item_string
= XCONS (item1
)->car
;
544 tem
= Fkeymapp (def
);
545 if (XSTRING (item_string
)->data
[0] == '@' && !NILP (tem
))
546 pending_maps
= Fcons (Fcons (def
, Fcons (item_string
, XCONS (item
)->car
)),
551 GCPRO4 (keymap
, pending_maps
, descrip
, item_string
);
552 submap
= get_keymap_1 (def
, 0, 1);
554 #ifndef USE_X_TOOLKIT
555 /* Indicate visually that this is a submenu. */
557 item_string
= concat2 (item_string
,
558 build_string (" >"));
560 /* If definition is nil, pass nil as the key. */
561 push_menu_item (item_string
, enabled
,
562 XCONS (item
)->car
, def
,
565 /* Display a submenu using the toolkit. */
568 push_submenu_start ();
569 single_keymap_panes (submap
, Qnil
,
570 XCONS (item
)->car
, notreal
);
578 else if (VECTORP (item
))
580 /* Loop over the char values represented in the vector. */
581 int len
= XVECTOR (item
)->size
;
583 for (c
= 0; c
< len
; c
++)
585 Lisp_Object character
;
586 XSETFASTINT (character
, c
);
587 item1
= XVECTOR (item
)->contents
[c
];
590 item_string
= XCONS (item1
)->car
;
591 if (STRINGP (item_string
))
595 /* These are the saved equivalent keyboard key sequence
596 and its key-description. */
598 Lisp_Object tem
, enabled
;
600 /* GCPRO because ...enabled_p will call eval
601 and ..._equiv_key may autoload something.
602 Protecting KEYMAP preserves everything we use;
603 aside from that, must protect whatever might be
604 a string. Since there's no GCPRO5, we refetch
605 item_string instead of protecting it. */
606 GCPRO4 (keymap
, pending_maps
, def
, descrip
);
607 descrip
= def
= Qnil
;
609 def
= menu_item_equiv_key (item_string
, item1
, &descrip
);
610 enabled
= menu_item_enabled_p (def
, notreal
);
614 item_string
= XCONS (item1
)->car
;
616 tem
= Fkeymapp (def
);
617 if (XSTRING (item_string
)->data
[0] == '@' && !NILP (tem
))
618 pending_maps
= Fcons (Fcons (def
, Fcons (item_string
, character
)),
623 GCPRO4 (keymap
, pending_maps
, descrip
, item_string
);
624 submap
= get_keymap_1 (def
, 0, 1);
626 #ifndef USE_X_TOOLKIT
628 item_string
= concat2 (item_string
,
629 build_string (" >"));
631 /* If definition is nil, pass nil as the key. */
632 push_menu_item (item_string
, enabled
, character
,
637 push_submenu_start ();
638 single_keymap_panes (submap
, Qnil
,
650 /* Process now any submenus which want to be panes at this level. */
651 while (!NILP (pending_maps
))
653 Lisp_Object elt
, eltcdr
, string
;
654 elt
= Fcar (pending_maps
);
655 eltcdr
= XCONS (elt
)->cdr
;
656 string
= XCONS (eltcdr
)->car
;
657 /* We no longer discard the @ from the beginning of the string here.
658 Instead, we do this in xmenu_show. */
659 single_keymap_panes (Fcar (elt
), string
,
660 XCONS (eltcdr
)->cdr
, notreal
);
661 pending_maps
= Fcdr (pending_maps
);
665 /* Push all the panes and items of a menu decsribed by the
666 alist-of-alists MENU.
667 This handles old-fashioned calls to x-popup-menu. */
677 for (tail
= menu
; !NILP (tail
); tail
= Fcdr (tail
))
679 Lisp_Object elt
, pane_name
, pane_data
;
681 pane_name
= Fcar (elt
);
682 CHECK_STRING (pane_name
, 0);
683 push_menu_pane (pane_name
, Qnil
);
684 pane_data
= Fcdr (elt
);
685 CHECK_CONS (pane_data
, 0);
686 list_of_items (pane_data
);
689 finish_menu_items ();
692 /* Push the items in a single pane defined by the alist PANE. */
698 Lisp_Object tail
, item
, item1
;
700 for (tail
= pane
; !NILP (tail
); tail
= Fcdr (tail
))
704 push_menu_item (item
, Qnil
, Qnil
, Qt
, Qnil
);
705 else if (NILP (item
))
706 push_left_right_boundary ();
709 CHECK_CONS (item
, 0);
711 CHECK_STRING (item1
, 1);
712 push_menu_item (item1
, Qt
, Fcdr (item
), Qt
, Qnil
);
717 DEFUN ("x-popup-menu", Fx_popup_menu
, Sx_popup_menu
, 2, 2, 0,
718 "Pop up a deck-of-cards menu and return user's selection.\n\
719 POSITION is a position specification. This is either a mouse button event\n\
720 or a list ((XOFFSET YOFFSET) WINDOW)\n\
721 where XOFFSET and YOFFSET are positions in pixels from the top left\n\
722 corner of WINDOW's frame. (WINDOW may be a frame object instead of a window.)\n\
723 This controls the position of the center of the first line\n\
724 in the first pane of the menu, not the top left of the menu as a whole.\n\
725 If POSITION is t, it means to use the current mouse position.\n\
727 MENU is a specifier for a menu. For the simplest case, MENU is a keymap.\n\
728 The menu items come from key bindings that have a menu string as well as\n\
729 a definition; actually, the \"definition\" in such a key binding looks like\n\
730 \(STRING . REAL-DEFINITION). To give the menu a title, put a string into\n\
731 the keymap as a top-level element.\n\n\
732 You can also use a list of keymaps as MENU.\n\
733 Then each keymap makes a separate pane.\n\
734 When MENU is a keymap or a list of keymaps, the return value\n\
735 is a list of events.\n\n\
736 Alternatively, you can specify a menu of multiple panes\n\
737 with a list of the form (TITLE PANE1 PANE2...),\n\
738 where each pane is a list of form (TITLE ITEM1 ITEM2...).\n\
739 Each ITEM is normally a cons cell (STRING . VALUE);\n\
740 but a string can appear as an item--that makes a nonselectable line\n\
742 With this form of menu, the return value is VALUE from the chosen item.\n\
744 If POSITION is nil, don't display the menu at all, just precalculate the\n\
745 cached information about equivalent key sequences.")
747 Lisp_Object position
, menu
;
749 int number_of_panes
, panes
;
750 Lisp_Object keymap
, tem
;
754 Lisp_Object selection
;
757 Lisp_Object x
, y
, window
;
762 if (! NILP (position
))
766 /* Decode the first argument: find the window and the coordinates. */
767 if (EQ (position
, Qt
)
768 || (CONSP (position
) && EQ (XCONS (position
)->car
, Qmenu_bar
)))
770 /* Use the mouse's current position. */
771 FRAME_PTR new_f
= selected_frame
;
772 Lisp_Object bar_window
;
776 if (mouse_position_hook
)
777 (*mouse_position_hook
) (&new_f
, 1, &bar_window
,
778 &part
, &x
, &y
, &time
);
780 XSETFRAME (window
, new_f
);
783 window
= selected_window
;
790 tem
= Fcar (position
);
793 window
= Fcar (Fcdr (position
));
795 y
= Fcar (Fcdr (tem
));
800 tem
= Fcar (Fcdr (position
)); /* EVENT_START (position) */
801 window
= Fcar (tem
); /* POSN_WINDOW (tem) */
802 tem
= Fcar (Fcdr (Fcdr (tem
))); /* POSN_WINDOW_POSN (tem) */
811 /* Decode where to put the menu. */
819 else if (WINDOWP (window
))
821 CHECK_LIVE_WINDOW (window
, 0);
822 f
= XFRAME (WINDOW_FRAME (XWINDOW (window
)));
824 xpos
= (FONT_WIDTH (f
->display
.x
->font
) * XWINDOW (window
)->left
);
825 ypos
= (f
->display
.x
->line_height
* XWINDOW (window
)->top
);
828 /* ??? Not really clean; should be CHECK_WINDOW_OR_FRAME,
829 but I don't want to make one now. */
830 CHECK_WINDOW (window
, 0);
839 /* Decode the menu items from what was specified. */
841 keymap
= Fkeymapp (menu
);
844 tem
= Fkeymapp (Fcar (menu
));
847 /* We were given a keymap. Extract menu info from the keymap. */
849 keymap
= get_keymap (menu
);
851 /* Extract the detailed info to make one pane. */
852 keymap_panes (&menu
, 1, NILP (position
));
854 /* Search for a string appearing directly as an element of the keymap.
855 That string is the title of the menu. */
856 prompt
= map_prompt (keymap
);
858 /* Make that be the pane title of the first pane. */
859 if (!NILP (prompt
) && menu_items_n_panes
>= 0)
860 XVECTOR (menu_items
)->contents
[MENU_ITEMS_PANE_NAME
] = prompt
;
864 else if (!NILP (tem
))
866 /* We were given a list of keymaps. */
867 int nmaps
= XFASTINT (Flength (menu
));
869 = (Lisp_Object
*) alloca (nmaps
* sizeof (Lisp_Object
));
874 /* The first keymap that has a prompt string
875 supplies the menu title. */
876 for (tem
= menu
, i
= 0; CONSP (tem
); tem
= Fcdr (tem
))
880 maps
[i
++] = keymap
= get_keymap (Fcar (tem
));
882 prompt
= map_prompt (keymap
);
883 if (NILP (title
) && !NILP (prompt
))
887 /* Extract the detailed info to make one pane. */
888 keymap_panes (maps
, nmaps
, NILP (position
));
890 /* Make the title be the pane title of the first pane. */
891 if (!NILP (title
) && menu_items_n_panes
>= 0)
892 XVECTOR (menu_items
)->contents
[MENU_ITEMS_PANE_NAME
] = title
;
898 /* We were given an old-fashioned menu. */
900 CHECK_STRING (title
, 1);
902 list_of_panes (Fcdr (menu
));
909 discard_menu_items ();
914 /* Display them in a menu. */
917 selection
= xmenu_show (f
, xpos
, ypos
, for_click
,
918 keymaps
, title
, &error_name
);
921 discard_menu_items ();
925 if (error_name
) error (error_name
);
929 DEFUN ("x-popup-dialog", Fx_popup_dialog
, Sx_popup_dialog
, 2, 2, 0,
930 "Pop up a dialog box and return user's selection.\n\
931 POSITION specifies which frame to use.\n\
932 This is normally a mouse button event or a window or frame.\n\
933 If POSITION is t, it means to use the frame the mouse is on.\n\
934 The dialog box appears in the middle of the specified frame.\n\
936 CONTENTS specifies the alternatives to display in the dialog box.\n\
937 It is a list of the form (TITLE ITEM1 ITEM2...).\n\
938 Each ITEM is a cons cell (STRING . VALUE).\n\
939 The return value is VALUE from the chosen item.\n\n\
940 An ITEM may also be just a string--that makes a nonselectable item.\n\
941 An ITEM may also be nil--that means to put all preceding items\n\
942 on the left of the dialog box and all following items on the right.\n\
943 \(By default, approximately half appear on each side.)")
945 Lisp_Object position
, contents
;
952 /* Decode the first argument: find the window or frame to use. */
953 if (EQ (position
, Qt
)
954 || (CONSP (position
) && EQ (XCONS (position
)->car
, Qmenu_bar
)))
956 #if 0 /* Using the frame the mouse is on may not be right. */
957 /* Use the mouse's current position. */
958 FRAME_PTR new_f
= selected_frame
;
959 Lisp_Object bar_window
;
964 (*mouse_position_hook
) (&new_f
, 1, &bar_window
, &part
, &x
, &y
, &time
);
967 XSETFRAME (window
, new_f
);
969 window
= selected_window
;
971 window
= selected_window
;
973 else if (CONSP (position
))
976 tem
= Fcar (position
);
978 window
= Fcar (Fcdr (position
));
981 tem
= Fcar (Fcdr (position
)); /* EVENT_START (position) */
982 window
= Fcar (tem
); /* POSN_WINDOW (tem) */
985 else if (WINDOWP (position
) || FRAMEP (position
))
988 /* Decode where to put the menu. */
992 else if (WINDOWP (window
))
994 CHECK_LIVE_WINDOW (window
, 0);
995 f
= XFRAME (WINDOW_FRAME (XWINDOW (window
)));
998 /* ??? Not really clean; should be CHECK_WINDOW_OR_FRAME,
999 but I don't want to make one now. */
1000 CHECK_WINDOW (window
, 0);
1002 #ifndef USE_X_TOOLKIT
1003 /* Display a menu with these alternatives
1004 in the middle of frame F. */
1006 Lisp_Object x
, y
, frame
, newpos
;
1007 XSETFRAME (frame
, f
);
1008 XSETINT (x
, x_pixel_width (f
) / 2);
1009 XSETINT (y
, x_pixel_height (f
) / 2);
1010 newpos
= Fcons (Fcons (x
, Fcons (y
, Qnil
)), Fcons (frame
, Qnil
));
1012 return Fx_popup_menu (newpos
,
1013 Fcons (Fcar (contents
), Fcons (contents
, Qnil
)));
1019 Lisp_Object selection
;
1021 /* Decode the dialog items from what was specified. */
1022 title
= Fcar (contents
);
1023 CHECK_STRING (title
, 1);
1025 list_of_panes (Fcons (contents
, Qnil
));
1027 /* Display them in a dialog box. */
1029 selection
= xdialog_show (f
, 0, title
, &error_name
);
1032 discard_menu_items ();
1034 if (error_name
) error (error_name
);
1040 #ifdef USE_X_TOOLKIT
1042 /* Loop in Xt until the menu pulldown or dialog popup has been
1043 popped down (deactivated).
1045 NOTE: All calls to popup_get_selection should be protected
1046 with BLOCK_INPUT, UNBLOCK_INPUT wrappers. */
1049 popup_get_selection (initial_event
, dpyinfo
, id
)
1050 XEvent
*initial_event
;
1051 struct x_display_info
*dpyinfo
;
1056 /* Define a queue to save up for later unreading
1057 all X events that don't pertain to the menu. */
1061 struct event_queue
*next
;
1064 struct event_queue
*queue
= NULL
;
1065 struct event_queue
*queue_tmp
;
1068 event
= *initial_event
;
1070 XtAppNextEvent (Xt_app_con
, &event
);
1074 /* Handle expose events for editor frames right away. */
1075 if (event
.type
== Expose
)
1076 process_expose_from_menu (event
);
1077 /* Make sure we don't consider buttons grabbed after menu goes. */
1078 else if (event
.type
== ButtonRelease
1079 && dpyinfo
->display
== event
.xbutton
.display
)
1080 dpyinfo
->grabbed
&= ~(1 << event
.xbutton
.button
);
1081 /* If the user presses a key, deactivate the menu.
1082 The user is likely to do that if we get wedged. */
1083 else if (event
.type
== KeyPress
1084 && dpyinfo
->display
== event
.xbutton
.display
)
1086 lw_destroy_all_widgets (id
);
1087 popup_activated_flag
= 0;
1091 /* Queue all events not for this popup,
1092 except for Expose, which we've already handled.
1093 Note that the X window is associated with the frame if this
1094 is a menu bar popup, but not if it's a dialog box. So we use
1095 x_non_menubar_window_to_frame, not x_any_window_to_frame. */
1096 if (event
.type
!= Expose
1097 && (event
.xany
.display
!= dpyinfo
->display
1098 || x_non_menubar_window_to_frame (dpyinfo
, event
.xany
.window
)))
1100 queue_tmp
= (struct event_queue
*) malloc (sizeof (struct event_queue
));
1102 if (queue_tmp
!= NULL
)
1104 queue_tmp
->event
= event
;
1105 queue_tmp
->next
= queue
;
1110 XtDispatchEvent (&event
);
1112 if (!popup_activated ())
1114 XtAppNextEvent (Xt_app_con
, &event
);
1117 /* Unread any events that we got but did not handle. */
1118 while (queue
!= NULL
)
1121 XPutBackEvent (queue_tmp
->event
.xany
.display
, &queue_tmp
->event
);
1122 queue
= queue_tmp
->next
;
1123 free ((char *)queue_tmp
);
1124 /* Cause these events to get read as soon as we UNBLOCK_INPUT. */
1125 interrupt_input_pending
= 1;
1129 /* Detect if a dialog or menu has been posted. */
1134 return popup_activated_flag
;
1138 /* This callback is invoked when the user selects a menubar cascade
1139 pushbutton, but before the pulldown menu is posted. */
1142 popup_activate_callback (widget
, id
, client_data
)
1145 XtPointer client_data
;
1147 popup_activated_flag
= 1;
1150 /* This callback is called from the menu bar pulldown menu
1151 when the user makes a selection.
1152 Figure out what the user chose
1153 and put the appropriate events into the keyboard buffer. */
1156 menubar_selection_callback (widget
, id
, client_data
)
1159 XtPointer client_data
;
1161 Lisp_Object prefix
, entry
;
1162 FRAME_PTR f
= XFRAME (XVECTOR (frame_vector
)->contents
[id
]);
1164 Lisp_Object
*subprefix_stack
;
1165 int submenu_depth
= 0;
1170 subprefix_stack
= (Lisp_Object
*) alloca (f
->menu_bar_items_used
* sizeof (Lisp_Object
));
1171 vector
= f
->menu_bar_vector
;
1174 while (i
< f
->menu_bar_items_used
)
1176 if (EQ (XVECTOR (vector
)->contents
[i
], Qnil
))
1178 subprefix_stack
[submenu_depth
++] = prefix
;
1182 else if (EQ (XVECTOR (vector
)->contents
[i
], Qlambda
))
1184 prefix
= subprefix_stack
[--submenu_depth
];
1187 else if (EQ (XVECTOR (vector
)->contents
[i
], Qt
))
1189 prefix
= XVECTOR (vector
)->contents
[i
+ MENU_ITEMS_PANE_PREFIX
];
1190 i
+= MENU_ITEMS_PANE_LENGTH
;
1194 entry
= XVECTOR (vector
)->contents
[i
+ MENU_ITEMS_ITEM_VALUE
];
1195 /* The EMACS_INT cast avoids a warning. There's no problem
1196 as long as pointers have enough bits to hold small integers. */
1197 if ((int) (EMACS_INT
) client_data
== i
)
1200 struct input_event buf
;
1203 XSETFRAME (frame
, f
);
1204 buf
.kind
= menu_bar_event
;
1205 buf
.frame_or_window
= Fcons (frame
, Fcons (Qmenu_bar
, Qnil
));
1206 kbd_buffer_store_event (&buf
);
1208 for (j
= 0; j
< submenu_depth
; j
++)
1209 if (!NILP (subprefix_stack
[j
]))
1211 buf
.kind
= menu_bar_event
;
1212 buf
.frame_or_window
= Fcons (frame
, subprefix_stack
[j
]);
1213 kbd_buffer_store_event (&buf
);
1218 buf
.kind
= menu_bar_event
;
1219 buf
.frame_or_window
= Fcons (frame
, prefix
);
1220 kbd_buffer_store_event (&buf
);
1223 buf
.kind
= menu_bar_event
;
1224 buf
.frame_or_window
= Fcons (frame
, entry
);
1225 kbd_buffer_store_event (&buf
);
1229 i
+= MENU_ITEMS_ITEM_LENGTH
;
1234 /* This callback is invoked when a dialog or menu is finished being
1235 used and has been unposted. */
1238 popup_deactivate_callback (widget
, id
, client_data
)
1241 XtPointer client_data
;
1243 popup_activated_flag
= 0;
1247 /* This recursively calls free_widget_value on the tree of widgets.
1248 It must free all data that was malloc'ed for these widget_values.
1249 In Emacs, many slots are pointers into the data of Lisp_Strings, and
1250 must be left alone. */
1253 free_menubar_widget_value_tree (wv
)
1258 wv
->name
= wv
->value
= wv
->key
= (char *) 0xDEADBEEF;
1260 if (wv
->contents
&& (wv
->contents
!= (widget_value
*)1))
1262 free_menubar_widget_value_tree (wv
->contents
);
1263 wv
->contents
= (widget_value
*) 0xDEADBEEF;
1267 free_menubar_widget_value_tree (wv
->next
);
1268 wv
->next
= (widget_value
*) 0xDEADBEEF;
1271 free_widget_value (wv
);
1275 /* Return a tree of widget_value structures for a menu bar item
1276 whose event type is ITEM_KEY (with string ITEM_NAME)
1277 and whose contents come from the list of keymaps MAPS. */
1279 static widget_value
*
1280 single_submenu (item_key
, item_name
, maps
)
1281 Lisp_Object item_key
, item_name
, maps
;
1283 widget_value
*wv
, *prev_wv
, *save_wv
, *first_wv
;
1285 int submenu_depth
= 0;
1288 Lisp_Object
*mapvec
;
1289 widget_value
**submenu_stack
;
1291 int previous_items
= menu_items_used
;
1293 length
= Flength (maps
);
1294 len
= XINT (length
);
1296 /* Convert the list MAPS into a vector MAPVEC. */
1297 mapvec
= (Lisp_Object
*) alloca (len
* sizeof (Lisp_Object
));
1298 for (i
= 0; i
< len
; i
++)
1300 mapvec
[i
] = Fcar (maps
);
1304 menu_items_n_panes
= 0;
1306 /* Loop over the given keymaps, making a pane for each map.
1307 But don't make a pane that is empty--ignore that map instead. */
1308 for (i
= 0; i
< len
; i
++)
1309 single_keymap_panes (mapvec
[i
], item_name
, item_key
, 0);
1311 /* Create a tree of widget_value objects
1312 representing the panes and their items. */
1315 = (widget_value
**) alloca (menu_items_used
* sizeof (widget_value
*));
1316 wv
= malloc_widget_value ();
1323 /* Loop over all panes and items made during this call
1324 and construct a tree of widget_value objects.
1325 Ignore the panes and items made by previous calls to
1326 single_submenu, even though those are also in menu_items. */
1328 while (i
< menu_items_used
)
1330 if (EQ (XVECTOR (menu_items
)->contents
[i
], Qnil
))
1332 submenu_stack
[submenu_depth
++] = save_wv
;
1337 else if (EQ (XVECTOR (menu_items
)->contents
[i
], Qlambda
))
1340 save_wv
= submenu_stack
[--submenu_depth
];
1343 else if (EQ (XVECTOR (menu_items
)->contents
[i
], Qt
)
1344 && submenu_depth
!= 0)
1345 i
+= MENU_ITEMS_PANE_LENGTH
;
1346 /* Ignore a nil in the item list.
1347 It's meaningful only for dialog boxes. */
1348 else if (EQ (XVECTOR (menu_items
)->contents
[i
], Qquote
))
1350 else if (EQ (XVECTOR (menu_items
)->contents
[i
], Qt
))
1352 /* Create a new pane. */
1353 Lisp_Object pane_name
, prefix
;
1355 pane_name
= XVECTOR (menu_items
)->contents
[i
+ MENU_ITEMS_PANE_NAME
];
1356 prefix
= XVECTOR (menu_items
)->contents
[i
+ MENU_ITEMS_PANE_PREFIX
];
1357 pane_string
= (NILP (pane_name
)
1358 ? "" : (char *) XSTRING (pane_name
)->data
);
1359 /* If there is just one top-level pane, put all its items directly
1360 under the top-level menu. */
1361 if (menu_items_n_panes
== 1)
1364 /* If the pane has a meaningful name,
1365 make the pane a top-level menu item
1366 with its items as a submenu beneath it. */
1367 if (strcmp (pane_string
, ""))
1369 wv
= malloc_widget_value ();
1373 first_wv
->contents
= wv
;
1374 wv
->name
= pane_string
;
1382 i
+= MENU_ITEMS_PANE_LENGTH
;
1386 /* Create a new item within current pane. */
1387 Lisp_Object item_name
, enable
, descrip
, def
;
1388 item_name
= XVECTOR (menu_items
)->contents
[i
+ MENU_ITEMS_ITEM_NAME
];
1389 enable
= XVECTOR (menu_items
)->contents
[i
+ MENU_ITEMS_ITEM_ENABLE
];
1391 = XVECTOR (menu_items
)->contents
[i
+ MENU_ITEMS_ITEM_EQUIV_KEY
];
1392 def
= XVECTOR (menu_items
)->contents
[i
+ MENU_ITEMS_ITEM_DEFINITION
];
1394 wv
= malloc_widget_value ();
1398 save_wv
->contents
= wv
;
1399 wv
->name
= (char *) XSTRING (item_name
)->data
;
1400 if (!NILP (descrip
))
1401 wv
->key
= (char *) XSTRING (descrip
)->data
;
1403 /* The EMACS_INT cast avoids a warning. There's no problem
1404 as long as pointers have enough bits to hold small integers. */
1405 wv
->call_data
= (!NILP (def
) ? (void *) (EMACS_INT
) i
: 0);
1406 wv
->enabled
= !NILP (enable
);
1409 i
+= MENU_ITEMS_ITEM_LENGTH
;
1416 extern void EmacsFrameSetCharSize ();
1418 /* Recompute all the widgets of frame F, when the menu bar
1419 has been changed. */
1422 update_frame_menubar (f
)
1425 struct x_display
*x
= f
->display
.x
;
1427 int menubar_changed
;
1429 Dimension shell_height
;
1431 /* We assume the menubar contents has changed if the global flag is set,
1432 or if the current buffer has changed, or if the menubar has never
1433 been updated before.
1435 menubar_changed
= (x
->menubar_widget
1436 && !XtIsManaged (x
->menubar_widget
));
1438 if (! (menubar_changed
))
1442 /* Save the size of the frame because the pane widget doesn't accept to
1443 resize itself. So force it. */
1447 /* Do the voodoo which means "I'm changing lots of things, don't try to
1448 refigure sizes until I'm done." */
1449 lw_refigure_widget (x
->column_widget
, False
);
1451 /* the order in which children are managed is the top to
1452 bottom order in which they are displayed in the paned window.
1453 First, remove the text-area widget.
1455 XtUnmanageChild (x
->edit_widget
);
1457 /* remove the menubar that is there now, and put up the menubar that
1460 if (menubar_changed
)
1462 XtManageChild (x
->menubar_widget
);
1463 XtMapWidget (x
->menubar_widget
);
1464 XtVaSetValues (x
->menubar_widget
, XtNmappedWhenManaged
, 1, 0);
1467 /* Re-manage the text-area widget, and then thrash the sizes. */
1468 XtManageChild (x
->edit_widget
);
1469 lw_refigure_widget (x
->column_widget
, True
);
1471 /* Force the pane widget to resize itself with the right values. */
1472 EmacsFrameSetCharSize (x
->edit_widget
, columns
, rows
);
1477 /* Set the contents of the menubar widgets of frame F.
1478 The argument FIRST_TIME is currently ignored;
1479 it is set the first time this is called, from initialize_frame_menubar. */
1482 set_frame_menubar (f
, first_time
)
1486 Widget menubar_widget
= f
->display
.x
->menubar_widget
;
1487 Lisp_Object tail
, items
, frame
;
1488 widget_value
*wv
, *first_wv
, *prev_wv
= 0;
1493 count
= inhibit_garbage_collection ();
1495 id
= frame_vector_add_frame (f
);
1497 wv
= malloc_widget_value ();
1498 wv
->name
= "menubar";
1502 items
= FRAME_MENU_BAR_ITEMS (f
);
1503 menu_items
= f
->menu_bar_vector
;
1504 menu_items_allocated
= XVECTOR (menu_items
)->size
;
1507 for (i
= 0; i
< XVECTOR (items
)->size
; i
+= 3)
1509 Lisp_Object key
, string
, maps
;
1511 key
= XVECTOR (items
)->contents
[i
];
1512 string
= XVECTOR (items
)->contents
[i
+ 1];
1513 maps
= XVECTOR (items
)->contents
[i
+ 2];
1517 wv
= single_submenu (key
, string
, maps
);
1521 first_wv
->contents
= wv
;
1522 /* Don't set wv->name here; GC during the loop might relocate it. */
1527 /* Now GC cannot happen during the lifetime of the widget_value,
1528 so it's safe to store data from a Lisp_String. */
1529 wv
= first_wv
->contents
;
1530 for (i
= 0; i
< XVECTOR (items
)->size
; i
+= 3)
1533 string
= XVECTOR (items
)->contents
[i
+ 1];
1536 wv
->name
= (char *) XSTRING (string
)->data
;
1540 finish_menu_items ();
1542 f
->menu_bar_vector
= menu_items
;
1543 f
->menu_bar_items_used
= menu_items_used
;
1546 unbind_to (count
, Qnil
);
1552 /* Disable resizing (done for Motif!) */
1553 lw_allow_resizing (f
->display
.x
->widget
, False
);
1555 /* The third arg is DEEP_P, which says to consider the entire
1556 menu trees we supply, rather than just the menu bar item names. */
1557 lw_modify_all_widgets ((LWLIB_ID
) id
, first_wv
, 1);
1559 /* Re-enable the edit widget to resize. */
1560 lw_allow_resizing (f
->display
.x
->widget
, True
);
1564 menubar_widget
= lw_create_widget ("menubar", "menubar",
1565 (LWLIB_ID
) id
, first_wv
,
1566 f
->display
.x
->column_widget
,
1568 popup_activate_callback
,
1569 menubar_selection_callback
,
1570 popup_deactivate_callback
);
1571 f
->display
.x
->menubar_widget
= menubar_widget
;
1576 = (f
->display
.x
->menubar_widget
1577 ? (f
->display
.x
->menubar_widget
->core
.height
1578 + f
->display
.x
->menubar_widget
->core
.border_width
)
1581 if (FRAME_EXTERNAL_MENU_BAR (f
))
1584 XtVaGetValues (f
->display
.x
->column_widget
,
1585 XtNinternalBorderWidth
, &ibw
, NULL
);
1586 menubar_size
+= ibw
;
1589 f
->display
.x
->menubar_height
= menubar_size
;
1592 free_menubar_widget_value_tree (first_wv
);
1594 update_frame_menubar (f
);
1599 /* Called from Fx_create_frame to create the inital menubar of a frame
1600 before it is mapped, so that the window is mapped with the menubar already
1601 there instead of us tacking it on later and thrashing the window after it
1605 initialize_frame_menubar (f
)
1608 /* This function is called before the first chance to redisplay
1609 the frame. It has to be, so the frame will have the right size. */
1610 FRAME_MENU_BAR_ITEMS (f
) = menu_bar_items (FRAME_MENU_BAR_ITEMS (f
));
1611 set_frame_menubar (f
, 1);
1614 /* Get rid of the menu bar of frame F, and free its storage.
1615 This is used when deleting a frame, and when turning off the menu bar. */
1618 free_frame_menubar (f
)
1621 Widget menubar_widget
;
1624 menubar_widget
= f
->display
.x
->menubar_widget
;
1628 id
= frame_vector_add_frame (f
);
1630 lw_destroy_all_widgets ((LWLIB_ID
) id
);
1631 XVECTOR (frame_vector
)->contents
[id
] = Qnil
;
1636 #endif /* USE_X_TOOLKIT */
1638 /* xmenu_show actually displays a menu using the panes and items in menu_items
1639 and returns the value selected from it.
1640 There are two versions of xmenu_show, one for Xt and one for Xlib.
1641 Both assume input is blocked by the caller. */
1643 /* F is the frame the menu is for.
1644 X and Y are the frame-relative specified position,
1645 relative to the inside upper left corner of the frame F.
1646 FOR_CLICK if this menu was invoked for a mouse click.
1647 KEYMAPS is 1 if this menu was specified with keymaps;
1648 in that case, we return a list containing the chosen item's value
1649 and perhaps also the pane's prefix.
1650 TITLE is the specified menu title.
1651 ERROR is a place to store an error message string in case of failure.
1652 (We return nil on failure, but the value doesn't actually matter.) */
1654 #ifdef USE_X_TOOLKIT
1656 /* We need a unique id for each widget handled by the Lucid Widget
1659 For the main windows, and popup menus, we use this counter,
1660 which we increment each time after use.
1662 For menu bars, we use the index of the frame in frame_vector
1664 LWLIB_ID widget_id_tick
;
1667 static Lisp_Object
*volatile menu_item_selection
;
1669 static Lisp_Object
*menu_item_selection
;
1673 popup_selection_callback (widget
, id
, client_data
)
1676 XtPointer client_data
;
1678 menu_item_selection
= (Lisp_Object
*) client_data
;
1682 xmenu_show (f
, x
, y
, for_click
, keymaps
, title
, error
)
1696 widget_value
*wv
, *save_wv
= 0, *first_wv
= 0, *prev_wv
= 0;
1697 widget_value
**submenu_stack
1698 = (widget_value
**) alloca (menu_items_used
* sizeof (widget_value
*));
1699 Lisp_Object
*subprefix_stack
1700 = (Lisp_Object
*) alloca (menu_items_used
* sizeof (Lisp_Object
));
1701 int submenu_depth
= 0;
1703 Position root_x
, root_y
;
1706 int next_release_must_exit
= 0;
1710 if (menu_items_used
<= MENU_ITEMS_PANE_LENGTH
)
1712 *error
= "Empty menu";
1716 /* Create a tree of widget_value objects
1717 representing the panes and their items. */
1718 wv
= malloc_widget_value ();
1725 /* Loop over all panes and items, filling in the tree. */
1727 while (i
< menu_items_used
)
1729 if (EQ (XVECTOR (menu_items
)->contents
[i
], Qnil
))
1731 submenu_stack
[submenu_depth
++] = save_wv
;
1737 else if (EQ (XVECTOR (menu_items
)->contents
[i
], Qlambda
))
1740 save_wv
= submenu_stack
[--submenu_depth
];
1744 else if (EQ (XVECTOR (menu_items
)->contents
[i
], Qt
)
1745 && submenu_depth
!= 0)
1746 i
+= MENU_ITEMS_PANE_LENGTH
;
1747 /* Ignore a nil in the item list.
1748 It's meaningful only for dialog boxes. */
1749 else if (EQ (XVECTOR (menu_items
)->contents
[i
], Qquote
))
1751 else if (EQ (XVECTOR (menu_items
)->contents
[i
], Qt
))
1753 /* Create a new pane. */
1754 Lisp_Object pane_name
, prefix
;
1756 pane_name
= XVECTOR (menu_items
)->contents
[i
+ MENU_ITEMS_PANE_NAME
];
1757 prefix
= XVECTOR (menu_items
)->contents
[i
+ MENU_ITEMS_PANE_PREFIX
];
1758 pane_string
= (NILP (pane_name
)
1759 ? "" : (char *) XSTRING (pane_name
)->data
);
1760 /* If there is just one top-level pane, put all its items directly
1761 under the top-level menu. */
1762 if (menu_items_n_panes
== 1)
1765 /* If the pane has a meaningful name,
1766 make the pane a top-level menu item
1767 with its items as a submenu beneath it. */
1768 if (!keymaps
&& strcmp (pane_string
, ""))
1770 wv
= malloc_widget_value ();
1774 first_wv
->contents
= wv
;
1775 wv
->name
= pane_string
;
1776 if (keymaps
&& !NILP (prefix
))
1783 else if (first_pane
)
1789 i
+= MENU_ITEMS_PANE_LENGTH
;
1793 /* Create a new item within current pane. */
1794 Lisp_Object item_name
, enable
, descrip
, def
;
1795 item_name
= XVECTOR (menu_items
)->contents
[i
+ MENU_ITEMS_ITEM_NAME
];
1796 enable
= XVECTOR (menu_items
)->contents
[i
+ MENU_ITEMS_ITEM_ENABLE
];
1798 = XVECTOR (menu_items
)->contents
[i
+ MENU_ITEMS_ITEM_EQUIV_KEY
];
1799 def
= XVECTOR (menu_items
)->contents
[i
+ MENU_ITEMS_ITEM_DEFINITION
];
1801 wv
= malloc_widget_value ();
1805 save_wv
->contents
= wv
;
1806 wv
->name
= (char *) XSTRING (item_name
)->data
;
1807 if (!NILP (descrip
))
1808 wv
->key
= (char *) XSTRING (descrip
)->data
;
1810 /* If this item has a null value,
1811 make the call_data null so that it won't display a box
1812 when the mouse is on it. */
1814 = (!NILP (def
) ? (void *) &XVECTOR (menu_items
)->contents
[i
] : 0);
1815 wv
->enabled
= !NILP (enable
);
1818 i
+= MENU_ITEMS_ITEM_LENGTH
;
1822 /* Deal with the title, if it is non-nil. */
1825 widget_value
*wv_title
= malloc_widget_value ();
1826 widget_value
*wv_sep1
= malloc_widget_value ();
1827 widget_value
*wv_sep2
= malloc_widget_value ();
1829 wv_sep2
->name
= "--";
1830 wv_sep2
->next
= first_wv
->contents
;
1832 wv_sep1
->name
= "--";
1833 wv_sep1
->next
= wv_sep2
;
1835 wv_title
->name
= (char *) XSTRING (title
)->data
;
1836 wv_title
->enabled
= True
;
1837 wv_title
->next
= wv_sep1
;
1838 first_wv
->contents
= wv_title
;
1841 /* Actually create the menu. */
1842 menu_id
= widget_id_tick
++;
1843 menu
= lw_create_widget ("popup", first_wv
->name
, menu_id
, first_wv
,
1844 f
->display
.x
->widget
, 1, 0,
1845 popup_selection_callback
,
1846 popup_deactivate_callback
);
1848 /* Don't allow any geometry request from the user. */
1849 XtSetArg (av
[ac
], XtNgeometry
, 0); ac
++;
1850 XtSetValues (menu
, av
, ac
);
1852 /* Free the widget_value objects we used to specify the contents. */
1853 free_menubar_widget_value_tree (first_wv
);
1855 /* No selection has been chosen yet. */
1856 menu_item_selection
= 0;
1858 /* Display the menu. */
1859 lw_popup_menu (menu
);
1860 popup_activated_flag
= 1;
1862 /* Process events that apply to the menu. */
1863 popup_get_selection ((XEvent
*) 0, FRAME_X_DISPLAY_INFO (f
), menu_id
);
1866 /* fp turned off the following statement and wrote a comment
1867 that it is unnecessary--that the menu has already disappeared.
1868 I observer that is not so. -- rms. */
1869 /* Make sure the menu disappears. */
1870 lw_destroy_all_widgets (menu_id
);
1873 /* Find the selected item, and its pane, to return
1874 the proper value. */
1875 if (menu_item_selection
!= 0)
1877 Lisp_Object prefix
, entry
;
1881 while (i
< menu_items_used
)
1883 if (EQ (XVECTOR (menu_items
)->contents
[i
], Qnil
))
1885 subprefix_stack
[submenu_depth
++] = prefix
;
1889 else if (EQ (XVECTOR (menu_items
)->contents
[i
], Qlambda
))
1891 prefix
= subprefix_stack
[--submenu_depth
];
1894 else if (EQ (XVECTOR (menu_items
)->contents
[i
], Qt
))
1897 = XVECTOR (menu_items
)->contents
[i
+ MENU_ITEMS_PANE_PREFIX
];
1898 i
+= MENU_ITEMS_PANE_LENGTH
;
1903 = XVECTOR (menu_items
)->contents
[i
+ MENU_ITEMS_ITEM_VALUE
];
1904 if (menu_item_selection
== &XVECTOR (menu_items
)->contents
[i
])
1910 entry
= Fcons (entry
, Qnil
);
1912 entry
= Fcons (prefix
, entry
);
1913 for (j
= submenu_depth
- 1; j
>= 0; j
--)
1914 if (!NILP (subprefix_stack
[j
]))
1915 entry
= Fcons (subprefix_stack
[j
], entry
);
1919 i
+= MENU_ITEMS_ITEM_LENGTH
;
1928 dialog_selection_callback (widget
, id
, client_data
)
1931 XtPointer client_data
;
1933 /* The EMACS_INT cast avoids a warning. There's no problem
1934 as long as pointers have enough bits to hold small integers. */
1935 if ((int) (EMACS_INT
) client_data
!= -1)
1936 menu_item_selection
= (Lisp_Object
*) client_data
;
1938 lw_destroy_all_widgets (id
);
1940 popup_activated_flag
= 0;
1943 static char * button_names
[] = {
1944 "button1", "button2", "button3", "button4", "button5",
1945 "button6", "button7", "button8", "button9", "button10" };
1948 xdialog_show (f
, keymaps
, title
, error
)
1954 int i
, nb_buttons
=0;
1957 char dialog_name
[6];
1959 widget_value
*wv
, *save_wv
= 0, *first_wv
= 0, *prev_wv
= 0;
1961 /* Number of elements seen so far, before boundary. */
1963 /* 1 means we've seen the boundary between left-hand elts and right-hand. */
1964 int boundary_seen
= 0;
1968 if (menu_items_n_panes
> 1)
1970 *error
= "Multiple panes in dialog box";
1974 /* Create a tree of widget_value objects
1975 representing the text label and buttons. */
1977 Lisp_Object pane_name
, prefix
;
1979 pane_name
= XVECTOR (menu_items
)->contents
[MENU_ITEMS_PANE_NAME
];
1980 prefix
= XVECTOR (menu_items
)->contents
[MENU_ITEMS_PANE_PREFIX
];
1981 pane_string
= (NILP (pane_name
)
1982 ? "" : (char *) XSTRING (pane_name
)->data
);
1983 prev_wv
= malloc_widget_value ();
1984 prev_wv
->value
= pane_string
;
1985 if (keymaps
&& !NILP (prefix
))
1987 prev_wv
->enabled
= 1;
1988 prev_wv
->name
= "message";
1991 /* Loop over all panes and items, filling in the tree. */
1992 i
= MENU_ITEMS_PANE_LENGTH
;
1993 while (i
< menu_items_used
)
1996 /* Create a new item within current pane. */
1997 Lisp_Object item_name
, enable
, descrip
;
1998 item_name
= XVECTOR (menu_items
)->contents
[i
+ MENU_ITEMS_ITEM_NAME
];
1999 enable
= XVECTOR (menu_items
)->contents
[i
+ MENU_ITEMS_ITEM_ENABLE
];
2001 = XVECTOR (menu_items
)->contents
[i
+ MENU_ITEMS_ITEM_EQUIV_KEY
];
2003 if (NILP (item_name
))
2005 free_menubar_widget_value_tree (first_wv
);
2006 *error
= "Submenu in dialog items";
2009 if (EQ (item_name
, Qquote
))
2011 /* This is the boundary between left-side elts
2012 and right-side elts. Stop incrementing right_count. */
2017 if (nb_buttons
>= 10)
2019 free_menubar_widget_value_tree (first_wv
);
2020 *error
= "Too many dialog items";
2024 wv
= malloc_widget_value ();
2026 wv
->name
= (char *) button_names
[nb_buttons
];
2027 if (!NILP (descrip
))
2028 wv
->key
= (char *) XSTRING (descrip
)->data
;
2029 wv
->value
= (char *) XSTRING (item_name
)->data
;
2030 wv
->call_data
= (void *) &XVECTOR (menu_items
)->contents
[i
];
2031 wv
->enabled
= !NILP (enable
);
2034 if (! boundary_seen
)
2038 i
+= MENU_ITEMS_ITEM_LENGTH
;
2041 /* If the boundary was not specified,
2042 by default put half on the left and half on the right. */
2043 if (! boundary_seen
)
2044 left_count
= nb_buttons
- nb_buttons
/ 2;
2046 wv
= malloc_widget_value ();
2047 wv
->name
= dialog_name
;
2049 /* Dialog boxes use a really stupid name encoding
2050 which specifies how many buttons to use
2051 and how many buttons are on the right.
2052 The Q means something also. */
2053 dialog_name
[0] = 'Q';
2054 dialog_name
[1] = '0' + nb_buttons
;
2055 dialog_name
[2] = 'B';
2056 dialog_name
[3] = 'R';
2057 /* Number of buttons to put on the right. */
2058 dialog_name
[4] = '0' + nb_buttons
- left_count
;
2060 wv
->contents
= first_wv
;
2064 /* Actually create the dialog. */
2065 dialog_id
= widget_id_tick
++;
2066 menu
= lw_create_widget (first_wv
->name
, "dialog", dialog_id
, first_wv
,
2067 f
->display
.x
->widget
, 1, 0,
2068 dialog_selection_callback
, 0);
2069 lw_modify_all_widgets (dialog_id
, first_wv
->contents
, True
);
2070 /* Free the widget_value objects we used to specify the contents. */
2071 free_menubar_widget_value_tree (first_wv
);
2073 /* No selection has been chosen yet. */
2074 menu_item_selection
= 0;
2076 /* Display the menu. */
2077 lw_pop_up_all_widgets (dialog_id
);
2078 popup_activated_flag
= 1;
2080 /* Process events that apply to the menu. */
2081 popup_get_selection ((XEvent
*) 0, FRAME_X_DISPLAY_INFO (f
), dialog_id
);
2083 /* Find the selected item, and its pane, to return
2084 the proper value. */
2085 if (menu_item_selection
!= 0)
2091 while (i
< menu_items_used
)
2095 if (EQ (XVECTOR (menu_items
)->contents
[i
], Qt
))
2098 = XVECTOR (menu_items
)->contents
[i
+ MENU_ITEMS_PANE_PREFIX
];
2099 i
+= MENU_ITEMS_PANE_LENGTH
;
2104 = XVECTOR (menu_items
)->contents
[i
+ MENU_ITEMS_ITEM_VALUE
];
2105 if (menu_item_selection
== &XVECTOR (menu_items
)->contents
[i
])
2109 entry
= Fcons (entry
, Qnil
);
2111 entry
= Fcons (prefix
, entry
);
2115 i
+= MENU_ITEMS_ITEM_LENGTH
;
2122 #else /* not USE_X_TOOLKIT */
2125 xmenu_show (f
, x
, y
, for_click
, keymaps
, title
, error
)
2135 int pane
, selidx
, lpane
, status
;
2136 Lisp_Object entry
, pane_prefix
;
2138 int ulx
, uly
, width
, height
;
2139 int dispwidth
, dispheight
;
2143 unsigned int dummy_uint
;
2146 if (menu_items_n_panes
== 0)
2149 if (menu_items_used
<= MENU_ITEMS_PANE_LENGTH
)
2151 *error
= "Empty menu";
2155 /* Figure out which root window F is on. */
2156 XGetGeometry (FRAME_X_DISPLAY (f
), FRAME_X_WINDOW (f
), &root
,
2157 &dummy_int
, &dummy_int
, &dummy_uint
, &dummy_uint
,
2158 &dummy_uint
, &dummy_uint
);
2160 /* Make the menu on that window. */
2161 menu
= XMenuCreate (FRAME_X_DISPLAY (f
), root
, "emacs");
2164 *error
= "Can't create menu";
2168 #ifdef HAVE_X_WINDOWS
2169 /* Adjust coordinates to relative to the outer (window manager) window. */
2172 int win_x
= 0, win_y
= 0;
2174 /* Find the position of the outside upper-left corner of
2175 the inner window, with respect to the outer window. */
2176 if (f
->display
.x
->parent_desc
!= FRAME_X_DISPLAY_INFO (f
)->root_window
)
2179 XTranslateCoordinates (FRAME_X_DISPLAY (f
),
2181 /* From-window, to-window. */
2182 f
->display
.x
->window_desc
,
2183 f
->display
.x
->parent_desc
,
2185 /* From-position, to-position. */
2186 0, 0, &win_x
, &win_y
,
2188 /* Child of window. */
2195 #endif /* HAVE_X_WINDOWS */
2197 /* Adjust coordinates to be root-window-relative. */
2198 x
+= f
->display
.x
->left_pos
;
2199 y
+= f
->display
.x
->top_pos
;
2201 /* Create all the necessary panes and their items. */
2203 while (i
< menu_items_used
)
2205 if (EQ (XVECTOR (menu_items
)->contents
[i
], Qt
))
2207 /* Create a new pane. */
2208 Lisp_Object pane_name
, prefix
;
2211 pane_name
= XVECTOR (menu_items
)->contents
[i
+ MENU_ITEMS_PANE_NAME
];
2212 prefix
= XVECTOR (menu_items
)->contents
[i
+ MENU_ITEMS_PANE_PREFIX
];
2213 pane_string
= (NILP (pane_name
)
2214 ? "" : (char *) XSTRING (pane_name
)->data
);
2215 if (keymaps
&& !NILP (prefix
))
2218 lpane
= XMenuAddPane (FRAME_X_DISPLAY (f
), menu
, pane_string
, TRUE
);
2219 if (lpane
== XM_FAILURE
)
2221 XMenuDestroy (FRAME_X_DISPLAY (f
), menu
);
2222 *error
= "Can't create pane";
2225 i
+= MENU_ITEMS_PANE_LENGTH
;
2227 /* Find the width of the widest item in this pane. */
2230 while (j
< menu_items_used
)
2233 item
= XVECTOR (menu_items
)->contents
[j
];
2241 width
= XSTRING (item
)->size
;
2242 if (width
> maxwidth
)
2245 j
+= MENU_ITEMS_ITEM_LENGTH
;
2248 /* Ignore a nil in the item list.
2249 It's meaningful only for dialog boxes. */
2250 else if (EQ (XVECTOR (menu_items
)->contents
[i
], Qquote
))
2254 /* Create a new item within current pane. */
2255 Lisp_Object item_name
, enable
, descrip
;
2256 unsigned char *item_data
;
2258 item_name
= XVECTOR (menu_items
)->contents
[i
+ MENU_ITEMS_ITEM_NAME
];
2259 enable
= XVECTOR (menu_items
)->contents
[i
+ MENU_ITEMS_ITEM_ENABLE
];
2261 = XVECTOR (menu_items
)->contents
[i
+ MENU_ITEMS_ITEM_EQUIV_KEY
];
2262 if (!NILP (descrip
))
2264 int gap
= maxwidth
- XSTRING (item_name
)->size
;
2267 spacer
= Fmake_string (make_number (gap
), make_number (' '));
2268 item_name
= concat2 (item_name
, spacer
);
2269 item_name
= concat2 (item_name
, descrip
);
2270 item_data
= XSTRING (item_name
)->data
;
2272 /* if alloca is fast, use that to make the space,
2273 to reduce gc needs. */
2275 = (unsigned char *) alloca (maxwidth
2276 + XSTRING (descrip
)->size
+ 1);
2277 bcopy (XSTRING (item_name
)->data
, item_data
,
2278 XSTRING (item_name
)->size
);
2279 for (j
= XSTRING (item_name
)->size
; j
< maxwidth
; j
++)
2281 bcopy (XSTRING (descrip
)->data
, item_data
+ j
,
2282 XSTRING (descrip
)->size
);
2283 item_data
[j
+ XSTRING (descrip
)->size
] = 0;
2287 item_data
= XSTRING (item_name
)->data
;
2289 if (XMenuAddSelection (FRAME_X_DISPLAY (f
),
2290 menu
, lpane
, 0, item_data
,
2294 XMenuDestroy (FRAME_X_DISPLAY (f
), menu
);
2295 *error
= "Can't add selection to menu";
2298 i
+= MENU_ITEMS_ITEM_LENGTH
;
2302 /* All set and ready to fly. */
2303 XMenuRecompute (FRAME_X_DISPLAY (f
), menu
);
2304 dispwidth
= DisplayWidth (FRAME_X_DISPLAY (f
),
2305 XScreenNumberOfScreen (FRAME_X_SCREEN (f
)));
2306 dispheight
= DisplayHeight (FRAME_X_DISPLAY (f
),
2307 XScreenNumberOfScreen (FRAME_X_SCREEN (f
)));
2308 x
= min (x
, dispwidth
);
2309 y
= min (y
, dispheight
);
2312 XMenuLocate (FRAME_X_DISPLAY (f
), menu
, 0, 0, x
, y
,
2313 &ulx
, &uly
, &width
, &height
);
2314 if (ulx
+width
> dispwidth
)
2316 x
-= (ulx
+ width
) - dispwidth
;
2317 ulx
= dispwidth
- width
;
2319 if (uly
+height
> dispheight
)
2321 y
-= (uly
+ height
) - dispheight
;
2322 uly
= dispheight
- height
;
2324 if (ulx
< 0) x
-= ulx
;
2325 if (uly
< 0) y
-= uly
;
2327 XMenuSetAEQ (menu
, TRUE
);
2328 XMenuSetFreeze (menu
, TRUE
);
2331 status
= XMenuActivate (FRAME_X_DISPLAY (f
), menu
, &pane
, &selidx
,
2332 x
, y
, ButtonReleaseMask
, &datap
);
2335 #ifdef HAVE_X_WINDOWS
2336 /* Assume the mouse has moved out of the X window.
2337 If it has actually moved in, we will get an EnterNotify. */
2338 x_mouse_leave (FRAME_X_DISPLAY_INFO (f
));
2345 fprintf (stderr
, "pane= %d line = %d\n", panes
, selidx
);
2348 /* Find the item number SELIDX in pane number PANE. */
2350 while (i
< menu_items_used
)
2352 if (EQ (XVECTOR (menu_items
)->contents
[i
], Qt
))
2356 = XVECTOR (menu_items
)->contents
[i
+ MENU_ITEMS_PANE_PREFIX
];
2358 i
+= MENU_ITEMS_PANE_LENGTH
;
2367 = XVECTOR (menu_items
)->contents
[i
+ MENU_ITEMS_ITEM_VALUE
];
2370 entry
= Fcons (entry
, Qnil
);
2371 if (!NILP (pane_prefix
))
2372 entry
= Fcons (pane_prefix
, entry
);
2378 i
+= MENU_ITEMS_ITEM_LENGTH
;
2384 *error
= "Can't activate menu";
2390 XMenuDestroy (FRAME_X_DISPLAY (f
), menu
);
2392 #ifdef HAVE_X_WINDOWS
2393 /* State that no mouse buttons are now held.
2394 (The oldXMenu code doesn't track this info for us.)
2395 That is not necessarily true, but the fiction leads to reasonable
2396 results, and it is a pain to ask which are actually held now. */
2397 FRAME_X_DISPLAY_INFO (f
)->grabbed
= 0;
2403 #endif /* not USE_X_TOOLKIT */
2407 staticpro (&menu_items
);
2410 #ifdef USE_X_TOOLKIT
2411 widget_id_tick
= (1<<16);
2414 staticpro (&frame_vector
);
2415 frame_vector
= Fmake_vector (make_number (10), Qnil
);
2417 defsubr (&Sx_popup_menu
);
2418 defsubr (&Sx_popup_dialog
);