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, 2007 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 Qdebug_on_next_call
;
102 extern Lisp_Object Vmenu_updating_frame
;
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
));
333 /* Push the whole state of menu_items processing onto the specpdl.
334 It will be restored when the specpdl is unwound. */
339 Lisp_Object saved
= list4 (!NILP (menu_items_inuse
) ? menu_items
: Qnil
,
340 make_number (menu_items_used
),
341 make_number (menu_items_n_panes
),
342 make_number (menu_items_submenu_depth
));
343 record_unwind_protect (restore_menu_items
, saved
);
344 menu_items_inuse
= Qnil
;
348 /* Make the menu_items vector twice as large. */
354 int old_size
= menu_items_allocated
;
357 menu_items_allocated
*= 2;
359 menu_items
= Fmake_vector (make_number (menu_items_allocated
), Qnil
);
360 bcopy (XVECTOR (old
)->contents
, XVECTOR (menu_items
)->contents
,
361 old_size
* sizeof (Lisp_Object
));
364 /* Begin a submenu. */
367 push_submenu_start ()
369 if (menu_items_used
+ 1 > menu_items_allocated
)
372 XVECTOR (menu_items
)->contents
[menu_items_used
++] = Qnil
;
373 menu_items_submenu_depth
++;
381 if (menu_items_used
+ 1 > menu_items_allocated
)
384 XVECTOR (menu_items
)->contents
[menu_items_used
++] = Qlambda
;
385 menu_items_submenu_depth
--;
388 /* Indicate boundary between left and right. */
391 push_left_right_boundary ()
393 if (menu_items_used
+ 1 > menu_items_allocated
)
396 XVECTOR (menu_items
)->contents
[menu_items_used
++] = Qquote
;
399 /* Start a new menu pane in menu_items.
400 NAME is the pane name. PREFIX_VEC is a prefix key for this pane. */
403 push_menu_pane (name
, prefix_vec
)
404 Lisp_Object name
, prefix_vec
;
406 if (menu_items_used
+ MENU_ITEMS_PANE_LENGTH
> menu_items_allocated
)
409 if (menu_items_submenu_depth
== 0)
410 menu_items_n_panes
++;
411 XVECTOR (menu_items
)->contents
[menu_items_used
++] = Qt
;
412 XVECTOR (menu_items
)->contents
[menu_items_used
++] = name
;
413 XVECTOR (menu_items
)->contents
[menu_items_used
++] = prefix_vec
;
416 /* Push one menu item into the current pane. NAME is the string to
417 display. ENABLE if non-nil means this item can be selected. KEY
418 is the key generated by choosing this item, or nil if this item
419 doesn't really have a definition. DEF is the definition of this
420 item. EQUIV is the textual description of the keyboard equivalent
421 for this item (or nil if none). TYPE is the type of this menu
422 item, one of nil, `toggle' or `radio'. */
425 push_menu_item (name
, enable
, key
, def
, equiv
, type
, selected
, help
)
426 Lisp_Object name
, enable
, key
, def
, equiv
, type
, selected
, help
;
428 if (menu_items_used
+ MENU_ITEMS_ITEM_LENGTH
> menu_items_allocated
)
431 XVECTOR (menu_items
)->contents
[menu_items_used
++] = name
;
432 XVECTOR (menu_items
)->contents
[menu_items_used
++] = enable
;
433 XVECTOR (menu_items
)->contents
[menu_items_used
++] = key
;
434 XVECTOR (menu_items
)->contents
[menu_items_used
++] = equiv
;
435 XVECTOR (menu_items
)->contents
[menu_items_used
++] = def
;
436 XVECTOR (menu_items
)->contents
[menu_items_used
++] = type
;
437 XVECTOR (menu_items
)->contents
[menu_items_used
++] = selected
;
438 XVECTOR (menu_items
)->contents
[menu_items_used
++] = help
;
441 /* Look through KEYMAPS, a vector of keymaps that is NMAPS long,
442 and generate menu panes for them in menu_items.
443 If NOTREAL is nonzero,
444 don't bother really computing whether an item is enabled. */
447 keymap_panes (keymaps
, nmaps
, notreal
)
448 Lisp_Object
*keymaps
;
456 /* Loop over the given keymaps, making a pane for each map.
457 But don't make a pane that is empty--ignore that map instead.
458 P is the number of panes we have made so far. */
459 for (mapno
= 0; mapno
< nmaps
; mapno
++)
460 single_keymap_panes (keymaps
[mapno
],
461 Fkeymap_prompt (keymaps
[mapno
]), Qnil
, notreal
, 10);
463 finish_menu_items ();
466 /* Args passed between single_keymap_panes and single_menu_item. */
469 Lisp_Object pending_maps
;
470 int maxdepth
, notreal
;
474 static void single_menu_item
P_ ((Lisp_Object
, Lisp_Object
, Lisp_Object
,
477 /* This is a recursive subroutine of keymap_panes.
478 It handles one keymap, KEYMAP.
479 The other arguments are passed along
480 or point to local variables of the previous function.
481 If NOTREAL is nonzero, only check for equivalent key bindings, don't
482 evaluate expressions in menu items and don't make any menu.
484 If we encounter submenus deeper than MAXDEPTH levels, ignore them. */
487 single_keymap_panes (keymap
, pane_name
, prefix
, notreal
, maxdepth
)
489 Lisp_Object pane_name
;
497 skp
.pending_maps
= Qnil
;
498 skp
.maxdepth
= maxdepth
;
499 skp
.notreal
= notreal
;
505 push_menu_pane (pane_name
, prefix
);
508 /* Remember index for first item in this pane so we can go back and
509 add a prefix when (if) we see the first button. After that, notbuttons
510 is set to 0, to mark that we have seen a button and all non button
511 items need a prefix. */
512 skp
.notbuttons
= menu_items_used
;
515 GCPRO1 (skp
.pending_maps
);
516 map_keymap (keymap
, single_menu_item
, Qnil
, &skp
, 1);
519 /* Process now any submenus which want to be panes at this level. */
520 while (CONSP (skp
.pending_maps
))
522 Lisp_Object elt
, eltcdr
, string
;
523 elt
= XCAR (skp
.pending_maps
);
525 string
= XCAR (eltcdr
);
526 /* We no longer discard the @ from the beginning of the string here.
527 Instead, we do this in xmenu_show. */
528 single_keymap_panes (Fcar (elt
), string
,
529 XCDR (eltcdr
), notreal
, maxdepth
- 1);
530 skp
.pending_maps
= XCDR (skp
.pending_maps
);
534 /* This is a subroutine of single_keymap_panes that handles one
536 KEY is a key in a keymap and ITEM is its binding.
537 SKP->PENDING_MAPS_PTR is a list of keymaps waiting to be made into
539 If SKP->NOTREAL is nonzero, only check for equivalent key bindings, don't
540 evaluate expressions in menu items and don't make any menu.
541 If we encounter submenus deeper than SKP->MAXDEPTH levels, ignore them.
542 SKP->NOTBUTTONS is only used when simulating toggle boxes and radio
543 buttons. It keeps track of if we have seen a button in this menu or
547 single_menu_item (key
, item
, dummy
, skp_v
)
548 Lisp_Object key
, item
, dummy
;
551 Lisp_Object map
, item_string
, enabled
;
552 struct gcpro gcpro1
, gcpro2
;
554 struct skp
*skp
= skp_v
;
556 /* Parse the menu item and leave the result in item_properties. */
558 res
= parse_menu_item (item
, skp
->notreal
, 0);
561 return; /* Not a menu item. */
563 map
= XVECTOR (item_properties
)->contents
[ITEM_PROPERTY_MAP
];
567 /* We don't want to make a menu, just traverse the keymaps to
568 precompute equivalent key bindings. */
570 single_keymap_panes (map
, Qnil
, key
, 1, skp
->maxdepth
- 1);
574 enabled
= XVECTOR (item_properties
)->contents
[ITEM_PROPERTY_ENABLE
];
575 item_string
= XVECTOR (item_properties
)->contents
[ITEM_PROPERTY_NAME
];
577 if (!NILP (map
) && SREF (item_string
, 0) == '@')
580 /* An enabled separate pane. Remember this to handle it later. */
581 skp
->pending_maps
= Fcons (Fcons (map
, Fcons (item_string
, key
)),
587 /* Simulate radio buttons and toggle boxes by putting a prefix in
590 Lisp_Object prefix
= Qnil
;
591 Lisp_Object type
= XVECTOR (item_properties
)->contents
[ITEM_PROPERTY_TYPE
];
595 = XVECTOR (item_properties
)->contents
[ITEM_PROPERTY_SELECTED
];
598 /* The first button. Line up previous items in this menu. */
600 int index
= skp
->notbuttons
; /* Index for first item this menu. */
603 while (index
< menu_items_used
)
606 = XVECTOR (menu_items
)->contents
[index
+ MENU_ITEMS_ITEM_NAME
];
610 submenu
++; /* Skip sub menu. */
612 else if (EQ (tem
, Qlambda
))
615 submenu
--; /* End sub menu. */
617 else if (EQ (tem
, Qt
))
618 index
+= 3; /* Skip new pane marker. */
619 else if (EQ (tem
, Qquote
))
620 index
++; /* Skip a left, right divider. */
623 if (!submenu
&& SREF (tem
, 0) != '\0'
624 && SREF (tem
, 0) != '-')
625 XVECTOR (menu_items
)->contents
[index
+ MENU_ITEMS_ITEM_NAME
]
626 = concat2 (build_string (" "), tem
);
627 index
+= MENU_ITEMS_ITEM_LENGTH
;
633 /* Calculate prefix, if any, for this item. */
634 if (EQ (type
, QCtoggle
))
635 prefix
= build_string (NILP (selected
) ? "[ ] " : "[X] ");
636 else if (EQ (type
, QCradio
))
637 prefix
= build_string (NILP (selected
) ? "( ) " : "(*) ");
639 /* Not a button. If we have earlier buttons, then we need a prefix. */
640 else if (!skp
->notbuttons
&& SREF (item_string
, 0) != '\0'
641 && SREF (item_string
, 0) != '-')
642 prefix
= build_string (" ");
645 item_string
= concat2 (prefix
, item_string
);
647 #endif /* not HAVE_BOXES */
649 #if ! defined (USE_X_TOOLKIT) && ! defined (USE_GTK)
651 /* Indicate visually that this is a submenu. */
652 item_string
= concat2 (item_string
, build_string (" >"));
655 push_menu_item (item_string
, enabled
, key
,
656 XVECTOR (item_properties
)->contents
[ITEM_PROPERTY_DEF
],
657 XVECTOR (item_properties
)->contents
[ITEM_PROPERTY_KEYEQ
],
658 XVECTOR (item_properties
)->contents
[ITEM_PROPERTY_TYPE
],
659 XVECTOR (item_properties
)->contents
[ITEM_PROPERTY_SELECTED
],
660 XVECTOR (item_properties
)->contents
[ITEM_PROPERTY_HELP
]);
662 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
663 /* Display a submenu using the toolkit. */
664 if (! (NILP (map
) || NILP (enabled
)))
666 push_submenu_start ();
667 single_keymap_panes (map
, Qnil
, key
, 0, skp
->maxdepth
- 1);
673 /* Push all the panes and items of a menu described by the
674 alist-of-alists MENU.
675 This handles old-fashioned calls to x-popup-menu. */
685 for (tail
= menu
; CONSP (tail
); tail
= XCDR (tail
))
687 Lisp_Object elt
, pane_name
, pane_data
;
689 pane_name
= Fcar (elt
);
690 CHECK_STRING (pane_name
);
691 push_menu_pane (ENCODE_MENU_STRING (pane_name
), Qnil
);
692 pane_data
= Fcdr (elt
);
693 CHECK_CONS (pane_data
);
694 list_of_items (pane_data
);
697 finish_menu_items ();
700 /* Push the items in a single pane defined by the alist PANE. */
706 Lisp_Object tail
, item
, item1
;
708 for (tail
= pane
; CONSP (tail
); tail
= XCDR (tail
))
712 push_menu_item (ENCODE_MENU_STRING (item
), Qnil
, Qnil
, Qt
,
713 Qnil
, Qnil
, Qnil
, Qnil
);
714 else if (CONSP (item
))
717 CHECK_STRING (item1
);
718 push_menu_item (ENCODE_MENU_STRING (item1
), Qt
, XCDR (item
),
719 Qt
, Qnil
, Qnil
, Qnil
, Qnil
);
722 push_left_right_boundary ();
727 #ifdef HAVE_X_WINDOWS
728 /* Return the mouse position in *X and *Y. The coordinates are window
729 relative for the edit window in frame F.
730 This is for Fx_popup_menu. The mouse_position_hook can not
731 be used for X, as it returns window relative coordinates
732 for the window where the mouse is in. This could be the menu bar,
733 the scroll bar or the edit window. Fx_popup_menu needs to be
734 sure it is the edit window. */
736 mouse_position_for_popup (f
, x
, y
)
741 Window root
, dummy_window
;
746 XQueryPointer (FRAME_X_DISPLAY (f
),
747 DefaultRootWindow (FRAME_X_DISPLAY (f
)),
749 /* The root window which contains the pointer. */
752 /* Window pointer is on, not used */
755 /* The position on that root window. */
758 /* x/y in dummy_window coordinates, not used. */
761 /* Modifier keys and pointer buttons, about which
763 (unsigned int *) &dummy
);
767 /* xmenu_show expects window coordinates, not root window
768 coordinates. Translate. */
769 *x
-= f
->left_pos
+ FRAME_OUTER_TO_INNER_DIFF_X (f
);
770 *y
-= f
->top_pos
+ FRAME_OUTER_TO_INNER_DIFF_Y (f
);
773 #endif /* HAVE_X_WINDOWS */
775 DEFUN ("x-popup-menu", Fx_popup_menu
, Sx_popup_menu
, 2, 2, 0,
776 doc
: /* Pop up a deck-of-cards menu and return user's selection.
777 POSITION is a position specification. This is either a mouse button event
778 or a list ((XOFFSET YOFFSET) WINDOW)
779 where XOFFSET and YOFFSET are positions in pixels from the top left
780 corner of WINDOW. (WINDOW may be a window or a frame object.)
781 This controls the position of the top left of the menu as a whole.
782 If POSITION is t, it means to use the current mouse position.
784 MENU is a specifier for a menu. For the simplest case, MENU is a keymap.
785 The menu items come from key bindings that have a menu string as well as
786 a definition; actually, the "definition" in such a key binding looks like
787 \(STRING . REAL-DEFINITION). To give the menu a title, put a string into
788 the keymap as a top-level element.
790 If REAL-DEFINITION is nil, that puts a nonselectable string in the menu.
791 Otherwise, REAL-DEFINITION should be a valid key binding definition.
793 You can also use a list of keymaps as MENU.
794 Then each keymap makes a separate pane.
796 When MENU is a keymap or a list of keymaps, the return value is the
797 list of events corresponding to the user's choice. Note that
798 `x-popup-menu' does not actually execute the command bound to that
801 Alternatively, you can specify a menu of multiple panes
802 with a list of the form (TITLE PANE1 PANE2...),
803 where each pane is a list of form (TITLE ITEM1 ITEM2...).
804 Each ITEM is normally a cons cell (STRING . VALUE);
805 but a string can appear as an item--that makes a nonselectable line
807 With this form of menu, the return value is VALUE from the chosen item.
809 If POSITION is nil, don't display the menu at all, just precalculate the
810 cached information about equivalent key sequences.
812 If the user gets rid of the menu without making a valid choice, for
813 instance by clicking the mouse away from a valid choice or by typing
814 keyboard input, then this normally results in a quit and
815 `x-popup-menu' does not return. But if POSITION is a mouse button
816 event (indicating that the user invoked the menu with the mouse) then
817 no quit occurs and `x-popup-menu' returns nil. */)
819 Lisp_Object position
, menu
;
821 Lisp_Object keymap
, tem
;
822 int xpos
= 0, ypos
= 0;
824 char *error_name
= NULL
;
825 Lisp_Object selection
= Qnil
;
827 Lisp_Object x
, y
, window
;
830 int specpdl_count
= SPECPDL_INDEX ();
834 if (! NILP (position
))
836 int get_current_pos_p
= 0;
839 /* Decode the first argument: find the window and the coordinates. */
840 if (EQ (position
, Qt
)
841 || (CONSP (position
) && (EQ (XCAR (position
), Qmenu_bar
)
842 || EQ (XCAR (position
), Qtool_bar
))))
844 get_current_pos_p
= 1;
848 tem
= Fcar (position
);
851 window
= Fcar (Fcdr (position
));
853 y
= Fcar (XCDR (tem
));
858 tem
= Fcar (Fcdr (position
)); /* EVENT_START (position) */
859 window
= Fcar (tem
); /* POSN_WINDOW (tem) */
860 tem
= Fcar (Fcdr (Fcdr (tem
))); /* POSN_WINDOW_POSN (tem) */
865 /* If a click happens in an external tool bar or a detached
866 tool bar, x and y is NIL. In that case, use the current
867 mouse position. This happens for the help button in the
868 tool bar. Ideally popup-menu should pass NIL to
869 this function, but it doesn't. */
870 if (NILP (x
) && NILP (y
))
871 get_current_pos_p
= 1;
874 if (get_current_pos_p
)
876 /* Use the mouse's current position. */
877 FRAME_PTR new_f
= SELECTED_FRAME ();
878 #ifdef HAVE_X_WINDOWS
879 /* Can't use mouse_position_hook for X since it returns
880 coordinates relative to the window the mouse is in,
881 we need coordinates relative to the edit widget always. */
886 mouse_position_for_popup (new_f
, &cur_x
, &cur_y
);
887 /* cur_x/y may be negative, so use make_number. */
888 x
= make_number (cur_x
);
889 y
= make_number (cur_y
);
892 #else /* not HAVE_X_WINDOWS */
893 Lisp_Object bar_window
;
894 enum scroll_bar_part part
;
897 if (mouse_position_hook
)
898 (*mouse_position_hook
) (&new_f
, 1, &bar_window
,
899 &part
, &x
, &y
, &time
);
900 #endif /* not HAVE_X_WINDOWS */
903 XSETFRAME (window
, new_f
);
906 window
= selected_window
;
915 /* Decode where to put the menu. */
923 else if (WINDOWP (window
))
925 CHECK_LIVE_WINDOW (window
);
926 f
= XFRAME (WINDOW_FRAME (XWINDOW (window
)));
928 xpos
= WINDOW_LEFT_EDGE_X (XWINDOW (window
));
929 ypos
= WINDOW_TOP_EDGE_Y (XWINDOW (window
));
932 /* ??? Not really clean; should be CHECK_WINDOW_OR_FRAME,
933 but I don't want to make one now. */
934 CHECK_WINDOW (window
);
939 XSETFRAME (Vmenu_updating_frame
, f
);
942 Vmenu_updating_frame
= Qnil
;
943 #endif /* HAVE_MENUS */
945 record_unwind_protect (unuse_menu_items
, Qnil
);
949 /* Decode the menu items from what was specified. */
951 keymap
= get_keymap (menu
, 0, 0);
954 /* We were given a keymap. Extract menu info from the keymap. */
957 /* Extract the detailed info to make one pane. */
958 keymap_panes (&menu
, 1, NILP (position
));
960 /* Search for a string appearing directly as an element of the keymap.
961 That string is the title of the menu. */
962 prompt
= Fkeymap_prompt (keymap
);
963 if (NILP (title
) && !NILP (prompt
))
966 /* Make that be the pane title of the first pane. */
967 if (!NILP (prompt
) && menu_items_n_panes
>= 0)
968 XVECTOR (menu_items
)->contents
[MENU_ITEMS_PANE_NAME
] = prompt
;
972 else if (CONSP (menu
) && KEYMAPP (XCAR (menu
)))
974 /* We were given a list of keymaps. */
975 int nmaps
= XFASTINT (Flength (menu
));
977 = (Lisp_Object
*) alloca (nmaps
* sizeof (Lisp_Object
));
982 /* The first keymap that has a prompt string
983 supplies the menu title. */
984 for (tem
= menu
, i
= 0; CONSP (tem
); tem
= XCDR (tem
))
988 maps
[i
++] = keymap
= get_keymap (XCAR (tem
), 1, 0);
990 prompt
= Fkeymap_prompt (keymap
);
991 if (NILP (title
) && !NILP (prompt
))
995 /* Extract the detailed info to make one pane. */
996 keymap_panes (maps
, nmaps
, NILP (position
));
998 /* Make the title be the pane title of the first pane. */
999 if (!NILP (title
) && menu_items_n_panes
>= 0)
1000 XVECTOR (menu_items
)->contents
[MENU_ITEMS_PANE_NAME
] = title
;
1006 /* We were given an old-fashioned menu. */
1007 title
= Fcar (menu
);
1008 CHECK_STRING (title
);
1010 list_of_panes (Fcdr (menu
));
1015 unbind_to (specpdl_count
, Qnil
);
1017 if (NILP (position
))
1019 discard_menu_items ();
1025 /* Display them in a menu. */
1028 selection
= xmenu_show (f
, xpos
, ypos
, for_click
,
1029 keymaps
, title
, &error_name
);
1032 discard_menu_items ();
1035 #endif /* HAVE_MENUS */
1037 if (error_name
) error (error_name
);
1043 DEFUN ("x-popup-dialog", Fx_popup_dialog
, Sx_popup_dialog
, 2, 3, 0,
1044 doc
: /* Pop up a dialog box and return user's selection.
1045 POSITION specifies which frame to use.
1046 This is normally a mouse button event or a window or frame.
1047 If POSITION is t, it means to use the frame the mouse is on.
1048 The dialog box appears in the middle of the specified frame.
1050 CONTENTS specifies the alternatives to display in the dialog box.
1051 It is a list of the form (DIALOG ITEM1 ITEM2...).
1052 Each ITEM is a cons cell (STRING . VALUE).
1053 The return value is VALUE from the chosen item.
1055 An ITEM may also be just a string--that makes a nonselectable item.
1056 An ITEM may also be nil--that means to put all preceding items
1057 on the left of the dialog box and all following items on the right.
1058 \(By default, approximately half appear on each side.)
1060 If HEADER is non-nil, the frame title for the box is "Information",
1061 otherwise it is "Question".
1063 If the user gets rid of the dialog box without making a valid choice,
1064 for instance using the window manager, then this produces a quit and
1065 `x-popup-dialog' does not return. */)
1066 (position
, contents
, header
)
1067 Lisp_Object position
, contents
, header
;
1074 /* Decode the first argument: find the window or frame to use. */
1075 if (EQ (position
, Qt
)
1076 || (CONSP (position
) && (EQ (XCAR (position
), Qmenu_bar
)
1077 || EQ (XCAR (position
), Qtool_bar
))))
1079 #if 0 /* Using the frame the mouse is on may not be right. */
1080 /* Use the mouse's current position. */
1081 FRAME_PTR new_f
= SELECTED_FRAME ();
1082 Lisp_Object bar_window
;
1083 enum scroll_bar_part part
;
1087 (*mouse_position_hook
) (&new_f
, 1, &bar_window
, &part
, &x
, &y
, &time
);
1090 XSETFRAME (window
, new_f
);
1092 window
= selected_window
;
1094 window
= selected_window
;
1096 else if (CONSP (position
))
1099 tem
= Fcar (position
);
1101 window
= Fcar (Fcdr (position
));
1104 tem
= Fcar (Fcdr (position
)); /* EVENT_START (position) */
1105 window
= Fcar (tem
); /* POSN_WINDOW (tem) */
1108 else if (WINDOWP (position
) || FRAMEP (position
))
1113 /* Decode where to put the menu. */
1115 if (FRAMEP (window
))
1116 f
= XFRAME (window
);
1117 else if (WINDOWP (window
))
1119 CHECK_LIVE_WINDOW (window
);
1120 f
= XFRAME (WINDOW_FRAME (XWINDOW (window
)));
1123 /* ??? Not really clean; should be CHECK_WINDOW_OR_FRAME,
1124 but I don't want to make one now. */
1125 CHECK_WINDOW (window
);
1127 #if ! defined (USE_X_TOOLKIT) && ! defined (USE_GTK)
1128 /* Display a menu with these alternatives
1129 in the middle of frame F. */
1131 Lisp_Object x
, y
, frame
, newpos
;
1132 XSETFRAME (frame
, f
);
1133 XSETINT (x
, x_pixel_width (f
) / 2);
1134 XSETINT (y
, x_pixel_height (f
) / 2);
1135 newpos
= Fcons (Fcons (x
, Fcons (y
, Qnil
)), Fcons (frame
, Qnil
));
1137 return Fx_popup_menu (newpos
,
1138 Fcons (Fcar (contents
), Fcons (contents
, Qnil
)));
1144 Lisp_Object selection
;
1145 int specpdl_count
= SPECPDL_INDEX ();
1147 /* Decode the dialog items from what was specified. */
1148 title
= Fcar (contents
);
1149 CHECK_STRING (title
);
1150 record_unwind_protect (unuse_menu_items
, Qnil
);
1152 if (NILP (Fcar (Fcdr (contents
))))
1153 /* No buttons specified, add an "Ok" button so users can pop down
1154 the dialog. Also, the lesstif/motif version crashes if there are
1156 contents
= Fcons (title
, Fcons (Fcons (build_string ("Ok"), Qt
), Qnil
));
1158 list_of_panes (Fcons (contents
, Qnil
));
1160 /* Display them in a dialog box. */
1162 selection
= xdialog_show (f
, 0, title
, header
, &error_name
);
1165 unbind_to (specpdl_count
, Qnil
);
1166 discard_menu_items ();
1168 if (error_name
) error (error_name
);
1177 /* Set menu_items_inuse so no other popup menu or dialog is created. */
1180 x_menu_set_in_use (in_use
)
1183 menu_items_inuse
= in_use
? Qt
: Qnil
;
1184 popup_activated_flag
= in_use
;
1185 #ifdef USE_X_TOOLKIT
1186 if (popup_activated_flag
)
1187 x_activate_timeout_atimer ();
1191 /* Wait for an X event to arrive or for a timer to expire. */
1194 x_menu_wait_for_event (void *data
)
1196 extern EMACS_TIME timer_check
P_ ((int));
1198 /* Another way to do this is to register a timer callback, that can be
1199 done in GTK and Xt. But we have to do it like this when using only X
1200 anyway, and with callbacks we would have three variants for timer handling
1201 instead of the small ifdefs below. */
1204 #ifdef USE_X_TOOLKIT
1205 ! XtAppPending (Xt_app_con
)
1206 #elif defined USE_GTK
1207 ! gtk_events_pending ()
1209 ! XPending ((Display
*) data
)
1213 EMACS_TIME next_time
= timer_check (1);
1214 long secs
= EMACS_SECS (next_time
);
1215 long usecs
= EMACS_USECS (next_time
);
1216 SELECT_TYPE read_fds
;
1217 struct x_display_info
*dpyinfo
;
1220 FD_ZERO (&read_fds
);
1221 for (dpyinfo
= x_display_list
; dpyinfo
; dpyinfo
= dpyinfo
->next
)
1223 int fd
= ConnectionNumber (dpyinfo
->display
);
1224 FD_SET (fd
, &read_fds
);
1228 if (secs
< 0 || (secs
== 0 && usecs
== 0))
1230 /* Sometimes timer_check returns -1 (no timers) even if there are
1231 timers. So do a timeout anyway. */
1232 EMACS_SET_SECS (next_time
, 1);
1233 EMACS_SET_USECS (next_time
, 0);
1236 select (n
+ 1, &read_fds
, (SELECT_TYPE
*)0, (SELECT_TYPE
*)0, &next_time
);
1239 #endif /* ! MSDOS */
1242 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
1244 #ifdef USE_X_TOOLKIT
1246 /* Loop in Xt until the menu pulldown or dialog popup has been
1247 popped down (deactivated). This is used for x-popup-menu
1248 and x-popup-dialog; it is not used for the menu bar.
1250 NOTE: All calls to popup_get_selection should be protected
1251 with BLOCK_INPUT, UNBLOCK_INPUT wrappers. */
1254 popup_get_selection (initial_event
, dpyinfo
, id
, do_timers
)
1255 XEvent
*initial_event
;
1256 struct x_display_info
*dpyinfo
;
1262 while (popup_activated_flag
)
1266 event
= *initial_event
;
1271 if (do_timers
) x_menu_wait_for_event (0);
1272 XtAppNextEvent (Xt_app_con
, &event
);
1275 /* Make sure we don't consider buttons grabbed after menu goes.
1276 And make sure to deactivate for any ButtonRelease,
1277 even if XtDispatchEvent doesn't do that. */
1278 if (event
.type
== ButtonRelease
1279 && dpyinfo
->display
== event
.xbutton
.display
)
1281 dpyinfo
->grabbed
&= ~(1 << event
.xbutton
.button
);
1282 #ifdef USE_MOTIF /* Pretending that the event came from a
1283 Btn1Down seems the only way to convince Motif to
1284 activate its callbacks; setting the XmNmenuPost
1285 isn't working. --marcus@sysc.pdx.edu. */
1286 event
.xbutton
.button
= 1;
1287 /* Motif only pops down menus when no Ctrl, Alt or Mod
1288 key is pressed and the button is released. So reset key state
1289 so Motif thinks this is the case. */
1290 event
.xbutton
.state
= 0;
1293 /* Pop down on C-g and Escape. */
1294 else if (event
.type
== KeyPress
1295 && dpyinfo
->display
== event
.xbutton
.display
)
1297 KeySym keysym
= XLookupKeysym (&event
.xkey
, 0);
1299 if ((keysym
== XK_g
&& (event
.xkey
.state
& ControlMask
) != 0)
1300 || keysym
== XK_Escape
) /* Any escape, ignore modifiers. */
1301 popup_activated_flag
= 0;
1304 x_dispatch_event (&event
, event
.xany
.display
);
1308 DEFUN ("menu-bar-open", Fmenu_bar_open
, Smenu_bar_open
, 0, 1, "i",
1309 doc
: /* Start key navigation of the menu bar in FRAME.
1310 This initially opens the first menu bar item and you can then navigate with the
1311 arrow keys, select a menu entry with the return key or cancel with the
1312 escape key. If FRAME has no menu bar this function does nothing.
1314 If FRAME is nil or not given, use the selected frame. */)
1319 FRAME_PTR f
= check_x_frame (frame
);
1323 if (FRAME_EXTERNAL_MENU_BAR (f
))
1324 set_frame_menubar (f
, 0, 1);
1326 menubar
= FRAME_X_OUTPUT (f
)->menubar_widget
;
1332 x_catch_errors (FRAME_X_DISPLAY (f
));
1333 memset (&ev
, 0, sizeof ev
);
1334 ev
.xbutton
.display
= FRAME_X_DISPLAY (f
);
1335 ev
.xbutton
.window
= XtWindow (menubar
);
1336 ev
.xbutton
.root
= FRAME_X_DISPLAY_INFO (f
)->root_window
;
1337 ev
.xbutton
.time
= XtLastTimestampProcessed (FRAME_X_DISPLAY (f
));
1338 ev
.xbutton
.button
= Button1
;
1339 ev
.xbutton
.x
= ev
.xbutton
.y
= FRAME_MENUBAR_HEIGHT (f
) / 2;
1340 ev
.xbutton
.same_screen
= True
;
1347 XtSetArg (al
[0], XtNchildren
, &list
);
1348 XtSetArg (al
[1], XtNnumChildren
, &nr
);
1349 XtGetValues (menubar
, al
, 2);
1350 ev
.xbutton
.window
= XtWindow (list
[0]);
1354 XTranslateCoordinates (FRAME_X_DISPLAY (f
),
1355 /* From-window, to-window. */
1356 ev
.xbutton
.window
, ev
.xbutton
.root
,
1358 /* From-position, to-position. */
1359 ev
.xbutton
.x
, ev
.xbutton
.y
,
1360 &ev
.xbutton
.x_root
, &ev
.xbutton
.y_root
,
1364 error_p
= x_had_errors_p (FRAME_X_DISPLAY (f
));
1365 x_uncatch_errors ();
1369 ev
.type
= ButtonPress
;
1370 ev
.xbutton
.state
= 0;
1372 XtDispatchEvent (&ev
);
1373 ev
.xbutton
.type
= ButtonRelease
;
1374 ev
.xbutton
.state
= Button1Mask
;
1375 XtDispatchEvent (&ev
);
1383 #endif /* USE_X_TOOLKIT */
1387 DEFUN ("menu-bar-open", Fmenu_bar_open
, Smenu_bar_open
, 0, 1, "i",
1388 doc
: /* Start key navigation of the menu bar in FRAME.
1389 This initially opens the first menu bar item and you can then navigate with the
1390 arrow keys, select a menu entry with the return key or cancel with the
1391 escape key. If FRAME has no menu bar this function does nothing.
1393 If FRAME is nil or not given, use the selected frame. */)
1400 /* gcc 2.95 doesn't accept the FRAME_PTR declaration after
1404 f
= check_x_frame (frame
);
1406 if (FRAME_EXTERNAL_MENU_BAR (f
))
1407 set_frame_menubar (f
, 0, 1);
1409 menubar
= FRAME_X_OUTPUT (f
)->menubar_widget
;
1412 /* Activate the first menu. */
1413 GList
*children
= gtk_container_get_children (GTK_CONTAINER (menubar
));
1415 gtk_menu_shell_select_item (GTK_MENU_SHELL (menubar
),
1416 GTK_WIDGET (children
->data
));
1418 popup_activated_flag
= 1;
1419 g_list_free (children
);
1426 /* Loop util popup_activated_flag is set to zero in a callback.
1427 Used for popup menus and dialogs. */
1430 popup_widget_loop (do_timers
, widget
)
1434 ++popup_activated_flag
;
1436 /* Process events in the Gtk event loop until done. */
1437 while (popup_activated_flag
)
1439 if (do_timers
) x_menu_wait_for_event (0);
1440 gtk_main_iteration ();
1445 /* Activate the menu bar of frame F.
1446 This is called from keyboard.c when it gets the
1447 MENU_BAR_ACTIVATE_EVENT out of the Emacs event queue.
1449 To activate the menu bar, we use the X button-press event
1450 that was saved in saved_menu_event.
1451 That makes the toolkit do its thing.
1453 But first we recompute the menu bar contents (the whole tree).
1455 The reason for saving the button event until here, instead of
1456 passing it to the toolkit right away, is that we can safely
1457 execute Lisp code. */
1460 x_activate_menubar (f
)
1463 if (!f
->output_data
.x
->saved_menu_event
->type
)
1467 if (! xg_win_to_widget (FRAME_X_DISPLAY (f
),
1468 f
->output_data
.x
->saved_menu_event
->xany
.window
))
1472 set_frame_menubar (f
, 0, 1);
1475 XPutBackEvent (f
->output_data
.x
->display_info
->display
,
1476 f
->output_data
.x
->saved_menu_event
);
1477 popup_activated_flag
= 1;
1479 XtDispatchEvent (f
->output_data
.x
->saved_menu_event
);
1483 if (f
->output_data
.x
->saved_menu_event
->type
== ButtonRelease
)
1484 pending_menu_activation
= 1;
1487 /* Ignore this if we get it a second time. */
1488 f
->output_data
.x
->saved_menu_event
->type
= 0;
1491 /* This callback is invoked when the user selects a menubar cascade
1492 pushbutton, but before the pulldown menu is posted. */
1496 popup_activate_callback (widget
, id
, client_data
)
1499 XtPointer client_data
;
1501 popup_activated_flag
= 1;
1502 #ifdef USE_X_TOOLKIT
1503 x_activate_timeout_atimer ();
1508 /* This callback is invoked when a dialog or menu is finished being
1509 used and has been unposted. */
1513 popup_deactivate_callback (widget
, client_data
)
1515 gpointer client_data
;
1517 popup_activated_flag
= 0;
1521 popup_deactivate_callback (widget
, id
, client_data
)
1524 XtPointer client_data
;
1526 popup_activated_flag
= 0;
1531 /* Function that finds the frame for WIDGET and shows the HELP text
1533 F is the frame if known, or NULL if not known. */
1535 show_help_event (f
, widget
, help
)
1537 xt_or_gtk_widget widget
;
1544 XSETFRAME (frame
, f
);
1545 kbd_buffer_store_help_event (frame
, help
);
1549 #if 0 /* This code doesn't do anything useful. ++kfs */
1550 /* WIDGET is the popup menu. It's parent is the frame's
1551 widget. See which frame that is. */
1552 xt_or_gtk_widget frame_widget
= XtParent (widget
);
1555 for (tail
= Vframe_list
; GC_CONSP (tail
); tail
= XCDR (tail
))
1557 frame
= XCAR (tail
);
1558 if (GC_FRAMEP (frame
)
1559 && (f
= XFRAME (frame
),
1560 FRAME_X_P (f
) && f
->output_data
.x
->widget
== frame_widget
))
1564 show_help_echo (help
, Qnil
, Qnil
, Qnil
, 1);
1568 /* Callback called when menu items are highlighted/unhighlighted
1569 while moving the mouse over them. WIDGET is the menu bar or menu
1570 popup widget. ID is its LWLIB_ID. CALL_DATA contains a pointer to
1571 the data structure for the menu item, or null in case of
1576 menu_highlight_callback (widget
, call_data
)
1580 xg_menu_item_cb_data
*cb_data
;
1583 cb_data
= (xg_menu_item_cb_data
*) g_object_get_data (G_OBJECT (widget
),
1585 if (! cb_data
) return;
1587 help
= call_data
? cb_data
->help
: Qnil
;
1589 /* If popup_activated_flag is greater than 1 we are in a popup menu.
1590 Don't show help for them, they won't appear before the
1591 popup is popped down. */
1592 if (popup_activated_flag
<= 1)
1593 show_help_event (cb_data
->cl_data
->f
, widget
, help
);
1597 menu_highlight_callback (widget
, id
, call_data
)
1605 widget_value
*wv
= (widget_value
*) call_data
;
1607 help
= wv
? wv
->help
: Qnil
;
1609 /* Determine the frame for the help event. */
1610 f
= menubar_id_to_frame (id
);
1612 show_help_event (f
, widget
, help
);
1616 /* Find the menu selection and store it in the keyboard buffer.
1617 F is the frame the menu is on.
1618 MENU_BAR_ITEMS_USED is the length of VECTOR.
1619 VECTOR is an array of menu events for the whole menu. */
1622 find_and_call_menu_selection (f
, menu_bar_items_used
, vector
, client_data
)
1624 int menu_bar_items_used
;
1628 Lisp_Object prefix
, entry
;
1629 Lisp_Object
*subprefix_stack
;
1630 int submenu_depth
= 0;
1634 subprefix_stack
= (Lisp_Object
*) alloca (menu_bar_items_used
* sizeof (Lisp_Object
));
1638 while (i
< menu_bar_items_used
)
1640 if (EQ (XVECTOR (vector
)->contents
[i
], Qnil
))
1642 subprefix_stack
[submenu_depth
++] = prefix
;
1646 else if (EQ (XVECTOR (vector
)->contents
[i
], Qlambda
))
1648 prefix
= subprefix_stack
[--submenu_depth
];
1651 else if (EQ (XVECTOR (vector
)->contents
[i
], Qt
))
1653 prefix
= XVECTOR (vector
)->contents
[i
+ MENU_ITEMS_PANE_PREFIX
];
1654 i
+= MENU_ITEMS_PANE_LENGTH
;
1658 entry
= XVECTOR (vector
)->contents
[i
+ MENU_ITEMS_ITEM_VALUE
];
1659 /* The EMACS_INT cast avoids a warning. There's no problem
1660 as long as pointers have enough bits to hold small integers. */
1661 if ((int) (EMACS_INT
) client_data
== i
)
1664 struct input_event buf
;
1668 XSETFRAME (frame
, f
);
1669 buf
.kind
= MENU_BAR_EVENT
;
1670 buf
.frame_or_window
= frame
;
1672 kbd_buffer_store_event (&buf
);
1674 for (j
= 0; j
< submenu_depth
; j
++)
1675 if (!NILP (subprefix_stack
[j
]))
1677 buf
.kind
= MENU_BAR_EVENT
;
1678 buf
.frame_or_window
= frame
;
1679 buf
.arg
= subprefix_stack
[j
];
1680 kbd_buffer_store_event (&buf
);
1685 buf
.kind
= MENU_BAR_EVENT
;
1686 buf
.frame_or_window
= frame
;
1688 kbd_buffer_store_event (&buf
);
1691 buf
.kind
= MENU_BAR_EVENT
;
1692 buf
.frame_or_window
= frame
;
1694 kbd_buffer_store_event (&buf
);
1698 i
+= MENU_ITEMS_ITEM_LENGTH
;
1705 /* Gtk calls callbacks just because we tell it what item should be
1706 selected in a radio group. If this variable is set to a non-zero
1707 value, we are creating menus and don't want callbacks right now.
1709 static int xg_crazy_callback_abort
;
1711 /* This callback is called from the menu bar pulldown menu
1712 when the user makes a selection.
1713 Figure out what the user chose
1714 and put the appropriate events into the keyboard buffer. */
1716 menubar_selection_callback (widget
, client_data
)
1718 gpointer client_data
;
1720 xg_menu_item_cb_data
*cb_data
= (xg_menu_item_cb_data
*) client_data
;
1722 if (xg_crazy_callback_abort
)
1725 if (! cb_data
|| ! cb_data
->cl_data
|| ! cb_data
->cl_data
->f
)
1728 /* For a group of radio buttons, GTK calls the selection callback first
1729 for the item that was active before the selection and then for the one that
1730 is active after the selection. For C-h k this means we get the help on
1731 the deselected item and then the selected item is executed. Prevent that
1732 by ignoring the non-active item. */
1733 if (GTK_IS_RADIO_MENU_ITEM (widget
)
1734 && ! gtk_check_menu_item_get_active (GTK_CHECK_MENU_ITEM (widget
)))
1737 /* When a menu is popped down, X generates a focus event (i.e. focus
1738 goes back to the frame below the menu). Since GTK buffers events,
1739 we force it out here before the menu selection event. Otherwise
1740 sit-for will exit at once if the focus event follows the menu selection
1744 while (gtk_events_pending ())
1745 gtk_main_iteration ();
1748 find_and_call_menu_selection (cb_data
->cl_data
->f
,
1749 cb_data
->cl_data
->menu_bar_items_used
,
1750 cb_data
->cl_data
->menu_bar_vector
,
1751 cb_data
->call_data
);
1754 #else /* not USE_GTK */
1756 /* This callback is called from the menu bar pulldown menu
1757 when the user makes a selection.
1758 Figure out what the user chose
1759 and put the appropriate events into the keyboard buffer. */
1761 menubar_selection_callback (widget
, id
, client_data
)
1764 XtPointer client_data
;
1768 f
= menubar_id_to_frame (id
);
1771 find_and_call_menu_selection (f
, f
->menu_bar_items_used
,
1772 f
->menu_bar_vector
, client_data
);
1774 #endif /* not USE_GTK */
1776 /* Allocate a widget_value, blocking input. */
1779 xmalloc_widget_value ()
1781 widget_value
*value
;
1784 value
= malloc_widget_value ();
1790 /* This recursively calls free_widget_value on the tree of widgets.
1791 It must free all data that was malloc'ed for these widget_values.
1792 In Emacs, many slots are pointers into the data of Lisp_Strings, and
1793 must be left alone. */
1796 free_menubar_widget_value_tree (wv
)
1801 wv
->name
= wv
->value
= wv
->key
= (char *) 0xDEADBEEF;
1803 if (wv
->contents
&& (wv
->contents
!= (widget_value
*)1))
1805 free_menubar_widget_value_tree (wv
->contents
);
1806 wv
->contents
= (widget_value
*) 0xDEADBEEF;
1810 free_menubar_widget_value_tree (wv
->next
);
1811 wv
->next
= (widget_value
*) 0xDEADBEEF;
1814 free_widget_value (wv
);
1818 /* Set up data in menu_items for a menu bar item
1819 whose event type is ITEM_KEY (with string ITEM_NAME)
1820 and whose contents come from the list of keymaps MAPS. */
1823 parse_single_submenu (item_key
, item_name
, maps
)
1824 Lisp_Object item_key
, item_name
, maps
;
1828 Lisp_Object
*mapvec
;
1830 int top_level_items
= 0;
1832 length
= Flength (maps
);
1833 len
= XINT (length
);
1835 /* Convert the list MAPS into a vector MAPVEC. */
1836 mapvec
= (Lisp_Object
*) alloca (len
* sizeof (Lisp_Object
));
1837 for (i
= 0; i
< len
; i
++)
1839 mapvec
[i
] = Fcar (maps
);
1843 /* Loop over the given keymaps, making a pane for each map.
1844 But don't make a pane that is empty--ignore that map instead. */
1845 for (i
= 0; i
< len
; i
++)
1847 if (!KEYMAPP (mapvec
[i
]))
1849 /* Here we have a command at top level in the menu bar
1850 as opposed to a submenu. */
1851 top_level_items
= 1;
1852 push_menu_pane (Qnil
, Qnil
);
1853 push_menu_item (item_name
, Qt
, item_key
, mapvec
[i
],
1854 Qnil
, Qnil
, Qnil
, Qnil
);
1859 prompt
= Fkeymap_prompt (mapvec
[i
]);
1860 single_keymap_panes (mapvec
[i
],
1861 !NILP (prompt
) ? prompt
: item_name
,
1866 return top_level_items
;
1869 /* Create a tree of widget_value objects
1870 representing the panes and items
1871 in menu_items starting at index START, up to index END. */
1873 static widget_value
*
1874 digest_single_submenu (start
, end
, top_level_items
)
1875 int start
, end
, top_level_items
;
1877 widget_value
*wv
, *prev_wv
, *save_wv
, *first_wv
;
1879 int submenu_depth
= 0;
1880 widget_value
**submenu_stack
;
1884 = (widget_value
**) alloca (menu_items_used
* sizeof (widget_value
*));
1885 wv
= xmalloc_widget_value ();
1889 wv
->button_type
= BUTTON_TYPE_NONE
;
1895 /* Loop over all panes and items made by the preceding call
1896 to parse_single_submenu and construct a tree of widget_value objects.
1897 Ignore the panes and items used by previous calls to
1898 digest_single_submenu, even though those are also in menu_items. */
1902 if (EQ (XVECTOR (menu_items
)->contents
[i
], Qnil
))
1904 submenu_stack
[submenu_depth
++] = save_wv
;
1909 else if (EQ (XVECTOR (menu_items
)->contents
[i
], Qlambda
))
1912 save_wv
= submenu_stack
[--submenu_depth
];
1915 else if (EQ (XVECTOR (menu_items
)->contents
[i
], Qt
)
1916 && submenu_depth
!= 0)
1917 i
+= MENU_ITEMS_PANE_LENGTH
;
1918 /* Ignore a nil in the item list.
1919 It's meaningful only for dialog boxes. */
1920 else if (EQ (XVECTOR (menu_items
)->contents
[i
], Qquote
))
1922 else if (EQ (XVECTOR (menu_items
)->contents
[i
], Qt
))
1924 /* Create a new pane. */
1925 Lisp_Object pane_name
, prefix
;
1930 pane_name
= XVECTOR (menu_items
)->contents
[i
+ MENU_ITEMS_PANE_NAME
];
1931 prefix
= XVECTOR (menu_items
)->contents
[i
+ MENU_ITEMS_PANE_PREFIX
];
1933 #ifndef HAVE_MULTILINGUAL_MENU
1934 if (STRINGP (pane_name
) && STRING_MULTIBYTE (pane_name
))
1936 pane_name
= ENCODE_MENU_STRING (pane_name
);
1937 AREF (menu_items
, i
+ MENU_ITEMS_PANE_NAME
) = pane_name
;
1940 pane_string
= (NILP (pane_name
)
1941 ? "" : (char *) SDATA (pane_name
));
1942 /* If there is just one top-level pane, put all its items directly
1943 under the top-level menu. */
1944 if (menu_items_n_panes
== 1)
1947 /* If the pane has a meaningful name,
1948 make the pane a top-level menu item
1949 with its items as a submenu beneath it. */
1950 if (strcmp (pane_string
, ""))
1952 wv
= xmalloc_widget_value ();
1956 first_wv
->contents
= wv
;
1957 wv
->lname
= pane_name
;
1958 /* Set value to 1 so update_submenu_strings can handle '@' */
1959 wv
->value
= (char *)1;
1961 wv
->button_type
= BUTTON_TYPE_NONE
;
1969 i
+= MENU_ITEMS_PANE_LENGTH
;
1973 /* Create a new item within current pane. */
1974 Lisp_Object item_name
, enable
, descrip
, def
, type
, selected
;
1977 /* All items should be contained in panes. */
1978 if (panes_seen
== 0)
1981 item_name
= AREF (menu_items
, i
+ MENU_ITEMS_ITEM_NAME
);
1982 enable
= AREF (menu_items
, i
+ MENU_ITEMS_ITEM_ENABLE
);
1983 descrip
= AREF (menu_items
, i
+ MENU_ITEMS_ITEM_EQUIV_KEY
);
1984 def
= AREF (menu_items
, i
+ MENU_ITEMS_ITEM_DEFINITION
);
1985 type
= AREF (menu_items
, i
+ MENU_ITEMS_ITEM_TYPE
);
1986 selected
= AREF (menu_items
, i
+ MENU_ITEMS_ITEM_SELECTED
);
1987 help
= AREF (menu_items
, i
+ MENU_ITEMS_ITEM_HELP
);
1989 #ifndef HAVE_MULTILINGUAL_MENU
1990 if (STRING_MULTIBYTE (item_name
))
1992 item_name
= ENCODE_MENU_STRING (item_name
);
1993 AREF (menu_items
, i
+ MENU_ITEMS_ITEM_NAME
) = item_name
;
1996 if (STRINGP (descrip
) && STRING_MULTIBYTE (descrip
))
1998 descrip
= ENCODE_MENU_STRING (descrip
);
1999 AREF (menu_items
, i
+ MENU_ITEMS_ITEM_EQUIV_KEY
) = descrip
;
2001 #endif /* not HAVE_MULTILINGUAL_MENU */
2003 wv
= xmalloc_widget_value ();
2007 save_wv
->contents
= wv
;
2009 wv
->lname
= item_name
;
2010 if (!NILP (descrip
))
2013 /* The EMACS_INT cast avoids a warning. There's no problem
2014 as long as pointers have enough bits to hold small integers. */
2015 wv
->call_data
= (!NILP (def
) ? (void *) (EMACS_INT
) i
: 0);
2016 wv
->enabled
= !NILP (enable
);
2019 wv
->button_type
= BUTTON_TYPE_NONE
;
2020 else if (EQ (type
, QCradio
))
2021 wv
->button_type
= BUTTON_TYPE_RADIO
;
2022 else if (EQ (type
, QCtoggle
))
2023 wv
->button_type
= BUTTON_TYPE_TOGGLE
;
2027 wv
->selected
= !NILP (selected
);
2028 if (! STRINGP (help
))
2035 i
+= MENU_ITEMS_ITEM_LENGTH
;
2039 /* If we have just one "menu item"
2040 that was originally a button, return it by itself. */
2041 if (top_level_items
&& first_wv
->contents
&& first_wv
->contents
->next
== 0)
2043 wv
= first_wv
->contents
;
2044 free_widget_value (first_wv
);
2051 /* Walk through the widget_value tree starting at FIRST_WV and update
2052 the char * pointers from the corresponding lisp values.
2053 We do this after building the whole tree, since GC may happen while the
2054 tree is constructed, and small strings are relocated. So we must wait
2055 until no GC can happen before storing pointers into lisp values. */
2057 update_submenu_strings (first_wv
)
2058 widget_value
*first_wv
;
2062 for (wv
= first_wv
; wv
; wv
= wv
->next
)
2064 if (STRINGP (wv
->lname
))
2066 wv
->name
= (char *) SDATA (wv
->lname
);
2068 /* Ignore the @ that means "separate pane".
2069 This is a kludge, but this isn't worth more time. */
2070 if (wv
->value
== (char *)1)
2072 if (wv
->name
[0] == '@')
2078 if (STRINGP (wv
->lkey
))
2079 wv
->key
= (char *) SDATA (wv
->lkey
);
2082 update_submenu_strings (wv
->contents
);
2087 /* Recompute all the widgets of frame F, when the menu bar has been
2088 changed. Value is non-zero if widgets were updated. */
2091 update_frame_menubar (f
)
2095 return xg_update_frame_menubar (f
);
2097 struct x_output
*x
= f
->output_data
.x
;
2100 if (!x
->menubar_widget
|| XtIsManaged (x
->menubar_widget
))
2104 /* Save the size of the frame because the pane widget doesn't accept
2105 to resize itself. So force it. */
2106 columns
= FRAME_COLS (f
);
2107 rows
= FRAME_LINES (f
);
2109 /* Do the voodoo which means "I'm changing lots of things, don't try
2110 to refigure sizes until I'm done." */
2111 lw_refigure_widget (x
->column_widget
, False
);
2113 /* The order in which children are managed is the top to bottom
2114 order in which they are displayed in the paned window. First,
2115 remove the text-area widget. */
2116 XtUnmanageChild (x
->edit_widget
);
2118 /* Remove the menubar that is there now, and put up the menubar that
2120 XtManageChild (x
->menubar_widget
);
2121 XtMapWidget (x
->menubar_widget
);
2122 XtVaSetValues (x
->menubar_widget
, XtNmappedWhenManaged
, 1, NULL
);
2124 /* Re-manage the text-area widget, and then thrash the sizes. */
2125 XtManageChild (x
->edit_widget
);
2126 lw_refigure_widget (x
->column_widget
, True
);
2128 /* Force the pane widget to resize itself with the right values. */
2129 EmacsFrameSetCharSize (x
->edit_widget
, columns
, rows
);
2135 /* Set the contents of the menubar widgets of frame F.
2136 The argument FIRST_TIME is currently ignored;
2137 it is set the first time this is called, from initialize_frame_menubar. */
2140 set_frame_menubar (f
, first_time
, deep_p
)
2145 xt_or_gtk_widget menubar_widget
= f
->output_data
.x
->menubar_widget
;
2146 #ifdef USE_X_TOOLKIT
2150 widget_value
*wv
, *first_wv
, *prev_wv
= 0;
2152 int *submenu_start
, *submenu_end
;
2153 int *submenu_top_level_items
, *submenu_n_panes
;
2156 XSETFRAME (Vmenu_updating_frame
, f
);
2158 #ifdef USE_X_TOOLKIT
2159 if (f
->output_data
.x
->id
== 0)
2160 f
->output_data
.x
->id
= next_menubar_widget_id
++;
2161 id
= f
->output_data
.x
->id
;
2164 if (! menubar_widget
)
2166 else if (pending_menu_activation
&& !deep_p
)
2168 /* Make the first call for any given frame always go deep. */
2169 else if (!f
->output_data
.x
->saved_menu_event
&& !deep_p
)
2172 f
->output_data
.x
->saved_menu_event
= (XEvent
*)xmalloc (sizeof (XEvent
));
2173 f
->output_data
.x
->saved_menu_event
->type
= 0;
2177 /* If we have detached menus, we must update deep so detached menus
2178 also gets updated. */
2179 deep_p
= deep_p
|| xg_have_tear_offs ();
2184 /* Make a widget-value tree representing the entire menu trees. */
2186 struct buffer
*prev
= current_buffer
;
2188 int specpdl_count
= SPECPDL_INDEX ();
2189 int previous_menu_items_used
= f
->menu_bar_items_used
;
2190 Lisp_Object
*previous_items
2191 = (Lisp_Object
*) alloca (previous_menu_items_used
2192 * sizeof (Lisp_Object
));
2194 /* If we are making a new widget, its contents are empty,
2195 do always reinitialize them. */
2196 if (! menubar_widget
)
2197 previous_menu_items_used
= 0;
2199 buffer
= XWINDOW (FRAME_SELECTED_WINDOW (f
))->buffer
;
2200 specbind (Qinhibit_quit
, Qt
);
2201 /* Don't let the debugger step into this code
2202 because it is not reentrant. */
2203 specbind (Qdebug_on_next_call
, Qnil
);
2205 record_unwind_save_match_data ();
2206 if (NILP (Voverriding_local_map_menu_flag
))
2208 specbind (Qoverriding_terminal_local_map
, Qnil
);
2209 specbind (Qoverriding_local_map
, Qnil
);
2212 set_buffer_internal_1 (XBUFFER (buffer
));
2214 /* Run the Lucid hook. */
2215 safe_run_hooks (Qactivate_menubar_hook
);
2217 /* If it has changed current-menubar from previous value,
2218 really recompute the menubar from the value. */
2219 if (! NILP (Vlucid_menu_bar_dirty_flag
))
2220 call0 (Qrecompute_lucid_menubar
);
2221 safe_run_hooks (Qmenu_bar_update_hook
);
2222 FRAME_MENU_BAR_ITEMS (f
) = menu_bar_items (FRAME_MENU_BAR_ITEMS (f
));
2224 items
= FRAME_MENU_BAR_ITEMS (f
);
2226 /* Save the frame's previous menu bar contents data. */
2227 if (previous_menu_items_used
)
2228 bcopy (XVECTOR (f
->menu_bar_vector
)->contents
, previous_items
,
2229 previous_menu_items_used
* sizeof (Lisp_Object
));
2231 /* Fill in menu_items with the current menu bar contents.
2232 This can evaluate Lisp code. */
2235 menu_items
= f
->menu_bar_vector
;
2236 menu_items_allocated
= VECTORP (menu_items
) ? ASIZE (menu_items
) : 0;
2237 submenu_start
= (int *) alloca (XVECTOR (items
)->size
* sizeof (int *));
2238 submenu_end
= (int *) alloca (XVECTOR (items
)->size
* sizeof (int *));
2239 submenu_n_panes
= (int *) alloca (XVECTOR (items
)->size
* sizeof (int));
2240 submenu_top_level_items
2241 = (int *) alloca (XVECTOR (items
)->size
* sizeof (int *));
2243 for (i
= 0; i
< XVECTOR (items
)->size
; i
+= 4)
2245 Lisp_Object key
, string
, maps
;
2249 key
= XVECTOR (items
)->contents
[i
];
2250 string
= XVECTOR (items
)->contents
[i
+ 1];
2251 maps
= XVECTOR (items
)->contents
[i
+ 2];
2255 submenu_start
[i
] = menu_items_used
;
2257 menu_items_n_panes
= 0;
2258 submenu_top_level_items
[i
]
2259 = parse_single_submenu (key
, string
, maps
);
2260 submenu_n_panes
[i
] = menu_items_n_panes
;
2262 submenu_end
[i
] = menu_items_used
;
2265 finish_menu_items ();
2267 /* Convert menu_items into widget_value trees
2268 to display the menu. This cannot evaluate Lisp code. */
2270 wv
= xmalloc_widget_value ();
2271 wv
->name
= "menubar";
2274 wv
->button_type
= BUTTON_TYPE_NONE
;
2278 for (i
= 0; i
< last_i
; i
+= 4)
2280 menu_items_n_panes
= submenu_n_panes
[i
];
2281 wv
= digest_single_submenu (submenu_start
[i
], submenu_end
[i
],
2282 submenu_top_level_items
[i
]);
2286 first_wv
->contents
= wv
;
2287 /* Don't set wv->name here; GC during the loop might relocate it. */
2289 wv
->button_type
= BUTTON_TYPE_NONE
;
2293 set_buffer_internal_1 (prev
);
2295 /* If there has been no change in the Lisp-level contents
2296 of the menu bar, skip redisplaying it. Just exit. */
2298 /* Compare the new menu items with the ones computed last time. */
2299 for (i
= 0; i
< previous_menu_items_used
; i
++)
2300 if (menu_items_used
== i
2301 || (!EQ (previous_items
[i
], XVECTOR (menu_items
)->contents
[i
])))
2303 if (i
== menu_items_used
&& i
== previous_menu_items_used
&& i
!= 0)
2305 /* The menu items have not changed. Don't bother updating
2306 the menus in any form, since it would be a no-op. */
2307 free_menubar_widget_value_tree (first_wv
);
2308 discard_menu_items ();
2309 unbind_to (specpdl_count
, Qnil
);
2313 /* The menu items are different, so store them in the frame. */
2314 f
->menu_bar_vector
= menu_items
;
2315 f
->menu_bar_items_used
= menu_items_used
;
2317 /* This calls restore_menu_items to restore menu_items, etc.,
2318 as they were outside. */
2319 unbind_to (specpdl_count
, Qnil
);
2321 /* Now GC cannot happen during the lifetime of the widget_value,
2322 so it's safe to store data from a Lisp_String. */
2323 wv
= first_wv
->contents
;
2324 for (i
= 0; i
< XVECTOR (items
)->size
; i
+= 4)
2327 string
= XVECTOR (items
)->contents
[i
+ 1];
2330 wv
->name
= (char *) SDATA (string
);
2331 update_submenu_strings (wv
->contents
);
2338 /* Make a widget-value tree containing
2339 just the top level menu bar strings. */
2341 wv
= xmalloc_widget_value ();
2342 wv
->name
= "menubar";
2345 wv
->button_type
= BUTTON_TYPE_NONE
;
2349 items
= FRAME_MENU_BAR_ITEMS (f
);
2350 for (i
= 0; i
< XVECTOR (items
)->size
; i
+= 4)
2354 string
= XVECTOR (items
)->contents
[i
+ 1];
2358 wv
= xmalloc_widget_value ();
2359 wv
->name
= (char *) SDATA (string
);
2362 wv
->button_type
= BUTTON_TYPE_NONE
;
2364 /* This prevents lwlib from assuming this
2365 menu item is really supposed to be empty. */
2366 /* The EMACS_INT cast avoids a warning.
2367 This value just has to be different from small integers. */
2368 wv
->call_data
= (void *) (EMACS_INT
) (-1);
2373 first_wv
->contents
= wv
;
2377 /* Forget what we thought we knew about what is in the
2378 detailed contents of the menu bar menus.
2379 Changing the top level always destroys the contents. */
2380 f
->menu_bar_items_used
= 0;
2383 /* Create or update the menu bar widget. */
2388 xg_crazy_callback_abort
= 1;
2391 /* The fourth arg is DEEP_P, which says to consider the entire
2392 menu trees we supply, rather than just the menu bar item names. */
2393 xg_modify_menubar_widgets (menubar_widget
,
2397 G_CALLBACK (menubar_selection_callback
),
2398 G_CALLBACK (popup_deactivate_callback
),
2399 G_CALLBACK (menu_highlight_callback
));
2403 GtkWidget
*wvbox
= f
->output_data
.x
->vbox_widget
;
2406 = xg_create_widget ("menubar", "menubar", f
, first_wv
,
2407 G_CALLBACK (menubar_selection_callback
),
2408 G_CALLBACK (popup_deactivate_callback
),
2409 G_CALLBACK (menu_highlight_callback
));
2411 f
->output_data
.x
->menubar_widget
= menubar_widget
;
2415 #else /* not USE_GTK */
2418 /* Disable resizing (done for Motif!) */
2419 lw_allow_resizing (f
->output_data
.x
->widget
, False
);
2421 /* The third arg is DEEP_P, which says to consider the entire
2422 menu trees we supply, rather than just the menu bar item names. */
2423 lw_modify_all_widgets (id
, first_wv
, deep_p
);
2425 /* Re-enable the edit widget to resize. */
2426 lw_allow_resizing (f
->output_data
.x
->widget
, True
);
2430 char menuOverride
[] = "Ctrl<KeyPress>g: MenuGadgetEscape()";
2431 XtTranslations override
= XtParseTranslationTable (menuOverride
);
2433 menubar_widget
= lw_create_widget ("menubar", "menubar", id
, first_wv
,
2434 f
->output_data
.x
->column_widget
,
2436 popup_activate_callback
,
2437 menubar_selection_callback
,
2438 popup_deactivate_callback
,
2439 menu_highlight_callback
);
2440 f
->output_data
.x
->menubar_widget
= menubar_widget
;
2442 /* Make menu pop down on C-g. */
2443 XtOverrideTranslations (menubar_widget
, override
);
2448 = (f
->output_data
.x
->menubar_widget
2449 ? (f
->output_data
.x
->menubar_widget
->core
.height
2450 + f
->output_data
.x
->menubar_widget
->core
.border_width
)
2453 #if 0 /* Experimentally, we now get the right results
2454 for -geometry -0-0 without this. 24 Aug 96, rms. */
2456 if (FRAME_EXTERNAL_MENU_BAR (f
))
2459 XtVaGetValues (f
->output_data
.x
->column_widget
,
2460 XtNinternalBorderWidth
, &ibw
, NULL
);
2461 menubar_size
+= ibw
;
2463 #endif /* USE_LUCID */
2466 f
->output_data
.x
->menubar_height
= menubar_size
;
2468 #endif /* not USE_GTK */
2470 free_menubar_widget_value_tree (first_wv
);
2471 update_frame_menubar (f
);
2474 xg_crazy_callback_abort
= 0;
2480 /* Called from Fx_create_frame to create the initial menubar of a frame
2481 before it is mapped, so that the window is mapped with the menubar already
2482 there instead of us tacking it on later and thrashing the window after it
2486 initialize_frame_menubar (f
)
2489 /* This function is called before the first chance to redisplay
2490 the frame. It has to be, so the frame will have the right size. */
2491 FRAME_MENU_BAR_ITEMS (f
) = menu_bar_items (FRAME_MENU_BAR_ITEMS (f
));
2492 set_frame_menubar (f
, 1, 1);
2496 /* Get rid of the menu bar of frame F, and free its storage.
2497 This is used when deleting a frame, and when turning off the menu bar.
2498 For GTK this function is in gtkutil.c. */
2502 free_frame_menubar (f
)
2505 Widget menubar_widget
;
2507 menubar_widget
= f
->output_data
.x
->menubar_widget
;
2509 f
->output_data
.x
->menubar_height
= 0;
2514 /* Removing the menu bar magically changes the shell widget's x
2515 and y position of (0, 0) which, when the menu bar is turned
2516 on again, leads to pull-down menuss appearing in strange
2517 positions near the upper-left corner of the display. This
2518 happens only with some window managers like twm and ctwm,
2519 but not with other like Motif's mwm or kwm, because the
2520 latter generate ConfigureNotify events when the menu bar
2521 is switched off, which fixes the shell position. */
2522 Position x0
, y0
, x1
, y1
;
2528 if (f
->output_data
.x
->widget
)
2529 XtVaGetValues (f
->output_data
.x
->widget
, XtNx
, &x0
, XtNy
, &y0
, NULL
);
2532 lw_destroy_all_widgets ((LWLIB_ID
) f
->output_data
.x
->id
);
2533 f
->output_data
.x
->menubar_widget
= NULL
;
2536 if (f
->output_data
.x
->widget
)
2538 XtVaGetValues (f
->output_data
.x
->widget
, XtNx
, &x1
, XtNy
, &y1
, NULL
);
2539 if (x1
== 0 && y1
== 0)
2540 XtVaSetValues (f
->output_data
.x
->widget
, XtNx
, x0
, XtNy
, y0
, NULL
);
2547 #endif /* not USE_GTK */
2549 #endif /* USE_X_TOOLKIT || USE_GTK */
2551 /* xmenu_show actually displays a menu using the panes and items in menu_items
2552 and returns the value selected from it.
2553 There are two versions of xmenu_show, one for Xt and one for Xlib.
2554 Both assume input is blocked by the caller. */
2556 /* F is the frame the menu is for.
2557 X and Y are the frame-relative specified position,
2558 relative to the inside upper left corner of the frame F.
2559 FOR_CLICK is nonzero if this menu was invoked for a mouse click.
2560 KEYMAPS is 1 if this menu was specified with keymaps;
2561 in that case, we return a list containing the chosen item's value
2562 and perhaps also the pane's prefix.
2563 TITLE is the specified menu title.
2564 ERROR is a place to store an error message string in case of failure.
2565 (We return nil on failure, but the value doesn't actually matter.) */
2567 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
2569 /* The item selected in the popup menu. */
2570 static Lisp_Object
*volatile menu_item_selection
;
2574 /* Used when position a popup menu. See menu_position_func and
2575 create_and_show_popup_menu below. */
2576 struct next_popup_x_y
2583 /* The menu position function to use if we are not putting a popup
2584 menu where the pointer is.
2585 MENU is the menu to pop up.
2586 X and Y shall on exit contain x/y where the menu shall pop up.
2587 PUSH_IN is not documented in the GTK manual.
2588 USER_DATA is any data passed in when calling gtk_menu_popup.
2589 Here it points to a struct next_popup_x_y where the coordinates
2590 to store in *X and *Y are as well as the frame for the popup.
2592 Here only X and Y are used. */
2594 menu_position_func (menu
, x
, y
, push_in
, user_data
)
2601 struct next_popup_x_y
* data
= (struct next_popup_x_y
*)user_data
;
2603 int disp_width
= FRAME_X_DISPLAY_INFO (data
->f
)->width
;
2604 int disp_height
= FRAME_X_DISPLAY_INFO (data
->f
)->height
;
2609 /* Check if there is room for the menu. If not, adjust x/y so that
2610 the menu is fully visible. */
2611 gtk_widget_size_request (GTK_WIDGET (menu
), &req
);
2612 if (data
->x
+ req
.width
> disp_width
)
2613 *x
-= data
->x
+ req
.width
- disp_width
;
2614 if (data
->y
+ req
.height
> disp_height
)
2615 *y
-= data
->y
+ req
.height
- disp_height
;
2619 popup_selection_callback (widget
, client_data
)
2621 gpointer client_data
;
2623 xg_menu_item_cb_data
*cb_data
= (xg_menu_item_cb_data
*) client_data
;
2625 if (xg_crazy_callback_abort
) return;
2626 if (cb_data
) menu_item_selection
= (Lisp_Object
*) cb_data
->call_data
;
2633 struct Lisp_Save_Value
*p
= XSAVE_VALUE (arg
);
2635 popup_activated_flag
= 0;
2637 gtk_widget_destroy (GTK_WIDGET (p
->pointer
));
2642 /* Pop up the menu for frame F defined by FIRST_WV at X/Y and loop until the
2644 menu_item_selection will be set to the selection. */
2646 create_and_show_popup_menu (f
, first_wv
, x
, y
, for_click
)
2648 widget_value
*first_wv
;
2655 GtkMenuPositionFunc pos_func
= 0; /* Pop up at pointer. */
2656 struct next_popup_x_y popup_x_y
;
2657 int specpdl_count
= SPECPDL_INDEX ();
2659 xg_crazy_callback_abort
= 1;
2660 menu
= xg_create_widget ("popup", first_wv
->name
, f
, first_wv
,
2661 G_CALLBACK (popup_selection_callback
),
2662 G_CALLBACK (popup_deactivate_callback
),
2663 G_CALLBACK (menu_highlight_callback
));
2664 xg_crazy_callback_abort
= 0;
2668 /* Not invoked by a click. pop up at x/y. */
2669 pos_func
= menu_position_func
;
2671 /* Adjust coordinates to be root-window-relative. */
2672 x
+= f
->left_pos
+ FRAME_OUTER_TO_INNER_DIFF_X (f
);
2673 y
+= f
->top_pos
+ FRAME_OUTER_TO_INNER_DIFF_Y (f
);
2679 i
= 0; /* gtk_menu_popup needs this to be 0 for a non-button popup. */
2683 for (i
= 0; i
< 5; i
++)
2684 if (FRAME_X_DISPLAY_INFO (f
)->grabbed
& (1 << i
))
2688 /* Display the menu. */
2689 gtk_widget_show_all (menu
);
2690 gtk_menu_popup (GTK_MENU (menu
), 0, 0, pos_func
, &popup_x_y
, i
, 0);
2692 record_unwind_protect (pop_down_menu
, make_save_value (menu
, 0));
2694 if (GTK_WIDGET_MAPPED (menu
))
2696 /* Set this to one. popup_widget_loop increases it by one, so it becomes
2697 two. show_help_echo uses this to detect popup menus. */
2698 popup_activated_flag
= 1;
2699 /* Process events that apply to the menu. */
2700 popup_widget_loop (1, menu
);
2703 unbind_to (specpdl_count
, Qnil
);
2705 /* Must reset this manually because the button release event is not passed
2706 to Emacs event loop. */
2707 FRAME_X_DISPLAY_INFO (f
)->grabbed
= 0;
2710 #else /* not USE_GTK */
2712 /* We need a unique id for each widget handled by the Lucid Widget
2715 For the main windows, and popup menus, we use this counter,
2716 which we increment each time after use. This starts from 1<<16.
2718 For menu bars, we use numbers starting at 0, counted in
2719 next_menubar_widget_id. */
2720 LWLIB_ID widget_id_tick
;
2723 popup_selection_callback (widget
, id
, client_data
)
2726 XtPointer client_data
;
2728 menu_item_selection
= (Lisp_Object
*) client_data
;
2731 /* ARG is the LWLIB ID of the dialog box, represented
2732 as a Lisp object as (HIGHPART . LOWPART). */
2738 LWLIB_ID id
= (XINT (XCAR (arg
)) << 4 * sizeof (LWLIB_ID
)
2739 | XINT (XCDR (arg
)));
2742 lw_destroy_all_widgets (id
);
2744 popup_activated_flag
= 0;
2749 /* Pop up the menu for frame F defined by FIRST_WV at X/Y and loop until the
2751 menu_item_selection will be set to the selection. */
2753 create_and_show_popup_menu (f
, first_wv
, x
, y
, for_click
)
2755 widget_value
*first_wv
;
2763 XButtonPressedEvent dummy
;
2767 menu_id
= widget_id_tick
++;
2768 menu
= lw_create_widget ("popup", first_wv
->name
, menu_id
, first_wv
,
2769 f
->output_data
.x
->widget
, 1, 0,
2770 popup_selection_callback
,
2771 popup_deactivate_callback
,
2772 menu_highlight_callback
);
2774 dummy
.type
= ButtonPress
;
2776 dummy
.send_event
= 0;
2777 dummy
.display
= FRAME_X_DISPLAY (f
);
2778 dummy
.time
= CurrentTime
;
2779 dummy
.root
= FRAME_X_DISPLAY_INFO (f
)->root_window
;
2780 dummy
.window
= dummy
.root
;
2781 dummy
.subwindow
= dummy
.root
;
2785 /* Adjust coordinates to be root-window-relative. */
2786 x
+= f
->left_pos
+ FRAME_OUTER_TO_INNER_DIFF_X (f
);
2787 y
+= f
->top_pos
+ FRAME_OUTER_TO_INNER_DIFF_Y (f
);
2794 for (i
= 0; i
< 5; i
++)
2795 if (FRAME_X_DISPLAY_INFO (f
)->grabbed
& (1 << i
))
2798 /* Don't allow any geometry request from the user. */
2799 XtSetArg (av
[ac
], XtNgeometry
, 0); ac
++;
2800 XtSetValues (menu
, av
, ac
);
2802 /* Display the menu. */
2803 lw_popup_menu (menu
, (XEvent
*) &dummy
);
2804 popup_activated_flag
= 1;
2805 x_activate_timeout_atimer ();
2808 int fact
= 4 * sizeof (LWLIB_ID
);
2809 int specpdl_count
= SPECPDL_INDEX ();
2810 record_unwind_protect (pop_down_menu
,
2811 Fcons (make_number (menu_id
>> (fact
)),
2812 make_number (menu_id
& ~(-1 << (fact
)))));
2814 /* Process events that apply to the menu. */
2815 popup_get_selection ((XEvent
*) 0, FRAME_X_DISPLAY_INFO (f
), menu_id
, 1);
2817 unbind_to (specpdl_count
, Qnil
);
2821 #endif /* not USE_GTK */
2824 xmenu_show (f
, x
, y
, for_click
, keymaps
, title
, error
)
2834 widget_value
*wv
, *save_wv
= 0, *first_wv
= 0, *prev_wv
= 0;
2835 widget_value
**submenu_stack
2836 = (widget_value
**) alloca (menu_items_used
* sizeof (widget_value
*));
2837 Lisp_Object
*subprefix_stack
2838 = (Lisp_Object
*) alloca (menu_items_used
* sizeof (Lisp_Object
));
2839 int submenu_depth
= 0;
2845 if (menu_items_used
<= MENU_ITEMS_PANE_LENGTH
)
2847 *error
= "Empty menu";
2851 /* Create a tree of widget_value objects
2852 representing the panes and their items. */
2853 wv
= xmalloc_widget_value ();
2857 wv
->button_type
= BUTTON_TYPE_NONE
;
2862 /* Loop over all panes and items, filling in the tree. */
2864 while (i
< menu_items_used
)
2866 if (EQ (XVECTOR (menu_items
)->contents
[i
], Qnil
))
2868 submenu_stack
[submenu_depth
++] = save_wv
;
2874 else if (EQ (XVECTOR (menu_items
)->contents
[i
], Qlambda
))
2877 save_wv
= submenu_stack
[--submenu_depth
];
2881 else if (EQ (XVECTOR (menu_items
)->contents
[i
], Qt
)
2882 && submenu_depth
!= 0)
2883 i
+= MENU_ITEMS_PANE_LENGTH
;
2884 /* Ignore a nil in the item list.
2885 It's meaningful only for dialog boxes. */
2886 else if (EQ (XVECTOR (menu_items
)->contents
[i
], Qquote
))
2888 else if (EQ (XVECTOR (menu_items
)->contents
[i
], Qt
))
2890 /* Create a new pane. */
2891 Lisp_Object pane_name
, prefix
;
2894 pane_name
= AREF (menu_items
, i
+ MENU_ITEMS_PANE_NAME
);
2895 prefix
= AREF (menu_items
, i
+ MENU_ITEMS_PANE_PREFIX
);
2897 #ifndef HAVE_MULTILINGUAL_MENU
2898 if (STRINGP (pane_name
) && STRING_MULTIBYTE (pane_name
))
2900 pane_name
= ENCODE_MENU_STRING (pane_name
);
2901 AREF (menu_items
, i
+ MENU_ITEMS_PANE_NAME
) = pane_name
;
2904 pane_string
= (NILP (pane_name
)
2905 ? "" : (char *) SDATA (pane_name
));
2906 /* If there is just one top-level pane, put all its items directly
2907 under the top-level menu. */
2908 if (menu_items_n_panes
== 1)
2911 /* If the pane has a meaningful name,
2912 make the pane a top-level menu item
2913 with its items as a submenu beneath it. */
2914 if (!keymaps
&& strcmp (pane_string
, ""))
2916 wv
= xmalloc_widget_value ();
2920 first_wv
->contents
= wv
;
2921 wv
->name
= pane_string
;
2922 if (keymaps
&& !NILP (prefix
))
2926 wv
->button_type
= BUTTON_TYPE_NONE
;
2931 else if (first_pane
)
2937 i
+= MENU_ITEMS_PANE_LENGTH
;
2941 /* Create a new item within current pane. */
2942 Lisp_Object item_name
, enable
, descrip
, def
, type
, selected
, help
;
2943 item_name
= AREF (menu_items
, i
+ MENU_ITEMS_ITEM_NAME
);
2944 enable
= AREF (menu_items
, i
+ MENU_ITEMS_ITEM_ENABLE
);
2945 descrip
= AREF (menu_items
, i
+ MENU_ITEMS_ITEM_EQUIV_KEY
);
2946 def
= AREF (menu_items
, i
+ MENU_ITEMS_ITEM_DEFINITION
);
2947 type
= AREF (menu_items
, i
+ MENU_ITEMS_ITEM_TYPE
);
2948 selected
= AREF (menu_items
, i
+ MENU_ITEMS_ITEM_SELECTED
);
2949 help
= AREF (menu_items
, i
+ MENU_ITEMS_ITEM_HELP
);
2951 #ifndef HAVE_MULTILINGUAL_MENU
2952 if (STRINGP (item_name
) && STRING_MULTIBYTE (item_name
))
2954 item_name
= ENCODE_MENU_STRING (item_name
);
2955 AREF (menu_items
, i
+ MENU_ITEMS_ITEM_NAME
) = item_name
;
2958 if (STRINGP (descrip
) && STRING_MULTIBYTE (descrip
))
2960 descrip
= ENCODE_MENU_STRING (descrip
);
2961 AREF (menu_items
, i
+ MENU_ITEMS_ITEM_EQUIV_KEY
) = descrip
;
2963 #endif /* not HAVE_MULTILINGUAL_MENU */
2965 wv
= xmalloc_widget_value ();
2969 save_wv
->contents
= wv
;
2970 wv
->name
= (char *) SDATA (item_name
);
2971 if (!NILP (descrip
))
2972 wv
->key
= (char *) SDATA (descrip
);
2974 /* If this item has a null value,
2975 make the call_data null so that it won't display a box
2976 when the mouse is on it. */
2978 = (!NILP (def
) ? (void *) &XVECTOR (menu_items
)->contents
[i
] : 0);
2979 wv
->enabled
= !NILP (enable
);
2982 wv
->button_type
= BUTTON_TYPE_NONE
;
2983 else if (EQ (type
, QCtoggle
))
2984 wv
->button_type
= BUTTON_TYPE_TOGGLE
;
2985 else if (EQ (type
, QCradio
))
2986 wv
->button_type
= BUTTON_TYPE_RADIO
;
2990 wv
->selected
= !NILP (selected
);
2992 if (! STRINGP (help
))
2999 i
+= MENU_ITEMS_ITEM_LENGTH
;
3003 /* Deal with the title, if it is non-nil. */
3006 widget_value
*wv_title
= xmalloc_widget_value ();
3007 widget_value
*wv_sep1
= xmalloc_widget_value ();
3008 widget_value
*wv_sep2
= xmalloc_widget_value ();
3010 wv_sep2
->name
= "--";
3011 wv_sep2
->next
= first_wv
->contents
;
3012 wv_sep2
->help
= Qnil
;
3014 wv_sep1
->name
= "--";
3015 wv_sep1
->next
= wv_sep2
;
3016 wv_sep1
->help
= Qnil
;
3018 #ifndef HAVE_MULTILINGUAL_MENU
3019 if (STRING_MULTIBYTE (title
))
3020 title
= ENCODE_MENU_STRING (title
);
3023 wv_title
->name
= (char *) SDATA (title
);
3024 wv_title
->enabled
= TRUE
;
3025 wv_title
->button_type
= BUTTON_TYPE_NONE
;
3026 wv_title
->next
= wv_sep1
;
3027 wv_title
->help
= Qnil
;
3028 first_wv
->contents
= wv_title
;
3031 /* No selection has been chosen yet. */
3032 menu_item_selection
= 0;
3034 /* Actually create and show the menu until popped down. */
3035 create_and_show_popup_menu (f
, first_wv
, x
, y
, for_click
);
3037 /* Free the widget_value objects we used to specify the contents. */
3038 free_menubar_widget_value_tree (first_wv
);
3040 /* Find the selected item, and its pane, to return
3041 the proper value. */
3042 if (menu_item_selection
!= 0)
3044 Lisp_Object prefix
, entry
;
3046 prefix
= entry
= Qnil
;
3048 while (i
< menu_items_used
)
3050 if (EQ (XVECTOR (menu_items
)->contents
[i
], Qnil
))
3052 subprefix_stack
[submenu_depth
++] = prefix
;
3056 else if (EQ (XVECTOR (menu_items
)->contents
[i
], Qlambda
))
3058 prefix
= subprefix_stack
[--submenu_depth
];
3061 else if (EQ (XVECTOR (menu_items
)->contents
[i
], Qt
))
3064 = XVECTOR (menu_items
)->contents
[i
+ MENU_ITEMS_PANE_PREFIX
];
3065 i
+= MENU_ITEMS_PANE_LENGTH
;
3067 /* Ignore a nil in the item list.
3068 It's meaningful only for dialog boxes. */
3069 else if (EQ (XVECTOR (menu_items
)->contents
[i
], Qquote
))
3074 = XVECTOR (menu_items
)->contents
[i
+ MENU_ITEMS_ITEM_VALUE
];
3075 if (menu_item_selection
== &XVECTOR (menu_items
)->contents
[i
])
3081 entry
= Fcons (entry
, Qnil
);
3083 entry
= Fcons (prefix
, entry
);
3084 for (j
= submenu_depth
- 1; j
>= 0; j
--)
3085 if (!NILP (subprefix_stack
[j
]))
3086 entry
= Fcons (subprefix_stack
[j
], entry
);
3090 i
+= MENU_ITEMS_ITEM_LENGTH
;
3094 else if (!for_click
)
3095 /* Make "Cancel" equivalent to C-g. */
3096 Fsignal (Qquit
, Qnil
);
3103 dialog_selection_callback (widget
, client_data
)
3105 gpointer client_data
;
3107 /* The EMACS_INT cast avoids a warning. There's no problem
3108 as long as pointers have enough bits to hold small integers. */
3109 if ((int) (EMACS_INT
) client_data
!= -1)
3110 menu_item_selection
= (Lisp_Object
*) client_data
;
3112 popup_activated_flag
= 0;
3115 /* Pop up the dialog for frame F defined by FIRST_WV and loop until the
3117 menu_item_selection will be set to the selection. */
3119 create_and_show_dialog (f
, first_wv
)
3121 widget_value
*first_wv
;
3125 menu
= xg_create_widget ("dialog", first_wv
->name
, f
, first_wv
,
3126 G_CALLBACK (dialog_selection_callback
),
3127 G_CALLBACK (popup_deactivate_callback
),
3132 int specpdl_count
= SPECPDL_INDEX ();
3133 record_unwind_protect (pop_down_menu
, make_save_value (menu
, 0));
3135 /* Display the menu. */
3136 gtk_widget_show_all (menu
);
3138 /* Process events that apply to the menu. */
3139 popup_widget_loop (1, menu
);
3141 unbind_to (specpdl_count
, Qnil
);
3145 #else /* not USE_GTK */
3147 dialog_selection_callback (widget
, id
, client_data
)
3150 XtPointer client_data
;
3152 /* The EMACS_INT cast avoids a warning. There's no problem
3153 as long as pointers have enough bits to hold small integers. */
3154 if ((int) (EMACS_INT
) client_data
!= -1)
3155 menu_item_selection
= (Lisp_Object
*) client_data
;
3158 lw_destroy_all_widgets (id
);
3160 popup_activated_flag
= 0;
3164 /* Pop up the dialog for frame F defined by FIRST_WV and loop until the
3166 menu_item_selection will be set to the selection. */
3168 create_and_show_dialog (f
, first_wv
)
3170 widget_value
*first_wv
;
3174 dialog_id
= widget_id_tick
++;
3175 lw_create_widget (first_wv
->name
, "dialog", dialog_id
, first_wv
,
3176 f
->output_data
.x
->widget
, 1, 0,
3177 dialog_selection_callback
, 0, 0);
3178 lw_modify_all_widgets (dialog_id
, first_wv
->contents
, True
);
3180 /* Display the dialog box. */
3181 lw_pop_up_all_widgets (dialog_id
);
3182 popup_activated_flag
= 1;
3183 x_activate_timeout_atimer ();
3185 /* Process events that apply to the dialog box.
3186 Also handle timers. */
3188 int count
= SPECPDL_INDEX ();
3189 int fact
= 4 * sizeof (LWLIB_ID
);
3191 /* xdialog_show_unwind is responsible for popping the dialog box down. */
3192 record_unwind_protect (pop_down_menu
,
3193 Fcons (make_number (dialog_id
>> (fact
)),
3194 make_number (dialog_id
& ~(-1 << (fact
)))));
3196 popup_get_selection ((XEvent
*) 0, FRAME_X_DISPLAY_INFO (f
),
3199 unbind_to (count
, Qnil
);
3203 #endif /* not USE_GTK */
3205 static char * button_names
[] = {
3206 "button1", "button2", "button3", "button4", "button5",
3207 "button6", "button7", "button8", "button9", "button10" };
3210 xdialog_show (f
, keymaps
, title
, header
, error_name
)
3213 Lisp_Object title
, header
;
3216 int i
, nb_buttons
=0;
3217 char dialog_name
[6];
3219 widget_value
*wv
, *first_wv
= 0, *prev_wv
= 0;
3221 /* Number of elements seen so far, before boundary. */
3223 /* 1 means we've seen the boundary between left-hand elts and right-hand. */
3224 int boundary_seen
= 0;
3228 if (menu_items_n_panes
> 1)
3230 *error_name
= "Multiple panes in dialog box";
3234 /* Create a tree of widget_value objects
3235 representing the text label and buttons. */
3237 Lisp_Object pane_name
, prefix
;
3239 pane_name
= XVECTOR (menu_items
)->contents
[MENU_ITEMS_PANE_NAME
];
3240 prefix
= XVECTOR (menu_items
)->contents
[MENU_ITEMS_PANE_PREFIX
];
3241 pane_string
= (NILP (pane_name
)
3242 ? "" : (char *) SDATA (pane_name
));
3243 prev_wv
= xmalloc_widget_value ();
3244 prev_wv
->value
= pane_string
;
3245 if (keymaps
&& !NILP (prefix
))
3247 prev_wv
->enabled
= 1;
3248 prev_wv
->name
= "message";
3249 prev_wv
->help
= Qnil
;
3252 /* Loop over all panes and items, filling in the tree. */
3253 i
= MENU_ITEMS_PANE_LENGTH
;
3254 while (i
< menu_items_used
)
3257 /* Create a new item within current pane. */
3258 Lisp_Object item_name
, enable
, descrip
;
3259 item_name
= XVECTOR (menu_items
)->contents
[i
+ MENU_ITEMS_ITEM_NAME
];
3260 enable
= XVECTOR (menu_items
)->contents
[i
+ MENU_ITEMS_ITEM_ENABLE
];
3262 = XVECTOR (menu_items
)->contents
[i
+ MENU_ITEMS_ITEM_EQUIV_KEY
];
3264 if (NILP (item_name
))
3266 free_menubar_widget_value_tree (first_wv
);
3267 *error_name
= "Submenu in dialog items";
3270 if (EQ (item_name
, Qquote
))
3272 /* This is the boundary between left-side elts
3273 and right-side elts. Stop incrementing right_count. */
3278 if (nb_buttons
>= 9)
3280 free_menubar_widget_value_tree (first_wv
);
3281 *error_name
= "Too many dialog items";
3285 wv
= xmalloc_widget_value ();
3287 wv
->name
= (char *) button_names
[nb_buttons
];
3288 if (!NILP (descrip
))
3289 wv
->key
= (char *) SDATA (descrip
);
3290 wv
->value
= (char *) SDATA (item_name
);
3291 wv
->call_data
= (void *) &XVECTOR (menu_items
)->contents
[i
];
3292 wv
->enabled
= !NILP (enable
);
3296 if (! boundary_seen
)
3300 i
+= MENU_ITEMS_ITEM_LENGTH
;
3303 /* If the boundary was not specified,
3304 by default put half on the left and half on the right. */
3305 if (! boundary_seen
)
3306 left_count
= nb_buttons
- nb_buttons
/ 2;
3308 wv
= xmalloc_widget_value ();
3309 wv
->name
= dialog_name
;
3312 /* Frame title: 'Q' = Question, 'I' = Information.
3313 Can also have 'E' = Error if, one day, we want
3314 a popup for errors. */
3316 dialog_name
[0] = 'Q';
3318 dialog_name
[0] = 'I';
3320 /* Dialog boxes use a really stupid name encoding
3321 which specifies how many buttons to use
3322 and how many buttons are on the right. */
3323 dialog_name
[1] = '0' + nb_buttons
;
3324 dialog_name
[2] = 'B';
3325 dialog_name
[3] = 'R';
3326 /* Number of buttons to put on the right. */
3327 dialog_name
[4] = '0' + nb_buttons
- left_count
;
3329 wv
->contents
= first_wv
;
3333 /* No selection has been chosen yet. */
3334 menu_item_selection
= 0;
3336 /* Actually create and show the dialog. */
3337 create_and_show_dialog (f
, first_wv
);
3339 /* Free the widget_value objects we used to specify the contents. */
3340 free_menubar_widget_value_tree (first_wv
);
3342 /* Find the selected item, and its pane, to return
3343 the proper value. */
3344 if (menu_item_selection
!= 0)
3350 while (i
< menu_items_used
)
3354 if (EQ (XVECTOR (menu_items
)->contents
[i
], Qt
))
3357 = XVECTOR (menu_items
)->contents
[i
+ MENU_ITEMS_PANE_PREFIX
];
3358 i
+= MENU_ITEMS_PANE_LENGTH
;
3360 else if (EQ (XVECTOR (menu_items
)->contents
[i
], Qquote
))
3362 /* This is the boundary between left-side elts and
3369 = XVECTOR (menu_items
)->contents
[i
+ MENU_ITEMS_ITEM_VALUE
];
3370 if (menu_item_selection
== &XVECTOR (menu_items
)->contents
[i
])
3374 entry
= Fcons (entry
, Qnil
);
3376 entry
= Fcons (prefix
, entry
);
3380 i
+= MENU_ITEMS_ITEM_LENGTH
;
3385 /* Make "Cancel" equivalent to C-g. */
3386 Fsignal (Qquit
, Qnil
);
3391 #else /* not USE_X_TOOLKIT && not USE_GTK */
3393 /* The frame of the last activated non-toolkit menu bar.
3394 Used to generate menu help events. */
3396 static struct frame
*menu_help_frame
;
3399 /* Show help HELP_STRING, or clear help if HELP_STRING is null.
3401 PANE is the pane number, and ITEM is the menu item number in
3402 the menu (currently not used).
3404 This cannot be done with generating a HELP_EVENT because
3405 XMenuActivate contains a loop that doesn't let Emacs process
3409 menu_help_callback (help_string
, pane
, item
)
3413 extern Lisp_Object Qmenu_item
;
3414 Lisp_Object
*first_item
;
3415 Lisp_Object pane_name
;
3416 Lisp_Object menu_object
;
3418 first_item
= XVECTOR (menu_items
)->contents
;
3419 if (EQ (first_item
[0], Qt
))
3420 pane_name
= first_item
[MENU_ITEMS_PANE_NAME
];
3421 else if (EQ (first_item
[0], Qquote
))
3422 /* This shouldn't happen, see xmenu_show. */
3423 pane_name
= empty_string
;
3425 pane_name
= first_item
[MENU_ITEMS_ITEM_NAME
];
3427 /* (menu-item MENU-NAME PANE-NUMBER) */
3428 menu_object
= Fcons (Qmenu_item
,
3430 Fcons (make_number (pane
), Qnil
)));
3431 show_help_echo (help_string
? build_string (help_string
) : Qnil
,
3432 Qnil
, menu_object
, make_number (item
), 1);
3439 struct Lisp_Save_Value
*p1
= XSAVE_VALUE (Fcar (arg
));
3440 struct Lisp_Save_Value
*p2
= XSAVE_VALUE (Fcdr (arg
));
3442 FRAME_PTR f
= p1
->pointer
;
3443 XMenu
*menu
= p2
->pointer
;
3447 XUngrabPointer (FRAME_X_DISPLAY (f
), CurrentTime
);
3448 XUngrabKeyboard (FRAME_X_DISPLAY (f
), CurrentTime
);
3450 XMenuDestroy (FRAME_X_DISPLAY (f
), menu
);
3452 #ifdef HAVE_X_WINDOWS
3453 /* Assume the mouse has moved out of the X window.
3454 If it has actually moved in, we will get an EnterNotify. */
3455 x_mouse_leave (FRAME_X_DISPLAY_INFO (f
));
3457 /* State that no mouse buttons are now held.
3458 (The oldXMenu code doesn't track this info for us.)
3459 That is not necessarily true, but the fiction leads to reasonable
3460 results, and it is a pain to ask which are actually held now. */
3461 FRAME_X_DISPLAY_INFO (f
)->grabbed
= 0;
3463 #endif /* HAVE_X_WINDOWS */
3472 xmenu_show (f
, x
, y
, for_click
, keymaps
, title
, error
)
3482 int pane
, selidx
, lpane
, status
;
3483 Lisp_Object entry
, pane_prefix
;
3485 int ulx
, uly
, width
, height
;
3486 int dispwidth
, dispheight
;
3487 int i
, j
, lines
, maxlines
;
3490 unsigned int dummy_uint
;
3491 int specpdl_count
= SPECPDL_INDEX ();
3494 if (menu_items_n_panes
== 0)
3497 if (menu_items_used
<= MENU_ITEMS_PANE_LENGTH
)
3499 *error
= "Empty menu";
3503 /* Figure out which root window F is on. */
3504 XGetGeometry (FRAME_X_DISPLAY (f
), FRAME_X_WINDOW (f
), &root
,
3505 &dummy_int
, &dummy_int
, &dummy_uint
, &dummy_uint
,
3506 &dummy_uint
, &dummy_uint
);
3508 /* Make the menu on that window. */
3509 menu
= XMenuCreate (FRAME_X_DISPLAY (f
), root
, "emacs");
3512 *error
= "Can't create menu";
3516 /* Don't GC while we prepare and show the menu,
3517 because we give the oldxmenu library pointers to the
3518 contents of strings. */
3519 inhibit_garbage_collection ();
3521 #ifdef HAVE_X_WINDOWS
3522 /* Adjust coordinates to relative to the outer (window manager) window. */
3523 x
+= FRAME_OUTER_TO_INNER_DIFF_X (f
);
3524 y
+= FRAME_OUTER_TO_INNER_DIFF_Y (f
);
3525 #endif /* HAVE_X_WINDOWS */
3527 /* Adjust coordinates to be root-window-relative. */
3531 /* Create all the necessary panes and their items. */
3532 maxlines
= lines
= i
= 0;
3533 while (i
< menu_items_used
)
3535 if (EQ (XVECTOR (menu_items
)->contents
[i
], Qt
))
3537 /* Create a new pane. */
3538 Lisp_Object pane_name
, prefix
;
3541 maxlines
= max (maxlines
, lines
);
3543 pane_name
= XVECTOR (menu_items
)->contents
[i
+ MENU_ITEMS_PANE_NAME
];
3544 prefix
= XVECTOR (menu_items
)->contents
[i
+ MENU_ITEMS_PANE_PREFIX
];
3545 pane_string
= (NILP (pane_name
)
3546 ? "" : (char *) SDATA (pane_name
));
3547 if (keymaps
&& !NILP (prefix
))
3550 lpane
= XMenuAddPane (FRAME_X_DISPLAY (f
), menu
, pane_string
, TRUE
);
3551 if (lpane
== XM_FAILURE
)
3553 XMenuDestroy (FRAME_X_DISPLAY (f
), menu
);
3554 *error
= "Can't create pane";
3557 i
+= MENU_ITEMS_PANE_LENGTH
;
3559 /* Find the width of the widest item in this pane. */
3562 while (j
< menu_items_used
)
3565 item
= XVECTOR (menu_items
)->contents
[j
];
3573 width
= SBYTES (item
);
3574 if (width
> maxwidth
)
3577 j
+= MENU_ITEMS_ITEM_LENGTH
;
3580 /* Ignore a nil in the item list.
3581 It's meaningful only for dialog boxes. */
3582 else if (EQ (XVECTOR (menu_items
)->contents
[i
], Qquote
))
3586 /* Create a new item within current pane. */
3587 Lisp_Object item_name
, enable
, descrip
, help
;
3588 unsigned char *item_data
;
3591 item_name
= XVECTOR (menu_items
)->contents
[i
+ MENU_ITEMS_ITEM_NAME
];
3592 enable
= XVECTOR (menu_items
)->contents
[i
+ MENU_ITEMS_ITEM_ENABLE
];
3594 = XVECTOR (menu_items
)->contents
[i
+ MENU_ITEMS_ITEM_EQUIV_KEY
];
3595 help
= XVECTOR (menu_items
)->contents
[i
+ MENU_ITEMS_ITEM_HELP
];
3596 help_string
= STRINGP (help
) ? SDATA (help
) : NULL
;
3598 if (!NILP (descrip
))
3600 int gap
= maxwidth
- SBYTES (item_name
);
3603 spacer
= Fmake_string (make_number (gap
), make_number (' '));
3604 item_name
= concat2 (item_name
, spacer
);
3605 item_name
= concat2 (item_name
, descrip
);
3606 item_data
= SDATA (item_name
);
3608 /* if alloca is fast, use that to make the space,
3609 to reduce gc needs. */
3611 = (unsigned char *) alloca (maxwidth
3612 + SBYTES (descrip
) + 1);
3613 bcopy (SDATA (item_name
), item_data
,
3614 SBYTES (item_name
));
3615 for (j
= SCHARS (item_name
); j
< maxwidth
; j
++)
3617 bcopy (SDATA (descrip
), item_data
+ j
,
3619 item_data
[j
+ SBYTES (descrip
)] = 0;
3623 item_data
= SDATA (item_name
);
3625 if (XMenuAddSelection (FRAME_X_DISPLAY (f
),
3626 menu
, lpane
, 0, item_data
,
3627 !NILP (enable
), help_string
)
3630 XMenuDestroy (FRAME_X_DISPLAY (f
), menu
);
3631 *error
= "Can't add selection to menu";
3634 i
+= MENU_ITEMS_ITEM_LENGTH
;
3639 maxlines
= max (maxlines
, lines
);
3641 /* All set and ready to fly. */
3642 XMenuRecompute (FRAME_X_DISPLAY (f
), menu
);
3643 dispwidth
= DisplayWidth (FRAME_X_DISPLAY (f
), FRAME_X_SCREEN_NUMBER (f
));
3644 dispheight
= DisplayHeight (FRAME_X_DISPLAY (f
), FRAME_X_SCREEN_NUMBER (f
));
3645 x
= min (x
, dispwidth
);
3646 y
= min (y
, dispheight
);
3649 XMenuLocate (FRAME_X_DISPLAY (f
), menu
, 0, 0, x
, y
,
3650 &ulx
, &uly
, &width
, &height
);
3651 if (ulx
+width
> dispwidth
)
3653 x
-= (ulx
+ width
) - dispwidth
;
3654 ulx
= dispwidth
- width
;
3656 if (uly
+height
> dispheight
)
3658 y
-= (uly
+ height
) - dispheight
;
3659 uly
= dispheight
- height
;
3661 if (ulx
< 0) x
-= ulx
;
3662 if (uly
< 0) y
-= uly
;
3666 /* If position was not given by a mouse click, adjust so upper left
3667 corner of the menu as a whole ends up at given coordinates. This
3668 is what x-popup-menu says in its documentation. */
3670 y
+= 1.5*height
/(maxlines
+2);
3673 XMenuSetAEQ (menu
, TRUE
);
3674 XMenuSetFreeze (menu
, TRUE
);
3678 XMenuActivateSetWaitFunction (x_menu_wait_for_event
, FRAME_X_DISPLAY (f
));
3681 record_unwind_protect (pop_down_menu
,
3682 Fcons (make_save_value (f
, 0),
3683 make_save_value (menu
, 0)));
3685 /* Help display under X won't work because XMenuActivate contains
3686 a loop that doesn't give Emacs a chance to process it. */
3687 menu_help_frame
= f
;
3688 status
= XMenuActivate (FRAME_X_DISPLAY (f
), menu
, &pane
, &selidx
,
3689 x
, y
, ButtonReleaseMask
, &datap
,
3690 menu_help_callback
);
3696 fprintf (stderr
, "pane= %d line = %d\n", panes
, selidx
);
3699 /* Find the item number SELIDX in pane number PANE. */
3701 while (i
< menu_items_used
)
3703 if (EQ (XVECTOR (menu_items
)->contents
[i
], Qt
))
3707 = XVECTOR (menu_items
)->contents
[i
+ MENU_ITEMS_PANE_PREFIX
];
3709 i
+= MENU_ITEMS_PANE_LENGTH
;
3718 = XVECTOR (menu_items
)->contents
[i
+ MENU_ITEMS_ITEM_VALUE
];
3721 entry
= Fcons (entry
, Qnil
);
3722 if (!NILP (pane_prefix
))
3723 entry
= Fcons (pane_prefix
, entry
);
3729 i
+= MENU_ITEMS_ITEM_LENGTH
;
3735 *error
= "Can't activate menu";
3740 /* Make "Cancel" equivalent to C-g unless FOR_CLICK (which means
3741 the menu was invoked with a mouse event as POSITION). */
3743 Fsignal (Qquit
, Qnil
);
3748 unbind_to (specpdl_count
, Qnil
);
3753 #endif /* not USE_X_TOOLKIT */
3755 #endif /* HAVE_MENUS */
3757 /* Detect if a dialog or menu has been posted. */
3762 return popup_activated_flag
;
3765 /* The following is used by delayed window autoselection. */
3767 DEFUN ("menu-or-popup-active-p", Fmenu_or_popup_active_p
, Smenu_or_popup_active_p
, 0, 0, 0,
3768 doc
: /* Return t if a menu or popup dialog is active. */)
3772 return (popup_activated ()) ? Qt
: Qnil
;
3775 #endif /* HAVE_MENUS */
3781 staticpro (&menu_items
);
3783 menu_items_inuse
= Qnil
;
3785 Qdebug_on_next_call
= intern ("debug-on-next-call");
3786 staticpro (&Qdebug_on_next_call
);
3788 #ifdef USE_X_TOOLKIT
3789 widget_id_tick
= (1<<16);
3790 next_menubar_widget_id
= 1;
3793 defsubr (&Sx_popup_menu
);
3794 defsubr (&Smenu_or_popup_active_p
);
3796 #if defined (USE_GTK) || defined (USE_X_TOOLKIT)
3797 defsubr (&Smenu_bar_open
);
3798 Ffset (intern ("accelerate-menu"), intern (Smenu_bar_open
.symbol_name
));
3802 defsubr (&Sx_popup_dialog
);
3806 /* arch-tag: 92ea573c-398e-496e-ac73-2436f7d63242
3807 (do not change this comment) */