1 /* X Communication module for terminals which understand the X protocol.
2 Copyright (C) 1986, 1988, 1993, 1994, 1996, 1999, 2000, 2001, 2002, 2003,
3 2004, 2005, 2006 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., 51 Franklin Street, Fifth Floor,
20 Boston, MA 02110-1301, 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 #if 0 /* Why was this included? And without syssignal.h? */
37 /* On 4.3 this loses if it comes after xterm.h. */
44 #include "termhooks.h"
49 #include "blockinput.h"
53 #include "sysselect.h"
60 /* This may include sys/types.h, and that somehow loses
61 if this is not done before the other system files. */
65 /* Load sys/types.h if not already loaded.
66 In some systems loading it twice is suicidal. */
68 #include <sys/types.h>
71 #include "dispextern.h"
74 /* Defining HAVE_MULTILINGUAL_MENU would mean that the toolkit menu
75 code accepts the Emacs internal encoding. */
76 #undef HAVE_MULTILINGUAL_MENU
80 #include <X11/IntrinsicP.h>
81 #include <X11/CoreP.h>
82 #include <X11/StringDefs.h>
83 #include <X11/Shell.h>
85 #include <X11/Xaw/Paned.h>
86 #endif /* USE_LUCID */
87 #include "../lwlib/lwlib.h"
88 #else /* not USE_X_TOOLKIT */
90 #include "../oldXMenu/XMenu.h"
92 #endif /* not USE_X_TOOLKIT */
93 #endif /* HAVE_X_WINDOWS */
100 Lisp_Object Vmenu_updating_frame
;
102 Lisp_Object Qdebug_on_next_call
;
104 extern Lisp_Object Qmenu_bar
;
106 extern Lisp_Object QCtoggle
, QCradio
;
108 extern Lisp_Object Voverriding_local_map
;
109 extern Lisp_Object Voverriding_local_map_menu_flag
;
111 extern Lisp_Object Qoverriding_local_map
, Qoverriding_terminal_local_map
;
113 extern Lisp_Object Qmenu_bar_update_hook
;
116 extern void set_frame_menubar
P_ ((FRAME_PTR
, int, int));
117 extern XtAppContext Xt_app_con
;
119 static Lisp_Object xdialog_show
P_ ((FRAME_PTR
, int, Lisp_Object
, Lisp_Object
,
121 static void popup_get_selection
P_ ((XEvent
*, struct x_display_info
*,
124 /* Define HAVE_BOXES if menus can handle radio and toggle buttons. */
127 #endif /* USE_X_TOOLKIT */
132 extern void set_frame_menubar
P_ ((FRAME_PTR
, int, int));
133 static Lisp_Object xdialog_show
P_ ((FRAME_PTR
, int, Lisp_Object
, Lisp_Object
,
137 /* This is how to deal with multibyte text if HAVE_MULTILINGUAL_MENU
138 isn't defined. The use of HAVE_MULTILINGUAL_MENU could probably be
139 confined to an extended version of this with sections of code below
140 using it unconditionally. */
142 /* gtk just uses utf-8. */
143 # define ENCODE_MENU_STRING(str) ENCODE_UTF_8 (str)
144 #elif defined HAVE_X_I18N
145 # define ENCODE_MENU_STRING(str) ENCODE_SYSTEM (str)
147 # define ENCODE_MENU_STRING(str) string_make_unibyte (str)
150 static void push_menu_item
P_ ((Lisp_Object
, Lisp_Object
, Lisp_Object
,
151 Lisp_Object
, Lisp_Object
, Lisp_Object
,
152 Lisp_Object
, Lisp_Object
));
153 static int update_frame_menubar
P_ ((struct frame
*));
154 static Lisp_Object xmenu_show
P_ ((struct frame
*, int, int, int, int,
155 Lisp_Object
, char **));
156 static void keymap_panes
P_ ((Lisp_Object
*, int, int));
157 static void single_keymap_panes
P_ ((Lisp_Object
, Lisp_Object
, Lisp_Object
,
159 static void list_of_panes
P_ ((Lisp_Object
));
160 static void list_of_items
P_ ((Lisp_Object
));
163 /* This holds a Lisp vector that holds the results of decoding
164 the keymaps or alist-of-alists that specify a menu.
166 It describes the panes and items within the panes.
168 Each pane is described by 3 elements in the vector:
169 t, the pane name, the pane's prefix key.
170 Then follow the pane's items, with 5 elements per item:
171 the item string, the enable flag, the item's value,
172 the definition, and the equivalent keyboard key's description string.
174 In some cases, multiple levels of menus may be described.
175 A single vector slot containing nil indicates the start of a submenu.
176 A single vector slot containing lambda indicates the end of a submenu.
177 The submenu follows a menu item which is the way to reach the submenu.
179 A single vector slot containing quote indicates that the
180 following items should appear on the right of a dialog box.
182 Using a Lisp vector to hold this information while we decode it
183 takes care of protecting all the data from GC. */
185 #define MENU_ITEMS_PANE_NAME 1
186 #define MENU_ITEMS_PANE_PREFIX 2
187 #define MENU_ITEMS_PANE_LENGTH 3
191 MENU_ITEMS_ITEM_NAME
= 0,
192 MENU_ITEMS_ITEM_ENABLE
,
193 MENU_ITEMS_ITEM_VALUE
,
194 MENU_ITEMS_ITEM_EQUIV_KEY
,
195 MENU_ITEMS_ITEM_DEFINITION
,
196 MENU_ITEMS_ITEM_TYPE
,
197 MENU_ITEMS_ITEM_SELECTED
,
198 MENU_ITEMS_ITEM_HELP
,
199 MENU_ITEMS_ITEM_LENGTH
202 static Lisp_Object menu_items
;
204 /* If non-nil, means that the global vars defined here are already in use.
205 Used to detect cases where we try to re-enter this non-reentrant code. */
206 static Lisp_Object menu_items_inuse
;
208 /* Number of slots currently allocated in menu_items. */
209 static int menu_items_allocated
;
211 /* This is the index in menu_items of the first empty slot. */
212 static int menu_items_used
;
214 /* The number of panes currently recorded in menu_items,
215 excluding those within submenus. */
216 static int menu_items_n_panes
;
218 /* Current depth within submenus. */
219 static int menu_items_submenu_depth
;
221 /* Flag which when set indicates a dialog or menu has been posted by
222 Xt on behalf of one of the widget sets. */
223 static int popup_activated_flag
;
225 static int next_menubar_widget_id
;
227 /* This is set nonzero after the user activates the menu bar, and set
228 to zero again after the menu bars are redisplayed by prepare_menu_bar.
229 While it is nonzero, all calls to set_frame_menubar go deep.
231 I don't understand why this is needed, but it does seem to be
232 needed on Motif, according to Marcus Daniels <marcus@sysc.pdx.edu>. */
234 int pending_menu_activation
;
238 /* Return the frame whose ->output_data.x->id equals ID, or 0 if none. */
240 static struct frame
*
241 menubar_id_to_frame (id
)
244 Lisp_Object tail
, frame
;
247 for (tail
= Vframe_list
; GC_CONSP (tail
); tail
= XCDR (tail
))
250 if (!GC_FRAMEP (frame
))
253 if (!FRAME_WINDOW_P (f
))
255 if (f
->output_data
.x
->id
== id
)
263 /* Initialize the menu_items structure if we haven't already done so.
264 Also mark it as currently empty. */
269 if (!NILP (menu_items_inuse
))
270 error ("Trying to use a menu from within a menu-entry");
272 if (NILP (menu_items
))
274 menu_items_allocated
= 60;
275 menu_items
= Fmake_vector (make_number (menu_items_allocated
), Qnil
);
278 menu_items_inuse
= Qt
;
280 menu_items_n_panes
= 0;
281 menu_items_submenu_depth
= 0;
284 /* Call at the end of generating the data in menu_items. */
292 unuse_menu_items (dummy
)
295 return menu_items_inuse
= Qnil
;
298 /* Call when finished using the data for the current menu
302 discard_menu_items ()
304 /* Free the structure if it is especially large.
305 Otherwise, hold on to it, to save time. */
306 if (menu_items_allocated
> 200)
309 menu_items_allocated
= 0;
311 xassert (NILP (menu_items_inuse
));
314 /* This undoes save_menu_items, and it is called by the specpdl unwind
318 restore_menu_items (saved
)
321 menu_items
= XCAR (saved
);
322 menu_items_inuse
= (! NILP (menu_items
) ? Qt
: Qnil
);
323 menu_items_allocated
= (VECTORP (menu_items
) ? ASIZE (menu_items
) : 0);
324 saved
= XCDR (saved
);
325 menu_items_used
= XINT (XCAR (saved
));
326 saved
= XCDR (saved
);
327 menu_items_n_panes
= XINT (XCAR (saved
));
328 saved
= XCDR (saved
);
329 menu_items_submenu_depth
= XINT (XCAR (saved
));
332 /* Push the whole state of menu_items processing onto the specpdl.
333 It will be restored when the specpdl is unwound. */
338 Lisp_Object saved
= list4 (!NILP (menu_items_inuse
) ? menu_items
: Qnil
,
339 make_number (menu_items_used
),
340 make_number (menu_items_n_panes
),
341 make_number (menu_items_submenu_depth
));
342 record_unwind_protect (restore_menu_items
, saved
);
343 menu_items_inuse
= Qnil
;
347 /* Make the menu_items vector twice as large. */
353 int old_size
= menu_items_allocated
;
356 menu_items_allocated
*= 2;
358 menu_items
= Fmake_vector (make_number (menu_items_allocated
), Qnil
);
359 bcopy (XVECTOR (old
)->contents
, XVECTOR (menu_items
)->contents
,
360 old_size
* sizeof (Lisp_Object
));
363 /* Begin a submenu. */
366 push_submenu_start ()
368 if (menu_items_used
+ 1 > menu_items_allocated
)
371 XVECTOR (menu_items
)->contents
[menu_items_used
++] = Qnil
;
372 menu_items_submenu_depth
++;
380 if (menu_items_used
+ 1 > menu_items_allocated
)
383 XVECTOR (menu_items
)->contents
[menu_items_used
++] = Qlambda
;
384 menu_items_submenu_depth
--;
387 /* Indicate boundary between left and right. */
390 push_left_right_boundary ()
392 if (menu_items_used
+ 1 > menu_items_allocated
)
395 XVECTOR (menu_items
)->contents
[menu_items_used
++] = Qquote
;
398 /* Start a new menu pane in menu_items.
399 NAME is the pane name. PREFIX_VEC is a prefix key for this pane. */
402 push_menu_pane (name
, prefix_vec
)
403 Lisp_Object name
, prefix_vec
;
405 if (menu_items_used
+ MENU_ITEMS_PANE_LENGTH
> menu_items_allocated
)
408 if (menu_items_submenu_depth
== 0)
409 menu_items_n_panes
++;
410 XVECTOR (menu_items
)->contents
[menu_items_used
++] = Qt
;
411 XVECTOR (menu_items
)->contents
[menu_items_used
++] = name
;
412 XVECTOR (menu_items
)->contents
[menu_items_used
++] = prefix_vec
;
415 /* Push one menu item into the current pane. NAME is the string to
416 display. ENABLE if non-nil means this item can be selected. KEY
417 is the key generated by choosing this item, or nil if this item
418 doesn't really have a definition. DEF is the definition of this
419 item. EQUIV is the textual description of the keyboard equivalent
420 for this item (or nil if none). TYPE is the type of this menu
421 item, one of nil, `toggle' or `radio'. */
424 push_menu_item (name
, enable
, key
, def
, equiv
, type
, selected
, help
)
425 Lisp_Object name
, enable
, key
, def
, equiv
, type
, selected
, help
;
427 if (menu_items_used
+ MENU_ITEMS_ITEM_LENGTH
> menu_items_allocated
)
430 XVECTOR (menu_items
)->contents
[menu_items_used
++] = name
;
431 XVECTOR (menu_items
)->contents
[menu_items_used
++] = enable
;
432 XVECTOR (menu_items
)->contents
[menu_items_used
++] = key
;
433 XVECTOR (menu_items
)->contents
[menu_items_used
++] = equiv
;
434 XVECTOR (menu_items
)->contents
[menu_items_used
++] = def
;
435 XVECTOR (menu_items
)->contents
[menu_items_used
++] = type
;
436 XVECTOR (menu_items
)->contents
[menu_items_used
++] = selected
;
437 XVECTOR (menu_items
)->contents
[menu_items_used
++] = help
;
440 /* Look through KEYMAPS, a vector of keymaps that is NMAPS long,
441 and generate menu panes for them in menu_items.
442 If NOTREAL is nonzero,
443 don't bother really computing whether an item is enabled. */
446 keymap_panes (keymaps
, nmaps
, notreal
)
447 Lisp_Object
*keymaps
;
455 /* Loop over the given keymaps, making a pane for each map.
456 But don't make a pane that is empty--ignore that map instead.
457 P is the number of panes we have made so far. */
458 for (mapno
= 0; mapno
< nmaps
; mapno
++)
459 single_keymap_panes (keymaps
[mapno
],
460 Fkeymap_prompt (keymaps
[mapno
]), Qnil
, notreal
, 10);
462 finish_menu_items ();
465 /* Args passed between single_keymap_panes and single_menu_item. */
468 Lisp_Object pending_maps
;
469 int maxdepth
, notreal
;
473 static void single_menu_item
P_ ((Lisp_Object
, Lisp_Object
, Lisp_Object
,
476 /* This is a recursive subroutine of keymap_panes.
477 It handles one keymap, KEYMAP.
478 The other arguments are passed along
479 or point to local variables of the previous function.
480 If NOTREAL is nonzero, only check for equivalent key bindings, don't
481 evaluate expressions in menu items and don't make any menu.
483 If we encounter submenus deeper than MAXDEPTH levels, ignore them. */
486 single_keymap_panes (keymap
, pane_name
, prefix
, notreal
, maxdepth
)
488 Lisp_Object pane_name
;
496 skp
.pending_maps
= Qnil
;
497 skp
.maxdepth
= maxdepth
;
498 skp
.notreal
= notreal
;
504 push_menu_pane (pane_name
, prefix
);
507 /* Remember index for first item in this pane so we can go back and
508 add a prefix when (if) we see the first button. After that, notbuttons
509 is set to 0, to mark that we have seen a button and all non button
510 items need a prefix. */
511 skp
.notbuttons
= menu_items_used
;
514 GCPRO1 (skp
.pending_maps
);
515 map_keymap (keymap
, single_menu_item
, Qnil
, &skp
, 1);
518 /* Process now any submenus which want to be panes at this level. */
519 while (CONSP (skp
.pending_maps
))
521 Lisp_Object elt
, eltcdr
, string
;
522 elt
= XCAR (skp
.pending_maps
);
524 string
= XCAR (eltcdr
);
525 /* We no longer discard the @ from the beginning of the string here.
526 Instead, we do this in xmenu_show. */
527 single_keymap_panes (Fcar (elt
), string
,
528 XCDR (eltcdr
), notreal
, maxdepth
- 1);
529 skp
.pending_maps
= XCDR (skp
.pending_maps
);
533 /* This is a subroutine of single_keymap_panes that handles one
535 KEY is a key in a keymap and ITEM is its binding.
536 SKP->PENDING_MAPS_PTR is a list of keymaps waiting to be made into
538 If SKP->NOTREAL is nonzero, only check for equivalent key bindings, don't
539 evaluate expressions in menu items and don't make any menu.
540 If we encounter submenus deeper than SKP->MAXDEPTH levels, ignore them.
541 SKP->NOTBUTTONS is only used when simulating toggle boxes and radio
542 buttons. It keeps track of if we have seen a button in this menu or
546 single_menu_item (key
, item
, dummy
, skp_v
)
547 Lisp_Object key
, item
, dummy
;
550 Lisp_Object map
, item_string
, enabled
;
551 struct gcpro gcpro1
, gcpro2
;
553 struct skp
*skp
= skp_v
;
555 /* Parse the menu item and leave the result in item_properties. */
557 res
= parse_menu_item (item
, skp
->notreal
, 0);
560 return; /* Not a menu item. */
562 map
= XVECTOR (item_properties
)->contents
[ITEM_PROPERTY_MAP
];
566 /* We don't want to make a menu, just traverse the keymaps to
567 precompute equivalent key bindings. */
569 single_keymap_panes (map
, Qnil
, key
, 1, skp
->maxdepth
- 1);
573 enabled
= XVECTOR (item_properties
)->contents
[ITEM_PROPERTY_ENABLE
];
574 item_string
= XVECTOR (item_properties
)->contents
[ITEM_PROPERTY_NAME
];
576 if (!NILP (map
) && SREF (item_string
, 0) == '@')
579 /* An enabled separate pane. Remember this to handle it later. */
580 skp
->pending_maps
= Fcons (Fcons (map
, Fcons (item_string
, key
)),
586 /* Simulate radio buttons and toggle boxes by putting a prefix in
589 Lisp_Object prefix
= Qnil
;
590 Lisp_Object type
= XVECTOR (item_properties
)->contents
[ITEM_PROPERTY_TYPE
];
594 = XVECTOR (item_properties
)->contents
[ITEM_PROPERTY_SELECTED
];
597 /* The first button. Line up previous items in this menu. */
599 int index
= skp
->notbuttons
; /* Index for first item this menu. */
602 while (index
< menu_items_used
)
605 = XVECTOR (menu_items
)->contents
[index
+ MENU_ITEMS_ITEM_NAME
];
609 submenu
++; /* Skip sub menu. */
611 else if (EQ (tem
, Qlambda
))
614 submenu
--; /* End sub menu. */
616 else if (EQ (tem
, Qt
))
617 index
+= 3; /* Skip new pane marker. */
618 else if (EQ (tem
, Qquote
))
619 index
++; /* Skip a left, right divider. */
622 if (!submenu
&& SREF (tem
, 0) != '\0'
623 && SREF (tem
, 0) != '-')
624 XVECTOR (menu_items
)->contents
[index
+ MENU_ITEMS_ITEM_NAME
]
625 = concat2 (build_string (" "), tem
);
626 index
+= MENU_ITEMS_ITEM_LENGTH
;
632 /* Calculate prefix, if any, for this item. */
633 if (EQ (type
, QCtoggle
))
634 prefix
= build_string (NILP (selected
) ? "[ ] " : "[X] ");
635 else if (EQ (type
, QCradio
))
636 prefix
= build_string (NILP (selected
) ? "( ) " : "(*) ");
638 /* Not a button. If we have earlier buttons, then we need a prefix. */
639 else if (!skp
->notbuttons
&& SREF (item_string
, 0) != '\0'
640 && SREF (item_string
, 0) != '-')
641 prefix
= build_string (" ");
644 item_string
= concat2 (prefix
, item_string
);
646 #endif /* not HAVE_BOXES */
648 #if ! defined (USE_X_TOOLKIT) && ! defined (USE_GTK)
650 /* Indicate visually that this is a submenu. */
651 item_string
= concat2 (item_string
, build_string (" >"));
654 push_menu_item (item_string
, enabled
, key
,
655 XVECTOR (item_properties
)->contents
[ITEM_PROPERTY_DEF
],
656 XVECTOR (item_properties
)->contents
[ITEM_PROPERTY_KEYEQ
],
657 XVECTOR (item_properties
)->contents
[ITEM_PROPERTY_TYPE
],
658 XVECTOR (item_properties
)->contents
[ITEM_PROPERTY_SELECTED
],
659 XVECTOR (item_properties
)->contents
[ITEM_PROPERTY_HELP
]);
661 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
662 /* Display a submenu using the toolkit. */
663 if (! (NILP (map
) || NILP (enabled
)))
665 push_submenu_start ();
666 single_keymap_panes (map
, Qnil
, key
, 0, skp
->maxdepth
- 1);
672 /* Push all the panes and items of a menu described by the
673 alist-of-alists MENU.
674 This handles old-fashioned calls to x-popup-menu. */
684 for (tail
= menu
; CONSP (tail
); tail
= XCDR (tail
))
686 Lisp_Object elt
, pane_name
, pane_data
;
688 pane_name
= Fcar (elt
);
689 CHECK_STRING (pane_name
);
690 push_menu_pane (ENCODE_MENU_STRING (pane_name
), Qnil
);
691 pane_data
= Fcdr (elt
);
692 CHECK_CONS (pane_data
);
693 list_of_items (pane_data
);
696 finish_menu_items ();
699 /* Push the items in a single pane defined by the alist PANE. */
705 Lisp_Object tail
, item
, item1
;
707 for (tail
= pane
; CONSP (tail
); tail
= XCDR (tail
))
711 push_menu_item (ENCODE_MENU_STRING (item
), Qnil
, Qnil
, Qt
,
712 Qnil
, Qnil
, Qnil
, Qnil
);
713 else if (CONSP (item
))
716 CHECK_STRING (item1
);
717 push_menu_item (ENCODE_MENU_STRING (item1
), Qt
, XCDR (item
),
718 Qt
, Qnil
, Qnil
, Qnil
, Qnil
);
721 push_left_right_boundary ();
726 #ifdef HAVE_X_WINDOWS
727 /* Return the mouse position in *X and *Y. The coordinates are window
728 relative for the edit window in frame F.
729 This is for Fx_popup_menu. The mouse_position_hook can not
730 be used for X, as it returns window relative coordinates
731 for the window where the mouse is in. This could be the menu bar,
732 the scroll bar or the edit window. Fx_popup_menu needs to be
733 sure it is the edit window. */
735 mouse_position_for_popup (f
, x
, y
)
740 Window root
, dummy_window
;
745 XQueryPointer (FRAME_X_DISPLAY (f
),
746 DefaultRootWindow (FRAME_X_DISPLAY (f
)),
748 /* The root window which contains the pointer. */
751 /* Window pointer is on, not used */
754 /* The position on that root window. */
757 /* x/y in dummy_window coordinates, not used. */
760 /* Modifier keys and pointer buttons, about which
762 (unsigned int *) &dummy
);
766 /* xmenu_show expects window coordinates, not root window
767 coordinates. Translate. */
768 *x
-= f
->left_pos
+ FRAME_OUTER_TO_INNER_DIFF_X (f
);
769 *y
-= f
->top_pos
+ FRAME_OUTER_TO_INNER_DIFF_Y (f
);
772 #endif /* HAVE_X_WINDOWS */
774 DEFUN ("x-popup-menu", Fx_popup_menu
, Sx_popup_menu
, 2, 2, 0,
775 doc
: /* Pop up a deck-of-cards menu and return user's selection.
776 POSITION is a position specification. This is either a mouse button event
777 or a list ((XOFFSET YOFFSET) WINDOW)
778 where XOFFSET and YOFFSET are positions in pixels from the top left
779 corner of WINDOW. (WINDOW may be a window or a frame object.)
780 This controls the position of the top left of the menu as a whole.
781 If POSITION is t, it means to use the current mouse position.
783 MENU is a specifier for a menu. For the simplest case, MENU is a keymap.
784 The menu items come from key bindings that have a menu string as well as
785 a definition; actually, the "definition" in such a key binding looks like
786 \(STRING . REAL-DEFINITION). To give the menu a title, put a string into
787 the keymap as a top-level element.
789 If REAL-DEFINITION is nil, that puts a nonselectable string in the menu.
790 Otherwise, REAL-DEFINITION should be a valid key binding definition.
792 You can also use a list of keymaps as MENU.
793 Then each keymap makes a separate pane.
795 When MENU is a keymap or a list of keymaps, the return value is the
796 list of events corresponding to the user's choice. Note that
797 `x-popup-menu' does not actually execute the command bound to that
800 Alternatively, you can specify a menu of multiple panes
801 with a list of the form (TITLE PANE1 PANE2...),
802 where each pane is a list of form (TITLE ITEM1 ITEM2...).
803 Each ITEM is normally a cons cell (STRING . VALUE);
804 but a string can appear as an item--that makes a nonselectable line
806 With this form of menu, the return value is VALUE from the chosen item.
808 If POSITION is nil, don't display the menu at all, just precalculate the
809 cached information about equivalent key sequences.
811 If the user gets rid of the menu without making a valid choice, for
812 instance by clicking the mouse away from a valid choice or by typing
813 keyboard input, then this normally results in a quit and
814 `x-popup-menu' does not return. But if POSITION is a mouse button
815 event (indicating that the user invoked the menu with the mouse) then
816 no quit occurs and `x-popup-menu' returns nil. */)
818 Lisp_Object position
, menu
;
820 Lisp_Object keymap
, tem
;
821 int xpos
= 0, ypos
= 0;
823 char *error_name
= NULL
;
824 Lisp_Object selection
;
826 Lisp_Object x
, y
, window
;
829 int specpdl_count
= SPECPDL_INDEX ();
833 if (! NILP (position
))
835 int get_current_pos_p
= 0;
838 /* Decode the first argument: find the window and the coordinates. */
839 if (EQ (position
, Qt
)
840 || (CONSP (position
) && (EQ (XCAR (position
), Qmenu_bar
)
841 || EQ (XCAR (position
), Qtool_bar
))))
843 get_current_pos_p
= 1;
847 tem
= Fcar (position
);
850 window
= Fcar (Fcdr (position
));
852 y
= Fcar (XCDR (tem
));
857 tem
= Fcar (Fcdr (position
)); /* EVENT_START (position) */
858 window
= Fcar (tem
); /* POSN_WINDOW (tem) */
859 tem
= Fcar (Fcdr (Fcdr (tem
))); /* POSN_WINDOW_POSN (tem) */
864 /* If a click happens in an external tool bar or a detached
865 tool bar, x and y is NIL. In that case, use the current
866 mouse position. This happens for the help button in the
867 tool bar. Ideally popup-menu should pass NIL to
868 this function, but it doesn't. */
869 if (NILP (x
) && NILP (y
))
870 get_current_pos_p
= 1;
873 if (get_current_pos_p
)
875 /* Use the mouse's current position. */
876 FRAME_PTR new_f
= SELECTED_FRAME ();
877 #ifdef HAVE_X_WINDOWS
878 /* Can't use mouse_position_hook for X since it returns
879 coordinates relative to the window the mouse is in,
880 we need coordinates relative to the edit widget always. */
885 mouse_position_for_popup (new_f
, &cur_x
, &cur_y
);
886 /* cur_x/y may be negative, so use make_number. */
887 x
= make_number (cur_x
);
888 y
= make_number (cur_y
);
891 #else /* not HAVE_X_WINDOWS */
892 Lisp_Object bar_window
;
893 enum scroll_bar_part part
;
896 if (mouse_position_hook
)
897 (*mouse_position_hook
) (&new_f
, 1, &bar_window
,
898 &part
, &x
, &y
, &time
);
899 #endif /* not HAVE_X_WINDOWS */
902 XSETFRAME (window
, new_f
);
905 window
= selected_window
;
914 /* Decode where to put the menu. */
922 else if (WINDOWP (window
))
924 CHECK_LIVE_WINDOW (window
);
925 f
= XFRAME (WINDOW_FRAME (XWINDOW (window
)));
927 xpos
= WINDOW_LEFT_EDGE_X (XWINDOW (window
));
928 ypos
= WINDOW_TOP_EDGE_Y (XWINDOW (window
));
931 /* ??? Not really clean; should be CHECK_WINDOW_OR_FRAME,
932 but I don't want to make one now. */
933 CHECK_WINDOW (window
);
938 XSETFRAME (Vmenu_updating_frame
, f
);
941 Vmenu_updating_frame
= Qnil
;
942 #endif /* HAVE_MENUS */
944 record_unwind_protect (unuse_menu_items
, Qnil
);
948 /* Decode the menu items from what was specified. */
950 keymap
= get_keymap (menu
, 0, 0);
953 /* We were given a keymap. Extract menu info from the keymap. */
956 /* Extract the detailed info to make one pane. */
957 keymap_panes (&menu
, 1, NILP (position
));
959 /* Search for a string appearing directly as an element of the keymap.
960 That string is the title of the menu. */
961 prompt
= Fkeymap_prompt (keymap
);
962 if (NILP (title
) && !NILP (prompt
))
965 /* Make that be the pane title of the first pane. */
966 if (!NILP (prompt
) && menu_items_n_panes
>= 0)
967 XVECTOR (menu_items
)->contents
[MENU_ITEMS_PANE_NAME
] = prompt
;
971 else if (CONSP (menu
) && KEYMAPP (XCAR (menu
)))
973 /* We were given a list of keymaps. */
974 int nmaps
= XFASTINT (Flength (menu
));
976 = (Lisp_Object
*) alloca (nmaps
* sizeof (Lisp_Object
));
981 /* The first keymap that has a prompt string
982 supplies the menu title. */
983 for (tem
= menu
, i
= 0; CONSP (tem
); tem
= XCDR (tem
))
987 maps
[i
++] = keymap
= get_keymap (XCAR (tem
), 1, 0);
989 prompt
= Fkeymap_prompt (keymap
);
990 if (NILP (title
) && !NILP (prompt
))
994 /* Extract the detailed info to make one pane. */
995 keymap_panes (maps
, nmaps
, NILP (position
));
997 /* Make the title be the pane title of the first pane. */
998 if (!NILP (title
) && menu_items_n_panes
>= 0)
999 XVECTOR (menu_items
)->contents
[MENU_ITEMS_PANE_NAME
] = title
;
1005 /* We were given an old-fashioned menu. */
1006 title
= Fcar (menu
);
1007 CHECK_STRING (title
);
1009 list_of_panes (Fcdr (menu
));
1014 unbind_to (specpdl_count
, Qnil
);
1016 if (NILP (position
))
1018 discard_menu_items ();
1024 /* Display them in a menu. */
1027 selection
= xmenu_show (f
, xpos
, ypos
, for_click
,
1028 keymaps
, title
, &error_name
);
1031 discard_menu_items ();
1034 #endif /* HAVE_MENUS */
1036 if (error_name
) error (error_name
);
1042 DEFUN ("x-popup-dialog", Fx_popup_dialog
, Sx_popup_dialog
, 2, 3, 0,
1043 doc
: /* Pop up a dialog box and return user's selection.
1044 POSITION specifies which frame to use.
1045 This is normally a mouse button event or a window or frame.
1046 If POSITION is t, it means to use the frame the mouse is on.
1047 The dialog box appears in the middle of the specified frame.
1049 CONTENTS specifies the alternatives to display in the dialog box.
1050 It is a list of the form (DIALOG ITEM1 ITEM2...).
1051 Each ITEM is a cons cell (STRING . VALUE).
1052 The return value is VALUE from the chosen item.
1054 An ITEM may also be just a string--that makes a nonselectable item.
1055 An ITEM may also be nil--that means to put all preceding items
1056 on the left of the dialog box and all following items on the right.
1057 \(By default, approximately half appear on each side.)
1059 If HEADER is non-nil, the frame title for the box is "Information",
1060 otherwise it is "Question".
1062 If the user gets rid of the dialog box without making a valid choice,
1063 for instance using the window manager, then this produces a quit and
1064 `x-popup-dialog' does not return. */)
1065 (position
, contents
, header
)
1066 Lisp_Object position
, contents
, header
;
1073 /* Decode the first argument: find the window or frame to use. */
1074 if (EQ (position
, Qt
)
1075 || (CONSP (position
) && (EQ (XCAR (position
), Qmenu_bar
)
1076 || EQ (XCAR (position
), Qtool_bar
))))
1078 #if 0 /* Using the frame the mouse is on may not be right. */
1079 /* Use the mouse's current position. */
1080 FRAME_PTR new_f
= SELECTED_FRAME ();
1081 Lisp_Object bar_window
;
1082 enum scroll_bar_part part
;
1086 (*mouse_position_hook
) (&new_f
, 1, &bar_window
, &part
, &x
, &y
, &time
);
1089 XSETFRAME (window
, new_f
);
1091 window
= selected_window
;
1093 window
= selected_window
;
1095 else if (CONSP (position
))
1098 tem
= Fcar (position
);
1100 window
= Fcar (Fcdr (position
));
1103 tem
= Fcar (Fcdr (position
)); /* EVENT_START (position) */
1104 window
= Fcar (tem
); /* POSN_WINDOW (tem) */
1107 else if (WINDOWP (position
) || FRAMEP (position
))
1112 /* Decode where to put the menu. */
1114 if (FRAMEP (window
))
1115 f
= XFRAME (window
);
1116 else if (WINDOWP (window
))
1118 CHECK_LIVE_WINDOW (window
);
1119 f
= XFRAME (WINDOW_FRAME (XWINDOW (window
)));
1122 /* ??? Not really clean; should be CHECK_WINDOW_OR_FRAME,
1123 but I don't want to make one now. */
1124 CHECK_WINDOW (window
);
1126 #if ! defined (USE_X_TOOLKIT) && ! defined (USE_GTK)
1127 /* Display a menu with these alternatives
1128 in the middle of frame F. */
1130 Lisp_Object x
, y
, frame
, newpos
;
1131 XSETFRAME (frame
, f
);
1132 XSETINT (x
, x_pixel_width (f
) / 2);
1133 XSETINT (y
, x_pixel_height (f
) / 2);
1134 newpos
= Fcons (Fcons (x
, Fcons (y
, Qnil
)), Fcons (frame
, Qnil
));
1136 return Fx_popup_menu (newpos
,
1137 Fcons (Fcar (contents
), Fcons (contents
, Qnil
)));
1143 Lisp_Object selection
;
1144 int specpdl_count
= SPECPDL_INDEX ();
1146 /* Decode the dialog items from what was specified. */
1147 title
= Fcar (contents
);
1148 CHECK_STRING (title
);
1149 record_unwind_protect (unuse_menu_items
, Qnil
);
1151 if (NILP (Fcar (Fcdr (contents
))))
1152 /* No buttons specified, add an "Ok" button so users can pop down
1153 the dialog. Also, the lesstif/motif version crashes if there are
1155 contents
= Fcons (title
, Fcons (Fcons (build_string ("Ok"), Qt
), Qnil
));
1157 list_of_panes (Fcons (contents
, Qnil
));
1159 /* Display them in a dialog box. */
1161 selection
= xdialog_show (f
, 0, title
, header
, &error_name
);
1164 unbind_to (specpdl_count
, Qnil
);
1165 discard_menu_items ();
1167 if (error_name
) error (error_name
);
1176 /* Set menu_items_inuse so no other popup menu or dialog is created. */
1179 x_menu_set_in_use (in_use
)
1182 menu_items_inuse
= in_use
? Qt
: Qnil
;
1183 popup_activated_flag
= in_use
;
1186 /* Wait for an X event to arrive or for a timer to expire. */
1189 x_menu_wait_for_event (void *data
)
1191 extern EMACS_TIME timer_check
P_ ((int));
1193 /* Another way to do this is to register a timer callback, that can be
1194 done in GTK and Xt. But we have to do it like this when using only X
1195 anyway, and with callbacks we would have three variants for timer handling
1196 instead of the small ifdefs below. */
1199 #ifdef USE_X_TOOLKIT
1200 ! XtAppPending (Xt_app_con
)
1201 #elif defined USE_GTK
1202 ! gtk_events_pending ()
1204 ! XPending ((Display
*) data
)
1208 EMACS_TIME next_time
= timer_check (1);
1209 long secs
= EMACS_SECS (next_time
);
1210 long usecs
= EMACS_USECS (next_time
);
1211 SELECT_TYPE read_fds
;
1212 struct x_display_info
*dpyinfo
;
1215 FD_ZERO (&read_fds
);
1216 for (dpyinfo
= x_display_list
; dpyinfo
; dpyinfo
= dpyinfo
->next
)
1218 int fd
= ConnectionNumber (dpyinfo
->display
);
1219 FD_SET (fd
, &read_fds
);
1223 if (secs
< 0 || (secs
== 0 && usecs
== 0))
1225 /* Sometimes timer_check returns -1 (no timers) even if there are
1226 timers. So do a timeout anyway. */
1227 EMACS_SET_SECS (next_time
, 1);
1228 EMACS_SET_USECS (next_time
, 0);
1231 select (n
+ 1, &read_fds
, (SELECT_TYPE
*)0, (SELECT_TYPE
*)0, &next_time
);
1234 #endif /* ! MSDOS */
1237 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
1239 #ifdef USE_X_TOOLKIT
1241 /* Loop in Xt until the menu pulldown or dialog popup has been
1242 popped down (deactivated). This is used for x-popup-menu
1243 and x-popup-dialog; it is not used for the menu bar.
1245 NOTE: All calls to popup_get_selection should be protected
1246 with BLOCK_INPUT, UNBLOCK_INPUT wrappers. */
1249 popup_get_selection (initial_event
, dpyinfo
, id
, do_timers
)
1250 XEvent
*initial_event
;
1251 struct x_display_info
*dpyinfo
;
1257 while (popup_activated_flag
)
1261 event
= *initial_event
;
1266 if (do_timers
) x_menu_wait_for_event (0);
1267 XtAppNextEvent (Xt_app_con
, &event
);
1270 /* Make sure we don't consider buttons grabbed after menu goes.
1271 And make sure to deactivate for any ButtonRelease,
1272 even if XtDispatchEvent doesn't do that. */
1273 if (event
.type
== ButtonRelease
1274 && dpyinfo
->display
== event
.xbutton
.display
)
1276 dpyinfo
->grabbed
&= ~(1 << event
.xbutton
.button
);
1277 #ifdef USE_MOTIF /* Pretending that the event came from a
1278 Btn1Down seems the only way to convince Motif to
1279 activate its callbacks; setting the XmNmenuPost
1280 isn't working. --marcus@sysc.pdx.edu. */
1281 event
.xbutton
.button
= 1;
1282 /* Motif only pops down menus when no Ctrl, Alt or Mod
1283 key is pressed and the button is released. So reset key state
1284 so Motif thinks this is the case. */
1285 event
.xbutton
.state
= 0;
1288 /* Pop down on C-g and Escape. */
1289 else if (event
.type
== KeyPress
1290 && dpyinfo
->display
== event
.xbutton
.display
)
1292 KeySym keysym
= XLookupKeysym (&event
.xkey
, 0);
1294 if ((keysym
== XK_g
&& (event
.xkey
.state
& ControlMask
) != 0)
1295 || keysym
== XK_Escape
) /* Any escape, ignore modifiers. */
1296 popup_activated_flag
= 0;
1299 x_dispatch_event (&event
, event
.xany
.display
);
1303 #endif /* USE_X_TOOLKIT */
1306 /* Loop util popup_activated_flag is set to zero in a callback.
1307 Used for popup menus and dialogs. */
1310 popup_widget_loop (do_timers
, widget
)
1314 ++popup_activated_flag
;
1316 /* Process events in the Gtk event loop until done. */
1317 while (popup_activated_flag
)
1319 if (do_timers
) x_menu_wait_for_event (0);
1320 gtk_main_iteration ();
1325 /* Activate the menu bar of frame F.
1326 This is called from keyboard.c when it gets the
1327 MENU_BAR_ACTIVATE_EVENT out of the Emacs event queue.
1329 To activate the menu bar, we use the X button-press event
1330 that was saved in saved_menu_event.
1331 That makes the toolkit do its thing.
1333 But first we recompute the menu bar contents (the whole tree).
1335 The reason for saving the button event until here, instead of
1336 passing it to the toolkit right away, is that we can safely
1337 execute Lisp code. */
1340 x_activate_menubar (f
)
1343 if (!f
->output_data
.x
->saved_menu_event
->type
)
1347 if (! xg_win_to_widget (FRAME_X_DISPLAY (f
),
1348 f
->output_data
.x
->saved_menu_event
->xany
.window
))
1352 set_frame_menubar (f
, 0, 1);
1355 XPutBackEvent (f
->output_data
.x
->display_info
->display
,
1356 f
->output_data
.x
->saved_menu_event
);
1357 popup_activated_flag
= 1;
1359 XtDispatchEvent (f
->output_data
.x
->saved_menu_event
);
1363 if (f
->output_data
.x
->saved_menu_event
->type
== ButtonRelease
)
1364 pending_menu_activation
= 1;
1367 /* Ignore this if we get it a second time. */
1368 f
->output_data
.x
->saved_menu_event
->type
= 0;
1371 /* Detect if a dialog or menu has been posted. */
1376 return popup_activated_flag
;
1379 /* This callback is invoked when the user selects a menubar cascade
1380 pushbutton, but before the pulldown menu is posted. */
1384 popup_activate_callback (widget
, id
, client_data
)
1387 XtPointer client_data
;
1389 popup_activated_flag
= 1;
1393 /* This callback is invoked when a dialog or menu is finished being
1394 used and has been unposted. */
1398 popup_deactivate_callback (widget
, client_data
)
1400 gpointer client_data
;
1402 popup_activated_flag
= 0;
1406 popup_deactivate_callback (widget
, id
, client_data
)
1409 XtPointer client_data
;
1411 popup_activated_flag
= 0;
1416 /* Function that finds the frame for WIDGET and shows the HELP text
1418 F is the frame if known, or NULL if not known. */
1420 show_help_event (f
, widget
, help
)
1422 xt_or_gtk_widget widget
;
1429 XSETFRAME (frame
, f
);
1430 kbd_buffer_store_help_event (frame
, help
);
1434 #if 0 /* This code doesn't do anything useful. ++kfs */
1435 /* WIDGET is the popup menu. It's parent is the frame's
1436 widget. See which frame that is. */
1437 xt_or_gtk_widget frame_widget
= XtParent (widget
);
1440 for (tail
= Vframe_list
; GC_CONSP (tail
); tail
= XCDR (tail
))
1442 frame
= XCAR (tail
);
1443 if (GC_FRAMEP (frame
)
1444 && (f
= XFRAME (frame
),
1445 FRAME_X_P (f
) && f
->output_data
.x
->widget
== frame_widget
))
1449 show_help_echo (help
, Qnil
, Qnil
, Qnil
, 1);
1453 /* Callback called when menu items are highlighted/unhighlighted
1454 while moving the mouse over them. WIDGET is the menu bar or menu
1455 popup widget. ID is its LWLIB_ID. CALL_DATA contains a pointer to
1456 the data structure for the menu item, or null in case of
1461 menu_highlight_callback (widget
, call_data
)
1465 xg_menu_item_cb_data
*cb_data
;
1468 cb_data
= (xg_menu_item_cb_data
*) g_object_get_data (G_OBJECT (widget
),
1470 if (! cb_data
) return;
1472 help
= call_data
? cb_data
->help
: Qnil
;
1474 /* If popup_activated_flag is greater than 1 we are in a popup menu.
1475 Don't show help for them, they won't appear before the
1476 popup is popped down. */
1477 if (popup_activated_flag
<= 1)
1478 show_help_event (cb_data
->cl_data
->f
, widget
, help
);
1482 menu_highlight_callback (widget
, id
, call_data
)
1490 widget_value
*wv
= (widget_value
*) call_data
;
1492 help
= wv
? wv
->help
: Qnil
;
1494 /* Determine the frame for the help event. */
1495 f
= menubar_id_to_frame (id
);
1497 show_help_event (f
, widget
, help
);
1501 /* Find the menu selection and store it in the keyboard buffer.
1502 F is the frame the menu is on.
1503 MENU_BAR_ITEMS_USED is the length of VECTOR.
1504 VECTOR is an array of menu events for the whole menu. */
1507 find_and_call_menu_selection (f
, menu_bar_items_used
, vector
, client_data
)
1509 int menu_bar_items_used
;
1513 Lisp_Object prefix
, entry
;
1514 Lisp_Object
*subprefix_stack
;
1515 int submenu_depth
= 0;
1519 subprefix_stack
= (Lisp_Object
*) alloca (menu_bar_items_used
* sizeof (Lisp_Object
));
1523 while (i
< menu_bar_items_used
)
1525 if (EQ (XVECTOR (vector
)->contents
[i
], Qnil
))
1527 subprefix_stack
[submenu_depth
++] = prefix
;
1531 else if (EQ (XVECTOR (vector
)->contents
[i
], Qlambda
))
1533 prefix
= subprefix_stack
[--submenu_depth
];
1536 else if (EQ (XVECTOR (vector
)->contents
[i
], Qt
))
1538 prefix
= XVECTOR (vector
)->contents
[i
+ MENU_ITEMS_PANE_PREFIX
];
1539 i
+= MENU_ITEMS_PANE_LENGTH
;
1543 entry
= XVECTOR (vector
)->contents
[i
+ MENU_ITEMS_ITEM_VALUE
];
1544 /* The EMACS_INT cast avoids a warning. There's no problem
1545 as long as pointers have enough bits to hold small integers. */
1546 if ((int) (EMACS_INT
) client_data
== i
)
1549 struct input_event buf
;
1553 XSETFRAME (frame
, f
);
1554 buf
.kind
= MENU_BAR_EVENT
;
1555 buf
.frame_or_window
= frame
;
1557 kbd_buffer_store_event (&buf
);
1559 for (j
= 0; j
< submenu_depth
; j
++)
1560 if (!NILP (subprefix_stack
[j
]))
1562 buf
.kind
= MENU_BAR_EVENT
;
1563 buf
.frame_or_window
= frame
;
1564 buf
.arg
= subprefix_stack
[j
];
1565 kbd_buffer_store_event (&buf
);
1570 buf
.kind
= MENU_BAR_EVENT
;
1571 buf
.frame_or_window
= frame
;
1573 kbd_buffer_store_event (&buf
);
1576 buf
.kind
= MENU_BAR_EVENT
;
1577 buf
.frame_or_window
= frame
;
1579 kbd_buffer_store_event (&buf
);
1583 i
+= MENU_ITEMS_ITEM_LENGTH
;
1590 /* Gtk calls callbacks just because we tell it what item should be
1591 selected in a radio group. If this variable is set to a non-zero
1592 value, we are creating menus and don't want callbacks right now.
1594 static int xg_crazy_callback_abort
;
1596 /* This callback is called from the menu bar pulldown menu
1597 when the user makes a selection.
1598 Figure out what the user chose
1599 and put the appropriate events into the keyboard buffer. */
1601 menubar_selection_callback (widget
, client_data
)
1603 gpointer client_data
;
1605 xg_menu_item_cb_data
*cb_data
= (xg_menu_item_cb_data
*) client_data
;
1607 if (xg_crazy_callback_abort
)
1610 if (! cb_data
|| ! cb_data
->cl_data
|| ! cb_data
->cl_data
->f
)
1613 /* For a group of radio buttons, GTK calls the selection callback first
1614 for the item that was active before the selection and then for the one that
1615 is active after the selection. For C-h k this means we get the help on
1616 the deselected item and then the selected item is executed. Prevent that
1617 by ignoring the non-active item. */
1618 if (GTK_IS_RADIO_MENU_ITEM (widget
)
1619 && ! gtk_check_menu_item_get_active (GTK_CHECK_MENU_ITEM (widget
)))
1622 /* When a menu is popped down, X generates a focus event (i.e. focus
1623 goes back to the frame below the menu). Since GTK buffers events,
1624 we force it out here before the menu selection event. Otherwise
1625 sit-for will exit at once if the focus event follows the menu selection
1629 while (gtk_events_pending ())
1630 gtk_main_iteration ();
1633 find_and_call_menu_selection (cb_data
->cl_data
->f
,
1634 cb_data
->cl_data
->menu_bar_items_used
,
1635 cb_data
->cl_data
->menu_bar_vector
,
1636 cb_data
->call_data
);
1639 #else /* not USE_GTK */
1641 /* This callback is called from the menu bar pulldown menu
1642 when the user makes a selection.
1643 Figure out what the user chose
1644 and put the appropriate events into the keyboard buffer. */
1646 menubar_selection_callback (widget
, id
, client_data
)
1649 XtPointer client_data
;
1653 f
= menubar_id_to_frame (id
);
1656 find_and_call_menu_selection (f
, f
->menu_bar_items_used
,
1657 f
->menu_bar_vector
, client_data
);
1659 #endif /* not USE_GTK */
1661 /* Allocate a widget_value, blocking input. */
1664 xmalloc_widget_value ()
1666 widget_value
*value
;
1669 value
= malloc_widget_value ();
1675 /* This recursively calls free_widget_value on the tree of widgets.
1676 It must free all data that was malloc'ed for these widget_values.
1677 In Emacs, many slots are pointers into the data of Lisp_Strings, and
1678 must be left alone. */
1681 free_menubar_widget_value_tree (wv
)
1686 wv
->name
= wv
->value
= wv
->key
= (char *) 0xDEADBEEF;
1688 if (wv
->contents
&& (wv
->contents
!= (widget_value
*)1))
1690 free_menubar_widget_value_tree (wv
->contents
);
1691 wv
->contents
= (widget_value
*) 0xDEADBEEF;
1695 free_menubar_widget_value_tree (wv
->next
);
1696 wv
->next
= (widget_value
*) 0xDEADBEEF;
1699 free_widget_value (wv
);
1703 /* Set up data in menu_items for a menu bar item
1704 whose event type is ITEM_KEY (with string ITEM_NAME)
1705 and whose contents come from the list of keymaps MAPS. */
1708 parse_single_submenu (item_key
, item_name
, maps
)
1709 Lisp_Object item_key
, item_name
, maps
;
1713 Lisp_Object
*mapvec
;
1715 int top_level_items
= 0;
1717 length
= Flength (maps
);
1718 len
= XINT (length
);
1720 /* Convert the list MAPS into a vector MAPVEC. */
1721 mapvec
= (Lisp_Object
*) alloca (len
* sizeof (Lisp_Object
));
1722 for (i
= 0; i
< len
; i
++)
1724 mapvec
[i
] = Fcar (maps
);
1728 /* Loop over the given keymaps, making a pane for each map.
1729 But don't make a pane that is empty--ignore that map instead. */
1730 for (i
= 0; i
< len
; i
++)
1732 if (!KEYMAPP (mapvec
[i
]))
1734 /* Here we have a command at top level in the menu bar
1735 as opposed to a submenu. */
1736 top_level_items
= 1;
1737 push_menu_pane (Qnil
, Qnil
);
1738 push_menu_item (item_name
, Qt
, item_key
, mapvec
[i
],
1739 Qnil
, Qnil
, Qnil
, Qnil
);
1744 prompt
= Fkeymap_prompt (mapvec
[i
]);
1745 single_keymap_panes (mapvec
[i
],
1746 !NILP (prompt
) ? prompt
: item_name
,
1751 return top_level_items
;
1754 /* Create a tree of widget_value objects
1755 representing the panes and items
1756 in menu_items starting at index START, up to index END. */
1758 static widget_value
*
1759 digest_single_submenu (start
, end
, top_level_items
)
1760 int start
, end
, top_level_items
;
1762 widget_value
*wv
, *prev_wv
, *save_wv
, *first_wv
;
1764 int submenu_depth
= 0;
1765 widget_value
**submenu_stack
;
1769 = (widget_value
**) alloca (menu_items_used
* sizeof (widget_value
*));
1770 wv
= xmalloc_widget_value ();
1774 wv
->button_type
= BUTTON_TYPE_NONE
;
1780 /* Loop over all panes and items made by the preceding call
1781 to parse_single_submenu and construct a tree of widget_value objects.
1782 Ignore the panes and items used by previous calls to
1783 digest_single_submenu, even though those are also in menu_items. */
1787 if (EQ (XVECTOR (menu_items
)->contents
[i
], Qnil
))
1789 submenu_stack
[submenu_depth
++] = save_wv
;
1794 else if (EQ (XVECTOR (menu_items
)->contents
[i
], Qlambda
))
1797 save_wv
= submenu_stack
[--submenu_depth
];
1800 else if (EQ (XVECTOR (menu_items
)->contents
[i
], Qt
)
1801 && submenu_depth
!= 0)
1802 i
+= MENU_ITEMS_PANE_LENGTH
;
1803 /* Ignore a nil in the item list.
1804 It's meaningful only for dialog boxes. */
1805 else if (EQ (XVECTOR (menu_items
)->contents
[i
], Qquote
))
1807 else if (EQ (XVECTOR (menu_items
)->contents
[i
], Qt
))
1809 /* Create a new pane. */
1810 Lisp_Object pane_name
, prefix
;
1815 pane_name
= XVECTOR (menu_items
)->contents
[i
+ MENU_ITEMS_PANE_NAME
];
1816 prefix
= XVECTOR (menu_items
)->contents
[i
+ MENU_ITEMS_PANE_PREFIX
];
1818 #ifndef HAVE_MULTILINGUAL_MENU
1819 if (STRINGP (pane_name
) && STRING_MULTIBYTE (pane_name
))
1821 pane_name
= ENCODE_MENU_STRING (pane_name
);
1822 AREF (menu_items
, i
+ MENU_ITEMS_PANE_NAME
) = pane_name
;
1825 pane_string
= (NILP (pane_name
)
1826 ? "" : (char *) SDATA (pane_name
));
1827 /* If there is just one top-level pane, put all its items directly
1828 under the top-level menu. */
1829 if (menu_items_n_panes
== 1)
1832 /* If the pane has a meaningful name,
1833 make the pane a top-level menu item
1834 with its items as a submenu beneath it. */
1835 if (strcmp (pane_string
, ""))
1837 wv
= xmalloc_widget_value ();
1841 first_wv
->contents
= wv
;
1842 wv
->lname
= pane_name
;
1843 /* Set value to 1 so update_submenu_strings can handle '@' */
1844 wv
->value
= (char *)1;
1846 wv
->button_type
= BUTTON_TYPE_NONE
;
1854 i
+= MENU_ITEMS_PANE_LENGTH
;
1858 /* Create a new item within current pane. */
1859 Lisp_Object item_name
, enable
, descrip
, def
, type
, selected
;
1862 /* All items should be contained in panes. */
1863 if (panes_seen
== 0)
1866 item_name
= AREF (menu_items
, i
+ MENU_ITEMS_ITEM_NAME
);
1867 enable
= AREF (menu_items
, i
+ MENU_ITEMS_ITEM_ENABLE
);
1868 descrip
= AREF (menu_items
, i
+ MENU_ITEMS_ITEM_EQUIV_KEY
);
1869 def
= AREF (menu_items
, i
+ MENU_ITEMS_ITEM_DEFINITION
);
1870 type
= AREF (menu_items
, i
+ MENU_ITEMS_ITEM_TYPE
);
1871 selected
= AREF (menu_items
, i
+ MENU_ITEMS_ITEM_SELECTED
);
1872 help
= AREF (menu_items
, i
+ MENU_ITEMS_ITEM_HELP
);
1874 #ifndef HAVE_MULTILINGUAL_MENU
1875 if (STRING_MULTIBYTE (item_name
))
1877 item_name
= ENCODE_MENU_STRING (item_name
);
1878 AREF (menu_items
, i
+ MENU_ITEMS_ITEM_NAME
) = item_name
;
1881 if (STRINGP (descrip
) && STRING_MULTIBYTE (descrip
))
1883 descrip
= ENCODE_MENU_STRING (descrip
);
1884 AREF (menu_items
, i
+ MENU_ITEMS_ITEM_EQUIV_KEY
) = descrip
;
1886 #endif /* not HAVE_MULTILINGUAL_MENU */
1888 wv
= xmalloc_widget_value ();
1892 save_wv
->contents
= wv
;
1894 wv
->lname
= item_name
;
1895 if (!NILP (descrip
))
1898 /* The EMACS_INT cast avoids a warning. There's no problem
1899 as long as pointers have enough bits to hold small integers. */
1900 wv
->call_data
= (!NILP (def
) ? (void *) (EMACS_INT
) i
: 0);
1901 wv
->enabled
= !NILP (enable
);
1904 wv
->button_type
= BUTTON_TYPE_NONE
;
1905 else if (EQ (type
, QCradio
))
1906 wv
->button_type
= BUTTON_TYPE_RADIO
;
1907 else if (EQ (type
, QCtoggle
))
1908 wv
->button_type
= BUTTON_TYPE_TOGGLE
;
1912 wv
->selected
= !NILP (selected
);
1913 if (! STRINGP (help
))
1920 i
+= MENU_ITEMS_ITEM_LENGTH
;
1924 /* If we have just one "menu item"
1925 that was originally a button, return it by itself. */
1926 if (top_level_items
&& first_wv
->contents
&& first_wv
->contents
->next
== 0)
1928 wv
= first_wv
->contents
;
1929 free_widget_value (first_wv
);
1936 /* Walk through the widget_value tree starting at FIRST_WV and update
1937 the char * pointers from the corresponding lisp values.
1938 We do this after building the whole tree, since GC may happen while the
1939 tree is constructed, and small strings are relocated. So we must wait
1940 until no GC can happen before storing pointers into lisp values. */
1942 update_submenu_strings (first_wv
)
1943 widget_value
*first_wv
;
1947 for (wv
= first_wv
; wv
; wv
= wv
->next
)
1949 if (STRINGP (wv
->lname
))
1951 wv
->name
= (char *) SDATA (wv
->lname
);
1953 /* Ignore the @ that means "separate pane".
1954 This is a kludge, but this isn't worth more time. */
1955 if (wv
->value
== (char *)1)
1957 if (wv
->name
[0] == '@')
1963 if (STRINGP (wv
->lkey
))
1964 wv
->key
= (char *) SDATA (wv
->lkey
);
1967 update_submenu_strings (wv
->contents
);
1972 /* Recompute all the widgets of frame F, when the menu bar has been
1973 changed. Value is non-zero if widgets were updated. */
1976 update_frame_menubar (f
)
1980 return xg_update_frame_menubar (f
);
1982 struct x_output
*x
= f
->output_data
.x
;
1985 if (!x
->menubar_widget
|| XtIsManaged (x
->menubar_widget
))
1989 /* Save the size of the frame because the pane widget doesn't accept
1990 to resize itself. So force it. */
1991 columns
= FRAME_COLS (f
);
1992 rows
= FRAME_LINES (f
);
1994 /* Do the voodoo which means "I'm changing lots of things, don't try
1995 to refigure sizes until I'm done." */
1996 lw_refigure_widget (x
->column_widget
, False
);
1998 /* The order in which children are managed is the top to bottom
1999 order in which they are displayed in the paned window. First,
2000 remove the text-area widget. */
2001 XtUnmanageChild (x
->edit_widget
);
2003 /* Remove the menubar that is there now, and put up the menubar that
2005 XtManageChild (x
->menubar_widget
);
2006 XtMapWidget (x
->menubar_widget
);
2007 XtVaSetValues (x
->menubar_widget
, XtNmappedWhenManaged
, 1, NULL
);
2009 /* Re-manage the text-area widget, and then thrash the sizes. */
2010 XtManageChild (x
->edit_widget
);
2011 lw_refigure_widget (x
->column_widget
, True
);
2013 /* Force the pane widget to resize itself with the right values. */
2014 EmacsFrameSetCharSize (x
->edit_widget
, columns
, rows
);
2020 /* Set the contents of the menubar widgets of frame F.
2021 The argument FIRST_TIME is currently ignored;
2022 it is set the first time this is called, from initialize_frame_menubar. */
2025 set_frame_menubar (f
, first_time
, deep_p
)
2030 xt_or_gtk_widget menubar_widget
= f
->output_data
.x
->menubar_widget
;
2031 #ifdef USE_X_TOOLKIT
2035 widget_value
*wv
, *first_wv
, *prev_wv
= 0;
2037 int *submenu_start
, *submenu_end
;
2038 int *submenu_top_level_items
, *submenu_n_panes
;
2041 XSETFRAME (Vmenu_updating_frame
, f
);
2043 #ifdef USE_X_TOOLKIT
2044 if (f
->output_data
.x
->id
== 0)
2045 f
->output_data
.x
->id
= next_menubar_widget_id
++;
2046 id
= f
->output_data
.x
->id
;
2049 if (! menubar_widget
)
2051 else if (pending_menu_activation
&& !deep_p
)
2053 /* Make the first call for any given frame always go deep. */
2054 else if (!f
->output_data
.x
->saved_menu_event
&& !deep_p
)
2057 f
->output_data
.x
->saved_menu_event
= (XEvent
*)xmalloc (sizeof (XEvent
));
2058 f
->output_data
.x
->saved_menu_event
->type
= 0;
2062 /* If we have detached menus, we must update deep so detached menus
2063 also gets updated. */
2064 deep_p
= deep_p
|| xg_have_tear_offs ();
2069 /* Make a widget-value tree representing the entire menu trees. */
2071 struct buffer
*prev
= current_buffer
;
2073 int specpdl_count
= SPECPDL_INDEX ();
2074 int previous_menu_items_used
= f
->menu_bar_items_used
;
2075 Lisp_Object
*previous_items
2076 = (Lisp_Object
*) alloca (previous_menu_items_used
2077 * sizeof (Lisp_Object
));
2079 /* If we are making a new widget, its contents are empty,
2080 do always reinitialize them. */
2081 if (! menubar_widget
)
2082 previous_menu_items_used
= 0;
2084 buffer
= XWINDOW (FRAME_SELECTED_WINDOW (f
))->buffer
;
2085 specbind (Qinhibit_quit
, Qt
);
2086 /* Don't let the debugger step into this code
2087 because it is not reentrant. */
2088 specbind (Qdebug_on_next_call
, Qnil
);
2090 record_unwind_save_match_data ();
2091 if (NILP (Voverriding_local_map_menu_flag
))
2093 specbind (Qoverriding_terminal_local_map
, Qnil
);
2094 specbind (Qoverriding_local_map
, Qnil
);
2097 set_buffer_internal_1 (XBUFFER (buffer
));
2099 /* Run the Lucid hook. */
2100 safe_run_hooks (Qactivate_menubar_hook
);
2102 /* If it has changed current-menubar from previous value,
2103 really recompute the menubar from the value. */
2104 if (! NILP (Vlucid_menu_bar_dirty_flag
))
2105 call0 (Qrecompute_lucid_menubar
);
2106 safe_run_hooks (Qmenu_bar_update_hook
);
2107 FRAME_MENU_BAR_ITEMS (f
) = menu_bar_items (FRAME_MENU_BAR_ITEMS (f
));
2109 items
= FRAME_MENU_BAR_ITEMS (f
);
2111 /* Save the frame's previous menu bar contents data. */
2112 if (previous_menu_items_used
)
2113 bcopy (XVECTOR (f
->menu_bar_vector
)->contents
, previous_items
,
2114 previous_menu_items_used
* sizeof (Lisp_Object
));
2116 /* Fill in menu_items with the current menu bar contents.
2117 This can evaluate Lisp code. */
2120 menu_items
= f
->menu_bar_vector
;
2121 menu_items_allocated
= VECTORP (menu_items
) ? ASIZE (menu_items
) : 0;
2122 submenu_start
= (int *) alloca (XVECTOR (items
)->size
* sizeof (int *));
2123 submenu_end
= (int *) alloca (XVECTOR (items
)->size
* sizeof (int *));
2124 submenu_n_panes
= (int *) alloca (XVECTOR (items
)->size
* sizeof (int));
2125 submenu_top_level_items
2126 = (int *) alloca (XVECTOR (items
)->size
* sizeof (int *));
2128 for (i
= 0; i
< XVECTOR (items
)->size
; i
+= 4)
2130 Lisp_Object key
, string
, maps
;
2134 key
= XVECTOR (items
)->contents
[i
];
2135 string
= XVECTOR (items
)->contents
[i
+ 1];
2136 maps
= XVECTOR (items
)->contents
[i
+ 2];
2140 submenu_start
[i
] = menu_items_used
;
2142 menu_items_n_panes
= 0;
2143 submenu_top_level_items
[i
]
2144 = parse_single_submenu (key
, string
, maps
);
2145 submenu_n_panes
[i
] = menu_items_n_panes
;
2147 submenu_end
[i
] = menu_items_used
;
2150 finish_menu_items ();
2152 /* Convert menu_items into widget_value trees
2153 to display the menu. This cannot evaluate Lisp code. */
2155 wv
= xmalloc_widget_value ();
2156 wv
->name
= "menubar";
2159 wv
->button_type
= BUTTON_TYPE_NONE
;
2163 for (i
= 0; i
< last_i
; i
+= 4)
2165 menu_items_n_panes
= submenu_n_panes
[i
];
2166 wv
= digest_single_submenu (submenu_start
[i
], submenu_end
[i
],
2167 submenu_top_level_items
[i
]);
2171 first_wv
->contents
= wv
;
2172 /* Don't set wv->name here; GC during the loop might relocate it. */
2174 wv
->button_type
= BUTTON_TYPE_NONE
;
2178 set_buffer_internal_1 (prev
);
2180 /* If there has been no change in the Lisp-level contents
2181 of the menu bar, skip redisplaying it. Just exit. */
2183 for (i
= 0; i
< previous_menu_items_used
; i
++)
2184 if (menu_items_used
== i
2185 || (!EQ (previous_items
[i
], XVECTOR (menu_items
)->contents
[i
])))
2187 if (i
== menu_items_used
&& i
== previous_menu_items_used
&& i
!= 0)
2189 free_menubar_widget_value_tree (first_wv
);
2190 discard_menu_items ();
2191 unbind_to (specpdl_count
, Qnil
);
2195 f
->menu_bar_vector
= menu_items
;
2196 f
->menu_bar_items_used
= menu_items_used
;
2198 unbind_to (specpdl_count
, Qnil
);
2200 /* Now GC cannot happen during the lifetime of the widget_value,
2201 so it's safe to store data from a Lisp_String. */
2202 wv
= first_wv
->contents
;
2203 for (i
= 0; i
< XVECTOR (items
)->size
; i
+= 4)
2206 string
= XVECTOR (items
)->contents
[i
+ 1];
2209 wv
->name
= (char *) SDATA (string
);
2210 update_submenu_strings (wv
->contents
);
2217 /* Make a widget-value tree containing
2218 just the top level menu bar strings. */
2220 wv
= xmalloc_widget_value ();
2221 wv
->name
= "menubar";
2224 wv
->button_type
= BUTTON_TYPE_NONE
;
2228 items
= FRAME_MENU_BAR_ITEMS (f
);
2229 for (i
= 0; i
< XVECTOR (items
)->size
; i
+= 4)
2233 string
= XVECTOR (items
)->contents
[i
+ 1];
2237 wv
= xmalloc_widget_value ();
2238 wv
->name
= (char *) SDATA (string
);
2241 wv
->button_type
= BUTTON_TYPE_NONE
;
2243 /* This prevents lwlib from assuming this
2244 menu item is really supposed to be empty. */
2245 /* The EMACS_INT cast avoids a warning.
2246 This value just has to be different from small integers. */
2247 wv
->call_data
= (void *) (EMACS_INT
) (-1);
2252 first_wv
->contents
= wv
;
2256 /* Forget what we thought we knew about what is in the
2257 detailed contents of the menu bar menus.
2258 Changing the top level always destroys the contents. */
2259 f
->menu_bar_items_used
= 0;
2262 /* Create or update the menu bar widget. */
2267 xg_crazy_callback_abort
= 1;
2270 /* The fourth arg is DEEP_P, which says to consider the entire
2271 menu trees we supply, rather than just the menu bar item names. */
2272 xg_modify_menubar_widgets (menubar_widget
,
2276 G_CALLBACK (menubar_selection_callback
),
2277 G_CALLBACK (popup_deactivate_callback
),
2278 G_CALLBACK (menu_highlight_callback
));
2282 GtkWidget
*wvbox
= f
->output_data
.x
->vbox_widget
;
2285 = xg_create_widget ("menubar", "menubar", f
, first_wv
,
2286 G_CALLBACK (menubar_selection_callback
),
2287 G_CALLBACK (popup_deactivate_callback
),
2288 G_CALLBACK (menu_highlight_callback
));
2290 f
->output_data
.x
->menubar_widget
= menubar_widget
;
2294 #else /* not USE_GTK */
2297 /* Disable resizing (done for Motif!) */
2298 lw_allow_resizing (f
->output_data
.x
->widget
, False
);
2300 /* The third arg is DEEP_P, which says to consider the entire
2301 menu trees we supply, rather than just the menu bar item names. */
2302 lw_modify_all_widgets (id
, first_wv
, deep_p
);
2304 /* Re-enable the edit widget to resize. */
2305 lw_allow_resizing (f
->output_data
.x
->widget
, True
);
2309 char menuOverride
[] = "Ctrl<KeyPress>g: MenuGadgetEscape()";
2310 XtTranslations override
= XtParseTranslationTable (menuOverride
);
2312 menubar_widget
= lw_create_widget ("menubar", "menubar", id
, first_wv
,
2313 f
->output_data
.x
->column_widget
,
2315 popup_activate_callback
,
2316 menubar_selection_callback
,
2317 popup_deactivate_callback
,
2318 menu_highlight_callback
);
2319 f
->output_data
.x
->menubar_widget
= menubar_widget
;
2321 /* Make menu pop down on C-g. */
2322 XtOverrideTranslations (menubar_widget
, override
);
2327 = (f
->output_data
.x
->menubar_widget
2328 ? (f
->output_data
.x
->menubar_widget
->core
.height
2329 + f
->output_data
.x
->menubar_widget
->core
.border_width
)
2332 #if 0 /* Experimentally, we now get the right results
2333 for -geometry -0-0 without this. 24 Aug 96, rms. */
2335 if (FRAME_EXTERNAL_MENU_BAR (f
))
2338 XtVaGetValues (f
->output_data
.x
->column_widget
,
2339 XtNinternalBorderWidth
, &ibw
, NULL
);
2340 menubar_size
+= ibw
;
2342 #endif /* USE_LUCID */
2345 f
->output_data
.x
->menubar_height
= menubar_size
;
2347 #endif /* not USE_GTK */
2349 free_menubar_widget_value_tree (first_wv
);
2350 update_frame_menubar (f
);
2353 xg_crazy_callback_abort
= 0;
2359 /* Called from Fx_create_frame to create the initial menubar of a frame
2360 before it is mapped, so that the window is mapped with the menubar already
2361 there instead of us tacking it on later and thrashing the window after it
2365 initialize_frame_menubar (f
)
2368 /* This function is called before the first chance to redisplay
2369 the frame. It has to be, so the frame will have the right size. */
2370 FRAME_MENU_BAR_ITEMS (f
) = menu_bar_items (FRAME_MENU_BAR_ITEMS (f
));
2371 set_frame_menubar (f
, 1, 1);
2375 /* Get rid of the menu bar of frame F, and free its storage.
2376 This is used when deleting a frame, and when turning off the menu bar.
2377 For GTK this function is in gtkutil.c. */
2381 free_frame_menubar (f
)
2384 Widget menubar_widget
;
2386 menubar_widget
= f
->output_data
.x
->menubar_widget
;
2388 f
->output_data
.x
->menubar_height
= 0;
2393 /* Removing the menu bar magically changes the shell widget's x
2394 and y position of (0, 0) which, when the menu bar is turned
2395 on again, leads to pull-down menuss appearing in strange
2396 positions near the upper-left corner of the display. This
2397 happens only with some window managers like twm and ctwm,
2398 but not with other like Motif's mwm or kwm, because the
2399 latter generate ConfigureNotify events when the menu bar
2400 is switched off, which fixes the shell position. */
2401 Position x0
, y0
, x1
, y1
;
2407 if (f
->output_data
.x
->widget
)
2408 XtVaGetValues (f
->output_data
.x
->widget
, XtNx
, &x0
, XtNy
, &y0
, NULL
);
2411 lw_destroy_all_widgets ((LWLIB_ID
) f
->output_data
.x
->id
);
2412 f
->output_data
.x
->menubar_widget
= NULL
;
2415 if (f
->output_data
.x
->widget
)
2417 XtVaGetValues (f
->output_data
.x
->widget
, XtNx
, &x1
, XtNy
, &y1
, NULL
);
2418 if (x1
== 0 && y1
== 0)
2419 XtVaSetValues (f
->output_data
.x
->widget
, XtNx
, x0
, XtNy
, y0
, NULL
);
2426 #endif /* not USE_GTK */
2428 #endif /* USE_X_TOOLKIT || USE_GTK */
2430 /* xmenu_show actually displays a menu using the panes and items in menu_items
2431 and returns the value selected from it.
2432 There are two versions of xmenu_show, one for Xt and one for Xlib.
2433 Both assume input is blocked by the caller. */
2435 /* F is the frame the menu is for.
2436 X and Y are the frame-relative specified position,
2437 relative to the inside upper left corner of the frame F.
2438 FOR_CLICK is nonzero if this menu was invoked for a mouse click.
2439 KEYMAPS is 1 if this menu was specified with keymaps;
2440 in that case, we return a list containing the chosen item's value
2441 and perhaps also the pane's prefix.
2442 TITLE is the specified menu title.
2443 ERROR is a place to store an error message string in case of failure.
2444 (We return nil on failure, but the value doesn't actually matter.) */
2446 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
2448 /* The item selected in the popup menu. */
2449 static Lisp_Object
*volatile menu_item_selection
;
2453 /* Used when position a popup menu. See menu_position_func and
2454 create_and_show_popup_menu below. */
2455 struct next_popup_x_y
2462 /* The menu position function to use if we are not putting a popup
2463 menu where the pointer is.
2464 MENU is the menu to pop up.
2465 X and Y shall on exit contain x/y where the menu shall pop up.
2466 PUSH_IN is not documented in the GTK manual.
2467 USER_DATA is any data passed in when calling gtk_menu_popup.
2468 Here it points to a struct next_popup_x_y where the coordinates
2469 to store in *X and *Y are as well as the frame for the popup.
2471 Here only X and Y are used. */
2473 menu_position_func (menu
, x
, y
, push_in
, user_data
)
2480 struct next_popup_x_y
* data
= (struct next_popup_x_y
*)user_data
;
2482 int disp_width
= FRAME_X_DISPLAY_INFO (data
->f
)->width
;
2483 int disp_height
= FRAME_X_DISPLAY_INFO (data
->f
)->height
;
2488 /* Check if there is room for the menu. If not, adjust x/y so that
2489 the menu is fully visible. */
2490 gtk_widget_size_request (GTK_WIDGET (menu
), &req
);
2491 if (data
->x
+ req
.width
> disp_width
)
2492 *x
-= data
->x
+ req
.width
- disp_width
;
2493 if (data
->y
+ req
.height
> disp_height
)
2494 *y
-= data
->y
+ req
.height
- disp_height
;
2498 popup_selection_callback (widget
, client_data
)
2500 gpointer client_data
;
2502 xg_menu_item_cb_data
*cb_data
= (xg_menu_item_cb_data
*) client_data
;
2504 if (xg_crazy_callback_abort
) return;
2505 if (cb_data
) menu_item_selection
= (Lisp_Object
*) cb_data
->call_data
;
2512 struct Lisp_Save_Value
*p
= XSAVE_VALUE (arg
);
2514 popup_activated_flag
= 0;
2516 gtk_widget_destroy (GTK_WIDGET (p
->pointer
));
2521 /* Pop up the menu for frame F defined by FIRST_WV at X/Y and loop until the
2523 menu_item_selection will be set to the selection. */
2525 create_and_show_popup_menu (f
, first_wv
, x
, y
, for_click
)
2527 widget_value
*first_wv
;
2534 GtkMenuPositionFunc pos_func
= 0; /* Pop up at pointer. */
2535 struct next_popup_x_y popup_x_y
;
2536 int specpdl_count
= SPECPDL_INDEX ();
2538 xg_crazy_callback_abort
= 1;
2539 menu
= xg_create_widget ("popup", first_wv
->name
, f
, first_wv
,
2540 G_CALLBACK (popup_selection_callback
),
2541 G_CALLBACK (popup_deactivate_callback
),
2542 G_CALLBACK (menu_highlight_callback
));
2543 xg_crazy_callback_abort
= 0;
2547 /* Not invoked by a click. pop up at x/y. */
2548 pos_func
= menu_position_func
;
2550 /* Adjust coordinates to be root-window-relative. */
2551 x
+= f
->left_pos
+ FRAME_OUTER_TO_INNER_DIFF_X (f
);
2552 y
+= f
->top_pos
+ FRAME_OUTER_TO_INNER_DIFF_Y (f
);
2558 i
= 0; /* gtk_menu_popup needs this to be 0 for a non-button popup. */
2562 for (i
= 0; i
< 5; i
++)
2563 if (FRAME_X_DISPLAY_INFO (f
)->grabbed
& (1 << i
))
2567 /* Display the menu. */
2568 gtk_widget_show_all (menu
);
2569 gtk_menu_popup (GTK_MENU (menu
), 0, 0, pos_func
, &popup_x_y
, i
, 0);
2571 record_unwind_protect (pop_down_menu
, make_save_value (menu
, 0));
2573 if (GTK_WIDGET_MAPPED (menu
))
2575 /* Set this to one. popup_widget_loop increases it by one, so it becomes
2576 two. show_help_echo uses this to detect popup menus. */
2577 popup_activated_flag
= 1;
2578 /* Process events that apply to the menu. */
2579 popup_widget_loop (1, menu
);
2582 unbind_to (specpdl_count
, Qnil
);
2584 /* Must reset this manually because the button release event is not passed
2585 to Emacs event loop. */
2586 FRAME_X_DISPLAY_INFO (f
)->grabbed
= 0;
2589 #else /* not USE_GTK */
2591 /* We need a unique id for each widget handled by the Lucid Widget
2594 For the main windows, and popup menus, we use this counter,
2595 which we increment each time after use. This starts from 1<<16.
2597 For menu bars, we use numbers starting at 0, counted in
2598 next_menubar_widget_id. */
2599 LWLIB_ID widget_id_tick
;
2602 popup_selection_callback (widget
, id
, client_data
)
2605 XtPointer client_data
;
2607 menu_item_selection
= (Lisp_Object
*) client_data
;
2610 /* ARG is the LWLIB ID of the dialog box, represented
2611 as a Lisp object as (HIGHPART . LOWPART). */
2617 LWLIB_ID id
= (XINT (XCAR (arg
)) << 4 * sizeof (LWLIB_ID
)
2618 | XINT (XCDR (arg
)));
2621 lw_destroy_all_widgets (id
);
2623 popup_activated_flag
= 0;
2628 /* Pop up the menu for frame F defined by FIRST_WV at X/Y and loop until the
2630 menu_item_selection will be set to the selection. */
2632 create_and_show_popup_menu (f
, first_wv
, x
, y
, for_click
)
2634 widget_value
*first_wv
;
2642 XButtonPressedEvent dummy
;
2646 menu_id
= widget_id_tick
++;
2647 menu
= lw_create_widget ("popup", first_wv
->name
, menu_id
, first_wv
,
2648 f
->output_data
.x
->widget
, 1, 0,
2649 popup_selection_callback
,
2650 popup_deactivate_callback
,
2651 menu_highlight_callback
);
2653 dummy
.type
= ButtonPress
;
2655 dummy
.send_event
= 0;
2656 dummy
.display
= FRAME_X_DISPLAY (f
);
2657 dummy
.time
= CurrentTime
;
2658 dummy
.root
= FRAME_X_DISPLAY_INFO (f
)->root_window
;
2659 dummy
.window
= dummy
.root
;
2660 dummy
.subwindow
= dummy
.root
;
2664 /* Adjust coordinates to be root-window-relative. */
2665 x
+= f
->left_pos
+ FRAME_OUTER_TO_INNER_DIFF_X (f
);
2666 y
+= f
->top_pos
+ FRAME_OUTER_TO_INNER_DIFF_Y (f
);
2673 for (i
= 0; i
< 5; i
++)
2674 if (FRAME_X_DISPLAY_INFO (f
)->grabbed
& (1 << i
))
2677 /* Don't allow any geometry request from the user. */
2678 XtSetArg (av
[ac
], XtNgeometry
, 0); ac
++;
2679 XtSetValues (menu
, av
, ac
);
2681 /* Display the menu. */
2682 lw_popup_menu (menu
, (XEvent
*) &dummy
);
2683 popup_activated_flag
= 1;
2686 int fact
= 4 * sizeof (LWLIB_ID
);
2687 int specpdl_count
= SPECPDL_INDEX ();
2688 record_unwind_protect (pop_down_menu
,
2689 Fcons (make_number (menu_id
>> (fact
)),
2690 make_number (menu_id
& ~(-1 << (fact
)))));
2692 /* Process events that apply to the menu. */
2693 popup_get_selection ((XEvent
*) 0, FRAME_X_DISPLAY_INFO (f
), menu_id
, 1);
2695 unbind_to (specpdl_count
, Qnil
);
2699 #endif /* not USE_GTK */
2702 xmenu_show (f
, x
, y
, for_click
, keymaps
, title
, error
)
2712 widget_value
*wv
, *save_wv
= 0, *first_wv
= 0, *prev_wv
= 0;
2713 widget_value
**submenu_stack
2714 = (widget_value
**) alloca (menu_items_used
* sizeof (widget_value
*));
2715 Lisp_Object
*subprefix_stack
2716 = (Lisp_Object
*) alloca (menu_items_used
* sizeof (Lisp_Object
));
2717 int submenu_depth
= 0;
2723 if (menu_items_used
<= MENU_ITEMS_PANE_LENGTH
)
2725 *error
= "Empty menu";
2729 /* Create a tree of widget_value objects
2730 representing the panes and their items. */
2731 wv
= xmalloc_widget_value ();
2735 wv
->button_type
= BUTTON_TYPE_NONE
;
2740 /* Loop over all panes and items, filling in the tree. */
2742 while (i
< menu_items_used
)
2744 if (EQ (XVECTOR (menu_items
)->contents
[i
], Qnil
))
2746 submenu_stack
[submenu_depth
++] = save_wv
;
2752 else if (EQ (XVECTOR (menu_items
)->contents
[i
], Qlambda
))
2755 save_wv
= submenu_stack
[--submenu_depth
];
2759 else if (EQ (XVECTOR (menu_items
)->contents
[i
], Qt
)
2760 && submenu_depth
!= 0)
2761 i
+= MENU_ITEMS_PANE_LENGTH
;
2762 /* Ignore a nil in the item list.
2763 It's meaningful only for dialog boxes. */
2764 else if (EQ (XVECTOR (menu_items
)->contents
[i
], Qquote
))
2766 else if (EQ (XVECTOR (menu_items
)->contents
[i
], Qt
))
2768 /* Create a new pane. */
2769 Lisp_Object pane_name
, prefix
;
2772 pane_name
= AREF (menu_items
, i
+ MENU_ITEMS_PANE_NAME
);
2773 prefix
= AREF (menu_items
, i
+ MENU_ITEMS_PANE_PREFIX
);
2775 #ifndef HAVE_MULTILINGUAL_MENU
2776 if (STRINGP (pane_name
) && STRING_MULTIBYTE (pane_name
))
2778 pane_name
= ENCODE_MENU_STRING (pane_name
);
2779 AREF (menu_items
, i
+ MENU_ITEMS_PANE_NAME
) = pane_name
;
2782 pane_string
= (NILP (pane_name
)
2783 ? "" : (char *) SDATA (pane_name
));
2784 /* If there is just one top-level pane, put all its items directly
2785 under the top-level menu. */
2786 if (menu_items_n_panes
== 1)
2789 /* If the pane has a meaningful name,
2790 make the pane a top-level menu item
2791 with its items as a submenu beneath it. */
2792 if (!keymaps
&& strcmp (pane_string
, ""))
2794 wv
= xmalloc_widget_value ();
2798 first_wv
->contents
= wv
;
2799 wv
->name
= pane_string
;
2800 if (keymaps
&& !NILP (prefix
))
2804 wv
->button_type
= BUTTON_TYPE_NONE
;
2809 else if (first_pane
)
2815 i
+= MENU_ITEMS_PANE_LENGTH
;
2819 /* Create a new item within current pane. */
2820 Lisp_Object item_name
, enable
, descrip
, def
, type
, selected
, help
;
2821 item_name
= AREF (menu_items
, i
+ MENU_ITEMS_ITEM_NAME
);
2822 enable
= AREF (menu_items
, i
+ MENU_ITEMS_ITEM_ENABLE
);
2823 descrip
= AREF (menu_items
, i
+ MENU_ITEMS_ITEM_EQUIV_KEY
);
2824 def
= AREF (menu_items
, i
+ MENU_ITEMS_ITEM_DEFINITION
);
2825 type
= AREF (menu_items
, i
+ MENU_ITEMS_ITEM_TYPE
);
2826 selected
= AREF (menu_items
, i
+ MENU_ITEMS_ITEM_SELECTED
);
2827 help
= AREF (menu_items
, i
+ MENU_ITEMS_ITEM_HELP
);
2829 #ifndef HAVE_MULTILINGUAL_MENU
2830 if (STRINGP (item_name
) && STRING_MULTIBYTE (item_name
))
2832 item_name
= ENCODE_MENU_STRING (item_name
);
2833 AREF (menu_items
, i
+ MENU_ITEMS_ITEM_NAME
) = item_name
;
2836 if (STRINGP (descrip
) && STRING_MULTIBYTE (descrip
))
2838 descrip
= ENCODE_MENU_STRING (descrip
);
2839 AREF (menu_items
, i
+ MENU_ITEMS_ITEM_EQUIV_KEY
) = descrip
;
2841 #endif /* not HAVE_MULTILINGUAL_MENU */
2843 wv
= xmalloc_widget_value ();
2847 save_wv
->contents
= wv
;
2848 wv
->name
= (char *) SDATA (item_name
);
2849 if (!NILP (descrip
))
2850 wv
->key
= (char *) SDATA (descrip
);
2852 /* If this item has a null value,
2853 make the call_data null so that it won't display a box
2854 when the mouse is on it. */
2856 = (!NILP (def
) ? (void *) &XVECTOR (menu_items
)->contents
[i
] : 0);
2857 wv
->enabled
= !NILP (enable
);
2860 wv
->button_type
= BUTTON_TYPE_NONE
;
2861 else if (EQ (type
, QCtoggle
))
2862 wv
->button_type
= BUTTON_TYPE_TOGGLE
;
2863 else if (EQ (type
, QCradio
))
2864 wv
->button_type
= BUTTON_TYPE_RADIO
;
2868 wv
->selected
= !NILP (selected
);
2870 if (! STRINGP (help
))
2877 i
+= MENU_ITEMS_ITEM_LENGTH
;
2881 /* Deal with the title, if it is non-nil. */
2884 widget_value
*wv_title
= xmalloc_widget_value ();
2885 widget_value
*wv_sep1
= xmalloc_widget_value ();
2886 widget_value
*wv_sep2
= xmalloc_widget_value ();
2888 wv_sep2
->name
= "--";
2889 wv_sep2
->next
= first_wv
->contents
;
2890 wv_sep2
->help
= Qnil
;
2892 wv_sep1
->name
= "--";
2893 wv_sep1
->next
= wv_sep2
;
2894 wv_sep1
->help
= Qnil
;
2896 #ifndef HAVE_MULTILINGUAL_MENU
2897 if (STRING_MULTIBYTE (title
))
2898 title
= ENCODE_MENU_STRING (title
);
2901 wv_title
->name
= (char *) SDATA (title
);
2902 wv_title
->enabled
= TRUE
;
2903 wv_title
->button_type
= BUTTON_TYPE_NONE
;
2904 wv_title
->next
= wv_sep1
;
2905 wv_title
->help
= Qnil
;
2906 first_wv
->contents
= wv_title
;
2909 /* No selection has been chosen yet. */
2910 menu_item_selection
= 0;
2912 /* Actually create and show the menu until popped down. */
2913 create_and_show_popup_menu (f
, first_wv
, x
, y
, for_click
);
2915 /* Free the widget_value objects we used to specify the contents. */
2916 free_menubar_widget_value_tree (first_wv
);
2918 /* Find the selected item, and its pane, to return
2919 the proper value. */
2920 if (menu_item_selection
!= 0)
2922 Lisp_Object prefix
, entry
;
2924 prefix
= entry
= Qnil
;
2926 while (i
< menu_items_used
)
2928 if (EQ (XVECTOR (menu_items
)->contents
[i
], Qnil
))
2930 subprefix_stack
[submenu_depth
++] = prefix
;
2934 else if (EQ (XVECTOR (menu_items
)->contents
[i
], Qlambda
))
2936 prefix
= subprefix_stack
[--submenu_depth
];
2939 else if (EQ (XVECTOR (menu_items
)->contents
[i
], Qt
))
2942 = XVECTOR (menu_items
)->contents
[i
+ MENU_ITEMS_PANE_PREFIX
];
2943 i
+= MENU_ITEMS_PANE_LENGTH
;
2945 /* Ignore a nil in the item list.
2946 It's meaningful only for dialog boxes. */
2947 else if (EQ (XVECTOR (menu_items
)->contents
[i
], Qquote
))
2952 = XVECTOR (menu_items
)->contents
[i
+ MENU_ITEMS_ITEM_VALUE
];
2953 if (menu_item_selection
== &XVECTOR (menu_items
)->contents
[i
])
2959 entry
= Fcons (entry
, Qnil
);
2961 entry
= Fcons (prefix
, entry
);
2962 for (j
= submenu_depth
- 1; j
>= 0; j
--)
2963 if (!NILP (subprefix_stack
[j
]))
2964 entry
= Fcons (subprefix_stack
[j
], entry
);
2968 i
+= MENU_ITEMS_ITEM_LENGTH
;
2972 else if (!for_click
)
2973 /* Make "Cancel" equivalent to C-g. */
2974 Fsignal (Qquit
, Qnil
);
2981 dialog_selection_callback (widget
, client_data
)
2983 gpointer client_data
;
2985 /* The EMACS_INT cast avoids a warning. There's no problem
2986 as long as pointers have enough bits to hold small integers. */
2987 if ((int) (EMACS_INT
) client_data
!= -1)
2988 menu_item_selection
= (Lisp_Object
*) client_data
;
2990 popup_activated_flag
= 0;
2993 /* Pop up the dialog for frame F defined by FIRST_WV and loop until the
2995 menu_item_selection will be set to the selection. */
2997 create_and_show_dialog (f
, first_wv
)
2999 widget_value
*first_wv
;
3003 menu
= xg_create_widget ("dialog", first_wv
->name
, f
, first_wv
,
3004 G_CALLBACK (dialog_selection_callback
),
3005 G_CALLBACK (popup_deactivate_callback
),
3010 int specpdl_count
= SPECPDL_INDEX ();
3011 record_unwind_protect (pop_down_menu
, make_save_value (menu
, 0));
3013 /* Display the menu. */
3014 gtk_widget_show_all (menu
);
3016 /* Process events that apply to the menu. */
3017 popup_widget_loop (1, menu
);
3019 unbind_to (specpdl_count
, Qnil
);
3023 #else /* not USE_GTK */
3025 dialog_selection_callback (widget
, id
, client_data
)
3028 XtPointer client_data
;
3030 /* The EMACS_INT cast avoids a warning. There's no problem
3031 as long as pointers have enough bits to hold small integers. */
3032 if ((int) (EMACS_INT
) client_data
!= -1)
3033 menu_item_selection
= (Lisp_Object
*) client_data
;
3036 lw_destroy_all_widgets (id
);
3038 popup_activated_flag
= 0;
3042 /* Pop up the dialog for frame F defined by FIRST_WV and loop until the
3044 menu_item_selection will be set to the selection. */
3046 create_and_show_dialog (f
, first_wv
)
3048 widget_value
*first_wv
;
3052 dialog_id
= widget_id_tick
++;
3053 lw_create_widget (first_wv
->name
, "dialog", dialog_id
, first_wv
,
3054 f
->output_data
.x
->widget
, 1, 0,
3055 dialog_selection_callback
, 0, 0);
3056 lw_modify_all_widgets (dialog_id
, first_wv
->contents
, True
);
3058 /* Display the dialog box. */
3059 lw_pop_up_all_widgets (dialog_id
);
3060 popup_activated_flag
= 1;
3062 /* Process events that apply to the dialog box.
3063 Also handle timers. */
3065 int count
= SPECPDL_INDEX ();
3066 int fact
= 4 * sizeof (LWLIB_ID
);
3068 /* xdialog_show_unwind is responsible for popping the dialog box down. */
3069 record_unwind_protect (pop_down_menu
,
3070 Fcons (make_number (dialog_id
>> (fact
)),
3071 make_number (dialog_id
& ~(-1 << (fact
)))));
3073 popup_get_selection ((XEvent
*) 0, FRAME_X_DISPLAY_INFO (f
),
3076 unbind_to (count
, Qnil
);
3080 #endif /* not USE_GTK */
3082 static char * button_names
[] = {
3083 "button1", "button2", "button3", "button4", "button5",
3084 "button6", "button7", "button8", "button9", "button10" };
3087 xdialog_show (f
, keymaps
, title
, header
, error_name
)
3090 Lisp_Object title
, header
;
3093 int i
, nb_buttons
=0;
3094 char dialog_name
[6];
3096 widget_value
*wv
, *first_wv
= 0, *prev_wv
= 0;
3098 /* Number of elements seen so far, before boundary. */
3100 /* 1 means we've seen the boundary between left-hand elts and right-hand. */
3101 int boundary_seen
= 0;
3105 if (menu_items_n_panes
> 1)
3107 *error_name
= "Multiple panes in dialog box";
3111 /* Create a tree of widget_value objects
3112 representing the text label and buttons. */
3114 Lisp_Object pane_name
, prefix
;
3116 pane_name
= XVECTOR (menu_items
)->contents
[MENU_ITEMS_PANE_NAME
];
3117 prefix
= XVECTOR (menu_items
)->contents
[MENU_ITEMS_PANE_PREFIX
];
3118 pane_string
= (NILP (pane_name
)
3119 ? "" : (char *) SDATA (pane_name
));
3120 prev_wv
= xmalloc_widget_value ();
3121 prev_wv
->value
= pane_string
;
3122 if (keymaps
&& !NILP (prefix
))
3124 prev_wv
->enabled
= 1;
3125 prev_wv
->name
= "message";
3126 prev_wv
->help
= Qnil
;
3129 /* Loop over all panes and items, filling in the tree. */
3130 i
= MENU_ITEMS_PANE_LENGTH
;
3131 while (i
< menu_items_used
)
3134 /* Create a new item within current pane. */
3135 Lisp_Object item_name
, enable
, descrip
;
3136 item_name
= XVECTOR (menu_items
)->contents
[i
+ MENU_ITEMS_ITEM_NAME
];
3137 enable
= XVECTOR (menu_items
)->contents
[i
+ MENU_ITEMS_ITEM_ENABLE
];
3139 = XVECTOR (menu_items
)->contents
[i
+ MENU_ITEMS_ITEM_EQUIV_KEY
];
3141 if (NILP (item_name
))
3143 free_menubar_widget_value_tree (first_wv
);
3144 *error_name
= "Submenu in dialog items";
3147 if (EQ (item_name
, Qquote
))
3149 /* This is the boundary between left-side elts
3150 and right-side elts. Stop incrementing right_count. */
3155 if (nb_buttons
>= 9)
3157 free_menubar_widget_value_tree (first_wv
);
3158 *error_name
= "Too many dialog items";
3162 wv
= xmalloc_widget_value ();
3164 wv
->name
= (char *) button_names
[nb_buttons
];
3165 if (!NILP (descrip
))
3166 wv
->key
= (char *) SDATA (descrip
);
3167 wv
->value
= (char *) SDATA (item_name
);
3168 wv
->call_data
= (void *) &XVECTOR (menu_items
)->contents
[i
];
3169 wv
->enabled
= !NILP (enable
);
3173 if (! boundary_seen
)
3177 i
+= MENU_ITEMS_ITEM_LENGTH
;
3180 /* If the boundary was not specified,
3181 by default put half on the left and half on the right. */
3182 if (! boundary_seen
)
3183 left_count
= nb_buttons
- nb_buttons
/ 2;
3185 wv
= xmalloc_widget_value ();
3186 wv
->name
= dialog_name
;
3189 /* Frame title: 'Q' = Question, 'I' = Information.
3190 Can also have 'E' = Error if, one day, we want
3191 a popup for errors. */
3193 dialog_name
[0] = 'Q';
3195 dialog_name
[0] = 'I';
3197 /* Dialog boxes use a really stupid name encoding
3198 which specifies how many buttons to use
3199 and how many buttons are on the right. */
3200 dialog_name
[1] = '0' + nb_buttons
;
3201 dialog_name
[2] = 'B';
3202 dialog_name
[3] = 'R';
3203 /* Number of buttons to put on the right. */
3204 dialog_name
[4] = '0' + nb_buttons
- left_count
;
3206 wv
->contents
= first_wv
;
3210 /* No selection has been chosen yet. */
3211 menu_item_selection
= 0;
3213 /* Actually create and show the dialog. */
3214 create_and_show_dialog (f
, first_wv
);
3216 /* Free the widget_value objects we used to specify the contents. */
3217 free_menubar_widget_value_tree (first_wv
);
3219 /* Find the selected item, and its pane, to return
3220 the proper value. */
3221 if (menu_item_selection
!= 0)
3227 while (i
< menu_items_used
)
3231 if (EQ (XVECTOR (menu_items
)->contents
[i
], Qt
))
3234 = XVECTOR (menu_items
)->contents
[i
+ MENU_ITEMS_PANE_PREFIX
];
3235 i
+= MENU_ITEMS_PANE_LENGTH
;
3237 else if (EQ (XVECTOR (menu_items
)->contents
[i
], Qquote
))
3239 /* This is the boundary between left-side elts and
3246 = XVECTOR (menu_items
)->contents
[i
+ MENU_ITEMS_ITEM_VALUE
];
3247 if (menu_item_selection
== &XVECTOR (menu_items
)->contents
[i
])
3251 entry
= Fcons (entry
, Qnil
);
3253 entry
= Fcons (prefix
, entry
);
3257 i
+= MENU_ITEMS_ITEM_LENGTH
;
3262 /* Make "Cancel" equivalent to C-g. */
3263 Fsignal (Qquit
, Qnil
);
3268 #else /* not USE_X_TOOLKIT && not USE_GTK */
3270 /* The frame of the last activated non-toolkit menu bar.
3271 Used to generate menu help events. */
3273 static struct frame
*menu_help_frame
;
3276 /* Show help HELP_STRING, or clear help if HELP_STRING is null.
3278 PANE is the pane number, and ITEM is the menu item number in
3279 the menu (currently not used).
3281 This cannot be done with generating a HELP_EVENT because
3282 XMenuActivate contains a loop that doesn't let Emacs process
3286 menu_help_callback (help_string
, pane
, item
)
3290 extern Lisp_Object Qmenu_item
;
3291 Lisp_Object
*first_item
;
3292 Lisp_Object pane_name
;
3293 Lisp_Object menu_object
;
3295 first_item
= XVECTOR (menu_items
)->contents
;
3296 if (EQ (first_item
[0], Qt
))
3297 pane_name
= first_item
[MENU_ITEMS_PANE_NAME
];
3298 else if (EQ (first_item
[0], Qquote
))
3299 /* This shouldn't happen, see xmenu_show. */
3300 pane_name
= empty_string
;
3302 pane_name
= first_item
[MENU_ITEMS_ITEM_NAME
];
3304 /* (menu-item MENU-NAME PANE-NUMBER) */
3305 menu_object
= Fcons (Qmenu_item
,
3307 Fcons (make_number (pane
), Qnil
)));
3308 show_help_echo (help_string
? build_string (help_string
) : Qnil
,
3309 Qnil
, menu_object
, make_number (item
), 1);
3316 struct Lisp_Save_Value
*p1
= XSAVE_VALUE (Fcar (arg
));
3317 struct Lisp_Save_Value
*p2
= XSAVE_VALUE (Fcdr (arg
));
3319 FRAME_PTR f
= p1
->pointer
;
3320 XMenu
*menu
= p2
->pointer
;
3324 XUngrabPointer (FRAME_X_DISPLAY (f
), CurrentTime
);
3325 XUngrabKeyboard (FRAME_X_DISPLAY (f
), CurrentTime
);
3327 XMenuDestroy (FRAME_X_DISPLAY (f
), menu
);
3329 #ifdef HAVE_X_WINDOWS
3330 /* Assume the mouse has moved out of the X window.
3331 If it has actually moved in, we will get an EnterNotify. */
3332 x_mouse_leave (FRAME_X_DISPLAY_INFO (f
));
3334 /* State that no mouse buttons are now held.
3335 (The oldXMenu code doesn't track this info for us.)
3336 That is not necessarily true, but the fiction leads to reasonable
3337 results, and it is a pain to ask which are actually held now. */
3338 FRAME_X_DISPLAY_INFO (f
)->grabbed
= 0;
3340 #endif /* HAVE_X_WINDOWS */
3349 xmenu_show (f
, x
, y
, for_click
, keymaps
, title
, error
)
3359 int pane
, selidx
, lpane
, status
;
3360 Lisp_Object entry
, pane_prefix
;
3362 int ulx
, uly
, width
, height
;
3363 int dispwidth
, dispheight
;
3364 int i
, j
, lines
, maxlines
;
3367 unsigned int dummy_uint
;
3368 int specpdl_count
= SPECPDL_INDEX ();
3371 if (menu_items_n_panes
== 0)
3374 if (menu_items_used
<= MENU_ITEMS_PANE_LENGTH
)
3376 *error
= "Empty menu";
3380 /* Figure out which root window F is on. */
3381 XGetGeometry (FRAME_X_DISPLAY (f
), FRAME_X_WINDOW (f
), &root
,
3382 &dummy_int
, &dummy_int
, &dummy_uint
, &dummy_uint
,
3383 &dummy_uint
, &dummy_uint
);
3385 /* Make the menu on that window. */
3386 menu
= XMenuCreate (FRAME_X_DISPLAY (f
), root
, "emacs");
3389 *error
= "Can't create menu";
3393 /* Don't GC while we prepare and show the menu,
3394 because we give the oldxmenu library pointers to the
3395 contents of strings. */
3396 inhibit_garbage_collection ();
3398 #ifdef HAVE_X_WINDOWS
3399 /* Adjust coordinates to relative to the outer (window manager) window. */
3400 x
+= FRAME_OUTER_TO_INNER_DIFF_X (f
);
3401 y
+= FRAME_OUTER_TO_INNER_DIFF_Y (f
);
3402 #endif /* HAVE_X_WINDOWS */
3404 /* Adjust coordinates to be root-window-relative. */
3408 /* Create all the necessary panes and their items. */
3409 maxlines
= lines
= i
= 0;
3410 while (i
< menu_items_used
)
3412 if (EQ (XVECTOR (menu_items
)->contents
[i
], Qt
))
3414 /* Create a new pane. */
3415 Lisp_Object pane_name
, prefix
;
3418 maxlines
= max (maxlines
, lines
);
3420 pane_name
= XVECTOR (menu_items
)->contents
[i
+ MENU_ITEMS_PANE_NAME
];
3421 prefix
= XVECTOR (menu_items
)->contents
[i
+ MENU_ITEMS_PANE_PREFIX
];
3422 pane_string
= (NILP (pane_name
)
3423 ? "" : (char *) SDATA (pane_name
));
3424 if (keymaps
&& !NILP (prefix
))
3427 lpane
= XMenuAddPane (FRAME_X_DISPLAY (f
), menu
, pane_string
, TRUE
);
3428 if (lpane
== XM_FAILURE
)
3430 XMenuDestroy (FRAME_X_DISPLAY (f
), menu
);
3431 *error
= "Can't create pane";
3434 i
+= MENU_ITEMS_PANE_LENGTH
;
3436 /* Find the width of the widest item in this pane. */
3439 while (j
< menu_items_used
)
3442 item
= XVECTOR (menu_items
)->contents
[j
];
3450 width
= SBYTES (item
);
3451 if (width
> maxwidth
)
3454 j
+= MENU_ITEMS_ITEM_LENGTH
;
3457 /* Ignore a nil in the item list.
3458 It's meaningful only for dialog boxes. */
3459 else if (EQ (XVECTOR (menu_items
)->contents
[i
], Qquote
))
3463 /* Create a new item within current pane. */
3464 Lisp_Object item_name
, enable
, descrip
, help
;
3465 unsigned char *item_data
;
3468 item_name
= XVECTOR (menu_items
)->contents
[i
+ MENU_ITEMS_ITEM_NAME
];
3469 enable
= XVECTOR (menu_items
)->contents
[i
+ MENU_ITEMS_ITEM_ENABLE
];
3471 = XVECTOR (menu_items
)->contents
[i
+ MENU_ITEMS_ITEM_EQUIV_KEY
];
3472 help
= XVECTOR (menu_items
)->contents
[i
+ MENU_ITEMS_ITEM_HELP
];
3473 help_string
= STRINGP (help
) ? SDATA (help
) : NULL
;
3475 if (!NILP (descrip
))
3477 int gap
= maxwidth
- SBYTES (item_name
);
3480 spacer
= Fmake_string (make_number (gap
), make_number (' '));
3481 item_name
= concat2 (item_name
, spacer
);
3482 item_name
= concat2 (item_name
, descrip
);
3483 item_data
= SDATA (item_name
);
3485 /* if alloca is fast, use that to make the space,
3486 to reduce gc needs. */
3488 = (unsigned char *) alloca (maxwidth
3489 + SBYTES (descrip
) + 1);
3490 bcopy (SDATA (item_name
), item_data
,
3491 SBYTES (item_name
));
3492 for (j
= SCHARS (item_name
); j
< maxwidth
; j
++)
3494 bcopy (SDATA (descrip
), item_data
+ j
,
3496 item_data
[j
+ SBYTES (descrip
)] = 0;
3500 item_data
= SDATA (item_name
);
3502 if (XMenuAddSelection (FRAME_X_DISPLAY (f
),
3503 menu
, lpane
, 0, item_data
,
3504 !NILP (enable
), help_string
)
3507 XMenuDestroy (FRAME_X_DISPLAY (f
), menu
);
3508 *error
= "Can't add selection to menu";
3511 i
+= MENU_ITEMS_ITEM_LENGTH
;
3516 maxlines
= max (maxlines
, lines
);
3518 /* All set and ready to fly. */
3519 XMenuRecompute (FRAME_X_DISPLAY (f
), menu
);
3520 dispwidth
= DisplayWidth (FRAME_X_DISPLAY (f
), FRAME_X_SCREEN_NUMBER (f
));
3521 dispheight
= DisplayHeight (FRAME_X_DISPLAY (f
), FRAME_X_SCREEN_NUMBER (f
));
3522 x
= min (x
, dispwidth
);
3523 y
= min (y
, dispheight
);
3526 XMenuLocate (FRAME_X_DISPLAY (f
), menu
, 0, 0, x
, y
,
3527 &ulx
, &uly
, &width
, &height
);
3528 if (ulx
+width
> dispwidth
)
3530 x
-= (ulx
+ width
) - dispwidth
;
3531 ulx
= dispwidth
- width
;
3533 if (uly
+height
> dispheight
)
3535 y
-= (uly
+ height
) - dispheight
;
3536 uly
= dispheight
- height
;
3538 if (ulx
< 0) x
-= ulx
;
3539 if (uly
< 0) y
-= uly
;
3543 /* If position was not given by a mouse click, adjust so upper left
3544 corner of the menu as a whole ends up at given coordinates. This
3545 is what x-popup-menu says in its documentation. */
3547 y
+= 1.5*height
/(maxlines
+2);
3550 XMenuSetAEQ (menu
, TRUE
);
3551 XMenuSetFreeze (menu
, TRUE
);
3555 XMenuActivateSetWaitFunction (x_menu_wait_for_event
, FRAME_X_DISPLAY (f
));
3558 record_unwind_protect (pop_down_menu
,
3559 Fcons (make_save_value (f
, 0),
3560 make_save_value (menu
, 0)));
3562 /* Help display under X won't work because XMenuActivate contains
3563 a loop that doesn't give Emacs a chance to process it. */
3564 menu_help_frame
= f
;
3565 status
= XMenuActivate (FRAME_X_DISPLAY (f
), menu
, &pane
, &selidx
,
3566 x
, y
, ButtonReleaseMask
, &datap
,
3567 menu_help_callback
);
3573 fprintf (stderr
, "pane= %d line = %d\n", panes
, selidx
);
3576 /* Find the item number SELIDX in pane number PANE. */
3578 while (i
< menu_items_used
)
3580 if (EQ (XVECTOR (menu_items
)->contents
[i
], Qt
))
3584 = XVECTOR (menu_items
)->contents
[i
+ MENU_ITEMS_PANE_PREFIX
];
3586 i
+= MENU_ITEMS_PANE_LENGTH
;
3595 = XVECTOR (menu_items
)->contents
[i
+ MENU_ITEMS_ITEM_VALUE
];
3598 entry
= Fcons (entry
, Qnil
);
3599 if (!NILP (pane_prefix
))
3600 entry
= Fcons (pane_prefix
, entry
);
3606 i
+= MENU_ITEMS_ITEM_LENGTH
;
3612 *error
= "Can't activate menu";
3617 /* Make "Cancel" equivalent to C-g unless FOR_CLICK (which means
3618 the menu was invoked with a mouse event as POSITION). */
3620 Fsignal (Qquit
, Qnil
);
3625 unbind_to (specpdl_count
, Qnil
);
3630 #endif /* not USE_X_TOOLKIT */
3632 #endif /* HAVE_MENUS */
3637 staticpro (&menu_items
);
3639 menu_items_inuse
= Qnil
;
3641 Qdebug_on_next_call
= intern ("debug-on-next-call");
3642 staticpro (&Qdebug_on_next_call
);
3644 DEFVAR_LISP ("menu-updating-frame", &Vmenu_updating_frame
,
3645 doc
: /* Frame for which we are updating a menu.
3646 The enable predicate for a menu command should check this variable. */);
3647 Vmenu_updating_frame
= Qnil
;
3649 #ifdef USE_X_TOOLKIT
3650 widget_id_tick
= (1<<16);
3651 next_menubar_widget_id
= 1;
3654 defsubr (&Sx_popup_menu
);
3656 defsubr (&Sx_popup_dialog
);
3660 /* arch-tag: 92ea573c-398e-496e-ac73-2436f7d63242
3661 (do not change this comment) */