1 /* X Communication module for terminals which understand the X protocol.
2 Copyright (C) 1986, 1988, 1993, 1994, 1996, 1999, 2000, 2001, 2003, 2004,
3 2005 Free Software Foundation, Inc.
5 This file is part of GNU Emacs.
7 GNU Emacs is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
12 GNU Emacs is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GNU Emacs; see the file COPYING. If not, write to
19 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA. */
22 /* X pop-up deck-of-cards menu facility for GNU Emacs.
24 * Written by Jon Arnold and Roman Budzianowski
25 * Mods and rewrite by Robert Krawitz
29 /* Modified by Fred Pierresteguy on December 93
30 to make the popup menus and menubar use the Xt. */
32 /* Rewritten for clarity and GC protection by rms in Feb 94. */
36 /* On 4.3 this loses if it comes after xterm.h. */
42 #include "termhooks.h"
47 #include "blockinput.h"
51 #include "sysselect.h"
58 /* This may include sys/types.h, and that somehow loses
59 if this is not done before the other system files. */
63 /* Load sys/types.h if not already loaded.
64 In some systems loading it twice is suicidal. */
66 #include <sys/types.h>
69 #include "dispextern.h"
72 /* Defining HAVE_MULTILINGUAL_MENU would mean that the toolkit menu
73 code accepts the Emacs internal encoding. */
74 #undef HAVE_MULTILINGUAL_MENU
78 #include <X11/IntrinsicP.h>
79 #include <X11/CoreP.h>
80 #include <X11/StringDefs.h>
81 #include <X11/Shell.h>
83 #include <X11/Xaw/Paned.h>
84 #endif /* USE_LUCID */
85 #include "../lwlib/lwlib.h"
86 #else /* not USE_X_TOOLKIT */
88 #include "../oldXMenu/XMenu.h"
90 #endif /* not USE_X_TOOLKIT */
91 #endif /* HAVE_X_WINDOWS */
98 Lisp_Object Vmenu_updating_frame
;
100 Lisp_Object Qdebug_on_next_call
;
102 extern Lisp_Object Qmenu_bar
;
104 extern Lisp_Object QCtoggle
, QCradio
;
106 extern Lisp_Object Voverriding_local_map
;
107 extern Lisp_Object Voverriding_local_map_menu_flag
;
109 extern Lisp_Object Qoverriding_local_map
, Qoverriding_terminal_local_map
;
111 extern Lisp_Object Qmenu_bar_update_hook
;
114 extern void set_frame_menubar
P_ ((FRAME_PTR
, int, int));
115 extern XtAppContext Xt_app_con
;
117 static Lisp_Object xdialog_show
P_ ((FRAME_PTR
, int, Lisp_Object
, char **));
118 static void popup_get_selection
P_ ((XEvent
*, struct x_display_info
*,
121 /* Define HAVE_BOXES if menus can handle radio and toggle buttons. */
124 #endif /* USE_X_TOOLKIT */
129 extern void set_frame_menubar
P_ ((FRAME_PTR
, int, int));
130 static Lisp_Object xdialog_show
P_ ((FRAME_PTR
, int, Lisp_Object
, char **));
133 /* This is how to deal with multibyte text if HAVE_MULTILINGUAL_MENU
134 isn't defined. The use of HAVE_MULTILINGUAL_MENU could probably be
135 confined to an extended version of this with sections of code below
136 using it unconditionally. */
138 /* gtk just uses utf-8. */
139 # define ENCODE_MENU_STRING(str) ENCODE_UTF_8 (str)
141 # define ENCODE_MENU_STRING(str) string_make_unibyte (str)
144 static void push_menu_item
P_ ((Lisp_Object
, Lisp_Object
, Lisp_Object
,
145 Lisp_Object
, Lisp_Object
, Lisp_Object
,
146 Lisp_Object
, Lisp_Object
));
147 static int update_frame_menubar
P_ ((struct frame
*));
148 static Lisp_Object xmenu_show
P_ ((struct frame
*, int, int, int, int,
149 Lisp_Object
, char **));
150 static void keymap_panes
P_ ((Lisp_Object
*, int, int));
151 static void single_keymap_panes
P_ ((Lisp_Object
, Lisp_Object
, Lisp_Object
,
153 static void list_of_panes
P_ ((Lisp_Object
));
154 static void list_of_items
P_ ((Lisp_Object
));
157 /* This holds a Lisp vector that holds the results of decoding
158 the keymaps or alist-of-alists that specify a menu.
160 It describes the panes and items within the panes.
162 Each pane is described by 3 elements in the vector:
163 t, the pane name, the pane's prefix key.
164 Then follow the pane's items, with 5 elements per item:
165 the item string, the enable flag, the item's value,
166 the definition, and the equivalent keyboard key's description string.
168 In some cases, multiple levels of menus may be described.
169 A single vector slot containing nil indicates the start of a submenu.
170 A single vector slot containing lambda indicates the end of a submenu.
171 The submenu follows a menu item which is the way to reach the submenu.
173 A single vector slot containing quote indicates that the
174 following items should appear on the right of a dialog box.
176 Using a Lisp vector to hold this information while we decode it
177 takes care of protecting all the data from GC. */
179 #define MENU_ITEMS_PANE_NAME 1
180 #define MENU_ITEMS_PANE_PREFIX 2
181 #define MENU_ITEMS_PANE_LENGTH 3
185 MENU_ITEMS_ITEM_NAME
= 0,
186 MENU_ITEMS_ITEM_ENABLE
,
187 MENU_ITEMS_ITEM_VALUE
,
188 MENU_ITEMS_ITEM_EQUIV_KEY
,
189 MENU_ITEMS_ITEM_DEFINITION
,
190 MENU_ITEMS_ITEM_TYPE
,
191 MENU_ITEMS_ITEM_SELECTED
,
192 MENU_ITEMS_ITEM_HELP
,
193 MENU_ITEMS_ITEM_LENGTH
196 static Lisp_Object menu_items
;
198 /* If non-nil, means that the global vars defined here are already in use.
199 Used to detect cases where we try to re-enter this non-reentrant code. */
200 static Lisp_Object menu_items_inuse
;
202 /* Number of slots currently allocated in menu_items. */
203 static int menu_items_allocated
;
205 /* This is the index in menu_items of the first empty slot. */
206 static int menu_items_used
;
208 /* The number of panes currently recorded in menu_items,
209 excluding those within submenus. */
210 static int menu_items_n_panes
;
212 /* Current depth within submenus. */
213 static int menu_items_submenu_depth
;
215 /* Flag which when set indicates a dialog or menu has been posted by
216 Xt on behalf of one of the widget sets. */
217 static int popup_activated_flag
;
219 static int next_menubar_widget_id
;
221 /* This is set nonzero after the user activates the menu bar, and set
222 to zero again after the menu bars are redisplayed by prepare_menu_bar.
223 While it is nonzero, all calls to set_frame_menubar go deep.
225 I don't understand why this is needed, but it does seem to be
226 needed on Motif, according to Marcus Daniels <marcus@sysc.pdx.edu>. */
228 int pending_menu_activation
;
232 /* Return the frame whose ->output_data.x->id equals ID, or 0 if none. */
234 static struct frame
*
235 menubar_id_to_frame (id
)
238 Lisp_Object tail
, frame
;
241 for (tail
= Vframe_list
; GC_CONSP (tail
); tail
= XCDR (tail
))
244 if (!GC_FRAMEP (frame
))
247 if (!FRAME_WINDOW_P (f
))
249 if (f
->output_data
.x
->id
== id
)
257 /* Initialize the menu_items structure if we haven't already done so.
258 Also mark it as currently empty. */
263 if (NILP (menu_items
))
265 menu_items_allocated
= 60;
266 menu_items
= Fmake_vector (make_number (menu_items_allocated
), Qnil
);
269 if (!NILP (menu_items_inuse
))
270 error ("Trying to use a menu from within a menu-entry");
271 menu_items_inuse
= Qt
;
273 menu_items_n_panes
= 0;
274 menu_items_submenu_depth
= 0;
277 /* Call at the end of generating the data in menu_items. */
285 unuse_menu_items (dummy
)
288 return menu_items_inuse
= Qnil
;
291 /* Call when finished using the data for the current menu
295 discard_menu_items ()
297 /* Free the structure if it is especially large.
298 Otherwise, hold on to it, to save time. */
299 if (menu_items_allocated
> 200)
302 menu_items_allocated
= 0;
304 xassert (NILP (menu_items_inuse
));
307 /* Make the menu_items vector twice as large. */
313 int old_size
= menu_items_allocated
;
316 menu_items_allocated
*= 2;
317 menu_items
= Fmake_vector (make_number (menu_items_allocated
), Qnil
);
318 bcopy (XVECTOR (old
)->contents
, XVECTOR (menu_items
)->contents
,
319 old_size
* sizeof (Lisp_Object
));
322 /* Begin a submenu. */
325 push_submenu_start ()
327 if (menu_items_used
+ 1 > menu_items_allocated
)
330 XVECTOR (menu_items
)->contents
[menu_items_used
++] = Qnil
;
331 menu_items_submenu_depth
++;
339 if (menu_items_used
+ 1 > menu_items_allocated
)
342 XVECTOR (menu_items
)->contents
[menu_items_used
++] = Qlambda
;
343 menu_items_submenu_depth
--;
346 /* Indicate boundary between left and right. */
349 push_left_right_boundary ()
351 if (menu_items_used
+ 1 > menu_items_allocated
)
354 XVECTOR (menu_items
)->contents
[menu_items_used
++] = Qquote
;
357 /* Start a new menu pane in menu_items.
358 NAME is the pane name. PREFIX_VEC is a prefix key for this pane. */
361 push_menu_pane (name
, prefix_vec
)
362 Lisp_Object name
, prefix_vec
;
364 if (menu_items_used
+ MENU_ITEMS_PANE_LENGTH
> menu_items_allocated
)
367 if (menu_items_submenu_depth
== 0)
368 menu_items_n_panes
++;
369 XVECTOR (menu_items
)->contents
[menu_items_used
++] = Qt
;
370 XVECTOR (menu_items
)->contents
[menu_items_used
++] = name
;
371 XVECTOR (menu_items
)->contents
[menu_items_used
++] = prefix_vec
;
374 /* Push one menu item into the current pane. NAME is the string to
375 display. ENABLE if non-nil means this item can be selected. KEY
376 is the key generated by choosing this item, or nil if this item
377 doesn't really have a definition. DEF is the definition of this
378 item. EQUIV is the textual description of the keyboard equivalent
379 for this item (or nil if none). TYPE is the type of this menu
380 item, one of nil, `toggle' or `radio'. */
383 push_menu_item (name
, enable
, key
, def
, equiv
, type
, selected
, help
)
384 Lisp_Object name
, enable
, key
, def
, equiv
, type
, selected
, help
;
386 if (menu_items_used
+ MENU_ITEMS_ITEM_LENGTH
> menu_items_allocated
)
389 XVECTOR (menu_items
)->contents
[menu_items_used
++] = name
;
390 XVECTOR (menu_items
)->contents
[menu_items_used
++] = enable
;
391 XVECTOR (menu_items
)->contents
[menu_items_used
++] = key
;
392 XVECTOR (menu_items
)->contents
[menu_items_used
++] = equiv
;
393 XVECTOR (menu_items
)->contents
[menu_items_used
++] = def
;
394 XVECTOR (menu_items
)->contents
[menu_items_used
++] = type
;
395 XVECTOR (menu_items
)->contents
[menu_items_used
++] = selected
;
396 XVECTOR (menu_items
)->contents
[menu_items_used
++] = help
;
399 /* Look through KEYMAPS, a vector of keymaps that is NMAPS long,
400 and generate menu panes for them in menu_items.
401 If NOTREAL is nonzero,
402 don't bother really computing whether an item is enabled. */
405 keymap_panes (keymaps
, nmaps
, notreal
)
406 Lisp_Object
*keymaps
;
414 /* Loop over the given keymaps, making a pane for each map.
415 But don't make a pane that is empty--ignore that map instead.
416 P is the number of panes we have made so far. */
417 for (mapno
= 0; mapno
< nmaps
; mapno
++)
418 single_keymap_panes (keymaps
[mapno
],
419 Fkeymap_prompt (keymaps
[mapno
]), Qnil
, notreal
, 10);
421 finish_menu_items ();
424 /* Args passed between single_keymap_panes and single_menu_item. */
427 Lisp_Object pending_maps
;
428 int maxdepth
, notreal
;
432 static void single_menu_item
P_ ((Lisp_Object
, Lisp_Object
, Lisp_Object
,
435 /* This is a recursive subroutine of keymap_panes.
436 It handles one keymap, KEYMAP.
437 The other arguments are passed along
438 or point to local variables of the previous function.
439 If NOTREAL is nonzero, only check for equivalent key bindings, don't
440 evaluate expressions in menu items and don't make any menu.
442 If we encounter submenus deeper than MAXDEPTH levels, ignore them. */
445 single_keymap_panes (keymap
, pane_name
, prefix
, notreal
, maxdepth
)
447 Lisp_Object pane_name
;
455 skp
.pending_maps
= Qnil
;
456 skp
.maxdepth
= maxdepth
;
457 skp
.notreal
= notreal
;
463 push_menu_pane (pane_name
, prefix
);
466 /* Remember index for first item in this pane so we can go back and
467 add a prefix when (if) we see the first button. After that, notbuttons
468 is set to 0, to mark that we have seen a button and all non button
469 items need a prefix. */
470 skp
.notbuttons
= menu_items_used
;
473 GCPRO1 (skp
.pending_maps
);
474 map_keymap (keymap
, single_menu_item
, Qnil
, &skp
, 1);
477 /* Process now any submenus which want to be panes at this level. */
478 while (CONSP (skp
.pending_maps
))
480 Lisp_Object elt
, eltcdr
, string
;
481 elt
= XCAR (skp
.pending_maps
);
483 string
= XCAR (eltcdr
);
484 /* We no longer discard the @ from the beginning of the string here.
485 Instead, we do this in xmenu_show. */
486 single_keymap_panes (Fcar (elt
), string
,
487 XCDR (eltcdr
), notreal
, maxdepth
- 1);
488 skp
.pending_maps
= XCDR (skp
.pending_maps
);
492 /* This is a subroutine of single_keymap_panes that handles one
494 KEY is a key in a keymap and ITEM is its binding.
495 SKP->PENDING_MAPS_PTR is a list of keymaps waiting to be made into
497 If SKP->NOTREAL is nonzero, only check for equivalent key bindings, don't
498 evaluate expressions in menu items and don't make any menu.
499 If we encounter submenus deeper than SKP->MAXDEPTH levels, ignore them.
500 SKP->NOTBUTTONS is only used when simulating toggle boxes and radio
501 buttons. It keeps track of if we have seen a button in this menu or
505 single_menu_item (key
, item
, dummy
, skp_v
)
506 Lisp_Object key
, item
, dummy
;
509 Lisp_Object map
, item_string
, enabled
;
510 struct gcpro gcpro1
, gcpro2
;
512 struct skp
*skp
= skp_v
;
514 /* Parse the menu item and leave the result in item_properties. */
516 res
= parse_menu_item (item
, skp
->notreal
, 0);
519 return; /* Not a menu item. */
521 map
= XVECTOR (item_properties
)->contents
[ITEM_PROPERTY_MAP
];
525 /* We don't want to make a menu, just traverse the keymaps to
526 precompute equivalent key bindings. */
528 single_keymap_panes (map
, Qnil
, key
, 1, skp
->maxdepth
- 1);
532 enabled
= XVECTOR (item_properties
)->contents
[ITEM_PROPERTY_ENABLE
];
533 item_string
= XVECTOR (item_properties
)->contents
[ITEM_PROPERTY_NAME
];
535 if (!NILP (map
) && SREF (item_string
, 0) == '@')
538 /* An enabled separate pane. Remember this to handle it later. */
539 skp
->pending_maps
= Fcons (Fcons (map
, Fcons (item_string
, key
)),
545 /* Simulate radio buttons and toggle boxes by putting a prefix in
548 Lisp_Object prefix
= Qnil
;
549 Lisp_Object type
= XVECTOR (item_properties
)->contents
[ITEM_PROPERTY_TYPE
];
553 = XVECTOR (item_properties
)->contents
[ITEM_PROPERTY_SELECTED
];
556 /* The first button. Line up previous items in this menu. */
558 int index
= skp
->notbuttons
; /* Index for first item this menu. */
561 while (index
< menu_items_used
)
564 = XVECTOR (menu_items
)->contents
[index
+ MENU_ITEMS_ITEM_NAME
];
568 submenu
++; /* Skip sub menu. */
570 else if (EQ (tem
, Qlambda
))
573 submenu
--; /* End sub menu. */
575 else if (EQ (tem
, Qt
))
576 index
+= 3; /* Skip new pane marker. */
577 else if (EQ (tem
, Qquote
))
578 index
++; /* Skip a left, right divider. */
581 if (!submenu
&& SREF (tem
, 0) != '\0'
582 && SREF (tem
, 0) != '-')
583 XVECTOR (menu_items
)->contents
[index
+ MENU_ITEMS_ITEM_NAME
]
584 = concat2 (build_string (" "), tem
);
585 index
+= MENU_ITEMS_ITEM_LENGTH
;
591 /* Calculate prefix, if any, for this item. */
592 if (EQ (type
, QCtoggle
))
593 prefix
= build_string (NILP (selected
) ? "[ ] " : "[X] ");
594 else if (EQ (type
, QCradio
))
595 prefix
= build_string (NILP (selected
) ? "( ) " : "(*) ");
597 /* Not a button. If we have earlier buttons, then we need a prefix. */
598 else if (!skp
->notbuttons
&& SREF (item_string
, 0) != '\0'
599 && SREF (item_string
, 0) != '-')
600 prefix
= build_string (" ");
603 item_string
= concat2 (prefix
, item_string
);
605 #endif /* not HAVE_BOXES */
607 #if ! defined (USE_X_TOOLKIT) && ! defined (USE_GTK)
609 /* Indicate visually that this is a submenu. */
610 item_string
= concat2 (item_string
, build_string (" >"));
613 push_menu_item (item_string
, enabled
, key
,
614 XVECTOR (item_properties
)->contents
[ITEM_PROPERTY_DEF
],
615 XVECTOR (item_properties
)->contents
[ITEM_PROPERTY_KEYEQ
],
616 XVECTOR (item_properties
)->contents
[ITEM_PROPERTY_TYPE
],
617 XVECTOR (item_properties
)->contents
[ITEM_PROPERTY_SELECTED
],
618 XVECTOR (item_properties
)->contents
[ITEM_PROPERTY_HELP
]);
620 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
621 /* Display a submenu using the toolkit. */
622 if (! (NILP (map
) || NILP (enabled
)))
624 push_submenu_start ();
625 single_keymap_panes (map
, Qnil
, key
, 0, skp
->maxdepth
- 1);
631 /* Push all the panes and items of a menu described by the
632 alist-of-alists MENU.
633 This handles old-fashioned calls to x-popup-menu. */
643 for (tail
= menu
; CONSP (tail
); tail
= XCDR (tail
))
645 Lisp_Object elt
, pane_name
, pane_data
;
647 pane_name
= Fcar (elt
);
648 CHECK_STRING (pane_name
);
649 push_menu_pane (ENCODE_MENU_STRING (pane_name
), Qnil
);
650 pane_data
= Fcdr (elt
);
651 CHECK_CONS (pane_data
);
652 list_of_items (pane_data
);
655 finish_menu_items ();
658 /* Push the items in a single pane defined by the alist PANE. */
664 Lisp_Object tail
, item
, item1
;
666 for (tail
= pane
; CONSP (tail
); tail
= XCDR (tail
))
670 push_menu_item (ENCODE_MENU_STRING (item
), Qnil
, Qnil
, Qt
,
671 Qnil
, Qnil
, Qnil
, Qnil
);
672 else if (CONSP (item
))
675 CHECK_STRING (item1
);
676 push_menu_item (ENCODE_MENU_STRING (item1
), Qt
, XCDR (item
),
677 Qt
, Qnil
, Qnil
, Qnil
, Qnil
);
680 push_left_right_boundary ();
685 #ifdef HAVE_X_WINDOWS
686 /* Return the mouse position in *X and *Y. The coordinates are window
687 relative for the edit window in frame F.
688 This is for Fx_popup_menu. The mouse_position_hook can not
689 be used for X, as it returns window relative coordinates
690 for the window where the mouse is in. This could be the menu bar,
691 the scroll bar or the edit window. Fx_popup_menu needs to be
692 sure it is the edit window. */
694 mouse_position_for_popup (f
, x
, y
)
699 Window root
, dummy_window
;
704 XQueryPointer (FRAME_X_DISPLAY (f
),
705 DefaultRootWindow (FRAME_X_DISPLAY (f
)),
707 /* The root window which contains the pointer. */
710 /* Window pointer is on, not used */
713 /* The position on that root window. */
716 /* x/y in dummy_window coordinates, not used. */
719 /* Modifier keys and pointer buttons, about which
721 (unsigned int *) &dummy
);
725 /* xmenu_show expects window coordinates, not root window
726 coordinates. Translate. */
727 *x
-= f
->left_pos
+ FRAME_OUTER_TO_INNER_DIFF_X (f
);
728 *y
-= f
->top_pos
+ FRAME_OUTER_TO_INNER_DIFF_Y (f
);
731 #endif /* HAVE_X_WINDOWS */
733 DEFUN ("x-popup-menu", Fx_popup_menu
, Sx_popup_menu
, 2, 2, 0,
734 doc
: /* Pop up a deck-of-cards menu and return user's selection.
735 POSITION is a position specification. This is either a mouse button event
736 or a list ((XOFFSET YOFFSET) WINDOW)
737 where XOFFSET and YOFFSET are positions in pixels from the top left
738 corner of WINDOW's frame. (WINDOW may be a frame object instead of a window.)
739 This controls the position of the top left of the menu as a whole.
740 If POSITION is t, it means to use the current mouse position.
742 MENU is a specifier for a menu. For the simplest case, MENU is a keymap.
743 The menu items come from key bindings that have a menu string as well as
744 a definition; actually, the "definition" in such a key binding looks like
745 \(STRING . REAL-DEFINITION). To give the menu a title, put a string into
746 the keymap as a top-level element.
748 If REAL-DEFINITION is nil, that puts a nonselectable string in the menu.
749 Otherwise, REAL-DEFINITION should be a valid key binding definition.
751 You can also use a list of keymaps as MENU.
752 Then each keymap makes a separate pane.
753 When MENU is a keymap or a list of keymaps, the return value
756 Alternatively, you can specify a menu of multiple panes
757 with a list of the form (TITLE PANE1 PANE2...),
758 where each pane is a list of form (TITLE ITEM1 ITEM2...).
759 Each ITEM is normally a cons cell (STRING . VALUE);
760 but a string can appear as an item--that makes a nonselectable line
762 With this form of menu, the return value is VALUE from the chosen item.
764 If POSITION is nil, don't display the menu at all, just precalculate the
765 cached information about equivalent key sequences. */)
767 Lisp_Object position
, menu
;
769 Lisp_Object keymap
, tem
;
770 int xpos
= 0, ypos
= 0;
773 Lisp_Object selection
;
775 Lisp_Object x
, y
, window
;
778 int specpdl_count
= SPECPDL_INDEX ();
782 if (! NILP (position
))
784 int get_current_pos_p
= 0;
787 /* Decode the first argument: find the window and the coordinates. */
788 if (EQ (position
, Qt
)
789 || (CONSP (position
) && (EQ (XCAR (position
), Qmenu_bar
)
790 || EQ (XCAR (position
), Qtool_bar
))))
792 get_current_pos_p
= 1;
796 tem
= Fcar (position
);
799 window
= Fcar (Fcdr (position
));
801 y
= Fcar (XCDR (tem
));
806 tem
= Fcar (Fcdr (position
)); /* EVENT_START (position) */
807 window
= Fcar (tem
); /* POSN_WINDOW (tem) */
808 tem
= Fcar (Fcdr (Fcdr (tem
))); /* POSN_WINDOW_POSN (tem) */
813 /* If a click happens in an external tool bar or a detached
814 tool bar, x and y is NIL. In that case, use the current
815 mouse position. This happens for the help button in the
816 tool bar. Ideally popup-menu should pass NIL to
817 this function, but it doesn't. */
818 if (NILP (x
) && NILP (y
))
819 get_current_pos_p
= 1;
822 if (get_current_pos_p
)
824 /* Use the mouse's current position. */
825 FRAME_PTR new_f
= SELECTED_FRAME ();
826 #ifdef HAVE_X_WINDOWS
827 /* Can't use mouse_position_hook for X since it returns
828 coordinates relative to the window the mouse is in,
829 we need coordinates relative to the edit widget always. */
834 mouse_position_for_popup (new_f
, &cur_x
, &cur_y
);
835 /* cur_x/y may be negative, so use make_number. */
836 x
= make_number (cur_x
);
837 y
= make_number (cur_y
);
840 #else /* not HAVE_X_WINDOWS */
841 Lisp_Object bar_window
;
842 enum scroll_bar_part part
;
845 if (mouse_position_hook
)
846 (*mouse_position_hook
) (&new_f
, 1, &bar_window
,
847 &part
, &x
, &y
, &time
);
848 #endif /* not HAVE_X_WINDOWS */
851 XSETFRAME (window
, new_f
);
854 window
= selected_window
;
863 /* Decode where to put the menu. */
871 else if (WINDOWP (window
))
873 CHECK_LIVE_WINDOW (window
);
874 f
= XFRAME (WINDOW_FRAME (XWINDOW (window
)));
876 xpos
= WINDOW_LEFT_EDGE_X (XWINDOW (window
));
877 ypos
= WINDOW_TOP_EDGE_Y (XWINDOW (window
));
880 /* ??? Not really clean; should be CHECK_WINDOW_OR_FRAME,
881 but I don't want to make one now. */
882 CHECK_WINDOW (window
);
887 Vmenu_updating_frame
= Qnil
;
888 #endif /* HAVE_MENUS */
890 record_unwind_protect (unuse_menu_items
, Qnil
);
894 /* Decode the menu items from what was specified. */
896 keymap
= get_keymap (menu
, 0, 0);
899 /* We were given a keymap. Extract menu info from the keymap. */
902 /* Extract the detailed info to make one pane. */
903 keymap_panes (&menu
, 1, NILP (position
));
905 /* Search for a string appearing directly as an element of the keymap.
906 That string is the title of the menu. */
907 prompt
= Fkeymap_prompt (keymap
);
908 if (NILP (title
) && !NILP (prompt
))
911 /* Make that be the pane title of the first pane. */
912 if (!NILP (prompt
) && menu_items_n_panes
>= 0)
913 XVECTOR (menu_items
)->contents
[MENU_ITEMS_PANE_NAME
] = prompt
;
917 else if (CONSP (menu
) && KEYMAPP (XCAR (menu
)))
919 /* We were given a list of keymaps. */
920 int nmaps
= XFASTINT (Flength (menu
));
922 = (Lisp_Object
*) alloca (nmaps
* sizeof (Lisp_Object
));
927 /* The first keymap that has a prompt string
928 supplies the menu title. */
929 for (tem
= menu
, i
= 0; CONSP (tem
); tem
= XCDR (tem
))
933 maps
[i
++] = keymap
= get_keymap (XCAR (tem
), 1, 0);
935 prompt
= Fkeymap_prompt (keymap
);
936 if (NILP (title
) && !NILP (prompt
))
940 /* Extract the detailed info to make one pane. */
941 keymap_panes (maps
, nmaps
, NILP (position
));
943 /* Make the title be the pane title of the first pane. */
944 if (!NILP (title
) && menu_items_n_panes
>= 0)
945 XVECTOR (menu_items
)->contents
[MENU_ITEMS_PANE_NAME
] = title
;
951 /* We were given an old-fashioned menu. */
953 CHECK_STRING (title
);
955 list_of_panes (Fcdr (menu
));
960 unbind_to (specpdl_count
, Qnil
);
964 discard_menu_items ();
970 /* Display them in a menu. */
973 selection
= xmenu_show (f
, xpos
, ypos
, for_click
,
974 keymaps
, title
, &error_name
);
977 discard_menu_items ();
980 #endif /* HAVE_MENUS */
982 if (error_name
) error (error_name
);
988 DEFUN ("x-popup-dialog", Fx_popup_dialog
, Sx_popup_dialog
, 2, 2, 0,
989 doc
: /* Pop up a dialog box and return user's selection.
990 POSITION specifies which frame to use.
991 This is normally a mouse button event or a window or frame.
992 If POSITION is t, it means to use the frame the mouse is on.
993 The dialog box appears in the middle of the specified frame.
995 CONTENTS specifies the alternatives to display in the dialog box.
996 It is a list of the form (TITLE ITEM1 ITEM2...).
997 Each ITEM is a cons cell (STRING . VALUE).
998 The return value is VALUE from the chosen item.
1000 An ITEM may also be just a string--that makes a nonselectable item.
1001 An ITEM may also be nil--that means to put all preceding items
1002 on the left of the dialog box and all following items on the right.
1003 \(By default, approximately half appear on each side.) */)
1004 (position
, contents
)
1005 Lisp_Object position
, contents
;
1012 /* Decode the first argument: find the window or frame to use. */
1013 if (EQ (position
, Qt
)
1014 || (CONSP (position
) && (EQ (XCAR (position
), Qmenu_bar
)
1015 || EQ (XCAR (position
), Qtool_bar
))))
1017 #if 0 /* Using the frame the mouse is on may not be right. */
1018 /* Use the mouse's current position. */
1019 FRAME_PTR new_f
= SELECTED_FRAME ();
1020 Lisp_Object bar_window
;
1021 enum scroll_bar_part part
;
1025 (*mouse_position_hook
) (&new_f
, 1, &bar_window
, &part
, &x
, &y
, &time
);
1028 XSETFRAME (window
, new_f
);
1030 window
= selected_window
;
1032 window
= selected_window
;
1034 else if (CONSP (position
))
1037 tem
= Fcar (position
);
1039 window
= Fcar (Fcdr (position
));
1042 tem
= Fcar (Fcdr (position
)); /* EVENT_START (position) */
1043 window
= Fcar (tem
); /* POSN_WINDOW (tem) */
1046 else if (WINDOWP (position
) || FRAMEP (position
))
1051 /* Decode where to put the menu. */
1053 if (FRAMEP (window
))
1054 f
= XFRAME (window
);
1055 else if (WINDOWP (window
))
1057 CHECK_LIVE_WINDOW (window
);
1058 f
= XFRAME (WINDOW_FRAME (XWINDOW (window
)));
1061 /* ??? Not really clean; should be CHECK_WINDOW_OR_FRAME,
1062 but I don't want to make one now. */
1063 CHECK_WINDOW (window
);
1065 #if ! defined (USE_X_TOOLKIT) && ! defined (USE_GTK)
1066 /* Display a menu with these alternatives
1067 in the middle of frame F. */
1069 Lisp_Object x
, y
, frame
, newpos
;
1070 XSETFRAME (frame
, f
);
1071 XSETINT (x
, x_pixel_width (f
) / 2);
1072 XSETINT (y
, x_pixel_height (f
) / 2);
1073 newpos
= Fcons (Fcons (x
, Fcons (y
, Qnil
)), Fcons (frame
, Qnil
));
1075 return Fx_popup_menu (newpos
,
1076 Fcons (Fcar (contents
), Fcons (contents
, Qnil
)));
1082 Lisp_Object selection
;
1083 int specpdl_count
= SPECPDL_INDEX ();
1085 /* Decode the dialog items from what was specified. */
1086 title
= Fcar (contents
);
1087 CHECK_STRING (title
);
1088 record_unwind_protect (unuse_menu_items
, Qnil
);
1090 if (NILP (Fcar (Fcdr (contents
))))
1091 /* No buttons specified, add an "Ok" button so users can pop down
1092 the dialog. Also, the lesstif/motif version crashes if there are
1094 contents
= Fcons (title
, Fcons (Fcons (build_string ("Ok"), Qt
), Qnil
));
1096 list_of_panes (Fcons (contents
, Qnil
));
1098 /* Display them in a dialog box. */
1100 selection
= xdialog_show (f
, 0, title
, &error_name
);
1103 unbind_to (specpdl_count
, Qnil
);
1104 discard_menu_items ();
1106 if (error_name
) error (error_name
);
1115 /* Set menu_items_inuse so no other popup menu or dialog is created. */
1118 x_menu_set_in_use (in_use
)
1121 menu_items_inuse
= in_use
? Qt
: Qnil
;
1122 popup_activated_flag
= in_use
;
1125 /* Wait for an X event to arrive or for a timer to expire. */
1128 x_menu_wait_for_event (void *data
)
1130 extern EMACS_TIME timer_check
P_ ((int));
1132 /* Another way to do this is to register a timer callback, that can be
1133 done in GTK and Xt. But we have to do it like this when using only X
1134 anyway, and with callbacks we would have three variants for timer handling
1135 instead of the small ifdefs below. */
1138 #ifdef USE_X_TOOLKIT
1139 ! XtAppPending (Xt_app_con
)
1140 #elif defined USE_GTK
1141 ! gtk_events_pending ()
1143 ! XPending ((Display
*) data
)
1147 EMACS_TIME next_time
= timer_check (1);
1148 long secs
= EMACS_SECS (next_time
);
1149 long usecs
= EMACS_USECS (next_time
);
1150 SELECT_TYPE read_fds
;
1151 struct x_display_info
*dpyinfo
;
1154 FD_ZERO (&read_fds
);
1155 for (dpyinfo
= x_display_list
; dpyinfo
; dpyinfo
= dpyinfo
->next
)
1157 int fd
= ConnectionNumber (dpyinfo
->display
);
1158 FD_SET (fd
, &read_fds
);
1162 if (secs
< 0 || (secs
== 0 && usecs
== 0))
1164 /* Sometimes timer_check returns -1 (no timers) even if there are
1165 timers. So do a timeout anyway. */
1166 EMACS_SET_SECS (next_time
, 1);
1167 EMACS_SET_USECS (next_time
, 0);
1170 select (n
+ 1, &read_fds
, (SELECT_TYPE
*)0, (SELECT_TYPE
*)0, &next_time
);
1173 #endif /* ! MSDOS */
1176 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
1178 #ifdef USE_X_TOOLKIT
1180 /* Loop in Xt until the menu pulldown or dialog popup has been
1181 popped down (deactivated). This is used for x-popup-menu
1182 and x-popup-dialog; it is not used for the menu bar.
1184 NOTE: All calls to popup_get_selection should be protected
1185 with BLOCK_INPUT, UNBLOCK_INPUT wrappers. */
1188 popup_get_selection (initial_event
, dpyinfo
, id
, do_timers
)
1189 XEvent
*initial_event
;
1190 struct x_display_info
*dpyinfo
;
1196 while (popup_activated_flag
)
1200 event
= *initial_event
;
1205 if (do_timers
) x_menu_wait_for_event (0);
1206 XtAppNextEvent (Xt_app_con
, &event
);
1209 /* Make sure we don't consider buttons grabbed after menu goes.
1210 And make sure to deactivate for any ButtonRelease,
1211 even if XtDispatchEvent doesn't do that. */
1212 if (event
.type
== ButtonRelease
1213 && dpyinfo
->display
== event
.xbutton
.display
)
1215 dpyinfo
->grabbed
&= ~(1 << event
.xbutton
.button
);
1216 #ifdef USE_MOTIF /* Pretending that the event came from a
1217 Btn1Down seems the only way to convince Motif to
1218 activate its callbacks; setting the XmNmenuPost
1219 isn't working. --marcus@sysc.pdx.edu. */
1220 event
.xbutton
.button
= 1;
1221 /* Motif only pops down menus when no Ctrl, Alt or Mod
1222 key is pressed and the button is released. So reset key state
1223 so Motif thinks this is the case. */
1224 event
.xbutton
.state
= 0;
1227 /* Pop down on C-g and Escape. */
1228 else if (event
.type
== KeyPress
1229 && dpyinfo
->display
== event
.xbutton
.display
)
1231 KeySym keysym
= XLookupKeysym (&event
.xkey
, 0);
1233 if ((keysym
== XK_g
&& (event
.xkey
.state
& ControlMask
) != 0)
1234 || keysym
== XK_Escape
) /* Any escape, ignore modifiers. */
1235 popup_activated_flag
= 0;
1238 x_dispatch_event (&event
, event
.xany
.display
);
1242 #endif /* USE_X_TOOLKIT */
1245 /* Loop util popup_activated_flag is set to zero in a callback.
1246 Used for popup menus and dialogs. */
1249 popup_widget_loop (do_timers
, widget
)
1253 ++popup_activated_flag
;
1255 /* Process events in the Gtk event loop until done. */
1256 while (popup_activated_flag
)
1258 if (do_timers
) x_menu_wait_for_event (0);
1259 gtk_main_iteration ();
1264 /* Activate the menu bar of frame F.
1265 This is called from keyboard.c when it gets the
1266 MENU_BAR_ACTIVATE_EVENT out of the Emacs event queue.
1268 To activate the menu bar, we use the X button-press event
1269 that was saved in saved_menu_event.
1270 That makes the toolkit do its thing.
1272 But first we recompute the menu bar contents (the whole tree).
1274 The reason for saving the button event until here, instead of
1275 passing it to the toolkit right away, is that we can safely
1276 execute Lisp code. */
1279 x_activate_menubar (f
)
1282 if (!f
->output_data
.x
->saved_menu_event
->type
)
1286 if (! xg_win_to_widget (FRAME_X_DISPLAY (f
),
1287 f
->output_data
.x
->saved_menu_event
->xany
.window
))
1291 set_frame_menubar (f
, 0, 1);
1294 XPutBackEvent (f
->output_data
.x
->display_info
->display
,
1295 f
->output_data
.x
->saved_menu_event
);
1296 popup_activated_flag
= 1;
1298 XtDispatchEvent (f
->output_data
.x
->saved_menu_event
);
1302 if (f
->output_data
.x
->saved_menu_event
->type
== ButtonRelease
)
1303 pending_menu_activation
= 1;
1306 /* Ignore this if we get it a second time. */
1307 f
->output_data
.x
->saved_menu_event
->type
= 0;
1310 /* Detect if a dialog or menu has been posted. */
1315 return popup_activated_flag
;
1318 /* This callback is invoked when the user selects a menubar cascade
1319 pushbutton, but before the pulldown menu is posted. */
1323 popup_activate_callback (widget
, id
, client_data
)
1326 XtPointer client_data
;
1328 popup_activated_flag
= 1;
1332 /* This callback is invoked when a dialog or menu is finished being
1333 used and has been unposted. */
1337 popup_deactivate_callback (widget
, client_data
)
1339 gpointer client_data
;
1341 popup_activated_flag
= 0;
1345 popup_deactivate_callback (widget
, id
, client_data
)
1348 XtPointer client_data
;
1350 popup_activated_flag
= 0;
1355 /* Function that finds the frame for WIDGET and shows the HELP text
1357 F is the frame if known, or NULL if not known. */
1359 show_help_event (f
, widget
, help
)
1361 xt_or_gtk_widget widget
;
1368 XSETFRAME (frame
, f
);
1369 kbd_buffer_store_help_event (frame
, help
);
1373 #if 0 /* This code doesn't do anything useful. ++kfs */
1374 /* WIDGET is the popup menu. It's parent is the frame's
1375 widget. See which frame that is. */
1376 xt_or_gtk_widget frame_widget
= XtParent (widget
);
1379 for (tail
= Vframe_list
; GC_CONSP (tail
); tail
= XCDR (tail
))
1381 frame
= XCAR (tail
);
1382 if (GC_FRAMEP (frame
)
1383 && (f
= XFRAME (frame
),
1384 FRAME_X_P (f
) && f
->output_data
.x
->widget
== frame_widget
))
1388 show_help_echo (help
, Qnil
, Qnil
, Qnil
, 1);
1392 /* Callback called when menu items are highlighted/unhighlighted
1393 while moving the mouse over them. WIDGET is the menu bar or menu
1394 popup widget. ID is its LWLIB_ID. CALL_DATA contains a pointer to
1395 the data structure for the menu item, or null in case of
1400 menu_highlight_callback (widget
, call_data
)
1404 xg_menu_item_cb_data
*cb_data
;
1407 cb_data
= (xg_menu_item_cb_data
*) g_object_get_data (G_OBJECT (widget
),
1409 if (! cb_data
) return;
1411 help
= call_data
? cb_data
->help
: Qnil
;
1413 /* If popup_activated_flag is greater than 1 we are in a popup menu.
1414 Don't show help for them, they won't appear before the
1415 popup is popped down. */
1416 if (popup_activated_flag
<= 1)
1417 show_help_event (cb_data
->cl_data
->f
, widget
, help
);
1421 menu_highlight_callback (widget
, id
, call_data
)
1429 widget_value
*wv
= (widget_value
*) call_data
;
1431 help
= wv
? wv
->help
: Qnil
;
1433 /* Determine the frame for the help event. */
1434 f
= menubar_id_to_frame (id
);
1436 show_help_event (f
, widget
, help
);
1440 /* Find the menu selection and store it in the keyboard buffer.
1441 F is the frame the menu is on.
1442 MENU_BAR_ITEMS_USED is the length of VECTOR.
1443 VECTOR is an array of menu events for the whole menu. */
1446 find_and_call_menu_selection (f
, menu_bar_items_used
, vector
, client_data
)
1448 int menu_bar_items_used
;
1452 Lisp_Object prefix
, entry
;
1453 Lisp_Object
*subprefix_stack
;
1454 int submenu_depth
= 0;
1458 subprefix_stack
= (Lisp_Object
*) alloca (menu_bar_items_used
* sizeof (Lisp_Object
));
1462 while (i
< menu_bar_items_used
)
1464 if (EQ (XVECTOR (vector
)->contents
[i
], Qnil
))
1466 subprefix_stack
[submenu_depth
++] = prefix
;
1470 else if (EQ (XVECTOR (vector
)->contents
[i
], Qlambda
))
1472 prefix
= subprefix_stack
[--submenu_depth
];
1475 else if (EQ (XVECTOR (vector
)->contents
[i
], Qt
))
1477 prefix
= XVECTOR (vector
)->contents
[i
+ MENU_ITEMS_PANE_PREFIX
];
1478 i
+= MENU_ITEMS_PANE_LENGTH
;
1482 entry
= XVECTOR (vector
)->contents
[i
+ MENU_ITEMS_ITEM_VALUE
];
1483 /* The EMACS_INT cast avoids a warning. There's no problem
1484 as long as pointers have enough bits to hold small integers. */
1485 if ((int) (EMACS_INT
) client_data
== i
)
1488 struct input_event buf
;
1492 XSETFRAME (frame
, f
);
1493 buf
.kind
= MENU_BAR_EVENT
;
1494 buf
.frame_or_window
= frame
;
1496 kbd_buffer_store_event (&buf
);
1498 for (j
= 0; j
< submenu_depth
; j
++)
1499 if (!NILP (subprefix_stack
[j
]))
1501 buf
.kind
= MENU_BAR_EVENT
;
1502 buf
.frame_or_window
= frame
;
1503 buf
.arg
= subprefix_stack
[j
];
1504 kbd_buffer_store_event (&buf
);
1509 buf
.kind
= MENU_BAR_EVENT
;
1510 buf
.frame_or_window
= frame
;
1512 kbd_buffer_store_event (&buf
);
1515 buf
.kind
= MENU_BAR_EVENT
;
1516 buf
.frame_or_window
= frame
;
1518 kbd_buffer_store_event (&buf
);
1522 i
+= MENU_ITEMS_ITEM_LENGTH
;
1529 /* Gtk calls callbacks just because we tell it what item should be
1530 selected in a radio group. If this variable is set to a non-zero
1531 value, we are creating menus and don't want callbacks right now.
1533 static int xg_crazy_callback_abort
;
1535 /* This callback is called from the menu bar pulldown menu
1536 when the user makes a selection.
1537 Figure out what the user chose
1538 and put the appropriate events into the keyboard buffer. */
1540 menubar_selection_callback (widget
, client_data
)
1542 gpointer client_data
;
1544 xg_menu_item_cb_data
*cb_data
= (xg_menu_item_cb_data
*) client_data
;
1546 if (xg_crazy_callback_abort
)
1549 if (! cb_data
|| ! cb_data
->cl_data
|| ! cb_data
->cl_data
->f
)
1552 /* When a menu is popped down, X generates a focus event (i.e. focus
1553 goes back to the frame below the menu). Since GTK buffers events,
1554 we force it out here before the menu selection event. Otherwise
1555 sit-for will exit at once if the focus event follows the menu selection
1559 while (gtk_events_pending ())
1560 gtk_main_iteration ();
1563 find_and_call_menu_selection (cb_data
->cl_data
->f
,
1564 cb_data
->cl_data
->menu_bar_items_used
,
1565 cb_data
->cl_data
->menu_bar_vector
,
1566 cb_data
->call_data
);
1569 #else /* not USE_GTK */
1571 /* This callback is called from the menu bar pulldown menu
1572 when the user makes a selection.
1573 Figure out what the user chose
1574 and put the appropriate events into the keyboard buffer. */
1576 menubar_selection_callback (widget
, id
, client_data
)
1579 XtPointer client_data
;
1583 f
= menubar_id_to_frame (id
);
1586 find_and_call_menu_selection (f
, f
->menu_bar_items_used
,
1587 f
->menu_bar_vector
, client_data
);
1589 #endif /* not USE_GTK */
1591 /* Allocate a widget_value, blocking input. */
1594 xmalloc_widget_value ()
1596 widget_value
*value
;
1599 value
= malloc_widget_value ();
1605 /* This recursively calls free_widget_value on the tree of widgets.
1606 It must free all data that was malloc'ed for these widget_values.
1607 In Emacs, many slots are pointers into the data of Lisp_Strings, and
1608 must be left alone. */
1611 free_menubar_widget_value_tree (wv
)
1616 wv
->name
= wv
->value
= wv
->key
= (char *) 0xDEADBEEF;
1618 if (wv
->contents
&& (wv
->contents
!= (widget_value
*)1))
1620 free_menubar_widget_value_tree (wv
->contents
);
1621 wv
->contents
= (widget_value
*) 0xDEADBEEF;
1625 free_menubar_widget_value_tree (wv
->next
);
1626 wv
->next
= (widget_value
*) 0xDEADBEEF;
1629 free_widget_value (wv
);
1633 /* Set up data in menu_items for a menu bar item
1634 whose event type is ITEM_KEY (with string ITEM_NAME)
1635 and whose contents come from the list of keymaps MAPS. */
1638 parse_single_submenu (item_key
, item_name
, maps
)
1639 Lisp_Object item_key
, item_name
, maps
;
1643 Lisp_Object
*mapvec
;
1645 int top_level_items
= 0;
1647 length
= Flength (maps
);
1648 len
= XINT (length
);
1650 /* Convert the list MAPS into a vector MAPVEC. */
1651 mapvec
= (Lisp_Object
*) alloca (len
* sizeof (Lisp_Object
));
1652 for (i
= 0; i
< len
; i
++)
1654 mapvec
[i
] = Fcar (maps
);
1658 /* Loop over the given keymaps, making a pane for each map.
1659 But don't make a pane that is empty--ignore that map instead. */
1660 for (i
= 0; i
< len
; i
++)
1662 if (!KEYMAPP (mapvec
[i
]))
1664 /* Here we have a command at top level in the menu bar
1665 as opposed to a submenu. */
1666 top_level_items
= 1;
1667 push_menu_pane (Qnil
, Qnil
);
1668 push_menu_item (item_name
, Qt
, item_key
, mapvec
[i
],
1669 Qnil
, Qnil
, Qnil
, Qnil
);
1674 prompt
= Fkeymap_prompt (mapvec
[i
]);
1675 single_keymap_panes (mapvec
[i
],
1676 !NILP (prompt
) ? prompt
: item_name
,
1681 return top_level_items
;
1684 /* Create a tree of widget_value objects
1685 representing the panes and items
1686 in menu_items starting at index START, up to index END. */
1688 static widget_value
*
1689 digest_single_submenu (start
, end
, top_level_items
)
1690 int start
, end
, top_level_items
;
1692 widget_value
*wv
, *prev_wv
, *save_wv
, *first_wv
;
1694 int submenu_depth
= 0;
1695 widget_value
**submenu_stack
;
1698 = (widget_value
**) alloca (menu_items_used
* sizeof (widget_value
*));
1699 wv
= xmalloc_widget_value ();
1703 wv
->button_type
= BUTTON_TYPE_NONE
;
1709 /* Loop over all panes and items made by the preceding call
1710 to parse_single_submenu and construct a tree of widget_value objects.
1711 Ignore the panes and items used by previous calls to
1712 digest_single_submenu, even though those are also in menu_items. */
1716 if (EQ (XVECTOR (menu_items
)->contents
[i
], Qnil
))
1718 submenu_stack
[submenu_depth
++] = save_wv
;
1723 else if (EQ (XVECTOR (menu_items
)->contents
[i
], Qlambda
))
1726 save_wv
= submenu_stack
[--submenu_depth
];
1729 else if (EQ (XVECTOR (menu_items
)->contents
[i
], Qt
)
1730 && submenu_depth
!= 0)
1731 i
+= MENU_ITEMS_PANE_LENGTH
;
1732 /* Ignore a nil in the item list.
1733 It's meaningful only for dialog boxes. */
1734 else if (EQ (XVECTOR (menu_items
)->contents
[i
], Qquote
))
1736 else if (EQ (XVECTOR (menu_items
)->contents
[i
], Qt
))
1738 /* Create a new pane. */
1739 Lisp_Object pane_name
, prefix
;
1742 pane_name
= XVECTOR (menu_items
)->contents
[i
+ MENU_ITEMS_PANE_NAME
];
1743 prefix
= XVECTOR (menu_items
)->contents
[i
+ MENU_ITEMS_PANE_PREFIX
];
1745 #ifndef HAVE_MULTILINGUAL_MENU
1746 if (STRINGP (pane_name
) && STRING_MULTIBYTE (pane_name
))
1748 pane_name
= ENCODE_MENU_STRING (pane_name
);
1749 AREF (menu_items
, i
+ MENU_ITEMS_PANE_NAME
) = pane_name
;
1752 pane_string
= (NILP (pane_name
)
1753 ? "" : (char *) SDATA (pane_name
));
1754 /* If there is just one top-level pane, put all its items directly
1755 under the top-level menu. */
1756 if (menu_items_n_panes
== 1)
1759 /* If the pane has a meaningful name,
1760 make the pane a top-level menu item
1761 with its items as a submenu beneath it. */
1762 if (strcmp (pane_string
, ""))
1764 wv
= xmalloc_widget_value ();
1768 first_wv
->contents
= wv
;
1769 wv
->lname
= pane_name
;
1770 /* Set value to 1 so update_submenu_strings can handle '@' */
1771 wv
->value
= (char *)1;
1773 wv
->button_type
= BUTTON_TYPE_NONE
;
1778 i
+= MENU_ITEMS_PANE_LENGTH
;
1782 /* Create a new item within current pane. */
1783 Lisp_Object item_name
, enable
, descrip
, def
, type
, selected
;
1786 item_name
= AREF (menu_items
, i
+ MENU_ITEMS_ITEM_NAME
);
1787 enable
= AREF (menu_items
, i
+ MENU_ITEMS_ITEM_ENABLE
);
1788 descrip
= AREF (menu_items
, i
+ MENU_ITEMS_ITEM_EQUIV_KEY
);
1789 def
= AREF (menu_items
, i
+ MENU_ITEMS_ITEM_DEFINITION
);
1790 type
= AREF (menu_items
, i
+ MENU_ITEMS_ITEM_TYPE
);
1791 selected
= AREF (menu_items
, i
+ MENU_ITEMS_ITEM_SELECTED
);
1792 help
= AREF (menu_items
, i
+ MENU_ITEMS_ITEM_HELP
);
1794 #ifndef HAVE_MULTILINGUAL_MENU
1795 if (STRING_MULTIBYTE (item_name
))
1797 item_name
= ENCODE_MENU_STRING (item_name
);
1798 AREF (menu_items
, i
+ MENU_ITEMS_ITEM_NAME
) = item_name
;
1801 if (STRINGP (descrip
) && STRING_MULTIBYTE (descrip
))
1803 descrip
= ENCODE_MENU_STRING (descrip
);
1804 AREF (menu_items
, i
+ MENU_ITEMS_ITEM_EQUIV_KEY
) = descrip
;
1806 #endif /* not HAVE_MULTILINGUAL_MENU */
1808 wv
= xmalloc_widget_value ();
1812 save_wv
->contents
= wv
;
1814 wv
->lname
= item_name
;
1815 if (!NILP (descrip
))
1818 /* The EMACS_INT cast avoids a warning. There's no problem
1819 as long as pointers have enough bits to hold small integers. */
1820 wv
->call_data
= (!NILP (def
) ? (void *) (EMACS_INT
) i
: 0);
1821 wv
->enabled
= !NILP (enable
);
1824 wv
->button_type
= BUTTON_TYPE_NONE
;
1825 else if (EQ (type
, QCradio
))
1826 wv
->button_type
= BUTTON_TYPE_RADIO
;
1827 else if (EQ (type
, QCtoggle
))
1828 wv
->button_type
= BUTTON_TYPE_TOGGLE
;
1832 wv
->selected
= !NILP (selected
);
1833 if (! STRINGP (help
))
1840 i
+= MENU_ITEMS_ITEM_LENGTH
;
1844 /* If we have just one "menu item"
1845 that was originally a button, return it by itself. */
1846 if (top_level_items
&& first_wv
->contents
&& first_wv
->contents
->next
== 0)
1848 wv
= first_wv
->contents
;
1849 free_widget_value (first_wv
);
1856 /* Walk through the widget_value tree starting at FIRST_WV and update
1857 the char * pointers from the corresponding lisp values.
1858 We do this after building the whole tree, since GC may happen while the
1859 tree is constructed, and small strings are relocated. So we must wait
1860 until no GC can happen before storing pointers into lisp values. */
1862 update_submenu_strings (first_wv
)
1863 widget_value
*first_wv
;
1867 for (wv
= first_wv
; wv
; wv
= wv
->next
)
1869 if (STRINGP (wv
->lname
))
1871 wv
->name
= SDATA (wv
->lname
);
1873 /* Ignore the @ that means "separate pane".
1874 This is a kludge, but this isn't worth more time. */
1875 if (wv
->value
== (char *)1)
1877 if (wv
->name
[0] == '@')
1883 if (STRINGP (wv
->lkey
))
1884 wv
->key
= SDATA (wv
->lkey
);
1887 update_submenu_strings (wv
->contents
);
1892 /* Recompute all the widgets of frame F, when the menu bar has been
1893 changed. Value is non-zero if widgets were updated. */
1896 update_frame_menubar (f
)
1900 return xg_update_frame_menubar (f
);
1902 struct x_output
*x
= f
->output_data
.x
;
1905 if (!x
->menubar_widget
|| XtIsManaged (x
->menubar_widget
))
1909 /* Save the size of the frame because the pane widget doesn't accept
1910 to resize itself. So force it. */
1911 columns
= FRAME_COLS (f
);
1912 rows
= FRAME_LINES (f
);
1914 /* Do the voodoo which means "I'm changing lots of things, don't try
1915 to refigure sizes until I'm done." */
1916 lw_refigure_widget (x
->column_widget
, False
);
1918 /* The order in which children are managed is the top to bottom
1919 order in which they are displayed in the paned window. First,
1920 remove the text-area widget. */
1921 XtUnmanageChild (x
->edit_widget
);
1923 /* Remove the menubar that is there now, and put up the menubar that
1925 XtManageChild (x
->menubar_widget
);
1926 XtMapWidget (x
->menubar_widget
);
1927 XtVaSetValues (x
->menubar_widget
, XtNmappedWhenManaged
, 1, NULL
);
1929 /* Re-manage the text-area widget, and then thrash the sizes. */
1930 XtManageChild (x
->edit_widget
);
1931 lw_refigure_widget (x
->column_widget
, True
);
1933 /* Force the pane widget to resize itself with the right values. */
1934 EmacsFrameSetCharSize (x
->edit_widget
, columns
, rows
);
1940 /* Set the contents of the menubar widgets of frame F.
1941 The argument FIRST_TIME is currently ignored;
1942 it is set the first time this is called, from initialize_frame_menubar. */
1945 set_frame_menubar (f
, first_time
, deep_p
)
1950 xt_or_gtk_widget menubar_widget
= f
->output_data
.x
->menubar_widget
;
1951 #ifdef USE_X_TOOLKIT
1955 widget_value
*wv
, *first_wv
, *prev_wv
= 0;
1957 int *submenu_start
, *submenu_end
;
1958 int *submenu_top_level_items
, *submenu_n_panes
;
1961 XSETFRAME (Vmenu_updating_frame
, f
);
1963 #ifdef USE_X_TOOLKIT
1964 if (f
->output_data
.x
->id
== 0)
1965 f
->output_data
.x
->id
= next_menubar_widget_id
++;
1966 id
= f
->output_data
.x
->id
;
1969 if (! menubar_widget
)
1971 else if (pending_menu_activation
&& !deep_p
)
1973 /* Make the first call for any given frame always go deep. */
1974 else if (!f
->output_data
.x
->saved_menu_event
&& !deep_p
)
1977 f
->output_data
.x
->saved_menu_event
= (XEvent
*)xmalloc (sizeof (XEvent
));
1978 f
->output_data
.x
->saved_menu_event
->type
= 0;
1982 /* If we have detached menus, we must update deep so detached menus
1983 also gets updated. */
1984 deep_p
= deep_p
|| xg_have_tear_offs ();
1989 /* Make a widget-value tree representing the entire menu trees. */
1991 struct buffer
*prev
= current_buffer
;
1993 int specpdl_count
= SPECPDL_INDEX ();
1994 int previous_menu_items_used
= f
->menu_bar_items_used
;
1995 Lisp_Object
*previous_items
1996 = (Lisp_Object
*) alloca (previous_menu_items_used
1997 * sizeof (Lisp_Object
));
1999 /* If we are making a new widget, its contents are empty,
2000 do always reinitialize them. */
2001 if (! menubar_widget
)
2002 previous_menu_items_used
= 0;
2004 buffer
= XWINDOW (FRAME_SELECTED_WINDOW (f
))->buffer
;
2005 specbind (Qinhibit_quit
, Qt
);
2006 /* Don't let the debugger step into this code
2007 because it is not reentrant. */
2008 specbind (Qdebug_on_next_call
, Qnil
);
2010 record_unwind_protect (Fset_match_data
, Fmatch_data (Qnil
, Qnil
));
2011 record_unwind_protect (unuse_menu_items
, Qnil
);
2012 if (NILP (Voverriding_local_map_menu_flag
))
2014 specbind (Qoverriding_terminal_local_map
, Qnil
);
2015 specbind (Qoverriding_local_map
, Qnil
);
2018 set_buffer_internal_1 (XBUFFER (buffer
));
2020 /* Run the Lucid hook. */
2021 safe_run_hooks (Qactivate_menubar_hook
);
2023 /* If it has changed current-menubar from previous value,
2024 really recompute the menubar from the value. */
2025 if (! NILP (Vlucid_menu_bar_dirty_flag
))
2026 call0 (Qrecompute_lucid_menubar
);
2027 safe_run_hooks (Qmenu_bar_update_hook
);
2028 FRAME_MENU_BAR_ITEMS (f
) = menu_bar_items (FRAME_MENU_BAR_ITEMS (f
));
2030 items
= FRAME_MENU_BAR_ITEMS (f
);
2032 /* Save the frame's previous menu bar contents data. */
2033 if (previous_menu_items_used
)
2034 bcopy (XVECTOR (f
->menu_bar_vector
)->contents
, previous_items
,
2035 previous_menu_items_used
* sizeof (Lisp_Object
));
2037 /* Fill in menu_items with the current menu bar contents.
2038 This can evaluate Lisp code. */
2039 menu_items
= f
->menu_bar_vector
;
2040 menu_items_allocated
= VECTORP (menu_items
) ? ASIZE (menu_items
) : 0;
2041 submenu_start
= (int *) alloca (XVECTOR (items
)->size
* sizeof (int *));
2042 submenu_end
= (int *) alloca (XVECTOR (items
)->size
* sizeof (int *));
2043 submenu_n_panes
= (int *) alloca (XVECTOR (items
)->size
* sizeof (int));
2044 submenu_top_level_items
2045 = (int *) alloca (XVECTOR (items
)->size
* sizeof (int *));
2047 for (i
= 0; i
< XVECTOR (items
)->size
; i
+= 4)
2049 Lisp_Object key
, string
, maps
;
2053 key
= XVECTOR (items
)->contents
[i
];
2054 string
= XVECTOR (items
)->contents
[i
+ 1];
2055 maps
= XVECTOR (items
)->contents
[i
+ 2];
2059 submenu_start
[i
] = menu_items_used
;
2061 menu_items_n_panes
= 0;
2062 submenu_top_level_items
[i
]
2063 = parse_single_submenu (key
, string
, maps
);
2064 submenu_n_panes
[i
] = menu_items_n_panes
;
2066 submenu_end
[i
] = menu_items_used
;
2069 finish_menu_items ();
2071 /* Convert menu_items into widget_value trees
2072 to display the menu. This cannot evaluate Lisp code. */
2074 wv
= xmalloc_widget_value ();
2075 wv
->name
= "menubar";
2078 wv
->button_type
= BUTTON_TYPE_NONE
;
2082 for (i
= 0; i
< last_i
; i
+= 4)
2084 menu_items_n_panes
= submenu_n_panes
[i
];
2085 wv
= digest_single_submenu (submenu_start
[i
], submenu_end
[i
],
2086 submenu_top_level_items
[i
]);
2090 first_wv
->contents
= wv
;
2091 /* Don't set wv->name here; GC during the loop might relocate it. */
2093 wv
->button_type
= BUTTON_TYPE_NONE
;
2097 set_buffer_internal_1 (prev
);
2098 unbind_to (specpdl_count
, Qnil
);
2100 /* If there has been no change in the Lisp-level contents
2101 of the menu bar, skip redisplaying it. Just exit. */
2103 for (i
= 0; i
< previous_menu_items_used
; i
++)
2104 if (menu_items_used
== i
2105 || (!EQ (previous_items
[i
], XVECTOR (menu_items
)->contents
[i
])))
2107 if (i
== menu_items_used
&& i
== previous_menu_items_used
&& i
!= 0)
2109 free_menubar_widget_value_tree (first_wv
);
2110 discard_menu_items ();
2115 /* Now GC cannot happen during the lifetime of the widget_value,
2116 so it's safe to store data from a Lisp_String. */
2117 wv
= first_wv
->contents
;
2118 for (i
= 0; i
< XVECTOR (items
)->size
; i
+= 4)
2121 string
= XVECTOR (items
)->contents
[i
+ 1];
2124 wv
->name
= (char *) SDATA (string
);
2125 update_submenu_strings (wv
->contents
);
2129 f
->menu_bar_vector
= menu_items
;
2130 f
->menu_bar_items_used
= menu_items_used
;
2131 discard_menu_items ();
2135 /* Make a widget-value tree containing
2136 just the top level menu bar strings. */
2138 wv
= xmalloc_widget_value ();
2139 wv
->name
= "menubar";
2142 wv
->button_type
= BUTTON_TYPE_NONE
;
2146 items
= FRAME_MENU_BAR_ITEMS (f
);
2147 for (i
= 0; i
< XVECTOR (items
)->size
; i
+= 4)
2151 string
= XVECTOR (items
)->contents
[i
+ 1];
2155 wv
= xmalloc_widget_value ();
2156 wv
->name
= (char *) SDATA (string
);
2159 wv
->button_type
= BUTTON_TYPE_NONE
;
2161 /* This prevents lwlib from assuming this
2162 menu item is really supposed to be empty. */
2163 /* The EMACS_INT cast avoids a warning.
2164 This value just has to be different from small integers. */
2165 wv
->call_data
= (void *) (EMACS_INT
) (-1);
2170 first_wv
->contents
= wv
;
2174 /* Forget what we thought we knew about what is in the
2175 detailed contents of the menu bar menus.
2176 Changing the top level always destroys the contents. */
2177 f
->menu_bar_items_used
= 0;
2180 /* Create or update the menu bar widget. */
2185 xg_crazy_callback_abort
= 1;
2188 /* The fourth arg is DEEP_P, which says to consider the entire
2189 menu trees we supply, rather than just the menu bar item names. */
2190 xg_modify_menubar_widgets (menubar_widget
,
2194 G_CALLBACK (menubar_selection_callback
),
2195 G_CALLBACK (popup_deactivate_callback
),
2196 G_CALLBACK (menu_highlight_callback
));
2200 GtkWidget
*wvbox
= f
->output_data
.x
->vbox_widget
;
2203 = xg_create_widget ("menubar", "menubar", f
, first_wv
,
2204 G_CALLBACK (menubar_selection_callback
),
2205 G_CALLBACK (popup_deactivate_callback
),
2206 G_CALLBACK (menu_highlight_callback
));
2208 f
->output_data
.x
->menubar_widget
= menubar_widget
;
2212 #else /* not USE_GTK */
2215 /* Disable resizing (done for Motif!) */
2216 lw_allow_resizing (f
->output_data
.x
->widget
, False
);
2218 /* The third arg is DEEP_P, which says to consider the entire
2219 menu trees we supply, rather than just the menu bar item names. */
2220 lw_modify_all_widgets (id
, first_wv
, deep_p
);
2222 /* Re-enable the edit widget to resize. */
2223 lw_allow_resizing (f
->output_data
.x
->widget
, True
);
2227 char menuOverride
[] = "Ctrl<KeyPress>g: MenuGadgetEscape()";
2228 XtTranslations override
= XtParseTranslationTable (menuOverride
);
2230 menubar_widget
= lw_create_widget ("menubar", "menubar", id
, first_wv
,
2231 f
->output_data
.x
->column_widget
,
2233 popup_activate_callback
,
2234 menubar_selection_callback
,
2235 popup_deactivate_callback
,
2236 menu_highlight_callback
);
2237 f
->output_data
.x
->menubar_widget
= menubar_widget
;
2239 /* Make menu pop down on C-g. */
2240 XtOverrideTranslations (menubar_widget
, override
);
2245 = (f
->output_data
.x
->menubar_widget
2246 ? (f
->output_data
.x
->menubar_widget
->core
.height
2247 + f
->output_data
.x
->menubar_widget
->core
.border_width
)
2250 #if 0 /* Experimentally, we now get the right results
2251 for -geometry -0-0 without this. 24 Aug 96, rms. */
2253 if (FRAME_EXTERNAL_MENU_BAR (f
))
2256 XtVaGetValues (f
->output_data
.x
->column_widget
,
2257 XtNinternalBorderWidth
, &ibw
, NULL
);
2258 menubar_size
+= ibw
;
2260 #endif /* USE_LUCID */
2263 f
->output_data
.x
->menubar_height
= menubar_size
;
2265 #endif /* not USE_GTK */
2267 free_menubar_widget_value_tree (first_wv
);
2268 update_frame_menubar (f
);
2271 xg_crazy_callback_abort
= 0;
2277 /* Called from Fx_create_frame to create the initial menubar of a frame
2278 before it is mapped, so that the window is mapped with the menubar already
2279 there instead of us tacking it on later and thrashing the window after it
2283 initialize_frame_menubar (f
)
2286 /* This function is called before the first chance to redisplay
2287 the frame. It has to be, so the frame will have the right size. */
2288 FRAME_MENU_BAR_ITEMS (f
) = menu_bar_items (FRAME_MENU_BAR_ITEMS (f
));
2289 set_frame_menubar (f
, 1, 1);
2293 /* Get rid of the menu bar of frame F, and free its storage.
2294 This is used when deleting a frame, and when turning off the menu bar.
2295 For GTK this function is in gtkutil.c. */
2299 free_frame_menubar (f
)
2302 Widget menubar_widget
;
2304 menubar_widget
= f
->output_data
.x
->menubar_widget
;
2306 f
->output_data
.x
->menubar_height
= 0;
2311 /* Removing the menu bar magically changes the shell widget's x
2312 and y position of (0, 0) which, when the menu bar is turned
2313 on again, leads to pull-down menuss appearing in strange
2314 positions near the upper-left corner of the display. This
2315 happens only with some window managers like twm and ctwm,
2316 but not with other like Motif's mwm or kwm, because the
2317 latter generate ConfigureNotify events when the menu bar
2318 is switched off, which fixes the shell position. */
2319 Position x0
, y0
, x1
, y1
;
2325 if (f
->output_data
.x
->widget
)
2326 XtVaGetValues (f
->output_data
.x
->widget
, XtNx
, &x0
, XtNy
, &y0
, NULL
);
2329 lw_destroy_all_widgets ((LWLIB_ID
) f
->output_data
.x
->id
);
2330 f
->output_data
.x
->menubar_widget
= NULL
;
2333 if (f
->output_data
.x
->widget
)
2335 XtVaGetValues (f
->output_data
.x
->widget
, XtNx
, &x1
, XtNy
, &y1
, NULL
);
2336 if (x1
== 0 && y1
== 0)
2337 XtVaSetValues (f
->output_data
.x
->widget
, XtNx
, x0
, XtNy
, y0
, NULL
);
2344 #endif /* not USE_GTK */
2346 #endif /* USE_X_TOOLKIT || USE_GTK */
2348 /* xmenu_show actually displays a menu using the panes and items in menu_items
2349 and returns the value selected from it.
2350 There are two versions of xmenu_show, one for Xt and one for Xlib.
2351 Both assume input is blocked by the caller. */
2353 /* F is the frame the menu is for.
2354 X and Y are the frame-relative specified position,
2355 relative to the inside upper left corner of the frame F.
2356 FOR_CLICK is nonzero if this menu was invoked for a mouse click.
2357 KEYMAPS is 1 if this menu was specified with keymaps;
2358 in that case, we return a list containing the chosen item's value
2359 and perhaps also the pane's prefix.
2360 TITLE is the specified menu title.
2361 ERROR is a place to store an error message string in case of failure.
2362 (We return nil on failure, but the value doesn't actually matter.) */
2364 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
2366 /* The item selected in the popup menu. */
2367 static Lisp_Object
*volatile menu_item_selection
;
2371 /* Used when position a popup menu. See menu_position_func and
2372 create_and_show_popup_menu below. */
2373 struct next_popup_x_y
2380 /* The menu position function to use if we are not putting a popup
2381 menu where the pointer is.
2382 MENU is the menu to pop up.
2383 X and Y shall on exit contain x/y where the menu shall pop up.
2384 PUSH_IN is not documented in the GTK manual.
2385 USER_DATA is any data passed in when calling gtk_menu_popup.
2386 Here it points to a struct next_popup_x_y where the coordinates
2387 to store in *X and *Y are as well as the frame for the popup.
2389 Here only X and Y are used. */
2391 menu_position_func (menu
, x
, y
, push_in
, user_data
)
2398 struct next_popup_x_y
* data
= (struct next_popup_x_y
*)user_data
;
2400 int disp_width
= FRAME_X_DISPLAY_INFO (data
->f
)->width
;
2401 int disp_height
= FRAME_X_DISPLAY_INFO (data
->f
)->height
;
2406 /* Check if there is room for the menu. If not, adjust x/y so that
2407 the menu is fully visible. */
2408 gtk_widget_size_request (GTK_WIDGET (menu
), &req
);
2409 if (data
->x
+ req
.width
> disp_width
)
2410 *x
-= data
->x
+ req
.width
- disp_width
;
2411 if (data
->y
+ req
.height
> disp_height
)
2412 *y
-= data
->y
+ req
.height
- disp_height
;
2416 popup_selection_callback (widget
, client_data
)
2418 gpointer client_data
;
2420 xg_menu_item_cb_data
*cb_data
= (xg_menu_item_cb_data
*) client_data
;
2422 if (xg_crazy_callback_abort
) return;
2423 if (cb_data
) menu_item_selection
= (Lisp_Object
*) cb_data
->call_data
;
2430 struct Lisp_Save_Value
*p
= XSAVE_VALUE (arg
);
2432 popup_activated_flag
= 0;
2434 gtk_widget_destroy (GTK_WIDGET (p
->pointer
));
2439 /* Pop up the menu for frame F defined by FIRST_WV at X/Y and loop until the
2441 menu_item_selection will be set to the selection. */
2443 create_and_show_popup_menu (f
, first_wv
, x
, y
, for_click
)
2445 widget_value
*first_wv
;
2452 GtkMenuPositionFunc pos_func
= 0; /* Pop up at pointer. */
2453 struct next_popup_x_y popup_x_y
;
2454 int specpdl_count
= SPECPDL_INDEX ();
2456 xg_crazy_callback_abort
= 1;
2457 menu
= xg_create_widget ("popup", first_wv
->name
, f
, first_wv
,
2458 G_CALLBACK (popup_selection_callback
),
2459 G_CALLBACK (popup_deactivate_callback
),
2460 G_CALLBACK (menu_highlight_callback
));
2461 xg_crazy_callback_abort
= 0;
2465 /* Not invoked by a click. pop up at x/y. */
2466 pos_func
= menu_position_func
;
2468 /* Adjust coordinates to be root-window-relative. */
2469 x
+= f
->left_pos
+ FRAME_OUTER_TO_INNER_DIFF_X (f
);
2470 y
+= f
->top_pos
+ FRAME_OUTER_TO_INNER_DIFF_Y (f
);
2476 i
= 0; /* gtk_menu_popup needs this to be 0 for a non-button popup. */
2480 for (i
= 0; i
< 5; i
++)
2481 if (FRAME_X_DISPLAY_INFO (f
)->grabbed
& (1 << i
))
2485 /* Display the menu. */
2486 gtk_widget_show_all (menu
);
2487 gtk_menu_popup (GTK_MENU (menu
), 0, 0, pos_func
, &popup_x_y
, i
, 0);
2489 record_unwind_protect (pop_down_menu
, make_save_value (menu
, 0));
2491 /* Set this to one. popup_widget_loop increases it by one, so it becomes
2492 two. show_help_echo uses this to detect popup menus. */
2493 popup_activated_flag
= 1;
2494 /* Process events that apply to the menu. */
2495 popup_widget_loop (1, menu
);
2497 unbind_to (specpdl_count
, Qnil
);
2499 /* Must reset this manually because the button release event is not passed
2500 to Emacs event loop. */
2501 FRAME_X_DISPLAY_INFO (f
)->grabbed
= 0;
2504 #else /* not USE_GTK */
2506 /* We need a unique id for each widget handled by the Lucid Widget
2509 For the main windows, and popup menus, we use this counter,
2510 which we increment each time after use. This starts from 1<<16.
2512 For menu bars, we use numbers starting at 0, counted in
2513 next_menubar_widget_id. */
2514 LWLIB_ID widget_id_tick
;
2517 popup_selection_callback (widget
, id
, client_data
)
2520 XtPointer client_data
;
2522 menu_item_selection
= (Lisp_Object
*) client_data
;
2525 /* ARG is the LWLIB ID of the dialog box, represented
2526 as a Lisp object as (HIGHPART . LOWPART). */
2532 LWLIB_ID id
= (XINT (XCAR (arg
)) << 4 * sizeof (LWLIB_ID
)
2533 | XINT (XCDR (arg
)));
2536 lw_destroy_all_widgets (id
);
2538 popup_activated_flag
= 0;
2543 /* Pop up the menu for frame F defined by FIRST_WV at X/Y and loop until the
2545 menu_item_selection will be set to the selection. */
2547 create_and_show_popup_menu (f
, first_wv
, x
, y
, for_click
)
2549 widget_value
*first_wv
;
2557 XButtonPressedEvent dummy
;
2561 menu_id
= widget_id_tick
++;
2562 menu
= lw_create_widget ("popup", first_wv
->name
, menu_id
, first_wv
,
2563 f
->output_data
.x
->widget
, 1, 0,
2564 popup_selection_callback
,
2565 popup_deactivate_callback
,
2566 menu_highlight_callback
);
2568 dummy
.type
= ButtonPress
;
2570 dummy
.send_event
= 0;
2571 dummy
.display
= FRAME_X_DISPLAY (f
);
2572 dummy
.time
= CurrentTime
;
2573 dummy
.root
= FRAME_X_DISPLAY_INFO (f
)->root_window
;
2574 dummy
.window
= dummy
.root
;
2575 dummy
.subwindow
= dummy
.root
;
2579 /* Adjust coordinates to be root-window-relative. */
2580 x
+= f
->left_pos
+ FRAME_OUTER_TO_INNER_DIFF_X (f
);
2581 y
+= f
->top_pos
+ FRAME_OUTER_TO_INNER_DIFF_Y (f
);
2588 for (i
= 0; i
< 5; i
++)
2589 if (FRAME_X_DISPLAY_INFO (f
)->grabbed
& (1 << i
))
2592 /* Don't allow any geometry request from the user. */
2593 XtSetArg (av
[ac
], XtNgeometry
, 0); ac
++;
2594 XtSetValues (menu
, av
, ac
);
2596 /* Display the menu. */
2597 lw_popup_menu (menu
, (XEvent
*) &dummy
);
2598 popup_activated_flag
= 1;
2601 int fact
= 4 * sizeof (LWLIB_ID
);
2602 int specpdl_count
= SPECPDL_INDEX ();
2603 record_unwind_protect (pop_down_menu
,
2604 Fcons (make_number (menu_id
>> (fact
)),
2605 make_number (menu_id
& ~(-1 << (fact
)))));
2607 /* Process events that apply to the menu. */
2608 popup_get_selection ((XEvent
*) 0, FRAME_X_DISPLAY_INFO (f
), menu_id
, 1);
2610 unbind_to (specpdl_count
, Qnil
);
2614 #endif /* not USE_GTK */
2617 xmenu_show (f
, x
, y
, for_click
, keymaps
, title
, error
)
2627 widget_value
*wv
, *save_wv
= 0, *first_wv
= 0, *prev_wv
= 0;
2628 widget_value
**submenu_stack
2629 = (widget_value
**) alloca (menu_items_used
* sizeof (widget_value
*));
2630 Lisp_Object
*subprefix_stack
2631 = (Lisp_Object
*) alloca (menu_items_used
* sizeof (Lisp_Object
));
2632 int submenu_depth
= 0;
2638 if (menu_items_used
<= MENU_ITEMS_PANE_LENGTH
)
2640 *error
= "Empty menu";
2644 /* Create a tree of widget_value objects
2645 representing the panes and their items. */
2646 wv
= xmalloc_widget_value ();
2650 wv
->button_type
= BUTTON_TYPE_NONE
;
2655 /* Loop over all panes and items, filling in the tree. */
2657 while (i
< menu_items_used
)
2659 if (EQ (XVECTOR (menu_items
)->contents
[i
], Qnil
))
2661 submenu_stack
[submenu_depth
++] = save_wv
;
2667 else if (EQ (XVECTOR (menu_items
)->contents
[i
], Qlambda
))
2670 save_wv
= submenu_stack
[--submenu_depth
];
2674 else if (EQ (XVECTOR (menu_items
)->contents
[i
], Qt
)
2675 && submenu_depth
!= 0)
2676 i
+= MENU_ITEMS_PANE_LENGTH
;
2677 /* Ignore a nil in the item list.
2678 It's meaningful only for dialog boxes. */
2679 else if (EQ (XVECTOR (menu_items
)->contents
[i
], Qquote
))
2681 else if (EQ (XVECTOR (menu_items
)->contents
[i
], Qt
))
2683 /* Create a new pane. */
2684 Lisp_Object pane_name
, prefix
;
2687 pane_name
= AREF (menu_items
, i
+ MENU_ITEMS_PANE_NAME
);
2688 prefix
= AREF (menu_items
, i
+ MENU_ITEMS_PANE_PREFIX
);
2690 #ifndef HAVE_MULTILINGUAL_MENU
2691 if (STRINGP (pane_name
) && STRING_MULTIBYTE (pane_name
))
2693 pane_name
= ENCODE_MENU_STRING (pane_name
);
2694 AREF (menu_items
, i
+ MENU_ITEMS_PANE_NAME
) = pane_name
;
2697 pane_string
= (NILP (pane_name
)
2698 ? "" : (char *) SDATA (pane_name
));
2699 /* If there is just one top-level pane, put all its items directly
2700 under the top-level menu. */
2701 if (menu_items_n_panes
== 1)
2704 /* If the pane has a meaningful name,
2705 make the pane a top-level menu item
2706 with its items as a submenu beneath it. */
2707 if (!keymaps
&& strcmp (pane_string
, ""))
2709 wv
= xmalloc_widget_value ();
2713 first_wv
->contents
= wv
;
2714 wv
->name
= pane_string
;
2715 if (keymaps
&& !NILP (prefix
))
2719 wv
->button_type
= BUTTON_TYPE_NONE
;
2724 else if (first_pane
)
2730 i
+= MENU_ITEMS_PANE_LENGTH
;
2734 /* Create a new item within current pane. */
2735 Lisp_Object item_name
, enable
, descrip
, def
, type
, selected
, help
;
2736 item_name
= AREF (menu_items
, i
+ MENU_ITEMS_ITEM_NAME
);
2737 enable
= AREF (menu_items
, i
+ MENU_ITEMS_ITEM_ENABLE
);
2738 descrip
= AREF (menu_items
, i
+ MENU_ITEMS_ITEM_EQUIV_KEY
);
2739 def
= AREF (menu_items
, i
+ MENU_ITEMS_ITEM_DEFINITION
);
2740 type
= AREF (menu_items
, i
+ MENU_ITEMS_ITEM_TYPE
);
2741 selected
= AREF (menu_items
, i
+ MENU_ITEMS_ITEM_SELECTED
);
2742 help
= AREF (menu_items
, i
+ MENU_ITEMS_ITEM_HELP
);
2744 #ifndef HAVE_MULTILINGUAL_MENU
2745 if (STRINGP (item_name
) && STRING_MULTIBYTE (item_name
))
2747 item_name
= ENCODE_MENU_STRING (item_name
);
2748 AREF (menu_items
, i
+ MENU_ITEMS_ITEM_NAME
) = item_name
;
2751 if (STRINGP (descrip
) && STRING_MULTIBYTE (descrip
))
2753 descrip
= ENCODE_MENU_STRING (descrip
);
2754 AREF (menu_items
, i
+ MENU_ITEMS_ITEM_EQUIV_KEY
) = descrip
;
2756 #endif /* not HAVE_MULTILINGUAL_MENU */
2758 wv
= xmalloc_widget_value ();
2762 save_wv
->contents
= wv
;
2763 wv
->name
= (char *) SDATA (item_name
);
2764 if (!NILP (descrip
))
2765 wv
->key
= (char *) SDATA (descrip
);
2767 /* If this item has a null value,
2768 make the call_data null so that it won't display a box
2769 when the mouse is on it. */
2771 = (!NILP (def
) ? (void *) &XVECTOR (menu_items
)->contents
[i
] : 0);
2772 wv
->enabled
= !NILP (enable
);
2775 wv
->button_type
= BUTTON_TYPE_NONE
;
2776 else if (EQ (type
, QCtoggle
))
2777 wv
->button_type
= BUTTON_TYPE_TOGGLE
;
2778 else if (EQ (type
, QCradio
))
2779 wv
->button_type
= BUTTON_TYPE_RADIO
;
2783 wv
->selected
= !NILP (selected
);
2785 if (! STRINGP (help
))
2792 i
+= MENU_ITEMS_ITEM_LENGTH
;
2796 /* Deal with the title, if it is non-nil. */
2799 widget_value
*wv_title
= xmalloc_widget_value ();
2800 widget_value
*wv_sep1
= xmalloc_widget_value ();
2801 widget_value
*wv_sep2
= xmalloc_widget_value ();
2803 wv_sep2
->name
= "--";
2804 wv_sep2
->next
= first_wv
->contents
;
2805 wv_sep2
->help
= Qnil
;
2807 wv_sep1
->name
= "--";
2808 wv_sep1
->next
= wv_sep2
;
2809 wv_sep1
->help
= Qnil
;
2811 #ifndef HAVE_MULTILINGUAL_MENU
2812 if (STRING_MULTIBYTE (title
))
2813 title
= ENCODE_MENU_STRING (title
);
2816 wv_title
->name
= (char *) SDATA (title
);
2817 wv_title
->enabled
= TRUE
;
2818 wv_title
->button_type
= BUTTON_TYPE_NONE
;
2819 wv_title
->next
= wv_sep1
;
2820 wv_title
->help
= Qnil
;
2821 first_wv
->contents
= wv_title
;
2824 /* No selection has been chosen yet. */
2825 menu_item_selection
= 0;
2827 /* Actually create and show the menu until popped down. */
2828 create_and_show_popup_menu (f
, first_wv
, x
, y
, for_click
);
2830 /* Free the widget_value objects we used to specify the contents. */
2831 free_menubar_widget_value_tree (first_wv
);
2833 /* Find the selected item, and its pane, to return
2834 the proper value. */
2835 if (menu_item_selection
!= 0)
2837 Lisp_Object prefix
, entry
;
2839 prefix
= entry
= Qnil
;
2841 while (i
< menu_items_used
)
2843 if (EQ (XVECTOR (menu_items
)->contents
[i
], Qnil
))
2845 subprefix_stack
[submenu_depth
++] = prefix
;
2849 else if (EQ (XVECTOR (menu_items
)->contents
[i
], Qlambda
))
2851 prefix
= subprefix_stack
[--submenu_depth
];
2854 else if (EQ (XVECTOR (menu_items
)->contents
[i
], Qt
))
2857 = XVECTOR (menu_items
)->contents
[i
+ MENU_ITEMS_PANE_PREFIX
];
2858 i
+= MENU_ITEMS_PANE_LENGTH
;
2860 /* Ignore a nil in the item list.
2861 It's meaningful only for dialog boxes. */
2862 else if (EQ (XVECTOR (menu_items
)->contents
[i
], Qquote
))
2867 = XVECTOR (menu_items
)->contents
[i
+ MENU_ITEMS_ITEM_VALUE
];
2868 if (menu_item_selection
== &XVECTOR (menu_items
)->contents
[i
])
2874 entry
= Fcons (entry
, Qnil
);
2876 entry
= Fcons (prefix
, entry
);
2877 for (j
= submenu_depth
- 1; j
>= 0; j
--)
2878 if (!NILP (subprefix_stack
[j
]))
2879 entry
= Fcons (subprefix_stack
[j
], entry
);
2883 i
+= MENU_ITEMS_ITEM_LENGTH
;
2893 dialog_selection_callback (widget
, client_data
)
2895 gpointer client_data
;
2897 /* The EMACS_INT cast avoids a warning. There's no problem
2898 as long as pointers have enough bits to hold small integers. */
2899 if ((int) (EMACS_INT
) client_data
!= -1)
2900 menu_item_selection
= (Lisp_Object
*) client_data
;
2902 popup_activated_flag
= 0;
2905 /* Pop up the dialog for frame F defined by FIRST_WV and loop until the
2907 menu_item_selection will be set to the selection. */
2909 create_and_show_dialog (f
, first_wv
)
2911 widget_value
*first_wv
;
2915 menu
= xg_create_widget ("dialog", first_wv
->name
, f
, first_wv
,
2916 G_CALLBACK (dialog_selection_callback
),
2917 G_CALLBACK (popup_deactivate_callback
),
2922 int specpdl_count
= SPECPDL_INDEX ();
2923 record_unwind_protect (pop_down_menu
, make_save_value (menu
, 0));
2925 /* Display the menu. */
2926 gtk_widget_show_all (menu
);
2928 /* Process events that apply to the menu. */
2929 popup_widget_loop (1, menu
);
2931 unbind_to (specpdl_count
, Qnil
);
2935 #else /* not USE_GTK */
2937 dialog_selection_callback (widget
, id
, client_data
)
2940 XtPointer client_data
;
2942 /* The EMACS_INT cast avoids a warning. There's no problem
2943 as long as pointers have enough bits to hold small integers. */
2944 if ((int) (EMACS_INT
) client_data
!= -1)
2945 menu_item_selection
= (Lisp_Object
*) client_data
;
2948 lw_destroy_all_widgets (id
);
2950 popup_activated_flag
= 0;
2954 /* Pop up the dialog for frame F defined by FIRST_WV and loop until the
2956 menu_item_selection will be set to the selection. */
2958 create_and_show_dialog (f
, first_wv
)
2960 widget_value
*first_wv
;
2964 dialog_id
= widget_id_tick
++;
2965 lw_create_widget (first_wv
->name
, "dialog", dialog_id
, first_wv
,
2966 f
->output_data
.x
->widget
, 1, 0,
2967 dialog_selection_callback
, 0, 0);
2968 lw_modify_all_widgets (dialog_id
, first_wv
->contents
, True
);
2970 /* Display the dialog box. */
2971 lw_pop_up_all_widgets (dialog_id
);
2972 popup_activated_flag
= 1;
2974 /* Process events that apply to the dialog box.
2975 Also handle timers. */
2977 int count
= SPECPDL_INDEX ();
2978 int fact
= 4 * sizeof (LWLIB_ID
);
2980 /* xdialog_show_unwind is responsible for popping the dialog box down. */
2981 record_unwind_protect (pop_down_menu
,
2982 Fcons (make_number (dialog_id
>> (fact
)),
2983 make_number (dialog_id
& ~(-1 << (fact
)))));
2985 popup_get_selection ((XEvent
*) 0, FRAME_X_DISPLAY_INFO (f
),
2988 unbind_to (count
, Qnil
);
2992 #endif /* not USE_GTK */
2994 static char * button_names
[] = {
2995 "button1", "button2", "button3", "button4", "button5",
2996 "button6", "button7", "button8", "button9", "button10" };
2999 xdialog_show (f
, keymaps
, title
, error
)
3005 int i
, nb_buttons
=0;
3006 char dialog_name
[6];
3008 widget_value
*wv
, *first_wv
= 0, *prev_wv
= 0;
3010 /* Number of elements seen so far, before boundary. */
3012 /* 1 means we've seen the boundary between left-hand elts and right-hand. */
3013 int boundary_seen
= 0;
3017 if (menu_items_n_panes
> 1)
3019 *error
= "Multiple panes in dialog box";
3023 /* Create a tree of widget_value objects
3024 representing the text label and buttons. */
3026 Lisp_Object pane_name
, prefix
;
3028 pane_name
= XVECTOR (menu_items
)->contents
[MENU_ITEMS_PANE_NAME
];
3029 prefix
= XVECTOR (menu_items
)->contents
[MENU_ITEMS_PANE_PREFIX
];
3030 pane_string
= (NILP (pane_name
)
3031 ? "" : (char *) SDATA (pane_name
));
3032 prev_wv
= xmalloc_widget_value ();
3033 prev_wv
->value
= pane_string
;
3034 if (keymaps
&& !NILP (prefix
))
3036 prev_wv
->enabled
= 1;
3037 prev_wv
->name
= "message";
3038 prev_wv
->help
= Qnil
;
3041 /* Loop over all panes and items, filling in the tree. */
3042 i
= MENU_ITEMS_PANE_LENGTH
;
3043 while (i
< menu_items_used
)
3046 /* Create a new item within current pane. */
3047 Lisp_Object item_name
, enable
, descrip
;
3048 item_name
= XVECTOR (menu_items
)->contents
[i
+ MENU_ITEMS_ITEM_NAME
];
3049 enable
= XVECTOR (menu_items
)->contents
[i
+ MENU_ITEMS_ITEM_ENABLE
];
3051 = XVECTOR (menu_items
)->contents
[i
+ MENU_ITEMS_ITEM_EQUIV_KEY
];
3053 if (NILP (item_name
))
3055 free_menubar_widget_value_tree (first_wv
);
3056 *error
= "Submenu in dialog items";
3059 if (EQ (item_name
, Qquote
))
3061 /* This is the boundary between left-side elts
3062 and right-side elts. Stop incrementing right_count. */
3067 if (nb_buttons
>= 9)
3069 free_menubar_widget_value_tree (first_wv
);
3070 *error
= "Too many dialog items";
3074 wv
= xmalloc_widget_value ();
3076 wv
->name
= (char *) button_names
[nb_buttons
];
3077 if (!NILP (descrip
))
3078 wv
->key
= (char *) SDATA (descrip
);
3079 wv
->value
= (char *) SDATA (item_name
);
3080 wv
->call_data
= (void *) &XVECTOR (menu_items
)->contents
[i
];
3081 wv
->enabled
= !NILP (enable
);
3085 if (! boundary_seen
)
3089 i
+= MENU_ITEMS_ITEM_LENGTH
;
3092 /* If the boundary was not specified,
3093 by default put half on the left and half on the right. */
3094 if (! boundary_seen
)
3095 left_count
= nb_buttons
- nb_buttons
/ 2;
3097 wv
= xmalloc_widget_value ();
3098 wv
->name
= dialog_name
;
3100 /* Dialog boxes use a really stupid name encoding
3101 which specifies how many buttons to use
3102 and how many buttons are on the right.
3103 The Q means something also. */
3104 dialog_name
[0] = 'Q';
3105 dialog_name
[1] = '0' + nb_buttons
;
3106 dialog_name
[2] = 'B';
3107 dialog_name
[3] = 'R';
3108 /* Number of buttons to put on the right. */
3109 dialog_name
[4] = '0' + nb_buttons
- left_count
;
3111 wv
->contents
= first_wv
;
3115 /* No selection has been chosen yet. */
3116 menu_item_selection
= 0;
3118 /* Actually create and show the dialog. */
3119 create_and_show_dialog (f
, first_wv
);
3121 /* Free the widget_value objects we used to specify the contents. */
3122 free_menubar_widget_value_tree (first_wv
);
3124 /* Find the selected item, and its pane, to return
3125 the proper value. */
3126 if (menu_item_selection
!= 0)
3132 while (i
< menu_items_used
)
3136 if (EQ (XVECTOR (menu_items
)->contents
[i
], Qt
))
3139 = XVECTOR (menu_items
)->contents
[i
+ MENU_ITEMS_PANE_PREFIX
];
3140 i
+= MENU_ITEMS_PANE_LENGTH
;
3142 else if (EQ (XVECTOR (menu_items
)->contents
[i
], Qquote
))
3144 /* This is the boundary between left-side elts and
3151 = XVECTOR (menu_items
)->contents
[i
+ MENU_ITEMS_ITEM_VALUE
];
3152 if (menu_item_selection
== &XVECTOR (menu_items
)->contents
[i
])
3156 entry
= Fcons (entry
, Qnil
);
3158 entry
= Fcons (prefix
, entry
);
3162 i
+= MENU_ITEMS_ITEM_LENGTH
;
3167 /* Make "Cancel" equivalent to C-g. */
3168 Fsignal (Qquit
, Qnil
);
3173 #else /* not USE_X_TOOLKIT && not USE_GTK */
3175 /* The frame of the last activated non-toolkit menu bar.
3176 Used to generate menu help events. */
3178 static struct frame
*menu_help_frame
;
3181 /* Show help HELP_STRING, or clear help if HELP_STRING is null.
3183 PANE is the pane number, and ITEM is the menu item number in
3184 the menu (currently not used).
3186 This cannot be done with generating a HELP_EVENT because
3187 XMenuActivate contains a loop that doesn't let Emacs process
3191 menu_help_callback (help_string
, pane
, item
)
3195 extern Lisp_Object Qmenu_item
;
3196 Lisp_Object
*first_item
;
3197 Lisp_Object pane_name
;
3198 Lisp_Object menu_object
;
3200 first_item
= XVECTOR (menu_items
)->contents
;
3201 if (EQ (first_item
[0], Qt
))
3202 pane_name
= first_item
[MENU_ITEMS_PANE_NAME
];
3203 else if (EQ (first_item
[0], Qquote
))
3204 /* This shouldn't happen, see xmenu_show. */
3205 pane_name
= empty_string
;
3207 pane_name
= first_item
[MENU_ITEMS_ITEM_NAME
];
3209 /* (menu-item MENU-NAME PANE-NUMBER) */
3210 menu_object
= Fcons (Qmenu_item
,
3212 Fcons (make_number (pane
), Qnil
)));
3213 show_help_echo (help_string
? build_string (help_string
) : Qnil
,
3214 Qnil
, menu_object
, make_number (item
), 1);
3221 struct Lisp_Save_Value
*p1
= XSAVE_VALUE (Fcar (arg
));
3222 struct Lisp_Save_Value
*p2
= XSAVE_VALUE (Fcdr (arg
));
3224 FRAME_PTR f
= p1
->pointer
;
3225 XMenu
*menu
= p2
->pointer
;
3229 XUngrabPointer (FRAME_X_DISPLAY (f
), CurrentTime
);
3230 XUngrabKeyboard (FRAME_X_DISPLAY (f
), CurrentTime
);
3232 XMenuDestroy (FRAME_X_DISPLAY (f
), menu
);
3234 #ifdef HAVE_X_WINDOWS
3235 /* Assume the mouse has moved out of the X window.
3236 If it has actually moved in, we will get an EnterNotify. */
3237 x_mouse_leave (FRAME_X_DISPLAY_INFO (f
));
3239 /* State that no mouse buttons are now held.
3240 (The oldXMenu code doesn't track this info for us.)
3241 That is not necessarily true, but the fiction leads to reasonable
3242 results, and it is a pain to ask which are actually held now. */
3243 FRAME_X_DISPLAY_INFO (f
)->grabbed
= 0;
3245 #endif /* HAVE_X_WINDOWS */
3254 xmenu_show (f
, x
, y
, for_click
, keymaps
, title
, error
)
3264 int pane
, selidx
, lpane
, status
;
3265 Lisp_Object entry
, pane_prefix
;
3267 int ulx
, uly
, width
, height
;
3268 int dispwidth
, dispheight
;
3269 int i
, j
, lines
, maxlines
;
3272 unsigned int dummy_uint
;
3273 int specpdl_count
= SPECPDL_INDEX ();
3276 if (menu_items_n_panes
== 0)
3279 if (menu_items_used
<= MENU_ITEMS_PANE_LENGTH
)
3281 *error
= "Empty menu";
3285 /* Figure out which root window F is on. */
3286 XGetGeometry (FRAME_X_DISPLAY (f
), FRAME_X_WINDOW (f
), &root
,
3287 &dummy_int
, &dummy_int
, &dummy_uint
, &dummy_uint
,
3288 &dummy_uint
, &dummy_uint
);
3290 /* Make the menu on that window. */
3291 menu
= XMenuCreate (FRAME_X_DISPLAY (f
), root
, "emacs");
3294 *error
= "Can't create menu";
3298 #ifdef HAVE_X_WINDOWS
3299 /* Adjust coordinates to relative to the outer (window manager) window. */
3300 x
+= FRAME_OUTER_TO_INNER_DIFF_X (f
);
3301 y
+= FRAME_OUTER_TO_INNER_DIFF_Y (f
);
3302 #endif /* HAVE_X_WINDOWS */
3304 /* Adjust coordinates to be root-window-relative. */
3308 /* Create all the necessary panes and their items. */
3309 maxlines
= lines
= i
= 0;
3310 while (i
< menu_items_used
)
3312 if (EQ (XVECTOR (menu_items
)->contents
[i
], Qt
))
3314 /* Create a new pane. */
3315 Lisp_Object pane_name
, prefix
;
3318 maxlines
= max (maxlines
, lines
);
3320 pane_name
= XVECTOR (menu_items
)->contents
[i
+ MENU_ITEMS_PANE_NAME
];
3321 prefix
= XVECTOR (menu_items
)->contents
[i
+ MENU_ITEMS_PANE_PREFIX
];
3322 pane_string
= (NILP (pane_name
)
3323 ? "" : (char *) SDATA (pane_name
));
3324 if (keymaps
&& !NILP (prefix
))
3327 lpane
= XMenuAddPane (FRAME_X_DISPLAY (f
), menu
, pane_string
, TRUE
);
3328 if (lpane
== XM_FAILURE
)
3330 XMenuDestroy (FRAME_X_DISPLAY (f
), menu
);
3331 *error
= "Can't create pane";
3334 i
+= MENU_ITEMS_PANE_LENGTH
;
3336 /* Find the width of the widest item in this pane. */
3339 while (j
< menu_items_used
)
3342 item
= XVECTOR (menu_items
)->contents
[j
];
3350 width
= SBYTES (item
);
3351 if (width
> maxwidth
)
3354 j
+= MENU_ITEMS_ITEM_LENGTH
;
3357 /* Ignore a nil in the item list.
3358 It's meaningful only for dialog boxes. */
3359 else if (EQ (XVECTOR (menu_items
)->contents
[i
], Qquote
))
3363 /* Create a new item within current pane. */
3364 Lisp_Object item_name
, enable
, descrip
, help
;
3365 unsigned char *item_data
;
3368 item_name
= XVECTOR (menu_items
)->contents
[i
+ MENU_ITEMS_ITEM_NAME
];
3369 enable
= XVECTOR (menu_items
)->contents
[i
+ MENU_ITEMS_ITEM_ENABLE
];
3371 = XVECTOR (menu_items
)->contents
[i
+ MENU_ITEMS_ITEM_EQUIV_KEY
];
3372 help
= XVECTOR (menu_items
)->contents
[i
+ MENU_ITEMS_ITEM_HELP
];
3373 help_string
= STRINGP (help
) ? SDATA (help
) : NULL
;
3375 if (!NILP (descrip
))
3377 int gap
= maxwidth
- SBYTES (item_name
);
3380 spacer
= Fmake_string (make_number (gap
), make_number (' '));
3381 item_name
= concat2 (item_name
, spacer
);
3382 item_name
= concat2 (item_name
, descrip
);
3383 item_data
= SDATA (item_name
);
3385 /* if alloca is fast, use that to make the space,
3386 to reduce gc needs. */
3388 = (unsigned char *) alloca (maxwidth
3389 + SBYTES (descrip
) + 1);
3390 bcopy (SDATA (item_name
), item_data
,
3391 SBYTES (item_name
));
3392 for (j
= SCHARS (item_name
); j
< maxwidth
; j
++)
3394 bcopy (SDATA (descrip
), item_data
+ j
,
3396 item_data
[j
+ SBYTES (descrip
)] = 0;
3400 item_data
= SDATA (item_name
);
3402 if (XMenuAddSelection (FRAME_X_DISPLAY (f
),
3403 menu
, lpane
, 0, item_data
,
3404 !NILP (enable
), help_string
)
3407 XMenuDestroy (FRAME_X_DISPLAY (f
), menu
);
3408 *error
= "Can't add selection to menu";
3411 i
+= MENU_ITEMS_ITEM_LENGTH
;
3416 maxlines
= max (maxlines
, lines
);
3418 /* All set and ready to fly. */
3419 XMenuRecompute (FRAME_X_DISPLAY (f
), menu
);
3420 dispwidth
= DisplayWidth (FRAME_X_DISPLAY (f
), FRAME_X_SCREEN_NUMBER (f
));
3421 dispheight
= DisplayHeight (FRAME_X_DISPLAY (f
), FRAME_X_SCREEN_NUMBER (f
));
3422 x
= min (x
, dispwidth
);
3423 y
= min (y
, dispheight
);
3426 XMenuLocate (FRAME_X_DISPLAY (f
), menu
, 0, 0, x
, y
,
3427 &ulx
, &uly
, &width
, &height
);
3428 if (ulx
+width
> dispwidth
)
3430 x
-= (ulx
+ width
) - dispwidth
;
3431 ulx
= dispwidth
- width
;
3433 if (uly
+height
> dispheight
)
3435 y
-= (uly
+ height
) - dispheight
;
3436 uly
= dispheight
- height
;
3438 if (ulx
< 0) x
-= ulx
;
3439 if (uly
< 0) y
-= uly
;
3443 /* If position was not given by a mouse click, adjust so upper left
3444 corner of the menu as a whole ends up at given coordinates. This
3445 is what x-popup-menu says in its documentation. */
3447 y
+= 1.5*height
/(maxlines
+2);
3450 XMenuSetAEQ (menu
, TRUE
);
3451 XMenuSetFreeze (menu
, TRUE
);
3455 XMenuActivateSetWaitFunction (x_menu_wait_for_event
, FRAME_X_DISPLAY (f
));
3458 record_unwind_protect (pop_down_menu
,
3459 Fcons (make_save_value (f
, 0),
3460 make_save_value (menu
, 0)));
3462 /* Help display under X won't work because XMenuActivate contains
3463 a loop that doesn't give Emacs a chance to process it. */
3464 menu_help_frame
= f
;
3465 status
= XMenuActivate (FRAME_X_DISPLAY (f
), menu
, &pane
, &selidx
,
3466 x
, y
, ButtonReleaseMask
, &datap
,
3467 menu_help_callback
);
3473 fprintf (stderr
, "pane= %d line = %d\n", panes
, selidx
);
3476 /* Find the item number SELIDX in pane number PANE. */
3478 while (i
< menu_items_used
)
3480 if (EQ (XVECTOR (menu_items
)->contents
[i
], Qt
))
3484 = XVECTOR (menu_items
)->contents
[i
+ MENU_ITEMS_PANE_PREFIX
];
3486 i
+= MENU_ITEMS_PANE_LENGTH
;
3495 = XVECTOR (menu_items
)->contents
[i
+ MENU_ITEMS_ITEM_VALUE
];
3498 entry
= Fcons (entry
, Qnil
);
3499 if (!NILP (pane_prefix
))
3500 entry
= Fcons (pane_prefix
, entry
);
3506 i
+= MENU_ITEMS_ITEM_LENGTH
;
3512 *error
= "Can't activate menu";
3517 /* Make "Cancel" equivalent to C-g unless this menu was popped up by
3520 Fsignal (Qquit
, Qnil
);
3525 unbind_to (specpdl_count
, Qnil
);
3530 #endif /* not USE_X_TOOLKIT */
3532 #endif /* HAVE_MENUS */
3537 staticpro (&menu_items
);
3539 menu_items_inuse
= Qnil
;
3541 Qdebug_on_next_call
= intern ("debug-on-next-call");
3542 staticpro (&Qdebug_on_next_call
);
3544 DEFVAR_LISP ("menu-updating-frame", &Vmenu_updating_frame
,
3545 doc
: /* Frame for which we are updating a menu.
3546 The enable predicate for a menu command should check this variable. */);
3547 Vmenu_updating_frame
= Qnil
;
3549 #ifdef USE_X_TOOLKIT
3550 widget_id_tick
= (1<<16);
3551 next_menubar_widget_id
= 1;
3554 defsubr (&Sx_popup_menu
);
3556 defsubr (&Sx_popup_dialog
);
3560 /* arch-tag: 92ea573c-398e-496e-ac73-2436f7d63242
3561 (do not change this comment) */